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