2011-03-31 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
c906108c 67
1042e4c0
SS
68/* readline include files */
69#include "readline/readline.h"
70#include "readline/history.h"
71
72/* readline defines this. */
73#undef savestring
74
034dad6f 75#include "mi/mi-common.h"
7371cf6d 76#include "python/python.h"
104c1213 77
44feb3ce
TT
78/* Arguments to pass as context to some catch command handlers. */
79#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
80#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
95a42b64
TT
92static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
c906108c 95
a14ed312 96static void ignore_command (char *, int);
c906108c 97
4efb68b1 98static int breakpoint_re_set_one (void *);
c906108c 99
a14ed312 100static void clear_command (char *, int);
c906108c 101
a14ed312 102static void catch_command (char *, int);
c906108c 103
e09342b5 104static int can_use_hardware_watchpoint (struct value *, int);
c906108c 105
98deb0da 106static void break_command_1 (char *, int, int);
c906108c 107
a14ed312 108static void mention (struct breakpoint *);
c906108c 109
4a64f543
MS
110/* This function is used in gdbtk sources and thus can not be made
111 static. */
63c252f8 112struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
113 struct symtab_and_line,
114 enum bptype);
c906108c 115
76897487
KB
116static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
117
a6d9a66e
UW
118static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
119 CORE_ADDR bpaddr,
88f7da05 120 enum bptype bptype);
76897487 121
6c95b8df
PA
122static void describe_other_breakpoints (struct gdbarch *,
123 struct program_space *, CORE_ADDR,
5af949e3 124 struct obj_section *, int);
c906108c 125
6c95b8df
PA
126static int breakpoint_address_match (struct address_space *aspace1,
127 CORE_ADDR addr1,
128 struct address_space *aspace2,
129 CORE_ADDR addr2);
130
85d721b8
PA
131static int watchpoint_locations_match (struct bp_location *loc1,
132 struct bp_location *loc2);
133
a14ed312 134static void breakpoints_info (char *, int);
c906108c 135
d77f58be
SS
136static void watchpoints_info (char *, int);
137
e5a67952
MS
138static int breakpoint_1 (char *, int,
139 int (*) (const struct breakpoint *));
c906108c 140
4efb68b1 141static int breakpoint_cond_eval (void *);
c906108c 142
4efb68b1 143static void cleanup_executing_breakpoints (void *);
c906108c 144
a14ed312 145static void commands_command (char *, int);
c906108c 146
a14ed312 147static void condition_command (char *, int);
c906108c 148
c5aa993b
JM
149typedef enum
150 {
151 mark_inserted,
152 mark_uninserted
153 }
154insertion_state_t;
c906108c 155
0bde7532 156static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 157static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 158
a14ed312 159static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
160
161static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 162
4efb68b1 163static int watchpoint_check (void *);
c906108c 164
a14ed312 165static void maintenance_info_breakpoints (char *, int);
c906108c 166
a14ed312 167static int hw_breakpoint_used_count (void);
c906108c 168
a14ed312 169static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 170
a14ed312 171static void hbreak_command (char *, int);
c906108c 172
a14ed312 173static void thbreak_command (char *, int);
c906108c 174
a14ed312 175static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 176
a14ed312 177static void stop_command (char *arg, int from_tty);
7a292a7a 178
a14ed312 179static void stopin_command (char *arg, int from_tty);
7a292a7a 180
a14ed312 181static void stopat_command (char *arg, int from_tty);
7a292a7a 182
a14ed312 183static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 184
d85310f7
MS
185static void catch_exception_command_1 (enum exception_event_kind ex_event,
186 char *arg, int tempflag, int from_tty);
7a292a7a 187
a14ed312 188static void tcatch_command (char *arg, int from_tty);
7a292a7a 189
d03285ec
UW
190static void detach_single_step_breakpoints (void);
191
6c95b8df
PA
192static int single_step_breakpoint_inserted_here_p (struct address_space *,
193 CORE_ADDR pc);
1aafd4da 194
fe3f5fa8 195static void free_bp_location (struct bp_location *loc);
f431efe5
PA
196static void incref_bp_location (struct bp_location *loc);
197static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 198
39d61571 199static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 200
b60e7edf 201static void update_global_location_list (int);
a5606eee 202
b60e7edf 203static void update_global_location_list_nothrow (int);
74960c60 204
d77f58be 205static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 206
d77f58be 207static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 208
74960c60 209static void insert_breakpoint_locations (void);
a5606eee 210
a96d9b2e
SDJ
211static int syscall_catchpoint_p (struct breakpoint *b);
212
1042e4c0
SS
213static void tracepoints_info (char *, int);
214
215static void delete_trace_command (char *, int);
216
217static void enable_trace_command (char *, int);
218
219static void disable_trace_command (char *, int);
220
221static void trace_pass_command (char *, int);
222
0fb4aa4b
PA
223/* Assuming we're creating a static tracepoint, does S look like a
224 static tracepoint marker spec ("-m MARKER_ID")? */
225#define is_marker_spec(s) \
f5a8e22b 226 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 227
5cea2a26
PA
228/* A reference-counted struct command_line. This lets multiple
229 breakpoints share a single command list. */
230struct counted_command_line
231{
232 /* The reference count. */
233 int refc;
234
235 /* The command list. */
236 struct command_line *commands;
237};
238
239struct command_line *
240breakpoint_commands (struct breakpoint *b)
241{
242 return b->commands ? b->commands->commands : NULL;
243}
3daf8fe5 244
f3b1572e
PA
245/* Flag indicating that a command has proceeded the inferior past the
246 current breakpoint. */
247
248static int breakpoint_proceeded;
249
2cec12e5
AR
250static const char *
251bpdisp_text (enum bpdisp disp)
252{
4a64f543
MS
253 /* NOTE: the following values are a part of MI protocol and
254 represent values of 'disp' field returned when inferior stops at
255 a breakpoint. */
bc043ef3 256 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 257
2cec12e5
AR
258 return bpdisps[(int) disp];
259}
c906108c 260
4a64f543 261/* Prototypes for exported functions. */
c906108c 262/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 263 if such is available. */
c906108c
SS
264static int can_use_hw_watchpoints;
265
920d2a44
AC
266static void
267show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
268 struct cmd_list_element *c,
269 const char *value)
270{
3e43a32a
MS
271 fprintf_filtered (file,
272 _("Debugger's willingness to use "
273 "watchpoint hardware is %s.\n"),
920d2a44
AC
274 value);
275}
276
fa8d40ab
JJ
277/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
278 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 279 for unrecognized breakpoint locations.
fa8d40ab
JJ
280 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
281static enum auto_boolean pending_break_support;
920d2a44
AC
282static void
283show_pending_break_support (struct ui_file *file, int from_tty,
284 struct cmd_list_element *c,
285 const char *value)
286{
3e43a32a
MS
287 fprintf_filtered (file,
288 _("Debugger's behavior regarding "
289 "pending breakpoints is %s.\n"),
920d2a44
AC
290 value);
291}
fa8d40ab 292
765dc015 293/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 294 set with "break" but falling in read-only memory.
765dc015
VP
295 If 0, gdb will warn about such breakpoints, but won't automatically
296 use hardware breakpoints. */
297static int automatic_hardware_breakpoints;
298static void
299show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
300 struct cmd_list_element *c,
301 const char *value)
302{
3e43a32a
MS
303 fprintf_filtered (file,
304 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
305 value);
306}
307
33e5cbd6
PA
308/* If on, gdb will keep breakpoints inserted even as inferior is
309 stopped, and immediately insert any new breakpoints. If off, gdb
310 will insert breakpoints into inferior only when resuming it, and
311 will remove breakpoints upon stop. If auto, GDB will behave as ON
312 if in non-stop mode, and as OFF if all-stop mode.*/
313
314static const char always_inserted_auto[] = "auto";
315static const char always_inserted_on[] = "on";
316static const char always_inserted_off[] = "off";
317static const char *always_inserted_enums[] = {
318 always_inserted_auto,
319 always_inserted_off,
320 always_inserted_on,
321 NULL
322};
323static const char *always_inserted_mode = always_inserted_auto;
324static void
74960c60 325show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 326 struct cmd_list_element *c, const char *value)
74960c60 327{
33e5cbd6 328 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
329 fprintf_filtered (file,
330 _("Always inserted breakpoint "
331 "mode is %s (currently %s).\n"),
33e5cbd6
PA
332 value,
333 breakpoints_always_inserted_mode () ? "on" : "off");
334 else
3e43a32a
MS
335 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
336 value);
74960c60
VP
337}
338
33e5cbd6
PA
339int
340breakpoints_always_inserted_mode (void)
341{
342 return (always_inserted_mode == always_inserted_on
343 || (always_inserted_mode == always_inserted_auto && non_stop));
344}
765dc015 345
a14ed312 346void _initialize_breakpoint (void);
c906108c 347
c906108c
SS
348/* Are we executing breakpoint commands? */
349static int executing_breakpoint_commands;
350
c02f5703
MS
351/* Are overlay event breakpoints enabled? */
352static int overlay_events_enabled;
353
e09342b5
TJB
354/* See description in breakpoint.h. */
355int target_exact_watchpoints = 0;
356
c906108c 357/* Walk the following statement or block through all breakpoints.
4a64f543
MS
358 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
359 current breakpoint. */
c906108c 360
5c44784c 361#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 362
5c44784c
JM
363#define ALL_BREAKPOINTS_SAFE(B,TMP) \
364 for (B = breakpoint_chain; \
365 B ? (TMP=B->next, 1): 0; \
366 B = TMP)
c906108c 367
4a64f543
MS
368/* Similar iterator for the low-level breakpoints. SAFE variant is
369 not provided so update_global_location_list must not be called
370 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 371
876fa593
JK
372#define ALL_BP_LOCATIONS(B,BP_TMP) \
373 for (BP_TMP = bp_location; \
374 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
375 BP_TMP++)
7cc221ef 376
1042e4c0
SS
377/* Iterator for tracepoints only. */
378
379#define ALL_TRACEPOINTS(B) \
380 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 381 if (is_tracepoint (B))
1042e4c0 382
7cc221ef 383/* Chains of all breakpoints defined. */
c906108c
SS
384
385struct breakpoint *breakpoint_chain;
386
876fa593
JK
387/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
388
389static struct bp_location **bp_location;
390
391/* Number of elements of BP_LOCATION. */
392
393static unsigned bp_location_count;
394
4a64f543
MS
395/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
396 ADDRESS for the current elements of BP_LOCATION which get a valid
397 result from bp_location_has_shadow. You can use it for roughly
398 limiting the subrange of BP_LOCATION to scan for shadow bytes for
399 an address you need to read. */
876fa593
JK
400
401static CORE_ADDR bp_location_placed_address_before_address_max;
402
4a64f543
MS
403/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
404 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
405 BP_LOCATION which get a valid result from bp_location_has_shadow.
406 You can use it for roughly limiting the subrange of BP_LOCATION to
407 scan for shadow bytes for an address you need to read. */
876fa593
JK
408
409static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 410
4a64f543
MS
411/* The locations that no longer correspond to any breakpoint, unlinked
412 from bp_location array, but for which a hit may still be reported
413 by a target. */
20874c92
VP
414VEC(bp_location_p) *moribund_locations = NULL;
415
c906108c
SS
416/* Number of last breakpoint made. */
417
95a42b64
TT
418static int breakpoint_count;
419
86b17b60
PA
420/* The value of `breakpoint_count' before the last command that
421 created breakpoints. If the last (break-like) command created more
422 than one breakpoint, then the difference between BREAKPOINT_COUNT
423 and PREV_BREAKPOINT_COUNT is more than one. */
424static int prev_breakpoint_count;
c906108c 425
1042e4c0
SS
426/* Number of last tracepoint made. */
427
95a42b64 428static int tracepoint_count;
1042e4c0 429
6149aea9
PA
430static struct cmd_list_element *breakpoint_set_cmdlist;
431static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 432struct cmd_list_element *save_cmdlist;
6149aea9 433
468d015d
JJ
434/* Return whether a breakpoint is an active enabled breakpoint. */
435static int
436breakpoint_enabled (struct breakpoint *b)
437{
0d381245 438 return (b->enable_state == bp_enabled);
468d015d
JJ
439}
440
c906108c
SS
441/* Set breakpoint count to NUM. */
442
95a42b64 443static void
fba45db2 444set_breakpoint_count (int num)
c906108c 445{
86b17b60 446 prev_breakpoint_count = breakpoint_count;
c906108c 447 breakpoint_count = num;
4fa62494 448 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
449}
450
86b17b60
PA
451/* Used by `start_rbreak_breakpoints' below, to record the current
452 breakpoint count before "rbreak" creates any breakpoint. */
453static int rbreak_start_breakpoint_count;
454
95a42b64
TT
455/* Called at the start an "rbreak" command to record the first
456 breakpoint made. */
86b17b60 457
95a42b64
TT
458void
459start_rbreak_breakpoints (void)
460{
86b17b60 461 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
462}
463
464/* Called at the end of an "rbreak" command to record the last
465 breakpoint made. */
86b17b60 466
95a42b64
TT
467void
468end_rbreak_breakpoints (void)
469{
86b17b60 470 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
471}
472
4a64f543 473/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
474
475void
fba45db2 476clear_breakpoint_hit_counts (void)
c906108c
SS
477{
478 struct breakpoint *b;
479
480 ALL_BREAKPOINTS (b)
481 b->hit_count = 0;
482}
483
9add0f1b
TT
484/* Allocate a new counted_command_line with reference count of 1.
485 The new structure owns COMMANDS. */
486
487static struct counted_command_line *
488alloc_counted_command_line (struct command_line *commands)
489{
490 struct counted_command_line *result
491 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 492
9add0f1b
TT
493 result->refc = 1;
494 result->commands = commands;
495 return result;
496}
497
498/* Increment reference count. This does nothing if CMD is NULL. */
499
500static void
501incref_counted_command_line (struct counted_command_line *cmd)
502{
503 if (cmd)
504 ++cmd->refc;
505}
506
507/* Decrement reference count. If the reference count reaches 0,
508 destroy the counted_command_line. Sets *CMDP to NULL. This does
509 nothing if *CMDP is NULL. */
510
511static void
512decref_counted_command_line (struct counted_command_line **cmdp)
513{
514 if (*cmdp)
515 {
516 if (--(*cmdp)->refc == 0)
517 {
518 free_command_lines (&(*cmdp)->commands);
519 xfree (*cmdp);
520 }
521 *cmdp = NULL;
522 }
523}
524
525/* A cleanup function that calls decref_counted_command_line. */
526
527static void
528do_cleanup_counted_command_line (void *arg)
529{
530 decref_counted_command_line (arg);
531}
532
533/* Create a cleanup that calls decref_counted_command_line on the
534 argument. */
535
536static struct cleanup *
537make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
538{
539 return make_cleanup (do_cleanup_counted_command_line, cmdp);
540}
541
c906108c
SS
542/* Default address, symtab and line to put a breakpoint at
543 for "break" command with no arg.
4a64f543 544 If default_breakpoint_valid is zero, the other three are
c906108c
SS
545 not valid, and "break" with no arg is an error.
546
547 This set by print_stack_frame, which calls set_default_breakpoint. */
548
549int default_breakpoint_valid;
550CORE_ADDR default_breakpoint_address;
551struct symtab *default_breakpoint_symtab;
552int default_breakpoint_line;
6c95b8df
PA
553struct program_space *default_breakpoint_pspace;
554
c906108c 555\f
48cb2d85
VP
556/* Return the breakpoint with the specified number, or NULL
557 if the number does not refer to an existing breakpoint. */
558
559struct breakpoint *
560get_breakpoint (int num)
561{
562 struct breakpoint *b;
563
564 ALL_BREAKPOINTS (b)
565 if (b->number == num)
566 return b;
567
568 return NULL;
569}
5c44784c 570
c906108c 571\f
adc36818
PM
572
573void
574set_breakpoint_condition (struct breakpoint *b, char *exp,
575 int from_tty)
576{
577 struct bp_location *loc = b->loc;
578
579 for (; loc; loc = loc->next)
580 {
581 xfree (loc->cond);
582 loc->cond = NULL;
583 }
584 xfree (b->cond_string);
585 b->cond_string = NULL;
586 xfree (b->cond_exp);
587 b->cond_exp = NULL;
588
589 if (*exp == 0)
590 {
591 if (from_tty)
592 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
593 }
594 else
595 {
596 char *arg = exp;
cc59ec59 597
adc36818
PM
598 /* I don't know if it matters whether this is the string the user
599 typed in or the decompiled expression. */
600 b->cond_string = xstrdup (arg);
601 b->condition_not_parsed = 0;
602
603 if (is_watchpoint (b))
604 {
605 innermost_block = NULL;
606 arg = exp;
607 b->cond_exp = parse_exp_1 (&arg, 0, 0);
608 if (*arg)
609 error (_("Junk at end of expression"));
610 b->cond_exp_valid_block = innermost_block;
611 }
612 else
613 {
614 for (loc = b->loc; loc; loc = loc->next)
615 {
616 arg = exp;
617 loc->cond =
618 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
619 if (*arg)
620 error (_("Junk at end of expression"));
621 }
622 }
623 }
624 breakpoints_changed ();
625 observer_notify_breakpoint_modified (b->number);
626}
627
c906108c
SS
628/* condition N EXP -- set break condition of breakpoint N to EXP. */
629
630static void
fba45db2 631condition_command (char *arg, int from_tty)
c906108c 632{
52f0bd74 633 struct breakpoint *b;
c906108c 634 char *p;
52f0bd74 635 int bnum;
c906108c
SS
636
637 if (arg == 0)
e2e0b3e5 638 error_no_arg (_("breakpoint number"));
c906108c
SS
639
640 p = arg;
641 bnum = get_number (&p);
5c44784c 642 if (bnum == 0)
8a3fe4f8 643 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
644
645 ALL_BREAKPOINTS (b)
646 if (b->number == bnum)
2f069f6f 647 {
7371cf6d
PM
648 /* Check if this breakpoint has a Python object assigned to
649 it, and if it has a definition of the "stop"
650 method. This method and conditions entered into GDB from
651 the CLI are mutually exclusive. */
652 if (b->py_bp_object
653 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
654 error (_("Cannot set a condition where a Python 'stop' "
655 "method has been defined in the breakpoint."));
2566ad2d 656 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
657 return;
658 }
c906108c 659
8a3fe4f8 660 error (_("No breakpoint number %d."), bnum);
c906108c
SS
661}
662
a7bdde9e
VP
663/* Check that COMMAND do not contain commands that are suitable
664 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
665 Throw if any such commands is found. */
666
a7bdde9e
VP
667static void
668check_no_tracepoint_commands (struct command_line *commands)
669{
670 struct command_line *c;
cc59ec59 671
a7bdde9e
VP
672 for (c = commands; c; c = c->next)
673 {
674 int i;
675
676 if (c->control_type == while_stepping_control)
3e43a32a
MS
677 error (_("The 'while-stepping' command can "
678 "only be used for tracepoints"));
a7bdde9e
VP
679
680 for (i = 0; i < c->body_count; ++i)
681 check_no_tracepoint_commands ((c->body_list)[i]);
682
683 /* Not that command parsing removes leading whitespace and comment
4a64f543 684 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
685 command directly. */
686 if (strstr (c->line, "collect ") == c->line)
687 error (_("The 'collect' command can only be used for tracepoints"));
688
51661e93
VP
689 if (strstr (c->line, "teval ") == c->line)
690 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
691 }
692}
693
d77f58be
SS
694/* Encapsulate tests for different types of tracepoints. */
695
a7bdde9e 696int
d77f58be 697is_tracepoint (const struct breakpoint *b)
a7bdde9e 698{
0fb4aa4b
PA
699 return (b->type == bp_tracepoint
700 || b->type == bp_fast_tracepoint
701 || b->type == bp_static_tracepoint);
a7bdde9e 702}
d77f58be 703
95a42b64
TT
704/* A helper function that validsates that COMMANDS are valid for a
705 breakpoint. This function will throw an exception if a problem is
706 found. */
48cb2d85 707
95a42b64
TT
708static void
709validate_commands_for_breakpoint (struct breakpoint *b,
710 struct command_line *commands)
48cb2d85 711{
d77f58be 712 if (is_tracepoint (b))
a7bdde9e 713 {
4a64f543
MS
714 /* We need to verify that each top-level element of commands is
715 valid for tracepoints, that there's at most one
716 while-stepping element, and that while-stepping's body has
717 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
718 struct command_line *c;
719 struct command_line *while_stepping = 0;
720 for (c = commands; c; c = c->next)
721 {
a7bdde9e
VP
722 if (c->control_type == while_stepping_control)
723 {
724 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
725 error (_("The 'while-stepping' command "
726 "cannot be used for fast tracepoint"));
0fb4aa4b 727 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
728 error (_("The 'while-stepping' command "
729 "cannot be used for static tracepoint"));
a7bdde9e
VP
730
731 if (while_stepping)
3e43a32a
MS
732 error (_("The 'while-stepping' command "
733 "can be used only once"));
a7bdde9e
VP
734 else
735 while_stepping = c;
736 }
737 }
738 if (while_stepping)
739 {
740 struct command_line *c2;
741
742 gdb_assert (while_stepping->body_count == 1);
743 c2 = while_stepping->body_list[0];
744 for (; c2; c2 = c2->next)
745 {
a7bdde9e
VP
746 if (c2->control_type == while_stepping_control)
747 error (_("The 'while-stepping' command cannot be nested"));
748 }
749 }
750 }
751 else
752 {
753 check_no_tracepoint_commands (commands);
754 }
95a42b64
TT
755}
756
0fb4aa4b
PA
757/* Return a vector of all the static tracepoints set at ADDR. The
758 caller is responsible for releasing the vector. */
759
760VEC(breakpoint_p) *
761static_tracepoints_here (CORE_ADDR addr)
762{
763 struct breakpoint *b;
764 VEC(breakpoint_p) *found = 0;
765 struct bp_location *loc;
766
767 ALL_BREAKPOINTS (b)
768 if (b->type == bp_static_tracepoint)
769 {
770 for (loc = b->loc; loc; loc = loc->next)
771 if (loc->address == addr)
772 VEC_safe_push(breakpoint_p, found, b);
773 }
774
775 return found;
776}
777
95a42b64 778/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 779 validate that only allowed commands are included. */
95a42b64
TT
780
781void
4a64f543
MS
782breakpoint_set_commands (struct breakpoint *b,
783 struct command_line *commands)
95a42b64
TT
784{
785 validate_commands_for_breakpoint (b, commands);
a7bdde9e 786
9add0f1b
TT
787 decref_counted_command_line (&b->commands);
788 b->commands = alloc_counted_command_line (commands);
48cb2d85
VP
789 breakpoints_changed ();
790 observer_notify_breakpoint_modified (b->number);
791}
792
45a43567
TT
793/* Set the internal `silent' flag on the breakpoint. Note that this
794 is not the same as the "silent" that may appear in the breakpoint's
795 commands. */
796
797void
798breakpoint_set_silent (struct breakpoint *b, int silent)
799{
800 int old_silent = b->silent;
801
802 b->silent = silent;
803 if (old_silent != silent)
804 observer_notify_breakpoint_modified (b->number);
805}
806
807/* Set the thread for this breakpoint. If THREAD is -1, make the
808 breakpoint work for any thread. */
809
810void
811breakpoint_set_thread (struct breakpoint *b, int thread)
812{
813 int old_thread = b->thread;
814
815 b->thread = thread;
816 if (old_thread != thread)
817 observer_notify_breakpoint_modified (b->number);
818}
819
820/* Set the task for this breakpoint. If TASK is 0, make the
821 breakpoint work for any task. */
822
823void
824breakpoint_set_task (struct breakpoint *b, int task)
825{
826 int old_task = b->task;
827
828 b->task = task;
829 if (old_task != task)
830 observer_notify_breakpoint_modified (b->number);
831}
832
95a42b64
TT
833void
834check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
835{
836 struct breakpoint *b = closure;
cc59ec59 837
a7bdde9e
VP
838 validate_actionline (&line, b);
839}
840
95a42b64
TT
841/* A structure used to pass information through
842 map_breakpoint_numbers. */
843
844struct commands_info
845{
846 /* True if the command was typed at a tty. */
847 int from_tty;
86b17b60
PA
848
849 /* The breakpoint range spec. */
850 char *arg;
851
95a42b64
TT
852 /* Non-NULL if the body of the commands are being read from this
853 already-parsed command. */
854 struct command_line *control;
86b17b60 855
95a42b64
TT
856 /* The command lines read from the user, or NULL if they have not
857 yet been read. */
858 struct counted_command_line *cmd;
859};
860
861/* A callback for map_breakpoint_numbers that sets the commands for
862 commands_command. */
863
c906108c 864static void
95a42b64 865do_map_commands_command (struct breakpoint *b, void *data)
c906108c 866{
95a42b64 867 struct commands_info *info = data;
c906108c 868
95a42b64
TT
869 if (info->cmd == NULL)
870 {
871 struct command_line *l;
5c44784c 872
95a42b64
TT
873 if (info->control != NULL)
874 l = copy_command_lines (info->control->body_list[0]);
875 else
86b17b60
PA
876 {
877 struct cleanup *old_chain;
878 char *str;
c5aa993b 879
3e43a32a
MS
880 str = xstrprintf (_("Type commands for breakpoint(s) "
881 "%s, one per line."),
86b17b60
PA
882 info->arg);
883
884 old_chain = make_cleanup (xfree, str);
885
886 l = read_command_lines (str,
887 info->from_tty, 1,
d77f58be 888 (is_tracepoint (b)
86b17b60
PA
889 ? check_tracepoint_command : 0),
890 b);
891
892 do_cleanups (old_chain);
893 }
a7bdde9e 894
95a42b64
TT
895 info->cmd = alloc_counted_command_line (l);
896 }
897
898 /* If a breakpoint was on the list more than once, we don't need to
899 do anything. */
900 if (b->commands != info->cmd)
901 {
902 validate_commands_for_breakpoint (b, info->cmd->commands);
903 incref_counted_command_line (info->cmd);
904 decref_counted_command_line (&b->commands);
905 b->commands = info->cmd;
906 breakpoints_changed ();
907 observer_notify_breakpoint_modified (b->number);
c5aa993b 908 }
95a42b64
TT
909}
910
911static void
4a64f543
MS
912commands_command_1 (char *arg, int from_tty,
913 struct command_line *control)
95a42b64
TT
914{
915 struct cleanup *cleanups;
916 struct commands_info info;
917
918 info.from_tty = from_tty;
919 info.control = control;
920 info.cmd = NULL;
921 /* If we read command lines from the user, then `info' will hold an
922 extra reference to the commands that we must clean up. */
923 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
924
925 if (arg == NULL || !*arg)
926 {
86b17b60 927 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
928 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
929 breakpoint_count);
95a42b64
TT
930 else if (breakpoint_count > 0)
931 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
932 else
933 {
934 /* So that we don't try to free the incoming non-NULL
935 argument in the cleanup below. Mapping breakpoint
936 numbers will fail in this case. */
937 arg = NULL;
938 }
95a42b64 939 }
9766ced4
SS
940 else
941 /* The command loop has some static state, so we need to preserve
942 our argument. */
943 arg = xstrdup (arg);
86b17b60
PA
944
945 if (arg != NULL)
946 make_cleanup (xfree, arg);
947
948 info.arg = arg;
95a42b64
TT
949
950 map_breakpoint_numbers (arg, do_map_commands_command, &info);
951
952 if (info.cmd == NULL)
953 error (_("No breakpoints specified."));
954
955 do_cleanups (cleanups);
956}
957
958static void
959commands_command (char *arg, int from_tty)
960{
961 commands_command_1 (arg, from_tty, NULL);
c906108c 962}
40c03ae8
EZ
963
964/* Like commands_command, but instead of reading the commands from
965 input stream, takes them from an already parsed command structure.
966
967 This is used by cli-script.c to DTRT with breakpoint commands
968 that are part of if and while bodies. */
969enum command_control_type
970commands_from_control_command (char *arg, struct command_line *cmd)
971{
95a42b64
TT
972 commands_command_1 (arg, 0, cmd);
973 return simple_control;
40c03ae8 974}
876fa593
JK
975
976/* Return non-zero if BL->TARGET_INFO contains valid information. */
977
978static int
979bp_location_has_shadow (struct bp_location *bl)
980{
981 if (bl->loc_type != bp_loc_software_breakpoint)
982 return 0;
983 if (!bl->inserted)
984 return 0;
985 if (bl->target_info.shadow_len == 0)
986 /* bp isn't valid, or doesn't shadow memory. */
987 return 0;
988 return 1;
989}
990
8defab1a 991/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
992 by replacing any memory breakpoints with their shadowed contents.
993
994 The range of shadowed area by each bp_location is:
35df4500
TJB
995 bl->address - bp_location_placed_address_before_address_max
996 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
997 The range we were requested to resolve shadows for is:
998 memaddr ... memaddr + len
999 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1000 memaddr + len <= (bl->address
1001 - bp_location_placed_address_before_address_max)
876fa593 1002 and:
35df4500 1003 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1004
8defab1a
DJ
1005void
1006breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1007{
4a64f543
MS
1008 /* Left boundary, right boundary and median element of our binary
1009 search. */
876fa593
JK
1010 unsigned bc_l, bc_r, bc;
1011
4a64f543
MS
1012 /* Find BC_L which is a leftmost element which may affect BUF
1013 content. It is safe to report lower value but a failure to
1014 report higher one. */
876fa593
JK
1015
1016 bc_l = 0;
1017 bc_r = bp_location_count;
1018 while (bc_l + 1 < bc_r)
1019 {
35df4500 1020 struct bp_location *bl;
876fa593
JK
1021
1022 bc = (bc_l + bc_r) / 2;
35df4500 1023 bl = bp_location[bc];
876fa593 1024
4a64f543
MS
1025 /* Check first BL->ADDRESS will not overflow due to the added
1026 constant. Then advance the left boundary only if we are sure
1027 the BC element can in no way affect the BUF content (MEMADDR
1028 to MEMADDR + LEN range).
876fa593 1029
4a64f543
MS
1030 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1031 offset so that we cannot miss a breakpoint with its shadow
1032 range tail still reaching MEMADDR. */
c5aa993b 1033
35df4500
TJB
1034 if ((bl->address + bp_location_shadow_len_after_address_max
1035 >= bl->address)
1036 && (bl->address + bp_location_shadow_len_after_address_max
1037 <= memaddr))
876fa593
JK
1038 bc_l = bc;
1039 else
1040 bc_r = bc;
1041 }
1042
128070bb
PA
1043 /* Due to the binary search above, we need to make sure we pick the
1044 first location that's at BC_L's address. E.g., if there are
1045 multiple locations at the same address, BC_L may end up pointing
1046 at a duplicate location, and miss the "master"/"inserted"
1047 location. Say, given locations L1, L2 and L3 at addresses A and
1048 B:
1049
1050 L1@A, L2@A, L3@B, ...
1051
1052 BC_L could end up pointing at location L2, while the "master"
1053 location could be L1. Since the `loc->inserted' flag is only set
1054 on "master" locations, we'd forget to restore the shadow of L1
1055 and L2. */
1056 while (bc_l > 0
1057 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1058 bc_l--;
1059
876fa593
JK
1060 /* Now do full processing of the found relevant range of elements. */
1061
1062 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1063 {
35df4500 1064 struct bp_location *bl = bp_location[bc];
876fa593
JK
1065 CORE_ADDR bp_addr = 0;
1066 int bp_size = 0;
1067 int bptoffset = 0;
1068
35df4500
TJB
1069 /* bp_location array has BL->OWNER always non-NULL. */
1070 if (bl->owner->type == bp_none)
8a3fe4f8 1071 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1072 bl->owner->number);
ffce0d52 1073
876fa593
JK
1074 /* Performance optimization: any futher element can no longer affect BUF
1075 content. */
1076
35df4500
TJB
1077 if (bl->address >= bp_location_placed_address_before_address_max
1078 && memaddr + len <= (bl->address
1079 - bp_location_placed_address_before_address_max))
876fa593
JK
1080 break;
1081
35df4500 1082 if (!bp_location_has_shadow (bl))
c5aa993b 1083 continue;
35df4500 1084 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1085 current_program_space->aspace, 0))
1086 continue;
1087
c5aa993b
JM
1088 /* Addresses and length of the part of the breakpoint that
1089 we need to copy. */
35df4500
TJB
1090 bp_addr = bl->target_info.placed_address;
1091 bp_size = bl->target_info.shadow_len;
8defab1a 1092
c5aa993b
JM
1093 if (bp_addr + bp_size <= memaddr)
1094 /* The breakpoint is entirely before the chunk of memory we
1095 are reading. */
1096 continue;
8defab1a 1097
c5aa993b
JM
1098 if (bp_addr >= memaddr + len)
1099 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1100 reading. */
c5aa993b 1101 continue;
c5aa993b 1102
8defab1a
DJ
1103 /* Offset within shadow_contents. */
1104 if (bp_addr < memaddr)
1105 {
1106 /* Only copy the second part of the breakpoint. */
1107 bp_size -= memaddr - bp_addr;
1108 bptoffset = memaddr - bp_addr;
1109 bp_addr = memaddr;
1110 }
c5aa993b 1111
8defab1a
DJ
1112 if (bp_addr + bp_size > memaddr + len)
1113 {
1114 /* Only copy the first part of the breakpoint. */
1115 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1116 }
c5aa993b 1117
8defab1a 1118 memcpy (buf + bp_addr - memaddr,
35df4500 1119 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1120 }
c906108c 1121}
c906108c 1122\f
c5aa993b 1123
60e1c644
PA
1124/* Return true if BPT is of any hardware watchpoint kind. */
1125
a5606eee 1126static int
d77f58be 1127is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1128{
1129 return (bpt->type == bp_hardware_watchpoint
1130 || bpt->type == bp_read_watchpoint
1131 || bpt->type == bp_access_watchpoint);
1132}
7270d8f2 1133
60e1c644
PA
1134/* Return true if BPT is of any watchpoint kind, hardware or
1135 software. */
1136
1137static int
d77f58be 1138is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1139{
1140 return (is_hardware_watchpoint (bpt)
1141 || bpt->type == bp_watchpoint);
1142}
1143
f6bc2008
PA
1144/* Assuming that B is a watchpoint: returns true if the current thread
1145 and its running state are safe to evaluate or update watchpoint B.
1146 Watchpoints on local expressions need to be evaluated in the
1147 context of the thread that was current when the watchpoint was
1148 created, and, that thread needs to be stopped to be able to select
1149 the correct frame context. Watchpoints on global expressions can
1150 be evaluated on any thread, and in any state. It is presently left
1151 to the target allowing memory accesses when threads are
1152 running. */
1153
1154static int
1155watchpoint_in_thread_scope (struct breakpoint *b)
1156{
1157 return (ptid_equal (b->watchpoint_thread, null_ptid)
1158 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1159 && !is_executing (inferior_ptid)));
1160}
1161
d0fb5eae
JK
1162/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1163 associated bp_watchpoint_scope breakpoint. */
1164
1165static void
1166watchpoint_del_at_next_stop (struct breakpoint *b)
1167{
1168 gdb_assert (is_watchpoint (b));
1169
1170 if (b->related_breakpoint != b)
1171 {
1172 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1173 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1174 b->related_breakpoint->disposition = disp_del_at_next_stop;
1175 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1176 b->related_breakpoint = b;
1177 }
1178 b->disposition = disp_del_at_next_stop;
1179}
1180
567e1b4e
JB
1181/* Assuming that B is a watchpoint:
1182 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1183 - Evaluate expression and store the result in B->val
567e1b4e
JB
1184 - Evaluate the condition if there is one, and store the result
1185 in b->loc->cond.
a5606eee
VP
1186 - Update the list of values that must be watched in B->loc.
1187
4a64f543
MS
1188 If the watchpoint disposition is disp_del_at_next_stop, then do
1189 nothing. If this is local watchpoint that is out of scope, delete
1190 it.
1191
1192 Even with `set breakpoint always-inserted on' the watchpoints are
1193 removed + inserted on each stop here. Normal breakpoints must
1194 never be removed because they might be missed by a running thread
1195 when debugging in non-stop mode. On the other hand, hardware
1196 watchpoints (is_hardware_watchpoint; processed here) are specific
1197 to each LWP since they are stored in each LWP's hardware debug
1198 registers. Therefore, such LWP must be stopped first in order to
1199 be able to modify its hardware watchpoints.
1200
1201 Hardware watchpoints must be reset exactly once after being
1202 presented to the user. It cannot be done sooner, because it would
1203 reset the data used to present the watchpoint hit to the user. And
1204 it must not be done later because it could display the same single
1205 watchpoint hit during multiple GDB stops. Note that the latter is
1206 relevant only to the hardware watchpoint types bp_read_watchpoint
1207 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1208 not user-visible - its hit is suppressed if the memory content has
1209 not changed.
1210
1211 The following constraints influence the location where we can reset
1212 hardware watchpoints:
1213
1214 * target_stopped_by_watchpoint and target_stopped_data_address are
1215 called several times when GDB stops.
1216
1217 [linux]
1218 * Multiple hardware watchpoints can be hit at the same time,
1219 causing GDB to stop. GDB only presents one hardware watchpoint
1220 hit at a time as the reason for stopping, and all the other hits
1221 are presented later, one after the other, each time the user
1222 requests the execution to be resumed. Execution is not resumed
1223 for the threads still having pending hit event stored in
1224 LWP_INFO->STATUS. While the watchpoint is already removed from
1225 the inferior on the first stop the thread hit event is kept being
1226 reported from its cached value by linux_nat_stopped_data_address
1227 until the real thread resume happens after the watchpoint gets
1228 presented and thus its LWP_INFO->STATUS gets reset.
1229
1230 Therefore the hardware watchpoint hit can get safely reset on the
1231 watchpoint removal from inferior. */
a79d3c27 1232
b40ce68a 1233static void
a5606eee 1234update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1235{
a5606eee 1236 int within_current_scope;
a5606eee 1237 struct frame_id saved_frame_id;
66076460 1238 int frame_saved;
a5606eee 1239
d0fb5eae
JK
1240 gdb_assert (is_watchpoint (b));
1241
f6bc2008
PA
1242 /* If this is a local watchpoint, we only want to check if the
1243 watchpoint frame is in scope if the current thread is the thread
1244 that was used to create the watchpoint. */
1245 if (!watchpoint_in_thread_scope (b))
1246 return;
1247
a5606eee
VP
1248 if (b->disposition == disp_del_at_next_stop)
1249 return;
1250
66076460 1251 frame_saved = 0;
a5606eee
VP
1252
1253 /* Determine if the watchpoint is within scope. */
1254 if (b->exp_valid_block == NULL)
1255 within_current_scope = 1;
1256 else
1257 {
b5db5dfc
UW
1258 struct frame_info *fi = get_current_frame ();
1259 struct gdbarch *frame_arch = get_frame_arch (fi);
1260 CORE_ADDR frame_pc = get_frame_pc (fi);
1261
1262 /* If we're in a function epilogue, unwinding may not work
1263 properly, so do not attempt to recreate locations at this
1264 point. See similar comments in watchpoint_check. */
1265 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1266 return;
66076460
DJ
1267
1268 /* Save the current frame's ID so we can restore it after
1269 evaluating the watchpoint expression on its own frame. */
1270 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1271 took a frame parameter, so that we didn't have to change the
1272 selected frame. */
1273 frame_saved = 1;
1274 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1275
a5606eee
VP
1276 fi = frame_find_by_id (b->watchpoint_frame);
1277 within_current_scope = (fi != NULL);
1278 if (within_current_scope)
1279 select_frame (fi);
1280 }
1281
b5db5dfc
UW
1282 /* We don't free locations. They are stored in the bp_location array
1283 and update_global_location_list will eventually delete them and
1284 remove breakpoints if needed. */
1285 b->loc = NULL;
1286
a5606eee
VP
1287 if (within_current_scope && reparse)
1288 {
1289 char *s;
d63d0675 1290
a5606eee
VP
1291 if (b->exp)
1292 {
1293 xfree (b->exp);
1294 b->exp = NULL;
1295 }
d63d0675 1296 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1297 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1298 /* If the meaning of expression itself changed, the old value is
1299 no longer relevant. We don't want to report a watchpoint hit
1300 to the user when the old value and the new value may actually
1301 be completely different objects. */
1302 value_free (b->val);
fa4727a6
DJ
1303 b->val = NULL;
1304 b->val_valid = 0;
60e1c644
PA
1305
1306 /* Note that unlike with breakpoints, the watchpoint's condition
1307 expression is stored in the breakpoint object, not in the
1308 locations (re)created below. */
1309 if (b->cond_string != NULL)
1310 {
1311 if (b->cond_exp != NULL)
1312 {
1313 xfree (b->cond_exp);
1314 b->cond_exp = NULL;
1315 }
1316
1317 s = b->cond_string;
1318 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1319 }
a5606eee 1320 }
a5606eee
VP
1321
1322 /* If we failed to parse the expression, for example because
1323 it refers to a global variable in a not-yet-loaded shared library,
1324 don't try to insert watchpoint. We don't automatically delete
1325 such watchpoint, though, since failure to parse expression
1326 is different from out-of-scope watchpoint. */
2d134ed3
PA
1327 if ( !target_has_execution)
1328 {
1329 /* Without execution, memory can't change. No use to try and
1330 set watchpoint locations. The watchpoint will be reset when
1331 the target gains execution, through breakpoint_re_set. */
1332 }
1333 else if (within_current_scope && b->exp)
a5606eee 1334 {
0cf6dd15 1335 int pc = 0;
fa4727a6 1336 struct value *val_chain, *v, *result, *next;
2d134ed3 1337 struct program_space *frame_pspace;
a5606eee 1338
0cf6dd15 1339 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1340
a5606eee
VP
1341 /* Avoid setting b->val if it's already set. The meaning of
1342 b->val is 'the last value' user saw, and we should update
1343 it only if we reported that last value to user. As it
1344 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1345 if (!b->val_valid)
1346 {
1347 b->val = v;
1348 b->val_valid = 1;
1349 }
a5606eee 1350
2d134ed3
PA
1351 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1352
a5606eee 1353 /* Look at each value on the value chain. */
9fa40276 1354 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1355 {
1356 /* If it's a memory location, and GDB actually needed
1357 its contents to evaluate the expression, then we
fa4727a6
DJ
1358 must watch it. If the first value returned is
1359 still lazy, that means an error occurred reading it;
1360 watch it anyway in case it becomes readable. */
a5606eee 1361 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1362 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1363 {
1364 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1365
a5606eee
VP
1366 /* We only watch structs and arrays if user asked
1367 for it explicitly, never if they just happen to
1368 appear in the middle of some value chain. */
fa4727a6 1369 if (v == result
a5606eee
VP
1370 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1371 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1372 {
1373 CORE_ADDR addr;
1374 int len, type;
1375 struct bp_location *loc, **tmp;
1376
42ae5230 1377 addr = value_address (v);
a5606eee
VP
1378 len = TYPE_LENGTH (value_type (v));
1379 type = hw_write;
1380 if (b->type == bp_read_watchpoint)
1381 type = hw_read;
1382 else if (b->type == bp_access_watchpoint)
1383 type = hw_access;
1384
39d61571 1385 loc = allocate_bp_location (b);
a5606eee
VP
1386 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1387 ;
1388 *tmp = loc;
a6d9a66e 1389 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1390
1391 loc->pspace = frame_pspace;
a5606eee
VP
1392 loc->address = addr;
1393 loc->length = len;
1394 loc->watchpoint_type = type;
1395 }
1396 }
9fa40276
TJB
1397 }
1398
1399 /* Change the type of breakpoint between hardware assisted or
1400 an ordinary watchpoint depending on the hardware support
1401 and free hardware slots. REPARSE is set when the inferior
1402 is started. */
1403 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1404 && reparse)
1405 {
e09342b5 1406 int reg_cnt;
9fa40276
TJB
1407 enum bp_loc_type loc_type;
1408 struct bp_location *bl;
a5606eee 1409
e09342b5
TJB
1410 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1411
1412 if (reg_cnt)
9fa40276
TJB
1413 {
1414 int i, target_resources_ok, other_type_used;
e09342b5 1415 enum enable_state orig_enable_state;
9fa40276
TJB
1416
1417 /* We need to determine how many resources are already
e09342b5
TJB
1418 used for all other hardware watchpoints plus this one
1419 to see if we still have enough resources to also fit
1420 this watchpoint in as well. To guarantee the
1421 hw_watchpoint_used_count call below counts this
1422 watchpoint, make sure that it is marked as a hardware
1423 watchpoint. */
1424 b->type = bp_hardware_watchpoint;
1425
1426 /* hw_watchpoint_used_count ignores disabled watchpoints,
1427 and b might be disabled if we're being called from
1428 do_enable_breakpoint. */
1429 orig_enable_state = b->enable_state;
1430 b->enable_state = bp_enabled;
1431
9fa40276
TJB
1432 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1433 &other_type_used);
9fa40276 1434
e09342b5
TJB
1435 b->enable_state = orig_enable_state;
1436
1437 target_resources_ok = target_can_use_hardware_watchpoint
1438 (bp_hardware_watchpoint, i, other_type_used);
1439 if (target_resources_ok <= 0)
1440 b->type = bp_watchpoint;
9fa40276
TJB
1441 }
1442 else
1443 b->type = bp_watchpoint;
1444
1445 loc_type = (b->type == bp_watchpoint? bp_loc_other
1446 : bp_loc_hardware_watchpoint);
1447 for (bl = b->loc; bl; bl = bl->next)
1448 bl->loc_type = loc_type;
1449 }
1450
1451 for (v = val_chain; v; v = next)
1452 {
a5606eee
VP
1453 next = value_next (v);
1454 if (v != b->val)
1455 value_free (v);
1456 }
1457
c7437ca6
PA
1458 /* If a software watchpoint is not watching any memory, then the
1459 above left it without any location set up. But,
1460 bpstat_stop_status requires a location to be able to report
1461 stops, so make sure there's at least a dummy one. */
1462 if (b->type == bp_watchpoint && b->loc == NULL)
1463 {
1464 b->loc = allocate_bp_location (b);
1465 b->loc->pspace = frame_pspace;
1466 b->loc->address = -1;
1467 b->loc->length = -1;
1468 b->loc->watchpoint_type = -1;
1469 }
a5606eee
VP
1470 }
1471 else if (!within_current_scope)
7270d8f2 1472 {
ac74f770
MS
1473 printf_filtered (_("\
1474Watchpoint %d deleted because the program has left the block\n\
1475in which its expression is valid.\n"),
a5606eee 1476 b->number);
d0fb5eae 1477 watchpoint_del_at_next_stop (b);
7270d8f2 1478 }
a5606eee
VP
1479
1480 /* Restore the selected frame. */
66076460
DJ
1481 if (frame_saved)
1482 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1483}
1484
a5606eee 1485
74960c60
VP
1486/* Returns 1 iff breakpoint location should be
1487 inserted in the inferior. */
1488static int
35df4500 1489should_be_inserted (struct bp_location *bl)
74960c60 1490{
35df4500 1491 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1492 return 0;
1493
35df4500 1494 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1495 return 0;
1496
35df4500 1497 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1498 return 0;
1499
56710373
PA
1500 /* This is set for example, when we're attached to the parent of a
1501 vfork, and have detached from the child. The child is running
1502 free, and we expect it to do an exec or exit, at which point the
1503 OS makes the parent schedulable again (and the target reports
1504 that the vfork is done). Until the child is done with the shared
1505 memory region, do not insert breakpoints in the parent, otherwise
1506 the child could still trip on the parent's breakpoints. Since
1507 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1508 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1509 return 0;
1510
1042e4c0
SS
1511 /* Tracepoints are inserted by the target at a time of its choosing,
1512 not by us. */
35df4500 1513 if (is_tracepoint (bl->owner))
1042e4c0
SS
1514 return 0;
1515
74960c60
VP
1516 return 1;
1517}
1518
35df4500
TJB
1519/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1520 location. Any error messages are printed to TMP_ERROR_STREAM; and
1521 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1522
4a64f543
MS
1523 NOTE drow/2003-09-09: This routine could be broken down to an
1524 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1525static int
35df4500 1526insert_bp_location (struct bp_location *bl,
26bb91f3 1527 struct ui_file *tmp_error_stream,
fa3a767f 1528 int *disabled_breaks,
26bb91f3 1529 int *hw_breakpoint_error)
879bfdc2
DJ
1530{
1531 int val = 0;
1532
35df4500 1533 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1534 return 0;
1535
8181d85f 1536 /* Initialize the target-specific information. */
35df4500
TJB
1537 memset (&bl->target_info, 0, sizeof (bl->target_info));
1538 bl->target_info.placed_address = bl->address;
1539 bl->target_info.placed_address_space = bl->pspace->aspace;
8181d85f 1540
35df4500
TJB
1541 if (bl->loc_type == bp_loc_software_breakpoint
1542 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1543 {
35df4500 1544 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1545 {
1546 /* If the explicitly specified breakpoint type
1547 is not hardware breakpoint, check the memory map to see
1548 if the breakpoint address is in read only memory or not.
4a64f543 1549
765dc015
VP
1550 Two important cases are:
1551 - location type is not hardware breakpoint, memory
1552 is readonly. We change the type of the location to
1553 hardware breakpoint.
4a64f543
MS
1554 - location type is hardware breakpoint, memory is
1555 read-write. This means we've previously made the
1556 location hardware one, but then the memory map changed,
1557 so we undo.
765dc015 1558
4a64f543
MS
1559 When breakpoints are removed, remove_breakpoints will use
1560 location types we've just set here, the only possible
1561 problem is that memory map has changed during running
1562 program, but it's not going to work anyway with current
1563 gdb. */
765dc015 1564 struct mem_region *mr
35df4500 1565 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1566
1567 if (mr)
1568 {
1569 if (automatic_hardware_breakpoints)
1570 {
765dc015
VP
1571 enum bp_loc_type new_type;
1572
1573 if (mr->attrib.mode != MEM_RW)
1574 new_type = bp_loc_hardware_breakpoint;
1575 else
1576 new_type = bp_loc_software_breakpoint;
1577
35df4500 1578 if (new_type != bl->loc_type)
765dc015
VP
1579 {
1580 static int said = 0;
cc59ec59 1581
35df4500 1582 bl->loc_type = new_type;
765dc015
VP
1583 if (!said)
1584 {
3e43a32a
MS
1585 fprintf_filtered (gdb_stdout,
1586 _("Note: automatically using "
1587 "hardware breakpoints for "
1588 "read-only addresses.\n"));
765dc015
VP
1589 said = 1;
1590 }
1591 }
1592 }
35df4500 1593 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1594 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1595 warning (_("cannot set software breakpoint "
1596 "at readonly address %s"),
35df4500 1597 paddress (bl->gdbarch, bl->address));
765dc015
VP
1598 }
1599 }
1600
879bfdc2
DJ
1601 /* First check to see if we have to handle an overlay. */
1602 if (overlay_debugging == ovly_off
35df4500
TJB
1603 || bl->section == NULL
1604 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1605 {
1606 /* No overlay handling: just set the breakpoint. */
1607
35df4500
TJB
1608 if (bl->loc_type == bp_loc_hardware_breakpoint)
1609 val = target_insert_hw_breakpoint (bl->gdbarch,
1610 &bl->target_info);
879bfdc2 1611 else
35df4500
TJB
1612 val = target_insert_breakpoint (bl->gdbarch,
1613 &bl->target_info);
879bfdc2
DJ
1614 }
1615 else
1616 {
4a64f543 1617 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1618 Shall we set a breakpoint at the LMA? */
1619 if (!overlay_events_enabled)
1620 {
1621 /* Yes -- overlay event support is not active,
1622 so we must try to set a breakpoint at the LMA.
1623 This will not work for a hardware breakpoint. */
35df4500 1624 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1625 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1626 bl->owner->number);
879bfdc2
DJ
1627 else
1628 {
35df4500
TJB
1629 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1630 bl->section);
879bfdc2 1631 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1632 bl->overlay_target_info = bl->target_info;
1633 bl->overlay_target_info.placed_address = addr;
1634 val = target_insert_breakpoint (bl->gdbarch,
1635 &bl->overlay_target_info);
879bfdc2 1636 if (val != 0)
99361f52 1637 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1638 "Overlay breakpoint %d "
1639 "failed: in ROM?\n",
35df4500 1640 bl->owner->number);
879bfdc2
DJ
1641 }
1642 }
1643 /* Shall we set a breakpoint at the VMA? */
35df4500 1644 if (section_is_mapped (bl->section))
879bfdc2
DJ
1645 {
1646 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1647 if (bl->loc_type == bp_loc_hardware_breakpoint)
1648 val = target_insert_hw_breakpoint (bl->gdbarch,
1649 &bl->target_info);
879bfdc2 1650 else
35df4500
TJB
1651 val = target_insert_breakpoint (bl->gdbarch,
1652 &bl->target_info);
879bfdc2
DJ
1653 }
1654 else
1655 {
1656 /* No. This breakpoint will not be inserted.
1657 No error, but do not mark the bp as 'inserted'. */
1658 return 0;
1659 }
1660 }
1661
1662 if (val)
1663 {
1664 /* Can't set the breakpoint. */
35df4500 1665 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1666 {
4a64f543 1667 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1668 val = 0;
35df4500 1669 bl->shlib_disabled = 1;
879bfdc2
DJ
1670 if (!*disabled_breaks)
1671 {
1672 fprintf_unfiltered (tmp_error_stream,
1673 "Cannot insert breakpoint %d.\n",
35df4500 1674 bl->owner->number);
879bfdc2 1675 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1676 "Temporarily disabling shared "
1677 "library breakpoints:\n");
879bfdc2
DJ
1678 }
1679 *disabled_breaks = 1;
1680 fprintf_unfiltered (tmp_error_stream,
35df4500 1681 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1682 }
1683 else
879bfdc2 1684 {
35df4500 1685 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1686 {
1687 *hw_breakpoint_error = 1;
3e43a32a
MS
1688 fprintf_unfiltered (tmp_error_stream,
1689 "Cannot insert hardware "
1690 "breakpoint %d.\n",
35df4500 1691 bl->owner->number);
879bfdc2
DJ
1692 }
1693 else
1694 {
1695 fprintf_unfiltered (tmp_error_stream,
1696 "Cannot insert breakpoint %d.\n",
35df4500 1697 bl->owner->number);
879bfdc2
DJ
1698 fprintf_filtered (tmp_error_stream,
1699 "Error accessing memory address ");
35df4500 1700 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1701 tmp_error_stream);
879bfdc2
DJ
1702 fprintf_filtered (tmp_error_stream, ": %s.\n",
1703 safe_strerror (val));
1704 }
1705
1706 }
1707 }
1708 else
35df4500 1709 bl->inserted = 1;
879bfdc2
DJ
1710
1711 return val;
1712 }
1713
35df4500 1714 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1715 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1716 watchpoints. It's not clear that it's necessary... */
35df4500 1717 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1718 {
77b06cd7
TJB
1719 gdb_assert (bl->owner->ops != NULL
1720 && bl->owner->ops->insert_location != NULL);
1721
1722 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1723
1724 /* If trying to set a read-watchpoint, and it turns out it's not
1725 supported, try emulating one with an access watchpoint. */
35df4500 1726 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1727 {
1728 struct bp_location *loc, **loc_temp;
1729
1730 /* But don't try to insert it, if there's already another
1731 hw_access location that would be considered a duplicate
1732 of this one. */
1733 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1734 if (loc != bl
85d721b8 1735 && loc->watchpoint_type == hw_access
35df4500 1736 && watchpoint_locations_match (bl, loc))
85d721b8 1737 {
35df4500
TJB
1738 bl->duplicate = 1;
1739 bl->inserted = 1;
1740 bl->target_info = loc->target_info;
1741 bl->watchpoint_type = hw_access;
85d721b8
PA
1742 val = 0;
1743 break;
1744 }
1745
1746 if (val == 1)
1747 {
77b06cd7
TJB
1748 bl->watchpoint_type = hw_access;
1749 val = bl->owner->ops->insert_location (bl);
1750
1751 if (val)
1752 /* Back to the original value. */
1753 bl->watchpoint_type = hw_read;
85d721b8
PA
1754 }
1755 }
1756
35df4500 1757 bl->inserted = (val == 0);
879bfdc2
DJ
1758 }
1759
35df4500 1760 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1761 {
77b06cd7
TJB
1762 gdb_assert (bl->owner->ops != NULL
1763 && bl->owner->ops->insert_location != NULL);
1764
1765 val = bl->owner->ops->insert_location (bl);
1766 if (val)
1767 {
1768 bl->owner->enable_state = bp_disabled;
1769
1770 if (val == 1)
1771 warning (_("\
1772Error inserting catchpoint %d: Your system does not support this type\n\
1773of catchpoint."), bl->owner->number);
1774 else
1775 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1776 }
1777
1778 bl->inserted = (val == 0);
1640b821
DJ
1779
1780 /* We've already printed an error message if there was a problem
1781 inserting this catchpoint, and we've disabled the catchpoint,
1782 so just return success. */
1783 return 0;
879bfdc2
DJ
1784 }
1785
1786 return 0;
1787}
1788
6c95b8df
PA
1789/* This function is called when program space PSPACE is about to be
1790 deleted. It takes care of updating breakpoints to not reference
1791 PSPACE anymore. */
1792
1793void
1794breakpoint_program_space_exit (struct program_space *pspace)
1795{
1796 struct breakpoint *b, *b_temp;
876fa593 1797 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1798
1799 /* Remove any breakpoint that was set through this program space. */
1800 ALL_BREAKPOINTS_SAFE (b, b_temp)
1801 {
1802 if (b->pspace == pspace)
1803 delete_breakpoint (b);
1804 }
1805
1806 /* Breakpoints set through other program spaces could have locations
1807 bound to PSPACE as well. Remove those. */
876fa593 1808 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1809 {
1810 struct bp_location *tmp;
1811
1812 if (loc->pspace == pspace)
1813 {
2bdf28a0 1814 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1815 if (loc->owner->loc == loc)
1816 loc->owner->loc = loc->next;
1817 else
1818 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1819 if (tmp->next == loc)
1820 {
1821 tmp->next = loc->next;
1822 break;
1823 }
1824 }
1825 }
1826
1827 /* Now update the global location list to permanently delete the
1828 removed locations above. */
1829 update_global_location_list (0);
1830}
1831
74960c60
VP
1832/* Make sure all breakpoints are inserted in inferior.
1833 Throws exception on any error.
1834 A breakpoint that is already inserted won't be inserted
1835 again, so calling this function twice is safe. */
1836void
1837insert_breakpoints (void)
1838{
1839 struct breakpoint *bpt;
1840
1841 ALL_BREAKPOINTS (bpt)
1842 if (is_hardware_watchpoint (bpt))
4a64f543 1843 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1844
b60e7edf 1845 update_global_location_list (1);
74960c60 1846
c35b1492
PA
1847 /* update_global_location_list does not insert breakpoints when
1848 always_inserted_mode is not enabled. Explicitly insert them
1849 now. */
1850 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1851 insert_breakpoint_locations ();
1852}
1853
c906108c
SS
1854/* insert_breakpoints is used when starting or continuing the program.
1855 remove_breakpoints is used when the program stops.
1856 Both return zero if successful,
1857 or an `errno' value if could not write the inferior. */
1858
74960c60
VP
1859static void
1860insert_breakpoint_locations (void)
c906108c 1861{
a5606eee 1862 struct breakpoint *bpt;
35df4500 1863 struct bp_location *bl, **blp_tmp;
e236ba44 1864 int error = 0;
c906108c
SS
1865 int val = 0;
1866 int disabled_breaks = 0;
81d0cc19 1867 int hw_breakpoint_error = 0;
c906108c 1868
81d0cc19 1869 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1870 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1871
81d0cc19
GS
1872 /* Explicitly mark the warning -- this will only be printed if
1873 there was an error. */
1874 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1875
1876 save_current_space_and_thread ();
1877
35df4500 1878 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1879 {
35df4500 1880 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1881 continue;
1882
4a64f543
MS
1883 /* There is no point inserting thread-specific breakpoints if
1884 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1885 has BL->OWNER always non-NULL. */
35df4500
TJB
1886 if (bl->owner->thread != -1
1887 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1888 continue;
1889
35df4500 1890 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1891
1892 /* For targets that support global breakpoints, there's no need
1893 to select an inferior to insert breakpoint to. In fact, even
1894 if we aren't attached to any process yet, we should still
1895 insert breakpoints. */
1896 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1897 && ptid_equal (inferior_ptid, null_ptid))
1898 continue;
1899
35df4500 1900 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1901 &hw_breakpoint_error);
1902 if (val)
e236ba44 1903 error = val;
879bfdc2 1904 }
c906108c 1905
4a64f543
MS
1906 /* If we failed to insert all locations of a watchpoint, remove
1907 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1908 ALL_BREAKPOINTS (bpt)
1909 {
1910 int some_failed = 0;
1911 struct bp_location *loc;
1912
1913 if (!is_hardware_watchpoint (bpt))
1914 continue;
1915
d6b74ac4 1916 if (!breakpoint_enabled (bpt))
a5606eee 1917 continue;
74960c60
VP
1918
1919 if (bpt->disposition == disp_del_at_next_stop)
1920 continue;
a5606eee
VP
1921
1922 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1923 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1924 {
1925 some_failed = 1;
1926 break;
1927 }
1928 if (some_failed)
1929 {
1930 for (loc = bpt->loc; loc; loc = loc->next)
1931 if (loc->inserted)
1932 remove_breakpoint (loc, mark_uninserted);
1933
1934 hw_breakpoint_error = 1;
1935 fprintf_unfiltered (tmp_error_stream,
1936 "Could not insert hardware watchpoint %d.\n",
1937 bpt->number);
1938 error = -1;
1939 }
1940 }
1941
e236ba44 1942 if (error)
81d0cc19
GS
1943 {
1944 /* If a hardware breakpoint or watchpoint was inserted, add a
1945 message about possibly exhausted resources. */
879bfdc2 1946 if (hw_breakpoint_error)
81d0cc19 1947 {
c6510018
MS
1948 fprintf_unfiltered (tmp_error_stream,
1949 "Could not insert hardware breakpoints:\n\
1950You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1951 }
81d0cc19
GS
1952 target_terminal_ours_for_output ();
1953 error_stream (tmp_error_stream);
1954 }
f7545552
TT
1955
1956 do_cleanups (cleanups);
c906108c
SS
1957}
1958
c906108c 1959int
fba45db2 1960remove_breakpoints (void)
c906108c 1961{
35df4500 1962 struct bp_location *bl, **blp_tmp;
3a1bae8e 1963 int val = 0;
c906108c 1964
35df4500 1965 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1966 {
35df4500
TJB
1967 if (bl->inserted)
1968 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1969 }
3a1bae8e 1970 return val;
c906108c
SS
1971}
1972
6c95b8df
PA
1973/* Remove breakpoints of process PID. */
1974
1975int
1976remove_breakpoints_pid (int pid)
1977{
35df4500 1978 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1979 int val;
1980 struct inferior *inf = find_inferior_pid (pid);
1981
35df4500 1982 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 1983 {
35df4500 1984 if (bl->pspace != inf->pspace)
6c95b8df
PA
1985 continue;
1986
35df4500 1987 if (bl->inserted)
6c95b8df 1988 {
35df4500 1989 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
1990 if (val != 0)
1991 return val;
1992 }
1993 }
1994 return 0;
1995}
1996
692590c1 1997int
80ce1ecb 1998remove_hw_watchpoints (void)
692590c1 1999{
35df4500 2000 struct bp_location *bl, **blp_tmp;
3a1bae8e 2001 int val = 0;
692590c1 2002
35df4500 2003 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 2004 {
35df4500
TJB
2005 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2006 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 2007 }
3a1bae8e 2008 return val;
692590c1
MS
2009}
2010
c906108c 2011int
fba45db2 2012reattach_breakpoints (int pid)
c906108c 2013{
6c95b8df 2014 struct cleanup *old_chain;
35df4500 2015 struct bp_location *bl, **blp_tmp;
c906108c 2016 int val;
86b887df 2017 struct ui_file *tmp_error_stream;
fa3a767f 2018 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2019 struct inferior *inf;
2020 struct thread_info *tp;
2021
2022 tp = any_live_thread_of_process (pid);
2023 if (tp == NULL)
2024 return 1;
2025
2026 inf = find_inferior_pid (pid);
2027 old_chain = save_inferior_ptid ();
2028
2029 inferior_ptid = tp->ptid;
a4954f26 2030
86b887df 2031 tmp_error_stream = mem_fileopen ();
a4954f26 2032 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2033
35df4500 2034 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2035 {
35df4500 2036 if (bl->pspace != inf->pspace)
6c95b8df
PA
2037 continue;
2038
35df4500 2039 if (bl->inserted)
c5aa993b 2040 {
35df4500
TJB
2041 bl->inserted = 0;
2042 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2043 if (val != 0)
2044 {
ce696e05 2045 do_cleanups (old_chain);
c5aa993b
JM
2046 return val;
2047 }
2048 }
2049 }
ce696e05 2050 do_cleanups (old_chain);
c906108c
SS
2051 return 0;
2052}
2053
e58b0e63
PA
2054static int internal_breakpoint_number = -1;
2055
84f4c1fe
PM
2056/* Set the breakpoint number of B, depending on the value of INTERNAL.
2057 If INTERNAL is non-zero, the breakpoint number will be populated
2058 from internal_breakpoint_number and that variable decremented.
2059 Otherwis the breakpoint number will be populated from
2060 breakpoint_count and that value incremented. Internal breakpoints
2061 do not set the internal var bpnum. */
2062static void
2063set_breakpoint_number (int internal, struct breakpoint *b)
2064{
2065 if (internal)
2066 b->number = internal_breakpoint_number--;
2067 else
2068 {
2069 set_breakpoint_count (breakpoint_count + 1);
2070 b->number = breakpoint_count;
2071 }
2072}
2073
e62c965a 2074static struct breakpoint *
a6d9a66e
UW
2075create_internal_breakpoint (struct gdbarch *gdbarch,
2076 CORE_ADDR address, enum bptype type)
e62c965a 2077{
e62c965a
PP
2078 struct symtab_and_line sal;
2079 struct breakpoint *b;
2080
4a64f543 2081 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2082
2083 sal.pc = address;
2084 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2085 sal.pspace = current_program_space;
e62c965a 2086
a6d9a66e 2087 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2088 b->number = internal_breakpoint_number--;
2089 b->disposition = disp_donttouch;
2090
2091 return b;
2092}
2093
17450429
PP
2094static const char *const longjmp_names[] =
2095 {
2096 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2097 };
2098#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2099
2100/* Per-objfile data private to breakpoint.c. */
2101struct breakpoint_objfile_data
2102{
2103 /* Minimal symbol for "_ovly_debug_event" (if any). */
2104 struct minimal_symbol *overlay_msym;
2105
2106 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2107 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2108
2109 /* Minimal symbol for "std::terminate()" (if any). */
2110 struct minimal_symbol *terminate_msym;
2111
2112 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2113 struct minimal_symbol *exception_msym;
2114};
2115
2116static const struct objfile_data *breakpoint_objfile_key;
2117
2118/* Minimal symbol not found sentinel. */
2119static struct minimal_symbol msym_not_found;
2120
2121/* Returns TRUE if MSYM point to the "not found" sentinel. */
2122
2123static int
2124msym_not_found_p (const struct minimal_symbol *msym)
2125{
2126 return msym == &msym_not_found;
2127}
2128
2129/* Return per-objfile data needed by breakpoint.c.
2130 Allocate the data if necessary. */
2131
2132static struct breakpoint_objfile_data *
2133get_breakpoint_objfile_data (struct objfile *objfile)
2134{
2135 struct breakpoint_objfile_data *bp_objfile_data;
2136
2137 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2138 if (bp_objfile_data == NULL)
2139 {
2140 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2141 sizeof (*bp_objfile_data));
2142
2143 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2144 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2145 }
2146 return bp_objfile_data;
2147}
2148
e62c965a 2149static void
af02033e 2150create_overlay_event_breakpoint (void)
e62c965a 2151{
69de3c6a 2152 struct objfile *objfile;
af02033e 2153 const char *const func_name = "_ovly_debug_event";
e62c965a 2154
69de3c6a
PP
2155 ALL_OBJFILES (objfile)
2156 {
2157 struct breakpoint *b;
17450429
PP
2158 struct breakpoint_objfile_data *bp_objfile_data;
2159 CORE_ADDR addr;
69de3c6a 2160
17450429
PP
2161 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2162
2163 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2164 continue;
2165
2166 if (bp_objfile_data->overlay_msym == NULL)
2167 {
2168 struct minimal_symbol *m;
2169
2170 m = lookup_minimal_symbol_text (func_name, objfile);
2171 if (m == NULL)
2172 {
2173 /* Avoid future lookups in this objfile. */
2174 bp_objfile_data->overlay_msym = &msym_not_found;
2175 continue;
2176 }
2177 bp_objfile_data->overlay_msym = m;
2178 }
e62c965a 2179
17450429
PP
2180 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2181 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2182 bp_overlay_event);
2183 b->addr_string = xstrdup (func_name);
e62c965a 2184
69de3c6a
PP
2185 if (overlay_debugging == ovly_auto)
2186 {
2187 b->enable_state = bp_enabled;
2188 overlay_events_enabled = 1;
2189 }
2190 else
2191 {
2192 b->enable_state = bp_disabled;
2193 overlay_events_enabled = 0;
2194 }
e62c965a
PP
2195 }
2196 update_global_location_list (1);
2197}
2198
0fd8e87f 2199static void
af02033e 2200create_longjmp_master_breakpoint (void)
0fd8e87f 2201{
6c95b8df 2202 struct program_space *pspace;
6c95b8df
PA
2203 struct cleanup *old_chain;
2204
2205 old_chain = save_current_program_space ();
0fd8e87f 2206
6c95b8df 2207 ALL_PSPACES (pspace)
af02033e
PP
2208 {
2209 struct objfile *objfile;
2210
2211 set_current_program_space (pspace);
2212
2213 ALL_OBJFILES (objfile)
0fd8e87f 2214 {
af02033e
PP
2215 int i;
2216 struct gdbarch *gdbarch;
17450429 2217 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2218
af02033e
PP
2219 gdbarch = get_objfile_arch (objfile);
2220 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2221 continue;
2222
17450429
PP
2223 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2224
2225 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2226 {
2227 struct breakpoint *b;
af02033e 2228 const char *func_name;
17450429 2229 CORE_ADDR addr;
6c95b8df 2230
17450429 2231 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2232 continue;
0fd8e87f 2233
17450429
PP
2234 func_name = longjmp_names[i];
2235 if (bp_objfile_data->longjmp_msym[i] == NULL)
2236 {
2237 struct minimal_symbol *m;
2238
2239 m = lookup_minimal_symbol_text (func_name, objfile);
2240 if (m == NULL)
2241 {
2242 /* Prevent future lookups in this objfile. */
2243 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2244 continue;
2245 }
2246 bp_objfile_data->longjmp_msym[i] = m;
2247 }
2248
2249 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2250 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2251 b->addr_string = xstrdup (func_name);
2252 b->enable_state = bp_disabled;
2253 }
0fd8e87f 2254 }
af02033e 2255 }
0fd8e87f 2256 update_global_location_list (1);
6c95b8df
PA
2257
2258 do_cleanups (old_chain);
0fd8e87f
UW
2259}
2260
af02033e 2261/* Create a master std::terminate breakpoint. */
aa7d318d 2262static void
af02033e 2263create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2264{
2265 struct program_space *pspace;
aa7d318d 2266 struct cleanup *old_chain;
af02033e 2267 const char *const func_name = "std::terminate()";
aa7d318d
TT
2268
2269 old_chain = save_current_program_space ();
2270
2271 ALL_PSPACES (pspace)
17450429
PP
2272 {
2273 struct objfile *objfile;
2274 CORE_ADDR addr;
2275
2276 set_current_program_space (pspace);
2277
aa7d318d
TT
2278 ALL_OBJFILES (objfile)
2279 {
2280 struct breakpoint *b;
17450429 2281 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2282
17450429 2283 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2284
17450429
PP
2285 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2286 continue;
2287
2288 if (bp_objfile_data->terminate_msym == NULL)
2289 {
2290 struct minimal_symbol *m;
2291
2292 m = lookup_minimal_symbol (func_name, NULL, objfile);
2293 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2294 && MSYMBOL_TYPE (m) != mst_file_text))
2295 {
2296 /* Prevent future lookups in this objfile. */
2297 bp_objfile_data->terminate_msym = &msym_not_found;
2298 continue;
2299 }
2300 bp_objfile_data->terminate_msym = m;
2301 }
aa7d318d 2302
17450429
PP
2303 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2304 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2305 bp_std_terminate_master);
2306 b->addr_string = xstrdup (func_name);
2307 b->enable_state = bp_disabled;
2308 }
17450429
PP
2309 }
2310
aa7d318d
TT
2311 update_global_location_list (1);
2312
2313 do_cleanups (old_chain);
2314}
2315
186c406b
TT
2316/* Install a master breakpoint on the unwinder's debug hook. */
2317
2318void
2319create_exception_master_breakpoint (void)
2320{
2321 struct objfile *objfile;
17450429 2322 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2323
2324 ALL_OBJFILES (objfile)
2325 {
17450429
PP
2326 struct breakpoint *b;
2327 struct gdbarch *gdbarch;
2328 struct breakpoint_objfile_data *bp_objfile_data;
2329 CORE_ADDR addr;
2330
2331 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2332
2333 if (msym_not_found_p (bp_objfile_data->exception_msym))
2334 continue;
2335
2336 gdbarch = get_objfile_arch (objfile);
186c406b 2337
17450429 2338 if (bp_objfile_data->exception_msym == NULL)
186c406b 2339 {
17450429 2340 struct minimal_symbol *debug_hook;
186c406b 2341
17450429
PP
2342 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2343 if (debug_hook == NULL)
2344 {
2345 bp_objfile_data->exception_msym = &msym_not_found;
2346 continue;
2347 }
2348
2349 bp_objfile_data->exception_msym = debug_hook;
186c406b 2350 }
17450429
PP
2351
2352 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2353 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2354 &current_target);
2355 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2356 b->addr_string = xstrdup (func_name);
2357 b->enable_state = bp_disabled;
186c406b
TT
2358 }
2359
2360 update_global_location_list (1);
2361}
2362
c906108c 2363void
fba45db2 2364update_breakpoints_after_exec (void)
c906108c 2365{
35df4500 2366 struct breakpoint *b, *b_tmp;
876fa593 2367 struct bp_location *bploc, **bplocp_tmp;
c906108c 2368
25b22b0a
PA
2369 /* We're about to delete breakpoints from GDB's lists. If the
2370 INSERTED flag is true, GDB will try to lift the breakpoints by
2371 writing the breakpoints' "shadow contents" back into memory. The
2372 "shadow contents" are NOT valid after an exec, so GDB should not
2373 do that. Instead, the target is responsible from marking
2374 breakpoints out as soon as it detects an exec. We don't do that
2375 here instead, because there may be other attempts to delete
2376 breakpoints after detecting an exec and before reaching here. */
876fa593 2377 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2378 if (bploc->pspace == current_program_space)
2379 gdb_assert (!bploc->inserted);
c906108c 2380
35df4500 2381 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2382 {
6c95b8df
PA
2383 if (b->pspace != current_program_space)
2384 continue;
2385
4a64f543 2386 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2387 if (b->type == bp_shlib_event)
2388 {
2389 delete_breakpoint (b);
2390 continue;
2391 }
c906108c 2392
4a64f543 2393 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2394 if (b->type == bp_jit_event)
2395 {
2396 delete_breakpoint (b);
2397 continue;
2398 }
2399
1900040c 2400 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2401 as must overlay event and longjmp master breakpoints. */
2402 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2403 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2404 || b->type == bp_exception_master)
c4093a6a
JM
2405 {
2406 delete_breakpoint (b);
2407 continue;
2408 }
2409
4a64f543 2410 /* Step-resume breakpoints are meaningless after an exec(). */
c5aa993b
JM
2411 if (b->type == bp_step_resume)
2412 {
2413 delete_breakpoint (b);
2414 continue;
2415 }
2416
611c83ae
PA
2417 /* Longjmp and longjmp-resume breakpoints are also meaningless
2418 after an exec. */
186c406b
TT
2419 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2420 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2421 {
2422 delete_breakpoint (b);
2423 continue;
2424 }
2425
ce78b96d
JB
2426 if (b->type == bp_catchpoint)
2427 {
2428 /* For now, none of the bp_catchpoint breakpoints need to
2429 do anything at this point. In the future, if some of
2430 the catchpoints need to something, we will need to add
2431 a new method, and call this method from here. */
2432 continue;
2433 }
2434
c5aa993b
JM
2435 /* bp_finish is a special case. The only way we ought to be able
2436 to see one of these when an exec() has happened, is if the user
2437 caught a vfork, and then said "finish". Ordinarily a finish just
2438 carries them to the call-site of the current callee, by setting
2439 a temporary bp there and resuming. But in this case, the finish
2440 will carry them entirely through the vfork & exec.
2441
2442 We don't want to allow a bp_finish to remain inserted now. But
2443 we can't safely delete it, 'cause finish_command has a handle to
2444 the bp on a bpstat, and will later want to delete it. There's a
2445 chance (and I've seen it happen) that if we delete the bp_finish
2446 here, that its storage will get reused by the time finish_command
2447 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2448 We really must allow finish_command to delete a bp_finish.
2449
53a5351d
JM
2450 In the absense of a general solution for the "how do we know
2451 it's safe to delete something others may have handles to?"
2452 problem, what we'll do here is just uninsert the bp_finish, and
2453 let finish_command delete it.
2454
2455 (We know the bp_finish is "doomed" in the sense that it's
2456 momentary, and will be deleted as soon as finish_command sees
2457 the inferior stopped. So it doesn't matter that the bp's
2458 address is probably bogus in the new a.out, unlike e.g., the
2459 solib breakpoints.) */
c5aa993b 2460
c5aa993b
JM
2461 if (b->type == bp_finish)
2462 {
2463 continue;
2464 }
2465
2466 /* Without a symbolic address, we have little hope of the
2467 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2468 a.out. */
c5aa993b
JM
2469 if (b->addr_string == NULL)
2470 {
2471 delete_breakpoint (b);
2472 continue;
2473 }
c5aa993b 2474 }
1900040c 2475 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2476 create_overlay_event_breakpoint ();
2477 create_longjmp_master_breakpoint ();
2478 create_std_terminate_master_breakpoint ();
186c406b 2479 create_exception_master_breakpoint ();
c906108c
SS
2480}
2481
2482int
fba45db2 2483detach_breakpoints (int pid)
c906108c 2484{
35df4500 2485 struct bp_location *bl, **blp_tmp;
3a1bae8e 2486 int val = 0;
ce696e05 2487 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2488 struct inferior *inf = current_inferior ();
c5aa993b 2489
39f77062 2490 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2491 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2492
6c95b8df 2493 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2494 inferior_ptid = pid_to_ptid (pid);
35df4500 2495 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2496 {
35df4500 2497 if (bl->pspace != inf->pspace)
6c95b8df
PA
2498 continue;
2499
35df4500
TJB
2500 if (bl->inserted)
2501 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2502 }
d03285ec
UW
2503
2504 /* Detach single-step breakpoints as well. */
2505 detach_single_step_breakpoints ();
2506
ce696e05 2507 do_cleanups (old_chain);
3a1bae8e 2508 return val;
c906108c
SS
2509}
2510
35df4500 2511/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2512 Note that this is used to detach breakpoints from a child fork.
2513 When we get here, the child isn't in the inferior list, and neither
2514 do we have objects to represent its address space --- we should
35df4500 2515 *not* look at bl->pspace->aspace here. */
6c95b8df 2516
c906108c 2517static int
35df4500 2518remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2519{
2520 int val;
c5aa993b 2521
35df4500
TJB
2522 /* BL is never in moribund_locations by our callers. */
2523 gdb_assert (bl->owner != NULL);
2bdf28a0 2524
35df4500 2525 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2526 /* Permanent breakpoints cannot be inserted or removed. */
2527 return 0;
2528
74960c60
VP
2529 /* The type of none suggests that owner is actually deleted.
2530 This should not ever happen. */
35df4500 2531 gdb_assert (bl->owner->type != bp_none);
0bde7532 2532
35df4500
TJB
2533 if (bl->loc_type == bp_loc_software_breakpoint
2534 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2535 {
c02f5703
MS
2536 /* "Normal" instruction breakpoint: either the standard
2537 trap-instruction bp (bp_breakpoint), or a
2538 bp_hardware_breakpoint. */
2539
2540 /* First check to see if we have to handle an overlay. */
2541 if (overlay_debugging == ovly_off
35df4500
TJB
2542 || bl->section == NULL
2543 || !(section_is_overlay (bl->section)))
c02f5703
MS
2544 {
2545 /* No overlay handling: just remove the breakpoint. */
2546
35df4500
TJB
2547 if (bl->loc_type == bp_loc_hardware_breakpoint)
2548 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2549 else
35df4500 2550 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2551 }
c906108c
SS
2552 else
2553 {
4a64f543 2554 /* This breakpoint is in an overlay section.
c02f5703
MS
2555 Did we set a breakpoint at the LMA? */
2556 if (!overlay_events_enabled)
2557 {
2558 /* Yes -- overlay event support is not active, so we
2559 should have set a breakpoint at the LMA. Remove it.
2560 */
c02f5703
MS
2561 /* Ignore any failures: if the LMA is in ROM, we will
2562 have already warned when we failed to insert it. */
35df4500
TJB
2563 if (bl->loc_type == bp_loc_hardware_breakpoint)
2564 target_remove_hw_breakpoint (bl->gdbarch,
2565 &bl->overlay_target_info);
c02f5703 2566 else
35df4500
TJB
2567 target_remove_breakpoint (bl->gdbarch,
2568 &bl->overlay_target_info);
c02f5703
MS
2569 }
2570 /* Did we set a breakpoint at the VMA?
2571 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2572 if (bl->inserted)
c906108c 2573 {
c02f5703
MS
2574 /* Yes -- remove it. Previously we did not bother to
2575 remove the breakpoint if the section had been
2576 unmapped, but let's not rely on that being safe. We
2577 don't know what the overlay manager might do. */
35df4500
TJB
2578 if (bl->loc_type == bp_loc_hardware_breakpoint)
2579 val = target_remove_hw_breakpoint (bl->gdbarch,
2580 &bl->target_info);
aa67235e
UW
2581
2582 /* However, we should remove *software* breakpoints only
2583 if the section is still mapped, or else we overwrite
2584 wrong code with the saved shadow contents. */
35df4500
TJB
2585 else if (section_is_mapped (bl->section))
2586 val = target_remove_breakpoint (bl->gdbarch,
2587 &bl->target_info);
aa67235e
UW
2588 else
2589 val = 0;
c906108c 2590 }
c02f5703
MS
2591 else
2592 {
2593 /* No -- not inserted, so no need to remove. No error. */
2594 val = 0;
2595 }
c906108c 2596 }
879d1e6b
UW
2597
2598 /* In some cases, we might not be able to remove a breakpoint
2599 in a shared library that has already been removed, but we
2600 have not yet processed the shlib unload event. */
35df4500 2601 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2602 val = 0;
2603
c906108c
SS
2604 if (val)
2605 return val;
35df4500 2606 bl->inserted = (is == mark_inserted);
c906108c 2607 }
35df4500 2608 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2609 {
77b06cd7
TJB
2610 gdb_assert (bl->owner->ops != NULL
2611 && bl->owner->ops->remove_location != NULL);
2612
35df4500 2613 bl->inserted = (is == mark_inserted);
77b06cd7 2614 bl->owner->ops->remove_location (bl);
2e70b7b9 2615
c906108c 2616 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2617 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2618 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2619 bl->owner->number);
c906108c 2620 }
35df4500
TJB
2621 else if (bl->owner->type == bp_catchpoint
2622 && breakpoint_enabled (bl->owner)
2623 && !bl->duplicate)
ce78b96d 2624 {
77b06cd7
TJB
2625 gdb_assert (bl->owner->ops != NULL
2626 && bl->owner->ops->remove_location != NULL);
ce78b96d 2627
77b06cd7 2628 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2629 if (val)
2630 return val;
77b06cd7 2631
35df4500 2632 bl->inserted = (is == mark_inserted);
ce78b96d 2633 }
c906108c
SS
2634
2635 return 0;
2636}
2637
6c95b8df 2638static int
35df4500 2639remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2640{
2641 int ret;
2642 struct cleanup *old_chain;
2643
35df4500
TJB
2644 /* BL is never in moribund_locations by our callers. */
2645 gdb_assert (bl->owner != NULL);
2bdf28a0 2646
35df4500 2647 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2648 /* Permanent breakpoints cannot be inserted or removed. */
2649 return 0;
2650
2651 /* The type of none suggests that owner is actually deleted.
2652 This should not ever happen. */
35df4500 2653 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2654
2655 old_chain = save_current_space_and_thread ();
2656
35df4500 2657 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2658
35df4500 2659 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2660
2661 do_cleanups (old_chain);
2662 return ret;
2663}
2664
c906108c
SS
2665/* Clear the "inserted" flag in all breakpoints. */
2666
25b22b0a 2667void
fba45db2 2668mark_breakpoints_out (void)
c906108c 2669{
35df4500 2670 struct bp_location *bl, **blp_tmp;
c906108c 2671
35df4500
TJB
2672 ALL_BP_LOCATIONS (bl, blp_tmp)
2673 if (bl->pspace == current_program_space)
2674 bl->inserted = 0;
c906108c
SS
2675}
2676
53a5351d
JM
2677/* Clear the "inserted" flag in all breakpoints and delete any
2678 breakpoints which should go away between runs of the program.
c906108c
SS
2679
2680 Plus other such housekeeping that has to be done for breakpoints
2681 between runs.
2682
53a5351d
JM
2683 Note: this function gets called at the end of a run (by
2684 generic_mourn_inferior) and when a run begins (by
4a64f543 2685 init_wait_for_inferior). */
c906108c
SS
2686
2687
2688
2689void
fba45db2 2690breakpoint_init_inferior (enum inf_context context)
c906108c 2691{
35df4500
TJB
2692 struct breakpoint *b, *b_tmp;
2693 struct bp_location *bl, **blp_tmp;
1c5cfe86 2694 int ix;
6c95b8df 2695 struct program_space *pspace = current_program_space;
c906108c 2696
50c71eaf
PA
2697 /* If breakpoint locations are shared across processes, then there's
2698 nothing to do. */
2567c7d9 2699 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2700 return;
2701
35df4500 2702 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2703 {
35df4500
TJB
2704 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2705 if (bl->pspace == pspace
2706 && bl->owner->enable_state != bp_permanent)
2707 bl->inserted = 0;
6c95b8df 2708 }
075f6582 2709
35df4500 2710 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2711 {
6c95b8df
PA
2712 if (b->loc && b->loc->pspace != pspace)
2713 continue;
2714
c5aa993b
JM
2715 switch (b->type)
2716 {
2717 case bp_call_dummy:
c906108c 2718
c5aa993b 2719 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2720 cause problems when the inferior is rerun, so we better get
2721 rid of it. */
2722
2723 case bp_watchpoint_scope:
2724
2725 /* Also get rid of scope breakpoints. */
2726
2727 case bp_shlib_event:
2728
2729 /* Also remove solib event breakpoints. Their addresses may
2730 have changed since the last time we ran the program.
2731 Actually we may now be debugging against different target;
2732 and so the solib backend that installed this breakpoint may
2733 not be used in by the target. E.g.,
2734
2735 (gdb) file prog-linux
2736 (gdb) run # native linux target
2737 ...
2738 (gdb) kill
2739 (gdb) file prog-win.exe
2740 (gdb) tar rem :9999 # remote Windows gdbserver.
2741 */
c906108c 2742
c5aa993b
JM
2743 delete_breakpoint (b);
2744 break;
c906108c 2745
c5aa993b
JM
2746 case bp_watchpoint:
2747 case bp_hardware_watchpoint:
2748 case bp_read_watchpoint:
2749 case bp_access_watchpoint:
c906108c 2750
c5aa993b
JM
2751 /* Likewise for watchpoints on local expressions. */
2752 if (b->exp_valid_block != NULL)
2753 delete_breakpoint (b);
967af18d 2754 else if (context == inf_starting)
c860120c 2755 {
4a64f543
MS
2756 /* Reset val field to force reread of starting value in
2757 insert_breakpoints. */
c860120c
PM
2758 if (b->val)
2759 value_free (b->val);
2760 b->val = NULL;
fa4727a6 2761 b->val_valid = 0;
c860120c 2762 }
c5aa993b
JM
2763 break;
2764 default:
c5aa993b
JM
2765 break;
2766 }
2767 }
1c5cfe86
PA
2768
2769 /* Get rid of the moribund locations. */
35df4500
TJB
2770 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2771 decref_bp_location (&bl);
1c5cfe86 2772 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2773}
2774
6c95b8df
PA
2775/* These functions concern about actual breakpoints inserted in the
2776 target --- to e.g. check if we need to do decr_pc adjustment or if
2777 we need to hop over the bkpt --- so we check for address space
2778 match, not program space. */
2779
c2c6d25f
JM
2780/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2781 exists at PC. It returns ordinary_breakpoint_here if it's an
2782 ordinary breakpoint, or permanent_breakpoint_here if it's a
2783 permanent breakpoint.
2784 - When continuing from a location with an ordinary breakpoint, we
2785 actually single step once before calling insert_breakpoints.
2786 - When continuing from a localion with a permanent breakpoint, we
2787 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2788 the target, to advance the PC past the breakpoint. */
c906108c 2789
c2c6d25f 2790enum breakpoint_here
6c95b8df 2791breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2792{
35df4500 2793 struct bp_location *bl, **blp_tmp;
c2c6d25f 2794 int any_breakpoint_here = 0;
c906108c 2795
35df4500 2796 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2797 {
35df4500
TJB
2798 if (bl->loc_type != bp_loc_software_breakpoint
2799 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2800 continue;
2801
35df4500
TJB
2802 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2803 if ((breakpoint_enabled (bl->owner)
2804 || bl->owner->enable_state == bp_permanent)
2805 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2806 aspace, pc))
075f6582
DJ
2807 {
2808 if (overlay_debugging
35df4500
TJB
2809 && section_is_overlay (bl->section)
2810 && !section_is_mapped (bl->section))
075f6582 2811 continue; /* unmapped overlay -- can't be a match */
35df4500 2812 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2813 return permanent_breakpoint_here;
2814 else
2815 any_breakpoint_here = 1;
2816 }
2817 }
c906108c 2818
c2c6d25f 2819 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2820}
2821
1c5cfe86
PA
2822/* Return true if there's a moribund breakpoint at PC. */
2823
2824int
6c95b8df 2825moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2826{
2827 struct bp_location *loc;
2828 int ix;
2829
2830 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
6c95b8df
PA
2831 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2832 aspace, pc))
1c5cfe86
PA
2833 return 1;
2834
2835 return 0;
2836}
c2c6d25f 2837
c36b740a 2838/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2839 inserted using regular breakpoint_chain / bp_location array
2840 mechanism. This does not check for single-step breakpoints, which
2841 are inserted and removed using direct target manipulation. */
c906108c
SS
2842
2843int
4a64f543
MS
2844regular_breakpoint_inserted_here_p (struct address_space *aspace,
2845 CORE_ADDR pc)
c906108c 2846{
35df4500 2847 struct bp_location *bl, **blp_tmp;
c906108c 2848
35df4500 2849 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2850 {
35df4500
TJB
2851 if (bl->loc_type != bp_loc_software_breakpoint
2852 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2853 continue;
2854
35df4500
TJB
2855 if (bl->inserted
2856 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2857 aspace, pc))
075f6582
DJ
2858 {
2859 if (overlay_debugging
35df4500
TJB
2860 && section_is_overlay (bl->section)
2861 && !section_is_mapped (bl->section))
075f6582
DJ
2862 continue; /* unmapped overlay -- can't be a match */
2863 else
2864 return 1;
2865 }
c5aa993b 2866 }
c36b740a
VP
2867 return 0;
2868}
2869
2870/* Returns non-zero iff there's either regular breakpoint
2871 or a single step breakpoint inserted at PC. */
2872
2873int
6c95b8df 2874breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2875{
6c95b8df 2876 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2877 return 1;
c906108c 2878
6c95b8df 2879 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2880 return 1;
2881
c906108c
SS
2882 return 0;
2883}
2884
4fa8626c
DJ
2885/* This function returns non-zero iff there is a software breakpoint
2886 inserted at PC. */
2887
2888int
3e43a32a
MS
2889software_breakpoint_inserted_here_p (struct address_space *aspace,
2890 CORE_ADDR pc)
4fa8626c 2891{
35df4500 2892 struct bp_location *bl, **blp_tmp;
4fa8626c 2893
35df4500 2894 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2895 {
35df4500 2896 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2897 continue;
2898
35df4500
TJB
2899 if (bl->inserted
2900 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2901 aspace, pc))
4fa8626c
DJ
2902 {
2903 if (overlay_debugging
35df4500
TJB
2904 && section_is_overlay (bl->section)
2905 && !section_is_mapped (bl->section))
4fa8626c
DJ
2906 continue; /* unmapped overlay -- can't be a match */
2907 else
2908 return 1;
2909 }
2910 }
2911
1aafd4da 2912 /* Also check for software single-step breakpoints. */
6c95b8df 2913 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2914 return 1;
2915
4fa8626c
DJ
2916 return 0;
2917}
2918
9093389c
PA
2919int
2920hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2921 CORE_ADDR addr, ULONGEST len)
2922{
2923 struct breakpoint *bpt;
2924
2925 ALL_BREAKPOINTS (bpt)
2926 {
2927 struct bp_location *loc;
2928
2929 if (bpt->type != bp_hardware_watchpoint
2930 && bpt->type != bp_access_watchpoint)
2931 continue;
2932
2933 if (!breakpoint_enabled (bpt))
2934 continue;
2935
2936 for (loc = bpt->loc; loc; loc = loc->next)
2937 if (loc->pspace->aspace == aspace && loc->inserted)
2938 {
2939 CORE_ADDR l, h;
2940
2941 /* Check for intersection. */
2942 l = max (loc->address, addr);
2943 h = min (loc->address + loc->length, addr + len);
2944 if (l < h)
2945 return 1;
2946 }
2947 }
2948 return 0;
2949}
2950
075f6582
DJ
2951/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2952 PC is valid for process/thread PTID. */
c906108c
SS
2953
2954int
6c95b8df
PA
2955breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2956 ptid_t ptid)
c906108c 2957{
35df4500 2958 struct bp_location *bl, **blp_tmp;
4a306c9a 2959 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2960 int thread = -1;
4a306c9a 2961 int task = 0;
a6f1cd96 2962
35df4500 2963 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2964 {
35df4500
TJB
2965 if (bl->loc_type != bp_loc_software_breakpoint
2966 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2967 continue;
2968
35df4500
TJB
2969 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2970 if (!breakpoint_enabled (bl->owner)
2971 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2972 continue;
2973
35df4500 2974 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2975 aspace, pc))
a6f1cd96
JB
2976 continue;
2977
35df4500 2978 if (bl->owner->thread != -1)
075f6582 2979 {
a6f1cd96
JB
2980 /* This is a thread-specific breakpoint. Check that ptid
2981 matches that thread. If thread hasn't been computed yet,
2982 it is now time to do so. */
2983 if (thread == -1)
2984 thread = pid_to_thread_id (ptid);
35df4500 2985 if (bl->owner->thread != thread)
a6f1cd96 2986 continue;
075f6582 2987 }
a6f1cd96 2988
35df4500 2989 if (bl->owner->task != 0)
4a306c9a
JB
2990 {
2991 /* This is a task-specific breakpoint. Check that ptid
2992 matches that task. If task hasn't been computed yet,
2993 it is now time to do so. */
2994 if (task == 0)
2995 task = ada_get_task_number (ptid);
35df4500 2996 if (bl->owner->task != task)
4a306c9a
JB
2997 continue;
2998 }
2999
a6f1cd96 3000 if (overlay_debugging
35df4500
TJB
3001 && section_is_overlay (bl->section)
3002 && !section_is_mapped (bl->section))
a6f1cd96
JB
3003 continue; /* unmapped overlay -- can't be a match */
3004
3005 return 1;
c5aa993b 3006 }
c906108c
SS
3007
3008 return 0;
3009}
c906108c 3010\f
c5aa993b 3011
c906108c
SS
3012/* bpstat stuff. External routines' interfaces are documented
3013 in breakpoint.h. */
3014
3015int
fba45db2 3016ep_is_catchpoint (struct breakpoint *ep)
c906108c 3017{
533be4dd 3018 return (ep->type == bp_catchpoint);
c906108c
SS
3019}
3020
f431efe5
PA
3021/* Frees any storage that is part of a bpstat. Does not walk the
3022 'next' chain. */
3023
3024static void
198757a8
VP
3025bpstat_free (bpstat bs)
3026{
3027 if (bs->old_val != NULL)
3028 value_free (bs->old_val);
9add0f1b 3029 decref_counted_command_line (&bs->commands);
f431efe5 3030 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3031 xfree (bs);
3032}
3033
c906108c
SS
3034/* Clear a bpstat so that it says we are not at any breakpoint.
3035 Also free any storage that is part of a bpstat. */
3036
3037void
fba45db2 3038bpstat_clear (bpstat *bsp)
c906108c
SS
3039{
3040 bpstat p;
3041 bpstat q;
3042
3043 if (bsp == 0)
3044 return;
3045 p = *bsp;
3046 while (p != NULL)
3047 {
3048 q = p->next;
198757a8 3049 bpstat_free (p);
c906108c
SS
3050 p = q;
3051 }
3052 *bsp = NULL;
3053}
3054
3055/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3056 is part of the bpstat is copied as well. */
3057
3058bpstat
fba45db2 3059bpstat_copy (bpstat bs)
c906108c
SS
3060{
3061 bpstat p = NULL;
3062 bpstat tmp;
3063 bpstat retval = NULL;
3064
3065 if (bs == NULL)
3066 return bs;
3067
3068 for (; bs != NULL; bs = bs->next)
3069 {
3070 tmp = (bpstat) xmalloc (sizeof (*tmp));
3071 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3072 incref_counted_command_line (tmp->commands);
f431efe5 3073 incref_bp_location (tmp->bp_location_at);
31cc81e9 3074 if (bs->old_val != NULL)
3c3185ac
JK
3075 {
3076 tmp->old_val = value_copy (bs->old_val);
3077 release_value (tmp->old_val);
3078 }
31cc81e9 3079
c906108c
SS
3080 if (p == NULL)
3081 /* This is the first thing in the chain. */
3082 retval = tmp;
3083 else
3084 p->next = tmp;
3085 p = tmp;
3086 }
3087 p->next = NULL;
3088 return retval;
3089}
3090
4a64f543 3091/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3092
3093bpstat
fba45db2 3094bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3095{
c5aa993b
JM
3096 if (bsp == NULL)
3097 return NULL;
c906108c 3098
c5aa993b
JM
3099 for (; bsp != NULL; bsp = bsp->next)
3100 {
f431efe5 3101 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3102 return bsp;
3103 }
c906108c
SS
3104 return NULL;
3105}
3106
4a64f543
MS
3107/* Put in *NUM the breakpoint number of the first breakpoint we are
3108 stopped at. *BSP upon return is a bpstat which points to the
3109 remaining breakpoints stopped at (but which is not guaranteed to be
3110 good for anything but further calls to bpstat_num).
3111
8671a17b
PA
3112 Return 0 if passed a bpstat which does not indicate any breakpoints.
3113 Return -1 if stopped at a breakpoint that has been deleted since
3114 we set it.
3115 Return 1 otherwise. */
c906108c
SS
3116
3117int
8671a17b 3118bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3119{
3120 struct breakpoint *b;
3121
3122 if ((*bsp) == NULL)
3123 return 0; /* No more breakpoint values */
8671a17b 3124
4a64f543
MS
3125 /* We assume we'll never have several bpstats that correspond to a
3126 single breakpoint -- otherwise, this function might return the
3127 same number more than once and this will look ugly. */
f431efe5 3128 b = (*bsp)->breakpoint_at;
8671a17b
PA
3129 *bsp = (*bsp)->next;
3130 if (b == NULL)
3131 return -1; /* breakpoint that's been deleted since */
3132
3133 *num = b->number; /* We have its number */
3134 return 1;
c906108c
SS
3135}
3136
3137/* Modify BS so that the actions will not be performed. */
3138
3139void
fba45db2 3140bpstat_clear_actions (bpstat bs)
c906108c
SS
3141{
3142 for (; bs != NULL; bs = bs->next)
3143 {
9add0f1b 3144 decref_counted_command_line (&bs->commands);
dde2d684 3145 bs->commands_left = NULL;
c906108c
SS
3146 if (bs->old_val != NULL)
3147 {
3148 value_free (bs->old_val);
3149 bs->old_val = NULL;
3150 }
3151 }
3152}
3153
f3b1572e
PA
3154/* Called when a command is about to proceed the inferior. */
3155
3156static void
3157breakpoint_about_to_proceed (void)
3158{
3159 if (!ptid_equal (inferior_ptid, null_ptid))
3160 {
3161 struct thread_info *tp = inferior_thread ();
3162
3163 /* Allow inferior function calls in breakpoint commands to not
3164 interrupt the command list. When the call finishes
3165 successfully, the inferior will be standing at the same
3166 breakpoint as if nothing happened. */
16c381f0 3167 if (tp->control.in_infcall)
f3b1572e
PA
3168 return;
3169 }
3170
3171 breakpoint_proceeded = 1;
3172}
3173
4a64f543
MS
3174/* Stub for cleaning up our state if we error-out of a breakpoint
3175 command. */
c906108c 3176static void
4efb68b1 3177cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3178{
3179 executing_breakpoint_commands = 0;
3180}
3181
4a64f543
MS
3182/* Execute all the commands associated with all the breakpoints at
3183 this location. Any of these commands could cause the process to
3184 proceed beyond this point, etc. We look out for such changes by
3185 checking the global "breakpoint_proceeded" after each command.
c906108c 3186
347bddb7
PA
3187 Returns true if a breakpoint command resumed the inferior. In that
3188 case, it is the caller's responsibility to recall it again with the
3189 bpstat of the current thread. */
3190
3191static int
3192bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3193{
3194 bpstat bs;
3195 struct cleanup *old_chain;
347bddb7 3196 int again = 0;
c906108c
SS
3197
3198 /* Avoid endless recursion if a `source' command is contained
3199 in bs->commands. */
3200 if (executing_breakpoint_commands)
347bddb7 3201 return 0;
c906108c
SS
3202
3203 executing_breakpoint_commands = 1;
3204 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3205
4a64f543 3206 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3207 bs = *bsp;
3208
3209 breakpoint_proceeded = 0;
3210 for (; bs != NULL; bs = bs->next)
3211 {
9add0f1b 3212 struct counted_command_line *ccmd;
6c50ab1c
JB
3213 struct command_line *cmd;
3214 struct cleanup *this_cmd_tree_chain;
3215
3216 /* Take ownership of the BSP's command tree, if it has one.
3217
3218 The command tree could legitimately contain commands like
3219 'step' and 'next', which call clear_proceed_status, which
3220 frees stop_bpstat's command tree. To make sure this doesn't
3221 free the tree we're executing out from under us, we need to
3222 take ownership of the tree ourselves. Since a given bpstat's
3223 commands are only executed once, we don't need to copy it; we
3224 can clear the pointer in the bpstat, and make sure we free
3225 the tree when we're done. */
9add0f1b
TT
3226 ccmd = bs->commands;
3227 bs->commands = NULL;
3228 this_cmd_tree_chain
3229 = make_cleanup_decref_counted_command_line (&ccmd);
3230 cmd = bs->commands_left;
3231 bs->commands_left = NULL;
6c50ab1c 3232
c906108c
SS
3233 while (cmd != NULL)
3234 {
3235 execute_control_command (cmd);
3236
3237 if (breakpoint_proceeded)
3238 break;
3239 else
3240 cmd = cmd->next;
3241 }
6c50ab1c
JB
3242
3243 /* We can free this command tree now. */
3244 do_cleanups (this_cmd_tree_chain);
3245
c906108c 3246 if (breakpoint_proceeded)
32c1e744
VP
3247 {
3248 if (target_can_async_p ())
347bddb7
PA
3249 /* If we are in async mode, then the target might be still
3250 running, not stopped at any breakpoint, so nothing for
3251 us to do here -- just return to the event loop. */
3252 ;
32c1e744
VP
3253 else
3254 /* In sync mode, when execute_control_command returns
3255 we're already standing on the next breakpoint.
347bddb7
PA
3256 Breakpoint commands for that stop were not run, since
3257 execute_command does not run breakpoint commands --
3258 only command_line_handler does, but that one is not
3259 involved in execution of breakpoint commands. So, we
3260 can now execute breakpoint commands. It should be
3261 noted that making execute_command do bpstat actions is
3262 not an option -- in this case we'll have recursive
3263 invocation of bpstat for each breakpoint with a
3264 command, and can easily blow up GDB stack. Instead, we
3265 return true, which will trigger the caller to recall us
3266 with the new stop_bpstat. */
3267 again = 1;
3268 break;
32c1e744 3269 }
c906108c 3270 }
c2b8ed2c 3271 do_cleanups (old_chain);
347bddb7
PA
3272 return again;
3273}
3274
3275void
3276bpstat_do_actions (void)
3277{
3278 /* Do any commands attached to breakpoint we are stopped at. */
3279 while (!ptid_equal (inferior_ptid, null_ptid)
3280 && target_has_execution
3281 && !is_exited (inferior_ptid)
3282 && !is_executing (inferior_ptid))
3283 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3284 and only return when it is stopped at the next breakpoint, we
3285 keep doing breakpoint actions until it returns false to
3286 indicate the inferior was not resumed. */
16c381f0 3287 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3288 break;
c906108c
SS
3289}
3290
fa4727a6
DJ
3291/* Print out the (old or new) value associated with a watchpoint. */
3292
3293static void
3294watchpoint_value_print (struct value *val, struct ui_file *stream)
3295{
3296 if (val == NULL)
3297 fprintf_unfiltered (stream, _("<unreadable>"));
3298 else
79a45b7d
TT
3299 {
3300 struct value_print_options opts;
3301 get_user_print_options (&opts);
3302 value_print (val, stream, &opts);
3303 }
fa4727a6
DJ
3304}
3305
e514a9d6 3306/* This is the normal print function for a bpstat. In the future,
c906108c 3307 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3308 by having it set different print_it values.
3309
3310 Current scheme: When we stop, bpstat_print() is called. It loops
3311 through the bpstat list of things causing this stop, calling the
4a64f543 3312 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3313 print_bp_stop_message function depends on the print_it field of
4a64f543 3314 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3315
3316 Return values from this routine (ultimately used by bpstat_print()
3317 and normal_stop() to decide what to do):
3318 PRINT_NOTHING: Means we already printed all we needed to print,
3319 don't print anything else.
3320 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3321 that something to be followed by a location.
3322 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3323 that something to be followed by a location.
3324 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3325 analysis. */
c906108c 3326
917317f4 3327static enum print_stop_action
fba45db2 3328print_it_typical (bpstat bs)
c906108c 3329{
f7545552 3330 struct cleanup *old_chain;
4f8d1dc6 3331 struct breakpoint *b;
89f9893c 3332 const struct bp_location *bl;
8b93c638 3333 struct ui_stream *stb;
f7545552
TT
3334 int bp_temp = 0;
3335 enum print_stop_action result;
3336
f431efe5
PA
3337 gdb_assert (bs->bp_location_at != NULL);
3338
3339 bl = bs->bp_location_at;
3340 b = bs->breakpoint_at;
c906108c 3341
f7545552
TT
3342 stb = ui_out_stream_new (uiout);
3343 old_chain = make_cleanup_ui_out_stream_delete (stb);
3344
4f8d1dc6 3345 switch (b->type)
c906108c 3346 {
e514a9d6
JM
3347 case bp_breakpoint:
3348 case bp_hardware_breakpoint:
f431efe5 3349 bp_temp = b->disposition == disp_del;
0d381245
VP
3350 if (bl->address != bl->requested_address)
3351 breakpoint_adjustment_warning (bl->requested_address,
3352 bl->address,
4f8d1dc6
VP
3353 b->number, 1);
3354 annotate_breakpoint (b->number);
2cec12e5
AR
3355 if (bp_temp)
3356 ui_out_text (uiout, "\nTemporary breakpoint ");
3357 else
3358 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3359 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3360 {
3361 ui_out_field_string (uiout, "reason",
3362 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3363 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3364 }
4f8d1dc6 3365 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3366 ui_out_text (uiout, ", ");
f7545552 3367 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3368 break;
3369
3370 case bp_shlib_event:
917317f4
JM
3371 /* Did we stop because the user set the stop_on_solib_events
3372 variable? (If so, we report this as a generic, "Stopped due
3373 to shlib event" message.) */
a3f17187 3374 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3375 result = PRINT_NOTHING;
e514a9d6
JM
3376 break;
3377
c4093a6a 3378 case bp_thread_event:
4a64f543 3379 /* Not sure how we will get here.
c4093a6a 3380 GDB should not stop for these breakpoints. */
a3f17187 3381 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3382 result = PRINT_NOTHING;
c4093a6a
JM
3383 break;
3384
1900040c 3385 case bp_overlay_event:
4a64f543 3386 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3387 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3388 result = PRINT_NOTHING;
1900040c
MS
3389 break;
3390
0fd8e87f
UW
3391 case bp_longjmp_master:
3392 /* These should never be enabled. */
3393 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3394 result = PRINT_NOTHING;
3395 break;
3396
aa7d318d
TT
3397 case bp_std_terminate_master:
3398 /* These should never be enabled. */
3e43a32a
MS
3399 printf_filtered (_("std::terminate Master Breakpoint: "
3400 "gdb should not stop!\n"));
aa7d318d
TT
3401 result = PRINT_NOTHING;
3402 break;
3403
186c406b
TT
3404 case bp_exception_master:
3405 /* These should never be enabled. */
3e43a32a
MS
3406 printf_filtered (_("Exception Master Breakpoint: "
3407 "gdb should not stop!\n"));
186c406b
TT
3408 result = PRINT_NOTHING;
3409 break;
3410
e514a9d6
JM
3411 case bp_watchpoint:
3412 case bp_hardware_watchpoint:
fa4727a6
DJ
3413 annotate_watchpoint (b->number);
3414 if (ui_out_is_mi_like_p (uiout))
3415 ui_out_field_string
3416 (uiout, "reason",
3417 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3418 mention (b);
f7545552 3419 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3420 ui_out_text (uiout, "\nOld value = ");
3421 watchpoint_value_print (bs->old_val, stb->stream);
3422 ui_out_field_stream (uiout, "old", stb);
3423 ui_out_text (uiout, "\nNew value = ");
3424 watchpoint_value_print (b->val, stb->stream);
3425 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3426 ui_out_text (uiout, "\n");
e514a9d6 3427 /* More than one watchpoint may have been triggered. */
f7545552 3428 result = PRINT_UNKNOWN;
e514a9d6
JM
3429 break;
3430
3431 case bp_read_watchpoint:
9dc5e2a9 3432 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3433 ui_out_field_string
3434 (uiout, "reason",
3435 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3436 mention (b);
f7545552 3437 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3438 ui_out_text (uiout, "\nValue = ");
fa4727a6 3439 watchpoint_value_print (b->val, stb->stream);
8b93c638 3440 ui_out_field_stream (uiout, "value", stb);
8b93c638 3441 ui_out_text (uiout, "\n");
f7545552 3442 result = PRINT_UNKNOWN;
e514a9d6
JM
3443 break;
3444
3445 case bp_access_watchpoint:
fa4727a6 3446 if (bs->old_val != NULL)
8b93c638 3447 {
4f8d1dc6 3448 annotate_watchpoint (b->number);
9dc5e2a9 3449 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3450 ui_out_field_string
3451 (uiout, "reason",
3452 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3453 mention (b);
f7545552 3454 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3455 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3456 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3457 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3458 ui_out_text (uiout, "\nNew value = ");
3459 }
3460 else
3461 {
4f8d1dc6 3462 mention (b);
9dc5e2a9 3463 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3464 ui_out_field_string
3465 (uiout, "reason",
3466 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3467 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3468 ui_out_text (uiout, "\nValue = ");
3469 }
fa4727a6 3470 watchpoint_value_print (b->val, stb->stream);
8b93c638 3471 ui_out_field_stream (uiout, "new", stb);
8b93c638 3472 ui_out_text (uiout, "\n");
f7545552 3473 result = PRINT_UNKNOWN;
e514a9d6 3474 break;
4ce44c66 3475
e514a9d6 3476 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3477 here. */
e514a9d6 3478
11cf8741 3479 case bp_finish:
9dc5e2a9 3480 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3481 ui_out_field_string
3482 (uiout, "reason",
3483 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3484 result = PRINT_UNKNOWN;
8b93c638
JM
3485 break;
3486
e514a9d6 3487 case bp_until:
9dc5e2a9 3488 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3489 ui_out_field_string
3490 (uiout, "reason",
3491 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3492 result = PRINT_UNKNOWN;
8b93c638
JM
3493 break;
3494
c2d11a7d 3495 case bp_none:
e514a9d6
JM
3496 case bp_longjmp:
3497 case bp_longjmp_resume:
186c406b
TT
3498 case bp_exception:
3499 case bp_exception_resume:
e514a9d6 3500 case bp_step_resume:
e514a9d6
JM
3501 case bp_watchpoint_scope:
3502 case bp_call_dummy:
aa7d318d 3503 case bp_std_terminate:
1042e4c0 3504 case bp_tracepoint:
7a697b8d 3505 case bp_fast_tracepoint:
4efc6507 3506 case bp_jit_event:
0e30163f
JK
3507 case bp_gnu_ifunc_resolver:
3508 case bp_gnu_ifunc_resolver_return:
e514a9d6 3509 default:
f7545552
TT
3510 result = PRINT_UNKNOWN;
3511 break;
e514a9d6 3512 }
f7545552
TT
3513
3514 do_cleanups (old_chain);
3515 return result;
e514a9d6
JM
3516}
3517
3518/* Generic routine for printing messages indicating why we
4a64f543 3519 stopped. The behavior of this function depends on the value
e514a9d6
JM
3520 'print_it' in the bpstat structure. Under some circumstances we
3521 may decide not to print anything here and delegate the task to
4a64f543 3522 normal_stop(). */
e514a9d6
JM
3523
3524static enum print_stop_action
3525print_bp_stop_message (bpstat bs)
3526{
3527 switch (bs->print_it)
3528 {
3529 case print_it_noop:
4a64f543 3530 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3531 return PRINT_UNKNOWN;
3532 break;
3533
3534 case print_it_done:
3535 /* We still want to print the frame, but we already printed the
4a64f543 3536 relevant messages. */
e514a9d6
JM
3537 return PRINT_SRC_AND_LOC;
3538 break;
3539
3540 case print_it_normal:
4f8d1dc6 3541 {
f431efe5
PA
3542 struct breakpoint *b = bs->breakpoint_at;
3543
1a6a67de
TJB
3544 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3545 which has since been deleted. */
3546 if (b == NULL)
3547 return PRINT_UNKNOWN;
3548
4f8d1dc6
VP
3549 /* Normal case. Call the breakpoint's print_it method, or
3550 print_it_typical. */
1a6a67de 3551 if (b->ops != NULL && b->ops->print_it != NULL)
4f8d1dc6
VP
3552 return b->ops->print_it (b);
3553 else
3554 return print_it_typical (bs);
3555 }
3556 break;
3086aeae 3557
e514a9d6 3558 default:
8e65ff28 3559 internal_error (__FILE__, __LINE__,
e2e0b3e5 3560 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3561 break;
c906108c 3562 }
c906108c
SS
3563}
3564
e514a9d6
JM
3565/* Print a message indicating what happened. This is called from
3566 normal_stop(). The input to this routine is the head of the bpstat
3567 list - a list of the eventpoints that caused this stop. This
3568 routine calls the generic print routine for printing a message
3569 about reasons for stopping. This will print (for example) the
3570 "Breakpoint n," part of the output. The return value of this
3571 routine is one of:
c906108c 3572
4a64f543 3573 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3574 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3575 code to print the location. An example is
c5aa993b
JM
3576 "Breakpoint 1, " which should be followed by
3577 the location.
917317f4 3578 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3579 to also print the location part of the message.
3580 An example is the catch/throw messages, which
4a64f543 3581 don't require a location appended to the end.
917317f4 3582 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3583 further info to be printed. */
c906108c 3584
917317f4 3585enum print_stop_action
fba45db2 3586bpstat_print (bpstat bs)
c906108c
SS
3587{
3588 int val;
c5aa993b 3589
c906108c 3590 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3591 (Currently all watchpoints go on the bpstat whether hit or not.
3592 That probably could (should) be changed, provided care is taken
c906108c 3593 with respect to bpstat_explains_signal). */
e514a9d6
JM
3594 for (; bs; bs = bs->next)
3595 {
3596 val = print_bp_stop_message (bs);
3597 if (val == PRINT_SRC_ONLY
3598 || val == PRINT_SRC_AND_LOC
3599 || val == PRINT_NOTHING)
3600 return val;
3601 }
c906108c 3602
e514a9d6 3603 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3604 with and nothing was printed. */
917317f4 3605 return PRINT_UNKNOWN;
c906108c
SS
3606}
3607
4a64f543
MS
3608/* Evaluate the expression EXP and return 1 if value is zero. This is
3609 used inside a catch_errors to evaluate the breakpoint condition.
3610 The argument is a "struct expression *" that has been cast to a
3611 "char *" to make it pass through catch_errors. */
c906108c
SS
3612
3613static int
4efb68b1 3614breakpoint_cond_eval (void *exp)
c906108c 3615{
278cd55f 3616 struct value *mark = value_mark ();
c5aa993b 3617 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3618
c906108c
SS
3619 value_free_to_mark (mark);
3620 return i;
3621}
3622
5760d0ab 3623/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3624
3625static bpstat
5760d0ab 3626bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3627{
3628 bpstat bs;
3629
3630 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3631 bs->next = NULL;
3632 **bs_link_pointer = bs;
3633 *bs_link_pointer = &bs->next;
f431efe5
PA
3634 bs->breakpoint_at = bl->owner;
3635 bs->bp_location_at = bl;
3636 incref_bp_location (bl);
c906108c
SS
3637 /* If the condition is false, etc., don't do the commands. */
3638 bs->commands = NULL;
9add0f1b 3639 bs->commands_left = NULL;
c906108c
SS
3640 bs->old_val = NULL;
3641 bs->print_it = print_it_normal;
3642 return bs;
3643}
3644\f
d983da9c
DJ
3645/* The target has stopped with waitstatus WS. Check if any hardware
3646 watchpoints have triggered, according to the target. */
3647
3648int
3649watchpoints_triggered (struct target_waitstatus *ws)
3650{
d92524f1 3651 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3652 CORE_ADDR addr;
3653 struct breakpoint *b;
3654
3655 if (!stopped_by_watchpoint)
3656 {
3657 /* We were not stopped by a watchpoint. Mark all watchpoints
3658 as not triggered. */
3659 ALL_BREAKPOINTS (b)
cc60f2e3 3660 if (is_hardware_watchpoint (b))
d983da9c
DJ
3661 b->watchpoint_triggered = watch_triggered_no;
3662
3663 return 0;
3664 }
3665
3666 if (!target_stopped_data_address (&current_target, &addr))
3667 {
3668 /* We were stopped by a watchpoint, but we don't know where.
3669 Mark all watchpoints as unknown. */
3670 ALL_BREAKPOINTS (b)
cc60f2e3 3671 if (is_hardware_watchpoint (b))
d983da9c
DJ
3672 b->watchpoint_triggered = watch_triggered_unknown;
3673
3674 return stopped_by_watchpoint;
3675 }
3676
3677 /* The target could report the data address. Mark watchpoints
3678 affected by this data address as triggered, and all others as not
3679 triggered. */
3680
3681 ALL_BREAKPOINTS (b)
cc60f2e3 3682 if (is_hardware_watchpoint (b))
d983da9c 3683 {
a5606eee 3684 struct bp_location *loc;
d983da9c
DJ
3685
3686 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3687 for (loc = b->loc; loc; loc = loc->next)
3688 /* Exact match not required. Within range is
3689 sufficient. */
5009afc5
AS
3690 if (target_watchpoint_addr_within_range (&current_target,
3691 addr, loc->address,
3692 loc->length))
a5606eee
VP
3693 {
3694 b->watchpoint_triggered = watch_triggered_yes;
3695 break;
3696 }
d983da9c
DJ
3697 }
3698
3699 return 1;
3700}
3701
c906108c
SS
3702/* Possible return values for watchpoint_check (this can't be an enum
3703 because of check_errors). */
3704/* The watchpoint has been deleted. */
3705#define WP_DELETED 1
3706/* The value has changed. */
3707#define WP_VALUE_CHANGED 2
3708/* The value has not changed. */
3709#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3710/* Ignore this watchpoint, no matter if the value changed or not. */
3711#define WP_IGNORE 4
c906108c
SS
3712
3713#define BP_TEMPFLAG 1
3714#define BP_HARDWAREFLAG 2
3715
4a64f543
MS
3716/* Evaluate watchpoint condition expression and check if its value
3717 changed.
553e4c11
JB
3718
3719 P should be a pointer to struct bpstat, but is defined as a void *
3720 in order for this function to be usable with catch_errors. */
c906108c
SS
3721
3722static int
4efb68b1 3723watchpoint_check (void *p)
c906108c
SS
3724{
3725 bpstat bs = (bpstat) p;
3726 struct breakpoint *b;
3727 struct frame_info *fr;
3728 int within_current_scope;
3729
f431efe5 3730 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3731 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3732 b = bs->breakpoint_at;
c906108c 3733
d0fb5eae
JK
3734 gdb_assert (is_watchpoint (b));
3735
f6bc2008
PA
3736 /* If this is a local watchpoint, we only want to check if the
3737 watchpoint frame is in scope if the current thread is the thread
3738 that was used to create the watchpoint. */
3739 if (!watchpoint_in_thread_scope (b))
60e1c644 3740 return WP_IGNORE;
f6bc2008 3741
c906108c
SS
3742 if (b->exp_valid_block == NULL)
3743 within_current_scope = 1;
3744 else
3745 {
edb3359d
DJ
3746 struct frame_info *frame = get_current_frame ();
3747 struct gdbarch *frame_arch = get_frame_arch (frame);
3748 CORE_ADDR frame_pc = get_frame_pc (frame);
3749
4a64f543
MS
3750 /* in_function_epilogue_p() returns a non-zero value if we're
3751 still in the function but the stack frame has already been
3752 invalidated. Since we can't rely on the values of local
3753 variables after the stack has been destroyed, we are treating
3754 the watchpoint in that state as `not changed' without further
3755 checking. Don't mark watchpoints as changed if the current
3756 frame is in an epilogue - even if they are in some other
3757 frame, our view of the stack is likely to be wrong and
3758 frame_find_by_id could error out. */
a0f49112 3759 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3760 return WP_IGNORE;
a0f49112 3761
101dcfbe 3762 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3763 within_current_scope = (fr != NULL);
69fbadd5
DJ
3764
3765 /* If we've gotten confused in the unwinder, we might have
3766 returned a frame that can't describe this variable. */
edb3359d
DJ
3767 if (within_current_scope)
3768 {
3769 struct symbol *function;
3770
3771 function = get_frame_function (fr);
3772 if (function == NULL
3773 || !contained_in (b->exp_valid_block,
3774 SYMBOL_BLOCK_VALUE (function)))
3775 within_current_scope = 0;
3776 }
69fbadd5 3777
edb3359d 3778 if (within_current_scope)
c906108c
SS
3779 /* If we end up stopping, the current frame will get selected
3780 in normal_stop. So this call to select_frame won't affect
3781 the user. */
0f7d239c 3782 select_frame (fr);
c906108c 3783 }
c5aa993b 3784
c906108c
SS
3785 if (within_current_scope)
3786 {
4a64f543
MS
3787 /* We use value_{,free_to_}mark because it could be a *long*
3788 time before we return to the command level and call
3789 free_all_values. We can't call free_all_values because we
3790 might be in the middle of evaluating a function call. */
c906108c 3791
0cf6dd15 3792 int pc = 0;
278cd55f 3793 struct value *mark = value_mark ();
fa4727a6
DJ
3794 struct value *new_val;
3795
0cf6dd15 3796 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3797
4a64f543
MS
3798 /* We use value_equal_contents instead of value_equal because
3799 the latter coerces an array to a pointer, thus comparing just
3800 the address of the array instead of its contents. This is
3801 not what we want. */
fa4727a6 3802 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3803 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3804 {
fa4727a6
DJ
3805 if (new_val != NULL)
3806 {
3807 release_value (new_val);
3808 value_free_to_mark (mark);
3809 }
c906108c
SS
3810 bs->old_val = b->val;
3811 b->val = new_val;
fa4727a6 3812 b->val_valid = 1;
c906108c
SS
3813 return WP_VALUE_CHANGED;
3814 }
3815 else
3816 {
60e1c644 3817 /* Nothing changed. */
c906108c 3818 value_free_to_mark (mark);
c906108c
SS
3819 return WP_VALUE_NOT_CHANGED;
3820 }
3821 }
3822 else
3823 {
3824 /* This seems like the only logical thing to do because
c5aa993b
JM
3825 if we temporarily ignored the watchpoint, then when
3826 we reenter the block in which it is valid it contains
3827 garbage (in the case of a function, it may have two
3828 garbage values, one before and one after the prologue).
3829 So we can't even detect the first assignment to it and
3830 watch after that (since the garbage may or may not equal
3831 the first value assigned). */
4ce44c66
JM
3832 /* We print all the stop information in print_it_typical(), but
3833 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3834 will be deleted already. So we have no choice but print the
3835 information here. */
9dc5e2a9 3836 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3837 ui_out_field_string
3838 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3839 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3840 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3841 ui_out_text (uiout,
3842 " deleted because the program has left the block in\n\
8b93c638 3843which its expression is valid.\n");
4ce44c66 3844
d0fb5eae 3845 watchpoint_del_at_next_stop (b);
c906108c
SS
3846
3847 return WP_DELETED;
3848 }
3849}
3850
18a18393
VP
3851/* Return true if it looks like target has stopped due to hitting
3852 breakpoint location BL. This function does not check if we
3853 should stop, only if BL explains the stop. */
3854static int
6c95b8df
PA
3855bpstat_check_location (const struct bp_location *bl,
3856 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3857{
3858 struct breakpoint *b = bl->owner;
3859
2bdf28a0
JK
3860 /* BL is from existing struct breakpoint. */
3861 gdb_assert (b != NULL);
3862
e8595ef6
SS
3863 /* By definition, the inferior does not report stops at
3864 tracepoints. */
d77f58be 3865 if (is_tracepoint (b))
e8595ef6
SS
3866 return 0;
3867
cc60f2e3 3868 if (!is_watchpoint (b)
18a18393 3869 && b->type != bp_hardware_breakpoint
fe798b75 3870 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3871 {
6c95b8df
PA
3872 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3873 aspace, bp_addr))
18a18393
VP
3874 return 0;
3875 if (overlay_debugging /* unmapped overlay section */
35df4500 3876 && section_is_overlay (bl->section)
18a18393
VP
3877 && !section_is_mapped (bl->section))
3878 return 0;
3879 }
cc60f2e3 3880
18a18393
VP
3881 /* Continuable hardware watchpoints are treated as non-existent if the
3882 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3883 some data address). Otherwise gdb won't stop on a break instruction
3884 in the code (not from a breakpoint) when a hardware watchpoint has
3885 been defined. Also skip watchpoints which we know did not trigger
3886 (did not match the data address). */
cc60f2e3
PA
3887
3888 if (is_hardware_watchpoint (b)
18a18393
VP
3889 && b->watchpoint_triggered == watch_triggered_no)
3890 return 0;
3891
3892 if (b->type == bp_hardware_breakpoint)
3893 {
3894 if (bl->address != bp_addr)
3895 return 0;
3896 if (overlay_debugging /* unmapped overlay section */
35df4500 3897 && section_is_overlay (bl->section)
18a18393
VP
3898 && !section_is_mapped (bl->section))
3899 return 0;
3900 }
ce78b96d 3901
ce78b96d
JB
3902 if (b->type == bp_catchpoint)
3903 {
3904 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3905 if (!b->ops->breakpoint_hit (b))
3906 return 0;
3907 }
3908
18a18393
VP
3909 return 1;
3910}
3911
3912/* If BS refers to a watchpoint, determine if the watched values
3913 has actually changed, and we should stop. If not, set BS->stop
3914 to 0. */
3915static void
3916bpstat_check_watchpoint (bpstat bs)
3917{
2bdf28a0
JK
3918 const struct bp_location *bl;
3919 struct breakpoint *b;
3920
3921 /* BS is built for existing struct breakpoint. */
f431efe5 3922 bl = bs->bp_location_at;
2bdf28a0 3923 gdb_assert (bl != NULL);
f431efe5 3924 b = bs->breakpoint_at;
2bdf28a0 3925 gdb_assert (b != NULL);
18a18393 3926
cc60f2e3 3927 if (is_watchpoint (b))
18a18393 3928 {
18a18393
VP
3929 int must_check_value = 0;
3930
3931 if (b->type == bp_watchpoint)
3932 /* For a software watchpoint, we must always check the
3933 watched value. */
3934 must_check_value = 1;
3935 else if (b->watchpoint_triggered == watch_triggered_yes)
3936 /* We have a hardware watchpoint (read, write, or access)
3937 and the target earlier reported an address watched by
3938 this watchpoint. */
3939 must_check_value = 1;
3940 else if (b->watchpoint_triggered == watch_triggered_unknown
3941 && b->type == bp_hardware_watchpoint)
3942 /* We were stopped by a hardware watchpoint, but the target could
3943 not report the data address. We must check the watchpoint's
3944 value. Access and read watchpoints are out of luck; without
3945 a data address, we can't figure it out. */
3946 must_check_value = 1;
3947
3948 if (must_check_value)
3949 {
3e43a32a
MS
3950 char *message
3951 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3952 b->number);
18a18393
VP
3953 struct cleanup *cleanups = make_cleanup (xfree, message);
3954 int e = catch_errors (watchpoint_check, bs, message,
3955 RETURN_MASK_ALL);
3956 do_cleanups (cleanups);
3957 switch (e)
3958 {
3959 case WP_DELETED:
3960 /* We've already printed what needs to be printed. */
3961 bs->print_it = print_it_done;
3962 /* Stop. */
3963 break;
60e1c644
PA
3964 case WP_IGNORE:
3965 bs->print_it = print_it_noop;
3966 bs->stop = 0;
3967 break;
18a18393
VP
3968 case WP_VALUE_CHANGED:
3969 if (b->type == bp_read_watchpoint)
3970 {
85d721b8
PA
3971 /* There are two cases to consider here:
3972
4a64f543 3973 1. We're watching the triggered memory for reads.
85d721b8
PA
3974 In that case, trust the target, and always report
3975 the watchpoint hit to the user. Even though
3976 reads don't cause value changes, the value may
3977 have changed since the last time it was read, and
3978 since we're not trapping writes, we will not see
3979 those, and as such we should ignore our notion of
3980 old value.
3981
4a64f543 3982 2. We're watching the triggered memory for both
85d721b8
PA
3983 reads and writes. There are two ways this may
3984 happen:
3985
4a64f543 3986 2.1. This is a target that can't break on data
85d721b8
PA
3987 reads only, but can break on accesses (reads or
3988 writes), such as e.g., x86. We detect this case
3989 at the time we try to insert read watchpoints.
3990
4a64f543 3991 2.2. Otherwise, the target supports read
85d721b8
PA
3992 watchpoints, but, the user set an access or write
3993 watchpoint watching the same memory as this read
3994 watchpoint.
3995
3996 If we're watching memory writes as well as reads,
3997 ignore watchpoint hits when we find that the
3998 value hasn't changed, as reads don't cause
3999 changes. This still gives false positives when
4000 the program writes the same value to memory as
4001 what there was already in memory (we will confuse
4002 it for a read), but it's much better than
4003 nothing. */
4004
4005 int other_write_watchpoint = 0;
4006
4007 if (bl->watchpoint_type == hw_read)
4008 {
4009 struct breakpoint *other_b;
4010
4011 ALL_BREAKPOINTS (other_b)
4012 if ((other_b->type == bp_hardware_watchpoint
4013 || other_b->type == bp_access_watchpoint)
4014 && (other_b->watchpoint_triggered
4015 == watch_triggered_yes))
4016 {
4017 other_write_watchpoint = 1;
4018 break;
4019 }
4020 }
4021
4022 if (other_write_watchpoint
4023 || bl->watchpoint_type == hw_access)
4024 {
4025 /* We're watching the same memory for writes,
4026 and the value changed since the last time we
4027 updated it, so this trap must be for a write.
4028 Ignore it. */
4029 bs->print_it = print_it_noop;
4030 bs->stop = 0;
4031 }
18a18393
VP
4032 }
4033 break;
4034 case WP_VALUE_NOT_CHANGED:
4035 if (b->type == bp_hardware_watchpoint
4036 || b->type == bp_watchpoint)
4037 {
4038 /* Don't stop: write watchpoints shouldn't fire if
4039 the value hasn't changed. */
4040 bs->print_it = print_it_noop;
4041 bs->stop = 0;
4042 }
4043 /* Stop. */
4044 break;
4045 default:
4046 /* Can't happen. */
4047 case 0:
4048 /* Error from catch_errors. */
4049 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 4050 watchpoint_del_at_next_stop (b);
18a18393
VP
4051 /* We've already printed what needs to be printed. */
4052 bs->print_it = print_it_done;
4053 break;
4054 }
4055 }
4056 else /* must_check_value == 0 */
4057 {
4058 /* This is a case where some watchpoint(s) triggered, but
4059 not at the address of this watchpoint, or else no
4060 watchpoint triggered after all. So don't print
4061 anything for this watchpoint. */
4062 bs->print_it = print_it_noop;
4063 bs->stop = 0;
4064 }
4065 }
4066}
4067
4068
4069/* Check conditions (condition proper, frame, thread and ignore count)
4070 of breakpoint referred to by BS. If we should not stop for this
4071 breakpoint, set BS->stop to 0. */
f431efe5 4072
18a18393
VP
4073static void
4074bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4075{
4076 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4077 const struct bp_location *bl;
4078 struct breakpoint *b;
4079
4080 /* BS is built for existing struct breakpoint. */
f431efe5 4081 bl = bs->bp_location_at;
2bdf28a0 4082 gdb_assert (bl != NULL);
f431efe5 4083 b = bs->breakpoint_at;
2bdf28a0 4084 gdb_assert (b != NULL);
18a18393
VP
4085
4086 if (frame_id_p (b->frame_id)
edb3359d 4087 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4088 bs->stop = 0;
4089 else if (bs->stop)
4090 {
4091 int value_is_zero = 0;
60e1c644
PA
4092 struct expression *cond;
4093
7371cf6d
PM
4094 /* Evaluate Python breakpoints that have a "stop"
4095 method implemented. */
4096 if (b->py_bp_object)
4097 bs->stop = gdbpy_should_stop (b->py_bp_object);
4098
60e1c644
PA
4099 if (is_watchpoint (b))
4100 cond = b->cond_exp;
4101 else
4102 cond = bl->cond;
4103
f431efe5 4104 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4105 {
60e1c644
PA
4106 int within_current_scope = 1;
4107
c5bc3a77
DJ
4108 /* We use value_mark and value_free_to_mark because it could
4109 be a long time before we return to the command level and
4110 call free_all_values. We can't call free_all_values
4111 because we might be in the middle of evaluating a
4112 function call. */
4113 struct value *mark = value_mark ();
4114
edb3359d
DJ
4115 /* Need to select the frame, with all that implies so that
4116 the conditions will have the right context. Because we
4117 use the frame, we will not see an inlined function's
4118 variables when we arrive at a breakpoint at the start
4119 of the inlined function; the current frame will be the
4120 call site. */
60e1c644
PA
4121 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4122 select_frame (get_current_frame ());
4123 else
4124 {
4125 struct frame_info *frame;
4126
4127 /* For local watchpoint expressions, which particular
4128 instance of a local is being watched matters, so we
4129 keep track of the frame to evaluate the expression
4130 in. To evaluate the condition however, it doesn't
4131 really matter which instantiation of the function
4132 where the condition makes sense triggers the
4133 watchpoint. This allows an expression like "watch
4134 global if q > 10" set in `func', catch writes to
4135 global on all threads that call `func', or catch
4136 writes on all recursive calls of `func' by a single
4137 thread. We simply always evaluate the condition in
4138 the innermost frame that's executing where it makes
4139 sense to evaluate the condition. It seems
4140 intuitive. */
4141 frame = block_innermost_frame (b->cond_exp_valid_block);
4142 if (frame != NULL)
4143 select_frame (frame);
4144 else
4145 within_current_scope = 0;
4146 }
4147 if (within_current_scope)
4148 value_is_zero
4149 = catch_errors (breakpoint_cond_eval, cond,
4150 "Error in testing breakpoint condition:\n",
4151 RETURN_MASK_ALL);
4152 else
4153 {
4154 warning (_("Watchpoint condition cannot be tested "
4155 "in the current scope"));
4156 /* If we failed to set the right context for this
4157 watchpoint, unconditionally report it. */
4158 value_is_zero = 0;
4159 }
4a64f543 4160 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4161 value_free_to_mark (mark);
18a18393 4162 }
60e1c644
PA
4163
4164 if (cond && value_is_zero)
18a18393
VP
4165 {
4166 bs->stop = 0;
4167 }
4168 else if (b->thread != -1 && b->thread != thread_id)
4169 {
4170 bs->stop = 0;
4171 }
4172 else if (b->ignore_count > 0)
4173 {
4174 b->ignore_count--;
4175 annotate_ignore_count_change ();
4176 bs->stop = 0;
4a64f543 4177 /* Increase the hit count even though we don't stop. */
18a18393
VP
4178 ++(b->hit_count);
4179 }
4180 }
4181}
4182
4183
9709f61c 4184/* Get a bpstat associated with having just stopped at address
d983da9c 4185 BP_ADDR in thread PTID.
c906108c 4186
d983da9c 4187 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4188 don't understand this stop. Result is a chain of bpstat's such
4189 that:
c906108c 4190
c5aa993b 4191 if we don't understand the stop, the result is a null pointer.
c906108c 4192
c5aa993b 4193 if we understand why we stopped, the result is not null.
c906108c 4194
c5aa993b
JM
4195 Each element of the chain refers to a particular breakpoint or
4196 watchpoint at which we have stopped. (We may have stopped for
4197 several reasons concurrently.)
c906108c 4198
c5aa993b
JM
4199 Each element of the chain has valid next, breakpoint_at,
4200 commands, FIXME??? fields. */
c906108c
SS
4201
4202bpstat
6c95b8df
PA
4203bpstat_stop_status (struct address_space *aspace,
4204 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4205{
0d381245 4206 struct breakpoint *b = NULL;
afe38095 4207 struct bp_location *bl;
20874c92 4208 struct bp_location *loc;
5760d0ab
JK
4209 /* First item of allocated bpstat's. */
4210 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4211 /* Pointer to the last thing in the chain currently. */
5760d0ab 4212 bpstat bs;
20874c92 4213 int ix;
429374b8 4214 int need_remove_insert;
f431efe5 4215 int removed_any;
c906108c 4216
f431efe5
PA
4217 /* First, build the bpstat chain with locations that explain a
4218 target stop, while being careful to not set the target running,
4219 as that may invalidate locations (in particular watchpoint
4220 locations are recreated). Resuming will happen here with
4221 breakpoint conditions or watchpoint expressions that include
4222 inferior function calls. */
c5aa993b 4223
429374b8
JK
4224 ALL_BREAKPOINTS (b)
4225 {
4226 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4227 continue;
a5606eee 4228
429374b8
JK
4229 for (bl = b->loc; bl != NULL; bl = bl->next)
4230 {
4a64f543
MS
4231 /* For hardware watchpoints, we look only at the first
4232 location. The watchpoint_check function will work on the
4233 entire expression, not the individual locations. For
4234 read watchpoints, the watchpoints_triggered function has
4235 checked all locations already. */
429374b8
JK
4236 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4237 break;
18a18393 4238
429374b8
JK
4239 if (bl->shlib_disabled)
4240 continue;
c5aa993b 4241
429374b8
JK
4242 if (!bpstat_check_location (bl, aspace, bp_addr))
4243 continue;
c5aa993b 4244
4a64f543
MS
4245 /* Come here if it's a watchpoint, or if the break address
4246 matches. */
c5aa993b 4247
4a64f543
MS
4248 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4249 explain stop. */
c5aa993b 4250
f431efe5
PA
4251 /* Assume we stop. Should we find a watchpoint that is not
4252 actually triggered, or if the condition of the breakpoint
4253 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4254 bs->stop = 1;
4255 bs->print = 1;
d983da9c 4256
f431efe5
PA
4257 /* If this is a scope breakpoint, mark the associated
4258 watchpoint as triggered so that we will handle the
4259 out-of-scope event. We'll get to the watchpoint next
4260 iteration. */
d0fb5eae 4261 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4262 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4263 }
4264 }
4265
4266 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4267 {
4268 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
4269 aspace, bp_addr))
4270 {
5760d0ab 4271 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4272 /* For hits of moribund locations, we should just proceed. */
4273 bs->stop = 0;
4274 bs->print = 0;
4275 bs->print_it = print_it_noop;
4276 }
4277 }
4278
f431efe5
PA
4279 /* Now go through the locations that caused the target to stop, and
4280 check whether we're interested in reporting this stop to higher
4281 layers, or whether we should resume the target transparently. */
4282
4283 removed_any = 0;
4284
5760d0ab 4285 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4286 {
4287 if (!bs->stop)
4288 continue;
4289
4290 bpstat_check_watchpoint (bs);
4291 if (!bs->stop)
4292 continue;
4293
4294 b = bs->breakpoint_at;
18a18393 4295
429374b8 4296 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4297 || b->type == bp_longjmp_master
186c406b
TT
4298 || b->type == bp_std_terminate_master
4299 || b->type == bp_exception_master)
429374b8
JK
4300 /* We do not stop for these. */
4301 bs->stop = 0;
4302 else
4303 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4304
429374b8
JK
4305 if (bs->stop)
4306 {
4307 ++(b->hit_count);
c906108c 4308
4a64f543 4309 /* We will stop here. */
429374b8
JK
4310 if (b->disposition == disp_disable)
4311 {
4312 if (b->enable_state != bp_permanent)
4313 b->enable_state = bp_disabled;
f431efe5 4314 removed_any = 1;
429374b8
JK
4315 }
4316 if (b->silent)
4317 bs->print = 0;
4318 bs->commands = b->commands;
9add0f1b
TT
4319 incref_counted_command_line (bs->commands);
4320 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4321 if (bs->commands_left
4322 && (strcmp ("silent", bs->commands_left->line) == 0
4323 || (xdb_commands
4324 && strcmp ("Q",
4325 bs->commands_left->line) == 0)))
429374b8 4326 {
9add0f1b 4327 bs->commands_left = bs->commands_left->next;
429374b8
JK
4328 bs->print = 0;
4329 }
429374b8
JK
4330 }
4331
4332 /* Print nothing for this entry if we dont stop or dont print. */
4333 if (bs->stop == 0 || bs->print == 0)
4334 bs->print_it = print_it_noop;
429374b8 4335 }
876fa593 4336
d983da9c
DJ
4337 /* If we aren't stopping, the value of some hardware watchpoint may
4338 not have changed, but the intermediate memory locations we are
4339 watching may have. Don't bother if we're stopping; this will get
4340 done later. */
d832cb68 4341 need_remove_insert = 0;
5760d0ab
JK
4342 if (! bpstat_causes_stop (bs_head))
4343 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4344 if (!bs->stop
f431efe5
PA
4345 && bs->breakpoint_at
4346 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4347 {
4a64f543 4348 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4349 need_remove_insert = 1;
d983da9c
DJ
4350 }
4351
d832cb68 4352 if (need_remove_insert)
2d134ed3 4353 update_global_location_list (1);
f431efe5
PA
4354 else if (removed_any)
4355 update_global_location_list (0);
d832cb68 4356
5760d0ab 4357 return bs_head;
c906108c 4358}
628fe4e4
JK
4359
4360static void
4361handle_jit_event (void)
4362{
4363 struct frame_info *frame;
4364 struct gdbarch *gdbarch;
4365
4366 /* Switch terminal for any messages produced by
4367 breakpoint_re_set. */
4368 target_terminal_ours_for_output ();
4369
4370 frame = get_current_frame ();
4371 gdbarch = get_frame_arch (frame);
4372
4373 jit_event_handler (gdbarch);
4374
4375 target_terminal_inferior ();
4376}
4377
4378/* Prepare WHAT final decision for infrun. */
4379
4380/* Decide what infrun needs to do with this bpstat. */
4381
c906108c 4382struct bpstat_what
0e30163f 4383bpstat_what (bpstat bs_head)
c906108c 4384{
c906108c 4385 struct bpstat_what retval;
628fe4e4
JK
4386 /* We need to defer calling `solib_add', as adding new symbols
4387 resets breakpoints, which in turn deletes breakpoint locations,
4388 and hence may clear unprocessed entries in the BS chain. */
4389 int shlib_event = 0;
4390 int jit_event = 0;
0e30163f 4391 bpstat bs;
c906108c 4392
628fe4e4 4393 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4394 retval.call_dummy = STOP_NONE;
186c406b 4395 retval.is_longjmp = 0;
628fe4e4 4396
0e30163f 4397 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4398 {
628fe4e4
JK
4399 /* Extract this BS's action. After processing each BS, we check
4400 if its action overrides all we've seem so far. */
4401 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4402 enum bptype bptype;
4403
c906108c 4404 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4405 {
4406 /* I suspect this can happen if it was a momentary
4407 breakpoint which has since been deleted. */
4408 bptype = bp_none;
4409 }
f431efe5 4410 else if (bs->breakpoint_at == NULL)
628fe4e4 4411 bptype = bp_none;
20874c92 4412 else
f431efe5 4413 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4414
4415 switch (bptype)
c906108c
SS
4416 {
4417 case bp_none:
628fe4e4 4418 break;
c906108c
SS
4419 case bp_breakpoint:
4420 case bp_hardware_breakpoint:
4421 case bp_until:
4422 case bp_finish:
4423 if (bs->stop)
4424 {
4425 if (bs->print)
628fe4e4 4426 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4427 else
628fe4e4 4428 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4429 }
4430 else
628fe4e4 4431 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4432 break;
4433 case bp_watchpoint:
4434 case bp_hardware_watchpoint:
4435 case bp_read_watchpoint:
4436 case bp_access_watchpoint:
4437 if (bs->stop)
4438 {
4439 if (bs->print)
628fe4e4 4440 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4441 else
628fe4e4 4442 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4443 }
4444 else
628fe4e4
JK
4445 {
4446 /* There was a watchpoint, but we're not stopping.
4447 This requires no further action. */
4448 }
c906108c
SS
4449 break;
4450 case bp_longjmp:
186c406b 4451 case bp_exception:
628fe4e4 4452 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4453 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4454 break;
4455 case bp_longjmp_resume:
186c406b 4456 case bp_exception_resume:
628fe4e4 4457 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4458 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4459 break;
4460 case bp_step_resume:
4461 if (bs->stop)
628fe4e4
JK
4462 this_action = BPSTAT_WHAT_STEP_RESUME;
4463 else
c906108c 4464 {
628fe4e4
JK
4465 /* It is for the wrong frame. */
4466 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4467 }
c906108c 4468 break;
c906108c 4469 case bp_watchpoint_scope:
c4093a6a 4470 case bp_thread_event:
1900040c 4471 case bp_overlay_event:
0fd8e87f 4472 case bp_longjmp_master:
aa7d318d 4473 case bp_std_terminate_master:
186c406b 4474 case bp_exception_master:
628fe4e4 4475 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4476 break;
ce78b96d 4477 case bp_catchpoint:
c5aa993b
JM
4478 if (bs->stop)
4479 {
4480 if (bs->print)
628fe4e4 4481 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4482 else
628fe4e4 4483 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4484 }
4485 else
628fe4e4
JK
4486 {
4487 /* There was a catchpoint, but we're not stopping.
4488 This requires no further action. */
4489 }
4490 break;
4491 case bp_shlib_event:
4492 shlib_event = 1;
4493
4494 /* If requested, stop when the dynamic linker notifies GDB
4495 of events. This allows the user to get control and place
4496 breakpoints in initializer routines for dynamically
4497 loaded objects (among other things). */
4498 if (stop_on_solib_events)
4499 this_action = BPSTAT_WHAT_STOP_NOISY;
4500 else
4501 this_action = BPSTAT_WHAT_SINGLE;
4502 break;
4503 case bp_jit_event:
4504 jit_event = 1;
4505 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4506 break;
c906108c 4507 case bp_call_dummy:
53a5351d
JM
4508 /* Make sure the action is stop (silent or noisy),
4509 so infrun.c pops the dummy frame. */
aa7d318d 4510 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4511 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4512 break;
4513 case bp_std_terminate:
4514 /* Make sure the action is stop (silent or noisy),
4515 so infrun.c pops the dummy frame. */
aa7d318d 4516 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4517 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4518 break;
1042e4c0 4519 case bp_tracepoint:
7a697b8d 4520 case bp_fast_tracepoint:
0fb4aa4b 4521 case bp_static_tracepoint:
1042e4c0
SS
4522 /* Tracepoint hits should not be reported back to GDB, and
4523 if one got through somehow, it should have been filtered
4524 out already. */
4525 internal_error (__FILE__, __LINE__,
7a697b8d 4526 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4527 break;
4528 case bp_gnu_ifunc_resolver:
4529 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4530 this_action = BPSTAT_WHAT_SINGLE;
4531 break;
4532 case bp_gnu_ifunc_resolver_return:
4533 /* The breakpoint will be removed, execution will restart from the
4534 PC of the former breakpoint. */
4535 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4536 break;
628fe4e4
JK
4537 default:
4538 internal_error (__FILE__, __LINE__,
4539 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4540 }
628fe4e4
JK
4541
4542 retval.main_action = max (retval.main_action, this_action);
c906108c 4543 }
628fe4e4 4544
0e30163f
JK
4545 /* These operations may affect the bs->breakpoint_at state so they are
4546 delayed after MAIN_ACTION is decided above. */
4547
628fe4e4
JK
4548 if (shlib_event)
4549 {
4550 if (debug_infrun)
4551 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4552
4553 /* Check for any newly added shared libraries if we're supposed
4554 to be adding them automatically. */
4555
4556 /* Switch terminal for any messages produced by
4557 breakpoint_re_set. */
4558 target_terminal_ours_for_output ();
4559
4560#ifdef SOLIB_ADD
4561 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4562#else
4563 solib_add (NULL, 0, &current_target, auto_solib_add);
4564#endif
4565
4566 target_terminal_inferior ();
4567 }
4568
4569 if (jit_event)
4570 {
4571 if (debug_infrun)
4572 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4573
4574 handle_jit_event ();
4575 }
4576
0e30163f
JK
4577 for (bs = bs_head; bs != NULL; bs = bs->next)
4578 {
4579 struct breakpoint *b = bs->breakpoint_at;
4580
4581 if (b == NULL)
4582 continue;
4583 switch (b->type)
4584 {
4585 case bp_gnu_ifunc_resolver:
4586 gnu_ifunc_resolver_stop (b);
4587 break;
4588 case bp_gnu_ifunc_resolver_return:
4589 gnu_ifunc_resolver_return_stop (b);
4590 break;
4591 }
4592 }
4593
c906108c
SS
4594 return retval;
4595}
4596
4597/* Nonzero if we should step constantly (e.g. watchpoints on machines
4598 without hardware support). This isn't related to a specific bpstat,
4599 just to things like whether watchpoints are set. */
4600
c5aa993b 4601int
fba45db2 4602bpstat_should_step (void)
c906108c
SS
4603{
4604 struct breakpoint *b;
cc59ec59 4605
c906108c 4606 ALL_BREAKPOINTS (b)
717a8278 4607 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4608 return 1;
c906108c
SS
4609 return 0;
4610}
4611
67822962
PA
4612int
4613bpstat_causes_stop (bpstat bs)
4614{
4615 for (; bs != NULL; bs = bs->next)
4616 if (bs->stop)
4617 return 1;
4618
4619 return 0;
4620}
4621
c906108c 4622\f
c5aa993b 4623
170b53b2
UW
4624/* Compute a string of spaces suitable to indent the next line
4625 so it starts at the position corresponding to the table column
4626 named COL_NAME in the currently active table of UIOUT. */
4627
4628static char *
4629wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4630{
4631 static char wrap_indent[80];
4632 int i, total_width, width, align;
4633 char *text;
4634
4635 total_width = 0;
4636 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4637 {
4638 if (strcmp (text, col_name) == 0)
4639 {
4640 gdb_assert (total_width < sizeof wrap_indent);
4641 memset (wrap_indent, ' ', total_width);
4642 wrap_indent[total_width] = 0;
4643
4644 return wrap_indent;
4645 }
4646
4647 total_width += width + 1;
4648 }
4649
4650 return NULL;
4651}
4652
859825b8
JK
4653/* Print the LOC location out of the list of B->LOC locations. */
4654
170b53b2
UW
4655static void
4656print_breakpoint_location (struct breakpoint *b,
4657 struct bp_location *loc)
0d381245 4658{
6c95b8df
PA
4659 struct cleanup *old_chain = save_current_program_space ();
4660
859825b8
JK
4661 if (loc != NULL && loc->shlib_disabled)
4662 loc = NULL;
4663
6c95b8df
PA
4664 if (loc != NULL)
4665 set_current_program_space (loc->pspace);
4666
56435ebe
TT
4667 if (b->display_canonical)
4668 ui_out_field_string (uiout, "what", b->addr_string);
4669 else if (b->source_file && loc)
0d381245
VP
4670 {
4671 struct symbol *sym
4672 = find_pc_sect_function (loc->address, loc->section);
4673 if (sym)
4674 {
4675 ui_out_text (uiout, "in ");
4676 ui_out_field_string (uiout, "func",
4677 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4678 ui_out_text (uiout, " ");
4679 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4680 ui_out_text (uiout, "at ");
0d381245
VP
4681 }
4682 ui_out_field_string (uiout, "file", b->source_file);
4683 ui_out_text (uiout, ":");
4684
4685 if (ui_out_is_mi_like_p (uiout))
4686 {
4687 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4688 char *fullname = symtab_to_fullname (sal.symtab);
4689
4690 if (fullname)
4691 ui_out_field_string (uiout, "fullname", fullname);
4692 }
4693
4694 ui_out_field_int (uiout, "line", b->line_number);
4695 }
859825b8 4696 else if (loc)
0d381245 4697 {
170b53b2
UW
4698 struct ui_stream *stb = ui_out_stream_new (uiout);
4699 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4700
22e722e1
DJ
4701 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4702 demangle, "");
0d381245 4703 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4704
4705 do_cleanups (stb_chain);
0d381245 4706 }
859825b8
JK
4707 else
4708 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4709
4710 do_cleanups (old_chain);
0d381245
VP
4711}
4712
269b11a2
PA
4713static const char *
4714bptype_string (enum bptype type)
c906108c 4715{
c4093a6a
JM
4716 struct ep_type_description
4717 {
4718 enum bptype type;
4719 char *description;
4720 };
4721 static struct ep_type_description bptypes[] =
c906108c 4722 {
c5aa993b
JM
4723 {bp_none, "?deleted?"},
4724 {bp_breakpoint, "breakpoint"},
c906108c 4725 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4726 {bp_until, "until"},
4727 {bp_finish, "finish"},
4728 {bp_watchpoint, "watchpoint"},
c906108c 4729 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4730 {bp_read_watchpoint, "read watchpoint"},
4731 {bp_access_watchpoint, "acc watchpoint"},
4732 {bp_longjmp, "longjmp"},
4733 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4734 {bp_exception, "exception"},
4735 {bp_exception_resume, "exception resume"},
c5aa993b 4736 {bp_step_resume, "step resume"},
c5aa993b
JM
4737 {bp_watchpoint_scope, "watchpoint scope"},
4738 {bp_call_dummy, "call dummy"},
aa7d318d 4739 {bp_std_terminate, "std::terminate"},
c5aa993b 4740 {bp_shlib_event, "shlib events"},
c4093a6a 4741 {bp_thread_event, "thread events"},
1900040c 4742 {bp_overlay_event, "overlay events"},
0fd8e87f 4743 {bp_longjmp_master, "longjmp master"},
aa7d318d 4744 {bp_std_terminate_master, "std::terminate master"},
186c406b 4745 {bp_exception_master, "exception master"},
ce78b96d 4746 {bp_catchpoint, "catchpoint"},
1042e4c0 4747 {bp_tracepoint, "tracepoint"},
7a697b8d 4748 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4749 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4750 {bp_jit_event, "jit events"},
0e30163f
JK
4751 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4752 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4753 };
269b11a2
PA
4754
4755 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4756 || ((int) type != bptypes[(int) type].type))
4757 internal_error (__FILE__, __LINE__,
4758 _("bptypes table does not describe type #%d."),
4759 (int) type);
4760
4761 return bptypes[(int) type].description;
4762}
4763
4764/* Print B to gdb_stdout. */
4765
4766static void
4767print_one_breakpoint_location (struct breakpoint *b,
4768 struct bp_location *loc,
4769 int loc_number,
4770 struct bp_location **last_loc,
269b11a2
PA
4771 int allflag)
4772{
4773 struct command_line *l;
c2c6d25f 4774 static char bpenables[] = "nynny";
3b31d625 4775 struct cleanup *bkpt_chain;
c906108c 4776
0d381245
VP
4777 int header_of_multiple = 0;
4778 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4779 struct value_print_options opts;
4780
4781 get_user_print_options (&opts);
0d381245
VP
4782
4783 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4784 /* See comment in print_one_breakpoint concerning treatment of
4785 breakpoints with single disabled location. */
0d381245
VP
4786 if (loc == NULL
4787 && (b->loc != NULL
4788 && (b->loc->next != NULL || !b->loc->enabled)))
4789 header_of_multiple = 1;
4790 if (loc == NULL)
4791 loc = b->loc;
4792
c4093a6a 4793 annotate_record ();
3b31d625 4794 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4795
4796 /* 1 */
4797 annotate_field (0);
0d381245
VP
4798 if (part_of_multiple)
4799 {
4800 char *formatted;
0c6773c1 4801 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4802 ui_out_field_string (uiout, "number", formatted);
4803 xfree (formatted);
4804 }
4805 else
4806 {
4807 ui_out_field_int (uiout, "number", b->number);
4808 }
c4093a6a
JM
4809
4810 /* 2 */
4811 annotate_field (1);
0d381245
VP
4812 if (part_of_multiple)
4813 ui_out_field_skip (uiout, "type");
269b11a2
PA
4814 else
4815 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4816
4817 /* 3 */
4818 annotate_field (2);
0d381245
VP
4819 if (part_of_multiple)
4820 ui_out_field_skip (uiout, "disp");
4821 else
2cec12e5 4822 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4823
c4093a6a
JM
4824
4825 /* 4 */
4826 annotate_field (3);
0d381245 4827 if (part_of_multiple)
54e52265 4828 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4829 else
4a64f543
MS
4830 ui_out_field_fmt (uiout, "enabled", "%c",
4831 bpenables[(int) b->enable_state]);
54e52265 4832 ui_out_spaces (uiout, 2);
0d381245 4833
c4093a6a
JM
4834
4835 /* 5 and 6 */
3086aeae 4836 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4837 {
4a64f543
MS
4838 /* Although the print_one can possibly print all locations,
4839 calling it here is not likely to get any nice result. So,
4840 make sure there's just one location. */
0d381245 4841 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4842 b->ops->print_one (b, last_loc);
0d381245 4843 }
3086aeae
DJ
4844 else
4845 switch (b->type)
4846 {
4847 case bp_none:
4848 internal_error (__FILE__, __LINE__,
e2e0b3e5 4849 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4850 break;
c906108c 4851
3086aeae
DJ
4852 case bp_watchpoint:
4853 case bp_hardware_watchpoint:
4854 case bp_read_watchpoint:
4855 case bp_access_watchpoint:
4856 /* Field 4, the address, is omitted (which makes the columns
4857 not line up too nicely with the headers, but the effect
4858 is relatively readable). */
79a45b7d 4859 if (opts.addressprint)
3086aeae
DJ
4860 ui_out_field_skip (uiout, "addr");
4861 annotate_field (5);
fa8a61dc 4862 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4863 break;
4864
3086aeae
DJ
4865 case bp_breakpoint:
4866 case bp_hardware_breakpoint:
4867 case bp_until:
4868 case bp_finish:
4869 case bp_longjmp:
4870 case bp_longjmp_resume:
186c406b
TT
4871 case bp_exception:
4872 case bp_exception_resume:
3086aeae 4873 case bp_step_resume:
3086aeae
DJ
4874 case bp_watchpoint_scope:
4875 case bp_call_dummy:
aa7d318d 4876 case bp_std_terminate:
3086aeae
DJ
4877 case bp_shlib_event:
4878 case bp_thread_event:
4879 case bp_overlay_event:
0fd8e87f 4880 case bp_longjmp_master:
aa7d318d 4881 case bp_std_terminate_master:
186c406b 4882 case bp_exception_master:
1042e4c0 4883 case bp_tracepoint:
7a697b8d 4884 case bp_fast_tracepoint:
0fb4aa4b 4885 case bp_static_tracepoint:
4efc6507 4886 case bp_jit_event:
0e30163f
JK
4887 case bp_gnu_ifunc_resolver:
4888 case bp_gnu_ifunc_resolver_return:
79a45b7d 4889 if (opts.addressprint)
3086aeae
DJ
4890 {
4891 annotate_field (4);
54e52265 4892 if (header_of_multiple)
0d381245 4893 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4894 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4895 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4896 else
5af949e3
UW
4897 ui_out_field_core_addr (uiout, "addr",
4898 loc->gdbarch, loc->address);
3086aeae
DJ
4899 }
4900 annotate_field (5);
0d381245 4901 if (!header_of_multiple)
170b53b2 4902 print_breakpoint_location (b, loc);
0d381245 4903 if (b->loc)
a6d9a66e 4904 *last_loc = b->loc;
3086aeae
DJ
4905 break;
4906 }
c906108c 4907
6c95b8df
PA
4908
4909 /* For backward compatibility, don't display inferiors unless there
4910 are several. */
4911 if (loc != NULL
4912 && !header_of_multiple
4913 && (allflag
4914 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4915 && (number_of_program_spaces () > 1
4916 || number_of_inferiors () > 1)
4a64f543
MS
4917 /* LOC is for existing B, it cannot be in
4918 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4919 && loc->owner->type != bp_catchpoint)))
4920 {
4921 struct inferior *inf;
4922 int first = 1;
4923
4924 for (inf = inferior_list; inf != NULL; inf = inf->next)
4925 {
4926 if (inf->pspace == loc->pspace)
4927 {
4928 if (first)
4929 {
4930 first = 0;
4931 ui_out_text (uiout, " inf ");
4932 }
4933 else
4934 ui_out_text (uiout, ", ");
4935 ui_out_text (uiout, plongest (inf->num));
4936 }
4937 }
4938 }
4939
4a306c9a 4940 if (!part_of_multiple)
c4093a6a 4941 {
4a306c9a
JB
4942 if (b->thread != -1)
4943 {
4944 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4945 "stop only in" line a little further down. */
4a306c9a
JB
4946 ui_out_text (uiout, " thread ");
4947 ui_out_field_int (uiout, "thread", b->thread);
4948 }
4949 else if (b->task != 0)
4950 {
4951 ui_out_text (uiout, " task ");
4952 ui_out_field_int (uiout, "task", b->task);
4953 }
c4093a6a
JM
4954 }
4955
8b93c638 4956 ui_out_text (uiout, "\n");
c4093a6a 4957
0fb4aa4b
PA
4958 if (!part_of_multiple && b->static_trace_marker_id)
4959 {
4960 gdb_assert (b->type == bp_static_tracepoint);
4961
4962 ui_out_text (uiout, "\tmarker id is ");
4963 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4964 b->static_trace_marker_id);
4965 ui_out_text (uiout, "\n");
4966 }
4967
0d381245 4968 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4969 {
4970 annotate_field (6);
8b93c638 4971 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4972 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4973 the frame ID. */
5af949e3
UW
4974 ui_out_field_core_addr (uiout, "frame",
4975 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4976 ui_out_text (uiout, "\n");
c4093a6a
JM
4977 }
4978
0d381245 4979 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4980 {
f7f9143b
JB
4981 /* We do not print the condition for Ada exception catchpoints
4982 because the condition is an internal implementation detail
4983 that we do not want to expose to the user. */
c4093a6a 4984 annotate_field (7);
d77f58be 4985 if (is_tracepoint (b))
1042e4c0
SS
4986 ui_out_text (uiout, "\ttrace only if ");
4987 else
4988 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4989 ui_out_field_string (uiout, "cond", b->cond_string);
4990 ui_out_text (uiout, "\n");
4991 }
4992
0d381245 4993 if (!part_of_multiple && b->thread != -1)
c4093a6a 4994 {
4a64f543 4995 /* FIXME should make an annotation for this. */
8b93c638
JM
4996 ui_out_text (uiout, "\tstop only in thread ");
4997 ui_out_field_int (uiout, "thread", b->thread);
4998 ui_out_text (uiout, "\n");
c4093a6a
JM
4999 }
5000
63c715c6 5001 if (!part_of_multiple && b->hit_count)
c4093a6a 5002 {
4a64f543 5003 /* FIXME should make an annotation for this. */
8b93c638
JM
5004 if (ep_is_catchpoint (b))
5005 ui_out_text (uiout, "\tcatchpoint");
5006 else
5007 ui_out_text (uiout, "\tbreakpoint");
5008 ui_out_text (uiout, " already hit ");
5009 ui_out_field_int (uiout, "times", b->hit_count);
5010 if (b->hit_count == 1)
5011 ui_out_text (uiout, " time\n");
5012 else
5013 ui_out_text (uiout, " times\n");
c4093a6a
JM
5014 }
5015
4a64f543
MS
5016 /* Output the count also if it is zero, but only if this is mi.
5017 FIXME: Should have a better test for this. */
9dc5e2a9 5018 if (ui_out_is_mi_like_p (uiout))
63c715c6 5019 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5020 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5021
0d381245 5022 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5023 {
5024 annotate_field (8);
8b93c638
JM
5025 ui_out_text (uiout, "\tignore next ");
5026 ui_out_field_int (uiout, "ignore", b->ignore_count);
5027 ui_out_text (uiout, " hits\n");
c4093a6a 5028 }
059fb39f 5029
9add0f1b 5030 l = b->commands ? b->commands->commands : NULL;
059fb39f 5031 if (!part_of_multiple && l)
c4093a6a 5032 {
3b31d625
EZ
5033 struct cleanup *script_chain;
5034
c4093a6a 5035 annotate_field (9);
3b31d625 5036 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5037 print_command_lines (uiout, l, 4);
3b31d625 5038 do_cleanups (script_chain);
c4093a6a 5039 }
d24317b4 5040
1042e4c0
SS
5041 if (!part_of_multiple && b->pass_count)
5042 {
5043 annotate_field (10);
5044 ui_out_text (uiout, "\tpass count ");
5045 ui_out_field_int (uiout, "pass", b->pass_count);
5046 ui_out_text (uiout, " \n");
5047 }
5048
d24317b4
VP
5049 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5050 {
5051 if (b->addr_string)
5052 ui_out_field_string (uiout, "original-location", b->addr_string);
5053 else if (b->exp_string)
5054 ui_out_field_string (uiout, "original-location", b->exp_string);
5055 }
5056
3b31d625 5057 do_cleanups (bkpt_chain);
c4093a6a 5058}
c5aa993b 5059
0d381245
VP
5060static void
5061print_one_breakpoint (struct breakpoint *b,
4a64f543 5062 struct bp_location **last_loc,
6c95b8df 5063 int allflag)
0d381245 5064{
12c5a436 5065 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
0d381245
VP
5066
5067 /* If this breakpoint has custom print function,
5068 it's already printed. Otherwise, print individual
5069 locations, if any. */
5070 if (b->ops == NULL || b->ops->print_one == NULL)
5071 {
4a64f543
MS
5072 /* If breakpoint has a single location that is disabled, we
5073 print it as if it had several locations, since otherwise it's
5074 hard to represent "breakpoint enabled, location disabled"
5075 situation.
5076
5077 Note that while hardware watchpoints have several locations
a3be7890 5078 internally, that's not a property exposed to user. */
0d381245 5079 if (b->loc
a5606eee 5080 && !is_hardware_watchpoint (b)
0d381245 5081 && (b->loc->next || !b->loc->enabled)
a5606eee 5082 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
5083 {
5084 struct bp_location *loc;
5085 int n = 1;
5086 for (loc = b->loc; loc; loc = loc->next, ++n)
12c5a436 5087 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
0d381245
VP
5088 }
5089 }
5090}
5091
a6d9a66e
UW
5092static int
5093breakpoint_address_bits (struct breakpoint *b)
5094{
5095 int print_address_bits = 0;
5096 struct bp_location *loc;
5097
5098 for (loc = b->loc; loc; loc = loc->next)
5099 {
c7437ca6
PA
5100 int addr_bit;
5101
5102 /* Software watchpoints that aren't watching memory don't have
5103 an address to print. */
5104 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5105 continue;
5106
5107 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5108 if (addr_bit > print_address_bits)
5109 print_address_bits = addr_bit;
5110 }
5111
5112 return print_address_bits;
5113}
0d381245 5114
c4093a6a
JM
5115struct captured_breakpoint_query_args
5116 {
5117 int bnum;
5118 };
c5aa993b 5119
c4093a6a 5120static int
2b65245e 5121do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5122{
5123 struct captured_breakpoint_query_args *args = data;
52f0bd74 5124 struct breakpoint *b;
a6d9a66e 5125 struct bp_location *dummy_loc = NULL;
cc59ec59 5126
c4093a6a
JM
5127 ALL_BREAKPOINTS (b)
5128 {
5129 if (args->bnum == b->number)
c5aa993b 5130 {
12c5a436 5131 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5132 return GDB_RC_OK;
c5aa993b 5133 }
c4093a6a
JM
5134 }
5135 return GDB_RC_NONE;
5136}
c5aa993b 5137
c4093a6a 5138enum gdb_rc
4a64f543
MS
5139gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5140 char **error_message)
c4093a6a
JM
5141{
5142 struct captured_breakpoint_query_args args;
cc59ec59 5143
c4093a6a
JM
5144 args.bnum = bnum;
5145 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5146 an error. */
b0b13bb4
DJ
5147 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5148 error_message, RETURN_MASK_ALL) < 0)
5149 return GDB_RC_FAIL;
5150 else
5151 return GDB_RC_OK;
c4093a6a 5152}
c5aa993b 5153
7f3b0473 5154/* Return non-zero if B is user settable (breakpoints, watchpoints,
4a64f543 5155 catchpoints, et.al.). */
7f3b0473
AC
5156
5157static int
5158user_settable_breakpoint (const struct breakpoint *b)
5159{
5160 return (b->type == bp_breakpoint
ce78b96d 5161 || b->type == bp_catchpoint
7f3b0473 5162 || b->type == bp_hardware_breakpoint
d77f58be 5163 || is_tracepoint (b)
0e30163f
JK
5164 || is_watchpoint (b)
5165 || b->type == bp_gnu_ifunc_resolver);
7f3b0473 5166}
4a64f543 5167
09d682a4
TT
5168/* Return true if this breakpoint was set by the user, false if it is
5169 internal or momentary. */
5170
5171int
5172user_breakpoint_p (struct breakpoint *b)
5173{
5174 return user_settable_breakpoint (b) && b->number > 0;
5175}
5176
7f3b0473 5177/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5178 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5179 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5180 FILTER is non-NULL, call it on each breakpoint and only include the
5181 ones for which it returns non-zero. Return the total number of
5182 breakpoints listed. */
c906108c 5183
d77f58be 5184static int
e5a67952 5185breakpoint_1 (char *args, int allflag,
4a64f543 5186 int (*filter) (const struct breakpoint *))
c4093a6a 5187{
52f0bd74 5188 struct breakpoint *b;
a6d9a66e 5189 struct bp_location *last_loc = NULL;
7f3b0473 5190 int nr_printable_breakpoints;
3b31d625 5191 struct cleanup *bkpttbl_chain;
79a45b7d 5192 struct value_print_options opts;
a6d9a66e 5193 int print_address_bits = 0;
269b11a2
PA
5194 int print_type_col_width = 14;
5195
79a45b7d
TT
5196 get_user_print_options (&opts);
5197
4a64f543
MS
5198 /* Compute the number of rows in the table, as well as the size
5199 required for address fields. */
7f3b0473
AC
5200 nr_printable_breakpoints = 0;
5201 ALL_BREAKPOINTS (b)
e5a67952
MS
5202 {
5203 /* If we have a filter, only list the breakpoints it accepts. */
5204 if (filter && !filter (b))
5205 continue;
5206
5207 /* If we have an "args" string, it is a list of breakpoints to
5208 accept. Skip the others. */
5209 if (args != NULL && *args != '\0')
5210 {
5211 if (allflag && parse_and_eval_long (args) != b->number)
5212 continue;
5213 if (!allflag && !number_is_in_list (args, b->number))
5214 continue;
5215 }
269b11a2 5216
e5a67952
MS
5217 if (allflag || user_breakpoint_p (b))
5218 {
5219 int addr_bit, type_len;
a6d9a66e 5220
e5a67952
MS
5221 addr_bit = breakpoint_address_bits (b);
5222 if (addr_bit > print_address_bits)
5223 print_address_bits = addr_bit;
269b11a2 5224
e5a67952
MS
5225 type_len = strlen (bptype_string (b->type));
5226 if (type_len > print_type_col_width)
5227 print_type_col_width = type_len;
5228
5229 nr_printable_breakpoints++;
5230 }
5231 }
7f3b0473 5232
79a45b7d 5233 if (opts.addressprint)
3b31d625 5234 bkpttbl_chain
3e43a32a
MS
5235 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5236 nr_printable_breakpoints,
3b31d625 5237 "BreakpointTable");
8b93c638 5238 else
3b31d625 5239 bkpttbl_chain
3e43a32a
MS
5240 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5241 nr_printable_breakpoints,
3b31d625 5242 "BreakpointTable");
8b93c638 5243
7f3b0473 5244 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5245 annotate_breakpoints_headers ();
5246 if (nr_printable_breakpoints > 0)
5247 annotate_field (0);
4a64f543 5248 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5249 if (nr_printable_breakpoints > 0)
5250 annotate_field (1);
269b11a2 5251 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5252 "type", "Type"); /* 2 */
d7faa9e7
AC
5253 if (nr_printable_breakpoints > 0)
5254 annotate_field (2);
4a64f543 5255 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5256 if (nr_printable_breakpoints > 0)
5257 annotate_field (3);
54e52265 5258 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5259 if (opts.addressprint)
e5a67952
MS
5260 {
5261 if (nr_printable_breakpoints > 0)
5262 annotate_field (4);
5263 if (print_address_bits <= 32)
5264 ui_out_table_header (uiout, 10, ui_left,
5265 "addr", "Address"); /* 5 */
5266 else
5267 ui_out_table_header (uiout, 18, ui_left,
5268 "addr", "Address"); /* 5 */
5269 }
d7faa9e7
AC
5270 if (nr_printable_breakpoints > 0)
5271 annotate_field (5);
5272 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5273 ui_out_table_body (uiout);
5274 if (nr_printable_breakpoints > 0)
5275 annotate_breakpoints_table ();
7f3b0473 5276
c4093a6a 5277 ALL_BREAKPOINTS (b)
e5a67952
MS
5278 {
5279 QUIT;
5280 /* If we have a filter, only list the breakpoints it accepts. */
5281 if (filter && !filter (b))
5282 continue;
5283
5284 /* If we have an "args" string, it is a list of breakpoints to
5285 accept. Skip the others. */
5286
5287 if (args != NULL && *args != '\0')
5288 {
5289 if (allflag) /* maintenance info breakpoint */
5290 {
5291 if (parse_and_eval_long (args) != b->number)
5292 continue;
5293 }
5294 else /* all others */
5295 {
5296 if (!number_is_in_list (args, b->number))
5297 continue;
5298 }
5299 }
5300 /* We only print out user settable breakpoints unless the
5301 allflag is set. */
5302 if (allflag || user_breakpoint_p (b))
12c5a436 5303 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5304 }
5305
3b31d625 5306 do_cleanups (bkpttbl_chain);
698384cd 5307
7f3b0473 5308 if (nr_printable_breakpoints == 0)
c906108c 5309 {
4a64f543
MS
5310 /* If there's a filter, let the caller decide how to report
5311 empty list. */
d77f58be
SS
5312 if (!filter)
5313 {
e5a67952 5314 if (args == NULL || *args == '\0')
d77f58be
SS
5315 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5316 else
4a64f543 5317 ui_out_message (uiout, 0,
e5a67952
MS
5318 "No breakpoint or watchpoint matching '%s'.\n",
5319 args);
d77f58be 5320 }
c906108c
SS
5321 }
5322 else
c4093a6a 5323 {
a6d9a66e
UW
5324 if (last_loc && !server_command)
5325 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5326 }
c906108c 5327
4a64f543 5328 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5329 there have been breakpoints? */
c906108c 5330 annotate_breakpoints_table_end ();
d77f58be
SS
5331
5332 return nr_printable_breakpoints;
c906108c
SS
5333}
5334
ad443146
SS
5335/* Display the value of default-collect in a way that is generally
5336 compatible with the breakpoint list. */
5337
5338static void
5339default_collect_info (void)
5340{
5341 /* If it has no value (which is frequently the case), say nothing; a
5342 message like "No default-collect." gets in user's face when it's
5343 not wanted. */
5344 if (!*default_collect)
5345 return;
5346
5347 /* The following phrase lines up nicely with per-tracepoint collect
5348 actions. */
5349 ui_out_text (uiout, "default collect ");
5350 ui_out_field_string (uiout, "default-collect", default_collect);
5351 ui_out_text (uiout, " \n");
5352}
5353
c906108c 5354static void
e5a67952 5355breakpoints_info (char *args, int from_tty)
c906108c 5356{
e5a67952 5357 breakpoint_1 (args, 0, NULL);
ad443146
SS
5358
5359 default_collect_info ();
d77f58be
SS
5360}
5361
5362static void
e5a67952 5363watchpoints_info (char *args, int from_tty)
d77f58be 5364{
e5a67952 5365 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5366
5367 if (num_printed == 0)
5368 {
e5a67952 5369 if (args == NULL || *args == '\0')
d77f58be
SS
5370 ui_out_message (uiout, 0, "No watchpoints.\n");
5371 else
e5a67952 5372 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5373 }
c906108c
SS
5374}
5375
7a292a7a 5376static void
e5a67952 5377maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5378{
e5a67952 5379 breakpoint_1 (args, 1, NULL);
ad443146
SS
5380
5381 default_collect_info ();
c906108c
SS
5382}
5383
0d381245 5384static int
714835d5 5385breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5386 struct program_space *pspace,
714835d5 5387 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5388{
5389 struct bp_location *bl = b->loc;
cc59ec59 5390
0d381245
VP
5391 for (; bl; bl = bl->next)
5392 {
6c95b8df
PA
5393 if (bl->pspace == pspace
5394 && bl->address == pc
0d381245
VP
5395 && (!overlay_debugging || bl->section == section))
5396 return 1;
5397 }
5398 return 0;
5399}
5400
6c95b8df
PA
5401/* Print a message describing any breakpoints set at PC. This
5402 concerns with logical breakpoints, so we match program spaces, not
5403 address spaces. */
c906108c
SS
5404
5405static void
6c95b8df
PA
5406describe_other_breakpoints (struct gdbarch *gdbarch,
5407 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5408 struct obj_section *section, int thread)
c906108c 5409{
52f0bd74
AC
5410 int others = 0;
5411 struct breakpoint *b;
c906108c
SS
5412
5413 ALL_BREAKPOINTS (b)
6c95b8df 5414 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5415 if (others > 0)
5416 {
a3f17187
AC
5417 if (others == 1)
5418 printf_filtered (_("Note: breakpoint "));
5419 else /* if (others == ???) */
5420 printf_filtered (_("Note: breakpoints "));
c906108c 5421 ALL_BREAKPOINTS (b)
6c95b8df 5422 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5423 {
5424 others--;
5425 printf_filtered ("%d", b->number);
5426 if (b->thread == -1 && thread != -1)
5427 printf_filtered (" (all threads)");
5428 else if (b->thread != -1)
5429 printf_filtered (" (thread %d)", b->thread);
5430 printf_filtered ("%s%s ",
059fb39f 5431 ((b->enable_state == bp_disabled
8bea4e01
UW
5432 || b->enable_state == bp_call_disabled
5433 || b->enable_state == bp_startup_disabled)
0d381245
VP
5434 ? " (disabled)"
5435 : b->enable_state == bp_permanent
5436 ? " (permanent)"
5437 : ""),
5438 (others > 1) ? ","
5439 : ((others == 1) ? " and" : ""));
5440 }
a3f17187 5441 printf_filtered (_("also set at pc "));
5af949e3 5442 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5443 printf_filtered (".\n");
5444 }
5445}
5446\f
5447/* Set the default place to put a breakpoint
5448 for the `break' command with no arguments. */
5449
5450void
6c95b8df
PA
5451set_default_breakpoint (int valid, struct program_space *pspace,
5452 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5453 int line)
c906108c
SS
5454{
5455 default_breakpoint_valid = valid;
6c95b8df 5456 default_breakpoint_pspace = pspace;
c906108c
SS
5457 default_breakpoint_address = addr;
5458 default_breakpoint_symtab = symtab;
5459 default_breakpoint_line = line;
5460}
5461
e4f237da
KB
5462/* Return true iff it is meaningful to use the address member of
5463 BPT. For some breakpoint types, the address member is irrelevant
5464 and it makes no sense to attempt to compare it to other addresses
5465 (or use it for any other purpose either).
5466
4a64f543
MS
5467 More specifically, each of the following breakpoint types will
5468 always have a zero valued address and we don't want to mark
5469 breakpoints of any of these types to be a duplicate of an actual
5470 breakpoint at address zero:
e4f237da
KB
5471
5472 bp_watchpoint
2d134ed3
PA
5473 bp_catchpoint
5474
5475*/
e4f237da
KB
5476
5477static int
5478breakpoint_address_is_meaningful (struct breakpoint *bpt)
5479{
5480 enum bptype type = bpt->type;
5481
2d134ed3
PA
5482 return (type != bp_watchpoint && type != bp_catchpoint);
5483}
5484
5485/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5486 true if LOC1 and LOC2 represent the same watchpoint location. */
5487
5488static int
4a64f543
MS
5489watchpoint_locations_match (struct bp_location *loc1,
5490 struct bp_location *loc2)
2d134ed3 5491{
2bdf28a0
JK
5492 /* Both of them must not be in moribund_locations. */
5493 gdb_assert (loc1->owner != NULL);
5494 gdb_assert (loc2->owner != NULL);
5495
4a64f543
MS
5496 /* If the target can evaluate the condition expression in hardware,
5497 then we we need to insert both watchpoints even if they are at
5498 the same place. Otherwise the watchpoint will only trigger when
5499 the condition of whichever watchpoint was inserted evaluates to
5500 true, not giving a chance for GDB to check the condition of the
5501 other watchpoint. */
0cf6dd15 5502 if ((loc1->owner->cond_exp
4a64f543
MS
5503 && target_can_accel_watchpoint_condition (loc1->address,
5504 loc1->length,
0cf6dd15
TJB
5505 loc1->watchpoint_type,
5506 loc1->owner->cond_exp))
5507 || (loc2->owner->cond_exp
4a64f543
MS
5508 && target_can_accel_watchpoint_condition (loc2->address,
5509 loc2->length,
0cf6dd15
TJB
5510 loc2->watchpoint_type,
5511 loc2->owner->cond_exp)))
5512 return 0;
5513
85d721b8
PA
5514 /* Note that this checks the owner's type, not the location's. In
5515 case the target does not support read watchpoints, but does
5516 support access watchpoints, we'll have bp_read_watchpoint
5517 watchpoints with hw_access locations. Those should be considered
5518 duplicates of hw_read locations. The hw_read locations will
5519 become hw_access locations later. */
2d134ed3
PA
5520 return (loc1->owner->type == loc2->owner->type
5521 && loc1->pspace->aspace == loc2->pspace->aspace
5522 && loc1->address == loc2->address
5523 && loc1->length == loc2->length);
e4f237da
KB
5524}
5525
6c95b8df
PA
5526/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5527 same breakpoint location. In most targets, this can only be true
5528 if ASPACE1 matches ASPACE2. On targets that have global
5529 breakpoints, the address space doesn't really matter. */
5530
5531static int
5532breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5533 struct address_space *aspace2, CORE_ADDR addr2)
5534{
5535 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5536 || aspace1 == aspace2)
5537 && addr1 == addr2);
5538}
5539
2d134ed3
PA
5540/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5541 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5542 represent the same location. */
5543
5544static int
4a64f543
MS
5545breakpoint_locations_match (struct bp_location *loc1,
5546 struct bp_location *loc2)
2d134ed3 5547{
2bdf28a0
JK
5548 int hw_point1, hw_point2;
5549
5550 /* Both of them must not be in moribund_locations. */
5551 gdb_assert (loc1->owner != NULL);
5552 gdb_assert (loc2->owner != NULL);
5553
5554 hw_point1 = is_hardware_watchpoint (loc1->owner);
5555 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5556
5557 if (hw_point1 != hw_point2)
5558 return 0;
5559 else if (hw_point1)
5560 return watchpoint_locations_match (loc1, loc2);
5561 else
5562 return breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5563 loc2->pspace->aspace, loc2->address);
5564}
5565
76897487
KB
5566static void
5567breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5568 int bnum, int have_bnum)
5569{
f63fbe86
MS
5570 /* The longest string possibly returned by hex_string_custom
5571 is 50 chars. These must be at least that big for safety. */
5572 char astr1[64];
5573 char astr2[64];
76897487 5574
bb599908
PH
5575 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5576 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5577 if (have_bnum)
8a3fe4f8 5578 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5579 bnum, astr1, astr2);
5580 else
8a3fe4f8 5581 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5582}
5583
4a64f543
MS
5584/* Adjust a breakpoint's address to account for architectural
5585 constraints on breakpoint placement. Return the adjusted address.
5586 Note: Very few targets require this kind of adjustment. For most
5587 targets, this function is simply the identity function. */
76897487
KB
5588
5589static CORE_ADDR
a6d9a66e
UW
5590adjust_breakpoint_address (struct gdbarch *gdbarch,
5591 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5592{
a6d9a66e 5593 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5594 {
5595 /* Very few targets need any kind of breakpoint adjustment. */
5596 return bpaddr;
5597 }
88f7da05
KB
5598 else if (bptype == bp_watchpoint
5599 || bptype == bp_hardware_watchpoint
5600 || bptype == bp_read_watchpoint
5601 || bptype == bp_access_watchpoint
fe798b75 5602 || bptype == bp_catchpoint)
88f7da05
KB
5603 {
5604 /* Watchpoints and the various bp_catch_* eventpoints should not
5605 have their addresses modified. */
5606 return bpaddr;
5607 }
76897487
KB
5608 else
5609 {
5610 CORE_ADDR adjusted_bpaddr;
5611
5612 /* Some targets have architectural constraints on the placement
5613 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5614 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5615
5616 /* An adjusted breakpoint address can significantly alter
5617 a user's expectations. Print a warning if an adjustment
5618 is required. */
5619 if (adjusted_bpaddr != bpaddr)
5620 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5621
5622 return adjusted_bpaddr;
5623 }
5624}
5625
7cc221ef
DJ
5626/* Allocate a struct bp_location. */
5627
26bb91f3 5628static struct bp_location *
39d61571 5629allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5630{
afe38095 5631 struct bp_location *loc;
7cc221ef
DJ
5632
5633 loc = xmalloc (sizeof (struct bp_location));
5634 memset (loc, 0, sizeof (*loc));
5635
e049a4b5 5636 loc->owner = bpt;
511a6cd4 5637 loc->cond = NULL;
0d381245
VP
5638 loc->shlib_disabled = 0;
5639 loc->enabled = 1;
e049a4b5 5640
39d61571 5641 switch (bpt->type)
e049a4b5
DJ
5642 {
5643 case bp_breakpoint:
5644 case bp_until:
5645 case bp_finish:
5646 case bp_longjmp:
5647 case bp_longjmp_resume:
186c406b
TT
5648 case bp_exception:
5649 case bp_exception_resume:
e049a4b5 5650 case bp_step_resume:
e049a4b5
DJ
5651 case bp_watchpoint_scope:
5652 case bp_call_dummy:
aa7d318d 5653 case bp_std_terminate:
e049a4b5
DJ
5654 case bp_shlib_event:
5655 case bp_thread_event:
5656 case bp_overlay_event:
4efc6507 5657 case bp_jit_event:
0fd8e87f 5658 case bp_longjmp_master:
aa7d318d 5659 case bp_std_terminate_master:
186c406b 5660 case bp_exception_master:
0e30163f
JK
5661 case bp_gnu_ifunc_resolver:
5662 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5663 loc->loc_type = bp_loc_software_breakpoint;
5664 break;
5665 case bp_hardware_breakpoint:
5666 loc->loc_type = bp_loc_hardware_breakpoint;
5667 break;
5668 case bp_hardware_watchpoint:
5669 case bp_read_watchpoint:
5670 case bp_access_watchpoint:
5671 loc->loc_type = bp_loc_hardware_watchpoint;
5672 break;
5673 case bp_watchpoint:
ce78b96d 5674 case bp_catchpoint:
15c3d785
PA
5675 case bp_tracepoint:
5676 case bp_fast_tracepoint:
0fb4aa4b 5677 case bp_static_tracepoint:
e049a4b5
DJ
5678 loc->loc_type = bp_loc_other;
5679 break;
5680 default:
e2e0b3e5 5681 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5682 }
5683
f431efe5 5684 loc->refc = 1;
7cc221ef
DJ
5685 return loc;
5686}
5687
f431efe5
PA
5688static void
5689free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5690{
5691 if (loc->cond)
5692 xfree (loc->cond);
74960c60
VP
5693
5694 if (loc->function_name)
5695 xfree (loc->function_name);
f431efe5 5696
fe3f5fa8
VP
5697 xfree (loc);
5698}
5699
f431efe5
PA
5700/* Increment reference count. */
5701
5702static void
5703incref_bp_location (struct bp_location *bl)
5704{
5705 ++bl->refc;
5706}
5707
5708/* Decrement reference count. If the reference count reaches 0,
5709 destroy the bp_location. Sets *BLP to NULL. */
5710
5711static void
5712decref_bp_location (struct bp_location **blp)
5713{
0807b50c
PA
5714 gdb_assert ((*blp)->refc > 0);
5715
f431efe5
PA
5716 if (--(*blp)->refc == 0)
5717 free_bp_location (*blp);
5718 *blp = NULL;
5719}
5720
4a64f543
MS
5721/* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5722 type BPTYPE and has no locations as yet. */
5723/* This function is used in gdbtk sources and thus can not be made
5724 static. */
c906108c 5725
c40e75cd 5726static struct breakpoint *
a6d9a66e
UW
5727set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5728 enum bptype bptype)
c906108c 5729{
52f0bd74 5730 struct breakpoint *b, *b1;
c906108c
SS
5731
5732 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5733 memset (b, 0, sizeof (*b));
2219d63c 5734
4d28f7a8 5735 b->type = bptype;
a6d9a66e 5736 b->gdbarch = gdbarch;
c906108c
SS
5737 b->language = current_language->la_language;
5738 b->input_radix = input_radix;
5739 b->thread = -1;
b5de0fa7 5740 b->enable_state = bp_enabled;
c906108c
SS
5741 b->next = 0;
5742 b->silent = 0;
5743 b->ignore_count = 0;
5744 b->commands = NULL;
818dd999 5745 b->frame_id = null_frame_id;
3a3e9ee3 5746 b->forked_inferior_pid = null_ptid;
c906108c 5747 b->exec_pathname = NULL;
a96d9b2e 5748 b->syscalls_to_be_caught = NULL;
3086aeae 5749 b->ops = NULL;
0d381245 5750 b->condition_not_parsed = 0;
84f4c1fe 5751 b->py_bp_object = NULL;
d0fb5eae 5752 b->related_breakpoint = b;
c906108c 5753
4a64f543
MS
5754 /* Add this breakpoint to the end of the chain so that a list of
5755 breakpoints will come out in order of increasing numbers. */
c906108c
SS
5756
5757 b1 = breakpoint_chain;
5758 if (b1 == 0)
5759 breakpoint_chain = b;
5760 else
5761 {
5762 while (b1->next)
5763 b1 = b1->next;
5764 b1->next = b;
5765 }
0d381245
VP
5766 return b;
5767}
5768
0e30163f
JK
5769/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5770 resolutions should be made as the user specified the location explicitly
5771 enough. */
5772
0d381245 5773static void
0e30163f 5774set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5775{
2bdf28a0
JK
5776 gdb_assert (loc->owner != NULL);
5777
0d381245 5778 if (loc->owner->type == bp_breakpoint
1042e4c0 5779 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5780 || is_tracepoint (loc->owner))
0d381245 5781 {
0e30163f
JK
5782 int is_gnu_ifunc;
5783
5784 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5785 NULL, NULL, &is_gnu_ifunc);
5786
5787 if (is_gnu_ifunc && !explicit_loc)
5788 {
5789 struct breakpoint *b = loc->owner;
5790
5791 gdb_assert (loc->pspace == current_program_space);
5792 if (gnu_ifunc_resolve_name (loc->function_name,
5793 &loc->requested_address))
5794 {
5795 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5796 loc->address = adjust_breakpoint_address (loc->gdbarch,
5797 loc->requested_address,
5798 b->type);
5799 }
5800 else if (b->type == bp_breakpoint && b->loc == loc
5801 && loc->next == NULL && b->related_breakpoint == b)
5802 {
5803 /* Create only the whole new breakpoint of this type but do not
5804 mess more complicated breakpoints with multiple locations. */
5805 b->type = bp_gnu_ifunc_resolver;
5806 }
5807 }
5808
0d381245
VP
5809 if (loc->function_name)
5810 loc->function_name = xstrdup (loc->function_name);
5811 }
5812}
5813
a6d9a66e
UW
5814/* Attempt to determine architecture of location identified by SAL. */
5815static struct gdbarch *
5816get_sal_arch (struct symtab_and_line sal)
5817{
5818 if (sal.section)
5819 return get_objfile_arch (sal.section->objfile);
5820 if (sal.symtab)
5821 return get_objfile_arch (sal.symtab->objfile);
5822
5823 return NULL;
5824}
5825
0d381245
VP
5826/* set_raw_breakpoint is a low level routine for allocating and
5827 partially initializing a breakpoint of type BPTYPE. The newly
5828 created breakpoint's address, section, source file name, and line
5829 number are provided by SAL. The newly created and partially
5830 initialized breakpoint is added to the breakpoint chain and
5831 is also returned as the value of this function.
5832
5833 It is expected that the caller will complete the initialization of
5834 the newly created breakpoint struct as well as output any status
5835 information regarding the creation of a new breakpoint. In
5836 particular, set_raw_breakpoint does NOT set the breakpoint
5837 number! Care should be taken to not allow an error to occur
5838 prior to completing the initialization of the breakpoint. If this
5839 should happen, a bogus breakpoint will be left on the chain. */
5840
63c252f8 5841struct breakpoint *
a6d9a66e
UW
5842set_raw_breakpoint (struct gdbarch *gdbarch,
5843 struct symtab_and_line sal, enum bptype bptype)
0d381245 5844{
4a64f543
MS
5845 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5846 bptype);
0d381245 5847 CORE_ADDR adjusted_address;
a6d9a66e
UW
5848 struct gdbarch *loc_gdbarch;
5849
5850 loc_gdbarch = get_sal_arch (sal);
5851 if (!loc_gdbarch)
5852 loc_gdbarch = b->gdbarch;
0d381245 5853
6c95b8df
PA
5854 if (bptype != bp_catchpoint)
5855 gdb_assert (sal.pspace != NULL);
5856
0d381245
VP
5857 /* Adjust the breakpoint's address prior to allocating a location.
5858 Once we call allocate_bp_location(), that mostly uninitialized
5859 location will be placed on the location chain. Adjustment of the
8defab1a 5860 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5861 not want its scan of the location chain to find a breakpoint and
5862 location that's only been partially initialized. */
4a64f543
MS
5863 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5864 sal.pc, b->type);
0d381245 5865
39d61571 5866 b->loc = allocate_bp_location (b);
a6d9a66e 5867 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5868 b->loc->requested_address = sal.pc;
5869 b->loc->address = adjusted_address;
6c95b8df
PA
5870 b->loc->pspace = sal.pspace;
5871
5872 /* Store the program space that was used to set the breakpoint, for
5873 breakpoint resetting. */
5874 b->pspace = sal.pspace;
0d381245
VP
5875
5876 if (sal.symtab == NULL)
5877 b->source_file = NULL;
5878 else
1b36a34b 5879 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5880 b->loc->section = sal.section;
5881 b->line_number = sal.line;
5882
0e30163f
JK
5883 set_breakpoint_location_function (b->loc,
5884 sal.explicit_pc || sal.explicit_line);
c906108c 5885
c906108c
SS
5886 breakpoints_changed ();
5887
5888 return b;
5889}
5890
c2c6d25f
JM
5891
5892/* Note that the breakpoint object B describes a permanent breakpoint
5893 instruction, hard-wired into the inferior's code. */
5894void
5895make_breakpoint_permanent (struct breakpoint *b)
5896{
0d381245 5897 struct bp_location *bl;
cc59ec59 5898
b5de0fa7 5899 b->enable_state = bp_permanent;
c2c6d25f 5900
4a64f543
MS
5901 /* By definition, permanent breakpoints are already present in the
5902 code. Mark all locations as inserted. For now,
5903 make_breakpoint_permanent is called in just one place, so it's
5904 hard to say if it's reasonable to have permanent breakpoint with
5905 multiple locations or not, but it's easy to implmement. */
0d381245
VP
5906 for (bl = b->loc; bl; bl = bl->next)
5907 bl->inserted = 1;
c2c6d25f
JM
5908}
5909
53a5351d 5910/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5911 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5912 initiated the operation. */
c906108c
SS
5913
5914void
186c406b 5915set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5916{
35df4500 5917 struct breakpoint *b, *b_tmp;
186c406b 5918 int thread = tp->num;
0fd8e87f
UW
5919
5920 /* To avoid having to rescan all objfile symbols at every step,
5921 we maintain a list of continually-inserted but always disabled
5922 longjmp "master" breakpoints. Here, we simply create momentary
5923 clones of those and enable them for the requested thread. */
35df4500 5924 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5925 if (b->pspace == current_program_space
186c406b
TT
5926 && (b->type == bp_longjmp_master
5927 || b->type == bp_exception_master))
0fd8e87f
UW
5928 {
5929 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5930
186c406b 5931 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5932 clone->thread = thread;
5933 }
186c406b
TT
5934
5935 tp->initiating_frame = frame;
c906108c
SS
5936}
5937
611c83ae 5938/* Delete all longjmp breakpoints from THREAD. */
c906108c 5939void
611c83ae 5940delete_longjmp_breakpoint (int thread)
c906108c 5941{
35df4500 5942 struct breakpoint *b, *b_tmp;
c906108c 5943
35df4500 5944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5945 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5946 {
5947 if (b->thread == thread)
5948 delete_breakpoint (b);
5949 }
c906108c
SS
5950}
5951
1900040c
MS
5952void
5953enable_overlay_breakpoints (void)
5954{
52f0bd74 5955 struct breakpoint *b;
1900040c
MS
5956
5957 ALL_BREAKPOINTS (b)
5958 if (b->type == bp_overlay_event)
5959 {
5960 b->enable_state = bp_enabled;
b60e7edf 5961 update_global_location_list (1);
c02f5703 5962 overlay_events_enabled = 1;
1900040c
MS
5963 }
5964}
5965
5966void
5967disable_overlay_breakpoints (void)
5968{
52f0bd74 5969 struct breakpoint *b;
1900040c
MS
5970
5971 ALL_BREAKPOINTS (b)
5972 if (b->type == bp_overlay_event)
5973 {
5974 b->enable_state = bp_disabled;
b60e7edf 5975 update_global_location_list (0);
c02f5703 5976 overlay_events_enabled = 0;
1900040c
MS
5977 }
5978}
5979
aa7d318d
TT
5980/* Set an active std::terminate breakpoint for each std::terminate
5981 master breakpoint. */
5982void
5983set_std_terminate_breakpoint (void)
5984{
35df4500 5985 struct breakpoint *b, *b_tmp;
aa7d318d 5986
35df4500 5987 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5988 if (b->pspace == current_program_space
5989 && b->type == bp_std_terminate_master)
5990 {
5991 struct breakpoint *clone = clone_momentary_breakpoint (b);
5992 clone->type = bp_std_terminate;
5993 }
5994}
5995
5996/* Delete all the std::terminate breakpoints. */
5997void
5998delete_std_terminate_breakpoint (void)
5999{
35df4500 6000 struct breakpoint *b, *b_tmp;
aa7d318d 6001
35df4500 6002 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6003 if (b->type == bp_std_terminate)
6004 delete_breakpoint (b);
6005}
6006
c4093a6a 6007struct breakpoint *
a6d9a66e 6008create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6009{
6010 struct breakpoint *b;
c4093a6a 6011
a6d9a66e 6012 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 6013
b5de0fa7 6014 b->enable_state = bp_enabled;
c4093a6a 6015 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6016 b->addr_string
6017 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6018
b60e7edf 6019 update_global_location_list_nothrow (1);
74960c60 6020
c4093a6a
JM
6021 return b;
6022}
6023
6024void
6025remove_thread_event_breakpoints (void)
6026{
35df4500 6027 struct breakpoint *b, *b_tmp;
c4093a6a 6028
35df4500 6029 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6030 if (b->type == bp_thread_event
6031 && b->loc->pspace == current_program_space)
c4093a6a
JM
6032 delete_breakpoint (b);
6033}
6034
0101ce28
JJ
6035struct captured_parse_breakpoint_args
6036 {
6037 char **arg_p;
6038 struct symtabs_and_lines *sals_p;
7efd8fc2 6039 struct linespec_result *canonical_p;
0101ce28
JJ
6040 int *not_found_ptr;
6041 };
6042
6043struct lang_and_radix
6044 {
6045 enum language lang;
6046 int radix;
6047 };
6048
4efc6507
DE
6049/* Create a breakpoint for JIT code registration and unregistration. */
6050
6051struct breakpoint *
6052create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6053{
6054 struct breakpoint *b;
6055
6056 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6057 update_global_location_list_nothrow (1);
6058 return b;
6059}
0101ce28 6060
03673fc7
PP
6061/* Remove JIT code registration and unregistration breakpoint(s). */
6062
6063void
6064remove_jit_event_breakpoints (void)
6065{
6066 struct breakpoint *b, *b_tmp;
6067
6068 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6069 if (b->type == bp_jit_event
6070 && b->loc->pspace == current_program_space)
6071 delete_breakpoint (b);
6072}
6073
cae688ec
JJ
6074void
6075remove_solib_event_breakpoints (void)
6076{
35df4500 6077 struct breakpoint *b, *b_tmp;
cae688ec 6078
35df4500 6079 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6080 if (b->type == bp_shlib_event
6081 && b->loc->pspace == current_program_space)
cae688ec
JJ
6082 delete_breakpoint (b);
6083}
6084
6085struct breakpoint *
a6d9a66e 6086create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6087{
6088 struct breakpoint *b;
6089
a6d9a66e 6090 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 6091 update_global_location_list_nothrow (1);
cae688ec
JJ
6092 return b;
6093}
6094
6095/* Disable any breakpoints that are on code in shared libraries. Only
6096 apply to enabled breakpoints, disabled ones can just stay disabled. */
6097
6098void
cb851954 6099disable_breakpoints_in_shlibs (void)
cae688ec 6100{
876fa593 6101 struct bp_location *loc, **locp_tmp;
cae688ec 6102
876fa593 6103 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6104 {
2bdf28a0 6105 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6106 struct breakpoint *b = loc->owner;
2bdf28a0 6107
4a64f543
MS
6108 /* We apply the check to all breakpoints, including disabled for
6109 those with loc->duplicate set. This is so that when breakpoint
6110 becomes enabled, or the duplicate is removed, gdb will try to
6111 insert all breakpoints. If we don't set shlib_disabled here,
6112 we'll try to insert those breakpoints and fail. */
1042e4c0 6113 if (((b->type == bp_breakpoint)
508ccb1f 6114 || (b->type == bp_jit_event)
1042e4c0 6115 || (b->type == bp_hardware_breakpoint)
d77f58be 6116 || (is_tracepoint (b)))
6c95b8df 6117 && loc->pspace == current_program_space
0d381245 6118 && !loc->shlib_disabled
a77053c2 6119#ifdef PC_SOLIB
0d381245 6120 && PC_SOLIB (loc->address)
a77053c2 6121#else
6c95b8df 6122 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6123#endif
6124 )
0d381245
VP
6125 {
6126 loc->shlib_disabled = 1;
6127 }
cae688ec
JJ
6128 }
6129}
6130
4a64f543
MS
6131/* Disable any breakpoints that are in in an unloaded shared library.
6132 Only apply to enabled breakpoints, disabled ones can just stay
6133 disabled. */
84acb35a 6134
75149521 6135static void
84acb35a
JJ
6136disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6137{
876fa593 6138 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6139 int disabled_shlib_breaks = 0;
6140
c86cf029
VP
6141 /* SunOS a.out shared libraries are always mapped, so do not
6142 disable breakpoints; they will only be reported as unloaded
6143 through clear_solib when GDB discards its shared library
6144 list. See clear_solib for more information. */
6145 if (exec_bfd != NULL
6146 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6147 return;
6148
876fa593 6149 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6150 {
2bdf28a0 6151 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6152 struct breakpoint *b = loc->owner;
cc59ec59 6153
0d381245
VP
6154 if ((loc->loc_type == bp_loc_hardware_breakpoint
6155 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6156 && solib->pspace == loc->pspace
e2dd7057 6157 && !loc->shlib_disabled
508ccb1f
TT
6158 && (b->type == bp_breakpoint
6159 || b->type == bp_jit_event
6160 || b->type == bp_hardware_breakpoint)
e2dd7057 6161 && solib_contains_address_p (solib, loc->address))
84acb35a 6162 {
e2dd7057
PP
6163 loc->shlib_disabled = 1;
6164 /* At this point, we cannot rely on remove_breakpoint
6165 succeeding so we must mark the breakpoint as not inserted
6166 to prevent future errors occurring in remove_breakpoints. */
6167 loc->inserted = 0;
6168 if (!disabled_shlib_breaks)
6169 {
6170 target_terminal_ours_for_output ();
3e43a32a
MS
6171 warning (_("Temporarily disabling breakpoints "
6172 "for unloaded shared library \"%s\""),
e2dd7057 6173 solib->so_name);
84acb35a 6174 }
e2dd7057 6175 disabled_shlib_breaks = 1;
84acb35a
JJ
6176 }
6177 }
84acb35a
JJ
6178}
6179
ce78b96d
JB
6180/* FORK & VFORK catchpoints. */
6181
4a64f543
MS
6182/* Implement the "insert" breakpoint_ops method for fork
6183 catchpoints. */
ce78b96d 6184
77b06cd7
TJB
6185static int
6186insert_catch_fork (struct bp_location *bl)
ce78b96d 6187{
77b06cd7 6188 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6189}
6190
4a64f543
MS
6191/* Implement the "remove" breakpoint_ops method for fork
6192 catchpoints. */
ce78b96d
JB
6193
6194static int
77b06cd7 6195remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6196{
6197 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6198}
6199
6200/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6201 catchpoints. */
6202
6203static int
6204breakpoint_hit_catch_fork (struct breakpoint *b)
6205{
6206 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
6207}
6208
4a64f543
MS
6209/* Implement the "print_it" breakpoint_ops method for fork
6210 catchpoints. */
ce78b96d
JB
6211
6212static enum print_stop_action
6213print_it_catch_fork (struct breakpoint *b)
6214{
6215 annotate_catchpoint (b->number);
6216 printf_filtered (_("\nCatchpoint %d (forked 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 fork
6222 catchpoints. */
ce78b96d
JB
6223
6224static void
a6d9a66e 6225print_one_catch_fork (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);
6230
4a64f543
MS
6231 /* Field 4, the address, is omitted (which makes the columns not
6232 line up too nicely with the headers, but the effect is relatively
6233 readable). */
79a45b7d 6234 if (opts.addressprint)
ce78b96d
JB
6235 ui_out_field_skip (uiout, "addr");
6236 annotate_field (5);
6237 ui_out_text (uiout, "fork");
6238 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6239 {
6240 ui_out_text (uiout, ", process ");
6241 ui_out_field_int (uiout, "what",
6242 ptid_get_pid (b->forked_inferior_pid));
6243 ui_out_spaces (uiout, 1);
6244 }
6245}
6246
6247/* Implement the "print_mention" breakpoint_ops method for fork
6248 catchpoints. */
6249
6250static void
6251print_mention_catch_fork (struct breakpoint *b)
6252{
6253 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6254}
6255
6149aea9
PA
6256/* Implement the "print_recreate" breakpoint_ops method for fork
6257 catchpoints. */
6258
6259static void
6260print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6261{
6262 fprintf_unfiltered (fp, "catch fork");
6263}
6264
ce78b96d
JB
6265/* The breakpoint_ops structure to be used in fork catchpoints. */
6266
6267static struct breakpoint_ops catch_fork_breakpoint_ops =
6268{
6269 insert_catch_fork,
6270 remove_catch_fork,
6271 breakpoint_hit_catch_fork,
e09342b5 6272 NULL, /* resources_needed */
ce78b96d
JB
6273 print_it_catch_fork,
6274 print_one_catch_fork,
6149aea9
PA
6275 print_mention_catch_fork,
6276 print_recreate_catch_fork
ce78b96d
JB
6277};
6278
4a64f543
MS
6279/* Implement the "insert" breakpoint_ops method for vfork
6280 catchpoints. */
ce78b96d 6281
77b06cd7
TJB
6282static int
6283insert_catch_vfork (struct bp_location *bl)
ce78b96d 6284{
77b06cd7 6285 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6286}
6287
4a64f543
MS
6288/* Implement the "remove" breakpoint_ops method for vfork
6289 catchpoints. */
ce78b96d
JB
6290
6291static int
77b06cd7 6292remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6293{
6294 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6295}
6296
6297/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6298 catchpoints. */
6299
6300static int
6301breakpoint_hit_catch_vfork (struct breakpoint *b)
6302{
6303 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
6304}
6305
4a64f543
MS
6306/* Implement the "print_it" breakpoint_ops method for vfork
6307 catchpoints. */
ce78b96d
JB
6308
6309static enum print_stop_action
6310print_it_catch_vfork (struct breakpoint *b)
6311{
6312 annotate_catchpoint (b->number);
6313 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6314 b->number, ptid_get_pid (b->forked_inferior_pid));
6315 return PRINT_SRC_AND_LOC;
6316}
6317
4a64f543
MS
6318/* Implement the "print_one" breakpoint_ops method for vfork
6319 catchpoints. */
ce78b96d
JB
6320
6321static void
a6d9a66e 6322print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6323{
79a45b7d
TT
6324 struct value_print_options opts;
6325
6326 get_user_print_options (&opts);
4a64f543
MS
6327 /* Field 4, the address, is omitted (which makes the columns not
6328 line up too nicely with the headers, but the effect is relatively
6329 readable). */
79a45b7d 6330 if (opts.addressprint)
ce78b96d
JB
6331 ui_out_field_skip (uiout, "addr");
6332 annotate_field (5);
6333 ui_out_text (uiout, "vfork");
6334 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6335 {
6336 ui_out_text (uiout, ", process ");
6337 ui_out_field_int (uiout, "what",
6338 ptid_get_pid (b->forked_inferior_pid));
6339 ui_out_spaces (uiout, 1);
6340 }
6341}
6342
6343/* Implement the "print_mention" breakpoint_ops method for vfork
6344 catchpoints. */
6345
6346static void
6347print_mention_catch_vfork (struct breakpoint *b)
6348{
6349 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6350}
6351
6149aea9
PA
6352/* Implement the "print_recreate" breakpoint_ops method for vfork
6353 catchpoints. */
6354
6355static void
6356print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6357{
6358 fprintf_unfiltered (fp, "catch vfork");
6359}
6360
ce78b96d
JB
6361/* The breakpoint_ops structure to be used in vfork catchpoints. */
6362
6363static struct breakpoint_ops catch_vfork_breakpoint_ops =
6364{
6365 insert_catch_vfork,
6366 remove_catch_vfork,
6367 breakpoint_hit_catch_vfork,
e09342b5 6368 NULL, /* resources_needed */
ce78b96d
JB
6369 print_it_catch_vfork,
6370 print_one_catch_vfork,
6149aea9
PA
6371 print_mention_catch_vfork,
6372 print_recreate_catch_vfork
ce78b96d
JB
6373};
6374
a96d9b2e
SDJ
6375/* Implement the "insert" breakpoint_ops method for syscall
6376 catchpoints. */
6377
77b06cd7
TJB
6378static int
6379insert_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6380{
6381 struct inferior *inf = current_inferior ();
6382
6383 ++inf->total_syscalls_count;
77b06cd7 6384 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6385 ++inf->any_syscall_count;
6386 else
6387 {
6388 int i, iter;
cc59ec59 6389
a96d9b2e 6390 for (i = 0;
77b06cd7 6391 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6392 i++)
6393 {
6394 int elem;
cc59ec59 6395
a96d9b2e
SDJ
6396 if (iter >= VEC_length (int, inf->syscalls_counts))
6397 {
6398 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6399 uintptr_t vec_addr_offset
6400 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6401 uintptr_t vec_addr;
6402 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6403 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6404 vec_addr_offset;
6405 memset ((void *) vec_addr, 0,
6406 (iter + 1 - old_size) * sizeof (int));
6407 }
6408 elem = VEC_index (int, inf->syscalls_counts, iter);
6409 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6410 }
6411 }
6412
77b06cd7
TJB
6413 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6414 inf->total_syscalls_count != 0,
6415 inf->any_syscall_count,
6416 VEC_length (int, inf->syscalls_counts),
6417 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6418}
6419
6420/* Implement the "remove" breakpoint_ops method for syscall
6421 catchpoints. */
6422
6423static int
77b06cd7 6424remove_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6425{
6426 struct inferior *inf = current_inferior ();
6427
6428 --inf->total_syscalls_count;
77b06cd7 6429 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6430 --inf->any_syscall_count;
6431 else
6432 {
6433 int i, iter;
cc59ec59 6434
a96d9b2e 6435 for (i = 0;
77b06cd7 6436 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6437 i++)
6438 {
6439 int elem;
6440 if (iter >= VEC_length (int, inf->syscalls_counts))
6441 /* Shouldn't happen. */
6442 continue;
6443 elem = VEC_index (int, inf->syscalls_counts, iter);
6444 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6445 }
6446 }
6447
6448 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6449 inf->total_syscalls_count != 0,
6450 inf->any_syscall_count,
6451 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6452 VEC_address (int,
6453 inf->syscalls_counts));
a96d9b2e
SDJ
6454}
6455
6456/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6457 catchpoints. */
6458
6459static int
6460breakpoint_hit_catch_syscall (struct breakpoint *b)
6461{
4a64f543
MS
6462 /* We must check if we are catching specific syscalls in this
6463 breakpoint. If we are, then we must guarantee that the called
6464 syscall is the same syscall we are catching. */
a96d9b2e
SDJ
6465 int syscall_number = 0;
6466
6467 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6468 return 0;
6469
6470 /* Now, checking if the syscall is the same. */
6471 if (b->syscalls_to_be_caught)
6472 {
6473 int i, iter;
cc59ec59 6474
a96d9b2e
SDJ
6475 for (i = 0;
6476 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6477 i++)
6478 if (syscall_number == iter)
6479 break;
6480 /* Not the same. */
6481 if (!iter)
6482 return 0;
6483 }
6484
6485 return 1;
6486}
6487
6488/* Implement the "print_it" breakpoint_ops method for syscall
6489 catchpoints. */
6490
6491static enum print_stop_action
6492print_it_catch_syscall (struct breakpoint *b)
6493{
6494 /* These are needed because we want to know in which state a
6495 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6496 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6497 must print "called syscall" or "returned from syscall". */
6498 ptid_t ptid;
6499 struct target_waitstatus last;
6500 struct syscall s;
6501 struct cleanup *old_chain;
6502 char *syscall_id;
6503
6504 get_last_target_status (&ptid, &last);
6505
6506 get_syscall_by_number (last.value.syscall_number, &s);
6507
6508 annotate_catchpoint (b->number);
6509
6510 if (s.name == NULL)
6511 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6512 else
6513 syscall_id = xstrprintf ("'%s'", s.name);
6514
6515 old_chain = make_cleanup (xfree, syscall_id);
6516
6517 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6518 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6519 b->number, syscall_id);
6520 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6521 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6522 b->number, syscall_id);
6523
6524 do_cleanups (old_chain);
6525
6526 return PRINT_SRC_AND_LOC;
6527}
6528
6529/* Implement the "print_one" breakpoint_ops method for syscall
6530 catchpoints. */
6531
6532static void
6533print_one_catch_syscall (struct breakpoint *b,
6534 struct bp_location **last_loc)
6535{
6536 struct value_print_options opts;
6537
6538 get_user_print_options (&opts);
4a64f543
MS
6539 /* Field 4, the address, is omitted (which makes the columns not
6540 line up too nicely with the headers, but the effect is relatively
6541 readable). */
a96d9b2e
SDJ
6542 if (opts.addressprint)
6543 ui_out_field_skip (uiout, "addr");
6544 annotate_field (5);
6545
6546 if (b->syscalls_to_be_caught
6547 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6548 ui_out_text (uiout, "syscalls \"");
6549 else
6550 ui_out_text (uiout, "syscall \"");
6551
6552 if (b->syscalls_to_be_caught)
6553 {
6554 int i, iter;
6555 char *text = xstrprintf ("%s", "");
cc59ec59 6556
a96d9b2e
SDJ
6557 for (i = 0;
6558 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6559 i++)
6560 {
6561 char *x = text;
6562 struct syscall s;
6563 get_syscall_by_number (iter, &s);
6564
6565 if (s.name != NULL)
6566 text = xstrprintf ("%s%s, ", text, s.name);
6567 else
6568 text = xstrprintf ("%s%d, ", text, iter);
6569
6570 /* We have to xfree the last 'text' (now stored at 'x')
6571 because xstrprintf dinamically allocates new space for it
6572 on every call. */
6573 xfree (x);
6574 }
6575 /* Remove the last comma. */
6576 text[strlen (text) - 2] = '\0';
6577 ui_out_field_string (uiout, "what", text);
6578 }
6579 else
6580 ui_out_field_string (uiout, "what", "<any syscall>");
6581 ui_out_text (uiout, "\" ");
6582}
6583
6584/* Implement the "print_mention" breakpoint_ops method for syscall
6585 catchpoints. */
6586
6587static void
6588print_mention_catch_syscall (struct breakpoint *b)
6589{
6590 if (b->syscalls_to_be_caught)
6591 {
6592 int i, iter;
6593
6594 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6595 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6596 else
6597 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6598
6599 for (i = 0;
6600 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6601 i++)
6602 {
6603 struct syscall s;
6604 get_syscall_by_number (iter, &s);
6605
6606 if (s.name)
6607 printf_filtered (" '%s' [%d]", s.name, s.number);
6608 else
6609 printf_filtered (" %d", s.number);
6610 }
6611 printf_filtered (")");
6612 }
6613 else
6614 printf_filtered (_("Catchpoint %d (any syscall)"),
6615 b->number);
6616}
6617
6149aea9
PA
6618/* Implement the "print_recreate" breakpoint_ops method for syscall
6619 catchpoints. */
6620
6621static void
6622print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6623{
6624 fprintf_unfiltered (fp, "catch syscall");
6625
6626 if (b->syscalls_to_be_caught)
6627 {
6628 int i, iter;
6629
6630 for (i = 0;
6631 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6632 i++)
6633 {
6634 struct syscall s;
6635
6636 get_syscall_by_number (iter, &s);
6637 if (s.name)
6638 fprintf_unfiltered (fp, " %s", s.name);
6639 else
6640 fprintf_unfiltered (fp, " %d", s.number);
6641 }
6642 }
6643}
6644
a96d9b2e
SDJ
6645/* The breakpoint_ops structure to be used in syscall catchpoints. */
6646
6647static struct breakpoint_ops catch_syscall_breakpoint_ops =
6648{
6649 insert_catch_syscall,
6650 remove_catch_syscall,
6651 breakpoint_hit_catch_syscall,
e09342b5 6652 NULL, /* resources_needed */
a96d9b2e
SDJ
6653 print_it_catch_syscall,
6654 print_one_catch_syscall,
6149aea9
PA
6655 print_mention_catch_syscall,
6656 print_recreate_catch_syscall
a96d9b2e
SDJ
6657};
6658
6659/* Returns non-zero if 'b' is a syscall catchpoint. */
6660
6661static int
6662syscall_catchpoint_p (struct breakpoint *b)
6663{
6664 return (b->ops == &catch_syscall_breakpoint_ops);
6665}
6666
6667/* Create a new breakpoint of the bp_catchpoint kind and return it,
6668 but does NOT mention it nor update the global location list.
6669 This is useful if you need to fill more fields in the
6670 struct breakpoint before calling mention.
ce78b96d
JB
6671
6672 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6673 If COND_STRING is not NULL, then store it in the breakpoint.
6674 OPS, if not NULL, is the breakpoint_ops structure associated
6675 to the catchpoint. */
6676
6677static struct breakpoint *
a96d9b2e
SDJ
6678create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6679 char *cond_string,
6680 struct breakpoint_ops *ops)
c906108c 6681{
c5aa993b
JM
6682 struct symtab_and_line sal;
6683 struct breakpoint *b;
c5aa993b 6684
fe39c653 6685 init_sal (&sal);
6c95b8df 6686 sal.pspace = current_program_space;
c5aa993b 6687
a6d9a66e 6688 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6689 set_breakpoint_count (breakpoint_count + 1);
6690 b->number = breakpoint_count;
ce78b96d 6691
1b36a34b 6692 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6693 b->thread = -1;
c906108c 6694 b->addr_string = NULL;
b5de0fa7
EZ
6695 b->enable_state = bp_enabled;
6696 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6697 b->ops = ops;
c5aa993b 6698
a96d9b2e
SDJ
6699 return b;
6700}
6701
6702/* Create a new breakpoint of the bp_catchpoint kind and return it.
6703
6704 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6705 If COND_STRING is not NULL, then store it in the breakpoint.
6706 OPS, if not NULL, is the breakpoint_ops structure associated
6707 to the catchpoint. */
6708
6709static struct breakpoint *
6710create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6711 char *cond_string, struct breakpoint_ops *ops)
6712{
6713 struct breakpoint *b =
6714 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6715
c906108c 6716 mention (b);
ce78b96d 6717 update_global_location_list (1);
c906108c 6718
ce78b96d 6719 return b;
c906108c 6720}
c5aa993b 6721
9b70b993 6722static void
a6d9a66e
UW
6723create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6724 int tempflag, char *cond_string,
ce78b96d 6725 struct breakpoint_ops *ops)
c906108c 6726{
a6d9a66e
UW
6727 struct breakpoint *b
6728 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6729
6730 /* FIXME: We should put this information in a breakpoint private data
6731 area. */
6732 b->forked_inferior_pid = null_ptid;
c906108c
SS
6733}
6734
fe798b75
JB
6735/* Exec catchpoints. */
6736
77b06cd7
TJB
6737static int
6738insert_catch_exec (struct bp_location *bl)
c906108c 6739{
77b06cd7 6740 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6741}
c906108c 6742
fe798b75 6743static int
77b06cd7 6744remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6745{
6746 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6747}
c906108c 6748
fe798b75
JB
6749static int
6750breakpoint_hit_catch_exec (struct breakpoint *b)
6751{
6752 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
6753}
c906108c 6754
fe798b75
JB
6755static enum print_stop_action
6756print_it_catch_exec (struct breakpoint *b)
6757{
6758 annotate_catchpoint (b->number);
6759 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6760 b->exec_pathname);
6761 return PRINT_SRC_AND_LOC;
c906108c
SS
6762}
6763
fe798b75 6764static void
a6d9a66e 6765print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6766{
6767 struct value_print_options opts;
6768
6769 get_user_print_options (&opts);
6770
6771 /* Field 4, the address, is omitted (which makes the columns
6772 not line up too nicely with the headers, but the effect
6773 is relatively readable). */
6774 if (opts.addressprint)
6775 ui_out_field_skip (uiout, "addr");
6776 annotate_field (5);
6777 ui_out_text (uiout, "exec");
6778 if (b->exec_pathname != NULL)
6779 {
6780 ui_out_text (uiout, ", program \"");
6781 ui_out_field_string (uiout, "what", b->exec_pathname);
6782 ui_out_text (uiout, "\" ");
6783 }
6784}
6785
6786static void
6787print_mention_catch_exec (struct breakpoint *b)
6788{
6789 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6790}
6791
6149aea9
PA
6792/* Implement the "print_recreate" breakpoint_ops method for exec
6793 catchpoints. */
6794
6795static void
6796print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6797{
6798 fprintf_unfiltered (fp, "catch exec");
6799}
6800
fe798b75
JB
6801static struct breakpoint_ops catch_exec_breakpoint_ops =
6802{
6803 insert_catch_exec,
6804 remove_catch_exec,
6805 breakpoint_hit_catch_exec,
e09342b5 6806 NULL, /* resources_needed */
fe798b75
JB
6807 print_it_catch_exec,
6808 print_one_catch_exec,
6149aea9
PA
6809 print_mention_catch_exec,
6810 print_recreate_catch_exec
fe798b75
JB
6811};
6812
a96d9b2e
SDJ
6813static void
6814create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6815 struct breakpoint_ops *ops)
6816{
6817 struct gdbarch *gdbarch = get_current_arch ();
6818 struct breakpoint *b =
6819 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6820
6821 b->syscalls_to_be_caught = filter;
6822
6823 /* Now, we have to mention the breakpoint and update the global
6824 location list. */
6825 mention (b);
6826 update_global_location_list (1);
6827}
6828
c906108c 6829static int
fba45db2 6830hw_breakpoint_used_count (void)
c906108c 6831{
52f0bd74 6832 struct breakpoint *b;
c906108c
SS
6833 int i = 0;
6834
6835 ALL_BREAKPOINTS (b)
c5aa993b 6836 {
d6b74ac4 6837 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
6838 i++;
6839 }
c906108c
SS
6840
6841 return i;
6842}
6843
6844static int
fba45db2 6845hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6846{
c906108c 6847 int i = 0;
e09342b5
TJB
6848 struct breakpoint *b;
6849 struct bp_location *bl;
c906108c
SS
6850
6851 *other_type_used = 0;
6852 ALL_BREAKPOINTS (b)
e09342b5
TJB
6853 {
6854 if (!breakpoint_enabled (b))
6855 continue;
6856
c5aa993b 6857 if (b->type == type)
e09342b5
TJB
6858 for (bl = b->loc; bl; bl = bl->next)
6859 {
6860 /* Special types of hardware watchpoints may use more than
6861 one register. */
6862 if (b->ops && b->ops->resources_needed)
6863 i += b->ops->resources_needed (bl);
6864 else
6865 i++;
6866 }
cc60f2e3 6867 else if (is_hardware_watchpoint (b))
c5aa993b 6868 *other_type_used = 1;
e09342b5
TJB
6869 }
6870
c906108c
SS
6871 return i;
6872}
6873
c906108c 6874void
fba45db2 6875disable_watchpoints_before_interactive_call_start (void)
c906108c 6876{
c5aa993b 6877 struct breakpoint *b;
c906108c
SS
6878
6879 ALL_BREAKPOINTS (b)
c5aa993b 6880 {
cc60f2e3 6881 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6882 {
b5de0fa7 6883 b->enable_state = bp_call_disabled;
b60e7edf 6884 update_global_location_list (0);
c5aa993b
JM
6885 }
6886 }
c906108c
SS
6887}
6888
6889void
fba45db2 6890enable_watchpoints_after_interactive_call_stop (void)
c906108c 6891{
c5aa993b 6892 struct breakpoint *b;
c906108c
SS
6893
6894 ALL_BREAKPOINTS (b)
c5aa993b 6895 {
cc60f2e3 6896 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6897 {
b5de0fa7 6898 b->enable_state = bp_enabled;
b60e7edf 6899 update_global_location_list (1);
c5aa993b
JM
6900 }
6901 }
c906108c
SS
6902}
6903
8bea4e01
UW
6904void
6905disable_breakpoints_before_startup (void)
6906{
6907 struct breakpoint *b;
6908 int found = 0;
6909
6910 ALL_BREAKPOINTS (b)
6911 {
6c95b8df
PA
6912 if (b->pspace != current_program_space)
6913 continue;
6914
8bea4e01
UW
6915 if ((b->type == bp_breakpoint
6916 || b->type == bp_hardware_breakpoint)
6917 && breakpoint_enabled (b))
6918 {
6919 b->enable_state = bp_startup_disabled;
6920 found = 1;
6921 }
6922 }
6923
6924 if (found)
6925 update_global_location_list (0);
6926
6c95b8df 6927 current_program_space->executing_startup = 1;
8bea4e01
UW
6928}
6929
6930void
6931enable_breakpoints_after_startup (void)
6932{
6933 struct breakpoint *b;
6934 int found = 0;
6935
6c95b8df 6936 current_program_space->executing_startup = 0;
8bea4e01
UW
6937
6938 ALL_BREAKPOINTS (b)
6939 {
6c95b8df
PA
6940 if (b->pspace != current_program_space)
6941 continue;
6942
8bea4e01
UW
6943 if ((b->type == bp_breakpoint
6944 || b->type == bp_hardware_breakpoint)
6945 && b->enable_state == bp_startup_disabled)
6946 {
6947 b->enable_state = bp_enabled;
6948 found = 1;
6949 }
6950 }
6951
6952 if (found)
6953 breakpoint_re_set ();
6954}
6955
c906108c
SS
6956
6957/* Set a breakpoint that will evaporate an end of command
6958 at address specified by SAL.
6959 Restrict it to frame FRAME if FRAME is nonzero. */
6960
6961struct breakpoint *
a6d9a66e
UW
6962set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6963 struct frame_id frame_id, enum bptype type)
c906108c 6964{
52f0bd74 6965 struct breakpoint *b;
edb3359d
DJ
6966
6967 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6968 one. */
6969 gdb_assert (!frame_id_inlined_p (frame_id));
6970
a6d9a66e 6971 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
6972 b->enable_state = bp_enabled;
6973 b->disposition = disp_donttouch;
818dd999 6974 b->frame_id = frame_id;
c906108c 6975
4a64f543
MS
6976 /* If we're debugging a multi-threaded program, then we want
6977 momentary breakpoints to be active in only a single thread of
6978 control. */
39f77062
KB
6979 if (in_thread_list (inferior_ptid))
6980 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 6981
b60e7edf 6982 update_global_location_list_nothrow (1);
74960c60 6983
c906108c
SS
6984 return b;
6985}
611c83ae 6986
e58b0e63
PA
6987/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
6988 ORIG is NULL. */
6989
6990struct breakpoint *
6991clone_momentary_breakpoint (struct breakpoint *orig)
6992{
6993 struct breakpoint *copy;
6994
6995 /* If there's nothing to clone, then return nothing. */
6996 if (orig == NULL)
6997 return NULL;
6998
a6d9a66e 6999 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63 7000 copy->loc = allocate_bp_location (copy);
0e30163f 7001 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7002
a6d9a66e 7003 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7004 copy->loc->requested_address = orig->loc->requested_address;
7005 copy->loc->address = orig->loc->address;
7006 copy->loc->section = orig->loc->section;
6c95b8df 7007 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7008
7009 if (orig->source_file == NULL)
7010 copy->source_file = NULL;
7011 else
7012 copy->source_file = xstrdup (orig->source_file);
7013
7014 copy->line_number = orig->line_number;
7015 copy->frame_id = orig->frame_id;
7016 copy->thread = orig->thread;
6c95b8df 7017 copy->pspace = orig->pspace;
e58b0e63
PA
7018
7019 copy->enable_state = bp_enabled;
7020 copy->disposition = disp_donttouch;
7021 copy->number = internal_breakpoint_number--;
7022
7023 update_global_location_list_nothrow (0);
7024 return copy;
7025}
7026
611c83ae 7027struct breakpoint *
a6d9a66e
UW
7028set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7029 enum bptype type)
611c83ae
PA
7030{
7031 struct symtab_and_line sal;
7032
7033 sal = find_pc_line (pc, 0);
7034 sal.pc = pc;
7035 sal.section = find_pc_overlay (pc);
7036 sal.explicit_pc = 1;
7037
a6d9a66e 7038 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7039}
c906108c 7040\f
c5aa993b 7041
c906108c
SS
7042/* Tell the user we have just set a breakpoint B. */
7043
7044static void
fba45db2 7045mention (struct breakpoint *b)
c906108c
SS
7046{
7047 int say_where = 0;
fa8a61dc 7048 struct cleanup *ui_out_chain;
79a45b7d
TT
7049 struct value_print_options opts;
7050
7051 get_user_print_options (&opts);
8b93c638 7052
9a4105ab
AC
7053 /* FIXME: This is misplaced; mention() is called by things (like
7054 hitting a watchpoint) other than breakpoint creation. It should
7055 be possible to clean this up and at the same time replace the
7f4b89d1 7056 random calls to breakpoint_changed with this hook. */
383f836e 7057 observer_notify_breakpoint_created (b->number);
c906108c 7058
3086aeae
DJ
7059 if (b->ops != NULL && b->ops->print_mention != NULL)
7060 b->ops->print_mention (b);
7061 else
7062 switch (b->type)
7063 {
7064 case bp_none:
3e43a32a
MS
7065 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7066 b->number);
3086aeae
DJ
7067 break;
7068 case bp_watchpoint:
7069 ui_out_text (uiout, "Watchpoint ");
7070 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7071 ui_out_field_int (uiout, "number", b->number);
7072 ui_out_text (uiout, ": ");
fa8a61dc 7073 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7074 do_cleanups (ui_out_chain);
7075 break;
7076 case bp_hardware_watchpoint:
7077 ui_out_text (uiout, "Hardware watchpoint ");
7078 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7079 ui_out_field_int (uiout, "number", b->number);
7080 ui_out_text (uiout, ": ");
fa8a61dc 7081 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7082 do_cleanups (ui_out_chain);
7083 break;
7084 case bp_read_watchpoint:
7085 ui_out_text (uiout, "Hardware read watchpoint ");
7086 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7087 ui_out_field_int (uiout, "number", b->number);
7088 ui_out_text (uiout, ": ");
fa8a61dc 7089 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7090 do_cleanups (ui_out_chain);
7091 break;
7092 case bp_access_watchpoint:
7093 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7094 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7095 ui_out_field_int (uiout, "number", b->number);
7096 ui_out_text (uiout, ": ");
fa8a61dc 7097 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7098 do_cleanups (ui_out_chain);
7099 break;
7100 case bp_breakpoint:
0e30163f 7101 case bp_gnu_ifunc_resolver:
3086aeae
DJ
7102 if (ui_out_is_mi_like_p (uiout))
7103 {
7104 say_where = 0;
7105 break;
7106 }
2cec12e5
AR
7107 if (b->disposition == disp_del)
7108 printf_filtered (_("Temporary breakpoint"));
7109 else
7110 printf_filtered (_("Breakpoint"));
7111 printf_filtered (_(" %d"), b->number);
0e30163f
JK
7112 if (b->type == bp_gnu_ifunc_resolver)
7113 printf_filtered (_(" at gnu-indirect-function resolver"));
3086aeae
DJ
7114 say_where = 1;
7115 break;
7116 case bp_hardware_breakpoint:
7117 if (ui_out_is_mi_like_p (uiout))
7118 {
7119 say_where = 0;
7120 break;
7121 }
a3f17187 7122 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7123 say_where = 1;
7124 break;
1042e4c0
SS
7125 case bp_tracepoint:
7126 if (ui_out_is_mi_like_p (uiout))
7127 {
7128 say_where = 0;
7129 break;
7130 }
7131 printf_filtered (_("Tracepoint"));
7132 printf_filtered (_(" %d"), b->number);
7133 say_where = 1;
7134 break;
7a697b8d
SS
7135 case bp_fast_tracepoint:
7136 if (ui_out_is_mi_like_p (uiout))
7137 {
7138 say_where = 0;
7139 break;
7140 }
7141 printf_filtered (_("Fast tracepoint"));
7142 printf_filtered (_(" %d"), b->number);
7143 say_where = 1;
7144 break;
0fb4aa4b
PA
7145 case bp_static_tracepoint:
7146 if (ui_out_is_mi_like_p (uiout))
7147 {
7148 say_where = 0;
7149 break;
7150 }
7151 printf_filtered (_("Static tracepoint"));
7152 printf_filtered (_(" %d"), b->number);
7153 say_where = 1;
7154 break;
3086aeae
DJ
7155
7156 case bp_until:
7157 case bp_finish:
7158 case bp_longjmp:
7159 case bp_longjmp_resume:
186c406b
TT
7160 case bp_exception:
7161 case bp_exception_resume:
3086aeae 7162 case bp_step_resume:
3086aeae 7163 case bp_call_dummy:
aa7d318d 7164 case bp_std_terminate:
3086aeae
DJ
7165 case bp_watchpoint_scope:
7166 case bp_shlib_event:
7167 case bp_thread_event:
7168 case bp_overlay_event:
4efc6507 7169 case bp_jit_event:
0fd8e87f 7170 case bp_longjmp_master:
aa7d318d 7171 case bp_std_terminate_master:
186c406b 7172 case bp_exception_master:
0e30163f 7173 case bp_gnu_ifunc_resolver_return:
3086aeae
DJ
7174 break;
7175 }
c906108c 7176
c906108c
SS
7177 if (say_where)
7178 {
a3f17187
AC
7179 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7180 single string. */
0d381245 7181 if (b->loc == NULL)
c906108c 7182 {
a3f17187 7183 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7184 }
7185 else
7186 {
79a45b7d 7187 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7188 {
7189 printf_filtered (" at ");
5af949e3
UW
7190 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7191 gdb_stdout);
0101ce28
JJ
7192 }
7193 if (b->source_file)
7194 printf_filtered (": file %s, line %d.",
7195 b->source_file, b->line_number);
0d381245
VP
7196
7197 if (b->loc->next)
7198 {
7199 struct bp_location *loc = b->loc;
7200 int n = 0;
7201 for (; loc; loc = loc->next)
7202 ++n;
7203 printf_filtered (" (%d locations)", n);
7204 }
7205
c906108c 7206 }
c906108c 7207 }
9dc5e2a9 7208 if (ui_out_is_mi_like_p (uiout))
fb40c209 7209 return;
c906108c
SS
7210 printf_filtered ("\n");
7211}
c906108c 7212\f
c5aa993b 7213
0d381245 7214static struct bp_location *
39d61571 7215add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7216 const struct symtab_and_line *sal)
7217{
7218 struct bp_location *loc, **tmp;
7219
39d61571 7220 loc = allocate_bp_location (b);
0d381245
VP
7221 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7222 ;
7223 *tmp = loc;
a6d9a66e
UW
7224 loc->gdbarch = get_sal_arch (*sal);
7225 if (!loc->gdbarch)
7226 loc->gdbarch = b->gdbarch;
0d381245 7227 loc->requested_address = sal->pc;
a6d9a66e
UW
7228 loc->address = adjust_breakpoint_address (loc->gdbarch,
7229 loc->requested_address, b->type);
6c95b8df
PA
7230 loc->pspace = sal->pspace;
7231 gdb_assert (loc->pspace != NULL);
0d381245
VP
7232 loc->section = sal->section;
7233
0e30163f
JK
7234 set_breakpoint_location_function (loc,
7235 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7236 return loc;
7237}
514f746b
AR
7238\f
7239
7240/* Return 1 if LOC is pointing to a permanent breakpoint,
7241 return 0 otherwise. */
7242
7243static int
7244bp_loc_is_permanent (struct bp_location *loc)
7245{
7246 int len;
7247 CORE_ADDR addr;
7248 const gdb_byte *brk;
7249 gdb_byte *target_mem;
939c61fa
JK
7250 struct cleanup *cleanup;
7251 int retval = 0;
514f746b
AR
7252
7253 gdb_assert (loc != NULL);
7254
7255 addr = loc->address;
a6d9a66e 7256 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7257
939c61fa
JK
7258 /* Software breakpoints unsupported? */
7259 if (brk == NULL)
7260 return 0;
7261
514f746b
AR
7262 target_mem = alloca (len);
7263
939c61fa
JK
7264 /* Enable the automatic memory restoration from breakpoints while
7265 we read the memory. Otherwise we could say about our temporary
7266 breakpoints they are permanent. */
6c95b8df
PA
7267 cleanup = save_current_space_and_thread ();
7268
7269 switch_to_program_space_and_thread (loc->pspace);
7270 make_show_memory_breakpoints_cleanup (0);
939c61fa 7271
514f746b
AR
7272 if (target_read_memory (loc->address, target_mem, len) == 0
7273 && memcmp (target_mem, brk, len) == 0)
939c61fa 7274 retval = 1;
514f746b 7275
939c61fa
JK
7276 do_cleanups (cleanup);
7277
7278 return retval;
514f746b
AR
7279}
7280
7281
c3f6f71d 7282
018d34a4
VP
7283/* Create a breakpoint with SAL as location. Use ADDR_STRING
7284 as textual description of the location, and COND_STRING
db107f19 7285 as condition expression. */
018d34a4
VP
7286
7287static void
8cdf0e15
VP
7288create_breakpoint_sal (struct gdbarch *gdbarch,
7289 struct symtabs_and_lines sals, char *addr_string,
7290 char *cond_string,
7291 enum bptype type, enum bpdisp disposition,
7292 int thread, int task, int ignore_count,
84f4c1fe 7293 struct breakpoint_ops *ops, int from_tty,
56435ebe 7294 int enabled, int internal, int display_canonical)
018d34a4 7295{
0d381245
VP
7296 struct breakpoint *b = NULL;
7297 int i;
018d34a4
VP
7298
7299 if (type == bp_hardware_breakpoint)
7300 {
7301 int i = hw_breakpoint_used_count ();
7302 int target_resources_ok =
d92524f1 7303 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7304 i + 1, 0);
7305 if (target_resources_ok == 0)
7306 error (_("No hardware breakpoint support in the target."));
7307 else if (target_resources_ok < 0)
7308 error (_("Hardware breakpoints used exceeds limit."));
7309 }
7310
6c95b8df
PA
7311 gdb_assert (sals.nelts > 0);
7312
0d381245
VP
7313 for (i = 0; i < sals.nelts; ++i)
7314 {
7315 struct symtab_and_line sal = sals.sals[i];
7316 struct bp_location *loc;
7317
7318 if (from_tty)
5af949e3
UW
7319 {
7320 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7321 if (!loc_gdbarch)
7322 loc_gdbarch = gdbarch;
7323
7324 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7325 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7326 }
0d381245
VP
7327
7328 if (i == 0)
7329 {
a6d9a66e 7330 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7331 set_breakpoint_number (internal, b);
0d381245 7332 b->thread = thread;
4a306c9a 7333 b->task = task;
018d34a4 7334
0d381245
VP
7335 b->cond_string = cond_string;
7336 b->ignore_count = ignore_count;
41447f92 7337 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7338 b->disposition = disposition;
6c95b8df
PA
7339 b->pspace = sals.sals[0].pspace;
7340
0fb4aa4b
PA
7341 if (type == bp_static_tracepoint)
7342 {
7343 struct static_tracepoint_marker marker;
7344
7345 if (is_marker_spec (addr_string))
7346 {
7347 /* We already know the marker exists, otherwise, we
7348 wouldn't see a sal for it. */
7349 char *p = &addr_string[3];
7350 char *endp;
7351 char *marker_str;
7352 int i;
7353
e9cafbcc 7354 p = skip_spaces (p);
0fb4aa4b 7355
e9cafbcc 7356 endp = skip_to_space (p);
0fb4aa4b
PA
7357
7358 marker_str = savestring (p, endp - p);
7359 b->static_trace_marker_id = marker_str;
7360
3e43a32a
MS
7361 printf_filtered (_("Probed static tracepoint "
7362 "marker \"%s\"\n"),
0fb4aa4b
PA
7363 b->static_trace_marker_id);
7364 }
7365 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7366 {
7367 b->static_trace_marker_id = xstrdup (marker.str_id);
7368 release_static_tracepoint_marker (&marker);
7369
3e43a32a
MS
7370 printf_filtered (_("Probed static tracepoint "
7371 "marker \"%s\"\n"),
0fb4aa4b
PA
7372 b->static_trace_marker_id);
7373 }
7374 else
3e43a32a
MS
7375 warning (_("Couldn't determine the static "
7376 "tracepoint marker to probe"));
0fb4aa4b
PA
7377 }
7378
6c95b8df 7379 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7380 && (b->type == bp_breakpoint
7381 || b->type == bp_hardware_breakpoint))
7382 b->enable_state = bp_startup_disabled;
7383
0d381245
VP
7384 loc = b->loc;
7385 }
7386 else
018d34a4 7387 {
39d61571 7388 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7389 }
7390
514f746b
AR
7391 if (bp_loc_is_permanent (loc))
7392 make_breakpoint_permanent (b);
7393
0d381245
VP
7394 if (b->cond_string)
7395 {
7396 char *arg = b->cond_string;
d32a6982 7397 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7398 if (*arg)
db107f19 7399 error (_("Garbage %s follows condition"), arg);
018d34a4 7400 }
0d381245 7401 }
018d34a4 7402
56435ebe 7403 b->display_canonical = display_canonical;
018d34a4
VP
7404 if (addr_string)
7405 b->addr_string = addr_string;
7406 else
7407 /* addr_string has to be used or breakpoint_re_set will delete
7408 me. */
5af949e3
UW
7409 b->addr_string
7410 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7411
604133b5 7412 b->ops = ops;
84f4c1fe
PM
7413 if (internal)
7414 /* Do not mention breakpoints with a negative number, but do
7415 notify observers. */
7416 observer_notify_breakpoint_created (b->number);
7417 else
7418 mention (b);
018d34a4
VP
7419}
7420
ed0616c6
VP
7421/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7422 elements to fill the void space. */
2c0b251b
PA
7423static void
7424remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7425{
7426 int i = index_to_remove+1;
7427 int last_index = sal->nelts-1;
7428
7429 for (;i <= last_index; ++i)
7430 sal->sals[i-1] = sal->sals[i];
7431
7432 --(sal->nelts);
7433}
7434
6c95b8df
PA
7435/* If appropriate, obtains all sals that correspond to the same file
7436 and line as SAL, in all program spaces. Users debugging with IDEs,
7437 will want to set a breakpoint at foo.c:line, and not really care
7438 about program spaces. This is done only if SAL does not have
7439 explicit PC and has line and file information. If we got just a
7440 single expanded sal, return the original.
ed0616c6 7441
6c95b8df
PA
7442 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7443 which the name of enclosing function is different from SAL. This
7444 makes sure that if we have breakpoint originally set in template
7445 instantiation, say foo<int>(), we won't expand SAL to locations at
7446 the same line in all existing instantiations of 'foo'. */
ed0616c6 7447
2c0b251b 7448static struct symtabs_and_lines
ed0616c6
VP
7449expand_line_sal_maybe (struct symtab_and_line sal)
7450{
7451 struct symtabs_and_lines expanded;
7452 CORE_ADDR original_pc = sal.pc;
7453 char *original_function = NULL;
7454 int found;
7455 int i;
6c95b8df 7456 struct cleanup *old_chain;
ed0616c6
VP
7457
7458 /* If we have explicit pc, don't expand.
7459 If we have no line number, we can't expand. */
7460 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7461 {
7462 expanded.nelts = 1;
7463 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7464 expanded.sals[0] = sal;
7465 return expanded;
7466 }
7467
7468 sal.pc = 0;
6c95b8df
PA
7469
7470 old_chain = save_current_space_and_thread ();
7471
7472 switch_to_program_space_and_thread (sal.pspace);
7473
ed0616c6 7474 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7475
7476 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7477 expanded = expand_line_sal (sal);
6c95b8df 7478
ed0616c6
VP
7479 if (expanded.nelts == 1)
7480 {
3dba1c98
JB
7481 /* We had one sal, we got one sal. Return that sal, adjusting it
7482 past the function prologue if necessary. */
ed0616c6
VP
7483 xfree (expanded.sals);
7484 expanded.nelts = 1;
7485 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7486 sal.pc = original_pc;
7487 expanded.sals[0] = sal;
3dba1c98 7488 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7489 do_cleanups (old_chain);
ed0616c6
VP
7490 return expanded;
7491 }
7492
7493 if (!sal.explicit_line)
7494 {
7495 CORE_ADDR func_addr, func_end;
7496 for (i = 0; i < expanded.nelts; ++i)
7497 {
7498 CORE_ADDR pc = expanded.sals[i].pc;
7499 char *this_function;
6c95b8df
PA
7500
7501 /* We need to switch threads as well since we're about to
7502 read memory. */
7503 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7504
ed0616c6
VP
7505 if (find_pc_partial_function (pc, &this_function,
7506 &func_addr, &func_end))
7507 {
059fb39f
PM
7508 if (this_function
7509 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7510 {
7511 remove_sal (&expanded, i);
7512 --i;
7513 }
ed0616c6
VP
7514 }
7515 }
7516 }
059acae7
UW
7517
7518 /* Skip the function prologue if necessary. */
7519 for (i = 0; i < expanded.nelts; ++i)
7520 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7521
6c95b8df
PA
7522 do_cleanups (old_chain);
7523
ed0616c6
VP
7524 if (expanded.nelts <= 1)
7525 {
4a64f543
MS
7526 /* This is un ugly workaround. If we get zero expanded sals
7527 then something is really wrong. Fix that by returning the
7528 original sal. */
7529
ed0616c6
VP
7530 xfree (expanded.sals);
7531 expanded.nelts = 1;
7532 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7533 sal.pc = original_pc;
7534 expanded.sals[0] = sal;
7535 return expanded;
7536 }
7537
7538 if (original_pc)
7539 {
7540 found = 0;
7541 for (i = 0; i < expanded.nelts; ++i)
7542 if (expanded.sals[i].pc == original_pc)
7543 {
7544 found = 1;
7545 break;
7546 }
7547 gdb_assert (found);
7548 }
7549
7550 return expanded;
7551}
7552
018d34a4
VP
7553/* Add SALS.nelts breakpoints to the breakpoint table. For each
7554 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7555 value. COND_STRING, if not NULL, specified the condition to be
7556 used for all breakpoints. Essentially the only case where
7557 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7558 function. In that case, it's still not possible to specify
7559 separate conditions for different overloaded functions, so
7560 we take just a single condition string.
7561
c3f6f71d 7562 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7563 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7564 array contents). If the function fails (error() is called), the
7565 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7566 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7567
7568static void
8cdf0e15 7569create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7570 struct symtabs_and_lines sals,
7571 struct linespec_result *canonical,
8cdf0e15
VP
7572 char *cond_string,
7573 enum bptype type, enum bpdisp disposition,
7574 int thread, int task, int ignore_count,
7575 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7576 int enabled, int internal)
c906108c 7577{
018d34a4 7578 int i;
cc59ec59 7579
018d34a4 7580 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7581 {
ed0616c6
VP
7582 struct symtabs_and_lines expanded =
7583 expand_line_sal_maybe (sals.sals[i]);
0d381245 7584
7efd8fc2 7585 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7586 cond_string, type, disposition,
84f4c1fe 7587 thread, task, ignore_count, ops,
56435ebe
TT
7588 from_tty, enabled, internal,
7589 canonical->special_display);
c3f6f71d 7590 }
c3f6f71d 7591}
c906108c 7592
9998af43 7593/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7594 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7595 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7596 address strings. ADDRESS points to the end of the SAL.
7597
7598 The array and the line spec strings are allocated on the heap, it is
7599 the caller's responsibility to free them. */
c906108c 7600
b9362cc7 7601static void
c3f6f71d
JM
7602parse_breakpoint_sals (char **address,
7603 struct symtabs_and_lines *sals,
7efd8fc2 7604 struct linespec_result *canonical,
0101ce28 7605 int *not_found_ptr)
c3f6f71d
JM
7606{
7607 char *addr_start = *address;
cc59ec59 7608
c3f6f71d 7609 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7610 breakpoint. */
c3f6f71d
JM
7611 if ((*address) == NULL
7612 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7613 {
7614 if (default_breakpoint_valid)
7615 {
c3f6f71d 7616 struct symtab_and_line sal;
cc59ec59 7617
4a64f543 7618 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7619 sals->sals = (struct symtab_and_line *)
c906108c
SS
7620 xmalloc (sizeof (struct symtab_and_line));
7621 sal.pc = default_breakpoint_address;
7622 sal.line = default_breakpoint_line;
7623 sal.symtab = default_breakpoint_symtab;
6c95b8df 7624 sal.pspace = default_breakpoint_pspace;
c5aa993b 7625 sal.section = find_pc_overlay (sal.pc);
00903456 7626
4a64f543
MS
7627 /* "break" without arguments is equivalent to "break *PC"
7628 where PC is the default_breakpoint_address. So make sure
7629 to set sal.explicit_pc to prevent GDB from trying to
7630 expand the list of sals to include all other instances
7631 with the same symtab and line. */
00903456
JK
7632 sal.explicit_pc = 1;
7633
c3f6f71d
JM
7634 sals->sals[0] = sal;
7635 sals->nelts = 1;
c906108c
SS
7636 }
7637 else
8a3fe4f8 7638 error (_("No default breakpoint address now."));
c906108c
SS
7639 }
7640 else
7641 {
c906108c 7642 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7643 current_source_symtab (which is decode_line_1's default).
7644 This should produce the results we want almost all of the
7645 time while leaving default_breakpoint_* alone.
7646
1aeae86e
AF
7647 ObjC: However, don't match an Objective-C method name which
7648 may have a '+' or '-' succeeded by a '[' */
0378c332 7649
c214a6fd 7650 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7651
c906108c 7652 if (default_breakpoint_valid
0378c332 7653 && (!cursal.symtab
1aeae86e
AF
7654 || ((strchr ("+-", (*address)[0]) != NULL)
7655 && ((*address)[1] != '['))))
c3f6f71d 7656 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
7efd8fc2 7657 default_breakpoint_line, canonical,
0101ce28 7658 not_found_ptr);
c906108c 7659 else
0101ce28 7660 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7efd8fc2 7661 canonical, not_found_ptr);
c906108c 7662 }
4a64f543 7663 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2
TT
7664 if (sals->nelts > 0 && canonical->canonical == NULL)
7665 canonical->canonical = xcalloc (sals->nelts, sizeof (char **));
c3f6f71d 7666 if (addr_start != (*address))
c906108c 7667 {
c3f6f71d 7668 int i;
cc59ec59 7669
c3f6f71d 7670 for (i = 0; i < sals->nelts; i++)
c906108c 7671 {
4a64f543 7672 /* Add the string if not present. */
7efd8fc2
TT
7673 if (canonical->canonical[i] == NULL)
7674 canonical->canonical[i] = savestring (addr_start,
7675 (*address) - addr_start);
c906108c
SS
7676 }
7677 }
c3f6f71d 7678}
c906108c 7679
c906108c 7680
c3f6f71d 7681/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7682 inserted as a breakpoint. If it can't throw an error. */
c906108c 7683
b9362cc7 7684static void
23e7acfb 7685breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7686{
7687 int i;
cc59ec59 7688
c3f6f71d 7689 for (i = 0; i < sals->nelts; i++)
ee53e872 7690 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7691}
7692
7a697b8d
SS
7693/* Fast tracepoints may have restrictions on valid locations. For
7694 instance, a fast tracepoint using a jump instead of a trap will
7695 likely have to overwrite more bytes than a trap would, and so can
7696 only be placed where the instruction is longer than the jump, or a
7697 multi-instruction sequence does not have a jump into the middle of
7698 it, etc. */
7699
7700static void
7701check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7702 struct symtabs_and_lines *sals)
7703{
7704 int i, rslt;
7705 struct symtab_and_line *sal;
7706 char *msg;
7707 struct cleanup *old_chain;
7708
7709 for (i = 0; i < sals->nelts; i++)
7710 {
7711 sal = &sals->sals[i];
7712
7713 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7714 NULL, &msg);
7715 old_chain = make_cleanup (xfree, msg);
7716
7717 if (!rslt)
7718 error (_("May not have a fast tracepoint at 0x%s%s"),
7719 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7720
7721 do_cleanups (old_chain);
7722 }
7723}
7724
05ff989b 7725static void
0101ce28
JJ
7726do_captured_parse_breakpoint (struct ui_out *ui, void *data)
7727{
7728 struct captured_parse_breakpoint_args *args = data;
7729
7efd8fc2 7730 parse_breakpoint_sals (args->arg_p, args->sals_p, args->canonical_p,
0101ce28 7731 args->not_found_ptr);
0101ce28
JJ
7732}
7733
018d34a4
VP
7734/* Given TOK, a string specification of condition and thread, as
7735 accepted by the 'break' command, extract the condition
7736 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7737 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7738 If no condition is found, *COND_STRING is set to NULL.
7739 If no thread is found, *THREAD is set to -1. */
7740static void
7741find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7742 char **cond_string, int *thread, int *task)
018d34a4
VP
7743{
7744 *cond_string = NULL;
7745 *thread = -1;
7746 while (tok && *tok)
7747 {
7748 char *end_tok;
7749 int toklen;
7750 char *cond_start = NULL;
7751 char *cond_end = NULL;
cc59ec59 7752
e9cafbcc 7753 tok = skip_spaces (tok);
018d34a4 7754
e9cafbcc 7755 end_tok = skip_to_space (tok);
018d34a4
VP
7756
7757 toklen = end_tok - tok;
7758
7759 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7760 {
f7545552
TT
7761 struct expression *expr;
7762
018d34a4 7763 tok = cond_start = end_tok + 1;
f7545552
TT
7764 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7765 xfree (expr);
018d34a4
VP
7766 cond_end = tok;
7767 *cond_string = savestring (cond_start,
7768 cond_end - cond_start);
7769 }
7770 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7771 {
7772 char *tmptok;
7773
7774 tok = end_tok + 1;
7775 tmptok = tok;
7776 *thread = strtol (tok, &tok, 0);
7777 if (tok == tmptok)
7778 error (_("Junk after thread keyword."));
7779 if (!valid_thread_id (*thread))
7780 error (_("Unknown thread %d."), *thread);
7781 }
4a306c9a
JB
7782 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7783 {
7784 char *tmptok;
7785
7786 tok = end_tok + 1;
7787 tmptok = tok;
7788 *task = strtol (tok, &tok, 0);
7789 if (tok == tmptok)
7790 error (_("Junk after task keyword."));
7791 if (!valid_task_id (*task))
b6199126 7792 error (_("Unknown task %d."), *task);
4a306c9a 7793 }
018d34a4
VP
7794 else
7795 error (_("Junk at end of arguments."));
7796 }
7797}
7798
0fb4aa4b
PA
7799/* Decode a static tracepoint marker spec. */
7800
7801static struct symtabs_and_lines
7802decode_static_tracepoint_spec (char **arg_p)
7803{
7804 VEC(static_tracepoint_marker_p) *markers = NULL;
7805 struct symtabs_and_lines sals;
7806 struct symtab_and_line sal;
7807 struct symbol *sym;
7808 struct cleanup *old_chain;
7809 char *p = &(*arg_p)[3];
7810 char *endp;
7811 char *marker_str;
7812 int i;
7813
e9cafbcc 7814 p = skip_spaces (p);
0fb4aa4b 7815
e9cafbcc 7816 endp = skip_to_space (p);
0fb4aa4b
PA
7817
7818 marker_str = savestring (p, endp - p);
7819 old_chain = make_cleanup (xfree, marker_str);
7820
7821 markers = target_static_tracepoint_markers_by_strid (marker_str);
7822 if (VEC_empty(static_tracepoint_marker_p, markers))
7823 error (_("No known static tracepoint marker named %s"), marker_str);
7824
7825 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7826 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7827
7828 for (i = 0; i < sals.nelts; i++)
7829 {
7830 struct static_tracepoint_marker *marker;
7831
7832 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7833
7834 init_sal (&sals.sals[i]);
7835
7836 sals.sals[i] = find_pc_line (marker->address, 0);
7837 sals.sals[i].pc = marker->address;
7838
7839 release_static_tracepoint_marker (marker);
7840 }
7841
7842 do_cleanups (old_chain);
7843
7844 *arg_p = endp;
7845 return sals;
7846}
7847
fd9b8c24
PA
7848/* Set a breakpoint. This function is shared between CLI and MI
7849 functions for setting a breakpoint. This function has two major
7850 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7851 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7852 breakpoint location, address and thread. Otherwise, ARG is just
7853 the location of breakpoint, with condition and thread specified by
7854 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7855 the breakpoint number will be allocated from the internal
7856 breakpoint count. Returns true if any breakpoint was created;
7857 false otherwise. */
0101ce28 7858
8cdf0e15
VP
7859int
7860create_breakpoint (struct gdbarch *gdbarch,
7861 char *arg, char *cond_string, int thread,
7862 int parse_condition_and_thread,
0fb4aa4b 7863 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7864 int ignore_count,
7865 enum auto_boolean pending_break_support,
7866 struct breakpoint_ops *ops,
84f4c1fe 7867 int from_tty, int enabled, int internal)
c3f6f71d 7868{
71fff37b 7869 struct gdb_exception e;
c3f6f71d 7870 struct symtabs_and_lines sals;
0101ce28 7871 struct symtab_and_line pending_sal;
0101ce28 7872 char *copy_arg;
c3f6f71d 7873 char *addr_start = arg;
7efd8fc2 7874 struct linespec_result canonical;
c3f6f71d 7875 struct cleanup *old_chain;
80c99de1 7876 struct cleanup *bkpt_chain = NULL;
0101ce28 7877 struct captured_parse_breakpoint_args parse_args;
05ff989b 7878 int i;
0101ce28 7879 int pending = 0;
0101ce28 7880 int not_found = 0;
4a306c9a 7881 int task = 0;
86b17b60 7882 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7883
c3f6f71d
JM
7884 sals.sals = NULL;
7885 sals.nelts = 0;
7efd8fc2 7886 init_linespec_result (&canonical);
c3f6f71d 7887
0101ce28
JJ
7888 parse_args.arg_p = &arg;
7889 parse_args.sals_p = &sals;
7efd8fc2 7890 parse_args.canonical_p = &canonical;
0101ce28
JJ
7891 parse_args.not_found_ptr = &not_found;
7892
0fb4aa4b
PA
7893 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7894 {
7895 int i;
7896
7897 sals = decode_static_tracepoint_spec (&arg);
7898
7899 copy_arg = savestring (addr_start, arg - addr_start);
7efd8fc2 7900 canonical.canonical = xcalloc (sals.nelts, sizeof (char **));
0fb4aa4b 7901 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7902 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7903 goto done;
7904 }
7905
05ff989b
AC
7906 e = catch_exception (uiout, do_captured_parse_breakpoint,
7907 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
7908
7909 /* If caller is interested in rc value from parse, set value. */
05ff989b 7910 switch (e.reason)
0101ce28 7911 {
05ff989b 7912 case RETURN_QUIT:
98deb0da 7913 throw_exception (e);
05ff989b
AC
7914 case RETURN_ERROR:
7915 switch (e.error)
0101ce28 7916 {
05ff989b 7917 case NOT_FOUND_ERROR:
0101ce28 7918
05ff989b
AC
7919 /* If pending breakpoint support is turned off, throw
7920 error. */
fa8d40ab
JJ
7921
7922 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7923 throw_exception (e);
7924
7925 exception_print (gdb_stderr, e);
fa8d40ab 7926
05ff989b
AC
7927 /* If pending breakpoint support is auto query and the user
7928 selects no, then simply return the error code. */
059fb39f 7929 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7930 && !nquery (_("Make breakpoint pending on "
7931 "future shared library load? ")))
fd9b8c24 7932 return 0;
fa8d40ab 7933
05ff989b
AC
7934 /* At this point, either the user was queried about setting
7935 a pending breakpoint and selected yes, or pending
7936 breakpoint behavior is on and thus a pending breakpoint
7937 is defaulted on behalf of the user. */
0101ce28 7938 copy_arg = xstrdup (addr_start);
7efd8fc2 7939 canonical.canonical = &copy_arg;
0101ce28
JJ
7940 sals.nelts = 1;
7941 sals.sals = &pending_sal;
7942 pending_sal.pc = 0;
7943 pending = 1;
05ff989b
AC
7944 break;
7945 default:
98deb0da 7946 throw_exception (e);
0101ce28 7947 }
2abae994 7948 break;
05ff989b
AC
7949 default:
7950 if (!sals.nelts)
fd9b8c24 7951 return 0;
0101ce28 7952 }
c3f6f71d 7953
0fb4aa4b
PA
7954 done:
7955
4a64f543 7956 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7957 old_chain = make_cleanup (null_cleanup, 0);
7958
0101ce28
JJ
7959 if (!pending)
7960 {
7961 /* Make sure that all storage allocated to SALS gets freed. */
7962 make_cleanup (xfree, sals.sals);
7963
7efd8fc2
TT
7964 /* Cleanup the canonical array but not its contents. */
7965 make_cleanup (xfree, canonical.canonical);
0101ce28 7966 }
c3f6f71d 7967
c3f6f71d
JM
7968 /* ----------------------------- SNIP -----------------------------
7969 Anything added to the cleanup chain beyond this point is assumed
7970 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7971 then the memory is not reclaimed. */
7972 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7973
7efd8fc2 7974 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7975 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7976 for (i = 0; i < sals.nelts; i++)
7977 {
7efd8fc2
TT
7978 if (canonical.canonical[i] != NULL)
7979 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7980 }
7981
7982 /* Resolve all line numbers to PC's and verify that the addresses
7983 are ok for the target. */
0101ce28 7984 if (!pending)
23e7acfb 7985 breakpoint_sals_to_pc (&sals);
c3f6f71d 7986
7a697b8d
SS
7987 /* Fast tracepoints may have additional restrictions on location. */
7988 if (type_wanted == bp_fast_tracepoint)
7989 check_fast_tracepoint_sals (gdbarch, &sals);
7990
c3f6f71d
JM
7991 /* Verify that condition can be parsed, before setting any
7992 breakpoints. Allocate a separate condition expression for each
4a64f543 7993 breakpoint. */
0101ce28 7994 if (!pending)
c3f6f71d 7995 {
2f069f6f 7996 if (parse_condition_and_thread)
72b2ff0e
VP
7997 {
7998 /* Here we only parse 'arg' to separate condition
7999 from thread number, so parsing in context of first
8000 sal is OK. When setting the breakpoint we'll
8001 re-parse it in context of each sal. */
8002 cond_string = NULL;
8003 thread = -1;
4a306c9a
JB
8004 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8005 &thread, &task);
72b2ff0e
VP
8006 if (cond_string)
8007 make_cleanup (xfree, cond_string);
8008 }
2f069f6f 8009 else
72b2ff0e
VP
8010 {
8011 /* Create a private copy of condition string. */
8012 if (cond_string)
8013 {
8014 cond_string = xstrdup (cond_string);
8015 make_cleanup (xfree, cond_string);
8016 }
8017 }
0fb4aa4b
PA
8018
8019 /* If the user is creating a static tracepoint by marker id
8020 (strace -m MARKER_ID), then store the sals index, so that
8021 breakpoint_re_set can try to match up which of the newly
8022 found markers corresponds to this one, and, don't try to
8023 expand multiple locations for each sal, given than SALS
8024 already should contain all sals for MARKER_ID. */
8025 if (type_wanted == bp_static_tracepoint
7efd8fc2 8026 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8027 {
8028 int i;
8029
8030 for (i = 0; i < sals.nelts; ++i)
8031 {
8032 struct symtabs_and_lines expanded;
8033 struct breakpoint *tp;
8034 struct cleanup *old_chain;
8035
8036 expanded.nelts = 1;
8037 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8038 expanded.sals[0] = sals.sals[i];
8039 old_chain = make_cleanup (xfree, expanded.sals);
8040
7efd8fc2 8041 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
8042 cond_string, type_wanted,
8043 tempflag ? disp_del : disp_donttouch,
8044 thread, task, ignore_count, ops,
56435ebe
TT
8045 from_tty, enabled, internal,
8046 canonical.special_display);
0fb4aa4b
PA
8047
8048 do_cleanups (old_chain);
8049
8050 /* Get the tracepoint we just created. */
84f4c1fe
PM
8051 if (internal)
8052 tp = get_breakpoint (internal_breakpoint_number);
8053 else
8054 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
8055 gdb_assert (tp != NULL);
8056
8057 /* Given that its possible to have multiple markers with
8058 the same string id, if the user is creating a static
8059 tracepoint by marker id ("strace -m MARKER_ID"), then
8060 store the sals index, so that breakpoint_re_set can
8061 try to match up which of the newly found markers
8062 corresponds to this one */
8063 tp->static_trace_marker_id_idx = i;
8064 }
8065 }
8066 else
7efd8fc2 8067 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8068 type_wanted,
8069 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8070 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8071 enabled, internal);
c906108c 8072 }
0101ce28
JJ
8073 else
8074 {
0101ce28
JJ
8075 struct breakpoint *b;
8076
0101ce28
JJ
8077 make_cleanup (xfree, copy_arg);
8078
a6d9a66e 8079 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 8080 set_breakpoint_number (internal, b);
72b2ff0e 8081 b->thread = -1;
7efd8fc2 8082 b->addr_string = canonical.canonical[0];
72b2ff0e 8083 b->cond_string = NULL;
0101ce28 8084 b->ignore_count = ignore_count;
0101ce28 8085 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8086 b->condition_not_parsed = 1;
604133b5 8087 b->ops = ops;
41447f92 8088 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8089 b->pspace = current_program_space;
84f4c1fe 8090 b->py_bp_object = NULL;
74960c60 8091
6c95b8df 8092 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8093 && (b->type == bp_breakpoint
8094 || b->type == bp_hardware_breakpoint))
8095 b->enable_state = bp_startup_disabled;
8096
84f4c1fe
PM
8097 if (internal)
8098 /* Do not mention breakpoints with a negative number,
8099 but do notify observers. */
8100 observer_notify_breakpoint_created (b->number);
8101 else
8102 mention (b);
0101ce28
JJ
8103 }
8104
c3f6f71d 8105 if (sals.nelts > 1)
95a42b64 8106 {
3e43a32a
MS
8107 warning (_("Multiple breakpoints were set.\nUse the "
8108 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8109 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8110 }
8111
80c99de1
PA
8112 /* That's it. Discard the cleanups for data inserted into the
8113 breakpoint. */
8114 discard_cleanups (bkpt_chain);
8115 /* But cleanup everything else. */
c3f6f71d 8116 do_cleanups (old_chain);
217dc9e2 8117
80c99de1 8118 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8119 update_global_location_list (1);
fd9b8c24
PA
8120
8121 return 1;
c3f6f71d 8122}
c906108c 8123
72b2ff0e
VP
8124/* Set a breakpoint.
8125 ARG is a string describing breakpoint address,
8126 condition, and thread.
8127 FLAG specifies if a breakpoint is hardware on,
8128 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8129 and BP_TEMPFLAG. */
8130
98deb0da 8131static void
72b2ff0e 8132break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8133{
72b2ff0e 8134 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8135 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8136 ? bp_hardware_breakpoint
8137 : bp_breakpoint);
c3f6f71d 8138
8cdf0e15
VP
8139 create_breakpoint (get_current_arch (),
8140 arg,
8141 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8142 tempflag, type_wanted,
8cdf0e15
VP
8143 0 /* Ignore count */,
8144 pending_break_support,
8145 NULL /* breakpoint_ops */,
8146 from_tty,
84f4c1fe
PM
8147 1 /* enabled */,
8148 0 /* internal */);
c906108c
SS
8149}
8150
72b2ff0e 8151
c906108c
SS
8152/* Helper function for break_command_1 and disassemble_command. */
8153
8154void
fba45db2 8155resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8156{
8157 CORE_ADDR pc;
8158
8159 if (sal->pc == 0 && sal->symtab != NULL)
8160 {
8161 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8162 error (_("No line %d in file \"%s\"."),
c906108c
SS
8163 sal->line, sal->symtab->filename);
8164 sal->pc = pc;
6a048695 8165
4a64f543
MS
8166 /* If this SAL corresponds to a breakpoint inserted using a line
8167 number, then skip the function prologue if necessary. */
6a048695 8168 if (sal->explicit_line)
059acae7 8169 skip_prologue_sal (sal);
c906108c
SS
8170 }
8171
8172 if (sal->section == 0 && sal->symtab != NULL)
8173 {
8174 struct blockvector *bv;
c5aa993b
JM
8175 struct block *b;
8176 struct symbol *sym;
c906108c 8177
801e3a5b 8178 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8179 if (bv != NULL)
8180 {
7f0df278 8181 sym = block_linkage_function (b);
c906108c
SS
8182 if (sym != NULL)
8183 {
8184 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8185 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8186 }
8187 else
8188 {
4a64f543
MS
8189 /* It really is worthwhile to have the section, so we'll
8190 just have to look harder. This case can be executed
8191 if we have line numbers but no functions (as can
8192 happen in assembly source). */
c906108c 8193
c5aa993b 8194 struct minimal_symbol *msym;
6c95b8df
PA
8195 struct cleanup *old_chain = save_current_space_and_thread ();
8196
8197 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8198
8199 msym = lookup_minimal_symbol_by_pc (sal->pc);
8200 if (msym)
714835d5 8201 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8202
8203 do_cleanups (old_chain);
c906108c
SS
8204 }
8205 }
8206 }
8207}
8208
8209void
fba45db2 8210break_command (char *arg, int from_tty)
c906108c 8211{
db107f19 8212 break_command_1 (arg, 0, from_tty);
c906108c
SS
8213}
8214
c906108c 8215void
fba45db2 8216tbreak_command (char *arg, int from_tty)
c906108c 8217{
db107f19 8218 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8219}
8220
c906108c 8221static void
fba45db2 8222hbreak_command (char *arg, int from_tty)
c906108c 8223{
db107f19 8224 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8225}
8226
8227static void
fba45db2 8228thbreak_command (char *arg, int from_tty)
c906108c 8229{
db107f19 8230 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8231}
8232
8233static void
fba45db2 8234stop_command (char *arg, int from_tty)
c906108c 8235{
a3f17187 8236 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8237Usage: stop in <function | address>\n\
a3f17187 8238 stop at <line>\n"));
c906108c
SS
8239}
8240
8241static void
fba45db2 8242stopin_command (char *arg, int from_tty)
c906108c
SS
8243{
8244 int badInput = 0;
8245
c5aa993b 8246 if (arg == (char *) NULL)
c906108c
SS
8247 badInput = 1;
8248 else if (*arg != '*')
8249 {
8250 char *argptr = arg;
8251 int hasColon = 0;
8252
4a64f543 8253 /* Look for a ':'. If this is a line number specification, then
53a5351d 8254 say it is bad, otherwise, it should be an address or
4a64f543 8255 function/method name. */
c906108c 8256 while (*argptr && !hasColon)
c5aa993b
JM
8257 {
8258 hasColon = (*argptr == ':');
8259 argptr++;
8260 }
c906108c
SS
8261
8262 if (hasColon)
c5aa993b 8263 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8264 else
c5aa993b 8265 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8266 }
8267
8268 if (badInput)
a3f17187 8269 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8270 else
db107f19 8271 break_command_1 (arg, 0, from_tty);
c906108c
SS
8272}
8273
8274static void
fba45db2 8275stopat_command (char *arg, int from_tty)
c906108c
SS
8276{
8277 int badInput = 0;
8278
c5aa993b 8279 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8280 badInput = 1;
8281 else
8282 {
8283 char *argptr = arg;
8284 int hasColon = 0;
8285
4a64f543
MS
8286 /* Look for a ':'. If there is a '::' then get out, otherwise
8287 it is probably a line number. */
c906108c 8288 while (*argptr && !hasColon)
c5aa993b
JM
8289 {
8290 hasColon = (*argptr == ':');
8291 argptr++;
8292 }
c906108c
SS
8293
8294 if (hasColon)
c5aa993b 8295 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8296 else
c5aa993b 8297 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8298 }
8299
8300 if (badInput)
a3f17187 8301 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8302 else
db107f19 8303 break_command_1 (arg, 0, from_tty);
c906108c
SS
8304}
8305
4a64f543
MS
8306/* Return non-zero if EXP is verified as constant. Returned zero
8307 means EXP is variable. Also the constant detection may fail for
8308 some constant expressions and in such case still falsely return
8309 zero. */
65d79d4b
SDJ
8310static int
8311watchpoint_exp_is_const (const struct expression *exp)
8312{
8313 int i = exp->nelts;
8314
8315 while (i > 0)
8316 {
8317 int oplenp, argsp;
8318
8319 /* We are only interested in the descriptor of each element. */
8320 operator_length (exp, i, &oplenp, &argsp);
8321 i -= oplenp;
8322
8323 switch (exp->elts[i].opcode)
8324 {
8325 case BINOP_ADD:
8326 case BINOP_SUB:
8327 case BINOP_MUL:
8328 case BINOP_DIV:
8329 case BINOP_REM:
8330 case BINOP_MOD:
8331 case BINOP_LSH:
8332 case BINOP_RSH:
8333 case BINOP_LOGICAL_AND:
8334 case BINOP_LOGICAL_OR:
8335 case BINOP_BITWISE_AND:
8336 case BINOP_BITWISE_IOR:
8337 case BINOP_BITWISE_XOR:
8338 case BINOP_EQUAL:
8339 case BINOP_NOTEQUAL:
8340 case BINOP_LESS:
8341 case BINOP_GTR:
8342 case BINOP_LEQ:
8343 case BINOP_GEQ:
8344 case BINOP_REPEAT:
8345 case BINOP_COMMA:
8346 case BINOP_EXP:
8347 case BINOP_MIN:
8348 case BINOP_MAX:
8349 case BINOP_INTDIV:
8350 case BINOP_CONCAT:
8351 case BINOP_IN:
8352 case BINOP_RANGE:
8353 case TERNOP_COND:
8354 case TERNOP_SLICE:
8355 case TERNOP_SLICE_COUNT:
8356
8357 case OP_LONG:
8358 case OP_DOUBLE:
8359 case OP_DECFLOAT:
8360 case OP_LAST:
8361 case OP_COMPLEX:
8362 case OP_STRING:
8363 case OP_BITSTRING:
8364 case OP_ARRAY:
8365 case OP_TYPE:
8366 case OP_NAME:
8367 case OP_OBJC_NSSTRING:
8368
8369 case UNOP_NEG:
8370 case UNOP_LOGICAL_NOT:
8371 case UNOP_COMPLEMENT:
8372 case UNOP_ADDR:
8373 case UNOP_HIGH:
4a64f543
MS
8374 /* Unary, binary and ternary operators: We have to check
8375 their operands. If they are constant, then so is the
8376 result of that operation. For instance, if A and B are
8377 determined to be constants, then so is "A + B".
8378
8379 UNOP_IND is one exception to the rule above, because the
8380 value of *ADDR is not necessarily a constant, even when
8381 ADDR is. */
65d79d4b
SDJ
8382 break;
8383
8384 case OP_VAR_VALUE:
8385 /* Check whether the associated symbol is a constant.
4a64f543 8386
65d79d4b 8387 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8388 possible that a buggy compiler could mark a variable as
8389 constant even when it is not, and TYPE_CONST would return
8390 true in this case, while SYMBOL_CLASS wouldn't.
8391
8392 We also have to check for function symbols because they
8393 are always constant. */
65d79d4b
SDJ
8394 {
8395 struct symbol *s = exp->elts[i + 2].symbol;
8396
8397 if (SYMBOL_CLASS (s) != LOC_BLOCK
8398 && SYMBOL_CLASS (s) != LOC_CONST
8399 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8400 return 0;
8401 break;
8402 }
8403
8404 /* The default action is to return 0 because we are using
8405 the optimistic approach here: If we don't know something,
8406 then it is not a constant. */
8407 default:
8408 return 0;
8409 }
8410 }
8411
8412 return 1;
8413}
8414
77b06cd7
TJB
8415/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8416
8417static int
8418insert_watchpoint (struct bp_location *bl)
8419{
e09342b5
TJB
8420 int length = bl->owner->exact? 1 : bl->length;
8421
8422 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8423 bl->owner->cond_exp);
77b06cd7
TJB
8424}
8425
8426/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8427
8428static int
8429remove_watchpoint (struct bp_location *bl)
8430{
e09342b5
TJB
8431 int length = bl->owner->exact? 1 : bl->length;
8432
8433 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8434 bl->owner->cond_exp);
8435}
8436
8437/* Implement the "resources_needed" breakpoint_ops method for
8438 hardware watchpoints. */
8439
8440static int
8441resources_needed_watchpoint (const struct bp_location *bl)
8442{
90e4670f 8443 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8444
90e4670f 8445 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8446}
8447
8448/* The breakpoint_ops structure to be used in hardware watchpoints. */
8449
8450static struct breakpoint_ops watchpoint_breakpoint_ops =
8451{
8452 insert_watchpoint,
8453 remove_watchpoint,
8454 NULL, /* breakpoint_hit */
e09342b5 8455 resources_needed_watchpoint,
77b06cd7
TJB
8456 NULL, /* print_it */
8457 NULL, /* print_one */
8458 NULL, /* print_mention */
8459 NULL /* print_recreate */
8460};
8461
53a5351d
JM
8462/* accessflag: hw_write: watch write,
8463 hw_read: watch read,
8464 hw_access: watch access (read or write) */
c906108c 8465static void
84f4c1fe
PM
8466watch_command_1 (char *arg, int accessflag, int from_tty,
8467 int just_location, int internal)
c906108c 8468{
d983da9c 8469 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8470 struct expression *exp;
60e1c644 8471 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8472 struct value *val, *mark, *result;
c906108c 8473 struct frame_info *frame;
c906108c
SS
8474 char *exp_start = NULL;
8475 char *exp_end = NULL;
37e4754d 8476 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8477 int toklen;
8478 char *cond_start = NULL;
8479 char *cond_end = NULL;
c906108c
SS
8480 int i, other_type_used, target_resources_ok = 0;
8481 enum bptype bp_type;
e09342b5 8482 int reg_cnt = 0;
37e4754d 8483 int thread = -1;
0cf6dd15 8484 int pc = 0;
c906108c 8485
37e4754d
LM
8486 /* Make sure that we actually have parameters to parse. */
8487 if (arg != NULL && arg[0] != '\0')
8488 {
8489 toklen = strlen (arg); /* Size of argument list. */
8490
8491 /* Points tok to the end of the argument list. */
8492 tok = arg + toklen - 1;
8493
4a64f543
MS
8494 /* Go backwards in the parameters list. Skip the last
8495 parameter. If we're expecting a 'thread <thread_num>'
8496 parameter, this should be the thread identifier. */
37e4754d
LM
8497 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8498 tok--;
8499 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8500 tok--;
8501
8502 /* Points end_tok to the beginning of the last token. */
8503 id_tok_start = tok + 1;
8504
4a64f543
MS
8505 /* Go backwards in the parameters list. Skip one more
8506 parameter. If we're expecting a 'thread <thread_num>'
8507 parameter, we should reach a "thread" token. */
37e4754d
LM
8508 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8509 tok--;
8510
8511 end_tok = tok;
8512
8513 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8514 tok--;
8515
8516 /* Move the pointer forward to skip the whitespace and
8517 calculate the length of the token. */
8518 tok++;
8519 toklen = end_tok - tok;
8520
8521 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8522 {
8523 /* At this point we've found a "thread" token, which means
8524 the user is trying to set a watchpoint that triggers
8525 only in a specific thread. */
8526 char *endp;
8527
8528 /* Extract the thread ID from the next token. */
8529 thread = strtol (id_tok_start, &endp, 0);
8530
8531 /* Check if the user provided a valid numeric value for the
8532 thread ID. */
8533 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8534 error (_("Invalid thread ID specification %s."), id_tok_start);
8535
8536 /* Check if the thread actually exists. */
8537 if (!valid_thread_id (thread))
8538 error (_("Unknown thread %d."), thread);
8539
8540 /* Truncate the string and get rid of the thread <thread_num>
8541 parameter before the parameter list is parsed by the
8542 evaluate_expression() function. */
8543 *tok = '\0';
8544 }
8545 }
8546
8547 /* Parse the rest of the arguments. */
c906108c
SS
8548 innermost_block = NULL;
8549 exp_start = arg;
8550 exp = parse_exp_1 (&arg, 0, 0);
8551 exp_end = arg;
fa8a61dc
TT
8552 /* Remove trailing whitespace from the expression before saving it.
8553 This makes the eventual display of the expression string a bit
8554 prettier. */
8555 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8556 --exp_end;
8557
65d79d4b
SDJ
8558 /* Checking if the expression is not constant. */
8559 if (watchpoint_exp_is_const (exp))
8560 {
8561 int len;
8562
8563 len = exp_end - exp_start;
8564 while (len > 0 && isspace (exp_start[len - 1]))
8565 len--;
8566 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8567 }
8568
c906108c
SS
8569 exp_valid_block = innermost_block;
8570 mark = value_mark ();
a1442452 8571 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8572
8573 if (just_location)
8574 {
8575 exp_valid_block = NULL;
a1442452 8576 val = value_addr (result);
06a64a0b
TT
8577 release_value (val);
8578 value_free_to_mark (mark);
8579 }
8580 else if (val != NULL)
fa4727a6 8581 release_value (val);
c906108c 8582
e9cafbcc
TT
8583 tok = skip_spaces (arg);
8584 end_tok = skip_to_space (tok);
c906108c
SS
8585
8586 toklen = end_tok - tok;
8587 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8588 {
2d134ed3
PA
8589 struct expression *cond;
8590
60e1c644 8591 innermost_block = NULL;
c906108c
SS
8592 tok = cond_start = end_tok + 1;
8593 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8594
8595 /* The watchpoint expression may not be local, but the condition
8596 may still be. E.g.: `watch global if local > 0'. */
8597 cond_exp_valid_block = innermost_block;
8598
2d134ed3 8599 xfree (cond);
c906108c
SS
8600 cond_end = tok;
8601 }
8602 if (*tok)
8a3fe4f8 8603 error (_("Junk at end of command."));
c906108c 8604
53a5351d 8605 if (accessflag == hw_read)
c5aa993b 8606 bp_type = bp_read_watchpoint;
53a5351d 8607 else if (accessflag == hw_access)
c5aa993b
JM
8608 bp_type = bp_access_watchpoint;
8609 else
8610 bp_type = bp_hardware_watchpoint;
c906108c 8611
e09342b5
TJB
8612 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8613 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8614 error (_("Expression cannot be implemented with read/access watchpoint."));
e09342b5 8615 if (reg_cnt != 0)
c5aa993b
JM
8616 {
8617 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8618 target_resources_ok =
e09342b5 8619 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
53a5351d 8620 other_type_used);
c5aa993b 8621 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8622 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8623
c5aa993b 8624 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
3e43a32a
MS
8625 error (_("Target can only support one kind "
8626 "of HW watchpoint at a time."));
c5aa993b 8627 }
c906108c 8628
4a64f543
MS
8629 /* Change the type of breakpoint to an ordinary watchpoint if a
8630 hardware watchpoint could not be set. */
e09342b5 8631 if (!reg_cnt || target_resources_ok <= 0)
4d28f7a8
KB
8632 bp_type = bp_watchpoint;
8633
d983da9c 8634 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8635
8636 /* If the expression is "local", then set up a "watchpoint scope"
8637 breakpoint at the point where we've left the scope of the watchpoint
8638 expression. Create the scope breakpoint before the watchpoint, so
8639 that we will encounter it first in bpstat_stop_status. */
60e1c644 8640 if (exp_valid_block && frame)
d983da9c 8641 {
edb3359d
DJ
8642 if (frame_id_p (frame_unwind_caller_id (frame)))
8643 {
8644 scope_breakpoint
a6d9a66e
UW
8645 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8646 frame_unwind_caller_pc (frame),
edb3359d 8647 bp_watchpoint_scope);
d983da9c 8648
edb3359d 8649 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8650
edb3359d
DJ
8651 /* Automatically delete the breakpoint when it hits. */
8652 scope_breakpoint->disposition = disp_del;
d983da9c 8653
edb3359d
DJ
8654 /* Only break in the proper frame (help with recursion). */
8655 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8656
edb3359d 8657 /* Set the address at which we will stop. */
a6d9a66e
UW
8658 scope_breakpoint->loc->gdbarch
8659 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8660 scope_breakpoint->loc->requested_address
8661 = frame_unwind_caller_pc (frame);
8662 scope_breakpoint->loc->address
a6d9a66e
UW
8663 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8664 scope_breakpoint->loc->requested_address,
edb3359d
DJ
8665 scope_breakpoint->type);
8666 }
d983da9c
DJ
8667 }
8668
c906108c 8669 /* Now set up the breakpoint. */
2d134ed3 8670 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 8671 set_breakpoint_number (internal, b);
37e4754d 8672 b->thread = thread;
b5de0fa7 8673 b->disposition = disp_donttouch;
c906108c
SS
8674 b->exp = exp;
8675 b->exp_valid_block = exp_valid_block;
60e1c644 8676 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
8677 if (just_location)
8678 {
8679 struct type *t = value_type (val);
8680 CORE_ADDR addr = value_as_address (val);
8681 char *name;
8682
8683 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
8684 name = type_to_string (t);
8685
d63d0675
JK
8686 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
8687 core_addr_to_string (addr));
06a64a0b
TT
8688 xfree (name);
8689
d63d0675
JK
8690 b->exp_string = xstrprintf ("-location: %.*s",
8691 (int) (exp_end - exp_start), exp_start);
8692
06a64a0b
TT
8693 /* The above expression is in C. */
8694 b->language = language_c;
8695 }
8696 else
8697 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 8698 b->val = val;
fa4727a6 8699 b->val_valid = 1;
77b06cd7
TJB
8700 b->ops = &watchpoint_breakpoint_ops;
8701
e09342b5
TJB
8702 /* Use an exact watchpoint when there's only one memory region to be
8703 watched, and only one debug register is needed to watch it. */
8704 b->exact = target_exact_watchpoints && reg_cnt == 1;
8705
c906108c
SS
8706 if (cond_start)
8707 b->cond_string = savestring (cond_start, cond_end - cond_start);
8708 else
8709 b->cond_string = 0;
c5aa993b 8710
c906108c 8711 if (frame)
f6bc2008
PA
8712 {
8713 b->watchpoint_frame = get_frame_id (frame);
8714 b->watchpoint_thread = inferior_ptid;
8715 }
c906108c 8716 else
f6bc2008
PA
8717 {
8718 b->watchpoint_frame = null_frame_id;
8719 b->watchpoint_thread = null_ptid;
8720 }
c906108c 8721
d983da9c 8722 if (scope_breakpoint != NULL)
c906108c 8723 {
d983da9c
DJ
8724 /* The scope breakpoint is related to the watchpoint. We will
8725 need to act on them together. */
8726 b->related_breakpoint = scope_breakpoint;
8727 scope_breakpoint->related_breakpoint = b;
c906108c 8728 }
d983da9c 8729
06a64a0b
TT
8730 if (!just_location)
8731 value_free_to_mark (mark);
2d134ed3
PA
8732
8733 /* Finally update the new watchpoint. This creates the locations
8734 that should be inserted. */
8735 update_watchpoint (b, 1);
84f4c1fe
PM
8736 if (internal)
8737 /* Do not mention breakpoints with a negative number, but do
8738 notify observers. */
8739 observer_notify_breakpoint_created (b->number);
8740 else
8741 mention (b);
b60e7edf 8742 update_global_location_list (1);
c906108c
SS
8743}
8744
e09342b5
TJB
8745/* Return count of debug registers needed to watch the given expression.
8746 If EXACT_WATCHPOINTS is 1, then consider that only the address of
8747 the start of the watched region will be monitored (i.e., all accesses
8748 will be aligned). This uses less debug registers on some targets.
8749
8750 If the watchpoint cannot be handled in hardware return zero. */
c906108c 8751
c906108c 8752static int
e09342b5 8753can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
c906108c
SS
8754{
8755 int found_memory_cnt = 0;
2e70b7b9 8756 struct value *head = v;
c906108c
SS
8757
8758 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 8759 if (!can_use_hw_watchpoints)
c906108c 8760 return 0;
c5aa993b 8761
5c44784c
JM
8762 /* Make sure that the value of the expression depends only upon
8763 memory contents, and values computed from them within GDB. If we
8764 find any register references or function calls, we can't use a
8765 hardware watchpoint.
8766
8767 The idea here is that evaluating an expression generates a series
8768 of values, one holding the value of every subexpression. (The
8769 expression a*b+c has five subexpressions: a, b, a*b, c, and
8770 a*b+c.) GDB's values hold almost enough information to establish
8771 the criteria given above --- they identify memory lvalues,
8772 register lvalues, computed values, etcetera. So we can evaluate
8773 the expression, and then scan the chain of values that leaves
8774 behind to decide whether we can detect any possible change to the
8775 expression's final value using only hardware watchpoints.
8776
8777 However, I don't think that the values returned by inferior
8778 function calls are special in any way. So this function may not
8779 notice that an expression involving an inferior function call
8780 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 8781 for (; v; v = value_next (v))
c906108c 8782 {
5c44784c 8783 if (VALUE_LVAL (v) == lval_memory)
c906108c 8784 {
8464be76
DJ
8785 if (v != head && value_lazy (v))
8786 /* A lazy memory lvalue in the chain is one that GDB never
8787 needed to fetch; we either just used its address (e.g.,
8788 `a' in `a.b') or we never needed it at all (e.g., `a'
8789 in `a,b'). This doesn't apply to HEAD; if that is
8790 lazy then it was not readable, but watch it anyway. */
5c44784c 8791 ;
53a5351d 8792 else
5c44784c
JM
8793 {
8794 /* Ahh, memory we actually used! Check if we can cover
8795 it with hardware watchpoints. */
df407dfe 8796 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
8797
8798 /* We only watch structs and arrays if user asked for it
8799 explicitly, never if they just happen to appear in a
8800 middle of some value chain. */
8801 if (v == head
8802 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
8803 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
8804 {
42ae5230 8805 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
8806 int len;
8807 int num_regs;
8808
8809 len = (exact_watchpoints
8810 && is_scalar_type_recursive (vtype))?
8811 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 8812
e09342b5
TJB
8813 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
8814 if (!num_regs)
2e70b7b9
MS
8815 return 0;
8816 else
e09342b5 8817 found_memory_cnt += num_regs;
2e70b7b9 8818 }
5c44784c 8819 }
c5aa993b 8820 }
5086187c
AC
8821 else if (VALUE_LVAL (v) != not_lval
8822 && deprecated_value_modifiable (v) == 0)
38b6c3b3 8823 return 0; /* These are values from the history (e.g., $1). */
5086187c 8824 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 8825 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
8826 }
8827
8828 /* The expression itself looks suitable for using a hardware
8829 watchpoint, but give the target machine a chance to reject it. */
8830 return found_memory_cnt;
8831}
8832
8b93c638 8833void
84f4c1fe 8834watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8835{
84f4c1fe 8836 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
8837}
8838
8839/* A helper function that looks for an argument at the start of a
8840 string. The argument must also either be at the end of the string,
8841 or be followed by whitespace. Returns 1 if it finds the argument,
8842 0 otherwise. If the argument is found, it updates *STR. */
8843
8844static int
8845check_for_argument (char **str, char *arg, int arg_len)
8846{
8847 if (strncmp (*str, arg, arg_len) == 0
8848 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
8849 {
8850 *str += arg_len;
8851 return 1;
8852 }
8853 return 0;
8854}
8855
8856/* A helper function that looks for the "-location" argument and then
8857 calls watch_command_1. */
8858
8859static void
8860watch_maybe_just_location (char *arg, int accessflag, int from_tty)
8861{
8862 int just_location = 0;
8863
8864 if (arg
8865 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
8866 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
8867 {
e9cafbcc 8868 arg = skip_spaces (arg);
06a64a0b
TT
8869 just_location = 1;
8870 }
8871
84f4c1fe 8872 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 8873}
8926118c 8874
c5aa993b 8875static void
fba45db2 8876watch_command (char *arg, int from_tty)
c906108c 8877{
06a64a0b 8878 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
8879}
8880
8b93c638 8881void
84f4c1fe 8882rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8883{
84f4c1fe 8884 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 8885}
8926118c 8886
c5aa993b 8887static void
fba45db2 8888rwatch_command (char *arg, int from_tty)
c906108c 8889{
06a64a0b 8890 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
8891}
8892
8b93c638 8893void
84f4c1fe 8894awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 8895{
84f4c1fe 8896 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 8897}
8926118c 8898
c5aa993b 8899static void
fba45db2 8900awatch_command (char *arg, int from_tty)
c906108c 8901{
06a64a0b 8902 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 8903}
c906108c 8904\f
c5aa993b 8905
43ff13b4 8906/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
8907 because it uses the mechanisms of breakpoints. */
8908
bfec99b2
PA
8909struct until_break_command_continuation_args
8910{
8911 struct breakpoint *breakpoint;
8912 struct breakpoint *breakpoint2;
186c406b 8913 int thread_num;
bfec99b2
PA
8914};
8915
43ff13b4 8916/* This function is called by fetch_inferior_event via the
4a64f543 8917 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 8918 care of cleaning up the temporary breakpoints set up by the until
4a64f543 8919 command. */
c2c6d25f 8920static void
604ead4a 8921until_break_command_continuation (void *arg)
43ff13b4 8922{
bfec99b2
PA
8923 struct until_break_command_continuation_args *a = arg;
8924
8925 delete_breakpoint (a->breakpoint);
8926 if (a->breakpoint2)
8927 delete_breakpoint (a->breakpoint2);
186c406b 8928 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
8929}
8930
c906108c 8931void
ae66c1fc 8932until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
8933{
8934 struct symtabs_and_lines sals;
8935 struct symtab_and_line sal;
206415a3 8936 struct frame_info *frame = get_selected_frame (NULL);
c906108c 8937 struct breakpoint *breakpoint;
f107f563 8938 struct breakpoint *breakpoint2 = NULL;
c906108c 8939 struct cleanup *old_chain;
186c406b
TT
8940 int thread;
8941 struct thread_info *tp;
c906108c
SS
8942
8943 clear_proceed_status ();
8944
8945 /* Set a breakpoint where the user wants it and at return from
4a64f543 8946 this function. */
c5aa993b 8947
c906108c
SS
8948 if (default_breakpoint_valid)
8949 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
7efd8fc2 8950 default_breakpoint_line, NULL, NULL);
c906108c 8951 else
7efd8fc2 8952 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL, NULL);
c5aa993b 8953
c906108c 8954 if (sals.nelts != 1)
8a3fe4f8 8955 error (_("Couldn't get information on specified line."));
c5aa993b 8956
c906108c 8957 sal = sals.sals[0];
4a64f543 8958 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 8959
c906108c 8960 if (*arg)
8a3fe4f8 8961 error (_("Junk at end of arguments."));
c5aa993b 8962
c906108c 8963 resolve_sal_pc (&sal);
c5aa993b 8964
ae66c1fc
EZ
8965 if (anywhere)
8966 /* If the user told us to continue until a specified location,
8967 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
8968 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8969 null_frame_id, bp_until);
ae66c1fc 8970 else
4a64f543
MS
8971 /* Otherwise, specify the selected frame, because we want to stop
8972 only at the very same frame. */
a6d9a66e
UW
8973 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
8974 get_stack_frame_id (frame),
ae66c1fc 8975 bp_until);
c5aa993b 8976
f107f563 8977 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 8978
186c406b
TT
8979 tp = inferior_thread ();
8980 thread = tp->num;
8981
ae66c1fc
EZ
8982 /* Keep within the current frame, or in frames called by the current
8983 one. */
edb3359d
DJ
8984
8985 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 8986 {
edb3359d
DJ
8987 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
8988 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
8989 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
8990 sal,
edb3359d 8991 frame_unwind_caller_id (frame),
f107f563
VP
8992 bp_until);
8993 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
8994
8995 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
8996 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 8997 }
c5aa993b 8998
c906108c 8999 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9000
4a64f543
MS
9001 /* If we are running asynchronously, and proceed call above has
9002 actually managed to start the target, arrange for breakpoints to
9003 be deleted when the target stops. Otherwise, we're already
9004 stopped and delete breakpoints via cleanup chain. */
f107f563 9005
8ea051c5 9006 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9007 {
bfec99b2
PA
9008 struct until_break_command_continuation_args *args;
9009 args = xmalloc (sizeof (*args));
f107f563 9010
bfec99b2
PA
9011 args->breakpoint = breakpoint;
9012 args->breakpoint2 = breakpoint2;
186c406b 9013 args->thread_num = thread;
f107f563
VP
9014
9015 discard_cleanups (old_chain);
95e54da7
PA
9016 add_continuation (inferior_thread (),
9017 until_break_command_continuation, args,
604ead4a 9018 xfree);
f107f563
VP
9019 }
9020 else
c5aa993b 9021 do_cleanups (old_chain);
c906108c 9022}
ae66c1fc 9023
c906108c
SS
9024/* This function attempts to parse an optional "if <cond>" clause
9025 from the arg string. If one is not found, it returns NULL.
c5aa993b 9026
c906108c
SS
9027 Else, it returns a pointer to the condition string. (It does not
9028 attempt to evaluate the string against a particular block.) And,
9029 it updates arg to point to the first character following the parsed
4a64f543 9030 if clause in the arg string. */
53a5351d 9031
c906108c 9032static char *
fba45db2 9033ep_parse_optional_if_clause (char **arg)
c906108c 9034{
c5aa993b
JM
9035 char *cond_string;
9036
9037 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9038 return NULL;
c5aa993b 9039
4a64f543 9040 /* Skip the "if" keyword. */
c906108c 9041 (*arg) += 2;
c5aa993b 9042
c906108c 9043 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9044 condition string. */
e9cafbcc 9045 *arg = skip_spaces (*arg);
c906108c 9046 cond_string = *arg;
c5aa993b 9047
4a64f543
MS
9048 /* Assume that the condition occupies the remainder of the arg
9049 string. */
c906108c 9050 (*arg) += strlen (cond_string);
c5aa993b 9051
c906108c
SS
9052 return cond_string;
9053}
c5aa993b 9054
c906108c
SS
9055/* Commands to deal with catching events, such as signals, exceptions,
9056 process start/exit, etc. */
c5aa993b
JM
9057
9058typedef enum
9059{
44feb3ce
TT
9060 catch_fork_temporary, catch_vfork_temporary,
9061 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9062}
9063catch_fork_kind;
9064
c906108c 9065static void
cc59ec59
MS
9066catch_fork_command_1 (char *arg, int from_tty,
9067 struct cmd_list_element *command)
c906108c 9068{
a6d9a66e 9069 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9070 char *cond_string = NULL;
44feb3ce
TT
9071 catch_fork_kind fork_kind;
9072 int tempflag;
9073
9074 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9075 tempflag = (fork_kind == catch_fork_temporary
9076 || fork_kind == catch_vfork_temporary);
c5aa993b 9077
44feb3ce
TT
9078 if (!arg)
9079 arg = "";
e9cafbcc 9080 arg = skip_spaces (arg);
c5aa993b 9081
c906108c 9082 /* The allowed syntax is:
c5aa993b
JM
9083 catch [v]fork
9084 catch [v]fork if <cond>
9085
4a64f543 9086 First, check if there's an if clause. */
c906108c 9087 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9088
c906108c 9089 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9090 error (_("Junk at end of arguments."));
c5aa993b 9091
c906108c 9092 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9093 and enable reporting of such events. */
c5aa993b
JM
9094 switch (fork_kind)
9095 {
44feb3ce
TT
9096 case catch_fork_temporary:
9097 case catch_fork_permanent:
a6d9a66e 9098 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9099 &catch_fork_breakpoint_ops);
c906108c 9100 break;
44feb3ce
TT
9101 case catch_vfork_temporary:
9102 case catch_vfork_permanent:
a6d9a66e 9103 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9104 &catch_vfork_breakpoint_ops);
c906108c 9105 break;
c5aa993b 9106 default:
8a3fe4f8 9107 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9108 break;
c5aa993b 9109 }
c906108c
SS
9110}
9111
9112static void
cc59ec59
MS
9113catch_exec_command_1 (char *arg, int from_tty,
9114 struct cmd_list_element *command)
c906108c 9115{
a6d9a66e 9116 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9117 int tempflag;
c5aa993b 9118 char *cond_string = NULL;
c906108c 9119
44feb3ce
TT
9120 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9121
9122 if (!arg)
9123 arg = "";
e9cafbcc 9124 arg = skip_spaces (arg);
c906108c
SS
9125
9126 /* The allowed syntax is:
c5aa993b
JM
9127 catch exec
9128 catch exec if <cond>
c906108c 9129
4a64f543 9130 First, check if there's an if clause. */
c906108c
SS
9131 cond_string = ep_parse_optional_if_clause (&arg);
9132
9133 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9134 error (_("Junk at end of arguments."));
c906108c
SS
9135
9136 /* If this target supports it, create an exec catchpoint
4a64f543 9137 and enable reporting of such events. */
a6d9a66e
UW
9138 create_catchpoint (gdbarch, tempflag, cond_string,
9139 &catch_exec_breakpoint_ops);
c906108c 9140}
c5aa993b 9141
3086aeae
DJ
9142static enum print_stop_action
9143print_exception_catchpoint (struct breakpoint *b)
9144{
ade92717 9145 int bp_temp, bp_throw;
3086aeae 9146
ade92717 9147 annotate_catchpoint (b->number);
3086aeae 9148
ade92717
AR
9149 bp_throw = strstr (b->addr_string, "throw") != NULL;
9150 if (b->loc->address != b->loc->requested_address)
9151 breakpoint_adjustment_warning (b->loc->requested_address,
9152 b->loc->address,
9153 b->number, 1);
df2b6d2d 9154 bp_temp = b->disposition == disp_del;
ade92717
AR
9155 ui_out_text (uiout,
9156 bp_temp ? "Temporary catchpoint "
9157 : "Catchpoint ");
9158 if (!ui_out_is_mi_like_p (uiout))
9159 ui_out_field_int (uiout, "bkptno", b->number);
9160 ui_out_text (uiout,
c0b37c48
AR
9161 bp_throw ? " (exception thrown), "
9162 : " (exception caught), ");
ade92717
AR
9163 if (ui_out_is_mi_like_p (uiout))
9164 {
9165 ui_out_field_string (uiout, "reason",
9166 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9167 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9168 ui_out_field_int (uiout, "bkptno", b->number);
9169 }
3086aeae
DJ
9170 return PRINT_SRC_AND_LOC;
9171}
9172
9173static void
cc59ec59
MS
9174print_one_exception_catchpoint (struct breakpoint *b,
9175 struct bp_location **last_loc)
3086aeae 9176{
79a45b7d 9177 struct value_print_options opts;
cc59ec59 9178
79a45b7d
TT
9179 get_user_print_options (&opts);
9180 if (opts.addressprint)
3086aeae
DJ
9181 {
9182 annotate_field (4);
604133b5
AR
9183 if (b->loc == NULL || b->loc->shlib_disabled)
9184 ui_out_field_string (uiout, "addr", "<PENDING>");
9185 else
5af949e3
UW
9186 ui_out_field_core_addr (uiout, "addr",
9187 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9188 }
9189 annotate_field (5);
604133b5 9190 if (b->loc)
a6d9a66e 9191 *last_loc = b->loc;
3086aeae
DJ
9192 if (strstr (b->addr_string, "throw") != NULL)
9193 ui_out_field_string (uiout, "what", "exception throw");
9194 else
9195 ui_out_field_string (uiout, "what", "exception catch");
9196}
9197
9198static void
9199print_mention_exception_catchpoint (struct breakpoint *b)
9200{
ade92717
AR
9201 int bp_temp;
9202 int bp_throw;
9203
df2b6d2d 9204 bp_temp = b->disposition == disp_del;
ade92717
AR
9205 bp_throw = strstr (b->addr_string, "throw") != NULL;
9206 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9207 : _("Catchpoint "));
9208 ui_out_field_int (uiout, "bkptno", b->number);
9209 ui_out_text (uiout, bp_throw ? _(" (throw)")
9210 : _(" (catch)"));
3086aeae
DJ
9211}
9212
6149aea9
PA
9213/* Implement the "print_recreate" breakpoint_ops method for throw and
9214 catch catchpoints. */
9215
9216static void
4a64f543
MS
9217print_recreate_exception_catchpoint (struct breakpoint *b,
9218 struct ui_file *fp)
6149aea9
PA
9219{
9220 int bp_temp;
9221 int bp_throw;
9222
9223 bp_temp = b->disposition == disp_del;
9224 bp_throw = strstr (b->addr_string, "throw") != NULL;
9225 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9226 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9227}
9228
3086aeae 9229static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
9230 NULL, /* insert */
9231 NULL, /* remove */
9232 NULL, /* breakpoint_hit */
e09342b5 9233 NULL, /* resources_needed */
3086aeae
DJ
9234 print_exception_catchpoint,
9235 print_one_exception_catchpoint,
6149aea9
PA
9236 print_mention_exception_catchpoint,
9237 print_recreate_exception_catchpoint
3086aeae
DJ
9238};
9239
9240static int
9241handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9242 enum exception_event_kind ex_event, int from_tty)
9243{
604133b5
AR
9244 char *trigger_func_name;
9245
3086aeae 9246 if (ex_event == EX_EVENT_CATCH)
604133b5 9247 trigger_func_name = "__cxa_begin_catch";
3086aeae 9248 else
604133b5 9249 trigger_func_name = "__cxa_throw";
3086aeae 9250
8cdf0e15
VP
9251 create_breakpoint (get_current_arch (),
9252 trigger_func_name, cond_string, -1,
9253 0 /* condition and thread are valid. */,
0fb4aa4b 9254 tempflag, bp_breakpoint,
8cdf0e15
VP
9255 0,
9256 AUTO_BOOLEAN_TRUE /* pending */,
9257 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9258 1 /* enabled */,
9259 0 /* internal */);
3086aeae 9260
3086aeae
DJ
9261 return 1;
9262}
9263
4a64f543 9264/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9265
9266static void
fba45db2
KB
9267catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9268 int tempflag, int from_tty)
c906108c 9269{
c5aa993b 9270 char *cond_string = NULL;
c5aa993b 9271
44feb3ce
TT
9272 if (!arg)
9273 arg = "";
e9cafbcc 9274 arg = skip_spaces (arg);
c5aa993b 9275
c906108c
SS
9276 cond_string = ep_parse_optional_if_clause (&arg);
9277
9278 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9279 error (_("Junk at end of arguments."));
c906108c 9280
059fb39f
PM
9281 if (ex_event != EX_EVENT_THROW
9282 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9283 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9284
3086aeae
DJ
9285 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9286 return;
9287
8a3fe4f8 9288 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9289}
9290
44feb3ce
TT
9291/* Implementation of "catch catch" command. */
9292
9293static void
9294catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9295{
9296 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9297
44feb3ce
TT
9298 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9299}
9300
9301/* Implementation of "catch throw" command. */
9302
9303static void
9304catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9305{
9306 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9307
44feb3ce
TT
9308 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9309}
9310
f7f9143b
JB
9311/* Create a breakpoint struct for Ada exception catchpoints. */
9312
9313static void
a6d9a66e
UW
9314create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9315 struct symtab_and_line sal,
f7f9143b
JB
9316 char *addr_string,
9317 char *exp_string,
9318 char *cond_string,
9319 struct expression *cond,
9320 struct breakpoint_ops *ops,
9321 int tempflag,
9322 int from_tty)
9323{
9324 struct breakpoint *b;
9325
9326 if (from_tty)
9327 {
5af949e3
UW
9328 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9329 if (!loc_gdbarch)
9330 loc_gdbarch = gdbarch;
9331
6c95b8df
PA
9332 describe_other_breakpoints (loc_gdbarch,
9333 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9334 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9335 version for exception catchpoints, because two catchpoints
9336 used for different exception names will use the same address.
9337 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9338 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
9339 inapropriate, we should use the word catchpoint, and tell
9340 the user what type of catchpoint it is. The above is good
9341 enough for now, though. */
9342 }
9343
a6d9a66e 9344 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9345 set_breakpoint_count (breakpoint_count + 1);
9346
9347 b->enable_state = bp_enabled;
9348 b->disposition = tempflag ? disp_del : disp_donttouch;
9349 b->number = breakpoint_count;
9350 b->ignore_count = 0;
511a6cd4 9351 b->loc->cond = cond;
f7f9143b
JB
9352 b->addr_string = addr_string;
9353 b->language = language_ada;
9354 b->cond_string = cond_string;
9355 b->exp_string = exp_string;
9356 b->thread = -1;
9357 b->ops = ops;
f7f9143b
JB
9358
9359 mention (b);
b60e7edf 9360 update_global_location_list (1);
f7f9143b
JB
9361}
9362
9363/* Implement the "catch exception" command. */
9364
9365static void
44feb3ce
TT
9366catch_ada_exception_command (char *arg, int from_tty,
9367 struct cmd_list_element *command)
f7f9143b 9368{
a6d9a66e 9369 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9370 int tempflag;
f7f9143b 9371 struct symtab_and_line sal;
f7f9143b
JB
9372 char *addr_string = NULL;
9373 char *exp_string = NULL;
9374 char *cond_string = NULL;
9375 struct expression *cond = NULL;
9376 struct breakpoint_ops *ops = NULL;
9377
44feb3ce
TT
9378 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9379
9380 if (!arg)
9381 arg = "";
f7f9143b
JB
9382 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9383 &cond_string, &cond, &ops);
a6d9a66e 9384 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9385 cond_string, cond, ops, tempflag,
9386 from_tty);
9387}
9388
a96d9b2e
SDJ
9389/* Cleanup function for a syscall filter list. */
9390static void
9391clean_up_filters (void *arg)
9392{
9393 VEC(int) *iter = *(VEC(int) **) arg;
9394 VEC_free (int, iter);
9395}
9396
9397/* Splits the argument using space as delimiter. Returns an xmalloc'd
9398 filter list, or NULL if no filtering is required. */
9399static VEC(int) *
9400catch_syscall_split_args (char *arg)
9401{
9402 VEC(int) *result = NULL;
9403 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9404
9405 while (*arg != '\0')
9406 {
9407 int i, syscall_number;
9408 char *endptr;
9409 char cur_name[128];
9410 struct syscall s;
9411
9412 /* Skip whitespace. */
9413 while (isspace (*arg))
9414 arg++;
9415
9416 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9417 cur_name[i] = arg[i];
9418 cur_name[i] = '\0';
9419 arg += i;
9420
9421 /* Check if the user provided a syscall name or a number. */
9422 syscall_number = (int) strtol (cur_name, &endptr, 0);
9423 if (*endptr == '\0')
bccd0dd2 9424 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9425 else
9426 {
9427 /* We have a name. Let's check if it's valid and convert it
9428 to a number. */
9429 get_syscall_by_name (cur_name, &s);
9430
9431 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9432 /* Here we have to issue an error instead of a warning,
9433 because GDB cannot do anything useful if there's no
9434 syscall number to be caught. */
a96d9b2e
SDJ
9435 error (_("Unknown syscall name '%s'."), cur_name);
9436 }
9437
9438 /* Ok, it's valid. */
9439 VEC_safe_push (int, result, s.number);
9440 }
9441
9442 discard_cleanups (cleanup);
9443 return result;
9444}
9445
9446/* Implement the "catch syscall" command. */
9447
9448static void
cc59ec59
MS
9449catch_syscall_command_1 (char *arg, int from_tty,
9450 struct cmd_list_element *command)
a96d9b2e
SDJ
9451{
9452 int tempflag;
9453 VEC(int) *filter;
9454 struct syscall s;
9455 struct gdbarch *gdbarch = get_current_arch ();
9456
9457 /* Checking if the feature if supported. */
9458 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9459 error (_("The feature 'catch syscall' is not supported on \
ea666128 9460this architecture yet."));
a96d9b2e
SDJ
9461
9462 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9463
e9cafbcc 9464 arg = skip_spaces (arg);
a96d9b2e
SDJ
9465
9466 /* We need to do this first "dummy" translation in order
9467 to get the syscall XML file loaded or, most important,
9468 to display a warning to the user if there's no XML file
9469 for his/her architecture. */
9470 get_syscall_by_number (0, &s);
9471
9472 /* The allowed syntax is:
9473 catch syscall
9474 catch syscall <name | number> [<name | number> ... <name | number>]
9475
9476 Let's check if there's a syscall name. */
9477
9478 if (arg != NULL)
9479 filter = catch_syscall_split_args (arg);
9480 else
9481 filter = NULL;
9482
9483 create_syscall_event_catchpoint (tempflag, filter,
9484 &catch_syscall_breakpoint_ops);
9485}
9486
f7f9143b
JB
9487/* Implement the "catch assert" command. */
9488
9489static void
3e43a32a
MS
9490catch_assert_command (char *arg, int from_tty,
9491 struct cmd_list_element *command)
f7f9143b 9492{
a6d9a66e 9493 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9494 int tempflag;
f7f9143b
JB
9495 struct symtab_and_line sal;
9496 char *addr_string = NULL;
9497 struct breakpoint_ops *ops = NULL;
9498
44feb3ce
TT
9499 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9500
9501 if (!arg)
9502 arg = "";
f7f9143b 9503 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9504 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9505 ops, tempflag, from_tty);
f7f9143b
JB
9506}
9507
c906108c 9508static void
fba45db2 9509catch_command (char *arg, int from_tty)
c906108c 9510{
44feb3ce 9511 error (_("Catch requires an event name."));
c906108c
SS
9512}
9513\f
9514
9515static void
fba45db2 9516tcatch_command (char *arg, int from_tty)
c906108c 9517{
44feb3ce 9518 error (_("Catch requires an event name."));
c906108c
SS
9519}
9520
80f8a6eb 9521/* Delete breakpoints by address or line. */
c906108c
SS
9522
9523static void
fba45db2 9524clear_command (char *arg, int from_tty)
c906108c 9525{
d6e956e5
VP
9526 struct breakpoint *b;
9527 VEC(breakpoint_p) *found = 0;
9528 int ix;
c906108c
SS
9529 int default_match;
9530 struct symtabs_and_lines sals;
9531 struct symtab_and_line sal;
c906108c
SS
9532 int i;
9533
9534 if (arg)
9535 {
9536 sals = decode_line_spec (arg, 1);
9537 default_match = 0;
9538 }
9539 else
9540 {
c5aa993b 9541 sals.sals = (struct symtab_and_line *)
c906108c 9542 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9543 make_cleanup (xfree, sals.sals);
4a64f543 9544 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9545 sal.line = default_breakpoint_line;
9546 sal.symtab = default_breakpoint_symtab;
9547 sal.pc = default_breakpoint_address;
6c95b8df 9548 sal.pspace = default_breakpoint_pspace;
c906108c 9549 if (sal.symtab == 0)
8a3fe4f8 9550 error (_("No source file specified."));
c906108c
SS
9551
9552 sals.sals[0] = sal;
9553 sals.nelts = 1;
9554
9555 default_match = 1;
9556 }
9557
4a64f543
MS
9558 /* We don't call resolve_sal_pc here. That's not as bad as it
9559 seems, because all existing breakpoints typically have both
9560 file/line and pc set. So, if clear is given file/line, we can
9561 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9562
9563 We only support clearing given the address explicitly
9564 present in breakpoint table. Say, we've set breakpoint
4a64f543 9565 at file:line. There were several PC values for that file:line,
ed0616c6 9566 due to optimization, all in one block.
4a64f543
MS
9567
9568 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9569 PC corresponding to the same file:line, the breakpoint won't
9570 be cleared. We probably can still clear the breakpoint, but
9571 since the other PC value is never presented to user, user
9572 can only find it by guessing, and it does not seem important
9573 to support that. */
9574
4a64f543
MS
9575 /* For each line spec given, delete bps which correspond to it. Do
9576 it in two passes, solely to preserve the current behavior that
9577 from_tty is forced true if we delete more than one
9578 breakpoint. */
c906108c 9579
80f8a6eb 9580 found = NULL;
c906108c
SS
9581 for (i = 0; i < sals.nelts; i++)
9582 {
9583 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9584 If line given (pc == 0), clear all bpts on specified line.
9585 If defaulting, clear all bpts on default line
c906108c 9586 or at default pc.
c5aa993b
JM
9587
9588 defaulting sal.pc != 0 tests to do
9589
9590 0 1 pc
9591 1 1 pc _and_ line
9592 0 0 line
9593 1 0 <can't happen> */
c906108c
SS
9594
9595 sal = sals.sals[i];
c906108c 9596
4a64f543 9597 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9598 ALL_BREAKPOINTS (b)
c5aa993b 9599 {
0d381245 9600 int match = 0;
4a64f543 9601 /* Are we going to delete b? */
cc60f2e3 9602 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9603 {
9604 struct bp_location *loc = b->loc;
9605 for (; loc; loc = loc->next)
9606 {
6c95b8df
PA
9607 int pc_match = sal.pc
9608 && (loc->pspace == sal.pspace)
0d381245
VP
9609 && (loc->address == sal.pc)
9610 && (!section_is_overlay (loc->section)
9611 || loc->section == sal.section);
9612 int line_match = ((default_match || (0 == sal.pc))
9613 && b->source_file != NULL
9614 && sal.symtab != NULL
6c95b8df 9615 && sal.pspace == loc->pspace
0ba1096a
KT
9616 && filename_cmp (b->source_file,
9617 sal.symtab->filename) == 0
0d381245
VP
9618 && b->line_number == sal.line);
9619 if (pc_match || line_match)
9620 {
9621 match = 1;
9622 break;
9623 }
9624 }
9625 }
9626
9627 if (match)
d6e956e5 9628 VEC_safe_push(breakpoint_p, found, b);
c906108c 9629 }
80f8a6eb
MS
9630 }
9631 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9632 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9633 {
9634 if (arg)
8a3fe4f8 9635 error (_("No breakpoint at %s."), arg);
80f8a6eb 9636 else
8a3fe4f8 9637 error (_("No breakpoint at this line."));
80f8a6eb 9638 }
c906108c 9639
d6e956e5 9640 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9641 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9642 if (from_tty)
a3f17187 9643 {
d6e956e5 9644 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9645 printf_unfiltered (_("Deleted breakpoint "));
9646 else
9647 printf_unfiltered (_("Deleted breakpoints "));
9648 }
80f8a6eb 9649 breakpoints_changed ();
d6e956e5
VP
9650
9651 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9652 {
c5aa993b 9653 if (from_tty)
d6e956e5
VP
9654 printf_unfiltered ("%d ", b->number);
9655 delete_breakpoint (b);
c906108c 9656 }
80f8a6eb
MS
9657 if (from_tty)
9658 putchar_unfiltered ('\n');
c906108c
SS
9659}
9660\f
9661/* Delete breakpoint in BS if they are `delete' breakpoints and
9662 all breakpoints that are marked for deletion, whether hit or not.
9663 This is called after any breakpoint is hit, or after errors. */
9664
9665void
fba45db2 9666breakpoint_auto_delete (bpstat bs)
c906108c 9667{
35df4500 9668 struct breakpoint *b, *b_tmp;
c906108c
SS
9669
9670 for (; bs; bs = bs->next)
f431efe5
PA
9671 if (bs->breakpoint_at
9672 && bs->breakpoint_at->disposition == disp_del
c906108c 9673 && bs->stop)
f431efe5 9674 delete_breakpoint (bs->breakpoint_at);
c906108c 9675
35df4500 9676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9677 {
b5de0fa7 9678 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
9679 delete_breakpoint (b);
9680 }
c906108c
SS
9681}
9682
4a64f543
MS
9683/* A comparison function for bp_location AP and BP being interfaced to
9684 qsort. Sort elements primarily by their ADDRESS (no matter what
9685 does breakpoint_address_is_meaningful say for its OWNER),
9686 secondarily by ordering first bp_permanent OWNERed elements and
9687 terciarily just ensuring the array is sorted stable way despite
9688 qsort being an instable algorithm. */
876fa593
JK
9689
9690static int
494cfb0f 9691bp_location_compare (const void *ap, const void *bp)
876fa593 9692{
494cfb0f
JK
9693 struct bp_location *a = *(void **) ap;
9694 struct bp_location *b = *(void **) bp;
2bdf28a0 9695 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
9696 int a_perm = a->owner->enable_state == bp_permanent;
9697 int b_perm = b->owner->enable_state == bp_permanent;
9698
9699 if (a->address != b->address)
9700 return (a->address > b->address) - (a->address < b->address);
9701
9702 /* Sort permanent breakpoints first. */
9703 if (a_perm != b_perm)
9704 return (a_perm < b_perm) - (a_perm > b_perm);
9705
4a64f543
MS
9706 /* Make the user-visible order stable across GDB runs. Locations of
9707 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
9708
9709 if (a->owner->number != b->owner->number)
9710 return (a->owner->number > b->owner->number)
9711 - (a->owner->number < b->owner->number);
9712
9713 return (a > b) - (a < b);
9714}
9715
876fa593 9716/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
9717 bp_location_shadow_len_after_address_max according to the current
9718 content of the bp_location array. */
f7545552
TT
9719
9720static void
876fa593 9721bp_location_target_extensions_update (void)
f7545552 9722{
876fa593
JK
9723 struct bp_location *bl, **blp_tmp;
9724
9725 bp_location_placed_address_before_address_max = 0;
9726 bp_location_shadow_len_after_address_max = 0;
9727
9728 ALL_BP_LOCATIONS (bl, blp_tmp)
9729 {
9730 CORE_ADDR start, end, addr;
9731
9732 if (!bp_location_has_shadow (bl))
9733 continue;
9734
9735 start = bl->target_info.placed_address;
9736 end = start + bl->target_info.shadow_len;
9737
9738 gdb_assert (bl->address >= start);
9739 addr = bl->address - start;
9740 if (addr > bp_location_placed_address_before_address_max)
9741 bp_location_placed_address_before_address_max = addr;
9742
9743 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
9744
9745 gdb_assert (bl->address < end);
9746 addr = end - bl->address;
9747 if (addr > bp_location_shadow_len_after_address_max)
9748 bp_location_shadow_len_after_address_max = addr;
9749 }
f7545552
TT
9750}
9751
4cd9bd08 9752/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
9753 into the inferior, only remove already-inserted locations that no
9754 longer should be inserted. Functions that delete a breakpoint or
9755 breakpoints should pass false, so that deleting a breakpoint
9756 doesn't have the side effect of inserting the locations of other
9757 breakpoints that are marked not-inserted, but should_be_inserted
9758 returns true on them.
9759
9760 This behaviour is useful is situations close to tear-down -- e.g.,
9761 after an exec, while the target still has execution, but breakpoint
9762 shadows of the previous executable image should *NOT* be restored
9763 to the new image; or before detaching, where the target still has
9764 execution and wants to delete breakpoints from GDB's lists, and all
9765 breakpoints had already been removed from the inferior. */
9766
0d381245 9767static void
b60e7edf 9768update_global_location_list (int should_insert)
0d381245 9769{
74960c60 9770 struct breakpoint *b;
876fa593 9771 struct bp_location **locp, *loc;
f7545552
TT
9772 struct cleanup *cleanups;
9773
2d134ed3
PA
9774 /* Used in the duplicates detection below. When iterating over all
9775 bp_locations, points to the first bp_location of a given address.
9776 Breakpoints and watchpoints of different types are never
9777 duplicates of each other. Keep one pointer for each type of
9778 breakpoint/watchpoint, so we only need to loop over all locations
9779 once. */
9780 struct bp_location *bp_loc_first; /* breakpoint */
9781 struct bp_location *wp_loc_first; /* hardware watchpoint */
9782 struct bp_location *awp_loc_first; /* access watchpoint */
9783 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 9784
4a64f543
MS
9785 /* Saved former bp_location array which we compare against the newly
9786 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
9787 struct bp_location **old_location, **old_locp;
9788 unsigned old_location_count;
9789
9790 old_location = bp_location;
9791 old_location_count = bp_location_count;
9792 bp_location = NULL;
9793 bp_location_count = 0;
9794 cleanups = make_cleanup (xfree, old_location);
0d381245 9795
74960c60 9796 ALL_BREAKPOINTS (b)
876fa593
JK
9797 for (loc = b->loc; loc; loc = loc->next)
9798 bp_location_count++;
9799
9800 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
9801 locp = bp_location;
9802 ALL_BREAKPOINTS (b)
9803 for (loc = b->loc; loc; loc = loc->next)
9804 *locp++ = loc;
9805 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 9806 bp_location_compare);
876fa593
JK
9807
9808 bp_location_target_extensions_update ();
74960c60 9809
4a64f543
MS
9810 /* Identify bp_location instances that are no longer present in the
9811 new list, and therefore should be freed. Note that it's not
9812 necessary that those locations should be removed from inferior --
9813 if there's another location at the same address (previously
9814 marked as duplicate), we don't need to remove/insert the
9815 location.
876fa593 9816
4a64f543
MS
9817 LOCP is kept in sync with OLD_LOCP, each pointing to the current
9818 and former bp_location array state respectively. */
876fa593
JK
9819
9820 locp = bp_location;
9821 for (old_locp = old_location; old_locp < old_location + old_location_count;
9822 old_locp++)
74960c60 9823 {
876fa593 9824 struct bp_location *old_loc = *old_locp;
c7d46a38 9825 struct bp_location **loc2p;
876fa593 9826
4a64f543
MS
9827 /* Tells if 'old_loc' is found amoung the new locations. If
9828 not, we have to free it. */
c7d46a38 9829 int found_object = 0;
20874c92
VP
9830 /* Tells if the location should remain inserted in the target. */
9831 int keep_in_target = 0;
9832 int removed = 0;
876fa593 9833
4a64f543
MS
9834 /* Skip LOCP entries which will definitely never be needed.
9835 Stop either at or being the one matching OLD_LOC. */
876fa593 9836 while (locp < bp_location + bp_location_count
c7d46a38 9837 && (*locp)->address < old_loc->address)
876fa593 9838 locp++;
c7d46a38
PA
9839
9840 for (loc2p = locp;
9841 (loc2p < bp_location + bp_location_count
9842 && (*loc2p)->address == old_loc->address);
9843 loc2p++)
9844 {
9845 if (*loc2p == old_loc)
9846 {
9847 found_object = 1;
9848 break;
9849 }
9850 }
74960c60 9851
4a64f543
MS
9852 /* If this location is no longer present, and inserted, look if
9853 there's maybe a new location at the same address. If so,
9854 mark that one inserted, and don't remove this one. This is
9855 needed so that we don't have a time window where a breakpoint
9856 at certain location is not inserted. */
74960c60 9857
876fa593 9858 if (old_loc->inserted)
0d381245 9859 {
4a64f543
MS
9860 /* If the location is inserted now, we might have to remove
9861 it. */
74960c60 9862
876fa593 9863 if (found_object && should_be_inserted (old_loc))
74960c60 9864 {
4a64f543
MS
9865 /* The location is still present in the location list,
9866 and still should be inserted. Don't do anything. */
20874c92 9867 keep_in_target = 1;
74960c60
VP
9868 }
9869 else
9870 {
4a64f543
MS
9871 /* The location is either no longer present, or got
9872 disabled. See if there's another location at the
9873 same address, in which case we don't need to remove
9874 this one from the target. */
876fa593 9875
2bdf28a0 9876 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
9877 if (breakpoint_address_is_meaningful (old_loc->owner))
9878 {
876fa593 9879 for (loc2p = locp;
c7d46a38
PA
9880 (loc2p < bp_location + bp_location_count
9881 && (*loc2p)->address == old_loc->address);
876fa593
JK
9882 loc2p++)
9883 {
9884 struct bp_location *loc2 = *loc2p;
9885
2d134ed3 9886 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
9887 {
9888 /* For the sake of should_be_inserted.
4a64f543
MS
9889 Duplicates check below will fix up this
9890 later. */
c7d46a38 9891 loc2->duplicate = 0;
85d721b8
PA
9892
9893 /* Read watchpoint locations are switched to
9894 access watchpoints, if the former are not
9895 supported, but the latter are. */
9896 if (is_hardware_watchpoint (old_loc->owner))
9897 {
9898 gdb_assert (is_hardware_watchpoint (loc2->owner));
9899 loc2->watchpoint_type = old_loc->watchpoint_type;
9900 }
9901
c7d46a38
PA
9902 if (loc2 != old_loc && should_be_inserted (loc2))
9903 {
9904 loc2->inserted = 1;
9905 loc2->target_info = old_loc->target_info;
9906 keep_in_target = 1;
9907 break;
9908 }
876fa593
JK
9909 }
9910 }
9911 }
74960c60
VP
9912 }
9913
20874c92
VP
9914 if (!keep_in_target)
9915 {
876fa593 9916 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 9917 {
4a64f543
MS
9918 /* This is just about all we can do. We could keep
9919 this location on the global list, and try to
9920 remove it next time, but there's no particular
9921 reason why we will succeed next time.
20874c92 9922
4a64f543
MS
9923 Note that at this point, old_loc->owner is still
9924 valid, as delete_breakpoint frees the breakpoint
9925 only after calling us. */
3e43a32a
MS
9926 printf_filtered (_("warning: Error removing "
9927 "breakpoint %d\n"),
876fa593 9928 old_loc->owner->number);
20874c92
VP
9929 }
9930 removed = 1;
9931 }
0d381245 9932 }
74960c60
VP
9933
9934 if (!found_object)
1c5cfe86 9935 {
db82e815
PA
9936 if (removed && non_stop
9937 && breakpoint_address_is_meaningful (old_loc->owner)
9938 && !is_hardware_watchpoint (old_loc->owner))
20874c92 9939 {
db82e815
PA
9940 /* This location was removed from the target. In
9941 non-stop mode, a race condition is possible where
9942 we've removed a breakpoint, but stop events for that
9943 breakpoint are already queued and will arrive later.
9944 We apply an heuristic to be able to distinguish such
9945 SIGTRAPs from other random SIGTRAPs: we keep this
9946 breakpoint location for a bit, and will retire it
9947 after we see some number of events. The theory here
9948 is that reporting of events should, "on the average",
9949 be fair, so after a while we'll see events from all
9950 threads that have anything of interest, and no longer
9951 need to keep this breakpoint location around. We
9952 don't hold locations forever so to reduce chances of
9953 mistaking a non-breakpoint SIGTRAP for a breakpoint
9954 SIGTRAP.
9955
9956 The heuristic failing can be disastrous on
9957 decr_pc_after_break targets.
9958
9959 On decr_pc_after_break targets, like e.g., x86-linux,
9960 if we fail to recognize a late breakpoint SIGTRAP,
9961 because events_till_retirement has reached 0 too
9962 soon, we'll fail to do the PC adjustment, and report
9963 a random SIGTRAP to the user. When the user resumes
9964 the inferior, it will most likely immediately crash
2dec564e 9965 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
9966 corrupted, because of being resumed e.g., in the
9967 middle of a multi-byte instruction, or skipped a
9968 one-byte instruction. This was actually seen happen
9969 on native x86-linux, and should be less rare on
9970 targets that do not support new thread events, like
9971 remote, due to the heuristic depending on
9972 thread_count.
9973
9974 Mistaking a random SIGTRAP for a breakpoint trap
9975 causes similar symptoms (PC adjustment applied when
9976 it shouldn't), but then again, playing with SIGTRAPs
9977 behind the debugger's back is asking for trouble.
9978
9979 Since hardware watchpoint traps are always
9980 distinguishable from other traps, so we don't need to
9981 apply keep hardware watchpoint moribund locations
9982 around. We simply always ignore hardware watchpoint
9983 traps we can no longer explain. */
9984
876fa593
JK
9985 old_loc->events_till_retirement = 3 * (thread_count () + 1);
9986 old_loc->owner = NULL;
20874c92 9987
876fa593 9988 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
9989 }
9990 else
f431efe5
PA
9991 {
9992 old_loc->owner = NULL;
9993 decref_bp_location (&old_loc);
9994 }
20874c92 9995 }
74960c60 9996 }
1c5cfe86 9997
2d134ed3
PA
9998 /* Rescan breakpoints at the same address and section, marking the
9999 first one as "first" and any others as "duplicates". This is so
10000 that the bpt instruction is only inserted once. If we have a
10001 permanent breakpoint at the same place as BPT, make that one the
10002 official one, and the rest as duplicates. Permanent breakpoints
10003 are sorted first for the same address.
10004
10005 Do the same for hardware watchpoints, but also considering the
10006 watchpoint's type (regular/access/read) and length. */
876fa593 10007
2d134ed3
PA
10008 bp_loc_first = NULL;
10009 wp_loc_first = NULL;
10010 awp_loc_first = NULL;
10011 rwp_loc_first = NULL;
876fa593 10012 ALL_BP_LOCATIONS (loc, locp)
74960c60 10013 {
4a64f543
MS
10014 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10015 non-NULL. */
876fa593 10016 struct breakpoint *b = loc->owner;
2d134ed3 10017 struct bp_location **loc_first_p;
876fa593
JK
10018
10019 if (b->enable_state == bp_disabled
10020 || b->enable_state == bp_call_disabled
10021 || b->enable_state == bp_startup_disabled
10022 || !loc->enabled
10023 || loc->shlib_disabled
15c3d785 10024 || !breakpoint_address_is_meaningful (b)
d77f58be 10025 || is_tracepoint (b))
876fa593
JK
10026 continue;
10027
10028 /* Permanent breakpoint should always be inserted. */
10029 if (b->enable_state == bp_permanent && ! loc->inserted)
10030 internal_error (__FILE__, __LINE__,
10031 _("allegedly permanent breakpoint is not "
10032 "actually inserted"));
10033
2d134ed3
PA
10034 if (b->type == bp_hardware_watchpoint)
10035 loc_first_p = &wp_loc_first;
10036 else if (b->type == bp_read_watchpoint)
10037 loc_first_p = &rwp_loc_first;
10038 else if (b->type == bp_access_watchpoint)
10039 loc_first_p = &awp_loc_first;
10040 else
10041 loc_first_p = &bp_loc_first;
10042
10043 if (*loc_first_p == NULL
10044 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10045 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 10046 {
2d134ed3 10047 *loc_first_p = loc;
876fa593
JK
10048 loc->duplicate = 0;
10049 continue;
10050 }
10051
10052 loc->duplicate = 1;
10053
2d134ed3
PA
10054 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10055 && b->enable_state != bp_permanent)
876fa593
JK
10056 internal_error (__FILE__, __LINE__,
10057 _("another breakpoint was inserted on top of "
10058 "a permanent breakpoint"));
0d381245 10059 }
74960c60 10060
50c71eaf 10061 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 10062 && (have_live_inferiors ()
2567c7d9 10063 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 10064 insert_breakpoint_locations ();
f7545552
TT
10065
10066 do_cleanups (cleanups);
74960c60
VP
10067}
10068
20874c92
VP
10069void
10070breakpoint_retire_moribund (void)
10071{
10072 struct bp_location *loc;
10073 int ix;
10074
10075 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10076 if (--(loc->events_till_retirement) == 0)
10077 {
f431efe5 10078 decref_bp_location (&loc);
20874c92
VP
10079 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10080 --ix;
10081 }
10082}
10083
74960c60 10084static void
b60e7edf 10085update_global_location_list_nothrow (int inserting)
74960c60
VP
10086{
10087 struct gdb_exception e;
cc59ec59 10088
74960c60 10089 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 10090 update_global_location_list (inserting);
0d381245
VP
10091}
10092
f431efe5
PA
10093/* Clear BKP from a BPS. */
10094
a474d7c2 10095static void
f431efe5 10096bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
10097{
10098 bpstat bs;
cc59ec59 10099
a474d7c2 10100 for (bs = bps; bs; bs = bs->next)
f431efe5 10101 if (bs->breakpoint_at == bpt)
a474d7c2
PA
10102 {
10103 bs->breakpoint_at = NULL;
10104 bs->old_val = NULL;
10105 /* bs->commands will be freed later. */
10106 }
10107}
10108
10109/* Callback for iterate_over_threads. */
10110static int
f431efe5 10111bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10112{
f431efe5 10113 struct breakpoint *bpt = data;
cc59ec59 10114
16c381f0 10115 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10116 return 0;
10117}
10118
53a5351d 10119/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10120 structures. */
c906108c
SS
10121
10122void
fba45db2 10123delete_breakpoint (struct breakpoint *bpt)
c906108c 10124{
52f0bd74 10125 struct breakpoint *b;
c906108c 10126
8a3fe4f8 10127 gdb_assert (bpt != NULL);
c906108c 10128
4a64f543
MS
10129 /* Has this bp already been deleted? This can happen because
10130 multiple lists can hold pointers to bp's. bpstat lists are
10131 especial culprits.
10132
10133 One example of this happening is a watchpoint's scope bp. When
10134 the scope bp triggers, we notice that the watchpoint is out of
10135 scope, and delete it. We also delete its scope bp. But the
10136 scope bp is marked "auto-deleting", and is already on a bpstat.
10137 That bpstat is then checked for auto-deleting bp's, which are
10138 deleted.
10139
10140 A real solution to this problem might involve reference counts in
10141 bp's, and/or giving them pointers back to their referencing
10142 bpstat's, and teaching delete_breakpoint to only free a bp's
10143 storage when no more references were extent. A cheaper bandaid
10144 was chosen. */
c906108c
SS
10145 if (bpt->type == bp_none)
10146 return;
10147
4a64f543
MS
10148 /* At least avoid this stale reference until the reference counting
10149 of breakpoints gets resolved. */
d0fb5eae 10150 if (bpt->related_breakpoint != bpt)
e5a0a904 10151 {
d0fb5eae
JK
10152 struct breakpoint *related;
10153
10154 if (bpt->type == bp_watchpoint_scope)
10155 watchpoint_del_at_next_stop (bpt->related_breakpoint);
10156 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
10157 watchpoint_del_at_next_stop (bpt);
10158
10159 /* Unlink bpt from the bpt->related_breakpoint ring. */
10160 for (related = bpt; related->related_breakpoint != bpt;
10161 related = related->related_breakpoint);
10162 related->related_breakpoint = bpt->related_breakpoint;
10163 bpt->related_breakpoint = bpt;
e5a0a904
JK
10164 }
10165
383f836e 10166 observer_notify_breakpoint_deleted (bpt->number);
c906108c 10167
c906108c
SS
10168 if (breakpoint_chain == bpt)
10169 breakpoint_chain = bpt->next;
10170
c906108c
SS
10171 ALL_BREAKPOINTS (b)
10172 if (b->next == bpt)
c5aa993b
JM
10173 {
10174 b->next = bpt->next;
10175 break;
10176 }
c906108c 10177
9add0f1b 10178 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10179 xfree (bpt->cond_string);
10180 xfree (bpt->cond_exp);
10181 xfree (bpt->addr_string);
10182 xfree (bpt->exp);
10183 xfree (bpt->exp_string);
d63d0675 10184 xfree (bpt->exp_string_reparse);
60e1c644
PA
10185 value_free (bpt->val);
10186 xfree (bpt->source_file);
10187 xfree (bpt->exec_pathname);
a96d9b2e 10188 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 10189
f431efe5
PA
10190
10191 /* Be sure no bpstat's are pointing at the breakpoint after it's
10192 been freed. */
10193 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10194 in all threeds for now. Note that we cannot just remove bpstats
10195 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10196 commands are associated with the bpstat; if we remove it here,
10197 then the later call to bpstat_do_actions (&stop_bpstat); in
10198 event-top.c won't do anything, and temporary breakpoints with
10199 commands won't work. */
10200
10201 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10202
4a64f543
MS
10203 /* Now that breakpoint is removed from breakpoint list, update the
10204 global location list. This will remove locations that used to
10205 belong to this breakpoint. Do this before freeing the breakpoint
10206 itself, since remove_breakpoint looks at location's owner. It
10207 might be better design to have location completely
10208 self-contained, but it's not the case now. */
b60e7edf 10209 update_global_location_list (0);
74960c60
VP
10210
10211
4a64f543
MS
10212 /* On the chance that someone will soon try again to delete this
10213 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10214 bpt->type = bp_none;
10215
b8c9b27d 10216 xfree (bpt);
c906108c
SS
10217}
10218
4d6140d9
AC
10219static void
10220do_delete_breakpoint_cleanup (void *b)
10221{
10222 delete_breakpoint (b);
10223}
10224
10225struct cleanup *
10226make_cleanup_delete_breakpoint (struct breakpoint *b)
10227{
10228 return make_cleanup (do_delete_breakpoint_cleanup, b);
10229}
10230
95a42b64
TT
10231/* A callback for map_breakpoint_numbers that calls
10232 delete_breakpoint. */
10233
10234static void
10235do_delete_breakpoint (struct breakpoint *b, void *ignore)
10236{
10237 delete_breakpoint (b);
10238}
10239
c906108c 10240void
fba45db2 10241delete_command (char *arg, int from_tty)
c906108c 10242{
35df4500 10243 struct breakpoint *b, *b_tmp;
c906108c 10244
ea9365bb
TT
10245 dont_repeat ();
10246
c906108c
SS
10247 if (arg == 0)
10248 {
10249 int breaks_to_delete = 0;
10250
10251 /* Delete all breakpoints if no argument.
4a64f543
MS
10252 Do not delete internal or call-dummy breakpoints, these have
10253 to be deleted with an explicit breakpoint number argument. */
c5aa993b
JM
10254 ALL_BREAKPOINTS (b)
10255 {
059fb39f 10256 if (b->type != bp_call_dummy
aa7d318d 10257 && b->type != bp_std_terminate
059fb39f 10258 && b->type != bp_shlib_event
4efc6507 10259 && b->type != bp_jit_event
059fb39f
PM
10260 && b->type != bp_thread_event
10261 && b->type != bp_overlay_event
0fd8e87f 10262 && b->type != bp_longjmp_master
aa7d318d 10263 && b->type != bp_std_terminate_master
186c406b 10264 && b->type != bp_exception_master
059fb39f 10265 && b->number >= 0)
973d738b
DJ
10266 {
10267 breaks_to_delete = 1;
10268 break;
10269 }
c5aa993b 10270 }
c906108c
SS
10271
10272 /* Ask user only if there are some breakpoints to delete. */
10273 if (!from_tty
e2e0b3e5 10274 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10275 {
35df4500 10276 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10277 {
059fb39f 10278 if (b->type != bp_call_dummy
aa7d318d 10279 && b->type != bp_std_terminate
059fb39f
PM
10280 && b->type != bp_shlib_event
10281 && b->type != bp_thread_event
4efc6507 10282 && b->type != bp_jit_event
059fb39f 10283 && b->type != bp_overlay_event
0fd8e87f 10284 && b->type != bp_longjmp_master
aa7d318d 10285 && b->type != bp_std_terminate_master
186c406b 10286 && b->type != bp_exception_master
059fb39f 10287 && b->number >= 0)
c5aa993b
JM
10288 delete_breakpoint (b);
10289 }
c906108c
SS
10290 }
10291 }
10292 else
95a42b64 10293 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
10294}
10295
0d381245
VP
10296static int
10297all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 10298{
0d381245
VP
10299 for (; loc; loc = loc->next)
10300 if (!loc->shlib_disabled)
10301 return 0;
10302 return 1;
fe3f5fa8
VP
10303}
10304
776592bf
DE
10305/* Subroutine of update_breakpoint_locations to simplify it.
10306 Return non-zero if multiple fns in list LOC have the same name.
10307 Null names are ignored. */
10308
10309static int
10310ambiguous_names_p (struct bp_location *loc)
10311{
10312 struct bp_location *l;
10313 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
10314 (int (*) (const void *,
10315 const void *)) streq,
776592bf
DE
10316 NULL, xcalloc, xfree);
10317
10318 for (l = loc; l != NULL; l = l->next)
10319 {
10320 const char **slot;
10321 const char *name = l->function_name;
10322
10323 /* Allow for some names to be NULL, ignore them. */
10324 if (name == NULL)
10325 continue;
10326
10327 slot = (const char **) htab_find_slot (htab, (const void *) name,
10328 INSERT);
4a64f543
MS
10329 /* NOTE: We can assume slot != NULL here because xcalloc never
10330 returns NULL. */
776592bf
DE
10331 if (*slot != NULL)
10332 {
10333 htab_delete (htab);
10334 return 1;
10335 }
10336 *slot = name;
10337 }
10338
10339 htab_delete (htab);
10340 return 0;
10341}
10342
0fb4aa4b
PA
10343/* When symbols change, it probably means the sources changed as well,
10344 and it might mean the static tracepoint markers are no longer at
10345 the same address or line numbers they used to be at last we
10346 checked. Losing your static tracepoints whenever you rebuild is
10347 undesirable. This function tries to resync/rematch gdb static
10348 tracepoints with the markers on the target, for static tracepoints
10349 that have not been set by marker id. Static tracepoint that have
10350 been set by marker id are reset by marker id in breakpoint_re_set.
10351 The heuristic is:
10352
10353 1) For a tracepoint set at a specific address, look for a marker at
10354 the old PC. If one is found there, assume to be the same marker.
10355 If the name / string id of the marker found is different from the
10356 previous known name, assume that means the user renamed the marker
10357 in the sources, and output a warning.
10358
10359 2) For a tracepoint set at a given line number, look for a marker
10360 at the new address of the old line number. If one is found there,
10361 assume to be the same marker. If the name / string id of the
10362 marker found is different from the previous known name, assume that
10363 means the user renamed the marker in the sources, and output a
10364 warning.
10365
10366 3) If a marker is no longer found at the same address or line, it
10367 may mean the marker no longer exists. But it may also just mean
10368 the code changed a bit. Maybe the user added a few lines of code
10369 that made the marker move up or down (in line number terms). Ask
10370 the target for info about the marker with the string id as we knew
10371 it. If found, update line number and address in the matching
10372 static tracepoint. This will get confused if there's more than one
10373 marker with the same ID (possible in UST, although unadvised
10374 precisely because it confuses tools). */
10375
10376static struct symtab_and_line
10377update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10378{
10379 struct static_tracepoint_marker marker;
10380 CORE_ADDR pc;
10381 int i;
10382
10383 pc = sal.pc;
10384 if (sal.line)
10385 find_line_pc (sal.symtab, sal.line, &pc);
10386
10387 if (target_static_tracepoint_marker_at (pc, &marker))
10388 {
10389 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10390 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10391 b->number,
10392 b->static_trace_marker_id, marker.str_id);
10393
10394 xfree (b->static_trace_marker_id);
10395 b->static_trace_marker_id = xstrdup (marker.str_id);
10396 release_static_tracepoint_marker (&marker);
10397
10398 return sal;
10399 }
10400
10401 /* Old marker wasn't found on target at lineno. Try looking it up
10402 by string ID. */
10403 if (!sal.explicit_pc
10404 && sal.line != 0
10405 && sal.symtab != NULL
10406 && b->static_trace_marker_id != NULL)
10407 {
10408 VEC(static_tracepoint_marker_p) *markers;
10409
10410 markers
10411 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10412
10413 if (!VEC_empty(static_tracepoint_marker_p, markers))
10414 {
10415 struct symtab_and_line sal;
10416 struct symbol *sym;
10417 struct static_tracepoint_marker *marker;
10418
10419 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10420
10421 xfree (b->static_trace_marker_id);
10422 b->static_trace_marker_id = xstrdup (marker->str_id);
10423
10424 warning (_("marker for static tracepoint %d (%s) not "
10425 "found at previous line number"),
10426 b->number, b->static_trace_marker_id);
10427
10428 init_sal (&sal);
10429
10430 sal.pc = marker->address;
10431
10432 sal = find_pc_line (marker->address, 0);
10433 sym = find_pc_sect_function (marker->address, NULL);
10434 ui_out_text (uiout, "Now in ");
10435 if (sym)
10436 {
10437 ui_out_field_string (uiout, "func",
10438 SYMBOL_PRINT_NAME (sym));
10439 ui_out_text (uiout, " at ");
10440 }
10441 ui_out_field_string (uiout, "file", sal.symtab->filename);
10442 ui_out_text (uiout, ":");
10443
10444 if (ui_out_is_mi_like_p (uiout))
10445 {
10446 char *fullname = symtab_to_fullname (sal.symtab);
10447
10448 if (fullname)
10449 ui_out_field_string (uiout, "fullname", fullname);
10450 }
10451
10452 ui_out_field_int (uiout, "line", sal.line);
10453 ui_out_text (uiout, "\n");
10454
10455 b->line_number = sal.line;
10456
10457 xfree (b->source_file);
10458 if (sym)
10459 b->source_file = xstrdup (sal.symtab->filename);
10460 else
10461 b->source_file = NULL;
10462
10463 xfree (b->addr_string);
10464 b->addr_string = xstrprintf ("%s:%d",
10465 sal.symtab->filename, b->line_number);
10466
10467 /* Might be nice to check if function changed, and warn if
10468 so. */
10469
10470 release_static_tracepoint_marker (marker);
10471 }
10472 }
10473 return sal;
10474}
10475
0e30163f 10476void
0d381245
VP
10477update_breakpoint_locations (struct breakpoint *b,
10478 struct symtabs_and_lines sals)
fe3f5fa8
VP
10479{
10480 int i;
10481 char *s;
0d381245
VP
10482 struct bp_location *existing_locations = b->loc;
10483
4a64f543
MS
10484 /* If there's no new locations, and all existing locations are
10485 pending, don't do anything. This optimizes the common case where
10486 all locations are in the same shared library, that was unloaded.
10487 We'd like to retain the location, so that when the library is
10488 loaded again, we don't loose the enabled/disabled status of the
10489 individual locations. */
0d381245 10490 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10491 return;
10492
fe3f5fa8
VP
10493 b->loc = NULL;
10494
0d381245 10495 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10496 {
0d381245 10497 struct bp_location *new_loc =
39d61571 10498 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10499
0d381245
VP
10500 /* Reparse conditions, they might contain references to the
10501 old symtab. */
10502 if (b->cond_string != NULL)
10503 {
10504 struct gdb_exception e;
fe3f5fa8 10505
0d381245
VP
10506 s = b->cond_string;
10507 TRY_CATCH (e, RETURN_MASK_ERROR)
10508 {
10509 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10510 0);
10511 }
10512 if (e.reason < 0)
10513 {
3e43a32a
MS
10514 warning (_("failed to reevaluate condition "
10515 "for breakpoint %d: %s"),
0d381245
VP
10516 b->number, e.message);
10517 new_loc->enabled = 0;
10518 }
10519 }
fe3f5fa8 10520
0d381245
VP
10521 if (b->source_file != NULL)
10522 xfree (b->source_file);
10523 if (sals.sals[i].symtab == NULL)
10524 b->source_file = NULL;
10525 else
1b36a34b 10526 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10527
0d381245
VP
10528 if (b->line_number == 0)
10529 b->line_number = sals.sals[i].line;
10530 }
fe3f5fa8 10531
514f746b
AR
10532 /* Update locations of permanent breakpoints. */
10533 if (b->enable_state == bp_permanent)
10534 make_breakpoint_permanent (b);
10535
4a64f543
MS
10536 /* If possible, carry over 'disable' status from existing
10537 breakpoints. */
0d381245
VP
10538 {
10539 struct bp_location *e = existing_locations;
776592bf
DE
10540 /* If there are multiple breakpoints with the same function name,
10541 e.g. for inline functions, comparing function names won't work.
10542 Instead compare pc addresses; this is just a heuristic as things
10543 may have moved, but in practice it gives the correct answer
10544 often enough until a better solution is found. */
10545 int have_ambiguous_names = ambiguous_names_p (b->loc);
10546
0d381245
VP
10547 for (; e; e = e->next)
10548 {
10549 if (!e->enabled && e->function_name)
10550 {
10551 struct bp_location *l = b->loc;
776592bf
DE
10552 if (have_ambiguous_names)
10553 {
10554 for (; l; l = l->next)
6c95b8df
PA
10555 if (breakpoint_address_match (e->pspace->aspace, e->address,
10556 l->pspace->aspace, l->address))
776592bf
DE
10557 {
10558 l->enabled = 0;
10559 break;
10560 }
10561 }
10562 else
10563 {
10564 for (; l; l = l->next)
10565 if (l->function_name
10566 && strcmp (e->function_name, l->function_name) == 0)
10567 {
10568 l->enabled = 0;
10569 break;
10570 }
10571 }
0d381245
VP
10572 }
10573 }
10574 }
fe3f5fa8 10575
b60e7edf 10576 update_global_location_list (1);
fe3f5fa8
VP
10577}
10578
ef23e705
TJB
10579/* Find the SaL locations corresponding to the given ADDR_STRING.
10580 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
10581
10582static struct symtabs_and_lines
10583addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
10584{
10585 char *s;
10586 int marker_spec, not_found;
02d20e4a 10587 struct symtabs_and_lines sals = {0};
ef23e705
TJB
10588 struct gdb_exception e;
10589
10590 s = addr_string;
10591 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10592
10593 TRY_CATCH (e, RETURN_MASK_ERROR)
10594 {
10595 if (marker_spec)
10596 {
10597 sals = decode_static_tracepoint_spec (&s);
10598 if (sals.nelts > b->static_trace_marker_id_idx)
10599 {
10600 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10601 sals.nelts = 1;
10602 }
10603 else
10604 error (_("marker %s not found"), b->static_trace_marker_id);
10605 }
10606 else
10607 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10608 NULL, &not_found);
10609 }
10610 if (e.reason < 0)
10611 {
10612 int not_found_and_ok = 0;
10613 /* For pending breakpoints, it's expected that parsing will
10614 fail until the right shared library is loaded. User has
10615 already told to create pending breakpoints and don't need
10616 extra messages. If breakpoint is in bp_shlib_disabled
10617 state, then user already saw the message about that
10618 breakpoint being disabled, and don't want to see more
10619 errors. */
10620 if (not_found
10621 && (b->condition_not_parsed
10622 || (b->loc && b->loc->shlib_disabled)
10623 || b->enable_state == bp_disabled))
10624 not_found_and_ok = 1;
10625
10626 if (!not_found_and_ok)
10627 {
10628 /* We surely don't want to warn about the same breakpoint
10629 10 times. One solution, implemented here, is disable
10630 the breakpoint on error. Another solution would be to
10631 have separate 'warning emitted' flag. Since this
10632 happens only when a binary has changed, I don't know
10633 which approach is better. */
10634 b->enable_state = bp_disabled;
10635 throw_exception (e);
10636 }
10637 }
10638
10639 if (!not_found)
10640 {
10641 gdb_assert (sals.nelts == 1);
10642
10643 resolve_sal_pc (&sals.sals[0]);
10644 if (b->condition_not_parsed && s && s[0])
10645 {
10646 char *cond_string = 0;
10647 int thread = -1;
10648 int task = 0;
10649
10650 find_condition_and_thread (s, sals.sals[0].pc,
10651 &cond_string, &thread, &task);
10652 if (cond_string)
10653 b->cond_string = cond_string;
10654 b->thread = thread;
10655 b->task = task;
10656 b->condition_not_parsed = 0;
10657 }
10658
10659 if (b->type == bp_static_tracepoint && !marker_spec)
10660 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
10661 }
10662
10663 *found = !not_found;
10664
10665 return sals;
10666}
10667
10668/* Reevaluate a hardware or software breakpoint and recreate its locations.
10669 This is necessary after symbols are read (e.g., an executable or DSO
10670 was loaded, or the inferior just started). */
10671
10672static void
10673re_set_breakpoint (struct breakpoint *b)
10674{
10675 int found;
10676 struct symtabs_and_lines sals;
10677 struct symtabs_and_lines expanded = {0};
10678 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
10679
10680 input_radix = b->input_radix;
10681 save_current_space_and_thread ();
10682 switch_to_program_space_and_thread (b->pspace);
10683 set_language (b->language);
10684
10685 sals = addr_string_to_sals (b, b->addr_string, &found);
10686 if (found)
10687 {
10688 make_cleanup (xfree, sals.sals);
10689 expanded = expand_line_sal_maybe (sals.sals[0]);
10690 }
10691
10692 update_breakpoint_locations (b, expanded);
10693 do_cleanups (cleanups);
10694}
10695
c906108c
SS
10696/* Reset a breakpoint given it's struct breakpoint * BINT.
10697 The value we return ends up being the return value from catch_errors.
10698 Unused in this case. */
10699
10700static int
4efb68b1 10701breakpoint_re_set_one (void *bint)
c906108c 10702{
4a64f543 10703 /* Get past catch_errs. */
53a5351d 10704 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
10705
10706 switch (b->type)
10707 {
10708 case bp_none:
8a3fe4f8 10709 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 10710 b->number);
c906108c
SS
10711 return 0;
10712 case bp_breakpoint:
10713 case bp_hardware_breakpoint:
1042e4c0 10714 case bp_tracepoint:
7a697b8d 10715 case bp_fast_tracepoint:
0fb4aa4b 10716 case bp_static_tracepoint:
0e30163f 10717 case bp_gnu_ifunc_resolver:
8bea4e01
UW
10718 /* Do not attempt to re-set breakpoints disabled during startup. */
10719 if (b->enable_state == bp_startup_disabled)
10720 return 0;
10721
c906108c
SS
10722 if (b->addr_string == NULL)
10723 {
4a64f543 10724 /* Anything without a string can't be re-set. */
c906108c
SS
10725 delete_breakpoint (b);
10726 return 0;
10727 }
c906108c 10728
ef23e705 10729 re_set_breakpoint (b);
c906108c
SS
10730 break;
10731
10732 case bp_watchpoint:
10733 case bp_hardware_watchpoint:
10734 case bp_read_watchpoint:
10735 case bp_access_watchpoint:
4a64f543
MS
10736 /* Watchpoint can be either on expression using entirely global
10737 variables, or it can be on local variables.
10738
10739 Watchpoints of the first kind are never auto-deleted, and
10740 even persist across program restarts. Since they can use
10741 variables from shared libraries, we need to reparse
10742 expression as libraries are loaded and unloaded.
10743
10744 Watchpoints on local variables can also change meaning as
10745 result of solib event. For example, if a watchpoint uses
10746 both a local and a global variables in expression, it's a
10747 local watchpoint, but unloading of a shared library will make
10748 the expression invalid. This is not a very common use case,
10749 but we still re-evaluate expression, to avoid surprises to
10750 the user.
0b3de036
VP
10751
10752 Note that for local watchpoints, we re-evaluate it only if
10753 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
10754 the watchpoint is out of scope and will be deleted soon. In
10755 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
10756
10757 If a local watchpoint's frame id is still valid, then
4a64f543 10758 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
10759
10760 Don't do anything about disabled watchpoints, since they will
10761 be reevaluated again when enabled. */
a5606eee 10762 update_watchpoint (b, 1 /* reparse */);
c906108c 10763 break;
c5aa993b
JM
10764 /* We needn't really do anything to reset these, since the mask
10765 that requests them is unaffected by e.g., new libraries being
4a64f543 10766 loaded. */
ce78b96d 10767 case bp_catchpoint:
c906108c 10768 break;
c5aa993b 10769
c906108c 10770 default:
a3f17187 10771 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 10772 /* fall through */
0fd8e87f
UW
10773 /* Delete overlay event and longjmp master breakpoints; they will be
10774 reset later by breakpoint_re_set. */
1900040c 10775 case bp_overlay_event:
0fd8e87f 10776 case bp_longjmp_master:
aa7d318d 10777 case bp_std_terminate_master:
186c406b 10778 case bp_exception_master:
c906108c
SS
10779 delete_breakpoint (b);
10780 break;
10781
c5aa993b
JM
10782 /* This breakpoint is special, it's set up when the inferior
10783 starts and we really don't want to touch it. */
c906108c
SS
10784 case bp_shlib_event:
10785
c4093a6a
JM
10786 /* Like bp_shlib_event, this breakpoint type is special.
10787 Once it is set up, we do not want to touch it. */
10788 case bp_thread_event:
10789
4a64f543
MS
10790 /* Keep temporary breakpoints, which can be encountered when we
10791 step over a dlopen call and SOLIB_ADD is resetting the
10792 breakpoints. Otherwise these should have been blown away via
10793 the cleanup chain or by breakpoint_init_inferior when we
10794 rerun the executable. */
c906108c
SS
10795 case bp_until:
10796 case bp_finish:
10797 case bp_watchpoint_scope:
10798 case bp_call_dummy:
aa7d318d 10799 case bp_std_terminate:
c906108c 10800 case bp_step_resume:
611c83ae
PA
10801 case bp_longjmp:
10802 case bp_longjmp_resume:
186c406b
TT
10803 case bp_exception:
10804 case bp_exception_resume:
4efc6507 10805 case bp_jit_event:
0e30163f 10806 case bp_gnu_ifunc_resolver_return:
c906108c
SS
10807 break;
10808 }
10809
10810 return 0;
10811}
10812
69de3c6a 10813/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 10814void
69de3c6a 10815breakpoint_re_set (void)
c906108c 10816{
35df4500 10817 struct breakpoint *b, *b_tmp;
c906108c
SS
10818 enum language save_language;
10819 int save_input_radix;
6c95b8df 10820 struct cleanup *old_chain;
c5aa993b 10821
c906108c
SS
10822 save_language = current_language->la_language;
10823 save_input_radix = input_radix;
6c95b8df
PA
10824 old_chain = save_current_program_space ();
10825
35df4500 10826 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10827 {
4a64f543 10828 /* Format possible error msg. */
fe3f5fa8 10829 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
10830 b->number);
10831 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 10832 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 10833 do_cleanups (cleanups);
c5aa993b 10834 }
c906108c
SS
10835 set_language (save_language);
10836 input_radix = save_input_radix;
e62c965a 10837
0756c555 10838 jit_breakpoint_re_set ();
4efc6507 10839
6c95b8df
PA
10840 do_cleanups (old_chain);
10841
af02033e
PP
10842 create_overlay_event_breakpoint ();
10843 create_longjmp_master_breakpoint ();
10844 create_std_terminate_master_breakpoint ();
186c406b 10845 create_exception_master_breakpoint ();
c906108c
SS
10846}
10847\f
c906108c
SS
10848/* Reset the thread number of this breakpoint:
10849
10850 - If the breakpoint is for all threads, leave it as-is.
4a64f543 10851 - Else, reset it to the current thread for inferior_ptid. */
c906108c 10852void
fba45db2 10853breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
10854{
10855 if (b->thread != -1)
10856 {
39f77062
KB
10857 if (in_thread_list (inferior_ptid))
10858 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
10859
10860 /* We're being called after following a fork. The new fork is
10861 selected as current, and unless this was a vfork will have a
10862 different program space from the original thread. Reset that
10863 as well. */
10864 b->loc->pspace = current_program_space;
c906108c
SS
10865 }
10866}
10867
03ac34d5
MS
10868/* Set ignore-count of breakpoint number BPTNUM to COUNT.
10869 If from_tty is nonzero, it prints a message to that effect,
10870 which ends with a period (no newline). */
10871
c906108c 10872void
fba45db2 10873set_ignore_count (int bptnum, int count, int from_tty)
c906108c 10874{
52f0bd74 10875 struct breakpoint *b;
c906108c
SS
10876
10877 if (count < 0)
10878 count = 0;
10879
10880 ALL_BREAKPOINTS (b)
10881 if (b->number == bptnum)
c5aa993b 10882 {
d77f58be
SS
10883 if (is_tracepoint (b))
10884 {
10885 if (from_tty && count != 0)
10886 printf_filtered (_("Ignore count ignored for tracepoint %d."),
10887 bptnum);
10888 return;
10889 }
10890
c5aa993b 10891 b->ignore_count = count;
221ea385
KS
10892 if (from_tty)
10893 {
10894 if (count == 0)
3e43a32a
MS
10895 printf_filtered (_("Will stop next time "
10896 "breakpoint %d is reached."),
221ea385
KS
10897 bptnum);
10898 else if (count == 1)
a3f17187 10899 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
10900 bptnum);
10901 else
3e43a32a
MS
10902 printf_filtered (_("Will ignore next %d "
10903 "crossings of breakpoint %d."),
221ea385
KS
10904 count, bptnum);
10905 }
c5aa993b 10906 breakpoints_changed ();
383f836e 10907 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
10908 return;
10909 }
c906108c 10910
8a3fe4f8 10911 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
10912}
10913
c906108c
SS
10914/* Command to set ignore-count of breakpoint N to COUNT. */
10915
10916static void
fba45db2 10917ignore_command (char *args, int from_tty)
c906108c
SS
10918{
10919 char *p = args;
52f0bd74 10920 int num;
c906108c
SS
10921
10922 if (p == 0)
e2e0b3e5 10923 error_no_arg (_("a breakpoint number"));
c5aa993b 10924
c906108c 10925 num = get_number (&p);
5c44784c 10926 if (num == 0)
8a3fe4f8 10927 error (_("bad breakpoint number: '%s'"), args);
c906108c 10928 if (*p == 0)
8a3fe4f8 10929 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
10930
10931 set_ignore_count (num,
10932 longest_to_int (value_as_long (parse_and_eval (p))),
10933 from_tty);
221ea385
KS
10934 if (from_tty)
10935 printf_filtered ("\n");
c906108c
SS
10936}
10937\f
10938/* Call FUNCTION on each of the breakpoints
10939 whose numbers are given in ARGS. */
10940
10941static void
95a42b64
TT
10942map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
10943 void *),
10944 void *data)
c906108c 10945{
52f0bd74
AC
10946 int num;
10947 struct breakpoint *b, *tmp;
11cf8741 10948 int match;
197f0a60 10949 struct get_number_or_range_state state;
c906108c 10950
197f0a60 10951 if (args == 0)
e2e0b3e5 10952 error_no_arg (_("one or more breakpoint numbers"));
c906108c 10953
197f0a60
TT
10954 init_number_or_range (&state, args);
10955
10956 while (!state.finished)
c906108c 10957 {
197f0a60
TT
10958 char *p = state.string;
10959
11cf8741 10960 match = 0;
c5aa993b 10961
197f0a60 10962 num = get_number_or_range (&state);
5c44784c 10963 if (num == 0)
c5aa993b 10964 {
8a3fe4f8 10965 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
10966 }
10967 else
10968 {
10969 ALL_BREAKPOINTS_SAFE (b, tmp)
10970 if (b->number == num)
10971 {
d0fb5eae
JK
10972 struct breakpoint *related_breakpoint;
10973
11cf8741 10974 match = 1;
d0fb5eae
JK
10975 related_breakpoint = b;
10976 do
10977 {
10978 struct breakpoint *next_related_b;
10979
10980 /* FUNCTION can be also delete_breakpoint. */
10981 next_related_b = related_breakpoint->related_breakpoint;
10982 function (related_breakpoint, data);
10983
10984 /* For delete_breakpoint of the last entry of the ring we
10985 were traversing we would never get back to B. */
10986 if (next_related_b == related_breakpoint)
10987 break;
10988 related_breakpoint = next_related_b;
10989 }
10990 while (related_breakpoint != b);
11cf8741 10991 break;
5c44784c 10992 }
11cf8741 10993 if (match == 0)
a3f17187 10994 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 10995 }
c906108c
SS
10996 }
10997}
10998
0d381245
VP
10999static struct bp_location *
11000find_location_by_number (char *number)
11001{
11002 char *dot = strchr (number, '.');
11003 char *p1;
11004 int bp_num;
11005 int loc_num;
11006 struct breakpoint *b;
11007 struct bp_location *loc;
11008
11009 *dot = '\0';
11010
11011 p1 = number;
197f0a60 11012 bp_num = get_number (&p1);
0d381245
VP
11013 if (bp_num == 0)
11014 error (_("Bad breakpoint number '%s'"), number);
11015
11016 ALL_BREAKPOINTS (b)
11017 if (b->number == bp_num)
11018 {
11019 break;
11020 }
11021
11022 if (!b || b->number != bp_num)
11023 error (_("Bad breakpoint number '%s'"), number);
11024
11025 p1 = dot+1;
197f0a60 11026 loc_num = get_number (&p1);
0d381245
VP
11027 if (loc_num == 0)
11028 error (_("Bad breakpoint location number '%s'"), number);
11029
11030 --loc_num;
11031 loc = b->loc;
11032 for (;loc_num && loc; --loc_num, loc = loc->next)
11033 ;
11034 if (!loc)
11035 error (_("Bad breakpoint location number '%s'"), dot+1);
11036
11037 return loc;
11038}
11039
11040
1900040c
MS
11041/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11042 If from_tty is nonzero, it prints a message to that effect,
11043 which ends with a period (no newline). */
11044
c906108c 11045void
fba45db2 11046disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11047{
11048 /* Never disable a watchpoint scope breakpoint; we want to
11049 hit them when we leave scope so we can delete both the
11050 watchpoint and its scope breakpoint at that time. */
11051 if (bpt->type == bp_watchpoint_scope)
11052 return;
11053
c2c6d25f 11054 /* You can't disable permanent breakpoints. */
b5de0fa7 11055 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11056 return;
11057
b5de0fa7 11058 bpt->enable_state = bp_disabled;
c906108c 11059
b60e7edf 11060 update_global_location_list (0);
c906108c 11061
383f836e 11062 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11063}
11064
95a42b64
TT
11065/* A callback for map_breakpoint_numbers that calls
11066 disable_breakpoint. */
11067
11068static void
11069do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11070{
11071 disable_breakpoint (b);
11072}
11073
c906108c 11074static void
fba45db2 11075disable_command (char *args, int from_tty)
c906108c 11076{
52f0bd74 11077 struct breakpoint *bpt;
cc59ec59 11078
c906108c
SS
11079 if (args == 0)
11080 ALL_BREAKPOINTS (bpt)
11081 switch (bpt->type)
c5aa993b
JM
11082 {
11083 case bp_none:
8a3fe4f8 11084 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 11085 bpt->number);
488919c4 11086 break;
c5aa993b 11087 case bp_breakpoint:
1042e4c0 11088 case bp_tracepoint:
7a697b8d 11089 case bp_fast_tracepoint:
0fb4aa4b 11090 case bp_static_tracepoint:
ce78b96d 11091 case bp_catchpoint:
c5aa993b
JM
11092 case bp_hardware_breakpoint:
11093 case bp_watchpoint:
11094 case bp_hardware_watchpoint:
11095 case bp_read_watchpoint:
11096 case bp_access_watchpoint:
11097 disable_breakpoint (bpt);
488919c4 11098 break;
c5aa993b 11099 default:
488919c4 11100 break;
c5aa993b 11101 }
0d381245
VP
11102 else if (strchr (args, '.'))
11103 {
11104 struct bp_location *loc = find_location_by_number (args);
11105 if (loc)
11106 loc->enabled = 0;
b60e7edf 11107 update_global_location_list (0);
0d381245 11108 }
c906108c 11109 else
95a42b64 11110 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11111}
11112
11113static void
fba45db2 11114do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11115{
afe38095 11116 int target_resources_ok;
c906108c
SS
11117
11118 if (bpt->type == bp_hardware_breakpoint)
11119 {
11120 int i;
c5aa993b 11121 i = hw_breakpoint_used_count ();
53a5351d 11122 target_resources_ok =
d92524f1 11123 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11124 i + 1, 0);
c906108c 11125 if (target_resources_ok == 0)
8a3fe4f8 11126 error (_("No hardware breakpoint support in the target."));
c906108c 11127 else if (target_resources_ok < 0)
8a3fe4f8 11128 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11129 }
11130
cc60f2e3 11131 if (is_watchpoint (bpt))
c906108c 11132 {
dde02812
ES
11133 struct gdb_exception e;
11134
11135 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11136 {
dde02812 11137 update_watchpoint (bpt, 1 /* reparse */);
c906108c 11138 }
dde02812 11139 if (e.reason < 0)
c5aa993b 11140 {
dde02812
ES
11141 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11142 bpt->number);
11143 return;
c5aa993b 11144 }
c906108c 11145 }
0101ce28 11146
b4c291bb
KH
11147 if (bpt->enable_state != bp_permanent)
11148 bpt->enable_state = bp_enabled;
11149 bpt->disposition = disposition;
b60e7edf 11150 update_global_location_list (1);
b4c291bb
KH
11151 breakpoints_changed ();
11152
383f836e 11153 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11154}
11155
fe3f5fa8 11156
c906108c 11157void
fba45db2 11158enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11159{
11160 do_enable_breakpoint (bpt, bpt->disposition);
11161}
11162
95a42b64
TT
11163/* A callback for map_breakpoint_numbers that calls
11164 enable_breakpoint. */
11165
11166static void
11167do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11168{
11169 enable_breakpoint (b);
11170}
11171
c906108c
SS
11172/* The enable command enables the specified breakpoints (or all defined
11173 breakpoints) so they once again become (or continue to be) effective
1272ad14 11174 in stopping the inferior. */
c906108c 11175
c906108c 11176static void
fba45db2 11177enable_command (char *args, int from_tty)
c906108c 11178{
52f0bd74 11179 struct breakpoint *bpt;
cc59ec59 11180
c906108c
SS
11181 if (args == 0)
11182 ALL_BREAKPOINTS (bpt)
11183 switch (bpt->type)
c5aa993b
JM
11184 {
11185 case bp_none:
8a3fe4f8 11186 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 11187 bpt->number);
488919c4 11188 break;
c5aa993b 11189 case bp_breakpoint:
1042e4c0 11190 case bp_tracepoint:
7a697b8d 11191 case bp_fast_tracepoint:
0fb4aa4b 11192 case bp_static_tracepoint:
ce78b96d 11193 case bp_catchpoint:
c5aa993b
JM
11194 case bp_hardware_breakpoint:
11195 case bp_watchpoint:
11196 case bp_hardware_watchpoint:
11197 case bp_read_watchpoint:
11198 case bp_access_watchpoint:
11199 enable_breakpoint (bpt);
488919c4 11200 break;
c5aa993b 11201 default:
488919c4 11202 break;
c5aa993b 11203 }
0d381245
VP
11204 else if (strchr (args, '.'))
11205 {
11206 struct bp_location *loc = find_location_by_number (args);
11207 if (loc)
11208 loc->enabled = 1;
b60e7edf 11209 update_global_location_list (1);
0d381245 11210 }
c906108c 11211 else
95a42b64 11212 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
11213}
11214
11215static void
95a42b64 11216enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11217{
b5de0fa7 11218 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
11219}
11220
c906108c 11221static void
fba45db2 11222enable_once_command (char *args, int from_tty)
c906108c 11223{
95a42b64 11224 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
11225}
11226
11227static void
95a42b64 11228enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11229{
b5de0fa7 11230 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
11231}
11232
c906108c 11233static void
fba45db2 11234enable_delete_command (char *args, int from_tty)
c906108c 11235{
95a42b64 11236 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
11237}
11238\f
fa8d40ab
JJ
11239static void
11240set_breakpoint_cmd (char *args, int from_tty)
11241{
11242}
11243
11244static void
11245show_breakpoint_cmd (char *args, int from_tty)
11246{
11247}
11248
1f3b5d1b
PP
11249/* Invalidate last known value of any hardware watchpoint if
11250 the memory which that value represents has been written to by
11251 GDB itself. */
11252
11253static void
11254invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11255 const bfd_byte *data)
11256{
11257 struct breakpoint *bp;
11258
11259 ALL_BREAKPOINTS (bp)
11260 if (bp->enable_state == bp_enabled
11261 && bp->type == bp_hardware_watchpoint
11262 && bp->val_valid && bp->val)
11263 {
11264 struct bp_location *loc;
11265
11266 for (loc = bp->loc; loc != NULL; loc = loc->next)
11267 if (loc->loc_type == bp_loc_hardware_watchpoint
11268 && loc->address + loc->length > addr
11269 && addr + len > loc->address)
11270 {
11271 value_free (bp->val);
11272 bp->val = NULL;
11273 bp->val_valid = 0;
11274 }
11275 }
11276}
11277
c906108c
SS
11278/* Use default_breakpoint_'s, or nothing if they aren't valid. */
11279
11280struct symtabs_and_lines
fba45db2 11281decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
11282{
11283 struct symtabs_and_lines sals;
cc59ec59 11284
c906108c 11285 if (string == 0)
8a3fe4f8 11286 error (_("Empty line specification."));
c906108c
SS
11287 if (default_breakpoint_valid)
11288 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
11289 default_breakpoint_symtab,
11290 default_breakpoint_line,
7efd8fc2 11291 NULL, NULL);
c906108c
SS
11292 else
11293 sals = decode_line_1 (&string, funfirstline,
7efd8fc2 11294 (struct symtab *) NULL, 0, NULL, NULL);
c906108c 11295 if (*string)
8a3fe4f8 11296 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
11297 return sals;
11298}
8181d85f
DJ
11299
11300/* Create and insert a raw software breakpoint at PC. Return an
11301 identifier, which should be used to remove the breakpoint later.
11302 In general, places which call this should be using something on the
11303 breakpoint chain instead; this function should be eliminated
11304 someday. */
11305
11306void *
6c95b8df
PA
11307deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11308 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
11309{
11310 struct bp_target_info *bp_tgt;
11311
6c95b8df 11312 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 11313
6c95b8df 11314 bp_tgt->placed_address_space = aspace;
8181d85f 11315 bp_tgt->placed_address = pc;
6c95b8df 11316
a6d9a66e 11317 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
11318 {
11319 /* Could not insert the breakpoint. */
11320 xfree (bp_tgt);
11321 return NULL;
11322 }
11323
11324 return bp_tgt;
11325}
11326
4a64f543
MS
11327/* Remove a breakpoint BP inserted by
11328 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
11329
11330int
a6d9a66e 11331deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
11332{
11333 struct bp_target_info *bp_tgt = bp;
11334 int ret;
11335
a6d9a66e 11336 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
11337 xfree (bp_tgt);
11338
11339 return ret;
11340}
11341
4a64f543
MS
11342/* One (or perhaps two) breakpoints used for software single
11343 stepping. */
8181d85f
DJ
11344
11345static void *single_step_breakpoints[2];
a6d9a66e 11346static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
11347
11348/* Create and insert a breakpoint for software single step. */
11349
11350void
6c95b8df 11351insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
11352 struct address_space *aspace,
11353 CORE_ADDR next_pc)
8181d85f
DJ
11354{
11355 void **bpt_p;
11356
11357 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
11358 {
11359 bpt_p = &single_step_breakpoints[0];
11360 single_step_gdbarch[0] = gdbarch;
11361 }
8181d85f
DJ
11362 else
11363 {
11364 gdb_assert (single_step_breakpoints[1] == NULL);
11365 bpt_p = &single_step_breakpoints[1];
a6d9a66e 11366 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
11367 }
11368
4a64f543
MS
11369 /* NOTE drow/2006-04-11: A future improvement to this function would
11370 be to only create the breakpoints once, and actually put them on
11371 the breakpoint chain. That would let us use set_raw_breakpoint.
11372 We could adjust the addresses each time they were needed. Doing
11373 this requires corresponding changes elsewhere where single step
11374 breakpoints are handled, however. So, for now, we use this. */
8181d85f 11375
6c95b8df 11376 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 11377 if (*bpt_p == NULL)
5af949e3
UW
11378 error (_("Could not insert single-step breakpoint at %s"),
11379 paddress (gdbarch, next_pc));
8181d85f
DJ
11380}
11381
f02253f1
HZ
11382/* Check if the breakpoints used for software single stepping
11383 were inserted or not. */
11384
11385int
11386single_step_breakpoints_inserted (void)
11387{
11388 return (single_step_breakpoints[0] != NULL
11389 || single_step_breakpoints[1] != NULL);
11390}
11391
8181d85f
DJ
11392/* Remove and delete any breakpoints used for software single step. */
11393
11394void
11395remove_single_step_breakpoints (void)
11396{
11397 gdb_assert (single_step_breakpoints[0] != NULL);
11398
11399 /* See insert_single_step_breakpoint for more about this deprecated
11400 call. */
a6d9a66e
UW
11401 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11402 single_step_breakpoints[0]);
11403 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11404 single_step_breakpoints[0] = NULL;
11405
11406 if (single_step_breakpoints[1] != NULL)
11407 {
a6d9a66e
UW
11408 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11409 single_step_breakpoints[1]);
11410 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11411 single_step_breakpoints[1] = NULL;
11412 }
11413}
11414
d03285ec
UW
11415/* Delete software single step breakpoints without removing them from
11416 the inferior. This is intended to be used if the inferior's address
11417 space where they were inserted is already gone, e.g. after exit or
11418 exec. */
11419
11420void
11421cancel_single_step_breakpoints (void)
11422{
11423 int i;
11424
11425 for (i = 0; i < 2; i++)
11426 if (single_step_breakpoints[i])
11427 {
11428 xfree (single_step_breakpoints[i]);
11429 single_step_breakpoints[i] = NULL;
11430 single_step_gdbarch[i] = NULL;
11431 }
11432}
11433
11434/* Detach software single-step breakpoints from INFERIOR_PTID without
11435 removing them. */
11436
11437static void
11438detach_single_step_breakpoints (void)
11439{
11440 int i;
11441
11442 for (i = 0; i < 2; i++)
11443 if (single_step_breakpoints[i])
11444 target_remove_breakpoint (single_step_gdbarch[i],
11445 single_step_breakpoints[i]);
11446}
11447
4a64f543
MS
11448/* Check whether a software single-step breakpoint is inserted at
11449 PC. */
1aafd4da
UW
11450
11451static int
cc59ec59
MS
11452single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11453 CORE_ADDR pc)
1aafd4da
UW
11454{
11455 int i;
11456
11457 for (i = 0; i < 2; i++)
11458 {
11459 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11460 if (bp_tgt
11461 && breakpoint_address_match (bp_tgt->placed_address_space,
11462 bp_tgt->placed_address,
11463 aspace, pc))
1aafd4da
UW
11464 return 1;
11465 }
11466
11467 return 0;
11468}
11469
a96d9b2e
SDJ
11470/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11471 non-zero otherwise. */
11472static int
11473is_syscall_catchpoint_enabled (struct breakpoint *bp)
11474{
11475 if (syscall_catchpoint_p (bp)
11476 && bp->enable_state != bp_disabled
11477 && bp->enable_state != bp_call_disabled)
11478 return 1;
11479 else
11480 return 0;
11481}
11482
11483int
11484catch_syscall_enabled (void)
11485{
11486 struct inferior *inf = current_inferior ();
11487
11488 return inf->total_syscalls_count != 0;
11489}
11490
11491int
11492catching_syscall_number (int syscall_number)
11493{
11494 struct breakpoint *bp;
11495
11496 ALL_BREAKPOINTS (bp)
11497 if (is_syscall_catchpoint_enabled (bp))
11498 {
11499 if (bp->syscalls_to_be_caught)
11500 {
11501 int i, iter;
11502 for (i = 0;
11503 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11504 i++)
11505 if (syscall_number == iter)
11506 return 1;
11507 }
11508 else
11509 return 1;
11510 }
11511
11512 return 0;
11513}
11514
11515/* Complete syscall names. Used by "catch syscall". */
11516static char **
11517catch_syscall_completer (struct cmd_list_element *cmd,
11518 char *text, char *word)
11519{
11520 const char **list = get_syscall_names ();
c38eea1a
MS
11521 char **retlist
11522 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 11523
c38eea1a
MS
11524 xfree (list);
11525 return retlist;
a96d9b2e
SDJ
11526}
11527
1042e4c0
SS
11528/* Tracepoint-specific operations. */
11529
11530/* Set tracepoint count to NUM. */
11531static void
11532set_tracepoint_count (int num)
11533{
11534 tracepoint_count = num;
4fa62494 11535 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11536}
11537
11538void
11539trace_command (char *arg, int from_tty)
11540{
8cdf0e15
VP
11541 if (create_breakpoint (get_current_arch (),
11542 arg,
11543 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11544 0 /* tempflag */,
11545 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11546 0 /* Ignore count */,
11547 pending_break_support,
11548 NULL,
11549 from_tty,
84f4c1fe
PM
11550 1 /* enabled */,
11551 0 /* internal */))
fd9b8c24 11552 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11553}
11554
7a697b8d
SS
11555void
11556ftrace_command (char *arg, int from_tty)
11557{
8cdf0e15
VP
11558 if (create_breakpoint (get_current_arch (),
11559 arg,
11560 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11561 0 /* tempflag */,
11562 bp_fast_tracepoint /* type_wanted */,
11563 0 /* Ignore count */,
11564 pending_break_support,
11565 NULL,
11566 from_tty,
84f4c1fe
PM
11567 1 /* enabled */,
11568 0 /* internal */))
0fb4aa4b
PA
11569 set_tracepoint_count (breakpoint_count);
11570}
11571
11572/* strace command implementation. Creates a static tracepoint. */
11573
11574void
11575strace_command (char *arg, int from_tty)
11576{
11577 if (create_breakpoint (get_current_arch (),
11578 arg,
11579 NULL, 0, 1 /* parse arg */,
11580 0 /* tempflag */,
11581 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11582 0 /* Ignore count */,
11583 pending_break_support,
11584 NULL,
11585 from_tty,
84f4c1fe
PM
11586 1 /* enabled */,
11587 0 /* internal */))
fd9b8c24 11588 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11589}
11590
409873ef
SS
11591/* Set up a fake reader function that gets command lines from a linked
11592 list that was acquired during tracepoint uploading. */
11593
11594static struct uploaded_tp *this_utp;
3149d8c1 11595static int next_cmd;
409873ef
SS
11596
11597static char *
11598read_uploaded_action (void)
11599{
11600 char *rslt;
11601
3149d8c1 11602 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11603
3149d8c1 11604 next_cmd++;
409873ef
SS
11605
11606 return rslt;
11607}
11608
00bf0b85
SS
11609/* Given information about a tracepoint as recorded on a target (which
11610 can be either a live system or a trace file), attempt to create an
11611 equivalent GDB tracepoint. This is not a reliable process, since
11612 the target does not necessarily have all the information used when
11613 the tracepoint was originally defined. */
11614
11615struct breakpoint *
11616create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11617{
409873ef 11618 char *addr_str, small_buf[100];
d5551862 11619 struct breakpoint *tp;
fd9b8c24 11620
409873ef
SS
11621 if (utp->at_string)
11622 addr_str = utp->at_string;
11623 else
11624 {
11625 /* In the absence of a source location, fall back to raw
11626 address. Since there is no way to confirm that the address
11627 means the same thing as when the trace was started, warn the
11628 user. */
3e43a32a
MS
11629 warning (_("Uploaded tracepoint %d has no "
11630 "source location, using raw address"),
409873ef
SS
11631 utp->number);
11632 sprintf (small_buf, "*%s", hex_string (utp->addr));
11633 addr_str = small_buf;
11634 }
11635
11636 /* There's not much we can do with a sequence of bytecodes. */
11637 if (utp->cond && !utp->cond_string)
3e43a32a
MS
11638 warning (_("Uploaded tracepoint %d condition "
11639 "has no source form, ignoring it"),
409873ef 11640 utp->number);
d5551862 11641
8cdf0e15 11642 if (!create_breakpoint (get_current_arch (),
409873ef
SS
11643 addr_str,
11644 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 11645 0 /* tempflag */,
0fb4aa4b 11646 utp->type /* type_wanted */,
8cdf0e15
VP
11647 0 /* Ignore count */,
11648 pending_break_support,
11649 NULL,
11650 0 /* from_tty */,
84f4c1fe
PM
11651 utp->enabled /* enabled */,
11652 0 /* internal */))
fd9b8c24
PA
11653 return NULL;
11654
00bf0b85
SS
11655 set_tracepoint_count (breakpoint_count);
11656
409873ef 11657 /* Get the tracepoint we just created. */
fd9b8c24
PA
11658 tp = get_tracepoint (tracepoint_count);
11659 gdb_assert (tp != NULL);
d5551862 11660
00bf0b85
SS
11661 if (utp->pass > 0)
11662 {
409873ef 11663 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 11664
409873ef 11665 trace_pass_command (small_buf, 0);
00bf0b85
SS
11666 }
11667
409873ef
SS
11668 /* If we have uploaded versions of the original commands, set up a
11669 special-purpose "reader" function and call the usual command line
11670 reader, then pass the result to the breakpoint command-setting
11671 function. */
3149d8c1 11672 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 11673 {
409873ef 11674 struct command_line *cmd_list;
00bf0b85 11675
409873ef 11676 this_utp = utp;
3149d8c1 11677 next_cmd = 0;
d5551862 11678
409873ef
SS
11679 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
11680
11681 breakpoint_set_commands (tp, cmd_list);
00bf0b85 11682 }
3149d8c1
SS
11683 else if (!VEC_empty (char_ptr, utp->actions)
11684 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
11685 warning (_("Uploaded tracepoint %d actions "
11686 "have no source form, ignoring them"),
409873ef 11687 utp->number);
00bf0b85
SS
11688
11689 return tp;
11690 }
11691
1042e4c0
SS
11692/* Print information on tracepoint number TPNUM_EXP, or all if
11693 omitted. */
11694
11695static void
e5a67952 11696tracepoints_info (char *args, int from_tty)
1042e4c0 11697{
e5a67952 11698 int num_printed;
1042e4c0 11699
e5a67952 11700 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
11701
11702 if (num_printed == 0)
1042e4c0 11703 {
e5a67952 11704 if (args == NULL || *args == '\0')
d77f58be
SS
11705 ui_out_message (uiout, 0, "No tracepoints.\n");
11706 else
e5a67952 11707 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 11708 }
ad443146
SS
11709
11710 default_collect_info ();
1042e4c0
SS
11711}
11712
4a64f543 11713/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
11714 Not supported by all targets. */
11715static void
11716enable_trace_command (char *args, int from_tty)
11717{
11718 enable_command (args, from_tty);
11719}
11720
4a64f543 11721/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
11722 Not supported by all targets. */
11723static void
11724disable_trace_command (char *args, int from_tty)
11725{
11726 disable_command (args, from_tty);
11727}
11728
4a64f543 11729/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
11730static void
11731delete_trace_command (char *arg, int from_tty)
11732{
35df4500 11733 struct breakpoint *b, *b_tmp;
1042e4c0
SS
11734
11735 dont_repeat ();
11736
11737 if (arg == 0)
11738 {
11739 int breaks_to_delete = 0;
11740
11741 /* Delete all breakpoints if no argument.
11742 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
11743 have to be deleted with an explicit breakpoint number
11744 argument. */
1042e4c0
SS
11745 ALL_TRACEPOINTS (b)
11746 {
11747 if (b->number >= 0)
11748 {
11749 breaks_to_delete = 1;
11750 break;
11751 }
11752 }
11753
11754 /* Ask user only if there are some breakpoints to delete. */
11755 if (!from_tty
11756 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
11757 {
35df4500 11758 ALL_BREAKPOINTS_SAFE (b, b_tmp)
1042e4c0 11759 {
d77f58be 11760 if (is_tracepoint (b)
059fb39f 11761 && b->number >= 0)
1042e4c0
SS
11762 delete_breakpoint (b);
11763 }
11764 }
11765 }
11766 else
95a42b64 11767 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
11768}
11769
197f0a60
TT
11770/* Helper function for trace_pass_command. */
11771
11772static void
11773trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
11774{
11775 bp->pass_count = count;
11776 observer_notify_tracepoint_modified (bp->number);
11777 if (from_tty)
11778 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
11779 bp->number, count);
11780}
11781
1042e4c0
SS
11782/* Set passcount for tracepoint.
11783
11784 First command argument is passcount, second is tracepoint number.
11785 If tracepoint number omitted, apply to most recently defined.
11786 Also accepts special argument "all". */
11787
11788static void
11789trace_pass_command (char *args, int from_tty)
11790{
197f0a60 11791 struct breakpoint *t1;
1042e4c0 11792 unsigned int count;
1042e4c0
SS
11793
11794 if (args == 0 || *args == 0)
3e43a32a
MS
11795 error (_("passcount command requires an "
11796 "argument (count + optional TP num)"));
1042e4c0 11797
4a64f543 11798 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
11799
11800 while (*args && isspace ((int) *args))
11801 args++;
11802
11803 if (*args && strncasecmp (args, "all", 3) == 0)
11804 {
11805 args += 3; /* Skip special argument "all". */
1042e4c0
SS
11806 if (*args)
11807 error (_("Junk at end of arguments."));
1042e4c0 11808
197f0a60
TT
11809 ALL_TRACEPOINTS (t1)
11810 {
11811 trace_pass_set_count (t1, count, from_tty);
11812 }
11813 }
11814 else if (*args == '\0')
1042e4c0 11815 {
197f0a60 11816 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 11817 if (t1)
197f0a60
TT
11818 trace_pass_set_count (t1, count, from_tty);
11819 }
11820 else
11821 {
11822 struct get_number_or_range_state state;
11823
11824 init_number_or_range (&state, args);
11825 while (!state.finished)
1042e4c0 11826 {
197f0a60
TT
11827 t1 = get_tracepoint_by_number (&args, &state, 1);
11828 if (t1)
11829 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
11830 }
11831 }
1042e4c0
SS
11832}
11833
11834struct breakpoint *
11835get_tracepoint (int num)
11836{
11837 struct breakpoint *t;
11838
11839 ALL_TRACEPOINTS (t)
11840 if (t->number == num)
11841 return t;
11842
11843 return NULL;
11844}
11845
d5551862
SS
11846/* Find the tracepoint with the given target-side number (which may be
11847 different from the tracepoint number after disconnecting and
11848 reconnecting). */
11849
11850struct breakpoint *
11851get_tracepoint_by_number_on_target (int num)
11852{
11853 struct breakpoint *t;
11854
11855 ALL_TRACEPOINTS (t)
11856 if (t->number_on_target == num)
11857 return t;
11858
11859 return NULL;
11860}
11861
1042e4c0 11862/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
11863 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
11864 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
11865 recent tracepoint (tracepoint_count) is returned. */
11866struct breakpoint *
197f0a60
TT
11867get_tracepoint_by_number (char **arg,
11868 struct get_number_or_range_state *state,
11869 int optional_p)
1042e4c0
SS
11870{
11871 extern int tracepoint_count;
11872 struct breakpoint *t;
11873 int tpnum;
11874 char *instring = arg == NULL ? NULL : *arg;
11875
197f0a60
TT
11876 if (state)
11877 {
11878 gdb_assert (!state->finished);
11879 tpnum = get_number_or_range (state);
11880 }
11881 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
11882 {
11883 if (optional_p)
11884 tpnum = tracepoint_count;
11885 else
11886 error_no_arg (_("tracepoint number"));
11887 }
11888 else
197f0a60 11889 tpnum = get_number (arg);
1042e4c0
SS
11890
11891 if (tpnum <= 0)
11892 {
11893 if (instring && *instring)
11894 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
11895 instring);
11896 else
3e43a32a
MS
11897 printf_filtered (_("Tracepoint argument missing "
11898 "and no previous tracepoint\n"));
1042e4c0
SS
11899 return NULL;
11900 }
11901
11902 ALL_TRACEPOINTS (t)
11903 if (t->number == tpnum)
11904 {
11905 return t;
11906 }
11907
1042e4c0
SS
11908 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
11909 return NULL;
11910}
11911
6149aea9
PA
11912/* Save information on user settable breakpoints (watchpoints, etc) to
11913 a new script file named FILENAME. If FILTER is non-NULL, call it
11914 on each breakpoint and only include the ones for which it returns
11915 non-zero. */
11916
1042e4c0 11917static void
6149aea9
PA
11918save_breakpoints (char *filename, int from_tty,
11919 int (*filter) (const struct breakpoint *))
1042e4c0
SS
11920{
11921 struct breakpoint *tp;
6149aea9 11922 int any = 0;
a7bdde9e 11923 char *pathname;
1042e4c0 11924 struct cleanup *cleanup;
a7bdde9e 11925 struct ui_file *fp;
6149aea9 11926 int extra_trace_bits = 0;
1042e4c0 11927
6149aea9
PA
11928 if (filename == 0 || *filename == 0)
11929 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
11930
11931 /* See if we have anything to save. */
6149aea9 11932 ALL_BREAKPOINTS (tp)
1042e4c0 11933 {
6149aea9 11934 /* Skip internal and momentary breakpoints. */
09d682a4 11935 if (!user_breakpoint_p (tp))
6149aea9
PA
11936 continue;
11937
11938 /* If we have a filter, only save the breakpoints it accepts. */
11939 if (filter && !filter (tp))
11940 continue;
11941
11942 any = 1;
11943
11944 if (is_tracepoint (tp))
11945 {
11946 extra_trace_bits = 1;
11947
11948 /* We can stop searching. */
11949 break;
11950 }
1042e4c0 11951 }
6149aea9
PA
11952
11953 if (!any)
1042e4c0 11954 {
6149aea9 11955 warning (_("Nothing to save."));
1042e4c0
SS
11956 return;
11957 }
11958
6149aea9 11959 pathname = tilde_expand (filename);
1042e4c0 11960 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 11961 fp = gdb_fopen (pathname, "w");
059fb39f 11962 if (!fp)
6149aea9
PA
11963 error (_("Unable to open file '%s' for saving (%s)"),
11964 filename, safe_strerror (errno));
a7bdde9e 11965 make_cleanup_ui_file_delete (fp);
8bf6485c 11966
6149aea9
PA
11967 if (extra_trace_bits)
11968 save_trace_state_variables (fp);
8bf6485c 11969
6149aea9 11970 ALL_BREAKPOINTS (tp)
1042e4c0 11971 {
6149aea9 11972 /* Skip internal and momentary breakpoints. */
09d682a4 11973 if (!user_breakpoint_p (tp))
6149aea9 11974 continue;
8bf6485c 11975
6149aea9
PA
11976 /* If we have a filter, only save the breakpoints it accepts. */
11977 if (filter && !filter (tp))
11978 continue;
11979
11980 if (tp->ops != NULL)
11981 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
11982 else
11983 {
6149aea9
PA
11984 if (tp->type == bp_fast_tracepoint)
11985 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
11986 if (tp->type == bp_static_tracepoint)
11987 fprintf_unfiltered (fp, "strace");
6149aea9
PA
11988 else if (tp->type == bp_tracepoint)
11989 fprintf_unfiltered (fp, "trace");
11990 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11991 fprintf_unfiltered (fp, "tbreak");
11992 else if (tp->type == bp_breakpoint)
11993 fprintf_unfiltered (fp, "break");
11994 else if (tp->type == bp_hardware_breakpoint
11995 && tp->disposition == disp_del)
11996 fprintf_unfiltered (fp, "thbreak");
11997 else if (tp->type == bp_hardware_breakpoint)
11998 fprintf_unfiltered (fp, "hbreak");
11999 else if (tp->type == bp_watchpoint)
12000 fprintf_unfiltered (fp, "watch");
12001 else if (tp->type == bp_hardware_watchpoint)
12002 fprintf_unfiltered (fp, "watch");
12003 else if (tp->type == bp_read_watchpoint)
12004 fprintf_unfiltered (fp, "rwatch");
12005 else if (tp->type == bp_access_watchpoint)
12006 fprintf_unfiltered (fp, "awatch");
12007 else
12008 internal_error (__FILE__, __LINE__,
12009 _("unhandled breakpoint type %d"), (int) tp->type);
12010
12011 if (tp->exp_string)
12012 fprintf_unfiltered (fp, " %s", tp->exp_string);
12013 else if (tp->addr_string)
12014 fprintf_unfiltered (fp, " %s", tp->addr_string);
12015 else
12016 {
12017 char tmp[40];
12018
12019 sprintf_vma (tmp, tp->loc->address);
12020 fprintf_unfiltered (fp, " *0x%s", tmp);
12021 }
1042e4c0
SS
12022 }
12023
6149aea9
PA
12024 if (tp->thread != -1)
12025 fprintf_unfiltered (fp, " thread %d", tp->thread);
12026
12027 if (tp->task != 0)
12028 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12029
12030 fprintf_unfiltered (fp, "\n");
12031
6149aea9
PA
12032 /* Note, we can't rely on tp->number for anything, as we can't
12033 assume the recreated breakpoint numbers will match. Use $bpnum
12034 instead. */
12035
12036 if (tp->cond_string)
12037 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12038
12039 if (tp->ignore_count)
12040 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12041
1042e4c0 12042 if (tp->pass_count)
a7bdde9e 12043 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12044
a7bdde9e 12045 if (tp->commands)
1042e4c0 12046 {
a7bdde9e
VP
12047 volatile struct gdb_exception ex;
12048
6149aea9 12049 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12050
12051 ui_out_redirect (uiout, fp);
14dba4b4 12052 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12053 {
9add0f1b 12054 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12055 }
12056 ui_out_redirect (uiout, NULL);
1042e4c0 12057
a7bdde9e
VP
12058 if (ex.reason < 0)
12059 throw_exception (ex);
1042e4c0 12060
a7bdde9e 12061 fprintf_unfiltered (fp, " end\n");
1042e4c0 12062 }
6149aea9
PA
12063
12064 if (tp->enable_state == bp_disabled)
12065 fprintf_unfiltered (fp, "disable\n");
12066
12067 /* If this is a multi-location breakpoint, check if the locations
12068 should be individually disabled. Watchpoint locations are
12069 special, and not user visible. */
12070 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12071 {
12072 struct bp_location *loc;
12073 int n = 1;
12074
12075 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12076 if (!loc->enabled)
12077 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12078 }
1042e4c0 12079 }
8bf6485c 12080
6149aea9 12081 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12082 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12083
1042e4c0
SS
12084 do_cleanups (cleanup);
12085 if (from_tty)
6149aea9
PA
12086 printf_filtered (_("Saved to file '%s'.\n"), filename);
12087}
12088
12089/* The `save breakpoints' command. */
12090
12091static void
12092save_breakpoints_command (char *args, int from_tty)
12093{
12094 save_breakpoints (args, from_tty, NULL);
12095}
12096
12097/* The `save tracepoints' command. */
12098
12099static void
12100save_tracepoints_command (char *args, int from_tty)
12101{
12102 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12103}
12104
12105/* Create a vector of all tracepoints. */
12106
12107VEC(breakpoint_p) *
eeae04df 12108all_tracepoints (void)
1042e4c0
SS
12109{
12110 VEC(breakpoint_p) *tp_vec = 0;
12111 struct breakpoint *tp;
12112
12113 ALL_TRACEPOINTS (tp)
12114 {
12115 VEC_safe_push (breakpoint_p, tp_vec, tp);
12116 }
12117
12118 return tp_vec;
12119}
12120
c906108c 12121\f
4a64f543
MS
12122/* This help string is used for the break, hbreak, tbreak and thbreak
12123 commands. It is defined as a macro to prevent duplication.
12124 COMMAND should be a string constant containing the name of the
12125 command. */
31e2b00f
AS
12126#define BREAK_ARGS_HELP(command) \
12127command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12128LOCATION may be a line number, function name, or \"*\" and an address.\n\
12129If a line number is specified, break at start of code for that line.\n\
12130If a function is specified, break at start of code for that function.\n\
12131If an address is specified, break at that exact address.\n\
dc10affe
PA
12132With no LOCATION, uses current execution address of the selected\n\
12133stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12134\n\
12135THREADNUM is the number from \"info threads\".\n\
12136CONDITION is a boolean expression.\n\
12137\n\
d41c0fc8
PA
12138Multiple breakpoints at one place are permitted, and useful if their\n\
12139conditions are different.\n\
31e2b00f
AS
12140\n\
12141Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12142
44feb3ce
TT
12143/* List of subcommands for "catch". */
12144static struct cmd_list_element *catch_cmdlist;
12145
12146/* List of subcommands for "tcatch". */
12147static struct cmd_list_element *tcatch_cmdlist;
12148
12149/* Like add_cmd, but add the command to both the "catch" and "tcatch"
12150 lists, and pass some additional user data to the command function. */
12151static void
12152add_catch_command (char *name, char *docstring,
12153 void (*sfunc) (char *args, int from_tty,
12154 struct cmd_list_element *command),
a96d9b2e
SDJ
12155 char **(*completer) (struct cmd_list_element *cmd,
12156 char *text, char *word),
44feb3ce
TT
12157 void *user_data_catch,
12158 void *user_data_tcatch)
12159{
12160 struct cmd_list_element *command;
12161
12162 command = add_cmd (name, class_breakpoint, NULL, docstring,
12163 &catch_cmdlist);
12164 set_cmd_sfunc (command, sfunc);
12165 set_cmd_context (command, user_data_catch);
a96d9b2e 12166 set_cmd_completer (command, completer);
44feb3ce
TT
12167
12168 command = add_cmd (name, class_breakpoint, NULL, docstring,
12169 &tcatch_cmdlist);
12170 set_cmd_sfunc (command, sfunc);
12171 set_cmd_context (command, user_data_tcatch);
a96d9b2e 12172 set_cmd_completer (command, completer);
44feb3ce
TT
12173}
12174
6c95b8df 12175static void
a79b8f6e 12176clear_syscall_counts (struct inferior *inf)
6c95b8df 12177{
6c95b8df
PA
12178 inf->total_syscalls_count = 0;
12179 inf->any_syscall_count = 0;
12180 VEC_free (int, inf->syscalls_counts);
12181}
12182
6149aea9
PA
12183static void
12184save_command (char *arg, int from_tty)
12185{
3e43a32a
MS
12186 printf_unfiltered (_("\"save\" must be followed by "
12187 "the name of a save subcommand.\n"));
6149aea9
PA
12188 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12189}
12190
84f4c1fe
PM
12191struct breakpoint *
12192iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12193 void *data)
12194{
35df4500 12195 struct breakpoint *b, *b_tmp;
84f4c1fe 12196
35df4500 12197 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12198 {
12199 if ((*callback) (b, data))
12200 return b;
12201 }
12202
12203 return NULL;
12204}
12205
c906108c 12206void
fba45db2 12207_initialize_breakpoint (void)
c906108c
SS
12208{
12209 struct cmd_list_element *c;
12210
84acb35a 12211 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 12212 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 12213 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 12214
17450429
PP
12215 breakpoint_objfile_key = register_objfile_data ();
12216
c906108c
SS
12217 breakpoint_chain = 0;
12218 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12219 before a breakpoint is set. */
12220 breakpoint_count = 0;
12221
1042e4c0
SS
12222 tracepoint_count = 0;
12223
1bedd215
AC
12224 add_com ("ignore", class_breakpoint, ignore_command, _("\
12225Set ignore-count of breakpoint number N to COUNT.\n\
12226Usage is `ignore N COUNT'."));
c906108c 12227 if (xdb_commands)
c5aa993b 12228 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 12229
1bedd215
AC
12230 add_com ("commands", class_breakpoint, commands_command, _("\
12231Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
12232Give breakpoint number as argument after \"commands\".\n\
12233With no argument, the targeted breakpoint is the last one set.\n\
12234The commands themselves follow starting on the next line.\n\
12235Type a line containing \"end\" to indicate the end of them.\n\
12236Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 12237then no output is printed when it is hit, except what the commands print."));
c906108c 12238
1bedd215
AC
12239 add_com ("condition", class_breakpoint, condition_command, _("\
12240Specify breakpoint number N to break only if COND is true.\n\
c906108c 12241Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 12242expression to be evaluated whenever breakpoint N is reached."));
c906108c 12243
1bedd215 12244 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 12245Set a temporary breakpoint.\n\
c906108c
SS
12246Like \"break\" except the breakpoint is only temporary,\n\
12247so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
12248by using \"enable delete\" on the breakpoint number.\n\
12249\n"
12250BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 12251 set_cmd_completer (c, location_completer);
c94fdfd0 12252
1bedd215 12253 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 12254Set a hardware assisted breakpoint.\n\
c906108c 12255Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
12256some target hardware may not have this support.\n\
12257\n"
12258BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 12259 set_cmd_completer (c, location_completer);
c906108c 12260
1bedd215 12261 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 12262Set a temporary hardware assisted breakpoint.\n\
c906108c 12263Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
12264so it will be deleted when hit.\n\
12265\n"
12266BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 12267 set_cmd_completer (c, location_completer);
c906108c 12268
1bedd215
AC
12269 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12270Enable some breakpoints.\n\
c906108c
SS
12271Give breakpoint numbers (separated by spaces) as arguments.\n\
12272With no subcommand, breakpoints are enabled until you command otherwise.\n\
12273This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12274With a subcommand you can enable temporarily."),
c906108c
SS
12275 &enablelist, "enable ", 1, &cmdlist);
12276 if (xdb_commands)
1bedd215
AC
12277 add_com ("ab", class_breakpoint, enable_command, _("\
12278Enable some breakpoints.\n\
c906108c
SS
12279Give breakpoint numbers (separated by spaces) as arguments.\n\
12280With no subcommand, breakpoints are enabled until you command otherwise.\n\
12281This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12282With a subcommand you can enable temporarily."));
c906108c
SS
12283
12284 add_com_alias ("en", "enable", class_breakpoint, 1);
12285
84951ab5 12286 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 12287Enable some breakpoints.\n\
c906108c
SS
12288Give breakpoint numbers (separated by spaces) as arguments.\n\
12289This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12290May be abbreviated to simply \"enable\".\n"),
c5aa993b 12291 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 12292
1a966eab
AC
12293 add_cmd ("once", no_class, enable_once_command, _("\
12294Enable breakpoints for one hit. Give breakpoint numbers.\n\
12295If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12296 &enablebreaklist);
12297
1a966eab
AC
12298 add_cmd ("delete", no_class, enable_delete_command, _("\
12299Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12300If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12301 &enablebreaklist);
12302
1a966eab
AC
12303 add_cmd ("delete", no_class, enable_delete_command, _("\
12304Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12305If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12306 &enablelist);
12307
1a966eab
AC
12308 add_cmd ("once", no_class, enable_once_command, _("\
12309Enable breakpoints for one hit. Give breakpoint numbers.\n\
12310If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12311 &enablelist);
12312
1bedd215
AC
12313 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12314Disable some breakpoints.\n\
c906108c
SS
12315Arguments are breakpoint numbers with spaces in between.\n\
12316To disable all breakpoints, give no argument.\n\
1bedd215 12317A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
12318 &disablelist, "disable ", 1, &cmdlist);
12319 add_com_alias ("dis", "disable", class_breakpoint, 1);
12320 add_com_alias ("disa", "disable", class_breakpoint, 1);
12321 if (xdb_commands)
1bedd215
AC
12322 add_com ("sb", class_breakpoint, disable_command, _("\
12323Disable some breakpoints.\n\
c906108c
SS
12324Arguments are breakpoint numbers with spaces in between.\n\
12325To disable all breakpoints, give no argument.\n\
1bedd215 12326A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 12327
1a966eab
AC
12328 add_cmd ("breakpoints", class_alias, disable_command, _("\
12329Disable some breakpoints.\n\
c906108c
SS
12330Arguments are breakpoint numbers with spaces in between.\n\
12331To disable all breakpoints, give no argument.\n\
12332A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 12333This command may be abbreviated \"disable\"."),
c906108c
SS
12334 &disablelist);
12335
1bedd215
AC
12336 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12337Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12338Arguments are breakpoint numbers with spaces in between.\n\
12339To delete all breakpoints, give no argument.\n\
12340\n\
12341Also a prefix command for deletion of other GDB objects.\n\
1bedd215 12342The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
12343 &deletelist, "delete ", 1, &cmdlist);
12344 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 12345 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 12346 if (xdb_commands)
1bedd215
AC
12347 add_com ("db", class_breakpoint, delete_command, _("\
12348Delete some breakpoints.\n\
c906108c 12349Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 12350To delete all breakpoints, give no argument.\n"));
c906108c 12351
1a966eab
AC
12352 add_cmd ("breakpoints", class_alias, delete_command, _("\
12353Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12354Arguments are breakpoint numbers with spaces in between.\n\
12355To delete all breakpoints, give no argument.\n\
1a966eab 12356This command may be abbreviated \"delete\"."),
c906108c
SS
12357 &deletelist);
12358
1bedd215
AC
12359 add_com ("clear", class_breakpoint, clear_command, _("\
12360Clear breakpoint at specified line or function.\n\
c906108c
SS
12361Argument may be line number, function name, or \"*\" and an address.\n\
12362If line number is specified, all breakpoints in that line are cleared.\n\
12363If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
12364If an address is specified, breakpoints at that address are cleared.\n\
12365\n\
12366With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
12367is executing in.\n\
12368\n\
1bedd215 12369See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 12370 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 12371
1bedd215 12372 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
12373Set breakpoint at specified line or function.\n"
12374BREAK_ARGS_HELP ("break")));
5ba2abeb 12375 set_cmd_completer (c, location_completer);
c94fdfd0 12376
c906108c
SS
12377 add_com_alias ("b", "break", class_run, 1);
12378 add_com_alias ("br", "break", class_run, 1);
12379 add_com_alias ("bre", "break", class_run, 1);
12380 add_com_alias ("brea", "break", class_run, 1);
12381
7681d515
PM
12382 if (xdb_commands)
12383 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
12384
12385 if (dbx_commands)
12386 {
1bedd215
AC
12387 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12388Break in function/address or break at a line in the current file."),
c5aa993b
JM
12389 &stoplist, "stop ", 1, &cmdlist);
12390 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 12391 _("Break in function or address."), &stoplist);
c5aa993b 12392 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 12393 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
12394 add_com ("status", class_info, breakpoints_info, _("\
12395Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12396The \"Type\" column indicates one of:\n\
12397\tbreakpoint - normal breakpoint\n\
12398\twatchpoint - watchpoint\n\
12399The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12400the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12401breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12402address and file/line number respectively.\n\
12403\n\
12404Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12405are set to the address of the last breakpoint listed unless the command\n\
12406is prefixed with \"server \".\n\n\
c906108c 12407Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12408breakpoint set."));
c906108c
SS
12409 }
12410
1bedd215 12411 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 12412Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
12413The \"Type\" column indicates one of:\n\
12414\tbreakpoint - normal breakpoint\n\
12415\twatchpoint - watchpoint\n\
12416The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12417the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12418breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12419address and file/line number respectively.\n\
12420\n\
12421Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12422are set to the address of the last breakpoint listed unless the command\n\
12423is prefixed with \"server \".\n\n\
c906108c 12424Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12425breakpoint set."));
c906108c 12426
6b04bdb7
MS
12427 add_info_alias ("b", "breakpoints", 1);
12428
c906108c 12429 if (xdb_commands)
1bedd215
AC
12430 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12431Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12432The \"Type\" column indicates one of:\n\
12433\tbreakpoint - normal breakpoint\n\
12434\twatchpoint - watchpoint\n\
12435The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12436the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12437breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12438address and file/line number respectively.\n\
12439\n\
12440Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12441are set to the address of the last breakpoint listed unless the command\n\
12442is prefixed with \"server \".\n\n\
c906108c 12443Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12444breakpoint set."));
c906108c 12445
1a966eab
AC
12446 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12447Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12448The \"Type\" column indicates one of:\n\
12449\tbreakpoint - normal breakpoint\n\
12450\twatchpoint - watchpoint\n\
12451\tlongjmp - internal breakpoint used to step through longjmp()\n\
12452\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12453\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12454\tfinish - internal breakpoint used by the \"finish\" command\n\
12455The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12456the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12457breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12458address and file/line number respectively.\n\
12459\n\
12460Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12461are set to the address of the last breakpoint listed unless the command\n\
12462is prefixed with \"server \".\n\n\
c906108c 12463Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12464breakpoint set."),
c906108c
SS
12465 &maintenanceinfolist);
12466
44feb3ce
TT
12467 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12468Set catchpoints to catch events."),
12469 &catch_cmdlist, "catch ",
12470 0/*allow-unknown*/, &cmdlist);
12471
12472 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12473Set temporary catchpoints to catch events."),
12474 &tcatch_cmdlist, "tcatch ",
12475 0/*allow-unknown*/, &cmdlist);
12476
12477 /* Add catch and tcatch sub-commands. */
12478 add_catch_command ("catch", _("\
12479Catch an exception, when caught.\n\
12480With an argument, catch only exceptions with the given name."),
12481 catch_catch_command,
a96d9b2e 12482 NULL,
44feb3ce
TT
12483 CATCH_PERMANENT,
12484 CATCH_TEMPORARY);
12485 add_catch_command ("throw", _("\
12486Catch an exception, when thrown.\n\
12487With an argument, catch only exceptions with the given name."),
12488 catch_throw_command,
a96d9b2e 12489 NULL,
44feb3ce
TT
12490 CATCH_PERMANENT,
12491 CATCH_TEMPORARY);
12492 add_catch_command ("fork", _("Catch calls to fork."),
12493 catch_fork_command_1,
a96d9b2e 12494 NULL,
44feb3ce
TT
12495 (void *) (uintptr_t) catch_fork_permanent,
12496 (void *) (uintptr_t) catch_fork_temporary);
12497 add_catch_command ("vfork", _("Catch calls to vfork."),
12498 catch_fork_command_1,
a96d9b2e 12499 NULL,
44feb3ce
TT
12500 (void *) (uintptr_t) catch_vfork_permanent,
12501 (void *) (uintptr_t) catch_vfork_temporary);
12502 add_catch_command ("exec", _("Catch calls to exec."),
12503 catch_exec_command_1,
a96d9b2e
SDJ
12504 NULL,
12505 CATCH_PERMANENT,
12506 CATCH_TEMPORARY);
12507 add_catch_command ("syscall", _("\
12508Catch system calls by their names and/or numbers.\n\
12509Arguments say which system calls to catch. If no arguments\n\
12510are given, every system call will be caught.\n\
12511Arguments, if given, should be one or more system call names\n\
12512(if your system supports that), or system call numbers."),
12513 catch_syscall_command_1,
12514 catch_syscall_completer,
44feb3ce
TT
12515 CATCH_PERMANENT,
12516 CATCH_TEMPORARY);
44feb3ce
TT
12517 add_catch_command ("exception", _("\
12518Catch Ada exceptions, when raised.\n\
12519With an argument, catch only exceptions with the given name."),
12520 catch_ada_exception_command,
a96d9b2e 12521 NULL,
44feb3ce
TT
12522 CATCH_PERMANENT,
12523 CATCH_TEMPORARY);
12524 add_catch_command ("assert", _("\
12525Catch failed Ada assertions, when raised.\n\
12526With an argument, catch only exceptions with the given name."),
12527 catch_assert_command,
a96d9b2e 12528 NULL,
44feb3ce
TT
12529 CATCH_PERMANENT,
12530 CATCH_TEMPORARY);
c5aa993b 12531
1bedd215
AC
12532 c = add_com ("watch", class_breakpoint, watch_command, _("\
12533Set a watchpoint for an expression.\n\
06a64a0b 12534Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12535A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12536an expression changes.\n\
12537If -l or -location is given, this evaluates EXPRESSION and watches\n\
12538the memory to which it refers."));
65d12d83 12539 set_cmd_completer (c, expression_completer);
c906108c 12540
1bedd215
AC
12541 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12542Set a read watchpoint for an expression.\n\
06a64a0b 12543Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12544A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12545an expression is read.\n\
12546If -l or -location is given, this evaluates EXPRESSION and watches\n\
12547the memory to which it refers."));
65d12d83 12548 set_cmd_completer (c, expression_completer);
c906108c 12549
1bedd215
AC
12550 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12551Set a watchpoint for an expression.\n\
06a64a0b 12552Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12553A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12554an expression is either read or written.\n\
12555If -l or -location is given, this evaluates EXPRESSION and watches\n\
12556the memory to which it refers."));
65d12d83 12557 set_cmd_completer (c, expression_completer);
c906108c 12558
d77f58be 12559 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 12560Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 12561
920d2a44
AC
12562 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12563 respond to changes - contrary to the description. */
85c07804
AC
12564 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12565 &can_use_hw_watchpoints, _("\
12566Set debugger's willingness to use watchpoint hardware."), _("\
12567Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12568If zero, gdb will not use hardware for new watchpoints, even if\n\
12569such is available. (However, any hardware watchpoints that were\n\
12570created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12571hardware.)"),
12572 NULL,
920d2a44 12573 show_can_use_hw_watchpoints,
85c07804 12574 &setlist, &showlist);
c906108c
SS
12575
12576 can_use_hw_watchpoints = 1;
fa8d40ab 12577
1042e4c0
SS
12578 /* Tracepoint manipulation commands. */
12579
12580 c = add_com ("trace", class_breakpoint, trace_command, _("\
12581Set a tracepoint at specified line or function.\n\
12582\n"
12583BREAK_ARGS_HELP ("trace") "\n\
12584Do \"help tracepoints\" for info on other tracepoint commands."));
12585 set_cmd_completer (c, location_completer);
12586
12587 add_com_alias ("tp", "trace", class_alias, 0);
12588 add_com_alias ("tr", "trace", class_alias, 1);
12589 add_com_alias ("tra", "trace", class_alias, 1);
12590 add_com_alias ("trac", "trace", class_alias, 1);
12591
7a697b8d
SS
12592 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12593Set a fast tracepoint at specified line or function.\n\
12594\n"
12595BREAK_ARGS_HELP ("ftrace") "\n\
12596Do \"help tracepoints\" for info on other tracepoint commands."));
12597 set_cmd_completer (c, location_completer);
12598
0fb4aa4b
PA
12599 c = add_com ("strace", class_breakpoint, strace_command, _("\
12600Set a static tracepoint at specified line, function or marker.\n\
12601\n\
12602strace [LOCATION] [if CONDITION]\n\
12603LOCATION may be a line number, function name, \"*\" and an address,\n\
12604or -m MARKER_ID.\n\
12605If a line number is specified, probe the marker at start of code\n\
12606for that line. If a function is specified, probe the marker at start\n\
12607of code for that function. If an address is specified, probe the marker\n\
12608at that exact address. If a marker id is specified, probe the marker\n\
12609with that name. With no LOCATION, uses current execution address of\n\
12610the selected stack frame.\n\
12611Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12612This collects arbitrary user data passed in the probe point call to the\n\
12613tracing library. You can inspect it when analyzing the trace buffer,\n\
12614by printing the $_sdata variable like any other convenience variable.\n\
12615\n\
12616CONDITION is a boolean expression.\n\
12617\n\
d41c0fc8
PA
12618Multiple tracepoints at one place are permitted, and useful if their\n\
12619conditions are different.\n\
0fb4aa4b
PA
12620\n\
12621Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12622Do \"help tracepoints\" for info on other tracepoint commands."));
12623 set_cmd_completer (c, location_completer);
12624
1042e4c0 12625 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 12626Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
12627Convenience variable \"$tpnum\" contains the number of the\n\
12628last tracepoint set."));
12629
12630 add_info_alias ("tp", "tracepoints", 1);
12631
12632 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12633Delete specified tracepoints.\n\
12634Arguments are tracepoint numbers, separated by spaces.\n\
12635No argument means delete all tracepoints."),
12636 &deletelist);
12637
12638 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12639Disable specified tracepoints.\n\
12640Arguments are tracepoint numbers, separated by spaces.\n\
12641No argument means disable all tracepoints."),
12642 &disablelist);
12643 deprecate_cmd (c, "disable");
12644
12645 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12646Enable specified tracepoints.\n\
12647Arguments are tracepoint numbers, separated by spaces.\n\
12648No argument means enable all tracepoints."),
12649 &enablelist);
12650 deprecate_cmd (c, "enable");
12651
12652 add_com ("passcount", class_trace, trace_pass_command, _("\
12653Set the passcount for a tracepoint.\n\
12654The trace will end when the tracepoint has been passed 'count' times.\n\
12655Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12656if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12657
6149aea9
PA
12658 add_prefix_cmd ("save", class_breakpoint, save_command,
12659 _("Save breakpoint definitions as a script."),
12660 &save_cmdlist, "save ",
12661 0/*allow-unknown*/, &cmdlist);
12662
12663 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12664Save current breakpoint definitions as a script.\n\
cce7e648 12665This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
12666catchpoints, tracepoints). Use the 'source' command in another debug\n\
12667session to restore them."),
12668 &save_cmdlist);
12669 set_cmd_completer (c, filename_completer);
12670
12671 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 12672Save current tracepoint definitions as a script.\n\
6149aea9
PA
12673Use the 'source' command in another debug session to restore them."),
12674 &save_cmdlist);
1042e4c0
SS
12675 set_cmd_completer (c, filename_completer);
12676
6149aea9
PA
12677 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12678 deprecate_cmd (c, "save tracepoints");
12679
1bedd215 12680 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
12681Breakpoint specific settings\n\
12682Configure various breakpoint-specific variables such as\n\
1bedd215 12683pending breakpoint behavior"),
fa8d40ab
JJ
12684 &breakpoint_set_cmdlist, "set breakpoint ",
12685 0/*allow-unknown*/, &setlist);
1bedd215 12686 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
12687Breakpoint specific settings\n\
12688Configure various breakpoint-specific variables such as\n\
1bedd215 12689pending breakpoint behavior"),
fa8d40ab
JJ
12690 &breakpoint_show_cmdlist, "show breakpoint ",
12691 0/*allow-unknown*/, &showlist);
12692
7915a72c
AC
12693 add_setshow_auto_boolean_cmd ("pending", no_class,
12694 &pending_break_support, _("\
12695Set debugger's behavior regarding pending breakpoints."), _("\
12696Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
12697If on, an unrecognized breakpoint location will cause gdb to create a\n\
12698pending breakpoint. If off, an unrecognized breakpoint location results in\n\
12699an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 12700user-query to see if a pending breakpoint should be created."),
2c5b56ce 12701 NULL,
920d2a44 12702 show_pending_break_support,
6e1d7d6c
AC
12703 &breakpoint_set_cmdlist,
12704 &breakpoint_show_cmdlist);
fa8d40ab
JJ
12705
12706 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
12707
12708 add_setshow_boolean_cmd ("auto-hw", no_class,
12709 &automatic_hardware_breakpoints, _("\
12710Set automatic usage of hardware breakpoints."), _("\
12711Show automatic usage of hardware breakpoints."), _("\
12712If set, the debugger will automatically use hardware breakpoints for\n\
12713breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
12714a warning will be emitted for such breakpoints."),
12715 NULL,
12716 show_automatic_hardware_breakpoints,
12717 &breakpoint_set_cmdlist,
12718 &breakpoint_show_cmdlist);
74960c60 12719
33e5cbd6
PA
12720 add_setshow_enum_cmd ("always-inserted", class_support,
12721 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
12722Set mode for inserting breakpoints."), _("\
12723Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
12724When this mode is off, breakpoints are inserted in inferior when it is\n\
12725resumed, and removed when execution stops. When this mode is on,\n\
12726breakpoints are inserted immediately and removed only when the user\n\
12727deletes the breakpoint. When this mode is auto (which is the default),\n\
12728the behaviour depends on the non-stop setting (see help set non-stop).\n\
12729In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
12730behaves as if always-inserted mode is on; if gdb is controlling the\n\
12731inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
12732 NULL,
12733 &show_always_inserted_mode,
12734 &breakpoint_set_cmdlist,
12735 &breakpoint_show_cmdlist);
765dc015
VP
12736
12737 automatic_hardware_breakpoints = 1;
f3b1572e
PA
12738
12739 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 12740}
This page took 1.8335 seconds and 4 git commands to generate.