* breakpoint.c (update_watchpoint): Move code to change
[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
f1310107
TJB
134static int breakpoint_location_address_match (struct bp_location *bl,
135 struct address_space *aspace,
136 CORE_ADDR addr);
137
a14ed312 138static void breakpoints_info (char *, int);
c906108c 139
d77f58be
SS
140static void watchpoints_info (char *, int);
141
e5a67952
MS
142static int breakpoint_1 (char *, int,
143 int (*) (const struct breakpoint *));
c906108c 144
4efb68b1 145static int breakpoint_cond_eval (void *);
c906108c 146
4efb68b1 147static void cleanup_executing_breakpoints (void *);
c906108c 148
a14ed312 149static void commands_command (char *, int);
c906108c 150
a14ed312 151static void condition_command (char *, int);
c906108c 152
c5aa993b
JM
153typedef enum
154 {
155 mark_inserted,
156 mark_uninserted
157 }
158insertion_state_t;
c906108c 159
0bde7532 160static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 161static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 162
a14ed312 163static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
164
165static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 166
4efb68b1 167static int watchpoint_check (void *);
c906108c 168
a14ed312 169static void maintenance_info_breakpoints (char *, int);
c906108c 170
a14ed312 171static int hw_breakpoint_used_count (void);
c906108c 172
a14ed312 173static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 174
a14ed312 175static void hbreak_command (char *, int);
c906108c 176
a14ed312 177static void thbreak_command (char *, int);
c906108c 178
a14ed312 179static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 180
a14ed312 181static void stop_command (char *arg, int from_tty);
7a292a7a 182
a14ed312 183static void stopin_command (char *arg, int from_tty);
7a292a7a 184
a14ed312 185static void stopat_command (char *arg, int from_tty);
7a292a7a 186
a14ed312 187static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 188
d85310f7
MS
189static void catch_exception_command_1 (enum exception_event_kind ex_event,
190 char *arg, int tempflag, int from_tty);
7a292a7a 191
a14ed312 192static void tcatch_command (char *arg, int from_tty);
7a292a7a 193
d03285ec
UW
194static void detach_single_step_breakpoints (void);
195
6c95b8df
PA
196static int single_step_breakpoint_inserted_here_p (struct address_space *,
197 CORE_ADDR pc);
1aafd4da 198
fe3f5fa8 199static void free_bp_location (struct bp_location *loc);
f431efe5
PA
200static void incref_bp_location (struct bp_location *loc);
201static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 202
39d61571 203static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 204
b60e7edf 205static void update_global_location_list (int);
a5606eee 206
b60e7edf 207static void update_global_location_list_nothrow (int);
74960c60 208
d77f58be 209static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 210
d77f58be 211static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 212
74960c60 213static void insert_breakpoint_locations (void);
a5606eee 214
a96d9b2e
SDJ
215static int syscall_catchpoint_p (struct breakpoint *b);
216
1042e4c0
SS
217static void tracepoints_info (char *, int);
218
219static void delete_trace_command (char *, int);
220
221static void enable_trace_command (char *, int);
222
223static void disable_trace_command (char *, int);
224
225static void trace_pass_command (char *, int);
226
0fb4aa4b
PA
227/* Assuming we're creating a static tracepoint, does S look like a
228 static tracepoint marker spec ("-m MARKER_ID")? */
229#define is_marker_spec(s) \
f5a8e22b 230 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 231
5cea2a26
PA
232/* A reference-counted struct command_line. This lets multiple
233 breakpoints share a single command list. */
234struct counted_command_line
235{
236 /* The reference count. */
237 int refc;
238
239 /* The command list. */
240 struct command_line *commands;
241};
242
243struct command_line *
244breakpoint_commands (struct breakpoint *b)
245{
246 return b->commands ? b->commands->commands : NULL;
247}
3daf8fe5 248
f3b1572e
PA
249/* Flag indicating that a command has proceeded the inferior past the
250 current breakpoint. */
251
252static int breakpoint_proceeded;
253
956a9fb9 254const char *
2cec12e5
AR
255bpdisp_text (enum bpdisp disp)
256{
4a64f543
MS
257 /* NOTE: the following values are a part of MI protocol and
258 represent values of 'disp' field returned when inferior stops at
259 a breakpoint. */
bc043ef3 260 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 261
2cec12e5
AR
262 return bpdisps[(int) disp];
263}
c906108c 264
4a64f543 265/* Prototypes for exported functions. */
c906108c 266/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 267 if such is available. */
c906108c
SS
268static int can_use_hw_watchpoints;
269
920d2a44
AC
270static void
271show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
272 struct cmd_list_element *c,
273 const char *value)
274{
3e43a32a
MS
275 fprintf_filtered (file,
276 _("Debugger's willingness to use "
277 "watchpoint hardware is %s.\n"),
920d2a44
AC
278 value);
279}
280
fa8d40ab
JJ
281/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
282 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 283 for unrecognized breakpoint locations.
fa8d40ab
JJ
284 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
285static enum auto_boolean pending_break_support;
920d2a44
AC
286static void
287show_pending_break_support (struct ui_file *file, int from_tty,
288 struct cmd_list_element *c,
289 const char *value)
290{
3e43a32a
MS
291 fprintf_filtered (file,
292 _("Debugger's behavior regarding "
293 "pending breakpoints is %s.\n"),
920d2a44
AC
294 value);
295}
fa8d40ab 296
765dc015 297/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 298 set with "break" but falling in read-only memory.
765dc015
VP
299 If 0, gdb will warn about such breakpoints, but won't automatically
300 use hardware breakpoints. */
301static int automatic_hardware_breakpoints;
302static void
303show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
304 struct cmd_list_element *c,
305 const char *value)
306{
3e43a32a
MS
307 fprintf_filtered (file,
308 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
309 value);
310}
311
33e5cbd6
PA
312/* If on, gdb will keep breakpoints inserted even as inferior is
313 stopped, and immediately insert any new breakpoints. If off, gdb
314 will insert breakpoints into inferior only when resuming it, and
315 will remove breakpoints upon stop. If auto, GDB will behave as ON
316 if in non-stop mode, and as OFF if all-stop mode.*/
317
318static const char always_inserted_auto[] = "auto";
319static const char always_inserted_on[] = "on";
320static const char always_inserted_off[] = "off";
321static const char *always_inserted_enums[] = {
322 always_inserted_auto,
323 always_inserted_off,
324 always_inserted_on,
325 NULL
326};
327static const char *always_inserted_mode = always_inserted_auto;
328static void
74960c60 329show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 330 struct cmd_list_element *c, const char *value)
74960c60 331{
33e5cbd6 332 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
333 fprintf_filtered (file,
334 _("Always inserted breakpoint "
335 "mode is %s (currently %s).\n"),
33e5cbd6
PA
336 value,
337 breakpoints_always_inserted_mode () ? "on" : "off");
338 else
3e43a32a
MS
339 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
340 value);
74960c60
VP
341}
342
33e5cbd6
PA
343int
344breakpoints_always_inserted_mode (void)
345{
346 return (always_inserted_mode == always_inserted_on
347 || (always_inserted_mode == always_inserted_auto && non_stop));
348}
765dc015 349
a14ed312 350void _initialize_breakpoint (void);
c906108c 351
c906108c
SS
352/* Are we executing breakpoint commands? */
353static int executing_breakpoint_commands;
354
c02f5703
MS
355/* Are overlay event breakpoints enabled? */
356static int overlay_events_enabled;
357
e09342b5
TJB
358/* See description in breakpoint.h. */
359int target_exact_watchpoints = 0;
360
c906108c 361/* Walk the following statement or block through all breakpoints.
4a64f543
MS
362 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
363 current breakpoint. */
c906108c 364
5c44784c 365#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 366
5c44784c
JM
367#define ALL_BREAKPOINTS_SAFE(B,TMP) \
368 for (B = breakpoint_chain; \
369 B ? (TMP=B->next, 1): 0; \
370 B = TMP)
c906108c 371
4a64f543
MS
372/* Similar iterator for the low-level breakpoints. SAFE variant is
373 not provided so update_global_location_list must not be called
374 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 375
876fa593
JK
376#define ALL_BP_LOCATIONS(B,BP_TMP) \
377 for (BP_TMP = bp_location; \
378 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
379 BP_TMP++)
7cc221ef 380
1042e4c0
SS
381/* Iterator for tracepoints only. */
382
383#define ALL_TRACEPOINTS(B) \
384 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 385 if (is_tracepoint (B))
1042e4c0 386
7cc221ef 387/* Chains of all breakpoints defined. */
c906108c
SS
388
389struct breakpoint *breakpoint_chain;
390
876fa593
JK
391/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
392
393static struct bp_location **bp_location;
394
395/* Number of elements of BP_LOCATION. */
396
397static unsigned bp_location_count;
398
4a64f543
MS
399/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
400 ADDRESS for the current elements of BP_LOCATION which get a valid
401 result from bp_location_has_shadow. You can use it for roughly
402 limiting the subrange of BP_LOCATION to scan for shadow bytes for
403 an address you need to read. */
876fa593
JK
404
405static CORE_ADDR bp_location_placed_address_before_address_max;
406
4a64f543
MS
407/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
408 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
409 BP_LOCATION which get a valid result from bp_location_has_shadow.
410 You can use it for roughly limiting the subrange of BP_LOCATION to
411 scan for shadow bytes for an address you need to read. */
876fa593
JK
412
413static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 414
4a64f543
MS
415/* The locations that no longer correspond to any breakpoint, unlinked
416 from bp_location array, but for which a hit may still be reported
417 by a target. */
20874c92
VP
418VEC(bp_location_p) *moribund_locations = NULL;
419
c906108c
SS
420/* Number of last breakpoint made. */
421
95a42b64
TT
422static int breakpoint_count;
423
86b17b60
PA
424/* The value of `breakpoint_count' before the last command that
425 created breakpoints. If the last (break-like) command created more
426 than one breakpoint, then the difference between BREAKPOINT_COUNT
427 and PREV_BREAKPOINT_COUNT is more than one. */
428static int prev_breakpoint_count;
c906108c 429
1042e4c0
SS
430/* Number of last tracepoint made. */
431
95a42b64 432static int tracepoint_count;
1042e4c0 433
6149aea9
PA
434static struct cmd_list_element *breakpoint_set_cmdlist;
435static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 436struct cmd_list_element *save_cmdlist;
6149aea9 437
468d015d
JJ
438/* Return whether a breakpoint is an active enabled breakpoint. */
439static int
440breakpoint_enabled (struct breakpoint *b)
441{
0d381245 442 return (b->enable_state == bp_enabled);
468d015d
JJ
443}
444
c906108c
SS
445/* Set breakpoint count to NUM. */
446
95a42b64 447static void
fba45db2 448set_breakpoint_count (int num)
c906108c 449{
86b17b60 450 prev_breakpoint_count = breakpoint_count;
c906108c 451 breakpoint_count = num;
4fa62494 452 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
453}
454
86b17b60
PA
455/* Used by `start_rbreak_breakpoints' below, to record the current
456 breakpoint count before "rbreak" creates any breakpoint. */
457static int rbreak_start_breakpoint_count;
458
95a42b64
TT
459/* Called at the start an "rbreak" command to record the first
460 breakpoint made. */
86b17b60 461
95a42b64
TT
462void
463start_rbreak_breakpoints (void)
464{
86b17b60 465 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
466}
467
468/* Called at the end of an "rbreak" command to record the last
469 breakpoint made. */
86b17b60 470
95a42b64
TT
471void
472end_rbreak_breakpoints (void)
473{
86b17b60 474 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
475}
476
4a64f543 477/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
478
479void
fba45db2 480clear_breakpoint_hit_counts (void)
c906108c
SS
481{
482 struct breakpoint *b;
483
484 ALL_BREAKPOINTS (b)
485 b->hit_count = 0;
486}
487
9add0f1b
TT
488/* Allocate a new counted_command_line with reference count of 1.
489 The new structure owns COMMANDS. */
490
491static struct counted_command_line *
492alloc_counted_command_line (struct command_line *commands)
493{
494 struct counted_command_line *result
495 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 496
9add0f1b
TT
497 result->refc = 1;
498 result->commands = commands;
499 return result;
500}
501
502/* Increment reference count. This does nothing if CMD is NULL. */
503
504static void
505incref_counted_command_line (struct counted_command_line *cmd)
506{
507 if (cmd)
508 ++cmd->refc;
509}
510
511/* Decrement reference count. If the reference count reaches 0,
512 destroy the counted_command_line. Sets *CMDP to NULL. This does
513 nothing if *CMDP is NULL. */
514
515static void
516decref_counted_command_line (struct counted_command_line **cmdp)
517{
518 if (*cmdp)
519 {
520 if (--(*cmdp)->refc == 0)
521 {
522 free_command_lines (&(*cmdp)->commands);
523 xfree (*cmdp);
524 }
525 *cmdp = NULL;
526 }
527}
528
529/* A cleanup function that calls decref_counted_command_line. */
530
531static void
532do_cleanup_counted_command_line (void *arg)
533{
534 decref_counted_command_line (arg);
535}
536
537/* Create a cleanup that calls decref_counted_command_line on the
538 argument. */
539
540static struct cleanup *
541make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
542{
543 return make_cleanup (do_cleanup_counted_command_line, cmdp);
544}
545
c906108c
SS
546/* Default address, symtab and line to put a breakpoint at
547 for "break" command with no arg.
4a64f543 548 If default_breakpoint_valid is zero, the other three are
c906108c
SS
549 not valid, and "break" with no arg is an error.
550
551 This set by print_stack_frame, which calls set_default_breakpoint. */
552
553int default_breakpoint_valid;
554CORE_ADDR default_breakpoint_address;
555struct symtab *default_breakpoint_symtab;
556int default_breakpoint_line;
6c95b8df
PA
557struct program_space *default_breakpoint_pspace;
558
c906108c 559\f
48cb2d85
VP
560/* Return the breakpoint with the specified number, or NULL
561 if the number does not refer to an existing breakpoint. */
562
563struct breakpoint *
564get_breakpoint (int num)
565{
566 struct breakpoint *b;
567
568 ALL_BREAKPOINTS (b)
569 if (b->number == num)
570 return b;
571
572 return NULL;
573}
5c44784c 574
c906108c 575\f
adc36818
PM
576
577void
578set_breakpoint_condition (struct breakpoint *b, char *exp,
579 int from_tty)
580{
581 struct bp_location *loc = b->loc;
582
583 for (; loc; loc = loc->next)
584 {
585 xfree (loc->cond);
586 loc->cond = NULL;
587 }
588 xfree (b->cond_string);
589 b->cond_string = NULL;
590 xfree (b->cond_exp);
591 b->cond_exp = NULL;
592
593 if (*exp == 0)
594 {
595 if (from_tty)
596 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
597 }
598 else
599 {
600 char *arg = exp;
cc59ec59 601
adc36818
PM
602 /* I don't know if it matters whether this is the string the user
603 typed in or the decompiled expression. */
604 b->cond_string = xstrdup (arg);
605 b->condition_not_parsed = 0;
606
607 if (is_watchpoint (b))
608 {
609 innermost_block = NULL;
610 arg = exp;
611 b->cond_exp = parse_exp_1 (&arg, 0, 0);
612 if (*arg)
613 error (_("Junk at end of expression"));
614 b->cond_exp_valid_block = innermost_block;
615 }
616 else
617 {
618 for (loc = b->loc; loc; loc = loc->next)
619 {
620 arg = exp;
621 loc->cond =
622 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
623 if (*arg)
624 error (_("Junk at end of expression"));
625 }
626 }
627 }
628 breakpoints_changed ();
8d3788bd 629 observer_notify_breakpoint_modified (b);
adc36818
PM
630}
631
c906108c
SS
632/* condition N EXP -- set break condition of breakpoint N to EXP. */
633
634static void
fba45db2 635condition_command (char *arg, int from_tty)
c906108c 636{
52f0bd74 637 struct breakpoint *b;
c906108c 638 char *p;
52f0bd74 639 int bnum;
c906108c
SS
640
641 if (arg == 0)
e2e0b3e5 642 error_no_arg (_("breakpoint number"));
c906108c
SS
643
644 p = arg;
645 bnum = get_number (&p);
5c44784c 646 if (bnum == 0)
8a3fe4f8 647 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
648
649 ALL_BREAKPOINTS (b)
650 if (b->number == bnum)
2f069f6f 651 {
7371cf6d
PM
652 /* Check if this breakpoint has a Python object assigned to
653 it, and if it has a definition of the "stop"
654 method. This method and conditions entered into GDB from
655 the CLI are mutually exclusive. */
656 if (b->py_bp_object
657 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
658 error (_("Cannot set a condition where a Python 'stop' "
659 "method has been defined in the breakpoint."));
2566ad2d 660 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
661 return;
662 }
c906108c 663
8a3fe4f8 664 error (_("No breakpoint number %d."), bnum);
c906108c
SS
665}
666
a7bdde9e
VP
667/* Check that COMMAND do not contain commands that are suitable
668 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
669 Throw if any such commands is found. */
670
a7bdde9e
VP
671static void
672check_no_tracepoint_commands (struct command_line *commands)
673{
674 struct command_line *c;
cc59ec59 675
a7bdde9e
VP
676 for (c = commands; c; c = c->next)
677 {
678 int i;
679
680 if (c->control_type == while_stepping_control)
3e43a32a
MS
681 error (_("The 'while-stepping' command can "
682 "only be used for tracepoints"));
a7bdde9e
VP
683
684 for (i = 0; i < c->body_count; ++i)
685 check_no_tracepoint_commands ((c->body_list)[i]);
686
687 /* Not that command parsing removes leading whitespace and comment
4a64f543 688 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
689 command directly. */
690 if (strstr (c->line, "collect ") == c->line)
691 error (_("The 'collect' command can only be used for tracepoints"));
692
51661e93
VP
693 if (strstr (c->line, "teval ") == c->line)
694 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
695 }
696}
697
d77f58be
SS
698/* Encapsulate tests for different types of tracepoints. */
699
a7bdde9e 700int
d77f58be 701is_tracepoint (const struct breakpoint *b)
a7bdde9e 702{
0fb4aa4b
PA
703 return (b->type == bp_tracepoint
704 || b->type == bp_fast_tracepoint
705 || b->type == bp_static_tracepoint);
a7bdde9e 706}
d77f58be 707
95a42b64
TT
708/* A helper function that validsates that COMMANDS are valid for a
709 breakpoint. This function will throw an exception if a problem is
710 found. */
48cb2d85 711
95a42b64
TT
712static void
713validate_commands_for_breakpoint (struct breakpoint *b,
714 struct command_line *commands)
48cb2d85 715{
d77f58be 716 if (is_tracepoint (b))
a7bdde9e 717 {
4a64f543
MS
718 /* We need to verify that each top-level element of commands is
719 valid for tracepoints, that there's at most one
720 while-stepping element, and that while-stepping's body has
721 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
722 struct command_line *c;
723 struct command_line *while_stepping = 0;
724 for (c = commands; c; c = c->next)
725 {
a7bdde9e
VP
726 if (c->control_type == while_stepping_control)
727 {
728 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
729 error (_("The 'while-stepping' command "
730 "cannot be used for fast tracepoint"));
0fb4aa4b 731 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
732 error (_("The 'while-stepping' command "
733 "cannot be used for static tracepoint"));
a7bdde9e
VP
734
735 if (while_stepping)
3e43a32a
MS
736 error (_("The 'while-stepping' command "
737 "can be used only once"));
a7bdde9e
VP
738 else
739 while_stepping = c;
740 }
741 }
742 if (while_stepping)
743 {
744 struct command_line *c2;
745
746 gdb_assert (while_stepping->body_count == 1);
747 c2 = while_stepping->body_list[0];
748 for (; c2; c2 = c2->next)
749 {
a7bdde9e
VP
750 if (c2->control_type == while_stepping_control)
751 error (_("The 'while-stepping' command cannot be nested"));
752 }
753 }
754 }
755 else
756 {
757 check_no_tracepoint_commands (commands);
758 }
95a42b64
TT
759}
760
0fb4aa4b
PA
761/* Return a vector of all the static tracepoints set at ADDR. The
762 caller is responsible for releasing the vector. */
763
764VEC(breakpoint_p) *
765static_tracepoints_here (CORE_ADDR addr)
766{
767 struct breakpoint *b;
768 VEC(breakpoint_p) *found = 0;
769 struct bp_location *loc;
770
771 ALL_BREAKPOINTS (b)
772 if (b->type == bp_static_tracepoint)
773 {
774 for (loc = b->loc; loc; loc = loc->next)
775 if (loc->address == addr)
776 VEC_safe_push(breakpoint_p, found, b);
777 }
778
779 return found;
780}
781
95a42b64 782/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 783 validate that only allowed commands are included. */
95a42b64
TT
784
785void
4a64f543
MS
786breakpoint_set_commands (struct breakpoint *b,
787 struct command_line *commands)
95a42b64
TT
788{
789 validate_commands_for_breakpoint (b, commands);
a7bdde9e 790
9add0f1b
TT
791 decref_counted_command_line (&b->commands);
792 b->commands = alloc_counted_command_line (commands);
48cb2d85 793 breakpoints_changed ();
8d3788bd 794 observer_notify_breakpoint_modified (b);
48cb2d85
VP
795}
796
45a43567
TT
797/* Set the internal `silent' flag on the breakpoint. Note that this
798 is not the same as the "silent" that may appear in the breakpoint's
799 commands. */
800
801void
802breakpoint_set_silent (struct breakpoint *b, int silent)
803{
804 int old_silent = b->silent;
805
806 b->silent = silent;
807 if (old_silent != silent)
8d3788bd 808 observer_notify_breakpoint_modified (b);
45a43567
TT
809}
810
811/* Set the thread for this breakpoint. If THREAD is -1, make the
812 breakpoint work for any thread. */
813
814void
815breakpoint_set_thread (struct breakpoint *b, int thread)
816{
817 int old_thread = b->thread;
818
819 b->thread = thread;
820 if (old_thread != thread)
8d3788bd 821 observer_notify_breakpoint_modified (b);
45a43567
TT
822}
823
824/* Set the task for this breakpoint. If TASK is 0, make the
825 breakpoint work for any task. */
826
827void
828breakpoint_set_task (struct breakpoint *b, int task)
829{
830 int old_task = b->task;
831
832 b->task = task;
833 if (old_task != task)
8d3788bd 834 observer_notify_breakpoint_modified (b);
45a43567
TT
835}
836
95a42b64
TT
837void
838check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
839{
840 struct breakpoint *b = closure;
cc59ec59 841
a7bdde9e
VP
842 validate_actionline (&line, b);
843}
844
95a42b64
TT
845/* A structure used to pass information through
846 map_breakpoint_numbers. */
847
848struct commands_info
849{
850 /* True if the command was typed at a tty. */
851 int from_tty;
86b17b60
PA
852
853 /* The breakpoint range spec. */
854 char *arg;
855
95a42b64
TT
856 /* Non-NULL if the body of the commands are being read from this
857 already-parsed command. */
858 struct command_line *control;
86b17b60 859
95a42b64
TT
860 /* The command lines read from the user, or NULL if they have not
861 yet been read. */
862 struct counted_command_line *cmd;
863};
864
865/* A callback for map_breakpoint_numbers that sets the commands for
866 commands_command. */
867
c906108c 868static void
95a42b64 869do_map_commands_command (struct breakpoint *b, void *data)
c906108c 870{
95a42b64 871 struct commands_info *info = data;
c906108c 872
95a42b64
TT
873 if (info->cmd == NULL)
874 {
875 struct command_line *l;
5c44784c 876
95a42b64
TT
877 if (info->control != NULL)
878 l = copy_command_lines (info->control->body_list[0]);
879 else
86b17b60
PA
880 {
881 struct cleanup *old_chain;
882 char *str;
c5aa993b 883
3e43a32a
MS
884 str = xstrprintf (_("Type commands for breakpoint(s) "
885 "%s, one per line."),
86b17b60
PA
886 info->arg);
887
888 old_chain = make_cleanup (xfree, str);
889
890 l = read_command_lines (str,
891 info->from_tty, 1,
d77f58be 892 (is_tracepoint (b)
86b17b60
PA
893 ? check_tracepoint_command : 0),
894 b);
895
896 do_cleanups (old_chain);
897 }
a7bdde9e 898
95a42b64
TT
899 info->cmd = alloc_counted_command_line (l);
900 }
901
902 /* If a breakpoint was on the list more than once, we don't need to
903 do anything. */
904 if (b->commands != info->cmd)
905 {
906 validate_commands_for_breakpoint (b, info->cmd->commands);
907 incref_counted_command_line (info->cmd);
908 decref_counted_command_line (&b->commands);
909 b->commands = info->cmd;
910 breakpoints_changed ();
8d3788bd 911 observer_notify_breakpoint_modified (b);
c5aa993b 912 }
95a42b64
TT
913}
914
915static void
4a64f543
MS
916commands_command_1 (char *arg, int from_tty,
917 struct command_line *control)
95a42b64
TT
918{
919 struct cleanup *cleanups;
920 struct commands_info info;
921
922 info.from_tty = from_tty;
923 info.control = control;
924 info.cmd = NULL;
925 /* If we read command lines from the user, then `info' will hold an
926 extra reference to the commands that we must clean up. */
927 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
928
929 if (arg == NULL || !*arg)
930 {
86b17b60 931 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
932 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
933 breakpoint_count);
95a42b64
TT
934 else if (breakpoint_count > 0)
935 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
936 else
937 {
938 /* So that we don't try to free the incoming non-NULL
939 argument in the cleanup below. Mapping breakpoint
940 numbers will fail in this case. */
941 arg = NULL;
942 }
95a42b64 943 }
9766ced4
SS
944 else
945 /* The command loop has some static state, so we need to preserve
946 our argument. */
947 arg = xstrdup (arg);
86b17b60
PA
948
949 if (arg != NULL)
950 make_cleanup (xfree, arg);
951
952 info.arg = arg;
95a42b64
TT
953
954 map_breakpoint_numbers (arg, do_map_commands_command, &info);
955
956 if (info.cmd == NULL)
957 error (_("No breakpoints specified."));
958
959 do_cleanups (cleanups);
960}
961
962static void
963commands_command (char *arg, int from_tty)
964{
965 commands_command_1 (arg, from_tty, NULL);
c906108c 966}
40c03ae8
EZ
967
968/* Like commands_command, but instead of reading the commands from
969 input stream, takes them from an already parsed command structure.
970
971 This is used by cli-script.c to DTRT with breakpoint commands
972 that are part of if and while bodies. */
973enum command_control_type
974commands_from_control_command (char *arg, struct command_line *cmd)
975{
95a42b64
TT
976 commands_command_1 (arg, 0, cmd);
977 return simple_control;
40c03ae8 978}
876fa593
JK
979
980/* Return non-zero if BL->TARGET_INFO contains valid information. */
981
982static int
983bp_location_has_shadow (struct bp_location *bl)
984{
985 if (bl->loc_type != bp_loc_software_breakpoint)
986 return 0;
987 if (!bl->inserted)
988 return 0;
989 if (bl->target_info.shadow_len == 0)
990 /* bp isn't valid, or doesn't shadow memory. */
991 return 0;
992 return 1;
993}
994
8defab1a 995/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
996 by replacing any memory breakpoints with their shadowed contents.
997
998 The range of shadowed area by each bp_location is:
35df4500
TJB
999 bl->address - bp_location_placed_address_before_address_max
1000 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1001 The range we were requested to resolve shadows for is:
1002 memaddr ... memaddr + len
1003 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1004 memaddr + len <= (bl->address
1005 - bp_location_placed_address_before_address_max)
876fa593 1006 and:
35df4500 1007 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1008
8defab1a
DJ
1009void
1010breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1011{
4a64f543
MS
1012 /* Left boundary, right boundary and median element of our binary
1013 search. */
876fa593
JK
1014 unsigned bc_l, bc_r, bc;
1015
4a64f543
MS
1016 /* Find BC_L which is a leftmost element which may affect BUF
1017 content. It is safe to report lower value but a failure to
1018 report higher one. */
876fa593
JK
1019
1020 bc_l = 0;
1021 bc_r = bp_location_count;
1022 while (bc_l + 1 < bc_r)
1023 {
35df4500 1024 struct bp_location *bl;
876fa593
JK
1025
1026 bc = (bc_l + bc_r) / 2;
35df4500 1027 bl = bp_location[bc];
876fa593 1028
4a64f543
MS
1029 /* Check first BL->ADDRESS will not overflow due to the added
1030 constant. Then advance the left boundary only if we are sure
1031 the BC element can in no way affect the BUF content (MEMADDR
1032 to MEMADDR + LEN range).
876fa593 1033
4a64f543
MS
1034 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1035 offset so that we cannot miss a breakpoint with its shadow
1036 range tail still reaching MEMADDR. */
c5aa993b 1037
35df4500
TJB
1038 if ((bl->address + bp_location_shadow_len_after_address_max
1039 >= bl->address)
1040 && (bl->address + bp_location_shadow_len_after_address_max
1041 <= memaddr))
876fa593
JK
1042 bc_l = bc;
1043 else
1044 bc_r = bc;
1045 }
1046
128070bb
PA
1047 /* Due to the binary search above, we need to make sure we pick the
1048 first location that's at BC_L's address. E.g., if there are
1049 multiple locations at the same address, BC_L may end up pointing
1050 at a duplicate location, and miss the "master"/"inserted"
1051 location. Say, given locations L1, L2 and L3 at addresses A and
1052 B:
1053
1054 L1@A, L2@A, L3@B, ...
1055
1056 BC_L could end up pointing at location L2, while the "master"
1057 location could be L1. Since the `loc->inserted' flag is only set
1058 on "master" locations, we'd forget to restore the shadow of L1
1059 and L2. */
1060 while (bc_l > 0
1061 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1062 bc_l--;
1063
876fa593
JK
1064 /* Now do full processing of the found relevant range of elements. */
1065
1066 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1067 {
35df4500 1068 struct bp_location *bl = bp_location[bc];
876fa593
JK
1069 CORE_ADDR bp_addr = 0;
1070 int bp_size = 0;
1071 int bptoffset = 0;
1072
35df4500
TJB
1073 /* bp_location array has BL->OWNER always non-NULL. */
1074 if (bl->owner->type == bp_none)
8a3fe4f8 1075 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1076 bl->owner->number);
ffce0d52 1077
876fa593
JK
1078 /* Performance optimization: any futher element can no longer affect BUF
1079 content. */
1080
35df4500
TJB
1081 if (bl->address >= bp_location_placed_address_before_address_max
1082 && memaddr + len <= (bl->address
1083 - bp_location_placed_address_before_address_max))
876fa593
JK
1084 break;
1085
35df4500 1086 if (!bp_location_has_shadow (bl))
c5aa993b 1087 continue;
35df4500 1088 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1089 current_program_space->aspace, 0))
1090 continue;
1091
c5aa993b
JM
1092 /* Addresses and length of the part of the breakpoint that
1093 we need to copy. */
35df4500
TJB
1094 bp_addr = bl->target_info.placed_address;
1095 bp_size = bl->target_info.shadow_len;
8defab1a 1096
c5aa993b
JM
1097 if (bp_addr + bp_size <= memaddr)
1098 /* The breakpoint is entirely before the chunk of memory we
1099 are reading. */
1100 continue;
8defab1a 1101
c5aa993b
JM
1102 if (bp_addr >= memaddr + len)
1103 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1104 reading. */
c5aa993b 1105 continue;
c5aa993b 1106
8defab1a
DJ
1107 /* Offset within shadow_contents. */
1108 if (bp_addr < memaddr)
1109 {
1110 /* Only copy the second part of the breakpoint. */
1111 bp_size -= memaddr - bp_addr;
1112 bptoffset = memaddr - bp_addr;
1113 bp_addr = memaddr;
1114 }
c5aa993b 1115
8defab1a
DJ
1116 if (bp_addr + bp_size > memaddr + len)
1117 {
1118 /* Only copy the first part of the breakpoint. */
1119 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1120 }
c5aa993b 1121
8defab1a 1122 memcpy (buf + bp_addr - memaddr,
35df4500 1123 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1124 }
c906108c 1125}
c906108c 1126\f
c5aa993b 1127
60e1c644
PA
1128/* Return true if BPT is of any hardware watchpoint kind. */
1129
a5606eee 1130static int
d77f58be 1131is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1132{
1133 return (bpt->type == bp_hardware_watchpoint
1134 || bpt->type == bp_read_watchpoint
1135 || bpt->type == bp_access_watchpoint);
1136}
7270d8f2 1137
60e1c644
PA
1138/* Return true if BPT is of any watchpoint kind, hardware or
1139 software. */
1140
1141static int
d77f58be 1142is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1143{
1144 return (is_hardware_watchpoint (bpt)
1145 || bpt->type == bp_watchpoint);
1146}
1147
f6bc2008
PA
1148/* Assuming that B is a watchpoint: returns true if the current thread
1149 and its running state are safe to evaluate or update watchpoint B.
1150 Watchpoints on local expressions need to be evaluated in the
1151 context of the thread that was current when the watchpoint was
1152 created, and, that thread needs to be stopped to be able to select
1153 the correct frame context. Watchpoints on global expressions can
1154 be evaluated on any thread, and in any state. It is presently left
1155 to the target allowing memory accesses when threads are
1156 running. */
1157
1158static int
1159watchpoint_in_thread_scope (struct breakpoint *b)
1160{
1161 return (ptid_equal (b->watchpoint_thread, null_ptid)
1162 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1163 && !is_executing (inferior_ptid)));
1164}
1165
d0fb5eae
JK
1166/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1167 associated bp_watchpoint_scope breakpoint. */
1168
1169static void
1170watchpoint_del_at_next_stop (struct breakpoint *b)
1171{
1172 gdb_assert (is_watchpoint (b));
1173
1174 if (b->related_breakpoint != b)
1175 {
1176 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1177 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1178 b->related_breakpoint->disposition = disp_del_at_next_stop;
1179 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1180 b->related_breakpoint = b;
1181 }
1182 b->disposition = disp_del_at_next_stop;
1183}
1184
567e1b4e
JB
1185/* Assuming that B is a watchpoint:
1186 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1187 - Evaluate expression and store the result in B->val
567e1b4e
JB
1188 - Evaluate the condition if there is one, and store the result
1189 in b->loc->cond.
a5606eee
VP
1190 - Update the list of values that must be watched in B->loc.
1191
4a64f543
MS
1192 If the watchpoint disposition is disp_del_at_next_stop, then do
1193 nothing. If this is local watchpoint that is out of scope, delete
1194 it.
1195
1196 Even with `set breakpoint always-inserted on' the watchpoints are
1197 removed + inserted on each stop here. Normal breakpoints must
1198 never be removed because they might be missed by a running thread
1199 when debugging in non-stop mode. On the other hand, hardware
1200 watchpoints (is_hardware_watchpoint; processed here) are specific
1201 to each LWP since they are stored in each LWP's hardware debug
1202 registers. Therefore, such LWP must be stopped first in order to
1203 be able to modify its hardware watchpoints.
1204
1205 Hardware watchpoints must be reset exactly once after being
1206 presented to the user. It cannot be done sooner, because it would
1207 reset the data used to present the watchpoint hit to the user. And
1208 it must not be done later because it could display the same single
1209 watchpoint hit during multiple GDB stops. Note that the latter is
1210 relevant only to the hardware watchpoint types bp_read_watchpoint
1211 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1212 not user-visible - its hit is suppressed if the memory content has
1213 not changed.
1214
1215 The following constraints influence the location where we can reset
1216 hardware watchpoints:
1217
1218 * target_stopped_by_watchpoint and target_stopped_data_address are
1219 called several times when GDB stops.
1220
1221 [linux]
1222 * Multiple hardware watchpoints can be hit at the same time,
1223 causing GDB to stop. GDB only presents one hardware watchpoint
1224 hit at a time as the reason for stopping, and all the other hits
1225 are presented later, one after the other, each time the user
1226 requests the execution to be resumed. Execution is not resumed
1227 for the threads still having pending hit event stored in
1228 LWP_INFO->STATUS. While the watchpoint is already removed from
1229 the inferior on the first stop the thread hit event is kept being
1230 reported from its cached value by linux_nat_stopped_data_address
1231 until the real thread resume happens after the watchpoint gets
1232 presented and thus its LWP_INFO->STATUS gets reset.
1233
1234 Therefore the hardware watchpoint hit can get safely reset on the
1235 watchpoint removal from inferior. */
a79d3c27 1236
b40ce68a 1237static void
a5606eee 1238update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1239{
a5606eee 1240 int within_current_scope;
a5606eee 1241 struct frame_id saved_frame_id;
66076460 1242 int frame_saved;
a5606eee 1243
d0fb5eae
JK
1244 gdb_assert (is_watchpoint (b));
1245
f6bc2008
PA
1246 /* If this is a local watchpoint, we only want to check if the
1247 watchpoint frame is in scope if the current thread is the thread
1248 that was used to create the watchpoint. */
1249 if (!watchpoint_in_thread_scope (b))
1250 return;
1251
a5606eee
VP
1252 if (b->disposition == disp_del_at_next_stop)
1253 return;
1254
66076460 1255 frame_saved = 0;
a5606eee
VP
1256
1257 /* Determine if the watchpoint is within scope. */
1258 if (b->exp_valid_block == NULL)
1259 within_current_scope = 1;
1260 else
1261 {
b5db5dfc
UW
1262 struct frame_info *fi = get_current_frame ();
1263 struct gdbarch *frame_arch = get_frame_arch (fi);
1264 CORE_ADDR frame_pc = get_frame_pc (fi);
1265
1266 /* If we're in a function epilogue, unwinding may not work
1267 properly, so do not attempt to recreate locations at this
1268 point. See similar comments in watchpoint_check. */
1269 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1270 return;
66076460
DJ
1271
1272 /* Save the current frame's ID so we can restore it after
1273 evaluating the watchpoint expression on its own frame. */
1274 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1275 took a frame parameter, so that we didn't have to change the
1276 selected frame. */
1277 frame_saved = 1;
1278 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1279
a5606eee
VP
1280 fi = frame_find_by_id (b->watchpoint_frame);
1281 within_current_scope = (fi != NULL);
1282 if (within_current_scope)
1283 select_frame (fi);
1284 }
1285
b5db5dfc
UW
1286 /* We don't free locations. They are stored in the bp_location array
1287 and update_global_location_list will eventually delete them and
1288 remove breakpoints if needed. */
1289 b->loc = NULL;
1290
a5606eee
VP
1291 if (within_current_scope && reparse)
1292 {
1293 char *s;
d63d0675 1294
a5606eee
VP
1295 if (b->exp)
1296 {
1297 xfree (b->exp);
1298 b->exp = NULL;
1299 }
d63d0675 1300 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1301 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1302 /* If the meaning of expression itself changed, the old value is
1303 no longer relevant. We don't want to report a watchpoint hit
1304 to the user when the old value and the new value may actually
1305 be completely different objects. */
1306 value_free (b->val);
fa4727a6
DJ
1307 b->val = NULL;
1308 b->val_valid = 0;
60e1c644
PA
1309
1310 /* Note that unlike with breakpoints, the watchpoint's condition
1311 expression is stored in the breakpoint object, not in the
1312 locations (re)created below. */
1313 if (b->cond_string != NULL)
1314 {
1315 if (b->cond_exp != NULL)
1316 {
1317 xfree (b->cond_exp);
1318 b->cond_exp = NULL;
1319 }
1320
1321 s = b->cond_string;
1322 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1323 }
a5606eee 1324 }
a5606eee
VP
1325
1326 /* If we failed to parse the expression, for example because
1327 it refers to a global variable in a not-yet-loaded shared library,
1328 don't try to insert watchpoint. We don't automatically delete
1329 such watchpoint, though, since failure to parse expression
1330 is different from out-of-scope watchpoint. */
2d134ed3
PA
1331 if ( !target_has_execution)
1332 {
1333 /* Without execution, memory can't change. No use to try and
1334 set watchpoint locations. The watchpoint will be reset when
1335 the target gains execution, through breakpoint_re_set. */
1336 }
1337 else if (within_current_scope && b->exp)
a5606eee 1338 {
0cf6dd15 1339 int pc = 0;
fa4727a6 1340 struct value *val_chain, *v, *result, *next;
2d134ed3 1341 struct program_space *frame_pspace;
a5606eee 1342
0cf6dd15 1343 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1344
a5606eee
VP
1345 /* Avoid setting b->val if it's already set. The meaning of
1346 b->val is 'the last value' user saw, and we should update
1347 it only if we reported that last value to user. As it
1348 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1349 if (!b->val_valid)
1350 {
1351 b->val = v;
1352 b->val_valid = 1;
1353 }
a5606eee 1354
2d134ed3
PA
1355 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1356
a5606eee 1357 /* Look at each value on the value chain. */
9fa40276 1358 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1359 {
1360 /* If it's a memory location, and GDB actually needed
1361 its contents to evaluate the expression, then we
fa4727a6
DJ
1362 must watch it. If the first value returned is
1363 still lazy, that means an error occurred reading it;
1364 watch it anyway in case it becomes readable. */
a5606eee 1365 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1366 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1367 {
1368 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1369
a5606eee
VP
1370 /* We only watch structs and arrays if user asked
1371 for it explicitly, never if they just happen to
1372 appear in the middle of some value chain. */
fa4727a6 1373 if (v == result
a5606eee
VP
1374 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1375 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1376 {
1377 CORE_ADDR addr;
1378 int len, type;
1379 struct bp_location *loc, **tmp;
1380
42ae5230 1381 addr = value_address (v);
a5606eee
VP
1382 len = TYPE_LENGTH (value_type (v));
1383 type = hw_write;
1384 if (b->type == bp_read_watchpoint)
1385 type = hw_read;
1386 else if (b->type == bp_access_watchpoint)
1387 type = hw_access;
1388
39d61571 1389 loc = allocate_bp_location (b);
a5606eee
VP
1390 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1391 ;
1392 *tmp = loc;
a6d9a66e 1393 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1394
1395 loc->pspace = frame_pspace;
a5606eee
VP
1396 loc->address = addr;
1397 loc->length = len;
1398 loc->watchpoint_type = type;
1399 }
1400 }
9fa40276
TJB
1401 }
1402
1403 /* Change the type of breakpoint between hardware assisted or
1404 an ordinary watchpoint depending on the hardware support
1405 and free hardware slots. REPARSE is set when the inferior
1406 is started. */
1407 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1408 && reparse)
1409 {
e09342b5 1410 int reg_cnt;
9fa40276
TJB
1411 enum bp_loc_type loc_type;
1412 struct bp_location *bl;
a5606eee 1413
e09342b5
TJB
1414 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1415
1416 if (reg_cnt)
9fa40276
TJB
1417 {
1418 int i, target_resources_ok, other_type_used;
1419
1420 /* We need to determine how many resources are already
e09342b5
TJB
1421 used for all other hardware watchpoints plus this one
1422 to see if we still have enough resources to also fit
1423 this watchpoint in as well. To guarantee the
1424 hw_watchpoint_used_count call below counts this
1425 watchpoint, make sure that it is marked as a hardware
1426 watchpoint. */
1427 b->type = bp_hardware_watchpoint;
1428
9fa40276
TJB
1429 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1430 &other_type_used);
9fa40276 1431
e09342b5
TJB
1432 target_resources_ok = target_can_use_hardware_watchpoint
1433 (bp_hardware_watchpoint, i, other_type_used);
1434 if (target_resources_ok <= 0)
1435 b->type = bp_watchpoint;
9fa40276
TJB
1436 }
1437 else
1438 b->type = bp_watchpoint;
1439
1440 loc_type = (b->type == bp_watchpoint? bp_loc_other
1441 : bp_loc_hardware_watchpoint);
1442 for (bl = b->loc; bl; bl = bl->next)
1443 bl->loc_type = loc_type;
1444 }
1445
1446 for (v = val_chain; v; v = next)
1447 {
a5606eee
VP
1448 next = value_next (v);
1449 if (v != b->val)
1450 value_free (v);
1451 }
1452
c7437ca6
PA
1453 /* If a software watchpoint is not watching any memory, then the
1454 above left it without any location set up. But,
1455 bpstat_stop_status requires a location to be able to report
1456 stops, so make sure there's at least a dummy one. */
1457 if (b->type == bp_watchpoint && b->loc == NULL)
1458 {
1459 b->loc = allocate_bp_location (b);
1460 b->loc->pspace = frame_pspace;
1461 b->loc->address = -1;
1462 b->loc->length = -1;
1463 b->loc->watchpoint_type = -1;
1464 }
a5606eee
VP
1465 }
1466 else if (!within_current_scope)
7270d8f2 1467 {
ac74f770
MS
1468 printf_filtered (_("\
1469Watchpoint %d deleted because the program has left the block\n\
1470in which its expression is valid.\n"),
a5606eee 1471 b->number);
d0fb5eae 1472 watchpoint_del_at_next_stop (b);
7270d8f2 1473 }
a5606eee
VP
1474
1475 /* Restore the selected frame. */
66076460
DJ
1476 if (frame_saved)
1477 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1478}
1479
a5606eee 1480
74960c60
VP
1481/* Returns 1 iff breakpoint location should be
1482 inserted in the inferior. */
1483static int
35df4500 1484should_be_inserted (struct bp_location *bl)
74960c60 1485{
35df4500 1486 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1487 return 0;
1488
35df4500 1489 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1490 return 0;
1491
35df4500 1492 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1493 return 0;
1494
56710373
PA
1495 /* This is set for example, when we're attached to the parent of a
1496 vfork, and have detached from the child. The child is running
1497 free, and we expect it to do an exec or exit, at which point the
1498 OS makes the parent schedulable again (and the target reports
1499 that the vfork is done). Until the child is done with the shared
1500 memory region, do not insert breakpoints in the parent, otherwise
1501 the child could still trip on the parent's breakpoints. Since
1502 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1503 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1504 return 0;
1505
1042e4c0
SS
1506 /* Tracepoints are inserted by the target at a time of its choosing,
1507 not by us. */
35df4500 1508 if (is_tracepoint (bl->owner))
1042e4c0
SS
1509 return 0;
1510
74960c60
VP
1511 return 1;
1512}
1513
35df4500
TJB
1514/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1515 location. Any error messages are printed to TMP_ERROR_STREAM; and
1516 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1517
4a64f543
MS
1518 NOTE drow/2003-09-09: This routine could be broken down to an
1519 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1520static int
35df4500 1521insert_bp_location (struct bp_location *bl,
26bb91f3 1522 struct ui_file *tmp_error_stream,
fa3a767f 1523 int *disabled_breaks,
26bb91f3 1524 int *hw_breakpoint_error)
879bfdc2
DJ
1525{
1526 int val = 0;
1527
35df4500 1528 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1529 return 0;
1530
8181d85f 1531 /* Initialize the target-specific information. */
35df4500
TJB
1532 memset (&bl->target_info, 0, sizeof (bl->target_info));
1533 bl->target_info.placed_address = bl->address;
1534 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1535 bl->target_info.length = bl->length;
8181d85f 1536
35df4500
TJB
1537 if (bl->loc_type == bp_loc_software_breakpoint
1538 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1539 {
35df4500 1540 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1541 {
1542 /* If the explicitly specified breakpoint type
1543 is not hardware breakpoint, check the memory map to see
1544 if the breakpoint address is in read only memory or not.
4a64f543 1545
765dc015
VP
1546 Two important cases are:
1547 - location type is not hardware breakpoint, memory
1548 is readonly. We change the type of the location to
1549 hardware breakpoint.
4a64f543
MS
1550 - location type is hardware breakpoint, memory is
1551 read-write. This means we've previously made the
1552 location hardware one, but then the memory map changed,
1553 so we undo.
765dc015 1554
4a64f543
MS
1555 When breakpoints are removed, remove_breakpoints will use
1556 location types we've just set here, the only possible
1557 problem is that memory map has changed during running
1558 program, but it's not going to work anyway with current
1559 gdb. */
765dc015 1560 struct mem_region *mr
35df4500 1561 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1562
1563 if (mr)
1564 {
1565 if (automatic_hardware_breakpoints)
1566 {
765dc015
VP
1567 enum bp_loc_type new_type;
1568
1569 if (mr->attrib.mode != MEM_RW)
1570 new_type = bp_loc_hardware_breakpoint;
1571 else
1572 new_type = bp_loc_software_breakpoint;
1573
35df4500 1574 if (new_type != bl->loc_type)
765dc015
VP
1575 {
1576 static int said = 0;
cc59ec59 1577
35df4500 1578 bl->loc_type = new_type;
765dc015
VP
1579 if (!said)
1580 {
3e43a32a
MS
1581 fprintf_filtered (gdb_stdout,
1582 _("Note: automatically using "
1583 "hardware breakpoints for "
1584 "read-only addresses.\n"));
765dc015
VP
1585 said = 1;
1586 }
1587 }
1588 }
35df4500 1589 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1590 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1591 warning (_("cannot set software breakpoint "
1592 "at readonly address %s"),
35df4500 1593 paddress (bl->gdbarch, bl->address));
765dc015
VP
1594 }
1595 }
1596
879bfdc2
DJ
1597 /* First check to see if we have to handle an overlay. */
1598 if (overlay_debugging == ovly_off
35df4500
TJB
1599 || bl->section == NULL
1600 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1601 {
1602 /* No overlay handling: just set the breakpoint. */
1603
35df4500
TJB
1604 if (bl->loc_type == bp_loc_hardware_breakpoint)
1605 val = target_insert_hw_breakpoint (bl->gdbarch,
1606 &bl->target_info);
879bfdc2 1607 else
35df4500
TJB
1608 val = target_insert_breakpoint (bl->gdbarch,
1609 &bl->target_info);
879bfdc2
DJ
1610 }
1611 else
1612 {
4a64f543 1613 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1614 Shall we set a breakpoint at the LMA? */
1615 if (!overlay_events_enabled)
1616 {
1617 /* Yes -- overlay event support is not active,
1618 so we must try to set a breakpoint at the LMA.
1619 This will not work for a hardware breakpoint. */
35df4500 1620 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1621 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1622 bl->owner->number);
879bfdc2
DJ
1623 else
1624 {
35df4500
TJB
1625 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1626 bl->section);
879bfdc2 1627 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1628 bl->overlay_target_info = bl->target_info;
1629 bl->overlay_target_info.placed_address = addr;
1630 val = target_insert_breakpoint (bl->gdbarch,
1631 &bl->overlay_target_info);
879bfdc2 1632 if (val != 0)
99361f52 1633 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1634 "Overlay breakpoint %d "
1635 "failed: in ROM?\n",
35df4500 1636 bl->owner->number);
879bfdc2
DJ
1637 }
1638 }
1639 /* Shall we set a breakpoint at the VMA? */
35df4500 1640 if (section_is_mapped (bl->section))
879bfdc2
DJ
1641 {
1642 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1643 if (bl->loc_type == bp_loc_hardware_breakpoint)
1644 val = target_insert_hw_breakpoint (bl->gdbarch,
1645 &bl->target_info);
879bfdc2 1646 else
35df4500
TJB
1647 val = target_insert_breakpoint (bl->gdbarch,
1648 &bl->target_info);
879bfdc2
DJ
1649 }
1650 else
1651 {
1652 /* No. This breakpoint will not be inserted.
1653 No error, but do not mark the bp as 'inserted'. */
1654 return 0;
1655 }
1656 }
1657
1658 if (val)
1659 {
1660 /* Can't set the breakpoint. */
35df4500 1661 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1662 {
4a64f543 1663 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1664 val = 0;
35df4500 1665 bl->shlib_disabled = 1;
8d3788bd 1666 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1667 if (!*disabled_breaks)
1668 {
1669 fprintf_unfiltered (tmp_error_stream,
1670 "Cannot insert breakpoint %d.\n",
35df4500 1671 bl->owner->number);
879bfdc2 1672 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1673 "Temporarily disabling shared "
1674 "library breakpoints:\n");
879bfdc2
DJ
1675 }
1676 *disabled_breaks = 1;
1677 fprintf_unfiltered (tmp_error_stream,
35df4500 1678 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1679 }
1680 else
879bfdc2 1681 {
35df4500 1682 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1683 {
1684 *hw_breakpoint_error = 1;
3e43a32a
MS
1685 fprintf_unfiltered (tmp_error_stream,
1686 "Cannot insert hardware "
1687 "breakpoint %d.\n",
35df4500 1688 bl->owner->number);
879bfdc2
DJ
1689 }
1690 else
1691 {
1692 fprintf_unfiltered (tmp_error_stream,
1693 "Cannot insert breakpoint %d.\n",
35df4500 1694 bl->owner->number);
879bfdc2
DJ
1695 fprintf_filtered (tmp_error_stream,
1696 "Error accessing memory address ");
35df4500 1697 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1698 tmp_error_stream);
879bfdc2
DJ
1699 fprintf_filtered (tmp_error_stream, ": %s.\n",
1700 safe_strerror (val));
1701 }
1702
1703 }
1704 }
1705 else
35df4500 1706 bl->inserted = 1;
879bfdc2
DJ
1707
1708 return val;
1709 }
1710
35df4500 1711 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1712 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1713 watchpoints. It's not clear that it's necessary... */
35df4500 1714 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1715 {
77b06cd7
TJB
1716 gdb_assert (bl->owner->ops != NULL
1717 && bl->owner->ops->insert_location != NULL);
1718
1719 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1720
1721 /* If trying to set a read-watchpoint, and it turns out it's not
1722 supported, try emulating one with an access watchpoint. */
35df4500 1723 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1724 {
1725 struct bp_location *loc, **loc_temp;
1726
1727 /* But don't try to insert it, if there's already another
1728 hw_access location that would be considered a duplicate
1729 of this one. */
1730 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1731 if (loc != bl
85d721b8 1732 && loc->watchpoint_type == hw_access
35df4500 1733 && watchpoint_locations_match (bl, loc))
85d721b8 1734 {
35df4500
TJB
1735 bl->duplicate = 1;
1736 bl->inserted = 1;
1737 bl->target_info = loc->target_info;
1738 bl->watchpoint_type = hw_access;
85d721b8
PA
1739 val = 0;
1740 break;
1741 }
1742
1743 if (val == 1)
1744 {
77b06cd7
TJB
1745 bl->watchpoint_type = hw_access;
1746 val = bl->owner->ops->insert_location (bl);
1747
1748 if (val)
1749 /* Back to the original value. */
1750 bl->watchpoint_type = hw_read;
85d721b8
PA
1751 }
1752 }
1753
35df4500 1754 bl->inserted = (val == 0);
879bfdc2
DJ
1755 }
1756
35df4500 1757 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1758 {
77b06cd7
TJB
1759 gdb_assert (bl->owner->ops != NULL
1760 && bl->owner->ops->insert_location != NULL);
1761
1762 val = bl->owner->ops->insert_location (bl);
1763 if (val)
1764 {
1765 bl->owner->enable_state = bp_disabled;
1766
1767 if (val == 1)
1768 warning (_("\
1769Error inserting catchpoint %d: Your system does not support this type\n\
1770of catchpoint."), bl->owner->number);
1771 else
1772 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1773 }
1774
1775 bl->inserted = (val == 0);
1640b821
DJ
1776
1777 /* We've already printed an error message if there was a problem
1778 inserting this catchpoint, and we've disabled the catchpoint,
1779 so just return success. */
1780 return 0;
879bfdc2
DJ
1781 }
1782
1783 return 0;
1784}
1785
6c95b8df
PA
1786/* This function is called when program space PSPACE is about to be
1787 deleted. It takes care of updating breakpoints to not reference
1788 PSPACE anymore. */
1789
1790void
1791breakpoint_program_space_exit (struct program_space *pspace)
1792{
1793 struct breakpoint *b, *b_temp;
876fa593 1794 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1795
1796 /* Remove any breakpoint that was set through this program space. */
1797 ALL_BREAKPOINTS_SAFE (b, b_temp)
1798 {
1799 if (b->pspace == pspace)
1800 delete_breakpoint (b);
1801 }
1802
1803 /* Breakpoints set through other program spaces could have locations
1804 bound to PSPACE as well. Remove those. */
876fa593 1805 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1806 {
1807 struct bp_location *tmp;
1808
1809 if (loc->pspace == pspace)
1810 {
2bdf28a0 1811 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1812 if (loc->owner->loc == loc)
1813 loc->owner->loc = loc->next;
1814 else
1815 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1816 if (tmp->next == loc)
1817 {
1818 tmp->next = loc->next;
1819 break;
1820 }
1821 }
1822 }
1823
1824 /* Now update the global location list to permanently delete the
1825 removed locations above. */
1826 update_global_location_list (0);
1827}
1828
74960c60
VP
1829/* Make sure all breakpoints are inserted in inferior.
1830 Throws exception on any error.
1831 A breakpoint that is already inserted won't be inserted
1832 again, so calling this function twice is safe. */
1833void
1834insert_breakpoints (void)
1835{
1836 struct breakpoint *bpt;
1837
1838 ALL_BREAKPOINTS (bpt)
1839 if (is_hardware_watchpoint (bpt))
4a64f543 1840 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1841
b60e7edf 1842 update_global_location_list (1);
74960c60 1843
c35b1492
PA
1844 /* update_global_location_list does not insert breakpoints when
1845 always_inserted_mode is not enabled. Explicitly insert them
1846 now. */
1847 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1848 insert_breakpoint_locations ();
1849}
1850
c906108c
SS
1851/* insert_breakpoints is used when starting or continuing the program.
1852 remove_breakpoints is used when the program stops.
1853 Both return zero if successful,
1854 or an `errno' value if could not write the inferior. */
1855
74960c60
VP
1856static void
1857insert_breakpoint_locations (void)
c906108c 1858{
a5606eee 1859 struct breakpoint *bpt;
35df4500 1860 struct bp_location *bl, **blp_tmp;
e236ba44 1861 int error = 0;
c906108c
SS
1862 int val = 0;
1863 int disabled_breaks = 0;
81d0cc19 1864 int hw_breakpoint_error = 0;
c906108c 1865
81d0cc19 1866 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1867 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1868
81d0cc19
GS
1869 /* Explicitly mark the warning -- this will only be printed if
1870 there was an error. */
1871 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1872
1873 save_current_space_and_thread ();
1874
35df4500 1875 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1876 {
35df4500 1877 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1878 continue;
1879
4a64f543
MS
1880 /* There is no point inserting thread-specific breakpoints if
1881 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1882 has BL->OWNER always non-NULL. */
35df4500
TJB
1883 if (bl->owner->thread != -1
1884 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1885 continue;
1886
35df4500 1887 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1888
1889 /* For targets that support global breakpoints, there's no need
1890 to select an inferior to insert breakpoint to. In fact, even
1891 if we aren't attached to any process yet, we should still
1892 insert breakpoints. */
1893 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1894 && ptid_equal (inferior_ptid, null_ptid))
1895 continue;
1896
35df4500 1897 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1898 &hw_breakpoint_error);
1899 if (val)
e236ba44 1900 error = val;
879bfdc2 1901 }
c906108c 1902
4a64f543
MS
1903 /* If we failed to insert all locations of a watchpoint, remove
1904 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1905 ALL_BREAKPOINTS (bpt)
1906 {
1907 int some_failed = 0;
1908 struct bp_location *loc;
1909
1910 if (!is_hardware_watchpoint (bpt))
1911 continue;
1912
d6b74ac4 1913 if (!breakpoint_enabled (bpt))
a5606eee 1914 continue;
74960c60
VP
1915
1916 if (bpt->disposition == disp_del_at_next_stop)
1917 continue;
a5606eee
VP
1918
1919 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1920 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1921 {
1922 some_failed = 1;
1923 break;
1924 }
1925 if (some_failed)
1926 {
1927 for (loc = bpt->loc; loc; loc = loc->next)
1928 if (loc->inserted)
1929 remove_breakpoint (loc, mark_uninserted);
1930
1931 hw_breakpoint_error = 1;
1932 fprintf_unfiltered (tmp_error_stream,
1933 "Could not insert hardware watchpoint %d.\n",
1934 bpt->number);
1935 error = -1;
1936 }
1937 }
1938
e236ba44 1939 if (error)
81d0cc19
GS
1940 {
1941 /* If a hardware breakpoint or watchpoint was inserted, add a
1942 message about possibly exhausted resources. */
879bfdc2 1943 if (hw_breakpoint_error)
81d0cc19 1944 {
c6510018
MS
1945 fprintf_unfiltered (tmp_error_stream,
1946 "Could not insert hardware breakpoints:\n\
1947You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1948 }
81d0cc19
GS
1949 target_terminal_ours_for_output ();
1950 error_stream (tmp_error_stream);
1951 }
f7545552
TT
1952
1953 do_cleanups (cleanups);
c906108c
SS
1954}
1955
c906108c 1956int
fba45db2 1957remove_breakpoints (void)
c906108c 1958{
35df4500 1959 struct bp_location *bl, **blp_tmp;
3a1bae8e 1960 int val = 0;
c906108c 1961
35df4500 1962 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1963 {
35df4500
TJB
1964 if (bl->inserted)
1965 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1966 }
3a1bae8e 1967 return val;
c906108c
SS
1968}
1969
6c95b8df
PA
1970/* Remove breakpoints of process PID. */
1971
1972int
1973remove_breakpoints_pid (int pid)
1974{
35df4500 1975 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1976 int val;
1977 struct inferior *inf = find_inferior_pid (pid);
1978
35df4500 1979 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 1980 {
35df4500 1981 if (bl->pspace != inf->pspace)
6c95b8df
PA
1982 continue;
1983
35df4500 1984 if (bl->inserted)
6c95b8df 1985 {
35df4500 1986 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
1987 if (val != 0)
1988 return val;
1989 }
1990 }
1991 return 0;
1992}
1993
692590c1 1994int
80ce1ecb 1995remove_hw_watchpoints (void)
692590c1 1996{
35df4500 1997 struct bp_location *bl, **blp_tmp;
3a1bae8e 1998 int val = 0;
692590c1 1999
35df4500 2000 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 2001 {
35df4500
TJB
2002 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2003 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 2004 }
3a1bae8e 2005 return val;
692590c1
MS
2006}
2007
c906108c 2008int
fba45db2 2009reattach_breakpoints (int pid)
c906108c 2010{
6c95b8df 2011 struct cleanup *old_chain;
35df4500 2012 struct bp_location *bl, **blp_tmp;
c906108c 2013 int val;
86b887df 2014 struct ui_file *tmp_error_stream;
fa3a767f 2015 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2016 struct inferior *inf;
2017 struct thread_info *tp;
2018
2019 tp = any_live_thread_of_process (pid);
2020 if (tp == NULL)
2021 return 1;
2022
2023 inf = find_inferior_pid (pid);
2024 old_chain = save_inferior_ptid ();
2025
2026 inferior_ptid = tp->ptid;
a4954f26 2027
86b887df 2028 tmp_error_stream = mem_fileopen ();
a4954f26 2029 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2030
35df4500 2031 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2032 {
35df4500 2033 if (bl->pspace != inf->pspace)
6c95b8df
PA
2034 continue;
2035
35df4500 2036 if (bl->inserted)
c5aa993b 2037 {
35df4500
TJB
2038 bl->inserted = 0;
2039 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2040 if (val != 0)
2041 {
ce696e05 2042 do_cleanups (old_chain);
c5aa993b
JM
2043 return val;
2044 }
2045 }
2046 }
ce696e05 2047 do_cleanups (old_chain);
c906108c
SS
2048 return 0;
2049}
2050
e58b0e63
PA
2051static int internal_breakpoint_number = -1;
2052
84f4c1fe
PM
2053/* Set the breakpoint number of B, depending on the value of INTERNAL.
2054 If INTERNAL is non-zero, the breakpoint number will be populated
2055 from internal_breakpoint_number and that variable decremented.
2056 Otherwis the breakpoint number will be populated from
2057 breakpoint_count and that value incremented. Internal breakpoints
2058 do not set the internal var bpnum. */
2059static void
2060set_breakpoint_number (int internal, struct breakpoint *b)
2061{
2062 if (internal)
2063 b->number = internal_breakpoint_number--;
2064 else
2065 {
2066 set_breakpoint_count (breakpoint_count + 1);
2067 b->number = breakpoint_count;
2068 }
2069}
2070
e62c965a 2071static struct breakpoint *
a6d9a66e
UW
2072create_internal_breakpoint (struct gdbarch *gdbarch,
2073 CORE_ADDR address, enum bptype type)
e62c965a 2074{
e62c965a
PP
2075 struct symtab_and_line sal;
2076 struct breakpoint *b;
2077
4a64f543 2078 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2079
2080 sal.pc = address;
2081 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2082 sal.pspace = current_program_space;
e62c965a 2083
a6d9a66e 2084 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2085 b->number = internal_breakpoint_number--;
2086 b->disposition = disp_donttouch;
2087
2088 return b;
2089}
2090
17450429
PP
2091static const char *const longjmp_names[] =
2092 {
2093 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2094 };
2095#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2096
2097/* Per-objfile data private to breakpoint.c. */
2098struct breakpoint_objfile_data
2099{
2100 /* Minimal symbol for "_ovly_debug_event" (if any). */
2101 struct minimal_symbol *overlay_msym;
2102
2103 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2104 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2105
2106 /* Minimal symbol for "std::terminate()" (if any). */
2107 struct minimal_symbol *terminate_msym;
2108
2109 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2110 struct minimal_symbol *exception_msym;
2111};
2112
2113static const struct objfile_data *breakpoint_objfile_key;
2114
2115/* Minimal symbol not found sentinel. */
2116static struct minimal_symbol msym_not_found;
2117
2118/* Returns TRUE if MSYM point to the "not found" sentinel. */
2119
2120static int
2121msym_not_found_p (const struct minimal_symbol *msym)
2122{
2123 return msym == &msym_not_found;
2124}
2125
2126/* Return per-objfile data needed by breakpoint.c.
2127 Allocate the data if necessary. */
2128
2129static struct breakpoint_objfile_data *
2130get_breakpoint_objfile_data (struct objfile *objfile)
2131{
2132 struct breakpoint_objfile_data *bp_objfile_data;
2133
2134 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2135 if (bp_objfile_data == NULL)
2136 {
2137 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2138 sizeof (*bp_objfile_data));
2139
2140 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2141 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2142 }
2143 return bp_objfile_data;
2144}
2145
e62c965a 2146static void
af02033e 2147create_overlay_event_breakpoint (void)
e62c965a 2148{
69de3c6a 2149 struct objfile *objfile;
af02033e 2150 const char *const func_name = "_ovly_debug_event";
e62c965a 2151
69de3c6a
PP
2152 ALL_OBJFILES (objfile)
2153 {
2154 struct breakpoint *b;
17450429
PP
2155 struct breakpoint_objfile_data *bp_objfile_data;
2156 CORE_ADDR addr;
69de3c6a 2157
17450429
PP
2158 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2159
2160 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2161 continue;
2162
2163 if (bp_objfile_data->overlay_msym == NULL)
2164 {
2165 struct minimal_symbol *m;
2166
2167 m = lookup_minimal_symbol_text (func_name, objfile);
2168 if (m == NULL)
2169 {
2170 /* Avoid future lookups in this objfile. */
2171 bp_objfile_data->overlay_msym = &msym_not_found;
2172 continue;
2173 }
2174 bp_objfile_data->overlay_msym = m;
2175 }
e62c965a 2176
17450429
PP
2177 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2178 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2179 bp_overlay_event);
2180 b->addr_string = xstrdup (func_name);
e62c965a 2181
69de3c6a
PP
2182 if (overlay_debugging == ovly_auto)
2183 {
2184 b->enable_state = bp_enabled;
2185 overlay_events_enabled = 1;
2186 }
2187 else
2188 {
2189 b->enable_state = bp_disabled;
2190 overlay_events_enabled = 0;
2191 }
e62c965a
PP
2192 }
2193 update_global_location_list (1);
2194}
2195
0fd8e87f 2196static void
af02033e 2197create_longjmp_master_breakpoint (void)
0fd8e87f 2198{
6c95b8df 2199 struct program_space *pspace;
6c95b8df
PA
2200 struct cleanup *old_chain;
2201
2202 old_chain = save_current_program_space ();
0fd8e87f 2203
6c95b8df 2204 ALL_PSPACES (pspace)
af02033e
PP
2205 {
2206 struct objfile *objfile;
2207
2208 set_current_program_space (pspace);
2209
2210 ALL_OBJFILES (objfile)
0fd8e87f 2211 {
af02033e
PP
2212 int i;
2213 struct gdbarch *gdbarch;
17450429 2214 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2215
af02033e
PP
2216 gdbarch = get_objfile_arch (objfile);
2217 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2218 continue;
2219
17450429
PP
2220 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2221
2222 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2223 {
2224 struct breakpoint *b;
af02033e 2225 const char *func_name;
17450429 2226 CORE_ADDR addr;
6c95b8df 2227
17450429 2228 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2229 continue;
0fd8e87f 2230
17450429
PP
2231 func_name = longjmp_names[i];
2232 if (bp_objfile_data->longjmp_msym[i] == NULL)
2233 {
2234 struct minimal_symbol *m;
2235
2236 m = lookup_minimal_symbol_text (func_name, objfile);
2237 if (m == NULL)
2238 {
2239 /* Prevent future lookups in this objfile. */
2240 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2241 continue;
2242 }
2243 bp_objfile_data->longjmp_msym[i] = m;
2244 }
2245
2246 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2247 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2248 b->addr_string = xstrdup (func_name);
2249 b->enable_state = bp_disabled;
2250 }
0fd8e87f 2251 }
af02033e 2252 }
0fd8e87f 2253 update_global_location_list (1);
6c95b8df
PA
2254
2255 do_cleanups (old_chain);
0fd8e87f
UW
2256}
2257
af02033e 2258/* Create a master std::terminate breakpoint. */
aa7d318d 2259static void
af02033e 2260create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2261{
2262 struct program_space *pspace;
aa7d318d 2263 struct cleanup *old_chain;
af02033e 2264 const char *const func_name = "std::terminate()";
aa7d318d
TT
2265
2266 old_chain = save_current_program_space ();
2267
2268 ALL_PSPACES (pspace)
17450429
PP
2269 {
2270 struct objfile *objfile;
2271 CORE_ADDR addr;
2272
2273 set_current_program_space (pspace);
2274
aa7d318d
TT
2275 ALL_OBJFILES (objfile)
2276 {
2277 struct breakpoint *b;
17450429 2278 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2279
17450429 2280 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2281
17450429
PP
2282 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2283 continue;
2284
2285 if (bp_objfile_data->terminate_msym == NULL)
2286 {
2287 struct minimal_symbol *m;
2288
2289 m = lookup_minimal_symbol (func_name, NULL, objfile);
2290 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2291 && MSYMBOL_TYPE (m) != mst_file_text))
2292 {
2293 /* Prevent future lookups in this objfile. */
2294 bp_objfile_data->terminate_msym = &msym_not_found;
2295 continue;
2296 }
2297 bp_objfile_data->terminate_msym = m;
2298 }
aa7d318d 2299
17450429
PP
2300 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2301 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2302 bp_std_terminate_master);
2303 b->addr_string = xstrdup (func_name);
2304 b->enable_state = bp_disabled;
2305 }
17450429
PP
2306 }
2307
aa7d318d
TT
2308 update_global_location_list (1);
2309
2310 do_cleanups (old_chain);
2311}
2312
186c406b
TT
2313/* Install a master breakpoint on the unwinder's debug hook. */
2314
2315void
2316create_exception_master_breakpoint (void)
2317{
2318 struct objfile *objfile;
17450429 2319 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2320
2321 ALL_OBJFILES (objfile)
2322 {
17450429
PP
2323 struct breakpoint *b;
2324 struct gdbarch *gdbarch;
2325 struct breakpoint_objfile_data *bp_objfile_data;
2326 CORE_ADDR addr;
2327
2328 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2329
2330 if (msym_not_found_p (bp_objfile_data->exception_msym))
2331 continue;
2332
2333 gdbarch = get_objfile_arch (objfile);
186c406b 2334
17450429 2335 if (bp_objfile_data->exception_msym == NULL)
186c406b 2336 {
17450429 2337 struct minimal_symbol *debug_hook;
186c406b 2338
17450429
PP
2339 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2340 if (debug_hook == NULL)
2341 {
2342 bp_objfile_data->exception_msym = &msym_not_found;
2343 continue;
2344 }
2345
2346 bp_objfile_data->exception_msym = debug_hook;
186c406b 2347 }
17450429
PP
2348
2349 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2350 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2351 &current_target);
2352 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2353 b->addr_string = xstrdup (func_name);
2354 b->enable_state = bp_disabled;
186c406b
TT
2355 }
2356
2357 update_global_location_list (1);
2358}
2359
c906108c 2360void
fba45db2 2361update_breakpoints_after_exec (void)
c906108c 2362{
35df4500 2363 struct breakpoint *b, *b_tmp;
876fa593 2364 struct bp_location *bploc, **bplocp_tmp;
c906108c 2365
25b22b0a
PA
2366 /* We're about to delete breakpoints from GDB's lists. If the
2367 INSERTED flag is true, GDB will try to lift the breakpoints by
2368 writing the breakpoints' "shadow contents" back into memory. The
2369 "shadow contents" are NOT valid after an exec, so GDB should not
2370 do that. Instead, the target is responsible from marking
2371 breakpoints out as soon as it detects an exec. We don't do that
2372 here instead, because there may be other attempts to delete
2373 breakpoints after detecting an exec and before reaching here. */
876fa593 2374 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2375 if (bploc->pspace == current_program_space)
2376 gdb_assert (!bploc->inserted);
c906108c 2377
35df4500 2378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2379 {
6c95b8df
PA
2380 if (b->pspace != current_program_space)
2381 continue;
2382
4a64f543 2383 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2384 if (b->type == bp_shlib_event)
2385 {
2386 delete_breakpoint (b);
2387 continue;
2388 }
c906108c 2389
4a64f543 2390 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2391 if (b->type == bp_jit_event)
2392 {
2393 delete_breakpoint (b);
2394 continue;
2395 }
2396
1900040c 2397 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2398 as must overlay event and longjmp master breakpoints. */
2399 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2400 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2401 || b->type == bp_exception_master)
c4093a6a
JM
2402 {
2403 delete_breakpoint (b);
2404 continue;
2405 }
2406
4a64f543 2407 /* Step-resume breakpoints are meaningless after an exec(). */
c5aa993b
JM
2408 if (b->type == bp_step_resume)
2409 {
2410 delete_breakpoint (b);
2411 continue;
2412 }
2413
611c83ae
PA
2414 /* Longjmp and longjmp-resume breakpoints are also meaningless
2415 after an exec. */
186c406b
TT
2416 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2417 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2418 {
2419 delete_breakpoint (b);
2420 continue;
2421 }
2422
ce78b96d
JB
2423 if (b->type == bp_catchpoint)
2424 {
2425 /* For now, none of the bp_catchpoint breakpoints need to
2426 do anything at this point. In the future, if some of
2427 the catchpoints need to something, we will need to add
2428 a new method, and call this method from here. */
2429 continue;
2430 }
2431
c5aa993b
JM
2432 /* bp_finish is a special case. The only way we ought to be able
2433 to see one of these when an exec() has happened, is if the user
2434 caught a vfork, and then said "finish". Ordinarily a finish just
2435 carries them to the call-site of the current callee, by setting
2436 a temporary bp there and resuming. But in this case, the finish
2437 will carry them entirely through the vfork & exec.
2438
2439 We don't want to allow a bp_finish to remain inserted now. But
2440 we can't safely delete it, 'cause finish_command has a handle to
2441 the bp on a bpstat, and will later want to delete it. There's a
2442 chance (and I've seen it happen) that if we delete the bp_finish
2443 here, that its storage will get reused by the time finish_command
2444 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2445 We really must allow finish_command to delete a bp_finish.
2446
53a5351d
JM
2447 In the absense of a general solution for the "how do we know
2448 it's safe to delete something others may have handles to?"
2449 problem, what we'll do here is just uninsert the bp_finish, and
2450 let finish_command delete it.
2451
2452 (We know the bp_finish is "doomed" in the sense that it's
2453 momentary, and will be deleted as soon as finish_command sees
2454 the inferior stopped. So it doesn't matter that the bp's
2455 address is probably bogus in the new a.out, unlike e.g., the
2456 solib breakpoints.) */
c5aa993b 2457
c5aa993b
JM
2458 if (b->type == bp_finish)
2459 {
2460 continue;
2461 }
2462
2463 /* Without a symbolic address, we have little hope of the
2464 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2465 a.out. */
c5aa993b
JM
2466 if (b->addr_string == NULL)
2467 {
2468 delete_breakpoint (b);
2469 continue;
2470 }
c5aa993b 2471 }
1900040c 2472 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2473 create_overlay_event_breakpoint ();
2474 create_longjmp_master_breakpoint ();
2475 create_std_terminate_master_breakpoint ();
186c406b 2476 create_exception_master_breakpoint ();
c906108c
SS
2477}
2478
2479int
fba45db2 2480detach_breakpoints (int pid)
c906108c 2481{
35df4500 2482 struct bp_location *bl, **blp_tmp;
3a1bae8e 2483 int val = 0;
ce696e05 2484 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2485 struct inferior *inf = current_inferior ();
c5aa993b 2486
39f77062 2487 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2488 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2489
6c95b8df 2490 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2491 inferior_ptid = pid_to_ptid (pid);
35df4500 2492 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2493 {
35df4500 2494 if (bl->pspace != inf->pspace)
6c95b8df
PA
2495 continue;
2496
35df4500
TJB
2497 if (bl->inserted)
2498 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2499 }
d03285ec
UW
2500
2501 /* Detach single-step breakpoints as well. */
2502 detach_single_step_breakpoints ();
2503
ce696e05 2504 do_cleanups (old_chain);
3a1bae8e 2505 return val;
c906108c
SS
2506}
2507
35df4500 2508/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2509 Note that this is used to detach breakpoints from a child fork.
2510 When we get here, the child isn't in the inferior list, and neither
2511 do we have objects to represent its address space --- we should
35df4500 2512 *not* look at bl->pspace->aspace here. */
6c95b8df 2513
c906108c 2514static int
35df4500 2515remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2516{
2517 int val;
c5aa993b 2518
35df4500
TJB
2519 /* BL is never in moribund_locations by our callers. */
2520 gdb_assert (bl->owner != NULL);
2bdf28a0 2521
35df4500 2522 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2523 /* Permanent breakpoints cannot be inserted or removed. */
2524 return 0;
2525
74960c60
VP
2526 /* The type of none suggests that owner is actually deleted.
2527 This should not ever happen. */
35df4500 2528 gdb_assert (bl->owner->type != bp_none);
0bde7532 2529
35df4500
TJB
2530 if (bl->loc_type == bp_loc_software_breakpoint
2531 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2532 {
c02f5703
MS
2533 /* "Normal" instruction breakpoint: either the standard
2534 trap-instruction bp (bp_breakpoint), or a
2535 bp_hardware_breakpoint. */
2536
2537 /* First check to see if we have to handle an overlay. */
2538 if (overlay_debugging == ovly_off
35df4500
TJB
2539 || bl->section == NULL
2540 || !(section_is_overlay (bl->section)))
c02f5703
MS
2541 {
2542 /* No overlay handling: just remove the breakpoint. */
2543
35df4500
TJB
2544 if (bl->loc_type == bp_loc_hardware_breakpoint)
2545 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2546 else
35df4500 2547 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2548 }
c906108c
SS
2549 else
2550 {
4a64f543 2551 /* This breakpoint is in an overlay section.
c02f5703
MS
2552 Did we set a breakpoint at the LMA? */
2553 if (!overlay_events_enabled)
2554 {
2555 /* Yes -- overlay event support is not active, so we
2556 should have set a breakpoint at the LMA. Remove it.
2557 */
c02f5703
MS
2558 /* Ignore any failures: if the LMA is in ROM, we will
2559 have already warned when we failed to insert it. */
35df4500
TJB
2560 if (bl->loc_type == bp_loc_hardware_breakpoint)
2561 target_remove_hw_breakpoint (bl->gdbarch,
2562 &bl->overlay_target_info);
c02f5703 2563 else
35df4500
TJB
2564 target_remove_breakpoint (bl->gdbarch,
2565 &bl->overlay_target_info);
c02f5703
MS
2566 }
2567 /* Did we set a breakpoint at the VMA?
2568 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2569 if (bl->inserted)
c906108c 2570 {
c02f5703
MS
2571 /* Yes -- remove it. Previously we did not bother to
2572 remove the breakpoint if the section had been
2573 unmapped, but let's not rely on that being safe. We
2574 don't know what the overlay manager might do. */
35df4500
TJB
2575 if (bl->loc_type == bp_loc_hardware_breakpoint)
2576 val = target_remove_hw_breakpoint (bl->gdbarch,
2577 &bl->target_info);
aa67235e
UW
2578
2579 /* However, we should remove *software* breakpoints only
2580 if the section is still mapped, or else we overwrite
2581 wrong code with the saved shadow contents. */
35df4500
TJB
2582 else if (section_is_mapped (bl->section))
2583 val = target_remove_breakpoint (bl->gdbarch,
2584 &bl->target_info);
aa67235e
UW
2585 else
2586 val = 0;
c906108c 2587 }
c02f5703
MS
2588 else
2589 {
2590 /* No -- not inserted, so no need to remove. No error. */
2591 val = 0;
2592 }
c906108c 2593 }
879d1e6b
UW
2594
2595 /* In some cases, we might not be able to remove a breakpoint
2596 in a shared library that has already been removed, but we
2597 have not yet processed the shlib unload event. */
35df4500 2598 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2599 val = 0;
2600
c906108c
SS
2601 if (val)
2602 return val;
35df4500 2603 bl->inserted = (is == mark_inserted);
c906108c 2604 }
35df4500 2605 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2606 {
77b06cd7
TJB
2607 gdb_assert (bl->owner->ops != NULL
2608 && bl->owner->ops->remove_location != NULL);
2609
35df4500 2610 bl->inserted = (is == mark_inserted);
77b06cd7 2611 bl->owner->ops->remove_location (bl);
2e70b7b9 2612
c906108c 2613 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2614 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2615 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2616 bl->owner->number);
c906108c 2617 }
35df4500
TJB
2618 else if (bl->owner->type == bp_catchpoint
2619 && breakpoint_enabled (bl->owner)
2620 && !bl->duplicate)
ce78b96d 2621 {
77b06cd7
TJB
2622 gdb_assert (bl->owner->ops != NULL
2623 && bl->owner->ops->remove_location != NULL);
ce78b96d 2624
77b06cd7 2625 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2626 if (val)
2627 return val;
77b06cd7 2628
35df4500 2629 bl->inserted = (is == mark_inserted);
ce78b96d 2630 }
c906108c
SS
2631
2632 return 0;
2633}
2634
6c95b8df 2635static int
35df4500 2636remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2637{
2638 int ret;
2639 struct cleanup *old_chain;
2640
35df4500
TJB
2641 /* BL is never in moribund_locations by our callers. */
2642 gdb_assert (bl->owner != NULL);
2bdf28a0 2643
35df4500 2644 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2645 /* Permanent breakpoints cannot be inserted or removed. */
2646 return 0;
2647
2648 /* The type of none suggests that owner is actually deleted.
2649 This should not ever happen. */
35df4500 2650 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2651
2652 old_chain = save_current_space_and_thread ();
2653
35df4500 2654 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2655
35df4500 2656 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2657
2658 do_cleanups (old_chain);
2659 return ret;
2660}
2661
c906108c
SS
2662/* Clear the "inserted" flag in all breakpoints. */
2663
25b22b0a 2664void
fba45db2 2665mark_breakpoints_out (void)
c906108c 2666{
35df4500 2667 struct bp_location *bl, **blp_tmp;
c906108c 2668
35df4500
TJB
2669 ALL_BP_LOCATIONS (bl, blp_tmp)
2670 if (bl->pspace == current_program_space)
2671 bl->inserted = 0;
c906108c
SS
2672}
2673
53a5351d
JM
2674/* Clear the "inserted" flag in all breakpoints and delete any
2675 breakpoints which should go away between runs of the program.
c906108c
SS
2676
2677 Plus other such housekeeping that has to be done for breakpoints
2678 between runs.
2679
53a5351d
JM
2680 Note: this function gets called at the end of a run (by
2681 generic_mourn_inferior) and when a run begins (by
4a64f543 2682 init_wait_for_inferior). */
c906108c
SS
2683
2684
2685
2686void
fba45db2 2687breakpoint_init_inferior (enum inf_context context)
c906108c 2688{
35df4500
TJB
2689 struct breakpoint *b, *b_tmp;
2690 struct bp_location *bl, **blp_tmp;
1c5cfe86 2691 int ix;
6c95b8df 2692 struct program_space *pspace = current_program_space;
c906108c 2693
50c71eaf
PA
2694 /* If breakpoint locations are shared across processes, then there's
2695 nothing to do. */
2567c7d9 2696 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2697 return;
2698
35df4500 2699 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2700 {
35df4500
TJB
2701 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2702 if (bl->pspace == pspace
2703 && bl->owner->enable_state != bp_permanent)
2704 bl->inserted = 0;
6c95b8df 2705 }
075f6582 2706
35df4500 2707 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2708 {
6c95b8df
PA
2709 if (b->loc && b->loc->pspace != pspace)
2710 continue;
2711
c5aa993b
JM
2712 switch (b->type)
2713 {
2714 case bp_call_dummy:
c906108c 2715
c5aa993b 2716 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2717 cause problems when the inferior is rerun, so we better get
2718 rid of it. */
2719
2720 case bp_watchpoint_scope:
2721
2722 /* Also get rid of scope breakpoints. */
2723
2724 case bp_shlib_event:
2725
2726 /* Also remove solib event breakpoints. Their addresses may
2727 have changed since the last time we ran the program.
2728 Actually we may now be debugging against different target;
2729 and so the solib backend that installed this breakpoint may
2730 not be used in by the target. E.g.,
2731
2732 (gdb) file prog-linux
2733 (gdb) run # native linux target
2734 ...
2735 (gdb) kill
2736 (gdb) file prog-win.exe
2737 (gdb) tar rem :9999 # remote Windows gdbserver.
2738 */
c906108c 2739
c5aa993b
JM
2740 delete_breakpoint (b);
2741 break;
c906108c 2742
c5aa993b
JM
2743 case bp_watchpoint:
2744 case bp_hardware_watchpoint:
2745 case bp_read_watchpoint:
2746 case bp_access_watchpoint:
c906108c 2747
c5aa993b
JM
2748 /* Likewise for watchpoints on local expressions. */
2749 if (b->exp_valid_block != NULL)
2750 delete_breakpoint (b);
967af18d 2751 else if (context == inf_starting)
c860120c 2752 {
4a64f543
MS
2753 /* Reset val field to force reread of starting value in
2754 insert_breakpoints. */
c860120c
PM
2755 if (b->val)
2756 value_free (b->val);
2757 b->val = NULL;
fa4727a6 2758 b->val_valid = 0;
c860120c 2759 }
c5aa993b
JM
2760 break;
2761 default:
c5aa993b
JM
2762 break;
2763 }
2764 }
1c5cfe86
PA
2765
2766 /* Get rid of the moribund locations. */
35df4500
TJB
2767 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2768 decref_bp_location (&bl);
1c5cfe86 2769 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2770}
2771
6c95b8df
PA
2772/* These functions concern about actual breakpoints inserted in the
2773 target --- to e.g. check if we need to do decr_pc adjustment or if
2774 we need to hop over the bkpt --- so we check for address space
2775 match, not program space. */
2776
c2c6d25f
JM
2777/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2778 exists at PC. It returns ordinary_breakpoint_here if it's an
2779 ordinary breakpoint, or permanent_breakpoint_here if it's a
2780 permanent breakpoint.
2781 - When continuing from a location with an ordinary breakpoint, we
2782 actually single step once before calling insert_breakpoints.
2783 - When continuing from a localion with a permanent breakpoint, we
2784 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2785 the target, to advance the PC past the breakpoint. */
c906108c 2786
c2c6d25f 2787enum breakpoint_here
6c95b8df 2788breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2789{
35df4500 2790 struct bp_location *bl, **blp_tmp;
c2c6d25f 2791 int any_breakpoint_here = 0;
c906108c 2792
35df4500 2793 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2794 {
35df4500
TJB
2795 if (bl->loc_type != bp_loc_software_breakpoint
2796 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2797 continue;
2798
f1310107 2799 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2800 if ((breakpoint_enabled (bl->owner)
2801 || bl->owner->enable_state == bp_permanent)
f1310107 2802 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2803 {
2804 if (overlay_debugging
35df4500
TJB
2805 && section_is_overlay (bl->section)
2806 && !section_is_mapped (bl->section))
075f6582 2807 continue; /* unmapped overlay -- can't be a match */
35df4500 2808 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2809 return permanent_breakpoint_here;
2810 else
2811 any_breakpoint_here = 1;
2812 }
2813 }
c906108c 2814
c2c6d25f 2815 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2816}
2817
1c5cfe86
PA
2818/* Return true if there's a moribund breakpoint at PC. */
2819
2820int
6c95b8df 2821moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2822{
2823 struct bp_location *loc;
2824 int ix;
2825
2826 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2827 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2828 return 1;
2829
2830 return 0;
2831}
c2c6d25f 2832
c36b740a 2833/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2834 inserted using regular breakpoint_chain / bp_location array
2835 mechanism. This does not check for single-step breakpoints, which
2836 are inserted and removed using direct target manipulation. */
c906108c
SS
2837
2838int
4a64f543
MS
2839regular_breakpoint_inserted_here_p (struct address_space *aspace,
2840 CORE_ADDR pc)
c906108c 2841{
35df4500 2842 struct bp_location *bl, **blp_tmp;
c906108c 2843
35df4500 2844 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2845 {
35df4500
TJB
2846 if (bl->loc_type != bp_loc_software_breakpoint
2847 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2848 continue;
2849
35df4500 2850 if (bl->inserted
f1310107 2851 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2852 {
2853 if (overlay_debugging
35df4500
TJB
2854 && section_is_overlay (bl->section)
2855 && !section_is_mapped (bl->section))
075f6582
DJ
2856 continue; /* unmapped overlay -- can't be a match */
2857 else
2858 return 1;
2859 }
c5aa993b 2860 }
c36b740a
VP
2861 return 0;
2862}
2863
2864/* Returns non-zero iff there's either regular breakpoint
2865 or a single step breakpoint inserted at PC. */
2866
2867int
6c95b8df 2868breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2869{
6c95b8df 2870 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2871 return 1;
c906108c 2872
6c95b8df 2873 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2874 return 1;
2875
c906108c
SS
2876 return 0;
2877}
2878
4fa8626c
DJ
2879/* This function returns non-zero iff there is a software breakpoint
2880 inserted at PC. */
2881
2882int
3e43a32a
MS
2883software_breakpoint_inserted_here_p (struct address_space *aspace,
2884 CORE_ADDR pc)
4fa8626c 2885{
35df4500 2886 struct bp_location *bl, **blp_tmp;
4fa8626c 2887
35df4500 2888 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2889 {
35df4500 2890 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2891 continue;
2892
35df4500
TJB
2893 if (bl->inserted
2894 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2895 aspace, pc))
4fa8626c
DJ
2896 {
2897 if (overlay_debugging
35df4500
TJB
2898 && section_is_overlay (bl->section)
2899 && !section_is_mapped (bl->section))
4fa8626c
DJ
2900 continue; /* unmapped overlay -- can't be a match */
2901 else
2902 return 1;
2903 }
2904 }
2905
1aafd4da 2906 /* Also check for software single-step breakpoints. */
6c95b8df 2907 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2908 return 1;
2909
4fa8626c
DJ
2910 return 0;
2911}
2912
9093389c
PA
2913int
2914hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2915 CORE_ADDR addr, ULONGEST len)
2916{
2917 struct breakpoint *bpt;
2918
2919 ALL_BREAKPOINTS (bpt)
2920 {
2921 struct bp_location *loc;
2922
2923 if (bpt->type != bp_hardware_watchpoint
2924 && bpt->type != bp_access_watchpoint)
2925 continue;
2926
2927 if (!breakpoint_enabled (bpt))
2928 continue;
2929
2930 for (loc = bpt->loc; loc; loc = loc->next)
2931 if (loc->pspace->aspace == aspace && loc->inserted)
2932 {
2933 CORE_ADDR l, h;
2934
2935 /* Check for intersection. */
2936 l = max (loc->address, addr);
2937 h = min (loc->address + loc->length, addr + len);
2938 if (l < h)
2939 return 1;
2940 }
2941 }
2942 return 0;
2943}
2944
075f6582
DJ
2945/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2946 PC is valid for process/thread PTID. */
c906108c
SS
2947
2948int
6c95b8df
PA
2949breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2950 ptid_t ptid)
c906108c 2951{
35df4500 2952 struct bp_location *bl, **blp_tmp;
4a306c9a 2953 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2954 int thread = -1;
4a306c9a 2955 int task = 0;
a6f1cd96 2956
35df4500 2957 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2958 {
35df4500
TJB
2959 if (bl->loc_type != bp_loc_software_breakpoint
2960 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2961 continue;
2962
35df4500
TJB
2963 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2964 if (!breakpoint_enabled (bl->owner)
2965 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2966 continue;
2967
f1310107 2968 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
2969 continue;
2970
35df4500 2971 if (bl->owner->thread != -1)
075f6582 2972 {
a6f1cd96
JB
2973 /* This is a thread-specific breakpoint. Check that ptid
2974 matches that thread. If thread hasn't been computed yet,
2975 it is now time to do so. */
2976 if (thread == -1)
2977 thread = pid_to_thread_id (ptid);
35df4500 2978 if (bl->owner->thread != thread)
a6f1cd96 2979 continue;
075f6582 2980 }
a6f1cd96 2981
35df4500 2982 if (bl->owner->task != 0)
4a306c9a
JB
2983 {
2984 /* This is a task-specific breakpoint. Check that ptid
2985 matches that task. If task hasn't been computed yet,
2986 it is now time to do so. */
2987 if (task == 0)
2988 task = ada_get_task_number (ptid);
35df4500 2989 if (bl->owner->task != task)
4a306c9a
JB
2990 continue;
2991 }
2992
a6f1cd96 2993 if (overlay_debugging
35df4500
TJB
2994 && section_is_overlay (bl->section)
2995 && !section_is_mapped (bl->section))
a6f1cd96
JB
2996 continue; /* unmapped overlay -- can't be a match */
2997
2998 return 1;
c5aa993b 2999 }
c906108c
SS
3000
3001 return 0;
3002}
c906108c 3003\f
c5aa993b 3004
c906108c
SS
3005/* bpstat stuff. External routines' interfaces are documented
3006 in breakpoint.h. */
3007
3008int
fba45db2 3009ep_is_catchpoint (struct breakpoint *ep)
c906108c 3010{
533be4dd 3011 return (ep->type == bp_catchpoint);
c906108c
SS
3012}
3013
f431efe5
PA
3014/* Frees any storage that is part of a bpstat. Does not walk the
3015 'next' chain. */
3016
3017static void
198757a8
VP
3018bpstat_free (bpstat bs)
3019{
3020 if (bs->old_val != NULL)
3021 value_free (bs->old_val);
9add0f1b 3022 decref_counted_command_line (&bs->commands);
f431efe5 3023 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3024 xfree (bs);
3025}
3026
c906108c
SS
3027/* Clear a bpstat so that it says we are not at any breakpoint.
3028 Also free any storage that is part of a bpstat. */
3029
3030void
fba45db2 3031bpstat_clear (bpstat *bsp)
c906108c
SS
3032{
3033 bpstat p;
3034 bpstat q;
3035
3036 if (bsp == 0)
3037 return;
3038 p = *bsp;
3039 while (p != NULL)
3040 {
3041 q = p->next;
198757a8 3042 bpstat_free (p);
c906108c
SS
3043 p = q;
3044 }
3045 *bsp = NULL;
3046}
3047
3048/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3049 is part of the bpstat is copied as well. */
3050
3051bpstat
fba45db2 3052bpstat_copy (bpstat bs)
c906108c
SS
3053{
3054 bpstat p = NULL;
3055 bpstat tmp;
3056 bpstat retval = NULL;
3057
3058 if (bs == NULL)
3059 return bs;
3060
3061 for (; bs != NULL; bs = bs->next)
3062 {
3063 tmp = (bpstat) xmalloc (sizeof (*tmp));
3064 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3065 incref_counted_command_line (tmp->commands);
f431efe5 3066 incref_bp_location (tmp->bp_location_at);
31cc81e9 3067 if (bs->old_val != NULL)
3c3185ac
JK
3068 {
3069 tmp->old_val = value_copy (bs->old_val);
3070 release_value (tmp->old_val);
3071 }
31cc81e9 3072
c906108c
SS
3073 if (p == NULL)
3074 /* This is the first thing in the chain. */
3075 retval = tmp;
3076 else
3077 p->next = tmp;
3078 p = tmp;
3079 }
3080 p->next = NULL;
3081 return retval;
3082}
3083
4a64f543 3084/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3085
3086bpstat
fba45db2 3087bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3088{
c5aa993b
JM
3089 if (bsp == NULL)
3090 return NULL;
c906108c 3091
c5aa993b
JM
3092 for (; bsp != NULL; bsp = bsp->next)
3093 {
f431efe5 3094 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3095 return bsp;
3096 }
c906108c
SS
3097 return NULL;
3098}
3099
4a64f543
MS
3100/* Put in *NUM the breakpoint number of the first breakpoint we are
3101 stopped at. *BSP upon return is a bpstat which points to the
3102 remaining breakpoints stopped at (but which is not guaranteed to be
3103 good for anything but further calls to bpstat_num).
3104
8671a17b
PA
3105 Return 0 if passed a bpstat which does not indicate any breakpoints.
3106 Return -1 if stopped at a breakpoint that has been deleted since
3107 we set it.
3108 Return 1 otherwise. */
c906108c
SS
3109
3110int
8671a17b 3111bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3112{
3113 struct breakpoint *b;
3114
3115 if ((*bsp) == NULL)
3116 return 0; /* No more breakpoint values */
8671a17b 3117
4a64f543
MS
3118 /* We assume we'll never have several bpstats that correspond to a
3119 single breakpoint -- otherwise, this function might return the
3120 same number more than once and this will look ugly. */
f431efe5 3121 b = (*bsp)->breakpoint_at;
8671a17b
PA
3122 *bsp = (*bsp)->next;
3123 if (b == NULL)
3124 return -1; /* breakpoint that's been deleted since */
3125
3126 *num = b->number; /* We have its number */
3127 return 1;
c906108c
SS
3128}
3129
3130/* Modify BS so that the actions will not be performed. */
3131
3132void
fba45db2 3133bpstat_clear_actions (bpstat bs)
c906108c
SS
3134{
3135 for (; bs != NULL; bs = bs->next)
3136 {
9add0f1b 3137 decref_counted_command_line (&bs->commands);
dde2d684 3138 bs->commands_left = NULL;
c906108c
SS
3139 if (bs->old_val != NULL)
3140 {
3141 value_free (bs->old_val);
3142 bs->old_val = NULL;
3143 }
3144 }
3145}
3146
f3b1572e
PA
3147/* Called when a command is about to proceed the inferior. */
3148
3149static void
3150breakpoint_about_to_proceed (void)
3151{
3152 if (!ptid_equal (inferior_ptid, null_ptid))
3153 {
3154 struct thread_info *tp = inferior_thread ();
3155
3156 /* Allow inferior function calls in breakpoint commands to not
3157 interrupt the command list. When the call finishes
3158 successfully, the inferior will be standing at the same
3159 breakpoint as if nothing happened. */
16c381f0 3160 if (tp->control.in_infcall)
f3b1572e
PA
3161 return;
3162 }
3163
3164 breakpoint_proceeded = 1;
3165}
3166
4a64f543
MS
3167/* Stub for cleaning up our state if we error-out of a breakpoint
3168 command. */
c906108c 3169static void
4efb68b1 3170cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3171{
3172 executing_breakpoint_commands = 0;
3173}
3174
4a64f543
MS
3175/* Execute all the commands associated with all the breakpoints at
3176 this location. Any of these commands could cause the process to
3177 proceed beyond this point, etc. We look out for such changes by
3178 checking the global "breakpoint_proceeded" after each command.
c906108c 3179
347bddb7
PA
3180 Returns true if a breakpoint command resumed the inferior. In that
3181 case, it is the caller's responsibility to recall it again with the
3182 bpstat of the current thread. */
3183
3184static int
3185bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3186{
3187 bpstat bs;
3188 struct cleanup *old_chain;
347bddb7 3189 int again = 0;
c906108c
SS
3190
3191 /* Avoid endless recursion if a `source' command is contained
3192 in bs->commands. */
3193 if (executing_breakpoint_commands)
347bddb7 3194 return 0;
c906108c
SS
3195
3196 executing_breakpoint_commands = 1;
3197 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3198
4a64f543 3199 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3200 bs = *bsp;
3201
3202 breakpoint_proceeded = 0;
3203 for (; bs != NULL; bs = bs->next)
3204 {
9add0f1b 3205 struct counted_command_line *ccmd;
6c50ab1c
JB
3206 struct command_line *cmd;
3207 struct cleanup *this_cmd_tree_chain;
3208
3209 /* Take ownership of the BSP's command tree, if it has one.
3210
3211 The command tree could legitimately contain commands like
3212 'step' and 'next', which call clear_proceed_status, which
3213 frees stop_bpstat's command tree. To make sure this doesn't
3214 free the tree we're executing out from under us, we need to
3215 take ownership of the tree ourselves. Since a given bpstat's
3216 commands are only executed once, we don't need to copy it; we
3217 can clear the pointer in the bpstat, and make sure we free
3218 the tree when we're done. */
9add0f1b
TT
3219 ccmd = bs->commands;
3220 bs->commands = NULL;
3221 this_cmd_tree_chain
3222 = make_cleanup_decref_counted_command_line (&ccmd);
3223 cmd = bs->commands_left;
3224 bs->commands_left = NULL;
6c50ab1c 3225
c906108c
SS
3226 while (cmd != NULL)
3227 {
3228 execute_control_command (cmd);
3229
3230 if (breakpoint_proceeded)
3231 break;
3232 else
3233 cmd = cmd->next;
3234 }
6c50ab1c
JB
3235
3236 /* We can free this command tree now. */
3237 do_cleanups (this_cmd_tree_chain);
3238
c906108c 3239 if (breakpoint_proceeded)
32c1e744
VP
3240 {
3241 if (target_can_async_p ())
347bddb7
PA
3242 /* If we are in async mode, then the target might be still
3243 running, not stopped at any breakpoint, so nothing for
3244 us to do here -- just return to the event loop. */
3245 ;
32c1e744
VP
3246 else
3247 /* In sync mode, when execute_control_command returns
3248 we're already standing on the next breakpoint.
347bddb7
PA
3249 Breakpoint commands for that stop were not run, since
3250 execute_command does not run breakpoint commands --
3251 only command_line_handler does, but that one is not
3252 involved in execution of breakpoint commands. So, we
3253 can now execute breakpoint commands. It should be
3254 noted that making execute_command do bpstat actions is
3255 not an option -- in this case we'll have recursive
3256 invocation of bpstat for each breakpoint with a
3257 command, and can easily blow up GDB stack. Instead, we
3258 return true, which will trigger the caller to recall us
3259 with the new stop_bpstat. */
3260 again = 1;
3261 break;
32c1e744 3262 }
c906108c 3263 }
c2b8ed2c 3264 do_cleanups (old_chain);
347bddb7
PA
3265 return again;
3266}
3267
3268void
3269bpstat_do_actions (void)
3270{
3271 /* Do any commands attached to breakpoint we are stopped at. */
3272 while (!ptid_equal (inferior_ptid, null_ptid)
3273 && target_has_execution
3274 && !is_exited (inferior_ptid)
3275 && !is_executing (inferior_ptid))
3276 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3277 and only return when it is stopped at the next breakpoint, we
3278 keep doing breakpoint actions until it returns false to
3279 indicate the inferior was not resumed. */
16c381f0 3280 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3281 break;
c906108c
SS
3282}
3283
fa4727a6
DJ
3284/* Print out the (old or new) value associated with a watchpoint. */
3285
3286static void
3287watchpoint_value_print (struct value *val, struct ui_file *stream)
3288{
3289 if (val == NULL)
3290 fprintf_unfiltered (stream, _("<unreadable>"));
3291 else
79a45b7d
TT
3292 {
3293 struct value_print_options opts;
3294 get_user_print_options (&opts);
3295 value_print (val, stream, &opts);
3296 }
fa4727a6
DJ
3297}
3298
e514a9d6 3299/* This is the normal print function for a bpstat. In the future,
c906108c 3300 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3301 by having it set different print_it values.
3302
3303 Current scheme: When we stop, bpstat_print() is called. It loops
3304 through the bpstat list of things causing this stop, calling the
4a64f543 3305 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3306 print_bp_stop_message function depends on the print_it field of
4a64f543 3307 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3308
3309 Return values from this routine (ultimately used by bpstat_print()
3310 and normal_stop() to decide what to do):
3311 PRINT_NOTHING: Means we already printed all we needed to print,
3312 don't print anything else.
3313 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3314 that something to be followed by a location.
3315 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3316 that something to be followed by a location.
3317 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3318 analysis. */
c906108c 3319
917317f4 3320static enum print_stop_action
fba45db2 3321print_it_typical (bpstat bs)
c906108c 3322{
f7545552 3323 struct cleanup *old_chain;
4f8d1dc6 3324 struct breakpoint *b;
89f9893c 3325 const struct bp_location *bl;
8b93c638 3326 struct ui_stream *stb;
f7545552
TT
3327 int bp_temp = 0;
3328 enum print_stop_action result;
3329
f431efe5
PA
3330 gdb_assert (bs->bp_location_at != NULL);
3331
3332 bl = bs->bp_location_at;
3333 b = bs->breakpoint_at;
c906108c 3334
f7545552
TT
3335 stb = ui_out_stream_new (uiout);
3336 old_chain = make_cleanup_ui_out_stream_delete (stb);
3337
4f8d1dc6 3338 switch (b->type)
c906108c 3339 {
e514a9d6
JM
3340 case bp_breakpoint:
3341 case bp_hardware_breakpoint:
f431efe5 3342 bp_temp = b->disposition == disp_del;
0d381245
VP
3343 if (bl->address != bl->requested_address)
3344 breakpoint_adjustment_warning (bl->requested_address,
3345 bl->address,
4f8d1dc6
VP
3346 b->number, 1);
3347 annotate_breakpoint (b->number);
2cec12e5
AR
3348 if (bp_temp)
3349 ui_out_text (uiout, "\nTemporary breakpoint ");
3350 else
3351 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3352 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3353 {
3354 ui_out_field_string (uiout, "reason",
3355 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3356 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3357 }
4f8d1dc6 3358 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3359 ui_out_text (uiout, ", ");
f7545552 3360 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3361 break;
3362
3363 case bp_shlib_event:
917317f4
JM
3364 /* Did we stop because the user set the stop_on_solib_events
3365 variable? (If so, we report this as a generic, "Stopped due
3366 to shlib event" message.) */
a3f17187 3367 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3368 result = PRINT_NOTHING;
e514a9d6
JM
3369 break;
3370
c4093a6a 3371 case bp_thread_event:
4a64f543 3372 /* Not sure how we will get here.
c4093a6a 3373 GDB should not stop for these breakpoints. */
a3f17187 3374 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3375 result = PRINT_NOTHING;
c4093a6a
JM
3376 break;
3377
1900040c 3378 case bp_overlay_event:
4a64f543 3379 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3380 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3381 result = PRINT_NOTHING;
1900040c
MS
3382 break;
3383
0fd8e87f
UW
3384 case bp_longjmp_master:
3385 /* These should never be enabled. */
3386 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3387 result = PRINT_NOTHING;
3388 break;
3389
aa7d318d
TT
3390 case bp_std_terminate_master:
3391 /* These should never be enabled. */
3e43a32a
MS
3392 printf_filtered (_("std::terminate Master Breakpoint: "
3393 "gdb should not stop!\n"));
aa7d318d
TT
3394 result = PRINT_NOTHING;
3395 break;
3396
186c406b
TT
3397 case bp_exception_master:
3398 /* These should never be enabled. */
3e43a32a
MS
3399 printf_filtered (_("Exception Master Breakpoint: "
3400 "gdb should not stop!\n"));
186c406b
TT
3401 result = PRINT_NOTHING;
3402 break;
3403
e514a9d6
JM
3404 case bp_watchpoint:
3405 case bp_hardware_watchpoint:
fa4727a6
DJ
3406 annotate_watchpoint (b->number);
3407 if (ui_out_is_mi_like_p (uiout))
3408 ui_out_field_string
3409 (uiout, "reason",
3410 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3411 mention (b);
f7545552 3412 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3413 ui_out_text (uiout, "\nOld value = ");
3414 watchpoint_value_print (bs->old_val, stb->stream);
3415 ui_out_field_stream (uiout, "old", stb);
3416 ui_out_text (uiout, "\nNew value = ");
3417 watchpoint_value_print (b->val, stb->stream);
3418 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3419 ui_out_text (uiout, "\n");
e514a9d6 3420 /* More than one watchpoint may have been triggered. */
f7545552 3421 result = PRINT_UNKNOWN;
e514a9d6
JM
3422 break;
3423
3424 case bp_read_watchpoint:
9dc5e2a9 3425 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3426 ui_out_field_string
3427 (uiout, "reason",
3428 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3429 mention (b);
f7545552 3430 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3431 ui_out_text (uiout, "\nValue = ");
fa4727a6 3432 watchpoint_value_print (b->val, stb->stream);
8b93c638 3433 ui_out_field_stream (uiout, "value", stb);
8b93c638 3434 ui_out_text (uiout, "\n");
f7545552 3435 result = PRINT_UNKNOWN;
e514a9d6
JM
3436 break;
3437
3438 case bp_access_watchpoint:
fa4727a6 3439 if (bs->old_val != NULL)
8b93c638 3440 {
4f8d1dc6 3441 annotate_watchpoint (b->number);
9dc5e2a9 3442 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3443 ui_out_field_string
3444 (uiout, "reason",
3445 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3446 mention (b);
f7545552 3447 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3448 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3449 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3450 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3451 ui_out_text (uiout, "\nNew value = ");
3452 }
3453 else
3454 {
4f8d1dc6 3455 mention (b);
9dc5e2a9 3456 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3457 ui_out_field_string
3458 (uiout, "reason",
3459 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3460 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3461 ui_out_text (uiout, "\nValue = ");
3462 }
fa4727a6 3463 watchpoint_value_print (b->val, stb->stream);
8b93c638 3464 ui_out_field_stream (uiout, "new", stb);
8b93c638 3465 ui_out_text (uiout, "\n");
f7545552 3466 result = PRINT_UNKNOWN;
e514a9d6 3467 break;
4ce44c66 3468
e514a9d6 3469 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3470 here. */
e514a9d6 3471
11cf8741 3472 case bp_finish:
9dc5e2a9 3473 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3474 ui_out_field_string
3475 (uiout, "reason",
3476 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3477 result = PRINT_UNKNOWN;
8b93c638
JM
3478 break;
3479
e514a9d6 3480 case bp_until:
9dc5e2a9 3481 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3482 ui_out_field_string
3483 (uiout, "reason",
3484 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3485 result = PRINT_UNKNOWN;
8b93c638
JM
3486 break;
3487
c2d11a7d 3488 case bp_none:
e514a9d6
JM
3489 case bp_longjmp:
3490 case bp_longjmp_resume:
186c406b
TT
3491 case bp_exception:
3492 case bp_exception_resume:
e514a9d6 3493 case bp_step_resume:
e514a9d6
JM
3494 case bp_watchpoint_scope:
3495 case bp_call_dummy:
aa7d318d 3496 case bp_std_terminate:
1042e4c0 3497 case bp_tracepoint:
7a697b8d 3498 case bp_fast_tracepoint:
4efc6507 3499 case bp_jit_event:
0e30163f
JK
3500 case bp_gnu_ifunc_resolver:
3501 case bp_gnu_ifunc_resolver_return:
e514a9d6 3502 default:
f7545552
TT
3503 result = PRINT_UNKNOWN;
3504 break;
e514a9d6 3505 }
f7545552
TT
3506
3507 do_cleanups (old_chain);
3508 return result;
e514a9d6
JM
3509}
3510
3511/* Generic routine for printing messages indicating why we
4a64f543 3512 stopped. The behavior of this function depends on the value
e514a9d6
JM
3513 'print_it' in the bpstat structure. Under some circumstances we
3514 may decide not to print anything here and delegate the task to
4a64f543 3515 normal_stop(). */
e514a9d6
JM
3516
3517static enum print_stop_action
3518print_bp_stop_message (bpstat bs)
3519{
3520 switch (bs->print_it)
3521 {
3522 case print_it_noop:
4a64f543 3523 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3524 return PRINT_UNKNOWN;
3525 break;
3526
3527 case print_it_done:
3528 /* We still want to print the frame, but we already printed the
4a64f543 3529 relevant messages. */
e514a9d6
JM
3530 return PRINT_SRC_AND_LOC;
3531 break;
3532
3533 case print_it_normal:
4f8d1dc6 3534 {
f431efe5
PA
3535 struct breakpoint *b = bs->breakpoint_at;
3536
1a6a67de
TJB
3537 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3538 which has since been deleted. */
3539 if (b == NULL)
3540 return PRINT_UNKNOWN;
3541
4f8d1dc6
VP
3542 /* Normal case. Call the breakpoint's print_it method, or
3543 print_it_typical. */
1a6a67de 3544 if (b->ops != NULL && b->ops->print_it != NULL)
4f8d1dc6
VP
3545 return b->ops->print_it (b);
3546 else
3547 return print_it_typical (bs);
3548 }
3549 break;
3086aeae 3550
e514a9d6 3551 default:
8e65ff28 3552 internal_error (__FILE__, __LINE__,
e2e0b3e5 3553 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3554 break;
c906108c 3555 }
c906108c
SS
3556}
3557
e514a9d6
JM
3558/* Print a message indicating what happened. This is called from
3559 normal_stop(). The input to this routine is the head of the bpstat
3560 list - a list of the eventpoints that caused this stop. This
3561 routine calls the generic print routine for printing a message
3562 about reasons for stopping. This will print (for example) the
3563 "Breakpoint n," part of the output. The return value of this
3564 routine is one of:
c906108c 3565
4a64f543 3566 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3567 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3568 code to print the location. An example is
c5aa993b
JM
3569 "Breakpoint 1, " which should be followed by
3570 the location.
917317f4 3571 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3572 to also print the location part of the message.
3573 An example is the catch/throw messages, which
4a64f543 3574 don't require a location appended to the end.
917317f4 3575 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3576 further info to be printed. */
c906108c 3577
917317f4 3578enum print_stop_action
fba45db2 3579bpstat_print (bpstat bs)
c906108c
SS
3580{
3581 int val;
c5aa993b 3582
c906108c 3583 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3584 (Currently all watchpoints go on the bpstat whether hit or not.
3585 That probably could (should) be changed, provided care is taken
c906108c 3586 with respect to bpstat_explains_signal). */
e514a9d6
JM
3587 for (; bs; bs = bs->next)
3588 {
3589 val = print_bp_stop_message (bs);
3590 if (val == PRINT_SRC_ONLY
3591 || val == PRINT_SRC_AND_LOC
3592 || val == PRINT_NOTHING)
3593 return val;
3594 }
c906108c 3595
e514a9d6 3596 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3597 with and nothing was printed. */
917317f4 3598 return PRINT_UNKNOWN;
c906108c
SS
3599}
3600
4a64f543
MS
3601/* Evaluate the expression EXP and return 1 if value is zero. This is
3602 used inside a catch_errors to evaluate the breakpoint condition.
3603 The argument is a "struct expression *" that has been cast to a
3604 "char *" to make it pass through catch_errors. */
c906108c
SS
3605
3606static int
4efb68b1 3607breakpoint_cond_eval (void *exp)
c906108c 3608{
278cd55f 3609 struct value *mark = value_mark ();
c5aa993b 3610 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3611
c906108c
SS
3612 value_free_to_mark (mark);
3613 return i;
3614}
3615
5760d0ab 3616/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3617
3618static bpstat
5760d0ab 3619bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3620{
3621 bpstat bs;
3622
3623 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3624 bs->next = NULL;
3625 **bs_link_pointer = bs;
3626 *bs_link_pointer = &bs->next;
f431efe5
PA
3627 bs->breakpoint_at = bl->owner;
3628 bs->bp_location_at = bl;
3629 incref_bp_location (bl);
c906108c
SS
3630 /* If the condition is false, etc., don't do the commands. */
3631 bs->commands = NULL;
9add0f1b 3632 bs->commands_left = NULL;
c906108c
SS
3633 bs->old_val = NULL;
3634 bs->print_it = print_it_normal;
3635 return bs;
3636}
3637\f
d983da9c
DJ
3638/* The target has stopped with waitstatus WS. Check if any hardware
3639 watchpoints have triggered, according to the target. */
3640
3641int
3642watchpoints_triggered (struct target_waitstatus *ws)
3643{
d92524f1 3644 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3645 CORE_ADDR addr;
3646 struct breakpoint *b;
3647
3648 if (!stopped_by_watchpoint)
3649 {
3650 /* We were not stopped by a watchpoint. Mark all watchpoints
3651 as not triggered. */
3652 ALL_BREAKPOINTS (b)
cc60f2e3 3653 if (is_hardware_watchpoint (b))
d983da9c
DJ
3654 b->watchpoint_triggered = watch_triggered_no;
3655
3656 return 0;
3657 }
3658
3659 if (!target_stopped_data_address (&current_target, &addr))
3660 {
3661 /* We were stopped by a watchpoint, but we don't know where.
3662 Mark all watchpoints as unknown. */
3663 ALL_BREAKPOINTS (b)
cc60f2e3 3664 if (is_hardware_watchpoint (b))
d983da9c
DJ
3665 b->watchpoint_triggered = watch_triggered_unknown;
3666
3667 return stopped_by_watchpoint;
3668 }
3669
3670 /* The target could report the data address. Mark watchpoints
3671 affected by this data address as triggered, and all others as not
3672 triggered. */
3673
3674 ALL_BREAKPOINTS (b)
cc60f2e3 3675 if (is_hardware_watchpoint (b))
d983da9c 3676 {
a5606eee 3677 struct bp_location *loc;
d983da9c
DJ
3678
3679 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3680 for (loc = b->loc; loc; loc = loc->next)
3681 /* Exact match not required. Within range is
3682 sufficient. */
5009afc5
AS
3683 if (target_watchpoint_addr_within_range (&current_target,
3684 addr, loc->address,
3685 loc->length))
a5606eee
VP
3686 {
3687 b->watchpoint_triggered = watch_triggered_yes;
3688 break;
3689 }
d983da9c
DJ
3690 }
3691
3692 return 1;
3693}
3694
c906108c
SS
3695/* Possible return values for watchpoint_check (this can't be an enum
3696 because of check_errors). */
3697/* The watchpoint has been deleted. */
3698#define WP_DELETED 1
3699/* The value has changed. */
3700#define WP_VALUE_CHANGED 2
3701/* The value has not changed. */
3702#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3703/* Ignore this watchpoint, no matter if the value changed or not. */
3704#define WP_IGNORE 4
c906108c
SS
3705
3706#define BP_TEMPFLAG 1
3707#define BP_HARDWAREFLAG 2
3708
4a64f543
MS
3709/* Evaluate watchpoint condition expression and check if its value
3710 changed.
553e4c11
JB
3711
3712 P should be a pointer to struct bpstat, but is defined as a void *
3713 in order for this function to be usable with catch_errors. */
c906108c
SS
3714
3715static int
4efb68b1 3716watchpoint_check (void *p)
c906108c
SS
3717{
3718 bpstat bs = (bpstat) p;
3719 struct breakpoint *b;
3720 struct frame_info *fr;
3721 int within_current_scope;
3722
f431efe5 3723 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3724 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3725 b = bs->breakpoint_at;
c906108c 3726
d0fb5eae
JK
3727 gdb_assert (is_watchpoint (b));
3728
f6bc2008
PA
3729 /* If this is a local watchpoint, we only want to check if the
3730 watchpoint frame is in scope if the current thread is the thread
3731 that was used to create the watchpoint. */
3732 if (!watchpoint_in_thread_scope (b))
60e1c644 3733 return WP_IGNORE;
f6bc2008 3734
c906108c
SS
3735 if (b->exp_valid_block == NULL)
3736 within_current_scope = 1;
3737 else
3738 {
edb3359d
DJ
3739 struct frame_info *frame = get_current_frame ();
3740 struct gdbarch *frame_arch = get_frame_arch (frame);
3741 CORE_ADDR frame_pc = get_frame_pc (frame);
3742
4a64f543
MS
3743 /* in_function_epilogue_p() returns a non-zero value if we're
3744 still in the function but the stack frame has already been
3745 invalidated. Since we can't rely on the values of local
3746 variables after the stack has been destroyed, we are treating
3747 the watchpoint in that state as `not changed' without further
3748 checking. Don't mark watchpoints as changed if the current
3749 frame is in an epilogue - even if they are in some other
3750 frame, our view of the stack is likely to be wrong and
3751 frame_find_by_id could error out. */
a0f49112 3752 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3753 return WP_IGNORE;
a0f49112 3754
101dcfbe 3755 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3756 within_current_scope = (fr != NULL);
69fbadd5
DJ
3757
3758 /* If we've gotten confused in the unwinder, we might have
3759 returned a frame that can't describe this variable. */
edb3359d
DJ
3760 if (within_current_scope)
3761 {
3762 struct symbol *function;
3763
3764 function = get_frame_function (fr);
3765 if (function == NULL
3766 || !contained_in (b->exp_valid_block,
3767 SYMBOL_BLOCK_VALUE (function)))
3768 within_current_scope = 0;
3769 }
69fbadd5 3770
edb3359d 3771 if (within_current_scope)
c906108c
SS
3772 /* If we end up stopping, the current frame will get selected
3773 in normal_stop. So this call to select_frame won't affect
3774 the user. */
0f7d239c 3775 select_frame (fr);
c906108c 3776 }
c5aa993b 3777
c906108c
SS
3778 if (within_current_scope)
3779 {
4a64f543
MS
3780 /* We use value_{,free_to_}mark because it could be a *long*
3781 time before we return to the command level and call
3782 free_all_values. We can't call free_all_values because we
3783 might be in the middle of evaluating a function call. */
c906108c 3784
0cf6dd15 3785 int pc = 0;
278cd55f 3786 struct value *mark = value_mark ();
fa4727a6
DJ
3787 struct value *new_val;
3788
0cf6dd15 3789 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3790
4a64f543
MS
3791 /* We use value_equal_contents instead of value_equal because
3792 the latter coerces an array to a pointer, thus comparing just
3793 the address of the array instead of its contents. This is
3794 not what we want. */
fa4727a6 3795 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3796 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3797 {
fa4727a6
DJ
3798 if (new_val != NULL)
3799 {
3800 release_value (new_val);
3801 value_free_to_mark (mark);
3802 }
c906108c
SS
3803 bs->old_val = b->val;
3804 b->val = new_val;
fa4727a6 3805 b->val_valid = 1;
c906108c
SS
3806 return WP_VALUE_CHANGED;
3807 }
3808 else
3809 {
60e1c644 3810 /* Nothing changed. */
c906108c 3811 value_free_to_mark (mark);
c906108c
SS
3812 return WP_VALUE_NOT_CHANGED;
3813 }
3814 }
3815 else
3816 {
3817 /* This seems like the only logical thing to do because
c5aa993b
JM
3818 if we temporarily ignored the watchpoint, then when
3819 we reenter the block in which it is valid it contains
3820 garbage (in the case of a function, it may have two
3821 garbage values, one before and one after the prologue).
3822 So we can't even detect the first assignment to it and
3823 watch after that (since the garbage may or may not equal
3824 the first value assigned). */
4ce44c66
JM
3825 /* We print all the stop information in print_it_typical(), but
3826 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3827 will be deleted already. So we have no choice but print the
3828 information here. */
9dc5e2a9 3829 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3830 ui_out_field_string
3831 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3832 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3833 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3834 ui_out_text (uiout,
3835 " deleted because the program has left the block in\n\
8b93c638 3836which its expression is valid.\n");
4ce44c66 3837
d0fb5eae 3838 watchpoint_del_at_next_stop (b);
c906108c
SS
3839
3840 return WP_DELETED;
3841 }
3842}
3843
18a18393
VP
3844/* Return true if it looks like target has stopped due to hitting
3845 breakpoint location BL. This function does not check if we
3846 should stop, only if BL explains the stop. */
3847static int
6c95b8df
PA
3848bpstat_check_location (const struct bp_location *bl,
3849 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3850{
3851 struct breakpoint *b = bl->owner;
3852
2bdf28a0
JK
3853 /* BL is from existing struct breakpoint. */
3854 gdb_assert (b != NULL);
3855
f1310107
TJB
3856 if (b->ops && b->ops->breakpoint_hit)
3857 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
3858
e8595ef6
SS
3859 /* By definition, the inferior does not report stops at
3860 tracepoints. */
d77f58be 3861 if (is_tracepoint (b))
e8595ef6
SS
3862 return 0;
3863
cc60f2e3 3864 if (!is_watchpoint (b)
18a18393 3865 && b->type != bp_hardware_breakpoint
fe798b75 3866 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3867 {
6c95b8df
PA
3868 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3869 aspace, bp_addr))
18a18393
VP
3870 return 0;
3871 if (overlay_debugging /* unmapped overlay section */
35df4500 3872 && section_is_overlay (bl->section)
18a18393
VP
3873 && !section_is_mapped (bl->section))
3874 return 0;
3875 }
cc60f2e3 3876
18a18393
VP
3877 /* Continuable hardware watchpoints are treated as non-existent if the
3878 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3879 some data address). Otherwise gdb won't stop on a break instruction
3880 in the code (not from a breakpoint) when a hardware watchpoint has
3881 been defined. Also skip watchpoints which we know did not trigger
3882 (did not match the data address). */
cc60f2e3
PA
3883
3884 if (is_hardware_watchpoint (b)
18a18393
VP
3885 && b->watchpoint_triggered == watch_triggered_no)
3886 return 0;
f1310107 3887
18a18393
VP
3888 if (b->type == bp_hardware_breakpoint)
3889 {
3890 if (bl->address != bp_addr)
3891 return 0;
3892 if (overlay_debugging /* unmapped overlay section */
35df4500 3893 && section_is_overlay (bl->section)
18a18393
VP
3894 && !section_is_mapped (bl->section))
3895 return 0;
3896 }
ce78b96d 3897
18a18393
VP
3898 return 1;
3899}
3900
3901/* If BS refers to a watchpoint, determine if the watched values
3902 has actually changed, and we should stop. If not, set BS->stop
3903 to 0. */
3904static void
3905bpstat_check_watchpoint (bpstat bs)
3906{
2bdf28a0
JK
3907 const struct bp_location *bl;
3908 struct breakpoint *b;
3909
3910 /* BS is built for existing struct breakpoint. */
f431efe5 3911 bl = bs->bp_location_at;
2bdf28a0 3912 gdb_assert (bl != NULL);
f431efe5 3913 b = bs->breakpoint_at;
2bdf28a0 3914 gdb_assert (b != NULL);
18a18393 3915
cc60f2e3 3916 if (is_watchpoint (b))
18a18393 3917 {
18a18393
VP
3918 int must_check_value = 0;
3919
3920 if (b->type == bp_watchpoint)
3921 /* For a software watchpoint, we must always check the
3922 watched value. */
3923 must_check_value = 1;
3924 else if (b->watchpoint_triggered == watch_triggered_yes)
3925 /* We have a hardware watchpoint (read, write, or access)
3926 and the target earlier reported an address watched by
3927 this watchpoint. */
3928 must_check_value = 1;
3929 else if (b->watchpoint_triggered == watch_triggered_unknown
3930 && b->type == bp_hardware_watchpoint)
3931 /* We were stopped by a hardware watchpoint, but the target could
3932 not report the data address. We must check the watchpoint's
3933 value. Access and read watchpoints are out of luck; without
3934 a data address, we can't figure it out. */
3935 must_check_value = 1;
3936
3937 if (must_check_value)
3938 {
3e43a32a
MS
3939 char *message
3940 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3941 b->number);
18a18393
VP
3942 struct cleanup *cleanups = make_cleanup (xfree, message);
3943 int e = catch_errors (watchpoint_check, bs, message,
3944 RETURN_MASK_ALL);
3945 do_cleanups (cleanups);
3946 switch (e)
3947 {
3948 case WP_DELETED:
3949 /* We've already printed what needs to be printed. */
3950 bs->print_it = print_it_done;
3951 /* Stop. */
3952 break;
60e1c644
PA
3953 case WP_IGNORE:
3954 bs->print_it = print_it_noop;
3955 bs->stop = 0;
3956 break;
18a18393
VP
3957 case WP_VALUE_CHANGED:
3958 if (b->type == bp_read_watchpoint)
3959 {
85d721b8
PA
3960 /* There are two cases to consider here:
3961
4a64f543 3962 1. We're watching the triggered memory for reads.
85d721b8
PA
3963 In that case, trust the target, and always report
3964 the watchpoint hit to the user. Even though
3965 reads don't cause value changes, the value may
3966 have changed since the last time it was read, and
3967 since we're not trapping writes, we will not see
3968 those, and as such we should ignore our notion of
3969 old value.
3970
4a64f543 3971 2. We're watching the triggered memory for both
85d721b8
PA
3972 reads and writes. There are two ways this may
3973 happen:
3974
4a64f543 3975 2.1. This is a target that can't break on data
85d721b8
PA
3976 reads only, but can break on accesses (reads or
3977 writes), such as e.g., x86. We detect this case
3978 at the time we try to insert read watchpoints.
3979
4a64f543 3980 2.2. Otherwise, the target supports read
85d721b8
PA
3981 watchpoints, but, the user set an access or write
3982 watchpoint watching the same memory as this read
3983 watchpoint.
3984
3985 If we're watching memory writes as well as reads,
3986 ignore watchpoint hits when we find that the
3987 value hasn't changed, as reads don't cause
3988 changes. This still gives false positives when
3989 the program writes the same value to memory as
3990 what there was already in memory (we will confuse
3991 it for a read), but it's much better than
3992 nothing. */
3993
3994 int other_write_watchpoint = 0;
3995
3996 if (bl->watchpoint_type == hw_read)
3997 {
3998 struct breakpoint *other_b;
3999
4000 ALL_BREAKPOINTS (other_b)
4001 if ((other_b->type == bp_hardware_watchpoint
4002 || other_b->type == bp_access_watchpoint)
4003 && (other_b->watchpoint_triggered
4004 == watch_triggered_yes))
4005 {
4006 other_write_watchpoint = 1;
4007 break;
4008 }
4009 }
4010
4011 if (other_write_watchpoint
4012 || bl->watchpoint_type == hw_access)
4013 {
4014 /* We're watching the same memory for writes,
4015 and the value changed since the last time we
4016 updated it, so this trap must be for a write.
4017 Ignore it. */
4018 bs->print_it = print_it_noop;
4019 bs->stop = 0;
4020 }
18a18393
VP
4021 }
4022 break;
4023 case WP_VALUE_NOT_CHANGED:
4024 if (b->type == bp_hardware_watchpoint
4025 || b->type == bp_watchpoint)
4026 {
4027 /* Don't stop: write watchpoints shouldn't fire if
4028 the value hasn't changed. */
4029 bs->print_it = print_it_noop;
4030 bs->stop = 0;
4031 }
4032 /* Stop. */
4033 break;
4034 default:
4035 /* Can't happen. */
4036 case 0:
4037 /* Error from catch_errors. */
4038 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 4039 watchpoint_del_at_next_stop (b);
18a18393
VP
4040 /* We've already printed what needs to be printed. */
4041 bs->print_it = print_it_done;
4042 break;
4043 }
4044 }
4045 else /* must_check_value == 0 */
4046 {
4047 /* This is a case where some watchpoint(s) triggered, but
4048 not at the address of this watchpoint, or else no
4049 watchpoint triggered after all. So don't print
4050 anything for this watchpoint. */
4051 bs->print_it = print_it_noop;
4052 bs->stop = 0;
4053 }
4054 }
4055}
4056
4057
4058/* Check conditions (condition proper, frame, thread and ignore count)
4059 of breakpoint referred to by BS. If we should not stop for this
4060 breakpoint, set BS->stop to 0. */
f431efe5 4061
18a18393
VP
4062static void
4063bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4064{
4065 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4066 const struct bp_location *bl;
4067 struct breakpoint *b;
4068
4069 /* BS is built for existing struct breakpoint. */
f431efe5 4070 bl = bs->bp_location_at;
2bdf28a0 4071 gdb_assert (bl != NULL);
f431efe5 4072 b = bs->breakpoint_at;
2bdf28a0 4073 gdb_assert (b != NULL);
18a18393
VP
4074
4075 if (frame_id_p (b->frame_id)
edb3359d 4076 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4077 bs->stop = 0;
4078 else if (bs->stop)
4079 {
4080 int value_is_zero = 0;
60e1c644
PA
4081 struct expression *cond;
4082
7371cf6d
PM
4083 /* Evaluate Python breakpoints that have a "stop"
4084 method implemented. */
4085 if (b->py_bp_object)
4086 bs->stop = gdbpy_should_stop (b->py_bp_object);
4087
60e1c644
PA
4088 if (is_watchpoint (b))
4089 cond = b->cond_exp;
4090 else
4091 cond = bl->cond;
4092
f431efe5 4093 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4094 {
60e1c644
PA
4095 int within_current_scope = 1;
4096
c5bc3a77
DJ
4097 /* We use value_mark and value_free_to_mark because it could
4098 be a long time before we return to the command level and
4099 call free_all_values. We can't call free_all_values
4100 because we might be in the middle of evaluating a
4101 function call. */
4102 struct value *mark = value_mark ();
4103
edb3359d
DJ
4104 /* Need to select the frame, with all that implies so that
4105 the conditions will have the right context. Because we
4106 use the frame, we will not see an inlined function's
4107 variables when we arrive at a breakpoint at the start
4108 of the inlined function; the current frame will be the
4109 call site. */
60e1c644
PA
4110 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4111 select_frame (get_current_frame ());
4112 else
4113 {
4114 struct frame_info *frame;
4115
4116 /* For local watchpoint expressions, which particular
4117 instance of a local is being watched matters, so we
4118 keep track of the frame to evaluate the expression
4119 in. To evaluate the condition however, it doesn't
4120 really matter which instantiation of the function
4121 where the condition makes sense triggers the
4122 watchpoint. This allows an expression like "watch
4123 global if q > 10" set in `func', catch writes to
4124 global on all threads that call `func', or catch
4125 writes on all recursive calls of `func' by a single
4126 thread. We simply always evaluate the condition in
4127 the innermost frame that's executing where it makes
4128 sense to evaluate the condition. It seems
4129 intuitive. */
4130 frame = block_innermost_frame (b->cond_exp_valid_block);
4131 if (frame != NULL)
4132 select_frame (frame);
4133 else
4134 within_current_scope = 0;
4135 }
4136 if (within_current_scope)
4137 value_is_zero
4138 = catch_errors (breakpoint_cond_eval, cond,
4139 "Error in testing breakpoint condition:\n",
4140 RETURN_MASK_ALL);
4141 else
4142 {
4143 warning (_("Watchpoint condition cannot be tested "
4144 "in the current scope"));
4145 /* If we failed to set the right context for this
4146 watchpoint, unconditionally report it. */
4147 value_is_zero = 0;
4148 }
4a64f543 4149 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4150 value_free_to_mark (mark);
18a18393 4151 }
60e1c644
PA
4152
4153 if (cond && value_is_zero)
18a18393
VP
4154 {
4155 bs->stop = 0;
4156 }
4157 else if (b->thread != -1 && b->thread != thread_id)
4158 {
4159 bs->stop = 0;
4160 }
4161 else if (b->ignore_count > 0)
4162 {
4163 b->ignore_count--;
4164 annotate_ignore_count_change ();
4165 bs->stop = 0;
4a64f543 4166 /* Increase the hit count even though we don't stop. */
18a18393 4167 ++(b->hit_count);
8d3788bd 4168 observer_notify_breakpoint_modified (b);
18a18393
VP
4169 }
4170 }
4171}
4172
4173
9709f61c 4174/* Get a bpstat associated with having just stopped at address
d983da9c 4175 BP_ADDR in thread PTID.
c906108c 4176
d983da9c 4177 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4178 don't understand this stop. Result is a chain of bpstat's such
4179 that:
c906108c 4180
c5aa993b 4181 if we don't understand the stop, the result is a null pointer.
c906108c 4182
c5aa993b 4183 if we understand why we stopped, the result is not null.
c906108c 4184
c5aa993b
JM
4185 Each element of the chain refers to a particular breakpoint or
4186 watchpoint at which we have stopped. (We may have stopped for
4187 several reasons concurrently.)
c906108c 4188
c5aa993b
JM
4189 Each element of the chain has valid next, breakpoint_at,
4190 commands, FIXME??? fields. */
c906108c
SS
4191
4192bpstat
6c95b8df
PA
4193bpstat_stop_status (struct address_space *aspace,
4194 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4195{
0d381245 4196 struct breakpoint *b = NULL;
afe38095 4197 struct bp_location *bl;
20874c92 4198 struct bp_location *loc;
5760d0ab
JK
4199 /* First item of allocated bpstat's. */
4200 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4201 /* Pointer to the last thing in the chain currently. */
5760d0ab 4202 bpstat bs;
20874c92 4203 int ix;
429374b8 4204 int need_remove_insert;
f431efe5 4205 int removed_any;
c906108c 4206
f431efe5
PA
4207 /* First, build the bpstat chain with locations that explain a
4208 target stop, while being careful to not set the target running,
4209 as that may invalidate locations (in particular watchpoint
4210 locations are recreated). Resuming will happen here with
4211 breakpoint conditions or watchpoint expressions that include
4212 inferior function calls. */
c5aa993b 4213
429374b8
JK
4214 ALL_BREAKPOINTS (b)
4215 {
4216 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4217 continue;
a5606eee 4218
429374b8
JK
4219 for (bl = b->loc; bl != NULL; bl = bl->next)
4220 {
4a64f543
MS
4221 /* For hardware watchpoints, we look only at the first
4222 location. The watchpoint_check function will work on the
4223 entire expression, not the individual locations. For
4224 read watchpoints, the watchpoints_triggered function has
4225 checked all locations already. */
429374b8
JK
4226 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4227 break;
18a18393 4228
429374b8
JK
4229 if (bl->shlib_disabled)
4230 continue;
c5aa993b 4231
429374b8
JK
4232 if (!bpstat_check_location (bl, aspace, bp_addr))
4233 continue;
c5aa993b 4234
4a64f543
MS
4235 /* Come here if it's a watchpoint, or if the break address
4236 matches. */
c5aa993b 4237
4a64f543
MS
4238 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4239 explain stop. */
c5aa993b 4240
f431efe5
PA
4241 /* Assume we stop. Should we find a watchpoint that is not
4242 actually triggered, or if the condition of the breakpoint
4243 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4244 bs->stop = 1;
4245 bs->print = 1;
d983da9c 4246
f431efe5
PA
4247 /* If this is a scope breakpoint, mark the associated
4248 watchpoint as triggered so that we will handle the
4249 out-of-scope event. We'll get to the watchpoint next
4250 iteration. */
d0fb5eae 4251 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4252 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4253 }
4254 }
4255
4256 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4257 {
f1310107 4258 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4259 {
5760d0ab 4260 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4261 /* For hits of moribund locations, we should just proceed. */
4262 bs->stop = 0;
4263 bs->print = 0;
4264 bs->print_it = print_it_noop;
4265 }
4266 }
4267
f431efe5
PA
4268 /* Now go through the locations that caused the target to stop, and
4269 check whether we're interested in reporting this stop to higher
4270 layers, or whether we should resume the target transparently. */
4271
4272 removed_any = 0;
4273
5760d0ab 4274 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4275 {
4276 if (!bs->stop)
4277 continue;
4278
4279 bpstat_check_watchpoint (bs);
4280 if (!bs->stop)
4281 continue;
4282
4283 b = bs->breakpoint_at;
18a18393 4284
429374b8 4285 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4286 || b->type == bp_longjmp_master
186c406b
TT
4287 || b->type == bp_std_terminate_master
4288 || b->type == bp_exception_master)
429374b8
JK
4289 /* We do not stop for these. */
4290 bs->stop = 0;
4291 else
4292 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4293
429374b8
JK
4294 if (bs->stop)
4295 {
4296 ++(b->hit_count);
8d3788bd 4297 observer_notify_breakpoint_modified (b);
c906108c 4298
4a64f543 4299 /* We will stop here. */
429374b8
JK
4300 if (b->disposition == disp_disable)
4301 {
4302 if (b->enable_state != bp_permanent)
4303 b->enable_state = bp_disabled;
f431efe5 4304 removed_any = 1;
429374b8
JK
4305 }
4306 if (b->silent)
4307 bs->print = 0;
4308 bs->commands = b->commands;
9add0f1b
TT
4309 incref_counted_command_line (bs->commands);
4310 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4311 if (bs->commands_left
4312 && (strcmp ("silent", bs->commands_left->line) == 0
4313 || (xdb_commands
4314 && strcmp ("Q",
4315 bs->commands_left->line) == 0)))
429374b8 4316 {
9add0f1b 4317 bs->commands_left = bs->commands_left->next;
429374b8
JK
4318 bs->print = 0;
4319 }
429374b8
JK
4320 }
4321
4322 /* Print nothing for this entry if we dont stop or dont print. */
4323 if (bs->stop == 0 || bs->print == 0)
4324 bs->print_it = print_it_noop;
429374b8 4325 }
876fa593 4326
d983da9c
DJ
4327 /* If we aren't stopping, the value of some hardware watchpoint may
4328 not have changed, but the intermediate memory locations we are
4329 watching may have. Don't bother if we're stopping; this will get
4330 done later. */
d832cb68 4331 need_remove_insert = 0;
5760d0ab
JK
4332 if (! bpstat_causes_stop (bs_head))
4333 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4334 if (!bs->stop
f431efe5
PA
4335 && bs->breakpoint_at
4336 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4337 {
4a64f543 4338 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4339 need_remove_insert = 1;
d983da9c
DJ
4340 }
4341
d832cb68 4342 if (need_remove_insert)
2d134ed3 4343 update_global_location_list (1);
f431efe5
PA
4344 else if (removed_any)
4345 update_global_location_list (0);
d832cb68 4346
5760d0ab 4347 return bs_head;
c906108c 4348}
628fe4e4
JK
4349
4350static void
4351handle_jit_event (void)
4352{
4353 struct frame_info *frame;
4354 struct gdbarch *gdbarch;
4355
4356 /* Switch terminal for any messages produced by
4357 breakpoint_re_set. */
4358 target_terminal_ours_for_output ();
4359
4360 frame = get_current_frame ();
4361 gdbarch = get_frame_arch (frame);
4362
4363 jit_event_handler (gdbarch);
4364
4365 target_terminal_inferior ();
4366}
4367
4368/* Prepare WHAT final decision for infrun. */
4369
4370/* Decide what infrun needs to do with this bpstat. */
4371
c906108c 4372struct bpstat_what
0e30163f 4373bpstat_what (bpstat bs_head)
c906108c 4374{
c906108c 4375 struct bpstat_what retval;
628fe4e4
JK
4376 /* We need to defer calling `solib_add', as adding new symbols
4377 resets breakpoints, which in turn deletes breakpoint locations,
4378 and hence may clear unprocessed entries in the BS chain. */
4379 int shlib_event = 0;
4380 int jit_event = 0;
0e30163f 4381 bpstat bs;
c906108c 4382
628fe4e4 4383 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4384 retval.call_dummy = STOP_NONE;
186c406b 4385 retval.is_longjmp = 0;
628fe4e4 4386
0e30163f 4387 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4388 {
628fe4e4
JK
4389 /* Extract this BS's action. After processing each BS, we check
4390 if its action overrides all we've seem so far. */
4391 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4392 enum bptype bptype;
4393
c906108c 4394 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4395 {
4396 /* I suspect this can happen if it was a momentary
4397 breakpoint which has since been deleted. */
4398 bptype = bp_none;
4399 }
f431efe5 4400 else if (bs->breakpoint_at == NULL)
628fe4e4 4401 bptype = bp_none;
20874c92 4402 else
f431efe5 4403 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4404
4405 switch (bptype)
c906108c
SS
4406 {
4407 case bp_none:
628fe4e4 4408 break;
c906108c
SS
4409 case bp_breakpoint:
4410 case bp_hardware_breakpoint:
4411 case bp_until:
4412 case bp_finish:
4413 if (bs->stop)
4414 {
4415 if (bs->print)
628fe4e4 4416 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4417 else
628fe4e4 4418 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4419 }
4420 else
628fe4e4 4421 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4422 break;
4423 case bp_watchpoint:
4424 case bp_hardware_watchpoint:
4425 case bp_read_watchpoint:
4426 case bp_access_watchpoint:
4427 if (bs->stop)
4428 {
4429 if (bs->print)
628fe4e4 4430 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4431 else
628fe4e4 4432 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4433 }
4434 else
628fe4e4
JK
4435 {
4436 /* There was a watchpoint, but we're not stopping.
4437 This requires no further action. */
4438 }
c906108c
SS
4439 break;
4440 case bp_longjmp:
186c406b 4441 case bp_exception:
628fe4e4 4442 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4443 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4444 break;
4445 case bp_longjmp_resume:
186c406b 4446 case bp_exception_resume:
628fe4e4 4447 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4448 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4449 break;
4450 case bp_step_resume:
4451 if (bs->stop)
628fe4e4
JK
4452 this_action = BPSTAT_WHAT_STEP_RESUME;
4453 else
c906108c 4454 {
628fe4e4
JK
4455 /* It is for the wrong frame. */
4456 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4457 }
c906108c 4458 break;
c906108c 4459 case bp_watchpoint_scope:
c4093a6a 4460 case bp_thread_event:
1900040c 4461 case bp_overlay_event:
0fd8e87f 4462 case bp_longjmp_master:
aa7d318d 4463 case bp_std_terminate_master:
186c406b 4464 case bp_exception_master:
628fe4e4 4465 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4466 break;
ce78b96d 4467 case bp_catchpoint:
c5aa993b
JM
4468 if (bs->stop)
4469 {
4470 if (bs->print)
628fe4e4 4471 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4472 else
628fe4e4 4473 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4474 }
4475 else
628fe4e4
JK
4476 {
4477 /* There was a catchpoint, but we're not stopping.
4478 This requires no further action. */
4479 }
4480 break;
4481 case bp_shlib_event:
4482 shlib_event = 1;
4483
4484 /* If requested, stop when the dynamic linker notifies GDB
4485 of events. This allows the user to get control and place
4486 breakpoints in initializer routines for dynamically
4487 loaded objects (among other things). */
4488 if (stop_on_solib_events)
4489 this_action = BPSTAT_WHAT_STOP_NOISY;
4490 else
4491 this_action = BPSTAT_WHAT_SINGLE;
4492 break;
4493 case bp_jit_event:
4494 jit_event = 1;
4495 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4496 break;
c906108c 4497 case bp_call_dummy:
53a5351d
JM
4498 /* Make sure the action is stop (silent or noisy),
4499 so infrun.c pops the dummy frame. */
aa7d318d 4500 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4501 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4502 break;
4503 case bp_std_terminate:
4504 /* Make sure the action is stop (silent or noisy),
4505 so infrun.c pops the dummy frame. */
aa7d318d 4506 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4507 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4508 break;
1042e4c0 4509 case bp_tracepoint:
7a697b8d 4510 case bp_fast_tracepoint:
0fb4aa4b 4511 case bp_static_tracepoint:
1042e4c0
SS
4512 /* Tracepoint hits should not be reported back to GDB, and
4513 if one got through somehow, it should have been filtered
4514 out already. */
4515 internal_error (__FILE__, __LINE__,
7a697b8d 4516 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4517 break;
4518 case bp_gnu_ifunc_resolver:
4519 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4520 this_action = BPSTAT_WHAT_SINGLE;
4521 break;
4522 case bp_gnu_ifunc_resolver_return:
4523 /* The breakpoint will be removed, execution will restart from the
4524 PC of the former breakpoint. */
4525 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4526 break;
628fe4e4
JK
4527 default:
4528 internal_error (__FILE__, __LINE__,
4529 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4530 }
628fe4e4
JK
4531
4532 retval.main_action = max (retval.main_action, this_action);
c906108c 4533 }
628fe4e4 4534
0e30163f
JK
4535 /* These operations may affect the bs->breakpoint_at state so they are
4536 delayed after MAIN_ACTION is decided above. */
4537
628fe4e4
JK
4538 if (shlib_event)
4539 {
4540 if (debug_infrun)
4541 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4542
4543 /* Check for any newly added shared libraries if we're supposed
4544 to be adding them automatically. */
4545
4546 /* Switch terminal for any messages produced by
4547 breakpoint_re_set. */
4548 target_terminal_ours_for_output ();
4549
4550#ifdef SOLIB_ADD
4551 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4552#else
4553 solib_add (NULL, 0, &current_target, auto_solib_add);
4554#endif
4555
4556 target_terminal_inferior ();
4557 }
4558
4559 if (jit_event)
4560 {
4561 if (debug_infrun)
4562 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4563
4564 handle_jit_event ();
4565 }
4566
0e30163f
JK
4567 for (bs = bs_head; bs != NULL; bs = bs->next)
4568 {
4569 struct breakpoint *b = bs->breakpoint_at;
4570
4571 if (b == NULL)
4572 continue;
4573 switch (b->type)
4574 {
4575 case bp_gnu_ifunc_resolver:
4576 gnu_ifunc_resolver_stop (b);
4577 break;
4578 case bp_gnu_ifunc_resolver_return:
4579 gnu_ifunc_resolver_return_stop (b);
4580 break;
4581 }
4582 }
4583
c906108c
SS
4584 return retval;
4585}
4586
4587/* Nonzero if we should step constantly (e.g. watchpoints on machines
4588 without hardware support). This isn't related to a specific bpstat,
4589 just to things like whether watchpoints are set. */
4590
c5aa993b 4591int
fba45db2 4592bpstat_should_step (void)
c906108c
SS
4593{
4594 struct breakpoint *b;
cc59ec59 4595
c906108c 4596 ALL_BREAKPOINTS (b)
717a8278 4597 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4598 return 1;
c906108c
SS
4599 return 0;
4600}
4601
67822962
PA
4602int
4603bpstat_causes_stop (bpstat bs)
4604{
4605 for (; bs != NULL; bs = bs->next)
4606 if (bs->stop)
4607 return 1;
4608
4609 return 0;
4610}
4611
c906108c 4612\f
c5aa993b 4613
170b53b2
UW
4614/* Compute a string of spaces suitable to indent the next line
4615 so it starts at the position corresponding to the table column
4616 named COL_NAME in the currently active table of UIOUT. */
4617
4618static char *
4619wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4620{
4621 static char wrap_indent[80];
4622 int i, total_width, width, align;
4623 char *text;
4624
4625 total_width = 0;
4626 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4627 {
4628 if (strcmp (text, col_name) == 0)
4629 {
4630 gdb_assert (total_width < sizeof wrap_indent);
4631 memset (wrap_indent, ' ', total_width);
4632 wrap_indent[total_width] = 0;
4633
4634 return wrap_indent;
4635 }
4636
4637 total_width += width + 1;
4638 }
4639
4640 return NULL;
4641}
4642
859825b8
JK
4643/* Print the LOC location out of the list of B->LOC locations. */
4644
170b53b2
UW
4645static void
4646print_breakpoint_location (struct breakpoint *b,
4647 struct bp_location *loc)
0d381245 4648{
6c95b8df
PA
4649 struct cleanup *old_chain = save_current_program_space ();
4650
859825b8
JK
4651 if (loc != NULL && loc->shlib_disabled)
4652 loc = NULL;
4653
6c95b8df
PA
4654 if (loc != NULL)
4655 set_current_program_space (loc->pspace);
4656
56435ebe
TT
4657 if (b->display_canonical)
4658 ui_out_field_string (uiout, "what", b->addr_string);
4659 else if (b->source_file && loc)
0d381245
VP
4660 {
4661 struct symbol *sym
4662 = find_pc_sect_function (loc->address, loc->section);
4663 if (sym)
4664 {
4665 ui_out_text (uiout, "in ");
4666 ui_out_field_string (uiout, "func",
4667 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4668 ui_out_text (uiout, " ");
4669 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4670 ui_out_text (uiout, "at ");
0d381245
VP
4671 }
4672 ui_out_field_string (uiout, "file", b->source_file);
4673 ui_out_text (uiout, ":");
4674
4675 if (ui_out_is_mi_like_p (uiout))
4676 {
4677 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4678 char *fullname = symtab_to_fullname (sal.symtab);
4679
4680 if (fullname)
4681 ui_out_field_string (uiout, "fullname", fullname);
4682 }
4683
4684 ui_out_field_int (uiout, "line", b->line_number);
4685 }
859825b8 4686 else if (loc)
0d381245 4687 {
170b53b2
UW
4688 struct ui_stream *stb = ui_out_stream_new (uiout);
4689 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4690
22e722e1
DJ
4691 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4692 demangle, "");
0d381245 4693 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4694
4695 do_cleanups (stb_chain);
0d381245 4696 }
859825b8
JK
4697 else
4698 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4699
4700 do_cleanups (old_chain);
0d381245
VP
4701}
4702
269b11a2
PA
4703static const char *
4704bptype_string (enum bptype type)
c906108c 4705{
c4093a6a
JM
4706 struct ep_type_description
4707 {
4708 enum bptype type;
4709 char *description;
4710 };
4711 static struct ep_type_description bptypes[] =
c906108c 4712 {
c5aa993b
JM
4713 {bp_none, "?deleted?"},
4714 {bp_breakpoint, "breakpoint"},
c906108c 4715 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4716 {bp_until, "until"},
4717 {bp_finish, "finish"},
4718 {bp_watchpoint, "watchpoint"},
c906108c 4719 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4720 {bp_read_watchpoint, "read watchpoint"},
4721 {bp_access_watchpoint, "acc watchpoint"},
4722 {bp_longjmp, "longjmp"},
4723 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4724 {bp_exception, "exception"},
4725 {bp_exception_resume, "exception resume"},
c5aa993b 4726 {bp_step_resume, "step resume"},
c5aa993b
JM
4727 {bp_watchpoint_scope, "watchpoint scope"},
4728 {bp_call_dummy, "call dummy"},
aa7d318d 4729 {bp_std_terminate, "std::terminate"},
c5aa993b 4730 {bp_shlib_event, "shlib events"},
c4093a6a 4731 {bp_thread_event, "thread events"},
1900040c 4732 {bp_overlay_event, "overlay events"},
0fd8e87f 4733 {bp_longjmp_master, "longjmp master"},
aa7d318d 4734 {bp_std_terminate_master, "std::terminate master"},
186c406b 4735 {bp_exception_master, "exception master"},
ce78b96d 4736 {bp_catchpoint, "catchpoint"},
1042e4c0 4737 {bp_tracepoint, "tracepoint"},
7a697b8d 4738 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4739 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4740 {bp_jit_event, "jit events"},
0e30163f
JK
4741 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4742 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4743 };
269b11a2
PA
4744
4745 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4746 || ((int) type != bptypes[(int) type].type))
4747 internal_error (__FILE__, __LINE__,
4748 _("bptypes table does not describe type #%d."),
4749 (int) type);
4750
4751 return bptypes[(int) type].description;
4752}
4753
4754/* Print B to gdb_stdout. */
4755
4756static void
4757print_one_breakpoint_location (struct breakpoint *b,
4758 struct bp_location *loc,
4759 int loc_number,
4760 struct bp_location **last_loc,
269b11a2
PA
4761 int allflag)
4762{
4763 struct command_line *l;
c2c6d25f 4764 static char bpenables[] = "nynny";
c906108c 4765
0d381245
VP
4766 int header_of_multiple = 0;
4767 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4768 struct value_print_options opts;
4769
4770 get_user_print_options (&opts);
0d381245
VP
4771
4772 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4773 /* See comment in print_one_breakpoint concerning treatment of
4774 breakpoints with single disabled location. */
0d381245
VP
4775 if (loc == NULL
4776 && (b->loc != NULL
4777 && (b->loc->next != NULL || !b->loc->enabled)))
4778 header_of_multiple = 1;
4779 if (loc == NULL)
4780 loc = b->loc;
4781
c4093a6a
JM
4782 annotate_record ();
4783
4784 /* 1 */
4785 annotate_field (0);
0d381245
VP
4786 if (part_of_multiple)
4787 {
4788 char *formatted;
0c6773c1 4789 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4790 ui_out_field_string (uiout, "number", formatted);
4791 xfree (formatted);
4792 }
4793 else
4794 {
4795 ui_out_field_int (uiout, "number", b->number);
4796 }
c4093a6a
JM
4797
4798 /* 2 */
4799 annotate_field (1);
0d381245
VP
4800 if (part_of_multiple)
4801 ui_out_field_skip (uiout, "type");
269b11a2
PA
4802 else
4803 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4804
4805 /* 3 */
4806 annotate_field (2);
0d381245
VP
4807 if (part_of_multiple)
4808 ui_out_field_skip (uiout, "disp");
4809 else
2cec12e5 4810 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4811
c4093a6a
JM
4812
4813 /* 4 */
4814 annotate_field (3);
0d381245 4815 if (part_of_multiple)
54e52265 4816 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4817 else
4a64f543
MS
4818 ui_out_field_fmt (uiout, "enabled", "%c",
4819 bpenables[(int) b->enable_state]);
54e52265 4820 ui_out_spaces (uiout, 2);
0d381245 4821
c4093a6a
JM
4822
4823 /* 5 and 6 */
3086aeae 4824 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4825 {
4a64f543
MS
4826 /* Although the print_one can possibly print all locations,
4827 calling it here is not likely to get any nice result. So,
4828 make sure there's just one location. */
0d381245 4829 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4830 b->ops->print_one (b, last_loc);
0d381245 4831 }
3086aeae
DJ
4832 else
4833 switch (b->type)
4834 {
4835 case bp_none:
4836 internal_error (__FILE__, __LINE__,
e2e0b3e5 4837 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4838 break;
c906108c 4839
3086aeae
DJ
4840 case bp_watchpoint:
4841 case bp_hardware_watchpoint:
4842 case bp_read_watchpoint:
4843 case bp_access_watchpoint:
4844 /* Field 4, the address, is omitted (which makes the columns
4845 not line up too nicely with the headers, but the effect
4846 is relatively readable). */
79a45b7d 4847 if (opts.addressprint)
3086aeae
DJ
4848 ui_out_field_skip (uiout, "addr");
4849 annotate_field (5);
fa8a61dc 4850 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4851 break;
4852
3086aeae
DJ
4853 case bp_breakpoint:
4854 case bp_hardware_breakpoint:
4855 case bp_until:
4856 case bp_finish:
4857 case bp_longjmp:
4858 case bp_longjmp_resume:
186c406b
TT
4859 case bp_exception:
4860 case bp_exception_resume:
3086aeae 4861 case bp_step_resume:
3086aeae
DJ
4862 case bp_watchpoint_scope:
4863 case bp_call_dummy:
aa7d318d 4864 case bp_std_terminate:
3086aeae
DJ
4865 case bp_shlib_event:
4866 case bp_thread_event:
4867 case bp_overlay_event:
0fd8e87f 4868 case bp_longjmp_master:
aa7d318d 4869 case bp_std_terminate_master:
186c406b 4870 case bp_exception_master:
1042e4c0 4871 case bp_tracepoint:
7a697b8d 4872 case bp_fast_tracepoint:
0fb4aa4b 4873 case bp_static_tracepoint:
4efc6507 4874 case bp_jit_event:
0e30163f
JK
4875 case bp_gnu_ifunc_resolver:
4876 case bp_gnu_ifunc_resolver_return:
79a45b7d 4877 if (opts.addressprint)
3086aeae
DJ
4878 {
4879 annotate_field (4);
54e52265 4880 if (header_of_multiple)
0d381245 4881 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4882 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4883 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4884 else
5af949e3
UW
4885 ui_out_field_core_addr (uiout, "addr",
4886 loc->gdbarch, loc->address);
3086aeae
DJ
4887 }
4888 annotate_field (5);
0d381245 4889 if (!header_of_multiple)
170b53b2 4890 print_breakpoint_location (b, loc);
0d381245 4891 if (b->loc)
a6d9a66e 4892 *last_loc = b->loc;
3086aeae
DJ
4893 break;
4894 }
c906108c 4895
6c95b8df
PA
4896
4897 /* For backward compatibility, don't display inferiors unless there
4898 are several. */
4899 if (loc != NULL
4900 && !header_of_multiple
4901 && (allflag
4902 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4903 && (number_of_program_spaces () > 1
4904 || number_of_inferiors () > 1)
4a64f543
MS
4905 /* LOC is for existing B, it cannot be in
4906 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4907 && loc->owner->type != bp_catchpoint)))
4908 {
4909 struct inferior *inf;
4910 int first = 1;
4911
4912 for (inf = inferior_list; inf != NULL; inf = inf->next)
4913 {
4914 if (inf->pspace == loc->pspace)
4915 {
4916 if (first)
4917 {
4918 first = 0;
4919 ui_out_text (uiout, " inf ");
4920 }
4921 else
4922 ui_out_text (uiout, ", ");
4923 ui_out_text (uiout, plongest (inf->num));
4924 }
4925 }
4926 }
4927
4a306c9a 4928 if (!part_of_multiple)
c4093a6a 4929 {
4a306c9a
JB
4930 if (b->thread != -1)
4931 {
4932 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4933 "stop only in" line a little further down. */
4a306c9a
JB
4934 ui_out_text (uiout, " thread ");
4935 ui_out_field_int (uiout, "thread", b->thread);
4936 }
4937 else if (b->task != 0)
4938 {
4939 ui_out_text (uiout, " task ");
4940 ui_out_field_int (uiout, "task", b->task);
4941 }
c4093a6a 4942 }
f1310107 4943
8b93c638 4944 ui_out_text (uiout, "\n");
f1310107
TJB
4945
4946 if (!part_of_multiple && b->ops && b->ops->print_one_detail)
4947 b->ops->print_one_detail (b, uiout);
4948
0fb4aa4b
PA
4949 if (!part_of_multiple && b->static_trace_marker_id)
4950 {
4951 gdb_assert (b->type == bp_static_tracepoint);
4952
4953 ui_out_text (uiout, "\tmarker id is ");
4954 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4955 b->static_trace_marker_id);
4956 ui_out_text (uiout, "\n");
4957 }
4958
0d381245 4959 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4960 {
4961 annotate_field (6);
8b93c638 4962 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4963 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4964 the frame ID. */
5af949e3
UW
4965 ui_out_field_core_addr (uiout, "frame",
4966 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4967 ui_out_text (uiout, "\n");
c4093a6a
JM
4968 }
4969
0d381245 4970 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4971 {
f7f9143b
JB
4972 /* We do not print the condition for Ada exception catchpoints
4973 because the condition is an internal implementation detail
4974 that we do not want to expose to the user. */
c4093a6a 4975 annotate_field (7);
d77f58be 4976 if (is_tracepoint (b))
1042e4c0
SS
4977 ui_out_text (uiout, "\ttrace only if ");
4978 else
4979 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4980 ui_out_field_string (uiout, "cond", b->cond_string);
4981 ui_out_text (uiout, "\n");
4982 }
4983
0d381245 4984 if (!part_of_multiple && b->thread != -1)
c4093a6a 4985 {
4a64f543 4986 /* FIXME should make an annotation for this. */
8b93c638
JM
4987 ui_out_text (uiout, "\tstop only in thread ");
4988 ui_out_field_int (uiout, "thread", b->thread);
4989 ui_out_text (uiout, "\n");
c4093a6a
JM
4990 }
4991
63c715c6 4992 if (!part_of_multiple && b->hit_count)
c4093a6a 4993 {
4a64f543 4994 /* FIXME should make an annotation for this. */
8b93c638
JM
4995 if (ep_is_catchpoint (b))
4996 ui_out_text (uiout, "\tcatchpoint");
4997 else
4998 ui_out_text (uiout, "\tbreakpoint");
4999 ui_out_text (uiout, " already hit ");
5000 ui_out_field_int (uiout, "times", b->hit_count);
5001 if (b->hit_count == 1)
5002 ui_out_text (uiout, " time\n");
5003 else
5004 ui_out_text (uiout, " times\n");
c4093a6a
JM
5005 }
5006
4a64f543
MS
5007 /* Output the count also if it is zero, but only if this is mi.
5008 FIXME: Should have a better test for this. */
9dc5e2a9 5009 if (ui_out_is_mi_like_p (uiout))
63c715c6 5010 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5011 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5012
0d381245 5013 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5014 {
5015 annotate_field (8);
8b93c638
JM
5016 ui_out_text (uiout, "\tignore next ");
5017 ui_out_field_int (uiout, "ignore", b->ignore_count);
5018 ui_out_text (uiout, " hits\n");
c4093a6a 5019 }
059fb39f 5020
9add0f1b 5021 l = b->commands ? b->commands->commands : NULL;
059fb39f 5022 if (!part_of_multiple && l)
c4093a6a 5023 {
3b31d625
EZ
5024 struct cleanup *script_chain;
5025
c4093a6a 5026 annotate_field (9);
3b31d625 5027 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5028 print_command_lines (uiout, l, 4);
3b31d625 5029 do_cleanups (script_chain);
c4093a6a 5030 }
d24317b4 5031
1042e4c0
SS
5032 if (!part_of_multiple && b->pass_count)
5033 {
5034 annotate_field (10);
5035 ui_out_text (uiout, "\tpass count ");
5036 ui_out_field_int (uiout, "pass", b->pass_count);
5037 ui_out_text (uiout, " \n");
5038 }
5039
d24317b4
VP
5040 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5041 {
5042 if (b->addr_string)
5043 ui_out_field_string (uiout, "original-location", b->addr_string);
5044 else if (b->exp_string)
5045 ui_out_field_string (uiout, "original-location", b->exp_string);
5046 }
c4093a6a 5047}
c5aa993b 5048
0d381245
VP
5049static void
5050print_one_breakpoint (struct breakpoint *b,
4a64f543 5051 struct bp_location **last_loc,
6c95b8df 5052 int allflag)
0d381245 5053{
8d3788bd
VP
5054 struct cleanup *bkpt_chain;
5055
5056 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5057
12c5a436 5058 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5059 do_cleanups (bkpt_chain);
0d381245
VP
5060
5061 /* If this breakpoint has custom print function,
5062 it's already printed. Otherwise, print individual
5063 locations, if any. */
5064 if (b->ops == NULL || b->ops->print_one == NULL)
5065 {
4a64f543
MS
5066 /* If breakpoint has a single location that is disabled, we
5067 print it as if it had several locations, since otherwise it's
5068 hard to represent "breakpoint enabled, location disabled"
5069 situation.
5070
5071 Note that while hardware watchpoints have several locations
a3be7890 5072 internally, that's not a property exposed to user. */
0d381245 5073 if (b->loc
a5606eee 5074 && !is_hardware_watchpoint (b)
8d3788bd 5075 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5076 {
5077 struct bp_location *loc;
5078 int n = 1;
8d3788bd 5079
0d381245 5080 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5081 {
5082 struct cleanup *inner2 =
5083 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5084 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5085 do_cleanups (inner2);
5086 }
0d381245
VP
5087 }
5088 }
5089}
5090
a6d9a66e
UW
5091static int
5092breakpoint_address_bits (struct breakpoint *b)
5093{
5094 int print_address_bits = 0;
5095 struct bp_location *loc;
5096
5097 for (loc = b->loc; loc; loc = loc->next)
5098 {
c7437ca6
PA
5099 int addr_bit;
5100
5101 /* Software watchpoints that aren't watching memory don't have
5102 an address to print. */
5103 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5104 continue;
5105
5106 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5107 if (addr_bit > print_address_bits)
5108 print_address_bits = addr_bit;
5109 }
5110
5111 return print_address_bits;
5112}
0d381245 5113
c4093a6a
JM
5114struct captured_breakpoint_query_args
5115 {
5116 int bnum;
5117 };
c5aa993b 5118
c4093a6a 5119static int
2b65245e 5120do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5121{
5122 struct captured_breakpoint_query_args *args = data;
52f0bd74 5123 struct breakpoint *b;
a6d9a66e 5124 struct bp_location *dummy_loc = NULL;
cc59ec59 5125
c4093a6a
JM
5126 ALL_BREAKPOINTS (b)
5127 {
5128 if (args->bnum == b->number)
c5aa993b 5129 {
12c5a436 5130 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5131 return GDB_RC_OK;
c5aa993b 5132 }
c4093a6a
JM
5133 }
5134 return GDB_RC_NONE;
5135}
c5aa993b 5136
c4093a6a 5137enum gdb_rc
4a64f543
MS
5138gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5139 char **error_message)
c4093a6a
JM
5140{
5141 struct captured_breakpoint_query_args args;
cc59ec59 5142
c4093a6a
JM
5143 args.bnum = bnum;
5144 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5145 an error. */
b0b13bb4
DJ
5146 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5147 error_message, RETURN_MASK_ALL) < 0)
5148 return GDB_RC_FAIL;
5149 else
5150 return GDB_RC_OK;
c4093a6a 5151}
c5aa993b 5152
09d682a4
TT
5153/* Return true if this breakpoint was set by the user, false if it is
5154 internal or momentary. */
5155
5156int
5157user_breakpoint_p (struct breakpoint *b)
5158{
46c6471b 5159 return b->number > 0;
09d682a4
TT
5160}
5161
7f3b0473 5162/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5163 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5164 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5165 FILTER is non-NULL, call it on each breakpoint and only include the
5166 ones for which it returns non-zero. Return the total number of
5167 breakpoints listed. */
c906108c 5168
d77f58be 5169static int
e5a67952 5170breakpoint_1 (char *args, int allflag,
4a64f543 5171 int (*filter) (const struct breakpoint *))
c4093a6a 5172{
52f0bd74 5173 struct breakpoint *b;
a6d9a66e 5174 struct bp_location *last_loc = NULL;
7f3b0473 5175 int nr_printable_breakpoints;
3b31d625 5176 struct cleanup *bkpttbl_chain;
79a45b7d 5177 struct value_print_options opts;
a6d9a66e 5178 int print_address_bits = 0;
269b11a2
PA
5179 int print_type_col_width = 14;
5180
79a45b7d
TT
5181 get_user_print_options (&opts);
5182
4a64f543
MS
5183 /* Compute the number of rows in the table, as well as the size
5184 required for address fields. */
7f3b0473
AC
5185 nr_printable_breakpoints = 0;
5186 ALL_BREAKPOINTS (b)
e5a67952
MS
5187 {
5188 /* If we have a filter, only list the breakpoints it accepts. */
5189 if (filter && !filter (b))
5190 continue;
5191
5192 /* If we have an "args" string, it is a list of breakpoints to
5193 accept. Skip the others. */
5194 if (args != NULL && *args != '\0')
5195 {
5196 if (allflag && parse_and_eval_long (args) != b->number)
5197 continue;
5198 if (!allflag && !number_is_in_list (args, b->number))
5199 continue;
5200 }
269b11a2 5201
e5a67952
MS
5202 if (allflag || user_breakpoint_p (b))
5203 {
5204 int addr_bit, type_len;
a6d9a66e 5205
e5a67952
MS
5206 addr_bit = breakpoint_address_bits (b);
5207 if (addr_bit > print_address_bits)
5208 print_address_bits = addr_bit;
269b11a2 5209
e5a67952
MS
5210 type_len = strlen (bptype_string (b->type));
5211 if (type_len > print_type_col_width)
5212 print_type_col_width = type_len;
5213
5214 nr_printable_breakpoints++;
5215 }
5216 }
7f3b0473 5217
79a45b7d 5218 if (opts.addressprint)
3b31d625 5219 bkpttbl_chain
3e43a32a
MS
5220 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5221 nr_printable_breakpoints,
3b31d625 5222 "BreakpointTable");
8b93c638 5223 else
3b31d625 5224 bkpttbl_chain
3e43a32a
MS
5225 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5226 nr_printable_breakpoints,
3b31d625 5227 "BreakpointTable");
8b93c638 5228
7f3b0473 5229 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5230 annotate_breakpoints_headers ();
5231 if (nr_printable_breakpoints > 0)
5232 annotate_field (0);
4a64f543 5233 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5234 if (nr_printable_breakpoints > 0)
5235 annotate_field (1);
269b11a2 5236 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5237 "type", "Type"); /* 2 */
d7faa9e7
AC
5238 if (nr_printable_breakpoints > 0)
5239 annotate_field (2);
4a64f543 5240 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5241 if (nr_printable_breakpoints > 0)
5242 annotate_field (3);
54e52265 5243 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5244 if (opts.addressprint)
e5a67952
MS
5245 {
5246 if (nr_printable_breakpoints > 0)
5247 annotate_field (4);
5248 if (print_address_bits <= 32)
5249 ui_out_table_header (uiout, 10, ui_left,
5250 "addr", "Address"); /* 5 */
5251 else
5252 ui_out_table_header (uiout, 18, ui_left,
5253 "addr", "Address"); /* 5 */
5254 }
d7faa9e7
AC
5255 if (nr_printable_breakpoints > 0)
5256 annotate_field (5);
5257 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5258 ui_out_table_body (uiout);
5259 if (nr_printable_breakpoints > 0)
5260 annotate_breakpoints_table ();
7f3b0473 5261
c4093a6a 5262 ALL_BREAKPOINTS (b)
e5a67952
MS
5263 {
5264 QUIT;
5265 /* If we have a filter, only list the breakpoints it accepts. */
5266 if (filter && !filter (b))
5267 continue;
5268
5269 /* If we have an "args" string, it is a list of breakpoints to
5270 accept. Skip the others. */
5271
5272 if (args != NULL && *args != '\0')
5273 {
5274 if (allflag) /* maintenance info breakpoint */
5275 {
5276 if (parse_and_eval_long (args) != b->number)
5277 continue;
5278 }
5279 else /* all others */
5280 {
5281 if (!number_is_in_list (args, b->number))
5282 continue;
5283 }
5284 }
5285 /* We only print out user settable breakpoints unless the
5286 allflag is set. */
5287 if (allflag || user_breakpoint_p (b))
12c5a436 5288 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5289 }
5290
3b31d625 5291 do_cleanups (bkpttbl_chain);
698384cd 5292
7f3b0473 5293 if (nr_printable_breakpoints == 0)
c906108c 5294 {
4a64f543
MS
5295 /* If there's a filter, let the caller decide how to report
5296 empty list. */
d77f58be
SS
5297 if (!filter)
5298 {
e5a67952 5299 if (args == NULL || *args == '\0')
d77f58be
SS
5300 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5301 else
4a64f543 5302 ui_out_message (uiout, 0,
e5a67952
MS
5303 "No breakpoint or watchpoint matching '%s'.\n",
5304 args);
d77f58be 5305 }
c906108c
SS
5306 }
5307 else
c4093a6a 5308 {
a6d9a66e
UW
5309 if (last_loc && !server_command)
5310 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5311 }
c906108c 5312
4a64f543 5313 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5314 there have been breakpoints? */
c906108c 5315 annotate_breakpoints_table_end ();
d77f58be
SS
5316
5317 return nr_printable_breakpoints;
c906108c
SS
5318}
5319
ad443146
SS
5320/* Display the value of default-collect in a way that is generally
5321 compatible with the breakpoint list. */
5322
5323static void
5324default_collect_info (void)
5325{
5326 /* If it has no value (which is frequently the case), say nothing; a
5327 message like "No default-collect." gets in user's face when it's
5328 not wanted. */
5329 if (!*default_collect)
5330 return;
5331
5332 /* The following phrase lines up nicely with per-tracepoint collect
5333 actions. */
5334 ui_out_text (uiout, "default collect ");
5335 ui_out_field_string (uiout, "default-collect", default_collect);
5336 ui_out_text (uiout, " \n");
5337}
5338
c906108c 5339static void
e5a67952 5340breakpoints_info (char *args, int from_tty)
c906108c 5341{
e5a67952 5342 breakpoint_1 (args, 0, NULL);
ad443146
SS
5343
5344 default_collect_info ();
d77f58be
SS
5345}
5346
5347static void
e5a67952 5348watchpoints_info (char *args, int from_tty)
d77f58be 5349{
e5a67952 5350 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5351
5352 if (num_printed == 0)
5353 {
e5a67952 5354 if (args == NULL || *args == '\0')
d77f58be
SS
5355 ui_out_message (uiout, 0, "No watchpoints.\n");
5356 else
e5a67952 5357 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5358 }
c906108c
SS
5359}
5360
7a292a7a 5361static void
e5a67952 5362maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5363{
e5a67952 5364 breakpoint_1 (args, 1, NULL);
ad443146
SS
5365
5366 default_collect_info ();
c906108c
SS
5367}
5368
0d381245 5369static int
714835d5 5370breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5371 struct program_space *pspace,
714835d5 5372 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5373{
5374 struct bp_location *bl = b->loc;
cc59ec59 5375
0d381245
VP
5376 for (; bl; bl = bl->next)
5377 {
6c95b8df
PA
5378 if (bl->pspace == pspace
5379 && bl->address == pc
0d381245
VP
5380 && (!overlay_debugging || bl->section == section))
5381 return 1;
5382 }
5383 return 0;
5384}
5385
6c95b8df
PA
5386/* Print a message describing any breakpoints set at PC. This
5387 concerns with logical breakpoints, so we match program spaces, not
5388 address spaces. */
c906108c
SS
5389
5390static void
6c95b8df
PA
5391describe_other_breakpoints (struct gdbarch *gdbarch,
5392 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5393 struct obj_section *section, int thread)
c906108c 5394{
52f0bd74
AC
5395 int others = 0;
5396 struct breakpoint *b;
c906108c
SS
5397
5398 ALL_BREAKPOINTS (b)
6c95b8df 5399 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5400 if (others > 0)
5401 {
a3f17187
AC
5402 if (others == 1)
5403 printf_filtered (_("Note: breakpoint "));
5404 else /* if (others == ???) */
5405 printf_filtered (_("Note: breakpoints "));
c906108c 5406 ALL_BREAKPOINTS (b)
6c95b8df 5407 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5408 {
5409 others--;
5410 printf_filtered ("%d", b->number);
5411 if (b->thread == -1 && thread != -1)
5412 printf_filtered (" (all threads)");
5413 else if (b->thread != -1)
5414 printf_filtered (" (thread %d)", b->thread);
5415 printf_filtered ("%s%s ",
059fb39f 5416 ((b->enable_state == bp_disabled
8bea4e01
UW
5417 || b->enable_state == bp_call_disabled
5418 || b->enable_state == bp_startup_disabled)
0d381245
VP
5419 ? " (disabled)"
5420 : b->enable_state == bp_permanent
5421 ? " (permanent)"
5422 : ""),
5423 (others > 1) ? ","
5424 : ((others == 1) ? " and" : ""));
5425 }
a3f17187 5426 printf_filtered (_("also set at pc "));
5af949e3 5427 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5428 printf_filtered (".\n");
5429 }
5430}
5431\f
5432/* Set the default place to put a breakpoint
5433 for the `break' command with no arguments. */
5434
5435void
6c95b8df
PA
5436set_default_breakpoint (int valid, struct program_space *pspace,
5437 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5438 int line)
c906108c
SS
5439{
5440 default_breakpoint_valid = valid;
6c95b8df 5441 default_breakpoint_pspace = pspace;
c906108c
SS
5442 default_breakpoint_address = addr;
5443 default_breakpoint_symtab = symtab;
5444 default_breakpoint_line = line;
5445}
5446
e4f237da
KB
5447/* Return true iff it is meaningful to use the address member of
5448 BPT. For some breakpoint types, the address member is irrelevant
5449 and it makes no sense to attempt to compare it to other addresses
5450 (or use it for any other purpose either).
5451
4a64f543
MS
5452 More specifically, each of the following breakpoint types will
5453 always have a zero valued address and we don't want to mark
5454 breakpoints of any of these types to be a duplicate of an actual
5455 breakpoint at address zero:
e4f237da
KB
5456
5457 bp_watchpoint
2d134ed3
PA
5458 bp_catchpoint
5459
5460*/
e4f237da
KB
5461
5462static int
5463breakpoint_address_is_meaningful (struct breakpoint *bpt)
5464{
5465 enum bptype type = bpt->type;
5466
2d134ed3
PA
5467 return (type != bp_watchpoint && type != bp_catchpoint);
5468}
5469
5470/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5471 true if LOC1 and LOC2 represent the same watchpoint location. */
5472
5473static int
4a64f543
MS
5474watchpoint_locations_match (struct bp_location *loc1,
5475 struct bp_location *loc2)
2d134ed3 5476{
2bdf28a0
JK
5477 /* Both of them must not be in moribund_locations. */
5478 gdb_assert (loc1->owner != NULL);
5479 gdb_assert (loc2->owner != NULL);
5480
4a64f543
MS
5481 /* If the target can evaluate the condition expression in hardware,
5482 then we we need to insert both watchpoints even if they are at
5483 the same place. Otherwise the watchpoint will only trigger when
5484 the condition of whichever watchpoint was inserted evaluates to
5485 true, not giving a chance for GDB to check the condition of the
5486 other watchpoint. */
0cf6dd15 5487 if ((loc1->owner->cond_exp
4a64f543
MS
5488 && target_can_accel_watchpoint_condition (loc1->address,
5489 loc1->length,
0cf6dd15
TJB
5490 loc1->watchpoint_type,
5491 loc1->owner->cond_exp))
5492 || (loc2->owner->cond_exp
4a64f543
MS
5493 && target_can_accel_watchpoint_condition (loc2->address,
5494 loc2->length,
0cf6dd15
TJB
5495 loc2->watchpoint_type,
5496 loc2->owner->cond_exp)))
5497 return 0;
5498
85d721b8
PA
5499 /* Note that this checks the owner's type, not the location's. In
5500 case the target does not support read watchpoints, but does
5501 support access watchpoints, we'll have bp_read_watchpoint
5502 watchpoints with hw_access locations. Those should be considered
5503 duplicates of hw_read locations. The hw_read locations will
5504 become hw_access locations later. */
2d134ed3
PA
5505 return (loc1->owner->type == loc2->owner->type
5506 && loc1->pspace->aspace == loc2->pspace->aspace
5507 && loc1->address == loc2->address
5508 && loc1->length == loc2->length);
e4f237da
KB
5509}
5510
6c95b8df
PA
5511/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5512 same breakpoint location. In most targets, this can only be true
5513 if ASPACE1 matches ASPACE2. On targets that have global
5514 breakpoints, the address space doesn't really matter. */
5515
5516static int
5517breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5518 struct address_space *aspace2, CORE_ADDR addr2)
5519{
5520 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5521 || aspace1 == aspace2)
5522 && addr1 == addr2);
5523}
5524
f1310107
TJB
5525/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5526 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5527 matches ASPACE2. On targets that have global breakpoints, the address
5528 space doesn't really matter. */
5529
5530static int
5531breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5532 int len1, struct address_space *aspace2,
5533 CORE_ADDR addr2)
5534{
5535 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5536 || aspace1 == aspace2)
5537 && addr2 >= addr1 && addr2 < addr1 + len1);
5538}
5539
5540/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5541 a ranged breakpoint. In most targets, a match happens only if ASPACE
5542 matches the breakpoint's address space. On targets that have global
5543 breakpoints, the address space doesn't really matter. */
5544
5545static int
5546breakpoint_location_address_match (struct bp_location *bl,
5547 struct address_space *aspace,
5548 CORE_ADDR addr)
5549{
5550 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5551 aspace, addr)
5552 || (bl->length
5553 && breakpoint_address_match_range (bl->pspace->aspace,
5554 bl->address, bl->length,
5555 aspace, addr)));
5556}
5557
2d134ed3
PA
5558/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5559 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5560 represent the same location. */
5561
5562static int
4a64f543
MS
5563breakpoint_locations_match (struct bp_location *loc1,
5564 struct bp_location *loc2)
2d134ed3 5565{
2bdf28a0
JK
5566 int hw_point1, hw_point2;
5567
5568 /* Both of them must not be in moribund_locations. */
5569 gdb_assert (loc1->owner != NULL);
5570 gdb_assert (loc2->owner != NULL);
5571
5572 hw_point1 = is_hardware_watchpoint (loc1->owner);
5573 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5574
5575 if (hw_point1 != hw_point2)
5576 return 0;
5577 else if (hw_point1)
5578 return watchpoint_locations_match (loc1, loc2);
5579 else
f1310107
TJB
5580 /* We compare bp_location.length in order to cover ranged breakpoints. */
5581 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5582 loc2->pspace->aspace, loc2->address)
5583 && loc1->length == loc2->length);
2d134ed3
PA
5584}
5585
76897487
KB
5586static void
5587breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5588 int bnum, int have_bnum)
5589{
f63fbe86
MS
5590 /* The longest string possibly returned by hex_string_custom
5591 is 50 chars. These must be at least that big for safety. */
5592 char astr1[64];
5593 char astr2[64];
76897487 5594
bb599908
PH
5595 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5596 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5597 if (have_bnum)
8a3fe4f8 5598 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5599 bnum, astr1, astr2);
5600 else
8a3fe4f8 5601 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5602}
5603
4a64f543
MS
5604/* Adjust a breakpoint's address to account for architectural
5605 constraints on breakpoint placement. Return the adjusted address.
5606 Note: Very few targets require this kind of adjustment. For most
5607 targets, this function is simply the identity function. */
76897487
KB
5608
5609static CORE_ADDR
a6d9a66e
UW
5610adjust_breakpoint_address (struct gdbarch *gdbarch,
5611 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5612{
a6d9a66e 5613 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5614 {
5615 /* Very few targets need any kind of breakpoint adjustment. */
5616 return bpaddr;
5617 }
88f7da05
KB
5618 else if (bptype == bp_watchpoint
5619 || bptype == bp_hardware_watchpoint
5620 || bptype == bp_read_watchpoint
5621 || bptype == bp_access_watchpoint
fe798b75 5622 || bptype == bp_catchpoint)
88f7da05
KB
5623 {
5624 /* Watchpoints and the various bp_catch_* eventpoints should not
5625 have their addresses modified. */
5626 return bpaddr;
5627 }
76897487
KB
5628 else
5629 {
5630 CORE_ADDR adjusted_bpaddr;
5631
5632 /* Some targets have architectural constraints on the placement
5633 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5634 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5635
5636 /* An adjusted breakpoint address can significantly alter
5637 a user's expectations. Print a warning if an adjustment
5638 is required. */
5639 if (adjusted_bpaddr != bpaddr)
5640 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5641
5642 return adjusted_bpaddr;
5643 }
5644}
5645
7cc221ef
DJ
5646/* Allocate a struct bp_location. */
5647
26bb91f3 5648static struct bp_location *
39d61571 5649allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5650{
afe38095 5651 struct bp_location *loc;
7cc221ef
DJ
5652
5653 loc = xmalloc (sizeof (struct bp_location));
5654 memset (loc, 0, sizeof (*loc));
5655
e049a4b5 5656 loc->owner = bpt;
511a6cd4 5657 loc->cond = NULL;
0d381245
VP
5658 loc->shlib_disabled = 0;
5659 loc->enabled = 1;
e049a4b5 5660
39d61571 5661 switch (bpt->type)
e049a4b5
DJ
5662 {
5663 case bp_breakpoint:
5664 case bp_until:
5665 case bp_finish:
5666 case bp_longjmp:
5667 case bp_longjmp_resume:
186c406b
TT
5668 case bp_exception:
5669 case bp_exception_resume:
e049a4b5 5670 case bp_step_resume:
e049a4b5
DJ
5671 case bp_watchpoint_scope:
5672 case bp_call_dummy:
aa7d318d 5673 case bp_std_terminate:
e049a4b5
DJ
5674 case bp_shlib_event:
5675 case bp_thread_event:
5676 case bp_overlay_event:
4efc6507 5677 case bp_jit_event:
0fd8e87f 5678 case bp_longjmp_master:
aa7d318d 5679 case bp_std_terminate_master:
186c406b 5680 case bp_exception_master:
0e30163f
JK
5681 case bp_gnu_ifunc_resolver:
5682 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5683 loc->loc_type = bp_loc_software_breakpoint;
5684 break;
5685 case bp_hardware_breakpoint:
5686 loc->loc_type = bp_loc_hardware_breakpoint;
5687 break;
5688 case bp_hardware_watchpoint:
5689 case bp_read_watchpoint:
5690 case bp_access_watchpoint:
5691 loc->loc_type = bp_loc_hardware_watchpoint;
5692 break;
5693 case bp_watchpoint:
ce78b96d 5694 case bp_catchpoint:
15c3d785
PA
5695 case bp_tracepoint:
5696 case bp_fast_tracepoint:
0fb4aa4b 5697 case bp_static_tracepoint:
e049a4b5
DJ
5698 loc->loc_type = bp_loc_other;
5699 break;
5700 default:
e2e0b3e5 5701 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5702 }
5703
f431efe5 5704 loc->refc = 1;
7cc221ef
DJ
5705 return loc;
5706}
5707
f431efe5
PA
5708static void
5709free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5710{
5711 if (loc->cond)
5712 xfree (loc->cond);
74960c60
VP
5713
5714 if (loc->function_name)
5715 xfree (loc->function_name);
f431efe5 5716
fe3f5fa8
VP
5717 xfree (loc);
5718}
5719
f431efe5
PA
5720/* Increment reference count. */
5721
5722static void
5723incref_bp_location (struct bp_location *bl)
5724{
5725 ++bl->refc;
5726}
5727
5728/* Decrement reference count. If the reference count reaches 0,
5729 destroy the bp_location. Sets *BLP to NULL. */
5730
5731static void
5732decref_bp_location (struct bp_location **blp)
5733{
0807b50c
PA
5734 gdb_assert ((*blp)->refc > 0);
5735
f431efe5
PA
5736 if (--(*blp)->refc == 0)
5737 free_bp_location (*blp);
5738 *blp = NULL;
5739}
5740
4a64f543
MS
5741/* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5742 type BPTYPE and has no locations as yet. */
5743/* This function is used in gdbtk sources and thus can not be made
5744 static. */
c906108c 5745
c40e75cd 5746static struct breakpoint *
a6d9a66e
UW
5747set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5748 enum bptype bptype)
c906108c 5749{
52f0bd74 5750 struct breakpoint *b, *b1;
c906108c
SS
5751
5752 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5753 memset (b, 0, sizeof (*b));
2219d63c 5754
4d28f7a8 5755 b->type = bptype;
a6d9a66e 5756 b->gdbarch = gdbarch;
c906108c
SS
5757 b->language = current_language->la_language;
5758 b->input_radix = input_radix;
5759 b->thread = -1;
b5de0fa7 5760 b->enable_state = bp_enabled;
c906108c
SS
5761 b->next = 0;
5762 b->silent = 0;
5763 b->ignore_count = 0;
5764 b->commands = NULL;
818dd999 5765 b->frame_id = null_frame_id;
3a3e9ee3 5766 b->forked_inferior_pid = null_ptid;
c906108c 5767 b->exec_pathname = NULL;
a96d9b2e 5768 b->syscalls_to_be_caught = NULL;
3086aeae 5769 b->ops = NULL;
0d381245 5770 b->condition_not_parsed = 0;
84f4c1fe 5771 b->py_bp_object = NULL;
d0fb5eae 5772 b->related_breakpoint = b;
c906108c 5773
4a64f543
MS
5774 /* Add this breakpoint to the end of the chain so that a list of
5775 breakpoints will come out in order of increasing numbers. */
c906108c
SS
5776
5777 b1 = breakpoint_chain;
5778 if (b1 == 0)
5779 breakpoint_chain = b;
5780 else
5781 {
5782 while (b1->next)
5783 b1 = b1->next;
5784 b1->next = b;
5785 }
0d381245
VP
5786 return b;
5787}
5788
0e30163f
JK
5789/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5790 resolutions should be made as the user specified the location explicitly
5791 enough. */
5792
0d381245 5793static void
0e30163f 5794set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5795{
2bdf28a0
JK
5796 gdb_assert (loc->owner != NULL);
5797
0d381245 5798 if (loc->owner->type == bp_breakpoint
1042e4c0 5799 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5800 || is_tracepoint (loc->owner))
0d381245 5801 {
0e30163f
JK
5802 int is_gnu_ifunc;
5803
5804 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5805 NULL, NULL, &is_gnu_ifunc);
5806
5807 if (is_gnu_ifunc && !explicit_loc)
5808 {
5809 struct breakpoint *b = loc->owner;
5810
5811 gdb_assert (loc->pspace == current_program_space);
5812 if (gnu_ifunc_resolve_name (loc->function_name,
5813 &loc->requested_address))
5814 {
5815 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5816 loc->address = adjust_breakpoint_address (loc->gdbarch,
5817 loc->requested_address,
5818 b->type);
5819 }
5820 else if (b->type == bp_breakpoint && b->loc == loc
5821 && loc->next == NULL && b->related_breakpoint == b)
5822 {
5823 /* Create only the whole new breakpoint of this type but do not
5824 mess more complicated breakpoints with multiple locations. */
5825 b->type = bp_gnu_ifunc_resolver;
5826 }
5827 }
5828
0d381245
VP
5829 if (loc->function_name)
5830 loc->function_name = xstrdup (loc->function_name);
5831 }
5832}
5833
a6d9a66e
UW
5834/* Attempt to determine architecture of location identified by SAL. */
5835static struct gdbarch *
5836get_sal_arch (struct symtab_and_line sal)
5837{
5838 if (sal.section)
5839 return get_objfile_arch (sal.section->objfile);
5840 if (sal.symtab)
5841 return get_objfile_arch (sal.symtab->objfile);
5842
5843 return NULL;
5844}
5845
0d381245
VP
5846/* set_raw_breakpoint is a low level routine for allocating and
5847 partially initializing a breakpoint of type BPTYPE. The newly
5848 created breakpoint's address, section, source file name, and line
5849 number are provided by SAL. The newly created and partially
5850 initialized breakpoint is added to the breakpoint chain and
5851 is also returned as the value of this function.
5852
5853 It is expected that the caller will complete the initialization of
5854 the newly created breakpoint struct as well as output any status
5855 information regarding the creation of a new breakpoint. In
5856 particular, set_raw_breakpoint does NOT set the breakpoint
5857 number! Care should be taken to not allow an error to occur
5858 prior to completing the initialization of the breakpoint. If this
5859 should happen, a bogus breakpoint will be left on the chain. */
5860
63c252f8 5861struct breakpoint *
a6d9a66e
UW
5862set_raw_breakpoint (struct gdbarch *gdbarch,
5863 struct symtab_and_line sal, enum bptype bptype)
0d381245 5864{
4a64f543
MS
5865 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5866 bptype);
0d381245 5867 CORE_ADDR adjusted_address;
a6d9a66e
UW
5868 struct gdbarch *loc_gdbarch;
5869
5870 loc_gdbarch = get_sal_arch (sal);
5871 if (!loc_gdbarch)
5872 loc_gdbarch = b->gdbarch;
0d381245 5873
6c95b8df
PA
5874 if (bptype != bp_catchpoint)
5875 gdb_assert (sal.pspace != NULL);
5876
0d381245
VP
5877 /* Adjust the breakpoint's address prior to allocating a location.
5878 Once we call allocate_bp_location(), that mostly uninitialized
5879 location will be placed on the location chain. Adjustment of the
8defab1a 5880 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5881 not want its scan of the location chain to find a breakpoint and
5882 location that's only been partially initialized. */
4a64f543
MS
5883 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5884 sal.pc, b->type);
0d381245 5885
39d61571 5886 b->loc = allocate_bp_location (b);
a6d9a66e 5887 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5888 b->loc->requested_address = sal.pc;
5889 b->loc->address = adjusted_address;
6c95b8df
PA
5890 b->loc->pspace = sal.pspace;
5891
5892 /* Store the program space that was used to set the breakpoint, for
5893 breakpoint resetting. */
5894 b->pspace = sal.pspace;
0d381245
VP
5895
5896 if (sal.symtab == NULL)
5897 b->source_file = NULL;
5898 else
1b36a34b 5899 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5900 b->loc->section = sal.section;
5901 b->line_number = sal.line;
5902
0e30163f
JK
5903 set_breakpoint_location_function (b->loc,
5904 sal.explicit_pc || sal.explicit_line);
c906108c 5905
c906108c
SS
5906 breakpoints_changed ();
5907
5908 return b;
5909}
5910
c2c6d25f
JM
5911
5912/* Note that the breakpoint object B describes a permanent breakpoint
5913 instruction, hard-wired into the inferior's code. */
5914void
5915make_breakpoint_permanent (struct breakpoint *b)
5916{
0d381245 5917 struct bp_location *bl;
cc59ec59 5918
b5de0fa7 5919 b->enable_state = bp_permanent;
c2c6d25f 5920
4a64f543
MS
5921 /* By definition, permanent breakpoints are already present in the
5922 code. Mark all locations as inserted. For now,
5923 make_breakpoint_permanent is called in just one place, so it's
5924 hard to say if it's reasonable to have permanent breakpoint with
5925 multiple locations or not, but it's easy to implmement. */
0d381245
VP
5926 for (bl = b->loc; bl; bl = bl->next)
5927 bl->inserted = 1;
c2c6d25f
JM
5928}
5929
53a5351d 5930/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5931 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5932 initiated the operation. */
c906108c
SS
5933
5934void
186c406b 5935set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5936{
35df4500 5937 struct breakpoint *b, *b_tmp;
186c406b 5938 int thread = tp->num;
0fd8e87f
UW
5939
5940 /* To avoid having to rescan all objfile symbols at every step,
5941 we maintain a list of continually-inserted but always disabled
5942 longjmp "master" breakpoints. Here, we simply create momentary
5943 clones of those and enable them for the requested thread. */
35df4500 5944 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5945 if (b->pspace == current_program_space
186c406b
TT
5946 && (b->type == bp_longjmp_master
5947 || b->type == bp_exception_master))
0fd8e87f
UW
5948 {
5949 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5950
186c406b 5951 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5952 clone->thread = thread;
5953 }
186c406b
TT
5954
5955 tp->initiating_frame = frame;
c906108c
SS
5956}
5957
611c83ae 5958/* Delete all longjmp breakpoints from THREAD. */
c906108c 5959void
611c83ae 5960delete_longjmp_breakpoint (int thread)
c906108c 5961{
35df4500 5962 struct breakpoint *b, *b_tmp;
c906108c 5963
35df4500 5964 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5965 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5966 {
5967 if (b->thread == thread)
5968 delete_breakpoint (b);
5969 }
c906108c
SS
5970}
5971
1900040c
MS
5972void
5973enable_overlay_breakpoints (void)
5974{
52f0bd74 5975 struct breakpoint *b;
1900040c
MS
5976
5977 ALL_BREAKPOINTS (b)
5978 if (b->type == bp_overlay_event)
5979 {
5980 b->enable_state = bp_enabled;
b60e7edf 5981 update_global_location_list (1);
c02f5703 5982 overlay_events_enabled = 1;
1900040c
MS
5983 }
5984}
5985
5986void
5987disable_overlay_breakpoints (void)
5988{
52f0bd74 5989 struct breakpoint *b;
1900040c
MS
5990
5991 ALL_BREAKPOINTS (b)
5992 if (b->type == bp_overlay_event)
5993 {
5994 b->enable_state = bp_disabled;
b60e7edf 5995 update_global_location_list (0);
c02f5703 5996 overlay_events_enabled = 0;
1900040c
MS
5997 }
5998}
5999
aa7d318d
TT
6000/* Set an active std::terminate breakpoint for each std::terminate
6001 master breakpoint. */
6002void
6003set_std_terminate_breakpoint (void)
6004{
35df4500 6005 struct breakpoint *b, *b_tmp;
aa7d318d 6006
35df4500 6007 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6008 if (b->pspace == current_program_space
6009 && b->type == bp_std_terminate_master)
6010 {
6011 struct breakpoint *clone = clone_momentary_breakpoint (b);
6012 clone->type = bp_std_terminate;
6013 }
6014}
6015
6016/* Delete all the std::terminate breakpoints. */
6017void
6018delete_std_terminate_breakpoint (void)
6019{
35df4500 6020 struct breakpoint *b, *b_tmp;
aa7d318d 6021
35df4500 6022 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6023 if (b->type == bp_std_terminate)
6024 delete_breakpoint (b);
6025}
6026
c4093a6a 6027struct breakpoint *
a6d9a66e 6028create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6029{
6030 struct breakpoint *b;
c4093a6a 6031
a6d9a66e 6032 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 6033
b5de0fa7 6034 b->enable_state = bp_enabled;
c4093a6a 6035 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6036 b->addr_string
6037 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6038
b60e7edf 6039 update_global_location_list_nothrow (1);
74960c60 6040
c4093a6a
JM
6041 return b;
6042}
6043
6044void
6045remove_thread_event_breakpoints (void)
6046{
35df4500 6047 struct breakpoint *b, *b_tmp;
c4093a6a 6048
35df4500 6049 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6050 if (b->type == bp_thread_event
6051 && b->loc->pspace == current_program_space)
c4093a6a
JM
6052 delete_breakpoint (b);
6053}
6054
0101ce28
JJ
6055struct lang_and_radix
6056 {
6057 enum language lang;
6058 int radix;
6059 };
6060
4efc6507
DE
6061/* Create a breakpoint for JIT code registration and unregistration. */
6062
6063struct breakpoint *
6064create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6065{
6066 struct breakpoint *b;
6067
6068 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6069 update_global_location_list_nothrow (1);
6070 return b;
6071}
0101ce28 6072
03673fc7
PP
6073/* Remove JIT code registration and unregistration breakpoint(s). */
6074
6075void
6076remove_jit_event_breakpoints (void)
6077{
6078 struct breakpoint *b, *b_tmp;
6079
6080 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6081 if (b->type == bp_jit_event
6082 && b->loc->pspace == current_program_space)
6083 delete_breakpoint (b);
6084}
6085
cae688ec
JJ
6086void
6087remove_solib_event_breakpoints (void)
6088{
35df4500 6089 struct breakpoint *b, *b_tmp;
cae688ec 6090
35df4500 6091 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6092 if (b->type == bp_shlib_event
6093 && b->loc->pspace == current_program_space)
cae688ec
JJ
6094 delete_breakpoint (b);
6095}
6096
6097struct breakpoint *
a6d9a66e 6098create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6099{
6100 struct breakpoint *b;
6101
a6d9a66e 6102 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 6103 update_global_location_list_nothrow (1);
cae688ec
JJ
6104 return b;
6105}
6106
6107/* Disable any breakpoints that are on code in shared libraries. Only
6108 apply to enabled breakpoints, disabled ones can just stay disabled. */
6109
6110void
cb851954 6111disable_breakpoints_in_shlibs (void)
cae688ec 6112{
876fa593 6113 struct bp_location *loc, **locp_tmp;
cae688ec 6114
876fa593 6115 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6116 {
2bdf28a0 6117 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6118 struct breakpoint *b = loc->owner;
2bdf28a0 6119
4a64f543
MS
6120 /* We apply the check to all breakpoints, including disabled for
6121 those with loc->duplicate set. This is so that when breakpoint
6122 becomes enabled, or the duplicate is removed, gdb will try to
6123 insert all breakpoints. If we don't set shlib_disabled here,
6124 we'll try to insert those breakpoints and fail. */
1042e4c0 6125 if (((b->type == bp_breakpoint)
508ccb1f 6126 || (b->type == bp_jit_event)
1042e4c0 6127 || (b->type == bp_hardware_breakpoint)
d77f58be 6128 || (is_tracepoint (b)))
6c95b8df 6129 && loc->pspace == current_program_space
0d381245 6130 && !loc->shlib_disabled
a77053c2 6131#ifdef PC_SOLIB
0d381245 6132 && PC_SOLIB (loc->address)
a77053c2 6133#else
6c95b8df 6134 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6135#endif
6136 )
0d381245
VP
6137 {
6138 loc->shlib_disabled = 1;
6139 }
cae688ec
JJ
6140 }
6141}
6142
7a9dd1b2 6143/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
6144 Only apply to enabled breakpoints, disabled ones can just stay
6145 disabled. */
84acb35a 6146
75149521 6147static void
84acb35a
JJ
6148disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6149{
876fa593 6150 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6151 int disabled_shlib_breaks = 0;
6152
c86cf029
VP
6153 /* SunOS a.out shared libraries are always mapped, so do not
6154 disable breakpoints; they will only be reported as unloaded
6155 through clear_solib when GDB discards its shared library
6156 list. See clear_solib for more information. */
6157 if (exec_bfd != NULL
6158 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6159 return;
6160
876fa593 6161 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6162 {
2bdf28a0 6163 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6164 struct breakpoint *b = loc->owner;
cc59ec59 6165
0d381245
VP
6166 if ((loc->loc_type == bp_loc_hardware_breakpoint
6167 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6168 && solib->pspace == loc->pspace
e2dd7057 6169 && !loc->shlib_disabled
508ccb1f
TT
6170 && (b->type == bp_breakpoint
6171 || b->type == bp_jit_event
6172 || b->type == bp_hardware_breakpoint)
e2dd7057 6173 && solib_contains_address_p (solib, loc->address))
84acb35a 6174 {
e2dd7057
PP
6175 loc->shlib_disabled = 1;
6176 /* At this point, we cannot rely on remove_breakpoint
6177 succeeding so we must mark the breakpoint as not inserted
6178 to prevent future errors occurring in remove_breakpoints. */
6179 loc->inserted = 0;
8d3788bd
VP
6180
6181 /* This may cause duplicate notifications for the same breakpoint. */
6182 observer_notify_breakpoint_modified (b);
6183
e2dd7057
PP
6184 if (!disabled_shlib_breaks)
6185 {
6186 target_terminal_ours_for_output ();
3e43a32a
MS
6187 warning (_("Temporarily disabling breakpoints "
6188 "for unloaded shared library \"%s\""),
e2dd7057 6189 solib->so_name);
84acb35a 6190 }
e2dd7057 6191 disabled_shlib_breaks = 1;
84acb35a
JJ
6192 }
6193 }
84acb35a
JJ
6194}
6195
ce78b96d
JB
6196/* FORK & VFORK catchpoints. */
6197
4a64f543
MS
6198/* Implement the "insert" breakpoint_ops method for fork
6199 catchpoints. */
ce78b96d 6200
77b06cd7
TJB
6201static int
6202insert_catch_fork (struct bp_location *bl)
ce78b96d 6203{
77b06cd7 6204 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6205}
6206
4a64f543
MS
6207/* Implement the "remove" breakpoint_ops method for fork
6208 catchpoints. */
ce78b96d
JB
6209
6210static int
77b06cd7 6211remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6212{
6213 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6214}
6215
6216/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6217 catchpoints. */
6218
6219static int
f1310107
TJB
6220breakpoint_hit_catch_fork (const struct bp_location *bl,
6221 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6222{
f1310107 6223 return inferior_has_forked (inferior_ptid, &bl->owner->forked_inferior_pid);
ce78b96d
JB
6224}
6225
4a64f543
MS
6226/* Implement the "print_it" breakpoint_ops method for fork
6227 catchpoints. */
ce78b96d
JB
6228
6229static enum print_stop_action
6230print_it_catch_fork (struct breakpoint *b)
6231{
6232 annotate_catchpoint (b->number);
6233 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6234 b->number, ptid_get_pid (b->forked_inferior_pid));
6235 return PRINT_SRC_AND_LOC;
6236}
6237
4a64f543
MS
6238/* Implement the "print_one" breakpoint_ops method for fork
6239 catchpoints. */
ce78b96d
JB
6240
6241static void
a6d9a66e 6242print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6243{
79a45b7d
TT
6244 struct value_print_options opts;
6245
6246 get_user_print_options (&opts);
6247
4a64f543
MS
6248 /* Field 4, the address, is omitted (which makes the columns not
6249 line up too nicely with the headers, but the effect is relatively
6250 readable). */
79a45b7d 6251 if (opts.addressprint)
ce78b96d
JB
6252 ui_out_field_skip (uiout, "addr");
6253 annotate_field (5);
6254 ui_out_text (uiout, "fork");
6255 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6256 {
6257 ui_out_text (uiout, ", process ");
6258 ui_out_field_int (uiout, "what",
6259 ptid_get_pid (b->forked_inferior_pid));
6260 ui_out_spaces (uiout, 1);
6261 }
6262}
6263
6264/* Implement the "print_mention" breakpoint_ops method for fork
6265 catchpoints. */
6266
6267static void
6268print_mention_catch_fork (struct breakpoint *b)
6269{
6270 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6271}
6272
6149aea9
PA
6273/* Implement the "print_recreate" breakpoint_ops method for fork
6274 catchpoints. */
6275
6276static void
6277print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6278{
6279 fprintf_unfiltered (fp, "catch fork");
6280}
6281
ce78b96d
JB
6282/* The breakpoint_ops structure to be used in fork catchpoints. */
6283
6284static struct breakpoint_ops catch_fork_breakpoint_ops =
6285{
6286 insert_catch_fork,
6287 remove_catch_fork,
6288 breakpoint_hit_catch_fork,
e09342b5 6289 NULL, /* resources_needed */
ce78b96d
JB
6290 print_it_catch_fork,
6291 print_one_catch_fork,
f1310107 6292 NULL, /* print_one_detail */
6149aea9
PA
6293 print_mention_catch_fork,
6294 print_recreate_catch_fork
ce78b96d
JB
6295};
6296
4a64f543
MS
6297/* Implement the "insert" breakpoint_ops method for vfork
6298 catchpoints. */
ce78b96d 6299
77b06cd7
TJB
6300static int
6301insert_catch_vfork (struct bp_location *bl)
ce78b96d 6302{
77b06cd7 6303 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6304}
6305
4a64f543
MS
6306/* Implement the "remove" breakpoint_ops method for vfork
6307 catchpoints. */
ce78b96d
JB
6308
6309static int
77b06cd7 6310remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6311{
6312 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6313}
6314
6315/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6316 catchpoints. */
6317
6318static int
f1310107
TJB
6319breakpoint_hit_catch_vfork (const struct bp_location *bl,
6320 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6321{
f1310107 6322 return inferior_has_vforked (inferior_ptid, &bl->owner->forked_inferior_pid);
ce78b96d
JB
6323}
6324
4a64f543
MS
6325/* Implement the "print_it" breakpoint_ops method for vfork
6326 catchpoints. */
ce78b96d
JB
6327
6328static enum print_stop_action
6329print_it_catch_vfork (struct breakpoint *b)
6330{
6331 annotate_catchpoint (b->number);
6332 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6333 b->number, ptid_get_pid (b->forked_inferior_pid));
6334 return PRINT_SRC_AND_LOC;
6335}
6336
4a64f543
MS
6337/* Implement the "print_one" breakpoint_ops method for vfork
6338 catchpoints. */
ce78b96d
JB
6339
6340static void
a6d9a66e 6341print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6342{
79a45b7d
TT
6343 struct value_print_options opts;
6344
6345 get_user_print_options (&opts);
4a64f543
MS
6346 /* Field 4, the address, is omitted (which makes the columns not
6347 line up too nicely with the headers, but the effect is relatively
6348 readable). */
79a45b7d 6349 if (opts.addressprint)
ce78b96d
JB
6350 ui_out_field_skip (uiout, "addr");
6351 annotate_field (5);
6352 ui_out_text (uiout, "vfork");
6353 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6354 {
6355 ui_out_text (uiout, ", process ");
6356 ui_out_field_int (uiout, "what",
6357 ptid_get_pid (b->forked_inferior_pid));
6358 ui_out_spaces (uiout, 1);
6359 }
6360}
6361
6362/* Implement the "print_mention" breakpoint_ops method for vfork
6363 catchpoints. */
6364
6365static void
6366print_mention_catch_vfork (struct breakpoint *b)
6367{
6368 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6369}
6370
6149aea9
PA
6371/* Implement the "print_recreate" breakpoint_ops method for vfork
6372 catchpoints. */
6373
6374static void
6375print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6376{
6377 fprintf_unfiltered (fp, "catch vfork");
6378}
6379
ce78b96d
JB
6380/* The breakpoint_ops structure to be used in vfork catchpoints. */
6381
6382static struct breakpoint_ops catch_vfork_breakpoint_ops =
6383{
6384 insert_catch_vfork,
6385 remove_catch_vfork,
6386 breakpoint_hit_catch_vfork,
e09342b5 6387 NULL, /* resources_needed */
ce78b96d
JB
6388 print_it_catch_vfork,
6389 print_one_catch_vfork,
f1310107 6390 NULL, /* print_one_detail */
6149aea9
PA
6391 print_mention_catch_vfork,
6392 print_recreate_catch_vfork
ce78b96d
JB
6393};
6394
a96d9b2e
SDJ
6395/* Implement the "insert" breakpoint_ops method for syscall
6396 catchpoints. */
6397
77b06cd7
TJB
6398static int
6399insert_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6400{
6401 struct inferior *inf = current_inferior ();
6402
6403 ++inf->total_syscalls_count;
77b06cd7 6404 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6405 ++inf->any_syscall_count;
6406 else
6407 {
6408 int i, iter;
cc59ec59 6409
a96d9b2e 6410 for (i = 0;
77b06cd7 6411 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6412 i++)
6413 {
6414 int elem;
cc59ec59 6415
a96d9b2e
SDJ
6416 if (iter >= VEC_length (int, inf->syscalls_counts))
6417 {
6418 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6419 uintptr_t vec_addr_offset
6420 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6421 uintptr_t vec_addr;
6422 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6423 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6424 vec_addr_offset;
6425 memset ((void *) vec_addr, 0,
6426 (iter + 1 - old_size) * sizeof (int));
6427 }
6428 elem = VEC_index (int, inf->syscalls_counts, iter);
6429 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6430 }
6431 }
6432
77b06cd7
TJB
6433 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6434 inf->total_syscalls_count != 0,
6435 inf->any_syscall_count,
6436 VEC_length (int, inf->syscalls_counts),
6437 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6438}
6439
6440/* Implement the "remove" breakpoint_ops method for syscall
6441 catchpoints. */
6442
6443static int
77b06cd7 6444remove_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6445{
6446 struct inferior *inf = current_inferior ();
6447
6448 --inf->total_syscalls_count;
77b06cd7 6449 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6450 --inf->any_syscall_count;
6451 else
6452 {
6453 int i, iter;
cc59ec59 6454
a96d9b2e 6455 for (i = 0;
77b06cd7 6456 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6457 i++)
6458 {
6459 int elem;
6460 if (iter >= VEC_length (int, inf->syscalls_counts))
6461 /* Shouldn't happen. */
6462 continue;
6463 elem = VEC_index (int, inf->syscalls_counts, iter);
6464 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6465 }
6466 }
6467
6468 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6469 inf->total_syscalls_count != 0,
6470 inf->any_syscall_count,
6471 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6472 VEC_address (int,
6473 inf->syscalls_counts));
a96d9b2e
SDJ
6474}
6475
6476/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6477 catchpoints. */
6478
6479static int
f1310107
TJB
6480breakpoint_hit_catch_syscall (const struct bp_location *bl,
6481 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6482{
4a64f543
MS
6483 /* We must check if we are catching specific syscalls in this
6484 breakpoint. If we are, then we must guarantee that the called
6485 syscall is the same syscall we are catching. */
a96d9b2e 6486 int syscall_number = 0;
f1310107 6487 const struct breakpoint *b = bl->owner;
a96d9b2e
SDJ
6488
6489 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6490 return 0;
6491
6492 /* Now, checking if the syscall is the same. */
6493 if (b->syscalls_to_be_caught)
6494 {
6495 int i, iter;
cc59ec59 6496
a96d9b2e
SDJ
6497 for (i = 0;
6498 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6499 i++)
6500 if (syscall_number == iter)
6501 break;
6502 /* Not the same. */
6503 if (!iter)
6504 return 0;
6505 }
6506
6507 return 1;
6508}
6509
6510/* Implement the "print_it" breakpoint_ops method for syscall
6511 catchpoints. */
6512
6513static enum print_stop_action
6514print_it_catch_syscall (struct breakpoint *b)
6515{
6516 /* These are needed because we want to know in which state a
6517 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6518 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6519 must print "called syscall" or "returned from syscall". */
6520 ptid_t ptid;
6521 struct target_waitstatus last;
6522 struct syscall s;
6523 struct cleanup *old_chain;
6524 char *syscall_id;
6525
6526 get_last_target_status (&ptid, &last);
6527
6528 get_syscall_by_number (last.value.syscall_number, &s);
6529
6530 annotate_catchpoint (b->number);
6531
6532 if (s.name == NULL)
6533 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6534 else
6535 syscall_id = xstrprintf ("'%s'", s.name);
6536
6537 old_chain = make_cleanup (xfree, syscall_id);
6538
6539 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6540 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6541 b->number, syscall_id);
6542 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6543 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6544 b->number, syscall_id);
6545
6546 do_cleanups (old_chain);
6547
6548 return PRINT_SRC_AND_LOC;
6549}
6550
6551/* Implement the "print_one" breakpoint_ops method for syscall
6552 catchpoints. */
6553
6554static void
6555print_one_catch_syscall (struct breakpoint *b,
f1310107 6556 struct bp_location **last_loc)
a96d9b2e
SDJ
6557{
6558 struct value_print_options opts;
6559
6560 get_user_print_options (&opts);
4a64f543
MS
6561 /* Field 4, the address, is omitted (which makes the columns not
6562 line up too nicely with the headers, but the effect is relatively
6563 readable). */
a96d9b2e
SDJ
6564 if (opts.addressprint)
6565 ui_out_field_skip (uiout, "addr");
6566 annotate_field (5);
6567
6568 if (b->syscalls_to_be_caught
6569 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6570 ui_out_text (uiout, "syscalls \"");
6571 else
6572 ui_out_text (uiout, "syscall \"");
6573
6574 if (b->syscalls_to_be_caught)
6575 {
6576 int i, iter;
6577 char *text = xstrprintf ("%s", "");
cc59ec59 6578
a96d9b2e
SDJ
6579 for (i = 0;
6580 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6581 i++)
6582 {
6583 char *x = text;
6584 struct syscall s;
6585 get_syscall_by_number (iter, &s);
6586
6587 if (s.name != NULL)
6588 text = xstrprintf ("%s%s, ", text, s.name);
6589 else
6590 text = xstrprintf ("%s%d, ", text, iter);
6591
6592 /* We have to xfree the last 'text' (now stored at 'x')
6593 because xstrprintf dinamically allocates new space for it
6594 on every call. */
6595 xfree (x);
6596 }
6597 /* Remove the last comma. */
6598 text[strlen (text) - 2] = '\0';
6599 ui_out_field_string (uiout, "what", text);
6600 }
6601 else
6602 ui_out_field_string (uiout, "what", "<any syscall>");
6603 ui_out_text (uiout, "\" ");
6604}
6605
6606/* Implement the "print_mention" breakpoint_ops method for syscall
6607 catchpoints. */
6608
6609static void
6610print_mention_catch_syscall (struct breakpoint *b)
6611{
6612 if (b->syscalls_to_be_caught)
6613 {
6614 int i, iter;
6615
6616 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6617 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6618 else
6619 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6620
6621 for (i = 0;
6622 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6623 i++)
6624 {
6625 struct syscall s;
6626 get_syscall_by_number (iter, &s);
6627
6628 if (s.name)
6629 printf_filtered (" '%s' [%d]", s.name, s.number);
6630 else
6631 printf_filtered (" %d", s.number);
6632 }
6633 printf_filtered (")");
6634 }
6635 else
6636 printf_filtered (_("Catchpoint %d (any syscall)"),
6637 b->number);
6638}
6639
6149aea9
PA
6640/* Implement the "print_recreate" breakpoint_ops method for syscall
6641 catchpoints. */
6642
6643static void
6644print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6645{
6646 fprintf_unfiltered (fp, "catch syscall");
6647
6648 if (b->syscalls_to_be_caught)
6649 {
6650 int i, iter;
6651
6652 for (i = 0;
6653 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6654 i++)
6655 {
6656 struct syscall s;
6657
6658 get_syscall_by_number (iter, &s);
6659 if (s.name)
6660 fprintf_unfiltered (fp, " %s", s.name);
6661 else
6662 fprintf_unfiltered (fp, " %d", s.number);
6663 }
6664 }
6665}
6666
a96d9b2e
SDJ
6667/* The breakpoint_ops structure to be used in syscall catchpoints. */
6668
6669static struct breakpoint_ops catch_syscall_breakpoint_ops =
6670{
6671 insert_catch_syscall,
6672 remove_catch_syscall,
6673 breakpoint_hit_catch_syscall,
e09342b5 6674 NULL, /* resources_needed */
a96d9b2e
SDJ
6675 print_it_catch_syscall,
6676 print_one_catch_syscall,
f1310107 6677 NULL, /* print_one_detail */
6149aea9
PA
6678 print_mention_catch_syscall,
6679 print_recreate_catch_syscall
a96d9b2e
SDJ
6680};
6681
6682/* Returns non-zero if 'b' is a syscall catchpoint. */
6683
6684static int
6685syscall_catchpoint_p (struct breakpoint *b)
6686{
6687 return (b->ops == &catch_syscall_breakpoint_ops);
6688}
6689
6690/* Create a new breakpoint of the bp_catchpoint kind and return it,
6691 but does NOT mention it nor update the global location list.
6692 This is useful if you need to fill more fields in the
6693 struct breakpoint before calling mention.
ce78b96d
JB
6694
6695 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6696 If COND_STRING is not NULL, then store it in the breakpoint.
6697 OPS, if not NULL, is the breakpoint_ops structure associated
6698 to the catchpoint. */
6699
6700static struct breakpoint *
a96d9b2e
SDJ
6701create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6702 char *cond_string,
6703 struct breakpoint_ops *ops)
c906108c 6704{
c5aa993b
JM
6705 struct symtab_and_line sal;
6706 struct breakpoint *b;
c5aa993b 6707
fe39c653 6708 init_sal (&sal);
6c95b8df 6709 sal.pspace = current_program_space;
c5aa993b 6710
a6d9a66e 6711 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6712 set_breakpoint_count (breakpoint_count + 1);
6713 b->number = breakpoint_count;
ce78b96d 6714
1b36a34b 6715 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6716 b->thread = -1;
c906108c 6717 b->addr_string = NULL;
b5de0fa7
EZ
6718 b->enable_state = bp_enabled;
6719 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6720 b->ops = ops;
c5aa993b 6721
a96d9b2e
SDJ
6722 return b;
6723}
6724
6725/* Create a new breakpoint of the bp_catchpoint kind and return it.
6726
6727 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6728 If COND_STRING is not NULL, then store it in the breakpoint.
6729 OPS, if not NULL, is the breakpoint_ops structure associated
6730 to the catchpoint. */
6731
6732static struct breakpoint *
6733create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6734 char *cond_string, struct breakpoint_ops *ops)
6735{
6736 struct breakpoint *b =
6737 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6738
c906108c 6739 mention (b);
8d3788bd 6740 observer_notify_breakpoint_created (b);
ce78b96d 6741 update_global_location_list (1);
c906108c 6742
ce78b96d 6743 return b;
c906108c 6744}
c5aa993b 6745
9b70b993 6746static void
a6d9a66e
UW
6747create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6748 int tempflag, char *cond_string,
ce78b96d 6749 struct breakpoint_ops *ops)
c906108c 6750{
a6d9a66e
UW
6751 struct breakpoint *b
6752 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6753
6754 /* FIXME: We should put this information in a breakpoint private data
6755 area. */
6756 b->forked_inferior_pid = null_ptid;
c906108c
SS
6757}
6758
fe798b75
JB
6759/* Exec catchpoints. */
6760
77b06cd7
TJB
6761static int
6762insert_catch_exec (struct bp_location *bl)
c906108c 6763{
77b06cd7 6764 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6765}
c906108c 6766
fe798b75 6767static int
77b06cd7 6768remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6769{
6770 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6771}
c906108c 6772
fe798b75 6773static int
f1310107
TJB
6774breakpoint_hit_catch_exec (const struct bp_location *bl,
6775 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6776{
f1310107 6777 return inferior_has_execd (inferior_ptid, &bl->owner->exec_pathname);
fe798b75 6778}
c906108c 6779
fe798b75
JB
6780static enum print_stop_action
6781print_it_catch_exec (struct breakpoint *b)
6782{
6783 annotate_catchpoint (b->number);
6784 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6785 b->exec_pathname);
6786 return PRINT_SRC_AND_LOC;
c906108c
SS
6787}
6788
fe798b75 6789static void
a6d9a66e 6790print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6791{
6792 struct value_print_options opts;
6793
6794 get_user_print_options (&opts);
6795
6796 /* Field 4, the address, is omitted (which makes the columns
6797 not line up too nicely with the headers, but the effect
6798 is relatively readable). */
6799 if (opts.addressprint)
6800 ui_out_field_skip (uiout, "addr");
6801 annotate_field (5);
6802 ui_out_text (uiout, "exec");
6803 if (b->exec_pathname != NULL)
6804 {
6805 ui_out_text (uiout, ", program \"");
6806 ui_out_field_string (uiout, "what", b->exec_pathname);
6807 ui_out_text (uiout, "\" ");
6808 }
6809}
6810
6811static void
6812print_mention_catch_exec (struct breakpoint *b)
6813{
6814 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6815}
6816
6149aea9
PA
6817/* Implement the "print_recreate" breakpoint_ops method for exec
6818 catchpoints. */
6819
6820static void
6821print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6822{
6823 fprintf_unfiltered (fp, "catch exec");
6824}
6825
fe798b75
JB
6826static struct breakpoint_ops catch_exec_breakpoint_ops =
6827{
6828 insert_catch_exec,
6829 remove_catch_exec,
6830 breakpoint_hit_catch_exec,
e09342b5 6831 NULL, /* resources_needed */
fe798b75
JB
6832 print_it_catch_exec,
6833 print_one_catch_exec,
f1310107 6834 NULL, /* print_one_detail */
6149aea9
PA
6835 print_mention_catch_exec,
6836 print_recreate_catch_exec
fe798b75
JB
6837};
6838
a96d9b2e
SDJ
6839static void
6840create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6841 struct breakpoint_ops *ops)
6842{
6843 struct gdbarch *gdbarch = get_current_arch ();
6844 struct breakpoint *b =
6845 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6846
6847 b->syscalls_to_be_caught = filter;
6848
6849 /* Now, we have to mention the breakpoint and update the global
6850 location list. */
6851 mention (b);
8d3788bd 6852 observer_notify_breakpoint_created (b);
a96d9b2e
SDJ
6853 update_global_location_list (1);
6854}
6855
c906108c 6856static int
fba45db2 6857hw_breakpoint_used_count (void)
c906108c 6858{
c906108c 6859 int i = 0;
f1310107
TJB
6860 struct breakpoint *b;
6861 struct bp_location *bl;
c906108c
SS
6862
6863 ALL_BREAKPOINTS (b)
c5aa993b 6864 {
d6b74ac4 6865 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6866 for (bl = b->loc; bl; bl = bl->next)
6867 {
6868 /* Special types of hardware breakpoints may use more than
6869 one register. */
6870 if (b->ops && b->ops->resources_needed)
6871 i += b->ops->resources_needed (bl);
6872 else
6873 i++;
6874 }
c5aa993b 6875 }
c906108c
SS
6876
6877 return i;
6878}
6879
6880static int
fba45db2 6881hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6882{
c906108c 6883 int i = 0;
e09342b5
TJB
6884 struct breakpoint *b;
6885 struct bp_location *bl;
c906108c
SS
6886
6887 *other_type_used = 0;
6888 ALL_BREAKPOINTS (b)
e09342b5
TJB
6889 {
6890 if (!breakpoint_enabled (b))
6891 continue;
6892
c5aa993b 6893 if (b->type == type)
e09342b5
TJB
6894 for (bl = b->loc; bl; bl = bl->next)
6895 {
6896 /* Special types of hardware watchpoints may use more than
6897 one register. */
6898 if (b->ops && b->ops->resources_needed)
6899 i += b->ops->resources_needed (bl);
6900 else
6901 i++;
6902 }
cc60f2e3 6903 else if (is_hardware_watchpoint (b))
c5aa993b 6904 *other_type_used = 1;
e09342b5
TJB
6905 }
6906
c906108c
SS
6907 return i;
6908}
6909
c906108c 6910void
fba45db2 6911disable_watchpoints_before_interactive_call_start (void)
c906108c 6912{
c5aa993b 6913 struct breakpoint *b;
c906108c
SS
6914
6915 ALL_BREAKPOINTS (b)
c5aa993b 6916 {
cc60f2e3 6917 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6918 {
b5de0fa7 6919 b->enable_state = bp_call_disabled;
b60e7edf 6920 update_global_location_list (0);
c5aa993b
JM
6921 }
6922 }
c906108c
SS
6923}
6924
6925void
fba45db2 6926enable_watchpoints_after_interactive_call_stop (void)
c906108c 6927{
c5aa993b 6928 struct breakpoint *b;
c906108c
SS
6929
6930 ALL_BREAKPOINTS (b)
c5aa993b 6931 {
cc60f2e3 6932 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6933 {
b5de0fa7 6934 b->enable_state = bp_enabled;
b60e7edf 6935 update_global_location_list (1);
c5aa993b
JM
6936 }
6937 }
c906108c
SS
6938}
6939
8bea4e01
UW
6940void
6941disable_breakpoints_before_startup (void)
6942{
6943 struct breakpoint *b;
6944 int found = 0;
6945
6946 ALL_BREAKPOINTS (b)
6947 {
6c95b8df
PA
6948 if (b->pspace != current_program_space)
6949 continue;
6950
8bea4e01
UW
6951 if ((b->type == bp_breakpoint
6952 || b->type == bp_hardware_breakpoint)
6953 && breakpoint_enabled (b))
6954 {
6955 b->enable_state = bp_startup_disabled;
6956 found = 1;
6957 }
6958 }
6959
6960 if (found)
6961 update_global_location_list (0);
6962
6c95b8df 6963 current_program_space->executing_startup = 1;
8bea4e01
UW
6964}
6965
6966void
6967enable_breakpoints_after_startup (void)
6968{
6969 struct breakpoint *b;
6970 int found = 0;
6971
6c95b8df 6972 current_program_space->executing_startup = 0;
8bea4e01
UW
6973
6974 ALL_BREAKPOINTS (b)
6975 {
6c95b8df
PA
6976 if (b->pspace != current_program_space)
6977 continue;
6978
8bea4e01
UW
6979 if ((b->type == bp_breakpoint
6980 || b->type == bp_hardware_breakpoint)
6981 && b->enable_state == bp_startup_disabled)
6982 {
6983 b->enable_state = bp_enabled;
6984 found = 1;
6985 }
6986 }
6987
6988 if (found)
6989 breakpoint_re_set ();
6990}
6991
c906108c
SS
6992
6993/* Set a breakpoint that will evaporate an end of command
6994 at address specified by SAL.
6995 Restrict it to frame FRAME if FRAME is nonzero. */
6996
6997struct breakpoint *
a6d9a66e
UW
6998set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6999 struct frame_id frame_id, enum bptype type)
c906108c 7000{
52f0bd74 7001 struct breakpoint *b;
edb3359d
DJ
7002
7003 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7004 one. */
7005 gdb_assert (!frame_id_inlined_p (frame_id));
7006
a6d9a66e 7007 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
7008 b->enable_state = bp_enabled;
7009 b->disposition = disp_donttouch;
818dd999 7010 b->frame_id = frame_id;
c906108c 7011
4a64f543
MS
7012 /* If we're debugging a multi-threaded program, then we want
7013 momentary breakpoints to be active in only a single thread of
7014 control. */
39f77062
KB
7015 if (in_thread_list (inferior_ptid))
7016 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7017
b60e7edf 7018 update_global_location_list_nothrow (1);
74960c60 7019
c906108c
SS
7020 return b;
7021}
611c83ae 7022
e58b0e63
PA
7023/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7024 ORIG is NULL. */
7025
7026struct breakpoint *
7027clone_momentary_breakpoint (struct breakpoint *orig)
7028{
7029 struct breakpoint *copy;
7030
7031 /* If there's nothing to clone, then return nothing. */
7032 if (orig == NULL)
7033 return NULL;
7034
a6d9a66e 7035 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63 7036 copy->loc = allocate_bp_location (copy);
0e30163f 7037 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7038
a6d9a66e 7039 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7040 copy->loc->requested_address = orig->loc->requested_address;
7041 copy->loc->address = orig->loc->address;
7042 copy->loc->section = orig->loc->section;
6c95b8df 7043 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7044
7045 if (orig->source_file == NULL)
7046 copy->source_file = NULL;
7047 else
7048 copy->source_file = xstrdup (orig->source_file);
7049
7050 copy->line_number = orig->line_number;
7051 copy->frame_id = orig->frame_id;
7052 copy->thread = orig->thread;
6c95b8df 7053 copy->pspace = orig->pspace;
e58b0e63
PA
7054
7055 copy->enable_state = bp_enabled;
7056 copy->disposition = disp_donttouch;
7057 copy->number = internal_breakpoint_number--;
7058
7059 update_global_location_list_nothrow (0);
7060 return copy;
7061}
7062
611c83ae 7063struct breakpoint *
a6d9a66e
UW
7064set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7065 enum bptype type)
611c83ae
PA
7066{
7067 struct symtab_and_line sal;
7068
7069 sal = find_pc_line (pc, 0);
7070 sal.pc = pc;
7071 sal.section = find_pc_overlay (pc);
7072 sal.explicit_pc = 1;
7073
a6d9a66e 7074 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7075}
c906108c 7076\f
c5aa993b 7077
c906108c
SS
7078/* Tell the user we have just set a breakpoint B. */
7079
7080static void
fba45db2 7081mention (struct breakpoint *b)
c906108c
SS
7082{
7083 int say_where = 0;
fa8a61dc 7084 struct cleanup *ui_out_chain;
79a45b7d
TT
7085 struct value_print_options opts;
7086
7087 get_user_print_options (&opts);
8b93c638 7088
3086aeae
DJ
7089 if (b->ops != NULL && b->ops->print_mention != NULL)
7090 b->ops->print_mention (b);
7091 else
7092 switch (b->type)
7093 {
7094 case bp_none:
3e43a32a
MS
7095 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7096 b->number);
3086aeae
DJ
7097 break;
7098 case bp_watchpoint:
7099 ui_out_text (uiout, "Watchpoint ");
7100 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7101 ui_out_field_int (uiout, "number", b->number);
7102 ui_out_text (uiout, ": ");
fa8a61dc 7103 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7104 do_cleanups (ui_out_chain);
7105 break;
7106 case bp_hardware_watchpoint:
7107 ui_out_text (uiout, "Hardware watchpoint ");
7108 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7109 ui_out_field_int (uiout, "number", b->number);
7110 ui_out_text (uiout, ": ");
fa8a61dc 7111 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7112 do_cleanups (ui_out_chain);
7113 break;
7114 case bp_read_watchpoint:
7115 ui_out_text (uiout, "Hardware read watchpoint ");
7116 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7117 ui_out_field_int (uiout, "number", b->number);
7118 ui_out_text (uiout, ": ");
fa8a61dc 7119 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7120 do_cleanups (ui_out_chain);
7121 break;
7122 case bp_access_watchpoint:
7123 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7124 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7125 ui_out_field_int (uiout, "number", b->number);
7126 ui_out_text (uiout, ": ");
fa8a61dc 7127 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7128 do_cleanups (ui_out_chain);
7129 break;
7130 case bp_breakpoint:
0e30163f 7131 case bp_gnu_ifunc_resolver:
3086aeae
DJ
7132 if (ui_out_is_mi_like_p (uiout))
7133 {
7134 say_where = 0;
7135 break;
7136 }
2cec12e5
AR
7137 if (b->disposition == disp_del)
7138 printf_filtered (_("Temporary breakpoint"));
7139 else
7140 printf_filtered (_("Breakpoint"));
7141 printf_filtered (_(" %d"), b->number);
0e30163f
JK
7142 if (b->type == bp_gnu_ifunc_resolver)
7143 printf_filtered (_(" at gnu-indirect-function resolver"));
3086aeae
DJ
7144 say_where = 1;
7145 break;
7146 case bp_hardware_breakpoint:
7147 if (ui_out_is_mi_like_p (uiout))
7148 {
7149 say_where = 0;
7150 break;
7151 }
a3f17187 7152 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7153 say_where = 1;
7154 break;
1042e4c0
SS
7155 case bp_tracepoint:
7156 if (ui_out_is_mi_like_p (uiout))
7157 {
7158 say_where = 0;
7159 break;
7160 }
7161 printf_filtered (_("Tracepoint"));
7162 printf_filtered (_(" %d"), b->number);
7163 say_where = 1;
7164 break;
7a697b8d
SS
7165 case bp_fast_tracepoint:
7166 if (ui_out_is_mi_like_p (uiout))
7167 {
7168 say_where = 0;
7169 break;
7170 }
7171 printf_filtered (_("Fast tracepoint"));
7172 printf_filtered (_(" %d"), b->number);
7173 say_where = 1;
7174 break;
0fb4aa4b
PA
7175 case bp_static_tracepoint:
7176 if (ui_out_is_mi_like_p (uiout))
7177 {
7178 say_where = 0;
7179 break;
7180 }
7181 printf_filtered (_("Static tracepoint"));
7182 printf_filtered (_(" %d"), b->number);
7183 say_where = 1;
7184 break;
3086aeae
DJ
7185
7186 case bp_until:
7187 case bp_finish:
7188 case bp_longjmp:
7189 case bp_longjmp_resume:
186c406b
TT
7190 case bp_exception:
7191 case bp_exception_resume:
3086aeae 7192 case bp_step_resume:
3086aeae 7193 case bp_call_dummy:
aa7d318d 7194 case bp_std_terminate:
3086aeae
DJ
7195 case bp_watchpoint_scope:
7196 case bp_shlib_event:
7197 case bp_thread_event:
7198 case bp_overlay_event:
4efc6507 7199 case bp_jit_event:
0fd8e87f 7200 case bp_longjmp_master:
aa7d318d 7201 case bp_std_terminate_master:
186c406b 7202 case bp_exception_master:
0e30163f 7203 case bp_gnu_ifunc_resolver_return:
3086aeae
DJ
7204 break;
7205 }
c906108c 7206
c906108c
SS
7207 if (say_where)
7208 {
a3f17187
AC
7209 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7210 single string. */
0d381245 7211 if (b->loc == NULL)
c906108c 7212 {
a3f17187 7213 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7214 }
7215 else
7216 {
79a45b7d 7217 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7218 {
7219 printf_filtered (" at ");
5af949e3
UW
7220 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7221 gdb_stdout);
0101ce28
JJ
7222 }
7223 if (b->source_file)
7224 printf_filtered (": file %s, line %d.",
7225 b->source_file, b->line_number);
0d381245
VP
7226
7227 if (b->loc->next)
7228 {
7229 struct bp_location *loc = b->loc;
7230 int n = 0;
7231 for (; loc; loc = loc->next)
7232 ++n;
7233 printf_filtered (" (%d locations)", n);
7234 }
7235
c906108c 7236 }
c906108c 7237 }
9dc5e2a9 7238 if (ui_out_is_mi_like_p (uiout))
fb40c209 7239 return;
c906108c
SS
7240 printf_filtered ("\n");
7241}
c906108c 7242\f
c5aa993b 7243
0d381245 7244static struct bp_location *
39d61571 7245add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7246 const struct symtab_and_line *sal)
7247{
7248 struct bp_location *loc, **tmp;
7249
39d61571 7250 loc = allocate_bp_location (b);
0d381245
VP
7251 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7252 ;
7253 *tmp = loc;
a6d9a66e
UW
7254 loc->gdbarch = get_sal_arch (*sal);
7255 if (!loc->gdbarch)
7256 loc->gdbarch = b->gdbarch;
0d381245 7257 loc->requested_address = sal->pc;
a6d9a66e
UW
7258 loc->address = adjust_breakpoint_address (loc->gdbarch,
7259 loc->requested_address, b->type);
6c95b8df
PA
7260 loc->pspace = sal->pspace;
7261 gdb_assert (loc->pspace != NULL);
0d381245
VP
7262 loc->section = sal->section;
7263
0e30163f
JK
7264 set_breakpoint_location_function (loc,
7265 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7266 return loc;
7267}
514f746b
AR
7268\f
7269
7270/* Return 1 if LOC is pointing to a permanent breakpoint,
7271 return 0 otherwise. */
7272
7273static int
7274bp_loc_is_permanent (struct bp_location *loc)
7275{
7276 int len;
7277 CORE_ADDR addr;
7278 const gdb_byte *brk;
7279 gdb_byte *target_mem;
939c61fa
JK
7280 struct cleanup *cleanup;
7281 int retval = 0;
514f746b
AR
7282
7283 gdb_assert (loc != NULL);
7284
7285 addr = loc->address;
a6d9a66e 7286 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7287
939c61fa
JK
7288 /* Software breakpoints unsupported? */
7289 if (brk == NULL)
7290 return 0;
7291
514f746b
AR
7292 target_mem = alloca (len);
7293
939c61fa
JK
7294 /* Enable the automatic memory restoration from breakpoints while
7295 we read the memory. Otherwise we could say about our temporary
7296 breakpoints they are permanent. */
6c95b8df
PA
7297 cleanup = save_current_space_and_thread ();
7298
7299 switch_to_program_space_and_thread (loc->pspace);
7300 make_show_memory_breakpoints_cleanup (0);
939c61fa 7301
514f746b
AR
7302 if (target_read_memory (loc->address, target_mem, len) == 0
7303 && memcmp (target_mem, brk, len) == 0)
939c61fa 7304 retval = 1;
514f746b 7305
939c61fa
JK
7306 do_cleanups (cleanup);
7307
7308 return retval;
514f746b
AR
7309}
7310
7311
c3f6f71d 7312
018d34a4
VP
7313/* Create a breakpoint with SAL as location. Use ADDR_STRING
7314 as textual description of the location, and COND_STRING
db107f19 7315 as condition expression. */
018d34a4
VP
7316
7317static void
8cdf0e15
VP
7318create_breakpoint_sal (struct gdbarch *gdbarch,
7319 struct symtabs_and_lines sals, char *addr_string,
7320 char *cond_string,
7321 enum bptype type, enum bpdisp disposition,
7322 int thread, int task, int ignore_count,
84f4c1fe 7323 struct breakpoint_ops *ops, int from_tty,
56435ebe 7324 int enabled, int internal, int display_canonical)
018d34a4 7325{
0d381245
VP
7326 struct breakpoint *b = NULL;
7327 int i;
018d34a4
VP
7328
7329 if (type == bp_hardware_breakpoint)
7330 {
7331 int i = hw_breakpoint_used_count ();
7332 int target_resources_ok =
d92524f1 7333 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7334 i + 1, 0);
7335 if (target_resources_ok == 0)
7336 error (_("No hardware breakpoint support in the target."));
7337 else if (target_resources_ok < 0)
7338 error (_("Hardware breakpoints used exceeds limit."));
7339 }
7340
6c95b8df
PA
7341 gdb_assert (sals.nelts > 0);
7342
0d381245
VP
7343 for (i = 0; i < sals.nelts; ++i)
7344 {
7345 struct symtab_and_line sal = sals.sals[i];
7346 struct bp_location *loc;
7347
7348 if (from_tty)
5af949e3
UW
7349 {
7350 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7351 if (!loc_gdbarch)
7352 loc_gdbarch = gdbarch;
7353
7354 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7355 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7356 }
0d381245
VP
7357
7358 if (i == 0)
7359 {
a6d9a66e 7360 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7361 set_breakpoint_number (internal, b);
0d381245 7362 b->thread = thread;
4a306c9a 7363 b->task = task;
018d34a4 7364
0d381245
VP
7365 b->cond_string = cond_string;
7366 b->ignore_count = ignore_count;
41447f92 7367 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7368 b->disposition = disposition;
6c95b8df
PA
7369 b->pspace = sals.sals[0].pspace;
7370
0fb4aa4b
PA
7371 if (type == bp_static_tracepoint)
7372 {
7373 struct static_tracepoint_marker marker;
7374
7375 if (is_marker_spec (addr_string))
7376 {
7377 /* We already know the marker exists, otherwise, we
7378 wouldn't see a sal for it. */
7379 char *p = &addr_string[3];
7380 char *endp;
7381 char *marker_str;
7382 int i;
7383
e9cafbcc 7384 p = skip_spaces (p);
0fb4aa4b 7385
e9cafbcc 7386 endp = skip_to_space (p);
0fb4aa4b
PA
7387
7388 marker_str = savestring (p, endp - p);
7389 b->static_trace_marker_id = marker_str;
7390
3e43a32a
MS
7391 printf_filtered (_("Probed static tracepoint "
7392 "marker \"%s\"\n"),
0fb4aa4b
PA
7393 b->static_trace_marker_id);
7394 }
7395 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7396 {
7397 b->static_trace_marker_id = xstrdup (marker.str_id);
7398 release_static_tracepoint_marker (&marker);
7399
3e43a32a
MS
7400 printf_filtered (_("Probed static tracepoint "
7401 "marker \"%s\"\n"),
0fb4aa4b
PA
7402 b->static_trace_marker_id);
7403 }
7404 else
3e43a32a
MS
7405 warning (_("Couldn't determine the static "
7406 "tracepoint marker to probe"));
0fb4aa4b
PA
7407 }
7408
6c95b8df 7409 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7410 && (b->type == bp_breakpoint
7411 || b->type == bp_hardware_breakpoint))
7412 b->enable_state = bp_startup_disabled;
7413
0d381245
VP
7414 loc = b->loc;
7415 }
7416 else
018d34a4 7417 {
39d61571 7418 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7419 }
7420
514f746b
AR
7421 if (bp_loc_is_permanent (loc))
7422 make_breakpoint_permanent (b);
7423
0d381245
VP
7424 if (b->cond_string)
7425 {
7426 char *arg = b->cond_string;
d32a6982 7427 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7428 if (*arg)
db107f19 7429 error (_("Garbage %s follows condition"), arg);
018d34a4 7430 }
0d381245 7431 }
018d34a4 7432
56435ebe 7433 b->display_canonical = display_canonical;
018d34a4
VP
7434 if (addr_string)
7435 b->addr_string = addr_string;
7436 else
7437 /* addr_string has to be used or breakpoint_re_set will delete
7438 me. */
5af949e3
UW
7439 b->addr_string
7440 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7441
604133b5 7442 b->ops = ops;
8d3788bd
VP
7443 /* Do not mention breakpoints with a negative number, but do
7444 notify observers. */
7445 if (!internal)
84f4c1fe 7446 mention (b);
8d3788bd 7447 observer_notify_breakpoint_created (b);
018d34a4
VP
7448}
7449
ed0616c6
VP
7450/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7451 elements to fill the void space. */
2c0b251b
PA
7452static void
7453remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7454{
7455 int i = index_to_remove+1;
7456 int last_index = sal->nelts-1;
7457
7458 for (;i <= last_index; ++i)
7459 sal->sals[i-1] = sal->sals[i];
7460
7461 --(sal->nelts);
7462}
7463
6c95b8df
PA
7464/* If appropriate, obtains all sals that correspond to the same file
7465 and line as SAL, in all program spaces. Users debugging with IDEs,
7466 will want to set a breakpoint at foo.c:line, and not really care
7467 about program spaces. This is done only if SAL does not have
7468 explicit PC and has line and file information. If we got just a
7469 single expanded sal, return the original.
ed0616c6 7470
6c95b8df
PA
7471 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7472 which the name of enclosing function is different from SAL. This
7473 makes sure that if we have breakpoint originally set in template
7474 instantiation, say foo<int>(), we won't expand SAL to locations at
7475 the same line in all existing instantiations of 'foo'. */
ed0616c6 7476
2c0b251b 7477static struct symtabs_and_lines
ed0616c6
VP
7478expand_line_sal_maybe (struct symtab_and_line sal)
7479{
7480 struct symtabs_and_lines expanded;
7481 CORE_ADDR original_pc = sal.pc;
7482 char *original_function = NULL;
7483 int found;
7484 int i;
6c95b8df 7485 struct cleanup *old_chain;
ed0616c6
VP
7486
7487 /* If we have explicit pc, don't expand.
7488 If we have no line number, we can't expand. */
7489 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7490 {
7491 expanded.nelts = 1;
7492 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7493 expanded.sals[0] = sal;
7494 return expanded;
7495 }
7496
7497 sal.pc = 0;
6c95b8df
PA
7498
7499 old_chain = save_current_space_and_thread ();
7500
7501 switch_to_program_space_and_thread (sal.pspace);
7502
ed0616c6 7503 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7504
7505 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7506 expanded = expand_line_sal (sal);
6c95b8df 7507
ed0616c6
VP
7508 if (expanded.nelts == 1)
7509 {
3dba1c98
JB
7510 /* We had one sal, we got one sal. Return that sal, adjusting it
7511 past the function prologue if necessary. */
ed0616c6
VP
7512 xfree (expanded.sals);
7513 expanded.nelts = 1;
7514 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7515 sal.pc = original_pc;
7516 expanded.sals[0] = sal;
3dba1c98 7517 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7518 do_cleanups (old_chain);
ed0616c6
VP
7519 return expanded;
7520 }
7521
7522 if (!sal.explicit_line)
7523 {
7524 CORE_ADDR func_addr, func_end;
7525 for (i = 0; i < expanded.nelts; ++i)
7526 {
7527 CORE_ADDR pc = expanded.sals[i].pc;
7528 char *this_function;
6c95b8df
PA
7529
7530 /* We need to switch threads as well since we're about to
7531 read memory. */
7532 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7533
ed0616c6
VP
7534 if (find_pc_partial_function (pc, &this_function,
7535 &func_addr, &func_end))
7536 {
059fb39f
PM
7537 if (this_function
7538 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7539 {
7540 remove_sal (&expanded, i);
7541 --i;
7542 }
ed0616c6
VP
7543 }
7544 }
7545 }
059acae7
UW
7546
7547 /* Skip the function prologue if necessary. */
7548 for (i = 0; i < expanded.nelts; ++i)
7549 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7550
6c95b8df
PA
7551 do_cleanups (old_chain);
7552
ed0616c6
VP
7553 if (expanded.nelts <= 1)
7554 {
4a64f543
MS
7555 /* This is un ugly workaround. If we get zero expanded sals
7556 then something is really wrong. Fix that by returning the
7557 original sal. */
7558
ed0616c6
VP
7559 xfree (expanded.sals);
7560 expanded.nelts = 1;
7561 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7562 sal.pc = original_pc;
7563 expanded.sals[0] = sal;
7564 return expanded;
7565 }
7566
7567 if (original_pc)
7568 {
7569 found = 0;
7570 for (i = 0; i < expanded.nelts; ++i)
7571 if (expanded.sals[i].pc == original_pc)
7572 {
7573 found = 1;
7574 break;
7575 }
7576 gdb_assert (found);
7577 }
7578
7579 return expanded;
7580}
7581
018d34a4
VP
7582/* Add SALS.nelts breakpoints to the breakpoint table. For each
7583 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7584 value. COND_STRING, if not NULL, specified the condition to be
7585 used for all breakpoints. Essentially the only case where
7586 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7587 function. In that case, it's still not possible to specify
7588 separate conditions for different overloaded functions, so
7589 we take just a single condition string.
7590
c3f6f71d 7591 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7592 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7593 array contents). If the function fails (error() is called), the
7594 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7595 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7596
7597static void
8cdf0e15 7598create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7599 struct symtabs_and_lines sals,
7600 struct linespec_result *canonical,
8cdf0e15
VP
7601 char *cond_string,
7602 enum bptype type, enum bpdisp disposition,
7603 int thread, int task, int ignore_count,
7604 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7605 int enabled, int internal)
c906108c 7606{
018d34a4 7607 int i;
cc59ec59 7608
018d34a4 7609 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7610 {
ed0616c6
VP
7611 struct symtabs_and_lines expanded =
7612 expand_line_sal_maybe (sals.sals[i]);
0d381245 7613
7efd8fc2 7614 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7615 cond_string, type, disposition,
84f4c1fe 7616 thread, task, ignore_count, ops,
56435ebe
TT
7617 from_tty, enabled, internal,
7618 canonical->special_display);
c3f6f71d 7619 }
c3f6f71d 7620}
c906108c 7621
9998af43 7622/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7623 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7624 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7625 address strings. ADDRESS points to the end of the SAL.
7626
7627 The array and the line spec strings are allocated on the heap, it is
7628 the caller's responsibility to free them. */
c906108c 7629
b9362cc7 7630static void
c3f6f71d
JM
7631parse_breakpoint_sals (char **address,
7632 struct symtabs_and_lines *sals,
58438ac1 7633 struct linespec_result *canonical)
c3f6f71d
JM
7634{
7635 char *addr_start = *address;
cc59ec59 7636
c3f6f71d 7637 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7638 breakpoint. */
c3f6f71d
JM
7639 if ((*address) == NULL
7640 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7641 {
7642 if (default_breakpoint_valid)
7643 {
c3f6f71d 7644 struct symtab_and_line sal;
cc59ec59 7645
4a64f543 7646 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7647 sals->sals = (struct symtab_and_line *)
c906108c
SS
7648 xmalloc (sizeof (struct symtab_and_line));
7649 sal.pc = default_breakpoint_address;
7650 sal.line = default_breakpoint_line;
7651 sal.symtab = default_breakpoint_symtab;
6c95b8df 7652 sal.pspace = default_breakpoint_pspace;
c5aa993b 7653 sal.section = find_pc_overlay (sal.pc);
00903456 7654
4a64f543
MS
7655 /* "break" without arguments is equivalent to "break *PC"
7656 where PC is the default_breakpoint_address. So make sure
7657 to set sal.explicit_pc to prevent GDB from trying to
7658 expand the list of sals to include all other instances
7659 with the same symtab and line. */
00903456
JK
7660 sal.explicit_pc = 1;
7661
c3f6f71d
JM
7662 sals->sals[0] = sal;
7663 sals->nelts = 1;
c906108c
SS
7664 }
7665 else
8a3fe4f8 7666 error (_("No default breakpoint address now."));
c906108c
SS
7667 }
7668 else
7669 {
c906108c 7670 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7671 current_source_symtab (which is decode_line_1's default).
7672 This should produce the results we want almost all of the
7673 time while leaving default_breakpoint_* alone.
7674
1aeae86e
AF
7675 ObjC: However, don't match an Objective-C method name which
7676 may have a '+' or '-' succeeded by a '[' */
0378c332 7677
c214a6fd 7678 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7679
c906108c 7680 if (default_breakpoint_valid
0378c332 7681 && (!cursal.symtab
1aeae86e
AF
7682 || ((strchr ("+-", (*address)[0]) != NULL)
7683 && ((*address)[1] != '['))))
c3f6f71d 7684 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7685 default_breakpoint_line, canonical);
c906108c 7686 else
0101ce28 7687 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7688 canonical);
c906108c 7689 }
4a64f543 7690 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7691 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7692 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7693 if (addr_start != (*address))
c906108c 7694 {
c3f6f71d 7695 int i;
cc59ec59 7696
c3f6f71d 7697 for (i = 0; i < sals->nelts; i++)
c906108c 7698 {
4a64f543 7699 /* Add the string if not present. */
7efd8fc2
TT
7700 if (canonical->canonical[i] == NULL)
7701 canonical->canonical[i] = savestring (addr_start,
7702 (*address) - addr_start);
c906108c
SS
7703 }
7704 }
c3f6f71d 7705}
c906108c 7706
c906108c 7707
c3f6f71d 7708/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7709 inserted as a breakpoint. If it can't throw an error. */
c906108c 7710
b9362cc7 7711static void
23e7acfb 7712breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7713{
7714 int i;
cc59ec59 7715
c3f6f71d 7716 for (i = 0; i < sals->nelts; i++)
ee53e872 7717 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7718}
7719
7a697b8d
SS
7720/* Fast tracepoints may have restrictions on valid locations. For
7721 instance, a fast tracepoint using a jump instead of a trap will
7722 likely have to overwrite more bytes than a trap would, and so can
7723 only be placed where the instruction is longer than the jump, or a
7724 multi-instruction sequence does not have a jump into the middle of
7725 it, etc. */
7726
7727static void
7728check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7729 struct symtabs_and_lines *sals)
7730{
7731 int i, rslt;
7732 struct symtab_and_line *sal;
7733 char *msg;
7734 struct cleanup *old_chain;
7735
7736 for (i = 0; i < sals->nelts; i++)
7737 {
7738 sal = &sals->sals[i];
7739
7740 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7741 NULL, &msg);
7742 old_chain = make_cleanup (xfree, msg);
7743
7744 if (!rslt)
7745 error (_("May not have a fast tracepoint at 0x%s%s"),
7746 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7747
7748 do_cleanups (old_chain);
7749 }
7750}
7751
018d34a4
VP
7752/* Given TOK, a string specification of condition and thread, as
7753 accepted by the 'break' command, extract the condition
7754 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7755 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7756 If no condition is found, *COND_STRING is set to NULL.
7757 If no thread is found, *THREAD is set to -1. */
7758static void
7759find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7760 char **cond_string, int *thread, int *task)
018d34a4
VP
7761{
7762 *cond_string = NULL;
7763 *thread = -1;
7764 while (tok && *tok)
7765 {
7766 char *end_tok;
7767 int toklen;
7768 char *cond_start = NULL;
7769 char *cond_end = NULL;
cc59ec59 7770
e9cafbcc 7771 tok = skip_spaces (tok);
018d34a4 7772
e9cafbcc 7773 end_tok = skip_to_space (tok);
018d34a4
VP
7774
7775 toklen = end_tok - tok;
7776
7777 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7778 {
f7545552
TT
7779 struct expression *expr;
7780
018d34a4 7781 tok = cond_start = end_tok + 1;
f7545552
TT
7782 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7783 xfree (expr);
018d34a4
VP
7784 cond_end = tok;
7785 *cond_string = savestring (cond_start,
7786 cond_end - cond_start);
7787 }
7788 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7789 {
7790 char *tmptok;
7791
7792 tok = end_tok + 1;
7793 tmptok = tok;
7794 *thread = strtol (tok, &tok, 0);
7795 if (tok == tmptok)
7796 error (_("Junk after thread keyword."));
7797 if (!valid_thread_id (*thread))
7798 error (_("Unknown thread %d."), *thread);
7799 }
4a306c9a
JB
7800 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7801 {
7802 char *tmptok;
7803
7804 tok = end_tok + 1;
7805 tmptok = tok;
7806 *task = strtol (tok, &tok, 0);
7807 if (tok == tmptok)
7808 error (_("Junk after task keyword."));
7809 if (!valid_task_id (*task))
b6199126 7810 error (_("Unknown task %d."), *task);
4a306c9a 7811 }
018d34a4
VP
7812 else
7813 error (_("Junk at end of arguments."));
7814 }
7815}
7816
0fb4aa4b
PA
7817/* Decode a static tracepoint marker spec. */
7818
7819static struct symtabs_and_lines
7820decode_static_tracepoint_spec (char **arg_p)
7821{
7822 VEC(static_tracepoint_marker_p) *markers = NULL;
7823 struct symtabs_and_lines sals;
7824 struct symtab_and_line sal;
7825 struct symbol *sym;
7826 struct cleanup *old_chain;
7827 char *p = &(*arg_p)[3];
7828 char *endp;
7829 char *marker_str;
7830 int i;
7831
e9cafbcc 7832 p = skip_spaces (p);
0fb4aa4b 7833
e9cafbcc 7834 endp = skip_to_space (p);
0fb4aa4b
PA
7835
7836 marker_str = savestring (p, endp - p);
7837 old_chain = make_cleanup (xfree, marker_str);
7838
7839 markers = target_static_tracepoint_markers_by_strid (marker_str);
7840 if (VEC_empty(static_tracepoint_marker_p, markers))
7841 error (_("No known static tracepoint marker named %s"), marker_str);
7842
7843 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7844 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7845
7846 for (i = 0; i < sals.nelts; i++)
7847 {
7848 struct static_tracepoint_marker *marker;
7849
7850 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7851
7852 init_sal (&sals.sals[i]);
7853
7854 sals.sals[i] = find_pc_line (marker->address, 0);
7855 sals.sals[i].pc = marker->address;
7856
7857 release_static_tracepoint_marker (marker);
7858 }
7859
7860 do_cleanups (old_chain);
7861
7862 *arg_p = endp;
7863 return sals;
7864}
7865
fd9b8c24
PA
7866/* Set a breakpoint. This function is shared between CLI and MI
7867 functions for setting a breakpoint. This function has two major
7868 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7869 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7870 breakpoint location, address and thread. Otherwise, ARG is just
7871 the location of breakpoint, with condition and thread specified by
7872 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7873 the breakpoint number will be allocated from the internal
7874 breakpoint count. Returns true if any breakpoint was created;
7875 false otherwise. */
0101ce28 7876
8cdf0e15
VP
7877int
7878create_breakpoint (struct gdbarch *gdbarch,
7879 char *arg, char *cond_string, int thread,
7880 int parse_condition_and_thread,
0fb4aa4b 7881 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7882 int ignore_count,
7883 enum auto_boolean pending_break_support,
7884 struct breakpoint_ops *ops,
84f4c1fe 7885 int from_tty, int enabled, int internal)
c3f6f71d 7886{
b78a6381 7887 volatile struct gdb_exception e;
c3f6f71d 7888 struct symtabs_and_lines sals;
0101ce28 7889 struct symtab_and_line pending_sal;
0101ce28 7890 char *copy_arg;
c3f6f71d 7891 char *addr_start = arg;
7efd8fc2 7892 struct linespec_result canonical;
c3f6f71d 7893 struct cleanup *old_chain;
80c99de1 7894 struct cleanup *bkpt_chain = NULL;
05ff989b 7895 int i;
0101ce28 7896 int pending = 0;
4a306c9a 7897 int task = 0;
86b17b60 7898 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7899
c3f6f71d
JM
7900 sals.sals = NULL;
7901 sals.nelts = 0;
7efd8fc2 7902 init_linespec_result (&canonical);
c3f6f71d 7903
0fb4aa4b
PA
7904 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7905 {
7906 int i;
7907
7908 sals = decode_static_tracepoint_spec (&arg);
7909
7910 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7911 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7912 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7913 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7914 goto done;
7915 }
7916
b78a6381
TT
7917 TRY_CATCH (e, RETURN_MASK_ALL)
7918 {
58438ac1 7919 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7920 }
0101ce28
JJ
7921
7922 /* If caller is interested in rc value from parse, set value. */
05ff989b 7923 switch (e.reason)
0101ce28 7924 {
05ff989b 7925 case RETURN_QUIT:
98deb0da 7926 throw_exception (e);
05ff989b
AC
7927 case RETURN_ERROR:
7928 switch (e.error)
0101ce28 7929 {
05ff989b 7930 case NOT_FOUND_ERROR:
0101ce28 7931
05ff989b
AC
7932 /* If pending breakpoint support is turned off, throw
7933 error. */
fa8d40ab
JJ
7934
7935 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7936 throw_exception (e);
7937
7938 exception_print (gdb_stderr, e);
fa8d40ab 7939
05ff989b
AC
7940 /* If pending breakpoint support is auto query and the user
7941 selects no, then simply return the error code. */
059fb39f 7942 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7943 && !nquery (_("Make breakpoint pending on "
7944 "future shared library load? ")))
fd9b8c24 7945 return 0;
fa8d40ab 7946
05ff989b
AC
7947 /* At this point, either the user was queried about setting
7948 a pending breakpoint and selected yes, or pending
7949 breakpoint behavior is on and thus a pending breakpoint
7950 is defaulted on behalf of the user. */
0101ce28 7951 copy_arg = xstrdup (addr_start);
7efd8fc2 7952 canonical.canonical = &copy_arg;
0101ce28
JJ
7953 sals.nelts = 1;
7954 sals.sals = &pending_sal;
7955 pending_sal.pc = 0;
7956 pending = 1;
05ff989b
AC
7957 break;
7958 default:
98deb0da 7959 throw_exception (e);
0101ce28 7960 }
2abae994 7961 break;
05ff989b
AC
7962 default:
7963 if (!sals.nelts)
fd9b8c24 7964 return 0;
0101ce28 7965 }
c3f6f71d 7966
0fb4aa4b
PA
7967 done:
7968
4a64f543 7969 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7970 old_chain = make_cleanup (null_cleanup, 0);
7971
0101ce28
JJ
7972 if (!pending)
7973 {
7974 /* Make sure that all storage allocated to SALS gets freed. */
7975 make_cleanup (xfree, sals.sals);
7976
7efd8fc2
TT
7977 /* Cleanup the canonical array but not its contents. */
7978 make_cleanup (xfree, canonical.canonical);
0101ce28 7979 }
c3f6f71d 7980
c3f6f71d
JM
7981 /* ----------------------------- SNIP -----------------------------
7982 Anything added to the cleanup chain beyond this point is assumed
7983 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7984 then the memory is not reclaimed. */
7985 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7986
7efd8fc2 7987 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7988 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7989 for (i = 0; i < sals.nelts; i++)
7990 {
7efd8fc2
TT
7991 if (canonical.canonical[i] != NULL)
7992 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7993 }
7994
7995 /* Resolve all line numbers to PC's and verify that the addresses
7996 are ok for the target. */
0101ce28 7997 if (!pending)
23e7acfb 7998 breakpoint_sals_to_pc (&sals);
c3f6f71d 7999
7a697b8d
SS
8000 /* Fast tracepoints may have additional restrictions on location. */
8001 if (type_wanted == bp_fast_tracepoint)
8002 check_fast_tracepoint_sals (gdbarch, &sals);
8003
c3f6f71d
JM
8004 /* Verify that condition can be parsed, before setting any
8005 breakpoints. Allocate a separate condition expression for each
4a64f543 8006 breakpoint. */
0101ce28 8007 if (!pending)
c3f6f71d 8008 {
2f069f6f 8009 if (parse_condition_and_thread)
72b2ff0e
VP
8010 {
8011 /* Here we only parse 'arg' to separate condition
8012 from thread number, so parsing in context of first
8013 sal is OK. When setting the breakpoint we'll
8014 re-parse it in context of each sal. */
8015 cond_string = NULL;
8016 thread = -1;
4a306c9a
JB
8017 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8018 &thread, &task);
72b2ff0e
VP
8019 if (cond_string)
8020 make_cleanup (xfree, cond_string);
8021 }
2f069f6f 8022 else
72b2ff0e
VP
8023 {
8024 /* Create a private copy of condition string. */
8025 if (cond_string)
8026 {
8027 cond_string = xstrdup (cond_string);
8028 make_cleanup (xfree, cond_string);
8029 }
8030 }
0fb4aa4b
PA
8031
8032 /* If the user is creating a static tracepoint by marker id
8033 (strace -m MARKER_ID), then store the sals index, so that
8034 breakpoint_re_set can try to match up which of the newly
8035 found markers corresponds to this one, and, don't try to
8036 expand multiple locations for each sal, given than SALS
8037 already should contain all sals for MARKER_ID. */
8038 if (type_wanted == bp_static_tracepoint
7efd8fc2 8039 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8040 {
8041 int i;
8042
8043 for (i = 0; i < sals.nelts; ++i)
8044 {
8045 struct symtabs_and_lines expanded;
8046 struct breakpoint *tp;
8047 struct cleanup *old_chain;
8048
8049 expanded.nelts = 1;
8050 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8051 expanded.sals[0] = sals.sals[i];
8052 old_chain = make_cleanup (xfree, expanded.sals);
8053
7efd8fc2 8054 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
8055 cond_string, type_wanted,
8056 tempflag ? disp_del : disp_donttouch,
8057 thread, task, ignore_count, ops,
56435ebe
TT
8058 from_tty, enabled, internal,
8059 canonical.special_display);
0fb4aa4b
PA
8060
8061 do_cleanups (old_chain);
8062
8063 /* Get the tracepoint we just created. */
84f4c1fe
PM
8064 if (internal)
8065 tp = get_breakpoint (internal_breakpoint_number);
8066 else
8067 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
8068 gdb_assert (tp != NULL);
8069
8070 /* Given that its possible to have multiple markers with
8071 the same string id, if the user is creating a static
8072 tracepoint by marker id ("strace -m MARKER_ID"), then
8073 store the sals index, so that breakpoint_re_set can
8074 try to match up which of the newly found markers
8075 corresponds to this one */
8076 tp->static_trace_marker_id_idx = i;
8077 }
8078 }
8079 else
7efd8fc2 8080 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8081 type_wanted,
8082 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8083 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8084 enabled, internal);
c906108c 8085 }
0101ce28
JJ
8086 else
8087 {
0101ce28
JJ
8088 struct breakpoint *b;
8089
0101ce28
JJ
8090 make_cleanup (xfree, copy_arg);
8091
a6d9a66e 8092 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 8093 set_breakpoint_number (internal, b);
72b2ff0e 8094 b->thread = -1;
7efd8fc2 8095 b->addr_string = canonical.canonical[0];
72b2ff0e 8096 b->cond_string = NULL;
0101ce28 8097 b->ignore_count = ignore_count;
0101ce28 8098 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8099 b->condition_not_parsed = 1;
604133b5 8100 b->ops = ops;
41447f92 8101 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8102 b->pspace = current_program_space;
84f4c1fe 8103 b->py_bp_object = NULL;
74960c60 8104
6c95b8df 8105 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8106 && (b->type == bp_breakpoint
8107 || b->type == bp_hardware_breakpoint))
8108 b->enable_state = bp_startup_disabled;
8109
8d3788bd 8110 if (!internal)
84f4c1fe
PM
8111 /* Do not mention breakpoints with a negative number,
8112 but do notify observers. */
8d3788bd
VP
8113 mention (b);
8114 observer_notify_breakpoint_created (b);
0101ce28
JJ
8115 }
8116
c3f6f71d 8117 if (sals.nelts > 1)
95a42b64 8118 {
3e43a32a
MS
8119 warning (_("Multiple breakpoints were set.\nUse the "
8120 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8121 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8122 }
8123
80c99de1
PA
8124 /* That's it. Discard the cleanups for data inserted into the
8125 breakpoint. */
8126 discard_cleanups (bkpt_chain);
8127 /* But cleanup everything else. */
c3f6f71d 8128 do_cleanups (old_chain);
217dc9e2 8129
80c99de1 8130 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8131 update_global_location_list (1);
fd9b8c24
PA
8132
8133 return 1;
c3f6f71d 8134}
c906108c 8135
72b2ff0e
VP
8136/* Set a breakpoint.
8137 ARG is a string describing breakpoint address,
8138 condition, and thread.
8139 FLAG specifies if a breakpoint is hardware on,
8140 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8141 and BP_TEMPFLAG. */
8142
98deb0da 8143static void
72b2ff0e 8144break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8145{
72b2ff0e 8146 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8147 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8148 ? bp_hardware_breakpoint
8149 : bp_breakpoint);
c3f6f71d 8150
8cdf0e15
VP
8151 create_breakpoint (get_current_arch (),
8152 arg,
8153 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8154 tempflag, type_wanted,
8cdf0e15
VP
8155 0 /* Ignore count */,
8156 pending_break_support,
8157 NULL /* breakpoint_ops */,
8158 from_tty,
84f4c1fe
PM
8159 1 /* enabled */,
8160 0 /* internal */);
c906108c
SS
8161}
8162
72b2ff0e 8163
c906108c
SS
8164/* Helper function for break_command_1 and disassemble_command. */
8165
8166void
fba45db2 8167resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8168{
8169 CORE_ADDR pc;
8170
8171 if (sal->pc == 0 && sal->symtab != NULL)
8172 {
8173 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8174 error (_("No line %d in file \"%s\"."),
c906108c
SS
8175 sal->line, sal->symtab->filename);
8176 sal->pc = pc;
6a048695 8177
4a64f543
MS
8178 /* If this SAL corresponds to a breakpoint inserted using a line
8179 number, then skip the function prologue if necessary. */
6a048695 8180 if (sal->explicit_line)
059acae7 8181 skip_prologue_sal (sal);
c906108c
SS
8182 }
8183
8184 if (sal->section == 0 && sal->symtab != NULL)
8185 {
8186 struct blockvector *bv;
c5aa993b
JM
8187 struct block *b;
8188 struct symbol *sym;
c906108c 8189
801e3a5b 8190 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8191 if (bv != NULL)
8192 {
7f0df278 8193 sym = block_linkage_function (b);
c906108c
SS
8194 if (sym != NULL)
8195 {
8196 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8197 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8198 }
8199 else
8200 {
4a64f543
MS
8201 /* It really is worthwhile to have the section, so we'll
8202 just have to look harder. This case can be executed
8203 if we have line numbers but no functions (as can
8204 happen in assembly source). */
c906108c 8205
c5aa993b 8206 struct minimal_symbol *msym;
6c95b8df
PA
8207 struct cleanup *old_chain = save_current_space_and_thread ();
8208
8209 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8210
8211 msym = lookup_minimal_symbol_by_pc (sal->pc);
8212 if (msym)
714835d5 8213 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8214
8215 do_cleanups (old_chain);
c906108c
SS
8216 }
8217 }
8218 }
8219}
8220
8221void
fba45db2 8222break_command (char *arg, int from_tty)
c906108c 8223{
db107f19 8224 break_command_1 (arg, 0, from_tty);
c906108c
SS
8225}
8226
c906108c 8227void
fba45db2 8228tbreak_command (char *arg, int from_tty)
c906108c 8229{
db107f19 8230 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8231}
8232
c906108c 8233static void
fba45db2 8234hbreak_command (char *arg, int from_tty)
c906108c 8235{
db107f19 8236 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8237}
8238
8239static void
fba45db2 8240thbreak_command (char *arg, int from_tty)
c906108c 8241{
db107f19 8242 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8243}
8244
8245static void
fba45db2 8246stop_command (char *arg, int from_tty)
c906108c 8247{
a3f17187 8248 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8249Usage: stop in <function | address>\n\
a3f17187 8250 stop at <line>\n"));
c906108c
SS
8251}
8252
8253static void
fba45db2 8254stopin_command (char *arg, int from_tty)
c906108c
SS
8255{
8256 int badInput = 0;
8257
c5aa993b 8258 if (arg == (char *) NULL)
c906108c
SS
8259 badInput = 1;
8260 else if (*arg != '*')
8261 {
8262 char *argptr = arg;
8263 int hasColon = 0;
8264
4a64f543 8265 /* Look for a ':'. If this is a line number specification, then
53a5351d 8266 say it is bad, otherwise, it should be an address or
4a64f543 8267 function/method name. */
c906108c 8268 while (*argptr && !hasColon)
c5aa993b
JM
8269 {
8270 hasColon = (*argptr == ':');
8271 argptr++;
8272 }
c906108c
SS
8273
8274 if (hasColon)
c5aa993b 8275 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8276 else
c5aa993b 8277 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8278 }
8279
8280 if (badInput)
a3f17187 8281 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8282 else
db107f19 8283 break_command_1 (arg, 0, from_tty);
c906108c
SS
8284}
8285
8286static void
fba45db2 8287stopat_command (char *arg, int from_tty)
c906108c
SS
8288{
8289 int badInput = 0;
8290
c5aa993b 8291 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8292 badInput = 1;
8293 else
8294 {
8295 char *argptr = arg;
8296 int hasColon = 0;
8297
4a64f543
MS
8298 /* Look for a ':'. If there is a '::' then get out, otherwise
8299 it is probably a line number. */
c906108c 8300 while (*argptr && !hasColon)
c5aa993b
JM
8301 {
8302 hasColon = (*argptr == ':');
8303 argptr++;
8304 }
c906108c
SS
8305
8306 if (hasColon)
c5aa993b 8307 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8308 else
c5aa993b 8309 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8310 }
8311
8312 if (badInput)
a3f17187 8313 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8314 else
db107f19 8315 break_command_1 (arg, 0, from_tty);
c906108c
SS
8316}
8317
f1310107
TJB
8318/* Implement the "breakpoint_hit" breakpoint_ops method for
8319 ranged breakpoints. */
8320
8321static int
8322breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8323 struct address_space *aspace,
8324 CORE_ADDR bp_addr)
8325{
8326 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8327 bl->length, aspace, bp_addr);
8328}
8329
8330/* Implement the "resources_needed" breakpoint_ops method for
8331 ranged breakpoints. */
8332
8333static int
8334resources_needed_ranged_breakpoint (const struct bp_location *bl)
8335{
8336 return target_ranged_break_num_registers ();
8337}
8338
8339/* Implement the "print_it" breakpoint_ops method for
8340 ranged breakpoints. */
8341
8342static enum print_stop_action
8343print_it_ranged_breakpoint (struct breakpoint *b)
8344{
8345 struct bp_location *bl = b->loc;
8346
8347 gdb_assert (b->type == bp_hardware_breakpoint);
8348
8349 /* Ranged breakpoints have only one location. */
8350 gdb_assert (bl && bl->next == NULL);
8351
8352 annotate_breakpoint (b->number);
8353 if (b->disposition == disp_del)
8354 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8355 else
8356 ui_out_text (uiout, "\nRanged breakpoint ");
8357 if (ui_out_is_mi_like_p (uiout))
8358 {
8359 ui_out_field_string (uiout, "reason",
8360 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8361 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8362 }
8363 ui_out_field_int (uiout, "bkptno", b->number);
8364 ui_out_text (uiout, ", ");
8365
8366 return PRINT_SRC_AND_LOC;
8367}
8368
8369/* Implement the "print_one" breakpoint_ops method for
8370 ranged breakpoints. */
8371
8372static void
8373print_one_ranged_breakpoint (struct breakpoint *b,
8374 struct bp_location **last_loc)
8375{
8376 struct bp_location *bl = b->loc;
8377 struct value_print_options opts;
8378
8379 /* Ranged breakpoints have only one location. */
8380 gdb_assert (bl && bl->next == NULL);
8381
8382 get_user_print_options (&opts);
8383
8384 if (opts.addressprint)
8385 /* We don't print the address range here, it will be printed later
8386 by print_one_detail_ranged_breakpoint. */
8387 ui_out_field_skip (uiout, "addr");
8388 annotate_field (5);
8389 print_breakpoint_location (b, bl);
8390 *last_loc = bl;
8391}
8392
8393/* Implement the "print_one_detail" breakpoint_ops method for
8394 ranged breakpoints. */
8395
8396static void
8397print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8398 struct ui_out *uiout)
8399{
8400 CORE_ADDR address_start, address_end;
8401 struct bp_location *bl = b->loc;
8402 struct ui_stream *stb = ui_out_stream_new (uiout);
8403 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8404
8405 gdb_assert (bl);
8406
8407 address_start = bl->address;
8408 address_end = address_start + bl->length - 1;
8409
8410 ui_out_text (uiout, "\taddress range: ");
8411 fprintf_unfiltered (stb->stream, "[%s, %s]",
8412 print_core_address (bl->gdbarch, address_start),
8413 print_core_address (bl->gdbarch, address_end));
8414 ui_out_field_stream (uiout, "addr", stb);
8415 ui_out_text (uiout, "\n");
8416
8417 do_cleanups (cleanup);
8418}
8419
8420/* Implement the "print_mention" breakpoint_ops method for
8421 ranged breakpoints. */
8422
8423static void
8424print_mention_ranged_breakpoint (struct breakpoint *b)
8425{
8426 struct bp_location *bl = b->loc;
8427
8428 gdb_assert (bl);
8429 gdb_assert (b->type == bp_hardware_breakpoint);
8430
8431 if (ui_out_is_mi_like_p (uiout))
8432 return;
8433
8434 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8435 b->number, paddress (bl->gdbarch, bl->address),
8436 paddress (bl->gdbarch, bl->address + bl->length - 1));
8437}
8438
8439/* Implement the "print_recreate" breakpoint_ops method for
8440 ranged breakpoints. */
8441
8442static void
8443print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8444{
8445 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8446 b->addr_string_range_end);
8447}
8448
8449/* The breakpoint_ops structure to be used in ranged breakpoints. */
8450
8451static struct breakpoint_ops ranged_breakpoint_ops =
8452{
8453 NULL, /* insert */
8454 NULL, /* remove */
8455 breakpoint_hit_ranged_breakpoint,
8456 resources_needed_ranged_breakpoint,
8457 print_it_ranged_breakpoint,
8458 print_one_ranged_breakpoint,
8459 print_one_detail_ranged_breakpoint,
8460 print_mention_ranged_breakpoint,
8461 print_recreate_ranged_breakpoint
8462};
8463
8464/* Find the address where the end of the breakpoint range should be
8465 placed, given the SAL of the end of the range. This is so that if
8466 the user provides a line number, the end of the range is set to the
8467 last instruction of the given line. */
8468
8469static CORE_ADDR
8470find_breakpoint_range_end (struct symtab_and_line sal)
8471{
8472 CORE_ADDR end;
8473
8474 /* If the user provided a PC value, use it. Otherwise,
8475 find the address of the end of the given location. */
8476 if (sal.explicit_pc)
8477 end = sal.pc;
8478 else
8479 {
8480 int ret;
8481 CORE_ADDR start;
8482
8483 ret = find_line_pc_range (sal, &start, &end);
8484 if (!ret)
8485 error (_("Could not find location of the end of the range."));
8486
8487 /* find_line_pc_range returns the start of the next line. */
8488 end--;
8489 }
8490
8491 return end;
8492}
8493
8494/* Implement the "break-range" CLI command. */
8495
8496static void
8497break_range_command (char *arg, int from_tty)
8498{
8499 char *arg_start, *addr_string_start, *addr_string_end;
8500 struct linespec_result canonical_start, canonical_end;
8501 int bp_count, can_use_bp, length;
8502 CORE_ADDR end;
8503 struct breakpoint *b;
8504 struct symtab_and_line sal_start, sal_end;
8505 struct symtabs_and_lines sals_start, sals_end;
8506 struct cleanup *cleanup_bkpt;
8507
8508 /* We don't support software ranged breakpoints. */
8509 if (target_ranged_break_num_registers () < 0)
8510 error (_("This target does not support hardware ranged breakpoints."));
8511
8512 bp_count = hw_breakpoint_used_count ();
8513 bp_count += target_ranged_break_num_registers ();
8514 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8515 bp_count, 0);
8516 if (can_use_bp < 0)
8517 error (_("Hardware breakpoints used exceeds limit."));
8518
8519 if (arg == NULL || arg[0] == '\0')
8520 error(_("No address range specified."));
8521
8522 sals_start.sals = NULL;
8523 sals_start.nelts = 0;
8524 init_linespec_result (&canonical_start);
8525
8526 while (*arg == ' ' || *arg == '\t')
8527 arg++;
8528
58438ac1 8529 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8530
8531 sal_start = sals_start.sals[0];
8532 addr_string_start = canonical_start.canonical[0];
8533 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8534 xfree (sals_start.sals);
8535 xfree (canonical_start.canonical);
8536
8537 if (arg[0] != ',')
8538 error (_("Too few arguments."));
8539 else if (sals_start.nelts == 0)
8540 error (_("Could not find location of the beginning of the range."));
8541 else if (sals_start.nelts != 1)
8542 error (_("Cannot create a ranged breakpoint with multiple locations."));
8543
8544 resolve_sal_pc (&sal_start);
8545
8546 arg++; /* Skip the comma. */
8547 while (*arg == ' ' || *arg == '\t')
8548 arg++;
8549
8550 /* Parse the end location. */
8551
8552 sals_end.sals = NULL;
8553 sals_end.nelts = 0;
8554 init_linespec_result (&canonical_end);
8555 arg_start = arg;
8556
423f41a5 8557 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8558 parse_breakpoint_sals because we need to specify the start location's
8559 symtab and line as the default symtab and line for the end of the
8560 range. This makes it possible to have ranges like "foo.c:27, +14",
8561 where +14 means 14 lines from the start location. */
8562 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8563 &canonical_end);
f1310107
TJB
8564
8565 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8566 if (canonical_end.canonical == NULL)
38a714bb 8567 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8568 /* Add the string if not present. */
8569 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8570 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8571
8572 sal_end = sals_end.sals[0];
8573 addr_string_end = canonical_end.canonical[0];
8574 make_cleanup (xfree, addr_string_end);
8575 xfree (sals_end.sals);
8576 xfree (canonical_end.canonical);
8577
8578 if (sals_end.nelts == 0)
8579 error (_("Could not find location of the end of the range."));
8580 else if (sals_end.nelts != 1)
8581 error (_("Cannot create a ranged breakpoint with multiple locations."));
8582
8583 resolve_sal_pc (&sal_end);
8584
8585 end = find_breakpoint_range_end (sal_end);
8586 if (sal_start.pc > end)
8587 error (_("Invalid address range, end preceeds start."));
8588
8589 length = end - sal_start.pc + 1;
8590 if (length < 0)
8591 /* Length overflowed. */
8592 error (_("Address range too large."));
8593 else if (length == 1)
8594 {
8595 /* This range is simple enough to be handled by
8596 the `hbreak' command. */
8597 hbreak_command (addr_string_start, 1);
8598
8599 do_cleanups (cleanup_bkpt);
8600
8601 return;
8602 }
8603
8604 /* Now set up the breakpoint. */
8605 b = set_raw_breakpoint (get_current_arch (), sal_start,
8606 bp_hardware_breakpoint);
8607 set_breakpoint_count (breakpoint_count + 1);
8608 b->number = breakpoint_count;
8609 b->disposition = disp_donttouch;
8610 b->addr_string = addr_string_start;
8611 b->addr_string_range_end = addr_string_end;
8612 b->ops = &ranged_breakpoint_ops;
8613 b->loc->length = length;
8614
8615 discard_cleanups (cleanup_bkpt);
8616
8617 mention (b);
8d3788bd 8618 observer_notify_breakpoint_created (b);
f1310107
TJB
8619 update_global_location_list (1);
8620}
8621
4a64f543
MS
8622/* Return non-zero if EXP is verified as constant. Returned zero
8623 means EXP is variable. Also the constant detection may fail for
8624 some constant expressions and in such case still falsely return
8625 zero. */
65d79d4b
SDJ
8626static int
8627watchpoint_exp_is_const (const struct expression *exp)
8628{
8629 int i = exp->nelts;
8630
8631 while (i > 0)
8632 {
8633 int oplenp, argsp;
8634
8635 /* We are only interested in the descriptor of each element. */
8636 operator_length (exp, i, &oplenp, &argsp);
8637 i -= oplenp;
8638
8639 switch (exp->elts[i].opcode)
8640 {
8641 case BINOP_ADD:
8642 case BINOP_SUB:
8643 case BINOP_MUL:
8644 case BINOP_DIV:
8645 case BINOP_REM:
8646 case BINOP_MOD:
8647 case BINOP_LSH:
8648 case BINOP_RSH:
8649 case BINOP_LOGICAL_AND:
8650 case BINOP_LOGICAL_OR:
8651 case BINOP_BITWISE_AND:
8652 case BINOP_BITWISE_IOR:
8653 case BINOP_BITWISE_XOR:
8654 case BINOP_EQUAL:
8655 case BINOP_NOTEQUAL:
8656 case BINOP_LESS:
8657 case BINOP_GTR:
8658 case BINOP_LEQ:
8659 case BINOP_GEQ:
8660 case BINOP_REPEAT:
8661 case BINOP_COMMA:
8662 case BINOP_EXP:
8663 case BINOP_MIN:
8664 case BINOP_MAX:
8665 case BINOP_INTDIV:
8666 case BINOP_CONCAT:
8667 case BINOP_IN:
8668 case BINOP_RANGE:
8669 case TERNOP_COND:
8670 case TERNOP_SLICE:
8671 case TERNOP_SLICE_COUNT:
8672
8673 case OP_LONG:
8674 case OP_DOUBLE:
8675 case OP_DECFLOAT:
8676 case OP_LAST:
8677 case OP_COMPLEX:
8678 case OP_STRING:
8679 case OP_BITSTRING:
8680 case OP_ARRAY:
8681 case OP_TYPE:
8682 case OP_NAME:
8683 case OP_OBJC_NSSTRING:
8684
8685 case UNOP_NEG:
8686 case UNOP_LOGICAL_NOT:
8687 case UNOP_COMPLEMENT:
8688 case UNOP_ADDR:
8689 case UNOP_HIGH:
4a64f543
MS
8690 /* Unary, binary and ternary operators: We have to check
8691 their operands. If they are constant, then so is the
8692 result of that operation. For instance, if A and B are
8693 determined to be constants, then so is "A + B".
8694
8695 UNOP_IND is one exception to the rule above, because the
8696 value of *ADDR is not necessarily a constant, even when
8697 ADDR is. */
65d79d4b
SDJ
8698 break;
8699
8700 case OP_VAR_VALUE:
8701 /* Check whether the associated symbol is a constant.
4a64f543 8702
65d79d4b 8703 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8704 possible that a buggy compiler could mark a variable as
8705 constant even when it is not, and TYPE_CONST would return
8706 true in this case, while SYMBOL_CLASS wouldn't.
8707
8708 We also have to check for function symbols because they
8709 are always constant. */
65d79d4b
SDJ
8710 {
8711 struct symbol *s = exp->elts[i + 2].symbol;
8712
8713 if (SYMBOL_CLASS (s) != LOC_BLOCK
8714 && SYMBOL_CLASS (s) != LOC_CONST
8715 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8716 return 0;
8717 break;
8718 }
8719
8720 /* The default action is to return 0 because we are using
8721 the optimistic approach here: If we don't know something,
8722 then it is not a constant. */
8723 default:
8724 return 0;
8725 }
8726 }
8727
8728 return 1;
8729}
8730
77b06cd7
TJB
8731/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8732
8733static int
8734insert_watchpoint (struct bp_location *bl)
8735{
e09342b5
TJB
8736 int length = bl->owner->exact? 1 : bl->length;
8737
8738 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8739 bl->owner->cond_exp);
77b06cd7
TJB
8740}
8741
8742/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8743
8744static int
8745remove_watchpoint (struct bp_location *bl)
8746{
e09342b5
TJB
8747 int length = bl->owner->exact? 1 : bl->length;
8748
8749 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8750 bl->owner->cond_exp);
8751}
8752
8753/* Implement the "resources_needed" breakpoint_ops method for
8754 hardware watchpoints. */
8755
8756static int
8757resources_needed_watchpoint (const struct bp_location *bl)
8758{
90e4670f 8759 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8760
90e4670f 8761 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8762}
8763
8764/* The breakpoint_ops structure to be used in hardware watchpoints. */
8765
8766static struct breakpoint_ops watchpoint_breakpoint_ops =
8767{
8768 insert_watchpoint,
8769 remove_watchpoint,
8770 NULL, /* breakpoint_hit */
e09342b5 8771 resources_needed_watchpoint,
77b06cd7
TJB
8772 NULL, /* print_it */
8773 NULL, /* print_one */
f1310107 8774 NULL, /* print_one_detail */
77b06cd7
TJB
8775 NULL, /* print_mention */
8776 NULL /* print_recreate */
8777};
8778
53a5351d
JM
8779/* accessflag: hw_write: watch write,
8780 hw_read: watch read,
8781 hw_access: watch access (read or write) */
c906108c 8782static void
84f4c1fe
PM
8783watch_command_1 (char *arg, int accessflag, int from_tty,
8784 int just_location, int internal)
c906108c 8785{
d983da9c 8786 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8787 struct expression *exp;
60e1c644 8788 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8789 struct value *val, *mark, *result;
c906108c 8790 struct frame_info *frame;
c906108c
SS
8791 char *exp_start = NULL;
8792 char *exp_end = NULL;
37e4754d 8793 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8794 int toklen;
8795 char *cond_start = NULL;
8796 char *cond_end = NULL;
c906108c
SS
8797 int i, other_type_used, target_resources_ok = 0;
8798 enum bptype bp_type;
e09342b5 8799 int reg_cnt = 0;
37e4754d 8800 int thread = -1;
0cf6dd15 8801 int pc = 0;
c906108c 8802
37e4754d
LM
8803 /* Make sure that we actually have parameters to parse. */
8804 if (arg != NULL && arg[0] != '\0')
8805 {
8806 toklen = strlen (arg); /* Size of argument list. */
8807
8808 /* Points tok to the end of the argument list. */
8809 tok = arg + toklen - 1;
8810
4a64f543
MS
8811 /* Go backwards in the parameters list. Skip the last
8812 parameter. If we're expecting a 'thread <thread_num>'
8813 parameter, this should be the thread identifier. */
37e4754d
LM
8814 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8815 tok--;
8816 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8817 tok--;
8818
8819 /* Points end_tok to the beginning of the last token. */
8820 id_tok_start = tok + 1;
8821
4a64f543
MS
8822 /* Go backwards in the parameters list. Skip one more
8823 parameter. If we're expecting a 'thread <thread_num>'
8824 parameter, we should reach a "thread" token. */
37e4754d
LM
8825 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8826 tok--;
8827
8828 end_tok = tok;
8829
8830 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8831 tok--;
8832
8833 /* Move the pointer forward to skip the whitespace and
8834 calculate the length of the token. */
8835 tok++;
8836 toklen = end_tok - tok;
8837
8838 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8839 {
8840 /* At this point we've found a "thread" token, which means
8841 the user is trying to set a watchpoint that triggers
8842 only in a specific thread. */
8843 char *endp;
8844
8845 /* Extract the thread ID from the next token. */
8846 thread = strtol (id_tok_start, &endp, 0);
8847
8848 /* Check if the user provided a valid numeric value for the
8849 thread ID. */
8850 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8851 error (_("Invalid thread ID specification %s."), id_tok_start);
8852
8853 /* Check if the thread actually exists. */
8854 if (!valid_thread_id (thread))
8855 error (_("Unknown thread %d."), thread);
8856
8857 /* Truncate the string and get rid of the thread <thread_num>
8858 parameter before the parameter list is parsed by the
8859 evaluate_expression() function. */
8860 *tok = '\0';
8861 }
8862 }
8863
8864 /* Parse the rest of the arguments. */
c906108c
SS
8865 innermost_block = NULL;
8866 exp_start = arg;
8867 exp = parse_exp_1 (&arg, 0, 0);
8868 exp_end = arg;
fa8a61dc
TT
8869 /* Remove trailing whitespace from the expression before saving it.
8870 This makes the eventual display of the expression string a bit
8871 prettier. */
8872 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8873 --exp_end;
8874
65d79d4b
SDJ
8875 /* Checking if the expression is not constant. */
8876 if (watchpoint_exp_is_const (exp))
8877 {
8878 int len;
8879
8880 len = exp_end - exp_start;
8881 while (len > 0 && isspace (exp_start[len - 1]))
8882 len--;
8883 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8884 }
8885
c906108c
SS
8886 exp_valid_block = innermost_block;
8887 mark = value_mark ();
a1442452 8888 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8889
8890 if (just_location)
8891 {
8892 exp_valid_block = NULL;
a1442452 8893 val = value_addr (result);
06a64a0b
TT
8894 release_value (val);
8895 value_free_to_mark (mark);
8896 }
8897 else if (val != NULL)
fa4727a6 8898 release_value (val);
c906108c 8899
e9cafbcc
TT
8900 tok = skip_spaces (arg);
8901 end_tok = skip_to_space (tok);
c906108c
SS
8902
8903 toklen = end_tok - tok;
8904 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8905 {
2d134ed3
PA
8906 struct expression *cond;
8907
60e1c644 8908 innermost_block = NULL;
c906108c
SS
8909 tok = cond_start = end_tok + 1;
8910 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8911
8912 /* The watchpoint expression may not be local, but the condition
8913 may still be. E.g.: `watch global if local > 0'. */
8914 cond_exp_valid_block = innermost_block;
8915
2d134ed3 8916 xfree (cond);
c906108c
SS
8917 cond_end = tok;
8918 }
8919 if (*tok)
8a3fe4f8 8920 error (_("Junk at end of command."));
c906108c 8921
53a5351d 8922 if (accessflag == hw_read)
c5aa993b 8923 bp_type = bp_read_watchpoint;
53a5351d 8924 else if (accessflag == hw_access)
c5aa993b
JM
8925 bp_type = bp_access_watchpoint;
8926 else
8927 bp_type = bp_hardware_watchpoint;
c906108c 8928
e09342b5
TJB
8929 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8930 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8931 error (_("Expression cannot be implemented with read/access watchpoint."));
e09342b5 8932 if (reg_cnt != 0)
c5aa993b
JM
8933 {
8934 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8935 target_resources_ok =
e09342b5 8936 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
53a5351d 8937 other_type_used);
c5aa993b 8938 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8939 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8940
c5aa993b 8941 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
3e43a32a
MS
8942 error (_("Target can only support one kind "
8943 "of HW watchpoint at a time."));
c5aa993b 8944 }
c906108c 8945
4a64f543
MS
8946 /* Change the type of breakpoint to an ordinary watchpoint if a
8947 hardware watchpoint could not be set. */
e09342b5 8948 if (!reg_cnt || target_resources_ok <= 0)
4d28f7a8
KB
8949 bp_type = bp_watchpoint;
8950
d983da9c 8951 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8952
8953 /* If the expression is "local", then set up a "watchpoint scope"
8954 breakpoint at the point where we've left the scope of the watchpoint
8955 expression. Create the scope breakpoint before the watchpoint, so
8956 that we will encounter it first in bpstat_stop_status. */
60e1c644 8957 if (exp_valid_block && frame)
d983da9c 8958 {
edb3359d
DJ
8959 if (frame_id_p (frame_unwind_caller_id (frame)))
8960 {
8961 scope_breakpoint
a6d9a66e
UW
8962 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8963 frame_unwind_caller_pc (frame),
edb3359d 8964 bp_watchpoint_scope);
d983da9c 8965
edb3359d 8966 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8967
edb3359d
DJ
8968 /* Automatically delete the breakpoint when it hits. */
8969 scope_breakpoint->disposition = disp_del;
d983da9c 8970
edb3359d
DJ
8971 /* Only break in the proper frame (help with recursion). */
8972 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8973
edb3359d 8974 /* Set the address at which we will stop. */
a6d9a66e
UW
8975 scope_breakpoint->loc->gdbarch
8976 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8977 scope_breakpoint->loc->requested_address
8978 = frame_unwind_caller_pc (frame);
8979 scope_breakpoint->loc->address
a6d9a66e
UW
8980 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8981 scope_breakpoint->loc->requested_address,
edb3359d
DJ
8982 scope_breakpoint->type);
8983 }
d983da9c
DJ
8984 }
8985
c906108c 8986 /* Now set up the breakpoint. */
2d134ed3 8987 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 8988 set_breakpoint_number (internal, b);
37e4754d 8989 b->thread = thread;
b5de0fa7 8990 b->disposition = disp_donttouch;
c906108c
SS
8991 b->exp = exp;
8992 b->exp_valid_block = exp_valid_block;
60e1c644 8993 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
8994 if (just_location)
8995 {
8996 struct type *t = value_type (val);
8997 CORE_ADDR addr = value_as_address (val);
8998 char *name;
8999
9000 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9001 name = type_to_string (t);
9002
d63d0675
JK
9003 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9004 core_addr_to_string (addr));
06a64a0b
TT
9005 xfree (name);
9006
4a4106ca 9007 b->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9008 (int) (exp_end - exp_start), exp_start);
9009
06a64a0b
TT
9010 /* The above expression is in C. */
9011 b->language = language_c;
9012 }
9013 else
9014 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 9015 b->val = val;
fa4727a6 9016 b->val_valid = 1;
77b06cd7
TJB
9017 b->ops = &watchpoint_breakpoint_ops;
9018
e09342b5
TJB
9019 /* Use an exact watchpoint when there's only one memory region to be
9020 watched, and only one debug register is needed to watch it. */
9021 b->exact = target_exact_watchpoints && reg_cnt == 1;
9022
c906108c
SS
9023 if (cond_start)
9024 b->cond_string = savestring (cond_start, cond_end - cond_start);
9025 else
9026 b->cond_string = 0;
c5aa993b 9027
c906108c 9028 if (frame)
f6bc2008
PA
9029 {
9030 b->watchpoint_frame = get_frame_id (frame);
9031 b->watchpoint_thread = inferior_ptid;
9032 }
c906108c 9033 else
f6bc2008
PA
9034 {
9035 b->watchpoint_frame = null_frame_id;
9036 b->watchpoint_thread = null_ptid;
9037 }
c906108c 9038
d983da9c 9039 if (scope_breakpoint != NULL)
c906108c 9040 {
d983da9c
DJ
9041 /* The scope breakpoint is related to the watchpoint. We will
9042 need to act on them together. */
9043 b->related_breakpoint = scope_breakpoint;
9044 scope_breakpoint->related_breakpoint = b;
c906108c 9045 }
d983da9c 9046
06a64a0b
TT
9047 if (!just_location)
9048 value_free_to_mark (mark);
2d134ed3
PA
9049
9050 /* Finally update the new watchpoint. This creates the locations
9051 that should be inserted. */
9052 update_watchpoint (b, 1);
8d3788bd
VP
9053
9054 /* Do not mention breakpoints with a negative number, but do
84f4c1fe 9055 notify observers. */
8d3788bd 9056 if (!internal)
84f4c1fe 9057 mention (b);
8d3788bd
VP
9058 observer_notify_breakpoint_created (b);
9059
b60e7edf 9060 update_global_location_list (1);
c906108c
SS
9061}
9062
e09342b5
TJB
9063/* Return count of debug registers needed to watch the given expression.
9064 If EXACT_WATCHPOINTS is 1, then consider that only the address of
9065 the start of the watched region will be monitored (i.e., all accesses
9066 will be aligned). This uses less debug registers on some targets.
9067
9068 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9069
c906108c 9070static int
e09342b5 9071can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
c906108c
SS
9072{
9073 int found_memory_cnt = 0;
2e70b7b9 9074 struct value *head = v;
c906108c
SS
9075
9076 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9077 if (!can_use_hw_watchpoints)
c906108c 9078 return 0;
c5aa993b 9079
5c44784c
JM
9080 /* Make sure that the value of the expression depends only upon
9081 memory contents, and values computed from them within GDB. If we
9082 find any register references or function calls, we can't use a
9083 hardware watchpoint.
9084
9085 The idea here is that evaluating an expression generates a series
9086 of values, one holding the value of every subexpression. (The
9087 expression a*b+c has five subexpressions: a, b, a*b, c, and
9088 a*b+c.) GDB's values hold almost enough information to establish
9089 the criteria given above --- they identify memory lvalues,
9090 register lvalues, computed values, etcetera. So we can evaluate
9091 the expression, and then scan the chain of values that leaves
9092 behind to decide whether we can detect any possible change to the
9093 expression's final value using only hardware watchpoints.
9094
9095 However, I don't think that the values returned by inferior
9096 function calls are special in any way. So this function may not
9097 notice that an expression involving an inferior function call
9098 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9099 for (; v; v = value_next (v))
c906108c 9100 {
5c44784c 9101 if (VALUE_LVAL (v) == lval_memory)
c906108c 9102 {
8464be76
DJ
9103 if (v != head && value_lazy (v))
9104 /* A lazy memory lvalue in the chain is one that GDB never
9105 needed to fetch; we either just used its address (e.g.,
9106 `a' in `a.b') or we never needed it at all (e.g., `a'
9107 in `a,b'). This doesn't apply to HEAD; if that is
9108 lazy then it was not readable, but watch it anyway. */
5c44784c 9109 ;
53a5351d 9110 else
5c44784c
JM
9111 {
9112 /* Ahh, memory we actually used! Check if we can cover
9113 it with hardware watchpoints. */
df407dfe 9114 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9115
9116 /* We only watch structs and arrays if user asked for it
9117 explicitly, never if they just happen to appear in a
9118 middle of some value chain. */
9119 if (v == head
9120 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9121 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9122 {
42ae5230 9123 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9124 int len;
9125 int num_regs;
9126
9127 len = (exact_watchpoints
9128 && is_scalar_type_recursive (vtype))?
9129 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9130
e09342b5
TJB
9131 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9132 if (!num_regs)
2e70b7b9
MS
9133 return 0;
9134 else
e09342b5 9135 found_memory_cnt += num_regs;
2e70b7b9 9136 }
5c44784c 9137 }
c5aa993b 9138 }
5086187c
AC
9139 else if (VALUE_LVAL (v) != not_lval
9140 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9141 return 0; /* These are values from the history (e.g., $1). */
5086187c 9142 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9143 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9144 }
9145
9146 /* The expression itself looks suitable for using a hardware
9147 watchpoint, but give the target machine a chance to reject it. */
9148 return found_memory_cnt;
9149}
9150
8b93c638 9151void
84f4c1fe 9152watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9153{
84f4c1fe 9154 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9155}
9156
9157/* A helper function that looks for an argument at the start of a
9158 string. The argument must also either be at the end of the string,
9159 or be followed by whitespace. Returns 1 if it finds the argument,
9160 0 otherwise. If the argument is found, it updates *STR. */
9161
9162static int
9163check_for_argument (char **str, char *arg, int arg_len)
9164{
9165 if (strncmp (*str, arg, arg_len) == 0
9166 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9167 {
9168 *str += arg_len;
9169 return 1;
9170 }
9171 return 0;
9172}
9173
9174/* A helper function that looks for the "-location" argument and then
9175 calls watch_command_1. */
9176
9177static void
9178watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9179{
9180 int just_location = 0;
9181
9182 if (arg
9183 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9184 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9185 {
e9cafbcc 9186 arg = skip_spaces (arg);
06a64a0b
TT
9187 just_location = 1;
9188 }
9189
84f4c1fe 9190 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9191}
8926118c 9192
c5aa993b 9193static void
fba45db2 9194watch_command (char *arg, int from_tty)
c906108c 9195{
06a64a0b 9196 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9197}
9198
8b93c638 9199void
84f4c1fe 9200rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9201{
84f4c1fe 9202 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9203}
8926118c 9204
c5aa993b 9205static void
fba45db2 9206rwatch_command (char *arg, int from_tty)
c906108c 9207{
06a64a0b 9208 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9209}
9210
8b93c638 9211void
84f4c1fe 9212awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9213{
84f4c1fe 9214 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9215}
8926118c 9216
c5aa993b 9217static void
fba45db2 9218awatch_command (char *arg, int from_tty)
c906108c 9219{
06a64a0b 9220 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9221}
c906108c 9222\f
c5aa993b 9223
43ff13b4 9224/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9225 because it uses the mechanisms of breakpoints. */
9226
bfec99b2
PA
9227struct until_break_command_continuation_args
9228{
9229 struct breakpoint *breakpoint;
9230 struct breakpoint *breakpoint2;
186c406b 9231 int thread_num;
bfec99b2
PA
9232};
9233
43ff13b4 9234/* This function is called by fetch_inferior_event via the
4a64f543 9235 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9236 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9237 command. */
c2c6d25f 9238static void
604ead4a 9239until_break_command_continuation (void *arg)
43ff13b4 9240{
bfec99b2
PA
9241 struct until_break_command_continuation_args *a = arg;
9242
9243 delete_breakpoint (a->breakpoint);
9244 if (a->breakpoint2)
9245 delete_breakpoint (a->breakpoint2);
186c406b 9246 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9247}
9248
c906108c 9249void
ae66c1fc 9250until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9251{
9252 struct symtabs_and_lines sals;
9253 struct symtab_and_line sal;
206415a3 9254 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9255 struct breakpoint *breakpoint;
f107f563 9256 struct breakpoint *breakpoint2 = NULL;
c906108c 9257 struct cleanup *old_chain;
186c406b
TT
9258 int thread;
9259 struct thread_info *tp;
c906108c
SS
9260
9261 clear_proceed_status ();
9262
9263 /* Set a breakpoint where the user wants it and at return from
4a64f543 9264 this function. */
c5aa993b 9265
c906108c
SS
9266 if (default_breakpoint_valid)
9267 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9268 default_breakpoint_line, NULL);
c906108c 9269 else
58438ac1 9270 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9271
c906108c 9272 if (sals.nelts != 1)
8a3fe4f8 9273 error (_("Couldn't get information on specified line."));
c5aa993b 9274
c906108c 9275 sal = sals.sals[0];
4a64f543 9276 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9277
c906108c 9278 if (*arg)
8a3fe4f8 9279 error (_("Junk at end of arguments."));
c5aa993b 9280
c906108c 9281 resolve_sal_pc (&sal);
c5aa993b 9282
ae66c1fc
EZ
9283 if (anywhere)
9284 /* If the user told us to continue until a specified location,
9285 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9286 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9287 null_frame_id, bp_until);
ae66c1fc 9288 else
4a64f543
MS
9289 /* Otherwise, specify the selected frame, because we want to stop
9290 only at the very same frame. */
a6d9a66e
UW
9291 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9292 get_stack_frame_id (frame),
ae66c1fc 9293 bp_until);
c5aa993b 9294
f107f563 9295 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9296
186c406b
TT
9297 tp = inferior_thread ();
9298 thread = tp->num;
9299
ae66c1fc
EZ
9300 /* Keep within the current frame, or in frames called by the current
9301 one. */
edb3359d
DJ
9302
9303 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9304 {
edb3359d
DJ
9305 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9306 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9307 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9308 sal,
edb3359d 9309 frame_unwind_caller_id (frame),
f107f563
VP
9310 bp_until);
9311 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9312
9313 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9314 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9315 }
c5aa993b 9316
c906108c 9317 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9318
4a64f543
MS
9319 /* If we are running asynchronously, and proceed call above has
9320 actually managed to start the target, arrange for breakpoints to
9321 be deleted when the target stops. Otherwise, we're already
9322 stopped and delete breakpoints via cleanup chain. */
f107f563 9323
8ea051c5 9324 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9325 {
bfec99b2
PA
9326 struct until_break_command_continuation_args *args;
9327 args = xmalloc (sizeof (*args));
f107f563 9328
bfec99b2
PA
9329 args->breakpoint = breakpoint;
9330 args->breakpoint2 = breakpoint2;
186c406b 9331 args->thread_num = thread;
f107f563
VP
9332
9333 discard_cleanups (old_chain);
95e54da7
PA
9334 add_continuation (inferior_thread (),
9335 until_break_command_continuation, args,
604ead4a 9336 xfree);
f107f563
VP
9337 }
9338 else
c5aa993b 9339 do_cleanups (old_chain);
c906108c 9340}
ae66c1fc 9341
c906108c
SS
9342/* This function attempts to parse an optional "if <cond>" clause
9343 from the arg string. If one is not found, it returns NULL.
c5aa993b 9344
c906108c
SS
9345 Else, it returns a pointer to the condition string. (It does not
9346 attempt to evaluate the string against a particular block.) And,
9347 it updates arg to point to the first character following the parsed
4a64f543 9348 if clause in the arg string. */
53a5351d 9349
c906108c 9350static char *
fba45db2 9351ep_parse_optional_if_clause (char **arg)
c906108c 9352{
c5aa993b
JM
9353 char *cond_string;
9354
9355 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9356 return NULL;
c5aa993b 9357
4a64f543 9358 /* Skip the "if" keyword. */
c906108c 9359 (*arg) += 2;
c5aa993b 9360
c906108c 9361 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9362 condition string. */
e9cafbcc 9363 *arg = skip_spaces (*arg);
c906108c 9364 cond_string = *arg;
c5aa993b 9365
4a64f543
MS
9366 /* Assume that the condition occupies the remainder of the arg
9367 string. */
c906108c 9368 (*arg) += strlen (cond_string);
c5aa993b 9369
c906108c
SS
9370 return cond_string;
9371}
c5aa993b 9372
c906108c
SS
9373/* Commands to deal with catching events, such as signals, exceptions,
9374 process start/exit, etc. */
c5aa993b
JM
9375
9376typedef enum
9377{
44feb3ce
TT
9378 catch_fork_temporary, catch_vfork_temporary,
9379 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9380}
9381catch_fork_kind;
9382
c906108c 9383static void
cc59ec59
MS
9384catch_fork_command_1 (char *arg, int from_tty,
9385 struct cmd_list_element *command)
c906108c 9386{
a6d9a66e 9387 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9388 char *cond_string = NULL;
44feb3ce
TT
9389 catch_fork_kind fork_kind;
9390 int tempflag;
9391
9392 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9393 tempflag = (fork_kind == catch_fork_temporary
9394 || fork_kind == catch_vfork_temporary);
c5aa993b 9395
44feb3ce
TT
9396 if (!arg)
9397 arg = "";
e9cafbcc 9398 arg = skip_spaces (arg);
c5aa993b 9399
c906108c 9400 /* The allowed syntax is:
c5aa993b
JM
9401 catch [v]fork
9402 catch [v]fork if <cond>
9403
4a64f543 9404 First, check if there's an if clause. */
c906108c 9405 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9406
c906108c 9407 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9408 error (_("Junk at end of arguments."));
c5aa993b 9409
c906108c 9410 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9411 and enable reporting of such events. */
c5aa993b
JM
9412 switch (fork_kind)
9413 {
44feb3ce
TT
9414 case catch_fork_temporary:
9415 case catch_fork_permanent:
a6d9a66e 9416 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9417 &catch_fork_breakpoint_ops);
c906108c 9418 break;
44feb3ce
TT
9419 case catch_vfork_temporary:
9420 case catch_vfork_permanent:
a6d9a66e 9421 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9422 &catch_vfork_breakpoint_ops);
c906108c 9423 break;
c5aa993b 9424 default:
8a3fe4f8 9425 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9426 break;
c5aa993b 9427 }
c906108c
SS
9428}
9429
9430static void
cc59ec59
MS
9431catch_exec_command_1 (char *arg, int from_tty,
9432 struct cmd_list_element *command)
c906108c 9433{
a6d9a66e 9434 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9435 int tempflag;
c5aa993b 9436 char *cond_string = NULL;
c906108c 9437
44feb3ce
TT
9438 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9439
9440 if (!arg)
9441 arg = "";
e9cafbcc 9442 arg = skip_spaces (arg);
c906108c
SS
9443
9444 /* The allowed syntax is:
c5aa993b
JM
9445 catch exec
9446 catch exec if <cond>
c906108c 9447
4a64f543 9448 First, check if there's an if clause. */
c906108c
SS
9449 cond_string = ep_parse_optional_if_clause (&arg);
9450
9451 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9452 error (_("Junk at end of arguments."));
c906108c
SS
9453
9454 /* If this target supports it, create an exec catchpoint
4a64f543 9455 and enable reporting of such events. */
a6d9a66e
UW
9456 create_catchpoint (gdbarch, tempflag, cond_string,
9457 &catch_exec_breakpoint_ops);
c906108c 9458}
c5aa993b 9459
3086aeae 9460static enum print_stop_action
ba770c9c 9461print_it_exception_catchpoint (struct breakpoint *b)
3086aeae 9462{
ade92717 9463 int bp_temp, bp_throw;
3086aeae 9464
ade92717 9465 annotate_catchpoint (b->number);
3086aeae 9466
ade92717
AR
9467 bp_throw = strstr (b->addr_string, "throw") != NULL;
9468 if (b->loc->address != b->loc->requested_address)
9469 breakpoint_adjustment_warning (b->loc->requested_address,
9470 b->loc->address,
9471 b->number, 1);
df2b6d2d 9472 bp_temp = b->disposition == disp_del;
ade92717
AR
9473 ui_out_text (uiout,
9474 bp_temp ? "Temporary catchpoint "
9475 : "Catchpoint ");
9476 if (!ui_out_is_mi_like_p (uiout))
9477 ui_out_field_int (uiout, "bkptno", b->number);
9478 ui_out_text (uiout,
c0b37c48
AR
9479 bp_throw ? " (exception thrown), "
9480 : " (exception caught), ");
ade92717
AR
9481 if (ui_out_is_mi_like_p (uiout))
9482 {
9483 ui_out_field_string (uiout, "reason",
9484 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9485 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9486 ui_out_field_int (uiout, "bkptno", b->number);
9487 }
3086aeae
DJ
9488 return PRINT_SRC_AND_LOC;
9489}
9490
9491static void
cc59ec59
MS
9492print_one_exception_catchpoint (struct breakpoint *b,
9493 struct bp_location **last_loc)
3086aeae 9494{
79a45b7d 9495 struct value_print_options opts;
cc59ec59 9496
79a45b7d
TT
9497 get_user_print_options (&opts);
9498 if (opts.addressprint)
3086aeae
DJ
9499 {
9500 annotate_field (4);
604133b5
AR
9501 if (b->loc == NULL || b->loc->shlib_disabled)
9502 ui_out_field_string (uiout, "addr", "<PENDING>");
9503 else
5af949e3
UW
9504 ui_out_field_core_addr (uiout, "addr",
9505 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9506 }
9507 annotate_field (5);
604133b5 9508 if (b->loc)
a6d9a66e 9509 *last_loc = b->loc;
3086aeae
DJ
9510 if (strstr (b->addr_string, "throw") != NULL)
9511 ui_out_field_string (uiout, "what", "exception throw");
9512 else
9513 ui_out_field_string (uiout, "what", "exception catch");
9514}
9515
9516static void
9517print_mention_exception_catchpoint (struct breakpoint *b)
9518{
ade92717
AR
9519 int bp_temp;
9520 int bp_throw;
9521
df2b6d2d 9522 bp_temp = b->disposition == disp_del;
ade92717
AR
9523 bp_throw = strstr (b->addr_string, "throw") != NULL;
9524 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9525 : _("Catchpoint "));
9526 ui_out_field_int (uiout, "bkptno", b->number);
9527 ui_out_text (uiout, bp_throw ? _(" (throw)")
9528 : _(" (catch)"));
3086aeae
DJ
9529}
9530
6149aea9
PA
9531/* Implement the "print_recreate" breakpoint_ops method for throw and
9532 catch catchpoints. */
9533
9534static void
4a64f543
MS
9535print_recreate_exception_catchpoint (struct breakpoint *b,
9536 struct ui_file *fp)
6149aea9
PA
9537{
9538 int bp_temp;
9539 int bp_throw;
9540
9541 bp_temp = b->disposition == disp_del;
9542 bp_throw = strstr (b->addr_string, "throw") != NULL;
9543 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9544 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9545}
9546
3086aeae 9547static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
9548 NULL, /* insert */
9549 NULL, /* remove */
9550 NULL, /* breakpoint_hit */
e09342b5 9551 NULL, /* resources_needed */
ba770c9c 9552 print_it_exception_catchpoint,
3086aeae 9553 print_one_exception_catchpoint,
f1310107 9554 NULL, /* print_one_detail */
6149aea9
PA
9555 print_mention_exception_catchpoint,
9556 print_recreate_exception_catchpoint
3086aeae
DJ
9557};
9558
9559static int
9560handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9561 enum exception_event_kind ex_event, int from_tty)
9562{
604133b5
AR
9563 char *trigger_func_name;
9564
3086aeae 9565 if (ex_event == EX_EVENT_CATCH)
604133b5 9566 trigger_func_name = "__cxa_begin_catch";
3086aeae 9567 else
604133b5 9568 trigger_func_name = "__cxa_throw";
3086aeae 9569
8cdf0e15
VP
9570 create_breakpoint (get_current_arch (),
9571 trigger_func_name, cond_string, -1,
9572 0 /* condition and thread are valid. */,
0fb4aa4b 9573 tempflag, bp_breakpoint,
8cdf0e15
VP
9574 0,
9575 AUTO_BOOLEAN_TRUE /* pending */,
9576 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9577 1 /* enabled */,
9578 0 /* internal */);
3086aeae 9579
3086aeae
DJ
9580 return 1;
9581}
9582
4a64f543 9583/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9584
9585static void
fba45db2
KB
9586catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9587 int tempflag, int from_tty)
c906108c 9588{
c5aa993b 9589 char *cond_string = NULL;
c5aa993b 9590
44feb3ce
TT
9591 if (!arg)
9592 arg = "";
e9cafbcc 9593 arg = skip_spaces (arg);
c5aa993b 9594
c906108c
SS
9595 cond_string = ep_parse_optional_if_clause (&arg);
9596
9597 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9598 error (_("Junk at end of arguments."));
c906108c 9599
059fb39f
PM
9600 if (ex_event != EX_EVENT_THROW
9601 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9602 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9603
3086aeae
DJ
9604 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9605 return;
9606
8a3fe4f8 9607 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9608}
9609
44feb3ce
TT
9610/* Implementation of "catch catch" command. */
9611
9612static void
9613catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9614{
9615 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9616
44feb3ce
TT
9617 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9618}
9619
9620/* Implementation of "catch throw" command. */
9621
9622static void
9623catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9624{
9625 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9626
44feb3ce
TT
9627 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9628}
9629
f7f9143b
JB
9630/* Create a breakpoint struct for Ada exception catchpoints. */
9631
9632static void
a6d9a66e
UW
9633create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9634 struct symtab_and_line sal,
f7f9143b
JB
9635 char *addr_string,
9636 char *exp_string,
9637 char *cond_string,
9638 struct expression *cond,
9639 struct breakpoint_ops *ops,
9640 int tempflag,
9641 int from_tty)
9642{
9643 struct breakpoint *b;
9644
9645 if (from_tty)
9646 {
5af949e3
UW
9647 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9648 if (!loc_gdbarch)
9649 loc_gdbarch = gdbarch;
9650
6c95b8df
PA
9651 describe_other_breakpoints (loc_gdbarch,
9652 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9653 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9654 version for exception catchpoints, because two catchpoints
9655 used for different exception names will use the same address.
9656 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9657 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
9658 inapropriate, we should use the word catchpoint, and tell
9659 the user what type of catchpoint it is. The above is good
9660 enough for now, though. */
9661 }
9662
a6d9a66e 9663 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9664 set_breakpoint_count (breakpoint_count + 1);
9665
9666 b->enable_state = bp_enabled;
9667 b->disposition = tempflag ? disp_del : disp_donttouch;
9668 b->number = breakpoint_count;
9669 b->ignore_count = 0;
511a6cd4 9670 b->loc->cond = cond;
f7f9143b
JB
9671 b->addr_string = addr_string;
9672 b->language = language_ada;
9673 b->cond_string = cond_string;
9674 b->exp_string = exp_string;
9675 b->thread = -1;
9676 b->ops = ops;
f7f9143b
JB
9677
9678 mention (b);
8d3788bd 9679 observer_notify_breakpoint_created (b);
b60e7edf 9680 update_global_location_list (1);
f7f9143b
JB
9681}
9682
9683/* Implement the "catch exception" command. */
9684
9685static void
44feb3ce
TT
9686catch_ada_exception_command (char *arg, int from_tty,
9687 struct cmd_list_element *command)
f7f9143b 9688{
a6d9a66e 9689 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9690 int tempflag;
f7f9143b 9691 struct symtab_and_line sal;
f7f9143b
JB
9692 char *addr_string = NULL;
9693 char *exp_string = NULL;
9694 char *cond_string = NULL;
9695 struct expression *cond = NULL;
9696 struct breakpoint_ops *ops = NULL;
9697
44feb3ce
TT
9698 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9699
9700 if (!arg)
9701 arg = "";
f7f9143b
JB
9702 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9703 &cond_string, &cond, &ops);
a6d9a66e 9704 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9705 cond_string, cond, ops, tempflag,
9706 from_tty);
9707}
9708
a96d9b2e
SDJ
9709/* Cleanup function for a syscall filter list. */
9710static void
9711clean_up_filters (void *arg)
9712{
9713 VEC(int) *iter = *(VEC(int) **) arg;
9714 VEC_free (int, iter);
9715}
9716
9717/* Splits the argument using space as delimiter. Returns an xmalloc'd
9718 filter list, or NULL if no filtering is required. */
9719static VEC(int) *
9720catch_syscall_split_args (char *arg)
9721{
9722 VEC(int) *result = NULL;
9723 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9724
9725 while (*arg != '\0')
9726 {
9727 int i, syscall_number;
9728 char *endptr;
9729 char cur_name[128];
9730 struct syscall s;
9731
9732 /* Skip whitespace. */
9733 while (isspace (*arg))
9734 arg++;
9735
9736 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9737 cur_name[i] = arg[i];
9738 cur_name[i] = '\0';
9739 arg += i;
9740
9741 /* Check if the user provided a syscall name or a number. */
9742 syscall_number = (int) strtol (cur_name, &endptr, 0);
9743 if (*endptr == '\0')
bccd0dd2 9744 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9745 else
9746 {
9747 /* We have a name. Let's check if it's valid and convert it
9748 to a number. */
9749 get_syscall_by_name (cur_name, &s);
9750
9751 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9752 /* Here we have to issue an error instead of a warning,
9753 because GDB cannot do anything useful if there's no
9754 syscall number to be caught. */
a96d9b2e
SDJ
9755 error (_("Unknown syscall name '%s'."), cur_name);
9756 }
9757
9758 /* Ok, it's valid. */
9759 VEC_safe_push (int, result, s.number);
9760 }
9761
9762 discard_cleanups (cleanup);
9763 return result;
9764}
9765
9766/* Implement the "catch syscall" command. */
9767
9768static void
cc59ec59
MS
9769catch_syscall_command_1 (char *arg, int from_tty,
9770 struct cmd_list_element *command)
a96d9b2e
SDJ
9771{
9772 int tempflag;
9773 VEC(int) *filter;
9774 struct syscall s;
9775 struct gdbarch *gdbarch = get_current_arch ();
9776
9777 /* Checking if the feature if supported. */
9778 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9779 error (_("The feature 'catch syscall' is not supported on \
ea666128 9780this architecture yet."));
a96d9b2e
SDJ
9781
9782 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9783
e9cafbcc 9784 arg = skip_spaces (arg);
a96d9b2e
SDJ
9785
9786 /* We need to do this first "dummy" translation in order
9787 to get the syscall XML file loaded or, most important,
9788 to display a warning to the user if there's no XML file
9789 for his/her architecture. */
9790 get_syscall_by_number (0, &s);
9791
9792 /* The allowed syntax is:
9793 catch syscall
9794 catch syscall <name | number> [<name | number> ... <name | number>]
9795
9796 Let's check if there's a syscall name. */
9797
9798 if (arg != NULL)
9799 filter = catch_syscall_split_args (arg);
9800 else
9801 filter = NULL;
9802
9803 create_syscall_event_catchpoint (tempflag, filter,
9804 &catch_syscall_breakpoint_ops);
9805}
9806
f7f9143b
JB
9807/* Implement the "catch assert" command. */
9808
9809static void
3e43a32a
MS
9810catch_assert_command (char *arg, int from_tty,
9811 struct cmd_list_element *command)
f7f9143b 9812{
a6d9a66e 9813 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9814 int tempflag;
f7f9143b
JB
9815 struct symtab_and_line sal;
9816 char *addr_string = NULL;
9817 struct breakpoint_ops *ops = NULL;
9818
44feb3ce
TT
9819 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9820
9821 if (!arg)
9822 arg = "";
f7f9143b 9823 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9824 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9825 ops, tempflag, from_tty);
f7f9143b
JB
9826}
9827
c906108c 9828static void
fba45db2 9829catch_command (char *arg, int from_tty)
c906108c 9830{
44feb3ce 9831 error (_("Catch requires an event name."));
c906108c
SS
9832}
9833\f
9834
9835static void
fba45db2 9836tcatch_command (char *arg, int from_tty)
c906108c 9837{
44feb3ce 9838 error (_("Catch requires an event name."));
c906108c
SS
9839}
9840
80f8a6eb 9841/* Delete breakpoints by address or line. */
c906108c
SS
9842
9843static void
fba45db2 9844clear_command (char *arg, int from_tty)
c906108c 9845{
d6e956e5
VP
9846 struct breakpoint *b;
9847 VEC(breakpoint_p) *found = 0;
9848 int ix;
c906108c
SS
9849 int default_match;
9850 struct symtabs_and_lines sals;
9851 struct symtab_and_line sal;
c906108c
SS
9852 int i;
9853
9854 if (arg)
9855 {
9856 sals = decode_line_spec (arg, 1);
9857 default_match = 0;
9858 }
9859 else
9860 {
c5aa993b 9861 sals.sals = (struct symtab_and_line *)
c906108c 9862 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9863 make_cleanup (xfree, sals.sals);
4a64f543 9864 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9865 sal.line = default_breakpoint_line;
9866 sal.symtab = default_breakpoint_symtab;
9867 sal.pc = default_breakpoint_address;
6c95b8df 9868 sal.pspace = default_breakpoint_pspace;
c906108c 9869 if (sal.symtab == 0)
8a3fe4f8 9870 error (_("No source file specified."));
c906108c
SS
9871
9872 sals.sals[0] = sal;
9873 sals.nelts = 1;
9874
9875 default_match = 1;
9876 }
9877
4a64f543
MS
9878 /* We don't call resolve_sal_pc here. That's not as bad as it
9879 seems, because all existing breakpoints typically have both
9880 file/line and pc set. So, if clear is given file/line, we can
9881 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9882
9883 We only support clearing given the address explicitly
9884 present in breakpoint table. Say, we've set breakpoint
4a64f543 9885 at file:line. There were several PC values for that file:line,
ed0616c6 9886 due to optimization, all in one block.
4a64f543
MS
9887
9888 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9889 PC corresponding to the same file:line, the breakpoint won't
9890 be cleared. We probably can still clear the breakpoint, but
9891 since the other PC value is never presented to user, user
9892 can only find it by guessing, and it does not seem important
9893 to support that. */
9894
4a64f543
MS
9895 /* For each line spec given, delete bps which correspond to it. Do
9896 it in two passes, solely to preserve the current behavior that
9897 from_tty is forced true if we delete more than one
9898 breakpoint. */
c906108c 9899
80f8a6eb 9900 found = NULL;
c906108c
SS
9901 for (i = 0; i < sals.nelts; i++)
9902 {
9903 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9904 If line given (pc == 0), clear all bpts on specified line.
9905 If defaulting, clear all bpts on default line
c906108c 9906 or at default pc.
c5aa993b
JM
9907
9908 defaulting sal.pc != 0 tests to do
9909
9910 0 1 pc
9911 1 1 pc _and_ line
9912 0 0 line
9913 1 0 <can't happen> */
c906108c
SS
9914
9915 sal = sals.sals[i];
c906108c 9916
4a64f543 9917 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9918 ALL_BREAKPOINTS (b)
c5aa993b 9919 {
0d381245 9920 int match = 0;
4a64f543 9921 /* Are we going to delete b? */
cc60f2e3 9922 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9923 {
9924 struct bp_location *loc = b->loc;
9925 for (; loc; loc = loc->next)
9926 {
6c95b8df
PA
9927 int pc_match = sal.pc
9928 && (loc->pspace == sal.pspace)
0d381245
VP
9929 && (loc->address == sal.pc)
9930 && (!section_is_overlay (loc->section)
9931 || loc->section == sal.section);
9932 int line_match = ((default_match || (0 == sal.pc))
9933 && b->source_file != NULL
9934 && sal.symtab != NULL
6c95b8df 9935 && sal.pspace == loc->pspace
0ba1096a
KT
9936 && filename_cmp (b->source_file,
9937 sal.symtab->filename) == 0
0d381245
VP
9938 && b->line_number == sal.line);
9939 if (pc_match || line_match)
9940 {
9941 match = 1;
9942 break;
9943 }
9944 }
9945 }
9946
9947 if (match)
d6e956e5 9948 VEC_safe_push(breakpoint_p, found, b);
c906108c 9949 }
80f8a6eb
MS
9950 }
9951 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9952 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9953 {
9954 if (arg)
8a3fe4f8 9955 error (_("No breakpoint at %s."), arg);
80f8a6eb 9956 else
8a3fe4f8 9957 error (_("No breakpoint at this line."));
80f8a6eb 9958 }
c906108c 9959
d6e956e5 9960 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9961 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9962 if (from_tty)
a3f17187 9963 {
d6e956e5 9964 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9965 printf_unfiltered (_("Deleted breakpoint "));
9966 else
9967 printf_unfiltered (_("Deleted breakpoints "));
9968 }
80f8a6eb 9969 breakpoints_changed ();
d6e956e5
VP
9970
9971 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9972 {
c5aa993b 9973 if (from_tty)
d6e956e5
VP
9974 printf_unfiltered ("%d ", b->number);
9975 delete_breakpoint (b);
c906108c 9976 }
80f8a6eb
MS
9977 if (from_tty)
9978 putchar_unfiltered ('\n');
c906108c
SS
9979}
9980\f
9981/* Delete breakpoint in BS if they are `delete' breakpoints and
9982 all breakpoints that are marked for deletion, whether hit or not.
9983 This is called after any breakpoint is hit, or after errors. */
9984
9985void
fba45db2 9986breakpoint_auto_delete (bpstat bs)
c906108c 9987{
35df4500 9988 struct breakpoint *b, *b_tmp;
c906108c
SS
9989
9990 for (; bs; bs = bs->next)
f431efe5
PA
9991 if (bs->breakpoint_at
9992 && bs->breakpoint_at->disposition == disp_del
c906108c 9993 && bs->stop)
f431efe5 9994 delete_breakpoint (bs->breakpoint_at);
c906108c 9995
35df4500 9996 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 9997 {
b5de0fa7 9998 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
9999 delete_breakpoint (b);
10000 }
c906108c
SS
10001}
10002
4a64f543
MS
10003/* A comparison function for bp_location AP and BP being interfaced to
10004 qsort. Sort elements primarily by their ADDRESS (no matter what
10005 does breakpoint_address_is_meaningful say for its OWNER),
10006 secondarily by ordering first bp_permanent OWNERed elements and
10007 terciarily just ensuring the array is sorted stable way despite
10008 qsort being an instable algorithm. */
876fa593
JK
10009
10010static int
494cfb0f 10011bp_location_compare (const void *ap, const void *bp)
876fa593 10012{
494cfb0f
JK
10013 struct bp_location *a = *(void **) ap;
10014 struct bp_location *b = *(void **) bp;
2bdf28a0 10015 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10016 int a_perm = a->owner->enable_state == bp_permanent;
10017 int b_perm = b->owner->enable_state == bp_permanent;
10018
10019 if (a->address != b->address)
10020 return (a->address > b->address) - (a->address < b->address);
10021
10022 /* Sort permanent breakpoints first. */
10023 if (a_perm != b_perm)
10024 return (a_perm < b_perm) - (a_perm > b_perm);
10025
4a64f543
MS
10026 /* Make the user-visible order stable across GDB runs. Locations of
10027 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10028
10029 if (a->owner->number != b->owner->number)
10030 return (a->owner->number > b->owner->number)
10031 - (a->owner->number < b->owner->number);
10032
10033 return (a > b) - (a < b);
10034}
10035
876fa593 10036/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10037 bp_location_shadow_len_after_address_max according to the current
10038 content of the bp_location array. */
f7545552
TT
10039
10040static void
876fa593 10041bp_location_target_extensions_update (void)
f7545552 10042{
876fa593
JK
10043 struct bp_location *bl, **blp_tmp;
10044
10045 bp_location_placed_address_before_address_max = 0;
10046 bp_location_shadow_len_after_address_max = 0;
10047
10048 ALL_BP_LOCATIONS (bl, blp_tmp)
10049 {
10050 CORE_ADDR start, end, addr;
10051
10052 if (!bp_location_has_shadow (bl))
10053 continue;
10054
10055 start = bl->target_info.placed_address;
10056 end = start + bl->target_info.shadow_len;
10057
10058 gdb_assert (bl->address >= start);
10059 addr = bl->address - start;
10060 if (addr > bp_location_placed_address_before_address_max)
10061 bp_location_placed_address_before_address_max = addr;
10062
10063 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10064
10065 gdb_assert (bl->address < end);
10066 addr = end - bl->address;
10067 if (addr > bp_location_shadow_len_after_address_max)
10068 bp_location_shadow_len_after_address_max = addr;
10069 }
f7545552
TT
10070}
10071
4cd9bd08 10072/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10073 into the inferior, only remove already-inserted locations that no
10074 longer should be inserted. Functions that delete a breakpoint or
10075 breakpoints should pass false, so that deleting a breakpoint
10076 doesn't have the side effect of inserting the locations of other
10077 breakpoints that are marked not-inserted, but should_be_inserted
10078 returns true on them.
10079
10080 This behaviour is useful is situations close to tear-down -- e.g.,
10081 after an exec, while the target still has execution, but breakpoint
10082 shadows of the previous executable image should *NOT* be restored
10083 to the new image; or before detaching, where the target still has
10084 execution and wants to delete breakpoints from GDB's lists, and all
10085 breakpoints had already been removed from the inferior. */
10086
0d381245 10087static void
b60e7edf 10088update_global_location_list (int should_insert)
0d381245 10089{
74960c60 10090 struct breakpoint *b;
876fa593 10091 struct bp_location **locp, *loc;
f7545552
TT
10092 struct cleanup *cleanups;
10093
2d134ed3
PA
10094 /* Used in the duplicates detection below. When iterating over all
10095 bp_locations, points to the first bp_location of a given address.
10096 Breakpoints and watchpoints of different types are never
10097 duplicates of each other. Keep one pointer for each type of
10098 breakpoint/watchpoint, so we only need to loop over all locations
10099 once. */
10100 struct bp_location *bp_loc_first; /* breakpoint */
10101 struct bp_location *wp_loc_first; /* hardware watchpoint */
10102 struct bp_location *awp_loc_first; /* access watchpoint */
10103 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10104
4a64f543
MS
10105 /* Saved former bp_location array which we compare against the newly
10106 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10107 struct bp_location **old_location, **old_locp;
10108 unsigned old_location_count;
10109
10110 old_location = bp_location;
10111 old_location_count = bp_location_count;
10112 bp_location = NULL;
10113 bp_location_count = 0;
10114 cleanups = make_cleanup (xfree, old_location);
0d381245 10115
74960c60 10116 ALL_BREAKPOINTS (b)
876fa593
JK
10117 for (loc = b->loc; loc; loc = loc->next)
10118 bp_location_count++;
10119
10120 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10121 locp = bp_location;
10122 ALL_BREAKPOINTS (b)
10123 for (loc = b->loc; loc; loc = loc->next)
10124 *locp++ = loc;
10125 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10126 bp_location_compare);
876fa593
JK
10127
10128 bp_location_target_extensions_update ();
74960c60 10129
4a64f543
MS
10130 /* Identify bp_location instances that are no longer present in the
10131 new list, and therefore should be freed. Note that it's not
10132 necessary that those locations should be removed from inferior --
10133 if there's another location at the same address (previously
10134 marked as duplicate), we don't need to remove/insert the
10135 location.
876fa593 10136
4a64f543
MS
10137 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10138 and former bp_location array state respectively. */
876fa593
JK
10139
10140 locp = bp_location;
10141 for (old_locp = old_location; old_locp < old_location + old_location_count;
10142 old_locp++)
74960c60 10143 {
876fa593 10144 struct bp_location *old_loc = *old_locp;
c7d46a38 10145 struct bp_location **loc2p;
876fa593 10146
4a64f543
MS
10147 /* Tells if 'old_loc' is found amoung the new locations. If
10148 not, we have to free it. */
c7d46a38 10149 int found_object = 0;
20874c92
VP
10150 /* Tells if the location should remain inserted in the target. */
10151 int keep_in_target = 0;
10152 int removed = 0;
876fa593 10153
4a64f543
MS
10154 /* Skip LOCP entries which will definitely never be needed.
10155 Stop either at or being the one matching OLD_LOC. */
876fa593 10156 while (locp < bp_location + bp_location_count
c7d46a38 10157 && (*locp)->address < old_loc->address)
876fa593 10158 locp++;
c7d46a38
PA
10159
10160 for (loc2p = locp;
10161 (loc2p < bp_location + bp_location_count
10162 && (*loc2p)->address == old_loc->address);
10163 loc2p++)
10164 {
10165 if (*loc2p == old_loc)
10166 {
10167 found_object = 1;
10168 break;
10169 }
10170 }
74960c60 10171
4a64f543
MS
10172 /* If this location is no longer present, and inserted, look if
10173 there's maybe a new location at the same address. If so,
10174 mark that one inserted, and don't remove this one. This is
10175 needed so that we don't have a time window where a breakpoint
10176 at certain location is not inserted. */
74960c60 10177
876fa593 10178 if (old_loc->inserted)
0d381245 10179 {
4a64f543
MS
10180 /* If the location is inserted now, we might have to remove
10181 it. */
74960c60 10182
876fa593 10183 if (found_object && should_be_inserted (old_loc))
74960c60 10184 {
4a64f543
MS
10185 /* The location is still present in the location list,
10186 and still should be inserted. Don't do anything. */
20874c92 10187 keep_in_target = 1;
74960c60
VP
10188 }
10189 else
10190 {
4a64f543
MS
10191 /* The location is either no longer present, or got
10192 disabled. See if there's another location at the
10193 same address, in which case we don't need to remove
10194 this one from the target. */
876fa593 10195
2bdf28a0 10196 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10197 if (breakpoint_address_is_meaningful (old_loc->owner))
10198 {
876fa593 10199 for (loc2p = locp;
c7d46a38
PA
10200 (loc2p < bp_location + bp_location_count
10201 && (*loc2p)->address == old_loc->address);
876fa593
JK
10202 loc2p++)
10203 {
10204 struct bp_location *loc2 = *loc2p;
10205
2d134ed3 10206 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10207 {
10208 /* For the sake of should_be_inserted.
4a64f543
MS
10209 Duplicates check below will fix up this
10210 later. */
c7d46a38 10211 loc2->duplicate = 0;
85d721b8
PA
10212
10213 /* Read watchpoint locations are switched to
10214 access watchpoints, if the former are not
10215 supported, but the latter are. */
10216 if (is_hardware_watchpoint (old_loc->owner))
10217 {
10218 gdb_assert (is_hardware_watchpoint (loc2->owner));
10219 loc2->watchpoint_type = old_loc->watchpoint_type;
10220 }
10221
c7d46a38
PA
10222 if (loc2 != old_loc && should_be_inserted (loc2))
10223 {
10224 loc2->inserted = 1;
10225 loc2->target_info = old_loc->target_info;
10226 keep_in_target = 1;
10227 break;
10228 }
876fa593
JK
10229 }
10230 }
10231 }
74960c60
VP
10232 }
10233
20874c92
VP
10234 if (!keep_in_target)
10235 {
876fa593 10236 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10237 {
4a64f543
MS
10238 /* This is just about all we can do. We could keep
10239 this location on the global list, and try to
10240 remove it next time, but there's no particular
10241 reason why we will succeed next time.
20874c92 10242
4a64f543
MS
10243 Note that at this point, old_loc->owner is still
10244 valid, as delete_breakpoint frees the breakpoint
10245 only after calling us. */
3e43a32a
MS
10246 printf_filtered (_("warning: Error removing "
10247 "breakpoint %d\n"),
876fa593 10248 old_loc->owner->number);
20874c92
VP
10249 }
10250 removed = 1;
10251 }
0d381245 10252 }
74960c60
VP
10253
10254 if (!found_object)
1c5cfe86 10255 {
db82e815
PA
10256 if (removed && non_stop
10257 && breakpoint_address_is_meaningful (old_loc->owner)
10258 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10259 {
db82e815
PA
10260 /* This location was removed from the target. In
10261 non-stop mode, a race condition is possible where
10262 we've removed a breakpoint, but stop events for that
10263 breakpoint are already queued and will arrive later.
10264 We apply an heuristic to be able to distinguish such
10265 SIGTRAPs from other random SIGTRAPs: we keep this
10266 breakpoint location for a bit, and will retire it
10267 after we see some number of events. The theory here
10268 is that reporting of events should, "on the average",
10269 be fair, so after a while we'll see events from all
10270 threads that have anything of interest, and no longer
10271 need to keep this breakpoint location around. We
10272 don't hold locations forever so to reduce chances of
10273 mistaking a non-breakpoint SIGTRAP for a breakpoint
10274 SIGTRAP.
10275
10276 The heuristic failing can be disastrous on
10277 decr_pc_after_break targets.
10278
10279 On decr_pc_after_break targets, like e.g., x86-linux,
10280 if we fail to recognize a late breakpoint SIGTRAP,
10281 because events_till_retirement has reached 0 too
10282 soon, we'll fail to do the PC adjustment, and report
10283 a random SIGTRAP to the user. When the user resumes
10284 the inferior, it will most likely immediately crash
2dec564e 10285 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10286 corrupted, because of being resumed e.g., in the
10287 middle of a multi-byte instruction, or skipped a
10288 one-byte instruction. This was actually seen happen
10289 on native x86-linux, and should be less rare on
10290 targets that do not support new thread events, like
10291 remote, due to the heuristic depending on
10292 thread_count.
10293
10294 Mistaking a random SIGTRAP for a breakpoint trap
10295 causes similar symptoms (PC adjustment applied when
10296 it shouldn't), but then again, playing with SIGTRAPs
10297 behind the debugger's back is asking for trouble.
10298
10299 Since hardware watchpoint traps are always
10300 distinguishable from other traps, so we don't need to
10301 apply keep hardware watchpoint moribund locations
10302 around. We simply always ignore hardware watchpoint
10303 traps we can no longer explain. */
10304
876fa593
JK
10305 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10306 old_loc->owner = NULL;
20874c92 10307
876fa593 10308 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10309 }
10310 else
f431efe5
PA
10311 {
10312 old_loc->owner = NULL;
10313 decref_bp_location (&old_loc);
10314 }
20874c92 10315 }
74960c60 10316 }
1c5cfe86 10317
2d134ed3
PA
10318 /* Rescan breakpoints at the same address and section, marking the
10319 first one as "first" and any others as "duplicates". This is so
10320 that the bpt instruction is only inserted once. If we have a
10321 permanent breakpoint at the same place as BPT, make that one the
10322 official one, and the rest as duplicates. Permanent breakpoints
10323 are sorted first for the same address.
10324
10325 Do the same for hardware watchpoints, but also considering the
10326 watchpoint's type (regular/access/read) and length. */
876fa593 10327
2d134ed3
PA
10328 bp_loc_first = NULL;
10329 wp_loc_first = NULL;
10330 awp_loc_first = NULL;
10331 rwp_loc_first = NULL;
876fa593 10332 ALL_BP_LOCATIONS (loc, locp)
74960c60 10333 {
4a64f543
MS
10334 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10335 non-NULL. */
876fa593 10336 struct breakpoint *b = loc->owner;
2d134ed3 10337 struct bp_location **loc_first_p;
876fa593
JK
10338
10339 if (b->enable_state == bp_disabled
10340 || b->enable_state == bp_call_disabled
10341 || b->enable_state == bp_startup_disabled
10342 || !loc->enabled
10343 || loc->shlib_disabled
15c3d785 10344 || !breakpoint_address_is_meaningful (b)
d77f58be 10345 || is_tracepoint (b))
876fa593
JK
10346 continue;
10347
10348 /* Permanent breakpoint should always be inserted. */
10349 if (b->enable_state == bp_permanent && ! loc->inserted)
10350 internal_error (__FILE__, __LINE__,
10351 _("allegedly permanent breakpoint is not "
10352 "actually inserted"));
10353
2d134ed3
PA
10354 if (b->type == bp_hardware_watchpoint)
10355 loc_first_p = &wp_loc_first;
10356 else if (b->type == bp_read_watchpoint)
10357 loc_first_p = &rwp_loc_first;
10358 else if (b->type == bp_access_watchpoint)
10359 loc_first_p = &awp_loc_first;
10360 else
10361 loc_first_p = &bp_loc_first;
10362
10363 if (*loc_first_p == NULL
10364 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10365 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 10366 {
2d134ed3 10367 *loc_first_p = loc;
876fa593
JK
10368 loc->duplicate = 0;
10369 continue;
10370 }
10371
10372 loc->duplicate = 1;
10373
2d134ed3
PA
10374 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10375 && b->enable_state != bp_permanent)
876fa593
JK
10376 internal_error (__FILE__, __LINE__,
10377 _("another breakpoint was inserted on top of "
10378 "a permanent breakpoint"));
0d381245 10379 }
74960c60 10380
50c71eaf 10381 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 10382 && (have_live_inferiors ()
2567c7d9 10383 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 10384 insert_breakpoint_locations ();
f7545552
TT
10385
10386 do_cleanups (cleanups);
74960c60
VP
10387}
10388
20874c92
VP
10389void
10390breakpoint_retire_moribund (void)
10391{
10392 struct bp_location *loc;
10393 int ix;
10394
10395 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10396 if (--(loc->events_till_retirement) == 0)
10397 {
f431efe5 10398 decref_bp_location (&loc);
20874c92
VP
10399 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10400 --ix;
10401 }
10402}
10403
74960c60 10404static void
b60e7edf 10405update_global_location_list_nothrow (int inserting)
74960c60
VP
10406{
10407 struct gdb_exception e;
cc59ec59 10408
74960c60 10409 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 10410 update_global_location_list (inserting);
0d381245
VP
10411}
10412
f431efe5
PA
10413/* Clear BKP from a BPS. */
10414
a474d7c2 10415static void
f431efe5 10416bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
10417{
10418 bpstat bs;
cc59ec59 10419
a474d7c2 10420 for (bs = bps; bs; bs = bs->next)
f431efe5 10421 if (bs->breakpoint_at == bpt)
a474d7c2
PA
10422 {
10423 bs->breakpoint_at = NULL;
10424 bs->old_val = NULL;
10425 /* bs->commands will be freed later. */
10426 }
10427}
10428
10429/* Callback for iterate_over_threads. */
10430static int
f431efe5 10431bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10432{
f431efe5 10433 struct breakpoint *bpt = data;
cc59ec59 10434
16c381f0 10435 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10436 return 0;
10437}
10438
53a5351d 10439/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10440 structures. */
c906108c
SS
10441
10442void
fba45db2 10443delete_breakpoint (struct breakpoint *bpt)
c906108c 10444{
52f0bd74 10445 struct breakpoint *b;
c906108c 10446
8a3fe4f8 10447 gdb_assert (bpt != NULL);
c906108c 10448
4a64f543
MS
10449 /* Has this bp already been deleted? This can happen because
10450 multiple lists can hold pointers to bp's. bpstat lists are
10451 especial culprits.
10452
10453 One example of this happening is a watchpoint's scope bp. When
10454 the scope bp triggers, we notice that the watchpoint is out of
10455 scope, and delete it. We also delete its scope bp. But the
10456 scope bp is marked "auto-deleting", and is already on a bpstat.
10457 That bpstat is then checked for auto-deleting bp's, which are
10458 deleted.
10459
10460 A real solution to this problem might involve reference counts in
10461 bp's, and/or giving them pointers back to their referencing
10462 bpstat's, and teaching delete_breakpoint to only free a bp's
10463 storage when no more references were extent. A cheaper bandaid
10464 was chosen. */
c906108c
SS
10465 if (bpt->type == bp_none)
10466 return;
10467
4a64f543
MS
10468 /* At least avoid this stale reference until the reference counting
10469 of breakpoints gets resolved. */
d0fb5eae 10470 if (bpt->related_breakpoint != bpt)
e5a0a904 10471 {
d0fb5eae
JK
10472 struct breakpoint *related;
10473
10474 if (bpt->type == bp_watchpoint_scope)
10475 watchpoint_del_at_next_stop (bpt->related_breakpoint);
10476 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
10477 watchpoint_del_at_next_stop (bpt);
10478
10479 /* Unlink bpt from the bpt->related_breakpoint ring. */
10480 for (related = bpt; related->related_breakpoint != bpt;
10481 related = related->related_breakpoint);
10482 related->related_breakpoint = bpt->related_breakpoint;
10483 bpt->related_breakpoint = bpt;
e5a0a904
JK
10484 }
10485
8d3788bd 10486 observer_notify_breakpoint_deleted (bpt);
c906108c 10487
c906108c
SS
10488 if (breakpoint_chain == bpt)
10489 breakpoint_chain = bpt->next;
10490
c906108c
SS
10491 ALL_BREAKPOINTS (b)
10492 if (b->next == bpt)
c5aa993b
JM
10493 {
10494 b->next = bpt->next;
10495 break;
10496 }
c906108c 10497
9add0f1b 10498 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10499 xfree (bpt->cond_string);
10500 xfree (bpt->cond_exp);
10501 xfree (bpt->addr_string);
f1310107 10502 xfree (bpt->addr_string_range_end);
60e1c644
PA
10503 xfree (bpt->exp);
10504 xfree (bpt->exp_string);
d63d0675 10505 xfree (bpt->exp_string_reparse);
60e1c644
PA
10506 value_free (bpt->val);
10507 xfree (bpt->source_file);
10508 xfree (bpt->exec_pathname);
a96d9b2e 10509 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 10510
f431efe5
PA
10511
10512 /* Be sure no bpstat's are pointing at the breakpoint after it's
10513 been freed. */
10514 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10515 in all threeds for now. Note that we cannot just remove bpstats
10516 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10517 commands are associated with the bpstat; if we remove it here,
10518 then the later call to bpstat_do_actions (&stop_bpstat); in
10519 event-top.c won't do anything, and temporary breakpoints with
10520 commands won't work. */
10521
10522 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10523
4a64f543
MS
10524 /* Now that breakpoint is removed from breakpoint list, update the
10525 global location list. This will remove locations that used to
10526 belong to this breakpoint. Do this before freeing the breakpoint
10527 itself, since remove_breakpoint looks at location's owner. It
10528 might be better design to have location completely
10529 self-contained, but it's not the case now. */
b60e7edf 10530 update_global_location_list (0);
74960c60
VP
10531
10532
4a64f543
MS
10533 /* On the chance that someone will soon try again to delete this
10534 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10535 bpt->type = bp_none;
10536
b8c9b27d 10537 xfree (bpt);
c906108c
SS
10538}
10539
4d6140d9
AC
10540static void
10541do_delete_breakpoint_cleanup (void *b)
10542{
10543 delete_breakpoint (b);
10544}
10545
10546struct cleanup *
10547make_cleanup_delete_breakpoint (struct breakpoint *b)
10548{
10549 return make_cleanup (do_delete_breakpoint_cleanup, b);
10550}
10551
95a42b64
TT
10552/* A callback for map_breakpoint_numbers that calls
10553 delete_breakpoint. */
10554
10555static void
10556do_delete_breakpoint (struct breakpoint *b, void *ignore)
10557{
10558 delete_breakpoint (b);
10559}
10560
c906108c 10561void
fba45db2 10562delete_command (char *arg, int from_tty)
c906108c 10563{
35df4500 10564 struct breakpoint *b, *b_tmp;
c906108c 10565
ea9365bb
TT
10566 dont_repeat ();
10567
c906108c
SS
10568 if (arg == 0)
10569 {
10570 int breaks_to_delete = 0;
10571
46c6471b
PA
10572 /* Delete all breakpoints if no argument. Do not delete
10573 internal breakpoints, these have to be deleted with an
10574 explicit breakpoint number argument. */
c5aa993b 10575 ALL_BREAKPOINTS (b)
46c6471b 10576 if (user_breakpoint_p (b))
973d738b
DJ
10577 {
10578 breaks_to_delete = 1;
10579 break;
10580 }
c906108c
SS
10581
10582 /* Ask user only if there are some breakpoints to delete. */
10583 if (!from_tty
e2e0b3e5 10584 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10585 {
35df4500 10586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 10587 if (user_breakpoint_p (b))
c5aa993b 10588 delete_breakpoint (b);
c906108c
SS
10589 }
10590 }
10591 else
95a42b64 10592 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
10593}
10594
0d381245
VP
10595static int
10596all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 10597{
0d381245
VP
10598 for (; loc; loc = loc->next)
10599 if (!loc->shlib_disabled)
10600 return 0;
10601 return 1;
fe3f5fa8
VP
10602}
10603
776592bf
DE
10604/* Subroutine of update_breakpoint_locations to simplify it.
10605 Return non-zero if multiple fns in list LOC have the same name.
10606 Null names are ignored. */
10607
10608static int
10609ambiguous_names_p (struct bp_location *loc)
10610{
10611 struct bp_location *l;
10612 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
10613 (int (*) (const void *,
10614 const void *)) streq,
776592bf
DE
10615 NULL, xcalloc, xfree);
10616
10617 for (l = loc; l != NULL; l = l->next)
10618 {
10619 const char **slot;
10620 const char *name = l->function_name;
10621
10622 /* Allow for some names to be NULL, ignore them. */
10623 if (name == NULL)
10624 continue;
10625
10626 slot = (const char **) htab_find_slot (htab, (const void *) name,
10627 INSERT);
4a64f543
MS
10628 /* NOTE: We can assume slot != NULL here because xcalloc never
10629 returns NULL. */
776592bf
DE
10630 if (*slot != NULL)
10631 {
10632 htab_delete (htab);
10633 return 1;
10634 }
10635 *slot = name;
10636 }
10637
10638 htab_delete (htab);
10639 return 0;
10640}
10641
0fb4aa4b
PA
10642/* When symbols change, it probably means the sources changed as well,
10643 and it might mean the static tracepoint markers are no longer at
10644 the same address or line numbers they used to be at last we
10645 checked. Losing your static tracepoints whenever you rebuild is
10646 undesirable. This function tries to resync/rematch gdb static
10647 tracepoints with the markers on the target, for static tracepoints
10648 that have not been set by marker id. Static tracepoint that have
10649 been set by marker id are reset by marker id in breakpoint_re_set.
10650 The heuristic is:
10651
10652 1) For a tracepoint set at a specific address, look for a marker at
10653 the old PC. If one is found there, assume to be the same marker.
10654 If the name / string id of the marker found is different from the
10655 previous known name, assume that means the user renamed the marker
10656 in the sources, and output a warning.
10657
10658 2) For a tracepoint set at a given line number, look for a marker
10659 at the new address of the old line number. If one is found there,
10660 assume to be the same marker. If the name / string id of the
10661 marker found is different from the previous known name, assume that
10662 means the user renamed the marker in the sources, and output a
10663 warning.
10664
10665 3) If a marker is no longer found at the same address or line, it
10666 may mean the marker no longer exists. But it may also just mean
10667 the code changed a bit. Maybe the user added a few lines of code
10668 that made the marker move up or down (in line number terms). Ask
10669 the target for info about the marker with the string id as we knew
10670 it. If found, update line number and address in the matching
10671 static tracepoint. This will get confused if there's more than one
10672 marker with the same ID (possible in UST, although unadvised
10673 precisely because it confuses tools). */
10674
10675static struct symtab_and_line
10676update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10677{
10678 struct static_tracepoint_marker marker;
10679 CORE_ADDR pc;
10680 int i;
10681
10682 pc = sal.pc;
10683 if (sal.line)
10684 find_line_pc (sal.symtab, sal.line, &pc);
10685
10686 if (target_static_tracepoint_marker_at (pc, &marker))
10687 {
10688 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10689 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10690 b->number,
10691 b->static_trace_marker_id, marker.str_id);
10692
10693 xfree (b->static_trace_marker_id);
10694 b->static_trace_marker_id = xstrdup (marker.str_id);
10695 release_static_tracepoint_marker (&marker);
10696
10697 return sal;
10698 }
10699
10700 /* Old marker wasn't found on target at lineno. Try looking it up
10701 by string ID. */
10702 if (!sal.explicit_pc
10703 && sal.line != 0
10704 && sal.symtab != NULL
10705 && b->static_trace_marker_id != NULL)
10706 {
10707 VEC(static_tracepoint_marker_p) *markers;
10708
10709 markers
10710 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10711
10712 if (!VEC_empty(static_tracepoint_marker_p, markers))
10713 {
10714 struct symtab_and_line sal;
10715 struct symbol *sym;
10716 struct static_tracepoint_marker *marker;
10717
10718 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10719
10720 xfree (b->static_trace_marker_id);
10721 b->static_trace_marker_id = xstrdup (marker->str_id);
10722
10723 warning (_("marker for static tracepoint %d (%s) not "
10724 "found at previous line number"),
10725 b->number, b->static_trace_marker_id);
10726
10727 init_sal (&sal);
10728
10729 sal.pc = marker->address;
10730
10731 sal = find_pc_line (marker->address, 0);
10732 sym = find_pc_sect_function (marker->address, NULL);
10733 ui_out_text (uiout, "Now in ");
10734 if (sym)
10735 {
10736 ui_out_field_string (uiout, "func",
10737 SYMBOL_PRINT_NAME (sym));
10738 ui_out_text (uiout, " at ");
10739 }
10740 ui_out_field_string (uiout, "file", sal.symtab->filename);
10741 ui_out_text (uiout, ":");
10742
10743 if (ui_out_is_mi_like_p (uiout))
10744 {
10745 char *fullname = symtab_to_fullname (sal.symtab);
10746
10747 if (fullname)
10748 ui_out_field_string (uiout, "fullname", fullname);
10749 }
10750
10751 ui_out_field_int (uiout, "line", sal.line);
10752 ui_out_text (uiout, "\n");
10753
10754 b->line_number = sal.line;
10755
10756 xfree (b->source_file);
10757 if (sym)
10758 b->source_file = xstrdup (sal.symtab->filename);
10759 else
10760 b->source_file = NULL;
10761
10762 xfree (b->addr_string);
10763 b->addr_string = xstrprintf ("%s:%d",
10764 sal.symtab->filename, b->line_number);
10765
10766 /* Might be nice to check if function changed, and warn if
10767 so. */
10768
10769 release_static_tracepoint_marker (marker);
10770 }
10771 }
10772 return sal;
10773}
10774
8d3788bd
VP
10775/* Returns 1 iff locations A and B are sufficiently same that
10776 we don't need to report breakpoint as changed. */
10777
10778static int
10779locations_are_equal (struct bp_location *a, struct bp_location *b)
10780{
10781 while (a && b)
10782 {
10783 if (a->address != b->address)
10784 return 0;
10785
10786 if (a->shlib_disabled != b->shlib_disabled)
10787 return 0;
10788
10789 if (a->enabled != b->enabled)
10790 return 0;
10791
10792 a = a->next;
10793 b = b->next;
10794 }
10795
10796 if ((a == NULL) != (b == NULL))
10797 return 0;
10798
10799 return 1;
10800}
10801
f1310107
TJB
10802/* Create new breakpoint locations for B (a hardware or software breakpoint)
10803 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
10804 a ranged breakpoint. */
10805
0e30163f 10806void
0d381245 10807update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
10808 struct symtabs_and_lines sals,
10809 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
10810{
10811 int i;
0d381245
VP
10812 struct bp_location *existing_locations = b->loc;
10813
f1310107
TJB
10814 /* Ranged breakpoints have only one start location and one end location. */
10815 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
10816
4a64f543
MS
10817 /* If there's no new locations, and all existing locations are
10818 pending, don't do anything. This optimizes the common case where
10819 all locations are in the same shared library, that was unloaded.
10820 We'd like to retain the location, so that when the library is
10821 loaded again, we don't loose the enabled/disabled status of the
10822 individual locations. */
0d381245 10823 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10824 return;
10825
fe3f5fa8
VP
10826 b->loc = NULL;
10827
0d381245 10828 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10829 {
0d381245 10830 struct bp_location *new_loc =
39d61571 10831 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10832
0d381245
VP
10833 /* Reparse conditions, they might contain references to the
10834 old symtab. */
10835 if (b->cond_string != NULL)
10836 {
f1310107 10837 char *s;
0d381245 10838 struct gdb_exception e;
fe3f5fa8 10839
0d381245
VP
10840 s = b->cond_string;
10841 TRY_CATCH (e, RETURN_MASK_ERROR)
10842 {
10843 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10844 0);
10845 }
10846 if (e.reason < 0)
10847 {
3e43a32a
MS
10848 warning (_("failed to reevaluate condition "
10849 "for breakpoint %d: %s"),
0d381245
VP
10850 b->number, e.message);
10851 new_loc->enabled = 0;
10852 }
10853 }
fe3f5fa8 10854
0d381245
VP
10855 if (b->source_file != NULL)
10856 xfree (b->source_file);
10857 if (sals.sals[i].symtab == NULL)
10858 b->source_file = NULL;
10859 else
1b36a34b 10860 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10861
0d381245
VP
10862 if (b->line_number == 0)
10863 b->line_number = sals.sals[i].line;
f1310107
TJB
10864
10865 if (sals_end.nelts)
10866 {
10867 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
10868
10869 new_loc->length = end - sals.sals[0].pc + 1;
10870 }
0d381245 10871 }
fe3f5fa8 10872
514f746b
AR
10873 /* Update locations of permanent breakpoints. */
10874 if (b->enable_state == bp_permanent)
10875 make_breakpoint_permanent (b);
10876
4a64f543
MS
10877 /* If possible, carry over 'disable' status from existing
10878 breakpoints. */
0d381245
VP
10879 {
10880 struct bp_location *e = existing_locations;
776592bf
DE
10881 /* If there are multiple breakpoints with the same function name,
10882 e.g. for inline functions, comparing function names won't work.
10883 Instead compare pc addresses; this is just a heuristic as things
10884 may have moved, but in practice it gives the correct answer
10885 often enough until a better solution is found. */
10886 int have_ambiguous_names = ambiguous_names_p (b->loc);
10887
0d381245
VP
10888 for (; e; e = e->next)
10889 {
10890 if (!e->enabled && e->function_name)
10891 {
10892 struct bp_location *l = b->loc;
776592bf
DE
10893 if (have_ambiguous_names)
10894 {
10895 for (; l; l = l->next)
f1310107 10896 if (breakpoint_locations_match (e, l))
776592bf
DE
10897 {
10898 l->enabled = 0;
10899 break;
10900 }
10901 }
10902 else
10903 {
10904 for (; l; l = l->next)
10905 if (l->function_name
10906 && strcmp (e->function_name, l->function_name) == 0)
10907 {
10908 l->enabled = 0;
10909 break;
10910 }
10911 }
0d381245
VP
10912 }
10913 }
10914 }
fe3f5fa8 10915
8d3788bd
VP
10916 if (!locations_are_equal (existing_locations, b->loc))
10917 observer_notify_breakpoint_modified (b);
10918
b60e7edf 10919 update_global_location_list (1);
fe3f5fa8
VP
10920}
10921
ef23e705
TJB
10922/* Find the SaL locations corresponding to the given ADDR_STRING.
10923 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
10924
10925static struct symtabs_and_lines
10926addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
10927{
10928 char *s;
58438ac1 10929 int marker_spec;
02d20e4a 10930 struct symtabs_and_lines sals = {0};
ef23e705
TJB
10931 struct gdb_exception e;
10932
10933 s = addr_string;
10934 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10935
10936 TRY_CATCH (e, RETURN_MASK_ERROR)
10937 {
10938 if (marker_spec)
10939 {
10940 sals = decode_static_tracepoint_spec (&s);
10941 if (sals.nelts > b->static_trace_marker_id_idx)
10942 {
10943 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10944 sals.nelts = 1;
10945 }
10946 else
10947 error (_("marker %s not found"), b->static_trace_marker_id);
10948 }
10949 else
58438ac1 10950 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
10951 }
10952 if (e.reason < 0)
10953 {
10954 int not_found_and_ok = 0;
10955 /* For pending breakpoints, it's expected that parsing will
10956 fail until the right shared library is loaded. User has
10957 already told to create pending breakpoints and don't need
10958 extra messages. If breakpoint is in bp_shlib_disabled
10959 state, then user already saw the message about that
10960 breakpoint being disabled, and don't want to see more
10961 errors. */
58438ac1 10962 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
10963 && (b->condition_not_parsed
10964 || (b->loc && b->loc->shlib_disabled)
10965 || b->enable_state == bp_disabled))
10966 not_found_and_ok = 1;
10967
10968 if (!not_found_and_ok)
10969 {
10970 /* We surely don't want to warn about the same breakpoint
10971 10 times. One solution, implemented here, is disable
10972 the breakpoint on error. Another solution would be to
10973 have separate 'warning emitted' flag. Since this
10974 happens only when a binary has changed, I don't know
10975 which approach is better. */
10976 b->enable_state = bp_disabled;
10977 throw_exception (e);
10978 }
10979 }
10980
58438ac1 10981 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
10982 {
10983 gdb_assert (sals.nelts == 1);
10984
10985 resolve_sal_pc (&sals.sals[0]);
10986 if (b->condition_not_parsed && s && s[0])
10987 {
10988 char *cond_string = 0;
10989 int thread = -1;
10990 int task = 0;
10991
10992 find_condition_and_thread (s, sals.sals[0].pc,
10993 &cond_string, &thread, &task);
10994 if (cond_string)
10995 b->cond_string = cond_string;
10996 b->thread = thread;
10997 b->task = task;
10998 b->condition_not_parsed = 0;
10999 }
11000
11001 if (b->type == bp_static_tracepoint && !marker_spec)
11002 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11003
58438ac1
TT
11004 *found = 1;
11005 }
11006 else
11007 *found = 0;
ef23e705
TJB
11008
11009 return sals;
11010}
11011
11012/* Reevaluate a hardware or software breakpoint and recreate its locations.
11013 This is necessary after symbols are read (e.g., an executable or DSO
11014 was loaded, or the inferior just started). */
11015
11016static void
11017re_set_breakpoint (struct breakpoint *b)
11018{
11019 int found;
f1310107 11020 struct symtabs_and_lines sals, sals_end;
ef23e705 11021 struct symtabs_and_lines expanded = {0};
f1310107 11022 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11023 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11024
11025 input_radix = b->input_radix;
11026 save_current_space_and_thread ();
11027 switch_to_program_space_and_thread (b->pspace);
11028 set_language (b->language);
11029
11030 sals = addr_string_to_sals (b, b->addr_string, &found);
11031 if (found)
11032 {
11033 make_cleanup (xfree, sals.sals);
11034 expanded = expand_line_sal_maybe (sals.sals[0]);
11035 }
11036
f1310107
TJB
11037 if (b->addr_string_range_end)
11038 {
11039 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11040 if (found)
11041 {
11042 make_cleanup (xfree, sals_end.sals);
11043 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11044 }
11045 }
11046
11047 update_breakpoint_locations (b, expanded, expanded_end);
ef23e705
TJB
11048 do_cleanups (cleanups);
11049}
11050
c906108c
SS
11051/* Reset a breakpoint given it's struct breakpoint * BINT.
11052 The value we return ends up being the return value from catch_errors.
11053 Unused in this case. */
11054
11055static int
4efb68b1 11056breakpoint_re_set_one (void *bint)
c906108c 11057{
4a64f543 11058 /* Get past catch_errs. */
53a5351d 11059 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
11060
11061 switch (b->type)
11062 {
11063 case bp_none:
8a3fe4f8 11064 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 11065 b->number);
c906108c
SS
11066 return 0;
11067 case bp_breakpoint:
11068 case bp_hardware_breakpoint:
1042e4c0 11069 case bp_tracepoint:
7a697b8d 11070 case bp_fast_tracepoint:
0fb4aa4b 11071 case bp_static_tracepoint:
0e30163f 11072 case bp_gnu_ifunc_resolver:
8bea4e01
UW
11073 /* Do not attempt to re-set breakpoints disabled during startup. */
11074 if (b->enable_state == bp_startup_disabled)
11075 return 0;
11076
c906108c
SS
11077 if (b->addr_string == NULL)
11078 {
4a64f543 11079 /* Anything without a string can't be re-set. */
c906108c
SS
11080 delete_breakpoint (b);
11081 return 0;
11082 }
c906108c 11083
ef23e705 11084 re_set_breakpoint (b);
c906108c
SS
11085 break;
11086
11087 case bp_watchpoint:
11088 case bp_hardware_watchpoint:
11089 case bp_read_watchpoint:
11090 case bp_access_watchpoint:
4a64f543
MS
11091 /* Watchpoint can be either on expression using entirely global
11092 variables, or it can be on local variables.
11093
11094 Watchpoints of the first kind are never auto-deleted, and
11095 even persist across program restarts. Since they can use
11096 variables from shared libraries, we need to reparse
11097 expression as libraries are loaded and unloaded.
11098
11099 Watchpoints on local variables can also change meaning as
11100 result of solib event. For example, if a watchpoint uses
11101 both a local and a global variables in expression, it's a
11102 local watchpoint, but unloading of a shared library will make
11103 the expression invalid. This is not a very common use case,
11104 but we still re-evaluate expression, to avoid surprises to
11105 the user.
0b3de036
VP
11106
11107 Note that for local watchpoints, we re-evaluate it only if
11108 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
11109 the watchpoint is out of scope and will be deleted soon. In
11110 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
11111
11112 If a local watchpoint's frame id is still valid, then
4a64f543 11113 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
11114
11115 Don't do anything about disabled watchpoints, since they will
11116 be reevaluated again when enabled. */
a5606eee 11117 update_watchpoint (b, 1 /* reparse */);
c906108c 11118 break;
c5aa993b
JM
11119 /* We needn't really do anything to reset these, since the mask
11120 that requests them is unaffected by e.g., new libraries being
4a64f543 11121 loaded. */
ce78b96d 11122 case bp_catchpoint:
c906108c 11123 break;
c5aa993b 11124
c906108c 11125 default:
a3f17187 11126 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 11127 /* fall through */
0fd8e87f
UW
11128 /* Delete overlay event and longjmp master breakpoints; they will be
11129 reset later by breakpoint_re_set. */
1900040c 11130 case bp_overlay_event:
0fd8e87f 11131 case bp_longjmp_master:
aa7d318d 11132 case bp_std_terminate_master:
186c406b 11133 case bp_exception_master:
c906108c
SS
11134 delete_breakpoint (b);
11135 break;
11136
c5aa993b
JM
11137 /* This breakpoint is special, it's set up when the inferior
11138 starts and we really don't want to touch it. */
c906108c
SS
11139 case bp_shlib_event:
11140
c4093a6a
JM
11141 /* Like bp_shlib_event, this breakpoint type is special.
11142 Once it is set up, we do not want to touch it. */
11143 case bp_thread_event:
11144
4a64f543
MS
11145 /* Keep temporary breakpoints, which can be encountered when we
11146 step over a dlopen call and SOLIB_ADD is resetting the
11147 breakpoints. Otherwise these should have been blown away via
11148 the cleanup chain or by breakpoint_init_inferior when we
11149 rerun the executable. */
c906108c
SS
11150 case bp_until:
11151 case bp_finish:
11152 case bp_watchpoint_scope:
11153 case bp_call_dummy:
aa7d318d 11154 case bp_std_terminate:
c906108c 11155 case bp_step_resume:
611c83ae
PA
11156 case bp_longjmp:
11157 case bp_longjmp_resume:
186c406b
TT
11158 case bp_exception:
11159 case bp_exception_resume:
4efc6507 11160 case bp_jit_event:
0e30163f 11161 case bp_gnu_ifunc_resolver_return:
c906108c
SS
11162 break;
11163 }
11164
11165 return 0;
11166}
11167
69de3c6a 11168/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11169void
69de3c6a 11170breakpoint_re_set (void)
c906108c 11171{
35df4500 11172 struct breakpoint *b, *b_tmp;
c906108c
SS
11173 enum language save_language;
11174 int save_input_radix;
6c95b8df 11175 struct cleanup *old_chain;
c5aa993b 11176
c906108c
SS
11177 save_language = current_language->la_language;
11178 save_input_radix = input_radix;
6c95b8df
PA
11179 old_chain = save_current_program_space ();
11180
35df4500 11181 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11182 {
4a64f543 11183 /* Format possible error msg. */
fe3f5fa8 11184 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11185 b->number);
11186 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11187 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11188 do_cleanups (cleanups);
c5aa993b 11189 }
c906108c
SS
11190 set_language (save_language);
11191 input_radix = save_input_radix;
e62c965a 11192
0756c555 11193 jit_breakpoint_re_set ();
4efc6507 11194
6c95b8df
PA
11195 do_cleanups (old_chain);
11196
af02033e
PP
11197 create_overlay_event_breakpoint ();
11198 create_longjmp_master_breakpoint ();
11199 create_std_terminate_master_breakpoint ();
186c406b 11200 create_exception_master_breakpoint ();
c906108c
SS
11201}
11202\f
c906108c
SS
11203/* Reset the thread number of this breakpoint:
11204
11205 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11206 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11207void
fba45db2 11208breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11209{
11210 if (b->thread != -1)
11211 {
39f77062
KB
11212 if (in_thread_list (inferior_ptid))
11213 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11214
11215 /* We're being called after following a fork. The new fork is
11216 selected as current, and unless this was a vfork will have a
11217 different program space from the original thread. Reset that
11218 as well. */
11219 b->loc->pspace = current_program_space;
c906108c
SS
11220 }
11221}
11222
03ac34d5
MS
11223/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11224 If from_tty is nonzero, it prints a message to that effect,
11225 which ends with a period (no newline). */
11226
c906108c 11227void
fba45db2 11228set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11229{
52f0bd74 11230 struct breakpoint *b;
c906108c
SS
11231
11232 if (count < 0)
11233 count = 0;
11234
11235 ALL_BREAKPOINTS (b)
11236 if (b->number == bptnum)
c5aa993b 11237 {
d77f58be
SS
11238 if (is_tracepoint (b))
11239 {
11240 if (from_tty && count != 0)
11241 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11242 bptnum);
11243 return;
11244 }
11245
c5aa993b 11246 b->ignore_count = count;
221ea385
KS
11247 if (from_tty)
11248 {
11249 if (count == 0)
3e43a32a
MS
11250 printf_filtered (_("Will stop next time "
11251 "breakpoint %d is reached."),
221ea385
KS
11252 bptnum);
11253 else if (count == 1)
a3f17187 11254 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11255 bptnum);
11256 else
3e43a32a
MS
11257 printf_filtered (_("Will ignore next %d "
11258 "crossings of breakpoint %d."),
221ea385
KS
11259 count, bptnum);
11260 }
c5aa993b 11261 breakpoints_changed ();
8d3788bd 11262 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11263 return;
11264 }
c906108c 11265
8a3fe4f8 11266 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11267}
11268
c906108c
SS
11269/* Command to set ignore-count of breakpoint N to COUNT. */
11270
11271static void
fba45db2 11272ignore_command (char *args, int from_tty)
c906108c
SS
11273{
11274 char *p = args;
52f0bd74 11275 int num;
c906108c
SS
11276
11277 if (p == 0)
e2e0b3e5 11278 error_no_arg (_("a breakpoint number"));
c5aa993b 11279
c906108c 11280 num = get_number (&p);
5c44784c 11281 if (num == 0)
8a3fe4f8 11282 error (_("bad breakpoint number: '%s'"), args);
c906108c 11283 if (*p == 0)
8a3fe4f8 11284 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11285
11286 set_ignore_count (num,
11287 longest_to_int (value_as_long (parse_and_eval (p))),
11288 from_tty);
221ea385
KS
11289 if (from_tty)
11290 printf_filtered ("\n");
c906108c
SS
11291}
11292\f
11293/* Call FUNCTION on each of the breakpoints
11294 whose numbers are given in ARGS. */
11295
11296static void
95a42b64
TT
11297map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11298 void *),
11299 void *data)
c906108c 11300{
52f0bd74
AC
11301 int num;
11302 struct breakpoint *b, *tmp;
11cf8741 11303 int match;
197f0a60 11304 struct get_number_or_range_state state;
c906108c 11305
197f0a60 11306 if (args == 0)
e2e0b3e5 11307 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11308
197f0a60
TT
11309 init_number_or_range (&state, args);
11310
11311 while (!state.finished)
c906108c 11312 {
197f0a60
TT
11313 char *p = state.string;
11314
11cf8741 11315 match = 0;
c5aa993b 11316
197f0a60 11317 num = get_number_or_range (&state);
5c44784c 11318 if (num == 0)
c5aa993b 11319 {
8a3fe4f8 11320 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11321 }
11322 else
11323 {
11324 ALL_BREAKPOINTS_SAFE (b, tmp)
11325 if (b->number == num)
11326 {
d0fb5eae
JK
11327 struct breakpoint *related_breakpoint;
11328
11cf8741 11329 match = 1;
d0fb5eae
JK
11330 related_breakpoint = b;
11331 do
11332 {
11333 struct breakpoint *next_related_b;
11334
11335 /* FUNCTION can be also delete_breakpoint. */
11336 next_related_b = related_breakpoint->related_breakpoint;
11337 function (related_breakpoint, data);
11338
11339 /* For delete_breakpoint of the last entry of the ring we
11340 were traversing we would never get back to B. */
11341 if (next_related_b == related_breakpoint)
11342 break;
11343 related_breakpoint = next_related_b;
11344 }
11345 while (related_breakpoint != b);
11cf8741 11346 break;
5c44784c 11347 }
11cf8741 11348 if (match == 0)
a3f17187 11349 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11350 }
c906108c
SS
11351 }
11352}
11353
0d381245
VP
11354static struct bp_location *
11355find_location_by_number (char *number)
11356{
11357 char *dot = strchr (number, '.');
11358 char *p1;
11359 int bp_num;
11360 int loc_num;
11361 struct breakpoint *b;
11362 struct bp_location *loc;
11363
11364 *dot = '\0';
11365
11366 p1 = number;
197f0a60 11367 bp_num = get_number (&p1);
0d381245
VP
11368 if (bp_num == 0)
11369 error (_("Bad breakpoint number '%s'"), number);
11370
11371 ALL_BREAKPOINTS (b)
11372 if (b->number == bp_num)
11373 {
11374 break;
11375 }
11376
11377 if (!b || b->number != bp_num)
11378 error (_("Bad breakpoint number '%s'"), number);
11379
11380 p1 = dot+1;
197f0a60 11381 loc_num = get_number (&p1);
0d381245
VP
11382 if (loc_num == 0)
11383 error (_("Bad breakpoint location number '%s'"), number);
11384
11385 --loc_num;
11386 loc = b->loc;
11387 for (;loc_num && loc; --loc_num, loc = loc->next)
11388 ;
11389 if (!loc)
11390 error (_("Bad breakpoint location number '%s'"), dot+1);
11391
11392 return loc;
11393}
11394
11395
1900040c
MS
11396/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11397 If from_tty is nonzero, it prints a message to that effect,
11398 which ends with a period (no newline). */
11399
c906108c 11400void
fba45db2 11401disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11402{
11403 /* Never disable a watchpoint scope breakpoint; we want to
11404 hit them when we leave scope so we can delete both the
11405 watchpoint and its scope breakpoint at that time. */
11406 if (bpt->type == bp_watchpoint_scope)
11407 return;
11408
c2c6d25f 11409 /* You can't disable permanent breakpoints. */
b5de0fa7 11410 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11411 return;
11412
b5de0fa7 11413 bpt->enable_state = bp_disabled;
c906108c 11414
b60e7edf 11415 update_global_location_list (0);
c906108c 11416
8d3788bd 11417 observer_notify_breakpoint_modified (bpt);
c906108c
SS
11418}
11419
95a42b64
TT
11420/* A callback for map_breakpoint_numbers that calls
11421 disable_breakpoint. */
11422
11423static void
11424do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11425{
11426 disable_breakpoint (b);
11427}
11428
c906108c 11429static void
fba45db2 11430disable_command (char *args, int from_tty)
c906108c 11431{
c906108c 11432 if (args == 0)
46c6471b
PA
11433 {
11434 struct breakpoint *bpt;
11435
11436 ALL_BREAKPOINTS (bpt)
11437 if (user_breakpoint_p (bpt))
11438 disable_breakpoint (bpt);
11439 }
0d381245
VP
11440 else if (strchr (args, '.'))
11441 {
11442 struct bp_location *loc = find_location_by_number (args);
11443 if (loc)
11444 loc->enabled = 0;
b60e7edf 11445 update_global_location_list (0);
0d381245 11446 }
c906108c 11447 else
95a42b64 11448 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11449}
11450
11451static void
fba45db2 11452do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11453{
afe38095 11454 int target_resources_ok;
c906108c
SS
11455
11456 if (bpt->type == bp_hardware_breakpoint)
11457 {
11458 int i;
c5aa993b 11459 i = hw_breakpoint_used_count ();
53a5351d 11460 target_resources_ok =
d92524f1 11461 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11462 i + 1, 0);
c906108c 11463 if (target_resources_ok == 0)
8a3fe4f8 11464 error (_("No hardware breakpoint support in the target."));
c906108c 11465 else if (target_resources_ok < 0)
8a3fe4f8 11466 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11467 }
11468
cc60f2e3 11469 if (is_watchpoint (bpt))
c906108c 11470 {
1e718ff1 11471 enum enable_state orig_enable_state;
dde02812
ES
11472 struct gdb_exception e;
11473
11474 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11475 {
1e718ff1
TJB
11476 orig_enable_state = bpt->enable_state;
11477 bpt->enable_state = bp_enabled;
dde02812 11478 update_watchpoint (bpt, 1 /* reparse */);
c906108c 11479 }
dde02812 11480 if (e.reason < 0)
c5aa993b 11481 {
1e718ff1 11482 bpt->enable_state = orig_enable_state;
dde02812
ES
11483 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11484 bpt->number);
11485 return;
c5aa993b 11486 }
c906108c 11487 }
0101ce28 11488
b4c291bb
KH
11489 if (bpt->enable_state != bp_permanent)
11490 bpt->enable_state = bp_enabled;
11491 bpt->disposition = disposition;
b60e7edf 11492 update_global_location_list (1);
b4c291bb
KH
11493 breakpoints_changed ();
11494
8d3788bd 11495 observer_notify_breakpoint_modified (bpt);
c906108c
SS
11496}
11497
fe3f5fa8 11498
c906108c 11499void
fba45db2 11500enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11501{
11502 do_enable_breakpoint (bpt, bpt->disposition);
11503}
11504
95a42b64
TT
11505/* A callback for map_breakpoint_numbers that calls
11506 enable_breakpoint. */
11507
11508static void
11509do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11510{
11511 enable_breakpoint (b);
11512}
11513
c906108c
SS
11514/* The enable command enables the specified breakpoints (or all defined
11515 breakpoints) so they once again become (or continue to be) effective
1272ad14 11516 in stopping the inferior. */
c906108c 11517
c906108c 11518static void
fba45db2 11519enable_command (char *args, int from_tty)
c906108c 11520{
c906108c 11521 if (args == 0)
46c6471b
PA
11522 {
11523 struct breakpoint *bpt;
11524
11525 ALL_BREAKPOINTS (bpt)
11526 if (user_breakpoint_p (bpt))
11527 enable_breakpoint (bpt);
11528 }
0d381245
VP
11529 else if (strchr (args, '.'))
11530 {
11531 struct bp_location *loc = find_location_by_number (args);
11532 if (loc)
11533 loc->enabled = 1;
b60e7edf 11534 update_global_location_list (1);
0d381245 11535 }
c906108c 11536 else
95a42b64 11537 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
11538}
11539
11540static void
95a42b64 11541enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11542{
b5de0fa7 11543 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
11544}
11545
c906108c 11546static void
fba45db2 11547enable_once_command (char *args, int from_tty)
c906108c 11548{
95a42b64 11549 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
11550}
11551
11552static void
95a42b64 11553enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11554{
b5de0fa7 11555 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
11556}
11557
c906108c 11558static void
fba45db2 11559enable_delete_command (char *args, int from_tty)
c906108c 11560{
95a42b64 11561 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
11562}
11563\f
fa8d40ab
JJ
11564static void
11565set_breakpoint_cmd (char *args, int from_tty)
11566{
11567}
11568
11569static void
11570show_breakpoint_cmd (char *args, int from_tty)
11571{
11572}
11573
1f3b5d1b
PP
11574/* Invalidate last known value of any hardware watchpoint if
11575 the memory which that value represents has been written to by
11576 GDB itself. */
11577
11578static void
11579invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11580 const bfd_byte *data)
11581{
11582 struct breakpoint *bp;
11583
11584 ALL_BREAKPOINTS (bp)
11585 if (bp->enable_state == bp_enabled
11586 && bp->type == bp_hardware_watchpoint
11587 && bp->val_valid && bp->val)
11588 {
11589 struct bp_location *loc;
11590
11591 for (loc = bp->loc; loc != NULL; loc = loc->next)
11592 if (loc->loc_type == bp_loc_hardware_watchpoint
11593 && loc->address + loc->length > addr
11594 && addr + len > loc->address)
11595 {
11596 value_free (bp->val);
11597 bp->val = NULL;
11598 bp->val_valid = 0;
11599 }
11600 }
11601}
11602
c906108c
SS
11603/* Use default_breakpoint_'s, or nothing if they aren't valid. */
11604
11605struct symtabs_and_lines
fba45db2 11606decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
11607{
11608 struct symtabs_and_lines sals;
cc59ec59 11609
c906108c 11610 if (string == 0)
8a3fe4f8 11611 error (_("Empty line specification."));
c906108c
SS
11612 if (default_breakpoint_valid)
11613 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
11614 default_breakpoint_symtab,
11615 default_breakpoint_line,
58438ac1 11616 NULL);
c906108c
SS
11617 else
11618 sals = decode_line_1 (&string, funfirstline,
58438ac1 11619 (struct symtab *) NULL, 0, NULL);
c906108c 11620 if (*string)
8a3fe4f8 11621 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
11622 return sals;
11623}
8181d85f
DJ
11624
11625/* Create and insert a raw software breakpoint at PC. Return an
11626 identifier, which should be used to remove the breakpoint later.
11627 In general, places which call this should be using something on the
11628 breakpoint chain instead; this function should be eliminated
11629 someday. */
11630
11631void *
6c95b8df
PA
11632deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11633 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
11634{
11635 struct bp_target_info *bp_tgt;
11636
6c95b8df 11637 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 11638
6c95b8df 11639 bp_tgt->placed_address_space = aspace;
8181d85f 11640 bp_tgt->placed_address = pc;
6c95b8df 11641
a6d9a66e 11642 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
11643 {
11644 /* Could not insert the breakpoint. */
11645 xfree (bp_tgt);
11646 return NULL;
11647 }
11648
11649 return bp_tgt;
11650}
11651
4a64f543
MS
11652/* Remove a breakpoint BP inserted by
11653 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
11654
11655int
a6d9a66e 11656deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
11657{
11658 struct bp_target_info *bp_tgt = bp;
11659 int ret;
11660
a6d9a66e 11661 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
11662 xfree (bp_tgt);
11663
11664 return ret;
11665}
11666
4a64f543
MS
11667/* One (or perhaps two) breakpoints used for software single
11668 stepping. */
8181d85f
DJ
11669
11670static void *single_step_breakpoints[2];
a6d9a66e 11671static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
11672
11673/* Create and insert a breakpoint for software single step. */
11674
11675void
6c95b8df 11676insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
11677 struct address_space *aspace,
11678 CORE_ADDR next_pc)
8181d85f
DJ
11679{
11680 void **bpt_p;
11681
11682 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
11683 {
11684 bpt_p = &single_step_breakpoints[0];
11685 single_step_gdbarch[0] = gdbarch;
11686 }
8181d85f
DJ
11687 else
11688 {
11689 gdb_assert (single_step_breakpoints[1] == NULL);
11690 bpt_p = &single_step_breakpoints[1];
a6d9a66e 11691 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
11692 }
11693
4a64f543
MS
11694 /* NOTE drow/2006-04-11: A future improvement to this function would
11695 be to only create the breakpoints once, and actually put them on
11696 the breakpoint chain. That would let us use set_raw_breakpoint.
11697 We could adjust the addresses each time they were needed. Doing
11698 this requires corresponding changes elsewhere where single step
11699 breakpoints are handled, however. So, for now, we use this. */
8181d85f 11700
6c95b8df 11701 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 11702 if (*bpt_p == NULL)
5af949e3
UW
11703 error (_("Could not insert single-step breakpoint at %s"),
11704 paddress (gdbarch, next_pc));
8181d85f
DJ
11705}
11706
f02253f1
HZ
11707/* Check if the breakpoints used for software single stepping
11708 were inserted or not. */
11709
11710int
11711single_step_breakpoints_inserted (void)
11712{
11713 return (single_step_breakpoints[0] != NULL
11714 || single_step_breakpoints[1] != NULL);
11715}
11716
8181d85f
DJ
11717/* Remove and delete any breakpoints used for software single step. */
11718
11719void
11720remove_single_step_breakpoints (void)
11721{
11722 gdb_assert (single_step_breakpoints[0] != NULL);
11723
11724 /* See insert_single_step_breakpoint for more about this deprecated
11725 call. */
a6d9a66e
UW
11726 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11727 single_step_breakpoints[0]);
11728 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11729 single_step_breakpoints[0] = NULL;
11730
11731 if (single_step_breakpoints[1] != NULL)
11732 {
a6d9a66e
UW
11733 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11734 single_step_breakpoints[1]);
11735 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11736 single_step_breakpoints[1] = NULL;
11737 }
11738}
11739
d03285ec
UW
11740/* Delete software single step breakpoints without removing them from
11741 the inferior. This is intended to be used if the inferior's address
11742 space where they were inserted is already gone, e.g. after exit or
11743 exec. */
11744
11745void
11746cancel_single_step_breakpoints (void)
11747{
11748 int i;
11749
11750 for (i = 0; i < 2; i++)
11751 if (single_step_breakpoints[i])
11752 {
11753 xfree (single_step_breakpoints[i]);
11754 single_step_breakpoints[i] = NULL;
11755 single_step_gdbarch[i] = NULL;
11756 }
11757}
11758
11759/* Detach software single-step breakpoints from INFERIOR_PTID without
11760 removing them. */
11761
11762static void
11763detach_single_step_breakpoints (void)
11764{
11765 int i;
11766
11767 for (i = 0; i < 2; i++)
11768 if (single_step_breakpoints[i])
11769 target_remove_breakpoint (single_step_gdbarch[i],
11770 single_step_breakpoints[i]);
11771}
11772
4a64f543
MS
11773/* Check whether a software single-step breakpoint is inserted at
11774 PC. */
1aafd4da
UW
11775
11776static int
cc59ec59
MS
11777single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11778 CORE_ADDR pc)
1aafd4da
UW
11779{
11780 int i;
11781
11782 for (i = 0; i < 2; i++)
11783 {
11784 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11785 if (bp_tgt
11786 && breakpoint_address_match (bp_tgt->placed_address_space,
11787 bp_tgt->placed_address,
11788 aspace, pc))
1aafd4da
UW
11789 return 1;
11790 }
11791
11792 return 0;
11793}
11794
a96d9b2e
SDJ
11795/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11796 non-zero otherwise. */
11797static int
11798is_syscall_catchpoint_enabled (struct breakpoint *bp)
11799{
11800 if (syscall_catchpoint_p (bp)
11801 && bp->enable_state != bp_disabled
11802 && bp->enable_state != bp_call_disabled)
11803 return 1;
11804 else
11805 return 0;
11806}
11807
11808int
11809catch_syscall_enabled (void)
11810{
11811 struct inferior *inf = current_inferior ();
11812
11813 return inf->total_syscalls_count != 0;
11814}
11815
11816int
11817catching_syscall_number (int syscall_number)
11818{
11819 struct breakpoint *bp;
11820
11821 ALL_BREAKPOINTS (bp)
11822 if (is_syscall_catchpoint_enabled (bp))
11823 {
11824 if (bp->syscalls_to_be_caught)
11825 {
11826 int i, iter;
11827 for (i = 0;
11828 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11829 i++)
11830 if (syscall_number == iter)
11831 return 1;
11832 }
11833 else
11834 return 1;
11835 }
11836
11837 return 0;
11838}
11839
11840/* Complete syscall names. Used by "catch syscall". */
11841static char **
11842catch_syscall_completer (struct cmd_list_element *cmd,
11843 char *text, char *word)
11844{
11845 const char **list = get_syscall_names ();
c38eea1a
MS
11846 char **retlist
11847 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 11848
c38eea1a
MS
11849 xfree (list);
11850 return retlist;
a96d9b2e
SDJ
11851}
11852
1042e4c0
SS
11853/* Tracepoint-specific operations. */
11854
11855/* Set tracepoint count to NUM. */
11856static void
11857set_tracepoint_count (int num)
11858{
11859 tracepoint_count = num;
4fa62494 11860 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11861}
11862
11863void
11864trace_command (char *arg, int from_tty)
11865{
8cdf0e15
VP
11866 if (create_breakpoint (get_current_arch (),
11867 arg,
11868 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11869 0 /* tempflag */,
11870 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11871 0 /* Ignore count */,
11872 pending_break_support,
11873 NULL,
11874 from_tty,
84f4c1fe
PM
11875 1 /* enabled */,
11876 0 /* internal */))
fd9b8c24 11877 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11878}
11879
7a697b8d
SS
11880void
11881ftrace_command (char *arg, int from_tty)
11882{
8cdf0e15
VP
11883 if (create_breakpoint (get_current_arch (),
11884 arg,
11885 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11886 0 /* tempflag */,
11887 bp_fast_tracepoint /* type_wanted */,
11888 0 /* Ignore count */,
11889 pending_break_support,
11890 NULL,
11891 from_tty,
84f4c1fe
PM
11892 1 /* enabled */,
11893 0 /* internal */))
0fb4aa4b
PA
11894 set_tracepoint_count (breakpoint_count);
11895}
11896
11897/* strace command implementation. Creates a static tracepoint. */
11898
11899void
11900strace_command (char *arg, int from_tty)
11901{
11902 if (create_breakpoint (get_current_arch (),
11903 arg,
11904 NULL, 0, 1 /* parse arg */,
11905 0 /* tempflag */,
11906 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11907 0 /* Ignore count */,
11908 pending_break_support,
11909 NULL,
11910 from_tty,
84f4c1fe
PM
11911 1 /* enabled */,
11912 0 /* internal */))
fd9b8c24 11913 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11914}
11915
409873ef
SS
11916/* Set up a fake reader function that gets command lines from a linked
11917 list that was acquired during tracepoint uploading. */
11918
11919static struct uploaded_tp *this_utp;
3149d8c1 11920static int next_cmd;
409873ef
SS
11921
11922static char *
11923read_uploaded_action (void)
11924{
11925 char *rslt;
11926
3149d8c1 11927 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11928
3149d8c1 11929 next_cmd++;
409873ef
SS
11930
11931 return rslt;
11932}
11933
00bf0b85
SS
11934/* Given information about a tracepoint as recorded on a target (which
11935 can be either a live system or a trace file), attempt to create an
11936 equivalent GDB tracepoint. This is not a reliable process, since
11937 the target does not necessarily have all the information used when
11938 the tracepoint was originally defined. */
11939
11940struct breakpoint *
11941create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11942{
409873ef 11943 char *addr_str, small_buf[100];
d5551862 11944 struct breakpoint *tp;
fd9b8c24 11945
409873ef
SS
11946 if (utp->at_string)
11947 addr_str = utp->at_string;
11948 else
11949 {
11950 /* In the absence of a source location, fall back to raw
11951 address. Since there is no way to confirm that the address
11952 means the same thing as when the trace was started, warn the
11953 user. */
3e43a32a
MS
11954 warning (_("Uploaded tracepoint %d has no "
11955 "source location, using raw address"),
409873ef
SS
11956 utp->number);
11957 sprintf (small_buf, "*%s", hex_string (utp->addr));
11958 addr_str = small_buf;
11959 }
11960
11961 /* There's not much we can do with a sequence of bytecodes. */
11962 if (utp->cond && !utp->cond_string)
3e43a32a
MS
11963 warning (_("Uploaded tracepoint %d condition "
11964 "has no source form, ignoring it"),
409873ef 11965 utp->number);
d5551862 11966
8cdf0e15 11967 if (!create_breakpoint (get_current_arch (),
409873ef
SS
11968 addr_str,
11969 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 11970 0 /* tempflag */,
0fb4aa4b 11971 utp->type /* type_wanted */,
8cdf0e15
VP
11972 0 /* Ignore count */,
11973 pending_break_support,
11974 NULL,
11975 0 /* from_tty */,
84f4c1fe
PM
11976 utp->enabled /* enabled */,
11977 0 /* internal */))
fd9b8c24
PA
11978 return NULL;
11979
00bf0b85
SS
11980 set_tracepoint_count (breakpoint_count);
11981
409873ef 11982 /* Get the tracepoint we just created. */
fd9b8c24
PA
11983 tp = get_tracepoint (tracepoint_count);
11984 gdb_assert (tp != NULL);
d5551862 11985
00bf0b85
SS
11986 if (utp->pass > 0)
11987 {
409873ef 11988 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 11989
409873ef 11990 trace_pass_command (small_buf, 0);
00bf0b85
SS
11991 }
11992
409873ef
SS
11993 /* If we have uploaded versions of the original commands, set up a
11994 special-purpose "reader" function and call the usual command line
11995 reader, then pass the result to the breakpoint command-setting
11996 function. */
3149d8c1 11997 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 11998 {
409873ef 11999 struct command_line *cmd_list;
00bf0b85 12000
409873ef 12001 this_utp = utp;
3149d8c1 12002 next_cmd = 0;
d5551862 12003
409873ef
SS
12004 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12005
12006 breakpoint_set_commands (tp, cmd_list);
00bf0b85 12007 }
3149d8c1
SS
12008 else if (!VEC_empty (char_ptr, utp->actions)
12009 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12010 warning (_("Uploaded tracepoint %d actions "
12011 "have no source form, ignoring them"),
409873ef 12012 utp->number);
00bf0b85
SS
12013
12014 return tp;
12015 }
12016
1042e4c0
SS
12017/* Print information on tracepoint number TPNUM_EXP, or all if
12018 omitted. */
12019
12020static void
e5a67952 12021tracepoints_info (char *args, int from_tty)
1042e4c0 12022{
e5a67952 12023 int num_printed;
1042e4c0 12024
e5a67952 12025 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12026
12027 if (num_printed == 0)
1042e4c0 12028 {
e5a67952 12029 if (args == NULL || *args == '\0')
d77f58be
SS
12030 ui_out_message (uiout, 0, "No tracepoints.\n");
12031 else
e5a67952 12032 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12033 }
ad443146
SS
12034
12035 default_collect_info ();
1042e4c0
SS
12036}
12037
4a64f543 12038/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12039 Not supported by all targets. */
12040static void
12041enable_trace_command (char *args, int from_tty)
12042{
12043 enable_command (args, from_tty);
12044}
12045
4a64f543 12046/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12047 Not supported by all targets. */
12048static void
12049disable_trace_command (char *args, int from_tty)
12050{
12051 disable_command (args, from_tty);
12052}
12053
4a64f543 12054/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12055static void
12056delete_trace_command (char *arg, int from_tty)
12057{
35df4500 12058 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12059
12060 dont_repeat ();
12061
12062 if (arg == 0)
12063 {
12064 int breaks_to_delete = 0;
12065
12066 /* Delete all breakpoints if no argument.
12067 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12068 have to be deleted with an explicit breakpoint number
12069 argument. */
1042e4c0 12070 ALL_TRACEPOINTS (b)
46c6471b 12071 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12072 {
12073 breaks_to_delete = 1;
12074 break;
12075 }
1042e4c0
SS
12076
12077 /* Ask user only if there are some breakpoints to delete. */
12078 if (!from_tty
12079 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12080 {
35df4500 12081 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12082 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12083 delete_breakpoint (b);
1042e4c0
SS
12084 }
12085 }
12086 else
95a42b64 12087 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
12088}
12089
197f0a60
TT
12090/* Helper function for trace_pass_command. */
12091
12092static void
12093trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12094{
12095 bp->pass_count = count;
12096 observer_notify_tracepoint_modified (bp->number);
12097 if (from_tty)
12098 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12099 bp->number, count);
12100}
12101
1042e4c0
SS
12102/* Set passcount for tracepoint.
12103
12104 First command argument is passcount, second is tracepoint number.
12105 If tracepoint number omitted, apply to most recently defined.
12106 Also accepts special argument "all". */
12107
12108static void
12109trace_pass_command (char *args, int from_tty)
12110{
197f0a60 12111 struct breakpoint *t1;
1042e4c0 12112 unsigned int count;
1042e4c0
SS
12113
12114 if (args == 0 || *args == 0)
3e43a32a
MS
12115 error (_("passcount command requires an "
12116 "argument (count + optional TP num)"));
1042e4c0 12117
4a64f543 12118 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12119
12120 while (*args && isspace ((int) *args))
12121 args++;
12122
12123 if (*args && strncasecmp (args, "all", 3) == 0)
12124 {
12125 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12126 if (*args)
12127 error (_("Junk at end of arguments."));
1042e4c0 12128
197f0a60
TT
12129 ALL_TRACEPOINTS (t1)
12130 {
12131 trace_pass_set_count (t1, count, from_tty);
12132 }
12133 }
12134 else if (*args == '\0')
1042e4c0 12135 {
197f0a60 12136 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12137 if (t1)
197f0a60
TT
12138 trace_pass_set_count (t1, count, from_tty);
12139 }
12140 else
12141 {
12142 struct get_number_or_range_state state;
12143
12144 init_number_or_range (&state, args);
12145 while (!state.finished)
1042e4c0 12146 {
197f0a60
TT
12147 t1 = get_tracepoint_by_number (&args, &state, 1);
12148 if (t1)
12149 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12150 }
12151 }
1042e4c0
SS
12152}
12153
12154struct breakpoint *
12155get_tracepoint (int num)
12156{
12157 struct breakpoint *t;
12158
12159 ALL_TRACEPOINTS (t)
12160 if (t->number == num)
12161 return t;
12162
12163 return NULL;
12164}
12165
d5551862
SS
12166/* Find the tracepoint with the given target-side number (which may be
12167 different from the tracepoint number after disconnecting and
12168 reconnecting). */
12169
12170struct breakpoint *
12171get_tracepoint_by_number_on_target (int num)
12172{
12173 struct breakpoint *t;
12174
12175 ALL_TRACEPOINTS (t)
12176 if (t->number_on_target == num)
12177 return t;
12178
12179 return NULL;
12180}
12181
1042e4c0 12182/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12183 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12184 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
12185 recent tracepoint (tracepoint_count) is returned. */
12186struct breakpoint *
197f0a60
TT
12187get_tracepoint_by_number (char **arg,
12188 struct get_number_or_range_state *state,
12189 int optional_p)
1042e4c0
SS
12190{
12191 extern int tracepoint_count;
12192 struct breakpoint *t;
12193 int tpnum;
12194 char *instring = arg == NULL ? NULL : *arg;
12195
197f0a60
TT
12196 if (state)
12197 {
12198 gdb_assert (!state->finished);
12199 tpnum = get_number_or_range (state);
12200 }
12201 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12202 {
12203 if (optional_p)
12204 tpnum = tracepoint_count;
12205 else
12206 error_no_arg (_("tracepoint number"));
12207 }
12208 else
197f0a60 12209 tpnum = get_number (arg);
1042e4c0
SS
12210
12211 if (tpnum <= 0)
12212 {
12213 if (instring && *instring)
12214 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12215 instring);
12216 else
3e43a32a
MS
12217 printf_filtered (_("Tracepoint argument missing "
12218 "and no previous tracepoint\n"));
1042e4c0
SS
12219 return NULL;
12220 }
12221
12222 ALL_TRACEPOINTS (t)
12223 if (t->number == tpnum)
12224 {
12225 return t;
12226 }
12227
1042e4c0
SS
12228 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12229 return NULL;
12230}
12231
6149aea9
PA
12232/* Save information on user settable breakpoints (watchpoints, etc) to
12233 a new script file named FILENAME. If FILTER is non-NULL, call it
12234 on each breakpoint and only include the ones for which it returns
12235 non-zero. */
12236
1042e4c0 12237static void
6149aea9
PA
12238save_breakpoints (char *filename, int from_tty,
12239 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12240{
12241 struct breakpoint *tp;
6149aea9 12242 int any = 0;
a7bdde9e 12243 char *pathname;
1042e4c0 12244 struct cleanup *cleanup;
a7bdde9e 12245 struct ui_file *fp;
6149aea9 12246 int extra_trace_bits = 0;
1042e4c0 12247
6149aea9
PA
12248 if (filename == 0 || *filename == 0)
12249 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12250
12251 /* See if we have anything to save. */
6149aea9 12252 ALL_BREAKPOINTS (tp)
1042e4c0 12253 {
6149aea9 12254 /* Skip internal and momentary breakpoints. */
09d682a4 12255 if (!user_breakpoint_p (tp))
6149aea9
PA
12256 continue;
12257
12258 /* If we have a filter, only save the breakpoints it accepts. */
12259 if (filter && !filter (tp))
12260 continue;
12261
12262 any = 1;
12263
12264 if (is_tracepoint (tp))
12265 {
12266 extra_trace_bits = 1;
12267
12268 /* We can stop searching. */
12269 break;
12270 }
1042e4c0 12271 }
6149aea9
PA
12272
12273 if (!any)
1042e4c0 12274 {
6149aea9 12275 warning (_("Nothing to save."));
1042e4c0
SS
12276 return;
12277 }
12278
6149aea9 12279 pathname = tilde_expand (filename);
1042e4c0 12280 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 12281 fp = gdb_fopen (pathname, "w");
059fb39f 12282 if (!fp)
6149aea9
PA
12283 error (_("Unable to open file '%s' for saving (%s)"),
12284 filename, safe_strerror (errno));
a7bdde9e 12285 make_cleanup_ui_file_delete (fp);
8bf6485c 12286
6149aea9
PA
12287 if (extra_trace_bits)
12288 save_trace_state_variables (fp);
8bf6485c 12289
6149aea9 12290 ALL_BREAKPOINTS (tp)
1042e4c0 12291 {
6149aea9 12292 /* Skip internal and momentary breakpoints. */
09d682a4 12293 if (!user_breakpoint_p (tp))
6149aea9 12294 continue;
8bf6485c 12295
6149aea9
PA
12296 /* If we have a filter, only save the breakpoints it accepts. */
12297 if (filter && !filter (tp))
12298 continue;
12299
26063d49 12300 if (tp->ops != NULL && tp->ops->print_recreate != NULL)
6149aea9 12301 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
12302 else
12303 {
6149aea9
PA
12304 if (tp->type == bp_fast_tracepoint)
12305 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
12306 if (tp->type == bp_static_tracepoint)
12307 fprintf_unfiltered (fp, "strace");
6149aea9
PA
12308 else if (tp->type == bp_tracepoint)
12309 fprintf_unfiltered (fp, "trace");
12310 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12311 fprintf_unfiltered (fp, "tbreak");
12312 else if (tp->type == bp_breakpoint)
12313 fprintf_unfiltered (fp, "break");
12314 else if (tp->type == bp_hardware_breakpoint
12315 && tp->disposition == disp_del)
12316 fprintf_unfiltered (fp, "thbreak");
12317 else if (tp->type == bp_hardware_breakpoint)
12318 fprintf_unfiltered (fp, "hbreak");
12319 else if (tp->type == bp_watchpoint)
12320 fprintf_unfiltered (fp, "watch");
12321 else if (tp->type == bp_hardware_watchpoint)
12322 fprintf_unfiltered (fp, "watch");
12323 else if (tp->type == bp_read_watchpoint)
12324 fprintf_unfiltered (fp, "rwatch");
12325 else if (tp->type == bp_access_watchpoint)
12326 fprintf_unfiltered (fp, "awatch");
12327 else
12328 internal_error (__FILE__, __LINE__,
12329 _("unhandled breakpoint type %d"), (int) tp->type);
12330
12331 if (tp->exp_string)
12332 fprintf_unfiltered (fp, " %s", tp->exp_string);
12333 else if (tp->addr_string)
12334 fprintf_unfiltered (fp, " %s", tp->addr_string);
12335 else
12336 {
12337 char tmp[40];
12338
12339 sprintf_vma (tmp, tp->loc->address);
12340 fprintf_unfiltered (fp, " *0x%s", tmp);
12341 }
1042e4c0
SS
12342 }
12343
6149aea9
PA
12344 if (tp->thread != -1)
12345 fprintf_unfiltered (fp, " thread %d", tp->thread);
12346
12347 if (tp->task != 0)
12348 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12349
12350 fprintf_unfiltered (fp, "\n");
12351
6149aea9
PA
12352 /* Note, we can't rely on tp->number for anything, as we can't
12353 assume the recreated breakpoint numbers will match. Use $bpnum
12354 instead. */
12355
12356 if (tp->cond_string)
12357 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12358
12359 if (tp->ignore_count)
12360 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12361
1042e4c0 12362 if (tp->pass_count)
a7bdde9e 12363 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12364
a7bdde9e 12365 if (tp->commands)
1042e4c0 12366 {
a7bdde9e
VP
12367 volatile struct gdb_exception ex;
12368
6149aea9 12369 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12370
12371 ui_out_redirect (uiout, fp);
14dba4b4 12372 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12373 {
9add0f1b 12374 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12375 }
12376 ui_out_redirect (uiout, NULL);
1042e4c0 12377
a7bdde9e
VP
12378 if (ex.reason < 0)
12379 throw_exception (ex);
1042e4c0 12380
a7bdde9e 12381 fprintf_unfiltered (fp, " end\n");
1042e4c0 12382 }
6149aea9
PA
12383
12384 if (tp->enable_state == bp_disabled)
12385 fprintf_unfiltered (fp, "disable\n");
12386
12387 /* If this is a multi-location breakpoint, check if the locations
12388 should be individually disabled. Watchpoint locations are
12389 special, and not user visible. */
12390 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12391 {
12392 struct bp_location *loc;
12393 int n = 1;
12394
12395 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12396 if (!loc->enabled)
12397 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12398 }
1042e4c0 12399 }
8bf6485c 12400
6149aea9 12401 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12402 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12403
1042e4c0
SS
12404 do_cleanups (cleanup);
12405 if (from_tty)
6149aea9
PA
12406 printf_filtered (_("Saved to file '%s'.\n"), filename);
12407}
12408
12409/* The `save breakpoints' command. */
12410
12411static void
12412save_breakpoints_command (char *args, int from_tty)
12413{
12414 save_breakpoints (args, from_tty, NULL);
12415}
12416
12417/* The `save tracepoints' command. */
12418
12419static void
12420save_tracepoints_command (char *args, int from_tty)
12421{
12422 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12423}
12424
12425/* Create a vector of all tracepoints. */
12426
12427VEC(breakpoint_p) *
eeae04df 12428all_tracepoints (void)
1042e4c0
SS
12429{
12430 VEC(breakpoint_p) *tp_vec = 0;
12431 struct breakpoint *tp;
12432
12433 ALL_TRACEPOINTS (tp)
12434 {
12435 VEC_safe_push (breakpoint_p, tp_vec, tp);
12436 }
12437
12438 return tp_vec;
12439}
12440
c906108c 12441\f
4a64f543
MS
12442/* This help string is used for the break, hbreak, tbreak and thbreak
12443 commands. It is defined as a macro to prevent duplication.
12444 COMMAND should be a string constant containing the name of the
12445 command. */
31e2b00f
AS
12446#define BREAK_ARGS_HELP(command) \
12447command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12448LOCATION may be a line number, function name, or \"*\" and an address.\n\
12449If a line number is specified, break at start of code for that line.\n\
12450If a function is specified, break at start of code for that function.\n\
12451If an address is specified, break at that exact address.\n\
dc10affe
PA
12452With no LOCATION, uses current execution address of the selected\n\
12453stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12454\n\
12455THREADNUM is the number from \"info threads\".\n\
12456CONDITION is a boolean expression.\n\
12457\n\
d41c0fc8
PA
12458Multiple breakpoints at one place are permitted, and useful if their\n\
12459conditions are different.\n\
31e2b00f
AS
12460\n\
12461Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12462
44feb3ce
TT
12463/* List of subcommands for "catch". */
12464static struct cmd_list_element *catch_cmdlist;
12465
12466/* List of subcommands for "tcatch". */
12467static struct cmd_list_element *tcatch_cmdlist;
12468
12469/* Like add_cmd, but add the command to both the "catch" and "tcatch"
12470 lists, and pass some additional user data to the command function. */
12471static void
12472add_catch_command (char *name, char *docstring,
12473 void (*sfunc) (char *args, int from_tty,
12474 struct cmd_list_element *command),
a96d9b2e
SDJ
12475 char **(*completer) (struct cmd_list_element *cmd,
12476 char *text, char *word),
44feb3ce
TT
12477 void *user_data_catch,
12478 void *user_data_tcatch)
12479{
12480 struct cmd_list_element *command;
12481
12482 command = add_cmd (name, class_breakpoint, NULL, docstring,
12483 &catch_cmdlist);
12484 set_cmd_sfunc (command, sfunc);
12485 set_cmd_context (command, user_data_catch);
a96d9b2e 12486 set_cmd_completer (command, completer);
44feb3ce
TT
12487
12488 command = add_cmd (name, class_breakpoint, NULL, docstring,
12489 &tcatch_cmdlist);
12490 set_cmd_sfunc (command, sfunc);
12491 set_cmd_context (command, user_data_tcatch);
a96d9b2e 12492 set_cmd_completer (command, completer);
44feb3ce
TT
12493}
12494
6c95b8df 12495static void
a79b8f6e 12496clear_syscall_counts (struct inferior *inf)
6c95b8df 12497{
6c95b8df
PA
12498 inf->total_syscalls_count = 0;
12499 inf->any_syscall_count = 0;
12500 VEC_free (int, inf->syscalls_counts);
12501}
12502
6149aea9
PA
12503static void
12504save_command (char *arg, int from_tty)
12505{
3e43a32a
MS
12506 printf_unfiltered (_("\"save\" must be followed by "
12507 "the name of a save subcommand.\n"));
6149aea9
PA
12508 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12509}
12510
84f4c1fe
PM
12511struct breakpoint *
12512iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12513 void *data)
12514{
35df4500 12515 struct breakpoint *b, *b_tmp;
84f4c1fe 12516
35df4500 12517 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12518 {
12519 if ((*callback) (b, data))
12520 return b;
12521 }
12522
12523 return NULL;
12524}
12525
c906108c 12526void
fba45db2 12527_initialize_breakpoint (void)
c906108c
SS
12528{
12529 struct cmd_list_element *c;
12530
84acb35a 12531 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 12532 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 12533 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 12534
17450429
PP
12535 breakpoint_objfile_key = register_objfile_data ();
12536
c906108c
SS
12537 breakpoint_chain = 0;
12538 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12539 before a breakpoint is set. */
12540 breakpoint_count = 0;
12541
1042e4c0
SS
12542 tracepoint_count = 0;
12543
1bedd215
AC
12544 add_com ("ignore", class_breakpoint, ignore_command, _("\
12545Set ignore-count of breakpoint number N to COUNT.\n\
12546Usage is `ignore N COUNT'."));
c906108c 12547 if (xdb_commands)
c5aa993b 12548 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 12549
1bedd215
AC
12550 add_com ("commands", class_breakpoint, commands_command, _("\
12551Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
12552Give breakpoint number as argument after \"commands\".\n\
12553With no argument, the targeted breakpoint is the last one set.\n\
12554The commands themselves follow starting on the next line.\n\
12555Type a line containing \"end\" to indicate the end of them.\n\
12556Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 12557then no output is printed when it is hit, except what the commands print."));
c906108c 12558
1bedd215
AC
12559 add_com ("condition", class_breakpoint, condition_command, _("\
12560Specify breakpoint number N to break only if COND is true.\n\
c906108c 12561Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 12562expression to be evaluated whenever breakpoint N is reached."));
c906108c 12563
1bedd215 12564 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 12565Set a temporary breakpoint.\n\
c906108c
SS
12566Like \"break\" except the breakpoint is only temporary,\n\
12567so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
12568by using \"enable delete\" on the breakpoint number.\n\
12569\n"
12570BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 12571 set_cmd_completer (c, location_completer);
c94fdfd0 12572
1bedd215 12573 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 12574Set a hardware assisted breakpoint.\n\
c906108c 12575Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
12576some target hardware may not have this support.\n\
12577\n"
12578BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 12579 set_cmd_completer (c, location_completer);
c906108c 12580
1bedd215 12581 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 12582Set a temporary hardware assisted breakpoint.\n\
c906108c 12583Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
12584so it will be deleted when hit.\n\
12585\n"
12586BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 12587 set_cmd_completer (c, location_completer);
c906108c 12588
1bedd215
AC
12589 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12590Enable some breakpoints.\n\
c906108c
SS
12591Give breakpoint numbers (separated by spaces) as arguments.\n\
12592With no subcommand, breakpoints are enabled until you command otherwise.\n\
12593This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12594With a subcommand you can enable temporarily."),
c906108c
SS
12595 &enablelist, "enable ", 1, &cmdlist);
12596 if (xdb_commands)
1bedd215
AC
12597 add_com ("ab", class_breakpoint, enable_command, _("\
12598Enable some breakpoints.\n\
c906108c
SS
12599Give breakpoint numbers (separated by spaces) as arguments.\n\
12600With no subcommand, breakpoints are enabled until you command otherwise.\n\
12601This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12602With a subcommand you can enable temporarily."));
c906108c
SS
12603
12604 add_com_alias ("en", "enable", class_breakpoint, 1);
12605
84951ab5 12606 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 12607Enable some breakpoints.\n\
c906108c
SS
12608Give breakpoint numbers (separated by spaces) as arguments.\n\
12609This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12610May be abbreviated to simply \"enable\".\n"),
c5aa993b 12611 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 12612
1a966eab
AC
12613 add_cmd ("once", no_class, enable_once_command, _("\
12614Enable breakpoints for one hit. Give breakpoint numbers.\n\
12615If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12616 &enablebreaklist);
12617
1a966eab
AC
12618 add_cmd ("delete", no_class, enable_delete_command, _("\
12619Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12620If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12621 &enablebreaklist);
12622
1a966eab
AC
12623 add_cmd ("delete", no_class, enable_delete_command, _("\
12624Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12625If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12626 &enablelist);
12627
1a966eab
AC
12628 add_cmd ("once", no_class, enable_once_command, _("\
12629Enable breakpoints for one hit. Give breakpoint numbers.\n\
12630If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12631 &enablelist);
12632
1bedd215
AC
12633 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12634Disable some breakpoints.\n\
c906108c
SS
12635Arguments are breakpoint numbers with spaces in between.\n\
12636To disable all breakpoints, give no argument.\n\
1bedd215 12637A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
12638 &disablelist, "disable ", 1, &cmdlist);
12639 add_com_alias ("dis", "disable", class_breakpoint, 1);
12640 add_com_alias ("disa", "disable", class_breakpoint, 1);
12641 if (xdb_commands)
1bedd215
AC
12642 add_com ("sb", class_breakpoint, disable_command, _("\
12643Disable some breakpoints.\n\
c906108c
SS
12644Arguments are breakpoint numbers with spaces in between.\n\
12645To disable all breakpoints, give no argument.\n\
1bedd215 12646A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 12647
1a966eab
AC
12648 add_cmd ("breakpoints", class_alias, disable_command, _("\
12649Disable some breakpoints.\n\
c906108c
SS
12650Arguments are breakpoint numbers with spaces in between.\n\
12651To disable all breakpoints, give no argument.\n\
12652A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 12653This command may be abbreviated \"disable\"."),
c906108c
SS
12654 &disablelist);
12655
1bedd215
AC
12656 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12657Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12658Arguments are breakpoint numbers with spaces in between.\n\
12659To delete all breakpoints, give no argument.\n\
12660\n\
12661Also a prefix command for deletion of other GDB objects.\n\
1bedd215 12662The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
12663 &deletelist, "delete ", 1, &cmdlist);
12664 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 12665 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 12666 if (xdb_commands)
1bedd215
AC
12667 add_com ("db", class_breakpoint, delete_command, _("\
12668Delete some breakpoints.\n\
c906108c 12669Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 12670To delete all breakpoints, give no argument.\n"));
c906108c 12671
1a966eab
AC
12672 add_cmd ("breakpoints", class_alias, delete_command, _("\
12673Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12674Arguments are breakpoint numbers with spaces in between.\n\
12675To delete all breakpoints, give no argument.\n\
1a966eab 12676This command may be abbreviated \"delete\"."),
c906108c
SS
12677 &deletelist);
12678
1bedd215
AC
12679 add_com ("clear", class_breakpoint, clear_command, _("\
12680Clear breakpoint at specified line or function.\n\
c906108c
SS
12681Argument may be line number, function name, or \"*\" and an address.\n\
12682If line number is specified, all breakpoints in that line are cleared.\n\
12683If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
12684If an address is specified, breakpoints at that address are cleared.\n\
12685\n\
12686With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
12687is executing in.\n\
12688\n\
1bedd215 12689See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 12690 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 12691
1bedd215 12692 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
12693Set breakpoint at specified line or function.\n"
12694BREAK_ARGS_HELP ("break")));
5ba2abeb 12695 set_cmd_completer (c, location_completer);
c94fdfd0 12696
c906108c
SS
12697 add_com_alias ("b", "break", class_run, 1);
12698 add_com_alias ("br", "break", class_run, 1);
12699 add_com_alias ("bre", "break", class_run, 1);
12700 add_com_alias ("brea", "break", class_run, 1);
12701
7681d515
PM
12702 if (xdb_commands)
12703 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
12704
12705 if (dbx_commands)
12706 {
1bedd215
AC
12707 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12708Break in function/address or break at a line in the current file."),
c5aa993b
JM
12709 &stoplist, "stop ", 1, &cmdlist);
12710 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 12711 _("Break in function or address."), &stoplist);
c5aa993b 12712 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 12713 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
12714 add_com ("status", class_info, breakpoints_info, _("\
12715Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12716The \"Type\" column indicates one of:\n\
12717\tbreakpoint - normal breakpoint\n\
12718\twatchpoint - watchpoint\n\
12719The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12720the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12721breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12722address and file/line number respectively.\n\
12723\n\
12724Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12725are set to the address of the last breakpoint listed unless the command\n\
12726is prefixed with \"server \".\n\n\
c906108c 12727Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12728breakpoint set."));
c906108c
SS
12729 }
12730
1bedd215 12731 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 12732Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
12733The \"Type\" column indicates one of:\n\
12734\tbreakpoint - normal breakpoint\n\
12735\twatchpoint - watchpoint\n\
12736The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12737the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12738breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12739address and file/line number respectively.\n\
12740\n\
12741Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12742are set to the address of the last breakpoint listed unless the command\n\
12743is prefixed with \"server \".\n\n\
c906108c 12744Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12745breakpoint set."));
c906108c 12746
6b04bdb7
MS
12747 add_info_alias ("b", "breakpoints", 1);
12748
c906108c 12749 if (xdb_commands)
1bedd215
AC
12750 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12751Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12752The \"Type\" column indicates one of:\n\
12753\tbreakpoint - normal breakpoint\n\
12754\twatchpoint - watchpoint\n\
12755The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12756the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12757breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12758address and file/line number respectively.\n\
12759\n\
12760Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12761are set to the address of the last breakpoint listed unless the command\n\
12762is prefixed with \"server \".\n\n\
c906108c 12763Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12764breakpoint set."));
c906108c 12765
1a966eab
AC
12766 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12767Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12768The \"Type\" column indicates one of:\n\
12769\tbreakpoint - normal breakpoint\n\
12770\twatchpoint - watchpoint\n\
12771\tlongjmp - internal breakpoint used to step through longjmp()\n\
12772\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12773\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12774\tfinish - internal breakpoint used by the \"finish\" command\n\
12775The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12776the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12777breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12778address and file/line number respectively.\n\
12779\n\
12780Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12781are set to the address of the last breakpoint listed unless the command\n\
12782is prefixed with \"server \".\n\n\
c906108c 12783Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12784breakpoint set."),
c906108c
SS
12785 &maintenanceinfolist);
12786
44feb3ce
TT
12787 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12788Set catchpoints to catch events."),
12789 &catch_cmdlist, "catch ",
12790 0/*allow-unknown*/, &cmdlist);
12791
12792 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12793Set temporary catchpoints to catch events."),
12794 &tcatch_cmdlist, "tcatch ",
12795 0/*allow-unknown*/, &cmdlist);
12796
12797 /* Add catch and tcatch sub-commands. */
12798 add_catch_command ("catch", _("\
12799Catch an exception, when caught.\n\
12800With an argument, catch only exceptions with the given name."),
12801 catch_catch_command,
a96d9b2e 12802 NULL,
44feb3ce
TT
12803 CATCH_PERMANENT,
12804 CATCH_TEMPORARY);
12805 add_catch_command ("throw", _("\
12806Catch an exception, when thrown.\n\
12807With an argument, catch only exceptions with the given name."),
12808 catch_throw_command,
a96d9b2e 12809 NULL,
44feb3ce
TT
12810 CATCH_PERMANENT,
12811 CATCH_TEMPORARY);
12812 add_catch_command ("fork", _("Catch calls to fork."),
12813 catch_fork_command_1,
a96d9b2e 12814 NULL,
44feb3ce
TT
12815 (void *) (uintptr_t) catch_fork_permanent,
12816 (void *) (uintptr_t) catch_fork_temporary);
12817 add_catch_command ("vfork", _("Catch calls to vfork."),
12818 catch_fork_command_1,
a96d9b2e 12819 NULL,
44feb3ce
TT
12820 (void *) (uintptr_t) catch_vfork_permanent,
12821 (void *) (uintptr_t) catch_vfork_temporary);
12822 add_catch_command ("exec", _("Catch calls to exec."),
12823 catch_exec_command_1,
a96d9b2e
SDJ
12824 NULL,
12825 CATCH_PERMANENT,
12826 CATCH_TEMPORARY);
12827 add_catch_command ("syscall", _("\
12828Catch system calls by their names and/or numbers.\n\
12829Arguments say which system calls to catch. If no arguments\n\
12830are given, every system call will be caught.\n\
12831Arguments, if given, should be one or more system call names\n\
12832(if your system supports that), or system call numbers."),
12833 catch_syscall_command_1,
12834 catch_syscall_completer,
44feb3ce
TT
12835 CATCH_PERMANENT,
12836 CATCH_TEMPORARY);
44feb3ce
TT
12837 add_catch_command ("exception", _("\
12838Catch Ada exceptions, when raised.\n\
12839With an argument, catch only exceptions with the given name."),
12840 catch_ada_exception_command,
a96d9b2e 12841 NULL,
44feb3ce
TT
12842 CATCH_PERMANENT,
12843 CATCH_TEMPORARY);
12844 add_catch_command ("assert", _("\
12845Catch failed Ada assertions, when raised.\n\
12846With an argument, catch only exceptions with the given name."),
12847 catch_assert_command,
a96d9b2e 12848 NULL,
44feb3ce
TT
12849 CATCH_PERMANENT,
12850 CATCH_TEMPORARY);
c5aa993b 12851
1bedd215
AC
12852 c = add_com ("watch", class_breakpoint, watch_command, _("\
12853Set a watchpoint for an expression.\n\
06a64a0b 12854Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12855A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12856an expression changes.\n\
12857If -l or -location is given, this evaluates EXPRESSION and watches\n\
12858the memory to which it refers."));
65d12d83 12859 set_cmd_completer (c, expression_completer);
c906108c 12860
1bedd215
AC
12861 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12862Set a read watchpoint for an expression.\n\
06a64a0b 12863Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12864A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12865an expression is read.\n\
12866If -l or -location is given, this evaluates EXPRESSION and watches\n\
12867the memory to which it refers."));
65d12d83 12868 set_cmd_completer (c, expression_completer);
c906108c 12869
1bedd215
AC
12870 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12871Set a watchpoint for an expression.\n\
06a64a0b 12872Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12873A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12874an expression is either read or written.\n\
12875If -l or -location is given, this evaluates EXPRESSION and watches\n\
12876the memory to which it refers."));
65d12d83 12877 set_cmd_completer (c, expression_completer);
c906108c 12878
d77f58be 12879 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 12880Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 12881
920d2a44
AC
12882 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12883 respond to changes - contrary to the description. */
85c07804
AC
12884 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12885 &can_use_hw_watchpoints, _("\
12886Set debugger's willingness to use watchpoint hardware."), _("\
12887Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12888If zero, gdb will not use hardware for new watchpoints, even if\n\
12889such is available. (However, any hardware watchpoints that were\n\
12890created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12891hardware.)"),
12892 NULL,
920d2a44 12893 show_can_use_hw_watchpoints,
85c07804 12894 &setlist, &showlist);
c906108c
SS
12895
12896 can_use_hw_watchpoints = 1;
fa8d40ab 12897
1042e4c0
SS
12898 /* Tracepoint manipulation commands. */
12899
12900 c = add_com ("trace", class_breakpoint, trace_command, _("\
12901Set a tracepoint at specified line or function.\n\
12902\n"
12903BREAK_ARGS_HELP ("trace") "\n\
12904Do \"help tracepoints\" for info on other tracepoint commands."));
12905 set_cmd_completer (c, location_completer);
12906
12907 add_com_alias ("tp", "trace", class_alias, 0);
12908 add_com_alias ("tr", "trace", class_alias, 1);
12909 add_com_alias ("tra", "trace", class_alias, 1);
12910 add_com_alias ("trac", "trace", class_alias, 1);
12911
7a697b8d
SS
12912 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12913Set a fast tracepoint at specified line or function.\n\
12914\n"
12915BREAK_ARGS_HELP ("ftrace") "\n\
12916Do \"help tracepoints\" for info on other tracepoint commands."));
12917 set_cmd_completer (c, location_completer);
12918
0fb4aa4b
PA
12919 c = add_com ("strace", class_breakpoint, strace_command, _("\
12920Set a static tracepoint at specified line, function or marker.\n\
12921\n\
12922strace [LOCATION] [if CONDITION]\n\
12923LOCATION may be a line number, function name, \"*\" and an address,\n\
12924or -m MARKER_ID.\n\
12925If a line number is specified, probe the marker at start of code\n\
12926for that line. If a function is specified, probe the marker at start\n\
12927of code for that function. If an address is specified, probe the marker\n\
12928at that exact address. If a marker id is specified, probe the marker\n\
12929with that name. With no LOCATION, uses current execution address of\n\
12930the selected stack frame.\n\
12931Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12932This collects arbitrary user data passed in the probe point call to the\n\
12933tracing library. You can inspect it when analyzing the trace buffer,\n\
12934by printing the $_sdata variable like any other convenience variable.\n\
12935\n\
12936CONDITION is a boolean expression.\n\
12937\n\
d41c0fc8
PA
12938Multiple tracepoints at one place are permitted, and useful if their\n\
12939conditions are different.\n\
0fb4aa4b
PA
12940\n\
12941Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12942Do \"help tracepoints\" for info on other tracepoint commands."));
12943 set_cmd_completer (c, location_completer);
12944
1042e4c0 12945 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 12946Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
12947Convenience variable \"$tpnum\" contains the number of the\n\
12948last tracepoint set."));
12949
12950 add_info_alias ("tp", "tracepoints", 1);
12951
12952 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12953Delete specified tracepoints.\n\
12954Arguments are tracepoint numbers, separated by spaces.\n\
12955No argument means delete all tracepoints."),
12956 &deletelist);
12957
12958 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
12959Disable specified tracepoints.\n\
12960Arguments are tracepoint numbers, separated by spaces.\n\
12961No argument means disable all tracepoints."),
12962 &disablelist);
12963 deprecate_cmd (c, "disable");
12964
12965 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
12966Enable specified tracepoints.\n\
12967Arguments are tracepoint numbers, separated by spaces.\n\
12968No argument means enable all tracepoints."),
12969 &enablelist);
12970 deprecate_cmd (c, "enable");
12971
12972 add_com ("passcount", class_trace, trace_pass_command, _("\
12973Set the passcount for a tracepoint.\n\
12974The trace will end when the tracepoint has been passed 'count' times.\n\
12975Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
12976if TPNUM is omitted, passcount refers to the last tracepoint defined."));
12977
6149aea9
PA
12978 add_prefix_cmd ("save", class_breakpoint, save_command,
12979 _("Save breakpoint definitions as a script."),
12980 &save_cmdlist, "save ",
12981 0/*allow-unknown*/, &cmdlist);
12982
12983 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
12984Save current breakpoint definitions as a script.\n\
cce7e648 12985This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
12986catchpoints, tracepoints). Use the 'source' command in another debug\n\
12987session to restore them."),
12988 &save_cmdlist);
12989 set_cmd_completer (c, filename_completer);
12990
12991 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 12992Save current tracepoint definitions as a script.\n\
6149aea9
PA
12993Use the 'source' command in another debug session to restore them."),
12994 &save_cmdlist);
1042e4c0
SS
12995 set_cmd_completer (c, filename_completer);
12996
6149aea9
PA
12997 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
12998 deprecate_cmd (c, "save tracepoints");
12999
1bedd215 13000 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13001Breakpoint specific settings\n\
13002Configure various breakpoint-specific variables such as\n\
1bedd215 13003pending breakpoint behavior"),
fa8d40ab
JJ
13004 &breakpoint_set_cmdlist, "set breakpoint ",
13005 0/*allow-unknown*/, &setlist);
1bedd215 13006 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13007Breakpoint specific settings\n\
13008Configure various breakpoint-specific variables such as\n\
1bedd215 13009pending breakpoint behavior"),
fa8d40ab
JJ
13010 &breakpoint_show_cmdlist, "show breakpoint ",
13011 0/*allow-unknown*/, &showlist);
13012
7915a72c
AC
13013 add_setshow_auto_boolean_cmd ("pending", no_class,
13014 &pending_break_support, _("\
13015Set debugger's behavior regarding pending breakpoints."), _("\
13016Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13017If on, an unrecognized breakpoint location will cause gdb to create a\n\
13018pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13019an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13020user-query to see if a pending breakpoint should be created."),
2c5b56ce 13021 NULL,
920d2a44 13022 show_pending_break_support,
6e1d7d6c
AC
13023 &breakpoint_set_cmdlist,
13024 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13025
13026 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13027
13028 add_setshow_boolean_cmd ("auto-hw", no_class,
13029 &automatic_hardware_breakpoints, _("\
13030Set automatic usage of hardware breakpoints."), _("\
13031Show automatic usage of hardware breakpoints."), _("\
13032If set, the debugger will automatically use hardware breakpoints for\n\
13033breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13034a warning will be emitted for such breakpoints."),
13035 NULL,
13036 show_automatic_hardware_breakpoints,
13037 &breakpoint_set_cmdlist,
13038 &breakpoint_show_cmdlist);
74960c60 13039
33e5cbd6
PA
13040 add_setshow_enum_cmd ("always-inserted", class_support,
13041 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13042Set mode for inserting breakpoints."), _("\
13043Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13044When this mode is off, breakpoints are inserted in inferior when it is\n\
13045resumed, and removed when execution stops. When this mode is on,\n\
13046breakpoints are inserted immediately and removed only when the user\n\
13047deletes the breakpoint. When this mode is auto (which is the default),\n\
13048the behaviour depends on the non-stop setting (see help set non-stop).\n\
13049In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13050behaves as if always-inserted mode is on; if gdb is controlling the\n\
13051inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13052 NULL,
13053 &show_always_inserted_mode,
13054 &breakpoint_set_cmdlist,
13055 &breakpoint_show_cmdlist);
f1310107
TJB
13056
13057 add_com ("break-range", class_breakpoint, break_range_command, _("\
13058Set a breakpoint for an address range.\n\
13059break-range START-LOCATION, END-LOCATION\n\
13060where START-LOCATION and END-LOCATION can be one of the following:\n\
13061 LINENUM, for that line in the current file,\n\
13062 FILE:LINENUM, for that line in that file,\n\
13063 +OFFSET, for that number of lines after the current line\n\
13064 or the start of the range\n\
13065 FUNCTION, for the first line in that function,\n\
13066 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13067 *ADDRESS, for the instruction at that address.\n\
13068\n\
13069The breakpoint will stop execution of the inferior whenever it executes\n\
13070an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13071range (including START-LOCATION and END-LOCATION)."));
13072
765dc015 13073 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13074
13075 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13076}
This page took 2.071134 seconds and 4 git commands to generate.