Add a testcase for PR ld/12627.
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
c906108c 67
1042e4c0
SS
68/* readline include files */
69#include "readline/readline.h"
70#include "readline/history.h"
71
72/* readline defines this. */
73#undef savestring
74
034dad6f 75#include "mi/mi-common.h"
7371cf6d 76#include "python/python.h"
104c1213 77
44feb3ce
TT
78/* Arguments to pass as context to some catch command handlers. */
79#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
80#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
a14ed312 88static void disable_command (char *, int);
c906108c 89
a14ed312 90static void enable_command (char *, int);
c906108c 91
95a42b64
TT
92static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
93 void *),
94 void *);
c906108c 95
a14ed312 96static void ignore_command (char *, int);
c906108c 97
4efb68b1 98static int breakpoint_re_set_one (void *);
c906108c 99
a14ed312 100static void clear_command (char *, int);
c906108c 101
a14ed312 102static void catch_command (char *, int);
c906108c 103
e09342b5 104static int can_use_hardware_watchpoint (struct value *, int);
c906108c 105
98deb0da 106static void break_command_1 (char *, int, int);
c906108c 107
a14ed312 108static void mention (struct breakpoint *);
c906108c 109
4a64f543
MS
110/* This function is used in gdbtk sources and thus can not be made
111 static. */
63c252f8 112struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
113 struct symtab_and_line,
114 enum bptype);
c906108c 115
76897487
KB
116static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
117
a6d9a66e
UW
118static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
119 CORE_ADDR bpaddr,
88f7da05 120 enum bptype bptype);
76897487 121
6c95b8df
PA
122static void describe_other_breakpoints (struct gdbarch *,
123 struct program_space *, CORE_ADDR,
5af949e3 124 struct obj_section *, int);
c906108c 125
6c95b8df
PA
126static int breakpoint_address_match (struct address_space *aspace1,
127 CORE_ADDR addr1,
128 struct address_space *aspace2,
129 CORE_ADDR addr2);
130
85d721b8
PA
131static int watchpoint_locations_match (struct bp_location *loc1,
132 struct bp_location *loc2);
133
f1310107
TJB
134static int breakpoint_location_address_match (struct bp_location *bl,
135 struct address_space *aspace,
136 CORE_ADDR addr);
137
a14ed312 138static void breakpoints_info (char *, int);
c906108c 139
d77f58be
SS
140static void watchpoints_info (char *, int);
141
e5a67952
MS
142static int breakpoint_1 (char *, int,
143 int (*) (const struct breakpoint *));
c906108c 144
4efb68b1 145static int breakpoint_cond_eval (void *);
c906108c 146
4efb68b1 147static void cleanup_executing_breakpoints (void *);
c906108c 148
a14ed312 149static void commands_command (char *, int);
c906108c 150
a14ed312 151static void condition_command (char *, int);
c906108c 152
c5aa993b
JM
153typedef enum
154 {
155 mark_inserted,
156 mark_uninserted
157 }
158insertion_state_t;
c906108c 159
0bde7532 160static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 161static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 162
a14ed312 163static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
164
165static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 166
4efb68b1 167static int watchpoint_check (void *);
c906108c 168
a14ed312 169static void maintenance_info_breakpoints (char *, int);
c906108c 170
a14ed312 171static int hw_breakpoint_used_count (void);
c906108c 172
a14ed312 173static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 174
a14ed312 175static void hbreak_command (char *, int);
c906108c 176
a14ed312 177static void thbreak_command (char *, int);
c906108c 178
a14ed312 179static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 180
a14ed312 181static void stop_command (char *arg, int from_tty);
7a292a7a 182
a14ed312 183static void stopin_command (char *arg, int from_tty);
7a292a7a 184
a14ed312 185static void stopat_command (char *arg, int from_tty);
7a292a7a 186
a14ed312 187static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 188
d85310f7
MS
189static void catch_exception_command_1 (enum exception_event_kind ex_event,
190 char *arg, int tempflag, int from_tty);
7a292a7a 191
a14ed312 192static void tcatch_command (char *arg, int from_tty);
7a292a7a 193
d03285ec
UW
194static void detach_single_step_breakpoints (void);
195
6c95b8df
PA
196static int single_step_breakpoint_inserted_here_p (struct address_space *,
197 CORE_ADDR pc);
1aafd4da 198
fe3f5fa8 199static void free_bp_location (struct bp_location *loc);
f431efe5
PA
200static void incref_bp_location (struct bp_location *loc);
201static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 202
39d61571 203static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 204
b60e7edf 205static void update_global_location_list (int);
a5606eee 206
b60e7edf 207static void update_global_location_list_nothrow (int);
74960c60 208
d77f58be 209static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 210
d77f58be 211static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 212
74960c60 213static void insert_breakpoint_locations (void);
a5606eee 214
a96d9b2e
SDJ
215static int syscall_catchpoint_p (struct breakpoint *b);
216
1042e4c0
SS
217static void tracepoints_info (char *, int);
218
219static void delete_trace_command (char *, int);
220
221static void enable_trace_command (char *, int);
222
223static void disable_trace_command (char *, int);
224
225static void trace_pass_command (char *, int);
226
0fb4aa4b
PA
227/* Assuming we're creating a static tracepoint, does S look like a
228 static tracepoint marker spec ("-m MARKER_ID")? */
229#define is_marker_spec(s) \
f5a8e22b 230 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 231
5cea2a26
PA
232/* A reference-counted struct command_line. This lets multiple
233 breakpoints share a single command list. */
234struct counted_command_line
235{
236 /* The reference count. */
237 int refc;
238
239 /* The command list. */
240 struct command_line *commands;
241};
242
243struct command_line *
244breakpoint_commands (struct breakpoint *b)
245{
246 return b->commands ? b->commands->commands : NULL;
247}
3daf8fe5 248
f3b1572e
PA
249/* Flag indicating that a command has proceeded the inferior past the
250 current breakpoint. */
251
252static int breakpoint_proceeded;
253
956a9fb9 254const char *
2cec12e5
AR
255bpdisp_text (enum bpdisp disp)
256{
4a64f543
MS
257 /* NOTE: the following values are a part of MI protocol and
258 represent values of 'disp' field returned when inferior stops at
259 a breakpoint. */
bc043ef3 260 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 261
2cec12e5
AR
262 return bpdisps[(int) disp];
263}
c906108c 264
4a64f543 265/* Prototypes for exported functions. */
c906108c 266/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 267 if such is available. */
c906108c
SS
268static int can_use_hw_watchpoints;
269
920d2a44
AC
270static void
271show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
272 struct cmd_list_element *c,
273 const char *value)
274{
3e43a32a
MS
275 fprintf_filtered (file,
276 _("Debugger's willingness to use "
277 "watchpoint hardware is %s.\n"),
920d2a44
AC
278 value);
279}
280
fa8d40ab
JJ
281/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
282 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 283 for unrecognized breakpoint locations.
fa8d40ab
JJ
284 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
285static enum auto_boolean pending_break_support;
920d2a44
AC
286static void
287show_pending_break_support (struct ui_file *file, int from_tty,
288 struct cmd_list_element *c,
289 const char *value)
290{
3e43a32a
MS
291 fprintf_filtered (file,
292 _("Debugger's behavior regarding "
293 "pending breakpoints is %s.\n"),
920d2a44
AC
294 value);
295}
fa8d40ab 296
765dc015 297/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 298 set with "break" but falling in read-only memory.
765dc015
VP
299 If 0, gdb will warn about such breakpoints, but won't automatically
300 use hardware breakpoints. */
301static int automatic_hardware_breakpoints;
302static void
303show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
304 struct cmd_list_element *c,
305 const char *value)
306{
3e43a32a
MS
307 fprintf_filtered (file,
308 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
309 value);
310}
311
33e5cbd6
PA
312/* If on, gdb will keep breakpoints inserted even as inferior is
313 stopped, and immediately insert any new breakpoints. If off, gdb
314 will insert breakpoints into inferior only when resuming it, and
315 will remove breakpoints upon stop. If auto, GDB will behave as ON
316 if in non-stop mode, and as OFF if all-stop mode.*/
317
318static const char always_inserted_auto[] = "auto";
319static const char always_inserted_on[] = "on";
320static const char always_inserted_off[] = "off";
321static const char *always_inserted_enums[] = {
322 always_inserted_auto,
323 always_inserted_off,
324 always_inserted_on,
325 NULL
326};
327static const char *always_inserted_mode = always_inserted_auto;
328static void
74960c60 329show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 330 struct cmd_list_element *c, const char *value)
74960c60 331{
33e5cbd6 332 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
333 fprintf_filtered (file,
334 _("Always inserted breakpoint "
335 "mode is %s (currently %s).\n"),
33e5cbd6
PA
336 value,
337 breakpoints_always_inserted_mode () ? "on" : "off");
338 else
3e43a32a
MS
339 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
340 value);
74960c60
VP
341}
342
33e5cbd6
PA
343int
344breakpoints_always_inserted_mode (void)
345{
346 return (always_inserted_mode == always_inserted_on
347 || (always_inserted_mode == always_inserted_auto && non_stop));
348}
765dc015 349
a14ed312 350void _initialize_breakpoint (void);
c906108c 351
c906108c
SS
352/* Are we executing breakpoint commands? */
353static int executing_breakpoint_commands;
354
c02f5703
MS
355/* Are overlay event breakpoints enabled? */
356static int overlay_events_enabled;
357
e09342b5
TJB
358/* See description in breakpoint.h. */
359int target_exact_watchpoints = 0;
360
c906108c 361/* Walk the following statement or block through all breakpoints.
4a64f543
MS
362 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
363 current breakpoint. */
c906108c 364
5c44784c 365#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 366
5c44784c
JM
367#define ALL_BREAKPOINTS_SAFE(B,TMP) \
368 for (B = breakpoint_chain; \
369 B ? (TMP=B->next, 1): 0; \
370 B = TMP)
c906108c 371
4a64f543
MS
372/* Similar iterator for the low-level breakpoints. SAFE variant is
373 not provided so update_global_location_list must not be called
374 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 375
876fa593
JK
376#define ALL_BP_LOCATIONS(B,BP_TMP) \
377 for (BP_TMP = bp_location; \
378 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
379 BP_TMP++)
7cc221ef 380
1042e4c0
SS
381/* Iterator for tracepoints only. */
382
383#define ALL_TRACEPOINTS(B) \
384 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 385 if (is_tracepoint (B))
1042e4c0 386
7cc221ef 387/* Chains of all breakpoints defined. */
c906108c
SS
388
389struct breakpoint *breakpoint_chain;
390
876fa593
JK
391/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
392
393static struct bp_location **bp_location;
394
395/* Number of elements of BP_LOCATION. */
396
397static unsigned bp_location_count;
398
4a64f543
MS
399/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
400 ADDRESS for the current elements of BP_LOCATION which get a valid
401 result from bp_location_has_shadow. You can use it for roughly
402 limiting the subrange of BP_LOCATION to scan for shadow bytes for
403 an address you need to read. */
876fa593
JK
404
405static CORE_ADDR bp_location_placed_address_before_address_max;
406
4a64f543
MS
407/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
408 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
409 BP_LOCATION which get a valid result from bp_location_has_shadow.
410 You can use it for roughly limiting the subrange of BP_LOCATION to
411 scan for shadow bytes for an address you need to read. */
876fa593
JK
412
413static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 414
4a64f543
MS
415/* The locations that no longer correspond to any breakpoint, unlinked
416 from bp_location array, but for which a hit may still be reported
417 by a target. */
20874c92
VP
418VEC(bp_location_p) *moribund_locations = NULL;
419
c906108c
SS
420/* Number of last breakpoint made. */
421
95a42b64
TT
422static int breakpoint_count;
423
86b17b60
PA
424/* The value of `breakpoint_count' before the last command that
425 created breakpoints. If the last (break-like) command created more
426 than one breakpoint, then the difference between BREAKPOINT_COUNT
427 and PREV_BREAKPOINT_COUNT is more than one. */
428static int prev_breakpoint_count;
c906108c 429
1042e4c0
SS
430/* Number of last tracepoint made. */
431
95a42b64 432static int tracepoint_count;
1042e4c0 433
6149aea9
PA
434static struct cmd_list_element *breakpoint_set_cmdlist;
435static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 436struct cmd_list_element *save_cmdlist;
6149aea9 437
468d015d
JJ
438/* Return whether a breakpoint is an active enabled breakpoint. */
439static int
440breakpoint_enabled (struct breakpoint *b)
441{
0d381245 442 return (b->enable_state == bp_enabled);
468d015d
JJ
443}
444
c906108c
SS
445/* Set breakpoint count to NUM. */
446
95a42b64 447static void
fba45db2 448set_breakpoint_count (int num)
c906108c 449{
86b17b60 450 prev_breakpoint_count = breakpoint_count;
c906108c 451 breakpoint_count = num;
4fa62494 452 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
453}
454
86b17b60
PA
455/* Used by `start_rbreak_breakpoints' below, to record the current
456 breakpoint count before "rbreak" creates any breakpoint. */
457static int rbreak_start_breakpoint_count;
458
95a42b64
TT
459/* Called at the start an "rbreak" command to record the first
460 breakpoint made. */
86b17b60 461
95a42b64
TT
462void
463start_rbreak_breakpoints (void)
464{
86b17b60 465 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
466}
467
468/* Called at the end of an "rbreak" command to record the last
469 breakpoint made. */
86b17b60 470
95a42b64
TT
471void
472end_rbreak_breakpoints (void)
473{
86b17b60 474 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
475}
476
4a64f543 477/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
478
479void
fba45db2 480clear_breakpoint_hit_counts (void)
c906108c
SS
481{
482 struct breakpoint *b;
483
484 ALL_BREAKPOINTS (b)
485 b->hit_count = 0;
486}
487
9add0f1b
TT
488/* Allocate a new counted_command_line with reference count of 1.
489 The new structure owns COMMANDS. */
490
491static struct counted_command_line *
492alloc_counted_command_line (struct command_line *commands)
493{
494 struct counted_command_line *result
495 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 496
9add0f1b
TT
497 result->refc = 1;
498 result->commands = commands;
499 return result;
500}
501
502/* Increment reference count. This does nothing if CMD is NULL. */
503
504static void
505incref_counted_command_line (struct counted_command_line *cmd)
506{
507 if (cmd)
508 ++cmd->refc;
509}
510
511/* Decrement reference count. If the reference count reaches 0,
512 destroy the counted_command_line. Sets *CMDP to NULL. This does
513 nothing if *CMDP is NULL. */
514
515static void
516decref_counted_command_line (struct counted_command_line **cmdp)
517{
518 if (*cmdp)
519 {
520 if (--(*cmdp)->refc == 0)
521 {
522 free_command_lines (&(*cmdp)->commands);
523 xfree (*cmdp);
524 }
525 *cmdp = NULL;
526 }
527}
528
529/* A cleanup function that calls decref_counted_command_line. */
530
531static void
532do_cleanup_counted_command_line (void *arg)
533{
534 decref_counted_command_line (arg);
535}
536
537/* Create a cleanup that calls decref_counted_command_line on the
538 argument. */
539
540static struct cleanup *
541make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
542{
543 return make_cleanup (do_cleanup_counted_command_line, cmdp);
544}
545
c906108c
SS
546/* Default address, symtab and line to put a breakpoint at
547 for "break" command with no arg.
4a64f543 548 If default_breakpoint_valid is zero, the other three are
c906108c
SS
549 not valid, and "break" with no arg is an error.
550
551 This set by print_stack_frame, which calls set_default_breakpoint. */
552
553int default_breakpoint_valid;
554CORE_ADDR default_breakpoint_address;
555struct symtab *default_breakpoint_symtab;
556int default_breakpoint_line;
6c95b8df
PA
557struct program_space *default_breakpoint_pspace;
558
c906108c 559\f
48cb2d85
VP
560/* Return the breakpoint with the specified number, or NULL
561 if the number does not refer to an existing breakpoint. */
562
563struct breakpoint *
564get_breakpoint (int num)
565{
566 struct breakpoint *b;
567
568 ALL_BREAKPOINTS (b)
569 if (b->number == num)
570 return b;
571
572 return NULL;
573}
5c44784c 574
c906108c 575\f
adc36818
PM
576
577void
578set_breakpoint_condition (struct breakpoint *b, char *exp,
579 int from_tty)
580{
581 struct bp_location *loc = b->loc;
582
583 for (; loc; loc = loc->next)
584 {
585 xfree (loc->cond);
586 loc->cond = NULL;
587 }
588 xfree (b->cond_string);
589 b->cond_string = NULL;
590 xfree (b->cond_exp);
591 b->cond_exp = NULL;
592
593 if (*exp == 0)
594 {
595 if (from_tty)
596 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
597 }
598 else
599 {
600 char *arg = exp;
cc59ec59 601
adc36818
PM
602 /* I don't know if it matters whether this is the string the user
603 typed in or the decompiled expression. */
604 b->cond_string = xstrdup (arg);
605 b->condition_not_parsed = 0;
606
607 if (is_watchpoint (b))
608 {
609 innermost_block = NULL;
610 arg = exp;
611 b->cond_exp = parse_exp_1 (&arg, 0, 0);
612 if (*arg)
613 error (_("Junk at end of expression"));
614 b->cond_exp_valid_block = innermost_block;
615 }
616 else
617 {
618 for (loc = b->loc; loc; loc = loc->next)
619 {
620 arg = exp;
621 loc->cond =
622 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
623 if (*arg)
624 error (_("Junk at end of expression"));
625 }
626 }
627 }
628 breakpoints_changed ();
629 observer_notify_breakpoint_modified (b->number);
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
VP
793 breakpoints_changed ();
794 observer_notify_breakpoint_modified (b->number);
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)
808 observer_notify_breakpoint_modified (b->number);
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)
821 observer_notify_breakpoint_modified (b->number);
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)
834 observer_notify_breakpoint_modified (b->number);
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 ();
911 observer_notify_breakpoint_modified (b->number);
c5aa993b 912 }
95a42b64
TT
913}
914
915static void
4a64f543
MS
916commands_command_1 (char *arg, int from_tty,
917 struct command_line *control)
95a42b64
TT
918{
919 struct cleanup *cleanups;
920 struct commands_info info;
921
922 info.from_tty = from_tty;
923 info.control = control;
924 info.cmd = NULL;
925 /* If we read command lines from the user, then `info' will hold an
926 extra reference to the commands that we must clean up. */
927 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
928
929 if (arg == NULL || !*arg)
930 {
86b17b60 931 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
932 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
933 breakpoint_count);
95a42b64
TT
934 else if (breakpoint_count > 0)
935 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
936 else
937 {
938 /* So that we don't try to free the incoming non-NULL
939 argument in the cleanup below. Mapping breakpoint
940 numbers will fail in this case. */
941 arg = NULL;
942 }
95a42b64 943 }
9766ced4
SS
944 else
945 /* The command loop has some static state, so we need to preserve
946 our argument. */
947 arg = xstrdup (arg);
86b17b60
PA
948
949 if (arg != NULL)
950 make_cleanup (xfree, arg);
951
952 info.arg = arg;
95a42b64
TT
953
954 map_breakpoint_numbers (arg, do_map_commands_command, &info);
955
956 if (info.cmd == NULL)
957 error (_("No breakpoints specified."));
958
959 do_cleanups (cleanups);
960}
961
962static void
963commands_command (char *arg, int from_tty)
964{
965 commands_command_1 (arg, from_tty, NULL);
c906108c 966}
40c03ae8
EZ
967
968/* Like commands_command, but instead of reading the commands from
969 input stream, takes them from an already parsed command structure.
970
971 This is used by cli-script.c to DTRT with breakpoint commands
972 that are part of if and while bodies. */
973enum command_control_type
974commands_from_control_command (char *arg, struct command_line *cmd)
975{
95a42b64
TT
976 commands_command_1 (arg, 0, cmd);
977 return simple_control;
40c03ae8 978}
876fa593
JK
979
980/* Return non-zero if BL->TARGET_INFO contains valid information. */
981
982static int
983bp_location_has_shadow (struct bp_location *bl)
984{
985 if (bl->loc_type != bp_loc_software_breakpoint)
986 return 0;
987 if (!bl->inserted)
988 return 0;
989 if (bl->target_info.shadow_len == 0)
990 /* bp isn't valid, or doesn't shadow memory. */
991 return 0;
992 return 1;
993}
994
8defab1a 995/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
996 by replacing any memory breakpoints with their shadowed contents.
997
998 The range of shadowed area by each bp_location is:
35df4500
TJB
999 bl->address - bp_location_placed_address_before_address_max
1000 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1001 The range we were requested to resolve shadows for is:
1002 memaddr ... memaddr + len
1003 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1004 memaddr + len <= (bl->address
1005 - bp_location_placed_address_before_address_max)
876fa593 1006 and:
35df4500 1007 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1008
8defab1a
DJ
1009void
1010breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1011{
4a64f543
MS
1012 /* Left boundary, right boundary and median element of our binary
1013 search. */
876fa593
JK
1014 unsigned bc_l, bc_r, bc;
1015
4a64f543
MS
1016 /* Find BC_L which is a leftmost element which may affect BUF
1017 content. It is safe to report lower value but a failure to
1018 report higher one. */
876fa593
JK
1019
1020 bc_l = 0;
1021 bc_r = bp_location_count;
1022 while (bc_l + 1 < bc_r)
1023 {
35df4500 1024 struct bp_location *bl;
876fa593
JK
1025
1026 bc = (bc_l + bc_r) / 2;
35df4500 1027 bl = bp_location[bc];
876fa593 1028
4a64f543
MS
1029 /* Check first BL->ADDRESS will not overflow due to the added
1030 constant. Then advance the left boundary only if we are sure
1031 the BC element can in no way affect the BUF content (MEMADDR
1032 to MEMADDR + LEN range).
876fa593 1033
4a64f543
MS
1034 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1035 offset so that we cannot miss a breakpoint with its shadow
1036 range tail still reaching MEMADDR. */
c5aa993b 1037
35df4500
TJB
1038 if ((bl->address + bp_location_shadow_len_after_address_max
1039 >= bl->address)
1040 && (bl->address + bp_location_shadow_len_after_address_max
1041 <= memaddr))
876fa593
JK
1042 bc_l = bc;
1043 else
1044 bc_r = bc;
1045 }
1046
128070bb
PA
1047 /* Due to the binary search above, we need to make sure we pick the
1048 first location that's at BC_L's address. E.g., if there are
1049 multiple locations at the same address, BC_L may end up pointing
1050 at a duplicate location, and miss the "master"/"inserted"
1051 location. Say, given locations L1, L2 and L3 at addresses A and
1052 B:
1053
1054 L1@A, L2@A, L3@B, ...
1055
1056 BC_L could end up pointing at location L2, while the "master"
1057 location could be L1. Since the `loc->inserted' flag is only set
1058 on "master" locations, we'd forget to restore the shadow of L1
1059 and L2. */
1060 while (bc_l > 0
1061 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1062 bc_l--;
1063
876fa593
JK
1064 /* Now do full processing of the found relevant range of elements. */
1065
1066 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1067 {
35df4500 1068 struct bp_location *bl = bp_location[bc];
876fa593
JK
1069 CORE_ADDR bp_addr = 0;
1070 int bp_size = 0;
1071 int bptoffset = 0;
1072
35df4500
TJB
1073 /* bp_location array has BL->OWNER always non-NULL. */
1074 if (bl->owner->type == bp_none)
8a3fe4f8 1075 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1076 bl->owner->number);
ffce0d52 1077
876fa593
JK
1078 /* Performance optimization: any futher element can no longer affect BUF
1079 content. */
1080
35df4500
TJB
1081 if (bl->address >= bp_location_placed_address_before_address_max
1082 && memaddr + len <= (bl->address
1083 - bp_location_placed_address_before_address_max))
876fa593
JK
1084 break;
1085
35df4500 1086 if (!bp_location_has_shadow (bl))
c5aa993b 1087 continue;
35df4500 1088 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1089 current_program_space->aspace, 0))
1090 continue;
1091
c5aa993b
JM
1092 /* Addresses and length of the part of the breakpoint that
1093 we need to copy. */
35df4500
TJB
1094 bp_addr = bl->target_info.placed_address;
1095 bp_size = bl->target_info.shadow_len;
8defab1a 1096
c5aa993b
JM
1097 if (bp_addr + bp_size <= memaddr)
1098 /* The breakpoint is entirely before the chunk of memory we
1099 are reading. */
1100 continue;
8defab1a 1101
c5aa993b
JM
1102 if (bp_addr >= memaddr + len)
1103 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1104 reading. */
c5aa993b 1105 continue;
c5aa993b 1106
8defab1a
DJ
1107 /* Offset within shadow_contents. */
1108 if (bp_addr < memaddr)
1109 {
1110 /* Only copy the second part of the breakpoint. */
1111 bp_size -= memaddr - bp_addr;
1112 bptoffset = memaddr - bp_addr;
1113 bp_addr = memaddr;
1114 }
c5aa993b 1115
8defab1a
DJ
1116 if (bp_addr + bp_size > memaddr + len)
1117 {
1118 /* Only copy the first part of the breakpoint. */
1119 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1120 }
c5aa993b 1121
8defab1a 1122 memcpy (buf + bp_addr - memaddr,
35df4500 1123 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1124 }
c906108c 1125}
c906108c 1126\f
c5aa993b 1127
60e1c644
PA
1128/* Return true if BPT is of any hardware watchpoint kind. */
1129
a5606eee 1130static int
d77f58be 1131is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1132{
1133 return (bpt->type == bp_hardware_watchpoint
1134 || bpt->type == bp_read_watchpoint
1135 || bpt->type == bp_access_watchpoint);
1136}
7270d8f2 1137
60e1c644
PA
1138/* Return true if BPT is of any watchpoint kind, hardware or
1139 software. */
1140
1141static int
d77f58be 1142is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1143{
1144 return (is_hardware_watchpoint (bpt)
1145 || bpt->type == bp_watchpoint);
1146}
1147
f6bc2008
PA
1148/* Assuming that B is a watchpoint: returns true if the current thread
1149 and its running state are safe to evaluate or update watchpoint B.
1150 Watchpoints on local expressions need to be evaluated in the
1151 context of the thread that was current when the watchpoint was
1152 created, and, that thread needs to be stopped to be able to select
1153 the correct frame context. Watchpoints on global expressions can
1154 be evaluated on any thread, and in any state. It is presently left
1155 to the target allowing memory accesses when threads are
1156 running. */
1157
1158static int
1159watchpoint_in_thread_scope (struct breakpoint *b)
1160{
1161 return (ptid_equal (b->watchpoint_thread, null_ptid)
1162 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1163 && !is_executing (inferior_ptid)));
1164}
1165
d0fb5eae
JK
1166/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1167 associated bp_watchpoint_scope breakpoint. */
1168
1169static void
1170watchpoint_del_at_next_stop (struct breakpoint *b)
1171{
1172 gdb_assert (is_watchpoint (b));
1173
1174 if (b->related_breakpoint != b)
1175 {
1176 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1177 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1178 b->related_breakpoint->disposition = disp_del_at_next_stop;
1179 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1180 b->related_breakpoint = b;
1181 }
1182 b->disposition = disp_del_at_next_stop;
1183}
1184
567e1b4e
JB
1185/* Assuming that B is a watchpoint:
1186 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1187 - Evaluate expression and store the result in B->val
567e1b4e
JB
1188 - Evaluate the condition if there is one, and store the result
1189 in b->loc->cond.
a5606eee
VP
1190 - Update the list of values that must be watched in B->loc.
1191
4a64f543
MS
1192 If the watchpoint disposition is disp_del_at_next_stop, then do
1193 nothing. If this is local watchpoint that is out of scope, delete
1194 it.
1195
1196 Even with `set breakpoint always-inserted on' the watchpoints are
1197 removed + inserted on each stop here. Normal breakpoints must
1198 never be removed because they might be missed by a running thread
1199 when debugging in non-stop mode. On the other hand, hardware
1200 watchpoints (is_hardware_watchpoint; processed here) are specific
1201 to each LWP since they are stored in each LWP's hardware debug
1202 registers. Therefore, such LWP must be stopped first in order to
1203 be able to modify its hardware watchpoints.
1204
1205 Hardware watchpoints must be reset exactly once after being
1206 presented to the user. It cannot be done sooner, because it would
1207 reset the data used to present the watchpoint hit to the user. And
1208 it must not be done later because it could display the same single
1209 watchpoint hit during multiple GDB stops. Note that the latter is
1210 relevant only to the hardware watchpoint types bp_read_watchpoint
1211 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1212 not user-visible - its hit is suppressed if the memory content has
1213 not changed.
1214
1215 The following constraints influence the location where we can reset
1216 hardware watchpoints:
1217
1218 * target_stopped_by_watchpoint and target_stopped_data_address are
1219 called several times when GDB stops.
1220
1221 [linux]
1222 * Multiple hardware watchpoints can be hit at the same time,
1223 causing GDB to stop. GDB only presents one hardware watchpoint
1224 hit at a time as the reason for stopping, and all the other hits
1225 are presented later, one after the other, each time the user
1226 requests the execution to be resumed. Execution is not resumed
1227 for the threads still having pending hit event stored in
1228 LWP_INFO->STATUS. While the watchpoint is already removed from
1229 the inferior on the first stop the thread hit event is kept being
1230 reported from its cached value by linux_nat_stopped_data_address
1231 until the real thread resume happens after the watchpoint gets
1232 presented and thus its LWP_INFO->STATUS gets reset.
1233
1234 Therefore the hardware watchpoint hit can get safely reset on the
1235 watchpoint removal from inferior. */
a79d3c27 1236
b40ce68a 1237static void
a5606eee 1238update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1239{
a5606eee 1240 int within_current_scope;
a5606eee 1241 struct frame_id saved_frame_id;
66076460 1242 int frame_saved;
a5606eee 1243
d0fb5eae
JK
1244 gdb_assert (is_watchpoint (b));
1245
f6bc2008
PA
1246 /* If this is a local watchpoint, we only want to check if the
1247 watchpoint frame is in scope if the current thread is the thread
1248 that was used to create the watchpoint. */
1249 if (!watchpoint_in_thread_scope (b))
1250 return;
1251
a5606eee
VP
1252 if (b->disposition == disp_del_at_next_stop)
1253 return;
1254
66076460 1255 frame_saved = 0;
a5606eee
VP
1256
1257 /* Determine if the watchpoint is within scope. */
1258 if (b->exp_valid_block == NULL)
1259 within_current_scope = 1;
1260 else
1261 {
b5db5dfc
UW
1262 struct frame_info *fi = get_current_frame ();
1263 struct gdbarch *frame_arch = get_frame_arch (fi);
1264 CORE_ADDR frame_pc = get_frame_pc (fi);
1265
1266 /* If we're in a function epilogue, unwinding may not work
1267 properly, so do not attempt to recreate locations at this
1268 point. See similar comments in watchpoint_check. */
1269 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1270 return;
66076460
DJ
1271
1272 /* Save the current frame's ID so we can restore it after
1273 evaluating the watchpoint expression on its own frame. */
1274 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1275 took a frame parameter, so that we didn't have to change the
1276 selected frame. */
1277 frame_saved = 1;
1278 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1279
a5606eee
VP
1280 fi = frame_find_by_id (b->watchpoint_frame);
1281 within_current_scope = (fi != NULL);
1282 if (within_current_scope)
1283 select_frame (fi);
1284 }
1285
b5db5dfc
UW
1286 /* We don't free locations. They are stored in the bp_location array
1287 and update_global_location_list will eventually delete them and
1288 remove breakpoints if needed. */
1289 b->loc = NULL;
1290
a5606eee
VP
1291 if (within_current_scope && reparse)
1292 {
1293 char *s;
d63d0675 1294
a5606eee
VP
1295 if (b->exp)
1296 {
1297 xfree (b->exp);
1298 b->exp = NULL;
1299 }
d63d0675 1300 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1301 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1302 /* If the meaning of expression itself changed, the old value is
1303 no longer relevant. We don't want to report a watchpoint hit
1304 to the user when the old value and the new value may actually
1305 be completely different objects. */
1306 value_free (b->val);
fa4727a6
DJ
1307 b->val = NULL;
1308 b->val_valid = 0;
60e1c644
PA
1309
1310 /* Note that unlike with breakpoints, the watchpoint's condition
1311 expression is stored in the breakpoint object, not in the
1312 locations (re)created below. */
1313 if (b->cond_string != NULL)
1314 {
1315 if (b->cond_exp != NULL)
1316 {
1317 xfree (b->cond_exp);
1318 b->cond_exp = NULL;
1319 }
1320
1321 s = b->cond_string;
1322 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1323 }
a5606eee 1324 }
a5606eee
VP
1325
1326 /* If we failed to parse the expression, for example because
1327 it refers to a global variable in a not-yet-loaded shared library,
1328 don't try to insert watchpoint. We don't automatically delete
1329 such watchpoint, though, since failure to parse expression
1330 is different from out-of-scope watchpoint. */
2d134ed3
PA
1331 if ( !target_has_execution)
1332 {
1333 /* Without execution, memory can't change. No use to try and
1334 set watchpoint locations. The watchpoint will be reset when
1335 the target gains execution, through breakpoint_re_set. */
1336 }
1337 else if (within_current_scope && b->exp)
a5606eee 1338 {
0cf6dd15 1339 int pc = 0;
fa4727a6 1340 struct value *val_chain, *v, *result, *next;
2d134ed3 1341 struct program_space *frame_pspace;
a5606eee 1342
0cf6dd15 1343 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1344
a5606eee
VP
1345 /* Avoid setting b->val if it's already set. The meaning of
1346 b->val is 'the last value' user saw, and we should update
1347 it only if we reported that last value to user. As it
1348 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
1349 if (!b->val_valid)
1350 {
1351 b->val = v;
1352 b->val_valid = 1;
1353 }
a5606eee 1354
2d134ed3
PA
1355 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1356
a5606eee 1357 /* Look at each value on the value chain. */
9fa40276 1358 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1359 {
1360 /* If it's a memory location, and GDB actually needed
1361 its contents to evaluate the expression, then we
fa4727a6
DJ
1362 must watch it. If the first value returned is
1363 still lazy, that means an error occurred reading it;
1364 watch it anyway in case it becomes readable. */
a5606eee 1365 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1366 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1367 {
1368 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1369
a5606eee
VP
1370 /* We only watch structs and arrays if user asked
1371 for it explicitly, never if they just happen to
1372 appear in the middle of some value chain. */
fa4727a6 1373 if (v == result
a5606eee
VP
1374 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1375 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1376 {
1377 CORE_ADDR addr;
1378 int len, type;
1379 struct bp_location *loc, **tmp;
1380
42ae5230 1381 addr = value_address (v);
a5606eee
VP
1382 len = TYPE_LENGTH (value_type (v));
1383 type = hw_write;
1384 if (b->type == bp_read_watchpoint)
1385 type = hw_read;
1386 else if (b->type == bp_access_watchpoint)
1387 type = hw_access;
1388
39d61571 1389 loc = allocate_bp_location (b);
a5606eee
VP
1390 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1391 ;
1392 *tmp = loc;
a6d9a66e 1393 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1394
1395 loc->pspace = frame_pspace;
a5606eee
VP
1396 loc->address = addr;
1397 loc->length = len;
1398 loc->watchpoint_type = type;
1399 }
1400 }
9fa40276
TJB
1401 }
1402
1403 /* Change the type of breakpoint between hardware assisted or
1404 an ordinary watchpoint depending on the hardware support
1405 and free hardware slots. REPARSE is set when the inferior
1406 is started. */
1407 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1408 && reparse)
1409 {
e09342b5 1410 int reg_cnt;
9fa40276
TJB
1411 enum bp_loc_type loc_type;
1412 struct bp_location *bl;
a5606eee 1413
e09342b5
TJB
1414 reg_cnt = can_use_hardware_watchpoint (val_chain, b->exact);
1415
1416 if (reg_cnt)
9fa40276
TJB
1417 {
1418 int i, target_resources_ok, other_type_used;
e09342b5 1419 enum enable_state orig_enable_state;
9fa40276
TJB
1420
1421 /* We need to determine how many resources are already
e09342b5
TJB
1422 used for all other hardware watchpoints plus this one
1423 to see if we still have enough resources to also fit
1424 this watchpoint in as well. To guarantee the
1425 hw_watchpoint_used_count call below counts this
1426 watchpoint, make sure that it is marked as a hardware
1427 watchpoint. */
1428 b->type = bp_hardware_watchpoint;
1429
1430 /* hw_watchpoint_used_count ignores disabled watchpoints,
1431 and b might be disabled if we're being called from
1432 do_enable_breakpoint. */
1433 orig_enable_state = b->enable_state;
1434 b->enable_state = bp_enabled;
1435
9fa40276
TJB
1436 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1437 &other_type_used);
9fa40276 1438
e09342b5
TJB
1439 b->enable_state = orig_enable_state;
1440
1441 target_resources_ok = target_can_use_hardware_watchpoint
1442 (bp_hardware_watchpoint, i, other_type_used);
1443 if (target_resources_ok <= 0)
1444 b->type = bp_watchpoint;
9fa40276
TJB
1445 }
1446 else
1447 b->type = bp_watchpoint;
1448
1449 loc_type = (b->type == bp_watchpoint? bp_loc_other
1450 : bp_loc_hardware_watchpoint);
1451 for (bl = b->loc; bl; bl = bl->next)
1452 bl->loc_type = loc_type;
1453 }
1454
1455 for (v = val_chain; v; v = next)
1456 {
a5606eee
VP
1457 next = value_next (v);
1458 if (v != b->val)
1459 value_free (v);
1460 }
1461
c7437ca6
PA
1462 /* If a software watchpoint is not watching any memory, then the
1463 above left it without any location set up. But,
1464 bpstat_stop_status requires a location to be able to report
1465 stops, so make sure there's at least a dummy one. */
1466 if (b->type == bp_watchpoint && b->loc == NULL)
1467 {
1468 b->loc = allocate_bp_location (b);
1469 b->loc->pspace = frame_pspace;
1470 b->loc->address = -1;
1471 b->loc->length = -1;
1472 b->loc->watchpoint_type = -1;
1473 }
a5606eee
VP
1474 }
1475 else if (!within_current_scope)
7270d8f2 1476 {
ac74f770
MS
1477 printf_filtered (_("\
1478Watchpoint %d deleted because the program has left the block\n\
1479in which its expression is valid.\n"),
a5606eee 1480 b->number);
d0fb5eae 1481 watchpoint_del_at_next_stop (b);
7270d8f2 1482 }
a5606eee
VP
1483
1484 /* Restore the selected frame. */
66076460
DJ
1485 if (frame_saved)
1486 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1487}
1488
a5606eee 1489
74960c60
VP
1490/* Returns 1 iff breakpoint location should be
1491 inserted in the inferior. */
1492static int
35df4500 1493should_be_inserted (struct bp_location *bl)
74960c60 1494{
35df4500 1495 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1496 return 0;
1497
35df4500 1498 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1499 return 0;
1500
35df4500 1501 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1502 return 0;
1503
56710373
PA
1504 /* This is set for example, when we're attached to the parent of a
1505 vfork, and have detached from the child. The child is running
1506 free, and we expect it to do an exec or exit, at which point the
1507 OS makes the parent schedulable again (and the target reports
1508 that the vfork is done). Until the child is done with the shared
1509 memory region, do not insert breakpoints in the parent, otherwise
1510 the child could still trip on the parent's breakpoints. Since
1511 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1512 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1513 return 0;
1514
1042e4c0
SS
1515 /* Tracepoints are inserted by the target at a time of its choosing,
1516 not by us. */
35df4500 1517 if (is_tracepoint (bl->owner))
1042e4c0
SS
1518 return 0;
1519
74960c60
VP
1520 return 1;
1521}
1522
35df4500
TJB
1523/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1524 location. Any error messages are printed to TMP_ERROR_STREAM; and
1525 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1526
4a64f543
MS
1527 NOTE drow/2003-09-09: This routine could be broken down to an
1528 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1529static int
35df4500 1530insert_bp_location (struct bp_location *bl,
26bb91f3 1531 struct ui_file *tmp_error_stream,
fa3a767f 1532 int *disabled_breaks,
26bb91f3 1533 int *hw_breakpoint_error)
879bfdc2
DJ
1534{
1535 int val = 0;
1536
35df4500 1537 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1538 return 0;
1539
8181d85f 1540 /* Initialize the target-specific information. */
35df4500
TJB
1541 memset (&bl->target_info, 0, sizeof (bl->target_info));
1542 bl->target_info.placed_address = bl->address;
1543 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1544 bl->target_info.length = bl->length;
8181d85f 1545
35df4500
TJB
1546 if (bl->loc_type == bp_loc_software_breakpoint
1547 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1548 {
35df4500 1549 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1550 {
1551 /* If the explicitly specified breakpoint type
1552 is not hardware breakpoint, check the memory map to see
1553 if the breakpoint address is in read only memory or not.
4a64f543 1554
765dc015
VP
1555 Two important cases are:
1556 - location type is not hardware breakpoint, memory
1557 is readonly. We change the type of the location to
1558 hardware breakpoint.
4a64f543
MS
1559 - location type is hardware breakpoint, memory is
1560 read-write. This means we've previously made the
1561 location hardware one, but then the memory map changed,
1562 so we undo.
765dc015 1563
4a64f543
MS
1564 When breakpoints are removed, remove_breakpoints will use
1565 location types we've just set here, the only possible
1566 problem is that memory map has changed during running
1567 program, but it's not going to work anyway with current
1568 gdb. */
765dc015 1569 struct mem_region *mr
35df4500 1570 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1571
1572 if (mr)
1573 {
1574 if (automatic_hardware_breakpoints)
1575 {
765dc015
VP
1576 enum bp_loc_type new_type;
1577
1578 if (mr->attrib.mode != MEM_RW)
1579 new_type = bp_loc_hardware_breakpoint;
1580 else
1581 new_type = bp_loc_software_breakpoint;
1582
35df4500 1583 if (new_type != bl->loc_type)
765dc015
VP
1584 {
1585 static int said = 0;
cc59ec59 1586
35df4500 1587 bl->loc_type = new_type;
765dc015
VP
1588 if (!said)
1589 {
3e43a32a
MS
1590 fprintf_filtered (gdb_stdout,
1591 _("Note: automatically using "
1592 "hardware breakpoints for "
1593 "read-only addresses.\n"));
765dc015
VP
1594 said = 1;
1595 }
1596 }
1597 }
35df4500 1598 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1599 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1600 warning (_("cannot set software breakpoint "
1601 "at readonly address %s"),
35df4500 1602 paddress (bl->gdbarch, bl->address));
765dc015
VP
1603 }
1604 }
1605
879bfdc2
DJ
1606 /* First check to see if we have to handle an overlay. */
1607 if (overlay_debugging == ovly_off
35df4500
TJB
1608 || bl->section == NULL
1609 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1610 {
1611 /* No overlay handling: just set the breakpoint. */
1612
35df4500
TJB
1613 if (bl->loc_type == bp_loc_hardware_breakpoint)
1614 val = target_insert_hw_breakpoint (bl->gdbarch,
1615 &bl->target_info);
879bfdc2 1616 else
35df4500
TJB
1617 val = target_insert_breakpoint (bl->gdbarch,
1618 &bl->target_info);
879bfdc2
DJ
1619 }
1620 else
1621 {
4a64f543 1622 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1623 Shall we set a breakpoint at the LMA? */
1624 if (!overlay_events_enabled)
1625 {
1626 /* Yes -- overlay event support is not active,
1627 so we must try to set a breakpoint at the LMA.
1628 This will not work for a hardware breakpoint. */
35df4500 1629 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1630 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1631 bl->owner->number);
879bfdc2
DJ
1632 else
1633 {
35df4500
TJB
1634 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1635 bl->section);
879bfdc2 1636 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1637 bl->overlay_target_info = bl->target_info;
1638 bl->overlay_target_info.placed_address = addr;
1639 val = target_insert_breakpoint (bl->gdbarch,
1640 &bl->overlay_target_info);
879bfdc2 1641 if (val != 0)
99361f52 1642 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1643 "Overlay breakpoint %d "
1644 "failed: in ROM?\n",
35df4500 1645 bl->owner->number);
879bfdc2
DJ
1646 }
1647 }
1648 /* Shall we set a breakpoint at the VMA? */
35df4500 1649 if (section_is_mapped (bl->section))
879bfdc2
DJ
1650 {
1651 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1652 if (bl->loc_type == bp_loc_hardware_breakpoint)
1653 val = target_insert_hw_breakpoint (bl->gdbarch,
1654 &bl->target_info);
879bfdc2 1655 else
35df4500
TJB
1656 val = target_insert_breakpoint (bl->gdbarch,
1657 &bl->target_info);
879bfdc2
DJ
1658 }
1659 else
1660 {
1661 /* No. This breakpoint will not be inserted.
1662 No error, but do not mark the bp as 'inserted'. */
1663 return 0;
1664 }
1665 }
1666
1667 if (val)
1668 {
1669 /* Can't set the breakpoint. */
35df4500 1670 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1671 {
4a64f543 1672 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1673 val = 0;
35df4500 1674 bl->shlib_disabled = 1;
879bfdc2
DJ
1675 if (!*disabled_breaks)
1676 {
1677 fprintf_unfiltered (tmp_error_stream,
1678 "Cannot insert breakpoint %d.\n",
35df4500 1679 bl->owner->number);
879bfdc2 1680 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1681 "Temporarily disabling shared "
1682 "library breakpoints:\n");
879bfdc2
DJ
1683 }
1684 *disabled_breaks = 1;
1685 fprintf_unfiltered (tmp_error_stream,
35df4500 1686 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1687 }
1688 else
879bfdc2 1689 {
35df4500 1690 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1691 {
1692 *hw_breakpoint_error = 1;
3e43a32a
MS
1693 fprintf_unfiltered (tmp_error_stream,
1694 "Cannot insert hardware "
1695 "breakpoint %d.\n",
35df4500 1696 bl->owner->number);
879bfdc2
DJ
1697 }
1698 else
1699 {
1700 fprintf_unfiltered (tmp_error_stream,
1701 "Cannot insert breakpoint %d.\n",
35df4500 1702 bl->owner->number);
879bfdc2
DJ
1703 fprintf_filtered (tmp_error_stream,
1704 "Error accessing memory address ");
35df4500 1705 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1706 tmp_error_stream);
879bfdc2
DJ
1707 fprintf_filtered (tmp_error_stream, ": %s.\n",
1708 safe_strerror (val));
1709 }
1710
1711 }
1712 }
1713 else
35df4500 1714 bl->inserted = 1;
879bfdc2
DJ
1715
1716 return val;
1717 }
1718
35df4500 1719 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1720 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1721 watchpoints. It's not clear that it's necessary... */
35df4500 1722 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1723 {
77b06cd7
TJB
1724 gdb_assert (bl->owner->ops != NULL
1725 && bl->owner->ops->insert_location != NULL);
1726
1727 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1728
1729 /* If trying to set a read-watchpoint, and it turns out it's not
1730 supported, try emulating one with an access watchpoint. */
35df4500 1731 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1732 {
1733 struct bp_location *loc, **loc_temp;
1734
1735 /* But don't try to insert it, if there's already another
1736 hw_access location that would be considered a duplicate
1737 of this one. */
1738 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1739 if (loc != bl
85d721b8 1740 && loc->watchpoint_type == hw_access
35df4500 1741 && watchpoint_locations_match (bl, loc))
85d721b8 1742 {
35df4500
TJB
1743 bl->duplicate = 1;
1744 bl->inserted = 1;
1745 bl->target_info = loc->target_info;
1746 bl->watchpoint_type = hw_access;
85d721b8
PA
1747 val = 0;
1748 break;
1749 }
1750
1751 if (val == 1)
1752 {
77b06cd7
TJB
1753 bl->watchpoint_type = hw_access;
1754 val = bl->owner->ops->insert_location (bl);
1755
1756 if (val)
1757 /* Back to the original value. */
1758 bl->watchpoint_type = hw_read;
85d721b8
PA
1759 }
1760 }
1761
35df4500 1762 bl->inserted = (val == 0);
879bfdc2
DJ
1763 }
1764
35df4500 1765 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1766 {
77b06cd7
TJB
1767 gdb_assert (bl->owner->ops != NULL
1768 && bl->owner->ops->insert_location != NULL);
1769
1770 val = bl->owner->ops->insert_location (bl);
1771 if (val)
1772 {
1773 bl->owner->enable_state = bp_disabled;
1774
1775 if (val == 1)
1776 warning (_("\
1777Error inserting catchpoint %d: Your system does not support this type\n\
1778of catchpoint."), bl->owner->number);
1779 else
1780 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1781 }
1782
1783 bl->inserted = (val == 0);
1640b821
DJ
1784
1785 /* We've already printed an error message if there was a problem
1786 inserting this catchpoint, and we've disabled the catchpoint,
1787 so just return success. */
1788 return 0;
879bfdc2
DJ
1789 }
1790
1791 return 0;
1792}
1793
6c95b8df
PA
1794/* This function is called when program space PSPACE is about to be
1795 deleted. It takes care of updating breakpoints to not reference
1796 PSPACE anymore. */
1797
1798void
1799breakpoint_program_space_exit (struct program_space *pspace)
1800{
1801 struct breakpoint *b, *b_temp;
876fa593 1802 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1803
1804 /* Remove any breakpoint that was set through this program space. */
1805 ALL_BREAKPOINTS_SAFE (b, b_temp)
1806 {
1807 if (b->pspace == pspace)
1808 delete_breakpoint (b);
1809 }
1810
1811 /* Breakpoints set through other program spaces could have locations
1812 bound to PSPACE as well. Remove those. */
876fa593 1813 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1814 {
1815 struct bp_location *tmp;
1816
1817 if (loc->pspace == pspace)
1818 {
2bdf28a0 1819 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1820 if (loc->owner->loc == loc)
1821 loc->owner->loc = loc->next;
1822 else
1823 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1824 if (tmp->next == loc)
1825 {
1826 tmp->next = loc->next;
1827 break;
1828 }
1829 }
1830 }
1831
1832 /* Now update the global location list to permanently delete the
1833 removed locations above. */
1834 update_global_location_list (0);
1835}
1836
74960c60
VP
1837/* Make sure all breakpoints are inserted in inferior.
1838 Throws exception on any error.
1839 A breakpoint that is already inserted won't be inserted
1840 again, so calling this function twice is safe. */
1841void
1842insert_breakpoints (void)
1843{
1844 struct breakpoint *bpt;
1845
1846 ALL_BREAKPOINTS (bpt)
1847 if (is_hardware_watchpoint (bpt))
4a64f543 1848 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1849
b60e7edf 1850 update_global_location_list (1);
74960c60 1851
c35b1492
PA
1852 /* update_global_location_list does not insert breakpoints when
1853 always_inserted_mode is not enabled. Explicitly insert them
1854 now. */
1855 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1856 insert_breakpoint_locations ();
1857}
1858
c906108c
SS
1859/* insert_breakpoints is used when starting or continuing the program.
1860 remove_breakpoints is used when the program stops.
1861 Both return zero if successful,
1862 or an `errno' value if could not write the inferior. */
1863
74960c60
VP
1864static void
1865insert_breakpoint_locations (void)
c906108c 1866{
a5606eee 1867 struct breakpoint *bpt;
35df4500 1868 struct bp_location *bl, **blp_tmp;
e236ba44 1869 int error = 0;
c906108c
SS
1870 int val = 0;
1871 int disabled_breaks = 0;
81d0cc19 1872 int hw_breakpoint_error = 0;
c906108c 1873
81d0cc19 1874 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1875 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1876
81d0cc19
GS
1877 /* Explicitly mark the warning -- this will only be printed if
1878 there was an error. */
1879 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1880
1881 save_current_space_and_thread ();
1882
35df4500 1883 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1884 {
35df4500 1885 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1886 continue;
1887
4a64f543
MS
1888 /* There is no point inserting thread-specific breakpoints if
1889 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1890 has BL->OWNER always non-NULL. */
35df4500
TJB
1891 if (bl->owner->thread != -1
1892 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1893 continue;
1894
35df4500 1895 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1896
1897 /* For targets that support global breakpoints, there's no need
1898 to select an inferior to insert breakpoint to. In fact, even
1899 if we aren't attached to any process yet, we should still
1900 insert breakpoints. */
1901 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1902 && ptid_equal (inferior_ptid, null_ptid))
1903 continue;
1904
35df4500 1905 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1906 &hw_breakpoint_error);
1907 if (val)
e236ba44 1908 error = val;
879bfdc2 1909 }
c906108c 1910
4a64f543
MS
1911 /* If we failed to insert all locations of a watchpoint, remove
1912 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1913 ALL_BREAKPOINTS (bpt)
1914 {
1915 int some_failed = 0;
1916 struct bp_location *loc;
1917
1918 if (!is_hardware_watchpoint (bpt))
1919 continue;
1920
d6b74ac4 1921 if (!breakpoint_enabled (bpt))
a5606eee 1922 continue;
74960c60
VP
1923
1924 if (bpt->disposition == disp_del_at_next_stop)
1925 continue;
a5606eee
VP
1926
1927 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1928 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1929 {
1930 some_failed = 1;
1931 break;
1932 }
1933 if (some_failed)
1934 {
1935 for (loc = bpt->loc; loc; loc = loc->next)
1936 if (loc->inserted)
1937 remove_breakpoint (loc, mark_uninserted);
1938
1939 hw_breakpoint_error = 1;
1940 fprintf_unfiltered (tmp_error_stream,
1941 "Could not insert hardware watchpoint %d.\n",
1942 bpt->number);
1943 error = -1;
1944 }
1945 }
1946
e236ba44 1947 if (error)
81d0cc19
GS
1948 {
1949 /* If a hardware breakpoint or watchpoint was inserted, add a
1950 message about possibly exhausted resources. */
879bfdc2 1951 if (hw_breakpoint_error)
81d0cc19 1952 {
c6510018
MS
1953 fprintf_unfiltered (tmp_error_stream,
1954 "Could not insert hardware breakpoints:\n\
1955You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1956 }
81d0cc19
GS
1957 target_terminal_ours_for_output ();
1958 error_stream (tmp_error_stream);
1959 }
f7545552
TT
1960
1961 do_cleanups (cleanups);
c906108c
SS
1962}
1963
c906108c 1964int
fba45db2 1965remove_breakpoints (void)
c906108c 1966{
35df4500 1967 struct bp_location *bl, **blp_tmp;
3a1bae8e 1968 int val = 0;
c906108c 1969
35df4500 1970 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1971 {
35df4500
TJB
1972 if (bl->inserted)
1973 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1974 }
3a1bae8e 1975 return val;
c906108c
SS
1976}
1977
6c95b8df
PA
1978/* Remove breakpoints of process PID. */
1979
1980int
1981remove_breakpoints_pid (int pid)
1982{
35df4500 1983 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1984 int val;
1985 struct inferior *inf = find_inferior_pid (pid);
1986
35df4500 1987 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 1988 {
35df4500 1989 if (bl->pspace != inf->pspace)
6c95b8df
PA
1990 continue;
1991
35df4500 1992 if (bl->inserted)
6c95b8df 1993 {
35df4500 1994 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
1995 if (val != 0)
1996 return val;
1997 }
1998 }
1999 return 0;
2000}
2001
692590c1 2002int
80ce1ecb 2003remove_hw_watchpoints (void)
692590c1 2004{
35df4500 2005 struct bp_location *bl, **blp_tmp;
3a1bae8e 2006 int val = 0;
692590c1 2007
35df4500 2008 ALL_BP_LOCATIONS (bl, blp_tmp)
692590c1 2009 {
35df4500
TJB
2010 if (bl->inserted && bl->loc_type == bp_loc_hardware_watchpoint)
2011 val |= remove_breakpoint (bl, mark_uninserted);
692590c1 2012 }
3a1bae8e 2013 return val;
692590c1
MS
2014}
2015
c906108c 2016int
fba45db2 2017reattach_breakpoints (int pid)
c906108c 2018{
6c95b8df 2019 struct cleanup *old_chain;
35df4500 2020 struct bp_location *bl, **blp_tmp;
c906108c 2021 int val;
86b887df 2022 struct ui_file *tmp_error_stream;
fa3a767f 2023 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2024 struct inferior *inf;
2025 struct thread_info *tp;
2026
2027 tp = any_live_thread_of_process (pid);
2028 if (tp == NULL)
2029 return 1;
2030
2031 inf = find_inferior_pid (pid);
2032 old_chain = save_inferior_ptid ();
2033
2034 inferior_ptid = tp->ptid;
a4954f26 2035
86b887df 2036 tmp_error_stream = mem_fileopen ();
a4954f26 2037 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2038
35df4500 2039 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2040 {
35df4500 2041 if (bl->pspace != inf->pspace)
6c95b8df
PA
2042 continue;
2043
35df4500 2044 if (bl->inserted)
c5aa993b 2045 {
35df4500
TJB
2046 bl->inserted = 0;
2047 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2048 if (val != 0)
2049 {
ce696e05 2050 do_cleanups (old_chain);
c5aa993b
JM
2051 return val;
2052 }
2053 }
2054 }
ce696e05 2055 do_cleanups (old_chain);
c906108c
SS
2056 return 0;
2057}
2058
e58b0e63
PA
2059static int internal_breakpoint_number = -1;
2060
84f4c1fe
PM
2061/* Set the breakpoint number of B, depending on the value of INTERNAL.
2062 If INTERNAL is non-zero, the breakpoint number will be populated
2063 from internal_breakpoint_number and that variable decremented.
2064 Otherwis the breakpoint number will be populated from
2065 breakpoint_count and that value incremented. Internal breakpoints
2066 do not set the internal var bpnum. */
2067static void
2068set_breakpoint_number (int internal, struct breakpoint *b)
2069{
2070 if (internal)
2071 b->number = internal_breakpoint_number--;
2072 else
2073 {
2074 set_breakpoint_count (breakpoint_count + 1);
2075 b->number = breakpoint_count;
2076 }
2077}
2078
e62c965a 2079static struct breakpoint *
a6d9a66e
UW
2080create_internal_breakpoint (struct gdbarch *gdbarch,
2081 CORE_ADDR address, enum bptype type)
e62c965a 2082{
e62c965a
PP
2083 struct symtab_and_line sal;
2084 struct breakpoint *b;
2085
4a64f543 2086 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2087
2088 sal.pc = address;
2089 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2090 sal.pspace = current_program_space;
e62c965a 2091
a6d9a66e 2092 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2093 b->number = internal_breakpoint_number--;
2094 b->disposition = disp_donttouch;
2095
2096 return b;
2097}
2098
17450429
PP
2099static const char *const longjmp_names[] =
2100 {
2101 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2102 };
2103#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2104
2105/* Per-objfile data private to breakpoint.c. */
2106struct breakpoint_objfile_data
2107{
2108 /* Minimal symbol for "_ovly_debug_event" (if any). */
2109 struct minimal_symbol *overlay_msym;
2110
2111 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2112 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2113
2114 /* Minimal symbol for "std::terminate()" (if any). */
2115 struct minimal_symbol *terminate_msym;
2116
2117 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2118 struct minimal_symbol *exception_msym;
2119};
2120
2121static const struct objfile_data *breakpoint_objfile_key;
2122
2123/* Minimal symbol not found sentinel. */
2124static struct minimal_symbol msym_not_found;
2125
2126/* Returns TRUE if MSYM point to the "not found" sentinel. */
2127
2128static int
2129msym_not_found_p (const struct minimal_symbol *msym)
2130{
2131 return msym == &msym_not_found;
2132}
2133
2134/* Return per-objfile data needed by breakpoint.c.
2135 Allocate the data if necessary. */
2136
2137static struct breakpoint_objfile_data *
2138get_breakpoint_objfile_data (struct objfile *objfile)
2139{
2140 struct breakpoint_objfile_data *bp_objfile_data;
2141
2142 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2143 if (bp_objfile_data == NULL)
2144 {
2145 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2146 sizeof (*bp_objfile_data));
2147
2148 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2149 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2150 }
2151 return bp_objfile_data;
2152}
2153
e62c965a 2154static void
af02033e 2155create_overlay_event_breakpoint (void)
e62c965a 2156{
69de3c6a 2157 struct objfile *objfile;
af02033e 2158 const char *const func_name = "_ovly_debug_event";
e62c965a 2159
69de3c6a
PP
2160 ALL_OBJFILES (objfile)
2161 {
2162 struct breakpoint *b;
17450429
PP
2163 struct breakpoint_objfile_data *bp_objfile_data;
2164 CORE_ADDR addr;
69de3c6a 2165
17450429
PP
2166 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2167
2168 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2169 continue;
2170
2171 if (bp_objfile_data->overlay_msym == NULL)
2172 {
2173 struct minimal_symbol *m;
2174
2175 m = lookup_minimal_symbol_text (func_name, objfile);
2176 if (m == NULL)
2177 {
2178 /* Avoid future lookups in this objfile. */
2179 bp_objfile_data->overlay_msym = &msym_not_found;
2180 continue;
2181 }
2182 bp_objfile_data->overlay_msym = m;
2183 }
e62c965a 2184
17450429
PP
2185 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2186 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2187 bp_overlay_event);
2188 b->addr_string = xstrdup (func_name);
e62c965a 2189
69de3c6a
PP
2190 if (overlay_debugging == ovly_auto)
2191 {
2192 b->enable_state = bp_enabled;
2193 overlay_events_enabled = 1;
2194 }
2195 else
2196 {
2197 b->enable_state = bp_disabled;
2198 overlay_events_enabled = 0;
2199 }
e62c965a
PP
2200 }
2201 update_global_location_list (1);
2202}
2203
0fd8e87f 2204static void
af02033e 2205create_longjmp_master_breakpoint (void)
0fd8e87f 2206{
6c95b8df 2207 struct program_space *pspace;
6c95b8df
PA
2208 struct cleanup *old_chain;
2209
2210 old_chain = save_current_program_space ();
0fd8e87f 2211
6c95b8df 2212 ALL_PSPACES (pspace)
af02033e
PP
2213 {
2214 struct objfile *objfile;
2215
2216 set_current_program_space (pspace);
2217
2218 ALL_OBJFILES (objfile)
0fd8e87f 2219 {
af02033e
PP
2220 int i;
2221 struct gdbarch *gdbarch;
17450429 2222 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2223
af02033e
PP
2224 gdbarch = get_objfile_arch (objfile);
2225 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2226 continue;
2227
17450429
PP
2228 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2229
2230 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2231 {
2232 struct breakpoint *b;
af02033e 2233 const char *func_name;
17450429 2234 CORE_ADDR addr;
6c95b8df 2235
17450429 2236 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2237 continue;
0fd8e87f 2238
17450429
PP
2239 func_name = longjmp_names[i];
2240 if (bp_objfile_data->longjmp_msym[i] == NULL)
2241 {
2242 struct minimal_symbol *m;
2243
2244 m = lookup_minimal_symbol_text (func_name, objfile);
2245 if (m == NULL)
2246 {
2247 /* Prevent future lookups in this objfile. */
2248 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2249 continue;
2250 }
2251 bp_objfile_data->longjmp_msym[i] = m;
2252 }
2253
2254 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2255 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2256 b->addr_string = xstrdup (func_name);
2257 b->enable_state = bp_disabled;
2258 }
0fd8e87f 2259 }
af02033e 2260 }
0fd8e87f 2261 update_global_location_list (1);
6c95b8df
PA
2262
2263 do_cleanups (old_chain);
0fd8e87f
UW
2264}
2265
af02033e 2266/* Create a master std::terminate breakpoint. */
aa7d318d 2267static void
af02033e 2268create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2269{
2270 struct program_space *pspace;
aa7d318d 2271 struct cleanup *old_chain;
af02033e 2272 const char *const func_name = "std::terminate()";
aa7d318d
TT
2273
2274 old_chain = save_current_program_space ();
2275
2276 ALL_PSPACES (pspace)
17450429
PP
2277 {
2278 struct objfile *objfile;
2279 CORE_ADDR addr;
2280
2281 set_current_program_space (pspace);
2282
aa7d318d
TT
2283 ALL_OBJFILES (objfile)
2284 {
2285 struct breakpoint *b;
17450429 2286 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2287
17450429 2288 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2289
17450429
PP
2290 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2291 continue;
2292
2293 if (bp_objfile_data->terminate_msym == NULL)
2294 {
2295 struct minimal_symbol *m;
2296
2297 m = lookup_minimal_symbol (func_name, NULL, objfile);
2298 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2299 && MSYMBOL_TYPE (m) != mst_file_text))
2300 {
2301 /* Prevent future lookups in this objfile. */
2302 bp_objfile_data->terminate_msym = &msym_not_found;
2303 continue;
2304 }
2305 bp_objfile_data->terminate_msym = m;
2306 }
aa7d318d 2307
17450429
PP
2308 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2309 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2310 bp_std_terminate_master);
2311 b->addr_string = xstrdup (func_name);
2312 b->enable_state = bp_disabled;
2313 }
17450429
PP
2314 }
2315
aa7d318d
TT
2316 update_global_location_list (1);
2317
2318 do_cleanups (old_chain);
2319}
2320
186c406b
TT
2321/* Install a master breakpoint on the unwinder's debug hook. */
2322
2323void
2324create_exception_master_breakpoint (void)
2325{
2326 struct objfile *objfile;
17450429 2327 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2328
2329 ALL_OBJFILES (objfile)
2330 {
17450429
PP
2331 struct breakpoint *b;
2332 struct gdbarch *gdbarch;
2333 struct breakpoint_objfile_data *bp_objfile_data;
2334 CORE_ADDR addr;
2335
2336 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2337
2338 if (msym_not_found_p (bp_objfile_data->exception_msym))
2339 continue;
2340
2341 gdbarch = get_objfile_arch (objfile);
186c406b 2342
17450429 2343 if (bp_objfile_data->exception_msym == NULL)
186c406b 2344 {
17450429 2345 struct minimal_symbol *debug_hook;
186c406b 2346
17450429
PP
2347 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2348 if (debug_hook == NULL)
2349 {
2350 bp_objfile_data->exception_msym = &msym_not_found;
2351 continue;
2352 }
2353
2354 bp_objfile_data->exception_msym = debug_hook;
186c406b 2355 }
17450429
PP
2356
2357 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2358 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2359 &current_target);
2360 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2361 b->addr_string = xstrdup (func_name);
2362 b->enable_state = bp_disabled;
186c406b
TT
2363 }
2364
2365 update_global_location_list (1);
2366}
2367
c906108c 2368void
fba45db2 2369update_breakpoints_after_exec (void)
c906108c 2370{
35df4500 2371 struct breakpoint *b, *b_tmp;
876fa593 2372 struct bp_location *bploc, **bplocp_tmp;
c906108c 2373
25b22b0a
PA
2374 /* We're about to delete breakpoints from GDB's lists. If the
2375 INSERTED flag is true, GDB will try to lift the breakpoints by
2376 writing the breakpoints' "shadow contents" back into memory. The
2377 "shadow contents" are NOT valid after an exec, so GDB should not
2378 do that. Instead, the target is responsible from marking
2379 breakpoints out as soon as it detects an exec. We don't do that
2380 here instead, because there may be other attempts to delete
2381 breakpoints after detecting an exec and before reaching here. */
876fa593 2382 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2383 if (bploc->pspace == current_program_space)
2384 gdb_assert (!bploc->inserted);
c906108c 2385
35df4500 2386 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2387 {
6c95b8df
PA
2388 if (b->pspace != current_program_space)
2389 continue;
2390
4a64f543 2391 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2392 if (b->type == bp_shlib_event)
2393 {
2394 delete_breakpoint (b);
2395 continue;
2396 }
c906108c 2397
4a64f543 2398 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2399 if (b->type == bp_jit_event)
2400 {
2401 delete_breakpoint (b);
2402 continue;
2403 }
2404
1900040c 2405 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2406 as must overlay event and longjmp master breakpoints. */
2407 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2408 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2409 || b->type == bp_exception_master)
c4093a6a
JM
2410 {
2411 delete_breakpoint (b);
2412 continue;
2413 }
2414
4a64f543 2415 /* Step-resume breakpoints are meaningless after an exec(). */
c5aa993b
JM
2416 if (b->type == bp_step_resume)
2417 {
2418 delete_breakpoint (b);
2419 continue;
2420 }
2421
611c83ae
PA
2422 /* Longjmp and longjmp-resume breakpoints are also meaningless
2423 after an exec. */
186c406b
TT
2424 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2425 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2426 {
2427 delete_breakpoint (b);
2428 continue;
2429 }
2430
ce78b96d
JB
2431 if (b->type == bp_catchpoint)
2432 {
2433 /* For now, none of the bp_catchpoint breakpoints need to
2434 do anything at this point. In the future, if some of
2435 the catchpoints need to something, we will need to add
2436 a new method, and call this method from here. */
2437 continue;
2438 }
2439
c5aa993b
JM
2440 /* bp_finish is a special case. The only way we ought to be able
2441 to see one of these when an exec() has happened, is if the user
2442 caught a vfork, and then said "finish". Ordinarily a finish just
2443 carries them to the call-site of the current callee, by setting
2444 a temporary bp there and resuming. But in this case, the finish
2445 will carry them entirely through the vfork & exec.
2446
2447 We don't want to allow a bp_finish to remain inserted now. But
2448 we can't safely delete it, 'cause finish_command has a handle to
2449 the bp on a bpstat, and will later want to delete it. There's a
2450 chance (and I've seen it happen) that if we delete the bp_finish
2451 here, that its storage will get reused by the time finish_command
2452 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2453 We really must allow finish_command to delete a bp_finish.
2454
53a5351d
JM
2455 In the absense of a general solution for the "how do we know
2456 it's safe to delete something others may have handles to?"
2457 problem, what we'll do here is just uninsert the bp_finish, and
2458 let finish_command delete it.
2459
2460 (We know the bp_finish is "doomed" in the sense that it's
2461 momentary, and will be deleted as soon as finish_command sees
2462 the inferior stopped. So it doesn't matter that the bp's
2463 address is probably bogus in the new a.out, unlike e.g., the
2464 solib breakpoints.) */
c5aa993b 2465
c5aa993b
JM
2466 if (b->type == bp_finish)
2467 {
2468 continue;
2469 }
2470
2471 /* Without a symbolic address, we have little hope of the
2472 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2473 a.out. */
c5aa993b
JM
2474 if (b->addr_string == NULL)
2475 {
2476 delete_breakpoint (b);
2477 continue;
2478 }
c5aa993b 2479 }
1900040c 2480 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2481 create_overlay_event_breakpoint ();
2482 create_longjmp_master_breakpoint ();
2483 create_std_terminate_master_breakpoint ();
186c406b 2484 create_exception_master_breakpoint ();
c906108c
SS
2485}
2486
2487int
fba45db2 2488detach_breakpoints (int pid)
c906108c 2489{
35df4500 2490 struct bp_location *bl, **blp_tmp;
3a1bae8e 2491 int val = 0;
ce696e05 2492 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2493 struct inferior *inf = current_inferior ();
c5aa993b 2494
39f77062 2495 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2496 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2497
6c95b8df 2498 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2499 inferior_ptid = pid_to_ptid (pid);
35df4500 2500 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2501 {
35df4500 2502 if (bl->pspace != inf->pspace)
6c95b8df
PA
2503 continue;
2504
35df4500
TJB
2505 if (bl->inserted)
2506 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2507 }
d03285ec
UW
2508
2509 /* Detach single-step breakpoints as well. */
2510 detach_single_step_breakpoints ();
2511
ce696e05 2512 do_cleanups (old_chain);
3a1bae8e 2513 return val;
c906108c
SS
2514}
2515
35df4500 2516/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2517 Note that this is used to detach breakpoints from a child fork.
2518 When we get here, the child isn't in the inferior list, and neither
2519 do we have objects to represent its address space --- we should
35df4500 2520 *not* look at bl->pspace->aspace here. */
6c95b8df 2521
c906108c 2522static int
35df4500 2523remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2524{
2525 int val;
c5aa993b 2526
35df4500
TJB
2527 /* BL is never in moribund_locations by our callers. */
2528 gdb_assert (bl->owner != NULL);
2bdf28a0 2529
35df4500 2530 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2531 /* Permanent breakpoints cannot be inserted or removed. */
2532 return 0;
2533
74960c60
VP
2534 /* The type of none suggests that owner is actually deleted.
2535 This should not ever happen. */
35df4500 2536 gdb_assert (bl->owner->type != bp_none);
0bde7532 2537
35df4500
TJB
2538 if (bl->loc_type == bp_loc_software_breakpoint
2539 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2540 {
c02f5703
MS
2541 /* "Normal" instruction breakpoint: either the standard
2542 trap-instruction bp (bp_breakpoint), or a
2543 bp_hardware_breakpoint. */
2544
2545 /* First check to see if we have to handle an overlay. */
2546 if (overlay_debugging == ovly_off
35df4500
TJB
2547 || bl->section == NULL
2548 || !(section_is_overlay (bl->section)))
c02f5703
MS
2549 {
2550 /* No overlay handling: just remove the breakpoint. */
2551
35df4500
TJB
2552 if (bl->loc_type == bp_loc_hardware_breakpoint)
2553 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2554 else
35df4500 2555 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2556 }
c906108c
SS
2557 else
2558 {
4a64f543 2559 /* This breakpoint is in an overlay section.
c02f5703
MS
2560 Did we set a breakpoint at the LMA? */
2561 if (!overlay_events_enabled)
2562 {
2563 /* Yes -- overlay event support is not active, so we
2564 should have set a breakpoint at the LMA. Remove it.
2565 */
c02f5703
MS
2566 /* Ignore any failures: if the LMA is in ROM, we will
2567 have already warned when we failed to insert it. */
35df4500
TJB
2568 if (bl->loc_type == bp_loc_hardware_breakpoint)
2569 target_remove_hw_breakpoint (bl->gdbarch,
2570 &bl->overlay_target_info);
c02f5703 2571 else
35df4500
TJB
2572 target_remove_breakpoint (bl->gdbarch,
2573 &bl->overlay_target_info);
c02f5703
MS
2574 }
2575 /* Did we set a breakpoint at the VMA?
2576 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2577 if (bl->inserted)
c906108c 2578 {
c02f5703
MS
2579 /* Yes -- remove it. Previously we did not bother to
2580 remove the breakpoint if the section had been
2581 unmapped, but let's not rely on that being safe. We
2582 don't know what the overlay manager might do. */
35df4500
TJB
2583 if (bl->loc_type == bp_loc_hardware_breakpoint)
2584 val = target_remove_hw_breakpoint (bl->gdbarch,
2585 &bl->target_info);
aa67235e
UW
2586
2587 /* However, we should remove *software* breakpoints only
2588 if the section is still mapped, or else we overwrite
2589 wrong code with the saved shadow contents. */
35df4500
TJB
2590 else if (section_is_mapped (bl->section))
2591 val = target_remove_breakpoint (bl->gdbarch,
2592 &bl->target_info);
aa67235e
UW
2593 else
2594 val = 0;
c906108c 2595 }
c02f5703
MS
2596 else
2597 {
2598 /* No -- not inserted, so no need to remove. No error. */
2599 val = 0;
2600 }
c906108c 2601 }
879d1e6b
UW
2602
2603 /* In some cases, we might not be able to remove a breakpoint
2604 in a shared library that has already been removed, but we
2605 have not yet processed the shlib unload event. */
35df4500 2606 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2607 val = 0;
2608
c906108c
SS
2609 if (val)
2610 return val;
35df4500 2611 bl->inserted = (is == mark_inserted);
c906108c 2612 }
35df4500 2613 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2614 {
77b06cd7
TJB
2615 gdb_assert (bl->owner->ops != NULL
2616 && bl->owner->ops->remove_location != NULL);
2617
35df4500 2618 bl->inserted = (is == mark_inserted);
77b06cd7 2619 bl->owner->ops->remove_location (bl);
2e70b7b9 2620
c906108c 2621 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2622 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2623 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2624 bl->owner->number);
c906108c 2625 }
35df4500
TJB
2626 else if (bl->owner->type == bp_catchpoint
2627 && breakpoint_enabled (bl->owner)
2628 && !bl->duplicate)
ce78b96d 2629 {
77b06cd7
TJB
2630 gdb_assert (bl->owner->ops != NULL
2631 && bl->owner->ops->remove_location != NULL);
ce78b96d 2632
77b06cd7 2633 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2634 if (val)
2635 return val;
77b06cd7 2636
35df4500 2637 bl->inserted = (is == mark_inserted);
ce78b96d 2638 }
c906108c
SS
2639
2640 return 0;
2641}
2642
6c95b8df 2643static int
35df4500 2644remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2645{
2646 int ret;
2647 struct cleanup *old_chain;
2648
35df4500
TJB
2649 /* BL is never in moribund_locations by our callers. */
2650 gdb_assert (bl->owner != NULL);
2bdf28a0 2651
35df4500 2652 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2653 /* Permanent breakpoints cannot be inserted or removed. */
2654 return 0;
2655
2656 /* The type of none suggests that owner is actually deleted.
2657 This should not ever happen. */
35df4500 2658 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2659
2660 old_chain = save_current_space_and_thread ();
2661
35df4500 2662 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2663
35df4500 2664 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2665
2666 do_cleanups (old_chain);
2667 return ret;
2668}
2669
c906108c
SS
2670/* Clear the "inserted" flag in all breakpoints. */
2671
25b22b0a 2672void
fba45db2 2673mark_breakpoints_out (void)
c906108c 2674{
35df4500 2675 struct bp_location *bl, **blp_tmp;
c906108c 2676
35df4500
TJB
2677 ALL_BP_LOCATIONS (bl, blp_tmp)
2678 if (bl->pspace == current_program_space)
2679 bl->inserted = 0;
c906108c
SS
2680}
2681
53a5351d
JM
2682/* Clear the "inserted" flag in all breakpoints and delete any
2683 breakpoints which should go away between runs of the program.
c906108c
SS
2684
2685 Plus other such housekeeping that has to be done for breakpoints
2686 between runs.
2687
53a5351d
JM
2688 Note: this function gets called at the end of a run (by
2689 generic_mourn_inferior) and when a run begins (by
4a64f543 2690 init_wait_for_inferior). */
c906108c
SS
2691
2692
2693
2694void
fba45db2 2695breakpoint_init_inferior (enum inf_context context)
c906108c 2696{
35df4500
TJB
2697 struct breakpoint *b, *b_tmp;
2698 struct bp_location *bl, **blp_tmp;
1c5cfe86 2699 int ix;
6c95b8df 2700 struct program_space *pspace = current_program_space;
c906108c 2701
50c71eaf
PA
2702 /* If breakpoint locations are shared across processes, then there's
2703 nothing to do. */
2567c7d9 2704 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2705 return;
2706
35df4500 2707 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2708 {
35df4500
TJB
2709 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2710 if (bl->pspace == pspace
2711 && bl->owner->enable_state != bp_permanent)
2712 bl->inserted = 0;
6c95b8df 2713 }
075f6582 2714
35df4500 2715 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2716 {
6c95b8df
PA
2717 if (b->loc && b->loc->pspace != pspace)
2718 continue;
2719
c5aa993b
JM
2720 switch (b->type)
2721 {
2722 case bp_call_dummy:
c906108c 2723
c5aa993b 2724 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2725 cause problems when the inferior is rerun, so we better get
2726 rid of it. */
2727
2728 case bp_watchpoint_scope:
2729
2730 /* Also get rid of scope breakpoints. */
2731
2732 case bp_shlib_event:
2733
2734 /* Also remove solib event breakpoints. Their addresses may
2735 have changed since the last time we ran the program.
2736 Actually we may now be debugging against different target;
2737 and so the solib backend that installed this breakpoint may
2738 not be used in by the target. E.g.,
2739
2740 (gdb) file prog-linux
2741 (gdb) run # native linux target
2742 ...
2743 (gdb) kill
2744 (gdb) file prog-win.exe
2745 (gdb) tar rem :9999 # remote Windows gdbserver.
2746 */
c906108c 2747
c5aa993b
JM
2748 delete_breakpoint (b);
2749 break;
c906108c 2750
c5aa993b
JM
2751 case bp_watchpoint:
2752 case bp_hardware_watchpoint:
2753 case bp_read_watchpoint:
2754 case bp_access_watchpoint:
c906108c 2755
c5aa993b
JM
2756 /* Likewise for watchpoints on local expressions. */
2757 if (b->exp_valid_block != NULL)
2758 delete_breakpoint (b);
967af18d 2759 else if (context == inf_starting)
c860120c 2760 {
4a64f543
MS
2761 /* Reset val field to force reread of starting value in
2762 insert_breakpoints. */
c860120c
PM
2763 if (b->val)
2764 value_free (b->val);
2765 b->val = NULL;
fa4727a6 2766 b->val_valid = 0;
c860120c 2767 }
c5aa993b
JM
2768 break;
2769 default:
c5aa993b
JM
2770 break;
2771 }
2772 }
1c5cfe86
PA
2773
2774 /* Get rid of the moribund locations. */
35df4500
TJB
2775 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2776 decref_bp_location (&bl);
1c5cfe86 2777 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2778}
2779
6c95b8df
PA
2780/* These functions concern about actual breakpoints inserted in the
2781 target --- to e.g. check if we need to do decr_pc adjustment or if
2782 we need to hop over the bkpt --- so we check for address space
2783 match, not program space. */
2784
c2c6d25f
JM
2785/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2786 exists at PC. It returns ordinary_breakpoint_here if it's an
2787 ordinary breakpoint, or permanent_breakpoint_here if it's a
2788 permanent breakpoint.
2789 - When continuing from a location with an ordinary breakpoint, we
2790 actually single step once before calling insert_breakpoints.
2791 - When continuing from a localion with a permanent breakpoint, we
2792 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2793 the target, to advance the PC past the breakpoint. */
c906108c 2794
c2c6d25f 2795enum breakpoint_here
6c95b8df 2796breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2797{
35df4500 2798 struct bp_location *bl, **blp_tmp;
c2c6d25f 2799 int any_breakpoint_here = 0;
c906108c 2800
35df4500 2801 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2802 {
35df4500
TJB
2803 if (bl->loc_type != bp_loc_software_breakpoint
2804 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2805 continue;
2806
f1310107 2807 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2808 if ((breakpoint_enabled (bl->owner)
2809 || bl->owner->enable_state == bp_permanent)
f1310107 2810 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2811 {
2812 if (overlay_debugging
35df4500
TJB
2813 && section_is_overlay (bl->section)
2814 && !section_is_mapped (bl->section))
075f6582 2815 continue; /* unmapped overlay -- can't be a match */
35df4500 2816 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2817 return permanent_breakpoint_here;
2818 else
2819 any_breakpoint_here = 1;
2820 }
2821 }
c906108c 2822
c2c6d25f 2823 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2824}
2825
1c5cfe86
PA
2826/* Return true if there's a moribund breakpoint at PC. */
2827
2828int
6c95b8df 2829moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2830{
2831 struct bp_location *loc;
2832 int ix;
2833
2834 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2835 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2836 return 1;
2837
2838 return 0;
2839}
c2c6d25f 2840
c36b740a 2841/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2842 inserted using regular breakpoint_chain / bp_location array
2843 mechanism. This does not check for single-step breakpoints, which
2844 are inserted and removed using direct target manipulation. */
c906108c
SS
2845
2846int
4a64f543
MS
2847regular_breakpoint_inserted_here_p (struct address_space *aspace,
2848 CORE_ADDR pc)
c906108c 2849{
35df4500 2850 struct bp_location *bl, **blp_tmp;
c906108c 2851
35df4500 2852 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2853 {
35df4500
TJB
2854 if (bl->loc_type != bp_loc_software_breakpoint
2855 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2856 continue;
2857
35df4500 2858 if (bl->inserted
f1310107 2859 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2860 {
2861 if (overlay_debugging
35df4500
TJB
2862 && section_is_overlay (bl->section)
2863 && !section_is_mapped (bl->section))
075f6582
DJ
2864 continue; /* unmapped overlay -- can't be a match */
2865 else
2866 return 1;
2867 }
c5aa993b 2868 }
c36b740a
VP
2869 return 0;
2870}
2871
2872/* Returns non-zero iff there's either regular breakpoint
2873 or a single step breakpoint inserted at PC. */
2874
2875int
6c95b8df 2876breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2877{
6c95b8df 2878 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2879 return 1;
c906108c 2880
6c95b8df 2881 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2882 return 1;
2883
c906108c
SS
2884 return 0;
2885}
2886
4fa8626c
DJ
2887/* This function returns non-zero iff there is a software breakpoint
2888 inserted at PC. */
2889
2890int
3e43a32a
MS
2891software_breakpoint_inserted_here_p (struct address_space *aspace,
2892 CORE_ADDR pc)
4fa8626c 2893{
35df4500 2894 struct bp_location *bl, **blp_tmp;
4fa8626c 2895
35df4500 2896 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2897 {
35df4500 2898 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2899 continue;
2900
35df4500
TJB
2901 if (bl->inserted
2902 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2903 aspace, pc))
4fa8626c
DJ
2904 {
2905 if (overlay_debugging
35df4500
TJB
2906 && section_is_overlay (bl->section)
2907 && !section_is_mapped (bl->section))
4fa8626c
DJ
2908 continue; /* unmapped overlay -- can't be a match */
2909 else
2910 return 1;
2911 }
2912 }
2913
1aafd4da 2914 /* Also check for software single-step breakpoints. */
6c95b8df 2915 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2916 return 1;
2917
4fa8626c
DJ
2918 return 0;
2919}
2920
9093389c
PA
2921int
2922hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2923 CORE_ADDR addr, ULONGEST len)
2924{
2925 struct breakpoint *bpt;
2926
2927 ALL_BREAKPOINTS (bpt)
2928 {
2929 struct bp_location *loc;
2930
2931 if (bpt->type != bp_hardware_watchpoint
2932 && bpt->type != bp_access_watchpoint)
2933 continue;
2934
2935 if (!breakpoint_enabled (bpt))
2936 continue;
2937
2938 for (loc = bpt->loc; loc; loc = loc->next)
2939 if (loc->pspace->aspace == aspace && loc->inserted)
2940 {
2941 CORE_ADDR l, h;
2942
2943 /* Check for intersection. */
2944 l = max (loc->address, addr);
2945 h = min (loc->address + loc->length, addr + len);
2946 if (l < h)
2947 return 1;
2948 }
2949 }
2950 return 0;
2951}
2952
075f6582
DJ
2953/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2954 PC is valid for process/thread PTID. */
c906108c
SS
2955
2956int
6c95b8df
PA
2957breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2958 ptid_t ptid)
c906108c 2959{
35df4500 2960 struct bp_location *bl, **blp_tmp;
4a306c9a 2961 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2962 int thread = -1;
4a306c9a 2963 int task = 0;
a6f1cd96 2964
35df4500 2965 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2966 {
35df4500
TJB
2967 if (bl->loc_type != bp_loc_software_breakpoint
2968 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2969 continue;
2970
35df4500
TJB
2971 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2972 if (!breakpoint_enabled (bl->owner)
2973 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2974 continue;
2975
f1310107 2976 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
2977 continue;
2978
35df4500 2979 if (bl->owner->thread != -1)
075f6582 2980 {
a6f1cd96
JB
2981 /* This is a thread-specific breakpoint. Check that ptid
2982 matches that thread. If thread hasn't been computed yet,
2983 it is now time to do so. */
2984 if (thread == -1)
2985 thread = pid_to_thread_id (ptid);
35df4500 2986 if (bl->owner->thread != thread)
a6f1cd96 2987 continue;
075f6582 2988 }
a6f1cd96 2989
35df4500 2990 if (bl->owner->task != 0)
4a306c9a
JB
2991 {
2992 /* This is a task-specific breakpoint. Check that ptid
2993 matches that task. If task hasn't been computed yet,
2994 it is now time to do so. */
2995 if (task == 0)
2996 task = ada_get_task_number (ptid);
35df4500 2997 if (bl->owner->task != task)
4a306c9a
JB
2998 continue;
2999 }
3000
a6f1cd96 3001 if (overlay_debugging
35df4500
TJB
3002 && section_is_overlay (bl->section)
3003 && !section_is_mapped (bl->section))
a6f1cd96
JB
3004 continue; /* unmapped overlay -- can't be a match */
3005
3006 return 1;
c5aa993b 3007 }
c906108c
SS
3008
3009 return 0;
3010}
c906108c 3011\f
c5aa993b 3012
c906108c
SS
3013/* bpstat stuff. External routines' interfaces are documented
3014 in breakpoint.h. */
3015
3016int
fba45db2 3017ep_is_catchpoint (struct breakpoint *ep)
c906108c 3018{
533be4dd 3019 return (ep->type == bp_catchpoint);
c906108c
SS
3020}
3021
f431efe5
PA
3022/* Frees any storage that is part of a bpstat. Does not walk the
3023 'next' chain. */
3024
3025static void
198757a8
VP
3026bpstat_free (bpstat bs)
3027{
3028 if (bs->old_val != NULL)
3029 value_free (bs->old_val);
9add0f1b 3030 decref_counted_command_line (&bs->commands);
f431efe5 3031 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3032 xfree (bs);
3033}
3034
c906108c
SS
3035/* Clear a bpstat so that it says we are not at any breakpoint.
3036 Also free any storage that is part of a bpstat. */
3037
3038void
fba45db2 3039bpstat_clear (bpstat *bsp)
c906108c
SS
3040{
3041 bpstat p;
3042 bpstat q;
3043
3044 if (bsp == 0)
3045 return;
3046 p = *bsp;
3047 while (p != NULL)
3048 {
3049 q = p->next;
198757a8 3050 bpstat_free (p);
c906108c
SS
3051 p = q;
3052 }
3053 *bsp = NULL;
3054}
3055
3056/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3057 is part of the bpstat is copied as well. */
3058
3059bpstat
fba45db2 3060bpstat_copy (bpstat bs)
c906108c
SS
3061{
3062 bpstat p = NULL;
3063 bpstat tmp;
3064 bpstat retval = NULL;
3065
3066 if (bs == NULL)
3067 return bs;
3068
3069 for (; bs != NULL; bs = bs->next)
3070 {
3071 tmp = (bpstat) xmalloc (sizeof (*tmp));
3072 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3073 incref_counted_command_line (tmp->commands);
f431efe5 3074 incref_bp_location (tmp->bp_location_at);
31cc81e9 3075 if (bs->old_val != NULL)
3c3185ac
JK
3076 {
3077 tmp->old_val = value_copy (bs->old_val);
3078 release_value (tmp->old_val);
3079 }
31cc81e9 3080
c906108c
SS
3081 if (p == NULL)
3082 /* This is the first thing in the chain. */
3083 retval = tmp;
3084 else
3085 p->next = tmp;
3086 p = tmp;
3087 }
3088 p->next = NULL;
3089 return retval;
3090}
3091
4a64f543 3092/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3093
3094bpstat
fba45db2 3095bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3096{
c5aa993b
JM
3097 if (bsp == NULL)
3098 return NULL;
c906108c 3099
c5aa993b
JM
3100 for (; bsp != NULL; bsp = bsp->next)
3101 {
f431efe5 3102 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3103 return bsp;
3104 }
c906108c
SS
3105 return NULL;
3106}
3107
4a64f543
MS
3108/* Put in *NUM the breakpoint number of the first breakpoint we are
3109 stopped at. *BSP upon return is a bpstat which points to the
3110 remaining breakpoints stopped at (but which is not guaranteed to be
3111 good for anything but further calls to bpstat_num).
3112
8671a17b
PA
3113 Return 0 if passed a bpstat which does not indicate any breakpoints.
3114 Return -1 if stopped at a breakpoint that has been deleted since
3115 we set it.
3116 Return 1 otherwise. */
c906108c
SS
3117
3118int
8671a17b 3119bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3120{
3121 struct breakpoint *b;
3122
3123 if ((*bsp) == NULL)
3124 return 0; /* No more breakpoint values */
8671a17b 3125
4a64f543
MS
3126 /* We assume we'll never have several bpstats that correspond to a
3127 single breakpoint -- otherwise, this function might return the
3128 same number more than once and this will look ugly. */
f431efe5 3129 b = (*bsp)->breakpoint_at;
8671a17b
PA
3130 *bsp = (*bsp)->next;
3131 if (b == NULL)
3132 return -1; /* breakpoint that's been deleted since */
3133
3134 *num = b->number; /* We have its number */
3135 return 1;
c906108c
SS
3136}
3137
3138/* Modify BS so that the actions will not be performed. */
3139
3140void
fba45db2 3141bpstat_clear_actions (bpstat bs)
c906108c
SS
3142{
3143 for (; bs != NULL; bs = bs->next)
3144 {
9add0f1b 3145 decref_counted_command_line (&bs->commands);
dde2d684 3146 bs->commands_left = NULL;
c906108c
SS
3147 if (bs->old_val != NULL)
3148 {
3149 value_free (bs->old_val);
3150 bs->old_val = NULL;
3151 }
3152 }
3153}
3154
f3b1572e
PA
3155/* Called when a command is about to proceed the inferior. */
3156
3157static void
3158breakpoint_about_to_proceed (void)
3159{
3160 if (!ptid_equal (inferior_ptid, null_ptid))
3161 {
3162 struct thread_info *tp = inferior_thread ();
3163
3164 /* Allow inferior function calls in breakpoint commands to not
3165 interrupt the command list. When the call finishes
3166 successfully, the inferior will be standing at the same
3167 breakpoint as if nothing happened. */
16c381f0 3168 if (tp->control.in_infcall)
f3b1572e
PA
3169 return;
3170 }
3171
3172 breakpoint_proceeded = 1;
3173}
3174
4a64f543
MS
3175/* Stub for cleaning up our state if we error-out of a breakpoint
3176 command. */
c906108c 3177static void
4efb68b1 3178cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3179{
3180 executing_breakpoint_commands = 0;
3181}
3182
4a64f543
MS
3183/* Execute all the commands associated with all the breakpoints at
3184 this location. Any of these commands could cause the process to
3185 proceed beyond this point, etc. We look out for such changes by
3186 checking the global "breakpoint_proceeded" after each command.
c906108c 3187
347bddb7
PA
3188 Returns true if a breakpoint command resumed the inferior. In that
3189 case, it is the caller's responsibility to recall it again with the
3190 bpstat of the current thread. */
3191
3192static int
3193bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3194{
3195 bpstat bs;
3196 struct cleanup *old_chain;
347bddb7 3197 int again = 0;
c906108c
SS
3198
3199 /* Avoid endless recursion if a `source' command is contained
3200 in bs->commands. */
3201 if (executing_breakpoint_commands)
347bddb7 3202 return 0;
c906108c
SS
3203
3204 executing_breakpoint_commands = 1;
3205 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3206
4a64f543 3207 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3208 bs = *bsp;
3209
3210 breakpoint_proceeded = 0;
3211 for (; bs != NULL; bs = bs->next)
3212 {
9add0f1b 3213 struct counted_command_line *ccmd;
6c50ab1c
JB
3214 struct command_line *cmd;
3215 struct cleanup *this_cmd_tree_chain;
3216
3217 /* Take ownership of the BSP's command tree, if it has one.
3218
3219 The command tree could legitimately contain commands like
3220 'step' and 'next', which call clear_proceed_status, which
3221 frees stop_bpstat's command tree. To make sure this doesn't
3222 free the tree we're executing out from under us, we need to
3223 take ownership of the tree ourselves. Since a given bpstat's
3224 commands are only executed once, we don't need to copy it; we
3225 can clear the pointer in the bpstat, and make sure we free
3226 the tree when we're done. */
9add0f1b
TT
3227 ccmd = bs->commands;
3228 bs->commands = NULL;
3229 this_cmd_tree_chain
3230 = make_cleanup_decref_counted_command_line (&ccmd);
3231 cmd = bs->commands_left;
3232 bs->commands_left = NULL;
6c50ab1c 3233
c906108c
SS
3234 while (cmd != NULL)
3235 {
3236 execute_control_command (cmd);
3237
3238 if (breakpoint_proceeded)
3239 break;
3240 else
3241 cmd = cmd->next;
3242 }
6c50ab1c
JB
3243
3244 /* We can free this command tree now. */
3245 do_cleanups (this_cmd_tree_chain);
3246
c906108c 3247 if (breakpoint_proceeded)
32c1e744
VP
3248 {
3249 if (target_can_async_p ())
347bddb7
PA
3250 /* If we are in async mode, then the target might be still
3251 running, not stopped at any breakpoint, so nothing for
3252 us to do here -- just return to the event loop. */
3253 ;
32c1e744
VP
3254 else
3255 /* In sync mode, when execute_control_command returns
3256 we're already standing on the next breakpoint.
347bddb7
PA
3257 Breakpoint commands for that stop were not run, since
3258 execute_command does not run breakpoint commands --
3259 only command_line_handler does, but that one is not
3260 involved in execution of breakpoint commands. So, we
3261 can now execute breakpoint commands. It should be
3262 noted that making execute_command do bpstat actions is
3263 not an option -- in this case we'll have recursive
3264 invocation of bpstat for each breakpoint with a
3265 command, and can easily blow up GDB stack. Instead, we
3266 return true, which will trigger the caller to recall us
3267 with the new stop_bpstat. */
3268 again = 1;
3269 break;
32c1e744 3270 }
c906108c 3271 }
c2b8ed2c 3272 do_cleanups (old_chain);
347bddb7
PA
3273 return again;
3274}
3275
3276void
3277bpstat_do_actions (void)
3278{
3279 /* Do any commands attached to breakpoint we are stopped at. */
3280 while (!ptid_equal (inferior_ptid, null_ptid)
3281 && target_has_execution
3282 && !is_exited (inferior_ptid)
3283 && !is_executing (inferior_ptid))
3284 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3285 and only return when it is stopped at the next breakpoint, we
3286 keep doing breakpoint actions until it returns false to
3287 indicate the inferior was not resumed. */
16c381f0 3288 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3289 break;
c906108c
SS
3290}
3291
fa4727a6
DJ
3292/* Print out the (old or new) value associated with a watchpoint. */
3293
3294static void
3295watchpoint_value_print (struct value *val, struct ui_file *stream)
3296{
3297 if (val == NULL)
3298 fprintf_unfiltered (stream, _("<unreadable>"));
3299 else
79a45b7d
TT
3300 {
3301 struct value_print_options opts;
3302 get_user_print_options (&opts);
3303 value_print (val, stream, &opts);
3304 }
fa4727a6
DJ
3305}
3306
e514a9d6 3307/* This is the normal print function for a bpstat. In the future,
c906108c 3308 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3309 by having it set different print_it values.
3310
3311 Current scheme: When we stop, bpstat_print() is called. It loops
3312 through the bpstat list of things causing this stop, calling the
4a64f543 3313 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3314 print_bp_stop_message function depends on the print_it field of
4a64f543 3315 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3316
3317 Return values from this routine (ultimately used by bpstat_print()
3318 and normal_stop() to decide what to do):
3319 PRINT_NOTHING: Means we already printed all we needed to print,
3320 don't print anything else.
3321 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3322 that something to be followed by a location.
3323 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3324 that something to be followed by a location.
3325 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3326 analysis. */
c906108c 3327
917317f4 3328static enum print_stop_action
fba45db2 3329print_it_typical (bpstat bs)
c906108c 3330{
f7545552 3331 struct cleanup *old_chain;
4f8d1dc6 3332 struct breakpoint *b;
89f9893c 3333 const struct bp_location *bl;
8b93c638 3334 struct ui_stream *stb;
f7545552
TT
3335 int bp_temp = 0;
3336 enum print_stop_action result;
3337
f431efe5
PA
3338 gdb_assert (bs->bp_location_at != NULL);
3339
3340 bl = bs->bp_location_at;
3341 b = bs->breakpoint_at;
c906108c 3342
f7545552
TT
3343 stb = ui_out_stream_new (uiout);
3344 old_chain = make_cleanup_ui_out_stream_delete (stb);
3345
4f8d1dc6 3346 switch (b->type)
c906108c 3347 {
e514a9d6
JM
3348 case bp_breakpoint:
3349 case bp_hardware_breakpoint:
f431efe5 3350 bp_temp = b->disposition == disp_del;
0d381245
VP
3351 if (bl->address != bl->requested_address)
3352 breakpoint_adjustment_warning (bl->requested_address,
3353 bl->address,
4f8d1dc6
VP
3354 b->number, 1);
3355 annotate_breakpoint (b->number);
2cec12e5
AR
3356 if (bp_temp)
3357 ui_out_text (uiout, "\nTemporary breakpoint ");
3358 else
3359 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3360 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3361 {
3362 ui_out_field_string (uiout, "reason",
3363 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3364 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3365 }
4f8d1dc6 3366 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3367 ui_out_text (uiout, ", ");
f7545552 3368 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3369 break;
3370
3371 case bp_shlib_event:
917317f4
JM
3372 /* Did we stop because the user set the stop_on_solib_events
3373 variable? (If so, we report this as a generic, "Stopped due
3374 to shlib event" message.) */
a3f17187 3375 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3376 result = PRINT_NOTHING;
e514a9d6
JM
3377 break;
3378
c4093a6a 3379 case bp_thread_event:
4a64f543 3380 /* Not sure how we will get here.
c4093a6a 3381 GDB should not stop for these breakpoints. */
a3f17187 3382 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3383 result = PRINT_NOTHING;
c4093a6a
JM
3384 break;
3385
1900040c 3386 case bp_overlay_event:
4a64f543 3387 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3388 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3389 result = PRINT_NOTHING;
1900040c
MS
3390 break;
3391
0fd8e87f
UW
3392 case bp_longjmp_master:
3393 /* These should never be enabled. */
3394 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3395 result = PRINT_NOTHING;
3396 break;
3397
aa7d318d
TT
3398 case bp_std_terminate_master:
3399 /* These should never be enabled. */
3e43a32a
MS
3400 printf_filtered (_("std::terminate Master Breakpoint: "
3401 "gdb should not stop!\n"));
aa7d318d
TT
3402 result = PRINT_NOTHING;
3403 break;
3404
186c406b
TT
3405 case bp_exception_master:
3406 /* These should never be enabled. */
3e43a32a
MS
3407 printf_filtered (_("Exception Master Breakpoint: "
3408 "gdb should not stop!\n"));
186c406b
TT
3409 result = PRINT_NOTHING;
3410 break;
3411
e514a9d6
JM
3412 case bp_watchpoint:
3413 case bp_hardware_watchpoint:
fa4727a6
DJ
3414 annotate_watchpoint (b->number);
3415 if (ui_out_is_mi_like_p (uiout))
3416 ui_out_field_string
3417 (uiout, "reason",
3418 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3419 mention (b);
f7545552 3420 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3421 ui_out_text (uiout, "\nOld value = ");
3422 watchpoint_value_print (bs->old_val, stb->stream);
3423 ui_out_field_stream (uiout, "old", stb);
3424 ui_out_text (uiout, "\nNew value = ");
3425 watchpoint_value_print (b->val, stb->stream);
3426 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3427 ui_out_text (uiout, "\n");
e514a9d6 3428 /* More than one watchpoint may have been triggered. */
f7545552 3429 result = PRINT_UNKNOWN;
e514a9d6
JM
3430 break;
3431
3432 case bp_read_watchpoint:
9dc5e2a9 3433 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3434 ui_out_field_string
3435 (uiout, "reason",
3436 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3437 mention (b);
f7545552 3438 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3439 ui_out_text (uiout, "\nValue = ");
fa4727a6 3440 watchpoint_value_print (b->val, stb->stream);
8b93c638 3441 ui_out_field_stream (uiout, "value", stb);
8b93c638 3442 ui_out_text (uiout, "\n");
f7545552 3443 result = PRINT_UNKNOWN;
e514a9d6
JM
3444 break;
3445
3446 case bp_access_watchpoint:
fa4727a6 3447 if (bs->old_val != NULL)
8b93c638 3448 {
4f8d1dc6 3449 annotate_watchpoint (b->number);
9dc5e2a9 3450 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3451 ui_out_field_string
3452 (uiout, "reason",
3453 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3454 mention (b);
f7545552 3455 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3456 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3457 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3458 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3459 ui_out_text (uiout, "\nNew value = ");
3460 }
3461 else
3462 {
4f8d1dc6 3463 mention (b);
9dc5e2a9 3464 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3465 ui_out_field_string
3466 (uiout, "reason",
3467 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3468 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3469 ui_out_text (uiout, "\nValue = ");
3470 }
fa4727a6 3471 watchpoint_value_print (b->val, stb->stream);
8b93c638 3472 ui_out_field_stream (uiout, "new", stb);
8b93c638 3473 ui_out_text (uiout, "\n");
f7545552 3474 result = PRINT_UNKNOWN;
e514a9d6 3475 break;
4ce44c66 3476
e514a9d6 3477 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3478 here. */
e514a9d6 3479
11cf8741 3480 case bp_finish:
9dc5e2a9 3481 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3482 ui_out_field_string
3483 (uiout, "reason",
3484 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3485 result = PRINT_UNKNOWN;
8b93c638
JM
3486 break;
3487
e514a9d6 3488 case bp_until:
9dc5e2a9 3489 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3490 ui_out_field_string
3491 (uiout, "reason",
3492 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3493 result = PRINT_UNKNOWN;
8b93c638
JM
3494 break;
3495
c2d11a7d 3496 case bp_none:
e514a9d6
JM
3497 case bp_longjmp:
3498 case bp_longjmp_resume:
186c406b
TT
3499 case bp_exception:
3500 case bp_exception_resume:
e514a9d6 3501 case bp_step_resume:
e514a9d6
JM
3502 case bp_watchpoint_scope:
3503 case bp_call_dummy:
aa7d318d 3504 case bp_std_terminate:
1042e4c0 3505 case bp_tracepoint:
7a697b8d 3506 case bp_fast_tracepoint:
4efc6507 3507 case bp_jit_event:
0e30163f
JK
3508 case bp_gnu_ifunc_resolver:
3509 case bp_gnu_ifunc_resolver_return:
e514a9d6 3510 default:
f7545552
TT
3511 result = PRINT_UNKNOWN;
3512 break;
e514a9d6 3513 }
f7545552
TT
3514
3515 do_cleanups (old_chain);
3516 return result;
e514a9d6
JM
3517}
3518
3519/* Generic routine for printing messages indicating why we
4a64f543 3520 stopped. The behavior of this function depends on the value
e514a9d6
JM
3521 'print_it' in the bpstat structure. Under some circumstances we
3522 may decide not to print anything here and delegate the task to
4a64f543 3523 normal_stop(). */
e514a9d6
JM
3524
3525static enum print_stop_action
3526print_bp_stop_message (bpstat bs)
3527{
3528 switch (bs->print_it)
3529 {
3530 case print_it_noop:
4a64f543 3531 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3532 return PRINT_UNKNOWN;
3533 break;
3534
3535 case print_it_done:
3536 /* We still want to print the frame, but we already printed the
4a64f543 3537 relevant messages. */
e514a9d6
JM
3538 return PRINT_SRC_AND_LOC;
3539 break;
3540
3541 case print_it_normal:
4f8d1dc6 3542 {
f431efe5
PA
3543 struct breakpoint *b = bs->breakpoint_at;
3544
1a6a67de
TJB
3545 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3546 which has since been deleted. */
3547 if (b == NULL)
3548 return PRINT_UNKNOWN;
3549
4f8d1dc6
VP
3550 /* Normal case. Call the breakpoint's print_it method, or
3551 print_it_typical. */
1a6a67de 3552 if (b->ops != NULL && b->ops->print_it != NULL)
4f8d1dc6
VP
3553 return b->ops->print_it (b);
3554 else
3555 return print_it_typical (bs);
3556 }
3557 break;
3086aeae 3558
e514a9d6 3559 default:
8e65ff28 3560 internal_error (__FILE__, __LINE__,
e2e0b3e5 3561 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3562 break;
c906108c 3563 }
c906108c
SS
3564}
3565
e514a9d6
JM
3566/* Print a message indicating what happened. This is called from
3567 normal_stop(). The input to this routine is the head of the bpstat
3568 list - a list of the eventpoints that caused this stop. This
3569 routine calls the generic print routine for printing a message
3570 about reasons for stopping. This will print (for example) the
3571 "Breakpoint n," part of the output. The return value of this
3572 routine is one of:
c906108c 3573
4a64f543 3574 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3575 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3576 code to print the location. An example is
c5aa993b
JM
3577 "Breakpoint 1, " which should be followed by
3578 the location.
917317f4 3579 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3580 to also print the location part of the message.
3581 An example is the catch/throw messages, which
4a64f543 3582 don't require a location appended to the end.
917317f4 3583 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3584 further info to be printed. */
c906108c 3585
917317f4 3586enum print_stop_action
fba45db2 3587bpstat_print (bpstat bs)
c906108c
SS
3588{
3589 int val;
c5aa993b 3590
c906108c 3591 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3592 (Currently all watchpoints go on the bpstat whether hit or not.
3593 That probably could (should) be changed, provided care is taken
c906108c 3594 with respect to bpstat_explains_signal). */
e514a9d6
JM
3595 for (; bs; bs = bs->next)
3596 {
3597 val = print_bp_stop_message (bs);
3598 if (val == PRINT_SRC_ONLY
3599 || val == PRINT_SRC_AND_LOC
3600 || val == PRINT_NOTHING)
3601 return val;
3602 }
c906108c 3603
e514a9d6 3604 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3605 with and nothing was printed. */
917317f4 3606 return PRINT_UNKNOWN;
c906108c
SS
3607}
3608
4a64f543
MS
3609/* Evaluate the expression EXP and return 1 if value is zero. This is
3610 used inside a catch_errors to evaluate the breakpoint condition.
3611 The argument is a "struct expression *" that has been cast to a
3612 "char *" to make it pass through catch_errors. */
c906108c
SS
3613
3614static int
4efb68b1 3615breakpoint_cond_eval (void *exp)
c906108c 3616{
278cd55f 3617 struct value *mark = value_mark ();
c5aa993b 3618 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3619
c906108c
SS
3620 value_free_to_mark (mark);
3621 return i;
3622}
3623
5760d0ab 3624/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3625
3626static bpstat
5760d0ab 3627bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3628{
3629 bpstat bs;
3630
3631 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3632 bs->next = NULL;
3633 **bs_link_pointer = bs;
3634 *bs_link_pointer = &bs->next;
f431efe5
PA
3635 bs->breakpoint_at = bl->owner;
3636 bs->bp_location_at = bl;
3637 incref_bp_location (bl);
c906108c
SS
3638 /* If the condition is false, etc., don't do the commands. */
3639 bs->commands = NULL;
9add0f1b 3640 bs->commands_left = NULL;
c906108c
SS
3641 bs->old_val = NULL;
3642 bs->print_it = print_it_normal;
3643 return bs;
3644}
3645\f
d983da9c
DJ
3646/* The target has stopped with waitstatus WS. Check if any hardware
3647 watchpoints have triggered, according to the target. */
3648
3649int
3650watchpoints_triggered (struct target_waitstatus *ws)
3651{
d92524f1 3652 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3653 CORE_ADDR addr;
3654 struct breakpoint *b;
3655
3656 if (!stopped_by_watchpoint)
3657 {
3658 /* We were not stopped by a watchpoint. Mark all watchpoints
3659 as not triggered. */
3660 ALL_BREAKPOINTS (b)
cc60f2e3 3661 if (is_hardware_watchpoint (b))
d983da9c
DJ
3662 b->watchpoint_triggered = watch_triggered_no;
3663
3664 return 0;
3665 }
3666
3667 if (!target_stopped_data_address (&current_target, &addr))
3668 {
3669 /* We were stopped by a watchpoint, but we don't know where.
3670 Mark all watchpoints as unknown. */
3671 ALL_BREAKPOINTS (b)
cc60f2e3 3672 if (is_hardware_watchpoint (b))
d983da9c
DJ
3673 b->watchpoint_triggered = watch_triggered_unknown;
3674
3675 return stopped_by_watchpoint;
3676 }
3677
3678 /* The target could report the data address. Mark watchpoints
3679 affected by this data address as triggered, and all others as not
3680 triggered. */
3681
3682 ALL_BREAKPOINTS (b)
cc60f2e3 3683 if (is_hardware_watchpoint (b))
d983da9c 3684 {
a5606eee 3685 struct bp_location *loc;
d983da9c
DJ
3686
3687 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
3688 for (loc = b->loc; loc; loc = loc->next)
3689 /* Exact match not required. Within range is
3690 sufficient. */
5009afc5
AS
3691 if (target_watchpoint_addr_within_range (&current_target,
3692 addr, loc->address,
3693 loc->length))
a5606eee
VP
3694 {
3695 b->watchpoint_triggered = watch_triggered_yes;
3696 break;
3697 }
d983da9c
DJ
3698 }
3699
3700 return 1;
3701}
3702
c906108c
SS
3703/* Possible return values for watchpoint_check (this can't be an enum
3704 because of check_errors). */
3705/* The watchpoint has been deleted. */
3706#define WP_DELETED 1
3707/* The value has changed. */
3708#define WP_VALUE_CHANGED 2
3709/* The value has not changed. */
3710#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3711/* Ignore this watchpoint, no matter if the value changed or not. */
3712#define WP_IGNORE 4
c906108c
SS
3713
3714#define BP_TEMPFLAG 1
3715#define BP_HARDWAREFLAG 2
3716
4a64f543
MS
3717/* Evaluate watchpoint condition expression and check if its value
3718 changed.
553e4c11
JB
3719
3720 P should be a pointer to struct bpstat, but is defined as a void *
3721 in order for this function to be usable with catch_errors. */
c906108c
SS
3722
3723static int
4efb68b1 3724watchpoint_check (void *p)
c906108c
SS
3725{
3726 bpstat bs = (bpstat) p;
3727 struct breakpoint *b;
3728 struct frame_info *fr;
3729 int within_current_scope;
3730
f431efe5 3731 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3732 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3733 b = bs->breakpoint_at;
c906108c 3734
d0fb5eae
JK
3735 gdb_assert (is_watchpoint (b));
3736
f6bc2008
PA
3737 /* If this is a local watchpoint, we only want to check if the
3738 watchpoint frame is in scope if the current thread is the thread
3739 that was used to create the watchpoint. */
3740 if (!watchpoint_in_thread_scope (b))
60e1c644 3741 return WP_IGNORE;
f6bc2008 3742
c906108c
SS
3743 if (b->exp_valid_block == NULL)
3744 within_current_scope = 1;
3745 else
3746 {
edb3359d
DJ
3747 struct frame_info *frame = get_current_frame ();
3748 struct gdbarch *frame_arch = get_frame_arch (frame);
3749 CORE_ADDR frame_pc = get_frame_pc (frame);
3750
4a64f543
MS
3751 /* in_function_epilogue_p() returns a non-zero value if we're
3752 still in the function but the stack frame has already been
3753 invalidated. Since we can't rely on the values of local
3754 variables after the stack has been destroyed, we are treating
3755 the watchpoint in that state as `not changed' without further
3756 checking. Don't mark watchpoints as changed if the current
3757 frame is in an epilogue - even if they are in some other
3758 frame, our view of the stack is likely to be wrong and
3759 frame_find_by_id could error out. */
a0f49112 3760 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3761 return WP_IGNORE;
a0f49112 3762
101dcfbe 3763 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3764 within_current_scope = (fr != NULL);
69fbadd5
DJ
3765
3766 /* If we've gotten confused in the unwinder, we might have
3767 returned a frame that can't describe this variable. */
edb3359d
DJ
3768 if (within_current_scope)
3769 {
3770 struct symbol *function;
3771
3772 function = get_frame_function (fr);
3773 if (function == NULL
3774 || !contained_in (b->exp_valid_block,
3775 SYMBOL_BLOCK_VALUE (function)))
3776 within_current_scope = 0;
3777 }
69fbadd5 3778
edb3359d 3779 if (within_current_scope)
c906108c
SS
3780 /* If we end up stopping, the current frame will get selected
3781 in normal_stop. So this call to select_frame won't affect
3782 the user. */
0f7d239c 3783 select_frame (fr);
c906108c 3784 }
c5aa993b 3785
c906108c
SS
3786 if (within_current_scope)
3787 {
4a64f543
MS
3788 /* We use value_{,free_to_}mark because it could be a *long*
3789 time before we return to the command level and call
3790 free_all_values. We can't call free_all_values because we
3791 might be in the middle of evaluating a function call. */
c906108c 3792
0cf6dd15 3793 int pc = 0;
278cd55f 3794 struct value *mark = value_mark ();
fa4727a6
DJ
3795 struct value *new_val;
3796
0cf6dd15 3797 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3798
4a64f543
MS
3799 /* We use value_equal_contents instead of value_equal because
3800 the latter coerces an array to a pointer, thus comparing just
3801 the address of the array instead of its contents. This is
3802 not what we want. */
fa4727a6 3803 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3804 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3805 {
fa4727a6
DJ
3806 if (new_val != NULL)
3807 {
3808 release_value (new_val);
3809 value_free_to_mark (mark);
3810 }
c906108c
SS
3811 bs->old_val = b->val;
3812 b->val = new_val;
fa4727a6 3813 b->val_valid = 1;
c906108c
SS
3814 return WP_VALUE_CHANGED;
3815 }
3816 else
3817 {
60e1c644 3818 /* Nothing changed. */
c906108c 3819 value_free_to_mark (mark);
c906108c
SS
3820 return WP_VALUE_NOT_CHANGED;
3821 }
3822 }
3823 else
3824 {
3825 /* This seems like the only logical thing to do because
c5aa993b
JM
3826 if we temporarily ignored the watchpoint, then when
3827 we reenter the block in which it is valid it contains
3828 garbage (in the case of a function, it may have two
3829 garbage values, one before and one after the prologue).
3830 So we can't even detect the first assignment to it and
3831 watch after that (since the garbage may or may not equal
3832 the first value assigned). */
4ce44c66
JM
3833 /* We print all the stop information in print_it_typical(), but
3834 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3835 will be deleted already. So we have no choice but print the
3836 information here. */
9dc5e2a9 3837 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3838 ui_out_field_string
3839 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3840 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3841 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3842 ui_out_text (uiout,
3843 " deleted because the program has left the block in\n\
8b93c638 3844which its expression is valid.\n");
4ce44c66 3845
d0fb5eae 3846 watchpoint_del_at_next_stop (b);
c906108c
SS
3847
3848 return WP_DELETED;
3849 }
3850}
3851
18a18393
VP
3852/* Return true if it looks like target has stopped due to hitting
3853 breakpoint location BL. This function does not check if we
3854 should stop, only if BL explains the stop. */
3855static int
6c95b8df
PA
3856bpstat_check_location (const struct bp_location *bl,
3857 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3858{
3859 struct breakpoint *b = bl->owner;
3860
2bdf28a0
JK
3861 /* BL is from existing struct breakpoint. */
3862 gdb_assert (b != NULL);
3863
f1310107
TJB
3864 if (b->ops && b->ops->breakpoint_hit)
3865 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
3866
e8595ef6
SS
3867 /* By definition, the inferior does not report stops at
3868 tracepoints. */
d77f58be 3869 if (is_tracepoint (b))
e8595ef6
SS
3870 return 0;
3871
cc60f2e3 3872 if (!is_watchpoint (b)
18a18393 3873 && b->type != bp_hardware_breakpoint
fe798b75 3874 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3875 {
6c95b8df
PA
3876 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3877 aspace, bp_addr))
18a18393
VP
3878 return 0;
3879 if (overlay_debugging /* unmapped overlay section */
35df4500 3880 && section_is_overlay (bl->section)
18a18393
VP
3881 && !section_is_mapped (bl->section))
3882 return 0;
3883 }
cc60f2e3 3884
18a18393
VP
3885 /* Continuable hardware watchpoints are treated as non-existent if the
3886 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3887 some data address). Otherwise gdb won't stop on a break instruction
3888 in the code (not from a breakpoint) when a hardware watchpoint has
3889 been defined. Also skip watchpoints which we know did not trigger
3890 (did not match the data address). */
cc60f2e3
PA
3891
3892 if (is_hardware_watchpoint (b)
18a18393
VP
3893 && b->watchpoint_triggered == watch_triggered_no)
3894 return 0;
f1310107 3895
18a18393
VP
3896 if (b->type == bp_hardware_breakpoint)
3897 {
3898 if (bl->address != bp_addr)
3899 return 0;
3900 if (overlay_debugging /* unmapped overlay section */
35df4500 3901 && section_is_overlay (bl->section)
18a18393
VP
3902 && !section_is_mapped (bl->section))
3903 return 0;
3904 }
ce78b96d 3905
18a18393
VP
3906 return 1;
3907}
3908
3909/* If BS refers to a watchpoint, determine if the watched values
3910 has actually changed, and we should stop. If not, set BS->stop
3911 to 0. */
3912static void
3913bpstat_check_watchpoint (bpstat bs)
3914{
2bdf28a0
JK
3915 const struct bp_location *bl;
3916 struct breakpoint *b;
3917
3918 /* BS is built for existing struct breakpoint. */
f431efe5 3919 bl = bs->bp_location_at;
2bdf28a0 3920 gdb_assert (bl != NULL);
f431efe5 3921 b = bs->breakpoint_at;
2bdf28a0 3922 gdb_assert (b != NULL);
18a18393 3923
cc60f2e3 3924 if (is_watchpoint (b))
18a18393 3925 {
18a18393
VP
3926 int must_check_value = 0;
3927
3928 if (b->type == bp_watchpoint)
3929 /* For a software watchpoint, we must always check the
3930 watched value. */
3931 must_check_value = 1;
3932 else if (b->watchpoint_triggered == watch_triggered_yes)
3933 /* We have a hardware watchpoint (read, write, or access)
3934 and the target earlier reported an address watched by
3935 this watchpoint. */
3936 must_check_value = 1;
3937 else if (b->watchpoint_triggered == watch_triggered_unknown
3938 && b->type == bp_hardware_watchpoint)
3939 /* We were stopped by a hardware watchpoint, but the target could
3940 not report the data address. We must check the watchpoint's
3941 value. Access and read watchpoints are out of luck; without
3942 a data address, we can't figure it out. */
3943 must_check_value = 1;
3944
3945 if (must_check_value)
3946 {
3e43a32a
MS
3947 char *message
3948 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3949 b->number);
18a18393
VP
3950 struct cleanup *cleanups = make_cleanup (xfree, message);
3951 int e = catch_errors (watchpoint_check, bs, message,
3952 RETURN_MASK_ALL);
3953 do_cleanups (cleanups);
3954 switch (e)
3955 {
3956 case WP_DELETED:
3957 /* We've already printed what needs to be printed. */
3958 bs->print_it = print_it_done;
3959 /* Stop. */
3960 break;
60e1c644
PA
3961 case WP_IGNORE:
3962 bs->print_it = print_it_noop;
3963 bs->stop = 0;
3964 break;
18a18393
VP
3965 case WP_VALUE_CHANGED:
3966 if (b->type == bp_read_watchpoint)
3967 {
85d721b8
PA
3968 /* There are two cases to consider here:
3969
4a64f543 3970 1. We're watching the triggered memory for reads.
85d721b8
PA
3971 In that case, trust the target, and always report
3972 the watchpoint hit to the user. Even though
3973 reads don't cause value changes, the value may
3974 have changed since the last time it was read, and
3975 since we're not trapping writes, we will not see
3976 those, and as such we should ignore our notion of
3977 old value.
3978
4a64f543 3979 2. We're watching the triggered memory for both
85d721b8
PA
3980 reads and writes. There are two ways this may
3981 happen:
3982
4a64f543 3983 2.1. This is a target that can't break on data
85d721b8
PA
3984 reads only, but can break on accesses (reads or
3985 writes), such as e.g., x86. We detect this case
3986 at the time we try to insert read watchpoints.
3987
4a64f543 3988 2.2. Otherwise, the target supports read
85d721b8
PA
3989 watchpoints, but, the user set an access or write
3990 watchpoint watching the same memory as this read
3991 watchpoint.
3992
3993 If we're watching memory writes as well as reads,
3994 ignore watchpoint hits when we find that the
3995 value hasn't changed, as reads don't cause
3996 changes. This still gives false positives when
3997 the program writes the same value to memory as
3998 what there was already in memory (we will confuse
3999 it for a read), but it's much better than
4000 nothing. */
4001
4002 int other_write_watchpoint = 0;
4003
4004 if (bl->watchpoint_type == hw_read)
4005 {
4006 struct breakpoint *other_b;
4007
4008 ALL_BREAKPOINTS (other_b)
4009 if ((other_b->type == bp_hardware_watchpoint
4010 || other_b->type == bp_access_watchpoint)
4011 && (other_b->watchpoint_triggered
4012 == watch_triggered_yes))
4013 {
4014 other_write_watchpoint = 1;
4015 break;
4016 }
4017 }
4018
4019 if (other_write_watchpoint
4020 || bl->watchpoint_type == hw_access)
4021 {
4022 /* We're watching the same memory for writes,
4023 and the value changed since the last time we
4024 updated it, so this trap must be for a write.
4025 Ignore it. */
4026 bs->print_it = print_it_noop;
4027 bs->stop = 0;
4028 }
18a18393
VP
4029 }
4030 break;
4031 case WP_VALUE_NOT_CHANGED:
4032 if (b->type == bp_hardware_watchpoint
4033 || b->type == bp_watchpoint)
4034 {
4035 /* Don't stop: write watchpoints shouldn't fire if
4036 the value hasn't changed. */
4037 bs->print_it = print_it_noop;
4038 bs->stop = 0;
4039 }
4040 /* Stop. */
4041 break;
4042 default:
4043 /* Can't happen. */
4044 case 0:
4045 /* Error from catch_errors. */
4046 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 4047 watchpoint_del_at_next_stop (b);
18a18393
VP
4048 /* We've already printed what needs to be printed. */
4049 bs->print_it = print_it_done;
4050 break;
4051 }
4052 }
4053 else /* must_check_value == 0 */
4054 {
4055 /* This is a case where some watchpoint(s) triggered, but
4056 not at the address of this watchpoint, or else no
4057 watchpoint triggered after all. So don't print
4058 anything for this watchpoint. */
4059 bs->print_it = print_it_noop;
4060 bs->stop = 0;
4061 }
4062 }
4063}
4064
4065
4066/* Check conditions (condition proper, frame, thread and ignore count)
4067 of breakpoint referred to by BS. If we should not stop for this
4068 breakpoint, set BS->stop to 0. */
f431efe5 4069
18a18393
VP
4070static void
4071bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4072{
4073 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4074 const struct bp_location *bl;
4075 struct breakpoint *b;
4076
4077 /* BS is built for existing struct breakpoint. */
f431efe5 4078 bl = bs->bp_location_at;
2bdf28a0 4079 gdb_assert (bl != NULL);
f431efe5 4080 b = bs->breakpoint_at;
2bdf28a0 4081 gdb_assert (b != NULL);
18a18393
VP
4082
4083 if (frame_id_p (b->frame_id)
edb3359d 4084 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4085 bs->stop = 0;
4086 else if (bs->stop)
4087 {
4088 int value_is_zero = 0;
60e1c644
PA
4089 struct expression *cond;
4090
7371cf6d
PM
4091 /* Evaluate Python breakpoints that have a "stop"
4092 method implemented. */
4093 if (b->py_bp_object)
4094 bs->stop = gdbpy_should_stop (b->py_bp_object);
4095
60e1c644
PA
4096 if (is_watchpoint (b))
4097 cond = b->cond_exp;
4098 else
4099 cond = bl->cond;
4100
f431efe5 4101 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4102 {
60e1c644
PA
4103 int within_current_scope = 1;
4104
c5bc3a77
DJ
4105 /* We use value_mark and value_free_to_mark because it could
4106 be a long time before we return to the command level and
4107 call free_all_values. We can't call free_all_values
4108 because we might be in the middle of evaluating a
4109 function call. */
4110 struct value *mark = value_mark ();
4111
edb3359d
DJ
4112 /* Need to select the frame, with all that implies so that
4113 the conditions will have the right context. Because we
4114 use the frame, we will not see an inlined function's
4115 variables when we arrive at a breakpoint at the start
4116 of the inlined function; the current frame will be the
4117 call site. */
60e1c644
PA
4118 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4119 select_frame (get_current_frame ());
4120 else
4121 {
4122 struct frame_info *frame;
4123
4124 /* For local watchpoint expressions, which particular
4125 instance of a local is being watched matters, so we
4126 keep track of the frame to evaluate the expression
4127 in. To evaluate the condition however, it doesn't
4128 really matter which instantiation of the function
4129 where the condition makes sense triggers the
4130 watchpoint. This allows an expression like "watch
4131 global if q > 10" set in `func', catch writes to
4132 global on all threads that call `func', or catch
4133 writes on all recursive calls of `func' by a single
4134 thread. We simply always evaluate the condition in
4135 the innermost frame that's executing where it makes
4136 sense to evaluate the condition. It seems
4137 intuitive. */
4138 frame = block_innermost_frame (b->cond_exp_valid_block);
4139 if (frame != NULL)
4140 select_frame (frame);
4141 else
4142 within_current_scope = 0;
4143 }
4144 if (within_current_scope)
4145 value_is_zero
4146 = catch_errors (breakpoint_cond_eval, cond,
4147 "Error in testing breakpoint condition:\n",
4148 RETURN_MASK_ALL);
4149 else
4150 {
4151 warning (_("Watchpoint condition cannot be tested "
4152 "in the current scope"));
4153 /* If we failed to set the right context for this
4154 watchpoint, unconditionally report it. */
4155 value_is_zero = 0;
4156 }
4a64f543 4157 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4158 value_free_to_mark (mark);
18a18393 4159 }
60e1c644
PA
4160
4161 if (cond && value_is_zero)
18a18393
VP
4162 {
4163 bs->stop = 0;
4164 }
4165 else if (b->thread != -1 && b->thread != thread_id)
4166 {
4167 bs->stop = 0;
4168 }
4169 else if (b->ignore_count > 0)
4170 {
4171 b->ignore_count--;
4172 annotate_ignore_count_change ();
4173 bs->stop = 0;
4a64f543 4174 /* Increase the hit count even though we don't stop. */
18a18393
VP
4175 ++(b->hit_count);
4176 }
4177 }
4178}
4179
4180
9709f61c 4181/* Get a bpstat associated with having just stopped at address
d983da9c 4182 BP_ADDR in thread PTID.
c906108c 4183
d983da9c 4184 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4185 don't understand this stop. Result is a chain of bpstat's such
4186 that:
c906108c 4187
c5aa993b 4188 if we don't understand the stop, the result is a null pointer.
c906108c 4189
c5aa993b 4190 if we understand why we stopped, the result is not null.
c906108c 4191
c5aa993b
JM
4192 Each element of the chain refers to a particular breakpoint or
4193 watchpoint at which we have stopped. (We may have stopped for
4194 several reasons concurrently.)
c906108c 4195
c5aa993b
JM
4196 Each element of the chain has valid next, breakpoint_at,
4197 commands, FIXME??? fields. */
c906108c
SS
4198
4199bpstat
6c95b8df
PA
4200bpstat_stop_status (struct address_space *aspace,
4201 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4202{
0d381245 4203 struct breakpoint *b = NULL;
afe38095 4204 struct bp_location *bl;
20874c92 4205 struct bp_location *loc;
5760d0ab
JK
4206 /* First item of allocated bpstat's. */
4207 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4208 /* Pointer to the last thing in the chain currently. */
5760d0ab 4209 bpstat bs;
20874c92 4210 int ix;
429374b8 4211 int need_remove_insert;
f431efe5 4212 int removed_any;
c906108c 4213
f431efe5
PA
4214 /* First, build the bpstat chain with locations that explain a
4215 target stop, while being careful to not set the target running,
4216 as that may invalidate locations (in particular watchpoint
4217 locations are recreated). Resuming will happen here with
4218 breakpoint conditions or watchpoint expressions that include
4219 inferior function calls. */
c5aa993b 4220
429374b8
JK
4221 ALL_BREAKPOINTS (b)
4222 {
4223 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4224 continue;
a5606eee 4225
429374b8
JK
4226 for (bl = b->loc; bl != NULL; bl = bl->next)
4227 {
4a64f543
MS
4228 /* For hardware watchpoints, we look only at the first
4229 location. The watchpoint_check function will work on the
4230 entire expression, not the individual locations. For
4231 read watchpoints, the watchpoints_triggered function has
4232 checked all locations already. */
429374b8
JK
4233 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4234 break;
18a18393 4235
429374b8
JK
4236 if (bl->shlib_disabled)
4237 continue;
c5aa993b 4238
429374b8
JK
4239 if (!bpstat_check_location (bl, aspace, bp_addr))
4240 continue;
c5aa993b 4241
4a64f543
MS
4242 /* Come here if it's a watchpoint, or if the break address
4243 matches. */
c5aa993b 4244
4a64f543
MS
4245 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4246 explain stop. */
c5aa993b 4247
f431efe5
PA
4248 /* Assume we stop. Should we find a watchpoint that is not
4249 actually triggered, or if the condition of the breakpoint
4250 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4251 bs->stop = 1;
4252 bs->print = 1;
d983da9c 4253
f431efe5
PA
4254 /* If this is a scope breakpoint, mark the associated
4255 watchpoint as triggered so that we will handle the
4256 out-of-scope event. We'll get to the watchpoint next
4257 iteration. */
d0fb5eae 4258 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4259 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4260 }
4261 }
4262
4263 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4264 {
f1310107 4265 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4266 {
5760d0ab 4267 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4268 /* For hits of moribund locations, we should just proceed. */
4269 bs->stop = 0;
4270 bs->print = 0;
4271 bs->print_it = print_it_noop;
4272 }
4273 }
4274
f431efe5
PA
4275 /* Now go through the locations that caused the target to stop, and
4276 check whether we're interested in reporting this stop to higher
4277 layers, or whether we should resume the target transparently. */
4278
4279 removed_any = 0;
4280
5760d0ab 4281 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4282 {
4283 if (!bs->stop)
4284 continue;
4285
4286 bpstat_check_watchpoint (bs);
4287 if (!bs->stop)
4288 continue;
4289
4290 b = bs->breakpoint_at;
18a18393 4291
429374b8 4292 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4293 || b->type == bp_longjmp_master
186c406b
TT
4294 || b->type == bp_std_terminate_master
4295 || b->type == bp_exception_master)
429374b8
JK
4296 /* We do not stop for these. */
4297 bs->stop = 0;
4298 else
4299 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4300
429374b8
JK
4301 if (bs->stop)
4302 {
4303 ++(b->hit_count);
c906108c 4304
4a64f543 4305 /* We will stop here. */
429374b8
JK
4306 if (b->disposition == disp_disable)
4307 {
4308 if (b->enable_state != bp_permanent)
4309 b->enable_state = bp_disabled;
f431efe5 4310 removed_any = 1;
429374b8
JK
4311 }
4312 if (b->silent)
4313 bs->print = 0;
4314 bs->commands = b->commands;
9add0f1b
TT
4315 incref_counted_command_line (bs->commands);
4316 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4317 if (bs->commands_left
4318 && (strcmp ("silent", bs->commands_left->line) == 0
4319 || (xdb_commands
4320 && strcmp ("Q",
4321 bs->commands_left->line) == 0)))
429374b8 4322 {
9add0f1b 4323 bs->commands_left = bs->commands_left->next;
429374b8
JK
4324 bs->print = 0;
4325 }
429374b8
JK
4326 }
4327
4328 /* Print nothing for this entry if we dont stop or dont print. */
4329 if (bs->stop == 0 || bs->print == 0)
4330 bs->print_it = print_it_noop;
429374b8 4331 }
876fa593 4332
d983da9c
DJ
4333 /* If we aren't stopping, the value of some hardware watchpoint may
4334 not have changed, but the intermediate memory locations we are
4335 watching may have. Don't bother if we're stopping; this will get
4336 done later. */
d832cb68 4337 need_remove_insert = 0;
5760d0ab
JK
4338 if (! bpstat_causes_stop (bs_head))
4339 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4340 if (!bs->stop
f431efe5
PA
4341 && bs->breakpoint_at
4342 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4343 {
4a64f543 4344 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4345 need_remove_insert = 1;
d983da9c
DJ
4346 }
4347
d832cb68 4348 if (need_remove_insert)
2d134ed3 4349 update_global_location_list (1);
f431efe5
PA
4350 else if (removed_any)
4351 update_global_location_list (0);
d832cb68 4352
5760d0ab 4353 return bs_head;
c906108c 4354}
628fe4e4
JK
4355
4356static void
4357handle_jit_event (void)
4358{
4359 struct frame_info *frame;
4360 struct gdbarch *gdbarch;
4361
4362 /* Switch terminal for any messages produced by
4363 breakpoint_re_set. */
4364 target_terminal_ours_for_output ();
4365
4366 frame = get_current_frame ();
4367 gdbarch = get_frame_arch (frame);
4368
4369 jit_event_handler (gdbarch);
4370
4371 target_terminal_inferior ();
4372}
4373
4374/* Prepare WHAT final decision for infrun. */
4375
4376/* Decide what infrun needs to do with this bpstat. */
4377
c906108c 4378struct bpstat_what
0e30163f 4379bpstat_what (bpstat bs_head)
c906108c 4380{
c906108c 4381 struct bpstat_what retval;
628fe4e4
JK
4382 /* We need to defer calling `solib_add', as adding new symbols
4383 resets breakpoints, which in turn deletes breakpoint locations,
4384 and hence may clear unprocessed entries in the BS chain. */
4385 int shlib_event = 0;
4386 int jit_event = 0;
0e30163f 4387 bpstat bs;
c906108c 4388
628fe4e4 4389 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4390 retval.call_dummy = STOP_NONE;
186c406b 4391 retval.is_longjmp = 0;
628fe4e4 4392
0e30163f 4393 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4394 {
628fe4e4
JK
4395 /* Extract this BS's action. After processing each BS, we check
4396 if its action overrides all we've seem so far. */
4397 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4398 enum bptype bptype;
4399
c906108c 4400 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4401 {
4402 /* I suspect this can happen if it was a momentary
4403 breakpoint which has since been deleted. */
4404 bptype = bp_none;
4405 }
f431efe5 4406 else if (bs->breakpoint_at == NULL)
628fe4e4 4407 bptype = bp_none;
20874c92 4408 else
f431efe5 4409 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4410
4411 switch (bptype)
c906108c
SS
4412 {
4413 case bp_none:
628fe4e4 4414 break;
c906108c
SS
4415 case bp_breakpoint:
4416 case bp_hardware_breakpoint:
4417 case bp_until:
4418 case bp_finish:
4419 if (bs->stop)
4420 {
4421 if (bs->print)
628fe4e4 4422 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4423 else
628fe4e4 4424 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4425 }
4426 else
628fe4e4 4427 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4428 break;
4429 case bp_watchpoint:
4430 case bp_hardware_watchpoint:
4431 case bp_read_watchpoint:
4432 case bp_access_watchpoint:
4433 if (bs->stop)
4434 {
4435 if (bs->print)
628fe4e4 4436 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4437 else
628fe4e4 4438 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4439 }
4440 else
628fe4e4
JK
4441 {
4442 /* There was a watchpoint, but we're not stopping.
4443 This requires no further action. */
4444 }
c906108c
SS
4445 break;
4446 case bp_longjmp:
186c406b 4447 case bp_exception:
628fe4e4 4448 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4449 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4450 break;
4451 case bp_longjmp_resume:
186c406b 4452 case bp_exception_resume:
628fe4e4 4453 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4454 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4455 break;
4456 case bp_step_resume:
4457 if (bs->stop)
628fe4e4
JK
4458 this_action = BPSTAT_WHAT_STEP_RESUME;
4459 else
c906108c 4460 {
628fe4e4
JK
4461 /* It is for the wrong frame. */
4462 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4463 }
c906108c 4464 break;
c906108c 4465 case bp_watchpoint_scope:
c4093a6a 4466 case bp_thread_event:
1900040c 4467 case bp_overlay_event:
0fd8e87f 4468 case bp_longjmp_master:
aa7d318d 4469 case bp_std_terminate_master:
186c406b 4470 case bp_exception_master:
628fe4e4 4471 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4472 break;
ce78b96d 4473 case bp_catchpoint:
c5aa993b
JM
4474 if (bs->stop)
4475 {
4476 if (bs->print)
628fe4e4 4477 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4478 else
628fe4e4 4479 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4480 }
4481 else
628fe4e4
JK
4482 {
4483 /* There was a catchpoint, but we're not stopping.
4484 This requires no further action. */
4485 }
4486 break;
4487 case bp_shlib_event:
4488 shlib_event = 1;
4489
4490 /* If requested, stop when the dynamic linker notifies GDB
4491 of events. This allows the user to get control and place
4492 breakpoints in initializer routines for dynamically
4493 loaded objects (among other things). */
4494 if (stop_on_solib_events)
4495 this_action = BPSTAT_WHAT_STOP_NOISY;
4496 else
4497 this_action = BPSTAT_WHAT_SINGLE;
4498 break;
4499 case bp_jit_event:
4500 jit_event = 1;
4501 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4502 break;
c906108c 4503 case bp_call_dummy:
53a5351d
JM
4504 /* Make sure the action is stop (silent or noisy),
4505 so infrun.c pops the dummy frame. */
aa7d318d 4506 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4507 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4508 break;
4509 case bp_std_terminate:
4510 /* Make sure the action is stop (silent or noisy),
4511 so infrun.c pops the dummy frame. */
aa7d318d 4512 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4513 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4514 break;
1042e4c0 4515 case bp_tracepoint:
7a697b8d 4516 case bp_fast_tracepoint:
0fb4aa4b 4517 case bp_static_tracepoint:
1042e4c0
SS
4518 /* Tracepoint hits should not be reported back to GDB, and
4519 if one got through somehow, it should have been filtered
4520 out already. */
4521 internal_error (__FILE__, __LINE__,
7a697b8d 4522 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4523 break;
4524 case bp_gnu_ifunc_resolver:
4525 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4526 this_action = BPSTAT_WHAT_SINGLE;
4527 break;
4528 case bp_gnu_ifunc_resolver_return:
4529 /* The breakpoint will be removed, execution will restart from the
4530 PC of the former breakpoint. */
4531 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4532 break;
628fe4e4
JK
4533 default:
4534 internal_error (__FILE__, __LINE__,
4535 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4536 }
628fe4e4
JK
4537
4538 retval.main_action = max (retval.main_action, this_action);
c906108c 4539 }
628fe4e4 4540
0e30163f
JK
4541 /* These operations may affect the bs->breakpoint_at state so they are
4542 delayed after MAIN_ACTION is decided above. */
4543
628fe4e4
JK
4544 if (shlib_event)
4545 {
4546 if (debug_infrun)
4547 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4548
4549 /* Check for any newly added shared libraries if we're supposed
4550 to be adding them automatically. */
4551
4552 /* Switch terminal for any messages produced by
4553 breakpoint_re_set. */
4554 target_terminal_ours_for_output ();
4555
4556#ifdef SOLIB_ADD
4557 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4558#else
4559 solib_add (NULL, 0, &current_target, auto_solib_add);
4560#endif
4561
4562 target_terminal_inferior ();
4563 }
4564
4565 if (jit_event)
4566 {
4567 if (debug_infrun)
4568 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4569
4570 handle_jit_event ();
4571 }
4572
0e30163f
JK
4573 for (bs = bs_head; bs != NULL; bs = bs->next)
4574 {
4575 struct breakpoint *b = bs->breakpoint_at;
4576
4577 if (b == NULL)
4578 continue;
4579 switch (b->type)
4580 {
4581 case bp_gnu_ifunc_resolver:
4582 gnu_ifunc_resolver_stop (b);
4583 break;
4584 case bp_gnu_ifunc_resolver_return:
4585 gnu_ifunc_resolver_return_stop (b);
4586 break;
4587 }
4588 }
4589
c906108c
SS
4590 return retval;
4591}
4592
4593/* Nonzero if we should step constantly (e.g. watchpoints on machines
4594 without hardware support). This isn't related to a specific bpstat,
4595 just to things like whether watchpoints are set. */
4596
c5aa993b 4597int
fba45db2 4598bpstat_should_step (void)
c906108c
SS
4599{
4600 struct breakpoint *b;
cc59ec59 4601
c906108c 4602 ALL_BREAKPOINTS (b)
717a8278 4603 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4604 return 1;
c906108c
SS
4605 return 0;
4606}
4607
67822962
PA
4608int
4609bpstat_causes_stop (bpstat bs)
4610{
4611 for (; bs != NULL; bs = bs->next)
4612 if (bs->stop)
4613 return 1;
4614
4615 return 0;
4616}
4617
c906108c 4618\f
c5aa993b 4619
170b53b2
UW
4620/* Compute a string of spaces suitable to indent the next line
4621 so it starts at the position corresponding to the table column
4622 named COL_NAME in the currently active table of UIOUT. */
4623
4624static char *
4625wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4626{
4627 static char wrap_indent[80];
4628 int i, total_width, width, align;
4629 char *text;
4630
4631 total_width = 0;
4632 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4633 {
4634 if (strcmp (text, col_name) == 0)
4635 {
4636 gdb_assert (total_width < sizeof wrap_indent);
4637 memset (wrap_indent, ' ', total_width);
4638 wrap_indent[total_width] = 0;
4639
4640 return wrap_indent;
4641 }
4642
4643 total_width += width + 1;
4644 }
4645
4646 return NULL;
4647}
4648
859825b8
JK
4649/* Print the LOC location out of the list of B->LOC locations. */
4650
170b53b2
UW
4651static void
4652print_breakpoint_location (struct breakpoint *b,
4653 struct bp_location *loc)
0d381245 4654{
6c95b8df
PA
4655 struct cleanup *old_chain = save_current_program_space ();
4656
859825b8
JK
4657 if (loc != NULL && loc->shlib_disabled)
4658 loc = NULL;
4659
6c95b8df
PA
4660 if (loc != NULL)
4661 set_current_program_space (loc->pspace);
4662
56435ebe
TT
4663 if (b->display_canonical)
4664 ui_out_field_string (uiout, "what", b->addr_string);
4665 else if (b->source_file && loc)
0d381245
VP
4666 {
4667 struct symbol *sym
4668 = find_pc_sect_function (loc->address, loc->section);
4669 if (sym)
4670 {
4671 ui_out_text (uiout, "in ");
4672 ui_out_field_string (uiout, "func",
4673 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4674 ui_out_text (uiout, " ");
4675 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4676 ui_out_text (uiout, "at ");
0d381245
VP
4677 }
4678 ui_out_field_string (uiout, "file", b->source_file);
4679 ui_out_text (uiout, ":");
4680
4681 if (ui_out_is_mi_like_p (uiout))
4682 {
4683 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4684 char *fullname = symtab_to_fullname (sal.symtab);
4685
4686 if (fullname)
4687 ui_out_field_string (uiout, "fullname", fullname);
4688 }
4689
4690 ui_out_field_int (uiout, "line", b->line_number);
4691 }
859825b8 4692 else if (loc)
0d381245 4693 {
170b53b2
UW
4694 struct ui_stream *stb = ui_out_stream_new (uiout);
4695 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4696
22e722e1
DJ
4697 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4698 demangle, "");
0d381245 4699 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4700
4701 do_cleanups (stb_chain);
0d381245 4702 }
859825b8
JK
4703 else
4704 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4705
4706 do_cleanups (old_chain);
0d381245
VP
4707}
4708
269b11a2
PA
4709static const char *
4710bptype_string (enum bptype type)
c906108c 4711{
c4093a6a
JM
4712 struct ep_type_description
4713 {
4714 enum bptype type;
4715 char *description;
4716 };
4717 static struct ep_type_description bptypes[] =
c906108c 4718 {
c5aa993b
JM
4719 {bp_none, "?deleted?"},
4720 {bp_breakpoint, "breakpoint"},
c906108c 4721 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4722 {bp_until, "until"},
4723 {bp_finish, "finish"},
4724 {bp_watchpoint, "watchpoint"},
c906108c 4725 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4726 {bp_read_watchpoint, "read watchpoint"},
4727 {bp_access_watchpoint, "acc watchpoint"},
4728 {bp_longjmp, "longjmp"},
4729 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4730 {bp_exception, "exception"},
4731 {bp_exception_resume, "exception resume"},
c5aa993b 4732 {bp_step_resume, "step resume"},
c5aa993b
JM
4733 {bp_watchpoint_scope, "watchpoint scope"},
4734 {bp_call_dummy, "call dummy"},
aa7d318d 4735 {bp_std_terminate, "std::terminate"},
c5aa993b 4736 {bp_shlib_event, "shlib events"},
c4093a6a 4737 {bp_thread_event, "thread events"},
1900040c 4738 {bp_overlay_event, "overlay events"},
0fd8e87f 4739 {bp_longjmp_master, "longjmp master"},
aa7d318d 4740 {bp_std_terminate_master, "std::terminate master"},
186c406b 4741 {bp_exception_master, "exception master"},
ce78b96d 4742 {bp_catchpoint, "catchpoint"},
1042e4c0 4743 {bp_tracepoint, "tracepoint"},
7a697b8d 4744 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4745 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4746 {bp_jit_event, "jit events"},
0e30163f
JK
4747 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4748 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4749 };
269b11a2
PA
4750
4751 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4752 || ((int) type != bptypes[(int) type].type))
4753 internal_error (__FILE__, __LINE__,
4754 _("bptypes table does not describe type #%d."),
4755 (int) type);
4756
4757 return bptypes[(int) type].description;
4758}
4759
4760/* Print B to gdb_stdout. */
4761
4762static void
4763print_one_breakpoint_location (struct breakpoint *b,
4764 struct bp_location *loc,
4765 int loc_number,
4766 struct bp_location **last_loc,
269b11a2
PA
4767 int allflag)
4768{
4769 struct command_line *l;
c2c6d25f 4770 static char bpenables[] = "nynny";
3b31d625 4771 struct cleanup *bkpt_chain;
c906108c 4772
0d381245
VP
4773 int header_of_multiple = 0;
4774 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4775 struct value_print_options opts;
4776
4777 get_user_print_options (&opts);
0d381245
VP
4778
4779 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4780 /* See comment in print_one_breakpoint concerning treatment of
4781 breakpoints with single disabled location. */
0d381245
VP
4782 if (loc == NULL
4783 && (b->loc != NULL
4784 && (b->loc->next != NULL || !b->loc->enabled)))
4785 header_of_multiple = 1;
4786 if (loc == NULL)
4787 loc = b->loc;
4788
c4093a6a 4789 annotate_record ();
3b31d625 4790 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
4791
4792 /* 1 */
4793 annotate_field (0);
0d381245
VP
4794 if (part_of_multiple)
4795 {
4796 char *formatted;
0c6773c1 4797 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4798 ui_out_field_string (uiout, "number", formatted);
4799 xfree (formatted);
4800 }
4801 else
4802 {
4803 ui_out_field_int (uiout, "number", b->number);
4804 }
c4093a6a
JM
4805
4806 /* 2 */
4807 annotate_field (1);
0d381245
VP
4808 if (part_of_multiple)
4809 ui_out_field_skip (uiout, "type");
269b11a2
PA
4810 else
4811 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4812
4813 /* 3 */
4814 annotate_field (2);
0d381245
VP
4815 if (part_of_multiple)
4816 ui_out_field_skip (uiout, "disp");
4817 else
2cec12e5 4818 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4819
c4093a6a
JM
4820
4821 /* 4 */
4822 annotate_field (3);
0d381245 4823 if (part_of_multiple)
54e52265 4824 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4825 else
4a64f543
MS
4826 ui_out_field_fmt (uiout, "enabled", "%c",
4827 bpenables[(int) b->enable_state]);
54e52265 4828 ui_out_spaces (uiout, 2);
0d381245 4829
c4093a6a
JM
4830
4831 /* 5 and 6 */
3086aeae 4832 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4833 {
4a64f543
MS
4834 /* Although the print_one can possibly print all locations,
4835 calling it here is not likely to get any nice result. So,
4836 make sure there's just one location. */
0d381245 4837 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4838 b->ops->print_one (b, last_loc);
0d381245 4839 }
3086aeae
DJ
4840 else
4841 switch (b->type)
4842 {
4843 case bp_none:
4844 internal_error (__FILE__, __LINE__,
e2e0b3e5 4845 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4846 break;
c906108c 4847
3086aeae
DJ
4848 case bp_watchpoint:
4849 case bp_hardware_watchpoint:
4850 case bp_read_watchpoint:
4851 case bp_access_watchpoint:
4852 /* Field 4, the address, is omitted (which makes the columns
4853 not line up too nicely with the headers, but the effect
4854 is relatively readable). */
79a45b7d 4855 if (opts.addressprint)
3086aeae
DJ
4856 ui_out_field_skip (uiout, "addr");
4857 annotate_field (5);
fa8a61dc 4858 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4859 break;
4860
3086aeae
DJ
4861 case bp_breakpoint:
4862 case bp_hardware_breakpoint:
4863 case bp_until:
4864 case bp_finish:
4865 case bp_longjmp:
4866 case bp_longjmp_resume:
186c406b
TT
4867 case bp_exception:
4868 case bp_exception_resume:
3086aeae 4869 case bp_step_resume:
3086aeae
DJ
4870 case bp_watchpoint_scope:
4871 case bp_call_dummy:
aa7d318d 4872 case bp_std_terminate:
3086aeae
DJ
4873 case bp_shlib_event:
4874 case bp_thread_event:
4875 case bp_overlay_event:
0fd8e87f 4876 case bp_longjmp_master:
aa7d318d 4877 case bp_std_terminate_master:
186c406b 4878 case bp_exception_master:
1042e4c0 4879 case bp_tracepoint:
7a697b8d 4880 case bp_fast_tracepoint:
0fb4aa4b 4881 case bp_static_tracepoint:
4efc6507 4882 case bp_jit_event:
0e30163f
JK
4883 case bp_gnu_ifunc_resolver:
4884 case bp_gnu_ifunc_resolver_return:
79a45b7d 4885 if (opts.addressprint)
3086aeae
DJ
4886 {
4887 annotate_field (4);
54e52265 4888 if (header_of_multiple)
0d381245 4889 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4890 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4891 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4892 else
5af949e3
UW
4893 ui_out_field_core_addr (uiout, "addr",
4894 loc->gdbarch, loc->address);
3086aeae
DJ
4895 }
4896 annotate_field (5);
0d381245 4897 if (!header_of_multiple)
170b53b2 4898 print_breakpoint_location (b, loc);
0d381245 4899 if (b->loc)
a6d9a66e 4900 *last_loc = b->loc;
3086aeae
DJ
4901 break;
4902 }
c906108c 4903
6c95b8df
PA
4904
4905 /* For backward compatibility, don't display inferiors unless there
4906 are several. */
4907 if (loc != NULL
4908 && !header_of_multiple
4909 && (allflag
4910 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4911 && (number_of_program_spaces () > 1
4912 || number_of_inferiors () > 1)
4a64f543
MS
4913 /* LOC is for existing B, it cannot be in
4914 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4915 && loc->owner->type != bp_catchpoint)))
4916 {
4917 struct inferior *inf;
4918 int first = 1;
4919
4920 for (inf = inferior_list; inf != NULL; inf = inf->next)
4921 {
4922 if (inf->pspace == loc->pspace)
4923 {
4924 if (first)
4925 {
4926 first = 0;
4927 ui_out_text (uiout, " inf ");
4928 }
4929 else
4930 ui_out_text (uiout, ", ");
4931 ui_out_text (uiout, plongest (inf->num));
4932 }
4933 }
4934 }
4935
4a306c9a 4936 if (!part_of_multiple)
c4093a6a 4937 {
4a306c9a
JB
4938 if (b->thread != -1)
4939 {
4940 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4941 "stop only in" line a little further down. */
4a306c9a
JB
4942 ui_out_text (uiout, " thread ");
4943 ui_out_field_int (uiout, "thread", b->thread);
4944 }
4945 else if (b->task != 0)
4946 {
4947 ui_out_text (uiout, " task ");
4948 ui_out_field_int (uiout, "task", b->task);
4949 }
c4093a6a 4950 }
f1310107 4951
8b93c638 4952 ui_out_text (uiout, "\n");
f1310107
TJB
4953
4954 if (!part_of_multiple && b->ops && b->ops->print_one_detail)
4955 b->ops->print_one_detail (b, uiout);
4956
0fb4aa4b
PA
4957 if (!part_of_multiple && b->static_trace_marker_id)
4958 {
4959 gdb_assert (b->type == bp_static_tracepoint);
4960
4961 ui_out_text (uiout, "\tmarker id is ");
4962 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
4963 b->static_trace_marker_id);
4964 ui_out_text (uiout, "\n");
4965 }
4966
0d381245 4967 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4968 {
4969 annotate_field (6);
8b93c638 4970 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
4971 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4972 the frame ID. */
5af949e3
UW
4973 ui_out_field_core_addr (uiout, "frame",
4974 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4975 ui_out_text (uiout, "\n");
c4093a6a
JM
4976 }
4977
0d381245 4978 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 4979 {
f7f9143b
JB
4980 /* We do not print the condition for Ada exception catchpoints
4981 because the condition is an internal implementation detail
4982 that we do not want to expose to the user. */
c4093a6a 4983 annotate_field (7);
d77f58be 4984 if (is_tracepoint (b))
1042e4c0
SS
4985 ui_out_text (uiout, "\ttrace only if ");
4986 else
4987 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4988 ui_out_field_string (uiout, "cond", b->cond_string);
4989 ui_out_text (uiout, "\n");
4990 }
4991
0d381245 4992 if (!part_of_multiple && b->thread != -1)
c4093a6a 4993 {
4a64f543 4994 /* FIXME should make an annotation for this. */
8b93c638
JM
4995 ui_out_text (uiout, "\tstop only in thread ");
4996 ui_out_field_int (uiout, "thread", b->thread);
4997 ui_out_text (uiout, "\n");
c4093a6a
JM
4998 }
4999
63c715c6 5000 if (!part_of_multiple && b->hit_count)
c4093a6a 5001 {
4a64f543 5002 /* FIXME should make an annotation for this. */
8b93c638
JM
5003 if (ep_is_catchpoint (b))
5004 ui_out_text (uiout, "\tcatchpoint");
5005 else
5006 ui_out_text (uiout, "\tbreakpoint");
5007 ui_out_text (uiout, " already hit ");
5008 ui_out_field_int (uiout, "times", b->hit_count);
5009 if (b->hit_count == 1)
5010 ui_out_text (uiout, " time\n");
5011 else
5012 ui_out_text (uiout, " times\n");
c4093a6a
JM
5013 }
5014
4a64f543
MS
5015 /* Output the count also if it is zero, but only if this is mi.
5016 FIXME: Should have a better test for this. */
9dc5e2a9 5017 if (ui_out_is_mi_like_p (uiout))
63c715c6 5018 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5019 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5020
0d381245 5021 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5022 {
5023 annotate_field (8);
8b93c638
JM
5024 ui_out_text (uiout, "\tignore next ");
5025 ui_out_field_int (uiout, "ignore", b->ignore_count);
5026 ui_out_text (uiout, " hits\n");
c4093a6a 5027 }
059fb39f 5028
9add0f1b 5029 l = b->commands ? b->commands->commands : NULL;
059fb39f 5030 if (!part_of_multiple && l)
c4093a6a 5031 {
3b31d625
EZ
5032 struct cleanup *script_chain;
5033
c4093a6a 5034 annotate_field (9);
3b31d625 5035 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5036 print_command_lines (uiout, l, 4);
3b31d625 5037 do_cleanups (script_chain);
c4093a6a 5038 }
d24317b4 5039
1042e4c0
SS
5040 if (!part_of_multiple && b->pass_count)
5041 {
5042 annotate_field (10);
5043 ui_out_text (uiout, "\tpass count ");
5044 ui_out_field_int (uiout, "pass", b->pass_count);
5045 ui_out_text (uiout, " \n");
5046 }
5047
d24317b4
VP
5048 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5049 {
5050 if (b->addr_string)
5051 ui_out_field_string (uiout, "original-location", b->addr_string);
5052 else if (b->exp_string)
5053 ui_out_field_string (uiout, "original-location", b->exp_string);
5054 }
5055
3b31d625 5056 do_cleanups (bkpt_chain);
c4093a6a 5057}
c5aa993b 5058
0d381245
VP
5059static void
5060print_one_breakpoint (struct breakpoint *b,
4a64f543 5061 struct bp_location **last_loc,
6c95b8df 5062 int allflag)
0d381245 5063{
12c5a436 5064 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
0d381245
VP
5065
5066 /* If this breakpoint has custom print function,
5067 it's already printed. Otherwise, print individual
5068 locations, if any. */
5069 if (b->ops == NULL || b->ops->print_one == NULL)
5070 {
4a64f543
MS
5071 /* If breakpoint has a single location that is disabled, we
5072 print it as if it had several locations, since otherwise it's
5073 hard to represent "breakpoint enabled, location disabled"
5074 situation.
5075
5076 Note that while hardware watchpoints have several locations
a3be7890 5077 internally, that's not a property exposed to user. */
0d381245 5078 if (b->loc
a5606eee 5079 && !is_hardware_watchpoint (b)
0d381245 5080 && (b->loc->next || !b->loc->enabled)
a5606eee 5081 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
5082 {
5083 struct bp_location *loc;
5084 int n = 1;
5085 for (loc = b->loc; loc; loc = loc->next, ++n)
12c5a436 5086 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
0d381245
VP
5087 }
5088 }
5089}
5090
a6d9a66e
UW
5091static int
5092breakpoint_address_bits (struct breakpoint *b)
5093{
5094 int print_address_bits = 0;
5095 struct bp_location *loc;
5096
5097 for (loc = b->loc; loc; loc = loc->next)
5098 {
c7437ca6
PA
5099 int addr_bit;
5100
5101 /* Software watchpoints that aren't watching memory don't have
5102 an address to print. */
5103 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5104 continue;
5105
5106 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5107 if (addr_bit > print_address_bits)
5108 print_address_bits = addr_bit;
5109 }
5110
5111 return print_address_bits;
5112}
0d381245 5113
c4093a6a
JM
5114struct captured_breakpoint_query_args
5115 {
5116 int bnum;
5117 };
c5aa993b 5118
c4093a6a 5119static int
2b65245e 5120do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5121{
5122 struct captured_breakpoint_query_args *args = data;
52f0bd74 5123 struct breakpoint *b;
a6d9a66e 5124 struct bp_location *dummy_loc = NULL;
cc59ec59 5125
c4093a6a
JM
5126 ALL_BREAKPOINTS (b)
5127 {
5128 if (args->bnum == b->number)
c5aa993b 5129 {
12c5a436 5130 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5131 return GDB_RC_OK;
c5aa993b 5132 }
c4093a6a
JM
5133 }
5134 return GDB_RC_NONE;
5135}
c5aa993b 5136
c4093a6a 5137enum gdb_rc
4a64f543
MS
5138gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5139 char **error_message)
c4093a6a
JM
5140{
5141 struct captured_breakpoint_query_args args;
cc59ec59 5142
c4093a6a
JM
5143 args.bnum = bnum;
5144 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5145 an error. */
b0b13bb4
DJ
5146 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5147 error_message, RETURN_MASK_ALL) < 0)
5148 return GDB_RC_FAIL;
5149 else
5150 return GDB_RC_OK;
c4093a6a 5151}
c5aa993b 5152
7f3b0473 5153/* Return non-zero if B is user settable (breakpoints, watchpoints,
4a64f543 5154 catchpoints, et.al.). */
7f3b0473
AC
5155
5156static int
5157user_settable_breakpoint (const struct breakpoint *b)
5158{
5159 return (b->type == bp_breakpoint
ce78b96d 5160 || b->type == bp_catchpoint
7f3b0473 5161 || b->type == bp_hardware_breakpoint
d77f58be 5162 || is_tracepoint (b)
0e30163f
JK
5163 || is_watchpoint (b)
5164 || b->type == bp_gnu_ifunc_resolver);
7f3b0473 5165}
4a64f543 5166
09d682a4
TT
5167/* Return true if this breakpoint was set by the user, false if it is
5168 internal or momentary. */
5169
5170int
5171user_breakpoint_p (struct breakpoint *b)
5172{
5173 return user_settable_breakpoint (b) && b->number > 0;
5174}
5175
7f3b0473 5176/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5177 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5178 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5179 FILTER is non-NULL, call it on each breakpoint and only include the
5180 ones for which it returns non-zero. Return the total number of
5181 breakpoints listed. */
c906108c 5182
d77f58be 5183static int
e5a67952 5184breakpoint_1 (char *args, int allflag,
4a64f543 5185 int (*filter) (const struct breakpoint *))
c4093a6a 5186{
52f0bd74 5187 struct breakpoint *b;
a6d9a66e 5188 struct bp_location *last_loc = NULL;
7f3b0473 5189 int nr_printable_breakpoints;
3b31d625 5190 struct cleanup *bkpttbl_chain;
79a45b7d 5191 struct value_print_options opts;
a6d9a66e 5192 int print_address_bits = 0;
269b11a2
PA
5193 int print_type_col_width = 14;
5194
79a45b7d
TT
5195 get_user_print_options (&opts);
5196
4a64f543
MS
5197 /* Compute the number of rows in the table, as well as the size
5198 required for address fields. */
7f3b0473
AC
5199 nr_printable_breakpoints = 0;
5200 ALL_BREAKPOINTS (b)
e5a67952
MS
5201 {
5202 /* If we have a filter, only list the breakpoints it accepts. */
5203 if (filter && !filter (b))
5204 continue;
5205
5206 /* If we have an "args" string, it is a list of breakpoints to
5207 accept. Skip the others. */
5208 if (args != NULL && *args != '\0')
5209 {
5210 if (allflag && parse_and_eval_long (args) != b->number)
5211 continue;
5212 if (!allflag && !number_is_in_list (args, b->number))
5213 continue;
5214 }
269b11a2 5215
e5a67952
MS
5216 if (allflag || user_breakpoint_p (b))
5217 {
5218 int addr_bit, type_len;
a6d9a66e 5219
e5a67952
MS
5220 addr_bit = breakpoint_address_bits (b);
5221 if (addr_bit > print_address_bits)
5222 print_address_bits = addr_bit;
269b11a2 5223
e5a67952
MS
5224 type_len = strlen (bptype_string (b->type));
5225 if (type_len > print_type_col_width)
5226 print_type_col_width = type_len;
5227
5228 nr_printable_breakpoints++;
5229 }
5230 }
7f3b0473 5231
79a45b7d 5232 if (opts.addressprint)
3b31d625 5233 bkpttbl_chain
3e43a32a
MS
5234 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5235 nr_printable_breakpoints,
3b31d625 5236 "BreakpointTable");
8b93c638 5237 else
3b31d625 5238 bkpttbl_chain
3e43a32a
MS
5239 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5240 nr_printable_breakpoints,
3b31d625 5241 "BreakpointTable");
8b93c638 5242
7f3b0473 5243 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5244 annotate_breakpoints_headers ();
5245 if (nr_printable_breakpoints > 0)
5246 annotate_field (0);
4a64f543 5247 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5248 if (nr_printable_breakpoints > 0)
5249 annotate_field (1);
269b11a2 5250 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5251 "type", "Type"); /* 2 */
d7faa9e7
AC
5252 if (nr_printable_breakpoints > 0)
5253 annotate_field (2);
4a64f543 5254 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5255 if (nr_printable_breakpoints > 0)
5256 annotate_field (3);
54e52265 5257 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5258 if (opts.addressprint)
e5a67952
MS
5259 {
5260 if (nr_printable_breakpoints > 0)
5261 annotate_field (4);
5262 if (print_address_bits <= 32)
5263 ui_out_table_header (uiout, 10, ui_left,
5264 "addr", "Address"); /* 5 */
5265 else
5266 ui_out_table_header (uiout, 18, ui_left,
5267 "addr", "Address"); /* 5 */
5268 }
d7faa9e7
AC
5269 if (nr_printable_breakpoints > 0)
5270 annotate_field (5);
5271 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5272 ui_out_table_body (uiout);
5273 if (nr_printable_breakpoints > 0)
5274 annotate_breakpoints_table ();
7f3b0473 5275
c4093a6a 5276 ALL_BREAKPOINTS (b)
e5a67952
MS
5277 {
5278 QUIT;
5279 /* If we have a filter, only list the breakpoints it accepts. */
5280 if (filter && !filter (b))
5281 continue;
5282
5283 /* If we have an "args" string, it is a list of breakpoints to
5284 accept. Skip the others. */
5285
5286 if (args != NULL && *args != '\0')
5287 {
5288 if (allflag) /* maintenance info breakpoint */
5289 {
5290 if (parse_and_eval_long (args) != b->number)
5291 continue;
5292 }
5293 else /* all others */
5294 {
5295 if (!number_is_in_list (args, b->number))
5296 continue;
5297 }
5298 }
5299 /* We only print out user settable breakpoints unless the
5300 allflag is set. */
5301 if (allflag || user_breakpoint_p (b))
12c5a436 5302 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5303 }
5304
3b31d625 5305 do_cleanups (bkpttbl_chain);
698384cd 5306
7f3b0473 5307 if (nr_printable_breakpoints == 0)
c906108c 5308 {
4a64f543
MS
5309 /* If there's a filter, let the caller decide how to report
5310 empty list. */
d77f58be
SS
5311 if (!filter)
5312 {
e5a67952 5313 if (args == NULL || *args == '\0')
d77f58be
SS
5314 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5315 else
4a64f543 5316 ui_out_message (uiout, 0,
e5a67952
MS
5317 "No breakpoint or watchpoint matching '%s'.\n",
5318 args);
d77f58be 5319 }
c906108c
SS
5320 }
5321 else
c4093a6a 5322 {
a6d9a66e
UW
5323 if (last_loc && !server_command)
5324 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5325 }
c906108c 5326
4a64f543 5327 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5328 there have been breakpoints? */
c906108c 5329 annotate_breakpoints_table_end ();
d77f58be
SS
5330
5331 return nr_printable_breakpoints;
c906108c
SS
5332}
5333
ad443146
SS
5334/* Display the value of default-collect in a way that is generally
5335 compatible with the breakpoint list. */
5336
5337static void
5338default_collect_info (void)
5339{
5340 /* If it has no value (which is frequently the case), say nothing; a
5341 message like "No default-collect." gets in user's face when it's
5342 not wanted. */
5343 if (!*default_collect)
5344 return;
5345
5346 /* The following phrase lines up nicely with per-tracepoint collect
5347 actions. */
5348 ui_out_text (uiout, "default collect ");
5349 ui_out_field_string (uiout, "default-collect", default_collect);
5350 ui_out_text (uiout, " \n");
5351}
5352
c906108c 5353static void
e5a67952 5354breakpoints_info (char *args, int from_tty)
c906108c 5355{
e5a67952 5356 breakpoint_1 (args, 0, NULL);
ad443146
SS
5357
5358 default_collect_info ();
d77f58be
SS
5359}
5360
5361static void
e5a67952 5362watchpoints_info (char *args, int from_tty)
d77f58be 5363{
e5a67952 5364 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5365
5366 if (num_printed == 0)
5367 {
e5a67952 5368 if (args == NULL || *args == '\0')
d77f58be
SS
5369 ui_out_message (uiout, 0, "No watchpoints.\n");
5370 else
e5a67952 5371 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5372 }
c906108c
SS
5373}
5374
7a292a7a 5375static void
e5a67952 5376maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5377{
e5a67952 5378 breakpoint_1 (args, 1, NULL);
ad443146
SS
5379
5380 default_collect_info ();
c906108c
SS
5381}
5382
0d381245 5383static int
714835d5 5384breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5385 struct program_space *pspace,
714835d5 5386 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5387{
5388 struct bp_location *bl = b->loc;
cc59ec59 5389
0d381245
VP
5390 for (; bl; bl = bl->next)
5391 {
6c95b8df
PA
5392 if (bl->pspace == pspace
5393 && bl->address == pc
0d381245
VP
5394 && (!overlay_debugging || bl->section == section))
5395 return 1;
5396 }
5397 return 0;
5398}
5399
6c95b8df
PA
5400/* Print a message describing any breakpoints set at PC. This
5401 concerns with logical breakpoints, so we match program spaces, not
5402 address spaces. */
c906108c
SS
5403
5404static void
6c95b8df
PA
5405describe_other_breakpoints (struct gdbarch *gdbarch,
5406 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5407 struct obj_section *section, int thread)
c906108c 5408{
52f0bd74
AC
5409 int others = 0;
5410 struct breakpoint *b;
c906108c
SS
5411
5412 ALL_BREAKPOINTS (b)
6c95b8df 5413 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5414 if (others > 0)
5415 {
a3f17187
AC
5416 if (others == 1)
5417 printf_filtered (_("Note: breakpoint "));
5418 else /* if (others == ???) */
5419 printf_filtered (_("Note: breakpoints "));
c906108c 5420 ALL_BREAKPOINTS (b)
6c95b8df 5421 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5422 {
5423 others--;
5424 printf_filtered ("%d", b->number);
5425 if (b->thread == -1 && thread != -1)
5426 printf_filtered (" (all threads)");
5427 else if (b->thread != -1)
5428 printf_filtered (" (thread %d)", b->thread);
5429 printf_filtered ("%s%s ",
059fb39f 5430 ((b->enable_state == bp_disabled
8bea4e01
UW
5431 || b->enable_state == bp_call_disabled
5432 || b->enable_state == bp_startup_disabled)
0d381245
VP
5433 ? " (disabled)"
5434 : b->enable_state == bp_permanent
5435 ? " (permanent)"
5436 : ""),
5437 (others > 1) ? ","
5438 : ((others == 1) ? " and" : ""));
5439 }
a3f17187 5440 printf_filtered (_("also set at pc "));
5af949e3 5441 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5442 printf_filtered (".\n");
5443 }
5444}
5445\f
5446/* Set the default place to put a breakpoint
5447 for the `break' command with no arguments. */
5448
5449void
6c95b8df
PA
5450set_default_breakpoint (int valid, struct program_space *pspace,
5451 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5452 int line)
c906108c
SS
5453{
5454 default_breakpoint_valid = valid;
6c95b8df 5455 default_breakpoint_pspace = pspace;
c906108c
SS
5456 default_breakpoint_address = addr;
5457 default_breakpoint_symtab = symtab;
5458 default_breakpoint_line = line;
5459}
5460
e4f237da
KB
5461/* Return true iff it is meaningful to use the address member of
5462 BPT. For some breakpoint types, the address member is irrelevant
5463 and it makes no sense to attempt to compare it to other addresses
5464 (or use it for any other purpose either).
5465
4a64f543
MS
5466 More specifically, each of the following breakpoint types will
5467 always have a zero valued address and we don't want to mark
5468 breakpoints of any of these types to be a duplicate of an actual
5469 breakpoint at address zero:
e4f237da
KB
5470
5471 bp_watchpoint
2d134ed3
PA
5472 bp_catchpoint
5473
5474*/
e4f237da
KB
5475
5476static int
5477breakpoint_address_is_meaningful (struct breakpoint *bpt)
5478{
5479 enum bptype type = bpt->type;
5480
2d134ed3
PA
5481 return (type != bp_watchpoint && type != bp_catchpoint);
5482}
5483
5484/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5485 true if LOC1 and LOC2 represent the same watchpoint location. */
5486
5487static int
4a64f543
MS
5488watchpoint_locations_match (struct bp_location *loc1,
5489 struct bp_location *loc2)
2d134ed3 5490{
2bdf28a0
JK
5491 /* Both of them must not be in moribund_locations. */
5492 gdb_assert (loc1->owner != NULL);
5493 gdb_assert (loc2->owner != NULL);
5494
4a64f543
MS
5495 /* If the target can evaluate the condition expression in hardware,
5496 then we we need to insert both watchpoints even if they are at
5497 the same place. Otherwise the watchpoint will only trigger when
5498 the condition of whichever watchpoint was inserted evaluates to
5499 true, not giving a chance for GDB to check the condition of the
5500 other watchpoint. */
0cf6dd15 5501 if ((loc1->owner->cond_exp
4a64f543
MS
5502 && target_can_accel_watchpoint_condition (loc1->address,
5503 loc1->length,
0cf6dd15
TJB
5504 loc1->watchpoint_type,
5505 loc1->owner->cond_exp))
5506 || (loc2->owner->cond_exp
4a64f543
MS
5507 && target_can_accel_watchpoint_condition (loc2->address,
5508 loc2->length,
0cf6dd15
TJB
5509 loc2->watchpoint_type,
5510 loc2->owner->cond_exp)))
5511 return 0;
5512
85d721b8
PA
5513 /* Note that this checks the owner's type, not the location's. In
5514 case the target does not support read watchpoints, but does
5515 support access watchpoints, we'll have bp_read_watchpoint
5516 watchpoints with hw_access locations. Those should be considered
5517 duplicates of hw_read locations. The hw_read locations will
5518 become hw_access locations later. */
2d134ed3
PA
5519 return (loc1->owner->type == loc2->owner->type
5520 && loc1->pspace->aspace == loc2->pspace->aspace
5521 && loc1->address == loc2->address
5522 && loc1->length == loc2->length);
e4f237da
KB
5523}
5524
6c95b8df
PA
5525/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5526 same breakpoint location. In most targets, this can only be true
5527 if ASPACE1 matches ASPACE2. On targets that have global
5528 breakpoints, the address space doesn't really matter. */
5529
5530static int
5531breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5532 struct address_space *aspace2, CORE_ADDR addr2)
5533{
5534 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5535 || aspace1 == aspace2)
5536 && addr1 == addr2);
5537}
5538
f1310107
TJB
5539/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5540 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5541 matches ASPACE2. On targets that have global breakpoints, the address
5542 space doesn't really matter. */
5543
5544static int
5545breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5546 int len1, struct address_space *aspace2,
5547 CORE_ADDR addr2)
5548{
5549 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5550 || aspace1 == aspace2)
5551 && addr2 >= addr1 && addr2 < addr1 + len1);
5552}
5553
5554/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5555 a ranged breakpoint. In most targets, a match happens only if ASPACE
5556 matches the breakpoint's address space. On targets that have global
5557 breakpoints, the address space doesn't really matter. */
5558
5559static int
5560breakpoint_location_address_match (struct bp_location *bl,
5561 struct address_space *aspace,
5562 CORE_ADDR addr)
5563{
5564 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5565 aspace, addr)
5566 || (bl->length
5567 && breakpoint_address_match_range (bl->pspace->aspace,
5568 bl->address, bl->length,
5569 aspace, addr)));
5570}
5571
2d134ed3
PA
5572/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5573 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5574 represent the same location. */
5575
5576static int
4a64f543
MS
5577breakpoint_locations_match (struct bp_location *loc1,
5578 struct bp_location *loc2)
2d134ed3 5579{
2bdf28a0
JK
5580 int hw_point1, hw_point2;
5581
5582 /* Both of them must not be in moribund_locations. */
5583 gdb_assert (loc1->owner != NULL);
5584 gdb_assert (loc2->owner != NULL);
5585
5586 hw_point1 = is_hardware_watchpoint (loc1->owner);
5587 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5588
5589 if (hw_point1 != hw_point2)
5590 return 0;
5591 else if (hw_point1)
5592 return watchpoint_locations_match (loc1, loc2);
5593 else
f1310107
TJB
5594 /* We compare bp_location.length in order to cover ranged breakpoints. */
5595 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5596 loc2->pspace->aspace, loc2->address)
5597 && loc1->length == loc2->length);
2d134ed3
PA
5598}
5599
76897487
KB
5600static void
5601breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5602 int bnum, int have_bnum)
5603{
f63fbe86
MS
5604 /* The longest string possibly returned by hex_string_custom
5605 is 50 chars. These must be at least that big for safety. */
5606 char astr1[64];
5607 char astr2[64];
76897487 5608
bb599908
PH
5609 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5610 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5611 if (have_bnum)
8a3fe4f8 5612 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5613 bnum, astr1, astr2);
5614 else
8a3fe4f8 5615 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5616}
5617
4a64f543
MS
5618/* Adjust a breakpoint's address to account for architectural
5619 constraints on breakpoint placement. Return the adjusted address.
5620 Note: Very few targets require this kind of adjustment. For most
5621 targets, this function is simply the identity function. */
76897487
KB
5622
5623static CORE_ADDR
a6d9a66e
UW
5624adjust_breakpoint_address (struct gdbarch *gdbarch,
5625 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5626{
a6d9a66e 5627 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5628 {
5629 /* Very few targets need any kind of breakpoint adjustment. */
5630 return bpaddr;
5631 }
88f7da05
KB
5632 else if (bptype == bp_watchpoint
5633 || bptype == bp_hardware_watchpoint
5634 || bptype == bp_read_watchpoint
5635 || bptype == bp_access_watchpoint
fe798b75 5636 || bptype == bp_catchpoint)
88f7da05
KB
5637 {
5638 /* Watchpoints and the various bp_catch_* eventpoints should not
5639 have their addresses modified. */
5640 return bpaddr;
5641 }
76897487
KB
5642 else
5643 {
5644 CORE_ADDR adjusted_bpaddr;
5645
5646 /* Some targets have architectural constraints on the placement
5647 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5648 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5649
5650 /* An adjusted breakpoint address can significantly alter
5651 a user's expectations. Print a warning if an adjustment
5652 is required. */
5653 if (adjusted_bpaddr != bpaddr)
5654 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5655
5656 return adjusted_bpaddr;
5657 }
5658}
5659
7cc221ef
DJ
5660/* Allocate a struct bp_location. */
5661
26bb91f3 5662static struct bp_location *
39d61571 5663allocate_bp_location (struct breakpoint *bpt)
7cc221ef 5664{
afe38095 5665 struct bp_location *loc;
7cc221ef
DJ
5666
5667 loc = xmalloc (sizeof (struct bp_location));
5668 memset (loc, 0, sizeof (*loc));
5669
e049a4b5 5670 loc->owner = bpt;
511a6cd4 5671 loc->cond = NULL;
0d381245
VP
5672 loc->shlib_disabled = 0;
5673 loc->enabled = 1;
e049a4b5 5674
39d61571 5675 switch (bpt->type)
e049a4b5
DJ
5676 {
5677 case bp_breakpoint:
5678 case bp_until:
5679 case bp_finish:
5680 case bp_longjmp:
5681 case bp_longjmp_resume:
186c406b
TT
5682 case bp_exception:
5683 case bp_exception_resume:
e049a4b5 5684 case bp_step_resume:
e049a4b5
DJ
5685 case bp_watchpoint_scope:
5686 case bp_call_dummy:
aa7d318d 5687 case bp_std_terminate:
e049a4b5
DJ
5688 case bp_shlib_event:
5689 case bp_thread_event:
5690 case bp_overlay_event:
4efc6507 5691 case bp_jit_event:
0fd8e87f 5692 case bp_longjmp_master:
aa7d318d 5693 case bp_std_terminate_master:
186c406b 5694 case bp_exception_master:
0e30163f
JK
5695 case bp_gnu_ifunc_resolver:
5696 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5697 loc->loc_type = bp_loc_software_breakpoint;
5698 break;
5699 case bp_hardware_breakpoint:
5700 loc->loc_type = bp_loc_hardware_breakpoint;
5701 break;
5702 case bp_hardware_watchpoint:
5703 case bp_read_watchpoint:
5704 case bp_access_watchpoint:
5705 loc->loc_type = bp_loc_hardware_watchpoint;
5706 break;
5707 case bp_watchpoint:
ce78b96d 5708 case bp_catchpoint:
15c3d785
PA
5709 case bp_tracepoint:
5710 case bp_fast_tracepoint:
0fb4aa4b 5711 case bp_static_tracepoint:
e049a4b5
DJ
5712 loc->loc_type = bp_loc_other;
5713 break;
5714 default:
e2e0b3e5 5715 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5716 }
5717
f431efe5 5718 loc->refc = 1;
7cc221ef
DJ
5719 return loc;
5720}
5721
f431efe5
PA
5722static void
5723free_bp_location (struct bp_location *loc)
fe3f5fa8
VP
5724{
5725 if (loc->cond)
5726 xfree (loc->cond);
74960c60
VP
5727
5728 if (loc->function_name)
5729 xfree (loc->function_name);
f431efe5 5730
fe3f5fa8
VP
5731 xfree (loc);
5732}
5733
f431efe5
PA
5734/* Increment reference count. */
5735
5736static void
5737incref_bp_location (struct bp_location *bl)
5738{
5739 ++bl->refc;
5740}
5741
5742/* Decrement reference count. If the reference count reaches 0,
5743 destroy the bp_location. Sets *BLP to NULL. */
5744
5745static void
5746decref_bp_location (struct bp_location **blp)
5747{
0807b50c
PA
5748 gdb_assert ((*blp)->refc > 0);
5749
f431efe5
PA
5750 if (--(*blp)->refc == 0)
5751 free_bp_location (*blp);
5752 *blp = NULL;
5753}
5754
4a64f543
MS
5755/* Helper to set_raw_breakpoint below. Creates a breakpoint that has
5756 type BPTYPE and has no locations as yet. */
5757/* This function is used in gdbtk sources and thus can not be made
5758 static. */
c906108c 5759
c40e75cd 5760static struct breakpoint *
a6d9a66e
UW
5761set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5762 enum bptype bptype)
c906108c 5763{
52f0bd74 5764 struct breakpoint *b, *b1;
c906108c
SS
5765
5766 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
5767 memset (b, 0, sizeof (*b));
2219d63c 5768
4d28f7a8 5769 b->type = bptype;
a6d9a66e 5770 b->gdbarch = gdbarch;
c906108c
SS
5771 b->language = current_language->la_language;
5772 b->input_radix = input_radix;
5773 b->thread = -1;
b5de0fa7 5774 b->enable_state = bp_enabled;
c906108c
SS
5775 b->next = 0;
5776 b->silent = 0;
5777 b->ignore_count = 0;
5778 b->commands = NULL;
818dd999 5779 b->frame_id = null_frame_id;
3a3e9ee3 5780 b->forked_inferior_pid = null_ptid;
c906108c 5781 b->exec_pathname = NULL;
a96d9b2e 5782 b->syscalls_to_be_caught = NULL;
3086aeae 5783 b->ops = NULL;
0d381245 5784 b->condition_not_parsed = 0;
84f4c1fe 5785 b->py_bp_object = NULL;
d0fb5eae 5786 b->related_breakpoint = b;
c906108c 5787
4a64f543
MS
5788 /* Add this breakpoint to the end of the chain so that a list of
5789 breakpoints will come out in order of increasing numbers. */
c906108c
SS
5790
5791 b1 = breakpoint_chain;
5792 if (b1 == 0)
5793 breakpoint_chain = b;
5794 else
5795 {
5796 while (b1->next)
5797 b1 = b1->next;
5798 b1->next = b;
5799 }
0d381245
VP
5800 return b;
5801}
5802
0e30163f
JK
5803/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5804 resolutions should be made as the user specified the location explicitly
5805 enough. */
5806
0d381245 5807static void
0e30163f 5808set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5809{
2bdf28a0
JK
5810 gdb_assert (loc->owner != NULL);
5811
0d381245 5812 if (loc->owner->type == bp_breakpoint
1042e4c0 5813 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5814 || is_tracepoint (loc->owner))
0d381245 5815 {
0e30163f
JK
5816 int is_gnu_ifunc;
5817
5818 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5819 NULL, NULL, &is_gnu_ifunc);
5820
5821 if (is_gnu_ifunc && !explicit_loc)
5822 {
5823 struct breakpoint *b = loc->owner;
5824
5825 gdb_assert (loc->pspace == current_program_space);
5826 if (gnu_ifunc_resolve_name (loc->function_name,
5827 &loc->requested_address))
5828 {
5829 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5830 loc->address = adjust_breakpoint_address (loc->gdbarch,
5831 loc->requested_address,
5832 b->type);
5833 }
5834 else if (b->type == bp_breakpoint && b->loc == loc
5835 && loc->next == NULL && b->related_breakpoint == b)
5836 {
5837 /* Create only the whole new breakpoint of this type but do not
5838 mess more complicated breakpoints with multiple locations. */
5839 b->type = bp_gnu_ifunc_resolver;
5840 }
5841 }
5842
0d381245
VP
5843 if (loc->function_name)
5844 loc->function_name = xstrdup (loc->function_name);
5845 }
5846}
5847
a6d9a66e
UW
5848/* Attempt to determine architecture of location identified by SAL. */
5849static struct gdbarch *
5850get_sal_arch (struct symtab_and_line sal)
5851{
5852 if (sal.section)
5853 return get_objfile_arch (sal.section->objfile);
5854 if (sal.symtab)
5855 return get_objfile_arch (sal.symtab->objfile);
5856
5857 return NULL;
5858}
5859
0d381245
VP
5860/* set_raw_breakpoint is a low level routine for allocating and
5861 partially initializing a breakpoint of type BPTYPE. The newly
5862 created breakpoint's address, section, source file name, and line
5863 number are provided by SAL. The newly created and partially
5864 initialized breakpoint is added to the breakpoint chain and
5865 is also returned as the value of this function.
5866
5867 It is expected that the caller will complete the initialization of
5868 the newly created breakpoint struct as well as output any status
5869 information regarding the creation of a new breakpoint. In
5870 particular, set_raw_breakpoint does NOT set the breakpoint
5871 number! Care should be taken to not allow an error to occur
5872 prior to completing the initialization of the breakpoint. If this
5873 should happen, a bogus breakpoint will be left on the chain. */
5874
63c252f8 5875struct breakpoint *
a6d9a66e
UW
5876set_raw_breakpoint (struct gdbarch *gdbarch,
5877 struct symtab_and_line sal, enum bptype bptype)
0d381245 5878{
4a64f543
MS
5879 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch,
5880 bptype);
0d381245 5881 CORE_ADDR adjusted_address;
a6d9a66e
UW
5882 struct gdbarch *loc_gdbarch;
5883
5884 loc_gdbarch = get_sal_arch (sal);
5885 if (!loc_gdbarch)
5886 loc_gdbarch = b->gdbarch;
0d381245 5887
6c95b8df
PA
5888 if (bptype != bp_catchpoint)
5889 gdb_assert (sal.pspace != NULL);
5890
0d381245
VP
5891 /* Adjust the breakpoint's address prior to allocating a location.
5892 Once we call allocate_bp_location(), that mostly uninitialized
5893 location will be placed on the location chain. Adjustment of the
8defab1a 5894 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5895 not want its scan of the location chain to find a breakpoint and
5896 location that's only been partially initialized. */
4a64f543
MS
5897 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5898 sal.pc, b->type);
0d381245 5899
39d61571 5900 b->loc = allocate_bp_location (b);
a6d9a66e 5901 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5902 b->loc->requested_address = sal.pc;
5903 b->loc->address = adjusted_address;
6c95b8df
PA
5904 b->loc->pspace = sal.pspace;
5905
5906 /* Store the program space that was used to set the breakpoint, for
5907 breakpoint resetting. */
5908 b->pspace = sal.pspace;
0d381245
VP
5909
5910 if (sal.symtab == NULL)
5911 b->source_file = NULL;
5912 else
1b36a34b 5913 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5914 b->loc->section = sal.section;
5915 b->line_number = sal.line;
5916
0e30163f
JK
5917 set_breakpoint_location_function (b->loc,
5918 sal.explicit_pc || sal.explicit_line);
c906108c 5919
c906108c
SS
5920 breakpoints_changed ();
5921
5922 return b;
5923}
5924
c2c6d25f
JM
5925
5926/* Note that the breakpoint object B describes a permanent breakpoint
5927 instruction, hard-wired into the inferior's code. */
5928void
5929make_breakpoint_permanent (struct breakpoint *b)
5930{
0d381245 5931 struct bp_location *bl;
cc59ec59 5932
b5de0fa7 5933 b->enable_state = bp_permanent;
c2c6d25f 5934
4a64f543
MS
5935 /* By definition, permanent breakpoints are already present in the
5936 code. Mark all locations as inserted. For now,
5937 make_breakpoint_permanent is called in just one place, so it's
5938 hard to say if it's reasonable to have permanent breakpoint with
5939 multiple locations or not, but it's easy to implmement. */
0d381245
VP
5940 for (bl = b->loc; bl; bl = bl->next)
5941 bl->inserted = 1;
c2c6d25f
JM
5942}
5943
53a5351d 5944/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5945 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5946 initiated the operation. */
c906108c
SS
5947
5948void
186c406b 5949set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5950{
35df4500 5951 struct breakpoint *b, *b_tmp;
186c406b 5952 int thread = tp->num;
0fd8e87f
UW
5953
5954 /* To avoid having to rescan all objfile symbols at every step,
5955 we maintain a list of continually-inserted but always disabled
5956 longjmp "master" breakpoints. Here, we simply create momentary
5957 clones of those and enable them for the requested thread. */
35df4500 5958 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5959 if (b->pspace == current_program_space
186c406b
TT
5960 && (b->type == bp_longjmp_master
5961 || b->type == bp_exception_master))
0fd8e87f
UW
5962 {
5963 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 5964
186c406b 5965 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
5966 clone->thread = thread;
5967 }
186c406b
TT
5968
5969 tp->initiating_frame = frame;
c906108c
SS
5970}
5971
611c83ae 5972/* Delete all longjmp breakpoints from THREAD. */
c906108c 5973void
611c83ae 5974delete_longjmp_breakpoint (int thread)
c906108c 5975{
35df4500 5976 struct breakpoint *b, *b_tmp;
c906108c 5977
35df4500 5978 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5979 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5980 {
5981 if (b->thread == thread)
5982 delete_breakpoint (b);
5983 }
c906108c
SS
5984}
5985
1900040c
MS
5986void
5987enable_overlay_breakpoints (void)
5988{
52f0bd74 5989 struct breakpoint *b;
1900040c
MS
5990
5991 ALL_BREAKPOINTS (b)
5992 if (b->type == bp_overlay_event)
5993 {
5994 b->enable_state = bp_enabled;
b60e7edf 5995 update_global_location_list (1);
c02f5703 5996 overlay_events_enabled = 1;
1900040c
MS
5997 }
5998}
5999
6000void
6001disable_overlay_breakpoints (void)
6002{
52f0bd74 6003 struct breakpoint *b;
1900040c
MS
6004
6005 ALL_BREAKPOINTS (b)
6006 if (b->type == bp_overlay_event)
6007 {
6008 b->enable_state = bp_disabled;
b60e7edf 6009 update_global_location_list (0);
c02f5703 6010 overlay_events_enabled = 0;
1900040c
MS
6011 }
6012}
6013
aa7d318d
TT
6014/* Set an active std::terminate breakpoint for each std::terminate
6015 master breakpoint. */
6016void
6017set_std_terminate_breakpoint (void)
6018{
35df4500 6019 struct breakpoint *b, *b_tmp;
aa7d318d 6020
35df4500 6021 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6022 if (b->pspace == current_program_space
6023 && b->type == bp_std_terminate_master)
6024 {
6025 struct breakpoint *clone = clone_momentary_breakpoint (b);
6026 clone->type = bp_std_terminate;
6027 }
6028}
6029
6030/* Delete all the std::terminate breakpoints. */
6031void
6032delete_std_terminate_breakpoint (void)
6033{
35df4500 6034 struct breakpoint *b, *b_tmp;
aa7d318d 6035
35df4500 6036 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6037 if (b->type == bp_std_terminate)
6038 delete_breakpoint (b);
6039}
6040
c4093a6a 6041struct breakpoint *
a6d9a66e 6042create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6043{
6044 struct breakpoint *b;
c4093a6a 6045
a6d9a66e 6046 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 6047
b5de0fa7 6048 b->enable_state = bp_enabled;
c4093a6a 6049 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6050 b->addr_string
6051 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6052
b60e7edf 6053 update_global_location_list_nothrow (1);
74960c60 6054
c4093a6a
JM
6055 return b;
6056}
6057
6058void
6059remove_thread_event_breakpoints (void)
6060{
35df4500 6061 struct breakpoint *b, *b_tmp;
c4093a6a 6062
35df4500 6063 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6064 if (b->type == bp_thread_event
6065 && b->loc->pspace == current_program_space)
c4093a6a
JM
6066 delete_breakpoint (b);
6067}
6068
0101ce28
JJ
6069struct lang_and_radix
6070 {
6071 enum language lang;
6072 int radix;
6073 };
6074
4efc6507
DE
6075/* Create a breakpoint for JIT code registration and unregistration. */
6076
6077struct breakpoint *
6078create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6079{
6080 struct breakpoint *b;
6081
6082 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6083 update_global_location_list_nothrow (1);
6084 return b;
6085}
0101ce28 6086
03673fc7
PP
6087/* Remove JIT code registration and unregistration breakpoint(s). */
6088
6089void
6090remove_jit_event_breakpoints (void)
6091{
6092 struct breakpoint *b, *b_tmp;
6093
6094 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6095 if (b->type == bp_jit_event
6096 && b->loc->pspace == current_program_space)
6097 delete_breakpoint (b);
6098}
6099
cae688ec
JJ
6100void
6101remove_solib_event_breakpoints (void)
6102{
35df4500 6103 struct breakpoint *b, *b_tmp;
cae688ec 6104
35df4500 6105 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6106 if (b->type == bp_shlib_event
6107 && b->loc->pspace == current_program_space)
cae688ec
JJ
6108 delete_breakpoint (b);
6109}
6110
6111struct breakpoint *
a6d9a66e 6112create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6113{
6114 struct breakpoint *b;
6115
a6d9a66e 6116 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 6117 update_global_location_list_nothrow (1);
cae688ec
JJ
6118 return b;
6119}
6120
6121/* Disable any breakpoints that are on code in shared libraries. Only
6122 apply to enabled breakpoints, disabled ones can just stay disabled. */
6123
6124void
cb851954 6125disable_breakpoints_in_shlibs (void)
cae688ec 6126{
876fa593 6127 struct bp_location *loc, **locp_tmp;
cae688ec 6128
876fa593 6129 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6130 {
2bdf28a0 6131 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6132 struct breakpoint *b = loc->owner;
2bdf28a0 6133
4a64f543
MS
6134 /* We apply the check to all breakpoints, including disabled for
6135 those with loc->duplicate set. This is so that when breakpoint
6136 becomes enabled, or the duplicate is removed, gdb will try to
6137 insert all breakpoints. If we don't set shlib_disabled here,
6138 we'll try to insert those breakpoints and fail. */
1042e4c0 6139 if (((b->type == bp_breakpoint)
508ccb1f 6140 || (b->type == bp_jit_event)
1042e4c0 6141 || (b->type == bp_hardware_breakpoint)
d77f58be 6142 || (is_tracepoint (b)))
6c95b8df 6143 && loc->pspace == current_program_space
0d381245 6144 && !loc->shlib_disabled
a77053c2 6145#ifdef PC_SOLIB
0d381245 6146 && PC_SOLIB (loc->address)
a77053c2 6147#else
6c95b8df 6148 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6149#endif
6150 )
0d381245
VP
6151 {
6152 loc->shlib_disabled = 1;
6153 }
cae688ec
JJ
6154 }
6155}
6156
4a64f543
MS
6157/* Disable any breakpoints that are in in an unloaded shared library.
6158 Only apply to enabled breakpoints, disabled ones can just stay
6159 disabled. */
84acb35a 6160
75149521 6161static void
84acb35a
JJ
6162disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6163{
876fa593 6164 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6165 int disabled_shlib_breaks = 0;
6166
c86cf029
VP
6167 /* SunOS a.out shared libraries are always mapped, so do not
6168 disable breakpoints; they will only be reported as unloaded
6169 through clear_solib when GDB discards its shared library
6170 list. See clear_solib for more information. */
6171 if (exec_bfd != NULL
6172 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6173 return;
6174
876fa593 6175 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6176 {
2bdf28a0 6177 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6178 struct breakpoint *b = loc->owner;
cc59ec59 6179
0d381245
VP
6180 if ((loc->loc_type == bp_loc_hardware_breakpoint
6181 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6182 && solib->pspace == loc->pspace
e2dd7057 6183 && !loc->shlib_disabled
508ccb1f
TT
6184 && (b->type == bp_breakpoint
6185 || b->type == bp_jit_event
6186 || b->type == bp_hardware_breakpoint)
e2dd7057 6187 && solib_contains_address_p (solib, loc->address))
84acb35a 6188 {
e2dd7057
PP
6189 loc->shlib_disabled = 1;
6190 /* At this point, we cannot rely on remove_breakpoint
6191 succeeding so we must mark the breakpoint as not inserted
6192 to prevent future errors occurring in remove_breakpoints. */
6193 loc->inserted = 0;
6194 if (!disabled_shlib_breaks)
6195 {
6196 target_terminal_ours_for_output ();
3e43a32a
MS
6197 warning (_("Temporarily disabling breakpoints "
6198 "for unloaded shared library \"%s\""),
e2dd7057 6199 solib->so_name);
84acb35a 6200 }
e2dd7057 6201 disabled_shlib_breaks = 1;
84acb35a
JJ
6202 }
6203 }
84acb35a
JJ
6204}
6205
ce78b96d
JB
6206/* FORK & VFORK catchpoints. */
6207
4a64f543
MS
6208/* Implement the "insert" breakpoint_ops method for fork
6209 catchpoints. */
ce78b96d 6210
77b06cd7
TJB
6211static int
6212insert_catch_fork (struct bp_location *bl)
ce78b96d 6213{
77b06cd7 6214 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6215}
6216
4a64f543
MS
6217/* Implement the "remove" breakpoint_ops method for fork
6218 catchpoints. */
ce78b96d
JB
6219
6220static int
77b06cd7 6221remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6222{
6223 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6224}
6225
6226/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6227 catchpoints. */
6228
6229static int
f1310107
TJB
6230breakpoint_hit_catch_fork (const struct bp_location *bl,
6231 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6232{
f1310107 6233 return inferior_has_forked (inferior_ptid, &bl->owner->forked_inferior_pid);
ce78b96d
JB
6234}
6235
4a64f543
MS
6236/* Implement the "print_it" breakpoint_ops method for fork
6237 catchpoints. */
ce78b96d
JB
6238
6239static enum print_stop_action
6240print_it_catch_fork (struct breakpoint *b)
6241{
6242 annotate_catchpoint (b->number);
6243 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
6244 b->number, ptid_get_pid (b->forked_inferior_pid));
6245 return PRINT_SRC_AND_LOC;
6246}
6247
4a64f543
MS
6248/* Implement the "print_one" breakpoint_ops method for fork
6249 catchpoints. */
ce78b96d
JB
6250
6251static void
a6d9a66e 6252print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6253{
79a45b7d
TT
6254 struct value_print_options opts;
6255
6256 get_user_print_options (&opts);
6257
4a64f543
MS
6258 /* Field 4, the address, is omitted (which makes the columns not
6259 line up too nicely with the headers, but the effect is relatively
6260 readable). */
79a45b7d 6261 if (opts.addressprint)
ce78b96d
JB
6262 ui_out_field_skip (uiout, "addr");
6263 annotate_field (5);
6264 ui_out_text (uiout, "fork");
6265 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6266 {
6267 ui_out_text (uiout, ", process ");
6268 ui_out_field_int (uiout, "what",
6269 ptid_get_pid (b->forked_inferior_pid));
6270 ui_out_spaces (uiout, 1);
6271 }
6272}
6273
6274/* Implement the "print_mention" breakpoint_ops method for fork
6275 catchpoints. */
6276
6277static void
6278print_mention_catch_fork (struct breakpoint *b)
6279{
6280 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6281}
6282
6149aea9
PA
6283/* Implement the "print_recreate" breakpoint_ops method for fork
6284 catchpoints. */
6285
6286static void
6287print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6288{
6289 fprintf_unfiltered (fp, "catch fork");
6290}
6291
ce78b96d
JB
6292/* The breakpoint_ops structure to be used in fork catchpoints. */
6293
6294static struct breakpoint_ops catch_fork_breakpoint_ops =
6295{
6296 insert_catch_fork,
6297 remove_catch_fork,
6298 breakpoint_hit_catch_fork,
e09342b5 6299 NULL, /* resources_needed */
ce78b96d
JB
6300 print_it_catch_fork,
6301 print_one_catch_fork,
f1310107 6302 NULL, /* print_one_detail */
6149aea9
PA
6303 print_mention_catch_fork,
6304 print_recreate_catch_fork
ce78b96d
JB
6305};
6306
4a64f543
MS
6307/* Implement the "insert" breakpoint_ops method for vfork
6308 catchpoints. */
ce78b96d 6309
77b06cd7
TJB
6310static int
6311insert_catch_vfork (struct bp_location *bl)
ce78b96d 6312{
77b06cd7 6313 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6314}
6315
4a64f543
MS
6316/* Implement the "remove" breakpoint_ops method for vfork
6317 catchpoints. */
ce78b96d
JB
6318
6319static int
77b06cd7 6320remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6321{
6322 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6323}
6324
6325/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6326 catchpoints. */
6327
6328static int
f1310107
TJB
6329breakpoint_hit_catch_vfork (const struct bp_location *bl,
6330 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6331{
f1310107 6332 return inferior_has_vforked (inferior_ptid, &bl->owner->forked_inferior_pid);
ce78b96d
JB
6333}
6334
4a64f543
MS
6335/* Implement the "print_it" breakpoint_ops method for vfork
6336 catchpoints. */
ce78b96d
JB
6337
6338static enum print_stop_action
6339print_it_catch_vfork (struct breakpoint *b)
6340{
6341 annotate_catchpoint (b->number);
6342 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
6343 b->number, ptid_get_pid (b->forked_inferior_pid));
6344 return PRINT_SRC_AND_LOC;
6345}
6346
4a64f543
MS
6347/* Implement the "print_one" breakpoint_ops method for vfork
6348 catchpoints. */
ce78b96d
JB
6349
6350static void
a6d9a66e 6351print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6352{
79a45b7d
TT
6353 struct value_print_options opts;
6354
6355 get_user_print_options (&opts);
4a64f543
MS
6356 /* Field 4, the address, is omitted (which makes the columns not
6357 line up too nicely with the headers, but the effect is relatively
6358 readable). */
79a45b7d 6359 if (opts.addressprint)
ce78b96d
JB
6360 ui_out_field_skip (uiout, "addr");
6361 annotate_field (5);
6362 ui_out_text (uiout, "vfork");
6363 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
6364 {
6365 ui_out_text (uiout, ", process ");
6366 ui_out_field_int (uiout, "what",
6367 ptid_get_pid (b->forked_inferior_pid));
6368 ui_out_spaces (uiout, 1);
6369 }
6370}
6371
6372/* Implement the "print_mention" breakpoint_ops method for vfork
6373 catchpoints. */
6374
6375static void
6376print_mention_catch_vfork (struct breakpoint *b)
6377{
6378 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6379}
6380
6149aea9
PA
6381/* Implement the "print_recreate" breakpoint_ops method for vfork
6382 catchpoints. */
6383
6384static void
6385print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6386{
6387 fprintf_unfiltered (fp, "catch vfork");
6388}
6389
ce78b96d
JB
6390/* The breakpoint_ops structure to be used in vfork catchpoints. */
6391
6392static struct breakpoint_ops catch_vfork_breakpoint_ops =
6393{
6394 insert_catch_vfork,
6395 remove_catch_vfork,
6396 breakpoint_hit_catch_vfork,
e09342b5 6397 NULL, /* resources_needed */
ce78b96d
JB
6398 print_it_catch_vfork,
6399 print_one_catch_vfork,
f1310107 6400 NULL, /* print_one_detail */
6149aea9
PA
6401 print_mention_catch_vfork,
6402 print_recreate_catch_vfork
ce78b96d
JB
6403};
6404
a96d9b2e
SDJ
6405/* Implement the "insert" breakpoint_ops method for syscall
6406 catchpoints. */
6407
77b06cd7
TJB
6408static int
6409insert_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6410{
6411 struct inferior *inf = current_inferior ();
6412
6413 ++inf->total_syscalls_count;
77b06cd7 6414 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6415 ++inf->any_syscall_count;
6416 else
6417 {
6418 int i, iter;
cc59ec59 6419
a96d9b2e 6420 for (i = 0;
77b06cd7 6421 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6422 i++)
6423 {
6424 int elem;
cc59ec59 6425
a96d9b2e
SDJ
6426 if (iter >= VEC_length (int, inf->syscalls_counts))
6427 {
6428 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6429 uintptr_t vec_addr_offset
6430 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6431 uintptr_t vec_addr;
6432 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6433 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6434 vec_addr_offset;
6435 memset ((void *) vec_addr, 0,
6436 (iter + 1 - old_size) * sizeof (int));
6437 }
6438 elem = VEC_index (int, inf->syscalls_counts, iter);
6439 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6440 }
6441 }
6442
77b06cd7
TJB
6443 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6444 inf->total_syscalls_count != 0,
6445 inf->any_syscall_count,
6446 VEC_length (int, inf->syscalls_counts),
6447 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6448}
6449
6450/* Implement the "remove" breakpoint_ops method for syscall
6451 catchpoints. */
6452
6453static int
77b06cd7 6454remove_catch_syscall (struct bp_location *bl)
a96d9b2e
SDJ
6455{
6456 struct inferior *inf = current_inferior ();
6457
6458 --inf->total_syscalls_count;
77b06cd7 6459 if (!bl->owner->syscalls_to_be_caught)
a96d9b2e
SDJ
6460 --inf->any_syscall_count;
6461 else
6462 {
6463 int i, iter;
cc59ec59 6464
a96d9b2e 6465 for (i = 0;
77b06cd7 6466 VEC_iterate (int, bl->owner->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6467 i++)
6468 {
6469 int elem;
6470 if (iter >= VEC_length (int, inf->syscalls_counts))
6471 /* Shouldn't happen. */
6472 continue;
6473 elem = VEC_index (int, inf->syscalls_counts, iter);
6474 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6475 }
6476 }
6477
6478 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6479 inf->total_syscalls_count != 0,
6480 inf->any_syscall_count,
6481 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6482 VEC_address (int,
6483 inf->syscalls_counts));
a96d9b2e
SDJ
6484}
6485
6486/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6487 catchpoints. */
6488
6489static int
f1310107
TJB
6490breakpoint_hit_catch_syscall (const struct bp_location *bl,
6491 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6492{
4a64f543
MS
6493 /* We must check if we are catching specific syscalls in this
6494 breakpoint. If we are, then we must guarantee that the called
6495 syscall is the same syscall we are catching. */
a96d9b2e 6496 int syscall_number = 0;
f1310107 6497 const struct breakpoint *b = bl->owner;
a96d9b2e
SDJ
6498
6499 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6500 return 0;
6501
6502 /* Now, checking if the syscall is the same. */
6503 if (b->syscalls_to_be_caught)
6504 {
6505 int i, iter;
cc59ec59 6506
a96d9b2e
SDJ
6507 for (i = 0;
6508 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6509 i++)
6510 if (syscall_number == iter)
6511 break;
6512 /* Not the same. */
6513 if (!iter)
6514 return 0;
6515 }
6516
6517 return 1;
6518}
6519
6520/* Implement the "print_it" breakpoint_ops method for syscall
6521 catchpoints. */
6522
6523static enum print_stop_action
6524print_it_catch_syscall (struct breakpoint *b)
6525{
6526 /* These are needed because we want to know in which state a
6527 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6528 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6529 must print "called syscall" or "returned from syscall". */
6530 ptid_t ptid;
6531 struct target_waitstatus last;
6532 struct syscall s;
6533 struct cleanup *old_chain;
6534 char *syscall_id;
6535
6536 get_last_target_status (&ptid, &last);
6537
6538 get_syscall_by_number (last.value.syscall_number, &s);
6539
6540 annotate_catchpoint (b->number);
6541
6542 if (s.name == NULL)
6543 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6544 else
6545 syscall_id = xstrprintf ("'%s'", s.name);
6546
6547 old_chain = make_cleanup (xfree, syscall_id);
6548
6549 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6550 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6551 b->number, syscall_id);
6552 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6553 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6554 b->number, syscall_id);
6555
6556 do_cleanups (old_chain);
6557
6558 return PRINT_SRC_AND_LOC;
6559}
6560
6561/* Implement the "print_one" breakpoint_ops method for syscall
6562 catchpoints. */
6563
6564static void
6565print_one_catch_syscall (struct breakpoint *b,
f1310107 6566 struct bp_location **last_loc)
a96d9b2e
SDJ
6567{
6568 struct value_print_options opts;
6569
6570 get_user_print_options (&opts);
4a64f543
MS
6571 /* Field 4, the address, is omitted (which makes the columns not
6572 line up too nicely with the headers, but the effect is relatively
6573 readable). */
a96d9b2e
SDJ
6574 if (opts.addressprint)
6575 ui_out_field_skip (uiout, "addr");
6576 annotate_field (5);
6577
6578 if (b->syscalls_to_be_caught
6579 && VEC_length (int, b->syscalls_to_be_caught) > 1)
6580 ui_out_text (uiout, "syscalls \"");
6581 else
6582 ui_out_text (uiout, "syscall \"");
6583
6584 if (b->syscalls_to_be_caught)
6585 {
6586 int i, iter;
6587 char *text = xstrprintf ("%s", "");
cc59ec59 6588
a96d9b2e
SDJ
6589 for (i = 0;
6590 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6591 i++)
6592 {
6593 char *x = text;
6594 struct syscall s;
6595 get_syscall_by_number (iter, &s);
6596
6597 if (s.name != NULL)
6598 text = xstrprintf ("%s%s, ", text, s.name);
6599 else
6600 text = xstrprintf ("%s%d, ", text, iter);
6601
6602 /* We have to xfree the last 'text' (now stored at 'x')
6603 because xstrprintf dinamically allocates new space for it
6604 on every call. */
6605 xfree (x);
6606 }
6607 /* Remove the last comma. */
6608 text[strlen (text) - 2] = '\0';
6609 ui_out_field_string (uiout, "what", text);
6610 }
6611 else
6612 ui_out_field_string (uiout, "what", "<any syscall>");
6613 ui_out_text (uiout, "\" ");
6614}
6615
6616/* Implement the "print_mention" breakpoint_ops method for syscall
6617 catchpoints. */
6618
6619static void
6620print_mention_catch_syscall (struct breakpoint *b)
6621{
6622 if (b->syscalls_to_be_caught)
6623 {
6624 int i, iter;
6625
6626 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
6627 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6628 else
6629 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6630
6631 for (i = 0;
6632 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6633 i++)
6634 {
6635 struct syscall s;
6636 get_syscall_by_number (iter, &s);
6637
6638 if (s.name)
6639 printf_filtered (" '%s' [%d]", s.name, s.number);
6640 else
6641 printf_filtered (" %d", s.number);
6642 }
6643 printf_filtered (")");
6644 }
6645 else
6646 printf_filtered (_("Catchpoint %d (any syscall)"),
6647 b->number);
6648}
6649
6149aea9
PA
6650/* Implement the "print_recreate" breakpoint_ops method for syscall
6651 catchpoints. */
6652
6653static void
6654print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6655{
6656 fprintf_unfiltered (fp, "catch syscall");
6657
6658 if (b->syscalls_to_be_caught)
6659 {
6660 int i, iter;
6661
6662 for (i = 0;
6663 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
6664 i++)
6665 {
6666 struct syscall s;
6667
6668 get_syscall_by_number (iter, &s);
6669 if (s.name)
6670 fprintf_unfiltered (fp, " %s", s.name);
6671 else
6672 fprintf_unfiltered (fp, " %d", s.number);
6673 }
6674 }
6675}
6676
a96d9b2e
SDJ
6677/* The breakpoint_ops structure to be used in syscall catchpoints. */
6678
6679static struct breakpoint_ops catch_syscall_breakpoint_ops =
6680{
6681 insert_catch_syscall,
6682 remove_catch_syscall,
6683 breakpoint_hit_catch_syscall,
e09342b5 6684 NULL, /* resources_needed */
a96d9b2e
SDJ
6685 print_it_catch_syscall,
6686 print_one_catch_syscall,
f1310107 6687 NULL, /* print_one_detail */
6149aea9
PA
6688 print_mention_catch_syscall,
6689 print_recreate_catch_syscall
a96d9b2e
SDJ
6690};
6691
6692/* Returns non-zero if 'b' is a syscall catchpoint. */
6693
6694static int
6695syscall_catchpoint_p (struct breakpoint *b)
6696{
6697 return (b->ops == &catch_syscall_breakpoint_ops);
6698}
6699
6700/* Create a new breakpoint of the bp_catchpoint kind and return it,
6701 but does NOT mention it nor update the global location list.
6702 This is useful if you need to fill more fields in the
6703 struct breakpoint before calling mention.
ce78b96d
JB
6704
6705 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6706 If COND_STRING is not NULL, then store it in the breakpoint.
6707 OPS, if not NULL, is the breakpoint_ops structure associated
6708 to the catchpoint. */
6709
6710static struct breakpoint *
a96d9b2e
SDJ
6711create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
6712 char *cond_string,
6713 struct breakpoint_ops *ops)
c906108c 6714{
c5aa993b
JM
6715 struct symtab_and_line sal;
6716 struct breakpoint *b;
c5aa993b 6717
fe39c653 6718 init_sal (&sal);
6c95b8df 6719 sal.pspace = current_program_space;
c5aa993b 6720
a6d9a66e 6721 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
c906108c
SS
6722 set_breakpoint_count (breakpoint_count + 1);
6723 b->number = breakpoint_count;
ce78b96d 6724
1b36a34b 6725 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 6726 b->thread = -1;
c906108c 6727 b->addr_string = NULL;
b5de0fa7
EZ
6728 b->enable_state = bp_enabled;
6729 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 6730 b->ops = ops;
c5aa993b 6731
a96d9b2e
SDJ
6732 return b;
6733}
6734
6735/* Create a new breakpoint of the bp_catchpoint kind and return it.
6736
6737 If TEMPFLAG is non-zero, then make the breakpoint temporary.
6738 If COND_STRING is not NULL, then store it in the breakpoint.
6739 OPS, if not NULL, is the breakpoint_ops structure associated
6740 to the catchpoint. */
6741
6742static struct breakpoint *
6743create_catchpoint (struct gdbarch *gdbarch, int tempflag,
6744 char *cond_string, struct breakpoint_ops *ops)
6745{
6746 struct breakpoint *b =
6747 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
6748
c906108c 6749 mention (b);
ce78b96d 6750 update_global_location_list (1);
c906108c 6751
ce78b96d 6752 return b;
c906108c 6753}
c5aa993b 6754
9b70b993 6755static void
a6d9a66e
UW
6756create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6757 int tempflag, char *cond_string,
ce78b96d 6758 struct breakpoint_ops *ops)
c906108c 6759{
a6d9a66e
UW
6760 struct breakpoint *b
6761 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
ce78b96d
JB
6762
6763 /* FIXME: We should put this information in a breakpoint private data
6764 area. */
6765 b->forked_inferior_pid = null_ptid;
c906108c
SS
6766}
6767
fe798b75
JB
6768/* Exec catchpoints. */
6769
77b06cd7
TJB
6770static int
6771insert_catch_exec (struct bp_location *bl)
c906108c 6772{
77b06cd7 6773 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6774}
c906108c 6775
fe798b75 6776static int
77b06cd7 6777remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6778{
6779 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6780}
c906108c 6781
fe798b75 6782static int
f1310107
TJB
6783breakpoint_hit_catch_exec (const struct bp_location *bl,
6784 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6785{
f1310107 6786 return inferior_has_execd (inferior_ptid, &bl->owner->exec_pathname);
fe798b75 6787}
c906108c 6788
fe798b75
JB
6789static enum print_stop_action
6790print_it_catch_exec (struct breakpoint *b)
6791{
6792 annotate_catchpoint (b->number);
6793 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
6794 b->exec_pathname);
6795 return PRINT_SRC_AND_LOC;
c906108c
SS
6796}
6797
fe798b75 6798static void
a6d9a66e 6799print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75
JB
6800{
6801 struct value_print_options opts;
6802
6803 get_user_print_options (&opts);
6804
6805 /* Field 4, the address, is omitted (which makes the columns
6806 not line up too nicely with the headers, but the effect
6807 is relatively readable). */
6808 if (opts.addressprint)
6809 ui_out_field_skip (uiout, "addr");
6810 annotate_field (5);
6811 ui_out_text (uiout, "exec");
6812 if (b->exec_pathname != NULL)
6813 {
6814 ui_out_text (uiout, ", program \"");
6815 ui_out_field_string (uiout, "what", b->exec_pathname);
6816 ui_out_text (uiout, "\" ");
6817 }
6818}
6819
6820static void
6821print_mention_catch_exec (struct breakpoint *b)
6822{
6823 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6824}
6825
6149aea9
PA
6826/* Implement the "print_recreate" breakpoint_ops method for exec
6827 catchpoints. */
6828
6829static void
6830print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6831{
6832 fprintf_unfiltered (fp, "catch exec");
6833}
6834
fe798b75
JB
6835static struct breakpoint_ops catch_exec_breakpoint_ops =
6836{
6837 insert_catch_exec,
6838 remove_catch_exec,
6839 breakpoint_hit_catch_exec,
e09342b5 6840 NULL, /* resources_needed */
fe798b75
JB
6841 print_it_catch_exec,
6842 print_one_catch_exec,
f1310107 6843 NULL, /* print_one_detail */
6149aea9
PA
6844 print_mention_catch_exec,
6845 print_recreate_catch_exec
fe798b75
JB
6846};
6847
a96d9b2e
SDJ
6848static void
6849create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
6850 struct breakpoint_ops *ops)
6851{
6852 struct gdbarch *gdbarch = get_current_arch ();
6853 struct breakpoint *b =
6854 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
6855
6856 b->syscalls_to_be_caught = filter;
6857
6858 /* Now, we have to mention the breakpoint and update the global
6859 location list. */
6860 mention (b);
6861 update_global_location_list (1);
6862}
6863
c906108c 6864static int
fba45db2 6865hw_breakpoint_used_count (void)
c906108c 6866{
c906108c 6867 int i = 0;
f1310107
TJB
6868 struct breakpoint *b;
6869 struct bp_location *bl;
c906108c
SS
6870
6871 ALL_BREAKPOINTS (b)
c5aa993b 6872 {
d6b74ac4 6873 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6874 for (bl = b->loc; bl; bl = bl->next)
6875 {
6876 /* Special types of hardware breakpoints may use more than
6877 one register. */
6878 if (b->ops && b->ops->resources_needed)
6879 i += b->ops->resources_needed (bl);
6880 else
6881 i++;
6882 }
c5aa993b 6883 }
c906108c
SS
6884
6885 return i;
6886}
6887
6888static int
fba45db2 6889hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 6890{
c906108c 6891 int i = 0;
e09342b5
TJB
6892 struct breakpoint *b;
6893 struct bp_location *bl;
c906108c
SS
6894
6895 *other_type_used = 0;
6896 ALL_BREAKPOINTS (b)
e09342b5
TJB
6897 {
6898 if (!breakpoint_enabled (b))
6899 continue;
6900
c5aa993b 6901 if (b->type == type)
e09342b5
TJB
6902 for (bl = b->loc; bl; bl = bl->next)
6903 {
6904 /* Special types of hardware watchpoints may use more than
6905 one register. */
6906 if (b->ops && b->ops->resources_needed)
6907 i += b->ops->resources_needed (bl);
6908 else
6909 i++;
6910 }
cc60f2e3 6911 else if (is_hardware_watchpoint (b))
c5aa993b 6912 *other_type_used = 1;
e09342b5
TJB
6913 }
6914
c906108c
SS
6915 return i;
6916}
6917
c906108c 6918void
fba45db2 6919disable_watchpoints_before_interactive_call_start (void)
c906108c 6920{
c5aa993b 6921 struct breakpoint *b;
c906108c
SS
6922
6923 ALL_BREAKPOINTS (b)
c5aa993b 6924 {
cc60f2e3 6925 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6926 {
b5de0fa7 6927 b->enable_state = bp_call_disabled;
b60e7edf 6928 update_global_location_list (0);
c5aa993b
JM
6929 }
6930 }
c906108c
SS
6931}
6932
6933void
fba45db2 6934enable_watchpoints_after_interactive_call_stop (void)
c906108c 6935{
c5aa993b 6936 struct breakpoint *b;
c906108c
SS
6937
6938 ALL_BREAKPOINTS (b)
c5aa993b 6939 {
cc60f2e3 6940 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6941 {
b5de0fa7 6942 b->enable_state = bp_enabled;
b60e7edf 6943 update_global_location_list (1);
c5aa993b
JM
6944 }
6945 }
c906108c
SS
6946}
6947
8bea4e01
UW
6948void
6949disable_breakpoints_before_startup (void)
6950{
6951 struct breakpoint *b;
6952 int found = 0;
6953
6954 ALL_BREAKPOINTS (b)
6955 {
6c95b8df
PA
6956 if (b->pspace != current_program_space)
6957 continue;
6958
8bea4e01
UW
6959 if ((b->type == bp_breakpoint
6960 || b->type == bp_hardware_breakpoint)
6961 && breakpoint_enabled (b))
6962 {
6963 b->enable_state = bp_startup_disabled;
6964 found = 1;
6965 }
6966 }
6967
6968 if (found)
6969 update_global_location_list (0);
6970
6c95b8df 6971 current_program_space->executing_startup = 1;
8bea4e01
UW
6972}
6973
6974void
6975enable_breakpoints_after_startup (void)
6976{
6977 struct breakpoint *b;
6978 int found = 0;
6979
6c95b8df 6980 current_program_space->executing_startup = 0;
8bea4e01
UW
6981
6982 ALL_BREAKPOINTS (b)
6983 {
6c95b8df
PA
6984 if (b->pspace != current_program_space)
6985 continue;
6986
8bea4e01
UW
6987 if ((b->type == bp_breakpoint
6988 || b->type == bp_hardware_breakpoint)
6989 && b->enable_state == bp_startup_disabled)
6990 {
6991 b->enable_state = bp_enabled;
6992 found = 1;
6993 }
6994 }
6995
6996 if (found)
6997 breakpoint_re_set ();
6998}
6999
c906108c
SS
7000
7001/* Set a breakpoint that will evaporate an end of command
7002 at address specified by SAL.
7003 Restrict it to frame FRAME if FRAME is nonzero. */
7004
7005struct breakpoint *
a6d9a66e
UW
7006set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7007 struct frame_id frame_id, enum bptype type)
c906108c 7008{
52f0bd74 7009 struct breakpoint *b;
edb3359d
DJ
7010
7011 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7012 one. */
7013 gdb_assert (!frame_id_inlined_p (frame_id));
7014
a6d9a66e 7015 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
7016 b->enable_state = bp_enabled;
7017 b->disposition = disp_donttouch;
818dd999 7018 b->frame_id = frame_id;
c906108c 7019
4a64f543
MS
7020 /* If we're debugging a multi-threaded program, then we want
7021 momentary breakpoints to be active in only a single thread of
7022 control. */
39f77062
KB
7023 if (in_thread_list (inferior_ptid))
7024 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7025
b60e7edf 7026 update_global_location_list_nothrow (1);
74960c60 7027
c906108c
SS
7028 return b;
7029}
611c83ae 7030
e58b0e63
PA
7031/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7032 ORIG is NULL. */
7033
7034struct breakpoint *
7035clone_momentary_breakpoint (struct breakpoint *orig)
7036{
7037 struct breakpoint *copy;
7038
7039 /* If there's nothing to clone, then return nothing. */
7040 if (orig == NULL)
7041 return NULL;
7042
a6d9a66e 7043 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63 7044 copy->loc = allocate_bp_location (copy);
0e30163f 7045 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7046
a6d9a66e 7047 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7048 copy->loc->requested_address = orig->loc->requested_address;
7049 copy->loc->address = orig->loc->address;
7050 copy->loc->section = orig->loc->section;
6c95b8df 7051 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7052
7053 if (orig->source_file == NULL)
7054 copy->source_file = NULL;
7055 else
7056 copy->source_file = xstrdup (orig->source_file);
7057
7058 copy->line_number = orig->line_number;
7059 copy->frame_id = orig->frame_id;
7060 copy->thread = orig->thread;
6c95b8df 7061 copy->pspace = orig->pspace;
e58b0e63
PA
7062
7063 copy->enable_state = bp_enabled;
7064 copy->disposition = disp_donttouch;
7065 copy->number = internal_breakpoint_number--;
7066
7067 update_global_location_list_nothrow (0);
7068 return copy;
7069}
7070
611c83ae 7071struct breakpoint *
a6d9a66e
UW
7072set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7073 enum bptype type)
611c83ae
PA
7074{
7075 struct symtab_and_line sal;
7076
7077 sal = find_pc_line (pc, 0);
7078 sal.pc = pc;
7079 sal.section = find_pc_overlay (pc);
7080 sal.explicit_pc = 1;
7081
a6d9a66e 7082 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7083}
c906108c 7084\f
c5aa993b 7085
c906108c
SS
7086/* Tell the user we have just set a breakpoint B. */
7087
7088static void
fba45db2 7089mention (struct breakpoint *b)
c906108c
SS
7090{
7091 int say_where = 0;
fa8a61dc 7092 struct cleanup *ui_out_chain;
79a45b7d
TT
7093 struct value_print_options opts;
7094
7095 get_user_print_options (&opts);
8b93c638 7096
9a4105ab
AC
7097 /* FIXME: This is misplaced; mention() is called by things (like
7098 hitting a watchpoint) other than breakpoint creation. It should
7099 be possible to clean this up and at the same time replace the
7f4b89d1 7100 random calls to breakpoint_changed with this hook. */
383f836e 7101 observer_notify_breakpoint_created (b->number);
c906108c 7102
3086aeae
DJ
7103 if (b->ops != NULL && b->ops->print_mention != NULL)
7104 b->ops->print_mention (b);
7105 else
7106 switch (b->type)
7107 {
7108 case bp_none:
3e43a32a
MS
7109 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7110 b->number);
3086aeae
DJ
7111 break;
7112 case bp_watchpoint:
7113 ui_out_text (uiout, "Watchpoint ");
7114 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7115 ui_out_field_int (uiout, "number", b->number);
7116 ui_out_text (uiout, ": ");
fa8a61dc 7117 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7118 do_cleanups (ui_out_chain);
7119 break;
7120 case bp_hardware_watchpoint:
7121 ui_out_text (uiout, "Hardware watchpoint ");
7122 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7123 ui_out_field_int (uiout, "number", b->number);
7124 ui_out_text (uiout, ": ");
fa8a61dc 7125 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7126 do_cleanups (ui_out_chain);
7127 break;
7128 case bp_read_watchpoint:
7129 ui_out_text (uiout, "Hardware read watchpoint ");
7130 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7131 ui_out_field_int (uiout, "number", b->number);
7132 ui_out_text (uiout, ": ");
fa8a61dc 7133 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7134 do_cleanups (ui_out_chain);
7135 break;
7136 case bp_access_watchpoint:
7137 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7138 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7139 ui_out_field_int (uiout, "number", b->number);
7140 ui_out_text (uiout, ": ");
fa8a61dc 7141 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7142 do_cleanups (ui_out_chain);
7143 break;
7144 case bp_breakpoint:
0e30163f 7145 case bp_gnu_ifunc_resolver:
3086aeae
DJ
7146 if (ui_out_is_mi_like_p (uiout))
7147 {
7148 say_where = 0;
7149 break;
7150 }
2cec12e5
AR
7151 if (b->disposition == disp_del)
7152 printf_filtered (_("Temporary breakpoint"));
7153 else
7154 printf_filtered (_("Breakpoint"));
7155 printf_filtered (_(" %d"), b->number);
0e30163f
JK
7156 if (b->type == bp_gnu_ifunc_resolver)
7157 printf_filtered (_(" at gnu-indirect-function resolver"));
3086aeae
DJ
7158 say_where = 1;
7159 break;
7160 case bp_hardware_breakpoint:
7161 if (ui_out_is_mi_like_p (uiout))
7162 {
7163 say_where = 0;
7164 break;
7165 }
a3f17187 7166 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7167 say_where = 1;
7168 break;
1042e4c0
SS
7169 case bp_tracepoint:
7170 if (ui_out_is_mi_like_p (uiout))
7171 {
7172 say_where = 0;
7173 break;
7174 }
7175 printf_filtered (_("Tracepoint"));
7176 printf_filtered (_(" %d"), b->number);
7177 say_where = 1;
7178 break;
7a697b8d
SS
7179 case bp_fast_tracepoint:
7180 if (ui_out_is_mi_like_p (uiout))
7181 {
7182 say_where = 0;
7183 break;
7184 }
7185 printf_filtered (_("Fast tracepoint"));
7186 printf_filtered (_(" %d"), b->number);
7187 say_where = 1;
7188 break;
0fb4aa4b
PA
7189 case bp_static_tracepoint:
7190 if (ui_out_is_mi_like_p (uiout))
7191 {
7192 say_where = 0;
7193 break;
7194 }
7195 printf_filtered (_("Static tracepoint"));
7196 printf_filtered (_(" %d"), b->number);
7197 say_where = 1;
7198 break;
3086aeae
DJ
7199
7200 case bp_until:
7201 case bp_finish:
7202 case bp_longjmp:
7203 case bp_longjmp_resume:
186c406b
TT
7204 case bp_exception:
7205 case bp_exception_resume:
3086aeae 7206 case bp_step_resume:
3086aeae 7207 case bp_call_dummy:
aa7d318d 7208 case bp_std_terminate:
3086aeae
DJ
7209 case bp_watchpoint_scope:
7210 case bp_shlib_event:
7211 case bp_thread_event:
7212 case bp_overlay_event:
4efc6507 7213 case bp_jit_event:
0fd8e87f 7214 case bp_longjmp_master:
aa7d318d 7215 case bp_std_terminate_master:
186c406b 7216 case bp_exception_master:
0e30163f 7217 case bp_gnu_ifunc_resolver_return:
3086aeae
DJ
7218 break;
7219 }
c906108c 7220
c906108c
SS
7221 if (say_where)
7222 {
a3f17187
AC
7223 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7224 single string. */
0d381245 7225 if (b->loc == NULL)
c906108c 7226 {
a3f17187 7227 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7228 }
7229 else
7230 {
79a45b7d 7231 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7232 {
7233 printf_filtered (" at ");
5af949e3
UW
7234 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7235 gdb_stdout);
0101ce28
JJ
7236 }
7237 if (b->source_file)
7238 printf_filtered (": file %s, line %d.",
7239 b->source_file, b->line_number);
0d381245
VP
7240
7241 if (b->loc->next)
7242 {
7243 struct bp_location *loc = b->loc;
7244 int n = 0;
7245 for (; loc; loc = loc->next)
7246 ++n;
7247 printf_filtered (" (%d locations)", n);
7248 }
7249
c906108c 7250 }
c906108c 7251 }
9dc5e2a9 7252 if (ui_out_is_mi_like_p (uiout))
fb40c209 7253 return;
c906108c
SS
7254 printf_filtered ("\n");
7255}
c906108c 7256\f
c5aa993b 7257
0d381245 7258static struct bp_location *
39d61571 7259add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7260 const struct symtab_and_line *sal)
7261{
7262 struct bp_location *loc, **tmp;
7263
39d61571 7264 loc = allocate_bp_location (b);
0d381245
VP
7265 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7266 ;
7267 *tmp = loc;
a6d9a66e
UW
7268 loc->gdbarch = get_sal_arch (*sal);
7269 if (!loc->gdbarch)
7270 loc->gdbarch = b->gdbarch;
0d381245 7271 loc->requested_address = sal->pc;
a6d9a66e
UW
7272 loc->address = adjust_breakpoint_address (loc->gdbarch,
7273 loc->requested_address, b->type);
6c95b8df
PA
7274 loc->pspace = sal->pspace;
7275 gdb_assert (loc->pspace != NULL);
0d381245
VP
7276 loc->section = sal->section;
7277
0e30163f
JK
7278 set_breakpoint_location_function (loc,
7279 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7280 return loc;
7281}
514f746b
AR
7282\f
7283
7284/* Return 1 if LOC is pointing to a permanent breakpoint,
7285 return 0 otherwise. */
7286
7287static int
7288bp_loc_is_permanent (struct bp_location *loc)
7289{
7290 int len;
7291 CORE_ADDR addr;
7292 const gdb_byte *brk;
7293 gdb_byte *target_mem;
939c61fa
JK
7294 struct cleanup *cleanup;
7295 int retval = 0;
514f746b
AR
7296
7297 gdb_assert (loc != NULL);
7298
7299 addr = loc->address;
a6d9a66e 7300 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7301
939c61fa
JK
7302 /* Software breakpoints unsupported? */
7303 if (brk == NULL)
7304 return 0;
7305
514f746b
AR
7306 target_mem = alloca (len);
7307
939c61fa
JK
7308 /* Enable the automatic memory restoration from breakpoints while
7309 we read the memory. Otherwise we could say about our temporary
7310 breakpoints they are permanent. */
6c95b8df
PA
7311 cleanup = save_current_space_and_thread ();
7312
7313 switch_to_program_space_and_thread (loc->pspace);
7314 make_show_memory_breakpoints_cleanup (0);
939c61fa 7315
514f746b
AR
7316 if (target_read_memory (loc->address, target_mem, len) == 0
7317 && memcmp (target_mem, brk, len) == 0)
939c61fa 7318 retval = 1;
514f746b 7319
939c61fa
JK
7320 do_cleanups (cleanup);
7321
7322 return retval;
514f746b
AR
7323}
7324
7325
c3f6f71d 7326
018d34a4
VP
7327/* Create a breakpoint with SAL as location. Use ADDR_STRING
7328 as textual description of the location, and COND_STRING
db107f19 7329 as condition expression. */
018d34a4
VP
7330
7331static void
8cdf0e15
VP
7332create_breakpoint_sal (struct gdbarch *gdbarch,
7333 struct symtabs_and_lines sals, char *addr_string,
7334 char *cond_string,
7335 enum bptype type, enum bpdisp disposition,
7336 int thread, int task, int ignore_count,
84f4c1fe 7337 struct breakpoint_ops *ops, int from_tty,
56435ebe 7338 int enabled, int internal, int display_canonical)
018d34a4 7339{
0d381245
VP
7340 struct breakpoint *b = NULL;
7341 int i;
018d34a4
VP
7342
7343 if (type == bp_hardware_breakpoint)
7344 {
7345 int i = hw_breakpoint_used_count ();
7346 int target_resources_ok =
d92524f1 7347 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7348 i + 1, 0);
7349 if (target_resources_ok == 0)
7350 error (_("No hardware breakpoint support in the target."));
7351 else if (target_resources_ok < 0)
7352 error (_("Hardware breakpoints used exceeds limit."));
7353 }
7354
6c95b8df
PA
7355 gdb_assert (sals.nelts > 0);
7356
0d381245
VP
7357 for (i = 0; i < sals.nelts; ++i)
7358 {
7359 struct symtab_and_line sal = sals.sals[i];
7360 struct bp_location *loc;
7361
7362 if (from_tty)
5af949e3
UW
7363 {
7364 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7365 if (!loc_gdbarch)
7366 loc_gdbarch = gdbarch;
7367
7368 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7369 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7370 }
0d381245
VP
7371
7372 if (i == 0)
7373 {
a6d9a66e 7374 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7375 set_breakpoint_number (internal, b);
0d381245 7376 b->thread = thread;
4a306c9a 7377 b->task = task;
018d34a4 7378
0d381245
VP
7379 b->cond_string = cond_string;
7380 b->ignore_count = ignore_count;
41447f92 7381 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7382 b->disposition = disposition;
6c95b8df
PA
7383 b->pspace = sals.sals[0].pspace;
7384
0fb4aa4b
PA
7385 if (type == bp_static_tracepoint)
7386 {
7387 struct static_tracepoint_marker marker;
7388
7389 if (is_marker_spec (addr_string))
7390 {
7391 /* We already know the marker exists, otherwise, we
7392 wouldn't see a sal for it. */
7393 char *p = &addr_string[3];
7394 char *endp;
7395 char *marker_str;
7396 int i;
7397
e9cafbcc 7398 p = skip_spaces (p);
0fb4aa4b 7399
e9cafbcc 7400 endp = skip_to_space (p);
0fb4aa4b
PA
7401
7402 marker_str = savestring (p, endp - p);
7403 b->static_trace_marker_id = marker_str;
7404
3e43a32a
MS
7405 printf_filtered (_("Probed static tracepoint "
7406 "marker \"%s\"\n"),
0fb4aa4b
PA
7407 b->static_trace_marker_id);
7408 }
7409 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7410 {
7411 b->static_trace_marker_id = xstrdup (marker.str_id);
7412 release_static_tracepoint_marker (&marker);
7413
3e43a32a
MS
7414 printf_filtered (_("Probed static tracepoint "
7415 "marker \"%s\"\n"),
0fb4aa4b
PA
7416 b->static_trace_marker_id);
7417 }
7418 else
3e43a32a
MS
7419 warning (_("Couldn't determine the static "
7420 "tracepoint marker to probe"));
0fb4aa4b
PA
7421 }
7422
6c95b8df 7423 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7424 && (b->type == bp_breakpoint
7425 || b->type == bp_hardware_breakpoint))
7426 b->enable_state = bp_startup_disabled;
7427
0d381245
VP
7428 loc = b->loc;
7429 }
7430 else
018d34a4 7431 {
39d61571 7432 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7433 }
7434
514f746b
AR
7435 if (bp_loc_is_permanent (loc))
7436 make_breakpoint_permanent (b);
7437
0d381245
VP
7438 if (b->cond_string)
7439 {
7440 char *arg = b->cond_string;
d32a6982 7441 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7442 if (*arg)
db107f19 7443 error (_("Garbage %s follows condition"), arg);
018d34a4 7444 }
0d381245 7445 }
018d34a4 7446
56435ebe 7447 b->display_canonical = display_canonical;
018d34a4
VP
7448 if (addr_string)
7449 b->addr_string = addr_string;
7450 else
7451 /* addr_string has to be used or breakpoint_re_set will delete
7452 me. */
5af949e3
UW
7453 b->addr_string
7454 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7455
604133b5 7456 b->ops = ops;
84f4c1fe
PM
7457 if (internal)
7458 /* Do not mention breakpoints with a negative number, but do
7459 notify observers. */
7460 observer_notify_breakpoint_created (b->number);
7461 else
7462 mention (b);
018d34a4
VP
7463}
7464
ed0616c6
VP
7465/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7466 elements to fill the void space. */
2c0b251b
PA
7467static void
7468remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7469{
7470 int i = index_to_remove+1;
7471 int last_index = sal->nelts-1;
7472
7473 for (;i <= last_index; ++i)
7474 sal->sals[i-1] = sal->sals[i];
7475
7476 --(sal->nelts);
7477}
7478
6c95b8df
PA
7479/* If appropriate, obtains all sals that correspond to the same file
7480 and line as SAL, in all program spaces. Users debugging with IDEs,
7481 will want to set a breakpoint at foo.c:line, and not really care
7482 about program spaces. This is done only if SAL does not have
7483 explicit PC and has line and file information. If we got just a
7484 single expanded sal, return the original.
ed0616c6 7485
6c95b8df
PA
7486 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7487 which the name of enclosing function is different from SAL. This
7488 makes sure that if we have breakpoint originally set in template
7489 instantiation, say foo<int>(), we won't expand SAL to locations at
7490 the same line in all existing instantiations of 'foo'. */
ed0616c6 7491
2c0b251b 7492static struct symtabs_and_lines
ed0616c6
VP
7493expand_line_sal_maybe (struct symtab_and_line sal)
7494{
7495 struct symtabs_and_lines expanded;
7496 CORE_ADDR original_pc = sal.pc;
7497 char *original_function = NULL;
7498 int found;
7499 int i;
6c95b8df 7500 struct cleanup *old_chain;
ed0616c6
VP
7501
7502 /* If we have explicit pc, don't expand.
7503 If we have no line number, we can't expand. */
7504 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7505 {
7506 expanded.nelts = 1;
7507 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7508 expanded.sals[0] = sal;
7509 return expanded;
7510 }
7511
7512 sal.pc = 0;
6c95b8df
PA
7513
7514 old_chain = save_current_space_and_thread ();
7515
7516 switch_to_program_space_and_thread (sal.pspace);
7517
ed0616c6 7518 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7519
7520 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7521 expanded = expand_line_sal (sal);
6c95b8df 7522
ed0616c6
VP
7523 if (expanded.nelts == 1)
7524 {
3dba1c98
JB
7525 /* We had one sal, we got one sal. Return that sal, adjusting it
7526 past the function prologue if necessary. */
ed0616c6
VP
7527 xfree (expanded.sals);
7528 expanded.nelts = 1;
7529 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7530 sal.pc = original_pc;
7531 expanded.sals[0] = sal;
3dba1c98 7532 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7533 do_cleanups (old_chain);
ed0616c6
VP
7534 return expanded;
7535 }
7536
7537 if (!sal.explicit_line)
7538 {
7539 CORE_ADDR func_addr, func_end;
7540 for (i = 0; i < expanded.nelts; ++i)
7541 {
7542 CORE_ADDR pc = expanded.sals[i].pc;
7543 char *this_function;
6c95b8df
PA
7544
7545 /* We need to switch threads as well since we're about to
7546 read memory. */
7547 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7548
ed0616c6
VP
7549 if (find_pc_partial_function (pc, &this_function,
7550 &func_addr, &func_end))
7551 {
059fb39f
PM
7552 if (this_function
7553 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7554 {
7555 remove_sal (&expanded, i);
7556 --i;
7557 }
ed0616c6
VP
7558 }
7559 }
7560 }
059acae7
UW
7561
7562 /* Skip the function prologue if necessary. */
7563 for (i = 0; i < expanded.nelts; ++i)
7564 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7565
6c95b8df
PA
7566 do_cleanups (old_chain);
7567
ed0616c6
VP
7568 if (expanded.nelts <= 1)
7569 {
4a64f543
MS
7570 /* This is un ugly workaround. If we get zero expanded sals
7571 then something is really wrong. Fix that by returning the
7572 original sal. */
7573
ed0616c6
VP
7574 xfree (expanded.sals);
7575 expanded.nelts = 1;
7576 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7577 sal.pc = original_pc;
7578 expanded.sals[0] = sal;
7579 return expanded;
7580 }
7581
7582 if (original_pc)
7583 {
7584 found = 0;
7585 for (i = 0; i < expanded.nelts; ++i)
7586 if (expanded.sals[i].pc == original_pc)
7587 {
7588 found = 1;
7589 break;
7590 }
7591 gdb_assert (found);
7592 }
7593
7594 return expanded;
7595}
7596
018d34a4
VP
7597/* Add SALS.nelts breakpoints to the breakpoint table. For each
7598 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7599 value. COND_STRING, if not NULL, specified the condition to be
7600 used for all breakpoints. Essentially the only case where
7601 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7602 function. In that case, it's still not possible to specify
7603 separate conditions for different overloaded functions, so
7604 we take just a single condition string.
7605
c3f6f71d 7606 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7607 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7608 array contents). If the function fails (error() is called), the
7609 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7610 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7611
7612static void
8cdf0e15 7613create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7614 struct symtabs_and_lines sals,
7615 struct linespec_result *canonical,
8cdf0e15
VP
7616 char *cond_string,
7617 enum bptype type, enum bpdisp disposition,
7618 int thread, int task, int ignore_count,
7619 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7620 int enabled, int internal)
c906108c 7621{
018d34a4 7622 int i;
cc59ec59 7623
018d34a4 7624 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7625 {
ed0616c6
VP
7626 struct symtabs_and_lines expanded =
7627 expand_line_sal_maybe (sals.sals[i]);
0d381245 7628
7efd8fc2 7629 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7630 cond_string, type, disposition,
84f4c1fe 7631 thread, task, ignore_count, ops,
56435ebe
TT
7632 from_tty, enabled, internal,
7633 canonical->special_display);
c3f6f71d 7634 }
c3f6f71d 7635}
c906108c 7636
9998af43 7637/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7638 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7639 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7640 address strings. ADDRESS points to the end of the SAL.
7641
7642 The array and the line spec strings are allocated on the heap, it is
7643 the caller's responsibility to free them. */
c906108c 7644
b9362cc7 7645static void
c3f6f71d
JM
7646parse_breakpoint_sals (char **address,
7647 struct symtabs_and_lines *sals,
7efd8fc2 7648 struct linespec_result *canonical,
0101ce28 7649 int *not_found_ptr)
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,
7efd8fc2 7701 default_breakpoint_line, canonical,
0101ce28 7702 not_found_ptr);
c906108c 7703 else
0101ce28 7704 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
7efd8fc2 7705 canonical, not_found_ptr);
c906108c 7706 }
4a64f543 7707 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2
TT
7708 if (sals->nelts > 0 && canonical->canonical == NULL)
7709 canonical->canonical = xcalloc (sals->nelts, sizeof (char **));
c3f6f71d 7710 if (addr_start != (*address))
c906108c 7711 {
c3f6f71d 7712 int i;
cc59ec59 7713
c3f6f71d 7714 for (i = 0; i < sals->nelts; i++)
c906108c 7715 {
4a64f543 7716 /* Add the string if not present. */
7efd8fc2
TT
7717 if (canonical->canonical[i] == NULL)
7718 canonical->canonical[i] = savestring (addr_start,
7719 (*address) - addr_start);
c906108c
SS
7720 }
7721 }
c3f6f71d 7722}
c906108c 7723
c906108c 7724
c3f6f71d 7725/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7726 inserted as a breakpoint. If it can't throw an error. */
c906108c 7727
b9362cc7 7728static void
23e7acfb 7729breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7730{
7731 int i;
cc59ec59 7732
c3f6f71d 7733 for (i = 0; i < sals->nelts; i++)
ee53e872 7734 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7735}
7736
7a697b8d
SS
7737/* Fast tracepoints may have restrictions on valid locations. For
7738 instance, a fast tracepoint using a jump instead of a trap will
7739 likely have to overwrite more bytes than a trap would, and so can
7740 only be placed where the instruction is longer than the jump, or a
7741 multi-instruction sequence does not have a jump into the middle of
7742 it, etc. */
7743
7744static void
7745check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7746 struct symtabs_and_lines *sals)
7747{
7748 int i, rslt;
7749 struct symtab_and_line *sal;
7750 char *msg;
7751 struct cleanup *old_chain;
7752
7753 for (i = 0; i < sals->nelts; i++)
7754 {
7755 sal = &sals->sals[i];
7756
7757 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7758 NULL, &msg);
7759 old_chain = make_cleanup (xfree, msg);
7760
7761 if (!rslt)
7762 error (_("May not have a fast tracepoint at 0x%s%s"),
7763 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7764
7765 do_cleanups (old_chain);
7766 }
7767}
7768
018d34a4
VP
7769/* Given TOK, a string specification of condition and thread, as
7770 accepted by the 'break' command, extract the condition
7771 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7772 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7773 If no condition is found, *COND_STRING is set to NULL.
7774 If no thread is found, *THREAD is set to -1. */
7775static void
7776find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7777 char **cond_string, int *thread, int *task)
018d34a4
VP
7778{
7779 *cond_string = NULL;
7780 *thread = -1;
7781 while (tok && *tok)
7782 {
7783 char *end_tok;
7784 int toklen;
7785 char *cond_start = NULL;
7786 char *cond_end = NULL;
cc59ec59 7787
e9cafbcc 7788 tok = skip_spaces (tok);
018d34a4 7789
e9cafbcc 7790 end_tok = skip_to_space (tok);
018d34a4
VP
7791
7792 toklen = end_tok - tok;
7793
7794 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7795 {
f7545552
TT
7796 struct expression *expr;
7797
018d34a4 7798 tok = cond_start = end_tok + 1;
f7545552
TT
7799 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7800 xfree (expr);
018d34a4
VP
7801 cond_end = tok;
7802 *cond_string = savestring (cond_start,
7803 cond_end - cond_start);
7804 }
7805 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7806 {
7807 char *tmptok;
7808
7809 tok = end_tok + 1;
7810 tmptok = tok;
7811 *thread = strtol (tok, &tok, 0);
7812 if (tok == tmptok)
7813 error (_("Junk after thread keyword."));
7814 if (!valid_thread_id (*thread))
7815 error (_("Unknown thread %d."), *thread);
7816 }
4a306c9a
JB
7817 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7818 {
7819 char *tmptok;
7820
7821 tok = end_tok + 1;
7822 tmptok = tok;
7823 *task = strtol (tok, &tok, 0);
7824 if (tok == tmptok)
7825 error (_("Junk after task keyword."));
7826 if (!valid_task_id (*task))
b6199126 7827 error (_("Unknown task %d."), *task);
4a306c9a 7828 }
018d34a4
VP
7829 else
7830 error (_("Junk at end of arguments."));
7831 }
7832}
7833
0fb4aa4b
PA
7834/* Decode a static tracepoint marker spec. */
7835
7836static struct symtabs_and_lines
7837decode_static_tracepoint_spec (char **arg_p)
7838{
7839 VEC(static_tracepoint_marker_p) *markers = NULL;
7840 struct symtabs_and_lines sals;
7841 struct symtab_and_line sal;
7842 struct symbol *sym;
7843 struct cleanup *old_chain;
7844 char *p = &(*arg_p)[3];
7845 char *endp;
7846 char *marker_str;
7847 int i;
7848
e9cafbcc 7849 p = skip_spaces (p);
0fb4aa4b 7850
e9cafbcc 7851 endp = skip_to_space (p);
0fb4aa4b
PA
7852
7853 marker_str = savestring (p, endp - p);
7854 old_chain = make_cleanup (xfree, marker_str);
7855
7856 markers = target_static_tracepoint_markers_by_strid (marker_str);
7857 if (VEC_empty(static_tracepoint_marker_p, markers))
7858 error (_("No known static tracepoint marker named %s"), marker_str);
7859
7860 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7861 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7862
7863 for (i = 0; i < sals.nelts; i++)
7864 {
7865 struct static_tracepoint_marker *marker;
7866
7867 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7868
7869 init_sal (&sals.sals[i]);
7870
7871 sals.sals[i] = find_pc_line (marker->address, 0);
7872 sals.sals[i].pc = marker->address;
7873
7874 release_static_tracepoint_marker (marker);
7875 }
7876
7877 do_cleanups (old_chain);
7878
7879 *arg_p = endp;
7880 return sals;
7881}
7882
fd9b8c24
PA
7883/* Set a breakpoint. This function is shared between CLI and MI
7884 functions for setting a breakpoint. This function has two major
7885 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7886 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7887 breakpoint location, address and thread. Otherwise, ARG is just
7888 the location of breakpoint, with condition and thread specified by
7889 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7890 the breakpoint number will be allocated from the internal
7891 breakpoint count. Returns true if any breakpoint was created;
7892 false otherwise. */
0101ce28 7893
8cdf0e15
VP
7894int
7895create_breakpoint (struct gdbarch *gdbarch,
7896 char *arg, char *cond_string, int thread,
7897 int parse_condition_and_thread,
0fb4aa4b 7898 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7899 int ignore_count,
7900 enum auto_boolean pending_break_support,
7901 struct breakpoint_ops *ops,
84f4c1fe 7902 int from_tty, int enabled, int internal)
c3f6f71d 7903{
b78a6381 7904 volatile struct gdb_exception e;
c3f6f71d 7905 struct symtabs_and_lines sals;
0101ce28 7906 struct symtab_and_line pending_sal;
0101ce28 7907 char *copy_arg;
c3f6f71d 7908 char *addr_start = arg;
7efd8fc2 7909 struct linespec_result canonical;
c3f6f71d 7910 struct cleanup *old_chain;
80c99de1 7911 struct cleanup *bkpt_chain = NULL;
05ff989b 7912 int i;
0101ce28 7913 int pending = 0;
0101ce28 7914 int not_found = 0;
4a306c9a 7915 int task = 0;
86b17b60 7916 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7917
c3f6f71d
JM
7918 sals.sals = NULL;
7919 sals.nelts = 0;
7efd8fc2 7920 init_linespec_result (&canonical);
c3f6f71d 7921
0fb4aa4b
PA
7922 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7923 {
7924 int i;
7925
7926 sals = decode_static_tracepoint_spec (&arg);
7927
7928 copy_arg = savestring (addr_start, arg - addr_start);
7efd8fc2 7929 canonical.canonical = xcalloc (sals.nelts, sizeof (char **));
0fb4aa4b 7930 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7931 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7932 goto done;
7933 }
7934
b78a6381
TT
7935 TRY_CATCH (e, RETURN_MASK_ALL)
7936 {
7937 parse_breakpoint_sals (&arg, &sals, &canonical, &not_found);
7938 }
0101ce28
JJ
7939
7940 /* If caller is interested in rc value from parse, set value. */
05ff989b 7941 switch (e.reason)
0101ce28 7942 {
05ff989b 7943 case RETURN_QUIT:
98deb0da 7944 throw_exception (e);
05ff989b
AC
7945 case RETURN_ERROR:
7946 switch (e.error)
0101ce28 7947 {
05ff989b 7948 case NOT_FOUND_ERROR:
0101ce28 7949
05ff989b
AC
7950 /* If pending breakpoint support is turned off, throw
7951 error. */
fa8d40ab
JJ
7952
7953 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7954 throw_exception (e);
7955
7956 exception_print (gdb_stderr, e);
fa8d40ab 7957
05ff989b
AC
7958 /* If pending breakpoint support is auto query and the user
7959 selects no, then simply return the error code. */
059fb39f 7960 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7961 && !nquery (_("Make breakpoint pending on "
7962 "future shared library load? ")))
fd9b8c24 7963 return 0;
fa8d40ab 7964
05ff989b
AC
7965 /* At this point, either the user was queried about setting
7966 a pending breakpoint and selected yes, or pending
7967 breakpoint behavior is on and thus a pending breakpoint
7968 is defaulted on behalf of the user. */
0101ce28 7969 copy_arg = xstrdup (addr_start);
7efd8fc2 7970 canonical.canonical = &copy_arg;
0101ce28
JJ
7971 sals.nelts = 1;
7972 sals.sals = &pending_sal;
7973 pending_sal.pc = 0;
7974 pending = 1;
05ff989b
AC
7975 break;
7976 default:
98deb0da 7977 throw_exception (e);
0101ce28 7978 }
2abae994 7979 break;
05ff989b
AC
7980 default:
7981 if (!sals.nelts)
fd9b8c24 7982 return 0;
0101ce28 7983 }
c3f6f71d 7984
0fb4aa4b
PA
7985 done:
7986
4a64f543 7987 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7988 old_chain = make_cleanup (null_cleanup, 0);
7989
0101ce28
JJ
7990 if (!pending)
7991 {
7992 /* Make sure that all storage allocated to SALS gets freed. */
7993 make_cleanup (xfree, sals.sals);
7994
7efd8fc2
TT
7995 /* Cleanup the canonical array but not its contents. */
7996 make_cleanup (xfree, canonical.canonical);
0101ce28 7997 }
c3f6f71d 7998
c3f6f71d
JM
7999 /* ----------------------------- SNIP -----------------------------
8000 Anything added to the cleanup chain beyond this point is assumed
8001 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
8002 then the memory is not reclaimed. */
8003 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 8004
7efd8fc2 8005 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 8006 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
8007 for (i = 0; i < sals.nelts; i++)
8008 {
7efd8fc2
TT
8009 if (canonical.canonical[i] != NULL)
8010 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
8011 }
8012
8013 /* Resolve all line numbers to PC's and verify that the addresses
8014 are ok for the target. */
0101ce28 8015 if (!pending)
23e7acfb 8016 breakpoint_sals_to_pc (&sals);
c3f6f71d 8017
7a697b8d
SS
8018 /* Fast tracepoints may have additional restrictions on location. */
8019 if (type_wanted == bp_fast_tracepoint)
8020 check_fast_tracepoint_sals (gdbarch, &sals);
8021
c3f6f71d
JM
8022 /* Verify that condition can be parsed, before setting any
8023 breakpoints. Allocate a separate condition expression for each
4a64f543 8024 breakpoint. */
0101ce28 8025 if (!pending)
c3f6f71d 8026 {
2f069f6f 8027 if (parse_condition_and_thread)
72b2ff0e
VP
8028 {
8029 /* Here we only parse 'arg' to separate condition
8030 from thread number, so parsing in context of first
8031 sal is OK. When setting the breakpoint we'll
8032 re-parse it in context of each sal. */
8033 cond_string = NULL;
8034 thread = -1;
4a306c9a
JB
8035 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8036 &thread, &task);
72b2ff0e
VP
8037 if (cond_string)
8038 make_cleanup (xfree, cond_string);
8039 }
2f069f6f 8040 else
72b2ff0e
VP
8041 {
8042 /* Create a private copy of condition string. */
8043 if (cond_string)
8044 {
8045 cond_string = xstrdup (cond_string);
8046 make_cleanup (xfree, cond_string);
8047 }
8048 }
0fb4aa4b
PA
8049
8050 /* If the user is creating a static tracepoint by marker id
8051 (strace -m MARKER_ID), then store the sals index, so that
8052 breakpoint_re_set can try to match up which of the newly
8053 found markers corresponds to this one, and, don't try to
8054 expand multiple locations for each sal, given than SALS
8055 already should contain all sals for MARKER_ID. */
8056 if (type_wanted == bp_static_tracepoint
7efd8fc2 8057 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8058 {
8059 int i;
8060
8061 for (i = 0; i < sals.nelts; ++i)
8062 {
8063 struct symtabs_and_lines expanded;
8064 struct breakpoint *tp;
8065 struct cleanup *old_chain;
8066
8067 expanded.nelts = 1;
8068 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8069 expanded.sals[0] = sals.sals[i];
8070 old_chain = make_cleanup (xfree, expanded.sals);
8071
7efd8fc2 8072 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
8073 cond_string, type_wanted,
8074 tempflag ? disp_del : disp_donttouch,
8075 thread, task, ignore_count, ops,
56435ebe
TT
8076 from_tty, enabled, internal,
8077 canonical.special_display);
0fb4aa4b
PA
8078
8079 do_cleanups (old_chain);
8080
8081 /* Get the tracepoint we just created. */
84f4c1fe
PM
8082 if (internal)
8083 tp = get_breakpoint (internal_breakpoint_number);
8084 else
8085 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
8086 gdb_assert (tp != NULL);
8087
8088 /* Given that its possible to have multiple markers with
8089 the same string id, if the user is creating a static
8090 tracepoint by marker id ("strace -m MARKER_ID"), then
8091 store the sals index, so that breakpoint_re_set can
8092 try to match up which of the newly found markers
8093 corresponds to this one */
8094 tp->static_trace_marker_id_idx = i;
8095 }
8096 }
8097 else
7efd8fc2 8098 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8099 type_wanted,
8100 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8101 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8102 enabled, internal);
c906108c 8103 }
0101ce28
JJ
8104 else
8105 {
0101ce28
JJ
8106 struct breakpoint *b;
8107
0101ce28
JJ
8108 make_cleanup (xfree, copy_arg);
8109
a6d9a66e 8110 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 8111 set_breakpoint_number (internal, b);
72b2ff0e 8112 b->thread = -1;
7efd8fc2 8113 b->addr_string = canonical.canonical[0];
72b2ff0e 8114 b->cond_string = NULL;
0101ce28 8115 b->ignore_count = ignore_count;
0101ce28 8116 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8117 b->condition_not_parsed = 1;
604133b5 8118 b->ops = ops;
41447f92 8119 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8120 b->pspace = current_program_space;
84f4c1fe 8121 b->py_bp_object = NULL;
74960c60 8122
6c95b8df 8123 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8124 && (b->type == bp_breakpoint
8125 || b->type == bp_hardware_breakpoint))
8126 b->enable_state = bp_startup_disabled;
8127
84f4c1fe
PM
8128 if (internal)
8129 /* Do not mention breakpoints with a negative number,
8130 but do notify observers. */
8131 observer_notify_breakpoint_created (b->number);
8132 else
8133 mention (b);
0101ce28
JJ
8134 }
8135
c3f6f71d 8136 if (sals.nelts > 1)
95a42b64 8137 {
3e43a32a
MS
8138 warning (_("Multiple breakpoints were set.\nUse the "
8139 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8140 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8141 }
8142
80c99de1
PA
8143 /* That's it. Discard the cleanups for data inserted into the
8144 breakpoint. */
8145 discard_cleanups (bkpt_chain);
8146 /* But cleanup everything else. */
c3f6f71d 8147 do_cleanups (old_chain);
217dc9e2 8148
80c99de1 8149 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8150 update_global_location_list (1);
fd9b8c24
PA
8151
8152 return 1;
c3f6f71d 8153}
c906108c 8154
72b2ff0e
VP
8155/* Set a breakpoint.
8156 ARG is a string describing breakpoint address,
8157 condition, and thread.
8158 FLAG specifies if a breakpoint is hardware on,
8159 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8160 and BP_TEMPFLAG. */
8161
98deb0da 8162static void
72b2ff0e 8163break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8164{
72b2ff0e 8165 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8166 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8167 ? bp_hardware_breakpoint
8168 : bp_breakpoint);
c3f6f71d 8169
8cdf0e15
VP
8170 create_breakpoint (get_current_arch (),
8171 arg,
8172 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8173 tempflag, type_wanted,
8cdf0e15
VP
8174 0 /* Ignore count */,
8175 pending_break_support,
8176 NULL /* breakpoint_ops */,
8177 from_tty,
84f4c1fe
PM
8178 1 /* enabled */,
8179 0 /* internal */);
c906108c
SS
8180}
8181
72b2ff0e 8182
c906108c
SS
8183/* Helper function for break_command_1 and disassemble_command. */
8184
8185void
fba45db2 8186resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8187{
8188 CORE_ADDR pc;
8189
8190 if (sal->pc == 0 && sal->symtab != NULL)
8191 {
8192 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8193 error (_("No line %d in file \"%s\"."),
c906108c
SS
8194 sal->line, sal->symtab->filename);
8195 sal->pc = pc;
6a048695 8196
4a64f543
MS
8197 /* If this SAL corresponds to a breakpoint inserted using a line
8198 number, then skip the function prologue if necessary. */
6a048695 8199 if (sal->explicit_line)
059acae7 8200 skip_prologue_sal (sal);
c906108c
SS
8201 }
8202
8203 if (sal->section == 0 && sal->symtab != NULL)
8204 {
8205 struct blockvector *bv;
c5aa993b
JM
8206 struct block *b;
8207 struct symbol *sym;
c906108c 8208
801e3a5b 8209 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8210 if (bv != NULL)
8211 {
7f0df278 8212 sym = block_linkage_function (b);
c906108c
SS
8213 if (sym != NULL)
8214 {
8215 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8216 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8217 }
8218 else
8219 {
4a64f543
MS
8220 /* It really is worthwhile to have the section, so we'll
8221 just have to look harder. This case can be executed
8222 if we have line numbers but no functions (as can
8223 happen in assembly source). */
c906108c 8224
c5aa993b 8225 struct minimal_symbol *msym;
6c95b8df
PA
8226 struct cleanup *old_chain = save_current_space_and_thread ();
8227
8228 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8229
8230 msym = lookup_minimal_symbol_by_pc (sal->pc);
8231 if (msym)
714835d5 8232 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8233
8234 do_cleanups (old_chain);
c906108c
SS
8235 }
8236 }
8237 }
8238}
8239
8240void
fba45db2 8241break_command (char *arg, int from_tty)
c906108c 8242{
db107f19 8243 break_command_1 (arg, 0, from_tty);
c906108c
SS
8244}
8245
c906108c 8246void
fba45db2 8247tbreak_command (char *arg, int from_tty)
c906108c 8248{
db107f19 8249 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8250}
8251
c906108c 8252static void
fba45db2 8253hbreak_command (char *arg, int from_tty)
c906108c 8254{
db107f19 8255 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8256}
8257
8258static void
fba45db2 8259thbreak_command (char *arg, int from_tty)
c906108c 8260{
db107f19 8261 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8262}
8263
8264static void
fba45db2 8265stop_command (char *arg, int from_tty)
c906108c 8266{
a3f17187 8267 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8268Usage: stop in <function | address>\n\
a3f17187 8269 stop at <line>\n"));
c906108c
SS
8270}
8271
8272static void
fba45db2 8273stopin_command (char *arg, int from_tty)
c906108c
SS
8274{
8275 int badInput = 0;
8276
c5aa993b 8277 if (arg == (char *) NULL)
c906108c
SS
8278 badInput = 1;
8279 else if (*arg != '*')
8280 {
8281 char *argptr = arg;
8282 int hasColon = 0;
8283
4a64f543 8284 /* Look for a ':'. If this is a line number specification, then
53a5351d 8285 say it is bad, otherwise, it should be an address or
4a64f543 8286 function/method name. */
c906108c 8287 while (*argptr && !hasColon)
c5aa993b
JM
8288 {
8289 hasColon = (*argptr == ':');
8290 argptr++;
8291 }
c906108c
SS
8292
8293 if (hasColon)
c5aa993b 8294 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8295 else
c5aa993b 8296 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8297 }
8298
8299 if (badInput)
a3f17187 8300 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8301 else
db107f19 8302 break_command_1 (arg, 0, from_tty);
c906108c
SS
8303}
8304
8305static void
fba45db2 8306stopat_command (char *arg, int from_tty)
c906108c
SS
8307{
8308 int badInput = 0;
8309
c5aa993b 8310 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8311 badInput = 1;
8312 else
8313 {
8314 char *argptr = arg;
8315 int hasColon = 0;
8316
4a64f543
MS
8317 /* Look for a ':'. If there is a '::' then get out, otherwise
8318 it is probably a line number. */
c906108c 8319 while (*argptr && !hasColon)
c5aa993b
JM
8320 {
8321 hasColon = (*argptr == ':');
8322 argptr++;
8323 }
c906108c
SS
8324
8325 if (hasColon)
c5aa993b 8326 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8327 else
c5aa993b 8328 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8329 }
8330
8331 if (badInput)
a3f17187 8332 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8333 else
db107f19 8334 break_command_1 (arg, 0, from_tty);
c906108c
SS
8335}
8336
f1310107
TJB
8337/* Implement the "breakpoint_hit" breakpoint_ops method for
8338 ranged breakpoints. */
8339
8340static int
8341breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8342 struct address_space *aspace,
8343 CORE_ADDR bp_addr)
8344{
8345 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8346 bl->length, aspace, bp_addr);
8347}
8348
8349/* Implement the "resources_needed" breakpoint_ops method for
8350 ranged breakpoints. */
8351
8352static int
8353resources_needed_ranged_breakpoint (const struct bp_location *bl)
8354{
8355 return target_ranged_break_num_registers ();
8356}
8357
8358/* Implement the "print_it" breakpoint_ops method for
8359 ranged breakpoints. */
8360
8361static enum print_stop_action
8362print_it_ranged_breakpoint (struct breakpoint *b)
8363{
8364 struct bp_location *bl = b->loc;
8365
8366 gdb_assert (b->type == bp_hardware_breakpoint);
8367
8368 /* Ranged breakpoints have only one location. */
8369 gdb_assert (bl && bl->next == NULL);
8370
8371 annotate_breakpoint (b->number);
8372 if (b->disposition == disp_del)
8373 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8374 else
8375 ui_out_text (uiout, "\nRanged breakpoint ");
8376 if (ui_out_is_mi_like_p (uiout))
8377 {
8378 ui_out_field_string (uiout, "reason",
8379 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8380 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8381 }
8382 ui_out_field_int (uiout, "bkptno", b->number);
8383 ui_out_text (uiout, ", ");
8384
8385 return PRINT_SRC_AND_LOC;
8386}
8387
8388/* Implement the "print_one" breakpoint_ops method for
8389 ranged breakpoints. */
8390
8391static void
8392print_one_ranged_breakpoint (struct breakpoint *b,
8393 struct bp_location **last_loc)
8394{
8395 struct bp_location *bl = b->loc;
8396 struct value_print_options opts;
8397
8398 /* Ranged breakpoints have only one location. */
8399 gdb_assert (bl && bl->next == NULL);
8400
8401 get_user_print_options (&opts);
8402
8403 if (opts.addressprint)
8404 /* We don't print the address range here, it will be printed later
8405 by print_one_detail_ranged_breakpoint. */
8406 ui_out_field_skip (uiout, "addr");
8407 annotate_field (5);
8408 print_breakpoint_location (b, bl);
8409 *last_loc = bl;
8410}
8411
8412/* Implement the "print_one_detail" breakpoint_ops method for
8413 ranged breakpoints. */
8414
8415static void
8416print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8417 struct ui_out *uiout)
8418{
8419 CORE_ADDR address_start, address_end;
8420 struct bp_location *bl = b->loc;
8421 struct ui_stream *stb = ui_out_stream_new (uiout);
8422 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8423
8424 gdb_assert (bl);
8425
8426 address_start = bl->address;
8427 address_end = address_start + bl->length - 1;
8428
8429 ui_out_text (uiout, "\taddress range: ");
8430 fprintf_unfiltered (stb->stream, "[%s, %s]",
8431 print_core_address (bl->gdbarch, address_start),
8432 print_core_address (bl->gdbarch, address_end));
8433 ui_out_field_stream (uiout, "addr", stb);
8434 ui_out_text (uiout, "\n");
8435
8436 do_cleanups (cleanup);
8437}
8438
8439/* Implement the "print_mention" breakpoint_ops method for
8440 ranged breakpoints. */
8441
8442static void
8443print_mention_ranged_breakpoint (struct breakpoint *b)
8444{
8445 struct bp_location *bl = b->loc;
8446
8447 gdb_assert (bl);
8448 gdb_assert (b->type == bp_hardware_breakpoint);
8449
8450 if (ui_out_is_mi_like_p (uiout))
8451 return;
8452
8453 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8454 b->number, paddress (bl->gdbarch, bl->address),
8455 paddress (bl->gdbarch, bl->address + bl->length - 1));
8456}
8457
8458/* Implement the "print_recreate" breakpoint_ops method for
8459 ranged breakpoints. */
8460
8461static void
8462print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8463{
8464 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8465 b->addr_string_range_end);
8466}
8467
8468/* The breakpoint_ops structure to be used in ranged breakpoints. */
8469
8470static struct breakpoint_ops ranged_breakpoint_ops =
8471{
8472 NULL, /* insert */
8473 NULL, /* remove */
8474 breakpoint_hit_ranged_breakpoint,
8475 resources_needed_ranged_breakpoint,
8476 print_it_ranged_breakpoint,
8477 print_one_ranged_breakpoint,
8478 print_one_detail_ranged_breakpoint,
8479 print_mention_ranged_breakpoint,
8480 print_recreate_ranged_breakpoint
8481};
8482
8483/* Find the address where the end of the breakpoint range should be
8484 placed, given the SAL of the end of the range. This is so that if
8485 the user provides a line number, the end of the range is set to the
8486 last instruction of the given line. */
8487
8488static CORE_ADDR
8489find_breakpoint_range_end (struct symtab_and_line sal)
8490{
8491 CORE_ADDR end;
8492
8493 /* If the user provided a PC value, use it. Otherwise,
8494 find the address of the end of the given location. */
8495 if (sal.explicit_pc)
8496 end = sal.pc;
8497 else
8498 {
8499 int ret;
8500 CORE_ADDR start;
8501
8502 ret = find_line_pc_range (sal, &start, &end);
8503 if (!ret)
8504 error (_("Could not find location of the end of the range."));
8505
8506 /* find_line_pc_range returns the start of the next line. */
8507 end--;
8508 }
8509
8510 return end;
8511}
8512
8513/* Implement the "break-range" CLI command. */
8514
8515static void
8516break_range_command (char *arg, int from_tty)
8517{
8518 char *arg_start, *addr_string_start, *addr_string_end;
8519 struct linespec_result canonical_start, canonical_end;
8520 int bp_count, can_use_bp, length;
8521 CORE_ADDR end;
8522 struct breakpoint *b;
8523 struct symtab_and_line sal_start, sal_end;
8524 struct symtabs_and_lines sals_start, sals_end;
8525 struct cleanup *cleanup_bkpt;
8526
8527 /* We don't support software ranged breakpoints. */
8528 if (target_ranged_break_num_registers () < 0)
8529 error (_("This target does not support hardware ranged breakpoints."));
8530
8531 bp_count = hw_breakpoint_used_count ();
8532 bp_count += target_ranged_break_num_registers ();
8533 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8534 bp_count, 0);
8535 if (can_use_bp < 0)
8536 error (_("Hardware breakpoints used exceeds limit."));
8537
8538 if (arg == NULL || arg[0] == '\0')
8539 error(_("No address range specified."));
8540
8541 sals_start.sals = NULL;
8542 sals_start.nelts = 0;
8543 init_linespec_result (&canonical_start);
8544
8545 while (*arg == ' ' || *arg == '\t')
8546 arg++;
8547
8548 parse_breakpoint_sals (&arg, &sals_start, &canonical_start, NULL);
8549
8550 sal_start = sals_start.sals[0];
8551 addr_string_start = canonical_start.canonical[0];
8552 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8553 xfree (sals_start.sals);
8554 xfree (canonical_start.canonical);
8555
8556 if (arg[0] != ',')
8557 error (_("Too few arguments."));
8558 else if (sals_start.nelts == 0)
8559 error (_("Could not find location of the beginning of the range."));
8560 else if (sals_start.nelts != 1)
8561 error (_("Cannot create a ranged breakpoint with multiple locations."));
8562
8563 resolve_sal_pc (&sal_start);
8564
8565 arg++; /* Skip the comma. */
8566 while (*arg == ' ' || *arg == '\t')
8567 arg++;
8568
8569 /* Parse the end location. */
8570
8571 sals_end.sals = NULL;
8572 sals_end.nelts = 0;
8573 init_linespec_result (&canonical_end);
8574 arg_start = arg;
8575
423f41a5 8576 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8577 parse_breakpoint_sals because we need to specify the start location's
8578 symtab and line as the default symtab and line for the end of the
8579 range. This makes it possible to have ranges like "foo.c:27, +14",
8580 where +14 means 14 lines from the start location. */
8581 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
8582 &canonical_end, NULL);
8583
8584 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8585 if (canonical_end.canonical == NULL)
8586 canonical_end.canonical = xcalloc (1, sizeof (char **));
8587 /* Add the string if not present. */
8588 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8589 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8590
8591 sal_end = sals_end.sals[0];
8592 addr_string_end = canonical_end.canonical[0];
8593 make_cleanup (xfree, addr_string_end);
8594 xfree (sals_end.sals);
8595 xfree (canonical_end.canonical);
8596
8597 if (sals_end.nelts == 0)
8598 error (_("Could not find location of the end of the range."));
8599 else if (sals_end.nelts != 1)
8600 error (_("Cannot create a ranged breakpoint with multiple locations."));
8601
8602 resolve_sal_pc (&sal_end);
8603
8604 end = find_breakpoint_range_end (sal_end);
8605 if (sal_start.pc > end)
8606 error (_("Invalid address range, end preceeds start."));
8607
8608 length = end - sal_start.pc + 1;
8609 if (length < 0)
8610 /* Length overflowed. */
8611 error (_("Address range too large."));
8612 else if (length == 1)
8613 {
8614 /* This range is simple enough to be handled by
8615 the `hbreak' command. */
8616 hbreak_command (addr_string_start, 1);
8617
8618 do_cleanups (cleanup_bkpt);
8619
8620 return;
8621 }
8622
8623 /* Now set up the breakpoint. */
8624 b = set_raw_breakpoint (get_current_arch (), sal_start,
8625 bp_hardware_breakpoint);
8626 set_breakpoint_count (breakpoint_count + 1);
8627 b->number = breakpoint_count;
8628 b->disposition = disp_donttouch;
8629 b->addr_string = addr_string_start;
8630 b->addr_string_range_end = addr_string_end;
8631 b->ops = &ranged_breakpoint_ops;
8632 b->loc->length = length;
8633
8634 discard_cleanups (cleanup_bkpt);
8635
8636 mention (b);
8637 update_global_location_list (1);
8638}
8639
4a64f543
MS
8640/* Return non-zero if EXP is verified as constant. Returned zero
8641 means EXP is variable. Also the constant detection may fail for
8642 some constant expressions and in such case still falsely return
8643 zero. */
65d79d4b
SDJ
8644static int
8645watchpoint_exp_is_const (const struct expression *exp)
8646{
8647 int i = exp->nelts;
8648
8649 while (i > 0)
8650 {
8651 int oplenp, argsp;
8652
8653 /* We are only interested in the descriptor of each element. */
8654 operator_length (exp, i, &oplenp, &argsp);
8655 i -= oplenp;
8656
8657 switch (exp->elts[i].opcode)
8658 {
8659 case BINOP_ADD:
8660 case BINOP_SUB:
8661 case BINOP_MUL:
8662 case BINOP_DIV:
8663 case BINOP_REM:
8664 case BINOP_MOD:
8665 case BINOP_LSH:
8666 case BINOP_RSH:
8667 case BINOP_LOGICAL_AND:
8668 case BINOP_LOGICAL_OR:
8669 case BINOP_BITWISE_AND:
8670 case BINOP_BITWISE_IOR:
8671 case BINOP_BITWISE_XOR:
8672 case BINOP_EQUAL:
8673 case BINOP_NOTEQUAL:
8674 case BINOP_LESS:
8675 case BINOP_GTR:
8676 case BINOP_LEQ:
8677 case BINOP_GEQ:
8678 case BINOP_REPEAT:
8679 case BINOP_COMMA:
8680 case BINOP_EXP:
8681 case BINOP_MIN:
8682 case BINOP_MAX:
8683 case BINOP_INTDIV:
8684 case BINOP_CONCAT:
8685 case BINOP_IN:
8686 case BINOP_RANGE:
8687 case TERNOP_COND:
8688 case TERNOP_SLICE:
8689 case TERNOP_SLICE_COUNT:
8690
8691 case OP_LONG:
8692 case OP_DOUBLE:
8693 case OP_DECFLOAT:
8694 case OP_LAST:
8695 case OP_COMPLEX:
8696 case OP_STRING:
8697 case OP_BITSTRING:
8698 case OP_ARRAY:
8699 case OP_TYPE:
8700 case OP_NAME:
8701 case OP_OBJC_NSSTRING:
8702
8703 case UNOP_NEG:
8704 case UNOP_LOGICAL_NOT:
8705 case UNOP_COMPLEMENT:
8706 case UNOP_ADDR:
8707 case UNOP_HIGH:
4a64f543
MS
8708 /* Unary, binary and ternary operators: We have to check
8709 their operands. If they are constant, then so is the
8710 result of that operation. For instance, if A and B are
8711 determined to be constants, then so is "A + B".
8712
8713 UNOP_IND is one exception to the rule above, because the
8714 value of *ADDR is not necessarily a constant, even when
8715 ADDR is. */
65d79d4b
SDJ
8716 break;
8717
8718 case OP_VAR_VALUE:
8719 /* Check whether the associated symbol is a constant.
4a64f543 8720
65d79d4b 8721 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8722 possible that a buggy compiler could mark a variable as
8723 constant even when it is not, and TYPE_CONST would return
8724 true in this case, while SYMBOL_CLASS wouldn't.
8725
8726 We also have to check for function symbols because they
8727 are always constant. */
65d79d4b
SDJ
8728 {
8729 struct symbol *s = exp->elts[i + 2].symbol;
8730
8731 if (SYMBOL_CLASS (s) != LOC_BLOCK
8732 && SYMBOL_CLASS (s) != LOC_CONST
8733 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8734 return 0;
8735 break;
8736 }
8737
8738 /* The default action is to return 0 because we are using
8739 the optimistic approach here: If we don't know something,
8740 then it is not a constant. */
8741 default:
8742 return 0;
8743 }
8744 }
8745
8746 return 1;
8747}
8748
77b06cd7
TJB
8749/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8750
8751static int
8752insert_watchpoint (struct bp_location *bl)
8753{
e09342b5
TJB
8754 int length = bl->owner->exact? 1 : bl->length;
8755
8756 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8757 bl->owner->cond_exp);
77b06cd7
TJB
8758}
8759
8760/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8761
8762static int
8763remove_watchpoint (struct bp_location *bl)
8764{
e09342b5
TJB
8765 int length = bl->owner->exact? 1 : bl->length;
8766
8767 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8768 bl->owner->cond_exp);
8769}
8770
8771/* Implement the "resources_needed" breakpoint_ops method for
8772 hardware watchpoints. */
8773
8774static int
8775resources_needed_watchpoint (const struct bp_location *bl)
8776{
90e4670f 8777 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8778
90e4670f 8779 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8780}
8781
8782/* The breakpoint_ops structure to be used in hardware watchpoints. */
8783
8784static struct breakpoint_ops watchpoint_breakpoint_ops =
8785{
8786 insert_watchpoint,
8787 remove_watchpoint,
8788 NULL, /* breakpoint_hit */
e09342b5 8789 resources_needed_watchpoint,
77b06cd7
TJB
8790 NULL, /* print_it */
8791 NULL, /* print_one */
f1310107 8792 NULL, /* print_one_detail */
77b06cd7
TJB
8793 NULL, /* print_mention */
8794 NULL /* print_recreate */
8795};
8796
53a5351d
JM
8797/* accessflag: hw_write: watch write,
8798 hw_read: watch read,
8799 hw_access: watch access (read or write) */
c906108c 8800static void
84f4c1fe
PM
8801watch_command_1 (char *arg, int accessflag, int from_tty,
8802 int just_location, int internal)
c906108c 8803{
d983da9c 8804 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 8805 struct expression *exp;
60e1c644 8806 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 8807 struct value *val, *mark, *result;
c906108c 8808 struct frame_info *frame;
c906108c
SS
8809 char *exp_start = NULL;
8810 char *exp_end = NULL;
37e4754d 8811 char *tok, *id_tok_start, *end_tok;
c906108c
SS
8812 int toklen;
8813 char *cond_start = NULL;
8814 char *cond_end = NULL;
c906108c
SS
8815 int i, other_type_used, target_resources_ok = 0;
8816 enum bptype bp_type;
e09342b5 8817 int reg_cnt = 0;
37e4754d 8818 int thread = -1;
0cf6dd15 8819 int pc = 0;
c906108c 8820
37e4754d
LM
8821 /* Make sure that we actually have parameters to parse. */
8822 if (arg != NULL && arg[0] != '\0')
8823 {
8824 toklen = strlen (arg); /* Size of argument list. */
8825
8826 /* Points tok to the end of the argument list. */
8827 tok = arg + toklen - 1;
8828
4a64f543
MS
8829 /* Go backwards in the parameters list. Skip the last
8830 parameter. If we're expecting a 'thread <thread_num>'
8831 parameter, this should be the thread identifier. */
37e4754d
LM
8832 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8833 tok--;
8834 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8835 tok--;
8836
8837 /* Points end_tok to the beginning of the last token. */
8838 id_tok_start = tok + 1;
8839
4a64f543
MS
8840 /* Go backwards in the parameters list. Skip one more
8841 parameter. If we're expecting a 'thread <thread_num>'
8842 parameter, we should reach a "thread" token. */
37e4754d
LM
8843 while (tok > arg && (*tok == ' ' || *tok == '\t'))
8844 tok--;
8845
8846 end_tok = tok;
8847
8848 while (tok > arg && (*tok != ' ' && *tok != '\t'))
8849 tok--;
8850
8851 /* Move the pointer forward to skip the whitespace and
8852 calculate the length of the token. */
8853 tok++;
8854 toklen = end_tok - tok;
8855
8856 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8857 {
8858 /* At this point we've found a "thread" token, which means
8859 the user is trying to set a watchpoint that triggers
8860 only in a specific thread. */
8861 char *endp;
8862
8863 /* Extract the thread ID from the next token. */
8864 thread = strtol (id_tok_start, &endp, 0);
8865
8866 /* Check if the user provided a valid numeric value for the
8867 thread ID. */
8868 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
8869 error (_("Invalid thread ID specification %s."), id_tok_start);
8870
8871 /* Check if the thread actually exists. */
8872 if (!valid_thread_id (thread))
8873 error (_("Unknown thread %d."), thread);
8874
8875 /* Truncate the string and get rid of the thread <thread_num>
8876 parameter before the parameter list is parsed by the
8877 evaluate_expression() function. */
8878 *tok = '\0';
8879 }
8880 }
8881
8882 /* Parse the rest of the arguments. */
c906108c
SS
8883 innermost_block = NULL;
8884 exp_start = arg;
8885 exp = parse_exp_1 (&arg, 0, 0);
8886 exp_end = arg;
fa8a61dc
TT
8887 /* Remove trailing whitespace from the expression before saving it.
8888 This makes the eventual display of the expression string a bit
8889 prettier. */
8890 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
8891 --exp_end;
8892
65d79d4b
SDJ
8893 /* Checking if the expression is not constant. */
8894 if (watchpoint_exp_is_const (exp))
8895 {
8896 int len;
8897
8898 len = exp_end - exp_start;
8899 while (len > 0 && isspace (exp_start[len - 1]))
8900 len--;
8901 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
8902 }
8903
c906108c
SS
8904 exp_valid_block = innermost_block;
8905 mark = value_mark ();
a1442452 8906 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
8907
8908 if (just_location)
8909 {
8910 exp_valid_block = NULL;
a1442452 8911 val = value_addr (result);
06a64a0b
TT
8912 release_value (val);
8913 value_free_to_mark (mark);
8914 }
8915 else if (val != NULL)
fa4727a6 8916 release_value (val);
c906108c 8917
e9cafbcc
TT
8918 tok = skip_spaces (arg);
8919 end_tok = skip_to_space (tok);
c906108c
SS
8920
8921 toklen = end_tok - tok;
8922 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8923 {
2d134ed3
PA
8924 struct expression *cond;
8925
60e1c644 8926 innermost_block = NULL;
c906108c
SS
8927 tok = cond_start = end_tok + 1;
8928 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
8929
8930 /* The watchpoint expression may not be local, but the condition
8931 may still be. E.g.: `watch global if local > 0'. */
8932 cond_exp_valid_block = innermost_block;
8933
2d134ed3 8934 xfree (cond);
c906108c
SS
8935 cond_end = tok;
8936 }
8937 if (*tok)
8a3fe4f8 8938 error (_("Junk at end of command."));
c906108c 8939
53a5351d 8940 if (accessflag == hw_read)
c5aa993b 8941 bp_type = bp_read_watchpoint;
53a5351d 8942 else if (accessflag == hw_access)
c5aa993b
JM
8943 bp_type = bp_access_watchpoint;
8944 else
8945 bp_type = bp_hardware_watchpoint;
c906108c 8946
e09342b5
TJB
8947 reg_cnt = can_use_hardware_watchpoint (val, target_exact_watchpoints);
8948 if (reg_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8949 error (_("Expression cannot be implemented with read/access watchpoint."));
e09342b5 8950 if (reg_cnt != 0)
c5aa993b
JM
8951 {
8952 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 8953 target_resources_ok =
e09342b5 8954 target_can_use_hardware_watchpoint (bp_type, i + reg_cnt,
53a5351d 8955 other_type_used);
c5aa993b 8956 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 8957 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 8958
c5aa993b 8959 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
3e43a32a
MS
8960 error (_("Target can only support one kind "
8961 "of HW watchpoint at a time."));
c5aa993b 8962 }
c906108c 8963
4a64f543
MS
8964 /* Change the type of breakpoint to an ordinary watchpoint if a
8965 hardware watchpoint could not be set. */
e09342b5 8966 if (!reg_cnt || target_resources_ok <= 0)
4d28f7a8
KB
8967 bp_type = bp_watchpoint;
8968
d983da9c 8969 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
8970
8971 /* If the expression is "local", then set up a "watchpoint scope"
8972 breakpoint at the point where we've left the scope of the watchpoint
8973 expression. Create the scope breakpoint before the watchpoint, so
8974 that we will encounter it first in bpstat_stop_status. */
60e1c644 8975 if (exp_valid_block && frame)
d983da9c 8976 {
edb3359d
DJ
8977 if (frame_id_p (frame_unwind_caller_id (frame)))
8978 {
8979 scope_breakpoint
a6d9a66e
UW
8980 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
8981 frame_unwind_caller_pc (frame),
edb3359d 8982 bp_watchpoint_scope);
d983da9c 8983
edb3359d 8984 scope_breakpoint->enable_state = bp_enabled;
d983da9c 8985
edb3359d
DJ
8986 /* Automatically delete the breakpoint when it hits. */
8987 scope_breakpoint->disposition = disp_del;
d983da9c 8988
edb3359d
DJ
8989 /* Only break in the proper frame (help with recursion). */
8990 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 8991
edb3359d 8992 /* Set the address at which we will stop. */
a6d9a66e
UW
8993 scope_breakpoint->loc->gdbarch
8994 = frame_unwind_caller_arch (frame);
edb3359d
DJ
8995 scope_breakpoint->loc->requested_address
8996 = frame_unwind_caller_pc (frame);
8997 scope_breakpoint->loc->address
a6d9a66e
UW
8998 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
8999 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9000 scope_breakpoint->type);
9001 }
d983da9c
DJ
9002 }
9003
c906108c 9004 /* Now set up the breakpoint. */
2d134ed3 9005 b = set_raw_breakpoint_without_location (NULL, bp_type);
84f4c1fe 9006 set_breakpoint_number (internal, b);
37e4754d 9007 b->thread = thread;
b5de0fa7 9008 b->disposition = disp_donttouch;
c906108c
SS
9009 b->exp = exp;
9010 b->exp_valid_block = exp_valid_block;
60e1c644 9011 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9012 if (just_location)
9013 {
9014 struct type *t = value_type (val);
9015 CORE_ADDR addr = value_as_address (val);
9016 char *name;
9017
9018 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9019 name = type_to_string (t);
9020
d63d0675
JK
9021 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9022 core_addr_to_string (addr));
06a64a0b
TT
9023 xfree (name);
9024
d63d0675
JK
9025 b->exp_string = xstrprintf ("-location: %.*s",
9026 (int) (exp_end - exp_start), exp_start);
9027
06a64a0b
TT
9028 /* The above expression is in C. */
9029 b->language = language_c;
9030 }
9031 else
9032 b->exp_string = savestring (exp_start, exp_end - exp_start);
c906108c 9033 b->val = val;
fa4727a6 9034 b->val_valid = 1;
77b06cd7
TJB
9035 b->ops = &watchpoint_breakpoint_ops;
9036
e09342b5
TJB
9037 /* Use an exact watchpoint when there's only one memory region to be
9038 watched, and only one debug register is needed to watch it. */
9039 b->exact = target_exact_watchpoints && reg_cnt == 1;
9040
c906108c
SS
9041 if (cond_start)
9042 b->cond_string = savestring (cond_start, cond_end - cond_start);
9043 else
9044 b->cond_string = 0;
c5aa993b 9045
c906108c 9046 if (frame)
f6bc2008
PA
9047 {
9048 b->watchpoint_frame = get_frame_id (frame);
9049 b->watchpoint_thread = inferior_ptid;
9050 }
c906108c 9051 else
f6bc2008
PA
9052 {
9053 b->watchpoint_frame = null_frame_id;
9054 b->watchpoint_thread = null_ptid;
9055 }
c906108c 9056
d983da9c 9057 if (scope_breakpoint != NULL)
c906108c 9058 {
d983da9c
DJ
9059 /* The scope breakpoint is related to the watchpoint. We will
9060 need to act on them together. */
9061 b->related_breakpoint = scope_breakpoint;
9062 scope_breakpoint->related_breakpoint = b;
c906108c 9063 }
d983da9c 9064
06a64a0b
TT
9065 if (!just_location)
9066 value_free_to_mark (mark);
2d134ed3
PA
9067
9068 /* Finally update the new watchpoint. This creates the locations
9069 that should be inserted. */
9070 update_watchpoint (b, 1);
84f4c1fe
PM
9071 if (internal)
9072 /* Do not mention breakpoints with a negative number, but do
9073 notify observers. */
9074 observer_notify_breakpoint_created (b->number);
9075 else
9076 mention (b);
b60e7edf 9077 update_global_location_list (1);
c906108c
SS
9078}
9079
e09342b5
TJB
9080/* Return count of debug registers needed to watch the given expression.
9081 If EXACT_WATCHPOINTS is 1, then consider that only the address of
9082 the start of the watched region will be monitored (i.e., all accesses
9083 will be aligned). This uses less debug registers on some targets.
9084
9085 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9086
c906108c 9087static int
e09342b5 9088can_use_hardware_watchpoint (struct value *v, int exact_watchpoints)
c906108c
SS
9089{
9090 int found_memory_cnt = 0;
2e70b7b9 9091 struct value *head = v;
c906108c
SS
9092
9093 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9094 if (!can_use_hw_watchpoints)
c906108c 9095 return 0;
c5aa993b 9096
5c44784c
JM
9097 /* Make sure that the value of the expression depends only upon
9098 memory contents, and values computed from them within GDB. If we
9099 find any register references or function calls, we can't use a
9100 hardware watchpoint.
9101
9102 The idea here is that evaluating an expression generates a series
9103 of values, one holding the value of every subexpression. (The
9104 expression a*b+c has five subexpressions: a, b, a*b, c, and
9105 a*b+c.) GDB's values hold almost enough information to establish
9106 the criteria given above --- they identify memory lvalues,
9107 register lvalues, computed values, etcetera. So we can evaluate
9108 the expression, and then scan the chain of values that leaves
9109 behind to decide whether we can detect any possible change to the
9110 expression's final value using only hardware watchpoints.
9111
9112 However, I don't think that the values returned by inferior
9113 function calls are special in any way. So this function may not
9114 notice that an expression involving an inferior function call
9115 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9116 for (; v; v = value_next (v))
c906108c 9117 {
5c44784c 9118 if (VALUE_LVAL (v) == lval_memory)
c906108c 9119 {
8464be76
DJ
9120 if (v != head && value_lazy (v))
9121 /* A lazy memory lvalue in the chain is one that GDB never
9122 needed to fetch; we either just used its address (e.g.,
9123 `a' in `a.b') or we never needed it at all (e.g., `a'
9124 in `a,b'). This doesn't apply to HEAD; if that is
9125 lazy then it was not readable, but watch it anyway. */
5c44784c 9126 ;
53a5351d 9127 else
5c44784c
JM
9128 {
9129 /* Ahh, memory we actually used! Check if we can cover
9130 it with hardware watchpoints. */
df407dfe 9131 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9132
9133 /* We only watch structs and arrays if user asked for it
9134 explicitly, never if they just happen to appear in a
9135 middle of some value chain. */
9136 if (v == head
9137 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9138 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9139 {
42ae5230 9140 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9141 int len;
9142 int num_regs;
9143
9144 len = (exact_watchpoints
9145 && is_scalar_type_recursive (vtype))?
9146 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9147
e09342b5
TJB
9148 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9149 if (!num_regs)
2e70b7b9
MS
9150 return 0;
9151 else
e09342b5 9152 found_memory_cnt += num_regs;
2e70b7b9 9153 }
5c44784c 9154 }
c5aa993b 9155 }
5086187c
AC
9156 else if (VALUE_LVAL (v) != not_lval
9157 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9158 return 0; /* These are values from the history (e.g., $1). */
5086187c 9159 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9160 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9161 }
9162
9163 /* The expression itself looks suitable for using a hardware
9164 watchpoint, but give the target machine a chance to reject it. */
9165 return found_memory_cnt;
9166}
9167
8b93c638 9168void
84f4c1fe 9169watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9170{
84f4c1fe 9171 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9172}
9173
9174/* A helper function that looks for an argument at the start of a
9175 string. The argument must also either be at the end of the string,
9176 or be followed by whitespace. Returns 1 if it finds the argument,
9177 0 otherwise. If the argument is found, it updates *STR. */
9178
9179static int
9180check_for_argument (char **str, char *arg, int arg_len)
9181{
9182 if (strncmp (*str, arg, arg_len) == 0
9183 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9184 {
9185 *str += arg_len;
9186 return 1;
9187 }
9188 return 0;
9189}
9190
9191/* A helper function that looks for the "-location" argument and then
9192 calls watch_command_1. */
9193
9194static void
9195watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9196{
9197 int just_location = 0;
9198
9199 if (arg
9200 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9201 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9202 {
e9cafbcc 9203 arg = skip_spaces (arg);
06a64a0b
TT
9204 just_location = 1;
9205 }
9206
84f4c1fe 9207 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9208}
8926118c 9209
c5aa993b 9210static void
fba45db2 9211watch_command (char *arg, int from_tty)
c906108c 9212{
06a64a0b 9213 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9214}
9215
8b93c638 9216void
84f4c1fe 9217rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9218{
84f4c1fe 9219 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9220}
8926118c 9221
c5aa993b 9222static void
fba45db2 9223rwatch_command (char *arg, int from_tty)
c906108c 9224{
06a64a0b 9225 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9226}
9227
8b93c638 9228void
84f4c1fe 9229awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9230{
84f4c1fe 9231 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9232}
8926118c 9233
c5aa993b 9234static void
fba45db2 9235awatch_command (char *arg, int from_tty)
c906108c 9236{
06a64a0b 9237 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9238}
c906108c 9239\f
c5aa993b 9240
43ff13b4 9241/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9242 because it uses the mechanisms of breakpoints. */
9243
bfec99b2
PA
9244struct until_break_command_continuation_args
9245{
9246 struct breakpoint *breakpoint;
9247 struct breakpoint *breakpoint2;
186c406b 9248 int thread_num;
bfec99b2
PA
9249};
9250
43ff13b4 9251/* This function is called by fetch_inferior_event via the
4a64f543 9252 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9253 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9254 command. */
c2c6d25f 9255static void
604ead4a 9256until_break_command_continuation (void *arg)
43ff13b4 9257{
bfec99b2
PA
9258 struct until_break_command_continuation_args *a = arg;
9259
9260 delete_breakpoint (a->breakpoint);
9261 if (a->breakpoint2)
9262 delete_breakpoint (a->breakpoint2);
186c406b 9263 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9264}
9265
c906108c 9266void
ae66c1fc 9267until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9268{
9269 struct symtabs_and_lines sals;
9270 struct symtab_and_line sal;
206415a3 9271 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9272 struct breakpoint *breakpoint;
f107f563 9273 struct breakpoint *breakpoint2 = NULL;
c906108c 9274 struct cleanup *old_chain;
186c406b
TT
9275 int thread;
9276 struct thread_info *tp;
c906108c
SS
9277
9278 clear_proceed_status ();
9279
9280 /* Set a breakpoint where the user wants it and at return from
4a64f543 9281 this function. */
c5aa993b 9282
c906108c
SS
9283 if (default_breakpoint_valid)
9284 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
7efd8fc2 9285 default_breakpoint_line, NULL, NULL);
c906108c 9286 else
7efd8fc2 9287 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL, NULL);
c5aa993b 9288
c906108c 9289 if (sals.nelts != 1)
8a3fe4f8 9290 error (_("Couldn't get information on specified line."));
c5aa993b 9291
c906108c 9292 sal = sals.sals[0];
4a64f543 9293 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9294
c906108c 9295 if (*arg)
8a3fe4f8 9296 error (_("Junk at end of arguments."));
c5aa993b 9297
c906108c 9298 resolve_sal_pc (&sal);
c5aa993b 9299
ae66c1fc
EZ
9300 if (anywhere)
9301 /* If the user told us to continue until a specified location,
9302 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9303 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9304 null_frame_id, bp_until);
ae66c1fc 9305 else
4a64f543
MS
9306 /* Otherwise, specify the selected frame, because we want to stop
9307 only at the very same frame. */
a6d9a66e
UW
9308 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9309 get_stack_frame_id (frame),
ae66c1fc 9310 bp_until);
c5aa993b 9311
f107f563 9312 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9313
186c406b
TT
9314 tp = inferior_thread ();
9315 thread = tp->num;
9316
ae66c1fc
EZ
9317 /* Keep within the current frame, or in frames called by the current
9318 one. */
edb3359d
DJ
9319
9320 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9321 {
edb3359d
DJ
9322 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9323 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9324 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9325 sal,
edb3359d 9326 frame_unwind_caller_id (frame),
f107f563
VP
9327 bp_until);
9328 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9329
9330 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9331 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9332 }
c5aa993b 9333
c906108c 9334 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9335
4a64f543
MS
9336 /* If we are running asynchronously, and proceed call above has
9337 actually managed to start the target, arrange for breakpoints to
9338 be deleted when the target stops. Otherwise, we're already
9339 stopped and delete breakpoints via cleanup chain. */
f107f563 9340
8ea051c5 9341 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9342 {
bfec99b2
PA
9343 struct until_break_command_continuation_args *args;
9344 args = xmalloc (sizeof (*args));
f107f563 9345
bfec99b2
PA
9346 args->breakpoint = breakpoint;
9347 args->breakpoint2 = breakpoint2;
186c406b 9348 args->thread_num = thread;
f107f563
VP
9349
9350 discard_cleanups (old_chain);
95e54da7
PA
9351 add_continuation (inferior_thread (),
9352 until_break_command_continuation, args,
604ead4a 9353 xfree);
f107f563
VP
9354 }
9355 else
c5aa993b 9356 do_cleanups (old_chain);
c906108c 9357}
ae66c1fc 9358
c906108c
SS
9359/* This function attempts to parse an optional "if <cond>" clause
9360 from the arg string. If one is not found, it returns NULL.
c5aa993b 9361
c906108c
SS
9362 Else, it returns a pointer to the condition string. (It does not
9363 attempt to evaluate the string against a particular block.) And,
9364 it updates arg to point to the first character following the parsed
4a64f543 9365 if clause in the arg string. */
53a5351d 9366
c906108c 9367static char *
fba45db2 9368ep_parse_optional_if_clause (char **arg)
c906108c 9369{
c5aa993b
JM
9370 char *cond_string;
9371
9372 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9373 return NULL;
c5aa993b 9374
4a64f543 9375 /* Skip the "if" keyword. */
c906108c 9376 (*arg) += 2;
c5aa993b 9377
c906108c 9378 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9379 condition string. */
e9cafbcc 9380 *arg = skip_spaces (*arg);
c906108c 9381 cond_string = *arg;
c5aa993b 9382
4a64f543
MS
9383 /* Assume that the condition occupies the remainder of the arg
9384 string. */
c906108c 9385 (*arg) += strlen (cond_string);
c5aa993b 9386
c906108c
SS
9387 return cond_string;
9388}
c5aa993b 9389
c906108c
SS
9390/* Commands to deal with catching events, such as signals, exceptions,
9391 process start/exit, etc. */
c5aa993b
JM
9392
9393typedef enum
9394{
44feb3ce
TT
9395 catch_fork_temporary, catch_vfork_temporary,
9396 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9397}
9398catch_fork_kind;
9399
c906108c 9400static void
cc59ec59
MS
9401catch_fork_command_1 (char *arg, int from_tty,
9402 struct cmd_list_element *command)
c906108c 9403{
a6d9a66e 9404 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9405 char *cond_string = NULL;
44feb3ce
TT
9406 catch_fork_kind fork_kind;
9407 int tempflag;
9408
9409 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9410 tempflag = (fork_kind == catch_fork_temporary
9411 || fork_kind == catch_vfork_temporary);
c5aa993b 9412
44feb3ce
TT
9413 if (!arg)
9414 arg = "";
e9cafbcc 9415 arg = skip_spaces (arg);
c5aa993b 9416
c906108c 9417 /* The allowed syntax is:
c5aa993b
JM
9418 catch [v]fork
9419 catch [v]fork if <cond>
9420
4a64f543 9421 First, check if there's an if clause. */
c906108c 9422 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9423
c906108c 9424 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9425 error (_("Junk at end of arguments."));
c5aa993b 9426
c906108c 9427 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9428 and enable reporting of such events. */
c5aa993b
JM
9429 switch (fork_kind)
9430 {
44feb3ce
TT
9431 case catch_fork_temporary:
9432 case catch_fork_permanent:
a6d9a66e 9433 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9434 &catch_fork_breakpoint_ops);
c906108c 9435 break;
44feb3ce
TT
9436 case catch_vfork_temporary:
9437 case catch_vfork_permanent:
a6d9a66e 9438 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9439 &catch_vfork_breakpoint_ops);
c906108c 9440 break;
c5aa993b 9441 default:
8a3fe4f8 9442 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9443 break;
c5aa993b 9444 }
c906108c
SS
9445}
9446
9447static void
cc59ec59
MS
9448catch_exec_command_1 (char *arg, int from_tty,
9449 struct cmd_list_element *command)
c906108c 9450{
a6d9a66e 9451 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9452 int tempflag;
c5aa993b 9453 char *cond_string = NULL;
c906108c 9454
44feb3ce
TT
9455 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9456
9457 if (!arg)
9458 arg = "";
e9cafbcc 9459 arg = skip_spaces (arg);
c906108c
SS
9460
9461 /* The allowed syntax is:
c5aa993b
JM
9462 catch exec
9463 catch exec if <cond>
c906108c 9464
4a64f543 9465 First, check if there's an if clause. */
c906108c
SS
9466 cond_string = ep_parse_optional_if_clause (&arg);
9467
9468 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9469 error (_("Junk at end of arguments."));
c906108c
SS
9470
9471 /* If this target supports it, create an exec catchpoint
4a64f543 9472 and enable reporting of such events. */
a6d9a66e
UW
9473 create_catchpoint (gdbarch, tempflag, cond_string,
9474 &catch_exec_breakpoint_ops);
c906108c 9475}
c5aa993b 9476
3086aeae
DJ
9477static enum print_stop_action
9478print_exception_catchpoint (struct breakpoint *b)
9479{
ade92717 9480 int bp_temp, bp_throw;
3086aeae 9481
ade92717 9482 annotate_catchpoint (b->number);
3086aeae 9483
ade92717
AR
9484 bp_throw = strstr (b->addr_string, "throw") != NULL;
9485 if (b->loc->address != b->loc->requested_address)
9486 breakpoint_adjustment_warning (b->loc->requested_address,
9487 b->loc->address,
9488 b->number, 1);
df2b6d2d 9489 bp_temp = b->disposition == disp_del;
ade92717
AR
9490 ui_out_text (uiout,
9491 bp_temp ? "Temporary catchpoint "
9492 : "Catchpoint ");
9493 if (!ui_out_is_mi_like_p (uiout))
9494 ui_out_field_int (uiout, "bkptno", b->number);
9495 ui_out_text (uiout,
c0b37c48
AR
9496 bp_throw ? " (exception thrown), "
9497 : " (exception caught), ");
ade92717
AR
9498 if (ui_out_is_mi_like_p (uiout))
9499 {
9500 ui_out_field_string (uiout, "reason",
9501 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9502 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9503 ui_out_field_int (uiout, "bkptno", b->number);
9504 }
3086aeae
DJ
9505 return PRINT_SRC_AND_LOC;
9506}
9507
9508static void
cc59ec59
MS
9509print_one_exception_catchpoint (struct breakpoint *b,
9510 struct bp_location **last_loc)
3086aeae 9511{
79a45b7d 9512 struct value_print_options opts;
cc59ec59 9513
79a45b7d
TT
9514 get_user_print_options (&opts);
9515 if (opts.addressprint)
3086aeae
DJ
9516 {
9517 annotate_field (4);
604133b5
AR
9518 if (b->loc == NULL || b->loc->shlib_disabled)
9519 ui_out_field_string (uiout, "addr", "<PENDING>");
9520 else
5af949e3
UW
9521 ui_out_field_core_addr (uiout, "addr",
9522 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9523 }
9524 annotate_field (5);
604133b5 9525 if (b->loc)
a6d9a66e 9526 *last_loc = b->loc;
3086aeae
DJ
9527 if (strstr (b->addr_string, "throw") != NULL)
9528 ui_out_field_string (uiout, "what", "exception throw");
9529 else
9530 ui_out_field_string (uiout, "what", "exception catch");
9531}
9532
9533static void
9534print_mention_exception_catchpoint (struct breakpoint *b)
9535{
ade92717
AR
9536 int bp_temp;
9537 int bp_throw;
9538
df2b6d2d 9539 bp_temp = b->disposition == disp_del;
ade92717
AR
9540 bp_throw = strstr (b->addr_string, "throw") != NULL;
9541 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9542 : _("Catchpoint "));
9543 ui_out_field_int (uiout, "bkptno", b->number);
9544 ui_out_text (uiout, bp_throw ? _(" (throw)")
9545 : _(" (catch)"));
3086aeae
DJ
9546}
9547
6149aea9
PA
9548/* Implement the "print_recreate" breakpoint_ops method for throw and
9549 catch catchpoints. */
9550
9551static void
4a64f543
MS
9552print_recreate_exception_catchpoint (struct breakpoint *b,
9553 struct ui_file *fp)
6149aea9
PA
9554{
9555 int bp_temp;
9556 int bp_throw;
9557
9558 bp_temp = b->disposition == disp_del;
9559 bp_throw = strstr (b->addr_string, "throw") != NULL;
9560 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9561 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9562}
9563
3086aeae 9564static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
9565 NULL, /* insert */
9566 NULL, /* remove */
9567 NULL, /* breakpoint_hit */
e09342b5 9568 NULL, /* resources_needed */
3086aeae
DJ
9569 print_exception_catchpoint,
9570 print_one_exception_catchpoint,
f1310107 9571 NULL, /* print_one_detail */
6149aea9
PA
9572 print_mention_exception_catchpoint,
9573 print_recreate_exception_catchpoint
3086aeae
DJ
9574};
9575
9576static int
9577handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9578 enum exception_event_kind ex_event, int from_tty)
9579{
604133b5
AR
9580 char *trigger_func_name;
9581
3086aeae 9582 if (ex_event == EX_EVENT_CATCH)
604133b5 9583 trigger_func_name = "__cxa_begin_catch";
3086aeae 9584 else
604133b5 9585 trigger_func_name = "__cxa_throw";
3086aeae 9586
8cdf0e15
VP
9587 create_breakpoint (get_current_arch (),
9588 trigger_func_name, cond_string, -1,
9589 0 /* condition and thread are valid. */,
0fb4aa4b 9590 tempflag, bp_breakpoint,
8cdf0e15
VP
9591 0,
9592 AUTO_BOOLEAN_TRUE /* pending */,
9593 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9594 1 /* enabled */,
9595 0 /* internal */);
3086aeae 9596
3086aeae
DJ
9597 return 1;
9598}
9599
4a64f543 9600/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9601
9602static void
fba45db2
KB
9603catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9604 int tempflag, int from_tty)
c906108c 9605{
c5aa993b 9606 char *cond_string = NULL;
c5aa993b 9607
44feb3ce
TT
9608 if (!arg)
9609 arg = "";
e9cafbcc 9610 arg = skip_spaces (arg);
c5aa993b 9611
c906108c
SS
9612 cond_string = ep_parse_optional_if_clause (&arg);
9613
9614 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9615 error (_("Junk at end of arguments."));
c906108c 9616
059fb39f
PM
9617 if (ex_event != EX_EVENT_THROW
9618 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9619 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9620
3086aeae
DJ
9621 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9622 return;
9623
8a3fe4f8 9624 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9625}
9626
44feb3ce
TT
9627/* Implementation of "catch catch" command. */
9628
9629static void
9630catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9631{
9632 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9633
44feb3ce
TT
9634 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9635}
9636
9637/* Implementation of "catch throw" command. */
9638
9639static void
9640catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9641{
9642 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9643
44feb3ce
TT
9644 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9645}
9646
f7f9143b
JB
9647/* Create a breakpoint struct for Ada exception catchpoints. */
9648
9649static void
a6d9a66e
UW
9650create_ada_exception_breakpoint (struct gdbarch *gdbarch,
9651 struct symtab_and_line sal,
f7f9143b
JB
9652 char *addr_string,
9653 char *exp_string,
9654 char *cond_string,
9655 struct expression *cond,
9656 struct breakpoint_ops *ops,
9657 int tempflag,
9658 int from_tty)
9659{
9660 struct breakpoint *b;
9661
9662 if (from_tty)
9663 {
5af949e3
UW
9664 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9665 if (!loc_gdbarch)
9666 loc_gdbarch = gdbarch;
9667
6c95b8df
PA
9668 describe_other_breakpoints (loc_gdbarch,
9669 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9670 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9671 version for exception catchpoints, because two catchpoints
9672 used for different exception names will use the same address.
9673 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9674 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
9675 inapropriate, we should use the word catchpoint, and tell
9676 the user what type of catchpoint it is. The above is good
9677 enough for now, though. */
9678 }
9679
a6d9a66e 9680 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
f7f9143b
JB
9681 set_breakpoint_count (breakpoint_count + 1);
9682
9683 b->enable_state = bp_enabled;
9684 b->disposition = tempflag ? disp_del : disp_donttouch;
9685 b->number = breakpoint_count;
9686 b->ignore_count = 0;
511a6cd4 9687 b->loc->cond = cond;
f7f9143b
JB
9688 b->addr_string = addr_string;
9689 b->language = language_ada;
9690 b->cond_string = cond_string;
9691 b->exp_string = exp_string;
9692 b->thread = -1;
9693 b->ops = ops;
f7f9143b
JB
9694
9695 mention (b);
b60e7edf 9696 update_global_location_list (1);
f7f9143b
JB
9697}
9698
9699/* Implement the "catch exception" command. */
9700
9701static void
44feb3ce
TT
9702catch_ada_exception_command (char *arg, int from_tty,
9703 struct cmd_list_element *command)
f7f9143b 9704{
a6d9a66e 9705 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9706 int tempflag;
f7f9143b 9707 struct symtab_and_line sal;
f7f9143b
JB
9708 char *addr_string = NULL;
9709 char *exp_string = NULL;
9710 char *cond_string = NULL;
9711 struct expression *cond = NULL;
9712 struct breakpoint_ops *ops = NULL;
9713
44feb3ce
TT
9714 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9715
9716 if (!arg)
9717 arg = "";
f7f9143b
JB
9718 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
9719 &cond_string, &cond, &ops);
a6d9a66e 9720 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
f7f9143b
JB
9721 cond_string, cond, ops, tempflag,
9722 from_tty);
9723}
9724
a96d9b2e
SDJ
9725/* Cleanup function for a syscall filter list. */
9726static void
9727clean_up_filters (void *arg)
9728{
9729 VEC(int) *iter = *(VEC(int) **) arg;
9730 VEC_free (int, iter);
9731}
9732
9733/* Splits the argument using space as delimiter. Returns an xmalloc'd
9734 filter list, or NULL if no filtering is required. */
9735static VEC(int) *
9736catch_syscall_split_args (char *arg)
9737{
9738 VEC(int) *result = NULL;
9739 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9740
9741 while (*arg != '\0')
9742 {
9743 int i, syscall_number;
9744 char *endptr;
9745 char cur_name[128];
9746 struct syscall s;
9747
9748 /* Skip whitespace. */
9749 while (isspace (*arg))
9750 arg++;
9751
9752 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9753 cur_name[i] = arg[i];
9754 cur_name[i] = '\0';
9755 arg += i;
9756
9757 /* Check if the user provided a syscall name or a number. */
9758 syscall_number = (int) strtol (cur_name, &endptr, 0);
9759 if (*endptr == '\0')
bccd0dd2 9760 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
9761 else
9762 {
9763 /* We have a name. Let's check if it's valid and convert it
9764 to a number. */
9765 get_syscall_by_name (cur_name, &s);
9766
9767 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
9768 /* Here we have to issue an error instead of a warning,
9769 because GDB cannot do anything useful if there's no
9770 syscall number to be caught. */
a96d9b2e
SDJ
9771 error (_("Unknown syscall name '%s'."), cur_name);
9772 }
9773
9774 /* Ok, it's valid. */
9775 VEC_safe_push (int, result, s.number);
9776 }
9777
9778 discard_cleanups (cleanup);
9779 return result;
9780}
9781
9782/* Implement the "catch syscall" command. */
9783
9784static void
cc59ec59
MS
9785catch_syscall_command_1 (char *arg, int from_tty,
9786 struct cmd_list_element *command)
a96d9b2e
SDJ
9787{
9788 int tempflag;
9789 VEC(int) *filter;
9790 struct syscall s;
9791 struct gdbarch *gdbarch = get_current_arch ();
9792
9793 /* Checking if the feature if supported. */
9794 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
9795 error (_("The feature 'catch syscall' is not supported on \
ea666128 9796this architecture yet."));
a96d9b2e
SDJ
9797
9798 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9799
e9cafbcc 9800 arg = skip_spaces (arg);
a96d9b2e
SDJ
9801
9802 /* We need to do this first "dummy" translation in order
9803 to get the syscall XML file loaded or, most important,
9804 to display a warning to the user if there's no XML file
9805 for his/her architecture. */
9806 get_syscall_by_number (0, &s);
9807
9808 /* The allowed syntax is:
9809 catch syscall
9810 catch syscall <name | number> [<name | number> ... <name | number>]
9811
9812 Let's check if there's a syscall name. */
9813
9814 if (arg != NULL)
9815 filter = catch_syscall_split_args (arg);
9816 else
9817 filter = NULL;
9818
9819 create_syscall_event_catchpoint (tempflag, filter,
9820 &catch_syscall_breakpoint_ops);
9821}
9822
f7f9143b
JB
9823/* Implement the "catch assert" command. */
9824
9825static void
3e43a32a
MS
9826catch_assert_command (char *arg, int from_tty,
9827 struct cmd_list_element *command)
f7f9143b 9828{
a6d9a66e 9829 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9830 int tempflag;
f7f9143b
JB
9831 struct symtab_and_line sal;
9832 char *addr_string = NULL;
9833 struct breakpoint_ops *ops = NULL;
9834
44feb3ce
TT
9835 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9836
9837 if (!arg)
9838 arg = "";
f7f9143b 9839 sal = ada_decode_assert_location (arg, &addr_string, &ops);
a6d9a66e
UW
9840 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
9841 ops, tempflag, from_tty);
f7f9143b
JB
9842}
9843
c906108c 9844static void
fba45db2 9845catch_command (char *arg, int from_tty)
c906108c 9846{
44feb3ce 9847 error (_("Catch requires an event name."));
c906108c
SS
9848}
9849\f
9850
9851static void
fba45db2 9852tcatch_command (char *arg, int from_tty)
c906108c 9853{
44feb3ce 9854 error (_("Catch requires an event name."));
c906108c
SS
9855}
9856
80f8a6eb 9857/* Delete breakpoints by address or line. */
c906108c
SS
9858
9859static void
fba45db2 9860clear_command (char *arg, int from_tty)
c906108c 9861{
d6e956e5
VP
9862 struct breakpoint *b;
9863 VEC(breakpoint_p) *found = 0;
9864 int ix;
c906108c
SS
9865 int default_match;
9866 struct symtabs_and_lines sals;
9867 struct symtab_and_line sal;
c906108c
SS
9868 int i;
9869
9870 if (arg)
9871 {
9872 sals = decode_line_spec (arg, 1);
9873 default_match = 0;
9874 }
9875 else
9876 {
c5aa993b 9877 sals.sals = (struct symtab_and_line *)
c906108c 9878 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 9879 make_cleanup (xfree, sals.sals);
4a64f543 9880 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
9881 sal.line = default_breakpoint_line;
9882 sal.symtab = default_breakpoint_symtab;
9883 sal.pc = default_breakpoint_address;
6c95b8df 9884 sal.pspace = default_breakpoint_pspace;
c906108c 9885 if (sal.symtab == 0)
8a3fe4f8 9886 error (_("No source file specified."));
c906108c
SS
9887
9888 sals.sals[0] = sal;
9889 sals.nelts = 1;
9890
9891 default_match = 1;
9892 }
9893
4a64f543
MS
9894 /* We don't call resolve_sal_pc here. That's not as bad as it
9895 seems, because all existing breakpoints typically have both
9896 file/line and pc set. So, if clear is given file/line, we can
9897 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
9898
9899 We only support clearing given the address explicitly
9900 present in breakpoint table. Say, we've set breakpoint
4a64f543 9901 at file:line. There were several PC values for that file:line,
ed0616c6 9902 due to optimization, all in one block.
4a64f543
MS
9903
9904 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
9905 PC corresponding to the same file:line, the breakpoint won't
9906 be cleared. We probably can still clear the breakpoint, but
9907 since the other PC value is never presented to user, user
9908 can only find it by guessing, and it does not seem important
9909 to support that. */
9910
4a64f543
MS
9911 /* For each line spec given, delete bps which correspond to it. Do
9912 it in two passes, solely to preserve the current behavior that
9913 from_tty is forced true if we delete more than one
9914 breakpoint. */
c906108c 9915
80f8a6eb 9916 found = NULL;
c906108c
SS
9917 for (i = 0; i < sals.nelts; i++)
9918 {
9919 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
9920 If line given (pc == 0), clear all bpts on specified line.
9921 If defaulting, clear all bpts on default line
c906108c 9922 or at default pc.
c5aa993b
JM
9923
9924 defaulting sal.pc != 0 tests to do
9925
9926 0 1 pc
9927 1 1 pc _and_ line
9928 0 0 line
9929 1 0 <can't happen> */
c906108c
SS
9930
9931 sal = sals.sals[i];
c906108c 9932
4a64f543 9933 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 9934 ALL_BREAKPOINTS (b)
c5aa993b 9935 {
0d381245 9936 int match = 0;
4a64f543 9937 /* Are we going to delete b? */
cc60f2e3 9938 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
9939 {
9940 struct bp_location *loc = b->loc;
9941 for (; loc; loc = loc->next)
9942 {
6c95b8df
PA
9943 int pc_match = sal.pc
9944 && (loc->pspace == sal.pspace)
0d381245
VP
9945 && (loc->address == sal.pc)
9946 && (!section_is_overlay (loc->section)
9947 || loc->section == sal.section);
9948 int line_match = ((default_match || (0 == sal.pc))
9949 && b->source_file != NULL
9950 && sal.symtab != NULL
6c95b8df 9951 && sal.pspace == loc->pspace
0ba1096a
KT
9952 && filename_cmp (b->source_file,
9953 sal.symtab->filename) == 0
0d381245
VP
9954 && b->line_number == sal.line);
9955 if (pc_match || line_match)
9956 {
9957 match = 1;
9958 break;
9959 }
9960 }
9961 }
9962
9963 if (match)
d6e956e5 9964 VEC_safe_push(breakpoint_p, found, b);
c906108c 9965 }
80f8a6eb
MS
9966 }
9967 /* Now go thru the 'found' chain and delete them. */
d6e956e5 9968 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
9969 {
9970 if (arg)
8a3fe4f8 9971 error (_("No breakpoint at %s."), arg);
80f8a6eb 9972 else
8a3fe4f8 9973 error (_("No breakpoint at this line."));
80f8a6eb 9974 }
c906108c 9975
d6e956e5 9976 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 9977 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 9978 if (from_tty)
a3f17187 9979 {
d6e956e5 9980 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
9981 printf_unfiltered (_("Deleted breakpoint "));
9982 else
9983 printf_unfiltered (_("Deleted breakpoints "));
9984 }
80f8a6eb 9985 breakpoints_changed ();
d6e956e5
VP
9986
9987 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 9988 {
c5aa993b 9989 if (from_tty)
d6e956e5
VP
9990 printf_unfiltered ("%d ", b->number);
9991 delete_breakpoint (b);
c906108c 9992 }
80f8a6eb
MS
9993 if (from_tty)
9994 putchar_unfiltered ('\n');
c906108c
SS
9995}
9996\f
9997/* Delete breakpoint in BS if they are `delete' breakpoints and
9998 all breakpoints that are marked for deletion, whether hit or not.
9999 This is called after any breakpoint is hit, or after errors. */
10000
10001void
fba45db2 10002breakpoint_auto_delete (bpstat bs)
c906108c 10003{
35df4500 10004 struct breakpoint *b, *b_tmp;
c906108c
SS
10005
10006 for (; bs; bs = bs->next)
f431efe5
PA
10007 if (bs->breakpoint_at
10008 && bs->breakpoint_at->disposition == disp_del
c906108c 10009 && bs->stop)
f431efe5 10010 delete_breakpoint (bs->breakpoint_at);
c906108c 10011
35df4500 10012 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10013 {
b5de0fa7 10014 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10015 delete_breakpoint (b);
10016 }
c906108c
SS
10017}
10018
4a64f543
MS
10019/* A comparison function for bp_location AP and BP being interfaced to
10020 qsort. Sort elements primarily by their ADDRESS (no matter what
10021 does breakpoint_address_is_meaningful say for its OWNER),
10022 secondarily by ordering first bp_permanent OWNERed elements and
10023 terciarily just ensuring the array is sorted stable way despite
10024 qsort being an instable algorithm. */
876fa593
JK
10025
10026static int
494cfb0f 10027bp_location_compare (const void *ap, const void *bp)
876fa593 10028{
494cfb0f
JK
10029 struct bp_location *a = *(void **) ap;
10030 struct bp_location *b = *(void **) bp;
2bdf28a0 10031 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10032 int a_perm = a->owner->enable_state == bp_permanent;
10033 int b_perm = b->owner->enable_state == bp_permanent;
10034
10035 if (a->address != b->address)
10036 return (a->address > b->address) - (a->address < b->address);
10037
10038 /* Sort permanent breakpoints first. */
10039 if (a_perm != b_perm)
10040 return (a_perm < b_perm) - (a_perm > b_perm);
10041
4a64f543
MS
10042 /* Make the user-visible order stable across GDB runs. Locations of
10043 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10044
10045 if (a->owner->number != b->owner->number)
10046 return (a->owner->number > b->owner->number)
10047 - (a->owner->number < b->owner->number);
10048
10049 return (a > b) - (a < b);
10050}
10051
876fa593 10052/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10053 bp_location_shadow_len_after_address_max according to the current
10054 content of the bp_location array. */
f7545552
TT
10055
10056static void
876fa593 10057bp_location_target_extensions_update (void)
f7545552 10058{
876fa593
JK
10059 struct bp_location *bl, **blp_tmp;
10060
10061 bp_location_placed_address_before_address_max = 0;
10062 bp_location_shadow_len_after_address_max = 0;
10063
10064 ALL_BP_LOCATIONS (bl, blp_tmp)
10065 {
10066 CORE_ADDR start, end, addr;
10067
10068 if (!bp_location_has_shadow (bl))
10069 continue;
10070
10071 start = bl->target_info.placed_address;
10072 end = start + bl->target_info.shadow_len;
10073
10074 gdb_assert (bl->address >= start);
10075 addr = bl->address - start;
10076 if (addr > bp_location_placed_address_before_address_max)
10077 bp_location_placed_address_before_address_max = addr;
10078
10079 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10080
10081 gdb_assert (bl->address < end);
10082 addr = end - bl->address;
10083 if (addr > bp_location_shadow_len_after_address_max)
10084 bp_location_shadow_len_after_address_max = addr;
10085 }
f7545552
TT
10086}
10087
4cd9bd08 10088/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10089 into the inferior, only remove already-inserted locations that no
10090 longer should be inserted. Functions that delete a breakpoint or
10091 breakpoints should pass false, so that deleting a breakpoint
10092 doesn't have the side effect of inserting the locations of other
10093 breakpoints that are marked not-inserted, but should_be_inserted
10094 returns true on them.
10095
10096 This behaviour is useful is situations close to tear-down -- e.g.,
10097 after an exec, while the target still has execution, but breakpoint
10098 shadows of the previous executable image should *NOT* be restored
10099 to the new image; or before detaching, where the target still has
10100 execution and wants to delete breakpoints from GDB's lists, and all
10101 breakpoints had already been removed from the inferior. */
10102
0d381245 10103static void
b60e7edf 10104update_global_location_list (int should_insert)
0d381245 10105{
74960c60 10106 struct breakpoint *b;
876fa593 10107 struct bp_location **locp, *loc;
f7545552
TT
10108 struct cleanup *cleanups;
10109
2d134ed3
PA
10110 /* Used in the duplicates detection below. When iterating over all
10111 bp_locations, points to the first bp_location of a given address.
10112 Breakpoints and watchpoints of different types are never
10113 duplicates of each other. Keep one pointer for each type of
10114 breakpoint/watchpoint, so we only need to loop over all locations
10115 once. */
10116 struct bp_location *bp_loc_first; /* breakpoint */
10117 struct bp_location *wp_loc_first; /* hardware watchpoint */
10118 struct bp_location *awp_loc_first; /* access watchpoint */
10119 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10120
4a64f543
MS
10121 /* Saved former bp_location array which we compare against the newly
10122 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10123 struct bp_location **old_location, **old_locp;
10124 unsigned old_location_count;
10125
10126 old_location = bp_location;
10127 old_location_count = bp_location_count;
10128 bp_location = NULL;
10129 bp_location_count = 0;
10130 cleanups = make_cleanup (xfree, old_location);
0d381245 10131
74960c60 10132 ALL_BREAKPOINTS (b)
876fa593
JK
10133 for (loc = b->loc; loc; loc = loc->next)
10134 bp_location_count++;
10135
10136 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10137 locp = bp_location;
10138 ALL_BREAKPOINTS (b)
10139 for (loc = b->loc; loc; loc = loc->next)
10140 *locp++ = loc;
10141 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10142 bp_location_compare);
876fa593
JK
10143
10144 bp_location_target_extensions_update ();
74960c60 10145
4a64f543
MS
10146 /* Identify bp_location instances that are no longer present in the
10147 new list, and therefore should be freed. Note that it's not
10148 necessary that those locations should be removed from inferior --
10149 if there's another location at the same address (previously
10150 marked as duplicate), we don't need to remove/insert the
10151 location.
876fa593 10152
4a64f543
MS
10153 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10154 and former bp_location array state respectively. */
876fa593
JK
10155
10156 locp = bp_location;
10157 for (old_locp = old_location; old_locp < old_location + old_location_count;
10158 old_locp++)
74960c60 10159 {
876fa593 10160 struct bp_location *old_loc = *old_locp;
c7d46a38 10161 struct bp_location **loc2p;
876fa593 10162
4a64f543
MS
10163 /* Tells if 'old_loc' is found amoung the new locations. If
10164 not, we have to free it. */
c7d46a38 10165 int found_object = 0;
20874c92
VP
10166 /* Tells if the location should remain inserted in the target. */
10167 int keep_in_target = 0;
10168 int removed = 0;
876fa593 10169
4a64f543
MS
10170 /* Skip LOCP entries which will definitely never be needed.
10171 Stop either at or being the one matching OLD_LOC. */
876fa593 10172 while (locp < bp_location + bp_location_count
c7d46a38 10173 && (*locp)->address < old_loc->address)
876fa593 10174 locp++;
c7d46a38
PA
10175
10176 for (loc2p = locp;
10177 (loc2p < bp_location + bp_location_count
10178 && (*loc2p)->address == old_loc->address);
10179 loc2p++)
10180 {
10181 if (*loc2p == old_loc)
10182 {
10183 found_object = 1;
10184 break;
10185 }
10186 }
74960c60 10187
4a64f543
MS
10188 /* If this location is no longer present, and inserted, look if
10189 there's maybe a new location at the same address. If so,
10190 mark that one inserted, and don't remove this one. This is
10191 needed so that we don't have a time window where a breakpoint
10192 at certain location is not inserted. */
74960c60 10193
876fa593 10194 if (old_loc->inserted)
0d381245 10195 {
4a64f543
MS
10196 /* If the location is inserted now, we might have to remove
10197 it. */
74960c60 10198
876fa593 10199 if (found_object && should_be_inserted (old_loc))
74960c60 10200 {
4a64f543
MS
10201 /* The location is still present in the location list,
10202 and still should be inserted. Don't do anything. */
20874c92 10203 keep_in_target = 1;
74960c60
VP
10204 }
10205 else
10206 {
4a64f543
MS
10207 /* The location is either no longer present, or got
10208 disabled. See if there's another location at the
10209 same address, in which case we don't need to remove
10210 this one from the target. */
876fa593 10211
2bdf28a0 10212 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10213 if (breakpoint_address_is_meaningful (old_loc->owner))
10214 {
876fa593 10215 for (loc2p = locp;
c7d46a38
PA
10216 (loc2p < bp_location + bp_location_count
10217 && (*loc2p)->address == old_loc->address);
876fa593
JK
10218 loc2p++)
10219 {
10220 struct bp_location *loc2 = *loc2p;
10221
2d134ed3 10222 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10223 {
10224 /* For the sake of should_be_inserted.
4a64f543
MS
10225 Duplicates check below will fix up this
10226 later. */
c7d46a38 10227 loc2->duplicate = 0;
85d721b8
PA
10228
10229 /* Read watchpoint locations are switched to
10230 access watchpoints, if the former are not
10231 supported, but the latter are. */
10232 if (is_hardware_watchpoint (old_loc->owner))
10233 {
10234 gdb_assert (is_hardware_watchpoint (loc2->owner));
10235 loc2->watchpoint_type = old_loc->watchpoint_type;
10236 }
10237
c7d46a38
PA
10238 if (loc2 != old_loc && should_be_inserted (loc2))
10239 {
10240 loc2->inserted = 1;
10241 loc2->target_info = old_loc->target_info;
10242 keep_in_target = 1;
10243 break;
10244 }
876fa593
JK
10245 }
10246 }
10247 }
74960c60
VP
10248 }
10249
20874c92
VP
10250 if (!keep_in_target)
10251 {
876fa593 10252 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10253 {
4a64f543
MS
10254 /* This is just about all we can do. We could keep
10255 this location on the global list, and try to
10256 remove it next time, but there's no particular
10257 reason why we will succeed next time.
20874c92 10258
4a64f543
MS
10259 Note that at this point, old_loc->owner is still
10260 valid, as delete_breakpoint frees the breakpoint
10261 only after calling us. */
3e43a32a
MS
10262 printf_filtered (_("warning: Error removing "
10263 "breakpoint %d\n"),
876fa593 10264 old_loc->owner->number);
20874c92
VP
10265 }
10266 removed = 1;
10267 }
0d381245 10268 }
74960c60
VP
10269
10270 if (!found_object)
1c5cfe86 10271 {
db82e815
PA
10272 if (removed && non_stop
10273 && breakpoint_address_is_meaningful (old_loc->owner)
10274 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10275 {
db82e815
PA
10276 /* This location was removed from the target. In
10277 non-stop mode, a race condition is possible where
10278 we've removed a breakpoint, but stop events for that
10279 breakpoint are already queued and will arrive later.
10280 We apply an heuristic to be able to distinguish such
10281 SIGTRAPs from other random SIGTRAPs: we keep this
10282 breakpoint location for a bit, and will retire it
10283 after we see some number of events. The theory here
10284 is that reporting of events should, "on the average",
10285 be fair, so after a while we'll see events from all
10286 threads that have anything of interest, and no longer
10287 need to keep this breakpoint location around. We
10288 don't hold locations forever so to reduce chances of
10289 mistaking a non-breakpoint SIGTRAP for a breakpoint
10290 SIGTRAP.
10291
10292 The heuristic failing can be disastrous on
10293 decr_pc_after_break targets.
10294
10295 On decr_pc_after_break targets, like e.g., x86-linux,
10296 if we fail to recognize a late breakpoint SIGTRAP,
10297 because events_till_retirement has reached 0 too
10298 soon, we'll fail to do the PC adjustment, and report
10299 a random SIGTRAP to the user. When the user resumes
10300 the inferior, it will most likely immediately crash
2dec564e 10301 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10302 corrupted, because of being resumed e.g., in the
10303 middle of a multi-byte instruction, or skipped a
10304 one-byte instruction. This was actually seen happen
10305 on native x86-linux, and should be less rare on
10306 targets that do not support new thread events, like
10307 remote, due to the heuristic depending on
10308 thread_count.
10309
10310 Mistaking a random SIGTRAP for a breakpoint trap
10311 causes similar symptoms (PC adjustment applied when
10312 it shouldn't), but then again, playing with SIGTRAPs
10313 behind the debugger's back is asking for trouble.
10314
10315 Since hardware watchpoint traps are always
10316 distinguishable from other traps, so we don't need to
10317 apply keep hardware watchpoint moribund locations
10318 around. We simply always ignore hardware watchpoint
10319 traps we can no longer explain. */
10320
876fa593
JK
10321 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10322 old_loc->owner = NULL;
20874c92 10323
876fa593 10324 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10325 }
10326 else
f431efe5
PA
10327 {
10328 old_loc->owner = NULL;
10329 decref_bp_location (&old_loc);
10330 }
20874c92 10331 }
74960c60 10332 }
1c5cfe86 10333
2d134ed3
PA
10334 /* Rescan breakpoints at the same address and section, marking the
10335 first one as "first" and any others as "duplicates". This is so
10336 that the bpt instruction is only inserted once. If we have a
10337 permanent breakpoint at the same place as BPT, make that one the
10338 official one, and the rest as duplicates. Permanent breakpoints
10339 are sorted first for the same address.
10340
10341 Do the same for hardware watchpoints, but also considering the
10342 watchpoint's type (regular/access/read) and length. */
876fa593 10343
2d134ed3
PA
10344 bp_loc_first = NULL;
10345 wp_loc_first = NULL;
10346 awp_loc_first = NULL;
10347 rwp_loc_first = NULL;
876fa593 10348 ALL_BP_LOCATIONS (loc, locp)
74960c60 10349 {
4a64f543
MS
10350 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10351 non-NULL. */
876fa593 10352 struct breakpoint *b = loc->owner;
2d134ed3 10353 struct bp_location **loc_first_p;
876fa593
JK
10354
10355 if (b->enable_state == bp_disabled
10356 || b->enable_state == bp_call_disabled
10357 || b->enable_state == bp_startup_disabled
10358 || !loc->enabled
10359 || loc->shlib_disabled
15c3d785 10360 || !breakpoint_address_is_meaningful (b)
d77f58be 10361 || is_tracepoint (b))
876fa593
JK
10362 continue;
10363
10364 /* Permanent breakpoint should always be inserted. */
10365 if (b->enable_state == bp_permanent && ! loc->inserted)
10366 internal_error (__FILE__, __LINE__,
10367 _("allegedly permanent breakpoint is not "
10368 "actually inserted"));
10369
2d134ed3
PA
10370 if (b->type == bp_hardware_watchpoint)
10371 loc_first_p = &wp_loc_first;
10372 else if (b->type == bp_read_watchpoint)
10373 loc_first_p = &rwp_loc_first;
10374 else if (b->type == bp_access_watchpoint)
10375 loc_first_p = &awp_loc_first;
10376 else
10377 loc_first_p = &bp_loc_first;
10378
10379 if (*loc_first_p == NULL
10380 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10381 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 10382 {
2d134ed3 10383 *loc_first_p = loc;
876fa593
JK
10384 loc->duplicate = 0;
10385 continue;
10386 }
10387
10388 loc->duplicate = 1;
10389
2d134ed3
PA
10390 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10391 && b->enable_state != bp_permanent)
876fa593
JK
10392 internal_error (__FILE__, __LINE__,
10393 _("another breakpoint was inserted on top of "
10394 "a permanent breakpoint"));
0d381245 10395 }
74960c60 10396
50c71eaf 10397 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 10398 && (have_live_inferiors ()
2567c7d9 10399 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 10400 insert_breakpoint_locations ();
f7545552
TT
10401
10402 do_cleanups (cleanups);
74960c60
VP
10403}
10404
20874c92
VP
10405void
10406breakpoint_retire_moribund (void)
10407{
10408 struct bp_location *loc;
10409 int ix;
10410
10411 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10412 if (--(loc->events_till_retirement) == 0)
10413 {
f431efe5 10414 decref_bp_location (&loc);
20874c92
VP
10415 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10416 --ix;
10417 }
10418}
10419
74960c60 10420static void
b60e7edf 10421update_global_location_list_nothrow (int inserting)
74960c60
VP
10422{
10423 struct gdb_exception e;
cc59ec59 10424
74960c60 10425 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 10426 update_global_location_list (inserting);
0d381245
VP
10427}
10428
f431efe5
PA
10429/* Clear BKP from a BPS. */
10430
a474d7c2 10431static void
f431efe5 10432bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
10433{
10434 bpstat bs;
cc59ec59 10435
a474d7c2 10436 for (bs = bps; bs; bs = bs->next)
f431efe5 10437 if (bs->breakpoint_at == bpt)
a474d7c2
PA
10438 {
10439 bs->breakpoint_at = NULL;
10440 bs->old_val = NULL;
10441 /* bs->commands will be freed later. */
10442 }
10443}
10444
10445/* Callback for iterate_over_threads. */
10446static int
f431efe5 10447bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10448{
f431efe5 10449 struct breakpoint *bpt = data;
cc59ec59 10450
16c381f0 10451 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10452 return 0;
10453}
10454
53a5351d 10455/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10456 structures. */
c906108c
SS
10457
10458void
fba45db2 10459delete_breakpoint (struct breakpoint *bpt)
c906108c 10460{
52f0bd74 10461 struct breakpoint *b;
c906108c 10462
8a3fe4f8 10463 gdb_assert (bpt != NULL);
c906108c 10464
4a64f543
MS
10465 /* Has this bp already been deleted? This can happen because
10466 multiple lists can hold pointers to bp's. bpstat lists are
10467 especial culprits.
10468
10469 One example of this happening is a watchpoint's scope bp. When
10470 the scope bp triggers, we notice that the watchpoint is out of
10471 scope, and delete it. We also delete its scope bp. But the
10472 scope bp is marked "auto-deleting", and is already on a bpstat.
10473 That bpstat is then checked for auto-deleting bp's, which are
10474 deleted.
10475
10476 A real solution to this problem might involve reference counts in
10477 bp's, and/or giving them pointers back to their referencing
10478 bpstat's, and teaching delete_breakpoint to only free a bp's
10479 storage when no more references were extent. A cheaper bandaid
10480 was chosen. */
c906108c
SS
10481 if (bpt->type == bp_none)
10482 return;
10483
4a64f543
MS
10484 /* At least avoid this stale reference until the reference counting
10485 of breakpoints gets resolved. */
d0fb5eae 10486 if (bpt->related_breakpoint != bpt)
e5a0a904 10487 {
d0fb5eae
JK
10488 struct breakpoint *related;
10489
10490 if (bpt->type == bp_watchpoint_scope)
10491 watchpoint_del_at_next_stop (bpt->related_breakpoint);
10492 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
10493 watchpoint_del_at_next_stop (bpt);
10494
10495 /* Unlink bpt from the bpt->related_breakpoint ring. */
10496 for (related = bpt; related->related_breakpoint != bpt;
10497 related = related->related_breakpoint);
10498 related->related_breakpoint = bpt->related_breakpoint;
10499 bpt->related_breakpoint = bpt;
e5a0a904
JK
10500 }
10501
383f836e 10502 observer_notify_breakpoint_deleted (bpt->number);
c906108c 10503
c906108c
SS
10504 if (breakpoint_chain == bpt)
10505 breakpoint_chain = bpt->next;
10506
c906108c
SS
10507 ALL_BREAKPOINTS (b)
10508 if (b->next == bpt)
c5aa993b
JM
10509 {
10510 b->next = bpt->next;
10511 break;
10512 }
c906108c 10513
9add0f1b 10514 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10515 xfree (bpt->cond_string);
10516 xfree (bpt->cond_exp);
10517 xfree (bpt->addr_string);
f1310107 10518 xfree (bpt->addr_string_range_end);
60e1c644
PA
10519 xfree (bpt->exp);
10520 xfree (bpt->exp_string);
d63d0675 10521 xfree (bpt->exp_string_reparse);
60e1c644
PA
10522 value_free (bpt->val);
10523 xfree (bpt->source_file);
10524 xfree (bpt->exec_pathname);
a96d9b2e 10525 clean_up_filters (&bpt->syscalls_to_be_caught);
c906108c 10526
f431efe5
PA
10527
10528 /* Be sure no bpstat's are pointing at the breakpoint after it's
10529 been freed. */
10530 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10531 in all threeds for now. Note that we cannot just remove bpstats
10532 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10533 commands are associated with the bpstat; if we remove it here,
10534 then the later call to bpstat_do_actions (&stop_bpstat); in
10535 event-top.c won't do anything, and temporary breakpoints with
10536 commands won't work. */
10537
10538 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10539
4a64f543
MS
10540 /* Now that breakpoint is removed from breakpoint list, update the
10541 global location list. This will remove locations that used to
10542 belong to this breakpoint. Do this before freeing the breakpoint
10543 itself, since remove_breakpoint looks at location's owner. It
10544 might be better design to have location completely
10545 self-contained, but it's not the case now. */
b60e7edf 10546 update_global_location_list (0);
74960c60
VP
10547
10548
4a64f543
MS
10549 /* On the chance that someone will soon try again to delete this
10550 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10551 bpt->type = bp_none;
10552
b8c9b27d 10553 xfree (bpt);
c906108c
SS
10554}
10555
4d6140d9
AC
10556static void
10557do_delete_breakpoint_cleanup (void *b)
10558{
10559 delete_breakpoint (b);
10560}
10561
10562struct cleanup *
10563make_cleanup_delete_breakpoint (struct breakpoint *b)
10564{
10565 return make_cleanup (do_delete_breakpoint_cleanup, b);
10566}
10567
95a42b64
TT
10568/* A callback for map_breakpoint_numbers that calls
10569 delete_breakpoint. */
10570
10571static void
10572do_delete_breakpoint (struct breakpoint *b, void *ignore)
10573{
10574 delete_breakpoint (b);
10575}
10576
c906108c 10577void
fba45db2 10578delete_command (char *arg, int from_tty)
c906108c 10579{
35df4500 10580 struct breakpoint *b, *b_tmp;
c906108c 10581
ea9365bb
TT
10582 dont_repeat ();
10583
c906108c
SS
10584 if (arg == 0)
10585 {
10586 int breaks_to_delete = 0;
10587
10588 /* Delete all breakpoints if no argument.
4a64f543
MS
10589 Do not delete internal or call-dummy breakpoints, these have
10590 to be deleted with an explicit breakpoint number argument. */
c5aa993b
JM
10591 ALL_BREAKPOINTS (b)
10592 {
059fb39f 10593 if (b->type != bp_call_dummy
aa7d318d 10594 && b->type != bp_std_terminate
059fb39f 10595 && b->type != bp_shlib_event
4efc6507 10596 && b->type != bp_jit_event
059fb39f
PM
10597 && b->type != bp_thread_event
10598 && b->type != bp_overlay_event
0fd8e87f 10599 && b->type != bp_longjmp_master
aa7d318d 10600 && b->type != bp_std_terminate_master
186c406b 10601 && b->type != bp_exception_master
059fb39f 10602 && b->number >= 0)
973d738b
DJ
10603 {
10604 breaks_to_delete = 1;
10605 break;
10606 }
c5aa993b 10607 }
c906108c
SS
10608
10609 /* Ask user only if there are some breakpoints to delete. */
10610 if (!from_tty
e2e0b3e5 10611 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10612 {
35df4500 10613 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10614 {
059fb39f 10615 if (b->type != bp_call_dummy
aa7d318d 10616 && b->type != bp_std_terminate
059fb39f
PM
10617 && b->type != bp_shlib_event
10618 && b->type != bp_thread_event
4efc6507 10619 && b->type != bp_jit_event
059fb39f 10620 && b->type != bp_overlay_event
0fd8e87f 10621 && b->type != bp_longjmp_master
aa7d318d 10622 && b->type != bp_std_terminate_master
186c406b 10623 && b->type != bp_exception_master
059fb39f 10624 && b->number >= 0)
c5aa993b
JM
10625 delete_breakpoint (b);
10626 }
c906108c
SS
10627 }
10628 }
10629 else
95a42b64 10630 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
c906108c
SS
10631}
10632
0d381245
VP
10633static int
10634all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 10635{
0d381245
VP
10636 for (; loc; loc = loc->next)
10637 if (!loc->shlib_disabled)
10638 return 0;
10639 return 1;
fe3f5fa8
VP
10640}
10641
776592bf
DE
10642/* Subroutine of update_breakpoint_locations to simplify it.
10643 Return non-zero if multiple fns in list LOC have the same name.
10644 Null names are ignored. */
10645
10646static int
10647ambiguous_names_p (struct bp_location *loc)
10648{
10649 struct bp_location *l;
10650 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
10651 (int (*) (const void *,
10652 const void *)) streq,
776592bf
DE
10653 NULL, xcalloc, xfree);
10654
10655 for (l = loc; l != NULL; l = l->next)
10656 {
10657 const char **slot;
10658 const char *name = l->function_name;
10659
10660 /* Allow for some names to be NULL, ignore them. */
10661 if (name == NULL)
10662 continue;
10663
10664 slot = (const char **) htab_find_slot (htab, (const void *) name,
10665 INSERT);
4a64f543
MS
10666 /* NOTE: We can assume slot != NULL here because xcalloc never
10667 returns NULL. */
776592bf
DE
10668 if (*slot != NULL)
10669 {
10670 htab_delete (htab);
10671 return 1;
10672 }
10673 *slot = name;
10674 }
10675
10676 htab_delete (htab);
10677 return 0;
10678}
10679
0fb4aa4b
PA
10680/* When symbols change, it probably means the sources changed as well,
10681 and it might mean the static tracepoint markers are no longer at
10682 the same address or line numbers they used to be at last we
10683 checked. Losing your static tracepoints whenever you rebuild is
10684 undesirable. This function tries to resync/rematch gdb static
10685 tracepoints with the markers on the target, for static tracepoints
10686 that have not been set by marker id. Static tracepoint that have
10687 been set by marker id are reset by marker id in breakpoint_re_set.
10688 The heuristic is:
10689
10690 1) For a tracepoint set at a specific address, look for a marker at
10691 the old PC. If one is found there, assume to be the same marker.
10692 If the name / string id of the marker found is different from the
10693 previous known name, assume that means the user renamed the marker
10694 in the sources, and output a warning.
10695
10696 2) For a tracepoint set at a given line number, look for a marker
10697 at the new address of the old line number. If one is found there,
10698 assume to be the same marker. If the name / string id of the
10699 marker found is different from the previous known name, assume that
10700 means the user renamed the marker in the sources, and output a
10701 warning.
10702
10703 3) If a marker is no longer found at the same address or line, it
10704 may mean the marker no longer exists. But it may also just mean
10705 the code changed a bit. Maybe the user added a few lines of code
10706 that made the marker move up or down (in line number terms). Ask
10707 the target for info about the marker with the string id as we knew
10708 it. If found, update line number and address in the matching
10709 static tracepoint. This will get confused if there's more than one
10710 marker with the same ID (possible in UST, although unadvised
10711 precisely because it confuses tools). */
10712
10713static struct symtab_and_line
10714update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
10715{
10716 struct static_tracepoint_marker marker;
10717 CORE_ADDR pc;
10718 int i;
10719
10720 pc = sal.pc;
10721 if (sal.line)
10722 find_line_pc (sal.symtab, sal.line, &pc);
10723
10724 if (target_static_tracepoint_marker_at (pc, &marker))
10725 {
10726 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
10727 warning (_("static tracepoint %d changed probed marker from %s to %s"),
10728 b->number,
10729 b->static_trace_marker_id, marker.str_id);
10730
10731 xfree (b->static_trace_marker_id);
10732 b->static_trace_marker_id = xstrdup (marker.str_id);
10733 release_static_tracepoint_marker (&marker);
10734
10735 return sal;
10736 }
10737
10738 /* Old marker wasn't found on target at lineno. Try looking it up
10739 by string ID. */
10740 if (!sal.explicit_pc
10741 && sal.line != 0
10742 && sal.symtab != NULL
10743 && b->static_trace_marker_id != NULL)
10744 {
10745 VEC(static_tracepoint_marker_p) *markers;
10746
10747 markers
10748 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
10749
10750 if (!VEC_empty(static_tracepoint_marker_p, markers))
10751 {
10752 struct symtab_and_line sal;
10753 struct symbol *sym;
10754 struct static_tracepoint_marker *marker;
10755
10756 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
10757
10758 xfree (b->static_trace_marker_id);
10759 b->static_trace_marker_id = xstrdup (marker->str_id);
10760
10761 warning (_("marker for static tracepoint %d (%s) not "
10762 "found at previous line number"),
10763 b->number, b->static_trace_marker_id);
10764
10765 init_sal (&sal);
10766
10767 sal.pc = marker->address;
10768
10769 sal = find_pc_line (marker->address, 0);
10770 sym = find_pc_sect_function (marker->address, NULL);
10771 ui_out_text (uiout, "Now in ");
10772 if (sym)
10773 {
10774 ui_out_field_string (uiout, "func",
10775 SYMBOL_PRINT_NAME (sym));
10776 ui_out_text (uiout, " at ");
10777 }
10778 ui_out_field_string (uiout, "file", sal.symtab->filename);
10779 ui_out_text (uiout, ":");
10780
10781 if (ui_out_is_mi_like_p (uiout))
10782 {
10783 char *fullname = symtab_to_fullname (sal.symtab);
10784
10785 if (fullname)
10786 ui_out_field_string (uiout, "fullname", fullname);
10787 }
10788
10789 ui_out_field_int (uiout, "line", sal.line);
10790 ui_out_text (uiout, "\n");
10791
10792 b->line_number = sal.line;
10793
10794 xfree (b->source_file);
10795 if (sym)
10796 b->source_file = xstrdup (sal.symtab->filename);
10797 else
10798 b->source_file = NULL;
10799
10800 xfree (b->addr_string);
10801 b->addr_string = xstrprintf ("%s:%d",
10802 sal.symtab->filename, b->line_number);
10803
10804 /* Might be nice to check if function changed, and warn if
10805 so. */
10806
10807 release_static_tracepoint_marker (marker);
10808 }
10809 }
10810 return sal;
10811}
10812
f1310107
TJB
10813/* Create new breakpoint locations for B (a hardware or software breakpoint)
10814 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
10815 a ranged breakpoint. */
10816
0e30163f 10817void
0d381245 10818update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
10819 struct symtabs_and_lines sals,
10820 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
10821{
10822 int i;
0d381245
VP
10823 struct bp_location *existing_locations = b->loc;
10824
f1310107
TJB
10825 /* Ranged breakpoints have only one start location and one end location. */
10826 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
10827
4a64f543
MS
10828 /* If there's no new locations, and all existing locations are
10829 pending, don't do anything. This optimizes the common case where
10830 all locations are in the same shared library, that was unloaded.
10831 We'd like to retain the location, so that when the library is
10832 loaded again, we don't loose the enabled/disabled status of the
10833 individual locations. */
0d381245 10834 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
10835 return;
10836
fe3f5fa8
VP
10837 b->loc = NULL;
10838
0d381245 10839 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 10840 {
0d381245 10841 struct bp_location *new_loc =
39d61571 10842 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 10843
0d381245
VP
10844 /* Reparse conditions, they might contain references to the
10845 old symtab. */
10846 if (b->cond_string != NULL)
10847 {
f1310107 10848 char *s;
0d381245 10849 struct gdb_exception e;
fe3f5fa8 10850
0d381245
VP
10851 s = b->cond_string;
10852 TRY_CATCH (e, RETURN_MASK_ERROR)
10853 {
10854 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
10855 0);
10856 }
10857 if (e.reason < 0)
10858 {
3e43a32a
MS
10859 warning (_("failed to reevaluate condition "
10860 "for breakpoint %d: %s"),
0d381245
VP
10861 b->number, e.message);
10862 new_loc->enabled = 0;
10863 }
10864 }
fe3f5fa8 10865
0d381245
VP
10866 if (b->source_file != NULL)
10867 xfree (b->source_file);
10868 if (sals.sals[i].symtab == NULL)
10869 b->source_file = NULL;
10870 else
1b36a34b 10871 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 10872
0d381245
VP
10873 if (b->line_number == 0)
10874 b->line_number = sals.sals[i].line;
f1310107
TJB
10875
10876 if (sals_end.nelts)
10877 {
10878 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
10879
10880 new_loc->length = end - sals.sals[0].pc + 1;
10881 }
0d381245 10882 }
fe3f5fa8 10883
514f746b
AR
10884 /* Update locations of permanent breakpoints. */
10885 if (b->enable_state == bp_permanent)
10886 make_breakpoint_permanent (b);
10887
4a64f543
MS
10888 /* If possible, carry over 'disable' status from existing
10889 breakpoints. */
0d381245
VP
10890 {
10891 struct bp_location *e = existing_locations;
776592bf
DE
10892 /* If there are multiple breakpoints with the same function name,
10893 e.g. for inline functions, comparing function names won't work.
10894 Instead compare pc addresses; this is just a heuristic as things
10895 may have moved, but in practice it gives the correct answer
10896 often enough until a better solution is found. */
10897 int have_ambiguous_names = ambiguous_names_p (b->loc);
10898
0d381245
VP
10899 for (; e; e = e->next)
10900 {
10901 if (!e->enabled && e->function_name)
10902 {
10903 struct bp_location *l = b->loc;
776592bf
DE
10904 if (have_ambiguous_names)
10905 {
10906 for (; l; l = l->next)
f1310107 10907 if (breakpoint_locations_match (e, l))
776592bf
DE
10908 {
10909 l->enabled = 0;
10910 break;
10911 }
10912 }
10913 else
10914 {
10915 for (; l; l = l->next)
10916 if (l->function_name
10917 && strcmp (e->function_name, l->function_name) == 0)
10918 {
10919 l->enabled = 0;
10920 break;
10921 }
10922 }
0d381245
VP
10923 }
10924 }
10925 }
fe3f5fa8 10926
b60e7edf 10927 update_global_location_list (1);
fe3f5fa8
VP
10928}
10929
ef23e705
TJB
10930/* Find the SaL locations corresponding to the given ADDR_STRING.
10931 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
10932
10933static struct symtabs_and_lines
10934addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
10935{
10936 char *s;
10937 int marker_spec, not_found;
02d20e4a 10938 struct symtabs_and_lines sals = {0};
ef23e705
TJB
10939 struct gdb_exception e;
10940
10941 s = addr_string;
10942 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
10943
10944 TRY_CATCH (e, RETURN_MASK_ERROR)
10945 {
10946 if (marker_spec)
10947 {
10948 sals = decode_static_tracepoint_spec (&s);
10949 if (sals.nelts > b->static_trace_marker_id_idx)
10950 {
10951 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
10952 sals.nelts = 1;
10953 }
10954 else
10955 error (_("marker %s not found"), b->static_trace_marker_id);
10956 }
10957 else
10958 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0,
10959 NULL, &not_found);
10960 }
10961 if (e.reason < 0)
10962 {
10963 int not_found_and_ok = 0;
10964 /* For pending breakpoints, it's expected that parsing will
10965 fail until the right shared library is loaded. User has
10966 already told to create pending breakpoints and don't need
10967 extra messages. If breakpoint is in bp_shlib_disabled
10968 state, then user already saw the message about that
10969 breakpoint being disabled, and don't want to see more
10970 errors. */
10971 if (not_found
10972 && (b->condition_not_parsed
10973 || (b->loc && b->loc->shlib_disabled)
10974 || b->enable_state == bp_disabled))
10975 not_found_and_ok = 1;
10976
10977 if (!not_found_and_ok)
10978 {
10979 /* We surely don't want to warn about the same breakpoint
10980 10 times. One solution, implemented here, is disable
10981 the breakpoint on error. Another solution would be to
10982 have separate 'warning emitted' flag. Since this
10983 happens only when a binary has changed, I don't know
10984 which approach is better. */
10985 b->enable_state = bp_disabled;
10986 throw_exception (e);
10987 }
10988 }
10989
10990 if (!not_found)
10991 {
10992 gdb_assert (sals.nelts == 1);
10993
10994 resolve_sal_pc (&sals.sals[0]);
10995 if (b->condition_not_parsed && s && s[0])
10996 {
10997 char *cond_string = 0;
10998 int thread = -1;
10999 int task = 0;
11000
11001 find_condition_and_thread (s, sals.sals[0].pc,
11002 &cond_string, &thread, &task);
11003 if (cond_string)
11004 b->cond_string = cond_string;
11005 b->thread = thread;
11006 b->task = task;
11007 b->condition_not_parsed = 0;
11008 }
11009
11010 if (b->type == bp_static_tracepoint && !marker_spec)
11011 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
11012 }
11013
11014 *found = !not_found;
11015
11016 return sals;
11017}
11018
11019/* Reevaluate a hardware or software breakpoint and recreate its locations.
11020 This is necessary after symbols are read (e.g., an executable or DSO
11021 was loaded, or the inferior just started). */
11022
11023static void
11024re_set_breakpoint (struct breakpoint *b)
11025{
11026 int found;
f1310107 11027 struct symtabs_and_lines sals, sals_end;
ef23e705 11028 struct symtabs_and_lines expanded = {0};
f1310107 11029 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11030 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11031
11032 input_radix = b->input_radix;
11033 save_current_space_and_thread ();
11034 switch_to_program_space_and_thread (b->pspace);
11035 set_language (b->language);
11036
11037 sals = addr_string_to_sals (b, b->addr_string, &found);
11038 if (found)
11039 {
11040 make_cleanup (xfree, sals.sals);
11041 expanded = expand_line_sal_maybe (sals.sals[0]);
11042 }
11043
f1310107
TJB
11044 if (b->addr_string_range_end)
11045 {
11046 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11047 if (found)
11048 {
11049 make_cleanup (xfree, sals_end.sals);
11050 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11051 }
11052 }
11053
11054 update_breakpoint_locations (b, expanded, expanded_end);
ef23e705
TJB
11055 do_cleanups (cleanups);
11056}
11057
c906108c
SS
11058/* Reset a breakpoint given it's struct breakpoint * BINT.
11059 The value we return ends up being the return value from catch_errors.
11060 Unused in this case. */
11061
11062static int
4efb68b1 11063breakpoint_re_set_one (void *bint)
c906108c 11064{
4a64f543 11065 /* Get past catch_errs. */
53a5351d 11066 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
11067
11068 switch (b->type)
11069 {
11070 case bp_none:
8a3fe4f8 11071 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 11072 b->number);
c906108c
SS
11073 return 0;
11074 case bp_breakpoint:
11075 case bp_hardware_breakpoint:
1042e4c0 11076 case bp_tracepoint:
7a697b8d 11077 case bp_fast_tracepoint:
0fb4aa4b 11078 case bp_static_tracepoint:
0e30163f 11079 case bp_gnu_ifunc_resolver:
8bea4e01
UW
11080 /* Do not attempt to re-set breakpoints disabled during startup. */
11081 if (b->enable_state == bp_startup_disabled)
11082 return 0;
11083
c906108c
SS
11084 if (b->addr_string == NULL)
11085 {
4a64f543 11086 /* Anything without a string can't be re-set. */
c906108c
SS
11087 delete_breakpoint (b);
11088 return 0;
11089 }
c906108c 11090
ef23e705 11091 re_set_breakpoint (b);
c906108c
SS
11092 break;
11093
11094 case bp_watchpoint:
11095 case bp_hardware_watchpoint:
11096 case bp_read_watchpoint:
11097 case bp_access_watchpoint:
4a64f543
MS
11098 /* Watchpoint can be either on expression using entirely global
11099 variables, or it can be on local variables.
11100
11101 Watchpoints of the first kind are never auto-deleted, and
11102 even persist across program restarts. Since they can use
11103 variables from shared libraries, we need to reparse
11104 expression as libraries are loaded and unloaded.
11105
11106 Watchpoints on local variables can also change meaning as
11107 result of solib event. For example, if a watchpoint uses
11108 both a local and a global variables in expression, it's a
11109 local watchpoint, but unloading of a shared library will make
11110 the expression invalid. This is not a very common use case,
11111 but we still re-evaluate expression, to avoid surprises to
11112 the user.
0b3de036
VP
11113
11114 Note that for local watchpoints, we re-evaluate it only if
11115 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
11116 the watchpoint is out of scope and will be deleted soon. In
11117 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
11118
11119 If a local watchpoint's frame id is still valid, then
4a64f543 11120 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
11121
11122 Don't do anything about disabled watchpoints, since they will
11123 be reevaluated again when enabled. */
a5606eee 11124 update_watchpoint (b, 1 /* reparse */);
c906108c 11125 break;
c5aa993b
JM
11126 /* We needn't really do anything to reset these, since the mask
11127 that requests them is unaffected by e.g., new libraries being
4a64f543 11128 loaded. */
ce78b96d 11129 case bp_catchpoint:
c906108c 11130 break;
c5aa993b 11131
c906108c 11132 default:
a3f17187 11133 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 11134 /* fall through */
0fd8e87f
UW
11135 /* Delete overlay event and longjmp master breakpoints; they will be
11136 reset later by breakpoint_re_set. */
1900040c 11137 case bp_overlay_event:
0fd8e87f 11138 case bp_longjmp_master:
aa7d318d 11139 case bp_std_terminate_master:
186c406b 11140 case bp_exception_master:
c906108c
SS
11141 delete_breakpoint (b);
11142 break;
11143
c5aa993b
JM
11144 /* This breakpoint is special, it's set up when the inferior
11145 starts and we really don't want to touch it. */
c906108c
SS
11146 case bp_shlib_event:
11147
c4093a6a
JM
11148 /* Like bp_shlib_event, this breakpoint type is special.
11149 Once it is set up, we do not want to touch it. */
11150 case bp_thread_event:
11151
4a64f543
MS
11152 /* Keep temporary breakpoints, which can be encountered when we
11153 step over a dlopen call and SOLIB_ADD is resetting the
11154 breakpoints. Otherwise these should have been blown away via
11155 the cleanup chain or by breakpoint_init_inferior when we
11156 rerun the executable. */
c906108c
SS
11157 case bp_until:
11158 case bp_finish:
11159 case bp_watchpoint_scope:
11160 case bp_call_dummy:
aa7d318d 11161 case bp_std_terminate:
c906108c 11162 case bp_step_resume:
611c83ae
PA
11163 case bp_longjmp:
11164 case bp_longjmp_resume:
186c406b
TT
11165 case bp_exception:
11166 case bp_exception_resume:
4efc6507 11167 case bp_jit_event:
0e30163f 11168 case bp_gnu_ifunc_resolver_return:
c906108c
SS
11169 break;
11170 }
11171
11172 return 0;
11173}
11174
69de3c6a 11175/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11176void
69de3c6a 11177breakpoint_re_set (void)
c906108c 11178{
35df4500 11179 struct breakpoint *b, *b_tmp;
c906108c
SS
11180 enum language save_language;
11181 int save_input_radix;
6c95b8df 11182 struct cleanup *old_chain;
c5aa993b 11183
c906108c
SS
11184 save_language = current_language->la_language;
11185 save_input_radix = input_radix;
6c95b8df
PA
11186 old_chain = save_current_program_space ();
11187
35df4500 11188 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11189 {
4a64f543 11190 /* Format possible error msg. */
fe3f5fa8 11191 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11192 b->number);
11193 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11194 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11195 do_cleanups (cleanups);
c5aa993b 11196 }
c906108c
SS
11197 set_language (save_language);
11198 input_radix = save_input_radix;
e62c965a 11199
0756c555 11200 jit_breakpoint_re_set ();
4efc6507 11201
6c95b8df
PA
11202 do_cleanups (old_chain);
11203
af02033e
PP
11204 create_overlay_event_breakpoint ();
11205 create_longjmp_master_breakpoint ();
11206 create_std_terminate_master_breakpoint ();
186c406b 11207 create_exception_master_breakpoint ();
c906108c
SS
11208}
11209\f
c906108c
SS
11210/* Reset the thread number of this breakpoint:
11211
11212 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11213 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11214void
fba45db2 11215breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11216{
11217 if (b->thread != -1)
11218 {
39f77062
KB
11219 if (in_thread_list (inferior_ptid))
11220 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11221
11222 /* We're being called after following a fork. The new fork is
11223 selected as current, and unless this was a vfork will have a
11224 different program space from the original thread. Reset that
11225 as well. */
11226 b->loc->pspace = current_program_space;
c906108c
SS
11227 }
11228}
11229
03ac34d5
MS
11230/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11231 If from_tty is nonzero, it prints a message to that effect,
11232 which ends with a period (no newline). */
11233
c906108c 11234void
fba45db2 11235set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11236{
52f0bd74 11237 struct breakpoint *b;
c906108c
SS
11238
11239 if (count < 0)
11240 count = 0;
11241
11242 ALL_BREAKPOINTS (b)
11243 if (b->number == bptnum)
c5aa993b 11244 {
d77f58be
SS
11245 if (is_tracepoint (b))
11246 {
11247 if (from_tty && count != 0)
11248 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11249 bptnum);
11250 return;
11251 }
11252
c5aa993b 11253 b->ignore_count = count;
221ea385
KS
11254 if (from_tty)
11255 {
11256 if (count == 0)
3e43a32a
MS
11257 printf_filtered (_("Will stop next time "
11258 "breakpoint %d is reached."),
221ea385
KS
11259 bptnum);
11260 else if (count == 1)
a3f17187 11261 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11262 bptnum);
11263 else
3e43a32a
MS
11264 printf_filtered (_("Will ignore next %d "
11265 "crossings of breakpoint %d."),
221ea385
KS
11266 count, bptnum);
11267 }
c5aa993b 11268 breakpoints_changed ();
383f836e 11269 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
11270 return;
11271 }
c906108c 11272
8a3fe4f8 11273 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11274}
11275
c906108c
SS
11276/* Command to set ignore-count of breakpoint N to COUNT. */
11277
11278static void
fba45db2 11279ignore_command (char *args, int from_tty)
c906108c
SS
11280{
11281 char *p = args;
52f0bd74 11282 int num;
c906108c
SS
11283
11284 if (p == 0)
e2e0b3e5 11285 error_no_arg (_("a breakpoint number"));
c5aa993b 11286
c906108c 11287 num = get_number (&p);
5c44784c 11288 if (num == 0)
8a3fe4f8 11289 error (_("bad breakpoint number: '%s'"), args);
c906108c 11290 if (*p == 0)
8a3fe4f8 11291 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11292
11293 set_ignore_count (num,
11294 longest_to_int (value_as_long (parse_and_eval (p))),
11295 from_tty);
221ea385
KS
11296 if (from_tty)
11297 printf_filtered ("\n");
c906108c
SS
11298}
11299\f
11300/* Call FUNCTION on each of the breakpoints
11301 whose numbers are given in ARGS. */
11302
11303static void
95a42b64
TT
11304map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11305 void *),
11306 void *data)
c906108c 11307{
52f0bd74
AC
11308 int num;
11309 struct breakpoint *b, *tmp;
11cf8741 11310 int match;
197f0a60 11311 struct get_number_or_range_state state;
c906108c 11312
197f0a60 11313 if (args == 0)
e2e0b3e5 11314 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11315
197f0a60
TT
11316 init_number_or_range (&state, args);
11317
11318 while (!state.finished)
c906108c 11319 {
197f0a60
TT
11320 char *p = state.string;
11321
11cf8741 11322 match = 0;
c5aa993b 11323
197f0a60 11324 num = get_number_or_range (&state);
5c44784c 11325 if (num == 0)
c5aa993b 11326 {
8a3fe4f8 11327 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11328 }
11329 else
11330 {
11331 ALL_BREAKPOINTS_SAFE (b, tmp)
11332 if (b->number == num)
11333 {
d0fb5eae
JK
11334 struct breakpoint *related_breakpoint;
11335
11cf8741 11336 match = 1;
d0fb5eae
JK
11337 related_breakpoint = b;
11338 do
11339 {
11340 struct breakpoint *next_related_b;
11341
11342 /* FUNCTION can be also delete_breakpoint. */
11343 next_related_b = related_breakpoint->related_breakpoint;
11344 function (related_breakpoint, data);
11345
11346 /* For delete_breakpoint of the last entry of the ring we
11347 were traversing we would never get back to B. */
11348 if (next_related_b == related_breakpoint)
11349 break;
11350 related_breakpoint = next_related_b;
11351 }
11352 while (related_breakpoint != b);
11cf8741 11353 break;
5c44784c 11354 }
11cf8741 11355 if (match == 0)
a3f17187 11356 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11357 }
c906108c
SS
11358 }
11359}
11360
0d381245
VP
11361static struct bp_location *
11362find_location_by_number (char *number)
11363{
11364 char *dot = strchr (number, '.');
11365 char *p1;
11366 int bp_num;
11367 int loc_num;
11368 struct breakpoint *b;
11369 struct bp_location *loc;
11370
11371 *dot = '\0';
11372
11373 p1 = number;
197f0a60 11374 bp_num = get_number (&p1);
0d381245
VP
11375 if (bp_num == 0)
11376 error (_("Bad breakpoint number '%s'"), number);
11377
11378 ALL_BREAKPOINTS (b)
11379 if (b->number == bp_num)
11380 {
11381 break;
11382 }
11383
11384 if (!b || b->number != bp_num)
11385 error (_("Bad breakpoint number '%s'"), number);
11386
11387 p1 = dot+1;
197f0a60 11388 loc_num = get_number (&p1);
0d381245
VP
11389 if (loc_num == 0)
11390 error (_("Bad breakpoint location number '%s'"), number);
11391
11392 --loc_num;
11393 loc = b->loc;
11394 for (;loc_num && loc; --loc_num, loc = loc->next)
11395 ;
11396 if (!loc)
11397 error (_("Bad breakpoint location number '%s'"), dot+1);
11398
11399 return loc;
11400}
11401
11402
1900040c
MS
11403/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11404 If from_tty is nonzero, it prints a message to that effect,
11405 which ends with a period (no newline). */
11406
c906108c 11407void
fba45db2 11408disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11409{
11410 /* Never disable a watchpoint scope breakpoint; we want to
11411 hit them when we leave scope so we can delete both the
11412 watchpoint and its scope breakpoint at that time. */
11413 if (bpt->type == bp_watchpoint_scope)
11414 return;
11415
c2c6d25f 11416 /* You can't disable permanent breakpoints. */
b5de0fa7 11417 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11418 return;
11419
b5de0fa7 11420 bpt->enable_state = bp_disabled;
c906108c 11421
b60e7edf 11422 update_global_location_list (0);
c906108c 11423
383f836e 11424 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11425}
11426
95a42b64
TT
11427/* A callback for map_breakpoint_numbers that calls
11428 disable_breakpoint. */
11429
11430static void
11431do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11432{
11433 disable_breakpoint (b);
11434}
11435
c906108c 11436static void
fba45db2 11437disable_command (char *args, int from_tty)
c906108c 11438{
52f0bd74 11439 struct breakpoint *bpt;
cc59ec59 11440
c906108c
SS
11441 if (args == 0)
11442 ALL_BREAKPOINTS (bpt)
11443 switch (bpt->type)
c5aa993b
JM
11444 {
11445 case bp_none:
8a3fe4f8 11446 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 11447 bpt->number);
488919c4 11448 break;
c5aa993b 11449 case bp_breakpoint:
1042e4c0 11450 case bp_tracepoint:
7a697b8d 11451 case bp_fast_tracepoint:
0fb4aa4b 11452 case bp_static_tracepoint:
ce78b96d 11453 case bp_catchpoint:
c5aa993b
JM
11454 case bp_hardware_breakpoint:
11455 case bp_watchpoint:
11456 case bp_hardware_watchpoint:
11457 case bp_read_watchpoint:
11458 case bp_access_watchpoint:
11459 disable_breakpoint (bpt);
488919c4 11460 break;
c5aa993b 11461 default:
488919c4 11462 break;
c5aa993b 11463 }
0d381245
VP
11464 else if (strchr (args, '.'))
11465 {
11466 struct bp_location *loc = find_location_by_number (args);
11467 if (loc)
11468 loc->enabled = 0;
b60e7edf 11469 update_global_location_list (0);
0d381245 11470 }
c906108c 11471 else
95a42b64 11472 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11473}
11474
11475static void
fba45db2 11476do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11477{
afe38095 11478 int target_resources_ok;
c906108c
SS
11479
11480 if (bpt->type == bp_hardware_breakpoint)
11481 {
11482 int i;
c5aa993b 11483 i = hw_breakpoint_used_count ();
53a5351d 11484 target_resources_ok =
d92524f1 11485 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11486 i + 1, 0);
c906108c 11487 if (target_resources_ok == 0)
8a3fe4f8 11488 error (_("No hardware breakpoint support in the target."));
c906108c 11489 else if (target_resources_ok < 0)
8a3fe4f8 11490 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11491 }
11492
cc60f2e3 11493 if (is_watchpoint (bpt))
c906108c 11494 {
dde02812
ES
11495 struct gdb_exception e;
11496
11497 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11498 {
dde02812 11499 update_watchpoint (bpt, 1 /* reparse */);
c906108c 11500 }
dde02812 11501 if (e.reason < 0)
c5aa993b 11502 {
dde02812
ES
11503 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11504 bpt->number);
11505 return;
c5aa993b 11506 }
c906108c 11507 }
0101ce28 11508
b4c291bb
KH
11509 if (bpt->enable_state != bp_permanent)
11510 bpt->enable_state = bp_enabled;
11511 bpt->disposition = disposition;
b60e7edf 11512 update_global_location_list (1);
b4c291bb
KH
11513 breakpoints_changed ();
11514
383f836e 11515 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
11516}
11517
fe3f5fa8 11518
c906108c 11519void
fba45db2 11520enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11521{
11522 do_enable_breakpoint (bpt, bpt->disposition);
11523}
11524
95a42b64
TT
11525/* A callback for map_breakpoint_numbers that calls
11526 enable_breakpoint. */
11527
11528static void
11529do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11530{
11531 enable_breakpoint (b);
11532}
11533
c906108c
SS
11534/* The enable command enables the specified breakpoints (or all defined
11535 breakpoints) so they once again become (or continue to be) effective
1272ad14 11536 in stopping the inferior. */
c906108c 11537
c906108c 11538static void
fba45db2 11539enable_command (char *args, int from_tty)
c906108c 11540{
52f0bd74 11541 struct breakpoint *bpt;
cc59ec59 11542
c906108c
SS
11543 if (args == 0)
11544 ALL_BREAKPOINTS (bpt)
11545 switch (bpt->type)
c5aa993b
JM
11546 {
11547 case bp_none:
8a3fe4f8 11548 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 11549 bpt->number);
488919c4 11550 break;
c5aa993b 11551 case bp_breakpoint:
1042e4c0 11552 case bp_tracepoint:
7a697b8d 11553 case bp_fast_tracepoint:
0fb4aa4b 11554 case bp_static_tracepoint:
ce78b96d 11555 case bp_catchpoint:
c5aa993b
JM
11556 case bp_hardware_breakpoint:
11557 case bp_watchpoint:
11558 case bp_hardware_watchpoint:
11559 case bp_read_watchpoint:
11560 case bp_access_watchpoint:
11561 enable_breakpoint (bpt);
488919c4 11562 break;
c5aa993b 11563 default:
488919c4 11564 break;
c5aa993b 11565 }
0d381245
VP
11566 else if (strchr (args, '.'))
11567 {
11568 struct bp_location *loc = find_location_by_number (args);
11569 if (loc)
11570 loc->enabled = 1;
b60e7edf 11571 update_global_location_list (1);
0d381245 11572 }
c906108c 11573 else
95a42b64 11574 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
11575}
11576
11577static void
95a42b64 11578enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11579{
b5de0fa7 11580 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
11581}
11582
c906108c 11583static void
fba45db2 11584enable_once_command (char *args, int from_tty)
c906108c 11585{
95a42b64 11586 map_breakpoint_numbers (args, enable_once_breakpoint, NULL);
c906108c
SS
11587}
11588
11589static void
95a42b64 11590enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 11591{
b5de0fa7 11592 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
11593}
11594
c906108c 11595static void
fba45db2 11596enable_delete_command (char *args, int from_tty)
c906108c 11597{
95a42b64 11598 map_breakpoint_numbers (args, enable_delete_breakpoint, NULL);
c906108c
SS
11599}
11600\f
fa8d40ab
JJ
11601static void
11602set_breakpoint_cmd (char *args, int from_tty)
11603{
11604}
11605
11606static void
11607show_breakpoint_cmd (char *args, int from_tty)
11608{
11609}
11610
1f3b5d1b
PP
11611/* Invalidate last known value of any hardware watchpoint if
11612 the memory which that value represents has been written to by
11613 GDB itself. */
11614
11615static void
11616invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
11617 const bfd_byte *data)
11618{
11619 struct breakpoint *bp;
11620
11621 ALL_BREAKPOINTS (bp)
11622 if (bp->enable_state == bp_enabled
11623 && bp->type == bp_hardware_watchpoint
11624 && bp->val_valid && bp->val)
11625 {
11626 struct bp_location *loc;
11627
11628 for (loc = bp->loc; loc != NULL; loc = loc->next)
11629 if (loc->loc_type == bp_loc_hardware_watchpoint
11630 && loc->address + loc->length > addr
11631 && addr + len > loc->address)
11632 {
11633 value_free (bp->val);
11634 bp->val = NULL;
11635 bp->val_valid = 0;
11636 }
11637 }
11638}
11639
c906108c
SS
11640/* Use default_breakpoint_'s, or nothing if they aren't valid. */
11641
11642struct symtabs_and_lines
fba45db2 11643decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
11644{
11645 struct symtabs_and_lines sals;
cc59ec59 11646
c906108c 11647 if (string == 0)
8a3fe4f8 11648 error (_("Empty line specification."));
c906108c
SS
11649 if (default_breakpoint_valid)
11650 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
11651 default_breakpoint_symtab,
11652 default_breakpoint_line,
7efd8fc2 11653 NULL, NULL);
c906108c
SS
11654 else
11655 sals = decode_line_1 (&string, funfirstline,
7efd8fc2 11656 (struct symtab *) NULL, 0, NULL, NULL);
c906108c 11657 if (*string)
8a3fe4f8 11658 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
11659 return sals;
11660}
8181d85f
DJ
11661
11662/* Create and insert a raw software breakpoint at PC. Return an
11663 identifier, which should be used to remove the breakpoint later.
11664 In general, places which call this should be using something on the
11665 breakpoint chain instead; this function should be eliminated
11666 someday. */
11667
11668void *
6c95b8df
PA
11669deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
11670 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
11671{
11672 struct bp_target_info *bp_tgt;
11673
6c95b8df 11674 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 11675
6c95b8df 11676 bp_tgt->placed_address_space = aspace;
8181d85f 11677 bp_tgt->placed_address = pc;
6c95b8df 11678
a6d9a66e 11679 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
11680 {
11681 /* Could not insert the breakpoint. */
11682 xfree (bp_tgt);
11683 return NULL;
11684 }
11685
11686 return bp_tgt;
11687}
11688
4a64f543
MS
11689/* Remove a breakpoint BP inserted by
11690 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
11691
11692int
a6d9a66e 11693deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
11694{
11695 struct bp_target_info *bp_tgt = bp;
11696 int ret;
11697
a6d9a66e 11698 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
11699 xfree (bp_tgt);
11700
11701 return ret;
11702}
11703
4a64f543
MS
11704/* One (or perhaps two) breakpoints used for software single
11705 stepping. */
8181d85f
DJ
11706
11707static void *single_step_breakpoints[2];
a6d9a66e 11708static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
11709
11710/* Create and insert a breakpoint for software single step. */
11711
11712void
6c95b8df 11713insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
11714 struct address_space *aspace,
11715 CORE_ADDR next_pc)
8181d85f
DJ
11716{
11717 void **bpt_p;
11718
11719 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
11720 {
11721 bpt_p = &single_step_breakpoints[0];
11722 single_step_gdbarch[0] = gdbarch;
11723 }
8181d85f
DJ
11724 else
11725 {
11726 gdb_assert (single_step_breakpoints[1] == NULL);
11727 bpt_p = &single_step_breakpoints[1];
a6d9a66e 11728 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
11729 }
11730
4a64f543
MS
11731 /* NOTE drow/2006-04-11: A future improvement to this function would
11732 be to only create the breakpoints once, and actually put them on
11733 the breakpoint chain. That would let us use set_raw_breakpoint.
11734 We could adjust the addresses each time they were needed. Doing
11735 this requires corresponding changes elsewhere where single step
11736 breakpoints are handled, however. So, for now, we use this. */
8181d85f 11737
6c95b8df 11738 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 11739 if (*bpt_p == NULL)
5af949e3
UW
11740 error (_("Could not insert single-step breakpoint at %s"),
11741 paddress (gdbarch, next_pc));
8181d85f
DJ
11742}
11743
f02253f1
HZ
11744/* Check if the breakpoints used for software single stepping
11745 were inserted or not. */
11746
11747int
11748single_step_breakpoints_inserted (void)
11749{
11750 return (single_step_breakpoints[0] != NULL
11751 || single_step_breakpoints[1] != NULL);
11752}
11753
8181d85f
DJ
11754/* Remove and delete any breakpoints used for software single step. */
11755
11756void
11757remove_single_step_breakpoints (void)
11758{
11759 gdb_assert (single_step_breakpoints[0] != NULL);
11760
11761 /* See insert_single_step_breakpoint for more about this deprecated
11762 call. */
a6d9a66e
UW
11763 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
11764 single_step_breakpoints[0]);
11765 single_step_gdbarch[0] = NULL;
8181d85f
DJ
11766 single_step_breakpoints[0] = NULL;
11767
11768 if (single_step_breakpoints[1] != NULL)
11769 {
a6d9a66e
UW
11770 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
11771 single_step_breakpoints[1]);
11772 single_step_gdbarch[1] = NULL;
8181d85f
DJ
11773 single_step_breakpoints[1] = NULL;
11774 }
11775}
11776
d03285ec
UW
11777/* Delete software single step breakpoints without removing them from
11778 the inferior. This is intended to be used if the inferior's address
11779 space where they were inserted is already gone, e.g. after exit or
11780 exec. */
11781
11782void
11783cancel_single_step_breakpoints (void)
11784{
11785 int i;
11786
11787 for (i = 0; i < 2; i++)
11788 if (single_step_breakpoints[i])
11789 {
11790 xfree (single_step_breakpoints[i]);
11791 single_step_breakpoints[i] = NULL;
11792 single_step_gdbarch[i] = NULL;
11793 }
11794}
11795
11796/* Detach software single-step breakpoints from INFERIOR_PTID without
11797 removing them. */
11798
11799static void
11800detach_single_step_breakpoints (void)
11801{
11802 int i;
11803
11804 for (i = 0; i < 2; i++)
11805 if (single_step_breakpoints[i])
11806 target_remove_breakpoint (single_step_gdbarch[i],
11807 single_step_breakpoints[i]);
11808}
11809
4a64f543
MS
11810/* Check whether a software single-step breakpoint is inserted at
11811 PC. */
1aafd4da
UW
11812
11813static int
cc59ec59
MS
11814single_step_breakpoint_inserted_here_p (struct address_space *aspace,
11815 CORE_ADDR pc)
1aafd4da
UW
11816{
11817 int i;
11818
11819 for (i = 0; i < 2; i++)
11820 {
11821 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
11822 if (bp_tgt
11823 && breakpoint_address_match (bp_tgt->placed_address_space,
11824 bp_tgt->placed_address,
11825 aspace, pc))
1aafd4da
UW
11826 return 1;
11827 }
11828
11829 return 0;
11830}
11831
a96d9b2e
SDJ
11832/* Returns 0 if 'bp' is NOT a syscall catchpoint,
11833 non-zero otherwise. */
11834static int
11835is_syscall_catchpoint_enabled (struct breakpoint *bp)
11836{
11837 if (syscall_catchpoint_p (bp)
11838 && bp->enable_state != bp_disabled
11839 && bp->enable_state != bp_call_disabled)
11840 return 1;
11841 else
11842 return 0;
11843}
11844
11845int
11846catch_syscall_enabled (void)
11847{
11848 struct inferior *inf = current_inferior ();
11849
11850 return inf->total_syscalls_count != 0;
11851}
11852
11853int
11854catching_syscall_number (int syscall_number)
11855{
11856 struct breakpoint *bp;
11857
11858 ALL_BREAKPOINTS (bp)
11859 if (is_syscall_catchpoint_enabled (bp))
11860 {
11861 if (bp->syscalls_to_be_caught)
11862 {
11863 int i, iter;
11864 for (i = 0;
11865 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
11866 i++)
11867 if (syscall_number == iter)
11868 return 1;
11869 }
11870 else
11871 return 1;
11872 }
11873
11874 return 0;
11875}
11876
11877/* Complete syscall names. Used by "catch syscall". */
11878static char **
11879catch_syscall_completer (struct cmd_list_element *cmd,
11880 char *text, char *word)
11881{
11882 const char **list = get_syscall_names ();
c38eea1a
MS
11883 char **retlist
11884 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 11885
c38eea1a
MS
11886 xfree (list);
11887 return retlist;
a96d9b2e
SDJ
11888}
11889
1042e4c0
SS
11890/* Tracepoint-specific operations. */
11891
11892/* Set tracepoint count to NUM. */
11893static void
11894set_tracepoint_count (int num)
11895{
11896 tracepoint_count = num;
4fa62494 11897 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
11898}
11899
11900void
11901trace_command (char *arg, int from_tty)
11902{
8cdf0e15
VP
11903 if (create_breakpoint (get_current_arch (),
11904 arg,
11905 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11906 0 /* tempflag */,
11907 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
11908 0 /* Ignore count */,
11909 pending_break_support,
11910 NULL,
11911 from_tty,
84f4c1fe
PM
11912 1 /* enabled */,
11913 0 /* internal */))
fd9b8c24 11914 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
11915}
11916
7a697b8d
SS
11917void
11918ftrace_command (char *arg, int from_tty)
11919{
8cdf0e15
VP
11920 if (create_breakpoint (get_current_arch (),
11921 arg,
11922 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
11923 0 /* tempflag */,
11924 bp_fast_tracepoint /* type_wanted */,
11925 0 /* Ignore count */,
11926 pending_break_support,
11927 NULL,
11928 from_tty,
84f4c1fe
PM
11929 1 /* enabled */,
11930 0 /* internal */))
0fb4aa4b
PA
11931 set_tracepoint_count (breakpoint_count);
11932}
11933
11934/* strace command implementation. Creates a static tracepoint. */
11935
11936void
11937strace_command (char *arg, int from_tty)
11938{
11939 if (create_breakpoint (get_current_arch (),
11940 arg,
11941 NULL, 0, 1 /* parse arg */,
11942 0 /* tempflag */,
11943 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
11944 0 /* Ignore count */,
11945 pending_break_support,
11946 NULL,
11947 from_tty,
84f4c1fe
PM
11948 1 /* enabled */,
11949 0 /* internal */))
fd9b8c24 11950 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
11951}
11952
409873ef
SS
11953/* Set up a fake reader function that gets command lines from a linked
11954 list that was acquired during tracepoint uploading. */
11955
11956static struct uploaded_tp *this_utp;
3149d8c1 11957static int next_cmd;
409873ef
SS
11958
11959static char *
11960read_uploaded_action (void)
11961{
11962 char *rslt;
11963
3149d8c1 11964 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 11965
3149d8c1 11966 next_cmd++;
409873ef
SS
11967
11968 return rslt;
11969}
11970
00bf0b85
SS
11971/* Given information about a tracepoint as recorded on a target (which
11972 can be either a live system or a trace file), attempt to create an
11973 equivalent GDB tracepoint. This is not a reliable process, since
11974 the target does not necessarily have all the information used when
11975 the tracepoint was originally defined. */
11976
11977struct breakpoint *
11978create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 11979{
409873ef 11980 char *addr_str, small_buf[100];
d5551862 11981 struct breakpoint *tp;
fd9b8c24 11982
409873ef
SS
11983 if (utp->at_string)
11984 addr_str = utp->at_string;
11985 else
11986 {
11987 /* In the absence of a source location, fall back to raw
11988 address. Since there is no way to confirm that the address
11989 means the same thing as when the trace was started, warn the
11990 user. */
3e43a32a
MS
11991 warning (_("Uploaded tracepoint %d has no "
11992 "source location, using raw address"),
409873ef
SS
11993 utp->number);
11994 sprintf (small_buf, "*%s", hex_string (utp->addr));
11995 addr_str = small_buf;
11996 }
11997
11998 /* There's not much we can do with a sequence of bytecodes. */
11999 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12000 warning (_("Uploaded tracepoint %d condition "
12001 "has no source form, ignoring it"),
409873ef 12002 utp->number);
d5551862 12003
8cdf0e15 12004 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12005 addr_str,
12006 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12007 0 /* tempflag */,
0fb4aa4b 12008 utp->type /* type_wanted */,
8cdf0e15
VP
12009 0 /* Ignore count */,
12010 pending_break_support,
12011 NULL,
12012 0 /* from_tty */,
84f4c1fe
PM
12013 utp->enabled /* enabled */,
12014 0 /* internal */))
fd9b8c24
PA
12015 return NULL;
12016
00bf0b85
SS
12017 set_tracepoint_count (breakpoint_count);
12018
409873ef 12019 /* Get the tracepoint we just created. */
fd9b8c24
PA
12020 tp = get_tracepoint (tracepoint_count);
12021 gdb_assert (tp != NULL);
d5551862 12022
00bf0b85
SS
12023 if (utp->pass > 0)
12024 {
409873ef 12025 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 12026
409873ef 12027 trace_pass_command (small_buf, 0);
00bf0b85
SS
12028 }
12029
409873ef
SS
12030 /* If we have uploaded versions of the original commands, set up a
12031 special-purpose "reader" function and call the usual command line
12032 reader, then pass the result to the breakpoint command-setting
12033 function. */
3149d8c1 12034 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12035 {
409873ef 12036 struct command_line *cmd_list;
00bf0b85 12037
409873ef 12038 this_utp = utp;
3149d8c1 12039 next_cmd = 0;
d5551862 12040
409873ef
SS
12041 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12042
12043 breakpoint_set_commands (tp, cmd_list);
00bf0b85 12044 }
3149d8c1
SS
12045 else if (!VEC_empty (char_ptr, utp->actions)
12046 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12047 warning (_("Uploaded tracepoint %d actions "
12048 "have no source form, ignoring them"),
409873ef 12049 utp->number);
00bf0b85
SS
12050
12051 return tp;
12052 }
12053
1042e4c0
SS
12054/* Print information on tracepoint number TPNUM_EXP, or all if
12055 omitted. */
12056
12057static void
e5a67952 12058tracepoints_info (char *args, int from_tty)
1042e4c0 12059{
e5a67952 12060 int num_printed;
1042e4c0 12061
e5a67952 12062 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12063
12064 if (num_printed == 0)
1042e4c0 12065 {
e5a67952 12066 if (args == NULL || *args == '\0')
d77f58be
SS
12067 ui_out_message (uiout, 0, "No tracepoints.\n");
12068 else
e5a67952 12069 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12070 }
ad443146
SS
12071
12072 default_collect_info ();
1042e4c0
SS
12073}
12074
4a64f543 12075/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12076 Not supported by all targets. */
12077static void
12078enable_trace_command (char *args, int from_tty)
12079{
12080 enable_command (args, from_tty);
12081}
12082
4a64f543 12083/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12084 Not supported by all targets. */
12085static void
12086disable_trace_command (char *args, int from_tty)
12087{
12088 disable_command (args, from_tty);
12089}
12090
4a64f543 12091/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12092static void
12093delete_trace_command (char *arg, int from_tty)
12094{
35df4500 12095 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12096
12097 dont_repeat ();
12098
12099 if (arg == 0)
12100 {
12101 int breaks_to_delete = 0;
12102
12103 /* Delete all breakpoints if no argument.
12104 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12105 have to be deleted with an explicit breakpoint number
12106 argument. */
1042e4c0
SS
12107 ALL_TRACEPOINTS (b)
12108 {
12109 if (b->number >= 0)
12110 {
12111 breaks_to_delete = 1;
12112 break;
12113 }
12114 }
12115
12116 /* Ask user only if there are some breakpoints to delete. */
12117 if (!from_tty
12118 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12119 {
35df4500 12120 ALL_BREAKPOINTS_SAFE (b, b_tmp)
1042e4c0 12121 {
d77f58be 12122 if (is_tracepoint (b)
059fb39f 12123 && b->number >= 0)
1042e4c0
SS
12124 delete_breakpoint (b);
12125 }
12126 }
12127 }
12128 else
95a42b64 12129 map_breakpoint_numbers (arg, do_delete_breakpoint, NULL);
1042e4c0
SS
12130}
12131
197f0a60
TT
12132/* Helper function for trace_pass_command. */
12133
12134static void
12135trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12136{
12137 bp->pass_count = count;
12138 observer_notify_tracepoint_modified (bp->number);
12139 if (from_tty)
12140 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12141 bp->number, count);
12142}
12143
1042e4c0
SS
12144/* Set passcount for tracepoint.
12145
12146 First command argument is passcount, second is tracepoint number.
12147 If tracepoint number omitted, apply to most recently defined.
12148 Also accepts special argument "all". */
12149
12150static void
12151trace_pass_command (char *args, int from_tty)
12152{
197f0a60 12153 struct breakpoint *t1;
1042e4c0 12154 unsigned int count;
1042e4c0
SS
12155
12156 if (args == 0 || *args == 0)
3e43a32a
MS
12157 error (_("passcount command requires an "
12158 "argument (count + optional TP num)"));
1042e4c0 12159
4a64f543 12160 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12161
12162 while (*args && isspace ((int) *args))
12163 args++;
12164
12165 if (*args && strncasecmp (args, "all", 3) == 0)
12166 {
12167 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12168 if (*args)
12169 error (_("Junk at end of arguments."));
1042e4c0 12170
197f0a60
TT
12171 ALL_TRACEPOINTS (t1)
12172 {
12173 trace_pass_set_count (t1, count, from_tty);
12174 }
12175 }
12176 else if (*args == '\0')
1042e4c0 12177 {
197f0a60 12178 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12179 if (t1)
197f0a60
TT
12180 trace_pass_set_count (t1, count, from_tty);
12181 }
12182 else
12183 {
12184 struct get_number_or_range_state state;
12185
12186 init_number_or_range (&state, args);
12187 while (!state.finished)
1042e4c0 12188 {
197f0a60
TT
12189 t1 = get_tracepoint_by_number (&args, &state, 1);
12190 if (t1)
12191 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12192 }
12193 }
1042e4c0
SS
12194}
12195
12196struct breakpoint *
12197get_tracepoint (int num)
12198{
12199 struct breakpoint *t;
12200
12201 ALL_TRACEPOINTS (t)
12202 if (t->number == num)
12203 return t;
12204
12205 return NULL;
12206}
12207
d5551862
SS
12208/* Find the tracepoint with the given target-side number (which may be
12209 different from the tracepoint number after disconnecting and
12210 reconnecting). */
12211
12212struct breakpoint *
12213get_tracepoint_by_number_on_target (int num)
12214{
12215 struct breakpoint *t;
12216
12217 ALL_TRACEPOINTS (t)
12218 if (t->number_on_target == num)
12219 return t;
12220
12221 return NULL;
12222}
12223
1042e4c0 12224/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12225 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12226 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
12227 recent tracepoint (tracepoint_count) is returned. */
12228struct breakpoint *
197f0a60
TT
12229get_tracepoint_by_number (char **arg,
12230 struct get_number_or_range_state *state,
12231 int optional_p)
1042e4c0
SS
12232{
12233 extern int tracepoint_count;
12234 struct breakpoint *t;
12235 int tpnum;
12236 char *instring = arg == NULL ? NULL : *arg;
12237
197f0a60
TT
12238 if (state)
12239 {
12240 gdb_assert (!state->finished);
12241 tpnum = get_number_or_range (state);
12242 }
12243 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12244 {
12245 if (optional_p)
12246 tpnum = tracepoint_count;
12247 else
12248 error_no_arg (_("tracepoint number"));
12249 }
12250 else
197f0a60 12251 tpnum = get_number (arg);
1042e4c0
SS
12252
12253 if (tpnum <= 0)
12254 {
12255 if (instring && *instring)
12256 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12257 instring);
12258 else
3e43a32a
MS
12259 printf_filtered (_("Tracepoint argument missing "
12260 "and no previous tracepoint\n"));
1042e4c0
SS
12261 return NULL;
12262 }
12263
12264 ALL_TRACEPOINTS (t)
12265 if (t->number == tpnum)
12266 {
12267 return t;
12268 }
12269
1042e4c0
SS
12270 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12271 return NULL;
12272}
12273
6149aea9
PA
12274/* Save information on user settable breakpoints (watchpoints, etc) to
12275 a new script file named FILENAME. If FILTER is non-NULL, call it
12276 on each breakpoint and only include the ones for which it returns
12277 non-zero. */
12278
1042e4c0 12279static void
6149aea9
PA
12280save_breakpoints (char *filename, int from_tty,
12281 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12282{
12283 struct breakpoint *tp;
6149aea9 12284 int any = 0;
a7bdde9e 12285 char *pathname;
1042e4c0 12286 struct cleanup *cleanup;
a7bdde9e 12287 struct ui_file *fp;
6149aea9 12288 int extra_trace_bits = 0;
1042e4c0 12289
6149aea9
PA
12290 if (filename == 0 || *filename == 0)
12291 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12292
12293 /* See if we have anything to save. */
6149aea9 12294 ALL_BREAKPOINTS (tp)
1042e4c0 12295 {
6149aea9 12296 /* Skip internal and momentary breakpoints. */
09d682a4 12297 if (!user_breakpoint_p (tp))
6149aea9
PA
12298 continue;
12299
12300 /* If we have a filter, only save the breakpoints it accepts. */
12301 if (filter && !filter (tp))
12302 continue;
12303
12304 any = 1;
12305
12306 if (is_tracepoint (tp))
12307 {
12308 extra_trace_bits = 1;
12309
12310 /* We can stop searching. */
12311 break;
12312 }
1042e4c0 12313 }
6149aea9
PA
12314
12315 if (!any)
1042e4c0 12316 {
6149aea9 12317 warning (_("Nothing to save."));
1042e4c0
SS
12318 return;
12319 }
12320
6149aea9 12321 pathname = tilde_expand (filename);
1042e4c0 12322 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 12323 fp = gdb_fopen (pathname, "w");
059fb39f 12324 if (!fp)
6149aea9
PA
12325 error (_("Unable to open file '%s' for saving (%s)"),
12326 filename, safe_strerror (errno));
a7bdde9e 12327 make_cleanup_ui_file_delete (fp);
8bf6485c 12328
6149aea9
PA
12329 if (extra_trace_bits)
12330 save_trace_state_variables (fp);
8bf6485c 12331
6149aea9 12332 ALL_BREAKPOINTS (tp)
1042e4c0 12333 {
6149aea9 12334 /* Skip internal and momentary breakpoints. */
09d682a4 12335 if (!user_breakpoint_p (tp))
6149aea9 12336 continue;
8bf6485c 12337
6149aea9
PA
12338 /* If we have a filter, only save the breakpoints it accepts. */
12339 if (filter && !filter (tp))
12340 continue;
12341
12342 if (tp->ops != NULL)
12343 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
12344 else
12345 {
6149aea9
PA
12346 if (tp->type == bp_fast_tracepoint)
12347 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
12348 if (tp->type == bp_static_tracepoint)
12349 fprintf_unfiltered (fp, "strace");
6149aea9
PA
12350 else if (tp->type == bp_tracepoint)
12351 fprintf_unfiltered (fp, "trace");
12352 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12353 fprintf_unfiltered (fp, "tbreak");
12354 else if (tp->type == bp_breakpoint)
12355 fprintf_unfiltered (fp, "break");
12356 else if (tp->type == bp_hardware_breakpoint
12357 && tp->disposition == disp_del)
12358 fprintf_unfiltered (fp, "thbreak");
12359 else if (tp->type == bp_hardware_breakpoint)
12360 fprintf_unfiltered (fp, "hbreak");
12361 else if (tp->type == bp_watchpoint)
12362 fprintf_unfiltered (fp, "watch");
12363 else if (tp->type == bp_hardware_watchpoint)
12364 fprintf_unfiltered (fp, "watch");
12365 else if (tp->type == bp_read_watchpoint)
12366 fprintf_unfiltered (fp, "rwatch");
12367 else if (tp->type == bp_access_watchpoint)
12368 fprintf_unfiltered (fp, "awatch");
12369 else
12370 internal_error (__FILE__, __LINE__,
12371 _("unhandled breakpoint type %d"), (int) tp->type);
12372
12373 if (tp->exp_string)
12374 fprintf_unfiltered (fp, " %s", tp->exp_string);
12375 else if (tp->addr_string)
12376 fprintf_unfiltered (fp, " %s", tp->addr_string);
12377 else
12378 {
12379 char tmp[40];
12380
12381 sprintf_vma (tmp, tp->loc->address);
12382 fprintf_unfiltered (fp, " *0x%s", tmp);
12383 }
1042e4c0
SS
12384 }
12385
6149aea9
PA
12386 if (tp->thread != -1)
12387 fprintf_unfiltered (fp, " thread %d", tp->thread);
12388
12389 if (tp->task != 0)
12390 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12391
12392 fprintf_unfiltered (fp, "\n");
12393
6149aea9
PA
12394 /* Note, we can't rely on tp->number for anything, as we can't
12395 assume the recreated breakpoint numbers will match. Use $bpnum
12396 instead. */
12397
12398 if (tp->cond_string)
12399 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12400
12401 if (tp->ignore_count)
12402 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12403
1042e4c0 12404 if (tp->pass_count)
a7bdde9e 12405 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12406
a7bdde9e 12407 if (tp->commands)
1042e4c0 12408 {
a7bdde9e
VP
12409 volatile struct gdb_exception ex;
12410
6149aea9 12411 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12412
12413 ui_out_redirect (uiout, fp);
14dba4b4 12414 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12415 {
9add0f1b 12416 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12417 }
12418 ui_out_redirect (uiout, NULL);
1042e4c0 12419
a7bdde9e
VP
12420 if (ex.reason < 0)
12421 throw_exception (ex);
1042e4c0 12422
a7bdde9e 12423 fprintf_unfiltered (fp, " end\n");
1042e4c0 12424 }
6149aea9
PA
12425
12426 if (tp->enable_state == bp_disabled)
12427 fprintf_unfiltered (fp, "disable\n");
12428
12429 /* If this is a multi-location breakpoint, check if the locations
12430 should be individually disabled. Watchpoint locations are
12431 special, and not user visible. */
12432 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12433 {
12434 struct bp_location *loc;
12435 int n = 1;
12436
12437 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12438 if (!loc->enabled)
12439 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12440 }
1042e4c0 12441 }
8bf6485c 12442
6149aea9 12443 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12444 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12445
1042e4c0
SS
12446 do_cleanups (cleanup);
12447 if (from_tty)
6149aea9
PA
12448 printf_filtered (_("Saved to file '%s'.\n"), filename);
12449}
12450
12451/* The `save breakpoints' command. */
12452
12453static void
12454save_breakpoints_command (char *args, int from_tty)
12455{
12456 save_breakpoints (args, from_tty, NULL);
12457}
12458
12459/* The `save tracepoints' command. */
12460
12461static void
12462save_tracepoints_command (char *args, int from_tty)
12463{
12464 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12465}
12466
12467/* Create a vector of all tracepoints. */
12468
12469VEC(breakpoint_p) *
eeae04df 12470all_tracepoints (void)
1042e4c0
SS
12471{
12472 VEC(breakpoint_p) *tp_vec = 0;
12473 struct breakpoint *tp;
12474
12475 ALL_TRACEPOINTS (tp)
12476 {
12477 VEC_safe_push (breakpoint_p, tp_vec, tp);
12478 }
12479
12480 return tp_vec;
12481}
12482
c906108c 12483\f
4a64f543
MS
12484/* This help string is used for the break, hbreak, tbreak and thbreak
12485 commands. It is defined as a macro to prevent duplication.
12486 COMMAND should be a string constant containing the name of the
12487 command. */
31e2b00f
AS
12488#define BREAK_ARGS_HELP(command) \
12489command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12490LOCATION may be a line number, function name, or \"*\" and an address.\n\
12491If a line number is specified, break at start of code for that line.\n\
12492If a function is specified, break at start of code for that function.\n\
12493If an address is specified, break at that exact address.\n\
dc10affe
PA
12494With no LOCATION, uses current execution address of the selected\n\
12495stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12496\n\
12497THREADNUM is the number from \"info threads\".\n\
12498CONDITION is a boolean expression.\n\
12499\n\
d41c0fc8
PA
12500Multiple breakpoints at one place are permitted, and useful if their\n\
12501conditions are different.\n\
31e2b00f
AS
12502\n\
12503Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12504
44feb3ce
TT
12505/* List of subcommands for "catch". */
12506static struct cmd_list_element *catch_cmdlist;
12507
12508/* List of subcommands for "tcatch". */
12509static struct cmd_list_element *tcatch_cmdlist;
12510
12511/* Like add_cmd, but add the command to both the "catch" and "tcatch"
12512 lists, and pass some additional user data to the command function. */
12513static void
12514add_catch_command (char *name, char *docstring,
12515 void (*sfunc) (char *args, int from_tty,
12516 struct cmd_list_element *command),
a96d9b2e
SDJ
12517 char **(*completer) (struct cmd_list_element *cmd,
12518 char *text, char *word),
44feb3ce
TT
12519 void *user_data_catch,
12520 void *user_data_tcatch)
12521{
12522 struct cmd_list_element *command;
12523
12524 command = add_cmd (name, class_breakpoint, NULL, docstring,
12525 &catch_cmdlist);
12526 set_cmd_sfunc (command, sfunc);
12527 set_cmd_context (command, user_data_catch);
a96d9b2e 12528 set_cmd_completer (command, completer);
44feb3ce
TT
12529
12530 command = add_cmd (name, class_breakpoint, NULL, docstring,
12531 &tcatch_cmdlist);
12532 set_cmd_sfunc (command, sfunc);
12533 set_cmd_context (command, user_data_tcatch);
a96d9b2e 12534 set_cmd_completer (command, completer);
44feb3ce
TT
12535}
12536
6c95b8df 12537static void
a79b8f6e 12538clear_syscall_counts (struct inferior *inf)
6c95b8df 12539{
6c95b8df
PA
12540 inf->total_syscalls_count = 0;
12541 inf->any_syscall_count = 0;
12542 VEC_free (int, inf->syscalls_counts);
12543}
12544
6149aea9
PA
12545static void
12546save_command (char *arg, int from_tty)
12547{
3e43a32a
MS
12548 printf_unfiltered (_("\"save\" must be followed by "
12549 "the name of a save subcommand.\n"));
6149aea9
PA
12550 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12551}
12552
84f4c1fe
PM
12553struct breakpoint *
12554iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12555 void *data)
12556{
35df4500 12557 struct breakpoint *b, *b_tmp;
84f4c1fe 12558
35df4500 12559 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12560 {
12561 if ((*callback) (b, data))
12562 return b;
12563 }
12564
12565 return NULL;
12566}
12567
c906108c 12568void
fba45db2 12569_initialize_breakpoint (void)
c906108c
SS
12570{
12571 struct cmd_list_element *c;
12572
84acb35a 12573 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 12574 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 12575 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 12576
17450429
PP
12577 breakpoint_objfile_key = register_objfile_data ();
12578
c906108c
SS
12579 breakpoint_chain = 0;
12580 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
12581 before a breakpoint is set. */
12582 breakpoint_count = 0;
12583
1042e4c0
SS
12584 tracepoint_count = 0;
12585
1bedd215
AC
12586 add_com ("ignore", class_breakpoint, ignore_command, _("\
12587Set ignore-count of breakpoint number N to COUNT.\n\
12588Usage is `ignore N COUNT'."));
c906108c 12589 if (xdb_commands)
c5aa993b 12590 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 12591
1bedd215
AC
12592 add_com ("commands", class_breakpoint, commands_command, _("\
12593Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
12594Give breakpoint number as argument after \"commands\".\n\
12595With no argument, the targeted breakpoint is the last one set.\n\
12596The commands themselves follow starting on the next line.\n\
12597Type a line containing \"end\" to indicate the end of them.\n\
12598Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 12599then no output is printed when it is hit, except what the commands print."));
c906108c 12600
1bedd215
AC
12601 add_com ("condition", class_breakpoint, condition_command, _("\
12602Specify breakpoint number N to break only if COND is true.\n\
c906108c 12603Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 12604expression to be evaluated whenever breakpoint N is reached."));
c906108c 12605
1bedd215 12606 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 12607Set a temporary breakpoint.\n\
c906108c
SS
12608Like \"break\" except the breakpoint is only temporary,\n\
12609so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
12610by using \"enable delete\" on the breakpoint number.\n\
12611\n"
12612BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 12613 set_cmd_completer (c, location_completer);
c94fdfd0 12614
1bedd215 12615 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 12616Set a hardware assisted breakpoint.\n\
c906108c 12617Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
12618some target hardware may not have this support.\n\
12619\n"
12620BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 12621 set_cmd_completer (c, location_completer);
c906108c 12622
1bedd215 12623 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 12624Set a temporary hardware assisted breakpoint.\n\
c906108c 12625Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
12626so it will be deleted when hit.\n\
12627\n"
12628BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 12629 set_cmd_completer (c, location_completer);
c906108c 12630
1bedd215
AC
12631 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
12632Enable some breakpoints.\n\
c906108c
SS
12633Give breakpoint numbers (separated by spaces) as arguments.\n\
12634With no subcommand, breakpoints are enabled until you command otherwise.\n\
12635This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12636With a subcommand you can enable temporarily."),
c906108c
SS
12637 &enablelist, "enable ", 1, &cmdlist);
12638 if (xdb_commands)
1bedd215
AC
12639 add_com ("ab", class_breakpoint, enable_command, _("\
12640Enable some breakpoints.\n\
c906108c
SS
12641Give breakpoint numbers (separated by spaces) as arguments.\n\
12642With no subcommand, breakpoints are enabled until you command otherwise.\n\
12643This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12644With a subcommand you can enable temporarily."));
c906108c
SS
12645
12646 add_com_alias ("en", "enable", class_breakpoint, 1);
12647
84951ab5 12648 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 12649Enable some breakpoints.\n\
c906108c
SS
12650Give breakpoint numbers (separated by spaces) as arguments.\n\
12651This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 12652May be abbreviated to simply \"enable\".\n"),
c5aa993b 12653 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 12654
1a966eab
AC
12655 add_cmd ("once", no_class, enable_once_command, _("\
12656Enable breakpoints for one hit. Give breakpoint numbers.\n\
12657If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12658 &enablebreaklist);
12659
1a966eab
AC
12660 add_cmd ("delete", no_class, enable_delete_command, _("\
12661Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12662If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12663 &enablebreaklist);
12664
1a966eab
AC
12665 add_cmd ("delete", no_class, enable_delete_command, _("\
12666Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
12667If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
12668 &enablelist);
12669
1a966eab
AC
12670 add_cmd ("once", no_class, enable_once_command, _("\
12671Enable breakpoints for one hit. Give breakpoint numbers.\n\
12672If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
12673 &enablelist);
12674
1bedd215
AC
12675 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
12676Disable some breakpoints.\n\
c906108c
SS
12677Arguments are breakpoint numbers with spaces in between.\n\
12678To disable all breakpoints, give no argument.\n\
1bedd215 12679A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
12680 &disablelist, "disable ", 1, &cmdlist);
12681 add_com_alias ("dis", "disable", class_breakpoint, 1);
12682 add_com_alias ("disa", "disable", class_breakpoint, 1);
12683 if (xdb_commands)
1bedd215
AC
12684 add_com ("sb", class_breakpoint, disable_command, _("\
12685Disable some breakpoints.\n\
c906108c
SS
12686Arguments are breakpoint numbers with spaces in between.\n\
12687To disable all breakpoints, give no argument.\n\
1bedd215 12688A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 12689
1a966eab
AC
12690 add_cmd ("breakpoints", class_alias, disable_command, _("\
12691Disable some breakpoints.\n\
c906108c
SS
12692Arguments are breakpoint numbers with spaces in between.\n\
12693To disable all breakpoints, give no argument.\n\
12694A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 12695This command may be abbreviated \"disable\"."),
c906108c
SS
12696 &disablelist);
12697
1bedd215
AC
12698 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
12699Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12700Arguments are breakpoint numbers with spaces in between.\n\
12701To delete all breakpoints, give no argument.\n\
12702\n\
12703Also a prefix command for deletion of other GDB objects.\n\
1bedd215 12704The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
12705 &deletelist, "delete ", 1, &cmdlist);
12706 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 12707 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 12708 if (xdb_commands)
1bedd215
AC
12709 add_com ("db", class_breakpoint, delete_command, _("\
12710Delete some breakpoints.\n\
c906108c 12711Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 12712To delete all breakpoints, give no argument.\n"));
c906108c 12713
1a966eab
AC
12714 add_cmd ("breakpoints", class_alias, delete_command, _("\
12715Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
12716Arguments are breakpoint numbers with spaces in between.\n\
12717To delete all breakpoints, give no argument.\n\
1a966eab 12718This command may be abbreviated \"delete\"."),
c906108c
SS
12719 &deletelist);
12720
1bedd215
AC
12721 add_com ("clear", class_breakpoint, clear_command, _("\
12722Clear breakpoint at specified line or function.\n\
c906108c
SS
12723Argument may be line number, function name, or \"*\" and an address.\n\
12724If line number is specified, all breakpoints in that line are cleared.\n\
12725If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
12726If an address is specified, breakpoints at that address are cleared.\n\
12727\n\
12728With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
12729is executing in.\n\
12730\n\
1bedd215 12731See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 12732 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 12733
1bedd215 12734 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
12735Set breakpoint at specified line or function.\n"
12736BREAK_ARGS_HELP ("break")));
5ba2abeb 12737 set_cmd_completer (c, location_completer);
c94fdfd0 12738
c906108c
SS
12739 add_com_alias ("b", "break", class_run, 1);
12740 add_com_alias ("br", "break", class_run, 1);
12741 add_com_alias ("bre", "break", class_run, 1);
12742 add_com_alias ("brea", "break", class_run, 1);
12743
7681d515
PM
12744 if (xdb_commands)
12745 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
12746
12747 if (dbx_commands)
12748 {
1bedd215
AC
12749 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
12750Break in function/address or break at a line in the current file."),
c5aa993b
JM
12751 &stoplist, "stop ", 1, &cmdlist);
12752 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 12753 _("Break in function or address."), &stoplist);
c5aa993b 12754 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 12755 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
12756 add_com ("status", class_info, breakpoints_info, _("\
12757Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12758The \"Type\" column indicates one of:\n\
12759\tbreakpoint - normal breakpoint\n\
12760\twatchpoint - watchpoint\n\
12761The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12762the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12763breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12764address and file/line number respectively.\n\
12765\n\
12766Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12767are set to the address of the last breakpoint listed unless the command\n\
12768is prefixed with \"server \".\n\n\
c906108c 12769Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12770breakpoint set."));
c906108c
SS
12771 }
12772
1bedd215 12773 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 12774Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
12775The \"Type\" column indicates one of:\n\
12776\tbreakpoint - normal breakpoint\n\
12777\twatchpoint - watchpoint\n\
12778The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12779the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12780breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12781address and file/line number respectively.\n\
12782\n\
12783Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12784are set to the address of the last breakpoint listed unless the command\n\
12785is prefixed with \"server \".\n\n\
c906108c 12786Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12787breakpoint set."));
c906108c 12788
6b04bdb7
MS
12789 add_info_alias ("b", "breakpoints", 1);
12790
c906108c 12791 if (xdb_commands)
1bedd215
AC
12792 add_com ("lb", class_breakpoint, breakpoints_info, _("\
12793Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12794The \"Type\" column indicates one of:\n\
12795\tbreakpoint - normal breakpoint\n\
12796\twatchpoint - watchpoint\n\
12797The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
12798the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12799breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
12800address and file/line number respectively.\n\
12801\n\
12802Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12803are set to the address of the last breakpoint listed unless the command\n\
12804is prefixed with \"server \".\n\n\
c906108c 12805Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 12806breakpoint set."));
c906108c 12807
1a966eab
AC
12808 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
12809Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
12810The \"Type\" column indicates one of:\n\
12811\tbreakpoint - normal breakpoint\n\
12812\twatchpoint - watchpoint\n\
12813\tlongjmp - internal breakpoint used to step through longjmp()\n\
12814\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
12815\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
12816\tfinish - internal breakpoint used by the \"finish\" command\n\
12817The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
12818the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
12819breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
12820address and file/line number respectively.\n\
12821\n\
12822Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
12823are set to the address of the last breakpoint listed unless the command\n\
12824is prefixed with \"server \".\n\n\
c906108c 12825Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 12826breakpoint set."),
c906108c
SS
12827 &maintenanceinfolist);
12828
44feb3ce
TT
12829 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
12830Set catchpoints to catch events."),
12831 &catch_cmdlist, "catch ",
12832 0/*allow-unknown*/, &cmdlist);
12833
12834 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
12835Set temporary catchpoints to catch events."),
12836 &tcatch_cmdlist, "tcatch ",
12837 0/*allow-unknown*/, &cmdlist);
12838
12839 /* Add catch and tcatch sub-commands. */
12840 add_catch_command ("catch", _("\
12841Catch an exception, when caught.\n\
12842With an argument, catch only exceptions with the given name."),
12843 catch_catch_command,
a96d9b2e 12844 NULL,
44feb3ce
TT
12845 CATCH_PERMANENT,
12846 CATCH_TEMPORARY);
12847 add_catch_command ("throw", _("\
12848Catch an exception, when thrown.\n\
12849With an argument, catch only exceptions with the given name."),
12850 catch_throw_command,
a96d9b2e 12851 NULL,
44feb3ce
TT
12852 CATCH_PERMANENT,
12853 CATCH_TEMPORARY);
12854 add_catch_command ("fork", _("Catch calls to fork."),
12855 catch_fork_command_1,
a96d9b2e 12856 NULL,
44feb3ce
TT
12857 (void *) (uintptr_t) catch_fork_permanent,
12858 (void *) (uintptr_t) catch_fork_temporary);
12859 add_catch_command ("vfork", _("Catch calls to vfork."),
12860 catch_fork_command_1,
a96d9b2e 12861 NULL,
44feb3ce
TT
12862 (void *) (uintptr_t) catch_vfork_permanent,
12863 (void *) (uintptr_t) catch_vfork_temporary);
12864 add_catch_command ("exec", _("Catch calls to exec."),
12865 catch_exec_command_1,
a96d9b2e
SDJ
12866 NULL,
12867 CATCH_PERMANENT,
12868 CATCH_TEMPORARY);
12869 add_catch_command ("syscall", _("\
12870Catch system calls by their names and/or numbers.\n\
12871Arguments say which system calls to catch. If no arguments\n\
12872are given, every system call will be caught.\n\
12873Arguments, if given, should be one or more system call names\n\
12874(if your system supports that), or system call numbers."),
12875 catch_syscall_command_1,
12876 catch_syscall_completer,
44feb3ce
TT
12877 CATCH_PERMANENT,
12878 CATCH_TEMPORARY);
44feb3ce
TT
12879 add_catch_command ("exception", _("\
12880Catch Ada exceptions, when raised.\n\
12881With an argument, catch only exceptions with the given name."),
12882 catch_ada_exception_command,
a96d9b2e 12883 NULL,
44feb3ce
TT
12884 CATCH_PERMANENT,
12885 CATCH_TEMPORARY);
12886 add_catch_command ("assert", _("\
12887Catch failed Ada assertions, when raised.\n\
12888With an argument, catch only exceptions with the given name."),
12889 catch_assert_command,
a96d9b2e 12890 NULL,
44feb3ce
TT
12891 CATCH_PERMANENT,
12892 CATCH_TEMPORARY);
c5aa993b 12893
1bedd215
AC
12894 c = add_com ("watch", class_breakpoint, watch_command, _("\
12895Set a watchpoint for an expression.\n\
06a64a0b 12896Usage: watch [-l|-location] EXPRESSION\n\
c906108c 12897A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12898an expression changes.\n\
12899If -l or -location is given, this evaluates EXPRESSION and watches\n\
12900the memory to which it refers."));
65d12d83 12901 set_cmd_completer (c, expression_completer);
c906108c 12902
1bedd215
AC
12903 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
12904Set a read watchpoint for an expression.\n\
06a64a0b 12905Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 12906A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12907an expression is read.\n\
12908If -l or -location is given, this evaluates EXPRESSION and watches\n\
12909the memory to which it refers."));
65d12d83 12910 set_cmd_completer (c, expression_completer);
c906108c 12911
1bedd215
AC
12912 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
12913Set a watchpoint for an expression.\n\
06a64a0b 12914Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 12915A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
12916an expression is either read or written.\n\
12917If -l or -location is given, this evaluates EXPRESSION and watches\n\
12918the memory to which it refers."));
65d12d83 12919 set_cmd_completer (c, expression_completer);
c906108c 12920
d77f58be 12921 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 12922Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 12923
920d2a44
AC
12924 /* XXX: cagney/2005-02-23: This should be a boolean, and should
12925 respond to changes - contrary to the description. */
85c07804
AC
12926 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
12927 &can_use_hw_watchpoints, _("\
12928Set debugger's willingness to use watchpoint hardware."), _("\
12929Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
12930If zero, gdb will not use hardware for new watchpoints, even if\n\
12931such is available. (However, any hardware watchpoints that were\n\
12932created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
12933hardware.)"),
12934 NULL,
920d2a44 12935 show_can_use_hw_watchpoints,
85c07804 12936 &setlist, &showlist);
c906108c
SS
12937
12938 can_use_hw_watchpoints = 1;
fa8d40ab 12939
1042e4c0
SS
12940 /* Tracepoint manipulation commands. */
12941
12942 c = add_com ("trace", class_breakpoint, trace_command, _("\
12943Set a tracepoint at specified line or function.\n\
12944\n"
12945BREAK_ARGS_HELP ("trace") "\n\
12946Do \"help tracepoints\" for info on other tracepoint commands."));
12947 set_cmd_completer (c, location_completer);
12948
12949 add_com_alias ("tp", "trace", class_alias, 0);
12950 add_com_alias ("tr", "trace", class_alias, 1);
12951 add_com_alias ("tra", "trace", class_alias, 1);
12952 add_com_alias ("trac", "trace", class_alias, 1);
12953
7a697b8d
SS
12954 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
12955Set a fast tracepoint at specified line or function.\n\
12956\n"
12957BREAK_ARGS_HELP ("ftrace") "\n\
12958Do \"help tracepoints\" for info on other tracepoint commands."));
12959 set_cmd_completer (c, location_completer);
12960
0fb4aa4b
PA
12961 c = add_com ("strace", class_breakpoint, strace_command, _("\
12962Set a static tracepoint at specified line, function or marker.\n\
12963\n\
12964strace [LOCATION] [if CONDITION]\n\
12965LOCATION may be a line number, function name, \"*\" and an address,\n\
12966or -m MARKER_ID.\n\
12967If a line number is specified, probe the marker at start of code\n\
12968for that line. If a function is specified, probe the marker at start\n\
12969of code for that function. If an address is specified, probe the marker\n\
12970at that exact address. If a marker id is specified, probe the marker\n\
12971with that name. With no LOCATION, uses current execution address of\n\
12972the selected stack frame.\n\
12973Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
12974This collects arbitrary user data passed in the probe point call to the\n\
12975tracing library. You can inspect it when analyzing the trace buffer,\n\
12976by printing the $_sdata variable like any other convenience variable.\n\
12977\n\
12978CONDITION is a boolean expression.\n\
12979\n\
d41c0fc8
PA
12980Multiple tracepoints at one place are permitted, and useful if their\n\
12981conditions are different.\n\
0fb4aa4b
PA
12982\n\
12983Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
12984Do \"help tracepoints\" for info on other tracepoint commands."));
12985 set_cmd_completer (c, location_completer);
12986
1042e4c0 12987 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 12988Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
12989Convenience variable \"$tpnum\" contains the number of the\n\
12990last tracepoint set."));
12991
12992 add_info_alias ("tp", "tracepoints", 1);
12993
12994 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
12995Delete specified tracepoints.\n\
12996Arguments are tracepoint numbers, separated by spaces.\n\
12997No argument means delete all tracepoints."),
12998 &deletelist);
12999
13000 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13001Disable specified tracepoints.\n\
13002Arguments are tracepoint numbers, separated by spaces.\n\
13003No argument means disable all tracepoints."),
13004 &disablelist);
13005 deprecate_cmd (c, "disable");
13006
13007 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13008Enable specified tracepoints.\n\
13009Arguments are tracepoint numbers, separated by spaces.\n\
13010No argument means enable all tracepoints."),
13011 &enablelist);
13012 deprecate_cmd (c, "enable");
13013
13014 add_com ("passcount", class_trace, trace_pass_command, _("\
13015Set the passcount for a tracepoint.\n\
13016The trace will end when the tracepoint has been passed 'count' times.\n\
13017Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13018if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13019
6149aea9
PA
13020 add_prefix_cmd ("save", class_breakpoint, save_command,
13021 _("Save breakpoint definitions as a script."),
13022 &save_cmdlist, "save ",
13023 0/*allow-unknown*/, &cmdlist);
13024
13025 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13026Save current breakpoint definitions as a script.\n\
cce7e648 13027This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13028catchpoints, tracepoints). Use the 'source' command in another debug\n\
13029session to restore them."),
13030 &save_cmdlist);
13031 set_cmd_completer (c, filename_completer);
13032
13033 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13034Save current tracepoint definitions as a script.\n\
6149aea9
PA
13035Use the 'source' command in another debug session to restore them."),
13036 &save_cmdlist);
1042e4c0
SS
13037 set_cmd_completer (c, filename_completer);
13038
6149aea9
PA
13039 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13040 deprecate_cmd (c, "save tracepoints");
13041
1bedd215 13042 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13043Breakpoint specific settings\n\
13044Configure various breakpoint-specific variables such as\n\
1bedd215 13045pending breakpoint behavior"),
fa8d40ab
JJ
13046 &breakpoint_set_cmdlist, "set breakpoint ",
13047 0/*allow-unknown*/, &setlist);
1bedd215 13048 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13049Breakpoint specific settings\n\
13050Configure various breakpoint-specific variables such as\n\
1bedd215 13051pending breakpoint behavior"),
fa8d40ab
JJ
13052 &breakpoint_show_cmdlist, "show breakpoint ",
13053 0/*allow-unknown*/, &showlist);
13054
7915a72c
AC
13055 add_setshow_auto_boolean_cmd ("pending", no_class,
13056 &pending_break_support, _("\
13057Set debugger's behavior regarding pending breakpoints."), _("\
13058Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13059If on, an unrecognized breakpoint location will cause gdb to create a\n\
13060pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13061an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13062user-query to see if a pending breakpoint should be created."),
2c5b56ce 13063 NULL,
920d2a44 13064 show_pending_break_support,
6e1d7d6c
AC
13065 &breakpoint_set_cmdlist,
13066 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13067
13068 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13069
13070 add_setshow_boolean_cmd ("auto-hw", no_class,
13071 &automatic_hardware_breakpoints, _("\
13072Set automatic usage of hardware breakpoints."), _("\
13073Show automatic usage of hardware breakpoints."), _("\
13074If set, the debugger will automatically use hardware breakpoints for\n\
13075breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13076a warning will be emitted for such breakpoints."),
13077 NULL,
13078 show_automatic_hardware_breakpoints,
13079 &breakpoint_set_cmdlist,
13080 &breakpoint_show_cmdlist);
74960c60 13081
33e5cbd6
PA
13082 add_setshow_enum_cmd ("always-inserted", class_support,
13083 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13084Set mode for inserting breakpoints."), _("\
13085Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13086When this mode is off, breakpoints are inserted in inferior when it is\n\
13087resumed, and removed when execution stops. When this mode is on,\n\
13088breakpoints are inserted immediately and removed only when the user\n\
13089deletes the breakpoint. When this mode is auto (which is the default),\n\
13090the behaviour depends on the non-stop setting (see help set non-stop).\n\
13091In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13092behaves as if always-inserted mode is on; if gdb is controlling the\n\
13093inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13094 NULL,
13095 &show_always_inserted_mode,
13096 &breakpoint_set_cmdlist,
13097 &breakpoint_show_cmdlist);
f1310107
TJB
13098
13099 add_com ("break-range", class_breakpoint, break_range_command, _("\
13100Set a breakpoint for an address range.\n\
13101break-range START-LOCATION, END-LOCATION\n\
13102where START-LOCATION and END-LOCATION can be one of the following:\n\
13103 LINENUM, for that line in the current file,\n\
13104 FILE:LINENUM, for that line in that file,\n\
13105 +OFFSET, for that number of lines after the current line\n\
13106 or the start of the range\n\
13107 FUNCTION, for the first line in that function,\n\
13108 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13109 *ADDRESS, for the instruction at that address.\n\
13110\n\
13111The breakpoint will stop execution of the inferior whenever it executes\n\
13112an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13113range (including START-LOCATION and END-LOCATION)."));
13114
765dc015 13115 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13116
13117 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13118}
This page took 2.559215 seconds and 4 git commands to generate.