Merge from gcc:
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
c906108c 68
1042e4c0
SS
69/* readline include files */
70#include "readline/readline.h"
71#include "readline/history.h"
72
73/* readline defines this. */
74#undef savestring
75
034dad6f 76#include "mi/mi-common.h"
7371cf6d 77#include "python/python.h"
104c1213 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
348d480f
PA
97static void breakpoint_re_set_default (struct breakpoint *);
98
a14ed312 99static void clear_command (char *, int);
c906108c 100
a14ed312 101static void catch_command (char *, int);
c906108c 102
a9634178 103static int can_use_hardware_watchpoint (struct value *);
c906108c 104
98deb0da 105static void break_command_1 (char *, int, int);
c906108c 106
a14ed312 107static void mention (struct breakpoint *);
c906108c 108
348d480f
PA
109static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
110 enum bptype,
c0a91b2b 111 const struct breakpoint_ops *);
4a64f543
MS
112/* This function is used in gdbtk sources and thus can not be made
113 static. */
63c252f8 114struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 115 struct symtab_and_line,
c0a91b2b
TT
116 enum bptype,
117 const struct breakpoint_ops *);
c906108c 118
06edf0c0
PA
119static struct breakpoint *
120 momentary_breakpoint_from_master (struct breakpoint *orig,
121 enum bptype type,
c0a91b2b 122 const struct breakpoint_ops *ops);
06edf0c0 123
76897487
KB
124static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
125
a6d9a66e
UW
126static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
127 CORE_ADDR bpaddr,
88f7da05 128 enum bptype bptype);
76897487 129
6c95b8df
PA
130static void describe_other_breakpoints (struct gdbarch *,
131 struct program_space *, CORE_ADDR,
5af949e3 132 struct obj_section *, int);
c906108c 133
6c95b8df
PA
134static int breakpoint_address_match (struct address_space *aspace1,
135 CORE_ADDR addr1,
136 struct address_space *aspace2,
137 CORE_ADDR addr2);
138
85d721b8
PA
139static int watchpoint_locations_match (struct bp_location *loc1,
140 struct bp_location *loc2);
141
f1310107
TJB
142static int breakpoint_location_address_match (struct bp_location *bl,
143 struct address_space *aspace,
144 CORE_ADDR addr);
145
a14ed312 146static void breakpoints_info (char *, int);
c906108c 147
d77f58be
SS
148static void watchpoints_info (char *, int);
149
e5a67952
MS
150static int breakpoint_1 (char *, int,
151 int (*) (const struct breakpoint *));
c906108c 152
4efb68b1 153static int breakpoint_cond_eval (void *);
c906108c 154
4efb68b1 155static void cleanup_executing_breakpoints (void *);
c906108c 156
a14ed312 157static void commands_command (char *, int);
c906108c 158
a14ed312 159static void condition_command (char *, int);
c906108c 160
c5aa993b
JM
161typedef enum
162 {
163 mark_inserted,
164 mark_uninserted
165 }
166insertion_state_t;
c906108c 167
0bde7532 168static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 169static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 170
e514a9d6 171static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 172
4efb68b1 173static int watchpoint_check (void *);
c906108c 174
a14ed312 175static void maintenance_info_breakpoints (char *, int);
c906108c 176
a14ed312 177static int hw_breakpoint_used_count (void);
c906108c 178
a14ed312 179static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 180
a14ed312 181static void hbreak_command (char *, int);
c906108c 182
a14ed312 183static void thbreak_command (char *, int);
c906108c 184
51be5b68 185static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 186
a14ed312 187static void stop_command (char *arg, int from_tty);
7a292a7a 188
a14ed312 189static void stopin_command (char *arg, int from_tty);
7a292a7a 190
a14ed312 191static void stopat_command (char *arg, int from_tty);
7a292a7a 192
a14ed312 193static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 194
d85310f7
MS
195static void catch_exception_command_1 (enum exception_event_kind ex_event,
196 char *arg, int tempflag, int from_tty);
7a292a7a 197
a14ed312 198static void tcatch_command (char *arg, int from_tty);
7a292a7a 199
d03285ec
UW
200static void detach_single_step_breakpoints (void);
201
6c95b8df
PA
202static int single_step_breakpoint_inserted_here_p (struct address_space *,
203 CORE_ADDR pc);
1aafd4da 204
fe3f5fa8 205static void free_bp_location (struct bp_location *loc);
f431efe5
PA
206static void incref_bp_location (struct bp_location *loc);
207static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 208
39d61571 209static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 210
b60e7edf 211static void update_global_location_list (int);
a5606eee 212
b60e7edf 213static void update_global_location_list_nothrow (int);
74960c60 214
d77f58be 215static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
216
217static void insert_breakpoint_locations (void);
a5606eee 218
a96d9b2e
SDJ
219static int syscall_catchpoint_p (struct breakpoint *b);
220
1042e4c0
SS
221static void tracepoints_info (char *, int);
222
223static void delete_trace_command (char *, int);
224
225static void enable_trace_command (char *, int);
226
227static void disable_trace_command (char *, int);
228
229static void trace_pass_command (char *, int);
230
9c06b0b4
TJB
231static int is_masked_watchpoint (const struct breakpoint *b);
232
0fb4aa4b
PA
233/* Assuming we're creating a static tracepoint, does S look like a
234 static tracepoint marker spec ("-m MARKER_ID")? */
235#define is_marker_spec(s) \
f5a8e22b 236 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 237
2060206e
PA
238/* The abstract base class all breakpoint_ops structures inherit
239 from. */
240static struct breakpoint_ops base_breakpoint_ops;
241
242/* The breakpoint_ops structure to be inherited by all breakpoint_ops
243 that are implemented on top of software or hardware breakpoints
244 (user breakpoints, internal and momentary breakpoints, etc.). */
245static struct breakpoint_ops bkpt_base_breakpoint_ops;
246
247/* Internal breakpoints class type. */
06edf0c0 248static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
249
250/* Momentary breakpoints class type. */
06edf0c0
PA
251static struct breakpoint_ops momentary_breakpoint_ops;
252
2060206e
PA
253/* The breakpoint_ops structure to be used in regular user created
254 breakpoints. */
255struct breakpoint_ops bkpt_breakpoint_ops;
256
5cea2a26
PA
257/* A reference-counted struct command_line. This lets multiple
258 breakpoints share a single command list. */
259struct counted_command_line
260{
261 /* The reference count. */
262 int refc;
263
264 /* The command list. */
265 struct command_line *commands;
266};
267
268struct command_line *
269breakpoint_commands (struct breakpoint *b)
270{
271 return b->commands ? b->commands->commands : NULL;
272}
3daf8fe5 273
f3b1572e
PA
274/* Flag indicating that a command has proceeded the inferior past the
275 current breakpoint. */
276
277static int breakpoint_proceeded;
278
956a9fb9 279const char *
2cec12e5
AR
280bpdisp_text (enum bpdisp disp)
281{
4a64f543
MS
282 /* NOTE: the following values are a part of MI protocol and
283 represent values of 'disp' field returned when inferior stops at
284 a breakpoint. */
bc043ef3 285 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 286
2cec12e5
AR
287 return bpdisps[(int) disp];
288}
c906108c 289
4a64f543 290/* Prototypes for exported functions. */
c906108c 291/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 292 if such is available. */
c906108c
SS
293static int can_use_hw_watchpoints;
294
920d2a44
AC
295static void
296show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
297 struct cmd_list_element *c,
298 const char *value)
299{
3e43a32a
MS
300 fprintf_filtered (file,
301 _("Debugger's willingness to use "
302 "watchpoint hardware is %s.\n"),
920d2a44
AC
303 value);
304}
305
fa8d40ab
JJ
306/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
307 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 308 for unrecognized breakpoint locations.
fa8d40ab
JJ
309 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
310static enum auto_boolean pending_break_support;
920d2a44
AC
311static void
312show_pending_break_support (struct ui_file *file, int from_tty,
313 struct cmd_list_element *c,
314 const char *value)
315{
3e43a32a
MS
316 fprintf_filtered (file,
317 _("Debugger's behavior regarding "
318 "pending breakpoints is %s.\n"),
920d2a44
AC
319 value);
320}
fa8d40ab 321
765dc015 322/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 323 set with "break" but falling in read-only memory.
765dc015
VP
324 If 0, gdb will warn about such breakpoints, but won't automatically
325 use hardware breakpoints. */
326static int automatic_hardware_breakpoints;
327static void
328show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
329 struct cmd_list_element *c,
330 const char *value)
331{
3e43a32a
MS
332 fprintf_filtered (file,
333 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
334 value);
335}
336
33e5cbd6
PA
337/* If on, gdb will keep breakpoints inserted even as inferior is
338 stopped, and immediately insert any new breakpoints. If off, gdb
339 will insert breakpoints into inferior only when resuming it, and
340 will remove breakpoints upon stop. If auto, GDB will behave as ON
341 if in non-stop mode, and as OFF if all-stop mode.*/
342
343static const char always_inserted_auto[] = "auto";
344static const char always_inserted_on[] = "on";
345static const char always_inserted_off[] = "off";
346static const char *always_inserted_enums[] = {
347 always_inserted_auto,
348 always_inserted_off,
349 always_inserted_on,
350 NULL
351};
352static const char *always_inserted_mode = always_inserted_auto;
353static void
74960c60 354show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 355 struct cmd_list_element *c, const char *value)
74960c60 356{
33e5cbd6 357 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Always inserted breakpoint "
360 "mode is %s (currently %s).\n"),
33e5cbd6
PA
361 value,
362 breakpoints_always_inserted_mode () ? "on" : "off");
363 else
3e43a32a
MS
364 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
365 value);
74960c60
VP
366}
367
33e5cbd6
PA
368int
369breakpoints_always_inserted_mode (void)
370{
371 return (always_inserted_mode == always_inserted_on
372 || (always_inserted_mode == always_inserted_auto && non_stop));
373}
765dc015 374
a14ed312 375void _initialize_breakpoint (void);
c906108c 376
c906108c
SS
377/* Are we executing breakpoint commands? */
378static int executing_breakpoint_commands;
379
c02f5703
MS
380/* Are overlay event breakpoints enabled? */
381static int overlay_events_enabled;
382
e09342b5
TJB
383/* See description in breakpoint.h. */
384int target_exact_watchpoints = 0;
385
c906108c 386/* Walk the following statement or block through all breakpoints.
e5dd4106 387 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 388 current breakpoint. */
c906108c 389
5c44784c 390#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 391
5c44784c
JM
392#define ALL_BREAKPOINTS_SAFE(B,TMP) \
393 for (B = breakpoint_chain; \
394 B ? (TMP=B->next, 1): 0; \
395 B = TMP)
c906108c 396
4a64f543
MS
397/* Similar iterator for the low-level breakpoints. SAFE variant is
398 not provided so update_global_location_list must not be called
399 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 400
876fa593
JK
401#define ALL_BP_LOCATIONS(B,BP_TMP) \
402 for (BP_TMP = bp_location; \
403 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
404 BP_TMP++)
7cc221ef 405
1042e4c0
SS
406/* Iterator for tracepoints only. */
407
408#define ALL_TRACEPOINTS(B) \
409 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 410 if (is_tracepoint (B))
1042e4c0 411
7cc221ef 412/* Chains of all breakpoints defined. */
c906108c
SS
413
414struct breakpoint *breakpoint_chain;
415
876fa593
JK
416/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
417
418static struct bp_location **bp_location;
419
420/* Number of elements of BP_LOCATION. */
421
422static unsigned bp_location_count;
423
4a64f543
MS
424/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
425 ADDRESS for the current elements of BP_LOCATION which get a valid
426 result from bp_location_has_shadow. You can use it for roughly
427 limiting the subrange of BP_LOCATION to scan for shadow bytes for
428 an address you need to read. */
876fa593
JK
429
430static CORE_ADDR bp_location_placed_address_before_address_max;
431
4a64f543
MS
432/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
433 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
434 BP_LOCATION which get a valid result from bp_location_has_shadow.
435 You can use it for roughly limiting the subrange of BP_LOCATION to
436 scan for shadow bytes for an address you need to read. */
876fa593
JK
437
438static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 439
4a64f543
MS
440/* The locations that no longer correspond to any breakpoint, unlinked
441 from bp_location array, but for which a hit may still be reported
442 by a target. */
20874c92
VP
443VEC(bp_location_p) *moribund_locations = NULL;
444
c906108c
SS
445/* Number of last breakpoint made. */
446
95a42b64
TT
447static int breakpoint_count;
448
86b17b60
PA
449/* The value of `breakpoint_count' before the last command that
450 created breakpoints. If the last (break-like) command created more
451 than one breakpoint, then the difference between BREAKPOINT_COUNT
452 and PREV_BREAKPOINT_COUNT is more than one. */
453static int prev_breakpoint_count;
c906108c 454
1042e4c0
SS
455/* Number of last tracepoint made. */
456
95a42b64 457static int tracepoint_count;
1042e4c0 458
6149aea9
PA
459static struct cmd_list_element *breakpoint_set_cmdlist;
460static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 461struct cmd_list_element *save_cmdlist;
6149aea9 462
468d015d
JJ
463/* Return whether a breakpoint is an active enabled breakpoint. */
464static int
465breakpoint_enabled (struct breakpoint *b)
466{
0d381245 467 return (b->enable_state == bp_enabled);
468d015d
JJ
468}
469
c906108c
SS
470/* Set breakpoint count to NUM. */
471
95a42b64 472static void
fba45db2 473set_breakpoint_count (int num)
c906108c 474{
86b17b60 475 prev_breakpoint_count = breakpoint_count;
c906108c 476 breakpoint_count = num;
4fa62494 477 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
478}
479
86b17b60
PA
480/* Used by `start_rbreak_breakpoints' below, to record the current
481 breakpoint count before "rbreak" creates any breakpoint. */
482static int rbreak_start_breakpoint_count;
483
95a42b64
TT
484/* Called at the start an "rbreak" command to record the first
485 breakpoint made. */
86b17b60 486
95a42b64
TT
487void
488start_rbreak_breakpoints (void)
489{
86b17b60 490 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
491}
492
493/* Called at the end of an "rbreak" command to record the last
494 breakpoint made. */
86b17b60 495
95a42b64
TT
496void
497end_rbreak_breakpoints (void)
498{
86b17b60 499 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
500}
501
4a64f543 502/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
503
504void
fba45db2 505clear_breakpoint_hit_counts (void)
c906108c
SS
506{
507 struct breakpoint *b;
508
509 ALL_BREAKPOINTS (b)
510 b->hit_count = 0;
511}
512
9add0f1b
TT
513/* Allocate a new counted_command_line with reference count of 1.
514 The new structure owns COMMANDS. */
515
516static struct counted_command_line *
517alloc_counted_command_line (struct command_line *commands)
518{
519 struct counted_command_line *result
520 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 521
9add0f1b
TT
522 result->refc = 1;
523 result->commands = commands;
524 return result;
525}
526
527/* Increment reference count. This does nothing if CMD is NULL. */
528
529static void
530incref_counted_command_line (struct counted_command_line *cmd)
531{
532 if (cmd)
533 ++cmd->refc;
534}
535
536/* Decrement reference count. If the reference count reaches 0,
537 destroy the counted_command_line. Sets *CMDP to NULL. This does
538 nothing if *CMDP is NULL. */
539
540static void
541decref_counted_command_line (struct counted_command_line **cmdp)
542{
543 if (*cmdp)
544 {
545 if (--(*cmdp)->refc == 0)
546 {
547 free_command_lines (&(*cmdp)->commands);
548 xfree (*cmdp);
549 }
550 *cmdp = NULL;
551 }
552}
553
554/* A cleanup function that calls decref_counted_command_line. */
555
556static void
557do_cleanup_counted_command_line (void *arg)
558{
559 decref_counted_command_line (arg);
560}
561
562/* Create a cleanup that calls decref_counted_command_line on the
563 argument. */
564
565static struct cleanup *
566make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
567{
568 return make_cleanup (do_cleanup_counted_command_line, cmdp);
569}
570
c906108c
SS
571/* Default address, symtab and line to put a breakpoint at
572 for "break" command with no arg.
4a64f543 573 If default_breakpoint_valid is zero, the other three are
c906108c
SS
574 not valid, and "break" with no arg is an error.
575
576 This set by print_stack_frame, which calls set_default_breakpoint. */
577
578int default_breakpoint_valid;
579CORE_ADDR default_breakpoint_address;
580struct symtab *default_breakpoint_symtab;
581int default_breakpoint_line;
6c95b8df
PA
582struct program_space *default_breakpoint_pspace;
583
c906108c 584\f
48cb2d85
VP
585/* Return the breakpoint with the specified number, or NULL
586 if the number does not refer to an existing breakpoint. */
587
588struct breakpoint *
589get_breakpoint (int num)
590{
591 struct breakpoint *b;
592
593 ALL_BREAKPOINTS (b)
594 if (b->number == num)
595 return b;
596
597 return NULL;
598}
5c44784c 599
c906108c 600\f
adc36818
PM
601
602void
603set_breakpoint_condition (struct breakpoint *b, char *exp,
604 int from_tty)
605{
3a5c3e22
PA
606 xfree (b->cond_string);
607 b->cond_string = NULL;
adc36818 608
3a5c3e22 609 if (is_watchpoint (b))
adc36818 610 {
3a5c3e22
PA
611 struct watchpoint *w = (struct watchpoint *) b;
612
613 xfree (w->cond_exp);
614 w->cond_exp = NULL;
615 }
616 else
617 {
618 struct bp_location *loc;
619
620 for (loc = b->loc; loc; loc = loc->next)
621 {
622 xfree (loc->cond);
623 loc->cond = NULL;
624 }
adc36818 625 }
adc36818
PM
626
627 if (*exp == 0)
628 {
629 if (from_tty)
630 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
631 }
632 else
633 {
634 char *arg = exp;
cc59ec59 635
adc36818
PM
636 /* I don't know if it matters whether this is the string the user
637 typed in or the decompiled expression. */
638 b->cond_string = xstrdup (arg);
639 b->condition_not_parsed = 0;
640
641 if (is_watchpoint (b))
642 {
3a5c3e22
PA
643 struct watchpoint *w = (struct watchpoint *) b;
644
adc36818
PM
645 innermost_block = NULL;
646 arg = exp;
3a5c3e22 647 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
648 if (*arg)
649 error (_("Junk at end of expression"));
3a5c3e22 650 w->cond_exp_valid_block = innermost_block;
adc36818
PM
651 }
652 else
653 {
3a5c3e22
PA
654 struct bp_location *loc;
655
adc36818
PM
656 for (loc = b->loc; loc; loc = loc->next)
657 {
658 arg = exp;
659 loc->cond =
660 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
661 if (*arg)
662 error (_("Junk at end of expression"));
663 }
664 }
665 }
666 breakpoints_changed ();
8d3788bd 667 observer_notify_breakpoint_modified (b);
adc36818
PM
668}
669
c906108c
SS
670/* condition N EXP -- set break condition of breakpoint N to EXP. */
671
672static void
fba45db2 673condition_command (char *arg, int from_tty)
c906108c 674{
52f0bd74 675 struct breakpoint *b;
c906108c 676 char *p;
52f0bd74 677 int bnum;
c906108c
SS
678
679 if (arg == 0)
e2e0b3e5 680 error_no_arg (_("breakpoint number"));
c906108c
SS
681
682 p = arg;
683 bnum = get_number (&p);
5c44784c 684 if (bnum == 0)
8a3fe4f8 685 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
686
687 ALL_BREAKPOINTS (b)
688 if (b->number == bnum)
2f069f6f 689 {
7371cf6d
PM
690 /* Check if this breakpoint has a Python object assigned to
691 it, and if it has a definition of the "stop"
692 method. This method and conditions entered into GDB from
693 the CLI are mutually exclusive. */
694 if (b->py_bp_object
695 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
696 error (_("Cannot set a condition where a Python 'stop' "
697 "method has been defined in the breakpoint."));
2566ad2d 698 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
699 return;
700 }
c906108c 701
8a3fe4f8 702 error (_("No breakpoint number %d."), bnum);
c906108c
SS
703}
704
a7bdde9e
VP
705/* Check that COMMAND do not contain commands that are suitable
706 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
707 Throw if any such commands is found. */
708
a7bdde9e
VP
709static void
710check_no_tracepoint_commands (struct command_line *commands)
711{
712 struct command_line *c;
cc59ec59 713
a7bdde9e
VP
714 for (c = commands; c; c = c->next)
715 {
716 int i;
717
718 if (c->control_type == while_stepping_control)
3e43a32a
MS
719 error (_("The 'while-stepping' command can "
720 "only be used for tracepoints"));
a7bdde9e
VP
721
722 for (i = 0; i < c->body_count; ++i)
723 check_no_tracepoint_commands ((c->body_list)[i]);
724
725 /* Not that command parsing removes leading whitespace and comment
4a64f543 726 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
727 command directly. */
728 if (strstr (c->line, "collect ") == c->line)
729 error (_("The 'collect' command can only be used for tracepoints"));
730
51661e93
VP
731 if (strstr (c->line, "teval ") == c->line)
732 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
733 }
734}
735
d77f58be
SS
736/* Encapsulate tests for different types of tracepoints. */
737
d9b3f62e
PA
738static int
739is_tracepoint_type (enum bptype type)
740{
741 return (type == bp_tracepoint
742 || type == bp_fast_tracepoint
743 || type == bp_static_tracepoint);
744}
745
a7bdde9e 746int
d77f58be 747is_tracepoint (const struct breakpoint *b)
a7bdde9e 748{
d9b3f62e 749 return is_tracepoint_type (b->type);
a7bdde9e 750}
d9b3f62e 751
e5dd4106 752/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
753 breakpoint. This function will throw an exception if a problem is
754 found. */
48cb2d85 755
95a42b64
TT
756static void
757validate_commands_for_breakpoint (struct breakpoint *b,
758 struct command_line *commands)
48cb2d85 759{
d77f58be 760 if (is_tracepoint (b))
a7bdde9e 761 {
4a64f543
MS
762 /* We need to verify that each top-level element of commands is
763 valid for tracepoints, that there's at most one
764 while-stepping element, and that while-stepping's body has
765 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
766 struct command_line *c;
767 struct command_line *while_stepping = 0;
768 for (c = commands; c; c = c->next)
769 {
a7bdde9e
VP
770 if (c->control_type == while_stepping_control)
771 {
772 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
773 error (_("The 'while-stepping' command "
774 "cannot be used for fast tracepoint"));
0fb4aa4b 775 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
776 error (_("The 'while-stepping' command "
777 "cannot be used for static tracepoint"));
a7bdde9e
VP
778
779 if (while_stepping)
3e43a32a
MS
780 error (_("The 'while-stepping' command "
781 "can be used only once"));
a7bdde9e
VP
782 else
783 while_stepping = c;
784 }
785 }
786 if (while_stepping)
787 {
788 struct command_line *c2;
789
790 gdb_assert (while_stepping->body_count == 1);
791 c2 = while_stepping->body_list[0];
792 for (; c2; c2 = c2->next)
793 {
a7bdde9e
VP
794 if (c2->control_type == while_stepping_control)
795 error (_("The 'while-stepping' command cannot be nested"));
796 }
797 }
798 }
799 else
800 {
801 check_no_tracepoint_commands (commands);
802 }
95a42b64
TT
803}
804
0fb4aa4b
PA
805/* Return a vector of all the static tracepoints set at ADDR. The
806 caller is responsible for releasing the vector. */
807
808VEC(breakpoint_p) *
809static_tracepoints_here (CORE_ADDR addr)
810{
811 struct breakpoint *b;
812 VEC(breakpoint_p) *found = 0;
813 struct bp_location *loc;
814
815 ALL_BREAKPOINTS (b)
816 if (b->type == bp_static_tracepoint)
817 {
818 for (loc = b->loc; loc; loc = loc->next)
819 if (loc->address == addr)
820 VEC_safe_push(breakpoint_p, found, b);
821 }
822
823 return found;
824}
825
95a42b64 826/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 827 validate that only allowed commands are included. */
95a42b64
TT
828
829void
4a64f543
MS
830breakpoint_set_commands (struct breakpoint *b,
831 struct command_line *commands)
95a42b64
TT
832{
833 validate_commands_for_breakpoint (b, commands);
a7bdde9e 834
9add0f1b
TT
835 decref_counted_command_line (&b->commands);
836 b->commands = alloc_counted_command_line (commands);
48cb2d85 837 breakpoints_changed ();
8d3788bd 838 observer_notify_breakpoint_modified (b);
48cb2d85
VP
839}
840
45a43567
TT
841/* Set the internal `silent' flag on the breakpoint. Note that this
842 is not the same as the "silent" that may appear in the breakpoint's
843 commands. */
844
845void
846breakpoint_set_silent (struct breakpoint *b, int silent)
847{
848 int old_silent = b->silent;
849
850 b->silent = silent;
851 if (old_silent != silent)
8d3788bd 852 observer_notify_breakpoint_modified (b);
45a43567
TT
853}
854
855/* Set the thread for this breakpoint. If THREAD is -1, make the
856 breakpoint work for any thread. */
857
858void
859breakpoint_set_thread (struct breakpoint *b, int thread)
860{
861 int old_thread = b->thread;
862
863 b->thread = thread;
864 if (old_thread != thread)
8d3788bd 865 observer_notify_breakpoint_modified (b);
45a43567
TT
866}
867
868/* Set the task for this breakpoint. If TASK is 0, make the
869 breakpoint work for any task. */
870
871void
872breakpoint_set_task (struct breakpoint *b, int task)
873{
874 int old_task = b->task;
875
876 b->task = task;
877 if (old_task != task)
8d3788bd 878 observer_notify_breakpoint_modified (b);
45a43567
TT
879}
880
95a42b64
TT
881void
882check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
883{
884 struct breakpoint *b = closure;
cc59ec59 885
a7bdde9e
VP
886 validate_actionline (&line, b);
887}
888
95a42b64
TT
889/* A structure used to pass information through
890 map_breakpoint_numbers. */
891
892struct commands_info
893{
894 /* True if the command was typed at a tty. */
895 int from_tty;
86b17b60
PA
896
897 /* The breakpoint range spec. */
898 char *arg;
899
95a42b64
TT
900 /* Non-NULL if the body of the commands are being read from this
901 already-parsed command. */
902 struct command_line *control;
86b17b60 903
95a42b64
TT
904 /* The command lines read from the user, or NULL if they have not
905 yet been read. */
906 struct counted_command_line *cmd;
907};
908
909/* A callback for map_breakpoint_numbers that sets the commands for
910 commands_command. */
911
c906108c 912static void
95a42b64 913do_map_commands_command (struct breakpoint *b, void *data)
c906108c 914{
95a42b64 915 struct commands_info *info = data;
c906108c 916
95a42b64
TT
917 if (info->cmd == NULL)
918 {
919 struct command_line *l;
5c44784c 920
95a42b64
TT
921 if (info->control != NULL)
922 l = copy_command_lines (info->control->body_list[0]);
923 else
86b17b60
PA
924 {
925 struct cleanup *old_chain;
926 char *str;
c5aa993b 927
3e43a32a
MS
928 str = xstrprintf (_("Type commands for breakpoint(s) "
929 "%s, one per line."),
86b17b60
PA
930 info->arg);
931
932 old_chain = make_cleanup (xfree, str);
933
934 l = read_command_lines (str,
935 info->from_tty, 1,
d77f58be 936 (is_tracepoint (b)
86b17b60
PA
937 ? check_tracepoint_command : 0),
938 b);
939
940 do_cleanups (old_chain);
941 }
a7bdde9e 942
95a42b64
TT
943 info->cmd = alloc_counted_command_line (l);
944 }
945
946 /* If a breakpoint was on the list more than once, we don't need to
947 do anything. */
948 if (b->commands != info->cmd)
949 {
950 validate_commands_for_breakpoint (b, info->cmd->commands);
951 incref_counted_command_line (info->cmd);
952 decref_counted_command_line (&b->commands);
953 b->commands = info->cmd;
954 breakpoints_changed ();
8d3788bd 955 observer_notify_breakpoint_modified (b);
c5aa993b 956 }
95a42b64
TT
957}
958
959static void
4a64f543
MS
960commands_command_1 (char *arg, int from_tty,
961 struct command_line *control)
95a42b64
TT
962{
963 struct cleanup *cleanups;
964 struct commands_info info;
965
966 info.from_tty = from_tty;
967 info.control = control;
968 info.cmd = NULL;
969 /* If we read command lines from the user, then `info' will hold an
970 extra reference to the commands that we must clean up. */
971 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
972
973 if (arg == NULL || !*arg)
974 {
86b17b60 975 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
976 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
977 breakpoint_count);
95a42b64
TT
978 else if (breakpoint_count > 0)
979 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
980 else
981 {
982 /* So that we don't try to free the incoming non-NULL
983 argument in the cleanup below. Mapping breakpoint
984 numbers will fail in this case. */
985 arg = NULL;
986 }
95a42b64 987 }
9766ced4
SS
988 else
989 /* The command loop has some static state, so we need to preserve
990 our argument. */
991 arg = xstrdup (arg);
86b17b60
PA
992
993 if (arg != NULL)
994 make_cleanup (xfree, arg);
995
996 info.arg = arg;
95a42b64
TT
997
998 map_breakpoint_numbers (arg, do_map_commands_command, &info);
999
1000 if (info.cmd == NULL)
1001 error (_("No breakpoints specified."));
1002
1003 do_cleanups (cleanups);
1004}
1005
1006static void
1007commands_command (char *arg, int from_tty)
1008{
1009 commands_command_1 (arg, from_tty, NULL);
c906108c 1010}
40c03ae8
EZ
1011
1012/* Like commands_command, but instead of reading the commands from
1013 input stream, takes them from an already parsed command structure.
1014
1015 This is used by cli-script.c to DTRT with breakpoint commands
1016 that are part of if and while bodies. */
1017enum command_control_type
1018commands_from_control_command (char *arg, struct command_line *cmd)
1019{
95a42b64
TT
1020 commands_command_1 (arg, 0, cmd);
1021 return simple_control;
40c03ae8 1022}
876fa593
JK
1023
1024/* Return non-zero if BL->TARGET_INFO contains valid information. */
1025
1026static int
1027bp_location_has_shadow (struct bp_location *bl)
1028{
1029 if (bl->loc_type != bp_loc_software_breakpoint)
1030 return 0;
1031 if (!bl->inserted)
1032 return 0;
1033 if (bl->target_info.shadow_len == 0)
e5dd4106 1034 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1035 return 0;
1036 return 1;
1037}
1038
8defab1a 1039/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1040 by replacing any memory breakpoints with their shadowed contents.
1041
1042 The range of shadowed area by each bp_location is:
35df4500
TJB
1043 bl->address - bp_location_placed_address_before_address_max
1044 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1045 The range we were requested to resolve shadows for is:
1046 memaddr ... memaddr + len
1047 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1048 memaddr + len <= (bl->address
1049 - bp_location_placed_address_before_address_max)
876fa593 1050 and:
35df4500 1051 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1052
8defab1a
DJ
1053void
1054breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1055{
4a64f543
MS
1056 /* Left boundary, right boundary and median element of our binary
1057 search. */
876fa593
JK
1058 unsigned bc_l, bc_r, bc;
1059
4a64f543
MS
1060 /* Find BC_L which is a leftmost element which may affect BUF
1061 content. It is safe to report lower value but a failure to
1062 report higher one. */
876fa593
JK
1063
1064 bc_l = 0;
1065 bc_r = bp_location_count;
1066 while (bc_l + 1 < bc_r)
1067 {
35df4500 1068 struct bp_location *bl;
876fa593
JK
1069
1070 bc = (bc_l + bc_r) / 2;
35df4500 1071 bl = bp_location[bc];
876fa593 1072
4a64f543
MS
1073 /* Check first BL->ADDRESS will not overflow due to the added
1074 constant. Then advance the left boundary only if we are sure
1075 the BC element can in no way affect the BUF content (MEMADDR
1076 to MEMADDR + LEN range).
876fa593 1077
4a64f543
MS
1078 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1079 offset so that we cannot miss a breakpoint with its shadow
1080 range tail still reaching MEMADDR. */
c5aa993b 1081
35df4500
TJB
1082 if ((bl->address + bp_location_shadow_len_after_address_max
1083 >= bl->address)
1084 && (bl->address + bp_location_shadow_len_after_address_max
1085 <= memaddr))
876fa593
JK
1086 bc_l = bc;
1087 else
1088 bc_r = bc;
1089 }
1090
128070bb
PA
1091 /* Due to the binary search above, we need to make sure we pick the
1092 first location that's at BC_L's address. E.g., if there are
1093 multiple locations at the same address, BC_L may end up pointing
1094 at a duplicate location, and miss the "master"/"inserted"
1095 location. Say, given locations L1, L2 and L3 at addresses A and
1096 B:
1097
1098 L1@A, L2@A, L3@B, ...
1099
1100 BC_L could end up pointing at location L2, while the "master"
1101 location could be L1. Since the `loc->inserted' flag is only set
1102 on "master" locations, we'd forget to restore the shadow of L1
1103 and L2. */
1104 while (bc_l > 0
1105 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1106 bc_l--;
1107
876fa593
JK
1108 /* Now do full processing of the found relevant range of elements. */
1109
1110 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1111 {
35df4500 1112 struct bp_location *bl = bp_location[bc];
876fa593
JK
1113 CORE_ADDR bp_addr = 0;
1114 int bp_size = 0;
1115 int bptoffset = 0;
1116
35df4500
TJB
1117 /* bp_location array has BL->OWNER always non-NULL. */
1118 if (bl->owner->type == bp_none)
8a3fe4f8 1119 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1120 bl->owner->number);
ffce0d52 1121
e5dd4106 1122 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1123 content. */
1124
35df4500
TJB
1125 if (bl->address >= bp_location_placed_address_before_address_max
1126 && memaddr + len <= (bl->address
1127 - bp_location_placed_address_before_address_max))
876fa593
JK
1128 break;
1129
35df4500 1130 if (!bp_location_has_shadow (bl))
c5aa993b 1131 continue;
35df4500 1132 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1133 current_program_space->aspace, 0))
1134 continue;
1135
c5aa993b
JM
1136 /* Addresses and length of the part of the breakpoint that
1137 we need to copy. */
35df4500
TJB
1138 bp_addr = bl->target_info.placed_address;
1139 bp_size = bl->target_info.shadow_len;
8defab1a 1140
c5aa993b
JM
1141 if (bp_addr + bp_size <= memaddr)
1142 /* The breakpoint is entirely before the chunk of memory we
1143 are reading. */
1144 continue;
8defab1a 1145
c5aa993b
JM
1146 if (bp_addr >= memaddr + len)
1147 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1148 reading. */
c5aa993b 1149 continue;
c5aa993b 1150
8defab1a
DJ
1151 /* Offset within shadow_contents. */
1152 if (bp_addr < memaddr)
1153 {
1154 /* Only copy the second part of the breakpoint. */
1155 bp_size -= memaddr - bp_addr;
1156 bptoffset = memaddr - bp_addr;
1157 bp_addr = memaddr;
1158 }
c5aa993b 1159
8defab1a
DJ
1160 if (bp_addr + bp_size > memaddr + len)
1161 {
1162 /* Only copy the first part of the breakpoint. */
1163 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1164 }
c5aa993b 1165
8defab1a 1166 memcpy (buf + bp_addr - memaddr,
35df4500 1167 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1168 }
c906108c 1169}
c906108c 1170\f
c5aa993b 1171
60e1c644
PA
1172/* Return true if BPT is of any hardware watchpoint kind. */
1173
a5606eee 1174static int
d77f58be 1175is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1176{
1177 return (bpt->type == bp_hardware_watchpoint
1178 || bpt->type == bp_read_watchpoint
1179 || bpt->type == bp_access_watchpoint);
1180}
7270d8f2 1181
60e1c644
PA
1182/* Return true if BPT is of any watchpoint kind, hardware or
1183 software. */
1184
3a5c3e22 1185int
d77f58be 1186is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1187{
1188 return (is_hardware_watchpoint (bpt)
1189 || bpt->type == bp_watchpoint);
1190}
1191
3a5c3e22
PA
1192/* Returns true if the current thread and its running state are safe
1193 to evaluate or update watchpoint B. Watchpoints on local
1194 expressions need to be evaluated in the context of the thread that
1195 was current when the watchpoint was created, and, that thread needs
1196 to be stopped to be able to select the correct frame context.
1197 Watchpoints on global expressions can be evaluated on any thread,
1198 and in any state. It is presently left to the target allowing
1199 memory accesses when threads are running. */
f6bc2008
PA
1200
1201static int
3a5c3e22 1202watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1203{
1204 return (ptid_equal (b->watchpoint_thread, null_ptid)
1205 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1206 && !is_executing (inferior_ptid)));
1207}
1208
d0fb5eae
JK
1209/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1210 associated bp_watchpoint_scope breakpoint. */
1211
1212static void
3a5c3e22 1213watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1214{
3a5c3e22 1215 struct breakpoint *b = &w->base;
d0fb5eae
JK
1216
1217 if (b->related_breakpoint != b)
1218 {
1219 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1220 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1221 b->related_breakpoint->disposition = disp_del_at_next_stop;
1222 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1223 b->related_breakpoint = b;
1224 }
1225 b->disposition = disp_del_at_next_stop;
1226}
1227
567e1b4e
JB
1228/* Assuming that B is a watchpoint:
1229 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1230 - Evaluate expression and store the result in B->val
567e1b4e
JB
1231 - Evaluate the condition if there is one, and store the result
1232 in b->loc->cond.
a5606eee
VP
1233 - Update the list of values that must be watched in B->loc.
1234
4a64f543
MS
1235 If the watchpoint disposition is disp_del_at_next_stop, then do
1236 nothing. If this is local watchpoint that is out of scope, delete
1237 it.
1238
1239 Even with `set breakpoint always-inserted on' the watchpoints are
1240 removed + inserted on each stop here. Normal breakpoints must
1241 never be removed because they might be missed by a running thread
1242 when debugging in non-stop mode. On the other hand, hardware
1243 watchpoints (is_hardware_watchpoint; processed here) are specific
1244 to each LWP since they are stored in each LWP's hardware debug
1245 registers. Therefore, such LWP must be stopped first in order to
1246 be able to modify its hardware watchpoints.
1247
1248 Hardware watchpoints must be reset exactly once after being
1249 presented to the user. It cannot be done sooner, because it would
1250 reset the data used to present the watchpoint hit to the user. And
1251 it must not be done later because it could display the same single
1252 watchpoint hit during multiple GDB stops. Note that the latter is
1253 relevant only to the hardware watchpoint types bp_read_watchpoint
1254 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1255 not user-visible - its hit is suppressed if the memory content has
1256 not changed.
1257
1258 The following constraints influence the location where we can reset
1259 hardware watchpoints:
1260
1261 * target_stopped_by_watchpoint and target_stopped_data_address are
1262 called several times when GDB stops.
1263
1264 [linux]
1265 * Multiple hardware watchpoints can be hit at the same time,
1266 causing GDB to stop. GDB only presents one hardware watchpoint
1267 hit at a time as the reason for stopping, and all the other hits
1268 are presented later, one after the other, each time the user
1269 requests the execution to be resumed. Execution is not resumed
1270 for the threads still having pending hit event stored in
1271 LWP_INFO->STATUS. While the watchpoint is already removed from
1272 the inferior on the first stop the thread hit event is kept being
1273 reported from its cached value by linux_nat_stopped_data_address
1274 until the real thread resume happens after the watchpoint gets
1275 presented and thus its LWP_INFO->STATUS gets reset.
1276
1277 Therefore the hardware watchpoint hit can get safely reset on the
1278 watchpoint removal from inferior. */
a79d3c27 1279
b40ce68a 1280static void
3a5c3e22 1281update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1282{
a5606eee 1283 int within_current_scope;
a5606eee 1284 struct frame_id saved_frame_id;
66076460 1285 int frame_saved;
a5606eee 1286
f6bc2008
PA
1287 /* If this is a local watchpoint, we only want to check if the
1288 watchpoint frame is in scope if the current thread is the thread
1289 that was used to create the watchpoint. */
1290 if (!watchpoint_in_thread_scope (b))
1291 return;
1292
3a5c3e22 1293 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1294 return;
1295
66076460 1296 frame_saved = 0;
a5606eee
VP
1297
1298 /* Determine if the watchpoint is within scope. */
1299 if (b->exp_valid_block == NULL)
1300 within_current_scope = 1;
1301 else
1302 {
b5db5dfc
UW
1303 struct frame_info *fi = get_current_frame ();
1304 struct gdbarch *frame_arch = get_frame_arch (fi);
1305 CORE_ADDR frame_pc = get_frame_pc (fi);
1306
1307 /* If we're in a function epilogue, unwinding may not work
1308 properly, so do not attempt to recreate locations at this
1309 point. See similar comments in watchpoint_check. */
1310 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1311 return;
66076460
DJ
1312
1313 /* Save the current frame's ID so we can restore it after
1314 evaluating the watchpoint expression on its own frame. */
1315 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1316 took a frame parameter, so that we didn't have to change the
1317 selected frame. */
1318 frame_saved = 1;
1319 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1320
a5606eee
VP
1321 fi = frame_find_by_id (b->watchpoint_frame);
1322 within_current_scope = (fi != NULL);
1323 if (within_current_scope)
1324 select_frame (fi);
1325 }
1326
b5db5dfc
UW
1327 /* We don't free locations. They are stored in the bp_location array
1328 and update_global_location_list will eventually delete them and
1329 remove breakpoints if needed. */
3a5c3e22 1330 b->base.loc = NULL;
b5db5dfc 1331
a5606eee
VP
1332 if (within_current_scope && reparse)
1333 {
1334 char *s;
d63d0675 1335
a5606eee
VP
1336 if (b->exp)
1337 {
1338 xfree (b->exp);
1339 b->exp = NULL;
1340 }
d63d0675 1341 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1342 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1343 /* If the meaning of expression itself changed, the old value is
1344 no longer relevant. We don't want to report a watchpoint hit
1345 to the user when the old value and the new value may actually
1346 be completely different objects. */
1347 value_free (b->val);
fa4727a6
DJ
1348 b->val = NULL;
1349 b->val_valid = 0;
60e1c644
PA
1350
1351 /* Note that unlike with breakpoints, the watchpoint's condition
1352 expression is stored in the breakpoint object, not in the
1353 locations (re)created below. */
3a5c3e22 1354 if (b->base.cond_string != NULL)
60e1c644
PA
1355 {
1356 if (b->cond_exp != NULL)
1357 {
1358 xfree (b->cond_exp);
1359 b->cond_exp = NULL;
1360 }
1361
3a5c3e22 1362 s = b->base.cond_string;
60e1c644
PA
1363 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1364 }
a5606eee 1365 }
a5606eee
VP
1366
1367 /* If we failed to parse the expression, for example because
1368 it refers to a global variable in a not-yet-loaded shared library,
1369 don't try to insert watchpoint. We don't automatically delete
1370 such watchpoint, though, since failure to parse expression
1371 is different from out-of-scope watchpoint. */
2d134ed3
PA
1372 if ( !target_has_execution)
1373 {
1374 /* Without execution, memory can't change. No use to try and
1375 set watchpoint locations. The watchpoint will be reset when
1376 the target gains execution, through breakpoint_re_set. */
1377 }
1378 else if (within_current_scope && b->exp)
a5606eee 1379 {
0cf6dd15 1380 int pc = 0;
fa4727a6 1381 struct value *val_chain, *v, *result, *next;
2d134ed3 1382 struct program_space *frame_pspace;
a5606eee 1383
0cf6dd15 1384 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1385
a5606eee
VP
1386 /* Avoid setting b->val if it's already set. The meaning of
1387 b->val is 'the last value' user saw, and we should update
1388 it only if we reported that last value to user. As it
9c06b0b4
TJB
1389 happens, the code that reports it updates b->val directly.
1390 We don't keep track of the memory value for masked
1391 watchpoints. */
3a5c3e22 1392 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1393 {
1394 b->val = v;
1395 b->val_valid = 1;
1396 }
a5606eee 1397
2d134ed3
PA
1398 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1399
a5606eee 1400 /* Look at each value on the value chain. */
9fa40276 1401 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1402 {
1403 /* If it's a memory location, and GDB actually needed
1404 its contents to evaluate the expression, then we
fa4727a6
DJ
1405 must watch it. If the first value returned is
1406 still lazy, that means an error occurred reading it;
1407 watch it anyway in case it becomes readable. */
a5606eee 1408 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1409 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1410 {
1411 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1412
a5606eee
VP
1413 /* We only watch structs and arrays if user asked
1414 for it explicitly, never if they just happen to
1415 appear in the middle of some value chain. */
fa4727a6 1416 if (v == result
a5606eee
VP
1417 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1418 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1419 {
1420 CORE_ADDR addr;
1421 int len, type;
1422 struct bp_location *loc, **tmp;
1423
42ae5230 1424 addr = value_address (v);
a5606eee
VP
1425 len = TYPE_LENGTH (value_type (v));
1426 type = hw_write;
3a5c3e22 1427 if (b->base.type == bp_read_watchpoint)
a5606eee 1428 type = hw_read;
3a5c3e22 1429 else if (b->base.type == bp_access_watchpoint)
a5606eee 1430 type = hw_access;
3a5c3e22
PA
1431
1432 loc = allocate_bp_location (&b->base);
1433 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1434 ;
1435 *tmp = loc;
a6d9a66e 1436 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1437
1438 loc->pspace = frame_pspace;
a5606eee
VP
1439 loc->address = addr;
1440 loc->length = len;
1441 loc->watchpoint_type = type;
1442 }
1443 }
9fa40276
TJB
1444 }
1445
1446 /* Change the type of breakpoint between hardware assisted or
1447 an ordinary watchpoint depending on the hardware support
1448 and free hardware slots. REPARSE is set when the inferior
1449 is started. */
a9634178 1450 if (reparse)
9fa40276 1451 {
e09342b5 1452 int reg_cnt;
9fa40276
TJB
1453 enum bp_loc_type loc_type;
1454 struct bp_location *bl;
a5606eee 1455
a9634178 1456 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1457
1458 if (reg_cnt)
9fa40276
TJB
1459 {
1460 int i, target_resources_ok, other_type_used;
1461
a9634178
TJB
1462 /* Use an exact watchpoint when there's only one memory region to be
1463 watched, and only one debug register is needed to watch it. */
1464 b->exact = target_exact_watchpoints && reg_cnt == 1;
1465
9fa40276 1466 /* We need to determine how many resources are already
e09342b5
TJB
1467 used for all other hardware watchpoints plus this one
1468 to see if we still have enough resources to also fit
1469 this watchpoint in as well. To guarantee the
1470 hw_watchpoint_used_count call below counts this
1471 watchpoint, make sure that it is marked as a hardware
1472 watchpoint. */
3a5c3e22
PA
1473 if (b->base.type == bp_watchpoint)
1474 b->base.type = bp_hardware_watchpoint;
9fa40276 1475
3a5c3e22 1476 i = hw_watchpoint_used_count (b->base.type, &other_type_used);
e09342b5 1477 target_resources_ok = target_can_use_hardware_watchpoint
3a5c3e22 1478 (b->base.type, i, other_type_used);
e09342b5 1479 if (target_resources_ok <= 0)
a9634178 1480 {
3a5c3e22 1481 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1482
1483 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1484 error (_("Target does not support this type of "
1485 "hardware watchpoint."));
9c06b0b4
TJB
1486 else if (target_resources_ok < 0 && !sw_mode)
1487 error (_("There are not enough available hardware "
1488 "resources for this watchpoint."));
a9634178 1489 else
3a5c3e22 1490 b->base.type = bp_watchpoint;
a9634178 1491 }
9fa40276 1492 }
3a5c3e22 1493 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1494 error (_("Expression cannot be implemented with "
1495 "read/access watchpoint."));
9fa40276 1496 else
3a5c3e22 1497 b->base.type = bp_watchpoint;
9fa40276 1498
3a5c3e22 1499 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1500 : bp_loc_hardware_watchpoint);
3a5c3e22 1501 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1502 bl->loc_type = loc_type;
1503 }
1504
1505 for (v = val_chain; v; v = next)
1506 {
a5606eee
VP
1507 next = value_next (v);
1508 if (v != b->val)
1509 value_free (v);
1510 }
1511
c7437ca6
PA
1512 /* If a software watchpoint is not watching any memory, then the
1513 above left it without any location set up. But,
1514 bpstat_stop_status requires a location to be able to report
1515 stops, so make sure there's at least a dummy one. */
3a5c3e22 1516 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1517 {
3a5c3e22
PA
1518 struct breakpoint *base = &b->base;
1519 base->loc = allocate_bp_location (base);
1520 base->loc->pspace = frame_pspace;
1521 base->loc->address = -1;
1522 base->loc->length = -1;
1523 base->loc->watchpoint_type = -1;
c7437ca6 1524 }
a5606eee
VP
1525 }
1526 else if (!within_current_scope)
7270d8f2 1527 {
ac74f770
MS
1528 printf_filtered (_("\
1529Watchpoint %d deleted because the program has left the block\n\
1530in which its expression is valid.\n"),
3a5c3e22 1531 b->base.number);
d0fb5eae 1532 watchpoint_del_at_next_stop (b);
7270d8f2 1533 }
a5606eee
VP
1534
1535 /* Restore the selected frame. */
66076460
DJ
1536 if (frame_saved)
1537 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1538}
1539
a5606eee 1540
74960c60
VP
1541/* Returns 1 iff breakpoint location should be
1542 inserted in the inferior. */
1543static int
35df4500 1544should_be_inserted (struct bp_location *bl)
74960c60 1545{
35df4500 1546 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1547 return 0;
1548
35df4500 1549 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1550 return 0;
1551
35df4500 1552 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1553 return 0;
1554
56710373
PA
1555 /* This is set for example, when we're attached to the parent of a
1556 vfork, and have detached from the child. The child is running
1557 free, and we expect it to do an exec or exit, at which point the
1558 OS makes the parent schedulable again (and the target reports
1559 that the vfork is done). Until the child is done with the shared
1560 memory region, do not insert breakpoints in the parent, otherwise
1561 the child could still trip on the parent's breakpoints. Since
1562 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1563 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1564 return 0;
1565
1042e4c0
SS
1566 /* Tracepoints are inserted by the target at a time of its choosing,
1567 not by us. */
35df4500 1568 if (is_tracepoint (bl->owner))
1042e4c0
SS
1569 return 0;
1570
74960c60
VP
1571 return 1;
1572}
1573
35df4500
TJB
1574/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1575 location. Any error messages are printed to TMP_ERROR_STREAM; and
1576 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1577
4a64f543
MS
1578 NOTE drow/2003-09-09: This routine could be broken down to an
1579 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1580static int
35df4500 1581insert_bp_location (struct bp_location *bl,
26bb91f3 1582 struct ui_file *tmp_error_stream,
fa3a767f 1583 int *disabled_breaks,
26bb91f3 1584 int *hw_breakpoint_error)
879bfdc2
DJ
1585{
1586 int val = 0;
1587
35df4500 1588 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1589 return 0;
1590
8181d85f 1591 /* Initialize the target-specific information. */
35df4500
TJB
1592 memset (&bl->target_info, 0, sizeof (bl->target_info));
1593 bl->target_info.placed_address = bl->address;
1594 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1595 bl->target_info.length = bl->length;
8181d85f 1596
35df4500
TJB
1597 if (bl->loc_type == bp_loc_software_breakpoint
1598 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1599 {
35df4500 1600 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1601 {
1602 /* If the explicitly specified breakpoint type
1603 is not hardware breakpoint, check the memory map to see
1604 if the breakpoint address is in read only memory or not.
4a64f543 1605
765dc015
VP
1606 Two important cases are:
1607 - location type is not hardware breakpoint, memory
1608 is readonly. We change the type of the location to
1609 hardware breakpoint.
4a64f543
MS
1610 - location type is hardware breakpoint, memory is
1611 read-write. This means we've previously made the
1612 location hardware one, but then the memory map changed,
1613 so we undo.
765dc015 1614
4a64f543
MS
1615 When breakpoints are removed, remove_breakpoints will use
1616 location types we've just set here, the only possible
1617 problem is that memory map has changed during running
1618 program, but it's not going to work anyway with current
1619 gdb. */
765dc015 1620 struct mem_region *mr
35df4500 1621 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1622
1623 if (mr)
1624 {
1625 if (automatic_hardware_breakpoints)
1626 {
765dc015
VP
1627 enum bp_loc_type new_type;
1628
1629 if (mr->attrib.mode != MEM_RW)
1630 new_type = bp_loc_hardware_breakpoint;
1631 else
1632 new_type = bp_loc_software_breakpoint;
1633
35df4500 1634 if (new_type != bl->loc_type)
765dc015
VP
1635 {
1636 static int said = 0;
cc59ec59 1637
35df4500 1638 bl->loc_type = new_type;
765dc015
VP
1639 if (!said)
1640 {
3e43a32a
MS
1641 fprintf_filtered (gdb_stdout,
1642 _("Note: automatically using "
1643 "hardware breakpoints for "
1644 "read-only addresses.\n"));
765dc015
VP
1645 said = 1;
1646 }
1647 }
1648 }
35df4500 1649 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1650 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1651 warning (_("cannot set software breakpoint "
1652 "at readonly address %s"),
35df4500 1653 paddress (bl->gdbarch, bl->address));
765dc015
VP
1654 }
1655 }
1656
879bfdc2
DJ
1657 /* First check to see if we have to handle an overlay. */
1658 if (overlay_debugging == ovly_off
35df4500
TJB
1659 || bl->section == NULL
1660 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1661 {
1662 /* No overlay handling: just set the breakpoint. */
1663
348d480f 1664 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1665 }
1666 else
1667 {
4a64f543 1668 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1669 Shall we set a breakpoint at the LMA? */
1670 if (!overlay_events_enabled)
1671 {
1672 /* Yes -- overlay event support is not active,
1673 so we must try to set a breakpoint at the LMA.
1674 This will not work for a hardware breakpoint. */
35df4500 1675 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1676 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1677 bl->owner->number);
879bfdc2
DJ
1678 else
1679 {
35df4500
TJB
1680 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1681 bl->section);
879bfdc2 1682 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1683 bl->overlay_target_info = bl->target_info;
1684 bl->overlay_target_info.placed_address = addr;
1685 val = target_insert_breakpoint (bl->gdbarch,
1686 &bl->overlay_target_info);
879bfdc2 1687 if (val != 0)
99361f52 1688 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1689 "Overlay breakpoint %d "
1690 "failed: in ROM?\n",
35df4500 1691 bl->owner->number);
879bfdc2
DJ
1692 }
1693 }
1694 /* Shall we set a breakpoint at the VMA? */
35df4500 1695 if (section_is_mapped (bl->section))
879bfdc2
DJ
1696 {
1697 /* Yes. This overlay section is mapped into memory. */
348d480f 1698 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1699 }
1700 else
1701 {
1702 /* No. This breakpoint will not be inserted.
1703 No error, but do not mark the bp as 'inserted'. */
1704 return 0;
1705 }
1706 }
1707
1708 if (val)
1709 {
1710 /* Can't set the breakpoint. */
35df4500 1711 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1712 {
4a64f543 1713 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1714 val = 0;
35df4500 1715 bl->shlib_disabled = 1;
8d3788bd 1716 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1717 if (!*disabled_breaks)
1718 {
1719 fprintf_unfiltered (tmp_error_stream,
1720 "Cannot insert breakpoint %d.\n",
35df4500 1721 bl->owner->number);
879bfdc2 1722 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1723 "Temporarily disabling shared "
1724 "library breakpoints:\n");
879bfdc2
DJ
1725 }
1726 *disabled_breaks = 1;
1727 fprintf_unfiltered (tmp_error_stream,
35df4500 1728 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1729 }
1730 else
879bfdc2 1731 {
35df4500 1732 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1733 {
1734 *hw_breakpoint_error = 1;
3e43a32a
MS
1735 fprintf_unfiltered (tmp_error_stream,
1736 "Cannot insert hardware "
1737 "breakpoint %d.\n",
35df4500 1738 bl->owner->number);
879bfdc2
DJ
1739 }
1740 else
1741 {
1742 fprintf_unfiltered (tmp_error_stream,
1743 "Cannot insert breakpoint %d.\n",
35df4500 1744 bl->owner->number);
879bfdc2
DJ
1745 fprintf_filtered (tmp_error_stream,
1746 "Error accessing memory address ");
35df4500 1747 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1748 tmp_error_stream);
879bfdc2
DJ
1749 fprintf_filtered (tmp_error_stream, ": %s.\n",
1750 safe_strerror (val));
1751 }
1752
1753 }
1754 }
1755 else
35df4500 1756 bl->inserted = 1;
879bfdc2
DJ
1757
1758 return val;
1759 }
1760
35df4500 1761 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1762 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1763 watchpoints. It's not clear that it's necessary... */
35df4500 1764 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1765 {
77b06cd7
TJB
1766 gdb_assert (bl->owner->ops != NULL
1767 && bl->owner->ops->insert_location != NULL);
1768
1769 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1770
1771 /* If trying to set a read-watchpoint, and it turns out it's not
1772 supported, try emulating one with an access watchpoint. */
35df4500 1773 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1774 {
1775 struct bp_location *loc, **loc_temp;
1776
1777 /* But don't try to insert it, if there's already another
1778 hw_access location that would be considered a duplicate
1779 of this one. */
1780 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1781 if (loc != bl
85d721b8 1782 && loc->watchpoint_type == hw_access
35df4500 1783 && watchpoint_locations_match (bl, loc))
85d721b8 1784 {
35df4500
TJB
1785 bl->duplicate = 1;
1786 bl->inserted = 1;
1787 bl->target_info = loc->target_info;
1788 bl->watchpoint_type = hw_access;
85d721b8
PA
1789 val = 0;
1790 break;
1791 }
1792
1793 if (val == 1)
1794 {
77b06cd7
TJB
1795 bl->watchpoint_type = hw_access;
1796 val = bl->owner->ops->insert_location (bl);
1797
1798 if (val)
1799 /* Back to the original value. */
1800 bl->watchpoint_type = hw_read;
85d721b8
PA
1801 }
1802 }
1803
35df4500 1804 bl->inserted = (val == 0);
879bfdc2
DJ
1805 }
1806
35df4500 1807 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1808 {
77b06cd7
TJB
1809 gdb_assert (bl->owner->ops != NULL
1810 && bl->owner->ops->insert_location != NULL);
1811
1812 val = bl->owner->ops->insert_location (bl);
1813 if (val)
1814 {
1815 bl->owner->enable_state = bp_disabled;
1816
1817 if (val == 1)
1818 warning (_("\
1819Error inserting catchpoint %d: Your system does not support this type\n\
1820of catchpoint."), bl->owner->number);
1821 else
1822 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1823 }
1824
1825 bl->inserted = (val == 0);
1640b821
DJ
1826
1827 /* We've already printed an error message if there was a problem
1828 inserting this catchpoint, and we've disabled the catchpoint,
1829 so just return success. */
1830 return 0;
879bfdc2
DJ
1831 }
1832
1833 return 0;
1834}
1835
6c95b8df
PA
1836/* This function is called when program space PSPACE is about to be
1837 deleted. It takes care of updating breakpoints to not reference
1838 PSPACE anymore. */
1839
1840void
1841breakpoint_program_space_exit (struct program_space *pspace)
1842{
1843 struct breakpoint *b, *b_temp;
876fa593 1844 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1845
1846 /* Remove any breakpoint that was set through this program space. */
1847 ALL_BREAKPOINTS_SAFE (b, b_temp)
1848 {
1849 if (b->pspace == pspace)
1850 delete_breakpoint (b);
1851 }
1852
1853 /* Breakpoints set through other program spaces could have locations
1854 bound to PSPACE as well. Remove those. */
876fa593 1855 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1856 {
1857 struct bp_location *tmp;
1858
1859 if (loc->pspace == pspace)
1860 {
2bdf28a0 1861 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1862 if (loc->owner->loc == loc)
1863 loc->owner->loc = loc->next;
1864 else
1865 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1866 if (tmp->next == loc)
1867 {
1868 tmp->next = loc->next;
1869 break;
1870 }
1871 }
1872 }
1873
1874 /* Now update the global location list to permanently delete the
1875 removed locations above. */
1876 update_global_location_list (0);
1877}
1878
74960c60
VP
1879/* Make sure all breakpoints are inserted in inferior.
1880 Throws exception on any error.
1881 A breakpoint that is already inserted won't be inserted
1882 again, so calling this function twice is safe. */
1883void
1884insert_breakpoints (void)
1885{
1886 struct breakpoint *bpt;
1887
1888 ALL_BREAKPOINTS (bpt)
1889 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1890 {
1891 struct watchpoint *w = (struct watchpoint *) bpt;
1892
1893 update_watchpoint (w, 0 /* don't reparse. */);
1894 }
74960c60 1895
b60e7edf 1896 update_global_location_list (1);
74960c60 1897
c35b1492
PA
1898 /* update_global_location_list does not insert breakpoints when
1899 always_inserted_mode is not enabled. Explicitly insert them
1900 now. */
1901 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1902 insert_breakpoint_locations ();
1903}
1904
c906108c
SS
1905/* insert_breakpoints is used when starting or continuing the program.
1906 remove_breakpoints is used when the program stops.
1907 Both return zero if successful,
1908 or an `errno' value if could not write the inferior. */
1909
74960c60
VP
1910static void
1911insert_breakpoint_locations (void)
c906108c 1912{
a5606eee 1913 struct breakpoint *bpt;
35df4500 1914 struct bp_location *bl, **blp_tmp;
e236ba44 1915 int error = 0;
c906108c
SS
1916 int val = 0;
1917 int disabled_breaks = 0;
81d0cc19 1918 int hw_breakpoint_error = 0;
c906108c 1919
81d0cc19 1920 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1921 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1922
81d0cc19
GS
1923 /* Explicitly mark the warning -- this will only be printed if
1924 there was an error. */
1925 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1926
1927 save_current_space_and_thread ();
1928
35df4500 1929 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1930 {
35df4500 1931 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1932 continue;
1933
4a64f543
MS
1934 /* There is no point inserting thread-specific breakpoints if
1935 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1936 has BL->OWNER always non-NULL. */
35df4500
TJB
1937 if (bl->owner->thread != -1
1938 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1939 continue;
1940
35df4500 1941 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1942
1943 /* For targets that support global breakpoints, there's no need
1944 to select an inferior to insert breakpoint to. In fact, even
1945 if we aren't attached to any process yet, we should still
1946 insert breakpoints. */
1947 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1948 && ptid_equal (inferior_ptid, null_ptid))
1949 continue;
1950
35df4500 1951 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1952 &hw_breakpoint_error);
1953 if (val)
e236ba44 1954 error = val;
879bfdc2 1955 }
c906108c 1956
4a64f543
MS
1957 /* If we failed to insert all locations of a watchpoint, remove
1958 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1959 ALL_BREAKPOINTS (bpt)
1960 {
1961 int some_failed = 0;
1962 struct bp_location *loc;
1963
1964 if (!is_hardware_watchpoint (bpt))
1965 continue;
1966
d6b74ac4 1967 if (!breakpoint_enabled (bpt))
a5606eee 1968 continue;
74960c60
VP
1969
1970 if (bpt->disposition == disp_del_at_next_stop)
1971 continue;
a5606eee
VP
1972
1973 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1974 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1975 {
1976 some_failed = 1;
1977 break;
1978 }
1979 if (some_failed)
1980 {
1981 for (loc = bpt->loc; loc; loc = loc->next)
1982 if (loc->inserted)
1983 remove_breakpoint (loc, mark_uninserted);
1984
1985 hw_breakpoint_error = 1;
1986 fprintf_unfiltered (tmp_error_stream,
1987 "Could not insert hardware watchpoint %d.\n",
1988 bpt->number);
1989 error = -1;
1990 }
1991 }
1992
e236ba44 1993 if (error)
81d0cc19
GS
1994 {
1995 /* If a hardware breakpoint or watchpoint was inserted, add a
1996 message about possibly exhausted resources. */
879bfdc2 1997 if (hw_breakpoint_error)
81d0cc19 1998 {
c6510018
MS
1999 fprintf_unfiltered (tmp_error_stream,
2000 "Could not insert hardware breakpoints:\n\
2001You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2002 }
81d0cc19
GS
2003 target_terminal_ours_for_output ();
2004 error_stream (tmp_error_stream);
2005 }
f7545552
TT
2006
2007 do_cleanups (cleanups);
c906108c
SS
2008}
2009
c906108c 2010int
fba45db2 2011remove_breakpoints (void)
c906108c 2012{
35df4500 2013 struct bp_location *bl, **blp_tmp;
3a1bae8e 2014 int val = 0;
c906108c 2015
35df4500 2016 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2017 {
35df4500
TJB
2018 if (bl->inserted)
2019 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2020 }
3a1bae8e 2021 return val;
c906108c
SS
2022}
2023
6c95b8df
PA
2024/* Remove breakpoints of process PID. */
2025
2026int
2027remove_breakpoints_pid (int pid)
2028{
35df4500 2029 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2030 int val;
2031 struct inferior *inf = find_inferior_pid (pid);
2032
35df4500 2033 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2034 {
35df4500 2035 if (bl->pspace != inf->pspace)
6c95b8df
PA
2036 continue;
2037
35df4500 2038 if (bl->inserted)
6c95b8df 2039 {
35df4500 2040 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2041 if (val != 0)
2042 return val;
2043 }
2044 }
2045 return 0;
2046}
2047
c906108c 2048int
fba45db2 2049reattach_breakpoints (int pid)
c906108c 2050{
6c95b8df 2051 struct cleanup *old_chain;
35df4500 2052 struct bp_location *bl, **blp_tmp;
c906108c 2053 int val;
86b887df 2054 struct ui_file *tmp_error_stream;
fa3a767f 2055 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2056 struct inferior *inf;
2057 struct thread_info *tp;
2058
2059 tp = any_live_thread_of_process (pid);
2060 if (tp == NULL)
2061 return 1;
2062
2063 inf = find_inferior_pid (pid);
2064 old_chain = save_inferior_ptid ();
2065
2066 inferior_ptid = tp->ptid;
a4954f26 2067
86b887df 2068 tmp_error_stream = mem_fileopen ();
a4954f26 2069 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2070
35df4500 2071 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2072 {
35df4500 2073 if (bl->pspace != inf->pspace)
6c95b8df
PA
2074 continue;
2075
35df4500 2076 if (bl->inserted)
c5aa993b 2077 {
35df4500
TJB
2078 bl->inserted = 0;
2079 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2080 if (val != 0)
2081 {
ce696e05 2082 do_cleanups (old_chain);
c5aa993b
JM
2083 return val;
2084 }
2085 }
2086 }
ce696e05 2087 do_cleanups (old_chain);
c906108c
SS
2088 return 0;
2089}
2090
e58b0e63
PA
2091static int internal_breakpoint_number = -1;
2092
84f4c1fe
PM
2093/* Set the breakpoint number of B, depending on the value of INTERNAL.
2094 If INTERNAL is non-zero, the breakpoint number will be populated
2095 from internal_breakpoint_number and that variable decremented.
e5dd4106 2096 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2097 breakpoint_count and that value incremented. Internal breakpoints
2098 do not set the internal var bpnum. */
2099static void
2100set_breakpoint_number (int internal, struct breakpoint *b)
2101{
2102 if (internal)
2103 b->number = internal_breakpoint_number--;
2104 else
2105 {
2106 set_breakpoint_count (breakpoint_count + 1);
2107 b->number = breakpoint_count;
2108 }
2109}
2110
e62c965a 2111static struct breakpoint *
a6d9a66e 2112create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2113 CORE_ADDR address, enum bptype type,
c0a91b2b 2114 const struct breakpoint_ops *ops)
e62c965a 2115{
e62c965a
PP
2116 struct symtab_and_line sal;
2117 struct breakpoint *b;
2118
4a64f543 2119 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2120
2121 sal.pc = address;
2122 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2123 sal.pspace = current_program_space;
e62c965a 2124
06edf0c0 2125 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2126 b->number = internal_breakpoint_number--;
2127 b->disposition = disp_donttouch;
2128
2129 return b;
2130}
2131
17450429
PP
2132static const char *const longjmp_names[] =
2133 {
2134 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2135 };
2136#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2137
2138/* Per-objfile data private to breakpoint.c. */
2139struct breakpoint_objfile_data
2140{
2141 /* Minimal symbol for "_ovly_debug_event" (if any). */
2142 struct minimal_symbol *overlay_msym;
2143
2144 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2145 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2146
2147 /* Minimal symbol for "std::terminate()" (if any). */
2148 struct minimal_symbol *terminate_msym;
2149
2150 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2151 struct minimal_symbol *exception_msym;
2152};
2153
2154static const struct objfile_data *breakpoint_objfile_key;
2155
2156/* Minimal symbol not found sentinel. */
2157static struct minimal_symbol msym_not_found;
2158
2159/* Returns TRUE if MSYM point to the "not found" sentinel. */
2160
2161static int
2162msym_not_found_p (const struct minimal_symbol *msym)
2163{
2164 return msym == &msym_not_found;
2165}
2166
2167/* Return per-objfile data needed by breakpoint.c.
2168 Allocate the data if necessary. */
2169
2170static struct breakpoint_objfile_data *
2171get_breakpoint_objfile_data (struct objfile *objfile)
2172{
2173 struct breakpoint_objfile_data *bp_objfile_data;
2174
2175 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2176 if (bp_objfile_data == NULL)
2177 {
2178 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2179 sizeof (*bp_objfile_data));
2180
2181 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2182 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2183 }
2184 return bp_objfile_data;
2185}
2186
e62c965a 2187static void
af02033e 2188create_overlay_event_breakpoint (void)
e62c965a 2189{
69de3c6a 2190 struct objfile *objfile;
af02033e 2191 const char *const func_name = "_ovly_debug_event";
e62c965a 2192
69de3c6a
PP
2193 ALL_OBJFILES (objfile)
2194 {
2195 struct breakpoint *b;
17450429
PP
2196 struct breakpoint_objfile_data *bp_objfile_data;
2197 CORE_ADDR addr;
69de3c6a 2198
17450429
PP
2199 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2200
2201 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2202 continue;
2203
2204 if (bp_objfile_data->overlay_msym == NULL)
2205 {
2206 struct minimal_symbol *m;
2207
2208 m = lookup_minimal_symbol_text (func_name, objfile);
2209 if (m == NULL)
2210 {
2211 /* Avoid future lookups in this objfile. */
2212 bp_objfile_data->overlay_msym = &msym_not_found;
2213 continue;
2214 }
2215 bp_objfile_data->overlay_msym = m;
2216 }
e62c965a 2217
17450429
PP
2218 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2219 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2220 bp_overlay_event,
2221 &internal_breakpoint_ops);
69de3c6a 2222 b->addr_string = xstrdup (func_name);
e62c965a 2223
69de3c6a
PP
2224 if (overlay_debugging == ovly_auto)
2225 {
2226 b->enable_state = bp_enabled;
2227 overlay_events_enabled = 1;
2228 }
2229 else
2230 {
2231 b->enable_state = bp_disabled;
2232 overlay_events_enabled = 0;
2233 }
e62c965a
PP
2234 }
2235 update_global_location_list (1);
2236}
2237
0fd8e87f 2238static void
af02033e 2239create_longjmp_master_breakpoint (void)
0fd8e87f 2240{
6c95b8df 2241 struct program_space *pspace;
6c95b8df
PA
2242 struct cleanup *old_chain;
2243
2244 old_chain = save_current_program_space ();
0fd8e87f 2245
6c95b8df 2246 ALL_PSPACES (pspace)
af02033e
PP
2247 {
2248 struct objfile *objfile;
2249
2250 set_current_program_space (pspace);
2251
2252 ALL_OBJFILES (objfile)
0fd8e87f 2253 {
af02033e
PP
2254 int i;
2255 struct gdbarch *gdbarch;
17450429 2256 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2257
af02033e
PP
2258 gdbarch = get_objfile_arch (objfile);
2259 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2260 continue;
2261
17450429
PP
2262 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2263
2264 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2265 {
2266 struct breakpoint *b;
af02033e 2267 const char *func_name;
17450429 2268 CORE_ADDR addr;
6c95b8df 2269
17450429 2270 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2271 continue;
0fd8e87f 2272
17450429
PP
2273 func_name = longjmp_names[i];
2274 if (bp_objfile_data->longjmp_msym[i] == NULL)
2275 {
2276 struct minimal_symbol *m;
2277
2278 m = lookup_minimal_symbol_text (func_name, objfile);
2279 if (m == NULL)
2280 {
2281 /* Prevent future lookups in this objfile. */
2282 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2283 continue;
2284 }
2285 bp_objfile_data->longjmp_msym[i] = m;
2286 }
2287
2288 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2289 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2290 &internal_breakpoint_ops);
af02033e
PP
2291 b->addr_string = xstrdup (func_name);
2292 b->enable_state = bp_disabled;
2293 }
0fd8e87f 2294 }
af02033e 2295 }
0fd8e87f 2296 update_global_location_list (1);
6c95b8df
PA
2297
2298 do_cleanups (old_chain);
0fd8e87f
UW
2299}
2300
af02033e 2301/* Create a master std::terminate breakpoint. */
aa7d318d 2302static void
af02033e 2303create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2304{
2305 struct program_space *pspace;
aa7d318d 2306 struct cleanup *old_chain;
af02033e 2307 const char *const func_name = "std::terminate()";
aa7d318d
TT
2308
2309 old_chain = save_current_program_space ();
2310
2311 ALL_PSPACES (pspace)
17450429
PP
2312 {
2313 struct objfile *objfile;
2314 CORE_ADDR addr;
2315
2316 set_current_program_space (pspace);
2317
aa7d318d
TT
2318 ALL_OBJFILES (objfile)
2319 {
2320 struct breakpoint *b;
17450429 2321 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2322
17450429 2323 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2324
17450429
PP
2325 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2326 continue;
2327
2328 if (bp_objfile_data->terminate_msym == NULL)
2329 {
2330 struct minimal_symbol *m;
2331
2332 m = lookup_minimal_symbol (func_name, NULL, objfile);
2333 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2334 && MSYMBOL_TYPE (m) != mst_file_text))
2335 {
2336 /* Prevent future lookups in this objfile. */
2337 bp_objfile_data->terminate_msym = &msym_not_found;
2338 continue;
2339 }
2340 bp_objfile_data->terminate_msym = m;
2341 }
aa7d318d 2342
17450429
PP
2343 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2344 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2345 bp_std_terminate_master,
2346 &internal_breakpoint_ops);
aa7d318d
TT
2347 b->addr_string = xstrdup (func_name);
2348 b->enable_state = bp_disabled;
2349 }
17450429
PP
2350 }
2351
aa7d318d
TT
2352 update_global_location_list (1);
2353
2354 do_cleanups (old_chain);
2355}
2356
186c406b
TT
2357/* Install a master breakpoint on the unwinder's debug hook. */
2358
2359void
2360create_exception_master_breakpoint (void)
2361{
2362 struct objfile *objfile;
17450429 2363 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2364
2365 ALL_OBJFILES (objfile)
2366 {
17450429
PP
2367 struct breakpoint *b;
2368 struct gdbarch *gdbarch;
2369 struct breakpoint_objfile_data *bp_objfile_data;
2370 CORE_ADDR addr;
2371
2372 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2373
2374 if (msym_not_found_p (bp_objfile_data->exception_msym))
2375 continue;
2376
2377 gdbarch = get_objfile_arch (objfile);
186c406b 2378
17450429 2379 if (bp_objfile_data->exception_msym == NULL)
186c406b 2380 {
17450429 2381 struct minimal_symbol *debug_hook;
186c406b 2382
17450429
PP
2383 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2384 if (debug_hook == NULL)
2385 {
2386 bp_objfile_data->exception_msym = &msym_not_found;
2387 continue;
2388 }
2389
2390 bp_objfile_data->exception_msym = debug_hook;
186c406b 2391 }
17450429
PP
2392
2393 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2394 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2395 &current_target);
06edf0c0
PA
2396 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2397 &internal_breakpoint_ops);
17450429
PP
2398 b->addr_string = xstrdup (func_name);
2399 b->enable_state = bp_disabled;
186c406b
TT
2400 }
2401
2402 update_global_location_list (1);
2403}
2404
c906108c 2405void
fba45db2 2406update_breakpoints_after_exec (void)
c906108c 2407{
35df4500 2408 struct breakpoint *b, *b_tmp;
876fa593 2409 struct bp_location *bploc, **bplocp_tmp;
c906108c 2410
25b22b0a
PA
2411 /* We're about to delete breakpoints from GDB's lists. If the
2412 INSERTED flag is true, GDB will try to lift the breakpoints by
2413 writing the breakpoints' "shadow contents" back into memory. The
2414 "shadow contents" are NOT valid after an exec, so GDB should not
2415 do that. Instead, the target is responsible from marking
2416 breakpoints out as soon as it detects an exec. We don't do that
2417 here instead, because there may be other attempts to delete
2418 breakpoints after detecting an exec and before reaching here. */
876fa593 2419 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2420 if (bploc->pspace == current_program_space)
2421 gdb_assert (!bploc->inserted);
c906108c 2422
35df4500 2423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2424 {
6c95b8df
PA
2425 if (b->pspace != current_program_space)
2426 continue;
2427
4a64f543 2428 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2429 if (b->type == bp_shlib_event)
2430 {
2431 delete_breakpoint (b);
2432 continue;
2433 }
c906108c 2434
4a64f543 2435 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2436 if (b->type == bp_jit_event)
2437 {
2438 delete_breakpoint (b);
2439 continue;
2440 }
2441
1900040c 2442 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2443 as must overlay event and longjmp master breakpoints. */
2444 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2445 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2446 || b->type == bp_exception_master)
c4093a6a
JM
2447 {
2448 delete_breakpoint (b);
2449 continue;
2450 }
2451
4a64f543 2452 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2453 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2454 {
2455 delete_breakpoint (b);
2456 continue;
2457 }
2458
611c83ae
PA
2459 /* Longjmp and longjmp-resume breakpoints are also meaningless
2460 after an exec. */
186c406b
TT
2461 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2462 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2463 {
2464 delete_breakpoint (b);
2465 continue;
2466 }
2467
ce78b96d
JB
2468 if (b->type == bp_catchpoint)
2469 {
2470 /* For now, none of the bp_catchpoint breakpoints need to
2471 do anything at this point. In the future, if some of
2472 the catchpoints need to something, we will need to add
2473 a new method, and call this method from here. */
2474 continue;
2475 }
2476
c5aa993b
JM
2477 /* bp_finish is a special case. The only way we ought to be able
2478 to see one of these when an exec() has happened, is if the user
2479 caught a vfork, and then said "finish". Ordinarily a finish just
2480 carries them to the call-site of the current callee, by setting
2481 a temporary bp there and resuming. But in this case, the finish
2482 will carry them entirely through the vfork & exec.
2483
2484 We don't want to allow a bp_finish to remain inserted now. But
2485 we can't safely delete it, 'cause finish_command has a handle to
2486 the bp on a bpstat, and will later want to delete it. There's a
2487 chance (and I've seen it happen) that if we delete the bp_finish
2488 here, that its storage will get reused by the time finish_command
2489 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2490 We really must allow finish_command to delete a bp_finish.
2491
e5dd4106 2492 In the absence of a general solution for the "how do we know
53a5351d
JM
2493 it's safe to delete something others may have handles to?"
2494 problem, what we'll do here is just uninsert the bp_finish, and
2495 let finish_command delete it.
2496
2497 (We know the bp_finish is "doomed" in the sense that it's
2498 momentary, and will be deleted as soon as finish_command sees
2499 the inferior stopped. So it doesn't matter that the bp's
2500 address is probably bogus in the new a.out, unlike e.g., the
2501 solib breakpoints.) */
c5aa993b 2502
c5aa993b
JM
2503 if (b->type == bp_finish)
2504 {
2505 continue;
2506 }
2507
2508 /* Without a symbolic address, we have little hope of the
2509 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2510 a.out. */
c5aa993b
JM
2511 if (b->addr_string == NULL)
2512 {
2513 delete_breakpoint (b);
2514 continue;
2515 }
c5aa993b 2516 }
1900040c 2517 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2518 create_overlay_event_breakpoint ();
2519 create_longjmp_master_breakpoint ();
2520 create_std_terminate_master_breakpoint ();
186c406b 2521 create_exception_master_breakpoint ();
c906108c
SS
2522}
2523
2524int
fba45db2 2525detach_breakpoints (int pid)
c906108c 2526{
35df4500 2527 struct bp_location *bl, **blp_tmp;
3a1bae8e 2528 int val = 0;
ce696e05 2529 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2530 struct inferior *inf = current_inferior ();
c5aa993b 2531
39f77062 2532 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2533 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2534
6c95b8df 2535 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2536 inferior_ptid = pid_to_ptid (pid);
35df4500 2537 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2538 {
35df4500 2539 if (bl->pspace != inf->pspace)
6c95b8df
PA
2540 continue;
2541
35df4500
TJB
2542 if (bl->inserted)
2543 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2544 }
d03285ec
UW
2545
2546 /* Detach single-step breakpoints as well. */
2547 detach_single_step_breakpoints ();
2548
ce696e05 2549 do_cleanups (old_chain);
3a1bae8e 2550 return val;
c906108c
SS
2551}
2552
35df4500 2553/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2554 Note that this is used to detach breakpoints from a child fork.
2555 When we get here, the child isn't in the inferior list, and neither
2556 do we have objects to represent its address space --- we should
35df4500 2557 *not* look at bl->pspace->aspace here. */
6c95b8df 2558
c906108c 2559static int
35df4500 2560remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2561{
2562 int val;
c5aa993b 2563
35df4500
TJB
2564 /* BL is never in moribund_locations by our callers. */
2565 gdb_assert (bl->owner != NULL);
2bdf28a0 2566
35df4500 2567 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2568 /* Permanent breakpoints cannot be inserted or removed. */
2569 return 0;
2570
74960c60
VP
2571 /* The type of none suggests that owner is actually deleted.
2572 This should not ever happen. */
35df4500 2573 gdb_assert (bl->owner->type != bp_none);
0bde7532 2574
35df4500
TJB
2575 if (bl->loc_type == bp_loc_software_breakpoint
2576 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2577 {
c02f5703
MS
2578 /* "Normal" instruction breakpoint: either the standard
2579 trap-instruction bp (bp_breakpoint), or a
2580 bp_hardware_breakpoint. */
2581
2582 /* First check to see if we have to handle an overlay. */
2583 if (overlay_debugging == ovly_off
35df4500
TJB
2584 || bl->section == NULL
2585 || !(section_is_overlay (bl->section)))
c02f5703
MS
2586 {
2587 /* No overlay handling: just remove the breakpoint. */
348d480f 2588 val = bl->owner->ops->remove_location (bl);
c02f5703 2589 }
c906108c
SS
2590 else
2591 {
4a64f543 2592 /* This breakpoint is in an overlay section.
c02f5703
MS
2593 Did we set a breakpoint at the LMA? */
2594 if (!overlay_events_enabled)
2595 {
2596 /* Yes -- overlay event support is not active, so we
2597 should have set a breakpoint at the LMA. Remove it.
2598 */
c02f5703
MS
2599 /* Ignore any failures: if the LMA is in ROM, we will
2600 have already warned when we failed to insert it. */
35df4500
TJB
2601 if (bl->loc_type == bp_loc_hardware_breakpoint)
2602 target_remove_hw_breakpoint (bl->gdbarch,
2603 &bl->overlay_target_info);
c02f5703 2604 else
35df4500
TJB
2605 target_remove_breakpoint (bl->gdbarch,
2606 &bl->overlay_target_info);
c02f5703
MS
2607 }
2608 /* Did we set a breakpoint at the VMA?
2609 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2610 if (bl->inserted)
c906108c 2611 {
c02f5703
MS
2612 /* Yes -- remove it. Previously we did not bother to
2613 remove the breakpoint if the section had been
2614 unmapped, but let's not rely on that being safe. We
2615 don't know what the overlay manager might do. */
aa67235e
UW
2616
2617 /* However, we should remove *software* breakpoints only
2618 if the section is still mapped, or else we overwrite
2619 wrong code with the saved shadow contents. */
348d480f
PA
2620 if (bl->loc_type == bp_loc_hardware_breakpoint
2621 || section_is_mapped (bl->section))
2622 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2623 else
2624 val = 0;
c906108c 2625 }
c02f5703
MS
2626 else
2627 {
2628 /* No -- not inserted, so no need to remove. No error. */
2629 val = 0;
2630 }
c906108c 2631 }
879d1e6b
UW
2632
2633 /* In some cases, we might not be able to remove a breakpoint
2634 in a shared library that has already been removed, but we
2635 have not yet processed the shlib unload event. */
35df4500 2636 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2637 val = 0;
2638
c906108c
SS
2639 if (val)
2640 return val;
35df4500 2641 bl->inserted = (is == mark_inserted);
c906108c 2642 }
35df4500 2643 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2644 {
77b06cd7
TJB
2645 gdb_assert (bl->owner->ops != NULL
2646 && bl->owner->ops->remove_location != NULL);
2647
35df4500 2648 bl->inserted = (is == mark_inserted);
77b06cd7 2649 bl->owner->ops->remove_location (bl);
2e70b7b9 2650
c906108c 2651 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2652 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2653 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2654 bl->owner->number);
c906108c 2655 }
35df4500
TJB
2656 else if (bl->owner->type == bp_catchpoint
2657 && breakpoint_enabled (bl->owner)
2658 && !bl->duplicate)
ce78b96d 2659 {
77b06cd7
TJB
2660 gdb_assert (bl->owner->ops != NULL
2661 && bl->owner->ops->remove_location != NULL);
ce78b96d 2662
77b06cd7 2663 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2664 if (val)
2665 return val;
77b06cd7 2666
35df4500 2667 bl->inserted = (is == mark_inserted);
ce78b96d 2668 }
c906108c
SS
2669
2670 return 0;
2671}
2672
6c95b8df 2673static int
35df4500 2674remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2675{
2676 int ret;
2677 struct cleanup *old_chain;
2678
35df4500
TJB
2679 /* BL is never in moribund_locations by our callers. */
2680 gdb_assert (bl->owner != NULL);
2bdf28a0 2681
35df4500 2682 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2683 /* Permanent breakpoints cannot be inserted or removed. */
2684 return 0;
2685
2686 /* The type of none suggests that owner is actually deleted.
2687 This should not ever happen. */
35df4500 2688 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2689
2690 old_chain = save_current_space_and_thread ();
2691
35df4500 2692 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2693
35df4500 2694 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2695
2696 do_cleanups (old_chain);
2697 return ret;
2698}
2699
c906108c
SS
2700/* Clear the "inserted" flag in all breakpoints. */
2701
25b22b0a 2702void
fba45db2 2703mark_breakpoints_out (void)
c906108c 2704{
35df4500 2705 struct bp_location *bl, **blp_tmp;
c906108c 2706
35df4500
TJB
2707 ALL_BP_LOCATIONS (bl, blp_tmp)
2708 if (bl->pspace == current_program_space)
2709 bl->inserted = 0;
c906108c
SS
2710}
2711
53a5351d
JM
2712/* Clear the "inserted" flag in all breakpoints and delete any
2713 breakpoints which should go away between runs of the program.
c906108c
SS
2714
2715 Plus other such housekeeping that has to be done for breakpoints
2716 between runs.
2717
53a5351d
JM
2718 Note: this function gets called at the end of a run (by
2719 generic_mourn_inferior) and when a run begins (by
4a64f543 2720 init_wait_for_inferior). */
c906108c
SS
2721
2722
2723
2724void
fba45db2 2725breakpoint_init_inferior (enum inf_context context)
c906108c 2726{
35df4500
TJB
2727 struct breakpoint *b, *b_tmp;
2728 struct bp_location *bl, **blp_tmp;
1c5cfe86 2729 int ix;
6c95b8df 2730 struct program_space *pspace = current_program_space;
c906108c 2731
50c71eaf
PA
2732 /* If breakpoint locations are shared across processes, then there's
2733 nothing to do. */
2567c7d9 2734 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2735 return;
2736
35df4500 2737 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2738 {
35df4500
TJB
2739 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2740 if (bl->pspace == pspace
2741 && bl->owner->enable_state != bp_permanent)
2742 bl->inserted = 0;
6c95b8df 2743 }
075f6582 2744
35df4500 2745 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2746 {
6c95b8df
PA
2747 if (b->loc && b->loc->pspace != pspace)
2748 continue;
2749
c5aa993b
JM
2750 switch (b->type)
2751 {
2752 case bp_call_dummy:
c906108c 2753
c5aa993b 2754 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2755 cause problems when the inferior is rerun, so we better get
2756 rid of it. */
2757
2758 case bp_watchpoint_scope:
2759
2760 /* Also get rid of scope breakpoints. */
2761
2762 case bp_shlib_event:
2763
2764 /* Also remove solib event breakpoints. Their addresses may
2765 have changed since the last time we ran the program.
2766 Actually we may now be debugging against different target;
2767 and so the solib backend that installed this breakpoint may
2768 not be used in by the target. E.g.,
2769
2770 (gdb) file prog-linux
2771 (gdb) run # native linux target
2772 ...
2773 (gdb) kill
2774 (gdb) file prog-win.exe
2775 (gdb) tar rem :9999 # remote Windows gdbserver.
2776 */
c906108c 2777
c5aa993b
JM
2778 delete_breakpoint (b);
2779 break;
c906108c 2780
c5aa993b
JM
2781 case bp_watchpoint:
2782 case bp_hardware_watchpoint:
2783 case bp_read_watchpoint:
2784 case bp_access_watchpoint:
3a5c3e22
PA
2785 {
2786 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2787
3a5c3e22
PA
2788 /* Likewise for watchpoints on local expressions. */
2789 if (w->exp_valid_block != NULL)
2790 delete_breakpoint (b);
2791 else if (context == inf_starting)
2792 {
2793 /* Reset val field to force reread of starting value in
2794 insert_breakpoints. */
2795 if (w->val)
2796 value_free (w->val);
2797 w->val = NULL;
2798 w->val_valid = 0;
c860120c 2799 }
3a5c3e22 2800 }
c5aa993b
JM
2801 break;
2802 default:
c5aa993b
JM
2803 break;
2804 }
2805 }
1c5cfe86
PA
2806
2807 /* Get rid of the moribund locations. */
35df4500
TJB
2808 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2809 decref_bp_location (&bl);
1c5cfe86 2810 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2811}
2812
6c95b8df
PA
2813/* These functions concern about actual breakpoints inserted in the
2814 target --- to e.g. check if we need to do decr_pc adjustment or if
2815 we need to hop over the bkpt --- so we check for address space
2816 match, not program space. */
2817
c2c6d25f
JM
2818/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2819 exists at PC. It returns ordinary_breakpoint_here if it's an
2820 ordinary breakpoint, or permanent_breakpoint_here if it's a
2821 permanent breakpoint.
2822 - When continuing from a location with an ordinary breakpoint, we
2823 actually single step once before calling insert_breakpoints.
e5dd4106 2824 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2825 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2826 the target, to advance the PC past the breakpoint. */
c906108c 2827
c2c6d25f 2828enum breakpoint_here
6c95b8df 2829breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2830{
35df4500 2831 struct bp_location *bl, **blp_tmp;
c2c6d25f 2832 int any_breakpoint_here = 0;
c906108c 2833
35df4500 2834 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2835 {
35df4500
TJB
2836 if (bl->loc_type != bp_loc_software_breakpoint
2837 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2838 continue;
2839
f1310107 2840 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2841 if ((breakpoint_enabled (bl->owner)
2842 || bl->owner->enable_state == bp_permanent)
f1310107 2843 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2844 {
2845 if (overlay_debugging
35df4500
TJB
2846 && section_is_overlay (bl->section)
2847 && !section_is_mapped (bl->section))
075f6582 2848 continue; /* unmapped overlay -- can't be a match */
35df4500 2849 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2850 return permanent_breakpoint_here;
2851 else
2852 any_breakpoint_here = 1;
2853 }
2854 }
c906108c 2855
c2c6d25f 2856 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2857}
2858
1c5cfe86
PA
2859/* Return true if there's a moribund breakpoint at PC. */
2860
2861int
6c95b8df 2862moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2863{
2864 struct bp_location *loc;
2865 int ix;
2866
2867 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2868 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2869 return 1;
2870
2871 return 0;
2872}
c2c6d25f 2873
c36b740a 2874/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2875 inserted using regular breakpoint_chain / bp_location array
2876 mechanism. This does not check for single-step breakpoints, which
2877 are inserted and removed using direct target manipulation. */
c906108c
SS
2878
2879int
4a64f543
MS
2880regular_breakpoint_inserted_here_p (struct address_space *aspace,
2881 CORE_ADDR pc)
c906108c 2882{
35df4500 2883 struct bp_location *bl, **blp_tmp;
c906108c 2884
35df4500 2885 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2886 {
35df4500
TJB
2887 if (bl->loc_type != bp_loc_software_breakpoint
2888 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2889 continue;
2890
35df4500 2891 if (bl->inserted
f1310107 2892 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2893 {
2894 if (overlay_debugging
35df4500
TJB
2895 && section_is_overlay (bl->section)
2896 && !section_is_mapped (bl->section))
075f6582
DJ
2897 continue; /* unmapped overlay -- can't be a match */
2898 else
2899 return 1;
2900 }
c5aa993b 2901 }
c36b740a
VP
2902 return 0;
2903}
2904
2905/* Returns non-zero iff there's either regular breakpoint
2906 or a single step breakpoint inserted at PC. */
2907
2908int
6c95b8df 2909breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2910{
6c95b8df 2911 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2912 return 1;
c906108c 2913
6c95b8df 2914 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2915 return 1;
2916
c906108c
SS
2917 return 0;
2918}
2919
4fa8626c
DJ
2920/* This function returns non-zero iff there is a software breakpoint
2921 inserted at PC. */
2922
2923int
3e43a32a
MS
2924software_breakpoint_inserted_here_p (struct address_space *aspace,
2925 CORE_ADDR pc)
4fa8626c 2926{
35df4500 2927 struct bp_location *bl, **blp_tmp;
4fa8626c 2928
35df4500 2929 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2930 {
35df4500 2931 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2932 continue;
2933
35df4500
TJB
2934 if (bl->inserted
2935 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2936 aspace, pc))
4fa8626c
DJ
2937 {
2938 if (overlay_debugging
35df4500
TJB
2939 && section_is_overlay (bl->section)
2940 && !section_is_mapped (bl->section))
4fa8626c
DJ
2941 continue; /* unmapped overlay -- can't be a match */
2942 else
2943 return 1;
2944 }
2945 }
2946
1aafd4da 2947 /* Also check for software single-step breakpoints. */
6c95b8df 2948 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2949 return 1;
2950
4fa8626c
DJ
2951 return 0;
2952}
2953
9093389c
PA
2954int
2955hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2956 CORE_ADDR addr, ULONGEST len)
2957{
2958 struct breakpoint *bpt;
2959
2960 ALL_BREAKPOINTS (bpt)
2961 {
2962 struct bp_location *loc;
2963
2964 if (bpt->type != bp_hardware_watchpoint
2965 && bpt->type != bp_access_watchpoint)
2966 continue;
2967
2968 if (!breakpoint_enabled (bpt))
2969 continue;
2970
2971 for (loc = bpt->loc; loc; loc = loc->next)
2972 if (loc->pspace->aspace == aspace && loc->inserted)
2973 {
2974 CORE_ADDR l, h;
2975
2976 /* Check for intersection. */
2977 l = max (loc->address, addr);
2978 h = min (loc->address + loc->length, addr + len);
2979 if (l < h)
2980 return 1;
2981 }
2982 }
2983 return 0;
2984}
2985
075f6582
DJ
2986/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2987 PC is valid for process/thread PTID. */
c906108c
SS
2988
2989int
6c95b8df
PA
2990breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2991 ptid_t ptid)
c906108c 2992{
35df4500 2993 struct bp_location *bl, **blp_tmp;
4a306c9a 2994 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2995 int thread = -1;
4a306c9a 2996 int task = 0;
a6f1cd96 2997
35df4500 2998 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2999 {
35df4500
TJB
3000 if (bl->loc_type != bp_loc_software_breakpoint
3001 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3002 continue;
3003
35df4500
TJB
3004 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3005 if (!breakpoint_enabled (bl->owner)
3006 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3007 continue;
3008
f1310107 3009 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3010 continue;
3011
35df4500 3012 if (bl->owner->thread != -1)
075f6582 3013 {
a6f1cd96
JB
3014 /* This is a thread-specific breakpoint. Check that ptid
3015 matches that thread. If thread hasn't been computed yet,
3016 it is now time to do so. */
3017 if (thread == -1)
3018 thread = pid_to_thread_id (ptid);
35df4500 3019 if (bl->owner->thread != thread)
a6f1cd96 3020 continue;
075f6582 3021 }
a6f1cd96 3022
35df4500 3023 if (bl->owner->task != 0)
4a306c9a
JB
3024 {
3025 /* This is a task-specific breakpoint. Check that ptid
3026 matches that task. If task hasn't been computed yet,
3027 it is now time to do so. */
3028 if (task == 0)
3029 task = ada_get_task_number (ptid);
35df4500 3030 if (bl->owner->task != task)
4a306c9a
JB
3031 continue;
3032 }
3033
a6f1cd96 3034 if (overlay_debugging
35df4500
TJB
3035 && section_is_overlay (bl->section)
3036 && !section_is_mapped (bl->section))
a6f1cd96
JB
3037 continue; /* unmapped overlay -- can't be a match */
3038
3039 return 1;
c5aa993b 3040 }
c906108c
SS
3041
3042 return 0;
3043}
c906108c 3044\f
c5aa993b 3045
c906108c
SS
3046/* bpstat stuff. External routines' interfaces are documented
3047 in breakpoint.h. */
3048
3049int
fba45db2 3050ep_is_catchpoint (struct breakpoint *ep)
c906108c 3051{
533be4dd 3052 return (ep->type == bp_catchpoint);
c906108c
SS
3053}
3054
f431efe5
PA
3055/* Frees any storage that is part of a bpstat. Does not walk the
3056 'next' chain. */
3057
3058static void
198757a8
VP
3059bpstat_free (bpstat bs)
3060{
3061 if (bs->old_val != NULL)
3062 value_free (bs->old_val);
9add0f1b 3063 decref_counted_command_line (&bs->commands);
f431efe5 3064 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3065 xfree (bs);
3066}
3067
c906108c
SS
3068/* Clear a bpstat so that it says we are not at any breakpoint.
3069 Also free any storage that is part of a bpstat. */
3070
3071void
fba45db2 3072bpstat_clear (bpstat *bsp)
c906108c
SS
3073{
3074 bpstat p;
3075 bpstat q;
3076
3077 if (bsp == 0)
3078 return;
3079 p = *bsp;
3080 while (p != NULL)
3081 {
3082 q = p->next;
198757a8 3083 bpstat_free (p);
c906108c
SS
3084 p = q;
3085 }
3086 *bsp = NULL;
3087}
3088
3089/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3090 is part of the bpstat is copied as well. */
3091
3092bpstat
fba45db2 3093bpstat_copy (bpstat bs)
c906108c
SS
3094{
3095 bpstat p = NULL;
3096 bpstat tmp;
3097 bpstat retval = NULL;
3098
3099 if (bs == NULL)
3100 return bs;
3101
3102 for (; bs != NULL; bs = bs->next)
3103 {
3104 tmp = (bpstat) xmalloc (sizeof (*tmp));
3105 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3106 incref_counted_command_line (tmp->commands);
f431efe5 3107 incref_bp_location (tmp->bp_location_at);
31cc81e9 3108 if (bs->old_val != NULL)
3c3185ac
JK
3109 {
3110 tmp->old_val = value_copy (bs->old_val);
3111 release_value (tmp->old_val);
3112 }
31cc81e9 3113
c906108c
SS
3114 if (p == NULL)
3115 /* This is the first thing in the chain. */
3116 retval = tmp;
3117 else
3118 p->next = tmp;
3119 p = tmp;
3120 }
3121 p->next = NULL;
3122 return retval;
3123}
3124
4a64f543 3125/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3126
3127bpstat
fba45db2 3128bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3129{
c5aa993b
JM
3130 if (bsp == NULL)
3131 return NULL;
c906108c 3132
c5aa993b
JM
3133 for (; bsp != NULL; bsp = bsp->next)
3134 {
f431efe5 3135 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3136 return bsp;
3137 }
c906108c
SS
3138 return NULL;
3139}
3140
4a64f543
MS
3141/* Put in *NUM the breakpoint number of the first breakpoint we are
3142 stopped at. *BSP upon return is a bpstat which points to the
3143 remaining breakpoints stopped at (but which is not guaranteed to be
3144 good for anything but further calls to bpstat_num).
3145
8671a17b
PA
3146 Return 0 if passed a bpstat which does not indicate any breakpoints.
3147 Return -1 if stopped at a breakpoint that has been deleted since
3148 we set it.
3149 Return 1 otherwise. */
c906108c
SS
3150
3151int
8671a17b 3152bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3153{
3154 struct breakpoint *b;
3155
3156 if ((*bsp) == NULL)
3157 return 0; /* No more breakpoint values */
8671a17b 3158
4a64f543
MS
3159 /* We assume we'll never have several bpstats that correspond to a
3160 single breakpoint -- otherwise, this function might return the
3161 same number more than once and this will look ugly. */
f431efe5 3162 b = (*bsp)->breakpoint_at;
8671a17b
PA
3163 *bsp = (*bsp)->next;
3164 if (b == NULL)
3165 return -1; /* breakpoint that's been deleted since */
3166
3167 *num = b->number; /* We have its number */
3168 return 1;
c906108c
SS
3169}
3170
3171/* Modify BS so that the actions will not be performed. */
3172
3173void
fba45db2 3174bpstat_clear_actions (bpstat bs)
c906108c
SS
3175{
3176 for (; bs != NULL; bs = bs->next)
3177 {
9add0f1b 3178 decref_counted_command_line (&bs->commands);
dde2d684 3179 bs->commands_left = NULL;
c906108c
SS
3180 if (bs->old_val != NULL)
3181 {
3182 value_free (bs->old_val);
3183 bs->old_val = NULL;
3184 }
3185 }
3186}
3187
f3b1572e
PA
3188/* Called when a command is about to proceed the inferior. */
3189
3190static void
3191breakpoint_about_to_proceed (void)
3192{
3193 if (!ptid_equal (inferior_ptid, null_ptid))
3194 {
3195 struct thread_info *tp = inferior_thread ();
3196
3197 /* Allow inferior function calls in breakpoint commands to not
3198 interrupt the command list. When the call finishes
3199 successfully, the inferior will be standing at the same
3200 breakpoint as if nothing happened. */
16c381f0 3201 if (tp->control.in_infcall)
f3b1572e
PA
3202 return;
3203 }
3204
3205 breakpoint_proceeded = 1;
3206}
3207
4a64f543
MS
3208/* Stub for cleaning up our state if we error-out of a breakpoint
3209 command. */
c906108c 3210static void
4efb68b1 3211cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3212{
3213 executing_breakpoint_commands = 0;
3214}
3215
4a64f543
MS
3216/* Execute all the commands associated with all the breakpoints at
3217 this location. Any of these commands could cause the process to
3218 proceed beyond this point, etc. We look out for such changes by
3219 checking the global "breakpoint_proceeded" after each command.
c906108c 3220
347bddb7
PA
3221 Returns true if a breakpoint command resumed the inferior. In that
3222 case, it is the caller's responsibility to recall it again with the
3223 bpstat of the current thread. */
3224
3225static int
3226bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3227{
3228 bpstat bs;
3229 struct cleanup *old_chain;
347bddb7 3230 int again = 0;
c906108c
SS
3231
3232 /* Avoid endless recursion if a `source' command is contained
3233 in bs->commands. */
3234 if (executing_breakpoint_commands)
347bddb7 3235 return 0;
c906108c
SS
3236
3237 executing_breakpoint_commands = 1;
3238 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3239
cf6c5ffb
TT
3240 prevent_dont_repeat ();
3241
4a64f543 3242 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3243 bs = *bsp;
3244
3245 breakpoint_proceeded = 0;
3246 for (; bs != NULL; bs = bs->next)
3247 {
9add0f1b 3248 struct counted_command_line *ccmd;
6c50ab1c
JB
3249 struct command_line *cmd;
3250 struct cleanup *this_cmd_tree_chain;
3251
3252 /* Take ownership of the BSP's command tree, if it has one.
3253
3254 The command tree could legitimately contain commands like
3255 'step' and 'next', which call clear_proceed_status, which
3256 frees stop_bpstat's command tree. To make sure this doesn't
3257 free the tree we're executing out from under us, we need to
3258 take ownership of the tree ourselves. Since a given bpstat's
3259 commands are only executed once, we don't need to copy it; we
3260 can clear the pointer in the bpstat, and make sure we free
3261 the tree when we're done. */
9add0f1b
TT
3262 ccmd = bs->commands;
3263 bs->commands = NULL;
3264 this_cmd_tree_chain
3265 = make_cleanup_decref_counted_command_line (&ccmd);
3266 cmd = bs->commands_left;
3267 bs->commands_left = NULL;
6c50ab1c 3268
c906108c
SS
3269 while (cmd != NULL)
3270 {
3271 execute_control_command (cmd);
3272
3273 if (breakpoint_proceeded)
3274 break;
3275 else
3276 cmd = cmd->next;
3277 }
6c50ab1c
JB
3278
3279 /* We can free this command tree now. */
3280 do_cleanups (this_cmd_tree_chain);
3281
c906108c 3282 if (breakpoint_proceeded)
32c1e744
VP
3283 {
3284 if (target_can_async_p ())
347bddb7
PA
3285 /* If we are in async mode, then the target might be still
3286 running, not stopped at any breakpoint, so nothing for
3287 us to do here -- just return to the event loop. */
3288 ;
32c1e744
VP
3289 else
3290 /* In sync mode, when execute_control_command returns
3291 we're already standing on the next breakpoint.
347bddb7
PA
3292 Breakpoint commands for that stop were not run, since
3293 execute_command does not run breakpoint commands --
3294 only command_line_handler does, but that one is not
3295 involved in execution of breakpoint commands. So, we
3296 can now execute breakpoint commands. It should be
3297 noted that making execute_command do bpstat actions is
3298 not an option -- in this case we'll have recursive
3299 invocation of bpstat for each breakpoint with a
3300 command, and can easily blow up GDB stack. Instead, we
3301 return true, which will trigger the caller to recall us
3302 with the new stop_bpstat. */
3303 again = 1;
3304 break;
32c1e744 3305 }
c906108c 3306 }
c2b8ed2c 3307 do_cleanups (old_chain);
347bddb7
PA
3308 return again;
3309}
3310
3311void
3312bpstat_do_actions (void)
3313{
3314 /* Do any commands attached to breakpoint we are stopped at. */
3315 while (!ptid_equal (inferior_ptid, null_ptid)
3316 && target_has_execution
3317 && !is_exited (inferior_ptid)
3318 && !is_executing (inferior_ptid))
3319 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3320 and only return when it is stopped at the next breakpoint, we
3321 keep doing breakpoint actions until it returns false to
3322 indicate the inferior was not resumed. */
16c381f0 3323 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3324 break;
c906108c
SS
3325}
3326
fa4727a6
DJ
3327/* Print out the (old or new) value associated with a watchpoint. */
3328
3329static void
3330watchpoint_value_print (struct value *val, struct ui_file *stream)
3331{
3332 if (val == NULL)
3333 fprintf_unfiltered (stream, _("<unreadable>"));
3334 else
79a45b7d
TT
3335 {
3336 struct value_print_options opts;
3337 get_user_print_options (&opts);
3338 value_print (val, stream, &opts);
3339 }
fa4727a6
DJ
3340}
3341
e514a9d6 3342/* Generic routine for printing messages indicating why we
4a64f543 3343 stopped. The behavior of this function depends on the value
e514a9d6
JM
3344 'print_it' in the bpstat structure. Under some circumstances we
3345 may decide not to print anything here and delegate the task to
4a64f543 3346 normal_stop(). */
e514a9d6
JM
3347
3348static enum print_stop_action
3349print_bp_stop_message (bpstat bs)
3350{
3351 switch (bs->print_it)
3352 {
3353 case print_it_noop:
4a64f543 3354 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3355 return PRINT_UNKNOWN;
3356 break;
3357
3358 case print_it_done:
3359 /* We still want to print the frame, but we already printed the
4a64f543 3360 relevant messages. */
e514a9d6
JM
3361 return PRINT_SRC_AND_LOC;
3362 break;
3363
3364 case print_it_normal:
4f8d1dc6 3365 {
f431efe5
PA
3366 struct breakpoint *b = bs->breakpoint_at;
3367
1a6a67de
TJB
3368 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3369 which has since been deleted. */
3370 if (b == NULL)
3371 return PRINT_UNKNOWN;
3372
348d480f
PA
3373 /* Normal case. Call the breakpoint's print_it method. */
3374 return b->ops->print_it (bs);
4f8d1dc6 3375 }
348d480f 3376 break;
3086aeae 3377
e514a9d6 3378 default:
8e65ff28 3379 internal_error (__FILE__, __LINE__,
e2e0b3e5 3380 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3381 break;
c906108c 3382 }
c906108c
SS
3383}
3384
e514a9d6
JM
3385/* Print a message indicating what happened. This is called from
3386 normal_stop(). The input to this routine is the head of the bpstat
3387 list - a list of the eventpoints that caused this stop. This
3388 routine calls the generic print routine for printing a message
3389 about reasons for stopping. This will print (for example) the
3390 "Breakpoint n," part of the output. The return value of this
3391 routine is one of:
c906108c 3392
4a64f543 3393 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3394 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3395 code to print the location. An example is
c5aa993b
JM
3396 "Breakpoint 1, " which should be followed by
3397 the location.
917317f4 3398 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3399 to also print the location part of the message.
3400 An example is the catch/throw messages, which
4a64f543 3401 don't require a location appended to the end.
917317f4 3402 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3403 further info to be printed. */
c906108c 3404
917317f4 3405enum print_stop_action
fba45db2 3406bpstat_print (bpstat bs)
c906108c
SS
3407{
3408 int val;
c5aa993b 3409
c906108c 3410 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3411 (Currently all watchpoints go on the bpstat whether hit or not.
3412 That probably could (should) be changed, provided care is taken
c906108c 3413 with respect to bpstat_explains_signal). */
e514a9d6
JM
3414 for (; bs; bs = bs->next)
3415 {
3416 val = print_bp_stop_message (bs);
3417 if (val == PRINT_SRC_ONLY
3418 || val == PRINT_SRC_AND_LOC
3419 || val == PRINT_NOTHING)
3420 return val;
3421 }
c906108c 3422
e514a9d6 3423 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3424 with and nothing was printed. */
917317f4 3425 return PRINT_UNKNOWN;
c906108c
SS
3426}
3427
4a64f543
MS
3428/* Evaluate the expression EXP and return 1 if value is zero. This is
3429 used inside a catch_errors to evaluate the breakpoint condition.
3430 The argument is a "struct expression *" that has been cast to a
3431 "char *" to make it pass through catch_errors. */
c906108c
SS
3432
3433static int
4efb68b1 3434breakpoint_cond_eval (void *exp)
c906108c 3435{
278cd55f 3436 struct value *mark = value_mark ();
c5aa993b 3437 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3438
c906108c
SS
3439 value_free_to_mark (mark);
3440 return i;
3441}
3442
5760d0ab 3443/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3444
3445static bpstat
5760d0ab 3446bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3447{
3448 bpstat bs;
3449
3450 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3451 bs->next = NULL;
3452 **bs_link_pointer = bs;
3453 *bs_link_pointer = &bs->next;
f431efe5
PA
3454 bs->breakpoint_at = bl->owner;
3455 bs->bp_location_at = bl;
3456 incref_bp_location (bl);
c906108c
SS
3457 /* If the condition is false, etc., don't do the commands. */
3458 bs->commands = NULL;
9add0f1b 3459 bs->commands_left = NULL;
c906108c
SS
3460 bs->old_val = NULL;
3461 bs->print_it = print_it_normal;
3462 return bs;
3463}
3464\f
d983da9c
DJ
3465/* The target has stopped with waitstatus WS. Check if any hardware
3466 watchpoints have triggered, according to the target. */
3467
3468int
3469watchpoints_triggered (struct target_waitstatus *ws)
3470{
d92524f1 3471 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3472 CORE_ADDR addr;
3473 struct breakpoint *b;
3474
3475 if (!stopped_by_watchpoint)
3476 {
3477 /* We were not stopped by a watchpoint. Mark all watchpoints
3478 as not triggered. */
3479 ALL_BREAKPOINTS (b)
cc60f2e3 3480 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3481 {
3482 struct watchpoint *w = (struct watchpoint *) b;
3483
3484 w->watchpoint_triggered = watch_triggered_no;
3485 }
d983da9c
DJ
3486
3487 return 0;
3488 }
3489
3490 if (!target_stopped_data_address (&current_target, &addr))
3491 {
3492 /* We were stopped by a watchpoint, but we don't know where.
3493 Mark all watchpoints as unknown. */
3494 ALL_BREAKPOINTS (b)
cc60f2e3 3495 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3496 {
3497 struct watchpoint *w = (struct watchpoint *) b;
3498
3499 w->watchpoint_triggered = watch_triggered_unknown;
3500 }
d983da9c
DJ
3501
3502 return stopped_by_watchpoint;
3503 }
3504
3505 /* The target could report the data address. Mark watchpoints
3506 affected by this data address as triggered, and all others as not
3507 triggered. */
3508
3509 ALL_BREAKPOINTS (b)
cc60f2e3 3510 if (is_hardware_watchpoint (b))
d983da9c 3511 {
3a5c3e22 3512 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3513 struct bp_location *loc;
d983da9c 3514
3a5c3e22 3515 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3516 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3517 {
3a5c3e22 3518 if (is_masked_watchpoint (b))
9c06b0b4 3519 {
3a5c3e22
PA
3520 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3521 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3522
3523 if (newaddr == start)
3524 {
3a5c3e22 3525 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3526 break;
3527 }
3528 }
3529 /* Exact match not required. Within range is sufficient. */
3530 else if (target_watchpoint_addr_within_range (&current_target,
3531 addr, loc->address,
3532 loc->length))
3533 {
3a5c3e22 3534 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3535 break;
3536 }
3537 }
d983da9c
DJ
3538 }
3539
3540 return 1;
3541}
3542
c906108c
SS
3543/* Possible return values for watchpoint_check (this can't be an enum
3544 because of check_errors). */
3545/* The watchpoint has been deleted. */
3546#define WP_DELETED 1
3547/* The value has changed. */
3548#define WP_VALUE_CHANGED 2
3549/* The value has not changed. */
3550#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3551/* Ignore this watchpoint, no matter if the value changed or not. */
3552#define WP_IGNORE 4
c906108c
SS
3553
3554#define BP_TEMPFLAG 1
3555#define BP_HARDWAREFLAG 2
3556
4a64f543
MS
3557/* Evaluate watchpoint condition expression and check if its value
3558 changed.
553e4c11
JB
3559
3560 P should be a pointer to struct bpstat, but is defined as a void *
3561 in order for this function to be usable with catch_errors. */
c906108c
SS
3562
3563static int
4efb68b1 3564watchpoint_check (void *p)
c906108c
SS
3565{
3566 bpstat bs = (bpstat) p;
3a5c3e22 3567 struct watchpoint *b;
c906108c
SS
3568 struct frame_info *fr;
3569 int within_current_scope;
3570
f431efe5 3571 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3572 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3573 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3574
f6bc2008
PA
3575 /* If this is a local watchpoint, we only want to check if the
3576 watchpoint frame is in scope if the current thread is the thread
3577 that was used to create the watchpoint. */
3578 if (!watchpoint_in_thread_scope (b))
60e1c644 3579 return WP_IGNORE;
f6bc2008 3580
c906108c
SS
3581 if (b->exp_valid_block == NULL)
3582 within_current_scope = 1;
3583 else
3584 {
edb3359d
DJ
3585 struct frame_info *frame = get_current_frame ();
3586 struct gdbarch *frame_arch = get_frame_arch (frame);
3587 CORE_ADDR frame_pc = get_frame_pc (frame);
3588
4a64f543
MS
3589 /* in_function_epilogue_p() returns a non-zero value if we're
3590 still in the function but the stack frame has already been
3591 invalidated. Since we can't rely on the values of local
3592 variables after the stack has been destroyed, we are treating
3593 the watchpoint in that state as `not changed' without further
3594 checking. Don't mark watchpoints as changed if the current
3595 frame is in an epilogue - even if they are in some other
3596 frame, our view of the stack is likely to be wrong and
3597 frame_find_by_id could error out. */
a0f49112 3598 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3599 return WP_IGNORE;
a0f49112 3600
101dcfbe 3601 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3602 within_current_scope = (fr != NULL);
69fbadd5
DJ
3603
3604 /* If we've gotten confused in the unwinder, we might have
3605 returned a frame that can't describe this variable. */
edb3359d
DJ
3606 if (within_current_scope)
3607 {
3608 struct symbol *function;
3609
3610 function = get_frame_function (fr);
3611 if (function == NULL
3612 || !contained_in (b->exp_valid_block,
3613 SYMBOL_BLOCK_VALUE (function)))
3614 within_current_scope = 0;
3615 }
69fbadd5 3616
edb3359d 3617 if (within_current_scope)
c906108c
SS
3618 /* If we end up stopping, the current frame will get selected
3619 in normal_stop. So this call to select_frame won't affect
3620 the user. */
0f7d239c 3621 select_frame (fr);
c906108c 3622 }
c5aa993b 3623
c906108c
SS
3624 if (within_current_scope)
3625 {
4a64f543
MS
3626 /* We use value_{,free_to_}mark because it could be a *long*
3627 time before we return to the command level and call
3628 free_all_values. We can't call free_all_values because we
3629 might be in the middle of evaluating a function call. */
c906108c 3630
0cf6dd15 3631 int pc = 0;
9c06b0b4 3632 struct value *mark;
fa4727a6
DJ
3633 struct value *new_val;
3634
3a5c3e22 3635 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3636 /* Since we don't know the exact trigger address (from
3637 stopped_data_address), just tell the user we've triggered
3638 a mask watchpoint. */
3639 return WP_VALUE_CHANGED;
3640
3641 mark = value_mark ();
0cf6dd15 3642 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3643
4a64f543
MS
3644 /* We use value_equal_contents instead of value_equal because
3645 the latter coerces an array to a pointer, thus comparing just
3646 the address of the array instead of its contents. This is
3647 not what we want. */
fa4727a6 3648 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3649 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3650 {
fa4727a6
DJ
3651 if (new_val != NULL)
3652 {
3653 release_value (new_val);
3654 value_free_to_mark (mark);
3655 }
c906108c
SS
3656 bs->old_val = b->val;
3657 b->val = new_val;
fa4727a6 3658 b->val_valid = 1;
c906108c
SS
3659 return WP_VALUE_CHANGED;
3660 }
3661 else
3662 {
60e1c644 3663 /* Nothing changed. */
c906108c 3664 value_free_to_mark (mark);
c906108c
SS
3665 return WP_VALUE_NOT_CHANGED;
3666 }
3667 }
3668 else
3669 {
3670 /* This seems like the only logical thing to do because
c5aa993b
JM
3671 if we temporarily ignored the watchpoint, then when
3672 we reenter the block in which it is valid it contains
3673 garbage (in the case of a function, it may have two
3674 garbage values, one before and one after the prologue).
3675 So we can't even detect the first assignment to it and
3676 watch after that (since the garbage may or may not equal
3677 the first value assigned). */
348d480f
PA
3678 /* We print all the stop information in
3679 breakpoint_ops->print_it, but in this case, by the time we
3680 call breakpoint_ops->print_it this bp will be deleted
3681 already. So we have no choice but print the information
3682 here. */
9dc5e2a9 3683 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3684 ui_out_field_string
3685 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3686 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3687 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3688 ui_out_text (uiout,
3689 " deleted because the program has left the block in\n\
8b93c638 3690which its expression is valid.\n");
4ce44c66 3691
cdac0397 3692 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3693 decref_counted_command_line (&b->base.commands);
d0fb5eae 3694 watchpoint_del_at_next_stop (b);
c906108c
SS
3695
3696 return WP_DELETED;
3697 }
3698}
3699
18a18393 3700/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3701 breakpoint location BL. This function does not check if we should
3702 stop, only if BL explains the stop. */
3703
18a18393 3704static int
6c95b8df
PA
3705bpstat_check_location (const struct bp_location *bl,
3706 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3707{
3708 struct breakpoint *b = bl->owner;
3709
348d480f 3710 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3711 gdb_assert (b != NULL);
3712
348d480f 3713 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3714}
3715
3a5c3e22
PA
3716/* Determine if the watched values have actually changed, and we
3717 should stop. If not, set BS->stop to 0. */
3718
18a18393
VP
3719static void
3720bpstat_check_watchpoint (bpstat bs)
3721{
2bdf28a0 3722 const struct bp_location *bl;
3a5c3e22 3723 struct watchpoint *b;
2bdf28a0
JK
3724
3725 /* BS is built for existing struct breakpoint. */
f431efe5 3726 bl = bs->bp_location_at;
2bdf28a0 3727 gdb_assert (bl != NULL);
3a5c3e22 3728 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3729 gdb_assert (b != NULL);
18a18393 3730
18a18393 3731 {
18a18393
VP
3732 int must_check_value = 0;
3733
3a5c3e22 3734 if (b->base.type == bp_watchpoint)
18a18393
VP
3735 /* For a software watchpoint, we must always check the
3736 watched value. */
3737 must_check_value = 1;
3738 else if (b->watchpoint_triggered == watch_triggered_yes)
3739 /* We have a hardware watchpoint (read, write, or access)
3740 and the target earlier reported an address watched by
3741 this watchpoint. */
3742 must_check_value = 1;
3743 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3744 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3745 /* We were stopped by a hardware watchpoint, but the target could
3746 not report the data address. We must check the watchpoint's
3747 value. Access and read watchpoints are out of luck; without
3748 a data address, we can't figure it out. */
3749 must_check_value = 1;
3a5c3e22 3750
18a18393
VP
3751 if (must_check_value)
3752 {
3e43a32a
MS
3753 char *message
3754 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3755 b->base.number);
18a18393
VP
3756 struct cleanup *cleanups = make_cleanup (xfree, message);
3757 int e = catch_errors (watchpoint_check, bs, message,
3758 RETURN_MASK_ALL);
3759 do_cleanups (cleanups);
3760 switch (e)
3761 {
3762 case WP_DELETED:
3763 /* We've already printed what needs to be printed. */
3764 bs->print_it = print_it_done;
3765 /* Stop. */
3766 break;
60e1c644
PA
3767 case WP_IGNORE:
3768 bs->print_it = print_it_noop;
3769 bs->stop = 0;
3770 break;
18a18393 3771 case WP_VALUE_CHANGED:
3a5c3e22 3772 if (b->base.type == bp_read_watchpoint)
18a18393 3773 {
85d721b8
PA
3774 /* There are two cases to consider here:
3775
4a64f543 3776 1. We're watching the triggered memory for reads.
85d721b8
PA
3777 In that case, trust the target, and always report
3778 the watchpoint hit to the user. Even though
3779 reads don't cause value changes, the value may
3780 have changed since the last time it was read, and
3781 since we're not trapping writes, we will not see
3782 those, and as such we should ignore our notion of
3783 old value.
3784
4a64f543 3785 2. We're watching the triggered memory for both
85d721b8
PA
3786 reads and writes. There are two ways this may
3787 happen:
3788
4a64f543 3789 2.1. This is a target that can't break on data
85d721b8
PA
3790 reads only, but can break on accesses (reads or
3791 writes), such as e.g., x86. We detect this case
3792 at the time we try to insert read watchpoints.
3793
4a64f543 3794 2.2. Otherwise, the target supports read
85d721b8
PA
3795 watchpoints, but, the user set an access or write
3796 watchpoint watching the same memory as this read
3797 watchpoint.
3798
3799 If we're watching memory writes as well as reads,
3800 ignore watchpoint hits when we find that the
3801 value hasn't changed, as reads don't cause
3802 changes. This still gives false positives when
3803 the program writes the same value to memory as
3804 what there was already in memory (we will confuse
3805 it for a read), but it's much better than
3806 nothing. */
3807
3808 int other_write_watchpoint = 0;
3809
3810 if (bl->watchpoint_type == hw_read)
3811 {
3812 struct breakpoint *other_b;
3813
3814 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3815 if (other_b->type == bp_hardware_watchpoint
3816 || other_b->type == bp_access_watchpoint)
85d721b8 3817 {
3a5c3e22
PA
3818 struct watchpoint *other_w =
3819 (struct watchpoint *) other_b;
3820
3821 if (other_w->watchpoint_triggered
3822 == watch_triggered_yes)
3823 {
3824 other_write_watchpoint = 1;
3825 break;
3826 }
85d721b8
PA
3827 }
3828 }
3829
3830 if (other_write_watchpoint
3831 || bl->watchpoint_type == hw_access)
3832 {
3833 /* We're watching the same memory for writes,
3834 and the value changed since the last time we
3835 updated it, so this trap must be for a write.
3836 Ignore it. */
3837 bs->print_it = print_it_noop;
3838 bs->stop = 0;
3839 }
18a18393
VP
3840 }
3841 break;
3842 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3843 if (b->base.type == bp_hardware_watchpoint
3844 || b->base.type == bp_watchpoint)
18a18393
VP
3845 {
3846 /* Don't stop: write watchpoints shouldn't fire if
3847 the value hasn't changed. */
3848 bs->print_it = print_it_noop;
3849 bs->stop = 0;
3850 }
3851 /* Stop. */
3852 break;
3853 default:
3854 /* Can't happen. */
3855 case 0:
3856 /* Error from catch_errors. */
3a5c3e22 3857 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3858 watchpoint_del_at_next_stop (b);
18a18393
VP
3859 /* We've already printed what needs to be printed. */
3860 bs->print_it = print_it_done;
3861 break;
3862 }
3863 }
3864 else /* must_check_value == 0 */
3865 {
3866 /* This is a case where some watchpoint(s) triggered, but
3867 not at the address of this watchpoint, or else no
3868 watchpoint triggered after all. So don't print
3869 anything for this watchpoint. */
3870 bs->print_it = print_it_noop;
3871 bs->stop = 0;
3872 }
3873 }
3874}
3875
3876
3877/* Check conditions (condition proper, frame, thread and ignore count)
3878 of breakpoint referred to by BS. If we should not stop for this
3879 breakpoint, set BS->stop to 0. */
f431efe5 3880
18a18393
VP
3881static void
3882bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3883{
3884 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3885 const struct bp_location *bl;
3886 struct breakpoint *b;
3887
3888 /* BS is built for existing struct breakpoint. */
f431efe5 3889 bl = bs->bp_location_at;
2bdf28a0 3890 gdb_assert (bl != NULL);
f431efe5 3891 b = bs->breakpoint_at;
2bdf28a0 3892 gdb_assert (b != NULL);
18a18393
VP
3893
3894 if (frame_id_p (b->frame_id)
edb3359d 3895 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3896 bs->stop = 0;
3897 else if (bs->stop)
3898 {
3899 int value_is_zero = 0;
60e1c644
PA
3900 struct expression *cond;
3901
7371cf6d
PM
3902 /* Evaluate Python breakpoints that have a "stop"
3903 method implemented. */
3904 if (b->py_bp_object)
3905 bs->stop = gdbpy_should_stop (b->py_bp_object);
3906
60e1c644 3907 if (is_watchpoint (b))
3a5c3e22
PA
3908 {
3909 struct watchpoint *w = (struct watchpoint *) b;
3910
3911 cond = w->cond_exp;
3912 }
60e1c644
PA
3913 else
3914 cond = bl->cond;
3915
f431efe5 3916 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3917 {
60e1c644 3918 int within_current_scope = 1;
3a5c3e22 3919 struct watchpoint * w;
60e1c644 3920
c5bc3a77
DJ
3921 /* We use value_mark and value_free_to_mark because it could
3922 be a long time before we return to the command level and
3923 call free_all_values. We can't call free_all_values
3924 because we might be in the middle of evaluating a
3925 function call. */
3926 struct value *mark = value_mark ();
3927
3a5c3e22
PA
3928 if (is_watchpoint (b))
3929 w = (struct watchpoint *) b;
3930 else
3931 w = NULL;
3932
edb3359d
DJ
3933 /* Need to select the frame, with all that implies so that
3934 the conditions will have the right context. Because we
3935 use the frame, we will not see an inlined function's
3936 variables when we arrive at a breakpoint at the start
3937 of the inlined function; the current frame will be the
3938 call site. */
3a5c3e22 3939 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
3940 select_frame (get_current_frame ());
3941 else
3942 {
3943 struct frame_info *frame;
3944
3945 /* For local watchpoint expressions, which particular
3946 instance of a local is being watched matters, so we
3947 keep track of the frame to evaluate the expression
3948 in. To evaluate the condition however, it doesn't
3949 really matter which instantiation of the function
3950 where the condition makes sense triggers the
3951 watchpoint. This allows an expression like "watch
3952 global if q > 10" set in `func', catch writes to
3953 global on all threads that call `func', or catch
3954 writes on all recursive calls of `func' by a single
3955 thread. We simply always evaluate the condition in
3956 the innermost frame that's executing where it makes
3957 sense to evaluate the condition. It seems
3958 intuitive. */
3a5c3e22 3959 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
3960 if (frame != NULL)
3961 select_frame (frame);
3962 else
3963 within_current_scope = 0;
3964 }
3965 if (within_current_scope)
3966 value_is_zero
3967 = catch_errors (breakpoint_cond_eval, cond,
3968 "Error in testing breakpoint condition:\n",
3969 RETURN_MASK_ALL);
3970 else
3971 {
3972 warning (_("Watchpoint condition cannot be tested "
3973 "in the current scope"));
3974 /* If we failed to set the right context for this
3975 watchpoint, unconditionally report it. */
3976 value_is_zero = 0;
3977 }
4a64f543 3978 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 3979 value_free_to_mark (mark);
18a18393 3980 }
60e1c644
PA
3981
3982 if (cond && value_is_zero)
18a18393
VP
3983 {
3984 bs->stop = 0;
3985 }
3986 else if (b->thread != -1 && b->thread != thread_id)
3987 {
3988 bs->stop = 0;
3989 }
3990 else if (b->ignore_count > 0)
3991 {
3992 b->ignore_count--;
3993 annotate_ignore_count_change ();
3994 bs->stop = 0;
4a64f543 3995 /* Increase the hit count even though we don't stop. */
18a18393 3996 ++(b->hit_count);
8d3788bd 3997 observer_notify_breakpoint_modified (b);
18a18393
VP
3998 }
3999 }
4000}
4001
4002
9709f61c 4003/* Get a bpstat associated with having just stopped at address
d983da9c 4004 BP_ADDR in thread PTID.
c906108c 4005
d983da9c 4006 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4007 don't understand this stop. Result is a chain of bpstat's such
4008 that:
c906108c 4009
c5aa993b 4010 if we don't understand the stop, the result is a null pointer.
c906108c 4011
c5aa993b 4012 if we understand why we stopped, the result is not null.
c906108c 4013
c5aa993b
JM
4014 Each element of the chain refers to a particular breakpoint or
4015 watchpoint at which we have stopped. (We may have stopped for
4016 several reasons concurrently.)
c906108c 4017
c5aa993b
JM
4018 Each element of the chain has valid next, breakpoint_at,
4019 commands, FIXME??? fields. */
c906108c
SS
4020
4021bpstat
6c95b8df
PA
4022bpstat_stop_status (struct address_space *aspace,
4023 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4024{
0d381245 4025 struct breakpoint *b = NULL;
afe38095 4026 struct bp_location *bl;
20874c92 4027 struct bp_location *loc;
5760d0ab
JK
4028 /* First item of allocated bpstat's. */
4029 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4030 /* Pointer to the last thing in the chain currently. */
5760d0ab 4031 bpstat bs;
20874c92 4032 int ix;
429374b8 4033 int need_remove_insert;
f431efe5 4034 int removed_any;
c906108c 4035
f431efe5
PA
4036 /* First, build the bpstat chain with locations that explain a
4037 target stop, while being careful to not set the target running,
4038 as that may invalidate locations (in particular watchpoint
4039 locations are recreated). Resuming will happen here with
4040 breakpoint conditions or watchpoint expressions that include
4041 inferior function calls. */
c5aa993b 4042
429374b8
JK
4043 ALL_BREAKPOINTS (b)
4044 {
4045 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4046 continue;
a5606eee 4047
429374b8
JK
4048 for (bl = b->loc; bl != NULL; bl = bl->next)
4049 {
4a64f543
MS
4050 /* For hardware watchpoints, we look only at the first
4051 location. The watchpoint_check function will work on the
4052 entire expression, not the individual locations. For
4053 read watchpoints, the watchpoints_triggered function has
4054 checked all locations already. */
429374b8
JK
4055 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4056 break;
18a18393 4057
429374b8
JK
4058 if (bl->shlib_disabled)
4059 continue;
c5aa993b 4060
429374b8
JK
4061 if (!bpstat_check_location (bl, aspace, bp_addr))
4062 continue;
c5aa993b 4063
4a64f543
MS
4064 /* Come here if it's a watchpoint, or if the break address
4065 matches. */
c5aa993b 4066
4a64f543
MS
4067 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4068 explain stop. */
c5aa993b 4069
f431efe5
PA
4070 /* Assume we stop. Should we find a watchpoint that is not
4071 actually triggered, or if the condition of the breakpoint
4072 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4073 bs->stop = 1;
4074 bs->print = 1;
d983da9c 4075
f431efe5
PA
4076 /* If this is a scope breakpoint, mark the associated
4077 watchpoint as triggered so that we will handle the
4078 out-of-scope event. We'll get to the watchpoint next
4079 iteration. */
d0fb5eae 4080 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4081 {
4082 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4083
4084 w->watchpoint_triggered = watch_triggered_yes;
4085 }
f431efe5
PA
4086 }
4087 }
4088
4089 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4090 {
f1310107 4091 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4092 {
5760d0ab 4093 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4094 /* For hits of moribund locations, we should just proceed. */
4095 bs->stop = 0;
4096 bs->print = 0;
4097 bs->print_it = print_it_noop;
4098 }
4099 }
4100
f431efe5
PA
4101 /* Now go through the locations that caused the target to stop, and
4102 check whether we're interested in reporting this stop to higher
4103 layers, or whether we should resume the target transparently. */
4104
4105 removed_any = 0;
4106
5760d0ab 4107 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4108 {
4109 if (!bs->stop)
4110 continue;
4111
f431efe5 4112 b = bs->breakpoint_at;
348d480f
PA
4113 b->ops->check_status (bs);
4114 if (bs->stop)
28010a5d 4115 {
348d480f 4116 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4117
429374b8
JK
4118 if (bs->stop)
4119 {
4120 ++(b->hit_count);
8d3788bd 4121 observer_notify_breakpoint_modified (b);
c906108c 4122
4a64f543 4123 /* We will stop here. */
429374b8
JK
4124 if (b->disposition == disp_disable)
4125 {
4126 if (b->enable_state != bp_permanent)
4127 b->enable_state = bp_disabled;
f431efe5 4128 removed_any = 1;
429374b8
JK
4129 }
4130 if (b->silent)
4131 bs->print = 0;
4132 bs->commands = b->commands;
9add0f1b
TT
4133 incref_counted_command_line (bs->commands);
4134 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4135 if (bs->commands_left
4136 && (strcmp ("silent", bs->commands_left->line) == 0
4137 || (xdb_commands
4138 && strcmp ("Q",
4139 bs->commands_left->line) == 0)))
429374b8 4140 {
9add0f1b 4141 bs->commands_left = bs->commands_left->next;
429374b8
JK
4142 bs->print = 0;
4143 }
429374b8
JK
4144 }
4145
e5dd4106 4146 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4147 if (bs->stop == 0 || bs->print == 0)
4148 bs->print_it = print_it_noop;
348d480f 4149 }
429374b8 4150 }
876fa593 4151
d983da9c
DJ
4152 /* If we aren't stopping, the value of some hardware watchpoint may
4153 not have changed, but the intermediate memory locations we are
4154 watching may have. Don't bother if we're stopping; this will get
4155 done later. */
d832cb68 4156 need_remove_insert = 0;
5760d0ab
JK
4157 if (! bpstat_causes_stop (bs_head))
4158 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4159 if (!bs->stop
f431efe5
PA
4160 && bs->breakpoint_at
4161 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4162 {
3a5c3e22
PA
4163 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4164
4165 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4166 need_remove_insert = 1;
d983da9c
DJ
4167 }
4168
d832cb68 4169 if (need_remove_insert)
2d134ed3 4170 update_global_location_list (1);
f431efe5
PA
4171 else if (removed_any)
4172 update_global_location_list (0);
d832cb68 4173
5760d0ab 4174 return bs_head;
c906108c 4175}
628fe4e4
JK
4176
4177static void
4178handle_jit_event (void)
4179{
4180 struct frame_info *frame;
4181 struct gdbarch *gdbarch;
4182
4183 /* Switch terminal for any messages produced by
4184 breakpoint_re_set. */
4185 target_terminal_ours_for_output ();
4186
4187 frame = get_current_frame ();
4188 gdbarch = get_frame_arch (frame);
4189
4190 jit_event_handler (gdbarch);
4191
4192 target_terminal_inferior ();
4193}
4194
4195/* Prepare WHAT final decision for infrun. */
4196
4197/* Decide what infrun needs to do with this bpstat. */
4198
c906108c 4199struct bpstat_what
0e30163f 4200bpstat_what (bpstat bs_head)
c906108c 4201{
c906108c 4202 struct bpstat_what retval;
628fe4e4
JK
4203 /* We need to defer calling `solib_add', as adding new symbols
4204 resets breakpoints, which in turn deletes breakpoint locations,
4205 and hence may clear unprocessed entries in the BS chain. */
4206 int shlib_event = 0;
4207 int jit_event = 0;
0e30163f 4208 bpstat bs;
c906108c 4209
628fe4e4 4210 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4211 retval.call_dummy = STOP_NONE;
186c406b 4212 retval.is_longjmp = 0;
628fe4e4 4213
0e30163f 4214 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4215 {
628fe4e4
JK
4216 /* Extract this BS's action. After processing each BS, we check
4217 if its action overrides all we've seem so far. */
4218 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4219 enum bptype bptype;
4220
c906108c 4221 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4222 {
4223 /* I suspect this can happen if it was a momentary
4224 breakpoint which has since been deleted. */
4225 bptype = bp_none;
4226 }
20874c92 4227 else
f431efe5 4228 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4229
4230 switch (bptype)
c906108c
SS
4231 {
4232 case bp_none:
628fe4e4 4233 break;
c906108c
SS
4234 case bp_breakpoint:
4235 case bp_hardware_breakpoint:
4236 case bp_until:
4237 case bp_finish:
4238 if (bs->stop)
4239 {
4240 if (bs->print)
628fe4e4 4241 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4242 else
628fe4e4 4243 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4244 }
4245 else
628fe4e4 4246 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4247 break;
4248 case bp_watchpoint:
4249 case bp_hardware_watchpoint:
4250 case bp_read_watchpoint:
4251 case bp_access_watchpoint:
4252 if (bs->stop)
4253 {
4254 if (bs->print)
628fe4e4 4255 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4256 else
628fe4e4 4257 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4258 }
4259 else
628fe4e4
JK
4260 {
4261 /* There was a watchpoint, but we're not stopping.
4262 This requires no further action. */
4263 }
c906108c
SS
4264 break;
4265 case bp_longjmp:
186c406b 4266 case bp_exception:
628fe4e4 4267 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4268 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4269 break;
4270 case bp_longjmp_resume:
186c406b 4271 case bp_exception_resume:
628fe4e4 4272 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4273 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4274 break;
4275 case bp_step_resume:
4276 if (bs->stop)
628fe4e4
JK
4277 this_action = BPSTAT_WHAT_STEP_RESUME;
4278 else
c906108c 4279 {
628fe4e4
JK
4280 /* It is for the wrong frame. */
4281 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4282 }
c906108c 4283 break;
2c03e5be
PA
4284 case bp_hp_step_resume:
4285 if (bs->stop)
4286 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4287 else
4288 {
4289 /* It is for the wrong frame. */
4290 this_action = BPSTAT_WHAT_SINGLE;
4291 }
4292 break;
c906108c 4293 case bp_watchpoint_scope:
c4093a6a 4294 case bp_thread_event:
1900040c 4295 case bp_overlay_event:
0fd8e87f 4296 case bp_longjmp_master:
aa7d318d 4297 case bp_std_terminate_master:
186c406b 4298 case bp_exception_master:
628fe4e4 4299 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4300 break;
ce78b96d 4301 case bp_catchpoint:
c5aa993b
JM
4302 if (bs->stop)
4303 {
4304 if (bs->print)
628fe4e4 4305 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4306 else
628fe4e4 4307 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4308 }
4309 else
628fe4e4
JK
4310 {
4311 /* There was a catchpoint, but we're not stopping.
4312 This requires no further action. */
4313 }
4314 break;
4315 case bp_shlib_event:
4316 shlib_event = 1;
4317
4318 /* If requested, stop when the dynamic linker notifies GDB
4319 of events. This allows the user to get control and place
4320 breakpoints in initializer routines for dynamically
4321 loaded objects (among other things). */
4322 if (stop_on_solib_events)
4323 this_action = BPSTAT_WHAT_STOP_NOISY;
4324 else
4325 this_action = BPSTAT_WHAT_SINGLE;
4326 break;
4327 case bp_jit_event:
4328 jit_event = 1;
4329 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4330 break;
c906108c 4331 case bp_call_dummy:
53a5351d
JM
4332 /* Make sure the action is stop (silent or noisy),
4333 so infrun.c pops the dummy frame. */
aa7d318d 4334 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4335 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4336 break;
4337 case bp_std_terminate:
4338 /* Make sure the action is stop (silent or noisy),
4339 so infrun.c pops the dummy frame. */
aa7d318d 4340 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4341 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4342 break;
1042e4c0 4343 case bp_tracepoint:
7a697b8d 4344 case bp_fast_tracepoint:
0fb4aa4b 4345 case bp_static_tracepoint:
1042e4c0
SS
4346 /* Tracepoint hits should not be reported back to GDB, and
4347 if one got through somehow, it should have been filtered
4348 out already. */
4349 internal_error (__FILE__, __LINE__,
7a697b8d 4350 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4351 break;
4352 case bp_gnu_ifunc_resolver:
4353 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4354 this_action = BPSTAT_WHAT_SINGLE;
4355 break;
4356 case bp_gnu_ifunc_resolver_return:
4357 /* The breakpoint will be removed, execution will restart from the
4358 PC of the former breakpoint. */
4359 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4360 break;
628fe4e4
JK
4361 default:
4362 internal_error (__FILE__, __LINE__,
4363 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4364 }
628fe4e4
JK
4365
4366 retval.main_action = max (retval.main_action, this_action);
c906108c 4367 }
628fe4e4 4368
0e30163f
JK
4369 /* These operations may affect the bs->breakpoint_at state so they are
4370 delayed after MAIN_ACTION is decided above. */
4371
628fe4e4
JK
4372 if (shlib_event)
4373 {
4374 if (debug_infrun)
4375 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4376
4377 /* Check for any newly added shared libraries if we're supposed
4378 to be adding them automatically. */
4379
4380 /* Switch terminal for any messages produced by
4381 breakpoint_re_set. */
4382 target_terminal_ours_for_output ();
4383
4384#ifdef SOLIB_ADD
4385 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4386#else
4387 solib_add (NULL, 0, &current_target, auto_solib_add);
4388#endif
4389
4390 target_terminal_inferior ();
4391 }
4392
4393 if (jit_event)
4394 {
4395 if (debug_infrun)
4396 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4397
4398 handle_jit_event ();
4399 }
4400
0e30163f
JK
4401 for (bs = bs_head; bs != NULL; bs = bs->next)
4402 {
4403 struct breakpoint *b = bs->breakpoint_at;
4404
4405 if (b == NULL)
4406 continue;
4407 switch (b->type)
4408 {
4409 case bp_gnu_ifunc_resolver:
4410 gnu_ifunc_resolver_stop (b);
4411 break;
4412 case bp_gnu_ifunc_resolver_return:
4413 gnu_ifunc_resolver_return_stop (b);
4414 break;
4415 }
4416 }
4417
c906108c
SS
4418 return retval;
4419}
4420
4421/* Nonzero if we should step constantly (e.g. watchpoints on machines
4422 without hardware support). This isn't related to a specific bpstat,
4423 just to things like whether watchpoints are set. */
4424
c5aa993b 4425int
fba45db2 4426bpstat_should_step (void)
c906108c
SS
4427{
4428 struct breakpoint *b;
cc59ec59 4429
c906108c 4430 ALL_BREAKPOINTS (b)
717a8278 4431 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4432 return 1;
c906108c
SS
4433 return 0;
4434}
4435
67822962
PA
4436int
4437bpstat_causes_stop (bpstat bs)
4438{
4439 for (; bs != NULL; bs = bs->next)
4440 if (bs->stop)
4441 return 1;
4442
4443 return 0;
4444}
4445
c906108c 4446\f
c5aa993b 4447
170b53b2
UW
4448/* Compute a string of spaces suitable to indent the next line
4449 so it starts at the position corresponding to the table column
4450 named COL_NAME in the currently active table of UIOUT. */
4451
4452static char *
4453wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4454{
4455 static char wrap_indent[80];
4456 int i, total_width, width, align;
4457 char *text;
4458
4459 total_width = 0;
4460 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4461 {
4462 if (strcmp (text, col_name) == 0)
4463 {
4464 gdb_assert (total_width < sizeof wrap_indent);
4465 memset (wrap_indent, ' ', total_width);
4466 wrap_indent[total_width] = 0;
4467
4468 return wrap_indent;
4469 }
4470
4471 total_width += width + 1;
4472 }
4473
4474 return NULL;
4475}
4476
859825b8
JK
4477/* Print the LOC location out of the list of B->LOC locations. */
4478
170b53b2
UW
4479static void
4480print_breakpoint_location (struct breakpoint *b,
4481 struct bp_location *loc)
0d381245 4482{
6c95b8df
PA
4483 struct cleanup *old_chain = save_current_program_space ();
4484
859825b8
JK
4485 if (loc != NULL && loc->shlib_disabled)
4486 loc = NULL;
4487
6c95b8df
PA
4488 if (loc != NULL)
4489 set_current_program_space (loc->pspace);
4490
56435ebe
TT
4491 if (b->display_canonical)
4492 ui_out_field_string (uiout, "what", b->addr_string);
4493 else if (b->source_file && loc)
0d381245
VP
4494 {
4495 struct symbol *sym
4496 = find_pc_sect_function (loc->address, loc->section);
4497 if (sym)
4498 {
4499 ui_out_text (uiout, "in ");
4500 ui_out_field_string (uiout, "func",
4501 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4502 ui_out_text (uiout, " ");
4503 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4504 ui_out_text (uiout, "at ");
0d381245
VP
4505 }
4506 ui_out_field_string (uiout, "file", b->source_file);
4507 ui_out_text (uiout, ":");
4508
4509 if (ui_out_is_mi_like_p (uiout))
4510 {
4511 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4512 char *fullname = symtab_to_fullname (sal.symtab);
4513
4514 if (fullname)
4515 ui_out_field_string (uiout, "fullname", fullname);
4516 }
4517
4518 ui_out_field_int (uiout, "line", b->line_number);
4519 }
859825b8 4520 else if (loc)
0d381245 4521 {
170b53b2
UW
4522 struct ui_stream *stb = ui_out_stream_new (uiout);
4523 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4524
22e722e1
DJ
4525 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4526 demangle, "");
0d381245 4527 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4528
4529 do_cleanups (stb_chain);
0d381245 4530 }
859825b8
JK
4531 else
4532 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4533
4534 do_cleanups (old_chain);
0d381245
VP
4535}
4536
269b11a2
PA
4537static const char *
4538bptype_string (enum bptype type)
c906108c 4539{
c4093a6a
JM
4540 struct ep_type_description
4541 {
4542 enum bptype type;
4543 char *description;
4544 };
4545 static struct ep_type_description bptypes[] =
c906108c 4546 {
c5aa993b
JM
4547 {bp_none, "?deleted?"},
4548 {bp_breakpoint, "breakpoint"},
c906108c 4549 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4550 {bp_until, "until"},
4551 {bp_finish, "finish"},
4552 {bp_watchpoint, "watchpoint"},
c906108c 4553 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4554 {bp_read_watchpoint, "read watchpoint"},
4555 {bp_access_watchpoint, "acc watchpoint"},
4556 {bp_longjmp, "longjmp"},
4557 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4558 {bp_exception, "exception"},
4559 {bp_exception_resume, "exception resume"},
c5aa993b 4560 {bp_step_resume, "step resume"},
2c03e5be 4561 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4562 {bp_watchpoint_scope, "watchpoint scope"},
4563 {bp_call_dummy, "call dummy"},
aa7d318d 4564 {bp_std_terminate, "std::terminate"},
c5aa993b 4565 {bp_shlib_event, "shlib events"},
c4093a6a 4566 {bp_thread_event, "thread events"},
1900040c 4567 {bp_overlay_event, "overlay events"},
0fd8e87f 4568 {bp_longjmp_master, "longjmp master"},
aa7d318d 4569 {bp_std_terminate_master, "std::terminate master"},
186c406b 4570 {bp_exception_master, "exception master"},
ce78b96d 4571 {bp_catchpoint, "catchpoint"},
1042e4c0 4572 {bp_tracepoint, "tracepoint"},
7a697b8d 4573 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4574 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4575 {bp_jit_event, "jit events"},
0e30163f
JK
4576 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4577 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4578 };
269b11a2
PA
4579
4580 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4581 || ((int) type != bptypes[(int) type].type))
4582 internal_error (__FILE__, __LINE__,
4583 _("bptypes table does not describe type #%d."),
4584 (int) type);
4585
4586 return bptypes[(int) type].description;
4587}
4588
4589/* Print B to gdb_stdout. */
4590
4591static void
4592print_one_breakpoint_location (struct breakpoint *b,
4593 struct bp_location *loc,
4594 int loc_number,
4595 struct bp_location **last_loc,
269b11a2
PA
4596 int allflag)
4597{
4598 struct command_line *l;
c2c6d25f 4599 static char bpenables[] = "nynny";
c906108c 4600
0d381245
VP
4601 int header_of_multiple = 0;
4602 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4603 struct value_print_options opts;
4604
4605 get_user_print_options (&opts);
0d381245
VP
4606
4607 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4608 /* See comment in print_one_breakpoint concerning treatment of
4609 breakpoints with single disabled location. */
0d381245
VP
4610 if (loc == NULL
4611 && (b->loc != NULL
4612 && (b->loc->next != NULL || !b->loc->enabled)))
4613 header_of_multiple = 1;
4614 if (loc == NULL)
4615 loc = b->loc;
4616
c4093a6a
JM
4617 annotate_record ();
4618
4619 /* 1 */
4620 annotate_field (0);
0d381245
VP
4621 if (part_of_multiple)
4622 {
4623 char *formatted;
0c6773c1 4624 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4625 ui_out_field_string (uiout, "number", formatted);
4626 xfree (formatted);
4627 }
4628 else
4629 {
4630 ui_out_field_int (uiout, "number", b->number);
4631 }
c4093a6a
JM
4632
4633 /* 2 */
4634 annotate_field (1);
0d381245
VP
4635 if (part_of_multiple)
4636 ui_out_field_skip (uiout, "type");
269b11a2
PA
4637 else
4638 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4639
4640 /* 3 */
4641 annotate_field (2);
0d381245
VP
4642 if (part_of_multiple)
4643 ui_out_field_skip (uiout, "disp");
4644 else
2cec12e5 4645 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4646
c4093a6a
JM
4647
4648 /* 4 */
4649 annotate_field (3);
0d381245 4650 if (part_of_multiple)
54e52265 4651 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4652 else
4a64f543
MS
4653 ui_out_field_fmt (uiout, "enabled", "%c",
4654 bpenables[(int) b->enable_state]);
54e52265 4655 ui_out_spaces (uiout, 2);
0d381245 4656
c4093a6a
JM
4657
4658 /* 5 and 6 */
3086aeae 4659 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4660 {
4a64f543
MS
4661 /* Although the print_one can possibly print all locations,
4662 calling it here is not likely to get any nice result. So,
4663 make sure there's just one location. */
0d381245 4664 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4665 b->ops->print_one (b, last_loc);
0d381245 4666 }
3086aeae
DJ
4667 else
4668 switch (b->type)
4669 {
4670 case bp_none:
4671 internal_error (__FILE__, __LINE__,
e2e0b3e5 4672 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4673 break;
c906108c 4674
3086aeae
DJ
4675 case bp_watchpoint:
4676 case bp_hardware_watchpoint:
4677 case bp_read_watchpoint:
4678 case bp_access_watchpoint:
3a5c3e22
PA
4679 {
4680 struct watchpoint *w = (struct watchpoint *) b;
4681
4682 /* Field 4, the address, is omitted (which makes the columns
4683 not line up too nicely with the headers, but the effect
4684 is relatively readable). */
4685 if (opts.addressprint)
4686 ui_out_field_skip (uiout, "addr");
4687 annotate_field (5);
4688 ui_out_field_string (uiout, "what", w->exp_string);
4689 }
3086aeae
DJ
4690 break;
4691
3086aeae
DJ
4692 case bp_breakpoint:
4693 case bp_hardware_breakpoint:
4694 case bp_until:
4695 case bp_finish:
4696 case bp_longjmp:
4697 case bp_longjmp_resume:
186c406b
TT
4698 case bp_exception:
4699 case bp_exception_resume:
3086aeae 4700 case bp_step_resume:
2c03e5be 4701 case bp_hp_step_resume:
3086aeae
DJ
4702 case bp_watchpoint_scope:
4703 case bp_call_dummy:
aa7d318d 4704 case bp_std_terminate:
3086aeae
DJ
4705 case bp_shlib_event:
4706 case bp_thread_event:
4707 case bp_overlay_event:
0fd8e87f 4708 case bp_longjmp_master:
aa7d318d 4709 case bp_std_terminate_master:
186c406b 4710 case bp_exception_master:
1042e4c0 4711 case bp_tracepoint:
7a697b8d 4712 case bp_fast_tracepoint:
0fb4aa4b 4713 case bp_static_tracepoint:
4efc6507 4714 case bp_jit_event:
0e30163f
JK
4715 case bp_gnu_ifunc_resolver:
4716 case bp_gnu_ifunc_resolver_return:
79a45b7d 4717 if (opts.addressprint)
3086aeae
DJ
4718 {
4719 annotate_field (4);
54e52265 4720 if (header_of_multiple)
0d381245 4721 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4722 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4723 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4724 else
5af949e3
UW
4725 ui_out_field_core_addr (uiout, "addr",
4726 loc->gdbarch, loc->address);
3086aeae
DJ
4727 }
4728 annotate_field (5);
0d381245 4729 if (!header_of_multiple)
170b53b2 4730 print_breakpoint_location (b, loc);
0d381245 4731 if (b->loc)
a6d9a66e 4732 *last_loc = b->loc;
3086aeae
DJ
4733 break;
4734 }
c906108c 4735
6c95b8df
PA
4736
4737 /* For backward compatibility, don't display inferiors unless there
4738 are several. */
4739 if (loc != NULL
4740 && !header_of_multiple
4741 && (allflag
4742 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4743 && (number_of_program_spaces () > 1
4744 || number_of_inferiors () > 1)
4a64f543
MS
4745 /* LOC is for existing B, it cannot be in
4746 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4747 && loc->owner->type != bp_catchpoint)))
4748 {
4749 struct inferior *inf;
4750 int first = 1;
4751
4752 for (inf = inferior_list; inf != NULL; inf = inf->next)
4753 {
4754 if (inf->pspace == loc->pspace)
4755 {
4756 if (first)
4757 {
4758 first = 0;
4759 ui_out_text (uiout, " inf ");
4760 }
4761 else
4762 ui_out_text (uiout, ", ");
4763 ui_out_text (uiout, plongest (inf->num));
4764 }
4765 }
4766 }
4767
4a306c9a 4768 if (!part_of_multiple)
c4093a6a 4769 {
4a306c9a
JB
4770 if (b->thread != -1)
4771 {
4772 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4773 "stop only in" line a little further down. */
4a306c9a
JB
4774 ui_out_text (uiout, " thread ");
4775 ui_out_field_int (uiout, "thread", b->thread);
4776 }
4777 else if (b->task != 0)
4778 {
4779 ui_out_text (uiout, " task ");
4780 ui_out_field_int (uiout, "task", b->task);
4781 }
c4093a6a 4782 }
f1310107 4783
8b93c638 4784 ui_out_text (uiout, "\n");
f1310107 4785
348d480f 4786 if (!part_of_multiple)
f1310107
TJB
4787 b->ops->print_one_detail (b, uiout);
4788
0d381245 4789 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4790 {
4791 annotate_field (6);
8b93c638 4792 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4793 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4794 the frame ID. */
5af949e3
UW
4795 ui_out_field_core_addr (uiout, "frame",
4796 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4797 ui_out_text (uiout, "\n");
c4093a6a
JM
4798 }
4799
28010a5d 4800 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4801 {
4802 annotate_field (7);
d77f58be 4803 if (is_tracepoint (b))
1042e4c0
SS
4804 ui_out_text (uiout, "\ttrace only if ");
4805 else
4806 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4807 ui_out_field_string (uiout, "cond", b->cond_string);
4808 ui_out_text (uiout, "\n");
4809 }
4810
0d381245 4811 if (!part_of_multiple && b->thread != -1)
c4093a6a 4812 {
4a64f543 4813 /* FIXME should make an annotation for this. */
8b93c638
JM
4814 ui_out_text (uiout, "\tstop only in thread ");
4815 ui_out_field_int (uiout, "thread", b->thread);
4816 ui_out_text (uiout, "\n");
c4093a6a
JM
4817 }
4818
63c715c6 4819 if (!part_of_multiple && b->hit_count)
c4093a6a 4820 {
4a64f543 4821 /* FIXME should make an annotation for this. */
8b93c638
JM
4822 if (ep_is_catchpoint (b))
4823 ui_out_text (uiout, "\tcatchpoint");
4824 else
4825 ui_out_text (uiout, "\tbreakpoint");
4826 ui_out_text (uiout, " already hit ");
4827 ui_out_field_int (uiout, "times", b->hit_count);
4828 if (b->hit_count == 1)
4829 ui_out_text (uiout, " time\n");
4830 else
4831 ui_out_text (uiout, " times\n");
c4093a6a
JM
4832 }
4833
4a64f543
MS
4834 /* Output the count also if it is zero, but only if this is mi.
4835 FIXME: Should have a better test for this. */
9dc5e2a9 4836 if (ui_out_is_mi_like_p (uiout))
63c715c6 4837 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4838 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4839
0d381245 4840 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4841 {
4842 annotate_field (8);
8b93c638
JM
4843 ui_out_text (uiout, "\tignore next ");
4844 ui_out_field_int (uiout, "ignore", b->ignore_count);
4845 ui_out_text (uiout, " hits\n");
c4093a6a 4846 }
059fb39f 4847
9add0f1b 4848 l = b->commands ? b->commands->commands : NULL;
059fb39f 4849 if (!part_of_multiple && l)
c4093a6a 4850 {
3b31d625
EZ
4851 struct cleanup *script_chain;
4852
c4093a6a 4853 annotate_field (9);
3b31d625 4854 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4855 print_command_lines (uiout, l, 4);
3b31d625 4856 do_cleanups (script_chain);
c4093a6a 4857 }
d24317b4 4858
d9b3f62e 4859 if (is_tracepoint (b))
1042e4c0 4860 {
d9b3f62e
PA
4861 struct tracepoint *t = (struct tracepoint *) b;
4862
4863 if (!part_of_multiple && t->pass_count)
4864 {
4865 annotate_field (10);
4866 ui_out_text (uiout, "\tpass count ");
4867 ui_out_field_int (uiout, "pass", t->pass_count);
4868 ui_out_text (uiout, " \n");
4869 }
1042e4c0
SS
4870 }
4871
d24317b4
VP
4872 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4873 {
3a5c3e22
PA
4874 if (is_watchpoint (b))
4875 {
4876 struct watchpoint *w = (struct watchpoint *) b;
4877
4878 ui_out_field_string (uiout, "original-location", w->exp_string);
4879 }
4880 else if (b->addr_string)
d24317b4 4881 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4882 }
c4093a6a 4883}
c5aa993b 4884
0d381245
VP
4885static void
4886print_one_breakpoint (struct breakpoint *b,
4a64f543 4887 struct bp_location **last_loc,
6c95b8df 4888 int allflag)
0d381245 4889{
8d3788bd
VP
4890 struct cleanup *bkpt_chain;
4891
4892 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4893
12c5a436 4894 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4895 do_cleanups (bkpt_chain);
0d381245
VP
4896
4897 /* If this breakpoint has custom print function,
4898 it's already printed. Otherwise, print individual
4899 locations, if any. */
4900 if (b->ops == NULL || b->ops->print_one == NULL)
4901 {
4a64f543
MS
4902 /* If breakpoint has a single location that is disabled, we
4903 print it as if it had several locations, since otherwise it's
4904 hard to represent "breakpoint enabled, location disabled"
4905 situation.
4906
4907 Note that while hardware watchpoints have several locations
a3be7890 4908 internally, that's not a property exposed to user. */
0d381245 4909 if (b->loc
a5606eee 4910 && !is_hardware_watchpoint (b)
8d3788bd 4911 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4912 {
4913 struct bp_location *loc;
4914 int n = 1;
8d3788bd 4915
0d381245 4916 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
4917 {
4918 struct cleanup *inner2 =
4919 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4920 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4921 do_cleanups (inner2);
4922 }
0d381245
VP
4923 }
4924 }
4925}
4926
a6d9a66e
UW
4927static int
4928breakpoint_address_bits (struct breakpoint *b)
4929{
4930 int print_address_bits = 0;
4931 struct bp_location *loc;
4932
4933 for (loc = b->loc; loc; loc = loc->next)
4934 {
c7437ca6
PA
4935 int addr_bit;
4936
4937 /* Software watchpoints that aren't watching memory don't have
4938 an address to print. */
4939 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4940 continue;
4941
4942 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
4943 if (addr_bit > print_address_bits)
4944 print_address_bits = addr_bit;
4945 }
4946
4947 return print_address_bits;
4948}
0d381245 4949
c4093a6a
JM
4950struct captured_breakpoint_query_args
4951 {
4952 int bnum;
4953 };
c5aa993b 4954
c4093a6a 4955static int
2b65245e 4956do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
4957{
4958 struct captured_breakpoint_query_args *args = data;
52f0bd74 4959 struct breakpoint *b;
a6d9a66e 4960 struct bp_location *dummy_loc = NULL;
cc59ec59 4961
c4093a6a
JM
4962 ALL_BREAKPOINTS (b)
4963 {
4964 if (args->bnum == b->number)
c5aa993b 4965 {
12c5a436 4966 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 4967 return GDB_RC_OK;
c5aa993b 4968 }
c4093a6a
JM
4969 }
4970 return GDB_RC_NONE;
4971}
c5aa993b 4972
c4093a6a 4973enum gdb_rc
4a64f543
MS
4974gdb_breakpoint_query (struct ui_out *uiout, int bnum,
4975 char **error_message)
c4093a6a
JM
4976{
4977 struct captured_breakpoint_query_args args;
cc59ec59 4978
c4093a6a
JM
4979 args.bnum = bnum;
4980 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 4981 an error. */
b0b13bb4
DJ
4982 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4983 error_message, RETURN_MASK_ALL) < 0)
4984 return GDB_RC_FAIL;
4985 else
4986 return GDB_RC_OK;
c4093a6a 4987}
c5aa993b 4988
09d682a4
TT
4989/* Return true if this breakpoint was set by the user, false if it is
4990 internal or momentary. */
4991
4992int
4993user_breakpoint_p (struct breakpoint *b)
4994{
46c6471b 4995 return b->number > 0;
09d682a4
TT
4996}
4997
7f3b0473 4998/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
4999 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5000 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5001 FILTER is non-NULL, call it on each breakpoint and only include the
5002 ones for which it returns non-zero. Return the total number of
5003 breakpoints listed. */
c906108c 5004
d77f58be 5005static int
e5a67952 5006breakpoint_1 (char *args, int allflag,
4a64f543 5007 int (*filter) (const struct breakpoint *))
c4093a6a 5008{
52f0bd74 5009 struct breakpoint *b;
a6d9a66e 5010 struct bp_location *last_loc = NULL;
7f3b0473 5011 int nr_printable_breakpoints;
3b31d625 5012 struct cleanup *bkpttbl_chain;
79a45b7d 5013 struct value_print_options opts;
a6d9a66e 5014 int print_address_bits = 0;
269b11a2
PA
5015 int print_type_col_width = 14;
5016
79a45b7d
TT
5017 get_user_print_options (&opts);
5018
4a64f543
MS
5019 /* Compute the number of rows in the table, as well as the size
5020 required for address fields. */
7f3b0473
AC
5021 nr_printable_breakpoints = 0;
5022 ALL_BREAKPOINTS (b)
e5a67952
MS
5023 {
5024 /* If we have a filter, only list the breakpoints it accepts. */
5025 if (filter && !filter (b))
5026 continue;
5027
5028 /* If we have an "args" string, it is a list of breakpoints to
5029 accept. Skip the others. */
5030 if (args != NULL && *args != '\0')
5031 {
5032 if (allflag && parse_and_eval_long (args) != b->number)
5033 continue;
5034 if (!allflag && !number_is_in_list (args, b->number))
5035 continue;
5036 }
269b11a2 5037
e5a67952
MS
5038 if (allflag || user_breakpoint_p (b))
5039 {
5040 int addr_bit, type_len;
a6d9a66e 5041
e5a67952
MS
5042 addr_bit = breakpoint_address_bits (b);
5043 if (addr_bit > print_address_bits)
5044 print_address_bits = addr_bit;
269b11a2 5045
e5a67952
MS
5046 type_len = strlen (bptype_string (b->type));
5047 if (type_len > print_type_col_width)
5048 print_type_col_width = type_len;
5049
5050 nr_printable_breakpoints++;
5051 }
5052 }
7f3b0473 5053
79a45b7d 5054 if (opts.addressprint)
3b31d625 5055 bkpttbl_chain
3e43a32a
MS
5056 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5057 nr_printable_breakpoints,
3b31d625 5058 "BreakpointTable");
8b93c638 5059 else
3b31d625 5060 bkpttbl_chain
3e43a32a
MS
5061 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5062 nr_printable_breakpoints,
3b31d625 5063 "BreakpointTable");
8b93c638 5064
7f3b0473 5065 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5066 annotate_breakpoints_headers ();
5067 if (nr_printable_breakpoints > 0)
5068 annotate_field (0);
4a64f543 5069 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5070 if (nr_printable_breakpoints > 0)
5071 annotate_field (1);
269b11a2 5072 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5073 "type", "Type"); /* 2 */
d7faa9e7
AC
5074 if (nr_printable_breakpoints > 0)
5075 annotate_field (2);
4a64f543 5076 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5077 if (nr_printable_breakpoints > 0)
5078 annotate_field (3);
54e52265 5079 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5080 if (opts.addressprint)
e5a67952
MS
5081 {
5082 if (nr_printable_breakpoints > 0)
5083 annotate_field (4);
5084 if (print_address_bits <= 32)
5085 ui_out_table_header (uiout, 10, ui_left,
5086 "addr", "Address"); /* 5 */
5087 else
5088 ui_out_table_header (uiout, 18, ui_left,
5089 "addr", "Address"); /* 5 */
5090 }
d7faa9e7
AC
5091 if (nr_printable_breakpoints > 0)
5092 annotate_field (5);
5093 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5094 ui_out_table_body (uiout);
5095 if (nr_printable_breakpoints > 0)
5096 annotate_breakpoints_table ();
7f3b0473 5097
c4093a6a 5098 ALL_BREAKPOINTS (b)
e5a67952
MS
5099 {
5100 QUIT;
5101 /* If we have a filter, only list the breakpoints it accepts. */
5102 if (filter && !filter (b))
5103 continue;
5104
5105 /* If we have an "args" string, it is a list of breakpoints to
5106 accept. Skip the others. */
5107
5108 if (args != NULL && *args != '\0')
5109 {
5110 if (allflag) /* maintenance info breakpoint */
5111 {
5112 if (parse_and_eval_long (args) != b->number)
5113 continue;
5114 }
5115 else /* all others */
5116 {
5117 if (!number_is_in_list (args, b->number))
5118 continue;
5119 }
5120 }
5121 /* We only print out user settable breakpoints unless the
5122 allflag is set. */
5123 if (allflag || user_breakpoint_p (b))
12c5a436 5124 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5125 }
5126
3b31d625 5127 do_cleanups (bkpttbl_chain);
698384cd 5128
7f3b0473 5129 if (nr_printable_breakpoints == 0)
c906108c 5130 {
4a64f543
MS
5131 /* If there's a filter, let the caller decide how to report
5132 empty list. */
d77f58be
SS
5133 if (!filter)
5134 {
e5a67952 5135 if (args == NULL || *args == '\0')
d77f58be
SS
5136 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5137 else
4a64f543 5138 ui_out_message (uiout, 0,
e5a67952
MS
5139 "No breakpoint or watchpoint matching '%s'.\n",
5140 args);
d77f58be 5141 }
c906108c
SS
5142 }
5143 else
c4093a6a 5144 {
a6d9a66e
UW
5145 if (last_loc && !server_command)
5146 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5147 }
c906108c 5148
4a64f543 5149 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5150 there have been breakpoints? */
c906108c 5151 annotate_breakpoints_table_end ();
d77f58be
SS
5152
5153 return nr_printable_breakpoints;
c906108c
SS
5154}
5155
ad443146
SS
5156/* Display the value of default-collect in a way that is generally
5157 compatible with the breakpoint list. */
5158
5159static void
5160default_collect_info (void)
5161{
5162 /* If it has no value (which is frequently the case), say nothing; a
5163 message like "No default-collect." gets in user's face when it's
5164 not wanted. */
5165 if (!*default_collect)
5166 return;
5167
5168 /* The following phrase lines up nicely with per-tracepoint collect
5169 actions. */
5170 ui_out_text (uiout, "default collect ");
5171 ui_out_field_string (uiout, "default-collect", default_collect);
5172 ui_out_text (uiout, " \n");
5173}
5174
c906108c 5175static void
e5a67952 5176breakpoints_info (char *args, int from_tty)
c906108c 5177{
e5a67952 5178 breakpoint_1 (args, 0, NULL);
ad443146
SS
5179
5180 default_collect_info ();
d77f58be
SS
5181}
5182
5183static void
e5a67952 5184watchpoints_info (char *args, int from_tty)
d77f58be 5185{
e5a67952 5186 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5187
5188 if (num_printed == 0)
5189 {
e5a67952 5190 if (args == NULL || *args == '\0')
d77f58be
SS
5191 ui_out_message (uiout, 0, "No watchpoints.\n");
5192 else
e5a67952 5193 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5194 }
c906108c
SS
5195}
5196
7a292a7a 5197static void
e5a67952 5198maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5199{
e5a67952 5200 breakpoint_1 (args, 1, NULL);
ad443146
SS
5201
5202 default_collect_info ();
c906108c
SS
5203}
5204
0d381245 5205static int
714835d5 5206breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5207 struct program_space *pspace,
714835d5 5208 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5209{
5210 struct bp_location *bl = b->loc;
cc59ec59 5211
0d381245
VP
5212 for (; bl; bl = bl->next)
5213 {
6c95b8df
PA
5214 if (bl->pspace == pspace
5215 && bl->address == pc
0d381245
VP
5216 && (!overlay_debugging || bl->section == section))
5217 return 1;
5218 }
5219 return 0;
5220}
5221
6c95b8df
PA
5222/* Print a message describing any breakpoints set at PC. This
5223 concerns with logical breakpoints, so we match program spaces, not
5224 address spaces. */
c906108c
SS
5225
5226static void
6c95b8df
PA
5227describe_other_breakpoints (struct gdbarch *gdbarch,
5228 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5229 struct obj_section *section, int thread)
c906108c 5230{
52f0bd74
AC
5231 int others = 0;
5232 struct breakpoint *b;
c906108c
SS
5233
5234 ALL_BREAKPOINTS (b)
6c95b8df 5235 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5236 if (others > 0)
5237 {
a3f17187
AC
5238 if (others == 1)
5239 printf_filtered (_("Note: breakpoint "));
5240 else /* if (others == ???) */
5241 printf_filtered (_("Note: breakpoints "));
c906108c 5242 ALL_BREAKPOINTS (b)
6c95b8df 5243 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5244 {
5245 others--;
5246 printf_filtered ("%d", b->number);
5247 if (b->thread == -1 && thread != -1)
5248 printf_filtered (" (all threads)");
5249 else if (b->thread != -1)
5250 printf_filtered (" (thread %d)", b->thread);
5251 printf_filtered ("%s%s ",
059fb39f 5252 ((b->enable_state == bp_disabled
8bea4e01
UW
5253 || b->enable_state == bp_call_disabled
5254 || b->enable_state == bp_startup_disabled)
0d381245
VP
5255 ? " (disabled)"
5256 : b->enable_state == bp_permanent
5257 ? " (permanent)"
5258 : ""),
5259 (others > 1) ? ","
5260 : ((others == 1) ? " and" : ""));
5261 }
a3f17187 5262 printf_filtered (_("also set at pc "));
5af949e3 5263 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5264 printf_filtered (".\n");
5265 }
5266}
5267\f
5268/* Set the default place to put a breakpoint
5269 for the `break' command with no arguments. */
5270
5271void
6c95b8df
PA
5272set_default_breakpoint (int valid, struct program_space *pspace,
5273 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5274 int line)
c906108c
SS
5275{
5276 default_breakpoint_valid = valid;
6c95b8df 5277 default_breakpoint_pspace = pspace;
c906108c
SS
5278 default_breakpoint_address = addr;
5279 default_breakpoint_symtab = symtab;
5280 default_breakpoint_line = line;
5281}
5282
e4f237da
KB
5283/* Return true iff it is meaningful to use the address member of
5284 BPT. For some breakpoint types, the address member is irrelevant
5285 and it makes no sense to attempt to compare it to other addresses
5286 (or use it for any other purpose either).
5287
4a64f543
MS
5288 More specifically, each of the following breakpoint types will
5289 always have a zero valued address and we don't want to mark
5290 breakpoints of any of these types to be a duplicate of an actual
5291 breakpoint at address zero:
e4f237da
KB
5292
5293 bp_watchpoint
2d134ed3
PA
5294 bp_catchpoint
5295
5296*/
e4f237da
KB
5297
5298static int
5299breakpoint_address_is_meaningful (struct breakpoint *bpt)
5300{
5301 enum bptype type = bpt->type;
5302
2d134ed3
PA
5303 return (type != bp_watchpoint && type != bp_catchpoint);
5304}
5305
5306/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5307 true if LOC1 and LOC2 represent the same watchpoint location. */
5308
5309static int
4a64f543
MS
5310watchpoint_locations_match (struct bp_location *loc1,
5311 struct bp_location *loc2)
2d134ed3 5312{
3a5c3e22
PA
5313 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5314 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5315
5316 /* Both of them must exist. */
5317 gdb_assert (w1 != NULL);
5318 gdb_assert (w2 != NULL);
2bdf28a0 5319
4a64f543
MS
5320 /* If the target can evaluate the condition expression in hardware,
5321 then we we need to insert both watchpoints even if they are at
5322 the same place. Otherwise the watchpoint will only trigger when
5323 the condition of whichever watchpoint was inserted evaluates to
5324 true, not giving a chance for GDB to check the condition of the
5325 other watchpoint. */
3a5c3e22 5326 if ((w1->cond_exp
4a64f543
MS
5327 && target_can_accel_watchpoint_condition (loc1->address,
5328 loc1->length,
0cf6dd15 5329 loc1->watchpoint_type,
3a5c3e22
PA
5330 w1->cond_exp))
5331 || (w2->cond_exp
4a64f543
MS
5332 && target_can_accel_watchpoint_condition (loc2->address,
5333 loc2->length,
0cf6dd15 5334 loc2->watchpoint_type,
3a5c3e22 5335 w2->cond_exp)))
0cf6dd15
TJB
5336 return 0;
5337
85d721b8
PA
5338 /* Note that this checks the owner's type, not the location's. In
5339 case the target does not support read watchpoints, but does
5340 support access watchpoints, we'll have bp_read_watchpoint
5341 watchpoints with hw_access locations. Those should be considered
5342 duplicates of hw_read locations. The hw_read locations will
5343 become hw_access locations later. */
2d134ed3
PA
5344 return (loc1->owner->type == loc2->owner->type
5345 && loc1->pspace->aspace == loc2->pspace->aspace
5346 && loc1->address == loc2->address
5347 && loc1->length == loc2->length);
e4f237da
KB
5348}
5349
6c95b8df
PA
5350/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5351 same breakpoint location. In most targets, this can only be true
5352 if ASPACE1 matches ASPACE2. On targets that have global
5353 breakpoints, the address space doesn't really matter. */
5354
5355static int
5356breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5357 struct address_space *aspace2, CORE_ADDR addr2)
5358{
5359 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5360 || aspace1 == aspace2)
5361 && addr1 == addr2);
5362}
5363
f1310107
TJB
5364/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5365 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5366 matches ASPACE2. On targets that have global breakpoints, the address
5367 space doesn't really matter. */
5368
5369static int
5370breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5371 int len1, struct address_space *aspace2,
5372 CORE_ADDR addr2)
5373{
5374 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5375 || aspace1 == aspace2)
5376 && addr2 >= addr1 && addr2 < addr1 + len1);
5377}
5378
5379/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5380 a ranged breakpoint. In most targets, a match happens only if ASPACE
5381 matches the breakpoint's address space. On targets that have global
5382 breakpoints, the address space doesn't really matter. */
5383
5384static int
5385breakpoint_location_address_match (struct bp_location *bl,
5386 struct address_space *aspace,
5387 CORE_ADDR addr)
5388{
5389 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5390 aspace, addr)
5391 || (bl->length
5392 && breakpoint_address_match_range (bl->pspace->aspace,
5393 bl->address, bl->length,
5394 aspace, addr)));
5395}
5396
2d134ed3
PA
5397/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5398 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5399 represent the same location. */
5400
5401static int
4a64f543
MS
5402breakpoint_locations_match (struct bp_location *loc1,
5403 struct bp_location *loc2)
2d134ed3 5404{
2bdf28a0
JK
5405 int hw_point1, hw_point2;
5406
5407 /* Both of them must not be in moribund_locations. */
5408 gdb_assert (loc1->owner != NULL);
5409 gdb_assert (loc2->owner != NULL);
5410
5411 hw_point1 = is_hardware_watchpoint (loc1->owner);
5412 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5413
5414 if (hw_point1 != hw_point2)
5415 return 0;
5416 else if (hw_point1)
5417 return watchpoint_locations_match (loc1, loc2);
5418 else
f1310107
TJB
5419 /* We compare bp_location.length in order to cover ranged breakpoints. */
5420 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5421 loc2->pspace->aspace, loc2->address)
5422 && loc1->length == loc2->length);
2d134ed3
PA
5423}
5424
76897487
KB
5425static void
5426breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5427 int bnum, int have_bnum)
5428{
f63fbe86
MS
5429 /* The longest string possibly returned by hex_string_custom
5430 is 50 chars. These must be at least that big for safety. */
5431 char astr1[64];
5432 char astr2[64];
76897487 5433
bb599908
PH
5434 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5435 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5436 if (have_bnum)
8a3fe4f8 5437 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5438 bnum, astr1, astr2);
5439 else
8a3fe4f8 5440 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5441}
5442
4a64f543
MS
5443/* Adjust a breakpoint's address to account for architectural
5444 constraints on breakpoint placement. Return the adjusted address.
5445 Note: Very few targets require this kind of adjustment. For most
5446 targets, this function is simply the identity function. */
76897487
KB
5447
5448static CORE_ADDR
a6d9a66e
UW
5449adjust_breakpoint_address (struct gdbarch *gdbarch,
5450 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5451{
a6d9a66e 5452 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5453 {
5454 /* Very few targets need any kind of breakpoint adjustment. */
5455 return bpaddr;
5456 }
88f7da05
KB
5457 else if (bptype == bp_watchpoint
5458 || bptype == bp_hardware_watchpoint
5459 || bptype == bp_read_watchpoint
5460 || bptype == bp_access_watchpoint
fe798b75 5461 || bptype == bp_catchpoint)
88f7da05
KB
5462 {
5463 /* Watchpoints and the various bp_catch_* eventpoints should not
5464 have their addresses modified. */
5465 return bpaddr;
5466 }
76897487
KB
5467 else
5468 {
5469 CORE_ADDR adjusted_bpaddr;
5470
5471 /* Some targets have architectural constraints on the placement
5472 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5473 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5474
5475 /* An adjusted breakpoint address can significantly alter
5476 a user's expectations. Print a warning if an adjustment
5477 is required. */
5478 if (adjusted_bpaddr != bpaddr)
5479 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5480
5481 return adjusted_bpaddr;
5482 }
5483}
5484
28010a5d
PA
5485void
5486init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5487 struct breakpoint *owner)
7cc221ef 5488{
7cc221ef
DJ
5489 memset (loc, 0, sizeof (*loc));
5490
348d480f
PA
5491 gdb_assert (ops != NULL);
5492
28010a5d
PA
5493 loc->ops = ops;
5494 loc->owner = owner;
511a6cd4 5495 loc->cond = NULL;
0d381245
VP
5496 loc->shlib_disabled = 0;
5497 loc->enabled = 1;
e049a4b5 5498
28010a5d 5499 switch (owner->type)
e049a4b5
DJ
5500 {
5501 case bp_breakpoint:
5502 case bp_until:
5503 case bp_finish:
5504 case bp_longjmp:
5505 case bp_longjmp_resume:
186c406b
TT
5506 case bp_exception:
5507 case bp_exception_resume:
e049a4b5 5508 case bp_step_resume:
2c03e5be 5509 case bp_hp_step_resume:
e049a4b5
DJ
5510 case bp_watchpoint_scope:
5511 case bp_call_dummy:
aa7d318d 5512 case bp_std_terminate:
e049a4b5
DJ
5513 case bp_shlib_event:
5514 case bp_thread_event:
5515 case bp_overlay_event:
4efc6507 5516 case bp_jit_event:
0fd8e87f 5517 case bp_longjmp_master:
aa7d318d 5518 case bp_std_terminate_master:
186c406b 5519 case bp_exception_master:
0e30163f
JK
5520 case bp_gnu_ifunc_resolver:
5521 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5522 loc->loc_type = bp_loc_software_breakpoint;
5523 break;
5524 case bp_hardware_breakpoint:
5525 loc->loc_type = bp_loc_hardware_breakpoint;
5526 break;
5527 case bp_hardware_watchpoint:
5528 case bp_read_watchpoint:
5529 case bp_access_watchpoint:
5530 loc->loc_type = bp_loc_hardware_watchpoint;
5531 break;
5532 case bp_watchpoint:
ce78b96d 5533 case bp_catchpoint:
15c3d785
PA
5534 case bp_tracepoint:
5535 case bp_fast_tracepoint:
0fb4aa4b 5536 case bp_static_tracepoint:
e049a4b5
DJ
5537 loc->loc_type = bp_loc_other;
5538 break;
5539 default:
e2e0b3e5 5540 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5541 }
5542
f431efe5 5543 loc->refc = 1;
28010a5d
PA
5544}
5545
5546/* Allocate a struct bp_location. */
5547
5548static struct bp_location *
5549allocate_bp_location (struct breakpoint *bpt)
5550{
348d480f
PA
5551 return bpt->ops->allocate_location (bpt);
5552}
7cc221ef 5553
f431efe5
PA
5554static void
5555free_bp_location (struct bp_location *loc)
fe3f5fa8 5556{
348d480f 5557 loc->ops->dtor (loc);
fe3f5fa8
VP
5558 xfree (loc);
5559}
5560
f431efe5
PA
5561/* Increment reference count. */
5562
5563static void
5564incref_bp_location (struct bp_location *bl)
5565{
5566 ++bl->refc;
5567}
5568
5569/* Decrement reference count. If the reference count reaches 0,
5570 destroy the bp_location. Sets *BLP to NULL. */
5571
5572static void
5573decref_bp_location (struct bp_location **blp)
5574{
0807b50c
PA
5575 gdb_assert ((*blp)->refc > 0);
5576
f431efe5
PA
5577 if (--(*blp)->refc == 0)
5578 free_bp_location (*blp);
5579 *blp = NULL;
5580}
5581
346774a9 5582/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5583
346774a9
PA
5584static void
5585add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5586{
346774a9 5587 struct breakpoint *b1;
c906108c 5588
346774a9
PA
5589 /* Add this breakpoint to the end of the chain so that a list of
5590 breakpoints will come out in order of increasing numbers. */
5591
5592 b1 = breakpoint_chain;
5593 if (b1 == 0)
5594 breakpoint_chain = b;
5595 else
5596 {
5597 while (b1->next)
5598 b1 = b1->next;
5599 b1->next = b;
5600 }
5601}
5602
5603/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5604
5605static void
5606init_raw_breakpoint_without_location (struct breakpoint *b,
5607 struct gdbarch *gdbarch,
28010a5d 5608 enum bptype bptype,
c0a91b2b 5609 const struct breakpoint_ops *ops)
346774a9 5610{
c906108c 5611 memset (b, 0, sizeof (*b));
2219d63c 5612
348d480f
PA
5613 gdb_assert (ops != NULL);
5614
28010a5d 5615 b->ops = ops;
4d28f7a8 5616 b->type = bptype;
a6d9a66e 5617 b->gdbarch = gdbarch;
c906108c
SS
5618 b->language = current_language->la_language;
5619 b->input_radix = input_radix;
5620 b->thread = -1;
b5de0fa7 5621 b->enable_state = bp_enabled;
c906108c
SS
5622 b->next = 0;
5623 b->silent = 0;
5624 b->ignore_count = 0;
5625 b->commands = NULL;
818dd999 5626 b->frame_id = null_frame_id;
0d381245 5627 b->condition_not_parsed = 0;
84f4c1fe 5628 b->py_bp_object = NULL;
d0fb5eae 5629 b->related_breakpoint = b;
346774a9
PA
5630}
5631
5632/* Helper to set_raw_breakpoint below. Creates a breakpoint
5633 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5634
5635static struct breakpoint *
5636set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5637 enum bptype bptype,
c0a91b2b 5638 const struct breakpoint_ops *ops)
346774a9
PA
5639{
5640 struct breakpoint *b = XNEW (struct breakpoint);
5641
348d480f 5642 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5643 add_to_breakpoint_chain (b);
0d381245
VP
5644 return b;
5645}
5646
0e30163f
JK
5647/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5648 resolutions should be made as the user specified the location explicitly
5649 enough. */
5650
0d381245 5651static void
0e30163f 5652set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5653{
2bdf28a0
JK
5654 gdb_assert (loc->owner != NULL);
5655
0d381245 5656 if (loc->owner->type == bp_breakpoint
1042e4c0 5657 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5658 || is_tracepoint (loc->owner))
0d381245 5659 {
0e30163f
JK
5660 int is_gnu_ifunc;
5661
5662 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5663 NULL, NULL, &is_gnu_ifunc);
5664
5665 if (is_gnu_ifunc && !explicit_loc)
5666 {
5667 struct breakpoint *b = loc->owner;
5668
5669 gdb_assert (loc->pspace == current_program_space);
5670 if (gnu_ifunc_resolve_name (loc->function_name,
5671 &loc->requested_address))
5672 {
5673 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5674 loc->address = adjust_breakpoint_address (loc->gdbarch,
5675 loc->requested_address,
5676 b->type);
5677 }
5678 else if (b->type == bp_breakpoint && b->loc == loc
5679 && loc->next == NULL && b->related_breakpoint == b)
5680 {
5681 /* Create only the whole new breakpoint of this type but do not
5682 mess more complicated breakpoints with multiple locations. */
5683 b->type = bp_gnu_ifunc_resolver;
5684 }
5685 }
5686
0d381245
VP
5687 if (loc->function_name)
5688 loc->function_name = xstrdup (loc->function_name);
5689 }
5690}
5691
a6d9a66e
UW
5692/* Attempt to determine architecture of location identified by SAL. */
5693static struct gdbarch *
5694get_sal_arch (struct symtab_and_line sal)
5695{
5696 if (sal.section)
5697 return get_objfile_arch (sal.section->objfile);
5698 if (sal.symtab)
5699 return get_objfile_arch (sal.symtab->objfile);
5700
5701 return NULL;
5702}
5703
346774a9
PA
5704/* Low level routine for partially initializing a breakpoint of type
5705 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5706 file name, and line number are provided by SAL.
0d381245
VP
5707
5708 It is expected that the caller will complete the initialization of
5709 the newly created breakpoint struct as well as output any status
c56053d2 5710 information regarding the creation of a new breakpoint. */
0d381245 5711
346774a9
PA
5712static void
5713init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5714 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5715 const struct breakpoint_ops *ops)
0d381245 5716{
0d381245 5717 CORE_ADDR adjusted_address;
a6d9a66e
UW
5718 struct gdbarch *loc_gdbarch;
5719
28010a5d 5720 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5721
a6d9a66e
UW
5722 loc_gdbarch = get_sal_arch (sal);
5723 if (!loc_gdbarch)
5724 loc_gdbarch = b->gdbarch;
0d381245 5725
6c95b8df
PA
5726 if (bptype != bp_catchpoint)
5727 gdb_assert (sal.pspace != NULL);
5728
0d381245
VP
5729 /* Adjust the breakpoint's address prior to allocating a location.
5730 Once we call allocate_bp_location(), that mostly uninitialized
5731 location will be placed on the location chain. Adjustment of the
8defab1a 5732 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5733 not want its scan of the location chain to find a breakpoint and
5734 location that's only been partially initialized. */
4a64f543
MS
5735 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5736 sal.pc, b->type);
0d381245 5737
39d61571 5738 b->loc = allocate_bp_location (b);
a6d9a66e 5739 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5740 b->loc->requested_address = sal.pc;
5741 b->loc->address = adjusted_address;
6c95b8df
PA
5742 b->loc->pspace = sal.pspace;
5743
5744 /* Store the program space that was used to set the breakpoint, for
5745 breakpoint resetting. */
5746 b->pspace = sal.pspace;
0d381245
VP
5747
5748 if (sal.symtab == NULL)
5749 b->source_file = NULL;
5750 else
1b36a34b 5751 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5752 b->loc->section = sal.section;
5753 b->line_number = sal.line;
5754
0e30163f
JK
5755 set_breakpoint_location_function (b->loc,
5756 sal.explicit_pc || sal.explicit_line);
c906108c 5757
c906108c 5758 breakpoints_changed ();
346774a9 5759}
c906108c 5760
346774a9
PA
5761/* set_raw_breakpoint is a low level routine for allocating and
5762 partially initializing a breakpoint of type BPTYPE. The newly
5763 created breakpoint's address, section, source file name, and line
5764 number are provided by SAL. The newly created and partially
5765 initialized breakpoint is added to the breakpoint chain and
5766 is also returned as the value of this function.
5767
5768 It is expected that the caller will complete the initialization of
5769 the newly created breakpoint struct as well as output any status
5770 information regarding the creation of a new breakpoint. In
5771 particular, set_raw_breakpoint does NOT set the breakpoint
5772 number! Care should be taken to not allow an error to occur
5773 prior to completing the initialization of the breakpoint. If this
5774 should happen, a bogus breakpoint will be left on the chain. */
5775
5776struct breakpoint *
5777set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5778 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5779 const struct breakpoint_ops *ops)
346774a9
PA
5780{
5781 struct breakpoint *b = XNEW (struct breakpoint);
5782
348d480f 5783 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5784 add_to_breakpoint_chain (b);
c906108c
SS
5785 return b;
5786}
5787
c2c6d25f
JM
5788
5789/* Note that the breakpoint object B describes a permanent breakpoint
5790 instruction, hard-wired into the inferior's code. */
5791void
5792make_breakpoint_permanent (struct breakpoint *b)
5793{
0d381245 5794 struct bp_location *bl;
cc59ec59 5795
b5de0fa7 5796 b->enable_state = bp_permanent;
c2c6d25f 5797
4a64f543
MS
5798 /* By definition, permanent breakpoints are already present in the
5799 code. Mark all locations as inserted. For now,
5800 make_breakpoint_permanent is called in just one place, so it's
5801 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5802 multiple locations or not, but it's easy to implement. */
0d381245
VP
5803 for (bl = b->loc; bl; bl = bl->next)
5804 bl->inserted = 1;
c2c6d25f
JM
5805}
5806
53a5351d 5807/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5808 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5809 initiated the operation. */
c906108c
SS
5810
5811void
186c406b 5812set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5813{
35df4500 5814 struct breakpoint *b, *b_tmp;
186c406b 5815 int thread = tp->num;
0fd8e87f
UW
5816
5817 /* To avoid having to rescan all objfile symbols at every step,
5818 we maintain a list of continually-inserted but always disabled
5819 longjmp "master" breakpoints. Here, we simply create momentary
5820 clones of those and enable them for the requested thread. */
35df4500 5821 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5822 if (b->pspace == current_program_space
186c406b
TT
5823 && (b->type == bp_longjmp_master
5824 || b->type == bp_exception_master))
0fd8e87f 5825 {
06edf0c0
PA
5826 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5827 struct breakpoint *clone;
cc59ec59 5828
06edf0c0
PA
5829 clone = momentary_breakpoint_from_master (b, type,
5830 &momentary_breakpoint_ops);
0fd8e87f
UW
5831 clone->thread = thread;
5832 }
186c406b
TT
5833
5834 tp->initiating_frame = frame;
c906108c
SS
5835}
5836
611c83ae 5837/* Delete all longjmp breakpoints from THREAD. */
c906108c 5838void
611c83ae 5839delete_longjmp_breakpoint (int thread)
c906108c 5840{
35df4500 5841 struct breakpoint *b, *b_tmp;
c906108c 5842
35df4500 5843 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5844 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5845 {
5846 if (b->thread == thread)
5847 delete_breakpoint (b);
5848 }
c906108c
SS
5849}
5850
1900040c
MS
5851void
5852enable_overlay_breakpoints (void)
5853{
52f0bd74 5854 struct breakpoint *b;
1900040c
MS
5855
5856 ALL_BREAKPOINTS (b)
5857 if (b->type == bp_overlay_event)
5858 {
5859 b->enable_state = bp_enabled;
b60e7edf 5860 update_global_location_list (1);
c02f5703 5861 overlay_events_enabled = 1;
1900040c
MS
5862 }
5863}
5864
5865void
5866disable_overlay_breakpoints (void)
5867{
52f0bd74 5868 struct breakpoint *b;
1900040c
MS
5869
5870 ALL_BREAKPOINTS (b)
5871 if (b->type == bp_overlay_event)
5872 {
5873 b->enable_state = bp_disabled;
b60e7edf 5874 update_global_location_list (0);
c02f5703 5875 overlay_events_enabled = 0;
1900040c
MS
5876 }
5877}
5878
aa7d318d
TT
5879/* Set an active std::terminate breakpoint for each std::terminate
5880 master breakpoint. */
5881void
5882set_std_terminate_breakpoint (void)
5883{
35df4500 5884 struct breakpoint *b, *b_tmp;
aa7d318d 5885
35df4500 5886 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5887 if (b->pspace == current_program_space
5888 && b->type == bp_std_terminate_master)
5889 {
06edf0c0
PA
5890 momentary_breakpoint_from_master (b, bp_std_terminate,
5891 &momentary_breakpoint_ops);
aa7d318d
TT
5892 }
5893}
5894
5895/* Delete all the std::terminate breakpoints. */
5896void
5897delete_std_terminate_breakpoint (void)
5898{
35df4500 5899 struct breakpoint *b, *b_tmp;
aa7d318d 5900
35df4500 5901 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5902 if (b->type == bp_std_terminate)
5903 delete_breakpoint (b);
5904}
5905
c4093a6a 5906struct breakpoint *
a6d9a66e 5907create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5908{
5909 struct breakpoint *b;
c4093a6a 5910
06edf0c0
PA
5911 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5912 &internal_breakpoint_ops);
5913
b5de0fa7 5914 b->enable_state = bp_enabled;
c4093a6a 5915 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5916 b->addr_string
5917 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5918
b60e7edf 5919 update_global_location_list_nothrow (1);
74960c60 5920
c4093a6a
JM
5921 return b;
5922}
5923
5924void
5925remove_thread_event_breakpoints (void)
5926{
35df4500 5927 struct breakpoint *b, *b_tmp;
c4093a6a 5928
35df4500 5929 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5930 if (b->type == bp_thread_event
5931 && b->loc->pspace == current_program_space)
c4093a6a
JM
5932 delete_breakpoint (b);
5933}
5934
0101ce28
JJ
5935struct lang_and_radix
5936 {
5937 enum language lang;
5938 int radix;
5939 };
5940
4efc6507
DE
5941/* Create a breakpoint for JIT code registration and unregistration. */
5942
5943struct breakpoint *
5944create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5945{
5946 struct breakpoint *b;
5947
06edf0c0
PA
5948 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5949 &internal_breakpoint_ops);
4efc6507
DE
5950 update_global_location_list_nothrow (1);
5951 return b;
5952}
0101ce28 5953
03673fc7
PP
5954/* Remove JIT code registration and unregistration breakpoint(s). */
5955
5956void
5957remove_jit_event_breakpoints (void)
5958{
5959 struct breakpoint *b, *b_tmp;
5960
5961 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5962 if (b->type == bp_jit_event
5963 && b->loc->pspace == current_program_space)
5964 delete_breakpoint (b);
5965}
5966
cae688ec
JJ
5967void
5968remove_solib_event_breakpoints (void)
5969{
35df4500 5970 struct breakpoint *b, *b_tmp;
cae688ec 5971
35df4500 5972 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5973 if (b->type == bp_shlib_event
5974 && b->loc->pspace == current_program_space)
cae688ec
JJ
5975 delete_breakpoint (b);
5976}
5977
5978struct breakpoint *
a6d9a66e 5979create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5980{
5981 struct breakpoint *b;
5982
06edf0c0
PA
5983 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
5984 &internal_breakpoint_ops);
b60e7edf 5985 update_global_location_list_nothrow (1);
cae688ec
JJ
5986 return b;
5987}
5988
5989/* Disable any breakpoints that are on code in shared libraries. Only
5990 apply to enabled breakpoints, disabled ones can just stay disabled. */
5991
5992void
cb851954 5993disable_breakpoints_in_shlibs (void)
cae688ec 5994{
876fa593 5995 struct bp_location *loc, **locp_tmp;
cae688ec 5996
876fa593 5997 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 5998 {
2bdf28a0 5999 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6000 struct breakpoint *b = loc->owner;
2bdf28a0 6001
4a64f543
MS
6002 /* We apply the check to all breakpoints, including disabled for
6003 those with loc->duplicate set. This is so that when breakpoint
6004 becomes enabled, or the duplicate is removed, gdb will try to
6005 insert all breakpoints. If we don't set shlib_disabled here,
6006 we'll try to insert those breakpoints and fail. */
1042e4c0 6007 if (((b->type == bp_breakpoint)
508ccb1f 6008 || (b->type == bp_jit_event)
1042e4c0 6009 || (b->type == bp_hardware_breakpoint)
d77f58be 6010 || (is_tracepoint (b)))
6c95b8df 6011 && loc->pspace == current_program_space
0d381245 6012 && !loc->shlib_disabled
a77053c2 6013#ifdef PC_SOLIB
0d381245 6014 && PC_SOLIB (loc->address)
a77053c2 6015#else
6c95b8df 6016 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6017#endif
6018 )
0d381245
VP
6019 {
6020 loc->shlib_disabled = 1;
6021 }
cae688ec
JJ
6022 }
6023}
6024
7a9dd1b2 6025/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
6026 Only apply to enabled breakpoints, disabled ones can just stay
6027 disabled. */
84acb35a 6028
75149521 6029static void
84acb35a
JJ
6030disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6031{
876fa593 6032 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6033 int disabled_shlib_breaks = 0;
6034
c86cf029
VP
6035 /* SunOS a.out shared libraries are always mapped, so do not
6036 disable breakpoints; they will only be reported as unloaded
6037 through clear_solib when GDB discards its shared library
6038 list. See clear_solib for more information. */
6039 if (exec_bfd != NULL
6040 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6041 return;
6042
876fa593 6043 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6044 {
2bdf28a0 6045 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6046 struct breakpoint *b = loc->owner;
cc59ec59 6047
0d381245
VP
6048 if ((loc->loc_type == bp_loc_hardware_breakpoint
6049 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6050 && solib->pspace == loc->pspace
e2dd7057 6051 && !loc->shlib_disabled
508ccb1f
TT
6052 && (b->type == bp_breakpoint
6053 || b->type == bp_jit_event
6054 || b->type == bp_hardware_breakpoint)
e2dd7057 6055 && solib_contains_address_p (solib, loc->address))
84acb35a 6056 {
e2dd7057
PP
6057 loc->shlib_disabled = 1;
6058 /* At this point, we cannot rely on remove_breakpoint
6059 succeeding so we must mark the breakpoint as not inserted
6060 to prevent future errors occurring in remove_breakpoints. */
6061 loc->inserted = 0;
8d3788bd
VP
6062
6063 /* This may cause duplicate notifications for the same breakpoint. */
6064 observer_notify_breakpoint_modified (b);
6065
e2dd7057
PP
6066 if (!disabled_shlib_breaks)
6067 {
6068 target_terminal_ours_for_output ();
3e43a32a
MS
6069 warning (_("Temporarily disabling breakpoints "
6070 "for unloaded shared library \"%s\""),
e2dd7057 6071 solib->so_name);
84acb35a 6072 }
e2dd7057 6073 disabled_shlib_breaks = 1;
84acb35a
JJ
6074 }
6075 }
84acb35a
JJ
6076}
6077
ce78b96d
JB
6078/* FORK & VFORK catchpoints. */
6079
e29a4733
PA
6080/* An instance of this type is used to represent a fork or vfork
6081 catchpoint. It includes a "struct breakpoint" as a kind of base
6082 class; users downcast to "struct breakpoint *" when needed. A
6083 breakpoint is really of this type iff its ops pointer points to
6084 CATCH_FORK_BREAKPOINT_OPS. */
6085
6086struct fork_catchpoint
6087{
6088 /* The base class. */
6089 struct breakpoint base;
6090
6091 /* Process id of a child process whose forking triggered this
6092 catchpoint. This field is only valid immediately after this
6093 catchpoint has triggered. */
6094 ptid_t forked_inferior_pid;
6095};
6096
4a64f543
MS
6097/* Implement the "insert" breakpoint_ops method for fork
6098 catchpoints. */
ce78b96d 6099
77b06cd7
TJB
6100static int
6101insert_catch_fork (struct bp_location *bl)
ce78b96d 6102{
77b06cd7 6103 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6104}
6105
4a64f543
MS
6106/* Implement the "remove" breakpoint_ops method for fork
6107 catchpoints. */
ce78b96d
JB
6108
6109static int
77b06cd7 6110remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6111{
6112 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6113}
6114
6115/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6116 catchpoints. */
6117
6118static int
f1310107
TJB
6119breakpoint_hit_catch_fork (const struct bp_location *bl,
6120 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6121{
e29a4733
PA
6122 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6123
6124 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6125}
6126
4a64f543
MS
6127/* Implement the "print_it" breakpoint_ops method for fork
6128 catchpoints. */
ce78b96d
JB
6129
6130static enum print_stop_action
348d480f 6131print_it_catch_fork (bpstat bs)
ce78b96d 6132{
348d480f
PA
6133 struct breakpoint *b = bs->breakpoint_at;
6134 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6135
ce78b96d
JB
6136 annotate_catchpoint (b->number);
6137 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6138 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6139 return PRINT_SRC_AND_LOC;
6140}
6141
4a64f543
MS
6142/* Implement the "print_one" breakpoint_ops method for fork
6143 catchpoints. */
ce78b96d
JB
6144
6145static void
a6d9a66e 6146print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6147{
e29a4733 6148 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6149 struct value_print_options opts;
6150
6151 get_user_print_options (&opts);
6152
4a64f543
MS
6153 /* Field 4, the address, is omitted (which makes the columns not
6154 line up too nicely with the headers, but the effect is relatively
6155 readable). */
79a45b7d 6156 if (opts.addressprint)
ce78b96d
JB
6157 ui_out_field_skip (uiout, "addr");
6158 annotate_field (5);
6159 ui_out_text (uiout, "fork");
e29a4733 6160 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6161 {
6162 ui_out_text (uiout, ", process ");
6163 ui_out_field_int (uiout, "what",
e29a4733 6164 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6165 ui_out_spaces (uiout, 1);
6166 }
6167}
6168
6169/* Implement the "print_mention" breakpoint_ops method for fork
6170 catchpoints. */
6171
6172static void
6173print_mention_catch_fork (struct breakpoint *b)
6174{
6175 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6176}
6177
6149aea9
PA
6178/* Implement the "print_recreate" breakpoint_ops method for fork
6179 catchpoints. */
6180
6181static void
6182print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6183{
6184 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6185 print_recreate_thread (b, fp);
6149aea9
PA
6186}
6187
ce78b96d
JB
6188/* The breakpoint_ops structure to be used in fork catchpoints. */
6189
2060206e 6190static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6191
4a64f543
MS
6192/* Implement the "insert" breakpoint_ops method for vfork
6193 catchpoints. */
ce78b96d 6194
77b06cd7
TJB
6195static int
6196insert_catch_vfork (struct bp_location *bl)
ce78b96d 6197{
77b06cd7 6198 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6199}
6200
4a64f543
MS
6201/* Implement the "remove" breakpoint_ops method for vfork
6202 catchpoints. */
ce78b96d
JB
6203
6204static int
77b06cd7 6205remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6206{
6207 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6208}
6209
6210/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6211 catchpoints. */
6212
6213static int
f1310107
TJB
6214breakpoint_hit_catch_vfork (const struct bp_location *bl,
6215 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6216{
e29a4733
PA
6217 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6218
6219 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6220}
6221
4a64f543
MS
6222/* Implement the "print_it" breakpoint_ops method for vfork
6223 catchpoints. */
ce78b96d
JB
6224
6225static enum print_stop_action
348d480f 6226print_it_catch_vfork (bpstat bs)
ce78b96d 6227{
348d480f 6228 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6229 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6230
ce78b96d
JB
6231 annotate_catchpoint (b->number);
6232 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6233 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6234 return PRINT_SRC_AND_LOC;
6235}
6236
4a64f543
MS
6237/* Implement the "print_one" breakpoint_ops method for vfork
6238 catchpoints. */
ce78b96d
JB
6239
6240static void
a6d9a66e 6241print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6242{
e29a4733 6243 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6244 struct value_print_options opts;
6245
6246 get_user_print_options (&opts);
4a64f543
MS
6247 /* Field 4, the address, is omitted (which makes the columns not
6248 line up too nicely with the headers, but the effect is relatively
6249 readable). */
79a45b7d 6250 if (opts.addressprint)
ce78b96d
JB
6251 ui_out_field_skip (uiout, "addr");
6252 annotate_field (5);
6253 ui_out_text (uiout, "vfork");
e29a4733 6254 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6255 {
6256 ui_out_text (uiout, ", process ");
6257 ui_out_field_int (uiout, "what",
e29a4733 6258 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6259 ui_out_spaces (uiout, 1);
6260 }
6261}
6262
6263/* Implement the "print_mention" breakpoint_ops method for vfork
6264 catchpoints. */
6265
6266static void
6267print_mention_catch_vfork (struct breakpoint *b)
6268{
6269 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6270}
6271
6149aea9
PA
6272/* Implement the "print_recreate" breakpoint_ops method for vfork
6273 catchpoints. */
6274
6275static void
6276print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6277{
6278 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6279 print_recreate_thread (b, fp);
6149aea9
PA
6280}
6281
ce78b96d
JB
6282/* The breakpoint_ops structure to be used in vfork catchpoints. */
6283
2060206e 6284static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6285
be5c67c1
PA
6286/* An instance of this type is used to represent a syscall catchpoint.
6287 It includes a "struct breakpoint" as a kind of base class; users
6288 downcast to "struct breakpoint *" when needed. A breakpoint is
6289 really of this type iff its ops pointer points to
6290 CATCH_SYSCALL_BREAKPOINT_OPS. */
6291
6292struct syscall_catchpoint
6293{
6294 /* The base class. */
6295 struct breakpoint base;
6296
6297 /* Syscall numbers used for the 'catch syscall' feature. If no
6298 syscall has been specified for filtering, its value is NULL.
6299 Otherwise, it holds a list of all syscalls to be caught. The
6300 list elements are allocated with xmalloc. */
6301 VEC(int) *syscalls_to_be_caught;
6302};
6303
6304/* Implement the "dtor" breakpoint_ops method for syscall
6305 catchpoints. */
6306
6307static void
6308dtor_catch_syscall (struct breakpoint *b)
6309{
6310 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6311
6312 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6313
2060206e 6314 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6315}
6316
a96d9b2e
SDJ
6317/* Implement the "insert" breakpoint_ops method for syscall
6318 catchpoints. */
6319
77b06cd7
TJB
6320static int
6321insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6322{
be5c67c1 6323 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6324 struct inferior *inf = current_inferior ();
6325
6326 ++inf->total_syscalls_count;
be5c67c1 6327 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6328 ++inf->any_syscall_count;
6329 else
6330 {
6331 int i, iter;
cc59ec59 6332
a96d9b2e 6333 for (i = 0;
be5c67c1 6334 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6335 i++)
6336 {
6337 int elem;
cc59ec59 6338
a96d9b2e
SDJ
6339 if (iter >= VEC_length (int, inf->syscalls_counts))
6340 {
6341 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6342 uintptr_t vec_addr_offset
6343 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6344 uintptr_t vec_addr;
6345 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6346 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6347 vec_addr_offset;
6348 memset ((void *) vec_addr, 0,
6349 (iter + 1 - old_size) * sizeof (int));
6350 }
6351 elem = VEC_index (int, inf->syscalls_counts, iter);
6352 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6353 }
6354 }
6355
77b06cd7
TJB
6356 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6357 inf->total_syscalls_count != 0,
6358 inf->any_syscall_count,
6359 VEC_length (int, inf->syscalls_counts),
6360 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6361}
6362
6363/* Implement the "remove" breakpoint_ops method for syscall
6364 catchpoints. */
6365
6366static int
77b06cd7 6367remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6368{
be5c67c1 6369 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6370 struct inferior *inf = current_inferior ();
6371
6372 --inf->total_syscalls_count;
be5c67c1 6373 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6374 --inf->any_syscall_count;
6375 else
6376 {
6377 int i, iter;
cc59ec59 6378
a96d9b2e 6379 for (i = 0;
be5c67c1 6380 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6381 i++)
6382 {
6383 int elem;
6384 if (iter >= VEC_length (int, inf->syscalls_counts))
6385 /* Shouldn't happen. */
6386 continue;
6387 elem = VEC_index (int, inf->syscalls_counts, iter);
6388 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6389 }
6390 }
6391
6392 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6393 inf->total_syscalls_count != 0,
6394 inf->any_syscall_count,
6395 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6396 VEC_address (int,
6397 inf->syscalls_counts));
a96d9b2e
SDJ
6398}
6399
6400/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6401 catchpoints. */
6402
6403static int
f1310107
TJB
6404breakpoint_hit_catch_syscall (const struct bp_location *bl,
6405 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6406{
4a64f543
MS
6407 /* We must check if we are catching specific syscalls in this
6408 breakpoint. If we are, then we must guarantee that the called
6409 syscall is the same syscall we are catching. */
a96d9b2e 6410 int syscall_number = 0;
be5c67c1
PA
6411 const struct syscall_catchpoint *c
6412 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6413
6414 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6415 return 0;
6416
6417 /* Now, checking if the syscall is the same. */
be5c67c1 6418 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6419 {
6420 int i, iter;
cc59ec59 6421
a96d9b2e 6422 for (i = 0;
be5c67c1 6423 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6424 i++)
6425 if (syscall_number == iter)
6426 break;
6427 /* Not the same. */
6428 if (!iter)
6429 return 0;
6430 }
6431
6432 return 1;
6433}
6434
6435/* Implement the "print_it" breakpoint_ops method for syscall
6436 catchpoints. */
6437
6438static enum print_stop_action
348d480f 6439print_it_catch_syscall (bpstat bs)
a96d9b2e 6440{
348d480f 6441 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6442 /* These are needed because we want to know in which state a
6443 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6444 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6445 must print "called syscall" or "returned from syscall". */
6446 ptid_t ptid;
6447 struct target_waitstatus last;
6448 struct syscall s;
6449 struct cleanup *old_chain;
6450 char *syscall_id;
6451
6452 get_last_target_status (&ptid, &last);
6453
6454 get_syscall_by_number (last.value.syscall_number, &s);
6455
6456 annotate_catchpoint (b->number);
6457
6458 if (s.name == NULL)
6459 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6460 else
6461 syscall_id = xstrprintf ("'%s'", s.name);
6462
6463 old_chain = make_cleanup (xfree, syscall_id);
6464
6465 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6466 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6467 b->number, syscall_id);
6468 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6469 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6470 b->number, syscall_id);
6471
6472 do_cleanups (old_chain);
6473
6474 return PRINT_SRC_AND_LOC;
6475}
6476
6477/* Implement the "print_one" breakpoint_ops method for syscall
6478 catchpoints. */
6479
6480static void
6481print_one_catch_syscall (struct breakpoint *b,
f1310107 6482 struct bp_location **last_loc)
a96d9b2e 6483{
be5c67c1 6484 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e
SDJ
6485 struct value_print_options opts;
6486
6487 get_user_print_options (&opts);
4a64f543
MS
6488 /* Field 4, the address, is omitted (which makes the columns not
6489 line up too nicely with the headers, but the effect is relatively
6490 readable). */
a96d9b2e
SDJ
6491 if (opts.addressprint)
6492 ui_out_field_skip (uiout, "addr");
6493 annotate_field (5);
6494
be5c67c1
PA
6495 if (c->syscalls_to_be_caught
6496 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6497 ui_out_text (uiout, "syscalls \"");
6498 else
6499 ui_out_text (uiout, "syscall \"");
6500
be5c67c1 6501 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6502 {
6503 int i, iter;
6504 char *text = xstrprintf ("%s", "");
cc59ec59 6505
a96d9b2e 6506 for (i = 0;
be5c67c1 6507 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6508 i++)
6509 {
6510 char *x = text;
6511 struct syscall s;
6512 get_syscall_by_number (iter, &s);
6513
6514 if (s.name != NULL)
6515 text = xstrprintf ("%s%s, ", text, s.name);
6516 else
6517 text = xstrprintf ("%s%d, ", text, iter);
6518
6519 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6520 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6521 on every call. */
6522 xfree (x);
6523 }
6524 /* Remove the last comma. */
6525 text[strlen (text) - 2] = '\0';
6526 ui_out_field_string (uiout, "what", text);
6527 }
6528 else
6529 ui_out_field_string (uiout, "what", "<any syscall>");
6530 ui_out_text (uiout, "\" ");
6531}
6532
6533/* Implement the "print_mention" breakpoint_ops method for syscall
6534 catchpoints. */
6535
6536static void
6537print_mention_catch_syscall (struct breakpoint *b)
6538{
be5c67c1
PA
6539 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6540
6541 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6542 {
6543 int i, iter;
6544
be5c67c1 6545 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6546 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6547 else
6548 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6549
6550 for (i = 0;
be5c67c1 6551 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6552 i++)
6553 {
6554 struct syscall s;
6555 get_syscall_by_number (iter, &s);
6556
6557 if (s.name)
6558 printf_filtered (" '%s' [%d]", s.name, s.number);
6559 else
6560 printf_filtered (" %d", s.number);
6561 }
6562 printf_filtered (")");
6563 }
6564 else
6565 printf_filtered (_("Catchpoint %d (any syscall)"),
6566 b->number);
6567}
6568
6149aea9
PA
6569/* Implement the "print_recreate" breakpoint_ops method for syscall
6570 catchpoints. */
6571
6572static void
6573print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6574{
be5c67c1
PA
6575 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6576
6149aea9
PA
6577 fprintf_unfiltered (fp, "catch syscall");
6578
be5c67c1 6579 if (c->syscalls_to_be_caught)
6149aea9
PA
6580 {
6581 int i, iter;
6582
6583 for (i = 0;
be5c67c1 6584 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6585 i++)
6586 {
6587 struct syscall s;
6588
6589 get_syscall_by_number (iter, &s);
6590 if (s.name)
6591 fprintf_unfiltered (fp, " %s", s.name);
6592 else
6593 fprintf_unfiltered (fp, " %d", s.number);
6594 }
6595 }
d9b3f62e 6596 print_recreate_thread (b, fp);
6149aea9
PA
6597}
6598
a96d9b2e
SDJ
6599/* The breakpoint_ops structure to be used in syscall catchpoints. */
6600
2060206e 6601static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6602
6603/* Returns non-zero if 'b' is a syscall catchpoint. */
6604
6605static int
6606syscall_catchpoint_p (struct breakpoint *b)
6607{
6608 return (b->ops == &catch_syscall_breakpoint_ops);
6609}
6610
346774a9
PA
6611/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6612 is non-zero, then make the breakpoint temporary. If COND_STRING is
6613 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6614 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6615
346774a9
PA
6616static void
6617init_catchpoint (struct breakpoint *b,
6618 struct gdbarch *gdbarch, int tempflag,
6619 char *cond_string,
c0a91b2b 6620 const struct breakpoint_ops *ops)
c906108c 6621{
c5aa993b 6622 struct symtab_and_line sal;
346774a9 6623
fe39c653 6624 init_sal (&sal);
6c95b8df 6625 sal.pspace = current_program_space;
c5aa993b 6626
28010a5d 6627 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6628
1b36a34b 6629 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6630 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6631}
6632
28010a5d 6633void
3a5c3e22 6634install_breakpoint (int internal, struct breakpoint *b)
c56053d2
PA
6635{
6636 add_to_breakpoint_chain (b);
3a5c3e22
PA
6637 set_breakpoint_number (internal, b);
6638 if (!internal)
6639 mention (b);
c56053d2
PA
6640 observer_notify_breakpoint_created (b);
6641 update_global_location_list (1);
6642}
6643
9b70b993 6644static void
a6d9a66e
UW
6645create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6646 int tempflag, char *cond_string,
c0a91b2b 6647 const struct breakpoint_ops *ops)
c906108c 6648{
e29a4733 6649 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6650
e29a4733
PA
6651 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6652
6653 c->forked_inferior_pid = null_ptid;
6654
3a5c3e22 6655 install_breakpoint (0, &c->base);
c906108c
SS
6656}
6657
fe798b75
JB
6658/* Exec catchpoints. */
6659
b4d90040
PA
6660/* An instance of this type is used to represent an exec catchpoint.
6661 It includes a "struct breakpoint" as a kind of base class; users
6662 downcast to "struct breakpoint *" when needed. A breakpoint is
6663 really of this type iff its ops pointer points to
6664 CATCH_EXEC_BREAKPOINT_OPS. */
6665
6666struct exec_catchpoint
6667{
6668 /* The base class. */
6669 struct breakpoint base;
6670
6671 /* Filename of a program whose exec triggered this catchpoint.
6672 This field is only valid immediately after this catchpoint has
6673 triggered. */
6674 char *exec_pathname;
6675};
6676
6677/* Implement the "dtor" breakpoint_ops method for exec
6678 catchpoints. */
6679
6680static void
6681dtor_catch_exec (struct breakpoint *b)
6682{
6683 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6684
6685 xfree (c->exec_pathname);
348d480f 6686
2060206e 6687 base_breakpoint_ops.dtor (b);
b4d90040
PA
6688}
6689
77b06cd7
TJB
6690static int
6691insert_catch_exec (struct bp_location *bl)
c906108c 6692{
77b06cd7 6693 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6694}
c906108c 6695
fe798b75 6696static int
77b06cd7 6697remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6698{
6699 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6700}
c906108c 6701
fe798b75 6702static int
f1310107
TJB
6703breakpoint_hit_catch_exec (const struct bp_location *bl,
6704 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6705{
b4d90040
PA
6706 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6707
6708 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6709}
c906108c 6710
fe798b75 6711static enum print_stop_action
348d480f 6712print_it_catch_exec (bpstat bs)
fe798b75 6713{
348d480f 6714 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6715 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6716
fe798b75
JB
6717 annotate_catchpoint (b->number);
6718 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6719 c->exec_pathname);
fe798b75 6720 return PRINT_SRC_AND_LOC;
c906108c
SS
6721}
6722
fe798b75 6723static void
a6d9a66e 6724print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6725{
b4d90040 6726 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75
JB
6727 struct value_print_options opts;
6728
6729 get_user_print_options (&opts);
6730
6731 /* Field 4, the address, is omitted (which makes the columns
6732 not line up too nicely with the headers, but the effect
6733 is relatively readable). */
6734 if (opts.addressprint)
6735 ui_out_field_skip (uiout, "addr");
6736 annotate_field (5);
6737 ui_out_text (uiout, "exec");
b4d90040 6738 if (c->exec_pathname != NULL)
fe798b75
JB
6739 {
6740 ui_out_text (uiout, ", program \"");
b4d90040 6741 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6742 ui_out_text (uiout, "\" ");
6743 }
6744}
6745
6746static void
6747print_mention_catch_exec (struct breakpoint *b)
6748{
6749 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6750}
6751
6149aea9
PA
6752/* Implement the "print_recreate" breakpoint_ops method for exec
6753 catchpoints. */
6754
6755static void
6756print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6757{
6758 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6759 print_recreate_thread (b, fp);
6149aea9
PA
6760}
6761
2060206e 6762static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6763
a96d9b2e
SDJ
6764static void
6765create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6766 const struct breakpoint_ops *ops)
a96d9b2e 6767{
be5c67c1 6768 struct syscall_catchpoint *c;
a96d9b2e 6769 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6770
be5c67c1
PA
6771 c = XNEW (struct syscall_catchpoint);
6772 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6773 c->syscalls_to_be_caught = filter;
a96d9b2e 6774
3a5c3e22 6775 install_breakpoint (0, &c->base);
a96d9b2e
SDJ
6776}
6777
c906108c 6778static int
fba45db2 6779hw_breakpoint_used_count (void)
c906108c 6780{
c906108c 6781 int i = 0;
f1310107
TJB
6782 struct breakpoint *b;
6783 struct bp_location *bl;
c906108c
SS
6784
6785 ALL_BREAKPOINTS (b)
c5aa993b 6786 {
d6b74ac4 6787 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6788 for (bl = b->loc; bl; bl = bl->next)
6789 {
6790 /* Special types of hardware breakpoints may use more than
6791 one register. */
348d480f 6792 i += b->ops->resources_needed (bl);
f1310107 6793 }
c5aa993b 6794 }
c906108c
SS
6795
6796 return i;
6797}
6798
6799static int
fba45db2 6800hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6801{
c906108c 6802 int i = 0;
e09342b5
TJB
6803 struct breakpoint *b;
6804 struct bp_location *bl;
c906108c
SS
6805
6806 *other_type_used = 0;
6807 ALL_BREAKPOINTS (b)
e09342b5
TJB
6808 {
6809 if (!breakpoint_enabled (b))
6810 continue;
6811
c5aa993b 6812 if (b->type == type)
e09342b5
TJB
6813 for (bl = b->loc; bl; bl = bl->next)
6814 {
6815 /* Special types of hardware watchpoints may use more than
6816 one register. */
348d480f 6817 i += b->ops->resources_needed (bl);
e09342b5 6818 }
cc60f2e3 6819 else if (is_hardware_watchpoint (b))
c5aa993b 6820 *other_type_used = 1;
e09342b5
TJB
6821 }
6822
c906108c
SS
6823 return i;
6824}
6825
c906108c 6826void
fba45db2 6827disable_watchpoints_before_interactive_call_start (void)
c906108c 6828{
c5aa993b 6829 struct breakpoint *b;
c906108c
SS
6830
6831 ALL_BREAKPOINTS (b)
c5aa993b 6832 {
cc60f2e3 6833 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6834 {
b5de0fa7 6835 b->enable_state = bp_call_disabled;
b60e7edf 6836 update_global_location_list (0);
c5aa993b
JM
6837 }
6838 }
c906108c
SS
6839}
6840
6841void
fba45db2 6842enable_watchpoints_after_interactive_call_stop (void)
c906108c 6843{
c5aa993b 6844 struct breakpoint *b;
c906108c
SS
6845
6846 ALL_BREAKPOINTS (b)
c5aa993b 6847 {
cc60f2e3 6848 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6849 {
b5de0fa7 6850 b->enable_state = bp_enabled;
b60e7edf 6851 update_global_location_list (1);
c5aa993b
JM
6852 }
6853 }
c906108c
SS
6854}
6855
8bea4e01
UW
6856void
6857disable_breakpoints_before_startup (void)
6858{
6859 struct breakpoint *b;
6860 int found = 0;
6861
6862 ALL_BREAKPOINTS (b)
6863 {
6c95b8df
PA
6864 if (b->pspace != current_program_space)
6865 continue;
6866
8bea4e01
UW
6867 if ((b->type == bp_breakpoint
6868 || b->type == bp_hardware_breakpoint)
6869 && breakpoint_enabled (b))
6870 {
6871 b->enable_state = bp_startup_disabled;
6872 found = 1;
6873 }
6874 }
6875
6876 if (found)
6877 update_global_location_list (0);
6878
6c95b8df 6879 current_program_space->executing_startup = 1;
8bea4e01
UW
6880}
6881
6882void
6883enable_breakpoints_after_startup (void)
6884{
6885 struct breakpoint *b;
6886 int found = 0;
6887
6c95b8df 6888 current_program_space->executing_startup = 0;
8bea4e01
UW
6889
6890 ALL_BREAKPOINTS (b)
6891 {
6c95b8df
PA
6892 if (b->pspace != current_program_space)
6893 continue;
6894
8bea4e01
UW
6895 if ((b->type == bp_breakpoint
6896 || b->type == bp_hardware_breakpoint)
6897 && b->enable_state == bp_startup_disabled)
6898 {
6899 b->enable_state = bp_enabled;
6900 found = 1;
6901 }
6902 }
6903
6904 if (found)
6905 breakpoint_re_set ();
6906}
6907
c906108c
SS
6908
6909/* Set a breakpoint that will evaporate an end of command
6910 at address specified by SAL.
6911 Restrict it to frame FRAME if FRAME is nonzero. */
6912
6913struct breakpoint *
a6d9a66e
UW
6914set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6915 struct frame_id frame_id, enum bptype type)
c906108c 6916{
52f0bd74 6917 struct breakpoint *b;
edb3359d
DJ
6918
6919 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6920 one. */
6921 gdb_assert (!frame_id_inlined_p (frame_id));
6922
06edf0c0 6923 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
6924 b->enable_state = bp_enabled;
6925 b->disposition = disp_donttouch;
818dd999 6926 b->frame_id = frame_id;
c906108c 6927
4a64f543
MS
6928 /* If we're debugging a multi-threaded program, then we want
6929 momentary breakpoints to be active in only a single thread of
6930 control. */
39f77062
KB
6931 if (in_thread_list (inferior_ptid))
6932 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6933
b60e7edf 6934 update_global_location_list_nothrow (1);
74960c60 6935
c906108c
SS
6936 return b;
6937}
611c83ae 6938
06edf0c0
PA
6939/* Make a momentary breakpoint based on the master breakpoint ORIG.
6940 The new breakpoint will have type TYPE, and use OPS as it
6941 breakpoint_ops. */
e58b0e63 6942
06edf0c0
PA
6943static struct breakpoint *
6944momentary_breakpoint_from_master (struct breakpoint *orig,
6945 enum bptype type,
c0a91b2b 6946 const struct breakpoint_ops *ops)
e58b0e63
PA
6947{
6948 struct breakpoint *copy;
6949
06edf0c0 6950 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 6951 copy->loc = allocate_bp_location (copy);
0e30163f 6952 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 6953
a6d9a66e 6954 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6955 copy->loc->requested_address = orig->loc->requested_address;
6956 copy->loc->address = orig->loc->address;
6957 copy->loc->section = orig->loc->section;
6c95b8df 6958 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6959
6960 if (orig->source_file == NULL)
6961 copy->source_file = NULL;
6962 else
6963 copy->source_file = xstrdup (orig->source_file);
6964
6965 copy->line_number = orig->line_number;
6966 copy->frame_id = orig->frame_id;
6967 copy->thread = orig->thread;
6c95b8df 6968 copy->pspace = orig->pspace;
e58b0e63
PA
6969
6970 copy->enable_state = bp_enabled;
6971 copy->disposition = disp_donttouch;
6972 copy->number = internal_breakpoint_number--;
6973
6974 update_global_location_list_nothrow (0);
6975 return copy;
6976}
6977
06edf0c0
PA
6978/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6979 ORIG is NULL. */
6980
6981struct breakpoint *
6982clone_momentary_breakpoint (struct breakpoint *orig)
6983{
6984 /* If there's nothing to clone, then return nothing. */
6985 if (orig == NULL)
6986 return NULL;
6987
6988 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
6989}
6990
611c83ae 6991struct breakpoint *
a6d9a66e
UW
6992set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6993 enum bptype type)
611c83ae
PA
6994{
6995 struct symtab_and_line sal;
6996
6997 sal = find_pc_line (pc, 0);
6998 sal.pc = pc;
6999 sal.section = find_pc_overlay (pc);
7000 sal.explicit_pc = 1;
7001
a6d9a66e 7002 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7003}
c906108c 7004\f
c5aa993b 7005
c906108c
SS
7006/* Tell the user we have just set a breakpoint B. */
7007
7008static void
fba45db2 7009mention (struct breakpoint *b)
c906108c 7010{
348d480f 7011 b->ops->print_mention (b);
9dc5e2a9 7012 if (ui_out_is_mi_like_p (uiout))
fb40c209 7013 return;
c906108c
SS
7014 printf_filtered ("\n");
7015}
c906108c 7016\f
c5aa993b 7017
0d381245 7018static struct bp_location *
39d61571 7019add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7020 const struct symtab_and_line *sal)
7021{
7022 struct bp_location *loc, **tmp;
7023
39d61571 7024 loc = allocate_bp_location (b);
0d381245
VP
7025 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7026 ;
7027 *tmp = loc;
a6d9a66e
UW
7028 loc->gdbarch = get_sal_arch (*sal);
7029 if (!loc->gdbarch)
7030 loc->gdbarch = b->gdbarch;
0d381245 7031 loc->requested_address = sal->pc;
a6d9a66e
UW
7032 loc->address = adjust_breakpoint_address (loc->gdbarch,
7033 loc->requested_address, b->type);
6c95b8df
PA
7034 loc->pspace = sal->pspace;
7035 gdb_assert (loc->pspace != NULL);
0d381245
VP
7036 loc->section = sal->section;
7037
0e30163f
JK
7038 set_breakpoint_location_function (loc,
7039 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7040 return loc;
7041}
514f746b
AR
7042\f
7043
7044/* Return 1 if LOC is pointing to a permanent breakpoint,
7045 return 0 otherwise. */
7046
7047static int
7048bp_loc_is_permanent (struct bp_location *loc)
7049{
7050 int len;
7051 CORE_ADDR addr;
7052 const gdb_byte *brk;
7053 gdb_byte *target_mem;
939c61fa
JK
7054 struct cleanup *cleanup;
7055 int retval = 0;
514f746b
AR
7056
7057 gdb_assert (loc != NULL);
7058
7059 addr = loc->address;
a6d9a66e 7060 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7061
939c61fa
JK
7062 /* Software breakpoints unsupported? */
7063 if (brk == NULL)
7064 return 0;
7065
514f746b
AR
7066 target_mem = alloca (len);
7067
939c61fa
JK
7068 /* Enable the automatic memory restoration from breakpoints while
7069 we read the memory. Otherwise we could say about our temporary
7070 breakpoints they are permanent. */
6c95b8df
PA
7071 cleanup = save_current_space_and_thread ();
7072
7073 switch_to_program_space_and_thread (loc->pspace);
7074 make_show_memory_breakpoints_cleanup (0);
939c61fa 7075
514f746b
AR
7076 if (target_read_memory (loc->address, target_mem, len) == 0
7077 && memcmp (target_mem, brk, len) == 0)
939c61fa 7078 retval = 1;
514f746b 7079
939c61fa
JK
7080 do_cleanups (cleanup);
7081
7082 return retval;
514f746b
AR
7083}
7084
7085
c3f6f71d 7086
018d34a4
VP
7087/* Create a breakpoint with SAL as location. Use ADDR_STRING
7088 as textual description of the location, and COND_STRING
db107f19 7089 as condition expression. */
018d34a4
VP
7090
7091static void
d9b3f62e
PA
7092init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7093 struct symtabs_and_lines sals, char *addr_string,
7094 char *cond_string,
7095 enum bptype type, enum bpdisp disposition,
7096 int thread, int task, int ignore_count,
c0a91b2b 7097 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7098 int enabled, int internal, int display_canonical)
018d34a4 7099{
0d381245 7100 int i;
018d34a4
VP
7101
7102 if (type == bp_hardware_breakpoint)
7103 {
7104 int i = hw_breakpoint_used_count ();
7105 int target_resources_ok =
d92524f1 7106 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7107 i + 1, 0);
7108 if (target_resources_ok == 0)
7109 error (_("No hardware breakpoint support in the target."));
7110 else if (target_resources_ok < 0)
7111 error (_("Hardware breakpoints used exceeds limit."));
7112 }
7113
6c95b8df
PA
7114 gdb_assert (sals.nelts > 0);
7115
0d381245
VP
7116 for (i = 0; i < sals.nelts; ++i)
7117 {
7118 struct symtab_and_line sal = sals.sals[i];
7119 struct bp_location *loc;
7120
7121 if (from_tty)
5af949e3
UW
7122 {
7123 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7124 if (!loc_gdbarch)
7125 loc_gdbarch = gdbarch;
7126
7127 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7128 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7129 }
0d381245
VP
7130
7131 if (i == 0)
7132 {
d9b3f62e 7133 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7134 b->thread = thread;
4a306c9a 7135 b->task = task;
018d34a4 7136
0d381245
VP
7137 b->cond_string = cond_string;
7138 b->ignore_count = ignore_count;
41447f92 7139 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7140 b->disposition = disposition;
6c95b8df
PA
7141 b->pspace = sals.sals[0].pspace;
7142
0fb4aa4b
PA
7143 if (type == bp_static_tracepoint)
7144 {
d9b3f62e 7145 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7146 struct static_tracepoint_marker marker;
7147
7148 if (is_marker_spec (addr_string))
7149 {
7150 /* We already know the marker exists, otherwise, we
7151 wouldn't see a sal for it. */
7152 char *p = &addr_string[3];
7153 char *endp;
7154 char *marker_str;
7155 int i;
7156
e9cafbcc 7157 p = skip_spaces (p);
0fb4aa4b 7158
e9cafbcc 7159 endp = skip_to_space (p);
0fb4aa4b
PA
7160
7161 marker_str = savestring (p, endp - p);
d9b3f62e 7162 t->static_trace_marker_id = marker_str;
0fb4aa4b 7163
3e43a32a
MS
7164 printf_filtered (_("Probed static tracepoint "
7165 "marker \"%s\"\n"),
d9b3f62e 7166 t->static_trace_marker_id);
0fb4aa4b
PA
7167 }
7168 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7169 {
d9b3f62e 7170 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7171 release_static_tracepoint_marker (&marker);
7172
3e43a32a
MS
7173 printf_filtered (_("Probed static tracepoint "
7174 "marker \"%s\"\n"),
d9b3f62e 7175 t->static_trace_marker_id);
0fb4aa4b
PA
7176 }
7177 else
3e43a32a
MS
7178 warning (_("Couldn't determine the static "
7179 "tracepoint marker to probe"));
0fb4aa4b
PA
7180 }
7181
6c95b8df 7182 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7183 && (b->type == bp_breakpoint
7184 || b->type == bp_hardware_breakpoint))
7185 b->enable_state = bp_startup_disabled;
7186
0d381245
VP
7187 loc = b->loc;
7188 }
7189 else
018d34a4 7190 {
39d61571 7191 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7192 }
7193
514f746b
AR
7194 if (bp_loc_is_permanent (loc))
7195 make_breakpoint_permanent (b);
7196
0d381245
VP
7197 if (b->cond_string)
7198 {
7199 char *arg = b->cond_string;
d32a6982 7200 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7201 if (*arg)
db107f19 7202 error (_("Garbage %s follows condition"), arg);
018d34a4 7203 }
0d381245 7204 }
018d34a4 7205
56435ebe 7206 b->display_canonical = display_canonical;
018d34a4
VP
7207 if (addr_string)
7208 b->addr_string = addr_string;
7209 else
7210 /* addr_string has to be used or breakpoint_re_set will delete
7211 me. */
5af949e3
UW
7212 b->addr_string
7213 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
d9b3f62e 7214}
018d34a4 7215
d9b3f62e
PA
7216static void
7217create_breakpoint_sal (struct gdbarch *gdbarch,
7218 struct symtabs_and_lines sals, char *addr_string,
7219 char *cond_string,
7220 enum bptype type, enum bpdisp disposition,
7221 int thread, int task, int ignore_count,
c0a91b2b 7222 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7223 int enabled, int internal, int display_canonical)
7224{
7225 struct breakpoint *b;
7226 struct cleanup *old_chain;
7227
7228 if (is_tracepoint_type (type))
7229 {
7230 struct tracepoint *t;
7231
7232 t = XCNEW (struct tracepoint);
7233 b = &t->base;
7234 }
7235 else
7236 b = XNEW (struct breakpoint);
7237
7238 old_chain = make_cleanup (xfree, b);
7239
7240 init_breakpoint_sal (b, gdbarch,
7241 sals, addr_string,
7242 cond_string,
7243 type, disposition,
7244 thread, task, ignore_count,
7245 ops, from_tty,
7246 enabled, internal, display_canonical);
7247 discard_cleanups (old_chain);
7248
7249 install_breakpoint (internal, b);
018d34a4
VP
7250}
7251
ed0616c6
VP
7252/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7253 elements to fill the void space. */
2c0b251b
PA
7254static void
7255remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7256{
7257 int i = index_to_remove+1;
7258 int last_index = sal->nelts-1;
7259
7260 for (;i <= last_index; ++i)
7261 sal->sals[i-1] = sal->sals[i];
7262
7263 --(sal->nelts);
7264}
7265
6c95b8df
PA
7266/* If appropriate, obtains all sals that correspond to the same file
7267 and line as SAL, in all program spaces. Users debugging with IDEs,
7268 will want to set a breakpoint at foo.c:line, and not really care
7269 about program spaces. This is done only if SAL does not have
7270 explicit PC and has line and file information. If we got just a
7271 single expanded sal, return the original.
ed0616c6 7272
6c95b8df
PA
7273 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7274 which the name of enclosing function is different from SAL. This
7275 makes sure that if we have breakpoint originally set in template
7276 instantiation, say foo<int>(), we won't expand SAL to locations at
7277 the same line in all existing instantiations of 'foo'. */
ed0616c6 7278
2c0b251b 7279static struct symtabs_and_lines
ed0616c6
VP
7280expand_line_sal_maybe (struct symtab_and_line sal)
7281{
7282 struct symtabs_and_lines expanded;
7283 CORE_ADDR original_pc = sal.pc;
7284 char *original_function = NULL;
7285 int found;
7286 int i;
6c95b8df 7287 struct cleanup *old_chain;
ed0616c6
VP
7288
7289 /* If we have explicit pc, don't expand.
7290 If we have no line number, we can't expand. */
7291 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7292 {
7293 expanded.nelts = 1;
7294 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7295 expanded.sals[0] = sal;
7296 return expanded;
7297 }
7298
7299 sal.pc = 0;
6c95b8df
PA
7300
7301 old_chain = save_current_space_and_thread ();
7302
7303 switch_to_program_space_and_thread (sal.pspace);
7304
ed0616c6 7305 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7306
7307 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7308 expanded = expand_line_sal (sal);
6c95b8df 7309
ed0616c6
VP
7310 if (expanded.nelts == 1)
7311 {
3dba1c98
JB
7312 /* We had one sal, we got one sal. Return that sal, adjusting it
7313 past the function prologue if necessary. */
ed0616c6
VP
7314 xfree (expanded.sals);
7315 expanded.nelts = 1;
7316 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7317 sal.pc = original_pc;
7318 expanded.sals[0] = sal;
3dba1c98 7319 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7320 do_cleanups (old_chain);
ed0616c6
VP
7321 return expanded;
7322 }
7323
7324 if (!sal.explicit_line)
7325 {
7326 CORE_ADDR func_addr, func_end;
7327 for (i = 0; i < expanded.nelts; ++i)
7328 {
7329 CORE_ADDR pc = expanded.sals[i].pc;
7330 char *this_function;
6c95b8df
PA
7331
7332 /* We need to switch threads as well since we're about to
7333 read memory. */
7334 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7335
ed0616c6
VP
7336 if (find_pc_partial_function (pc, &this_function,
7337 &func_addr, &func_end))
7338 {
059fb39f
PM
7339 if (this_function
7340 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7341 {
7342 remove_sal (&expanded, i);
7343 --i;
7344 }
ed0616c6
VP
7345 }
7346 }
7347 }
059acae7
UW
7348
7349 /* Skip the function prologue if necessary. */
7350 for (i = 0; i < expanded.nelts; ++i)
7351 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7352
6c95b8df
PA
7353 do_cleanups (old_chain);
7354
ed0616c6
VP
7355 if (expanded.nelts <= 1)
7356 {
e5dd4106 7357 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7358 then something is really wrong. Fix that by returning the
7359 original sal. */
7360
ed0616c6
VP
7361 xfree (expanded.sals);
7362 expanded.nelts = 1;
7363 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7364 sal.pc = original_pc;
7365 expanded.sals[0] = sal;
7366 return expanded;
7367 }
7368
7369 if (original_pc)
7370 {
7371 found = 0;
7372 for (i = 0; i < expanded.nelts; ++i)
7373 if (expanded.sals[i].pc == original_pc)
7374 {
7375 found = 1;
7376 break;
7377 }
7378 gdb_assert (found);
7379 }
7380
7381 return expanded;
7382}
7383
018d34a4
VP
7384/* Add SALS.nelts breakpoints to the breakpoint table. For each
7385 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7386 value. COND_STRING, if not NULL, specified the condition to be
7387 used for all breakpoints. Essentially the only case where
7388 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7389 function. In that case, it's still not possible to specify
7390 separate conditions for different overloaded functions, so
7391 we take just a single condition string.
7392
c3f6f71d 7393 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7394 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7395 array contents). If the function fails (error() is called), the
7396 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7397 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7398
7399static void
8cdf0e15 7400create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7401 struct symtabs_and_lines sals,
7402 struct linespec_result *canonical,
8cdf0e15
VP
7403 char *cond_string,
7404 enum bptype type, enum bpdisp disposition,
7405 int thread, int task, int ignore_count,
c0a91b2b 7406 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7407 int enabled, int internal)
c906108c 7408{
018d34a4 7409 int i;
cc59ec59 7410
018d34a4 7411 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7412 {
ed0616c6
VP
7413 struct symtabs_and_lines expanded =
7414 expand_line_sal_maybe (sals.sals[i]);
0d381245 7415
7efd8fc2 7416 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7417 cond_string, type, disposition,
84f4c1fe 7418 thread, task, ignore_count, ops,
56435ebe
TT
7419 from_tty, enabled, internal,
7420 canonical->special_display);
c3f6f71d 7421 }
c3f6f71d 7422}
c906108c 7423
9998af43 7424/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7425 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7426 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7427 address strings. ADDRESS points to the end of the SAL.
7428
7429 The array and the line spec strings are allocated on the heap, it is
7430 the caller's responsibility to free them. */
c906108c 7431
b9362cc7 7432static void
c3f6f71d
JM
7433parse_breakpoint_sals (char **address,
7434 struct symtabs_and_lines *sals,
58438ac1 7435 struct linespec_result *canonical)
c3f6f71d
JM
7436{
7437 char *addr_start = *address;
cc59ec59 7438
c3f6f71d 7439 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7440 breakpoint. */
c3f6f71d
JM
7441 if ((*address) == NULL
7442 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7443 {
7444 if (default_breakpoint_valid)
7445 {
c3f6f71d 7446 struct symtab_and_line sal;
cc59ec59 7447
4a64f543 7448 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7449 sals->sals = (struct symtab_and_line *)
c906108c
SS
7450 xmalloc (sizeof (struct symtab_and_line));
7451 sal.pc = default_breakpoint_address;
7452 sal.line = default_breakpoint_line;
7453 sal.symtab = default_breakpoint_symtab;
6c95b8df 7454 sal.pspace = default_breakpoint_pspace;
c5aa993b 7455 sal.section = find_pc_overlay (sal.pc);
00903456 7456
4a64f543
MS
7457 /* "break" without arguments is equivalent to "break *PC"
7458 where PC is the default_breakpoint_address. So make sure
7459 to set sal.explicit_pc to prevent GDB from trying to
7460 expand the list of sals to include all other instances
7461 with the same symtab and line. */
00903456
JK
7462 sal.explicit_pc = 1;
7463
c3f6f71d
JM
7464 sals->sals[0] = sal;
7465 sals->nelts = 1;
c906108c
SS
7466 }
7467 else
8a3fe4f8 7468 error (_("No default breakpoint address now."));
c906108c
SS
7469 }
7470 else
7471 {
c906108c 7472 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7473 current_source_symtab (which is decode_line_1's default).
7474 This should produce the results we want almost all of the
7475 time while leaving default_breakpoint_* alone.
7476
1aeae86e
AF
7477 ObjC: However, don't match an Objective-C method name which
7478 may have a '+' or '-' succeeded by a '[' */
0378c332 7479
c214a6fd 7480 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7481
c906108c 7482 if (default_breakpoint_valid
0378c332 7483 && (!cursal.symtab
1aeae86e
AF
7484 || ((strchr ("+-", (*address)[0]) != NULL)
7485 && ((*address)[1] != '['))))
c3f6f71d 7486 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7487 default_breakpoint_line, canonical);
c906108c 7488 else
0101ce28 7489 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7490 canonical);
c906108c 7491 }
4a64f543 7492 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7493 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7494 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7495 if (addr_start != (*address))
c906108c 7496 {
c3f6f71d 7497 int i;
cc59ec59 7498
c3f6f71d 7499 for (i = 0; i < sals->nelts; i++)
c906108c 7500 {
4a64f543 7501 /* Add the string if not present. */
7efd8fc2
TT
7502 if (canonical->canonical[i] == NULL)
7503 canonical->canonical[i] = savestring (addr_start,
7504 (*address) - addr_start);
c906108c
SS
7505 }
7506 }
c3f6f71d 7507}
c906108c 7508
c906108c 7509
c3f6f71d 7510/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7511 inserted as a breakpoint. If it can't throw an error. */
c906108c 7512
b9362cc7 7513static void
23e7acfb 7514breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7515{
7516 int i;
cc59ec59 7517
c3f6f71d 7518 for (i = 0; i < sals->nelts; i++)
ee53e872 7519 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7520}
7521
7a697b8d
SS
7522/* Fast tracepoints may have restrictions on valid locations. For
7523 instance, a fast tracepoint using a jump instead of a trap will
7524 likely have to overwrite more bytes than a trap would, and so can
7525 only be placed where the instruction is longer than the jump, or a
7526 multi-instruction sequence does not have a jump into the middle of
7527 it, etc. */
7528
7529static void
7530check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7531 struct symtabs_and_lines *sals)
7532{
7533 int i, rslt;
7534 struct symtab_and_line *sal;
7535 char *msg;
7536 struct cleanup *old_chain;
7537
7538 for (i = 0; i < sals->nelts; i++)
7539 {
7540 sal = &sals->sals[i];
7541
7542 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7543 NULL, &msg);
7544 old_chain = make_cleanup (xfree, msg);
7545
7546 if (!rslt)
7547 error (_("May not have a fast tracepoint at 0x%s%s"),
7548 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7549
7550 do_cleanups (old_chain);
7551 }
7552}
7553
018d34a4
VP
7554/* Given TOK, a string specification of condition and thread, as
7555 accepted by the 'break' command, extract the condition
7556 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7557 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7558 If no condition is found, *COND_STRING is set to NULL.
7559 If no thread is found, *THREAD is set to -1. */
7560static void
7561find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7562 char **cond_string, int *thread, int *task)
018d34a4
VP
7563{
7564 *cond_string = NULL;
7565 *thread = -1;
7566 while (tok && *tok)
7567 {
7568 char *end_tok;
7569 int toklen;
7570 char *cond_start = NULL;
7571 char *cond_end = NULL;
cc59ec59 7572
e9cafbcc 7573 tok = skip_spaces (tok);
018d34a4 7574
e9cafbcc 7575 end_tok = skip_to_space (tok);
018d34a4
VP
7576
7577 toklen = end_tok - tok;
7578
7579 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7580 {
f7545552
TT
7581 struct expression *expr;
7582
018d34a4 7583 tok = cond_start = end_tok + 1;
f7545552
TT
7584 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7585 xfree (expr);
018d34a4
VP
7586 cond_end = tok;
7587 *cond_string = savestring (cond_start,
7588 cond_end - cond_start);
7589 }
7590 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7591 {
7592 char *tmptok;
7593
7594 tok = end_tok + 1;
7595 tmptok = tok;
7596 *thread = strtol (tok, &tok, 0);
7597 if (tok == tmptok)
7598 error (_("Junk after thread keyword."));
7599 if (!valid_thread_id (*thread))
7600 error (_("Unknown thread %d."), *thread);
7601 }
4a306c9a
JB
7602 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7603 {
7604 char *tmptok;
7605
7606 tok = end_tok + 1;
7607 tmptok = tok;
7608 *task = strtol (tok, &tok, 0);
7609 if (tok == tmptok)
7610 error (_("Junk after task keyword."));
7611 if (!valid_task_id (*task))
b6199126 7612 error (_("Unknown task %d."), *task);
4a306c9a 7613 }
018d34a4
VP
7614 else
7615 error (_("Junk at end of arguments."));
7616 }
7617}
7618
0fb4aa4b
PA
7619/* Decode a static tracepoint marker spec. */
7620
7621static struct symtabs_and_lines
7622decode_static_tracepoint_spec (char **arg_p)
7623{
7624 VEC(static_tracepoint_marker_p) *markers = NULL;
7625 struct symtabs_and_lines sals;
7626 struct symtab_and_line sal;
7627 struct symbol *sym;
7628 struct cleanup *old_chain;
7629 char *p = &(*arg_p)[3];
7630 char *endp;
7631 char *marker_str;
7632 int i;
7633
e9cafbcc 7634 p = skip_spaces (p);
0fb4aa4b 7635
e9cafbcc 7636 endp = skip_to_space (p);
0fb4aa4b
PA
7637
7638 marker_str = savestring (p, endp - p);
7639 old_chain = make_cleanup (xfree, marker_str);
7640
7641 markers = target_static_tracepoint_markers_by_strid (marker_str);
7642 if (VEC_empty(static_tracepoint_marker_p, markers))
7643 error (_("No known static tracepoint marker named %s"), marker_str);
7644
7645 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7646 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7647
7648 for (i = 0; i < sals.nelts; i++)
7649 {
7650 struct static_tracepoint_marker *marker;
7651
7652 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7653
7654 init_sal (&sals.sals[i]);
7655
7656 sals.sals[i] = find_pc_line (marker->address, 0);
7657 sals.sals[i].pc = marker->address;
7658
7659 release_static_tracepoint_marker (marker);
7660 }
7661
7662 do_cleanups (old_chain);
7663
7664 *arg_p = endp;
7665 return sals;
7666}
7667
fd9b8c24
PA
7668/* Set a breakpoint. This function is shared between CLI and MI
7669 functions for setting a breakpoint. This function has two major
7670 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7671 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7672 breakpoint location, address and thread. Otherwise, ARG is just
7673 the location of breakpoint, with condition and thread specified by
7674 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7675 the breakpoint number will be allocated from the internal
7676 breakpoint count. Returns true if any breakpoint was created;
7677 false otherwise. */
0101ce28 7678
8cdf0e15
VP
7679int
7680create_breakpoint (struct gdbarch *gdbarch,
7681 char *arg, char *cond_string, int thread,
7682 int parse_condition_and_thread,
0fb4aa4b 7683 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7684 int ignore_count,
7685 enum auto_boolean pending_break_support,
c0a91b2b 7686 const struct breakpoint_ops *ops,
84f4c1fe 7687 int from_tty, int enabled, int internal)
c3f6f71d 7688{
b78a6381 7689 volatile struct gdb_exception e;
c3f6f71d 7690 struct symtabs_and_lines sals;
0101ce28 7691 struct symtab_and_line pending_sal;
0101ce28 7692 char *copy_arg;
c3f6f71d 7693 char *addr_start = arg;
7efd8fc2 7694 struct linespec_result canonical;
c3f6f71d 7695 struct cleanup *old_chain;
80c99de1 7696 struct cleanup *bkpt_chain = NULL;
05ff989b 7697 int i;
0101ce28 7698 int pending = 0;
4a306c9a 7699 int task = 0;
86b17b60 7700 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7701
348d480f
PA
7702 gdb_assert (ops != NULL);
7703
c3f6f71d
JM
7704 sals.sals = NULL;
7705 sals.nelts = 0;
7efd8fc2 7706 init_linespec_result (&canonical);
c3f6f71d 7707
0fb4aa4b
PA
7708 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7709 {
7710 int i;
7711
7712 sals = decode_static_tracepoint_spec (&arg);
7713
7714 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7715 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7716 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7717 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7718 goto done;
7719 }
7720
b78a6381
TT
7721 TRY_CATCH (e, RETURN_MASK_ALL)
7722 {
58438ac1 7723 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7724 }
0101ce28
JJ
7725
7726 /* If caller is interested in rc value from parse, set value. */
05ff989b 7727 switch (e.reason)
0101ce28 7728 {
05ff989b 7729 case RETURN_QUIT:
98deb0da 7730 throw_exception (e);
05ff989b
AC
7731 case RETURN_ERROR:
7732 switch (e.error)
0101ce28 7733 {
05ff989b 7734 case NOT_FOUND_ERROR:
0101ce28 7735
05ff989b
AC
7736 /* If pending breakpoint support is turned off, throw
7737 error. */
fa8d40ab
JJ
7738
7739 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7740 throw_exception (e);
7741
7742 exception_print (gdb_stderr, e);
fa8d40ab 7743
05ff989b
AC
7744 /* If pending breakpoint support is auto query and the user
7745 selects no, then simply return the error code. */
059fb39f 7746 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7747 && !nquery (_("Make breakpoint pending on "
7748 "future shared library load? ")))
fd9b8c24 7749 return 0;
fa8d40ab 7750
05ff989b
AC
7751 /* At this point, either the user was queried about setting
7752 a pending breakpoint and selected yes, or pending
7753 breakpoint behavior is on and thus a pending breakpoint
7754 is defaulted on behalf of the user. */
0101ce28 7755 copy_arg = xstrdup (addr_start);
7efd8fc2 7756 canonical.canonical = &copy_arg;
0101ce28
JJ
7757 sals.nelts = 1;
7758 sals.sals = &pending_sal;
7759 pending_sal.pc = 0;
7760 pending = 1;
05ff989b
AC
7761 break;
7762 default:
98deb0da 7763 throw_exception (e);
0101ce28 7764 }
2abae994 7765 break;
05ff989b
AC
7766 default:
7767 if (!sals.nelts)
fd9b8c24 7768 return 0;
0101ce28 7769 }
c3f6f71d 7770
0fb4aa4b
PA
7771 done:
7772
4a64f543 7773 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7774 old_chain = make_cleanup (null_cleanup, 0);
7775
0101ce28
JJ
7776 if (!pending)
7777 {
7778 /* Make sure that all storage allocated to SALS gets freed. */
7779 make_cleanup (xfree, sals.sals);
7780
7efd8fc2
TT
7781 /* Cleanup the canonical array but not its contents. */
7782 make_cleanup (xfree, canonical.canonical);
0101ce28 7783 }
c3f6f71d 7784
c3f6f71d
JM
7785 /* ----------------------------- SNIP -----------------------------
7786 Anything added to the cleanup chain beyond this point is assumed
7787 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7788 then the memory is not reclaimed. */
7789 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7790
7efd8fc2 7791 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7792 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7793 for (i = 0; i < sals.nelts; i++)
7794 {
7efd8fc2
TT
7795 if (canonical.canonical[i] != NULL)
7796 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7797 }
7798
7799 /* Resolve all line numbers to PC's and verify that the addresses
7800 are ok for the target. */
0101ce28 7801 if (!pending)
23e7acfb 7802 breakpoint_sals_to_pc (&sals);
c3f6f71d 7803
7a697b8d
SS
7804 /* Fast tracepoints may have additional restrictions on location. */
7805 if (type_wanted == bp_fast_tracepoint)
7806 check_fast_tracepoint_sals (gdbarch, &sals);
7807
c3f6f71d
JM
7808 /* Verify that condition can be parsed, before setting any
7809 breakpoints. Allocate a separate condition expression for each
4a64f543 7810 breakpoint. */
0101ce28 7811 if (!pending)
c3f6f71d 7812 {
2f069f6f 7813 if (parse_condition_and_thread)
72b2ff0e
VP
7814 {
7815 /* Here we only parse 'arg' to separate condition
7816 from thread number, so parsing in context of first
7817 sal is OK. When setting the breakpoint we'll
7818 re-parse it in context of each sal. */
7819 cond_string = NULL;
7820 thread = -1;
4a306c9a
JB
7821 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7822 &thread, &task);
72b2ff0e
VP
7823 if (cond_string)
7824 make_cleanup (xfree, cond_string);
7825 }
2f069f6f 7826 else
72b2ff0e
VP
7827 {
7828 /* Create a private copy of condition string. */
7829 if (cond_string)
7830 {
7831 cond_string = xstrdup (cond_string);
7832 make_cleanup (xfree, cond_string);
7833 }
7834 }
0fb4aa4b
PA
7835
7836 /* If the user is creating a static tracepoint by marker id
7837 (strace -m MARKER_ID), then store the sals index, so that
7838 breakpoint_re_set can try to match up which of the newly
7839 found markers corresponds to this one, and, don't try to
7840 expand multiple locations for each sal, given than SALS
7841 already should contain all sals for MARKER_ID. */
7842 if (type_wanted == bp_static_tracepoint
7efd8fc2 7843 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
7844 {
7845 int i;
7846
7847 for (i = 0; i < sals.nelts; ++i)
7848 {
7849 struct symtabs_and_lines expanded;
d9b3f62e 7850 struct tracepoint *tp;
0fb4aa4b
PA
7851 struct cleanup *old_chain;
7852
7853 expanded.nelts = 1;
7854 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7855 expanded.sals[0] = sals.sals[i];
7856 old_chain = make_cleanup (xfree, expanded.sals);
7857
d9b3f62e
PA
7858 tp = XCNEW (struct tracepoint);
7859 init_breakpoint_sal (&tp->base, gdbarch, expanded,
7860 canonical.canonical[i],
7861 cond_string, type_wanted,
7862 tempflag ? disp_del : disp_donttouch,
7863 thread, task, ignore_count, ops,
7864 from_tty, enabled, internal,
7865 canonical.special_display);
0fb4aa4b
PA
7866 /* Given that its possible to have multiple markers with
7867 the same string id, if the user is creating a static
7868 tracepoint by marker id ("strace -m MARKER_ID"), then
7869 store the sals index, so that breakpoint_re_set can
7870 try to match up which of the newly found markers
7871 corresponds to this one */
7872 tp->static_trace_marker_id_idx = i;
d9b3f62e
PA
7873
7874 install_breakpoint (internal, &tp->base);
7875
7876 do_cleanups (old_chain);
0fb4aa4b
PA
7877 }
7878 }
7879 else
7efd8fc2 7880 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
7881 type_wanted,
7882 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7883 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7884 enabled, internal);
c906108c 7885 }
0101ce28
JJ
7886 else
7887 {
0101ce28
JJ
7888 struct breakpoint *b;
7889
0101ce28
JJ
7890 make_cleanup (xfree, copy_arg);
7891
348d480f 7892 b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
84f4c1fe 7893 set_breakpoint_number (internal, b);
72b2ff0e 7894 b->thread = -1;
7efd8fc2 7895 b->addr_string = canonical.canonical[0];
72b2ff0e 7896 b->cond_string = NULL;
0101ce28 7897 b->ignore_count = ignore_count;
0101ce28 7898 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7899 b->condition_not_parsed = 1;
41447f92 7900 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7901 b->pspace = current_program_space;
84f4c1fe 7902 b->py_bp_object = NULL;
74960c60 7903
6c95b8df 7904 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7905 && (b->type == bp_breakpoint
7906 || b->type == bp_hardware_breakpoint))
7907 b->enable_state = bp_startup_disabled;
7908
8d3788bd 7909 if (!internal)
84f4c1fe
PM
7910 /* Do not mention breakpoints with a negative number,
7911 but do notify observers. */
8d3788bd
VP
7912 mention (b);
7913 observer_notify_breakpoint_created (b);
0101ce28
JJ
7914 }
7915
c3f6f71d 7916 if (sals.nelts > 1)
95a42b64 7917 {
3e43a32a
MS
7918 warning (_("Multiple breakpoints were set.\nUse the "
7919 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 7920 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
7921 }
7922
80c99de1
PA
7923 /* That's it. Discard the cleanups for data inserted into the
7924 breakpoint. */
7925 discard_cleanups (bkpt_chain);
7926 /* But cleanup everything else. */
c3f6f71d 7927 do_cleanups (old_chain);
217dc9e2 7928
80c99de1 7929 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 7930 update_global_location_list (1);
fd9b8c24
PA
7931
7932 return 1;
c3f6f71d 7933}
c906108c 7934
348d480f 7935/* Set a breakpoint.
72b2ff0e
VP
7936 ARG is a string describing breakpoint address,
7937 condition, and thread.
7938 FLAG specifies if a breakpoint is hardware on,
7939 and if breakpoint is temporary, using BP_HARDWARE_FLAG
7940 and BP_TEMPFLAG. */
348d480f 7941
98deb0da 7942static void
72b2ff0e 7943break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 7944{
72b2ff0e 7945 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
7946 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
7947 ? bp_hardware_breakpoint
7948 : bp_breakpoint);
c3f6f71d 7949
8cdf0e15
VP
7950 create_breakpoint (get_current_arch (),
7951 arg,
7952 NULL, 0, 1 /* parse arg */,
0fb4aa4b 7953 tempflag, type_wanted,
8cdf0e15
VP
7954 0 /* Ignore count */,
7955 pending_break_support,
348d480f 7956 &bkpt_breakpoint_ops,
8cdf0e15 7957 from_tty,
84f4c1fe
PM
7958 1 /* enabled */,
7959 0 /* internal */);
c906108c
SS
7960}
7961
c906108c
SS
7962/* Helper function for break_command_1 and disassemble_command. */
7963
7964void
fba45db2 7965resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
7966{
7967 CORE_ADDR pc;
7968
7969 if (sal->pc == 0 && sal->symtab != NULL)
7970 {
7971 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 7972 error (_("No line %d in file \"%s\"."),
c906108c
SS
7973 sal->line, sal->symtab->filename);
7974 sal->pc = pc;
6a048695 7975
4a64f543
MS
7976 /* If this SAL corresponds to a breakpoint inserted using a line
7977 number, then skip the function prologue if necessary. */
6a048695 7978 if (sal->explicit_line)
059acae7 7979 skip_prologue_sal (sal);
c906108c
SS
7980 }
7981
7982 if (sal->section == 0 && sal->symtab != NULL)
7983 {
7984 struct blockvector *bv;
c5aa993b
JM
7985 struct block *b;
7986 struct symbol *sym;
c906108c 7987
801e3a5b 7988 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
7989 if (bv != NULL)
7990 {
7f0df278 7991 sym = block_linkage_function (b);
c906108c
SS
7992 if (sym != NULL)
7993 {
7994 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 7995 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
7996 }
7997 else
7998 {
4a64f543
MS
7999 /* It really is worthwhile to have the section, so we'll
8000 just have to look harder. This case can be executed
8001 if we have line numbers but no functions (as can
8002 happen in assembly source). */
c906108c 8003
c5aa993b 8004 struct minimal_symbol *msym;
6c95b8df
PA
8005 struct cleanup *old_chain = save_current_space_and_thread ();
8006
8007 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8008
8009 msym = lookup_minimal_symbol_by_pc (sal->pc);
8010 if (msym)
714835d5 8011 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8012
8013 do_cleanups (old_chain);
c906108c
SS
8014 }
8015 }
8016 }
8017}
8018
8019void
fba45db2 8020break_command (char *arg, int from_tty)
c906108c 8021{
db107f19 8022 break_command_1 (arg, 0, from_tty);
c906108c
SS
8023}
8024
c906108c 8025void
fba45db2 8026tbreak_command (char *arg, int from_tty)
c906108c 8027{
db107f19 8028 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8029}
8030
c906108c 8031static void
fba45db2 8032hbreak_command (char *arg, int from_tty)
c906108c 8033{
db107f19 8034 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8035}
8036
8037static void
fba45db2 8038thbreak_command (char *arg, int from_tty)
c906108c 8039{
db107f19 8040 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8041}
8042
8043static void
fba45db2 8044stop_command (char *arg, int from_tty)
c906108c 8045{
a3f17187 8046 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8047Usage: stop in <function | address>\n\
a3f17187 8048 stop at <line>\n"));
c906108c
SS
8049}
8050
8051static void
fba45db2 8052stopin_command (char *arg, int from_tty)
c906108c
SS
8053{
8054 int badInput = 0;
8055
c5aa993b 8056 if (arg == (char *) NULL)
c906108c
SS
8057 badInput = 1;
8058 else if (*arg != '*')
8059 {
8060 char *argptr = arg;
8061 int hasColon = 0;
8062
4a64f543 8063 /* Look for a ':'. If this is a line number specification, then
53a5351d 8064 say it is bad, otherwise, it should be an address or
4a64f543 8065 function/method name. */
c906108c 8066 while (*argptr && !hasColon)
c5aa993b
JM
8067 {
8068 hasColon = (*argptr == ':');
8069 argptr++;
8070 }
c906108c
SS
8071
8072 if (hasColon)
c5aa993b 8073 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8074 else
c5aa993b 8075 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8076 }
8077
8078 if (badInput)
a3f17187 8079 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8080 else
db107f19 8081 break_command_1 (arg, 0, from_tty);
c906108c
SS
8082}
8083
8084static void
fba45db2 8085stopat_command (char *arg, int from_tty)
c906108c
SS
8086{
8087 int badInput = 0;
8088
c5aa993b 8089 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8090 badInput = 1;
8091 else
8092 {
8093 char *argptr = arg;
8094 int hasColon = 0;
8095
4a64f543
MS
8096 /* Look for a ':'. If there is a '::' then get out, otherwise
8097 it is probably a line number. */
c906108c 8098 while (*argptr && !hasColon)
c5aa993b
JM
8099 {
8100 hasColon = (*argptr == ':');
8101 argptr++;
8102 }
c906108c
SS
8103
8104 if (hasColon)
c5aa993b 8105 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8106 else
c5aa993b 8107 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8108 }
8109
8110 if (badInput)
a3f17187 8111 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8112 else
db107f19 8113 break_command_1 (arg, 0, from_tty);
c906108c
SS
8114}
8115
f1310107
TJB
8116/* Implement the "breakpoint_hit" breakpoint_ops method for
8117 ranged breakpoints. */
8118
8119static int
8120breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8121 struct address_space *aspace,
8122 CORE_ADDR bp_addr)
8123{
8124 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8125 bl->length, aspace, bp_addr);
8126}
8127
8128/* Implement the "resources_needed" breakpoint_ops method for
8129 ranged breakpoints. */
8130
8131static int
8132resources_needed_ranged_breakpoint (const struct bp_location *bl)
8133{
8134 return target_ranged_break_num_registers ();
8135}
8136
8137/* Implement the "print_it" breakpoint_ops method for
8138 ranged breakpoints. */
8139
8140static enum print_stop_action
348d480f 8141print_it_ranged_breakpoint (bpstat bs)
f1310107 8142{
348d480f 8143 struct breakpoint *b = bs->breakpoint_at;
f1310107
TJB
8144 struct bp_location *bl = b->loc;
8145
8146 gdb_assert (b->type == bp_hardware_breakpoint);
8147
8148 /* Ranged breakpoints have only one location. */
8149 gdb_assert (bl && bl->next == NULL);
8150
8151 annotate_breakpoint (b->number);
8152 if (b->disposition == disp_del)
8153 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8154 else
8155 ui_out_text (uiout, "\nRanged breakpoint ");
8156 if (ui_out_is_mi_like_p (uiout))
8157 {
8158 ui_out_field_string (uiout, "reason",
8159 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8160 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8161 }
8162 ui_out_field_int (uiout, "bkptno", b->number);
8163 ui_out_text (uiout, ", ");
8164
8165 return PRINT_SRC_AND_LOC;
8166}
8167
8168/* Implement the "print_one" breakpoint_ops method for
8169 ranged breakpoints. */
8170
8171static void
8172print_one_ranged_breakpoint (struct breakpoint *b,
8173 struct bp_location **last_loc)
8174{
8175 struct bp_location *bl = b->loc;
8176 struct value_print_options opts;
8177
8178 /* Ranged breakpoints have only one location. */
8179 gdb_assert (bl && bl->next == NULL);
8180
8181 get_user_print_options (&opts);
8182
8183 if (opts.addressprint)
8184 /* We don't print the address range here, it will be printed later
8185 by print_one_detail_ranged_breakpoint. */
8186 ui_out_field_skip (uiout, "addr");
8187 annotate_field (5);
8188 print_breakpoint_location (b, bl);
8189 *last_loc = bl;
8190}
8191
8192/* Implement the "print_one_detail" breakpoint_ops method for
8193 ranged breakpoints. */
8194
8195static void
8196print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8197 struct ui_out *uiout)
8198{
8199 CORE_ADDR address_start, address_end;
8200 struct bp_location *bl = b->loc;
8201 struct ui_stream *stb = ui_out_stream_new (uiout);
8202 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8203
8204 gdb_assert (bl);
8205
8206 address_start = bl->address;
8207 address_end = address_start + bl->length - 1;
8208
8209 ui_out_text (uiout, "\taddress range: ");
8210 fprintf_unfiltered (stb->stream, "[%s, %s]",
8211 print_core_address (bl->gdbarch, address_start),
8212 print_core_address (bl->gdbarch, address_end));
8213 ui_out_field_stream (uiout, "addr", stb);
8214 ui_out_text (uiout, "\n");
8215
8216 do_cleanups (cleanup);
8217}
8218
8219/* Implement the "print_mention" breakpoint_ops method for
8220 ranged breakpoints. */
8221
8222static void
8223print_mention_ranged_breakpoint (struct breakpoint *b)
8224{
8225 struct bp_location *bl = b->loc;
8226
8227 gdb_assert (bl);
8228 gdb_assert (b->type == bp_hardware_breakpoint);
8229
8230 if (ui_out_is_mi_like_p (uiout))
8231 return;
8232
8233 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8234 b->number, paddress (bl->gdbarch, bl->address),
8235 paddress (bl->gdbarch, bl->address + bl->length - 1));
8236}
8237
8238/* Implement the "print_recreate" breakpoint_ops method for
8239 ranged breakpoints. */
8240
8241static void
8242print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8243{
8244 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8245 b->addr_string_range_end);
d9b3f62e 8246 print_recreate_thread (b, fp);
f1310107
TJB
8247}
8248
8249/* The breakpoint_ops structure to be used in ranged breakpoints. */
8250
2060206e 8251static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8252
8253/* Find the address where the end of the breakpoint range should be
8254 placed, given the SAL of the end of the range. This is so that if
8255 the user provides a line number, the end of the range is set to the
8256 last instruction of the given line. */
8257
8258static CORE_ADDR
8259find_breakpoint_range_end (struct symtab_and_line sal)
8260{
8261 CORE_ADDR end;
8262
8263 /* If the user provided a PC value, use it. Otherwise,
8264 find the address of the end of the given location. */
8265 if (sal.explicit_pc)
8266 end = sal.pc;
8267 else
8268 {
8269 int ret;
8270 CORE_ADDR start;
8271
8272 ret = find_line_pc_range (sal, &start, &end);
8273 if (!ret)
8274 error (_("Could not find location of the end of the range."));
8275
8276 /* find_line_pc_range returns the start of the next line. */
8277 end--;
8278 }
8279
8280 return end;
8281}
8282
8283/* Implement the "break-range" CLI command. */
8284
8285static void
8286break_range_command (char *arg, int from_tty)
8287{
8288 char *arg_start, *addr_string_start, *addr_string_end;
8289 struct linespec_result canonical_start, canonical_end;
8290 int bp_count, can_use_bp, length;
8291 CORE_ADDR end;
8292 struct breakpoint *b;
8293 struct symtab_and_line sal_start, sal_end;
8294 struct symtabs_and_lines sals_start, sals_end;
8295 struct cleanup *cleanup_bkpt;
8296
8297 /* We don't support software ranged breakpoints. */
8298 if (target_ranged_break_num_registers () < 0)
8299 error (_("This target does not support hardware ranged breakpoints."));
8300
8301 bp_count = hw_breakpoint_used_count ();
8302 bp_count += target_ranged_break_num_registers ();
8303 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8304 bp_count, 0);
8305 if (can_use_bp < 0)
8306 error (_("Hardware breakpoints used exceeds limit."));
8307
8308 if (arg == NULL || arg[0] == '\0')
8309 error(_("No address range specified."));
8310
8311 sals_start.sals = NULL;
8312 sals_start.nelts = 0;
8313 init_linespec_result (&canonical_start);
8314
8315 while (*arg == ' ' || *arg == '\t')
8316 arg++;
8317
58438ac1 8318 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8319
8320 sal_start = sals_start.sals[0];
8321 addr_string_start = canonical_start.canonical[0];
8322 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8323 xfree (sals_start.sals);
8324 xfree (canonical_start.canonical);
8325
8326 if (arg[0] != ',')
8327 error (_("Too few arguments."));
8328 else if (sals_start.nelts == 0)
8329 error (_("Could not find location of the beginning of the range."));
8330 else if (sals_start.nelts != 1)
8331 error (_("Cannot create a ranged breakpoint with multiple locations."));
8332
8333 resolve_sal_pc (&sal_start);
8334
8335 arg++; /* Skip the comma. */
8336 while (*arg == ' ' || *arg == '\t')
8337 arg++;
8338
8339 /* Parse the end location. */
8340
8341 sals_end.sals = NULL;
8342 sals_end.nelts = 0;
8343 init_linespec_result (&canonical_end);
8344 arg_start = arg;
8345
423f41a5 8346 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8347 parse_breakpoint_sals because we need to specify the start location's
8348 symtab and line as the default symtab and line for the end of the
8349 range. This makes it possible to have ranges like "foo.c:27, +14",
8350 where +14 means 14 lines from the start location. */
8351 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8352 &canonical_end);
f1310107
TJB
8353
8354 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8355 if (canonical_end.canonical == NULL)
38a714bb 8356 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8357 /* Add the string if not present. */
8358 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8359 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8360
8361 sal_end = sals_end.sals[0];
8362 addr_string_end = canonical_end.canonical[0];
8363 make_cleanup (xfree, addr_string_end);
8364 xfree (sals_end.sals);
8365 xfree (canonical_end.canonical);
8366
8367 if (sals_end.nelts == 0)
8368 error (_("Could not find location of the end of the range."));
8369 else if (sals_end.nelts != 1)
8370 error (_("Cannot create a ranged breakpoint with multiple locations."));
8371
8372 resolve_sal_pc (&sal_end);
8373
8374 end = find_breakpoint_range_end (sal_end);
8375 if (sal_start.pc > end)
177b42fe 8376 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8377
8378 length = end - sal_start.pc + 1;
8379 if (length < 0)
8380 /* Length overflowed. */
8381 error (_("Address range too large."));
8382 else if (length == 1)
8383 {
8384 /* This range is simple enough to be handled by
8385 the `hbreak' command. */
8386 hbreak_command (addr_string_start, 1);
8387
8388 do_cleanups (cleanup_bkpt);
8389
8390 return;
8391 }
8392
8393 /* Now set up the breakpoint. */
8394 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8395 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8396 set_breakpoint_count (breakpoint_count + 1);
8397 b->number = breakpoint_count;
8398 b->disposition = disp_donttouch;
8399 b->addr_string = addr_string_start;
8400 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8401 b->loc->length = length;
8402
8403 discard_cleanups (cleanup_bkpt);
8404
8405 mention (b);
8d3788bd 8406 observer_notify_breakpoint_created (b);
f1310107
TJB
8407 update_global_location_list (1);
8408}
8409
4a64f543
MS
8410/* Return non-zero if EXP is verified as constant. Returned zero
8411 means EXP is variable. Also the constant detection may fail for
8412 some constant expressions and in such case still falsely return
8413 zero. */
65d79d4b
SDJ
8414static int
8415watchpoint_exp_is_const (const struct expression *exp)
8416{
8417 int i = exp->nelts;
8418
8419 while (i > 0)
8420 {
8421 int oplenp, argsp;
8422
8423 /* We are only interested in the descriptor of each element. */
8424 operator_length (exp, i, &oplenp, &argsp);
8425 i -= oplenp;
8426
8427 switch (exp->elts[i].opcode)
8428 {
8429 case BINOP_ADD:
8430 case BINOP_SUB:
8431 case BINOP_MUL:
8432 case BINOP_DIV:
8433 case BINOP_REM:
8434 case BINOP_MOD:
8435 case BINOP_LSH:
8436 case BINOP_RSH:
8437 case BINOP_LOGICAL_AND:
8438 case BINOP_LOGICAL_OR:
8439 case BINOP_BITWISE_AND:
8440 case BINOP_BITWISE_IOR:
8441 case BINOP_BITWISE_XOR:
8442 case BINOP_EQUAL:
8443 case BINOP_NOTEQUAL:
8444 case BINOP_LESS:
8445 case BINOP_GTR:
8446 case BINOP_LEQ:
8447 case BINOP_GEQ:
8448 case BINOP_REPEAT:
8449 case BINOP_COMMA:
8450 case BINOP_EXP:
8451 case BINOP_MIN:
8452 case BINOP_MAX:
8453 case BINOP_INTDIV:
8454 case BINOP_CONCAT:
8455 case BINOP_IN:
8456 case BINOP_RANGE:
8457 case TERNOP_COND:
8458 case TERNOP_SLICE:
8459 case TERNOP_SLICE_COUNT:
8460
8461 case OP_LONG:
8462 case OP_DOUBLE:
8463 case OP_DECFLOAT:
8464 case OP_LAST:
8465 case OP_COMPLEX:
8466 case OP_STRING:
8467 case OP_BITSTRING:
8468 case OP_ARRAY:
8469 case OP_TYPE:
8470 case OP_NAME:
8471 case OP_OBJC_NSSTRING:
8472
8473 case UNOP_NEG:
8474 case UNOP_LOGICAL_NOT:
8475 case UNOP_COMPLEMENT:
8476 case UNOP_ADDR:
8477 case UNOP_HIGH:
4a64f543
MS
8478 /* Unary, binary and ternary operators: We have to check
8479 their operands. If they are constant, then so is the
8480 result of that operation. For instance, if A and B are
8481 determined to be constants, then so is "A + B".
8482
8483 UNOP_IND is one exception to the rule above, because the
8484 value of *ADDR is not necessarily a constant, even when
8485 ADDR is. */
65d79d4b
SDJ
8486 break;
8487
8488 case OP_VAR_VALUE:
8489 /* Check whether the associated symbol is a constant.
4a64f543 8490
65d79d4b 8491 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8492 possible that a buggy compiler could mark a variable as
8493 constant even when it is not, and TYPE_CONST would return
8494 true in this case, while SYMBOL_CLASS wouldn't.
8495
8496 We also have to check for function symbols because they
8497 are always constant. */
65d79d4b
SDJ
8498 {
8499 struct symbol *s = exp->elts[i + 2].symbol;
8500
8501 if (SYMBOL_CLASS (s) != LOC_BLOCK
8502 && SYMBOL_CLASS (s) != LOC_CONST
8503 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8504 return 0;
8505 break;
8506 }
8507
8508 /* The default action is to return 0 because we are using
8509 the optimistic approach here: If we don't know something,
8510 then it is not a constant. */
8511 default:
8512 return 0;
8513 }
8514 }
8515
8516 return 1;
8517}
8518
3a5c3e22
PA
8519/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8520
8521static void
8522dtor_watchpoint (struct breakpoint *self)
8523{
8524 struct watchpoint *w = (struct watchpoint *) self;
8525
8526 xfree (w->cond_exp);
8527 xfree (w->exp);
8528 xfree (w->exp_string);
8529 xfree (w->exp_string_reparse);
8530 value_free (w->val);
8531
8532 base_breakpoint_ops.dtor (self);
8533}
8534
348d480f
PA
8535/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8536
8537static void
8538re_set_watchpoint (struct breakpoint *b)
8539{
3a5c3e22
PA
8540 struct watchpoint *w = (struct watchpoint *) b;
8541
348d480f
PA
8542 /* Watchpoint can be either on expression using entirely global
8543 variables, or it can be on local variables.
8544
8545 Watchpoints of the first kind are never auto-deleted, and even
8546 persist across program restarts. Since they can use variables
8547 from shared libraries, we need to reparse expression as libraries
8548 are loaded and unloaded.
8549
8550 Watchpoints on local variables can also change meaning as result
8551 of solib event. For example, if a watchpoint uses both a local
8552 and a global variables in expression, it's a local watchpoint,
8553 but unloading of a shared library will make the expression
8554 invalid. This is not a very common use case, but we still
8555 re-evaluate expression, to avoid surprises to the user.
8556
8557 Note that for local watchpoints, we re-evaluate it only if
8558 watchpoints frame id is still valid. If it's not, it means the
8559 watchpoint is out of scope and will be deleted soon. In fact,
8560 I'm not sure we'll ever be called in this case.
8561
8562 If a local watchpoint's frame id is still valid, then
3a5c3e22 8563 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8564
3a5c3e22
PA
8565 Don't do anything about disabled watchpoints, since they will be
8566 reevaluated again when enabled. */
8567 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8568}
8569
77b06cd7
TJB
8570/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8571
8572static int
8573insert_watchpoint (struct bp_location *bl)
8574{
3a5c3e22
PA
8575 struct watchpoint *w = (struct watchpoint *) bl->owner;
8576 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8577
8578 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8579 w->cond_exp);
77b06cd7
TJB
8580}
8581
8582/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8583
8584static int
8585remove_watchpoint (struct bp_location *bl)
8586{
3a5c3e22
PA
8587 struct watchpoint *w = (struct watchpoint *) bl->owner;
8588 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8589
8590 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8591 w->cond_exp);
e09342b5
TJB
8592}
8593
e09342b5 8594static int
348d480f
PA
8595breakpoint_hit_watchpoint (const struct bp_location *bl,
8596 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8597{
348d480f 8598 struct breakpoint *b = bl->owner;
3a5c3e22 8599 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8600
348d480f
PA
8601 /* Continuable hardware watchpoints are treated as non-existent if the
8602 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8603 some data address). Otherwise gdb won't stop on a break instruction
8604 in the code (not from a breakpoint) when a hardware watchpoint has
8605 been defined. Also skip watchpoints which we know did not trigger
8606 (did not match the data address). */
8607 if (is_hardware_watchpoint (b)
3a5c3e22 8608 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8609 return 0;
9c06b0b4 8610
348d480f 8611 return 1;
9c06b0b4
TJB
8612}
8613
348d480f
PA
8614static void
8615check_status_watchpoint (bpstat bs)
9c06b0b4 8616{
348d480f 8617 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8618
348d480f 8619 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8620}
8621
8622/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8623 hardware watchpoints. */
9c06b0b4
TJB
8624
8625static int
348d480f 8626resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8627{
3a5c3e22
PA
8628 struct watchpoint *w = (struct watchpoint *) bl->owner;
8629 int length = w->exact? 1 : bl->length;
348d480f
PA
8630
8631 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8632}
8633
8634/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8635 hardware watchpoints. */
9c06b0b4
TJB
8636
8637static int
348d480f 8638works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8639{
348d480f 8640 return b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8641}
8642
9c06b0b4 8643static enum print_stop_action
348d480f 8644print_it_watchpoint (bpstat bs)
9c06b0b4 8645{
348d480f
PA
8646 struct cleanup *old_chain;
8647 struct breakpoint *b;
8648 const struct bp_location *bl;
8649 struct ui_stream *stb;
8650 enum print_stop_action result;
3a5c3e22 8651 struct watchpoint *w;
348d480f
PA
8652
8653 gdb_assert (bs->bp_location_at != NULL);
8654
8655 bl = bs->bp_location_at;
8656 b = bs->breakpoint_at;
3a5c3e22 8657 w = (struct watchpoint *) b;
348d480f
PA
8658
8659 stb = ui_out_stream_new (uiout);
8660 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8661
8662 switch (b->type)
8663 {
348d480f 8664 case bp_watchpoint:
9c06b0b4
TJB
8665 case bp_hardware_watchpoint:
8666 annotate_watchpoint (b->number);
8667 if (ui_out_is_mi_like_p (uiout))
8668 ui_out_field_string
8669 (uiout, "reason",
8670 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8671 mention (b);
8672 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8673 ui_out_text (uiout, "\nOld value = ");
8674 watchpoint_value_print (bs->old_val, stb->stream);
8675 ui_out_field_stream (uiout, "old", stb);
8676 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8677 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8678 ui_out_field_stream (uiout, "new", stb);
8679 ui_out_text (uiout, "\n");
8680 /* More than one watchpoint may have been triggered. */
8681 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8682 break;
8683
8684 case bp_read_watchpoint:
8685 if (ui_out_is_mi_like_p (uiout))
8686 ui_out_field_string
8687 (uiout, "reason",
8688 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8689 mention (b);
8690 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8691 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8692 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8693 ui_out_field_stream (uiout, "value", stb);
8694 ui_out_text (uiout, "\n");
8695 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8696 break;
8697
8698 case bp_access_watchpoint:
348d480f
PA
8699 if (bs->old_val != NULL)
8700 {
8701 annotate_watchpoint (b->number);
8702 if (ui_out_is_mi_like_p (uiout))
8703 ui_out_field_string
8704 (uiout, "reason",
8705 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8706 mention (b);
8707 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8708 ui_out_text (uiout, "\nOld value = ");
8709 watchpoint_value_print (bs->old_val, stb->stream);
8710 ui_out_field_stream (uiout, "old", stb);
8711 ui_out_text (uiout, "\nNew value = ");
8712 }
8713 else
8714 {
8715 mention (b);
8716 if (ui_out_is_mi_like_p (uiout))
8717 ui_out_field_string
8718 (uiout, "reason",
8719 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8720 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8721 ui_out_text (uiout, "\nValue = ");
8722 }
3a5c3e22 8723 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8724 ui_out_field_stream (uiout, "new", stb);
8725 ui_out_text (uiout, "\n");
8726 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8727 break;
8728 default:
348d480f 8729 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8730 }
8731
348d480f
PA
8732 do_cleanups (old_chain);
8733 return result;
8734}
8735
8736/* Implement the "print_mention" breakpoint_ops method for hardware
8737 watchpoints. */
8738
8739static void
8740print_mention_watchpoint (struct breakpoint *b)
8741{
8742 struct cleanup *ui_out_chain;
3a5c3e22 8743 struct watchpoint *w = (struct watchpoint *) b;
348d480f
PA
8744
8745 switch (b->type)
8746 {
8747 case bp_watchpoint:
8748 ui_out_text (uiout, "Watchpoint ");
8749 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8750 break;
8751 case bp_hardware_watchpoint:
8752 ui_out_text (uiout, "Hardware watchpoint ");
8753 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8754 break;
8755 case bp_read_watchpoint:
8756 ui_out_text (uiout, "Hardware read watchpoint ");
8757 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8758 break;
8759 case bp_access_watchpoint:
8760 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8761 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8762 break;
8763 default:
8764 internal_error (__FILE__, __LINE__,
8765 _("Invalid hardware watchpoint type."));
8766 }
8767
8768 ui_out_field_int (uiout, "number", b->number);
8769 ui_out_text (uiout, ": ");
3a5c3e22 8770 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8771 do_cleanups (ui_out_chain);
8772}
8773
8774/* Implement the "print_recreate" breakpoint_ops method for
8775 watchpoints. */
8776
8777static void
8778print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8779{
3a5c3e22
PA
8780 struct watchpoint *w = (struct watchpoint *) b;
8781
348d480f
PA
8782 switch (b->type)
8783 {
8784 case bp_watchpoint:
8785 case bp_hardware_watchpoint:
8786 fprintf_unfiltered (fp, "watch");
8787 break;
8788 case bp_read_watchpoint:
8789 fprintf_unfiltered (fp, "rwatch");
8790 break;
8791 case bp_access_watchpoint:
8792 fprintf_unfiltered (fp, "awatch");
8793 break;
8794 default:
8795 internal_error (__FILE__, __LINE__,
8796 _("Invalid watchpoint type."));
8797 }
8798
3a5c3e22 8799 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8800 print_recreate_thread (b, fp);
348d480f
PA
8801}
8802
8803/* The breakpoint_ops structure to be used in hardware watchpoints. */
8804
2060206e 8805static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8806
8807/* Implement the "insert" breakpoint_ops method for
8808 masked hardware watchpoints. */
8809
8810static int
8811insert_masked_watchpoint (struct bp_location *bl)
8812{
3a5c3e22
PA
8813 struct watchpoint *w = (struct watchpoint *) bl->owner;
8814
8815 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8816 bl->watchpoint_type);
8817}
8818
8819/* Implement the "remove" breakpoint_ops method for
8820 masked hardware watchpoints. */
8821
8822static int
8823remove_masked_watchpoint (struct bp_location *bl)
8824{
3a5c3e22
PA
8825 struct watchpoint *w = (struct watchpoint *) bl->owner;
8826
8827 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8828 bl->watchpoint_type);
8829}
8830
8831/* Implement the "resources_needed" breakpoint_ops method for
8832 masked hardware watchpoints. */
8833
8834static int
8835resources_needed_masked_watchpoint (const struct bp_location *bl)
8836{
3a5c3e22
PA
8837 struct watchpoint *w = (struct watchpoint *) bl->owner;
8838
8839 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8840}
8841
8842/* Implement the "works_in_software_mode" breakpoint_ops method for
8843 masked hardware watchpoints. */
8844
8845static int
8846works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8847{
8848 return 0;
8849}
8850
8851/* Implement the "print_it" breakpoint_ops method for
8852 masked hardware watchpoints. */
8853
8854static enum print_stop_action
8855print_it_masked_watchpoint (bpstat bs)
8856{
8857 struct breakpoint *b = bs->breakpoint_at;
8858
8859 /* Masked watchpoints have only one location. */
8860 gdb_assert (b->loc && b->loc->next == NULL);
8861
8862 switch (b->type)
8863 {
8864 case bp_hardware_watchpoint:
8865 annotate_watchpoint (b->number);
8866 if (ui_out_is_mi_like_p (uiout))
8867 ui_out_field_string
8868 (uiout, "reason",
8869 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8870 break;
8871
8872 case bp_read_watchpoint:
8873 if (ui_out_is_mi_like_p (uiout))
8874 ui_out_field_string
8875 (uiout, "reason",
8876 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8877 break;
8878
8879 case bp_access_watchpoint:
8880 if (ui_out_is_mi_like_p (uiout))
8881 ui_out_field_string
8882 (uiout, "reason",
8883 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8884 break;
8885 default:
8886 internal_error (__FILE__, __LINE__,
8887 _("Invalid hardware watchpoint type."));
8888 }
8889
8890 mention (b);
9c06b0b4
TJB
8891 ui_out_text (uiout, _("\n\
8892Check the underlying instruction at PC for the memory\n\
8893address and value which triggered this watchpoint.\n"));
8894 ui_out_text (uiout, "\n");
8895
8896 /* More than one watchpoint may have been triggered. */
8897 return PRINT_UNKNOWN;
8898}
8899
8900/* Implement the "print_one_detail" breakpoint_ops method for
8901 masked hardware watchpoints. */
8902
8903static void
8904print_one_detail_masked_watchpoint (const struct breakpoint *b,
8905 struct ui_out *uiout)
8906{
3a5c3e22
PA
8907 struct watchpoint *w = (struct watchpoint *) b;
8908
9c06b0b4
TJB
8909 /* Masked watchpoints have only one location. */
8910 gdb_assert (b->loc && b->loc->next == NULL);
8911
8912 ui_out_text (uiout, "\tmask ");
3a5c3e22 8913 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
8914 ui_out_text (uiout, "\n");
8915}
8916
8917/* Implement the "print_mention" breakpoint_ops method for
8918 masked hardware watchpoints. */
8919
8920static void
8921print_mention_masked_watchpoint (struct breakpoint *b)
8922{
3a5c3e22 8923 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8924 struct cleanup *ui_out_chain;
8925
8926 switch (b->type)
8927 {
8928 case bp_hardware_watchpoint:
8929 ui_out_text (uiout, "Masked hardware watchpoint ");
8930 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8931 break;
8932 case bp_read_watchpoint:
8933 ui_out_text (uiout, "Masked hardware read watchpoint ");
8934 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8935 break;
8936 case bp_access_watchpoint:
8937 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
8938 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8939 break;
8940 default:
8941 internal_error (__FILE__, __LINE__,
8942 _("Invalid hardware watchpoint type."));
8943 }
8944
8945 ui_out_field_int (uiout, "number", b->number);
8946 ui_out_text (uiout, ": ");
3a5c3e22 8947 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
8948 do_cleanups (ui_out_chain);
8949}
8950
8951/* Implement the "print_recreate" breakpoint_ops method for
8952 masked hardware watchpoints. */
8953
8954static void
8955print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
8956{
3a5c3e22 8957 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
8958 char tmp[40];
8959
8960 switch (b->type)
8961 {
8962 case bp_hardware_watchpoint:
8963 fprintf_unfiltered (fp, "watch");
8964 break;
8965 case bp_read_watchpoint:
8966 fprintf_unfiltered (fp, "rwatch");
8967 break;
8968 case bp_access_watchpoint:
8969 fprintf_unfiltered (fp, "awatch");
8970 break;
8971 default:
8972 internal_error (__FILE__, __LINE__,
8973 _("Invalid hardware watchpoint type."));
8974 }
8975
3a5c3e22
PA
8976 sprintf_vma (tmp, w->hw_wp_mask);
8977 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 8978 print_recreate_thread (b, fp);
9c06b0b4
TJB
8979}
8980
8981/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
8982
2060206e 8983static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
8984
8985/* Tell whether the given watchpoint is a masked hardware watchpoint. */
8986
8987static int
8988is_masked_watchpoint (const struct breakpoint *b)
8989{
8990 return b->ops == &masked_watchpoint_breakpoint_ops;
8991}
8992
53a5351d
JM
8993/* accessflag: hw_write: watch write,
8994 hw_read: watch read,
8995 hw_access: watch access (read or write) */
c906108c 8996static void
84f4c1fe
PM
8997watch_command_1 (char *arg, int accessflag, int from_tty,
8998 int just_location, int internal)
c906108c 8999{
a9634178 9000 volatile struct gdb_exception e;
d983da9c 9001 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9002 struct expression *exp;
60e1c644 9003 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9004 struct value *val, *mark, *result;
c906108c 9005 struct frame_info *frame;
c906108c
SS
9006 char *exp_start = NULL;
9007 char *exp_end = NULL;
9c06b0b4
TJB
9008 char *tok, *end_tok;
9009 int toklen = -1;
c906108c
SS
9010 char *cond_start = NULL;
9011 char *cond_end = NULL;
c906108c 9012 enum bptype bp_type;
37e4754d 9013 int thread = -1;
0cf6dd15 9014 int pc = 0;
9c06b0b4
TJB
9015 /* Flag to indicate whether we are going to use masks for
9016 the hardware watchpoint. */
9017 int use_mask = 0;
9018 CORE_ADDR mask = 0;
3a5c3e22 9019 struct watchpoint *w;
c906108c 9020
37e4754d
LM
9021 /* Make sure that we actually have parameters to parse. */
9022 if (arg != NULL && arg[0] != '\0')
9023 {
9c06b0b4 9024 char *value_start;
37e4754d 9025
9c06b0b4
TJB
9026 /* Look for "parameter value" pairs at the end
9027 of the arguments string. */
9028 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9029 {
9030 /* Skip whitespace at the end of the argument list. */
9031 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9032 tok--;
9033
9034 /* Find the beginning of the last token.
9035 This is the value of the parameter. */
9036 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9037 tok--;
9038 value_start = tok + 1;
9039
9040 /* Skip whitespace. */
9041 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9042 tok--;
9043
9044 end_tok = tok;
9045
9046 /* Find the beginning of the second to last token.
9047 This is the parameter itself. */
9048 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9049 tok--;
9050 tok++;
9051 toklen = end_tok - tok + 1;
9052
9053 if (toklen == 6 && !strncmp (tok, "thread", 6))
9054 {
9055 /* At this point we've found a "thread" token, which means
9056 the user is trying to set a watchpoint that triggers
9057 only in a specific thread. */
9058 char *endp;
37e4754d 9059
9c06b0b4
TJB
9060 if (thread != -1)
9061 error(_("You can specify only one thread."));
37e4754d 9062
9c06b0b4
TJB
9063 /* Extract the thread ID from the next token. */
9064 thread = strtol (value_start, &endp, 0);
37e4754d 9065
9c06b0b4
TJB
9066 /* Check if the user provided a valid numeric value for the
9067 thread ID. */
9068 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9069 error (_("Invalid thread ID specification %s."), value_start);
9070
9071 /* Check if the thread actually exists. */
9072 if (!valid_thread_id (thread))
9073 error (_("Unknown thread %d."), thread);
9074 }
9075 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9076 {
9077 /* We've found a "mask" token, which means the user wants to
9078 create a hardware watchpoint that is going to have the mask
9079 facility. */
9080 struct value *mask_value, *mark;
37e4754d 9081
9c06b0b4
TJB
9082 if (use_mask)
9083 error(_("You can specify only one mask."));
37e4754d 9084
9c06b0b4 9085 use_mask = just_location = 1;
37e4754d 9086
9c06b0b4
TJB
9087 mark = value_mark ();
9088 mask_value = parse_to_comma_and_eval (&value_start);
9089 mask = value_as_address (mask_value);
9090 value_free_to_mark (mark);
9091 }
9092 else
9093 /* We didn't recognize what we found. We should stop here. */
9094 break;
37e4754d 9095
9c06b0b4
TJB
9096 /* Truncate the string and get rid of the "parameter value" pair before
9097 the arguments string is parsed by the parse_exp_1 function. */
9098 *tok = '\0';
9099 }
37e4754d
LM
9100 }
9101
9102 /* Parse the rest of the arguments. */
c906108c
SS
9103 innermost_block = NULL;
9104 exp_start = arg;
9105 exp = parse_exp_1 (&arg, 0, 0);
9106 exp_end = arg;
fa8a61dc
TT
9107 /* Remove trailing whitespace from the expression before saving it.
9108 This makes the eventual display of the expression string a bit
9109 prettier. */
9110 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9111 --exp_end;
9112
65d79d4b
SDJ
9113 /* Checking if the expression is not constant. */
9114 if (watchpoint_exp_is_const (exp))
9115 {
9116 int len;
9117
9118 len = exp_end - exp_start;
9119 while (len > 0 && isspace (exp_start[len - 1]))
9120 len--;
9121 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9122 }
9123
c906108c
SS
9124 exp_valid_block = innermost_block;
9125 mark = value_mark ();
a1442452 9126 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9127
9128 if (just_location)
9129 {
9c06b0b4
TJB
9130 int ret;
9131
06a64a0b 9132 exp_valid_block = NULL;
a1442452 9133 val = value_addr (result);
06a64a0b
TT
9134 release_value (val);
9135 value_free_to_mark (mark);
9c06b0b4
TJB
9136
9137 if (use_mask)
9138 {
9139 ret = target_masked_watch_num_registers (value_as_address (val),
9140 mask);
9141 if (ret == -1)
9142 error (_("This target does not support masked watchpoints."));
9143 else if (ret == -2)
9144 error (_("Invalid mask or memory region."));
9145 }
06a64a0b
TT
9146 }
9147 else if (val != NULL)
fa4727a6 9148 release_value (val);
c906108c 9149
e9cafbcc
TT
9150 tok = skip_spaces (arg);
9151 end_tok = skip_to_space (tok);
c906108c
SS
9152
9153 toklen = end_tok - tok;
9154 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9155 {
2d134ed3
PA
9156 struct expression *cond;
9157
60e1c644 9158 innermost_block = NULL;
c906108c
SS
9159 tok = cond_start = end_tok + 1;
9160 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9161
9162 /* The watchpoint expression may not be local, but the condition
9163 may still be. E.g.: `watch global if local > 0'. */
9164 cond_exp_valid_block = innermost_block;
9165
2d134ed3 9166 xfree (cond);
c906108c
SS
9167 cond_end = tok;
9168 }
9169 if (*tok)
8a3fe4f8 9170 error (_("Junk at end of command."));
c906108c 9171
53a5351d 9172 if (accessflag == hw_read)
c5aa993b 9173 bp_type = bp_read_watchpoint;
53a5351d 9174 else if (accessflag == hw_access)
c5aa993b
JM
9175 bp_type = bp_access_watchpoint;
9176 else
9177 bp_type = bp_hardware_watchpoint;
c906108c 9178
d983da9c 9179 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9180
9181 /* If the expression is "local", then set up a "watchpoint scope"
9182 breakpoint at the point where we've left the scope of the watchpoint
9183 expression. Create the scope breakpoint before the watchpoint, so
9184 that we will encounter it first in bpstat_stop_status. */
60e1c644 9185 if (exp_valid_block && frame)
d983da9c 9186 {
edb3359d
DJ
9187 if (frame_id_p (frame_unwind_caller_id (frame)))
9188 {
9189 scope_breakpoint
a6d9a66e
UW
9190 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9191 frame_unwind_caller_pc (frame),
06edf0c0
PA
9192 bp_watchpoint_scope,
9193 &momentary_breakpoint_ops);
d983da9c 9194
edb3359d 9195 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9196
edb3359d
DJ
9197 /* Automatically delete the breakpoint when it hits. */
9198 scope_breakpoint->disposition = disp_del;
d983da9c 9199
edb3359d
DJ
9200 /* Only break in the proper frame (help with recursion). */
9201 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9202
edb3359d 9203 /* Set the address at which we will stop. */
a6d9a66e
UW
9204 scope_breakpoint->loc->gdbarch
9205 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9206 scope_breakpoint->loc->requested_address
9207 = frame_unwind_caller_pc (frame);
9208 scope_breakpoint->loc->address
a6d9a66e
UW
9209 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9210 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9211 scope_breakpoint->type);
9212 }
d983da9c
DJ
9213 }
9214
c906108c 9215 /* Now set up the breakpoint. */
3a5c3e22
PA
9216
9217 w = XCNEW (struct watchpoint);
9218 b = &w->base;
348d480f 9219 if (use_mask)
3a5c3e22
PA
9220 init_raw_breakpoint_without_location (b, NULL, bp_type,
9221 &masked_watchpoint_breakpoint_ops);
348d480f 9222 else
3a5c3e22
PA
9223 init_raw_breakpoint_without_location (b, NULL, bp_type,
9224 &watchpoint_breakpoint_ops);
37e4754d 9225 b->thread = thread;
b5de0fa7 9226 b->disposition = disp_donttouch;
348d480f 9227 b->pspace = current_program_space;
3a5c3e22
PA
9228 w->exp = exp;
9229 w->exp_valid_block = exp_valid_block;
9230 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9231 if (just_location)
9232 {
9233 struct type *t = value_type (val);
9234 CORE_ADDR addr = value_as_address (val);
9235 char *name;
9236
9237 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9238 name = type_to_string (t);
9239
3a5c3e22 9240 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9241 core_addr_to_string (addr));
06a64a0b
TT
9242 xfree (name);
9243
3a5c3e22 9244 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9245 (int) (exp_end - exp_start), exp_start);
9246
06a64a0b
TT
9247 /* The above expression is in C. */
9248 b->language = language_c;
9249 }
9250 else
3a5c3e22 9251 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9252
9253 if (use_mask)
9254 {
3a5c3e22 9255 w->hw_wp_mask = mask;
9c06b0b4
TJB
9256 }
9257 else
9258 {
3a5c3e22
PA
9259 w->val = val;
9260 w->val_valid = 1;
9c06b0b4 9261 }
77b06cd7 9262
c906108c
SS
9263 if (cond_start)
9264 b->cond_string = savestring (cond_start, cond_end - cond_start);
9265 else
9266 b->cond_string = 0;
c5aa993b 9267
c906108c 9268 if (frame)
f6bc2008 9269 {
3a5c3e22
PA
9270 w->watchpoint_frame = get_frame_id (frame);
9271 w->watchpoint_thread = inferior_ptid;
f6bc2008 9272 }
c906108c 9273 else
f6bc2008 9274 {
3a5c3e22
PA
9275 w->watchpoint_frame = null_frame_id;
9276 w->watchpoint_thread = null_ptid;
f6bc2008 9277 }
c906108c 9278
d983da9c 9279 if (scope_breakpoint != NULL)
c906108c 9280 {
d983da9c
DJ
9281 /* The scope breakpoint is related to the watchpoint. We will
9282 need to act on them together. */
9283 b->related_breakpoint = scope_breakpoint;
9284 scope_breakpoint->related_breakpoint = b;
c906108c 9285 }
d983da9c 9286
06a64a0b
TT
9287 if (!just_location)
9288 value_free_to_mark (mark);
2d134ed3 9289
a9634178
TJB
9290 TRY_CATCH (e, RETURN_MASK_ALL)
9291 {
9292 /* Finally update the new watchpoint. This creates the locations
9293 that should be inserted. */
3a5c3e22 9294 update_watchpoint (w, 1);
a9634178
TJB
9295 }
9296 if (e.reason < 0)
9297 {
9298 delete_breakpoint (b);
9299 throw_exception (e);
9300 }
9301
3a5c3e22 9302 install_breakpoint (internal, b);
c906108c
SS
9303}
9304
e09342b5 9305/* Return count of debug registers needed to watch the given expression.
e09342b5 9306 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9307
c906108c 9308static int
a9634178 9309can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9310{
9311 int found_memory_cnt = 0;
2e70b7b9 9312 struct value *head = v;
c906108c
SS
9313
9314 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9315 if (!can_use_hw_watchpoints)
c906108c 9316 return 0;
c5aa993b 9317
5c44784c
JM
9318 /* Make sure that the value of the expression depends only upon
9319 memory contents, and values computed from them within GDB. If we
9320 find any register references or function calls, we can't use a
9321 hardware watchpoint.
9322
9323 The idea here is that evaluating an expression generates a series
9324 of values, one holding the value of every subexpression. (The
9325 expression a*b+c has five subexpressions: a, b, a*b, c, and
9326 a*b+c.) GDB's values hold almost enough information to establish
9327 the criteria given above --- they identify memory lvalues,
9328 register lvalues, computed values, etcetera. So we can evaluate
9329 the expression, and then scan the chain of values that leaves
9330 behind to decide whether we can detect any possible change to the
9331 expression's final value using only hardware watchpoints.
9332
9333 However, I don't think that the values returned by inferior
9334 function calls are special in any way. So this function may not
9335 notice that an expression involving an inferior function call
9336 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9337 for (; v; v = value_next (v))
c906108c 9338 {
5c44784c 9339 if (VALUE_LVAL (v) == lval_memory)
c906108c 9340 {
8464be76
DJ
9341 if (v != head && value_lazy (v))
9342 /* A lazy memory lvalue in the chain is one that GDB never
9343 needed to fetch; we either just used its address (e.g.,
9344 `a' in `a.b') or we never needed it at all (e.g., `a'
9345 in `a,b'). This doesn't apply to HEAD; if that is
9346 lazy then it was not readable, but watch it anyway. */
5c44784c 9347 ;
53a5351d 9348 else
5c44784c
JM
9349 {
9350 /* Ahh, memory we actually used! Check if we can cover
9351 it with hardware watchpoints. */
df407dfe 9352 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9353
9354 /* We only watch structs and arrays if user asked for it
9355 explicitly, never if they just happen to appear in a
9356 middle of some value chain. */
9357 if (v == head
9358 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9359 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9360 {
42ae5230 9361 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9362 int len;
9363 int num_regs;
9364
a9634178 9365 len = (target_exact_watchpoints
e09342b5
TJB
9366 && is_scalar_type_recursive (vtype))?
9367 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9368
e09342b5
TJB
9369 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9370 if (!num_regs)
2e70b7b9
MS
9371 return 0;
9372 else
e09342b5 9373 found_memory_cnt += num_regs;
2e70b7b9 9374 }
5c44784c 9375 }
c5aa993b 9376 }
5086187c
AC
9377 else if (VALUE_LVAL (v) != not_lval
9378 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9379 return 0; /* These are values from the history (e.g., $1). */
5086187c 9380 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9381 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9382 }
9383
9384 /* The expression itself looks suitable for using a hardware
9385 watchpoint, but give the target machine a chance to reject it. */
9386 return found_memory_cnt;
9387}
9388
8b93c638 9389void
84f4c1fe 9390watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9391{
84f4c1fe 9392 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9393}
9394
9395/* A helper function that looks for an argument at the start of a
9396 string. The argument must also either be at the end of the string,
9397 or be followed by whitespace. Returns 1 if it finds the argument,
9398 0 otherwise. If the argument is found, it updates *STR. */
9399
9400static int
9401check_for_argument (char **str, char *arg, int arg_len)
9402{
9403 if (strncmp (*str, arg, arg_len) == 0
9404 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9405 {
9406 *str += arg_len;
9407 return 1;
9408 }
9409 return 0;
9410}
9411
9412/* A helper function that looks for the "-location" argument and then
9413 calls watch_command_1. */
9414
9415static void
9416watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9417{
9418 int just_location = 0;
9419
9420 if (arg
9421 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9422 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9423 {
e9cafbcc 9424 arg = skip_spaces (arg);
06a64a0b
TT
9425 just_location = 1;
9426 }
9427
84f4c1fe 9428 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9429}
8926118c 9430
c5aa993b 9431static void
fba45db2 9432watch_command (char *arg, int from_tty)
c906108c 9433{
06a64a0b 9434 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9435}
9436
8b93c638 9437void
84f4c1fe 9438rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9439{
84f4c1fe 9440 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9441}
8926118c 9442
c5aa993b 9443static void
fba45db2 9444rwatch_command (char *arg, int from_tty)
c906108c 9445{
06a64a0b 9446 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9447}
9448
8b93c638 9449void
84f4c1fe 9450awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9451{
84f4c1fe 9452 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9453}
8926118c 9454
c5aa993b 9455static void
fba45db2 9456awatch_command (char *arg, int from_tty)
c906108c 9457{
06a64a0b 9458 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9459}
c906108c 9460\f
c5aa993b 9461
43ff13b4 9462/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9463 because it uses the mechanisms of breakpoints. */
9464
bfec99b2
PA
9465struct until_break_command_continuation_args
9466{
9467 struct breakpoint *breakpoint;
9468 struct breakpoint *breakpoint2;
186c406b 9469 int thread_num;
bfec99b2
PA
9470};
9471
43ff13b4 9472/* This function is called by fetch_inferior_event via the
4a64f543 9473 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9474 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9475 command. */
c2c6d25f 9476static void
fa4cd53f 9477until_break_command_continuation (void *arg, int err)
43ff13b4 9478{
bfec99b2
PA
9479 struct until_break_command_continuation_args *a = arg;
9480
9481 delete_breakpoint (a->breakpoint);
9482 if (a->breakpoint2)
9483 delete_breakpoint (a->breakpoint2);
186c406b 9484 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9485}
9486
c906108c 9487void
ae66c1fc 9488until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9489{
9490 struct symtabs_and_lines sals;
9491 struct symtab_and_line sal;
206415a3 9492 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9493 struct breakpoint *breakpoint;
f107f563 9494 struct breakpoint *breakpoint2 = NULL;
c906108c 9495 struct cleanup *old_chain;
186c406b
TT
9496 int thread;
9497 struct thread_info *tp;
c906108c
SS
9498
9499 clear_proceed_status ();
9500
9501 /* Set a breakpoint where the user wants it and at return from
4a64f543 9502 this function. */
c5aa993b 9503
c906108c
SS
9504 if (default_breakpoint_valid)
9505 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9506 default_breakpoint_line, NULL);
c906108c 9507 else
58438ac1 9508 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9509
c906108c 9510 if (sals.nelts != 1)
8a3fe4f8 9511 error (_("Couldn't get information on specified line."));
c5aa993b 9512
c906108c 9513 sal = sals.sals[0];
4a64f543 9514 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9515
c906108c 9516 if (*arg)
8a3fe4f8 9517 error (_("Junk at end of arguments."));
c5aa993b 9518
c906108c 9519 resolve_sal_pc (&sal);
c5aa993b 9520
ae66c1fc
EZ
9521 if (anywhere)
9522 /* If the user told us to continue until a specified location,
9523 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9524 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9525 null_frame_id, bp_until);
ae66c1fc 9526 else
4a64f543
MS
9527 /* Otherwise, specify the selected frame, because we want to stop
9528 only at the very same frame. */
a6d9a66e
UW
9529 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9530 get_stack_frame_id (frame),
ae66c1fc 9531 bp_until);
c5aa993b 9532
f107f563 9533 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9534
186c406b
TT
9535 tp = inferior_thread ();
9536 thread = tp->num;
9537
ae66c1fc
EZ
9538 /* Keep within the current frame, or in frames called by the current
9539 one. */
edb3359d
DJ
9540
9541 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9542 {
edb3359d
DJ
9543 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9544 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9545 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9546 sal,
edb3359d 9547 frame_unwind_caller_id (frame),
f107f563
VP
9548 bp_until);
9549 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9550
9551 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9552 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9553 }
c5aa993b 9554
c906108c 9555 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9556
4a64f543
MS
9557 /* If we are running asynchronously, and proceed call above has
9558 actually managed to start the target, arrange for breakpoints to
9559 be deleted when the target stops. Otherwise, we're already
9560 stopped and delete breakpoints via cleanup chain. */
f107f563 9561
8ea051c5 9562 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9563 {
bfec99b2
PA
9564 struct until_break_command_continuation_args *args;
9565 args = xmalloc (sizeof (*args));
f107f563 9566
bfec99b2
PA
9567 args->breakpoint = breakpoint;
9568 args->breakpoint2 = breakpoint2;
186c406b 9569 args->thread_num = thread;
f107f563
VP
9570
9571 discard_cleanups (old_chain);
95e54da7
PA
9572 add_continuation (inferior_thread (),
9573 until_break_command_continuation, args,
604ead4a 9574 xfree);
f107f563
VP
9575 }
9576 else
c5aa993b 9577 do_cleanups (old_chain);
c906108c 9578}
ae66c1fc 9579
c906108c
SS
9580/* This function attempts to parse an optional "if <cond>" clause
9581 from the arg string. If one is not found, it returns NULL.
c5aa993b 9582
c906108c
SS
9583 Else, it returns a pointer to the condition string. (It does not
9584 attempt to evaluate the string against a particular block.) And,
9585 it updates arg to point to the first character following the parsed
4a64f543 9586 if clause in the arg string. */
53a5351d 9587
c906108c 9588static char *
fba45db2 9589ep_parse_optional_if_clause (char **arg)
c906108c 9590{
c5aa993b
JM
9591 char *cond_string;
9592
9593 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9594 return NULL;
c5aa993b 9595
4a64f543 9596 /* Skip the "if" keyword. */
c906108c 9597 (*arg) += 2;
c5aa993b 9598
c906108c 9599 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9600 condition string. */
e9cafbcc 9601 *arg = skip_spaces (*arg);
c906108c 9602 cond_string = *arg;
c5aa993b 9603
4a64f543
MS
9604 /* Assume that the condition occupies the remainder of the arg
9605 string. */
c906108c 9606 (*arg) += strlen (cond_string);
c5aa993b 9607
c906108c
SS
9608 return cond_string;
9609}
c5aa993b 9610
c906108c
SS
9611/* Commands to deal with catching events, such as signals, exceptions,
9612 process start/exit, etc. */
c5aa993b
JM
9613
9614typedef enum
9615{
44feb3ce
TT
9616 catch_fork_temporary, catch_vfork_temporary,
9617 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9618}
9619catch_fork_kind;
9620
c906108c 9621static void
cc59ec59
MS
9622catch_fork_command_1 (char *arg, int from_tty,
9623 struct cmd_list_element *command)
c906108c 9624{
a6d9a66e 9625 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9626 char *cond_string = NULL;
44feb3ce
TT
9627 catch_fork_kind fork_kind;
9628 int tempflag;
9629
9630 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9631 tempflag = (fork_kind == catch_fork_temporary
9632 || fork_kind == catch_vfork_temporary);
c5aa993b 9633
44feb3ce
TT
9634 if (!arg)
9635 arg = "";
e9cafbcc 9636 arg = skip_spaces (arg);
c5aa993b 9637
c906108c 9638 /* The allowed syntax is:
c5aa993b
JM
9639 catch [v]fork
9640 catch [v]fork if <cond>
9641
4a64f543 9642 First, check if there's an if clause. */
c906108c 9643 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9644
c906108c 9645 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9646 error (_("Junk at end of arguments."));
c5aa993b 9647
c906108c 9648 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9649 and enable reporting of such events. */
c5aa993b
JM
9650 switch (fork_kind)
9651 {
44feb3ce
TT
9652 case catch_fork_temporary:
9653 case catch_fork_permanent:
a6d9a66e 9654 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9655 &catch_fork_breakpoint_ops);
c906108c 9656 break;
44feb3ce
TT
9657 case catch_vfork_temporary:
9658 case catch_vfork_permanent:
a6d9a66e 9659 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9660 &catch_vfork_breakpoint_ops);
c906108c 9661 break;
c5aa993b 9662 default:
8a3fe4f8 9663 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9664 break;
c5aa993b 9665 }
c906108c
SS
9666}
9667
9668static void
cc59ec59
MS
9669catch_exec_command_1 (char *arg, int from_tty,
9670 struct cmd_list_element *command)
c906108c 9671{
b4d90040 9672 struct exec_catchpoint *c;
a6d9a66e 9673 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9674 int tempflag;
c5aa993b 9675 char *cond_string = NULL;
c906108c 9676
44feb3ce
TT
9677 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9678
9679 if (!arg)
9680 arg = "";
e9cafbcc 9681 arg = skip_spaces (arg);
c906108c
SS
9682
9683 /* The allowed syntax is:
c5aa993b
JM
9684 catch exec
9685 catch exec if <cond>
c906108c 9686
4a64f543 9687 First, check if there's an if clause. */
c906108c
SS
9688 cond_string = ep_parse_optional_if_clause (&arg);
9689
9690 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9691 error (_("Junk at end of arguments."));
c906108c 9692
b4d90040
PA
9693 c = XNEW (struct exec_catchpoint);
9694 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9695 &catch_exec_breakpoint_ops);
9696 c->exec_pathname = NULL;
9697
3a5c3e22 9698 install_breakpoint (0, &c->base);
c906108c 9699}
c5aa993b 9700
3086aeae 9701static enum print_stop_action
348d480f 9702print_it_exception_catchpoint (bpstat bs)
3086aeae 9703{
348d480f 9704 struct breakpoint *b = bs->breakpoint_at;
ade92717 9705 int bp_temp, bp_throw;
3086aeae 9706
ade92717 9707 annotate_catchpoint (b->number);
3086aeae 9708
ade92717
AR
9709 bp_throw = strstr (b->addr_string, "throw") != NULL;
9710 if (b->loc->address != b->loc->requested_address)
9711 breakpoint_adjustment_warning (b->loc->requested_address,
9712 b->loc->address,
9713 b->number, 1);
df2b6d2d 9714 bp_temp = b->disposition == disp_del;
ade92717
AR
9715 ui_out_text (uiout,
9716 bp_temp ? "Temporary catchpoint "
9717 : "Catchpoint ");
9718 if (!ui_out_is_mi_like_p (uiout))
9719 ui_out_field_int (uiout, "bkptno", b->number);
9720 ui_out_text (uiout,
c0b37c48
AR
9721 bp_throw ? " (exception thrown), "
9722 : " (exception caught), ");
ade92717
AR
9723 if (ui_out_is_mi_like_p (uiout))
9724 {
9725 ui_out_field_string (uiout, "reason",
9726 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9727 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9728 ui_out_field_int (uiout, "bkptno", b->number);
9729 }
3086aeae
DJ
9730 return PRINT_SRC_AND_LOC;
9731}
9732
9733static void
cc59ec59
MS
9734print_one_exception_catchpoint (struct breakpoint *b,
9735 struct bp_location **last_loc)
3086aeae 9736{
79a45b7d 9737 struct value_print_options opts;
cc59ec59 9738
79a45b7d
TT
9739 get_user_print_options (&opts);
9740 if (opts.addressprint)
3086aeae
DJ
9741 {
9742 annotate_field (4);
604133b5
AR
9743 if (b->loc == NULL || b->loc->shlib_disabled)
9744 ui_out_field_string (uiout, "addr", "<PENDING>");
9745 else
5af949e3
UW
9746 ui_out_field_core_addr (uiout, "addr",
9747 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9748 }
9749 annotate_field (5);
604133b5 9750 if (b->loc)
a6d9a66e 9751 *last_loc = b->loc;
3086aeae
DJ
9752 if (strstr (b->addr_string, "throw") != NULL)
9753 ui_out_field_string (uiout, "what", "exception throw");
9754 else
9755 ui_out_field_string (uiout, "what", "exception catch");
9756}
9757
9758static void
9759print_mention_exception_catchpoint (struct breakpoint *b)
9760{
ade92717
AR
9761 int bp_temp;
9762 int bp_throw;
9763
df2b6d2d 9764 bp_temp = b->disposition == disp_del;
ade92717
AR
9765 bp_throw = strstr (b->addr_string, "throw") != NULL;
9766 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9767 : _("Catchpoint "));
9768 ui_out_field_int (uiout, "bkptno", b->number);
9769 ui_out_text (uiout, bp_throw ? _(" (throw)")
9770 : _(" (catch)"));
3086aeae
DJ
9771}
9772
6149aea9
PA
9773/* Implement the "print_recreate" breakpoint_ops method for throw and
9774 catch catchpoints. */
9775
9776static void
4a64f543
MS
9777print_recreate_exception_catchpoint (struct breakpoint *b,
9778 struct ui_file *fp)
6149aea9
PA
9779{
9780 int bp_temp;
9781 int bp_throw;
9782
9783 bp_temp = b->disposition == disp_del;
9784 bp_throw = strstr (b->addr_string, "throw") != NULL;
9785 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9786 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9787 print_recreate_thread (b, fp);
6149aea9
PA
9788}
9789
2060206e 9790static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9791
9792static int
9793handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9794 enum exception_event_kind ex_event, int from_tty)
9795{
604133b5
AR
9796 char *trigger_func_name;
9797
3086aeae 9798 if (ex_event == EX_EVENT_CATCH)
604133b5 9799 trigger_func_name = "__cxa_begin_catch";
3086aeae 9800 else
604133b5 9801 trigger_func_name = "__cxa_throw";
3086aeae 9802
8cdf0e15
VP
9803 create_breakpoint (get_current_arch (),
9804 trigger_func_name, cond_string, -1,
9805 0 /* condition and thread are valid. */,
0fb4aa4b 9806 tempflag, bp_breakpoint,
8cdf0e15
VP
9807 0,
9808 AUTO_BOOLEAN_TRUE /* pending */,
9809 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9810 1 /* enabled */,
9811 0 /* internal */);
3086aeae 9812
3086aeae
DJ
9813 return 1;
9814}
9815
4a64f543 9816/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9817
9818static void
fba45db2
KB
9819catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9820 int tempflag, int from_tty)
c906108c 9821{
c5aa993b 9822 char *cond_string = NULL;
c5aa993b 9823
44feb3ce
TT
9824 if (!arg)
9825 arg = "";
e9cafbcc 9826 arg = skip_spaces (arg);
c5aa993b 9827
c906108c
SS
9828 cond_string = ep_parse_optional_if_clause (&arg);
9829
9830 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9831 error (_("Junk at end of arguments."));
c906108c 9832
059fb39f
PM
9833 if (ex_event != EX_EVENT_THROW
9834 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9835 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9836
3086aeae
DJ
9837 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9838 return;
9839
8a3fe4f8 9840 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9841}
9842
44feb3ce
TT
9843/* Implementation of "catch catch" command. */
9844
9845static void
9846catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9847{
9848 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9849
44feb3ce
TT
9850 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9851}
9852
9853/* Implementation of "catch throw" command. */
9854
9855static void
9856catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9857{
9858 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9859
44feb3ce
TT
9860 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9861}
9862
9ac4176b 9863void
28010a5d
PA
9864init_ada_exception_breakpoint (struct breakpoint *b,
9865 struct gdbarch *gdbarch,
9866 struct symtab_and_line sal,
9867 char *addr_string,
c0a91b2b 9868 const struct breakpoint_ops *ops,
28010a5d
PA
9869 int tempflag,
9870 int from_tty)
f7f9143b 9871{
f7f9143b
JB
9872 if (from_tty)
9873 {
5af949e3
UW
9874 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9875 if (!loc_gdbarch)
9876 loc_gdbarch = gdbarch;
9877
6c95b8df
PA
9878 describe_other_breakpoints (loc_gdbarch,
9879 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9880 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9881 version for exception catchpoints, because two catchpoints
9882 used for different exception names will use the same address.
9883 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9884 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9885 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9886 the user what type of catchpoint it is. The above is good
9887 enough for now, though. */
9888 }
9889
28010a5d 9890 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9891
9892 b->enable_state = bp_enabled;
9893 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9894 b->addr_string = addr_string;
9895 b->language = language_ada;
f7f9143b
JB
9896}
9897
a96d9b2e
SDJ
9898/* Cleanup function for a syscall filter list. */
9899static void
9900clean_up_filters (void *arg)
9901{
9902 VEC(int) *iter = *(VEC(int) **) arg;
9903 VEC_free (int, iter);
9904}
9905
9906/* Splits the argument using space as delimiter. Returns an xmalloc'd
9907 filter list, or NULL if no filtering is required. */
9908static VEC(int) *
9909catch_syscall_split_args (char *arg)
9910{
9911 VEC(int) *result = NULL;
9912 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9913
9914 while (*arg != '\0')
9915 {
9916 int i, syscall_number;
9917 char *endptr;
9918 char cur_name[128];
9919 struct syscall s;
9920
9921 /* Skip whitespace. */
9922 while (isspace (*arg))
9923 arg++;
9924
9925 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9926 cur_name[i] = arg[i];
9927 cur_name[i] = '\0';
9928 arg += i;
9929
9930 /* Check if the user provided a syscall name or a number. */
9931 syscall_number = (int) strtol (cur_name, &endptr, 0);
9932 if (*endptr == '\0')
bccd0dd2 9933 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9934 else
9935 {
9936 /* We have a name. Let's check if it's valid and convert it
9937 to a number. */
9938 get_syscall_by_name (cur_name, &s);
9939
9940 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9941 /* Here we have to issue an error instead of a warning,
9942 because GDB cannot do anything useful if there's no
9943 syscall number to be caught. */
a96d9b2e
SDJ
9944 error (_("Unknown syscall name '%s'."), cur_name);
9945 }
9946
9947 /* Ok, it's valid. */
9948 VEC_safe_push (int, result, s.number);
9949 }
9950
9951 discard_cleanups (cleanup);
9952 return result;
9953}
9954
9955/* Implement the "catch syscall" command. */
9956
9957static void
cc59ec59
MS
9958catch_syscall_command_1 (char *arg, int from_tty,
9959 struct cmd_list_element *command)
a96d9b2e
SDJ
9960{
9961 int tempflag;
9962 VEC(int) *filter;
9963 struct syscall s;
9964 struct gdbarch *gdbarch = get_current_arch ();
9965
9966 /* Checking if the feature if supported. */
9967 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9968 error (_("The feature 'catch syscall' is not supported on \
ea666128 9969this architecture yet."));
a96d9b2e
SDJ
9970
9971 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9972
e9cafbcc 9973 arg = skip_spaces (arg);
a96d9b2e
SDJ
9974
9975 /* We need to do this first "dummy" translation in order
9976 to get the syscall XML file loaded or, most important,
9977 to display a warning to the user if there's no XML file
9978 for his/her architecture. */
9979 get_syscall_by_number (0, &s);
9980
9981 /* The allowed syntax is:
9982 catch syscall
9983 catch syscall <name | number> [<name | number> ... <name | number>]
9984
9985 Let's check if there's a syscall name. */
9986
9987 if (arg != NULL)
9988 filter = catch_syscall_split_args (arg);
9989 else
9990 filter = NULL;
9991
9992 create_syscall_event_catchpoint (tempflag, filter,
9993 &catch_syscall_breakpoint_ops);
9994}
9995
c906108c 9996static void
fba45db2 9997catch_command (char *arg, int from_tty)
c906108c 9998{
44feb3ce 9999 error (_("Catch requires an event name."));
c906108c
SS
10000}
10001\f
10002
10003static void
fba45db2 10004tcatch_command (char *arg, int from_tty)
c906108c 10005{
44feb3ce 10006 error (_("Catch requires an event name."));
c906108c
SS
10007}
10008
80f8a6eb 10009/* Delete breakpoints by address or line. */
c906108c
SS
10010
10011static void
fba45db2 10012clear_command (char *arg, int from_tty)
c906108c 10013{
d6e956e5
VP
10014 struct breakpoint *b;
10015 VEC(breakpoint_p) *found = 0;
10016 int ix;
c906108c
SS
10017 int default_match;
10018 struct symtabs_and_lines sals;
10019 struct symtab_and_line sal;
c906108c
SS
10020 int i;
10021
10022 if (arg)
10023 {
10024 sals = decode_line_spec (arg, 1);
10025 default_match = 0;
10026 }
10027 else
10028 {
c5aa993b 10029 sals.sals = (struct symtab_and_line *)
c906108c 10030 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10031 make_cleanup (xfree, sals.sals);
4a64f543 10032 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
10033 sal.line = default_breakpoint_line;
10034 sal.symtab = default_breakpoint_symtab;
10035 sal.pc = default_breakpoint_address;
6c95b8df 10036 sal.pspace = default_breakpoint_pspace;
c906108c 10037 if (sal.symtab == 0)
8a3fe4f8 10038 error (_("No source file specified."));
c906108c
SS
10039
10040 sals.sals[0] = sal;
10041 sals.nelts = 1;
10042
10043 default_match = 1;
10044 }
10045
4a64f543
MS
10046 /* We don't call resolve_sal_pc here. That's not as bad as it
10047 seems, because all existing breakpoints typically have both
10048 file/line and pc set. So, if clear is given file/line, we can
10049 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10050
10051 We only support clearing given the address explicitly
10052 present in breakpoint table. Say, we've set breakpoint
4a64f543 10053 at file:line. There were several PC values for that file:line,
ed0616c6 10054 due to optimization, all in one block.
4a64f543
MS
10055
10056 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10057 PC corresponding to the same file:line, the breakpoint won't
10058 be cleared. We probably can still clear the breakpoint, but
10059 since the other PC value is never presented to user, user
10060 can only find it by guessing, and it does not seem important
10061 to support that. */
10062
4a64f543
MS
10063 /* For each line spec given, delete bps which correspond to it. Do
10064 it in two passes, solely to preserve the current behavior that
10065 from_tty is forced true if we delete more than one
10066 breakpoint. */
c906108c 10067
80f8a6eb 10068 found = NULL;
c906108c
SS
10069 for (i = 0; i < sals.nelts; i++)
10070 {
10071 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10072 If line given (pc == 0), clear all bpts on specified line.
10073 If defaulting, clear all bpts on default line
c906108c 10074 or at default pc.
c5aa993b
JM
10075
10076 defaulting sal.pc != 0 tests to do
10077
10078 0 1 pc
10079 1 1 pc _and_ line
10080 0 0 line
10081 1 0 <can't happen> */
c906108c
SS
10082
10083 sal = sals.sals[i];
c906108c 10084
4a64f543 10085 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10086 ALL_BREAKPOINTS (b)
c5aa993b 10087 {
0d381245 10088 int match = 0;
4a64f543 10089 /* Are we going to delete b? */
cc60f2e3 10090 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10091 {
10092 struct bp_location *loc = b->loc;
10093 for (; loc; loc = loc->next)
10094 {
6c95b8df
PA
10095 int pc_match = sal.pc
10096 && (loc->pspace == sal.pspace)
0d381245
VP
10097 && (loc->address == sal.pc)
10098 && (!section_is_overlay (loc->section)
10099 || loc->section == sal.section);
10100 int line_match = ((default_match || (0 == sal.pc))
10101 && b->source_file != NULL
10102 && sal.symtab != NULL
6c95b8df 10103 && sal.pspace == loc->pspace
0ba1096a
KT
10104 && filename_cmp (b->source_file,
10105 sal.symtab->filename) == 0
0d381245
VP
10106 && b->line_number == sal.line);
10107 if (pc_match || line_match)
10108 {
10109 match = 1;
10110 break;
10111 }
10112 }
10113 }
10114
10115 if (match)
d6e956e5 10116 VEC_safe_push(breakpoint_p, found, b);
c906108c 10117 }
80f8a6eb
MS
10118 }
10119 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10120 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10121 {
10122 if (arg)
8a3fe4f8 10123 error (_("No breakpoint at %s."), arg);
80f8a6eb 10124 else
8a3fe4f8 10125 error (_("No breakpoint at this line."));
80f8a6eb 10126 }
c906108c 10127
d6e956e5 10128 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10129 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10130 if (from_tty)
a3f17187 10131 {
d6e956e5 10132 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10133 printf_unfiltered (_("Deleted breakpoint "));
10134 else
10135 printf_unfiltered (_("Deleted breakpoints "));
10136 }
80f8a6eb 10137 breakpoints_changed ();
d6e956e5
VP
10138
10139 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10140 {
c5aa993b 10141 if (from_tty)
d6e956e5
VP
10142 printf_unfiltered ("%d ", b->number);
10143 delete_breakpoint (b);
c906108c 10144 }
80f8a6eb
MS
10145 if (from_tty)
10146 putchar_unfiltered ('\n');
c906108c
SS
10147}
10148\f
10149/* Delete breakpoint in BS if they are `delete' breakpoints and
10150 all breakpoints that are marked for deletion, whether hit or not.
10151 This is called after any breakpoint is hit, or after errors. */
10152
10153void
fba45db2 10154breakpoint_auto_delete (bpstat bs)
c906108c 10155{
35df4500 10156 struct breakpoint *b, *b_tmp;
c906108c
SS
10157
10158 for (; bs; bs = bs->next)
f431efe5
PA
10159 if (bs->breakpoint_at
10160 && bs->breakpoint_at->disposition == disp_del
c906108c 10161 && bs->stop)
f431efe5 10162 delete_breakpoint (bs->breakpoint_at);
c906108c 10163
35df4500 10164 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10165 {
b5de0fa7 10166 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10167 delete_breakpoint (b);
10168 }
c906108c
SS
10169}
10170
4a64f543
MS
10171/* A comparison function for bp_location AP and BP being interfaced to
10172 qsort. Sort elements primarily by their ADDRESS (no matter what
10173 does breakpoint_address_is_meaningful say for its OWNER),
10174 secondarily by ordering first bp_permanent OWNERed elements and
10175 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10176 qsort being an unstable algorithm. */
876fa593
JK
10177
10178static int
494cfb0f 10179bp_location_compare (const void *ap, const void *bp)
876fa593 10180{
494cfb0f
JK
10181 struct bp_location *a = *(void **) ap;
10182 struct bp_location *b = *(void **) bp;
2bdf28a0 10183 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10184 int a_perm = a->owner->enable_state == bp_permanent;
10185 int b_perm = b->owner->enable_state == bp_permanent;
10186
10187 if (a->address != b->address)
10188 return (a->address > b->address) - (a->address < b->address);
10189
10190 /* Sort permanent breakpoints first. */
10191 if (a_perm != b_perm)
10192 return (a_perm < b_perm) - (a_perm > b_perm);
10193
4a64f543
MS
10194 /* Make the user-visible order stable across GDB runs. Locations of
10195 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10196
10197 if (a->owner->number != b->owner->number)
10198 return (a->owner->number > b->owner->number)
10199 - (a->owner->number < b->owner->number);
10200
10201 return (a > b) - (a < b);
10202}
10203
876fa593 10204/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10205 bp_location_shadow_len_after_address_max according to the current
10206 content of the bp_location array. */
f7545552
TT
10207
10208static void
876fa593 10209bp_location_target_extensions_update (void)
f7545552 10210{
876fa593
JK
10211 struct bp_location *bl, **blp_tmp;
10212
10213 bp_location_placed_address_before_address_max = 0;
10214 bp_location_shadow_len_after_address_max = 0;
10215
10216 ALL_BP_LOCATIONS (bl, blp_tmp)
10217 {
10218 CORE_ADDR start, end, addr;
10219
10220 if (!bp_location_has_shadow (bl))
10221 continue;
10222
10223 start = bl->target_info.placed_address;
10224 end = start + bl->target_info.shadow_len;
10225
10226 gdb_assert (bl->address >= start);
10227 addr = bl->address - start;
10228 if (addr > bp_location_placed_address_before_address_max)
10229 bp_location_placed_address_before_address_max = addr;
10230
10231 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10232
10233 gdb_assert (bl->address < end);
10234 addr = end - bl->address;
10235 if (addr > bp_location_shadow_len_after_address_max)
10236 bp_location_shadow_len_after_address_max = addr;
10237 }
f7545552
TT
10238}
10239
4cd9bd08 10240/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10241 into the inferior, only remove already-inserted locations that no
10242 longer should be inserted. Functions that delete a breakpoint or
10243 breakpoints should pass false, so that deleting a breakpoint
10244 doesn't have the side effect of inserting the locations of other
10245 breakpoints that are marked not-inserted, but should_be_inserted
10246 returns true on them.
10247
10248 This behaviour is useful is situations close to tear-down -- e.g.,
10249 after an exec, while the target still has execution, but breakpoint
10250 shadows of the previous executable image should *NOT* be restored
10251 to the new image; or before detaching, where the target still has
10252 execution and wants to delete breakpoints from GDB's lists, and all
10253 breakpoints had already been removed from the inferior. */
10254
0d381245 10255static void
b60e7edf 10256update_global_location_list (int should_insert)
0d381245 10257{
74960c60 10258 struct breakpoint *b;
876fa593 10259 struct bp_location **locp, *loc;
f7545552
TT
10260 struct cleanup *cleanups;
10261
2d134ed3
PA
10262 /* Used in the duplicates detection below. When iterating over all
10263 bp_locations, points to the first bp_location of a given address.
10264 Breakpoints and watchpoints of different types are never
10265 duplicates of each other. Keep one pointer for each type of
10266 breakpoint/watchpoint, so we only need to loop over all locations
10267 once. */
10268 struct bp_location *bp_loc_first; /* breakpoint */
10269 struct bp_location *wp_loc_first; /* hardware watchpoint */
10270 struct bp_location *awp_loc_first; /* access watchpoint */
10271 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10272
4a64f543
MS
10273 /* Saved former bp_location array which we compare against the newly
10274 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10275 struct bp_location **old_location, **old_locp;
10276 unsigned old_location_count;
10277
10278 old_location = bp_location;
10279 old_location_count = bp_location_count;
10280 bp_location = NULL;
10281 bp_location_count = 0;
10282 cleanups = make_cleanup (xfree, old_location);
0d381245 10283
74960c60 10284 ALL_BREAKPOINTS (b)
876fa593
JK
10285 for (loc = b->loc; loc; loc = loc->next)
10286 bp_location_count++;
10287
10288 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10289 locp = bp_location;
10290 ALL_BREAKPOINTS (b)
10291 for (loc = b->loc; loc; loc = loc->next)
10292 *locp++ = loc;
10293 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10294 bp_location_compare);
876fa593
JK
10295
10296 bp_location_target_extensions_update ();
74960c60 10297
4a64f543
MS
10298 /* Identify bp_location instances that are no longer present in the
10299 new list, and therefore should be freed. Note that it's not
10300 necessary that those locations should be removed from inferior --
10301 if there's another location at the same address (previously
10302 marked as duplicate), we don't need to remove/insert the
10303 location.
876fa593 10304
4a64f543
MS
10305 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10306 and former bp_location array state respectively. */
876fa593
JK
10307
10308 locp = bp_location;
10309 for (old_locp = old_location; old_locp < old_location + old_location_count;
10310 old_locp++)
74960c60 10311 {
876fa593 10312 struct bp_location *old_loc = *old_locp;
c7d46a38 10313 struct bp_location **loc2p;
876fa593 10314
e5dd4106 10315 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10316 not, we have to free it. */
c7d46a38 10317 int found_object = 0;
20874c92
VP
10318 /* Tells if the location should remain inserted in the target. */
10319 int keep_in_target = 0;
10320 int removed = 0;
876fa593 10321
4a64f543
MS
10322 /* Skip LOCP entries which will definitely never be needed.
10323 Stop either at or being the one matching OLD_LOC. */
876fa593 10324 while (locp < bp_location + bp_location_count
c7d46a38 10325 && (*locp)->address < old_loc->address)
876fa593 10326 locp++;
c7d46a38
PA
10327
10328 for (loc2p = locp;
10329 (loc2p < bp_location + bp_location_count
10330 && (*loc2p)->address == old_loc->address);
10331 loc2p++)
10332 {
10333 if (*loc2p == old_loc)
10334 {
10335 found_object = 1;
10336 break;
10337 }
10338 }
74960c60 10339
4a64f543
MS
10340 /* If this location is no longer present, and inserted, look if
10341 there's maybe a new location at the same address. If so,
10342 mark that one inserted, and don't remove this one. This is
10343 needed so that we don't have a time window where a breakpoint
10344 at certain location is not inserted. */
74960c60 10345
876fa593 10346 if (old_loc->inserted)
0d381245 10347 {
4a64f543
MS
10348 /* If the location is inserted now, we might have to remove
10349 it. */
74960c60 10350
876fa593 10351 if (found_object && should_be_inserted (old_loc))
74960c60 10352 {
4a64f543
MS
10353 /* The location is still present in the location list,
10354 and still should be inserted. Don't do anything. */
20874c92 10355 keep_in_target = 1;
74960c60
VP
10356 }
10357 else
10358 {
4a64f543
MS
10359 /* The location is either no longer present, or got
10360 disabled. See if there's another location at the
10361 same address, in which case we don't need to remove
10362 this one from the target. */
876fa593 10363
2bdf28a0 10364 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10365 if (breakpoint_address_is_meaningful (old_loc->owner))
10366 {
876fa593 10367 for (loc2p = locp;
c7d46a38
PA
10368 (loc2p < bp_location + bp_location_count
10369 && (*loc2p)->address == old_loc->address);
876fa593
JK
10370 loc2p++)
10371 {
10372 struct bp_location *loc2 = *loc2p;
10373
2d134ed3 10374 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10375 {
10376 /* For the sake of should_be_inserted.
4a64f543
MS
10377 Duplicates check below will fix up this
10378 later. */
c7d46a38 10379 loc2->duplicate = 0;
85d721b8
PA
10380
10381 /* Read watchpoint locations are switched to
10382 access watchpoints, if the former are not
10383 supported, but the latter are. */
10384 if (is_hardware_watchpoint (old_loc->owner))
10385 {
10386 gdb_assert (is_hardware_watchpoint (loc2->owner));
10387 loc2->watchpoint_type = old_loc->watchpoint_type;
10388 }
10389
c7d46a38
PA
10390 if (loc2 != old_loc && should_be_inserted (loc2))
10391 {
10392 loc2->inserted = 1;
10393 loc2->target_info = old_loc->target_info;
10394 keep_in_target = 1;
10395 break;
10396 }
876fa593
JK
10397 }
10398 }
10399 }
74960c60
VP
10400 }
10401
20874c92
VP
10402 if (!keep_in_target)
10403 {
876fa593 10404 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10405 {
4a64f543
MS
10406 /* This is just about all we can do. We could keep
10407 this location on the global list, and try to
10408 remove it next time, but there's no particular
10409 reason why we will succeed next time.
20874c92 10410
4a64f543
MS
10411 Note that at this point, old_loc->owner is still
10412 valid, as delete_breakpoint frees the breakpoint
10413 only after calling us. */
3e43a32a
MS
10414 printf_filtered (_("warning: Error removing "
10415 "breakpoint %d\n"),
876fa593 10416 old_loc->owner->number);
20874c92
VP
10417 }
10418 removed = 1;
10419 }
0d381245 10420 }
74960c60
VP
10421
10422 if (!found_object)
1c5cfe86 10423 {
db82e815
PA
10424 if (removed && non_stop
10425 && breakpoint_address_is_meaningful (old_loc->owner)
10426 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10427 {
db82e815
PA
10428 /* This location was removed from the target. In
10429 non-stop mode, a race condition is possible where
10430 we've removed a breakpoint, but stop events for that
10431 breakpoint are already queued and will arrive later.
10432 We apply an heuristic to be able to distinguish such
10433 SIGTRAPs from other random SIGTRAPs: we keep this
10434 breakpoint location for a bit, and will retire it
10435 after we see some number of events. The theory here
10436 is that reporting of events should, "on the average",
10437 be fair, so after a while we'll see events from all
10438 threads that have anything of interest, and no longer
10439 need to keep this breakpoint location around. We
10440 don't hold locations forever so to reduce chances of
10441 mistaking a non-breakpoint SIGTRAP for a breakpoint
10442 SIGTRAP.
10443
10444 The heuristic failing can be disastrous on
10445 decr_pc_after_break targets.
10446
10447 On decr_pc_after_break targets, like e.g., x86-linux,
10448 if we fail to recognize a late breakpoint SIGTRAP,
10449 because events_till_retirement has reached 0 too
10450 soon, we'll fail to do the PC adjustment, and report
10451 a random SIGTRAP to the user. When the user resumes
10452 the inferior, it will most likely immediately crash
2dec564e 10453 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10454 corrupted, because of being resumed e.g., in the
10455 middle of a multi-byte instruction, or skipped a
10456 one-byte instruction. This was actually seen happen
10457 on native x86-linux, and should be less rare on
10458 targets that do not support new thread events, like
10459 remote, due to the heuristic depending on
10460 thread_count.
10461
10462 Mistaking a random SIGTRAP for a breakpoint trap
10463 causes similar symptoms (PC adjustment applied when
10464 it shouldn't), but then again, playing with SIGTRAPs
10465 behind the debugger's back is asking for trouble.
10466
10467 Since hardware watchpoint traps are always
10468 distinguishable from other traps, so we don't need to
10469 apply keep hardware watchpoint moribund locations
10470 around. We simply always ignore hardware watchpoint
10471 traps we can no longer explain. */
10472
876fa593
JK
10473 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10474 old_loc->owner = NULL;
20874c92 10475
876fa593 10476 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10477 }
10478 else
f431efe5
PA
10479 {
10480 old_loc->owner = NULL;
10481 decref_bp_location (&old_loc);
10482 }
20874c92 10483 }
74960c60 10484 }
1c5cfe86 10485
348d480f
PA
10486 /* Rescan breakpoints at the same address and section, marking the
10487 first one as "first" and any others as "duplicates". This is so
10488 that the bpt instruction is only inserted once. If we have a
10489 permanent breakpoint at the same place as BPT, make that one the
10490 official one, and the rest as duplicates. Permanent breakpoints
10491 are sorted first for the same address.
10492
10493 Do the same for hardware watchpoints, but also considering the
10494 watchpoint's type (regular/access/read) and length. */
10495
10496 bp_loc_first = NULL;
10497 wp_loc_first = NULL;
10498 awp_loc_first = NULL;
10499 rwp_loc_first = NULL;
10500 ALL_BP_LOCATIONS (loc, locp)
10501 {
10502 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10503 non-NULL. */
10504 struct breakpoint *b = loc->owner;
10505 struct bp_location **loc_first_p;
10506
10507 if (b->enable_state == bp_disabled
10508 || b->enable_state == bp_call_disabled
10509 || b->enable_state == bp_startup_disabled
10510 || !loc->enabled
10511 || loc->shlib_disabled
10512 || !breakpoint_address_is_meaningful (b)
10513 || is_tracepoint (b))
10514 continue;
10515
10516 /* Permanent breakpoint should always be inserted. */
10517 if (b->enable_state == bp_permanent && ! loc->inserted)
10518 internal_error (__FILE__, __LINE__,
10519 _("allegedly permanent breakpoint is not "
10520 "actually inserted"));
10521
10522 if (b->type == bp_hardware_watchpoint)
10523 loc_first_p = &wp_loc_first;
10524 else if (b->type == bp_read_watchpoint)
10525 loc_first_p = &rwp_loc_first;
10526 else if (b->type == bp_access_watchpoint)
10527 loc_first_p = &awp_loc_first;
10528 else
10529 loc_first_p = &bp_loc_first;
10530
10531 if (*loc_first_p == NULL
10532 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10533 || !breakpoint_locations_match (loc, *loc_first_p))
10534 {
10535 *loc_first_p = loc;
10536 loc->duplicate = 0;
10537 continue;
10538 }
10539
10540 loc->duplicate = 1;
10541
10542 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10543 && b->enable_state != bp_permanent)
10544 internal_error (__FILE__, __LINE__,
10545 _("another breakpoint was inserted on top of "
10546 "a permanent breakpoint"));
10547 }
10548
10549 if (breakpoints_always_inserted_mode () && should_insert
10550 && (have_live_inferiors ()
10551 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10552 insert_breakpoint_locations ();
10553
10554 do_cleanups (cleanups);
10555}
10556
10557void
10558breakpoint_retire_moribund (void)
10559{
10560 struct bp_location *loc;
10561 int ix;
10562
10563 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10564 if (--(loc->events_till_retirement) == 0)
10565 {
10566 decref_bp_location (&loc);
10567 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10568 --ix;
10569 }
10570}
10571
10572static void
10573update_global_location_list_nothrow (int inserting)
10574{
10575 struct gdb_exception e;
10576
10577 TRY_CATCH (e, RETURN_MASK_ERROR)
10578 update_global_location_list (inserting);
10579}
10580
10581/* Clear BKP from a BPS. */
10582
10583static void
10584bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10585{
10586 bpstat bs;
10587
10588 for (bs = bps; bs; bs = bs->next)
10589 if (bs->breakpoint_at == bpt)
10590 {
10591 bs->breakpoint_at = NULL;
10592 bs->old_val = NULL;
10593 /* bs->commands will be freed later. */
10594 }
10595}
10596
10597/* Callback for iterate_over_threads. */
10598static int
10599bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10600{
10601 struct breakpoint *bpt = data;
10602
10603 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10604 return 0;
10605}
10606
10607/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10608 callbacks. */
10609
10610static void
10611say_where (struct breakpoint *b)
10612{
10613 struct value_print_options opts;
10614
10615 get_user_print_options (&opts);
10616
10617 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10618 single string. */
10619 if (b->loc == NULL)
10620 {
10621 printf_filtered (_(" (%s) pending."), b->addr_string);
10622 }
10623 else
10624 {
10625 if (opts.addressprint || b->source_file == NULL)
10626 {
10627 printf_filtered (" at ");
10628 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10629 gdb_stdout);
10630 }
10631 if (b->source_file)
10632 printf_filtered (": file %s, line %d.",
10633 b->source_file, b->line_number);
10634
10635 if (b->loc->next)
10636 {
10637 struct bp_location *loc = b->loc;
10638 int n = 0;
10639 for (; loc; loc = loc->next)
10640 ++n;
10641 printf_filtered (" (%d locations)", n);
10642 }
10643 }
10644}
10645
348d480f
PA
10646/* Default bp_location_ops methods. */
10647
10648static void
10649bp_location_dtor (struct bp_location *self)
10650{
10651 xfree (self->cond);
10652 xfree (self->function_name);
10653}
10654
10655static const struct bp_location_ops bp_location_ops =
10656{
10657 bp_location_dtor
10658};
10659
2060206e
PA
10660/* Default breakpoint_ops methods all breakpoint_ops ultimately
10661 inherit from. */
348d480f 10662
2060206e
PA
10663static void
10664base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10665{
10666 decref_counted_command_line (&self->commands);
10667 xfree (self->cond_string);
348d480f
PA
10668 xfree (self->addr_string);
10669 xfree (self->addr_string_range_end);
348d480f
PA
10670 xfree (self->source_file);
10671}
10672
2060206e
PA
10673static struct bp_location *
10674base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10675{
10676 struct bp_location *loc;
10677
10678 loc = XNEW (struct bp_location);
10679 init_bp_location (loc, &bp_location_ops, self);
10680 return loc;
10681}
10682
2060206e
PA
10683static void
10684base_breakpoint_re_set (struct breakpoint *b)
10685{
10686 /* Nothing to re-set. */
10687}
10688
10689#define internal_error_pure_virtual_called() \
10690 gdb_assert_not_reached ("pure virtual function called")
10691
10692static int
10693base_breakpoint_insert_location (struct bp_location *bl)
10694{
10695 internal_error_pure_virtual_called ();
10696}
10697
10698static int
10699base_breakpoint_remove_location (struct bp_location *bl)
10700{
10701 internal_error_pure_virtual_called ();
10702}
10703
10704static int
10705base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10706 struct address_space *aspace,
10707 CORE_ADDR bp_addr)
10708{
10709 internal_error_pure_virtual_called ();
10710}
10711
10712static void
10713base_breakpoint_check_status (bpstat bs)
10714{
10715 /* Always stop. */
10716}
10717
10718/* A "works_in_software_mode" breakpoint_ops method that just internal
10719 errors. */
10720
10721static int
10722base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10723{
10724 internal_error_pure_virtual_called ();
10725}
10726
10727/* A "resources_needed" breakpoint_ops method that just internal
10728 errors. */
10729
10730static int
10731base_breakpoint_resources_needed (const struct bp_location *bl)
10732{
10733 internal_error_pure_virtual_called ();
10734}
10735
10736static enum print_stop_action
10737base_breakpoint_print_it (bpstat bs)
10738{
10739 internal_error_pure_virtual_called ();
10740}
10741
10742static void
10743base_breakpoint_print_one_detail (const struct breakpoint *self,
10744 struct ui_out *uiout)
10745{
10746 /* nothing */
10747}
10748
10749static void
10750base_breakpoint_print_mention (struct breakpoint *b)
10751{
10752 internal_error_pure_virtual_called ();
10753}
10754
10755static void
10756base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10757{
10758 internal_error_pure_virtual_called ();
10759}
10760
10761static struct breakpoint_ops base_breakpoint_ops =
10762{
10763 base_breakpoint_dtor,
10764 base_breakpoint_allocate_location,
10765 base_breakpoint_re_set,
10766 base_breakpoint_insert_location,
10767 base_breakpoint_remove_location,
10768 base_breakpoint_breakpoint_hit,
10769 base_breakpoint_check_status,
10770 base_breakpoint_resources_needed,
10771 base_breakpoint_works_in_software_mode,
10772 base_breakpoint_print_it,
10773 NULL,
10774 base_breakpoint_print_one_detail,
10775 base_breakpoint_print_mention,
10776 base_breakpoint_print_recreate
10777};
10778
10779/* Default breakpoint_ops methods. */
10780
10781static void
348d480f
PA
10782bkpt_re_set (struct breakpoint *b)
10783{
06edf0c0
PA
10784 /* Do not attempt to re-set breakpoints disabled during startup. */
10785 if (b->enable_state == bp_startup_disabled)
10786 return;
348d480f 10787
06edf0c0
PA
10788 /* FIXME: is this still reachable? */
10789 if (b->addr_string == NULL)
10790 {
10791 /* Anything without a string can't be re-set. */
348d480f 10792 delete_breakpoint (b);
06edf0c0 10793 return;
348d480f 10794 }
06edf0c0
PA
10795
10796 breakpoint_re_set_default (b);
348d480f
PA
10797}
10798
2060206e 10799static int
348d480f
PA
10800bkpt_insert_location (struct bp_location *bl)
10801{
10802 if (bl->loc_type == bp_loc_hardware_breakpoint)
10803 return target_insert_hw_breakpoint (bl->gdbarch,
10804 &bl->target_info);
10805 else
10806 return target_insert_breakpoint (bl->gdbarch,
10807 &bl->target_info);
10808}
10809
2060206e 10810static int
348d480f
PA
10811bkpt_remove_location (struct bp_location *bl)
10812{
10813 if (bl->loc_type == bp_loc_hardware_breakpoint)
10814 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10815 else
10816 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10817}
10818
2060206e 10819static int
348d480f
PA
10820bkpt_breakpoint_hit (const struct bp_location *bl,
10821 struct address_space *aspace, CORE_ADDR bp_addr)
10822{
10823 struct breakpoint *b = bl->owner;
10824
10825 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10826 aspace, bp_addr))
10827 return 0;
10828
10829 if (overlay_debugging /* unmapped overlay section */
10830 && section_is_overlay (bl->section)
10831 && !section_is_mapped (bl->section))
10832 return 0;
10833
10834 return 1;
10835}
10836
2060206e 10837static int
348d480f
PA
10838bkpt_resources_needed (const struct bp_location *bl)
10839{
10840 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10841
10842 return 1;
10843}
10844
2060206e 10845static enum print_stop_action
348d480f
PA
10846bkpt_print_it (bpstat bs)
10847{
348d480f
PA
10848 struct breakpoint *b;
10849 const struct bp_location *bl;
001c8c33 10850 int bp_temp;
348d480f
PA
10851
10852 gdb_assert (bs->bp_location_at != NULL);
10853
10854 bl = bs->bp_location_at;
10855 b = bs->breakpoint_at;
10856
001c8c33
PA
10857 bp_temp = b->disposition == disp_del;
10858 if (bl->address != bl->requested_address)
10859 breakpoint_adjustment_warning (bl->requested_address,
10860 bl->address,
10861 b->number, 1);
10862 annotate_breakpoint (b->number);
10863 if (bp_temp)
10864 ui_out_text (uiout, "\nTemporary breakpoint ");
10865 else
10866 ui_out_text (uiout, "\nBreakpoint ");
10867 if (ui_out_is_mi_like_p (uiout))
348d480f 10868 {
001c8c33
PA
10869 ui_out_field_string (uiout, "reason",
10870 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10871 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10872 }
001c8c33
PA
10873 ui_out_field_int (uiout, "bkptno", b->number);
10874 ui_out_text (uiout, ", ");
06edf0c0 10875
001c8c33 10876 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10877}
10878
2060206e 10879static void
06edf0c0
PA
10880bkpt_print_mention (struct breakpoint *b)
10881{
10882 if (ui_out_is_mi_like_p (uiout))
10883 return;
10884
10885 switch (b->type)
10886 {
10887 case bp_breakpoint:
10888 case bp_gnu_ifunc_resolver:
10889 if (b->disposition == disp_del)
10890 printf_filtered (_("Temporary breakpoint"));
10891 else
10892 printf_filtered (_("Breakpoint"));
10893 printf_filtered (_(" %d"), b->number);
10894 if (b->type == bp_gnu_ifunc_resolver)
10895 printf_filtered (_(" at gnu-indirect-function resolver"));
10896 break;
10897 case bp_hardware_breakpoint:
10898 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10899 break;
10900 }
10901
10902 say_where (b);
10903}
10904
2060206e 10905static void
06edf0c0
PA
10906bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10907{
10908 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10909 fprintf_unfiltered (fp, "tbreak");
10910 else if (tp->type == bp_breakpoint)
10911 fprintf_unfiltered (fp, "break");
10912 else if (tp->type == bp_hardware_breakpoint
10913 && tp->disposition == disp_del)
10914 fprintf_unfiltered (fp, "thbreak");
10915 else if (tp->type == bp_hardware_breakpoint)
10916 fprintf_unfiltered (fp, "hbreak");
10917 else
10918 internal_error (__FILE__, __LINE__,
10919 _("unhandled breakpoint type %d"), (int) tp->type);
10920
2060206e 10921 fprintf_unfiltered (fp, " %s", tp->addr_string);
06edf0c0
PA
10922}
10923
06edf0c0
PA
10924/* Virtual table for internal breakpoints. */
10925
10926static void
10927internal_bkpt_re_set (struct breakpoint *b)
10928{
10929 switch (b->type)
10930 {
10931 /* Delete overlay event and longjmp master breakpoints; they
10932 will be reset later by breakpoint_re_set. */
10933 case bp_overlay_event:
10934 case bp_longjmp_master:
10935 case bp_std_terminate_master:
10936 case bp_exception_master:
10937 delete_breakpoint (b);
10938 break;
10939
10940 /* This breakpoint is special, it's set up when the inferior
10941 starts and we really don't want to touch it. */
10942 case bp_shlib_event:
10943
10944 /* Like bp_shlib_event, this breakpoint type is special. Once
10945 it is set up, we do not want to touch it. */
10946 case bp_thread_event:
10947 break;
10948 }
10949}
10950
10951static void
10952internal_bkpt_check_status (bpstat bs)
10953{
10954 /* We do not stop for these. */
10955 bs->stop = 0;
10956}
10957
10958static enum print_stop_action
10959internal_bkpt_print_it (bpstat bs)
10960{
06edf0c0 10961 struct breakpoint *b;
06edf0c0 10962
06edf0c0
PA
10963 b = bs->breakpoint_at;
10964
06edf0c0
PA
10965 switch (b->type)
10966 {
348d480f
PA
10967 case bp_shlib_event:
10968 /* Did we stop because the user set the stop_on_solib_events
10969 variable? (If so, we report this as a generic, "Stopped due
10970 to shlib event" message.) */
10971 printf_filtered (_("Stopped due to shared library event\n"));
348d480f
PA
10972 break;
10973
10974 case bp_thread_event:
10975 /* Not sure how we will get here.
10976 GDB should not stop for these breakpoints. */
10977 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10978 break;
10979
10980 case bp_overlay_event:
10981 /* By analogy with the thread event, GDB should not stop for these. */
10982 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
10983 break;
10984
10985 case bp_longjmp_master:
10986 /* These should never be enabled. */
10987 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
10988 break;
10989
10990 case bp_std_terminate_master:
10991 /* These should never be enabled. */
10992 printf_filtered (_("std::terminate Master Breakpoint: "
10993 "gdb should not stop!\n"));
348d480f
PA
10994 break;
10995
10996 case bp_exception_master:
10997 /* These should never be enabled. */
10998 printf_filtered (_("Exception Master Breakpoint: "
10999 "gdb should not stop!\n"));
06edf0c0
PA
11000 break;
11001 }
11002
001c8c33 11003 return PRINT_NOTHING;
06edf0c0
PA
11004}
11005
11006static void
11007internal_bkpt_print_mention (struct breakpoint *b)
11008{
11009 /* Nothing to mention. These breakpoints are internal. */
11010}
11011
06edf0c0
PA
11012/* Virtual table for momentary breakpoints */
11013
11014static void
11015momentary_bkpt_re_set (struct breakpoint *b)
11016{
11017 /* Keep temporary breakpoints, which can be encountered when we step
11018 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11019 Otherwise these should have been blown away via the cleanup chain
11020 or by breakpoint_init_inferior when we rerun the executable. */
11021}
11022
11023static void
11024momentary_bkpt_check_status (bpstat bs)
11025{
11026 /* Nothing. The point of these breakpoints is causing a stop. */
11027}
11028
11029static enum print_stop_action
11030momentary_bkpt_print_it (bpstat bs)
11031{
001c8c33 11032 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11033 {
001c8c33 11034 struct breakpoint *b = bs->breakpoint_at;
348d480f 11035
001c8c33
PA
11036 switch (b->type)
11037 {
11038 case bp_finish:
11039 ui_out_field_string
11040 (uiout, "reason",
11041 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11042 break;
348d480f 11043
001c8c33
PA
11044 case bp_until:
11045 ui_out_field_string
11046 (uiout, "reason",
11047 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11048 break;
11049 }
348d480f
PA
11050 }
11051
001c8c33 11052 return PRINT_UNKNOWN;
348d480f
PA
11053}
11054
06edf0c0
PA
11055static void
11056momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11057{
06edf0c0 11058 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11059}
11060
348d480f 11061/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11062
348d480f
PA
11063static void
11064tracepoint_re_set (struct breakpoint *b)
11065{
11066 breakpoint_re_set_default (b);
11067}
876fa593 11068
348d480f
PA
11069static int
11070tracepoint_breakpoint_hit (const struct bp_location *bl,
11071 struct address_space *aspace, CORE_ADDR bp_addr)
11072{
11073 /* By definition, the inferior does not report stops at
11074 tracepoints. */
11075 return 0;
74960c60
VP
11076}
11077
11078static void
348d480f
PA
11079tracepoint_print_one_detail (const struct breakpoint *self,
11080 struct ui_out *uiout)
74960c60 11081{
d9b3f62e
PA
11082 struct tracepoint *tp = (struct tracepoint *) self;
11083 if (tp->static_trace_marker_id)
348d480f
PA
11084 {
11085 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11086
348d480f
PA
11087 ui_out_text (uiout, "\tmarker id is ");
11088 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11089 tp->static_trace_marker_id);
348d480f
PA
11090 ui_out_text (uiout, "\n");
11091 }
0d381245
VP
11092}
11093
a474d7c2 11094static void
348d480f 11095tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11096{
348d480f
PA
11097 if (ui_out_is_mi_like_p (uiout))
11098 return;
cc59ec59 11099
348d480f
PA
11100 switch (b->type)
11101 {
11102 case bp_tracepoint:
11103 printf_filtered (_("Tracepoint"));
11104 printf_filtered (_(" %d"), b->number);
11105 break;
11106 case bp_fast_tracepoint:
11107 printf_filtered (_("Fast tracepoint"));
11108 printf_filtered (_(" %d"), b->number);
11109 break;
11110 case bp_static_tracepoint:
11111 printf_filtered (_("Static tracepoint"));
11112 printf_filtered (_(" %d"), b->number);
11113 break;
11114 default:
11115 internal_error (__FILE__, __LINE__,
11116 _("unhandled tracepoint type %d"), (int) b->type);
11117 }
11118
11119 say_where (b);
a474d7c2
PA
11120}
11121
348d480f 11122static void
d9b3f62e 11123tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11124{
d9b3f62e
PA
11125 struct tracepoint *tp = (struct tracepoint *) self;
11126
11127 if (self->type == bp_fast_tracepoint)
348d480f 11128 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11129 if (self->type == bp_static_tracepoint)
348d480f 11130 fprintf_unfiltered (fp, "strace");
d9b3f62e 11131 else if (self->type == bp_tracepoint)
348d480f
PA
11132 fprintf_unfiltered (fp, "trace");
11133 else
11134 internal_error (__FILE__, __LINE__,
d9b3f62e 11135 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11136
d9b3f62e
PA
11137 fprintf_unfiltered (fp, " %s", self->addr_string);
11138 print_recreate_thread (self, fp);
11139
11140 if (tp->pass_count)
11141 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11142}
11143
2060206e 11144struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11145
53a5351d 11146/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11147 structures. */
c906108c
SS
11148
11149void
fba45db2 11150delete_breakpoint (struct breakpoint *bpt)
c906108c 11151{
52f0bd74 11152 struct breakpoint *b;
c906108c 11153
8a3fe4f8 11154 gdb_assert (bpt != NULL);
c906108c 11155
4a64f543
MS
11156 /* Has this bp already been deleted? This can happen because
11157 multiple lists can hold pointers to bp's. bpstat lists are
11158 especial culprits.
11159
11160 One example of this happening is a watchpoint's scope bp. When
11161 the scope bp triggers, we notice that the watchpoint is out of
11162 scope, and delete it. We also delete its scope bp. But the
11163 scope bp is marked "auto-deleting", and is already on a bpstat.
11164 That bpstat is then checked for auto-deleting bp's, which are
11165 deleted.
11166
11167 A real solution to this problem might involve reference counts in
11168 bp's, and/or giving them pointers back to their referencing
11169 bpstat's, and teaching delete_breakpoint to only free a bp's
11170 storage when no more references were extent. A cheaper bandaid
11171 was chosen. */
c906108c
SS
11172 if (bpt->type == bp_none)
11173 return;
11174
4a64f543
MS
11175 /* At least avoid this stale reference until the reference counting
11176 of breakpoints gets resolved. */
d0fb5eae 11177 if (bpt->related_breakpoint != bpt)
e5a0a904 11178 {
d0fb5eae 11179 struct breakpoint *related;
3a5c3e22 11180 struct watchpoint *w;
d0fb5eae
JK
11181
11182 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11183 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11184 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11185 w = (struct watchpoint *) bpt;
11186 else
11187 w = NULL;
11188 if (w != NULL)
11189 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11190
11191 /* Unlink bpt from the bpt->related_breakpoint ring. */
11192 for (related = bpt; related->related_breakpoint != bpt;
11193 related = related->related_breakpoint);
11194 related->related_breakpoint = bpt->related_breakpoint;
11195 bpt->related_breakpoint = bpt;
e5a0a904
JK
11196 }
11197
a9634178
TJB
11198 /* watch_command_1 creates a watchpoint but only sets its number if
11199 update_watchpoint succeeds in creating its bp_locations. If there's
11200 a problem in that process, we'll be asked to delete the half-created
11201 watchpoint. In that case, don't announce the deletion. */
11202 if (bpt->number)
11203 observer_notify_breakpoint_deleted (bpt);
c906108c 11204
c906108c
SS
11205 if (breakpoint_chain == bpt)
11206 breakpoint_chain = bpt->next;
11207
c906108c
SS
11208 ALL_BREAKPOINTS (b)
11209 if (b->next == bpt)
c5aa993b
JM
11210 {
11211 b->next = bpt->next;
11212 break;
11213 }
c906108c 11214
f431efe5
PA
11215 /* Be sure no bpstat's are pointing at the breakpoint after it's
11216 been freed. */
11217 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11218 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11219 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11220 commands are associated with the bpstat; if we remove it here,
11221 then the later call to bpstat_do_actions (&stop_bpstat); in
11222 event-top.c won't do anything, and temporary breakpoints with
11223 commands won't work. */
11224
11225 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11226
4a64f543
MS
11227 /* Now that breakpoint is removed from breakpoint list, update the
11228 global location list. This will remove locations that used to
11229 belong to this breakpoint. Do this before freeing the breakpoint
11230 itself, since remove_breakpoint looks at location's owner. It
11231 might be better design to have location completely
11232 self-contained, but it's not the case now. */
b60e7edf 11233 update_global_location_list (0);
74960c60 11234
348d480f 11235 bpt->ops->dtor (bpt);
4a64f543
MS
11236 /* On the chance that someone will soon try again to delete this
11237 same bp, we mark it as deleted before freeing its storage. */
c906108c 11238 bpt->type = bp_none;
b8c9b27d 11239 xfree (bpt);
c906108c
SS
11240}
11241
4d6140d9
AC
11242static void
11243do_delete_breakpoint_cleanup (void *b)
11244{
11245 delete_breakpoint (b);
11246}
11247
11248struct cleanup *
11249make_cleanup_delete_breakpoint (struct breakpoint *b)
11250{
11251 return make_cleanup (do_delete_breakpoint_cleanup, b);
11252}
11253
51be5b68
PA
11254/* Iterator function to call a user-provided callback function once
11255 for each of B and its related breakpoints. */
11256
11257static void
11258iterate_over_related_breakpoints (struct breakpoint *b,
11259 void (*function) (struct breakpoint *,
11260 void *),
11261 void *data)
11262{
11263 struct breakpoint *related;
11264
11265 related = b;
11266 do
11267 {
11268 struct breakpoint *next;
11269
11270 /* FUNCTION may delete RELATED. */
11271 next = related->related_breakpoint;
11272
11273 if (next == related)
11274 {
11275 /* RELATED is the last ring entry. */
11276 function (related, data);
11277
11278 /* FUNCTION may have deleted it, so we'd never reach back to
11279 B. There's nothing left to do anyway, so just break
11280 out. */
11281 break;
11282 }
11283 else
11284 function (related, data);
11285
11286 related = next;
11287 }
11288 while (related != b);
11289}
95a42b64
TT
11290
11291static void
11292do_delete_breakpoint (struct breakpoint *b, void *ignore)
11293{
11294 delete_breakpoint (b);
11295}
11296
51be5b68
PA
11297/* A callback for map_breakpoint_numbers that calls
11298 delete_breakpoint. */
11299
11300static void
11301do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11302{
11303 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11304}
11305
c906108c 11306void
fba45db2 11307delete_command (char *arg, int from_tty)
c906108c 11308{
35df4500 11309 struct breakpoint *b, *b_tmp;
c906108c 11310
ea9365bb
TT
11311 dont_repeat ();
11312
c906108c
SS
11313 if (arg == 0)
11314 {
11315 int breaks_to_delete = 0;
11316
46c6471b
PA
11317 /* Delete all breakpoints if no argument. Do not delete
11318 internal breakpoints, these have to be deleted with an
11319 explicit breakpoint number argument. */
c5aa993b 11320 ALL_BREAKPOINTS (b)
46c6471b 11321 if (user_breakpoint_p (b))
973d738b
DJ
11322 {
11323 breaks_to_delete = 1;
11324 break;
11325 }
c906108c
SS
11326
11327 /* Ask user only if there are some breakpoints to delete. */
11328 if (!from_tty
e2e0b3e5 11329 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11330 {
35df4500 11331 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11332 if (user_breakpoint_p (b))
c5aa993b 11333 delete_breakpoint (b);
c906108c
SS
11334 }
11335 }
11336 else
51be5b68 11337 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11338}
11339
0d381245
VP
11340static int
11341all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11342{
0d381245
VP
11343 for (; loc; loc = loc->next)
11344 if (!loc->shlib_disabled)
11345 return 0;
11346 return 1;
fe3f5fa8
VP
11347}
11348
776592bf
DE
11349/* Subroutine of update_breakpoint_locations to simplify it.
11350 Return non-zero if multiple fns in list LOC have the same name.
11351 Null names are ignored. */
11352
11353static int
11354ambiguous_names_p (struct bp_location *loc)
11355{
11356 struct bp_location *l;
11357 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11358 (int (*) (const void *,
11359 const void *)) streq,
776592bf
DE
11360 NULL, xcalloc, xfree);
11361
11362 for (l = loc; l != NULL; l = l->next)
11363 {
11364 const char **slot;
11365 const char *name = l->function_name;
11366
11367 /* Allow for some names to be NULL, ignore them. */
11368 if (name == NULL)
11369 continue;
11370
11371 slot = (const char **) htab_find_slot (htab, (const void *) name,
11372 INSERT);
4a64f543
MS
11373 /* NOTE: We can assume slot != NULL here because xcalloc never
11374 returns NULL. */
776592bf
DE
11375 if (*slot != NULL)
11376 {
11377 htab_delete (htab);
11378 return 1;
11379 }
11380 *slot = name;
11381 }
11382
11383 htab_delete (htab);
11384 return 0;
11385}
11386
0fb4aa4b
PA
11387/* When symbols change, it probably means the sources changed as well,
11388 and it might mean the static tracepoint markers are no longer at
11389 the same address or line numbers they used to be at last we
11390 checked. Losing your static tracepoints whenever you rebuild is
11391 undesirable. This function tries to resync/rematch gdb static
11392 tracepoints with the markers on the target, for static tracepoints
11393 that have not been set by marker id. Static tracepoint that have
11394 been set by marker id are reset by marker id in breakpoint_re_set.
11395 The heuristic is:
11396
11397 1) For a tracepoint set at a specific address, look for a marker at
11398 the old PC. If one is found there, assume to be the same marker.
11399 If the name / string id of the marker found is different from the
11400 previous known name, assume that means the user renamed the marker
11401 in the sources, and output a warning.
11402
11403 2) For a tracepoint set at a given line number, look for a marker
11404 at the new address of the old line number. If one is found there,
11405 assume to be the same marker. If the name / string id of the
11406 marker found is different from the previous known name, assume that
11407 means the user renamed the marker in the sources, and output a
11408 warning.
11409
11410 3) If a marker is no longer found at the same address or line, it
11411 may mean the marker no longer exists. But it may also just mean
11412 the code changed a bit. Maybe the user added a few lines of code
11413 that made the marker move up or down (in line number terms). Ask
11414 the target for info about the marker with the string id as we knew
11415 it. If found, update line number and address in the matching
11416 static tracepoint. This will get confused if there's more than one
11417 marker with the same ID (possible in UST, although unadvised
11418 precisely because it confuses tools). */
11419
11420static struct symtab_and_line
11421update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11422{
d9b3f62e 11423 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11424 struct static_tracepoint_marker marker;
11425 CORE_ADDR pc;
11426 int i;
11427
11428 pc = sal.pc;
11429 if (sal.line)
11430 find_line_pc (sal.symtab, sal.line, &pc);
11431
11432 if (target_static_tracepoint_marker_at (pc, &marker))
11433 {
d9b3f62e 11434 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11435 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11436 b->number,
d9b3f62e 11437 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11438
d9b3f62e
PA
11439 xfree (tp->static_trace_marker_id);
11440 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11441 release_static_tracepoint_marker (&marker);
11442
11443 return sal;
11444 }
11445
11446 /* Old marker wasn't found on target at lineno. Try looking it up
11447 by string ID. */
11448 if (!sal.explicit_pc
11449 && sal.line != 0
11450 && sal.symtab != NULL
d9b3f62e 11451 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11452 {
11453 VEC(static_tracepoint_marker_p) *markers;
11454
11455 markers
d9b3f62e 11456 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11457
11458 if (!VEC_empty(static_tracepoint_marker_p, markers))
11459 {
11460 struct symtab_and_line sal;
11461 struct symbol *sym;
11462 struct static_tracepoint_marker *marker;
11463
11464 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11465
d9b3f62e
PA
11466 xfree (tp->static_trace_marker_id);
11467 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11468
11469 warning (_("marker for static tracepoint %d (%s) not "
11470 "found at previous line number"),
d9b3f62e 11471 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11472
11473 init_sal (&sal);
11474
11475 sal.pc = marker->address;
11476
11477 sal = find_pc_line (marker->address, 0);
11478 sym = find_pc_sect_function (marker->address, NULL);
11479 ui_out_text (uiout, "Now in ");
11480 if (sym)
11481 {
11482 ui_out_field_string (uiout, "func",
11483 SYMBOL_PRINT_NAME (sym));
11484 ui_out_text (uiout, " at ");
11485 }
11486 ui_out_field_string (uiout, "file", sal.symtab->filename);
11487 ui_out_text (uiout, ":");
11488
11489 if (ui_out_is_mi_like_p (uiout))
11490 {
11491 char *fullname = symtab_to_fullname (sal.symtab);
11492
11493 if (fullname)
11494 ui_out_field_string (uiout, "fullname", fullname);
11495 }
11496
11497 ui_out_field_int (uiout, "line", sal.line);
11498 ui_out_text (uiout, "\n");
11499
11500 b->line_number = sal.line;
11501
11502 xfree (b->source_file);
11503 if (sym)
11504 b->source_file = xstrdup (sal.symtab->filename);
11505 else
11506 b->source_file = NULL;
11507
11508 xfree (b->addr_string);
11509 b->addr_string = xstrprintf ("%s:%d",
11510 sal.symtab->filename, b->line_number);
11511
11512 /* Might be nice to check if function changed, and warn if
11513 so. */
11514
11515 release_static_tracepoint_marker (marker);
11516 }
11517 }
11518 return sal;
11519}
11520
8d3788bd
VP
11521/* Returns 1 iff locations A and B are sufficiently same that
11522 we don't need to report breakpoint as changed. */
11523
11524static int
11525locations_are_equal (struct bp_location *a, struct bp_location *b)
11526{
11527 while (a && b)
11528 {
11529 if (a->address != b->address)
11530 return 0;
11531
11532 if (a->shlib_disabled != b->shlib_disabled)
11533 return 0;
11534
11535 if (a->enabled != b->enabled)
11536 return 0;
11537
11538 a = a->next;
11539 b = b->next;
11540 }
11541
11542 if ((a == NULL) != (b == NULL))
11543 return 0;
11544
11545 return 1;
11546}
11547
f1310107
TJB
11548/* Create new breakpoint locations for B (a hardware or software breakpoint)
11549 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11550 a ranged breakpoint. */
11551
0e30163f 11552void
0d381245 11553update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11554 struct symtabs_and_lines sals,
11555 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11556{
11557 int i;
0d381245
VP
11558 struct bp_location *existing_locations = b->loc;
11559
f1310107
TJB
11560 /* Ranged breakpoints have only one start location and one end location. */
11561 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11562
4a64f543
MS
11563 /* If there's no new locations, and all existing locations are
11564 pending, don't do anything. This optimizes the common case where
11565 all locations are in the same shared library, that was unloaded.
11566 We'd like to retain the location, so that when the library is
11567 loaded again, we don't loose the enabled/disabled status of the
11568 individual locations. */
0d381245 11569 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11570 return;
11571
fe3f5fa8
VP
11572 b->loc = NULL;
11573
0d381245 11574 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11575 {
0d381245 11576 struct bp_location *new_loc =
39d61571 11577 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11578
0d381245
VP
11579 /* Reparse conditions, they might contain references to the
11580 old symtab. */
11581 if (b->cond_string != NULL)
11582 {
f1310107 11583 char *s;
0d381245 11584 struct gdb_exception e;
fe3f5fa8 11585
0d381245
VP
11586 s = b->cond_string;
11587 TRY_CATCH (e, RETURN_MASK_ERROR)
11588 {
11589 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11590 0);
11591 }
11592 if (e.reason < 0)
11593 {
3e43a32a
MS
11594 warning (_("failed to reevaluate condition "
11595 "for breakpoint %d: %s"),
0d381245
VP
11596 b->number, e.message);
11597 new_loc->enabled = 0;
11598 }
11599 }
fe3f5fa8 11600
0d381245
VP
11601 if (b->source_file != NULL)
11602 xfree (b->source_file);
11603 if (sals.sals[i].symtab == NULL)
11604 b->source_file = NULL;
11605 else
1b36a34b 11606 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11607
0d381245
VP
11608 if (b->line_number == 0)
11609 b->line_number = sals.sals[i].line;
f1310107
TJB
11610
11611 if (sals_end.nelts)
11612 {
11613 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11614
11615 new_loc->length = end - sals.sals[0].pc + 1;
11616 }
0d381245 11617 }
fe3f5fa8 11618
514f746b
AR
11619 /* Update locations of permanent breakpoints. */
11620 if (b->enable_state == bp_permanent)
11621 make_breakpoint_permanent (b);
11622
4a64f543
MS
11623 /* If possible, carry over 'disable' status from existing
11624 breakpoints. */
0d381245
VP
11625 {
11626 struct bp_location *e = existing_locations;
776592bf
DE
11627 /* If there are multiple breakpoints with the same function name,
11628 e.g. for inline functions, comparing function names won't work.
11629 Instead compare pc addresses; this is just a heuristic as things
11630 may have moved, but in practice it gives the correct answer
11631 often enough until a better solution is found. */
11632 int have_ambiguous_names = ambiguous_names_p (b->loc);
11633
0d381245
VP
11634 for (; e; e = e->next)
11635 {
11636 if (!e->enabled && e->function_name)
11637 {
11638 struct bp_location *l = b->loc;
776592bf
DE
11639 if (have_ambiguous_names)
11640 {
11641 for (; l; l = l->next)
f1310107 11642 if (breakpoint_locations_match (e, l))
776592bf
DE
11643 {
11644 l->enabled = 0;
11645 break;
11646 }
11647 }
11648 else
11649 {
11650 for (; l; l = l->next)
11651 if (l->function_name
11652 && strcmp (e->function_name, l->function_name) == 0)
11653 {
11654 l->enabled = 0;
11655 break;
11656 }
11657 }
0d381245
VP
11658 }
11659 }
11660 }
fe3f5fa8 11661
8d3788bd
VP
11662 if (!locations_are_equal (existing_locations, b->loc))
11663 observer_notify_breakpoint_modified (b);
11664
b60e7edf 11665 update_global_location_list (1);
fe3f5fa8
VP
11666}
11667
ef23e705
TJB
11668/* Find the SaL locations corresponding to the given ADDR_STRING.
11669 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11670
11671static struct symtabs_and_lines
11672addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11673{
11674 char *s;
58438ac1 11675 int marker_spec;
02d20e4a 11676 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11677 struct gdb_exception e;
11678
11679 s = addr_string;
11680 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11681
11682 TRY_CATCH (e, RETURN_MASK_ERROR)
11683 {
11684 if (marker_spec)
11685 {
d9b3f62e
PA
11686 struct tracepoint *tp = (struct tracepoint *) b;
11687
ef23e705 11688 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11689 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11690 {
d9b3f62e 11691 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11692 sals.nelts = 1;
11693 }
11694 else
d9b3f62e 11695 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11696 }
11697 else
58438ac1 11698 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11699 }
11700 if (e.reason < 0)
11701 {
11702 int not_found_and_ok = 0;
11703 /* For pending breakpoints, it's expected that parsing will
11704 fail until the right shared library is loaded. User has
11705 already told to create pending breakpoints and don't need
11706 extra messages. If breakpoint is in bp_shlib_disabled
11707 state, then user already saw the message about that
11708 breakpoint being disabled, and don't want to see more
11709 errors. */
58438ac1 11710 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11711 && (b->condition_not_parsed
11712 || (b->loc && b->loc->shlib_disabled)
11713 || b->enable_state == bp_disabled))
11714 not_found_and_ok = 1;
11715
11716 if (!not_found_and_ok)
11717 {
11718 /* We surely don't want to warn about the same breakpoint
11719 10 times. One solution, implemented here, is disable
11720 the breakpoint on error. Another solution would be to
11721 have separate 'warning emitted' flag. Since this
11722 happens only when a binary has changed, I don't know
11723 which approach is better. */
11724 b->enable_state = bp_disabled;
11725 throw_exception (e);
11726 }
11727 }
11728
58438ac1 11729 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11730 {
11731 gdb_assert (sals.nelts == 1);
11732
11733 resolve_sal_pc (&sals.sals[0]);
11734 if (b->condition_not_parsed && s && s[0])
11735 {
11736 char *cond_string = 0;
11737 int thread = -1;
11738 int task = 0;
11739
11740 find_condition_and_thread (s, sals.sals[0].pc,
11741 &cond_string, &thread, &task);
11742 if (cond_string)
11743 b->cond_string = cond_string;
11744 b->thread = thread;
11745 b->task = task;
11746 b->condition_not_parsed = 0;
11747 }
11748
11749 if (b->type == bp_static_tracepoint && !marker_spec)
11750 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11751
58438ac1
TT
11752 *found = 1;
11753 }
11754 else
11755 *found = 0;
ef23e705
TJB
11756
11757 return sals;
11758}
11759
348d480f
PA
11760/* The default re_set method, for typical hardware or software
11761 breakpoints. Reevaluate the breakpoint and recreate its
11762 locations. */
11763
11764static void
28010a5d 11765breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11766{
11767 int found;
f1310107 11768 struct symtabs_and_lines sals, sals_end;
ef23e705 11769 struct symtabs_and_lines expanded = {0};
f1310107 11770 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11771
11772 sals = addr_string_to_sals (b, b->addr_string, &found);
11773 if (found)
11774 {
11775 make_cleanup (xfree, sals.sals);
11776 expanded = expand_line_sal_maybe (sals.sals[0]);
11777 }
11778
f1310107
TJB
11779 if (b->addr_string_range_end)
11780 {
11781 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11782 if (found)
11783 {
11784 make_cleanup (xfree, sals_end.sals);
11785 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11786 }
11787 }
11788
11789 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11790}
11791
11792/* Prepare the global context for a re-set of breakpoint B. */
11793
11794static struct cleanup *
11795prepare_re_set_context (struct breakpoint *b)
11796{
11797 struct cleanup *cleanups;
11798
11799 input_radix = b->input_radix;
11800 cleanups = save_current_space_and_thread ();
11801 switch_to_program_space_and_thread (b->pspace);
11802 set_language (b->language);
11803
11804 return cleanups;
ef23e705
TJB
11805}
11806
c906108c
SS
11807/* Reset a breakpoint given it's struct breakpoint * BINT.
11808 The value we return ends up being the return value from catch_errors.
11809 Unused in this case. */
11810
11811static int
4efb68b1 11812breakpoint_re_set_one (void *bint)
c906108c 11813{
4a64f543 11814 /* Get past catch_errs. */
53a5351d 11815 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11816 struct cleanup *cleanups;
c906108c 11817
348d480f
PA
11818 cleanups = prepare_re_set_context (b);
11819 b->ops->re_set (b);
11820 do_cleanups (cleanups);
c906108c
SS
11821 return 0;
11822}
11823
69de3c6a 11824/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11825void
69de3c6a 11826breakpoint_re_set (void)
c906108c 11827{
35df4500 11828 struct breakpoint *b, *b_tmp;
c906108c
SS
11829 enum language save_language;
11830 int save_input_radix;
6c95b8df 11831 struct cleanup *old_chain;
c5aa993b 11832
c906108c
SS
11833 save_language = current_language->la_language;
11834 save_input_radix = input_radix;
6c95b8df
PA
11835 old_chain = save_current_program_space ();
11836
35df4500 11837 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11838 {
4a64f543 11839 /* Format possible error msg. */
fe3f5fa8 11840 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11841 b->number);
11842 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11843 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11844 do_cleanups (cleanups);
c5aa993b 11845 }
c906108c
SS
11846 set_language (save_language);
11847 input_radix = save_input_radix;
e62c965a 11848
0756c555 11849 jit_breakpoint_re_set ();
4efc6507 11850
6c95b8df
PA
11851 do_cleanups (old_chain);
11852
af02033e
PP
11853 create_overlay_event_breakpoint ();
11854 create_longjmp_master_breakpoint ();
11855 create_std_terminate_master_breakpoint ();
186c406b 11856 create_exception_master_breakpoint ();
c906108c
SS
11857}
11858\f
c906108c
SS
11859/* Reset the thread number of this breakpoint:
11860
11861 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11862 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11863void
fba45db2 11864breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11865{
11866 if (b->thread != -1)
11867 {
39f77062
KB
11868 if (in_thread_list (inferior_ptid))
11869 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11870
11871 /* We're being called after following a fork. The new fork is
11872 selected as current, and unless this was a vfork will have a
11873 different program space from the original thread. Reset that
11874 as well. */
11875 b->loc->pspace = current_program_space;
c906108c
SS
11876 }
11877}
11878
03ac34d5
MS
11879/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11880 If from_tty is nonzero, it prints a message to that effect,
11881 which ends with a period (no newline). */
11882
c906108c 11883void
fba45db2 11884set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11885{
52f0bd74 11886 struct breakpoint *b;
c906108c
SS
11887
11888 if (count < 0)
11889 count = 0;
11890
11891 ALL_BREAKPOINTS (b)
11892 if (b->number == bptnum)
c5aa993b 11893 {
d77f58be
SS
11894 if (is_tracepoint (b))
11895 {
11896 if (from_tty && count != 0)
11897 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11898 bptnum);
11899 return;
11900 }
11901
c5aa993b 11902 b->ignore_count = count;
221ea385
KS
11903 if (from_tty)
11904 {
11905 if (count == 0)
3e43a32a
MS
11906 printf_filtered (_("Will stop next time "
11907 "breakpoint %d is reached."),
221ea385
KS
11908 bptnum);
11909 else if (count == 1)
a3f17187 11910 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11911 bptnum);
11912 else
3e43a32a
MS
11913 printf_filtered (_("Will ignore next %d "
11914 "crossings of breakpoint %d."),
221ea385
KS
11915 count, bptnum);
11916 }
c5aa993b 11917 breakpoints_changed ();
8d3788bd 11918 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11919 return;
11920 }
c906108c 11921
8a3fe4f8 11922 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11923}
11924
c906108c
SS
11925/* Command to set ignore-count of breakpoint N to COUNT. */
11926
11927static void
fba45db2 11928ignore_command (char *args, int from_tty)
c906108c
SS
11929{
11930 char *p = args;
52f0bd74 11931 int num;
c906108c
SS
11932
11933 if (p == 0)
e2e0b3e5 11934 error_no_arg (_("a breakpoint number"));
c5aa993b 11935
c906108c 11936 num = get_number (&p);
5c44784c 11937 if (num == 0)
8a3fe4f8 11938 error (_("bad breakpoint number: '%s'"), args);
c906108c 11939 if (*p == 0)
8a3fe4f8 11940 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11941
11942 set_ignore_count (num,
11943 longest_to_int (value_as_long (parse_and_eval (p))),
11944 from_tty);
221ea385
KS
11945 if (from_tty)
11946 printf_filtered ("\n");
c906108c
SS
11947}
11948\f
11949/* Call FUNCTION on each of the breakpoints
11950 whose numbers are given in ARGS. */
11951
11952static void
95a42b64
TT
11953map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11954 void *),
11955 void *data)
c906108c 11956{
52f0bd74
AC
11957 int num;
11958 struct breakpoint *b, *tmp;
11cf8741 11959 int match;
197f0a60 11960 struct get_number_or_range_state state;
c906108c 11961
197f0a60 11962 if (args == 0)
e2e0b3e5 11963 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11964
197f0a60
TT
11965 init_number_or_range (&state, args);
11966
11967 while (!state.finished)
c906108c 11968 {
197f0a60
TT
11969 char *p = state.string;
11970
11cf8741 11971 match = 0;
c5aa993b 11972
197f0a60 11973 num = get_number_or_range (&state);
5c44784c 11974 if (num == 0)
c5aa993b 11975 {
8a3fe4f8 11976 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11977 }
11978 else
11979 {
11980 ALL_BREAKPOINTS_SAFE (b, tmp)
11981 if (b->number == num)
11982 {
11cf8741 11983 match = 1;
cdac0397 11984 function (b, data);
11cf8741 11985 break;
5c44784c 11986 }
11cf8741 11987 if (match == 0)
a3f17187 11988 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11989 }
c906108c
SS
11990 }
11991}
11992
0d381245
VP
11993static struct bp_location *
11994find_location_by_number (char *number)
11995{
11996 char *dot = strchr (number, '.');
11997 char *p1;
11998 int bp_num;
11999 int loc_num;
12000 struct breakpoint *b;
12001 struct bp_location *loc;
12002
12003 *dot = '\0';
12004
12005 p1 = number;
197f0a60 12006 bp_num = get_number (&p1);
0d381245
VP
12007 if (bp_num == 0)
12008 error (_("Bad breakpoint number '%s'"), number);
12009
12010 ALL_BREAKPOINTS (b)
12011 if (b->number == bp_num)
12012 {
12013 break;
12014 }
12015
12016 if (!b || b->number != bp_num)
12017 error (_("Bad breakpoint number '%s'"), number);
12018
12019 p1 = dot+1;
197f0a60 12020 loc_num = get_number (&p1);
0d381245
VP
12021 if (loc_num == 0)
12022 error (_("Bad breakpoint location number '%s'"), number);
12023
12024 --loc_num;
12025 loc = b->loc;
12026 for (;loc_num && loc; --loc_num, loc = loc->next)
12027 ;
12028 if (!loc)
12029 error (_("Bad breakpoint location number '%s'"), dot+1);
12030
12031 return loc;
12032}
12033
12034
1900040c
MS
12035/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12036 If from_tty is nonzero, it prints a message to that effect,
12037 which ends with a period (no newline). */
12038
c906108c 12039void
fba45db2 12040disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12041{
12042 /* Never disable a watchpoint scope breakpoint; we want to
12043 hit them when we leave scope so we can delete both the
12044 watchpoint and its scope breakpoint at that time. */
12045 if (bpt->type == bp_watchpoint_scope)
12046 return;
12047
c2c6d25f 12048 /* You can't disable permanent breakpoints. */
b5de0fa7 12049 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12050 return;
12051
b5de0fa7 12052 bpt->enable_state = bp_disabled;
c906108c 12053
d248b706
KY
12054 if (target_supports_enable_disable_tracepoint ()
12055 && current_trace_status ()->running && is_tracepoint (bpt))
12056 {
12057 struct bp_location *location;
12058
12059 for (location = bpt->loc; location; location = location->next)
12060 target_disable_tracepoint (location);
12061 }
12062
b60e7edf 12063 update_global_location_list (0);
c906108c 12064
8d3788bd 12065 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12066}
12067
51be5b68
PA
12068/* A callback for iterate_over_related_breakpoints. */
12069
12070static void
12071do_disable_breakpoint (struct breakpoint *b, void *ignore)
12072{
12073 disable_breakpoint (b);
12074}
12075
95a42b64
TT
12076/* A callback for map_breakpoint_numbers that calls
12077 disable_breakpoint. */
12078
12079static void
12080do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12081{
51be5b68 12082 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12083}
12084
c906108c 12085static void
fba45db2 12086disable_command (char *args, int from_tty)
c906108c 12087{
c906108c 12088 if (args == 0)
46c6471b
PA
12089 {
12090 struct breakpoint *bpt;
12091
12092 ALL_BREAKPOINTS (bpt)
12093 if (user_breakpoint_p (bpt))
12094 disable_breakpoint (bpt);
12095 }
0d381245
VP
12096 else if (strchr (args, '.'))
12097 {
12098 struct bp_location *loc = find_location_by_number (args);
12099 if (loc)
d248b706
KY
12100 {
12101 loc->enabled = 0;
12102 if (target_supports_enable_disable_tracepoint ()
12103 && current_trace_status ()->running && loc->owner
12104 && is_tracepoint (loc->owner))
12105 target_disable_tracepoint (loc);
12106 }
b60e7edf 12107 update_global_location_list (0);
0d381245 12108 }
c906108c 12109 else
95a42b64 12110 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12111}
12112
12113static void
51be5b68 12114enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12115{
afe38095 12116 int target_resources_ok;
c906108c
SS
12117
12118 if (bpt->type == bp_hardware_breakpoint)
12119 {
12120 int i;
c5aa993b 12121 i = hw_breakpoint_used_count ();
53a5351d 12122 target_resources_ok =
d92524f1 12123 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12124 i + 1, 0);
c906108c 12125 if (target_resources_ok == 0)
8a3fe4f8 12126 error (_("No hardware breakpoint support in the target."));
c906108c 12127 else if (target_resources_ok < 0)
8a3fe4f8 12128 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12129 }
12130
cc60f2e3 12131 if (is_watchpoint (bpt))
c906108c 12132 {
d07205c2
JK
12133 /* Initialize it just to avoid a GCC false warning. */
12134 enum enable_state orig_enable_state = 0;
dde02812
ES
12135 struct gdb_exception e;
12136
12137 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12138 {
3a5c3e22
PA
12139 struct watchpoint *w = (struct watchpoint *) bpt;
12140
1e718ff1
TJB
12141 orig_enable_state = bpt->enable_state;
12142 bpt->enable_state = bp_enabled;
3a5c3e22 12143 update_watchpoint (w, 1 /* reparse */);
c906108c 12144 }
dde02812 12145 if (e.reason < 0)
c5aa993b 12146 {
1e718ff1 12147 bpt->enable_state = orig_enable_state;
dde02812
ES
12148 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12149 bpt->number);
12150 return;
c5aa993b 12151 }
c906108c 12152 }
0101ce28 12153
b4c291bb
KH
12154 if (bpt->enable_state != bp_permanent)
12155 bpt->enable_state = bp_enabled;
d248b706
KY
12156
12157 if (target_supports_enable_disable_tracepoint ()
12158 && current_trace_status ()->running && is_tracepoint (bpt))
12159 {
12160 struct bp_location *location;
12161
12162 for (location = bpt->loc; location; location = location->next)
12163 target_enable_tracepoint (location);
12164 }
12165
b4c291bb 12166 bpt->disposition = disposition;
b60e7edf 12167 update_global_location_list (1);
b4c291bb
KH
12168 breakpoints_changed ();
12169
8d3788bd 12170 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12171}
12172
fe3f5fa8 12173
c906108c 12174void
fba45db2 12175enable_breakpoint (struct breakpoint *bpt)
c906108c 12176{
51be5b68
PA
12177 enable_breakpoint_disp (bpt, bpt->disposition);
12178}
12179
12180static void
12181do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12182{
12183 enable_breakpoint (bpt);
c906108c
SS
12184}
12185
95a42b64
TT
12186/* A callback for map_breakpoint_numbers that calls
12187 enable_breakpoint. */
12188
12189static void
12190do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12191{
51be5b68 12192 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12193}
12194
c906108c
SS
12195/* The enable command enables the specified breakpoints (or all defined
12196 breakpoints) so they once again become (or continue to be) effective
1272ad14 12197 in stopping the inferior. */
c906108c 12198
c906108c 12199static void
fba45db2 12200enable_command (char *args, int from_tty)
c906108c 12201{
c906108c 12202 if (args == 0)
46c6471b
PA
12203 {
12204 struct breakpoint *bpt;
12205
12206 ALL_BREAKPOINTS (bpt)
12207 if (user_breakpoint_p (bpt))
12208 enable_breakpoint (bpt);
12209 }
0d381245
VP
12210 else if (strchr (args, '.'))
12211 {
12212 struct bp_location *loc = find_location_by_number (args);
12213 if (loc)
d248b706
KY
12214 {
12215 loc->enabled = 1;
12216 if (target_supports_enable_disable_tracepoint ()
12217 && current_trace_status ()->running && loc->owner
12218 && is_tracepoint (loc->owner))
12219 target_enable_tracepoint (loc);
12220 }
b60e7edf 12221 update_global_location_list (1);
0d381245 12222 }
c906108c 12223 else
95a42b64 12224 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12225}
12226
12227static void
51be5b68
PA
12228do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12229{
12230 enum bpdisp disp = *(enum bpdisp *) arg;
12231
12232 enable_breakpoint_disp (bpt, disp);
12233}
12234
12235static void
12236do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12237{
51be5b68
PA
12238 enum bpdisp disp = disp_disable;
12239
12240 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12241}
12242
c906108c 12243static void
fba45db2 12244enable_once_command (char *args, int from_tty)
c906108c 12245{
51be5b68 12246 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12247}
12248
12249static void
51be5b68 12250do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12251{
51be5b68
PA
12252 enum bpdisp disp = disp_del;
12253
12254 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12255}
12256
c906108c 12257static void
fba45db2 12258enable_delete_command (char *args, int from_tty)
c906108c 12259{
51be5b68 12260 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12261}
12262\f
fa8d40ab
JJ
12263static void
12264set_breakpoint_cmd (char *args, int from_tty)
12265{
12266}
12267
12268static void
12269show_breakpoint_cmd (char *args, int from_tty)
12270{
12271}
12272
1f3b5d1b
PP
12273/* Invalidate last known value of any hardware watchpoint if
12274 the memory which that value represents has been written to by
12275 GDB itself. */
12276
12277static void
12278invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12279 const bfd_byte *data)
12280{
12281 struct breakpoint *bp;
12282
12283 ALL_BREAKPOINTS (bp)
12284 if (bp->enable_state == bp_enabled
3a5c3e22 12285 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12286 {
3a5c3e22 12287 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12288
3a5c3e22
PA
12289 if (wp->val_valid && wp->val)
12290 {
12291 struct bp_location *loc;
12292
12293 for (loc = bp->loc; loc != NULL; loc = loc->next)
12294 if (loc->loc_type == bp_loc_hardware_watchpoint
12295 && loc->address + loc->length > addr
12296 && addr + len > loc->address)
12297 {
12298 value_free (wp->val);
12299 wp->val = NULL;
12300 wp->val_valid = 0;
12301 }
12302 }
1f3b5d1b
PP
12303 }
12304}
12305
c906108c
SS
12306/* Use default_breakpoint_'s, or nothing if they aren't valid. */
12307
12308struct symtabs_and_lines
fba45db2 12309decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12310{
12311 struct symtabs_and_lines sals;
cc59ec59 12312
c906108c 12313 if (string == 0)
8a3fe4f8 12314 error (_("Empty line specification."));
c906108c
SS
12315 if (default_breakpoint_valid)
12316 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
12317 default_breakpoint_symtab,
12318 default_breakpoint_line,
58438ac1 12319 NULL);
c906108c
SS
12320 else
12321 sals = decode_line_1 (&string, funfirstline,
58438ac1 12322 (struct symtab *) NULL, 0, NULL);
c906108c 12323 if (*string)
8a3fe4f8 12324 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12325 return sals;
12326}
8181d85f
DJ
12327
12328/* Create and insert a raw software breakpoint at PC. Return an
12329 identifier, which should be used to remove the breakpoint later.
12330 In general, places which call this should be using something on the
12331 breakpoint chain instead; this function should be eliminated
12332 someday. */
12333
12334void *
6c95b8df
PA
12335deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12336 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12337{
12338 struct bp_target_info *bp_tgt;
12339
6c95b8df 12340 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12341
6c95b8df 12342 bp_tgt->placed_address_space = aspace;
8181d85f 12343 bp_tgt->placed_address = pc;
6c95b8df 12344
a6d9a66e 12345 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12346 {
12347 /* Could not insert the breakpoint. */
12348 xfree (bp_tgt);
12349 return NULL;
12350 }
12351
12352 return bp_tgt;
12353}
12354
4a64f543
MS
12355/* Remove a breakpoint BP inserted by
12356 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12357
12358int
a6d9a66e 12359deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12360{
12361 struct bp_target_info *bp_tgt = bp;
12362 int ret;
12363
a6d9a66e 12364 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12365 xfree (bp_tgt);
12366
12367 return ret;
12368}
12369
4a64f543
MS
12370/* One (or perhaps two) breakpoints used for software single
12371 stepping. */
8181d85f
DJ
12372
12373static void *single_step_breakpoints[2];
a6d9a66e 12374static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12375
12376/* Create and insert a breakpoint for software single step. */
12377
12378void
6c95b8df 12379insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12380 struct address_space *aspace,
12381 CORE_ADDR next_pc)
8181d85f
DJ
12382{
12383 void **bpt_p;
12384
12385 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12386 {
12387 bpt_p = &single_step_breakpoints[0];
12388 single_step_gdbarch[0] = gdbarch;
12389 }
8181d85f
DJ
12390 else
12391 {
12392 gdb_assert (single_step_breakpoints[1] == NULL);
12393 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12394 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12395 }
12396
4a64f543
MS
12397 /* NOTE drow/2006-04-11: A future improvement to this function would
12398 be to only create the breakpoints once, and actually put them on
12399 the breakpoint chain. That would let us use set_raw_breakpoint.
12400 We could adjust the addresses each time they were needed. Doing
12401 this requires corresponding changes elsewhere where single step
12402 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12403
6c95b8df 12404 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12405 if (*bpt_p == NULL)
5af949e3
UW
12406 error (_("Could not insert single-step breakpoint at %s"),
12407 paddress (gdbarch, next_pc));
8181d85f
DJ
12408}
12409
f02253f1
HZ
12410/* Check if the breakpoints used for software single stepping
12411 were inserted or not. */
12412
12413int
12414single_step_breakpoints_inserted (void)
12415{
12416 return (single_step_breakpoints[0] != NULL
12417 || single_step_breakpoints[1] != NULL);
12418}
12419
8181d85f
DJ
12420/* Remove and delete any breakpoints used for software single step. */
12421
12422void
12423remove_single_step_breakpoints (void)
12424{
12425 gdb_assert (single_step_breakpoints[0] != NULL);
12426
12427 /* See insert_single_step_breakpoint for more about this deprecated
12428 call. */
a6d9a66e
UW
12429 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12430 single_step_breakpoints[0]);
12431 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12432 single_step_breakpoints[0] = NULL;
12433
12434 if (single_step_breakpoints[1] != NULL)
12435 {
a6d9a66e
UW
12436 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12437 single_step_breakpoints[1]);
12438 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12439 single_step_breakpoints[1] = NULL;
12440 }
12441}
12442
d03285ec
UW
12443/* Delete software single step breakpoints without removing them from
12444 the inferior. This is intended to be used if the inferior's address
12445 space where they were inserted is already gone, e.g. after exit or
12446 exec. */
12447
12448void
12449cancel_single_step_breakpoints (void)
12450{
12451 int i;
12452
12453 for (i = 0; i < 2; i++)
12454 if (single_step_breakpoints[i])
12455 {
12456 xfree (single_step_breakpoints[i]);
12457 single_step_breakpoints[i] = NULL;
12458 single_step_gdbarch[i] = NULL;
12459 }
12460}
12461
12462/* Detach software single-step breakpoints from INFERIOR_PTID without
12463 removing them. */
12464
12465static void
12466detach_single_step_breakpoints (void)
12467{
12468 int i;
12469
12470 for (i = 0; i < 2; i++)
12471 if (single_step_breakpoints[i])
12472 target_remove_breakpoint (single_step_gdbarch[i],
12473 single_step_breakpoints[i]);
12474}
12475
4a64f543
MS
12476/* Check whether a software single-step breakpoint is inserted at
12477 PC. */
1aafd4da
UW
12478
12479static int
cc59ec59
MS
12480single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12481 CORE_ADDR pc)
1aafd4da
UW
12482{
12483 int i;
12484
12485 for (i = 0; i < 2; i++)
12486 {
12487 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12488 if (bp_tgt
12489 && breakpoint_address_match (bp_tgt->placed_address_space,
12490 bp_tgt->placed_address,
12491 aspace, pc))
1aafd4da
UW
12492 return 1;
12493 }
12494
12495 return 0;
12496}
12497
a96d9b2e
SDJ
12498/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12499 non-zero otherwise. */
12500static int
12501is_syscall_catchpoint_enabled (struct breakpoint *bp)
12502{
12503 if (syscall_catchpoint_p (bp)
12504 && bp->enable_state != bp_disabled
12505 && bp->enable_state != bp_call_disabled)
12506 return 1;
12507 else
12508 return 0;
12509}
12510
12511int
12512catch_syscall_enabled (void)
12513{
12514 struct inferior *inf = current_inferior ();
12515
12516 return inf->total_syscalls_count != 0;
12517}
12518
12519int
12520catching_syscall_number (int syscall_number)
12521{
12522 struct breakpoint *bp;
12523
12524 ALL_BREAKPOINTS (bp)
12525 if (is_syscall_catchpoint_enabled (bp))
12526 {
be5c67c1
PA
12527 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12528
12529 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12530 {
12531 int i, iter;
12532 for (i = 0;
be5c67c1 12533 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12534 i++)
12535 if (syscall_number == iter)
12536 return 1;
12537 }
12538 else
12539 return 1;
12540 }
12541
12542 return 0;
12543}
12544
12545/* Complete syscall names. Used by "catch syscall". */
12546static char **
12547catch_syscall_completer (struct cmd_list_element *cmd,
12548 char *text, char *word)
12549{
12550 const char **list = get_syscall_names ();
c38eea1a
MS
12551 char **retlist
12552 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12553
c38eea1a
MS
12554 xfree (list);
12555 return retlist;
a96d9b2e
SDJ
12556}
12557
1042e4c0
SS
12558/* Tracepoint-specific operations. */
12559
12560/* Set tracepoint count to NUM. */
12561static void
12562set_tracepoint_count (int num)
12563{
12564 tracepoint_count = num;
4fa62494 12565 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12566}
12567
12568void
12569trace_command (char *arg, int from_tty)
12570{
8cdf0e15
VP
12571 if (create_breakpoint (get_current_arch (),
12572 arg,
12573 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12574 0 /* tempflag */,
12575 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12576 0 /* Ignore count */,
12577 pending_break_support,
348d480f 12578 &tracepoint_breakpoint_ops,
8cdf0e15 12579 from_tty,
84f4c1fe
PM
12580 1 /* enabled */,
12581 0 /* internal */))
fd9b8c24 12582 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12583}
12584
7a697b8d
SS
12585void
12586ftrace_command (char *arg, int from_tty)
12587{
8cdf0e15
VP
12588 if (create_breakpoint (get_current_arch (),
12589 arg,
12590 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12591 0 /* tempflag */,
12592 bp_fast_tracepoint /* type_wanted */,
12593 0 /* Ignore count */,
12594 pending_break_support,
348d480f 12595 &tracepoint_breakpoint_ops,
0fb4aa4b 12596 from_tty,
84f4c1fe
PM
12597 1 /* enabled */,
12598 0 /* internal */))
0fb4aa4b
PA
12599 set_tracepoint_count (breakpoint_count);
12600}
12601
12602/* strace command implementation. Creates a static tracepoint. */
12603
12604void
12605strace_command (char *arg, int from_tty)
12606{
12607 if (create_breakpoint (get_current_arch (),
12608 arg,
12609 NULL, 0, 1 /* parse arg */,
12610 0 /* tempflag */,
12611 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12612 0 /* Ignore count */,
12613 pending_break_support,
348d480f 12614 &tracepoint_breakpoint_ops,
8cdf0e15 12615 from_tty,
84f4c1fe
PM
12616 1 /* enabled */,
12617 0 /* internal */))
fd9b8c24 12618 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12619}
12620
409873ef
SS
12621/* Set up a fake reader function that gets command lines from a linked
12622 list that was acquired during tracepoint uploading. */
12623
12624static struct uploaded_tp *this_utp;
3149d8c1 12625static int next_cmd;
409873ef
SS
12626
12627static char *
12628read_uploaded_action (void)
12629{
12630 char *rslt;
12631
3149d8c1 12632 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12633
3149d8c1 12634 next_cmd++;
409873ef
SS
12635
12636 return rslt;
12637}
12638
00bf0b85
SS
12639/* Given information about a tracepoint as recorded on a target (which
12640 can be either a live system or a trace file), attempt to create an
12641 equivalent GDB tracepoint. This is not a reliable process, since
12642 the target does not necessarily have all the information used when
12643 the tracepoint was originally defined. */
12644
d9b3f62e 12645struct tracepoint *
00bf0b85 12646create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12647{
409873ef 12648 char *addr_str, small_buf[100];
d9b3f62e 12649 struct tracepoint *tp;
fd9b8c24 12650
409873ef
SS
12651 if (utp->at_string)
12652 addr_str = utp->at_string;
12653 else
12654 {
12655 /* In the absence of a source location, fall back to raw
12656 address. Since there is no way to confirm that the address
12657 means the same thing as when the trace was started, warn the
12658 user. */
3e43a32a
MS
12659 warning (_("Uploaded tracepoint %d has no "
12660 "source location, using raw address"),
409873ef
SS
12661 utp->number);
12662 sprintf (small_buf, "*%s", hex_string (utp->addr));
12663 addr_str = small_buf;
12664 }
12665
12666 /* There's not much we can do with a sequence of bytecodes. */
12667 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12668 warning (_("Uploaded tracepoint %d condition "
12669 "has no source form, ignoring it"),
409873ef 12670 utp->number);
d5551862 12671
8cdf0e15 12672 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12673 addr_str,
12674 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12675 0 /* tempflag */,
0fb4aa4b 12676 utp->type /* type_wanted */,
8cdf0e15
VP
12677 0 /* Ignore count */,
12678 pending_break_support,
348d480f 12679 &tracepoint_breakpoint_ops,
8cdf0e15 12680 0 /* from_tty */,
84f4c1fe
PM
12681 utp->enabled /* enabled */,
12682 0 /* internal */))
fd9b8c24
PA
12683 return NULL;
12684
00bf0b85
SS
12685 set_tracepoint_count (breakpoint_count);
12686
409873ef 12687 /* Get the tracepoint we just created. */
fd9b8c24
PA
12688 tp = get_tracepoint (tracepoint_count);
12689 gdb_assert (tp != NULL);
d5551862 12690
00bf0b85
SS
12691 if (utp->pass > 0)
12692 {
d9b3f62e 12693 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12694
409873ef 12695 trace_pass_command (small_buf, 0);
00bf0b85
SS
12696 }
12697
409873ef
SS
12698 /* If we have uploaded versions of the original commands, set up a
12699 special-purpose "reader" function and call the usual command line
12700 reader, then pass the result to the breakpoint command-setting
12701 function. */
3149d8c1 12702 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12703 {
409873ef 12704 struct command_line *cmd_list;
00bf0b85 12705
409873ef 12706 this_utp = utp;
3149d8c1 12707 next_cmd = 0;
d5551862 12708
409873ef
SS
12709 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12710
d9b3f62e 12711 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12712 }
3149d8c1
SS
12713 else if (!VEC_empty (char_ptr, utp->actions)
12714 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12715 warning (_("Uploaded tracepoint %d actions "
12716 "have no source form, ignoring them"),
409873ef 12717 utp->number);
00bf0b85
SS
12718
12719 return tp;
d9b3f62e 12720}
00bf0b85 12721
1042e4c0
SS
12722/* Print information on tracepoint number TPNUM_EXP, or all if
12723 omitted. */
12724
12725static void
e5a67952 12726tracepoints_info (char *args, int from_tty)
1042e4c0 12727{
e5a67952 12728 int num_printed;
1042e4c0 12729
e5a67952 12730 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12731
12732 if (num_printed == 0)
1042e4c0 12733 {
e5a67952 12734 if (args == NULL || *args == '\0')
d77f58be
SS
12735 ui_out_message (uiout, 0, "No tracepoints.\n");
12736 else
e5a67952 12737 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12738 }
ad443146
SS
12739
12740 default_collect_info ();
1042e4c0
SS
12741}
12742
4a64f543 12743/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12744 Not supported by all targets. */
12745static void
12746enable_trace_command (char *args, int from_tty)
12747{
12748 enable_command (args, from_tty);
12749}
12750
4a64f543 12751/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12752 Not supported by all targets. */
12753static void
12754disable_trace_command (char *args, int from_tty)
12755{
12756 disable_command (args, from_tty);
12757}
12758
4a64f543 12759/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12760static void
12761delete_trace_command (char *arg, int from_tty)
12762{
35df4500 12763 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12764
12765 dont_repeat ();
12766
12767 if (arg == 0)
12768 {
12769 int breaks_to_delete = 0;
12770
12771 /* Delete all breakpoints if no argument.
12772 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12773 have to be deleted with an explicit breakpoint number
12774 argument. */
1042e4c0 12775 ALL_TRACEPOINTS (b)
46c6471b 12776 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12777 {
12778 breaks_to_delete = 1;
12779 break;
12780 }
1042e4c0
SS
12781
12782 /* Ask user only if there are some breakpoints to delete. */
12783 if (!from_tty
12784 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12785 {
35df4500 12786 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12787 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12788 delete_breakpoint (b);
1042e4c0
SS
12789 }
12790 }
12791 else
51be5b68 12792 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12793}
12794
197f0a60
TT
12795/* Helper function for trace_pass_command. */
12796
12797static void
d9b3f62e 12798trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12799{
d9b3f62e
PA
12800 tp->pass_count = count;
12801 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12802 if (from_tty)
12803 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12804 tp->base.number, count);
197f0a60
TT
12805}
12806
1042e4c0
SS
12807/* Set passcount for tracepoint.
12808
12809 First command argument is passcount, second is tracepoint number.
12810 If tracepoint number omitted, apply to most recently defined.
12811 Also accepts special argument "all". */
12812
12813static void
12814trace_pass_command (char *args, int from_tty)
12815{
d9b3f62e 12816 struct tracepoint *t1;
1042e4c0 12817 unsigned int count;
1042e4c0
SS
12818
12819 if (args == 0 || *args == 0)
3e43a32a
MS
12820 error (_("passcount command requires an "
12821 "argument (count + optional TP num)"));
1042e4c0 12822
4a64f543 12823 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12824
12825 while (*args && isspace ((int) *args))
12826 args++;
12827
12828 if (*args && strncasecmp (args, "all", 3) == 0)
12829 {
d9b3f62e
PA
12830 struct breakpoint *b;
12831
1042e4c0 12832 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12833 if (*args)
12834 error (_("Junk at end of arguments."));
1042e4c0 12835
d9b3f62e 12836 ALL_TRACEPOINTS (b)
197f0a60 12837 {
d9b3f62e 12838 t1 = (struct tracepoint *) b;
197f0a60
TT
12839 trace_pass_set_count (t1, count, from_tty);
12840 }
12841 }
12842 else if (*args == '\0')
1042e4c0 12843 {
197f0a60 12844 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12845 if (t1)
197f0a60
TT
12846 trace_pass_set_count (t1, count, from_tty);
12847 }
12848 else
12849 {
12850 struct get_number_or_range_state state;
12851
12852 init_number_or_range (&state, args);
12853 while (!state.finished)
1042e4c0 12854 {
197f0a60
TT
12855 t1 = get_tracepoint_by_number (&args, &state, 1);
12856 if (t1)
12857 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12858 }
12859 }
1042e4c0
SS
12860}
12861
d9b3f62e 12862struct tracepoint *
1042e4c0
SS
12863get_tracepoint (int num)
12864{
12865 struct breakpoint *t;
12866
12867 ALL_TRACEPOINTS (t)
12868 if (t->number == num)
d9b3f62e 12869 return (struct tracepoint *) t;
1042e4c0
SS
12870
12871 return NULL;
12872}
12873
d5551862
SS
12874/* Find the tracepoint with the given target-side number (which may be
12875 different from the tracepoint number after disconnecting and
12876 reconnecting). */
12877
d9b3f62e 12878struct tracepoint *
d5551862
SS
12879get_tracepoint_by_number_on_target (int num)
12880{
d9b3f62e 12881 struct breakpoint *b;
d5551862 12882
d9b3f62e
PA
12883 ALL_TRACEPOINTS (b)
12884 {
12885 struct tracepoint *t = (struct tracepoint *) b;
12886
12887 if (t->number_on_target == num)
12888 return t;
12889 }
d5551862
SS
12890
12891 return NULL;
12892}
12893
1042e4c0 12894/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12895 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12896 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 12897 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 12898struct tracepoint *
197f0a60
TT
12899get_tracepoint_by_number (char **arg,
12900 struct get_number_or_range_state *state,
12901 int optional_p)
1042e4c0
SS
12902{
12903 extern int tracepoint_count;
12904 struct breakpoint *t;
12905 int tpnum;
12906 char *instring = arg == NULL ? NULL : *arg;
12907
197f0a60
TT
12908 if (state)
12909 {
12910 gdb_assert (!state->finished);
12911 tpnum = get_number_or_range (state);
12912 }
12913 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12914 {
12915 if (optional_p)
12916 tpnum = tracepoint_count;
12917 else
12918 error_no_arg (_("tracepoint number"));
12919 }
12920 else
197f0a60 12921 tpnum = get_number (arg);
1042e4c0
SS
12922
12923 if (tpnum <= 0)
12924 {
12925 if (instring && *instring)
12926 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12927 instring);
12928 else
3e43a32a
MS
12929 printf_filtered (_("Tracepoint argument missing "
12930 "and no previous tracepoint\n"));
1042e4c0
SS
12931 return NULL;
12932 }
12933
12934 ALL_TRACEPOINTS (t)
12935 if (t->number == tpnum)
12936 {
d9b3f62e 12937 return (struct tracepoint *) t;
1042e4c0
SS
12938 }
12939
1042e4c0
SS
12940 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12941 return NULL;
12942}
12943
d9b3f62e
PA
12944void
12945print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
12946{
12947 if (b->thread != -1)
12948 fprintf_unfiltered (fp, " thread %d", b->thread);
12949
12950 if (b->task != 0)
12951 fprintf_unfiltered (fp, " task %d", b->task);
12952
12953 fprintf_unfiltered (fp, "\n");
12954}
12955
6149aea9
PA
12956/* Save information on user settable breakpoints (watchpoints, etc) to
12957 a new script file named FILENAME. If FILTER is non-NULL, call it
12958 on each breakpoint and only include the ones for which it returns
12959 non-zero. */
12960
1042e4c0 12961static void
6149aea9
PA
12962save_breakpoints (char *filename, int from_tty,
12963 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12964{
12965 struct breakpoint *tp;
6149aea9 12966 int any = 0;
a7bdde9e 12967 char *pathname;
1042e4c0 12968 struct cleanup *cleanup;
a7bdde9e 12969 struct ui_file *fp;
6149aea9 12970 int extra_trace_bits = 0;
1042e4c0 12971
6149aea9
PA
12972 if (filename == 0 || *filename == 0)
12973 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12974
12975 /* See if we have anything to save. */
6149aea9 12976 ALL_BREAKPOINTS (tp)
1042e4c0 12977 {
6149aea9 12978 /* Skip internal and momentary breakpoints. */
09d682a4 12979 if (!user_breakpoint_p (tp))
6149aea9
PA
12980 continue;
12981
12982 /* If we have a filter, only save the breakpoints it accepts. */
12983 if (filter && !filter (tp))
12984 continue;
12985
12986 any = 1;
12987
12988 if (is_tracepoint (tp))
12989 {
12990 extra_trace_bits = 1;
12991
12992 /* We can stop searching. */
12993 break;
12994 }
1042e4c0 12995 }
6149aea9
PA
12996
12997 if (!any)
1042e4c0 12998 {
6149aea9 12999 warning (_("Nothing to save."));
1042e4c0
SS
13000 return;
13001 }
13002
6149aea9 13003 pathname = tilde_expand (filename);
1042e4c0 13004 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13005 fp = gdb_fopen (pathname, "w");
059fb39f 13006 if (!fp)
6149aea9
PA
13007 error (_("Unable to open file '%s' for saving (%s)"),
13008 filename, safe_strerror (errno));
a7bdde9e 13009 make_cleanup_ui_file_delete (fp);
8bf6485c 13010
6149aea9
PA
13011 if (extra_trace_bits)
13012 save_trace_state_variables (fp);
8bf6485c 13013
6149aea9 13014 ALL_BREAKPOINTS (tp)
1042e4c0 13015 {
6149aea9 13016 /* Skip internal and momentary breakpoints. */
09d682a4 13017 if (!user_breakpoint_p (tp))
6149aea9 13018 continue;
8bf6485c 13019
6149aea9
PA
13020 /* If we have a filter, only save the breakpoints it accepts. */
13021 if (filter && !filter (tp))
13022 continue;
13023
348d480f 13024 tp->ops->print_recreate (tp, fp);
1042e4c0 13025
6149aea9
PA
13026 /* Note, we can't rely on tp->number for anything, as we can't
13027 assume the recreated breakpoint numbers will match. Use $bpnum
13028 instead. */
13029
13030 if (tp->cond_string)
13031 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13032
13033 if (tp->ignore_count)
13034 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13035
a7bdde9e 13036 if (tp->commands)
1042e4c0 13037 {
a7bdde9e
VP
13038 volatile struct gdb_exception ex;
13039
6149aea9 13040 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
13041
13042 ui_out_redirect (uiout, fp);
14dba4b4 13043 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13044 {
9add0f1b 13045 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
13046 }
13047 ui_out_redirect (uiout, NULL);
1042e4c0 13048
a7bdde9e
VP
13049 if (ex.reason < 0)
13050 throw_exception (ex);
1042e4c0 13051
a7bdde9e 13052 fprintf_unfiltered (fp, " end\n");
1042e4c0 13053 }
6149aea9
PA
13054
13055 if (tp->enable_state == bp_disabled)
13056 fprintf_unfiltered (fp, "disable\n");
13057
13058 /* If this is a multi-location breakpoint, check if the locations
13059 should be individually disabled. Watchpoint locations are
13060 special, and not user visible. */
13061 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13062 {
13063 struct bp_location *loc;
13064 int n = 1;
13065
13066 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13067 if (!loc->enabled)
13068 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13069 }
1042e4c0 13070 }
8bf6485c 13071
6149aea9 13072 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13073 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13074
1042e4c0
SS
13075 do_cleanups (cleanup);
13076 if (from_tty)
6149aea9
PA
13077 printf_filtered (_("Saved to file '%s'.\n"), filename);
13078}
13079
13080/* The `save breakpoints' command. */
13081
13082static void
13083save_breakpoints_command (char *args, int from_tty)
13084{
13085 save_breakpoints (args, from_tty, NULL);
13086}
13087
13088/* The `save tracepoints' command. */
13089
13090static void
13091save_tracepoints_command (char *args, int from_tty)
13092{
13093 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13094}
13095
13096/* Create a vector of all tracepoints. */
13097
13098VEC(breakpoint_p) *
eeae04df 13099all_tracepoints (void)
1042e4c0
SS
13100{
13101 VEC(breakpoint_p) *tp_vec = 0;
13102 struct breakpoint *tp;
13103
13104 ALL_TRACEPOINTS (tp)
13105 {
13106 VEC_safe_push (breakpoint_p, tp_vec, tp);
13107 }
13108
13109 return tp_vec;
13110}
13111
c906108c 13112\f
4a64f543
MS
13113/* This help string is used for the break, hbreak, tbreak and thbreak
13114 commands. It is defined as a macro to prevent duplication.
13115 COMMAND should be a string constant containing the name of the
13116 command. */
31e2b00f
AS
13117#define BREAK_ARGS_HELP(command) \
13118command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13119LOCATION may be a line number, function name, or \"*\" and an address.\n\
13120If a line number is specified, break at start of code for that line.\n\
13121If a function is specified, break at start of code for that function.\n\
13122If an address is specified, break at that exact address.\n\
dc10affe
PA
13123With no LOCATION, uses current execution address of the selected\n\
13124stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13125\n\
13126THREADNUM is the number from \"info threads\".\n\
13127CONDITION is a boolean expression.\n\
13128\n\
d41c0fc8
PA
13129Multiple breakpoints at one place are permitted, and useful if their\n\
13130conditions are different.\n\
31e2b00f
AS
13131\n\
13132Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13133
44feb3ce
TT
13134/* List of subcommands for "catch". */
13135static struct cmd_list_element *catch_cmdlist;
13136
13137/* List of subcommands for "tcatch". */
13138static struct cmd_list_element *tcatch_cmdlist;
13139
9ac4176b 13140void
44feb3ce
TT
13141add_catch_command (char *name, char *docstring,
13142 void (*sfunc) (char *args, int from_tty,
13143 struct cmd_list_element *command),
a96d9b2e
SDJ
13144 char **(*completer) (struct cmd_list_element *cmd,
13145 char *text, char *word),
44feb3ce
TT
13146 void *user_data_catch,
13147 void *user_data_tcatch)
13148{
13149 struct cmd_list_element *command;
13150
13151 command = add_cmd (name, class_breakpoint, NULL, docstring,
13152 &catch_cmdlist);
13153 set_cmd_sfunc (command, sfunc);
13154 set_cmd_context (command, user_data_catch);
a96d9b2e 13155 set_cmd_completer (command, completer);
44feb3ce
TT
13156
13157 command = add_cmd (name, class_breakpoint, NULL, docstring,
13158 &tcatch_cmdlist);
13159 set_cmd_sfunc (command, sfunc);
13160 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13161 set_cmd_completer (command, completer);
44feb3ce
TT
13162}
13163
6c95b8df 13164static void
a79b8f6e 13165clear_syscall_counts (struct inferior *inf)
6c95b8df 13166{
6c95b8df
PA
13167 inf->total_syscalls_count = 0;
13168 inf->any_syscall_count = 0;
13169 VEC_free (int, inf->syscalls_counts);
13170}
13171
6149aea9
PA
13172static void
13173save_command (char *arg, int from_tty)
13174{
3e43a32a
MS
13175 printf_unfiltered (_("\"save\" must be followed by "
13176 "the name of a save subcommand.\n"));
6149aea9
PA
13177 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13178}
13179
84f4c1fe
PM
13180struct breakpoint *
13181iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13182 void *data)
13183{
35df4500 13184 struct breakpoint *b, *b_tmp;
84f4c1fe 13185
35df4500 13186 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13187 {
13188 if ((*callback) (b, data))
13189 return b;
13190 }
13191
13192 return NULL;
13193}
13194
2060206e
PA
13195void
13196initialize_breakpoint_ops (void)
13197{
13198 static int initialized = 0;
13199
13200 struct breakpoint_ops *ops;
13201
13202 if (initialized)
13203 return;
13204 initialized = 1;
13205
13206 /* The breakpoint_ops structure to be inherit by all kinds of
13207 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13208 internal and momentary breakpoints, etc.). */
13209 ops = &bkpt_base_breakpoint_ops;
13210 *ops = base_breakpoint_ops;
13211 ops->re_set = bkpt_re_set;
13212 ops->insert_location = bkpt_insert_location;
13213 ops->remove_location = bkpt_remove_location;
13214 ops->breakpoint_hit = bkpt_breakpoint_hit;
13215
13216 /* The breakpoint_ops structure to be used in regular breakpoints. */
13217 ops = &bkpt_breakpoint_ops;
13218 *ops = bkpt_base_breakpoint_ops;
13219 ops->re_set = bkpt_re_set;
13220 ops->resources_needed = bkpt_resources_needed;
13221 ops->print_it = bkpt_print_it;
13222 ops->print_mention = bkpt_print_mention;
13223 ops->print_recreate = bkpt_print_recreate;
13224
13225 /* Ranged breakpoints. */
13226 ops = &ranged_breakpoint_ops;
13227 *ops = bkpt_breakpoint_ops;
13228 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13229 ops->resources_needed = resources_needed_ranged_breakpoint;
13230 ops->print_it = print_it_ranged_breakpoint;
13231 ops->print_one = print_one_ranged_breakpoint;
13232 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13233 ops->print_mention = print_mention_ranged_breakpoint;
13234 ops->print_recreate = print_recreate_ranged_breakpoint;
13235
13236 /* Internal breakpoints. */
13237 ops = &internal_breakpoint_ops;
13238 *ops = bkpt_base_breakpoint_ops;
13239 ops->re_set = internal_bkpt_re_set;
13240 ops->check_status = internal_bkpt_check_status;
13241 ops->print_it = internal_bkpt_print_it;
13242 ops->print_mention = internal_bkpt_print_mention;
13243
13244 /* Momentary breakpoints. */
13245 ops = &momentary_breakpoint_ops;
13246 *ops = bkpt_base_breakpoint_ops;
13247 ops->re_set = momentary_bkpt_re_set;
13248 ops->check_status = momentary_bkpt_check_status;
13249 ops->print_it = momentary_bkpt_print_it;
13250 ops->print_mention = momentary_bkpt_print_mention;
13251
13252 /* GNU v3 exception catchpoints. */
13253 ops = &gnu_v3_exception_catchpoint_ops;
13254 *ops = bkpt_breakpoint_ops;
13255 ops->print_it = print_it_exception_catchpoint;
13256 ops->print_one = print_one_exception_catchpoint;
13257 ops->print_mention = print_mention_exception_catchpoint;
13258 ops->print_recreate = print_recreate_exception_catchpoint;
13259
13260 /* Watchpoints. */
13261 ops = &watchpoint_breakpoint_ops;
13262 *ops = base_breakpoint_ops;
3a5c3e22 13263 ops->dtor = dtor_watchpoint;
2060206e
PA
13264 ops->re_set = re_set_watchpoint;
13265 ops->insert_location = insert_watchpoint;
13266 ops->remove_location = remove_watchpoint;
13267 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13268 ops->check_status = check_status_watchpoint;
13269 ops->resources_needed = resources_needed_watchpoint;
13270 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13271 ops->print_it = print_it_watchpoint;
13272 ops->print_mention = print_mention_watchpoint;
13273 ops->print_recreate = print_recreate_watchpoint;
13274
13275 /* Masked watchpoints. */
13276 ops = &masked_watchpoint_breakpoint_ops;
13277 *ops = watchpoint_breakpoint_ops;
13278 ops->insert_location = insert_masked_watchpoint;
13279 ops->remove_location = remove_masked_watchpoint;
13280 ops->resources_needed = resources_needed_masked_watchpoint;
13281 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13282 ops->print_it = print_it_masked_watchpoint;
13283 ops->print_one_detail = print_one_detail_masked_watchpoint;
13284 ops->print_mention = print_mention_masked_watchpoint;
13285 ops->print_recreate = print_recreate_masked_watchpoint;
13286
13287 /* Tracepoints. */
13288 ops = &tracepoint_breakpoint_ops;
13289 *ops = base_breakpoint_ops;
13290 ops->re_set = tracepoint_re_set;
13291 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13292 ops->print_one_detail = tracepoint_print_one_detail;
13293 ops->print_mention = tracepoint_print_mention;
13294 ops->print_recreate = tracepoint_print_recreate;
13295
13296 /* Fork catchpoints. */
13297 ops = &catch_fork_breakpoint_ops;
13298 *ops = base_breakpoint_ops;
13299 ops->insert_location = insert_catch_fork;
13300 ops->remove_location = remove_catch_fork;
13301 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13302 ops->print_it = print_it_catch_fork;
13303 ops->print_one = print_one_catch_fork;
13304 ops->print_mention = print_mention_catch_fork;
13305 ops->print_recreate = print_recreate_catch_fork;
13306
13307 /* Vfork catchpoints. */
13308 ops = &catch_vfork_breakpoint_ops;
13309 *ops = base_breakpoint_ops;
13310 ops->insert_location = insert_catch_vfork;
13311 ops->remove_location = remove_catch_vfork;
13312 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13313 ops->print_it = print_it_catch_vfork;
13314 ops->print_one = print_one_catch_vfork;
13315 ops->print_mention = print_mention_catch_vfork;
13316 ops->print_recreate = print_recreate_catch_vfork;
13317
13318 /* Exec catchpoints. */
13319 ops = &catch_exec_breakpoint_ops;
13320 *ops = base_breakpoint_ops;
13321 ops->dtor = dtor_catch_exec;
13322 ops->insert_location = insert_catch_exec;
13323 ops->remove_location = remove_catch_exec;
13324 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13325 ops->print_it = print_it_catch_exec;
13326 ops->print_one = print_one_catch_exec;
13327 ops->print_mention = print_mention_catch_exec;
13328 ops->print_recreate = print_recreate_catch_exec;
13329
13330 /* Syscall catchpoints. */
13331 ops = &catch_syscall_breakpoint_ops;
13332 *ops = base_breakpoint_ops;
13333 ops->dtor = dtor_catch_syscall;
13334 ops->insert_location = insert_catch_syscall;
13335 ops->remove_location = remove_catch_syscall;
13336 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13337 ops->print_it = print_it_catch_syscall;
13338 ops->print_one = print_one_catch_syscall;
13339 ops->print_mention = print_mention_catch_syscall;
13340 ops->print_recreate = print_recreate_catch_syscall;
13341}
13342
c906108c 13343void
fba45db2 13344_initialize_breakpoint (void)
c906108c
SS
13345{
13346 struct cmd_list_element *c;
13347
2060206e
PA
13348 initialize_breakpoint_ops ();
13349
84acb35a 13350 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13351 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13352 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13353
17450429
PP
13354 breakpoint_objfile_key = register_objfile_data ();
13355
c906108c
SS
13356 breakpoint_chain = 0;
13357 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13358 before a breakpoint is set. */
13359 breakpoint_count = 0;
13360
1042e4c0
SS
13361 tracepoint_count = 0;
13362
1bedd215
AC
13363 add_com ("ignore", class_breakpoint, ignore_command, _("\
13364Set ignore-count of breakpoint number N to COUNT.\n\
13365Usage is `ignore N COUNT'."));
c906108c 13366 if (xdb_commands)
c5aa993b 13367 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13368
1bedd215
AC
13369 add_com ("commands", class_breakpoint, commands_command, _("\
13370Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13371Give breakpoint number as argument after \"commands\".\n\
13372With no argument, the targeted breakpoint is the last one set.\n\
13373The commands themselves follow starting on the next line.\n\
13374Type a line containing \"end\" to indicate the end of them.\n\
13375Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13376then no output is printed when it is hit, except what the commands print."));
c906108c 13377
1bedd215
AC
13378 add_com ("condition", class_breakpoint, condition_command, _("\
13379Specify breakpoint number N to break only if COND is true.\n\
c906108c 13380Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13381expression to be evaluated whenever breakpoint N is reached."));
c906108c 13382
1bedd215 13383 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13384Set a temporary breakpoint.\n\
c906108c
SS
13385Like \"break\" except the breakpoint is only temporary,\n\
13386so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13387by using \"enable delete\" on the breakpoint number.\n\
13388\n"
13389BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13390 set_cmd_completer (c, location_completer);
c94fdfd0 13391
1bedd215 13392 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13393Set a hardware assisted breakpoint.\n\
c906108c 13394Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13395some target hardware may not have this support.\n\
13396\n"
13397BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13398 set_cmd_completer (c, location_completer);
c906108c 13399
1bedd215 13400 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13401Set a temporary hardware assisted breakpoint.\n\
c906108c 13402Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13403so it will be deleted when hit.\n\
13404\n"
13405BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13406 set_cmd_completer (c, location_completer);
c906108c 13407
1bedd215
AC
13408 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13409Enable some breakpoints.\n\
c906108c
SS
13410Give breakpoint numbers (separated by spaces) as arguments.\n\
13411With no subcommand, breakpoints are enabled until you command otherwise.\n\
13412This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13413With a subcommand you can enable temporarily."),
c906108c
SS
13414 &enablelist, "enable ", 1, &cmdlist);
13415 if (xdb_commands)
1bedd215
AC
13416 add_com ("ab", class_breakpoint, enable_command, _("\
13417Enable some breakpoints.\n\
c906108c
SS
13418Give breakpoint numbers (separated by spaces) as arguments.\n\
13419With no subcommand, breakpoints are enabled until you command otherwise.\n\
13420This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13421With a subcommand you can enable temporarily."));
c906108c
SS
13422
13423 add_com_alias ("en", "enable", class_breakpoint, 1);
13424
84951ab5 13425 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13426Enable some breakpoints.\n\
c906108c
SS
13427Give breakpoint numbers (separated by spaces) as arguments.\n\
13428This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13429May be abbreviated to simply \"enable\".\n"),
c5aa993b 13430 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13431
1a966eab
AC
13432 add_cmd ("once", no_class, enable_once_command, _("\
13433Enable breakpoints for one hit. Give breakpoint numbers.\n\
13434If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13435 &enablebreaklist);
13436
1a966eab
AC
13437 add_cmd ("delete", no_class, enable_delete_command, _("\
13438Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13439If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13440 &enablebreaklist);
13441
1a966eab
AC
13442 add_cmd ("delete", no_class, enable_delete_command, _("\
13443Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13444If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13445 &enablelist);
13446
1a966eab
AC
13447 add_cmd ("once", no_class, enable_once_command, _("\
13448Enable breakpoints for one hit. Give breakpoint numbers.\n\
13449If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13450 &enablelist);
13451
1bedd215
AC
13452 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13453Disable some breakpoints.\n\
c906108c
SS
13454Arguments are breakpoint numbers with spaces in between.\n\
13455To disable all breakpoints, give no argument.\n\
64b9b334 13456A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13457 &disablelist, "disable ", 1, &cmdlist);
13458 add_com_alias ("dis", "disable", class_breakpoint, 1);
13459 add_com_alias ("disa", "disable", class_breakpoint, 1);
13460 if (xdb_commands)
1bedd215
AC
13461 add_com ("sb", class_breakpoint, disable_command, _("\
13462Disable some breakpoints.\n\
c906108c
SS
13463Arguments are breakpoint numbers with spaces in between.\n\
13464To disable all breakpoints, give no argument.\n\
64b9b334 13465A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13466
1a966eab
AC
13467 add_cmd ("breakpoints", class_alias, disable_command, _("\
13468Disable some breakpoints.\n\
c906108c
SS
13469Arguments are breakpoint numbers with spaces in between.\n\
13470To disable all breakpoints, give no argument.\n\
64b9b334 13471A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13472This command may be abbreviated \"disable\"."),
c906108c
SS
13473 &disablelist);
13474
1bedd215
AC
13475 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13476Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13477Arguments are breakpoint numbers with spaces in between.\n\
13478To delete all breakpoints, give no argument.\n\
13479\n\
13480Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13481The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13482 &deletelist, "delete ", 1, &cmdlist);
13483 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13484 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13485 if (xdb_commands)
1bedd215
AC
13486 add_com ("db", class_breakpoint, delete_command, _("\
13487Delete some breakpoints.\n\
c906108c 13488Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13489To delete all breakpoints, give no argument.\n"));
c906108c 13490
1a966eab
AC
13491 add_cmd ("breakpoints", class_alias, delete_command, _("\
13492Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13493Arguments are breakpoint numbers with spaces in between.\n\
13494To delete all breakpoints, give no argument.\n\
1a966eab 13495This command may be abbreviated \"delete\"."),
c906108c
SS
13496 &deletelist);
13497
1bedd215
AC
13498 add_com ("clear", class_breakpoint, clear_command, _("\
13499Clear breakpoint at specified line or function.\n\
c906108c
SS
13500Argument may be line number, function name, or \"*\" and an address.\n\
13501If line number is specified, all breakpoints in that line are cleared.\n\
13502If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13503If an address is specified, breakpoints at that address are cleared.\n\
13504\n\
13505With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13506is executing in.\n\
13507\n\
1bedd215 13508See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13509 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13510
1bedd215 13511 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13512Set breakpoint at specified line or function.\n"
13513BREAK_ARGS_HELP ("break")));
5ba2abeb 13514 set_cmd_completer (c, location_completer);
c94fdfd0 13515
c906108c
SS
13516 add_com_alias ("b", "break", class_run, 1);
13517 add_com_alias ("br", "break", class_run, 1);
13518 add_com_alias ("bre", "break", class_run, 1);
13519 add_com_alias ("brea", "break", class_run, 1);
13520
7681d515
PM
13521 if (xdb_commands)
13522 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13523
13524 if (dbx_commands)
13525 {
1bedd215
AC
13526 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13527Break in function/address or break at a line in the current file."),
c5aa993b
JM
13528 &stoplist, "stop ", 1, &cmdlist);
13529 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13530 _("Break in function or address."), &stoplist);
c5aa993b 13531 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13532 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13533 add_com ("status", class_info, breakpoints_info, _("\
13534Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13535The \"Type\" column indicates one of:\n\
13536\tbreakpoint - normal breakpoint\n\
13537\twatchpoint - watchpoint\n\
13538The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13539the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13540breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13541address and file/line number respectively.\n\
13542\n\
13543Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13544are set to the address of the last breakpoint listed unless the command\n\
13545is prefixed with \"server \".\n\n\
c906108c 13546Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13547breakpoint set."));
c906108c
SS
13548 }
13549
1bedd215 13550 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13551Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13552The \"Type\" column indicates one of:\n\
13553\tbreakpoint - normal breakpoint\n\
13554\twatchpoint - watchpoint\n\
13555The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13556the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13557breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13558address and file/line number respectively.\n\
13559\n\
13560Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13561are set to the address of the last breakpoint listed unless the command\n\
13562is prefixed with \"server \".\n\n\
c906108c 13563Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13564breakpoint set."));
c906108c 13565
6b04bdb7
MS
13566 add_info_alias ("b", "breakpoints", 1);
13567
c906108c 13568 if (xdb_commands)
1bedd215
AC
13569 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13570Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13571The \"Type\" column indicates one of:\n\
13572\tbreakpoint - normal breakpoint\n\
13573\twatchpoint - watchpoint\n\
13574The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13575the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13576breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13577address and file/line number respectively.\n\
13578\n\
13579Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13580are set to the address of the last breakpoint listed unless the command\n\
13581is prefixed with \"server \".\n\n\
c906108c 13582Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13583breakpoint set."));
c906108c 13584
1a966eab
AC
13585 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13586Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13587The \"Type\" column indicates one of:\n\
13588\tbreakpoint - normal breakpoint\n\
13589\twatchpoint - watchpoint\n\
13590\tlongjmp - internal breakpoint used to step through longjmp()\n\
13591\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13592\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13593\tfinish - internal breakpoint used by the \"finish\" command\n\
13594The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13595the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13596breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13597address and file/line number respectively.\n\
13598\n\
13599Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13600are set to the address of the last breakpoint listed unless the command\n\
13601is prefixed with \"server \".\n\n\
c906108c 13602Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13603breakpoint set."),
c906108c
SS
13604 &maintenanceinfolist);
13605
44feb3ce
TT
13606 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13607Set catchpoints to catch events."),
13608 &catch_cmdlist, "catch ",
13609 0/*allow-unknown*/, &cmdlist);
13610
13611 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13612Set temporary catchpoints to catch events."),
13613 &tcatch_cmdlist, "tcatch ",
13614 0/*allow-unknown*/, &cmdlist);
13615
13616 /* Add catch and tcatch sub-commands. */
13617 add_catch_command ("catch", _("\
13618Catch an exception, when caught.\n\
13619With an argument, catch only exceptions with the given name."),
13620 catch_catch_command,
a96d9b2e 13621 NULL,
44feb3ce
TT
13622 CATCH_PERMANENT,
13623 CATCH_TEMPORARY);
13624 add_catch_command ("throw", _("\
13625Catch an exception, when thrown.\n\
13626With an argument, catch only exceptions with the given name."),
13627 catch_throw_command,
a96d9b2e 13628 NULL,
44feb3ce
TT
13629 CATCH_PERMANENT,
13630 CATCH_TEMPORARY);
13631 add_catch_command ("fork", _("Catch calls to fork."),
13632 catch_fork_command_1,
a96d9b2e 13633 NULL,
44feb3ce
TT
13634 (void *) (uintptr_t) catch_fork_permanent,
13635 (void *) (uintptr_t) catch_fork_temporary);
13636 add_catch_command ("vfork", _("Catch calls to vfork."),
13637 catch_fork_command_1,
a96d9b2e 13638 NULL,
44feb3ce
TT
13639 (void *) (uintptr_t) catch_vfork_permanent,
13640 (void *) (uintptr_t) catch_vfork_temporary);
13641 add_catch_command ("exec", _("Catch calls to exec."),
13642 catch_exec_command_1,
a96d9b2e
SDJ
13643 NULL,
13644 CATCH_PERMANENT,
13645 CATCH_TEMPORARY);
13646 add_catch_command ("syscall", _("\
13647Catch system calls by their names and/or numbers.\n\
13648Arguments say which system calls to catch. If no arguments\n\
13649are given, every system call will be caught.\n\
13650Arguments, if given, should be one or more system call names\n\
13651(if your system supports that), or system call numbers."),
13652 catch_syscall_command_1,
13653 catch_syscall_completer,
44feb3ce
TT
13654 CATCH_PERMANENT,
13655 CATCH_TEMPORARY);
c5aa993b 13656
1bedd215
AC
13657 c = add_com ("watch", class_breakpoint, watch_command, _("\
13658Set a watchpoint for an expression.\n\
06a64a0b 13659Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13660A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13661an expression changes.\n\
13662If -l or -location is given, this evaluates EXPRESSION and watches\n\
13663the memory to which it refers."));
65d12d83 13664 set_cmd_completer (c, expression_completer);
c906108c 13665
1bedd215
AC
13666 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13667Set a read watchpoint for an expression.\n\
06a64a0b 13668Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13669A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13670an expression is read.\n\
13671If -l or -location is given, this evaluates EXPRESSION and watches\n\
13672the memory to which it refers."));
65d12d83 13673 set_cmd_completer (c, expression_completer);
c906108c 13674
1bedd215
AC
13675 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13676Set a watchpoint for an expression.\n\
06a64a0b 13677Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13678A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13679an expression is either read or written.\n\
13680If -l or -location is given, this evaluates EXPRESSION and watches\n\
13681the memory to which it refers."));
65d12d83 13682 set_cmd_completer (c, expression_completer);
c906108c 13683
d77f58be 13684 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13685Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13686
920d2a44
AC
13687 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13688 respond to changes - contrary to the description. */
85c07804
AC
13689 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13690 &can_use_hw_watchpoints, _("\
13691Set debugger's willingness to use watchpoint hardware."), _("\
13692Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13693If zero, gdb will not use hardware for new watchpoints, even if\n\
13694such is available. (However, any hardware watchpoints that were\n\
13695created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13696hardware.)"),
13697 NULL,
920d2a44 13698 show_can_use_hw_watchpoints,
85c07804 13699 &setlist, &showlist);
c906108c
SS
13700
13701 can_use_hw_watchpoints = 1;
fa8d40ab 13702
1042e4c0
SS
13703 /* Tracepoint manipulation commands. */
13704
13705 c = add_com ("trace", class_breakpoint, trace_command, _("\
13706Set a tracepoint at specified line or function.\n\
13707\n"
13708BREAK_ARGS_HELP ("trace") "\n\
13709Do \"help tracepoints\" for info on other tracepoint commands."));
13710 set_cmd_completer (c, location_completer);
13711
13712 add_com_alias ("tp", "trace", class_alias, 0);
13713 add_com_alias ("tr", "trace", class_alias, 1);
13714 add_com_alias ("tra", "trace", class_alias, 1);
13715 add_com_alias ("trac", "trace", class_alias, 1);
13716
7a697b8d
SS
13717 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13718Set a fast tracepoint at specified line or function.\n\
13719\n"
13720BREAK_ARGS_HELP ("ftrace") "\n\
13721Do \"help tracepoints\" for info on other tracepoint commands."));
13722 set_cmd_completer (c, location_completer);
13723
0fb4aa4b
PA
13724 c = add_com ("strace", class_breakpoint, strace_command, _("\
13725Set a static tracepoint at specified line, function or marker.\n\
13726\n\
13727strace [LOCATION] [if CONDITION]\n\
13728LOCATION may be a line number, function name, \"*\" and an address,\n\
13729or -m MARKER_ID.\n\
13730If a line number is specified, probe the marker at start of code\n\
13731for that line. If a function is specified, probe the marker at start\n\
13732of code for that function. If an address is specified, probe the marker\n\
13733at that exact address. If a marker id is specified, probe the marker\n\
13734with that name. With no LOCATION, uses current execution address of\n\
13735the selected stack frame.\n\
13736Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13737This collects arbitrary user data passed in the probe point call to the\n\
13738tracing library. You can inspect it when analyzing the trace buffer,\n\
13739by printing the $_sdata variable like any other convenience variable.\n\
13740\n\
13741CONDITION is a boolean expression.\n\
13742\n\
d41c0fc8
PA
13743Multiple tracepoints at one place are permitted, and useful if their\n\
13744conditions are different.\n\
0fb4aa4b
PA
13745\n\
13746Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13747Do \"help tracepoints\" for info on other tracepoint commands."));
13748 set_cmd_completer (c, location_completer);
13749
1042e4c0 13750 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13751Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13752Convenience variable \"$tpnum\" contains the number of the\n\
13753last tracepoint set."));
13754
13755 add_info_alias ("tp", "tracepoints", 1);
13756
13757 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13758Delete specified tracepoints.\n\
13759Arguments are tracepoint numbers, separated by spaces.\n\
13760No argument means delete all tracepoints."),
13761 &deletelist);
13762
13763 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13764Disable specified tracepoints.\n\
13765Arguments are tracepoint numbers, separated by spaces.\n\
13766No argument means disable all tracepoints."),
13767 &disablelist);
13768 deprecate_cmd (c, "disable");
13769
13770 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13771Enable specified tracepoints.\n\
13772Arguments are tracepoint numbers, separated by spaces.\n\
13773No argument means enable all tracepoints."),
13774 &enablelist);
13775 deprecate_cmd (c, "enable");
13776
13777 add_com ("passcount", class_trace, trace_pass_command, _("\
13778Set the passcount for a tracepoint.\n\
13779The trace will end when the tracepoint has been passed 'count' times.\n\
13780Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13781if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13782
6149aea9
PA
13783 add_prefix_cmd ("save", class_breakpoint, save_command,
13784 _("Save breakpoint definitions as a script."),
13785 &save_cmdlist, "save ",
13786 0/*allow-unknown*/, &cmdlist);
13787
13788 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13789Save current breakpoint definitions as a script.\n\
cce7e648 13790This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13791catchpoints, tracepoints). Use the 'source' command in another debug\n\
13792session to restore them."),
13793 &save_cmdlist);
13794 set_cmd_completer (c, filename_completer);
13795
13796 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13797Save current tracepoint definitions as a script.\n\
6149aea9
PA
13798Use the 'source' command in another debug session to restore them."),
13799 &save_cmdlist);
1042e4c0
SS
13800 set_cmd_completer (c, filename_completer);
13801
6149aea9
PA
13802 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13803 deprecate_cmd (c, "save tracepoints");
13804
1bedd215 13805 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13806Breakpoint specific settings\n\
13807Configure various breakpoint-specific variables such as\n\
1bedd215 13808pending breakpoint behavior"),
fa8d40ab
JJ
13809 &breakpoint_set_cmdlist, "set breakpoint ",
13810 0/*allow-unknown*/, &setlist);
1bedd215 13811 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13812Breakpoint specific settings\n\
13813Configure various breakpoint-specific variables such as\n\
1bedd215 13814pending breakpoint behavior"),
fa8d40ab
JJ
13815 &breakpoint_show_cmdlist, "show breakpoint ",
13816 0/*allow-unknown*/, &showlist);
13817
7915a72c
AC
13818 add_setshow_auto_boolean_cmd ("pending", no_class,
13819 &pending_break_support, _("\
13820Set debugger's behavior regarding pending breakpoints."), _("\
13821Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13822If on, an unrecognized breakpoint location will cause gdb to create a\n\
13823pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13824an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13825user-query to see if a pending breakpoint should be created."),
2c5b56ce 13826 NULL,
920d2a44 13827 show_pending_break_support,
6e1d7d6c
AC
13828 &breakpoint_set_cmdlist,
13829 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13830
13831 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13832
13833 add_setshow_boolean_cmd ("auto-hw", no_class,
13834 &automatic_hardware_breakpoints, _("\
13835Set automatic usage of hardware breakpoints."), _("\
13836Show automatic usage of hardware breakpoints."), _("\
13837If set, the debugger will automatically use hardware breakpoints for\n\
13838breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13839a warning will be emitted for such breakpoints."),
13840 NULL,
13841 show_automatic_hardware_breakpoints,
13842 &breakpoint_set_cmdlist,
13843 &breakpoint_show_cmdlist);
74960c60 13844
33e5cbd6
PA
13845 add_setshow_enum_cmd ("always-inserted", class_support,
13846 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13847Set mode for inserting breakpoints."), _("\
13848Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13849When this mode is off, breakpoints are inserted in inferior when it is\n\
13850resumed, and removed when execution stops. When this mode is on,\n\
13851breakpoints are inserted immediately and removed only when the user\n\
13852deletes the breakpoint. When this mode is auto (which is the default),\n\
13853the behaviour depends on the non-stop setting (see help set non-stop).\n\
13854In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13855behaves as if always-inserted mode is on; if gdb is controlling the\n\
13856inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13857 NULL,
13858 &show_always_inserted_mode,
13859 &breakpoint_set_cmdlist,
13860 &breakpoint_show_cmdlist);
f1310107
TJB
13861
13862 add_com ("break-range", class_breakpoint, break_range_command, _("\
13863Set a breakpoint for an address range.\n\
13864break-range START-LOCATION, END-LOCATION\n\
13865where START-LOCATION and END-LOCATION can be one of the following:\n\
13866 LINENUM, for that line in the current file,\n\
13867 FILE:LINENUM, for that line in that file,\n\
13868 +OFFSET, for that number of lines after the current line\n\
13869 or the start of the range\n\
13870 FUNCTION, for the first line in that function,\n\
13871 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13872 *ADDRESS, for the instruction at that address.\n\
13873\n\
13874The breakpoint will stop execution of the inferior whenever it executes\n\
13875an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13876range (including START-LOCATION and END-LOCATION)."));
13877
765dc015 13878 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13879
13880 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13881}
This page took 2.032502 seconds and 4 git commands to generate.