]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
2011-02-28 Michael Snyder <msnyder@vmware.com>
[thirdparty/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"
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
fa4727a6 60#include "wrapper.h"
79a45b7d 61#include "valprint.h"
4efc6507 62#include "jit.h"
a96d9b2e 63#include "xml-syscall.h"
65d79d4b 64#include "parser-defs.h"
e9cafbcc 65#include "cli/cli-utils.h"
c906108c 66
1042e4c0
SS
67/* readline include files */
68#include "readline/readline.h"
69#include "readline/history.h"
70
71/* readline defines this. */
72#undef savestring
73
034dad6f 74#include "mi/mi-common.h"
104c1213 75
44feb3ce
TT
76/* Arguments to pass as context to some catch command handlers. */
77#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
78#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 79
4a64f543 80/* Prototypes for local functions. */
c906108c 81
a14ed312 82static void enable_delete_command (char *, int);
c906108c 83
a14ed312 84static void enable_once_command (char *, int);
c906108c 85
a14ed312 86static void disable_command (char *, int);
c906108c 87
a14ed312 88static void enable_command (char *, int);
c906108c 89
95a42b64
TT
90static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
91 void *),
92 void *);
c906108c 93
a14ed312 94static void ignore_command (char *, int);
c906108c 95
4efb68b1 96static int breakpoint_re_set_one (void *);
c906108c 97
a14ed312 98static void clear_command (char *, int);
c906108c 99
a14ed312 100static void catch_command (char *, int);
c906108c 101
e09342b5 102static int can_use_hardware_watchpoint (struct value *, int);
c906108c 103
98deb0da 104static void break_command_1 (char *, int, int);
c906108c 105
a14ed312 106static void mention (struct breakpoint *);
c906108c 107
4a64f543
MS
108/* This function is used in gdbtk sources and thus can not be made
109 static. */
63c252f8 110struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
111 struct symtab_and_line,
112 enum bptype);
c906108c 113
76897487
KB
114static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
115
a6d9a66e
UW
116static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
117 CORE_ADDR bpaddr,
88f7da05 118 enum bptype bptype);
76897487 119
6c95b8df
PA
120static void describe_other_breakpoints (struct gdbarch *,
121 struct program_space *, CORE_ADDR,
5af949e3 122 struct obj_section *, int);
c906108c 123
6c95b8df
PA
124static int breakpoint_address_match (struct address_space *aspace1,
125 CORE_ADDR addr1,
126 struct address_space *aspace2,
127 CORE_ADDR addr2);
128
85d721b8
PA
129static int watchpoint_locations_match (struct bp_location *loc1,
130 struct bp_location *loc2);
131
a14ed312 132static void breakpoints_info (char *, int);
c906108c 133
d77f58be
SS
134static void watchpoints_info (char *, int);
135
e5a67952
MS
136static int breakpoint_1 (char *, int,
137 int (*) (const struct breakpoint *));
c906108c 138
4efb68b1 139static int breakpoint_cond_eval (void *);
c906108c 140
4efb68b1 141static void cleanup_executing_breakpoints (void *);
c906108c 142
a14ed312 143static void commands_command (char *, int);
c906108c 144
a14ed312 145static void condition_command (char *, int);
c906108c 146
c5aa993b
JM
147typedef enum
148 {
149 mark_inserted,
150 mark_uninserted
151 }
152insertion_state_t;
c906108c 153
0bde7532 154static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 155static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 156
a14ed312 157static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
158
159static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 160
4efb68b1 161static int watchpoint_check (void *);
c906108c 162
a14ed312 163static void maintenance_info_breakpoints (char *, int);
c906108c 164
a14ed312 165static int hw_breakpoint_used_count (void);
c906108c 166
a14ed312 167static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 168
a14ed312 169static void hbreak_command (char *, int);
c906108c 170
a14ed312 171static void thbreak_command (char *, int);
c906108c 172
a14ed312 173static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 174
a14ed312 175static void stop_command (char *arg, int from_tty);
7a292a7a 176
a14ed312 177static void stopin_command (char *arg, int from_tty);
7a292a7a 178
a14ed312 179static void stopat_command (char *arg, int from_tty);
7a292a7a 180
a14ed312 181static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 182
d85310f7
MS
183static void catch_exception_command_1 (enum exception_event_kind ex_event,
184 char *arg, int tempflag, int from_tty);
7a292a7a 185
a14ed312 186static void tcatch_command (char *arg, int from_tty);
7a292a7a 187
d03285ec
UW
188static void detach_single_step_breakpoints (void);
189
6c95b8df
PA
190static int single_step_breakpoint_inserted_here_p (struct address_space *,
191 CORE_ADDR pc);
1aafd4da 192
fe3f5fa8 193static void free_bp_location (struct bp_location *loc);
f431efe5
PA
194static void incref_bp_location (struct bp_location *loc);
195static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 196
39d61571 197static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 198
b60e7edf 199static void update_global_location_list (int);
a5606eee 200
b60e7edf 201static void update_global_location_list_nothrow (int);
74960c60 202
d77f58be 203static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 204
d77f58be 205static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 206
74960c60 207static void insert_breakpoint_locations (void);
a5606eee 208
a96d9b2e
SDJ
209static int syscall_catchpoint_p (struct breakpoint *b);
210
1042e4c0
SS
211static void tracepoints_info (char *, int);
212
213static void delete_trace_command (char *, int);
214
215static void enable_trace_command (char *, int);
216
217static void disable_trace_command (char *, int);
218
219static void trace_pass_command (char *, int);
220
0fb4aa4b
PA
221/* Assuming we're creating a static tracepoint, does S look like a
222 static tracepoint marker spec ("-m MARKER_ID")? */
223#define is_marker_spec(s) \
f5a8e22b 224 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 225
5cea2a26
PA
226/* A reference-counted struct command_line. This lets multiple
227 breakpoints share a single command list. */
228struct counted_command_line
229{
230 /* The reference count. */
231 int refc;
232
233 /* The command list. */
234 struct command_line *commands;
235};
236
237struct command_line *
238breakpoint_commands (struct breakpoint *b)
239{
240 return b->commands ? b->commands->commands : NULL;
241}
3daf8fe5 242
f3b1572e
PA
243/* Flag indicating that a command has proceeded the inferior past the
244 current breakpoint. */
245
246static int breakpoint_proceeded;
247
2cec12e5
AR
248static const char *
249bpdisp_text (enum bpdisp disp)
250{
4a64f543
MS
251 /* NOTE: the following values are a part of MI protocol and
252 represent values of 'disp' field returned when inferior stops at
253 a breakpoint. */
bc043ef3 254 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 255
2cec12e5
AR
256 return bpdisps[(int) disp];
257}
c906108c 258
4a64f543 259/* Prototypes for exported functions. */
c906108c 260/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 261 if such is available. */
c906108c
SS
262static int can_use_hw_watchpoints;
263
920d2a44
AC
264static void
265show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
266 struct cmd_list_element *c,
267 const char *value)
268{
3e43a32a
MS
269 fprintf_filtered (file,
270 _("Debugger's willingness to use "
271 "watchpoint hardware is %s.\n"),
920d2a44
AC
272 value);
273}
274
fa8d40ab
JJ
275/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
276 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 277 for unrecognized breakpoint locations.
fa8d40ab
JJ
278 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
279static enum auto_boolean pending_break_support;
920d2a44
AC
280static void
281show_pending_break_support (struct ui_file *file, int from_tty,
282 struct cmd_list_element *c,
283 const char *value)
284{
3e43a32a
MS
285 fprintf_filtered (file,
286 _("Debugger's behavior regarding "
287 "pending breakpoints is %s.\n"),
920d2a44
AC
288 value);
289}
fa8d40ab 290
765dc015 291/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 292 set with "break" but falling in read-only memory.
765dc015
VP
293 If 0, gdb will warn about such breakpoints, but won't automatically
294 use hardware breakpoints. */
295static int automatic_hardware_breakpoints;
296static void
297show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
298 struct cmd_list_element *c,
299 const char *value)
300{
3e43a32a
MS
301 fprintf_filtered (file,
302 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
303 value);
304}
305
33e5cbd6
PA
306/* If on, gdb will keep breakpoints inserted even as inferior is
307 stopped, and immediately insert any new breakpoints. If off, gdb
308 will insert breakpoints into inferior only when resuming it, and
309 will remove breakpoints upon stop. If auto, GDB will behave as ON
310 if in non-stop mode, and as OFF if all-stop mode.*/
311
312static const char always_inserted_auto[] = "auto";
313static const char always_inserted_on[] = "on";
314static const char always_inserted_off[] = "off";
315static const char *always_inserted_enums[] = {
316 always_inserted_auto,
317 always_inserted_off,
318 always_inserted_on,
319 NULL
320};
321static const char *always_inserted_mode = always_inserted_auto;
322static void
74960c60 323show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 324 struct cmd_list_element *c, const char *value)
74960c60 325{
33e5cbd6 326 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
327 fprintf_filtered (file,
328 _("Always inserted breakpoint "
329 "mode is %s (currently %s).\n"),
33e5cbd6
PA
330 value,
331 breakpoints_always_inserted_mode () ? "on" : "off");
332 else
3e43a32a
MS
333 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
334 value);
74960c60
VP
335}
336
33e5cbd6
PA
337int
338breakpoints_always_inserted_mode (void)
339{
340 return (always_inserted_mode == always_inserted_on
341 || (always_inserted_mode == always_inserted_auto && non_stop));
342}
765dc015 343
a14ed312 344void _initialize_breakpoint (void);
c906108c 345
c906108c
SS
346/* Are we executing breakpoint commands? */
347static int executing_breakpoint_commands;
348
c02f5703
MS
349/* Are overlay event breakpoints enabled? */
350static int overlay_events_enabled;
351
e09342b5
TJB
352/* See description in breakpoint.h. */
353int target_exact_watchpoints = 0;
354
c906108c 355/* Walk the following statement or block through all breakpoints.
4a64f543
MS
356 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
357 current breakpoint. */
c906108c 358
5c44784c 359#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 360
5c44784c
JM
361#define ALL_BREAKPOINTS_SAFE(B,TMP) \
362 for (B = breakpoint_chain; \
363 B ? (TMP=B->next, 1): 0; \
364 B = TMP)
c906108c 365
4a64f543
MS
366/* Similar iterator for the low-level breakpoints. SAFE variant is
367 not provided so update_global_location_list must not be called
368 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 369
876fa593
JK
370#define ALL_BP_LOCATIONS(B,BP_TMP) \
371 for (BP_TMP = bp_location; \
372 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
373 BP_TMP++)
7cc221ef 374
1042e4c0
SS
375/* Iterator for tracepoints only. */
376
377#define ALL_TRACEPOINTS(B) \
378 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 379 if (is_tracepoint (B))
1042e4c0 380
7cc221ef 381/* Chains of all breakpoints defined. */
c906108c
SS
382
383struct breakpoint *breakpoint_chain;
384
876fa593
JK
385/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
386
387static struct bp_location **bp_location;
388
389/* Number of elements of BP_LOCATION. */
390
391static unsigned bp_location_count;
392
4a64f543
MS
393/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
394 ADDRESS for the current elements of BP_LOCATION which get a valid
395 result from bp_location_has_shadow. You can use it for roughly
396 limiting the subrange of BP_LOCATION to scan for shadow bytes for
397 an address you need to read. */
876fa593
JK
398
399static CORE_ADDR bp_location_placed_address_before_address_max;
400
4a64f543
MS
401/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
402 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
403 BP_LOCATION which get a valid result from bp_location_has_shadow.
404 You can use it for roughly limiting the subrange of BP_LOCATION to
405 scan for shadow bytes for an address you need to read. */
876fa593
JK
406
407static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 408
4a64f543
MS
409/* The locations that no longer correspond to any breakpoint, unlinked
410 from bp_location array, but for which a hit may still be reported
411 by a target. */
20874c92
VP
412VEC(bp_location_p) *moribund_locations = NULL;
413
c906108c
SS
414/* Number of last breakpoint made. */
415
95a42b64
TT
416static int breakpoint_count;
417
86b17b60
PA
418/* The value of `breakpoint_count' before the last command that
419 created breakpoints. If the last (break-like) command created more
420 than one breakpoint, then the difference between BREAKPOINT_COUNT
421 and PREV_BREAKPOINT_COUNT is more than one. */
422static int prev_breakpoint_count;
c906108c 423
1042e4c0
SS
424/* Number of last tracepoint made. */
425
95a42b64 426static int tracepoint_count;
1042e4c0 427
6149aea9
PA
428static struct cmd_list_element *breakpoint_set_cmdlist;
429static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 430struct cmd_list_element *save_cmdlist;
6149aea9 431
468d015d
JJ
432/* Return whether a breakpoint is an active enabled breakpoint. */
433static int
434breakpoint_enabled (struct breakpoint *b)
435{
0d381245 436 return (b->enable_state == bp_enabled);
468d015d
JJ
437}
438
c906108c
SS
439/* Set breakpoint count to NUM. */
440
95a42b64 441static void
fba45db2 442set_breakpoint_count (int num)
c906108c 443{
86b17b60 444 prev_breakpoint_count = breakpoint_count;
c906108c 445 breakpoint_count = num;
4fa62494 446 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
447}
448
86b17b60
PA
449/* Used by `start_rbreak_breakpoints' below, to record the current
450 breakpoint count before "rbreak" creates any breakpoint. */
451static int rbreak_start_breakpoint_count;
452
95a42b64
TT
453/* Called at the start an "rbreak" command to record the first
454 breakpoint made. */
86b17b60 455
95a42b64
TT
456void
457start_rbreak_breakpoints (void)
458{
86b17b60 459 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
460}
461
462/* Called at the end of an "rbreak" command to record the last
463 breakpoint made. */
86b17b60 464
95a42b64
TT
465void
466end_rbreak_breakpoints (void)
467{
86b17b60 468 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
469}
470
4a64f543 471/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
472
473void
fba45db2 474clear_breakpoint_hit_counts (void)
c906108c
SS
475{
476 struct breakpoint *b;
477
478 ALL_BREAKPOINTS (b)
479 b->hit_count = 0;
480}
481
9add0f1b
TT
482/* Allocate a new counted_command_line with reference count of 1.
483 The new structure owns COMMANDS. */
484
485static struct counted_command_line *
486alloc_counted_command_line (struct command_line *commands)
487{
488 struct counted_command_line *result
489 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 490
9add0f1b
TT
491 result->refc = 1;
492 result->commands = commands;
493 return result;
494}
495
496/* Increment reference count. This does nothing if CMD is NULL. */
497
498static void
499incref_counted_command_line (struct counted_command_line *cmd)
500{
501 if (cmd)
502 ++cmd->refc;
503}
504
505/* Decrement reference count. If the reference count reaches 0,
506 destroy the counted_command_line. Sets *CMDP to NULL. This does
507 nothing if *CMDP is NULL. */
508
509static void
510decref_counted_command_line (struct counted_command_line **cmdp)
511{
512 if (*cmdp)
513 {
514 if (--(*cmdp)->refc == 0)
515 {
516 free_command_lines (&(*cmdp)->commands);
517 xfree (*cmdp);
518 }
519 *cmdp = NULL;
520 }
521}
522
523/* A cleanup function that calls decref_counted_command_line. */
524
525static void
526do_cleanup_counted_command_line (void *arg)
527{
528 decref_counted_command_line (arg);
529}
530
531/* Create a cleanup that calls decref_counted_command_line on the
532 argument. */
533
534static struct cleanup *
535make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
536{
537 return make_cleanup (do_cleanup_counted_command_line, cmdp);
538}
539
c906108c
SS
540/* Default address, symtab and line to put a breakpoint at
541 for "break" command with no arg.
4a64f543 542 If default_breakpoint_valid is zero, the other three are
c906108c
SS
543 not valid, and "break" with no arg is an error.
544
545 This set by print_stack_frame, which calls set_default_breakpoint. */
546
547int default_breakpoint_valid;
548CORE_ADDR default_breakpoint_address;
549struct symtab *default_breakpoint_symtab;
550int default_breakpoint_line;
6c95b8df
PA
551struct program_space *default_breakpoint_pspace;
552
c906108c 553\f
48cb2d85
VP
554/* Return the breakpoint with the specified number, or NULL
555 if the number does not refer to an existing breakpoint. */
556
557struct breakpoint *
558get_breakpoint (int num)
559{
560 struct breakpoint *b;
561
562 ALL_BREAKPOINTS (b)
563 if (b->number == num)
564 return b;
565
566 return NULL;
567}
5c44784c 568
c906108c 569\f
adc36818
PM
570
571void
572set_breakpoint_condition (struct breakpoint *b, char *exp,
573 int from_tty)
574{
575 struct bp_location *loc = b->loc;
576
577 for (; loc; loc = loc->next)
578 {
579 xfree (loc->cond);
580 loc->cond = NULL;
581 }
582 xfree (b->cond_string);
583 b->cond_string = NULL;
584 xfree (b->cond_exp);
585 b->cond_exp = NULL;
586
587 if (*exp == 0)
588 {
589 if (from_tty)
590 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
591 }
592 else
593 {
594 char *arg = exp;
cc59ec59 595
adc36818
PM
596 /* I don't know if it matters whether this is the string the user
597 typed in or the decompiled expression. */
598 b->cond_string = xstrdup (arg);
599 b->condition_not_parsed = 0;
600
601 if (is_watchpoint (b))
602 {
603 innermost_block = NULL;
604 arg = exp;
605 b->cond_exp = parse_exp_1 (&arg, 0, 0);
606 if (*arg)
607 error (_("Junk at end of expression"));
608 b->cond_exp_valid_block = innermost_block;
609 }
610 else
611 {
612 for (loc = b->loc; loc; loc = loc->next)
613 {
614 arg = exp;
615 loc->cond =
616 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
617 if (*arg)
618 error (_("Junk at end of expression"));
619 }
620 }
621 }
622 breakpoints_changed ();
623 observer_notify_breakpoint_modified (b->number);
624}
625
c906108c
SS
626/* condition N EXP -- set break condition of breakpoint N to EXP. */
627
628static void
fba45db2 629condition_command (char *arg, int from_tty)
c906108c 630{
52f0bd74 631 struct breakpoint *b;
c906108c 632 char *p;
52f0bd74 633 int bnum;
c906108c
SS
634
635 if (arg == 0)
e2e0b3e5 636 error_no_arg (_("breakpoint number"));
c906108c
SS
637
638 p = arg;
639 bnum = get_number (&p);
5c44784c 640 if (bnum == 0)
8a3fe4f8 641 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
642
643 ALL_BREAKPOINTS (b)
644 if (b->number == bnum)
2f069f6f 645 {
2566ad2d 646 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
647 return;
648 }
c906108c 649
8a3fe4f8 650 error (_("No breakpoint number %d."), bnum);
c906108c
SS
651}
652
a7bdde9e
VP
653/* Check that COMMAND do not contain commands that are suitable
654 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
655 Throw if any such commands is found. */
656
a7bdde9e
VP
657static void
658check_no_tracepoint_commands (struct command_line *commands)
659{
660 struct command_line *c;
cc59ec59 661
a7bdde9e
VP
662 for (c = commands; c; c = c->next)
663 {
664 int i;
665
666 if (c->control_type == while_stepping_control)
3e43a32a
MS
667 error (_("The 'while-stepping' command can "
668 "only be used for tracepoints"));
a7bdde9e
VP
669
670 for (i = 0; i < c->body_count; ++i)
671 check_no_tracepoint_commands ((c->body_list)[i]);
672
673 /* Not that command parsing removes leading whitespace and comment
4a64f543 674 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
675 command directly. */
676 if (strstr (c->line, "collect ") == c->line)
677 error (_("The 'collect' command can only be used for tracepoints"));
678
51661e93
VP
679 if (strstr (c->line, "teval ") == c->line)
680 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
681 }
682}
683
d77f58be
SS
684/* Encapsulate tests for different types of tracepoints. */
685
a7bdde9e 686int
d77f58be 687is_tracepoint (const struct breakpoint *b)
a7bdde9e 688{
0fb4aa4b
PA
689 return (b->type == bp_tracepoint
690 || b->type == bp_fast_tracepoint
691 || b->type == bp_static_tracepoint);
a7bdde9e 692}
d77f58be 693
95a42b64
TT
694/* A helper function that validsates that COMMANDS are valid for a
695 breakpoint. This function will throw an exception if a problem is
696 found. */
48cb2d85 697
95a42b64
TT
698static void
699validate_commands_for_breakpoint (struct breakpoint *b,
700 struct command_line *commands)
48cb2d85 701{
d77f58be 702 if (is_tracepoint (b))
a7bdde9e 703 {
4a64f543
MS
704 /* We need to verify that each top-level element of commands is
705 valid for tracepoints, that there's at most one
706 while-stepping element, and that while-stepping's body has
707 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
708 struct command_line *c;
709 struct command_line *while_stepping = 0;
710 for (c = commands; c; c = c->next)
711 {
a7bdde9e
VP
712 if (c->control_type == while_stepping_control)
713 {
714 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
715 error (_("The 'while-stepping' command "
716 "cannot be used for fast tracepoint"));
0fb4aa4b 717 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
718 error (_("The 'while-stepping' command "
719 "cannot be used for static tracepoint"));
a7bdde9e
VP
720
721 if (while_stepping)
3e43a32a
MS
722 error (_("The 'while-stepping' command "
723 "can be used only once"));
a7bdde9e
VP
724 else
725 while_stepping = c;
726 }
727 }
728 if (while_stepping)
729 {
730 struct command_line *c2;
731
732 gdb_assert (while_stepping->body_count == 1);
733 c2 = while_stepping->body_list[0];
734 for (; c2; c2 = c2->next)
735 {
a7bdde9e
VP
736 if (c2->control_type == while_stepping_control)
737 error (_("The 'while-stepping' command cannot be nested"));
738 }
739 }
740 }
741 else
742 {
743 check_no_tracepoint_commands (commands);
744 }
95a42b64
TT
745}
746
0fb4aa4b
PA
747/* Return a vector of all the static tracepoints set at ADDR. The
748 caller is responsible for releasing the vector. */
749
750VEC(breakpoint_p) *
751static_tracepoints_here (CORE_ADDR addr)
752{
753 struct breakpoint *b;
754 VEC(breakpoint_p) *found = 0;
755 struct bp_location *loc;
756
757 ALL_BREAKPOINTS (b)
758 if (b->type == bp_static_tracepoint)
759 {
760 for (loc = b->loc; loc; loc = loc->next)
761 if (loc->address == addr)
762 VEC_safe_push(breakpoint_p, found, b);
763 }
764
765 return found;
766}
767
95a42b64 768/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 769 validate that only allowed commands are included. */
95a42b64
TT
770
771void
4a64f543
MS
772breakpoint_set_commands (struct breakpoint *b,
773 struct command_line *commands)
95a42b64
TT
774{
775 validate_commands_for_breakpoint (b, commands);
a7bdde9e 776
9add0f1b
TT
777 decref_counted_command_line (&b->commands);
778 b->commands = alloc_counted_command_line (commands);
48cb2d85
VP
779 breakpoints_changed ();
780 observer_notify_breakpoint_modified (b->number);
781}
782
45a43567
TT
783/* Set the internal `silent' flag on the breakpoint. Note that this
784 is not the same as the "silent" that may appear in the breakpoint's
785 commands. */
786
787void
788breakpoint_set_silent (struct breakpoint *b, int silent)
789{
790 int old_silent = b->silent;
791
792 b->silent = silent;
793 if (old_silent != silent)
794 observer_notify_breakpoint_modified (b->number);
795}
796
797/* Set the thread for this breakpoint. If THREAD is -1, make the
798 breakpoint work for any thread. */
799
800void
801breakpoint_set_thread (struct breakpoint *b, int thread)
802{
803 int old_thread = b->thread;
804
805 b->thread = thread;
806 if (old_thread != thread)
807 observer_notify_breakpoint_modified (b->number);
808}
809
810/* Set the task for this breakpoint. If TASK is 0, make the
811 breakpoint work for any task. */
812
813void
814breakpoint_set_task (struct breakpoint *b, int task)
815{
816 int old_task = b->task;
817
818 b->task = task;
819 if (old_task != task)
820 observer_notify_breakpoint_modified (b->number);
821}
822
95a42b64
TT
823void
824check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
825{
826 struct breakpoint *b = closure;
cc59ec59 827
a7bdde9e
VP
828 validate_actionline (&line, b);
829}
830
95a42b64
TT
831/* A structure used to pass information through
832 map_breakpoint_numbers. */
833
834struct commands_info
835{
836 /* True if the command was typed at a tty. */
837 int from_tty;
86b17b60
PA
838
839 /* The breakpoint range spec. */
840 char *arg;
841
95a42b64
TT
842 /* Non-NULL if the body of the commands are being read from this
843 already-parsed command. */
844 struct command_line *control;
86b17b60 845
95a42b64
TT
846 /* The command lines read from the user, or NULL if they have not
847 yet been read. */
848 struct counted_command_line *cmd;
849};
850
851/* A callback for map_breakpoint_numbers that sets the commands for
852 commands_command. */
853
c906108c 854static void
95a42b64 855do_map_commands_command (struct breakpoint *b, void *data)
c906108c 856{
95a42b64 857 struct commands_info *info = data;
c906108c 858
95a42b64
TT
859 if (info->cmd == NULL)
860 {
861 struct command_line *l;
5c44784c 862
95a42b64
TT
863 if (info->control != NULL)
864 l = copy_command_lines (info->control->body_list[0]);
865 else
86b17b60
PA
866 {
867 struct cleanup *old_chain;
868 char *str;
c5aa993b 869
3e43a32a
MS
870 str = xstrprintf (_("Type commands for breakpoint(s) "
871 "%s, one per line."),
86b17b60
PA
872 info->arg);
873
874 old_chain = make_cleanup (xfree, str);
875
876 l = read_command_lines (str,
877 info->from_tty, 1,
d77f58be 878 (is_tracepoint (b)
86b17b60
PA
879 ? check_tracepoint_command : 0),
880 b);
881
882 do_cleanups (old_chain);
883 }
a7bdde9e 884
95a42b64
TT
885 info->cmd = alloc_counted_command_line (l);
886 }
887
888 /* If a breakpoint was on the list more than once, we don't need to
889 do anything. */
890 if (b->commands != info->cmd)
891 {
892 validate_commands_for_breakpoint (b, info->cmd->commands);
893 incref_counted_command_line (info->cmd);
894 decref_counted_command_line (&b->commands);
895 b->commands = info->cmd;
896 breakpoints_changed ();
897 observer_notify_breakpoint_modified (b->number);
c5aa993b 898 }
95a42b64
TT
899}
900
901static void
4a64f543
MS
902commands_command_1 (char *arg, int from_tty,
903 struct command_line *control)
95a42b64
TT
904{
905 struct cleanup *cleanups;
906 struct commands_info info;
907
908 info.from_tty = from_tty;
909 info.control = control;
910 info.cmd = NULL;
911 /* If we read command lines from the user, then `info' will hold an
912 extra reference to the commands that we must clean up. */
913 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
914
915 if (arg == NULL || !*arg)
916 {
86b17b60 917 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
918 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
919 breakpoint_count);
95a42b64
TT
920 else if (breakpoint_count > 0)
921 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
922 else
923 {
924 /* So that we don't try to free the incoming non-NULL
925 argument in the cleanup below. Mapping breakpoint
926 numbers will fail in this case. */
927 arg = NULL;
928 }
95a42b64 929 }
9766ced4
SS
930 else
931 /* The command loop has some static state, so we need to preserve
932 our argument. */
933 arg = xstrdup (arg);
86b17b60
PA
934
935 if (arg != NULL)
936 make_cleanup (xfree, arg);
937
938 info.arg = arg;
95a42b64
TT
939
940 map_breakpoint_numbers (arg, do_map_commands_command, &info);
941
942 if (info.cmd == NULL)
943 error (_("No breakpoints specified."));
944
945 do_cleanups (cleanups);
946}
947
948static void
949commands_command (char *arg, int from_tty)
950{
951 commands_command_1 (arg, from_tty, NULL);
c906108c 952}
40c03ae8
EZ
953
954/* Like commands_command, but instead of reading the commands from
955 input stream, takes them from an already parsed command structure.
956
957 This is used by cli-script.c to DTRT with breakpoint commands
958 that are part of if and while bodies. */
959enum command_control_type
960commands_from_control_command (char *arg, struct command_line *cmd)
961{
95a42b64
TT
962 commands_command_1 (arg, 0, cmd);
963 return simple_control;
40c03ae8 964}
876fa593
JK
965
966/* Return non-zero if BL->TARGET_INFO contains valid information. */
967
968static int
969bp_location_has_shadow (struct bp_location *bl)
970{
971 if (bl->loc_type != bp_loc_software_breakpoint)
972 return 0;
973 if (!bl->inserted)
974 return 0;
975 if (bl->target_info.shadow_len == 0)
976 /* bp isn't valid, or doesn't shadow memory. */
977 return 0;
978 return 1;
979}
980
8defab1a 981/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
982 by replacing any memory breakpoints with their shadowed contents.
983
984 The range of shadowed area by each bp_location is:
35df4500
TJB
985 bl->address - bp_location_placed_address_before_address_max
986 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
987 The range we were requested to resolve shadows for is:
988 memaddr ... memaddr + len
989 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
990 memaddr + len <= (bl->address
991 - bp_location_placed_address_before_address_max)
876fa593 992 and:
35df4500 993 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 994
8defab1a
DJ
995void
996breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 997{
4a64f543
MS
998 /* Left boundary, right boundary and median element of our binary
999 search. */
876fa593
JK
1000 unsigned bc_l, bc_r, bc;
1001
4a64f543
MS
1002 /* Find BC_L which is a leftmost element which may affect BUF
1003 content. It is safe to report lower value but a failure to
1004 report higher one. */
876fa593
JK
1005
1006 bc_l = 0;
1007 bc_r = bp_location_count;
1008 while (bc_l + 1 < bc_r)
1009 {
35df4500 1010 struct bp_location *bl;
876fa593
JK
1011
1012 bc = (bc_l + bc_r) / 2;
35df4500 1013 bl = bp_location[bc];
876fa593 1014
4a64f543
MS
1015 /* Check first BL->ADDRESS will not overflow due to the added
1016 constant. Then advance the left boundary only if we are sure
1017 the BC element can in no way affect the BUF content (MEMADDR
1018 to MEMADDR + LEN range).
876fa593 1019
4a64f543
MS
1020 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1021 offset so that we cannot miss a breakpoint with its shadow
1022 range tail still reaching MEMADDR. */
c5aa993b 1023
35df4500
TJB
1024 if ((bl->address + bp_location_shadow_len_after_address_max
1025 >= bl->address)
1026 && (bl->address + bp_location_shadow_len_after_address_max
1027 <= memaddr))
876fa593
JK
1028 bc_l = bc;
1029 else
1030 bc_r = bc;
1031 }
1032
128070bb
PA
1033 /* Due to the binary search above, we need to make sure we pick the
1034 first location that's at BC_L's address. E.g., if there are
1035 multiple locations at the same address, BC_L may end up pointing
1036 at a duplicate location, and miss the "master"/"inserted"
1037 location. Say, given locations L1, L2 and L3 at addresses A and
1038 B:
1039
1040 L1@A, L2@A, L3@B, ...
1041
1042 BC_L could end up pointing at location L2, while the "master"
1043 location could be L1. Since the `loc->inserted' flag is only set
1044 on "master" locations, we'd forget to restore the shadow of L1
1045 and L2. */
1046 while (bc_l > 0
1047 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1048 bc_l--;
1049
876fa593
JK
1050 /* Now do full processing of the found relevant range of elements. */
1051
1052 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1053 {
35df4500 1054 struct bp_location *bl = bp_location[bc];
876fa593
JK
1055 CORE_ADDR bp_addr = 0;
1056 int bp_size = 0;
1057 int bptoffset = 0;
1058
35df4500
TJB
1059 /* bp_location array has BL->OWNER always non-NULL. */
1060 if (bl->owner->type == bp_none)
8a3fe4f8 1061 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1062 bl->owner->number);
ffce0d52 1063
876fa593
JK
1064 /* Performance optimization: any futher element can no longer affect BUF
1065 content. */
1066
35df4500
TJB
1067 if (bl->address >= bp_location_placed_address_before_address_max
1068 && memaddr + len <= (bl->address
1069 - bp_location_placed_address_before_address_max))
876fa593
JK
1070 break;
1071
35df4500 1072 if (!bp_location_has_shadow (bl))
c5aa993b 1073 continue;
35df4500 1074 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1075 current_program_space->aspace, 0))
1076 continue;
1077
c5aa993b
JM
1078 /* Addresses and length of the part of the breakpoint that
1079 we need to copy. */
35df4500
TJB
1080 bp_addr = bl->target_info.placed_address;
1081 bp_size = bl->target_info.shadow_len;
8defab1a 1082
c5aa993b
JM
1083 if (bp_addr + bp_size <= memaddr)
1084 /* The breakpoint is entirely before the chunk of memory we
1085 are reading. */
1086 continue;
8defab1a 1087
c5aa993b
JM
1088 if (bp_addr >= memaddr + len)
1089 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1090 reading. */
c5aa993b 1091 continue;
c5aa993b 1092
8defab1a
DJ
1093 /* Offset within shadow_contents. */
1094 if (bp_addr < memaddr)
1095 {
1096 /* Only copy the second part of the breakpoint. */
1097 bp_size -= memaddr - bp_addr;
1098 bptoffset = memaddr - bp_addr;
1099 bp_addr = memaddr;
1100 }
c5aa993b 1101
8defab1a
DJ
1102 if (bp_addr + bp_size > memaddr + len)
1103 {
1104 /* Only copy the first part of the breakpoint. */
1105 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1106 }
c5aa993b 1107
8defab1a 1108 memcpy (buf + bp_addr - memaddr,
35df4500 1109 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1110 }
c906108c 1111}
c906108c 1112\f
c5aa993b 1113
60e1c644
PA
1114/* Return true if BPT is of any hardware watchpoint kind. */
1115
a5606eee 1116static int
d77f58be 1117is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1118{
1119 return (bpt->type == bp_hardware_watchpoint
1120 || bpt->type == bp_read_watchpoint
1121 || bpt->type == bp_access_watchpoint);
1122}
7270d8f2 1123
60e1c644
PA
1124/* Return true if BPT is of any watchpoint kind, hardware or
1125 software. */
1126
1127static int
d77f58be 1128is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1129{
1130 return (is_hardware_watchpoint (bpt)
1131 || bpt->type == bp_watchpoint);
1132}
1133
f6bc2008
PA
1134/* Assuming that B is a watchpoint: returns true if the current thread
1135 and its running state are safe to evaluate or update watchpoint B.
1136 Watchpoints on local expressions need to be evaluated in the
1137 context of the thread that was current when the watchpoint was
1138 created, and, that thread needs to be stopped to be able to select
1139 the correct frame context. Watchpoints on global expressions can
1140 be evaluated on any thread, and in any state. It is presently left
1141 to the target allowing memory accesses when threads are
1142 running. */
1143
1144static int
1145watchpoint_in_thread_scope (struct breakpoint *b)
1146{
1147 return (ptid_equal (b->watchpoint_thread, null_ptid)
1148 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1149 && !is_executing (inferior_ptid)));
1150}
1151
567e1b4e
JB
1152/* Assuming that B is a watchpoint:
1153 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1154 - Evaluate expression and store the result in B->val
567e1b4e
JB
1155 - Evaluate the condition if there is one, and store the result
1156 in b->loc->cond.
a5606eee
VP
1157 - Update the list of values that must be watched in B->loc.
1158
4a64f543
MS
1159 If the watchpoint disposition is disp_del_at_next_stop, then do
1160 nothing. If this is local watchpoint that is out of scope, delete
1161 it.
1162
1163 Even with `set breakpoint always-inserted on' the watchpoints are
1164 removed + inserted on each stop here. Normal breakpoints must
1165 never be removed because they might be missed by a running thread
1166 when debugging in non-stop mode. On the other hand, hardware
1167 watchpoints (is_hardware_watchpoint; processed here) are specific
1168 to each LWP since they are stored in each LWP's hardware debug
1169 registers. Therefore, such LWP must be stopped first in order to
1170 be able to modify its hardware watchpoints.
1171
1172 Hardware watchpoints must be reset exactly once after being
1173 presented to the user. It cannot be done sooner, because it would
1174 reset the data used to present the watchpoint hit to the user. And
1175 it must not be done later because it could display the same single
1176 watchpoint hit during multiple GDB stops. Note that the latter is
1177 relevant only to the hardware watchpoint types bp_read_watchpoint
1178 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1179 not user-visible - its hit is suppressed if the memory content has
1180 not changed.
1181
1182 The following constraints influence the location where we can reset
1183 hardware watchpoints:
1184
1185 * target_stopped_by_watchpoint and target_stopped_data_address are
1186 called several times when GDB stops.
1187
1188 [linux]
1189 * Multiple hardware watchpoints can be hit at the same time,
1190 causing GDB to stop. GDB only presents one hardware watchpoint
1191 hit at a time as the reason for stopping, and all the other hits
1192 are presented later, one after the other, each time the user
1193 requests the execution to be resumed. Execution is not resumed
1194 for the threads still having pending hit event stored in
1195 LWP_INFO->STATUS. While the watchpoint is already removed from
1196 the inferior on the first stop the thread hit event is kept being
1197 reported from its cached value by linux_nat_stopped_data_address
1198 until the real thread resume happens after the watchpoint gets
1199 presented and thus its LWP_INFO->STATUS gets reset.
1200
1201 Therefore the hardware watchpoint hit can get safely reset on the
1202 watchpoint removal from inferior. */
a79d3c27 1203
b40ce68a 1204static void
a5606eee 1205update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1206{
a5606eee 1207 int within_current_scope;
a5606eee 1208 struct frame_id saved_frame_id;
66076460 1209 int frame_saved;
a5606eee 1210
f6bc2008
PA
1211 /* If this is a local watchpoint, we only want to check if the
1212 watchpoint frame is in scope if the current thread is the thread
1213 that was used to create the watchpoint. */
1214 if (!watchpoint_in_thread_scope (b))
1215 return;
1216
a5606eee
VP
1217 if (b->disposition == disp_del_at_next_stop)
1218 return;
1219
66076460 1220 frame_saved = 0;
a5606eee
VP
1221
1222 /* Determine if the watchpoint is within scope. */
1223 if (b->exp_valid_block == NULL)
1224 within_current_scope = 1;
1225 else
1226 {
b5db5dfc
UW
1227 struct frame_info *fi = get_current_frame ();
1228 struct gdbarch *frame_arch = get_frame_arch (fi);
1229 CORE_ADDR frame_pc = get_frame_pc (fi);
1230
1231 /* If we're in a function epilogue, unwinding may not work
1232 properly, so do not attempt to recreate locations at this
1233 point. See similar comments in watchpoint_check. */
1234 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1235 return;
66076460
DJ
1236
1237 /* Save the current frame's ID so we can restore it after
1238 evaluating the watchpoint expression on its own frame. */
1239 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1240 took a frame parameter, so that we didn't have to change the
1241 selected frame. */
1242 frame_saved = 1;
1243 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1244
a5606eee
VP
1245 fi = frame_find_by_id (b->watchpoint_frame);
1246 within_current_scope = (fi != NULL);
1247 if (within_current_scope)
1248 select_frame (fi);
1249 }
1250
b5db5dfc
UW
1251 /* We don't free locations. They are stored in the bp_location array
1252 and update_global_location_list will eventually delete them and
1253 remove breakpoints if needed. */
1254 b->loc = NULL;
1255
a5606eee
VP
1256 if (within_current_scope && reparse)
1257 {
1258 char *s;
d63d0675 1259
a5606eee
VP
1260 if (b->exp)
1261 {
1262 xfree (b->exp);
1263 b->exp = NULL;
1264 }
d63d0675 1265 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1266 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1267 /* If the meaning of expression itself changed, the old value is
1268 no longer relevant. We don't want to report a watchpoint hit
1269 to the user when the old value and the new value may actually
1270 be completely different objects. */
1271 value_free (b->val);
fa4727a6
DJ
1272 b->val = NULL;
1273 b->val_valid = 0;
60e1c644
PA
1274
1275 /* Note that unlike with breakpoints, the watchpoint's condition
1276 expression is stored in the breakpoint object, not in the
1277 locations (re)created below. */
1278 if (b->cond_string != NULL)
1279 {
1280 if (b->cond_exp != NULL)
1281 {
1282 xfree (b->cond_exp);
1283 b->cond_exp = NULL;
1284 }
1285
1286 s = b->cond_string;
1287 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1288 }
a5606eee 1289 }
a5606eee
VP
1290
1291 /* If we failed to parse the expression, for example because
1292 it refers to a global variable in a not-yet-loaded shared library,
1293 don't try to insert watchpoint. We don't automatically delete
1294 such watchpoint, though, since failure to parse expression
1295 is different from out-of-scope watchpoint. */
2d134ed3
PA
1296 if ( !target_has_execution)
1297 {
1298 /* Without execution, memory can't change. No use to try and
1299 set watchpoint locations. The watchpoint will be reset when
1300 the target gains execution, through breakpoint_re_set. */
1301 }
1302 else if (within_current_scope && b->exp)
a5606eee 1303 {
0cf6dd15 1304 int pc = 0;
fa4727a6 1305 struct value *val_chain, *v, *result, *next;
2d134ed3 1306 struct program_space *frame_pspace;
a5606eee 1307
0cf6dd15 1308 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1309
a5606eee
VP
1310 /* Avoid setting b->val if it's already set. The meaning of
1311 b->val is 'the last value' user saw, and we should update
1312 it only if we reported that last value to user. As it
1313 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1314 if (!b->val_valid)
1315 {
1316 b->val = v;
1317 b->val_valid = 1;
1318 }
a5606eee 1319
2d134ed3
PA
1320 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1321
a5606eee 1322 /* Look at each value on the value chain. */
9fa40276 1323 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1324 {
1325 /* If it's a memory location, and GDB actually needed
1326 its contents to evaluate the expression, then we
fa4727a6
DJ
1327 must watch it. If the first value returned is
1328 still lazy, that means an error occurred reading it;
1329 watch it anyway in case it becomes readable. */
a5606eee 1330 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1331 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1332 {
1333 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1334
a5606eee
VP
1335 /* We only watch structs and arrays if user asked
1336 for it explicitly, never if they just happen to
1337 appear in the middle of some value chain. */
fa4727a6 1338 if (v == result
a5606eee
VP
1339 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1340 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1341 {
1342 CORE_ADDR addr;
1343 int len, type;
1344 struct bp_location *loc, **tmp;
1345
42ae5230 1346 addr = value_address (v);
a5606eee
VP
1347 len = TYPE_LENGTH (value_type (v));
1348 type = hw_write;
1349 if (b->type == bp_read_watchpoint)
1350 type = hw_read;
1351 else if (b->type == bp_access_watchpoint)
1352 type = hw_access;
1353
39d61571 1354 loc = allocate_bp_location (b);
a5606eee
VP
1355 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1356 ;
1357 *tmp = loc;
a6d9a66e 1358 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1359
1360 loc->pspace = frame_pspace;
a5606eee
VP
1361 loc->address = addr;
1362 loc->length = len;
1363 loc->watchpoint_type = type;
1364 }
1365 }
9fa40276
TJB
1366 }
1367
1368 /* Change the type of breakpoint between hardware assisted or
1369 an ordinary watchpoint depending on the hardware support
1370 and free hardware slots. REPARSE is set when the inferior
1371 is started. */
1372 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1373 && reparse)
1374 {
e09342b5 1375 int reg_cnt;
9fa40276
TJB
1376 enum bp_loc_type loc_type;
1377 struct bp_location *bl;
a5606eee 1378
e09342b5
TJB
1379 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1380
1381 if (reg_cnt)
9fa40276
TJB
1382 {
1383 int i, target_resources_ok, other_type_used;
e09342b5 1384 enum enable_state orig_enable_state;
9fa40276
TJB
1385
1386 /* We need to determine how many resources are already
e09342b5
TJB
1387 used for all other hardware watchpoints plus this one
1388 to see if we still have enough resources to also fit
1389 this watchpoint in as well. To guarantee the
1390 hw_watchpoint_used_count call below counts this
1391 watchpoint, make sure that it is marked as a hardware
1392 watchpoint. */
1393 b->type = bp_hardware_watchpoint;
1394
1395 /* hw_watchpoint_used_count ignores disabled watchpoints,
1396 and b might be disabled if we're being called from
1397 do_enable_breakpoint. */
1398 orig_enable_state = b->enable_state;
1399 b->enable_state = bp_enabled;
1400
9fa40276
TJB
1401 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1402 &other_type_used);
9fa40276 1403
e09342b5
TJB
1404 b->enable_state = orig_enable_state;
1405
1406 target_resources_ok = target_can_use_hardware_watchpoint
1407 (bp_hardware_watchpoint, i, other_type_used);
1408 if (target_resources_ok <= 0)
1409 b->type = bp_watchpoint;
9fa40276
TJB
1410 }
1411 else
1412 b->type = bp_watchpoint;
1413
1414 loc_type = (b->type == bp_watchpoint? bp_loc_other
1415 : bp_loc_hardware_watchpoint);
1416 for (bl = b->loc; bl; bl = bl->next)
1417 bl->loc_type = loc_type;
1418 }
1419
1420 for (v = val_chain; v; v = next)
1421 {
a5606eee
VP
1422 next = value_next (v);
1423 if (v != b->val)
1424 value_free (v);
1425 }
1426
c7437ca6
PA
1427 /* If a software watchpoint is not watching any memory, then the
1428 above left it without any location set up. But,
1429 bpstat_stop_status requires a location to be able to report
1430 stops, so make sure there's at least a dummy one. */
1431 if (b->type == bp_watchpoint && b->loc == NULL)
1432 {
1433 b->loc = allocate_bp_location (b);
1434 b->loc->pspace = frame_pspace;
1435 b->loc->address = -1;
1436 b->loc->length = -1;
1437 b->loc->watchpoint_type = -1;
1438 }
a5606eee
VP
1439 }
1440 else if (!within_current_scope)
7270d8f2 1441 {
ac74f770
MS
1442 printf_filtered (_("\
1443Watchpoint %d deleted because the program has left the block\n\
1444in which its expression is valid.\n"),
a5606eee
VP
1445 b->number);
1446 if (b->related_breakpoint)
60e1c644
PA
1447 {
1448 b->related_breakpoint->disposition = disp_del_at_next_stop;
1449 b->related_breakpoint->related_breakpoint = NULL;
1450 b->related_breakpoint= NULL;
1451 }
a5606eee 1452 b->disposition = disp_del_at_next_stop;
7270d8f2 1453 }
a5606eee
VP
1454
1455 /* Restore the selected frame. */
66076460
DJ
1456 if (frame_saved)
1457 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1458}
1459
a5606eee 1460
74960c60
VP
1461/* Returns 1 iff breakpoint location should be
1462 inserted in the inferior. */
1463static int
35df4500 1464should_be_inserted (struct bp_location *bl)
74960c60 1465{
35df4500 1466 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1467 return 0;
1468
35df4500 1469 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1470 return 0;
1471
35df4500 1472 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1473 return 0;
1474
56710373
PA
1475 /* This is set for example, when we're attached to the parent of a
1476 vfork, and have detached from the child. The child is running
1477 free, and we expect it to do an exec or exit, at which point the
1478 OS makes the parent schedulable again (and the target reports
1479 that the vfork is done). Until the child is done with the shared
1480 memory region, do not insert breakpoints in the parent, otherwise
1481 the child could still trip on the parent's breakpoints. Since
1482 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1483 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1484 return 0;
1485
1042e4c0
SS
1486 /* Tracepoints are inserted by the target at a time of its choosing,
1487 not by us. */
35df4500 1488 if (is_tracepoint (bl->owner))
1042e4c0
SS
1489 return 0;
1490
74960c60
VP
1491 return 1;
1492}
1493
35df4500
TJB
1494/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1495 location. Any error messages are printed to TMP_ERROR_STREAM; and
1496 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1497
4a64f543
MS
1498 NOTE drow/2003-09-09: This routine could be broken down to an
1499 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1500static int
35df4500 1501insert_bp_location (struct bp_location *bl,
26bb91f3 1502 struct ui_file *tmp_error_stream,
fa3a767f 1503 int *disabled_breaks,
26bb91f3 1504 int *hw_breakpoint_error)
879bfdc2
DJ
1505{
1506 int val = 0;
1507
35df4500 1508 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1509 return 0;
1510
8181d85f 1511 /* Initialize the target-specific information. */
35df4500
TJB
1512 memset (&bl->target_info, 0, sizeof (bl->target_info));
1513 bl->target_info.placed_address = bl->address;
1514 bl->target_info.placed_address_space = bl->pspace->aspace;
8181d85f 1515
35df4500
TJB
1516 if (bl->loc_type == bp_loc_software_breakpoint
1517 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1518 {
35df4500 1519 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1520 {
1521 /* If the explicitly specified breakpoint type
1522 is not hardware breakpoint, check the memory map to see
1523 if the breakpoint address is in read only memory or not.
4a64f543 1524
765dc015
VP
1525 Two important cases are:
1526 - location type is not hardware breakpoint, memory
1527 is readonly. We change the type of the location to
1528 hardware breakpoint.
4a64f543
MS
1529 - location type is hardware breakpoint, memory is
1530 read-write. This means we've previously made the
1531 location hardware one, but then the memory map changed,
1532 so we undo.
765dc015 1533
4a64f543
MS
1534 When breakpoints are removed, remove_breakpoints will use
1535 location types we've just set here, the only possible
1536 problem is that memory map has changed during running
1537 program, but it's not going to work anyway with current
1538 gdb. */
765dc015 1539 struct mem_region *mr
35df4500 1540 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1541
1542 if (mr)
1543 {
1544 if (automatic_hardware_breakpoints)
1545 {
765dc015
VP
1546 enum bp_loc_type new_type;
1547
1548 if (mr->attrib.mode != MEM_RW)
1549 new_type = bp_loc_hardware_breakpoint;
1550 else
1551 new_type = bp_loc_software_breakpoint;
1552
35df4500 1553 if (new_type != bl->loc_type)
765dc015
VP
1554 {
1555 static int said = 0;
cc59ec59 1556
35df4500 1557 bl->loc_type = new_type;
765dc015
VP
1558 if (!said)
1559 {
3e43a32a
MS
1560 fprintf_filtered (gdb_stdout,
1561 _("Note: automatically using "
1562 "hardware breakpoints for "
1563 "read-only addresses.\n"));
765dc015
VP
1564 said = 1;
1565 }
1566 }
1567 }
35df4500 1568 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1569 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1570 warning (_("cannot set software breakpoint "
1571 "at readonly address %s"),
35df4500 1572 paddress (bl->gdbarch, bl->address));
765dc015
VP
1573 }
1574 }
1575
879bfdc2
DJ
1576 /* First check to see if we have to handle an overlay. */
1577 if (overlay_debugging == ovly_off
35df4500
TJB
1578 || bl->section == NULL
1579 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1580 {
1581 /* No overlay handling: just set the breakpoint. */
1582
35df4500
TJB
1583 if (bl->loc_type == bp_loc_hardware_breakpoint)
1584 val = target_insert_hw_breakpoint (bl->gdbarch,
1585 &bl->target_info);
879bfdc2 1586 else
35df4500
TJB
1587 val = target_insert_breakpoint (bl->gdbarch,
1588 &bl->target_info);
879bfdc2
DJ
1589 }
1590 else
1591 {
4a64f543 1592 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1593 Shall we set a breakpoint at the LMA? */
1594 if (!overlay_events_enabled)
1595 {
1596 /* Yes -- overlay event support is not active,
1597 so we must try to set a breakpoint at the LMA.
1598 This will not work for a hardware breakpoint. */
35df4500 1599 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1600 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1601 bl->owner->number);
879bfdc2
DJ
1602 else
1603 {
35df4500
TJB
1604 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1605 bl->section);
879bfdc2 1606 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1607 bl->overlay_target_info = bl->target_info;
1608 bl->overlay_target_info.placed_address = addr;
1609 val = target_insert_breakpoint (bl->gdbarch,
1610 &bl->overlay_target_info);
879bfdc2 1611 if (val != 0)
99361f52 1612 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1613 "Overlay breakpoint %d "
1614 "failed: in ROM?\n",
35df4500 1615 bl->owner->number);
879bfdc2
DJ
1616 }
1617 }
1618 /* Shall we set a breakpoint at the VMA? */
35df4500 1619 if (section_is_mapped (bl->section))
879bfdc2
DJ
1620 {
1621 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1622 if (bl->loc_type == bp_loc_hardware_breakpoint)
1623 val = target_insert_hw_breakpoint (bl->gdbarch,
1624 &bl->target_info);
879bfdc2 1625 else
35df4500
TJB
1626 val = target_insert_breakpoint (bl->gdbarch,
1627 &bl->target_info);
879bfdc2
DJ
1628 }
1629 else
1630 {
1631 /* No. This breakpoint will not be inserted.
1632 No error, but do not mark the bp as 'inserted'. */
1633 return 0;
1634 }
1635 }
1636
1637 if (val)
1638 {
1639 /* Can't set the breakpoint. */
35df4500 1640 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1641 {
4a64f543 1642 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1643 val = 0;
35df4500 1644 bl->shlib_disabled = 1;
879bfdc2
DJ
1645 if (!*disabled_breaks)
1646 {
1647 fprintf_unfiltered (tmp_error_stream,
1648 "Cannot insert breakpoint %d.\n",
35df4500 1649 bl->owner->number);
879bfdc2 1650 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1651 "Temporarily disabling shared "
1652 "library breakpoints:\n");
879bfdc2
DJ
1653 }
1654 *disabled_breaks = 1;
1655 fprintf_unfiltered (tmp_error_stream,
35df4500 1656 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1657 }
1658 else
879bfdc2 1659 {
35df4500 1660 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1661 {
1662 *hw_breakpoint_error = 1;
3e43a32a
MS
1663 fprintf_unfiltered (tmp_error_stream,
1664 "Cannot insert hardware "
1665 "breakpoint %d.\n",
35df4500 1666 bl->owner->number);
879bfdc2
DJ
1667 }
1668 else
1669 {
1670 fprintf_unfiltered (tmp_error_stream,
1671 "Cannot insert breakpoint %d.\n",
35df4500 1672 bl->owner->number);
879bfdc2
DJ
1673 fprintf_filtered (tmp_error_stream,
1674 "Error accessing memory address ");
35df4500 1675 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1676 tmp_error_stream);
879bfdc2
DJ
1677 fprintf_filtered (tmp_error_stream, ": %s.\n",
1678 safe_strerror (val));
1679 }
1680
1681 }
1682 }
1683 else
35df4500 1684 bl->inserted = 1;
879bfdc2
DJ
1685
1686 return val;
1687 }
1688
35df4500 1689 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1690 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1691 watchpoints. It's not clear that it's necessary... */
35df4500 1692 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1693 {
77b06cd7
TJB
1694 gdb_assert (bl->owner->ops != NULL
1695 && bl->owner->ops->insert_location != NULL);
1696
1697 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1698
1699 /* If trying to set a read-watchpoint, and it turns out it's not
1700 supported, try emulating one with an access watchpoint. */
35df4500 1701 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1702 {
1703 struct bp_location *loc, **loc_temp;
1704
1705 /* But don't try to insert it, if there's already another
1706 hw_access location that would be considered a duplicate
1707 of this one. */
1708 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1709 if (loc != bl
85d721b8 1710 && loc->watchpoint_type == hw_access
35df4500 1711 && watchpoint_locations_match (bl, loc))
85d721b8 1712 {
35df4500
TJB
1713 bl->duplicate = 1;
1714 bl->inserted = 1;
1715 bl->target_info = loc->target_info;
1716 bl->watchpoint_type = hw_access;
85d721b8
PA
1717 val = 0;
1718 break;
1719 }
1720
1721 if (val == 1)
1722 {
77b06cd7
TJB
1723 bl->watchpoint_type = hw_access;
1724 val = bl->owner->ops->insert_location (bl);
1725
1726 if (val)
1727 /* Back to the original value. */
1728 bl->watchpoint_type = hw_read;
85d721b8
PA
1729 }
1730 }
1731
35df4500 1732 bl->inserted = (val == 0);
879bfdc2
DJ
1733 }
1734
35df4500 1735 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1736 {
77b06cd7
TJB
1737 gdb_assert (bl->owner->ops != NULL
1738 && bl->owner->ops->insert_location != NULL);
1739
1740 val = bl->owner->ops->insert_location (bl);
1741 if (val)
1742 {
1743 bl->owner->enable_state = bp_disabled;
1744
1745 if (val == 1)
1746 warning (_("\
1747Error inserting catchpoint %d: Your system does not support this type\n\
1748of catchpoint."), bl->owner->number);
1749 else
1750 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1751 }
1752
1753 bl->inserted = (val == 0);
1640b821
DJ
1754
1755 /* We've already printed an error message if there was a problem
1756 inserting this catchpoint, and we've disabled the catchpoint,
1757 so just return success. */
1758 return 0;
879bfdc2
DJ
1759 }
1760
1761 return 0;
1762}
1763
6c95b8df
PA
1764/* This function is called when program space PSPACE is about to be
1765 deleted. It takes care of updating breakpoints to not reference
1766 PSPACE anymore. */
1767
1768void
1769breakpoint_program_space_exit (struct program_space *pspace)
1770{
1771 struct breakpoint *b, *b_temp;
876fa593 1772 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1773
1774 /* Remove any breakpoint that was set through this program space. */
1775 ALL_BREAKPOINTS_SAFE (b, b_temp)
1776 {
1777 if (b->pspace == pspace)
1778 delete_breakpoint (b);
1779 }
1780
1781 /* Breakpoints set through other program spaces could have locations
1782 bound to PSPACE as well. Remove those. */
876fa593 1783 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1784 {
1785 struct bp_location *tmp;
1786
1787 if (loc->pspace == pspace)
1788 {
2bdf28a0 1789 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1790 if (loc->owner->loc == loc)
1791 loc->owner->loc = loc->next;
1792 else
1793 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1794 if (tmp->next == loc)
1795 {
1796 tmp->next = loc->next;
1797 break;
1798 }
1799 }
1800 }
1801
1802 /* Now update the global location list to permanently delete the
1803 removed locations above. */
1804 update_global_location_list (0);
1805}
1806
74960c60
VP
1807/* Make sure all breakpoints are inserted in inferior.
1808 Throws exception on any error.
1809 A breakpoint that is already inserted won't be inserted
1810 again, so calling this function twice is safe. */
1811void
1812insert_breakpoints (void)
1813{
1814 struct breakpoint *bpt;
1815
1816 ALL_BREAKPOINTS (bpt)
1817 if (is_hardware_watchpoint (bpt))
4a64f543 1818 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1819
b60e7edf 1820 update_global_location_list (1);
74960c60 1821
c35b1492
PA
1822 /* update_global_location_list does not insert breakpoints when
1823 always_inserted_mode is not enabled. Explicitly insert them
1824 now. */
1825 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1826 insert_breakpoint_locations ();
1827}
1828
c906108c
SS
1829/* insert_breakpoints is used when starting or continuing the program.
1830 remove_breakpoints is used when the program stops.
1831 Both return zero if successful,
1832 or an `errno' value if could not write the inferior. */
1833
74960c60
VP
1834static void
1835insert_breakpoint_locations (void)
c906108c 1836{
a5606eee 1837 struct breakpoint *bpt;
35df4500 1838 struct bp_location *bl, **blp_tmp;
e236ba44 1839 int error = 0;
c906108c
SS
1840 int val = 0;
1841 int disabled_breaks = 0;
81d0cc19 1842 int hw_breakpoint_error = 0;
c906108c 1843
81d0cc19 1844 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1845 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1846
81d0cc19
GS
1847 /* Explicitly mark the warning -- this will only be printed if
1848 there was an error. */
1849 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1850
1851 save_current_space_and_thread ();
1852
35df4500 1853 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1854 {
35df4500 1855 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1856 continue;
1857
4a64f543
MS
1858 /* There is no point inserting thread-specific breakpoints if
1859 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1860 has BL->OWNER always non-NULL. */
35df4500
TJB
1861 if (bl->owner->thread != -1
1862 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1863 continue;
1864
35df4500 1865 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1866
1867 /* For targets that support global breakpoints, there's no need
1868 to select an inferior to insert breakpoint to. In fact, even
1869 if we aren't attached to any process yet, we should still
1870 insert breakpoints. */
1871 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1872 && ptid_equal (inferior_ptid, null_ptid))
1873 continue;
1874
35df4500 1875 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1876 &hw_breakpoint_error);
1877 if (val)
e236ba44 1878 error = val;
879bfdc2 1879 }
c906108c 1880
4a64f543
MS
1881 /* If we failed to insert all locations of a watchpoint, remove
1882 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1883 ALL_BREAKPOINTS (bpt)
1884 {
1885 int some_failed = 0;
1886 struct bp_location *loc;
1887
1888 if (!is_hardware_watchpoint (bpt))
1889 continue;
1890
d6b74ac4 1891 if (!breakpoint_enabled (bpt))
a5606eee 1892 continue;
74960c60
VP
1893
1894 if (bpt->disposition == disp_del_at_next_stop)
1895 continue;
a5606eee
VP
1896
1897 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1898 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1899 {
1900 some_failed = 1;
1901 break;
1902 }
1903 if (some_failed)
1904 {
1905 for (loc = bpt->loc; loc; loc = loc->next)
1906 if (loc->inserted)
1907 remove_breakpoint (loc, mark_uninserted);
1908
1909 hw_breakpoint_error = 1;
1910 fprintf_unfiltered (tmp_error_stream,
1911 "Could not insert hardware watchpoint %d.\n",
1912 bpt->number);
1913 error = -1;
1914 }
1915 }
1916
e236ba44 1917 if (error)
81d0cc19
GS
1918 {
1919 /* If a hardware breakpoint or watchpoint was inserted, add a
1920 message about possibly exhausted resources. */
879bfdc2 1921 if (hw_breakpoint_error)
81d0cc19 1922 {
c6510018
MS
1923 fprintf_unfiltered (tmp_error_stream,
1924 "Could not insert hardware breakpoints:\n\
1925You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1926 }
81d0cc19
GS
1927 target_terminal_ours_for_output ();
1928 error_stream (tmp_error_stream);
1929 }
f7545552
TT
1930
1931 do_cleanups (cleanups);
c906108c
SS
1932}
1933
c906108c 1934int
fba45db2 1935remove_breakpoints (void)
c906108c 1936{
35df4500 1937 struct bp_location *bl, **blp_tmp;
3a1bae8e 1938 int val = 0;
c906108c 1939
35df4500 1940 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1941 {
35df4500
TJB
1942 if (bl->inserted)
1943 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1944 }
3a1bae8e 1945 return val;
c906108c
SS
1946}
1947
6c95b8df
PA
1948/* Remove breakpoints of process PID. */
1949
1950int
1951remove_breakpoints_pid (int pid)
1952{
35df4500 1953 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1954 int val;
1955 struct inferior *inf = find_inferior_pid (pid);
1956
35df4500 1957 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 1958 {
35df4500 1959 if (bl->pspace != inf->pspace)
6c95b8df
PA
1960 continue;
1961
35df4500 1962 if (bl->inserted)
6c95b8df 1963 {
35df4500 1964 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
1965 if (val != 0)
1966 return val;
1967 }
1968 }
1969 return 0;
1970}
1971
692590c1 1972int
80ce1ecb 1973remove_hw_watchpoints (void)
692590c1 1974{
35df4500 1975 struct bp_location *bl, **blp_tmp;
3a1bae8e 1976 int val = 0;
692590c1 1977
35df4500 1978 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 1979 {
35df4500
TJB
1980 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
1981 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 1982 }
3a1bae8e 1983 return val;
692590c1
MS
1984}
1985
c906108c 1986int
fba45db2 1987reattach_breakpoints (int pid)
c906108c 1988{
6c95b8df 1989 struct cleanup *old_chain;
35df4500 1990 struct bp_location *bl, **blp_tmp;
c906108c 1991 int val;
86b887df 1992 struct ui_file *tmp_error_stream;
fa3a767f 1993 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
1994 struct inferior *inf;
1995 struct thread_info *tp;
1996
1997 tp = any_live_thread_of_process (pid);
1998 if (tp == NULL)
1999 return 1;
2000
2001 inf = find_inferior_pid (pid);
2002 old_chain = save_inferior_ptid ();
2003
2004 inferior_ptid = tp->ptid;
a4954f26 2005
86b887df 2006 tmp_error_stream = mem_fileopen ();
a4954f26 2007 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2008
35df4500 2009 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2010 {
35df4500 2011 if (bl->pspace != inf->pspace)
6c95b8df
PA
2012 continue;
2013
35df4500 2014 if (bl->inserted)
c5aa993b 2015 {
35df4500
TJB
2016 bl->inserted = 0;
2017 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2018 if (val != 0)
2019 {
ce696e05 2020 do_cleanups (old_chain);
c5aa993b
JM
2021 return val;
2022 }
2023 }
2024 }
ce696e05 2025 do_cleanups (old_chain);
c906108c
SS
2026 return 0;
2027}
2028
e58b0e63
PA
2029static int internal_breakpoint_number = -1;
2030
84f4c1fe
PM
2031/* Set the breakpoint number of B, depending on the value of INTERNAL.
2032 If INTERNAL is non-zero, the breakpoint number will be populated
2033 from internal_breakpoint_number and that variable decremented.
2034 Otherwis the breakpoint number will be populated from
2035 breakpoint_count and that value incremented. Internal breakpoints
2036 do not set the internal var bpnum. */
2037static void
2038set_breakpoint_number (int internal, struct breakpoint *b)
2039{
2040 if (internal)
2041 b->number = internal_breakpoint_number--;
2042 else
2043 {
2044 set_breakpoint_count (breakpoint_count + 1);
2045 b->number = breakpoint_count;
2046 }
2047}
2048
e62c965a 2049static struct breakpoint *
a6d9a66e
UW
2050create_internal_breakpoint (struct gdbarch *gdbarch,
2051 CORE_ADDR address, enum bptype type)
e62c965a 2052{
e62c965a
PP
2053 struct symtab_and_line sal;
2054 struct breakpoint *b;
2055
4a64f543 2056 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2057
2058 sal.pc = address;
2059 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2060 sal.pspace = current_program_space;
e62c965a 2061
a6d9a66e 2062 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2063 b->number = internal_breakpoint_number--;
2064 b->disposition = disp_donttouch;
2065
2066 return b;
2067}
2068
17450429
PP
2069static const char *const longjmp_names[] =
2070 {
2071 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2072 };
2073#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2074
2075/* Per-objfile data private to breakpoint.c. */
2076struct breakpoint_objfile_data
2077{
2078 /* Minimal symbol for "_ovly_debug_event" (if any). */
2079 struct minimal_symbol *overlay_msym;
2080
2081 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2082 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2083
2084 /* Minimal symbol for "std::terminate()" (if any). */
2085 struct minimal_symbol *terminate_msym;
2086
2087 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2088 struct minimal_symbol *exception_msym;
2089};
2090
2091static const struct objfile_data *breakpoint_objfile_key;
2092
2093/* Minimal symbol not found sentinel. */
2094static struct minimal_symbol msym_not_found;
2095
2096/* Returns TRUE if MSYM point to the "not found" sentinel. */
2097
2098static int
2099msym_not_found_p (const struct minimal_symbol *msym)
2100{
2101 return msym == &msym_not_found;
2102}
2103
2104/* Return per-objfile data needed by breakpoint.c.
2105 Allocate the data if necessary. */
2106
2107static struct breakpoint_objfile_data *
2108get_breakpoint_objfile_data (struct objfile *objfile)
2109{
2110 struct breakpoint_objfile_data *bp_objfile_data;
2111
2112 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2113 if (bp_objfile_data == NULL)
2114 {
2115 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2116 sizeof (*bp_objfile_data));
2117
2118 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2119 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2120 }
2121 return bp_objfile_data;
2122}
2123
e62c965a 2124static void
af02033e 2125create_overlay_event_breakpoint (void)
e62c965a 2126{
69de3c6a 2127 struct objfile *objfile;
af02033e 2128 const char *const func_name = "_ovly_debug_event";
e62c965a 2129
69de3c6a
PP
2130 ALL_OBJFILES (objfile)
2131 {
2132 struct breakpoint *b;
17450429
PP
2133 struct breakpoint_objfile_data *bp_objfile_data;
2134 CORE_ADDR addr;
69de3c6a 2135
17450429
PP
2136 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2137
2138 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2139 continue;
2140
2141 if (bp_objfile_data->overlay_msym == NULL)
2142 {
2143 struct minimal_symbol *m;
2144
2145 m = lookup_minimal_symbol_text (func_name, objfile);
2146 if (m == NULL)
2147 {
2148 /* Avoid future lookups in this objfile. */
2149 bp_objfile_data->overlay_msym = &msym_not_found;
2150 continue;
2151 }
2152 bp_objfile_data->overlay_msym = m;
2153 }
e62c965a 2154
17450429
PP
2155 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2156 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2157 bp_overlay_event);
2158 b->addr_string = xstrdup (func_name);
e62c965a 2159
69de3c6a
PP
2160 if (overlay_debugging == ovly_auto)
2161 {
2162 b->enable_state = bp_enabled;
2163 overlay_events_enabled = 1;
2164 }
2165 else
2166 {
2167 b->enable_state = bp_disabled;
2168 overlay_events_enabled = 0;
2169 }
e62c965a
PP
2170 }
2171 update_global_location_list (1);
2172}
2173
0fd8e87f 2174static void
af02033e 2175create_longjmp_master_breakpoint (void)
0fd8e87f 2176{
6c95b8df 2177 struct program_space *pspace;
6c95b8df
PA
2178 struct cleanup *old_chain;
2179
2180 old_chain = save_current_program_space ();
0fd8e87f 2181
6c95b8df 2182 ALL_PSPACES (pspace)
af02033e
PP
2183 {
2184 struct objfile *objfile;
2185
2186 set_current_program_space (pspace);
2187
2188 ALL_OBJFILES (objfile)
0fd8e87f 2189 {
af02033e
PP
2190 int i;
2191 struct gdbarch *gdbarch;
17450429 2192 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2193
af02033e
PP
2194 gdbarch = get_objfile_arch (objfile);
2195 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2196 continue;
2197
17450429
PP
2198 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2199
2200 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2201 {
2202 struct breakpoint *b;
af02033e 2203 const char *func_name;
17450429 2204 CORE_ADDR addr;
6c95b8df 2205
17450429 2206 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2207 continue;
0fd8e87f 2208
17450429
PP
2209 func_name = longjmp_names[i];
2210 if (bp_objfile_data->longjmp_msym[i] == NULL)
2211 {
2212 struct minimal_symbol *m;
2213
2214 m = lookup_minimal_symbol_text (func_name, objfile);
2215 if (m == NULL)
2216 {
2217 /* Prevent future lookups in this objfile. */
2218 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2219 continue;
2220 }
2221 bp_objfile_data->longjmp_msym[i] = m;
2222 }
2223
2224 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2225 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2226 b->addr_string = xstrdup (func_name);
2227 b->enable_state = bp_disabled;
2228 }
0fd8e87f 2229 }
af02033e 2230 }
0fd8e87f 2231 update_global_location_list (1);
6c95b8df
PA
2232
2233 do_cleanups (old_chain);
0fd8e87f
UW
2234}
2235
af02033e 2236/* Create a master std::terminate breakpoint. */
aa7d318d 2237static void
af02033e 2238create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2239{
2240 struct program_space *pspace;
aa7d318d 2241 struct cleanup *old_chain;
af02033e 2242 const char *const func_name = "std::terminate()";
aa7d318d
TT
2243
2244 old_chain = save_current_program_space ();
2245
2246 ALL_PSPACES (pspace)
17450429
PP
2247 {
2248 struct objfile *objfile;
2249 CORE_ADDR addr;
2250
2251 set_current_program_space (pspace);
2252
aa7d318d
TT
2253 ALL_OBJFILES (objfile)
2254 {
2255 struct breakpoint *b;
17450429 2256 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2257
17450429 2258 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2259
17450429
PP
2260 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2261 continue;
2262
2263 if (bp_objfile_data->terminate_msym == NULL)
2264 {
2265 struct minimal_symbol *m;
2266
2267 m = lookup_minimal_symbol (func_name, NULL, objfile);
2268 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2269 && MSYMBOL_TYPE (m) != mst_file_text))
2270 {
2271 /* Prevent future lookups in this objfile. */
2272 bp_objfile_data->terminate_msym = &msym_not_found;
2273 continue;
2274 }
2275 bp_objfile_data->terminate_msym = m;
2276 }
aa7d318d 2277
17450429
PP
2278 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2279 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2280 bp_std_terminate_master);
2281 b->addr_string = xstrdup (func_name);
2282 b->enable_state = bp_disabled;
2283 }
17450429
PP
2284 }
2285
aa7d318d
TT
2286 update_global_location_list (1);
2287
2288 do_cleanups (old_chain);
2289}
2290
186c406b
TT
2291/* Install a master breakpoint on the unwinder's debug hook. */
2292
2293void
2294create_exception_master_breakpoint (void)
2295{
2296 struct objfile *objfile;
17450429 2297 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2298
2299 ALL_OBJFILES (objfile)
2300 {
17450429
PP
2301 struct breakpoint *b;
2302 struct gdbarch *gdbarch;
2303 struct breakpoint_objfile_data *bp_objfile_data;
2304 CORE_ADDR addr;
2305
2306 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2307
2308 if (msym_not_found_p (bp_objfile_data->exception_msym))
2309 continue;
2310
2311 gdbarch = get_objfile_arch (objfile);
186c406b 2312
17450429 2313 if (bp_objfile_data->exception_msym == NULL)
186c406b 2314 {
17450429 2315 struct minimal_symbol *debug_hook;
186c406b 2316
17450429
PP
2317 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2318 if (debug_hook == NULL)
2319 {
2320 bp_objfile_data->exception_msym = &msym_not_found;
2321 continue;
2322 }
2323
2324 bp_objfile_data->exception_msym = debug_hook;
186c406b 2325 }
17450429
PP
2326
2327 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2328 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2329 &current_target);
2330 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2331 b->addr_string = xstrdup (func_name);
2332 b->enable_state = bp_disabled;
186c406b
TT
2333 }
2334
2335 update_global_location_list (1);
2336}
2337
c906108c 2338void
fba45db2 2339update_breakpoints_after_exec (void)
c906108c 2340{
35df4500 2341 struct breakpoint *b, *b_tmp;
876fa593 2342 struct bp_location *bploc, **bplocp_tmp;
c906108c 2343
25b22b0a
PA
2344 /* We're about to delete breakpoints from GDB's lists. If the
2345 INSERTED flag is true, GDB will try to lift the breakpoints by
2346 writing the breakpoints' "shadow contents" back into memory. The
2347 "shadow contents" are NOT valid after an exec, so GDB should not
2348 do that. Instead, the target is responsible from marking
2349 breakpoints out as soon as it detects an exec. We don't do that
2350 here instead, because there may be other attempts to delete
2351 breakpoints after detecting an exec and before reaching here. */
876fa593 2352 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2353 if (bploc->pspace == current_program_space)
2354 gdb_assert (!bploc->inserted);
c906108c 2355
35df4500 2356 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2357 {
6c95b8df
PA
2358 if (b->pspace != current_program_space)
2359 continue;
2360
4a64f543 2361 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2362 if (b->type == bp_shlib_event)
2363 {
2364 delete_breakpoint (b);
2365 continue;
2366 }
c906108c 2367
4a64f543 2368 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2369 if (b->type == bp_jit_event)
2370 {
2371 delete_breakpoint (b);
2372 continue;
2373 }
2374
1900040c 2375 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2376 as must overlay event and longjmp master breakpoints. */
2377 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2378 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2379 || b->type == bp_exception_master)
c4093a6a
JM
2380 {
2381 delete_breakpoint (b);
2382 continue;
2383 }
2384
4a64f543 2385 /* Step-resume breakpoints are meaningless after an exec(). */
c5aa993b
JM
2386 if (b->type == bp_step_resume)
2387 {
2388 delete_breakpoint (b);
2389 continue;
2390 }
2391
611c83ae
PA
2392 /* Longjmp and longjmp-resume breakpoints are also meaningless
2393 after an exec. */
186c406b
TT
2394 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2395 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2396 {
2397 delete_breakpoint (b);
2398 continue;
2399 }
2400
ce78b96d
JB
2401 if (b->type == bp_catchpoint)
2402 {
2403 /* For now, none of the bp_catchpoint breakpoints need to
2404 do anything at this point. In the future, if some of
2405 the catchpoints need to something, we will need to add
2406 a new method, and call this method from here. */
2407 continue;
2408 }
2409
c5aa993b
JM
2410 /* bp_finish is a special case. The only way we ought to be able
2411 to see one of these when an exec() has happened, is if the user
2412 caught a vfork, and then said "finish". Ordinarily a finish just
2413 carries them to the call-site of the current callee, by setting
2414 a temporary bp there and resuming. But in this case, the finish
2415 will carry them entirely through the vfork & exec.
2416
2417 We don't want to allow a bp_finish to remain inserted now. But
2418 we can't safely delete it, 'cause finish_command has a handle to
2419 the bp on a bpstat, and will later want to delete it. There's a
2420 chance (and I've seen it happen) that if we delete the bp_finish
2421 here, that its storage will get reused by the time finish_command
2422 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2423 We really must allow finish_command to delete a bp_finish.
2424
53a5351d
JM
2425 In the absense of a general solution for the "how do we know
2426 it's safe to delete something others may have handles to?"
2427 problem, what we'll do here is just uninsert the bp_finish, and
2428 let finish_command delete it.
2429
2430 (We know the bp_finish is "doomed" in the sense that it's
2431 momentary, and will be deleted as soon as finish_command sees
2432 the inferior stopped. So it doesn't matter that the bp's
2433 address is probably bogus in the new a.out, unlike e.g., the
2434 solib breakpoints.) */
c5aa993b 2435
c5aa993b
JM
2436 if (b->type == bp_finish)
2437 {
2438 continue;
2439 }
2440
2441 /* Without a symbolic address, we have little hope of the
2442 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2443 a.out. */
c5aa993b
JM
2444 if (b->addr_string == NULL)
2445 {
2446 delete_breakpoint (b);
2447 continue;
2448 }
c5aa993b 2449 }
1900040c 2450 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2451 create_overlay_event_breakpoint ();
2452 create_longjmp_master_breakpoint ();
2453 create_std_terminate_master_breakpoint ();
186c406b 2454 create_exception_master_breakpoint ();
c906108c
SS
2455}
2456
2457int
fba45db2 2458detach_breakpoints (int pid)
c906108c 2459{
35df4500 2460 struct bp_location *bl, **blp_tmp;
3a1bae8e 2461 int val = 0;
ce696e05 2462 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2463 struct inferior *inf = current_inferior ();
c5aa993b 2464
39f77062 2465 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2466 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2467
6c95b8df 2468 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2469 inferior_ptid = pid_to_ptid (pid);
35df4500 2470 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2471 {
35df4500 2472 if (bl->pspace != inf->pspace)
6c95b8df
PA
2473 continue;
2474
35df4500
TJB
2475 if (bl->inserted)
2476 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2477 }
d03285ec
UW
2478
2479 /* Detach single-step breakpoints as well. */
2480 detach_single_step_breakpoints ();
2481
ce696e05 2482 do_cleanups (old_chain);
3a1bae8e 2483 return val;
c906108c
SS
2484}
2485
35df4500 2486/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2487 Note that this is used to detach breakpoints from a child fork.
2488 When we get here, the child isn't in the inferior list, and neither
2489 do we have objects to represent its address space --- we should
35df4500 2490 *not* look at bl->pspace->aspace here. */
6c95b8df 2491
c906108c 2492static int
35df4500 2493remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2494{
2495 int val;
c5aa993b 2496
35df4500
TJB
2497 /* BL is never in moribund_locations by our callers. */
2498 gdb_assert (bl->owner != NULL);
2bdf28a0 2499
35df4500 2500 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2501 /* Permanent breakpoints cannot be inserted or removed. */
2502 return 0;
2503
74960c60
VP
2504 /* The type of none suggests that owner is actually deleted.
2505 This should not ever happen. */
35df4500 2506 gdb_assert (bl->owner->type != bp_none);
0bde7532 2507
35df4500
TJB
2508 if (bl->loc_type == bp_loc_software_breakpoint
2509 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2510 {
c02f5703
MS
2511 /* "Normal" instruction breakpoint: either the standard
2512 trap-instruction bp (bp_breakpoint), or a
2513 bp_hardware_breakpoint. */
2514
2515 /* First check to see if we have to handle an overlay. */
2516 if (overlay_debugging == ovly_off
35df4500
TJB
2517 || bl->section == NULL
2518 || !(section_is_overlay (bl->section)))
c02f5703
MS
2519 {
2520 /* No overlay handling: just remove the breakpoint. */
2521
35df4500
TJB
2522 if (bl->loc_type == bp_loc_hardware_breakpoint)
2523 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2524 else
35df4500 2525 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2526 }
c906108c
SS
2527 else
2528 {
4a64f543 2529 /* This breakpoint is in an overlay section.
c02f5703
MS
2530 Did we set a breakpoint at the LMA? */
2531 if (!overlay_events_enabled)
2532 {
2533 /* Yes -- overlay event support is not active, so we
2534 should have set a breakpoint at the LMA. Remove it.
2535 */
c02f5703
MS
2536 /* Ignore any failures: if the LMA is in ROM, we will
2537 have already warned when we failed to insert it. */
35df4500
TJB
2538 if (bl->loc_type == bp_loc_hardware_breakpoint)
2539 target_remove_hw_breakpoint (bl->gdbarch,
2540 &bl->overlay_target_info);
c02f5703 2541 else
35df4500
TJB
2542 target_remove_breakpoint (bl->gdbarch,
2543 &bl->overlay_target_info);
c02f5703
MS
2544 }
2545 /* Did we set a breakpoint at the VMA?
2546 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2547 if (bl->inserted)
c906108c 2548 {
c02f5703
MS
2549 /* Yes -- remove it. Previously we did not bother to
2550 remove the breakpoint if the section had been
2551 unmapped, but let's not rely on that being safe. We
2552 don't know what the overlay manager might do. */
35df4500
TJB
2553 if (bl->loc_type == bp_loc_hardware_breakpoint)
2554 val = target_remove_hw_breakpoint (bl->gdbarch,
2555 &bl->target_info);
aa67235e
UW
2556
2557 /* However, we should remove *software* breakpoints only
2558 if the section is still mapped, or else we overwrite
2559 wrong code with the saved shadow contents. */
35df4500
TJB
2560 else if (section_is_mapped (bl->section))
2561 val = target_remove_breakpoint (bl->gdbarch,
2562 &bl->target_info);
aa67235e
UW
2563 else
2564 val = 0;
c906108c 2565 }
c02f5703
MS
2566 else
2567 {
2568 /* No -- not inserted, so no need to remove. No error. */
2569 val = 0;
2570 }
c906108c 2571 }
879d1e6b
UW
2572
2573 /* In some cases, we might not be able to remove a breakpoint
2574 in a shared library that has already been removed, but we
2575 have not yet processed the shlib unload event. */
35df4500 2576 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2577 val = 0;
2578
c906108c
SS
2579 if (val)
2580 return val;
35df4500 2581 bl->inserted = (is == mark_inserted);
c906108c 2582 }
35df4500 2583 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2584 {
77b06cd7
TJB
2585 gdb_assert (bl->owner->ops != NULL
2586 && bl->owner->ops->remove_location != NULL);
2587
35df4500 2588 bl->inserted = (is == mark_inserted);
77b06cd7 2589 bl->owner->ops->remove_location (bl);
2e70b7b9 2590
c906108c 2591 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2592 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2593 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2594 bl->owner->number);
c906108c 2595 }
35df4500
TJB
2596 else if (bl->owner->type == bp_catchpoint
2597 && breakpoint_enabled (bl->owner)
2598 && !bl->duplicate)
ce78b96d 2599 {
77b06cd7
TJB
2600 gdb_assert (bl->owner->ops != NULL
2601 && bl->owner->ops->remove_location != NULL);
ce78b96d 2602
77b06cd7 2603 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2604 if (val)
2605 return val;
77b06cd7 2606
35df4500 2607 bl->inserted = (is == mark_inserted);
ce78b96d 2608 }
c906108c
SS
2609
2610 return 0;
2611}
2612
6c95b8df 2613static int
35df4500 2614remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2615{
2616 int ret;
2617 struct cleanup *old_chain;
2618
35df4500
TJB
2619 /* BL is never in moribund_locations by our callers. */
2620 gdb_assert (bl->owner != NULL);
2bdf28a0 2621
35df4500 2622 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2623 /* Permanent breakpoints cannot be inserted or removed. */
2624 return 0;
2625
2626 /* The type of none suggests that owner is actually deleted.
2627 This should not ever happen. */
35df4500 2628 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2629
2630 old_chain = save_current_space_and_thread ();
2631
35df4500 2632 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2633
35df4500 2634 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2635
2636 do_cleanups (old_chain);
2637 return ret;
2638}
2639
c906108c
SS
2640/* Clear the "inserted" flag in all breakpoints. */
2641
25b22b0a 2642void
fba45db2 2643mark_breakpoints_out (void)
c906108c 2644{
35df4500 2645 struct bp_location *bl, **blp_tmp;
c906108c 2646
35df4500
TJB
2647 ALL_BP_LOCATIONS (bl, blp_tmp)
2648 if (bl->pspace == current_program_space)
2649 bl->inserted = 0;
c906108c
SS
2650}
2651
53a5351d
JM
2652/* Clear the "inserted" flag in all breakpoints and delete any
2653 breakpoints which should go away between runs of the program.
c906108c
SS
2654
2655 Plus other such housekeeping that has to be done for breakpoints
2656 between runs.
2657
53a5351d
JM
2658 Note: this function gets called at the end of a run (by
2659 generic_mourn_inferior) and when a run begins (by
4a64f543 2660 init_wait_for_inferior). */
c906108c
SS
2661
2662
2663
2664void
fba45db2 2665breakpoint_init_inferior (enum inf_context context)
c906108c 2666{
35df4500
TJB
2667 struct breakpoint *b, *b_tmp;
2668 struct bp_location *bl, **blp_tmp;
1c5cfe86 2669 int ix;
6c95b8df 2670 struct program_space *pspace = current_program_space;
c906108c 2671
50c71eaf
PA
2672 /* If breakpoint locations are shared across processes, then there's
2673 nothing to do. */
2567c7d9 2674 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2675 return;
2676
35df4500 2677 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2678 {
35df4500
TJB
2679 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2680 if (bl->pspace == pspace
2681 && bl->owner->enable_state != bp_permanent)
2682 bl->inserted = 0;
6c95b8df 2683 }
075f6582 2684
35df4500 2685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2686 {
6c95b8df
PA
2687 if (b->loc && b->loc->pspace != pspace)
2688 continue;
2689
c5aa993b
JM
2690 switch (b->type)
2691 {
2692 case bp_call_dummy:
c906108c 2693
c5aa993b 2694 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2695 cause problems when the inferior is rerun, so we better get
2696 rid of it. */
2697
2698 case bp_watchpoint_scope:
2699
2700 /* Also get rid of scope breakpoints. */
2701
2702 case bp_shlib_event:
2703
2704 /* Also remove solib event breakpoints. Their addresses may
2705 have changed since the last time we ran the program.
2706 Actually we may now be debugging against different target;
2707 and so the solib backend that installed this breakpoint may
2708 not be used in by the target. E.g.,
2709
2710 (gdb) file prog-linux
2711 (gdb) run # native linux target
2712 ...
2713 (gdb) kill
2714 (gdb) file prog-win.exe
2715 (gdb) tar rem :9999 # remote Windows gdbserver.
2716 */
c906108c 2717
c5aa993b
JM
2718 delete_breakpoint (b);
2719 break;
c906108c 2720
c5aa993b
JM
2721 case bp_watchpoint:
2722 case bp_hardware_watchpoint:
2723 case bp_read_watchpoint:
2724 case bp_access_watchpoint:
c906108c 2725
c5aa993b
JM
2726 /* Likewise for watchpoints on local expressions. */
2727 if (b->exp_valid_block != NULL)
2728 delete_breakpoint (b);
967af18d 2729 else if (context == inf_starting)
c860120c 2730 {
4a64f543
MS
2731 /* Reset val field to force reread of starting value in
2732 insert_breakpoints. */
c860120c
PM
2733 if (b->val)
2734 value_free (b->val);
2735 b->val = NULL;
fa4727a6 2736 b->val_valid = 0;
c860120c 2737 }
c5aa993b
JM
2738 break;
2739 default:
c5aa993b
JM
2740 break;
2741 }
2742 }
1c5cfe86
PA
2743
2744 /* Get rid of the moribund locations. */
35df4500
TJB
2745 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2746 decref_bp_location (&bl);
1c5cfe86 2747 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2748}
2749
6c95b8df
PA
2750/* These functions concern about actual breakpoints inserted in the
2751 target --- to e.g. check if we need to do decr_pc adjustment or if
2752 we need to hop over the bkpt --- so we check for address space
2753 match, not program space. */
2754
c2c6d25f
JM
2755/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2756 exists at PC. It returns ordinary_breakpoint_here if it's an
2757 ordinary breakpoint, or permanent_breakpoint_here if it's a
2758 permanent breakpoint.
2759 - When continuing from a location with an ordinary breakpoint, we
2760 actually single step once before calling insert_breakpoints.
2761 - When continuing from a localion with a permanent breakpoint, we
2762 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2763 the target, to advance the PC past the breakpoint. */
c906108c 2764
c2c6d25f 2765enum breakpoint_here
6c95b8df 2766breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2767{
35df4500 2768 struct bp_location *bl, **blp_tmp;
c2c6d25f 2769 int any_breakpoint_here = 0;
c906108c 2770
35df4500 2771 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2772 {
35df4500
TJB
2773 if (bl->loc_type != bp_loc_software_breakpoint
2774 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2775 continue;
2776
35df4500
TJB
2777 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2778 if ((breakpoint_enabled (bl->owner)
2779 || bl->owner->enable_state == bp_permanent)
2780 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2781 aspace, pc))
075f6582
DJ
2782 {
2783 if (overlay_debugging
35df4500
TJB
2784 && section_is_overlay (bl->section)
2785 && !section_is_mapped (bl->section))
075f6582 2786 continue; /* unmapped overlay -- can't be a match */
35df4500 2787 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2788 return permanent_breakpoint_here;
2789 else
2790 any_breakpoint_here = 1;
2791 }
2792 }
c906108c 2793
c2c6d25f 2794 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2795}
2796
1c5cfe86
PA
2797/* Return true if there's a moribund breakpoint at PC. */
2798
2799int
6c95b8df 2800moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2801{
2802 struct bp_location *loc;
2803 int ix;
2804
2805 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
6c95b8df
PA
2806 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2807 aspace, pc))
1c5cfe86
PA
2808 return 1;
2809
2810 return 0;
2811}
c2c6d25f 2812
c36b740a 2813/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2814 inserted using regular breakpoint_chain / bp_location array
2815 mechanism. This does not check for single-step breakpoints, which
2816 are inserted and removed using direct target manipulation. */
c906108c
SS
2817
2818int
4a64f543
MS
2819regular_breakpoint_inserted_here_p (struct address_space *aspace,
2820 CORE_ADDR pc)
c906108c 2821{
35df4500 2822 struct bp_location *bl, **blp_tmp;
c906108c 2823
35df4500 2824 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2825 {
35df4500
TJB
2826 if (bl->loc_type != bp_loc_software_breakpoint
2827 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2828 continue;
2829
35df4500
TJB
2830 if (bl->inserted
2831 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2832 aspace, pc))
075f6582
DJ
2833 {
2834 if (overlay_debugging
35df4500
TJB
2835 && section_is_overlay (bl->section)
2836 && !section_is_mapped (bl->section))
075f6582
DJ
2837 continue; /* unmapped overlay -- can't be a match */
2838 else
2839 return 1;
2840 }
c5aa993b 2841 }
c36b740a
VP
2842 return 0;
2843}
2844
2845/* Returns non-zero iff there's either regular breakpoint
2846 or a single step breakpoint inserted at PC. */
2847
2848int
6c95b8df 2849breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2850{
6c95b8df 2851 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2852 return 1;
c906108c 2853
6c95b8df 2854 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2855 return 1;
2856
c906108c
SS
2857 return 0;
2858}
2859
4fa8626c
DJ
2860/* This function returns non-zero iff there is a software breakpoint
2861 inserted at PC. */
2862
2863int
3e43a32a
MS
2864software_breakpoint_inserted_here_p (struct address_space *aspace,
2865 CORE_ADDR pc)
4fa8626c 2866{
35df4500 2867 struct bp_location *bl, **blp_tmp;
4fa8626c 2868
35df4500 2869 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2870 {
35df4500 2871 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2872 continue;
2873
35df4500
TJB
2874 if (bl->inserted
2875 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2876 aspace, pc))
4fa8626c
DJ
2877 {
2878 if (overlay_debugging
35df4500
TJB
2879 && section_is_overlay (bl->section)
2880 && !section_is_mapped (bl->section))
4fa8626c
DJ
2881 continue; /* unmapped overlay -- can't be a match */
2882 else
2883 return 1;
2884 }
2885 }
2886
1aafd4da 2887 /* Also check for software single-step breakpoints. */
6c95b8df 2888 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2889 return 1;
2890
4fa8626c
DJ
2891 return 0;
2892}
2893
9093389c
PA
2894int
2895hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2896 CORE_ADDR addr, ULONGEST len)
2897{
2898 struct breakpoint *bpt;
2899
2900 ALL_BREAKPOINTS (bpt)
2901 {
2902 struct bp_location *loc;
2903
2904 if (bpt->type != bp_hardware_watchpoint
2905 && bpt->type != bp_access_watchpoint)
2906 continue;
2907
2908 if (!breakpoint_enabled (bpt))
2909 continue;
2910
2911 for (loc = bpt->loc; loc; loc = loc->next)
2912 if (loc->pspace->aspace == aspace && loc->inserted)
2913 {
2914 CORE_ADDR l, h;
2915
2916 /* Check for intersection. */
2917 l = max (loc->address, addr);
2918 h = min (loc->address + loc->length, addr + len);
2919 if (l < h)
2920 return 1;
2921 }
2922 }
2923 return 0;
2924}
2925
075f6582
DJ
2926/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2927 PC is valid for process/thread PTID. */
c906108c
SS
2928
2929int
6c95b8df
PA
2930breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2931 ptid_t ptid)
c906108c 2932{
35df4500 2933 struct bp_location *bl, **blp_tmp;
4a306c9a 2934 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2935 int thread = -1;
4a306c9a 2936 int task = 0;
a6f1cd96 2937
35df4500 2938 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2939 {
35df4500
TJB
2940 if (bl->loc_type != bp_loc_software_breakpoint
2941 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2942 continue;
2943
35df4500
TJB
2944 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2945 if (!breakpoint_enabled (bl->owner)
2946 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2947 continue;
2948
35df4500 2949 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2950 aspace, pc))
a6f1cd96
JB
2951 continue;
2952
35df4500 2953 if (bl->owner->thread != -1)
075f6582 2954 {
a6f1cd96
JB
2955 /* This is a thread-specific breakpoint. Check that ptid
2956 matches that thread. If thread hasn't been computed yet,
2957 it is now time to do so. */
2958 if (thread == -1)
2959 thread = pid_to_thread_id (ptid);
35df4500 2960 if (bl->owner->thread != thread)
a6f1cd96 2961 continue;
075f6582 2962 }
a6f1cd96 2963
35df4500 2964 if (bl->owner->task != 0)
4a306c9a
JB
2965 {
2966 /* This is a task-specific breakpoint. Check that ptid
2967 matches that task. If task hasn't been computed yet,
2968 it is now time to do so. */
2969 if (task == 0)
2970 task = ada_get_task_number (ptid);
35df4500 2971 if (bl->owner->task != task)
4a306c9a
JB
2972 continue;
2973 }
2974
a6f1cd96 2975 if (overlay_debugging
35df4500
TJB
2976 && section_is_overlay (bl->section)
2977 && !section_is_mapped (bl->section))
a6f1cd96
JB
2978 continue; /* unmapped overlay -- can't be a match */
2979
2980 return 1;
c5aa993b 2981 }
c906108c
SS
2982
2983 return 0;
2984}
c906108c 2985\f
c5aa993b 2986
c906108c
SS
2987/* bpstat stuff. External routines' interfaces are documented
2988 in breakpoint.h. */
2989
2990int
fba45db2 2991ep_is_catchpoint (struct breakpoint *ep)
c906108c 2992{
533be4dd 2993 return (ep->type == bp_catchpoint);
c906108c
SS
2994}
2995
f431efe5
PA
2996/* Frees any storage that is part of a bpstat. Does not walk the
2997 'next' chain. */
2998
2999static void
198757a8
VP
3000bpstat_free (bpstat bs)
3001{
3002 if (bs->old_val != NULL)
3003 value_free (bs->old_val);
9add0f1b 3004 decref_counted_command_line (&bs->commands);
f431efe5 3005 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3006 xfree (bs);
3007}
3008
c906108c
SS
3009/* Clear a bpstat so that it says we are not at any breakpoint.
3010 Also free any storage that is part of a bpstat. */
3011
3012void
fba45db2 3013bpstat_clear (bpstat *bsp)
c906108c
SS
3014{
3015 bpstat p;
3016 bpstat q;
3017
3018 if (bsp == 0)
3019 return;
3020 p = *bsp;
3021 while (p != NULL)
3022 {
3023 q = p->next;
198757a8 3024 bpstat_free (p);
c906108c
SS
3025 p = q;
3026 }
3027 *bsp = NULL;
3028}
3029
3030/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3031 is part of the bpstat is copied as well. */
3032
3033bpstat
fba45db2 3034bpstat_copy (bpstat bs)
c906108c
SS
3035{
3036 bpstat p = NULL;
3037 bpstat tmp;
3038 bpstat retval = NULL;
3039
3040 if (bs == NULL)
3041 return bs;
3042
3043 for (; bs != NULL; bs = bs->next)
3044 {
3045 tmp = (bpstat) xmalloc (sizeof (*tmp));
3046 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3047 incref_counted_command_line (tmp->commands);
f431efe5 3048 incref_bp_location (tmp->bp_location_at);
31cc81e9 3049 if (bs->old_val != NULL)
3c3185ac
JK
3050 {
3051 tmp->old_val = value_copy (bs->old_val);
3052 release_value (tmp->old_val);
3053 }
31cc81e9 3054
c906108c
SS
3055 if (p == NULL)
3056 /* This is the first thing in the chain. */
3057 retval = tmp;
3058 else
3059 p->next = tmp;
3060 p = tmp;
3061 }
3062 p->next = NULL;
3063 return retval;
3064}
3065
4a64f543 3066/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3067
3068bpstat
fba45db2 3069bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3070{
c5aa993b
JM
3071 if (bsp == NULL)
3072 return NULL;
c906108c 3073
c5aa993b
JM
3074 for (; bsp != NULL; bsp = bsp->next)
3075 {
f431efe5 3076 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3077 return bsp;
3078 }
c906108c
SS
3079 return NULL;
3080}
3081
4a64f543
MS
3082/* Put in *NUM the breakpoint number of the first breakpoint we are
3083 stopped at. *BSP upon return is a bpstat which points to the
3084 remaining breakpoints stopped at (but which is not guaranteed to be
3085 good for anything but further calls to bpstat_num).
3086
8671a17b
PA
3087 Return 0 if passed a bpstat which does not indicate any breakpoints.
3088 Return -1 if stopped at a breakpoint that has been deleted since
3089 we set it.
3090 Return 1 otherwise. */
c906108c
SS
3091
3092int
8671a17b 3093bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3094{
3095 struct breakpoint *b;
3096
3097 if ((*bsp) == NULL)
3098 return 0; /* No more breakpoint values */
8671a17b 3099
4a64f543
MS
3100 /* We assume we'll never have several bpstats that correspond to a
3101 single breakpoint -- otherwise, this function might return the
3102 same number more than once and this will look ugly. */
f431efe5 3103 b = (*bsp)->breakpoint_at;
8671a17b
PA
3104 *bsp = (*bsp)->next;
3105 if (b == NULL)
3106 return -1; /* breakpoint that's been deleted since */
3107
3108 *num = b->number; /* We have its number */
3109 return 1;
c906108c
SS
3110}
3111
3112/* Modify BS so that the actions will not be performed. */
3113
3114void
fba45db2 3115bpstat_clear_actions (bpstat bs)
c906108c
SS
3116{
3117 for (; bs != NULL; bs = bs->next)
3118 {
9add0f1b 3119 decref_counted_command_line (&bs->commands);
dde2d684 3120 bs->commands_left = NULL;
c906108c
SS
3121 if (bs->old_val != NULL)
3122 {
3123 value_free (bs->old_val);
3124 bs->old_val = NULL;
3125 }
3126 }
3127}
3128
f3b1572e
PA
3129/* Called when a command is about to proceed the inferior. */
3130
3131static void
3132breakpoint_about_to_proceed (void)
3133{
3134 if (!ptid_equal (inferior_ptid, null_ptid))
3135 {
3136 struct thread_info *tp = inferior_thread ();
3137
3138 /* Allow inferior function calls in breakpoint commands to not
3139 interrupt the command list. When the call finishes
3140 successfully, the inferior will be standing at the same
3141 breakpoint as if nothing happened. */
16c381f0 3142 if (tp->control.in_infcall)
f3b1572e
PA
3143 return;
3144 }
3145
3146 breakpoint_proceeded = 1;
3147}
3148
4a64f543
MS
3149/* Stub for cleaning up our state if we error-out of a breakpoint
3150 command. */
c906108c 3151static void
4efb68b1 3152cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3153{
3154 executing_breakpoint_commands = 0;
3155}
3156
4a64f543
MS
3157/* Execute all the commands associated with all the breakpoints at
3158 this location. Any of these commands could cause the process to
3159 proceed beyond this point, etc. We look out for such changes by
3160 checking the global "breakpoint_proceeded" after each command.
c906108c 3161
347bddb7
PA
3162 Returns true if a breakpoint command resumed the inferior. In that
3163 case, it is the caller's responsibility to recall it again with the
3164 bpstat of the current thread. */
3165
3166static int
3167bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3168{
3169 bpstat bs;
3170 struct cleanup *old_chain;
347bddb7 3171 int again = 0;
c906108c
SS
3172
3173 /* Avoid endless recursion if a `source' command is contained
3174 in bs->commands. */
3175 if (executing_breakpoint_commands)
347bddb7 3176 return 0;
c906108c
SS
3177
3178 executing_breakpoint_commands = 1;
3179 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3180
4a64f543 3181 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3182 bs = *bsp;
3183
3184 breakpoint_proceeded = 0;
3185 for (; bs != NULL; bs = bs->next)
3186 {
9add0f1b 3187 struct counted_command_line *ccmd;
6c50ab1c
JB
3188 struct command_line *cmd;
3189 struct cleanup *this_cmd_tree_chain;
3190
3191 /* Take ownership of the BSP's command tree, if it has one.
3192
3193 The command tree could legitimately contain commands like
3194 'step' and 'next', which call clear_proceed_status, which
3195 frees stop_bpstat's command tree. To make sure this doesn't
3196 free the tree we're executing out from under us, we need to
3197 take ownership of the tree ourselves. Since a given bpstat's
3198 commands are only executed once, we don't need to copy it; we
3199 can clear the pointer in the bpstat, and make sure we free
3200 the tree when we're done. */
9add0f1b
TT
3201 ccmd = bs->commands;
3202 bs->commands = NULL;
3203 this_cmd_tree_chain
3204 = make_cleanup_decref_counted_command_line (&ccmd);
3205 cmd = bs->commands_left;
3206 bs->commands_left = NULL;
6c50ab1c 3207
c906108c
SS
3208 while (cmd != NULL)
3209 {
3210 execute_control_command (cmd);
3211
3212 if (breakpoint_proceeded)
3213 break;
3214 else
3215 cmd = cmd->next;
3216 }
6c50ab1c
JB
3217
3218 /* We can free this command tree now. */
3219 do_cleanups (this_cmd_tree_chain);
3220
c906108c 3221 if (breakpoint_proceeded)
32c1e744
VP
3222 {
3223 if (target_can_async_p ())
347bddb7
PA
3224 /* If we are in async mode, then the target might be still
3225 running, not stopped at any breakpoint, so nothing for
3226 us to do here -- just return to the event loop. */
3227 ;
32c1e744
VP
3228 else
3229 /* In sync mode, when execute_control_command returns
3230 we're already standing on the next breakpoint.
347bddb7
PA
3231 Breakpoint commands for that stop were not run, since
3232 execute_command does not run breakpoint commands --
3233 only command_line_handler does, but that one is not
3234 involved in execution of breakpoint commands. So, we
3235 can now execute breakpoint commands. It should be
3236 noted that making execute_command do bpstat actions is
3237 not an option -- in this case we'll have recursive
3238 invocation of bpstat for each breakpoint with a
3239 command, and can easily blow up GDB stack. Instead, we
3240 return true, which will trigger the caller to recall us
3241 with the new stop_bpstat. */
3242 again = 1;
3243 break;
32c1e744 3244 }
c906108c 3245 }
c2b8ed2c 3246 do_cleanups (old_chain);
347bddb7
PA
3247 return again;
3248}
3249
3250void
3251bpstat_do_actions (void)
3252{
3253 /* Do any commands attached to breakpoint we are stopped at. */
3254 while (!ptid_equal (inferior_ptid, null_ptid)
3255 && target_has_execution
3256 && !is_exited (inferior_ptid)
3257 && !is_executing (inferior_ptid))
3258 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3259 and only return when it is stopped at the next breakpoint, we
3260 keep doing breakpoint actions until it returns false to
3261 indicate the inferior was not resumed. */
16c381f0 3262 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3263 break;
c906108c
SS
3264}
3265
fa4727a6
DJ
3266/* Print out the (old or new) value associated with a watchpoint. */
3267
3268static void
3269watchpoint_value_print (struct value *val, struct ui_file *stream)
3270{
3271 if (val == NULL)
3272 fprintf_unfiltered (stream, _("<unreadable>"));
3273 else
79a45b7d
TT
3274 {
3275 struct value_print_options opts;
3276 get_user_print_options (&opts);
3277 value_print (val, stream, &opts);
3278 }
fa4727a6
DJ
3279}
3280
e514a9d6 3281/* This is the normal print function for a bpstat. In the future,
c906108c 3282 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3283 by having it set different print_it values.
3284
3285 Current scheme: When we stop, bpstat_print() is called. It loops
3286 through the bpstat list of things causing this stop, calling the
4a64f543 3287 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3288 print_bp_stop_message function depends on the print_it field of
4a64f543 3289 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3290
3291 Return values from this routine (ultimately used by bpstat_print()
3292 and normal_stop() to decide what to do):
3293 PRINT_NOTHING: Means we already printed all we needed to print,
3294 don't print anything else.
3295 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3296 that something to be followed by a location.
3297 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3298 that something to be followed by a location.
3299 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3300 analysis. */
c906108c 3301
917317f4 3302static enum print_stop_action
fba45db2 3303print_it_typical (bpstat bs)
c906108c 3304{
f7545552 3305 struct cleanup *old_chain;
4f8d1dc6 3306 struct breakpoint *b;
89f9893c 3307 const struct bp_location *bl;
8b93c638 3308 struct ui_stream *stb;
f7545552
TT
3309 int bp_temp = 0;
3310 enum print_stop_action result;
3311
c906108c
SS
3312 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3313 which has since been deleted. */
e514a9d6 3314 if (bs->breakpoint_at == NULL)
917317f4 3315 return PRINT_UNKNOWN;
2bdf28a0 3316
f431efe5
PA
3317 gdb_assert (bs->bp_location_at != NULL);
3318
3319 bl = bs->bp_location_at;
3320 b = bs->breakpoint_at;
c906108c 3321
f7545552
TT
3322 stb = ui_out_stream_new (uiout);
3323 old_chain = make_cleanup_ui_out_stream_delete (stb);
3324
4f8d1dc6 3325 switch (b->type)
c906108c 3326 {
e514a9d6
JM
3327 case bp_breakpoint:
3328 case bp_hardware_breakpoint:
f431efe5 3329 bp_temp = b->disposition == disp_del;
0d381245
VP
3330 if (bl->address != bl->requested_address)
3331 breakpoint_adjustment_warning (bl->requested_address,
3332 bl->address,
4f8d1dc6
VP
3333 b->number, 1);
3334 annotate_breakpoint (b->number);
2cec12e5
AR
3335 if (bp_temp)
3336 ui_out_text (uiout, "\nTemporary breakpoint ");
3337 else
3338 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3339 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3340 {
3341 ui_out_field_string (uiout, "reason",
3342 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3343 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3344 }
4f8d1dc6 3345 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3346 ui_out_text (uiout, ", ");
f7545552 3347 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3348 break;
3349
3350 case bp_shlib_event:
917317f4
JM
3351 /* Did we stop because the user set the stop_on_solib_events
3352 variable? (If so, we report this as a generic, "Stopped due
3353 to shlib event" message.) */
a3f17187 3354 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3355 result = PRINT_NOTHING;
e514a9d6
JM
3356 break;
3357
c4093a6a 3358 case bp_thread_event:
4a64f543 3359 /* Not sure how we will get here.
c4093a6a 3360 GDB should not stop for these breakpoints. */
a3f17187 3361 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3362 result = PRINT_NOTHING;
c4093a6a
JM
3363 break;
3364
1900040c 3365 case bp_overlay_event:
4a64f543 3366 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3367 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3368 result = PRINT_NOTHING;
1900040c
MS
3369 break;
3370
0fd8e87f
UW
3371 case bp_longjmp_master:
3372 /* These should never be enabled. */
3373 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3374 result = PRINT_NOTHING;
3375 break;
3376
aa7d318d
TT
3377 case bp_std_terminate_master:
3378 /* These should never be enabled. */
3e43a32a
MS
3379 printf_filtered (_("std::terminate Master Breakpoint: "
3380 "gdb should not stop!\n"));
aa7d318d
TT
3381 result = PRINT_NOTHING;
3382 break;
3383
186c406b
TT
3384 case bp_exception_master:
3385 /* These should never be enabled. */
3e43a32a
MS
3386 printf_filtered (_("Exception Master Breakpoint: "
3387 "gdb should not stop!\n"));
186c406b
TT
3388 result = PRINT_NOTHING;
3389 break;
3390
e514a9d6
JM
3391 case bp_watchpoint:
3392 case bp_hardware_watchpoint:
fa4727a6
DJ
3393 annotate_watchpoint (b->number);
3394 if (ui_out_is_mi_like_p (uiout))
3395 ui_out_field_string
3396 (uiout, "reason",
3397 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3398 mention (b);
f7545552 3399 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3400 ui_out_text (uiout, "\nOld value = ");
3401 watchpoint_value_print (bs->old_val, stb->stream);
3402 ui_out_field_stream (uiout, "old", stb);
3403 ui_out_text (uiout, "\nNew value = ");
3404 watchpoint_value_print (b->val, stb->stream);
3405 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3406 ui_out_text (uiout, "\n");
e514a9d6 3407 /* More than one watchpoint may have been triggered. */
f7545552 3408 result = PRINT_UNKNOWN;
e514a9d6
JM
3409 break;
3410
3411 case bp_read_watchpoint:
9dc5e2a9 3412 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3413 ui_out_field_string
3414 (uiout, "reason",
3415 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3416 mention (b);
f7545552 3417 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3418 ui_out_text (uiout, "\nValue = ");
fa4727a6 3419 watchpoint_value_print (b->val, stb->stream);
8b93c638 3420 ui_out_field_stream (uiout, "value", stb);
8b93c638 3421 ui_out_text (uiout, "\n");
f7545552 3422 result = PRINT_UNKNOWN;
e514a9d6
JM
3423 break;
3424
3425 case bp_access_watchpoint:
fa4727a6 3426 if (bs->old_val != NULL)
8b93c638 3427 {
4f8d1dc6 3428 annotate_watchpoint (b->number);
9dc5e2a9 3429 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3430 ui_out_field_string
3431 (uiout, "reason",
3432 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3433 mention (b);
f7545552 3434 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3435 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3436 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3437 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3438 ui_out_text (uiout, "\nNew value = ");
3439 }
3440 else
3441 {
4f8d1dc6 3442 mention (b);
9dc5e2a9 3443 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3444 ui_out_field_string
3445 (uiout, "reason",
3446 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3447 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3448 ui_out_text (uiout, "\nValue = ");
3449 }
fa4727a6 3450 watchpoint_value_print (b->val, stb->stream);
8b93c638 3451 ui_out_field_stream (uiout, "new", stb);
8b93c638 3452 ui_out_text (uiout, "\n");
f7545552 3453 result = PRINT_UNKNOWN;
e514a9d6 3454 break;
4ce44c66 3455
e514a9d6 3456 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3457 here. */
e514a9d6 3458
11cf8741 3459 case bp_finish:
9dc5e2a9 3460 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3461 ui_out_field_string
3462 (uiout, "reason",
3463 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3464 result = PRINT_UNKNOWN;
8b93c638
JM
3465 break;
3466
e514a9d6 3467 case bp_until:
9dc5e2a9 3468 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3469 ui_out_field_string
3470 (uiout, "reason",
3471 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3472 result = PRINT_UNKNOWN;
8b93c638
JM
3473 break;
3474
c2d11a7d 3475 case bp_none:
e514a9d6
JM
3476 case bp_longjmp:
3477 case bp_longjmp_resume:
186c406b
TT
3478 case bp_exception:
3479 case bp_exception_resume:
e514a9d6 3480 case bp_step_resume:
e514a9d6
JM
3481 case bp_watchpoint_scope:
3482 case bp_call_dummy:
aa7d318d 3483 case bp_std_terminate:
1042e4c0 3484 case bp_tracepoint:
7a697b8d 3485 case bp_fast_tracepoint:
4efc6507 3486 case bp_jit_event:
e514a9d6 3487 default:
f7545552
TT
3488 result = PRINT_UNKNOWN;
3489 break;
e514a9d6 3490 }
f7545552
TT
3491
3492 do_cleanups (old_chain);
3493 return result;
e514a9d6
JM
3494}
3495
3496/* Generic routine for printing messages indicating why we
4a64f543 3497 stopped. The behavior of this function depends on the value
e514a9d6
JM
3498 'print_it' in the bpstat structure. Under some circumstances we
3499 may decide not to print anything here and delegate the task to
4a64f543 3500 normal_stop(). */
e514a9d6
JM
3501
3502static enum print_stop_action
3503print_bp_stop_message (bpstat bs)
3504{
3505 switch (bs->print_it)
3506 {
3507 case print_it_noop:
4a64f543 3508 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3509 return PRINT_UNKNOWN;
3510 break;
3511
3512 case print_it_done:
3513 /* We still want to print the frame, but we already printed the
4a64f543 3514 relevant messages. */
e514a9d6
JM
3515 return PRINT_SRC_AND_LOC;
3516 break;
3517
3518 case print_it_normal:
4f8d1dc6 3519 {
f431efe5
PA
3520 struct breakpoint *b = bs->breakpoint_at;
3521
4f8d1dc6
VP
3522 /* Normal case. Call the breakpoint's print_it method, or
3523 print_it_typical. */
3524 /* FIXME: how breakpoint can ever be NULL here? */
3525 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
3526 return b->ops->print_it (b);
3527 else
3528 return print_it_typical (bs);
3529 }
3530 break;
3086aeae 3531
e514a9d6 3532 default:
8e65ff28 3533 internal_error (__FILE__, __LINE__,
e2e0b3e5 3534 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3535 break;
c906108c 3536 }
c906108c
SS
3537}
3538
e514a9d6
JM
3539/* Print a message indicating what happened. This is called from
3540 normal_stop(). The input to this routine is the head of the bpstat
3541 list - a list of the eventpoints that caused this stop. This
3542 routine calls the generic print routine for printing a message
3543 about reasons for stopping. This will print (for example) the
3544 "Breakpoint n," part of the output. The return value of this
3545 routine is one of:
c906108c 3546
4a64f543 3547 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3548 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3549 code to print the location. An example is
c5aa993b
JM
3550 "Breakpoint 1, " which should be followed by
3551 the location.
917317f4 3552 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3553 to also print the location part of the message.
3554 An example is the catch/throw messages, which
4a64f543 3555 don't require a location appended to the end.
917317f4 3556 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3557 further info to be printed. */
c906108c 3558
917317f4 3559enum print_stop_action
fba45db2 3560bpstat_print (bpstat bs)
c906108c
SS
3561{
3562 int val;
c5aa993b 3563
c906108c 3564 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3565 (Currently all watchpoints go on the bpstat whether hit or not.
3566 That probably could (should) be changed, provided care is taken
c906108c 3567 with respect to bpstat_explains_signal). */
e514a9d6
JM
3568 for (; bs; bs = bs->next)
3569 {
3570 val = print_bp_stop_message (bs);
3571 if (val == PRINT_SRC_ONLY
3572 || val == PRINT_SRC_AND_LOC
3573 || val == PRINT_NOTHING)
3574 return val;
3575 }
c906108c 3576
e514a9d6 3577 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3578 with and nothing was printed. */
917317f4 3579 return PRINT_UNKNOWN;
c906108c
SS
3580}
3581
4a64f543
MS
3582/* Evaluate the expression EXP and return 1 if value is zero. This is
3583 used inside a catch_errors to evaluate the breakpoint condition.
3584 The argument is a "struct expression *" that has been cast to a
3585 "char *" to make it pass through catch_errors. */
c906108c
SS
3586
3587static int
4efb68b1 3588breakpoint_cond_eval (void *exp)
c906108c 3589{
278cd55f 3590 struct value *mark = value_mark ();
c5aa993b 3591 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3592
c906108c
SS
3593 value_free_to_mark (mark);
3594 return i;
3595}
3596
5760d0ab 3597/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3598
3599static bpstat
5760d0ab 3600bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3601{
3602 bpstat bs;
3603
3604 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3605 bs->next = NULL;
3606 **bs_link_pointer = bs;
3607 *bs_link_pointer = &bs->next;
f431efe5
PA
3608 bs->breakpoint_at = bl->owner;
3609 bs->bp_location_at = bl;
3610 incref_bp_location (bl);
c906108c
SS
3611 /* If the condition is false, etc., don't do the commands. */
3612 bs->commands = NULL;
9add0f1b 3613 bs->commands_left = NULL;
c906108c
SS
3614 bs->old_val = NULL;
3615 bs->print_it = print_it_normal;
3616 return bs;
3617}
3618\f
d983da9c
DJ
3619/* The target has stopped with waitstatus WS. Check if any hardware
3620 watchpoints have triggered, according to the target. */
3621
3622int
3623watchpoints_triggered (struct target_waitstatus *ws)
3624{
d92524f1 3625 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3626 CORE_ADDR addr;
3627 struct breakpoint *b;
3628
3629 if (!stopped_by_watchpoint)
3630 {
3631 /* We were not stopped by a watchpoint. Mark all watchpoints
3632 as not triggered. */
3633 ALL_BREAKPOINTS (b)
cc60f2e3 3634 if (is_hardware_watchpoint (b))
d983da9c
DJ
3635 b->watchpoint_triggered = watch_triggered_no;
3636
3637 return 0;
3638 }
3639
3640 if (!target_stopped_data_address (&current_target, &addr))
3641 {
3642 /* We were stopped by a watchpoint, but we don't know where.
3643 Mark all watchpoints as unknown. */
3644 ALL_BREAKPOINTS (b)
cc60f2e3 3645 if (is_hardware_watchpoint (b))
d983da9c
DJ
3646 b->watchpoint_triggered = watch_triggered_unknown;
3647
3648 return stopped_by_watchpoint;
3649 }
3650
3651 /* The target could report the data address. Mark watchpoints
3652 affected by this data address as triggered, and all others as not
3653 triggered. */
3654
3655 ALL_BREAKPOINTS (b)
cc60f2e3 3656 if (is_hardware_watchpoint (b))
d983da9c 3657 {
a5606eee 3658 struct bp_location *loc;
d983da9c
DJ
3659
3660 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3661 for (loc = b->loc; loc; loc = loc->next)
3662 /* Exact match not required. Within range is
3663 sufficient. */
5009afc5
AS
3664 if (target_watchpoint_addr_within_range (&current_target,
3665 addr, loc->address,
3666 loc->length))
a5606eee
VP
3667 {
3668 b->watchpoint_triggered = watch_triggered_yes;
3669 break;
3670 }
d983da9c
DJ
3671 }
3672
3673 return 1;
3674}
3675
c906108c
SS
3676/* Possible return values for watchpoint_check (this can't be an enum
3677 because of check_errors). */
3678/* The watchpoint has been deleted. */
3679#define WP_DELETED 1
3680/* The value has changed. */
3681#define WP_VALUE_CHANGED 2
3682/* The value has not changed. */
3683#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3684/* Ignore this watchpoint, no matter if the value changed or not. */
3685#define WP_IGNORE 4
c906108c
SS
3686
3687#define BP_TEMPFLAG 1
3688#define BP_HARDWAREFLAG 2
3689
4a64f543
MS
3690/* Evaluate watchpoint condition expression and check if its value
3691 changed.
553e4c11
JB
3692
3693 P should be a pointer to struct bpstat, but is defined as a void *
3694 in order for this function to be usable with catch_errors. */
c906108c
SS
3695
3696static int
4efb68b1 3697watchpoint_check (void *p)
c906108c
SS
3698{
3699 bpstat bs = (bpstat) p;
3700 struct breakpoint *b;
3701 struct frame_info *fr;
3702 int within_current_scope;
3703
f431efe5 3704 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3705 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3706 b = bs->breakpoint_at;
c906108c 3707
f6bc2008
PA
3708 /* If this is a local watchpoint, we only want to check if the
3709 watchpoint frame is in scope if the current thread is the thread
3710 that was used to create the watchpoint. */
3711 if (!watchpoint_in_thread_scope (b))
60e1c644 3712 return WP_IGNORE;
f6bc2008 3713
c906108c
SS
3714 if (b->exp_valid_block == NULL)
3715 within_current_scope = 1;
3716 else
3717 {
edb3359d
DJ
3718 struct frame_info *frame = get_current_frame ();
3719 struct gdbarch *frame_arch = get_frame_arch (frame);
3720 CORE_ADDR frame_pc = get_frame_pc (frame);
3721
4a64f543
MS
3722 /* in_function_epilogue_p() returns a non-zero value if we're
3723 still in the function but the stack frame has already been
3724 invalidated. Since we can't rely on the values of local
3725 variables after the stack has been destroyed, we are treating
3726 the watchpoint in that state as `not changed' without further
3727 checking. Don't mark watchpoints as changed if the current
3728 frame is in an epilogue - even if they are in some other
3729 frame, our view of the stack is likely to be wrong and
3730 frame_find_by_id could error out. */
a0f49112 3731 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3732 return WP_IGNORE;
a0f49112 3733
101dcfbe 3734 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3735 within_current_scope = (fr != NULL);
69fbadd5
DJ
3736
3737 /* If we've gotten confused in the unwinder, we might have
3738 returned a frame that can't describe this variable. */
edb3359d
DJ
3739 if (within_current_scope)
3740 {
3741 struct symbol *function;
3742
3743 function = get_frame_function (fr);
3744 if (function == NULL
3745 || !contained_in (b->exp_valid_block,
3746 SYMBOL_BLOCK_VALUE (function)))
3747 within_current_scope = 0;
3748 }
69fbadd5 3749
edb3359d 3750 if (within_current_scope)
c906108c
SS
3751 /* If we end up stopping, the current frame will get selected
3752 in normal_stop. So this call to select_frame won't affect
3753 the user. */
0f7d239c 3754 select_frame (fr);
c906108c 3755 }
c5aa993b 3756
c906108c
SS
3757 if (within_current_scope)
3758 {
4a64f543
MS
3759 /* We use value_{,free_to_}mark because it could be a *long*
3760 time before we return to the command level and call
3761 free_all_values. We can't call free_all_values because we
3762 might be in the middle of evaluating a function call. */
c906108c 3763
0cf6dd15 3764 int pc = 0;
278cd55f 3765 struct value *mark = value_mark ();
fa4727a6
DJ
3766 struct value *new_val;
3767
0cf6dd15 3768 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3769
4a64f543
MS
3770 /* We use value_equal_contents instead of value_equal because
3771 the latter coerces an array to a pointer, thus comparing just
3772 the address of the array instead of its contents. This is
3773 not what we want. */
fa4727a6 3774 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3775 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3776 {
fa4727a6
DJ
3777 if (new_val != NULL)
3778 {
3779 release_value (new_val);
3780 value_free_to_mark (mark);
3781 }
c906108c
SS
3782 bs->old_val = b->val;
3783 b->val = new_val;
fa4727a6 3784 b->val_valid = 1;
c906108c
SS
3785 return WP_VALUE_CHANGED;
3786 }
3787 else
3788 {
60e1c644 3789 /* Nothing changed. */
c906108c 3790 value_free_to_mark (mark);
c906108c
SS
3791 return WP_VALUE_NOT_CHANGED;
3792 }
3793 }
3794 else
3795 {
3796 /* This seems like the only logical thing to do because
c5aa993b
JM
3797 if we temporarily ignored the watchpoint, then when
3798 we reenter the block in which it is valid it contains
3799 garbage (in the case of a function, it may have two
3800 garbage values, one before and one after the prologue).
3801 So we can't even detect the first assignment to it and
3802 watch after that (since the garbage may or may not equal
3803 the first value assigned). */
4ce44c66
JM
3804 /* We print all the stop information in print_it_typical(), but
3805 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3806 will be deleted already. So we have no choice but print the
3807 information here. */
9dc5e2a9 3808 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3809 ui_out_field_string
3810 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3811 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3812 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3813 ui_out_text (uiout,
3814 " deleted because the program has left the block in\n\
8b93c638 3815which its expression is valid.\n");
4ce44c66 3816
c906108c 3817 if (b->related_breakpoint)
60e1c644
PA
3818 {
3819 b->related_breakpoint->disposition = disp_del_at_next_stop;
3820 b->related_breakpoint->related_breakpoint = NULL;
3821 b->related_breakpoint = NULL;
3822 }
b5de0fa7 3823 b->disposition = disp_del_at_next_stop;
c906108c
SS
3824
3825 return WP_DELETED;
3826 }
3827}
3828
18a18393
VP
3829/* Return true if it looks like target has stopped due to hitting
3830 breakpoint location BL. This function does not check if we
3831 should stop, only if BL explains the stop. */
3832static int
6c95b8df
PA
3833bpstat_check_location (const struct bp_location *bl,
3834 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3835{
3836 struct breakpoint *b = bl->owner;
3837
2bdf28a0
JK
3838 /* BL is from existing struct breakpoint. */
3839 gdb_assert (b != NULL);
3840
e8595ef6
SS
3841 /* By definition, the inferior does not report stops at
3842 tracepoints. */
d77f58be 3843 if (is_tracepoint (b))
e8595ef6
SS
3844 return 0;
3845
cc60f2e3 3846 if (!is_watchpoint (b)
18a18393 3847 && b->type != bp_hardware_breakpoint
fe798b75 3848 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3849 {
6c95b8df
PA
3850 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3851 aspace, bp_addr))
18a18393
VP
3852 return 0;
3853 if (overlay_debugging /* unmapped overlay section */
35df4500 3854 && section_is_overlay (bl->section)
18a18393
VP
3855 && !section_is_mapped (bl->section))
3856 return 0;
3857 }
cc60f2e3 3858
18a18393
VP
3859 /* Continuable hardware watchpoints are treated as non-existent if the
3860 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3861 some data address). Otherwise gdb won't stop on a break instruction
3862 in the code (not from a breakpoint) when a hardware watchpoint has
3863 been defined. Also skip watchpoints which we know did not trigger
3864 (did not match the data address). */
cc60f2e3
PA
3865
3866 if (is_hardware_watchpoint (b)
18a18393
VP
3867 && b->watchpoint_triggered == watch_triggered_no)
3868 return 0;
3869
3870 if (b->type == bp_hardware_breakpoint)
3871 {
3872 if (bl->address != bp_addr)
3873 return 0;
3874 if (overlay_debugging /* unmapped overlay section */
35df4500 3875 && section_is_overlay (bl->section)
18a18393
VP
3876 && !section_is_mapped (bl->section))
3877 return 0;
3878 }
ce78b96d 3879
ce78b96d
JB
3880 if (b->type == bp_catchpoint)
3881 {
3882 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3883 if (!b->ops->breakpoint_hit (b))
3884 return 0;
3885 }
3886
18a18393
VP
3887 return 1;
3888}
3889
3890/* If BS refers to a watchpoint, determine if the watched values
3891 has actually changed, and we should stop. If not, set BS->stop
3892 to 0. */
3893static void
3894bpstat_check_watchpoint (bpstat bs)
3895{
2bdf28a0
JK
3896 const struct bp_location *bl;
3897 struct breakpoint *b;
3898
3899 /* BS is built for existing struct breakpoint. */
f431efe5 3900 bl = bs->bp_location_at;
2bdf28a0 3901 gdb_assert (bl != NULL);
f431efe5 3902 b = bs->breakpoint_at;
2bdf28a0 3903 gdb_assert (b != NULL);
18a18393 3904
cc60f2e3 3905 if (is_watchpoint (b))
18a18393 3906 {
18a18393
VP
3907 int must_check_value = 0;
3908
3909 if (b->type == bp_watchpoint)
3910 /* For a software watchpoint, we must always check the
3911 watched value. */
3912 must_check_value = 1;
3913 else if (b->watchpoint_triggered == watch_triggered_yes)
3914 /* We have a hardware watchpoint (read, write, or access)
3915 and the target earlier reported an address watched by
3916 this watchpoint. */
3917 must_check_value = 1;
3918 else if (b->watchpoint_triggered == watch_triggered_unknown
3919 && b->type == bp_hardware_watchpoint)
3920 /* We were stopped by a hardware watchpoint, but the target could
3921 not report the data address. We must check the watchpoint's
3922 value. Access and read watchpoints are out of luck; without
3923 a data address, we can't figure it out. */
3924 must_check_value = 1;
3925
3926 if (must_check_value)
3927 {
3e43a32a
MS
3928 char *message
3929 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3930 b->number);
18a18393
VP
3931 struct cleanup *cleanups = make_cleanup (xfree, message);
3932 int e = catch_errors (watchpoint_check, bs, message,
3933 RETURN_MASK_ALL);
3934 do_cleanups (cleanups);
3935 switch (e)
3936 {
3937 case WP_DELETED:
3938 /* We've already printed what needs to be printed. */
3939 bs->print_it = print_it_done;
3940 /* Stop. */
3941 break;
60e1c644
PA
3942 case WP_IGNORE:
3943 bs->print_it = print_it_noop;
3944 bs->stop = 0;
3945 break;
18a18393
VP
3946 case WP_VALUE_CHANGED:
3947 if (b->type == bp_read_watchpoint)
3948 {
85d721b8
PA
3949 /* There are two cases to consider here:
3950
4a64f543 3951 1. We're watching the triggered memory for reads.
85d721b8
PA
3952 In that case, trust the target, and always report
3953 the watchpoint hit to the user. Even though
3954 reads don't cause value changes, the value may
3955 have changed since the last time it was read, and
3956 since we're not trapping writes, we will not see
3957 those, and as such we should ignore our notion of
3958 old value.
3959
4a64f543 3960 2. We're watching the triggered memory for both
85d721b8
PA
3961 reads and writes. There are two ways this may
3962 happen:
3963
4a64f543 3964 2.1. This is a target that can't break on data
85d721b8
PA
3965 reads only, but can break on accesses (reads or
3966 writes), such as e.g., x86. We detect this case
3967 at the time we try to insert read watchpoints.
3968
4a64f543 3969 2.2. Otherwise, the target supports read
85d721b8
PA
3970 watchpoints, but, the user set an access or write
3971 watchpoint watching the same memory as this read
3972 watchpoint.
3973
3974 If we're watching memory writes as well as reads,
3975 ignore watchpoint hits when we find that the
3976 value hasn't changed, as reads don't cause
3977 changes. This still gives false positives when
3978 the program writes the same value to memory as
3979 what there was already in memory (we will confuse
3980 it for a read), but it's much better than
3981 nothing. */
3982
3983 int other_write_watchpoint = 0;
3984
3985 if (bl->watchpoint_type == hw_read)
3986 {
3987 struct breakpoint *other_b;
3988
3989 ALL_BREAKPOINTS (other_b)
3990 if ((other_b->type == bp_hardware_watchpoint
3991 || other_b->type == bp_access_watchpoint)
3992 && (other_b->watchpoint_triggered
3993 == watch_triggered_yes))
3994 {
3995 other_write_watchpoint = 1;
3996 break;
3997 }
3998 }
3999
4000 if (other_write_watchpoint
4001 || bl->watchpoint_type == hw_access)
4002 {
4003 /* We're watching the same memory for writes,
4004 and the value changed since the last time we
4005 updated it, so this trap must be for a write.
4006 Ignore it. */
4007 bs->print_it = print_it_noop;
4008 bs->stop = 0;
4009 }
18a18393
VP
4010 }
4011 break;
4012 case WP_VALUE_NOT_CHANGED:
4013 if (b->type == bp_hardware_watchpoint
4014 || b->type == bp_watchpoint)
4015 {
4016 /* Don't stop: write watchpoints shouldn't fire if
4017 the value hasn't changed. */
4018 bs->print_it = print_it_noop;
4019 bs->stop = 0;
4020 }
4021 /* Stop. */
4022 break;
4023 default:
4024 /* Can't happen. */
4025 case 0:
4026 /* Error from catch_errors. */
4027 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
4028 if (b->related_breakpoint)
4029 b->related_breakpoint->disposition = disp_del_at_next_stop;
4030 b->disposition = disp_del_at_next_stop;
4031 /* We've already printed what needs to be printed. */
4032 bs->print_it = print_it_done;
4033 break;
4034 }
4035 }
4036 else /* must_check_value == 0 */
4037 {
4038 /* This is a case where some watchpoint(s) triggered, but
4039 not at the address of this watchpoint, or else no
4040 watchpoint triggered after all. So don't print
4041 anything for this watchpoint. */
4042 bs->print_it = print_it_noop;
4043 bs->stop = 0;
4044 }
4045 }
4046}
4047
4048
4049/* Check conditions (condition proper, frame, thread and ignore count)
4050 of breakpoint referred to by BS. If we should not stop for this
4051 breakpoint, set BS->stop to 0. */
f431efe5 4052
18a18393
VP
4053static void
4054bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4055{
4056 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4057 const struct bp_location *bl;
4058 struct breakpoint *b;
4059
4060 /* BS is built for existing struct breakpoint. */
f431efe5 4061 bl = bs->bp_location_at;
2bdf28a0 4062 gdb_assert (bl != NULL);
f431efe5 4063 b = bs->breakpoint_at;
2bdf28a0 4064 gdb_assert (b != NULL);
18a18393
VP
4065
4066 if (frame_id_p (b->frame_id)
edb3359d 4067 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4068 bs->stop = 0;
4069 else if (bs->stop)
4070 {
4071 int value_is_zero = 0;
60e1c644
PA
4072 struct expression *cond;
4073
60e1c644
PA
4074 if (is_watchpoint (b))
4075 cond = b->cond_exp;
4076 else
4077 cond = bl->cond;
4078
f431efe5 4079 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4080 {
60e1c644
PA
4081 int within_current_scope = 1;
4082
c5bc3a77
DJ
4083 /* We use value_mark and value_free_to_mark because it could
4084 be a long time before we return to the command level and
4085 call free_all_values. We can't call free_all_values
4086 because we might be in the middle of evaluating a
4087 function call. */
4088 struct value *mark = value_mark ();
4089
edb3359d
DJ
4090 /* Need to select the frame, with all that implies so that
4091 the conditions will have the right context. Because we
4092 use the frame, we will not see an inlined function's
4093 variables when we arrive at a breakpoint at the start
4094 of the inlined function; the current frame will be the
4095 call site. */
60e1c644
PA
4096 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4097 select_frame (get_current_frame ());
4098 else
4099 {
4100 struct frame_info *frame;
4101
4102 /* For local watchpoint expressions, which particular
4103 instance of a local is being watched matters, so we
4104 keep track of the frame to evaluate the expression
4105 in. To evaluate the condition however, it doesn't
4106 really matter which instantiation of the function
4107 where the condition makes sense triggers the
4108 watchpoint. This allows an expression like "watch
4109 global if q > 10" set in `func', catch writes to
4110 global on all threads that call `func', or catch
4111 writes on all recursive calls of `func' by a single
4112 thread. We simply always evaluate the condition in
4113 the innermost frame that's executing where it makes
4114 sense to evaluate the condition. It seems
4115 intuitive. */
4116 frame = block_innermost_frame (b->cond_exp_valid_block);
4117 if (frame != NULL)
4118 select_frame (frame);
4119 else
4120 within_current_scope = 0;
4121 }
4122 if (within_current_scope)
4123 value_is_zero
4124 = catch_errors (breakpoint_cond_eval, cond,
4125 "Error in testing breakpoint condition:\n",
4126 RETURN_MASK_ALL);
4127 else
4128 {
4129 warning (_("Watchpoint condition cannot be tested "
4130 "in the current scope"));
4131 /* If we failed to set the right context for this
4132 watchpoint, unconditionally report it. */
4133 value_is_zero = 0;
4134 }
4a64f543 4135 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4136 value_free_to_mark (mark);
18a18393 4137 }
60e1c644
PA
4138
4139 if (cond && value_is_zero)
18a18393
VP
4140 {
4141 bs->stop = 0;
4142 }
4143 else if (b->thread != -1 && b->thread != thread_id)
4144 {
4145 bs->stop = 0;
4146 }
4147 else if (b->ignore_count > 0)
4148 {
4149 b->ignore_count--;
4150 annotate_ignore_count_change ();
4151 bs->stop = 0;
4a64f543 4152 /* Increase the hit count even though we don't stop. */
18a18393
VP
4153 ++(b->hit_count);
4154 }
4155 }
4156}
4157
4158
9709f61c 4159/* Get a bpstat associated with having just stopped at address
d983da9c 4160 BP_ADDR in thread PTID.
c906108c 4161
d983da9c 4162 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4163 don't understand this stop. Result is a chain of bpstat's such
4164 that:
c906108c 4165
c5aa993b 4166 if we don't understand the stop, the result is a null pointer.
c906108c 4167
c5aa993b 4168 if we understand why we stopped, the result is not null.
c906108c 4169
c5aa993b
JM
4170 Each element of the chain refers to a particular breakpoint or
4171 watchpoint at which we have stopped. (We may have stopped for
4172 several reasons concurrently.)
c906108c 4173
c5aa993b
JM
4174 Each element of the chain has valid next, breakpoint_at,
4175 commands, FIXME??? fields. */
c906108c
SS
4176
4177bpstat
6c95b8df
PA
4178bpstat_stop_status (struct address_space *aspace,
4179 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4180{
0d381245 4181 struct breakpoint *b = NULL;
afe38095 4182 struct bp_location *bl;
20874c92 4183 struct bp_location *loc;
5760d0ab
JK
4184 /* First item of allocated bpstat's. */
4185 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4186 /* Pointer to the last thing in the chain currently. */
5760d0ab 4187 bpstat bs;
20874c92 4188 int ix;
429374b8 4189 int need_remove_insert;
f431efe5 4190 int removed_any;
c906108c 4191
f431efe5
PA
4192 /* First, build the bpstat chain with locations that explain a
4193 target stop, while being careful to not set the target running,
4194 as that may invalidate locations (in particular watchpoint
4195 locations are recreated). Resuming will happen here with
4196 breakpoint conditions or watchpoint expressions that include
4197 inferior function calls. */
c5aa993b 4198
429374b8
JK
4199 ALL_BREAKPOINTS (b)
4200 {
4201 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4202 continue;
a5606eee 4203
429374b8
JK
4204 for (bl = b->loc; bl != NULL; bl = bl->next)
4205 {
4a64f543
MS
4206 /* For hardware watchpoints, we look only at the first
4207 location. The watchpoint_check function will work on the
4208 entire expression, not the individual locations. For
4209 read watchpoints, the watchpoints_triggered function has
4210 checked all locations already. */
429374b8
JK
4211 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4212 break;
18a18393 4213
429374b8
JK
4214 if (bl->shlib_disabled)
4215 continue;
c5aa993b 4216
429374b8
JK
4217 if (!bpstat_check_location (bl, aspace, bp_addr))
4218 continue;
c5aa993b 4219
4a64f543
MS
4220 /* Come here if it's a watchpoint, or if the break address
4221 matches. */
c5aa993b 4222
4a64f543
MS
4223 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4224 explain stop. */
c5aa993b 4225
f431efe5
PA
4226 /* Assume we stop. Should we find a watchpoint that is not
4227 actually triggered, or if the condition of the breakpoint
4228 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4229 bs->stop = 1;
4230 bs->print = 1;
d983da9c 4231
f431efe5
PA
4232 /* If this is a scope breakpoint, mark the associated
4233 watchpoint as triggered so that we will handle the
4234 out-of-scope event. We'll get to the watchpoint next
4235 iteration. */
4236 if (b->type == bp_watchpoint_scope)
4237 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4238 }
4239 }
4240
4241 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4242 {
4243 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4244 aspace, bp_addr))
4245 {
5760d0ab 4246 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4247 /* For hits of moribund locations, we should just proceed. */
4248 bs->stop = 0;
4249 bs->print = 0;
4250 bs->print_it = print_it_noop;
4251 }
4252 }
4253
f431efe5
PA
4254 /* Now go through the locations that caused the target to stop, and
4255 check whether we're interested in reporting this stop to higher
4256 layers, or whether we should resume the target transparently. */
4257
4258 removed_any = 0;
4259
5760d0ab 4260 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4261 {
4262 if (!bs->stop)
4263 continue;
4264
4265 bpstat_check_watchpoint (bs);
4266 if (!bs->stop)
4267 continue;
4268
4269 b = bs->breakpoint_at;
18a18393 4270
429374b8 4271 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4272 || b->type == bp_longjmp_master
186c406b
TT
4273 || b->type == bp_std_terminate_master
4274 || b->type == bp_exception_master)
429374b8
JK
4275 /* We do not stop for these. */
4276 bs->stop = 0;
4277 else
4278 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4279
429374b8
JK
4280 if (bs->stop)
4281 {
4282 ++(b->hit_count);
c906108c 4283
4a64f543 4284 /* We will stop here. */
429374b8
JK
4285 if (b->disposition == disp_disable)
4286 {
4287 if (b->enable_state != bp_permanent)
4288 b->enable_state = bp_disabled;
f431efe5 4289 removed_any = 1;
429374b8
JK
4290 }
4291 if (b->silent)
4292 bs->print = 0;
4293 bs->commands = b->commands;
9add0f1b
TT
4294 incref_counted_command_line (bs->commands);
4295 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4296 if (bs->commands_left
4297 && (strcmp ("silent", bs->commands_left->line) == 0
4298 || (xdb_commands
4299 && strcmp ("Q",
4300 bs->commands_left->line) == 0)))
429374b8 4301 {
9add0f1b 4302 bs->commands_left = bs->commands_left->next;
429374b8
JK
4303 bs->print = 0;
4304 }
429374b8
JK
4305 }
4306
4307 /* Print nothing for this entry if we dont stop or dont print. */
4308 if (bs->stop == 0 || bs->print == 0)
4309 bs->print_it = print_it_noop;
429374b8 4310 }
876fa593 4311
d983da9c
DJ
4312 /* If we aren't stopping, the value of some hardware watchpoint may
4313 not have changed, but the intermediate memory locations we are
4314 watching may have. Don't bother if we're stopping; this will get
4315 done later. */
d832cb68 4316 need_remove_insert = 0;
5760d0ab
JK
4317 if (! bpstat_causes_stop (bs_head))
4318 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4319 if (!bs->stop
f431efe5
PA
4320 && bs->breakpoint_at
4321 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4322 {
4a64f543 4323 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4324 need_remove_insert = 1;
d983da9c
DJ
4325 }
4326
d832cb68 4327 if (need_remove_insert)
2d134ed3 4328 update_global_location_list (1);
f431efe5
PA
4329 else if (removed_any)
4330 update_global_location_list (0);
d832cb68 4331
5760d0ab 4332 return bs_head;
c906108c 4333}
628fe4e4
JK
4334
4335static void
4336handle_jit_event (void)
4337{
4338 struct frame_info *frame;
4339 struct gdbarch *gdbarch;
4340
4341 /* Switch terminal for any messages produced by
4342 breakpoint_re_set. */
4343 target_terminal_ours_for_output ();
4344
4345 frame = get_current_frame ();
4346 gdbarch = get_frame_arch (frame);
4347
4348 jit_event_handler (gdbarch);
4349
4350 target_terminal_inferior ();
4351}
4352
4353/* Prepare WHAT final decision for infrun. */
4354
4355/* Decide what infrun needs to do with this bpstat. */
4356
c906108c 4357struct bpstat_what
fba45db2 4358bpstat_what (bpstat bs)
c906108c 4359{
c906108c 4360 struct bpstat_what retval;
628fe4e4
JK
4361 /* We need to defer calling `solib_add', as adding new symbols
4362 resets breakpoints, which in turn deletes breakpoint locations,
4363 and hence may clear unprocessed entries in the BS chain. */
4364 int shlib_event = 0;
4365 int jit_event = 0;
c906108c 4366
628fe4e4 4367 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4368 retval.call_dummy = STOP_NONE;
186c406b 4369 retval.is_longjmp = 0;
628fe4e4 4370
c906108c
SS
4371 for (; bs != NULL; bs = bs->next)
4372 {
628fe4e4
JK
4373 /* Extract this BS's action. After processing each BS, we check
4374 if its action overrides all we've seem so far. */
4375 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4376 enum bptype bptype;
4377
c906108c 4378 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4379 {
4380 /* I suspect this can happen if it was a momentary
4381 breakpoint which has since been deleted. */
4382 bptype = bp_none;
4383 }
f431efe5 4384 else if (bs->breakpoint_at == NULL)
628fe4e4 4385 bptype = bp_none;
20874c92 4386 else
f431efe5 4387 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4388
4389 switch (bptype)
c906108c
SS
4390 {
4391 case bp_none:
628fe4e4 4392 break;
c906108c
SS
4393 case bp_breakpoint:
4394 case bp_hardware_breakpoint:
4395 case bp_until:
4396 case bp_finish:
4397 if (bs->stop)
4398 {
4399 if (bs->print)
628fe4e4 4400 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4401 else
628fe4e4 4402 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4403 }
4404 else
628fe4e4 4405 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4406 break;
4407 case bp_watchpoint:
4408 case bp_hardware_watchpoint:
4409 case bp_read_watchpoint:
4410 case bp_access_watchpoint:
4411 if (bs->stop)
4412 {
4413 if (bs->print)
628fe4e4 4414 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4415 else
628fe4e4 4416 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4417 }
4418 else
628fe4e4
JK
4419 {
4420 /* There was a watchpoint, but we're not stopping.
4421 This requires no further action. */
4422 }
c906108c
SS
4423 break;
4424 case bp_longjmp:
186c406b 4425 case bp_exception:
628fe4e4 4426 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4427 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4428 break;
4429 case bp_longjmp_resume:
186c406b 4430 case bp_exception_resume:
628fe4e4 4431 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4432 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4433 break;
4434 case bp_step_resume:
4435 if (bs->stop)
628fe4e4
JK
4436 this_action = BPSTAT_WHAT_STEP_RESUME;
4437 else
c906108c 4438 {
628fe4e4
JK
4439 /* It is for the wrong frame. */
4440 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4441 }
c906108c 4442 break;
c906108c 4443 case bp_watchpoint_scope:
c4093a6a 4444 case bp_thread_event:
1900040c 4445 case bp_overlay_event:
0fd8e87f 4446 case bp_longjmp_master:
aa7d318d 4447 case bp_std_terminate_master:
186c406b 4448 case bp_exception_master:
628fe4e4 4449 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4450 break;
ce78b96d 4451 case bp_catchpoint:
c5aa993b
JM
4452 if (bs->stop)
4453 {
4454 if (bs->print)
628fe4e4 4455 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4456 else
628fe4e4 4457 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4458 }
4459 else
628fe4e4
JK
4460 {
4461 /* There was a catchpoint, but we're not stopping.
4462 This requires no further action. */
4463 }
4464 break;
4465 case bp_shlib_event:
4466 shlib_event = 1;
4467
4468 /* If requested, stop when the dynamic linker notifies GDB
4469 of events. This allows the user to get control and place
4470 breakpoints in initializer routines for dynamically
4471 loaded objects (among other things). */
4472 if (stop_on_solib_events)
4473 this_action = BPSTAT_WHAT_STOP_NOISY;
4474 else
4475 this_action = BPSTAT_WHAT_SINGLE;
4476 break;
4477 case bp_jit_event:
4478 jit_event = 1;
4479 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4480 break;
c906108c 4481 case bp_call_dummy:
53a5351d
JM
4482 /* Make sure the action is stop (silent or noisy),
4483 so infrun.c pops the dummy frame. */
aa7d318d 4484 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4485 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4486 break;
4487 case bp_std_terminate:
4488 /* Make sure the action is stop (silent or noisy),
4489 so infrun.c pops the dummy frame. */
aa7d318d 4490 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4491 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4492 break;
1042e4c0 4493 case bp_tracepoint:
7a697b8d 4494 case bp_fast_tracepoint:
0fb4aa4b 4495 case bp_static_tracepoint:
1042e4c0
SS
4496 /* Tracepoint hits should not be reported back to GDB, and
4497 if one got through somehow, it should have been filtered
4498 out already. */
4499 internal_error (__FILE__, __LINE__,
7a697b8d 4500 _("bpstat_what: tracepoint encountered"));
628fe4e4
JK
4501 default:
4502 internal_error (__FILE__, __LINE__,
4503 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4504 }
628fe4e4
JK
4505
4506 retval.main_action = max (retval.main_action, this_action);
c906108c 4507 }
628fe4e4
JK
4508
4509 if (shlib_event)
4510 {
4511 if (debug_infrun)
4512 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4513
4514 /* Check for any newly added shared libraries if we're supposed
4515 to be adding them automatically. */
4516
4517 /* Switch terminal for any messages produced by
4518 breakpoint_re_set. */
4519 target_terminal_ours_for_output ();
4520
4521#ifdef SOLIB_ADD
4522 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4523#else
4524 solib_add (NULL, 0, &current_target, auto_solib_add);
4525#endif
4526
4527 target_terminal_inferior ();
4528 }
4529
4530 if (jit_event)
4531 {
4532 if (debug_infrun)
4533 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4534
4535 handle_jit_event ();
4536 }
4537
c906108c
SS
4538 return retval;
4539}
4540
4541/* Nonzero if we should step constantly (e.g. watchpoints on machines
4542 without hardware support). This isn't related to a specific bpstat,
4543 just to things like whether watchpoints are set. */
4544
c5aa993b 4545int
fba45db2 4546bpstat_should_step (void)
c906108c
SS
4547{
4548 struct breakpoint *b;
cc59ec59 4549
c906108c 4550 ALL_BREAKPOINTS (b)
717a8278 4551 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4552 return 1;
c906108c
SS
4553 return 0;
4554}
4555
67822962
PA
4556int
4557bpstat_causes_stop (bpstat bs)
4558{
4559 for (; bs != NULL; bs = bs->next)
4560 if (bs->stop)
4561 return 1;
4562
4563 return 0;
4564}
4565
c906108c 4566\f
c5aa993b 4567
170b53b2
UW
4568/* Compute a string of spaces suitable to indent the next line
4569 so it starts at the position corresponding to the table column
4570 named COL_NAME in the currently active table of UIOUT. */
4571
4572static char *
4573wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4574{
4575 static char wrap_indent[80];
4576 int i, total_width, width, align;
4577 char *text;
4578
4579 total_width = 0;
4580 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4581 {
4582 if (strcmp (text, col_name) == 0)
4583 {
4584 gdb_assert (total_width < sizeof wrap_indent);
4585 memset (wrap_indent, ' ', total_width);
4586 wrap_indent[total_width] = 0;
4587
4588 return wrap_indent;
4589 }
4590
4591 total_width += width + 1;
4592 }
4593
4594 return NULL;
4595}
4596
859825b8
JK
4597/* Print the LOC location out of the list of B->LOC locations. */
4598
170b53b2
UW
4599static void
4600print_breakpoint_location (struct breakpoint *b,
4601 struct bp_location *loc)
0d381245 4602{
6c95b8df
PA
4603 struct cleanup *old_chain = save_current_program_space ();
4604
859825b8
JK
4605 if (loc != NULL && loc->shlib_disabled)
4606 loc = NULL;
4607
6c95b8df
PA
4608 if (loc != NULL)
4609 set_current_program_space (loc->pspace);
4610
859825b8 4611 if (b->source_file && loc)
0d381245
VP
4612 {
4613 struct symbol *sym
4614 = find_pc_sect_function (loc->address, loc->section);
4615 if (sym)
4616 {
4617 ui_out_text (uiout, "in ");
4618 ui_out_field_string (uiout, "func",
4619 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4620 ui_out_text (uiout, " ");
4621 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4622 ui_out_text (uiout, "at ");
0d381245
VP
4623 }
4624 ui_out_field_string (uiout, "file", b->source_file);
4625 ui_out_text (uiout, ":");
4626
4627 if (ui_out_is_mi_like_p (uiout))
4628 {
4629 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4630 char *fullname = symtab_to_fullname (sal.symtab);
4631
4632 if (fullname)
4633 ui_out_field_string (uiout, "fullname", fullname);
4634 }
4635
4636 ui_out_field_int (uiout, "line", b->line_number);
4637 }
859825b8 4638 else if (loc)
0d381245 4639 {
170b53b2
UW
4640 struct ui_stream *stb = ui_out_stream_new (uiout);
4641 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4642
22e722e1
DJ
4643 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4644 demangle, "");
0d381245 4645 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4646
4647 do_cleanups (stb_chain);
0d381245 4648 }
859825b8
JK
4649 else
4650 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4651
4652 do_cleanups (old_chain);
0d381245
VP
4653}
4654
269b11a2
PA
4655static const char *
4656bptype_string (enum bptype type)
c906108c 4657{
c4093a6a
JM
4658 struct ep_type_description
4659 {
4660 enum bptype type;
4661 char *description;
4662 };
4663 static struct ep_type_description bptypes[] =
c906108c 4664 {
c5aa993b
JM
4665 {bp_none, "?deleted?"},
4666 {bp_breakpoint, "breakpoint"},
c906108c 4667 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4668 {bp_until, "until"},
4669 {bp_finish, "finish"},
4670 {bp_watchpoint, "watchpoint"},
c906108c 4671 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4672 {bp_read_watchpoint, "read watchpoint"},
4673 {bp_access_watchpoint, "acc watchpoint"},
4674 {bp_longjmp, "longjmp"},
4675 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4676 {bp_exception, "exception"},
4677 {bp_exception_resume, "exception resume"},
c5aa993b 4678 {bp_step_resume, "step resume"},
c5aa993b
JM
4679 {bp_watchpoint_scope, "watchpoint scope"},
4680 {bp_call_dummy, "call dummy"},
aa7d318d 4681 {bp_std_terminate, "std::terminate"},
c5aa993b 4682 {bp_shlib_event, "shlib events"},
c4093a6a 4683 {bp_thread_event, "thread events"},
1900040c 4684 {bp_overlay_event, "overlay events"},
0fd8e87f 4685 {bp_longjmp_master, "longjmp master"},
aa7d318d 4686 {bp_std_terminate_master, "std::terminate master"},
186c406b 4687 {bp_exception_master, "exception master"},
ce78b96d 4688 {bp_catchpoint, "catchpoint"},
1042e4c0 4689 {bp_tracepoint, "tracepoint"},
7a697b8d 4690 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4691 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4692 {bp_jit_event, "jit events"},
c5aa993b 4693 };
269b11a2
PA
4694
4695 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4696 || ((int) type != bptypes[(int) type].type))
4697 internal_error (__FILE__, __LINE__,
4698 _("bptypes table does not describe type #%d."),
4699 (int) type);
4700
4701 return bptypes[(int) type].description;
4702}
4703
4704/* Print B to gdb_stdout. */
4705
4706static void
4707print_one_breakpoint_location (struct breakpoint *b,
4708 struct bp_location *loc,
4709 int loc_number,
4710 struct bp_location **last_loc,
269b11a2
PA
4711 int allflag)
4712{
4713 struct command_line *l;
c2c6d25f 4714 static char bpenables[] = "nynny";
3b31d625 4715 struct cleanup *bkpt_chain;
c906108c 4716
0d381245
VP
4717 int header_of_multiple = 0;
4718 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4719 struct value_print_options opts;
4720
4721 get_user_print_options (&opts);
0d381245
VP
4722
4723 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4724 /* See comment in print_one_breakpoint concerning treatment of
4725 breakpoints with single disabled location. */
0d381245
VP
4726 if (loc == NULL
4727 && (b->loc != NULL
4728 && (b->loc->next != NULL || !b->loc->enabled)))
4729 header_of_multiple = 1;
4730 if (loc == NULL)
4731 loc = b->loc;
4732
c4093a6a 4733 annotate_record ();
3b31d625 4734 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4735
4736 /* 1 */
4737 annotate_field (0);
0d381245
VP
4738 if (part_of_multiple)
4739 {
4740 char *formatted;
0c6773c1 4741 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4742 ui_out_field_string (uiout, "number", formatted);
4743 xfree (formatted);
4744 }
4745 else
4746 {
4747 ui_out_field_int (uiout, "number", b->number);
4748 }
c4093a6a
JM
4749
4750 /* 2 */
4751 annotate_field (1);
0d381245
VP
4752 if (part_of_multiple)
4753 ui_out_field_skip (uiout, "type");
269b11a2
PA
4754 else
4755 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4756
4757 /* 3 */
4758 annotate_field (2);
0d381245
VP
4759 if (part_of_multiple)
4760 ui_out_field_skip (uiout, "disp");
4761 else
2cec12e5 4762 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4763
c4093a6a
JM
4764
4765 /* 4 */
4766 annotate_field (3);
0d381245 4767 if (part_of_multiple)
54e52265 4768 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4769 else
4a64f543
MS
4770 ui_out_field_fmt (uiout, "enabled", "%c",
4771 bpenables[(int) b->enable_state]);
54e52265 4772 ui_out_spaces (uiout, 2);
0d381245 4773
c4093a6a
JM
4774
4775 /* 5 and 6 */
3086aeae 4776 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4777 {
4a64f543
MS
4778 /* Although the print_one can possibly print all locations,
4779 calling it here is not likely to get any nice result. So,
4780 make sure there's just one location. */
0d381245 4781 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4782 b->ops->print_one (b, last_loc);
0d381245 4783 }
3086aeae
DJ
4784 else
4785 switch (b->type)
4786 {
4787 case bp_none:
4788 internal_error (__FILE__, __LINE__,
e2e0b3e5 4789 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4790 break;
c906108c 4791
3086aeae
DJ
4792 case bp_watchpoint:
4793 case bp_hardware_watchpoint:
4794 case bp_read_watchpoint:
4795 case bp_access_watchpoint:
4796 /* Field 4, the address, is omitted (which makes the columns
4797 not line up too nicely with the headers, but the effect
4798 is relatively readable). */
79a45b7d 4799 if (opts.addressprint)
3086aeae
DJ
4800 ui_out_field_skip (uiout, "addr");
4801 annotate_field (5);
fa8a61dc 4802 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4803 break;
4804
3086aeae
DJ
4805 case bp_breakpoint:
4806 case bp_hardware_breakpoint:
4807 case bp_until:
4808 case bp_finish:
4809 case bp_longjmp:
4810 case bp_longjmp_resume:
186c406b
TT
4811 case bp_exception:
4812 case bp_exception_resume:
3086aeae 4813 case bp_step_resume:
3086aeae
DJ
4814 case bp_watchpoint_scope:
4815 case bp_call_dummy:
aa7d318d 4816 case bp_std_terminate:
3086aeae
DJ
4817 case bp_shlib_event:
4818 case bp_thread_event:
4819 case bp_overlay_event:
0fd8e87f 4820 case bp_longjmp_master:
aa7d318d 4821 case bp_std_terminate_master:
186c406b 4822 case bp_exception_master:
1042e4c0 4823 case bp_tracepoint:
7a697b8d 4824 case bp_fast_tracepoint:
0fb4aa4b 4825 case bp_static_tracepoint:
4efc6507 4826 case bp_jit_event:
79a45b7d 4827 if (opts.addressprint)
3086aeae
DJ
4828 {
4829 annotate_field (4);
54e52265 4830 if (header_of_multiple)
0d381245 4831 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4832 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4833 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4834 else
5af949e3
UW
4835 ui_out_field_core_addr (uiout, "addr",
4836 loc->gdbarch, loc->address);
3086aeae
DJ
4837 }
4838 annotate_field (5);
0d381245 4839 if (!header_of_multiple)
170b53b2 4840 print_breakpoint_location (b, loc);
0d381245 4841 if (b->loc)
a6d9a66e 4842 *last_loc = b->loc;
3086aeae
DJ
4843 break;
4844 }
c906108c 4845
6c95b8df
PA
4846
4847 /* For backward compatibility, don't display inferiors unless there
4848 are several. */
4849 if (loc != NULL
4850 && !header_of_multiple
4851 && (allflag
4852 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4853 && (number_of_program_spaces () > 1
4854 || number_of_inferiors () > 1)
4a64f543
MS
4855 /* LOC is for existing B, it cannot be in
4856 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4857 && loc->owner->type != bp_catchpoint)))
4858 {
4859 struct inferior *inf;
4860 int first = 1;
4861
4862 for (inf = inferior_list; inf != NULL; inf = inf->next)
4863 {
4864 if (inf->pspace == loc->pspace)
4865 {
4866 if (first)
4867 {
4868 first = 0;
4869 ui_out_text (uiout, " inf ");
4870 }
4871 else
4872 ui_out_text (uiout, ", ");
4873 ui_out_text (uiout, plongest (inf->num));
4874 }
4875 }
4876 }
4877
4a306c9a 4878 if (!part_of_multiple)
c4093a6a 4879 {
4a306c9a
JB
4880 if (b->thread != -1)
4881 {
4882 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4883 "stop only in" line a little further down. */
4a306c9a
JB
4884 ui_out_text (uiout, " thread ");
4885 ui_out_field_int (uiout, "thread", b->thread);
4886 }
4887 else if (b->task != 0)
4888 {
4889 ui_out_text (uiout, " task ");
4890 ui_out_field_int (uiout, "task", b->task);
4891 }
c4093a6a
JM
4892 }
4893
8b93c638 4894 ui_out_text (uiout, "\n");
c4093a6a 4895
0fb4aa4b
PA
4896 if (!part_of_multiple && b->static_trace_marker_id)
4897 {
4898 gdb_assert (b->type == bp_static_tracepoint);
4899
4900 ui_out_text (uiout, "\tmarker id is ");
4901 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4902 b->static_trace_marker_id);
4903 ui_out_text (uiout, "\n");
4904 }
4905
0d381245 4906 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4907 {
4908 annotate_field (6);
8b93c638 4909 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4910 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4911 the frame ID. */
5af949e3
UW
4912 ui_out_field_core_addr (uiout, "frame",
4913 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4914 ui_out_text (uiout, "\n");
c4093a6a
JM
4915 }
4916
0d381245 4917 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4918 {
f7f9143b
JB
4919 /* We do not print the condition for Ada exception catchpoints
4920 because the condition is an internal implementation detail
4921 that we do not want to expose to the user. */
c4093a6a 4922 annotate_field (7);
d77f58be 4923 if (is_tracepoint (b))
1042e4c0
SS
4924 ui_out_text (uiout, "\ttrace only if ");
4925 else
4926 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4927 ui_out_field_string (uiout, "cond", b->cond_string);
4928 ui_out_text (uiout, "\n");
4929 }
4930
0d381245 4931 if (!part_of_multiple && b->thread != -1)
c4093a6a 4932 {
4a64f543 4933 /* FIXME should make an annotation for this. */
8b93c638
JM
4934 ui_out_text (uiout, "\tstop only in thread ");
4935 ui_out_field_int (uiout, "thread", b->thread);
4936 ui_out_text (uiout, "\n");
c4093a6a
JM
4937 }
4938
63c715c6 4939 if (!part_of_multiple && b->hit_count)
c4093a6a 4940 {
4a64f543 4941 /* FIXME should make an annotation for this. */
8b93c638
JM
4942 if (ep_is_catchpoint (b))
4943 ui_out_text (uiout, "\tcatchpoint");
4944 else
4945 ui_out_text (uiout, "\tbreakpoint");
4946 ui_out_text (uiout, " already hit ");
4947 ui_out_field_int (uiout, "times", b->hit_count);
4948 if (b->hit_count == 1)
4949 ui_out_text (uiout, " time\n");
4950 else
4951 ui_out_text (uiout, " times\n");
c4093a6a
JM
4952 }
4953
4a64f543
MS
4954 /* Output the count also if it is zero, but only if this is mi.
4955 FIXME: Should have a better test for this. */
9dc5e2a9 4956 if (ui_out_is_mi_like_p (uiout))
63c715c6 4957 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4958 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4959
0d381245 4960 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4961 {
4962 annotate_field (8);
8b93c638
JM
4963 ui_out_text (uiout, "\tignore next ");
4964 ui_out_field_int (uiout, "ignore", b->ignore_count);
4965 ui_out_text (uiout, " hits\n");
c4093a6a 4966 }
059fb39f 4967
9add0f1b 4968 l = b->commands ? b->commands->commands : NULL;
059fb39f 4969 if (!part_of_multiple && l)
c4093a6a 4970 {
3b31d625
EZ
4971 struct cleanup *script_chain;
4972
c4093a6a 4973 annotate_field (9);
3b31d625 4974 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4975 print_command_lines (uiout, l, 4);
3b31d625 4976 do_cleanups (script_chain);
c4093a6a 4977 }
d24317b4 4978
1042e4c0
SS
4979 if (!part_of_multiple && b->pass_count)
4980 {
4981 annotate_field (10);
4982 ui_out_text (uiout, "\tpass count ");
4983 ui_out_field_int (uiout, "pass", b->pass_count);
4984 ui_out_text (uiout, " \n");
4985 }
4986
d24317b4
VP
4987 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4988 {
4989 if (b->addr_string)
4990 ui_out_field_string (uiout, "original-location", b->addr_string);
4991 else if (b->exp_string)
4992 ui_out_field_string (uiout, "original-location", b->exp_string);
4993 }
4994
3b31d625 4995 do_cleanups (bkpt_chain);
c4093a6a 4996}
c5aa993b 4997
0d381245
VP
4998static void
4999print_one_breakpoint (struct breakpoint *b,
4a64f543 5000 struct bp_location **last_loc,
6c95b8df 5001 int allflag)
0d381245 5002{
12c5a436 5003 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
0d381245
VP
5004
5005 /* If this breakpoint has custom print function,
5006 it's already printed. Otherwise, print individual
5007 locations, if any. */
5008 if (b->ops == NULL || b->ops->print_one == NULL)
5009 {
4a64f543
MS
5010 /* If breakpoint has a single location that is disabled, we
5011 print it as if it had several locations, since otherwise it's
5012 hard to represent "breakpoint enabled, location disabled"
5013 situation.
5014
5015 Note that while hardware watchpoints have several locations
a3be7890 5016 internally, that's not a property exposed to user. */
0d381245 5017 if (b->loc
a5606eee 5018 && !is_hardware_watchpoint (b)
0d381245 5019 && (b->loc->next || !b->loc->enabled)
a5606eee 5020 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
5021 {
5022 struct bp_location *loc;
5023 int n = 1;
5024 for (loc = b->loc; loc; loc = loc->next, ++n)
12c5a436 5025 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
0d381245
VP
5026 }
5027 }
5028}
5029
a6d9a66e
UW
5030static int
5031breakpoint_address_bits (struct breakpoint *b)
5032{
5033 int print_address_bits = 0;
5034 struct bp_location *loc;
5035
5036 for (loc = b->loc; loc; loc = loc->next)
5037 {
c7437ca6
PA
5038 int addr_bit;
5039
5040 /* Software watchpoints that aren't watching memory don't have
5041 an address to print. */
5042 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5043 continue;
5044
5045 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5046 if (addr_bit > print_address_bits)
5047 print_address_bits = addr_bit;
5048 }
5049
5050 return print_address_bits;
5051}
0d381245 5052
c4093a6a
JM
5053struct captured_breakpoint_query_args
5054 {
5055 int bnum;
5056 };
c5aa993b 5057
c4093a6a 5058static int
2b65245e 5059do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5060{
5061 struct captured_breakpoint_query_args *args = data;
52f0bd74 5062 struct breakpoint *b;
a6d9a66e 5063 struct bp_location *dummy_loc = NULL;
cc59ec59 5064
c4093a6a
JM
5065 ALL_BREAKPOINTS (b)
5066 {
5067 if (args->bnum == b->number)
c5aa993b 5068 {
12c5a436 5069 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5070 return GDB_RC_OK;
c5aa993b 5071 }
c4093a6a
JM
5072 }
5073 return GDB_RC_NONE;
5074}
c5aa993b 5075
c4093a6a 5076enum gdb_rc
4a64f543
MS
5077gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5078 char **error_message)
c4093a6a
JM
5079{
5080 struct captured_breakpoint_query_args args;
cc59ec59 5081
c4093a6a
JM
5082 args.bnum = bnum;
5083 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5084 an error. */
b0b13bb4
DJ
5085 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5086 error_message, RETURN_MASK_ALL) < 0)
5087 return GDB_RC_FAIL;
5088 else
5089 return GDB_RC_OK;
c4093a6a 5090}
c5aa993b 5091
7f3b0473 5092/* Return non-zero if B is user settable (breakpoints, watchpoints,
4a64f543 5093 catchpoints, et.al.). */
7f3b0473
AC
5094
5095static int
5096user_settable_breakpoint (const struct breakpoint *b)
5097{
5098 return (b->type == bp_breakpoint
ce78b96d 5099 || b->type == bp_catchpoint
7f3b0473 5100 || b->type == bp_hardware_breakpoint
d77f58be 5101 || is_tracepoint (b)
cc60f2e3 5102 || is_watchpoint (b));
7f3b0473 5103}
4a64f543 5104
09d682a4
TT
5105/* Return true if this breakpoint was set by the user, false if it is
5106 internal or momentary. */
5107
5108int
5109user_breakpoint_p (struct breakpoint *b)
5110{
5111 return user_settable_breakpoint (b) && b->number > 0;
5112}
5113
7f3b0473 5114/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5115 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5116 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5117 FILTER is non-NULL, call it on each breakpoint and only include the
5118 ones for which it returns non-zero. Return the total number of
5119 breakpoints listed. */
c906108c 5120
d77f58be 5121static int
e5a67952 5122breakpoint_1 (char *args, int allflag,
4a64f543 5123 int (*filter) (const struct breakpoint *))
c4093a6a 5124{
52f0bd74 5125 struct breakpoint *b;
a6d9a66e 5126 struct bp_location *last_loc = NULL;
7f3b0473 5127 int nr_printable_breakpoints;
3b31d625 5128 struct cleanup *bkpttbl_chain;
79a45b7d 5129 struct value_print_options opts;
a6d9a66e 5130 int print_address_bits = 0;
269b11a2
PA
5131 int print_type_col_width = 14;
5132
79a45b7d
TT
5133 get_user_print_options (&opts);
5134
4a64f543
MS
5135 /* Compute the number of rows in the table, as well as the size
5136 required for address fields. */
7f3b0473
AC
5137 nr_printable_breakpoints = 0;
5138 ALL_BREAKPOINTS (b)
e5a67952
MS
5139 {
5140 /* If we have a filter, only list the breakpoints it accepts. */
5141 if (filter && !filter (b))
5142 continue;
5143
5144 /* If we have an "args" string, it is a list of breakpoints to
5145 accept. Skip the others. */
5146 if (args != NULL && *args != '\0')
5147 {
5148 if (allflag && parse_and_eval_long (args) != b->number)
5149 continue;
5150 if (!allflag && !number_is_in_list (args, b->number))
5151 continue;
5152 }
269b11a2 5153
e5a67952
MS
5154 if (allflag || user_breakpoint_p (b))
5155 {
5156 int addr_bit, type_len;
a6d9a66e 5157
e5a67952
MS
5158 addr_bit = breakpoint_address_bits (b);
5159 if (addr_bit > print_address_bits)
5160 print_address_bits = addr_bit;
269b11a2 5161
e5a67952
MS
5162 type_len = strlen (bptype_string (b->type));
5163 if (type_len > print_type_col_width)
5164 print_type_col_width = type_len;
5165
5166 nr_printable_breakpoints++;
5167 }
5168 }
7f3b0473 5169
79a45b7d 5170 if (opts.addressprint)
3b31d625 5171 bkpttbl_chain
3e43a32a
MS
5172 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5173 nr_printable_breakpoints,
3b31d625 5174 "BreakpointTable");
8b93c638 5175 else
3b31d625 5176 bkpttbl_chain
3e43a32a
MS
5177 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5178 nr_printable_breakpoints,
3b31d625 5179 "BreakpointTable");
8b93c638 5180
7f3b0473 5181 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5182 annotate_breakpoints_headers ();
5183 if (nr_printable_breakpoints > 0)
5184 annotate_field (0);
4a64f543 5185 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5186 if (nr_printable_breakpoints > 0)
5187 annotate_field (1);
269b11a2 5188 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5189 "type", "Type"); /* 2 */
d7faa9e7
AC
5190 if (nr_printable_breakpoints > 0)
5191 annotate_field (2);
4a64f543 5192 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5193 if (nr_printable_breakpoints > 0)
5194 annotate_field (3);
54e52265 5195 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5196 if (opts.addressprint)
e5a67952
MS
5197 {
5198 if (nr_printable_breakpoints > 0)
5199 annotate_field (4);
5200 if (print_address_bits <= 32)
5201 ui_out_table_header (uiout, 10, ui_left,
5202 "addr", "Address"); /* 5 */
5203 else
5204 ui_out_table_header (uiout, 18, ui_left,
5205 "addr", "Address"); /* 5 */
5206 }
d7faa9e7
AC
5207 if (nr_printable_breakpoints > 0)
5208 annotate_field (5);
5209 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5210 ui_out_table_body (uiout);
5211 if (nr_printable_breakpoints > 0)
5212 annotate_breakpoints_table ();
7f3b0473 5213
c4093a6a 5214 ALL_BREAKPOINTS (b)
e5a67952
MS
5215 {
5216 QUIT;
5217 /* If we have a filter, only list the breakpoints it accepts. */
5218 if (filter && !filter (b))
5219 continue;
5220
5221 /* If we have an "args" string, it is a list of breakpoints to
5222 accept. Skip the others. */
5223
5224 if (args != NULL && *args != '\0')
5225 {
5226 if (allflag) /* maintenance info breakpoint */
5227 {
5228 if (parse_and_eval_long (args) != b->number)
5229 continue;
5230 }
5231 else /* all others */
5232 {
5233 if (!number_is_in_list (args, b->number))
5234 continue;
5235 }
5236 }
5237 /* We only print out user settable breakpoints unless the
5238 allflag is set. */
5239 if (allflag || user_breakpoint_p (b))
12c5a436 5240 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5241 }
5242
3b31d625 5243 do_cleanups (bkpttbl_chain);
698384cd 5244
7f3b0473 5245 if (nr_printable_breakpoints == 0)
c906108c 5246 {
4a64f543
MS
5247 /* If there's a filter, let the caller decide how to report
5248 empty list. */
d77f58be
SS
5249 if (!filter)
5250 {
e5a67952 5251 if (args == NULL || *args == '\0')
d77f58be
SS
5252 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5253 else
4a64f543 5254 ui_out_message (uiout, 0,
e5a67952
MS
5255 "No breakpoint or watchpoint matching '%s'.\n",
5256 args);
d77f58be 5257 }
c906108c
SS
5258 }
5259 else
c4093a6a 5260 {
a6d9a66e
UW
5261 if (last_loc && !server_command)
5262 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5263 }
c906108c 5264
4a64f543 5265 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5266 there have been breakpoints? */
c906108c 5267 annotate_breakpoints_table_end ();
d77f58be
SS
5268
5269 return nr_printable_breakpoints;
c906108c
SS
5270}
5271
ad443146
SS
5272/* Display the value of default-collect in a way that is generally
5273 compatible with the breakpoint list. */
5274
5275static void
5276default_collect_info (void)
5277{
5278 /* If it has no value (which is frequently the case), say nothing; a
5279 message like "No default-collect." gets in user's face when it's
5280 not wanted. */
5281 if (!*default_collect)
5282 return;
5283
5284 /* The following phrase lines up nicely with per-tracepoint collect
5285 actions. */
5286 ui_out_text (uiout, "default collect ");
5287 ui_out_field_string (uiout, "default-collect", default_collect);
5288 ui_out_text (uiout, " \n");
5289}
5290
c906108c 5291static void
e5a67952 5292breakpoints_info (char *args, int from_tty)
c906108c 5293{
e5a67952 5294 breakpoint_1 (args, 0, NULL);
ad443146
SS
5295
5296 default_collect_info ();
d77f58be
SS
5297}
5298
5299static void
e5a67952 5300watchpoints_info (char *args, int from_tty)
d77f58be 5301{
e5a67952 5302 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5303
5304 if (num_printed == 0)
5305 {
e5a67952 5306 if (args == NULL || *args == '\0')
d77f58be
SS
5307 ui_out_message (uiout, 0, "No watchpoints.\n");
5308 else
e5a67952 5309 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5310 }
c906108c
SS
5311}
5312
7a292a7a 5313static void
e5a67952 5314maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5315{
e5a67952 5316 breakpoint_1 (args, 1, NULL);
ad443146
SS
5317
5318 default_collect_info ();
c906108c
SS
5319}
5320
0d381245 5321static int
714835d5 5322breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5323 struct program_space *pspace,
714835d5 5324 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5325{
5326 struct bp_location *bl = b->loc;
cc59ec59 5327
0d381245
VP
5328 for (; bl; bl = bl->next)
5329 {
6c95b8df
PA
5330 if (bl->pspace == pspace
5331 && bl->address == pc
0d381245
VP
5332 && (!overlay_debugging || bl->section == section))
5333 return 1;
5334 }
5335 return 0;
5336}
5337
6c95b8df
PA
5338/* Print a message describing any breakpoints set at PC. This
5339 concerns with logical breakpoints, so we match program spaces, not
5340 address spaces. */
c906108c
SS
5341
5342static void
6c95b8df
PA
5343describe_other_breakpoints (struct gdbarch *gdbarch,
5344 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5345 struct obj_section *section, int thread)
c906108c 5346{
52f0bd74
AC
5347 int others = 0;
5348 struct breakpoint *b;
c906108c
SS
5349
5350 ALL_BREAKPOINTS (b)
6c95b8df 5351 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5352 if (others > 0)
5353 {
a3f17187
AC
5354 if (others == 1)
5355 printf_filtered (_("Note: breakpoint "));
5356 else /* if (others == ???) */
5357 printf_filtered (_("Note: breakpoints "));
c906108c 5358 ALL_BREAKPOINTS (b)
6c95b8df 5359 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5360 {
5361 others--;
5362 printf_filtered ("%d", b->number);
5363 if (b->thread == -1 && thread != -1)
5364 printf_filtered (" (all threads)");
5365 else if (b->thread != -1)
5366 printf_filtered (" (thread %d)", b->thread);
5367 printf_filtered ("%s%s ",
059fb39f 5368 ((b->enable_state == bp_disabled
8bea4e01
UW
5369 || b->enable_state == bp_call_disabled
5370 || b->enable_state == bp_startup_disabled)
0d381245
VP
5371 ? " (disabled)"
5372 : b->enable_state == bp_permanent
5373 ? " (permanent)"
5374 : ""),
5375 (others > 1) ? ","
5376 : ((others == 1) ? " and" : ""));
5377 }
a3f17187 5378 printf_filtered (_("also set at pc "));
5af949e3 5379 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5380 printf_filtered (".\n");
5381 }
5382}
5383\f
5384/* Set the default place to put a breakpoint
5385 for the `break' command with no arguments. */
5386
5387void
6c95b8df
PA
5388set_default_breakpoint (int valid, struct program_space *pspace,
5389 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5390 int line)
c906108c
SS
5391{
5392 default_breakpoint_valid = valid;
6c95b8df 5393 default_breakpoint_pspace = pspace;
c906108c
SS
5394 default_breakpoint_address = addr;
5395 default_breakpoint_symtab = symtab;
5396 default_breakpoint_line = line;
5397}
5398
e4f237da
KB
5399/* Return true iff it is meaningful to use the address member of
5400 BPT. For some breakpoint types, the address member is irrelevant
5401 and it makes no sense to attempt to compare it to other addresses
5402 (or use it for any other purpose either).
5403
4a64f543
MS
5404 More specifically, each of the following breakpoint types will
5405 always have a zero valued address and we don't want to mark
5406 breakpoints of any of these types to be a duplicate of an actual
5407 breakpoint at address zero:
e4f237da
KB
5408
5409 bp_watchpoint
2d134ed3
PA
5410 bp_catchpoint
5411
5412*/
e4f237da
KB
5413
5414static int
5415breakpoint_address_is_meaningful (struct breakpoint *bpt)
5416{
5417 enum bptype type = bpt->type;
5418
2d134ed3
PA
5419 return (type != bp_watchpoint && type != bp_catchpoint);
5420}
5421
5422/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5423 true if LOC1 and LOC2 represent the same watchpoint location. */
5424
5425static int
4a64f543
MS
5426watchpoint_locations_match (struct bp_location *loc1,
5427 struct bp_location *loc2)
2d134ed3 5428{
2bdf28a0
JK
5429 /* Both of them must not be in moribund_locations. */
5430 gdb_assert (loc1->owner != NULL);
5431 gdb_assert (loc2->owner != NULL);
5432
4a64f543
MS
5433 /* If the target can evaluate the condition expression in hardware,
5434 then we we need to insert both watchpoints even if they are at
5435 the same place. Otherwise the watchpoint will only trigger when
5436 the condition of whichever watchpoint was inserted evaluates to
5437 true, not giving a chance for GDB to check the condition of the
5438 other watchpoint. */
0cf6dd15 5439 if ((loc1->owner->cond_exp
4a64f543
MS
5440 && target_can_accel_watchpoint_condition (loc1->address,
5441 loc1->length,
0cf6dd15
TJB
5442 loc1->watchpoint_type,
5443 loc1->owner->cond_exp))
5444 || (loc2->owner->cond_exp
4a64f543
MS
5445 && target_can_accel_watchpoint_condition (loc2->address,
5446 loc2->length,
0cf6dd15
TJB
5447 loc2->watchpoint_type,
5448 loc2->owner->cond_exp)))
5449 return 0;
5450
85d721b8
PA
5451 /* Note that this checks the owner's type, not the location's. In
5452 case the target does not support read watchpoints, but does
5453 support access watchpoints, we'll have bp_read_watchpoint
5454 watchpoints with hw_access locations. Those should be considered
5455 duplicates of hw_read locations. The hw_read locations will
5456 become hw_access locations later. */
2d134ed3
PA
5457 return (loc1->owner->type == loc2->owner->type
5458 && loc1->pspace->aspace == loc2->pspace->aspace
5459 && loc1->address == loc2->address
5460 && loc1->length == loc2->length);
e4f237da
KB
5461}
5462
6c95b8df
PA
5463/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5464 same breakpoint location. In most targets, this can only be true
5465 if ASPACE1 matches ASPACE2. On targets that have global
5466 breakpoints, the address space doesn't really matter. */
5467
5468static int
5469breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5470 struct address_space *aspace2, CORE_ADDR addr2)
5471{
5472 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5473 || aspace1 == aspace2)
5474 && addr1 == addr2);
5475}
5476
2d134ed3
PA
5477/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5478 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5479 represent the same location. */
5480
5481static int
4a64f543
MS
5482breakpoint_locations_match (struct bp_location *loc1,
5483 struct bp_location *loc2)
2d134ed3 5484{
2bdf28a0
JK
5485 int hw_point1, hw_point2;
5486
5487 /* Both of them must not be in moribund_locations. */
5488 gdb_assert (loc1->owner != NULL);
5489 gdb_assert (loc2->owner != NULL);
5490
5491 hw_point1 = is_hardware_watchpoint (loc1->owner);
5492 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5493
5494 if (hw_point1 != hw_point2)
5495 return 0;
5496 else if (hw_point1)
5497 return watchpoint_locations_match (loc1, loc2);
5498 else
5499 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5500 loc2->pspace->aspace, loc2->address);
5501}
5502
76897487
KB
5503static void
5504breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5505 int bnum, int have_bnum)
5506{
5507 char astr1[40];
5508 char astr2[40];
5509
bb599908
PH
5510 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5511 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5512 if (have_bnum)
8a3fe4f8 5513 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5514 bnum, astr1, astr2);
5515 else
8a3fe4f8 5516 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5517}
5518
4a64f543
MS
5519/* Adjust a breakpoint's address to account for architectural
5520 constraints on breakpoint placement. Return the adjusted address.
5521 Note: Very few targets require this kind of adjustment. For most
5522 targets, this function is simply the identity function. */
76897487
KB
5523
5524static CORE_ADDR
a6d9a66e
UW
5525adjust_breakpoint_address (struct gdbarch *gdbarch,
5526 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5527{
a6d9a66e 5528 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5529 {
5530 /* Very few targets need any kind of breakpoint adjustment. */
5531 return bpaddr;
5532 }
88f7da05
KB
5533 else if (bptype == bp_watchpoint
5534 || bptype == bp_hardware_watchpoint
5535 || bptype == bp_read_watchpoint
5536 || bptype == bp_access_watchpoint
fe798b75 5537 || bptype == bp_catchpoint)
88f7da05
KB
5538 {
5539 /* Watchpoints and the various bp_catch_* eventpoints should not
5540 have their addresses modified. */
5541 return bpaddr;
5542 }
76897487
KB
5543 else
5544 {
5545 CORE_ADDR adjusted_bpaddr;
5546
5547 /* Some targets have architectural constraints on the placement
5548 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5549 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5550
5551 /* An adjusted breakpoint address can significantly alter
5552 a user's expectations. Print a warning if an adjustment
5553 is required. */
5554 if (adjusted_bpaddr != bpaddr)
5555 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5556
5557 return adjusted_bpaddr;
5558 }
5559}
5560
7cc221ef
DJ
5561/* Allocate a struct bp_location. */
5562
26bb91f3 5563static struct bp_location *
39d61571 5564allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5565{
afe38095 5566 struct bp_location *loc;
7cc221ef
DJ
5567
5568 loc = xmalloc (sizeof (struct bp_location));
5569 memset (loc, 0, sizeof (*loc));
5570
e049a4b5 5571 loc->owner = bpt;
511a6cd4 5572 loc->cond = NULL;
0d381245
VP
5573 loc->shlib_disabled = 0;
5574 loc->enabled = 1;
e049a4b5 5575
39d61571 5576 switch (bpt->type)
e049a4b5
DJ
5577 {
5578 case bp_breakpoint:
5579 case bp_until:
5580 case bp_finish:
5581 case bp_longjmp:
5582 case bp_longjmp_resume:
186c406b
TT
5583 case bp_exception:
5584 case bp_exception_resume:
e049a4b5 5585 case bp_step_resume:
e049a4b5
DJ
5586 case bp_watchpoint_scope:
5587 case bp_call_dummy:
aa7d318d 5588 case bp_std_terminate:
e049a4b5
DJ
5589 case bp_shlib_event:
5590 case bp_thread_event:
5591 case bp_overlay_event:
4efc6507 5592 case bp_jit_event:
0fd8e87f 5593 case bp_longjmp_master:
aa7d318d 5594 case bp_std_terminate_master:
186c406b 5595 case bp_exception_master:
e049a4b5
DJ
5596 loc->loc_type = bp_loc_software_breakpoint;
5597 break;
5598 case bp_hardware_breakpoint:
5599 loc->loc_type = bp_loc_hardware_breakpoint;
5600 break;
5601 case bp_hardware_watchpoint:
5602 case bp_read_watchpoint:
5603 case bp_access_watchpoint:
5604 loc->loc_type = bp_loc_hardware_watchpoint;
5605 break;
5606 case bp_watchpoint:
ce78b96d 5607 case bp_catchpoint:
15c3d785
PA
5608 case bp_tracepoint:
5609 case bp_fast_tracepoint:
0fb4aa4b 5610 case bp_static_tracepoint:
e049a4b5
DJ
5611 loc->loc_type = bp_loc_other;
5612 break;
5613 default:
e2e0b3e5 5614 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5615 }
5616
f431efe5 5617 loc->refc = 1;
7cc221ef
DJ
5618 return loc;
5619}
5620
f431efe5
PA
5621static void
5622free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5623{
5624 if (loc->cond)
5625 xfree (loc->cond);
74960c60
VP
5626
5627 if (loc->function_name)
5628 xfree (loc->function_name);
f431efe5 5629
fe3f5fa8
VP
5630 xfree (loc);
5631}
5632
f431efe5
PA
5633/* Increment reference count. */
5634
5635static void
5636incref_bp_location (struct bp_location *bl)
5637{
5638 ++bl->refc;
5639}
5640
5641/* Decrement reference count. If the reference count reaches 0,
5642 destroy the bp_location. Sets *BLP to NULL. */
5643
5644static void
5645decref_bp_location (struct bp_location **blp)
5646{
0807b50c
PA
5647 gdb_assert ((*blp)->refc > 0);
5648
f431efe5
PA
5649 if (--(*blp)->refc == 0)
5650 free_bp_location (*blp);
5651 *blp = NULL;
5652}
5653
4a64f543
MS
5654/* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5655 type BPTYPE and has no locations as yet. */
5656/* This function is used in gdbtk sources and thus can not be made
5657 static. */
c906108c 5658
c40e75cd 5659static struct breakpoint *
a6d9a66e
UW
5660set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5661 enum bptype bptype)
c906108c 5662{
52f0bd74 5663 struct breakpoint *b, *b1;
c906108c
SS
5664
5665 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5666 memset (b, 0, sizeof (*b));
2219d63c 5667
4d28f7a8 5668 b->type = bptype;
a6d9a66e 5669 b->gdbarch = gdbarch;
c906108c
SS
5670 b->language = current_language->la_language;
5671 b->input_radix = input_radix;
5672 b->thread = -1;
b5de0fa7 5673 b->enable_state = bp_enabled;
c906108c
SS
5674 b->next = 0;
5675 b->silent = 0;
5676 b->ignore_count = 0;
5677 b->commands = NULL;
818dd999 5678 b->frame_id = null_frame_id;
3a3e9ee3 5679 b->forked_inferior_pid = null_ptid;
c906108c 5680 b->exec_pathname = NULL;
a96d9b2e 5681 b->syscalls_to_be_caught = NULL;
3086aeae 5682 b->ops = NULL;
0d381245 5683 b->condition_not_parsed = 0;
84f4c1fe 5684 b->py_bp_object = NULL;
c906108c 5685
4a64f543
MS
5686 /* Add this breakpoint to the end of the chain so that a list of
5687 breakpoints will come out in order of increasing numbers. */
c906108c
SS
5688
5689 b1 = breakpoint_chain;
5690 if (b1 == 0)
5691 breakpoint_chain = b;
5692 else
5693 {
5694 while (b1->next)
5695 b1 = b1->next;
5696 b1->next = b;
5697 }
0d381245
VP
5698 return b;
5699}
5700
5701/* Initialize loc->function_name. */
5702static void
5703set_breakpoint_location_function (struct bp_location *loc)
5704{
2bdf28a0
JK
5705 gdb_assert (loc->owner != NULL);
5706
0d381245 5707 if (loc->owner->type == bp_breakpoint
1042e4c0 5708 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5709 || is_tracepoint (loc->owner))
0d381245
VP
5710 {
5711 find_pc_partial_function (loc->address, &(loc->function_name),
5712 NULL, NULL);
5713 if (loc->function_name)
5714 loc->function_name = xstrdup (loc->function_name);
5715 }
5716}
5717
a6d9a66e
UW
5718/* Attempt to determine architecture of location identified by SAL. */
5719static struct gdbarch *
5720get_sal_arch (struct symtab_and_line sal)
5721{
5722 if (sal.section)
5723 return get_objfile_arch (sal.section->objfile);
5724 if (sal.symtab)
5725 return get_objfile_arch (sal.symtab->objfile);
5726
5727 return NULL;
5728}
5729
0d381245
VP
5730/* set_raw_breakpoint is a low level routine for allocating and
5731 partially initializing a breakpoint of type BPTYPE. The newly
5732 created breakpoint's address, section, source file name, and line
5733 number are provided by SAL. The newly created and partially
5734 initialized breakpoint is added to the breakpoint chain and
5735 is also returned as the value of this function.
5736
5737 It is expected that the caller will complete the initialization of
5738 the newly created breakpoint struct as well as output any status
5739 information regarding the creation of a new breakpoint. In
5740 particular, set_raw_breakpoint does NOT set the breakpoint
5741 number! Care should be taken to not allow an error to occur
5742 prior to completing the initialization of the breakpoint. If this
5743 should happen, a bogus breakpoint will be left on the chain. */
5744
63c252f8 5745struct breakpoint *
a6d9a66e
UW
5746set_raw_breakpoint (struct gdbarch *gdbarch,
5747 struct symtab_and_line sal, enum bptype bptype)
0d381245 5748{
4a64f543
MS
5749 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5750 bptype);
0d381245 5751 CORE_ADDR adjusted_address;
a6d9a66e
UW
5752 struct gdbarch *loc_gdbarch;
5753
5754 loc_gdbarch = get_sal_arch (sal);
5755 if (!loc_gdbarch)
5756 loc_gdbarch = b->gdbarch;
0d381245 5757
6c95b8df
PA
5758 if (bptype != bp_catchpoint)
5759 gdb_assert (sal.pspace != NULL);
5760
0d381245
VP
5761 /* Adjust the breakpoint's address prior to allocating a location.
5762 Once we call allocate_bp_location(), that mostly uninitialized
5763 location will be placed on the location chain. Adjustment of the
8defab1a 5764 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5765 not want its scan of the location chain to find a breakpoint and
5766 location that's only been partially initialized. */
4a64f543
MS
5767 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5768 sal.pc, b->type);
0d381245 5769
39d61571 5770 b->loc = allocate_bp_location (b);
a6d9a66e 5771 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5772 b->loc->requested_address = sal.pc;
5773 b->loc->address = adjusted_address;
6c95b8df
PA
5774 b->loc->pspace = sal.pspace;
5775
5776 /* Store the program space that was used to set the breakpoint, for
5777 breakpoint resetting. */
5778 b->pspace = sal.pspace;
0d381245
VP
5779
5780 if (sal.symtab == NULL)
5781 b->source_file = NULL;
5782 else
1b36a34b 5783 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5784 b->loc->section = sal.section;
5785 b->line_number = sal.line;
5786
5787 set_breakpoint_location_function (b->loc);
c906108c 5788
c906108c
SS
5789 breakpoints_changed ();
5790
5791 return b;
5792}
5793
c2c6d25f
JM
5794
5795/* Note that the breakpoint object B describes a permanent breakpoint
5796 instruction, hard-wired into the inferior's code. */
5797void
5798make_breakpoint_permanent (struct breakpoint *b)
5799{
0d381245 5800 struct bp_location *bl;
cc59ec59 5801
b5de0fa7 5802 b->enable_state = bp_permanent;
c2c6d25f 5803
4a64f543
MS
5804 /* By definition, permanent breakpoints are already present in the
5805 code. Mark all locations as inserted. For now,
5806 make_breakpoint_permanent is called in just one place, so it's
5807 hard to say if it's reasonable to have permanent breakpoint with
5808 multiple locations or not, but it's easy to implmement. */
0d381245
VP
5809 for (bl = b->loc; bl; bl = bl->next)
5810 bl->inserted = 1;
c2c6d25f
JM
5811}
5812
53a5351d 5813/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5814 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5815 initiated the operation. */
c906108c
SS
5816
5817void
186c406b 5818set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5819{
35df4500 5820 struct breakpoint *b, *b_tmp;
186c406b 5821 int thread = tp->num;
0fd8e87f
UW
5822
5823 /* To avoid having to rescan all objfile symbols at every step,
5824 we maintain a list of continually-inserted but always disabled
5825 longjmp "master" breakpoints. Here, we simply create momentary
5826 clones of those and enable them for the requested thread. */
35df4500 5827 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5828 if (b->pspace == current_program_space
186c406b
TT
5829 && (b->type == bp_longjmp_master
5830 || b->type == bp_exception_master))
0fd8e87f
UW
5831 {
5832 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5833
186c406b 5834 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5835 clone->thread = thread;
5836 }
186c406b
TT
5837
5838 tp->initiating_frame = frame;
c906108c
SS
5839}
5840
611c83ae 5841/* Delete all longjmp breakpoints from THREAD. */
c906108c 5842void
611c83ae 5843delete_longjmp_breakpoint (int thread)
c906108c 5844{
35df4500 5845 struct breakpoint *b, *b_tmp;
c906108c 5846
35df4500 5847 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5848 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5849 {
5850 if (b->thread == thread)
5851 delete_breakpoint (b);
5852 }
c906108c
SS
5853}
5854
1900040c
MS
5855void
5856enable_overlay_breakpoints (void)
5857{
52f0bd74 5858 struct breakpoint *b;
1900040c
MS
5859
5860 ALL_BREAKPOINTS (b)
5861 if (b->type == bp_overlay_event)
5862 {
5863 b->enable_state = bp_enabled;
b60e7edf 5864 update_global_location_list (1);
c02f5703 5865 overlay_events_enabled = 1;
1900040c
MS
5866 }
5867}
5868
5869void
5870disable_overlay_breakpoints (void)
5871{
52f0bd74 5872 struct breakpoint *b;
1900040c
MS
5873
5874 ALL_BREAKPOINTS (b)
5875 if (b->type == bp_overlay_event)
5876 {
5877 b->enable_state = bp_disabled;
b60e7edf 5878 update_global_location_list (0);
c02f5703 5879 overlay_events_enabled = 0;
1900040c
MS
5880 }
5881}
5882
aa7d318d
TT
5883/* Set an active std::terminate breakpoint for each std::terminate
5884 master breakpoint. */
5885void
5886set_std_terminate_breakpoint (void)
5887{
35df4500 5888 struct breakpoint *b, *b_tmp;
aa7d318d 5889
35df4500 5890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5891 if (b->pspace == current_program_space
5892 && b->type == bp_std_terminate_master)
5893 {
5894 struct breakpoint *clone = clone_momentary_breakpoint (b);
5895 clone->type = bp_std_terminate;
5896 }
5897}
5898
5899/* Delete all the std::terminate breakpoints. */
5900void
5901delete_std_terminate_breakpoint (void)
5902{
35df4500 5903 struct breakpoint *b, *b_tmp;
aa7d318d 5904
35df4500 5905 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5906 if (b->type == bp_std_terminate)
5907 delete_breakpoint (b);
5908}
5909
c4093a6a 5910struct breakpoint *
a6d9a66e 5911create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5912{
5913 struct breakpoint *b;
c4093a6a 5914
a6d9a66e 5915 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 5916
b5de0fa7 5917 b->enable_state = bp_enabled;
c4093a6a 5918 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5919 b->addr_string
5920 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5921
b60e7edf 5922 update_global_location_list_nothrow (1);
74960c60 5923
c4093a6a
JM
5924 return b;
5925}
5926
5927void
5928remove_thread_event_breakpoints (void)
5929{
35df4500 5930 struct breakpoint *b, *b_tmp;
c4093a6a 5931
35df4500 5932 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5933 if (b->type == bp_thread_event
5934 && b->loc->pspace == current_program_space)
c4093a6a
JM
5935 delete_breakpoint (b);
5936}
5937
0101ce28
JJ
5938struct captured_parse_breakpoint_args
5939 {
5940 char **arg_p;
5941 struct symtabs_and_lines *sals_p;
5942 char ***addr_string_p;
5943 int *not_found_ptr;
5944 };
5945
5946struct lang_and_radix
5947 {
5948 enum language lang;
5949 int radix;
5950 };
5951
4efc6507
DE
5952/* Create a breakpoint for JIT code registration and unregistration. */
5953
5954struct breakpoint *
5955create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5956{
5957 struct breakpoint *b;
5958
5959 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
5960 update_global_location_list_nothrow (1);
5961 return b;
5962}
0101ce28 5963
03673fc7
PP
5964/* Remove JIT code registration and unregistration breakpoint(s). */
5965
5966void
5967remove_jit_event_breakpoints (void)
5968{
5969 struct breakpoint *b, *b_tmp;
5970
5971 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5972 if (b->type == bp_jit_event
5973 && b->loc->pspace == current_program_space)
5974 delete_breakpoint (b);
5975}
5976
cae688ec
JJ
5977void
5978remove_solib_event_breakpoints (void)
5979{
35df4500 5980 struct breakpoint *b, *b_tmp;
cae688ec 5981
35df4500 5982 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5983 if (b->type == bp_shlib_event
5984 && b->loc->pspace == current_program_space)
cae688ec
JJ
5985 delete_breakpoint (b);
5986}
5987
5988struct breakpoint *
a6d9a66e 5989create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
5990{
5991 struct breakpoint *b;
5992
a6d9a66e 5993 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 5994 update_global_location_list_nothrow (1);
cae688ec
JJ
5995 return b;
5996}
5997
5998/* Disable any breakpoints that are on code in shared libraries. Only
5999 apply to enabled breakpoints, disabled ones can just stay disabled. */
6000
6001void
cb851954 6002disable_breakpoints_in_shlibs (void)
cae688ec 6003{
876fa593 6004 struct bp_location *loc, **locp_tmp;
cae688ec 6005
876fa593 6006 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6007 {
2bdf28a0 6008 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6009 struct breakpoint *b = loc->owner;
2bdf28a0 6010
4a64f543
MS
6011 /* We apply the check to all breakpoints, including disabled for
6012 those with loc->duplicate set. This is so that when breakpoint
6013 becomes enabled, or the duplicate is removed, gdb will try to
6014 insert all breakpoints. If we don't set shlib_disabled here,
6015 we'll try to insert those breakpoints and fail. */
1042e4c0 6016 if (((b->type == bp_breakpoint)
508ccb1f 6017 || (b->type == bp_jit_event)
1042e4c0 6018 || (b->type == bp_hardware_breakpoint)
d77f58be 6019 || (is_tracepoint (b)))
6c95b8df 6020 && loc->pspace == current_program_space
0d381245 6021 && !loc->shlib_disabled
a77053c2 6022#ifdef PC_SOLIB
0d381245 6023 && PC_SOLIB (loc->address)
a77053c2 6024#else
6c95b8df 6025 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6026#endif
6027 )
0d381245
VP
6028 {
6029 loc->shlib_disabled = 1;
6030 }
cae688ec
JJ
6031 }
6032}
6033
4a64f543
MS
6034/* Disable any breakpoints that are in in an unloaded shared library.
6035 Only apply to enabled breakpoints, disabled ones can just stay
6036 disabled. */
84acb35a 6037
75149521 6038static void
84acb35a
JJ
6039disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6040{
876fa593 6041 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6042 int disabled_shlib_breaks = 0;
6043
c86cf029
VP
6044 /* SunOS a.out shared libraries are always mapped, so do not
6045 disable breakpoints; they will only be reported as unloaded
6046 through clear_solib when GDB discards its shared library
6047 list. See clear_solib for more information. */
6048 if (exec_bfd != NULL
6049 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6050 return;
6051
876fa593 6052 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6053 {
2bdf28a0 6054 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6055 struct breakpoint *b = loc->owner;
cc59ec59 6056
0d381245
VP
6057 if ((loc->loc_type == bp_loc_hardware_breakpoint
6058 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6059 && solib->pspace == loc->pspace
e2dd7057 6060 && !loc->shlib_disabled
508ccb1f
TT
6061 && (b->type == bp_breakpoint
6062 || b->type == bp_jit_event
6063 || b->type == bp_hardware_breakpoint)
e2dd7057 6064 && solib_contains_address_p (solib, loc->address))
84acb35a 6065 {
e2dd7057
PP
6066 loc->shlib_disabled = 1;
6067 /* At this point, we cannot rely on remove_breakpoint
6068 succeeding so we must mark the breakpoint as not inserted
6069 to prevent future errors occurring in remove_breakpoints. */
6070 loc->inserted = 0;
6071 if (!disabled_shlib_breaks)
6072 {
6073 target_terminal_ours_for_output ();
3e43a32a
MS
6074 warning (_("Temporarily disabling breakpoints "
6075 "for unloaded shared library \"%s\""),
e2dd7057 6076 solib->so_name);
84acb35a 6077 }
e2dd7057 6078 disabled_shlib_breaks = 1;
84acb35a
JJ
6079 }
6080 }
84acb35a
JJ
6081}
6082
ce78b96d
JB
6083/* FORK & VFORK catchpoints. */
6084
4a64f543
MS
6085/* Implement the "insert" breakpoint_ops method for fork
6086 catchpoints. */
ce78b96d 6087
77b06cd7
TJB
6088static int
6089insert_catch_fork (struct bp_location *bl)
ce78b96d 6090{
77b06cd7 6091 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6092}
6093
4a64f543
MS
6094/* Implement the "remove" breakpoint_ops method for fork
6095 catchpoints. */
ce78b96d
JB
6096
6097static int
77b06cd7 6098remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6099{
6100 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6101}
6102
6103/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6104 catchpoints. */
6105
6106static int
6107breakpoint_hit_catch_fork (struct breakpoint *b)
6108{
6109 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6110}
6111
4a64f543
MS
6112/* Implement the "print_it" breakpoint_ops method for fork
6113 catchpoints. */
ce78b96d
JB
6114
6115static enum print_stop_action
6116print_it_catch_fork (struct breakpoint *b)
6117{
6118 annotate_catchpoint (b->number);
6119 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6120 b->number, ptid_get_pid (b->forked_inferior_pid));
6121 return PRINT_SRC_AND_LOC;
6122}
6123
4a64f543
MS
6124/* Implement the "print_one" breakpoint_ops method for fork
6125 catchpoints. */
ce78b96d
JB
6126
6127static void
a6d9a66e 6128print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6129{
79a45b7d
TT
6130 struct value_print_options opts;
6131
6132 get_user_print_options (&opts);
6133
4a64f543
MS
6134 /* Field 4, the address, is omitted (which makes the columns not
6135 line up too nicely with the headers, but the effect is relatively
6136 readable). */
79a45b7d 6137 if (opts.addressprint)
ce78b96d
JB
6138 ui_out_field_skip (uiout, "addr");
6139 annotate_field (5);
6140 ui_out_text (uiout, "fork");
6141 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6142 {
6143 ui_out_text (uiout, ", process ");
6144 ui_out_field_int (uiout, "what",
6145 ptid_get_pid (b->forked_inferior_pid));
6146 ui_out_spaces (uiout, 1);
6147 }
6148}
6149
6150/* Implement the "print_mention" breakpoint_ops method for fork
6151 catchpoints. */
6152
6153static void
6154print_mention_catch_fork (struct breakpoint *b)
6155{
6156 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6157}
6158
6149aea9
PA
6159/* Implement the "print_recreate" breakpoint_ops method for fork
6160 catchpoints. */
6161
6162static void
6163print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6164{
6165 fprintf_unfiltered (fp, "catch fork");
6166}
6167
ce78b96d
JB
6168/* The breakpoint_ops structure to be used in fork catchpoints. */
6169
6170static struct breakpoint_ops catch_fork_breakpoint_ops =
6171{
6172 insert_catch_fork,
6173 remove_catch_fork,
6174 breakpoint_hit_catch_fork,
e09342b5 6175 NULL, /* resources_needed */
ce78b96d
JB
6176 print_it_catch_fork,
6177 print_one_catch_fork,
6149aea9
PA
6178 print_mention_catch_fork,
6179 print_recreate_catch_fork
ce78b96d
JB
6180};
6181
4a64f543
MS
6182/* Implement the "insert" breakpoint_ops method for vfork
6183 catchpoints. */
ce78b96d 6184
77b06cd7
TJB
6185static int
6186insert_catch_vfork (struct bp_location *bl)
ce78b96d 6187{
77b06cd7 6188 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6189}
6190
4a64f543
MS
6191/* Implement the "remove" breakpoint_ops method for vfork
6192 catchpoints. */
ce78b96d
JB
6193
6194static int
77b06cd7 6195remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6196{
6197 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6198}
6199
6200/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6201 catchpoints. */
6202
6203static int
6204breakpoint_hit_catch_vfork (struct breakpoint *b)
6205{
6206 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6207}
6208
4a64f543
MS
6209/* Implement the "print_it" breakpoint_ops method for vfork
6210 catchpoints. */
ce78b96d
JB
6211
6212static enum print_stop_action
6213print_it_catch_vfork (struct breakpoint *b)
6214{
6215 annotate_catchpoint (b->number);
6216 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6217 b->number, ptid_get_pid (b->forked_inferior_pid));
6218 return PRINT_SRC_AND_LOC;
6219}
6220
4a64f543
MS
6221/* Implement the "print_one" breakpoint_ops method for vfork
6222 catchpoints. */
ce78b96d
JB
6223
6224static void
a6d9a66e 6225print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6226{
79a45b7d
TT
6227 struct value_print_options opts;
6228
6229 get_user_print_options (&opts);
4a64f543
MS
6230 /* Field 4, the address, is omitted (which makes the columns not
6231 line up too nicely with the headers, but the effect is relatively
6232 readable). */
79a45b7d 6233 if (opts.addressprint)
ce78b96d
JB
6234 ui_out_field_skip (uiout, "addr");
6235 annotate_field (5);
6236 ui_out_text (uiout, "vfork");
6237 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6238 {
6239 ui_out_text (uiout, ", process ");
6240 ui_out_field_int (uiout, "what",
6241 ptid_get_pid (b->forked_inferior_pid));
6242 ui_out_spaces (uiout, 1);
6243 }
6244}
6245
6246/* Implement the "print_mention" breakpoint_ops method for vfork
6247 catchpoints. */
6248
6249static void
6250print_mention_catch_vfork (struct breakpoint *b)
6251{
6252 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6253}
6254
6149aea9
PA
6255/* Implement the "print_recreate" breakpoint_ops method for vfork
6256 catchpoints. */
6257
6258static void
6259print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6260{
6261 fprintf_unfiltered (fp, "catch vfork");
6262}
6263
ce78b96d
JB
6264/* The breakpoint_ops structure to be used in vfork catchpoints. */
6265
6266static struct breakpoint_ops catch_vfork_breakpoint_ops =
6267{
6268 insert_catch_vfork,
6269 remove_catch_vfork,
6270 breakpoint_hit_catch_vfork,
e09342b5 6271 NULL, /* resources_needed */
ce78b96d
JB
6272 print_it_catch_vfork,
6273 print_one_catch_vfork,
6149aea9
PA
6274 print_mention_catch_vfork,
6275 print_recreate_catch_vfork
ce78b96d
JB
6276};
6277
a96d9b2e
SDJ
6278/* Implement the "insert" breakpoint_ops method for syscall
6279 catchpoints. */
6280
77b06cd7
TJB
6281static int
6282insert_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6283{
6284 struct inferior *inf = current_inferior ();
6285
6286 ++inf->total_syscalls_count;
77b06cd7 6287 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6288 ++inf->any_syscall_count;
6289 else
6290 {
6291 int i, iter;
cc59ec59 6292
a96d9b2e 6293 for (i = 0;
77b06cd7 6294 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6295 i++)
6296 {
6297 int elem;
cc59ec59 6298
a96d9b2e
SDJ
6299 if (iter >= VEC_length (int, inf->syscalls_counts))
6300 {
6301 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6302 uintptr_t vec_addr_offset
6303 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6304 uintptr_t vec_addr;
6305 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6306 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6307 vec_addr_offset;
6308 memset ((void *) vec_addr, 0,
6309 (iter + 1 - old_size) * sizeof (int));
6310 }
6311 elem = VEC_index (int, inf->syscalls_counts, iter);
6312 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6313 }
6314 }
6315
77b06cd7
TJB
6316 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6317 inf->total_syscalls_count != 0,
6318 inf->any_syscall_count,
6319 VEC_length (int, inf->syscalls_counts),
6320 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6321}
6322
6323/* Implement the "remove" breakpoint_ops method for syscall
6324 catchpoints. */
6325
6326static int
77b06cd7 6327remove_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6328{
6329 struct inferior *inf = current_inferior ();
6330
6331 --inf->total_syscalls_count;
77b06cd7 6332 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6333 --inf->any_syscall_count;
6334 else
6335 {
6336 int i, iter;
cc59ec59 6337
a96d9b2e 6338 for (i = 0;
77b06cd7 6339 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6340 i++)
6341 {
6342 int elem;
6343 if (iter >= VEC_length (int, inf->syscalls_counts))
6344 /* Shouldn't happen. */
6345 continue;
6346 elem = VEC_index (int, inf->syscalls_counts, iter);
6347 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6348 }
6349 }
6350
6351 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6352 inf->total_syscalls_count != 0,
6353 inf->any_syscall_count,
6354 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6355 VEC_address (int,
6356 inf->syscalls_counts));
a96d9b2e
SDJ
6357}
6358
6359/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6360 catchpoints. */
6361
6362static int
6363breakpoint_hit_catch_syscall (struct breakpoint *b)
6364{
4a64f543
MS
6365 /* We must check if we are catching specific syscalls in this
6366 breakpoint. If we are, then we must guarantee that the called
6367 syscall is the same syscall we are catching. */
a96d9b2e
SDJ
6368 int syscall_number = 0;
6369
6370 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6371 return 0;
6372
6373 /* Now, checking if the syscall is the same. */
6374 if (b->syscalls_to_be_caught)
6375 {
6376 int i, iter;
cc59ec59 6377
a96d9b2e
SDJ
6378 for (i = 0;
6379 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6380 i++)
6381 if (syscall_number == iter)
6382 break;
6383 /* Not the same. */
6384 if (!iter)
6385 return 0;
6386 }
6387
6388 return 1;
6389}
6390
6391/* Implement the "print_it" breakpoint_ops method for syscall
6392 catchpoints. */
6393
6394static enum print_stop_action
6395print_it_catch_syscall (struct breakpoint *b)
6396{
6397 /* These are needed because we want to know in which state a
6398 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6399 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6400 must print "called syscall" or "returned from syscall". */
6401 ptid_t ptid;
6402 struct target_waitstatus last;
6403 struct syscall s;
6404 struct cleanup *old_chain;
6405 char *syscall_id;
6406
6407 get_last_target_status (&ptid, &last);
6408
6409 get_syscall_by_number (last.value.syscall_number, &s);
6410
6411 annotate_catchpoint (b->number);
6412
6413 if (s.name == NULL)
6414 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6415 else
6416 syscall_id = xstrprintf ("'%s'", s.name);
6417
6418 old_chain = make_cleanup (xfree, syscall_id);
6419
6420 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6421 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6422 b->number, syscall_id);
6423 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6424 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6425 b->number, syscall_id);
6426
6427 do_cleanups (old_chain);
6428
6429 return PRINT_SRC_AND_LOC;
6430}
6431
6432/* Implement the "print_one" breakpoint_ops method for syscall
6433 catchpoints. */
6434
6435static void
6436print_one_catch_syscall (struct breakpoint *b,
6437 struct bp_location **last_loc)
6438{
6439 struct value_print_options opts;
6440
6441 get_user_print_options (&opts);
4a64f543
MS
6442 /* Field 4, the address, is omitted (which makes the columns not
6443 line up too nicely with the headers, but the effect is relatively
6444 readable). */
a96d9b2e
SDJ
6445 if (opts.addressprint)
6446 ui_out_field_skip (uiout, "addr");
6447 annotate_field (5);
6448
6449 if (b->syscalls_to_be_caught
6450 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6451 ui_out_text (uiout, "syscalls \"");
6452 else
6453 ui_out_text (uiout, "syscall \"");
6454
6455 if (b->syscalls_to_be_caught)
6456 {
6457 int i, iter;
6458 char *text = xstrprintf ("%s", "");
cc59ec59 6459
a96d9b2e
SDJ
6460 for (i = 0;
6461 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6462 i++)
6463 {
6464 char *x = text;
6465 struct syscall s;
6466 get_syscall_by_number (iter, &s);
6467
6468 if (s.name != NULL)
6469 text = xstrprintf ("%s%s, ", text, s.name);
6470 else
6471 text = xstrprintf ("%s%d, ", text, iter);
6472
6473 /* We have to xfree the last 'text' (now stored at 'x')
6474 because xstrprintf dinamically allocates new space for it
6475 on every call. */
6476 xfree (x);
6477 }
6478 /* Remove the last comma. */
6479 text[strlen (text) - 2] = '\0';
6480 ui_out_field_string (uiout, "what", text);
6481 }
6482 else
6483 ui_out_field_string (uiout, "what", "<any syscall>");
6484 ui_out_text (uiout, "\" ");
6485}
6486
6487/* Implement the "print_mention" breakpoint_ops method for syscall
6488 catchpoints. */
6489
6490static void
6491print_mention_catch_syscall (struct breakpoint *b)
6492{
6493 if (b->syscalls_to_be_caught)
6494 {
6495 int i, iter;
6496
6497 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6498 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6499 else
6500 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6501
6502 for (i = 0;
6503 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6504 i++)
6505 {
6506 struct syscall s;
6507 get_syscall_by_number (iter, &s);
6508
6509 if (s.name)
6510 printf_filtered (" '%s' [%d]", s.name, s.number);
6511 else
6512 printf_filtered (" %d", s.number);
6513 }
6514 printf_filtered (")");
6515 }
6516 else
6517 printf_filtered (_("Catchpoint %d (any syscall)"),
6518 b->number);
6519}
6520
6149aea9
PA
6521/* Implement the "print_recreate" breakpoint_ops method for syscall
6522 catchpoints. */
6523
6524static void
6525print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6526{
6527 fprintf_unfiltered (fp, "catch syscall");
6528
6529 if (b->syscalls_to_be_caught)
6530 {
6531 int i, iter;
6532
6533 for (i = 0;
6534 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6535 i++)
6536 {
6537 struct syscall s;
6538
6539 get_syscall_by_number (iter, &s);
6540 if (s.name)
6541 fprintf_unfiltered (fp, " %s", s.name);
6542 else
6543 fprintf_unfiltered (fp, " %d", s.number);
6544 }
6545 }
6546}
6547
a96d9b2e
SDJ
6548/* The breakpoint_ops structure to be used in syscall catchpoints. */
6549
6550static struct breakpoint_ops catch_syscall_breakpoint_ops =
6551{
6552 insert_catch_syscall,
6553 remove_catch_syscall,
6554 breakpoint_hit_catch_syscall,
e09342b5 6555 NULL, /* resources_needed */
a96d9b2e
SDJ
6556 print_it_catch_syscall,
6557 print_one_catch_syscall,
6149aea9
PA
6558 print_mention_catch_syscall,
6559 print_recreate_catch_syscall
a96d9b2e
SDJ
6560};
6561
6562/* Returns non-zero if 'b' is a syscall catchpoint. */
6563
6564static int
6565syscall_catchpoint_p (struct breakpoint *b)
6566{
6567 return (b->ops == &catch_syscall_breakpoint_ops);
6568}
6569
6570/* Create a new breakpoint of the bp_catchpoint kind and return it,
6571 but does NOT mention it nor update the global location list.
6572 This is useful if you need to fill more fields in the
6573 struct breakpoint before calling mention.
ce78b96d
JB
6574
6575 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6576 If COND_STRING is not NULL, then store it in the breakpoint.
6577 OPS, if not NULL, is the breakpoint_ops structure associated
6578 to the catchpoint. */
6579
6580static struct breakpoint *
a96d9b2e
SDJ
6581create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6582 char *cond_string,
6583 struct breakpoint_ops *ops)
c906108c 6584{
c5aa993b
JM
6585 struct symtab_and_line sal;
6586 struct breakpoint *b;
c5aa993b 6587
fe39c653 6588 init_sal (&sal);
6c95b8df 6589 sal.pspace = current_program_space;
c5aa993b 6590
a6d9a66e 6591 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6592 set_breakpoint_count (breakpoint_count + 1);
6593 b->number = breakpoint_count;
ce78b96d 6594
1b36a34b 6595 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6596 b->thread = -1;
c906108c 6597 b->addr_string = NULL;
b5de0fa7
EZ
6598 b->enable_state = bp_enabled;
6599 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6600 b->ops = ops;
c5aa993b 6601
a96d9b2e
SDJ
6602 return b;
6603}
6604
6605/* Create a new breakpoint of the bp_catchpoint kind and return it.
6606
6607 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6608 If COND_STRING is not NULL, then store it in the breakpoint.
6609 OPS, if not NULL, is the breakpoint_ops structure associated
6610 to the catchpoint. */
6611
6612static struct breakpoint *
6613create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6614 char *cond_string, struct breakpoint_ops *ops)
6615{
6616 struct breakpoint *b =
6617 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6618
c906108c 6619 mention (b);
ce78b96d 6620 update_global_location_list (1);
c906108c 6621
ce78b96d 6622 return b;
c906108c 6623}
c5aa993b 6624
9b70b993 6625static void
a6d9a66e
UW
6626create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6627 int tempflag, char *cond_string,
ce78b96d 6628 struct breakpoint_ops *ops)
c906108c 6629{
a6d9a66e
UW
6630 struct breakpoint *b
6631 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6632
6633 /* FIXME: We should put this information in a breakpoint private data
6634 area. */
6635 b->forked_inferior_pid = null_ptid;
c906108c
SS
6636}
6637
fe798b75
JB
6638/* Exec catchpoints. */
6639
77b06cd7
TJB
6640static int
6641insert_catch_exec (struct bp_location *bl)
c906108c 6642{
77b06cd7 6643 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6644}
c906108c 6645
fe798b75 6646static int
77b06cd7 6647remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6648{
6649 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6650}
c906108c 6651
fe798b75
JB
6652static int
6653breakpoint_hit_catch_exec (struct breakpoint *b)
6654{
6655 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6656}
c906108c 6657
fe798b75
JB
6658static enum print_stop_action
6659print_it_catch_exec (struct breakpoint *b)
6660{
6661 annotate_catchpoint (b->number);
6662 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6663 b->exec_pathname);
6664 return PRINT_SRC_AND_LOC;
c906108c
SS
6665}
6666
fe798b75 6667static void
a6d9a66e 6668print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6669{
6670 struct value_print_options opts;
6671
6672 get_user_print_options (&opts);
6673
6674 /* Field 4, the address, is omitted (which makes the columns
6675 not line up too nicely with the headers, but the effect
6676 is relatively readable). */
6677 if (opts.addressprint)
6678 ui_out_field_skip (uiout, "addr");
6679 annotate_field (5);
6680 ui_out_text (uiout, "exec");
6681 if (b->exec_pathname != NULL)
6682 {
6683 ui_out_text (uiout, ", program \"");
6684 ui_out_field_string (uiout, "what", b->exec_pathname);
6685 ui_out_text (uiout, "\" ");
6686 }
6687}
6688
6689static void
6690print_mention_catch_exec (struct breakpoint *b)
6691{
6692 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6693}
6694
6149aea9
PA
6695/* Implement the "print_recreate" breakpoint_ops method for exec
6696 catchpoints. */
6697
6698static void
6699print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6700{
6701 fprintf_unfiltered (fp, "catch exec");
6702}
6703
fe798b75
JB
6704static struct breakpoint_ops catch_exec_breakpoint_ops =
6705{
6706 insert_catch_exec,
6707 remove_catch_exec,
6708 breakpoint_hit_catch_exec,
e09342b5 6709 NULL, /* resources_needed */
fe798b75
JB
6710 print_it_catch_exec,
6711 print_one_catch_exec,
6149aea9
PA
6712 print_mention_catch_exec,
6713 print_recreate_catch_exec
fe798b75
JB
6714};
6715
a96d9b2e
SDJ
6716static void
6717create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6718 struct breakpoint_ops *ops)
6719{
6720 struct gdbarch *gdbarch = get_current_arch ();
6721 struct breakpoint *b =
6722 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6723
6724 b->syscalls_to_be_caught = filter;
6725
6726 /* Now, we have to mention the breakpoint and update the global
6727 location list. */
6728 mention (b);
6729 update_global_location_list (1);
6730}
6731
c906108c 6732static int
fba45db2 6733hw_breakpoint_used_count (void)
c906108c 6734{
52f0bd74 6735 struct breakpoint *b;
c906108c
SS
6736 int i = 0;
6737
6738 ALL_BREAKPOINTS (b)
c5aa993b 6739 {
d6b74ac4 6740 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
6741 i++;
6742 }
c906108c
SS
6743
6744 return i;
6745}
6746
6747static int
fba45db2 6748hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6749{
c906108c 6750 int i = 0;
e09342b5
TJB
6751 struct breakpoint *b;
6752 struct bp_location *bl;
c906108c
SS
6753
6754 *other_type_used = 0;
6755 ALL_BREAKPOINTS (b)
e09342b5
TJB
6756 {
6757 if (!breakpoint_enabled (b))
6758 continue;
6759
c5aa993b 6760 if (b->type == type)
e09342b5
TJB
6761 for (bl = b->loc; bl; bl = bl->next)
6762 {
6763 /* Special types of hardware watchpoints may use more than
6764 one register. */
6765 if (b->ops && b->ops->resources_needed)
6766 i += b->ops->resources_needed (bl);
6767 else
6768 i++;
6769 }
cc60f2e3 6770 else if (is_hardware_watchpoint (b))
c5aa993b 6771 *other_type_used = 1;
e09342b5
TJB
6772 }
6773
c906108c
SS
6774 return i;
6775}
6776
c906108c 6777void
fba45db2 6778disable_watchpoints_before_interactive_call_start (void)
c906108c 6779{
c5aa993b 6780 struct breakpoint *b;
c906108c
SS
6781
6782 ALL_BREAKPOINTS (b)
c5aa993b 6783 {
cc60f2e3 6784 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6785 {
b5de0fa7 6786 b->enable_state = bp_call_disabled;
b60e7edf 6787 update_global_location_list (0);
c5aa993b
JM
6788 }
6789 }
c906108c
SS
6790}
6791
6792void
fba45db2 6793enable_watchpoints_after_interactive_call_stop (void)
c906108c 6794{
c5aa993b 6795 struct breakpoint *b;
c906108c
SS
6796
6797 ALL_BREAKPOINTS (b)
c5aa993b 6798 {
cc60f2e3 6799 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6800 {
b5de0fa7 6801 b->enable_state = bp_enabled;
b60e7edf 6802 update_global_location_list (1);
c5aa993b
JM
6803 }
6804 }
c906108c
SS
6805}
6806
8bea4e01
UW
6807void
6808disable_breakpoints_before_startup (void)
6809{
6810 struct breakpoint *b;
6811 int found = 0;
6812
6813 ALL_BREAKPOINTS (b)
6814 {
6c95b8df
PA
6815 if (b->pspace != current_program_space)
6816 continue;
6817
8bea4e01
UW
6818 if ((b->type == bp_breakpoint
6819 || b->type == bp_hardware_breakpoint)
6820 && breakpoint_enabled (b))
6821 {
6822 b->enable_state = bp_startup_disabled;
6823 found = 1;
6824 }
6825 }
6826
6827 if (found)
6828 update_global_location_list (0);
6829
6c95b8df 6830 current_program_space->executing_startup = 1;
8bea4e01
UW
6831}
6832
6833void
6834enable_breakpoints_after_startup (void)
6835{
6836 struct breakpoint *b;
6837 int found = 0;
6838
6c95b8df 6839 current_program_space->executing_startup = 0;
8bea4e01
UW
6840
6841 ALL_BREAKPOINTS (b)
6842 {
6c95b8df
PA
6843 if (b->pspace != current_program_space)
6844 continue;
6845
8bea4e01
UW
6846 if ((b->type == bp_breakpoint
6847 || b->type == bp_hardware_breakpoint)
6848 && b->enable_state == bp_startup_disabled)
6849 {
6850 b->enable_state = bp_enabled;
6851 found = 1;
6852 }
6853 }
6854
6855 if (found)
6856 breakpoint_re_set ();
6857}
6858
c906108c
SS
6859
6860/* Set a breakpoint that will evaporate an end of command
6861 at address specified by SAL.
6862 Restrict it to frame FRAME if FRAME is nonzero. */
6863
6864struct breakpoint *
a6d9a66e
UW
6865set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6866 struct frame_id frame_id, enum bptype type)
c906108c 6867{
52f0bd74 6868 struct breakpoint *b;
edb3359d
DJ
6869
6870 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6871 one. */
6872 gdb_assert (!frame_id_inlined_p (frame_id));
6873
a6d9a66e 6874 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
6875 b->enable_state = bp_enabled;
6876 b->disposition = disp_donttouch;
818dd999 6877 b->frame_id = frame_id;
c906108c 6878
4a64f543
MS
6879 /* If we're debugging a multi-threaded program, then we want
6880 momentary breakpoints to be active in only a single thread of
6881 control. */
39f77062
KB
6882 if (in_thread_list (inferior_ptid))
6883 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6884
b60e7edf 6885 update_global_location_list_nothrow (1);
74960c60 6886
c906108c
SS
6887 return b;
6888}
611c83ae 6889
e58b0e63
PA
6890/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6891 ORIG is NULL. */
6892
6893struct breakpoint *
6894clone_momentary_breakpoint (struct breakpoint *orig)
6895{
6896 struct breakpoint *copy;
6897
6898 /* If there's nothing to clone, then return nothing. */
6899 if (orig == NULL)
6900 return NULL;
6901
a6d9a66e 6902 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63
PA
6903 copy->loc = allocate_bp_location (copy);
6904 set_breakpoint_location_function (copy->loc);
6905
a6d9a66e 6906 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
6907 copy->loc->requested_address = orig->loc->requested_address;
6908 copy->loc->address = orig->loc->address;
6909 copy->loc->section = orig->loc->section;
6c95b8df 6910 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
6911
6912 if (orig->source_file == NULL)
6913 copy->source_file = NULL;
6914 else
6915 copy->source_file = xstrdup (orig->source_file);
6916
6917 copy->line_number = orig->line_number;
6918 copy->frame_id = orig->frame_id;
6919 copy->thread = orig->thread;
6c95b8df 6920 copy->pspace = orig->pspace;
e58b0e63
PA
6921
6922 copy->enable_state = bp_enabled;
6923 copy->disposition = disp_donttouch;
6924 copy->number = internal_breakpoint_number--;
6925
6926 update_global_location_list_nothrow (0);
6927 return copy;
6928}
6929
611c83ae 6930struct breakpoint *
a6d9a66e
UW
6931set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
6932 enum bptype type)
611c83ae
PA
6933{
6934 struct symtab_and_line sal;
6935
6936 sal = find_pc_line (pc, 0);
6937 sal.pc = pc;
6938 sal.section = find_pc_overlay (pc);
6939 sal.explicit_pc = 1;
6940
a6d9a66e 6941 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 6942}
c906108c 6943\f
c5aa993b 6944
c906108c
SS
6945/* Tell the user we have just set a breakpoint B. */
6946
6947static void
fba45db2 6948mention (struct breakpoint *b)
c906108c
SS
6949{
6950 int say_where = 0;
fa8a61dc 6951 struct cleanup *ui_out_chain;
79a45b7d
TT
6952 struct value_print_options opts;
6953
6954 get_user_print_options (&opts);
8b93c638 6955
9a4105ab
AC
6956 /* FIXME: This is misplaced; mention() is called by things (like
6957 hitting a watchpoint) other than breakpoint creation. It should
6958 be possible to clean this up and at the same time replace the
7f4b89d1 6959 random calls to breakpoint_changed with this hook. */
383f836e 6960 observer_notify_breakpoint_created (b->number);
c906108c 6961
3086aeae
DJ
6962 if (b->ops != NULL && b->ops->print_mention != NULL)
6963 b->ops->print_mention (b);
6964 else
6965 switch (b->type)
6966 {
6967 case bp_none:
3e43a32a
MS
6968 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
6969 b->number);
3086aeae
DJ
6970 break;
6971 case bp_watchpoint:
6972 ui_out_text (uiout, "Watchpoint ");
6973 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6974 ui_out_field_int (uiout, "number", b->number);
6975 ui_out_text (uiout, ": ");
fa8a61dc 6976 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6977 do_cleanups (ui_out_chain);
6978 break;
6979 case bp_hardware_watchpoint:
6980 ui_out_text (uiout, "Hardware watchpoint ");
6981 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
6982 ui_out_field_int (uiout, "number", b->number);
6983 ui_out_text (uiout, ": ");
fa8a61dc 6984 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6985 do_cleanups (ui_out_chain);
6986 break;
6987 case bp_read_watchpoint:
6988 ui_out_text (uiout, "Hardware read watchpoint ");
6989 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
6990 ui_out_field_int (uiout, "number", b->number);
6991 ui_out_text (uiout, ": ");
fa8a61dc 6992 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
6993 do_cleanups (ui_out_chain);
6994 break;
6995 case bp_access_watchpoint:
6996 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
6997 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
6998 ui_out_field_int (uiout, "number", b->number);
6999 ui_out_text (uiout, ": ");
fa8a61dc 7000 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7001 do_cleanups (ui_out_chain);
7002 break;
7003 case bp_breakpoint:
7004 if (ui_out_is_mi_like_p (uiout))
7005 {
7006 say_where = 0;
7007 break;
7008 }
2cec12e5
AR
7009 if (b->disposition == disp_del)
7010 printf_filtered (_("Temporary breakpoint"));
7011 else
7012 printf_filtered (_("Breakpoint"));
7013 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
7014 say_where = 1;
7015 break;
7016 case bp_hardware_breakpoint:
7017 if (ui_out_is_mi_like_p (uiout))
7018 {
7019 say_where = 0;
7020 break;
7021 }
a3f17187 7022 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7023 say_where = 1;
7024 break;
1042e4c0
SS
7025 case bp_tracepoint:
7026 if (ui_out_is_mi_like_p (uiout))
7027 {
7028 say_where = 0;
7029 break;
7030 }
7031 printf_filtered (_("Tracepoint"));
7032 printf_filtered (_(" %d"), b->number);
7033 say_where = 1;
7034 break;
7a697b8d
SS
7035 case bp_fast_tracepoint:
7036 if (ui_out_is_mi_like_p (uiout))
7037 {
7038 say_where = 0;
7039 break;
7040 }
7041 printf_filtered (_("Fast tracepoint"));
7042 printf_filtered (_(" %d"), b->number);
7043 say_where = 1;
7044 break;
0fb4aa4b
PA
7045 case bp_static_tracepoint:
7046 if (ui_out_is_mi_like_p (uiout))
7047 {
7048 say_where = 0;
7049 break;
7050 }
7051 printf_filtered (_("Static tracepoint"));
7052 printf_filtered (_(" %d"), b->number);
7053 say_where = 1;
7054 break;
3086aeae
DJ
7055
7056 case bp_until:
7057 case bp_finish:
7058 case bp_longjmp:
7059 case bp_longjmp_resume:
186c406b
TT
7060 case bp_exception:
7061 case bp_exception_resume:
3086aeae 7062 case bp_step_resume:
3086aeae 7063 case bp_call_dummy:
aa7d318d 7064 case bp_std_terminate:
3086aeae
DJ
7065 case bp_watchpoint_scope:
7066 case bp_shlib_event:
7067 case bp_thread_event:
7068 case bp_overlay_event:
4efc6507 7069 case bp_jit_event:
0fd8e87f 7070 case bp_longjmp_master:
aa7d318d 7071 case bp_std_terminate_master:
186c406b 7072 case bp_exception_master:
3086aeae
DJ
7073 break;
7074 }
c906108c 7075
c906108c
SS
7076 if (say_where)
7077 {
a3f17187
AC
7078 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7079 single string. */
0d381245 7080 if (b->loc == NULL)
c906108c 7081 {
a3f17187 7082 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7083 }
7084 else
7085 {
79a45b7d 7086 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7087 {
7088 printf_filtered (" at ");
5af949e3
UW
7089 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7090 gdb_stdout);
0101ce28
JJ
7091 }
7092 if (b->source_file)
7093 printf_filtered (": file %s, line %d.",
7094 b->source_file, b->line_number);
0d381245
VP
7095
7096 if (b->loc->next)
7097 {
7098 struct bp_location *loc = b->loc;
7099 int n = 0;
7100 for (; loc; loc = loc->next)
7101 ++n;
7102 printf_filtered (" (%d locations)", n);
7103 }
7104
c906108c 7105 }
c906108c 7106 }
9dc5e2a9 7107 if (ui_out_is_mi_like_p (uiout))
fb40c209 7108 return;
c906108c
SS
7109 printf_filtered ("\n");
7110}
c906108c 7111\f
c5aa993b 7112
0d381245 7113static struct bp_location *
39d61571 7114add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7115 const struct symtab_and_line *sal)
7116{
7117 struct bp_location *loc, **tmp;
7118
39d61571 7119 loc = allocate_bp_location (b);
0d381245
VP
7120 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7121 ;
7122 *tmp = loc;
a6d9a66e
UW
7123 loc->gdbarch = get_sal_arch (*sal);
7124 if (!loc->gdbarch)
7125 loc->gdbarch = b->gdbarch;
0d381245 7126 loc->requested_address = sal->pc;
a6d9a66e
UW
7127 loc->address = adjust_breakpoint_address (loc->gdbarch,
7128 loc->requested_address, b->type);
6c95b8df
PA
7129 loc->pspace = sal->pspace;
7130 gdb_assert (loc->pspace != NULL);
0d381245
VP
7131 loc->section = sal->section;
7132
7133 set_breakpoint_location_function (loc);
7134 return loc;
7135}
514f746b
AR
7136\f
7137
7138/* Return 1 if LOC is pointing to a permanent breakpoint,
7139 return 0 otherwise. */
7140
7141static int
7142bp_loc_is_permanent (struct bp_location *loc)
7143{
7144 int len;
7145 CORE_ADDR addr;
7146 const gdb_byte *brk;
7147 gdb_byte *target_mem;
939c61fa
JK
7148 struct cleanup *cleanup;
7149 int retval = 0;
514f746b
AR
7150
7151 gdb_assert (loc != NULL);
7152
7153 addr = loc->address;
a6d9a66e 7154 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7155
939c61fa
JK
7156 /* Software breakpoints unsupported? */
7157 if (brk == NULL)
7158 return 0;
7159
514f746b
AR
7160 target_mem = alloca (len);
7161
939c61fa
JK
7162 /* Enable the automatic memory restoration from breakpoints while
7163 we read the memory. Otherwise we could say about our temporary
7164 breakpoints they are permanent. */
6c95b8df
PA
7165 cleanup = save_current_space_and_thread ();
7166
7167 switch_to_program_space_and_thread (loc->pspace);
7168 make_show_memory_breakpoints_cleanup (0);
939c61fa 7169
514f746b
AR
7170 if (target_read_memory (loc->address, target_mem, len) == 0
7171 && memcmp (target_mem, brk, len) == 0)
939c61fa 7172 retval = 1;
514f746b 7173
939c61fa
JK
7174 do_cleanups (cleanup);
7175
7176 return retval;
514f746b
AR
7177}
7178
7179
c3f6f71d 7180
018d34a4
VP
7181/* Create a breakpoint with SAL as location. Use ADDR_STRING
7182 as textual description of the location, and COND_STRING
db107f19 7183 as condition expression. */
018d34a4
VP
7184
7185static void
8cdf0e15
VP
7186create_breakpoint_sal (struct gdbarch *gdbarch,
7187 struct symtabs_and_lines sals, char *addr_string,
7188 char *cond_string,
7189 enum bptype type, enum bpdisp disposition,
7190 int thread, int task, int ignore_count,
84f4c1fe
PM
7191 struct breakpoint_ops *ops, int from_tty,
7192 int enabled, int internal)
018d34a4 7193{
0d381245
VP
7194 struct breakpoint *b = NULL;
7195 int i;
018d34a4
VP
7196
7197 if (type == bp_hardware_breakpoint)
7198 {
7199 int i = hw_breakpoint_used_count ();
7200 int target_resources_ok =
d92524f1 7201 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7202 i + 1, 0);
7203 if (target_resources_ok == 0)
7204 error (_("No hardware breakpoint support in the target."));
7205 else if (target_resources_ok < 0)
7206 error (_("Hardware breakpoints used exceeds limit."));
7207 }
7208
6c95b8df
PA
7209 gdb_assert (sals.nelts > 0);
7210
0d381245
VP
7211 for (i = 0; i < sals.nelts; ++i)
7212 {
7213 struct symtab_and_line sal = sals.sals[i];
7214 struct bp_location *loc;
7215
7216 if (from_tty)
5af949e3
UW
7217 {
7218 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7219 if (!loc_gdbarch)
7220 loc_gdbarch = gdbarch;
7221
7222 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7223 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7224 }
0d381245
VP
7225
7226 if (i == 0)
7227 {
a6d9a66e 7228 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7229 set_breakpoint_number (internal, b);
0d381245 7230 b->thread = thread;
4a306c9a 7231 b->task = task;
018d34a4 7232
0d381245
VP
7233 b->cond_string = cond_string;
7234 b->ignore_count = ignore_count;
41447f92 7235 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7236 b->disposition = disposition;
6c95b8df
PA
7237 b->pspace = sals.sals[0].pspace;
7238
0fb4aa4b
PA
7239 if (type == bp_static_tracepoint)
7240 {
7241 struct static_tracepoint_marker marker;
7242
7243 if (is_marker_spec (addr_string))
7244 {
7245 /* We already know the marker exists, otherwise, we
7246 wouldn't see a sal for it. */
7247 char *p = &addr_string[3];
7248 char *endp;
7249 char *marker_str;
7250 int i;
7251
e9cafbcc 7252 p = skip_spaces (p);
0fb4aa4b 7253
e9cafbcc 7254 endp = skip_to_space (p);
0fb4aa4b
PA
7255
7256 marker_str = savestring (p, endp - p);
7257 b->static_trace_marker_id = marker_str;
7258
3e43a32a
MS
7259 printf_filtered (_("Probed static tracepoint "
7260 "marker \"%s\"\n"),
0fb4aa4b
PA
7261 b->static_trace_marker_id);
7262 }
7263 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7264 {
7265 b->static_trace_marker_id = xstrdup (marker.str_id);
7266 release_static_tracepoint_marker (&marker);
7267
3e43a32a
MS
7268 printf_filtered (_("Probed static tracepoint "
7269 "marker \"%s\"\n"),
0fb4aa4b
PA
7270 b->static_trace_marker_id);
7271 }
7272 else
3e43a32a
MS
7273 warning (_("Couldn't determine the static "
7274 "tracepoint marker to probe"));
0fb4aa4b
PA
7275 }
7276
6c95b8df 7277 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7278 && (b->type == bp_breakpoint
7279 || b->type == bp_hardware_breakpoint))
7280 b->enable_state = bp_startup_disabled;
7281
0d381245
VP
7282 loc = b->loc;
7283 }
7284 else
018d34a4 7285 {
39d61571 7286 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7287 }
7288
514f746b
AR
7289 if (bp_loc_is_permanent (loc))
7290 make_breakpoint_permanent (b);
7291
0d381245
VP
7292 if (b->cond_string)
7293 {
7294 char *arg = b->cond_string;
d32a6982 7295 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7296 if (*arg)
db107f19 7297 error (_("Garbage %s follows condition"), arg);
018d34a4 7298 }
0d381245 7299 }
018d34a4
VP
7300
7301 if (addr_string)
7302 b->addr_string = addr_string;
7303 else
7304 /* addr_string has to be used or breakpoint_re_set will delete
7305 me. */
5af949e3
UW
7306 b->addr_string
7307 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7308
604133b5 7309 b->ops = ops;
84f4c1fe
PM
7310 if (internal)
7311 /* Do not mention breakpoints with a negative number, but do
7312 notify observers. */
7313 observer_notify_breakpoint_created (b->number);
7314 else
7315 mention (b);
018d34a4
VP
7316}
7317
ed0616c6
VP
7318/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7319 elements to fill the void space. */
2c0b251b
PA
7320static void
7321remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7322{
7323 int i = index_to_remove+1;
7324 int last_index = sal->nelts-1;
7325
7326 for (;i <= last_index; ++i)
7327 sal->sals[i-1] = sal->sals[i];
7328
7329 --(sal->nelts);
7330}
7331
6c95b8df
PA
7332/* If appropriate, obtains all sals that correspond to the same file
7333 and line as SAL, in all program spaces. Users debugging with IDEs,
7334 will want to set a breakpoint at foo.c:line, and not really care
7335 about program spaces. This is done only if SAL does not have
7336 explicit PC and has line and file information. If we got just a
7337 single expanded sal, return the original.
ed0616c6 7338
6c95b8df
PA
7339 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7340 which the name of enclosing function is different from SAL. This
7341 makes sure that if we have breakpoint originally set in template
7342 instantiation, say foo<int>(), we won't expand SAL to locations at
7343 the same line in all existing instantiations of 'foo'. */
ed0616c6 7344
2c0b251b 7345static struct symtabs_and_lines
ed0616c6
VP
7346expand_line_sal_maybe (struct symtab_and_line sal)
7347{
7348 struct symtabs_and_lines expanded;
7349 CORE_ADDR original_pc = sal.pc;
7350 char *original_function = NULL;
7351 int found;
7352 int i;
6c95b8df 7353 struct cleanup *old_chain;
ed0616c6
VP
7354
7355 /* If we have explicit pc, don't expand.
7356 If we have no line number, we can't expand. */
7357 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7358 {
7359 expanded.nelts = 1;
7360 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7361 expanded.sals[0] = sal;
7362 return expanded;
7363 }
7364
7365 sal.pc = 0;
6c95b8df
PA
7366
7367 old_chain = save_current_space_and_thread ();
7368
7369 switch_to_program_space_and_thread (sal.pspace);
7370
ed0616c6 7371 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7372
7373 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7374 expanded = expand_line_sal (sal);
6c95b8df 7375
ed0616c6
VP
7376 if (expanded.nelts == 1)
7377 {
3dba1c98
JB
7378 /* We had one sal, we got one sal. Return that sal, adjusting it
7379 past the function prologue if necessary. */
ed0616c6
VP
7380 xfree (expanded.sals);
7381 expanded.nelts = 1;
7382 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7383 sal.pc = original_pc;
7384 expanded.sals[0] = sal;
3dba1c98 7385 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7386 do_cleanups (old_chain);
ed0616c6
VP
7387 return expanded;
7388 }
7389
7390 if (!sal.explicit_line)
7391 {
7392 CORE_ADDR func_addr, func_end;
7393 for (i = 0; i < expanded.nelts; ++i)
7394 {
7395 CORE_ADDR pc = expanded.sals[i].pc;
7396 char *this_function;
6c95b8df
PA
7397
7398 /* We need to switch threads as well since we're about to
7399 read memory. */
7400 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7401
ed0616c6
VP
7402 if (find_pc_partial_function (pc, &this_function,
7403 &func_addr, &func_end))
7404 {
059fb39f
PM
7405 if (this_function
7406 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7407 {
7408 remove_sal (&expanded, i);
7409 --i;
7410 }
ed0616c6
VP
7411 }
7412 }
7413 }
059acae7
UW
7414
7415 /* Skip the function prologue if necessary. */
7416 for (i = 0; i < expanded.nelts; ++i)
7417 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7418
6c95b8df
PA
7419 do_cleanups (old_chain);
7420
ed0616c6
VP
7421 if (expanded.nelts <= 1)
7422 {
4a64f543
MS
7423 /* This is un ugly workaround. If we get zero expanded sals
7424 then something is really wrong. Fix that by returning the
7425 original sal. */
7426
ed0616c6
VP
7427 xfree (expanded.sals);
7428 expanded.nelts = 1;
7429 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7430 sal.pc = original_pc;
7431 expanded.sals[0] = sal;
7432 return expanded;
7433 }
7434
7435 if (original_pc)
7436 {
7437 found = 0;
7438 for (i = 0; i < expanded.nelts; ++i)
7439 if (expanded.sals[i].pc == original_pc)
7440 {
7441 found = 1;
7442 break;
7443 }
7444 gdb_assert (found);
7445 }
7446
7447 return expanded;
7448}
7449
018d34a4
VP
7450/* Add SALS.nelts breakpoints to the breakpoint table. For each
7451 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7452 value. COND_STRING, if not NULL, specified the condition to be
7453 used for all breakpoints. Essentially the only case where
7454 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7455 function. In that case, it's still not possible to specify
7456 separate conditions for different overloaded functions, so
7457 we take just a single condition string.
7458
c3f6f71d 7459 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7460 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7461 array contents). If the function fails (error() is called), the
7462 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7463 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7464
7465static void
8cdf0e15
VP
7466create_breakpoints_sal (struct gdbarch *gdbarch,
7467 struct symtabs_and_lines sals, char **addr_string,
7468 char *cond_string,
7469 enum bptype type, enum bpdisp disposition,
7470 int thread, int task, int ignore_count,
7471 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7472 int enabled, int internal)
c906108c 7473{
018d34a4 7474 int i;
cc59ec59 7475
018d34a4 7476 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7477 {
ed0616c6
VP
7478 struct symtabs_and_lines expanded =
7479 expand_line_sal_maybe (sals.sals[i]);
0d381245 7480
8cdf0e15
VP
7481 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7482 cond_string, type, disposition,
84f4c1fe
PM
7483 thread, task, ignore_count, ops,
7484 from_tty, enabled, internal);
c3f6f71d 7485 }
c3f6f71d 7486}
c906108c 7487
9998af43 7488/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7489 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7490 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7491 address strings. ADDRESS points to the end of the SAL.
7492
7493 The array and the line spec strings are allocated on the heap, it is
7494 the caller's responsibility to free them. */
c906108c 7495
b9362cc7 7496static void
c3f6f71d
JM
7497parse_breakpoint_sals (char **address,
7498 struct symtabs_and_lines *sals,
0101ce28
JJ
7499 char ***addr_string,
7500 int *not_found_ptr)
c3f6f71d
JM
7501{
7502 char *addr_start = *address;
cc59ec59 7503
c3f6f71d
JM
7504 *addr_string = NULL;
7505 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7506 breakpoint. */
c3f6f71d
JM
7507 if ((*address) == NULL
7508 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7509 {
7510 if (default_breakpoint_valid)
7511 {
c3f6f71d 7512 struct symtab_and_line sal;
cc59ec59 7513
4a64f543 7514 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7515 sals->sals = (struct symtab_and_line *)
c906108c
SS
7516 xmalloc (sizeof (struct symtab_and_line));
7517 sal.pc = default_breakpoint_address;
7518 sal.line = default_breakpoint_line;
7519 sal.symtab = default_breakpoint_symtab;
6c95b8df 7520 sal.pspace = default_breakpoint_pspace;
c5aa993b 7521 sal.section = find_pc_overlay (sal.pc);
00903456 7522
4a64f543
MS
7523 /* "break" without arguments is equivalent to "break *PC"
7524 where PC is the default_breakpoint_address. So make sure
7525 to set sal.explicit_pc to prevent GDB from trying to
7526 expand the list of sals to include all other instances
7527 with the same symtab and line. */
00903456
JK
7528 sal.explicit_pc = 1;
7529
c3f6f71d
JM
7530 sals->sals[0] = sal;
7531 sals->nelts = 1;
c906108c
SS
7532 }
7533 else
8a3fe4f8 7534 error (_("No default breakpoint address now."));
c906108c
SS
7535 }
7536 else
7537 {
c906108c 7538 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7539 current_source_symtab (which is decode_line_1's default).
7540 This should produce the results we want almost all of the
7541 time while leaving default_breakpoint_* alone.
7542
1aeae86e
AF
7543 ObjC: However, don't match an Objective-C method name which
7544 may have a '+' or '-' succeeded by a '[' */
0378c332 7545
c214a6fd 7546 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7547
c906108c 7548 if (default_breakpoint_valid
0378c332 7549 && (!cursal.symtab
1aeae86e
AF
7550 || ((strchr ("+-", (*address)[0]) != NULL)
7551 && ((*address)[1] != '['))))
c3f6f71d 7552 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
7553 default_breakpoint_line, addr_string,
7554 not_found_ptr);
c906108c 7555 else
0101ce28
JJ
7556 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7557 addr_string, not_found_ptr);
c906108c 7558 }
4a64f543 7559 /* For any SAL that didn't have a canonical string, fill one in. */
c3f6f71d
JM
7560 if (sals->nelts > 0 && *addr_string == NULL)
7561 *addr_string = xcalloc (sals->nelts, sizeof (char **));
7562 if (addr_start != (*address))
c906108c 7563 {
c3f6f71d 7564 int i;
cc59ec59 7565
c3f6f71d 7566 for (i = 0; i < sals->nelts; i++)
c906108c 7567 {
4a64f543 7568 /* Add the string if not present. */
c3f6f71d 7569 if ((*addr_string)[i] == NULL)
cc59ec59
MS
7570 (*addr_string)[i] = savestring (addr_start,
7571 (*address) - addr_start);
c906108c
SS
7572 }
7573 }
c3f6f71d 7574}
c906108c 7575
c906108c 7576
c3f6f71d 7577/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7578 inserted as a breakpoint. If it can't throw an error. */
c906108c 7579
b9362cc7 7580static void
23e7acfb 7581breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7582{
7583 int i;
cc59ec59 7584
c3f6f71d 7585 for (i = 0; i < sals->nelts; i++)
ee53e872 7586 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7587}
7588
7a697b8d
SS
7589/* Fast tracepoints may have restrictions on valid locations. For
7590 instance, a fast tracepoint using a jump instead of a trap will
7591 likely have to overwrite more bytes than a trap would, and so can
7592 only be placed where the instruction is longer than the jump, or a
7593 multi-instruction sequence does not have a jump into the middle of
7594 it, etc. */
7595
7596static void
7597check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7598 struct symtabs_and_lines *sals)
7599{
7600 int i, rslt;
7601 struct symtab_and_line *sal;
7602 char *msg;
7603 struct cleanup *old_chain;
7604
7605 for (i = 0; i < sals->nelts; i++)
7606 {
7607 sal = &sals->sals[i];
7608
7609 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7610 NULL, &msg);
7611 old_chain = make_cleanup (xfree, msg);
7612
7613 if (!rslt)
7614 error (_("May not have a fast tracepoint at 0x%s%s"),
7615 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7616
7617 do_cleanups (old_chain);
7618 }
7619}
7620
05ff989b 7621static void
0101ce28
JJ
7622do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7623{
7624 struct captured_parse_breakpoint_args *args = data;
7625
7626 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
7627 args->not_found_ptr);
0101ce28
JJ
7628}
7629
018d34a4
VP
7630/* Given TOK, a string specification of condition and thread, as
7631 accepted by the 'break' command, extract the condition
7632 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7633 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7634 If no condition is found, *COND_STRING is set to NULL.
7635 If no thread is found, *THREAD is set to -1. */
7636static void
7637find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7638 char **cond_string, int *thread, int *task)
018d34a4
VP
7639{
7640 *cond_string = NULL;
7641 *thread = -1;
7642 while (tok && *tok)
7643 {
7644 char *end_tok;
7645 int toklen;
7646 char *cond_start = NULL;
7647 char *cond_end = NULL;
cc59ec59 7648
e9cafbcc 7649 tok = skip_spaces (tok);
018d34a4 7650
e9cafbcc 7651 end_tok = skip_to_space (tok);
018d34a4
VP
7652
7653 toklen = end_tok - tok;
7654
7655 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7656 {
f7545552
TT
7657 struct expression *expr;
7658
018d34a4 7659 tok = cond_start = end_tok + 1;
f7545552
TT
7660 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7661 xfree (expr);
018d34a4
VP
7662 cond_end = tok;
7663 *cond_string = savestring (cond_start,
7664 cond_end - cond_start);
7665 }
7666 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7667 {
7668 char *tmptok;
7669
7670 tok = end_tok + 1;
7671 tmptok = tok;
7672 *thread = strtol (tok, &tok, 0);
7673 if (tok == tmptok)
7674 error (_("Junk after thread keyword."));
7675 if (!valid_thread_id (*thread))
7676 error (_("Unknown thread %d."), *thread);
7677 }
4a306c9a
JB
7678 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7679 {
7680 char *tmptok;
7681
7682 tok = end_tok + 1;
7683 tmptok = tok;
7684 *task = strtol (tok, &tok, 0);
7685 if (tok == tmptok)
7686 error (_("Junk after task keyword."));
7687 if (!valid_task_id (*task))
b6199126 7688 error (_("Unknown task %d."), *task);
4a306c9a 7689 }
018d34a4
VP
7690 else
7691 error (_("Junk at end of arguments."));
7692 }
7693}
7694
0fb4aa4b
PA
7695/* Decode a static tracepoint marker spec. */
7696
7697static struct symtabs_and_lines
7698decode_static_tracepoint_spec (char **arg_p)
7699{
7700 VEC(static_tracepoint_marker_p) *markers = NULL;
7701 struct symtabs_and_lines sals;
7702 struct symtab_and_line sal;
7703 struct symbol *sym;
7704 struct cleanup *old_chain;
7705 char *p = &(*arg_p)[3];
7706 char *endp;
7707 char *marker_str;
7708 int i;
7709
e9cafbcc 7710 p = skip_spaces (p);
0fb4aa4b 7711
e9cafbcc 7712 endp = skip_to_space (p);
0fb4aa4b
PA
7713
7714 marker_str = savestring (p, endp - p);
7715 old_chain = make_cleanup (xfree, marker_str);
7716
7717 markers = target_static_tracepoint_markers_by_strid (marker_str);
7718 if (VEC_empty(static_tracepoint_marker_p, markers))
7719 error (_("No known static tracepoint marker named %s"), marker_str);
7720
7721 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7722 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7723
7724 for (i = 0; i < sals.nelts; i++)
7725 {
7726 struct static_tracepoint_marker *marker;
7727
7728 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7729
7730 init_sal (&sals.sals[i]);
7731
7732 sals.sals[i] = find_pc_line (marker->address, 0);
7733 sals.sals[i].pc = marker->address;
7734
7735 release_static_tracepoint_marker (marker);
7736 }
7737
7738 do_cleanups (old_chain);
7739
7740 *arg_p = endp;
7741 return sals;
7742}
7743
fd9b8c24
PA
7744/* Set a breakpoint. This function is shared between CLI and MI
7745 functions for setting a breakpoint. This function has two major
7746 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7747 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7748 breakpoint location, address and thread. Otherwise, ARG is just
7749 the location of breakpoint, with condition and thread specified by
7750 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7751 the breakpoint number will be allocated from the internal
7752 breakpoint count. Returns true if any breakpoint was created;
7753 false otherwise. */
0101ce28 7754
8cdf0e15
VP
7755int
7756create_breakpoint (struct gdbarch *gdbarch,
7757 char *arg, char *cond_string, int thread,
7758 int parse_condition_and_thread,
0fb4aa4b 7759 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7760 int ignore_count,
7761 enum auto_boolean pending_break_support,
7762 struct breakpoint_ops *ops,
84f4c1fe 7763 int from_tty, int enabled, int internal)
c3f6f71d 7764{
71fff37b 7765 struct gdb_exception e;
c3f6f71d 7766 struct symtabs_and_lines sals;
0101ce28 7767 struct symtab_and_line pending_sal;
0101ce28 7768 char *copy_arg;
c3f6f71d
JM
7769 char *addr_start = arg;
7770 char **addr_string;
7771 struct cleanup *old_chain;
80c99de1 7772 struct cleanup *bkpt_chain = NULL;
0101ce28 7773 struct captured_parse_breakpoint_args parse_args;
05ff989b 7774 int i;
0101ce28 7775 int pending = 0;
0101ce28 7776 int not_found = 0;
4a306c9a 7777 int task = 0;
86b17b60 7778 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7779
c3f6f71d
JM
7780 sals.sals = NULL;
7781 sals.nelts = 0;
7782 addr_string = NULL;
c3f6f71d 7783
0101ce28
JJ
7784 parse_args.arg_p = &arg;
7785 parse_args.sals_p = &sals;
7786 parse_args.addr_string_p = &addr_string;
7787 parse_args.not_found_ptr = &not_found;
7788
0fb4aa4b
PA
7789 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7790 {
7791 int i;
7792
7793 sals = decode_static_tracepoint_spec (&arg);
7794
7795 copy_arg = savestring (addr_start, arg - addr_start);
7796 addr_string = xcalloc (sals.nelts, sizeof (char **));
7797 for (i = 0; i < sals.nelts; i++)
7798 addr_string[i] = xstrdup (copy_arg);
7799 goto done;
7800 }
7801
05ff989b
AC
7802 e = catch_exception (uiout, do_captured_parse_breakpoint,
7803 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
7804
7805 /* If caller is interested in rc value from parse, set value. */
05ff989b 7806 switch (e.reason)
0101ce28 7807 {
05ff989b 7808 case RETURN_QUIT:
98deb0da 7809 throw_exception (e);
05ff989b
AC
7810 case RETURN_ERROR:
7811 switch (e.error)
0101ce28 7812 {
05ff989b 7813 case NOT_FOUND_ERROR:
0101ce28 7814
05ff989b
AC
7815 /* If pending breakpoint support is turned off, throw
7816 error. */
fa8d40ab
JJ
7817
7818 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7819 throw_exception (e);
7820
7821 exception_print (gdb_stderr, e);
fa8d40ab 7822
05ff989b
AC
7823 /* If pending breakpoint support is auto query and the user
7824 selects no, then simply return the error code. */
059fb39f 7825 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7826 && !nquery (_("Make breakpoint pending on "
7827 "future shared library load? ")))
fd9b8c24 7828 return 0;
fa8d40ab 7829
05ff989b
AC
7830 /* At this point, either the user was queried about setting
7831 a pending breakpoint and selected yes, or pending
7832 breakpoint behavior is on and thus a pending breakpoint
7833 is defaulted on behalf of the user. */
0101ce28
JJ
7834 copy_arg = xstrdup (addr_start);
7835 addr_string = &copy_arg;
7836 sals.nelts = 1;
7837 sals.sals = &pending_sal;
7838 pending_sal.pc = 0;
7839 pending = 1;
05ff989b
AC
7840 break;
7841 default:
98deb0da 7842 throw_exception (e);
0101ce28 7843 }
05ff989b
AC
7844 default:
7845 if (!sals.nelts)
fd9b8c24 7846 return 0;
0101ce28 7847 }
c3f6f71d 7848
0fb4aa4b
PA
7849 done:
7850
4a64f543 7851 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7852 old_chain = make_cleanup (null_cleanup, 0);
7853
0101ce28
JJ
7854 if (!pending)
7855 {
7856 /* Make sure that all storage allocated to SALS gets freed. */
7857 make_cleanup (xfree, sals.sals);
7858
4a64f543 7859 /* Cleanup the addr_string array but not its contents. */
0101ce28
JJ
7860 make_cleanup (xfree, addr_string);
7861 }
c3f6f71d 7862
c3f6f71d
JM
7863 /* ----------------------------- SNIP -----------------------------
7864 Anything added to the cleanup chain beyond this point is assumed
7865 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7866 then the memory is not reclaimed. */
7867 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
7868
7869 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 7870 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7871 for (i = 0; i < sals.nelts; i++)
7872 {
7873 if (addr_string[i] != NULL)
b8c9b27d 7874 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
7875 }
7876
7877 /* Resolve all line numbers to PC's and verify that the addresses
7878 are ok for the target. */
0101ce28 7879 if (!pending)
23e7acfb 7880 breakpoint_sals_to_pc (&sals);
c3f6f71d 7881
7a697b8d
SS
7882 /* Fast tracepoints may have additional restrictions on location. */
7883 if (type_wanted == bp_fast_tracepoint)
7884 check_fast_tracepoint_sals (gdbarch, &sals);
7885
c3f6f71d
JM
7886 /* Verify that condition can be parsed, before setting any
7887 breakpoints. Allocate a separate condition expression for each
4a64f543 7888 breakpoint. */
0101ce28 7889 if (!pending)
c3f6f71d 7890 {
2f069f6f 7891 if (parse_condition_and_thread)
72b2ff0e
VP
7892 {
7893 /* Here we only parse 'arg' to separate condition
7894 from thread number, so parsing in context of first
7895 sal is OK. When setting the breakpoint we'll
7896 re-parse it in context of each sal. */
7897 cond_string = NULL;
7898 thread = -1;
4a306c9a
JB
7899 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7900 &thread, &task);
72b2ff0e
VP
7901 if (cond_string)
7902 make_cleanup (xfree, cond_string);
7903 }
2f069f6f 7904 else
72b2ff0e
VP
7905 {
7906 /* Create a private copy of condition string. */
7907 if (cond_string)
7908 {
7909 cond_string = xstrdup (cond_string);
7910 make_cleanup (xfree, cond_string);
7911 }
7912 }
0fb4aa4b
PA
7913
7914 /* If the user is creating a static tracepoint by marker id
7915 (strace -m MARKER_ID), then store the sals index, so that
7916 breakpoint_re_set can try to match up which of the newly
7917 found markers corresponds to this one, and, don't try to
7918 expand multiple locations for each sal, given than SALS
7919 already should contain all sals for MARKER_ID. */
7920 if (type_wanted == bp_static_tracepoint
7921 && is_marker_spec (addr_string[0]))
7922 {
7923 int i;
7924
7925 for (i = 0; i < sals.nelts; ++i)
7926 {
7927 struct symtabs_and_lines expanded;
7928 struct breakpoint *tp;
7929 struct cleanup *old_chain;
7930
7931 expanded.nelts = 1;
7932 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7933 expanded.sals[0] = sals.sals[i];
7934 old_chain = make_cleanup (xfree, expanded.sals);
7935
7936 create_breakpoint_sal (gdbarch, expanded, addr_string[i],
7937 cond_string, type_wanted,
7938 tempflag ? disp_del : disp_donttouch,
7939 thread, task, ignore_count, ops,
84f4c1fe 7940 from_tty, enabled, internal);
0fb4aa4b
PA
7941
7942 do_cleanups (old_chain);
7943
7944 /* Get the tracepoint we just created. */
84f4c1fe
PM
7945 if (internal)
7946 tp = get_breakpoint (internal_breakpoint_number);
7947 else
7948 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
7949 gdb_assert (tp != NULL);
7950
7951 /* Given that its possible to have multiple markers with
7952 the same string id, if the user is creating a static
7953 tracepoint by marker id ("strace -m MARKER_ID"), then
7954 store the sals index, so that breakpoint_re_set can
7955 try to match up which of the newly found markers
7956 corresponds to this one */
7957 tp->static_trace_marker_id_idx = i;
7958 }
7959 }
7960 else
7961 create_breakpoints_sal (gdbarch, sals, addr_string, cond_string,
3e43a32a
MS
7962 type_wanted,
7963 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7964 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7965 enabled, internal);
c906108c 7966 }
0101ce28
JJ
7967 else
7968 {
0101ce28
JJ
7969 struct breakpoint *b;
7970
0101ce28
JJ
7971 make_cleanup (xfree, copy_arg);
7972
a6d9a66e 7973 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 7974 set_breakpoint_number (internal, b);
72b2ff0e 7975 b->thread = -1;
018d34a4 7976 b->addr_string = addr_string[0];
72b2ff0e 7977 b->cond_string = NULL;
0101ce28 7978 b->ignore_count = ignore_count;
0101ce28 7979 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7980 b->condition_not_parsed = 1;
604133b5 7981 b->ops = ops;
41447f92 7982 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7983 b->pspace = current_program_space;
84f4c1fe 7984 b->py_bp_object = NULL;
74960c60 7985
6c95b8df 7986 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7987 && (b->type == bp_breakpoint
7988 || b->type == bp_hardware_breakpoint))
7989 b->enable_state = bp_startup_disabled;
7990
84f4c1fe
PM
7991 if (internal)
7992 /* Do not mention breakpoints with a negative number,
7993 but do notify observers. */
7994 observer_notify_breakpoint_created (b->number);
7995 else
7996 mention (b);
0101ce28
JJ
7997 }
7998
c3f6f71d 7999 if (sals.nelts > 1)
95a42b64 8000 {
3e43a32a
MS
8001 warning (_("Multiple breakpoints were set.\nUse the "
8002 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8003 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8004 }
8005
80c99de1
PA
8006 /* That's it. Discard the cleanups for data inserted into the
8007 breakpoint. */
8008 discard_cleanups (bkpt_chain);
8009 /* But cleanup everything else. */
c3f6f71d 8010 do_cleanups (old_chain);
217dc9e2 8011
80c99de1 8012 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8013 update_global_location_list (1);
fd9b8c24
PA
8014
8015 return 1;
c3f6f71d 8016}
c906108c 8017
72b2ff0e
VP
8018/* Set a breakpoint.
8019 ARG is a string describing breakpoint address,
8020 condition, and thread.
8021 FLAG specifies if a breakpoint is hardware on,
8022 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8023 and BP_TEMPFLAG. */
8024
98deb0da 8025static void
72b2ff0e 8026break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8027{
72b2ff0e 8028 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8029 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8030 ? bp_hardware_breakpoint
8031 : bp_breakpoint);
c3f6f71d 8032
8cdf0e15
VP
8033 create_breakpoint (get_current_arch (),
8034 arg,
8035 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8036 tempflag, type_wanted,
8cdf0e15
VP
8037 0 /* Ignore count */,
8038 pending_break_support,
8039 NULL /* breakpoint_ops */,
8040 from_tty,
84f4c1fe
PM
8041 1 /* enabled */,
8042 0 /* internal */);
c906108c
SS
8043}
8044
72b2ff0e 8045
c906108c
SS
8046/* Helper function for break_command_1 and disassemble_command. */
8047
8048void
fba45db2 8049resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8050{
8051 CORE_ADDR pc;
8052
8053 if (sal->pc == 0 && sal->symtab != NULL)
8054 {
8055 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8056 error (_("No line %d in file \"%s\"."),
c906108c
SS
8057 sal->line, sal->symtab->filename);
8058 sal->pc = pc;
6a048695 8059
4a64f543
MS
8060 /* If this SAL corresponds to a breakpoint inserted using a line
8061 number, then skip the function prologue if necessary. */
6a048695 8062 if (sal->explicit_line)
059acae7 8063 skip_prologue_sal (sal);
c906108c
SS
8064 }
8065
8066 if (sal->section == 0 && sal->symtab != NULL)
8067 {
8068 struct blockvector *bv;
c5aa993b
JM
8069 struct block *b;
8070 struct symbol *sym;
c906108c 8071
801e3a5b 8072 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8073 if (bv != NULL)
8074 {
7f0df278 8075 sym = block_linkage_function (b);
c906108c
SS
8076 if (sym != NULL)
8077 {
8078 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8079 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8080 }
8081 else
8082 {
4a64f543
MS
8083 /* It really is worthwhile to have the section, so we'll
8084 just have to look harder. This case can be executed
8085 if we have line numbers but no functions (as can
8086 happen in assembly source). */
c906108c 8087
c5aa993b 8088 struct minimal_symbol *msym;
6c95b8df
PA
8089 struct cleanup *old_chain = save_current_space_and_thread ();
8090
8091 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8092
8093 msym = lookup_minimal_symbol_by_pc (sal->pc);
8094 if (msym)
714835d5 8095 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8096
8097 do_cleanups (old_chain);
c906108c
SS
8098 }
8099 }
8100 }
8101}
8102
8103void
fba45db2 8104break_command (char *arg, int from_tty)
c906108c 8105{
db107f19 8106 break_command_1 (arg, 0, from_tty);
c906108c
SS
8107}
8108
c906108c 8109void
fba45db2 8110tbreak_command (char *arg, int from_tty)
c906108c 8111{
db107f19 8112 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8113}
8114
c906108c 8115static void
fba45db2 8116hbreak_command (char *arg, int from_tty)
c906108c 8117{
db107f19 8118 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8119}
8120
8121static void
fba45db2 8122thbreak_command (char *arg, int from_tty)
c906108c 8123{
db107f19 8124 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8125}
8126
8127static void
fba45db2 8128stop_command (char *arg, int from_tty)
c906108c 8129{
a3f17187 8130 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8131Usage: stop in <function | address>\n\
a3f17187 8132 stop at <line>\n"));
c906108c
SS
8133}
8134
8135static void
fba45db2 8136stopin_command (char *arg, int from_tty)
c906108c
SS
8137{
8138 int badInput = 0;
8139
c5aa993b 8140 if (arg == (char *) NULL)
c906108c
SS
8141 badInput = 1;
8142 else if (*arg != '*')
8143 {
8144 char *argptr = arg;
8145 int hasColon = 0;
8146
4a64f543 8147 /* Look for a ':'. If this is a line number specification, then
53a5351d 8148 say it is bad, otherwise, it should be an address or
4a64f543 8149 function/method name. */
c906108c 8150 while (*argptr && !hasColon)
c5aa993b
JM
8151 {
8152 hasColon = (*argptr == ':');
8153 argptr++;
8154 }
c906108c
SS
8155
8156 if (hasColon)
c5aa993b 8157 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8158 else
c5aa993b 8159 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8160 }
8161
8162 if (badInput)
a3f17187 8163 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8164 else
db107f19 8165 break_command_1 (arg, 0, from_tty);
c906108c
SS
8166}
8167
8168static void
fba45db2 8169stopat_command (char *arg, int from_tty)
c906108c
SS
8170{
8171 int badInput = 0;
8172
c5aa993b 8173 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8174 badInput = 1;
8175 else
8176 {
8177 char *argptr = arg;
8178 int hasColon = 0;
8179
4a64f543
MS
8180 /* Look for a ':'. If there is a '::' then get out, otherwise
8181 it is probably a line number. */
c906108c 8182 while (*argptr && !hasColon)
c5aa993b
JM
8183 {
8184 hasColon = (*argptr == ':');
8185 argptr++;
8186 }
c906108c
SS
8187
8188 if (hasColon)
c5aa993b 8189 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8190 else
c5aa993b 8191 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8192 }
8193
8194 if (badInput)
a3f17187 8195 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8196 else
db107f19 8197 break_command_1 (arg, 0, from_tty);
c906108c
SS
8198}
8199
4a64f543
MS
8200/* Return non-zero if EXP is verified as constant. Returned zero
8201 means EXP is variable. Also the constant detection may fail for
8202 some constant expressions and in such case still falsely return
8203 zero. */
65d79d4b
SDJ
8204static int
8205watchpoint_exp_is_const (const struct expression *exp)
8206{
8207 int i = exp->nelts;
8208
8209 while (i > 0)
8210 {
8211 int oplenp, argsp;
8212
8213 /* We are only interested in the descriptor of each element. */
8214 operator_length (exp, i, &oplenp, &argsp);
8215 i -= oplenp;
8216
8217 switch (exp->elts[i].opcode)
8218 {
8219 case BINOP_ADD:
8220 case BINOP_SUB:
8221 case BINOP_MUL:
8222 case BINOP_DIV:
8223 case BINOP_REM:
8224 case BINOP_MOD:
8225 case BINOP_LSH:
8226 case BINOP_RSH:
8227 case BINOP_LOGICAL_AND:
8228 case BINOP_LOGICAL_OR:
8229 case BINOP_BITWISE_AND:
8230 case BINOP_BITWISE_IOR:
8231 case BINOP_BITWISE_XOR:
8232 case BINOP_EQUAL:
8233 case BINOP_NOTEQUAL:
8234 case BINOP_LESS:
8235 case BINOP_GTR:
8236 case BINOP_LEQ:
8237 case BINOP_GEQ:
8238 case BINOP_REPEAT:
8239 case BINOP_COMMA:
8240 case BINOP_EXP:
8241 case BINOP_MIN:
8242 case BINOP_MAX:
8243 case BINOP_INTDIV:
8244 case BINOP_CONCAT:
8245 case BINOP_IN:
8246 case BINOP_RANGE:
8247 case TERNOP_COND:
8248 case TERNOP_SLICE:
8249 case TERNOP_SLICE_COUNT:
8250
8251 case OP_LONG:
8252 case OP_DOUBLE:
8253 case OP_DECFLOAT:
8254 case OP_LAST:
8255 case OP_COMPLEX:
8256 case OP_STRING:
8257 case OP_BITSTRING:
8258 case OP_ARRAY:
8259 case OP_TYPE:
8260 case OP_NAME:
8261 case OP_OBJC_NSSTRING:
8262
8263 case UNOP_NEG:
8264 case UNOP_LOGICAL_NOT:
8265 case UNOP_COMPLEMENT:
8266 case UNOP_ADDR:
8267 case UNOP_HIGH:
4a64f543
MS
8268 /* Unary, binary and ternary operators: We have to check
8269 their operands. If they are constant, then so is the
8270 result of that operation. For instance, if A and B are
8271 determined to be constants, then so is "A + B".
8272
8273 UNOP_IND is one exception to the rule above, because the
8274 value of *ADDR is not necessarily a constant, even when
8275 ADDR is. */
65d79d4b
SDJ
8276 break;
8277
8278 case OP_VAR_VALUE:
8279 /* Check whether the associated symbol is a constant.
4a64f543 8280
65d79d4b 8281 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8282 possible that a buggy compiler could mark a variable as
8283 constant even when it is not, and TYPE_CONST would return
8284 true in this case, while SYMBOL_CLASS wouldn't.
8285
8286 We also have to check for function symbols because they
8287 are always constant. */
65d79d4b
SDJ
8288 {
8289 struct symbol *s = exp->elts[i + 2].symbol;
8290
8291 if (SYMBOL_CLASS (s) != LOC_BLOCK
8292 && SYMBOL_CLASS (s) != LOC_CONST
8293 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8294 return 0;
8295 break;
8296 }
8297
8298 /* The default action is to return 0 because we are using
8299 the optimistic approach here: If we don't know something,
8300 then it is not a constant. */
8301 default:
8302 return 0;
8303 }
8304 }
8305
8306 return 1;
8307}
8308
77b06cd7
TJB
8309/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8310
8311static int
8312insert_watchpoint (struct bp_location *bl)
8313{
e09342b5
TJB
8314 int length = bl->owner->exact? 1 : bl->length;
8315
8316 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8317 bl->owner->cond_exp);
77b06cd7
TJB
8318}
8319
8320/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8321
8322static int
8323remove_watchpoint (struct bp_location *bl)
8324{
e09342b5
TJB
8325 int length = bl->owner->exact? 1 : bl->length;
8326
8327 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8328 bl->owner->cond_exp);
8329}
8330
8331/* Implement the "resources_needed" breakpoint_ops method for
8332 hardware watchpoints. */
8333
8334static int
8335resources_needed_watchpoint (const struct bp_location *bl)
8336{
90e4670f 8337 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8338
90e4670f 8339 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8340}
8341
8342/* The breakpoint_ops structure to be used in hardware watchpoints. */
8343
8344static struct breakpoint_ops watchpoint_breakpoint_ops =
8345{
8346 insert_watchpoint,
8347 remove_watchpoint,
8348 NULL, /* breakpoint_hit */
e09342b5 8349 resources_needed_watchpoint,
77b06cd7
TJB
8350 NULL, /* print_it */
8351 NULL, /* print_one */
8352 NULL, /* print_mention */
8353 NULL /* print_recreate */
8354};
8355
53a5351d
JM
8356/* accessflag: hw_write: watch write,
8357 hw_read: watch read,
8358 hw_access: watch access (read or write) */
c906108c 8359static void
84f4c1fe
PM
8360watch_command_1 (char *arg, int accessflag, int from_tty,
8361 int just_location, int internal)
c906108c 8362{
d983da9c 8363 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8364 struct expression *exp;
60e1c644 8365 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8366 struct value *val, *mark, *result;
c906108c 8367 struct frame_info *frame;
c906108c
SS
8368 char *exp_start = NULL;
8369 char *exp_end = NULL;
37e4754d 8370 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8371 int toklen;
8372 char *cond_start = NULL;
8373 char *cond_end = NULL;
c906108c
SS
8374 int i, other_type_used, target_resources_ok = 0;
8375 enum bptype bp_type;
e09342b5 8376 int reg_cnt = 0;
37e4754d 8377 int thread = -1;
0cf6dd15 8378 int pc = 0;
c906108c 8379
37e4754d
LM
8380 /* Make sure that we actually have parameters to parse. */
8381 if (arg != NULL && arg[0] != '\0')
8382 {
8383 toklen = strlen (arg); /* Size of argument list. */
8384
8385 /* Points tok to the end of the argument list. */
8386 tok = arg + toklen - 1;
8387
4a64f543
MS
8388 /* Go backwards in the parameters list. Skip the last
8389 parameter. If we're expecting a 'thread <thread_num>'
8390 parameter, this should be the thread identifier. */
37e4754d
LM
8391 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8392 tok--;
8393 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8394 tok--;
8395
8396 /* Points end_tok to the beginning of the last token. */
8397 id_tok_start = tok + 1;
8398
4a64f543
MS
8399 /* Go backwards in the parameters list. Skip one more
8400 parameter. If we're expecting a 'thread <thread_num>'
8401 parameter, we should reach a "thread" token. */
37e4754d
LM
8402 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8403 tok--;
8404
8405 end_tok = tok;
8406
8407 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8408 tok--;
8409
8410 /* Move the pointer forward to skip the whitespace and
8411 calculate the length of the token. */
8412 tok++;
8413 toklen = end_tok - tok;
8414
8415 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8416 {
8417 /* At this point we've found a "thread" token, which means
8418 the user is trying to set a watchpoint that triggers
8419 only in a specific thread. */
8420 char *endp;
8421
8422 /* Extract the thread ID from the next token. */
8423 thread = strtol (id_tok_start, &endp, 0);
8424
8425 /* Check if the user provided a valid numeric value for the
8426 thread ID. */
8427 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8428 error (_("Invalid thread ID specification %s."), id_tok_start);
8429
8430 /* Check if the thread actually exists. */
8431 if (!valid_thread_id (thread))
8432 error (_("Unknown thread %d."), thread);
8433
8434 /* Truncate the string and get rid of the thread <thread_num>
8435 parameter before the parameter list is parsed by the
8436 evaluate_expression() function. */
8437 *tok = '\0';
8438 }
8439 }
8440
8441 /* Parse the rest of the arguments. */
c906108c
SS
8442 innermost_block = NULL;
8443 exp_start = arg;
8444 exp = parse_exp_1 (&arg, 0, 0);
8445 exp_end = arg;
fa8a61dc
TT
8446 /* Remove trailing whitespace from the expression before saving it.
8447 This makes the eventual display of the expression string a bit
8448 prettier. */
8449 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8450 --exp_end;
8451
65d79d4b
SDJ
8452 /* Checking if the expression is not constant. */
8453 if (watchpoint_exp_is_const (exp))
8454 {
8455 int len;
8456
8457 len = exp_end - exp_start;
8458 while (len > 0 && isspace (exp_start[len - 1]))
8459 len--;
8460 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8461 }
8462
c906108c
SS
8463 exp_valid_block = innermost_block;
8464 mark = value_mark ();
a1442452 8465 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8466
8467 if (just_location)
8468 {
8469 exp_valid_block = NULL;
a1442452 8470 val = value_addr (result);
06a64a0b
TT
8471 release_value (val);
8472 value_free_to_mark (mark);
8473 }
8474 else if (val != NULL)
fa4727a6 8475 release_value (val);
c906108c 8476
e9cafbcc
TT
8477 tok = skip_spaces (arg);
8478 end_tok = skip_to_space (tok);
c906108c
SS
8479
8480 toklen = end_tok - tok;
8481 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8482 {
2d134ed3
PA
8483 struct expression *cond;
8484
60e1c644 8485 innermost_block = NULL;
c906108c
SS
8486 tok = cond_start = end_tok + 1;
8487 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8488
8489 /* The watchpoint expression may not be local, but the condition
8490 may still be. E.g.: `watch global if local > 0'. */
8491 cond_exp_valid_block = innermost_block;
8492
2d134ed3 8493 xfree (cond);
c906108c
SS
8494 cond_end = tok;
8495 }
8496 if (*tok)
8a3fe4f8 8497 error (_("Junk at end of command."));
c906108c 8498
53a5351d 8499 if (accessflag == hw_read)
c5aa993b 8500 bp_type = bp_read_watchpoint;
53a5351d 8501 else if (accessflag == hw_access)
c5aa993b
JM
8502 bp_type = bp_access_watchpoint;
8503 else
8504 bp_type = bp_hardware_watchpoint;
c906108c 8505
e09342b5
TJB
8506 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8507 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8508 error (_("Expression cannot be implemented with read/access watchpoint."));
e09342b5 8509 if (reg_cnt != 0)
c5aa993b
JM
8510 {
8511 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8512 target_resources_ok =
e09342b5 8513 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
53a5351d 8514 other_type_used);
c5aa993b 8515 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8516 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8517
c5aa993b 8518 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
3e43a32a
MS
8519 error (_("Target can only support one kind "
8520 "of HW watchpoint at a time."));
c5aa993b 8521 }
c906108c 8522
4a64f543
MS
8523 /* Change the type of breakpoint to an ordinary watchpoint if a
8524 hardware watchpoint could not be set. */
e09342b5 8525 if (!reg_cnt || target_resources_ok <= 0)
4d28f7a8
KB
8526 bp_type = bp_watchpoint;
8527
d983da9c 8528 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8529
8530 /* If the expression is "local", then set up a "watchpoint scope"
8531 breakpoint at the point where we've left the scope of the watchpoint
8532 expression. Create the scope breakpoint before the watchpoint, so
8533 that we will encounter it first in bpstat_stop_status. */
60e1c644 8534 if (exp_valid_block && frame)
d983da9c 8535 {
edb3359d
DJ
8536 if (frame_id_p (frame_unwind_caller_id (frame)))
8537 {
8538 scope_breakpoint
a6d9a66e
UW
8539 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8540 frame_unwind_caller_pc (frame),
edb3359d 8541 bp_watchpoint_scope);
d983da9c 8542
edb3359d 8543 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8544
edb3359d
DJ
8545 /* Automatically delete the breakpoint when it hits. */
8546 scope_breakpoint->disposition = disp_del;
d983da9c 8547
edb3359d
DJ
8548 /* Only break in the proper frame (help with recursion). */
8549 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8550
edb3359d 8551 /* Set the address at which we will stop. */
a6d9a66e
UW
8552 scope_breakpoint->loc->gdbarch
8553 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8554 scope_breakpoint->loc->requested_address
8555 = frame_unwind_caller_pc (frame);
8556 scope_breakpoint->loc->address
a6d9a66e
UW
8557 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8558 scope_breakpoint->loc->requested_address,
edb3359d
DJ
8559 scope_breakpoint->type);
8560 }
d983da9c
DJ
8561 }
8562
c906108c 8563 /* Now set up the breakpoint. */
2d134ed3 8564 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 8565 set_breakpoint_number (internal, b);
37e4754d 8566 b->thread = thread;
b5de0fa7 8567 b->disposition = disp_donttouch;
c906108c
SS
8568 b->exp = exp;
8569 b->exp_valid_block = exp_valid_block;
60e1c644 8570 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
8571 if (just_location)
8572 {
8573 struct type *t = value_type (val);
8574 CORE_ADDR addr = value_as_address (val);
8575 char *name;
8576
8577 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8578 name = type_to_string (t);
8579
d63d0675
JK
8580 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8581 core_addr_to_string (addr));
06a64a0b
TT
8582 xfree (name);
8583
d63d0675
JK
8584 b->exp_string = xstrprintf ("-location: %.*s",
8585 (int) (exp_end - exp_start), exp_start);
8586
06a64a0b
TT
8587 /* The above expression is in C. */
8588 b->language = language_c;
8589 }
8590 else
8591 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 8592 b->val = val;
fa4727a6 8593 b->val_valid = 1;
77b06cd7
TJB
8594 b->ops = &watchpoint_breakpoint_ops;
8595
e09342b5
TJB
8596 /* Use an exact watchpoint when there's only one memory region to be
8597 watched, and only one debug register is needed to watch it. */
8598 b->exact = target_exact_watchpoints && reg_cnt == 1;
8599
c906108c
SS
8600 if (cond_start)
8601 b->cond_string = savestring (cond_start, cond_end - cond_start);
8602 else
8603 b->cond_string = 0;
c5aa993b 8604
c906108c 8605 if (frame)
f6bc2008
PA
8606 {
8607 b->watchpoint_frame = get_frame_id (frame);
8608 b->watchpoint_thread = inferior_ptid;
8609 }
c906108c 8610 else
f6bc2008
PA
8611 {
8612 b->watchpoint_frame = null_frame_id;
8613 b->watchpoint_thread = null_ptid;
8614 }
c906108c 8615
d983da9c 8616 if (scope_breakpoint != NULL)
c906108c 8617 {
d983da9c
DJ
8618 /* The scope breakpoint is related to the watchpoint. We will
8619 need to act on them together. */
8620 b->related_breakpoint = scope_breakpoint;
8621 scope_breakpoint->related_breakpoint = b;
c906108c 8622 }
d983da9c 8623
06a64a0b
TT
8624 if (!just_location)
8625 value_free_to_mark (mark);
2d134ed3
PA
8626
8627 /* Finally update the new watchpoint. This creates the locations
8628 that should be inserted. */
8629 update_watchpoint (b, 1);
84f4c1fe
PM
8630 if (internal)
8631 /* Do not mention breakpoints with a negative number, but do
8632 notify observers. */
8633 observer_notify_breakpoint_created (b->number);
8634 else
8635 mention (b);
b60e7edf 8636 update_global_location_list (1);
c906108c
SS
8637}
8638
e09342b5
TJB
8639/* Return count of debug registers needed to watch the given expression.
8640 If EXACT_WATCHPOINTS is 1, then consider that only the address of
8641 the start of the watched region will be monitored (i.e., all accesses
8642 will be aligned). This uses less debug registers on some targets.
8643
8644 If the watchpoint cannot be handled in hardware return zero. */
c906108c 8645
c906108c 8646static int
e09342b5 8647can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
c906108c
SS
8648{
8649 int found_memory_cnt = 0;
2e70b7b9 8650 struct value *head = v;
c906108c
SS
8651
8652 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 8653 if (!can_use_hw_watchpoints)
c906108c 8654 return 0;
c5aa993b 8655
5c44784c
JM
8656 /* Make sure that the value of the expression depends only upon
8657 memory contents, and values computed from them within GDB. If we
8658 find any register references or function calls, we can't use a
8659 hardware watchpoint.
8660
8661 The idea here is that evaluating an expression generates a series
8662 of values, one holding the value of every subexpression. (The
8663 expression a*b+c has five subexpressions: a, b, a*b, c, and
8664 a*b+c.) GDB's values hold almost enough information to establish
8665 the criteria given above --- they identify memory lvalues,
8666 register lvalues, computed values, etcetera. So we can evaluate
8667 the expression, and then scan the chain of values that leaves
8668 behind to decide whether we can detect any possible change to the
8669 expression's final value using only hardware watchpoints.
8670
8671 However, I don't think that the values returned by inferior
8672 function calls are special in any way. So this function may not
8673 notice that an expression involving an inferior function call
8674 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 8675 for (; v; v = value_next (v))
c906108c 8676 {
5c44784c 8677 if (VALUE_LVAL (v) == lval_memory)
c906108c 8678 {
8464be76
DJ
8679 if (v != head && value_lazy (v))
8680 /* A lazy memory lvalue in the chain is one that GDB never
8681 needed to fetch; we either just used its address (e.g.,
8682 `a' in `a.b') or we never needed it at all (e.g., `a'
8683 in `a,b'). This doesn't apply to HEAD; if that is
8684 lazy then it was not readable, but watch it anyway. */
5c44784c 8685 ;
53a5351d 8686 else
5c44784c
JM
8687 {
8688 /* Ahh, memory we actually used! Check if we can cover
8689 it with hardware watchpoints. */
df407dfe 8690 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
8691
8692 /* We only watch structs and arrays if user asked for it
8693 explicitly, never if they just happen to appear in a
8694 middle of some value chain. */
8695 if (v == head
8696 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8697 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8698 {
42ae5230 8699 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
8700 int len;
8701 int num_regs;
8702
8703 len = (exact_watchpoints
8704 && is_scalar_type_recursive (vtype))?
8705 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 8706
e09342b5
TJB
8707 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8708 if (!num_regs)
2e70b7b9
MS
8709 return 0;
8710 else
e09342b5 8711 found_memory_cnt += num_regs;
2e70b7b9 8712 }
5c44784c 8713 }
c5aa993b 8714 }
5086187c
AC
8715 else if (VALUE_LVAL (v) != not_lval
8716 && deprecated_value_modifiable (v) == 0)
38b6c3b3 8717 return 0; /* These are values from the history (e.g., $1). */
5086187c 8718 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 8719 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
8720 }
8721
8722 /* The expression itself looks suitable for using a hardware
8723 watchpoint, but give the target machine a chance to reject it. */
8724 return found_memory_cnt;
8725}
8726
8b93c638 8727void
84f4c1fe 8728watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8729{
84f4c1fe 8730 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
8731}
8732
8733/* A helper function that looks for an argument at the start of a
8734 string. The argument must also either be at the end of the string,
8735 or be followed by whitespace. Returns 1 if it finds the argument,
8736 0 otherwise. If the argument is found, it updates *STR. */
8737
8738static int
8739check_for_argument (char **str, char *arg, int arg_len)
8740{
8741 if (strncmp (*str, arg, arg_len) == 0
8742 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8743 {
8744 *str += arg_len;
8745 return 1;
8746 }
8747 return 0;
8748}
8749
8750/* A helper function that looks for the "-location" argument and then
8751 calls watch_command_1. */
8752
8753static void
8754watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8755{
8756 int just_location = 0;
8757
8758 if (arg
8759 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8760 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8761 {
e9cafbcc 8762 arg = skip_spaces (arg);
06a64a0b
TT
8763 just_location = 1;
8764 }
8765
84f4c1fe 8766 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 8767}
8926118c 8768
c5aa993b 8769static void
fba45db2 8770watch_command (char *arg, int from_tty)
c906108c 8771{
06a64a0b 8772 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
8773}
8774
8b93c638 8775void
84f4c1fe 8776rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8777{
84f4c1fe 8778 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 8779}
8926118c 8780
c5aa993b 8781static void
fba45db2 8782rwatch_command (char *arg, int from_tty)
c906108c 8783{
06a64a0b 8784 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
8785}
8786
8b93c638 8787void
84f4c1fe 8788awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8789{
84f4c1fe 8790 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 8791}
8926118c 8792
c5aa993b 8793static void
fba45db2 8794awatch_command (char *arg, int from_tty)
c906108c 8795{
06a64a0b 8796 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 8797}
c906108c 8798\f
c5aa993b 8799
43ff13b4 8800/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
8801 because it uses the mechanisms of breakpoints. */
8802
bfec99b2
PA
8803struct until_break_command_continuation_args
8804{
8805 struct breakpoint *breakpoint;
8806 struct breakpoint *breakpoint2;
186c406b 8807 int thread_num;
bfec99b2
PA
8808};
8809
43ff13b4 8810/* This function is called by fetch_inferior_event via the
4a64f543 8811 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 8812 care of cleaning up the temporary breakpoints set up by the until
4a64f543 8813 command. */
c2c6d25f 8814static void
604ead4a 8815until_break_command_continuation (void *arg)
43ff13b4 8816{
bfec99b2
PA
8817 struct until_break_command_continuation_args *a = arg;
8818
8819 delete_breakpoint (a->breakpoint);
8820 if (a->breakpoint2)
8821 delete_breakpoint (a->breakpoint2);
186c406b 8822 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
8823}
8824
c906108c 8825void
ae66c1fc 8826until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
8827{
8828 struct symtabs_and_lines sals;
8829 struct symtab_and_line sal;
206415a3 8830 struct frame_info *frame = get_selected_frame (NULL);
c906108c 8831 struct breakpoint *breakpoint;
f107f563 8832 struct breakpoint *breakpoint2 = NULL;
c906108c 8833 struct cleanup *old_chain;
186c406b
TT
8834 int thread;
8835 struct thread_info *tp;
c906108c
SS
8836
8837 clear_proceed_status ();
8838
8839 /* Set a breakpoint where the user wants it and at return from
4a64f543 8840 this function. */
c5aa993b 8841
c906108c
SS
8842 if (default_breakpoint_valid)
8843 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 8844 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 8845 else
53a5351d 8846 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 8847 0, (char ***) NULL, NULL);
c5aa993b 8848
c906108c 8849 if (sals.nelts != 1)
8a3fe4f8 8850 error (_("Couldn't get information on specified line."));
c5aa993b 8851
c906108c 8852 sal = sals.sals[0];
4a64f543 8853 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 8854
c906108c 8855 if (*arg)
8a3fe4f8 8856 error (_("Junk at end of arguments."));
c5aa993b 8857
c906108c 8858 resolve_sal_pc (&sal);
c5aa993b 8859
ae66c1fc
EZ
8860 if (anywhere)
8861 /* If the user told us to continue until a specified location,
8862 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
8863 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8864 null_frame_id, bp_until);
ae66c1fc 8865 else
4a64f543
MS
8866 /* Otherwise, specify the selected frame, because we want to stop
8867 only at the very same frame. */
a6d9a66e
UW
8868 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8869 get_stack_frame_id (frame),
ae66c1fc 8870 bp_until);
c5aa993b 8871
f107f563 8872 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 8873
186c406b
TT
8874 tp = inferior_thread ();
8875 thread = tp->num;
8876
ae66c1fc
EZ
8877 /* Keep within the current frame, or in frames called by the current
8878 one. */
edb3359d
DJ
8879
8880 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 8881 {
edb3359d
DJ
8882 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8883 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
8884 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8885 sal,
edb3359d 8886 frame_unwind_caller_id (frame),
f107f563
VP
8887 bp_until);
8888 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
8889
8890 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8891 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 8892 }
c5aa993b 8893
c906108c 8894 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 8895
4a64f543
MS
8896 /* If we are running asynchronously, and proceed call above has
8897 actually managed to start the target, arrange for breakpoints to
8898 be deleted when the target stops. Otherwise, we're already
8899 stopped and delete breakpoints via cleanup chain. */
f107f563 8900
8ea051c5 8901 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 8902 {
bfec99b2
PA
8903 struct until_break_command_continuation_args *args;
8904 args = xmalloc (sizeof (*args));
f107f563 8905
bfec99b2
PA
8906 args->breakpoint = breakpoint;
8907 args->breakpoint2 = breakpoint2;
186c406b 8908 args->thread_num = thread;
f107f563
VP
8909
8910 discard_cleanups (old_chain);
95e54da7
PA
8911 add_continuation (inferior_thread (),
8912 until_break_command_continuation, args,
604ead4a 8913 xfree);
f107f563
VP
8914 }
8915 else
c5aa993b 8916 do_cleanups (old_chain);
c906108c 8917}
ae66c1fc 8918
c906108c
SS
8919/* This function attempts to parse an optional "if <cond>" clause
8920 from the arg string. If one is not found, it returns NULL.
c5aa993b 8921
c906108c
SS
8922 Else, it returns a pointer to the condition string. (It does not
8923 attempt to evaluate the string against a particular block.) And,
8924 it updates arg to point to the first character following the parsed
4a64f543 8925 if clause in the arg string. */
53a5351d 8926
c906108c 8927static char *
fba45db2 8928ep_parse_optional_if_clause (char **arg)
c906108c 8929{
c5aa993b
JM
8930 char *cond_string;
8931
8932 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 8933 return NULL;
c5aa993b 8934
4a64f543 8935 /* Skip the "if" keyword. */
c906108c 8936 (*arg) += 2;
c5aa993b 8937
c906108c 8938 /* Skip any extra leading whitespace, and record the start of the
4a64f543 8939 condition string. */
e9cafbcc 8940 *arg = skip_spaces (*arg);
c906108c 8941 cond_string = *arg;
c5aa993b 8942
4a64f543
MS
8943 /* Assume that the condition occupies the remainder of the arg
8944 string. */
c906108c 8945 (*arg) += strlen (cond_string);
c5aa993b 8946
c906108c
SS
8947 return cond_string;
8948}
c5aa993b 8949
c906108c
SS
8950/* Commands to deal with catching events, such as signals, exceptions,
8951 process start/exit, etc. */
c5aa993b
JM
8952
8953typedef enum
8954{
44feb3ce
TT
8955 catch_fork_temporary, catch_vfork_temporary,
8956 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
8957}
8958catch_fork_kind;
8959
c906108c 8960static void
cc59ec59
MS
8961catch_fork_command_1 (char *arg, int from_tty,
8962 struct cmd_list_element *command)
c906108c 8963{
a6d9a66e 8964 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 8965 char *cond_string = NULL;
44feb3ce
TT
8966 catch_fork_kind fork_kind;
8967 int tempflag;
8968
8969 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
8970 tempflag = (fork_kind == catch_fork_temporary
8971 || fork_kind == catch_vfork_temporary);
c5aa993b 8972
44feb3ce
TT
8973 if (!arg)
8974 arg = "";
e9cafbcc 8975 arg = skip_spaces (arg);
c5aa993b 8976
c906108c 8977 /* The allowed syntax is:
c5aa993b
JM
8978 catch [v]fork
8979 catch [v]fork if <cond>
8980
4a64f543 8981 First, check if there's an if clause. */
c906108c 8982 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 8983
c906108c 8984 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 8985 error (_("Junk at end of arguments."));
c5aa993b 8986
c906108c 8987 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 8988 and enable reporting of such events. */
c5aa993b
JM
8989 switch (fork_kind)
8990 {
44feb3ce
TT
8991 case catch_fork_temporary:
8992 case catch_fork_permanent:
a6d9a66e 8993 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 8994 &catch_fork_breakpoint_ops);
c906108c 8995 break;
44feb3ce
TT
8996 case catch_vfork_temporary:
8997 case catch_vfork_permanent:
a6d9a66e 8998 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 8999 &catch_vfork_breakpoint_ops);
c906108c 9000 break;
c5aa993b 9001 default:
8a3fe4f8 9002 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9003 break;
c5aa993b 9004 }
c906108c
SS
9005}
9006
9007static void
cc59ec59
MS
9008catch_exec_command_1 (char *arg, int from_tty,
9009 struct cmd_list_element *command)
c906108c 9010{
a6d9a66e 9011 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9012 int tempflag;
c5aa993b 9013 char *cond_string = NULL;
c906108c 9014
44feb3ce
TT
9015 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9016
9017 if (!arg)
9018 arg = "";
e9cafbcc 9019 arg = skip_spaces (arg);
c906108c
SS
9020
9021 /* The allowed syntax is:
c5aa993b
JM
9022 catch exec
9023 catch exec if <cond>
c906108c 9024
4a64f543 9025 First, check if there's an if clause. */
c906108c
SS
9026 cond_string = ep_parse_optional_if_clause (&arg);
9027
9028 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9029 error (_("Junk at end of arguments."));
c906108c
SS
9030
9031 /* If this target supports it, create an exec catchpoint
4a64f543 9032 and enable reporting of such events. */
a6d9a66e
UW
9033 create_catchpoint (gdbarch, tempflag, cond_string,
9034 &catch_exec_breakpoint_ops);
c906108c 9035}
c5aa993b 9036
3086aeae
DJ
9037static enum print_stop_action
9038print_exception_catchpoint (struct breakpoint *b)
9039{
ade92717 9040 int bp_temp, bp_throw;
3086aeae 9041
ade92717 9042 annotate_catchpoint (b->number);
3086aeae 9043
ade92717
AR
9044 bp_throw = strstr (b->addr_string, "throw") != NULL;
9045 if (b->loc->address != b->loc->requested_address)
9046 breakpoint_adjustment_warning (b->loc->requested_address,
9047 b->loc->address,
9048 b->number, 1);
df2b6d2d 9049 bp_temp = b->disposition == disp_del;
ade92717
AR
9050 ui_out_text (uiout,
9051 bp_temp ? "Temporary catchpoint "
9052 : "Catchpoint ");
9053 if (!ui_out_is_mi_like_p (uiout))
9054 ui_out_field_int (uiout, "bkptno", b->number);
9055 ui_out_text (uiout,
c0b37c48
AR
9056 bp_throw ? " (exception thrown), "
9057 : " (exception caught), ");
ade92717
AR
9058 if (ui_out_is_mi_like_p (uiout))
9059 {
9060 ui_out_field_string (uiout, "reason",
9061 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9062 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9063 ui_out_field_int (uiout, "bkptno", b->number);
9064 }
3086aeae
DJ
9065 return PRINT_SRC_AND_LOC;
9066}
9067
9068static void
cc59ec59
MS
9069print_one_exception_catchpoint (struct breakpoint *b,
9070 struct bp_location **last_loc)
3086aeae 9071{
79a45b7d 9072 struct value_print_options opts;
cc59ec59 9073
79a45b7d
TT
9074 get_user_print_options (&opts);
9075 if (opts.addressprint)
3086aeae
DJ
9076 {
9077 annotate_field (4);
604133b5
AR
9078 if (b->loc == NULL || b->loc->shlib_disabled)
9079 ui_out_field_string (uiout, "addr", "<PENDING>");
9080 else
5af949e3
UW
9081 ui_out_field_core_addr (uiout, "addr",
9082 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9083 }
9084 annotate_field (5);
604133b5 9085 if (b->loc)
a6d9a66e 9086 *last_loc = b->loc;
3086aeae
DJ
9087 if (strstr (b->addr_string, "throw") != NULL)
9088 ui_out_field_string (uiout, "what", "exception throw");
9089 else
9090 ui_out_field_string (uiout, "what", "exception catch");
9091}
9092
9093static void
9094print_mention_exception_catchpoint (struct breakpoint *b)
9095{
ade92717
AR
9096 int bp_temp;
9097 int bp_throw;
9098
df2b6d2d 9099 bp_temp = b->disposition == disp_del;
ade92717
AR
9100 bp_throw = strstr (b->addr_string, "throw") != NULL;
9101 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9102 : _("Catchpoint "));
9103 ui_out_field_int (uiout, "bkptno", b->number);
9104 ui_out_text (uiout, bp_throw ? _(" (throw)")
9105 : _(" (catch)"));
3086aeae
DJ
9106}
9107
6149aea9
PA
9108/* Implement the "print_recreate" breakpoint_ops method for throw and
9109 catch catchpoints. */
9110
9111static void
4a64f543
MS
9112print_recreate_exception_catchpoint (struct breakpoint *b,
9113 struct ui_file *fp)
6149aea9
PA
9114{
9115 int bp_temp;
9116 int bp_throw;
9117
9118 bp_temp = b->disposition == disp_del;
9119 bp_throw = strstr (b->addr_string, "throw") != NULL;
9120 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9121 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9122}
9123
3086aeae 9124static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
9125 NULL, /* insert */
9126 NULL, /* remove */
9127 NULL, /* breakpoint_hit */
e09342b5 9128 NULL, /* resources_needed */
3086aeae
DJ
9129 print_exception_catchpoint,
9130 print_one_exception_catchpoint,
6149aea9
PA
9131 print_mention_exception_catchpoint,
9132 print_recreate_exception_catchpoint
3086aeae
DJ
9133};
9134
9135static int
9136handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9137 enum exception_event_kind ex_event, int from_tty)
9138{
604133b5
AR
9139 char *trigger_func_name;
9140
3086aeae 9141 if (ex_event == EX_EVENT_CATCH)
604133b5 9142 trigger_func_name = "__cxa_begin_catch";
3086aeae 9143 else
604133b5 9144 trigger_func_name = "__cxa_throw";
3086aeae 9145
8cdf0e15
VP
9146 create_breakpoint (get_current_arch (),
9147 trigger_func_name, cond_string, -1,
9148 0 /* condition and thread are valid. */,
0fb4aa4b 9149 tempflag, bp_breakpoint,
8cdf0e15
VP
9150 0,
9151 AUTO_BOOLEAN_TRUE /* pending */,
9152 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9153 1 /* enabled */,
9154 0 /* internal */);
3086aeae 9155
3086aeae
DJ
9156 return 1;
9157}
9158
4a64f543 9159/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9160
9161static void
fba45db2
KB
9162catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9163 int tempflag, int from_tty)
c906108c 9164{
c5aa993b 9165 char *cond_string = NULL;
c5aa993b 9166
44feb3ce
TT
9167 if (!arg)
9168 arg = "";
e9cafbcc 9169 arg = skip_spaces (arg);
c5aa993b 9170
c906108c
SS
9171 cond_string = ep_parse_optional_if_clause (&arg);
9172
9173 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9174 error (_("Junk at end of arguments."));
c906108c 9175
059fb39f
PM
9176 if (ex_event != EX_EVENT_THROW
9177 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9178 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9179
3086aeae
DJ
9180 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9181 return;
9182
8a3fe4f8 9183 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9184}
9185
44feb3ce
TT
9186/* Implementation of "catch catch" command. */
9187
9188static void
9189catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9190{
9191 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9192
44feb3ce
TT
9193 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9194}
9195
9196/* Implementation of "catch throw" command. */
9197
9198static void
9199catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9200{
9201 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9202
44feb3ce
TT
9203 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9204}
9205
f7f9143b
JB
9206/* Create a breakpoint struct for Ada exception catchpoints. */
9207
9208static void
a6d9a66e
UW
9209create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9210 struct symtab_and_line sal,
f7f9143b
JB
9211 char *addr_string,
9212 char *exp_string,
9213 char *cond_string,
9214 struct expression *cond,
9215 struct breakpoint_ops *ops,
9216 int tempflag,
9217 int from_tty)
9218{
9219 struct breakpoint *b;
9220
9221 if (from_tty)
9222 {
5af949e3
UW
9223 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9224 if (!loc_gdbarch)
9225 loc_gdbarch = gdbarch;
9226
6c95b8df
PA
9227 describe_other_breakpoints (loc_gdbarch,
9228 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9229 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9230 version for exception catchpoints, because two catchpoints
9231 used for different exception names will use the same address.
9232 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9233 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
9234 inapropriate, we should use the word catchpoint, and tell
9235 the user what type of catchpoint it is. The above is good
9236 enough for now, though. */
9237 }
9238
a6d9a66e 9239 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9240 set_breakpoint_count (breakpoint_count + 1);
9241
9242 b->enable_state = bp_enabled;
9243 b->disposition = tempflag ? disp_del : disp_donttouch;
9244 b->number = breakpoint_count;
9245 b->ignore_count = 0;
511a6cd4 9246 b->loc->cond = cond;
f7f9143b
JB
9247 b->addr_string = addr_string;
9248 b->language = language_ada;
9249 b->cond_string = cond_string;
9250 b->exp_string = exp_string;
9251 b->thread = -1;
9252 b->ops = ops;
f7f9143b
JB
9253
9254 mention (b);
b60e7edf 9255 update_global_location_list (1);
f7f9143b
JB
9256}
9257
9258/* Implement the "catch exception" command. */
9259
9260static void
44feb3ce
TT
9261catch_ada_exception_command (char *arg, int from_tty,
9262 struct cmd_list_element *command)
f7f9143b 9263{
a6d9a66e 9264 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9265 int tempflag;
f7f9143b 9266 struct symtab_and_line sal;
f7f9143b
JB
9267 char *addr_string = NULL;
9268 char *exp_string = NULL;
9269 char *cond_string = NULL;
9270 struct expression *cond = NULL;
9271 struct breakpoint_ops *ops = NULL;
9272
44feb3ce
TT
9273 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9274
9275 if (!arg)
9276 arg = "";
f7f9143b
JB
9277 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9278 &cond_string, &cond, &ops);
a6d9a66e 9279 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9280 cond_string, cond, ops, tempflag,
9281 from_tty);
9282}
9283
a96d9b2e
SDJ
9284/* Cleanup function for a syscall filter list. */
9285static void
9286clean_up_filters (void *arg)
9287{
9288 VEC(int) *iter = *(VEC(int) **) arg;
9289 VEC_free (int, iter);
9290}
9291
9292/* Splits the argument using space as delimiter. Returns an xmalloc'd
9293 filter list, or NULL if no filtering is required. */
9294static VEC(int) *
9295catch_syscall_split_args (char *arg)
9296{
9297 VEC(int) *result = NULL;
9298 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9299
9300 while (*arg != '\0')
9301 {
9302 int i, syscall_number;
9303 char *endptr;
9304 char cur_name[128];
9305 struct syscall s;
9306
9307 /* Skip whitespace. */
9308 while (isspace (*arg))
9309 arg++;
9310
9311 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9312 cur_name[i] = arg[i];
9313 cur_name[i] = '\0';
9314 arg += i;
9315
9316 /* Check if the user provided a syscall name or a number. */
9317 syscall_number = (int) strtol (cur_name, &endptr, 0);
9318 if (*endptr == '\0')
bccd0dd2 9319 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9320 else
9321 {
9322 /* We have a name. Let's check if it's valid and convert it
9323 to a number. */
9324 get_syscall_by_name (cur_name, &s);
9325
9326 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9327 /* Here we have to issue an error instead of a warning,
9328 because GDB cannot do anything useful if there's no
9329 syscall number to be caught. */
a96d9b2e
SDJ
9330 error (_("Unknown syscall name '%s'."), cur_name);
9331 }
9332
9333 /* Ok, it's valid. */
9334 VEC_safe_push (int, result, s.number);
9335 }
9336
9337 discard_cleanups (cleanup);
9338 return result;
9339}
9340
9341/* Implement the "catch syscall" command. */
9342
9343static void
cc59ec59
MS
9344catch_syscall_command_1 (char *arg, int from_tty,
9345 struct cmd_list_element *command)
a96d9b2e
SDJ
9346{
9347 int tempflag;
9348 VEC(int) *filter;
9349 struct syscall s;
9350 struct gdbarch *gdbarch = get_current_arch ();
9351
9352 /* Checking if the feature if supported. */
9353 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9354 error (_("The feature 'catch syscall' is not supported on \
ea666128 9355this architecture yet."));
a96d9b2e
SDJ
9356
9357 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9358
e9cafbcc 9359 arg = skip_spaces (arg);
a96d9b2e
SDJ
9360
9361 /* We need to do this first "dummy" translation in order
9362 to get the syscall XML file loaded or, most important,
9363 to display a warning to the user if there's no XML file
9364 for his/her architecture. */
9365 get_syscall_by_number (0, &s);
9366
9367 /* The allowed syntax is:
9368 catch syscall
9369 catch syscall <name | number> [<name | number> ... <name | number>]
9370
9371 Let's check if there's a syscall name. */
9372
9373 if (arg != NULL)
9374 filter = catch_syscall_split_args (arg);
9375 else
9376 filter = NULL;
9377
9378 create_syscall_event_catchpoint (tempflag, filter,
9379 &catch_syscall_breakpoint_ops);
9380}
9381
f7f9143b
JB
9382/* Implement the "catch assert" command. */
9383
9384static void
3e43a32a
MS
9385catch_assert_command (char *arg, int from_tty,
9386 struct cmd_list_element *command)
f7f9143b 9387{
a6d9a66e 9388 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9389 int tempflag;
f7f9143b
JB
9390 struct symtab_and_line sal;
9391 char *addr_string = NULL;
9392 struct breakpoint_ops *ops = NULL;
9393
44feb3ce
TT
9394 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9395
9396 if (!arg)
9397 arg = "";
f7f9143b 9398 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9399 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9400 ops, tempflag, from_tty);
f7f9143b
JB
9401}
9402
c906108c 9403static void
fba45db2 9404catch_command (char *arg, int from_tty)
c906108c 9405{
44feb3ce 9406 error (_("Catch requires an event name."));
c906108c
SS
9407}
9408\f
9409
9410static void
fba45db2 9411tcatch_command (char *arg, int from_tty)
c906108c 9412{
44feb3ce 9413 error (_("Catch requires an event name."));
c906108c
SS
9414}
9415
80f8a6eb 9416/* Delete breakpoints by address or line. */
c906108c
SS
9417
9418static void
fba45db2 9419clear_command (char *arg, int from_tty)
c906108c 9420{
d6e956e5
VP
9421 struct breakpoint *b;
9422 VEC(breakpoint_p) *found = 0;
9423 int ix;
c906108c
SS
9424 int default_match;
9425 struct symtabs_and_lines sals;
9426 struct symtab_and_line sal;
c906108c
SS
9427 int i;
9428
9429 if (arg)
9430 {
9431 sals = decode_line_spec (arg, 1);
9432 default_match = 0;
9433 }
9434 else
9435 {
c5aa993b 9436 sals.sals = (struct symtab_and_line *)
c906108c 9437 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9438 make_cleanup (xfree, sals.sals);
4a64f543 9439 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9440 sal.line = default_breakpoint_line;
9441 sal.symtab = default_breakpoint_symtab;
9442 sal.pc = default_breakpoint_address;
6c95b8df 9443 sal.pspace = default_breakpoint_pspace;
c906108c 9444 if (sal.symtab == 0)
8a3fe4f8 9445 error (_("No source file specified."));
c906108c
SS
9446
9447 sals.sals[0] = sal;
9448 sals.nelts = 1;
9449
9450 default_match = 1;
9451 }
9452
4a64f543
MS
9453 /* We don't call resolve_sal_pc here. That's not as bad as it
9454 seems, because all existing breakpoints typically have both
9455 file/line and pc set. So, if clear is given file/line, we can
9456 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9457
9458 We only support clearing given the address explicitly
9459 present in breakpoint table. Say, we've set breakpoint
4a64f543 9460 at file:line. There were several PC values for that file:line,
ed0616c6 9461 due to optimization, all in one block.
4a64f543
MS
9462
9463 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9464 PC corresponding to the same file:line, the breakpoint won't
9465 be cleared. We probably can still clear the breakpoint, but
9466 since the other PC value is never presented to user, user
9467 can only find it by guessing, and it does not seem important
9468 to support that. */
9469
4a64f543
MS
9470 /* For each line spec given, delete bps which correspond to it. Do
9471 it in two passes, solely to preserve the current behavior that
9472 from_tty is forced true if we delete more than one
9473 breakpoint. */
c906108c 9474
80f8a6eb 9475 found = NULL;
c906108c
SS
9476 for (i = 0; i < sals.nelts; i++)
9477 {
9478 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9479 If line given (pc == 0), clear all bpts on specified line.
9480 If defaulting, clear all bpts on default line
c906108c 9481 or at default pc.
c5aa993b
JM
9482
9483 defaulting sal.pc != 0 tests to do
9484
9485 0 1 pc
9486 1 1 pc _and_ line
9487 0 0 line
9488 1 0 <can't happen> */
c906108c
SS
9489
9490 sal = sals.sals[i];
c906108c 9491
4a64f543 9492 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9493 ALL_BREAKPOINTS (b)
c5aa993b 9494 {
0d381245 9495 int match = 0;
4a64f543 9496 /* Are we going to delete b? */
cc60f2e3 9497 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9498 {
9499 struct bp_location *loc = b->loc;
9500 for (; loc; loc = loc->next)
9501 {
6c95b8df
PA
9502 int pc_match = sal.pc
9503 && (loc->pspace == sal.pspace)
0d381245
VP
9504 && (loc->address == sal.pc)
9505 && (!section_is_overlay (loc->section)
9506 || loc->section == sal.section);
9507 int line_match = ((default_match || (0 == sal.pc))
9508 && b->source_file != NULL
9509 && sal.symtab != NULL
6c95b8df 9510 && sal.pspace == loc->pspace
3e43a32a
MS
9511 && strcmp (b->source_file,
9512 sal.symtab->filename) == 0
0d381245
VP
9513 && b->line_number == sal.line);
9514 if (pc_match || line_match)
9515 {
9516 match = 1;
9517 break;
9518 }
9519 }
9520 }
9521
9522 if (match)
d6e956e5 9523 VEC_safe_push(breakpoint_p, found, b);
c906108c 9524 }
80f8a6eb
MS
9525 }
9526 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9527 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9528 {
9529 if (arg)
8a3fe4f8 9530 error (_("No breakpoint at %s."), arg);
80f8a6eb 9531 else
8a3fe4f8 9532 error (_("No breakpoint at this line."));
80f8a6eb 9533 }
c906108c 9534
d6e956e5 9535 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9536 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9537 if (from_tty)
a3f17187 9538 {
d6e956e5 9539 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9540 printf_unfiltered (_("Deleted breakpoint "));
9541 else
9542 printf_unfiltered (_("Deleted breakpoints "));
9543 }
80f8a6eb 9544 breakpoints_changed ();
d6e956e5
VP
9545
9546 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9547 {
c5aa993b 9548 if (from_tty)
d6e956e5
VP
9549 printf_unfiltered ("%d ", b->number);
9550 delete_breakpoint (b);
c906108c 9551 }
80f8a6eb
MS
9552 if (from_tty)
9553 putchar_unfiltered ('\n');
c906108c
SS
9554}
9555\f
9556/* Delete breakpoint in BS if they are `delete' breakpoints and
9557 all breakpoints that are marked for deletion, whether hit or not.
9558 This is called after any breakpoint is hit, or after errors. */
9559
9560void
fba45db2 9561breakpoint_auto_delete (bpstat bs)
c906108c 9562{
35df4500 9563 struct breakpoint *b, *b_tmp;
c906108c
SS
9564
9565 for (; bs; bs = bs->next)
f431efe5
PA
9566 if (bs->breakpoint_at
9567 && bs->breakpoint_at->disposition == disp_del
c906108c 9568 && bs->stop)
f431efe5 9569 delete_breakpoint (bs->breakpoint_at);
c906108c 9570
35df4500 9571 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9572 {
b5de0fa7 9573 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
9574 delete_breakpoint (b);
9575 }
c906108c
SS
9576}
9577
4a64f543
MS
9578/* A comparison function for bp_location AP and BP being interfaced to
9579 qsort. Sort elements primarily by their ADDRESS (no matter what
9580 does breakpoint_address_is_meaningful say for its OWNER),
9581 secondarily by ordering first bp_permanent OWNERed elements and
9582 terciarily just ensuring the array is sorted stable way despite
9583 qsort being an instable algorithm. */
876fa593
JK
9584
9585static int
494cfb0f 9586bp_location_compare (const void *ap, const void *bp)
876fa593 9587{
494cfb0f
JK
9588 struct bp_location *a = *(void **) ap;
9589 struct bp_location *b = *(void **) bp;
2bdf28a0 9590 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
9591 int a_perm = a->owner->enable_state == bp_permanent;
9592 int b_perm = b->owner->enable_state == bp_permanent;
9593
9594 if (a->address != b->address)
9595 return (a->address > b->address) - (a->address < b->address);
9596
9597 /* Sort permanent breakpoints first. */
9598 if (a_perm != b_perm)
9599 return (a_perm < b_perm) - (a_perm > b_perm);
9600
4a64f543
MS
9601 /* Make the user-visible order stable across GDB runs. Locations of
9602 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
9603
9604 if (a->owner->number != b->owner->number)
9605 return (a->owner->number > b->owner->number)
9606 - (a->owner->number < b->owner->number);
9607
9608 return (a > b) - (a < b);
9609}
9610
876fa593 9611/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
9612 bp_location_shadow_len_after_address_max according to the current
9613 content of the bp_location array. */
f7545552
TT
9614
9615static void
876fa593 9616bp_location_target_extensions_update (void)
f7545552 9617{
876fa593
JK
9618 struct bp_location *bl, **blp_tmp;
9619
9620 bp_location_placed_address_before_address_max = 0;
9621 bp_location_shadow_len_after_address_max = 0;
9622
9623 ALL_BP_LOCATIONS (bl, blp_tmp)
9624 {
9625 CORE_ADDR start, end, addr;
9626
9627 if (!bp_location_has_shadow (bl))
9628 continue;
9629
9630 start = bl->target_info.placed_address;
9631 end = start + bl->target_info.shadow_len;
9632
9633 gdb_assert (bl->address >= start);
9634 addr = bl->address - start;
9635 if (addr > bp_location_placed_address_before_address_max)
9636 bp_location_placed_address_before_address_max = addr;
9637
9638 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9639
9640 gdb_assert (bl->address < end);
9641 addr = end - bl->address;
9642 if (addr > bp_location_shadow_len_after_address_max)
9643 bp_location_shadow_len_after_address_max = addr;
9644 }
f7545552
TT
9645}
9646
4cd9bd08 9647/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
9648 into the inferior, only remove already-inserted locations that no
9649 longer should be inserted. Functions that delete a breakpoint or
9650 breakpoints should pass false, so that deleting a breakpoint
9651 doesn't have the side effect of inserting the locations of other
9652 breakpoints that are marked not-inserted, but should_be_inserted
9653 returns true on them.
9654
9655 This behaviour is useful is situations close to tear-down -- e.g.,
9656 after an exec, while the target still has execution, but breakpoint
9657 shadows of the previous executable image should *NOT* be restored
9658 to the new image; or before detaching, where the target still has
9659 execution and wants to delete breakpoints from GDB's lists, and all
9660 breakpoints had already been removed from the inferior. */
9661
0d381245 9662static void
b60e7edf 9663update_global_location_list (int should_insert)
0d381245 9664{
74960c60 9665 struct breakpoint *b;
876fa593 9666 struct bp_location **locp, *loc;
f7545552
TT
9667 struct cleanup *cleanups;
9668
2d134ed3
PA
9669 /* Used in the duplicates detection below. When iterating over all
9670 bp_locations, points to the first bp_location of a given address.
9671 Breakpoints and watchpoints of different types are never
9672 duplicates of each other. Keep one pointer for each type of
9673 breakpoint/watchpoint, so we only need to loop over all locations
9674 once. */
9675 struct bp_location *bp_loc_first; /* breakpoint */
9676 struct bp_location *wp_loc_first; /* hardware watchpoint */
9677 struct bp_location *awp_loc_first; /* access watchpoint */
9678 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 9679
4a64f543
MS
9680 /* Saved former bp_location array which we compare against the newly
9681 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
9682 struct bp_location **old_location, **old_locp;
9683 unsigned old_location_count;
9684
9685 old_location = bp_location;
9686 old_location_count = bp_location_count;
9687 bp_location = NULL;
9688 bp_location_count = 0;
9689 cleanups = make_cleanup (xfree, old_location);
0d381245 9690
74960c60 9691 ALL_BREAKPOINTS (b)
876fa593
JK
9692 for (loc = b->loc; loc; loc = loc->next)
9693 bp_location_count++;
9694
9695 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9696 locp = bp_location;
9697 ALL_BREAKPOINTS (b)
9698 for (loc = b->loc; loc; loc = loc->next)
9699 *locp++ = loc;
9700 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 9701 bp_location_compare);
876fa593
JK
9702
9703 bp_location_target_extensions_update ();
74960c60 9704
4a64f543
MS
9705 /* Identify bp_location instances that are no longer present in the
9706 new list, and therefore should be freed. Note that it's not
9707 necessary that those locations should be removed from inferior --
9708 if there's another location at the same address (previously
9709 marked as duplicate), we don't need to remove/insert the
9710 location.
876fa593 9711
4a64f543
MS
9712 LOCP is kept in sync with OLD_LOCP, each pointing to the current
9713 and former bp_location array state respectively. */
876fa593
JK
9714
9715 locp = bp_location;
9716 for (old_locp = old_location; old_locp < old_location + old_location_count;
9717 old_locp++)
74960c60 9718 {
876fa593 9719 struct bp_location *old_loc = *old_locp;
c7d46a38 9720 struct bp_location **loc2p;
876fa593 9721
4a64f543
MS
9722 /* Tells if 'old_loc' is found amoung the new locations. If
9723 not, we have to free it. */
c7d46a38 9724 int found_object = 0;
20874c92
VP
9725 /* Tells if the location should remain inserted in the target. */
9726 int keep_in_target = 0;
9727 int removed = 0;
876fa593 9728
4a64f543
MS
9729 /* Skip LOCP entries which will definitely never be needed.
9730 Stop either at or being the one matching OLD_LOC. */
876fa593 9731 while (locp < bp_location + bp_location_count
c7d46a38 9732 && (*locp)->address < old_loc->address)
876fa593 9733 locp++;
c7d46a38
PA
9734
9735 for (loc2p = locp;
9736 (loc2p < bp_location + bp_location_count
9737 && (*loc2p)->address == old_loc->address);
9738 loc2p++)
9739 {
9740 if (*loc2p == old_loc)
9741 {
9742 found_object = 1;
9743 break;
9744 }
9745 }
74960c60 9746
4a64f543
MS
9747 /* If this location is no longer present, and inserted, look if
9748 there's maybe a new location at the same address. If so,
9749 mark that one inserted, and don't remove this one. This is
9750 needed so that we don't have a time window where a breakpoint
9751 at certain location is not inserted. */
74960c60 9752
876fa593 9753 if (old_loc->inserted)
0d381245 9754 {
4a64f543
MS
9755 /* If the location is inserted now, we might have to remove
9756 it. */
74960c60 9757
876fa593 9758 if (found_object && should_be_inserted (old_loc))
74960c60 9759 {
4a64f543
MS
9760 /* The location is still present in the location list,
9761 and still should be inserted. Don't do anything. */
20874c92 9762 keep_in_target = 1;
74960c60
VP
9763 }
9764 else
9765 {
4a64f543
MS
9766 /* The location is either no longer present, or got
9767 disabled. See if there's another location at the
9768 same address, in which case we don't need to remove
9769 this one from the target. */
876fa593 9770
2bdf28a0 9771 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
9772 if (breakpoint_address_is_meaningful (old_loc->owner))
9773 {
876fa593 9774 for (loc2p = locp;
c7d46a38
PA
9775 (loc2p < bp_location + bp_location_count
9776 && (*loc2p)->address == old_loc->address);
876fa593
JK
9777 loc2p++)
9778 {
9779 struct bp_location *loc2 = *loc2p;
9780
2d134ed3 9781 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
9782 {
9783 /* For the sake of should_be_inserted.
4a64f543
MS
9784 Duplicates check below will fix up this
9785 later. */
c7d46a38 9786 loc2->duplicate = 0;
85d721b8
PA
9787
9788 /* Read watchpoint locations are switched to
9789 access watchpoints, if the former are not
9790 supported, but the latter are. */
9791 if (is_hardware_watchpoint (old_loc->owner))
9792 {
9793 gdb_assert (is_hardware_watchpoint (loc2->owner));
9794 loc2->watchpoint_type = old_loc->watchpoint_type;
9795 }
9796
c7d46a38
PA
9797 if (loc2 != old_loc && should_be_inserted (loc2))
9798 {
9799 loc2->inserted = 1;
9800 loc2->target_info = old_loc->target_info;
9801 keep_in_target = 1;
9802 break;
9803 }
876fa593
JK
9804 }
9805 }
9806 }
74960c60
VP
9807 }
9808
20874c92
VP
9809 if (!keep_in_target)
9810 {
876fa593 9811 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 9812 {
4a64f543
MS
9813 /* This is just about all we can do. We could keep
9814 this location on the global list, and try to
9815 remove it next time, but there's no particular
9816 reason why we will succeed next time.
20874c92 9817
4a64f543
MS
9818 Note that at this point, old_loc->owner is still
9819 valid, as delete_breakpoint frees the breakpoint
9820 only after calling us. */
3e43a32a
MS
9821 printf_filtered (_("warning: Error removing "
9822 "breakpoint %d\n"),
876fa593 9823 old_loc->owner->number);
20874c92
VP
9824 }
9825 removed = 1;
9826 }
0d381245 9827 }
74960c60
VP
9828
9829 if (!found_object)
1c5cfe86 9830 {
db82e815
PA
9831 if (removed && non_stop
9832 && breakpoint_address_is_meaningful (old_loc->owner)
9833 && !is_hardware_watchpoint (old_loc->owner))
20874c92 9834 {
db82e815
PA
9835 /* This location was removed from the target. In
9836 non-stop mode, a race condition is possible where
9837 we've removed a breakpoint, but stop events for that
9838 breakpoint are already queued and will arrive later.
9839 We apply an heuristic to be able to distinguish such
9840 SIGTRAPs from other random SIGTRAPs: we keep this
9841 breakpoint location for a bit, and will retire it
9842 after we see some number of events. The theory here
9843 is that reporting of events should, "on the average",
9844 be fair, so after a while we'll see events from all
9845 threads that have anything of interest, and no longer
9846 need to keep this breakpoint location around. We
9847 don't hold locations forever so to reduce chances of
9848 mistaking a non-breakpoint SIGTRAP for a breakpoint
9849 SIGTRAP.
9850
9851 The heuristic failing can be disastrous on
9852 decr_pc_after_break targets.
9853
9854 On decr_pc_after_break targets, like e.g., x86-linux,
9855 if we fail to recognize a late breakpoint SIGTRAP,
9856 because events_till_retirement has reached 0 too
9857 soon, we'll fail to do the PC adjustment, and report
9858 a random SIGTRAP to the user. When the user resumes
9859 the inferior, it will most likely immediately crash
2dec564e 9860 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
9861 corrupted, because of being resumed e.g., in the
9862 middle of a multi-byte instruction, or skipped a
9863 one-byte instruction. This was actually seen happen
9864 on native x86-linux, and should be less rare on
9865 targets that do not support new thread events, like
9866 remote, due to the heuristic depending on
9867 thread_count.
9868
9869 Mistaking a random SIGTRAP for a breakpoint trap
9870 causes similar symptoms (PC adjustment applied when
9871 it shouldn't), but then again, playing with SIGTRAPs
9872 behind the debugger's back is asking for trouble.
9873
9874 Since hardware watchpoint traps are always
9875 distinguishable from other traps, so we don't need to
9876 apply keep hardware watchpoint moribund locations
9877 around. We simply always ignore hardware watchpoint
9878 traps we can no longer explain. */
9879
876fa593
JK
9880 old_loc->events_till_retirement = 3 * (thread_count () + 1);
9881 old_loc->owner = NULL;
20874c92 9882
876fa593 9883 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
9884 }
9885 else
f431efe5
PA
9886 {
9887 old_loc->owner = NULL;
9888 decref_bp_location (&old_loc);
9889 }
20874c92 9890 }
74960c60 9891 }
1c5cfe86 9892
2d134ed3
PA
9893 /* Rescan breakpoints at the same address and section, marking the
9894 first one as "first" and any others as "duplicates". This is so
9895 that the bpt instruction is only inserted once. If we have a
9896 permanent breakpoint at the same place as BPT, make that one the
9897 official one, and the rest as duplicates. Permanent breakpoints
9898 are sorted first for the same address.
9899
9900 Do the same for hardware watchpoints, but also considering the
9901 watchpoint's type (regular/access/read) and length. */
876fa593 9902
2d134ed3
PA
9903 bp_loc_first = NULL;
9904 wp_loc_first = NULL;
9905 awp_loc_first = NULL;
9906 rwp_loc_first = NULL;
876fa593 9907 ALL_BP_LOCATIONS (loc, locp)
74960c60 9908 {
4a64f543
MS
9909 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
9910 non-NULL. */
876fa593 9911 struct breakpoint *b = loc->owner;
2d134ed3 9912 struct bp_location **loc_first_p;
876fa593
JK
9913
9914 if (b->enable_state == bp_disabled
9915 || b->enable_state == bp_call_disabled
9916 || b->enable_state == bp_startup_disabled
9917 || !loc->enabled
9918 || loc->shlib_disabled
15c3d785 9919 || !breakpoint_address_is_meaningful (b)
d77f58be 9920 || is_tracepoint (b))
876fa593
JK
9921 continue;
9922
9923 /* Permanent breakpoint should always be inserted. */
9924 if (b->enable_state == bp_permanent && ! loc->inserted)
9925 internal_error (__FILE__, __LINE__,
9926 _("allegedly permanent breakpoint is not "
9927 "actually inserted"));
9928
2d134ed3
PA
9929 if (b->type == bp_hardware_watchpoint)
9930 loc_first_p = &wp_loc_first;
9931 else if (b->type == bp_read_watchpoint)
9932 loc_first_p = &rwp_loc_first;
9933 else if (b->type == bp_access_watchpoint)
9934 loc_first_p = &awp_loc_first;
9935 else
9936 loc_first_p = &bp_loc_first;
9937
9938 if (*loc_first_p == NULL
9939 || (overlay_debugging && loc->section != (*loc_first_p)->section)
9940 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 9941 {
2d134ed3 9942 *loc_first_p = loc;
876fa593
JK
9943 loc->duplicate = 0;
9944 continue;
9945 }
9946
9947 loc->duplicate = 1;
9948
2d134ed3
PA
9949 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
9950 && b->enable_state != bp_permanent)
876fa593
JK
9951 internal_error (__FILE__, __LINE__,
9952 _("another breakpoint was inserted on top of "
9953 "a permanent breakpoint"));
0d381245 9954 }
74960c60 9955
50c71eaf 9956 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 9957 && (have_live_inferiors ()
2567c7d9 9958 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 9959 insert_breakpoint_locations ();
f7545552
TT
9960
9961 do_cleanups (cleanups);
74960c60
VP
9962}
9963
20874c92
VP
9964void
9965breakpoint_retire_moribund (void)
9966{
9967 struct bp_location *loc;
9968 int ix;
9969
9970 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
9971 if (--(loc->events_till_retirement) == 0)
9972 {
f431efe5 9973 decref_bp_location (&loc);
20874c92
VP
9974 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
9975 --ix;
9976 }
9977}
9978
74960c60 9979static void
b60e7edf 9980update_global_location_list_nothrow (int inserting)
74960c60
VP
9981{
9982 struct gdb_exception e;
cc59ec59 9983
74960c60 9984 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 9985 update_global_location_list (inserting);
0d381245
VP
9986}
9987
f431efe5
PA
9988/* Clear BKP from a BPS. */
9989
a474d7c2 9990static void
f431efe5 9991bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
9992{
9993 bpstat bs;
cc59ec59 9994
a474d7c2 9995 for (bs = bps; bs; bs = bs->next)
f431efe5 9996 if (bs->breakpoint_at == bpt)
a474d7c2
PA
9997 {
9998 bs->breakpoint_at = NULL;
9999 bs->old_val = NULL;
10000 /* bs->commands will be freed later. */
10001 }
10002}
10003
10004/* Callback for iterate_over_threads. */
10005static int
f431efe5 10006bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10007{
f431efe5 10008 struct breakpoint *bpt = data;
cc59ec59 10009
16c381f0 10010 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10011 return 0;
10012}
10013
53a5351d 10014/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10015 structures. */
c906108c
SS
10016
10017void
fba45db2 10018delete_breakpoint (struct breakpoint *bpt)
c906108c 10019{
52f0bd74 10020 struct breakpoint *b;
c906108c 10021
8a3fe4f8 10022 gdb_assert (bpt != NULL);
c906108c 10023
4a64f543
MS
10024 /* Has this bp already been deleted? This can happen because
10025 multiple lists can hold pointers to bp's. bpstat lists are
10026 especial culprits.
10027
10028 One example of this happening is a watchpoint's scope bp. When
10029 the scope bp triggers, we notice that the watchpoint is out of
10030 scope, and delete it. We also delete its scope bp. But the
10031 scope bp is marked "auto-deleting", and is already on a bpstat.
10032 That bpstat is then checked for auto-deleting bp's, which are
10033 deleted.
10034
10035 A real solution to this problem might involve reference counts in
10036 bp's, and/or giving them pointers back to their referencing
10037 bpstat's, and teaching delete_breakpoint to only free a bp's
10038 storage when no more references were extent. A cheaper bandaid
10039 was chosen. */
c906108c
SS
10040 if (bpt->type == bp_none)
10041 return;
10042
4a64f543
MS
10043 /* At least avoid this stale reference until the reference counting
10044 of breakpoints gets resolved. */
e5a0a904
JK
10045 if (bpt->related_breakpoint != NULL)
10046 {
10047 gdb_assert (bpt->related_breakpoint->related_breakpoint == bpt);
10048 bpt->related_breakpoint->disposition = disp_del_at_next_stop;
10049 bpt->related_breakpoint->related_breakpoint = NULL;
10050 bpt->related_breakpoint = NULL;
10051 }
10052
383f836e 10053 observer_notify_breakpoint_deleted (bpt->number);
c906108c 10054
c906108c
SS
10055 if (breakpoint_chain == bpt)
10056 breakpoint_chain = bpt->next;
10057
c906108c
SS
10058 ALL_BREAKPOINTS (b)
10059 if (b->next == bpt)
c5aa993b
JM
10060 {
10061 b->next = bpt->next;
10062 break;
10063 }
c906108c 10064
9add0f1b 10065 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10066 xfree (bpt->cond_string);
10067 xfree (bpt->cond_exp);
10068 xfree (bpt->addr_string);
10069 xfree (bpt->exp);
10070 xfree (bpt->exp_string);
d63d0675 10071 xfree (bpt->exp_string_reparse);
60e1c644
PA
10072 value_free (bpt->val);
10073 xfree (bpt->source_file);
10074 xfree (bpt->exec_pathname);
a96d9b2e 10075 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 10076
f431efe5
PA
10077
10078 /* Be sure no bpstat's are pointing at the breakpoint after it's
10079 been freed. */
10080 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10081 in all threeds for now. Note that we cannot just remove bpstats
10082 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10083 commands are associated with the bpstat; if we remove it here,
10084 then the later call to bpstat_do_actions (&stop_bpstat); in
10085 event-top.c won't do anything, and temporary breakpoints with
10086 commands won't work. */
10087
10088 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10089
4a64f543
MS
10090 /* Now that breakpoint is removed from breakpoint list, update the
10091 global location list. This will remove locations that used to
10092 belong to this breakpoint. Do this before freeing the breakpoint
10093 itself, since remove_breakpoint looks at location's owner. It
10094 might be better design to have location completely
10095 self-contained, but it's not the case now. */
b60e7edf 10096 update_global_location_list (0);
74960c60
VP
10097
10098
4a64f543
MS
10099 /* On the chance that someone will soon try again to delete this
10100 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10101 bpt->type = bp_none;
10102
b8c9b27d 10103 xfree (bpt);
c906108c
SS
10104}
10105
4d6140d9
AC
10106static void
10107do_delete_breakpoint_cleanup (void *b)
10108{
10109 delete_breakpoint (b);
10110}
10111
10112struct cleanup *
10113make_cleanup_delete_breakpoint (struct breakpoint *b)
10114{
10115 return make_cleanup (do_delete_breakpoint_cleanup, b);
10116}
10117
95a42b64
TT
10118/* A callback for map_breakpoint_numbers that calls
10119 delete_breakpoint. */
10120
10121static void
10122do_delete_breakpoint (struct breakpoint *b, void *ignore)
10123{
10124 delete_breakpoint (b);
10125}
10126
c906108c 10127void
fba45db2 10128delete_command (char *arg, int from_tty)
c906108c 10129{
35df4500 10130 struct breakpoint *b, *b_tmp;
c906108c 10131
ea9365bb
TT
10132 dont_repeat ();
10133
c906108c
SS
10134 if (arg == 0)
10135 {
10136 int breaks_to_delete = 0;
10137
10138 /* Delete all breakpoints if no argument.
4a64f543
MS
10139 Do not delete internal or call-dummy breakpoints, these have
10140 to be deleted with an explicit breakpoint number argument. */
c5aa993b
JM
10141 ALL_BREAKPOINTS (b)
10142 {
059fb39f 10143 if (b->type != bp_call_dummy
aa7d318d 10144 && b->type != bp_std_terminate
059fb39f 10145 && b->type != bp_shlib_event
4efc6507 10146 && b->type != bp_jit_event
059fb39f
PM
10147 && b->type != bp_thread_event
10148 && b->type != bp_overlay_event
0fd8e87f 10149 && b->type != bp_longjmp_master
aa7d318d 10150 && b->type != bp_std_terminate_master
186c406b 10151 && b->type != bp_exception_master
059fb39f 10152 && b->number >= 0)
973d738b
DJ
10153 {
10154 breaks_to_delete = 1;
10155 break;
10156 }
c5aa993b 10157 }
c906108c
SS
10158
10159 /* Ask user only if there are some breakpoints to delete. */
10160 if (!from_tty
e2e0b3e5 10161 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10162 {
35df4500 10163 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10164 {
059fb39f 10165 if (b->type != bp_call_dummy
aa7d318d 10166 && b->type != bp_std_terminate
059fb39f
PM
10167 && b->type != bp_shlib_event
10168 && b->type != bp_thread_event
4efc6507 10169 && b->type != bp_jit_event
059fb39f 10170 && b->type != bp_overlay_event
0fd8e87f 10171 && b->type != bp_longjmp_master
aa7d318d 10172 && b->type != bp_std_terminate_master
186c406b 10173 && b->type != bp_exception_master
059fb39f 10174 && b->number >= 0)
c5aa993b
JM
10175 delete_breakpoint (b);
10176 }
c906108c
SS
10177 }
10178 }
10179 else
95a42b64 10180 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
10181}
10182
0d381245
VP
10183static int
10184all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 10185{
0d381245
VP
10186 for (; loc; loc = loc->next)
10187 if (!loc->shlib_disabled)
10188 return 0;
10189 return 1;
fe3f5fa8
VP
10190}
10191
776592bf
DE
10192/* Subroutine of update_breakpoint_locations to simplify it.
10193 Return non-zero if multiple fns in list LOC have the same name.
10194 Null names are ignored. */
10195
10196static int
10197ambiguous_names_p (struct bp_location *loc)
10198{
10199 struct bp_location *l;
10200 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
10201 (int (*) (const void *,
10202 const void *)) streq,
776592bf
DE
10203 NULL, xcalloc, xfree);
10204
10205 for (l = loc; l != NULL; l = l->next)
10206 {
10207 const char **slot;
10208 const char *name = l->function_name;
10209
10210 /* Allow for some names to be NULL, ignore them. */
10211 if (name == NULL)
10212 continue;
10213
10214 slot = (const char **) htab_find_slot (htab, (const void *) name,
10215 INSERT);
4a64f543
MS
10216 /* NOTE: We can assume slot != NULL here because xcalloc never
10217 returns NULL. */
776592bf
DE
10218 if (*slot != NULL)
10219 {
10220 htab_delete (htab);
10221 return 1;
10222 }
10223 *slot = name;
10224 }
10225
10226 htab_delete (htab);
10227 return 0;
10228}
10229
0fb4aa4b
PA
10230/* When symbols change, it probably means the sources changed as well,
10231 and it might mean the static tracepoint markers are no longer at
10232 the same address or line numbers they used to be at last we
10233 checked. Losing your static tracepoints whenever you rebuild is
10234 undesirable. This function tries to resync/rematch gdb static
10235 tracepoints with the markers on the target, for static tracepoints
10236 that have not been set by marker id. Static tracepoint that have
10237 been set by marker id are reset by marker id in breakpoint_re_set.
10238 The heuristic is:
10239
10240 1) For a tracepoint set at a specific address, look for a marker at
10241 the old PC. If one is found there, assume to be the same marker.
10242 If the name / string id of the marker found is different from the
10243 previous known name, assume that means the user renamed the marker
10244 in the sources, and output a warning.
10245
10246 2) For a tracepoint set at a given line number, look for a marker
10247 at the new address of the old line number. If one is found there,
10248 assume to be the same marker. If the name / string id of the
10249 marker found is different from the previous known name, assume that
10250 means the user renamed the marker in the sources, and output a
10251 warning.
10252
10253 3) If a marker is no longer found at the same address or line, it
10254 may mean the marker no longer exists. But it may also just mean
10255 the code changed a bit. Maybe the user added a few lines of code
10256 that made the marker move up or down (in line number terms). Ask
10257 the target for info about the marker with the string id as we knew
10258 it. If found, update line number and address in the matching
10259 static tracepoint. This will get confused if there's more than one
10260 marker with the same ID (possible in UST, although unadvised
10261 precisely because it confuses tools). */
10262
10263static struct symtab_and_line
10264update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10265{
10266 struct static_tracepoint_marker marker;
10267 CORE_ADDR pc;
10268 int i;
10269
10270 pc = sal.pc;
10271 if (sal.line)
10272 find_line_pc (sal.symtab, sal.line, &pc);
10273
10274 if (target_static_tracepoint_marker_at (pc, &marker))
10275 {
10276 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10277 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10278 b->number,
10279 b->static_trace_marker_id, marker.str_id);
10280
10281 xfree (b->static_trace_marker_id);
10282 b->static_trace_marker_id = xstrdup (marker.str_id);
10283 release_static_tracepoint_marker (&marker);
10284
10285 return sal;
10286 }
10287
10288 /* Old marker wasn't found on target at lineno. Try looking it up
10289 by string ID. */
10290 if (!sal.explicit_pc
10291 && sal.line != 0
10292 && sal.symtab != NULL
10293 && b->static_trace_marker_id != NULL)
10294 {
10295 VEC(static_tracepoint_marker_p) *markers;
10296
10297 markers
10298 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10299
10300 if (!VEC_empty(static_tracepoint_marker_p, markers))
10301 {
10302 struct symtab_and_line sal;
10303 struct symbol *sym;
10304 struct static_tracepoint_marker *marker;
10305
10306 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10307
10308 xfree (b->static_trace_marker_id);
10309 b->static_trace_marker_id = xstrdup (marker->str_id);
10310
10311 warning (_("marker for static tracepoint %d (%s) not "
10312 "found at previous line number"),
10313 b->number, b->static_trace_marker_id);
10314
10315 init_sal (&sal);
10316
10317 sal.pc = marker->address;
10318
10319 sal = find_pc_line (marker->address, 0);
10320 sym = find_pc_sect_function (marker->address, NULL);
10321 ui_out_text (uiout, "Now in ");
10322 if (sym)
10323 {
10324 ui_out_field_string (uiout, "func",
10325 SYMBOL_PRINT_NAME (sym));
10326 ui_out_text (uiout, " at ");
10327 }
10328 ui_out_field_string (uiout, "file", sal.symtab->filename);
10329 ui_out_text (uiout, ":");
10330
10331 if (ui_out_is_mi_like_p (uiout))
10332 {
10333 char *fullname = symtab_to_fullname (sal.symtab);
10334
10335 if (fullname)
10336 ui_out_field_string (uiout, "fullname", fullname);
10337 }
10338
10339 ui_out_field_int (uiout, "line", sal.line);
10340 ui_out_text (uiout, "\n");
10341
10342 b->line_number = sal.line;
10343
10344 xfree (b->source_file);
10345 if (sym)
10346 b->source_file = xstrdup (sal.symtab->filename);
10347 else
10348 b->source_file = NULL;
10349
10350 xfree (b->addr_string);
10351 b->addr_string = xstrprintf ("%s:%d",
10352 sal.symtab->filename, b->line_number);
10353
10354 /* Might be nice to check if function changed, and warn if
10355 so. */
10356
10357 release_static_tracepoint_marker (marker);
10358 }
10359 }
10360 return sal;
10361}
10362
fe3f5fa8 10363static void
0d381245
VP
10364update_breakpoint_locations (struct breakpoint *b,
10365 struct symtabs_and_lines sals)
fe3f5fa8
VP
10366{
10367 int i;
10368 char *s;
0d381245
VP
10369 struct bp_location *existing_locations = b->loc;
10370
4a64f543
MS
10371 /* If there's no new locations, and all existing locations are
10372 pending, don't do anything. This optimizes the common case where
10373 all locations are in the same shared library, that was unloaded.
10374 We'd like to retain the location, so that when the library is
10375 loaded again, we don't loose the enabled/disabled status of the
10376 individual locations. */
0d381245 10377 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10378 return;
10379
fe3f5fa8
VP
10380 b->loc = NULL;
10381
0d381245 10382 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10383 {
0d381245 10384 struct bp_location *new_loc =
39d61571 10385 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10386
0d381245
VP
10387 /* Reparse conditions, they might contain references to the
10388 old symtab. */
10389 if (b->cond_string != NULL)
10390 {
10391 struct gdb_exception e;
fe3f5fa8 10392
0d381245
VP
10393 s = b->cond_string;
10394 TRY_CATCH (e, RETURN_MASK_ERROR)
10395 {
10396 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10397 0);
10398 }
10399 if (e.reason < 0)
10400 {
3e43a32a
MS
10401 warning (_("failed to reevaluate condition "
10402 "for breakpoint %d: %s"),
0d381245
VP
10403 b->number, e.message);
10404 new_loc->enabled = 0;
10405 }
10406 }
fe3f5fa8 10407
0d381245
VP
10408 if (b->source_file != NULL)
10409 xfree (b->source_file);
10410 if (sals.sals[i].symtab == NULL)
10411 b->source_file = NULL;
10412 else
1b36a34b 10413 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10414
0d381245
VP
10415 if (b->line_number == 0)
10416 b->line_number = sals.sals[i].line;
10417 }
fe3f5fa8 10418
514f746b
AR
10419 /* Update locations of permanent breakpoints. */
10420 if (b->enable_state == bp_permanent)
10421 make_breakpoint_permanent (b);
10422
4a64f543
MS
10423 /* If possible, carry over 'disable' status from existing
10424 breakpoints. */
0d381245
VP
10425 {
10426 struct bp_location *e = existing_locations;
776592bf
DE
10427 /* If there are multiple breakpoints with the same function name,
10428 e.g. for inline functions, comparing function names won't work.
10429 Instead compare pc addresses; this is just a heuristic as things
10430 may have moved, but in practice it gives the correct answer
10431 often enough until a better solution is found. */
10432 int have_ambiguous_names = ambiguous_names_p (b->loc);
10433
0d381245
VP
10434 for (; e; e = e->next)
10435 {
10436 if (!e->enabled && e->function_name)
10437 {
10438 struct bp_location *l = b->loc;
776592bf
DE
10439 if (have_ambiguous_names)
10440 {
10441 for (; l; l = l->next)
6c95b8df
PA
10442 if (breakpoint_address_match (e->pspace->aspace, e->address,
10443 l->pspace->aspace, l->address))
776592bf
DE
10444 {
10445 l->enabled = 0;
10446 break;
10447 }
10448 }
10449 else
10450 {
10451 for (; l; l = l->next)
10452 if (l->function_name
10453 && strcmp (e->function_name, l->function_name) == 0)
10454 {
10455 l->enabled = 0;
10456 break;
10457 }
10458 }
0d381245
VP
10459 }
10460 }
10461 }
fe3f5fa8 10462
b60e7edf 10463 update_global_location_list (1);
fe3f5fa8
VP
10464}
10465
c906108c
SS
10466/* Reset a breakpoint given it's struct breakpoint * BINT.
10467 The value we return ends up being the return value from catch_errors.
10468 Unused in this case. */
10469
10470static int
4efb68b1 10471breakpoint_re_set_one (void *bint)
c906108c 10472{
4a64f543 10473 /* Get past catch_errs. */
53a5351d 10474 struct breakpoint *b = (struct breakpoint *) bint;
fe3f5fa8
VP
10475 int not_found = 0;
10476 int *not_found_ptr = &not_found;
6c95b8df
PA
10477 struct symtabs_and_lines sals = {0};
10478 struct symtabs_and_lines expanded = {0};
c906108c 10479 char *s;
fe3f5fa8 10480 struct gdb_exception e;
6c95b8df 10481 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
0fb4aa4b 10482 int marker_spec = 0;
c906108c
SS
10483
10484 switch (b->type)
10485 {
10486 case bp_none:
8a3fe4f8 10487 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 10488 b->number);
c906108c
SS
10489 return 0;
10490 case bp_breakpoint:
10491 case bp_hardware_breakpoint:
1042e4c0 10492 case bp_tracepoint:
7a697b8d 10493 case bp_fast_tracepoint:
0fb4aa4b 10494 case bp_static_tracepoint:
8bea4e01
UW
10495 /* Do not attempt to re-set breakpoints disabled during startup. */
10496 if (b->enable_state == bp_startup_disabled)
10497 return 0;
10498
c906108c
SS
10499 if (b->addr_string == NULL)
10500 {
4a64f543 10501 /* Anything without a string can't be re-set. */
c906108c
SS
10502 delete_breakpoint (b);
10503 return 0;
10504 }
c906108c 10505
c906108c
SS
10506 input_radix = b->input_radix;
10507 s = b->addr_string;
6c95b8df
PA
10508
10509 save_current_space_and_thread ();
10510 switch_to_program_space_and_thread (b->pspace);
10511
0fb4aa4b
PA
10512 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10513
385d04dc 10514 set_language (b->language);
fe3f5fa8 10515 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 10516 {
0fb4aa4b
PA
10517 if (marker_spec)
10518 {
10519 sals = decode_static_tracepoint_spec (&s);
10520 if (sals.nelts > b->static_trace_marker_id_idx)
10521 {
10522 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10523 sals.nelts = 1;
10524 }
10525 else
10526 error (_("marker %s not found"), b->static_trace_marker_id);
10527 }
10528 else
3e43a32a
MS
10529 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10530 (char ***) NULL, not_found_ptr);
fe3f5fa8
VP
10531 }
10532 if (e.reason < 0)
10533 {
10534 int not_found_and_ok = 0;
4a64f543
MS
10535 /* For pending breakpoints, it's expected that parsing will
10536 fail until the right shared library is loaded. User has
10537 already told to create pending breakpoints and don't need
10538 extra messages. If breakpoint is in bp_shlib_disabled
10539 state, then user already saw the message about that
10540 breakpoint being disabled, and don't want to see more
10541 errors. */
0d381245
VP
10542 if (not_found
10543 && (b->condition_not_parsed
10544 || (b->loc && b->loc->shlib_disabled)
10545 || b->enable_state == bp_disabled))
fe3f5fa8
VP
10546 not_found_and_ok = 1;
10547
10548 if (!not_found_and_ok)
c906108c 10549 {
fe3f5fa8
VP
10550 /* We surely don't want to warn about the same breakpoint
10551 10 times. One solution, implemented here, is disable
10552 the breakpoint on error. Another solution would be to
10553 have separate 'warning emitted' flag. Since this
10554 happens only when a binary has changed, I don't know
10555 which approach is better. */
10556 b->enable_state = bp_disabled;
10557 throw_exception (e);
c906108c 10558 }
fe3f5fa8 10559 }
c906108c 10560
6c95b8df 10561 if (!not_found)
fe3f5fa8 10562 {
6c95b8df
PA
10563 gdb_assert (sals.nelts == 1);
10564
10565 resolve_sal_pc (&sals.sals[0]);
10566 if (b->condition_not_parsed && s && s[0])
10567 {
10568 char *cond_string = 0;
10569 int thread = -1;
10570 int task = 0;
10571
10572 find_condition_and_thread (s, sals.sals[0].pc,
10573 &cond_string, &thread, &task);
10574 if (cond_string)
10575 b->cond_string = cond_string;
10576 b->thread = thread;
10577 b->task = task;
10578 b->condition_not_parsed = 0;
10579 }
10580
0fb4aa4b
PA
10581 if (b->type == bp_static_tracepoint && !marker_spec)
10582 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10583
6c95b8df 10584 expanded = expand_line_sal_maybe (sals.sals[0]);
fe3f5fa8 10585 }
6c95b8df
PA
10586
10587 make_cleanup (xfree, sals.sals);
ed0616c6 10588 update_breakpoint_locations (b, expanded);
c906108c
SS
10589 break;
10590
10591 case bp_watchpoint:
10592 case bp_hardware_watchpoint:
10593 case bp_read_watchpoint:
10594 case bp_access_watchpoint:
4a64f543
MS
10595 /* Watchpoint can be either on expression using entirely global
10596 variables, or it can be on local variables.
10597
10598 Watchpoints of the first kind are never auto-deleted, and
10599 even persist across program restarts. Since they can use
10600 variables from shared libraries, we need to reparse
10601 expression as libraries are loaded and unloaded.
10602
10603 Watchpoints on local variables can also change meaning as
10604 result of solib event. For example, if a watchpoint uses
10605 both a local and a global variables in expression, it's a
10606 local watchpoint, but unloading of a shared library will make
10607 the expression invalid. This is not a very common use case,
10608 but we still re-evaluate expression, to avoid surprises to
10609 the user.
0b3de036
VP
10610
10611 Note that for local watchpoints, we re-evaluate it only if
10612 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
10613 the watchpoint is out of scope and will be deleted soon. In
10614 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
10615
10616 If a local watchpoint's frame id is still valid, then
4a64f543 10617 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
10618
10619 Don't do anything about disabled watchpoints, since they will
10620 be reevaluated again when enabled. */
a5606eee 10621 update_watchpoint (b, 1 /* reparse */);
c906108c 10622 break;
c5aa993b
JM
10623 /* We needn't really do anything to reset these, since the mask
10624 that requests them is unaffected by e.g., new libraries being
4a64f543 10625 loaded. */
ce78b96d 10626 case bp_catchpoint:
c906108c 10627 break;
c5aa993b 10628
c906108c 10629 default:
a3f17187 10630 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 10631 /* fall through */
0fd8e87f
UW
10632 /* Delete overlay event and longjmp master breakpoints; they will be
10633 reset later by breakpoint_re_set. */
1900040c 10634 case bp_overlay_event:
0fd8e87f 10635 case bp_longjmp_master:
aa7d318d 10636 case bp_std_terminate_master:
186c406b 10637 case bp_exception_master:
c906108c
SS
10638 delete_breakpoint (b);
10639 break;
10640
c5aa993b
JM
10641 /* This breakpoint is special, it's set up when the inferior
10642 starts and we really don't want to touch it. */
c906108c
SS
10643 case bp_shlib_event:
10644
c4093a6a
JM
10645 /* Like bp_shlib_event, this breakpoint type is special.
10646 Once it is set up, we do not want to touch it. */
10647 case bp_thread_event:
10648
4a64f543
MS
10649 /* Keep temporary breakpoints, which can be encountered when we
10650 step over a dlopen call and SOLIB_ADD is resetting the
10651 breakpoints. Otherwise these should have been blown away via
10652 the cleanup chain or by breakpoint_init_inferior when we
10653 rerun the executable. */
c906108c
SS
10654 case bp_until:
10655 case bp_finish:
10656 case bp_watchpoint_scope:
10657 case bp_call_dummy:
aa7d318d 10658 case bp_std_terminate:
c906108c 10659 case bp_step_resume:
611c83ae
PA
10660 case bp_longjmp:
10661 case bp_longjmp_resume:
186c406b
TT
10662 case bp_exception:
10663 case bp_exception_resume:
4efc6507 10664 case bp_jit_event:
c906108c
SS
10665 break;
10666 }
10667
6c95b8df 10668 do_cleanups (cleanups);
c906108c
SS
10669 return 0;
10670}
10671
69de3c6a 10672/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 10673void
69de3c6a 10674breakpoint_re_set (void)
c906108c 10675{
35df4500 10676 struct breakpoint *b, *b_tmp;
c906108c
SS
10677 enum language save_language;
10678 int save_input_radix;
6c95b8df 10679 struct cleanup *old_chain;
c5aa993b 10680
c906108c
SS
10681 save_language = current_language->la_language;
10682 save_input_radix = input_radix;
6c95b8df
PA
10683 old_chain = save_current_program_space ();
10684
35df4500 10685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10686 {
4a64f543 10687 /* Format possible error msg. */
fe3f5fa8 10688 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
10689 b->number);
10690 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 10691 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 10692 do_cleanups (cleanups);
c5aa993b 10693 }
c906108c
SS
10694 set_language (save_language);
10695 input_radix = save_input_radix;
e62c965a 10696
0756c555 10697 jit_breakpoint_re_set ();
4efc6507 10698
6c95b8df
PA
10699 do_cleanups (old_chain);
10700
af02033e
PP
10701 create_overlay_event_breakpoint ();
10702 create_longjmp_master_breakpoint ();
10703 create_std_terminate_master_breakpoint ();
186c406b 10704 create_exception_master_breakpoint ();
c906108c
SS
10705}
10706\f
c906108c
SS
10707/* Reset the thread number of this breakpoint:
10708
10709 - If the breakpoint is for all threads, leave it as-is.
4a64f543 10710 - Else, reset it to the current thread for inferior_ptid. */
c906108c 10711void
fba45db2 10712breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
10713{
10714 if (b->thread != -1)
10715 {
39f77062
KB
10716 if (in_thread_list (inferior_ptid))
10717 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
10718
10719 /* We're being called after following a fork. The new fork is
10720 selected as current, and unless this was a vfork will have a
10721 different program space from the original thread. Reset that
10722 as well. */
10723 b->loc->pspace = current_program_space;
c906108c
SS
10724 }
10725}
10726
03ac34d5
MS
10727/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10728 If from_tty is nonzero, it prints a message to that effect,
10729 which ends with a period (no newline). */
10730
c906108c 10731void
fba45db2 10732set_ignore_count (int bptnum, int count, int from_tty)
c906108c 10733{
52f0bd74 10734 struct breakpoint *b;
c906108c
SS
10735
10736 if (count < 0)
10737 count = 0;
10738
10739 ALL_BREAKPOINTS (b)
10740 if (b->number == bptnum)
c5aa993b 10741 {
d77f58be
SS
10742 if (is_tracepoint (b))
10743 {
10744 if (from_tty && count != 0)
10745 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10746 bptnum);
10747 return;
10748 }
10749
c5aa993b 10750 b->ignore_count = count;
221ea385
KS
10751 if (from_tty)
10752 {
10753 if (count == 0)
3e43a32a
MS
10754 printf_filtered (_("Will stop next time "
10755 "breakpoint %d is reached."),
221ea385
KS
10756 bptnum);
10757 else if (count == 1)
a3f17187 10758 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
10759 bptnum);
10760 else
3e43a32a
MS
10761 printf_filtered (_("Will ignore next %d "
10762 "crossings of breakpoint %d."),
221ea385
KS
10763 count, bptnum);
10764 }
c5aa993b 10765 breakpoints_changed ();
383f836e 10766 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
10767 return;
10768 }
c906108c 10769
8a3fe4f8 10770 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
10771}
10772
c906108c
SS
10773/* Command to set ignore-count of breakpoint N to COUNT. */
10774
10775static void
fba45db2 10776ignore_command (char *args, int from_tty)
c906108c
SS
10777{
10778 char *p = args;
52f0bd74 10779 int num;
c906108c
SS
10780
10781 if (p == 0)
e2e0b3e5 10782 error_no_arg (_("a breakpoint number"));
c5aa993b 10783
c906108c 10784 num = get_number (&p);
5c44784c 10785 if (num == 0)
8a3fe4f8 10786 error (_("bad breakpoint number: '%s'"), args);
c906108c 10787 if (*p == 0)
8a3fe4f8 10788 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
10789
10790 set_ignore_count (num,
10791 longest_to_int (value_as_long (parse_and_eval (p))),
10792 from_tty);
221ea385
KS
10793 if (from_tty)
10794 printf_filtered ("\n");
c906108c
SS
10795}
10796\f
10797/* Call FUNCTION on each of the breakpoints
10798 whose numbers are given in ARGS. */
10799
10800static void
95a42b64
TT
10801map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10802 void *),
10803 void *data)
c906108c 10804{
52f0bd74 10805 char *p = args;
c906108c 10806 char *p1;
52f0bd74
AC
10807 int num;
10808 struct breakpoint *b, *tmp;
11cf8741 10809 int match;
c906108c
SS
10810
10811 if (p == 0)
e2e0b3e5 10812 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
10813
10814 while (*p)
10815 {
11cf8741 10816 match = 0;
c906108c 10817 p1 = p;
c5aa993b 10818
5c44784c
JM
10819 num = get_number_or_range (&p1);
10820 if (num == 0)
c5aa993b 10821 {
8a3fe4f8 10822 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
10823 }
10824 else
10825 {
10826 ALL_BREAKPOINTS_SAFE (b, tmp)
10827 if (b->number == num)
10828 {
10829 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 10830 match = 1;
95a42b64 10831 function (b, data);
5c44784c 10832 if (related_breakpoint)
95a42b64 10833 function (related_breakpoint, data);
11cf8741 10834 break;
5c44784c 10835 }
11cf8741 10836 if (match == 0)
a3f17187 10837 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 10838 }
c906108c
SS
10839 p = p1;
10840 }
10841}
10842
0d381245
VP
10843static struct bp_location *
10844find_location_by_number (char *number)
10845{
10846 char *dot = strchr (number, '.');
10847 char *p1;
10848 int bp_num;
10849 int loc_num;
10850 struct breakpoint *b;
10851 struct bp_location *loc;
10852
10853 *dot = '\0';
10854
10855 p1 = number;
10856 bp_num = get_number_or_range (&p1);
10857 if (bp_num == 0)
10858 error (_("Bad breakpoint number '%s'"), number);
10859
10860 ALL_BREAKPOINTS (b)
10861 if (b->number == bp_num)
10862 {
10863 break;
10864 }
10865
10866 if (!b || b->number != bp_num)
10867 error (_("Bad breakpoint number '%s'"), number);
10868
10869 p1 = dot+1;
10870 loc_num = get_number_or_range (&p1);
10871 if (loc_num == 0)
10872 error (_("Bad breakpoint location number '%s'"), number);
10873
10874 --loc_num;
10875 loc = b->loc;
10876 for (;loc_num && loc; --loc_num, loc = loc->next)
10877 ;
10878 if (!loc)
10879 error (_("Bad breakpoint location number '%s'"), dot+1);
10880
10881 return loc;
10882}
10883
10884
1900040c
MS
10885/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10886 If from_tty is nonzero, it prints a message to that effect,
10887 which ends with a period (no newline). */
10888
c906108c 10889void
fba45db2 10890disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
10891{
10892 /* Never disable a watchpoint scope breakpoint; we want to
10893 hit them when we leave scope so we can delete both the
10894 watchpoint and its scope breakpoint at that time. */
10895 if (bpt->type == bp_watchpoint_scope)
10896 return;
10897
c2c6d25f 10898 /* You can't disable permanent breakpoints. */
b5de0fa7 10899 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
10900 return;
10901
b5de0fa7 10902 bpt->enable_state = bp_disabled;
c906108c 10903
b60e7edf 10904 update_global_location_list (0);
c906108c 10905
383f836e 10906 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
10907}
10908
95a42b64
TT
10909/* A callback for map_breakpoint_numbers that calls
10910 disable_breakpoint. */
10911
10912static void
10913do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
10914{
10915 disable_breakpoint (b);
10916}
10917
c906108c 10918static void
fba45db2 10919disable_command (char *args, int from_tty)
c906108c 10920{
52f0bd74 10921 struct breakpoint *bpt;
cc59ec59 10922
c906108c
SS
10923 if (args == 0)
10924 ALL_BREAKPOINTS (bpt)
10925 switch (bpt->type)
c5aa993b
JM
10926 {
10927 case bp_none:
8a3fe4f8 10928 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 10929 bpt->number);
c5aa993b
JM
10930 continue;
10931 case bp_breakpoint:
1042e4c0 10932 case bp_tracepoint:
7a697b8d 10933 case bp_fast_tracepoint:
0fb4aa4b 10934 case bp_static_tracepoint:
ce78b96d 10935 case bp_catchpoint:
c5aa993b
JM
10936 case bp_hardware_breakpoint:
10937 case bp_watchpoint:
10938 case bp_hardware_watchpoint:
10939 case bp_read_watchpoint:
10940 case bp_access_watchpoint:
10941 disable_breakpoint (bpt);
10942 default:
10943 continue;
10944 }
0d381245
VP
10945 else if (strchr (args, '.'))
10946 {
10947 struct bp_location *loc = find_location_by_number (args);
10948 if (loc)
10949 loc->enabled = 0;
b60e7edf 10950 update_global_location_list (0);
0d381245 10951 }
c906108c 10952 else
95a42b64 10953 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
10954}
10955
10956static void
fba45db2 10957do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 10958{
afe38095 10959 int target_resources_ok;
c906108c
SS
10960
10961 if (bpt->type == bp_hardware_breakpoint)
10962 {
10963 int i;
c5aa993b 10964 i = hw_breakpoint_used_count ();
53a5351d 10965 target_resources_ok =
d92524f1 10966 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 10967 i + 1, 0);
c906108c 10968 if (target_resources_ok == 0)
8a3fe4f8 10969 error (_("No hardware breakpoint support in the target."));
c906108c 10970 else if (target_resources_ok < 0)
8a3fe4f8 10971 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
10972 }
10973
cc60f2e3 10974 if (is_watchpoint (bpt))
c906108c 10975 {
dde02812
ES
10976 struct gdb_exception e;
10977
10978 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 10979 {
dde02812 10980 update_watchpoint (bpt, 1 /* reparse */);
c906108c 10981 }
dde02812 10982 if (e.reason < 0)
c5aa993b 10983 {
dde02812
ES
10984 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
10985 bpt->number);
10986 return;
c5aa993b 10987 }
c906108c 10988 }
0101ce28 10989
b4c291bb
KH
10990 if (bpt->enable_state != bp_permanent)
10991 bpt->enable_state = bp_enabled;
10992 bpt->disposition = disposition;
b60e7edf 10993 update_global_location_list (1);
b4c291bb
KH
10994 breakpoints_changed ();
10995
383f836e 10996 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
10997}
10998
fe3f5fa8 10999
c906108c 11000void
fba45db2 11001enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11002{
11003 do_enable_breakpoint (bpt, bpt->disposition);
11004}
11005
95a42b64
TT
11006/* A callback for map_breakpoint_numbers that calls
11007 enable_breakpoint. */
11008
11009static void
11010do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11011{
11012 enable_breakpoint (b);
11013}
11014
c906108c
SS
11015/* The enable command enables the specified breakpoints (or all defined
11016 breakpoints) so they once again become (or continue to be) effective
1272ad14 11017 in stopping the inferior. */
c906108c 11018
c906108c 11019static void
fba45db2 11020enable_command (char *args, int from_tty)
c906108c 11021{
52f0bd74 11022 struct breakpoint *bpt;
cc59ec59 11023
c906108c
SS
11024 if (args == 0)
11025 ALL_BREAKPOINTS (bpt)
11026 switch (bpt->type)
c5aa993b
JM
11027 {
11028 case bp_none:
8a3fe4f8 11029 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 11030 bpt->number);
c5aa993b
JM
11031 continue;
11032 case bp_breakpoint:
1042e4c0 11033 case bp_tracepoint:
7a697b8d 11034 case bp_fast_tracepoint:
0fb4aa4b 11035 case bp_static_tracepoint:
ce78b96d 11036 case bp_catchpoint:
c5aa993b
JM
11037 case bp_hardware_breakpoint:
11038 case bp_watchpoint:
11039 case bp_hardware_watchpoint:
11040 case bp_read_watchpoint:
11041 case bp_access_watchpoint:
11042 enable_breakpoint (bpt);
11043 default:
11044 continue;
11045 }
0d381245
VP
11046 else if (strchr (args, '.'))
11047 {
11048 struct bp_location *loc = find_location_by_number (args);
11049 if (loc)
11050 loc->enabled = 1;
b60e7edf 11051 update_global_location_list (1);
0d381245 11052 }
c906108c 11053 else
95a42b64 11054 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
11055}
11056
11057static void
95a42b64 11058enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11059{
b5de0fa7 11060 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
11061}
11062
c906108c 11063static void
fba45db2 11064enable_once_command (char *args, int from_tty)
c906108c 11065{
95a42b64 11066 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
11067}
11068
11069static void
95a42b64 11070enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11071{
b5de0fa7 11072 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
11073}
11074
c906108c 11075static void
fba45db2 11076enable_delete_command (char *args, int from_tty)
c906108c 11077{
95a42b64 11078 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
11079}
11080\f
fa8d40ab
JJ
11081static void
11082set_breakpoint_cmd (char *args, int from_tty)
11083{
11084}
11085
11086static void
11087show_breakpoint_cmd (char *args, int from_tty)
11088{
11089}
11090
1f3b5d1b
PP
11091/* Invalidate last known value of any hardware watchpoint if
11092 the memory which that value represents has been written to by
11093 GDB itself. */
11094
11095static void
11096invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11097 const bfd_byte *data)
11098{
11099 struct breakpoint *bp;
11100
11101 ALL_BREAKPOINTS (bp)
11102 if (bp->enable_state == bp_enabled
11103 && bp->type == bp_hardware_watchpoint
11104 && bp->val_valid && bp->val)
11105 {
11106 struct bp_location *loc;
11107
11108 for (loc = bp->loc; loc != NULL; loc = loc->next)
11109 if (loc->loc_type == bp_loc_hardware_watchpoint
11110 && loc->address + loc->length > addr
11111 && addr + len > loc->address)
11112 {
11113 value_free (bp->val);
11114 bp->val = NULL;
11115 bp->val_valid = 0;
11116 }
11117 }
11118}
11119
c906108c
SS
11120/* Use default_breakpoint_'s, or nothing if they aren't valid. */
11121
11122struct symtabs_and_lines
fba45db2 11123decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
11124{
11125 struct symtabs_and_lines sals;
cc59ec59 11126
c906108c 11127 if (string == 0)
8a3fe4f8 11128 error (_("Empty line specification."));
c906108c
SS
11129 if (default_breakpoint_valid)
11130 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
11131 default_breakpoint_symtab,
11132 default_breakpoint_line,
68219205 11133 (char ***) NULL, NULL);
c906108c
SS
11134 else
11135 sals = decode_line_1 (&string, funfirstline,
68219205 11136 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 11137 if (*string)
8a3fe4f8 11138 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
11139 return sals;
11140}
8181d85f
DJ
11141
11142/* Create and insert a raw software breakpoint at PC. Return an
11143 identifier, which should be used to remove the breakpoint later.
11144 In general, places which call this should be using something on the
11145 breakpoint chain instead; this function should be eliminated
11146 someday. */
11147
11148void *
6c95b8df
PA
11149deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11150 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
11151{
11152 struct bp_target_info *bp_tgt;
11153
6c95b8df 11154 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 11155
6c95b8df 11156 bp_tgt->placed_address_space = aspace;
8181d85f 11157 bp_tgt->placed_address = pc;
6c95b8df 11158
a6d9a66e 11159 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
11160 {
11161 /* Could not insert the breakpoint. */
11162 xfree (bp_tgt);
11163 return NULL;
11164 }
11165
11166 return bp_tgt;
11167}
11168
4a64f543
MS
11169/* Remove a breakpoint BP inserted by
11170 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
11171
11172int
a6d9a66e 11173deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
11174{
11175 struct bp_target_info *bp_tgt = bp;
11176 int ret;
11177
a6d9a66e 11178 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
11179 xfree (bp_tgt);
11180
11181 return ret;
11182}
11183
4a64f543
MS
11184/* One (or perhaps two) breakpoints used for software single
11185 stepping. */
8181d85f
DJ
11186
11187static void *single_step_breakpoints[2];
a6d9a66e 11188static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
11189
11190/* Create and insert a breakpoint for software single step. */
11191
11192void
6c95b8df 11193insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
11194 struct address_space *aspace,
11195 CORE_ADDR next_pc)
8181d85f
DJ
11196{
11197 void **bpt_p;
11198
11199 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
11200 {
11201 bpt_p = &single_step_breakpoints[0];
11202 single_step_gdbarch[0] = gdbarch;
11203 }
8181d85f
DJ
11204 else
11205 {
11206 gdb_assert (single_step_breakpoints[1] == NULL);
11207 bpt_p = &single_step_breakpoints[1];
a6d9a66e 11208 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
11209 }
11210
4a64f543
MS
11211 /* NOTE drow/2006-04-11: A future improvement to this function would
11212 be to only create the breakpoints once, and actually put them on
11213 the breakpoint chain. That would let us use set_raw_breakpoint.
11214 We could adjust the addresses each time they were needed. Doing
11215 this requires corresponding changes elsewhere where single step
11216 breakpoints are handled, however. So, for now, we use this. */
8181d85f 11217
6c95b8df 11218 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 11219 if (*bpt_p == NULL)
5af949e3
UW
11220 error (_("Could not insert single-step breakpoint at %s"),
11221 paddress (gdbarch, next_pc));
8181d85f
DJ
11222}
11223
f02253f1
HZ
11224/* Check if the breakpoints used for software single stepping
11225 were inserted or not. */
11226
11227int
11228single_step_breakpoints_inserted (void)
11229{
11230 return (single_step_breakpoints[0] != NULL
11231 || single_step_breakpoints[1] != NULL);
11232}
11233
8181d85f
DJ
11234/* Remove and delete any breakpoints used for software single step. */
11235
11236void
11237remove_single_step_breakpoints (void)
11238{
11239 gdb_assert (single_step_breakpoints[0] != NULL);
11240
11241 /* See insert_single_step_breakpoint for more about this deprecated
11242 call. */
a6d9a66e
UW
11243 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11244 single_step_breakpoints[0]);
11245 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11246 single_step_breakpoints[0] = NULL;
11247
11248 if (single_step_breakpoints[1] != NULL)
11249 {
a6d9a66e
UW
11250 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11251 single_step_breakpoints[1]);
11252 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11253 single_step_breakpoints[1] = NULL;
11254 }
11255}
11256
d03285ec
UW
11257/* Delete software single step breakpoints without removing them from
11258 the inferior. This is intended to be used if the inferior's address
11259 space where they were inserted is already gone, e.g. after exit or
11260 exec. */
11261
11262void
11263cancel_single_step_breakpoints (void)
11264{
11265 int i;
11266
11267 for (i = 0; i < 2; i++)
11268 if (single_step_breakpoints[i])
11269 {
11270 xfree (single_step_breakpoints[i]);
11271 single_step_breakpoints[i] = NULL;
11272 single_step_gdbarch[i] = NULL;
11273 }
11274}
11275
11276/* Detach software single-step breakpoints from INFERIOR_PTID without
11277 removing them. */
11278
11279static void
11280detach_single_step_breakpoints (void)
11281{
11282 int i;
11283
11284 for (i = 0; i < 2; i++)
11285 if (single_step_breakpoints[i])
11286 target_remove_breakpoint (single_step_gdbarch[i],
11287 single_step_breakpoints[i]);
11288}
11289
4a64f543
MS
11290/* Check whether a software single-step breakpoint is inserted at
11291 PC. */
1aafd4da
UW
11292
11293static int
cc59ec59
MS
11294single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11295 CORE_ADDR pc)
1aafd4da
UW
11296{
11297 int i;
11298
11299 for (i = 0; i < 2; i++)
11300 {
11301 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11302 if (bp_tgt
11303 && breakpoint_address_match (bp_tgt->placed_address_space,
11304 bp_tgt->placed_address,
11305 aspace, pc))
1aafd4da
UW
11306 return 1;
11307 }
11308
11309 return 0;
11310}
11311
a96d9b2e
SDJ
11312/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11313 non-zero otherwise. */
11314static int
11315is_syscall_catchpoint_enabled (struct breakpoint *bp)
11316{
11317 if (syscall_catchpoint_p (bp)
11318 && bp->enable_state != bp_disabled
11319 && bp->enable_state != bp_call_disabled)
11320 return 1;
11321 else
11322 return 0;
11323}
11324
11325int
11326catch_syscall_enabled (void)
11327{
11328 struct inferior *inf = current_inferior ();
11329
11330 return inf->total_syscalls_count != 0;
11331}
11332
11333int
11334catching_syscall_number (int syscall_number)
11335{
11336 struct breakpoint *bp;
11337
11338 ALL_BREAKPOINTS (bp)
11339 if (is_syscall_catchpoint_enabled (bp))
11340 {
11341 if (bp->syscalls_to_be_caught)
11342 {
11343 int i, iter;
11344 for (i = 0;
11345 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11346 i++)
11347 if (syscall_number == iter)
11348 return 1;
11349 }
11350 else
11351 return 1;
11352 }
11353
11354 return 0;
11355}
11356
11357/* Complete syscall names. Used by "catch syscall". */
11358static char **
11359catch_syscall_completer (struct cmd_list_element *cmd,
11360 char *text, char *word)
11361{
11362 const char **list = get_syscall_names ();
c38eea1a
MS
11363 char **retlist
11364 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 11365
c38eea1a
MS
11366 xfree (list);
11367 return retlist;
a96d9b2e
SDJ
11368}
11369
1042e4c0
SS
11370/* Tracepoint-specific operations. */
11371
11372/* Set tracepoint count to NUM. */
11373static void
11374set_tracepoint_count (int num)
11375{
11376 tracepoint_count = num;
4fa62494 11377 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11378}
11379
11380void
11381trace_command (char *arg, int from_tty)
11382{
8cdf0e15
VP
11383 if (create_breakpoint (get_current_arch (),
11384 arg,
11385 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11386 0 /* tempflag */,
11387 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11388 0 /* Ignore count */,
11389 pending_break_support,
11390 NULL,
11391 from_tty,
84f4c1fe
PM
11392 1 /* enabled */,
11393 0 /* internal */))
fd9b8c24 11394 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11395}
11396
7a697b8d
SS
11397void
11398ftrace_command (char *arg, int from_tty)
11399{
8cdf0e15
VP
11400 if (create_breakpoint (get_current_arch (),
11401 arg,
11402 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11403 0 /* tempflag */,
11404 bp_fast_tracepoint /* type_wanted */,
11405 0 /* Ignore count */,
11406 pending_break_support,
11407 NULL,
11408 from_tty,
84f4c1fe
PM
11409 1 /* enabled */,
11410 0 /* internal */))
0fb4aa4b
PA
11411 set_tracepoint_count (breakpoint_count);
11412}
11413
11414/* strace command implementation. Creates a static tracepoint. */
11415
11416void
11417strace_command (char *arg, int from_tty)
11418{
11419 if (create_breakpoint (get_current_arch (),
11420 arg,
11421 NULL, 0, 1 /* parse arg */,
11422 0 /* tempflag */,
11423 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11424 0 /* Ignore count */,
11425 pending_break_support,
11426 NULL,
11427 from_tty,
84f4c1fe
PM
11428 1 /* enabled */,
11429 0 /* internal */))
fd9b8c24 11430 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11431}
11432
409873ef
SS
11433/* Set up a fake reader function that gets command lines from a linked
11434 list that was acquired during tracepoint uploading. */
11435
11436static struct uploaded_tp *this_utp;
3149d8c1 11437static int next_cmd;
409873ef
SS
11438
11439static char *
11440read_uploaded_action (void)
11441{
11442 char *rslt;
11443
3149d8c1 11444 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11445
3149d8c1 11446 next_cmd++;
409873ef
SS
11447
11448 return rslt;
11449}
11450
00bf0b85
SS
11451/* Given information about a tracepoint as recorded on a target (which
11452 can be either a live system or a trace file), attempt to create an
11453 equivalent GDB tracepoint. This is not a reliable process, since
11454 the target does not necessarily have all the information used when
11455 the tracepoint was originally defined. */
11456
11457struct breakpoint *
11458create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11459{
409873ef 11460 char *addr_str, small_buf[100];
d5551862 11461 struct breakpoint *tp;
fd9b8c24 11462
409873ef
SS
11463 if (utp->at_string)
11464 addr_str = utp->at_string;
11465 else
11466 {
11467 /* In the absence of a source location, fall back to raw
11468 address. Since there is no way to confirm that the address
11469 means the same thing as when the trace was started, warn the
11470 user. */
3e43a32a
MS
11471 warning (_("Uploaded tracepoint %d has no "
11472 "source location, using raw address"),
409873ef
SS
11473 utp->number);
11474 sprintf (small_buf, "*%s", hex_string (utp->addr));
11475 addr_str = small_buf;
11476 }
11477
11478 /* There's not much we can do with a sequence of bytecodes. */
11479 if (utp->cond && !utp->cond_string)
3e43a32a
MS
11480 warning (_("Uploaded tracepoint %d condition "
11481 "has no source form, ignoring it"),
409873ef 11482 utp->number);
d5551862 11483
8cdf0e15 11484 if (!create_breakpoint (get_current_arch (),
409873ef
SS
11485 addr_str,
11486 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 11487 0 /* tempflag */,
0fb4aa4b 11488 utp->type /* type_wanted */,
8cdf0e15
VP
11489 0 /* Ignore count */,
11490 pending_break_support,
11491 NULL,
11492 0 /* from_tty */,
84f4c1fe
PM
11493 utp->enabled /* enabled */,
11494 0 /* internal */))
fd9b8c24
PA
11495 return NULL;
11496
00bf0b85
SS
11497 set_tracepoint_count (breakpoint_count);
11498
409873ef 11499 /* Get the tracepoint we just created. */
fd9b8c24
PA
11500 tp = get_tracepoint (tracepoint_count);
11501 gdb_assert (tp != NULL);
d5551862 11502
00bf0b85
SS
11503 if (utp->pass > 0)
11504 {
409873ef 11505 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 11506
409873ef 11507 trace_pass_command (small_buf, 0);
00bf0b85
SS
11508 }
11509
409873ef
SS
11510 /* If we have uploaded versions of the original commands, set up a
11511 special-purpose "reader" function and call the usual command line
11512 reader, then pass the result to the breakpoint command-setting
11513 function. */
3149d8c1 11514 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 11515 {
409873ef 11516 struct command_line *cmd_list;
00bf0b85 11517
409873ef 11518 this_utp = utp;
3149d8c1 11519 next_cmd = 0;
d5551862 11520
409873ef
SS
11521 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11522
11523 breakpoint_set_commands (tp, cmd_list);
00bf0b85 11524 }
3149d8c1
SS
11525 else if (!VEC_empty (char_ptr, utp->actions)
11526 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
11527 warning (_("Uploaded tracepoint %d actions "
11528 "have no source form, ignoring them"),
409873ef 11529 utp->number);
00bf0b85
SS
11530
11531 return tp;
11532 }
11533
1042e4c0
SS
11534/* Print information on tracepoint number TPNUM_EXP, or all if
11535 omitted. */
11536
11537static void
e5a67952 11538tracepoints_info (char *args, int from_tty)
1042e4c0 11539{
e5a67952 11540 int num_printed;
1042e4c0 11541
e5a67952 11542 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
11543
11544 if (num_printed == 0)
1042e4c0 11545 {
e5a67952 11546 if (args == NULL || *args == '\0')
d77f58be
SS
11547 ui_out_message (uiout, 0, "No tracepoints.\n");
11548 else
e5a67952 11549 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 11550 }
ad443146
SS
11551
11552 default_collect_info ();
1042e4c0
SS
11553}
11554
4a64f543 11555/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
11556 Not supported by all targets. */
11557static void
11558enable_trace_command (char *args, int from_tty)
11559{
11560 enable_command (args, from_tty);
11561}
11562
4a64f543 11563/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
11564 Not supported by all targets. */
11565static void
11566disable_trace_command (char *args, int from_tty)
11567{
11568 disable_command (args, from_tty);
11569}
11570
4a64f543 11571/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
11572static void
11573delete_trace_command (char *arg, int from_tty)
11574{
35df4500 11575 struct breakpoint *b, *b_tmp;
1042e4c0
SS
11576
11577 dont_repeat ();
11578
11579 if (arg == 0)
11580 {
11581 int breaks_to_delete = 0;
11582
11583 /* Delete all breakpoints if no argument.
11584 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
11585 have to be deleted with an explicit breakpoint number
11586 argument. */
1042e4c0
SS
11587 ALL_TRACEPOINTS (b)
11588 {
11589 if (b->number >= 0)
11590 {
11591 breaks_to_delete = 1;
11592 break;
11593 }
11594 }
11595
11596 /* Ask user only if there are some breakpoints to delete. */
11597 if (!from_tty
11598 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11599 {
35df4500 11600 ALL_BREAKPOINTS_SAFE (b, b_tmp)
1042e4c0 11601 {
d77f58be 11602 if (is_tracepoint (b)
059fb39f 11603 && b->number >= 0)
1042e4c0
SS
11604 delete_breakpoint (b);
11605 }
11606 }
11607 }
11608 else
95a42b64 11609 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
11610}
11611
11612/* Set passcount for tracepoint.
11613
11614 First command argument is passcount, second is tracepoint number.
11615 If tracepoint number omitted, apply to most recently defined.
11616 Also accepts special argument "all". */
11617
11618static void
11619trace_pass_command (char *args, int from_tty)
11620{
11621 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
11622 unsigned int count;
11623 int all = 0;
11624
11625 if (args == 0 || *args == 0)
3e43a32a
MS
11626 error (_("passcount command requires an "
11627 "argument (count + optional TP num)"));
1042e4c0 11628
4a64f543 11629 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
11630
11631 while (*args && isspace ((int) *args))
11632 args++;
11633
11634 if (*args && strncasecmp (args, "all", 3) == 0)
11635 {
11636 args += 3; /* Skip special argument "all". */
11637 all = 1;
11638 if (*args)
11639 error (_("Junk at end of arguments."));
11640 }
11641 else
11642 t1 = get_tracepoint_by_number (&args, 1, 1);
11643
11644 do
11645 {
11646 if (t1)
11647 {
11648 ALL_TRACEPOINTS (t2)
11649 if (t1 == (struct breakpoint *) -1 || t1 == t2)
11650 {
11651 t2->pass_count = count;
11652 observer_notify_tracepoint_modified (t2->number);
11653 if (from_tty)
3e43a32a
MS
11654 printf_filtered (_("Setting tracepoint %d's "
11655 "passcount to %d\n"),
1042e4c0
SS
11656 t2->number, count);
11657 }
11658 if (! all && *args)
11659 t1 = get_tracepoint_by_number (&args, 1, 0);
11660 }
11661 }
11662 while (*args);
11663}
11664
11665struct breakpoint *
11666get_tracepoint (int num)
11667{
11668 struct breakpoint *t;
11669
11670 ALL_TRACEPOINTS (t)
11671 if (t->number == num)
11672 return t;
11673
11674 return NULL;
11675}
11676
d5551862
SS
11677/* Find the tracepoint with the given target-side number (which may be
11678 different from the tracepoint number after disconnecting and
11679 reconnecting). */
11680
11681struct breakpoint *
11682get_tracepoint_by_number_on_target (int num)
11683{
11684 struct breakpoint *t;
11685
11686 ALL_TRACEPOINTS (t)
11687 if (t->number_on_target == num)
11688 return t;
11689
11690 return NULL;
11691}
11692
1042e4c0
SS
11693/* Utility: parse a tracepoint number and look it up in the list.
11694 If MULTI_P is true, there might be a range of tracepoints in ARG.
11695 if OPTIONAL_P is true, then if the argument is missing, the most
11696 recent tracepoint (tracepoint_count) is returned. */
11697struct breakpoint *
11698get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
11699{
11700 extern int tracepoint_count;
11701 struct breakpoint *t;
11702 int tpnum;
11703 char *instring = arg == NULL ? NULL : *arg;
11704
11705 if (arg == NULL || *arg == NULL || ! **arg)
11706 {
11707 if (optional_p)
11708 tpnum = tracepoint_count;
11709 else
11710 error_no_arg (_("tracepoint number"));
11711 }
11712 else
11713 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
11714
11715 if (tpnum <= 0)
11716 {
11717 if (instring && *instring)
11718 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11719 instring);
11720 else
3e43a32a
MS
11721 printf_filtered (_("Tracepoint argument missing "
11722 "and no previous tracepoint\n"));
1042e4c0
SS
11723 return NULL;
11724 }
11725
11726 ALL_TRACEPOINTS (t)
11727 if (t->number == tpnum)
11728 {
11729 return t;
11730 }
11731
11732 /* FIXME: if we are in the middle of a range we don't want to give
11733 a message. The current interface to get_number_or_range doesn't
11734 allow us to discover this. */
11735 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11736 return NULL;
11737}
11738
6149aea9
PA
11739/* Save information on user settable breakpoints (watchpoints, etc) to
11740 a new script file named FILENAME. If FILTER is non-NULL, call it
11741 on each breakpoint and only include the ones for which it returns
11742 non-zero. */
11743
1042e4c0 11744static void
6149aea9
PA
11745save_breakpoints (char *filename, int from_tty,
11746 int (*filter) (const struct breakpoint *))
1042e4c0
SS
11747{
11748 struct breakpoint *tp;
6149aea9 11749 int any = 0;
a7bdde9e 11750 char *pathname;
1042e4c0 11751 struct cleanup *cleanup;
a7bdde9e 11752 struct ui_file *fp;
6149aea9 11753 int extra_trace_bits = 0;
1042e4c0 11754
6149aea9
PA
11755 if (filename == 0 || *filename == 0)
11756 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
11757
11758 /* See if we have anything to save. */
6149aea9 11759 ALL_BREAKPOINTS (tp)
1042e4c0 11760 {
6149aea9 11761 /* Skip internal and momentary breakpoints. */
09d682a4 11762 if (!user_breakpoint_p (tp))
6149aea9
PA
11763 continue;
11764
11765 /* If we have a filter, only save the breakpoints it accepts. */
11766 if (filter && !filter (tp))
11767 continue;
11768
11769 any = 1;
11770
11771 if (is_tracepoint (tp))
11772 {
11773 extra_trace_bits = 1;
11774
11775 /* We can stop searching. */
11776 break;
11777 }
1042e4c0 11778 }
6149aea9
PA
11779
11780 if (!any)
1042e4c0 11781 {
6149aea9 11782 warning (_("Nothing to save."));
1042e4c0
SS
11783 return;
11784 }
11785
6149aea9 11786 pathname = tilde_expand (filename);
1042e4c0 11787 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 11788 fp = gdb_fopen (pathname, "w");
059fb39f 11789 if (!fp)
6149aea9
PA
11790 error (_("Unable to open file '%s' for saving (%s)"),
11791 filename, safe_strerror (errno));
a7bdde9e 11792 make_cleanup_ui_file_delete (fp);
8bf6485c 11793
6149aea9
PA
11794 if (extra_trace_bits)
11795 save_trace_state_variables (fp);
8bf6485c 11796
6149aea9 11797 ALL_BREAKPOINTS (tp)
1042e4c0 11798 {
6149aea9 11799 /* Skip internal and momentary breakpoints. */
09d682a4 11800 if (!user_breakpoint_p (tp))
6149aea9 11801 continue;
8bf6485c 11802
6149aea9
PA
11803 /* If we have a filter, only save the breakpoints it accepts. */
11804 if (filter && !filter (tp))
11805 continue;
11806
11807 if (tp->ops != NULL)
11808 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
11809 else
11810 {
6149aea9
PA
11811 if (tp->type == bp_fast_tracepoint)
11812 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
11813 if (tp->type == bp_static_tracepoint)
11814 fprintf_unfiltered (fp, "strace");
6149aea9
PA
11815 else if (tp->type == bp_tracepoint)
11816 fprintf_unfiltered (fp, "trace");
11817 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11818 fprintf_unfiltered (fp, "tbreak");
11819 else if (tp->type == bp_breakpoint)
11820 fprintf_unfiltered (fp, "break");
11821 else if (tp->type == bp_hardware_breakpoint
11822 && tp->disposition == disp_del)
11823 fprintf_unfiltered (fp, "thbreak");
11824 else if (tp->type == bp_hardware_breakpoint)
11825 fprintf_unfiltered (fp, "hbreak");
11826 else if (tp->type == bp_watchpoint)
11827 fprintf_unfiltered (fp, "watch");
11828 else if (tp->type == bp_hardware_watchpoint)
11829 fprintf_unfiltered (fp, "watch");
11830 else if (tp->type == bp_read_watchpoint)
11831 fprintf_unfiltered (fp, "rwatch");
11832 else if (tp->type == bp_access_watchpoint)
11833 fprintf_unfiltered (fp, "awatch");
11834 else
11835 internal_error (__FILE__, __LINE__,
11836 _("unhandled breakpoint type %d"), (int) tp->type);
11837
11838 if (tp->exp_string)
11839 fprintf_unfiltered (fp, " %s", tp->exp_string);
11840 else if (tp->addr_string)
11841 fprintf_unfiltered (fp, " %s", tp->addr_string);
11842 else
11843 {
11844 char tmp[40];
11845
11846 sprintf_vma (tmp, tp->loc->address);
11847 fprintf_unfiltered (fp, " *0x%s", tmp);
11848 }
1042e4c0
SS
11849 }
11850
6149aea9
PA
11851 if (tp->thread != -1)
11852 fprintf_unfiltered (fp, " thread %d", tp->thread);
11853
11854 if (tp->task != 0)
11855 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
11856
11857 fprintf_unfiltered (fp, "\n");
11858
6149aea9
PA
11859 /* Note, we can't rely on tp->number for anything, as we can't
11860 assume the recreated breakpoint numbers will match. Use $bpnum
11861 instead. */
11862
11863 if (tp->cond_string)
11864 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
11865
11866 if (tp->ignore_count)
11867 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
11868
1042e4c0 11869 if (tp->pass_count)
a7bdde9e 11870 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 11871
a7bdde9e 11872 if (tp->commands)
1042e4c0 11873 {
a7bdde9e
VP
11874 volatile struct gdb_exception ex;
11875
6149aea9 11876 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
11877
11878 ui_out_redirect (uiout, fp);
14dba4b4 11879 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 11880 {
9add0f1b 11881 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
11882 }
11883 ui_out_redirect (uiout, NULL);
1042e4c0 11884
a7bdde9e
VP
11885 if (ex.reason < 0)
11886 throw_exception (ex);
1042e4c0 11887
a7bdde9e 11888 fprintf_unfiltered (fp, " end\n");
1042e4c0 11889 }
6149aea9
PA
11890
11891 if (tp->enable_state == bp_disabled)
11892 fprintf_unfiltered (fp, "disable\n");
11893
11894 /* If this is a multi-location breakpoint, check if the locations
11895 should be individually disabled. Watchpoint locations are
11896 special, and not user visible. */
11897 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
11898 {
11899 struct bp_location *loc;
11900 int n = 1;
11901
11902 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
11903 if (!loc->enabled)
11904 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
11905 }
1042e4c0 11906 }
8bf6485c 11907
6149aea9 11908 if (extra_trace_bits && *default_collect)
8bf6485c
SS
11909 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
11910
1042e4c0
SS
11911 do_cleanups (cleanup);
11912 if (from_tty)
6149aea9
PA
11913 printf_filtered (_("Saved to file '%s'.\n"), filename);
11914}
11915
11916/* The `save breakpoints' command. */
11917
11918static void
11919save_breakpoints_command (char *args, int from_tty)
11920{
11921 save_breakpoints (args, from_tty, NULL);
11922}
11923
11924/* The `save tracepoints' command. */
11925
11926static void
11927save_tracepoints_command (char *args, int from_tty)
11928{
11929 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
11930}
11931
11932/* Create a vector of all tracepoints. */
11933
11934VEC(breakpoint_p) *
11935all_tracepoints ()
11936{
11937 VEC(breakpoint_p) *tp_vec = 0;
11938 struct breakpoint *tp;
11939
11940 ALL_TRACEPOINTS (tp)
11941 {
11942 VEC_safe_push (breakpoint_p, tp_vec, tp);
11943 }
11944
11945 return tp_vec;
11946}
11947
c906108c 11948\f
4a64f543
MS
11949/* This help string is used for the break, hbreak, tbreak and thbreak
11950 commands. It is defined as a macro to prevent duplication.
11951 COMMAND should be a string constant containing the name of the
11952 command. */
31e2b00f
AS
11953#define BREAK_ARGS_HELP(command) \
11954command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
11955LOCATION may be a line number, function name, or \"*\" and an address.\n\
11956If a line number is specified, break at start of code for that line.\n\
11957If a function is specified, break at start of code for that function.\n\
11958If an address is specified, break at that exact address.\n\
dc10affe
PA
11959With no LOCATION, uses current execution address of the selected\n\
11960stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
11961\n\
11962THREADNUM is the number from \"info threads\".\n\
11963CONDITION is a boolean expression.\n\
11964\n\
d41c0fc8
PA
11965Multiple breakpoints at one place are permitted, and useful if their\n\
11966conditions are different.\n\
31e2b00f
AS
11967\n\
11968Do \"help breakpoints\" for info on other commands dealing with breakpoints."
11969
44feb3ce
TT
11970/* List of subcommands for "catch". */
11971static struct cmd_list_element *catch_cmdlist;
11972
11973/* List of subcommands for "tcatch". */
11974static struct cmd_list_element *tcatch_cmdlist;
11975
11976/* Like add_cmd, but add the command to both the "catch" and "tcatch"
11977 lists, and pass some additional user data to the command function. */
11978static void
11979add_catch_command (char *name, char *docstring,
11980 void (*sfunc) (char *args, int from_tty,
11981 struct cmd_list_element *command),
a96d9b2e
SDJ
11982 char **(*completer) (struct cmd_list_element *cmd,
11983 char *text, char *word),
44feb3ce
TT
11984 void *user_data_catch,
11985 void *user_data_tcatch)
11986{
11987 struct cmd_list_element *command;
11988
11989 command = add_cmd (name, class_breakpoint, NULL, docstring,
11990 &catch_cmdlist);
11991 set_cmd_sfunc (command, sfunc);
11992 set_cmd_context (command, user_data_catch);
a96d9b2e 11993 set_cmd_completer (command, completer);
44feb3ce
TT
11994
11995 command = add_cmd (name, class_breakpoint, NULL, docstring,
11996 &tcatch_cmdlist);
11997 set_cmd_sfunc (command, sfunc);
11998 set_cmd_context (command, user_data_tcatch);
a96d9b2e 11999 set_cmd_completer (command, completer);
44feb3ce
TT
12000}
12001
6c95b8df 12002static void
a79b8f6e 12003clear_syscall_counts (struct inferior *inf)
6c95b8df 12004{
6c95b8df
PA
12005 inf->total_syscalls_count = 0;
12006 inf->any_syscall_count = 0;
12007 VEC_free (int, inf->syscalls_counts);
12008}
12009
6149aea9
PA
12010static void
12011save_command (char *arg, int from_tty)
12012{
3e43a32a
MS
12013 printf_unfiltered (_("\"save\" must be followed by "
12014 "the name of a save subcommand.\n"));
6149aea9
PA
12015 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12016}
12017
84f4c1fe
PM
12018struct breakpoint *
12019iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12020 void *data)
12021{
35df4500 12022 struct breakpoint *b, *b_tmp;
84f4c1fe 12023
35df4500 12024 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12025 {
12026 if ((*callback) (b, data))
12027 return b;
12028 }
12029
12030 return NULL;
12031}
12032
c906108c 12033void
fba45db2 12034_initialize_breakpoint (void)
c906108c
SS
12035{
12036 struct cmd_list_element *c;
12037
84acb35a 12038 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 12039 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 12040 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 12041
17450429
PP
12042 breakpoint_objfile_key = register_objfile_data ();
12043
c906108c
SS
12044 breakpoint_chain = 0;
12045 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12046 before a breakpoint is set. */
12047 breakpoint_count = 0;
12048
1042e4c0
SS
12049 tracepoint_count = 0;
12050
1bedd215
AC
12051 add_com ("ignore", class_breakpoint, ignore_command, _("\
12052Set ignore-count of breakpoint number N to COUNT.\n\
12053Usage is `ignore N COUNT'."));
c906108c 12054 if (xdb_commands)
c5aa993b 12055 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 12056
1bedd215
AC
12057 add_com ("commands", class_breakpoint, commands_command, _("\
12058Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
12059Give breakpoint number as argument after \"commands\".\n\
12060With no argument, the targeted breakpoint is the last one set.\n\
12061The commands themselves follow starting on the next line.\n\
12062Type a line containing \"end\" to indicate the end of them.\n\
12063Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 12064then no output is printed when it is hit, except what the commands print."));
c906108c 12065
1bedd215
AC
12066 add_com ("condition", class_breakpoint, condition_command, _("\
12067Specify breakpoint number N to break only if COND is true.\n\
c906108c 12068Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 12069expression to be evaluated whenever breakpoint N is reached."));
c906108c 12070
1bedd215 12071 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 12072Set a temporary breakpoint.\n\
c906108c
SS
12073Like \"break\" except the breakpoint is only temporary,\n\
12074so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
12075by using \"enable delete\" on the breakpoint number.\n\
12076\n"
12077BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 12078 set_cmd_completer (c, location_completer);
c94fdfd0 12079
1bedd215 12080 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 12081Set a hardware assisted breakpoint.\n\
c906108c 12082Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
12083some target hardware may not have this support.\n\
12084\n"
12085BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 12086 set_cmd_completer (c, location_completer);
c906108c 12087
1bedd215 12088 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 12089Set a temporary hardware assisted breakpoint.\n\
c906108c 12090Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
12091so it will be deleted when hit.\n\
12092\n"
12093BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 12094 set_cmd_completer (c, location_completer);
c906108c 12095
1bedd215
AC
12096 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12097Enable some breakpoints.\n\
c906108c
SS
12098Give breakpoint numbers (separated by spaces) as arguments.\n\
12099With no subcommand, breakpoints are enabled until you command otherwise.\n\
12100This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12101With a subcommand you can enable temporarily."),
c906108c
SS
12102 &enablelist, "enable ", 1, &cmdlist);
12103 if (xdb_commands)
1bedd215
AC
12104 add_com ("ab", class_breakpoint, enable_command, _("\
12105Enable some breakpoints.\n\
c906108c
SS
12106Give breakpoint numbers (separated by spaces) as arguments.\n\
12107With no subcommand, breakpoints are enabled until you command otherwise.\n\
12108This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12109With a subcommand you can enable temporarily."));
c906108c
SS
12110
12111 add_com_alias ("en", "enable", class_breakpoint, 1);
12112
84951ab5 12113 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 12114Enable some breakpoints.\n\
c906108c
SS
12115Give breakpoint numbers (separated by spaces) as arguments.\n\
12116This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12117May be abbreviated to simply \"enable\".\n"),
c5aa993b 12118 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 12119
1a966eab
AC
12120 add_cmd ("once", no_class, enable_once_command, _("\
12121Enable breakpoints for one hit. Give breakpoint numbers.\n\
12122If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12123 &enablebreaklist);
12124
1a966eab
AC
12125 add_cmd ("delete", no_class, enable_delete_command, _("\
12126Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12127If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12128 &enablebreaklist);
12129
1a966eab
AC
12130 add_cmd ("delete", no_class, enable_delete_command, _("\
12131Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12132If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12133 &enablelist);
12134
1a966eab
AC
12135 add_cmd ("once", no_class, enable_once_command, _("\
12136Enable breakpoints for one hit. Give breakpoint numbers.\n\
12137If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12138 &enablelist);
12139
1bedd215
AC
12140 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12141Disable some breakpoints.\n\
c906108c
SS
12142Arguments are breakpoint numbers with spaces in between.\n\
12143To disable all breakpoints, give no argument.\n\
1bedd215 12144A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
12145 &disablelist, "disable ", 1, &cmdlist);
12146 add_com_alias ("dis", "disable", class_breakpoint, 1);
12147 add_com_alias ("disa", "disable", class_breakpoint, 1);
12148 if (xdb_commands)
1bedd215
AC
12149 add_com ("sb", class_breakpoint, disable_command, _("\
12150Disable some breakpoints.\n\
c906108c
SS
12151Arguments are breakpoint numbers with spaces in between.\n\
12152To disable all breakpoints, give no argument.\n\
1bedd215 12153A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 12154
1a966eab
AC
12155 add_cmd ("breakpoints", class_alias, disable_command, _("\
12156Disable some breakpoints.\n\
c906108c
SS
12157Arguments are breakpoint numbers with spaces in between.\n\
12158To disable all breakpoints, give no argument.\n\
12159A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 12160This command may be abbreviated \"disable\"."),
c906108c
SS
12161 &disablelist);
12162
1bedd215
AC
12163 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12164Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12165Arguments are breakpoint numbers with spaces in between.\n\
12166To delete all breakpoints, give no argument.\n\
12167\n\
12168Also a prefix command for deletion of other GDB objects.\n\
1bedd215 12169The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
12170 &deletelist, "delete ", 1, &cmdlist);
12171 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 12172 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 12173 if (xdb_commands)
1bedd215
AC
12174 add_com ("db", class_breakpoint, delete_command, _("\
12175Delete some breakpoints.\n\
c906108c 12176Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 12177To delete all breakpoints, give no argument.\n"));
c906108c 12178
1a966eab
AC
12179 add_cmd ("breakpoints", class_alias, delete_command, _("\
12180Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12181Arguments are breakpoint numbers with spaces in between.\n\
12182To delete all breakpoints, give no argument.\n\
1a966eab 12183This command may be abbreviated \"delete\"."),
c906108c
SS
12184 &deletelist);
12185
1bedd215
AC
12186 add_com ("clear", class_breakpoint, clear_command, _("\
12187Clear breakpoint at specified line or function.\n\
c906108c
SS
12188Argument may be line number, function name, or \"*\" and an address.\n\
12189If line number is specified, all breakpoints in that line are cleared.\n\
12190If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
12191If an address is specified, breakpoints at that address are cleared.\n\
12192\n\
12193With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
12194is executing in.\n\
12195\n\
1bedd215 12196See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 12197 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 12198
1bedd215 12199 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
12200Set breakpoint at specified line or function.\n"
12201BREAK_ARGS_HELP ("break")));
5ba2abeb 12202 set_cmd_completer (c, location_completer);
c94fdfd0 12203
c906108c
SS
12204 add_com_alias ("b", "break", class_run, 1);
12205 add_com_alias ("br", "break", class_run, 1);
12206 add_com_alias ("bre", "break", class_run, 1);
12207 add_com_alias ("brea", "break", class_run, 1);
12208
7681d515
PM
12209 if (xdb_commands)
12210 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
12211
12212 if (dbx_commands)
12213 {
1bedd215
AC
12214 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12215Break in function/address or break at a line in the current file."),
c5aa993b
JM
12216 &stoplist, "stop ", 1, &cmdlist);
12217 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 12218 _("Break in function or address."), &stoplist);
c5aa993b 12219 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 12220 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
12221 add_com ("status", class_info, breakpoints_info, _("\
12222Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12223The \"Type\" column indicates one of:\n\
12224\tbreakpoint - normal breakpoint\n\
12225\twatchpoint - watchpoint\n\
12226The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12227the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12228breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12229address and file/line number respectively.\n\
12230\n\
12231Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12232are set to the address of the last breakpoint listed unless the command\n\
12233is prefixed with \"server \".\n\n\
c906108c 12234Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12235breakpoint set."));
c906108c
SS
12236 }
12237
1bedd215 12238 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 12239Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
12240The \"Type\" column indicates one of:\n\
12241\tbreakpoint - normal breakpoint\n\
12242\twatchpoint - watchpoint\n\
12243The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12244the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12245breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12246address and file/line number respectively.\n\
12247\n\
12248Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12249are set to the address of the last breakpoint listed unless the command\n\
12250is prefixed with \"server \".\n\n\
c906108c 12251Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12252breakpoint set."));
c906108c 12253
6b04bdb7
MS
12254 add_info_alias ("b", "breakpoints", 1);
12255
c906108c 12256 if (xdb_commands)
1bedd215
AC
12257 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12258Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12259The \"Type\" column indicates one of:\n\
12260\tbreakpoint - normal breakpoint\n\
12261\twatchpoint - watchpoint\n\
12262The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12263the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12264breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12265address and file/line number respectively.\n\
12266\n\
12267Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12268are set to the address of the last breakpoint listed unless the command\n\
12269is prefixed with \"server \".\n\n\
c906108c 12270Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12271breakpoint set."));
c906108c 12272
1a966eab
AC
12273 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12274Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12275The \"Type\" column indicates one of:\n\
12276\tbreakpoint - normal breakpoint\n\
12277\twatchpoint - watchpoint\n\
12278\tlongjmp - internal breakpoint used to step through longjmp()\n\
12279\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12280\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12281\tfinish - internal breakpoint used by the \"finish\" command\n\
12282The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12283the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12284breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12285address and file/line number respectively.\n\
12286\n\
12287Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12288are set to the address of the last breakpoint listed unless the command\n\
12289is prefixed with \"server \".\n\n\
c906108c 12290Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12291breakpoint set."),
c906108c
SS
12292 &maintenanceinfolist);
12293
44feb3ce
TT
12294 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12295Set catchpoints to catch events."),
12296 &catch_cmdlist, "catch ",
12297 0/*allow-unknown*/, &cmdlist);
12298
12299 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12300Set temporary catchpoints to catch events."),
12301 &tcatch_cmdlist, "tcatch ",
12302 0/*allow-unknown*/, &cmdlist);
12303
12304 /* Add catch and tcatch sub-commands. */
12305 add_catch_command ("catch", _("\
12306Catch an exception, when caught.\n\
12307With an argument, catch only exceptions with the given name."),
12308 catch_catch_command,
a96d9b2e 12309 NULL,
44feb3ce
TT
12310 CATCH_PERMANENT,
12311 CATCH_TEMPORARY);
12312 add_catch_command ("throw", _("\
12313Catch an exception, when thrown.\n\
12314With an argument, catch only exceptions with the given name."),
12315 catch_throw_command,
a96d9b2e 12316 NULL,
44feb3ce
TT
12317 CATCH_PERMANENT,
12318 CATCH_TEMPORARY);
12319 add_catch_command ("fork", _("Catch calls to fork."),
12320 catch_fork_command_1,
a96d9b2e 12321 NULL,
44feb3ce
TT
12322 (void *) (uintptr_t) catch_fork_permanent,
12323 (void *) (uintptr_t) catch_fork_temporary);
12324 add_catch_command ("vfork", _("Catch calls to vfork."),
12325 catch_fork_command_1,
a96d9b2e 12326 NULL,
44feb3ce
TT
12327 (void *) (uintptr_t) catch_vfork_permanent,
12328 (void *) (uintptr_t) catch_vfork_temporary);
12329 add_catch_command ("exec", _("Catch calls to exec."),
12330 catch_exec_command_1,
a96d9b2e
SDJ
12331 NULL,
12332 CATCH_PERMANENT,
12333 CATCH_TEMPORARY);
12334 add_catch_command ("syscall", _("\
12335Catch system calls by their names and/or numbers.\n\
12336Arguments say which system calls to catch. If no arguments\n\
12337are given, every system call will be caught.\n\
12338Arguments, if given, should be one or more system call names\n\
12339(if your system supports that), or system call numbers."),
12340 catch_syscall_command_1,
12341 catch_syscall_completer,
44feb3ce
TT
12342 CATCH_PERMANENT,
12343 CATCH_TEMPORARY);
44feb3ce
TT
12344 add_catch_command ("exception", _("\
12345Catch Ada exceptions, when raised.\n\
12346With an argument, catch only exceptions with the given name."),
12347 catch_ada_exception_command,
a96d9b2e 12348 NULL,
44feb3ce
TT
12349 CATCH_PERMANENT,
12350 CATCH_TEMPORARY);
12351 add_catch_command ("assert", _("\
12352Catch failed Ada assertions, when raised.\n\
12353With an argument, catch only exceptions with the given name."),
12354 catch_assert_command,
a96d9b2e 12355 NULL,
44feb3ce
TT
12356 CATCH_PERMANENT,
12357 CATCH_TEMPORARY);
c5aa993b 12358
1bedd215
AC
12359 c = add_com ("watch", class_breakpoint, watch_command, _("\
12360Set a watchpoint for an expression.\n\
06a64a0b 12361Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12362A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12363an expression changes.\n\
12364If -l or -location is given, this evaluates EXPRESSION and watches\n\
12365the memory to which it refers."));
65d12d83 12366 set_cmd_completer (c, expression_completer);
c906108c 12367
1bedd215
AC
12368 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12369Set a read watchpoint for an expression.\n\
06a64a0b 12370Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12371A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12372an expression is read.\n\
12373If -l or -location is given, this evaluates EXPRESSION and watches\n\
12374the memory to which it refers."));
65d12d83 12375 set_cmd_completer (c, expression_completer);
c906108c 12376
1bedd215
AC
12377 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12378Set a watchpoint for an expression.\n\
06a64a0b 12379Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12380A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12381an expression is either read or written.\n\
12382If -l or -location is given, this evaluates EXPRESSION and watches\n\
12383the memory to which it refers."));
65d12d83 12384 set_cmd_completer (c, expression_completer);
c906108c 12385
d77f58be 12386 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 12387Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 12388
920d2a44
AC
12389 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12390 respond to changes - contrary to the description. */
85c07804
AC
12391 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12392 &can_use_hw_watchpoints, _("\
12393Set debugger's willingness to use watchpoint hardware."), _("\
12394Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12395If zero, gdb will not use hardware for new watchpoints, even if\n\
12396such is available. (However, any hardware watchpoints that were\n\
12397created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12398hardware.)"),
12399 NULL,
920d2a44 12400 show_can_use_hw_watchpoints,
85c07804 12401 &setlist, &showlist);
c906108c
SS
12402
12403 can_use_hw_watchpoints = 1;
fa8d40ab 12404
1042e4c0
SS
12405 /* Tracepoint manipulation commands. */
12406
12407 c = add_com ("trace", class_breakpoint, trace_command, _("\
12408Set a tracepoint at specified line or function.\n\
12409\n"
12410BREAK_ARGS_HELP ("trace") "\n\
12411Do \"help tracepoints\" for info on other tracepoint commands."));
12412 set_cmd_completer (c, location_completer);
12413
12414 add_com_alias ("tp", "trace", class_alias, 0);
12415 add_com_alias ("tr", "trace", class_alias, 1);
12416 add_com_alias ("tra", "trace", class_alias, 1);
12417 add_com_alias ("trac", "trace", class_alias, 1);
12418
7a697b8d
SS
12419 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12420Set a fast tracepoint at specified line or function.\n\
12421\n"
12422BREAK_ARGS_HELP ("ftrace") "\n\
12423Do \"help tracepoints\" for info on other tracepoint commands."));
12424 set_cmd_completer (c, location_completer);
12425
0fb4aa4b
PA
12426 c = add_com ("strace", class_breakpoint, strace_command, _("\
12427Set a static tracepoint at specified line, function or marker.\n\
12428\n\
12429strace [LOCATION] [if CONDITION]\n\
12430LOCATION may be a line number, function name, \"*\" and an address,\n\
12431or -m MARKER_ID.\n\
12432If a line number is specified, probe the marker at start of code\n\
12433for that line. If a function is specified, probe the marker at start\n\
12434of code for that function. If an address is specified, probe the marker\n\
12435at that exact address. If a marker id is specified, probe the marker\n\
12436with that name. With no LOCATION, uses current execution address of\n\
12437the selected stack frame.\n\
12438Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12439This collects arbitrary user data passed in the probe point call to the\n\
12440tracing library. You can inspect it when analyzing the trace buffer,\n\
12441by printing the $_sdata variable like any other convenience variable.\n\
12442\n\
12443CONDITION is a boolean expression.\n\
12444\n\
d41c0fc8
PA
12445Multiple tracepoints at one place are permitted, and useful if their\n\
12446conditions are different.\n\
0fb4aa4b
PA
12447\n\
12448Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12449Do \"help tracepoints\" for info on other tracepoint commands."));
12450 set_cmd_completer (c, location_completer);
12451
1042e4c0 12452 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 12453Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
12454Convenience variable \"$tpnum\" contains the number of the\n\
12455last tracepoint set."));
12456
12457 add_info_alias ("tp", "tracepoints", 1);
12458
12459 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12460Delete specified tracepoints.\n\
12461Arguments are tracepoint numbers, separated by spaces.\n\
12462No argument means delete all tracepoints."),
12463 &deletelist);
12464
12465 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12466Disable specified tracepoints.\n\
12467Arguments are tracepoint numbers, separated by spaces.\n\
12468No argument means disable all tracepoints."),
12469 &disablelist);
12470 deprecate_cmd (c, "disable");
12471
12472 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12473Enable specified tracepoints.\n\
12474Arguments are tracepoint numbers, separated by spaces.\n\
12475No argument means enable all tracepoints."),
12476 &enablelist);
12477 deprecate_cmd (c, "enable");
12478
12479 add_com ("passcount", class_trace, trace_pass_command, _("\
12480Set the passcount for a tracepoint.\n\
12481The trace will end when the tracepoint has been passed 'count' times.\n\
12482Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12483if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12484
6149aea9
PA
12485 add_prefix_cmd ("save", class_breakpoint, save_command,
12486 _("Save breakpoint definitions as a script."),
12487 &save_cmdlist, "save ",
12488 0/*allow-unknown*/, &cmdlist);
12489
12490 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12491Save current breakpoint definitions as a script.\n\
cce7e648 12492This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
12493catchpoints, tracepoints). Use the 'source' command in another debug\n\
12494session to restore them."),
12495 &save_cmdlist);
12496 set_cmd_completer (c, filename_completer);
12497
12498 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 12499Save current tracepoint definitions as a script.\n\
6149aea9
PA
12500Use the 'source' command in another debug session to restore them."),
12501 &save_cmdlist);
1042e4c0
SS
12502 set_cmd_completer (c, filename_completer);
12503
6149aea9
PA
12504 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12505 deprecate_cmd (c, "save tracepoints");
12506
1bedd215 12507 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
12508Breakpoint specific settings\n\
12509Configure various breakpoint-specific variables such as\n\
1bedd215 12510pending breakpoint behavior"),
fa8d40ab
JJ
12511 &breakpoint_set_cmdlist, "set breakpoint ",
12512 0/*allow-unknown*/, &setlist);
1bedd215 12513 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
12514Breakpoint specific settings\n\
12515Configure various breakpoint-specific variables such as\n\
1bedd215 12516pending breakpoint behavior"),
fa8d40ab
JJ
12517 &breakpoint_show_cmdlist, "show breakpoint ",
12518 0/*allow-unknown*/, &showlist);
12519
7915a72c
AC
12520 add_setshow_auto_boolean_cmd ("pending", no_class,
12521 &pending_break_support, _("\
12522Set debugger's behavior regarding pending breakpoints."), _("\
12523Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
12524If on, an unrecognized breakpoint location will cause gdb to create a\n\
12525pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12526an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 12527user-query to see if a pending breakpoint should be created."),
2c5b56ce 12528 NULL,
920d2a44 12529 show_pending_break_support,
6e1d7d6c
AC
12530 &breakpoint_set_cmdlist,
12531 &breakpoint_show_cmdlist);
fa8d40ab
JJ
12532
12533 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
12534
12535 add_setshow_boolean_cmd ("auto-hw", no_class,
12536 &automatic_hardware_breakpoints, _("\
12537Set automatic usage of hardware breakpoints."), _("\
12538Show automatic usage of hardware breakpoints."), _("\
12539If set, the debugger will automatically use hardware breakpoints for\n\
12540breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12541a warning will be emitted for such breakpoints."),
12542 NULL,
12543 show_automatic_hardware_breakpoints,
12544 &breakpoint_set_cmdlist,
12545 &breakpoint_show_cmdlist);
74960c60 12546
33e5cbd6
PA
12547 add_setshow_enum_cmd ("always-inserted", class_support,
12548 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
12549Set mode for inserting breakpoints."), _("\
12550Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
12551When this mode is off, breakpoints are inserted in inferior when it is\n\
12552resumed, and removed when execution stops. When this mode is on,\n\
12553breakpoints are inserted immediately and removed only when the user\n\
12554deletes the breakpoint. When this mode is auto (which is the default),\n\
12555the behaviour depends on the non-stop setting (see help set non-stop).\n\
12556In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12557behaves as if always-inserted mode is on; if gdb is controlling the\n\
12558inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
12559 NULL,
12560 &show_always_inserted_mode,
12561 &breakpoint_set_cmdlist,
12562 &breakpoint_show_cmdlist);
765dc015
VP
12563
12564 automatic_hardware_breakpoints = 1;
f3b1572e
PA
12565
12566 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 12567}