]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
2012-06-18 Greta Yorsh <Greta.Yorsh@arm.com>
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
edcc5120 69#include "gdb_regex.h"
b775012e 70#include "ax-gdb.h"
c906108c 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
7371cf6d 80#include "python/python.h"
104c1213 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
816338b5
SS
88static void enable_count_command (char *, int);
89
a14ed312 90static void disable_command (char *, int);
c906108c 91
a14ed312 92static void enable_command (char *, int);
c906108c 93
95a42b64
TT
94static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
95 void *),
96 void *);
c906108c 97
a14ed312 98static void ignore_command (char *, int);
c906108c 99
4efb68b1 100static int breakpoint_re_set_one (void *);
c906108c 101
348d480f
PA
102static void breakpoint_re_set_default (struct breakpoint *);
103
983af33b
SDJ
104static void create_sals_from_address_default (char **,
105 struct linespec_result *,
106 enum bptype, char *,
107 char **);
108
109static void create_breakpoints_sal_default (struct gdbarch *,
110 struct linespec_result *,
111 struct linespec_sals *,
e7e0cddf 112 char *, char *, enum bptype,
983af33b
SDJ
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
44f238bb 116 int, int, int, unsigned);
983af33b
SDJ
117
118static void decode_linespec_default (struct breakpoint *, char **,
119 struct symtabs_and_lines *);
120
a14ed312 121static void clear_command (char *, int);
c906108c 122
a14ed312 123static void catch_command (char *, int);
c906108c 124
a9634178 125static int can_use_hardware_watchpoint (struct value *);
c906108c 126
98deb0da 127static void break_command_1 (char *, int, int);
c906108c 128
a14ed312 129static void mention (struct breakpoint *);
c906108c 130
348d480f
PA
131static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
132 enum bptype,
c0a91b2b 133 const struct breakpoint_ops *);
3742cc8b
YQ
134static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
135 const struct symtab_and_line *);
136
4a64f543
MS
137/* This function is used in gdbtk sources and thus can not be made
138 static. */
63c252f8 139struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 140 struct symtab_and_line,
c0a91b2b
TT
141 enum bptype,
142 const struct breakpoint_ops *);
c906108c 143
06edf0c0
PA
144static struct breakpoint *
145 momentary_breakpoint_from_master (struct breakpoint *orig,
146 enum bptype type,
c0a91b2b 147 const struct breakpoint_ops *ops);
06edf0c0 148
76897487
KB
149static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
150
a6d9a66e
UW
151static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
152 CORE_ADDR bpaddr,
88f7da05 153 enum bptype bptype);
76897487 154
6c95b8df
PA
155static void describe_other_breakpoints (struct gdbarch *,
156 struct program_space *, CORE_ADDR,
5af949e3 157 struct obj_section *, int);
c906108c 158
6c95b8df
PA
159static int breakpoint_address_match (struct address_space *aspace1,
160 CORE_ADDR addr1,
161 struct address_space *aspace2,
162 CORE_ADDR addr2);
163
85d721b8
PA
164static int watchpoint_locations_match (struct bp_location *loc1,
165 struct bp_location *loc2);
166
f1310107
TJB
167static int breakpoint_location_address_match (struct bp_location *bl,
168 struct address_space *aspace,
169 CORE_ADDR addr);
170
a14ed312 171static void breakpoints_info (char *, int);
c906108c 172
d77f58be
SS
173static void watchpoints_info (char *, int);
174
e5a67952
MS
175static int breakpoint_1 (char *, int,
176 int (*) (const struct breakpoint *));
c906108c 177
4efb68b1 178static int breakpoint_cond_eval (void *);
c906108c 179
4efb68b1 180static void cleanup_executing_breakpoints (void *);
c906108c 181
a14ed312 182static void commands_command (char *, int);
c906108c 183
a14ed312 184static void condition_command (char *, int);
c906108c 185
c5aa993b
JM
186typedef enum
187 {
188 mark_inserted,
189 mark_uninserted
190 }
191insertion_state_t;
c906108c 192
0bde7532 193static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 194static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 195
e514a9d6 196static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 197
4efb68b1 198static int watchpoint_check (void *);
c906108c 199
a14ed312 200static void maintenance_info_breakpoints (char *, int);
c906108c 201
a14ed312 202static int hw_breakpoint_used_count (void);
c906108c 203
a1398e0c
PA
204static int hw_watchpoint_use_count (struct breakpoint *);
205
206static int hw_watchpoint_used_count_others (struct breakpoint *except,
207 enum bptype type,
208 int *other_type_used);
c906108c 209
a14ed312 210static void hbreak_command (char *, int);
c906108c 211
a14ed312 212static void thbreak_command (char *, int);
c906108c 213
816338b5
SS
214static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
215 int count);
c906108c 216
a14ed312 217static void stop_command (char *arg, int from_tty);
7a292a7a 218
a14ed312 219static void stopin_command (char *arg, int from_tty);
7a292a7a 220
a14ed312 221static void stopat_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 224
d85310f7
MS
225static void catch_exception_command_1 (enum exception_event_kind ex_event,
226 char *arg, int tempflag, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
d03285ec
UW
230static void detach_single_step_breakpoints (void);
231
6c95b8df
PA
232static int single_step_breakpoint_inserted_here_p (struct address_space *,
233 CORE_ADDR pc);
1aafd4da 234
fe3f5fa8 235static void free_bp_location (struct bp_location *loc);
f431efe5
PA
236static void incref_bp_location (struct bp_location *loc);
237static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 238
39d61571 239static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 240
b60e7edf 241static void update_global_location_list (int);
a5606eee 242
b60e7edf 243static void update_global_location_list_nothrow (int);
74960c60 244
d77f58be 245static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
246
247static void insert_breakpoint_locations (void);
a5606eee 248
a96d9b2e
SDJ
249static int syscall_catchpoint_p (struct breakpoint *b);
250
1042e4c0
SS
251static void tracepoints_info (char *, int);
252
253static void delete_trace_command (char *, int);
254
255static void enable_trace_command (char *, int);
256
257static void disable_trace_command (char *, int);
258
259static void trace_pass_command (char *, int);
260
9c06b0b4
TJB
261static int is_masked_watchpoint (const struct breakpoint *b);
262
b775012e
LM
263static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
264
983af33b
SDJ
265/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
266 otherwise. */
267
268static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 269
edcc5120
TT
270static void init_catchpoint (struct breakpoint *b,
271 struct gdbarch *gdbarch, int tempflag,
272 char *cond_string,
273 const struct breakpoint_ops *ops);
274
2060206e
PA
275/* The abstract base class all breakpoint_ops structures inherit
276 from. */
277static struct breakpoint_ops base_breakpoint_ops;
278
279/* The breakpoint_ops structure to be inherited by all breakpoint_ops
280 that are implemented on top of software or hardware breakpoints
281 (user breakpoints, internal and momentary breakpoints, etc.). */
282static struct breakpoint_ops bkpt_base_breakpoint_ops;
283
284/* Internal breakpoints class type. */
06edf0c0 285static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
286
287/* Momentary breakpoints class type. */
06edf0c0
PA
288static struct breakpoint_ops momentary_breakpoint_ops;
289
2060206e
PA
290/* The breakpoint_ops structure to be used in regular user created
291 breakpoints. */
292struct breakpoint_ops bkpt_breakpoint_ops;
293
55aa24fb
SDJ
294/* Breakpoints set on probes. */
295static struct breakpoint_ops bkpt_probe_breakpoint_ops;
296
e7e0cddf
SS
297/* Dynamic printf class type. */
298static struct breakpoint_ops dprintf_breakpoint_ops;
299
5cea2a26
PA
300/* A reference-counted struct command_line. This lets multiple
301 breakpoints share a single command list. */
302struct counted_command_line
303{
304 /* The reference count. */
305 int refc;
306
307 /* The command list. */
308 struct command_line *commands;
309};
310
311struct command_line *
312breakpoint_commands (struct breakpoint *b)
313{
314 return b->commands ? b->commands->commands : NULL;
315}
3daf8fe5 316
f3b1572e
PA
317/* Flag indicating that a command has proceeded the inferior past the
318 current breakpoint. */
319
320static int breakpoint_proceeded;
321
956a9fb9 322const char *
2cec12e5
AR
323bpdisp_text (enum bpdisp disp)
324{
4a64f543
MS
325 /* NOTE: the following values are a part of MI protocol and
326 represent values of 'disp' field returned when inferior stops at
327 a breakpoint. */
bc043ef3 328 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 329
2cec12e5
AR
330 return bpdisps[(int) disp];
331}
c906108c 332
4a64f543 333/* Prototypes for exported functions. */
c906108c 334/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 335 if such is available. */
c906108c
SS
336static int can_use_hw_watchpoints;
337
920d2a44
AC
338static void
339show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342{
3e43a32a
MS
343 fprintf_filtered (file,
344 _("Debugger's willingness to use "
345 "watchpoint hardware is %s.\n"),
920d2a44
AC
346 value);
347}
348
fa8d40ab
JJ
349/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
350 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 351 for unrecognized breakpoint locations.
fa8d40ab
JJ
352 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
353static enum auto_boolean pending_break_support;
920d2a44
AC
354static void
355show_pending_break_support (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358{
3e43a32a
MS
359 fprintf_filtered (file,
360 _("Debugger's behavior regarding "
361 "pending breakpoints is %s.\n"),
920d2a44
AC
362 value);
363}
fa8d40ab 364
765dc015 365/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 366 set with "break" but falling in read-only memory.
765dc015
VP
367 If 0, gdb will warn about such breakpoints, but won't automatically
368 use hardware breakpoints. */
369static int automatic_hardware_breakpoints;
370static void
371show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
372 struct cmd_list_element *c,
373 const char *value)
374{
3e43a32a
MS
375 fprintf_filtered (file,
376 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
377 value);
378}
379
33e5cbd6
PA
380/* If on, gdb will keep breakpoints inserted even as inferior is
381 stopped, and immediately insert any new breakpoints. If off, gdb
382 will insert breakpoints into inferior only when resuming it, and
383 will remove breakpoints upon stop. If auto, GDB will behave as ON
384 if in non-stop mode, and as OFF if all-stop mode.*/
385
386static const char always_inserted_auto[] = "auto";
387static const char always_inserted_on[] = "on";
388static const char always_inserted_off[] = "off";
40478521 389static const char *const always_inserted_enums[] = {
33e5cbd6
PA
390 always_inserted_auto,
391 always_inserted_off,
392 always_inserted_on,
393 NULL
394};
395static const char *always_inserted_mode = always_inserted_auto;
396static void
74960c60 397show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 398 struct cmd_list_element *c, const char *value)
74960c60 399{
33e5cbd6 400 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
401 fprintf_filtered (file,
402 _("Always inserted breakpoint "
403 "mode is %s (currently %s).\n"),
33e5cbd6
PA
404 value,
405 breakpoints_always_inserted_mode () ? "on" : "off");
406 else
3e43a32a
MS
407 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
408 value);
74960c60
VP
409}
410
33e5cbd6
PA
411int
412breakpoints_always_inserted_mode (void)
413{
20388dd6
YQ
414 return (always_inserted_mode == always_inserted_on
415 || (always_inserted_mode == always_inserted_auto && non_stop));
33e5cbd6 416}
765dc015 417
b775012e
LM
418static const char condition_evaluation_both[] = "host or target";
419
420/* Modes for breakpoint condition evaluation. */
421static const char condition_evaluation_auto[] = "auto";
422static const char condition_evaluation_host[] = "host";
423static const char condition_evaluation_target[] = "target";
424static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429};
430
431/* Global that holds the current mode for breakpoint condition evaluation. */
432static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434/* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438/* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443static const char *
444translate_condition_evaluation_mode (const char *mode)
445{
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455}
456
457/* Discovers what condition_evaluation_auto translates to. */
458
459static const char *
460breakpoint_condition_evaluation_mode (void)
461{
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463}
464
465/* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468static int
469gdb_evaluates_breakpoint_condition_p (void)
470{
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474}
475
a14ed312 476void _initialize_breakpoint (void);
c906108c 477
c906108c
SS
478/* Are we executing breakpoint commands? */
479static int executing_breakpoint_commands;
480
c02f5703
MS
481/* Are overlay event breakpoints enabled? */
482static int overlay_events_enabled;
483
e09342b5
TJB
484/* See description in breakpoint.h. */
485int target_exact_watchpoints = 0;
486
c906108c 487/* Walk the following statement or block through all breakpoints.
e5dd4106 488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 489 current breakpoint. */
c906108c 490
5c44784c 491#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 492
5c44784c
JM
493#define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
c906108c 497
4a64f543
MS
498/* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 501
876fa593
JK
502#define ALL_BP_LOCATIONS(B,BP_TMP) \
503 for (BP_TMP = bp_location; \
504 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
505 BP_TMP++)
7cc221ef 506
b775012e
LM
507/* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
517 && (BP_LOCP_TMP < bp_location + bp_location_count \
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
1042e4c0
SS
521/* Iterator for tracepoints only. */
522
523#define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 525 if (is_tracepoint (B))
1042e4c0 526
7cc221ef 527/* Chains of all breakpoints defined. */
c906108c
SS
528
529struct breakpoint *breakpoint_chain;
530
876fa593
JK
531/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
532
533static struct bp_location **bp_location;
534
535/* Number of elements of BP_LOCATION. */
536
537static unsigned bp_location_count;
538
4a64f543
MS
539/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540 ADDRESS for the current elements of BP_LOCATION which get a valid
541 result from bp_location_has_shadow. You can use it for roughly
542 limiting the subrange of BP_LOCATION to scan for shadow bytes for
543 an address you need to read. */
876fa593
JK
544
545static CORE_ADDR bp_location_placed_address_before_address_max;
546
4a64f543
MS
547/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549 BP_LOCATION which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATION to
551 scan for shadow bytes for an address you need to read. */
876fa593
JK
552
553static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 554
4a64f543
MS
555/* The locations that no longer correspond to any breakpoint, unlinked
556 from bp_location array, but for which a hit may still be reported
557 by a target. */
20874c92
VP
558VEC(bp_location_p) *moribund_locations = NULL;
559
c906108c
SS
560/* Number of last breakpoint made. */
561
95a42b64
TT
562static int breakpoint_count;
563
86b17b60
PA
564/* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568static int prev_breakpoint_count;
c906108c 569
1042e4c0
SS
570/* Number of last tracepoint made. */
571
95a42b64 572static int tracepoint_count;
1042e4c0 573
6149aea9
PA
574static struct cmd_list_element *breakpoint_set_cmdlist;
575static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 576struct cmd_list_element *save_cmdlist;
6149aea9 577
468d015d
JJ
578/* Return whether a breakpoint is an active enabled breakpoint. */
579static int
580breakpoint_enabled (struct breakpoint *b)
581{
0d381245 582 return (b->enable_state == bp_enabled);
468d015d
JJ
583}
584
c906108c
SS
585/* Set breakpoint count to NUM. */
586
95a42b64 587static void
fba45db2 588set_breakpoint_count (int num)
c906108c 589{
86b17b60 590 prev_breakpoint_count = breakpoint_count;
c906108c 591 breakpoint_count = num;
4fa62494 592 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
593}
594
86b17b60
PA
595/* Used by `start_rbreak_breakpoints' below, to record the current
596 breakpoint count before "rbreak" creates any breakpoint. */
597static int rbreak_start_breakpoint_count;
598
95a42b64
TT
599/* Called at the start an "rbreak" command to record the first
600 breakpoint made. */
86b17b60 601
95a42b64
TT
602void
603start_rbreak_breakpoints (void)
604{
86b17b60 605 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
606}
607
608/* Called at the end of an "rbreak" command to record the last
609 breakpoint made. */
86b17b60 610
95a42b64
TT
611void
612end_rbreak_breakpoints (void)
613{
86b17b60 614 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
615}
616
4a64f543 617/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
618
619void
fba45db2 620clear_breakpoint_hit_counts (void)
c906108c
SS
621{
622 struct breakpoint *b;
623
624 ALL_BREAKPOINTS (b)
625 b->hit_count = 0;
626}
627
9add0f1b
TT
628/* Allocate a new counted_command_line with reference count of 1.
629 The new structure owns COMMANDS. */
630
631static struct counted_command_line *
632alloc_counted_command_line (struct command_line *commands)
633{
634 struct counted_command_line *result
635 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 636
9add0f1b
TT
637 result->refc = 1;
638 result->commands = commands;
639 return result;
640}
641
642/* Increment reference count. This does nothing if CMD is NULL. */
643
644static void
645incref_counted_command_line (struct counted_command_line *cmd)
646{
647 if (cmd)
648 ++cmd->refc;
649}
650
651/* Decrement reference count. If the reference count reaches 0,
652 destroy the counted_command_line. Sets *CMDP to NULL. This does
653 nothing if *CMDP is NULL. */
654
655static void
656decref_counted_command_line (struct counted_command_line **cmdp)
657{
658 if (*cmdp)
659 {
660 if (--(*cmdp)->refc == 0)
661 {
662 free_command_lines (&(*cmdp)->commands);
663 xfree (*cmdp);
664 }
665 *cmdp = NULL;
666 }
667}
668
669/* A cleanup function that calls decref_counted_command_line. */
670
671static void
672do_cleanup_counted_command_line (void *arg)
673{
674 decref_counted_command_line (arg);
675}
676
677/* Create a cleanup that calls decref_counted_command_line on the
678 argument. */
679
680static struct cleanup *
681make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
682{
683 return make_cleanup (do_cleanup_counted_command_line, cmdp);
684}
685
c906108c 686\f
48cb2d85
VP
687/* Return the breakpoint with the specified number, or NULL
688 if the number does not refer to an existing breakpoint. */
689
690struct breakpoint *
691get_breakpoint (int num)
692{
693 struct breakpoint *b;
694
695 ALL_BREAKPOINTS (b)
696 if (b->number == num)
697 return b;
698
699 return NULL;
700}
5c44784c 701
c906108c 702\f
adc36818 703
b775012e
LM
704/* Mark locations as "conditions have changed" in case the target supports
705 evaluating conditions on its side. */
706
707static void
708mark_breakpoint_modified (struct breakpoint *b)
709{
710 struct bp_location *loc;
711
712 /* This is only meaningful if the target is
713 evaluating conditions and if the user has
714 opted for condition evaluation on the target's
715 side. */
716 if (gdb_evaluates_breakpoint_condition_p ()
717 || !target_supports_evaluation_of_breakpoint_conditions ())
718 return;
719
720 if (!is_breakpoint (b))
721 return;
722
723 for (loc = b->loc; loc; loc = loc->next)
724 loc->condition_changed = condition_modified;
725}
726
727/* Mark location as "conditions have changed" in case the target supports
728 evaluating conditions on its side. */
729
730static void
731mark_breakpoint_location_modified (struct bp_location *loc)
732{
733 /* This is only meaningful if the target is
734 evaluating conditions and if the user has
735 opted for condition evaluation on the target's
736 side. */
737 if (gdb_evaluates_breakpoint_condition_p ()
738 || !target_supports_evaluation_of_breakpoint_conditions ())
739
740 return;
741
742 if (!is_breakpoint (loc->owner))
743 return;
744
745 loc->condition_changed = condition_modified;
746}
747
748/* Sets the condition-evaluation mode using the static global
749 condition_evaluation_mode. */
750
751static void
752set_condition_evaluation_mode (char *args, int from_tty,
753 struct cmd_list_element *c)
754{
b775012e
LM
755 const char *old_mode, *new_mode;
756
757 if ((condition_evaluation_mode_1 == condition_evaluation_target)
758 && !target_supports_evaluation_of_breakpoint_conditions ())
759 {
760 condition_evaluation_mode_1 = condition_evaluation_mode;
761 warning (_("Target does not support breakpoint condition evaluation.\n"
762 "Using host evaluation mode instead."));
763 return;
764 }
765
766 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
767 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
768
abf1152a
JK
769 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
770 settings was "auto". */
771 condition_evaluation_mode = condition_evaluation_mode_1;
772
b775012e
LM
773 /* Only update the mode if the user picked a different one. */
774 if (new_mode != old_mode)
775 {
776 struct bp_location *loc, **loc_tmp;
777 /* If the user switched to a different evaluation mode, we
778 need to synch the changes with the target as follows:
779
780 "host" -> "target": Send all (valid) conditions to the target.
781 "target" -> "host": Remove all the conditions from the target.
782 */
783
b775012e
LM
784 if (new_mode == condition_evaluation_target)
785 {
786 /* Mark everything modified and synch conditions with the
787 target. */
788 ALL_BP_LOCATIONS (loc, loc_tmp)
789 mark_breakpoint_location_modified (loc);
790 }
791 else
792 {
793 /* Manually mark non-duplicate locations to synch conditions
794 with the target. We do this to remove all the conditions the
795 target knows about. */
796 ALL_BP_LOCATIONS (loc, loc_tmp)
797 if (is_breakpoint (loc->owner) && loc->inserted)
798 loc->needs_update = 1;
799 }
800
801 /* Do the update. */
802 update_global_location_list (1);
803 }
804
805 return;
806}
807
808/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
809 what "auto" is translating to. */
810
811static void
812show_condition_evaluation_mode (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814{
815 if (condition_evaluation_mode == condition_evaluation_auto)
816 fprintf_filtered (file,
817 _("Breakpoint condition evaluation "
818 "mode is %s (currently %s).\n"),
819 value,
820 breakpoint_condition_evaluation_mode ());
821 else
822 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
823 value);
824}
825
826/* A comparison function for bp_location AP and BP that is used by
827 bsearch. This comparison function only cares about addresses, unlike
828 the more general bp_location_compare function. */
829
830static int
831bp_location_compare_addrs (const void *ap, const void *bp)
832{
833 struct bp_location *a = *(void **) ap;
834 struct bp_location *b = *(void **) bp;
835
836 if (a->address == b->address)
837 return 0;
838 else
839 return ((a->address > b->address) - (a->address < b->address));
840}
841
842/* Helper function to skip all bp_locations with addresses
843 less than ADDRESS. It returns the first bp_location that
844 is greater than or equal to ADDRESS. If none is found, just
845 return NULL. */
846
847static struct bp_location **
848get_first_locp_gte_addr (CORE_ADDR address)
849{
850 struct bp_location dummy_loc;
851 struct bp_location *dummy_locp = &dummy_loc;
852 struct bp_location **locp_found = NULL;
853
854 /* Initialize the dummy location's address field. */
855 memset (&dummy_loc, 0, sizeof (struct bp_location));
856 dummy_loc.address = address;
857
858 /* Find a close match to the first location at ADDRESS. */
859 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
860 sizeof (struct bp_location **),
861 bp_location_compare_addrs);
862
863 /* Nothing was found, nothing left to do. */
864 if (locp_found == NULL)
865 return NULL;
866
867 /* We may have found a location that is at ADDRESS but is not the first in the
868 location's list. Go backwards (if possible) and locate the first one. */
869 while ((locp_found - 1) >= bp_location
870 && (*(locp_found - 1))->address == address)
871 locp_found--;
872
873 return locp_found;
874}
875
adc36818
PM
876void
877set_breakpoint_condition (struct breakpoint *b, char *exp,
878 int from_tty)
879{
3a5c3e22
PA
880 xfree (b->cond_string);
881 b->cond_string = NULL;
adc36818 882
3a5c3e22 883 if (is_watchpoint (b))
adc36818 884 {
3a5c3e22
PA
885 struct watchpoint *w = (struct watchpoint *) b;
886
887 xfree (w->cond_exp);
888 w->cond_exp = NULL;
889 }
890 else
891 {
892 struct bp_location *loc;
893
894 for (loc = b->loc; loc; loc = loc->next)
895 {
896 xfree (loc->cond);
897 loc->cond = NULL;
b775012e
LM
898
899 /* No need to free the condition agent expression
900 bytecode (if we have one). We will handle this
901 when we go through update_global_location_list. */
3a5c3e22 902 }
adc36818 903 }
adc36818
PM
904
905 if (*exp == 0)
906 {
907 if (from_tty)
908 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
909 }
910 else
911 {
912 char *arg = exp;
cc59ec59 913
adc36818
PM
914 /* I don't know if it matters whether this is the string the user
915 typed in or the decompiled expression. */
916 b->cond_string = xstrdup (arg);
917 b->condition_not_parsed = 0;
918
919 if (is_watchpoint (b))
920 {
3a5c3e22
PA
921 struct watchpoint *w = (struct watchpoint *) b;
922
adc36818
PM
923 innermost_block = NULL;
924 arg = exp;
3a5c3e22 925 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
926 if (*arg)
927 error (_("Junk at end of expression"));
3a5c3e22 928 w->cond_exp_valid_block = innermost_block;
adc36818
PM
929 }
930 else
931 {
3a5c3e22
PA
932 struct bp_location *loc;
933
adc36818
PM
934 for (loc = b->loc; loc; loc = loc->next)
935 {
936 arg = exp;
937 loc->cond =
938 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
939 if (*arg)
940 error (_("Junk at end of expression"));
941 }
942 }
943 }
b775012e
LM
944 mark_breakpoint_modified (b);
945
adc36818 946 breakpoints_changed ();
8d3788bd 947 observer_notify_breakpoint_modified (b);
adc36818
PM
948}
949
d55637df
TT
950/* Completion for the "condition" command. */
951
952static VEC (char_ptr) *
953condition_completer (struct cmd_list_element *cmd, char *text, char *word)
954{
955 char *space;
956
957 text = skip_spaces (text);
958 space = skip_to_space (text);
959 if (*space == '\0')
960 {
961 int len;
962 struct breakpoint *b;
963 VEC (char_ptr) *result = NULL;
964
965 if (text[0] == '$')
966 {
967 /* We don't support completion of history indices. */
968 if (isdigit (text[1]))
969 return NULL;
970 return complete_internalvar (&text[1]);
971 }
972
973 /* We're completing the breakpoint number. */
974 len = strlen (text);
975
976 ALL_BREAKPOINTS (b)
977 {
978 int single = b->loc->next == NULL;
979 struct bp_location *loc;
980 int count = 1;
981
982 for (loc = b->loc; loc; loc = loc->next)
983 {
984 char location[50];
985
986 if (single)
987 sprintf (location, "%d", b->number);
988 else
989 sprintf (location, "%d.%d", b->number, count);
990
991 if (strncmp (location, text, len) == 0)
992 VEC_safe_push (char_ptr, result, xstrdup (location));
993
994 ++count;
995 }
996 }
997
998 return result;
999 }
1000
1001 /* We're completing the expression part. */
1002 text = skip_spaces (space);
1003 return expression_completer (cmd, text, word);
1004}
1005
c906108c
SS
1006/* condition N EXP -- set break condition of breakpoint N to EXP. */
1007
1008static void
fba45db2 1009condition_command (char *arg, int from_tty)
c906108c 1010{
52f0bd74 1011 struct breakpoint *b;
c906108c 1012 char *p;
52f0bd74 1013 int bnum;
c906108c
SS
1014
1015 if (arg == 0)
e2e0b3e5 1016 error_no_arg (_("breakpoint number"));
c906108c
SS
1017
1018 p = arg;
1019 bnum = get_number (&p);
5c44784c 1020 if (bnum == 0)
8a3fe4f8 1021 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1022
1023 ALL_BREAKPOINTS (b)
1024 if (b->number == bnum)
2f069f6f 1025 {
7371cf6d
PM
1026 /* Check if this breakpoint has a Python object assigned to
1027 it, and if it has a definition of the "stop"
1028 method. This method and conditions entered into GDB from
1029 the CLI are mutually exclusive. */
1030 if (b->py_bp_object
1031 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1032 error (_("Cannot set a condition where a Python 'stop' "
1033 "method has been defined in the breakpoint."));
2566ad2d 1034 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1035
1036 if (is_breakpoint (b))
1037 update_global_location_list (1);
1038
2f069f6f
JB
1039 return;
1040 }
c906108c 1041
8a3fe4f8 1042 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1043}
1044
a7bdde9e
VP
1045/* Check that COMMAND do not contain commands that are suitable
1046 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1047 Throw if any such commands is found. */
1048
a7bdde9e
VP
1049static void
1050check_no_tracepoint_commands (struct command_line *commands)
1051{
1052 struct command_line *c;
cc59ec59 1053
a7bdde9e
VP
1054 for (c = commands; c; c = c->next)
1055 {
1056 int i;
1057
1058 if (c->control_type == while_stepping_control)
3e43a32a
MS
1059 error (_("The 'while-stepping' command can "
1060 "only be used for tracepoints"));
a7bdde9e
VP
1061
1062 for (i = 0; i < c->body_count; ++i)
1063 check_no_tracepoint_commands ((c->body_list)[i]);
1064
1065 /* Not that command parsing removes leading whitespace and comment
4a64f543 1066 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1067 command directly. */
1068 if (strstr (c->line, "collect ") == c->line)
1069 error (_("The 'collect' command can only be used for tracepoints"));
1070
51661e93
VP
1071 if (strstr (c->line, "teval ") == c->line)
1072 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1073 }
1074}
1075
d77f58be
SS
1076/* Encapsulate tests for different types of tracepoints. */
1077
d9b3f62e
PA
1078static int
1079is_tracepoint_type (enum bptype type)
1080{
1081 return (type == bp_tracepoint
1082 || type == bp_fast_tracepoint
1083 || type == bp_static_tracepoint);
1084}
1085
a7bdde9e 1086int
d77f58be 1087is_tracepoint (const struct breakpoint *b)
a7bdde9e 1088{
d9b3f62e 1089 return is_tracepoint_type (b->type);
a7bdde9e 1090}
d9b3f62e 1091
e5dd4106 1092/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1093 breakpoint. This function will throw an exception if a problem is
1094 found. */
48cb2d85 1095
95a42b64
TT
1096static void
1097validate_commands_for_breakpoint (struct breakpoint *b,
1098 struct command_line *commands)
48cb2d85 1099{
d77f58be 1100 if (is_tracepoint (b))
a7bdde9e 1101 {
4a64f543
MS
1102 /* We need to verify that each top-level element of commands is
1103 valid for tracepoints, that there's at most one
1104 while-stepping element, and that while-stepping's body has
1105 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
1106 struct command_line *c;
1107 struct command_line *while_stepping = 0;
1108 for (c = commands; c; c = c->next)
1109 {
a7bdde9e
VP
1110 if (c->control_type == while_stepping_control)
1111 {
1112 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1113 error (_("The 'while-stepping' command "
1114 "cannot be used for fast tracepoint"));
0fb4aa4b 1115 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1116 error (_("The 'while-stepping' command "
1117 "cannot be used for static tracepoint"));
a7bdde9e
VP
1118
1119 if (while_stepping)
3e43a32a
MS
1120 error (_("The 'while-stepping' command "
1121 "can be used only once"));
a7bdde9e
VP
1122 else
1123 while_stepping = c;
1124 }
1125 }
1126 if (while_stepping)
1127 {
1128 struct command_line *c2;
1129
1130 gdb_assert (while_stepping->body_count == 1);
1131 c2 = while_stepping->body_list[0];
1132 for (; c2; c2 = c2->next)
1133 {
a7bdde9e
VP
1134 if (c2->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command cannot be nested"));
1136 }
1137 }
1138 }
1139 else
1140 {
1141 check_no_tracepoint_commands (commands);
1142 }
95a42b64
TT
1143}
1144
0fb4aa4b
PA
1145/* Return a vector of all the static tracepoints set at ADDR. The
1146 caller is responsible for releasing the vector. */
1147
1148VEC(breakpoint_p) *
1149static_tracepoints_here (CORE_ADDR addr)
1150{
1151 struct breakpoint *b;
1152 VEC(breakpoint_p) *found = 0;
1153 struct bp_location *loc;
1154
1155 ALL_BREAKPOINTS (b)
1156 if (b->type == bp_static_tracepoint)
1157 {
1158 for (loc = b->loc; loc; loc = loc->next)
1159 if (loc->address == addr)
1160 VEC_safe_push(breakpoint_p, found, b);
1161 }
1162
1163 return found;
1164}
1165
95a42b64 1166/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1167 validate that only allowed commands are included. */
95a42b64
TT
1168
1169void
4a64f543
MS
1170breakpoint_set_commands (struct breakpoint *b,
1171 struct command_line *commands)
95a42b64
TT
1172{
1173 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1174
9add0f1b
TT
1175 decref_counted_command_line (&b->commands);
1176 b->commands = alloc_counted_command_line (commands);
48cb2d85 1177 breakpoints_changed ();
8d3788bd 1178 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1179}
1180
45a43567
TT
1181/* Set the internal `silent' flag on the breakpoint. Note that this
1182 is not the same as the "silent" that may appear in the breakpoint's
1183 commands. */
1184
1185void
1186breakpoint_set_silent (struct breakpoint *b, int silent)
1187{
1188 int old_silent = b->silent;
1189
1190 b->silent = silent;
1191 if (old_silent != silent)
8d3788bd 1192 observer_notify_breakpoint_modified (b);
45a43567
TT
1193}
1194
1195/* Set the thread for this breakpoint. If THREAD is -1, make the
1196 breakpoint work for any thread. */
1197
1198void
1199breakpoint_set_thread (struct breakpoint *b, int thread)
1200{
1201 int old_thread = b->thread;
1202
1203 b->thread = thread;
1204 if (old_thread != thread)
8d3788bd 1205 observer_notify_breakpoint_modified (b);
45a43567
TT
1206}
1207
1208/* Set the task for this breakpoint. If TASK is 0, make the
1209 breakpoint work for any task. */
1210
1211void
1212breakpoint_set_task (struct breakpoint *b, int task)
1213{
1214 int old_task = b->task;
1215
1216 b->task = task;
1217 if (old_task != task)
8d3788bd 1218 observer_notify_breakpoint_modified (b);
45a43567
TT
1219}
1220
95a42b64
TT
1221void
1222check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1223{
1224 struct breakpoint *b = closure;
cc59ec59 1225
a7bdde9e
VP
1226 validate_actionline (&line, b);
1227}
1228
95a42b64
TT
1229/* A structure used to pass information through
1230 map_breakpoint_numbers. */
1231
1232struct commands_info
1233{
1234 /* True if the command was typed at a tty. */
1235 int from_tty;
86b17b60
PA
1236
1237 /* The breakpoint range spec. */
1238 char *arg;
1239
95a42b64
TT
1240 /* Non-NULL if the body of the commands are being read from this
1241 already-parsed command. */
1242 struct command_line *control;
86b17b60 1243
95a42b64
TT
1244 /* The command lines read from the user, or NULL if they have not
1245 yet been read. */
1246 struct counted_command_line *cmd;
1247};
1248
1249/* A callback for map_breakpoint_numbers that sets the commands for
1250 commands_command. */
1251
c906108c 1252static void
95a42b64 1253do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1254{
95a42b64 1255 struct commands_info *info = data;
c906108c 1256
95a42b64
TT
1257 if (info->cmd == NULL)
1258 {
1259 struct command_line *l;
5c44784c 1260
95a42b64
TT
1261 if (info->control != NULL)
1262 l = copy_command_lines (info->control->body_list[0]);
1263 else
86b17b60
PA
1264 {
1265 struct cleanup *old_chain;
1266 char *str;
c5aa993b 1267
3e43a32a
MS
1268 str = xstrprintf (_("Type commands for breakpoint(s) "
1269 "%s, one per line."),
86b17b60
PA
1270 info->arg);
1271
1272 old_chain = make_cleanup (xfree, str);
1273
1274 l = read_command_lines (str,
1275 info->from_tty, 1,
d77f58be 1276 (is_tracepoint (b)
86b17b60
PA
1277 ? check_tracepoint_command : 0),
1278 b);
1279
1280 do_cleanups (old_chain);
1281 }
a7bdde9e 1282
95a42b64
TT
1283 info->cmd = alloc_counted_command_line (l);
1284 }
1285
1286 /* If a breakpoint was on the list more than once, we don't need to
1287 do anything. */
1288 if (b->commands != info->cmd)
1289 {
1290 validate_commands_for_breakpoint (b, info->cmd->commands);
1291 incref_counted_command_line (info->cmd);
1292 decref_counted_command_line (&b->commands);
1293 b->commands = info->cmd;
1294 breakpoints_changed ();
8d3788bd 1295 observer_notify_breakpoint_modified (b);
c5aa993b 1296 }
95a42b64
TT
1297}
1298
1299static void
4a64f543
MS
1300commands_command_1 (char *arg, int from_tty,
1301 struct command_line *control)
95a42b64
TT
1302{
1303 struct cleanup *cleanups;
1304 struct commands_info info;
1305
1306 info.from_tty = from_tty;
1307 info.control = control;
1308 info.cmd = NULL;
1309 /* If we read command lines from the user, then `info' will hold an
1310 extra reference to the commands that we must clean up. */
1311 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1312
1313 if (arg == NULL || !*arg)
1314 {
86b17b60 1315 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1316 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1317 breakpoint_count);
95a42b64
TT
1318 else if (breakpoint_count > 0)
1319 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1320 else
1321 {
1322 /* So that we don't try to free the incoming non-NULL
1323 argument in the cleanup below. Mapping breakpoint
1324 numbers will fail in this case. */
1325 arg = NULL;
1326 }
95a42b64 1327 }
9766ced4
SS
1328 else
1329 /* The command loop has some static state, so we need to preserve
1330 our argument. */
1331 arg = xstrdup (arg);
86b17b60
PA
1332
1333 if (arg != NULL)
1334 make_cleanup (xfree, arg);
1335
1336 info.arg = arg;
95a42b64
TT
1337
1338 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1339
1340 if (info.cmd == NULL)
1341 error (_("No breakpoints specified."));
1342
1343 do_cleanups (cleanups);
1344}
1345
1346static void
1347commands_command (char *arg, int from_tty)
1348{
1349 commands_command_1 (arg, from_tty, NULL);
c906108c 1350}
40c03ae8
EZ
1351
1352/* Like commands_command, but instead of reading the commands from
1353 input stream, takes them from an already parsed command structure.
1354
1355 This is used by cli-script.c to DTRT with breakpoint commands
1356 that are part of if and while bodies. */
1357enum command_control_type
1358commands_from_control_command (char *arg, struct command_line *cmd)
1359{
95a42b64
TT
1360 commands_command_1 (arg, 0, cmd);
1361 return simple_control;
40c03ae8 1362}
876fa593
JK
1363
1364/* Return non-zero if BL->TARGET_INFO contains valid information. */
1365
1366static int
1367bp_location_has_shadow (struct bp_location *bl)
1368{
1369 if (bl->loc_type != bp_loc_software_breakpoint)
1370 return 0;
1371 if (!bl->inserted)
1372 return 0;
1373 if (bl->target_info.shadow_len == 0)
e5dd4106 1374 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1375 return 0;
1376 return 1;
1377}
1378
8defab1a 1379/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1380 by replacing any memory breakpoints with their shadowed contents.
1381
35c63cd8
JB
1382 If READBUF is not NULL, this buffer must not overlap with any of
1383 the breakpoint location's shadow_contents buffers. Otherwise,
1384 a failed assertion internal error will be raised.
1385
876fa593 1386 The range of shadowed area by each bp_location is:
35df4500
TJB
1387 bl->address - bp_location_placed_address_before_address_max
1388 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1389 The range we were requested to resolve shadows for is:
1390 memaddr ... memaddr + len
1391 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1392 memaddr + len <= (bl->address
1393 - bp_location_placed_address_before_address_max)
876fa593 1394 and:
35df4500 1395 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1396
8defab1a 1397void
f0ba3972
PA
1398breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1399 const gdb_byte *writebuf_org,
1400 ULONGEST memaddr, LONGEST len)
c906108c 1401{
4a64f543
MS
1402 /* Left boundary, right boundary and median element of our binary
1403 search. */
876fa593
JK
1404 unsigned bc_l, bc_r, bc;
1405
4a64f543
MS
1406 /* Find BC_L which is a leftmost element which may affect BUF
1407 content. It is safe to report lower value but a failure to
1408 report higher one. */
876fa593
JK
1409
1410 bc_l = 0;
1411 bc_r = bp_location_count;
1412 while (bc_l + 1 < bc_r)
1413 {
35df4500 1414 struct bp_location *bl;
876fa593
JK
1415
1416 bc = (bc_l + bc_r) / 2;
35df4500 1417 bl = bp_location[bc];
876fa593 1418
4a64f543
MS
1419 /* Check first BL->ADDRESS will not overflow due to the added
1420 constant. Then advance the left boundary only if we are sure
1421 the BC element can in no way affect the BUF content (MEMADDR
1422 to MEMADDR + LEN range).
876fa593 1423
4a64f543
MS
1424 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1425 offset so that we cannot miss a breakpoint with its shadow
1426 range tail still reaching MEMADDR. */
c5aa993b 1427
35df4500
TJB
1428 if ((bl->address + bp_location_shadow_len_after_address_max
1429 >= bl->address)
1430 && (bl->address + bp_location_shadow_len_after_address_max
1431 <= memaddr))
876fa593
JK
1432 bc_l = bc;
1433 else
1434 bc_r = bc;
1435 }
1436
128070bb
PA
1437 /* Due to the binary search above, we need to make sure we pick the
1438 first location that's at BC_L's address. E.g., if there are
1439 multiple locations at the same address, BC_L may end up pointing
1440 at a duplicate location, and miss the "master"/"inserted"
1441 location. Say, given locations L1, L2 and L3 at addresses A and
1442 B:
1443
1444 L1@A, L2@A, L3@B, ...
1445
1446 BC_L could end up pointing at location L2, while the "master"
1447 location could be L1. Since the `loc->inserted' flag is only set
1448 on "master" locations, we'd forget to restore the shadow of L1
1449 and L2. */
1450 while (bc_l > 0
1451 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1452 bc_l--;
1453
876fa593
JK
1454 /* Now do full processing of the found relevant range of elements. */
1455
1456 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1457 {
35df4500 1458 struct bp_location *bl = bp_location[bc];
876fa593
JK
1459 CORE_ADDR bp_addr = 0;
1460 int bp_size = 0;
1461 int bptoffset = 0;
1462
35df4500
TJB
1463 /* bp_location array has BL->OWNER always non-NULL. */
1464 if (bl->owner->type == bp_none)
8a3fe4f8 1465 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1466 bl->owner->number);
ffce0d52 1467
e5dd4106 1468 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1469 content. */
1470
35df4500
TJB
1471 if (bl->address >= bp_location_placed_address_before_address_max
1472 && memaddr + len <= (bl->address
1473 - bp_location_placed_address_before_address_max))
876fa593
JK
1474 break;
1475
35df4500 1476 if (!bp_location_has_shadow (bl))
c5aa993b 1477 continue;
35df4500 1478 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1479 current_program_space->aspace, 0))
1480 continue;
1481
c5aa993b
JM
1482 /* Addresses and length of the part of the breakpoint that
1483 we need to copy. */
35df4500
TJB
1484 bp_addr = bl->target_info.placed_address;
1485 bp_size = bl->target_info.shadow_len;
8defab1a 1486
c5aa993b
JM
1487 if (bp_addr + bp_size <= memaddr)
1488 /* The breakpoint is entirely before the chunk of memory we
1489 are reading. */
1490 continue;
8defab1a 1491
c5aa993b
JM
1492 if (bp_addr >= memaddr + len)
1493 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1494 reading. */
c5aa993b 1495 continue;
c5aa993b 1496
8defab1a
DJ
1497 /* Offset within shadow_contents. */
1498 if (bp_addr < memaddr)
1499 {
1500 /* Only copy the second part of the breakpoint. */
1501 bp_size -= memaddr - bp_addr;
1502 bptoffset = memaddr - bp_addr;
1503 bp_addr = memaddr;
1504 }
c5aa993b 1505
8defab1a
DJ
1506 if (bp_addr + bp_size > memaddr + len)
1507 {
1508 /* Only copy the first part of the breakpoint. */
1509 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1510 }
c5aa993b 1511
f0ba3972
PA
1512 if (readbuf != NULL)
1513 {
35c63cd8
JB
1514 /* Verify that the readbuf buffer does not overlap with
1515 the shadow_contents buffer. */
1516 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1517 || readbuf >= (bl->target_info.shadow_contents
1518 + bl->target_info.shadow_len));
1519
f0ba3972
PA
1520 /* Update the read buffer with this inserted breakpoint's
1521 shadow. */
1522 memcpy (readbuf + bp_addr - memaddr,
1523 bl->target_info.shadow_contents + bptoffset, bp_size);
1524 }
1525 else
1526 {
1527 struct gdbarch *gdbarch = bl->gdbarch;
1528 const unsigned char *bp;
1529 CORE_ADDR placed_address = bl->target_info.placed_address;
1530 unsigned placed_size = bl->target_info.placed_size;
1531
1532 /* Update the shadow with what we want to write to memory. */
1533 memcpy (bl->target_info.shadow_contents + bptoffset,
1534 writebuf_org + bp_addr - memaddr, bp_size);
1535
1536 /* Determine appropriate breakpoint contents and size for this
1537 address. */
1538 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1539
1540 /* Update the final write buffer with this inserted
1541 breakpoint's INSN. */
1542 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1543 }
c5aa993b 1544 }
c906108c 1545}
c906108c 1546\f
c5aa993b 1547
b775012e
LM
1548/* Return true if BPT is either a software breakpoint or a hardware
1549 breakpoint. */
1550
1551int
1552is_breakpoint (const struct breakpoint *bpt)
1553{
1554 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1555 || bpt->type == bp_hardware_breakpoint
1556 || bpt->type == bp_dprintf);
b775012e
LM
1557}
1558
60e1c644
PA
1559/* Return true if BPT is of any hardware watchpoint kind. */
1560
a5606eee 1561static int
d77f58be 1562is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1563{
1564 return (bpt->type == bp_hardware_watchpoint
1565 || bpt->type == bp_read_watchpoint
1566 || bpt->type == bp_access_watchpoint);
1567}
7270d8f2 1568
60e1c644
PA
1569/* Return true if BPT is of any watchpoint kind, hardware or
1570 software. */
1571
3a5c3e22 1572int
d77f58be 1573is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1574{
1575 return (is_hardware_watchpoint (bpt)
1576 || bpt->type == bp_watchpoint);
1577}
1578
3a5c3e22
PA
1579/* Returns true if the current thread and its running state are safe
1580 to evaluate or update watchpoint B. Watchpoints on local
1581 expressions need to be evaluated in the context of the thread that
1582 was current when the watchpoint was created, and, that thread needs
1583 to be stopped to be able to select the correct frame context.
1584 Watchpoints on global expressions can be evaluated on any thread,
1585 and in any state. It is presently left to the target allowing
1586 memory accesses when threads are running. */
f6bc2008
PA
1587
1588static int
3a5c3e22 1589watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1590{
d0d8b0c6
JK
1591 return (b->base.pspace == current_program_space
1592 && (ptid_equal (b->watchpoint_thread, null_ptid)
1593 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1594 && !is_executing (inferior_ptid))));
f6bc2008
PA
1595}
1596
d0fb5eae
JK
1597/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1598 associated bp_watchpoint_scope breakpoint. */
1599
1600static void
3a5c3e22 1601watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1602{
3a5c3e22 1603 struct breakpoint *b = &w->base;
d0fb5eae
JK
1604
1605 if (b->related_breakpoint != b)
1606 {
1607 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1608 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1609 b->related_breakpoint->disposition = disp_del_at_next_stop;
1610 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1611 b->related_breakpoint = b;
1612 }
1613 b->disposition = disp_del_at_next_stop;
1614}
1615
567e1b4e
JB
1616/* Assuming that B is a watchpoint:
1617 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1618 - Evaluate expression and store the result in B->val
567e1b4e
JB
1619 - Evaluate the condition if there is one, and store the result
1620 in b->loc->cond.
a5606eee
VP
1621 - Update the list of values that must be watched in B->loc.
1622
4a64f543
MS
1623 If the watchpoint disposition is disp_del_at_next_stop, then do
1624 nothing. If this is local watchpoint that is out of scope, delete
1625 it.
1626
1627 Even with `set breakpoint always-inserted on' the watchpoints are
1628 removed + inserted on each stop here. Normal breakpoints must
1629 never be removed because they might be missed by a running thread
1630 when debugging in non-stop mode. On the other hand, hardware
1631 watchpoints (is_hardware_watchpoint; processed here) are specific
1632 to each LWP since they are stored in each LWP's hardware debug
1633 registers. Therefore, such LWP must be stopped first in order to
1634 be able to modify its hardware watchpoints.
1635
1636 Hardware watchpoints must be reset exactly once after being
1637 presented to the user. It cannot be done sooner, because it would
1638 reset the data used to present the watchpoint hit to the user. And
1639 it must not be done later because it could display the same single
1640 watchpoint hit during multiple GDB stops. Note that the latter is
1641 relevant only to the hardware watchpoint types bp_read_watchpoint
1642 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1643 not user-visible - its hit is suppressed if the memory content has
1644 not changed.
1645
1646 The following constraints influence the location where we can reset
1647 hardware watchpoints:
1648
1649 * target_stopped_by_watchpoint and target_stopped_data_address are
1650 called several times when GDB stops.
1651
1652 [linux]
1653 * Multiple hardware watchpoints can be hit at the same time,
1654 causing GDB to stop. GDB only presents one hardware watchpoint
1655 hit at a time as the reason for stopping, and all the other hits
1656 are presented later, one after the other, each time the user
1657 requests the execution to be resumed. Execution is not resumed
1658 for the threads still having pending hit event stored in
1659 LWP_INFO->STATUS. While the watchpoint is already removed from
1660 the inferior on the first stop the thread hit event is kept being
1661 reported from its cached value by linux_nat_stopped_data_address
1662 until the real thread resume happens after the watchpoint gets
1663 presented and thus its LWP_INFO->STATUS gets reset.
1664
1665 Therefore the hardware watchpoint hit can get safely reset on the
1666 watchpoint removal from inferior. */
a79d3c27 1667
b40ce68a 1668static void
3a5c3e22 1669update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1670{
a5606eee 1671 int within_current_scope;
a5606eee 1672 struct frame_id saved_frame_id;
66076460 1673 int frame_saved;
a5606eee 1674
f6bc2008
PA
1675 /* If this is a local watchpoint, we only want to check if the
1676 watchpoint frame is in scope if the current thread is the thread
1677 that was used to create the watchpoint. */
1678 if (!watchpoint_in_thread_scope (b))
1679 return;
1680
3a5c3e22 1681 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1682 return;
1683
66076460 1684 frame_saved = 0;
a5606eee
VP
1685
1686 /* Determine if the watchpoint is within scope. */
1687 if (b->exp_valid_block == NULL)
1688 within_current_scope = 1;
1689 else
1690 {
b5db5dfc
UW
1691 struct frame_info *fi = get_current_frame ();
1692 struct gdbarch *frame_arch = get_frame_arch (fi);
1693 CORE_ADDR frame_pc = get_frame_pc (fi);
1694
1695 /* If we're in a function epilogue, unwinding may not work
1696 properly, so do not attempt to recreate locations at this
1697 point. See similar comments in watchpoint_check. */
1698 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1699 return;
66076460
DJ
1700
1701 /* Save the current frame's ID so we can restore it after
1702 evaluating the watchpoint expression on its own frame. */
1703 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1704 took a frame parameter, so that we didn't have to change the
1705 selected frame. */
1706 frame_saved = 1;
1707 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1708
a5606eee
VP
1709 fi = frame_find_by_id (b->watchpoint_frame);
1710 within_current_scope = (fi != NULL);
1711 if (within_current_scope)
1712 select_frame (fi);
1713 }
1714
b5db5dfc
UW
1715 /* We don't free locations. They are stored in the bp_location array
1716 and update_global_location_list will eventually delete them and
1717 remove breakpoints if needed. */
3a5c3e22 1718 b->base.loc = NULL;
b5db5dfc 1719
a5606eee
VP
1720 if (within_current_scope && reparse)
1721 {
1722 char *s;
d63d0675 1723
a5606eee
VP
1724 if (b->exp)
1725 {
1726 xfree (b->exp);
1727 b->exp = NULL;
1728 }
d63d0675 1729 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1730 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1731 /* If the meaning of expression itself changed, the old value is
1732 no longer relevant. We don't want to report a watchpoint hit
1733 to the user when the old value and the new value may actually
1734 be completely different objects. */
1735 value_free (b->val);
fa4727a6
DJ
1736 b->val = NULL;
1737 b->val_valid = 0;
60e1c644
PA
1738
1739 /* Note that unlike with breakpoints, the watchpoint's condition
1740 expression is stored in the breakpoint object, not in the
1741 locations (re)created below. */
3a5c3e22 1742 if (b->base.cond_string != NULL)
60e1c644
PA
1743 {
1744 if (b->cond_exp != NULL)
1745 {
1746 xfree (b->cond_exp);
1747 b->cond_exp = NULL;
1748 }
1749
3a5c3e22 1750 s = b->base.cond_string;
60e1c644
PA
1751 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1752 }
a5606eee 1753 }
a5606eee
VP
1754
1755 /* If we failed to parse the expression, for example because
1756 it refers to a global variable in a not-yet-loaded shared library,
1757 don't try to insert watchpoint. We don't automatically delete
1758 such watchpoint, though, since failure to parse expression
1759 is different from out-of-scope watchpoint. */
2d134ed3
PA
1760 if ( !target_has_execution)
1761 {
1762 /* Without execution, memory can't change. No use to try and
1763 set watchpoint locations. The watchpoint will be reset when
1764 the target gains execution, through breakpoint_re_set. */
1765 }
1766 else if (within_current_scope && b->exp)
a5606eee 1767 {
0cf6dd15 1768 int pc = 0;
fa4727a6 1769 struct value *val_chain, *v, *result, *next;
2d134ed3 1770 struct program_space *frame_pspace;
a5606eee 1771
0cf6dd15 1772 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1773
a5606eee
VP
1774 /* Avoid setting b->val if it's already set. The meaning of
1775 b->val is 'the last value' user saw, and we should update
1776 it only if we reported that last value to user. As it
9c06b0b4
TJB
1777 happens, the code that reports it updates b->val directly.
1778 We don't keep track of the memory value for masked
1779 watchpoints. */
3a5c3e22 1780 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1781 {
1782 b->val = v;
1783 b->val_valid = 1;
1784 }
a5606eee 1785
2d134ed3
PA
1786 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1787
a5606eee 1788 /* Look at each value on the value chain. */
9fa40276 1789 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1790 {
1791 /* If it's a memory location, and GDB actually needed
1792 its contents to evaluate the expression, then we
fa4727a6
DJ
1793 must watch it. If the first value returned is
1794 still lazy, that means an error occurred reading it;
1795 watch it anyway in case it becomes readable. */
a5606eee 1796 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1797 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1798 {
1799 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1800
a5606eee
VP
1801 /* We only watch structs and arrays if user asked
1802 for it explicitly, never if they just happen to
1803 appear in the middle of some value chain. */
fa4727a6 1804 if (v == result
a5606eee
VP
1805 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1806 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1807 {
1808 CORE_ADDR addr;
1809 int len, type;
1810 struct bp_location *loc, **tmp;
1811
42ae5230 1812 addr = value_address (v);
a5606eee
VP
1813 len = TYPE_LENGTH (value_type (v));
1814 type = hw_write;
3a5c3e22 1815 if (b->base.type == bp_read_watchpoint)
a5606eee 1816 type = hw_read;
3a5c3e22 1817 else if (b->base.type == bp_access_watchpoint)
a5606eee 1818 type = hw_access;
3a5c3e22
PA
1819
1820 loc = allocate_bp_location (&b->base);
1821 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1822 ;
1823 *tmp = loc;
a6d9a66e 1824 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1825
1826 loc->pspace = frame_pspace;
a5606eee
VP
1827 loc->address = addr;
1828 loc->length = len;
1829 loc->watchpoint_type = type;
1830 }
1831 }
9fa40276
TJB
1832 }
1833
1834 /* Change the type of breakpoint between hardware assisted or
1835 an ordinary watchpoint depending on the hardware support
1836 and free hardware slots. REPARSE is set when the inferior
1837 is started. */
a9634178 1838 if (reparse)
9fa40276 1839 {
e09342b5 1840 int reg_cnt;
9fa40276
TJB
1841 enum bp_loc_type loc_type;
1842 struct bp_location *bl;
a5606eee 1843
a9634178 1844 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1845
1846 if (reg_cnt)
9fa40276
TJB
1847 {
1848 int i, target_resources_ok, other_type_used;
a1398e0c 1849 enum bptype type;
9fa40276 1850
a9634178
TJB
1851 /* Use an exact watchpoint when there's only one memory region to be
1852 watched, and only one debug register is needed to watch it. */
1853 b->exact = target_exact_watchpoints && reg_cnt == 1;
1854
9fa40276 1855 /* We need to determine how many resources are already
e09342b5
TJB
1856 used for all other hardware watchpoints plus this one
1857 to see if we still have enough resources to also fit
a1398e0c
PA
1858 this watchpoint in as well. */
1859
1860 /* If this is a software watchpoint, we try to turn it
1861 to a hardware one -- count resources as if B was of
1862 hardware watchpoint type. */
1863 type = b->base.type;
1864 if (type == bp_watchpoint)
1865 type = bp_hardware_watchpoint;
1866
1867 /* This watchpoint may or may not have been placed on
1868 the list yet at this point (it won't be in the list
1869 if we're trying to create it for the first time,
1870 through watch_command), so always account for it
1871 manually. */
1872
1873 /* Count resources used by all watchpoints except B. */
1874 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1875
1876 /* Add in the resources needed for B. */
1877 i += hw_watchpoint_use_count (&b->base);
1878
1879 target_resources_ok
1880 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1881 if (target_resources_ok <= 0)
a9634178 1882 {
3a5c3e22 1883 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1884
1885 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1886 error (_("Target does not support this type of "
1887 "hardware watchpoint."));
9c06b0b4
TJB
1888 else if (target_resources_ok < 0 && !sw_mode)
1889 error (_("There are not enough available hardware "
1890 "resources for this watchpoint."));
a1398e0c
PA
1891
1892 /* Downgrade to software watchpoint. */
1893 b->base.type = bp_watchpoint;
1894 }
1895 else
1896 {
1897 /* If this was a software watchpoint, we've just
1898 found we have enough resources to turn it to a
1899 hardware watchpoint. Otherwise, this is a
1900 nop. */
1901 b->base.type = type;
a9634178 1902 }
9fa40276 1903 }
3a5c3e22 1904 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1905 error (_("Expression cannot be implemented with "
1906 "read/access watchpoint."));
9fa40276 1907 else
3a5c3e22 1908 b->base.type = bp_watchpoint;
9fa40276 1909
3a5c3e22 1910 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1911 : bp_loc_hardware_watchpoint);
3a5c3e22 1912 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1913 bl->loc_type = loc_type;
1914 }
1915
1916 for (v = val_chain; v; v = next)
1917 {
a5606eee
VP
1918 next = value_next (v);
1919 if (v != b->val)
1920 value_free (v);
1921 }
1922
c7437ca6
PA
1923 /* If a software watchpoint is not watching any memory, then the
1924 above left it without any location set up. But,
1925 bpstat_stop_status requires a location to be able to report
1926 stops, so make sure there's at least a dummy one. */
3a5c3e22 1927 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1928 {
3a5c3e22
PA
1929 struct breakpoint *base = &b->base;
1930 base->loc = allocate_bp_location (base);
1931 base->loc->pspace = frame_pspace;
1932 base->loc->address = -1;
1933 base->loc->length = -1;
1934 base->loc->watchpoint_type = -1;
c7437ca6 1935 }
a5606eee
VP
1936 }
1937 else if (!within_current_scope)
7270d8f2 1938 {
ac74f770
MS
1939 printf_filtered (_("\
1940Watchpoint %d deleted because the program has left the block\n\
1941in which its expression is valid.\n"),
3a5c3e22 1942 b->base.number);
d0fb5eae 1943 watchpoint_del_at_next_stop (b);
7270d8f2 1944 }
a5606eee
VP
1945
1946 /* Restore the selected frame. */
66076460
DJ
1947 if (frame_saved)
1948 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1949}
1950
a5606eee 1951
74960c60 1952/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1953 inserted in the inferior. We don't differentiate the type of BL's owner
1954 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1955 breakpoint_ops is not defined, because in insert_bp_location,
1956 tracepoint's insert_location will not be called. */
74960c60 1957static int
35df4500 1958should_be_inserted (struct bp_location *bl)
74960c60 1959{
35df4500 1960 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1961 return 0;
1962
35df4500 1963 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1964 return 0;
1965
35df4500 1966 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1967 return 0;
1968
f8eba3c6
TT
1969 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1970 return 0;
1971
56710373
PA
1972 /* This is set for example, when we're attached to the parent of a
1973 vfork, and have detached from the child. The child is running
1974 free, and we expect it to do an exec or exit, at which point the
1975 OS makes the parent schedulable again (and the target reports
1976 that the vfork is done). Until the child is done with the shared
1977 memory region, do not insert breakpoints in the parent, otherwise
1978 the child could still trip on the parent's breakpoints. Since
1979 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1980 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1981 return 0;
1982
74960c60
VP
1983 return 1;
1984}
1985
934709f0
PW
1986/* Same as should_be_inserted but does the check assuming
1987 that the location is not duplicated. */
1988
1989static int
1990unduplicated_should_be_inserted (struct bp_location *bl)
1991{
1992 int result;
1993 const int save_duplicate = bl->duplicate;
1994
1995 bl->duplicate = 0;
1996 result = should_be_inserted (bl);
1997 bl->duplicate = save_duplicate;
1998 return result;
1999}
2000
b775012e
LM
2001/* Parses a conditional described by an expression COND into an
2002 agent expression bytecode suitable for evaluation
2003 by the bytecode interpreter. Return NULL if there was
2004 any error during parsing. */
2005
2006static struct agent_expr *
2007parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2008{
2009 struct agent_expr *aexpr = NULL;
2010 struct cleanup *old_chain = NULL;
2011 volatile struct gdb_exception ex;
2012
2013 if (!cond)
2014 return NULL;
2015
2016 /* We don't want to stop processing, so catch any errors
2017 that may show up. */
2018 TRY_CATCH (ex, RETURN_MASK_ERROR)
2019 {
2020 aexpr = gen_eval_for_expr (scope, cond);
2021 }
2022
2023 if (ex.reason < 0)
2024 {
2025 /* If we got here, it means the condition could not be parsed to a valid
2026 bytecode expression and thus can't be evaluated on the target's side.
2027 It's no use iterating through the conditions. */
2028 return NULL;
2029 }
2030
2031 /* We have a valid agent expression. */
2032 return aexpr;
2033}
2034
2035/* Based on location BL, create a list of breakpoint conditions to be
2036 passed on to the target. If we have duplicated locations with different
2037 conditions, we will add such conditions to the list. The idea is that the
2038 target will evaluate the list of conditions and will only notify GDB when
2039 one of them is true. */
2040
2041static void
2042build_target_condition_list (struct bp_location *bl)
2043{
2044 struct bp_location **locp = NULL, **loc2p;
2045 int null_condition_or_parse_error = 0;
2046 int modified = bl->needs_update;
2047 struct bp_location *loc;
2048
2049 /* This is only meaningful if the target is
2050 evaluating conditions and if the user has
2051 opted for condition evaluation on the target's
2052 side. */
2053 if (gdb_evaluates_breakpoint_condition_p ()
2054 || !target_supports_evaluation_of_breakpoint_conditions ())
2055 return;
2056
2057 /* Do a first pass to check for locations with no assigned
2058 conditions or conditions that fail to parse to a valid agent expression
2059 bytecode. If any of these happen, then it's no use to send conditions
2060 to the target since this location will always trigger and generate a
2061 response back to GDB. */
2062 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2063 {
2064 loc = (*loc2p);
2065 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2066 {
2067 if (modified)
2068 {
2069 struct agent_expr *aexpr;
2070
2071 /* Re-parse the conditions since something changed. In that
2072 case we already freed the condition bytecodes (see
2073 force_breakpoint_reinsertion). We just
2074 need to parse the condition to bytecodes again. */
2075 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2076 loc->cond_bytecode = aexpr;
2077
2078 /* Check if we managed to parse the conditional expression
2079 correctly. If not, we will not send this condition
2080 to the target. */
2081 if (aexpr)
2082 continue;
2083 }
2084
2085 /* If we have a NULL bytecode expression, it means something
2086 went wrong or we have a null condition expression. */
2087 if (!loc->cond_bytecode)
2088 {
2089 null_condition_or_parse_error = 1;
2090 break;
2091 }
2092 }
2093 }
2094
2095 /* If any of these happened, it means we will have to evaluate the conditions
2096 for the location's address on gdb's side. It is no use keeping bytecodes
2097 for all the other duplicate locations, thus we free all of them here.
2098
2099 This is so we have a finer control over which locations' conditions are
2100 being evaluated by GDB or the remote stub. */
2101 if (null_condition_or_parse_error)
2102 {
2103 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2104 {
2105 loc = (*loc2p);
2106 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2107 {
2108 /* Only go as far as the first NULL bytecode is
2109 located. */
2110 if (!loc->cond_bytecode)
2111 return;
2112
2113 free_agent_expr (loc->cond_bytecode);
2114 loc->cond_bytecode = NULL;
2115 }
2116 }
2117 }
2118
2119 /* No NULL conditions or failed bytecode generation. Build a condition list
2120 for this location's address. */
2121 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2122 {
2123 loc = (*loc2p);
2124 if (loc->cond
2125 && is_breakpoint (loc->owner)
2126 && loc->pspace->num == bl->pspace->num
2127 && loc->owner->enable_state == bp_enabled
2128 && loc->enabled)
2129 /* Add the condition to the vector. This will be used later to send the
2130 conditions to the target. */
2131 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2132 loc->cond_bytecode);
2133 }
2134
2135 return;
2136}
2137
35df4500
TJB
2138/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2139 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2140 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2141 Returns 0 for success, 1 if the bp_location type is not supported or
2142 -1 for failure.
879bfdc2 2143
4a64f543
MS
2144 NOTE drow/2003-09-09: This routine could be broken down to an
2145 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2146static int
35df4500 2147insert_bp_location (struct bp_location *bl,
26bb91f3 2148 struct ui_file *tmp_error_stream,
3fbb6ffa 2149 int *disabled_breaks,
26bb91f3 2150 int *hw_breakpoint_error)
879bfdc2
DJ
2151{
2152 int val = 0;
2153
b775012e 2154 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2155 return 0;
2156
35c63cd8
JB
2157 /* Note we don't initialize bl->target_info, as that wipes out
2158 the breakpoint location's shadow_contents if the breakpoint
2159 is still inserted at that location. This in turn breaks
2160 target_read_memory which depends on these buffers when
2161 a memory read is requested at the breakpoint location:
2162 Once the target_info has been wiped, we fail to see that
2163 we have a breakpoint inserted at that address and thus
2164 read the breakpoint instead of returning the data saved in
2165 the breakpoint location's shadow contents. */
35df4500
TJB
2166 bl->target_info.placed_address = bl->address;
2167 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2168 bl->target_info.length = bl->length;
8181d85f 2169
b775012e
LM
2170 /* When working with target-side conditions, we must pass all the conditions
2171 for the same breakpoint address down to the target since GDB will not
2172 insert those locations. With a list of breakpoint conditions, the target
2173 can decide when to stop and notify GDB. */
2174
2175 if (is_breakpoint (bl->owner))
2176 {
2177 build_target_condition_list (bl);
2178 /* Reset the condition modification marker. */
2179 bl->needs_update = 0;
2180 }
2181
35df4500
TJB
2182 if (bl->loc_type == bp_loc_software_breakpoint
2183 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2184 {
35df4500 2185 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2186 {
2187 /* If the explicitly specified breakpoint type
2188 is not hardware breakpoint, check the memory map to see
2189 if the breakpoint address is in read only memory or not.
4a64f543 2190
765dc015
VP
2191 Two important cases are:
2192 - location type is not hardware breakpoint, memory
2193 is readonly. We change the type of the location to
2194 hardware breakpoint.
4a64f543
MS
2195 - location type is hardware breakpoint, memory is
2196 read-write. This means we've previously made the
2197 location hardware one, but then the memory map changed,
2198 so we undo.
765dc015 2199
4a64f543
MS
2200 When breakpoints are removed, remove_breakpoints will use
2201 location types we've just set here, the only possible
2202 problem is that memory map has changed during running
2203 program, but it's not going to work anyway with current
2204 gdb. */
765dc015 2205 struct mem_region *mr
35df4500 2206 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2207
2208 if (mr)
2209 {
2210 if (automatic_hardware_breakpoints)
2211 {
765dc015
VP
2212 enum bp_loc_type new_type;
2213
2214 if (mr->attrib.mode != MEM_RW)
2215 new_type = bp_loc_hardware_breakpoint;
2216 else
2217 new_type = bp_loc_software_breakpoint;
2218
35df4500 2219 if (new_type != bl->loc_type)
765dc015
VP
2220 {
2221 static int said = 0;
cc59ec59 2222
35df4500 2223 bl->loc_type = new_type;
765dc015
VP
2224 if (!said)
2225 {
3e43a32a
MS
2226 fprintf_filtered (gdb_stdout,
2227 _("Note: automatically using "
2228 "hardware breakpoints for "
2229 "read-only addresses.\n"));
765dc015
VP
2230 said = 1;
2231 }
2232 }
2233 }
35df4500 2234 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2235 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2236 warning (_("cannot set software breakpoint "
2237 "at readonly address %s"),
35df4500 2238 paddress (bl->gdbarch, bl->address));
765dc015
VP
2239 }
2240 }
2241
879bfdc2
DJ
2242 /* First check to see if we have to handle an overlay. */
2243 if (overlay_debugging == ovly_off
35df4500
TJB
2244 || bl->section == NULL
2245 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2246 {
2247 /* No overlay handling: just set the breakpoint. */
2248
348d480f 2249 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2250 }
2251 else
2252 {
4a64f543 2253 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2254 Shall we set a breakpoint at the LMA? */
2255 if (!overlay_events_enabled)
2256 {
2257 /* Yes -- overlay event support is not active,
2258 so we must try to set a breakpoint at the LMA.
2259 This will not work for a hardware breakpoint. */
35df4500 2260 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2261 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2262 bl->owner->number);
879bfdc2
DJ
2263 else
2264 {
35df4500
TJB
2265 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2266 bl->section);
879bfdc2 2267 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2268 bl->overlay_target_info = bl->target_info;
2269 bl->overlay_target_info.placed_address = addr;
2270 val = target_insert_breakpoint (bl->gdbarch,
2271 &bl->overlay_target_info);
879bfdc2 2272 if (val != 0)
99361f52 2273 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2274 "Overlay breakpoint %d "
2275 "failed: in ROM?\n",
35df4500 2276 bl->owner->number);
879bfdc2
DJ
2277 }
2278 }
2279 /* Shall we set a breakpoint at the VMA? */
35df4500 2280 if (section_is_mapped (bl->section))
879bfdc2
DJ
2281 {
2282 /* Yes. This overlay section is mapped into memory. */
348d480f 2283 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2284 }
2285 else
2286 {
2287 /* No. This breakpoint will not be inserted.
2288 No error, but do not mark the bp as 'inserted'. */
2289 return 0;
2290 }
2291 }
2292
2293 if (val)
2294 {
2295 /* Can't set the breakpoint. */
35df4500 2296 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2297 {
4a64f543 2298 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2299 val = 0;
35df4500 2300 bl->shlib_disabled = 1;
8d3788bd 2301 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2302 if (!*disabled_breaks)
2303 {
2304 fprintf_unfiltered (tmp_error_stream,
2305 "Cannot insert breakpoint %d.\n",
2306 bl->owner->number);
2307 fprintf_unfiltered (tmp_error_stream,
2308 "Temporarily disabling shared "
2309 "library breakpoints:\n");
2310 }
2311 *disabled_breaks = 1;
879bfdc2 2312 fprintf_unfiltered (tmp_error_stream,
35df4500 2313 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2314 }
2315 else
879bfdc2 2316 {
35df4500 2317 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
2318 {
2319 *hw_breakpoint_error = 1;
3e43a32a
MS
2320 fprintf_unfiltered (tmp_error_stream,
2321 "Cannot insert hardware "
2322 "breakpoint %d.\n",
35df4500 2323 bl->owner->number);
879bfdc2
DJ
2324 }
2325 else
2326 {
2327 fprintf_unfiltered (tmp_error_stream,
2328 "Cannot insert breakpoint %d.\n",
35df4500 2329 bl->owner->number);
879bfdc2
DJ
2330 fprintf_filtered (tmp_error_stream,
2331 "Error accessing memory address ");
35df4500 2332 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2333 tmp_error_stream);
879bfdc2
DJ
2334 fprintf_filtered (tmp_error_stream, ": %s.\n",
2335 safe_strerror (val));
2336 }
2337
2338 }
2339 }
2340 else
35df4500 2341 bl->inserted = 1;
879bfdc2
DJ
2342
2343 return val;
2344 }
2345
35df4500 2346 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2347 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2348 watchpoints. It's not clear that it's necessary... */
35df4500 2349 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2350 {
77b06cd7
TJB
2351 gdb_assert (bl->owner->ops != NULL
2352 && bl->owner->ops->insert_location != NULL);
2353
2354 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2355
2356 /* If trying to set a read-watchpoint, and it turns out it's not
2357 supported, try emulating one with an access watchpoint. */
35df4500 2358 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2359 {
2360 struct bp_location *loc, **loc_temp;
2361
2362 /* But don't try to insert it, if there's already another
2363 hw_access location that would be considered a duplicate
2364 of this one. */
2365 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2366 if (loc != bl
85d721b8 2367 && loc->watchpoint_type == hw_access
35df4500 2368 && watchpoint_locations_match (bl, loc))
85d721b8 2369 {
35df4500
TJB
2370 bl->duplicate = 1;
2371 bl->inserted = 1;
2372 bl->target_info = loc->target_info;
2373 bl->watchpoint_type = hw_access;
85d721b8
PA
2374 val = 0;
2375 break;
2376 }
2377
2378 if (val == 1)
2379 {
77b06cd7
TJB
2380 bl->watchpoint_type = hw_access;
2381 val = bl->owner->ops->insert_location (bl);
2382
2383 if (val)
2384 /* Back to the original value. */
2385 bl->watchpoint_type = hw_read;
85d721b8
PA
2386 }
2387 }
2388
35df4500 2389 bl->inserted = (val == 0);
879bfdc2
DJ
2390 }
2391
35df4500 2392 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2393 {
77b06cd7
TJB
2394 gdb_assert (bl->owner->ops != NULL
2395 && bl->owner->ops->insert_location != NULL);
2396
2397 val = bl->owner->ops->insert_location (bl);
2398 if (val)
2399 {
2400 bl->owner->enable_state = bp_disabled;
2401
2402 if (val == 1)
2403 warning (_("\
2404Error inserting catchpoint %d: Your system does not support this type\n\
2405of catchpoint."), bl->owner->number);
2406 else
2407 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2408 }
2409
2410 bl->inserted = (val == 0);
1640b821
DJ
2411
2412 /* We've already printed an error message if there was a problem
2413 inserting this catchpoint, and we've disabled the catchpoint,
2414 so just return success. */
2415 return 0;
879bfdc2
DJ
2416 }
2417
2418 return 0;
2419}
2420
6c95b8df
PA
2421/* This function is called when program space PSPACE is about to be
2422 deleted. It takes care of updating breakpoints to not reference
2423 PSPACE anymore. */
2424
2425void
2426breakpoint_program_space_exit (struct program_space *pspace)
2427{
2428 struct breakpoint *b, *b_temp;
876fa593 2429 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2430
2431 /* Remove any breakpoint that was set through this program space. */
2432 ALL_BREAKPOINTS_SAFE (b, b_temp)
2433 {
2434 if (b->pspace == pspace)
2435 delete_breakpoint (b);
2436 }
2437
2438 /* Breakpoints set through other program spaces could have locations
2439 bound to PSPACE as well. Remove those. */
876fa593 2440 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2441 {
2442 struct bp_location *tmp;
2443
2444 if (loc->pspace == pspace)
2445 {
2bdf28a0 2446 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2447 if (loc->owner->loc == loc)
2448 loc->owner->loc = loc->next;
2449 else
2450 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2451 if (tmp->next == loc)
2452 {
2453 tmp->next = loc->next;
2454 break;
2455 }
2456 }
2457 }
2458
2459 /* Now update the global location list to permanently delete the
2460 removed locations above. */
2461 update_global_location_list (0);
2462}
2463
74960c60
VP
2464/* Make sure all breakpoints are inserted in inferior.
2465 Throws exception on any error.
2466 A breakpoint that is already inserted won't be inserted
2467 again, so calling this function twice is safe. */
2468void
2469insert_breakpoints (void)
2470{
2471 struct breakpoint *bpt;
2472
2473 ALL_BREAKPOINTS (bpt)
2474 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2475 {
2476 struct watchpoint *w = (struct watchpoint *) bpt;
2477
2478 update_watchpoint (w, 0 /* don't reparse. */);
2479 }
74960c60 2480
b60e7edf 2481 update_global_location_list (1);
74960c60 2482
c35b1492
PA
2483 /* update_global_location_list does not insert breakpoints when
2484 always_inserted_mode is not enabled. Explicitly insert them
2485 now. */
2486 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2487 insert_breakpoint_locations ();
2488}
2489
20388dd6
YQ
2490/* Invoke CALLBACK for each of bp_location. */
2491
2492void
2493iterate_over_bp_locations (walk_bp_location_callback callback)
2494{
2495 struct bp_location *loc, **loc_tmp;
2496
2497 ALL_BP_LOCATIONS (loc, loc_tmp)
2498 {
2499 callback (loc, NULL);
2500 }
2501}
2502
b775012e
LM
2503/* This is used when we need to synch breakpoint conditions between GDB and the
2504 target. It is the case with deleting and disabling of breakpoints when using
2505 always-inserted mode. */
2506
2507static void
2508update_inserted_breakpoint_locations (void)
2509{
2510 struct bp_location *bl, **blp_tmp;
2511 int error_flag = 0;
2512 int val = 0;
2513 int disabled_breaks = 0;
2514 int hw_breakpoint_error = 0;
2515
2516 struct ui_file *tmp_error_stream = mem_fileopen ();
2517 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2518
2519 /* Explicitly mark the warning -- this will only be printed if
2520 there was an error. */
2521 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2522
2523 save_current_space_and_thread ();
2524
2525 ALL_BP_LOCATIONS (bl, blp_tmp)
2526 {
2527 /* We only want to update software breakpoints and hardware
2528 breakpoints. */
2529 if (!is_breakpoint (bl->owner))
2530 continue;
2531
2532 /* We only want to update locations that are already inserted
2533 and need updating. This is to avoid unwanted insertion during
2534 deletion of breakpoints. */
2535 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2536 continue;
2537
2538 switch_to_program_space_and_thread (bl->pspace);
2539
2540 /* For targets that support global breakpoints, there's no need
2541 to select an inferior to insert breakpoint to. In fact, even
2542 if we aren't attached to any process yet, we should still
2543 insert breakpoints. */
2544 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2545 && ptid_equal (inferior_ptid, null_ptid))
2546 continue;
2547
2548 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2549 &hw_breakpoint_error);
2550 if (val)
2551 error_flag = val;
2552 }
2553
2554 if (error_flag)
2555 {
2556 target_terminal_ours_for_output ();
2557 error_stream (tmp_error_stream);
2558 }
2559
2560 do_cleanups (cleanups);
2561}
2562
c30eee59 2563/* Used when starting or continuing the program. */
c906108c 2564
74960c60
VP
2565static void
2566insert_breakpoint_locations (void)
c906108c 2567{
a5606eee 2568 struct breakpoint *bpt;
35df4500 2569 struct bp_location *bl, **blp_tmp;
eacd795a 2570 int error_flag = 0;
c906108c 2571 int val = 0;
3fbb6ffa 2572 int disabled_breaks = 0;
81d0cc19 2573 int hw_breakpoint_error = 0;
c906108c 2574
81d0cc19 2575 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2576 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2577
81d0cc19
GS
2578 /* Explicitly mark the warning -- this will only be printed if
2579 there was an error. */
2580 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2581
2582 save_current_space_and_thread ();
2583
35df4500 2584 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2585 {
b775012e 2586 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2587 continue;
2588
4a64f543
MS
2589 /* There is no point inserting thread-specific breakpoints if
2590 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2591 has BL->OWNER always non-NULL. */
35df4500
TJB
2592 if (bl->owner->thread != -1
2593 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2594 continue;
2595
35df4500 2596 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2597
2598 /* For targets that support global breakpoints, there's no need
2599 to select an inferior to insert breakpoint to. In fact, even
2600 if we aren't attached to any process yet, we should still
2601 insert breakpoints. */
2602 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2603 && ptid_equal (inferior_ptid, null_ptid))
2604 continue;
2605
3fbb6ffa
TJB
2606 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2607 &hw_breakpoint_error);
879bfdc2 2608 if (val)
eacd795a 2609 error_flag = val;
879bfdc2 2610 }
c906108c 2611
4a64f543
MS
2612 /* If we failed to insert all locations of a watchpoint, remove
2613 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2614 ALL_BREAKPOINTS (bpt)
2615 {
2616 int some_failed = 0;
2617 struct bp_location *loc;
2618
2619 if (!is_hardware_watchpoint (bpt))
2620 continue;
2621
d6b74ac4 2622 if (!breakpoint_enabled (bpt))
a5606eee 2623 continue;
74960c60
VP
2624
2625 if (bpt->disposition == disp_del_at_next_stop)
2626 continue;
a5606eee
VP
2627
2628 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2629 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2630 {
2631 some_failed = 1;
2632 break;
2633 }
2634 if (some_failed)
2635 {
2636 for (loc = bpt->loc; loc; loc = loc->next)
2637 if (loc->inserted)
2638 remove_breakpoint (loc, mark_uninserted);
2639
2640 hw_breakpoint_error = 1;
2641 fprintf_unfiltered (tmp_error_stream,
2642 "Could not insert hardware watchpoint %d.\n",
2643 bpt->number);
eacd795a 2644 error_flag = -1;
a5606eee
VP
2645 }
2646 }
2647
eacd795a 2648 if (error_flag)
81d0cc19
GS
2649 {
2650 /* If a hardware breakpoint or watchpoint was inserted, add a
2651 message about possibly exhausted resources. */
879bfdc2 2652 if (hw_breakpoint_error)
81d0cc19 2653 {
c6510018
MS
2654 fprintf_unfiltered (tmp_error_stream,
2655 "Could not insert hardware breakpoints:\n\
2656You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2657 }
81d0cc19
GS
2658 target_terminal_ours_for_output ();
2659 error_stream (tmp_error_stream);
2660 }
f7545552
TT
2661
2662 do_cleanups (cleanups);
c906108c
SS
2663}
2664
c30eee59
TJB
2665/* Used when the program stops.
2666 Returns zero if successful, or non-zero if there was a problem
2667 removing a breakpoint location. */
2668
c906108c 2669int
fba45db2 2670remove_breakpoints (void)
c906108c 2671{
35df4500 2672 struct bp_location *bl, **blp_tmp;
3a1bae8e 2673 int val = 0;
c906108c 2674
35df4500 2675 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2676 {
1e4d1764 2677 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2678 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2679 }
3a1bae8e 2680 return val;
c906108c
SS
2681}
2682
6c95b8df
PA
2683/* Remove breakpoints of process PID. */
2684
2685int
2686remove_breakpoints_pid (int pid)
2687{
35df4500 2688 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2689 int val;
2690 struct inferior *inf = find_inferior_pid (pid);
2691
35df4500 2692 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2693 {
35df4500 2694 if (bl->pspace != inf->pspace)
6c95b8df
PA
2695 continue;
2696
35df4500 2697 if (bl->inserted)
6c95b8df 2698 {
35df4500 2699 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2700 if (val != 0)
2701 return val;
2702 }
2703 }
2704 return 0;
2705}
2706
c906108c 2707int
fba45db2 2708reattach_breakpoints (int pid)
c906108c 2709{
6c95b8df 2710 struct cleanup *old_chain;
35df4500 2711 struct bp_location *bl, **blp_tmp;
c906108c 2712 int val;
86b887df 2713 struct ui_file *tmp_error_stream;
3fbb6ffa 2714 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2715 struct inferior *inf;
2716 struct thread_info *tp;
2717
2718 tp = any_live_thread_of_process (pid);
2719 if (tp == NULL)
2720 return 1;
2721
2722 inf = find_inferior_pid (pid);
2723 old_chain = save_inferior_ptid ();
2724
2725 inferior_ptid = tp->ptid;
a4954f26 2726
86b887df 2727 tmp_error_stream = mem_fileopen ();
a4954f26 2728 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2729
35df4500 2730 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2731 {
35df4500 2732 if (bl->pspace != inf->pspace)
6c95b8df
PA
2733 continue;
2734
35df4500 2735 if (bl->inserted)
c5aa993b 2736 {
35df4500 2737 bl->inserted = 0;
3fbb6ffa 2738 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2739 if (val != 0)
2740 {
ce696e05 2741 do_cleanups (old_chain);
c5aa993b
JM
2742 return val;
2743 }
2744 }
2745 }
ce696e05 2746 do_cleanups (old_chain);
c906108c
SS
2747 return 0;
2748}
2749
e58b0e63
PA
2750static int internal_breakpoint_number = -1;
2751
84f4c1fe
PM
2752/* Set the breakpoint number of B, depending on the value of INTERNAL.
2753 If INTERNAL is non-zero, the breakpoint number will be populated
2754 from internal_breakpoint_number and that variable decremented.
e5dd4106 2755 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2756 breakpoint_count and that value incremented. Internal breakpoints
2757 do not set the internal var bpnum. */
2758static void
2759set_breakpoint_number (int internal, struct breakpoint *b)
2760{
2761 if (internal)
2762 b->number = internal_breakpoint_number--;
2763 else
2764 {
2765 set_breakpoint_count (breakpoint_count + 1);
2766 b->number = breakpoint_count;
2767 }
2768}
2769
e62c965a 2770static struct breakpoint *
a6d9a66e 2771create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2772 CORE_ADDR address, enum bptype type,
c0a91b2b 2773 const struct breakpoint_ops *ops)
e62c965a 2774{
e62c965a
PP
2775 struct symtab_and_line sal;
2776 struct breakpoint *b;
2777
4a64f543 2778 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2779
2780 sal.pc = address;
2781 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2782 sal.pspace = current_program_space;
e62c965a 2783
06edf0c0 2784 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2785 b->number = internal_breakpoint_number--;
2786 b->disposition = disp_donttouch;
2787
2788 return b;
2789}
2790
17450429
PP
2791static const char *const longjmp_names[] =
2792 {
2793 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2794 };
2795#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2796
2797/* Per-objfile data private to breakpoint.c. */
2798struct breakpoint_objfile_data
2799{
2800 /* Minimal symbol for "_ovly_debug_event" (if any). */
2801 struct minimal_symbol *overlay_msym;
2802
2803 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2804 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2805
28106bc2
SDJ
2806 /* True if we have looked for longjmp probes. */
2807 int longjmp_searched;
2808
2809 /* SystemTap probe points for longjmp (if any). */
2810 VEC (probe_p) *longjmp_probes;
2811
17450429
PP
2812 /* Minimal symbol for "std::terminate()" (if any). */
2813 struct minimal_symbol *terminate_msym;
2814
2815 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2816 struct minimal_symbol *exception_msym;
28106bc2
SDJ
2817
2818 /* True if we have looked for exception probes. */
2819 int exception_searched;
2820
2821 /* SystemTap probe points for unwinding (if any). */
2822 VEC (probe_p) *exception_probes;
17450429
PP
2823};
2824
2825static const struct objfile_data *breakpoint_objfile_key;
2826
2827/* Minimal symbol not found sentinel. */
2828static struct minimal_symbol msym_not_found;
2829
2830/* Returns TRUE if MSYM point to the "not found" sentinel. */
2831
2832static int
2833msym_not_found_p (const struct minimal_symbol *msym)
2834{
2835 return msym == &msym_not_found;
2836}
2837
2838/* Return per-objfile data needed by breakpoint.c.
2839 Allocate the data if necessary. */
2840
2841static struct breakpoint_objfile_data *
2842get_breakpoint_objfile_data (struct objfile *objfile)
2843{
2844 struct breakpoint_objfile_data *bp_objfile_data;
2845
2846 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2847 if (bp_objfile_data == NULL)
2848 {
2849 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2850 sizeof (*bp_objfile_data));
2851
2852 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2853 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2854 }
2855 return bp_objfile_data;
2856}
2857
28106bc2
SDJ
2858static void
2859free_breakpoint_probes (struct objfile *obj, void *data)
2860{
2861 struct breakpoint_objfile_data *bp_objfile_data = data;
2862
2863 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2864 VEC_free (probe_p, bp_objfile_data->exception_probes);
2865}
2866
e62c965a 2867static void
af02033e 2868create_overlay_event_breakpoint (void)
e62c965a 2869{
69de3c6a 2870 struct objfile *objfile;
af02033e 2871 const char *const func_name = "_ovly_debug_event";
e62c965a 2872
69de3c6a
PP
2873 ALL_OBJFILES (objfile)
2874 {
2875 struct breakpoint *b;
17450429
PP
2876 struct breakpoint_objfile_data *bp_objfile_data;
2877 CORE_ADDR addr;
69de3c6a 2878
17450429
PP
2879 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2880
2881 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2882 continue;
2883
2884 if (bp_objfile_data->overlay_msym == NULL)
2885 {
2886 struct minimal_symbol *m;
2887
2888 m = lookup_minimal_symbol_text (func_name, objfile);
2889 if (m == NULL)
2890 {
2891 /* Avoid future lookups in this objfile. */
2892 bp_objfile_data->overlay_msym = &msym_not_found;
2893 continue;
2894 }
2895 bp_objfile_data->overlay_msym = m;
2896 }
e62c965a 2897
17450429
PP
2898 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2899 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2900 bp_overlay_event,
2901 &internal_breakpoint_ops);
69de3c6a 2902 b->addr_string = xstrdup (func_name);
e62c965a 2903
69de3c6a
PP
2904 if (overlay_debugging == ovly_auto)
2905 {
2906 b->enable_state = bp_enabled;
2907 overlay_events_enabled = 1;
2908 }
2909 else
2910 {
2911 b->enable_state = bp_disabled;
2912 overlay_events_enabled = 0;
2913 }
e62c965a
PP
2914 }
2915 update_global_location_list (1);
2916}
2917
0fd8e87f 2918static void
af02033e 2919create_longjmp_master_breakpoint (void)
0fd8e87f 2920{
6c95b8df 2921 struct program_space *pspace;
6c95b8df
PA
2922 struct cleanup *old_chain;
2923
2924 old_chain = save_current_program_space ();
0fd8e87f 2925
6c95b8df 2926 ALL_PSPACES (pspace)
af02033e
PP
2927 {
2928 struct objfile *objfile;
2929
2930 set_current_program_space (pspace);
2931
2932 ALL_OBJFILES (objfile)
0fd8e87f 2933 {
af02033e
PP
2934 int i;
2935 struct gdbarch *gdbarch;
17450429 2936 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2937
af02033e
PP
2938 gdbarch = get_objfile_arch (objfile);
2939 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2940 continue;
2941
17450429
PP
2942 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2943
28106bc2
SDJ
2944 if (!bp_objfile_data->longjmp_searched)
2945 {
2946 bp_objfile_data->longjmp_probes
2947 = find_probes_in_objfile (objfile, "libc", "longjmp");
2948 bp_objfile_data->longjmp_searched = 1;
2949 }
2950
2951 if (bp_objfile_data->longjmp_probes != NULL)
2952 {
2953 int i;
2954 struct probe *probe;
2955 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2956
2957 for (i = 0;
2958 VEC_iterate (probe_p,
2959 bp_objfile_data->longjmp_probes,
2960 i, probe);
2961 ++i)
2962 {
2963 struct breakpoint *b;
2964
2965 b = create_internal_breakpoint (gdbarch, probe->address,
2966 bp_longjmp_master,
2967 &internal_breakpoint_ops);
2968 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2969 b->enable_state = bp_disabled;
2970 }
2971
2972 continue;
2973 }
2974
17450429 2975 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2976 {
2977 struct breakpoint *b;
af02033e 2978 const char *func_name;
17450429 2979 CORE_ADDR addr;
6c95b8df 2980
17450429 2981 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2982 continue;
0fd8e87f 2983
17450429
PP
2984 func_name = longjmp_names[i];
2985 if (bp_objfile_data->longjmp_msym[i] == NULL)
2986 {
2987 struct minimal_symbol *m;
2988
2989 m = lookup_minimal_symbol_text (func_name, objfile);
2990 if (m == NULL)
2991 {
2992 /* Prevent future lookups in this objfile. */
2993 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2994 continue;
2995 }
2996 bp_objfile_data->longjmp_msym[i] = m;
2997 }
2998
2999 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3000 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3001 &internal_breakpoint_ops);
af02033e
PP
3002 b->addr_string = xstrdup (func_name);
3003 b->enable_state = bp_disabled;
3004 }
0fd8e87f 3005 }
af02033e 3006 }
0fd8e87f 3007 update_global_location_list (1);
6c95b8df
PA
3008
3009 do_cleanups (old_chain);
0fd8e87f
UW
3010}
3011
af02033e 3012/* Create a master std::terminate breakpoint. */
aa7d318d 3013static void
af02033e 3014create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3015{
3016 struct program_space *pspace;
aa7d318d 3017 struct cleanup *old_chain;
af02033e 3018 const char *const func_name = "std::terminate()";
aa7d318d
TT
3019
3020 old_chain = save_current_program_space ();
3021
3022 ALL_PSPACES (pspace)
17450429
PP
3023 {
3024 struct objfile *objfile;
3025 CORE_ADDR addr;
3026
3027 set_current_program_space (pspace);
3028
aa7d318d
TT
3029 ALL_OBJFILES (objfile)
3030 {
3031 struct breakpoint *b;
17450429 3032 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3033
17450429 3034 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3035
17450429
PP
3036 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3037 continue;
3038
3039 if (bp_objfile_data->terminate_msym == NULL)
3040 {
3041 struct minimal_symbol *m;
3042
3043 m = lookup_minimal_symbol (func_name, NULL, objfile);
3044 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3045 && MSYMBOL_TYPE (m) != mst_file_text))
3046 {
3047 /* Prevent future lookups in this objfile. */
3048 bp_objfile_data->terminate_msym = &msym_not_found;
3049 continue;
3050 }
3051 bp_objfile_data->terminate_msym = m;
3052 }
aa7d318d 3053
17450429
PP
3054 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3055 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3056 bp_std_terminate_master,
3057 &internal_breakpoint_ops);
aa7d318d
TT
3058 b->addr_string = xstrdup (func_name);
3059 b->enable_state = bp_disabled;
3060 }
17450429
PP
3061 }
3062
aa7d318d
TT
3063 update_global_location_list (1);
3064
3065 do_cleanups (old_chain);
3066}
3067
186c406b
TT
3068/* Install a master breakpoint on the unwinder's debug hook. */
3069
70221824 3070static void
186c406b
TT
3071create_exception_master_breakpoint (void)
3072{
3073 struct objfile *objfile;
17450429 3074 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3075
3076 ALL_OBJFILES (objfile)
3077 {
17450429
PP
3078 struct breakpoint *b;
3079 struct gdbarch *gdbarch;
3080 struct breakpoint_objfile_data *bp_objfile_data;
3081 CORE_ADDR addr;
3082
3083 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3084
28106bc2
SDJ
3085 /* We prefer the SystemTap probe point if it exists. */
3086 if (!bp_objfile_data->exception_searched)
3087 {
3088 bp_objfile_data->exception_probes
3089 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3090 bp_objfile_data->exception_searched = 1;
3091 }
3092
3093 if (bp_objfile_data->exception_probes != NULL)
3094 {
3095 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3096 int i;
3097 struct probe *probe;
3098
3099 for (i = 0;
3100 VEC_iterate (probe_p,
3101 bp_objfile_data->exception_probes,
3102 i, probe);
3103 ++i)
3104 {
3105 struct breakpoint *b;
3106
3107 b = create_internal_breakpoint (gdbarch, probe->address,
3108 bp_exception_master,
3109 &internal_breakpoint_ops);
3110 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3111 b->enable_state = bp_disabled;
3112 }
3113
3114 continue;
3115 }
3116
3117 /* Otherwise, try the hook function. */
3118
17450429
PP
3119 if (msym_not_found_p (bp_objfile_data->exception_msym))
3120 continue;
3121
3122 gdbarch = get_objfile_arch (objfile);
186c406b 3123
17450429 3124 if (bp_objfile_data->exception_msym == NULL)
186c406b 3125 {
17450429 3126 struct minimal_symbol *debug_hook;
186c406b 3127
17450429
PP
3128 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3129 if (debug_hook == NULL)
3130 {
3131 bp_objfile_data->exception_msym = &msym_not_found;
3132 continue;
3133 }
3134
3135 bp_objfile_data->exception_msym = debug_hook;
186c406b 3136 }
17450429
PP
3137
3138 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3139 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3140 &current_target);
06edf0c0
PA
3141 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3142 &internal_breakpoint_ops);
17450429
PP
3143 b->addr_string = xstrdup (func_name);
3144 b->enable_state = bp_disabled;
186c406b
TT
3145 }
3146
3147 update_global_location_list (1);
3148}
3149
c906108c 3150void
fba45db2 3151update_breakpoints_after_exec (void)
c906108c 3152{
35df4500 3153 struct breakpoint *b, *b_tmp;
876fa593 3154 struct bp_location *bploc, **bplocp_tmp;
c906108c 3155
25b22b0a
PA
3156 /* We're about to delete breakpoints from GDB's lists. If the
3157 INSERTED flag is true, GDB will try to lift the breakpoints by
3158 writing the breakpoints' "shadow contents" back into memory. The
3159 "shadow contents" are NOT valid after an exec, so GDB should not
3160 do that. Instead, the target is responsible from marking
3161 breakpoints out as soon as it detects an exec. We don't do that
3162 here instead, because there may be other attempts to delete
3163 breakpoints after detecting an exec and before reaching here. */
876fa593 3164 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3165 if (bploc->pspace == current_program_space)
3166 gdb_assert (!bploc->inserted);
c906108c 3167
35df4500 3168 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3169 {
6c95b8df
PA
3170 if (b->pspace != current_program_space)
3171 continue;
3172
4a64f543 3173 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3174 if (b->type == bp_shlib_event)
3175 {
3176 delete_breakpoint (b);
3177 continue;
3178 }
c906108c 3179
4a64f543 3180 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3181 if (b->type == bp_jit_event)
3182 {
3183 delete_breakpoint (b);
3184 continue;
3185 }
3186
1900040c 3187 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3188 as must overlay event and longjmp master breakpoints. */
3189 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3190 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3191 || b->type == bp_exception_master)
c4093a6a
JM
3192 {
3193 delete_breakpoint (b);
3194 continue;
3195 }
3196
4a64f543 3197 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3198 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3199 {
3200 delete_breakpoint (b);
3201 continue;
3202 }
3203
611c83ae
PA
3204 /* Longjmp and longjmp-resume breakpoints are also meaningless
3205 after an exec. */
186c406b
TT
3206 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3207 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3208 {
3209 delete_breakpoint (b);
3210 continue;
3211 }
3212
ce78b96d
JB
3213 if (b->type == bp_catchpoint)
3214 {
3215 /* For now, none of the bp_catchpoint breakpoints need to
3216 do anything at this point. In the future, if some of
3217 the catchpoints need to something, we will need to add
3218 a new method, and call this method from here. */
3219 continue;
3220 }
3221
c5aa993b
JM
3222 /* bp_finish is a special case. The only way we ought to be able
3223 to see one of these when an exec() has happened, is if the user
3224 caught a vfork, and then said "finish". Ordinarily a finish just
3225 carries them to the call-site of the current callee, by setting
3226 a temporary bp there and resuming. But in this case, the finish
3227 will carry them entirely through the vfork & exec.
3228
3229 We don't want to allow a bp_finish to remain inserted now. But
3230 we can't safely delete it, 'cause finish_command has a handle to
3231 the bp on a bpstat, and will later want to delete it. There's a
3232 chance (and I've seen it happen) that if we delete the bp_finish
3233 here, that its storage will get reused by the time finish_command
3234 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3235 We really must allow finish_command to delete a bp_finish.
3236
e5dd4106 3237 In the absence of a general solution for the "how do we know
53a5351d
JM
3238 it's safe to delete something others may have handles to?"
3239 problem, what we'll do here is just uninsert the bp_finish, and
3240 let finish_command delete it.
3241
3242 (We know the bp_finish is "doomed" in the sense that it's
3243 momentary, and will be deleted as soon as finish_command sees
3244 the inferior stopped. So it doesn't matter that the bp's
3245 address is probably bogus in the new a.out, unlike e.g., the
3246 solib breakpoints.) */
c5aa993b 3247
c5aa993b
JM
3248 if (b->type == bp_finish)
3249 {
3250 continue;
3251 }
3252
3253 /* Without a symbolic address, we have little hope of the
3254 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3255 a.out. */
c5aa993b
JM
3256 if (b->addr_string == NULL)
3257 {
3258 delete_breakpoint (b);
3259 continue;
3260 }
c5aa993b 3261 }
1900040c 3262 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3263 create_overlay_event_breakpoint ();
3264 create_longjmp_master_breakpoint ();
3265 create_std_terminate_master_breakpoint ();
186c406b 3266 create_exception_master_breakpoint ();
c906108c
SS
3267}
3268
3269int
fba45db2 3270detach_breakpoints (int pid)
c906108c 3271{
35df4500 3272 struct bp_location *bl, **blp_tmp;
3a1bae8e 3273 int val = 0;
ce696e05 3274 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3275 struct inferior *inf = current_inferior ();
c5aa993b 3276
39f77062 3277 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 3278 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3279
6c95b8df 3280 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 3281 inferior_ptid = pid_to_ptid (pid);
35df4500 3282 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3283 {
35df4500 3284 if (bl->pspace != inf->pspace)
6c95b8df
PA
3285 continue;
3286
35df4500
TJB
3287 if (bl->inserted)
3288 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3289 }
d03285ec
UW
3290
3291 /* Detach single-step breakpoints as well. */
3292 detach_single_step_breakpoints ();
3293
ce696e05 3294 do_cleanups (old_chain);
3a1bae8e 3295 return val;
c906108c
SS
3296}
3297
35df4500 3298/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3299 Note that this is used to detach breakpoints from a child fork.
3300 When we get here, the child isn't in the inferior list, and neither
3301 do we have objects to represent its address space --- we should
35df4500 3302 *not* look at bl->pspace->aspace here. */
6c95b8df 3303
c906108c 3304static int
35df4500 3305remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3306{
3307 int val;
c5aa993b 3308
35df4500
TJB
3309 /* BL is never in moribund_locations by our callers. */
3310 gdb_assert (bl->owner != NULL);
2bdf28a0 3311
35df4500 3312 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3313 /* Permanent breakpoints cannot be inserted or removed. */
3314 return 0;
3315
74960c60
VP
3316 /* The type of none suggests that owner is actually deleted.
3317 This should not ever happen. */
35df4500 3318 gdb_assert (bl->owner->type != bp_none);
0bde7532 3319
35df4500
TJB
3320 if (bl->loc_type == bp_loc_software_breakpoint
3321 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3322 {
c02f5703
MS
3323 /* "Normal" instruction breakpoint: either the standard
3324 trap-instruction bp (bp_breakpoint), or a
3325 bp_hardware_breakpoint. */
3326
3327 /* First check to see if we have to handle an overlay. */
3328 if (overlay_debugging == ovly_off
35df4500
TJB
3329 || bl->section == NULL
3330 || !(section_is_overlay (bl->section)))
c02f5703
MS
3331 {
3332 /* No overlay handling: just remove the breakpoint. */
348d480f 3333 val = bl->owner->ops->remove_location (bl);
c02f5703 3334 }
c906108c
SS
3335 else
3336 {
4a64f543 3337 /* This breakpoint is in an overlay section.
c02f5703
MS
3338 Did we set a breakpoint at the LMA? */
3339 if (!overlay_events_enabled)
3340 {
3341 /* Yes -- overlay event support is not active, so we
3342 should have set a breakpoint at the LMA. Remove it.
3343 */
c02f5703
MS
3344 /* Ignore any failures: if the LMA is in ROM, we will
3345 have already warned when we failed to insert it. */
35df4500
TJB
3346 if (bl->loc_type == bp_loc_hardware_breakpoint)
3347 target_remove_hw_breakpoint (bl->gdbarch,
3348 &bl->overlay_target_info);
c02f5703 3349 else
35df4500
TJB
3350 target_remove_breakpoint (bl->gdbarch,
3351 &bl->overlay_target_info);
c02f5703
MS
3352 }
3353 /* Did we set a breakpoint at the VMA?
3354 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3355 if (bl->inserted)
c906108c 3356 {
c02f5703
MS
3357 /* Yes -- remove it. Previously we did not bother to
3358 remove the breakpoint if the section had been
3359 unmapped, but let's not rely on that being safe. We
3360 don't know what the overlay manager might do. */
aa67235e
UW
3361
3362 /* However, we should remove *software* breakpoints only
3363 if the section is still mapped, or else we overwrite
3364 wrong code with the saved shadow contents. */
348d480f
PA
3365 if (bl->loc_type == bp_loc_hardware_breakpoint
3366 || section_is_mapped (bl->section))
3367 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3368 else
3369 val = 0;
c906108c 3370 }
c02f5703
MS
3371 else
3372 {
3373 /* No -- not inserted, so no need to remove. No error. */
3374 val = 0;
3375 }
c906108c 3376 }
879d1e6b
UW
3377
3378 /* In some cases, we might not be able to remove a breakpoint
3379 in a shared library that has already been removed, but we
3380 have not yet processed the shlib unload event. */
35df4500 3381 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3382 val = 0;
3383
c906108c
SS
3384 if (val)
3385 return val;
35df4500 3386 bl->inserted = (is == mark_inserted);
c906108c 3387 }
35df4500 3388 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3389 {
77b06cd7
TJB
3390 gdb_assert (bl->owner->ops != NULL
3391 && bl->owner->ops->remove_location != NULL);
3392
35df4500 3393 bl->inserted = (is == mark_inserted);
77b06cd7 3394 bl->owner->ops->remove_location (bl);
2e70b7b9 3395
c906108c 3396 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3397 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3398 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3399 bl->owner->number);
c906108c 3400 }
35df4500
TJB
3401 else if (bl->owner->type == bp_catchpoint
3402 && breakpoint_enabled (bl->owner)
3403 && !bl->duplicate)
ce78b96d 3404 {
77b06cd7
TJB
3405 gdb_assert (bl->owner->ops != NULL
3406 && bl->owner->ops->remove_location != NULL);
ce78b96d 3407
77b06cd7 3408 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3409 if (val)
3410 return val;
77b06cd7 3411
35df4500 3412 bl->inserted = (is == mark_inserted);
ce78b96d 3413 }
c906108c
SS
3414
3415 return 0;
3416}
3417
6c95b8df 3418static int
35df4500 3419remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3420{
3421 int ret;
3422 struct cleanup *old_chain;
3423
35df4500
TJB
3424 /* BL is never in moribund_locations by our callers. */
3425 gdb_assert (bl->owner != NULL);
2bdf28a0 3426
35df4500 3427 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3428 /* Permanent breakpoints cannot be inserted or removed. */
3429 return 0;
3430
3431 /* The type of none suggests that owner is actually deleted.
3432 This should not ever happen. */
35df4500 3433 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3434
3435 old_chain = save_current_space_and_thread ();
3436
35df4500 3437 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3438
35df4500 3439 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3440
3441 do_cleanups (old_chain);
3442 return ret;
3443}
3444
c906108c
SS
3445/* Clear the "inserted" flag in all breakpoints. */
3446
25b22b0a 3447void
fba45db2 3448mark_breakpoints_out (void)
c906108c 3449{
35df4500 3450 struct bp_location *bl, **blp_tmp;
c906108c 3451
35df4500
TJB
3452 ALL_BP_LOCATIONS (bl, blp_tmp)
3453 if (bl->pspace == current_program_space)
3454 bl->inserted = 0;
c906108c
SS
3455}
3456
53a5351d
JM
3457/* Clear the "inserted" flag in all breakpoints and delete any
3458 breakpoints which should go away between runs of the program.
c906108c
SS
3459
3460 Plus other such housekeeping that has to be done for breakpoints
3461 between runs.
3462
53a5351d
JM
3463 Note: this function gets called at the end of a run (by
3464 generic_mourn_inferior) and when a run begins (by
4a64f543 3465 init_wait_for_inferior). */
c906108c
SS
3466
3467
3468
3469void
fba45db2 3470breakpoint_init_inferior (enum inf_context context)
c906108c 3471{
35df4500
TJB
3472 struct breakpoint *b, *b_tmp;
3473 struct bp_location *bl, **blp_tmp;
1c5cfe86 3474 int ix;
6c95b8df 3475 struct program_space *pspace = current_program_space;
c906108c 3476
50c71eaf
PA
3477 /* If breakpoint locations are shared across processes, then there's
3478 nothing to do. */
2567c7d9 3479 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
3480 return;
3481
35df4500 3482 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3483 {
35df4500
TJB
3484 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3485 if (bl->pspace == pspace
3486 && bl->owner->enable_state != bp_permanent)
3487 bl->inserted = 0;
6c95b8df 3488 }
075f6582 3489
35df4500 3490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3491 {
6c95b8df
PA
3492 if (b->loc && b->loc->pspace != pspace)
3493 continue;
3494
c5aa993b
JM
3495 switch (b->type)
3496 {
3497 case bp_call_dummy:
c906108c 3498
c5aa993b 3499 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3500 cause problems when the inferior is rerun, so we better get
3501 rid of it. */
3502
3503 case bp_watchpoint_scope:
3504
3505 /* Also get rid of scope breakpoints. */
3506
3507 case bp_shlib_event:
3508
3509 /* Also remove solib event breakpoints. Their addresses may
3510 have changed since the last time we ran the program.
3511 Actually we may now be debugging against different target;
3512 and so the solib backend that installed this breakpoint may
3513 not be used in by the target. E.g.,
3514
3515 (gdb) file prog-linux
3516 (gdb) run # native linux target
3517 ...
3518 (gdb) kill
3519 (gdb) file prog-win.exe
3520 (gdb) tar rem :9999 # remote Windows gdbserver.
3521 */
c906108c 3522
f59f708a
PA
3523 case bp_step_resume:
3524
3525 /* Also remove step-resume breakpoints. */
3526
c5aa993b
JM
3527 delete_breakpoint (b);
3528 break;
c906108c 3529
c5aa993b
JM
3530 case bp_watchpoint:
3531 case bp_hardware_watchpoint:
3532 case bp_read_watchpoint:
3533 case bp_access_watchpoint:
3a5c3e22
PA
3534 {
3535 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3536
3a5c3e22
PA
3537 /* Likewise for watchpoints on local expressions. */
3538 if (w->exp_valid_block != NULL)
3539 delete_breakpoint (b);
3540 else if (context == inf_starting)
3541 {
3542 /* Reset val field to force reread of starting value in
3543 insert_breakpoints. */
3544 if (w->val)
3545 value_free (w->val);
3546 w->val = NULL;
3547 w->val_valid = 0;
c860120c 3548 }
3a5c3e22 3549 }
c5aa993b
JM
3550 break;
3551 default:
c5aa993b
JM
3552 break;
3553 }
3554 }
1c5cfe86
PA
3555
3556 /* Get rid of the moribund locations. */
35df4500
TJB
3557 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3558 decref_bp_location (&bl);
1c5cfe86 3559 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3560}
3561
6c95b8df
PA
3562/* These functions concern about actual breakpoints inserted in the
3563 target --- to e.g. check if we need to do decr_pc adjustment or if
3564 we need to hop over the bkpt --- so we check for address space
3565 match, not program space. */
3566
c2c6d25f
JM
3567/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3568 exists at PC. It returns ordinary_breakpoint_here if it's an
3569 ordinary breakpoint, or permanent_breakpoint_here if it's a
3570 permanent breakpoint.
3571 - When continuing from a location with an ordinary breakpoint, we
3572 actually single step once before calling insert_breakpoints.
e5dd4106 3573 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3574 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3575 the target, to advance the PC past the breakpoint. */
c906108c 3576
c2c6d25f 3577enum breakpoint_here
6c95b8df 3578breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3579{
35df4500 3580 struct bp_location *bl, **blp_tmp;
c2c6d25f 3581 int any_breakpoint_here = 0;
c906108c 3582
35df4500 3583 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3584 {
35df4500
TJB
3585 if (bl->loc_type != bp_loc_software_breakpoint
3586 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3587 continue;
3588
f1310107 3589 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3590 if ((breakpoint_enabled (bl->owner)
3591 || bl->owner->enable_state == bp_permanent)
f1310107 3592 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3593 {
3594 if (overlay_debugging
35df4500
TJB
3595 && section_is_overlay (bl->section)
3596 && !section_is_mapped (bl->section))
075f6582 3597 continue; /* unmapped overlay -- can't be a match */
35df4500 3598 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3599 return permanent_breakpoint_here;
3600 else
3601 any_breakpoint_here = 1;
3602 }
3603 }
c906108c 3604
c2c6d25f 3605 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3606}
3607
1c5cfe86
PA
3608/* Return true if there's a moribund breakpoint at PC. */
3609
3610int
6c95b8df 3611moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3612{
3613 struct bp_location *loc;
3614 int ix;
3615
3616 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3617 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3618 return 1;
3619
3620 return 0;
3621}
c2c6d25f 3622
c36b740a 3623/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3624 inserted using regular breakpoint_chain / bp_location array
3625 mechanism. This does not check for single-step breakpoints, which
3626 are inserted and removed using direct target manipulation. */
c906108c
SS
3627
3628int
4a64f543
MS
3629regular_breakpoint_inserted_here_p (struct address_space *aspace,
3630 CORE_ADDR pc)
c906108c 3631{
35df4500 3632 struct bp_location *bl, **blp_tmp;
c906108c 3633
35df4500 3634 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3635 {
35df4500
TJB
3636 if (bl->loc_type != bp_loc_software_breakpoint
3637 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3638 continue;
3639
35df4500 3640 if (bl->inserted
f1310107 3641 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3642 {
3643 if (overlay_debugging
35df4500
TJB
3644 && section_is_overlay (bl->section)
3645 && !section_is_mapped (bl->section))
075f6582
DJ
3646 continue; /* unmapped overlay -- can't be a match */
3647 else
3648 return 1;
3649 }
c5aa993b 3650 }
c36b740a
VP
3651 return 0;
3652}
3653
3654/* Returns non-zero iff there's either regular breakpoint
3655 or a single step breakpoint inserted at PC. */
3656
3657int
6c95b8df 3658breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3659{
6c95b8df 3660 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3661 return 1;
c906108c 3662
6c95b8df 3663 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3664 return 1;
3665
c906108c
SS
3666 return 0;
3667}
3668
4fa8626c
DJ
3669/* This function returns non-zero iff there is a software breakpoint
3670 inserted at PC. */
3671
3672int
3e43a32a
MS
3673software_breakpoint_inserted_here_p (struct address_space *aspace,
3674 CORE_ADDR pc)
4fa8626c 3675{
35df4500 3676 struct bp_location *bl, **blp_tmp;
4fa8626c 3677
35df4500 3678 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3679 {
35df4500 3680 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3681 continue;
3682
35df4500
TJB
3683 if (bl->inserted
3684 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3685 aspace, pc))
4fa8626c
DJ
3686 {
3687 if (overlay_debugging
35df4500
TJB
3688 && section_is_overlay (bl->section)
3689 && !section_is_mapped (bl->section))
4fa8626c
DJ
3690 continue; /* unmapped overlay -- can't be a match */
3691 else
3692 return 1;
3693 }
3694 }
3695
1aafd4da 3696 /* Also check for software single-step breakpoints. */
6c95b8df 3697 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3698 return 1;
3699
4fa8626c
DJ
3700 return 0;
3701}
3702
9093389c
PA
3703int
3704hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3705 CORE_ADDR addr, ULONGEST len)
3706{
3707 struct breakpoint *bpt;
3708
3709 ALL_BREAKPOINTS (bpt)
3710 {
3711 struct bp_location *loc;
3712
3713 if (bpt->type != bp_hardware_watchpoint
3714 && bpt->type != bp_access_watchpoint)
3715 continue;
3716
3717 if (!breakpoint_enabled (bpt))
3718 continue;
3719
3720 for (loc = bpt->loc; loc; loc = loc->next)
3721 if (loc->pspace->aspace == aspace && loc->inserted)
3722 {
3723 CORE_ADDR l, h;
3724
3725 /* Check for intersection. */
3726 l = max (loc->address, addr);
3727 h = min (loc->address + loc->length, addr + len);
3728 if (l < h)
3729 return 1;
3730 }
3731 }
3732 return 0;
3733}
3734
075f6582
DJ
3735/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3736 PC is valid for process/thread PTID. */
c906108c
SS
3737
3738int
6c95b8df
PA
3739breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3740 ptid_t ptid)
c906108c 3741{
35df4500 3742 struct bp_location *bl, **blp_tmp;
4a306c9a 3743 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3744 int thread = -1;
4a306c9a 3745 int task = 0;
a6f1cd96 3746
35df4500 3747 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3748 {
35df4500
TJB
3749 if (bl->loc_type != bp_loc_software_breakpoint
3750 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3751 continue;
3752
35df4500
TJB
3753 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3754 if (!breakpoint_enabled (bl->owner)
3755 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3756 continue;
3757
f1310107 3758 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3759 continue;
3760
35df4500 3761 if (bl->owner->thread != -1)
075f6582 3762 {
a6f1cd96
JB
3763 /* This is a thread-specific breakpoint. Check that ptid
3764 matches that thread. If thread hasn't been computed yet,
3765 it is now time to do so. */
3766 if (thread == -1)
3767 thread = pid_to_thread_id (ptid);
35df4500 3768 if (bl->owner->thread != thread)
a6f1cd96 3769 continue;
075f6582 3770 }
a6f1cd96 3771
35df4500 3772 if (bl->owner->task != 0)
4a306c9a
JB
3773 {
3774 /* This is a task-specific breakpoint. Check that ptid
3775 matches that task. If task hasn't been computed yet,
3776 it is now time to do so. */
3777 if (task == 0)
3778 task = ada_get_task_number (ptid);
35df4500 3779 if (bl->owner->task != task)
4a306c9a
JB
3780 continue;
3781 }
3782
a6f1cd96 3783 if (overlay_debugging
35df4500
TJB
3784 && section_is_overlay (bl->section)
3785 && !section_is_mapped (bl->section))
a6f1cd96
JB
3786 continue; /* unmapped overlay -- can't be a match */
3787
3788 return 1;
c5aa993b 3789 }
c906108c
SS
3790
3791 return 0;
3792}
c906108c 3793\f
c5aa993b 3794
c906108c
SS
3795/* bpstat stuff. External routines' interfaces are documented
3796 in breakpoint.h. */
3797
3798int
c326b90e 3799is_catchpoint (struct breakpoint *ep)
c906108c 3800{
533be4dd 3801 return (ep->type == bp_catchpoint);
c906108c
SS
3802}
3803
f431efe5
PA
3804/* Frees any storage that is part of a bpstat. Does not walk the
3805 'next' chain. */
3806
3807static void
198757a8
VP
3808bpstat_free (bpstat bs)
3809{
3810 if (bs->old_val != NULL)
3811 value_free (bs->old_val);
9add0f1b 3812 decref_counted_command_line (&bs->commands);
f431efe5 3813 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3814 xfree (bs);
3815}
3816
c906108c
SS
3817/* Clear a bpstat so that it says we are not at any breakpoint.
3818 Also free any storage that is part of a bpstat. */
3819
3820void
fba45db2 3821bpstat_clear (bpstat *bsp)
c906108c
SS
3822{
3823 bpstat p;
3824 bpstat q;
3825
3826 if (bsp == 0)
3827 return;
3828 p = *bsp;
3829 while (p != NULL)
3830 {
3831 q = p->next;
198757a8 3832 bpstat_free (p);
c906108c
SS
3833 p = q;
3834 }
3835 *bsp = NULL;
3836}
3837
3838/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3839 is part of the bpstat is copied as well. */
3840
3841bpstat
fba45db2 3842bpstat_copy (bpstat bs)
c906108c
SS
3843{
3844 bpstat p = NULL;
3845 bpstat tmp;
3846 bpstat retval = NULL;
3847
3848 if (bs == NULL)
3849 return bs;
3850
3851 for (; bs != NULL; bs = bs->next)
3852 {
3853 tmp = (bpstat) xmalloc (sizeof (*tmp));
3854 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3855 incref_counted_command_line (tmp->commands);
f431efe5 3856 incref_bp_location (tmp->bp_location_at);
31cc81e9 3857 if (bs->old_val != NULL)
3c3185ac
JK
3858 {
3859 tmp->old_val = value_copy (bs->old_val);
3860 release_value (tmp->old_val);
3861 }
31cc81e9 3862
c906108c
SS
3863 if (p == NULL)
3864 /* This is the first thing in the chain. */
3865 retval = tmp;
3866 else
3867 p->next = tmp;
3868 p = tmp;
3869 }
3870 p->next = NULL;
3871 return retval;
3872}
3873
4a64f543 3874/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3875
3876bpstat
fba45db2 3877bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3878{
c5aa993b
JM
3879 if (bsp == NULL)
3880 return NULL;
c906108c 3881
c5aa993b
JM
3882 for (; bsp != NULL; bsp = bsp->next)
3883 {
f431efe5 3884 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3885 return bsp;
3886 }
c906108c
SS
3887 return NULL;
3888}
3889
4a64f543
MS
3890/* Put in *NUM the breakpoint number of the first breakpoint we are
3891 stopped at. *BSP upon return is a bpstat which points to the
3892 remaining breakpoints stopped at (but which is not guaranteed to be
3893 good for anything but further calls to bpstat_num).
3894
8671a17b
PA
3895 Return 0 if passed a bpstat which does not indicate any breakpoints.
3896 Return -1 if stopped at a breakpoint that has been deleted since
3897 we set it.
3898 Return 1 otherwise. */
c906108c
SS
3899
3900int
8671a17b 3901bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3902{
3903 struct breakpoint *b;
3904
3905 if ((*bsp) == NULL)
3906 return 0; /* No more breakpoint values */
8671a17b 3907
4a64f543
MS
3908 /* We assume we'll never have several bpstats that correspond to a
3909 single breakpoint -- otherwise, this function might return the
3910 same number more than once and this will look ugly. */
f431efe5 3911 b = (*bsp)->breakpoint_at;
8671a17b
PA
3912 *bsp = (*bsp)->next;
3913 if (b == NULL)
3914 return -1; /* breakpoint that's been deleted since */
3915
3916 *num = b->number; /* We have its number */
3917 return 1;
c906108c
SS
3918}
3919
e93ca019 3920/* See breakpoint.h. */
c906108c
SS
3921
3922void
e93ca019 3923bpstat_clear_actions (void)
c906108c 3924{
e93ca019
JK
3925 struct thread_info *tp;
3926 bpstat bs;
3927
3928 if (ptid_equal (inferior_ptid, null_ptid))
3929 return;
3930
3931 tp = find_thread_ptid (inferior_ptid);
3932 if (tp == NULL)
3933 return;
3934
3935 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3936 {
9add0f1b 3937 decref_counted_command_line (&bs->commands);
abf85f46 3938
c906108c
SS
3939 if (bs->old_val != NULL)
3940 {
3941 value_free (bs->old_val);
3942 bs->old_val = NULL;
3943 }
3944 }
3945}
3946
f3b1572e
PA
3947/* Called when a command is about to proceed the inferior. */
3948
3949static void
3950breakpoint_about_to_proceed (void)
3951{
3952 if (!ptid_equal (inferior_ptid, null_ptid))
3953 {
3954 struct thread_info *tp = inferior_thread ();
3955
3956 /* Allow inferior function calls in breakpoint commands to not
3957 interrupt the command list. When the call finishes
3958 successfully, the inferior will be standing at the same
3959 breakpoint as if nothing happened. */
16c381f0 3960 if (tp->control.in_infcall)
f3b1572e
PA
3961 return;
3962 }
3963
3964 breakpoint_proceeded = 1;
3965}
3966
4a64f543
MS
3967/* Stub for cleaning up our state if we error-out of a breakpoint
3968 command. */
c906108c 3969static void
4efb68b1 3970cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3971{
3972 executing_breakpoint_commands = 0;
3973}
3974
abf85f46
JK
3975/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3976 or its equivalent. */
3977
3978static int
3979command_line_is_silent (struct command_line *cmd)
3980{
3981 return cmd && (strcmp ("silent", cmd->line) == 0
3982 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3983}
3984
4a64f543
MS
3985/* Execute all the commands associated with all the breakpoints at
3986 this location. Any of these commands could cause the process to
3987 proceed beyond this point, etc. We look out for such changes by
3988 checking the global "breakpoint_proceeded" after each command.
c906108c 3989
347bddb7
PA
3990 Returns true if a breakpoint command resumed the inferior. In that
3991 case, it is the caller's responsibility to recall it again with the
3992 bpstat of the current thread. */
3993
3994static int
3995bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3996{
3997 bpstat bs;
3998 struct cleanup *old_chain;
347bddb7 3999 int again = 0;
c906108c
SS
4000
4001 /* Avoid endless recursion if a `source' command is contained
4002 in bs->commands. */
4003 if (executing_breakpoint_commands)
347bddb7 4004 return 0;
c906108c
SS
4005
4006 executing_breakpoint_commands = 1;
4007 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4008
cf6c5ffb
TT
4009 prevent_dont_repeat ();
4010
4a64f543 4011 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4012 bs = *bsp;
4013
4014 breakpoint_proceeded = 0;
4015 for (; bs != NULL; bs = bs->next)
4016 {
9add0f1b 4017 struct counted_command_line *ccmd;
6c50ab1c
JB
4018 struct command_line *cmd;
4019 struct cleanup *this_cmd_tree_chain;
4020
4021 /* Take ownership of the BSP's command tree, if it has one.
4022
4023 The command tree could legitimately contain commands like
4024 'step' and 'next', which call clear_proceed_status, which
4025 frees stop_bpstat's command tree. To make sure this doesn't
4026 free the tree we're executing out from under us, we need to
4027 take ownership of the tree ourselves. Since a given bpstat's
4028 commands are only executed once, we don't need to copy it; we
4029 can clear the pointer in the bpstat, and make sure we free
4030 the tree when we're done. */
9add0f1b
TT
4031 ccmd = bs->commands;
4032 bs->commands = NULL;
abf85f46
JK
4033 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4034 cmd = ccmd ? ccmd->commands : NULL;
4035 if (command_line_is_silent (cmd))
4036 {
4037 /* The action has been already done by bpstat_stop_status. */
4038 cmd = cmd->next;
4039 }
6c50ab1c 4040
c906108c
SS
4041 while (cmd != NULL)
4042 {
4043 execute_control_command (cmd);
4044
4045 if (breakpoint_proceeded)
4046 break;
4047 else
4048 cmd = cmd->next;
4049 }
6c50ab1c
JB
4050
4051 /* We can free this command tree now. */
4052 do_cleanups (this_cmd_tree_chain);
4053
c906108c 4054 if (breakpoint_proceeded)
32c1e744
VP
4055 {
4056 if (target_can_async_p ())
347bddb7
PA
4057 /* If we are in async mode, then the target might be still
4058 running, not stopped at any breakpoint, so nothing for
4059 us to do here -- just return to the event loop. */
4060 ;
32c1e744
VP
4061 else
4062 /* In sync mode, when execute_control_command returns
4063 we're already standing on the next breakpoint.
347bddb7
PA
4064 Breakpoint commands for that stop were not run, since
4065 execute_command does not run breakpoint commands --
4066 only command_line_handler does, but that one is not
4067 involved in execution of breakpoint commands. So, we
4068 can now execute breakpoint commands. It should be
4069 noted that making execute_command do bpstat actions is
4070 not an option -- in this case we'll have recursive
4071 invocation of bpstat for each breakpoint with a
4072 command, and can easily blow up GDB stack. Instead, we
4073 return true, which will trigger the caller to recall us
4074 with the new stop_bpstat. */
4075 again = 1;
4076 break;
32c1e744 4077 }
c906108c 4078 }
c2b8ed2c 4079 do_cleanups (old_chain);
347bddb7
PA
4080 return again;
4081}
4082
4083void
4084bpstat_do_actions (void)
4085{
353d1d73
JK
4086 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4087
347bddb7
PA
4088 /* Do any commands attached to breakpoint we are stopped at. */
4089 while (!ptid_equal (inferior_ptid, null_ptid)
4090 && target_has_execution
4091 && !is_exited (inferior_ptid)
4092 && !is_executing (inferior_ptid))
4093 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4094 and only return when it is stopped at the next breakpoint, we
4095 keep doing breakpoint actions until it returns false to
4096 indicate the inferior was not resumed. */
16c381f0 4097 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4098 break;
353d1d73
JK
4099
4100 discard_cleanups (cleanup_if_error);
c906108c
SS
4101}
4102
fa4727a6
DJ
4103/* Print out the (old or new) value associated with a watchpoint. */
4104
4105static void
4106watchpoint_value_print (struct value *val, struct ui_file *stream)
4107{
4108 if (val == NULL)
4109 fprintf_unfiltered (stream, _("<unreadable>"));
4110 else
79a45b7d
TT
4111 {
4112 struct value_print_options opts;
4113 get_user_print_options (&opts);
4114 value_print (val, stream, &opts);
4115 }
fa4727a6
DJ
4116}
4117
e514a9d6 4118/* Generic routine for printing messages indicating why we
4a64f543 4119 stopped. The behavior of this function depends on the value
e514a9d6
JM
4120 'print_it' in the bpstat structure. Under some circumstances we
4121 may decide not to print anything here and delegate the task to
4a64f543 4122 normal_stop(). */
e514a9d6
JM
4123
4124static enum print_stop_action
4125print_bp_stop_message (bpstat bs)
4126{
4127 switch (bs->print_it)
4128 {
4129 case print_it_noop:
4a64f543 4130 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4131 return PRINT_UNKNOWN;
4132 break;
4133
4134 case print_it_done:
4135 /* We still want to print the frame, but we already printed the
4a64f543 4136 relevant messages. */
e514a9d6
JM
4137 return PRINT_SRC_AND_LOC;
4138 break;
4139
4140 case print_it_normal:
4f8d1dc6 4141 {
f431efe5
PA
4142 struct breakpoint *b = bs->breakpoint_at;
4143
1a6a67de
TJB
4144 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4145 which has since been deleted. */
4146 if (b == NULL)
4147 return PRINT_UNKNOWN;
4148
348d480f
PA
4149 /* Normal case. Call the breakpoint's print_it method. */
4150 return b->ops->print_it (bs);
4f8d1dc6 4151 }
348d480f 4152 break;
3086aeae 4153
e514a9d6 4154 default:
8e65ff28 4155 internal_error (__FILE__, __LINE__,
e2e0b3e5 4156 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4157 break;
c906108c 4158 }
c906108c
SS
4159}
4160
edcc5120
TT
4161/* A helper function that prints a shared library stopped event. */
4162
4163static void
4164print_solib_event (int is_catchpoint)
4165{
4166 int any_deleted
4167 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4168 int any_added
4169 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4170
4171 if (!is_catchpoint)
4172 {
4173 if (any_added || any_deleted)
4174 ui_out_text (current_uiout,
4175 _("Stopped due to shared library event:\n"));
4176 else
4177 ui_out_text (current_uiout,
4178 _("Stopped due to shared library event (no "
4179 "libraries added or removed)\n"));
4180 }
4181
4182 if (ui_out_is_mi_like_p (current_uiout))
4183 ui_out_field_string (current_uiout, "reason",
4184 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4185
4186 if (any_deleted)
4187 {
4188 struct cleanup *cleanup;
4189 char *name;
4190 int ix;
4191
4192 ui_out_text (current_uiout, _(" Inferior unloaded "));
4193 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4194 "removed");
4195 for (ix = 0;
4196 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4197 ix, name);
4198 ++ix)
4199 {
4200 if (ix > 0)
4201 ui_out_text (current_uiout, " ");
4202 ui_out_field_string (current_uiout, "library", name);
4203 ui_out_text (current_uiout, "\n");
4204 }
4205
4206 do_cleanups (cleanup);
4207 }
4208
4209 if (any_added)
4210 {
4211 struct so_list *iter;
4212 int ix;
4213 struct cleanup *cleanup;
4214
4215 ui_out_text (current_uiout, _(" Inferior loaded "));
4216 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4217 "added");
4218 for (ix = 0;
4219 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4220 ix, iter);
4221 ++ix)
4222 {
4223 if (ix > 0)
4224 ui_out_text (current_uiout, " ");
4225 ui_out_field_string (current_uiout, "library", iter->so_name);
4226 ui_out_text (current_uiout, "\n");
4227 }
4228
4229 do_cleanups (cleanup);
4230 }
4231}
4232
e514a9d6
JM
4233/* Print a message indicating what happened. This is called from
4234 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4235 list - a list of the eventpoints that caused this stop. KIND is
4236 the target_waitkind for the stopping event. This
e514a9d6
JM
4237 routine calls the generic print routine for printing a message
4238 about reasons for stopping. This will print (for example) the
4239 "Breakpoint n," part of the output. The return value of this
4240 routine is one of:
c906108c 4241
4a64f543 4242 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4243 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4244 code to print the location. An example is
c5aa993b
JM
4245 "Breakpoint 1, " which should be followed by
4246 the location.
917317f4 4247 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4248 to also print the location part of the message.
4249 An example is the catch/throw messages, which
4a64f543 4250 don't require a location appended to the end.
917317f4 4251 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4252 further info to be printed. */
c906108c 4253
917317f4 4254enum print_stop_action
36dfb11c 4255bpstat_print (bpstat bs, int kind)
c906108c
SS
4256{
4257 int val;
c5aa993b 4258
c906108c 4259 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4260 (Currently all watchpoints go on the bpstat whether hit or not.
4261 That probably could (should) be changed, provided care is taken
c906108c 4262 with respect to bpstat_explains_signal). */
e514a9d6
JM
4263 for (; bs; bs = bs->next)
4264 {
4265 val = print_bp_stop_message (bs);
4266 if (val == PRINT_SRC_ONLY
4267 || val == PRINT_SRC_AND_LOC
4268 || val == PRINT_NOTHING)
4269 return val;
4270 }
c906108c 4271
36dfb11c
TT
4272 /* If we had hit a shared library event breakpoint,
4273 print_bp_stop_message would print out this message. If we hit an
4274 OS-level shared library event, do the same thing. */
4275 if (kind == TARGET_WAITKIND_LOADED)
4276 {
edcc5120 4277 print_solib_event (0);
36dfb11c
TT
4278 return PRINT_NOTHING;
4279 }
4280
e514a9d6 4281 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4282 with and nothing was printed. */
917317f4 4283 return PRINT_UNKNOWN;
c906108c
SS
4284}
4285
4a64f543
MS
4286/* Evaluate the expression EXP and return 1 if value is zero. This is
4287 used inside a catch_errors to evaluate the breakpoint condition.
4288 The argument is a "struct expression *" that has been cast to a
4289 "char *" to make it pass through catch_errors. */
c906108c
SS
4290
4291static int
4efb68b1 4292breakpoint_cond_eval (void *exp)
c906108c 4293{
278cd55f 4294 struct value *mark = value_mark ();
c5aa993b 4295 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4296
c906108c
SS
4297 value_free_to_mark (mark);
4298 return i;
4299}
4300
5760d0ab 4301/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4302
4303static bpstat
5760d0ab 4304bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4305{
4306 bpstat bs;
4307
4308 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4309 bs->next = NULL;
4310 **bs_link_pointer = bs;
4311 *bs_link_pointer = &bs->next;
f431efe5
PA
4312 bs->breakpoint_at = bl->owner;
4313 bs->bp_location_at = bl;
4314 incref_bp_location (bl);
c906108c
SS
4315 /* If the condition is false, etc., don't do the commands. */
4316 bs->commands = NULL;
4317 bs->old_val = NULL;
4318 bs->print_it = print_it_normal;
4319 return bs;
4320}
4321\f
d983da9c
DJ
4322/* The target has stopped with waitstatus WS. Check if any hardware
4323 watchpoints have triggered, according to the target. */
4324
4325int
4326watchpoints_triggered (struct target_waitstatus *ws)
4327{
d92524f1 4328 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4329 CORE_ADDR addr;
4330 struct breakpoint *b;
4331
4332 if (!stopped_by_watchpoint)
4333 {
4334 /* We were not stopped by a watchpoint. Mark all watchpoints
4335 as not triggered. */
4336 ALL_BREAKPOINTS (b)
cc60f2e3 4337 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4338 {
4339 struct watchpoint *w = (struct watchpoint *) b;
4340
4341 w->watchpoint_triggered = watch_triggered_no;
4342 }
d983da9c
DJ
4343
4344 return 0;
4345 }
4346
4347 if (!target_stopped_data_address (&current_target, &addr))
4348 {
4349 /* We were stopped by a watchpoint, but we don't know where.
4350 Mark all watchpoints as unknown. */
4351 ALL_BREAKPOINTS (b)
cc60f2e3 4352 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4353 {
4354 struct watchpoint *w = (struct watchpoint *) b;
4355
4356 w->watchpoint_triggered = watch_triggered_unknown;
4357 }
d983da9c
DJ
4358
4359 return stopped_by_watchpoint;
4360 }
4361
4362 /* The target could report the data address. Mark watchpoints
4363 affected by this data address as triggered, and all others as not
4364 triggered. */
4365
4366 ALL_BREAKPOINTS (b)
cc60f2e3 4367 if (is_hardware_watchpoint (b))
d983da9c 4368 {
3a5c3e22 4369 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4370 struct bp_location *loc;
d983da9c 4371
3a5c3e22 4372 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4373 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4374 {
3a5c3e22 4375 if (is_masked_watchpoint (b))
9c06b0b4 4376 {
3a5c3e22
PA
4377 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4378 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4379
4380 if (newaddr == start)
4381 {
3a5c3e22 4382 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4383 break;
4384 }
4385 }
4386 /* Exact match not required. Within range is sufficient. */
4387 else if (target_watchpoint_addr_within_range (&current_target,
4388 addr, loc->address,
4389 loc->length))
4390 {
3a5c3e22 4391 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4392 break;
4393 }
4394 }
d983da9c
DJ
4395 }
4396
4397 return 1;
4398}
4399
c906108c
SS
4400/* Possible return values for watchpoint_check (this can't be an enum
4401 because of check_errors). */
4402/* The watchpoint has been deleted. */
4403#define WP_DELETED 1
4404/* The value has changed. */
4405#define WP_VALUE_CHANGED 2
4406/* The value has not changed. */
4407#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4408/* Ignore this watchpoint, no matter if the value changed or not. */
4409#define WP_IGNORE 4
c906108c
SS
4410
4411#define BP_TEMPFLAG 1
4412#define BP_HARDWAREFLAG 2
4413
4a64f543
MS
4414/* Evaluate watchpoint condition expression and check if its value
4415 changed.
553e4c11
JB
4416
4417 P should be a pointer to struct bpstat, but is defined as a void *
4418 in order for this function to be usable with catch_errors. */
c906108c
SS
4419
4420static int
4efb68b1 4421watchpoint_check (void *p)
c906108c
SS
4422{
4423 bpstat bs = (bpstat) p;
3a5c3e22 4424 struct watchpoint *b;
c906108c
SS
4425 struct frame_info *fr;
4426 int within_current_scope;
4427
f431efe5 4428 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4429 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4430 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4431
f6bc2008
PA
4432 /* If this is a local watchpoint, we only want to check if the
4433 watchpoint frame is in scope if the current thread is the thread
4434 that was used to create the watchpoint. */
4435 if (!watchpoint_in_thread_scope (b))
60e1c644 4436 return WP_IGNORE;
f6bc2008 4437
c906108c
SS
4438 if (b->exp_valid_block == NULL)
4439 within_current_scope = 1;
4440 else
4441 {
edb3359d
DJ
4442 struct frame_info *frame = get_current_frame ();
4443 struct gdbarch *frame_arch = get_frame_arch (frame);
4444 CORE_ADDR frame_pc = get_frame_pc (frame);
4445
4a64f543
MS
4446 /* in_function_epilogue_p() returns a non-zero value if we're
4447 still in the function but the stack frame has already been
4448 invalidated. Since we can't rely on the values of local
4449 variables after the stack has been destroyed, we are treating
4450 the watchpoint in that state as `not changed' without further
4451 checking. Don't mark watchpoints as changed if the current
4452 frame is in an epilogue - even if they are in some other
4453 frame, our view of the stack is likely to be wrong and
4454 frame_find_by_id could error out. */
a0f49112 4455 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4456 return WP_IGNORE;
a0f49112 4457
101dcfbe 4458 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4459 within_current_scope = (fr != NULL);
69fbadd5
DJ
4460
4461 /* If we've gotten confused in the unwinder, we might have
4462 returned a frame that can't describe this variable. */
edb3359d
DJ
4463 if (within_current_scope)
4464 {
4465 struct symbol *function;
4466
4467 function = get_frame_function (fr);
4468 if (function == NULL
4469 || !contained_in (b->exp_valid_block,
4470 SYMBOL_BLOCK_VALUE (function)))
4471 within_current_scope = 0;
4472 }
69fbadd5 4473
edb3359d 4474 if (within_current_scope)
c906108c
SS
4475 /* If we end up stopping, the current frame will get selected
4476 in normal_stop. So this call to select_frame won't affect
4477 the user. */
0f7d239c 4478 select_frame (fr);
c906108c 4479 }
c5aa993b 4480
c906108c
SS
4481 if (within_current_scope)
4482 {
4a64f543
MS
4483 /* We use value_{,free_to_}mark because it could be a *long*
4484 time before we return to the command level and call
4485 free_all_values. We can't call free_all_values because we
4486 might be in the middle of evaluating a function call. */
c906108c 4487
0cf6dd15 4488 int pc = 0;
9c06b0b4 4489 struct value *mark;
fa4727a6
DJ
4490 struct value *new_val;
4491
3a5c3e22 4492 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4493 /* Since we don't know the exact trigger address (from
4494 stopped_data_address), just tell the user we've triggered
4495 a mask watchpoint. */
4496 return WP_VALUE_CHANGED;
4497
4498 mark = value_mark ();
0cf6dd15 4499 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 4500
4a64f543
MS
4501 /* We use value_equal_contents instead of value_equal because
4502 the latter coerces an array to a pointer, thus comparing just
4503 the address of the array instead of its contents. This is
4504 not what we want. */
fa4727a6 4505 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4506 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4507 {
fa4727a6
DJ
4508 if (new_val != NULL)
4509 {
4510 release_value (new_val);
4511 value_free_to_mark (mark);
4512 }
c906108c
SS
4513 bs->old_val = b->val;
4514 b->val = new_val;
fa4727a6 4515 b->val_valid = 1;
c906108c
SS
4516 return WP_VALUE_CHANGED;
4517 }
4518 else
4519 {
60e1c644 4520 /* Nothing changed. */
c906108c 4521 value_free_to_mark (mark);
c906108c
SS
4522 return WP_VALUE_NOT_CHANGED;
4523 }
4524 }
4525 else
4526 {
79a45e25
PA
4527 struct ui_out *uiout = current_uiout;
4528
c906108c 4529 /* This seems like the only logical thing to do because
c5aa993b
JM
4530 if we temporarily ignored the watchpoint, then when
4531 we reenter the block in which it is valid it contains
4532 garbage (in the case of a function, it may have two
4533 garbage values, one before and one after the prologue).
4534 So we can't even detect the first assignment to it and
4535 watch after that (since the garbage may or may not equal
4536 the first value assigned). */
348d480f
PA
4537 /* We print all the stop information in
4538 breakpoint_ops->print_it, but in this case, by the time we
4539 call breakpoint_ops->print_it this bp will be deleted
4540 already. So we have no choice but print the information
4541 here. */
9dc5e2a9 4542 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4543 ui_out_field_string
4544 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4545 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4546 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4547 ui_out_text (uiout,
4548 " deleted because the program has left the block in\n\
8b93c638 4549which its expression is valid.\n");
4ce44c66 4550
cdac0397 4551 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4552 decref_counted_command_line (&b->base.commands);
d0fb5eae 4553 watchpoint_del_at_next_stop (b);
c906108c
SS
4554
4555 return WP_DELETED;
4556 }
4557}
4558
18a18393 4559/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4560 breakpoint location BL. This function does not check if we should
4561 stop, only if BL explains the stop. */
4562
18a18393 4563static int
6c95b8df 4564bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4565 struct address_space *aspace, CORE_ADDR bp_addr,
4566 const struct target_waitstatus *ws)
18a18393
VP
4567{
4568 struct breakpoint *b = bl->owner;
4569
348d480f 4570 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4571 gdb_assert (b != NULL);
4572
09ac7c10 4573 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4574}
4575
3a5c3e22
PA
4576/* Determine if the watched values have actually changed, and we
4577 should stop. If not, set BS->stop to 0. */
4578
18a18393
VP
4579static void
4580bpstat_check_watchpoint (bpstat bs)
4581{
2bdf28a0 4582 const struct bp_location *bl;
3a5c3e22 4583 struct watchpoint *b;
2bdf28a0
JK
4584
4585 /* BS is built for existing struct breakpoint. */
f431efe5 4586 bl = bs->bp_location_at;
2bdf28a0 4587 gdb_assert (bl != NULL);
3a5c3e22 4588 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4589 gdb_assert (b != NULL);
18a18393 4590
18a18393 4591 {
18a18393
VP
4592 int must_check_value = 0;
4593
3a5c3e22 4594 if (b->base.type == bp_watchpoint)
18a18393
VP
4595 /* For a software watchpoint, we must always check the
4596 watched value. */
4597 must_check_value = 1;
4598 else if (b->watchpoint_triggered == watch_triggered_yes)
4599 /* We have a hardware watchpoint (read, write, or access)
4600 and the target earlier reported an address watched by
4601 this watchpoint. */
4602 must_check_value = 1;
4603 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4604 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4605 /* We were stopped by a hardware watchpoint, but the target could
4606 not report the data address. We must check the watchpoint's
4607 value. Access and read watchpoints are out of luck; without
4608 a data address, we can't figure it out. */
4609 must_check_value = 1;
3a5c3e22 4610
18a18393
VP
4611 if (must_check_value)
4612 {
3e43a32a
MS
4613 char *message
4614 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4615 b->base.number);
18a18393
VP
4616 struct cleanup *cleanups = make_cleanup (xfree, message);
4617 int e = catch_errors (watchpoint_check, bs, message,
4618 RETURN_MASK_ALL);
4619 do_cleanups (cleanups);
4620 switch (e)
4621 {
4622 case WP_DELETED:
4623 /* We've already printed what needs to be printed. */
4624 bs->print_it = print_it_done;
4625 /* Stop. */
4626 break;
60e1c644
PA
4627 case WP_IGNORE:
4628 bs->print_it = print_it_noop;
4629 bs->stop = 0;
4630 break;
18a18393 4631 case WP_VALUE_CHANGED:
3a5c3e22 4632 if (b->base.type == bp_read_watchpoint)
18a18393 4633 {
85d721b8
PA
4634 /* There are two cases to consider here:
4635
4a64f543 4636 1. We're watching the triggered memory for reads.
85d721b8
PA
4637 In that case, trust the target, and always report
4638 the watchpoint hit to the user. Even though
4639 reads don't cause value changes, the value may
4640 have changed since the last time it was read, and
4641 since we're not trapping writes, we will not see
4642 those, and as such we should ignore our notion of
4643 old value.
4644
4a64f543 4645 2. We're watching the triggered memory for both
85d721b8
PA
4646 reads and writes. There are two ways this may
4647 happen:
4648
4a64f543 4649 2.1. This is a target that can't break on data
85d721b8
PA
4650 reads only, but can break on accesses (reads or
4651 writes), such as e.g., x86. We detect this case
4652 at the time we try to insert read watchpoints.
4653
4a64f543 4654 2.2. Otherwise, the target supports read
85d721b8
PA
4655 watchpoints, but, the user set an access or write
4656 watchpoint watching the same memory as this read
4657 watchpoint.
4658
4659 If we're watching memory writes as well as reads,
4660 ignore watchpoint hits when we find that the
4661 value hasn't changed, as reads don't cause
4662 changes. This still gives false positives when
4663 the program writes the same value to memory as
4664 what there was already in memory (we will confuse
4665 it for a read), but it's much better than
4666 nothing. */
4667
4668 int other_write_watchpoint = 0;
4669
4670 if (bl->watchpoint_type == hw_read)
4671 {
4672 struct breakpoint *other_b;
4673
4674 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4675 if (other_b->type == bp_hardware_watchpoint
4676 || other_b->type == bp_access_watchpoint)
85d721b8 4677 {
3a5c3e22
PA
4678 struct watchpoint *other_w =
4679 (struct watchpoint *) other_b;
4680
4681 if (other_w->watchpoint_triggered
4682 == watch_triggered_yes)
4683 {
4684 other_write_watchpoint = 1;
4685 break;
4686 }
85d721b8
PA
4687 }
4688 }
4689
4690 if (other_write_watchpoint
4691 || bl->watchpoint_type == hw_access)
4692 {
4693 /* We're watching the same memory for writes,
4694 and the value changed since the last time we
4695 updated it, so this trap must be for a write.
4696 Ignore it. */
4697 bs->print_it = print_it_noop;
4698 bs->stop = 0;
4699 }
18a18393
VP
4700 }
4701 break;
4702 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4703 if (b->base.type == bp_hardware_watchpoint
4704 || b->base.type == bp_watchpoint)
18a18393
VP
4705 {
4706 /* Don't stop: write watchpoints shouldn't fire if
4707 the value hasn't changed. */
4708 bs->print_it = print_it_noop;
4709 bs->stop = 0;
4710 }
4711 /* Stop. */
4712 break;
4713 default:
4714 /* Can't happen. */
4715 case 0:
4716 /* Error from catch_errors. */
3a5c3e22 4717 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4718 watchpoint_del_at_next_stop (b);
18a18393
VP
4719 /* We've already printed what needs to be printed. */
4720 bs->print_it = print_it_done;
4721 break;
4722 }
4723 }
4724 else /* must_check_value == 0 */
4725 {
4726 /* This is a case where some watchpoint(s) triggered, but
4727 not at the address of this watchpoint, or else no
4728 watchpoint triggered after all. So don't print
4729 anything for this watchpoint. */
4730 bs->print_it = print_it_noop;
4731 bs->stop = 0;
4732 }
4733 }
4734}
4735
4736
4737/* Check conditions (condition proper, frame, thread and ignore count)
4738 of breakpoint referred to by BS. If we should not stop for this
4739 breakpoint, set BS->stop to 0. */
f431efe5 4740
18a18393
VP
4741static void
4742bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4743{
4744 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4745 const struct bp_location *bl;
4746 struct breakpoint *b;
4747
4748 /* BS is built for existing struct breakpoint. */
f431efe5 4749 bl = bs->bp_location_at;
2bdf28a0 4750 gdb_assert (bl != NULL);
f431efe5 4751 b = bs->breakpoint_at;
2bdf28a0 4752 gdb_assert (b != NULL);
18a18393 4753
b775012e
LM
4754 /* Even if the target evaluated the condition on its end and notified GDB, we
4755 need to do so again since GDB does not know if we stopped due to a
4756 breakpoint or a single step breakpoint. */
4757
18a18393 4758 if (frame_id_p (b->frame_id)
edb3359d 4759 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4760 bs->stop = 0;
4761 else if (bs->stop)
4762 {
4763 int value_is_zero = 0;
60e1c644
PA
4764 struct expression *cond;
4765
7371cf6d
PM
4766 /* Evaluate Python breakpoints that have a "stop"
4767 method implemented. */
4768 if (b->py_bp_object)
4769 bs->stop = gdbpy_should_stop (b->py_bp_object);
4770
60e1c644 4771 if (is_watchpoint (b))
3a5c3e22
PA
4772 {
4773 struct watchpoint *w = (struct watchpoint *) b;
4774
4775 cond = w->cond_exp;
4776 }
60e1c644
PA
4777 else
4778 cond = bl->cond;
4779
f431efe5 4780 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4781 {
60e1c644 4782 int within_current_scope = 1;
3a5c3e22 4783 struct watchpoint * w;
60e1c644 4784
c5bc3a77
DJ
4785 /* We use value_mark and value_free_to_mark because it could
4786 be a long time before we return to the command level and
4787 call free_all_values. We can't call free_all_values
4788 because we might be in the middle of evaluating a
4789 function call. */
4790 struct value *mark = value_mark ();
4791
3a5c3e22
PA
4792 if (is_watchpoint (b))
4793 w = (struct watchpoint *) b;
4794 else
4795 w = NULL;
4796
edb3359d
DJ
4797 /* Need to select the frame, with all that implies so that
4798 the conditions will have the right context. Because we
4799 use the frame, we will not see an inlined function's
4800 variables when we arrive at a breakpoint at the start
4801 of the inlined function; the current frame will be the
4802 call site. */
3a5c3e22 4803 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4804 select_frame (get_current_frame ());
4805 else
4806 {
4807 struct frame_info *frame;
4808
4809 /* For local watchpoint expressions, which particular
4810 instance of a local is being watched matters, so we
4811 keep track of the frame to evaluate the expression
4812 in. To evaluate the condition however, it doesn't
4813 really matter which instantiation of the function
4814 where the condition makes sense triggers the
4815 watchpoint. This allows an expression like "watch
4816 global if q > 10" set in `func', catch writes to
4817 global on all threads that call `func', or catch
4818 writes on all recursive calls of `func' by a single
4819 thread. We simply always evaluate the condition in
4820 the innermost frame that's executing where it makes
4821 sense to evaluate the condition. It seems
4822 intuitive. */
3a5c3e22 4823 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4824 if (frame != NULL)
4825 select_frame (frame);
4826 else
4827 within_current_scope = 0;
4828 }
4829 if (within_current_scope)
4830 value_is_zero
4831 = catch_errors (breakpoint_cond_eval, cond,
4832 "Error in testing breakpoint condition:\n",
4833 RETURN_MASK_ALL);
4834 else
4835 {
4836 warning (_("Watchpoint condition cannot be tested "
4837 "in the current scope"));
4838 /* If we failed to set the right context for this
4839 watchpoint, unconditionally report it. */
4840 value_is_zero = 0;
4841 }
4a64f543 4842 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4843 value_free_to_mark (mark);
18a18393 4844 }
60e1c644
PA
4845
4846 if (cond && value_is_zero)
18a18393
VP
4847 {
4848 bs->stop = 0;
4849 }
4850 else if (b->thread != -1 && b->thread != thread_id)
4851 {
4852 bs->stop = 0;
4853 }
4854 else if (b->ignore_count > 0)
4855 {
4856 b->ignore_count--;
4857 annotate_ignore_count_change ();
4858 bs->stop = 0;
4a64f543 4859 /* Increase the hit count even though we don't stop. */
18a18393 4860 ++(b->hit_count);
8d3788bd 4861 observer_notify_breakpoint_modified (b);
18a18393
VP
4862 }
4863 }
4864}
4865
4866
9709f61c 4867/* Get a bpstat associated with having just stopped at address
d983da9c 4868 BP_ADDR in thread PTID.
c906108c 4869
d983da9c 4870 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4871 don't understand this stop. Result is a chain of bpstat's such
4872 that:
c906108c 4873
c5aa993b 4874 if we don't understand the stop, the result is a null pointer.
c906108c 4875
c5aa993b 4876 if we understand why we stopped, the result is not null.
c906108c 4877
c5aa993b
JM
4878 Each element of the chain refers to a particular breakpoint or
4879 watchpoint at which we have stopped. (We may have stopped for
4880 several reasons concurrently.)
c906108c 4881
c5aa993b
JM
4882 Each element of the chain has valid next, breakpoint_at,
4883 commands, FIXME??? fields. */
c906108c
SS
4884
4885bpstat
6c95b8df 4886bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4887 CORE_ADDR bp_addr, ptid_t ptid,
4888 const struct target_waitstatus *ws)
c906108c 4889{
0d381245 4890 struct breakpoint *b = NULL;
afe38095 4891 struct bp_location *bl;
20874c92 4892 struct bp_location *loc;
5760d0ab
JK
4893 /* First item of allocated bpstat's. */
4894 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4895 /* Pointer to the last thing in the chain currently. */
5760d0ab 4896 bpstat bs;
20874c92 4897 int ix;
429374b8 4898 int need_remove_insert;
f431efe5 4899 int removed_any;
c906108c 4900
f431efe5
PA
4901 /* First, build the bpstat chain with locations that explain a
4902 target stop, while being careful to not set the target running,
4903 as that may invalidate locations (in particular watchpoint
4904 locations are recreated). Resuming will happen here with
4905 breakpoint conditions or watchpoint expressions that include
4906 inferior function calls. */
c5aa993b 4907
429374b8
JK
4908 ALL_BREAKPOINTS (b)
4909 {
4910 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4911 continue;
a5606eee 4912
429374b8
JK
4913 for (bl = b->loc; bl != NULL; bl = bl->next)
4914 {
4a64f543
MS
4915 /* For hardware watchpoints, we look only at the first
4916 location. The watchpoint_check function will work on the
4917 entire expression, not the individual locations. For
4918 read watchpoints, the watchpoints_triggered function has
4919 checked all locations already. */
429374b8
JK
4920 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4921 break;
18a18393 4922
429374b8
JK
4923 if (bl->shlib_disabled)
4924 continue;
c5aa993b 4925
09ac7c10 4926 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4927 continue;
c5aa993b 4928
4a64f543
MS
4929 /* Come here if it's a watchpoint, or if the break address
4930 matches. */
c5aa993b 4931
4a64f543
MS
4932 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4933 explain stop. */
c5aa993b 4934
f431efe5
PA
4935 /* Assume we stop. Should we find a watchpoint that is not
4936 actually triggered, or if the condition of the breakpoint
4937 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4938 bs->stop = 1;
4939 bs->print = 1;
d983da9c 4940
f431efe5
PA
4941 /* If this is a scope breakpoint, mark the associated
4942 watchpoint as triggered so that we will handle the
4943 out-of-scope event. We'll get to the watchpoint next
4944 iteration. */
d0fb5eae 4945 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4946 {
4947 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4948
4949 w->watchpoint_triggered = watch_triggered_yes;
4950 }
f431efe5
PA
4951 }
4952 }
4953
4954 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4955 {
f1310107 4956 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4957 {
5760d0ab 4958 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4959 /* For hits of moribund locations, we should just proceed. */
4960 bs->stop = 0;
4961 bs->print = 0;
4962 bs->print_it = print_it_noop;
4963 }
4964 }
4965
edcc5120
TT
4966 /* A bit of special processing for shlib breakpoints. We need to
4967 process solib loading here, so that the lists of loaded and
4968 unloaded libraries are correct before we handle "catch load" and
4969 "catch unload". */
4970 for (bs = bs_head; bs != NULL; bs = bs->next)
4971 {
5d268276 4972 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4973 {
4974 handle_solib_event ();
4975 break;
4976 }
4977 }
4978
f431efe5
PA
4979 /* Now go through the locations that caused the target to stop, and
4980 check whether we're interested in reporting this stop to higher
4981 layers, or whether we should resume the target transparently. */
4982
4983 removed_any = 0;
4984
5760d0ab 4985 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4986 {
4987 if (!bs->stop)
4988 continue;
4989
f431efe5 4990 b = bs->breakpoint_at;
348d480f
PA
4991 b->ops->check_status (bs);
4992 if (bs->stop)
28010a5d 4993 {
348d480f 4994 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4995
429374b8
JK
4996 if (bs->stop)
4997 {
4998 ++(b->hit_count);
8d3788bd 4999 observer_notify_breakpoint_modified (b);
c906108c 5000
4a64f543 5001 /* We will stop here. */
429374b8
JK
5002 if (b->disposition == disp_disable)
5003 {
816338b5
SS
5004 --(b->enable_count);
5005 if (b->enable_count <= 0
5006 && b->enable_state != bp_permanent)
429374b8 5007 b->enable_state = bp_disabled;
f431efe5 5008 removed_any = 1;
429374b8
JK
5009 }
5010 if (b->silent)
5011 bs->print = 0;
5012 bs->commands = b->commands;
9add0f1b 5013 incref_counted_command_line (bs->commands);
abf85f46
JK
5014 if (command_line_is_silent (bs->commands
5015 ? bs->commands->commands : NULL))
5016 bs->print = 0;
429374b8
JK
5017 }
5018
348d480f 5019 }
a9b3a50f
PA
5020
5021 /* Print nothing for this entry if we don't stop or don't
5022 print. */
5023 if (!bs->stop || !bs->print)
5024 bs->print_it = print_it_noop;
429374b8 5025 }
876fa593 5026
d983da9c
DJ
5027 /* If we aren't stopping, the value of some hardware watchpoint may
5028 not have changed, but the intermediate memory locations we are
5029 watching may have. Don't bother if we're stopping; this will get
5030 done later. */
d832cb68 5031 need_remove_insert = 0;
5760d0ab
JK
5032 if (! bpstat_causes_stop (bs_head))
5033 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5034 if (!bs->stop
f431efe5
PA
5035 && bs->breakpoint_at
5036 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5037 {
3a5c3e22
PA
5038 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5039
5040 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5041 need_remove_insert = 1;
d983da9c
DJ
5042 }
5043
d832cb68 5044 if (need_remove_insert)
2d134ed3 5045 update_global_location_list (1);
f431efe5
PA
5046 else if (removed_any)
5047 update_global_location_list (0);
d832cb68 5048
5760d0ab 5049 return bs_head;
c906108c 5050}
628fe4e4
JK
5051
5052static void
5053handle_jit_event (void)
5054{
5055 struct frame_info *frame;
5056 struct gdbarch *gdbarch;
5057
5058 /* Switch terminal for any messages produced by
5059 breakpoint_re_set. */
5060 target_terminal_ours_for_output ();
5061
5062 frame = get_current_frame ();
5063 gdbarch = get_frame_arch (frame);
5064
5065 jit_event_handler (gdbarch);
5066
5067 target_terminal_inferior ();
5068}
5069
edcc5120
TT
5070/* Handle an solib event by calling solib_add. */
5071
5072void
5073handle_solib_event (void)
5074{
5075 clear_program_space_solib_cache (current_inferior ()->pspace);
5076
5077 /* Check for any newly added shared libraries if we're supposed to
5078 be adding them automatically. Switch terminal for any messages
5079 produced by breakpoint_re_set. */
5080 target_terminal_ours_for_output ();
5081#ifdef SOLIB_ADD
5082 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5083#else
5084 solib_add (NULL, 0, &current_target, auto_solib_add);
5085#endif
5086 target_terminal_inferior ();
5087}
5088
628fe4e4
JK
5089/* Prepare WHAT final decision for infrun. */
5090
5091/* Decide what infrun needs to do with this bpstat. */
5092
c906108c 5093struct bpstat_what
0e30163f 5094bpstat_what (bpstat bs_head)
c906108c 5095{
c906108c 5096 struct bpstat_what retval;
628fe4e4 5097 int jit_event = 0;
0e30163f 5098 bpstat bs;
c906108c 5099
628fe4e4 5100 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5101 retval.call_dummy = STOP_NONE;
186c406b 5102 retval.is_longjmp = 0;
628fe4e4 5103
0e30163f 5104 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5105 {
628fe4e4
JK
5106 /* Extract this BS's action. After processing each BS, we check
5107 if its action overrides all we've seem so far. */
5108 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5109 enum bptype bptype;
5110
c906108c 5111 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5112 {
5113 /* I suspect this can happen if it was a momentary
5114 breakpoint which has since been deleted. */
5115 bptype = bp_none;
5116 }
20874c92 5117 else
f431efe5 5118 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5119
5120 switch (bptype)
c906108c
SS
5121 {
5122 case bp_none:
628fe4e4 5123 break;
c906108c
SS
5124 case bp_breakpoint:
5125 case bp_hardware_breakpoint:
5126 case bp_until:
5127 case bp_finish:
a9b3a50f 5128 case bp_shlib_event:
c906108c
SS
5129 if (bs->stop)
5130 {
5131 if (bs->print)
628fe4e4 5132 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5133 else
628fe4e4 5134 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5135 }
5136 else
628fe4e4 5137 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5138 break;
5139 case bp_watchpoint:
5140 case bp_hardware_watchpoint:
5141 case bp_read_watchpoint:
5142 case bp_access_watchpoint:
5143 if (bs->stop)
5144 {
5145 if (bs->print)
628fe4e4 5146 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5147 else
628fe4e4 5148 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5149 }
5150 else
628fe4e4
JK
5151 {
5152 /* There was a watchpoint, but we're not stopping.
5153 This requires no further action. */
5154 }
c906108c
SS
5155 break;
5156 case bp_longjmp:
186c406b 5157 case bp_exception:
628fe4e4 5158 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 5159 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
5160 break;
5161 case bp_longjmp_resume:
186c406b 5162 case bp_exception_resume:
628fe4e4 5163 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5164 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5165 break;
5166 case bp_step_resume:
5167 if (bs->stop)
628fe4e4
JK
5168 this_action = BPSTAT_WHAT_STEP_RESUME;
5169 else
c906108c 5170 {
628fe4e4
JK
5171 /* It is for the wrong frame. */
5172 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5173 }
c906108c 5174 break;
2c03e5be
PA
5175 case bp_hp_step_resume:
5176 if (bs->stop)
5177 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5178 else
5179 {
5180 /* It is for the wrong frame. */
5181 this_action = BPSTAT_WHAT_SINGLE;
5182 }
5183 break;
c906108c 5184 case bp_watchpoint_scope:
c4093a6a 5185 case bp_thread_event:
1900040c 5186 case bp_overlay_event:
0fd8e87f 5187 case bp_longjmp_master:
aa7d318d 5188 case bp_std_terminate_master:
186c406b 5189 case bp_exception_master:
628fe4e4 5190 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5191 break;
ce78b96d 5192 case bp_catchpoint:
c5aa993b
JM
5193 if (bs->stop)
5194 {
5195 if (bs->print)
628fe4e4 5196 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5197 else
628fe4e4 5198 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5199 }
5200 else
628fe4e4
JK
5201 {
5202 /* There was a catchpoint, but we're not stopping.
5203 This requires no further action. */
5204 }
5205 break;
628fe4e4
JK
5206 case bp_jit_event:
5207 jit_event = 1;
5208 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5209 break;
c906108c 5210 case bp_call_dummy:
53a5351d
JM
5211 /* Make sure the action is stop (silent or noisy),
5212 so infrun.c pops the dummy frame. */
aa7d318d 5213 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5214 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5215 break;
5216 case bp_std_terminate:
5217 /* Make sure the action is stop (silent or noisy),
5218 so infrun.c pops the dummy frame. */
aa7d318d 5219 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5220 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5221 break;
1042e4c0 5222 case bp_tracepoint:
7a697b8d 5223 case bp_fast_tracepoint:
0fb4aa4b 5224 case bp_static_tracepoint:
1042e4c0
SS
5225 /* Tracepoint hits should not be reported back to GDB, and
5226 if one got through somehow, it should have been filtered
5227 out already. */
5228 internal_error (__FILE__, __LINE__,
7a697b8d 5229 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5230 break;
5231 case bp_gnu_ifunc_resolver:
5232 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5233 this_action = BPSTAT_WHAT_SINGLE;
5234 break;
5235 case bp_gnu_ifunc_resolver_return:
5236 /* The breakpoint will be removed, execution will restart from the
5237 PC of the former breakpoint. */
5238 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5239 break;
e7e0cddf
SS
5240
5241 case bp_dprintf:
5242 this_action = BPSTAT_WHAT_STOP_SILENT;
5243 break;
5244
628fe4e4
JK
5245 default:
5246 internal_error (__FILE__, __LINE__,
5247 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5248 }
628fe4e4
JK
5249
5250 retval.main_action = max (retval.main_action, this_action);
c906108c 5251 }
628fe4e4 5252
0e30163f
JK
5253 /* These operations may affect the bs->breakpoint_at state so they are
5254 delayed after MAIN_ACTION is decided above. */
5255
628fe4e4
JK
5256 if (jit_event)
5257 {
5258 if (debug_infrun)
5259 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5260
5261 handle_jit_event ();
5262 }
5263
0e30163f
JK
5264 for (bs = bs_head; bs != NULL; bs = bs->next)
5265 {
5266 struct breakpoint *b = bs->breakpoint_at;
5267
5268 if (b == NULL)
5269 continue;
5270 switch (b->type)
5271 {
5272 case bp_gnu_ifunc_resolver:
5273 gnu_ifunc_resolver_stop (b);
5274 break;
5275 case bp_gnu_ifunc_resolver_return:
5276 gnu_ifunc_resolver_return_stop (b);
5277 break;
5278 }
5279 }
5280
c906108c
SS
5281 return retval;
5282}
5283
5284/* Nonzero if we should step constantly (e.g. watchpoints on machines
5285 without hardware support). This isn't related to a specific bpstat,
5286 just to things like whether watchpoints are set. */
5287
c5aa993b 5288int
fba45db2 5289bpstat_should_step (void)
c906108c
SS
5290{
5291 struct breakpoint *b;
cc59ec59 5292
c906108c 5293 ALL_BREAKPOINTS (b)
717a8278 5294 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5295 return 1;
c906108c
SS
5296 return 0;
5297}
5298
67822962
PA
5299int
5300bpstat_causes_stop (bpstat bs)
5301{
5302 for (; bs != NULL; bs = bs->next)
5303 if (bs->stop)
5304 return 1;
5305
5306 return 0;
5307}
5308
c906108c 5309\f
c5aa993b 5310
170b53b2
UW
5311/* Compute a string of spaces suitable to indent the next line
5312 so it starts at the position corresponding to the table column
5313 named COL_NAME in the currently active table of UIOUT. */
5314
5315static char *
5316wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5317{
5318 static char wrap_indent[80];
5319 int i, total_width, width, align;
5320 char *text;
5321
5322 total_width = 0;
5323 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5324 {
5325 if (strcmp (text, col_name) == 0)
5326 {
5327 gdb_assert (total_width < sizeof wrap_indent);
5328 memset (wrap_indent, ' ', total_width);
5329 wrap_indent[total_width] = 0;
5330
5331 return wrap_indent;
5332 }
5333
5334 total_width += width + 1;
5335 }
5336
5337 return NULL;
5338}
5339
b775012e
LM
5340/* Determine if the locations of this breakpoint will have their conditions
5341 evaluated by the target, host or a mix of both. Returns the following:
5342
5343 "host": Host evals condition.
5344 "host or target": Host or Target evals condition.
5345 "target": Target evals condition.
5346*/
5347
5348static const char *
5349bp_condition_evaluator (struct breakpoint *b)
5350{
5351 struct bp_location *bl;
5352 char host_evals = 0;
5353 char target_evals = 0;
5354
5355 if (!b)
5356 return NULL;
5357
5358 if (!is_breakpoint (b))
5359 return NULL;
5360
5361 if (gdb_evaluates_breakpoint_condition_p ()
5362 || !target_supports_evaluation_of_breakpoint_conditions ())
5363 return condition_evaluation_host;
5364
5365 for (bl = b->loc; bl; bl = bl->next)
5366 {
5367 if (bl->cond_bytecode)
5368 target_evals++;
5369 else
5370 host_evals++;
5371 }
5372
5373 if (host_evals && target_evals)
5374 return condition_evaluation_both;
5375 else if (target_evals)
5376 return condition_evaluation_target;
5377 else
5378 return condition_evaluation_host;
5379}
5380
5381/* Determine the breakpoint location's condition evaluator. This is
5382 similar to bp_condition_evaluator, but for locations. */
5383
5384static const char *
5385bp_location_condition_evaluator (struct bp_location *bl)
5386{
5387 if (bl && !is_breakpoint (bl->owner))
5388 return NULL;
5389
5390 if (gdb_evaluates_breakpoint_condition_p ()
5391 || !target_supports_evaluation_of_breakpoint_conditions ())
5392 return condition_evaluation_host;
5393
5394 if (bl && bl->cond_bytecode)
5395 return condition_evaluation_target;
5396 else
5397 return condition_evaluation_host;
5398}
5399
859825b8
JK
5400/* Print the LOC location out of the list of B->LOC locations. */
5401
170b53b2
UW
5402static void
5403print_breakpoint_location (struct breakpoint *b,
5404 struct bp_location *loc)
0d381245 5405{
79a45e25 5406 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5407 struct cleanup *old_chain = save_current_program_space ();
5408
859825b8
JK
5409 if (loc != NULL && loc->shlib_disabled)
5410 loc = NULL;
5411
6c95b8df
PA
5412 if (loc != NULL)
5413 set_current_program_space (loc->pspace);
5414
56435ebe
TT
5415 if (b->display_canonical)
5416 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 5417 else if (loc && loc->source_file)
0d381245
VP
5418 {
5419 struct symbol *sym
5420 = find_pc_sect_function (loc->address, loc->section);
5421 if (sym)
5422 {
5423 ui_out_text (uiout, "in ");
5424 ui_out_field_string (uiout, "func",
5425 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5426 ui_out_text (uiout, " ");
5427 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5428 ui_out_text (uiout, "at ");
0d381245 5429 }
f8eba3c6 5430 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
5431 ui_out_text (uiout, ":");
5432
5433 if (ui_out_is_mi_like_p (uiout))
5434 {
5435 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5436 char *fullname = symtab_to_fullname (sal.symtab);
5437
5438 if (fullname)
5439 ui_out_field_string (uiout, "fullname", fullname);
5440 }
5441
f8eba3c6 5442 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5443 }
859825b8 5444 else if (loc)
0d381245 5445 {
f99d8bf4
PA
5446 struct ui_file *stb = mem_fileopen ();
5447 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5448
f99d8bf4 5449 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5450 demangle, "");
0d381245 5451 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5452
5453 do_cleanups (stb_chain);
0d381245 5454 }
859825b8
JK
5455 else
5456 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5457
b775012e
LM
5458 if (loc && is_breakpoint (b)
5459 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5460 && bp_condition_evaluator (b) == condition_evaluation_both)
5461 {
5462 ui_out_text (uiout, " (");
5463 ui_out_field_string (uiout, "evaluated-by",
5464 bp_location_condition_evaluator (loc));
5465 ui_out_text (uiout, ")");
5466 }
5467
6c95b8df 5468 do_cleanups (old_chain);
0d381245
VP
5469}
5470
269b11a2
PA
5471static const char *
5472bptype_string (enum bptype type)
c906108c 5473{
c4093a6a
JM
5474 struct ep_type_description
5475 {
5476 enum bptype type;
5477 char *description;
5478 };
5479 static struct ep_type_description bptypes[] =
c906108c 5480 {
c5aa993b
JM
5481 {bp_none, "?deleted?"},
5482 {bp_breakpoint, "breakpoint"},
c906108c 5483 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5484 {bp_until, "until"},
5485 {bp_finish, "finish"},
5486 {bp_watchpoint, "watchpoint"},
c906108c 5487 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5488 {bp_read_watchpoint, "read watchpoint"},
5489 {bp_access_watchpoint, "acc watchpoint"},
5490 {bp_longjmp, "longjmp"},
5491 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
5492 {bp_exception, "exception"},
5493 {bp_exception_resume, "exception resume"},
c5aa993b 5494 {bp_step_resume, "step resume"},
2c03e5be 5495 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5496 {bp_watchpoint_scope, "watchpoint scope"},
5497 {bp_call_dummy, "call dummy"},
aa7d318d 5498 {bp_std_terminate, "std::terminate"},
c5aa993b 5499 {bp_shlib_event, "shlib events"},
c4093a6a 5500 {bp_thread_event, "thread events"},
1900040c 5501 {bp_overlay_event, "overlay events"},
0fd8e87f 5502 {bp_longjmp_master, "longjmp master"},
aa7d318d 5503 {bp_std_terminate_master, "std::terminate master"},
186c406b 5504 {bp_exception_master, "exception master"},
ce78b96d 5505 {bp_catchpoint, "catchpoint"},
1042e4c0 5506 {bp_tracepoint, "tracepoint"},
7a697b8d 5507 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5508 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5509 {bp_dprintf, "dprintf"},
4efc6507 5510 {bp_jit_event, "jit events"},
0e30163f
JK
5511 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5512 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5513 };
269b11a2
PA
5514
5515 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5516 || ((int) type != bptypes[(int) type].type))
5517 internal_error (__FILE__, __LINE__,
5518 _("bptypes table does not describe type #%d."),
5519 (int) type);
5520
5521 return bptypes[(int) type].description;
5522}
5523
5524/* Print B to gdb_stdout. */
5525
5526static void
5527print_one_breakpoint_location (struct breakpoint *b,
5528 struct bp_location *loc,
5529 int loc_number,
5530 struct bp_location **last_loc,
269b11a2
PA
5531 int allflag)
5532{
5533 struct command_line *l;
c2c6d25f 5534 static char bpenables[] = "nynny";
c906108c 5535
79a45e25 5536 struct ui_out *uiout = current_uiout;
0d381245
VP
5537 int header_of_multiple = 0;
5538 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5539 struct value_print_options opts;
5540
5541 get_user_print_options (&opts);
0d381245
VP
5542
5543 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5544 /* See comment in print_one_breakpoint concerning treatment of
5545 breakpoints with single disabled location. */
0d381245
VP
5546 if (loc == NULL
5547 && (b->loc != NULL
5548 && (b->loc->next != NULL || !b->loc->enabled)))
5549 header_of_multiple = 1;
5550 if (loc == NULL)
5551 loc = b->loc;
5552
c4093a6a
JM
5553 annotate_record ();
5554
5555 /* 1 */
5556 annotate_field (0);
0d381245
VP
5557 if (part_of_multiple)
5558 {
5559 char *formatted;
0c6773c1 5560 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5561 ui_out_field_string (uiout, "number", formatted);
5562 xfree (formatted);
5563 }
5564 else
5565 {
5566 ui_out_field_int (uiout, "number", b->number);
5567 }
c4093a6a
JM
5568
5569 /* 2 */
5570 annotate_field (1);
0d381245
VP
5571 if (part_of_multiple)
5572 ui_out_field_skip (uiout, "type");
269b11a2
PA
5573 else
5574 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5575
5576 /* 3 */
5577 annotate_field (2);
0d381245
VP
5578 if (part_of_multiple)
5579 ui_out_field_skip (uiout, "disp");
5580 else
2cec12e5 5581 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5582
c4093a6a
JM
5583
5584 /* 4 */
5585 annotate_field (3);
0d381245 5586 if (part_of_multiple)
54e52265 5587 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5588 else
4a64f543
MS
5589 ui_out_field_fmt (uiout, "enabled", "%c",
5590 bpenables[(int) b->enable_state]);
54e52265 5591 ui_out_spaces (uiout, 2);
0d381245 5592
c4093a6a
JM
5593
5594 /* 5 and 6 */
3086aeae 5595 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5596 {
4a64f543
MS
5597 /* Although the print_one can possibly print all locations,
5598 calling it here is not likely to get any nice result. So,
5599 make sure there's just one location. */
0d381245 5600 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5601 b->ops->print_one (b, last_loc);
0d381245 5602 }
3086aeae
DJ
5603 else
5604 switch (b->type)
5605 {
5606 case bp_none:
5607 internal_error (__FILE__, __LINE__,
e2e0b3e5 5608 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5609 break;
c906108c 5610
3086aeae
DJ
5611 case bp_watchpoint:
5612 case bp_hardware_watchpoint:
5613 case bp_read_watchpoint:
5614 case bp_access_watchpoint:
3a5c3e22
PA
5615 {
5616 struct watchpoint *w = (struct watchpoint *) b;
5617
5618 /* Field 4, the address, is omitted (which makes the columns
5619 not line up too nicely with the headers, but the effect
5620 is relatively readable). */
5621 if (opts.addressprint)
5622 ui_out_field_skip (uiout, "addr");
5623 annotate_field (5);
5624 ui_out_field_string (uiout, "what", w->exp_string);
5625 }
3086aeae
DJ
5626 break;
5627
3086aeae
DJ
5628 case bp_breakpoint:
5629 case bp_hardware_breakpoint:
5630 case bp_until:
5631 case bp_finish:
5632 case bp_longjmp:
5633 case bp_longjmp_resume:
186c406b
TT
5634 case bp_exception:
5635 case bp_exception_resume:
3086aeae 5636 case bp_step_resume:
2c03e5be 5637 case bp_hp_step_resume:
3086aeae
DJ
5638 case bp_watchpoint_scope:
5639 case bp_call_dummy:
aa7d318d 5640 case bp_std_terminate:
3086aeae
DJ
5641 case bp_shlib_event:
5642 case bp_thread_event:
5643 case bp_overlay_event:
0fd8e87f 5644 case bp_longjmp_master:
aa7d318d 5645 case bp_std_terminate_master:
186c406b 5646 case bp_exception_master:
1042e4c0 5647 case bp_tracepoint:
7a697b8d 5648 case bp_fast_tracepoint:
0fb4aa4b 5649 case bp_static_tracepoint:
e7e0cddf 5650 case bp_dprintf:
4efc6507 5651 case bp_jit_event:
0e30163f
JK
5652 case bp_gnu_ifunc_resolver:
5653 case bp_gnu_ifunc_resolver_return:
79a45b7d 5654 if (opts.addressprint)
3086aeae
DJ
5655 {
5656 annotate_field (4);
54e52265 5657 if (header_of_multiple)
0d381245 5658 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 5659 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 5660 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 5661 else
5af949e3
UW
5662 ui_out_field_core_addr (uiout, "addr",
5663 loc->gdbarch, loc->address);
3086aeae
DJ
5664 }
5665 annotate_field (5);
0d381245 5666 if (!header_of_multiple)
170b53b2 5667 print_breakpoint_location (b, loc);
0d381245 5668 if (b->loc)
a6d9a66e 5669 *last_loc = b->loc;
3086aeae
DJ
5670 break;
5671 }
c906108c 5672
6c95b8df
PA
5673
5674 /* For backward compatibility, don't display inferiors unless there
5675 are several. */
5676 if (loc != NULL
5677 && !header_of_multiple
5678 && (allflag
5679 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5680 && (number_of_program_spaces () > 1
5681 || number_of_inferiors () > 1)
4a64f543
MS
5682 /* LOC is for existing B, it cannot be in
5683 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
5684 && loc->owner->type != bp_catchpoint)))
5685 {
5686 struct inferior *inf;
5687 int first = 1;
5688
5689 for (inf = inferior_list; inf != NULL; inf = inf->next)
5690 {
5691 if (inf->pspace == loc->pspace)
5692 {
5693 if (first)
5694 {
5695 first = 0;
5696 ui_out_text (uiout, " inf ");
5697 }
5698 else
5699 ui_out_text (uiout, ", ");
5700 ui_out_text (uiout, plongest (inf->num));
5701 }
5702 }
5703 }
5704
4a306c9a 5705 if (!part_of_multiple)
c4093a6a 5706 {
4a306c9a
JB
5707 if (b->thread != -1)
5708 {
5709 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5710 "stop only in" line a little further down. */
4a306c9a
JB
5711 ui_out_text (uiout, " thread ");
5712 ui_out_field_int (uiout, "thread", b->thread);
5713 }
5714 else if (b->task != 0)
5715 {
5716 ui_out_text (uiout, " task ");
5717 ui_out_field_int (uiout, "task", b->task);
5718 }
c4093a6a 5719 }
f1310107 5720
8b93c638 5721 ui_out_text (uiout, "\n");
f1310107 5722
348d480f 5723 if (!part_of_multiple)
f1310107
TJB
5724 b->ops->print_one_detail (b, uiout);
5725
0d381245 5726 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5727 {
5728 annotate_field (6);
8b93c638 5729 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 5730 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 5731 the frame ID. */
5af949e3
UW
5732 ui_out_field_core_addr (uiout, "frame",
5733 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5734 ui_out_text (uiout, "\n");
c4093a6a
JM
5735 }
5736
28010a5d 5737 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5738 {
5739 annotate_field (7);
d77f58be 5740 if (is_tracepoint (b))
1042e4c0
SS
5741 ui_out_text (uiout, "\ttrace only if ");
5742 else
5743 ui_out_text (uiout, "\tstop only if ");
0101ce28 5744 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
5745
5746 /* Print whether the target is doing the breakpoint's condition
5747 evaluation. If GDB is doing the evaluation, don't print anything. */
5748 if (is_breakpoint (b)
5749 && breakpoint_condition_evaluation_mode ()
5750 == condition_evaluation_target)
5751 {
5752 ui_out_text (uiout, " (");
5753 ui_out_field_string (uiout, "evaluated-by",
5754 bp_condition_evaluator (b));
5755 ui_out_text (uiout, " evals)");
5756 }
0101ce28
JJ
5757 ui_out_text (uiout, "\n");
5758 }
5759
0d381245 5760 if (!part_of_multiple && b->thread != -1)
c4093a6a 5761 {
4a64f543 5762 /* FIXME should make an annotation for this. */
8b93c638
JM
5763 ui_out_text (uiout, "\tstop only in thread ");
5764 ui_out_field_int (uiout, "thread", b->thread);
5765 ui_out_text (uiout, "\n");
c4093a6a
JM
5766 }
5767
63c715c6 5768 if (!part_of_multiple && b->hit_count)
c4093a6a 5769 {
4a64f543 5770 /* FIXME should make an annotation for this. */
c326b90e 5771 if (is_catchpoint (b))
8b93c638 5772 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5773 else if (is_tracepoint (b))
5774 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5775 else
5776 ui_out_text (uiout, "\tbreakpoint");
5777 ui_out_text (uiout, " already hit ");
5778 ui_out_field_int (uiout, "times", b->hit_count);
5779 if (b->hit_count == 1)
5780 ui_out_text (uiout, " time\n");
5781 else
5782 ui_out_text (uiout, " times\n");
c4093a6a
JM
5783 }
5784
4a64f543
MS
5785 /* Output the count also if it is zero, but only if this is mi.
5786 FIXME: Should have a better test for this. */
9dc5e2a9 5787 if (ui_out_is_mi_like_p (uiout))
63c715c6 5788 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5789 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5790
0d381245 5791 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5792 {
5793 annotate_field (8);
8b93c638
JM
5794 ui_out_text (uiout, "\tignore next ");
5795 ui_out_field_int (uiout, "ignore", b->ignore_count);
5796 ui_out_text (uiout, " hits\n");
c4093a6a 5797 }
059fb39f 5798
816338b5
SS
5799 /* Note that an enable count of 1 corresponds to "enable once"
5800 behavior, which is reported by the combination of enablement and
5801 disposition, so we don't need to mention it here. */
5802 if (!part_of_multiple && b->enable_count > 1)
5803 {
5804 annotate_field (8);
5805 ui_out_text (uiout, "\tdisable after ");
5806 /* Tweak the wording to clarify that ignore and enable counts
5807 are distinct, and have additive effect. */
5808 if (b->ignore_count)
5809 ui_out_text (uiout, "additional ");
5810 else
5811 ui_out_text (uiout, "next ");
5812 ui_out_field_int (uiout, "enable", b->enable_count);
5813 ui_out_text (uiout, " hits\n");
5814 }
5815
f196051f
SS
5816 if (!part_of_multiple && is_tracepoint (b))
5817 {
5818 struct tracepoint *tp = (struct tracepoint *) b;
5819
5820 if (tp->traceframe_usage)
5821 {
5822 ui_out_text (uiout, "\ttrace buffer usage ");
5823 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5824 ui_out_text (uiout, " bytes\n");
5825 }
5826 }
5827
9add0f1b 5828 l = b->commands ? b->commands->commands : NULL;
059fb39f 5829 if (!part_of_multiple && l)
c4093a6a 5830 {
3b31d625
EZ
5831 struct cleanup *script_chain;
5832
c4093a6a 5833 annotate_field (9);
3b31d625 5834 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5835 print_command_lines (uiout, l, 4);
3b31d625 5836 do_cleanups (script_chain);
c4093a6a 5837 }
d24317b4 5838
d9b3f62e 5839 if (is_tracepoint (b))
1042e4c0 5840 {
d9b3f62e
PA
5841 struct tracepoint *t = (struct tracepoint *) b;
5842
5843 if (!part_of_multiple && t->pass_count)
5844 {
5845 annotate_field (10);
5846 ui_out_text (uiout, "\tpass count ");
5847 ui_out_field_int (uiout, "pass", t->pass_count);
5848 ui_out_text (uiout, " \n");
5849 }
1042e4c0
SS
5850 }
5851
d24317b4
VP
5852 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5853 {
3a5c3e22
PA
5854 if (is_watchpoint (b))
5855 {
5856 struct watchpoint *w = (struct watchpoint *) b;
5857
5858 ui_out_field_string (uiout, "original-location", w->exp_string);
5859 }
5860 else if (b->addr_string)
d24317b4 5861 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5862 }
c4093a6a 5863}
c5aa993b 5864
0d381245
VP
5865static void
5866print_one_breakpoint (struct breakpoint *b,
4a64f543 5867 struct bp_location **last_loc,
6c95b8df 5868 int allflag)
0d381245 5869{
8d3788bd 5870 struct cleanup *bkpt_chain;
79a45e25 5871 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5872
5873 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5874
12c5a436 5875 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5876 do_cleanups (bkpt_chain);
0d381245
VP
5877
5878 /* If this breakpoint has custom print function,
5879 it's already printed. Otherwise, print individual
5880 locations, if any. */
5881 if (b->ops == NULL || b->ops->print_one == NULL)
5882 {
4a64f543
MS
5883 /* If breakpoint has a single location that is disabled, we
5884 print it as if it had several locations, since otherwise it's
5885 hard to represent "breakpoint enabled, location disabled"
5886 situation.
5887
5888 Note that while hardware watchpoints have several locations
a3be7890 5889 internally, that's not a property exposed to user. */
0d381245 5890 if (b->loc
a5606eee 5891 && !is_hardware_watchpoint (b)
8d3788bd 5892 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5893 {
5894 struct bp_location *loc;
5895 int n = 1;
8d3788bd 5896
0d381245 5897 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5898 {
5899 struct cleanup *inner2 =
5900 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5901 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5902 do_cleanups (inner2);
5903 }
0d381245
VP
5904 }
5905 }
5906}
5907
a6d9a66e
UW
5908static int
5909breakpoint_address_bits (struct breakpoint *b)
5910{
5911 int print_address_bits = 0;
5912 struct bp_location *loc;
5913
5914 for (loc = b->loc; loc; loc = loc->next)
5915 {
c7437ca6
PA
5916 int addr_bit;
5917
5918 /* Software watchpoints that aren't watching memory don't have
5919 an address to print. */
5920 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5921 continue;
5922
5923 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5924 if (addr_bit > print_address_bits)
5925 print_address_bits = addr_bit;
5926 }
5927
5928 return print_address_bits;
5929}
0d381245 5930
c4093a6a
JM
5931struct captured_breakpoint_query_args
5932 {
5933 int bnum;
5934 };
c5aa993b 5935
c4093a6a 5936static int
2b65245e 5937do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5938{
5939 struct captured_breakpoint_query_args *args = data;
52f0bd74 5940 struct breakpoint *b;
a6d9a66e 5941 struct bp_location *dummy_loc = NULL;
cc59ec59 5942
c4093a6a
JM
5943 ALL_BREAKPOINTS (b)
5944 {
5945 if (args->bnum == b->number)
c5aa993b 5946 {
12c5a436 5947 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5948 return GDB_RC_OK;
c5aa993b 5949 }
c4093a6a
JM
5950 }
5951 return GDB_RC_NONE;
5952}
c5aa993b 5953
c4093a6a 5954enum gdb_rc
4a64f543
MS
5955gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5956 char **error_message)
c4093a6a
JM
5957{
5958 struct captured_breakpoint_query_args args;
cc59ec59 5959
c4093a6a
JM
5960 args.bnum = bnum;
5961 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5962 an error. */
b0b13bb4
DJ
5963 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5964 error_message, RETURN_MASK_ALL) < 0)
5965 return GDB_RC_FAIL;
5966 else
5967 return GDB_RC_OK;
c4093a6a 5968}
c5aa993b 5969
09d682a4
TT
5970/* Return true if this breakpoint was set by the user, false if it is
5971 internal or momentary. */
5972
5973int
5974user_breakpoint_p (struct breakpoint *b)
5975{
46c6471b 5976 return b->number > 0;
09d682a4
TT
5977}
5978
7f3b0473 5979/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5980 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5981 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5982 FILTER is non-NULL, call it on each breakpoint and only include the
5983 ones for which it returns non-zero. Return the total number of
5984 breakpoints listed. */
c906108c 5985
d77f58be 5986static int
e5a67952 5987breakpoint_1 (char *args, int allflag,
4a64f543 5988 int (*filter) (const struct breakpoint *))
c4093a6a 5989{
52f0bd74 5990 struct breakpoint *b;
a6d9a66e 5991 struct bp_location *last_loc = NULL;
7f3b0473 5992 int nr_printable_breakpoints;
3b31d625 5993 struct cleanup *bkpttbl_chain;
79a45b7d 5994 struct value_print_options opts;
a6d9a66e 5995 int print_address_bits = 0;
269b11a2 5996 int print_type_col_width = 14;
79a45e25 5997 struct ui_out *uiout = current_uiout;
269b11a2 5998
79a45b7d
TT
5999 get_user_print_options (&opts);
6000
4a64f543
MS
6001 /* Compute the number of rows in the table, as well as the size
6002 required for address fields. */
7f3b0473
AC
6003 nr_printable_breakpoints = 0;
6004 ALL_BREAKPOINTS (b)
e5a67952
MS
6005 {
6006 /* If we have a filter, only list the breakpoints it accepts. */
6007 if (filter && !filter (b))
6008 continue;
6009
6010 /* If we have an "args" string, it is a list of breakpoints to
6011 accept. Skip the others. */
6012 if (args != NULL && *args != '\0')
6013 {
6014 if (allflag && parse_and_eval_long (args) != b->number)
6015 continue;
6016 if (!allflag && !number_is_in_list (args, b->number))
6017 continue;
6018 }
269b11a2 6019
e5a67952
MS
6020 if (allflag || user_breakpoint_p (b))
6021 {
6022 int addr_bit, type_len;
a6d9a66e 6023
e5a67952
MS
6024 addr_bit = breakpoint_address_bits (b);
6025 if (addr_bit > print_address_bits)
6026 print_address_bits = addr_bit;
269b11a2 6027
e5a67952
MS
6028 type_len = strlen (bptype_string (b->type));
6029 if (type_len > print_type_col_width)
6030 print_type_col_width = type_len;
6031
6032 nr_printable_breakpoints++;
6033 }
6034 }
7f3b0473 6035
79a45b7d 6036 if (opts.addressprint)
3b31d625 6037 bkpttbl_chain
3e43a32a
MS
6038 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6039 nr_printable_breakpoints,
3b31d625 6040 "BreakpointTable");
8b93c638 6041 else
3b31d625 6042 bkpttbl_chain
3e43a32a
MS
6043 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6044 nr_printable_breakpoints,
3b31d625 6045 "BreakpointTable");
8b93c638 6046
7f3b0473 6047 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6048 annotate_breakpoints_headers ();
6049 if (nr_printable_breakpoints > 0)
6050 annotate_field (0);
4a64f543 6051 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6052 if (nr_printable_breakpoints > 0)
6053 annotate_field (1);
269b11a2 6054 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6055 "type", "Type"); /* 2 */
d7faa9e7
AC
6056 if (nr_printable_breakpoints > 0)
6057 annotate_field (2);
4a64f543 6058 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6059 if (nr_printable_breakpoints > 0)
6060 annotate_field (3);
54e52265 6061 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6062 if (opts.addressprint)
e5a67952
MS
6063 {
6064 if (nr_printable_breakpoints > 0)
6065 annotate_field (4);
6066 if (print_address_bits <= 32)
6067 ui_out_table_header (uiout, 10, ui_left,
6068 "addr", "Address"); /* 5 */
6069 else
6070 ui_out_table_header (uiout, 18, ui_left,
6071 "addr", "Address"); /* 5 */
6072 }
d7faa9e7
AC
6073 if (nr_printable_breakpoints > 0)
6074 annotate_field (5);
6075 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6076 ui_out_table_body (uiout);
6077 if (nr_printable_breakpoints > 0)
6078 annotate_breakpoints_table ();
7f3b0473 6079
c4093a6a 6080 ALL_BREAKPOINTS (b)
e5a67952
MS
6081 {
6082 QUIT;
6083 /* If we have a filter, only list the breakpoints it accepts. */
6084 if (filter && !filter (b))
6085 continue;
6086
6087 /* If we have an "args" string, it is a list of breakpoints to
6088 accept. Skip the others. */
6089
6090 if (args != NULL && *args != '\0')
6091 {
6092 if (allflag) /* maintenance info breakpoint */
6093 {
6094 if (parse_and_eval_long (args) != b->number)
6095 continue;
6096 }
6097 else /* all others */
6098 {
6099 if (!number_is_in_list (args, b->number))
6100 continue;
6101 }
6102 }
6103 /* We only print out user settable breakpoints unless the
6104 allflag is set. */
6105 if (allflag || user_breakpoint_p (b))
12c5a436 6106 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6107 }
6108
3b31d625 6109 do_cleanups (bkpttbl_chain);
698384cd 6110
7f3b0473 6111 if (nr_printable_breakpoints == 0)
c906108c 6112 {
4a64f543
MS
6113 /* If there's a filter, let the caller decide how to report
6114 empty list. */
d77f58be
SS
6115 if (!filter)
6116 {
e5a67952 6117 if (args == NULL || *args == '\0')
d77f58be
SS
6118 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6119 else
4a64f543 6120 ui_out_message (uiout, 0,
e5a67952
MS
6121 "No breakpoint or watchpoint matching '%s'.\n",
6122 args);
d77f58be 6123 }
c906108c
SS
6124 }
6125 else
c4093a6a 6126 {
a6d9a66e
UW
6127 if (last_loc && !server_command)
6128 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6129 }
c906108c 6130
4a64f543 6131 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6132 there have been breakpoints? */
c906108c 6133 annotate_breakpoints_table_end ();
d77f58be
SS
6134
6135 return nr_printable_breakpoints;
c906108c
SS
6136}
6137
ad443146
SS
6138/* Display the value of default-collect in a way that is generally
6139 compatible with the breakpoint list. */
6140
6141static void
6142default_collect_info (void)
6143{
79a45e25
PA
6144 struct ui_out *uiout = current_uiout;
6145
ad443146
SS
6146 /* If it has no value (which is frequently the case), say nothing; a
6147 message like "No default-collect." gets in user's face when it's
6148 not wanted. */
6149 if (!*default_collect)
6150 return;
6151
6152 /* The following phrase lines up nicely with per-tracepoint collect
6153 actions. */
6154 ui_out_text (uiout, "default collect ");
6155 ui_out_field_string (uiout, "default-collect", default_collect);
6156 ui_out_text (uiout, " \n");
6157}
6158
c906108c 6159static void
e5a67952 6160breakpoints_info (char *args, int from_tty)
c906108c 6161{
e5a67952 6162 breakpoint_1 (args, 0, NULL);
ad443146
SS
6163
6164 default_collect_info ();
d77f58be
SS
6165}
6166
6167static void
e5a67952 6168watchpoints_info (char *args, int from_tty)
d77f58be 6169{
e5a67952 6170 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6171 struct ui_out *uiout = current_uiout;
d77f58be
SS
6172
6173 if (num_printed == 0)
6174 {
e5a67952 6175 if (args == NULL || *args == '\0')
d77f58be
SS
6176 ui_out_message (uiout, 0, "No watchpoints.\n");
6177 else
e5a67952 6178 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6179 }
c906108c
SS
6180}
6181
7a292a7a 6182static void
e5a67952 6183maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6184{
e5a67952 6185 breakpoint_1 (args, 1, NULL);
ad443146
SS
6186
6187 default_collect_info ();
c906108c
SS
6188}
6189
0d381245 6190static int
714835d5 6191breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6192 struct program_space *pspace,
714835d5 6193 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6194{
6195 struct bp_location *bl = b->loc;
cc59ec59 6196
0d381245
VP
6197 for (; bl; bl = bl->next)
6198 {
6c95b8df
PA
6199 if (bl->pspace == pspace
6200 && bl->address == pc
0d381245
VP
6201 && (!overlay_debugging || bl->section == section))
6202 return 1;
6203 }
6204 return 0;
6205}
6206
672f9b60 6207/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6208 concerns with logical breakpoints, so we match program spaces, not
6209 address spaces. */
c906108c
SS
6210
6211static void
6c95b8df
PA
6212describe_other_breakpoints (struct gdbarch *gdbarch,
6213 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6214 struct obj_section *section, int thread)
c906108c 6215{
52f0bd74
AC
6216 int others = 0;
6217 struct breakpoint *b;
c906108c
SS
6218
6219 ALL_BREAKPOINTS (b)
672f9b60
KP
6220 others += (user_breakpoint_p (b)
6221 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6222 if (others > 0)
6223 {
a3f17187
AC
6224 if (others == 1)
6225 printf_filtered (_("Note: breakpoint "));
6226 else /* if (others == ???) */
6227 printf_filtered (_("Note: breakpoints "));
c906108c 6228 ALL_BREAKPOINTS (b)
672f9b60 6229 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6230 {
6231 others--;
6232 printf_filtered ("%d", b->number);
6233 if (b->thread == -1 && thread != -1)
6234 printf_filtered (" (all threads)");
6235 else if (b->thread != -1)
6236 printf_filtered (" (thread %d)", b->thread);
6237 printf_filtered ("%s%s ",
059fb39f 6238 ((b->enable_state == bp_disabled
f8eba3c6 6239 || b->enable_state == bp_call_disabled)
0d381245
VP
6240 ? " (disabled)"
6241 : b->enable_state == bp_permanent
6242 ? " (permanent)"
6243 : ""),
6244 (others > 1) ? ","
6245 : ((others == 1) ? " and" : ""));
6246 }
a3f17187 6247 printf_filtered (_("also set at pc "));
5af949e3 6248 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6249 printf_filtered (".\n");
6250 }
6251}
6252\f
c906108c 6253
e4f237da
KB
6254/* Return true iff it is meaningful to use the address member of
6255 BPT. For some breakpoint types, the address member is irrelevant
6256 and it makes no sense to attempt to compare it to other addresses
6257 (or use it for any other purpose either).
6258
4a64f543
MS
6259 More specifically, each of the following breakpoint types will
6260 always have a zero valued address and we don't want to mark
6261 breakpoints of any of these types to be a duplicate of an actual
6262 breakpoint at address zero:
e4f237da
KB
6263
6264 bp_watchpoint
2d134ed3
PA
6265 bp_catchpoint
6266
6267*/
e4f237da
KB
6268
6269static int
6270breakpoint_address_is_meaningful (struct breakpoint *bpt)
6271{
6272 enum bptype type = bpt->type;
6273
2d134ed3
PA
6274 return (type != bp_watchpoint && type != bp_catchpoint);
6275}
6276
6277/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6278 true if LOC1 and LOC2 represent the same watchpoint location. */
6279
6280static int
4a64f543
MS
6281watchpoint_locations_match (struct bp_location *loc1,
6282 struct bp_location *loc2)
2d134ed3 6283{
3a5c3e22
PA
6284 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6285 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6286
6287 /* Both of them must exist. */
6288 gdb_assert (w1 != NULL);
6289 gdb_assert (w2 != NULL);
2bdf28a0 6290
4a64f543
MS
6291 /* If the target can evaluate the condition expression in hardware,
6292 then we we need to insert both watchpoints even if they are at
6293 the same place. Otherwise the watchpoint will only trigger when
6294 the condition of whichever watchpoint was inserted evaluates to
6295 true, not giving a chance for GDB to check the condition of the
6296 other watchpoint. */
3a5c3e22 6297 if ((w1->cond_exp
4a64f543
MS
6298 && target_can_accel_watchpoint_condition (loc1->address,
6299 loc1->length,
0cf6dd15 6300 loc1->watchpoint_type,
3a5c3e22
PA
6301 w1->cond_exp))
6302 || (w2->cond_exp
4a64f543
MS
6303 && target_can_accel_watchpoint_condition (loc2->address,
6304 loc2->length,
0cf6dd15 6305 loc2->watchpoint_type,
3a5c3e22 6306 w2->cond_exp)))
0cf6dd15
TJB
6307 return 0;
6308
85d721b8
PA
6309 /* Note that this checks the owner's type, not the location's. In
6310 case the target does not support read watchpoints, but does
6311 support access watchpoints, we'll have bp_read_watchpoint
6312 watchpoints with hw_access locations. Those should be considered
6313 duplicates of hw_read locations. The hw_read locations will
6314 become hw_access locations later. */
2d134ed3
PA
6315 return (loc1->owner->type == loc2->owner->type
6316 && loc1->pspace->aspace == loc2->pspace->aspace
6317 && loc1->address == loc2->address
6318 && loc1->length == loc2->length);
e4f237da
KB
6319}
6320
6c95b8df
PA
6321/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6322 same breakpoint location. In most targets, this can only be true
6323 if ASPACE1 matches ASPACE2. On targets that have global
6324 breakpoints, the address space doesn't really matter. */
6325
6326static int
6327breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6328 struct address_space *aspace2, CORE_ADDR addr2)
6329{
6330 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6331 || aspace1 == aspace2)
6332 && addr1 == addr2);
6333}
6334
f1310107
TJB
6335/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6336 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6337 matches ASPACE2. On targets that have global breakpoints, the address
6338 space doesn't really matter. */
6339
6340static int
6341breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6342 int len1, struct address_space *aspace2,
6343 CORE_ADDR addr2)
6344{
6345 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6346 || aspace1 == aspace2)
6347 && addr2 >= addr1 && addr2 < addr1 + len1);
6348}
6349
6350/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6351 a ranged breakpoint. In most targets, a match happens only if ASPACE
6352 matches the breakpoint's address space. On targets that have global
6353 breakpoints, the address space doesn't really matter. */
6354
6355static int
6356breakpoint_location_address_match (struct bp_location *bl,
6357 struct address_space *aspace,
6358 CORE_ADDR addr)
6359{
6360 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6361 aspace, addr)
6362 || (bl->length
6363 && breakpoint_address_match_range (bl->pspace->aspace,
6364 bl->address, bl->length,
6365 aspace, addr)));
6366}
6367
1e4d1764
YQ
6368/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6369 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6370 true, otherwise returns false. */
6371
6372static int
6373tracepoint_locations_match (struct bp_location *loc1,
6374 struct bp_location *loc2)
6375{
6376 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6377 /* Since tracepoint locations are never duplicated with others', tracepoint
6378 locations at the same address of different tracepoints are regarded as
6379 different locations. */
6380 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6381 else
6382 return 0;
6383}
6384
2d134ed3
PA
6385/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6386 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6387 represent the same location. */
6388
6389static int
4a64f543
MS
6390breakpoint_locations_match (struct bp_location *loc1,
6391 struct bp_location *loc2)
2d134ed3 6392{
2bdf28a0
JK
6393 int hw_point1, hw_point2;
6394
6395 /* Both of them must not be in moribund_locations. */
6396 gdb_assert (loc1->owner != NULL);
6397 gdb_assert (loc2->owner != NULL);
6398
6399 hw_point1 = is_hardware_watchpoint (loc1->owner);
6400 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6401
6402 if (hw_point1 != hw_point2)
6403 return 0;
6404 else if (hw_point1)
6405 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6406 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6407 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6408 else
f1310107
TJB
6409 /* We compare bp_location.length in order to cover ranged breakpoints. */
6410 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6411 loc2->pspace->aspace, loc2->address)
6412 && loc1->length == loc2->length);
2d134ed3
PA
6413}
6414
76897487
KB
6415static void
6416breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6417 int bnum, int have_bnum)
6418{
f63fbe86
MS
6419 /* The longest string possibly returned by hex_string_custom
6420 is 50 chars. These must be at least that big for safety. */
6421 char astr1[64];
6422 char astr2[64];
76897487 6423
bb599908
PH
6424 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6425 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6426 if (have_bnum)
8a3fe4f8 6427 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6428 bnum, astr1, astr2);
6429 else
8a3fe4f8 6430 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6431}
6432
4a64f543
MS
6433/* Adjust a breakpoint's address to account for architectural
6434 constraints on breakpoint placement. Return the adjusted address.
6435 Note: Very few targets require this kind of adjustment. For most
6436 targets, this function is simply the identity function. */
76897487
KB
6437
6438static CORE_ADDR
a6d9a66e
UW
6439adjust_breakpoint_address (struct gdbarch *gdbarch,
6440 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6441{
a6d9a66e 6442 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6443 {
6444 /* Very few targets need any kind of breakpoint adjustment. */
6445 return bpaddr;
6446 }
88f7da05
KB
6447 else if (bptype == bp_watchpoint
6448 || bptype == bp_hardware_watchpoint
6449 || bptype == bp_read_watchpoint
6450 || bptype == bp_access_watchpoint
fe798b75 6451 || bptype == bp_catchpoint)
88f7da05
KB
6452 {
6453 /* Watchpoints and the various bp_catch_* eventpoints should not
6454 have their addresses modified. */
6455 return bpaddr;
6456 }
76897487
KB
6457 else
6458 {
6459 CORE_ADDR adjusted_bpaddr;
6460
6461 /* Some targets have architectural constraints on the placement
6462 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6463 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6464
6465 /* An adjusted breakpoint address can significantly alter
6466 a user's expectations. Print a warning if an adjustment
6467 is required. */
6468 if (adjusted_bpaddr != bpaddr)
6469 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6470
6471 return adjusted_bpaddr;
6472 }
6473}
6474
28010a5d
PA
6475void
6476init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6477 struct breakpoint *owner)
7cc221ef 6478{
7cc221ef
DJ
6479 memset (loc, 0, sizeof (*loc));
6480
348d480f
PA
6481 gdb_assert (ops != NULL);
6482
28010a5d
PA
6483 loc->ops = ops;
6484 loc->owner = owner;
511a6cd4 6485 loc->cond = NULL;
b775012e 6486 loc->cond_bytecode = NULL;
0d381245
VP
6487 loc->shlib_disabled = 0;
6488 loc->enabled = 1;
e049a4b5 6489
28010a5d 6490 switch (owner->type)
e049a4b5
DJ
6491 {
6492 case bp_breakpoint:
6493 case bp_until:
6494 case bp_finish:
6495 case bp_longjmp:
6496 case bp_longjmp_resume:
186c406b
TT
6497 case bp_exception:
6498 case bp_exception_resume:
e049a4b5 6499 case bp_step_resume:
2c03e5be 6500 case bp_hp_step_resume:
e049a4b5
DJ
6501 case bp_watchpoint_scope:
6502 case bp_call_dummy:
aa7d318d 6503 case bp_std_terminate:
e049a4b5
DJ
6504 case bp_shlib_event:
6505 case bp_thread_event:
6506 case bp_overlay_event:
4efc6507 6507 case bp_jit_event:
0fd8e87f 6508 case bp_longjmp_master:
aa7d318d 6509 case bp_std_terminate_master:
186c406b 6510 case bp_exception_master:
0e30163f
JK
6511 case bp_gnu_ifunc_resolver:
6512 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6513 case bp_dprintf:
e049a4b5 6514 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6515 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6516 break;
6517 case bp_hardware_breakpoint:
6518 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6519 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6520 break;
6521 case bp_hardware_watchpoint:
6522 case bp_read_watchpoint:
6523 case bp_access_watchpoint:
6524 loc->loc_type = bp_loc_hardware_watchpoint;
6525 break;
6526 case bp_watchpoint:
ce78b96d 6527 case bp_catchpoint:
15c3d785
PA
6528 case bp_tracepoint:
6529 case bp_fast_tracepoint:
0fb4aa4b 6530 case bp_static_tracepoint:
e049a4b5
DJ
6531 loc->loc_type = bp_loc_other;
6532 break;
6533 default:
e2e0b3e5 6534 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6535 }
6536
f431efe5 6537 loc->refc = 1;
28010a5d
PA
6538}
6539
6540/* Allocate a struct bp_location. */
6541
6542static struct bp_location *
6543allocate_bp_location (struct breakpoint *bpt)
6544{
348d480f
PA
6545 return bpt->ops->allocate_location (bpt);
6546}
7cc221ef 6547
f431efe5
PA
6548static void
6549free_bp_location (struct bp_location *loc)
fe3f5fa8 6550{
348d480f 6551 loc->ops->dtor (loc);
fe3f5fa8
VP
6552 xfree (loc);
6553}
6554
f431efe5
PA
6555/* Increment reference count. */
6556
6557static void
6558incref_bp_location (struct bp_location *bl)
6559{
6560 ++bl->refc;
6561}
6562
6563/* Decrement reference count. If the reference count reaches 0,
6564 destroy the bp_location. Sets *BLP to NULL. */
6565
6566static void
6567decref_bp_location (struct bp_location **blp)
6568{
0807b50c
PA
6569 gdb_assert ((*blp)->refc > 0);
6570
f431efe5
PA
6571 if (--(*blp)->refc == 0)
6572 free_bp_location (*blp);
6573 *blp = NULL;
6574}
6575
346774a9 6576/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6577
346774a9
PA
6578static void
6579add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6580{
346774a9 6581 struct breakpoint *b1;
c906108c 6582
346774a9
PA
6583 /* Add this breakpoint to the end of the chain so that a list of
6584 breakpoints will come out in order of increasing numbers. */
6585
6586 b1 = breakpoint_chain;
6587 if (b1 == 0)
6588 breakpoint_chain = b;
6589 else
6590 {
6591 while (b1->next)
6592 b1 = b1->next;
6593 b1->next = b;
6594 }
6595}
6596
6597/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6598
6599static void
6600init_raw_breakpoint_without_location (struct breakpoint *b,
6601 struct gdbarch *gdbarch,
28010a5d 6602 enum bptype bptype,
c0a91b2b 6603 const struct breakpoint_ops *ops)
346774a9 6604{
c906108c 6605 memset (b, 0, sizeof (*b));
2219d63c 6606
348d480f
PA
6607 gdb_assert (ops != NULL);
6608
28010a5d 6609 b->ops = ops;
4d28f7a8 6610 b->type = bptype;
a6d9a66e 6611 b->gdbarch = gdbarch;
c906108c
SS
6612 b->language = current_language->la_language;
6613 b->input_radix = input_radix;
6614 b->thread = -1;
b5de0fa7 6615 b->enable_state = bp_enabled;
c906108c
SS
6616 b->next = 0;
6617 b->silent = 0;
6618 b->ignore_count = 0;
6619 b->commands = NULL;
818dd999 6620 b->frame_id = null_frame_id;
0d381245 6621 b->condition_not_parsed = 0;
84f4c1fe 6622 b->py_bp_object = NULL;
d0fb5eae 6623 b->related_breakpoint = b;
346774a9
PA
6624}
6625
6626/* Helper to set_raw_breakpoint below. Creates a breakpoint
6627 that has type BPTYPE and has no locations as yet. */
346774a9
PA
6628
6629static struct breakpoint *
6630set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 6631 enum bptype bptype,
c0a91b2b 6632 const struct breakpoint_ops *ops)
346774a9
PA
6633{
6634 struct breakpoint *b = XNEW (struct breakpoint);
6635
348d480f 6636 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 6637 add_to_breakpoint_chain (b);
0d381245
VP
6638 return b;
6639}
6640
0e30163f
JK
6641/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6642 resolutions should be made as the user specified the location explicitly
6643 enough. */
6644
0d381245 6645static void
0e30163f 6646set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 6647{
2bdf28a0
JK
6648 gdb_assert (loc->owner != NULL);
6649
0d381245 6650 if (loc->owner->type == bp_breakpoint
1042e4c0 6651 || loc->owner->type == bp_hardware_breakpoint
d77f58be 6652 || is_tracepoint (loc->owner))
0d381245 6653 {
0e30163f 6654 int is_gnu_ifunc;
2c02bd72 6655 const char *function_name;
6a3a010b 6656 CORE_ADDR func_addr;
0e30163f 6657
2c02bd72 6658 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 6659 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
6660
6661 if (is_gnu_ifunc && !explicit_loc)
6662 {
6663 struct breakpoint *b = loc->owner;
6664
6665 gdb_assert (loc->pspace == current_program_space);
2c02bd72 6666 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
6667 &loc->requested_address))
6668 {
6669 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6670 loc->address = adjust_breakpoint_address (loc->gdbarch,
6671 loc->requested_address,
6672 b->type);
6673 }
6674 else if (b->type == bp_breakpoint && b->loc == loc
6675 && loc->next == NULL && b->related_breakpoint == b)
6676 {
6677 /* Create only the whole new breakpoint of this type but do not
6678 mess more complicated breakpoints with multiple locations. */
6679 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
6680 /* Remember the resolver's address for use by the return
6681 breakpoint. */
6682 loc->related_address = func_addr;
0e30163f
JK
6683 }
6684 }
6685
2c02bd72
DE
6686 if (function_name)
6687 loc->function_name = xstrdup (function_name);
0d381245
VP
6688 }
6689}
6690
a6d9a66e 6691/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 6692struct gdbarch *
a6d9a66e
UW
6693get_sal_arch (struct symtab_and_line sal)
6694{
6695 if (sal.section)
6696 return get_objfile_arch (sal.section->objfile);
6697 if (sal.symtab)
6698 return get_objfile_arch (sal.symtab->objfile);
6699
6700 return NULL;
6701}
6702
346774a9
PA
6703/* Low level routine for partially initializing a breakpoint of type
6704 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 6705 file name, and line number are provided by SAL.
0d381245
VP
6706
6707 It is expected that the caller will complete the initialization of
6708 the newly created breakpoint struct as well as output any status
c56053d2 6709 information regarding the creation of a new breakpoint. */
0d381245 6710
346774a9
PA
6711static void
6712init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 6713 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6714 const struct breakpoint_ops *ops)
0d381245 6715{
28010a5d 6716 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 6717
3742cc8b 6718 add_location_to_breakpoint (b, &sal);
0d381245 6719
6c95b8df
PA
6720 if (bptype != bp_catchpoint)
6721 gdb_assert (sal.pspace != NULL);
6722
f8eba3c6
TT
6723 /* Store the program space that was used to set the breakpoint,
6724 except for ordinary breakpoints, which are independent of the
6725 program space. */
6726 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6727 b->pspace = sal.pspace;
0d381245 6728
c906108c 6729 breakpoints_changed ();
346774a9 6730}
c906108c 6731
346774a9
PA
6732/* set_raw_breakpoint is a low level routine for allocating and
6733 partially initializing a breakpoint of type BPTYPE. The newly
6734 created breakpoint's address, section, source file name, and line
6735 number are provided by SAL. The newly created and partially
6736 initialized breakpoint is added to the breakpoint chain and
6737 is also returned as the value of this function.
6738
6739 It is expected that the caller will complete the initialization of
6740 the newly created breakpoint struct as well as output any status
6741 information regarding the creation of a new breakpoint. In
6742 particular, set_raw_breakpoint does NOT set the breakpoint
6743 number! Care should be taken to not allow an error to occur
6744 prior to completing the initialization of the breakpoint. If this
6745 should happen, a bogus breakpoint will be left on the chain. */
6746
6747struct breakpoint *
6748set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 6749 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6750 const struct breakpoint_ops *ops)
346774a9
PA
6751{
6752 struct breakpoint *b = XNEW (struct breakpoint);
6753
348d480f 6754 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 6755 add_to_breakpoint_chain (b);
c906108c
SS
6756 return b;
6757}
6758
c2c6d25f
JM
6759
6760/* Note that the breakpoint object B describes a permanent breakpoint
6761 instruction, hard-wired into the inferior's code. */
6762void
6763make_breakpoint_permanent (struct breakpoint *b)
6764{
0d381245 6765 struct bp_location *bl;
cc59ec59 6766
b5de0fa7 6767 b->enable_state = bp_permanent;
c2c6d25f 6768
4a64f543
MS
6769 /* By definition, permanent breakpoints are already present in the
6770 code. Mark all locations as inserted. For now,
6771 make_breakpoint_permanent is called in just one place, so it's
6772 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6773 multiple locations or not, but it's easy to implement. */
0d381245
VP
6774 for (bl = b->loc; bl; bl = bl->next)
6775 bl->inserted = 1;
c2c6d25f
JM
6776}
6777
53a5351d 6778/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6779 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6780 initiated the operation. */
c906108c
SS
6781
6782void
186c406b 6783set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6784{
35df4500 6785 struct breakpoint *b, *b_tmp;
186c406b 6786 int thread = tp->num;
0fd8e87f
UW
6787
6788 /* To avoid having to rescan all objfile symbols at every step,
6789 we maintain a list of continually-inserted but always disabled
6790 longjmp "master" breakpoints. Here, we simply create momentary
6791 clones of those and enable them for the requested thread. */
35df4500 6792 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6793 if (b->pspace == current_program_space
186c406b
TT
6794 && (b->type == bp_longjmp_master
6795 || b->type == bp_exception_master))
0fd8e87f 6796 {
06edf0c0
PA
6797 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6798 struct breakpoint *clone;
cc59ec59 6799
06edf0c0
PA
6800 clone = momentary_breakpoint_from_master (b, type,
6801 &momentary_breakpoint_ops);
0fd8e87f
UW
6802 clone->thread = thread;
6803 }
186c406b
TT
6804
6805 tp->initiating_frame = frame;
c906108c
SS
6806}
6807
611c83ae 6808/* Delete all longjmp breakpoints from THREAD. */
c906108c 6809void
611c83ae 6810delete_longjmp_breakpoint (int thread)
c906108c 6811{
35df4500 6812 struct breakpoint *b, *b_tmp;
c906108c 6813
35df4500 6814 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6815 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6816 {
6817 if (b->thread == thread)
6818 delete_breakpoint (b);
6819 }
c906108c
SS
6820}
6821
f59f708a
PA
6822void
6823delete_longjmp_breakpoint_at_next_stop (int thread)
6824{
6825 struct breakpoint *b, *b_tmp;
6826
6827 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6828 if (b->type == bp_longjmp || b->type == bp_exception)
6829 {
6830 if (b->thread == thread)
6831 b->disposition = disp_del_at_next_stop;
6832 }
6833}
6834
1900040c
MS
6835void
6836enable_overlay_breakpoints (void)
6837{
52f0bd74 6838 struct breakpoint *b;
1900040c
MS
6839
6840 ALL_BREAKPOINTS (b)
6841 if (b->type == bp_overlay_event)
6842 {
6843 b->enable_state = bp_enabled;
b60e7edf 6844 update_global_location_list (1);
c02f5703 6845 overlay_events_enabled = 1;
1900040c
MS
6846 }
6847}
6848
6849void
6850disable_overlay_breakpoints (void)
6851{
52f0bd74 6852 struct breakpoint *b;
1900040c
MS
6853
6854 ALL_BREAKPOINTS (b)
6855 if (b->type == bp_overlay_event)
6856 {
6857 b->enable_state = bp_disabled;
b60e7edf 6858 update_global_location_list (0);
c02f5703 6859 overlay_events_enabled = 0;
1900040c
MS
6860 }
6861}
6862
aa7d318d
TT
6863/* Set an active std::terminate breakpoint for each std::terminate
6864 master breakpoint. */
6865void
6866set_std_terminate_breakpoint (void)
6867{
35df4500 6868 struct breakpoint *b, *b_tmp;
aa7d318d 6869
35df4500 6870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6871 if (b->pspace == current_program_space
6872 && b->type == bp_std_terminate_master)
6873 {
06edf0c0
PA
6874 momentary_breakpoint_from_master (b, bp_std_terminate,
6875 &momentary_breakpoint_ops);
aa7d318d
TT
6876 }
6877}
6878
6879/* Delete all the std::terminate breakpoints. */
6880void
6881delete_std_terminate_breakpoint (void)
6882{
35df4500 6883 struct breakpoint *b, *b_tmp;
aa7d318d 6884
35df4500 6885 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6886 if (b->type == bp_std_terminate)
6887 delete_breakpoint (b);
6888}
6889
c4093a6a 6890struct breakpoint *
a6d9a66e 6891create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6892{
6893 struct breakpoint *b;
c4093a6a 6894
06edf0c0
PA
6895 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6896 &internal_breakpoint_ops);
6897
b5de0fa7 6898 b->enable_state = bp_enabled;
c4093a6a 6899 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6900 b->addr_string
6901 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6902
b60e7edf 6903 update_global_location_list_nothrow (1);
74960c60 6904
c4093a6a
JM
6905 return b;
6906}
6907
6908void
6909remove_thread_event_breakpoints (void)
6910{
35df4500 6911 struct breakpoint *b, *b_tmp;
c4093a6a 6912
35df4500 6913 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6914 if (b->type == bp_thread_event
6915 && b->loc->pspace == current_program_space)
c4093a6a
JM
6916 delete_breakpoint (b);
6917}
6918
0101ce28
JJ
6919struct lang_and_radix
6920 {
6921 enum language lang;
6922 int radix;
6923 };
6924
4efc6507
DE
6925/* Create a breakpoint for JIT code registration and unregistration. */
6926
6927struct breakpoint *
6928create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6929{
6930 struct breakpoint *b;
6931
06edf0c0
PA
6932 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6933 &internal_breakpoint_ops);
4efc6507
DE
6934 update_global_location_list_nothrow (1);
6935 return b;
6936}
0101ce28 6937
03673fc7
PP
6938/* Remove JIT code registration and unregistration breakpoint(s). */
6939
6940void
6941remove_jit_event_breakpoints (void)
6942{
6943 struct breakpoint *b, *b_tmp;
6944
6945 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6946 if (b->type == bp_jit_event
6947 && b->loc->pspace == current_program_space)
6948 delete_breakpoint (b);
6949}
6950
cae688ec
JJ
6951void
6952remove_solib_event_breakpoints (void)
6953{
35df4500 6954 struct breakpoint *b, *b_tmp;
cae688ec 6955
35df4500 6956 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6957 if (b->type == bp_shlib_event
6958 && b->loc->pspace == current_program_space)
cae688ec
JJ
6959 delete_breakpoint (b);
6960}
6961
6962struct breakpoint *
a6d9a66e 6963create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6964{
6965 struct breakpoint *b;
6966
06edf0c0
PA
6967 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6968 &internal_breakpoint_ops);
b60e7edf 6969 update_global_location_list_nothrow (1);
cae688ec
JJ
6970 return b;
6971}
6972
6973/* Disable any breakpoints that are on code in shared libraries. Only
6974 apply to enabled breakpoints, disabled ones can just stay disabled. */
6975
6976void
cb851954 6977disable_breakpoints_in_shlibs (void)
cae688ec 6978{
876fa593 6979 struct bp_location *loc, **locp_tmp;
cae688ec 6980
876fa593 6981 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6982 {
2bdf28a0 6983 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6984 struct breakpoint *b = loc->owner;
2bdf28a0 6985
4a64f543
MS
6986 /* We apply the check to all breakpoints, including disabled for
6987 those with loc->duplicate set. This is so that when breakpoint
6988 becomes enabled, or the duplicate is removed, gdb will try to
6989 insert all breakpoints. If we don't set shlib_disabled here,
6990 we'll try to insert those breakpoints and fail. */
1042e4c0 6991 if (((b->type == bp_breakpoint)
508ccb1f 6992 || (b->type == bp_jit_event)
1042e4c0 6993 || (b->type == bp_hardware_breakpoint)
d77f58be 6994 || (is_tracepoint (b)))
6c95b8df 6995 && loc->pspace == current_program_space
0d381245 6996 && !loc->shlib_disabled
a77053c2 6997#ifdef PC_SOLIB
0d381245 6998 && PC_SOLIB (loc->address)
a77053c2 6999#else
6c95b8df 7000 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
7001#endif
7002 )
0d381245
VP
7003 {
7004 loc->shlib_disabled = 1;
7005 }
cae688ec
JJ
7006 }
7007}
7008
1e4d1764
YQ
7009/* Disable any breakpoints and tracepoints that are in an unloaded shared
7010 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 7011 disabled. */
84acb35a 7012
75149521 7013static void
84acb35a
JJ
7014disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7015{
876fa593 7016 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7017 int disabled_shlib_breaks = 0;
7018
c86cf029
VP
7019 /* SunOS a.out shared libraries are always mapped, so do not
7020 disable breakpoints; they will only be reported as unloaded
7021 through clear_solib when GDB discards its shared library
7022 list. See clear_solib for more information. */
7023 if (exec_bfd != NULL
7024 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7025 return;
7026
876fa593 7027 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7028 {
2bdf28a0 7029 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7030 struct breakpoint *b = loc->owner;
cc59ec59 7031
1e4d1764 7032 if (solib->pspace == loc->pspace
e2dd7057 7033 && !loc->shlib_disabled
1e4d1764
YQ
7034 && (((b->type == bp_breakpoint
7035 || b->type == bp_jit_event
7036 || b->type == bp_hardware_breakpoint)
7037 && (loc->loc_type == bp_loc_hardware_breakpoint
7038 || loc->loc_type == bp_loc_software_breakpoint))
7039 || is_tracepoint (b))
e2dd7057 7040 && solib_contains_address_p (solib, loc->address))
84acb35a 7041 {
e2dd7057
PP
7042 loc->shlib_disabled = 1;
7043 /* At this point, we cannot rely on remove_breakpoint
7044 succeeding so we must mark the breakpoint as not inserted
7045 to prevent future errors occurring in remove_breakpoints. */
7046 loc->inserted = 0;
8d3788bd
VP
7047
7048 /* This may cause duplicate notifications for the same breakpoint. */
7049 observer_notify_breakpoint_modified (b);
7050
e2dd7057
PP
7051 if (!disabled_shlib_breaks)
7052 {
7053 target_terminal_ours_for_output ();
3e43a32a
MS
7054 warning (_("Temporarily disabling breakpoints "
7055 "for unloaded shared library \"%s\""),
e2dd7057 7056 solib->so_name);
84acb35a 7057 }
e2dd7057 7058 disabled_shlib_breaks = 1;
84acb35a
JJ
7059 }
7060 }
84acb35a
JJ
7061}
7062
ce78b96d
JB
7063/* FORK & VFORK catchpoints. */
7064
e29a4733
PA
7065/* An instance of this type is used to represent a fork or vfork
7066 catchpoint. It includes a "struct breakpoint" as a kind of base
7067 class; users downcast to "struct breakpoint *" when needed. A
7068 breakpoint is really of this type iff its ops pointer points to
7069 CATCH_FORK_BREAKPOINT_OPS. */
7070
7071struct fork_catchpoint
7072{
7073 /* The base class. */
7074 struct breakpoint base;
7075
7076 /* Process id of a child process whose forking triggered this
7077 catchpoint. This field is only valid immediately after this
7078 catchpoint has triggered. */
7079 ptid_t forked_inferior_pid;
7080};
7081
4a64f543
MS
7082/* Implement the "insert" breakpoint_ops method for fork
7083 catchpoints. */
ce78b96d 7084
77b06cd7
TJB
7085static int
7086insert_catch_fork (struct bp_location *bl)
ce78b96d 7087{
77b06cd7 7088 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7089}
7090
4a64f543
MS
7091/* Implement the "remove" breakpoint_ops method for fork
7092 catchpoints. */
ce78b96d
JB
7093
7094static int
77b06cd7 7095remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7096{
7097 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7098}
7099
7100/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7101 catchpoints. */
7102
7103static int
f1310107 7104breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7105 struct address_space *aspace, CORE_ADDR bp_addr,
7106 const struct target_waitstatus *ws)
ce78b96d 7107{
e29a4733
PA
7108 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7109
f90263c1
TT
7110 if (ws->kind != TARGET_WAITKIND_FORKED)
7111 return 0;
7112
7113 c->forked_inferior_pid = ws->value.related_pid;
7114 return 1;
ce78b96d
JB
7115}
7116
4a64f543
MS
7117/* Implement the "print_it" breakpoint_ops method for fork
7118 catchpoints. */
ce78b96d
JB
7119
7120static enum print_stop_action
348d480f 7121print_it_catch_fork (bpstat bs)
ce78b96d 7122{
36dfb11c 7123 struct ui_out *uiout = current_uiout;
348d480f
PA
7124 struct breakpoint *b = bs->breakpoint_at;
7125 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7126
ce78b96d 7127 annotate_catchpoint (b->number);
36dfb11c
TT
7128 if (b->disposition == disp_del)
7129 ui_out_text (uiout, "\nTemporary catchpoint ");
7130 else
7131 ui_out_text (uiout, "\nCatchpoint ");
7132 if (ui_out_is_mi_like_p (uiout))
7133 {
7134 ui_out_field_string (uiout, "reason",
7135 async_reason_lookup (EXEC_ASYNC_FORK));
7136 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7137 }
7138 ui_out_field_int (uiout, "bkptno", b->number);
7139 ui_out_text (uiout, " (forked process ");
7140 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7141 ui_out_text (uiout, "), ");
ce78b96d
JB
7142 return PRINT_SRC_AND_LOC;
7143}
7144
4a64f543
MS
7145/* Implement the "print_one" breakpoint_ops method for fork
7146 catchpoints. */
ce78b96d
JB
7147
7148static void
a6d9a66e 7149print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7150{
e29a4733 7151 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7152 struct value_print_options opts;
79a45e25 7153 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7154
7155 get_user_print_options (&opts);
7156
4a64f543
MS
7157 /* Field 4, the address, is omitted (which makes the columns not
7158 line up too nicely with the headers, but the effect is relatively
7159 readable). */
79a45b7d 7160 if (opts.addressprint)
ce78b96d
JB
7161 ui_out_field_skip (uiout, "addr");
7162 annotate_field (5);
7163 ui_out_text (uiout, "fork");
e29a4733 7164 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7165 {
7166 ui_out_text (uiout, ", process ");
7167 ui_out_field_int (uiout, "what",
e29a4733 7168 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7169 ui_out_spaces (uiout, 1);
7170 }
7171}
7172
7173/* Implement the "print_mention" breakpoint_ops method for fork
7174 catchpoints. */
7175
7176static void
7177print_mention_catch_fork (struct breakpoint *b)
7178{
7179 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7180}
7181
6149aea9
PA
7182/* Implement the "print_recreate" breakpoint_ops method for fork
7183 catchpoints. */
7184
7185static void
7186print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7187{
7188 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7189 print_recreate_thread (b, fp);
6149aea9
PA
7190}
7191
ce78b96d
JB
7192/* The breakpoint_ops structure to be used in fork catchpoints. */
7193
2060206e 7194static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7195
4a64f543
MS
7196/* Implement the "insert" breakpoint_ops method for vfork
7197 catchpoints. */
ce78b96d 7198
77b06cd7
TJB
7199static int
7200insert_catch_vfork (struct bp_location *bl)
ce78b96d 7201{
77b06cd7 7202 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7203}
7204
4a64f543
MS
7205/* Implement the "remove" breakpoint_ops method for vfork
7206 catchpoints. */
ce78b96d
JB
7207
7208static int
77b06cd7 7209remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7210{
7211 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7212}
7213
7214/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7215 catchpoints. */
7216
7217static int
f1310107 7218breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7219 struct address_space *aspace, CORE_ADDR bp_addr,
7220 const struct target_waitstatus *ws)
ce78b96d 7221{
e29a4733
PA
7222 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7223
f90263c1
TT
7224 if (ws->kind != TARGET_WAITKIND_VFORKED)
7225 return 0;
7226
7227 c->forked_inferior_pid = ws->value.related_pid;
7228 return 1;
ce78b96d
JB
7229}
7230
4a64f543
MS
7231/* Implement the "print_it" breakpoint_ops method for vfork
7232 catchpoints. */
ce78b96d
JB
7233
7234static enum print_stop_action
348d480f 7235print_it_catch_vfork (bpstat bs)
ce78b96d 7236{
36dfb11c 7237 struct ui_out *uiout = current_uiout;
348d480f 7238 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7239 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7240
ce78b96d 7241 annotate_catchpoint (b->number);
36dfb11c
TT
7242 if (b->disposition == disp_del)
7243 ui_out_text (uiout, "\nTemporary catchpoint ");
7244 else
7245 ui_out_text (uiout, "\nCatchpoint ");
7246 if (ui_out_is_mi_like_p (uiout))
7247 {
7248 ui_out_field_string (uiout, "reason",
7249 async_reason_lookup (EXEC_ASYNC_VFORK));
7250 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7251 }
7252 ui_out_field_int (uiout, "bkptno", b->number);
7253 ui_out_text (uiout, " (vforked process ");
7254 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7255 ui_out_text (uiout, "), ");
ce78b96d
JB
7256 return PRINT_SRC_AND_LOC;
7257}
7258
4a64f543
MS
7259/* Implement the "print_one" breakpoint_ops method for vfork
7260 catchpoints. */
ce78b96d
JB
7261
7262static void
a6d9a66e 7263print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7264{
e29a4733 7265 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7266 struct value_print_options opts;
79a45e25 7267 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7268
7269 get_user_print_options (&opts);
4a64f543
MS
7270 /* Field 4, the address, is omitted (which makes the columns not
7271 line up too nicely with the headers, but the effect is relatively
7272 readable). */
79a45b7d 7273 if (opts.addressprint)
ce78b96d
JB
7274 ui_out_field_skip (uiout, "addr");
7275 annotate_field (5);
7276 ui_out_text (uiout, "vfork");
e29a4733 7277 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7278 {
7279 ui_out_text (uiout, ", process ");
7280 ui_out_field_int (uiout, "what",
e29a4733 7281 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7282 ui_out_spaces (uiout, 1);
7283 }
7284}
7285
7286/* Implement the "print_mention" breakpoint_ops method for vfork
7287 catchpoints. */
7288
7289static void
7290print_mention_catch_vfork (struct breakpoint *b)
7291{
7292 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7293}
7294
6149aea9
PA
7295/* Implement the "print_recreate" breakpoint_ops method for vfork
7296 catchpoints. */
7297
7298static void
7299print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7300{
7301 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7302 print_recreate_thread (b, fp);
6149aea9
PA
7303}
7304
ce78b96d
JB
7305/* The breakpoint_ops structure to be used in vfork catchpoints. */
7306
2060206e 7307static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7308
edcc5120
TT
7309/* An instance of this type is used to represent an solib catchpoint.
7310 It includes a "struct breakpoint" as a kind of base class; users
7311 downcast to "struct breakpoint *" when needed. A breakpoint is
7312 really of this type iff its ops pointer points to
7313 CATCH_SOLIB_BREAKPOINT_OPS. */
7314
7315struct solib_catchpoint
7316{
7317 /* The base class. */
7318 struct breakpoint base;
7319
7320 /* True for "catch load", false for "catch unload". */
7321 unsigned char is_load;
7322
7323 /* Regular expression to match, if any. COMPILED is only valid when
7324 REGEX is non-NULL. */
7325 char *regex;
7326 regex_t compiled;
7327};
7328
7329static void
7330dtor_catch_solib (struct breakpoint *b)
7331{
7332 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7333
7334 if (self->regex)
7335 regfree (&self->compiled);
7336 xfree (self->regex);
7337
7338 base_breakpoint_ops.dtor (b);
7339}
7340
7341static int
7342insert_catch_solib (struct bp_location *ignore)
7343{
7344 return 0;
7345}
7346
7347static int
7348remove_catch_solib (struct bp_location *ignore)
7349{
7350 return 0;
7351}
7352
7353static int
7354breakpoint_hit_catch_solib (const struct bp_location *bl,
7355 struct address_space *aspace,
7356 CORE_ADDR bp_addr,
7357 const struct target_waitstatus *ws)
7358{
7359 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7360 struct breakpoint *other;
7361
7362 if (ws->kind == TARGET_WAITKIND_LOADED)
7363 return 1;
7364
7365 ALL_BREAKPOINTS (other)
7366 {
7367 struct bp_location *other_bl;
7368
7369 if (other == bl->owner)
7370 continue;
7371
7372 if (other->type != bp_shlib_event)
7373 continue;
7374
7375 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7376 continue;
7377
7378 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7379 {
7380 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7381 return 1;
7382 }
7383 }
7384
7385 return 0;
7386}
7387
7388static void
7389check_status_catch_solib (struct bpstats *bs)
7390{
7391 struct solib_catchpoint *self
7392 = (struct solib_catchpoint *) bs->breakpoint_at;
7393 int ix;
7394
7395 if (self->is_load)
7396 {
7397 struct so_list *iter;
7398
7399 for (ix = 0;
7400 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7401 ix, iter);
7402 ++ix)
7403 {
7404 if (!self->regex
7405 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7406 return;
7407 }
7408 }
7409 else
7410 {
7411 char *iter;
7412
7413 for (ix = 0;
7414 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7415 ix, iter);
7416 ++ix)
7417 {
7418 if (!self->regex
7419 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7420 return;
7421 }
7422 }
7423
7424 bs->stop = 0;
7425 bs->print_it = print_it_noop;
7426}
7427
7428static enum print_stop_action
7429print_it_catch_solib (bpstat bs)
7430{
7431 struct breakpoint *b = bs->breakpoint_at;
7432 struct ui_out *uiout = current_uiout;
7433
7434 annotate_catchpoint (b->number);
7435 if (b->disposition == disp_del)
7436 ui_out_text (uiout, "\nTemporary catchpoint ");
7437 else
7438 ui_out_text (uiout, "\nCatchpoint ");
7439 ui_out_field_int (uiout, "bkptno", b->number);
7440 ui_out_text (uiout, "\n");
7441 if (ui_out_is_mi_like_p (uiout))
7442 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7443 print_solib_event (1);
7444 return PRINT_SRC_AND_LOC;
7445}
7446
7447static void
7448print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7449{
7450 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7451 struct value_print_options opts;
7452 struct ui_out *uiout = current_uiout;
7453 char *msg;
7454
7455 get_user_print_options (&opts);
7456 /* Field 4, the address, is omitted (which makes the columns not
7457 line up too nicely with the headers, but the effect is relatively
7458 readable). */
7459 if (opts.addressprint)
7460 {
7461 annotate_field (4);
7462 ui_out_field_skip (uiout, "addr");
7463 }
7464
7465 annotate_field (5);
7466 if (self->is_load)
7467 {
7468 if (self->regex)
7469 msg = xstrprintf (_("load of library matching %s"), self->regex);
7470 else
7471 msg = xstrdup (_("load of library"));
7472 }
7473 else
7474 {
7475 if (self->regex)
7476 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7477 else
7478 msg = xstrdup (_("unload of library"));
7479 }
7480 ui_out_field_string (uiout, "what", msg);
7481 xfree (msg);
7482}
7483
7484static void
7485print_mention_catch_solib (struct breakpoint *b)
7486{
7487 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7488
7489 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7490 self->is_load ? "load" : "unload");
7491}
7492
7493static void
7494print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7495{
7496 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7497
7498 fprintf_unfiltered (fp, "%s %s",
7499 b->disposition == disp_del ? "tcatch" : "catch",
7500 self->is_load ? "load" : "unload");
7501 if (self->regex)
7502 fprintf_unfiltered (fp, " %s", self->regex);
7503 fprintf_unfiltered (fp, "\n");
7504}
7505
7506static struct breakpoint_ops catch_solib_breakpoint_ops;
7507
7508/* A helper function that does all the work for "catch load" and
7509 "catch unload". */
7510
7511static void
7512catch_load_or_unload (char *arg, int from_tty, int is_load,
7513 struct cmd_list_element *command)
7514{
7515 struct solib_catchpoint *c;
7516 struct gdbarch *gdbarch = get_current_arch ();
7517 int tempflag;
edcc5120
TT
7518 struct cleanup *cleanup;
7519
7520 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7521
7522 if (!arg)
7523 arg = "";
7524 arg = skip_spaces (arg);
7525
7526 c = XCNEW (struct solib_catchpoint);
7527 cleanup = make_cleanup (xfree, c);
7528
7529 if (*arg != '\0')
7530 {
7531 int errcode;
7532
7533 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7534 if (errcode != 0)
7535 {
7536 char *err = get_regcomp_error (errcode, &c->compiled);
7537
7538 make_cleanup (xfree, err);
7539 error (_("Invalid regexp (%s): %s"), err, arg);
7540 }
7541 c->regex = xstrdup (arg);
7542 }
7543
7544 c->is_load = is_load;
7545 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7546 &catch_solib_breakpoint_ops);
7547
7548 discard_cleanups (cleanup);
7549 install_breakpoint (0, &c->base, 1);
7550}
7551
7552static void
7553catch_load_command_1 (char *arg, int from_tty,
7554 struct cmd_list_element *command)
7555{
7556 catch_load_or_unload (arg, from_tty, 1, command);
7557}
7558
7559static void
7560catch_unload_command_1 (char *arg, int from_tty,
7561 struct cmd_list_element *command)
7562{
7563 catch_load_or_unload (arg, from_tty, 0, command);
7564}
7565
fa3064dd
YQ
7566DEF_VEC_I(int);
7567
be5c67c1
PA
7568/* An instance of this type is used to represent a syscall catchpoint.
7569 It includes a "struct breakpoint" as a kind of base class; users
7570 downcast to "struct breakpoint *" when needed. A breakpoint is
7571 really of this type iff its ops pointer points to
7572 CATCH_SYSCALL_BREAKPOINT_OPS. */
7573
7574struct syscall_catchpoint
7575{
7576 /* The base class. */
7577 struct breakpoint base;
7578
7579 /* Syscall numbers used for the 'catch syscall' feature. If no
7580 syscall has been specified for filtering, its value is NULL.
7581 Otherwise, it holds a list of all syscalls to be caught. The
7582 list elements are allocated with xmalloc. */
7583 VEC(int) *syscalls_to_be_caught;
7584};
7585
7586/* Implement the "dtor" breakpoint_ops method for syscall
7587 catchpoints. */
7588
7589static void
7590dtor_catch_syscall (struct breakpoint *b)
7591{
7592 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7593
7594 VEC_free (int, c->syscalls_to_be_caught);
348d480f 7595
2060206e 7596 base_breakpoint_ops.dtor (b);
be5c67c1
PA
7597}
7598
fa3064dd
YQ
7599static const struct inferior_data *catch_syscall_inferior_data = NULL;
7600
7601struct catch_syscall_inferior_data
7602{
7603 /* We keep a count of the number of times the user has requested a
7604 particular syscall to be tracked, and pass this information to the
7605 target. This lets capable targets implement filtering directly. */
7606
7607 /* Number of times that "any" syscall is requested. */
7608 int any_syscall_count;
7609
7610 /* Count of each system call. */
7611 VEC(int) *syscalls_counts;
7612
7613 /* This counts all syscall catch requests, so we can readily determine
7614 if any catching is necessary. */
7615 int total_syscalls_count;
7616};
7617
7618static struct catch_syscall_inferior_data*
7619get_catch_syscall_inferior_data (struct inferior *inf)
7620{
7621 struct catch_syscall_inferior_data *inf_data;
7622
7623 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7624 if (inf_data == NULL)
7625 {
7626 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7627 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7628 }
7629
7630 return inf_data;
7631}
7632
7633static void
7634catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7635{
7636 xfree (arg);
7637}
7638
7639
a96d9b2e
SDJ
7640/* Implement the "insert" breakpoint_ops method for syscall
7641 catchpoints. */
7642
77b06cd7
TJB
7643static int
7644insert_catch_syscall (struct bp_location *bl)
a96d9b2e 7645{
be5c67c1 7646 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7647 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7648 struct catch_syscall_inferior_data *inf_data
7649 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7650
fa3064dd 7651 ++inf_data->total_syscalls_count;
be5c67c1 7652 if (!c->syscalls_to_be_caught)
fa3064dd 7653 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
7654 else
7655 {
7656 int i, iter;
cc59ec59 7657
a96d9b2e 7658 for (i = 0;
be5c67c1 7659 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7660 i++)
7661 {
7662 int elem;
cc59ec59 7663
fa3064dd 7664 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 7665 {
fa3064dd 7666 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
7667 uintptr_t vec_addr_offset
7668 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 7669 uintptr_t vec_addr;
fa3064dd
YQ
7670 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7671 vec_addr = ((uintptr_t) VEC_address (int,
7672 inf_data->syscalls_counts)
7673 + vec_addr_offset);
a96d9b2e
SDJ
7674 memset ((void *) vec_addr, 0,
7675 (iter + 1 - old_size) * sizeof (int));
7676 }
fa3064dd
YQ
7677 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7678 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
7679 }
7680 }
7681
77b06cd7 7682 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7683 inf_data->total_syscalls_count != 0,
7684 inf_data->any_syscall_count,
7685 VEC_length (int,
7686 inf_data->syscalls_counts),
7687 VEC_address (int,
7688 inf_data->syscalls_counts));
a96d9b2e
SDJ
7689}
7690
7691/* Implement the "remove" breakpoint_ops method for syscall
7692 catchpoints. */
7693
7694static int
77b06cd7 7695remove_catch_syscall (struct bp_location *bl)
a96d9b2e 7696{
be5c67c1 7697 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7698 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7699 struct catch_syscall_inferior_data *inf_data
7700 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7701
fa3064dd 7702 --inf_data->total_syscalls_count;
be5c67c1 7703 if (!c->syscalls_to_be_caught)
fa3064dd 7704 --inf_data->any_syscall_count;
a96d9b2e
SDJ
7705 else
7706 {
7707 int i, iter;
cc59ec59 7708
a96d9b2e 7709 for (i = 0;
be5c67c1 7710 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7711 i++)
7712 {
7713 int elem;
fa3064dd 7714 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
7715 /* Shouldn't happen. */
7716 continue;
fa3064dd
YQ
7717 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7718 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
7719 }
7720 }
7721
7722 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7723 inf_data->total_syscalls_count != 0,
7724 inf_data->any_syscall_count,
7725 VEC_length (int,
7726 inf_data->syscalls_counts),
3e43a32a 7727 VEC_address (int,
fa3064dd 7728 inf_data->syscalls_counts));
a96d9b2e
SDJ
7729}
7730
7731/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7732 catchpoints. */
7733
7734static int
f1310107 7735breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
7736 struct address_space *aspace, CORE_ADDR bp_addr,
7737 const struct target_waitstatus *ws)
a96d9b2e 7738{
4a64f543
MS
7739 /* We must check if we are catching specific syscalls in this
7740 breakpoint. If we are, then we must guarantee that the called
7741 syscall is the same syscall we are catching. */
a96d9b2e 7742 int syscall_number = 0;
be5c67c1
PA
7743 const struct syscall_catchpoint *c
7744 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 7745
f90263c1
TT
7746 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7747 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
7748 return 0;
7749
f90263c1
TT
7750 syscall_number = ws->value.syscall_number;
7751
a96d9b2e 7752 /* Now, checking if the syscall is the same. */
be5c67c1 7753 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7754 {
7755 int i, iter;
cc59ec59 7756
a96d9b2e 7757 for (i = 0;
be5c67c1 7758 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7759 i++)
7760 if (syscall_number == iter)
7761 break;
7762 /* Not the same. */
7763 if (!iter)
7764 return 0;
7765 }
7766
7767 return 1;
7768}
7769
7770/* Implement the "print_it" breakpoint_ops method for syscall
7771 catchpoints. */
7772
7773static enum print_stop_action
348d480f 7774print_it_catch_syscall (bpstat bs)
a96d9b2e 7775{
36dfb11c 7776 struct ui_out *uiout = current_uiout;
348d480f 7777 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
7778 /* These are needed because we want to know in which state a
7779 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7780 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7781 must print "called syscall" or "returned from syscall". */
7782 ptid_t ptid;
7783 struct target_waitstatus last;
7784 struct syscall s;
a96d9b2e
SDJ
7785
7786 get_last_target_status (&ptid, &last);
7787
7788 get_syscall_by_number (last.value.syscall_number, &s);
7789
7790 annotate_catchpoint (b->number);
7791
36dfb11c
TT
7792 if (b->disposition == disp_del)
7793 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 7794 else
36dfb11c
TT
7795 ui_out_text (uiout, "\nCatchpoint ");
7796 if (ui_out_is_mi_like_p (uiout))
7797 {
7798 ui_out_field_string (uiout, "reason",
7799 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7800 ? EXEC_ASYNC_SYSCALL_ENTRY
7801 : EXEC_ASYNC_SYSCALL_RETURN));
7802 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7803 }
7804 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
7805
7806 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
7807 ui_out_text (uiout, " (call to syscall ");
7808 else
7809 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 7810
36dfb11c
TT
7811 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7812 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7813 if (s.name != NULL)
7814 ui_out_field_string (uiout, "syscall-name", s.name);
7815
7816 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
7817
7818 return PRINT_SRC_AND_LOC;
7819}
7820
7821/* Implement the "print_one" breakpoint_ops method for syscall
7822 catchpoints. */
7823
7824static void
7825print_one_catch_syscall (struct breakpoint *b,
f1310107 7826 struct bp_location **last_loc)
a96d9b2e 7827{
be5c67c1 7828 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7829 struct value_print_options opts;
79a45e25 7830 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7831
7832 get_user_print_options (&opts);
4a64f543
MS
7833 /* Field 4, the address, is omitted (which makes the columns not
7834 line up too nicely with the headers, but the effect is relatively
7835 readable). */
a96d9b2e
SDJ
7836 if (opts.addressprint)
7837 ui_out_field_skip (uiout, "addr");
7838 annotate_field (5);
7839
be5c67c1
PA
7840 if (c->syscalls_to_be_caught
7841 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7842 ui_out_text (uiout, "syscalls \"");
7843 else
7844 ui_out_text (uiout, "syscall \"");
7845
be5c67c1 7846 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7847 {
7848 int i, iter;
7849 char *text = xstrprintf ("%s", "");
cc59ec59 7850
a96d9b2e 7851 for (i = 0;
be5c67c1 7852 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7853 i++)
7854 {
7855 char *x = text;
7856 struct syscall s;
7857 get_syscall_by_number (iter, &s);
7858
7859 if (s.name != NULL)
7860 text = xstrprintf ("%s%s, ", text, s.name);
7861 else
7862 text = xstrprintf ("%s%d, ", text, iter);
7863
7864 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7865 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7866 on every call. */
7867 xfree (x);
7868 }
7869 /* Remove the last comma. */
7870 text[strlen (text) - 2] = '\0';
7871 ui_out_field_string (uiout, "what", text);
7872 }
7873 else
7874 ui_out_field_string (uiout, "what", "<any syscall>");
7875 ui_out_text (uiout, "\" ");
7876}
7877
7878/* Implement the "print_mention" breakpoint_ops method for syscall
7879 catchpoints. */
7880
7881static void
7882print_mention_catch_syscall (struct breakpoint *b)
7883{
be5c67c1
PA
7884 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7885
7886 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7887 {
7888 int i, iter;
7889
be5c67c1 7890 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7891 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7892 else
7893 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7894
7895 for (i = 0;
be5c67c1 7896 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7897 i++)
7898 {
7899 struct syscall s;
7900 get_syscall_by_number (iter, &s);
7901
7902 if (s.name)
7903 printf_filtered (" '%s' [%d]", s.name, s.number);
7904 else
7905 printf_filtered (" %d", s.number);
7906 }
7907 printf_filtered (")");
7908 }
7909 else
7910 printf_filtered (_("Catchpoint %d (any syscall)"),
7911 b->number);
7912}
7913
6149aea9
PA
7914/* Implement the "print_recreate" breakpoint_ops method for syscall
7915 catchpoints. */
7916
7917static void
7918print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7919{
be5c67c1
PA
7920 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7921
6149aea9
PA
7922 fprintf_unfiltered (fp, "catch syscall");
7923
be5c67c1 7924 if (c->syscalls_to_be_caught)
6149aea9
PA
7925 {
7926 int i, iter;
7927
7928 for (i = 0;
be5c67c1 7929 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
7930 i++)
7931 {
7932 struct syscall s;
7933
7934 get_syscall_by_number (iter, &s);
7935 if (s.name)
7936 fprintf_unfiltered (fp, " %s", s.name);
7937 else
7938 fprintf_unfiltered (fp, " %d", s.number);
7939 }
7940 }
d9b3f62e 7941 print_recreate_thread (b, fp);
6149aea9
PA
7942}
7943
a96d9b2e
SDJ
7944/* The breakpoint_ops structure to be used in syscall catchpoints. */
7945
2060206e 7946static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
7947
7948/* Returns non-zero if 'b' is a syscall catchpoint. */
7949
7950static int
7951syscall_catchpoint_p (struct breakpoint *b)
7952{
7953 return (b->ops == &catch_syscall_breakpoint_ops);
7954}
7955
346774a9
PA
7956/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
7957 is non-zero, then make the breakpoint temporary. If COND_STRING is
7958 not NULL, then store it in the breakpoint. OPS, if not NULL, is
7959 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 7960
346774a9
PA
7961static void
7962init_catchpoint (struct breakpoint *b,
7963 struct gdbarch *gdbarch, int tempflag,
7964 char *cond_string,
c0a91b2b 7965 const struct breakpoint_ops *ops)
c906108c 7966{
c5aa993b 7967 struct symtab_and_line sal;
346774a9 7968
fe39c653 7969 init_sal (&sal);
6c95b8df 7970 sal.pspace = current_program_space;
c5aa993b 7971
28010a5d 7972 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 7973
1b36a34b 7974 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 7975 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
7976}
7977
28010a5d 7978void
3ea46bff 7979install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
7980{
7981 add_to_breakpoint_chain (b);
3a5c3e22
PA
7982 set_breakpoint_number (internal, b);
7983 if (!internal)
7984 mention (b);
c56053d2 7985 observer_notify_breakpoint_created (b);
3ea46bff
YQ
7986
7987 if (update_gll)
7988 update_global_location_list (1);
c56053d2
PA
7989}
7990
9b70b993 7991static void
a6d9a66e
UW
7992create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7993 int tempflag, char *cond_string,
c0a91b2b 7994 const struct breakpoint_ops *ops)
c906108c 7995{
e29a4733 7996 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 7997
e29a4733
PA
7998 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7999
8000 c->forked_inferior_pid = null_ptid;
8001
3ea46bff 8002 install_breakpoint (0, &c->base, 1);
c906108c
SS
8003}
8004
fe798b75
JB
8005/* Exec catchpoints. */
8006
b4d90040
PA
8007/* An instance of this type is used to represent an exec catchpoint.
8008 It includes a "struct breakpoint" as a kind of base class; users
8009 downcast to "struct breakpoint *" when needed. A breakpoint is
8010 really of this type iff its ops pointer points to
8011 CATCH_EXEC_BREAKPOINT_OPS. */
8012
8013struct exec_catchpoint
8014{
8015 /* The base class. */
8016 struct breakpoint base;
8017
8018 /* Filename of a program whose exec triggered this catchpoint.
8019 This field is only valid immediately after this catchpoint has
8020 triggered. */
8021 char *exec_pathname;
8022};
8023
8024/* Implement the "dtor" breakpoint_ops method for exec
8025 catchpoints. */
8026
8027static void
8028dtor_catch_exec (struct breakpoint *b)
8029{
8030 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8031
8032 xfree (c->exec_pathname);
348d480f 8033
2060206e 8034 base_breakpoint_ops.dtor (b);
b4d90040
PA
8035}
8036
77b06cd7
TJB
8037static int
8038insert_catch_exec (struct bp_location *bl)
c906108c 8039{
77b06cd7 8040 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 8041}
c906108c 8042
fe798b75 8043static int
77b06cd7 8044remove_catch_exec (struct bp_location *bl)
fe798b75
JB
8045{
8046 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8047}
c906108c 8048
fe798b75 8049static int
f1310107 8050breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8051 struct address_space *aspace, CORE_ADDR bp_addr,
8052 const struct target_waitstatus *ws)
fe798b75 8053{
b4d90040
PA
8054 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8055
f90263c1
TT
8056 if (ws->kind != TARGET_WAITKIND_EXECD)
8057 return 0;
8058
8059 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8060 return 1;
fe798b75 8061}
c906108c 8062
fe798b75 8063static enum print_stop_action
348d480f 8064print_it_catch_exec (bpstat bs)
fe798b75 8065{
36dfb11c 8066 struct ui_out *uiout = current_uiout;
348d480f 8067 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8068 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8069
fe798b75 8070 annotate_catchpoint (b->number);
36dfb11c
TT
8071 if (b->disposition == disp_del)
8072 ui_out_text (uiout, "\nTemporary catchpoint ");
8073 else
8074 ui_out_text (uiout, "\nCatchpoint ");
8075 if (ui_out_is_mi_like_p (uiout))
8076 {
8077 ui_out_field_string (uiout, "reason",
8078 async_reason_lookup (EXEC_ASYNC_EXEC));
8079 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8080 }
8081 ui_out_field_int (uiout, "bkptno", b->number);
8082 ui_out_text (uiout, " (exec'd ");
8083 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8084 ui_out_text (uiout, "), ");
8085
fe798b75 8086 return PRINT_SRC_AND_LOC;
c906108c
SS
8087}
8088
fe798b75 8089static void
a6d9a66e 8090print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8091{
b4d90040 8092 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8093 struct value_print_options opts;
79a45e25 8094 struct ui_out *uiout = current_uiout;
fe798b75
JB
8095
8096 get_user_print_options (&opts);
8097
8098 /* Field 4, the address, is omitted (which makes the columns
8099 not line up too nicely with the headers, but the effect
8100 is relatively readable). */
8101 if (opts.addressprint)
8102 ui_out_field_skip (uiout, "addr");
8103 annotate_field (5);
8104 ui_out_text (uiout, "exec");
b4d90040 8105 if (c->exec_pathname != NULL)
fe798b75
JB
8106 {
8107 ui_out_text (uiout, ", program \"");
b4d90040 8108 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8109 ui_out_text (uiout, "\" ");
8110 }
8111}
8112
8113static void
8114print_mention_catch_exec (struct breakpoint *b)
8115{
8116 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8117}
8118
6149aea9
PA
8119/* Implement the "print_recreate" breakpoint_ops method for exec
8120 catchpoints. */
8121
8122static void
8123print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8124{
8125 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8126 print_recreate_thread (b, fp);
6149aea9
PA
8127}
8128
2060206e 8129static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8130
a96d9b2e
SDJ
8131static void
8132create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8133 const struct breakpoint_ops *ops)
a96d9b2e 8134{
be5c67c1 8135 struct syscall_catchpoint *c;
a96d9b2e 8136 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8137
be5c67c1
PA
8138 c = XNEW (struct syscall_catchpoint);
8139 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8140 c->syscalls_to_be_caught = filter;
a96d9b2e 8141
3ea46bff 8142 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8143}
8144
c906108c 8145static int
fba45db2 8146hw_breakpoint_used_count (void)
c906108c 8147{
c906108c 8148 int i = 0;
f1310107
TJB
8149 struct breakpoint *b;
8150 struct bp_location *bl;
c906108c
SS
8151
8152 ALL_BREAKPOINTS (b)
c5aa993b 8153 {
d6b74ac4 8154 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8155 for (bl = b->loc; bl; bl = bl->next)
8156 {
8157 /* Special types of hardware breakpoints may use more than
8158 one register. */
348d480f 8159 i += b->ops->resources_needed (bl);
f1310107 8160 }
c5aa993b 8161 }
c906108c
SS
8162
8163 return i;
8164}
8165
a1398e0c
PA
8166/* Returns the resources B would use if it were a hardware
8167 watchpoint. */
8168
c906108c 8169static int
a1398e0c 8170hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8171{
c906108c 8172 int i = 0;
e09342b5 8173 struct bp_location *bl;
c906108c 8174
a1398e0c
PA
8175 if (!breakpoint_enabled (b))
8176 return 0;
8177
8178 for (bl = b->loc; bl; bl = bl->next)
8179 {
8180 /* Special types of hardware watchpoints may use more than
8181 one register. */
8182 i += b->ops->resources_needed (bl);
8183 }
8184
8185 return i;
8186}
8187
8188/* Returns the sum the used resources of all hardware watchpoints of
8189 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8190 the sum of the used resources of all hardware watchpoints of other
8191 types _not_ TYPE. */
8192
8193static int
8194hw_watchpoint_used_count_others (struct breakpoint *except,
8195 enum bptype type, int *other_type_used)
8196{
8197 int i = 0;
8198 struct breakpoint *b;
8199
c906108c
SS
8200 *other_type_used = 0;
8201 ALL_BREAKPOINTS (b)
e09342b5 8202 {
a1398e0c
PA
8203 if (b == except)
8204 continue;
e09342b5
TJB
8205 if (!breakpoint_enabled (b))
8206 continue;
8207
a1398e0c
PA
8208 if (b->type == type)
8209 i += hw_watchpoint_use_count (b);
8210 else if (is_hardware_watchpoint (b))
8211 *other_type_used = 1;
e09342b5
TJB
8212 }
8213
c906108c
SS
8214 return i;
8215}
8216
c906108c 8217void
fba45db2 8218disable_watchpoints_before_interactive_call_start (void)
c906108c 8219{
c5aa993b 8220 struct breakpoint *b;
c906108c
SS
8221
8222 ALL_BREAKPOINTS (b)
c5aa993b 8223 {
cc60f2e3 8224 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8225 {
b5de0fa7 8226 b->enable_state = bp_call_disabled;
b60e7edf 8227 update_global_location_list (0);
c5aa993b
JM
8228 }
8229 }
c906108c
SS
8230}
8231
8232void
fba45db2 8233enable_watchpoints_after_interactive_call_stop (void)
c906108c 8234{
c5aa993b 8235 struct breakpoint *b;
c906108c
SS
8236
8237 ALL_BREAKPOINTS (b)
c5aa993b 8238 {
cc60f2e3 8239 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8240 {
b5de0fa7 8241 b->enable_state = bp_enabled;
b60e7edf 8242 update_global_location_list (1);
c5aa993b
JM
8243 }
8244 }
c906108c
SS
8245}
8246
8bea4e01
UW
8247void
8248disable_breakpoints_before_startup (void)
8249{
6c95b8df 8250 current_program_space->executing_startup = 1;
f8eba3c6 8251 update_global_location_list (0);
8bea4e01
UW
8252}
8253
8254void
8255enable_breakpoints_after_startup (void)
8256{
6c95b8df 8257 current_program_space->executing_startup = 0;
f8eba3c6 8258 breakpoint_re_set ();
8bea4e01
UW
8259}
8260
c906108c
SS
8261
8262/* Set a breakpoint that will evaporate an end of command
8263 at address specified by SAL.
8264 Restrict it to frame FRAME if FRAME is nonzero. */
8265
8266struct breakpoint *
a6d9a66e
UW
8267set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8268 struct frame_id frame_id, enum bptype type)
c906108c 8269{
52f0bd74 8270 struct breakpoint *b;
edb3359d
DJ
8271
8272 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8273 one. */
8274 gdb_assert (!frame_id_inlined_p (frame_id));
8275
06edf0c0 8276 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8277 b->enable_state = bp_enabled;
8278 b->disposition = disp_donttouch;
818dd999 8279 b->frame_id = frame_id;
c906108c 8280
4a64f543
MS
8281 /* If we're debugging a multi-threaded program, then we want
8282 momentary breakpoints to be active in only a single thread of
8283 control. */
39f77062
KB
8284 if (in_thread_list (inferior_ptid))
8285 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8286
b60e7edf 8287 update_global_location_list_nothrow (1);
74960c60 8288
c906108c
SS
8289 return b;
8290}
611c83ae 8291
06edf0c0
PA
8292/* Make a momentary breakpoint based on the master breakpoint ORIG.
8293 The new breakpoint will have type TYPE, and use OPS as it
8294 breakpoint_ops. */
e58b0e63 8295
06edf0c0
PA
8296static struct breakpoint *
8297momentary_breakpoint_from_master (struct breakpoint *orig,
8298 enum bptype type,
c0a91b2b 8299 const struct breakpoint_ops *ops)
e58b0e63
PA
8300{
8301 struct breakpoint *copy;
8302
06edf0c0 8303 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8304 copy->loc = allocate_bp_location (copy);
0e30163f 8305 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8306
a6d9a66e 8307 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8308 copy->loc->requested_address = orig->loc->requested_address;
8309 copy->loc->address = orig->loc->address;
8310 copy->loc->section = orig->loc->section;
6c95b8df 8311 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8312 copy->loc->probe = orig->loc->probe;
e58b0e63 8313
f8eba3c6
TT
8314 if (orig->loc->source_file != NULL)
8315 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 8316
f8eba3c6 8317 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
8318 copy->frame_id = orig->frame_id;
8319 copy->thread = orig->thread;
6c95b8df 8320 copy->pspace = orig->pspace;
e58b0e63
PA
8321
8322 copy->enable_state = bp_enabled;
8323 copy->disposition = disp_donttouch;
8324 copy->number = internal_breakpoint_number--;
8325
8326 update_global_location_list_nothrow (0);
8327 return copy;
8328}
8329
06edf0c0
PA
8330/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8331 ORIG is NULL. */
8332
8333struct breakpoint *
8334clone_momentary_breakpoint (struct breakpoint *orig)
8335{
8336 /* If there's nothing to clone, then return nothing. */
8337 if (orig == NULL)
8338 return NULL;
8339
8340 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8341}
8342
611c83ae 8343struct breakpoint *
a6d9a66e
UW
8344set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8345 enum bptype type)
611c83ae
PA
8346{
8347 struct symtab_and_line sal;
8348
8349 sal = find_pc_line (pc, 0);
8350 sal.pc = pc;
8351 sal.section = find_pc_overlay (pc);
8352 sal.explicit_pc = 1;
8353
a6d9a66e 8354 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8355}
c906108c 8356\f
c5aa993b 8357
c906108c
SS
8358/* Tell the user we have just set a breakpoint B. */
8359
8360static void
fba45db2 8361mention (struct breakpoint *b)
c906108c 8362{
348d480f 8363 b->ops->print_mention (b);
79a45e25 8364 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8365 return;
c906108c
SS
8366 printf_filtered ("\n");
8367}
c906108c 8368\f
c5aa993b 8369
0d381245 8370static struct bp_location *
39d61571 8371add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8372 const struct symtab_and_line *sal)
8373{
8374 struct bp_location *loc, **tmp;
3742cc8b
YQ
8375 CORE_ADDR adjusted_address;
8376 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8377
8378 if (loc_gdbarch == NULL)
8379 loc_gdbarch = b->gdbarch;
8380
8381 /* Adjust the breakpoint's address prior to allocating a location.
8382 Once we call allocate_bp_location(), that mostly uninitialized
8383 location will be placed on the location chain. Adjustment of the
8384 breakpoint may cause target_read_memory() to be called and we do
8385 not want its scan of the location chain to find a breakpoint and
8386 location that's only been partially initialized. */
8387 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8388 sal->pc, b->type);
0d381245 8389
39d61571 8390 loc = allocate_bp_location (b);
0d381245
VP
8391 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8392 ;
8393 *tmp = loc;
3742cc8b 8394
0d381245 8395 loc->requested_address = sal->pc;
3742cc8b 8396 loc->address = adjusted_address;
6c95b8df 8397 loc->pspace = sal->pspace;
55aa24fb 8398 loc->probe = sal->probe;
6c95b8df 8399 gdb_assert (loc->pspace != NULL);
0d381245 8400 loc->section = sal->section;
3742cc8b 8401 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
8402
8403 if (sal->symtab != NULL)
8404 loc->source_file = xstrdup (sal->symtab->filename);
8405 loc->line_number = sal->line;
8406
0e30163f
JK
8407 set_breakpoint_location_function (loc,
8408 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8409 return loc;
8410}
514f746b
AR
8411\f
8412
8413/* Return 1 if LOC is pointing to a permanent breakpoint,
8414 return 0 otherwise. */
8415
8416static int
8417bp_loc_is_permanent (struct bp_location *loc)
8418{
8419 int len;
8420 CORE_ADDR addr;
1afeeb75 8421 const gdb_byte *bpoint;
514f746b 8422 gdb_byte *target_mem;
939c61fa
JK
8423 struct cleanup *cleanup;
8424 int retval = 0;
514f746b
AR
8425
8426 gdb_assert (loc != NULL);
8427
8428 addr = loc->address;
1afeeb75 8429 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8430
939c61fa 8431 /* Software breakpoints unsupported? */
1afeeb75 8432 if (bpoint == NULL)
939c61fa
JK
8433 return 0;
8434
514f746b
AR
8435 target_mem = alloca (len);
8436
939c61fa
JK
8437 /* Enable the automatic memory restoration from breakpoints while
8438 we read the memory. Otherwise we could say about our temporary
8439 breakpoints they are permanent. */
6c95b8df
PA
8440 cleanup = save_current_space_and_thread ();
8441
8442 switch_to_program_space_and_thread (loc->pspace);
8443 make_show_memory_breakpoints_cleanup (0);
939c61fa 8444
514f746b 8445 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8446 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8447 retval = 1;
514f746b 8448
939c61fa
JK
8449 do_cleanups (cleanup);
8450
8451 return retval;
514f746b
AR
8452}
8453
e7e0cddf
SS
8454/* The style in which to perform a dynamic printf. This is a user
8455 option because different output options have different tradeoffs;
8456 if GDB does the printing, there is better error handling if there
8457 is a problem with any of the arguments, but using an inferior
8458 function lets you have special-purpose printers and sending of
8459 output to the same place as compiled-in print functions. (Future
8460 styles may include the ability to do a target-side printf.) */
8461
8462static const char dprintf_style_gdb[] = "gdb";
8463static const char dprintf_style_call[] = "call";
8464static const char *const dprintf_style_enums[] = {
8465 dprintf_style_gdb,
8466 dprintf_style_call,
8467 NULL
8468};
8469static const char *dprintf_style = dprintf_style_gdb;
8470
8471/* The function to use for dynamic printf if the preferred style is to
8472 call into the inferior. The value is simply a string that is
8473 copied into the command, so it can be anything that GDB can
8474 evaluate to a callable address, not necessarily a function name. */
8475
8476static char *dprintf_function = "";
8477
8478/* The channel to use for dynamic printf if the preferred style is to
8479 call into the inferior; if a nonempty string, it will be passed to
8480 the call as the first argument, with the format string as the
8481 second. As with the dprintf function, this can be anything that
8482 GDB knows how to evaluate, so in addition to common choices like
8483 "stderr", this could be an app-specific expression like
8484 "mystreams[curlogger]". */
514f746b 8485
e7e0cddf
SS
8486static char *dprintf_channel = "";
8487
8488/* Build a command list for the dprintf corresponding to the current
8489 settings of the dprintf style options. */
8490
8491static void
8492update_dprintf_command_list (struct breakpoint *b)
8493{
8494 char *dprintf_args = b->extra_string;
8495 char *printf_line = NULL;
8496
8497 if (!dprintf_args)
8498 return;
8499
8500 dprintf_args = skip_spaces (dprintf_args);
8501
8502 /* Allow a comma, as it may have terminated a location, but don't
8503 insist on it. */
8504 if (*dprintf_args == ',')
8505 ++dprintf_args;
8506 dprintf_args = skip_spaces (dprintf_args);
8507
8508 if (*dprintf_args != '"')
8509 error (_("Bad format string, missing '\"'."));
8510
8511 if (strcmp (dprintf_style, "gdb") == 0)
8512 printf_line = xstrprintf ("printf %s", dprintf_args);
8513 else if (strcmp (dprintf_style, "call") == 0)
8514 {
8515 if (!dprintf_function)
8516 error (_("No function supplied for dprintf call"));
8517
8518 if (dprintf_channel && strlen (dprintf_channel) > 0)
8519 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8520 dprintf_function,
8521 dprintf_channel,
8522 dprintf_args);
8523 else
8524 printf_line = xstrprintf ("call (void) %s (%s)",
8525 dprintf_function,
8526 dprintf_args);
8527 }
8528 else
8529 internal_error (__FILE__, __LINE__,
8530 _("Invalid dprintf style."));
8531
8532 /* Manufacture a printf/continue sequence. */
8533 if (printf_line)
8534 {
8535 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8536
8537 cont_cmd_line = xmalloc (sizeof (struct command_line));
8538 cont_cmd_line->control_type = simple_control;
8539 cont_cmd_line->body_count = 0;
8540 cont_cmd_line->body_list = NULL;
8541 cont_cmd_line->next = NULL;
8542 cont_cmd_line->line = xstrdup ("continue");
8543
8544 printf_cmd_line = xmalloc (sizeof (struct command_line));
8545 printf_cmd_line->control_type = simple_control;
8546 printf_cmd_line->body_count = 0;
8547 printf_cmd_line->body_list = NULL;
8548 printf_cmd_line->next = cont_cmd_line;
8549 printf_cmd_line->line = printf_line;
8550
8551 breakpoint_set_commands (b, printf_cmd_line);
8552 }
8553}
8554
8555/* Update all dprintf commands, making their command lists reflect
8556 current style settings. */
8557
8558static void
8559update_dprintf_commands (char *args, int from_tty,
8560 struct cmd_list_element *c)
8561{
8562 struct breakpoint *b;
8563
8564 ALL_BREAKPOINTS (b)
8565 {
8566 if (b->type == bp_dprintf)
8567 update_dprintf_command_list (b);
8568 }
8569}
c3f6f71d 8570
018d34a4
VP
8571/* Create a breakpoint with SAL as location. Use ADDR_STRING
8572 as textual description of the location, and COND_STRING
db107f19 8573 as condition expression. */
018d34a4
VP
8574
8575static void
d9b3f62e
PA
8576init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8577 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8578 char *filter, char *cond_string,
e7e0cddf 8579 char *extra_string,
d9b3f62e
PA
8580 enum bptype type, enum bpdisp disposition,
8581 int thread, int task, int ignore_count,
c0a91b2b 8582 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8583 int enabled, int internal, unsigned flags,
8584 int display_canonical)
018d34a4 8585{
0d381245 8586 int i;
018d34a4
VP
8587
8588 if (type == bp_hardware_breakpoint)
8589 {
fbbd034e
AS
8590 int target_resources_ok;
8591
8592 i = hw_breakpoint_used_count ();
8593 target_resources_ok =
8594 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8595 i + 1, 0);
8596 if (target_resources_ok == 0)
8597 error (_("No hardware breakpoint support in the target."));
8598 else if (target_resources_ok < 0)
8599 error (_("Hardware breakpoints used exceeds limit."));
8600 }
8601
6c95b8df
PA
8602 gdb_assert (sals.nelts > 0);
8603
0d381245
VP
8604 for (i = 0; i < sals.nelts; ++i)
8605 {
8606 struct symtab_and_line sal = sals.sals[i];
8607 struct bp_location *loc;
8608
8609 if (from_tty)
5af949e3
UW
8610 {
8611 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8612 if (!loc_gdbarch)
8613 loc_gdbarch = gdbarch;
8614
8615 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8616 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8617 }
0d381245
VP
8618
8619 if (i == 0)
8620 {
d9b3f62e 8621 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8622 b->thread = thread;
4a306c9a 8623 b->task = task;
855a6e68 8624
0d381245 8625 b->cond_string = cond_string;
e7e0cddf 8626 b->extra_string = extra_string;
0d381245 8627 b->ignore_count = ignore_count;
41447f92 8628 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8629 b->disposition = disposition;
6c95b8df 8630
44f238bb
PA
8631 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8632 b->loc->inserted = 1;
8633
0fb4aa4b
PA
8634 if (type == bp_static_tracepoint)
8635 {
d9b3f62e 8636 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8637 struct static_tracepoint_marker marker;
8638
983af33b 8639 if (strace_marker_p (b))
0fb4aa4b
PA
8640 {
8641 /* We already know the marker exists, otherwise, we
8642 wouldn't see a sal for it. */
8643 char *p = &addr_string[3];
8644 char *endp;
8645 char *marker_str;
0fb4aa4b 8646
e9cafbcc 8647 p = skip_spaces (p);
0fb4aa4b 8648
e9cafbcc 8649 endp = skip_to_space (p);
0fb4aa4b
PA
8650
8651 marker_str = savestring (p, endp - p);
d9b3f62e 8652 t->static_trace_marker_id = marker_str;
0fb4aa4b 8653
3e43a32a
MS
8654 printf_filtered (_("Probed static tracepoint "
8655 "marker \"%s\"\n"),
d9b3f62e 8656 t->static_trace_marker_id);
0fb4aa4b
PA
8657 }
8658 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8659 {
d9b3f62e 8660 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8661 release_static_tracepoint_marker (&marker);
8662
3e43a32a
MS
8663 printf_filtered (_("Probed static tracepoint "
8664 "marker \"%s\"\n"),
d9b3f62e 8665 t->static_trace_marker_id);
0fb4aa4b
PA
8666 }
8667 else
3e43a32a
MS
8668 warning (_("Couldn't determine the static "
8669 "tracepoint marker to probe"));
0fb4aa4b
PA
8670 }
8671
0d381245
VP
8672 loc = b->loc;
8673 }
8674 else
018d34a4 8675 {
39d61571 8676 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8677 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8678 loc->inserted = 1;
0d381245
VP
8679 }
8680
514f746b
AR
8681 if (bp_loc_is_permanent (loc))
8682 make_breakpoint_permanent (b);
8683
0d381245
VP
8684 if (b->cond_string)
8685 {
8686 char *arg = b->cond_string;
d32a6982 8687 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 8688 if (*arg)
588ae58c 8689 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8690 }
e7e0cddf
SS
8691
8692 /* Dynamic printf requires and uses additional arguments on the
8693 command line, otherwise it's an error. */
8694 if (type == bp_dprintf)
8695 {
8696 if (b->extra_string)
8697 update_dprintf_command_list (b);
8698 else
8699 error (_("Format string required"));
8700 }
8701 else if (b->extra_string)
588ae58c 8702 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8703 }
018d34a4 8704
56435ebe 8705 b->display_canonical = display_canonical;
018d34a4
VP
8706 if (addr_string)
8707 b->addr_string = addr_string;
8708 else
8709 /* addr_string has to be used or breakpoint_re_set will delete
8710 me. */
5af949e3
UW
8711 b->addr_string
8712 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 8713 b->filter = filter;
d9b3f62e 8714}
018d34a4 8715
d9b3f62e
PA
8716static void
8717create_breakpoint_sal (struct gdbarch *gdbarch,
8718 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8719 char *filter, char *cond_string,
e7e0cddf 8720 char *extra_string,
d9b3f62e
PA
8721 enum bptype type, enum bpdisp disposition,
8722 int thread, int task, int ignore_count,
c0a91b2b 8723 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8724 int enabled, int internal, unsigned flags,
8725 int display_canonical)
d9b3f62e
PA
8726{
8727 struct breakpoint *b;
8728 struct cleanup *old_chain;
8729
8730 if (is_tracepoint_type (type))
8731 {
8732 struct tracepoint *t;
8733
8734 t = XCNEW (struct tracepoint);
8735 b = &t->base;
8736 }
8737 else
8738 b = XNEW (struct breakpoint);
8739
8740 old_chain = make_cleanup (xfree, b);
8741
8742 init_breakpoint_sal (b, gdbarch,
8743 sals, addr_string,
e7e0cddf 8744 filter, cond_string, extra_string,
d9b3f62e
PA
8745 type, disposition,
8746 thread, task, ignore_count,
8747 ops, from_tty,
44f238bb
PA
8748 enabled, internal, flags,
8749 display_canonical);
d9b3f62e
PA
8750 discard_cleanups (old_chain);
8751
3ea46bff 8752 install_breakpoint (internal, b, 0);
018d34a4
VP
8753}
8754
8755/* Add SALS.nelts breakpoints to the breakpoint table. For each
8756 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8757 value. COND_STRING, if not NULL, specified the condition to be
8758 used for all breakpoints. Essentially the only case where
8759 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8760 function. In that case, it's still not possible to specify
8761 separate conditions for different overloaded functions, so
8762 we take just a single condition string.
8763
c3f6f71d 8764 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8765 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8766 array contents). If the function fails (error() is called), the
8767 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8768 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8769
8770static void
8cdf0e15 8771create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8772 struct linespec_result *canonical,
e7e0cddf 8773 char *cond_string, char *extra_string,
8cdf0e15
VP
8774 enum bptype type, enum bpdisp disposition,
8775 int thread, int task, int ignore_count,
c0a91b2b 8776 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8777 int enabled, int internal, unsigned flags)
c906108c 8778{
018d34a4 8779 int i;
f8eba3c6 8780 struct linespec_sals *lsal;
cc59ec59 8781
f8eba3c6
TT
8782 if (canonical->pre_expanded)
8783 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8784
8785 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 8786 {
f8eba3c6
TT
8787 /* Note that 'addr_string' can be NULL in the case of a plain
8788 'break', without arguments. */
8789 char *addr_string = (canonical->addr_string
8790 ? xstrdup (canonical->addr_string)
8791 : NULL);
8792 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8793 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 8794
f8eba3c6
TT
8795 make_cleanup (xfree, filter_string);
8796 create_breakpoint_sal (gdbarch, lsal->sals,
8797 addr_string,
8798 filter_string,
e7e0cddf
SS
8799 cond_string, extra_string,
8800 type, disposition,
84f4c1fe 8801 thread, task, ignore_count, ops,
44f238bb 8802 from_tty, enabled, internal, flags,
56435ebe 8803 canonical->special_display);
f8eba3c6 8804 discard_cleanups (inner);
c3f6f71d 8805 }
c3f6f71d 8806}
c906108c 8807
9998af43 8808/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 8809 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 8810 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
8811 address strings. ADDRESS points to the end of the SAL.
8812
8813 The array and the line spec strings are allocated on the heap, it is
8814 the caller's responsibility to free them. */
c906108c 8815
b9362cc7 8816static void
c3f6f71d 8817parse_breakpoint_sals (char **address,
58438ac1 8818 struct linespec_result *canonical)
c3f6f71d
JM
8819{
8820 char *addr_start = *address;
cc59ec59 8821
c3f6f71d 8822 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 8823 breakpoint. */
c3f6f71d
JM
8824 if ((*address) == NULL
8825 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 8826 {
1bfeeb0f
JL
8827 /* The last displayed codepoint, if it's valid, is our default breakpoint
8828 address. */
8829 if (last_displayed_sal_is_valid ())
c906108c 8830 {
f8eba3c6 8831 struct linespec_sals lsal;
c3f6f71d 8832 struct symtab_and_line sal;
cc59ec59 8833
4a64f543 8834 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 8835 lsal.sals.sals = (struct symtab_and_line *)
c906108c 8836 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
8837
8838 /* Set sal's pspace, pc, symtab, and line to the values
8839 corresponding to the last call to print_frame_info. */
8840 get_last_displayed_sal (&sal);
8841 sal.section = find_pc_overlay (sal.pc);
00903456 8842
4a64f543 8843 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
8844 where PC is the last displayed codepoint's address. So
8845 make sure to set sal.explicit_pc to prevent GDB from
8846 trying to expand the list of sals to include all other
8847 instances with the same symtab and line. */
00903456
JK
8848 sal.explicit_pc = 1;
8849
f8eba3c6
TT
8850 lsal.sals.sals[0] = sal;
8851 lsal.sals.nelts = 1;
8852 lsal.canonical = NULL;
8853
8854 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
8855 }
8856 else
8a3fe4f8 8857 error (_("No default breakpoint address now."));
c906108c
SS
8858 }
8859 else
8860 {
cc80f267
JK
8861 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8862
c906108c 8863 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
8864 current_source_symtab (which is decode_line_1's default).
8865 This should produce the results we want almost all of the
cc80f267
JK
8866 time while leaving default_breakpoint_* alone.
8867
8868 ObjC: However, don't match an Objective-C method name which
8869 may have a '+' or '-' succeeded by a '['. */
8870 if (last_displayed_sal_is_valid ()
8871 && (!cursal.symtab
8872 || ((strchr ("+-", (*address)[0]) != NULL)
8873 && ((*address)[1] != '['))))
f8eba3c6
TT
8874 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8875 get_last_displayed_symtab (),
8876 get_last_displayed_line (),
8877 canonical, NULL, NULL);
c906108c 8878 else
f8eba3c6 8879 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 8880 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 8881 }
c3f6f71d 8882}
c906108c 8883
c906108c 8884
c3f6f71d 8885/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8886 inserted as a breakpoint. If it can't throw an error. */
c906108c 8887
b9362cc7 8888static void
23e7acfb 8889breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
8890{
8891 int i;
cc59ec59 8892
c3f6f71d 8893 for (i = 0; i < sals->nelts; i++)
ee53e872 8894 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
8895}
8896
7a697b8d
SS
8897/* Fast tracepoints may have restrictions on valid locations. For
8898 instance, a fast tracepoint using a jump instead of a trap will
8899 likely have to overwrite more bytes than a trap would, and so can
8900 only be placed where the instruction is longer than the jump, or a
8901 multi-instruction sequence does not have a jump into the middle of
8902 it, etc. */
8903
8904static void
8905check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8906 struct symtabs_and_lines *sals)
8907{
8908 int i, rslt;
8909 struct symtab_and_line *sal;
8910 char *msg;
8911 struct cleanup *old_chain;
8912
8913 for (i = 0; i < sals->nelts; i++)
8914 {
f8eba3c6
TT
8915 struct gdbarch *sarch;
8916
7a697b8d
SS
8917 sal = &sals->sals[i];
8918
f8eba3c6
TT
8919 sarch = get_sal_arch (*sal);
8920 /* We fall back to GDBARCH if there is no architecture
8921 associated with SAL. */
8922 if (sarch == NULL)
8923 sarch = gdbarch;
8924 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
8925 NULL, &msg);
8926 old_chain = make_cleanup (xfree, msg);
8927
8928 if (!rslt)
8929 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 8930 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
8931
8932 do_cleanups (old_chain);
8933 }
8934}
8935
018d34a4
VP
8936/* Given TOK, a string specification of condition and thread, as
8937 accepted by the 'break' command, extract the condition
8938 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8939 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8940 If no condition is found, *COND_STRING is set to NULL.
8941 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8942
8943static void
8944find_condition_and_thread (char *tok, CORE_ADDR pc,
e7e0cddf
SS
8945 char **cond_string, int *thread, int *task,
8946 char **rest)
018d34a4
VP
8947{
8948 *cond_string = NULL;
8949 *thread = -1;
8950 while (tok && *tok)
8951 {
8952 char *end_tok;
8953 int toklen;
8954 char *cond_start = NULL;
8955 char *cond_end = NULL;
cc59ec59 8956
e9cafbcc 8957 tok = skip_spaces (tok);
e7e0cddf
SS
8958
8959 if ((*tok == '"' || *tok == ',') && rest)
8960 {
8961 *rest = savestring (tok, strlen (tok));
8962 return;
8963 }
8964
e9cafbcc 8965 end_tok = skip_to_space (tok);
d634f2de 8966
018d34a4 8967 toklen = end_tok - tok;
d634f2de 8968
018d34a4
VP
8969 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8970 {
f7545552
TT
8971 struct expression *expr;
8972
018d34a4 8973 tok = cond_start = end_tok + 1;
f7545552
TT
8974 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
8975 xfree (expr);
018d34a4 8976 cond_end = tok;
d634f2de 8977 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
8978 }
8979 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8980 {
8981 char *tmptok;
d634f2de 8982
018d34a4
VP
8983 tok = end_tok + 1;
8984 tmptok = tok;
8985 *thread = strtol (tok, &tok, 0);
8986 if (tok == tmptok)
8987 error (_("Junk after thread keyword."));
8988 if (!valid_thread_id (*thread))
8989 error (_("Unknown thread %d."), *thread);
8990 }
4a306c9a
JB
8991 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8992 {
8993 char *tmptok;
8994
8995 tok = end_tok + 1;
8996 tmptok = tok;
8997 *task = strtol (tok, &tok, 0);
8998 if (tok == tmptok)
8999 error (_("Junk after task keyword."));
9000 if (!valid_task_id (*task))
b6199126 9001 error (_("Unknown task %d."), *task);
4a306c9a 9002 }
e7e0cddf
SS
9003 else if (rest)
9004 {
9005 *rest = savestring (tok, strlen (tok));
ccab2054 9006 return;
e7e0cddf 9007 }
018d34a4
VP
9008 else
9009 error (_("Junk at end of arguments."));
9010 }
9011}
9012
0fb4aa4b
PA
9013/* Decode a static tracepoint marker spec. */
9014
9015static struct symtabs_and_lines
9016decode_static_tracepoint_spec (char **arg_p)
9017{
9018 VEC(static_tracepoint_marker_p) *markers = NULL;
9019 struct symtabs_and_lines sals;
0fb4aa4b
PA
9020 struct cleanup *old_chain;
9021 char *p = &(*arg_p)[3];
9022 char *endp;
9023 char *marker_str;
9024 int i;
9025
e9cafbcc 9026 p = skip_spaces (p);
0fb4aa4b 9027
e9cafbcc 9028 endp = skip_to_space (p);
0fb4aa4b
PA
9029
9030 marker_str = savestring (p, endp - p);
9031 old_chain = make_cleanup (xfree, marker_str);
9032
9033 markers = target_static_tracepoint_markers_by_strid (marker_str);
9034 if (VEC_empty(static_tracepoint_marker_p, markers))
9035 error (_("No known static tracepoint marker named %s"), marker_str);
9036
9037 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9038 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9039
9040 for (i = 0; i < sals.nelts; i++)
9041 {
9042 struct static_tracepoint_marker *marker;
9043
9044 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9045
9046 init_sal (&sals.sals[i]);
9047
9048 sals.sals[i] = find_pc_line (marker->address, 0);
9049 sals.sals[i].pc = marker->address;
9050
9051 release_static_tracepoint_marker (marker);
9052 }
9053
9054 do_cleanups (old_chain);
9055
9056 *arg_p = endp;
9057 return sals;
9058}
9059
fd9b8c24
PA
9060/* Set a breakpoint. This function is shared between CLI and MI
9061 functions for setting a breakpoint. This function has two major
9062 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9063 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
9064 breakpoint location, address and thread. Otherwise, ARG is just
9065 the location of breakpoint, with condition and thread specified by
9066 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9067 the breakpoint number will be allocated from the internal
9068 breakpoint count. Returns true if any breakpoint was created;
9069 false otherwise. */
0101ce28 9070
8cdf0e15
VP
9071int
9072create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9073 char *arg, char *cond_string,
9074 int thread, char *extra_string,
8cdf0e15 9075 int parse_condition_and_thread,
0fb4aa4b 9076 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9077 int ignore_count,
9078 enum auto_boolean pending_break_support,
c0a91b2b 9079 const struct breakpoint_ops *ops,
44f238bb
PA
9080 int from_tty, int enabled, int internal,
9081 unsigned flags)
c3f6f71d 9082{
b78a6381 9083 volatile struct gdb_exception e;
f8eba3c6 9084 char *copy_arg = NULL;
c3f6f71d 9085 char *addr_start = arg;
7efd8fc2 9086 struct linespec_result canonical;
c3f6f71d 9087 struct cleanup *old_chain;
80c99de1 9088 struct cleanup *bkpt_chain = NULL;
0101ce28 9089 int pending = 0;
4a306c9a 9090 int task = 0;
86b17b60 9091 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9092
348d480f
PA
9093 gdb_assert (ops != NULL);
9094
7efd8fc2 9095 init_linespec_result (&canonical);
c3f6f71d 9096
b78a6381
TT
9097 TRY_CATCH (e, RETURN_MASK_ALL)
9098 {
983af33b
SDJ
9099 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9100 addr_start, &copy_arg);
b78a6381 9101 }
0101ce28
JJ
9102
9103 /* If caller is interested in rc value from parse, set value. */
05ff989b 9104 switch (e.reason)
0101ce28 9105 {
983af33b
SDJ
9106 case GDB_NO_ERROR:
9107 if (VEC_empty (linespec_sals, canonical.sals))
9108 return 0;
9109 break;
05ff989b
AC
9110 case RETURN_ERROR:
9111 switch (e.error)
0101ce28 9112 {
05ff989b 9113 case NOT_FOUND_ERROR:
0101ce28 9114
05ff989b
AC
9115 /* If pending breakpoint support is turned off, throw
9116 error. */
fa8d40ab
JJ
9117
9118 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9119 throw_exception (e);
9120
9121 exception_print (gdb_stderr, e);
fa8d40ab 9122
05ff989b
AC
9123 /* If pending breakpoint support is auto query and the user
9124 selects no, then simply return the error code. */
059fb39f 9125 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9126 && !nquery (_("Make %s pending on future shared library load? "),
9127 bptype_string (type_wanted)))
fd9b8c24 9128 return 0;
fa8d40ab 9129
05ff989b
AC
9130 /* At this point, either the user was queried about setting
9131 a pending breakpoint and selected yes, or pending
9132 breakpoint behavior is on and thus a pending breakpoint
9133 is defaulted on behalf of the user. */
f8eba3c6
TT
9134 {
9135 struct linespec_sals lsal;
9136
9137 copy_arg = xstrdup (addr_start);
9138 lsal.canonical = xstrdup (copy_arg);
9139 lsal.sals.nelts = 1;
9140 lsal.sals.sals = XNEW (struct symtab_and_line);
9141 init_sal (&lsal.sals.sals[0]);
9142 pending = 1;
9143 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9144 }
05ff989b
AC
9145 break;
9146 default:
98deb0da 9147 throw_exception (e);
0101ce28 9148 }
2abae994 9149 break;
05ff989b 9150 default:
983af33b 9151 throw_exception (e);
0101ce28 9152 }
c3f6f71d 9153
4a64f543 9154 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9155 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9156
c3f6f71d
JM
9157 /* ----------------------------- SNIP -----------------------------
9158 Anything added to the cleanup chain beyond this point is assumed
9159 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9160 then the memory is not reclaimed. */
9161 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9162
c3f6f71d
JM
9163 /* Resolve all line numbers to PC's and verify that the addresses
9164 are ok for the target. */
0101ce28 9165 if (!pending)
f8eba3c6
TT
9166 {
9167 int ix;
9168 struct linespec_sals *iter;
9169
9170 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9171 breakpoint_sals_to_pc (&iter->sals);
9172 }
c3f6f71d 9173
7a697b8d 9174 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9175 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9176 {
9177 int ix;
9178 struct linespec_sals *iter;
9179
9180 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9181 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9182 }
7a697b8d 9183
c3f6f71d
JM
9184 /* Verify that condition can be parsed, before setting any
9185 breakpoints. Allocate a separate condition expression for each
4a64f543 9186 breakpoint. */
0101ce28 9187 if (!pending)
c3f6f71d 9188 {
f8eba3c6
TT
9189 struct linespec_sals *lsal;
9190
9191 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9192
2f069f6f 9193 if (parse_condition_and_thread)
72b2ff0e 9194 {
e7e0cddf 9195 char *rest;
72b2ff0e
VP
9196 /* Here we only parse 'arg' to separate condition
9197 from thread number, so parsing in context of first
9198 sal is OK. When setting the breakpoint we'll
9199 re-parse it in context of each sal. */
9200 cond_string = NULL;
9201 thread = -1;
e7e0cddf 9202 rest = NULL;
f8eba3c6 9203 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
e7e0cddf 9204 &thread, &task, &rest);
72b2ff0e
VP
9205 if (cond_string)
9206 make_cleanup (xfree, cond_string);
e7e0cddf
SS
9207 if (rest)
9208 make_cleanup (xfree, rest);
9209 if (rest)
9210 extra_string = rest;
72b2ff0e 9211 }
2f069f6f 9212 else
72b2ff0e
VP
9213 {
9214 /* Create a private copy of condition string. */
9215 if (cond_string)
9216 {
9217 cond_string = xstrdup (cond_string);
9218 make_cleanup (xfree, cond_string);
9219 }
e7e0cddf
SS
9220 /* Create a private copy of any extra string. */
9221 if (extra_string)
9222 {
9223 extra_string = xstrdup (extra_string);
9224 make_cleanup (xfree, extra_string);
9225 }
72b2ff0e 9226 }
0fb4aa4b 9227
983af33b 9228 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
e7e0cddf 9229 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9230 tempflag ? disp_del : disp_donttouch,
9231 thread, task, ignore_count, ops,
44f238bb 9232 from_tty, enabled, internal, flags);
c906108c 9233 }
0101ce28
JJ
9234 else
9235 {
0101ce28
JJ
9236 struct breakpoint *b;
9237
0101ce28
JJ
9238 make_cleanup (xfree, copy_arg);
9239
bfccc43c
YQ
9240 if (is_tracepoint_type (type_wanted))
9241 {
9242 struct tracepoint *t;
9243
9244 t = XCNEW (struct tracepoint);
9245 b = &t->base;
9246 }
9247 else
9248 b = XNEW (struct breakpoint);
9249
9250 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9251
f8eba3c6 9252 b->addr_string = copy_arg;
72b2ff0e 9253 b->cond_string = NULL;
e7e0cddf 9254 b->extra_string = NULL;
0101ce28 9255 b->ignore_count = ignore_count;
0101ce28 9256 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9257 b->condition_not_parsed = 1;
41447f92 9258 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9259 if ((type_wanted != bp_breakpoint
9260 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9261 b->pspace = current_program_space;
8bea4e01 9262
bfccc43c 9263 install_breakpoint (internal, b, 0);
0101ce28
JJ
9264 }
9265
f8eba3c6 9266 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9267 {
3e43a32a
MS
9268 warning (_("Multiple breakpoints were set.\nUse the "
9269 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9270 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9271 }
9272
80c99de1
PA
9273 /* That's it. Discard the cleanups for data inserted into the
9274 breakpoint. */
9275 discard_cleanups (bkpt_chain);
9276 /* But cleanup everything else. */
c3f6f71d 9277 do_cleanups (old_chain);
217dc9e2 9278
80c99de1 9279 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9280 update_global_location_list (1);
fd9b8c24
PA
9281
9282 return 1;
c3f6f71d 9283}
c906108c 9284
348d480f 9285/* Set a breakpoint.
72b2ff0e
VP
9286 ARG is a string describing breakpoint address,
9287 condition, and thread.
9288 FLAG specifies if a breakpoint is hardware on,
9289 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9290 and BP_TEMPFLAG. */
348d480f 9291
98deb0da 9292static void
72b2ff0e 9293break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9294{
72b2ff0e 9295 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9296 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9297 ? bp_hardware_breakpoint
9298 : bp_breakpoint);
55aa24fb
SDJ
9299 struct breakpoint_ops *ops;
9300 const char *arg_cp = arg;
9301
9302 /* Matching breakpoints on probes. */
9303 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9304 ops = &bkpt_probe_breakpoint_ops;
9305 else
9306 ops = &bkpt_breakpoint_ops;
c3f6f71d 9307
8cdf0e15
VP
9308 create_breakpoint (get_current_arch (),
9309 arg,
e7e0cddf 9310 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9311 tempflag, type_wanted,
8cdf0e15
VP
9312 0 /* Ignore count */,
9313 pending_break_support,
55aa24fb 9314 ops,
8cdf0e15 9315 from_tty,
84f4c1fe 9316 1 /* enabled */,
44f238bb
PA
9317 0 /* internal */,
9318 0);
c906108c
SS
9319}
9320
c906108c
SS
9321/* Helper function for break_command_1 and disassemble_command. */
9322
9323void
fba45db2 9324resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9325{
9326 CORE_ADDR pc;
9327
9328 if (sal->pc == 0 && sal->symtab != NULL)
9329 {
9330 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9331 error (_("No line %d in file \"%s\"."),
c906108c
SS
9332 sal->line, sal->symtab->filename);
9333 sal->pc = pc;
6a048695 9334
4a64f543
MS
9335 /* If this SAL corresponds to a breakpoint inserted using a line
9336 number, then skip the function prologue if necessary. */
6a048695 9337 if (sal->explicit_line)
059acae7 9338 skip_prologue_sal (sal);
c906108c
SS
9339 }
9340
9341 if (sal->section == 0 && sal->symtab != NULL)
9342 {
9343 struct blockvector *bv;
c5aa993b
JM
9344 struct block *b;
9345 struct symbol *sym;
c906108c 9346
801e3a5b 9347 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9348 if (bv != NULL)
9349 {
7f0df278 9350 sym = block_linkage_function (b);
c906108c
SS
9351 if (sym != NULL)
9352 {
9353 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 9354 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
9355 }
9356 else
9357 {
4a64f543
MS
9358 /* It really is worthwhile to have the section, so we'll
9359 just have to look harder. This case can be executed
9360 if we have line numbers but no functions (as can
9361 happen in assembly source). */
c906108c 9362
c5aa993b 9363 struct minimal_symbol *msym;
6c95b8df
PA
9364 struct cleanup *old_chain = save_current_space_and_thread ();
9365
9366 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9367
9368 msym = lookup_minimal_symbol_by_pc (sal->pc);
9369 if (msym)
714835d5 9370 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
9371
9372 do_cleanups (old_chain);
c906108c
SS
9373 }
9374 }
9375 }
9376}
9377
9378void
fba45db2 9379break_command (char *arg, int from_tty)
c906108c 9380{
db107f19 9381 break_command_1 (arg, 0, from_tty);
c906108c
SS
9382}
9383
c906108c 9384void
fba45db2 9385tbreak_command (char *arg, int from_tty)
c906108c 9386{
db107f19 9387 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9388}
9389
c906108c 9390static void
fba45db2 9391hbreak_command (char *arg, int from_tty)
c906108c 9392{
db107f19 9393 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9394}
9395
9396static void
fba45db2 9397thbreak_command (char *arg, int from_tty)
c906108c 9398{
db107f19 9399 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9400}
9401
9402static void
fba45db2 9403stop_command (char *arg, int from_tty)
c906108c 9404{
a3f17187 9405 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9406Usage: stop in <function | address>\n\
a3f17187 9407 stop at <line>\n"));
c906108c
SS
9408}
9409
9410static void
fba45db2 9411stopin_command (char *arg, int from_tty)
c906108c
SS
9412{
9413 int badInput = 0;
9414
c5aa993b 9415 if (arg == (char *) NULL)
c906108c
SS
9416 badInput = 1;
9417 else if (*arg != '*')
9418 {
9419 char *argptr = arg;
9420 int hasColon = 0;
9421
4a64f543 9422 /* Look for a ':'. If this is a line number specification, then
53a5351d 9423 say it is bad, otherwise, it should be an address or
4a64f543 9424 function/method name. */
c906108c 9425 while (*argptr && !hasColon)
c5aa993b
JM
9426 {
9427 hasColon = (*argptr == ':');
9428 argptr++;
9429 }
c906108c
SS
9430
9431 if (hasColon)
c5aa993b 9432 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9433 else
c5aa993b 9434 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9435 }
9436
9437 if (badInput)
a3f17187 9438 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9439 else
db107f19 9440 break_command_1 (arg, 0, from_tty);
c906108c
SS
9441}
9442
9443static void
fba45db2 9444stopat_command (char *arg, int from_tty)
c906108c
SS
9445{
9446 int badInput = 0;
9447
c5aa993b 9448 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9449 badInput = 1;
9450 else
9451 {
9452 char *argptr = arg;
9453 int hasColon = 0;
9454
4a64f543
MS
9455 /* Look for a ':'. If there is a '::' then get out, otherwise
9456 it is probably a line number. */
c906108c 9457 while (*argptr && !hasColon)
c5aa993b
JM
9458 {
9459 hasColon = (*argptr == ':');
9460 argptr++;
9461 }
c906108c
SS
9462
9463 if (hasColon)
c5aa993b 9464 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9465 else
c5aa993b 9466 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9467 }
9468
9469 if (badInput)
a3f17187 9470 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9471 else
db107f19 9472 break_command_1 (arg, 0, from_tty);
c906108c
SS
9473}
9474
e7e0cddf
SS
9475void dprintf_command (char *arg, int from_tty);
9476
9477/* The dynamic printf command is mostly like a regular breakpoint, but
9478 with a prewired command list consisting of a single output command,
9479 built from extra arguments supplied on the dprintf command
9480 line. */
9481
9482void
9483dprintf_command (char *arg, int from_tty)
9484{
9485 create_breakpoint (get_current_arch (),
9486 arg,
9487 NULL, 0, NULL, 1 /* parse arg */,
9488 0, bp_dprintf,
9489 0 /* Ignore count */,
9490 pending_break_support,
9491 &dprintf_breakpoint_ops,
9492 from_tty,
9493 1 /* enabled */,
9494 0 /* internal */,
9495 0);
9496}
9497
f1310107
TJB
9498/* Implement the "breakpoint_hit" breakpoint_ops method for
9499 ranged breakpoints. */
9500
9501static int
9502breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9503 struct address_space *aspace,
09ac7c10
TT
9504 CORE_ADDR bp_addr,
9505 const struct target_waitstatus *ws)
f1310107 9506{
09ac7c10 9507 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9508 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9509 return 0;
9510
f1310107
TJB
9511 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9512 bl->length, aspace, bp_addr);
9513}
9514
9515/* Implement the "resources_needed" breakpoint_ops method for
9516 ranged breakpoints. */
9517
9518static int
9519resources_needed_ranged_breakpoint (const struct bp_location *bl)
9520{
9521 return target_ranged_break_num_registers ();
9522}
9523
9524/* Implement the "print_it" breakpoint_ops method for
9525 ranged breakpoints. */
9526
9527static enum print_stop_action
348d480f 9528print_it_ranged_breakpoint (bpstat bs)
f1310107 9529{
348d480f 9530 struct breakpoint *b = bs->breakpoint_at;
f1310107 9531 struct bp_location *bl = b->loc;
79a45e25 9532 struct ui_out *uiout = current_uiout;
f1310107
TJB
9533
9534 gdb_assert (b->type == bp_hardware_breakpoint);
9535
9536 /* Ranged breakpoints have only one location. */
9537 gdb_assert (bl && bl->next == NULL);
9538
9539 annotate_breakpoint (b->number);
9540 if (b->disposition == disp_del)
9541 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9542 else
9543 ui_out_text (uiout, "\nRanged breakpoint ");
9544 if (ui_out_is_mi_like_p (uiout))
9545 {
9546 ui_out_field_string (uiout, "reason",
9547 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9548 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9549 }
9550 ui_out_field_int (uiout, "bkptno", b->number);
9551 ui_out_text (uiout, ", ");
9552
9553 return PRINT_SRC_AND_LOC;
9554}
9555
9556/* Implement the "print_one" breakpoint_ops method for
9557 ranged breakpoints. */
9558
9559static void
9560print_one_ranged_breakpoint (struct breakpoint *b,
9561 struct bp_location **last_loc)
9562{
9563 struct bp_location *bl = b->loc;
9564 struct value_print_options opts;
79a45e25 9565 struct ui_out *uiout = current_uiout;
f1310107
TJB
9566
9567 /* Ranged breakpoints have only one location. */
9568 gdb_assert (bl && bl->next == NULL);
9569
9570 get_user_print_options (&opts);
9571
9572 if (opts.addressprint)
9573 /* We don't print the address range here, it will be printed later
9574 by print_one_detail_ranged_breakpoint. */
9575 ui_out_field_skip (uiout, "addr");
9576 annotate_field (5);
9577 print_breakpoint_location (b, bl);
9578 *last_loc = bl;
9579}
9580
9581/* Implement the "print_one_detail" breakpoint_ops method for
9582 ranged breakpoints. */
9583
9584static void
9585print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9586 struct ui_out *uiout)
9587{
9588 CORE_ADDR address_start, address_end;
9589 struct bp_location *bl = b->loc;
f99d8bf4
PA
9590 struct ui_file *stb = mem_fileopen ();
9591 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
9592
9593 gdb_assert (bl);
9594
9595 address_start = bl->address;
9596 address_end = address_start + bl->length - 1;
9597
9598 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 9599 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
9600 print_core_address (bl->gdbarch, address_start),
9601 print_core_address (bl->gdbarch, address_end));
9602 ui_out_field_stream (uiout, "addr", stb);
9603 ui_out_text (uiout, "\n");
9604
9605 do_cleanups (cleanup);
9606}
9607
9608/* Implement the "print_mention" breakpoint_ops method for
9609 ranged breakpoints. */
9610
9611static void
9612print_mention_ranged_breakpoint (struct breakpoint *b)
9613{
9614 struct bp_location *bl = b->loc;
79a45e25 9615 struct ui_out *uiout = current_uiout;
f1310107
TJB
9616
9617 gdb_assert (bl);
9618 gdb_assert (b->type == bp_hardware_breakpoint);
9619
9620 if (ui_out_is_mi_like_p (uiout))
9621 return;
9622
9623 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9624 b->number, paddress (bl->gdbarch, bl->address),
9625 paddress (bl->gdbarch, bl->address + bl->length - 1));
9626}
9627
9628/* Implement the "print_recreate" breakpoint_ops method for
9629 ranged breakpoints. */
9630
9631static void
9632print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9633{
9634 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9635 b->addr_string_range_end);
d9b3f62e 9636 print_recreate_thread (b, fp);
f1310107
TJB
9637}
9638
9639/* The breakpoint_ops structure to be used in ranged breakpoints. */
9640
2060206e 9641static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9642
9643/* Find the address where the end of the breakpoint range should be
9644 placed, given the SAL of the end of the range. This is so that if
9645 the user provides a line number, the end of the range is set to the
9646 last instruction of the given line. */
9647
9648static CORE_ADDR
9649find_breakpoint_range_end (struct symtab_and_line sal)
9650{
9651 CORE_ADDR end;
9652
9653 /* If the user provided a PC value, use it. Otherwise,
9654 find the address of the end of the given location. */
9655 if (sal.explicit_pc)
9656 end = sal.pc;
9657 else
9658 {
9659 int ret;
9660 CORE_ADDR start;
9661
9662 ret = find_line_pc_range (sal, &start, &end);
9663 if (!ret)
9664 error (_("Could not find location of the end of the range."));
9665
9666 /* find_line_pc_range returns the start of the next line. */
9667 end--;
9668 }
9669
9670 return end;
9671}
9672
9673/* Implement the "break-range" CLI command. */
9674
9675static void
9676break_range_command (char *arg, int from_tty)
9677{
9678 char *arg_start, *addr_string_start, *addr_string_end;
9679 struct linespec_result canonical_start, canonical_end;
9680 int bp_count, can_use_bp, length;
9681 CORE_ADDR end;
9682 struct breakpoint *b;
9683 struct symtab_and_line sal_start, sal_end;
f1310107 9684 struct cleanup *cleanup_bkpt;
f8eba3c6 9685 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
9686
9687 /* We don't support software ranged breakpoints. */
9688 if (target_ranged_break_num_registers () < 0)
9689 error (_("This target does not support hardware ranged breakpoints."));
9690
9691 bp_count = hw_breakpoint_used_count ();
9692 bp_count += target_ranged_break_num_registers ();
9693 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9694 bp_count, 0);
9695 if (can_use_bp < 0)
9696 error (_("Hardware breakpoints used exceeds limit."));
9697
f8eba3c6 9698 arg = skip_spaces (arg);
f1310107
TJB
9699 if (arg == NULL || arg[0] == '\0')
9700 error(_("No address range specified."));
9701
f1310107
TJB
9702 init_linespec_result (&canonical_start);
9703
f8eba3c6
TT
9704 arg_start = arg;
9705 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 9706
f8eba3c6 9707 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
9708
9709 if (arg[0] != ',')
9710 error (_("Too few arguments."));
f8eba3c6 9711 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 9712 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
9713
9714 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9715
9716 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9717 || lsal_start->sals.nelts != 1)
f1310107
TJB
9718 error (_("Cannot create a ranged breakpoint with multiple locations."));
9719
f8eba3c6
TT
9720 sal_start = lsal_start->sals.sals[0];
9721 addr_string_start = savestring (arg_start, arg - arg_start);
9722 make_cleanup (xfree, addr_string_start);
f1310107
TJB
9723
9724 arg++; /* Skip the comma. */
f8eba3c6 9725 arg = skip_spaces (arg);
f1310107
TJB
9726
9727 /* Parse the end location. */
9728
f1310107
TJB
9729 init_linespec_result (&canonical_end);
9730 arg_start = arg;
9731
f8eba3c6 9732 /* We call decode_line_full directly here instead of using
f1310107
TJB
9733 parse_breakpoint_sals because we need to specify the start location's
9734 symtab and line as the default symtab and line for the end of the
9735 range. This makes it possible to have ranges like "foo.c:27, +14",
9736 where +14 means 14 lines from the start location. */
f8eba3c6
TT
9737 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9738 sal_start.symtab, sal_start.line,
9739 &canonical_end, NULL, NULL);
9740
9741 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 9742
f8eba3c6 9743 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 9744 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
9745
9746 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9747 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9748 || lsal_end->sals.nelts != 1)
f1310107
TJB
9749 error (_("Cannot create a ranged breakpoint with multiple locations."));
9750
f8eba3c6
TT
9751 sal_end = lsal_end->sals.sals[0];
9752 addr_string_end = savestring (arg_start, arg - arg_start);
9753 make_cleanup (xfree, addr_string_end);
f1310107
TJB
9754
9755 end = find_breakpoint_range_end (sal_end);
9756 if (sal_start.pc > end)
177b42fe 9757 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9758
9759 length = end - sal_start.pc + 1;
9760 if (length < 0)
9761 /* Length overflowed. */
9762 error (_("Address range too large."));
9763 else if (length == 1)
9764 {
9765 /* This range is simple enough to be handled by
9766 the `hbreak' command. */
9767 hbreak_command (addr_string_start, 1);
9768
9769 do_cleanups (cleanup_bkpt);
9770
9771 return;
9772 }
9773
9774 /* Now set up the breakpoint. */
9775 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9776 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9777 set_breakpoint_count (breakpoint_count + 1);
9778 b->number = breakpoint_count;
9779 b->disposition = disp_donttouch;
f8eba3c6
TT
9780 b->addr_string = xstrdup (addr_string_start);
9781 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
9782 b->loc->length = length;
9783
f8eba3c6 9784 do_cleanups (cleanup_bkpt);
f1310107
TJB
9785
9786 mention (b);
8d3788bd 9787 observer_notify_breakpoint_created (b);
f1310107
TJB
9788 update_global_location_list (1);
9789}
9790
4a64f543
MS
9791/* Return non-zero if EXP is verified as constant. Returned zero
9792 means EXP is variable. Also the constant detection may fail for
9793 some constant expressions and in such case still falsely return
9794 zero. */
2e6e3d9c 9795
65d79d4b
SDJ
9796static int
9797watchpoint_exp_is_const (const struct expression *exp)
9798{
9799 int i = exp->nelts;
9800
9801 while (i > 0)
9802 {
9803 int oplenp, argsp;
9804
9805 /* We are only interested in the descriptor of each element. */
9806 operator_length (exp, i, &oplenp, &argsp);
9807 i -= oplenp;
9808
9809 switch (exp->elts[i].opcode)
9810 {
9811 case BINOP_ADD:
9812 case BINOP_SUB:
9813 case BINOP_MUL:
9814 case BINOP_DIV:
9815 case BINOP_REM:
9816 case BINOP_MOD:
9817 case BINOP_LSH:
9818 case BINOP_RSH:
9819 case BINOP_LOGICAL_AND:
9820 case BINOP_LOGICAL_OR:
9821 case BINOP_BITWISE_AND:
9822 case BINOP_BITWISE_IOR:
9823 case BINOP_BITWISE_XOR:
9824 case BINOP_EQUAL:
9825 case BINOP_NOTEQUAL:
9826 case BINOP_LESS:
9827 case BINOP_GTR:
9828 case BINOP_LEQ:
9829 case BINOP_GEQ:
9830 case BINOP_REPEAT:
9831 case BINOP_COMMA:
9832 case BINOP_EXP:
9833 case BINOP_MIN:
9834 case BINOP_MAX:
9835 case BINOP_INTDIV:
9836 case BINOP_CONCAT:
9837 case BINOP_IN:
9838 case BINOP_RANGE:
9839 case TERNOP_COND:
9840 case TERNOP_SLICE:
9841 case TERNOP_SLICE_COUNT:
9842
9843 case OP_LONG:
9844 case OP_DOUBLE:
9845 case OP_DECFLOAT:
9846 case OP_LAST:
9847 case OP_COMPLEX:
9848 case OP_STRING:
9849 case OP_BITSTRING:
9850 case OP_ARRAY:
9851 case OP_TYPE:
9852 case OP_NAME:
9853 case OP_OBJC_NSSTRING:
9854
9855 case UNOP_NEG:
9856 case UNOP_LOGICAL_NOT:
9857 case UNOP_COMPLEMENT:
9858 case UNOP_ADDR:
9859 case UNOP_HIGH:
aeaa2474 9860 case UNOP_CAST:
4a64f543
MS
9861 /* Unary, binary and ternary operators: We have to check
9862 their operands. If they are constant, then so is the
9863 result of that operation. For instance, if A and B are
9864 determined to be constants, then so is "A + B".
9865
9866 UNOP_IND is one exception to the rule above, because the
9867 value of *ADDR is not necessarily a constant, even when
9868 ADDR is. */
65d79d4b
SDJ
9869 break;
9870
9871 case OP_VAR_VALUE:
9872 /* Check whether the associated symbol is a constant.
4a64f543 9873
65d79d4b 9874 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9875 possible that a buggy compiler could mark a variable as
9876 constant even when it is not, and TYPE_CONST would return
9877 true in this case, while SYMBOL_CLASS wouldn't.
9878
9879 We also have to check for function symbols because they
9880 are always constant. */
65d79d4b
SDJ
9881 {
9882 struct symbol *s = exp->elts[i + 2].symbol;
9883
9884 if (SYMBOL_CLASS (s) != LOC_BLOCK
9885 && SYMBOL_CLASS (s) != LOC_CONST
9886 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9887 return 0;
9888 break;
9889 }
9890
9891 /* The default action is to return 0 because we are using
9892 the optimistic approach here: If we don't know something,
9893 then it is not a constant. */
9894 default:
9895 return 0;
9896 }
9897 }
9898
9899 return 1;
9900}
9901
3a5c3e22
PA
9902/* Implement the "dtor" breakpoint_ops method for watchpoints. */
9903
9904static void
9905dtor_watchpoint (struct breakpoint *self)
9906{
9907 struct watchpoint *w = (struct watchpoint *) self;
9908
9909 xfree (w->cond_exp);
9910 xfree (w->exp);
9911 xfree (w->exp_string);
9912 xfree (w->exp_string_reparse);
9913 value_free (w->val);
9914
9915 base_breakpoint_ops.dtor (self);
9916}
9917
348d480f
PA
9918/* Implement the "re_set" breakpoint_ops method for watchpoints. */
9919
9920static void
9921re_set_watchpoint (struct breakpoint *b)
9922{
3a5c3e22
PA
9923 struct watchpoint *w = (struct watchpoint *) b;
9924
348d480f
PA
9925 /* Watchpoint can be either on expression using entirely global
9926 variables, or it can be on local variables.
9927
9928 Watchpoints of the first kind are never auto-deleted, and even
9929 persist across program restarts. Since they can use variables
9930 from shared libraries, we need to reparse expression as libraries
9931 are loaded and unloaded.
9932
9933 Watchpoints on local variables can also change meaning as result
9934 of solib event. For example, if a watchpoint uses both a local
9935 and a global variables in expression, it's a local watchpoint,
9936 but unloading of a shared library will make the expression
9937 invalid. This is not a very common use case, but we still
9938 re-evaluate expression, to avoid surprises to the user.
9939
9940 Note that for local watchpoints, we re-evaluate it only if
9941 watchpoints frame id is still valid. If it's not, it means the
9942 watchpoint is out of scope and will be deleted soon. In fact,
9943 I'm not sure we'll ever be called in this case.
9944
9945 If a local watchpoint's frame id is still valid, then
3a5c3e22 9946 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 9947
3a5c3e22
PA
9948 Don't do anything about disabled watchpoints, since they will be
9949 reevaluated again when enabled. */
9950 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
9951}
9952
77b06cd7
TJB
9953/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
9954
9955static int
9956insert_watchpoint (struct bp_location *bl)
9957{
3a5c3e22
PA
9958 struct watchpoint *w = (struct watchpoint *) bl->owner;
9959 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
9960
9961 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 9962 w->cond_exp);
77b06cd7
TJB
9963}
9964
9965/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
9966
9967static int
9968remove_watchpoint (struct bp_location *bl)
9969{
3a5c3e22
PA
9970 struct watchpoint *w = (struct watchpoint *) bl->owner;
9971 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
9972
9973 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 9974 w->cond_exp);
e09342b5
TJB
9975}
9976
e09342b5 9977static int
348d480f 9978breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
9979 struct address_space *aspace, CORE_ADDR bp_addr,
9980 const struct target_waitstatus *ws)
e09342b5 9981{
348d480f 9982 struct breakpoint *b = bl->owner;
3a5c3e22 9983 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 9984
348d480f
PA
9985 /* Continuable hardware watchpoints are treated as non-existent if the
9986 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9987 some data address). Otherwise gdb won't stop on a break instruction
9988 in the code (not from a breakpoint) when a hardware watchpoint has
9989 been defined. Also skip watchpoints which we know did not trigger
9990 (did not match the data address). */
9991 if (is_hardware_watchpoint (b)
3a5c3e22 9992 && w->watchpoint_triggered == watch_triggered_no)
348d480f 9993 return 0;
9c06b0b4 9994
348d480f 9995 return 1;
9c06b0b4
TJB
9996}
9997
348d480f
PA
9998static void
9999check_status_watchpoint (bpstat bs)
9c06b0b4 10000{
348d480f 10001 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10002
348d480f 10003 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10004}
10005
10006/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10007 hardware watchpoints. */
9c06b0b4
TJB
10008
10009static int
348d480f 10010resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10011{
3a5c3e22
PA
10012 struct watchpoint *w = (struct watchpoint *) bl->owner;
10013 int length = w->exact? 1 : bl->length;
348d480f
PA
10014
10015 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10016}
10017
10018/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10019 hardware watchpoints. */
9c06b0b4
TJB
10020
10021static int
348d480f 10022works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10023{
efa80663
PA
10024 /* Read and access watchpoints only work with hardware support. */
10025 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10026}
10027
9c06b0b4 10028static enum print_stop_action
348d480f 10029print_it_watchpoint (bpstat bs)
9c06b0b4 10030{
348d480f
PA
10031 struct cleanup *old_chain;
10032 struct breakpoint *b;
10033 const struct bp_location *bl;
f99d8bf4 10034 struct ui_file *stb;
348d480f 10035 enum print_stop_action result;
3a5c3e22 10036 struct watchpoint *w;
79a45e25 10037 struct ui_out *uiout = current_uiout;
348d480f
PA
10038
10039 gdb_assert (bs->bp_location_at != NULL);
10040
10041 bl = bs->bp_location_at;
10042 b = bs->breakpoint_at;
3a5c3e22 10043 w = (struct watchpoint *) b;
348d480f 10044
f99d8bf4
PA
10045 stb = mem_fileopen ();
10046 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10047
10048 switch (b->type)
10049 {
348d480f 10050 case bp_watchpoint:
9c06b0b4
TJB
10051 case bp_hardware_watchpoint:
10052 annotate_watchpoint (b->number);
10053 if (ui_out_is_mi_like_p (uiout))
10054 ui_out_field_string
10055 (uiout, "reason",
10056 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10057 mention (b);
10058 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10059 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10060 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10061 ui_out_field_stream (uiout, "old", stb);
10062 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10063 watchpoint_value_print (w->val, stb);
348d480f
PA
10064 ui_out_field_stream (uiout, "new", stb);
10065 ui_out_text (uiout, "\n");
10066 /* More than one watchpoint may have been triggered. */
10067 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10068 break;
10069
10070 case bp_read_watchpoint:
10071 if (ui_out_is_mi_like_p (uiout))
10072 ui_out_field_string
10073 (uiout, "reason",
10074 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10075 mention (b);
10076 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10077 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10078 watchpoint_value_print (w->val, stb);
348d480f
PA
10079 ui_out_field_stream (uiout, "value", stb);
10080 ui_out_text (uiout, "\n");
10081 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10082 break;
10083
10084 case bp_access_watchpoint:
348d480f
PA
10085 if (bs->old_val != NULL)
10086 {
10087 annotate_watchpoint (b->number);
10088 if (ui_out_is_mi_like_p (uiout))
10089 ui_out_field_string
10090 (uiout, "reason",
10091 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10092 mention (b);
10093 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10094 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10095 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10096 ui_out_field_stream (uiout, "old", stb);
10097 ui_out_text (uiout, "\nNew value = ");
10098 }
10099 else
10100 {
10101 mention (b);
10102 if (ui_out_is_mi_like_p (uiout))
10103 ui_out_field_string
10104 (uiout, "reason",
10105 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10106 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10107 ui_out_text (uiout, "\nValue = ");
10108 }
f99d8bf4 10109 watchpoint_value_print (w->val, stb);
348d480f
PA
10110 ui_out_field_stream (uiout, "new", stb);
10111 ui_out_text (uiout, "\n");
10112 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10113 break;
10114 default:
348d480f 10115 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10116 }
10117
348d480f
PA
10118 do_cleanups (old_chain);
10119 return result;
10120}
10121
10122/* Implement the "print_mention" breakpoint_ops method for hardware
10123 watchpoints. */
10124
10125static void
10126print_mention_watchpoint (struct breakpoint *b)
10127{
10128 struct cleanup *ui_out_chain;
3a5c3e22 10129 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10130 struct ui_out *uiout = current_uiout;
348d480f
PA
10131
10132 switch (b->type)
10133 {
10134 case bp_watchpoint:
10135 ui_out_text (uiout, "Watchpoint ");
10136 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10137 break;
10138 case bp_hardware_watchpoint:
10139 ui_out_text (uiout, "Hardware watchpoint ");
10140 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10141 break;
10142 case bp_read_watchpoint:
10143 ui_out_text (uiout, "Hardware read watchpoint ");
10144 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10145 break;
10146 case bp_access_watchpoint:
10147 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10148 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10149 break;
10150 default:
10151 internal_error (__FILE__, __LINE__,
10152 _("Invalid hardware watchpoint type."));
10153 }
10154
10155 ui_out_field_int (uiout, "number", b->number);
10156 ui_out_text (uiout, ": ");
3a5c3e22 10157 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10158 do_cleanups (ui_out_chain);
10159}
10160
10161/* Implement the "print_recreate" breakpoint_ops method for
10162 watchpoints. */
10163
10164static void
10165print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10166{
3a5c3e22
PA
10167 struct watchpoint *w = (struct watchpoint *) b;
10168
348d480f
PA
10169 switch (b->type)
10170 {
10171 case bp_watchpoint:
10172 case bp_hardware_watchpoint:
10173 fprintf_unfiltered (fp, "watch");
10174 break;
10175 case bp_read_watchpoint:
10176 fprintf_unfiltered (fp, "rwatch");
10177 break;
10178 case bp_access_watchpoint:
10179 fprintf_unfiltered (fp, "awatch");
10180 break;
10181 default:
10182 internal_error (__FILE__, __LINE__,
10183 _("Invalid watchpoint type."));
10184 }
10185
3a5c3e22 10186 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10187 print_recreate_thread (b, fp);
348d480f
PA
10188}
10189
10190/* The breakpoint_ops structure to be used in hardware watchpoints. */
10191
2060206e 10192static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10193
10194/* Implement the "insert" breakpoint_ops method for
10195 masked hardware watchpoints. */
10196
10197static int
10198insert_masked_watchpoint (struct bp_location *bl)
10199{
3a5c3e22
PA
10200 struct watchpoint *w = (struct watchpoint *) bl->owner;
10201
10202 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10203 bl->watchpoint_type);
10204}
10205
10206/* Implement the "remove" breakpoint_ops method for
10207 masked hardware watchpoints. */
10208
10209static int
10210remove_masked_watchpoint (struct bp_location *bl)
10211{
3a5c3e22
PA
10212 struct watchpoint *w = (struct watchpoint *) bl->owner;
10213
10214 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10215 bl->watchpoint_type);
10216}
10217
10218/* Implement the "resources_needed" breakpoint_ops method for
10219 masked hardware watchpoints. */
10220
10221static int
10222resources_needed_masked_watchpoint (const struct bp_location *bl)
10223{
3a5c3e22
PA
10224 struct watchpoint *w = (struct watchpoint *) bl->owner;
10225
10226 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10227}
10228
10229/* Implement the "works_in_software_mode" breakpoint_ops method for
10230 masked hardware watchpoints. */
10231
10232static int
10233works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10234{
10235 return 0;
10236}
10237
10238/* Implement the "print_it" breakpoint_ops method for
10239 masked hardware watchpoints. */
10240
10241static enum print_stop_action
10242print_it_masked_watchpoint (bpstat bs)
10243{
10244 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10245 struct ui_out *uiout = current_uiout;
348d480f
PA
10246
10247 /* Masked watchpoints have only one location. */
10248 gdb_assert (b->loc && b->loc->next == NULL);
10249
10250 switch (b->type)
10251 {
10252 case bp_hardware_watchpoint:
10253 annotate_watchpoint (b->number);
10254 if (ui_out_is_mi_like_p (uiout))
10255 ui_out_field_string
10256 (uiout, "reason",
10257 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10258 break;
10259
10260 case bp_read_watchpoint:
10261 if (ui_out_is_mi_like_p (uiout))
10262 ui_out_field_string
10263 (uiout, "reason",
10264 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10265 break;
10266
10267 case bp_access_watchpoint:
10268 if (ui_out_is_mi_like_p (uiout))
10269 ui_out_field_string
10270 (uiout, "reason",
10271 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10272 break;
10273 default:
10274 internal_error (__FILE__, __LINE__,
10275 _("Invalid hardware watchpoint type."));
10276 }
10277
10278 mention (b);
9c06b0b4
TJB
10279 ui_out_text (uiout, _("\n\
10280Check the underlying instruction at PC for the memory\n\
10281address and value which triggered this watchpoint.\n"));
10282 ui_out_text (uiout, "\n");
10283
10284 /* More than one watchpoint may have been triggered. */
10285 return PRINT_UNKNOWN;
10286}
10287
10288/* Implement the "print_one_detail" breakpoint_ops method for
10289 masked hardware watchpoints. */
10290
10291static void
10292print_one_detail_masked_watchpoint (const struct breakpoint *b,
10293 struct ui_out *uiout)
10294{
3a5c3e22
PA
10295 struct watchpoint *w = (struct watchpoint *) b;
10296
9c06b0b4
TJB
10297 /* Masked watchpoints have only one location. */
10298 gdb_assert (b->loc && b->loc->next == NULL);
10299
10300 ui_out_text (uiout, "\tmask ");
3a5c3e22 10301 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10302 ui_out_text (uiout, "\n");
10303}
10304
10305/* Implement the "print_mention" breakpoint_ops method for
10306 masked hardware watchpoints. */
10307
10308static void
10309print_mention_masked_watchpoint (struct breakpoint *b)
10310{
3a5c3e22 10311 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10312 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10313 struct cleanup *ui_out_chain;
10314
10315 switch (b->type)
10316 {
10317 case bp_hardware_watchpoint:
10318 ui_out_text (uiout, "Masked hardware watchpoint ");
10319 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10320 break;
10321 case bp_read_watchpoint:
10322 ui_out_text (uiout, "Masked hardware read watchpoint ");
10323 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10324 break;
10325 case bp_access_watchpoint:
10326 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10327 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10328 break;
10329 default:
10330 internal_error (__FILE__, __LINE__,
10331 _("Invalid hardware watchpoint type."));
10332 }
10333
10334 ui_out_field_int (uiout, "number", b->number);
10335 ui_out_text (uiout, ": ");
3a5c3e22 10336 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10337 do_cleanups (ui_out_chain);
10338}
10339
10340/* Implement the "print_recreate" breakpoint_ops method for
10341 masked hardware watchpoints. */
10342
10343static void
10344print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10345{
3a5c3e22 10346 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10347 char tmp[40];
10348
10349 switch (b->type)
10350 {
10351 case bp_hardware_watchpoint:
10352 fprintf_unfiltered (fp, "watch");
10353 break;
10354 case bp_read_watchpoint:
10355 fprintf_unfiltered (fp, "rwatch");
10356 break;
10357 case bp_access_watchpoint:
10358 fprintf_unfiltered (fp, "awatch");
10359 break;
10360 default:
10361 internal_error (__FILE__, __LINE__,
10362 _("Invalid hardware watchpoint type."));
10363 }
10364
3a5c3e22
PA
10365 sprintf_vma (tmp, w->hw_wp_mask);
10366 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10367 print_recreate_thread (b, fp);
9c06b0b4
TJB
10368}
10369
10370/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10371
2060206e 10372static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10373
10374/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10375
10376static int
10377is_masked_watchpoint (const struct breakpoint *b)
10378{
10379 return b->ops == &masked_watchpoint_breakpoint_ops;
10380}
10381
53a5351d
JM
10382/* accessflag: hw_write: watch write,
10383 hw_read: watch read,
10384 hw_access: watch access (read or write) */
c906108c 10385static void
84f4c1fe
PM
10386watch_command_1 (char *arg, int accessflag, int from_tty,
10387 int just_location, int internal)
c906108c 10388{
a9634178 10389 volatile struct gdb_exception e;
d983da9c 10390 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10391 struct expression *exp;
60e1c644 10392 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10393 struct value *val, *mark, *result;
c906108c 10394 struct frame_info *frame;
c906108c
SS
10395 char *exp_start = NULL;
10396 char *exp_end = NULL;
9c06b0b4
TJB
10397 char *tok, *end_tok;
10398 int toklen = -1;
c906108c
SS
10399 char *cond_start = NULL;
10400 char *cond_end = NULL;
c906108c 10401 enum bptype bp_type;
37e4754d 10402 int thread = -1;
0cf6dd15 10403 int pc = 0;
9c06b0b4
TJB
10404 /* Flag to indicate whether we are going to use masks for
10405 the hardware watchpoint. */
10406 int use_mask = 0;
10407 CORE_ADDR mask = 0;
3a5c3e22 10408 struct watchpoint *w;
c906108c 10409
37e4754d
LM
10410 /* Make sure that we actually have parameters to parse. */
10411 if (arg != NULL && arg[0] != '\0')
10412 {
9c06b0b4 10413 char *value_start;
37e4754d 10414
9c06b0b4
TJB
10415 /* Look for "parameter value" pairs at the end
10416 of the arguments string. */
10417 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10418 {
10419 /* Skip whitespace at the end of the argument list. */
10420 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10421 tok--;
10422
10423 /* Find the beginning of the last token.
10424 This is the value of the parameter. */
10425 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10426 tok--;
10427 value_start = tok + 1;
10428
10429 /* Skip whitespace. */
10430 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10431 tok--;
10432
10433 end_tok = tok;
10434
10435 /* Find the beginning of the second to last token.
10436 This is the parameter itself. */
10437 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10438 tok--;
10439 tok++;
10440 toklen = end_tok - tok + 1;
10441
10442 if (toklen == 6 && !strncmp (tok, "thread", 6))
10443 {
10444 /* At this point we've found a "thread" token, which means
10445 the user is trying to set a watchpoint that triggers
10446 only in a specific thread. */
10447 char *endp;
37e4754d 10448
9c06b0b4
TJB
10449 if (thread != -1)
10450 error(_("You can specify only one thread."));
37e4754d 10451
9c06b0b4
TJB
10452 /* Extract the thread ID from the next token. */
10453 thread = strtol (value_start, &endp, 0);
37e4754d 10454
9c06b0b4
TJB
10455 /* Check if the user provided a valid numeric value for the
10456 thread ID. */
10457 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10458 error (_("Invalid thread ID specification %s."), value_start);
10459
10460 /* Check if the thread actually exists. */
10461 if (!valid_thread_id (thread))
10462 error (_("Unknown thread %d."), thread);
10463 }
10464 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10465 {
10466 /* We've found a "mask" token, which means the user wants to
10467 create a hardware watchpoint that is going to have the mask
10468 facility. */
10469 struct value *mask_value, *mark;
37e4754d 10470
9c06b0b4
TJB
10471 if (use_mask)
10472 error(_("You can specify only one mask."));
37e4754d 10473
9c06b0b4 10474 use_mask = just_location = 1;
37e4754d 10475
9c06b0b4
TJB
10476 mark = value_mark ();
10477 mask_value = parse_to_comma_and_eval (&value_start);
10478 mask = value_as_address (mask_value);
10479 value_free_to_mark (mark);
10480 }
10481 else
10482 /* We didn't recognize what we found. We should stop here. */
10483 break;
37e4754d 10484
9c06b0b4
TJB
10485 /* Truncate the string and get rid of the "parameter value" pair before
10486 the arguments string is parsed by the parse_exp_1 function. */
10487 *tok = '\0';
10488 }
37e4754d
LM
10489 }
10490
10491 /* Parse the rest of the arguments. */
c906108c
SS
10492 innermost_block = NULL;
10493 exp_start = arg;
10494 exp = parse_exp_1 (&arg, 0, 0);
10495 exp_end = arg;
fa8a61dc
TT
10496 /* Remove trailing whitespace from the expression before saving it.
10497 This makes the eventual display of the expression string a bit
10498 prettier. */
10499 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10500 --exp_end;
10501
65d79d4b
SDJ
10502 /* Checking if the expression is not constant. */
10503 if (watchpoint_exp_is_const (exp))
10504 {
10505 int len;
10506
10507 len = exp_end - exp_start;
10508 while (len > 0 && isspace (exp_start[len - 1]))
10509 len--;
10510 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10511 }
10512
c906108c
SS
10513 exp_valid_block = innermost_block;
10514 mark = value_mark ();
a1442452 10515 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
10516
10517 if (just_location)
10518 {
9c06b0b4
TJB
10519 int ret;
10520
06a64a0b 10521 exp_valid_block = NULL;
a1442452 10522 val = value_addr (result);
06a64a0b
TT
10523 release_value (val);
10524 value_free_to_mark (mark);
9c06b0b4
TJB
10525
10526 if (use_mask)
10527 {
10528 ret = target_masked_watch_num_registers (value_as_address (val),
10529 mask);
10530 if (ret == -1)
10531 error (_("This target does not support masked watchpoints."));
10532 else if (ret == -2)
10533 error (_("Invalid mask or memory region."));
10534 }
06a64a0b
TT
10535 }
10536 else if (val != NULL)
fa4727a6 10537 release_value (val);
c906108c 10538
e9cafbcc
TT
10539 tok = skip_spaces (arg);
10540 end_tok = skip_to_space (tok);
c906108c
SS
10541
10542 toklen = end_tok - tok;
10543 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10544 {
2d134ed3
PA
10545 struct expression *cond;
10546
60e1c644 10547 innermost_block = NULL;
c906108c
SS
10548 tok = cond_start = end_tok + 1;
10549 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
10550
10551 /* The watchpoint expression may not be local, but the condition
10552 may still be. E.g.: `watch global if local > 0'. */
10553 cond_exp_valid_block = innermost_block;
10554
2d134ed3 10555 xfree (cond);
c906108c
SS
10556 cond_end = tok;
10557 }
10558 if (*tok)
8a3fe4f8 10559 error (_("Junk at end of command."));
c906108c 10560
53a5351d 10561 if (accessflag == hw_read)
c5aa993b 10562 bp_type = bp_read_watchpoint;
53a5351d 10563 else if (accessflag == hw_access)
c5aa993b
JM
10564 bp_type = bp_access_watchpoint;
10565 else
10566 bp_type = bp_hardware_watchpoint;
c906108c 10567
d983da9c 10568 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
10569
10570 /* If the expression is "local", then set up a "watchpoint scope"
10571 breakpoint at the point where we've left the scope of the watchpoint
10572 expression. Create the scope breakpoint before the watchpoint, so
10573 that we will encounter it first in bpstat_stop_status. */
60e1c644 10574 if (exp_valid_block && frame)
d983da9c 10575 {
edb3359d
DJ
10576 if (frame_id_p (frame_unwind_caller_id (frame)))
10577 {
10578 scope_breakpoint
a6d9a66e
UW
10579 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10580 frame_unwind_caller_pc (frame),
06edf0c0
PA
10581 bp_watchpoint_scope,
10582 &momentary_breakpoint_ops);
d983da9c 10583
edb3359d 10584 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10585
edb3359d
DJ
10586 /* Automatically delete the breakpoint when it hits. */
10587 scope_breakpoint->disposition = disp_del;
d983da9c 10588
edb3359d
DJ
10589 /* Only break in the proper frame (help with recursion). */
10590 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 10591
edb3359d 10592 /* Set the address at which we will stop. */
a6d9a66e
UW
10593 scope_breakpoint->loc->gdbarch
10594 = frame_unwind_caller_arch (frame);
edb3359d
DJ
10595 scope_breakpoint->loc->requested_address
10596 = frame_unwind_caller_pc (frame);
10597 scope_breakpoint->loc->address
a6d9a66e
UW
10598 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10599 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10600 scope_breakpoint->type);
10601 }
d983da9c
DJ
10602 }
10603
c906108c 10604 /* Now set up the breakpoint. */
3a5c3e22
PA
10605
10606 w = XCNEW (struct watchpoint);
10607 b = &w->base;
348d480f 10608 if (use_mask)
3a5c3e22
PA
10609 init_raw_breakpoint_without_location (b, NULL, bp_type,
10610 &masked_watchpoint_breakpoint_ops);
348d480f 10611 else
3a5c3e22
PA
10612 init_raw_breakpoint_without_location (b, NULL, bp_type,
10613 &watchpoint_breakpoint_ops);
37e4754d 10614 b->thread = thread;
b5de0fa7 10615 b->disposition = disp_donttouch;
348d480f 10616 b->pspace = current_program_space;
3a5c3e22
PA
10617 w->exp = exp;
10618 w->exp_valid_block = exp_valid_block;
10619 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10620 if (just_location)
10621 {
10622 struct type *t = value_type (val);
10623 CORE_ADDR addr = value_as_address (val);
10624 char *name;
10625
10626 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10627 name = type_to_string (t);
10628
3a5c3e22 10629 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 10630 core_addr_to_string (addr));
06a64a0b
TT
10631 xfree (name);
10632
3a5c3e22 10633 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
10634 (int) (exp_end - exp_start), exp_start);
10635
06a64a0b
TT
10636 /* The above expression is in C. */
10637 b->language = language_c;
10638 }
10639 else
3a5c3e22 10640 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10641
10642 if (use_mask)
10643 {
3a5c3e22 10644 w->hw_wp_mask = mask;
9c06b0b4
TJB
10645 }
10646 else
10647 {
3a5c3e22
PA
10648 w->val = val;
10649 w->val_valid = 1;
9c06b0b4 10650 }
77b06cd7 10651
c906108c
SS
10652 if (cond_start)
10653 b->cond_string = savestring (cond_start, cond_end - cond_start);
10654 else
10655 b->cond_string = 0;
c5aa993b 10656
c906108c 10657 if (frame)
f6bc2008 10658 {
3a5c3e22
PA
10659 w->watchpoint_frame = get_frame_id (frame);
10660 w->watchpoint_thread = inferior_ptid;
f6bc2008 10661 }
c906108c 10662 else
f6bc2008 10663 {
3a5c3e22
PA
10664 w->watchpoint_frame = null_frame_id;
10665 w->watchpoint_thread = null_ptid;
f6bc2008 10666 }
c906108c 10667
d983da9c 10668 if (scope_breakpoint != NULL)
c906108c 10669 {
d983da9c
DJ
10670 /* The scope breakpoint is related to the watchpoint. We will
10671 need to act on them together. */
10672 b->related_breakpoint = scope_breakpoint;
10673 scope_breakpoint->related_breakpoint = b;
c906108c 10674 }
d983da9c 10675
06a64a0b
TT
10676 if (!just_location)
10677 value_free_to_mark (mark);
2d134ed3 10678
a9634178
TJB
10679 TRY_CATCH (e, RETURN_MASK_ALL)
10680 {
10681 /* Finally update the new watchpoint. This creates the locations
10682 that should be inserted. */
3a5c3e22 10683 update_watchpoint (w, 1);
a9634178
TJB
10684 }
10685 if (e.reason < 0)
10686 {
10687 delete_breakpoint (b);
10688 throw_exception (e);
10689 }
10690
3ea46bff 10691 install_breakpoint (internal, b, 1);
c906108c
SS
10692}
10693
e09342b5 10694/* Return count of debug registers needed to watch the given expression.
e09342b5 10695 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10696
c906108c 10697static int
a9634178 10698can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10699{
10700 int found_memory_cnt = 0;
2e70b7b9 10701 struct value *head = v;
c906108c
SS
10702
10703 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10704 if (!can_use_hw_watchpoints)
c906108c 10705 return 0;
c5aa993b 10706
5c44784c
JM
10707 /* Make sure that the value of the expression depends only upon
10708 memory contents, and values computed from them within GDB. If we
10709 find any register references or function calls, we can't use a
10710 hardware watchpoint.
10711
10712 The idea here is that evaluating an expression generates a series
10713 of values, one holding the value of every subexpression. (The
10714 expression a*b+c has five subexpressions: a, b, a*b, c, and
10715 a*b+c.) GDB's values hold almost enough information to establish
10716 the criteria given above --- they identify memory lvalues,
10717 register lvalues, computed values, etcetera. So we can evaluate
10718 the expression, and then scan the chain of values that leaves
10719 behind to decide whether we can detect any possible change to the
10720 expression's final value using only hardware watchpoints.
10721
10722 However, I don't think that the values returned by inferior
10723 function calls are special in any way. So this function may not
10724 notice that an expression involving an inferior function call
10725 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10726 for (; v; v = value_next (v))
c906108c 10727 {
5c44784c 10728 if (VALUE_LVAL (v) == lval_memory)
c906108c 10729 {
8464be76
DJ
10730 if (v != head && value_lazy (v))
10731 /* A lazy memory lvalue in the chain is one that GDB never
10732 needed to fetch; we either just used its address (e.g.,
10733 `a' in `a.b') or we never needed it at all (e.g., `a'
10734 in `a,b'). This doesn't apply to HEAD; if that is
10735 lazy then it was not readable, but watch it anyway. */
5c44784c 10736 ;
53a5351d 10737 else
5c44784c
JM
10738 {
10739 /* Ahh, memory we actually used! Check if we can cover
10740 it with hardware watchpoints. */
df407dfe 10741 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10742
10743 /* We only watch structs and arrays if user asked for it
10744 explicitly, never if they just happen to appear in a
10745 middle of some value chain. */
10746 if (v == head
10747 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10748 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10749 {
42ae5230 10750 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10751 int len;
10752 int num_regs;
10753
a9634178 10754 len = (target_exact_watchpoints
e09342b5
TJB
10755 && is_scalar_type_recursive (vtype))?
10756 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10757
e09342b5
TJB
10758 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10759 if (!num_regs)
2e70b7b9
MS
10760 return 0;
10761 else
e09342b5 10762 found_memory_cnt += num_regs;
2e70b7b9 10763 }
5c44784c 10764 }
c5aa993b 10765 }
5086187c
AC
10766 else if (VALUE_LVAL (v) != not_lval
10767 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10768 return 0; /* These are values from the history (e.g., $1). */
5086187c 10769 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10770 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10771 }
10772
10773 /* The expression itself looks suitable for using a hardware
10774 watchpoint, but give the target machine a chance to reject it. */
10775 return found_memory_cnt;
10776}
10777
8b93c638 10778void
84f4c1fe 10779watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10780{
84f4c1fe 10781 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10782}
10783
10784/* A helper function that looks for an argument at the start of a
10785 string. The argument must also either be at the end of the string,
10786 or be followed by whitespace. Returns 1 if it finds the argument,
10787 0 otherwise. If the argument is found, it updates *STR. */
10788
10789static int
10790check_for_argument (char **str, char *arg, int arg_len)
10791{
10792 if (strncmp (*str, arg, arg_len) == 0
10793 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10794 {
10795 *str += arg_len;
10796 return 1;
10797 }
10798 return 0;
10799}
10800
10801/* A helper function that looks for the "-location" argument and then
10802 calls watch_command_1. */
10803
10804static void
10805watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10806{
10807 int just_location = 0;
10808
10809 if (arg
10810 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10811 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10812 {
e9cafbcc 10813 arg = skip_spaces (arg);
06a64a0b
TT
10814 just_location = 1;
10815 }
10816
84f4c1fe 10817 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10818}
8926118c 10819
c5aa993b 10820static void
fba45db2 10821watch_command (char *arg, int from_tty)
c906108c 10822{
06a64a0b 10823 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10824}
10825
8b93c638 10826void
84f4c1fe 10827rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10828{
84f4c1fe 10829 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10830}
8926118c 10831
c5aa993b 10832static void
fba45db2 10833rwatch_command (char *arg, int from_tty)
c906108c 10834{
06a64a0b 10835 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10836}
10837
8b93c638 10838void
84f4c1fe 10839awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10840{
84f4c1fe 10841 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10842}
8926118c 10843
c5aa993b 10844static void
fba45db2 10845awatch_command (char *arg, int from_tty)
c906108c 10846{
06a64a0b 10847 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10848}
c906108c 10849\f
c5aa993b 10850
43ff13b4 10851/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
10852 because it uses the mechanisms of breakpoints. */
10853
bfec99b2
PA
10854struct until_break_command_continuation_args
10855{
10856 struct breakpoint *breakpoint;
10857 struct breakpoint *breakpoint2;
186c406b 10858 int thread_num;
bfec99b2
PA
10859};
10860
43ff13b4 10861/* This function is called by fetch_inferior_event via the
4a64f543 10862 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 10863 care of cleaning up the temporary breakpoints set up by the until
4a64f543 10864 command. */
c2c6d25f 10865static void
fa4cd53f 10866until_break_command_continuation (void *arg, int err)
43ff13b4 10867{
bfec99b2
PA
10868 struct until_break_command_continuation_args *a = arg;
10869
10870 delete_breakpoint (a->breakpoint);
10871 if (a->breakpoint2)
10872 delete_breakpoint (a->breakpoint2);
186c406b 10873 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
10874}
10875
c906108c 10876void
ae66c1fc 10877until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
10878{
10879 struct symtabs_and_lines sals;
10880 struct symtab_and_line sal;
8556afb4
PA
10881 struct frame_info *frame;
10882 struct gdbarch *frame_gdbarch;
10883 struct frame_id stack_frame_id;
10884 struct frame_id caller_frame_id;
c906108c 10885 struct breakpoint *breakpoint;
f107f563 10886 struct breakpoint *breakpoint2 = NULL;
c906108c 10887 struct cleanup *old_chain;
186c406b
TT
10888 int thread;
10889 struct thread_info *tp;
c906108c
SS
10890
10891 clear_proceed_status ();
10892
10893 /* Set a breakpoint where the user wants it and at return from
4a64f543 10894 this function. */
c5aa993b 10895
1bfeeb0f 10896 if (last_displayed_sal_is_valid ())
f8eba3c6 10897 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 10898 get_last_displayed_symtab (),
f8eba3c6 10899 get_last_displayed_line ());
c906108c 10900 else
f8eba3c6
TT
10901 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10902 (struct symtab *) NULL, 0);
c5aa993b 10903
c906108c 10904 if (sals.nelts != 1)
8a3fe4f8 10905 error (_("Couldn't get information on specified line."));
c5aa993b 10906
c906108c 10907 sal = sals.sals[0];
4a64f543 10908 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 10909
c906108c 10910 if (*arg)
8a3fe4f8 10911 error (_("Junk at end of arguments."));
c5aa993b 10912
c906108c 10913 resolve_sal_pc (&sal);
c5aa993b 10914
186c406b
TT
10915 tp = inferior_thread ();
10916 thread = tp->num;
10917
883bc8d1
PA
10918 old_chain = make_cleanup (null_cleanup, NULL);
10919
8556afb4
PA
10920 /* Note linespec handling above invalidates the frame chain.
10921 Installing a breakpoint also invalidates the frame chain (as it
10922 may need to switch threads), so do any frame handling before
10923 that. */
10924
10925 frame = get_selected_frame (NULL);
10926 frame_gdbarch = get_frame_arch (frame);
10927 stack_frame_id = get_stack_frame_id (frame);
10928 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 10929
ae66c1fc
EZ
10930 /* Keep within the current frame, or in frames called by the current
10931 one. */
edb3359d 10932
883bc8d1 10933 if (frame_id_p (caller_frame_id))
c906108c 10934 {
883bc8d1
PA
10935 struct symtab_and_line sal2;
10936
10937 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10938 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 10939 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
10940 sal2,
10941 caller_frame_id,
f107f563
VP
10942 bp_until);
10943 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 10944
883bc8d1 10945 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 10946 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 10947 }
c5aa993b 10948
c70a6932
JK
10949 /* set_momentary_breakpoint could invalidate FRAME. */
10950 frame = NULL;
10951
883bc8d1
PA
10952 if (anywhere)
10953 /* If the user told us to continue until a specified location,
10954 we don't specify a frame at which we need to stop. */
10955 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10956 null_frame_id, bp_until);
10957 else
10958 /* Otherwise, specify the selected frame, because we want to stop
10959 only at the very same frame. */
10960 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10961 stack_frame_id, bp_until);
10962 make_cleanup_delete_breakpoint (breakpoint);
10963
a493e3e2 10964 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 10965
4a64f543
MS
10966 /* If we are running asynchronously, and proceed call above has
10967 actually managed to start the target, arrange for breakpoints to
10968 be deleted when the target stops. Otherwise, we're already
10969 stopped and delete breakpoints via cleanup chain. */
f107f563 10970
8ea051c5 10971 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 10972 {
bfec99b2
PA
10973 struct until_break_command_continuation_args *args;
10974 args = xmalloc (sizeof (*args));
f107f563 10975
bfec99b2
PA
10976 args->breakpoint = breakpoint;
10977 args->breakpoint2 = breakpoint2;
186c406b 10978 args->thread_num = thread;
f107f563
VP
10979
10980 discard_cleanups (old_chain);
95e54da7
PA
10981 add_continuation (inferior_thread (),
10982 until_break_command_continuation, args,
604ead4a 10983 xfree);
f107f563
VP
10984 }
10985 else
c5aa993b 10986 do_cleanups (old_chain);
c906108c 10987}
ae66c1fc 10988
c906108c
SS
10989/* This function attempts to parse an optional "if <cond>" clause
10990 from the arg string. If one is not found, it returns NULL.
c5aa993b 10991
c906108c
SS
10992 Else, it returns a pointer to the condition string. (It does not
10993 attempt to evaluate the string against a particular block.) And,
10994 it updates arg to point to the first character following the parsed
4a64f543 10995 if clause in the arg string. */
53a5351d 10996
c906108c 10997static char *
fba45db2 10998ep_parse_optional_if_clause (char **arg)
c906108c 10999{
c5aa993b
JM
11000 char *cond_string;
11001
11002 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11003 return NULL;
c5aa993b 11004
4a64f543 11005 /* Skip the "if" keyword. */
c906108c 11006 (*arg) += 2;
c5aa993b 11007
c906108c 11008 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11009 condition string. */
e9cafbcc 11010 *arg = skip_spaces (*arg);
c906108c 11011 cond_string = *arg;
c5aa993b 11012
4a64f543
MS
11013 /* Assume that the condition occupies the remainder of the arg
11014 string. */
c906108c 11015 (*arg) += strlen (cond_string);
c5aa993b 11016
c906108c
SS
11017 return cond_string;
11018}
c5aa993b 11019
c906108c
SS
11020/* Commands to deal with catching events, such as signals, exceptions,
11021 process start/exit, etc. */
c5aa993b
JM
11022
11023typedef enum
11024{
44feb3ce
TT
11025 catch_fork_temporary, catch_vfork_temporary,
11026 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11027}
11028catch_fork_kind;
11029
c906108c 11030static void
cc59ec59
MS
11031catch_fork_command_1 (char *arg, int from_tty,
11032 struct cmd_list_element *command)
c906108c 11033{
a6d9a66e 11034 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11035 char *cond_string = NULL;
44feb3ce
TT
11036 catch_fork_kind fork_kind;
11037 int tempflag;
11038
11039 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11040 tempflag = (fork_kind == catch_fork_temporary
11041 || fork_kind == catch_vfork_temporary);
c5aa993b 11042
44feb3ce
TT
11043 if (!arg)
11044 arg = "";
e9cafbcc 11045 arg = skip_spaces (arg);
c5aa993b 11046
c906108c 11047 /* The allowed syntax is:
c5aa993b
JM
11048 catch [v]fork
11049 catch [v]fork if <cond>
11050
4a64f543 11051 First, check if there's an if clause. */
c906108c 11052 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11053
c906108c 11054 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11055 error (_("Junk at end of arguments."));
c5aa993b 11056
c906108c 11057 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11058 and enable reporting of such events. */
c5aa993b
JM
11059 switch (fork_kind)
11060 {
44feb3ce
TT
11061 case catch_fork_temporary:
11062 case catch_fork_permanent:
a6d9a66e 11063 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11064 &catch_fork_breakpoint_ops);
c906108c 11065 break;
44feb3ce
TT
11066 case catch_vfork_temporary:
11067 case catch_vfork_permanent:
a6d9a66e 11068 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11069 &catch_vfork_breakpoint_ops);
c906108c 11070 break;
c5aa993b 11071 default:
8a3fe4f8 11072 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11073 break;
c5aa993b 11074 }
c906108c
SS
11075}
11076
11077static void
cc59ec59
MS
11078catch_exec_command_1 (char *arg, int from_tty,
11079 struct cmd_list_element *command)
c906108c 11080{
b4d90040 11081 struct exec_catchpoint *c;
a6d9a66e 11082 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11083 int tempflag;
c5aa993b 11084 char *cond_string = NULL;
c906108c 11085
44feb3ce
TT
11086 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11087
11088 if (!arg)
11089 arg = "";
e9cafbcc 11090 arg = skip_spaces (arg);
c906108c
SS
11091
11092 /* The allowed syntax is:
c5aa993b
JM
11093 catch exec
11094 catch exec if <cond>
c906108c 11095
4a64f543 11096 First, check if there's an if clause. */
c906108c
SS
11097 cond_string = ep_parse_optional_if_clause (&arg);
11098
11099 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11100 error (_("Junk at end of arguments."));
c906108c 11101
b4d90040
PA
11102 c = XNEW (struct exec_catchpoint);
11103 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11104 &catch_exec_breakpoint_ops);
11105 c->exec_pathname = NULL;
11106
3ea46bff 11107 install_breakpoint (0, &c->base, 1);
c906108c 11108}
c5aa993b 11109
3086aeae 11110static enum print_stop_action
348d480f 11111print_it_exception_catchpoint (bpstat bs)
3086aeae 11112{
79a45e25 11113 struct ui_out *uiout = current_uiout;
348d480f 11114 struct breakpoint *b = bs->breakpoint_at;
ade92717 11115 int bp_temp, bp_throw;
3086aeae 11116
ade92717 11117 annotate_catchpoint (b->number);
3086aeae 11118
ade92717
AR
11119 bp_throw = strstr (b->addr_string, "throw") != NULL;
11120 if (b->loc->address != b->loc->requested_address)
11121 breakpoint_adjustment_warning (b->loc->requested_address,
11122 b->loc->address,
11123 b->number, 1);
df2b6d2d 11124 bp_temp = b->disposition == disp_del;
ade92717
AR
11125 ui_out_text (uiout,
11126 bp_temp ? "Temporary catchpoint "
11127 : "Catchpoint ");
11128 if (!ui_out_is_mi_like_p (uiout))
11129 ui_out_field_int (uiout, "bkptno", b->number);
11130 ui_out_text (uiout,
c0b37c48
AR
11131 bp_throw ? " (exception thrown), "
11132 : " (exception caught), ");
ade92717
AR
11133 if (ui_out_is_mi_like_p (uiout))
11134 {
11135 ui_out_field_string (uiout, "reason",
11136 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11137 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11138 ui_out_field_int (uiout, "bkptno", b->number);
11139 }
3086aeae
DJ
11140 return PRINT_SRC_AND_LOC;
11141}
11142
11143static void
cc59ec59
MS
11144print_one_exception_catchpoint (struct breakpoint *b,
11145 struct bp_location **last_loc)
3086aeae 11146{
79a45b7d 11147 struct value_print_options opts;
79a45e25 11148 struct ui_out *uiout = current_uiout;
cc59ec59 11149
79a45b7d
TT
11150 get_user_print_options (&opts);
11151 if (opts.addressprint)
3086aeae
DJ
11152 {
11153 annotate_field (4);
604133b5
AR
11154 if (b->loc == NULL || b->loc->shlib_disabled)
11155 ui_out_field_string (uiout, "addr", "<PENDING>");
11156 else
5af949e3
UW
11157 ui_out_field_core_addr (uiout, "addr",
11158 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
11159 }
11160 annotate_field (5);
604133b5 11161 if (b->loc)
a6d9a66e 11162 *last_loc = b->loc;
3086aeae
DJ
11163 if (strstr (b->addr_string, "throw") != NULL)
11164 ui_out_field_string (uiout, "what", "exception throw");
11165 else
11166 ui_out_field_string (uiout, "what", "exception catch");
11167}
11168
11169static void
11170print_mention_exception_catchpoint (struct breakpoint *b)
11171{
79a45e25 11172 struct ui_out *uiout = current_uiout;
ade92717
AR
11173 int bp_temp;
11174 int bp_throw;
11175
df2b6d2d 11176 bp_temp = b->disposition == disp_del;
ade92717
AR
11177 bp_throw = strstr (b->addr_string, "throw") != NULL;
11178 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11179 : _("Catchpoint "));
11180 ui_out_field_int (uiout, "bkptno", b->number);
11181 ui_out_text (uiout, bp_throw ? _(" (throw)")
11182 : _(" (catch)"));
3086aeae
DJ
11183}
11184
6149aea9
PA
11185/* Implement the "print_recreate" breakpoint_ops method for throw and
11186 catch catchpoints. */
11187
11188static void
4a64f543
MS
11189print_recreate_exception_catchpoint (struct breakpoint *b,
11190 struct ui_file *fp)
6149aea9
PA
11191{
11192 int bp_temp;
11193 int bp_throw;
11194
11195 bp_temp = b->disposition == disp_del;
11196 bp_throw = strstr (b->addr_string, "throw") != NULL;
11197 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11198 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 11199 print_recreate_thread (b, fp);
6149aea9
PA
11200}
11201
2060206e 11202static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
11203
11204static int
11205handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11206 enum exception_event_kind ex_event, int from_tty)
11207{
604133b5
AR
11208 char *trigger_func_name;
11209
3086aeae 11210 if (ex_event == EX_EVENT_CATCH)
604133b5 11211 trigger_func_name = "__cxa_begin_catch";
3086aeae 11212 else
604133b5 11213 trigger_func_name = "__cxa_throw";
3086aeae 11214
8cdf0e15 11215 create_breakpoint (get_current_arch (),
e7e0cddf 11216 trigger_func_name, cond_string, -1, NULL,
8cdf0e15 11217 0 /* condition and thread are valid. */,
0fb4aa4b 11218 tempflag, bp_breakpoint,
8cdf0e15
VP
11219 0,
11220 AUTO_BOOLEAN_TRUE /* pending */,
11221 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe 11222 1 /* enabled */,
44f238bb
PA
11223 0 /* internal */,
11224 0);
3086aeae 11225
3086aeae
DJ
11226 return 1;
11227}
11228
4a64f543 11229/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
11230
11231static void
fba45db2
KB
11232catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11233 int tempflag, int from_tty)
c906108c 11234{
c5aa993b 11235 char *cond_string = NULL;
c5aa993b 11236
44feb3ce
TT
11237 if (!arg)
11238 arg = "";
e9cafbcc 11239 arg = skip_spaces (arg);
c5aa993b 11240
c906108c
SS
11241 cond_string = ep_parse_optional_if_clause (&arg);
11242
11243 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11244 error (_("Junk at end of arguments."));
c906108c 11245
059fb39f
PM
11246 if (ex_event != EX_EVENT_THROW
11247 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 11248 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 11249
3086aeae
DJ
11250 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11251 return;
11252
8a3fe4f8 11253 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
11254}
11255
44feb3ce
TT
11256/* Implementation of "catch catch" command. */
11257
11258static void
11259catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11260{
11261 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11262
44feb3ce
TT
11263 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11264}
11265
11266/* Implementation of "catch throw" command. */
11267
11268static void
11269catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11270{
11271 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11272
44feb3ce
TT
11273 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11274}
11275
9ac4176b 11276void
28010a5d
PA
11277init_ada_exception_breakpoint (struct breakpoint *b,
11278 struct gdbarch *gdbarch,
11279 struct symtab_and_line sal,
11280 char *addr_string,
c0a91b2b 11281 const struct breakpoint_ops *ops,
28010a5d
PA
11282 int tempflag,
11283 int from_tty)
f7f9143b 11284{
f7f9143b
JB
11285 if (from_tty)
11286 {
5af949e3
UW
11287 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11288 if (!loc_gdbarch)
11289 loc_gdbarch = gdbarch;
11290
6c95b8df
PA
11291 describe_other_breakpoints (loc_gdbarch,
11292 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11293 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11294 version for exception catchpoints, because two catchpoints
11295 used for different exception names will use the same address.
11296 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11297 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11298 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11299 the user what type of catchpoint it is. The above is good
11300 enough for now, though. */
11301 }
11302
28010a5d 11303 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11304
11305 b->enable_state = bp_enabled;
11306 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11307 b->addr_string = addr_string;
11308 b->language = language_ada;
f7f9143b
JB
11309}
11310
a96d9b2e
SDJ
11311/* Splits the argument using space as delimiter. Returns an xmalloc'd
11312 filter list, or NULL if no filtering is required. */
11313static VEC(int) *
11314catch_syscall_split_args (char *arg)
11315{
11316 VEC(int) *result = NULL;
29d0bb3d 11317 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11318
11319 while (*arg != '\0')
11320 {
11321 int i, syscall_number;
11322 char *endptr;
11323 char cur_name[128];
11324 struct syscall s;
11325
11326 /* Skip whitespace. */
11327 while (isspace (*arg))
11328 arg++;
11329
11330 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11331 cur_name[i] = arg[i];
11332 cur_name[i] = '\0';
11333 arg += i;
11334
11335 /* Check if the user provided a syscall name or a number. */
11336 syscall_number = (int) strtol (cur_name, &endptr, 0);
11337 if (*endptr == '\0')
bccd0dd2 11338 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11339 else
11340 {
11341 /* We have a name. Let's check if it's valid and convert it
11342 to a number. */
11343 get_syscall_by_name (cur_name, &s);
11344
11345 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11346 /* Here we have to issue an error instead of a warning,
11347 because GDB cannot do anything useful if there's no
11348 syscall number to be caught. */
a96d9b2e
SDJ
11349 error (_("Unknown syscall name '%s'."), cur_name);
11350 }
11351
11352 /* Ok, it's valid. */
11353 VEC_safe_push (int, result, s.number);
11354 }
11355
11356 discard_cleanups (cleanup);
11357 return result;
11358}
11359
11360/* Implement the "catch syscall" command. */
11361
11362static void
cc59ec59
MS
11363catch_syscall_command_1 (char *arg, int from_tty,
11364 struct cmd_list_element *command)
a96d9b2e
SDJ
11365{
11366 int tempflag;
11367 VEC(int) *filter;
11368 struct syscall s;
11369 struct gdbarch *gdbarch = get_current_arch ();
11370
11371 /* Checking if the feature if supported. */
11372 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11373 error (_("The feature 'catch syscall' is not supported on \
ea666128 11374this architecture yet."));
a96d9b2e
SDJ
11375
11376 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11377
e9cafbcc 11378 arg = skip_spaces (arg);
a96d9b2e
SDJ
11379
11380 /* We need to do this first "dummy" translation in order
11381 to get the syscall XML file loaded or, most important,
11382 to display a warning to the user if there's no XML file
11383 for his/her architecture. */
11384 get_syscall_by_number (0, &s);
11385
11386 /* The allowed syntax is:
11387 catch syscall
11388 catch syscall <name | number> [<name | number> ... <name | number>]
11389
11390 Let's check if there's a syscall name. */
11391
11392 if (arg != NULL)
11393 filter = catch_syscall_split_args (arg);
11394 else
11395 filter = NULL;
11396
11397 create_syscall_event_catchpoint (tempflag, filter,
11398 &catch_syscall_breakpoint_ops);
11399}
11400
c906108c 11401static void
fba45db2 11402catch_command (char *arg, int from_tty)
c906108c 11403{
44feb3ce 11404 error (_("Catch requires an event name."));
c906108c
SS
11405}
11406\f
11407
11408static void
fba45db2 11409tcatch_command (char *arg, int from_tty)
c906108c 11410{
44feb3ce 11411 error (_("Catch requires an event name."));
c906108c
SS
11412}
11413
8a2c437b
TT
11414/* A qsort comparison function that sorts breakpoints in order. */
11415
11416static int
11417compare_breakpoints (const void *a, const void *b)
11418{
11419 const breakpoint_p *ba = a;
11420 uintptr_t ua = (uintptr_t) *ba;
11421 const breakpoint_p *bb = b;
11422 uintptr_t ub = (uintptr_t) *bb;
11423
11424 if ((*ba)->number < (*bb)->number)
11425 return -1;
11426 else if ((*ba)->number > (*bb)->number)
11427 return 1;
11428
11429 /* Now sort by address, in case we see, e..g, two breakpoints with
11430 the number 0. */
11431 if (ua < ub)
11432 return -1;
11433 return ub > ub ? 1 : 0;
11434}
11435
80f8a6eb 11436/* Delete breakpoints by address or line. */
c906108c
SS
11437
11438static void
fba45db2 11439clear_command (char *arg, int from_tty)
c906108c 11440{
8a2c437b 11441 struct breakpoint *b, *prev;
d6e956e5
VP
11442 VEC(breakpoint_p) *found = 0;
11443 int ix;
c906108c
SS
11444 int default_match;
11445 struct symtabs_and_lines sals;
11446 struct symtab_and_line sal;
c906108c 11447 int i;
8a2c437b 11448 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11449
11450 if (arg)
11451 {
f8eba3c6
TT
11452 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11453 | DECODE_LINE_LIST_MODE));
c906108c
SS
11454 default_match = 0;
11455 }
11456 else
11457 {
c5aa993b 11458 sals.sals = (struct symtab_and_line *)
c906108c 11459 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11460 make_cleanup (xfree, sals.sals);
4a64f543 11461 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11462
11463 /* Set sal's line, symtab, pc, and pspace to the values
11464 corresponding to the last call to print_frame_info. If the
11465 codepoint is not valid, this will set all the fields to 0. */
11466 get_last_displayed_sal (&sal);
c906108c 11467 if (sal.symtab == 0)
8a3fe4f8 11468 error (_("No source file specified."));
c906108c
SS
11469
11470 sals.sals[0] = sal;
11471 sals.nelts = 1;
11472
11473 default_match = 1;
11474 }
11475
4a64f543
MS
11476 /* We don't call resolve_sal_pc here. That's not as bad as it
11477 seems, because all existing breakpoints typically have both
11478 file/line and pc set. So, if clear is given file/line, we can
11479 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11480
11481 We only support clearing given the address explicitly
11482 present in breakpoint table. Say, we've set breakpoint
4a64f543 11483 at file:line. There were several PC values for that file:line,
ed0616c6 11484 due to optimization, all in one block.
4a64f543
MS
11485
11486 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11487 PC corresponding to the same file:line, the breakpoint won't
11488 be cleared. We probably can still clear the breakpoint, but
11489 since the other PC value is never presented to user, user
11490 can only find it by guessing, and it does not seem important
11491 to support that. */
11492
4a64f543
MS
11493 /* For each line spec given, delete bps which correspond to it. Do
11494 it in two passes, solely to preserve the current behavior that
11495 from_tty is forced true if we delete more than one
11496 breakpoint. */
c906108c 11497
80f8a6eb 11498 found = NULL;
8a2c437b 11499 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11500 for (i = 0; i < sals.nelts; i++)
11501 {
4aac40c8
TT
11502 int is_abs, sal_name_len;
11503
c906108c 11504 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11505 If line given (pc == 0), clear all bpts on specified line.
11506 If defaulting, clear all bpts on default line
c906108c 11507 or at default pc.
c5aa993b
JM
11508
11509 defaulting sal.pc != 0 tests to do
11510
11511 0 1 pc
11512 1 1 pc _and_ line
11513 0 0 line
11514 1 0 <can't happen> */
c906108c
SS
11515
11516 sal = sals.sals[i];
4aac40c8
TT
11517 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11518 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 11519
4a64f543 11520 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11521 ALL_BREAKPOINTS (b)
c5aa993b 11522 {
0d381245 11523 int match = 0;
4a64f543 11524 /* Are we going to delete b? */
cc60f2e3 11525 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11526 {
11527 struct bp_location *loc = b->loc;
11528 for (; loc; loc = loc->next)
11529 {
f8eba3c6
TT
11530 /* If the user specified file:line, don't allow a PC
11531 match. This matches historical gdb behavior. */
11532 int pc_match = (!sal.explicit_line
11533 && sal.pc
11534 && (loc->pspace == sal.pspace)
11535 && (loc->address == sal.pc)
11536 && (!section_is_overlay (loc->section)
11537 || loc->section == sal.section));
4aac40c8
TT
11538 int line_match = 0;
11539
11540 if ((default_match || sal.explicit_line)
11541 && loc->source_file != NULL
11542 && sal.symtab != NULL
11543 && sal.pspace == loc->pspace
11544 && loc->line_number == sal.line)
11545 {
11546 if (filename_cmp (loc->source_file,
11547 sal.symtab->filename) == 0)
11548 line_match = 1;
11549 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11550 && compare_filenames_for_search (loc->source_file,
11551 sal.symtab->filename,
11552 sal_name_len))
11553 line_match = 1;
11554 }
11555
0d381245
VP
11556 if (pc_match || line_match)
11557 {
11558 match = 1;
11559 break;
11560 }
11561 }
11562 }
11563
11564 if (match)
d6e956e5 11565 VEC_safe_push(breakpoint_p, found, b);
c906108c 11566 }
80f8a6eb 11567 }
8a2c437b 11568
80f8a6eb 11569 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11570 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11571 {
11572 if (arg)
8a3fe4f8 11573 error (_("No breakpoint at %s."), arg);
80f8a6eb 11574 else
8a3fe4f8 11575 error (_("No breakpoint at this line."));
80f8a6eb 11576 }
c906108c 11577
8a2c437b
TT
11578 /* Remove duplicates from the vec. */
11579 qsort (VEC_address (breakpoint_p, found),
11580 VEC_length (breakpoint_p, found),
11581 sizeof (breakpoint_p),
11582 compare_breakpoints);
11583 prev = VEC_index (breakpoint_p, found, 0);
11584 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11585 {
11586 if (b == prev)
11587 {
11588 VEC_ordered_remove (breakpoint_p, found, ix);
11589 --ix;
11590 }
11591 }
11592
d6e956e5 11593 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11594 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11595 if (from_tty)
a3f17187 11596 {
d6e956e5 11597 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11598 printf_unfiltered (_("Deleted breakpoint "));
11599 else
11600 printf_unfiltered (_("Deleted breakpoints "));
11601 }
80f8a6eb 11602 breakpoints_changed ();
d6e956e5
VP
11603
11604 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11605 {
c5aa993b 11606 if (from_tty)
d6e956e5
VP
11607 printf_unfiltered ("%d ", b->number);
11608 delete_breakpoint (b);
c906108c 11609 }
80f8a6eb
MS
11610 if (from_tty)
11611 putchar_unfiltered ('\n');
8a2c437b
TT
11612
11613 do_cleanups (cleanups);
c906108c
SS
11614}
11615\f
11616/* Delete breakpoint in BS if they are `delete' breakpoints and
11617 all breakpoints that are marked for deletion, whether hit or not.
11618 This is called after any breakpoint is hit, or after errors. */
11619
11620void
fba45db2 11621breakpoint_auto_delete (bpstat bs)
c906108c 11622{
35df4500 11623 struct breakpoint *b, *b_tmp;
c906108c
SS
11624
11625 for (; bs; bs = bs->next)
f431efe5
PA
11626 if (bs->breakpoint_at
11627 && bs->breakpoint_at->disposition == disp_del
c906108c 11628 && bs->stop)
f431efe5 11629 delete_breakpoint (bs->breakpoint_at);
c906108c 11630
35df4500 11631 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11632 {
b5de0fa7 11633 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11634 delete_breakpoint (b);
11635 }
c906108c
SS
11636}
11637
4a64f543
MS
11638/* A comparison function for bp_location AP and BP being interfaced to
11639 qsort. Sort elements primarily by their ADDRESS (no matter what
11640 does breakpoint_address_is_meaningful say for its OWNER),
11641 secondarily by ordering first bp_permanent OWNERed elements and
11642 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11643 qsort being an unstable algorithm. */
876fa593
JK
11644
11645static int
494cfb0f 11646bp_location_compare (const void *ap, const void *bp)
876fa593 11647{
494cfb0f
JK
11648 struct bp_location *a = *(void **) ap;
11649 struct bp_location *b = *(void **) bp;
2bdf28a0 11650 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11651 int a_perm = a->owner->enable_state == bp_permanent;
11652 int b_perm = b->owner->enable_state == bp_permanent;
11653
11654 if (a->address != b->address)
11655 return (a->address > b->address) - (a->address < b->address);
11656
dea2aa5f
LM
11657 /* Sort locations at the same address by their pspace number, keeping
11658 locations of the same inferior (in a multi-inferior environment)
11659 grouped. */
11660
11661 if (a->pspace->num != b->pspace->num)
11662 return ((a->pspace->num > b->pspace->num)
11663 - (a->pspace->num < b->pspace->num));
11664
876fa593
JK
11665 /* Sort permanent breakpoints first. */
11666 if (a_perm != b_perm)
11667 return (a_perm < b_perm) - (a_perm > b_perm);
11668
c56a97f9
JK
11669 /* Make the internal GDB representation stable across GDB runs
11670 where A and B memory inside GDB can differ. Breakpoint locations of
11671 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11672
11673 if (a->owner->number != b->owner->number)
c56a97f9
JK
11674 return ((a->owner->number > b->owner->number)
11675 - (a->owner->number < b->owner->number));
876fa593
JK
11676
11677 return (a > b) - (a < b);
11678}
11679
876fa593 11680/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11681 bp_location_shadow_len_after_address_max according to the current
11682 content of the bp_location array. */
f7545552
TT
11683
11684static void
876fa593 11685bp_location_target_extensions_update (void)
f7545552 11686{
876fa593
JK
11687 struct bp_location *bl, **blp_tmp;
11688
11689 bp_location_placed_address_before_address_max = 0;
11690 bp_location_shadow_len_after_address_max = 0;
11691
11692 ALL_BP_LOCATIONS (bl, blp_tmp)
11693 {
11694 CORE_ADDR start, end, addr;
11695
11696 if (!bp_location_has_shadow (bl))
11697 continue;
11698
11699 start = bl->target_info.placed_address;
11700 end = start + bl->target_info.shadow_len;
11701
11702 gdb_assert (bl->address >= start);
11703 addr = bl->address - start;
11704 if (addr > bp_location_placed_address_before_address_max)
11705 bp_location_placed_address_before_address_max = addr;
11706
11707 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11708
11709 gdb_assert (bl->address < end);
11710 addr = end - bl->address;
11711 if (addr > bp_location_shadow_len_after_address_max)
11712 bp_location_shadow_len_after_address_max = addr;
11713 }
f7545552
TT
11714}
11715
1e4d1764
YQ
11716/* Download tracepoint locations if they haven't been. */
11717
11718static void
11719download_tracepoint_locations (void)
11720{
11721 struct bp_location *bl, **blp_tmp;
11722 struct cleanup *old_chain;
11723
11724 if (!target_can_download_tracepoint ())
11725 return;
11726
11727 old_chain = save_current_space_and_thread ();
11728
11729 ALL_BP_LOCATIONS (bl, blp_tmp)
11730 {
11731 struct tracepoint *t;
11732
11733 if (!is_tracepoint (bl->owner))
11734 continue;
11735
11736 if ((bl->owner->type == bp_fast_tracepoint
11737 ? !may_insert_fast_tracepoints
11738 : !may_insert_tracepoints))
11739 continue;
11740
11741 /* In tracepoint, locations are _never_ duplicated, so
11742 should_be_inserted is equivalent to
11743 unduplicated_should_be_inserted. */
11744 if (!should_be_inserted (bl) || bl->inserted)
11745 continue;
11746
11747 switch_to_program_space_and_thread (bl->pspace);
11748
11749 target_download_tracepoint (bl);
11750
11751 bl->inserted = 1;
11752 t = (struct tracepoint *) bl->owner;
11753 t->number_on_target = bl->owner->number;
11754 }
11755
11756 do_cleanups (old_chain);
11757}
11758
934709f0
PW
11759/* Swap the insertion/duplication state between two locations. */
11760
11761static void
11762swap_insertion (struct bp_location *left, struct bp_location *right)
11763{
11764 const int left_inserted = left->inserted;
11765 const int left_duplicate = left->duplicate;
b775012e 11766 const int left_needs_update = left->needs_update;
934709f0
PW
11767 const struct bp_target_info left_target_info = left->target_info;
11768
1e4d1764
YQ
11769 /* Locations of tracepoints can never be duplicated. */
11770 if (is_tracepoint (left->owner))
11771 gdb_assert (!left->duplicate);
11772 if (is_tracepoint (right->owner))
11773 gdb_assert (!right->duplicate);
11774
934709f0
PW
11775 left->inserted = right->inserted;
11776 left->duplicate = right->duplicate;
b775012e 11777 left->needs_update = right->needs_update;
934709f0
PW
11778 left->target_info = right->target_info;
11779 right->inserted = left_inserted;
11780 right->duplicate = left_duplicate;
b775012e 11781 right->needs_update = left_needs_update;
934709f0
PW
11782 right->target_info = left_target_info;
11783}
11784
b775012e
LM
11785/* Force the re-insertion of the locations at ADDRESS. This is called
11786 once a new/deleted/modified duplicate location is found and we are evaluating
11787 conditions on the target's side. Such conditions need to be updated on
11788 the target. */
11789
11790static void
11791force_breakpoint_reinsertion (struct bp_location *bl)
11792{
11793 struct bp_location **locp = NULL, **loc2p;
11794 struct bp_location *loc;
11795 CORE_ADDR address = 0;
11796 int pspace_num;
11797
11798 address = bl->address;
11799 pspace_num = bl->pspace->num;
11800
11801 /* This is only meaningful if the target is
11802 evaluating conditions and if the user has
11803 opted for condition evaluation on the target's
11804 side. */
11805 if (gdb_evaluates_breakpoint_condition_p ()
11806 || !target_supports_evaluation_of_breakpoint_conditions ())
11807 return;
11808
11809 /* Flag all breakpoint locations with this address and
11810 the same program space as the location
11811 as "its condition has changed". We need to
11812 update the conditions on the target's side. */
11813 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11814 {
11815 loc = *loc2p;
11816
11817 if (!is_breakpoint (loc->owner)
11818 || pspace_num != loc->pspace->num)
11819 continue;
11820
11821 /* Flag the location appropriately. We use a different state to
11822 let everyone know that we already updated the set of locations
11823 with addr bl->address and program space bl->pspace. This is so
11824 we don't have to keep calling these functions just to mark locations
11825 that have already been marked. */
11826 loc->condition_changed = condition_updated;
11827
11828 /* Free the agent expression bytecode as well. We will compute
11829 it later on. */
11830 if (loc->cond_bytecode)
11831 {
11832 free_agent_expr (loc->cond_bytecode);
11833 loc->cond_bytecode = NULL;
11834 }
11835 }
11836}
11837
4cd9bd08 11838/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
11839 into the inferior, only remove already-inserted locations that no
11840 longer should be inserted. Functions that delete a breakpoint or
11841 breakpoints should pass false, so that deleting a breakpoint
11842 doesn't have the side effect of inserting the locations of other
11843 breakpoints that are marked not-inserted, but should_be_inserted
11844 returns true on them.
11845
11846 This behaviour is useful is situations close to tear-down -- e.g.,
11847 after an exec, while the target still has execution, but breakpoint
11848 shadows of the previous executable image should *NOT* be restored
11849 to the new image; or before detaching, where the target still has
11850 execution and wants to delete breakpoints from GDB's lists, and all
11851 breakpoints had already been removed from the inferior. */
11852
0d381245 11853static void
b60e7edf 11854update_global_location_list (int should_insert)
0d381245 11855{
74960c60 11856 struct breakpoint *b;
876fa593 11857 struct bp_location **locp, *loc;
f7545552 11858 struct cleanup *cleanups;
b775012e
LM
11859 /* Last breakpoint location address that was marked for update. */
11860 CORE_ADDR last_addr = 0;
11861 /* Last breakpoint location program space that was marked for update. */
11862 int last_pspace_num = -1;
f7545552 11863
2d134ed3
PA
11864 /* Used in the duplicates detection below. When iterating over all
11865 bp_locations, points to the first bp_location of a given address.
11866 Breakpoints and watchpoints of different types are never
11867 duplicates of each other. Keep one pointer for each type of
11868 breakpoint/watchpoint, so we only need to loop over all locations
11869 once. */
11870 struct bp_location *bp_loc_first; /* breakpoint */
11871 struct bp_location *wp_loc_first; /* hardware watchpoint */
11872 struct bp_location *awp_loc_first; /* access watchpoint */
11873 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11874
4a64f543
MS
11875 /* Saved former bp_location array which we compare against the newly
11876 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
11877 struct bp_location **old_location, **old_locp;
11878 unsigned old_location_count;
11879
11880 old_location = bp_location;
11881 old_location_count = bp_location_count;
11882 bp_location = NULL;
11883 bp_location_count = 0;
11884 cleanups = make_cleanup (xfree, old_location);
0d381245 11885
74960c60 11886 ALL_BREAKPOINTS (b)
876fa593
JK
11887 for (loc = b->loc; loc; loc = loc->next)
11888 bp_location_count++;
11889
11890 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11891 locp = bp_location;
11892 ALL_BREAKPOINTS (b)
11893 for (loc = b->loc; loc; loc = loc->next)
11894 *locp++ = loc;
11895 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 11896 bp_location_compare);
876fa593
JK
11897
11898 bp_location_target_extensions_update ();
74960c60 11899
4a64f543
MS
11900 /* Identify bp_location instances that are no longer present in the
11901 new list, and therefore should be freed. Note that it's not
11902 necessary that those locations should be removed from inferior --
11903 if there's another location at the same address (previously
11904 marked as duplicate), we don't need to remove/insert the
11905 location.
876fa593 11906
4a64f543
MS
11907 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11908 and former bp_location array state respectively. */
876fa593
JK
11909
11910 locp = bp_location;
11911 for (old_locp = old_location; old_locp < old_location + old_location_count;
11912 old_locp++)
74960c60 11913 {
876fa593 11914 struct bp_location *old_loc = *old_locp;
c7d46a38 11915 struct bp_location **loc2p;
876fa593 11916
e5dd4106 11917 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11918 not, we have to free it. */
c7d46a38 11919 int found_object = 0;
20874c92
VP
11920 /* Tells if the location should remain inserted in the target. */
11921 int keep_in_target = 0;
11922 int removed = 0;
876fa593 11923
4a64f543
MS
11924 /* Skip LOCP entries which will definitely never be needed.
11925 Stop either at or being the one matching OLD_LOC. */
876fa593 11926 while (locp < bp_location + bp_location_count
c7d46a38 11927 && (*locp)->address < old_loc->address)
876fa593 11928 locp++;
c7d46a38
PA
11929
11930 for (loc2p = locp;
11931 (loc2p < bp_location + bp_location_count
11932 && (*loc2p)->address == old_loc->address);
11933 loc2p++)
11934 {
b775012e
LM
11935 /* Check if this is a new/duplicated location or a duplicated
11936 location that had its condition modified. If so, we want to send
11937 its condition to the target if evaluation of conditions is taking
11938 place there. */
11939 if ((*loc2p)->condition_changed == condition_modified
11940 && (last_addr != old_loc->address
11941 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11942 {
b775012e
LM
11943 force_breakpoint_reinsertion (*loc2p);
11944 last_pspace_num = old_loc->pspace->num;
c7d46a38 11945 }
b775012e
LM
11946
11947 if (*loc2p == old_loc)
11948 found_object = 1;
c7d46a38 11949 }
74960c60 11950
b775012e
LM
11951 /* We have already handled this address, update it so that we don't
11952 have to go through updates again. */
11953 last_addr = old_loc->address;
11954
11955 /* Target-side condition evaluation: Handle deleted locations. */
11956 if (!found_object)
11957 force_breakpoint_reinsertion (old_loc);
11958
4a64f543
MS
11959 /* If this location is no longer present, and inserted, look if
11960 there's maybe a new location at the same address. If so,
11961 mark that one inserted, and don't remove this one. This is
11962 needed so that we don't have a time window where a breakpoint
11963 at certain location is not inserted. */
74960c60 11964
876fa593 11965 if (old_loc->inserted)
0d381245 11966 {
4a64f543
MS
11967 /* If the location is inserted now, we might have to remove
11968 it. */
74960c60 11969
876fa593 11970 if (found_object && should_be_inserted (old_loc))
74960c60 11971 {
4a64f543
MS
11972 /* The location is still present in the location list,
11973 and still should be inserted. Don't do anything. */
20874c92 11974 keep_in_target = 1;
74960c60
VP
11975 }
11976 else
11977 {
b775012e
LM
11978 /* This location still exists, but it won't be kept in the
11979 target since it may have been disabled. We proceed to
11980 remove its target-side condition. */
11981
4a64f543
MS
11982 /* The location is either no longer present, or got
11983 disabled. See if there's another location at the
11984 same address, in which case we don't need to remove
11985 this one from the target. */
876fa593 11986
2bdf28a0 11987 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
11988 if (breakpoint_address_is_meaningful (old_loc->owner))
11989 {
876fa593 11990 for (loc2p = locp;
c7d46a38
PA
11991 (loc2p < bp_location + bp_location_count
11992 && (*loc2p)->address == old_loc->address);
876fa593
JK
11993 loc2p++)
11994 {
11995 struct bp_location *loc2 = *loc2p;
11996
2d134ed3 11997 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11998 {
85d721b8
PA
11999 /* Read watchpoint locations are switched to
12000 access watchpoints, if the former are not
12001 supported, but the latter are. */
12002 if (is_hardware_watchpoint (old_loc->owner))
12003 {
12004 gdb_assert (is_hardware_watchpoint (loc2->owner));
12005 loc2->watchpoint_type = old_loc->watchpoint_type;
12006 }
12007
934709f0
PW
12008 /* loc2 is a duplicated location. We need to check
12009 if it should be inserted in case it will be
12010 unduplicated. */
12011 if (loc2 != old_loc
12012 && unduplicated_should_be_inserted (loc2))
c7d46a38 12013 {
934709f0 12014 swap_insertion (old_loc, loc2);
c7d46a38
PA
12015 keep_in_target = 1;
12016 break;
12017 }
876fa593
JK
12018 }
12019 }
12020 }
74960c60
VP
12021 }
12022
20874c92
VP
12023 if (!keep_in_target)
12024 {
876fa593 12025 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12026 {
4a64f543
MS
12027 /* This is just about all we can do. We could keep
12028 this location on the global list, and try to
12029 remove it next time, but there's no particular
12030 reason why we will succeed next time.
20874c92 12031
4a64f543
MS
12032 Note that at this point, old_loc->owner is still
12033 valid, as delete_breakpoint frees the breakpoint
12034 only after calling us. */
3e43a32a
MS
12035 printf_filtered (_("warning: Error removing "
12036 "breakpoint %d\n"),
876fa593 12037 old_loc->owner->number);
20874c92
VP
12038 }
12039 removed = 1;
12040 }
0d381245 12041 }
74960c60
VP
12042
12043 if (!found_object)
1c5cfe86 12044 {
db82e815
PA
12045 if (removed && non_stop
12046 && breakpoint_address_is_meaningful (old_loc->owner)
12047 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12048 {
db82e815
PA
12049 /* This location was removed from the target. In
12050 non-stop mode, a race condition is possible where
12051 we've removed a breakpoint, but stop events for that
12052 breakpoint are already queued and will arrive later.
12053 We apply an heuristic to be able to distinguish such
12054 SIGTRAPs from other random SIGTRAPs: we keep this
12055 breakpoint location for a bit, and will retire it
12056 after we see some number of events. The theory here
12057 is that reporting of events should, "on the average",
12058 be fair, so after a while we'll see events from all
12059 threads that have anything of interest, and no longer
12060 need to keep this breakpoint location around. We
12061 don't hold locations forever so to reduce chances of
12062 mistaking a non-breakpoint SIGTRAP for a breakpoint
12063 SIGTRAP.
12064
12065 The heuristic failing can be disastrous on
12066 decr_pc_after_break targets.
12067
12068 On decr_pc_after_break targets, like e.g., x86-linux,
12069 if we fail to recognize a late breakpoint SIGTRAP,
12070 because events_till_retirement has reached 0 too
12071 soon, we'll fail to do the PC adjustment, and report
12072 a random SIGTRAP to the user. When the user resumes
12073 the inferior, it will most likely immediately crash
2dec564e 12074 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12075 corrupted, because of being resumed e.g., in the
12076 middle of a multi-byte instruction, or skipped a
12077 one-byte instruction. This was actually seen happen
12078 on native x86-linux, and should be less rare on
12079 targets that do not support new thread events, like
12080 remote, due to the heuristic depending on
12081 thread_count.
12082
12083 Mistaking a random SIGTRAP for a breakpoint trap
12084 causes similar symptoms (PC adjustment applied when
12085 it shouldn't), but then again, playing with SIGTRAPs
12086 behind the debugger's back is asking for trouble.
12087
12088 Since hardware watchpoint traps are always
12089 distinguishable from other traps, so we don't need to
12090 apply keep hardware watchpoint moribund locations
12091 around. We simply always ignore hardware watchpoint
12092 traps we can no longer explain. */
12093
876fa593
JK
12094 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12095 old_loc->owner = NULL;
20874c92 12096
876fa593 12097 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12098 }
12099 else
f431efe5
PA
12100 {
12101 old_loc->owner = NULL;
12102 decref_bp_location (&old_loc);
12103 }
20874c92 12104 }
74960c60 12105 }
1c5cfe86 12106
348d480f
PA
12107 /* Rescan breakpoints at the same address and section, marking the
12108 first one as "first" and any others as "duplicates". This is so
12109 that the bpt instruction is only inserted once. If we have a
12110 permanent breakpoint at the same place as BPT, make that one the
12111 official one, and the rest as duplicates. Permanent breakpoints
12112 are sorted first for the same address.
12113
12114 Do the same for hardware watchpoints, but also considering the
12115 watchpoint's type (regular/access/read) and length. */
12116
12117 bp_loc_first = NULL;
12118 wp_loc_first = NULL;
12119 awp_loc_first = NULL;
12120 rwp_loc_first = NULL;
12121 ALL_BP_LOCATIONS (loc, locp)
12122 {
12123 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12124 non-NULL. */
348d480f 12125 struct bp_location **loc_first_p;
d3fbdd86 12126 b = loc->owner;
348d480f 12127
f8eba3c6 12128 if (!should_be_inserted (loc)
348d480f 12129 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12130 /* Don't detect duplicate for tracepoint locations because they are
12131 never duplicated. See the comments in field `duplicate' of
12132 `struct bp_location'. */
348d480f 12133 || is_tracepoint (b))
b775012e
LM
12134 {
12135 /* Clear the condition modification flag. */
12136 loc->condition_changed = condition_unchanged;
12137 continue;
12138 }
348d480f
PA
12139
12140 /* Permanent breakpoint should always be inserted. */
12141 if (b->enable_state == bp_permanent && ! loc->inserted)
12142 internal_error (__FILE__, __LINE__,
12143 _("allegedly permanent breakpoint is not "
12144 "actually inserted"));
12145
12146 if (b->type == bp_hardware_watchpoint)
12147 loc_first_p = &wp_loc_first;
12148 else if (b->type == bp_read_watchpoint)
12149 loc_first_p = &rwp_loc_first;
12150 else if (b->type == bp_access_watchpoint)
12151 loc_first_p = &awp_loc_first;
12152 else
12153 loc_first_p = &bp_loc_first;
12154
12155 if (*loc_first_p == NULL
12156 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12157 || !breakpoint_locations_match (loc, *loc_first_p))
12158 {
12159 *loc_first_p = loc;
12160 loc->duplicate = 0;
b775012e
LM
12161
12162 if (is_breakpoint (loc->owner) && loc->condition_changed)
12163 {
12164 loc->needs_update = 1;
12165 /* Clear the condition modification flag. */
12166 loc->condition_changed = condition_unchanged;
12167 }
348d480f
PA
12168 continue;
12169 }
12170
934709f0
PW
12171
12172 /* This and the above ensure the invariant that the first location
12173 is not duplicated, and is the inserted one.
12174 All following are marked as duplicated, and are not inserted. */
12175 if (loc->inserted)
12176 swap_insertion (loc, *loc_first_p);
348d480f
PA
12177 loc->duplicate = 1;
12178
b775012e
LM
12179 /* Clear the condition modification flag. */
12180 loc->condition_changed = condition_unchanged;
12181
348d480f
PA
12182 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12183 && b->enable_state != bp_permanent)
12184 internal_error (__FILE__, __LINE__,
12185 _("another breakpoint was inserted on top of "
12186 "a permanent breakpoint"));
12187 }
12188
b775012e 12189 if (breakpoints_always_inserted_mode ()
348d480f
PA
12190 && (have_live_inferiors ()
12191 || (gdbarch_has_global_breakpoints (target_gdbarch))))
b775012e
LM
12192 {
12193 if (should_insert)
12194 insert_breakpoint_locations ();
12195 else
12196 {
12197 /* Though should_insert is false, we may need to update conditions
12198 on the target's side if it is evaluating such conditions. We
12199 only update conditions for locations that are marked
12200 "needs_update". */
12201 update_inserted_breakpoint_locations ();
12202 }
12203 }
348d480f 12204
1e4d1764
YQ
12205 if (should_insert)
12206 download_tracepoint_locations ();
12207
348d480f
PA
12208 do_cleanups (cleanups);
12209}
12210
12211void
12212breakpoint_retire_moribund (void)
12213{
12214 struct bp_location *loc;
12215 int ix;
12216
12217 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12218 if (--(loc->events_till_retirement) == 0)
12219 {
12220 decref_bp_location (&loc);
12221 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12222 --ix;
12223 }
12224}
12225
12226static void
12227update_global_location_list_nothrow (int inserting)
12228{
bfd189b1 12229 volatile struct gdb_exception e;
348d480f
PA
12230
12231 TRY_CATCH (e, RETURN_MASK_ERROR)
12232 update_global_location_list (inserting);
12233}
12234
12235/* Clear BKP from a BPS. */
12236
12237static void
12238bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12239{
12240 bpstat bs;
12241
12242 for (bs = bps; bs; bs = bs->next)
12243 if (bs->breakpoint_at == bpt)
12244 {
12245 bs->breakpoint_at = NULL;
12246 bs->old_val = NULL;
12247 /* bs->commands will be freed later. */
12248 }
12249}
12250
12251/* Callback for iterate_over_threads. */
12252static int
12253bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12254{
12255 struct breakpoint *bpt = data;
12256
12257 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12258 return 0;
12259}
12260
12261/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12262 callbacks. */
12263
12264static void
12265say_where (struct breakpoint *b)
12266{
79a45e25 12267 struct ui_out *uiout = current_uiout;
348d480f
PA
12268 struct value_print_options opts;
12269
12270 get_user_print_options (&opts);
12271
12272 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12273 single string. */
12274 if (b->loc == NULL)
12275 {
12276 printf_filtered (_(" (%s) pending."), b->addr_string);
12277 }
12278 else
12279 {
f8eba3c6 12280 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
12281 {
12282 printf_filtered (" at ");
12283 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12284 gdb_stdout);
12285 }
f8eba3c6
TT
12286 if (b->loc->source_file)
12287 {
12288 /* If there is a single location, we can print the location
12289 more nicely. */
12290 if (b->loc->next == NULL)
12291 printf_filtered (": file %s, line %d.",
12292 b->loc->source_file, b->loc->line_number);
12293 else
12294 /* This is not ideal, but each location may have a
12295 different file name, and this at least reflects the
12296 real situation somewhat. */
12297 printf_filtered (": %s.", b->addr_string);
12298 }
348d480f
PA
12299
12300 if (b->loc->next)
12301 {
12302 struct bp_location *loc = b->loc;
12303 int n = 0;
12304 for (; loc; loc = loc->next)
12305 ++n;
12306 printf_filtered (" (%d locations)", n);
12307 }
12308 }
12309}
12310
348d480f
PA
12311/* Default bp_location_ops methods. */
12312
12313static void
12314bp_location_dtor (struct bp_location *self)
12315{
12316 xfree (self->cond);
b775012e
LM
12317 if (self->cond_bytecode)
12318 free_agent_expr (self->cond_bytecode);
348d480f 12319 xfree (self->function_name);
f8eba3c6 12320 xfree (self->source_file);
348d480f
PA
12321}
12322
12323static const struct bp_location_ops bp_location_ops =
12324{
12325 bp_location_dtor
12326};
12327
2060206e
PA
12328/* Default breakpoint_ops methods all breakpoint_ops ultimately
12329 inherit from. */
348d480f 12330
2060206e
PA
12331static void
12332base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12333{
12334 decref_counted_command_line (&self->commands);
12335 xfree (self->cond_string);
348d480f 12336 xfree (self->addr_string);
f8eba3c6 12337 xfree (self->filter);
348d480f 12338 xfree (self->addr_string_range_end);
348d480f
PA
12339}
12340
2060206e
PA
12341static struct bp_location *
12342base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12343{
12344 struct bp_location *loc;
12345
12346 loc = XNEW (struct bp_location);
12347 init_bp_location (loc, &bp_location_ops, self);
12348 return loc;
12349}
12350
2060206e
PA
12351static void
12352base_breakpoint_re_set (struct breakpoint *b)
12353{
12354 /* Nothing to re-set. */
12355}
12356
12357#define internal_error_pure_virtual_called() \
12358 gdb_assert_not_reached ("pure virtual function called")
12359
12360static int
12361base_breakpoint_insert_location (struct bp_location *bl)
12362{
12363 internal_error_pure_virtual_called ();
12364}
12365
12366static int
12367base_breakpoint_remove_location (struct bp_location *bl)
12368{
12369 internal_error_pure_virtual_called ();
12370}
12371
12372static int
12373base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12374 struct address_space *aspace,
09ac7c10
TT
12375 CORE_ADDR bp_addr,
12376 const struct target_waitstatus *ws)
2060206e
PA
12377{
12378 internal_error_pure_virtual_called ();
12379}
12380
12381static void
12382base_breakpoint_check_status (bpstat bs)
12383{
12384 /* Always stop. */
12385}
12386
12387/* A "works_in_software_mode" breakpoint_ops method that just internal
12388 errors. */
12389
12390static int
12391base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12392{
12393 internal_error_pure_virtual_called ();
12394}
12395
12396/* A "resources_needed" breakpoint_ops method that just internal
12397 errors. */
12398
12399static int
12400base_breakpoint_resources_needed (const struct bp_location *bl)
12401{
12402 internal_error_pure_virtual_called ();
12403}
12404
12405static enum print_stop_action
12406base_breakpoint_print_it (bpstat bs)
12407{
12408 internal_error_pure_virtual_called ();
12409}
12410
12411static void
12412base_breakpoint_print_one_detail (const struct breakpoint *self,
12413 struct ui_out *uiout)
12414{
12415 /* nothing */
12416}
12417
12418static void
12419base_breakpoint_print_mention (struct breakpoint *b)
12420{
12421 internal_error_pure_virtual_called ();
12422}
12423
12424static void
12425base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12426{
12427 internal_error_pure_virtual_called ();
12428}
12429
983af33b
SDJ
12430static void
12431base_breakpoint_create_sals_from_address (char **arg,
12432 struct linespec_result *canonical,
12433 enum bptype type_wanted,
12434 char *addr_start,
12435 char **copy_arg)
12436{
12437 internal_error_pure_virtual_called ();
12438}
12439
12440static void
12441base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12442 struct linespec_result *c,
12443 struct linespec_sals *lsal,
12444 char *cond_string,
e7e0cddf 12445 char *extra_string,
983af33b
SDJ
12446 enum bptype type_wanted,
12447 enum bpdisp disposition,
12448 int thread,
12449 int task, int ignore_count,
12450 const struct breakpoint_ops *o,
12451 int from_tty, int enabled,
44f238bb 12452 int internal, unsigned flags)
983af33b
SDJ
12453{
12454 internal_error_pure_virtual_called ();
12455}
12456
12457static void
12458base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12459 struct symtabs_and_lines *sals)
12460{
12461 internal_error_pure_virtual_called ();
12462}
12463
2060206e
PA
12464static struct breakpoint_ops base_breakpoint_ops =
12465{
12466 base_breakpoint_dtor,
12467 base_breakpoint_allocate_location,
12468 base_breakpoint_re_set,
12469 base_breakpoint_insert_location,
12470 base_breakpoint_remove_location,
12471 base_breakpoint_breakpoint_hit,
12472 base_breakpoint_check_status,
12473 base_breakpoint_resources_needed,
12474 base_breakpoint_works_in_software_mode,
12475 base_breakpoint_print_it,
12476 NULL,
12477 base_breakpoint_print_one_detail,
12478 base_breakpoint_print_mention,
983af33b
SDJ
12479 base_breakpoint_print_recreate,
12480 base_breakpoint_create_sals_from_address,
12481 base_breakpoint_create_breakpoints_sal,
12482 base_breakpoint_decode_linespec,
2060206e
PA
12483};
12484
12485/* Default breakpoint_ops methods. */
12486
12487static void
348d480f
PA
12488bkpt_re_set (struct breakpoint *b)
12489{
06edf0c0
PA
12490 /* FIXME: is this still reachable? */
12491 if (b->addr_string == NULL)
12492 {
12493 /* Anything without a string can't be re-set. */
348d480f 12494 delete_breakpoint (b);
06edf0c0 12495 return;
348d480f 12496 }
06edf0c0
PA
12497
12498 breakpoint_re_set_default (b);
348d480f
PA
12499}
12500
2060206e 12501static int
348d480f
PA
12502bkpt_insert_location (struct bp_location *bl)
12503{
12504 if (bl->loc_type == bp_loc_hardware_breakpoint)
12505 return target_insert_hw_breakpoint (bl->gdbarch,
12506 &bl->target_info);
12507 else
12508 return target_insert_breakpoint (bl->gdbarch,
12509 &bl->target_info);
12510}
12511
2060206e 12512static int
348d480f
PA
12513bkpt_remove_location (struct bp_location *bl)
12514{
12515 if (bl->loc_type == bp_loc_hardware_breakpoint)
12516 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12517 else
12518 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12519}
12520
2060206e 12521static int
348d480f 12522bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12523 struct address_space *aspace, CORE_ADDR bp_addr,
12524 const struct target_waitstatus *ws)
348d480f
PA
12525{
12526 struct breakpoint *b = bl->owner;
12527
09ac7c10 12528 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12529 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12530 return 0;
12531
348d480f
PA
12532 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12533 aspace, bp_addr))
12534 return 0;
12535
12536 if (overlay_debugging /* unmapped overlay section */
12537 && section_is_overlay (bl->section)
12538 && !section_is_mapped (bl->section))
12539 return 0;
12540
12541 return 1;
12542}
12543
2060206e 12544static int
348d480f
PA
12545bkpt_resources_needed (const struct bp_location *bl)
12546{
12547 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12548
12549 return 1;
12550}
12551
2060206e 12552static enum print_stop_action
348d480f
PA
12553bkpt_print_it (bpstat bs)
12554{
348d480f
PA
12555 struct breakpoint *b;
12556 const struct bp_location *bl;
001c8c33 12557 int bp_temp;
79a45e25 12558 struct ui_out *uiout = current_uiout;
348d480f
PA
12559
12560 gdb_assert (bs->bp_location_at != NULL);
12561
12562 bl = bs->bp_location_at;
12563 b = bs->breakpoint_at;
12564
001c8c33
PA
12565 bp_temp = b->disposition == disp_del;
12566 if (bl->address != bl->requested_address)
12567 breakpoint_adjustment_warning (bl->requested_address,
12568 bl->address,
12569 b->number, 1);
12570 annotate_breakpoint (b->number);
12571 if (bp_temp)
12572 ui_out_text (uiout, "\nTemporary breakpoint ");
12573 else
12574 ui_out_text (uiout, "\nBreakpoint ");
12575 if (ui_out_is_mi_like_p (uiout))
348d480f 12576 {
001c8c33
PA
12577 ui_out_field_string (uiout, "reason",
12578 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12579 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12580 }
001c8c33
PA
12581 ui_out_field_int (uiout, "bkptno", b->number);
12582 ui_out_text (uiout, ", ");
06edf0c0 12583
001c8c33 12584 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12585}
12586
2060206e 12587static void
06edf0c0
PA
12588bkpt_print_mention (struct breakpoint *b)
12589{
79a45e25 12590 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12591 return;
12592
12593 switch (b->type)
12594 {
12595 case bp_breakpoint:
12596 case bp_gnu_ifunc_resolver:
12597 if (b->disposition == disp_del)
12598 printf_filtered (_("Temporary breakpoint"));
12599 else
12600 printf_filtered (_("Breakpoint"));
12601 printf_filtered (_(" %d"), b->number);
12602 if (b->type == bp_gnu_ifunc_resolver)
12603 printf_filtered (_(" at gnu-indirect-function resolver"));
12604 break;
12605 case bp_hardware_breakpoint:
12606 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12607 break;
e7e0cddf
SS
12608 case bp_dprintf:
12609 printf_filtered (_("Dprintf %d"), b->number);
12610 break;
06edf0c0
PA
12611 }
12612
12613 say_where (b);
12614}
12615
2060206e 12616static void
06edf0c0
PA
12617bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12618{
12619 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12620 fprintf_unfiltered (fp, "tbreak");
12621 else if (tp->type == bp_breakpoint)
12622 fprintf_unfiltered (fp, "break");
12623 else if (tp->type == bp_hardware_breakpoint
12624 && tp->disposition == disp_del)
12625 fprintf_unfiltered (fp, "thbreak");
12626 else if (tp->type == bp_hardware_breakpoint)
12627 fprintf_unfiltered (fp, "hbreak");
12628 else
12629 internal_error (__FILE__, __LINE__,
12630 _("unhandled breakpoint type %d"), (int) tp->type);
12631
2060206e 12632 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12633 print_recreate_thread (tp, fp);
06edf0c0
PA
12634}
12635
983af33b
SDJ
12636static void
12637bkpt_create_sals_from_address (char **arg,
12638 struct linespec_result *canonical,
12639 enum bptype type_wanted,
12640 char *addr_start, char **copy_arg)
12641{
12642 create_sals_from_address_default (arg, canonical, type_wanted,
12643 addr_start, copy_arg);
12644}
12645
12646static void
12647bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12648 struct linespec_result *canonical,
12649 struct linespec_sals *lsal,
12650 char *cond_string,
e7e0cddf 12651 char *extra_string,
983af33b
SDJ
12652 enum bptype type_wanted,
12653 enum bpdisp disposition,
12654 int thread,
12655 int task, int ignore_count,
12656 const struct breakpoint_ops *ops,
12657 int from_tty, int enabled,
44f238bb 12658 int internal, unsigned flags)
983af33b
SDJ
12659{
12660 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12661 cond_string, extra_string,
12662 type_wanted,
983af33b
SDJ
12663 disposition, thread, task,
12664 ignore_count, ops, from_tty,
44f238bb 12665 enabled, internal, flags);
983af33b
SDJ
12666}
12667
12668static void
12669bkpt_decode_linespec (struct breakpoint *b, char **s,
12670 struct symtabs_and_lines *sals)
12671{
12672 decode_linespec_default (b, s, sals);
12673}
12674
06edf0c0
PA
12675/* Virtual table for internal breakpoints. */
12676
12677static void
12678internal_bkpt_re_set (struct breakpoint *b)
12679{
12680 switch (b->type)
12681 {
12682 /* Delete overlay event and longjmp master breakpoints; they
12683 will be reset later by breakpoint_re_set. */
12684 case bp_overlay_event:
12685 case bp_longjmp_master:
12686 case bp_std_terminate_master:
12687 case bp_exception_master:
12688 delete_breakpoint (b);
12689 break;
12690
12691 /* This breakpoint is special, it's set up when the inferior
12692 starts and we really don't want to touch it. */
12693 case bp_shlib_event:
12694
12695 /* Like bp_shlib_event, this breakpoint type is special. Once
12696 it is set up, we do not want to touch it. */
12697 case bp_thread_event:
12698 break;
12699 }
12700}
12701
12702static void
12703internal_bkpt_check_status (bpstat bs)
12704{
a9b3a50f
PA
12705 if (bs->breakpoint_at->type == bp_shlib_event)
12706 {
12707 /* If requested, stop when the dynamic linker notifies GDB of
12708 events. This allows the user to get control and place
12709 breakpoints in initializer routines for dynamically loaded
12710 objects (among other things). */
12711 bs->stop = stop_on_solib_events;
12712 bs->print = stop_on_solib_events;
12713 }
12714 else
12715 bs->stop = 0;
06edf0c0
PA
12716}
12717
12718static enum print_stop_action
12719internal_bkpt_print_it (bpstat bs)
12720{
36dfb11c 12721 struct ui_out *uiout = current_uiout;
06edf0c0 12722 struct breakpoint *b;
06edf0c0 12723
06edf0c0
PA
12724 b = bs->breakpoint_at;
12725
06edf0c0
PA
12726 switch (b->type)
12727 {
348d480f
PA
12728 case bp_shlib_event:
12729 /* Did we stop because the user set the stop_on_solib_events
12730 variable? (If so, we report this as a generic, "Stopped due
12731 to shlib event" message.) */
edcc5120 12732 print_solib_event (0);
348d480f
PA
12733 break;
12734
12735 case bp_thread_event:
12736 /* Not sure how we will get here.
12737 GDB should not stop for these breakpoints. */
12738 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12739 break;
12740
12741 case bp_overlay_event:
12742 /* By analogy with the thread event, GDB should not stop for these. */
12743 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12744 break;
12745
12746 case bp_longjmp_master:
12747 /* These should never be enabled. */
12748 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12749 break;
12750
12751 case bp_std_terminate_master:
12752 /* These should never be enabled. */
12753 printf_filtered (_("std::terminate Master Breakpoint: "
12754 "gdb should not stop!\n"));
348d480f
PA
12755 break;
12756
12757 case bp_exception_master:
12758 /* These should never be enabled. */
12759 printf_filtered (_("Exception Master Breakpoint: "
12760 "gdb should not stop!\n"));
06edf0c0
PA
12761 break;
12762 }
12763
001c8c33 12764 return PRINT_NOTHING;
06edf0c0
PA
12765}
12766
12767static void
12768internal_bkpt_print_mention (struct breakpoint *b)
12769{
12770 /* Nothing to mention. These breakpoints are internal. */
12771}
12772
06edf0c0
PA
12773/* Virtual table for momentary breakpoints */
12774
12775static void
12776momentary_bkpt_re_set (struct breakpoint *b)
12777{
12778 /* Keep temporary breakpoints, which can be encountered when we step
12779 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12780 Otherwise these should have been blown away via the cleanup chain
12781 or by breakpoint_init_inferior when we rerun the executable. */
12782}
12783
12784static void
12785momentary_bkpt_check_status (bpstat bs)
12786{
12787 /* Nothing. The point of these breakpoints is causing a stop. */
12788}
12789
12790static enum print_stop_action
12791momentary_bkpt_print_it (bpstat bs)
12792{
79a45e25
PA
12793 struct ui_out *uiout = current_uiout;
12794
001c8c33 12795 if (ui_out_is_mi_like_p (uiout))
06edf0c0 12796 {
001c8c33 12797 struct breakpoint *b = bs->breakpoint_at;
348d480f 12798
001c8c33
PA
12799 switch (b->type)
12800 {
12801 case bp_finish:
12802 ui_out_field_string
12803 (uiout, "reason",
12804 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12805 break;
348d480f 12806
001c8c33
PA
12807 case bp_until:
12808 ui_out_field_string
12809 (uiout, "reason",
12810 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12811 break;
12812 }
348d480f
PA
12813 }
12814
001c8c33 12815 return PRINT_UNKNOWN;
348d480f
PA
12816}
12817
06edf0c0
PA
12818static void
12819momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12820{
06edf0c0 12821 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12822}
12823
55aa24fb
SDJ
12824/* Specific methods for probe breakpoints. */
12825
12826static int
12827bkpt_probe_insert_location (struct bp_location *bl)
12828{
12829 int v = bkpt_insert_location (bl);
12830
12831 if (v == 0)
12832 {
12833 /* The insertion was successful, now let's set the probe's semaphore
12834 if needed. */
12835 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12836 }
12837
12838 return v;
12839}
12840
12841static int
12842bkpt_probe_remove_location (struct bp_location *bl)
12843{
12844 /* Let's clear the semaphore before removing the location. */
12845 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12846
12847 return bkpt_remove_location (bl);
12848}
12849
12850static void
12851bkpt_probe_create_sals_from_address (char **arg,
12852 struct linespec_result *canonical,
12853 enum bptype type_wanted,
12854 char *addr_start, char **copy_arg)
12855{
12856 struct linespec_sals lsal;
12857
12858 lsal.sals = parse_probes (arg, canonical);
12859
12860 *copy_arg = xstrdup (canonical->addr_string);
12861 lsal.canonical = xstrdup (*copy_arg);
12862
12863 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12864}
12865
12866static void
12867bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12868 struct symtabs_and_lines *sals)
12869{
12870 *sals = parse_probes (s, NULL);
12871 if (!sals->sals)
12872 error (_("probe not found"));
12873}
12874
348d480f 12875/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12876
348d480f
PA
12877static void
12878tracepoint_re_set (struct breakpoint *b)
12879{
12880 breakpoint_re_set_default (b);
12881}
876fa593 12882
348d480f
PA
12883static int
12884tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12885 struct address_space *aspace, CORE_ADDR bp_addr,
12886 const struct target_waitstatus *ws)
348d480f
PA
12887{
12888 /* By definition, the inferior does not report stops at
12889 tracepoints. */
12890 return 0;
74960c60
VP
12891}
12892
12893static void
348d480f
PA
12894tracepoint_print_one_detail (const struct breakpoint *self,
12895 struct ui_out *uiout)
74960c60 12896{
d9b3f62e
PA
12897 struct tracepoint *tp = (struct tracepoint *) self;
12898 if (tp->static_trace_marker_id)
348d480f
PA
12899 {
12900 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12901
348d480f
PA
12902 ui_out_text (uiout, "\tmarker id is ");
12903 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 12904 tp->static_trace_marker_id);
348d480f
PA
12905 ui_out_text (uiout, "\n");
12906 }
0d381245
VP
12907}
12908
a474d7c2 12909static void
348d480f 12910tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12911{
79a45e25 12912 if (ui_out_is_mi_like_p (current_uiout))
348d480f 12913 return;
cc59ec59 12914
348d480f
PA
12915 switch (b->type)
12916 {
12917 case bp_tracepoint:
12918 printf_filtered (_("Tracepoint"));
12919 printf_filtered (_(" %d"), b->number);
12920 break;
12921 case bp_fast_tracepoint:
12922 printf_filtered (_("Fast tracepoint"));
12923 printf_filtered (_(" %d"), b->number);
12924 break;
12925 case bp_static_tracepoint:
12926 printf_filtered (_("Static tracepoint"));
12927 printf_filtered (_(" %d"), b->number);
12928 break;
12929 default:
12930 internal_error (__FILE__, __LINE__,
12931 _("unhandled tracepoint type %d"), (int) b->type);
12932 }
12933
12934 say_where (b);
a474d7c2
PA
12935}
12936
348d480f 12937static void
d9b3f62e 12938tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12939{
d9b3f62e
PA
12940 struct tracepoint *tp = (struct tracepoint *) self;
12941
12942 if (self->type == bp_fast_tracepoint)
348d480f 12943 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 12944 if (self->type == bp_static_tracepoint)
348d480f 12945 fprintf_unfiltered (fp, "strace");
d9b3f62e 12946 else if (self->type == bp_tracepoint)
348d480f
PA
12947 fprintf_unfiltered (fp, "trace");
12948 else
12949 internal_error (__FILE__, __LINE__,
d9b3f62e 12950 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12951
d9b3f62e
PA
12952 fprintf_unfiltered (fp, " %s", self->addr_string);
12953 print_recreate_thread (self, fp);
12954
12955 if (tp->pass_count)
12956 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12957}
12958
983af33b
SDJ
12959static void
12960tracepoint_create_sals_from_address (char **arg,
12961 struct linespec_result *canonical,
12962 enum bptype type_wanted,
12963 char *addr_start, char **copy_arg)
12964{
12965 create_sals_from_address_default (arg, canonical, type_wanted,
12966 addr_start, copy_arg);
12967}
12968
12969static void
12970tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12971 struct linespec_result *canonical,
12972 struct linespec_sals *lsal,
12973 char *cond_string,
e7e0cddf 12974 char *extra_string,
983af33b
SDJ
12975 enum bptype type_wanted,
12976 enum bpdisp disposition,
12977 int thread,
12978 int task, int ignore_count,
12979 const struct breakpoint_ops *ops,
12980 int from_tty, int enabled,
44f238bb 12981 int internal, unsigned flags)
983af33b
SDJ
12982{
12983 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12984 cond_string, extra_string,
12985 type_wanted,
983af33b
SDJ
12986 disposition, thread, task,
12987 ignore_count, ops, from_tty,
44f238bb 12988 enabled, internal, flags);
983af33b
SDJ
12989}
12990
12991static void
12992tracepoint_decode_linespec (struct breakpoint *b, char **s,
12993 struct symtabs_and_lines *sals)
12994{
12995 decode_linespec_default (b, s, sals);
12996}
12997
2060206e 12998struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12999
55aa24fb
SDJ
13000/* The breakpoint_ops structure to be use on tracepoints placed in a
13001 static probe. */
13002
13003static void
13004tracepoint_probe_create_sals_from_address (char **arg,
13005 struct linespec_result *canonical,
13006 enum bptype type_wanted,
13007 char *addr_start, char **copy_arg)
13008{
13009 /* We use the same method for breakpoint on probes. */
13010 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13011 addr_start, copy_arg);
13012}
13013
13014static void
13015tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13016 struct symtabs_and_lines *sals)
13017{
13018 /* We use the same method for breakpoint on probes. */
13019 bkpt_probe_decode_linespec (b, s, sals);
13020}
13021
13022static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13023
983af33b
SDJ
13024/* The breakpoint_ops structure to be used on static tracepoints with
13025 markers (`-m'). */
13026
13027static void
13028strace_marker_create_sals_from_address (char **arg,
13029 struct linespec_result *canonical,
13030 enum bptype type_wanted,
13031 char *addr_start, char **copy_arg)
13032{
13033 struct linespec_sals lsal;
13034
13035 lsal.sals = decode_static_tracepoint_spec (arg);
13036
13037 *copy_arg = savestring (addr_start, *arg - addr_start);
13038
13039 canonical->addr_string = xstrdup (*copy_arg);
13040 lsal.canonical = xstrdup (*copy_arg);
13041 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13042}
13043
13044static void
13045strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13046 struct linespec_result *canonical,
13047 struct linespec_sals *lsal,
13048 char *cond_string,
e7e0cddf 13049 char *extra_string,
983af33b
SDJ
13050 enum bptype type_wanted,
13051 enum bpdisp disposition,
13052 int thread,
13053 int task, int ignore_count,
13054 const struct breakpoint_ops *ops,
13055 int from_tty, int enabled,
44f238bb 13056 int internal, unsigned flags)
983af33b
SDJ
13057{
13058 int i;
13059
13060 /* If the user is creating a static tracepoint by marker id
13061 (strace -m MARKER_ID), then store the sals index, so that
13062 breakpoint_re_set can try to match up which of the newly
13063 found markers corresponds to this one, and, don't try to
13064 expand multiple locations for each sal, given than SALS
13065 already should contain all sals for MARKER_ID. */
13066
13067 for (i = 0; i < lsal->sals.nelts; ++i)
13068 {
13069 struct symtabs_and_lines expanded;
13070 struct tracepoint *tp;
13071 struct cleanup *old_chain;
13072 char *addr_string;
13073
13074 expanded.nelts = 1;
13075 expanded.sals = &lsal->sals.sals[i];
13076
13077 addr_string = xstrdup (canonical->addr_string);
13078 old_chain = make_cleanup (xfree, addr_string);
13079
13080 tp = XCNEW (struct tracepoint);
13081 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13082 addr_string, NULL,
e7e0cddf
SS
13083 cond_string, extra_string,
13084 type_wanted, disposition,
983af33b 13085 thread, task, ignore_count, ops,
44f238bb 13086 from_tty, enabled, internal, flags,
983af33b
SDJ
13087 canonical->special_display);
13088 /* Given that its possible to have multiple markers with
13089 the same string id, if the user is creating a static
13090 tracepoint by marker id ("strace -m MARKER_ID"), then
13091 store the sals index, so that breakpoint_re_set can
13092 try to match up which of the newly found markers
13093 corresponds to this one */
13094 tp->static_trace_marker_id_idx = i;
13095
13096 install_breakpoint (internal, &tp->base, 0);
13097
13098 discard_cleanups (old_chain);
13099 }
13100}
13101
13102static void
13103strace_marker_decode_linespec (struct breakpoint *b, char **s,
13104 struct symtabs_and_lines *sals)
13105{
13106 struct tracepoint *tp = (struct tracepoint *) b;
13107
13108 *sals = decode_static_tracepoint_spec (s);
13109 if (sals->nelts > tp->static_trace_marker_id_idx)
13110 {
13111 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13112 sals->nelts = 1;
13113 }
13114 else
13115 error (_("marker %s not found"), tp->static_trace_marker_id);
13116}
13117
13118static struct breakpoint_ops strace_marker_breakpoint_ops;
13119
13120static int
13121strace_marker_p (struct breakpoint *b)
13122{
13123 return b->ops == &strace_marker_breakpoint_ops;
13124}
13125
53a5351d 13126/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13127 structures. */
c906108c
SS
13128
13129void
fba45db2 13130delete_breakpoint (struct breakpoint *bpt)
c906108c 13131{
52f0bd74 13132 struct breakpoint *b;
c906108c 13133
8a3fe4f8 13134 gdb_assert (bpt != NULL);
c906108c 13135
4a64f543
MS
13136 /* Has this bp already been deleted? This can happen because
13137 multiple lists can hold pointers to bp's. bpstat lists are
13138 especial culprits.
13139
13140 One example of this happening is a watchpoint's scope bp. When
13141 the scope bp triggers, we notice that the watchpoint is out of
13142 scope, and delete it. We also delete its scope bp. But the
13143 scope bp is marked "auto-deleting", and is already on a bpstat.
13144 That bpstat is then checked for auto-deleting bp's, which are
13145 deleted.
13146
13147 A real solution to this problem might involve reference counts in
13148 bp's, and/or giving them pointers back to their referencing
13149 bpstat's, and teaching delete_breakpoint to only free a bp's
13150 storage when no more references were extent. A cheaper bandaid
13151 was chosen. */
c906108c
SS
13152 if (bpt->type == bp_none)
13153 return;
13154
4a64f543
MS
13155 /* At least avoid this stale reference until the reference counting
13156 of breakpoints gets resolved. */
d0fb5eae 13157 if (bpt->related_breakpoint != bpt)
e5a0a904 13158 {
d0fb5eae 13159 struct breakpoint *related;
3a5c3e22 13160 struct watchpoint *w;
d0fb5eae
JK
13161
13162 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13163 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13164 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13165 w = (struct watchpoint *) bpt;
13166 else
13167 w = NULL;
13168 if (w != NULL)
13169 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13170
13171 /* Unlink bpt from the bpt->related_breakpoint ring. */
13172 for (related = bpt; related->related_breakpoint != bpt;
13173 related = related->related_breakpoint);
13174 related->related_breakpoint = bpt->related_breakpoint;
13175 bpt->related_breakpoint = bpt;
e5a0a904
JK
13176 }
13177
a9634178
TJB
13178 /* watch_command_1 creates a watchpoint but only sets its number if
13179 update_watchpoint succeeds in creating its bp_locations. If there's
13180 a problem in that process, we'll be asked to delete the half-created
13181 watchpoint. In that case, don't announce the deletion. */
13182 if (bpt->number)
13183 observer_notify_breakpoint_deleted (bpt);
c906108c 13184
c906108c
SS
13185 if (breakpoint_chain == bpt)
13186 breakpoint_chain = bpt->next;
13187
c906108c
SS
13188 ALL_BREAKPOINTS (b)
13189 if (b->next == bpt)
c5aa993b
JM
13190 {
13191 b->next = bpt->next;
13192 break;
13193 }
c906108c 13194
f431efe5
PA
13195 /* Be sure no bpstat's are pointing at the breakpoint after it's
13196 been freed. */
13197 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13198 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13199 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13200 commands are associated with the bpstat; if we remove it here,
13201 then the later call to bpstat_do_actions (&stop_bpstat); in
13202 event-top.c won't do anything, and temporary breakpoints with
13203 commands won't work. */
13204
13205 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13206
4a64f543
MS
13207 /* Now that breakpoint is removed from breakpoint list, update the
13208 global location list. This will remove locations that used to
13209 belong to this breakpoint. Do this before freeing the breakpoint
13210 itself, since remove_breakpoint looks at location's owner. It
13211 might be better design to have location completely
13212 self-contained, but it's not the case now. */
b60e7edf 13213 update_global_location_list (0);
74960c60 13214
348d480f 13215 bpt->ops->dtor (bpt);
4a64f543
MS
13216 /* On the chance that someone will soon try again to delete this
13217 same bp, we mark it as deleted before freeing its storage. */
c906108c 13218 bpt->type = bp_none;
b8c9b27d 13219 xfree (bpt);
c906108c
SS
13220}
13221
4d6140d9
AC
13222static void
13223do_delete_breakpoint_cleanup (void *b)
13224{
13225 delete_breakpoint (b);
13226}
13227
13228struct cleanup *
13229make_cleanup_delete_breakpoint (struct breakpoint *b)
13230{
13231 return make_cleanup (do_delete_breakpoint_cleanup, b);
13232}
13233
51be5b68
PA
13234/* Iterator function to call a user-provided callback function once
13235 for each of B and its related breakpoints. */
13236
13237static void
13238iterate_over_related_breakpoints (struct breakpoint *b,
13239 void (*function) (struct breakpoint *,
13240 void *),
13241 void *data)
13242{
13243 struct breakpoint *related;
13244
13245 related = b;
13246 do
13247 {
13248 struct breakpoint *next;
13249
13250 /* FUNCTION may delete RELATED. */
13251 next = related->related_breakpoint;
13252
13253 if (next == related)
13254 {
13255 /* RELATED is the last ring entry. */
13256 function (related, data);
13257
13258 /* FUNCTION may have deleted it, so we'd never reach back to
13259 B. There's nothing left to do anyway, so just break
13260 out. */
13261 break;
13262 }
13263 else
13264 function (related, data);
13265
13266 related = next;
13267 }
13268 while (related != b);
13269}
95a42b64
TT
13270
13271static void
13272do_delete_breakpoint (struct breakpoint *b, void *ignore)
13273{
13274 delete_breakpoint (b);
13275}
13276
51be5b68
PA
13277/* A callback for map_breakpoint_numbers that calls
13278 delete_breakpoint. */
13279
13280static void
13281do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13282{
13283 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13284}
13285
c906108c 13286void
fba45db2 13287delete_command (char *arg, int from_tty)
c906108c 13288{
35df4500 13289 struct breakpoint *b, *b_tmp;
c906108c 13290
ea9365bb
TT
13291 dont_repeat ();
13292
c906108c
SS
13293 if (arg == 0)
13294 {
13295 int breaks_to_delete = 0;
13296
46c6471b
PA
13297 /* Delete all breakpoints if no argument. Do not delete
13298 internal breakpoints, these have to be deleted with an
13299 explicit breakpoint number argument. */
c5aa993b 13300 ALL_BREAKPOINTS (b)
46c6471b 13301 if (user_breakpoint_p (b))
973d738b
DJ
13302 {
13303 breaks_to_delete = 1;
13304 break;
13305 }
c906108c
SS
13306
13307 /* Ask user only if there are some breakpoints to delete. */
13308 if (!from_tty
e2e0b3e5 13309 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13310 {
35df4500 13311 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13312 if (user_breakpoint_p (b))
c5aa993b 13313 delete_breakpoint (b);
c906108c
SS
13314 }
13315 }
13316 else
51be5b68 13317 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13318}
13319
0d381245
VP
13320static int
13321all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13322{
0d381245 13323 for (; loc; loc = loc->next)
8645ff69
UW
13324 if (!loc->shlib_disabled
13325 && !loc->pspace->executing_startup)
0d381245
VP
13326 return 0;
13327 return 1;
fe3f5fa8
VP
13328}
13329
776592bf
DE
13330/* Subroutine of update_breakpoint_locations to simplify it.
13331 Return non-zero if multiple fns in list LOC have the same name.
13332 Null names are ignored. */
13333
13334static int
13335ambiguous_names_p (struct bp_location *loc)
13336{
13337 struct bp_location *l;
13338 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13339 (int (*) (const void *,
13340 const void *)) streq,
776592bf
DE
13341 NULL, xcalloc, xfree);
13342
13343 for (l = loc; l != NULL; l = l->next)
13344 {
13345 const char **slot;
13346 const char *name = l->function_name;
13347
13348 /* Allow for some names to be NULL, ignore them. */
13349 if (name == NULL)
13350 continue;
13351
13352 slot = (const char **) htab_find_slot (htab, (const void *) name,
13353 INSERT);
4a64f543
MS
13354 /* NOTE: We can assume slot != NULL here because xcalloc never
13355 returns NULL. */
776592bf
DE
13356 if (*slot != NULL)
13357 {
13358 htab_delete (htab);
13359 return 1;
13360 }
13361 *slot = name;
13362 }
13363
13364 htab_delete (htab);
13365 return 0;
13366}
13367
0fb4aa4b
PA
13368/* When symbols change, it probably means the sources changed as well,
13369 and it might mean the static tracepoint markers are no longer at
13370 the same address or line numbers they used to be at last we
13371 checked. Losing your static tracepoints whenever you rebuild is
13372 undesirable. This function tries to resync/rematch gdb static
13373 tracepoints with the markers on the target, for static tracepoints
13374 that have not been set by marker id. Static tracepoint that have
13375 been set by marker id are reset by marker id in breakpoint_re_set.
13376 The heuristic is:
13377
13378 1) For a tracepoint set at a specific address, look for a marker at
13379 the old PC. If one is found there, assume to be the same marker.
13380 If the name / string id of the marker found is different from the
13381 previous known name, assume that means the user renamed the marker
13382 in the sources, and output a warning.
13383
13384 2) For a tracepoint set at a given line number, look for a marker
13385 at the new address of the old line number. If one is found there,
13386 assume to be the same marker. If the name / string id of the
13387 marker found is different from the previous known name, assume that
13388 means the user renamed the marker in the sources, and output a
13389 warning.
13390
13391 3) If a marker is no longer found at the same address or line, it
13392 may mean the marker no longer exists. But it may also just mean
13393 the code changed a bit. Maybe the user added a few lines of code
13394 that made the marker move up or down (in line number terms). Ask
13395 the target for info about the marker with the string id as we knew
13396 it. If found, update line number and address in the matching
13397 static tracepoint. This will get confused if there's more than one
13398 marker with the same ID (possible in UST, although unadvised
13399 precisely because it confuses tools). */
13400
13401static struct symtab_and_line
13402update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13403{
d9b3f62e 13404 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13405 struct static_tracepoint_marker marker;
13406 CORE_ADDR pc;
0fb4aa4b
PA
13407
13408 pc = sal.pc;
13409 if (sal.line)
13410 find_line_pc (sal.symtab, sal.line, &pc);
13411
13412 if (target_static_tracepoint_marker_at (pc, &marker))
13413 {
d9b3f62e 13414 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13415 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13416 b->number,
d9b3f62e 13417 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13418
d9b3f62e
PA
13419 xfree (tp->static_trace_marker_id);
13420 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13421 release_static_tracepoint_marker (&marker);
13422
13423 return sal;
13424 }
13425
13426 /* Old marker wasn't found on target at lineno. Try looking it up
13427 by string ID. */
13428 if (!sal.explicit_pc
13429 && sal.line != 0
13430 && sal.symtab != NULL
d9b3f62e 13431 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13432 {
13433 VEC(static_tracepoint_marker_p) *markers;
13434
13435 markers
d9b3f62e 13436 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13437
13438 if (!VEC_empty(static_tracepoint_marker_p, markers))
13439 {
80e1d417 13440 struct symtab_and_line sal2;
0fb4aa4b 13441 struct symbol *sym;
80e1d417 13442 struct static_tracepoint_marker *tpmarker;
79a45e25 13443 struct ui_out *uiout = current_uiout;
0fb4aa4b 13444
80e1d417 13445 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13446
d9b3f62e 13447 xfree (tp->static_trace_marker_id);
80e1d417 13448 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13449
13450 warning (_("marker for static tracepoint %d (%s) not "
13451 "found at previous line number"),
d9b3f62e 13452 b->number, tp->static_trace_marker_id);
0fb4aa4b 13453
80e1d417 13454 init_sal (&sal2);
0fb4aa4b 13455
80e1d417 13456 sal2.pc = tpmarker->address;
0fb4aa4b 13457
80e1d417
AS
13458 sal2 = find_pc_line (tpmarker->address, 0);
13459 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13460 ui_out_text (uiout, "Now in ");
13461 if (sym)
13462 {
13463 ui_out_field_string (uiout, "func",
13464 SYMBOL_PRINT_NAME (sym));
13465 ui_out_text (uiout, " at ");
13466 }
80e1d417 13467 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
13468 ui_out_text (uiout, ":");
13469
13470 if (ui_out_is_mi_like_p (uiout))
13471 {
80e1d417 13472 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
13473
13474 if (fullname)
13475 ui_out_field_string (uiout, "fullname", fullname);
13476 }
13477
80e1d417 13478 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13479 ui_out_text (uiout, "\n");
13480
80e1d417 13481 b->loc->line_number = sal2.line;
0fb4aa4b 13482
f8eba3c6 13483 xfree (b->loc->source_file);
0fb4aa4b 13484 if (sym)
80e1d417 13485 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 13486 else
f8eba3c6 13487 b->loc->source_file = NULL;
0fb4aa4b
PA
13488
13489 xfree (b->addr_string);
13490 b->addr_string = xstrprintf ("%s:%d",
80e1d417 13491 sal2.symtab->filename,
f8eba3c6 13492 b->loc->line_number);
0fb4aa4b
PA
13493
13494 /* Might be nice to check if function changed, and warn if
13495 so. */
13496
80e1d417 13497 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13498 }
13499 }
13500 return sal;
13501}
13502
8d3788bd
VP
13503/* Returns 1 iff locations A and B are sufficiently same that
13504 we don't need to report breakpoint as changed. */
13505
13506static int
13507locations_are_equal (struct bp_location *a, struct bp_location *b)
13508{
13509 while (a && b)
13510 {
13511 if (a->address != b->address)
13512 return 0;
13513
13514 if (a->shlib_disabled != b->shlib_disabled)
13515 return 0;
13516
13517 if (a->enabled != b->enabled)
13518 return 0;
13519
13520 a = a->next;
13521 b = b->next;
13522 }
13523
13524 if ((a == NULL) != (b == NULL))
13525 return 0;
13526
13527 return 1;
13528}
13529
f1310107
TJB
13530/* Create new breakpoint locations for B (a hardware or software breakpoint)
13531 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13532 a ranged breakpoint. */
13533
0e30163f 13534void
0d381245 13535update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13536 struct symtabs_and_lines sals,
13537 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13538{
13539 int i;
0d381245
VP
13540 struct bp_location *existing_locations = b->loc;
13541
f8eba3c6
TT
13542 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13543 {
13544 /* Ranged breakpoints have only one start location and one end
13545 location. */
13546 b->enable_state = bp_disabled;
13547 update_global_location_list (1);
13548 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13549 "multiple locations found\n"),
13550 b->number);
13551 return;
13552 }
f1310107 13553
4a64f543
MS
13554 /* If there's no new locations, and all existing locations are
13555 pending, don't do anything. This optimizes the common case where
13556 all locations are in the same shared library, that was unloaded.
13557 We'd like to retain the location, so that when the library is
13558 loaded again, we don't loose the enabled/disabled status of the
13559 individual locations. */
0d381245 13560 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13561 return;
13562
fe3f5fa8
VP
13563 b->loc = NULL;
13564
0d381245 13565 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13566 {
f8eba3c6
TT
13567 struct bp_location *new_loc;
13568
13569 switch_to_program_space_and_thread (sals.sals[i].pspace);
13570
13571 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13572
0d381245
VP
13573 /* Reparse conditions, they might contain references to the
13574 old symtab. */
13575 if (b->cond_string != NULL)
13576 {
f1310107 13577 char *s;
bfd189b1 13578 volatile struct gdb_exception e;
fe3f5fa8 13579
0d381245
VP
13580 s = b->cond_string;
13581 TRY_CATCH (e, RETURN_MASK_ERROR)
13582 {
13583 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
13584 0);
13585 }
13586 if (e.reason < 0)
13587 {
3e43a32a
MS
13588 warning (_("failed to reevaluate condition "
13589 "for breakpoint %d: %s"),
0d381245
VP
13590 b->number, e.message);
13591 new_loc->enabled = 0;
13592 }
13593 }
fe3f5fa8 13594
f1310107
TJB
13595 if (sals_end.nelts)
13596 {
13597 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13598
13599 new_loc->length = end - sals.sals[0].pc + 1;
13600 }
0d381245 13601 }
fe3f5fa8 13602
514f746b
AR
13603 /* Update locations of permanent breakpoints. */
13604 if (b->enable_state == bp_permanent)
13605 make_breakpoint_permanent (b);
13606
4a64f543
MS
13607 /* If possible, carry over 'disable' status from existing
13608 breakpoints. */
0d381245
VP
13609 {
13610 struct bp_location *e = existing_locations;
776592bf
DE
13611 /* If there are multiple breakpoints with the same function name,
13612 e.g. for inline functions, comparing function names won't work.
13613 Instead compare pc addresses; this is just a heuristic as things
13614 may have moved, but in practice it gives the correct answer
13615 often enough until a better solution is found. */
13616 int have_ambiguous_names = ambiguous_names_p (b->loc);
13617
0d381245
VP
13618 for (; e; e = e->next)
13619 {
13620 if (!e->enabled && e->function_name)
13621 {
13622 struct bp_location *l = b->loc;
776592bf
DE
13623 if (have_ambiguous_names)
13624 {
13625 for (; l; l = l->next)
f1310107 13626 if (breakpoint_locations_match (e, l))
776592bf
DE
13627 {
13628 l->enabled = 0;
13629 break;
13630 }
13631 }
13632 else
13633 {
13634 for (; l; l = l->next)
13635 if (l->function_name
13636 && strcmp (e->function_name, l->function_name) == 0)
13637 {
13638 l->enabled = 0;
13639 break;
13640 }
13641 }
0d381245
VP
13642 }
13643 }
13644 }
fe3f5fa8 13645
8d3788bd
VP
13646 if (!locations_are_equal (existing_locations, b->loc))
13647 observer_notify_breakpoint_modified (b);
13648
b60e7edf 13649 update_global_location_list (1);
fe3f5fa8
VP
13650}
13651
ef23e705
TJB
13652/* Find the SaL locations corresponding to the given ADDR_STRING.
13653 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13654
13655static struct symtabs_and_lines
13656addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13657{
13658 char *s;
02d20e4a 13659 struct symtabs_and_lines sals = {0};
f8eba3c6 13660 volatile struct gdb_exception e;
ef23e705 13661
983af33b 13662 gdb_assert (b->ops != NULL);
ef23e705 13663 s = addr_string;
ef23e705
TJB
13664
13665 TRY_CATCH (e, RETURN_MASK_ERROR)
13666 {
983af33b 13667 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
13668 }
13669 if (e.reason < 0)
13670 {
13671 int not_found_and_ok = 0;
13672 /* For pending breakpoints, it's expected that parsing will
13673 fail until the right shared library is loaded. User has
13674 already told to create pending breakpoints and don't need
13675 extra messages. If breakpoint is in bp_shlib_disabled
13676 state, then user already saw the message about that
13677 breakpoint being disabled, and don't want to see more
13678 errors. */
58438ac1 13679 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
13680 && (b->condition_not_parsed
13681 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13682 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13683 || b->enable_state == bp_disabled))
13684 not_found_and_ok = 1;
13685
13686 if (!not_found_and_ok)
13687 {
13688 /* We surely don't want to warn about the same breakpoint
13689 10 times. One solution, implemented here, is disable
13690 the breakpoint on error. Another solution would be to
13691 have separate 'warning emitted' flag. Since this
13692 happens only when a binary has changed, I don't know
13693 which approach is better. */
13694 b->enable_state = bp_disabled;
13695 throw_exception (e);
13696 }
13697 }
13698
58438ac1 13699 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 13700 {
f8eba3c6 13701 int i;
ef23e705 13702
f8eba3c6
TT
13703 for (i = 0; i < sals.nelts; ++i)
13704 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
13705 if (b->condition_not_parsed && s && s[0])
13706 {
13707 char *cond_string = 0;
13708 int thread = -1;
13709 int task = 0;
e7e0cddf 13710 char *extra_string = NULL;
ef23e705
TJB
13711
13712 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
13713 &cond_string, &thread, &task,
13714 &extra_string);
ef23e705
TJB
13715 if (cond_string)
13716 b->cond_string = cond_string;
13717 b->thread = thread;
13718 b->task = task;
e7e0cddf
SS
13719 if (extra_string)
13720 b->extra_string = extra_string;
ef23e705
TJB
13721 b->condition_not_parsed = 0;
13722 }
13723
983af33b 13724 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 13725 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 13726
58438ac1
TT
13727 *found = 1;
13728 }
13729 else
13730 *found = 0;
ef23e705
TJB
13731
13732 return sals;
13733}
13734
348d480f
PA
13735/* The default re_set method, for typical hardware or software
13736 breakpoints. Reevaluate the breakpoint and recreate its
13737 locations. */
13738
13739static void
28010a5d 13740breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
13741{
13742 int found;
f1310107 13743 struct symtabs_and_lines sals, sals_end;
ef23e705 13744 struct symtabs_and_lines expanded = {0};
f1310107 13745 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
13746
13747 sals = addr_string_to_sals (b, b->addr_string, &found);
13748 if (found)
13749 {
13750 make_cleanup (xfree, sals.sals);
f8eba3c6 13751 expanded = sals;
ef23e705
TJB
13752 }
13753
f1310107
TJB
13754 if (b->addr_string_range_end)
13755 {
13756 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13757 if (found)
13758 {
13759 make_cleanup (xfree, sals_end.sals);
f8eba3c6 13760 expanded_end = sals_end;
f1310107
TJB
13761 }
13762 }
13763
13764 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
13765}
13766
983af33b
SDJ
13767/* Default method for creating SALs from an address string. It basically
13768 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13769
13770static void
13771create_sals_from_address_default (char **arg,
13772 struct linespec_result *canonical,
13773 enum bptype type_wanted,
13774 char *addr_start, char **copy_arg)
13775{
13776 parse_breakpoint_sals (arg, canonical);
13777}
13778
13779/* Call create_breakpoints_sal for the given arguments. This is the default
13780 function for the `create_breakpoints_sal' method of
13781 breakpoint_ops. */
13782
13783static void
13784create_breakpoints_sal_default (struct gdbarch *gdbarch,
13785 struct linespec_result *canonical,
13786 struct linespec_sals *lsal,
13787 char *cond_string,
e7e0cddf 13788 char *extra_string,
983af33b
SDJ
13789 enum bptype type_wanted,
13790 enum bpdisp disposition,
13791 int thread,
13792 int task, int ignore_count,
13793 const struct breakpoint_ops *ops,
13794 int from_tty, int enabled,
44f238bb 13795 int internal, unsigned flags)
983af33b
SDJ
13796{
13797 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 13798 extra_string,
983af33b
SDJ
13799 type_wanted, disposition,
13800 thread, task, ignore_count, ops, from_tty,
44f238bb 13801 enabled, internal, flags);
983af33b
SDJ
13802}
13803
13804/* Decode the line represented by S by calling decode_line_full. This is the
13805 default function for the `decode_linespec' method of breakpoint_ops. */
13806
13807static void
13808decode_linespec_default (struct breakpoint *b, char **s,
13809 struct symtabs_and_lines *sals)
13810{
13811 struct linespec_result canonical;
13812
13813 init_linespec_result (&canonical);
13814 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13815 (struct symtab *) NULL, 0,
13816 &canonical, multiple_symbols_all,
13817 b->filter);
13818
13819 /* We should get 0 or 1 resulting SALs. */
13820 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13821
13822 if (VEC_length (linespec_sals, canonical.sals) > 0)
13823 {
13824 struct linespec_sals *lsal;
13825
13826 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13827 *sals = lsal->sals;
13828 /* Arrange it so the destructor does not free the
13829 contents. */
13830 lsal->sals.sals = NULL;
13831 }
13832
13833 destroy_linespec_result (&canonical);
13834}
13835
28010a5d
PA
13836/* Prepare the global context for a re-set of breakpoint B. */
13837
13838static struct cleanup *
13839prepare_re_set_context (struct breakpoint *b)
13840{
13841 struct cleanup *cleanups;
13842
13843 input_radix = b->input_radix;
13844 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
13845 if (b->pspace != NULL)
13846 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
13847 set_language (b->language);
13848
13849 return cleanups;
ef23e705
TJB
13850}
13851
c906108c
SS
13852/* Reset a breakpoint given it's struct breakpoint * BINT.
13853 The value we return ends up being the return value from catch_errors.
13854 Unused in this case. */
13855
13856static int
4efb68b1 13857breakpoint_re_set_one (void *bint)
c906108c 13858{
4a64f543 13859 /* Get past catch_errs. */
53a5351d 13860 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 13861 struct cleanup *cleanups;
c906108c 13862
348d480f
PA
13863 cleanups = prepare_re_set_context (b);
13864 b->ops->re_set (b);
13865 do_cleanups (cleanups);
c906108c
SS
13866 return 0;
13867}
13868
69de3c6a 13869/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 13870void
69de3c6a 13871breakpoint_re_set (void)
c906108c 13872{
35df4500 13873 struct breakpoint *b, *b_tmp;
c906108c
SS
13874 enum language save_language;
13875 int save_input_radix;
6c95b8df 13876 struct cleanup *old_chain;
c5aa993b 13877
c906108c
SS
13878 save_language = current_language->la_language;
13879 save_input_radix = input_radix;
6c95b8df
PA
13880 old_chain = save_current_program_space ();
13881
35df4500 13882 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 13883 {
4a64f543 13884 /* Format possible error msg. */
fe3f5fa8 13885 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
13886 b->number);
13887 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 13888 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 13889 do_cleanups (cleanups);
c5aa993b 13890 }
c906108c
SS
13891 set_language (save_language);
13892 input_radix = save_input_radix;
e62c965a 13893
0756c555 13894 jit_breakpoint_re_set ();
4efc6507 13895
6c95b8df
PA
13896 do_cleanups (old_chain);
13897
af02033e
PP
13898 create_overlay_event_breakpoint ();
13899 create_longjmp_master_breakpoint ();
13900 create_std_terminate_master_breakpoint ();
186c406b 13901 create_exception_master_breakpoint ();
1bfeeb0f
JL
13902
13903 /* While we're at it, reset the skip list too. */
13904 skip_re_set ();
c906108c
SS
13905}
13906\f
c906108c
SS
13907/* Reset the thread number of this breakpoint:
13908
13909 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13910 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13911void
fba45db2 13912breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13913{
13914 if (b->thread != -1)
13915 {
39f77062
KB
13916 if (in_thread_list (inferior_ptid))
13917 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
13918
13919 /* We're being called after following a fork. The new fork is
13920 selected as current, and unless this was a vfork will have a
13921 different program space from the original thread. Reset that
13922 as well. */
13923 b->loc->pspace = current_program_space;
c906108c
SS
13924 }
13925}
13926
03ac34d5
MS
13927/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13928 If from_tty is nonzero, it prints a message to that effect,
13929 which ends with a period (no newline). */
13930
c906108c 13931void
fba45db2 13932set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13933{
52f0bd74 13934 struct breakpoint *b;
c906108c
SS
13935
13936 if (count < 0)
13937 count = 0;
13938
13939 ALL_BREAKPOINTS (b)
13940 if (b->number == bptnum)
c5aa993b 13941 {
d77f58be
SS
13942 if (is_tracepoint (b))
13943 {
13944 if (from_tty && count != 0)
13945 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13946 bptnum);
13947 return;
13948 }
13949
c5aa993b 13950 b->ignore_count = count;
221ea385
KS
13951 if (from_tty)
13952 {
13953 if (count == 0)
3e43a32a
MS
13954 printf_filtered (_("Will stop next time "
13955 "breakpoint %d is reached."),
221ea385
KS
13956 bptnum);
13957 else if (count == 1)
a3f17187 13958 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13959 bptnum);
13960 else
3e43a32a
MS
13961 printf_filtered (_("Will ignore next %d "
13962 "crossings of breakpoint %d."),
221ea385
KS
13963 count, bptnum);
13964 }
c5aa993b 13965 breakpoints_changed ();
8d3788bd 13966 observer_notify_breakpoint_modified (b);
c5aa993b
JM
13967 return;
13968 }
c906108c 13969
8a3fe4f8 13970 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13971}
13972
c906108c
SS
13973/* Command to set ignore-count of breakpoint N to COUNT. */
13974
13975static void
fba45db2 13976ignore_command (char *args, int from_tty)
c906108c
SS
13977{
13978 char *p = args;
52f0bd74 13979 int num;
c906108c
SS
13980
13981 if (p == 0)
e2e0b3e5 13982 error_no_arg (_("a breakpoint number"));
c5aa993b 13983
c906108c 13984 num = get_number (&p);
5c44784c 13985 if (num == 0)
8a3fe4f8 13986 error (_("bad breakpoint number: '%s'"), args);
c906108c 13987 if (*p == 0)
8a3fe4f8 13988 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13989
13990 set_ignore_count (num,
13991 longest_to_int (value_as_long (parse_and_eval (p))),
13992 from_tty);
221ea385
KS
13993 if (from_tty)
13994 printf_filtered ("\n");
c906108c
SS
13995}
13996\f
13997/* Call FUNCTION on each of the breakpoints
13998 whose numbers are given in ARGS. */
13999
14000static void
95a42b64
TT
14001map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14002 void *),
14003 void *data)
c906108c 14004{
52f0bd74
AC
14005 int num;
14006 struct breakpoint *b, *tmp;
11cf8741 14007 int match;
197f0a60 14008 struct get_number_or_range_state state;
c906108c 14009
197f0a60 14010 if (args == 0)
e2e0b3e5 14011 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14012
197f0a60
TT
14013 init_number_or_range (&state, args);
14014
14015 while (!state.finished)
c906108c 14016 {
197f0a60
TT
14017 char *p = state.string;
14018
11cf8741 14019 match = 0;
c5aa993b 14020
197f0a60 14021 num = get_number_or_range (&state);
5c44784c 14022 if (num == 0)
c5aa993b 14023 {
8a3fe4f8 14024 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14025 }
14026 else
14027 {
14028 ALL_BREAKPOINTS_SAFE (b, tmp)
14029 if (b->number == num)
14030 {
11cf8741 14031 match = 1;
cdac0397 14032 function (b, data);
11cf8741 14033 break;
5c44784c 14034 }
11cf8741 14035 if (match == 0)
a3f17187 14036 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14037 }
c906108c
SS
14038 }
14039}
14040
0d381245
VP
14041static struct bp_location *
14042find_location_by_number (char *number)
14043{
14044 char *dot = strchr (number, '.');
14045 char *p1;
14046 int bp_num;
14047 int loc_num;
14048 struct breakpoint *b;
14049 struct bp_location *loc;
14050
14051 *dot = '\0';
14052
14053 p1 = number;
197f0a60 14054 bp_num = get_number (&p1);
0d381245
VP
14055 if (bp_num == 0)
14056 error (_("Bad breakpoint number '%s'"), number);
14057
14058 ALL_BREAKPOINTS (b)
14059 if (b->number == bp_num)
14060 {
14061 break;
14062 }
14063
14064 if (!b || b->number != bp_num)
14065 error (_("Bad breakpoint number '%s'"), number);
14066
14067 p1 = dot+1;
197f0a60 14068 loc_num = get_number (&p1);
0d381245
VP
14069 if (loc_num == 0)
14070 error (_("Bad breakpoint location number '%s'"), number);
14071
14072 --loc_num;
14073 loc = b->loc;
14074 for (;loc_num && loc; --loc_num, loc = loc->next)
14075 ;
14076 if (!loc)
14077 error (_("Bad breakpoint location number '%s'"), dot+1);
14078
14079 return loc;
14080}
14081
14082
1900040c
MS
14083/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14084 If from_tty is nonzero, it prints a message to that effect,
14085 which ends with a period (no newline). */
14086
c906108c 14087void
fba45db2 14088disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14089{
14090 /* Never disable a watchpoint scope breakpoint; we want to
14091 hit them when we leave scope so we can delete both the
14092 watchpoint and its scope breakpoint at that time. */
14093 if (bpt->type == bp_watchpoint_scope)
14094 return;
14095
c2c6d25f 14096 /* You can't disable permanent breakpoints. */
b5de0fa7 14097 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14098 return;
14099
b5de0fa7 14100 bpt->enable_state = bp_disabled;
c906108c 14101
b775012e
LM
14102 /* Mark breakpoint locations modified. */
14103 mark_breakpoint_modified (bpt);
14104
d248b706
KY
14105 if (target_supports_enable_disable_tracepoint ()
14106 && current_trace_status ()->running && is_tracepoint (bpt))
14107 {
14108 struct bp_location *location;
14109
14110 for (location = bpt->loc; location; location = location->next)
14111 target_disable_tracepoint (location);
14112 }
14113
b60e7edf 14114 update_global_location_list (0);
c906108c 14115
8d3788bd 14116 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14117}
14118
51be5b68
PA
14119/* A callback for iterate_over_related_breakpoints. */
14120
14121static void
14122do_disable_breakpoint (struct breakpoint *b, void *ignore)
14123{
14124 disable_breakpoint (b);
14125}
14126
95a42b64
TT
14127/* A callback for map_breakpoint_numbers that calls
14128 disable_breakpoint. */
14129
14130static void
14131do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14132{
51be5b68 14133 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14134}
14135
c906108c 14136static void
fba45db2 14137disable_command (char *args, int from_tty)
c906108c 14138{
c906108c 14139 if (args == 0)
46c6471b
PA
14140 {
14141 struct breakpoint *bpt;
14142
14143 ALL_BREAKPOINTS (bpt)
14144 if (user_breakpoint_p (bpt))
14145 disable_breakpoint (bpt);
14146 }
0d381245
VP
14147 else if (strchr (args, '.'))
14148 {
14149 struct bp_location *loc = find_location_by_number (args);
14150 if (loc)
d248b706 14151 {
b775012e
LM
14152 if (loc->enabled)
14153 {
14154 loc->enabled = 0;
14155 mark_breakpoint_location_modified (loc);
14156 }
d248b706
KY
14157 if (target_supports_enable_disable_tracepoint ()
14158 && current_trace_status ()->running && loc->owner
14159 && is_tracepoint (loc->owner))
14160 target_disable_tracepoint (loc);
14161 }
b60e7edf 14162 update_global_location_list (0);
0d381245 14163 }
c906108c 14164 else
95a42b64 14165 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
14166}
14167
14168static void
816338b5
SS
14169enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14170 int count)
c906108c 14171{
afe38095 14172 int target_resources_ok;
c906108c
SS
14173
14174 if (bpt->type == bp_hardware_breakpoint)
14175 {
14176 int i;
c5aa993b 14177 i = hw_breakpoint_used_count ();
53a5351d 14178 target_resources_ok =
d92524f1 14179 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14180 i + 1, 0);
c906108c 14181 if (target_resources_ok == 0)
8a3fe4f8 14182 error (_("No hardware breakpoint support in the target."));
c906108c 14183 else if (target_resources_ok < 0)
8a3fe4f8 14184 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14185 }
14186
cc60f2e3 14187 if (is_watchpoint (bpt))
c906108c 14188 {
d07205c2
JK
14189 /* Initialize it just to avoid a GCC false warning. */
14190 enum enable_state orig_enable_state = 0;
bfd189b1 14191 volatile struct gdb_exception e;
dde02812
ES
14192
14193 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14194 {
3a5c3e22
PA
14195 struct watchpoint *w = (struct watchpoint *) bpt;
14196
1e718ff1
TJB
14197 orig_enable_state = bpt->enable_state;
14198 bpt->enable_state = bp_enabled;
3a5c3e22 14199 update_watchpoint (w, 1 /* reparse */);
c906108c 14200 }
dde02812 14201 if (e.reason < 0)
c5aa993b 14202 {
1e718ff1 14203 bpt->enable_state = orig_enable_state;
dde02812
ES
14204 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14205 bpt->number);
14206 return;
c5aa993b 14207 }
c906108c 14208 }
0101ce28 14209
b4c291bb
KH
14210 if (bpt->enable_state != bp_permanent)
14211 bpt->enable_state = bp_enabled;
d248b706 14212
b775012e
LM
14213 bpt->enable_state = bp_enabled;
14214
14215 /* Mark breakpoint locations modified. */
14216 mark_breakpoint_modified (bpt);
14217
d248b706
KY
14218 if (target_supports_enable_disable_tracepoint ()
14219 && current_trace_status ()->running && is_tracepoint (bpt))
14220 {
14221 struct bp_location *location;
14222
14223 for (location = bpt->loc; location; location = location->next)
14224 target_enable_tracepoint (location);
14225 }
14226
b4c291bb 14227 bpt->disposition = disposition;
816338b5 14228 bpt->enable_count = count;
b60e7edf 14229 update_global_location_list (1);
b4c291bb
KH
14230 breakpoints_changed ();
14231
8d3788bd 14232 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14233}
14234
fe3f5fa8 14235
c906108c 14236void
fba45db2 14237enable_breakpoint (struct breakpoint *bpt)
c906108c 14238{
816338b5 14239 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14240}
14241
14242static void
14243do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14244{
14245 enable_breakpoint (bpt);
c906108c
SS
14246}
14247
95a42b64
TT
14248/* A callback for map_breakpoint_numbers that calls
14249 enable_breakpoint. */
14250
14251static void
14252do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14253{
51be5b68 14254 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14255}
14256
c906108c
SS
14257/* The enable command enables the specified breakpoints (or all defined
14258 breakpoints) so they once again become (or continue to be) effective
1272ad14 14259 in stopping the inferior. */
c906108c 14260
c906108c 14261static void
fba45db2 14262enable_command (char *args, int from_tty)
c906108c 14263{
c906108c 14264 if (args == 0)
46c6471b
PA
14265 {
14266 struct breakpoint *bpt;
14267
14268 ALL_BREAKPOINTS (bpt)
14269 if (user_breakpoint_p (bpt))
14270 enable_breakpoint (bpt);
14271 }
0d381245
VP
14272 else if (strchr (args, '.'))
14273 {
14274 struct bp_location *loc = find_location_by_number (args);
14275 if (loc)
d248b706 14276 {
b775012e
LM
14277 if (!loc->enabled)
14278 {
14279 loc->enabled = 1;
14280 mark_breakpoint_location_modified (loc);
14281 }
d248b706
KY
14282 if (target_supports_enable_disable_tracepoint ()
14283 && current_trace_status ()->running && loc->owner
14284 && is_tracepoint (loc->owner))
14285 target_enable_tracepoint (loc);
14286 }
b60e7edf 14287 update_global_location_list (1);
0d381245 14288 }
c906108c 14289 else
95a42b64 14290 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
14291}
14292
816338b5
SS
14293/* This struct packages up disposition data for application to multiple
14294 breakpoints. */
14295
14296struct disp_data
14297{
14298 enum bpdisp disp;
14299 int count;
14300};
14301
c906108c 14302static void
51be5b68
PA
14303do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14304{
816338b5 14305 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14306
816338b5 14307 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14308}
14309
14310static void
14311do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14312{
816338b5 14313 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14314
14315 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14316}
14317
c906108c 14318static void
fba45db2 14319enable_once_command (char *args, int from_tty)
c906108c 14320{
51be5b68 14321 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14322}
14323
816338b5
SS
14324static void
14325do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14326{
14327 struct disp_data disp = { disp_disable, *(int *) countptr };
14328
14329 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14330}
14331
14332static void
14333enable_count_command (char *args, int from_tty)
14334{
14335 int count = get_number (&args);
14336
14337 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14338}
14339
c906108c 14340static void
51be5b68 14341do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14342{
816338b5 14343 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14344
14345 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14346}
14347
c906108c 14348static void
fba45db2 14349enable_delete_command (char *args, int from_tty)
c906108c 14350{
51be5b68 14351 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14352}
14353\f
fa8d40ab
JJ
14354static void
14355set_breakpoint_cmd (char *args, int from_tty)
14356{
14357}
14358
14359static void
14360show_breakpoint_cmd (char *args, int from_tty)
14361{
14362}
14363
1f3b5d1b
PP
14364/* Invalidate last known value of any hardware watchpoint if
14365 the memory which that value represents has been written to by
14366 GDB itself. */
14367
14368static void
14369invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14370 const bfd_byte *data)
14371{
14372 struct breakpoint *bp;
14373
14374 ALL_BREAKPOINTS (bp)
14375 if (bp->enable_state == bp_enabled
3a5c3e22 14376 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14377 {
3a5c3e22 14378 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14379
3a5c3e22
PA
14380 if (wp->val_valid && wp->val)
14381 {
14382 struct bp_location *loc;
14383
14384 for (loc = bp->loc; loc != NULL; loc = loc->next)
14385 if (loc->loc_type == bp_loc_hardware_watchpoint
14386 && loc->address + loc->length > addr
14387 && addr + len > loc->address)
14388 {
14389 value_free (wp->val);
14390 wp->val = NULL;
14391 wp->val_valid = 0;
14392 }
14393 }
1f3b5d1b
PP
14394 }
14395}
14396
1bfeeb0f
JL
14397/* Use the last displayed codepoint's values, or nothing
14398 if they aren't valid. */
c906108c
SS
14399
14400struct symtabs_and_lines
f8eba3c6 14401decode_line_spec_1 (char *string, int flags)
c906108c
SS
14402{
14403 struct symtabs_and_lines sals;
cc59ec59 14404
c906108c 14405 if (string == 0)
8a3fe4f8 14406 error (_("Empty line specification."));
1bfeeb0f 14407 if (last_displayed_sal_is_valid ())
f8eba3c6 14408 sals = decode_line_1 (&string, flags,
1bfeeb0f 14409 get_last_displayed_symtab (),
f8eba3c6 14410 get_last_displayed_line ());
c906108c 14411 else
f8eba3c6 14412 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 14413 if (*string)
8a3fe4f8 14414 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
14415 return sals;
14416}
8181d85f
DJ
14417
14418/* Create and insert a raw software breakpoint at PC. Return an
14419 identifier, which should be used to remove the breakpoint later.
14420 In general, places which call this should be using something on the
14421 breakpoint chain instead; this function should be eliminated
14422 someday. */
14423
14424void *
6c95b8df
PA
14425deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14426 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14427{
14428 struct bp_target_info *bp_tgt;
14429
6c95b8df 14430 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14431
6c95b8df 14432 bp_tgt->placed_address_space = aspace;
8181d85f 14433 bp_tgt->placed_address = pc;
6c95b8df 14434
a6d9a66e 14435 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14436 {
14437 /* Could not insert the breakpoint. */
14438 xfree (bp_tgt);
14439 return NULL;
14440 }
14441
14442 return bp_tgt;
14443}
14444
4a64f543
MS
14445/* Remove a breakpoint BP inserted by
14446 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14447
14448int
a6d9a66e 14449deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14450{
14451 struct bp_target_info *bp_tgt = bp;
14452 int ret;
14453
a6d9a66e 14454 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14455 xfree (bp_tgt);
14456
14457 return ret;
14458}
14459
4a64f543
MS
14460/* One (or perhaps two) breakpoints used for software single
14461 stepping. */
8181d85f
DJ
14462
14463static void *single_step_breakpoints[2];
a6d9a66e 14464static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14465
14466/* Create and insert a breakpoint for software single step. */
14467
14468void
6c95b8df 14469insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14470 struct address_space *aspace,
14471 CORE_ADDR next_pc)
8181d85f
DJ
14472{
14473 void **bpt_p;
14474
14475 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14476 {
14477 bpt_p = &single_step_breakpoints[0];
14478 single_step_gdbarch[0] = gdbarch;
14479 }
8181d85f
DJ
14480 else
14481 {
14482 gdb_assert (single_step_breakpoints[1] == NULL);
14483 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14484 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14485 }
14486
4a64f543
MS
14487 /* NOTE drow/2006-04-11: A future improvement to this function would
14488 be to only create the breakpoints once, and actually put them on
14489 the breakpoint chain. That would let us use set_raw_breakpoint.
14490 We could adjust the addresses each time they were needed. Doing
14491 this requires corresponding changes elsewhere where single step
14492 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14493
6c95b8df 14494 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14495 if (*bpt_p == NULL)
5af949e3
UW
14496 error (_("Could not insert single-step breakpoint at %s"),
14497 paddress (gdbarch, next_pc));
8181d85f
DJ
14498}
14499
f02253f1
HZ
14500/* Check if the breakpoints used for software single stepping
14501 were inserted or not. */
14502
14503int
14504single_step_breakpoints_inserted (void)
14505{
14506 return (single_step_breakpoints[0] != NULL
14507 || single_step_breakpoints[1] != NULL);
14508}
14509
8181d85f
DJ
14510/* Remove and delete any breakpoints used for software single step. */
14511
14512void
14513remove_single_step_breakpoints (void)
14514{
14515 gdb_assert (single_step_breakpoints[0] != NULL);
14516
14517 /* See insert_single_step_breakpoint for more about this deprecated
14518 call. */
a6d9a66e
UW
14519 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14520 single_step_breakpoints[0]);
14521 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14522 single_step_breakpoints[0] = NULL;
14523
14524 if (single_step_breakpoints[1] != NULL)
14525 {
a6d9a66e
UW
14526 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14527 single_step_breakpoints[1]);
14528 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14529 single_step_breakpoints[1] = NULL;
14530 }
14531}
14532
d03285ec
UW
14533/* Delete software single step breakpoints without removing them from
14534 the inferior. This is intended to be used if the inferior's address
14535 space where they were inserted is already gone, e.g. after exit or
14536 exec. */
14537
14538void
14539cancel_single_step_breakpoints (void)
14540{
14541 int i;
14542
14543 for (i = 0; i < 2; i++)
14544 if (single_step_breakpoints[i])
14545 {
14546 xfree (single_step_breakpoints[i]);
14547 single_step_breakpoints[i] = NULL;
14548 single_step_gdbarch[i] = NULL;
14549 }
14550}
14551
14552/* Detach software single-step breakpoints from INFERIOR_PTID without
14553 removing them. */
14554
14555static void
14556detach_single_step_breakpoints (void)
14557{
14558 int i;
14559
14560 for (i = 0; i < 2; i++)
14561 if (single_step_breakpoints[i])
14562 target_remove_breakpoint (single_step_gdbarch[i],
14563 single_step_breakpoints[i]);
14564}
14565
4a64f543
MS
14566/* Check whether a software single-step breakpoint is inserted at
14567 PC. */
1aafd4da
UW
14568
14569static int
cc59ec59
MS
14570single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14571 CORE_ADDR pc)
1aafd4da
UW
14572{
14573 int i;
14574
14575 for (i = 0; i < 2; i++)
14576 {
14577 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14578 if (bp_tgt
14579 && breakpoint_address_match (bp_tgt->placed_address_space,
14580 bp_tgt->placed_address,
14581 aspace, pc))
1aafd4da
UW
14582 return 1;
14583 }
14584
14585 return 0;
14586}
14587
a96d9b2e
SDJ
14588/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14589 non-zero otherwise. */
14590static int
14591is_syscall_catchpoint_enabled (struct breakpoint *bp)
14592{
14593 if (syscall_catchpoint_p (bp)
14594 && bp->enable_state != bp_disabled
14595 && bp->enable_state != bp_call_disabled)
14596 return 1;
14597 else
14598 return 0;
14599}
14600
14601int
14602catch_syscall_enabled (void)
14603{
fa3064dd
YQ
14604 struct catch_syscall_inferior_data *inf_data
14605 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 14606
fa3064dd 14607 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
14608}
14609
14610int
14611catching_syscall_number (int syscall_number)
14612{
14613 struct breakpoint *bp;
14614
14615 ALL_BREAKPOINTS (bp)
14616 if (is_syscall_catchpoint_enabled (bp))
14617 {
be5c67c1
PA
14618 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14619
14620 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
14621 {
14622 int i, iter;
14623 for (i = 0;
be5c67c1 14624 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
14625 i++)
14626 if (syscall_number == iter)
14627 return 1;
14628 }
14629 else
14630 return 1;
14631 }
14632
14633 return 0;
14634}
14635
14636/* Complete syscall names. Used by "catch syscall". */
49c4e619 14637static VEC (char_ptr) *
a96d9b2e
SDJ
14638catch_syscall_completer (struct cmd_list_element *cmd,
14639 char *text, char *word)
14640{
14641 const char **list = get_syscall_names ();
49c4e619 14642 VEC (char_ptr) *retlist
c38eea1a 14643 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 14644
c38eea1a
MS
14645 xfree (list);
14646 return retlist;
a96d9b2e
SDJ
14647}
14648
1042e4c0
SS
14649/* Tracepoint-specific operations. */
14650
14651/* Set tracepoint count to NUM. */
14652static void
14653set_tracepoint_count (int num)
14654{
14655 tracepoint_count = num;
4fa62494 14656 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14657}
14658
70221824 14659static void
1042e4c0
SS
14660trace_command (char *arg, int from_tty)
14661{
55aa24fb
SDJ
14662 struct breakpoint_ops *ops;
14663 const char *arg_cp = arg;
14664
14665 if (arg && probe_linespec_to_ops (&arg_cp))
14666 ops = &tracepoint_probe_breakpoint_ops;
14667 else
14668 ops = &tracepoint_breakpoint_ops;
14669
8cdf0e15
VP
14670 if (create_breakpoint (get_current_arch (),
14671 arg,
e7e0cddf 14672 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14673 0 /* tempflag */,
14674 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
14675 0 /* Ignore count */,
14676 pending_break_support,
55aa24fb 14677 ops,
8cdf0e15 14678 from_tty,
84f4c1fe 14679 1 /* enabled */,
44f238bb 14680 0 /* internal */, 0))
fd9b8c24 14681 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
14682}
14683
70221824 14684static void
7a697b8d
SS
14685ftrace_command (char *arg, int from_tty)
14686{
8cdf0e15
VP
14687 if (create_breakpoint (get_current_arch (),
14688 arg,
e7e0cddf 14689 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14690 0 /* tempflag */,
14691 bp_fast_tracepoint /* type_wanted */,
14692 0 /* Ignore count */,
14693 pending_break_support,
348d480f 14694 &tracepoint_breakpoint_ops,
0fb4aa4b 14695 from_tty,
84f4c1fe 14696 1 /* enabled */,
44f238bb 14697 0 /* internal */, 0))
0fb4aa4b
PA
14698 set_tracepoint_count (breakpoint_count);
14699}
14700
14701/* strace command implementation. Creates a static tracepoint. */
14702
70221824 14703static void
0fb4aa4b
PA
14704strace_command (char *arg, int from_tty)
14705{
983af33b
SDJ
14706 struct breakpoint_ops *ops;
14707
14708 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14709 or with a normal static tracepoint. */
14710 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14711 ops = &strace_marker_breakpoint_ops;
14712 else
14713 ops = &tracepoint_breakpoint_ops;
14714
0fb4aa4b
PA
14715 if (create_breakpoint (get_current_arch (),
14716 arg,
e7e0cddf 14717 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14718 0 /* tempflag */,
14719 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
14720 0 /* Ignore count */,
14721 pending_break_support,
983af33b 14722 ops,
8cdf0e15 14723 from_tty,
84f4c1fe 14724 1 /* enabled */,
44f238bb 14725 0 /* internal */, 0))
fd9b8c24 14726 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
14727}
14728
409873ef
SS
14729/* Set up a fake reader function that gets command lines from a linked
14730 list that was acquired during tracepoint uploading. */
14731
14732static struct uploaded_tp *this_utp;
3149d8c1 14733static int next_cmd;
409873ef
SS
14734
14735static char *
14736read_uploaded_action (void)
14737{
14738 char *rslt;
14739
3149d8c1 14740 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14741
3149d8c1 14742 next_cmd++;
409873ef
SS
14743
14744 return rslt;
14745}
14746
00bf0b85
SS
14747/* Given information about a tracepoint as recorded on a target (which
14748 can be either a live system or a trace file), attempt to create an
14749 equivalent GDB tracepoint. This is not a reliable process, since
14750 the target does not necessarily have all the information used when
14751 the tracepoint was originally defined. */
14752
d9b3f62e 14753struct tracepoint *
00bf0b85 14754create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14755{
409873ef 14756 char *addr_str, small_buf[100];
d9b3f62e 14757 struct tracepoint *tp;
fd9b8c24 14758
409873ef
SS
14759 if (utp->at_string)
14760 addr_str = utp->at_string;
14761 else
14762 {
14763 /* In the absence of a source location, fall back to raw
14764 address. Since there is no way to confirm that the address
14765 means the same thing as when the trace was started, warn the
14766 user. */
3e43a32a
MS
14767 warning (_("Uploaded tracepoint %d has no "
14768 "source location, using raw address"),
409873ef
SS
14769 utp->number);
14770 sprintf (small_buf, "*%s", hex_string (utp->addr));
14771 addr_str = small_buf;
14772 }
14773
14774 /* There's not much we can do with a sequence of bytecodes. */
14775 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14776 warning (_("Uploaded tracepoint %d condition "
14777 "has no source form, ignoring it"),
409873ef 14778 utp->number);
d5551862 14779
8cdf0e15 14780 if (!create_breakpoint (get_current_arch (),
409873ef 14781 addr_str,
e7e0cddf
SS
14782 utp->cond_string, -1, NULL,
14783 0 /* parse cond/thread */,
8cdf0e15 14784 0 /* tempflag */,
0fb4aa4b 14785 utp->type /* type_wanted */,
8cdf0e15
VP
14786 0 /* Ignore count */,
14787 pending_break_support,
348d480f 14788 &tracepoint_breakpoint_ops,
8cdf0e15 14789 0 /* from_tty */,
84f4c1fe 14790 utp->enabled /* enabled */,
44f238bb
PA
14791 0 /* internal */,
14792 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14793 return NULL;
14794
00bf0b85
SS
14795 set_tracepoint_count (breakpoint_count);
14796
409873ef 14797 /* Get the tracepoint we just created. */
fd9b8c24
PA
14798 tp = get_tracepoint (tracepoint_count);
14799 gdb_assert (tp != NULL);
d5551862 14800
00bf0b85
SS
14801 if (utp->pass > 0)
14802 {
d9b3f62e 14803 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 14804
409873ef 14805 trace_pass_command (small_buf, 0);
00bf0b85
SS
14806 }
14807
409873ef
SS
14808 /* If we have uploaded versions of the original commands, set up a
14809 special-purpose "reader" function and call the usual command line
14810 reader, then pass the result to the breakpoint command-setting
14811 function. */
3149d8c1 14812 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14813 {
409873ef 14814 struct command_line *cmd_list;
00bf0b85 14815
409873ef 14816 this_utp = utp;
3149d8c1 14817 next_cmd = 0;
d5551862 14818
409873ef
SS
14819 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14820
d9b3f62e 14821 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 14822 }
3149d8c1
SS
14823 else if (!VEC_empty (char_ptr, utp->actions)
14824 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14825 warning (_("Uploaded tracepoint %d actions "
14826 "have no source form, ignoring them"),
409873ef 14827 utp->number);
00bf0b85 14828
f196051f
SS
14829 /* Copy any status information that might be available. */
14830 tp->base.hit_count = utp->hit_count;
14831 tp->traceframe_usage = utp->traceframe_usage;
14832
00bf0b85 14833 return tp;
d9b3f62e 14834}
00bf0b85 14835
1042e4c0
SS
14836/* Print information on tracepoint number TPNUM_EXP, or all if
14837 omitted. */
14838
14839static void
e5a67952 14840tracepoints_info (char *args, int from_tty)
1042e4c0 14841{
79a45e25 14842 struct ui_out *uiout = current_uiout;
e5a67952 14843 int num_printed;
1042e4c0 14844
e5a67952 14845 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14846
14847 if (num_printed == 0)
1042e4c0 14848 {
e5a67952 14849 if (args == NULL || *args == '\0')
d77f58be
SS
14850 ui_out_message (uiout, 0, "No tracepoints.\n");
14851 else
e5a67952 14852 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 14853 }
ad443146
SS
14854
14855 default_collect_info ();
1042e4c0
SS
14856}
14857
4a64f543 14858/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14859 Not supported by all targets. */
14860static void
14861enable_trace_command (char *args, int from_tty)
14862{
14863 enable_command (args, from_tty);
14864}
14865
4a64f543 14866/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14867 Not supported by all targets. */
14868static void
14869disable_trace_command (char *args, int from_tty)
14870{
14871 disable_command (args, from_tty);
14872}
14873
4a64f543 14874/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
14875static void
14876delete_trace_command (char *arg, int from_tty)
14877{
35df4500 14878 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14879
14880 dont_repeat ();
14881
14882 if (arg == 0)
14883 {
14884 int breaks_to_delete = 0;
14885
14886 /* Delete all breakpoints if no argument.
14887 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14888 have to be deleted with an explicit breakpoint number
14889 argument. */
1042e4c0 14890 ALL_TRACEPOINTS (b)
46c6471b 14891 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14892 {
14893 breaks_to_delete = 1;
14894 break;
14895 }
1042e4c0
SS
14896
14897 /* Ask user only if there are some breakpoints to delete. */
14898 if (!from_tty
14899 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14900 {
35df4500 14901 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14902 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14903 delete_breakpoint (b);
1042e4c0
SS
14904 }
14905 }
14906 else
51be5b68 14907 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
14908}
14909
197f0a60
TT
14910/* Helper function for trace_pass_command. */
14911
14912static void
d9b3f62e 14913trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14914{
d9b3f62e
PA
14915 tp->pass_count = count;
14916 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
14917 if (from_tty)
14918 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 14919 tp->base.number, count);
197f0a60
TT
14920}
14921
1042e4c0
SS
14922/* Set passcount for tracepoint.
14923
14924 First command argument is passcount, second is tracepoint number.
14925 If tracepoint number omitted, apply to most recently defined.
14926 Also accepts special argument "all". */
14927
14928static void
14929trace_pass_command (char *args, int from_tty)
14930{
d9b3f62e 14931 struct tracepoint *t1;
1042e4c0 14932 unsigned int count;
1042e4c0
SS
14933
14934 if (args == 0 || *args == 0)
3e43a32a
MS
14935 error (_("passcount command requires an "
14936 "argument (count + optional TP num)"));
1042e4c0 14937
4a64f543 14938 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
14939
14940 while (*args && isspace ((int) *args))
14941 args++;
14942
14943 if (*args && strncasecmp (args, "all", 3) == 0)
14944 {
d9b3f62e
PA
14945 struct breakpoint *b;
14946
1042e4c0 14947 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14948 if (*args)
14949 error (_("Junk at end of arguments."));
1042e4c0 14950
d9b3f62e 14951 ALL_TRACEPOINTS (b)
197f0a60 14952 {
d9b3f62e 14953 t1 = (struct tracepoint *) b;
197f0a60
TT
14954 trace_pass_set_count (t1, count, from_tty);
14955 }
14956 }
14957 else if (*args == '\0')
1042e4c0 14958 {
197f0a60 14959 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 14960 if (t1)
197f0a60
TT
14961 trace_pass_set_count (t1, count, from_tty);
14962 }
14963 else
14964 {
14965 struct get_number_or_range_state state;
14966
14967 init_number_or_range (&state, args);
14968 while (!state.finished)
1042e4c0 14969 {
197f0a60
TT
14970 t1 = get_tracepoint_by_number (&args, &state, 1);
14971 if (t1)
14972 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14973 }
14974 }
1042e4c0
SS
14975}
14976
d9b3f62e 14977struct tracepoint *
1042e4c0
SS
14978get_tracepoint (int num)
14979{
14980 struct breakpoint *t;
14981
14982 ALL_TRACEPOINTS (t)
14983 if (t->number == num)
d9b3f62e 14984 return (struct tracepoint *) t;
1042e4c0
SS
14985
14986 return NULL;
14987}
14988
d5551862
SS
14989/* Find the tracepoint with the given target-side number (which may be
14990 different from the tracepoint number after disconnecting and
14991 reconnecting). */
14992
d9b3f62e 14993struct tracepoint *
d5551862
SS
14994get_tracepoint_by_number_on_target (int num)
14995{
d9b3f62e 14996 struct breakpoint *b;
d5551862 14997
d9b3f62e
PA
14998 ALL_TRACEPOINTS (b)
14999 {
15000 struct tracepoint *t = (struct tracepoint *) b;
15001
15002 if (t->number_on_target == num)
15003 return t;
15004 }
d5551862
SS
15005
15006 return NULL;
15007}
15008
1042e4c0 15009/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15010 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15011 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15012 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15013struct tracepoint *
197f0a60
TT
15014get_tracepoint_by_number (char **arg,
15015 struct get_number_or_range_state *state,
15016 int optional_p)
1042e4c0
SS
15017{
15018 extern int tracepoint_count;
15019 struct breakpoint *t;
15020 int tpnum;
15021 char *instring = arg == NULL ? NULL : *arg;
15022
197f0a60
TT
15023 if (state)
15024 {
15025 gdb_assert (!state->finished);
15026 tpnum = get_number_or_range (state);
15027 }
15028 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15029 {
15030 if (optional_p)
15031 tpnum = tracepoint_count;
15032 else
15033 error_no_arg (_("tracepoint number"));
15034 }
15035 else
197f0a60 15036 tpnum = get_number (arg);
1042e4c0
SS
15037
15038 if (tpnum <= 0)
15039 {
15040 if (instring && *instring)
15041 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15042 instring);
15043 else
3e43a32a
MS
15044 printf_filtered (_("Tracepoint argument missing "
15045 "and no previous tracepoint\n"));
1042e4c0
SS
15046 return NULL;
15047 }
15048
15049 ALL_TRACEPOINTS (t)
15050 if (t->number == tpnum)
15051 {
d9b3f62e 15052 return (struct tracepoint *) t;
1042e4c0
SS
15053 }
15054
1042e4c0
SS
15055 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15056 return NULL;
15057}
15058
d9b3f62e
PA
15059void
15060print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15061{
15062 if (b->thread != -1)
15063 fprintf_unfiltered (fp, " thread %d", b->thread);
15064
15065 if (b->task != 0)
15066 fprintf_unfiltered (fp, " task %d", b->task);
15067
15068 fprintf_unfiltered (fp, "\n");
15069}
15070
6149aea9
PA
15071/* Save information on user settable breakpoints (watchpoints, etc) to
15072 a new script file named FILENAME. If FILTER is non-NULL, call it
15073 on each breakpoint and only include the ones for which it returns
15074 non-zero. */
15075
1042e4c0 15076static void
6149aea9
PA
15077save_breakpoints (char *filename, int from_tty,
15078 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15079{
15080 struct breakpoint *tp;
6149aea9 15081 int any = 0;
a7bdde9e 15082 char *pathname;
1042e4c0 15083 struct cleanup *cleanup;
a7bdde9e 15084 struct ui_file *fp;
6149aea9 15085 int extra_trace_bits = 0;
1042e4c0 15086
6149aea9
PA
15087 if (filename == 0 || *filename == 0)
15088 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15089
15090 /* See if we have anything to save. */
6149aea9 15091 ALL_BREAKPOINTS (tp)
1042e4c0 15092 {
6149aea9 15093 /* Skip internal and momentary breakpoints. */
09d682a4 15094 if (!user_breakpoint_p (tp))
6149aea9
PA
15095 continue;
15096
15097 /* If we have a filter, only save the breakpoints it accepts. */
15098 if (filter && !filter (tp))
15099 continue;
15100
15101 any = 1;
15102
15103 if (is_tracepoint (tp))
15104 {
15105 extra_trace_bits = 1;
15106
15107 /* We can stop searching. */
15108 break;
15109 }
1042e4c0 15110 }
6149aea9
PA
15111
15112 if (!any)
1042e4c0 15113 {
6149aea9 15114 warning (_("Nothing to save."));
1042e4c0
SS
15115 return;
15116 }
15117
6149aea9 15118 pathname = tilde_expand (filename);
1042e4c0 15119 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 15120 fp = gdb_fopen (pathname, "w");
059fb39f 15121 if (!fp)
6149aea9
PA
15122 error (_("Unable to open file '%s' for saving (%s)"),
15123 filename, safe_strerror (errno));
a7bdde9e 15124 make_cleanup_ui_file_delete (fp);
8bf6485c 15125
6149aea9
PA
15126 if (extra_trace_bits)
15127 save_trace_state_variables (fp);
8bf6485c 15128
6149aea9 15129 ALL_BREAKPOINTS (tp)
1042e4c0 15130 {
6149aea9 15131 /* Skip internal and momentary breakpoints. */
09d682a4 15132 if (!user_breakpoint_p (tp))
6149aea9 15133 continue;
8bf6485c 15134
6149aea9
PA
15135 /* If we have a filter, only save the breakpoints it accepts. */
15136 if (filter && !filter (tp))
15137 continue;
15138
348d480f 15139 tp->ops->print_recreate (tp, fp);
1042e4c0 15140
6149aea9
PA
15141 /* Note, we can't rely on tp->number for anything, as we can't
15142 assume the recreated breakpoint numbers will match. Use $bpnum
15143 instead. */
15144
15145 if (tp->cond_string)
15146 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15147
15148 if (tp->ignore_count)
15149 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15150
a7bdde9e 15151 if (tp->commands)
1042e4c0 15152 {
a7bdde9e
VP
15153 volatile struct gdb_exception ex;
15154
6149aea9 15155 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15156
79a45e25 15157 ui_out_redirect (current_uiout, fp);
14dba4b4 15158 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15159 {
79a45e25 15160 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15161 }
79a45e25 15162 ui_out_redirect (current_uiout, NULL);
1042e4c0 15163
a7bdde9e
VP
15164 if (ex.reason < 0)
15165 throw_exception (ex);
1042e4c0 15166
a7bdde9e 15167 fprintf_unfiltered (fp, " end\n");
1042e4c0 15168 }
6149aea9
PA
15169
15170 if (tp->enable_state == bp_disabled)
15171 fprintf_unfiltered (fp, "disable\n");
15172
15173 /* If this is a multi-location breakpoint, check if the locations
15174 should be individually disabled. Watchpoint locations are
15175 special, and not user visible. */
15176 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15177 {
15178 struct bp_location *loc;
15179 int n = 1;
15180
15181 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15182 if (!loc->enabled)
15183 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15184 }
1042e4c0 15185 }
8bf6485c 15186
6149aea9 15187 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15188 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15189
1042e4c0
SS
15190 do_cleanups (cleanup);
15191 if (from_tty)
6149aea9
PA
15192 printf_filtered (_("Saved to file '%s'.\n"), filename);
15193}
15194
15195/* The `save breakpoints' command. */
15196
15197static void
15198save_breakpoints_command (char *args, int from_tty)
15199{
15200 save_breakpoints (args, from_tty, NULL);
15201}
15202
15203/* The `save tracepoints' command. */
15204
15205static void
15206save_tracepoints_command (char *args, int from_tty)
15207{
15208 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15209}
15210
15211/* Create a vector of all tracepoints. */
15212
15213VEC(breakpoint_p) *
eeae04df 15214all_tracepoints (void)
1042e4c0
SS
15215{
15216 VEC(breakpoint_p) *tp_vec = 0;
15217 struct breakpoint *tp;
15218
15219 ALL_TRACEPOINTS (tp)
15220 {
15221 VEC_safe_push (breakpoint_p, tp_vec, tp);
15222 }
15223
15224 return tp_vec;
15225}
15226
c906108c 15227\f
4a64f543
MS
15228/* This help string is used for the break, hbreak, tbreak and thbreak
15229 commands. It is defined as a macro to prevent duplication.
15230 COMMAND should be a string constant containing the name of the
15231 command. */
31e2b00f
AS
15232#define BREAK_ARGS_HELP(command) \
15233command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15234LOCATION may be a line number, function name, or \"*\" and an address.\n\
15235If a line number is specified, break at start of code for that line.\n\
15236If a function is specified, break at start of code for that function.\n\
15237If an address is specified, break at that exact address.\n\
dc10affe
PA
15238With no LOCATION, uses current execution address of the selected\n\
15239stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15240\n\
15241THREADNUM is the number from \"info threads\".\n\
15242CONDITION is a boolean expression.\n\
15243\n\
d41c0fc8
PA
15244Multiple breakpoints at one place are permitted, and useful if their\n\
15245conditions are different.\n\
31e2b00f
AS
15246\n\
15247Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15248
44feb3ce
TT
15249/* List of subcommands for "catch". */
15250static struct cmd_list_element *catch_cmdlist;
15251
15252/* List of subcommands for "tcatch". */
15253static struct cmd_list_element *tcatch_cmdlist;
15254
9ac4176b 15255void
44feb3ce
TT
15256add_catch_command (char *name, char *docstring,
15257 void (*sfunc) (char *args, int from_tty,
15258 struct cmd_list_element *command),
625e8578 15259 completer_ftype *completer,
44feb3ce
TT
15260 void *user_data_catch,
15261 void *user_data_tcatch)
15262{
15263 struct cmd_list_element *command;
15264
15265 command = add_cmd (name, class_breakpoint, NULL, docstring,
15266 &catch_cmdlist);
15267 set_cmd_sfunc (command, sfunc);
15268 set_cmd_context (command, user_data_catch);
a96d9b2e 15269 set_cmd_completer (command, completer);
44feb3ce
TT
15270
15271 command = add_cmd (name, class_breakpoint, NULL, docstring,
15272 &tcatch_cmdlist);
15273 set_cmd_sfunc (command, sfunc);
15274 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15275 set_cmd_completer (command, completer);
44feb3ce
TT
15276}
15277
6c95b8df 15278static void
a79b8f6e 15279clear_syscall_counts (struct inferior *inf)
6c95b8df 15280{
fa3064dd
YQ
15281 struct catch_syscall_inferior_data *inf_data
15282 = get_catch_syscall_inferior_data (inf);
15283
15284 inf_data->total_syscalls_count = 0;
15285 inf_data->any_syscall_count = 0;
15286 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15287}
15288
6149aea9
PA
15289static void
15290save_command (char *arg, int from_tty)
15291{
3e43a32a
MS
15292 printf_unfiltered (_("\"save\" must be followed by "
15293 "the name of a save subcommand.\n"));
6149aea9
PA
15294 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15295}
15296
84f4c1fe
PM
15297struct breakpoint *
15298iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15299 void *data)
15300{
35df4500 15301 struct breakpoint *b, *b_tmp;
84f4c1fe 15302
35df4500 15303 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15304 {
15305 if ((*callback) (b, data))
15306 return b;
15307 }
15308
15309 return NULL;
15310}
15311
0574c78f
GB
15312/* Zero if any of the breakpoint's locations could be a location where
15313 functions have been inlined, nonzero otherwise. */
15314
15315static int
15316is_non_inline_function (struct breakpoint *b)
15317{
15318 /* The shared library event breakpoint is set on the address of a
15319 non-inline function. */
15320 if (b->type == bp_shlib_event)
15321 return 1;
15322
15323 return 0;
15324}
15325
15326/* Nonzero if the specified PC cannot be a location where functions
15327 have been inlined. */
15328
15329int
09ac7c10
TT
15330pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15331 const struct target_waitstatus *ws)
0574c78f
GB
15332{
15333 struct breakpoint *b;
15334 struct bp_location *bl;
15335
15336 ALL_BREAKPOINTS (b)
15337 {
15338 if (!is_non_inline_function (b))
15339 continue;
15340
15341 for (bl = b->loc; bl != NULL; bl = bl->next)
15342 {
15343 if (!bl->shlib_disabled
09ac7c10 15344 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15345 return 1;
15346 }
15347 }
15348
15349 return 0;
15350}
15351
2060206e
PA
15352void
15353initialize_breakpoint_ops (void)
15354{
15355 static int initialized = 0;
15356
15357 struct breakpoint_ops *ops;
15358
15359 if (initialized)
15360 return;
15361 initialized = 1;
15362
15363 /* The breakpoint_ops structure to be inherit by all kinds of
15364 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15365 internal and momentary breakpoints, etc.). */
15366 ops = &bkpt_base_breakpoint_ops;
15367 *ops = base_breakpoint_ops;
15368 ops->re_set = bkpt_re_set;
15369 ops->insert_location = bkpt_insert_location;
15370 ops->remove_location = bkpt_remove_location;
15371 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15372 ops->create_sals_from_address = bkpt_create_sals_from_address;
15373 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15374 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15375
15376 /* The breakpoint_ops structure to be used in regular breakpoints. */
15377 ops = &bkpt_breakpoint_ops;
15378 *ops = bkpt_base_breakpoint_ops;
15379 ops->re_set = bkpt_re_set;
15380 ops->resources_needed = bkpt_resources_needed;
15381 ops->print_it = bkpt_print_it;
15382 ops->print_mention = bkpt_print_mention;
15383 ops->print_recreate = bkpt_print_recreate;
15384
15385 /* Ranged breakpoints. */
15386 ops = &ranged_breakpoint_ops;
15387 *ops = bkpt_breakpoint_ops;
15388 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15389 ops->resources_needed = resources_needed_ranged_breakpoint;
15390 ops->print_it = print_it_ranged_breakpoint;
15391 ops->print_one = print_one_ranged_breakpoint;
15392 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15393 ops->print_mention = print_mention_ranged_breakpoint;
15394 ops->print_recreate = print_recreate_ranged_breakpoint;
15395
15396 /* Internal breakpoints. */
15397 ops = &internal_breakpoint_ops;
15398 *ops = bkpt_base_breakpoint_ops;
15399 ops->re_set = internal_bkpt_re_set;
15400 ops->check_status = internal_bkpt_check_status;
15401 ops->print_it = internal_bkpt_print_it;
15402 ops->print_mention = internal_bkpt_print_mention;
15403
15404 /* Momentary breakpoints. */
15405 ops = &momentary_breakpoint_ops;
15406 *ops = bkpt_base_breakpoint_ops;
15407 ops->re_set = momentary_bkpt_re_set;
15408 ops->check_status = momentary_bkpt_check_status;
15409 ops->print_it = momentary_bkpt_print_it;
15410 ops->print_mention = momentary_bkpt_print_mention;
15411
55aa24fb
SDJ
15412 /* Probe breakpoints. */
15413 ops = &bkpt_probe_breakpoint_ops;
15414 *ops = bkpt_breakpoint_ops;
15415 ops->insert_location = bkpt_probe_insert_location;
15416 ops->remove_location = bkpt_probe_remove_location;
15417 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15418 ops->decode_linespec = bkpt_probe_decode_linespec;
15419
2060206e
PA
15420 /* GNU v3 exception catchpoints. */
15421 ops = &gnu_v3_exception_catchpoint_ops;
15422 *ops = bkpt_breakpoint_ops;
15423 ops->print_it = print_it_exception_catchpoint;
15424 ops->print_one = print_one_exception_catchpoint;
15425 ops->print_mention = print_mention_exception_catchpoint;
15426 ops->print_recreate = print_recreate_exception_catchpoint;
15427
15428 /* Watchpoints. */
15429 ops = &watchpoint_breakpoint_ops;
15430 *ops = base_breakpoint_ops;
3a5c3e22 15431 ops->dtor = dtor_watchpoint;
2060206e
PA
15432 ops->re_set = re_set_watchpoint;
15433 ops->insert_location = insert_watchpoint;
15434 ops->remove_location = remove_watchpoint;
15435 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15436 ops->check_status = check_status_watchpoint;
15437 ops->resources_needed = resources_needed_watchpoint;
15438 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15439 ops->print_it = print_it_watchpoint;
15440 ops->print_mention = print_mention_watchpoint;
15441 ops->print_recreate = print_recreate_watchpoint;
15442
15443 /* Masked watchpoints. */
15444 ops = &masked_watchpoint_breakpoint_ops;
15445 *ops = watchpoint_breakpoint_ops;
15446 ops->insert_location = insert_masked_watchpoint;
15447 ops->remove_location = remove_masked_watchpoint;
15448 ops->resources_needed = resources_needed_masked_watchpoint;
15449 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15450 ops->print_it = print_it_masked_watchpoint;
15451 ops->print_one_detail = print_one_detail_masked_watchpoint;
15452 ops->print_mention = print_mention_masked_watchpoint;
15453 ops->print_recreate = print_recreate_masked_watchpoint;
15454
15455 /* Tracepoints. */
15456 ops = &tracepoint_breakpoint_ops;
15457 *ops = base_breakpoint_ops;
15458 ops->re_set = tracepoint_re_set;
15459 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15460 ops->print_one_detail = tracepoint_print_one_detail;
15461 ops->print_mention = tracepoint_print_mention;
15462 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15463 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15464 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15465 ops->decode_linespec = tracepoint_decode_linespec;
15466
55aa24fb
SDJ
15467 /* Probe tracepoints. */
15468 ops = &tracepoint_probe_breakpoint_ops;
15469 *ops = tracepoint_breakpoint_ops;
15470 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15471 ops->decode_linespec = tracepoint_probe_decode_linespec;
15472
983af33b
SDJ
15473 /* Static tracepoints with marker (`-m'). */
15474 ops = &strace_marker_breakpoint_ops;
15475 *ops = tracepoint_breakpoint_ops;
15476 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15477 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15478 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15479
15480 /* Fork catchpoints. */
15481 ops = &catch_fork_breakpoint_ops;
15482 *ops = base_breakpoint_ops;
15483 ops->insert_location = insert_catch_fork;
15484 ops->remove_location = remove_catch_fork;
15485 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15486 ops->print_it = print_it_catch_fork;
15487 ops->print_one = print_one_catch_fork;
15488 ops->print_mention = print_mention_catch_fork;
15489 ops->print_recreate = print_recreate_catch_fork;
15490
15491 /* Vfork catchpoints. */
15492 ops = &catch_vfork_breakpoint_ops;
15493 *ops = base_breakpoint_ops;
15494 ops->insert_location = insert_catch_vfork;
15495 ops->remove_location = remove_catch_vfork;
15496 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15497 ops->print_it = print_it_catch_vfork;
15498 ops->print_one = print_one_catch_vfork;
15499 ops->print_mention = print_mention_catch_vfork;
15500 ops->print_recreate = print_recreate_catch_vfork;
15501
15502 /* Exec catchpoints. */
15503 ops = &catch_exec_breakpoint_ops;
15504 *ops = base_breakpoint_ops;
15505 ops->dtor = dtor_catch_exec;
15506 ops->insert_location = insert_catch_exec;
15507 ops->remove_location = remove_catch_exec;
15508 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15509 ops->print_it = print_it_catch_exec;
15510 ops->print_one = print_one_catch_exec;
15511 ops->print_mention = print_mention_catch_exec;
15512 ops->print_recreate = print_recreate_catch_exec;
15513
15514 /* Syscall catchpoints. */
15515 ops = &catch_syscall_breakpoint_ops;
15516 *ops = base_breakpoint_ops;
15517 ops->dtor = dtor_catch_syscall;
15518 ops->insert_location = insert_catch_syscall;
15519 ops->remove_location = remove_catch_syscall;
15520 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15521 ops->print_it = print_it_catch_syscall;
15522 ops->print_one = print_one_catch_syscall;
15523 ops->print_mention = print_mention_catch_syscall;
15524 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15525
15526 /* Solib-related catchpoints. */
15527 ops = &catch_solib_breakpoint_ops;
15528 *ops = base_breakpoint_ops;
15529 ops->dtor = dtor_catch_solib;
15530 ops->insert_location = insert_catch_solib;
15531 ops->remove_location = remove_catch_solib;
15532 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15533 ops->check_status = check_status_catch_solib;
15534 ops->print_it = print_it_catch_solib;
15535 ops->print_one = print_one_catch_solib;
15536 ops->print_mention = print_mention_catch_solib;
15537 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15538
15539 ops = &dprintf_breakpoint_ops;
15540 *ops = bkpt_base_breakpoint_ops;
15541 ops->re_set = bkpt_re_set;
15542 ops->resources_needed = bkpt_resources_needed;
15543 ops->print_it = bkpt_print_it;
15544 ops->print_mention = bkpt_print_mention;
15545 ops->print_recreate = bkpt_print_recreate;
2060206e
PA
15546}
15547
c906108c 15548void
fba45db2 15549_initialize_breakpoint (void)
c906108c
SS
15550{
15551 struct cmd_list_element *c;
15552
2060206e
PA
15553 initialize_breakpoint_ops ();
15554
84acb35a 15555 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15556 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15557 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15558
55aa24fb
SDJ
15559 breakpoint_objfile_key
15560 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15561
fa3064dd
YQ
15562 catch_syscall_inferior_data
15563 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15564
c906108c
SS
15565 breakpoint_chain = 0;
15566 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15567 before a breakpoint is set. */
15568 breakpoint_count = 0;
15569
1042e4c0
SS
15570 tracepoint_count = 0;
15571
1bedd215
AC
15572 add_com ("ignore", class_breakpoint, ignore_command, _("\
15573Set ignore-count of breakpoint number N to COUNT.\n\
15574Usage is `ignore N COUNT'."));
c906108c 15575 if (xdb_commands)
c5aa993b 15576 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15577
1bedd215
AC
15578 add_com ("commands", class_breakpoint, commands_command, _("\
15579Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15580Give breakpoint number as argument after \"commands\".\n\
15581With no argument, the targeted breakpoint is the last one set.\n\
15582The commands themselves follow starting on the next line.\n\
15583Type a line containing \"end\" to indicate the end of them.\n\
15584Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15585then no output is printed when it is hit, except what the commands print."));
c906108c 15586
d55637df 15587 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15588Specify breakpoint number N to break only if COND is true.\n\
c906108c 15589Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15590expression to be evaluated whenever breakpoint N is reached."));
d55637df 15591 set_cmd_completer (c, condition_completer);
c906108c 15592
1bedd215 15593 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15594Set a temporary breakpoint.\n\
c906108c
SS
15595Like \"break\" except the breakpoint is only temporary,\n\
15596so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15597by using \"enable delete\" on the breakpoint number.\n\
15598\n"
15599BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15600 set_cmd_completer (c, location_completer);
c94fdfd0 15601
1bedd215 15602 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15603Set a hardware assisted breakpoint.\n\
c906108c 15604Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15605some target hardware may not have this support.\n\
15606\n"
15607BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15608 set_cmd_completer (c, location_completer);
c906108c 15609
1bedd215 15610 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15611Set a temporary hardware assisted breakpoint.\n\
c906108c 15612Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15613so it will be deleted when hit.\n\
15614\n"
15615BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15616 set_cmd_completer (c, location_completer);
c906108c 15617
1bedd215
AC
15618 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15619Enable some breakpoints.\n\
c906108c
SS
15620Give breakpoint numbers (separated by spaces) as arguments.\n\
15621With no subcommand, breakpoints are enabled until you command otherwise.\n\
15622This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15623With a subcommand you can enable temporarily."),
c906108c
SS
15624 &enablelist, "enable ", 1, &cmdlist);
15625 if (xdb_commands)
1bedd215
AC
15626 add_com ("ab", class_breakpoint, enable_command, _("\
15627Enable some breakpoints.\n\
c906108c
SS
15628Give breakpoint numbers (separated by spaces) as arguments.\n\
15629With no subcommand, breakpoints are enabled until you command otherwise.\n\
15630This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15631With a subcommand you can enable temporarily."));
c906108c
SS
15632
15633 add_com_alias ("en", "enable", class_breakpoint, 1);
15634
84951ab5 15635 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15636Enable some breakpoints.\n\
c906108c
SS
15637Give breakpoint numbers (separated by spaces) as arguments.\n\
15638This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15639May be abbreviated to simply \"enable\".\n"),
c5aa993b 15640 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15641
1a966eab
AC
15642 add_cmd ("once", no_class, enable_once_command, _("\
15643Enable breakpoints for one hit. Give breakpoint numbers.\n\
15644If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15645 &enablebreaklist);
15646
1a966eab
AC
15647 add_cmd ("delete", no_class, enable_delete_command, _("\
15648Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15649If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15650 &enablebreaklist);
15651
816338b5
SS
15652 add_cmd ("count", no_class, enable_count_command, _("\
15653Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15654If a breakpoint is hit while enabled in this fashion,\n\
15655the count is decremented; when it reaches zero, the breakpoint is disabled."),
15656 &enablebreaklist);
15657
1a966eab
AC
15658 add_cmd ("delete", no_class, enable_delete_command, _("\
15659Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15660If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15661 &enablelist);
15662
1a966eab
AC
15663 add_cmd ("once", no_class, enable_once_command, _("\
15664Enable breakpoints for one hit. Give breakpoint numbers.\n\
15665If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15666 &enablelist);
15667
15668 add_cmd ("count", no_class, enable_count_command, _("\
15669Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15670If a breakpoint is hit while enabled in this fashion,\n\
15671the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15672 &enablelist);
15673
1bedd215
AC
15674 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15675Disable some breakpoints.\n\
c906108c
SS
15676Arguments are breakpoint numbers with spaces in between.\n\
15677To disable all breakpoints, give no argument.\n\
64b9b334 15678A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15679 &disablelist, "disable ", 1, &cmdlist);
15680 add_com_alias ("dis", "disable", class_breakpoint, 1);
15681 add_com_alias ("disa", "disable", class_breakpoint, 1);
15682 if (xdb_commands)
1bedd215
AC
15683 add_com ("sb", class_breakpoint, disable_command, _("\
15684Disable some breakpoints.\n\
c906108c
SS
15685Arguments are breakpoint numbers with spaces in between.\n\
15686To disable all breakpoints, give no argument.\n\
64b9b334 15687A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15688
1a966eab
AC
15689 add_cmd ("breakpoints", class_alias, disable_command, _("\
15690Disable some breakpoints.\n\
c906108c
SS
15691Arguments are breakpoint numbers with spaces in between.\n\
15692To disable all breakpoints, give no argument.\n\
64b9b334 15693A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15694This command may be abbreviated \"disable\"."),
c906108c
SS
15695 &disablelist);
15696
1bedd215
AC
15697 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15698Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15699Arguments are breakpoint numbers with spaces in between.\n\
15700To delete all breakpoints, give no argument.\n\
15701\n\
15702Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15703The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15704 &deletelist, "delete ", 1, &cmdlist);
15705 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15706 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15707 if (xdb_commands)
1bedd215
AC
15708 add_com ("db", class_breakpoint, delete_command, _("\
15709Delete some breakpoints.\n\
c906108c 15710Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15711To delete all breakpoints, give no argument.\n"));
c906108c 15712
1a966eab
AC
15713 add_cmd ("breakpoints", class_alias, delete_command, _("\
15714Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15715Arguments are breakpoint numbers with spaces in between.\n\
15716To delete all breakpoints, give no argument.\n\
1a966eab 15717This command may be abbreviated \"delete\"."),
c906108c
SS
15718 &deletelist);
15719
1bedd215
AC
15720 add_com ("clear", class_breakpoint, clear_command, _("\
15721Clear breakpoint at specified line or function.\n\
c906108c
SS
15722Argument may be line number, function name, or \"*\" and an address.\n\
15723If line number is specified, all breakpoints in that line are cleared.\n\
15724If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15725If an address is specified, breakpoints at that address are cleared.\n\
15726\n\
15727With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15728is executing in.\n\
15729\n\
1bedd215 15730See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15731 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15732
1bedd215 15733 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15734Set breakpoint at specified line or function.\n"
15735BREAK_ARGS_HELP ("break")));
5ba2abeb 15736 set_cmd_completer (c, location_completer);
c94fdfd0 15737
c906108c
SS
15738 add_com_alias ("b", "break", class_run, 1);
15739 add_com_alias ("br", "break", class_run, 1);
15740 add_com_alias ("bre", "break", class_run, 1);
15741 add_com_alias ("brea", "break", class_run, 1);
15742
7681d515
PM
15743 if (xdb_commands)
15744 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15745
15746 if (dbx_commands)
15747 {
1bedd215
AC
15748 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15749Break in function/address or break at a line in the current file."),
c5aa993b
JM
15750 &stoplist, "stop ", 1, &cmdlist);
15751 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15752 _("Break in function or address."), &stoplist);
c5aa993b 15753 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15754 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15755 add_com ("status", class_info, breakpoints_info, _("\
15756Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15757The \"Type\" column indicates one of:\n\
15758\tbreakpoint - normal breakpoint\n\
15759\twatchpoint - watchpoint\n\
15760The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15761the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15762breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15763address and file/line number respectively.\n\
15764\n\
15765Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15766are set to the address of the last breakpoint listed unless the command\n\
15767is prefixed with \"server \".\n\n\
c906108c 15768Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15769breakpoint set."));
c906108c
SS
15770 }
15771
1bedd215 15772 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15773Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15774The \"Type\" column indicates one of:\n\
15775\tbreakpoint - normal breakpoint\n\
15776\twatchpoint - watchpoint\n\
15777The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15778the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15779breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15780address and file/line number respectively.\n\
15781\n\
15782Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15783are set to the address of the last breakpoint listed unless the command\n\
15784is prefixed with \"server \".\n\n\
c906108c 15785Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15786breakpoint set."));
c906108c 15787
6b04bdb7
MS
15788 add_info_alias ("b", "breakpoints", 1);
15789
c906108c 15790 if (xdb_commands)
1bedd215
AC
15791 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15792Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15793The \"Type\" column indicates one of:\n\
15794\tbreakpoint - normal breakpoint\n\
15795\twatchpoint - watchpoint\n\
15796The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15797the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15798breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15799address and file/line number respectively.\n\
15800\n\
15801Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15802are set to the address of the last breakpoint listed unless the command\n\
15803is prefixed with \"server \".\n\n\
c906108c 15804Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15805breakpoint set."));
c906108c 15806
1a966eab
AC
15807 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15808Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15809The \"Type\" column indicates one of:\n\
15810\tbreakpoint - normal breakpoint\n\
15811\twatchpoint - watchpoint\n\
15812\tlongjmp - internal breakpoint used to step through longjmp()\n\
15813\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15814\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15815\tfinish - internal breakpoint used by the \"finish\" command\n\
15816The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15817the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15818breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15819address and file/line number respectively.\n\
15820\n\
15821Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15822are set to the address of the last breakpoint listed unless the command\n\
15823is prefixed with \"server \".\n\n\
c906108c 15824Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15825breakpoint set."),
c906108c
SS
15826 &maintenanceinfolist);
15827
44feb3ce
TT
15828 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15829Set catchpoints to catch events."),
15830 &catch_cmdlist, "catch ",
15831 0/*allow-unknown*/, &cmdlist);
15832
15833 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15834Set temporary catchpoints to catch events."),
15835 &tcatch_cmdlist, "tcatch ",
15836 0/*allow-unknown*/, &cmdlist);
15837
15838 /* Add catch and tcatch sub-commands. */
15839 add_catch_command ("catch", _("\
88e7d25d 15840Catch an exception, when caught."),
44feb3ce 15841 catch_catch_command,
a96d9b2e 15842 NULL,
44feb3ce
TT
15843 CATCH_PERMANENT,
15844 CATCH_TEMPORARY);
15845 add_catch_command ("throw", _("\
88e7d25d 15846Catch an exception, when thrown."),
44feb3ce 15847 catch_throw_command,
a96d9b2e 15848 NULL,
44feb3ce
TT
15849 CATCH_PERMANENT,
15850 CATCH_TEMPORARY);
15851 add_catch_command ("fork", _("Catch calls to fork."),
15852 catch_fork_command_1,
a96d9b2e 15853 NULL,
44feb3ce
TT
15854 (void *) (uintptr_t) catch_fork_permanent,
15855 (void *) (uintptr_t) catch_fork_temporary);
15856 add_catch_command ("vfork", _("Catch calls to vfork."),
15857 catch_fork_command_1,
a96d9b2e 15858 NULL,
44feb3ce
TT
15859 (void *) (uintptr_t) catch_vfork_permanent,
15860 (void *) (uintptr_t) catch_vfork_temporary);
15861 add_catch_command ("exec", _("Catch calls to exec."),
15862 catch_exec_command_1,
a96d9b2e
SDJ
15863 NULL,
15864 CATCH_PERMANENT,
15865 CATCH_TEMPORARY);
edcc5120
TT
15866 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15867Usage: catch load [REGEX]\n\
15868If REGEX is given, only stop for libraries matching the regular expression."),
15869 catch_load_command_1,
15870 NULL,
15871 CATCH_PERMANENT,
15872 CATCH_TEMPORARY);
15873 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15874Usage: catch unload [REGEX]\n\
15875If REGEX is given, only stop for libraries matching the regular expression."),
15876 catch_unload_command_1,
15877 NULL,
15878 CATCH_PERMANENT,
15879 CATCH_TEMPORARY);
a96d9b2e
SDJ
15880 add_catch_command ("syscall", _("\
15881Catch system calls by their names and/or numbers.\n\
15882Arguments say which system calls to catch. If no arguments\n\
15883are given, every system call will be caught.\n\
15884Arguments, if given, should be one or more system call names\n\
15885(if your system supports that), or system call numbers."),
15886 catch_syscall_command_1,
15887 catch_syscall_completer,
44feb3ce
TT
15888 CATCH_PERMANENT,
15889 CATCH_TEMPORARY);
c5aa993b 15890
1bedd215
AC
15891 c = add_com ("watch", class_breakpoint, watch_command, _("\
15892Set a watchpoint for an expression.\n\
06a64a0b 15893Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15894A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15895an expression changes.\n\
15896If -l or -location is given, this evaluates EXPRESSION and watches\n\
15897the memory to which it refers."));
65d12d83 15898 set_cmd_completer (c, expression_completer);
c906108c 15899
1bedd215
AC
15900 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15901Set a read watchpoint for an expression.\n\
06a64a0b 15902Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15903A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15904an expression is read.\n\
15905If -l or -location is given, this evaluates EXPRESSION and watches\n\
15906the memory to which it refers."));
65d12d83 15907 set_cmd_completer (c, expression_completer);
c906108c 15908
1bedd215
AC
15909 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15910Set a watchpoint for an expression.\n\
06a64a0b 15911Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15912A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15913an expression is either read or written.\n\
15914If -l or -location is given, this evaluates EXPRESSION and watches\n\
15915the memory to which it refers."));
65d12d83 15916 set_cmd_completer (c, expression_completer);
c906108c 15917
d77f58be 15918 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 15919Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15920
920d2a44
AC
15921 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15922 respond to changes - contrary to the description. */
85c07804
AC
15923 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15924 &can_use_hw_watchpoints, _("\
15925Set debugger's willingness to use watchpoint hardware."), _("\
15926Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15927If zero, gdb will not use hardware for new watchpoints, even if\n\
15928such is available. (However, any hardware watchpoints that were\n\
15929created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15930hardware.)"),
15931 NULL,
920d2a44 15932 show_can_use_hw_watchpoints,
85c07804 15933 &setlist, &showlist);
c906108c
SS
15934
15935 can_use_hw_watchpoints = 1;
fa8d40ab 15936
1042e4c0
SS
15937 /* Tracepoint manipulation commands. */
15938
15939 c = add_com ("trace", class_breakpoint, trace_command, _("\
15940Set a tracepoint at specified line or function.\n\
15941\n"
15942BREAK_ARGS_HELP ("trace") "\n\
15943Do \"help tracepoints\" for info on other tracepoint commands."));
15944 set_cmd_completer (c, location_completer);
15945
15946 add_com_alias ("tp", "trace", class_alias, 0);
15947 add_com_alias ("tr", "trace", class_alias, 1);
15948 add_com_alias ("tra", "trace", class_alias, 1);
15949 add_com_alias ("trac", "trace", class_alias, 1);
15950
7a697b8d
SS
15951 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15952Set a fast tracepoint at specified line or function.\n\
15953\n"
15954BREAK_ARGS_HELP ("ftrace") "\n\
15955Do \"help tracepoints\" for info on other tracepoint commands."));
15956 set_cmd_completer (c, location_completer);
15957
0fb4aa4b
PA
15958 c = add_com ("strace", class_breakpoint, strace_command, _("\
15959Set a static tracepoint at specified line, function or marker.\n\
15960\n\
15961strace [LOCATION] [if CONDITION]\n\
15962LOCATION may be a line number, function name, \"*\" and an address,\n\
15963or -m MARKER_ID.\n\
15964If a line number is specified, probe the marker at start of code\n\
15965for that line. If a function is specified, probe the marker at start\n\
15966of code for that function. If an address is specified, probe the marker\n\
15967at that exact address. If a marker id is specified, probe the marker\n\
15968with that name. With no LOCATION, uses current execution address of\n\
15969the selected stack frame.\n\
15970Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15971This collects arbitrary user data passed in the probe point call to the\n\
15972tracing library. You can inspect it when analyzing the trace buffer,\n\
15973by printing the $_sdata variable like any other convenience variable.\n\
15974\n\
15975CONDITION is a boolean expression.\n\
15976\n\
d41c0fc8
PA
15977Multiple tracepoints at one place are permitted, and useful if their\n\
15978conditions are different.\n\
0fb4aa4b
PA
15979\n\
15980Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15981Do \"help tracepoints\" for info on other tracepoint commands."));
15982 set_cmd_completer (c, location_completer);
15983
1042e4c0 15984 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 15985Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15986Convenience variable \"$tpnum\" contains the number of the\n\
15987last tracepoint set."));
15988
15989 add_info_alias ("tp", "tracepoints", 1);
15990
15991 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15992Delete specified tracepoints.\n\
15993Arguments are tracepoint numbers, separated by spaces.\n\
15994No argument means delete all tracepoints."),
15995 &deletelist);
15996
15997 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15998Disable specified tracepoints.\n\
15999Arguments are tracepoint numbers, separated by spaces.\n\
16000No argument means disable all tracepoints."),
16001 &disablelist);
16002 deprecate_cmd (c, "disable");
16003
16004 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16005Enable specified tracepoints.\n\
16006Arguments are tracepoint numbers, separated by spaces.\n\
16007No argument means enable all tracepoints."),
16008 &enablelist);
16009 deprecate_cmd (c, "enable");
16010
16011 add_com ("passcount", class_trace, trace_pass_command, _("\
16012Set the passcount for a tracepoint.\n\
16013The trace will end when the tracepoint has been passed 'count' times.\n\
16014Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16015if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16016
6149aea9
PA
16017 add_prefix_cmd ("save", class_breakpoint, save_command,
16018 _("Save breakpoint definitions as a script."),
16019 &save_cmdlist, "save ",
16020 0/*allow-unknown*/, &cmdlist);
16021
16022 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16023Save current breakpoint definitions as a script.\n\
cce7e648 16024This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16025catchpoints, tracepoints). Use the 'source' command in another debug\n\
16026session to restore them."),
16027 &save_cmdlist);
16028 set_cmd_completer (c, filename_completer);
16029
16030 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16031Save current tracepoint definitions as a script.\n\
6149aea9
PA
16032Use the 'source' command in another debug session to restore them."),
16033 &save_cmdlist);
1042e4c0
SS
16034 set_cmd_completer (c, filename_completer);
16035
6149aea9
PA
16036 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16037 deprecate_cmd (c, "save tracepoints");
16038
1bedd215 16039 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16040Breakpoint specific settings\n\
16041Configure various breakpoint-specific variables such as\n\
1bedd215 16042pending breakpoint behavior"),
fa8d40ab
JJ
16043 &breakpoint_set_cmdlist, "set breakpoint ",
16044 0/*allow-unknown*/, &setlist);
1bedd215 16045 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16046Breakpoint specific settings\n\
16047Configure various breakpoint-specific variables such as\n\
1bedd215 16048pending breakpoint behavior"),
fa8d40ab
JJ
16049 &breakpoint_show_cmdlist, "show breakpoint ",
16050 0/*allow-unknown*/, &showlist);
16051
7915a72c
AC
16052 add_setshow_auto_boolean_cmd ("pending", no_class,
16053 &pending_break_support, _("\
16054Set debugger's behavior regarding pending breakpoints."), _("\
16055Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16056If on, an unrecognized breakpoint location will cause gdb to create a\n\
16057pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16058an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16059user-query to see if a pending breakpoint should be created."),
2c5b56ce 16060 NULL,
920d2a44 16061 show_pending_break_support,
6e1d7d6c
AC
16062 &breakpoint_set_cmdlist,
16063 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16064
16065 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16066
16067 add_setshow_boolean_cmd ("auto-hw", no_class,
16068 &automatic_hardware_breakpoints, _("\
16069Set automatic usage of hardware breakpoints."), _("\
16070Show automatic usage of hardware breakpoints."), _("\
16071If set, the debugger will automatically use hardware breakpoints for\n\
16072breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16073a warning will be emitted for such breakpoints."),
16074 NULL,
16075 show_automatic_hardware_breakpoints,
16076 &breakpoint_set_cmdlist,
16077 &breakpoint_show_cmdlist);
74960c60 16078
33e5cbd6
PA
16079 add_setshow_enum_cmd ("always-inserted", class_support,
16080 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
16081Set mode for inserting breakpoints."), _("\
16082Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16083When this mode is off, breakpoints are inserted in inferior when it is\n\
16084resumed, and removed when execution stops. When this mode is on,\n\
16085breakpoints are inserted immediately and removed only when the user\n\
16086deletes the breakpoint. When this mode is auto (which is the default),\n\
16087the behaviour depends on the non-stop setting (see help set non-stop).\n\
16088In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16089behaves as if always-inserted mode is on; if gdb is controlling the\n\
16090inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
16091 NULL,
16092 &show_always_inserted_mode,
16093 &breakpoint_set_cmdlist,
16094 &breakpoint_show_cmdlist);
f1310107 16095
b775012e
LM
16096 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16097 condition_evaluation_enums,
16098 &condition_evaluation_mode_1, _("\
16099Set mode of breakpoint condition evaluation."), _("\
16100Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16101When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16102evaluated on the host's side by GDB. When it is set to \"target\",\n\
16103breakpoint conditions will be downloaded to the target (if the target\n\
16104supports such feature) and conditions will be evaluated on the target's side.\n\
16105If this is set to \"auto\" (default), this will be automatically set to\n\
16106\"target\" if it supports condition evaluation, otherwise it will\n\
16107be set to \"gdb\""),
16108 &set_condition_evaluation_mode,
16109 &show_condition_evaluation_mode,
16110 &breakpoint_set_cmdlist,
16111 &breakpoint_show_cmdlist);
16112
f1310107
TJB
16113 add_com ("break-range", class_breakpoint, break_range_command, _("\
16114Set a breakpoint for an address range.\n\
16115break-range START-LOCATION, END-LOCATION\n\
16116where START-LOCATION and END-LOCATION can be one of the following:\n\
16117 LINENUM, for that line in the current file,\n\
16118 FILE:LINENUM, for that line in that file,\n\
16119 +OFFSET, for that number of lines after the current line\n\
16120 or the start of the range\n\
16121 FUNCTION, for the first line in that function,\n\
16122 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16123 *ADDRESS, for the instruction at that address.\n\
16124\n\
16125The breakpoint will stop execution of the inferior whenever it executes\n\
16126an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16127range (including START-LOCATION and END-LOCATION)."));
16128
e7e0cddf
SS
16129 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16130Set a dynamic printf at specified line or function.\n\
16131dprintf location,format string,arg1,arg2,...\n\
16132location may be a line number, function name, or \"*\" and an address.\n\
16133If a line number is specified, break at start of code for that line.\n\
16134If a function is specified, break at start of code for that function.\n\
16135"));
16136 set_cmd_completer (c, location_completer);
16137
16138 add_setshow_enum_cmd ("dprintf-style", class_support,
16139 dprintf_style_enums, &dprintf_style, _("\
16140Set the style of usage for dynamic printf."), _("\
16141Show the style of usage for dynamic printf."), _("\
16142This setting chooses how GDB will do a dynamic printf.\n\
16143If the value is \"gdb\", then the printing is done by GDB to its own\n\
16144console, as with the \"printf\" command.\n\
16145If the value is \"call\", the print is done by calling a function in your\n\
16146program; by default printf(), but you can choose a different function or\n\
16147output stream by setting dprintf-function and dprintf-channel."),
16148 update_dprintf_commands, NULL,
16149 &setlist, &showlist);
16150
16151 dprintf_function = xstrdup ("printf");
16152 add_setshow_string_cmd ("dprintf-function", class_support,
16153 &dprintf_function, _("\
16154Set the function to use for dynamic printf"), _("\
16155Show the function to use for dynamic printf"), NULL,
16156 update_dprintf_commands, NULL,
16157 &setlist, &showlist);
16158
16159 dprintf_channel = xstrdup ("");
16160 add_setshow_string_cmd ("dprintf-channel", class_support,
16161 &dprintf_channel, _("\
16162Set the channel to use for dynamic printf"), _("\
16163Show the channel to use for dynamic printf"), NULL,
16164 update_dprintf_commands, NULL,
16165 &setlist, &showlist);
16166
765dc015 16167 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16168
16169 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16170}