2011-06-22 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1 4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7b6bb8da 5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
a6d9a66e 23#include "arch-utils.h"
c906108c 24#include <ctype.h>
776592bf 25#include "hashtab.h"
c906108c
SS
26#include "symtab.h"
27#include "frame.h"
28#include "breakpoint.h"
1042e4c0 29#include "tracepoint.h"
c906108c
SS
30#include "gdbtypes.h"
31#include "expression.h"
32#include "gdbcore.h"
33#include "gdbcmd.h"
34#include "value.h"
35#include "command.h"
36#include "inferior.h"
37#include "gdbthread.h"
38#include "target.h"
39#include "language.h"
40#include "gdb_string.h"
41#include "demangle.h"
0ba1096a 42#include "filenames.h"
c906108c
SS
43#include "annotate.h"
44#include "symfile.h"
45#include "objfiles.h"
0378c332 46#include "source.h"
c5f0f3d0 47#include "linespec.h"
c94fdfd0 48#include "completer.h"
5b7f31a4 49#include "gdb.h"
8b93c638 50#include "ui-out.h"
e1507482 51#include "cli/cli-script.h"
0225421b 52#include "gdb_assert.h"
fe898f56 53#include "block.h"
a77053c2 54#include "solib.h"
84acb35a
JJ
55#include "solist.h"
56#include "observer.h"
60250e8b 57#include "exceptions.h"
765dc015 58#include "memattr.h"
f7f9143b 59#include "ada-lang.h"
d1aa2f50 60#include "top.h"
fa4727a6 61#include "wrapper.h"
79a45b7d 62#include "valprint.h"
4efc6507 63#include "jit.h"
a96d9b2e 64#include "xml-syscall.h"
65d79d4b 65#include "parser-defs.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
c906108c 68
1042e4c0
SS
69/* readline include files */
70#include "readline/readline.h"
71#include "readline/history.h"
72
73/* readline defines this. */
74#undef savestring
75
034dad6f 76#include "mi/mi-common.h"
7371cf6d 77#include "python/python.h"
104c1213 78
4a64f543 79/* Prototypes for local functions. */
c906108c 80
a14ed312 81static void enable_delete_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_command (char *, int);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
95a42b64
TT
89static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
90 void *),
91 void *);
c906108c 92
a14ed312 93static void ignore_command (char *, int);
c906108c 94
4efb68b1 95static int breakpoint_re_set_one (void *);
c906108c 96
a14ed312 97static void clear_command (char *, int);
c906108c 98
a14ed312 99static void catch_command (char *, int);
c906108c 100
a9634178 101static int can_use_hardware_watchpoint (struct value *);
c906108c 102
98deb0da 103static void break_command_1 (char *, int, int);
c906108c 104
a14ed312 105static void mention (struct breakpoint *);
c906108c 106
4a64f543
MS
107/* This function is used in gdbtk sources and thus can not be made
108 static. */
63c252f8 109struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
a6d9a66e
UW
110 struct symtab_and_line,
111 enum bptype);
c906108c 112
76897487
KB
113static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
114
a6d9a66e
UW
115static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
116 CORE_ADDR bpaddr,
88f7da05 117 enum bptype bptype);
76897487 118
6c95b8df
PA
119static void describe_other_breakpoints (struct gdbarch *,
120 struct program_space *, CORE_ADDR,
5af949e3 121 struct obj_section *, int);
c906108c 122
6c95b8df
PA
123static int breakpoint_address_match (struct address_space *aspace1,
124 CORE_ADDR addr1,
125 struct address_space *aspace2,
126 CORE_ADDR addr2);
127
85d721b8
PA
128static int watchpoint_locations_match (struct bp_location *loc1,
129 struct bp_location *loc2);
130
f1310107
TJB
131static int breakpoint_location_address_match (struct bp_location *bl,
132 struct address_space *aspace,
133 CORE_ADDR addr);
134
a14ed312 135static void breakpoints_info (char *, int);
c906108c 136
d77f58be
SS
137static void watchpoints_info (char *, int);
138
e5a67952
MS
139static int breakpoint_1 (char *, int,
140 int (*) (const struct breakpoint *));
c906108c 141
4efb68b1 142static int breakpoint_cond_eval (void *);
c906108c 143
4efb68b1 144static void cleanup_executing_breakpoints (void *);
c906108c 145
a14ed312 146static void commands_command (char *, int);
c906108c 147
a14ed312 148static void condition_command (char *, int);
c906108c 149
c5aa993b
JM
150typedef enum
151 {
152 mark_inserted,
153 mark_uninserted
154 }
155insertion_state_t;
c906108c 156
0bde7532 157static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 158static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 159
a14ed312 160static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
161
162static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 163
4efb68b1 164static int watchpoint_check (void *);
c906108c 165
a14ed312 166static void maintenance_info_breakpoints (char *, int);
c906108c 167
a14ed312 168static int hw_breakpoint_used_count (void);
c906108c 169
a14ed312 170static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 171
a14ed312 172static void hbreak_command (char *, int);
c906108c 173
a14ed312 174static void thbreak_command (char *, int);
c906108c 175
51be5b68 176static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 177
a14ed312 178static void stop_command (char *arg, int from_tty);
7a292a7a 179
a14ed312 180static void stopin_command (char *arg, int from_tty);
7a292a7a 181
a14ed312 182static void stopat_command (char *arg, int from_tty);
7a292a7a 183
a14ed312 184static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 185
d85310f7
MS
186static void catch_exception_command_1 (enum exception_event_kind ex_event,
187 char *arg, int tempflag, int from_tty);
7a292a7a 188
a14ed312 189static void tcatch_command (char *arg, int from_tty);
7a292a7a 190
d03285ec
UW
191static void detach_single_step_breakpoints (void);
192
6c95b8df
PA
193static int single_step_breakpoint_inserted_here_p (struct address_space *,
194 CORE_ADDR pc);
1aafd4da 195
fe3f5fa8 196static void free_bp_location (struct bp_location *loc);
f431efe5
PA
197static void incref_bp_location (struct bp_location *loc);
198static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 199
39d61571 200static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 201
b60e7edf 202static void update_global_location_list (int);
a5606eee 203
b60e7edf 204static void update_global_location_list_nothrow (int);
74960c60 205
d77f58be 206static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60 207
d77f58be 208static int is_watchpoint (const struct breakpoint *bpt);
60e1c644 209
74960c60 210static void insert_breakpoint_locations (void);
a5606eee 211
a96d9b2e
SDJ
212static int syscall_catchpoint_p (struct breakpoint *b);
213
1042e4c0
SS
214static void tracepoints_info (char *, int);
215
216static void delete_trace_command (char *, int);
217
218static void enable_trace_command (char *, int);
219
220static void disable_trace_command (char *, int);
221
222static void trace_pass_command (char *, int);
223
9c06b0b4
TJB
224static int is_masked_watchpoint (const struct breakpoint *b);
225
0fb4aa4b
PA
226/* Assuming we're creating a static tracepoint, does S look like a
227 static tracepoint marker spec ("-m MARKER_ID")? */
228#define is_marker_spec(s) \
f5a8e22b 229 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 230
5cea2a26
PA
231/* A reference-counted struct command_line. This lets multiple
232 breakpoints share a single command list. */
233struct counted_command_line
234{
235 /* The reference count. */
236 int refc;
237
238 /* The command list. */
239 struct command_line *commands;
240};
241
242struct command_line *
243breakpoint_commands (struct breakpoint *b)
244{
245 return b->commands ? b->commands->commands : NULL;
246}
3daf8fe5 247
f3b1572e
PA
248/* Flag indicating that a command has proceeded the inferior past the
249 current breakpoint. */
250
251static int breakpoint_proceeded;
252
956a9fb9 253const char *
2cec12e5
AR
254bpdisp_text (enum bpdisp disp)
255{
4a64f543
MS
256 /* NOTE: the following values are a part of MI protocol and
257 represent values of 'disp' field returned when inferior stops at
258 a breakpoint. */
bc043ef3 259 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 260
2cec12e5
AR
261 return bpdisps[(int) disp];
262}
c906108c 263
4a64f543 264/* Prototypes for exported functions. */
c906108c 265/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 266 if such is available. */
c906108c
SS
267static int can_use_hw_watchpoints;
268
920d2a44
AC
269static void
270show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
271 struct cmd_list_element *c,
272 const char *value)
273{
3e43a32a
MS
274 fprintf_filtered (file,
275 _("Debugger's willingness to use "
276 "watchpoint hardware is %s.\n"),
920d2a44
AC
277 value);
278}
279
fa8d40ab
JJ
280/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
281 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 282 for unrecognized breakpoint locations.
fa8d40ab
JJ
283 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
284static enum auto_boolean pending_break_support;
920d2a44
AC
285static void
286show_pending_break_support (struct ui_file *file, int from_tty,
287 struct cmd_list_element *c,
288 const char *value)
289{
3e43a32a
MS
290 fprintf_filtered (file,
291 _("Debugger's behavior regarding "
292 "pending breakpoints is %s.\n"),
920d2a44
AC
293 value);
294}
fa8d40ab 295
765dc015 296/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 297 set with "break" but falling in read-only memory.
765dc015
VP
298 If 0, gdb will warn about such breakpoints, but won't automatically
299 use hardware breakpoints. */
300static int automatic_hardware_breakpoints;
301static void
302show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
303 struct cmd_list_element *c,
304 const char *value)
305{
3e43a32a
MS
306 fprintf_filtered (file,
307 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
308 value);
309}
310
33e5cbd6
PA
311/* If on, gdb will keep breakpoints inserted even as inferior is
312 stopped, and immediately insert any new breakpoints. If off, gdb
313 will insert breakpoints into inferior only when resuming it, and
314 will remove breakpoints upon stop. If auto, GDB will behave as ON
315 if in non-stop mode, and as OFF if all-stop mode.*/
316
317static const char always_inserted_auto[] = "auto";
318static const char always_inserted_on[] = "on";
319static const char always_inserted_off[] = "off";
320static const char *always_inserted_enums[] = {
321 always_inserted_auto,
322 always_inserted_off,
323 always_inserted_on,
324 NULL
325};
326static const char *always_inserted_mode = always_inserted_auto;
327static void
74960c60 328show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 329 struct cmd_list_element *c, const char *value)
74960c60 330{
33e5cbd6 331 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
332 fprintf_filtered (file,
333 _("Always inserted breakpoint "
334 "mode is %s (currently %s).\n"),
33e5cbd6
PA
335 value,
336 breakpoints_always_inserted_mode () ? "on" : "off");
337 else
3e43a32a
MS
338 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
339 value);
74960c60
VP
340}
341
33e5cbd6
PA
342int
343breakpoints_always_inserted_mode (void)
344{
345 return (always_inserted_mode == always_inserted_on
346 || (always_inserted_mode == always_inserted_auto && non_stop));
347}
765dc015 348
a14ed312 349void _initialize_breakpoint (void);
c906108c 350
c906108c
SS
351/* Are we executing breakpoint commands? */
352static int executing_breakpoint_commands;
353
c02f5703
MS
354/* Are overlay event breakpoints enabled? */
355static int overlay_events_enabled;
356
e09342b5
TJB
357/* See description in breakpoint.h. */
358int target_exact_watchpoints = 0;
359
c906108c 360/* Walk the following statement or block through all breakpoints.
4a64f543
MS
361 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the
362 current breakpoint. */
c906108c 363
5c44784c 364#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 365
5c44784c
JM
366#define ALL_BREAKPOINTS_SAFE(B,TMP) \
367 for (B = breakpoint_chain; \
368 B ? (TMP=B->next, 1): 0; \
369 B = TMP)
c906108c 370
4a64f543
MS
371/* Similar iterator for the low-level breakpoints. SAFE variant is
372 not provided so update_global_location_list must not be called
373 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 374
876fa593
JK
375#define ALL_BP_LOCATIONS(B,BP_TMP) \
376 for (BP_TMP = bp_location; \
377 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
378 BP_TMP++)
7cc221ef 379
1042e4c0
SS
380/* Iterator for tracepoints only. */
381
382#define ALL_TRACEPOINTS(B) \
383 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 384 if (is_tracepoint (B))
1042e4c0 385
7cc221ef 386/* Chains of all breakpoints defined. */
c906108c
SS
387
388struct breakpoint *breakpoint_chain;
389
876fa593
JK
390/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
391
392static struct bp_location **bp_location;
393
394/* Number of elements of BP_LOCATION. */
395
396static unsigned bp_location_count;
397
4a64f543
MS
398/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
399 ADDRESS for the current elements of BP_LOCATION which get a valid
400 result from bp_location_has_shadow. You can use it for roughly
401 limiting the subrange of BP_LOCATION to scan for shadow bytes for
402 an address you need to read. */
876fa593
JK
403
404static CORE_ADDR bp_location_placed_address_before_address_max;
405
4a64f543
MS
406/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
407 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
408 BP_LOCATION which get a valid result from bp_location_has_shadow.
409 You can use it for roughly limiting the subrange of BP_LOCATION to
410 scan for shadow bytes for an address you need to read. */
876fa593
JK
411
412static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 413
4a64f543
MS
414/* The locations that no longer correspond to any breakpoint, unlinked
415 from bp_location array, but for which a hit may still be reported
416 by a target. */
20874c92
VP
417VEC(bp_location_p) *moribund_locations = NULL;
418
c906108c
SS
419/* Number of last breakpoint made. */
420
95a42b64
TT
421static int breakpoint_count;
422
86b17b60
PA
423/* The value of `breakpoint_count' before the last command that
424 created breakpoints. If the last (break-like) command created more
425 than one breakpoint, then the difference between BREAKPOINT_COUNT
426 and PREV_BREAKPOINT_COUNT is more than one. */
427static int prev_breakpoint_count;
c906108c 428
1042e4c0
SS
429/* Number of last tracepoint made. */
430
95a42b64 431static int tracepoint_count;
1042e4c0 432
6149aea9
PA
433static struct cmd_list_element *breakpoint_set_cmdlist;
434static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 435struct cmd_list_element *save_cmdlist;
6149aea9 436
468d015d
JJ
437/* Return whether a breakpoint is an active enabled breakpoint. */
438static int
439breakpoint_enabled (struct breakpoint *b)
440{
0d381245 441 return (b->enable_state == bp_enabled);
468d015d
JJ
442}
443
c906108c
SS
444/* Set breakpoint count to NUM. */
445
95a42b64 446static void
fba45db2 447set_breakpoint_count (int num)
c906108c 448{
86b17b60 449 prev_breakpoint_count = breakpoint_count;
c906108c 450 breakpoint_count = num;
4fa62494 451 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
452}
453
86b17b60
PA
454/* Used by `start_rbreak_breakpoints' below, to record the current
455 breakpoint count before "rbreak" creates any breakpoint. */
456static int rbreak_start_breakpoint_count;
457
95a42b64
TT
458/* Called at the start an "rbreak" command to record the first
459 breakpoint made. */
86b17b60 460
95a42b64
TT
461void
462start_rbreak_breakpoints (void)
463{
86b17b60 464 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
465}
466
467/* Called at the end of an "rbreak" command to record the last
468 breakpoint made. */
86b17b60 469
95a42b64
TT
470void
471end_rbreak_breakpoints (void)
472{
86b17b60 473 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
474}
475
4a64f543 476/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
477
478void
fba45db2 479clear_breakpoint_hit_counts (void)
c906108c
SS
480{
481 struct breakpoint *b;
482
483 ALL_BREAKPOINTS (b)
484 b->hit_count = 0;
485}
486
9add0f1b
TT
487/* Allocate a new counted_command_line with reference count of 1.
488 The new structure owns COMMANDS. */
489
490static struct counted_command_line *
491alloc_counted_command_line (struct command_line *commands)
492{
493 struct counted_command_line *result
494 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 495
9add0f1b
TT
496 result->refc = 1;
497 result->commands = commands;
498 return result;
499}
500
501/* Increment reference count. This does nothing if CMD is NULL. */
502
503static void
504incref_counted_command_line (struct counted_command_line *cmd)
505{
506 if (cmd)
507 ++cmd->refc;
508}
509
510/* Decrement reference count. If the reference count reaches 0,
511 destroy the counted_command_line. Sets *CMDP to NULL. This does
512 nothing if *CMDP is NULL. */
513
514static void
515decref_counted_command_line (struct counted_command_line **cmdp)
516{
517 if (*cmdp)
518 {
519 if (--(*cmdp)->refc == 0)
520 {
521 free_command_lines (&(*cmdp)->commands);
522 xfree (*cmdp);
523 }
524 *cmdp = NULL;
525 }
526}
527
528/* A cleanup function that calls decref_counted_command_line. */
529
530static void
531do_cleanup_counted_command_line (void *arg)
532{
533 decref_counted_command_line (arg);
534}
535
536/* Create a cleanup that calls decref_counted_command_line on the
537 argument. */
538
539static struct cleanup *
540make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
541{
542 return make_cleanup (do_cleanup_counted_command_line, cmdp);
543}
544
c906108c
SS
545/* Default address, symtab and line to put a breakpoint at
546 for "break" command with no arg.
4a64f543 547 If default_breakpoint_valid is zero, the other three are
c906108c
SS
548 not valid, and "break" with no arg is an error.
549
550 This set by print_stack_frame, which calls set_default_breakpoint. */
551
552int default_breakpoint_valid;
553CORE_ADDR default_breakpoint_address;
554struct symtab *default_breakpoint_symtab;
555int default_breakpoint_line;
6c95b8df
PA
556struct program_space *default_breakpoint_pspace;
557
c906108c 558\f
48cb2d85
VP
559/* Return the breakpoint with the specified number, or NULL
560 if the number does not refer to an existing breakpoint. */
561
562struct breakpoint *
563get_breakpoint (int num)
564{
565 struct breakpoint *b;
566
567 ALL_BREAKPOINTS (b)
568 if (b->number == num)
569 return b;
570
571 return NULL;
572}
5c44784c 573
c906108c 574\f
adc36818
PM
575
576void
577set_breakpoint_condition (struct breakpoint *b, char *exp,
578 int from_tty)
579{
580 struct bp_location *loc = b->loc;
581
582 for (; loc; loc = loc->next)
583 {
584 xfree (loc->cond);
585 loc->cond = NULL;
586 }
587 xfree (b->cond_string);
588 b->cond_string = NULL;
589 xfree (b->cond_exp);
590 b->cond_exp = NULL;
591
592 if (*exp == 0)
593 {
594 if (from_tty)
595 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
596 }
597 else
598 {
599 char *arg = exp;
cc59ec59 600
adc36818
PM
601 /* I don't know if it matters whether this is the string the user
602 typed in or the decompiled expression. */
603 b->cond_string = xstrdup (arg);
604 b->condition_not_parsed = 0;
605
606 if (is_watchpoint (b))
607 {
608 innermost_block = NULL;
609 arg = exp;
610 b->cond_exp = parse_exp_1 (&arg, 0, 0);
611 if (*arg)
612 error (_("Junk at end of expression"));
613 b->cond_exp_valid_block = innermost_block;
614 }
615 else
616 {
617 for (loc = b->loc; loc; loc = loc->next)
618 {
619 arg = exp;
620 loc->cond =
621 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
622 if (*arg)
623 error (_("Junk at end of expression"));
624 }
625 }
626 }
627 breakpoints_changed ();
8d3788bd 628 observer_notify_breakpoint_modified (b);
adc36818
PM
629}
630
c906108c
SS
631/* condition N EXP -- set break condition of breakpoint N to EXP. */
632
633static void
fba45db2 634condition_command (char *arg, int from_tty)
c906108c 635{
52f0bd74 636 struct breakpoint *b;
c906108c 637 char *p;
52f0bd74 638 int bnum;
c906108c
SS
639
640 if (arg == 0)
e2e0b3e5 641 error_no_arg (_("breakpoint number"));
c906108c
SS
642
643 p = arg;
644 bnum = get_number (&p);
5c44784c 645 if (bnum == 0)
8a3fe4f8 646 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
647
648 ALL_BREAKPOINTS (b)
649 if (b->number == bnum)
2f069f6f 650 {
7371cf6d
PM
651 /* Check if this breakpoint has a Python object assigned to
652 it, and if it has a definition of the "stop"
653 method. This method and conditions entered into GDB from
654 the CLI are mutually exclusive. */
655 if (b->py_bp_object
656 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
657 error (_("Cannot set a condition where a Python 'stop' "
658 "method has been defined in the breakpoint."));
2566ad2d 659 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
660 return;
661 }
c906108c 662
8a3fe4f8 663 error (_("No breakpoint number %d."), bnum);
c906108c
SS
664}
665
a7bdde9e
VP
666/* Check that COMMAND do not contain commands that are suitable
667 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
668 Throw if any such commands is found. */
669
a7bdde9e
VP
670static void
671check_no_tracepoint_commands (struct command_line *commands)
672{
673 struct command_line *c;
cc59ec59 674
a7bdde9e
VP
675 for (c = commands; c; c = c->next)
676 {
677 int i;
678
679 if (c->control_type == while_stepping_control)
3e43a32a
MS
680 error (_("The 'while-stepping' command can "
681 "only be used for tracepoints"));
a7bdde9e
VP
682
683 for (i = 0; i < c->body_count; ++i)
684 check_no_tracepoint_commands ((c->body_list)[i]);
685
686 /* Not that command parsing removes leading whitespace and comment
4a64f543 687 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
688 command directly. */
689 if (strstr (c->line, "collect ") == c->line)
690 error (_("The 'collect' command can only be used for tracepoints"));
691
51661e93
VP
692 if (strstr (c->line, "teval ") == c->line)
693 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
694 }
695}
696
d77f58be
SS
697/* Encapsulate tests for different types of tracepoints. */
698
a7bdde9e 699int
d77f58be 700is_tracepoint (const struct breakpoint *b)
a7bdde9e 701{
0fb4aa4b
PA
702 return (b->type == bp_tracepoint
703 || b->type == bp_fast_tracepoint
704 || b->type == bp_static_tracepoint);
a7bdde9e 705}
d77f58be 706
95a42b64
TT
707/* A helper function that validsates that COMMANDS are valid for a
708 breakpoint. This function will throw an exception if a problem is
709 found. */
48cb2d85 710
95a42b64
TT
711static void
712validate_commands_for_breakpoint (struct breakpoint *b,
713 struct command_line *commands)
48cb2d85 714{
d77f58be 715 if (is_tracepoint (b))
a7bdde9e 716 {
4a64f543
MS
717 /* We need to verify that each top-level element of commands is
718 valid for tracepoints, that there's at most one
719 while-stepping element, and that while-stepping's body has
720 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
721 struct command_line *c;
722 struct command_line *while_stepping = 0;
723 for (c = commands; c; c = c->next)
724 {
a7bdde9e
VP
725 if (c->control_type == while_stepping_control)
726 {
727 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
728 error (_("The 'while-stepping' command "
729 "cannot be used for fast tracepoint"));
0fb4aa4b 730 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
731 error (_("The 'while-stepping' command "
732 "cannot be used for static tracepoint"));
a7bdde9e
VP
733
734 if (while_stepping)
3e43a32a
MS
735 error (_("The 'while-stepping' command "
736 "can be used only once"));
a7bdde9e
VP
737 else
738 while_stepping = c;
739 }
740 }
741 if (while_stepping)
742 {
743 struct command_line *c2;
744
745 gdb_assert (while_stepping->body_count == 1);
746 c2 = while_stepping->body_list[0];
747 for (; c2; c2 = c2->next)
748 {
a7bdde9e
VP
749 if (c2->control_type == while_stepping_control)
750 error (_("The 'while-stepping' command cannot be nested"));
751 }
752 }
753 }
754 else
755 {
756 check_no_tracepoint_commands (commands);
757 }
95a42b64
TT
758}
759
0fb4aa4b
PA
760/* Return a vector of all the static tracepoints set at ADDR. The
761 caller is responsible for releasing the vector. */
762
763VEC(breakpoint_p) *
764static_tracepoints_here (CORE_ADDR addr)
765{
766 struct breakpoint *b;
767 VEC(breakpoint_p) *found = 0;
768 struct bp_location *loc;
769
770 ALL_BREAKPOINTS (b)
771 if (b->type == bp_static_tracepoint)
772 {
773 for (loc = b->loc; loc; loc = loc->next)
774 if (loc->address == addr)
775 VEC_safe_push(breakpoint_p, found, b);
776 }
777
778 return found;
779}
780
95a42b64 781/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 782 validate that only allowed commands are included. */
95a42b64
TT
783
784void
4a64f543
MS
785breakpoint_set_commands (struct breakpoint *b,
786 struct command_line *commands)
95a42b64
TT
787{
788 validate_commands_for_breakpoint (b, commands);
a7bdde9e 789
9add0f1b
TT
790 decref_counted_command_line (&b->commands);
791 b->commands = alloc_counted_command_line (commands);
48cb2d85 792 breakpoints_changed ();
8d3788bd 793 observer_notify_breakpoint_modified (b);
48cb2d85
VP
794}
795
45a43567
TT
796/* Set the internal `silent' flag on the breakpoint. Note that this
797 is not the same as the "silent" that may appear in the breakpoint's
798 commands. */
799
800void
801breakpoint_set_silent (struct breakpoint *b, int silent)
802{
803 int old_silent = b->silent;
804
805 b->silent = silent;
806 if (old_silent != silent)
8d3788bd 807 observer_notify_breakpoint_modified (b);
45a43567
TT
808}
809
810/* Set the thread for this breakpoint. If THREAD is -1, make the
811 breakpoint work for any thread. */
812
813void
814breakpoint_set_thread (struct breakpoint *b, int thread)
815{
816 int old_thread = b->thread;
817
818 b->thread = thread;
819 if (old_thread != thread)
8d3788bd 820 observer_notify_breakpoint_modified (b);
45a43567
TT
821}
822
823/* Set the task for this breakpoint. If TASK is 0, make the
824 breakpoint work for any task. */
825
826void
827breakpoint_set_task (struct breakpoint *b, int task)
828{
829 int old_task = b->task;
830
831 b->task = task;
832 if (old_task != task)
8d3788bd 833 observer_notify_breakpoint_modified (b);
45a43567
TT
834}
835
95a42b64
TT
836void
837check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
838{
839 struct breakpoint *b = closure;
cc59ec59 840
a7bdde9e
VP
841 validate_actionline (&line, b);
842}
843
95a42b64
TT
844/* A structure used to pass information through
845 map_breakpoint_numbers. */
846
847struct commands_info
848{
849 /* True if the command was typed at a tty. */
850 int from_tty;
86b17b60
PA
851
852 /* The breakpoint range spec. */
853 char *arg;
854
95a42b64
TT
855 /* Non-NULL if the body of the commands are being read from this
856 already-parsed command. */
857 struct command_line *control;
86b17b60 858
95a42b64
TT
859 /* The command lines read from the user, or NULL if they have not
860 yet been read. */
861 struct counted_command_line *cmd;
862};
863
864/* A callback for map_breakpoint_numbers that sets the commands for
865 commands_command. */
866
c906108c 867static void
95a42b64 868do_map_commands_command (struct breakpoint *b, void *data)
c906108c 869{
95a42b64 870 struct commands_info *info = data;
c906108c 871
95a42b64
TT
872 if (info->cmd == NULL)
873 {
874 struct command_line *l;
5c44784c 875
95a42b64
TT
876 if (info->control != NULL)
877 l = copy_command_lines (info->control->body_list[0]);
878 else
86b17b60
PA
879 {
880 struct cleanup *old_chain;
881 char *str;
c5aa993b 882
3e43a32a
MS
883 str = xstrprintf (_("Type commands for breakpoint(s) "
884 "%s, one per line."),
86b17b60
PA
885 info->arg);
886
887 old_chain = make_cleanup (xfree, str);
888
889 l = read_command_lines (str,
890 info->from_tty, 1,
d77f58be 891 (is_tracepoint (b)
86b17b60
PA
892 ? check_tracepoint_command : 0),
893 b);
894
895 do_cleanups (old_chain);
896 }
a7bdde9e 897
95a42b64
TT
898 info->cmd = alloc_counted_command_line (l);
899 }
900
901 /* If a breakpoint was on the list more than once, we don't need to
902 do anything. */
903 if (b->commands != info->cmd)
904 {
905 validate_commands_for_breakpoint (b, info->cmd->commands);
906 incref_counted_command_line (info->cmd);
907 decref_counted_command_line (&b->commands);
908 b->commands = info->cmd;
909 breakpoints_changed ();
8d3788bd 910 observer_notify_breakpoint_modified (b);
c5aa993b 911 }
95a42b64
TT
912}
913
914static void
4a64f543
MS
915commands_command_1 (char *arg, int from_tty,
916 struct command_line *control)
95a42b64
TT
917{
918 struct cleanup *cleanups;
919 struct commands_info info;
920
921 info.from_tty = from_tty;
922 info.control = control;
923 info.cmd = NULL;
924 /* If we read command lines from the user, then `info' will hold an
925 extra reference to the commands that we must clean up. */
926 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
927
928 if (arg == NULL || !*arg)
929 {
86b17b60 930 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
931 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
932 breakpoint_count);
95a42b64
TT
933 else if (breakpoint_count > 0)
934 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
935 else
936 {
937 /* So that we don't try to free the incoming non-NULL
938 argument in the cleanup below. Mapping breakpoint
939 numbers will fail in this case. */
940 arg = NULL;
941 }
95a42b64 942 }
9766ced4
SS
943 else
944 /* The command loop has some static state, so we need to preserve
945 our argument. */
946 arg = xstrdup (arg);
86b17b60
PA
947
948 if (arg != NULL)
949 make_cleanup (xfree, arg);
950
951 info.arg = arg;
95a42b64
TT
952
953 map_breakpoint_numbers (arg, do_map_commands_command, &info);
954
955 if (info.cmd == NULL)
956 error (_("No breakpoints specified."));
957
958 do_cleanups (cleanups);
959}
960
961static void
962commands_command (char *arg, int from_tty)
963{
964 commands_command_1 (arg, from_tty, NULL);
c906108c 965}
40c03ae8
EZ
966
967/* Like commands_command, but instead of reading the commands from
968 input stream, takes them from an already parsed command structure.
969
970 This is used by cli-script.c to DTRT with breakpoint commands
971 that are part of if and while bodies. */
972enum command_control_type
973commands_from_control_command (char *arg, struct command_line *cmd)
974{
95a42b64
TT
975 commands_command_1 (arg, 0, cmd);
976 return simple_control;
40c03ae8 977}
876fa593
JK
978
979/* Return non-zero if BL->TARGET_INFO contains valid information. */
980
981static int
982bp_location_has_shadow (struct bp_location *bl)
983{
984 if (bl->loc_type != bp_loc_software_breakpoint)
985 return 0;
986 if (!bl->inserted)
987 return 0;
988 if (bl->target_info.shadow_len == 0)
989 /* bp isn't valid, or doesn't shadow memory. */
990 return 0;
991 return 1;
992}
993
8defab1a 994/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
995 by replacing any memory breakpoints with their shadowed contents.
996
997 The range of shadowed area by each bp_location is:
35df4500
TJB
998 bl->address - bp_location_placed_address_before_address_max
999 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1000 The range we were requested to resolve shadows for is:
1001 memaddr ... memaddr + len
1002 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1003 memaddr + len <= (bl->address
1004 - bp_location_placed_address_before_address_max)
876fa593 1005 and:
35df4500 1006 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1007
8defab1a
DJ
1008void
1009breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1010{
4a64f543
MS
1011 /* Left boundary, right boundary and median element of our binary
1012 search. */
876fa593
JK
1013 unsigned bc_l, bc_r, bc;
1014
4a64f543
MS
1015 /* Find BC_L which is a leftmost element which may affect BUF
1016 content. It is safe to report lower value but a failure to
1017 report higher one. */
876fa593
JK
1018
1019 bc_l = 0;
1020 bc_r = bp_location_count;
1021 while (bc_l + 1 < bc_r)
1022 {
35df4500 1023 struct bp_location *bl;
876fa593
JK
1024
1025 bc = (bc_l + bc_r) / 2;
35df4500 1026 bl = bp_location[bc];
876fa593 1027
4a64f543
MS
1028 /* Check first BL->ADDRESS will not overflow due to the added
1029 constant. Then advance the left boundary only if we are sure
1030 the BC element can in no way affect the BUF content (MEMADDR
1031 to MEMADDR + LEN range).
876fa593 1032
4a64f543
MS
1033 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1034 offset so that we cannot miss a breakpoint with its shadow
1035 range tail still reaching MEMADDR. */
c5aa993b 1036
35df4500
TJB
1037 if ((bl->address + bp_location_shadow_len_after_address_max
1038 >= bl->address)
1039 && (bl->address + bp_location_shadow_len_after_address_max
1040 <= memaddr))
876fa593
JK
1041 bc_l = bc;
1042 else
1043 bc_r = bc;
1044 }
1045
128070bb
PA
1046 /* Due to the binary search above, we need to make sure we pick the
1047 first location that's at BC_L's address. E.g., if there are
1048 multiple locations at the same address, BC_L may end up pointing
1049 at a duplicate location, and miss the "master"/"inserted"
1050 location. Say, given locations L1, L2 and L3 at addresses A and
1051 B:
1052
1053 L1@A, L2@A, L3@B, ...
1054
1055 BC_L could end up pointing at location L2, while the "master"
1056 location could be L1. Since the `loc->inserted' flag is only set
1057 on "master" locations, we'd forget to restore the shadow of L1
1058 and L2. */
1059 while (bc_l > 0
1060 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1061 bc_l--;
1062
876fa593
JK
1063 /* Now do full processing of the found relevant range of elements. */
1064
1065 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1066 {
35df4500 1067 struct bp_location *bl = bp_location[bc];
876fa593
JK
1068 CORE_ADDR bp_addr = 0;
1069 int bp_size = 0;
1070 int bptoffset = 0;
1071
35df4500
TJB
1072 /* bp_location array has BL->OWNER always non-NULL. */
1073 if (bl->owner->type == bp_none)
8a3fe4f8 1074 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1075 bl->owner->number);
ffce0d52 1076
876fa593
JK
1077 /* Performance optimization: any futher element can no longer affect BUF
1078 content. */
1079
35df4500
TJB
1080 if (bl->address >= bp_location_placed_address_before_address_max
1081 && memaddr + len <= (bl->address
1082 - bp_location_placed_address_before_address_max))
876fa593
JK
1083 break;
1084
35df4500 1085 if (!bp_location_has_shadow (bl))
c5aa993b 1086 continue;
35df4500 1087 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1088 current_program_space->aspace, 0))
1089 continue;
1090
c5aa993b
JM
1091 /* Addresses and length of the part of the breakpoint that
1092 we need to copy. */
35df4500
TJB
1093 bp_addr = bl->target_info.placed_address;
1094 bp_size = bl->target_info.shadow_len;
8defab1a 1095
c5aa993b
JM
1096 if (bp_addr + bp_size <= memaddr)
1097 /* The breakpoint is entirely before the chunk of memory we
1098 are reading. */
1099 continue;
8defab1a 1100
c5aa993b
JM
1101 if (bp_addr >= memaddr + len)
1102 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1103 reading. */
c5aa993b 1104 continue;
c5aa993b 1105
8defab1a
DJ
1106 /* Offset within shadow_contents. */
1107 if (bp_addr < memaddr)
1108 {
1109 /* Only copy the second part of the breakpoint. */
1110 bp_size -= memaddr - bp_addr;
1111 bptoffset = memaddr - bp_addr;
1112 bp_addr = memaddr;
1113 }
c5aa993b 1114
8defab1a
DJ
1115 if (bp_addr + bp_size > memaddr + len)
1116 {
1117 /* Only copy the first part of the breakpoint. */
1118 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1119 }
c5aa993b 1120
8defab1a 1121 memcpy (buf + bp_addr - memaddr,
35df4500 1122 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1123 }
c906108c 1124}
c906108c 1125\f
c5aa993b 1126
60e1c644
PA
1127/* Return true if BPT is of any hardware watchpoint kind. */
1128
a5606eee 1129static int
d77f58be 1130is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1131{
1132 return (bpt->type == bp_hardware_watchpoint
1133 || bpt->type == bp_read_watchpoint
1134 || bpt->type == bp_access_watchpoint);
1135}
7270d8f2 1136
60e1c644
PA
1137/* Return true if BPT is of any watchpoint kind, hardware or
1138 software. */
1139
1140static int
d77f58be 1141is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1142{
1143 return (is_hardware_watchpoint (bpt)
1144 || bpt->type == bp_watchpoint);
1145}
1146
f6bc2008
PA
1147/* Assuming that B is a watchpoint: returns true if the current thread
1148 and its running state are safe to evaluate or update watchpoint B.
1149 Watchpoints on local expressions need to be evaluated in the
1150 context of the thread that was current when the watchpoint was
1151 created, and, that thread needs to be stopped to be able to select
1152 the correct frame context. Watchpoints on global expressions can
1153 be evaluated on any thread, and in any state. It is presently left
1154 to the target allowing memory accesses when threads are
1155 running. */
1156
1157static int
1158watchpoint_in_thread_scope (struct breakpoint *b)
1159{
1160 return (ptid_equal (b->watchpoint_thread, null_ptid)
1161 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1162 && !is_executing (inferior_ptid)));
1163}
1164
d0fb5eae
JK
1165/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1166 associated bp_watchpoint_scope breakpoint. */
1167
1168static void
1169watchpoint_del_at_next_stop (struct breakpoint *b)
1170{
1171 gdb_assert (is_watchpoint (b));
1172
1173 if (b->related_breakpoint != b)
1174 {
1175 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1176 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1177 b->related_breakpoint->disposition = disp_del_at_next_stop;
1178 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1179 b->related_breakpoint = b;
1180 }
1181 b->disposition = disp_del_at_next_stop;
1182}
1183
567e1b4e
JB
1184/* Assuming that B is a watchpoint:
1185 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1186 - Evaluate expression and store the result in B->val
567e1b4e
JB
1187 - Evaluate the condition if there is one, and store the result
1188 in b->loc->cond.
a5606eee
VP
1189 - Update the list of values that must be watched in B->loc.
1190
4a64f543
MS
1191 If the watchpoint disposition is disp_del_at_next_stop, then do
1192 nothing. If this is local watchpoint that is out of scope, delete
1193 it.
1194
1195 Even with `set breakpoint always-inserted on' the watchpoints are
1196 removed + inserted on each stop here. Normal breakpoints must
1197 never be removed because they might be missed by a running thread
1198 when debugging in non-stop mode. On the other hand, hardware
1199 watchpoints (is_hardware_watchpoint; processed here) are specific
1200 to each LWP since they are stored in each LWP's hardware debug
1201 registers. Therefore, such LWP must be stopped first in order to
1202 be able to modify its hardware watchpoints.
1203
1204 Hardware watchpoints must be reset exactly once after being
1205 presented to the user. It cannot be done sooner, because it would
1206 reset the data used to present the watchpoint hit to the user. And
1207 it must not be done later because it could display the same single
1208 watchpoint hit during multiple GDB stops. Note that the latter is
1209 relevant only to the hardware watchpoint types bp_read_watchpoint
1210 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1211 not user-visible - its hit is suppressed if the memory content has
1212 not changed.
1213
1214 The following constraints influence the location where we can reset
1215 hardware watchpoints:
1216
1217 * target_stopped_by_watchpoint and target_stopped_data_address are
1218 called several times when GDB stops.
1219
1220 [linux]
1221 * Multiple hardware watchpoints can be hit at the same time,
1222 causing GDB to stop. GDB only presents one hardware watchpoint
1223 hit at a time as the reason for stopping, and all the other hits
1224 are presented later, one after the other, each time the user
1225 requests the execution to be resumed. Execution is not resumed
1226 for the threads still having pending hit event stored in
1227 LWP_INFO->STATUS. While the watchpoint is already removed from
1228 the inferior on the first stop the thread hit event is kept being
1229 reported from its cached value by linux_nat_stopped_data_address
1230 until the real thread resume happens after the watchpoint gets
1231 presented and thus its LWP_INFO->STATUS gets reset.
1232
1233 Therefore the hardware watchpoint hit can get safely reset on the
1234 watchpoint removal from inferior. */
a79d3c27 1235
b40ce68a 1236static void
a5606eee 1237update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 1238{
a5606eee 1239 int within_current_scope;
a5606eee 1240 struct frame_id saved_frame_id;
66076460 1241 int frame_saved;
a5606eee 1242
d0fb5eae
JK
1243 gdb_assert (is_watchpoint (b));
1244
f6bc2008
PA
1245 /* If this is a local watchpoint, we only want to check if the
1246 watchpoint frame is in scope if the current thread is the thread
1247 that was used to create the watchpoint. */
1248 if (!watchpoint_in_thread_scope (b))
1249 return;
1250
a5606eee
VP
1251 if (b->disposition == disp_del_at_next_stop)
1252 return;
1253
66076460 1254 frame_saved = 0;
a5606eee
VP
1255
1256 /* Determine if the watchpoint is within scope. */
1257 if (b->exp_valid_block == NULL)
1258 within_current_scope = 1;
1259 else
1260 {
b5db5dfc
UW
1261 struct frame_info *fi = get_current_frame ();
1262 struct gdbarch *frame_arch = get_frame_arch (fi);
1263 CORE_ADDR frame_pc = get_frame_pc (fi);
1264
1265 /* If we're in a function epilogue, unwinding may not work
1266 properly, so do not attempt to recreate locations at this
1267 point. See similar comments in watchpoint_check. */
1268 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1269 return;
66076460
DJ
1270
1271 /* Save the current frame's ID so we can restore it after
1272 evaluating the watchpoint expression on its own frame. */
1273 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1274 took a frame parameter, so that we didn't have to change the
1275 selected frame. */
1276 frame_saved = 1;
1277 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1278
a5606eee
VP
1279 fi = frame_find_by_id (b->watchpoint_frame);
1280 within_current_scope = (fi != NULL);
1281 if (within_current_scope)
1282 select_frame (fi);
1283 }
1284
b5db5dfc
UW
1285 /* We don't free locations. They are stored in the bp_location array
1286 and update_global_location_list will eventually delete them and
1287 remove breakpoints if needed. */
1288 b->loc = NULL;
1289
a5606eee
VP
1290 if (within_current_scope && reparse)
1291 {
1292 char *s;
d63d0675 1293
a5606eee
VP
1294 if (b->exp)
1295 {
1296 xfree (b->exp);
1297 b->exp = NULL;
1298 }
d63d0675 1299 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1300 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1301 /* If the meaning of expression itself changed, the old value is
1302 no longer relevant. We don't want to report a watchpoint hit
1303 to the user when the old value and the new value may actually
1304 be completely different objects. */
1305 value_free (b->val);
fa4727a6
DJ
1306 b->val = NULL;
1307 b->val_valid = 0;
60e1c644
PA
1308
1309 /* Note that unlike with breakpoints, the watchpoint's condition
1310 expression is stored in the breakpoint object, not in the
1311 locations (re)created below. */
1312 if (b->cond_string != NULL)
1313 {
1314 if (b->cond_exp != NULL)
1315 {
1316 xfree (b->cond_exp);
1317 b->cond_exp = NULL;
1318 }
1319
1320 s = b->cond_string;
1321 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1322 }
a5606eee 1323 }
a5606eee
VP
1324
1325 /* If we failed to parse the expression, for example because
1326 it refers to a global variable in a not-yet-loaded shared library,
1327 don't try to insert watchpoint. We don't automatically delete
1328 such watchpoint, though, since failure to parse expression
1329 is different from out-of-scope watchpoint. */
2d134ed3
PA
1330 if ( !target_has_execution)
1331 {
1332 /* Without execution, memory can't change. No use to try and
1333 set watchpoint locations. The watchpoint will be reset when
1334 the target gains execution, through breakpoint_re_set. */
1335 }
1336 else if (within_current_scope && b->exp)
a5606eee 1337 {
0cf6dd15 1338 int pc = 0;
fa4727a6 1339 struct value *val_chain, *v, *result, *next;
2d134ed3 1340 struct program_space *frame_pspace;
a5606eee 1341
0cf6dd15 1342 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1343
a5606eee
VP
1344 /* Avoid setting b->val if it's already set. The meaning of
1345 b->val is 'the last value' user saw, and we should update
1346 it only if we reported that last value to user. As it
9c06b0b4
TJB
1347 happens, the code that reports it updates b->val directly.
1348 We don't keep track of the memory value for masked
1349 watchpoints. */
1350 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6
DJ
1351 {
1352 b->val = v;
1353 b->val_valid = 1;
1354 }
a5606eee 1355
2d134ed3
PA
1356 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1357
a5606eee 1358 /* Look at each value on the value chain. */
9fa40276 1359 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1360 {
1361 /* If it's a memory location, and GDB actually needed
1362 its contents to evaluate the expression, then we
fa4727a6
DJ
1363 must watch it. If the first value returned is
1364 still lazy, that means an error occurred reading it;
1365 watch it anyway in case it becomes readable. */
a5606eee 1366 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1367 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1368 {
1369 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1370
a5606eee
VP
1371 /* We only watch structs and arrays if user asked
1372 for it explicitly, never if they just happen to
1373 appear in the middle of some value chain. */
fa4727a6 1374 if (v == result
a5606eee
VP
1375 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1376 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1377 {
1378 CORE_ADDR addr;
1379 int len, type;
1380 struct bp_location *loc, **tmp;
1381
42ae5230 1382 addr = value_address (v);
a5606eee
VP
1383 len = TYPE_LENGTH (value_type (v));
1384 type = hw_write;
1385 if (b->type == bp_read_watchpoint)
1386 type = hw_read;
1387 else if (b->type == bp_access_watchpoint)
1388 type = hw_access;
1389
39d61571 1390 loc = allocate_bp_location (b);
a5606eee
VP
1391 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1392 ;
1393 *tmp = loc;
a6d9a66e 1394 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1395
1396 loc->pspace = frame_pspace;
a5606eee
VP
1397 loc->address = addr;
1398 loc->length = len;
1399 loc->watchpoint_type = type;
1400 }
1401 }
9fa40276
TJB
1402 }
1403
1404 /* Change the type of breakpoint between hardware assisted or
1405 an ordinary watchpoint depending on the hardware support
1406 and free hardware slots. REPARSE is set when the inferior
1407 is started. */
a9634178 1408 if (reparse)
9fa40276 1409 {
e09342b5 1410 int reg_cnt;
9fa40276
TJB
1411 enum bp_loc_type loc_type;
1412 struct bp_location *bl;
a5606eee 1413
a9634178 1414 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1415
1416 if (reg_cnt)
9fa40276
TJB
1417 {
1418 int i, target_resources_ok, other_type_used;
1419
a9634178
TJB
1420 /* Use an exact watchpoint when there's only one memory region to be
1421 watched, and only one debug register is needed to watch it. */
1422 b->exact = target_exact_watchpoints && reg_cnt == 1;
1423
9fa40276 1424 /* We need to determine how many resources are already
e09342b5
TJB
1425 used for all other hardware watchpoints plus this one
1426 to see if we still have enough resources to also fit
1427 this watchpoint in as well. To guarantee the
1428 hw_watchpoint_used_count call below counts this
1429 watchpoint, make sure that it is marked as a hardware
1430 watchpoint. */
a9634178
TJB
1431 if (b->type == bp_watchpoint)
1432 b->type = bp_hardware_watchpoint;
9fa40276 1433
a9634178 1434 i = hw_watchpoint_used_count (b->type, &other_type_used);
e09342b5 1435 target_resources_ok = target_can_use_hardware_watchpoint
a9634178 1436 (b->type, i, other_type_used);
e09342b5 1437 if (target_resources_ok <= 0)
a9634178 1438 {
9c06b0b4
TJB
1439 /* If there's no works_in_software_mode method, we
1440 assume that the watchpoint works in software mode. */
1441 int sw_mode = (!b->ops || !b->ops->works_in_software_mode
1442 || b->ops->works_in_software_mode (b));
1443
1444 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1445 error (_("Target does not support this type of "
1446 "hardware watchpoint."));
9c06b0b4
TJB
1447 else if (target_resources_ok < 0 && !sw_mode)
1448 error (_("There are not enough available hardware "
1449 "resources for this watchpoint."));
a9634178
TJB
1450 else
1451 b->type = bp_watchpoint;
1452 }
9fa40276 1453 }
9c06b0b4
TJB
1454 else if (b->ops && b->ops->works_in_software_mode
1455 && !b->ops->works_in_software_mode (b))
a9634178
TJB
1456 error (_("Expression cannot be implemented with "
1457 "read/access watchpoint."));
9fa40276
TJB
1458 else
1459 b->type = bp_watchpoint;
1460
1461 loc_type = (b->type == bp_watchpoint? bp_loc_other
1462 : bp_loc_hardware_watchpoint);
1463 for (bl = b->loc; bl; bl = bl->next)
1464 bl->loc_type = loc_type;
1465 }
1466
1467 for (v = val_chain; v; v = next)
1468 {
a5606eee
VP
1469 next = value_next (v);
1470 if (v != b->val)
1471 value_free (v);
1472 }
1473
c7437ca6
PA
1474 /* If a software watchpoint is not watching any memory, then the
1475 above left it without any location set up. But,
1476 bpstat_stop_status requires a location to be able to report
1477 stops, so make sure there's at least a dummy one. */
1478 if (b->type == bp_watchpoint && b->loc == NULL)
1479 {
1480 b->loc = allocate_bp_location (b);
1481 b->loc->pspace = frame_pspace;
1482 b->loc->address = -1;
1483 b->loc->length = -1;
1484 b->loc->watchpoint_type = -1;
1485 }
a5606eee
VP
1486 }
1487 else if (!within_current_scope)
7270d8f2 1488 {
ac74f770
MS
1489 printf_filtered (_("\
1490Watchpoint %d deleted because the program has left the block\n\
1491in which its expression is valid.\n"),
a5606eee 1492 b->number);
d0fb5eae 1493 watchpoint_del_at_next_stop (b);
7270d8f2 1494 }
a5606eee
VP
1495
1496 /* Restore the selected frame. */
66076460
DJ
1497 if (frame_saved)
1498 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1499}
1500
a5606eee 1501
74960c60
VP
1502/* Returns 1 iff breakpoint location should be
1503 inserted in the inferior. */
1504static int
35df4500 1505should_be_inserted (struct bp_location *bl)
74960c60 1506{
35df4500 1507 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1508 return 0;
1509
35df4500 1510 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1511 return 0;
1512
35df4500 1513 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1514 return 0;
1515
56710373
PA
1516 /* This is set for example, when we're attached to the parent of a
1517 vfork, and have detached from the child. The child is running
1518 free, and we expect it to do an exec or exit, at which point the
1519 OS makes the parent schedulable again (and the target reports
1520 that the vfork is done). Until the child is done with the shared
1521 memory region, do not insert breakpoints in the parent, otherwise
1522 the child could still trip on the parent's breakpoints. Since
1523 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1524 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1525 return 0;
1526
1042e4c0
SS
1527 /* Tracepoints are inserted by the target at a time of its choosing,
1528 not by us. */
35df4500 1529 if (is_tracepoint (bl->owner))
1042e4c0
SS
1530 return 0;
1531
74960c60
VP
1532 return 1;
1533}
1534
35df4500
TJB
1535/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1536 location. Any error messages are printed to TMP_ERROR_STREAM; and
1537 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2 1538
4a64f543
MS
1539 NOTE drow/2003-09-09: This routine could be broken down to an
1540 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1541static int
35df4500 1542insert_bp_location (struct bp_location *bl,
26bb91f3 1543 struct ui_file *tmp_error_stream,
fa3a767f 1544 int *disabled_breaks,
26bb91f3 1545 int *hw_breakpoint_error)
879bfdc2
DJ
1546{
1547 int val = 0;
1548
35df4500 1549 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1550 return 0;
1551
8181d85f 1552 /* Initialize the target-specific information. */
35df4500
TJB
1553 memset (&bl->target_info, 0, sizeof (bl->target_info));
1554 bl->target_info.placed_address = bl->address;
1555 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1556 bl->target_info.length = bl->length;
8181d85f 1557
35df4500
TJB
1558 if (bl->loc_type == bp_loc_software_breakpoint
1559 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1560 {
35df4500 1561 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1562 {
1563 /* If the explicitly specified breakpoint type
1564 is not hardware breakpoint, check the memory map to see
1565 if the breakpoint address is in read only memory or not.
4a64f543 1566
765dc015
VP
1567 Two important cases are:
1568 - location type is not hardware breakpoint, memory
1569 is readonly. We change the type of the location to
1570 hardware breakpoint.
4a64f543
MS
1571 - location type is hardware breakpoint, memory is
1572 read-write. This means we've previously made the
1573 location hardware one, but then the memory map changed,
1574 so we undo.
765dc015 1575
4a64f543
MS
1576 When breakpoints are removed, remove_breakpoints will use
1577 location types we've just set here, the only possible
1578 problem is that memory map has changed during running
1579 program, but it's not going to work anyway with current
1580 gdb. */
765dc015 1581 struct mem_region *mr
35df4500 1582 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1583
1584 if (mr)
1585 {
1586 if (automatic_hardware_breakpoints)
1587 {
765dc015
VP
1588 enum bp_loc_type new_type;
1589
1590 if (mr->attrib.mode != MEM_RW)
1591 new_type = bp_loc_hardware_breakpoint;
1592 else
1593 new_type = bp_loc_software_breakpoint;
1594
35df4500 1595 if (new_type != bl->loc_type)
765dc015
VP
1596 {
1597 static int said = 0;
cc59ec59 1598
35df4500 1599 bl->loc_type = new_type;
765dc015
VP
1600 if (!said)
1601 {
3e43a32a
MS
1602 fprintf_filtered (gdb_stdout,
1603 _("Note: automatically using "
1604 "hardware breakpoints for "
1605 "read-only addresses.\n"));
765dc015
VP
1606 said = 1;
1607 }
1608 }
1609 }
35df4500 1610 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1611 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1612 warning (_("cannot set software breakpoint "
1613 "at readonly address %s"),
35df4500 1614 paddress (bl->gdbarch, bl->address));
765dc015
VP
1615 }
1616 }
1617
879bfdc2
DJ
1618 /* First check to see if we have to handle an overlay. */
1619 if (overlay_debugging == ovly_off
35df4500
TJB
1620 || bl->section == NULL
1621 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1622 {
1623 /* No overlay handling: just set the breakpoint. */
1624
35df4500
TJB
1625 if (bl->loc_type == bp_loc_hardware_breakpoint)
1626 val = target_insert_hw_breakpoint (bl->gdbarch,
1627 &bl->target_info);
879bfdc2 1628 else
35df4500
TJB
1629 val = target_insert_breakpoint (bl->gdbarch,
1630 &bl->target_info);
879bfdc2
DJ
1631 }
1632 else
1633 {
4a64f543 1634 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1635 Shall we set a breakpoint at the LMA? */
1636 if (!overlay_events_enabled)
1637 {
1638 /* Yes -- overlay event support is not active,
1639 so we must try to set a breakpoint at the LMA.
1640 This will not work for a hardware breakpoint. */
35df4500 1641 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1642 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1643 bl->owner->number);
879bfdc2
DJ
1644 else
1645 {
35df4500
TJB
1646 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1647 bl->section);
879bfdc2 1648 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1649 bl->overlay_target_info = bl->target_info;
1650 bl->overlay_target_info.placed_address = addr;
1651 val = target_insert_breakpoint (bl->gdbarch,
1652 &bl->overlay_target_info);
879bfdc2 1653 if (val != 0)
99361f52 1654 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1655 "Overlay breakpoint %d "
1656 "failed: in ROM?\n",
35df4500 1657 bl->owner->number);
879bfdc2
DJ
1658 }
1659 }
1660 /* Shall we set a breakpoint at the VMA? */
35df4500 1661 if (section_is_mapped (bl->section))
879bfdc2
DJ
1662 {
1663 /* Yes. This overlay section is mapped into memory. */
35df4500
TJB
1664 if (bl->loc_type == bp_loc_hardware_breakpoint)
1665 val = target_insert_hw_breakpoint (bl->gdbarch,
1666 &bl->target_info);
879bfdc2 1667 else
35df4500
TJB
1668 val = target_insert_breakpoint (bl->gdbarch,
1669 &bl->target_info);
879bfdc2
DJ
1670 }
1671 else
1672 {
1673 /* No. This breakpoint will not be inserted.
1674 No error, but do not mark the bp as 'inserted'. */
1675 return 0;
1676 }
1677 }
1678
1679 if (val)
1680 {
1681 /* Can't set the breakpoint. */
35df4500 1682 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1683 {
4a64f543 1684 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1685 val = 0;
35df4500 1686 bl->shlib_disabled = 1;
8d3788bd 1687 observer_notify_breakpoint_modified (bl->owner);
879bfdc2
DJ
1688 if (!*disabled_breaks)
1689 {
1690 fprintf_unfiltered (tmp_error_stream,
1691 "Cannot insert breakpoint %d.\n",
35df4500 1692 bl->owner->number);
879bfdc2 1693 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1694 "Temporarily disabling shared "
1695 "library breakpoints:\n");
879bfdc2
DJ
1696 }
1697 *disabled_breaks = 1;
1698 fprintf_unfiltered (tmp_error_stream,
35df4500 1699 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1700 }
1701 else
879bfdc2 1702 {
35df4500 1703 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1704 {
1705 *hw_breakpoint_error = 1;
3e43a32a
MS
1706 fprintf_unfiltered (tmp_error_stream,
1707 "Cannot insert hardware "
1708 "breakpoint %d.\n",
35df4500 1709 bl->owner->number);
879bfdc2
DJ
1710 }
1711 else
1712 {
1713 fprintf_unfiltered (tmp_error_stream,
1714 "Cannot insert breakpoint %d.\n",
35df4500 1715 bl->owner->number);
879bfdc2
DJ
1716 fprintf_filtered (tmp_error_stream,
1717 "Error accessing memory address ");
35df4500 1718 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1719 tmp_error_stream);
879bfdc2
DJ
1720 fprintf_filtered (tmp_error_stream, ": %s.\n",
1721 safe_strerror (val));
1722 }
1723
1724 }
1725 }
1726 else
35df4500 1727 bl->inserted = 1;
879bfdc2
DJ
1728
1729 return val;
1730 }
1731
35df4500 1732 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1733 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1734 watchpoints. It's not clear that it's necessary... */
35df4500 1735 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1736 {
77b06cd7
TJB
1737 gdb_assert (bl->owner->ops != NULL
1738 && bl->owner->ops->insert_location != NULL);
1739
1740 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1741
1742 /* If trying to set a read-watchpoint, and it turns out it's not
1743 supported, try emulating one with an access watchpoint. */
35df4500 1744 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1745 {
1746 struct bp_location *loc, **loc_temp;
1747
1748 /* But don't try to insert it, if there's already another
1749 hw_access location that would be considered a duplicate
1750 of this one. */
1751 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1752 if (loc != bl
85d721b8 1753 && loc->watchpoint_type == hw_access
35df4500 1754 && watchpoint_locations_match (bl, loc))
85d721b8 1755 {
35df4500
TJB
1756 bl->duplicate = 1;
1757 bl->inserted = 1;
1758 bl->target_info = loc->target_info;
1759 bl->watchpoint_type = hw_access;
85d721b8
PA
1760 val = 0;
1761 break;
1762 }
1763
1764 if (val == 1)
1765 {
77b06cd7
TJB
1766 bl->watchpoint_type = hw_access;
1767 val = bl->owner->ops->insert_location (bl);
1768
1769 if (val)
1770 /* Back to the original value. */
1771 bl->watchpoint_type = hw_read;
85d721b8
PA
1772 }
1773 }
1774
35df4500 1775 bl->inserted = (val == 0);
879bfdc2
DJ
1776 }
1777
35df4500 1778 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1779 {
77b06cd7
TJB
1780 gdb_assert (bl->owner->ops != NULL
1781 && bl->owner->ops->insert_location != NULL);
1782
1783 val = bl->owner->ops->insert_location (bl);
1784 if (val)
1785 {
1786 bl->owner->enable_state = bp_disabled;
1787
1788 if (val == 1)
1789 warning (_("\
1790Error inserting catchpoint %d: Your system does not support this type\n\
1791of catchpoint."), bl->owner->number);
1792 else
1793 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1794 }
1795
1796 bl->inserted = (val == 0);
1640b821
DJ
1797
1798 /* We've already printed an error message if there was a problem
1799 inserting this catchpoint, and we've disabled the catchpoint,
1800 so just return success. */
1801 return 0;
879bfdc2
DJ
1802 }
1803
1804 return 0;
1805}
1806
6c95b8df
PA
1807/* This function is called when program space PSPACE is about to be
1808 deleted. It takes care of updating breakpoints to not reference
1809 PSPACE anymore. */
1810
1811void
1812breakpoint_program_space_exit (struct program_space *pspace)
1813{
1814 struct breakpoint *b, *b_temp;
876fa593 1815 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1816
1817 /* Remove any breakpoint that was set through this program space. */
1818 ALL_BREAKPOINTS_SAFE (b, b_temp)
1819 {
1820 if (b->pspace == pspace)
1821 delete_breakpoint (b);
1822 }
1823
1824 /* Breakpoints set through other program spaces could have locations
1825 bound to PSPACE as well. Remove those. */
876fa593 1826 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1827 {
1828 struct bp_location *tmp;
1829
1830 if (loc->pspace == pspace)
1831 {
2bdf28a0 1832 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1833 if (loc->owner->loc == loc)
1834 loc->owner->loc = loc->next;
1835 else
1836 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1837 if (tmp->next == loc)
1838 {
1839 tmp->next = loc->next;
1840 break;
1841 }
1842 }
1843 }
1844
1845 /* Now update the global location list to permanently delete the
1846 removed locations above. */
1847 update_global_location_list (0);
1848}
1849
74960c60
VP
1850/* Make sure all breakpoints are inserted in inferior.
1851 Throws exception on any error.
1852 A breakpoint that is already inserted won't be inserted
1853 again, so calling this function twice is safe. */
1854void
1855insert_breakpoints (void)
1856{
1857 struct breakpoint *bpt;
1858
1859 ALL_BREAKPOINTS (bpt)
1860 if (is_hardware_watchpoint (bpt))
4a64f543 1861 update_watchpoint (bpt, 0 /* don't reparse. */);
74960c60 1862
b60e7edf 1863 update_global_location_list (1);
74960c60 1864
c35b1492
PA
1865 /* update_global_location_list does not insert breakpoints when
1866 always_inserted_mode is not enabled. Explicitly insert them
1867 now. */
1868 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1869 insert_breakpoint_locations ();
1870}
1871
c906108c
SS
1872/* insert_breakpoints is used when starting or continuing the program.
1873 remove_breakpoints is used when the program stops.
1874 Both return zero if successful,
1875 or an `errno' value if could not write the inferior. */
1876
74960c60
VP
1877static void
1878insert_breakpoint_locations (void)
c906108c 1879{
a5606eee 1880 struct breakpoint *bpt;
35df4500 1881 struct bp_location *bl, **blp_tmp;
e236ba44 1882 int error = 0;
c906108c
SS
1883 int val = 0;
1884 int disabled_breaks = 0;
81d0cc19 1885 int hw_breakpoint_error = 0;
c906108c 1886
81d0cc19 1887 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1888 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1889
81d0cc19
GS
1890 /* Explicitly mark the warning -- this will only be printed if
1891 there was an error. */
1892 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1893
1894 save_current_space_and_thread ();
1895
35df4500 1896 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1897 {
35df4500 1898 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1899 continue;
1900
4a64f543
MS
1901 /* There is no point inserting thread-specific breakpoints if
1902 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1903 has BL->OWNER always non-NULL. */
35df4500
TJB
1904 if (bl->owner->thread != -1
1905 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1906 continue;
1907
35df4500 1908 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1909
1910 /* For targets that support global breakpoints, there's no need
1911 to select an inferior to insert breakpoint to. In fact, even
1912 if we aren't attached to any process yet, we should still
1913 insert breakpoints. */
1914 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1915 && ptid_equal (inferior_ptid, null_ptid))
1916 continue;
1917
35df4500 1918 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
879bfdc2
DJ
1919 &hw_breakpoint_error);
1920 if (val)
e236ba44 1921 error = val;
879bfdc2 1922 }
c906108c 1923
4a64f543
MS
1924 /* If we failed to insert all locations of a watchpoint, remove
1925 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1926 ALL_BREAKPOINTS (bpt)
1927 {
1928 int some_failed = 0;
1929 struct bp_location *loc;
1930
1931 if (!is_hardware_watchpoint (bpt))
1932 continue;
1933
d6b74ac4 1934 if (!breakpoint_enabled (bpt))
a5606eee 1935 continue;
74960c60
VP
1936
1937 if (bpt->disposition == disp_del_at_next_stop)
1938 continue;
a5606eee
VP
1939
1940 for (loc = bpt->loc; loc; loc = loc->next)
56710373 1941 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
1942 {
1943 some_failed = 1;
1944 break;
1945 }
1946 if (some_failed)
1947 {
1948 for (loc = bpt->loc; loc; loc = loc->next)
1949 if (loc->inserted)
1950 remove_breakpoint (loc, mark_uninserted);
1951
1952 hw_breakpoint_error = 1;
1953 fprintf_unfiltered (tmp_error_stream,
1954 "Could not insert hardware watchpoint %d.\n",
1955 bpt->number);
1956 error = -1;
1957 }
1958 }
1959
e236ba44 1960 if (error)
81d0cc19
GS
1961 {
1962 /* If a hardware breakpoint or watchpoint was inserted, add a
1963 message about possibly exhausted resources. */
879bfdc2 1964 if (hw_breakpoint_error)
81d0cc19 1965 {
c6510018
MS
1966 fprintf_unfiltered (tmp_error_stream,
1967 "Could not insert hardware breakpoints:\n\
1968You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1969 }
81d0cc19
GS
1970 target_terminal_ours_for_output ();
1971 error_stream (tmp_error_stream);
1972 }
f7545552
TT
1973
1974 do_cleanups (cleanups);
c906108c
SS
1975}
1976
c906108c 1977int
fba45db2 1978remove_breakpoints (void)
c906108c 1979{
35df4500 1980 struct bp_location *bl, **blp_tmp;
3a1bae8e 1981 int val = 0;
c906108c 1982
35df4500 1983 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 1984 {
35df4500
TJB
1985 if (bl->inserted)
1986 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 1987 }
3a1bae8e 1988 return val;
c906108c
SS
1989}
1990
6c95b8df
PA
1991/* Remove breakpoints of process PID. */
1992
1993int
1994remove_breakpoints_pid (int pid)
1995{
35df4500 1996 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
1997 int val;
1998 struct inferior *inf = find_inferior_pid (pid);
1999
35df4500 2000 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2001 {
35df4500 2002 if (bl->pspace != inf->pspace)
6c95b8df
PA
2003 continue;
2004
35df4500 2005 if (bl->inserted)
6c95b8df 2006 {
35df4500 2007 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2008 if (val != 0)
2009 return val;
2010 }
2011 }
2012 return 0;
2013}
2014
c906108c 2015int
fba45db2 2016reattach_breakpoints (int pid)
c906108c 2017{
6c95b8df 2018 struct cleanup *old_chain;
35df4500 2019 struct bp_location *bl, **blp_tmp;
c906108c 2020 int val;
86b887df 2021 struct ui_file *tmp_error_stream;
fa3a767f 2022 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2023 struct inferior *inf;
2024 struct thread_info *tp;
2025
2026 tp = any_live_thread_of_process (pid);
2027 if (tp == NULL)
2028 return 1;
2029
2030 inf = find_inferior_pid (pid);
2031 old_chain = save_inferior_ptid ();
2032
2033 inferior_ptid = tp->ptid;
a4954f26 2034
86b887df 2035 tmp_error_stream = mem_fileopen ();
a4954f26 2036 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2037
35df4500 2038 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2039 {
35df4500 2040 if (bl->pspace != inf->pspace)
6c95b8df
PA
2041 continue;
2042
35df4500 2043 if (bl->inserted)
c5aa993b 2044 {
35df4500
TJB
2045 bl->inserted = 0;
2046 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2047 if (val != 0)
2048 {
ce696e05 2049 do_cleanups (old_chain);
c5aa993b
JM
2050 return val;
2051 }
2052 }
2053 }
ce696e05 2054 do_cleanups (old_chain);
c906108c
SS
2055 return 0;
2056}
2057
e58b0e63
PA
2058static int internal_breakpoint_number = -1;
2059
84f4c1fe
PM
2060/* Set the breakpoint number of B, depending on the value of INTERNAL.
2061 If INTERNAL is non-zero, the breakpoint number will be populated
2062 from internal_breakpoint_number and that variable decremented.
2063 Otherwis the breakpoint number will be populated from
2064 breakpoint_count and that value incremented. Internal breakpoints
2065 do not set the internal var bpnum. */
2066static void
2067set_breakpoint_number (int internal, struct breakpoint *b)
2068{
2069 if (internal)
2070 b->number = internal_breakpoint_number--;
2071 else
2072 {
2073 set_breakpoint_count (breakpoint_count + 1);
2074 b->number = breakpoint_count;
2075 }
2076}
2077
e62c965a 2078static struct breakpoint *
a6d9a66e
UW
2079create_internal_breakpoint (struct gdbarch *gdbarch,
2080 CORE_ADDR address, enum bptype type)
e62c965a 2081{
e62c965a
PP
2082 struct symtab_and_line sal;
2083 struct breakpoint *b;
2084
4a64f543 2085 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2086
2087 sal.pc = address;
2088 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2089 sal.pspace = current_program_space;
e62c965a 2090
a6d9a66e 2091 b = set_raw_breakpoint (gdbarch, sal, type);
e62c965a
PP
2092 b->number = internal_breakpoint_number--;
2093 b->disposition = disp_donttouch;
2094
2095 return b;
2096}
2097
17450429
PP
2098static const char *const longjmp_names[] =
2099 {
2100 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2101 };
2102#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2103
2104/* Per-objfile data private to breakpoint.c. */
2105struct breakpoint_objfile_data
2106{
2107 /* Minimal symbol for "_ovly_debug_event" (if any). */
2108 struct minimal_symbol *overlay_msym;
2109
2110 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2111 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2112
2113 /* Minimal symbol for "std::terminate()" (if any). */
2114 struct minimal_symbol *terminate_msym;
2115
2116 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2117 struct minimal_symbol *exception_msym;
2118};
2119
2120static const struct objfile_data *breakpoint_objfile_key;
2121
2122/* Minimal symbol not found sentinel. */
2123static struct minimal_symbol msym_not_found;
2124
2125/* Returns TRUE if MSYM point to the "not found" sentinel. */
2126
2127static int
2128msym_not_found_p (const struct minimal_symbol *msym)
2129{
2130 return msym == &msym_not_found;
2131}
2132
2133/* Return per-objfile data needed by breakpoint.c.
2134 Allocate the data if necessary. */
2135
2136static struct breakpoint_objfile_data *
2137get_breakpoint_objfile_data (struct objfile *objfile)
2138{
2139 struct breakpoint_objfile_data *bp_objfile_data;
2140
2141 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2142 if (bp_objfile_data == NULL)
2143 {
2144 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2145 sizeof (*bp_objfile_data));
2146
2147 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2148 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2149 }
2150 return bp_objfile_data;
2151}
2152
e62c965a 2153static void
af02033e 2154create_overlay_event_breakpoint (void)
e62c965a 2155{
69de3c6a 2156 struct objfile *objfile;
af02033e 2157 const char *const func_name = "_ovly_debug_event";
e62c965a 2158
69de3c6a
PP
2159 ALL_OBJFILES (objfile)
2160 {
2161 struct breakpoint *b;
17450429
PP
2162 struct breakpoint_objfile_data *bp_objfile_data;
2163 CORE_ADDR addr;
69de3c6a 2164
17450429
PP
2165 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2166
2167 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2168 continue;
2169
2170 if (bp_objfile_data->overlay_msym == NULL)
2171 {
2172 struct minimal_symbol *m;
2173
2174 m = lookup_minimal_symbol_text (func_name, objfile);
2175 if (m == NULL)
2176 {
2177 /* Avoid future lookups in this objfile. */
2178 bp_objfile_data->overlay_msym = &msym_not_found;
2179 continue;
2180 }
2181 bp_objfile_data->overlay_msym = m;
2182 }
e62c965a 2183
17450429
PP
2184 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2185 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
69de3c6a
PP
2186 bp_overlay_event);
2187 b->addr_string = xstrdup (func_name);
e62c965a 2188
69de3c6a
PP
2189 if (overlay_debugging == ovly_auto)
2190 {
2191 b->enable_state = bp_enabled;
2192 overlay_events_enabled = 1;
2193 }
2194 else
2195 {
2196 b->enable_state = bp_disabled;
2197 overlay_events_enabled = 0;
2198 }
e62c965a
PP
2199 }
2200 update_global_location_list (1);
2201}
2202
0fd8e87f 2203static void
af02033e 2204create_longjmp_master_breakpoint (void)
0fd8e87f 2205{
6c95b8df 2206 struct program_space *pspace;
6c95b8df
PA
2207 struct cleanup *old_chain;
2208
2209 old_chain = save_current_program_space ();
0fd8e87f 2210
6c95b8df 2211 ALL_PSPACES (pspace)
af02033e
PP
2212 {
2213 struct objfile *objfile;
2214
2215 set_current_program_space (pspace);
2216
2217 ALL_OBJFILES (objfile)
0fd8e87f 2218 {
af02033e
PP
2219 int i;
2220 struct gdbarch *gdbarch;
17450429 2221 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2222
af02033e
PP
2223 gdbarch = get_objfile_arch (objfile);
2224 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2225 continue;
2226
17450429
PP
2227 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2228
2229 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2230 {
2231 struct breakpoint *b;
af02033e 2232 const char *func_name;
17450429 2233 CORE_ADDR addr;
6c95b8df 2234
17450429 2235 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2236 continue;
0fd8e87f 2237
17450429
PP
2238 func_name = longjmp_names[i];
2239 if (bp_objfile_data->longjmp_msym[i] == NULL)
2240 {
2241 struct minimal_symbol *m;
2242
2243 m = lookup_minimal_symbol_text (func_name, objfile);
2244 if (m == NULL)
2245 {
2246 /* Prevent future lookups in this objfile. */
2247 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2248 continue;
2249 }
2250 bp_objfile_data->longjmp_msym[i] = m;
2251 }
2252
2253 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
2254 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
af02033e
PP
2255 b->addr_string = xstrdup (func_name);
2256 b->enable_state = bp_disabled;
2257 }
0fd8e87f 2258 }
af02033e 2259 }
0fd8e87f 2260 update_global_location_list (1);
6c95b8df
PA
2261
2262 do_cleanups (old_chain);
0fd8e87f
UW
2263}
2264
af02033e 2265/* Create a master std::terminate breakpoint. */
aa7d318d 2266static void
af02033e 2267create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2268{
2269 struct program_space *pspace;
aa7d318d 2270 struct cleanup *old_chain;
af02033e 2271 const char *const func_name = "std::terminate()";
aa7d318d
TT
2272
2273 old_chain = save_current_program_space ();
2274
2275 ALL_PSPACES (pspace)
17450429
PP
2276 {
2277 struct objfile *objfile;
2278 CORE_ADDR addr;
2279
2280 set_current_program_space (pspace);
2281
aa7d318d
TT
2282 ALL_OBJFILES (objfile)
2283 {
2284 struct breakpoint *b;
17450429 2285 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2286
17450429 2287 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2288
17450429
PP
2289 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2290 continue;
2291
2292 if (bp_objfile_data->terminate_msym == NULL)
2293 {
2294 struct minimal_symbol *m;
2295
2296 m = lookup_minimal_symbol (func_name, NULL, objfile);
2297 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2298 && MSYMBOL_TYPE (m) != mst_file_text))
2299 {
2300 /* Prevent future lookups in this objfile. */
2301 bp_objfile_data->terminate_msym = &msym_not_found;
2302 continue;
2303 }
2304 bp_objfile_data->terminate_msym = m;
2305 }
aa7d318d 2306
17450429
PP
2307 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2308 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
aa7d318d
TT
2309 bp_std_terminate_master);
2310 b->addr_string = xstrdup (func_name);
2311 b->enable_state = bp_disabled;
2312 }
17450429
PP
2313 }
2314
aa7d318d
TT
2315 update_global_location_list (1);
2316
2317 do_cleanups (old_chain);
2318}
2319
186c406b
TT
2320/* Install a master breakpoint on the unwinder's debug hook. */
2321
2322void
2323create_exception_master_breakpoint (void)
2324{
2325 struct objfile *objfile;
17450429 2326 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2327
2328 ALL_OBJFILES (objfile)
2329 {
17450429
PP
2330 struct breakpoint *b;
2331 struct gdbarch *gdbarch;
2332 struct breakpoint_objfile_data *bp_objfile_data;
2333 CORE_ADDR addr;
2334
2335 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2336
2337 if (msym_not_found_p (bp_objfile_data->exception_msym))
2338 continue;
2339
2340 gdbarch = get_objfile_arch (objfile);
186c406b 2341
17450429 2342 if (bp_objfile_data->exception_msym == NULL)
186c406b 2343 {
17450429 2344 struct minimal_symbol *debug_hook;
186c406b 2345
17450429
PP
2346 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2347 if (debug_hook == NULL)
2348 {
2349 bp_objfile_data->exception_msym = &msym_not_found;
2350 continue;
2351 }
2352
2353 bp_objfile_data->exception_msym = debug_hook;
186c406b 2354 }
17450429
PP
2355
2356 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2357 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2358 &current_target);
2359 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
2360 b->addr_string = xstrdup (func_name);
2361 b->enable_state = bp_disabled;
186c406b
TT
2362 }
2363
2364 update_global_location_list (1);
2365}
2366
c906108c 2367void
fba45db2 2368update_breakpoints_after_exec (void)
c906108c 2369{
35df4500 2370 struct breakpoint *b, *b_tmp;
876fa593 2371 struct bp_location *bploc, **bplocp_tmp;
c906108c 2372
25b22b0a
PA
2373 /* We're about to delete breakpoints from GDB's lists. If the
2374 INSERTED flag is true, GDB will try to lift the breakpoints by
2375 writing the breakpoints' "shadow contents" back into memory. The
2376 "shadow contents" are NOT valid after an exec, so GDB should not
2377 do that. Instead, the target is responsible from marking
2378 breakpoints out as soon as it detects an exec. We don't do that
2379 here instead, because there may be other attempts to delete
2380 breakpoints after detecting an exec and before reaching here. */
876fa593 2381 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2382 if (bploc->pspace == current_program_space)
2383 gdb_assert (!bploc->inserted);
c906108c 2384
35df4500 2385 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2386 {
6c95b8df
PA
2387 if (b->pspace != current_program_space)
2388 continue;
2389
4a64f543 2390 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2391 if (b->type == bp_shlib_event)
2392 {
2393 delete_breakpoint (b);
2394 continue;
2395 }
c906108c 2396
4a64f543 2397 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2398 if (b->type == bp_jit_event)
2399 {
2400 delete_breakpoint (b);
2401 continue;
2402 }
2403
1900040c 2404 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2405 as must overlay event and longjmp master breakpoints. */
2406 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2407 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2408 || b->type == bp_exception_master)
c4093a6a
JM
2409 {
2410 delete_breakpoint (b);
2411 continue;
2412 }
2413
4a64f543 2414 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2415 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2416 {
2417 delete_breakpoint (b);
2418 continue;
2419 }
2420
611c83ae
PA
2421 /* Longjmp and longjmp-resume breakpoints are also meaningless
2422 after an exec. */
186c406b
TT
2423 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2424 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2425 {
2426 delete_breakpoint (b);
2427 continue;
2428 }
2429
ce78b96d
JB
2430 if (b->type == bp_catchpoint)
2431 {
2432 /* For now, none of the bp_catchpoint breakpoints need to
2433 do anything at this point. In the future, if some of
2434 the catchpoints need to something, we will need to add
2435 a new method, and call this method from here. */
2436 continue;
2437 }
2438
c5aa993b
JM
2439 /* bp_finish is a special case. The only way we ought to be able
2440 to see one of these when an exec() has happened, is if the user
2441 caught a vfork, and then said "finish". Ordinarily a finish just
2442 carries them to the call-site of the current callee, by setting
2443 a temporary bp there and resuming. But in this case, the finish
2444 will carry them entirely through the vfork & exec.
2445
2446 We don't want to allow a bp_finish to remain inserted now. But
2447 we can't safely delete it, 'cause finish_command has a handle to
2448 the bp on a bpstat, and will later want to delete it. There's a
2449 chance (and I've seen it happen) that if we delete the bp_finish
2450 here, that its storage will get reused by the time finish_command
2451 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2452 We really must allow finish_command to delete a bp_finish.
2453
53a5351d
JM
2454 In the absense of a general solution for the "how do we know
2455 it's safe to delete something others may have handles to?"
2456 problem, what we'll do here is just uninsert the bp_finish, and
2457 let finish_command delete it.
2458
2459 (We know the bp_finish is "doomed" in the sense that it's
2460 momentary, and will be deleted as soon as finish_command sees
2461 the inferior stopped. So it doesn't matter that the bp's
2462 address is probably bogus in the new a.out, unlike e.g., the
2463 solib breakpoints.) */
c5aa993b 2464
c5aa993b
JM
2465 if (b->type == bp_finish)
2466 {
2467 continue;
2468 }
2469
2470 /* Without a symbolic address, we have little hope of the
2471 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2472 a.out. */
c5aa993b
JM
2473 if (b->addr_string == NULL)
2474 {
2475 delete_breakpoint (b);
2476 continue;
2477 }
c5aa993b 2478 }
1900040c 2479 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2480 create_overlay_event_breakpoint ();
2481 create_longjmp_master_breakpoint ();
2482 create_std_terminate_master_breakpoint ();
186c406b 2483 create_exception_master_breakpoint ();
c906108c
SS
2484}
2485
2486int
fba45db2 2487detach_breakpoints (int pid)
c906108c 2488{
35df4500 2489 struct bp_location *bl, **blp_tmp;
3a1bae8e 2490 int val = 0;
ce696e05 2491 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2492 struct inferior *inf = current_inferior ();
c5aa993b 2493
39f77062 2494 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2495 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2496
6c95b8df 2497 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2498 inferior_ptid = pid_to_ptid (pid);
35df4500 2499 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2500 {
35df4500 2501 if (bl->pspace != inf->pspace)
6c95b8df
PA
2502 continue;
2503
35df4500
TJB
2504 if (bl->inserted)
2505 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2506 }
d03285ec
UW
2507
2508 /* Detach single-step breakpoints as well. */
2509 detach_single_step_breakpoints ();
2510
ce696e05 2511 do_cleanups (old_chain);
3a1bae8e 2512 return val;
c906108c
SS
2513}
2514
35df4500 2515/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2516 Note that this is used to detach breakpoints from a child fork.
2517 When we get here, the child isn't in the inferior list, and neither
2518 do we have objects to represent its address space --- we should
35df4500 2519 *not* look at bl->pspace->aspace here. */
6c95b8df 2520
c906108c 2521static int
35df4500 2522remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2523{
2524 int val;
c5aa993b 2525
35df4500
TJB
2526 /* BL is never in moribund_locations by our callers. */
2527 gdb_assert (bl->owner != NULL);
2bdf28a0 2528
35df4500 2529 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2530 /* Permanent breakpoints cannot be inserted or removed. */
2531 return 0;
2532
74960c60
VP
2533 /* The type of none suggests that owner is actually deleted.
2534 This should not ever happen. */
35df4500 2535 gdb_assert (bl->owner->type != bp_none);
0bde7532 2536
35df4500
TJB
2537 if (bl->loc_type == bp_loc_software_breakpoint
2538 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2539 {
c02f5703
MS
2540 /* "Normal" instruction breakpoint: either the standard
2541 trap-instruction bp (bp_breakpoint), or a
2542 bp_hardware_breakpoint. */
2543
2544 /* First check to see if we have to handle an overlay. */
2545 if (overlay_debugging == ovly_off
35df4500
TJB
2546 || bl->section == NULL
2547 || !(section_is_overlay (bl->section)))
c02f5703
MS
2548 {
2549 /* No overlay handling: just remove the breakpoint. */
2550
35df4500
TJB
2551 if (bl->loc_type == bp_loc_hardware_breakpoint)
2552 val = target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2553 else
35df4500 2554 val = target_remove_breakpoint (bl->gdbarch, &bl->target_info);
c02f5703 2555 }
c906108c
SS
2556 else
2557 {
4a64f543 2558 /* This breakpoint is in an overlay section.
c02f5703
MS
2559 Did we set a breakpoint at the LMA? */
2560 if (!overlay_events_enabled)
2561 {
2562 /* Yes -- overlay event support is not active, so we
2563 should have set a breakpoint at the LMA. Remove it.
2564 */
c02f5703
MS
2565 /* Ignore any failures: if the LMA is in ROM, we will
2566 have already warned when we failed to insert it. */
35df4500
TJB
2567 if (bl->loc_type == bp_loc_hardware_breakpoint)
2568 target_remove_hw_breakpoint (bl->gdbarch,
2569 &bl->overlay_target_info);
c02f5703 2570 else
35df4500
TJB
2571 target_remove_breakpoint (bl->gdbarch,
2572 &bl->overlay_target_info);
c02f5703
MS
2573 }
2574 /* Did we set a breakpoint at the VMA?
2575 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2576 if (bl->inserted)
c906108c 2577 {
c02f5703
MS
2578 /* Yes -- remove it. Previously we did not bother to
2579 remove the breakpoint if the section had been
2580 unmapped, but let's not rely on that being safe. We
2581 don't know what the overlay manager might do. */
35df4500
TJB
2582 if (bl->loc_type == bp_loc_hardware_breakpoint)
2583 val = target_remove_hw_breakpoint (bl->gdbarch,
2584 &bl->target_info);
aa67235e
UW
2585
2586 /* However, we should remove *software* breakpoints only
2587 if the section is still mapped, or else we overwrite
2588 wrong code with the saved shadow contents. */
35df4500
TJB
2589 else if (section_is_mapped (bl->section))
2590 val = target_remove_breakpoint (bl->gdbarch,
2591 &bl->target_info);
aa67235e
UW
2592 else
2593 val = 0;
c906108c 2594 }
c02f5703
MS
2595 else
2596 {
2597 /* No -- not inserted, so no need to remove. No error. */
2598 val = 0;
2599 }
c906108c 2600 }
879d1e6b
UW
2601
2602 /* In some cases, we might not be able to remove a breakpoint
2603 in a shared library that has already been removed, but we
2604 have not yet processed the shlib unload event. */
35df4500 2605 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2606 val = 0;
2607
c906108c
SS
2608 if (val)
2609 return val;
35df4500 2610 bl->inserted = (is == mark_inserted);
c906108c 2611 }
35df4500 2612 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2613 {
77b06cd7
TJB
2614 gdb_assert (bl->owner->ops != NULL
2615 && bl->owner->ops->remove_location != NULL);
2616
35df4500 2617 bl->inserted = (is == mark_inserted);
77b06cd7 2618 bl->owner->ops->remove_location (bl);
2e70b7b9 2619
c906108c 2620 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2621 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2622 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2623 bl->owner->number);
c906108c 2624 }
35df4500
TJB
2625 else if (bl->owner->type == bp_catchpoint
2626 && breakpoint_enabled (bl->owner)
2627 && !bl->duplicate)
ce78b96d 2628 {
77b06cd7
TJB
2629 gdb_assert (bl->owner->ops != NULL
2630 && bl->owner->ops->remove_location != NULL);
ce78b96d 2631
77b06cd7 2632 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2633 if (val)
2634 return val;
77b06cd7 2635
35df4500 2636 bl->inserted = (is == mark_inserted);
ce78b96d 2637 }
c906108c
SS
2638
2639 return 0;
2640}
2641
6c95b8df 2642static int
35df4500 2643remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2644{
2645 int ret;
2646 struct cleanup *old_chain;
2647
35df4500
TJB
2648 /* BL is never in moribund_locations by our callers. */
2649 gdb_assert (bl->owner != NULL);
2bdf28a0 2650
35df4500 2651 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2652 /* Permanent breakpoints cannot be inserted or removed. */
2653 return 0;
2654
2655 /* The type of none suggests that owner is actually deleted.
2656 This should not ever happen. */
35df4500 2657 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2658
2659 old_chain = save_current_space_and_thread ();
2660
35df4500 2661 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2662
35df4500 2663 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2664
2665 do_cleanups (old_chain);
2666 return ret;
2667}
2668
c906108c
SS
2669/* Clear the "inserted" flag in all breakpoints. */
2670
25b22b0a 2671void
fba45db2 2672mark_breakpoints_out (void)
c906108c 2673{
35df4500 2674 struct bp_location *bl, **blp_tmp;
c906108c 2675
35df4500
TJB
2676 ALL_BP_LOCATIONS (bl, blp_tmp)
2677 if (bl->pspace == current_program_space)
2678 bl->inserted = 0;
c906108c
SS
2679}
2680
53a5351d
JM
2681/* Clear the "inserted" flag in all breakpoints and delete any
2682 breakpoints which should go away between runs of the program.
c906108c
SS
2683
2684 Plus other such housekeeping that has to be done for breakpoints
2685 between runs.
2686
53a5351d
JM
2687 Note: this function gets called at the end of a run (by
2688 generic_mourn_inferior) and when a run begins (by
4a64f543 2689 init_wait_for_inferior). */
c906108c
SS
2690
2691
2692
2693void
fba45db2 2694breakpoint_init_inferior (enum inf_context context)
c906108c 2695{
35df4500
TJB
2696 struct breakpoint *b, *b_tmp;
2697 struct bp_location *bl, **blp_tmp;
1c5cfe86 2698 int ix;
6c95b8df 2699 struct program_space *pspace = current_program_space;
c906108c 2700
50c71eaf
PA
2701 /* If breakpoint locations are shared across processes, then there's
2702 nothing to do. */
2567c7d9 2703 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2704 return;
2705
35df4500 2706 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2707 {
35df4500
TJB
2708 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2709 if (bl->pspace == pspace
2710 && bl->owner->enable_state != bp_permanent)
2711 bl->inserted = 0;
6c95b8df 2712 }
075f6582 2713
35df4500 2714 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2715 {
6c95b8df
PA
2716 if (b->loc && b->loc->pspace != pspace)
2717 continue;
2718
c5aa993b
JM
2719 switch (b->type)
2720 {
2721 case bp_call_dummy:
c906108c 2722
c5aa993b 2723 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2724 cause problems when the inferior is rerun, so we better get
2725 rid of it. */
2726
2727 case bp_watchpoint_scope:
2728
2729 /* Also get rid of scope breakpoints. */
2730
2731 case bp_shlib_event:
2732
2733 /* Also remove solib event breakpoints. Their addresses may
2734 have changed since the last time we ran the program.
2735 Actually we may now be debugging against different target;
2736 and so the solib backend that installed this breakpoint may
2737 not be used in by the target. E.g.,
2738
2739 (gdb) file prog-linux
2740 (gdb) run # native linux target
2741 ...
2742 (gdb) kill
2743 (gdb) file prog-win.exe
2744 (gdb) tar rem :9999 # remote Windows gdbserver.
2745 */
c906108c 2746
c5aa993b
JM
2747 delete_breakpoint (b);
2748 break;
c906108c 2749
c5aa993b
JM
2750 case bp_watchpoint:
2751 case bp_hardware_watchpoint:
2752 case bp_read_watchpoint:
2753 case bp_access_watchpoint:
c906108c 2754
c5aa993b
JM
2755 /* Likewise for watchpoints on local expressions. */
2756 if (b->exp_valid_block != NULL)
2757 delete_breakpoint (b);
967af18d 2758 else if (context == inf_starting)
c860120c 2759 {
4a64f543
MS
2760 /* Reset val field to force reread of starting value in
2761 insert_breakpoints. */
c860120c
PM
2762 if (b->val)
2763 value_free (b->val);
2764 b->val = NULL;
fa4727a6 2765 b->val_valid = 0;
c860120c 2766 }
c5aa993b
JM
2767 break;
2768 default:
c5aa993b
JM
2769 break;
2770 }
2771 }
1c5cfe86
PA
2772
2773 /* Get rid of the moribund locations. */
35df4500
TJB
2774 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2775 decref_bp_location (&bl);
1c5cfe86 2776 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2777}
2778
6c95b8df
PA
2779/* These functions concern about actual breakpoints inserted in the
2780 target --- to e.g. check if we need to do decr_pc adjustment or if
2781 we need to hop over the bkpt --- so we check for address space
2782 match, not program space. */
2783
c2c6d25f
JM
2784/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2785 exists at PC. It returns ordinary_breakpoint_here if it's an
2786 ordinary breakpoint, or permanent_breakpoint_here if it's a
2787 permanent breakpoint.
2788 - When continuing from a location with an ordinary breakpoint, we
2789 actually single step once before calling insert_breakpoints.
2790 - When continuing from a localion with a permanent breakpoint, we
2791 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2792 the target, to advance the PC past the breakpoint. */
c906108c 2793
c2c6d25f 2794enum breakpoint_here
6c95b8df 2795breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2796{
35df4500 2797 struct bp_location *bl, **blp_tmp;
c2c6d25f 2798 int any_breakpoint_here = 0;
c906108c 2799
35df4500 2800 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2801 {
35df4500
TJB
2802 if (bl->loc_type != bp_loc_software_breakpoint
2803 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2804 continue;
2805
f1310107 2806 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2807 if ((breakpoint_enabled (bl->owner)
2808 || bl->owner->enable_state == bp_permanent)
f1310107 2809 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2810 {
2811 if (overlay_debugging
35df4500
TJB
2812 && section_is_overlay (bl->section)
2813 && !section_is_mapped (bl->section))
075f6582 2814 continue; /* unmapped overlay -- can't be a match */
35df4500 2815 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2816 return permanent_breakpoint_here;
2817 else
2818 any_breakpoint_here = 1;
2819 }
2820 }
c906108c 2821
c2c6d25f 2822 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2823}
2824
1c5cfe86
PA
2825/* Return true if there's a moribund breakpoint at PC. */
2826
2827int
6c95b8df 2828moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2829{
2830 struct bp_location *loc;
2831 int ix;
2832
2833 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2834 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2835 return 1;
2836
2837 return 0;
2838}
c2c6d25f 2839
c36b740a 2840/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2841 inserted using regular breakpoint_chain / bp_location array
2842 mechanism. This does not check for single-step breakpoints, which
2843 are inserted and removed using direct target manipulation. */
c906108c
SS
2844
2845int
4a64f543
MS
2846regular_breakpoint_inserted_here_p (struct address_space *aspace,
2847 CORE_ADDR pc)
c906108c 2848{
35df4500 2849 struct bp_location *bl, **blp_tmp;
c906108c 2850
35df4500 2851 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2852 {
35df4500
TJB
2853 if (bl->loc_type != bp_loc_software_breakpoint
2854 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2855 continue;
2856
35df4500 2857 if (bl->inserted
f1310107 2858 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2859 {
2860 if (overlay_debugging
35df4500
TJB
2861 && section_is_overlay (bl->section)
2862 && !section_is_mapped (bl->section))
075f6582
DJ
2863 continue; /* unmapped overlay -- can't be a match */
2864 else
2865 return 1;
2866 }
c5aa993b 2867 }
c36b740a
VP
2868 return 0;
2869}
2870
2871/* Returns non-zero iff there's either regular breakpoint
2872 or a single step breakpoint inserted at PC. */
2873
2874int
6c95b8df 2875breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2876{
6c95b8df 2877 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2878 return 1;
c906108c 2879
6c95b8df 2880 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2881 return 1;
2882
c906108c
SS
2883 return 0;
2884}
2885
4fa8626c
DJ
2886/* This function returns non-zero iff there is a software breakpoint
2887 inserted at PC. */
2888
2889int
3e43a32a
MS
2890software_breakpoint_inserted_here_p (struct address_space *aspace,
2891 CORE_ADDR pc)
4fa8626c 2892{
35df4500 2893 struct bp_location *bl, **blp_tmp;
4fa8626c 2894
35df4500 2895 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2896 {
35df4500 2897 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2898 continue;
2899
35df4500
TJB
2900 if (bl->inserted
2901 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2902 aspace, pc))
4fa8626c
DJ
2903 {
2904 if (overlay_debugging
35df4500
TJB
2905 && section_is_overlay (bl->section)
2906 && !section_is_mapped (bl->section))
4fa8626c
DJ
2907 continue; /* unmapped overlay -- can't be a match */
2908 else
2909 return 1;
2910 }
2911 }
2912
1aafd4da 2913 /* Also check for software single-step breakpoints. */
6c95b8df 2914 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2915 return 1;
2916
4fa8626c
DJ
2917 return 0;
2918}
2919
9093389c
PA
2920int
2921hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2922 CORE_ADDR addr, ULONGEST len)
2923{
2924 struct breakpoint *bpt;
2925
2926 ALL_BREAKPOINTS (bpt)
2927 {
2928 struct bp_location *loc;
2929
2930 if (bpt->type != bp_hardware_watchpoint
2931 && bpt->type != bp_access_watchpoint)
2932 continue;
2933
2934 if (!breakpoint_enabled (bpt))
2935 continue;
2936
2937 for (loc = bpt->loc; loc; loc = loc->next)
2938 if (loc->pspace->aspace == aspace && loc->inserted)
2939 {
2940 CORE_ADDR l, h;
2941
2942 /* Check for intersection. */
2943 l = max (loc->address, addr);
2944 h = min (loc->address + loc->length, addr + len);
2945 if (l < h)
2946 return 1;
2947 }
2948 }
2949 return 0;
2950}
2951
075f6582
DJ
2952/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2953 PC is valid for process/thread PTID. */
c906108c
SS
2954
2955int
6c95b8df
PA
2956breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2957 ptid_t ptid)
c906108c 2958{
35df4500 2959 struct bp_location *bl, **blp_tmp;
4a306c9a 2960 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2961 int thread = -1;
4a306c9a 2962 int task = 0;
a6f1cd96 2963
35df4500 2964 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2965 {
35df4500
TJB
2966 if (bl->loc_type != bp_loc_software_breakpoint
2967 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2968 continue;
2969
35df4500
TJB
2970 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
2971 if (!breakpoint_enabled (bl->owner)
2972 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
2973 continue;
2974
f1310107 2975 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
2976 continue;
2977
35df4500 2978 if (bl->owner->thread != -1)
075f6582 2979 {
a6f1cd96
JB
2980 /* This is a thread-specific breakpoint. Check that ptid
2981 matches that thread. If thread hasn't been computed yet,
2982 it is now time to do so. */
2983 if (thread == -1)
2984 thread = pid_to_thread_id (ptid);
35df4500 2985 if (bl->owner->thread != thread)
a6f1cd96 2986 continue;
075f6582 2987 }
a6f1cd96 2988
35df4500 2989 if (bl->owner->task != 0)
4a306c9a
JB
2990 {
2991 /* This is a task-specific breakpoint. Check that ptid
2992 matches that task. If task hasn't been computed yet,
2993 it is now time to do so. */
2994 if (task == 0)
2995 task = ada_get_task_number (ptid);
35df4500 2996 if (bl->owner->task != task)
4a306c9a
JB
2997 continue;
2998 }
2999
a6f1cd96 3000 if (overlay_debugging
35df4500
TJB
3001 && section_is_overlay (bl->section)
3002 && !section_is_mapped (bl->section))
a6f1cd96
JB
3003 continue; /* unmapped overlay -- can't be a match */
3004
3005 return 1;
c5aa993b 3006 }
c906108c
SS
3007
3008 return 0;
3009}
c906108c 3010\f
c5aa993b 3011
c906108c
SS
3012/* bpstat stuff. External routines' interfaces are documented
3013 in breakpoint.h. */
3014
3015int
fba45db2 3016ep_is_catchpoint (struct breakpoint *ep)
c906108c 3017{
533be4dd 3018 return (ep->type == bp_catchpoint);
c906108c
SS
3019}
3020
f431efe5
PA
3021/* Frees any storage that is part of a bpstat. Does not walk the
3022 'next' chain. */
3023
3024static void
198757a8
VP
3025bpstat_free (bpstat bs)
3026{
3027 if (bs->old_val != NULL)
3028 value_free (bs->old_val);
9add0f1b 3029 decref_counted_command_line (&bs->commands);
f431efe5 3030 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3031 xfree (bs);
3032}
3033
c906108c
SS
3034/* Clear a bpstat so that it says we are not at any breakpoint.
3035 Also free any storage that is part of a bpstat. */
3036
3037void
fba45db2 3038bpstat_clear (bpstat *bsp)
c906108c
SS
3039{
3040 bpstat p;
3041 bpstat q;
3042
3043 if (bsp == 0)
3044 return;
3045 p = *bsp;
3046 while (p != NULL)
3047 {
3048 q = p->next;
198757a8 3049 bpstat_free (p);
c906108c
SS
3050 p = q;
3051 }
3052 *bsp = NULL;
3053}
3054
3055/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3056 is part of the bpstat is copied as well. */
3057
3058bpstat
fba45db2 3059bpstat_copy (bpstat bs)
c906108c
SS
3060{
3061 bpstat p = NULL;
3062 bpstat tmp;
3063 bpstat retval = NULL;
3064
3065 if (bs == NULL)
3066 return bs;
3067
3068 for (; bs != NULL; bs = bs->next)
3069 {
3070 tmp = (bpstat) xmalloc (sizeof (*tmp));
3071 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3072 incref_counted_command_line (tmp->commands);
f431efe5 3073 incref_bp_location (tmp->bp_location_at);
31cc81e9 3074 if (bs->old_val != NULL)
3c3185ac
JK
3075 {
3076 tmp->old_val = value_copy (bs->old_val);
3077 release_value (tmp->old_val);
3078 }
31cc81e9 3079
c906108c
SS
3080 if (p == NULL)
3081 /* This is the first thing in the chain. */
3082 retval = tmp;
3083 else
3084 p->next = tmp;
3085 p = tmp;
3086 }
3087 p->next = NULL;
3088 return retval;
3089}
3090
4a64f543 3091/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3092
3093bpstat
fba45db2 3094bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3095{
c5aa993b
JM
3096 if (bsp == NULL)
3097 return NULL;
c906108c 3098
c5aa993b
JM
3099 for (; bsp != NULL; bsp = bsp->next)
3100 {
f431efe5 3101 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3102 return bsp;
3103 }
c906108c
SS
3104 return NULL;
3105}
3106
4a64f543
MS
3107/* Put in *NUM the breakpoint number of the first breakpoint we are
3108 stopped at. *BSP upon return is a bpstat which points to the
3109 remaining breakpoints stopped at (but which is not guaranteed to be
3110 good for anything but further calls to bpstat_num).
3111
8671a17b
PA
3112 Return 0 if passed a bpstat which does not indicate any breakpoints.
3113 Return -1 if stopped at a breakpoint that has been deleted since
3114 we set it.
3115 Return 1 otherwise. */
c906108c
SS
3116
3117int
8671a17b 3118bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3119{
3120 struct breakpoint *b;
3121
3122 if ((*bsp) == NULL)
3123 return 0; /* No more breakpoint values */
8671a17b 3124
4a64f543
MS
3125 /* We assume we'll never have several bpstats that correspond to a
3126 single breakpoint -- otherwise, this function might return the
3127 same number more than once and this will look ugly. */
f431efe5 3128 b = (*bsp)->breakpoint_at;
8671a17b
PA
3129 *bsp = (*bsp)->next;
3130 if (b == NULL)
3131 return -1; /* breakpoint that's been deleted since */
3132
3133 *num = b->number; /* We have its number */
3134 return 1;
c906108c
SS
3135}
3136
3137/* Modify BS so that the actions will not be performed. */
3138
3139void
fba45db2 3140bpstat_clear_actions (bpstat bs)
c906108c
SS
3141{
3142 for (; bs != NULL; bs = bs->next)
3143 {
9add0f1b 3144 decref_counted_command_line (&bs->commands);
dde2d684 3145 bs->commands_left = NULL;
c906108c
SS
3146 if (bs->old_val != NULL)
3147 {
3148 value_free (bs->old_val);
3149 bs->old_val = NULL;
3150 }
3151 }
3152}
3153
f3b1572e
PA
3154/* Called when a command is about to proceed the inferior. */
3155
3156static void
3157breakpoint_about_to_proceed (void)
3158{
3159 if (!ptid_equal (inferior_ptid, null_ptid))
3160 {
3161 struct thread_info *tp = inferior_thread ();
3162
3163 /* Allow inferior function calls in breakpoint commands to not
3164 interrupt the command list. When the call finishes
3165 successfully, the inferior will be standing at the same
3166 breakpoint as if nothing happened. */
16c381f0 3167 if (tp->control.in_infcall)
f3b1572e
PA
3168 return;
3169 }
3170
3171 breakpoint_proceeded = 1;
3172}
3173
4a64f543
MS
3174/* Stub for cleaning up our state if we error-out of a breakpoint
3175 command. */
c906108c 3176static void
4efb68b1 3177cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3178{
3179 executing_breakpoint_commands = 0;
3180}
3181
4a64f543
MS
3182/* Execute all the commands associated with all the breakpoints at
3183 this location. Any of these commands could cause the process to
3184 proceed beyond this point, etc. We look out for such changes by
3185 checking the global "breakpoint_proceeded" after each command.
c906108c 3186
347bddb7
PA
3187 Returns true if a breakpoint command resumed the inferior. In that
3188 case, it is the caller's responsibility to recall it again with the
3189 bpstat of the current thread. */
3190
3191static int
3192bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3193{
3194 bpstat bs;
3195 struct cleanup *old_chain;
347bddb7 3196 int again = 0;
c906108c
SS
3197
3198 /* Avoid endless recursion if a `source' command is contained
3199 in bs->commands. */
3200 if (executing_breakpoint_commands)
347bddb7 3201 return 0;
c906108c
SS
3202
3203 executing_breakpoint_commands = 1;
3204 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3205
cf6c5ffb
TT
3206 prevent_dont_repeat ();
3207
4a64f543 3208 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3209 bs = *bsp;
3210
3211 breakpoint_proceeded = 0;
3212 for (; bs != NULL; bs = bs->next)
3213 {
9add0f1b 3214 struct counted_command_line *ccmd;
6c50ab1c
JB
3215 struct command_line *cmd;
3216 struct cleanup *this_cmd_tree_chain;
3217
3218 /* Take ownership of the BSP's command tree, if it has one.
3219
3220 The command tree could legitimately contain commands like
3221 'step' and 'next', which call clear_proceed_status, which
3222 frees stop_bpstat's command tree. To make sure this doesn't
3223 free the tree we're executing out from under us, we need to
3224 take ownership of the tree ourselves. Since a given bpstat's
3225 commands are only executed once, we don't need to copy it; we
3226 can clear the pointer in the bpstat, and make sure we free
3227 the tree when we're done. */
9add0f1b
TT
3228 ccmd = bs->commands;
3229 bs->commands = NULL;
3230 this_cmd_tree_chain
3231 = make_cleanup_decref_counted_command_line (&ccmd);
3232 cmd = bs->commands_left;
3233 bs->commands_left = NULL;
6c50ab1c 3234
c906108c
SS
3235 while (cmd != NULL)
3236 {
3237 execute_control_command (cmd);
3238
3239 if (breakpoint_proceeded)
3240 break;
3241 else
3242 cmd = cmd->next;
3243 }
6c50ab1c
JB
3244
3245 /* We can free this command tree now. */
3246 do_cleanups (this_cmd_tree_chain);
3247
c906108c 3248 if (breakpoint_proceeded)
32c1e744
VP
3249 {
3250 if (target_can_async_p ())
347bddb7
PA
3251 /* If we are in async mode, then the target might be still
3252 running, not stopped at any breakpoint, so nothing for
3253 us to do here -- just return to the event loop. */
3254 ;
32c1e744
VP
3255 else
3256 /* In sync mode, when execute_control_command returns
3257 we're already standing on the next breakpoint.
347bddb7
PA
3258 Breakpoint commands for that stop were not run, since
3259 execute_command does not run breakpoint commands --
3260 only command_line_handler does, but that one is not
3261 involved in execution of breakpoint commands. So, we
3262 can now execute breakpoint commands. It should be
3263 noted that making execute_command do bpstat actions is
3264 not an option -- in this case we'll have recursive
3265 invocation of bpstat for each breakpoint with a
3266 command, and can easily blow up GDB stack. Instead, we
3267 return true, which will trigger the caller to recall us
3268 with the new stop_bpstat. */
3269 again = 1;
3270 break;
32c1e744 3271 }
c906108c 3272 }
c2b8ed2c 3273 do_cleanups (old_chain);
347bddb7
PA
3274 return again;
3275}
3276
3277void
3278bpstat_do_actions (void)
3279{
3280 /* Do any commands attached to breakpoint we are stopped at. */
3281 while (!ptid_equal (inferior_ptid, null_ptid)
3282 && target_has_execution
3283 && !is_exited (inferior_ptid)
3284 && !is_executing (inferior_ptid))
3285 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3286 and only return when it is stopped at the next breakpoint, we
3287 keep doing breakpoint actions until it returns false to
3288 indicate the inferior was not resumed. */
16c381f0 3289 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3290 break;
c906108c
SS
3291}
3292
fa4727a6
DJ
3293/* Print out the (old or new) value associated with a watchpoint. */
3294
3295static void
3296watchpoint_value_print (struct value *val, struct ui_file *stream)
3297{
3298 if (val == NULL)
3299 fprintf_unfiltered (stream, _("<unreadable>"));
3300 else
79a45b7d
TT
3301 {
3302 struct value_print_options opts;
3303 get_user_print_options (&opts);
3304 value_print (val, stream, &opts);
3305 }
fa4727a6
DJ
3306}
3307
e514a9d6 3308/* This is the normal print function for a bpstat. In the future,
c906108c 3309 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
3310 by having it set different print_it values.
3311
3312 Current scheme: When we stop, bpstat_print() is called. It loops
3313 through the bpstat list of things causing this stop, calling the
4a64f543 3314 print_bp_stop_message function on each one. The behavior of the
e514a9d6 3315 print_bp_stop_message function depends on the print_it field of
4a64f543 3316 bpstat. If such field so indicates, call this function here.
e514a9d6
JM
3317
3318 Return values from this routine (ultimately used by bpstat_print()
3319 and normal_stop() to decide what to do):
3320 PRINT_NOTHING: Means we already printed all we needed to print,
3321 don't print anything else.
3322 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
3323 that something to be followed by a location.
3324 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
3325 that something to be followed by a location.
3326 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
3327 analysis. */
c906108c 3328
917317f4 3329static enum print_stop_action
fba45db2 3330print_it_typical (bpstat bs)
c906108c 3331{
f7545552 3332 struct cleanup *old_chain;
4f8d1dc6 3333 struct breakpoint *b;
89f9893c 3334 const struct bp_location *bl;
8b93c638 3335 struct ui_stream *stb;
f7545552
TT
3336 int bp_temp = 0;
3337 enum print_stop_action result;
3338
f431efe5
PA
3339 gdb_assert (bs->bp_location_at != NULL);
3340
3341 bl = bs->bp_location_at;
3342 b = bs->breakpoint_at;
c906108c 3343
f7545552
TT
3344 stb = ui_out_stream_new (uiout);
3345 old_chain = make_cleanup_ui_out_stream_delete (stb);
3346
4f8d1dc6 3347 switch (b->type)
c906108c 3348 {
e514a9d6
JM
3349 case bp_breakpoint:
3350 case bp_hardware_breakpoint:
f431efe5 3351 bp_temp = b->disposition == disp_del;
0d381245
VP
3352 if (bl->address != bl->requested_address)
3353 breakpoint_adjustment_warning (bl->requested_address,
3354 bl->address,
4f8d1dc6
VP
3355 b->number, 1);
3356 annotate_breakpoint (b->number);
2cec12e5
AR
3357 if (bp_temp)
3358 ui_out_text (uiout, "\nTemporary breakpoint ");
3359 else
3360 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 3361 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
3362 {
3363 ui_out_field_string (uiout, "reason",
3364 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
3365 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3366 }
4f8d1dc6 3367 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 3368 ui_out_text (uiout, ", ");
f7545552 3369 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
3370 break;
3371
3372 case bp_shlib_event:
917317f4
JM
3373 /* Did we stop because the user set the stop_on_solib_events
3374 variable? (If so, we report this as a generic, "Stopped due
3375 to shlib event" message.) */
a3f17187 3376 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 3377 result = PRINT_NOTHING;
e514a9d6
JM
3378 break;
3379
c4093a6a 3380 case bp_thread_event:
4a64f543 3381 /* Not sure how we will get here.
c4093a6a 3382 GDB should not stop for these breakpoints. */
a3f17187 3383 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 3384 result = PRINT_NOTHING;
c4093a6a
JM
3385 break;
3386
1900040c 3387 case bp_overlay_event:
4a64f543 3388 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 3389 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 3390 result = PRINT_NOTHING;
1900040c
MS
3391 break;
3392
0fd8e87f
UW
3393 case bp_longjmp_master:
3394 /* These should never be enabled. */
3395 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
3396 result = PRINT_NOTHING;
3397 break;
3398
aa7d318d
TT
3399 case bp_std_terminate_master:
3400 /* These should never be enabled. */
3e43a32a
MS
3401 printf_filtered (_("std::terminate Master Breakpoint: "
3402 "gdb should not stop!\n"));
aa7d318d
TT
3403 result = PRINT_NOTHING;
3404 break;
3405
186c406b
TT
3406 case bp_exception_master:
3407 /* These should never be enabled. */
3e43a32a
MS
3408 printf_filtered (_("Exception Master Breakpoint: "
3409 "gdb should not stop!\n"));
186c406b
TT
3410 result = PRINT_NOTHING;
3411 break;
3412
e514a9d6
JM
3413 case bp_watchpoint:
3414 case bp_hardware_watchpoint:
fa4727a6
DJ
3415 annotate_watchpoint (b->number);
3416 if (ui_out_is_mi_like_p (uiout))
3417 ui_out_field_string
3418 (uiout, "reason",
3419 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
3420 mention (b);
f7545552 3421 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
3422 ui_out_text (uiout, "\nOld value = ");
3423 watchpoint_value_print (bs->old_val, stb->stream);
3424 ui_out_field_stream (uiout, "old", stb);
3425 ui_out_text (uiout, "\nNew value = ");
3426 watchpoint_value_print (b->val, stb->stream);
3427 ui_out_field_stream (uiout, "new", stb);
fa4727a6 3428 ui_out_text (uiout, "\n");
e514a9d6 3429 /* More than one watchpoint may have been triggered. */
f7545552 3430 result = PRINT_UNKNOWN;
e514a9d6
JM
3431 break;
3432
3433 case bp_read_watchpoint:
9dc5e2a9 3434 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3435 ui_out_field_string
3436 (uiout, "reason",
3437 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 3438 mention (b);
f7545552 3439 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3440 ui_out_text (uiout, "\nValue = ");
fa4727a6 3441 watchpoint_value_print (b->val, stb->stream);
8b93c638 3442 ui_out_field_stream (uiout, "value", stb);
8b93c638 3443 ui_out_text (uiout, "\n");
f7545552 3444 result = PRINT_UNKNOWN;
e514a9d6
JM
3445 break;
3446
3447 case bp_access_watchpoint:
fa4727a6 3448 if (bs->old_val != NULL)
8b93c638 3449 {
4f8d1dc6 3450 annotate_watchpoint (b->number);
9dc5e2a9 3451 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3452 ui_out_field_string
3453 (uiout, "reason",
3454 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 3455 mention (b);
f7545552 3456 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 3457 ui_out_text (uiout, "\nOld value = ");
fa4727a6 3458 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 3459 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
3460 ui_out_text (uiout, "\nNew value = ");
3461 }
3462 else
3463 {
4f8d1dc6 3464 mention (b);
9dc5e2a9 3465 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3466 ui_out_field_string
3467 (uiout, "reason",
3468 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 3469 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
3470 ui_out_text (uiout, "\nValue = ");
3471 }
fa4727a6 3472 watchpoint_value_print (b->val, stb->stream);
8b93c638 3473 ui_out_field_stream (uiout, "new", stb);
8b93c638 3474 ui_out_text (uiout, "\n");
f7545552 3475 result = PRINT_UNKNOWN;
e514a9d6 3476 break;
4ce44c66 3477
e514a9d6 3478 /* Fall through, we don't deal with these types of breakpoints
4a64f543 3479 here. */
e514a9d6 3480
11cf8741 3481 case bp_finish:
9dc5e2a9 3482 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3483 ui_out_field_string
3484 (uiout, "reason",
3485 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 3486 result = PRINT_UNKNOWN;
8b93c638
JM
3487 break;
3488
e514a9d6 3489 case bp_until:
9dc5e2a9 3490 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
3491 ui_out_field_string
3492 (uiout, "reason",
3493 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 3494 result = PRINT_UNKNOWN;
8b93c638
JM
3495 break;
3496
c2d11a7d 3497 case bp_none:
e514a9d6
JM
3498 case bp_longjmp:
3499 case bp_longjmp_resume:
186c406b
TT
3500 case bp_exception:
3501 case bp_exception_resume:
e514a9d6 3502 case bp_step_resume:
2c03e5be 3503 case bp_hp_step_resume:
e514a9d6
JM
3504 case bp_watchpoint_scope:
3505 case bp_call_dummy:
aa7d318d 3506 case bp_std_terminate:
1042e4c0 3507 case bp_tracepoint:
7a697b8d 3508 case bp_fast_tracepoint:
4efc6507 3509 case bp_jit_event:
0e30163f
JK
3510 case bp_gnu_ifunc_resolver:
3511 case bp_gnu_ifunc_resolver_return:
e514a9d6 3512 default:
f7545552
TT
3513 result = PRINT_UNKNOWN;
3514 break;
e514a9d6 3515 }
f7545552
TT
3516
3517 do_cleanups (old_chain);
3518 return result;
e514a9d6
JM
3519}
3520
3521/* Generic routine for printing messages indicating why we
4a64f543 3522 stopped. The behavior of this function depends on the value
e514a9d6
JM
3523 'print_it' in the bpstat structure. Under some circumstances we
3524 may decide not to print anything here and delegate the task to
4a64f543 3525 normal_stop(). */
e514a9d6
JM
3526
3527static enum print_stop_action
3528print_bp_stop_message (bpstat bs)
3529{
3530 switch (bs->print_it)
3531 {
3532 case print_it_noop:
4a64f543 3533 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3534 return PRINT_UNKNOWN;
3535 break;
3536
3537 case print_it_done:
3538 /* We still want to print the frame, but we already printed the
4a64f543 3539 relevant messages. */
e514a9d6
JM
3540 return PRINT_SRC_AND_LOC;
3541 break;
3542
3543 case print_it_normal:
4f8d1dc6 3544 {
f431efe5
PA
3545 struct breakpoint *b = bs->breakpoint_at;
3546
1a6a67de
TJB
3547 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3548 which has since been deleted. */
3549 if (b == NULL)
3550 return PRINT_UNKNOWN;
3551
4f8d1dc6
VP
3552 /* Normal case. Call the breakpoint's print_it method, or
3553 print_it_typical. */
1a6a67de 3554 if (b->ops != NULL && b->ops->print_it != NULL)
4f8d1dc6
VP
3555 return b->ops->print_it (b);
3556 else
3557 return print_it_typical (bs);
3558 }
3559 break;
3086aeae 3560
e514a9d6 3561 default:
8e65ff28 3562 internal_error (__FILE__, __LINE__,
e2e0b3e5 3563 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3564 break;
c906108c 3565 }
c906108c
SS
3566}
3567
e514a9d6
JM
3568/* Print a message indicating what happened. This is called from
3569 normal_stop(). The input to this routine is the head of the bpstat
3570 list - a list of the eventpoints that caused this stop. This
3571 routine calls the generic print routine for printing a message
3572 about reasons for stopping. This will print (for example) the
3573 "Breakpoint n," part of the output. The return value of this
3574 routine is one of:
c906108c 3575
4a64f543 3576 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3577 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3578 code to print the location. An example is
c5aa993b
JM
3579 "Breakpoint 1, " which should be followed by
3580 the location.
917317f4 3581 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3582 to also print the location part of the message.
3583 An example is the catch/throw messages, which
4a64f543 3584 don't require a location appended to the end.
917317f4 3585 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3586 further info to be printed. */
c906108c 3587
917317f4 3588enum print_stop_action
fba45db2 3589bpstat_print (bpstat bs)
c906108c
SS
3590{
3591 int val;
c5aa993b 3592
c906108c 3593 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3594 (Currently all watchpoints go on the bpstat whether hit or not.
3595 That probably could (should) be changed, provided care is taken
c906108c 3596 with respect to bpstat_explains_signal). */
e514a9d6
JM
3597 for (; bs; bs = bs->next)
3598 {
3599 val = print_bp_stop_message (bs);
3600 if (val == PRINT_SRC_ONLY
3601 || val == PRINT_SRC_AND_LOC
3602 || val == PRINT_NOTHING)
3603 return val;
3604 }
c906108c 3605
e514a9d6 3606 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3607 with and nothing was printed. */
917317f4 3608 return PRINT_UNKNOWN;
c906108c
SS
3609}
3610
4a64f543
MS
3611/* Evaluate the expression EXP and return 1 if value is zero. This is
3612 used inside a catch_errors to evaluate the breakpoint condition.
3613 The argument is a "struct expression *" that has been cast to a
3614 "char *" to make it pass through catch_errors. */
c906108c
SS
3615
3616static int
4efb68b1 3617breakpoint_cond_eval (void *exp)
c906108c 3618{
278cd55f 3619 struct value *mark = value_mark ();
c5aa993b 3620 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3621
c906108c
SS
3622 value_free_to_mark (mark);
3623 return i;
3624}
3625
5760d0ab 3626/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3627
3628static bpstat
5760d0ab 3629bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3630{
3631 bpstat bs;
3632
3633 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3634 bs->next = NULL;
3635 **bs_link_pointer = bs;
3636 *bs_link_pointer = &bs->next;
f431efe5
PA
3637 bs->breakpoint_at = bl->owner;
3638 bs->bp_location_at = bl;
3639 incref_bp_location (bl);
c906108c
SS
3640 /* If the condition is false, etc., don't do the commands. */
3641 bs->commands = NULL;
9add0f1b 3642 bs->commands_left = NULL;
c906108c
SS
3643 bs->old_val = NULL;
3644 bs->print_it = print_it_normal;
3645 return bs;
3646}
3647\f
d983da9c
DJ
3648/* The target has stopped with waitstatus WS. Check if any hardware
3649 watchpoints have triggered, according to the target. */
3650
3651int
3652watchpoints_triggered (struct target_waitstatus *ws)
3653{
d92524f1 3654 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3655 CORE_ADDR addr;
3656 struct breakpoint *b;
3657
3658 if (!stopped_by_watchpoint)
3659 {
3660 /* We were not stopped by a watchpoint. Mark all watchpoints
3661 as not triggered. */
3662 ALL_BREAKPOINTS (b)
cc60f2e3 3663 if (is_hardware_watchpoint (b))
d983da9c
DJ
3664 b->watchpoint_triggered = watch_triggered_no;
3665
3666 return 0;
3667 }
3668
3669 if (!target_stopped_data_address (&current_target, &addr))
3670 {
3671 /* We were stopped by a watchpoint, but we don't know where.
3672 Mark all watchpoints as unknown. */
3673 ALL_BREAKPOINTS (b)
cc60f2e3 3674 if (is_hardware_watchpoint (b))
d983da9c
DJ
3675 b->watchpoint_triggered = watch_triggered_unknown;
3676
3677 return stopped_by_watchpoint;
3678 }
3679
3680 /* The target could report the data address. Mark watchpoints
3681 affected by this data address as triggered, and all others as not
3682 triggered. */
3683
3684 ALL_BREAKPOINTS (b)
cc60f2e3 3685 if (is_hardware_watchpoint (b))
d983da9c 3686 {
a5606eee 3687 struct bp_location *loc;
d983da9c
DJ
3688
3689 b->watchpoint_triggered = watch_triggered_no;
a5606eee 3690 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4
TJB
3691 {
3692 if (is_masked_watchpoint (loc->owner))
3693 {
3694 CORE_ADDR newaddr = addr & loc->owner->hw_wp_mask;
3695 CORE_ADDR start = loc->address & loc->owner->hw_wp_mask;
3696
3697 if (newaddr == start)
3698 {
3699 b->watchpoint_triggered = watch_triggered_yes;
3700 break;
3701 }
3702 }
3703 /* Exact match not required. Within range is sufficient. */
3704 else if (target_watchpoint_addr_within_range (&current_target,
3705 addr, loc->address,
3706 loc->length))
3707 {
3708 b->watchpoint_triggered = watch_triggered_yes;
3709 break;
3710 }
3711 }
d983da9c
DJ
3712 }
3713
3714 return 1;
3715}
3716
c906108c
SS
3717/* Possible return values for watchpoint_check (this can't be an enum
3718 because of check_errors). */
3719/* The watchpoint has been deleted. */
3720#define WP_DELETED 1
3721/* The value has changed. */
3722#define WP_VALUE_CHANGED 2
3723/* The value has not changed. */
3724#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3725/* Ignore this watchpoint, no matter if the value changed or not. */
3726#define WP_IGNORE 4
c906108c
SS
3727
3728#define BP_TEMPFLAG 1
3729#define BP_HARDWAREFLAG 2
3730
4a64f543
MS
3731/* Evaluate watchpoint condition expression and check if its value
3732 changed.
553e4c11
JB
3733
3734 P should be a pointer to struct bpstat, but is defined as a void *
3735 in order for this function to be usable with catch_errors. */
c906108c
SS
3736
3737static int
4efb68b1 3738watchpoint_check (void *p)
c906108c
SS
3739{
3740 bpstat bs = (bpstat) p;
3741 struct breakpoint *b;
3742 struct frame_info *fr;
3743 int within_current_scope;
3744
f431efe5 3745 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3746 gdb_assert (bs->breakpoint_at != NULL);
f431efe5 3747 b = bs->breakpoint_at;
c906108c 3748
d0fb5eae
JK
3749 gdb_assert (is_watchpoint (b));
3750
f6bc2008
PA
3751 /* If this is a local watchpoint, we only want to check if the
3752 watchpoint frame is in scope if the current thread is the thread
3753 that was used to create the watchpoint. */
3754 if (!watchpoint_in_thread_scope (b))
60e1c644 3755 return WP_IGNORE;
f6bc2008 3756
c906108c
SS
3757 if (b->exp_valid_block == NULL)
3758 within_current_scope = 1;
3759 else
3760 {
edb3359d
DJ
3761 struct frame_info *frame = get_current_frame ();
3762 struct gdbarch *frame_arch = get_frame_arch (frame);
3763 CORE_ADDR frame_pc = get_frame_pc (frame);
3764
4a64f543
MS
3765 /* in_function_epilogue_p() returns a non-zero value if we're
3766 still in the function but the stack frame has already been
3767 invalidated. Since we can't rely on the values of local
3768 variables after the stack has been destroyed, we are treating
3769 the watchpoint in that state as `not changed' without further
3770 checking. Don't mark watchpoints as changed if the current
3771 frame is in an epilogue - even if they are in some other
3772 frame, our view of the stack is likely to be wrong and
3773 frame_find_by_id could error out. */
a0f49112 3774 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3775 return WP_IGNORE;
a0f49112 3776
101dcfbe 3777 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3778 within_current_scope = (fr != NULL);
69fbadd5
DJ
3779
3780 /* If we've gotten confused in the unwinder, we might have
3781 returned a frame that can't describe this variable. */
edb3359d
DJ
3782 if (within_current_scope)
3783 {
3784 struct symbol *function;
3785
3786 function = get_frame_function (fr);
3787 if (function == NULL
3788 || !contained_in (b->exp_valid_block,
3789 SYMBOL_BLOCK_VALUE (function)))
3790 within_current_scope = 0;
3791 }
69fbadd5 3792
edb3359d 3793 if (within_current_scope)
c906108c
SS
3794 /* If we end up stopping, the current frame will get selected
3795 in normal_stop. So this call to select_frame won't affect
3796 the user. */
0f7d239c 3797 select_frame (fr);
c906108c 3798 }
c5aa993b 3799
c906108c
SS
3800 if (within_current_scope)
3801 {
4a64f543
MS
3802 /* We use value_{,free_to_}mark because it could be a *long*
3803 time before we return to the command level and call
3804 free_all_values. We can't call free_all_values because we
3805 might be in the middle of evaluating a function call. */
c906108c 3806
0cf6dd15 3807 int pc = 0;
9c06b0b4 3808 struct value *mark;
fa4727a6
DJ
3809 struct value *new_val;
3810
9c06b0b4
TJB
3811 if (is_masked_watchpoint (b))
3812 /* Since we don't know the exact trigger address (from
3813 stopped_data_address), just tell the user we've triggered
3814 a mask watchpoint. */
3815 return WP_VALUE_CHANGED;
3816
3817 mark = value_mark ();
0cf6dd15 3818 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3819
4a64f543
MS
3820 /* We use value_equal_contents instead of value_equal because
3821 the latter coerces an array to a pointer, thus comparing just
3822 the address of the array instead of its contents. This is
3823 not what we want. */
fa4727a6 3824 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3825 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3826 {
fa4727a6
DJ
3827 if (new_val != NULL)
3828 {
3829 release_value (new_val);
3830 value_free_to_mark (mark);
3831 }
c906108c
SS
3832 bs->old_val = b->val;
3833 b->val = new_val;
fa4727a6 3834 b->val_valid = 1;
c906108c
SS
3835 return WP_VALUE_CHANGED;
3836 }
3837 else
3838 {
60e1c644 3839 /* Nothing changed. */
c906108c 3840 value_free_to_mark (mark);
c906108c
SS
3841 return WP_VALUE_NOT_CHANGED;
3842 }
3843 }
3844 else
3845 {
3846 /* This seems like the only logical thing to do because
c5aa993b
JM
3847 if we temporarily ignored the watchpoint, then when
3848 we reenter the block in which it is valid it contains
3849 garbage (in the case of a function, it may have two
3850 garbage values, one before and one after the prologue).
3851 So we can't even detect the first assignment to it and
3852 watch after that (since the garbage may or may not equal
3853 the first value assigned). */
4ce44c66
JM
3854 /* We print all the stop information in print_it_typical(), but
3855 in this case, by the time we call print_it_typical() this bp
4a64f543
MS
3856 will be deleted already. So we have no choice but print the
3857 information here. */
9dc5e2a9 3858 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3859 ui_out_field_string
3860 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3861 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 3862 ui_out_field_int (uiout, "wpnum", b->number);
3e43a32a
MS
3863 ui_out_text (uiout,
3864 " deleted because the program has left the block in\n\
8b93c638 3865which its expression is valid.\n");
4ce44c66 3866
cdac0397
PA
3867 /* Make sure the watchpoint's commands aren't executed. */
3868 decref_counted_command_line (&b->commands);
d0fb5eae 3869 watchpoint_del_at_next_stop (b);
c906108c
SS
3870
3871 return WP_DELETED;
3872 }
3873}
3874
18a18393
VP
3875/* Return true if it looks like target has stopped due to hitting
3876 breakpoint location BL. This function does not check if we
3877 should stop, only if BL explains the stop. */
3878static int
6c95b8df
PA
3879bpstat_check_location (const struct bp_location *bl,
3880 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3881{
3882 struct breakpoint *b = bl->owner;
3883
2bdf28a0
JK
3884 /* BL is from existing struct breakpoint. */
3885 gdb_assert (b != NULL);
3886
f1310107
TJB
3887 if (b->ops && b->ops->breakpoint_hit)
3888 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
3889
e8595ef6
SS
3890 /* By definition, the inferior does not report stops at
3891 tracepoints. */
d77f58be 3892 if (is_tracepoint (b))
e8595ef6
SS
3893 return 0;
3894
cc60f2e3 3895 if (!is_watchpoint (b)
18a18393 3896 && b->type != bp_hardware_breakpoint
fe798b75 3897 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393 3898 {
6c95b8df
PA
3899 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3900 aspace, bp_addr))
18a18393
VP
3901 return 0;
3902 if (overlay_debugging /* unmapped overlay section */
35df4500 3903 && section_is_overlay (bl->section)
18a18393
VP
3904 && !section_is_mapped (bl->section))
3905 return 0;
3906 }
cc60f2e3 3907
18a18393
VP
3908 /* Continuable hardware watchpoints are treated as non-existent if the
3909 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3910 some data address). Otherwise gdb won't stop on a break instruction
3911 in the code (not from a breakpoint) when a hardware watchpoint has
3912 been defined. Also skip watchpoints which we know did not trigger
3913 (did not match the data address). */
cc60f2e3
PA
3914
3915 if (is_hardware_watchpoint (b)
18a18393
VP
3916 && b->watchpoint_triggered == watch_triggered_no)
3917 return 0;
f1310107 3918
18a18393
VP
3919 if (b->type == bp_hardware_breakpoint)
3920 {
3921 if (bl->address != bp_addr)
3922 return 0;
3923 if (overlay_debugging /* unmapped overlay section */
35df4500 3924 && section_is_overlay (bl->section)
18a18393
VP
3925 && !section_is_mapped (bl->section))
3926 return 0;
3927 }
ce78b96d 3928
18a18393
VP
3929 return 1;
3930}
3931
3932/* If BS refers to a watchpoint, determine if the watched values
3933 has actually changed, and we should stop. If not, set BS->stop
3934 to 0. */
3935static void
3936bpstat_check_watchpoint (bpstat bs)
3937{
2bdf28a0
JK
3938 const struct bp_location *bl;
3939 struct breakpoint *b;
3940
3941 /* BS is built for existing struct breakpoint. */
f431efe5 3942 bl = bs->bp_location_at;
2bdf28a0 3943 gdb_assert (bl != NULL);
f431efe5 3944 b = bs->breakpoint_at;
2bdf28a0 3945 gdb_assert (b != NULL);
18a18393 3946
cc60f2e3 3947 if (is_watchpoint (b))
18a18393 3948 {
18a18393
VP
3949 int must_check_value = 0;
3950
3951 if (b->type == bp_watchpoint)
3952 /* For a software watchpoint, we must always check the
3953 watched value. */
3954 must_check_value = 1;
3955 else if (b->watchpoint_triggered == watch_triggered_yes)
3956 /* We have a hardware watchpoint (read, write, or access)
3957 and the target earlier reported an address watched by
3958 this watchpoint. */
3959 must_check_value = 1;
3960 else if (b->watchpoint_triggered == watch_triggered_unknown
3961 && b->type == bp_hardware_watchpoint)
3962 /* We were stopped by a hardware watchpoint, but the target could
3963 not report the data address. We must check the watchpoint's
3964 value. Access and read watchpoints are out of luck; without
3965 a data address, we can't figure it out. */
3966 must_check_value = 1;
3967
3968 if (must_check_value)
3969 {
3e43a32a
MS
3970 char *message
3971 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3972 b->number);
18a18393
VP
3973 struct cleanup *cleanups = make_cleanup (xfree, message);
3974 int e = catch_errors (watchpoint_check, bs, message,
3975 RETURN_MASK_ALL);
3976 do_cleanups (cleanups);
3977 switch (e)
3978 {
3979 case WP_DELETED:
3980 /* We've already printed what needs to be printed. */
3981 bs->print_it = print_it_done;
3982 /* Stop. */
3983 break;
60e1c644
PA
3984 case WP_IGNORE:
3985 bs->print_it = print_it_noop;
3986 bs->stop = 0;
3987 break;
18a18393
VP
3988 case WP_VALUE_CHANGED:
3989 if (b->type == bp_read_watchpoint)
3990 {
85d721b8
PA
3991 /* There are two cases to consider here:
3992
4a64f543 3993 1. We're watching the triggered memory for reads.
85d721b8
PA
3994 In that case, trust the target, and always report
3995 the watchpoint hit to the user. Even though
3996 reads don't cause value changes, the value may
3997 have changed since the last time it was read, and
3998 since we're not trapping writes, we will not see
3999 those, and as such we should ignore our notion of
4000 old value.
4001
4a64f543 4002 2. We're watching the triggered memory for both
85d721b8
PA
4003 reads and writes. There are two ways this may
4004 happen:
4005
4a64f543 4006 2.1. This is a target that can't break on data
85d721b8
PA
4007 reads only, but can break on accesses (reads or
4008 writes), such as e.g., x86. We detect this case
4009 at the time we try to insert read watchpoints.
4010
4a64f543 4011 2.2. Otherwise, the target supports read
85d721b8
PA
4012 watchpoints, but, the user set an access or write
4013 watchpoint watching the same memory as this read
4014 watchpoint.
4015
4016 If we're watching memory writes as well as reads,
4017 ignore watchpoint hits when we find that the
4018 value hasn't changed, as reads don't cause
4019 changes. This still gives false positives when
4020 the program writes the same value to memory as
4021 what there was already in memory (we will confuse
4022 it for a read), but it's much better than
4023 nothing. */
4024
4025 int other_write_watchpoint = 0;
4026
4027 if (bl->watchpoint_type == hw_read)
4028 {
4029 struct breakpoint *other_b;
4030
4031 ALL_BREAKPOINTS (other_b)
4032 if ((other_b->type == bp_hardware_watchpoint
4033 || other_b->type == bp_access_watchpoint)
4034 && (other_b->watchpoint_triggered
4035 == watch_triggered_yes))
4036 {
4037 other_write_watchpoint = 1;
4038 break;
4039 }
4040 }
4041
4042 if (other_write_watchpoint
4043 || bl->watchpoint_type == hw_access)
4044 {
4045 /* We're watching the same memory for writes,
4046 and the value changed since the last time we
4047 updated it, so this trap must be for a write.
4048 Ignore it. */
4049 bs->print_it = print_it_noop;
4050 bs->stop = 0;
4051 }
18a18393
VP
4052 }
4053 break;
4054 case WP_VALUE_NOT_CHANGED:
4055 if (b->type == bp_hardware_watchpoint
4056 || b->type == bp_watchpoint)
4057 {
4058 /* Don't stop: write watchpoints shouldn't fire if
4059 the value hasn't changed. */
4060 bs->print_it = print_it_noop;
4061 bs->stop = 0;
4062 }
4063 /* Stop. */
4064 break;
4065 default:
4066 /* Can't happen. */
4067 case 0:
4068 /* Error from catch_errors. */
4069 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
d0fb5eae 4070 watchpoint_del_at_next_stop (b);
18a18393
VP
4071 /* We've already printed what needs to be printed. */
4072 bs->print_it = print_it_done;
4073 break;
4074 }
4075 }
4076 else /* must_check_value == 0 */
4077 {
4078 /* This is a case where some watchpoint(s) triggered, but
4079 not at the address of this watchpoint, or else no
4080 watchpoint triggered after all. So don't print
4081 anything for this watchpoint. */
4082 bs->print_it = print_it_noop;
4083 bs->stop = 0;
4084 }
4085 }
4086}
4087
4088
4089/* Check conditions (condition proper, frame, thread and ignore count)
4090 of breakpoint referred to by BS. If we should not stop for this
4091 breakpoint, set BS->stop to 0. */
f431efe5 4092
18a18393
VP
4093static void
4094bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4095{
4096 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4097 const struct bp_location *bl;
4098 struct breakpoint *b;
4099
4100 /* BS is built for existing struct breakpoint. */
f431efe5 4101 bl = bs->bp_location_at;
2bdf28a0 4102 gdb_assert (bl != NULL);
f431efe5 4103 b = bs->breakpoint_at;
2bdf28a0 4104 gdb_assert (b != NULL);
18a18393
VP
4105
4106 if (frame_id_p (b->frame_id)
edb3359d 4107 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4108 bs->stop = 0;
4109 else if (bs->stop)
4110 {
4111 int value_is_zero = 0;
60e1c644
PA
4112 struct expression *cond;
4113
7371cf6d
PM
4114 /* Evaluate Python breakpoints that have a "stop"
4115 method implemented. */
4116 if (b->py_bp_object)
4117 bs->stop = gdbpy_should_stop (b->py_bp_object);
4118
60e1c644
PA
4119 if (is_watchpoint (b))
4120 cond = b->cond_exp;
4121 else
4122 cond = bl->cond;
4123
f431efe5 4124 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4125 {
60e1c644
PA
4126 int within_current_scope = 1;
4127
c5bc3a77
DJ
4128 /* We use value_mark and value_free_to_mark because it could
4129 be a long time before we return to the command level and
4130 call free_all_values. We can't call free_all_values
4131 because we might be in the middle of evaluating a
4132 function call. */
4133 struct value *mark = value_mark ();
4134
edb3359d
DJ
4135 /* Need to select the frame, with all that implies so that
4136 the conditions will have the right context. Because we
4137 use the frame, we will not see an inlined function's
4138 variables when we arrive at a breakpoint at the start
4139 of the inlined function; the current frame will be the
4140 call site. */
60e1c644
PA
4141 if (!is_watchpoint (b) || b->cond_exp_valid_block == NULL)
4142 select_frame (get_current_frame ());
4143 else
4144 {
4145 struct frame_info *frame;
4146
4147 /* For local watchpoint expressions, which particular
4148 instance of a local is being watched matters, so we
4149 keep track of the frame to evaluate the expression
4150 in. To evaluate the condition however, it doesn't
4151 really matter which instantiation of the function
4152 where the condition makes sense triggers the
4153 watchpoint. This allows an expression like "watch
4154 global if q > 10" set in `func', catch writes to
4155 global on all threads that call `func', or catch
4156 writes on all recursive calls of `func' by a single
4157 thread. We simply always evaluate the condition in
4158 the innermost frame that's executing where it makes
4159 sense to evaluate the condition. It seems
4160 intuitive. */
4161 frame = block_innermost_frame (b->cond_exp_valid_block);
4162 if (frame != NULL)
4163 select_frame (frame);
4164 else
4165 within_current_scope = 0;
4166 }
4167 if (within_current_scope)
4168 value_is_zero
4169 = catch_errors (breakpoint_cond_eval, cond,
4170 "Error in testing breakpoint condition:\n",
4171 RETURN_MASK_ALL);
4172 else
4173 {
4174 warning (_("Watchpoint condition cannot be tested "
4175 "in the current scope"));
4176 /* If we failed to set the right context for this
4177 watchpoint, unconditionally report it. */
4178 value_is_zero = 0;
4179 }
4a64f543 4180 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4181 value_free_to_mark (mark);
18a18393 4182 }
60e1c644
PA
4183
4184 if (cond && value_is_zero)
18a18393
VP
4185 {
4186 bs->stop = 0;
4187 }
4188 else if (b->thread != -1 && b->thread != thread_id)
4189 {
4190 bs->stop = 0;
4191 }
4192 else if (b->ignore_count > 0)
4193 {
4194 b->ignore_count--;
4195 annotate_ignore_count_change ();
4196 bs->stop = 0;
4a64f543 4197 /* Increase the hit count even though we don't stop. */
18a18393 4198 ++(b->hit_count);
8d3788bd 4199 observer_notify_breakpoint_modified (b);
18a18393
VP
4200 }
4201 }
4202}
4203
4204
9709f61c 4205/* Get a bpstat associated with having just stopped at address
d983da9c 4206 BP_ADDR in thread PTID.
c906108c 4207
d983da9c 4208 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4209 don't understand this stop. Result is a chain of bpstat's such
4210 that:
c906108c 4211
c5aa993b 4212 if we don't understand the stop, the result is a null pointer.
c906108c 4213
c5aa993b 4214 if we understand why we stopped, the result is not null.
c906108c 4215
c5aa993b
JM
4216 Each element of the chain refers to a particular breakpoint or
4217 watchpoint at which we have stopped. (We may have stopped for
4218 several reasons concurrently.)
c906108c 4219
c5aa993b
JM
4220 Each element of the chain has valid next, breakpoint_at,
4221 commands, FIXME??? fields. */
c906108c
SS
4222
4223bpstat
6c95b8df
PA
4224bpstat_stop_status (struct address_space *aspace,
4225 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4226{
0d381245 4227 struct breakpoint *b = NULL;
afe38095 4228 struct bp_location *bl;
20874c92 4229 struct bp_location *loc;
5760d0ab
JK
4230 /* First item of allocated bpstat's. */
4231 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4232 /* Pointer to the last thing in the chain currently. */
5760d0ab 4233 bpstat bs;
20874c92 4234 int ix;
429374b8 4235 int need_remove_insert;
f431efe5 4236 int removed_any;
c906108c 4237
f431efe5
PA
4238 /* First, build the bpstat chain with locations that explain a
4239 target stop, while being careful to not set the target running,
4240 as that may invalidate locations (in particular watchpoint
4241 locations are recreated). Resuming will happen here with
4242 breakpoint conditions or watchpoint expressions that include
4243 inferior function calls. */
c5aa993b 4244
429374b8
JK
4245 ALL_BREAKPOINTS (b)
4246 {
4247 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4248 continue;
a5606eee 4249
429374b8
JK
4250 for (bl = b->loc; bl != NULL; bl = bl->next)
4251 {
4a64f543
MS
4252 /* For hardware watchpoints, we look only at the first
4253 location. The watchpoint_check function will work on the
4254 entire expression, not the individual locations. For
4255 read watchpoints, the watchpoints_triggered function has
4256 checked all locations already. */
429374b8
JK
4257 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4258 break;
18a18393 4259
429374b8
JK
4260 if (bl->shlib_disabled)
4261 continue;
c5aa993b 4262
429374b8
JK
4263 if (!bpstat_check_location (bl, aspace, bp_addr))
4264 continue;
c5aa993b 4265
4a64f543
MS
4266 /* Come here if it's a watchpoint, or if the break address
4267 matches. */
c5aa993b 4268
4a64f543
MS
4269 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4270 explain stop. */
c5aa993b 4271
f431efe5
PA
4272 /* Assume we stop. Should we find a watchpoint that is not
4273 actually triggered, or if the condition of the breakpoint
4274 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4275 bs->stop = 1;
4276 bs->print = 1;
d983da9c 4277
f431efe5
PA
4278 /* If this is a scope breakpoint, mark the associated
4279 watchpoint as triggered so that we will handle the
4280 out-of-scope event. We'll get to the watchpoint next
4281 iteration. */
d0fb5eae 4282 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
f431efe5
PA
4283 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
4284 }
4285 }
4286
4287 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4288 {
f1310107 4289 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4290 {
5760d0ab 4291 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4292 /* For hits of moribund locations, we should just proceed. */
4293 bs->stop = 0;
4294 bs->print = 0;
4295 bs->print_it = print_it_noop;
4296 }
4297 }
4298
f431efe5
PA
4299 /* Now go through the locations that caused the target to stop, and
4300 check whether we're interested in reporting this stop to higher
4301 layers, or whether we should resume the target transparently. */
4302
4303 removed_any = 0;
4304
5760d0ab 4305 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4306 {
4307 if (!bs->stop)
4308 continue;
4309
4310 bpstat_check_watchpoint (bs);
4311 if (!bs->stop)
4312 continue;
4313
4314 b = bs->breakpoint_at;
18a18393 4315
28010a5d
PA
4316 if (b->ops != NULL && b->ops->check_status != NULL)
4317 {
4318 b->ops->check_status (bs);
4319 if (!bs->stop)
4320 continue;
4321 }
4322
429374b8 4323 if (b->type == bp_thread_event || b->type == bp_overlay_event
aa7d318d 4324 || b->type == bp_longjmp_master
186c406b
TT
4325 || b->type == bp_std_terminate_master
4326 || b->type == bp_exception_master)
429374b8
JK
4327 /* We do not stop for these. */
4328 bs->stop = 0;
4329 else
4330 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4331
429374b8
JK
4332 if (bs->stop)
4333 {
4334 ++(b->hit_count);
8d3788bd 4335 observer_notify_breakpoint_modified (b);
c906108c 4336
4a64f543 4337 /* We will stop here. */
429374b8
JK
4338 if (b->disposition == disp_disable)
4339 {
4340 if (b->enable_state != bp_permanent)
4341 b->enable_state = bp_disabled;
f431efe5 4342 removed_any = 1;
429374b8
JK
4343 }
4344 if (b->silent)
4345 bs->print = 0;
4346 bs->commands = b->commands;
9add0f1b
TT
4347 incref_counted_command_line (bs->commands);
4348 bs->commands_left = bs->commands ? bs->commands->commands : NULL;
4349 if (bs->commands_left
4350 && (strcmp ("silent", bs->commands_left->line) == 0
4351 || (xdb_commands
4352 && strcmp ("Q",
4353 bs->commands_left->line) == 0)))
429374b8 4354 {
9add0f1b 4355 bs->commands_left = bs->commands_left->next;
429374b8
JK
4356 bs->print = 0;
4357 }
429374b8
JK
4358 }
4359
4360 /* Print nothing for this entry if we dont stop or dont print. */
4361 if (bs->stop == 0 || bs->print == 0)
4362 bs->print_it = print_it_noop;
429374b8 4363 }
876fa593 4364
d983da9c
DJ
4365 /* If we aren't stopping, the value of some hardware watchpoint may
4366 not have changed, but the intermediate memory locations we are
4367 watching may have. Don't bother if we're stopping; this will get
4368 done later. */
d832cb68 4369 need_remove_insert = 0;
5760d0ab
JK
4370 if (! bpstat_causes_stop (bs_head))
4371 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4372 if (!bs->stop
f431efe5
PA
4373 && bs->breakpoint_at
4374 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4375 {
4a64f543 4376 update_watchpoint (bs->breakpoint_at, 0 /* don't reparse. */);
d832cb68 4377 need_remove_insert = 1;
d983da9c
DJ
4378 }
4379
d832cb68 4380 if (need_remove_insert)
2d134ed3 4381 update_global_location_list (1);
f431efe5
PA
4382 else if (removed_any)
4383 update_global_location_list (0);
d832cb68 4384
5760d0ab 4385 return bs_head;
c906108c 4386}
628fe4e4
JK
4387
4388static void
4389handle_jit_event (void)
4390{
4391 struct frame_info *frame;
4392 struct gdbarch *gdbarch;
4393
4394 /* Switch terminal for any messages produced by
4395 breakpoint_re_set. */
4396 target_terminal_ours_for_output ();
4397
4398 frame = get_current_frame ();
4399 gdbarch = get_frame_arch (frame);
4400
4401 jit_event_handler (gdbarch);
4402
4403 target_terminal_inferior ();
4404}
4405
4406/* Prepare WHAT final decision for infrun. */
4407
4408/* Decide what infrun needs to do with this bpstat. */
4409
c906108c 4410struct bpstat_what
0e30163f 4411bpstat_what (bpstat bs_head)
c906108c 4412{
c906108c 4413 struct bpstat_what retval;
628fe4e4
JK
4414 /* We need to defer calling `solib_add', as adding new symbols
4415 resets breakpoints, which in turn deletes breakpoint locations,
4416 and hence may clear unprocessed entries in the BS chain. */
4417 int shlib_event = 0;
4418 int jit_event = 0;
0e30163f 4419 bpstat bs;
c906108c 4420
628fe4e4 4421 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4422 retval.call_dummy = STOP_NONE;
186c406b 4423 retval.is_longjmp = 0;
628fe4e4 4424
0e30163f 4425 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4426 {
628fe4e4
JK
4427 /* Extract this BS's action. After processing each BS, we check
4428 if its action overrides all we've seem so far. */
4429 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4430 enum bptype bptype;
4431
c906108c 4432 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4433 {
4434 /* I suspect this can happen if it was a momentary
4435 breakpoint which has since been deleted. */
4436 bptype = bp_none;
4437 }
f431efe5 4438 else if (bs->breakpoint_at == NULL)
628fe4e4 4439 bptype = bp_none;
20874c92 4440 else
f431efe5 4441 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4442
4443 switch (bptype)
c906108c
SS
4444 {
4445 case bp_none:
628fe4e4 4446 break;
c906108c
SS
4447 case bp_breakpoint:
4448 case bp_hardware_breakpoint:
4449 case bp_until:
4450 case bp_finish:
4451 if (bs->stop)
4452 {
4453 if (bs->print)
628fe4e4 4454 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4455 else
628fe4e4 4456 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4457 }
4458 else
628fe4e4 4459 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4460 break;
4461 case bp_watchpoint:
4462 case bp_hardware_watchpoint:
4463 case bp_read_watchpoint:
4464 case bp_access_watchpoint:
4465 if (bs->stop)
4466 {
4467 if (bs->print)
628fe4e4 4468 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4469 else
628fe4e4 4470 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4471 }
4472 else
628fe4e4
JK
4473 {
4474 /* There was a watchpoint, but we're not stopping.
4475 This requires no further action. */
4476 }
c906108c
SS
4477 break;
4478 case bp_longjmp:
186c406b 4479 case bp_exception:
628fe4e4 4480 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4481 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4482 break;
4483 case bp_longjmp_resume:
186c406b 4484 case bp_exception_resume:
628fe4e4 4485 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4486 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4487 break;
4488 case bp_step_resume:
4489 if (bs->stop)
628fe4e4
JK
4490 this_action = BPSTAT_WHAT_STEP_RESUME;
4491 else
c906108c 4492 {
628fe4e4
JK
4493 /* It is for the wrong frame. */
4494 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4495 }
c906108c 4496 break;
2c03e5be
PA
4497 case bp_hp_step_resume:
4498 if (bs->stop)
4499 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4500 else
4501 {
4502 /* It is for the wrong frame. */
4503 this_action = BPSTAT_WHAT_SINGLE;
4504 }
4505 break;
c906108c 4506 case bp_watchpoint_scope:
c4093a6a 4507 case bp_thread_event:
1900040c 4508 case bp_overlay_event:
0fd8e87f 4509 case bp_longjmp_master:
aa7d318d 4510 case bp_std_terminate_master:
186c406b 4511 case bp_exception_master:
628fe4e4 4512 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4513 break;
ce78b96d 4514 case bp_catchpoint:
c5aa993b
JM
4515 if (bs->stop)
4516 {
4517 if (bs->print)
628fe4e4 4518 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4519 else
628fe4e4 4520 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4521 }
4522 else
628fe4e4
JK
4523 {
4524 /* There was a catchpoint, but we're not stopping.
4525 This requires no further action. */
4526 }
4527 break;
4528 case bp_shlib_event:
4529 shlib_event = 1;
4530
4531 /* If requested, stop when the dynamic linker notifies GDB
4532 of events. This allows the user to get control and place
4533 breakpoints in initializer routines for dynamically
4534 loaded objects (among other things). */
4535 if (stop_on_solib_events)
4536 this_action = BPSTAT_WHAT_STOP_NOISY;
4537 else
4538 this_action = BPSTAT_WHAT_SINGLE;
4539 break;
4540 case bp_jit_event:
4541 jit_event = 1;
4542 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4543 break;
c906108c 4544 case bp_call_dummy:
53a5351d
JM
4545 /* Make sure the action is stop (silent or noisy),
4546 so infrun.c pops the dummy frame. */
aa7d318d 4547 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4548 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4549 break;
4550 case bp_std_terminate:
4551 /* Make sure the action is stop (silent or noisy),
4552 so infrun.c pops the dummy frame. */
aa7d318d 4553 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4554 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4555 break;
1042e4c0 4556 case bp_tracepoint:
7a697b8d 4557 case bp_fast_tracepoint:
0fb4aa4b 4558 case bp_static_tracepoint:
1042e4c0
SS
4559 /* Tracepoint hits should not be reported back to GDB, and
4560 if one got through somehow, it should have been filtered
4561 out already. */
4562 internal_error (__FILE__, __LINE__,
7a697b8d 4563 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4564 break;
4565 case bp_gnu_ifunc_resolver:
4566 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4567 this_action = BPSTAT_WHAT_SINGLE;
4568 break;
4569 case bp_gnu_ifunc_resolver_return:
4570 /* The breakpoint will be removed, execution will restart from the
4571 PC of the former breakpoint. */
4572 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4573 break;
628fe4e4
JK
4574 default:
4575 internal_error (__FILE__, __LINE__,
4576 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4577 }
628fe4e4
JK
4578
4579 retval.main_action = max (retval.main_action, this_action);
c906108c 4580 }
628fe4e4 4581
0e30163f
JK
4582 /* These operations may affect the bs->breakpoint_at state so they are
4583 delayed after MAIN_ACTION is decided above. */
4584
628fe4e4
JK
4585 if (shlib_event)
4586 {
4587 if (debug_infrun)
4588 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4589
4590 /* Check for any newly added shared libraries if we're supposed
4591 to be adding them automatically. */
4592
4593 /* Switch terminal for any messages produced by
4594 breakpoint_re_set. */
4595 target_terminal_ours_for_output ();
4596
4597#ifdef SOLIB_ADD
4598 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4599#else
4600 solib_add (NULL, 0, &current_target, auto_solib_add);
4601#endif
4602
4603 target_terminal_inferior ();
4604 }
4605
4606 if (jit_event)
4607 {
4608 if (debug_infrun)
4609 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4610
4611 handle_jit_event ();
4612 }
4613
0e30163f
JK
4614 for (bs = bs_head; bs != NULL; bs = bs->next)
4615 {
4616 struct breakpoint *b = bs->breakpoint_at;
4617
4618 if (b == NULL)
4619 continue;
4620 switch (b->type)
4621 {
4622 case bp_gnu_ifunc_resolver:
4623 gnu_ifunc_resolver_stop (b);
4624 break;
4625 case bp_gnu_ifunc_resolver_return:
4626 gnu_ifunc_resolver_return_stop (b);
4627 break;
4628 }
4629 }
4630
c906108c
SS
4631 return retval;
4632}
4633
4634/* Nonzero if we should step constantly (e.g. watchpoints on machines
4635 without hardware support). This isn't related to a specific bpstat,
4636 just to things like whether watchpoints are set. */
4637
c5aa993b 4638int
fba45db2 4639bpstat_should_step (void)
c906108c
SS
4640{
4641 struct breakpoint *b;
cc59ec59 4642
c906108c 4643 ALL_BREAKPOINTS (b)
717a8278 4644 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4645 return 1;
c906108c
SS
4646 return 0;
4647}
4648
67822962
PA
4649int
4650bpstat_causes_stop (bpstat bs)
4651{
4652 for (; bs != NULL; bs = bs->next)
4653 if (bs->stop)
4654 return 1;
4655
4656 return 0;
4657}
4658
c906108c 4659\f
c5aa993b 4660
170b53b2
UW
4661/* Compute a string of spaces suitable to indent the next line
4662 so it starts at the position corresponding to the table column
4663 named COL_NAME in the currently active table of UIOUT. */
4664
4665static char *
4666wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4667{
4668 static char wrap_indent[80];
4669 int i, total_width, width, align;
4670 char *text;
4671
4672 total_width = 0;
4673 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4674 {
4675 if (strcmp (text, col_name) == 0)
4676 {
4677 gdb_assert (total_width < sizeof wrap_indent);
4678 memset (wrap_indent, ' ', total_width);
4679 wrap_indent[total_width] = 0;
4680
4681 return wrap_indent;
4682 }
4683
4684 total_width += width + 1;
4685 }
4686
4687 return NULL;
4688}
4689
859825b8
JK
4690/* Print the LOC location out of the list of B->LOC locations. */
4691
170b53b2
UW
4692static void
4693print_breakpoint_location (struct breakpoint *b,
4694 struct bp_location *loc)
0d381245 4695{
6c95b8df
PA
4696 struct cleanup *old_chain = save_current_program_space ();
4697
859825b8
JK
4698 if (loc != NULL && loc->shlib_disabled)
4699 loc = NULL;
4700
6c95b8df
PA
4701 if (loc != NULL)
4702 set_current_program_space (loc->pspace);
4703
56435ebe
TT
4704 if (b->display_canonical)
4705 ui_out_field_string (uiout, "what", b->addr_string);
4706 else if (b->source_file && loc)
0d381245
VP
4707 {
4708 struct symbol *sym
4709 = find_pc_sect_function (loc->address, loc->section);
4710 if (sym)
4711 {
4712 ui_out_text (uiout, "in ");
4713 ui_out_field_string (uiout, "func",
4714 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4715 ui_out_text (uiout, " ");
4716 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4717 ui_out_text (uiout, "at ");
0d381245
VP
4718 }
4719 ui_out_field_string (uiout, "file", b->source_file);
4720 ui_out_text (uiout, ":");
4721
4722 if (ui_out_is_mi_like_p (uiout))
4723 {
4724 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4725 char *fullname = symtab_to_fullname (sal.symtab);
4726
4727 if (fullname)
4728 ui_out_field_string (uiout, "fullname", fullname);
4729 }
4730
4731 ui_out_field_int (uiout, "line", b->line_number);
4732 }
859825b8 4733 else if (loc)
0d381245 4734 {
170b53b2
UW
4735 struct ui_stream *stb = ui_out_stream_new (uiout);
4736 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4737
22e722e1
DJ
4738 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4739 demangle, "");
0d381245 4740 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4741
4742 do_cleanups (stb_chain);
0d381245 4743 }
859825b8
JK
4744 else
4745 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4746
4747 do_cleanups (old_chain);
0d381245
VP
4748}
4749
269b11a2
PA
4750static const char *
4751bptype_string (enum bptype type)
c906108c 4752{
c4093a6a
JM
4753 struct ep_type_description
4754 {
4755 enum bptype type;
4756 char *description;
4757 };
4758 static struct ep_type_description bptypes[] =
c906108c 4759 {
c5aa993b
JM
4760 {bp_none, "?deleted?"},
4761 {bp_breakpoint, "breakpoint"},
c906108c 4762 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4763 {bp_until, "until"},
4764 {bp_finish, "finish"},
4765 {bp_watchpoint, "watchpoint"},
c906108c 4766 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4767 {bp_read_watchpoint, "read watchpoint"},
4768 {bp_access_watchpoint, "acc watchpoint"},
4769 {bp_longjmp, "longjmp"},
4770 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4771 {bp_exception, "exception"},
4772 {bp_exception_resume, "exception resume"},
c5aa993b 4773 {bp_step_resume, "step resume"},
2c03e5be 4774 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4775 {bp_watchpoint_scope, "watchpoint scope"},
4776 {bp_call_dummy, "call dummy"},
aa7d318d 4777 {bp_std_terminate, "std::terminate"},
c5aa993b 4778 {bp_shlib_event, "shlib events"},
c4093a6a 4779 {bp_thread_event, "thread events"},
1900040c 4780 {bp_overlay_event, "overlay events"},
0fd8e87f 4781 {bp_longjmp_master, "longjmp master"},
aa7d318d 4782 {bp_std_terminate_master, "std::terminate master"},
186c406b 4783 {bp_exception_master, "exception master"},
ce78b96d 4784 {bp_catchpoint, "catchpoint"},
1042e4c0 4785 {bp_tracepoint, "tracepoint"},
7a697b8d 4786 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4787 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4788 {bp_jit_event, "jit events"},
0e30163f
JK
4789 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4790 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4791 };
269b11a2
PA
4792
4793 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4794 || ((int) type != bptypes[(int) type].type))
4795 internal_error (__FILE__, __LINE__,
4796 _("bptypes table does not describe type #%d."),
4797 (int) type);
4798
4799 return bptypes[(int) type].description;
4800}
4801
4802/* Print B to gdb_stdout. */
4803
4804static void
4805print_one_breakpoint_location (struct breakpoint *b,
4806 struct bp_location *loc,
4807 int loc_number,
4808 struct bp_location **last_loc,
269b11a2
PA
4809 int allflag)
4810{
4811 struct command_line *l;
c2c6d25f 4812 static char bpenables[] = "nynny";
c906108c 4813
0d381245
VP
4814 int header_of_multiple = 0;
4815 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4816 struct value_print_options opts;
4817
4818 get_user_print_options (&opts);
0d381245
VP
4819
4820 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4821 /* See comment in print_one_breakpoint concerning treatment of
4822 breakpoints with single disabled location. */
0d381245
VP
4823 if (loc == NULL
4824 && (b->loc != NULL
4825 && (b->loc->next != NULL || !b->loc->enabled)))
4826 header_of_multiple = 1;
4827 if (loc == NULL)
4828 loc = b->loc;
4829
c4093a6a
JM
4830 annotate_record ();
4831
4832 /* 1 */
4833 annotate_field (0);
0d381245
VP
4834 if (part_of_multiple)
4835 {
4836 char *formatted;
0c6773c1 4837 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4838 ui_out_field_string (uiout, "number", formatted);
4839 xfree (formatted);
4840 }
4841 else
4842 {
4843 ui_out_field_int (uiout, "number", b->number);
4844 }
c4093a6a
JM
4845
4846 /* 2 */
4847 annotate_field (1);
0d381245
VP
4848 if (part_of_multiple)
4849 ui_out_field_skip (uiout, "type");
269b11a2
PA
4850 else
4851 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4852
4853 /* 3 */
4854 annotate_field (2);
0d381245
VP
4855 if (part_of_multiple)
4856 ui_out_field_skip (uiout, "disp");
4857 else
2cec12e5 4858 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4859
c4093a6a
JM
4860
4861 /* 4 */
4862 annotate_field (3);
0d381245 4863 if (part_of_multiple)
54e52265 4864 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4865 else
4a64f543
MS
4866 ui_out_field_fmt (uiout, "enabled", "%c",
4867 bpenables[(int) b->enable_state]);
54e52265 4868 ui_out_spaces (uiout, 2);
0d381245 4869
c4093a6a
JM
4870
4871 /* 5 and 6 */
3086aeae 4872 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4873 {
4a64f543
MS
4874 /* Although the print_one can possibly print all locations,
4875 calling it here is not likely to get any nice result. So,
4876 make sure there's just one location. */
0d381245 4877 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4878 b->ops->print_one (b, last_loc);
0d381245 4879 }
3086aeae
DJ
4880 else
4881 switch (b->type)
4882 {
4883 case bp_none:
4884 internal_error (__FILE__, __LINE__,
e2e0b3e5 4885 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4886 break;
c906108c 4887
3086aeae
DJ
4888 case bp_watchpoint:
4889 case bp_hardware_watchpoint:
4890 case bp_read_watchpoint:
4891 case bp_access_watchpoint:
4892 /* Field 4, the address, is omitted (which makes the columns
4893 not line up too nicely with the headers, but the effect
4894 is relatively readable). */
79a45b7d 4895 if (opts.addressprint)
3086aeae
DJ
4896 ui_out_field_skip (uiout, "addr");
4897 annotate_field (5);
fa8a61dc 4898 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
4899 break;
4900
3086aeae
DJ
4901 case bp_breakpoint:
4902 case bp_hardware_breakpoint:
4903 case bp_until:
4904 case bp_finish:
4905 case bp_longjmp:
4906 case bp_longjmp_resume:
186c406b
TT
4907 case bp_exception:
4908 case bp_exception_resume:
3086aeae 4909 case bp_step_resume:
2c03e5be 4910 case bp_hp_step_resume:
3086aeae
DJ
4911 case bp_watchpoint_scope:
4912 case bp_call_dummy:
aa7d318d 4913 case bp_std_terminate:
3086aeae
DJ
4914 case bp_shlib_event:
4915 case bp_thread_event:
4916 case bp_overlay_event:
0fd8e87f 4917 case bp_longjmp_master:
aa7d318d 4918 case bp_std_terminate_master:
186c406b 4919 case bp_exception_master:
1042e4c0 4920 case bp_tracepoint:
7a697b8d 4921 case bp_fast_tracepoint:
0fb4aa4b 4922 case bp_static_tracepoint:
4efc6507 4923 case bp_jit_event:
0e30163f
JK
4924 case bp_gnu_ifunc_resolver:
4925 case bp_gnu_ifunc_resolver_return:
79a45b7d 4926 if (opts.addressprint)
3086aeae
DJ
4927 {
4928 annotate_field (4);
54e52265 4929 if (header_of_multiple)
0d381245 4930 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4931 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4932 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4933 else
5af949e3
UW
4934 ui_out_field_core_addr (uiout, "addr",
4935 loc->gdbarch, loc->address);
3086aeae
DJ
4936 }
4937 annotate_field (5);
0d381245 4938 if (!header_of_multiple)
170b53b2 4939 print_breakpoint_location (b, loc);
0d381245 4940 if (b->loc)
a6d9a66e 4941 *last_loc = b->loc;
3086aeae
DJ
4942 break;
4943 }
c906108c 4944
6c95b8df
PA
4945
4946 /* For backward compatibility, don't display inferiors unless there
4947 are several. */
4948 if (loc != NULL
4949 && !header_of_multiple
4950 && (allflag
4951 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4952 && (number_of_program_spaces () > 1
4953 || number_of_inferiors () > 1)
4a64f543
MS
4954 /* LOC is for existing B, it cannot be in
4955 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4956 && loc->owner->type != bp_catchpoint)))
4957 {
4958 struct inferior *inf;
4959 int first = 1;
4960
4961 for (inf = inferior_list; inf != NULL; inf = inf->next)
4962 {
4963 if (inf->pspace == loc->pspace)
4964 {
4965 if (first)
4966 {
4967 first = 0;
4968 ui_out_text (uiout, " inf ");
4969 }
4970 else
4971 ui_out_text (uiout, ", ");
4972 ui_out_text (uiout, plongest (inf->num));
4973 }
4974 }
4975 }
4976
4a306c9a 4977 if (!part_of_multiple)
c4093a6a 4978 {
4a306c9a
JB
4979 if (b->thread != -1)
4980 {
4981 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4982 "stop only in" line a little further down. */
4a306c9a
JB
4983 ui_out_text (uiout, " thread ");
4984 ui_out_field_int (uiout, "thread", b->thread);
4985 }
4986 else if (b->task != 0)
4987 {
4988 ui_out_text (uiout, " task ");
4989 ui_out_field_int (uiout, "task", b->task);
4990 }
c4093a6a 4991 }
f1310107 4992
8b93c638 4993 ui_out_text (uiout, "\n");
f1310107
TJB
4994
4995 if (!part_of_multiple && b->ops && b->ops->print_one_detail)
4996 b->ops->print_one_detail (b, uiout);
4997
0fb4aa4b
PA
4998 if (!part_of_multiple && b->static_trace_marker_id)
4999 {
5000 gdb_assert (b->type == bp_static_tracepoint);
5001
5002 ui_out_text (uiout, "\tmarker id is ");
5003 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
5004 b->static_trace_marker_id);
5005 ui_out_text (uiout, "\n");
5006 }
5007
0d381245 5008 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5009 {
5010 annotate_field (6);
8b93c638 5011 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
5012 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
5013 the frame ID. */
5af949e3
UW
5014 ui_out_field_core_addr (uiout, "frame",
5015 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5016 ui_out_text (uiout, "\n");
c4093a6a
JM
5017 }
5018
28010a5d 5019 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5020 {
5021 annotate_field (7);
d77f58be 5022 if (is_tracepoint (b))
1042e4c0
SS
5023 ui_out_text (uiout, "\ttrace only if ");
5024 else
5025 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
5026 ui_out_field_string (uiout, "cond", b->cond_string);
5027 ui_out_text (uiout, "\n");
5028 }
5029
0d381245 5030 if (!part_of_multiple && b->thread != -1)
c4093a6a 5031 {
4a64f543 5032 /* FIXME should make an annotation for this. */
8b93c638
JM
5033 ui_out_text (uiout, "\tstop only in thread ");
5034 ui_out_field_int (uiout, "thread", b->thread);
5035 ui_out_text (uiout, "\n");
c4093a6a
JM
5036 }
5037
63c715c6 5038 if (!part_of_multiple && b->hit_count)
c4093a6a 5039 {
4a64f543 5040 /* FIXME should make an annotation for this. */
8b93c638
JM
5041 if (ep_is_catchpoint (b))
5042 ui_out_text (uiout, "\tcatchpoint");
5043 else
5044 ui_out_text (uiout, "\tbreakpoint");
5045 ui_out_text (uiout, " already hit ");
5046 ui_out_field_int (uiout, "times", b->hit_count);
5047 if (b->hit_count == 1)
5048 ui_out_text (uiout, " time\n");
5049 else
5050 ui_out_text (uiout, " times\n");
c4093a6a
JM
5051 }
5052
4a64f543
MS
5053 /* Output the count also if it is zero, but only if this is mi.
5054 FIXME: Should have a better test for this. */
9dc5e2a9 5055 if (ui_out_is_mi_like_p (uiout))
63c715c6 5056 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5057 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5058
0d381245 5059 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5060 {
5061 annotate_field (8);
8b93c638
JM
5062 ui_out_text (uiout, "\tignore next ");
5063 ui_out_field_int (uiout, "ignore", b->ignore_count);
5064 ui_out_text (uiout, " hits\n");
c4093a6a 5065 }
059fb39f 5066
9add0f1b 5067 l = b->commands ? b->commands->commands : NULL;
059fb39f 5068 if (!part_of_multiple && l)
c4093a6a 5069 {
3b31d625
EZ
5070 struct cleanup *script_chain;
5071
c4093a6a 5072 annotate_field (9);
3b31d625 5073 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5074 print_command_lines (uiout, l, 4);
3b31d625 5075 do_cleanups (script_chain);
c4093a6a 5076 }
d24317b4 5077
1042e4c0
SS
5078 if (!part_of_multiple && b->pass_count)
5079 {
5080 annotate_field (10);
5081 ui_out_text (uiout, "\tpass count ");
5082 ui_out_field_int (uiout, "pass", b->pass_count);
5083 ui_out_text (uiout, " \n");
5084 }
5085
d24317b4
VP
5086 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5087 {
5088 if (b->addr_string)
5089 ui_out_field_string (uiout, "original-location", b->addr_string);
5090 else if (b->exp_string)
5091 ui_out_field_string (uiout, "original-location", b->exp_string);
5092 }
c4093a6a 5093}
c5aa993b 5094
0d381245
VP
5095static void
5096print_one_breakpoint (struct breakpoint *b,
4a64f543 5097 struct bp_location **last_loc,
6c95b8df 5098 int allflag)
0d381245 5099{
8d3788bd
VP
5100 struct cleanup *bkpt_chain;
5101
5102 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5103
12c5a436 5104 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5105 do_cleanups (bkpt_chain);
0d381245
VP
5106
5107 /* If this breakpoint has custom print function,
5108 it's already printed. Otherwise, print individual
5109 locations, if any. */
5110 if (b->ops == NULL || b->ops->print_one == NULL)
5111 {
4a64f543
MS
5112 /* If breakpoint has a single location that is disabled, we
5113 print it as if it had several locations, since otherwise it's
5114 hard to represent "breakpoint enabled, location disabled"
5115 situation.
5116
5117 Note that while hardware watchpoints have several locations
a3be7890 5118 internally, that's not a property exposed to user. */
0d381245 5119 if (b->loc
a5606eee 5120 && !is_hardware_watchpoint (b)
8d3788bd 5121 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5122 {
5123 struct bp_location *loc;
5124 int n = 1;
8d3788bd 5125
0d381245 5126 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5127 {
5128 struct cleanup *inner2 =
5129 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5130 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5131 do_cleanups (inner2);
5132 }
0d381245
VP
5133 }
5134 }
5135}
5136
a6d9a66e
UW
5137static int
5138breakpoint_address_bits (struct breakpoint *b)
5139{
5140 int print_address_bits = 0;
5141 struct bp_location *loc;
5142
5143 for (loc = b->loc; loc; loc = loc->next)
5144 {
c7437ca6
PA
5145 int addr_bit;
5146
5147 /* Software watchpoints that aren't watching memory don't have
5148 an address to print. */
5149 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5150 continue;
5151
5152 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5153 if (addr_bit > print_address_bits)
5154 print_address_bits = addr_bit;
5155 }
5156
5157 return print_address_bits;
5158}
0d381245 5159
c4093a6a
JM
5160struct captured_breakpoint_query_args
5161 {
5162 int bnum;
5163 };
c5aa993b 5164
c4093a6a 5165static int
2b65245e 5166do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5167{
5168 struct captured_breakpoint_query_args *args = data;
52f0bd74 5169 struct breakpoint *b;
a6d9a66e 5170 struct bp_location *dummy_loc = NULL;
cc59ec59 5171
c4093a6a
JM
5172 ALL_BREAKPOINTS (b)
5173 {
5174 if (args->bnum == b->number)
c5aa993b 5175 {
12c5a436 5176 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5177 return GDB_RC_OK;
c5aa993b 5178 }
c4093a6a
JM
5179 }
5180 return GDB_RC_NONE;
5181}
c5aa993b 5182
c4093a6a 5183enum gdb_rc
4a64f543
MS
5184gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5185 char **error_message)
c4093a6a
JM
5186{
5187 struct captured_breakpoint_query_args args;
cc59ec59 5188
c4093a6a
JM
5189 args.bnum = bnum;
5190 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5191 an error. */
b0b13bb4
DJ
5192 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5193 error_message, RETURN_MASK_ALL) < 0)
5194 return GDB_RC_FAIL;
5195 else
5196 return GDB_RC_OK;
c4093a6a 5197}
c5aa993b 5198
09d682a4
TT
5199/* Return true if this breakpoint was set by the user, false if it is
5200 internal or momentary. */
5201
5202int
5203user_breakpoint_p (struct breakpoint *b)
5204{
46c6471b 5205 return b->number > 0;
09d682a4
TT
5206}
5207
7f3b0473 5208/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5209 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5210 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5211 FILTER is non-NULL, call it on each breakpoint and only include the
5212 ones for which it returns non-zero. Return the total number of
5213 breakpoints listed. */
c906108c 5214
d77f58be 5215static int
e5a67952 5216breakpoint_1 (char *args, int allflag,
4a64f543 5217 int (*filter) (const struct breakpoint *))
c4093a6a 5218{
52f0bd74 5219 struct breakpoint *b;
a6d9a66e 5220 struct bp_location *last_loc = NULL;
7f3b0473 5221 int nr_printable_breakpoints;
3b31d625 5222 struct cleanup *bkpttbl_chain;
79a45b7d 5223 struct value_print_options opts;
a6d9a66e 5224 int print_address_bits = 0;
269b11a2
PA
5225 int print_type_col_width = 14;
5226
79a45b7d
TT
5227 get_user_print_options (&opts);
5228
4a64f543
MS
5229 /* Compute the number of rows in the table, as well as the size
5230 required for address fields. */
7f3b0473
AC
5231 nr_printable_breakpoints = 0;
5232 ALL_BREAKPOINTS (b)
e5a67952
MS
5233 {
5234 /* If we have a filter, only list the breakpoints it accepts. */
5235 if (filter && !filter (b))
5236 continue;
5237
5238 /* If we have an "args" string, it is a list of breakpoints to
5239 accept. Skip the others. */
5240 if (args != NULL && *args != '\0')
5241 {
5242 if (allflag && parse_and_eval_long (args) != b->number)
5243 continue;
5244 if (!allflag && !number_is_in_list (args, b->number))
5245 continue;
5246 }
269b11a2 5247
e5a67952
MS
5248 if (allflag || user_breakpoint_p (b))
5249 {
5250 int addr_bit, type_len;
a6d9a66e 5251
e5a67952
MS
5252 addr_bit = breakpoint_address_bits (b);
5253 if (addr_bit > print_address_bits)
5254 print_address_bits = addr_bit;
269b11a2 5255
e5a67952
MS
5256 type_len = strlen (bptype_string (b->type));
5257 if (type_len > print_type_col_width)
5258 print_type_col_width = type_len;
5259
5260 nr_printable_breakpoints++;
5261 }
5262 }
7f3b0473 5263
79a45b7d 5264 if (opts.addressprint)
3b31d625 5265 bkpttbl_chain
3e43a32a
MS
5266 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5267 nr_printable_breakpoints,
3b31d625 5268 "BreakpointTable");
8b93c638 5269 else
3b31d625 5270 bkpttbl_chain
3e43a32a
MS
5271 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5272 nr_printable_breakpoints,
3b31d625 5273 "BreakpointTable");
8b93c638 5274
7f3b0473 5275 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5276 annotate_breakpoints_headers ();
5277 if (nr_printable_breakpoints > 0)
5278 annotate_field (0);
4a64f543 5279 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5280 if (nr_printable_breakpoints > 0)
5281 annotate_field (1);
269b11a2 5282 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5283 "type", "Type"); /* 2 */
d7faa9e7
AC
5284 if (nr_printable_breakpoints > 0)
5285 annotate_field (2);
4a64f543 5286 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5287 if (nr_printable_breakpoints > 0)
5288 annotate_field (3);
54e52265 5289 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5290 if (opts.addressprint)
e5a67952
MS
5291 {
5292 if (nr_printable_breakpoints > 0)
5293 annotate_field (4);
5294 if (print_address_bits <= 32)
5295 ui_out_table_header (uiout, 10, ui_left,
5296 "addr", "Address"); /* 5 */
5297 else
5298 ui_out_table_header (uiout, 18, ui_left,
5299 "addr", "Address"); /* 5 */
5300 }
d7faa9e7
AC
5301 if (nr_printable_breakpoints > 0)
5302 annotate_field (5);
5303 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5304 ui_out_table_body (uiout);
5305 if (nr_printable_breakpoints > 0)
5306 annotate_breakpoints_table ();
7f3b0473 5307
c4093a6a 5308 ALL_BREAKPOINTS (b)
e5a67952
MS
5309 {
5310 QUIT;
5311 /* If we have a filter, only list the breakpoints it accepts. */
5312 if (filter && !filter (b))
5313 continue;
5314
5315 /* If we have an "args" string, it is a list of breakpoints to
5316 accept. Skip the others. */
5317
5318 if (args != NULL && *args != '\0')
5319 {
5320 if (allflag) /* maintenance info breakpoint */
5321 {
5322 if (parse_and_eval_long (args) != b->number)
5323 continue;
5324 }
5325 else /* all others */
5326 {
5327 if (!number_is_in_list (args, b->number))
5328 continue;
5329 }
5330 }
5331 /* We only print out user settable breakpoints unless the
5332 allflag is set. */
5333 if (allflag || user_breakpoint_p (b))
12c5a436 5334 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5335 }
5336
3b31d625 5337 do_cleanups (bkpttbl_chain);
698384cd 5338
7f3b0473 5339 if (nr_printable_breakpoints == 0)
c906108c 5340 {
4a64f543
MS
5341 /* If there's a filter, let the caller decide how to report
5342 empty list. */
d77f58be
SS
5343 if (!filter)
5344 {
e5a67952 5345 if (args == NULL || *args == '\0')
d77f58be
SS
5346 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5347 else
4a64f543 5348 ui_out_message (uiout, 0,
e5a67952
MS
5349 "No breakpoint or watchpoint matching '%s'.\n",
5350 args);
d77f58be 5351 }
c906108c
SS
5352 }
5353 else
c4093a6a 5354 {
a6d9a66e
UW
5355 if (last_loc && !server_command)
5356 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5357 }
c906108c 5358
4a64f543 5359 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5360 there have been breakpoints? */
c906108c 5361 annotate_breakpoints_table_end ();
d77f58be
SS
5362
5363 return nr_printable_breakpoints;
c906108c
SS
5364}
5365
ad443146
SS
5366/* Display the value of default-collect in a way that is generally
5367 compatible with the breakpoint list. */
5368
5369static void
5370default_collect_info (void)
5371{
5372 /* If it has no value (which is frequently the case), say nothing; a
5373 message like "No default-collect." gets in user's face when it's
5374 not wanted. */
5375 if (!*default_collect)
5376 return;
5377
5378 /* The following phrase lines up nicely with per-tracepoint collect
5379 actions. */
5380 ui_out_text (uiout, "default collect ");
5381 ui_out_field_string (uiout, "default-collect", default_collect);
5382 ui_out_text (uiout, " \n");
5383}
5384
c906108c 5385static void
e5a67952 5386breakpoints_info (char *args, int from_tty)
c906108c 5387{
e5a67952 5388 breakpoint_1 (args, 0, NULL);
ad443146
SS
5389
5390 default_collect_info ();
d77f58be
SS
5391}
5392
5393static void
e5a67952 5394watchpoints_info (char *args, int from_tty)
d77f58be 5395{
e5a67952 5396 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
d77f58be
SS
5397
5398 if (num_printed == 0)
5399 {
e5a67952 5400 if (args == NULL || *args == '\0')
d77f58be
SS
5401 ui_out_message (uiout, 0, "No watchpoints.\n");
5402 else
e5a67952 5403 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5404 }
c906108c
SS
5405}
5406
7a292a7a 5407static void
e5a67952 5408maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5409{
e5a67952 5410 breakpoint_1 (args, 1, NULL);
ad443146
SS
5411
5412 default_collect_info ();
c906108c
SS
5413}
5414
0d381245 5415static int
714835d5 5416breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5417 struct program_space *pspace,
714835d5 5418 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5419{
5420 struct bp_location *bl = b->loc;
cc59ec59 5421
0d381245
VP
5422 for (; bl; bl = bl->next)
5423 {
6c95b8df
PA
5424 if (bl->pspace == pspace
5425 && bl->address == pc
0d381245
VP
5426 && (!overlay_debugging || bl->section == section))
5427 return 1;
5428 }
5429 return 0;
5430}
5431
6c95b8df
PA
5432/* Print a message describing any breakpoints set at PC. This
5433 concerns with logical breakpoints, so we match program spaces, not
5434 address spaces. */
c906108c
SS
5435
5436static void
6c95b8df
PA
5437describe_other_breakpoints (struct gdbarch *gdbarch,
5438 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5439 struct obj_section *section, int thread)
c906108c 5440{
52f0bd74
AC
5441 int others = 0;
5442 struct breakpoint *b;
c906108c
SS
5443
5444 ALL_BREAKPOINTS (b)
6c95b8df 5445 others += breakpoint_has_pc (b, pspace, pc, section);
c906108c
SS
5446 if (others > 0)
5447 {
a3f17187
AC
5448 if (others == 1)
5449 printf_filtered (_("Note: breakpoint "));
5450 else /* if (others == ???) */
5451 printf_filtered (_("Note: breakpoints "));
c906108c 5452 ALL_BREAKPOINTS (b)
6c95b8df 5453 if (breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5454 {
5455 others--;
5456 printf_filtered ("%d", b->number);
5457 if (b->thread == -1 && thread != -1)
5458 printf_filtered (" (all threads)");
5459 else if (b->thread != -1)
5460 printf_filtered (" (thread %d)", b->thread);
5461 printf_filtered ("%s%s ",
059fb39f 5462 ((b->enable_state == bp_disabled
8bea4e01
UW
5463 || b->enable_state == bp_call_disabled
5464 || b->enable_state == bp_startup_disabled)
0d381245
VP
5465 ? " (disabled)"
5466 : b->enable_state == bp_permanent
5467 ? " (permanent)"
5468 : ""),
5469 (others > 1) ? ","
5470 : ((others == 1) ? " and" : ""));
5471 }
a3f17187 5472 printf_filtered (_("also set at pc "));
5af949e3 5473 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5474 printf_filtered (".\n");
5475 }
5476}
5477\f
5478/* Set the default place to put a breakpoint
5479 for the `break' command with no arguments. */
5480
5481void
6c95b8df
PA
5482set_default_breakpoint (int valid, struct program_space *pspace,
5483 CORE_ADDR addr, struct symtab *symtab,
fba45db2 5484 int line)
c906108c
SS
5485{
5486 default_breakpoint_valid = valid;
6c95b8df 5487 default_breakpoint_pspace = pspace;
c906108c
SS
5488 default_breakpoint_address = addr;
5489 default_breakpoint_symtab = symtab;
5490 default_breakpoint_line = line;
5491}
5492
e4f237da
KB
5493/* Return true iff it is meaningful to use the address member of
5494 BPT. For some breakpoint types, the address member is irrelevant
5495 and it makes no sense to attempt to compare it to other addresses
5496 (or use it for any other purpose either).
5497
4a64f543
MS
5498 More specifically, each of the following breakpoint types will
5499 always have a zero valued address and we don't want to mark
5500 breakpoints of any of these types to be a duplicate of an actual
5501 breakpoint at address zero:
e4f237da
KB
5502
5503 bp_watchpoint
2d134ed3
PA
5504 bp_catchpoint
5505
5506*/
e4f237da
KB
5507
5508static int
5509breakpoint_address_is_meaningful (struct breakpoint *bpt)
5510{
5511 enum bptype type = bpt->type;
5512
2d134ed3
PA
5513 return (type != bp_watchpoint && type != bp_catchpoint);
5514}
5515
5516/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5517 true if LOC1 and LOC2 represent the same watchpoint location. */
5518
5519static int
4a64f543
MS
5520watchpoint_locations_match (struct bp_location *loc1,
5521 struct bp_location *loc2)
2d134ed3 5522{
2bdf28a0
JK
5523 /* Both of them must not be in moribund_locations. */
5524 gdb_assert (loc1->owner != NULL);
5525 gdb_assert (loc2->owner != NULL);
5526
4a64f543
MS
5527 /* If the target can evaluate the condition expression in hardware,
5528 then we we need to insert both watchpoints even if they are at
5529 the same place. Otherwise the watchpoint will only trigger when
5530 the condition of whichever watchpoint was inserted evaluates to
5531 true, not giving a chance for GDB to check the condition of the
5532 other watchpoint. */
0cf6dd15 5533 if ((loc1->owner->cond_exp
4a64f543
MS
5534 && target_can_accel_watchpoint_condition (loc1->address,
5535 loc1->length,
0cf6dd15
TJB
5536 loc1->watchpoint_type,
5537 loc1->owner->cond_exp))
5538 || (loc2->owner->cond_exp
4a64f543
MS
5539 && target_can_accel_watchpoint_condition (loc2->address,
5540 loc2->length,
0cf6dd15
TJB
5541 loc2->watchpoint_type,
5542 loc2->owner->cond_exp)))
5543 return 0;
5544
85d721b8
PA
5545 /* Note that this checks the owner's type, not the location's. In
5546 case the target does not support read watchpoints, but does
5547 support access watchpoints, we'll have bp_read_watchpoint
5548 watchpoints with hw_access locations. Those should be considered
5549 duplicates of hw_read locations. The hw_read locations will
5550 become hw_access locations later. */
2d134ed3
PA
5551 return (loc1->owner->type == loc2->owner->type
5552 && loc1->pspace->aspace == loc2->pspace->aspace
5553 && loc1->address == loc2->address
5554 && loc1->length == loc2->length);
e4f237da
KB
5555}
5556
6c95b8df
PA
5557/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5558 same breakpoint location. In most targets, this can only be true
5559 if ASPACE1 matches ASPACE2. On targets that have global
5560 breakpoints, the address space doesn't really matter. */
5561
5562static int
5563breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5564 struct address_space *aspace2, CORE_ADDR addr2)
5565{
5566 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5567 || aspace1 == aspace2)
5568 && addr1 == addr2);
5569}
5570
f1310107
TJB
5571/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5572 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5573 matches ASPACE2. On targets that have global breakpoints, the address
5574 space doesn't really matter. */
5575
5576static int
5577breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5578 int len1, struct address_space *aspace2,
5579 CORE_ADDR addr2)
5580{
5581 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5582 || aspace1 == aspace2)
5583 && addr2 >= addr1 && addr2 < addr1 + len1);
5584}
5585
5586/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5587 a ranged breakpoint. In most targets, a match happens only if ASPACE
5588 matches the breakpoint's address space. On targets that have global
5589 breakpoints, the address space doesn't really matter. */
5590
5591static int
5592breakpoint_location_address_match (struct bp_location *bl,
5593 struct address_space *aspace,
5594 CORE_ADDR addr)
5595{
5596 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5597 aspace, addr)
5598 || (bl->length
5599 && breakpoint_address_match_range (bl->pspace->aspace,
5600 bl->address, bl->length,
5601 aspace, addr)));
5602}
5603
2d134ed3
PA
5604/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5605 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5606 represent the same location. */
5607
5608static int
4a64f543
MS
5609breakpoint_locations_match (struct bp_location *loc1,
5610 struct bp_location *loc2)
2d134ed3 5611{
2bdf28a0
JK
5612 int hw_point1, hw_point2;
5613
5614 /* Both of them must not be in moribund_locations. */
5615 gdb_assert (loc1->owner != NULL);
5616 gdb_assert (loc2->owner != NULL);
5617
5618 hw_point1 = is_hardware_watchpoint (loc1->owner);
5619 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5620
5621 if (hw_point1 != hw_point2)
5622 return 0;
5623 else if (hw_point1)
5624 return watchpoint_locations_match (loc1, loc2);
5625 else
f1310107
TJB
5626 /* We compare bp_location.length in order to cover ranged breakpoints. */
5627 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5628 loc2->pspace->aspace, loc2->address)
5629 && loc1->length == loc2->length);
2d134ed3
PA
5630}
5631
76897487
KB
5632static void
5633breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5634 int bnum, int have_bnum)
5635{
f63fbe86
MS
5636 /* The longest string possibly returned by hex_string_custom
5637 is 50 chars. These must be at least that big for safety. */
5638 char astr1[64];
5639 char astr2[64];
76897487 5640
bb599908
PH
5641 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5642 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5643 if (have_bnum)
8a3fe4f8 5644 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5645 bnum, astr1, astr2);
5646 else
8a3fe4f8 5647 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5648}
5649
4a64f543
MS
5650/* Adjust a breakpoint's address to account for architectural
5651 constraints on breakpoint placement. Return the adjusted address.
5652 Note: Very few targets require this kind of adjustment. For most
5653 targets, this function is simply the identity function. */
76897487
KB
5654
5655static CORE_ADDR
a6d9a66e
UW
5656adjust_breakpoint_address (struct gdbarch *gdbarch,
5657 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5658{
a6d9a66e 5659 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5660 {
5661 /* Very few targets need any kind of breakpoint adjustment. */
5662 return bpaddr;
5663 }
88f7da05
KB
5664 else if (bptype == bp_watchpoint
5665 || bptype == bp_hardware_watchpoint
5666 || bptype == bp_read_watchpoint
5667 || bptype == bp_access_watchpoint
fe798b75 5668 || bptype == bp_catchpoint)
88f7da05
KB
5669 {
5670 /* Watchpoints and the various bp_catch_* eventpoints should not
5671 have their addresses modified. */
5672 return bpaddr;
5673 }
76897487
KB
5674 else
5675 {
5676 CORE_ADDR adjusted_bpaddr;
5677
5678 /* Some targets have architectural constraints on the placement
5679 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5680 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5681
5682 /* An adjusted breakpoint address can significantly alter
5683 a user's expectations. Print a warning if an adjustment
5684 is required. */
5685 if (adjusted_bpaddr != bpaddr)
5686 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5687
5688 return adjusted_bpaddr;
5689 }
5690}
5691
28010a5d
PA
5692void
5693init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5694 struct breakpoint *owner)
7cc221ef 5695{
7cc221ef
DJ
5696 memset (loc, 0, sizeof (*loc));
5697
28010a5d
PA
5698 loc->ops = ops;
5699 loc->owner = owner;
511a6cd4 5700 loc->cond = NULL;
0d381245
VP
5701 loc->shlib_disabled = 0;
5702 loc->enabled = 1;
e049a4b5 5703
28010a5d 5704 switch (owner->type)
e049a4b5
DJ
5705 {
5706 case bp_breakpoint:
5707 case bp_until:
5708 case bp_finish:
5709 case bp_longjmp:
5710 case bp_longjmp_resume:
186c406b
TT
5711 case bp_exception:
5712 case bp_exception_resume:
e049a4b5 5713 case bp_step_resume:
2c03e5be 5714 case bp_hp_step_resume:
e049a4b5
DJ
5715 case bp_watchpoint_scope:
5716 case bp_call_dummy:
aa7d318d 5717 case bp_std_terminate:
e049a4b5
DJ
5718 case bp_shlib_event:
5719 case bp_thread_event:
5720 case bp_overlay_event:
4efc6507 5721 case bp_jit_event:
0fd8e87f 5722 case bp_longjmp_master:
aa7d318d 5723 case bp_std_terminate_master:
186c406b 5724 case bp_exception_master:
0e30163f
JK
5725 case bp_gnu_ifunc_resolver:
5726 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5727 loc->loc_type = bp_loc_software_breakpoint;
5728 break;
5729 case bp_hardware_breakpoint:
5730 loc->loc_type = bp_loc_hardware_breakpoint;
5731 break;
5732 case bp_hardware_watchpoint:
5733 case bp_read_watchpoint:
5734 case bp_access_watchpoint:
5735 loc->loc_type = bp_loc_hardware_watchpoint;
5736 break;
5737 case bp_watchpoint:
ce78b96d 5738 case bp_catchpoint:
15c3d785
PA
5739 case bp_tracepoint:
5740 case bp_fast_tracepoint:
0fb4aa4b 5741 case bp_static_tracepoint:
e049a4b5
DJ
5742 loc->loc_type = bp_loc_other;
5743 break;
5744 default:
e2e0b3e5 5745 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5746 }
5747
f431efe5 5748 loc->refc = 1;
28010a5d
PA
5749}
5750
5751/* Allocate a struct bp_location. */
5752
5753static struct bp_location *
5754allocate_bp_location (struct breakpoint *bpt)
5755{
5756 struct bp_location *loc;
5757
5758 if (bpt->ops && bpt->ops->allocate_location)
5759 return bpt->ops->allocate_location (bpt);
5760
5761 loc = xmalloc (sizeof (struct bp_location));
5762 init_bp_location (loc, NULL, bpt);
7cc221ef
DJ
5763 return loc;
5764}
5765
f431efe5
PA
5766static void
5767free_bp_location (struct bp_location *loc)
fe3f5fa8 5768{
28010a5d
PA
5769 if (loc->ops && loc->ops->dtor)
5770 loc->ops->dtor (loc);
5771
fe3f5fa8
VP
5772 if (loc->cond)
5773 xfree (loc->cond);
74960c60
VP
5774
5775 if (loc->function_name)
5776 xfree (loc->function_name);
f431efe5 5777
fe3f5fa8
VP
5778 xfree (loc);
5779}
5780
f431efe5
PA
5781/* Increment reference count. */
5782
5783static void
5784incref_bp_location (struct bp_location *bl)
5785{
5786 ++bl->refc;
5787}
5788
5789/* Decrement reference count. If the reference count reaches 0,
5790 destroy the bp_location. Sets *BLP to NULL. */
5791
5792static void
5793decref_bp_location (struct bp_location **blp)
5794{
0807b50c
PA
5795 gdb_assert ((*blp)->refc > 0);
5796
f431efe5
PA
5797 if (--(*blp)->refc == 0)
5798 free_bp_location (*blp);
5799 *blp = NULL;
5800}
5801
346774a9 5802/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5803
346774a9
PA
5804static void
5805add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5806{
346774a9 5807 struct breakpoint *b1;
c906108c 5808
346774a9
PA
5809 /* Add this breakpoint to the end of the chain so that a list of
5810 breakpoints will come out in order of increasing numbers. */
5811
5812 b1 = breakpoint_chain;
5813 if (b1 == 0)
5814 breakpoint_chain = b;
5815 else
5816 {
5817 while (b1->next)
5818 b1 = b1->next;
5819 b1->next = b;
5820 }
5821}
5822
5823/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5824
5825static void
5826init_raw_breakpoint_without_location (struct breakpoint *b,
5827 struct gdbarch *gdbarch,
28010a5d
PA
5828 enum bptype bptype,
5829 struct breakpoint_ops *ops)
346774a9 5830{
c906108c 5831 memset (b, 0, sizeof (*b));
2219d63c 5832
28010a5d 5833 b->ops = ops;
4d28f7a8 5834 b->type = bptype;
a6d9a66e 5835 b->gdbarch = gdbarch;
c906108c
SS
5836 b->language = current_language->la_language;
5837 b->input_radix = input_radix;
5838 b->thread = -1;
b5de0fa7 5839 b->enable_state = bp_enabled;
c906108c
SS
5840 b->next = 0;
5841 b->silent = 0;
5842 b->ignore_count = 0;
5843 b->commands = NULL;
818dd999 5844 b->frame_id = null_frame_id;
0d381245 5845 b->condition_not_parsed = 0;
84f4c1fe 5846 b->py_bp_object = NULL;
d0fb5eae 5847 b->related_breakpoint = b;
346774a9
PA
5848}
5849
5850/* Helper to set_raw_breakpoint below. Creates a breakpoint
5851 that has type BPTYPE and has no locations as yet. */
5852/* This function is used in gdbtk sources and thus can not be made
5853 static. */
5854
5855static struct breakpoint *
5856set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
5857 enum bptype bptype)
5858{
5859 struct breakpoint *b = XNEW (struct breakpoint);
5860
28010a5d 5861 init_raw_breakpoint_without_location (b, gdbarch, bptype, NULL);
c56053d2 5862 add_to_breakpoint_chain (b);
0d381245
VP
5863 return b;
5864}
5865
0e30163f
JK
5866/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5867 resolutions should be made as the user specified the location explicitly
5868 enough. */
5869
0d381245 5870static void
0e30163f 5871set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5872{
2bdf28a0
JK
5873 gdb_assert (loc->owner != NULL);
5874
0d381245 5875 if (loc->owner->type == bp_breakpoint
1042e4c0 5876 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5877 || is_tracepoint (loc->owner))
0d381245 5878 {
0e30163f
JK
5879 int is_gnu_ifunc;
5880
5881 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5882 NULL, NULL, &is_gnu_ifunc);
5883
5884 if (is_gnu_ifunc && !explicit_loc)
5885 {
5886 struct breakpoint *b = loc->owner;
5887
5888 gdb_assert (loc->pspace == current_program_space);
5889 if (gnu_ifunc_resolve_name (loc->function_name,
5890 &loc->requested_address))
5891 {
5892 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5893 loc->address = adjust_breakpoint_address (loc->gdbarch,
5894 loc->requested_address,
5895 b->type);
5896 }
5897 else if (b->type == bp_breakpoint && b->loc == loc
5898 && loc->next == NULL && b->related_breakpoint == b)
5899 {
5900 /* Create only the whole new breakpoint of this type but do not
5901 mess more complicated breakpoints with multiple locations. */
5902 b->type = bp_gnu_ifunc_resolver;
5903 }
5904 }
5905
0d381245
VP
5906 if (loc->function_name)
5907 loc->function_name = xstrdup (loc->function_name);
5908 }
5909}
5910
a6d9a66e
UW
5911/* Attempt to determine architecture of location identified by SAL. */
5912static struct gdbarch *
5913get_sal_arch (struct symtab_and_line sal)
5914{
5915 if (sal.section)
5916 return get_objfile_arch (sal.section->objfile);
5917 if (sal.symtab)
5918 return get_objfile_arch (sal.symtab->objfile);
5919
5920 return NULL;
5921}
5922
346774a9
PA
5923/* Low level routine for partially initializing a breakpoint of type
5924 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5925 file name, and line number are provided by SAL.
0d381245
VP
5926
5927 It is expected that the caller will complete the initialization of
5928 the newly created breakpoint struct as well as output any status
c56053d2 5929 information regarding the creation of a new breakpoint. */
0d381245 5930
346774a9
PA
5931static void
5932init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d
PA
5933 struct symtab_and_line sal, enum bptype bptype,
5934 struct breakpoint_ops *ops)
0d381245 5935{
0d381245 5936 CORE_ADDR adjusted_address;
a6d9a66e
UW
5937 struct gdbarch *loc_gdbarch;
5938
28010a5d 5939 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5940
a6d9a66e
UW
5941 loc_gdbarch = get_sal_arch (sal);
5942 if (!loc_gdbarch)
5943 loc_gdbarch = b->gdbarch;
0d381245 5944
6c95b8df
PA
5945 if (bptype != bp_catchpoint)
5946 gdb_assert (sal.pspace != NULL);
5947
0d381245
VP
5948 /* Adjust the breakpoint's address prior to allocating a location.
5949 Once we call allocate_bp_location(), that mostly uninitialized
5950 location will be placed on the location chain. Adjustment of the
8defab1a 5951 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5952 not want its scan of the location chain to find a breakpoint and
5953 location that's only been partially initialized. */
4a64f543
MS
5954 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5955 sal.pc, b->type);
0d381245 5956
39d61571 5957 b->loc = allocate_bp_location (b);
a6d9a66e 5958 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5959 b->loc->requested_address = sal.pc;
5960 b->loc->address = adjusted_address;
6c95b8df
PA
5961 b->loc->pspace = sal.pspace;
5962
5963 /* Store the program space that was used to set the breakpoint, for
5964 breakpoint resetting. */
5965 b->pspace = sal.pspace;
0d381245
VP
5966
5967 if (sal.symtab == NULL)
5968 b->source_file = NULL;
5969 else
1b36a34b 5970 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5971 b->loc->section = sal.section;
5972 b->line_number = sal.line;
5973
0e30163f
JK
5974 set_breakpoint_location_function (b->loc,
5975 sal.explicit_pc || sal.explicit_line);
c906108c 5976
c906108c 5977 breakpoints_changed ();
346774a9 5978}
c906108c 5979
346774a9
PA
5980/* set_raw_breakpoint is a low level routine for allocating and
5981 partially initializing a breakpoint of type BPTYPE. The newly
5982 created breakpoint's address, section, source file name, and line
5983 number are provided by SAL. The newly created and partially
5984 initialized breakpoint is added to the breakpoint chain and
5985 is also returned as the value of this function.
5986
5987 It is expected that the caller will complete the initialization of
5988 the newly created breakpoint struct as well as output any status
5989 information regarding the creation of a new breakpoint. In
5990 particular, set_raw_breakpoint does NOT set the breakpoint
5991 number! Care should be taken to not allow an error to occur
5992 prior to completing the initialization of the breakpoint. If this
5993 should happen, a bogus breakpoint will be left on the chain. */
5994
5995struct breakpoint *
5996set_raw_breakpoint (struct gdbarch *gdbarch,
5997 struct symtab_and_line sal, enum bptype bptype)
5998{
5999 struct breakpoint *b = XNEW (struct breakpoint);
6000
28010a5d 6001 init_raw_breakpoint (b, gdbarch, sal, bptype, NULL);
c56053d2 6002 add_to_breakpoint_chain (b);
c906108c
SS
6003 return b;
6004}
6005
c2c6d25f
JM
6006
6007/* Note that the breakpoint object B describes a permanent breakpoint
6008 instruction, hard-wired into the inferior's code. */
6009void
6010make_breakpoint_permanent (struct breakpoint *b)
6011{
0d381245 6012 struct bp_location *bl;
cc59ec59 6013
b5de0fa7 6014 b->enable_state = bp_permanent;
c2c6d25f 6015
4a64f543
MS
6016 /* By definition, permanent breakpoints are already present in the
6017 code. Mark all locations as inserted. For now,
6018 make_breakpoint_permanent is called in just one place, so it's
6019 hard to say if it's reasonable to have permanent breakpoint with
6020 multiple locations or not, but it's easy to implmement. */
0d381245
VP
6021 for (bl = b->loc; bl; bl = bl->next)
6022 bl->inserted = 1;
c2c6d25f
JM
6023}
6024
53a5351d 6025/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6026 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6027 initiated the operation. */
c906108c
SS
6028
6029void
186c406b 6030set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6031{
35df4500 6032 struct breakpoint *b, *b_tmp;
186c406b 6033 int thread = tp->num;
0fd8e87f
UW
6034
6035 /* To avoid having to rescan all objfile symbols at every step,
6036 we maintain a list of continually-inserted but always disabled
6037 longjmp "master" breakpoints. Here, we simply create momentary
6038 clones of those and enable them for the requested thread. */
35df4500 6039 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6040 if (b->pspace == current_program_space
186c406b
TT
6041 && (b->type == bp_longjmp_master
6042 || b->type == bp_exception_master))
0fd8e87f
UW
6043 {
6044 struct breakpoint *clone = clone_momentary_breakpoint (b);
cc59ec59 6045
186c406b 6046 clone->type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
0fd8e87f
UW
6047 clone->thread = thread;
6048 }
186c406b
TT
6049
6050 tp->initiating_frame = frame;
c906108c
SS
6051}
6052
611c83ae 6053/* Delete all longjmp breakpoints from THREAD. */
c906108c 6054void
611c83ae 6055delete_longjmp_breakpoint (int thread)
c906108c 6056{
35df4500 6057 struct breakpoint *b, *b_tmp;
c906108c 6058
35df4500 6059 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6060 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6061 {
6062 if (b->thread == thread)
6063 delete_breakpoint (b);
6064 }
c906108c
SS
6065}
6066
1900040c
MS
6067void
6068enable_overlay_breakpoints (void)
6069{
52f0bd74 6070 struct breakpoint *b;
1900040c
MS
6071
6072 ALL_BREAKPOINTS (b)
6073 if (b->type == bp_overlay_event)
6074 {
6075 b->enable_state = bp_enabled;
b60e7edf 6076 update_global_location_list (1);
c02f5703 6077 overlay_events_enabled = 1;
1900040c
MS
6078 }
6079}
6080
6081void
6082disable_overlay_breakpoints (void)
6083{
52f0bd74 6084 struct breakpoint *b;
1900040c
MS
6085
6086 ALL_BREAKPOINTS (b)
6087 if (b->type == bp_overlay_event)
6088 {
6089 b->enable_state = bp_disabled;
b60e7edf 6090 update_global_location_list (0);
c02f5703 6091 overlay_events_enabled = 0;
1900040c
MS
6092 }
6093}
6094
aa7d318d
TT
6095/* Set an active std::terminate breakpoint for each std::terminate
6096 master breakpoint. */
6097void
6098set_std_terminate_breakpoint (void)
6099{
35df4500 6100 struct breakpoint *b, *b_tmp;
aa7d318d 6101
35df4500 6102 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6103 if (b->pspace == current_program_space
6104 && b->type == bp_std_terminate_master)
6105 {
6106 struct breakpoint *clone = clone_momentary_breakpoint (b);
6107 clone->type = bp_std_terminate;
6108 }
6109}
6110
6111/* Delete all the std::terminate breakpoints. */
6112void
6113delete_std_terminate_breakpoint (void)
6114{
35df4500 6115 struct breakpoint *b, *b_tmp;
aa7d318d 6116
35df4500 6117 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6118 if (b->type == bp_std_terminate)
6119 delete_breakpoint (b);
6120}
6121
c4093a6a 6122struct breakpoint *
a6d9a66e 6123create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6124{
6125 struct breakpoint *b;
c4093a6a 6126
a6d9a66e 6127 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
c4093a6a 6128
b5de0fa7 6129 b->enable_state = bp_enabled;
c4093a6a 6130 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6131 b->addr_string
6132 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6133
b60e7edf 6134 update_global_location_list_nothrow (1);
74960c60 6135
c4093a6a
JM
6136 return b;
6137}
6138
6139void
6140remove_thread_event_breakpoints (void)
6141{
35df4500 6142 struct breakpoint *b, *b_tmp;
c4093a6a 6143
35df4500 6144 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6145 if (b->type == bp_thread_event
6146 && b->loc->pspace == current_program_space)
c4093a6a
JM
6147 delete_breakpoint (b);
6148}
6149
0101ce28
JJ
6150struct lang_and_radix
6151 {
6152 enum language lang;
6153 int radix;
6154 };
6155
4efc6507
DE
6156/* Create a breakpoint for JIT code registration and unregistration. */
6157
6158struct breakpoint *
6159create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6160{
6161 struct breakpoint *b;
6162
6163 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
6164 update_global_location_list_nothrow (1);
6165 return b;
6166}
0101ce28 6167
03673fc7
PP
6168/* Remove JIT code registration and unregistration breakpoint(s). */
6169
6170void
6171remove_jit_event_breakpoints (void)
6172{
6173 struct breakpoint *b, *b_tmp;
6174
6175 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6176 if (b->type == bp_jit_event
6177 && b->loc->pspace == current_program_space)
6178 delete_breakpoint (b);
6179}
6180
cae688ec
JJ
6181void
6182remove_solib_event_breakpoints (void)
6183{
35df4500 6184 struct breakpoint *b, *b_tmp;
cae688ec 6185
35df4500 6186 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6187 if (b->type == bp_shlib_event
6188 && b->loc->pspace == current_program_space)
cae688ec
JJ
6189 delete_breakpoint (b);
6190}
6191
6192struct breakpoint *
a6d9a66e 6193create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6194{
6195 struct breakpoint *b;
6196
a6d9a66e 6197 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
b60e7edf 6198 update_global_location_list_nothrow (1);
cae688ec
JJ
6199 return b;
6200}
6201
6202/* Disable any breakpoints that are on code in shared libraries. Only
6203 apply to enabled breakpoints, disabled ones can just stay disabled. */
6204
6205void
cb851954 6206disable_breakpoints_in_shlibs (void)
cae688ec 6207{
876fa593 6208 struct bp_location *loc, **locp_tmp;
cae688ec 6209
876fa593 6210 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6211 {
2bdf28a0 6212 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6213 struct breakpoint *b = loc->owner;
2bdf28a0 6214
4a64f543
MS
6215 /* We apply the check to all breakpoints, including disabled for
6216 those with loc->duplicate set. This is so that when breakpoint
6217 becomes enabled, or the duplicate is removed, gdb will try to
6218 insert all breakpoints. If we don't set shlib_disabled here,
6219 we'll try to insert those breakpoints and fail. */
1042e4c0 6220 if (((b->type == bp_breakpoint)
508ccb1f 6221 || (b->type == bp_jit_event)
1042e4c0 6222 || (b->type == bp_hardware_breakpoint)
d77f58be 6223 || (is_tracepoint (b)))
6c95b8df 6224 && loc->pspace == current_program_space
0d381245 6225 && !loc->shlib_disabled
a77053c2 6226#ifdef PC_SOLIB
0d381245 6227 && PC_SOLIB (loc->address)
a77053c2 6228#else
6c95b8df 6229 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6230#endif
6231 )
0d381245
VP
6232 {
6233 loc->shlib_disabled = 1;
6234 }
cae688ec
JJ
6235 }
6236}
6237
7a9dd1b2 6238/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
6239 Only apply to enabled breakpoints, disabled ones can just stay
6240 disabled. */
84acb35a 6241
75149521 6242static void
84acb35a
JJ
6243disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6244{
876fa593 6245 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6246 int disabled_shlib_breaks = 0;
6247
c86cf029
VP
6248 /* SunOS a.out shared libraries are always mapped, so do not
6249 disable breakpoints; they will only be reported as unloaded
6250 through clear_solib when GDB discards its shared library
6251 list. See clear_solib for more information. */
6252 if (exec_bfd != NULL
6253 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6254 return;
6255
876fa593 6256 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6257 {
2bdf28a0 6258 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6259 struct breakpoint *b = loc->owner;
cc59ec59 6260
0d381245
VP
6261 if ((loc->loc_type == bp_loc_hardware_breakpoint
6262 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6263 && solib->pspace == loc->pspace
e2dd7057 6264 && !loc->shlib_disabled
508ccb1f
TT
6265 && (b->type == bp_breakpoint
6266 || b->type == bp_jit_event
6267 || b->type == bp_hardware_breakpoint)
e2dd7057 6268 && solib_contains_address_p (solib, loc->address))
84acb35a 6269 {
e2dd7057
PP
6270 loc->shlib_disabled = 1;
6271 /* At this point, we cannot rely on remove_breakpoint
6272 succeeding so we must mark the breakpoint as not inserted
6273 to prevent future errors occurring in remove_breakpoints. */
6274 loc->inserted = 0;
8d3788bd
VP
6275
6276 /* This may cause duplicate notifications for the same breakpoint. */
6277 observer_notify_breakpoint_modified (b);
6278
e2dd7057
PP
6279 if (!disabled_shlib_breaks)
6280 {
6281 target_terminal_ours_for_output ();
3e43a32a
MS
6282 warning (_("Temporarily disabling breakpoints "
6283 "for unloaded shared library \"%s\""),
e2dd7057 6284 solib->so_name);
84acb35a 6285 }
e2dd7057 6286 disabled_shlib_breaks = 1;
84acb35a
JJ
6287 }
6288 }
84acb35a
JJ
6289}
6290
ce78b96d
JB
6291/* FORK & VFORK catchpoints. */
6292
e29a4733
PA
6293/* An instance of this type is used to represent a fork or vfork
6294 catchpoint. It includes a "struct breakpoint" as a kind of base
6295 class; users downcast to "struct breakpoint *" when needed. A
6296 breakpoint is really of this type iff its ops pointer points to
6297 CATCH_FORK_BREAKPOINT_OPS. */
6298
6299struct fork_catchpoint
6300{
6301 /* The base class. */
6302 struct breakpoint base;
6303
6304 /* Process id of a child process whose forking triggered this
6305 catchpoint. This field is only valid immediately after this
6306 catchpoint has triggered. */
6307 ptid_t forked_inferior_pid;
6308};
6309
4a64f543
MS
6310/* Implement the "insert" breakpoint_ops method for fork
6311 catchpoints. */
ce78b96d 6312
77b06cd7
TJB
6313static int
6314insert_catch_fork (struct bp_location *bl)
ce78b96d 6315{
77b06cd7 6316 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6317}
6318
4a64f543
MS
6319/* Implement the "remove" breakpoint_ops method for fork
6320 catchpoints. */
ce78b96d
JB
6321
6322static int
77b06cd7 6323remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6324{
6325 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6326}
6327
6328/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6329 catchpoints. */
6330
6331static int
f1310107
TJB
6332breakpoint_hit_catch_fork (const struct bp_location *bl,
6333 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6334{
e29a4733
PA
6335 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6336
6337 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6338}
6339
4a64f543
MS
6340/* Implement the "print_it" breakpoint_ops method for fork
6341 catchpoints. */
ce78b96d
JB
6342
6343static enum print_stop_action
6344print_it_catch_fork (struct breakpoint *b)
6345{
e29a4733
PA
6346 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6347
ce78b96d
JB
6348 annotate_catchpoint (b->number);
6349 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6350 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6351 return PRINT_SRC_AND_LOC;
6352}
6353
4a64f543
MS
6354/* Implement the "print_one" breakpoint_ops method for fork
6355 catchpoints. */
ce78b96d
JB
6356
6357static void
a6d9a66e 6358print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6359{
e29a4733 6360 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6361 struct value_print_options opts;
6362
6363 get_user_print_options (&opts);
6364
4a64f543
MS
6365 /* Field 4, the address, is omitted (which makes the columns not
6366 line up too nicely with the headers, but the effect is relatively
6367 readable). */
79a45b7d 6368 if (opts.addressprint)
ce78b96d
JB
6369 ui_out_field_skip (uiout, "addr");
6370 annotate_field (5);
6371 ui_out_text (uiout, "fork");
e29a4733 6372 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6373 {
6374 ui_out_text (uiout, ", process ");
6375 ui_out_field_int (uiout, "what",
e29a4733 6376 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6377 ui_out_spaces (uiout, 1);
6378 }
6379}
6380
6381/* Implement the "print_mention" breakpoint_ops method for fork
6382 catchpoints. */
6383
6384static void
6385print_mention_catch_fork (struct breakpoint *b)
6386{
6387 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6388}
6389
6149aea9
PA
6390/* Implement the "print_recreate" breakpoint_ops method for fork
6391 catchpoints. */
6392
6393static void
6394print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6395{
6396 fprintf_unfiltered (fp, "catch fork");
6397}
6398
ce78b96d
JB
6399/* The breakpoint_ops structure to be used in fork catchpoints. */
6400
6401static struct breakpoint_ops catch_fork_breakpoint_ops =
6402{
be5c67c1 6403 NULL, /* dtor */
28010a5d
PA
6404 NULL, /* allocate_location */
6405 NULL, /* re_set */
ce78b96d
JB
6406 insert_catch_fork,
6407 remove_catch_fork,
6408 breakpoint_hit_catch_fork,
28010a5d 6409 NULL, /* check_status */
e09342b5 6410 NULL, /* resources_needed */
9c06b0b4 6411 NULL, /* works_in_software_mode */
ce78b96d
JB
6412 print_it_catch_fork,
6413 print_one_catch_fork,
f1310107 6414 NULL, /* print_one_detail */
6149aea9
PA
6415 print_mention_catch_fork,
6416 print_recreate_catch_fork
ce78b96d
JB
6417};
6418
4a64f543
MS
6419/* Implement the "insert" breakpoint_ops method for vfork
6420 catchpoints. */
ce78b96d 6421
77b06cd7
TJB
6422static int
6423insert_catch_vfork (struct bp_location *bl)
ce78b96d 6424{
77b06cd7 6425 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6426}
6427
4a64f543
MS
6428/* Implement the "remove" breakpoint_ops method for vfork
6429 catchpoints. */
ce78b96d
JB
6430
6431static int
77b06cd7 6432remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6433{
6434 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6435}
6436
6437/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6438 catchpoints. */
6439
6440static int
f1310107
TJB
6441breakpoint_hit_catch_vfork (const struct bp_location *bl,
6442 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6443{
e29a4733
PA
6444 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6445
6446 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6447}
6448
4a64f543
MS
6449/* Implement the "print_it" breakpoint_ops method for vfork
6450 catchpoints. */
ce78b96d
JB
6451
6452static enum print_stop_action
6453print_it_catch_vfork (struct breakpoint *b)
6454{
e29a4733
PA
6455 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6456
ce78b96d
JB
6457 annotate_catchpoint (b->number);
6458 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6459 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6460 return PRINT_SRC_AND_LOC;
6461}
6462
4a64f543
MS
6463/* Implement the "print_one" breakpoint_ops method for vfork
6464 catchpoints. */
ce78b96d
JB
6465
6466static void
a6d9a66e 6467print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6468{
e29a4733 6469 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d
TT
6470 struct value_print_options opts;
6471
6472 get_user_print_options (&opts);
4a64f543
MS
6473 /* Field 4, the address, is omitted (which makes the columns not
6474 line up too nicely with the headers, but the effect is relatively
6475 readable). */
79a45b7d 6476 if (opts.addressprint)
ce78b96d
JB
6477 ui_out_field_skip (uiout, "addr");
6478 annotate_field (5);
6479 ui_out_text (uiout, "vfork");
e29a4733 6480 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6481 {
6482 ui_out_text (uiout, ", process ");
6483 ui_out_field_int (uiout, "what",
e29a4733 6484 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6485 ui_out_spaces (uiout, 1);
6486 }
6487}
6488
6489/* Implement the "print_mention" breakpoint_ops method for vfork
6490 catchpoints. */
6491
6492static void
6493print_mention_catch_vfork (struct breakpoint *b)
6494{
6495 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6496}
6497
6149aea9
PA
6498/* Implement the "print_recreate" breakpoint_ops method for vfork
6499 catchpoints. */
6500
6501static void
6502print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6503{
6504 fprintf_unfiltered (fp, "catch vfork");
6505}
6506
ce78b96d
JB
6507/* The breakpoint_ops structure to be used in vfork catchpoints. */
6508
6509static struct breakpoint_ops catch_vfork_breakpoint_ops =
6510{
be5c67c1 6511 NULL, /* dtor */
28010a5d
PA
6512 NULL, /* allocate_location */
6513 NULL, /* re_set */
ce78b96d
JB
6514 insert_catch_vfork,
6515 remove_catch_vfork,
6516 breakpoint_hit_catch_vfork,
28010a5d 6517 NULL, /* check_status */
e09342b5 6518 NULL, /* resources_needed */
9c06b0b4 6519 NULL, /* works_in_software_mode */
ce78b96d
JB
6520 print_it_catch_vfork,
6521 print_one_catch_vfork,
f1310107 6522 NULL, /* print_one_detail */
6149aea9
PA
6523 print_mention_catch_vfork,
6524 print_recreate_catch_vfork
ce78b96d
JB
6525};
6526
be5c67c1
PA
6527/* An instance of this type is used to represent a syscall catchpoint.
6528 It includes a "struct breakpoint" as a kind of base class; users
6529 downcast to "struct breakpoint *" when needed. A breakpoint is
6530 really of this type iff its ops pointer points to
6531 CATCH_SYSCALL_BREAKPOINT_OPS. */
6532
6533struct syscall_catchpoint
6534{
6535 /* The base class. */
6536 struct breakpoint base;
6537
6538 /* Syscall numbers used for the 'catch syscall' feature. If no
6539 syscall has been specified for filtering, its value is NULL.
6540 Otherwise, it holds a list of all syscalls to be caught. The
6541 list elements are allocated with xmalloc. */
6542 VEC(int) *syscalls_to_be_caught;
6543};
6544
6545/* Implement the "dtor" breakpoint_ops method for syscall
6546 catchpoints. */
6547
6548static void
6549dtor_catch_syscall (struct breakpoint *b)
6550{
6551 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6552
6553 VEC_free (int, c->syscalls_to_be_caught);
6554}
6555
a96d9b2e
SDJ
6556/* Implement the "insert" breakpoint_ops method for syscall
6557 catchpoints. */
6558
77b06cd7
TJB
6559static int
6560insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6561{
be5c67c1 6562 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6563 struct inferior *inf = current_inferior ();
6564
6565 ++inf->total_syscalls_count;
be5c67c1 6566 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6567 ++inf->any_syscall_count;
6568 else
6569 {
6570 int i, iter;
cc59ec59 6571
a96d9b2e 6572 for (i = 0;
be5c67c1 6573 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6574 i++)
6575 {
6576 int elem;
cc59ec59 6577
a96d9b2e
SDJ
6578 if (iter >= VEC_length (int, inf->syscalls_counts))
6579 {
6580 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6581 uintptr_t vec_addr_offset
6582 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6583 uintptr_t vec_addr;
6584 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6585 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6586 vec_addr_offset;
6587 memset ((void *) vec_addr, 0,
6588 (iter + 1 - old_size) * sizeof (int));
6589 }
6590 elem = VEC_index (int, inf->syscalls_counts, iter);
6591 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6592 }
6593 }
6594
77b06cd7
TJB
6595 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6596 inf->total_syscalls_count != 0,
6597 inf->any_syscall_count,
6598 VEC_length (int, inf->syscalls_counts),
6599 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6600}
6601
6602/* Implement the "remove" breakpoint_ops method for syscall
6603 catchpoints. */
6604
6605static int
77b06cd7 6606remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6607{
be5c67c1 6608 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6609 struct inferior *inf = current_inferior ();
6610
6611 --inf->total_syscalls_count;
be5c67c1 6612 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6613 --inf->any_syscall_count;
6614 else
6615 {
6616 int i, iter;
cc59ec59 6617
a96d9b2e 6618 for (i = 0;
be5c67c1 6619 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6620 i++)
6621 {
6622 int elem;
6623 if (iter >= VEC_length (int, inf->syscalls_counts))
6624 /* Shouldn't happen. */
6625 continue;
6626 elem = VEC_index (int, inf->syscalls_counts, iter);
6627 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6628 }
6629 }
6630
6631 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6632 inf->total_syscalls_count != 0,
6633 inf->any_syscall_count,
6634 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6635 VEC_address (int,
6636 inf->syscalls_counts));
a96d9b2e
SDJ
6637}
6638
6639/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6640 catchpoints. */
6641
6642static int
f1310107
TJB
6643breakpoint_hit_catch_syscall (const struct bp_location *bl,
6644 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6645{
4a64f543
MS
6646 /* We must check if we are catching specific syscalls in this
6647 breakpoint. If we are, then we must guarantee that the called
6648 syscall is the same syscall we are catching. */
a96d9b2e 6649 int syscall_number = 0;
be5c67c1
PA
6650 const struct syscall_catchpoint *c
6651 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6652
6653 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6654 return 0;
6655
6656 /* Now, checking if the syscall is the same. */
be5c67c1 6657 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6658 {
6659 int i, iter;
cc59ec59 6660
a96d9b2e 6661 for (i = 0;
be5c67c1 6662 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6663 i++)
6664 if (syscall_number == iter)
6665 break;
6666 /* Not the same. */
6667 if (!iter)
6668 return 0;
6669 }
6670
6671 return 1;
6672}
6673
6674/* Implement the "print_it" breakpoint_ops method for syscall
6675 catchpoints. */
6676
6677static enum print_stop_action
6678print_it_catch_syscall (struct breakpoint *b)
6679{
6680 /* These are needed because we want to know in which state a
6681 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6682 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6683 must print "called syscall" or "returned from syscall". */
6684 ptid_t ptid;
6685 struct target_waitstatus last;
6686 struct syscall s;
6687 struct cleanup *old_chain;
6688 char *syscall_id;
6689
6690 get_last_target_status (&ptid, &last);
6691
6692 get_syscall_by_number (last.value.syscall_number, &s);
6693
6694 annotate_catchpoint (b->number);
6695
6696 if (s.name == NULL)
6697 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6698 else
6699 syscall_id = xstrprintf ("'%s'", s.name);
6700
6701 old_chain = make_cleanup (xfree, syscall_id);
6702
6703 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6704 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6705 b->number, syscall_id);
6706 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6707 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6708 b->number, syscall_id);
6709
6710 do_cleanups (old_chain);
6711
6712 return PRINT_SRC_AND_LOC;
6713}
6714
6715/* Implement the "print_one" breakpoint_ops method for syscall
6716 catchpoints. */
6717
6718static void
6719print_one_catch_syscall (struct breakpoint *b,
f1310107 6720 struct bp_location **last_loc)
a96d9b2e 6721{
be5c67c1 6722 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e
SDJ
6723 struct value_print_options opts;
6724
6725 get_user_print_options (&opts);
4a64f543
MS
6726 /* Field 4, the address, is omitted (which makes the columns not
6727 line up too nicely with the headers, but the effect is relatively
6728 readable). */
a96d9b2e
SDJ
6729 if (opts.addressprint)
6730 ui_out_field_skip (uiout, "addr");
6731 annotate_field (5);
6732
be5c67c1
PA
6733 if (c->syscalls_to_be_caught
6734 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6735 ui_out_text (uiout, "syscalls \"");
6736 else
6737 ui_out_text (uiout, "syscall \"");
6738
be5c67c1 6739 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6740 {
6741 int i, iter;
6742 char *text = xstrprintf ("%s", "");
cc59ec59 6743
a96d9b2e 6744 for (i = 0;
be5c67c1 6745 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6746 i++)
6747 {
6748 char *x = text;
6749 struct syscall s;
6750 get_syscall_by_number (iter, &s);
6751
6752 if (s.name != NULL)
6753 text = xstrprintf ("%s%s, ", text, s.name);
6754 else
6755 text = xstrprintf ("%s%d, ", text, iter);
6756
6757 /* We have to xfree the last 'text' (now stored at 'x')
6758 because xstrprintf dinamically allocates new space for it
6759 on every call. */
6760 xfree (x);
6761 }
6762 /* Remove the last comma. */
6763 text[strlen (text) - 2] = '\0';
6764 ui_out_field_string (uiout, "what", text);
6765 }
6766 else
6767 ui_out_field_string (uiout, "what", "<any syscall>");
6768 ui_out_text (uiout, "\" ");
6769}
6770
6771/* Implement the "print_mention" breakpoint_ops method for syscall
6772 catchpoints. */
6773
6774static void
6775print_mention_catch_syscall (struct breakpoint *b)
6776{
be5c67c1
PA
6777 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6778
6779 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6780 {
6781 int i, iter;
6782
be5c67c1 6783 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6784 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6785 else
6786 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6787
6788 for (i = 0;
be5c67c1 6789 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6790 i++)
6791 {
6792 struct syscall s;
6793 get_syscall_by_number (iter, &s);
6794
6795 if (s.name)
6796 printf_filtered (" '%s' [%d]", s.name, s.number);
6797 else
6798 printf_filtered (" %d", s.number);
6799 }
6800 printf_filtered (")");
6801 }
6802 else
6803 printf_filtered (_("Catchpoint %d (any syscall)"),
6804 b->number);
6805}
6806
6149aea9
PA
6807/* Implement the "print_recreate" breakpoint_ops method for syscall
6808 catchpoints. */
6809
6810static void
6811print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6812{
be5c67c1
PA
6813 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6814
6149aea9
PA
6815 fprintf_unfiltered (fp, "catch syscall");
6816
be5c67c1 6817 if (c->syscalls_to_be_caught)
6149aea9
PA
6818 {
6819 int i, iter;
6820
6821 for (i = 0;
be5c67c1 6822 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6823 i++)
6824 {
6825 struct syscall s;
6826
6827 get_syscall_by_number (iter, &s);
6828 if (s.name)
6829 fprintf_unfiltered (fp, " %s", s.name);
6830 else
6831 fprintf_unfiltered (fp, " %d", s.number);
6832 }
6833 }
6834}
6835
a96d9b2e
SDJ
6836/* The breakpoint_ops structure to be used in syscall catchpoints. */
6837
6838static struct breakpoint_ops catch_syscall_breakpoint_ops =
6839{
be5c67c1 6840 dtor_catch_syscall,
28010a5d
PA
6841 NULL, /* allocate_location */
6842 NULL, /* re_set */
a96d9b2e
SDJ
6843 insert_catch_syscall,
6844 remove_catch_syscall,
6845 breakpoint_hit_catch_syscall,
28010a5d 6846 NULL, /* check_status */
e09342b5 6847 NULL, /* resources_needed */
9c06b0b4 6848 NULL, /* works_in_software_mode */
a96d9b2e
SDJ
6849 print_it_catch_syscall,
6850 print_one_catch_syscall,
f1310107 6851 NULL, /* print_one_detail */
6149aea9
PA
6852 print_mention_catch_syscall,
6853 print_recreate_catch_syscall
a96d9b2e
SDJ
6854};
6855
6856/* Returns non-zero if 'b' is a syscall catchpoint. */
6857
6858static int
6859syscall_catchpoint_p (struct breakpoint *b)
6860{
6861 return (b->ops == &catch_syscall_breakpoint_ops);
6862}
6863
346774a9
PA
6864/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6865 is non-zero, then make the breakpoint temporary. If COND_STRING is
6866 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6867 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6868
346774a9
PA
6869static void
6870init_catchpoint (struct breakpoint *b,
6871 struct gdbarch *gdbarch, int tempflag,
6872 char *cond_string,
6873 struct breakpoint_ops *ops)
c906108c 6874{
c5aa993b 6875 struct symtab_and_line sal;
346774a9 6876
fe39c653 6877 init_sal (&sal);
6c95b8df 6878 sal.pspace = current_program_space;
c5aa993b 6879
28010a5d 6880 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6881
1b36a34b 6882 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6883 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6884}
6885
28010a5d
PA
6886void
6887install_breakpoint (struct breakpoint *b)
c56053d2
PA
6888{
6889 add_to_breakpoint_chain (b);
6890 set_breakpoint_count (breakpoint_count + 1);
6891 b->number = breakpoint_count;
6892 mention (b);
6893 observer_notify_breakpoint_created (b);
6894 update_global_location_list (1);
6895}
6896
9b70b993 6897static void
a6d9a66e
UW
6898create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6899 int tempflag, char *cond_string,
ce78b96d 6900 struct breakpoint_ops *ops)
c906108c 6901{
e29a4733 6902 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6903
e29a4733
PA
6904 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6905
6906 c->forked_inferior_pid = null_ptid;
6907
28010a5d 6908 install_breakpoint (&c->base);
c906108c
SS
6909}
6910
fe798b75
JB
6911/* Exec catchpoints. */
6912
b4d90040
PA
6913/* An instance of this type is used to represent an exec catchpoint.
6914 It includes a "struct breakpoint" as a kind of base class; users
6915 downcast to "struct breakpoint *" when needed. A breakpoint is
6916 really of this type iff its ops pointer points to
6917 CATCH_EXEC_BREAKPOINT_OPS. */
6918
6919struct exec_catchpoint
6920{
6921 /* The base class. */
6922 struct breakpoint base;
6923
6924 /* Filename of a program whose exec triggered this catchpoint.
6925 This field is only valid immediately after this catchpoint has
6926 triggered. */
6927 char *exec_pathname;
6928};
6929
6930/* Implement the "dtor" breakpoint_ops method for exec
6931 catchpoints. */
6932
6933static void
6934dtor_catch_exec (struct breakpoint *b)
6935{
6936 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6937
6938 xfree (c->exec_pathname);
6939}
6940
77b06cd7
TJB
6941static int
6942insert_catch_exec (struct bp_location *bl)
c906108c 6943{
77b06cd7 6944 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6945}
c906108c 6946
fe798b75 6947static int
77b06cd7 6948remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6949{
6950 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6951}
c906108c 6952
fe798b75 6953static int
f1310107
TJB
6954breakpoint_hit_catch_exec (const struct bp_location *bl,
6955 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6956{
b4d90040
PA
6957 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6958
6959 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6960}
c906108c 6961
fe798b75
JB
6962static enum print_stop_action
6963print_it_catch_exec (struct breakpoint *b)
6964{
b4d90040
PA
6965 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6966
fe798b75
JB
6967 annotate_catchpoint (b->number);
6968 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6969 c->exec_pathname);
fe798b75 6970 return PRINT_SRC_AND_LOC;
c906108c
SS
6971}
6972
fe798b75 6973static void
a6d9a66e 6974print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6975{
b4d90040 6976 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75
JB
6977 struct value_print_options opts;
6978
6979 get_user_print_options (&opts);
6980
6981 /* Field 4, the address, is omitted (which makes the columns
6982 not line up too nicely with the headers, but the effect
6983 is relatively readable). */
6984 if (opts.addressprint)
6985 ui_out_field_skip (uiout, "addr");
6986 annotate_field (5);
6987 ui_out_text (uiout, "exec");
b4d90040 6988 if (c->exec_pathname != NULL)
fe798b75
JB
6989 {
6990 ui_out_text (uiout, ", program \"");
b4d90040 6991 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6992 ui_out_text (uiout, "\" ");
6993 }
6994}
6995
6996static void
6997print_mention_catch_exec (struct breakpoint *b)
6998{
6999 printf_filtered (_("Catchpoint %d (exec)"), b->number);
7000}
7001
6149aea9
PA
7002/* Implement the "print_recreate" breakpoint_ops method for exec
7003 catchpoints. */
7004
7005static void
7006print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
7007{
7008 fprintf_unfiltered (fp, "catch exec");
7009}
7010
fe798b75
JB
7011static struct breakpoint_ops catch_exec_breakpoint_ops =
7012{
b4d90040 7013 dtor_catch_exec,
28010a5d
PA
7014 NULL, /* allocate_location */
7015 NULL, /* re_set */
fe798b75
JB
7016 insert_catch_exec,
7017 remove_catch_exec,
7018 breakpoint_hit_catch_exec,
28010a5d 7019 NULL, /* check_status */
e09342b5 7020 NULL, /* resources_needed */
9c06b0b4 7021 NULL, /* works_in_software_mode */
fe798b75
JB
7022 print_it_catch_exec,
7023 print_one_catch_exec,
f1310107 7024 NULL, /* print_one_detail */
6149aea9
PA
7025 print_mention_catch_exec,
7026 print_recreate_catch_exec
fe798b75
JB
7027};
7028
a96d9b2e
SDJ
7029static void
7030create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
7031 struct breakpoint_ops *ops)
7032{
be5c67c1 7033 struct syscall_catchpoint *c;
a96d9b2e 7034 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 7035
be5c67c1
PA
7036 c = XNEW (struct syscall_catchpoint);
7037 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
7038 c->syscalls_to_be_caught = filter;
a96d9b2e 7039
28010a5d 7040 install_breakpoint (&c->base);
a96d9b2e
SDJ
7041}
7042
c906108c 7043static int
fba45db2 7044hw_breakpoint_used_count (void)
c906108c 7045{
c906108c 7046 int i = 0;
f1310107
TJB
7047 struct breakpoint *b;
7048 struct bp_location *bl;
c906108c
SS
7049
7050 ALL_BREAKPOINTS (b)
c5aa993b 7051 {
d6b74ac4 7052 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
7053 for (bl = b->loc; bl; bl = bl->next)
7054 {
7055 /* Special types of hardware breakpoints may use more than
7056 one register. */
7057 if (b->ops && b->ops->resources_needed)
7058 i += b->ops->resources_needed (bl);
7059 else
7060 i++;
7061 }
c5aa993b 7062 }
c906108c
SS
7063
7064 return i;
7065}
7066
7067static int
fba45db2 7068hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 7069{
c906108c 7070 int i = 0;
e09342b5
TJB
7071 struct breakpoint *b;
7072 struct bp_location *bl;
c906108c
SS
7073
7074 *other_type_used = 0;
7075 ALL_BREAKPOINTS (b)
e09342b5
TJB
7076 {
7077 if (!breakpoint_enabled (b))
7078 continue;
7079
c5aa993b 7080 if (b->type == type)
e09342b5
TJB
7081 for (bl = b->loc; bl; bl = bl->next)
7082 {
7083 /* Special types of hardware watchpoints may use more than
7084 one register. */
7085 if (b->ops && b->ops->resources_needed)
7086 i += b->ops->resources_needed (bl);
7087 else
7088 i++;
7089 }
cc60f2e3 7090 else if (is_hardware_watchpoint (b))
c5aa993b 7091 *other_type_used = 1;
e09342b5
TJB
7092 }
7093
c906108c
SS
7094 return i;
7095}
7096
c906108c 7097void
fba45db2 7098disable_watchpoints_before_interactive_call_start (void)
c906108c 7099{
c5aa993b 7100 struct breakpoint *b;
c906108c
SS
7101
7102 ALL_BREAKPOINTS (b)
c5aa993b 7103 {
cc60f2e3 7104 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 7105 {
b5de0fa7 7106 b->enable_state = bp_call_disabled;
b60e7edf 7107 update_global_location_list (0);
c5aa993b
JM
7108 }
7109 }
c906108c
SS
7110}
7111
7112void
fba45db2 7113enable_watchpoints_after_interactive_call_stop (void)
c906108c 7114{
c5aa993b 7115 struct breakpoint *b;
c906108c
SS
7116
7117 ALL_BREAKPOINTS (b)
c5aa993b 7118 {
cc60f2e3 7119 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 7120 {
b5de0fa7 7121 b->enable_state = bp_enabled;
b60e7edf 7122 update_global_location_list (1);
c5aa993b
JM
7123 }
7124 }
c906108c
SS
7125}
7126
8bea4e01
UW
7127void
7128disable_breakpoints_before_startup (void)
7129{
7130 struct breakpoint *b;
7131 int found = 0;
7132
7133 ALL_BREAKPOINTS (b)
7134 {
6c95b8df
PA
7135 if (b->pspace != current_program_space)
7136 continue;
7137
8bea4e01
UW
7138 if ((b->type == bp_breakpoint
7139 || b->type == bp_hardware_breakpoint)
7140 && breakpoint_enabled (b))
7141 {
7142 b->enable_state = bp_startup_disabled;
7143 found = 1;
7144 }
7145 }
7146
7147 if (found)
7148 update_global_location_list (0);
7149
6c95b8df 7150 current_program_space->executing_startup = 1;
8bea4e01
UW
7151}
7152
7153void
7154enable_breakpoints_after_startup (void)
7155{
7156 struct breakpoint *b;
7157 int found = 0;
7158
6c95b8df 7159 current_program_space->executing_startup = 0;
8bea4e01
UW
7160
7161 ALL_BREAKPOINTS (b)
7162 {
6c95b8df
PA
7163 if (b->pspace != current_program_space)
7164 continue;
7165
8bea4e01
UW
7166 if ((b->type == bp_breakpoint
7167 || b->type == bp_hardware_breakpoint)
7168 && b->enable_state == bp_startup_disabled)
7169 {
7170 b->enable_state = bp_enabled;
7171 found = 1;
7172 }
7173 }
7174
7175 if (found)
7176 breakpoint_re_set ();
7177}
7178
c906108c
SS
7179
7180/* Set a breakpoint that will evaporate an end of command
7181 at address specified by SAL.
7182 Restrict it to frame FRAME if FRAME is nonzero. */
7183
7184struct breakpoint *
a6d9a66e
UW
7185set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7186 struct frame_id frame_id, enum bptype type)
c906108c 7187{
52f0bd74 7188 struct breakpoint *b;
edb3359d
DJ
7189
7190 /* If FRAME_ID is valid, it should be a real frame, not an inlined
7191 one. */
7192 gdb_assert (!frame_id_inlined_p (frame_id));
7193
a6d9a66e 7194 b = set_raw_breakpoint (gdbarch, sal, type);
b5de0fa7
EZ
7195 b->enable_state = bp_enabled;
7196 b->disposition = disp_donttouch;
818dd999 7197 b->frame_id = frame_id;
c906108c 7198
4a64f543
MS
7199 /* If we're debugging a multi-threaded program, then we want
7200 momentary breakpoints to be active in only a single thread of
7201 control. */
39f77062
KB
7202 if (in_thread_list (inferior_ptid))
7203 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7204
b60e7edf 7205 update_global_location_list_nothrow (1);
74960c60 7206
c906108c
SS
7207 return b;
7208}
611c83ae 7209
e58b0e63
PA
7210/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7211 ORIG is NULL. */
7212
7213struct breakpoint *
7214clone_momentary_breakpoint (struct breakpoint *orig)
7215{
7216 struct breakpoint *copy;
7217
7218 /* If there's nothing to clone, then return nothing. */
7219 if (orig == NULL)
7220 return NULL;
7221
a6d9a66e 7222 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
e58b0e63 7223 copy->loc = allocate_bp_location (copy);
0e30163f 7224 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7225
a6d9a66e 7226 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7227 copy->loc->requested_address = orig->loc->requested_address;
7228 copy->loc->address = orig->loc->address;
7229 copy->loc->section = orig->loc->section;
6c95b8df 7230 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7231
7232 if (orig->source_file == NULL)
7233 copy->source_file = NULL;
7234 else
7235 copy->source_file = xstrdup (orig->source_file);
7236
7237 copy->line_number = orig->line_number;
7238 copy->frame_id = orig->frame_id;
7239 copy->thread = orig->thread;
6c95b8df 7240 copy->pspace = orig->pspace;
e58b0e63
PA
7241
7242 copy->enable_state = bp_enabled;
7243 copy->disposition = disp_donttouch;
7244 copy->number = internal_breakpoint_number--;
7245
7246 update_global_location_list_nothrow (0);
7247 return copy;
7248}
7249
611c83ae 7250struct breakpoint *
a6d9a66e
UW
7251set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7252 enum bptype type)
611c83ae
PA
7253{
7254 struct symtab_and_line sal;
7255
7256 sal = find_pc_line (pc, 0);
7257 sal.pc = pc;
7258 sal.section = find_pc_overlay (pc);
7259 sal.explicit_pc = 1;
7260
a6d9a66e 7261 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7262}
c906108c 7263\f
c5aa993b 7264
c906108c
SS
7265/* Tell the user we have just set a breakpoint B. */
7266
7267static void
fba45db2 7268mention (struct breakpoint *b)
c906108c
SS
7269{
7270 int say_where = 0;
fa8a61dc 7271 struct cleanup *ui_out_chain;
79a45b7d
TT
7272 struct value_print_options opts;
7273
7274 get_user_print_options (&opts);
8b93c638 7275
3086aeae
DJ
7276 if (b->ops != NULL && b->ops->print_mention != NULL)
7277 b->ops->print_mention (b);
7278 else
7279 switch (b->type)
7280 {
7281 case bp_none:
3e43a32a
MS
7282 printf_filtered (_("(apparently deleted?) Eventpoint %d: "),
7283 b->number);
3086aeae
DJ
7284 break;
7285 case bp_watchpoint:
7286 ui_out_text (uiout, "Watchpoint ");
7287 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7288 ui_out_field_int (uiout, "number", b->number);
7289 ui_out_text (uiout, ": ");
fa8a61dc 7290 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7291 do_cleanups (ui_out_chain);
7292 break;
7293 case bp_hardware_watchpoint:
7294 ui_out_text (uiout, "Hardware watchpoint ");
7295 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
7296 ui_out_field_int (uiout, "number", b->number);
7297 ui_out_text (uiout, ": ");
fa8a61dc 7298 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7299 do_cleanups (ui_out_chain);
7300 break;
7301 case bp_read_watchpoint:
7302 ui_out_text (uiout, "Hardware read watchpoint ");
7303 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
7304 ui_out_field_int (uiout, "number", b->number);
7305 ui_out_text (uiout, ": ");
fa8a61dc 7306 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7307 do_cleanups (ui_out_chain);
7308 break;
7309 case bp_access_watchpoint:
7310 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
7311 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
7312 ui_out_field_int (uiout, "number", b->number);
7313 ui_out_text (uiout, ": ");
fa8a61dc 7314 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
7315 do_cleanups (ui_out_chain);
7316 break;
7317 case bp_breakpoint:
0e30163f 7318 case bp_gnu_ifunc_resolver:
3086aeae
DJ
7319 if (ui_out_is_mi_like_p (uiout))
7320 {
7321 say_where = 0;
7322 break;
7323 }
2cec12e5
AR
7324 if (b->disposition == disp_del)
7325 printf_filtered (_("Temporary breakpoint"));
7326 else
7327 printf_filtered (_("Breakpoint"));
7328 printf_filtered (_(" %d"), b->number);
0e30163f
JK
7329 if (b->type == bp_gnu_ifunc_resolver)
7330 printf_filtered (_(" at gnu-indirect-function resolver"));
3086aeae
DJ
7331 say_where = 1;
7332 break;
7333 case bp_hardware_breakpoint:
7334 if (ui_out_is_mi_like_p (uiout))
7335 {
7336 say_where = 0;
7337 break;
7338 }
a3f17187 7339 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
7340 say_where = 1;
7341 break;
1042e4c0
SS
7342 case bp_tracepoint:
7343 if (ui_out_is_mi_like_p (uiout))
7344 {
7345 say_where = 0;
7346 break;
7347 }
7348 printf_filtered (_("Tracepoint"));
7349 printf_filtered (_(" %d"), b->number);
7350 say_where = 1;
7351 break;
7a697b8d
SS
7352 case bp_fast_tracepoint:
7353 if (ui_out_is_mi_like_p (uiout))
7354 {
7355 say_where = 0;
7356 break;
7357 }
7358 printf_filtered (_("Fast tracepoint"));
7359 printf_filtered (_(" %d"), b->number);
7360 say_where = 1;
7361 break;
0fb4aa4b
PA
7362 case bp_static_tracepoint:
7363 if (ui_out_is_mi_like_p (uiout))
7364 {
7365 say_where = 0;
7366 break;
7367 }
7368 printf_filtered (_("Static tracepoint"));
7369 printf_filtered (_(" %d"), b->number);
7370 say_where = 1;
7371 break;
3086aeae
DJ
7372
7373 case bp_until:
7374 case bp_finish:
7375 case bp_longjmp:
7376 case bp_longjmp_resume:
186c406b
TT
7377 case bp_exception:
7378 case bp_exception_resume:
3086aeae 7379 case bp_step_resume:
2c03e5be 7380 case bp_hp_step_resume:
3086aeae 7381 case bp_call_dummy:
aa7d318d 7382 case bp_std_terminate:
3086aeae
DJ
7383 case bp_watchpoint_scope:
7384 case bp_shlib_event:
7385 case bp_thread_event:
7386 case bp_overlay_event:
4efc6507 7387 case bp_jit_event:
0fd8e87f 7388 case bp_longjmp_master:
aa7d318d 7389 case bp_std_terminate_master:
186c406b 7390 case bp_exception_master:
0e30163f 7391 case bp_gnu_ifunc_resolver_return:
3086aeae
DJ
7392 break;
7393 }
c906108c 7394
c906108c
SS
7395 if (say_where)
7396 {
a3f17187
AC
7397 /* i18n: cagney/2005-02-11: Below needs to be merged into a
7398 single string. */
0d381245 7399 if (b->loc == NULL)
c906108c 7400 {
a3f17187 7401 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
7402 }
7403 else
7404 {
79a45b7d 7405 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
7406 {
7407 printf_filtered (" at ");
5af949e3
UW
7408 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
7409 gdb_stdout);
0101ce28
JJ
7410 }
7411 if (b->source_file)
7412 printf_filtered (": file %s, line %d.",
7413 b->source_file, b->line_number);
0d381245
VP
7414
7415 if (b->loc->next)
7416 {
7417 struct bp_location *loc = b->loc;
7418 int n = 0;
7419 for (; loc; loc = loc->next)
7420 ++n;
7421 printf_filtered (" (%d locations)", n);
7422 }
7423
c906108c 7424 }
c906108c 7425 }
9dc5e2a9 7426 if (ui_out_is_mi_like_p (uiout))
fb40c209 7427 return;
c906108c
SS
7428 printf_filtered ("\n");
7429}
c906108c 7430\f
c5aa993b 7431
0d381245 7432static struct bp_location *
39d61571 7433add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7434 const struct symtab_and_line *sal)
7435{
7436 struct bp_location *loc, **tmp;
7437
39d61571 7438 loc = allocate_bp_location (b);
0d381245
VP
7439 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7440 ;
7441 *tmp = loc;
a6d9a66e
UW
7442 loc->gdbarch = get_sal_arch (*sal);
7443 if (!loc->gdbarch)
7444 loc->gdbarch = b->gdbarch;
0d381245 7445 loc->requested_address = sal->pc;
a6d9a66e
UW
7446 loc->address = adjust_breakpoint_address (loc->gdbarch,
7447 loc->requested_address, b->type);
6c95b8df
PA
7448 loc->pspace = sal->pspace;
7449 gdb_assert (loc->pspace != NULL);
0d381245
VP
7450 loc->section = sal->section;
7451
0e30163f
JK
7452 set_breakpoint_location_function (loc,
7453 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7454 return loc;
7455}
514f746b
AR
7456\f
7457
7458/* Return 1 if LOC is pointing to a permanent breakpoint,
7459 return 0 otherwise. */
7460
7461static int
7462bp_loc_is_permanent (struct bp_location *loc)
7463{
7464 int len;
7465 CORE_ADDR addr;
7466 const gdb_byte *brk;
7467 gdb_byte *target_mem;
939c61fa
JK
7468 struct cleanup *cleanup;
7469 int retval = 0;
514f746b
AR
7470
7471 gdb_assert (loc != NULL);
7472
7473 addr = loc->address;
a6d9a66e 7474 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7475
939c61fa
JK
7476 /* Software breakpoints unsupported? */
7477 if (brk == NULL)
7478 return 0;
7479
514f746b
AR
7480 target_mem = alloca (len);
7481
939c61fa
JK
7482 /* Enable the automatic memory restoration from breakpoints while
7483 we read the memory. Otherwise we could say about our temporary
7484 breakpoints they are permanent. */
6c95b8df
PA
7485 cleanup = save_current_space_and_thread ();
7486
7487 switch_to_program_space_and_thread (loc->pspace);
7488 make_show_memory_breakpoints_cleanup (0);
939c61fa 7489
514f746b
AR
7490 if (target_read_memory (loc->address, target_mem, len) == 0
7491 && memcmp (target_mem, brk, len) == 0)
939c61fa 7492 retval = 1;
514f746b 7493
939c61fa
JK
7494 do_cleanups (cleanup);
7495
7496 return retval;
514f746b
AR
7497}
7498
7499
c3f6f71d 7500
018d34a4
VP
7501/* Create a breakpoint with SAL as location. Use ADDR_STRING
7502 as textual description of the location, and COND_STRING
db107f19 7503 as condition expression. */
018d34a4
VP
7504
7505static void
8cdf0e15
VP
7506create_breakpoint_sal (struct gdbarch *gdbarch,
7507 struct symtabs_and_lines sals, char *addr_string,
7508 char *cond_string,
7509 enum bptype type, enum bpdisp disposition,
7510 int thread, int task, int ignore_count,
84f4c1fe 7511 struct breakpoint_ops *ops, int from_tty,
56435ebe 7512 int enabled, int internal, int display_canonical)
018d34a4 7513{
0d381245
VP
7514 struct breakpoint *b = NULL;
7515 int i;
018d34a4
VP
7516
7517 if (type == bp_hardware_breakpoint)
7518 {
7519 int i = hw_breakpoint_used_count ();
7520 int target_resources_ok =
d92524f1 7521 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7522 i + 1, 0);
7523 if (target_resources_ok == 0)
7524 error (_("No hardware breakpoint support in the target."));
7525 else if (target_resources_ok < 0)
7526 error (_("Hardware breakpoints used exceeds limit."));
7527 }
7528
6c95b8df
PA
7529 gdb_assert (sals.nelts > 0);
7530
0d381245
VP
7531 for (i = 0; i < sals.nelts; ++i)
7532 {
7533 struct symtab_and_line sal = sals.sals[i];
7534 struct bp_location *loc;
7535
7536 if (from_tty)
5af949e3
UW
7537 {
7538 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7539 if (!loc_gdbarch)
7540 loc_gdbarch = gdbarch;
7541
7542 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7543 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7544 }
0d381245
VP
7545
7546 if (i == 0)
7547 {
a6d9a66e 7548 b = set_raw_breakpoint (gdbarch, sal, type);
84f4c1fe 7549 set_breakpoint_number (internal, b);
0d381245 7550 b->thread = thread;
4a306c9a 7551 b->task = task;
018d34a4 7552
0d381245
VP
7553 b->cond_string = cond_string;
7554 b->ignore_count = ignore_count;
41447f92 7555 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7556 b->disposition = disposition;
6c95b8df
PA
7557 b->pspace = sals.sals[0].pspace;
7558
0fb4aa4b
PA
7559 if (type == bp_static_tracepoint)
7560 {
7561 struct static_tracepoint_marker marker;
7562
7563 if (is_marker_spec (addr_string))
7564 {
7565 /* We already know the marker exists, otherwise, we
7566 wouldn't see a sal for it. */
7567 char *p = &addr_string[3];
7568 char *endp;
7569 char *marker_str;
7570 int i;
7571
e9cafbcc 7572 p = skip_spaces (p);
0fb4aa4b 7573
e9cafbcc 7574 endp = skip_to_space (p);
0fb4aa4b
PA
7575
7576 marker_str = savestring (p, endp - p);
7577 b->static_trace_marker_id = marker_str;
7578
3e43a32a
MS
7579 printf_filtered (_("Probed static tracepoint "
7580 "marker \"%s\"\n"),
0fb4aa4b
PA
7581 b->static_trace_marker_id);
7582 }
7583 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7584 {
7585 b->static_trace_marker_id = xstrdup (marker.str_id);
7586 release_static_tracepoint_marker (&marker);
7587
3e43a32a
MS
7588 printf_filtered (_("Probed static tracepoint "
7589 "marker \"%s\"\n"),
0fb4aa4b
PA
7590 b->static_trace_marker_id);
7591 }
7592 else
3e43a32a
MS
7593 warning (_("Couldn't determine the static "
7594 "tracepoint marker to probe"));
0fb4aa4b
PA
7595 }
7596
6c95b8df 7597 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7598 && (b->type == bp_breakpoint
7599 || b->type == bp_hardware_breakpoint))
7600 b->enable_state = bp_startup_disabled;
7601
0d381245
VP
7602 loc = b->loc;
7603 }
7604 else
018d34a4 7605 {
39d61571 7606 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7607 }
7608
514f746b
AR
7609 if (bp_loc_is_permanent (loc))
7610 make_breakpoint_permanent (b);
7611
0d381245
VP
7612 if (b->cond_string)
7613 {
7614 char *arg = b->cond_string;
d32a6982 7615 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7616 if (*arg)
db107f19 7617 error (_("Garbage %s follows condition"), arg);
018d34a4 7618 }
0d381245 7619 }
018d34a4 7620
56435ebe 7621 b->display_canonical = display_canonical;
018d34a4
VP
7622 if (addr_string)
7623 b->addr_string = addr_string;
7624 else
7625 /* addr_string has to be used or breakpoint_re_set will delete
7626 me. */
5af949e3
UW
7627 b->addr_string
7628 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
018d34a4 7629
604133b5 7630 b->ops = ops;
8d3788bd
VP
7631 /* Do not mention breakpoints with a negative number, but do
7632 notify observers. */
7633 if (!internal)
84f4c1fe 7634 mention (b);
8d3788bd 7635 observer_notify_breakpoint_created (b);
018d34a4
VP
7636}
7637
ed0616c6
VP
7638/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7639 elements to fill the void space. */
2c0b251b
PA
7640static void
7641remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7642{
7643 int i = index_to_remove+1;
7644 int last_index = sal->nelts-1;
7645
7646 for (;i <= last_index; ++i)
7647 sal->sals[i-1] = sal->sals[i];
7648
7649 --(sal->nelts);
7650}
7651
6c95b8df
PA
7652/* If appropriate, obtains all sals that correspond to the same file
7653 and line as SAL, in all program spaces. Users debugging with IDEs,
7654 will want to set a breakpoint at foo.c:line, and not really care
7655 about program spaces. This is done only if SAL does not have
7656 explicit PC and has line and file information. If we got just a
7657 single expanded sal, return the original.
ed0616c6 7658
6c95b8df
PA
7659 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7660 which the name of enclosing function is different from SAL. This
7661 makes sure that if we have breakpoint originally set in template
7662 instantiation, say foo<int>(), we won't expand SAL to locations at
7663 the same line in all existing instantiations of 'foo'. */
ed0616c6 7664
2c0b251b 7665static struct symtabs_and_lines
ed0616c6
VP
7666expand_line_sal_maybe (struct symtab_and_line sal)
7667{
7668 struct symtabs_and_lines expanded;
7669 CORE_ADDR original_pc = sal.pc;
7670 char *original_function = NULL;
7671 int found;
7672 int i;
6c95b8df 7673 struct cleanup *old_chain;
ed0616c6
VP
7674
7675 /* If we have explicit pc, don't expand.
7676 If we have no line number, we can't expand. */
7677 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7678 {
7679 expanded.nelts = 1;
7680 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7681 expanded.sals[0] = sal;
7682 return expanded;
7683 }
7684
7685 sal.pc = 0;
6c95b8df
PA
7686
7687 old_chain = save_current_space_and_thread ();
7688
7689 switch_to_program_space_and_thread (sal.pspace);
7690
ed0616c6 7691 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7692
7693 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7694 expanded = expand_line_sal (sal);
6c95b8df 7695
ed0616c6
VP
7696 if (expanded.nelts == 1)
7697 {
3dba1c98
JB
7698 /* We had one sal, we got one sal. Return that sal, adjusting it
7699 past the function prologue if necessary. */
ed0616c6
VP
7700 xfree (expanded.sals);
7701 expanded.nelts = 1;
7702 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7703 sal.pc = original_pc;
7704 expanded.sals[0] = sal;
3dba1c98 7705 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7706 do_cleanups (old_chain);
ed0616c6
VP
7707 return expanded;
7708 }
7709
7710 if (!sal.explicit_line)
7711 {
7712 CORE_ADDR func_addr, func_end;
7713 for (i = 0; i < expanded.nelts; ++i)
7714 {
7715 CORE_ADDR pc = expanded.sals[i].pc;
7716 char *this_function;
6c95b8df
PA
7717
7718 /* We need to switch threads as well since we're about to
7719 read memory. */
7720 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7721
ed0616c6
VP
7722 if (find_pc_partial_function (pc, &this_function,
7723 &func_addr, &func_end))
7724 {
059fb39f
PM
7725 if (this_function
7726 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7727 {
7728 remove_sal (&expanded, i);
7729 --i;
7730 }
ed0616c6
VP
7731 }
7732 }
7733 }
059acae7
UW
7734
7735 /* Skip the function prologue if necessary. */
7736 for (i = 0; i < expanded.nelts; ++i)
7737 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7738
6c95b8df
PA
7739 do_cleanups (old_chain);
7740
ed0616c6
VP
7741 if (expanded.nelts <= 1)
7742 {
4a64f543
MS
7743 /* This is un ugly workaround. If we get zero expanded sals
7744 then something is really wrong. Fix that by returning the
7745 original sal. */
7746
ed0616c6
VP
7747 xfree (expanded.sals);
7748 expanded.nelts = 1;
7749 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7750 sal.pc = original_pc;
7751 expanded.sals[0] = sal;
7752 return expanded;
7753 }
7754
7755 if (original_pc)
7756 {
7757 found = 0;
7758 for (i = 0; i < expanded.nelts; ++i)
7759 if (expanded.sals[i].pc == original_pc)
7760 {
7761 found = 1;
7762 break;
7763 }
7764 gdb_assert (found);
7765 }
7766
7767 return expanded;
7768}
7769
018d34a4
VP
7770/* Add SALS.nelts breakpoints to the breakpoint table. For each
7771 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7772 value. COND_STRING, if not NULL, specified the condition to be
7773 used for all breakpoints. Essentially the only case where
7774 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7775 function. In that case, it's still not possible to specify
7776 separate conditions for different overloaded functions, so
7777 we take just a single condition string.
7778
c3f6f71d 7779 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7780 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7781 array contents). If the function fails (error() is called), the
7782 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7783 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7784
7785static void
8cdf0e15 7786create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7787 struct symtabs_and_lines sals,
7788 struct linespec_result *canonical,
8cdf0e15
VP
7789 char *cond_string,
7790 enum bptype type, enum bpdisp disposition,
7791 int thread, int task, int ignore_count,
7792 struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7793 int enabled, int internal)
c906108c 7794{
018d34a4 7795 int i;
cc59ec59 7796
018d34a4 7797 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7798 {
ed0616c6
VP
7799 struct symtabs_and_lines expanded =
7800 expand_line_sal_maybe (sals.sals[i]);
0d381245 7801
7efd8fc2 7802 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7803 cond_string, type, disposition,
84f4c1fe 7804 thread, task, ignore_count, ops,
56435ebe
TT
7805 from_tty, enabled, internal,
7806 canonical->special_display);
c3f6f71d 7807 }
c3f6f71d 7808}
c906108c 7809
9998af43 7810/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7811 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7812 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7813 address strings. ADDRESS points to the end of the SAL.
7814
7815 The array and the line spec strings are allocated on the heap, it is
7816 the caller's responsibility to free them. */
c906108c 7817
b9362cc7 7818static void
c3f6f71d
JM
7819parse_breakpoint_sals (char **address,
7820 struct symtabs_and_lines *sals,
58438ac1 7821 struct linespec_result *canonical)
c3f6f71d
JM
7822{
7823 char *addr_start = *address;
cc59ec59 7824
c3f6f71d 7825 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7826 breakpoint. */
c3f6f71d
JM
7827 if ((*address) == NULL
7828 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
7829 {
7830 if (default_breakpoint_valid)
7831 {
c3f6f71d 7832 struct symtab_and_line sal;
cc59ec59 7833
4a64f543 7834 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7835 sals->sals = (struct symtab_and_line *)
c906108c
SS
7836 xmalloc (sizeof (struct symtab_and_line));
7837 sal.pc = default_breakpoint_address;
7838 sal.line = default_breakpoint_line;
7839 sal.symtab = default_breakpoint_symtab;
6c95b8df 7840 sal.pspace = default_breakpoint_pspace;
c5aa993b 7841 sal.section = find_pc_overlay (sal.pc);
00903456 7842
4a64f543
MS
7843 /* "break" without arguments is equivalent to "break *PC"
7844 where PC is the default_breakpoint_address. So make sure
7845 to set sal.explicit_pc to prevent GDB from trying to
7846 expand the list of sals to include all other instances
7847 with the same symtab and line. */
00903456
JK
7848 sal.explicit_pc = 1;
7849
c3f6f71d
JM
7850 sals->sals[0] = sal;
7851 sals->nelts = 1;
c906108c
SS
7852 }
7853 else
8a3fe4f8 7854 error (_("No default breakpoint address now."));
c906108c
SS
7855 }
7856 else
7857 {
c906108c 7858 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7859 current_source_symtab (which is decode_line_1's default).
7860 This should produce the results we want almost all of the
7861 time while leaving default_breakpoint_* alone.
7862
1aeae86e
AF
7863 ObjC: However, don't match an Objective-C method name which
7864 may have a '+' or '-' succeeded by a '[' */
0378c332 7865
c214a6fd 7866 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7867
c906108c 7868 if (default_breakpoint_valid
0378c332 7869 && (!cursal.symtab
1aeae86e
AF
7870 || ((strchr ("+-", (*address)[0]) != NULL)
7871 && ((*address)[1] != '['))))
c3f6f71d 7872 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
58438ac1 7873 default_breakpoint_line, canonical);
c906108c 7874 else
0101ce28 7875 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7876 canonical);
c906108c 7877 }
4a64f543 7878 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7879 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7880 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7881 if (addr_start != (*address))
c906108c 7882 {
c3f6f71d 7883 int i;
cc59ec59 7884
c3f6f71d 7885 for (i = 0; i < sals->nelts; i++)
c906108c 7886 {
4a64f543 7887 /* Add the string if not present. */
7efd8fc2
TT
7888 if (canonical->canonical[i] == NULL)
7889 canonical->canonical[i] = savestring (addr_start,
7890 (*address) - addr_start);
c906108c
SS
7891 }
7892 }
c3f6f71d 7893}
c906108c 7894
c906108c 7895
c3f6f71d 7896/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7897 inserted as a breakpoint. If it can't throw an error. */
c906108c 7898
b9362cc7 7899static void
23e7acfb 7900breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7901{
7902 int i;
cc59ec59 7903
c3f6f71d 7904 for (i = 0; i < sals->nelts; i++)
ee53e872 7905 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7906}
7907
7a697b8d
SS
7908/* Fast tracepoints may have restrictions on valid locations. For
7909 instance, a fast tracepoint using a jump instead of a trap will
7910 likely have to overwrite more bytes than a trap would, and so can
7911 only be placed where the instruction is longer than the jump, or a
7912 multi-instruction sequence does not have a jump into the middle of
7913 it, etc. */
7914
7915static void
7916check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7917 struct symtabs_and_lines *sals)
7918{
7919 int i, rslt;
7920 struct symtab_and_line *sal;
7921 char *msg;
7922 struct cleanup *old_chain;
7923
7924 for (i = 0; i < sals->nelts; i++)
7925 {
7926 sal = &sals->sals[i];
7927
7928 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7929 NULL, &msg);
7930 old_chain = make_cleanup (xfree, msg);
7931
7932 if (!rslt)
7933 error (_("May not have a fast tracepoint at 0x%s%s"),
7934 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7935
7936 do_cleanups (old_chain);
7937 }
7938}
7939
018d34a4
VP
7940/* Given TOK, a string specification of condition and thread, as
7941 accepted by the 'break' command, extract the condition
7942 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7943 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7944 If no condition is found, *COND_STRING is set to NULL.
7945 If no thread is found, *THREAD is set to -1. */
7946static void
7947find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7948 char **cond_string, int *thread, int *task)
018d34a4
VP
7949{
7950 *cond_string = NULL;
7951 *thread = -1;
7952 while (tok && *tok)
7953 {
7954 char *end_tok;
7955 int toklen;
7956 char *cond_start = NULL;
7957 char *cond_end = NULL;
cc59ec59 7958
e9cafbcc 7959 tok = skip_spaces (tok);
018d34a4 7960
e9cafbcc 7961 end_tok = skip_to_space (tok);
018d34a4
VP
7962
7963 toklen = end_tok - tok;
7964
7965 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7966 {
f7545552
TT
7967 struct expression *expr;
7968
018d34a4 7969 tok = cond_start = end_tok + 1;
f7545552
TT
7970 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7971 xfree (expr);
018d34a4
VP
7972 cond_end = tok;
7973 *cond_string = savestring (cond_start,
7974 cond_end - cond_start);
7975 }
7976 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7977 {
7978 char *tmptok;
7979
7980 tok = end_tok + 1;
7981 tmptok = tok;
7982 *thread = strtol (tok, &tok, 0);
7983 if (tok == tmptok)
7984 error (_("Junk after thread keyword."));
7985 if (!valid_thread_id (*thread))
7986 error (_("Unknown thread %d."), *thread);
7987 }
4a306c9a
JB
7988 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7989 {
7990 char *tmptok;
7991
7992 tok = end_tok + 1;
7993 tmptok = tok;
7994 *task = strtol (tok, &tok, 0);
7995 if (tok == tmptok)
7996 error (_("Junk after task keyword."));
7997 if (!valid_task_id (*task))
b6199126 7998 error (_("Unknown task %d."), *task);
4a306c9a 7999 }
018d34a4
VP
8000 else
8001 error (_("Junk at end of arguments."));
8002 }
8003}
8004
0fb4aa4b
PA
8005/* Decode a static tracepoint marker spec. */
8006
8007static struct symtabs_and_lines
8008decode_static_tracepoint_spec (char **arg_p)
8009{
8010 VEC(static_tracepoint_marker_p) *markers = NULL;
8011 struct symtabs_and_lines sals;
8012 struct symtab_and_line sal;
8013 struct symbol *sym;
8014 struct cleanup *old_chain;
8015 char *p = &(*arg_p)[3];
8016 char *endp;
8017 char *marker_str;
8018 int i;
8019
e9cafbcc 8020 p = skip_spaces (p);
0fb4aa4b 8021
e9cafbcc 8022 endp = skip_to_space (p);
0fb4aa4b
PA
8023
8024 marker_str = savestring (p, endp - p);
8025 old_chain = make_cleanup (xfree, marker_str);
8026
8027 markers = target_static_tracepoint_markers_by_strid (marker_str);
8028 if (VEC_empty(static_tracepoint_marker_p, markers))
8029 error (_("No known static tracepoint marker named %s"), marker_str);
8030
8031 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8032 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
8033
8034 for (i = 0; i < sals.nelts; i++)
8035 {
8036 struct static_tracepoint_marker *marker;
8037
8038 marker = VEC_index (static_tracepoint_marker_p, markers, i);
8039
8040 init_sal (&sals.sals[i]);
8041
8042 sals.sals[i] = find_pc_line (marker->address, 0);
8043 sals.sals[i].pc = marker->address;
8044
8045 release_static_tracepoint_marker (marker);
8046 }
8047
8048 do_cleanups (old_chain);
8049
8050 *arg_p = endp;
8051 return sals;
8052}
8053
fd9b8c24
PA
8054/* Set a breakpoint. This function is shared between CLI and MI
8055 functions for setting a breakpoint. This function has two major
8056 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
8057 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
8058 breakpoint location, address and thread. Otherwise, ARG is just
8059 the location of breakpoint, with condition and thread specified by
8060 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
8061 the breakpoint number will be allocated from the internal
8062 breakpoint count. Returns true if any breakpoint was created;
8063 false otherwise. */
0101ce28 8064
8cdf0e15
VP
8065int
8066create_breakpoint (struct gdbarch *gdbarch,
8067 char *arg, char *cond_string, int thread,
8068 int parse_condition_and_thread,
0fb4aa4b 8069 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
8070 int ignore_count,
8071 enum auto_boolean pending_break_support,
8072 struct breakpoint_ops *ops,
84f4c1fe 8073 int from_tty, int enabled, int internal)
c3f6f71d 8074{
b78a6381 8075 volatile struct gdb_exception e;
c3f6f71d 8076 struct symtabs_and_lines sals;
0101ce28 8077 struct symtab_and_line pending_sal;
0101ce28 8078 char *copy_arg;
c3f6f71d 8079 char *addr_start = arg;
7efd8fc2 8080 struct linespec_result canonical;
c3f6f71d 8081 struct cleanup *old_chain;
80c99de1 8082 struct cleanup *bkpt_chain = NULL;
05ff989b 8083 int i;
0101ce28 8084 int pending = 0;
4a306c9a 8085 int task = 0;
86b17b60 8086 int prev_bkpt_count = breakpoint_count;
c3f6f71d 8087
c3f6f71d
JM
8088 sals.sals = NULL;
8089 sals.nelts = 0;
7efd8fc2 8090 init_linespec_result (&canonical);
c3f6f71d 8091
0fb4aa4b
PA
8092 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
8093 {
8094 int i;
8095
8096 sals = decode_static_tracepoint_spec (&arg);
8097
8098 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 8099 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 8100 for (i = 0; i < sals.nelts; i++)
7efd8fc2 8101 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
8102 goto done;
8103 }
8104
b78a6381
TT
8105 TRY_CATCH (e, RETURN_MASK_ALL)
8106 {
58438ac1 8107 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 8108 }
0101ce28
JJ
8109
8110 /* If caller is interested in rc value from parse, set value. */
05ff989b 8111 switch (e.reason)
0101ce28 8112 {
05ff989b 8113 case RETURN_QUIT:
98deb0da 8114 throw_exception (e);
05ff989b
AC
8115 case RETURN_ERROR:
8116 switch (e.error)
0101ce28 8117 {
05ff989b 8118 case NOT_FOUND_ERROR:
0101ce28 8119
05ff989b
AC
8120 /* If pending breakpoint support is turned off, throw
8121 error. */
fa8d40ab
JJ
8122
8123 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
8124 throw_exception (e);
8125
8126 exception_print (gdb_stderr, e);
fa8d40ab 8127
05ff989b
AC
8128 /* If pending breakpoint support is auto query and the user
8129 selects no, then simply return the error code. */
059fb39f 8130 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
8131 && !nquery (_("Make breakpoint pending on "
8132 "future shared library load? ")))
fd9b8c24 8133 return 0;
fa8d40ab 8134
05ff989b
AC
8135 /* At this point, either the user was queried about setting
8136 a pending breakpoint and selected yes, or pending
8137 breakpoint behavior is on and thus a pending breakpoint
8138 is defaulted on behalf of the user. */
0101ce28 8139 copy_arg = xstrdup (addr_start);
7efd8fc2 8140 canonical.canonical = &copy_arg;
0101ce28
JJ
8141 sals.nelts = 1;
8142 sals.sals = &pending_sal;
8143 pending_sal.pc = 0;
8144 pending = 1;
05ff989b
AC
8145 break;
8146 default:
98deb0da 8147 throw_exception (e);
0101ce28 8148 }
2abae994 8149 break;
05ff989b
AC
8150 default:
8151 if (!sals.nelts)
fd9b8c24 8152 return 0;
0101ce28 8153 }
c3f6f71d 8154
0fb4aa4b
PA
8155 done:
8156
4a64f543 8157 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
8158 old_chain = make_cleanup (null_cleanup, 0);
8159
0101ce28
JJ
8160 if (!pending)
8161 {
8162 /* Make sure that all storage allocated to SALS gets freed. */
8163 make_cleanup (xfree, sals.sals);
8164
7efd8fc2
TT
8165 /* Cleanup the canonical array but not its contents. */
8166 make_cleanup (xfree, canonical.canonical);
0101ce28 8167 }
c3f6f71d 8168
c3f6f71d
JM
8169 /* ----------------------------- SNIP -----------------------------
8170 Anything added to the cleanup chain beyond this point is assumed
8171 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
8172 then the memory is not reclaimed. */
8173 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 8174
7efd8fc2 8175 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 8176 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
8177 for (i = 0; i < sals.nelts; i++)
8178 {
7efd8fc2
TT
8179 if (canonical.canonical[i] != NULL)
8180 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
8181 }
8182
8183 /* Resolve all line numbers to PC's and verify that the addresses
8184 are ok for the target. */
0101ce28 8185 if (!pending)
23e7acfb 8186 breakpoint_sals_to_pc (&sals);
c3f6f71d 8187
7a697b8d
SS
8188 /* Fast tracepoints may have additional restrictions on location. */
8189 if (type_wanted == bp_fast_tracepoint)
8190 check_fast_tracepoint_sals (gdbarch, &sals);
8191
c3f6f71d
JM
8192 /* Verify that condition can be parsed, before setting any
8193 breakpoints. Allocate a separate condition expression for each
4a64f543 8194 breakpoint. */
0101ce28 8195 if (!pending)
c3f6f71d 8196 {
2f069f6f 8197 if (parse_condition_and_thread)
72b2ff0e
VP
8198 {
8199 /* Here we only parse 'arg' to separate condition
8200 from thread number, so parsing in context of first
8201 sal is OK. When setting the breakpoint we'll
8202 re-parse it in context of each sal. */
8203 cond_string = NULL;
8204 thread = -1;
4a306c9a
JB
8205 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
8206 &thread, &task);
72b2ff0e
VP
8207 if (cond_string)
8208 make_cleanup (xfree, cond_string);
8209 }
2f069f6f 8210 else
72b2ff0e
VP
8211 {
8212 /* Create a private copy of condition string. */
8213 if (cond_string)
8214 {
8215 cond_string = xstrdup (cond_string);
8216 make_cleanup (xfree, cond_string);
8217 }
8218 }
0fb4aa4b
PA
8219
8220 /* If the user is creating a static tracepoint by marker id
8221 (strace -m MARKER_ID), then store the sals index, so that
8222 breakpoint_re_set can try to match up which of the newly
8223 found markers corresponds to this one, and, don't try to
8224 expand multiple locations for each sal, given than SALS
8225 already should contain all sals for MARKER_ID. */
8226 if (type_wanted == bp_static_tracepoint
7efd8fc2 8227 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
8228 {
8229 int i;
8230
8231 for (i = 0; i < sals.nelts; ++i)
8232 {
8233 struct symtabs_and_lines expanded;
8234 struct breakpoint *tp;
8235 struct cleanup *old_chain;
8236
8237 expanded.nelts = 1;
8238 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
8239 expanded.sals[0] = sals.sals[i];
8240 old_chain = make_cleanup (xfree, expanded.sals);
8241
7efd8fc2 8242 create_breakpoint_sal (gdbarch, expanded, canonical.canonical[i],
0fb4aa4b
PA
8243 cond_string, type_wanted,
8244 tempflag ? disp_del : disp_donttouch,
8245 thread, task, ignore_count, ops,
56435ebe
TT
8246 from_tty, enabled, internal,
8247 canonical.special_display);
0fb4aa4b
PA
8248
8249 do_cleanups (old_chain);
8250
8251 /* Get the tracepoint we just created. */
84f4c1fe
PM
8252 if (internal)
8253 tp = get_breakpoint (internal_breakpoint_number);
8254 else
8255 tp = get_breakpoint (breakpoint_count);
0fb4aa4b
PA
8256 gdb_assert (tp != NULL);
8257
8258 /* Given that its possible to have multiple markers with
8259 the same string id, if the user is creating a static
8260 tracepoint by marker id ("strace -m MARKER_ID"), then
8261 store the sals index, so that breakpoint_re_set can
8262 try to match up which of the newly found markers
8263 corresponds to this one */
8264 tp->static_trace_marker_id_idx = i;
8265 }
8266 }
8267 else
7efd8fc2 8268 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
8269 type_wanted,
8270 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 8271 thread, task, ignore_count, ops, from_tty,
84f4c1fe 8272 enabled, internal);
c906108c 8273 }
0101ce28
JJ
8274 else
8275 {
0101ce28
JJ
8276 struct breakpoint *b;
8277
0101ce28
JJ
8278 make_cleanup (xfree, copy_arg);
8279
a6d9a66e 8280 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
84f4c1fe 8281 set_breakpoint_number (internal, b);
72b2ff0e 8282 b->thread = -1;
7efd8fc2 8283 b->addr_string = canonical.canonical[0];
72b2ff0e 8284 b->cond_string = NULL;
0101ce28 8285 b->ignore_count = ignore_count;
0101ce28 8286 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 8287 b->condition_not_parsed = 1;
604133b5 8288 b->ops = ops;
41447f92 8289 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 8290 b->pspace = current_program_space;
84f4c1fe 8291 b->py_bp_object = NULL;
74960c60 8292
6c95b8df 8293 if (enabled && b->pspace->executing_startup
8bea4e01
UW
8294 && (b->type == bp_breakpoint
8295 || b->type == bp_hardware_breakpoint))
8296 b->enable_state = bp_startup_disabled;
8297
8d3788bd 8298 if (!internal)
84f4c1fe
PM
8299 /* Do not mention breakpoints with a negative number,
8300 but do notify observers. */
8d3788bd
VP
8301 mention (b);
8302 observer_notify_breakpoint_created (b);
0101ce28
JJ
8303 }
8304
c3f6f71d 8305 if (sals.nelts > 1)
95a42b64 8306 {
3e43a32a
MS
8307 warning (_("Multiple breakpoints were set.\nUse the "
8308 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8309 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8310 }
8311
80c99de1
PA
8312 /* That's it. Discard the cleanups for data inserted into the
8313 breakpoint. */
8314 discard_cleanups (bkpt_chain);
8315 /* But cleanup everything else. */
c3f6f71d 8316 do_cleanups (old_chain);
217dc9e2 8317
80c99de1 8318 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8319 update_global_location_list (1);
fd9b8c24
PA
8320
8321 return 1;
c3f6f71d 8322}
c906108c 8323
72b2ff0e
VP
8324/* Set a breakpoint.
8325 ARG is a string describing breakpoint address,
8326 condition, and thread.
8327 FLAG specifies if a breakpoint is hardware on,
8328 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8329 and BP_TEMPFLAG. */
8330
98deb0da 8331static void
72b2ff0e 8332break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8333{
72b2ff0e 8334 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8335 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8336 ? bp_hardware_breakpoint
8337 : bp_breakpoint);
c3f6f71d 8338
8cdf0e15
VP
8339 create_breakpoint (get_current_arch (),
8340 arg,
8341 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8342 tempflag, type_wanted,
8cdf0e15
VP
8343 0 /* Ignore count */,
8344 pending_break_support,
8345 NULL /* breakpoint_ops */,
8346 from_tty,
84f4c1fe
PM
8347 1 /* enabled */,
8348 0 /* internal */);
c906108c
SS
8349}
8350
72b2ff0e 8351
c906108c
SS
8352/* Helper function for break_command_1 and disassemble_command. */
8353
8354void
fba45db2 8355resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8356{
8357 CORE_ADDR pc;
8358
8359 if (sal->pc == 0 && sal->symtab != NULL)
8360 {
8361 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8362 error (_("No line %d in file \"%s\"."),
c906108c
SS
8363 sal->line, sal->symtab->filename);
8364 sal->pc = pc;
6a048695 8365
4a64f543
MS
8366 /* If this SAL corresponds to a breakpoint inserted using a line
8367 number, then skip the function prologue if necessary. */
6a048695 8368 if (sal->explicit_line)
059acae7 8369 skip_prologue_sal (sal);
c906108c
SS
8370 }
8371
8372 if (sal->section == 0 && sal->symtab != NULL)
8373 {
8374 struct blockvector *bv;
c5aa993b
JM
8375 struct block *b;
8376 struct symbol *sym;
c906108c 8377
801e3a5b 8378 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8379 if (bv != NULL)
8380 {
7f0df278 8381 sym = block_linkage_function (b);
c906108c
SS
8382 if (sym != NULL)
8383 {
8384 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8385 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8386 }
8387 else
8388 {
4a64f543
MS
8389 /* It really is worthwhile to have the section, so we'll
8390 just have to look harder. This case can be executed
8391 if we have line numbers but no functions (as can
8392 happen in assembly source). */
c906108c 8393
c5aa993b 8394 struct minimal_symbol *msym;
6c95b8df
PA
8395 struct cleanup *old_chain = save_current_space_and_thread ();
8396
8397 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8398
8399 msym = lookup_minimal_symbol_by_pc (sal->pc);
8400 if (msym)
714835d5 8401 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8402
8403 do_cleanups (old_chain);
c906108c
SS
8404 }
8405 }
8406 }
8407}
8408
8409void
fba45db2 8410break_command (char *arg, int from_tty)
c906108c 8411{
db107f19 8412 break_command_1 (arg, 0, from_tty);
c906108c
SS
8413}
8414
c906108c 8415void
fba45db2 8416tbreak_command (char *arg, int from_tty)
c906108c 8417{
db107f19 8418 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8419}
8420
c906108c 8421static void
fba45db2 8422hbreak_command (char *arg, int from_tty)
c906108c 8423{
db107f19 8424 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8425}
8426
8427static void
fba45db2 8428thbreak_command (char *arg, int from_tty)
c906108c 8429{
db107f19 8430 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8431}
8432
8433static void
fba45db2 8434stop_command (char *arg, int from_tty)
c906108c 8435{
a3f17187 8436 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8437Usage: stop in <function | address>\n\
a3f17187 8438 stop at <line>\n"));
c906108c
SS
8439}
8440
8441static void
fba45db2 8442stopin_command (char *arg, int from_tty)
c906108c
SS
8443{
8444 int badInput = 0;
8445
c5aa993b 8446 if (arg == (char *) NULL)
c906108c
SS
8447 badInput = 1;
8448 else if (*arg != '*')
8449 {
8450 char *argptr = arg;
8451 int hasColon = 0;
8452
4a64f543 8453 /* Look for a ':'. If this is a line number specification, then
53a5351d 8454 say it is bad, otherwise, it should be an address or
4a64f543 8455 function/method name. */
c906108c 8456 while (*argptr && !hasColon)
c5aa993b
JM
8457 {
8458 hasColon = (*argptr == ':');
8459 argptr++;
8460 }
c906108c
SS
8461
8462 if (hasColon)
c5aa993b 8463 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8464 else
c5aa993b 8465 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8466 }
8467
8468 if (badInput)
a3f17187 8469 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8470 else
db107f19 8471 break_command_1 (arg, 0, from_tty);
c906108c
SS
8472}
8473
8474static void
fba45db2 8475stopat_command (char *arg, int from_tty)
c906108c
SS
8476{
8477 int badInput = 0;
8478
c5aa993b 8479 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8480 badInput = 1;
8481 else
8482 {
8483 char *argptr = arg;
8484 int hasColon = 0;
8485
4a64f543
MS
8486 /* Look for a ':'. If there is a '::' then get out, otherwise
8487 it is probably a line number. */
c906108c 8488 while (*argptr && !hasColon)
c5aa993b
JM
8489 {
8490 hasColon = (*argptr == ':');
8491 argptr++;
8492 }
c906108c
SS
8493
8494 if (hasColon)
c5aa993b 8495 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8496 else
c5aa993b 8497 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8498 }
8499
8500 if (badInput)
a3f17187 8501 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8502 else
db107f19 8503 break_command_1 (arg, 0, from_tty);
c906108c
SS
8504}
8505
f1310107
TJB
8506/* Implement the "breakpoint_hit" breakpoint_ops method for
8507 ranged breakpoints. */
8508
8509static int
8510breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8511 struct address_space *aspace,
8512 CORE_ADDR bp_addr)
8513{
8514 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8515 bl->length, aspace, bp_addr);
8516}
8517
8518/* Implement the "resources_needed" breakpoint_ops method for
8519 ranged breakpoints. */
8520
8521static int
8522resources_needed_ranged_breakpoint (const struct bp_location *bl)
8523{
8524 return target_ranged_break_num_registers ();
8525}
8526
8527/* Implement the "print_it" breakpoint_ops method for
8528 ranged breakpoints. */
8529
8530static enum print_stop_action
8531print_it_ranged_breakpoint (struct breakpoint *b)
8532{
8533 struct bp_location *bl = b->loc;
8534
8535 gdb_assert (b->type == bp_hardware_breakpoint);
8536
8537 /* Ranged breakpoints have only one location. */
8538 gdb_assert (bl && bl->next == NULL);
8539
8540 annotate_breakpoint (b->number);
8541 if (b->disposition == disp_del)
8542 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8543 else
8544 ui_out_text (uiout, "\nRanged breakpoint ");
8545 if (ui_out_is_mi_like_p (uiout))
8546 {
8547 ui_out_field_string (uiout, "reason",
8548 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8549 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8550 }
8551 ui_out_field_int (uiout, "bkptno", b->number);
8552 ui_out_text (uiout, ", ");
8553
8554 return PRINT_SRC_AND_LOC;
8555}
8556
8557/* Implement the "print_one" breakpoint_ops method for
8558 ranged breakpoints. */
8559
8560static void
8561print_one_ranged_breakpoint (struct breakpoint *b,
8562 struct bp_location **last_loc)
8563{
8564 struct bp_location *bl = b->loc;
8565 struct value_print_options opts;
8566
8567 /* Ranged breakpoints have only one location. */
8568 gdb_assert (bl && bl->next == NULL);
8569
8570 get_user_print_options (&opts);
8571
8572 if (opts.addressprint)
8573 /* We don't print the address range here, it will be printed later
8574 by print_one_detail_ranged_breakpoint. */
8575 ui_out_field_skip (uiout, "addr");
8576 annotate_field (5);
8577 print_breakpoint_location (b, bl);
8578 *last_loc = bl;
8579}
8580
8581/* Implement the "print_one_detail" breakpoint_ops method for
8582 ranged breakpoints. */
8583
8584static void
8585print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8586 struct ui_out *uiout)
8587{
8588 CORE_ADDR address_start, address_end;
8589 struct bp_location *bl = b->loc;
8590 struct ui_stream *stb = ui_out_stream_new (uiout);
8591 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8592
8593 gdb_assert (bl);
8594
8595 address_start = bl->address;
8596 address_end = address_start + bl->length - 1;
8597
8598 ui_out_text (uiout, "\taddress range: ");
8599 fprintf_unfiltered (stb->stream, "[%s, %s]",
8600 print_core_address (bl->gdbarch, address_start),
8601 print_core_address (bl->gdbarch, address_end));
8602 ui_out_field_stream (uiout, "addr", stb);
8603 ui_out_text (uiout, "\n");
8604
8605 do_cleanups (cleanup);
8606}
8607
8608/* Implement the "print_mention" breakpoint_ops method for
8609 ranged breakpoints. */
8610
8611static void
8612print_mention_ranged_breakpoint (struct breakpoint *b)
8613{
8614 struct bp_location *bl = b->loc;
8615
8616 gdb_assert (bl);
8617 gdb_assert (b->type == bp_hardware_breakpoint);
8618
8619 if (ui_out_is_mi_like_p (uiout))
8620 return;
8621
8622 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8623 b->number, paddress (bl->gdbarch, bl->address),
8624 paddress (bl->gdbarch, bl->address + bl->length - 1));
8625}
8626
8627/* Implement the "print_recreate" breakpoint_ops method for
8628 ranged breakpoints. */
8629
8630static void
8631print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8632{
8633 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8634 b->addr_string_range_end);
8635}
8636
8637/* The breakpoint_ops structure to be used in ranged breakpoints. */
8638
8639static struct breakpoint_ops ranged_breakpoint_ops =
8640{
be5c67c1 8641 NULL, /* dtor */
28010a5d
PA
8642 NULL, /* allocate_location */
8643 NULL, /* re_set */
f1310107
TJB
8644 NULL, /* insert */
8645 NULL, /* remove */
8646 breakpoint_hit_ranged_breakpoint,
28010a5d 8647 NULL, /* check_status */
f1310107 8648 resources_needed_ranged_breakpoint,
9c06b0b4 8649 NULL, /* works_in_software_mode */
f1310107
TJB
8650 print_it_ranged_breakpoint,
8651 print_one_ranged_breakpoint,
8652 print_one_detail_ranged_breakpoint,
8653 print_mention_ranged_breakpoint,
8654 print_recreate_ranged_breakpoint
8655};
8656
8657/* Find the address where the end of the breakpoint range should be
8658 placed, given the SAL of the end of the range. This is so that if
8659 the user provides a line number, the end of the range is set to the
8660 last instruction of the given line. */
8661
8662static CORE_ADDR
8663find_breakpoint_range_end (struct symtab_and_line sal)
8664{
8665 CORE_ADDR end;
8666
8667 /* If the user provided a PC value, use it. Otherwise,
8668 find the address of the end of the given location. */
8669 if (sal.explicit_pc)
8670 end = sal.pc;
8671 else
8672 {
8673 int ret;
8674 CORE_ADDR start;
8675
8676 ret = find_line_pc_range (sal, &start, &end);
8677 if (!ret)
8678 error (_("Could not find location of the end of the range."));
8679
8680 /* find_line_pc_range returns the start of the next line. */
8681 end--;
8682 }
8683
8684 return end;
8685}
8686
8687/* Implement the "break-range" CLI command. */
8688
8689static void
8690break_range_command (char *arg, int from_tty)
8691{
8692 char *arg_start, *addr_string_start, *addr_string_end;
8693 struct linespec_result canonical_start, canonical_end;
8694 int bp_count, can_use_bp, length;
8695 CORE_ADDR end;
8696 struct breakpoint *b;
8697 struct symtab_and_line sal_start, sal_end;
8698 struct symtabs_and_lines sals_start, sals_end;
8699 struct cleanup *cleanup_bkpt;
8700
8701 /* We don't support software ranged breakpoints. */
8702 if (target_ranged_break_num_registers () < 0)
8703 error (_("This target does not support hardware ranged breakpoints."));
8704
8705 bp_count = hw_breakpoint_used_count ();
8706 bp_count += target_ranged_break_num_registers ();
8707 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8708 bp_count, 0);
8709 if (can_use_bp < 0)
8710 error (_("Hardware breakpoints used exceeds limit."));
8711
8712 if (arg == NULL || arg[0] == '\0')
8713 error(_("No address range specified."));
8714
8715 sals_start.sals = NULL;
8716 sals_start.nelts = 0;
8717 init_linespec_result (&canonical_start);
8718
8719 while (*arg == ' ' || *arg == '\t')
8720 arg++;
8721
58438ac1 8722 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8723
8724 sal_start = sals_start.sals[0];
8725 addr_string_start = canonical_start.canonical[0];
8726 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8727 xfree (sals_start.sals);
8728 xfree (canonical_start.canonical);
8729
8730 if (arg[0] != ',')
8731 error (_("Too few arguments."));
8732 else if (sals_start.nelts == 0)
8733 error (_("Could not find location of the beginning of the range."));
8734 else if (sals_start.nelts != 1)
8735 error (_("Cannot create a ranged breakpoint with multiple locations."));
8736
8737 resolve_sal_pc (&sal_start);
8738
8739 arg++; /* Skip the comma. */
8740 while (*arg == ' ' || *arg == '\t')
8741 arg++;
8742
8743 /* Parse the end location. */
8744
8745 sals_end.sals = NULL;
8746 sals_end.nelts = 0;
8747 init_linespec_result (&canonical_end);
8748 arg_start = arg;
8749
423f41a5 8750 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8751 parse_breakpoint_sals because we need to specify the start location's
8752 symtab and line as the default symtab and line for the end of the
8753 range. This makes it possible to have ranges like "foo.c:27, +14",
8754 where +14 means 14 lines from the start location. */
8755 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8756 &canonical_end);
f1310107
TJB
8757
8758 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8759 if (canonical_end.canonical == NULL)
38a714bb 8760 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8761 /* Add the string if not present. */
8762 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8763 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8764
8765 sal_end = sals_end.sals[0];
8766 addr_string_end = canonical_end.canonical[0];
8767 make_cleanup (xfree, addr_string_end);
8768 xfree (sals_end.sals);
8769 xfree (canonical_end.canonical);
8770
8771 if (sals_end.nelts == 0)
8772 error (_("Could not find location of the end of the range."));
8773 else if (sals_end.nelts != 1)
8774 error (_("Cannot create a ranged breakpoint with multiple locations."));
8775
8776 resolve_sal_pc (&sal_end);
8777
8778 end = find_breakpoint_range_end (sal_end);
8779 if (sal_start.pc > end)
8780 error (_("Invalid address range, end preceeds start."));
8781
8782 length = end - sal_start.pc + 1;
8783 if (length < 0)
8784 /* Length overflowed. */
8785 error (_("Address range too large."));
8786 else if (length == 1)
8787 {
8788 /* This range is simple enough to be handled by
8789 the `hbreak' command. */
8790 hbreak_command (addr_string_start, 1);
8791
8792 do_cleanups (cleanup_bkpt);
8793
8794 return;
8795 }
8796
8797 /* Now set up the breakpoint. */
8798 b = set_raw_breakpoint (get_current_arch (), sal_start,
8799 bp_hardware_breakpoint);
8800 set_breakpoint_count (breakpoint_count + 1);
8801 b->number = breakpoint_count;
8802 b->disposition = disp_donttouch;
8803 b->addr_string = addr_string_start;
8804 b->addr_string_range_end = addr_string_end;
8805 b->ops = &ranged_breakpoint_ops;
8806 b->loc->length = length;
8807
8808 discard_cleanups (cleanup_bkpt);
8809
8810 mention (b);
8d3788bd 8811 observer_notify_breakpoint_created (b);
f1310107
TJB
8812 update_global_location_list (1);
8813}
8814
4a64f543
MS
8815/* Return non-zero if EXP is verified as constant. Returned zero
8816 means EXP is variable. Also the constant detection may fail for
8817 some constant expressions and in such case still falsely return
8818 zero. */
65d79d4b
SDJ
8819static int
8820watchpoint_exp_is_const (const struct expression *exp)
8821{
8822 int i = exp->nelts;
8823
8824 while (i > 0)
8825 {
8826 int oplenp, argsp;
8827
8828 /* We are only interested in the descriptor of each element. */
8829 operator_length (exp, i, &oplenp, &argsp);
8830 i -= oplenp;
8831
8832 switch (exp->elts[i].opcode)
8833 {
8834 case BINOP_ADD:
8835 case BINOP_SUB:
8836 case BINOP_MUL:
8837 case BINOP_DIV:
8838 case BINOP_REM:
8839 case BINOP_MOD:
8840 case BINOP_LSH:
8841 case BINOP_RSH:
8842 case BINOP_LOGICAL_AND:
8843 case BINOP_LOGICAL_OR:
8844 case BINOP_BITWISE_AND:
8845 case BINOP_BITWISE_IOR:
8846 case BINOP_BITWISE_XOR:
8847 case BINOP_EQUAL:
8848 case BINOP_NOTEQUAL:
8849 case BINOP_LESS:
8850 case BINOP_GTR:
8851 case BINOP_LEQ:
8852 case BINOP_GEQ:
8853 case BINOP_REPEAT:
8854 case BINOP_COMMA:
8855 case BINOP_EXP:
8856 case BINOP_MIN:
8857 case BINOP_MAX:
8858 case BINOP_INTDIV:
8859 case BINOP_CONCAT:
8860 case BINOP_IN:
8861 case BINOP_RANGE:
8862 case TERNOP_COND:
8863 case TERNOP_SLICE:
8864 case TERNOP_SLICE_COUNT:
8865
8866 case OP_LONG:
8867 case OP_DOUBLE:
8868 case OP_DECFLOAT:
8869 case OP_LAST:
8870 case OP_COMPLEX:
8871 case OP_STRING:
8872 case OP_BITSTRING:
8873 case OP_ARRAY:
8874 case OP_TYPE:
8875 case OP_NAME:
8876 case OP_OBJC_NSSTRING:
8877
8878 case UNOP_NEG:
8879 case UNOP_LOGICAL_NOT:
8880 case UNOP_COMPLEMENT:
8881 case UNOP_ADDR:
8882 case UNOP_HIGH:
4a64f543
MS
8883 /* Unary, binary and ternary operators: We have to check
8884 their operands. If they are constant, then so is the
8885 result of that operation. For instance, if A and B are
8886 determined to be constants, then so is "A + B".
8887
8888 UNOP_IND is one exception to the rule above, because the
8889 value of *ADDR is not necessarily a constant, even when
8890 ADDR is. */
65d79d4b
SDJ
8891 break;
8892
8893 case OP_VAR_VALUE:
8894 /* Check whether the associated symbol is a constant.
4a64f543 8895
65d79d4b 8896 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8897 possible that a buggy compiler could mark a variable as
8898 constant even when it is not, and TYPE_CONST would return
8899 true in this case, while SYMBOL_CLASS wouldn't.
8900
8901 We also have to check for function symbols because they
8902 are always constant. */
65d79d4b
SDJ
8903 {
8904 struct symbol *s = exp->elts[i + 2].symbol;
8905
8906 if (SYMBOL_CLASS (s) != LOC_BLOCK
8907 && SYMBOL_CLASS (s) != LOC_CONST
8908 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8909 return 0;
8910 break;
8911 }
8912
8913 /* The default action is to return 0 because we are using
8914 the optimistic approach here: If we don't know something,
8915 then it is not a constant. */
8916 default:
8917 return 0;
8918 }
8919 }
8920
8921 return 1;
8922}
8923
77b06cd7
TJB
8924/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8925
8926static int
8927insert_watchpoint (struct bp_location *bl)
8928{
e09342b5
TJB
8929 int length = bl->owner->exact? 1 : bl->length;
8930
8931 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
8932 bl->owner->cond_exp);
77b06cd7
TJB
8933}
8934
8935/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8936
8937static int
8938remove_watchpoint (struct bp_location *bl)
8939{
e09342b5
TJB
8940 int length = bl->owner->exact? 1 : bl->length;
8941
8942 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
8943 bl->owner->cond_exp);
8944}
8945
8946/* Implement the "resources_needed" breakpoint_ops method for
8947 hardware watchpoints. */
8948
8949static int
8950resources_needed_watchpoint (const struct bp_location *bl)
8951{
90e4670f 8952 int length = bl->owner->exact? 1 : bl->length;
e09342b5 8953
90e4670f 8954 return target_region_ok_for_hw_watchpoint (bl->address, length);
77b06cd7
TJB
8955}
8956
9c06b0b4
TJB
8957/* Implement the "works_in_software_mode" breakpoint_ops method for
8958 hardware watchpoints. */
8959
8960int
8961works_in_software_mode_watchpoint (const struct breakpoint *b)
8962{
8963 return b->type == bp_hardware_watchpoint;
8964}
8965
77b06cd7
TJB
8966/* The breakpoint_ops structure to be used in hardware watchpoints. */
8967
8968static struct breakpoint_ops watchpoint_breakpoint_ops =
8969{
be5c67c1 8970 NULL, /* dtor */
28010a5d
PA
8971 NULL, /* allocate_location */
8972 NULL, /* re_set */
77b06cd7
TJB
8973 insert_watchpoint,
8974 remove_watchpoint,
8975 NULL, /* breakpoint_hit */
28010a5d 8976 NULL, /* check_status */
e09342b5 8977 resources_needed_watchpoint,
9c06b0b4 8978 works_in_software_mode_watchpoint,
77b06cd7
TJB
8979 NULL, /* print_it */
8980 NULL, /* print_one */
f1310107 8981 NULL, /* print_one_detail */
77b06cd7
TJB
8982 NULL, /* print_mention */
8983 NULL /* print_recreate */
8984};
8985
9c06b0b4
TJB
8986/* Implement the "insert" breakpoint_ops method for
8987 masked hardware watchpoints. */
8988
8989static int
8990insert_masked_watchpoint (struct bp_location *bl)
8991{
8992 return target_insert_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
8993 bl->watchpoint_type);
8994}
8995
8996/* Implement the "remove" breakpoint_ops method for
8997 masked hardware watchpoints. */
8998
8999static int
9000remove_masked_watchpoint (struct bp_location *bl)
9001{
9002 return target_remove_mask_watchpoint (bl->address, bl->owner->hw_wp_mask,
9003 bl->watchpoint_type);
9004}
9005
9006/* Implement the "resources_needed" breakpoint_ops method for
9007 masked hardware watchpoints. */
9008
9009static int
9010resources_needed_masked_watchpoint (const struct bp_location *bl)
9011{
9012 return target_masked_watch_num_registers (bl->address,
9013 bl->owner->hw_wp_mask);
9014}
9015
9016/* Implement the "works_in_software_mode" breakpoint_ops method for
9017 masked hardware watchpoints. */
9018
9019static int
9020works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
9021{
9022 return 0;
9023}
9024
9025/* Implement the "print_it" breakpoint_ops method for
9026 masked hardware watchpoints. */
9027
9028static enum print_stop_action
9029print_it_masked_watchpoint (struct breakpoint *b)
9030{
9031 /* Masked watchpoints have only one location. */
9032 gdb_assert (b->loc && b->loc->next == NULL);
9033
9034 switch (b->type)
9035 {
9036 case bp_hardware_watchpoint:
9037 annotate_watchpoint (b->number);
9038 if (ui_out_is_mi_like_p (uiout))
9039 ui_out_field_string
9040 (uiout, "reason",
9041 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9042 break;
9043
9044 case bp_read_watchpoint:
9045 if (ui_out_is_mi_like_p (uiout))
9046 ui_out_field_string
9047 (uiout, "reason",
9048 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9049 break;
9050
9051 case bp_access_watchpoint:
9052 if (ui_out_is_mi_like_p (uiout))
9053 ui_out_field_string
9054 (uiout, "reason",
9055 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9056 break;
9057 default:
9058 internal_error (__FILE__, __LINE__,
9059 _("Invalid hardware watchpoint type."));
9060 }
9061
9062 mention (b);
9063 ui_out_text (uiout, _("\n\
9064Check the underlying instruction at PC for the memory\n\
9065address and value which triggered this watchpoint.\n"));
9066 ui_out_text (uiout, "\n");
9067
9068 /* More than one watchpoint may have been triggered. */
9069 return PRINT_UNKNOWN;
9070}
9071
9072/* Implement the "print_one_detail" breakpoint_ops method for
9073 masked hardware watchpoints. */
9074
9075static void
9076print_one_detail_masked_watchpoint (const struct breakpoint *b,
9077 struct ui_out *uiout)
9078{
9079 /* Masked watchpoints have only one location. */
9080 gdb_assert (b->loc && b->loc->next == NULL);
9081
9082 ui_out_text (uiout, "\tmask ");
9083 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, b->hw_wp_mask);
9084 ui_out_text (uiout, "\n");
9085}
9086
9087/* Implement the "print_mention" breakpoint_ops method for
9088 masked hardware watchpoints. */
9089
9090static void
9091print_mention_masked_watchpoint (struct breakpoint *b)
9092{
9093 struct cleanup *ui_out_chain;
9094
9095 switch (b->type)
9096 {
9097 case bp_hardware_watchpoint:
9098 ui_out_text (uiout, "Masked hardware watchpoint ");
9099 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9100 break;
9101 case bp_read_watchpoint:
9102 ui_out_text (uiout, "Masked hardware read watchpoint ");
9103 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9104 break;
9105 case bp_access_watchpoint:
9106 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9107 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9108 break;
9109 default:
9110 internal_error (__FILE__, __LINE__,
9111 _("Invalid hardware watchpoint type."));
9112 }
9113
9114 ui_out_field_int (uiout, "number", b->number);
9115 ui_out_text (uiout, ": ");
9116 ui_out_field_string (uiout, "exp", b->exp_string);
9117 do_cleanups (ui_out_chain);
9118}
9119
9120/* Implement the "print_recreate" breakpoint_ops method for
9121 masked hardware watchpoints. */
9122
9123static void
9124print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9125{
9126 char tmp[40];
9127
9128 switch (b->type)
9129 {
9130 case bp_hardware_watchpoint:
9131 fprintf_unfiltered (fp, "watch");
9132 break;
9133 case bp_read_watchpoint:
9134 fprintf_unfiltered (fp, "rwatch");
9135 break;
9136 case bp_access_watchpoint:
9137 fprintf_unfiltered (fp, "awatch");
9138 break;
9139 default:
9140 internal_error (__FILE__, __LINE__,
9141 _("Invalid hardware watchpoint type."));
9142 }
9143
9144 sprintf_vma (tmp, b->hw_wp_mask);
9145 fprintf_unfiltered (fp, " %s mask 0x%s", b->exp_string, tmp);
9146}
9147
9148/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9149
9150static struct breakpoint_ops masked_watchpoint_breakpoint_ops =
9151{
be5c67c1 9152 NULL, /* dtor */
28010a5d
PA
9153 NULL, /* allocate_location */
9154 NULL, /* re_set */
9c06b0b4
TJB
9155 insert_masked_watchpoint,
9156 remove_masked_watchpoint,
9157 NULL, /* breakpoint_hit */
28010a5d 9158 NULL, /* check_status */
9c06b0b4
TJB
9159 resources_needed_masked_watchpoint,
9160 works_in_software_mode_masked_watchpoint,
9161 print_it_masked_watchpoint,
9162 NULL, /* print_one */
9163 print_one_detail_masked_watchpoint,
9164 print_mention_masked_watchpoint,
9165 print_recreate_masked_watchpoint
9166};
9167
9168/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9169
9170static int
9171is_masked_watchpoint (const struct breakpoint *b)
9172{
9173 return b->ops == &masked_watchpoint_breakpoint_ops;
9174}
9175
53a5351d
JM
9176/* accessflag: hw_write: watch write,
9177 hw_read: watch read,
9178 hw_access: watch access (read or write) */
c906108c 9179static void
84f4c1fe
PM
9180watch_command_1 (char *arg, int accessflag, int from_tty,
9181 int just_location, int internal)
c906108c 9182{
a9634178 9183 volatile struct gdb_exception e;
d983da9c 9184 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9185 struct expression *exp;
60e1c644 9186 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9187 struct value *val, *mark, *result;
c906108c 9188 struct frame_info *frame;
c906108c
SS
9189 char *exp_start = NULL;
9190 char *exp_end = NULL;
9c06b0b4
TJB
9191 char *tok, *end_tok;
9192 int toklen = -1;
c906108c
SS
9193 char *cond_start = NULL;
9194 char *cond_end = NULL;
c906108c 9195 enum bptype bp_type;
37e4754d 9196 int thread = -1;
0cf6dd15 9197 int pc = 0;
9c06b0b4
TJB
9198 /* Flag to indicate whether we are going to use masks for
9199 the hardware watchpoint. */
9200 int use_mask = 0;
9201 CORE_ADDR mask = 0;
c906108c 9202
37e4754d
LM
9203 /* Make sure that we actually have parameters to parse. */
9204 if (arg != NULL && arg[0] != '\0')
9205 {
9c06b0b4 9206 char *value_start;
37e4754d 9207
9c06b0b4
TJB
9208 /* Look for "parameter value" pairs at the end
9209 of the arguments string. */
9210 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9211 {
9212 /* Skip whitespace at the end of the argument list. */
9213 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9214 tok--;
9215
9216 /* Find the beginning of the last token.
9217 This is the value of the parameter. */
9218 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9219 tok--;
9220 value_start = tok + 1;
9221
9222 /* Skip whitespace. */
9223 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9224 tok--;
9225
9226 end_tok = tok;
9227
9228 /* Find the beginning of the second to last token.
9229 This is the parameter itself. */
9230 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9231 tok--;
9232 tok++;
9233 toklen = end_tok - tok + 1;
9234
9235 if (toklen == 6 && !strncmp (tok, "thread", 6))
9236 {
9237 /* At this point we've found a "thread" token, which means
9238 the user is trying to set a watchpoint that triggers
9239 only in a specific thread. */
9240 char *endp;
37e4754d 9241
9c06b0b4
TJB
9242 if (thread != -1)
9243 error(_("You can specify only one thread."));
37e4754d 9244
9c06b0b4
TJB
9245 /* Extract the thread ID from the next token. */
9246 thread = strtol (value_start, &endp, 0);
37e4754d 9247
9c06b0b4
TJB
9248 /* Check if the user provided a valid numeric value for the
9249 thread ID. */
9250 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9251 error (_("Invalid thread ID specification %s."), value_start);
9252
9253 /* Check if the thread actually exists. */
9254 if (!valid_thread_id (thread))
9255 error (_("Unknown thread %d."), thread);
9256 }
9257 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9258 {
9259 /* We've found a "mask" token, which means the user wants to
9260 create a hardware watchpoint that is going to have the mask
9261 facility. */
9262 struct value *mask_value, *mark;
37e4754d 9263
9c06b0b4
TJB
9264 if (use_mask)
9265 error(_("You can specify only one mask."));
37e4754d 9266
9c06b0b4 9267 use_mask = just_location = 1;
37e4754d 9268
9c06b0b4
TJB
9269 mark = value_mark ();
9270 mask_value = parse_to_comma_and_eval (&value_start);
9271 mask = value_as_address (mask_value);
9272 value_free_to_mark (mark);
9273 }
9274 else
9275 /* We didn't recognize what we found. We should stop here. */
9276 break;
37e4754d 9277
9c06b0b4
TJB
9278 /* Truncate the string and get rid of the "parameter value" pair before
9279 the arguments string is parsed by the parse_exp_1 function. */
9280 *tok = '\0';
9281 }
37e4754d
LM
9282 }
9283
9284 /* Parse the rest of the arguments. */
c906108c
SS
9285 innermost_block = NULL;
9286 exp_start = arg;
9287 exp = parse_exp_1 (&arg, 0, 0);
9288 exp_end = arg;
fa8a61dc
TT
9289 /* Remove trailing whitespace from the expression before saving it.
9290 This makes the eventual display of the expression string a bit
9291 prettier. */
9292 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9293 --exp_end;
9294
65d79d4b
SDJ
9295 /* Checking if the expression is not constant. */
9296 if (watchpoint_exp_is_const (exp))
9297 {
9298 int len;
9299
9300 len = exp_end - exp_start;
9301 while (len > 0 && isspace (exp_start[len - 1]))
9302 len--;
9303 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9304 }
9305
c906108c
SS
9306 exp_valid_block = innermost_block;
9307 mark = value_mark ();
a1442452 9308 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9309
9310 if (just_location)
9311 {
9c06b0b4
TJB
9312 int ret;
9313
06a64a0b 9314 exp_valid_block = NULL;
a1442452 9315 val = value_addr (result);
06a64a0b
TT
9316 release_value (val);
9317 value_free_to_mark (mark);
9c06b0b4
TJB
9318
9319 if (use_mask)
9320 {
9321 ret = target_masked_watch_num_registers (value_as_address (val),
9322 mask);
9323 if (ret == -1)
9324 error (_("This target does not support masked watchpoints."));
9325 else if (ret == -2)
9326 error (_("Invalid mask or memory region."));
9327 }
06a64a0b
TT
9328 }
9329 else if (val != NULL)
fa4727a6 9330 release_value (val);
c906108c 9331
e9cafbcc
TT
9332 tok = skip_spaces (arg);
9333 end_tok = skip_to_space (tok);
c906108c
SS
9334
9335 toklen = end_tok - tok;
9336 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9337 {
2d134ed3
PA
9338 struct expression *cond;
9339
60e1c644 9340 innermost_block = NULL;
c906108c
SS
9341 tok = cond_start = end_tok + 1;
9342 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9343
9344 /* The watchpoint expression may not be local, but the condition
9345 may still be. E.g.: `watch global if local > 0'. */
9346 cond_exp_valid_block = innermost_block;
9347
2d134ed3 9348 xfree (cond);
c906108c
SS
9349 cond_end = tok;
9350 }
9351 if (*tok)
8a3fe4f8 9352 error (_("Junk at end of command."));
c906108c 9353
53a5351d 9354 if (accessflag == hw_read)
c5aa993b 9355 bp_type = bp_read_watchpoint;
53a5351d 9356 else if (accessflag == hw_access)
c5aa993b
JM
9357 bp_type = bp_access_watchpoint;
9358 else
9359 bp_type = bp_hardware_watchpoint;
c906108c 9360
d983da9c 9361 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9362
9363 /* If the expression is "local", then set up a "watchpoint scope"
9364 breakpoint at the point where we've left the scope of the watchpoint
9365 expression. Create the scope breakpoint before the watchpoint, so
9366 that we will encounter it first in bpstat_stop_status. */
60e1c644 9367 if (exp_valid_block && frame)
d983da9c 9368 {
edb3359d
DJ
9369 if (frame_id_p (frame_unwind_caller_id (frame)))
9370 {
9371 scope_breakpoint
a6d9a66e
UW
9372 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9373 frame_unwind_caller_pc (frame),
edb3359d 9374 bp_watchpoint_scope);
d983da9c 9375
edb3359d 9376 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9377
edb3359d
DJ
9378 /* Automatically delete the breakpoint when it hits. */
9379 scope_breakpoint->disposition = disp_del;
d983da9c 9380
edb3359d
DJ
9381 /* Only break in the proper frame (help with recursion). */
9382 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9383
edb3359d 9384 /* Set the address at which we will stop. */
a6d9a66e
UW
9385 scope_breakpoint->loc->gdbarch
9386 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9387 scope_breakpoint->loc->requested_address
9388 = frame_unwind_caller_pc (frame);
9389 scope_breakpoint->loc->address
a6d9a66e
UW
9390 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9391 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9392 scope_breakpoint->type);
9393 }
d983da9c
DJ
9394 }
9395
c906108c 9396 /* Now set up the breakpoint. */
2d134ed3 9397 b = set_raw_breakpoint_without_location (NULL, bp_type);
37e4754d 9398 b->thread = thread;
b5de0fa7 9399 b->disposition = disp_donttouch;
c906108c
SS
9400 b->exp = exp;
9401 b->exp_valid_block = exp_valid_block;
60e1c644 9402 b->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9403 if (just_location)
9404 {
9405 struct type *t = value_type (val);
9406 CORE_ADDR addr = value_as_address (val);
9407 char *name;
9408
9409 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9410 name = type_to_string (t);
9411
d63d0675
JK
9412 b->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
9413 core_addr_to_string (addr));
06a64a0b
TT
9414 xfree (name);
9415
4a4106ca 9416 b->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9417 (int) (exp_end - exp_start), exp_start);
9418
06a64a0b
TT
9419 /* The above expression is in C. */
9420 b->language = language_c;
9421 }
9422 else
9423 b->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9424
9425 if (use_mask)
9426 {
9427 b->hw_wp_mask = mask;
9428 b->ops = &masked_watchpoint_breakpoint_ops;
9429 }
9430 else
9431 {
9432 b->val = val;
9433 b->val_valid = 1;
9434 b->ops = &watchpoint_breakpoint_ops;
9435 }
77b06cd7 9436
c906108c
SS
9437 if (cond_start)
9438 b->cond_string = savestring (cond_start, cond_end - cond_start);
9439 else
9440 b->cond_string = 0;
c5aa993b 9441
c906108c 9442 if (frame)
f6bc2008
PA
9443 {
9444 b->watchpoint_frame = get_frame_id (frame);
9445 b->watchpoint_thread = inferior_ptid;
9446 }
c906108c 9447 else
f6bc2008
PA
9448 {
9449 b->watchpoint_frame = null_frame_id;
9450 b->watchpoint_thread = null_ptid;
9451 }
c906108c 9452
d983da9c 9453 if (scope_breakpoint != NULL)
c906108c 9454 {
d983da9c
DJ
9455 /* The scope breakpoint is related to the watchpoint. We will
9456 need to act on them together. */
9457 b->related_breakpoint = scope_breakpoint;
9458 scope_breakpoint->related_breakpoint = b;
c906108c 9459 }
d983da9c 9460
06a64a0b
TT
9461 if (!just_location)
9462 value_free_to_mark (mark);
2d134ed3 9463
a9634178
TJB
9464 TRY_CATCH (e, RETURN_MASK_ALL)
9465 {
9466 /* Finally update the new watchpoint. This creates the locations
9467 that should be inserted. */
9468 update_watchpoint (b, 1);
9469 }
9470 if (e.reason < 0)
9471 {
9472 delete_breakpoint (b);
9473 throw_exception (e);
9474 }
9475
9476 set_breakpoint_number (internal, b);
8d3788bd
VP
9477
9478 /* Do not mention breakpoints with a negative number, but do
a9634178 9479 notify observers. */
8d3788bd 9480 if (!internal)
84f4c1fe 9481 mention (b);
8d3788bd
VP
9482 observer_notify_breakpoint_created (b);
9483
b60e7edf 9484 update_global_location_list (1);
c906108c
SS
9485}
9486
e09342b5 9487/* Return count of debug registers needed to watch the given expression.
e09342b5 9488 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9489
c906108c 9490static int
a9634178 9491can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9492{
9493 int found_memory_cnt = 0;
2e70b7b9 9494 struct value *head = v;
c906108c
SS
9495
9496 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9497 if (!can_use_hw_watchpoints)
c906108c 9498 return 0;
c5aa993b 9499
5c44784c
JM
9500 /* Make sure that the value of the expression depends only upon
9501 memory contents, and values computed from them within GDB. If we
9502 find any register references or function calls, we can't use a
9503 hardware watchpoint.
9504
9505 The idea here is that evaluating an expression generates a series
9506 of values, one holding the value of every subexpression. (The
9507 expression a*b+c has five subexpressions: a, b, a*b, c, and
9508 a*b+c.) GDB's values hold almost enough information to establish
9509 the criteria given above --- they identify memory lvalues,
9510 register lvalues, computed values, etcetera. So we can evaluate
9511 the expression, and then scan the chain of values that leaves
9512 behind to decide whether we can detect any possible change to the
9513 expression's final value using only hardware watchpoints.
9514
9515 However, I don't think that the values returned by inferior
9516 function calls are special in any way. So this function may not
9517 notice that an expression involving an inferior function call
9518 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9519 for (; v; v = value_next (v))
c906108c 9520 {
5c44784c 9521 if (VALUE_LVAL (v) == lval_memory)
c906108c 9522 {
8464be76
DJ
9523 if (v != head && value_lazy (v))
9524 /* A lazy memory lvalue in the chain is one that GDB never
9525 needed to fetch; we either just used its address (e.g.,
9526 `a' in `a.b') or we never needed it at all (e.g., `a'
9527 in `a,b'). This doesn't apply to HEAD; if that is
9528 lazy then it was not readable, but watch it anyway. */
5c44784c 9529 ;
53a5351d 9530 else
5c44784c
JM
9531 {
9532 /* Ahh, memory we actually used! Check if we can cover
9533 it with hardware watchpoints. */
df407dfe 9534 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9535
9536 /* We only watch structs and arrays if user asked for it
9537 explicitly, never if they just happen to appear in a
9538 middle of some value chain. */
9539 if (v == head
9540 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9541 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9542 {
42ae5230 9543 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9544 int len;
9545 int num_regs;
9546
a9634178 9547 len = (target_exact_watchpoints
e09342b5
TJB
9548 && is_scalar_type_recursive (vtype))?
9549 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9550
e09342b5
TJB
9551 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9552 if (!num_regs)
2e70b7b9
MS
9553 return 0;
9554 else
e09342b5 9555 found_memory_cnt += num_regs;
2e70b7b9 9556 }
5c44784c 9557 }
c5aa993b 9558 }
5086187c
AC
9559 else if (VALUE_LVAL (v) != not_lval
9560 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9561 return 0; /* These are values from the history (e.g., $1). */
5086187c 9562 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9563 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9564 }
9565
9566 /* The expression itself looks suitable for using a hardware
9567 watchpoint, but give the target machine a chance to reject it. */
9568 return found_memory_cnt;
9569}
9570
8b93c638 9571void
84f4c1fe 9572watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9573{
84f4c1fe 9574 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9575}
9576
9577/* A helper function that looks for an argument at the start of a
9578 string. The argument must also either be at the end of the string,
9579 or be followed by whitespace. Returns 1 if it finds the argument,
9580 0 otherwise. If the argument is found, it updates *STR. */
9581
9582static int
9583check_for_argument (char **str, char *arg, int arg_len)
9584{
9585 if (strncmp (*str, arg, arg_len) == 0
9586 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9587 {
9588 *str += arg_len;
9589 return 1;
9590 }
9591 return 0;
9592}
9593
9594/* A helper function that looks for the "-location" argument and then
9595 calls watch_command_1. */
9596
9597static void
9598watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9599{
9600 int just_location = 0;
9601
9602 if (arg
9603 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9604 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9605 {
e9cafbcc 9606 arg = skip_spaces (arg);
06a64a0b
TT
9607 just_location = 1;
9608 }
9609
84f4c1fe 9610 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9611}
8926118c 9612
c5aa993b 9613static void
fba45db2 9614watch_command (char *arg, int from_tty)
c906108c 9615{
06a64a0b 9616 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9617}
9618
8b93c638 9619void
84f4c1fe 9620rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9621{
84f4c1fe 9622 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9623}
8926118c 9624
c5aa993b 9625static void
fba45db2 9626rwatch_command (char *arg, int from_tty)
c906108c 9627{
06a64a0b 9628 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9629}
9630
8b93c638 9631void
84f4c1fe 9632awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9633{
84f4c1fe 9634 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9635}
8926118c 9636
c5aa993b 9637static void
fba45db2 9638awatch_command (char *arg, int from_tty)
c906108c 9639{
06a64a0b 9640 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9641}
c906108c 9642\f
c5aa993b 9643
43ff13b4 9644/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9645 because it uses the mechanisms of breakpoints. */
9646
bfec99b2
PA
9647struct until_break_command_continuation_args
9648{
9649 struct breakpoint *breakpoint;
9650 struct breakpoint *breakpoint2;
186c406b 9651 int thread_num;
bfec99b2
PA
9652};
9653
43ff13b4 9654/* This function is called by fetch_inferior_event via the
4a64f543 9655 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9656 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9657 command. */
c2c6d25f 9658static void
fa4cd53f 9659until_break_command_continuation (void *arg, int err)
43ff13b4 9660{
bfec99b2
PA
9661 struct until_break_command_continuation_args *a = arg;
9662
9663 delete_breakpoint (a->breakpoint);
9664 if (a->breakpoint2)
9665 delete_breakpoint (a->breakpoint2);
186c406b 9666 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9667}
9668
c906108c 9669void
ae66c1fc 9670until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9671{
9672 struct symtabs_and_lines sals;
9673 struct symtab_and_line sal;
206415a3 9674 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9675 struct breakpoint *breakpoint;
f107f563 9676 struct breakpoint *breakpoint2 = NULL;
c906108c 9677 struct cleanup *old_chain;
186c406b
TT
9678 int thread;
9679 struct thread_info *tp;
c906108c
SS
9680
9681 clear_proceed_status ();
9682
9683 /* Set a breakpoint where the user wants it and at return from
4a64f543 9684 this function. */
c5aa993b 9685
c906108c
SS
9686 if (default_breakpoint_valid)
9687 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
58438ac1 9688 default_breakpoint_line, NULL);
c906108c 9689 else
58438ac1 9690 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9691
c906108c 9692 if (sals.nelts != 1)
8a3fe4f8 9693 error (_("Couldn't get information on specified line."));
c5aa993b 9694
c906108c 9695 sal = sals.sals[0];
4a64f543 9696 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9697
c906108c 9698 if (*arg)
8a3fe4f8 9699 error (_("Junk at end of arguments."));
c5aa993b 9700
c906108c 9701 resolve_sal_pc (&sal);
c5aa993b 9702
ae66c1fc
EZ
9703 if (anywhere)
9704 /* If the user told us to continue until a specified location,
9705 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9706 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9707 null_frame_id, bp_until);
ae66c1fc 9708 else
4a64f543
MS
9709 /* Otherwise, specify the selected frame, because we want to stop
9710 only at the very same frame. */
a6d9a66e
UW
9711 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9712 get_stack_frame_id (frame),
ae66c1fc 9713 bp_until);
c5aa993b 9714
f107f563 9715 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9716
186c406b
TT
9717 tp = inferior_thread ();
9718 thread = tp->num;
9719
ae66c1fc
EZ
9720 /* Keep within the current frame, or in frames called by the current
9721 one. */
edb3359d
DJ
9722
9723 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9724 {
edb3359d
DJ
9725 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9726 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9727 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9728 sal,
edb3359d 9729 frame_unwind_caller_id (frame),
f107f563
VP
9730 bp_until);
9731 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9732
9733 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9734 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9735 }
c5aa993b 9736
c906108c 9737 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9738
4a64f543
MS
9739 /* If we are running asynchronously, and proceed call above has
9740 actually managed to start the target, arrange for breakpoints to
9741 be deleted when the target stops. Otherwise, we're already
9742 stopped and delete breakpoints via cleanup chain. */
f107f563 9743
8ea051c5 9744 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9745 {
bfec99b2
PA
9746 struct until_break_command_continuation_args *args;
9747 args = xmalloc (sizeof (*args));
f107f563 9748
bfec99b2
PA
9749 args->breakpoint = breakpoint;
9750 args->breakpoint2 = breakpoint2;
186c406b 9751 args->thread_num = thread;
f107f563
VP
9752
9753 discard_cleanups (old_chain);
95e54da7
PA
9754 add_continuation (inferior_thread (),
9755 until_break_command_continuation, args,
604ead4a 9756 xfree);
f107f563
VP
9757 }
9758 else
c5aa993b 9759 do_cleanups (old_chain);
c906108c 9760}
ae66c1fc 9761
c906108c
SS
9762/* This function attempts to parse an optional "if <cond>" clause
9763 from the arg string. If one is not found, it returns NULL.
c5aa993b 9764
c906108c
SS
9765 Else, it returns a pointer to the condition string. (It does not
9766 attempt to evaluate the string against a particular block.) And,
9767 it updates arg to point to the first character following the parsed
4a64f543 9768 if clause in the arg string. */
53a5351d 9769
c906108c 9770static char *
fba45db2 9771ep_parse_optional_if_clause (char **arg)
c906108c 9772{
c5aa993b
JM
9773 char *cond_string;
9774
9775 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9776 return NULL;
c5aa993b 9777
4a64f543 9778 /* Skip the "if" keyword. */
c906108c 9779 (*arg) += 2;
c5aa993b 9780
c906108c 9781 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9782 condition string. */
e9cafbcc 9783 *arg = skip_spaces (*arg);
c906108c 9784 cond_string = *arg;
c5aa993b 9785
4a64f543
MS
9786 /* Assume that the condition occupies the remainder of the arg
9787 string. */
c906108c 9788 (*arg) += strlen (cond_string);
c5aa993b 9789
c906108c
SS
9790 return cond_string;
9791}
c5aa993b 9792
c906108c
SS
9793/* Commands to deal with catching events, such as signals, exceptions,
9794 process start/exit, etc. */
c5aa993b
JM
9795
9796typedef enum
9797{
44feb3ce
TT
9798 catch_fork_temporary, catch_vfork_temporary,
9799 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9800}
9801catch_fork_kind;
9802
c906108c 9803static void
cc59ec59
MS
9804catch_fork_command_1 (char *arg, int from_tty,
9805 struct cmd_list_element *command)
c906108c 9806{
a6d9a66e 9807 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9808 char *cond_string = NULL;
44feb3ce
TT
9809 catch_fork_kind fork_kind;
9810 int tempflag;
9811
9812 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9813 tempflag = (fork_kind == catch_fork_temporary
9814 || fork_kind == catch_vfork_temporary);
c5aa993b 9815
44feb3ce
TT
9816 if (!arg)
9817 arg = "";
e9cafbcc 9818 arg = skip_spaces (arg);
c5aa993b 9819
c906108c 9820 /* The allowed syntax is:
c5aa993b
JM
9821 catch [v]fork
9822 catch [v]fork if <cond>
9823
4a64f543 9824 First, check if there's an if clause. */
c906108c 9825 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9826
c906108c 9827 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9828 error (_("Junk at end of arguments."));
c5aa993b 9829
c906108c 9830 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9831 and enable reporting of such events. */
c5aa993b
JM
9832 switch (fork_kind)
9833 {
44feb3ce
TT
9834 case catch_fork_temporary:
9835 case catch_fork_permanent:
a6d9a66e 9836 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9837 &catch_fork_breakpoint_ops);
c906108c 9838 break;
44feb3ce
TT
9839 case catch_vfork_temporary:
9840 case catch_vfork_permanent:
a6d9a66e 9841 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9842 &catch_vfork_breakpoint_ops);
c906108c 9843 break;
c5aa993b 9844 default:
8a3fe4f8 9845 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9846 break;
c5aa993b 9847 }
c906108c
SS
9848}
9849
9850static void
cc59ec59
MS
9851catch_exec_command_1 (char *arg, int from_tty,
9852 struct cmd_list_element *command)
c906108c 9853{
b4d90040 9854 struct exec_catchpoint *c;
a6d9a66e 9855 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9856 int tempflag;
c5aa993b 9857 char *cond_string = NULL;
c906108c 9858
44feb3ce
TT
9859 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9860
9861 if (!arg)
9862 arg = "";
e9cafbcc 9863 arg = skip_spaces (arg);
c906108c
SS
9864
9865 /* The allowed syntax is:
c5aa993b
JM
9866 catch exec
9867 catch exec if <cond>
c906108c 9868
4a64f543 9869 First, check if there's an if clause. */
c906108c
SS
9870 cond_string = ep_parse_optional_if_clause (&arg);
9871
9872 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9873 error (_("Junk at end of arguments."));
c906108c 9874
b4d90040
PA
9875 c = XNEW (struct exec_catchpoint);
9876 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9877 &catch_exec_breakpoint_ops);
9878 c->exec_pathname = NULL;
9879
28010a5d 9880 install_breakpoint (&c->base);
c906108c 9881}
c5aa993b 9882
3086aeae 9883static enum print_stop_action
ba770c9c 9884print_it_exception_catchpoint (struct breakpoint *b)
3086aeae 9885{
ade92717 9886 int bp_temp, bp_throw;
3086aeae 9887
ade92717 9888 annotate_catchpoint (b->number);
3086aeae 9889
ade92717
AR
9890 bp_throw = strstr (b->addr_string, "throw") != NULL;
9891 if (b->loc->address != b->loc->requested_address)
9892 breakpoint_adjustment_warning (b->loc->requested_address,
9893 b->loc->address,
9894 b->number, 1);
df2b6d2d 9895 bp_temp = b->disposition == disp_del;
ade92717
AR
9896 ui_out_text (uiout,
9897 bp_temp ? "Temporary catchpoint "
9898 : "Catchpoint ");
9899 if (!ui_out_is_mi_like_p (uiout))
9900 ui_out_field_int (uiout, "bkptno", b->number);
9901 ui_out_text (uiout,
c0b37c48
AR
9902 bp_throw ? " (exception thrown), "
9903 : " (exception caught), ");
ade92717
AR
9904 if (ui_out_is_mi_like_p (uiout))
9905 {
9906 ui_out_field_string (uiout, "reason",
9907 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9908 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9909 ui_out_field_int (uiout, "bkptno", b->number);
9910 }
3086aeae
DJ
9911 return PRINT_SRC_AND_LOC;
9912}
9913
9914static void
cc59ec59
MS
9915print_one_exception_catchpoint (struct breakpoint *b,
9916 struct bp_location **last_loc)
3086aeae 9917{
79a45b7d 9918 struct value_print_options opts;
cc59ec59 9919
79a45b7d
TT
9920 get_user_print_options (&opts);
9921 if (opts.addressprint)
3086aeae
DJ
9922 {
9923 annotate_field (4);
604133b5
AR
9924 if (b->loc == NULL || b->loc->shlib_disabled)
9925 ui_out_field_string (uiout, "addr", "<PENDING>");
9926 else
5af949e3
UW
9927 ui_out_field_core_addr (uiout, "addr",
9928 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9929 }
9930 annotate_field (5);
604133b5 9931 if (b->loc)
a6d9a66e 9932 *last_loc = b->loc;
3086aeae
DJ
9933 if (strstr (b->addr_string, "throw") != NULL)
9934 ui_out_field_string (uiout, "what", "exception throw");
9935 else
9936 ui_out_field_string (uiout, "what", "exception catch");
9937}
9938
9939static void
9940print_mention_exception_catchpoint (struct breakpoint *b)
9941{
ade92717
AR
9942 int bp_temp;
9943 int bp_throw;
9944
df2b6d2d 9945 bp_temp = b->disposition == disp_del;
ade92717
AR
9946 bp_throw = strstr (b->addr_string, "throw") != NULL;
9947 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9948 : _("Catchpoint "));
9949 ui_out_field_int (uiout, "bkptno", b->number);
9950 ui_out_text (uiout, bp_throw ? _(" (throw)")
9951 : _(" (catch)"));
3086aeae
DJ
9952}
9953
6149aea9
PA
9954/* Implement the "print_recreate" breakpoint_ops method for throw and
9955 catch catchpoints. */
9956
9957static void
4a64f543
MS
9958print_recreate_exception_catchpoint (struct breakpoint *b,
9959 struct ui_file *fp)
6149aea9
PA
9960{
9961 int bp_temp;
9962 int bp_throw;
9963
9964 bp_temp = b->disposition == disp_del;
9965 bp_throw = strstr (b->addr_string, "throw") != NULL;
9966 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9967 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
9968}
9969
3086aeae 9970static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
be5c67c1 9971 NULL, /* dtor */
28010a5d
PA
9972 NULL, /* allocate_location */
9973 NULL, /* re_set */
ce78b96d
JB
9974 NULL, /* insert */
9975 NULL, /* remove */
9976 NULL, /* breakpoint_hit */
28010a5d 9977 NULL, /* check_status */
e09342b5 9978 NULL, /* resources_needed */
9c06b0b4 9979 NULL, /* works_in_software_mode */
ba770c9c 9980 print_it_exception_catchpoint,
3086aeae 9981 print_one_exception_catchpoint,
f1310107 9982 NULL, /* print_one_detail */
6149aea9
PA
9983 print_mention_exception_catchpoint,
9984 print_recreate_exception_catchpoint
3086aeae
DJ
9985};
9986
9987static int
9988handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9989 enum exception_event_kind ex_event, int from_tty)
9990{
604133b5
AR
9991 char *trigger_func_name;
9992
3086aeae 9993 if (ex_event == EX_EVENT_CATCH)
604133b5 9994 trigger_func_name = "__cxa_begin_catch";
3086aeae 9995 else
604133b5 9996 trigger_func_name = "__cxa_throw";
3086aeae 9997
8cdf0e15
VP
9998 create_breakpoint (get_current_arch (),
9999 trigger_func_name, cond_string, -1,
10000 0 /* condition and thread are valid. */,
0fb4aa4b 10001 tempflag, bp_breakpoint,
8cdf0e15
VP
10002 0,
10003 AUTO_BOOLEAN_TRUE /* pending */,
10004 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
10005 1 /* enabled */,
10006 0 /* internal */);
3086aeae 10007
3086aeae
DJ
10008 return 1;
10009}
10010
4a64f543 10011/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
10012
10013static void
fba45db2
KB
10014catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
10015 int tempflag, int from_tty)
c906108c 10016{
c5aa993b 10017 char *cond_string = NULL;
c5aa993b 10018
44feb3ce
TT
10019 if (!arg)
10020 arg = "";
e9cafbcc 10021 arg = skip_spaces (arg);
c5aa993b 10022
c906108c
SS
10023 cond_string = ep_parse_optional_if_clause (&arg);
10024
10025 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10026 error (_("Junk at end of arguments."));
c906108c 10027
059fb39f
PM
10028 if (ex_event != EX_EVENT_THROW
10029 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 10030 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 10031
3086aeae
DJ
10032 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
10033 return;
10034
8a3fe4f8 10035 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
10036}
10037
44feb3ce
TT
10038/* Implementation of "catch catch" command. */
10039
10040static void
10041catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
10042{
10043 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10044
44feb3ce
TT
10045 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
10046}
10047
10048/* Implementation of "catch throw" command. */
10049
10050static void
10051catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
10052{
10053 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 10054
44feb3ce
TT
10055 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
10056}
10057
9ac4176b 10058void
28010a5d
PA
10059init_ada_exception_breakpoint (struct breakpoint *b,
10060 struct gdbarch *gdbarch,
10061 struct symtab_and_line sal,
10062 char *addr_string,
10063 struct breakpoint_ops *ops,
10064 int tempflag,
10065 int from_tty)
f7f9143b 10066{
f7f9143b
JB
10067 if (from_tty)
10068 {
5af949e3
UW
10069 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10070 if (!loc_gdbarch)
10071 loc_gdbarch = gdbarch;
10072
6c95b8df
PA
10073 describe_other_breakpoints (loc_gdbarch,
10074 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
10075 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10076 version for exception catchpoints, because two catchpoints
10077 used for different exception names will use the same address.
10078 In this case, a "breakpoint ... also set at..." warning is
4a64f543 10079 unproductive. Besides, the warning phrasing is also a bit
f7f9143b
JB
10080 inapropriate, we should use the word catchpoint, and tell
10081 the user what type of catchpoint it is. The above is good
10082 enough for now, though. */
10083 }
10084
28010a5d 10085 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
10086
10087 b->enable_state = bp_enabled;
10088 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
10089 b->addr_string = addr_string;
10090 b->language = language_ada;
f7f9143b
JB
10091}
10092
a96d9b2e
SDJ
10093/* Cleanup function for a syscall filter list. */
10094static void
10095clean_up_filters (void *arg)
10096{
10097 VEC(int) *iter = *(VEC(int) **) arg;
10098 VEC_free (int, iter);
10099}
10100
10101/* Splits the argument using space as delimiter. Returns an xmalloc'd
10102 filter list, or NULL if no filtering is required. */
10103static VEC(int) *
10104catch_syscall_split_args (char *arg)
10105{
10106 VEC(int) *result = NULL;
10107 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
10108
10109 while (*arg != '\0')
10110 {
10111 int i, syscall_number;
10112 char *endptr;
10113 char cur_name[128];
10114 struct syscall s;
10115
10116 /* Skip whitespace. */
10117 while (isspace (*arg))
10118 arg++;
10119
10120 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10121 cur_name[i] = arg[i];
10122 cur_name[i] = '\0';
10123 arg += i;
10124
10125 /* Check if the user provided a syscall name or a number. */
10126 syscall_number = (int) strtol (cur_name, &endptr, 0);
10127 if (*endptr == '\0')
bccd0dd2 10128 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
10129 else
10130 {
10131 /* We have a name. Let's check if it's valid and convert it
10132 to a number. */
10133 get_syscall_by_name (cur_name, &s);
10134
10135 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
10136 /* Here we have to issue an error instead of a warning,
10137 because GDB cannot do anything useful if there's no
10138 syscall number to be caught. */
a96d9b2e
SDJ
10139 error (_("Unknown syscall name '%s'."), cur_name);
10140 }
10141
10142 /* Ok, it's valid. */
10143 VEC_safe_push (int, result, s.number);
10144 }
10145
10146 discard_cleanups (cleanup);
10147 return result;
10148}
10149
10150/* Implement the "catch syscall" command. */
10151
10152static void
cc59ec59
MS
10153catch_syscall_command_1 (char *arg, int from_tty,
10154 struct cmd_list_element *command)
a96d9b2e
SDJ
10155{
10156 int tempflag;
10157 VEC(int) *filter;
10158 struct syscall s;
10159 struct gdbarch *gdbarch = get_current_arch ();
10160
10161 /* Checking if the feature if supported. */
10162 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10163 error (_("The feature 'catch syscall' is not supported on \
ea666128 10164this architecture yet."));
a96d9b2e
SDJ
10165
10166 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10167
e9cafbcc 10168 arg = skip_spaces (arg);
a96d9b2e
SDJ
10169
10170 /* We need to do this first "dummy" translation in order
10171 to get the syscall XML file loaded or, most important,
10172 to display a warning to the user if there's no XML file
10173 for his/her architecture. */
10174 get_syscall_by_number (0, &s);
10175
10176 /* The allowed syntax is:
10177 catch syscall
10178 catch syscall <name | number> [<name | number> ... <name | number>]
10179
10180 Let's check if there's a syscall name. */
10181
10182 if (arg != NULL)
10183 filter = catch_syscall_split_args (arg);
10184 else
10185 filter = NULL;
10186
10187 create_syscall_event_catchpoint (tempflag, filter,
10188 &catch_syscall_breakpoint_ops);
10189}
10190
c906108c 10191static void
fba45db2 10192catch_command (char *arg, int from_tty)
c906108c 10193{
44feb3ce 10194 error (_("Catch requires an event name."));
c906108c
SS
10195}
10196\f
10197
10198static void
fba45db2 10199tcatch_command (char *arg, int from_tty)
c906108c 10200{
44feb3ce 10201 error (_("Catch requires an event name."));
c906108c
SS
10202}
10203
80f8a6eb 10204/* Delete breakpoints by address or line. */
c906108c
SS
10205
10206static void
fba45db2 10207clear_command (char *arg, int from_tty)
c906108c 10208{
d6e956e5
VP
10209 struct breakpoint *b;
10210 VEC(breakpoint_p) *found = 0;
10211 int ix;
c906108c
SS
10212 int default_match;
10213 struct symtabs_and_lines sals;
10214 struct symtab_and_line sal;
c906108c
SS
10215 int i;
10216
10217 if (arg)
10218 {
10219 sals = decode_line_spec (arg, 1);
10220 default_match = 0;
10221 }
10222 else
10223 {
c5aa993b 10224 sals.sals = (struct symtab_and_line *)
c906108c 10225 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10226 make_cleanup (xfree, sals.sals);
4a64f543 10227 init_sal (&sal); /* Initialize to zeroes. */
c906108c
SS
10228 sal.line = default_breakpoint_line;
10229 sal.symtab = default_breakpoint_symtab;
10230 sal.pc = default_breakpoint_address;
6c95b8df 10231 sal.pspace = default_breakpoint_pspace;
c906108c 10232 if (sal.symtab == 0)
8a3fe4f8 10233 error (_("No source file specified."));
c906108c
SS
10234
10235 sals.sals[0] = sal;
10236 sals.nelts = 1;
10237
10238 default_match = 1;
10239 }
10240
4a64f543
MS
10241 /* We don't call resolve_sal_pc here. That's not as bad as it
10242 seems, because all existing breakpoints typically have both
10243 file/line and pc set. So, if clear is given file/line, we can
10244 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10245
10246 We only support clearing given the address explicitly
10247 present in breakpoint table. Say, we've set breakpoint
4a64f543 10248 at file:line. There were several PC values for that file:line,
ed0616c6 10249 due to optimization, all in one block.
4a64f543
MS
10250
10251 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10252 PC corresponding to the same file:line, the breakpoint won't
10253 be cleared. We probably can still clear the breakpoint, but
10254 since the other PC value is never presented to user, user
10255 can only find it by guessing, and it does not seem important
10256 to support that. */
10257
4a64f543
MS
10258 /* For each line spec given, delete bps which correspond to it. Do
10259 it in two passes, solely to preserve the current behavior that
10260 from_tty is forced true if we delete more than one
10261 breakpoint. */
c906108c 10262
80f8a6eb 10263 found = NULL;
c906108c
SS
10264 for (i = 0; i < sals.nelts; i++)
10265 {
10266 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10267 If line given (pc == 0), clear all bpts on specified line.
10268 If defaulting, clear all bpts on default line
c906108c 10269 or at default pc.
c5aa993b
JM
10270
10271 defaulting sal.pc != 0 tests to do
10272
10273 0 1 pc
10274 1 1 pc _and_ line
10275 0 0 line
10276 1 0 <can't happen> */
c906108c
SS
10277
10278 sal = sals.sals[i];
c906108c 10279
4a64f543 10280 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10281 ALL_BREAKPOINTS (b)
c5aa993b 10282 {
0d381245 10283 int match = 0;
4a64f543 10284 /* Are we going to delete b? */
cc60f2e3 10285 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10286 {
10287 struct bp_location *loc = b->loc;
10288 for (; loc; loc = loc->next)
10289 {
6c95b8df
PA
10290 int pc_match = sal.pc
10291 && (loc->pspace == sal.pspace)
0d381245
VP
10292 && (loc->address == sal.pc)
10293 && (!section_is_overlay (loc->section)
10294 || loc->section == sal.section);
10295 int line_match = ((default_match || (0 == sal.pc))
10296 && b->source_file != NULL
10297 && sal.symtab != NULL
6c95b8df 10298 && sal.pspace == loc->pspace
0ba1096a
KT
10299 && filename_cmp (b->source_file,
10300 sal.symtab->filename) == 0
0d381245
VP
10301 && b->line_number == sal.line);
10302 if (pc_match || line_match)
10303 {
10304 match = 1;
10305 break;
10306 }
10307 }
10308 }
10309
10310 if (match)
d6e956e5 10311 VEC_safe_push(breakpoint_p, found, b);
c906108c 10312 }
80f8a6eb
MS
10313 }
10314 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10315 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10316 {
10317 if (arg)
8a3fe4f8 10318 error (_("No breakpoint at %s."), arg);
80f8a6eb 10319 else
8a3fe4f8 10320 error (_("No breakpoint at this line."));
80f8a6eb 10321 }
c906108c 10322
d6e956e5 10323 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10324 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10325 if (from_tty)
a3f17187 10326 {
d6e956e5 10327 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10328 printf_unfiltered (_("Deleted breakpoint "));
10329 else
10330 printf_unfiltered (_("Deleted breakpoints "));
10331 }
80f8a6eb 10332 breakpoints_changed ();
d6e956e5
VP
10333
10334 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10335 {
c5aa993b 10336 if (from_tty)
d6e956e5
VP
10337 printf_unfiltered ("%d ", b->number);
10338 delete_breakpoint (b);
c906108c 10339 }
80f8a6eb
MS
10340 if (from_tty)
10341 putchar_unfiltered ('\n');
c906108c
SS
10342}
10343\f
10344/* Delete breakpoint in BS if they are `delete' breakpoints and
10345 all breakpoints that are marked for deletion, whether hit or not.
10346 This is called after any breakpoint is hit, or after errors. */
10347
10348void
fba45db2 10349breakpoint_auto_delete (bpstat bs)
c906108c 10350{
35df4500 10351 struct breakpoint *b, *b_tmp;
c906108c
SS
10352
10353 for (; bs; bs = bs->next)
f431efe5
PA
10354 if (bs->breakpoint_at
10355 && bs->breakpoint_at->disposition == disp_del
c906108c 10356 && bs->stop)
f431efe5 10357 delete_breakpoint (bs->breakpoint_at);
c906108c 10358
35df4500 10359 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10360 {
b5de0fa7 10361 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10362 delete_breakpoint (b);
10363 }
c906108c
SS
10364}
10365
4a64f543
MS
10366/* A comparison function for bp_location AP and BP being interfaced to
10367 qsort. Sort elements primarily by their ADDRESS (no matter what
10368 does breakpoint_address_is_meaningful say for its OWNER),
10369 secondarily by ordering first bp_permanent OWNERed elements and
10370 terciarily just ensuring the array is sorted stable way despite
10371 qsort being an instable algorithm. */
876fa593
JK
10372
10373static int
494cfb0f 10374bp_location_compare (const void *ap, const void *bp)
876fa593 10375{
494cfb0f
JK
10376 struct bp_location *a = *(void **) ap;
10377 struct bp_location *b = *(void **) bp;
2bdf28a0 10378 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10379 int a_perm = a->owner->enable_state == bp_permanent;
10380 int b_perm = b->owner->enable_state == bp_permanent;
10381
10382 if (a->address != b->address)
10383 return (a->address > b->address) - (a->address < b->address);
10384
10385 /* Sort permanent breakpoints first. */
10386 if (a_perm != b_perm)
10387 return (a_perm < b_perm) - (a_perm > b_perm);
10388
4a64f543
MS
10389 /* Make the user-visible order stable across GDB runs. Locations of
10390 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10391
10392 if (a->owner->number != b->owner->number)
10393 return (a->owner->number > b->owner->number)
10394 - (a->owner->number < b->owner->number);
10395
10396 return (a > b) - (a < b);
10397}
10398
876fa593 10399/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10400 bp_location_shadow_len_after_address_max according to the current
10401 content of the bp_location array. */
f7545552
TT
10402
10403static void
876fa593 10404bp_location_target_extensions_update (void)
f7545552 10405{
876fa593
JK
10406 struct bp_location *bl, **blp_tmp;
10407
10408 bp_location_placed_address_before_address_max = 0;
10409 bp_location_shadow_len_after_address_max = 0;
10410
10411 ALL_BP_LOCATIONS (bl, blp_tmp)
10412 {
10413 CORE_ADDR start, end, addr;
10414
10415 if (!bp_location_has_shadow (bl))
10416 continue;
10417
10418 start = bl->target_info.placed_address;
10419 end = start + bl->target_info.shadow_len;
10420
10421 gdb_assert (bl->address >= start);
10422 addr = bl->address - start;
10423 if (addr > bp_location_placed_address_before_address_max)
10424 bp_location_placed_address_before_address_max = addr;
10425
10426 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10427
10428 gdb_assert (bl->address < end);
10429 addr = end - bl->address;
10430 if (addr > bp_location_shadow_len_after_address_max)
10431 bp_location_shadow_len_after_address_max = addr;
10432 }
f7545552
TT
10433}
10434
4cd9bd08 10435/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10436 into the inferior, only remove already-inserted locations that no
10437 longer should be inserted. Functions that delete a breakpoint or
10438 breakpoints should pass false, so that deleting a breakpoint
10439 doesn't have the side effect of inserting the locations of other
10440 breakpoints that are marked not-inserted, but should_be_inserted
10441 returns true on them.
10442
10443 This behaviour is useful is situations close to tear-down -- e.g.,
10444 after an exec, while the target still has execution, but breakpoint
10445 shadows of the previous executable image should *NOT* be restored
10446 to the new image; or before detaching, where the target still has
10447 execution and wants to delete breakpoints from GDB's lists, and all
10448 breakpoints had already been removed from the inferior. */
10449
0d381245 10450static void
b60e7edf 10451update_global_location_list (int should_insert)
0d381245 10452{
74960c60 10453 struct breakpoint *b;
876fa593 10454 struct bp_location **locp, *loc;
f7545552
TT
10455 struct cleanup *cleanups;
10456
2d134ed3
PA
10457 /* Used in the duplicates detection below. When iterating over all
10458 bp_locations, points to the first bp_location of a given address.
10459 Breakpoints and watchpoints of different types are never
10460 duplicates of each other. Keep one pointer for each type of
10461 breakpoint/watchpoint, so we only need to loop over all locations
10462 once. */
10463 struct bp_location *bp_loc_first; /* breakpoint */
10464 struct bp_location *wp_loc_first; /* hardware watchpoint */
10465 struct bp_location *awp_loc_first; /* access watchpoint */
10466 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10467
4a64f543
MS
10468 /* Saved former bp_location array which we compare against the newly
10469 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10470 struct bp_location **old_location, **old_locp;
10471 unsigned old_location_count;
10472
10473 old_location = bp_location;
10474 old_location_count = bp_location_count;
10475 bp_location = NULL;
10476 bp_location_count = 0;
10477 cleanups = make_cleanup (xfree, old_location);
0d381245 10478
74960c60 10479 ALL_BREAKPOINTS (b)
876fa593
JK
10480 for (loc = b->loc; loc; loc = loc->next)
10481 bp_location_count++;
10482
10483 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10484 locp = bp_location;
10485 ALL_BREAKPOINTS (b)
10486 for (loc = b->loc; loc; loc = loc->next)
10487 *locp++ = loc;
10488 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10489 bp_location_compare);
876fa593
JK
10490
10491 bp_location_target_extensions_update ();
74960c60 10492
4a64f543
MS
10493 /* Identify bp_location instances that are no longer present in the
10494 new list, and therefore should be freed. Note that it's not
10495 necessary that those locations should be removed from inferior --
10496 if there's another location at the same address (previously
10497 marked as duplicate), we don't need to remove/insert the
10498 location.
876fa593 10499
4a64f543
MS
10500 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10501 and former bp_location array state respectively. */
876fa593
JK
10502
10503 locp = bp_location;
10504 for (old_locp = old_location; old_locp < old_location + old_location_count;
10505 old_locp++)
74960c60 10506 {
876fa593 10507 struct bp_location *old_loc = *old_locp;
c7d46a38 10508 struct bp_location **loc2p;
876fa593 10509
4a64f543
MS
10510 /* Tells if 'old_loc' is found amoung the new locations. If
10511 not, we have to free it. */
c7d46a38 10512 int found_object = 0;
20874c92
VP
10513 /* Tells if the location should remain inserted in the target. */
10514 int keep_in_target = 0;
10515 int removed = 0;
876fa593 10516
4a64f543
MS
10517 /* Skip LOCP entries which will definitely never be needed.
10518 Stop either at or being the one matching OLD_LOC. */
876fa593 10519 while (locp < bp_location + bp_location_count
c7d46a38 10520 && (*locp)->address < old_loc->address)
876fa593 10521 locp++;
c7d46a38
PA
10522
10523 for (loc2p = locp;
10524 (loc2p < bp_location + bp_location_count
10525 && (*loc2p)->address == old_loc->address);
10526 loc2p++)
10527 {
10528 if (*loc2p == old_loc)
10529 {
10530 found_object = 1;
10531 break;
10532 }
10533 }
74960c60 10534
4a64f543
MS
10535 /* If this location is no longer present, and inserted, look if
10536 there's maybe a new location at the same address. If so,
10537 mark that one inserted, and don't remove this one. This is
10538 needed so that we don't have a time window where a breakpoint
10539 at certain location is not inserted. */
74960c60 10540
876fa593 10541 if (old_loc->inserted)
0d381245 10542 {
4a64f543
MS
10543 /* If the location is inserted now, we might have to remove
10544 it. */
74960c60 10545
876fa593 10546 if (found_object && should_be_inserted (old_loc))
74960c60 10547 {
4a64f543
MS
10548 /* The location is still present in the location list,
10549 and still should be inserted. Don't do anything. */
20874c92 10550 keep_in_target = 1;
74960c60
VP
10551 }
10552 else
10553 {
4a64f543
MS
10554 /* The location is either no longer present, or got
10555 disabled. See if there's another location at the
10556 same address, in which case we don't need to remove
10557 this one from the target. */
876fa593 10558
2bdf28a0 10559 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10560 if (breakpoint_address_is_meaningful (old_loc->owner))
10561 {
876fa593 10562 for (loc2p = locp;
c7d46a38
PA
10563 (loc2p < bp_location + bp_location_count
10564 && (*loc2p)->address == old_loc->address);
876fa593
JK
10565 loc2p++)
10566 {
10567 struct bp_location *loc2 = *loc2p;
10568
2d134ed3 10569 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38
PA
10570 {
10571 /* For the sake of should_be_inserted.
4a64f543
MS
10572 Duplicates check below will fix up this
10573 later. */
c7d46a38 10574 loc2->duplicate = 0;
85d721b8
PA
10575
10576 /* Read watchpoint locations are switched to
10577 access watchpoints, if the former are not
10578 supported, but the latter are. */
10579 if (is_hardware_watchpoint (old_loc->owner))
10580 {
10581 gdb_assert (is_hardware_watchpoint (loc2->owner));
10582 loc2->watchpoint_type = old_loc->watchpoint_type;
10583 }
10584
c7d46a38
PA
10585 if (loc2 != old_loc && should_be_inserted (loc2))
10586 {
10587 loc2->inserted = 1;
10588 loc2->target_info = old_loc->target_info;
10589 keep_in_target = 1;
10590 break;
10591 }
876fa593
JK
10592 }
10593 }
10594 }
74960c60
VP
10595 }
10596
20874c92
VP
10597 if (!keep_in_target)
10598 {
876fa593 10599 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10600 {
4a64f543
MS
10601 /* This is just about all we can do. We could keep
10602 this location on the global list, and try to
10603 remove it next time, but there's no particular
10604 reason why we will succeed next time.
20874c92 10605
4a64f543
MS
10606 Note that at this point, old_loc->owner is still
10607 valid, as delete_breakpoint frees the breakpoint
10608 only after calling us. */
3e43a32a
MS
10609 printf_filtered (_("warning: Error removing "
10610 "breakpoint %d\n"),
876fa593 10611 old_loc->owner->number);
20874c92
VP
10612 }
10613 removed = 1;
10614 }
0d381245 10615 }
74960c60
VP
10616
10617 if (!found_object)
1c5cfe86 10618 {
db82e815
PA
10619 if (removed && non_stop
10620 && breakpoint_address_is_meaningful (old_loc->owner)
10621 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10622 {
db82e815
PA
10623 /* This location was removed from the target. In
10624 non-stop mode, a race condition is possible where
10625 we've removed a breakpoint, but stop events for that
10626 breakpoint are already queued and will arrive later.
10627 We apply an heuristic to be able to distinguish such
10628 SIGTRAPs from other random SIGTRAPs: we keep this
10629 breakpoint location for a bit, and will retire it
10630 after we see some number of events. The theory here
10631 is that reporting of events should, "on the average",
10632 be fair, so after a while we'll see events from all
10633 threads that have anything of interest, and no longer
10634 need to keep this breakpoint location around. We
10635 don't hold locations forever so to reduce chances of
10636 mistaking a non-breakpoint SIGTRAP for a breakpoint
10637 SIGTRAP.
10638
10639 The heuristic failing can be disastrous on
10640 decr_pc_after_break targets.
10641
10642 On decr_pc_after_break targets, like e.g., x86-linux,
10643 if we fail to recognize a late breakpoint SIGTRAP,
10644 because events_till_retirement has reached 0 too
10645 soon, we'll fail to do the PC adjustment, and report
10646 a random SIGTRAP to the user. When the user resumes
10647 the inferior, it will most likely immediately crash
2dec564e 10648 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10649 corrupted, because of being resumed e.g., in the
10650 middle of a multi-byte instruction, or skipped a
10651 one-byte instruction. This was actually seen happen
10652 on native x86-linux, and should be less rare on
10653 targets that do not support new thread events, like
10654 remote, due to the heuristic depending on
10655 thread_count.
10656
10657 Mistaking a random SIGTRAP for a breakpoint trap
10658 causes similar symptoms (PC adjustment applied when
10659 it shouldn't), but then again, playing with SIGTRAPs
10660 behind the debugger's back is asking for trouble.
10661
10662 Since hardware watchpoint traps are always
10663 distinguishable from other traps, so we don't need to
10664 apply keep hardware watchpoint moribund locations
10665 around. We simply always ignore hardware watchpoint
10666 traps we can no longer explain. */
10667
876fa593
JK
10668 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10669 old_loc->owner = NULL;
20874c92 10670
876fa593 10671 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10672 }
10673 else
f431efe5
PA
10674 {
10675 old_loc->owner = NULL;
10676 decref_bp_location (&old_loc);
10677 }
20874c92 10678 }
74960c60 10679 }
1c5cfe86 10680
2d134ed3
PA
10681 /* Rescan breakpoints at the same address and section, marking the
10682 first one as "first" and any others as "duplicates". This is so
10683 that the bpt instruction is only inserted once. If we have a
10684 permanent breakpoint at the same place as BPT, make that one the
10685 official one, and the rest as duplicates. Permanent breakpoints
10686 are sorted first for the same address.
10687
10688 Do the same for hardware watchpoints, but also considering the
10689 watchpoint's type (regular/access/read) and length. */
876fa593 10690
2d134ed3
PA
10691 bp_loc_first = NULL;
10692 wp_loc_first = NULL;
10693 awp_loc_first = NULL;
10694 rwp_loc_first = NULL;
876fa593 10695 ALL_BP_LOCATIONS (loc, locp)
74960c60 10696 {
4a64f543
MS
10697 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10698 non-NULL. */
876fa593 10699 struct breakpoint *b = loc->owner;
2d134ed3 10700 struct bp_location **loc_first_p;
876fa593
JK
10701
10702 if (b->enable_state == bp_disabled
10703 || b->enable_state == bp_call_disabled
10704 || b->enable_state == bp_startup_disabled
10705 || !loc->enabled
10706 || loc->shlib_disabled
15c3d785 10707 || !breakpoint_address_is_meaningful (b)
d77f58be 10708 || is_tracepoint (b))
876fa593
JK
10709 continue;
10710
10711 /* Permanent breakpoint should always be inserted. */
10712 if (b->enable_state == bp_permanent && ! loc->inserted)
10713 internal_error (__FILE__, __LINE__,
10714 _("allegedly permanent breakpoint is not "
10715 "actually inserted"));
10716
2d134ed3
PA
10717 if (b->type == bp_hardware_watchpoint)
10718 loc_first_p = &wp_loc_first;
10719 else if (b->type == bp_read_watchpoint)
10720 loc_first_p = &rwp_loc_first;
10721 else if (b->type == bp_access_watchpoint)
10722 loc_first_p = &awp_loc_first;
10723 else
10724 loc_first_p = &bp_loc_first;
10725
10726 if (*loc_first_p == NULL
10727 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10728 || !breakpoint_locations_match (loc, *loc_first_p))
876fa593 10729 {
2d134ed3 10730 *loc_first_p = loc;
876fa593
JK
10731 loc->duplicate = 0;
10732 continue;
10733 }
10734
10735 loc->duplicate = 1;
10736
2d134ed3
PA
10737 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10738 && b->enable_state != bp_permanent)
876fa593
JK
10739 internal_error (__FILE__, __LINE__,
10740 _("another breakpoint was inserted on top of "
10741 "a permanent breakpoint"));
0d381245 10742 }
74960c60 10743
50c71eaf 10744 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 10745 && (have_live_inferiors ()
2567c7d9 10746 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 10747 insert_breakpoint_locations ();
f7545552
TT
10748
10749 do_cleanups (cleanups);
74960c60
VP
10750}
10751
20874c92
VP
10752void
10753breakpoint_retire_moribund (void)
10754{
10755 struct bp_location *loc;
10756 int ix;
10757
10758 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10759 if (--(loc->events_till_retirement) == 0)
10760 {
f431efe5 10761 decref_bp_location (&loc);
20874c92
VP
10762 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10763 --ix;
10764 }
10765}
10766
74960c60 10767static void
b60e7edf 10768update_global_location_list_nothrow (int inserting)
74960c60
VP
10769{
10770 struct gdb_exception e;
cc59ec59 10771
74960c60 10772 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 10773 update_global_location_list (inserting);
0d381245
VP
10774}
10775
f431efe5
PA
10776/* Clear BKP from a BPS. */
10777
a474d7c2 10778static void
f431efe5 10779bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
a474d7c2
PA
10780{
10781 bpstat bs;
cc59ec59 10782
a474d7c2 10783 for (bs = bps; bs; bs = bs->next)
f431efe5 10784 if (bs->breakpoint_at == bpt)
a474d7c2
PA
10785 {
10786 bs->breakpoint_at = NULL;
10787 bs->old_val = NULL;
10788 /* bs->commands will be freed later. */
10789 }
10790}
10791
10792/* Callback for iterate_over_threads. */
10793static int
f431efe5 10794bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
a474d7c2 10795{
f431efe5 10796 struct breakpoint *bpt = data;
cc59ec59 10797
16c381f0 10798 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
a474d7c2
PA
10799 return 0;
10800}
10801
53a5351d 10802/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 10803 structures. */
c906108c
SS
10804
10805void
fba45db2 10806delete_breakpoint (struct breakpoint *bpt)
c906108c 10807{
52f0bd74 10808 struct breakpoint *b;
c906108c 10809
8a3fe4f8 10810 gdb_assert (bpt != NULL);
c906108c 10811
4a64f543
MS
10812 /* Has this bp already been deleted? This can happen because
10813 multiple lists can hold pointers to bp's. bpstat lists are
10814 especial culprits.
10815
10816 One example of this happening is a watchpoint's scope bp. When
10817 the scope bp triggers, we notice that the watchpoint is out of
10818 scope, and delete it. We also delete its scope bp. But the
10819 scope bp is marked "auto-deleting", and is already on a bpstat.
10820 That bpstat is then checked for auto-deleting bp's, which are
10821 deleted.
10822
10823 A real solution to this problem might involve reference counts in
10824 bp's, and/or giving them pointers back to their referencing
10825 bpstat's, and teaching delete_breakpoint to only free a bp's
10826 storage when no more references were extent. A cheaper bandaid
10827 was chosen. */
c906108c
SS
10828 if (bpt->type == bp_none)
10829 return;
10830
4a64f543
MS
10831 /* At least avoid this stale reference until the reference counting
10832 of breakpoints gets resolved. */
d0fb5eae 10833 if (bpt->related_breakpoint != bpt)
e5a0a904 10834 {
d0fb5eae
JK
10835 struct breakpoint *related;
10836
10837 if (bpt->type == bp_watchpoint_scope)
10838 watchpoint_del_at_next_stop (bpt->related_breakpoint);
10839 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
10840 watchpoint_del_at_next_stop (bpt);
10841
10842 /* Unlink bpt from the bpt->related_breakpoint ring. */
10843 for (related = bpt; related->related_breakpoint != bpt;
10844 related = related->related_breakpoint);
10845 related->related_breakpoint = bpt->related_breakpoint;
10846 bpt->related_breakpoint = bpt;
e5a0a904
JK
10847 }
10848
a9634178
TJB
10849 /* watch_command_1 creates a watchpoint but only sets its number if
10850 update_watchpoint succeeds in creating its bp_locations. If there's
10851 a problem in that process, we'll be asked to delete the half-created
10852 watchpoint. In that case, don't announce the deletion. */
10853 if (bpt->number)
10854 observer_notify_breakpoint_deleted (bpt);
c906108c 10855
c906108c
SS
10856 if (breakpoint_chain == bpt)
10857 breakpoint_chain = bpt->next;
10858
c906108c
SS
10859 ALL_BREAKPOINTS (b)
10860 if (b->next == bpt)
c5aa993b
JM
10861 {
10862 b->next = bpt->next;
10863 break;
10864 }
c906108c 10865
be5c67c1
PA
10866 if (bpt->ops != NULL && bpt->ops->dtor != NULL)
10867 bpt->ops->dtor (bpt);
10868
9add0f1b 10869 decref_counted_command_line (&bpt->commands);
60e1c644
PA
10870 xfree (bpt->cond_string);
10871 xfree (bpt->cond_exp);
10872 xfree (bpt->addr_string);
f1310107 10873 xfree (bpt->addr_string_range_end);
60e1c644
PA
10874 xfree (bpt->exp);
10875 xfree (bpt->exp_string);
d63d0675 10876 xfree (bpt->exp_string_reparse);
60e1c644
PA
10877 value_free (bpt->val);
10878 xfree (bpt->source_file);
c906108c 10879
f431efe5
PA
10880
10881 /* Be sure no bpstat's are pointing at the breakpoint after it's
10882 been freed. */
10883 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
10884 in all threeds for now. Note that we cannot just remove bpstats
10885 pointing at bpt from the stop_bpstat list entirely, as breakpoint
10886 commands are associated with the bpstat; if we remove it here,
10887 then the later call to bpstat_do_actions (&stop_bpstat); in
10888 event-top.c won't do anything, and temporary breakpoints with
10889 commands won't work. */
10890
10891 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
10892
4a64f543
MS
10893 /* Now that breakpoint is removed from breakpoint list, update the
10894 global location list. This will remove locations that used to
10895 belong to this breakpoint. Do this before freeing the breakpoint
10896 itself, since remove_breakpoint looks at location's owner. It
10897 might be better design to have location completely
10898 self-contained, but it's not the case now. */
b60e7edf 10899 update_global_location_list (0);
74960c60
VP
10900
10901
4a64f543
MS
10902 /* On the chance that someone will soon try again to delete this
10903 same bp, we mark it as deleted before freeing its storage. */
c906108c
SS
10904 bpt->type = bp_none;
10905
b8c9b27d 10906 xfree (bpt);
c906108c
SS
10907}
10908
4d6140d9
AC
10909static void
10910do_delete_breakpoint_cleanup (void *b)
10911{
10912 delete_breakpoint (b);
10913}
10914
10915struct cleanup *
10916make_cleanup_delete_breakpoint (struct breakpoint *b)
10917{
10918 return make_cleanup (do_delete_breakpoint_cleanup, b);
10919}
10920
51be5b68
PA
10921/* Iterator function to call a user-provided callback function once
10922 for each of B and its related breakpoints. */
10923
10924static void
10925iterate_over_related_breakpoints (struct breakpoint *b,
10926 void (*function) (struct breakpoint *,
10927 void *),
10928 void *data)
10929{
10930 struct breakpoint *related;
10931
10932 related = b;
10933 do
10934 {
10935 struct breakpoint *next;
10936
10937 /* FUNCTION may delete RELATED. */
10938 next = related->related_breakpoint;
10939
10940 if (next == related)
10941 {
10942 /* RELATED is the last ring entry. */
10943 function (related, data);
10944
10945 /* FUNCTION may have deleted it, so we'd never reach back to
10946 B. There's nothing left to do anyway, so just break
10947 out. */
10948 break;
10949 }
10950 else
10951 function (related, data);
10952
10953 related = next;
10954 }
10955 while (related != b);
10956}
95a42b64
TT
10957
10958static void
10959do_delete_breakpoint (struct breakpoint *b, void *ignore)
10960{
10961 delete_breakpoint (b);
10962}
10963
51be5b68
PA
10964/* A callback for map_breakpoint_numbers that calls
10965 delete_breakpoint. */
10966
10967static void
10968do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
10969{
10970 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
10971}
10972
c906108c 10973void
fba45db2 10974delete_command (char *arg, int from_tty)
c906108c 10975{
35df4500 10976 struct breakpoint *b, *b_tmp;
c906108c 10977
ea9365bb
TT
10978 dont_repeat ();
10979
c906108c
SS
10980 if (arg == 0)
10981 {
10982 int breaks_to_delete = 0;
10983
46c6471b
PA
10984 /* Delete all breakpoints if no argument. Do not delete
10985 internal breakpoints, these have to be deleted with an
10986 explicit breakpoint number argument. */
c5aa993b 10987 ALL_BREAKPOINTS (b)
46c6471b 10988 if (user_breakpoint_p (b))
973d738b
DJ
10989 {
10990 breaks_to_delete = 1;
10991 break;
10992 }
c906108c
SS
10993
10994 /* Ask user only if there are some breakpoints to delete. */
10995 if (!from_tty
e2e0b3e5 10996 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 10997 {
35df4500 10998 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 10999 if (user_breakpoint_p (b))
c5aa993b 11000 delete_breakpoint (b);
c906108c
SS
11001 }
11002 }
11003 else
51be5b68 11004 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11005}
11006
0d381245
VP
11007static int
11008all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11009{
0d381245
VP
11010 for (; loc; loc = loc->next)
11011 if (!loc->shlib_disabled)
11012 return 0;
11013 return 1;
fe3f5fa8
VP
11014}
11015
776592bf
DE
11016/* Subroutine of update_breakpoint_locations to simplify it.
11017 Return non-zero if multiple fns in list LOC have the same name.
11018 Null names are ignored. */
11019
11020static int
11021ambiguous_names_p (struct bp_location *loc)
11022{
11023 struct bp_location *l;
11024 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11025 (int (*) (const void *,
11026 const void *)) streq,
776592bf
DE
11027 NULL, xcalloc, xfree);
11028
11029 for (l = loc; l != NULL; l = l->next)
11030 {
11031 const char **slot;
11032 const char *name = l->function_name;
11033
11034 /* Allow for some names to be NULL, ignore them. */
11035 if (name == NULL)
11036 continue;
11037
11038 slot = (const char **) htab_find_slot (htab, (const void *) name,
11039 INSERT);
4a64f543
MS
11040 /* NOTE: We can assume slot != NULL here because xcalloc never
11041 returns NULL. */
776592bf
DE
11042 if (*slot != NULL)
11043 {
11044 htab_delete (htab);
11045 return 1;
11046 }
11047 *slot = name;
11048 }
11049
11050 htab_delete (htab);
11051 return 0;
11052}
11053
0fb4aa4b
PA
11054/* When symbols change, it probably means the sources changed as well,
11055 and it might mean the static tracepoint markers are no longer at
11056 the same address or line numbers they used to be at last we
11057 checked. Losing your static tracepoints whenever you rebuild is
11058 undesirable. This function tries to resync/rematch gdb static
11059 tracepoints with the markers on the target, for static tracepoints
11060 that have not been set by marker id. Static tracepoint that have
11061 been set by marker id are reset by marker id in breakpoint_re_set.
11062 The heuristic is:
11063
11064 1) For a tracepoint set at a specific address, look for a marker at
11065 the old PC. If one is found there, assume to be the same marker.
11066 If the name / string id of the marker found is different from the
11067 previous known name, assume that means the user renamed the marker
11068 in the sources, and output a warning.
11069
11070 2) For a tracepoint set at a given line number, look for a marker
11071 at the new address of the old line number. If one is found there,
11072 assume to be the same marker. If the name / string id of the
11073 marker found is different from the previous known name, assume that
11074 means the user renamed the marker in the sources, and output a
11075 warning.
11076
11077 3) If a marker is no longer found at the same address or line, it
11078 may mean the marker no longer exists. But it may also just mean
11079 the code changed a bit. Maybe the user added a few lines of code
11080 that made the marker move up or down (in line number terms). Ask
11081 the target for info about the marker with the string id as we knew
11082 it. If found, update line number and address in the matching
11083 static tracepoint. This will get confused if there's more than one
11084 marker with the same ID (possible in UST, although unadvised
11085 precisely because it confuses tools). */
11086
11087static struct symtab_and_line
11088update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11089{
11090 struct static_tracepoint_marker marker;
11091 CORE_ADDR pc;
11092 int i;
11093
11094 pc = sal.pc;
11095 if (sal.line)
11096 find_line_pc (sal.symtab, sal.line, &pc);
11097
11098 if (target_static_tracepoint_marker_at (pc, &marker))
11099 {
11100 if (strcmp (b->static_trace_marker_id, marker.str_id) != 0)
11101 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11102 b->number,
11103 b->static_trace_marker_id, marker.str_id);
11104
11105 xfree (b->static_trace_marker_id);
11106 b->static_trace_marker_id = xstrdup (marker.str_id);
11107 release_static_tracepoint_marker (&marker);
11108
11109 return sal;
11110 }
11111
11112 /* Old marker wasn't found on target at lineno. Try looking it up
11113 by string ID. */
11114 if (!sal.explicit_pc
11115 && sal.line != 0
11116 && sal.symtab != NULL
11117 && b->static_trace_marker_id != NULL)
11118 {
11119 VEC(static_tracepoint_marker_p) *markers;
11120
11121 markers
11122 = target_static_tracepoint_markers_by_strid (b->static_trace_marker_id);
11123
11124 if (!VEC_empty(static_tracepoint_marker_p, markers))
11125 {
11126 struct symtab_and_line sal;
11127 struct symbol *sym;
11128 struct static_tracepoint_marker *marker;
11129
11130 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11131
11132 xfree (b->static_trace_marker_id);
11133 b->static_trace_marker_id = xstrdup (marker->str_id);
11134
11135 warning (_("marker for static tracepoint %d (%s) not "
11136 "found at previous line number"),
11137 b->number, b->static_trace_marker_id);
11138
11139 init_sal (&sal);
11140
11141 sal.pc = marker->address;
11142
11143 sal = find_pc_line (marker->address, 0);
11144 sym = find_pc_sect_function (marker->address, NULL);
11145 ui_out_text (uiout, "Now in ");
11146 if (sym)
11147 {
11148 ui_out_field_string (uiout, "func",
11149 SYMBOL_PRINT_NAME (sym));
11150 ui_out_text (uiout, " at ");
11151 }
11152 ui_out_field_string (uiout, "file", sal.symtab->filename);
11153 ui_out_text (uiout, ":");
11154
11155 if (ui_out_is_mi_like_p (uiout))
11156 {
11157 char *fullname = symtab_to_fullname (sal.symtab);
11158
11159 if (fullname)
11160 ui_out_field_string (uiout, "fullname", fullname);
11161 }
11162
11163 ui_out_field_int (uiout, "line", sal.line);
11164 ui_out_text (uiout, "\n");
11165
11166 b->line_number = sal.line;
11167
11168 xfree (b->source_file);
11169 if (sym)
11170 b->source_file = xstrdup (sal.symtab->filename);
11171 else
11172 b->source_file = NULL;
11173
11174 xfree (b->addr_string);
11175 b->addr_string = xstrprintf ("%s:%d",
11176 sal.symtab->filename, b->line_number);
11177
11178 /* Might be nice to check if function changed, and warn if
11179 so. */
11180
11181 release_static_tracepoint_marker (marker);
11182 }
11183 }
11184 return sal;
11185}
11186
8d3788bd
VP
11187/* Returns 1 iff locations A and B are sufficiently same that
11188 we don't need to report breakpoint as changed. */
11189
11190static int
11191locations_are_equal (struct bp_location *a, struct bp_location *b)
11192{
11193 while (a && b)
11194 {
11195 if (a->address != b->address)
11196 return 0;
11197
11198 if (a->shlib_disabled != b->shlib_disabled)
11199 return 0;
11200
11201 if (a->enabled != b->enabled)
11202 return 0;
11203
11204 a = a->next;
11205 b = b->next;
11206 }
11207
11208 if ((a == NULL) != (b == NULL))
11209 return 0;
11210
11211 return 1;
11212}
11213
f1310107
TJB
11214/* Create new breakpoint locations for B (a hardware or software breakpoint)
11215 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11216 a ranged breakpoint. */
11217
0e30163f 11218void
0d381245 11219update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11220 struct symtabs_and_lines sals,
11221 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11222{
11223 int i;
0d381245
VP
11224 struct bp_location *existing_locations = b->loc;
11225
f1310107
TJB
11226 /* Ranged breakpoints have only one start location and one end location. */
11227 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11228
4a64f543
MS
11229 /* If there's no new locations, and all existing locations are
11230 pending, don't do anything. This optimizes the common case where
11231 all locations are in the same shared library, that was unloaded.
11232 We'd like to retain the location, so that when the library is
11233 loaded again, we don't loose the enabled/disabled status of the
11234 individual locations. */
0d381245 11235 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11236 return;
11237
fe3f5fa8
VP
11238 b->loc = NULL;
11239
0d381245 11240 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11241 {
0d381245 11242 struct bp_location *new_loc =
39d61571 11243 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11244
0d381245
VP
11245 /* Reparse conditions, they might contain references to the
11246 old symtab. */
11247 if (b->cond_string != NULL)
11248 {
f1310107 11249 char *s;
0d381245 11250 struct gdb_exception e;
fe3f5fa8 11251
0d381245
VP
11252 s = b->cond_string;
11253 TRY_CATCH (e, RETURN_MASK_ERROR)
11254 {
11255 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11256 0);
11257 }
11258 if (e.reason < 0)
11259 {
3e43a32a
MS
11260 warning (_("failed to reevaluate condition "
11261 "for breakpoint %d: %s"),
0d381245
VP
11262 b->number, e.message);
11263 new_loc->enabled = 0;
11264 }
11265 }
fe3f5fa8 11266
0d381245
VP
11267 if (b->source_file != NULL)
11268 xfree (b->source_file);
11269 if (sals.sals[i].symtab == NULL)
11270 b->source_file = NULL;
11271 else
1b36a34b 11272 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11273
0d381245
VP
11274 if (b->line_number == 0)
11275 b->line_number = sals.sals[i].line;
f1310107
TJB
11276
11277 if (sals_end.nelts)
11278 {
11279 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11280
11281 new_loc->length = end - sals.sals[0].pc + 1;
11282 }
0d381245 11283 }
fe3f5fa8 11284
514f746b
AR
11285 /* Update locations of permanent breakpoints. */
11286 if (b->enable_state == bp_permanent)
11287 make_breakpoint_permanent (b);
11288
4a64f543
MS
11289 /* If possible, carry over 'disable' status from existing
11290 breakpoints. */
0d381245
VP
11291 {
11292 struct bp_location *e = existing_locations;
776592bf
DE
11293 /* If there are multiple breakpoints with the same function name,
11294 e.g. for inline functions, comparing function names won't work.
11295 Instead compare pc addresses; this is just a heuristic as things
11296 may have moved, but in practice it gives the correct answer
11297 often enough until a better solution is found. */
11298 int have_ambiguous_names = ambiguous_names_p (b->loc);
11299
0d381245
VP
11300 for (; e; e = e->next)
11301 {
11302 if (!e->enabled && e->function_name)
11303 {
11304 struct bp_location *l = b->loc;
776592bf
DE
11305 if (have_ambiguous_names)
11306 {
11307 for (; l; l = l->next)
f1310107 11308 if (breakpoint_locations_match (e, l))
776592bf
DE
11309 {
11310 l->enabled = 0;
11311 break;
11312 }
11313 }
11314 else
11315 {
11316 for (; l; l = l->next)
11317 if (l->function_name
11318 && strcmp (e->function_name, l->function_name) == 0)
11319 {
11320 l->enabled = 0;
11321 break;
11322 }
11323 }
0d381245
VP
11324 }
11325 }
11326 }
fe3f5fa8 11327
8d3788bd
VP
11328 if (!locations_are_equal (existing_locations, b->loc))
11329 observer_notify_breakpoint_modified (b);
11330
b60e7edf 11331 update_global_location_list (1);
fe3f5fa8
VP
11332}
11333
ef23e705
TJB
11334/* Find the SaL locations corresponding to the given ADDR_STRING.
11335 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11336
11337static struct symtabs_and_lines
11338addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11339{
11340 char *s;
58438ac1 11341 int marker_spec;
02d20e4a 11342 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11343 struct gdb_exception e;
11344
11345 s = addr_string;
11346 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11347
11348 TRY_CATCH (e, RETURN_MASK_ERROR)
11349 {
11350 if (marker_spec)
11351 {
11352 sals = decode_static_tracepoint_spec (&s);
11353 if (sals.nelts > b->static_trace_marker_id_idx)
11354 {
11355 sals.sals[0] = sals.sals[b->static_trace_marker_id_idx];
11356 sals.nelts = 1;
11357 }
11358 else
11359 error (_("marker %s not found"), b->static_trace_marker_id);
11360 }
11361 else
58438ac1 11362 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11363 }
11364 if (e.reason < 0)
11365 {
11366 int not_found_and_ok = 0;
11367 /* For pending breakpoints, it's expected that parsing will
11368 fail until the right shared library is loaded. User has
11369 already told to create pending breakpoints and don't need
11370 extra messages. If breakpoint is in bp_shlib_disabled
11371 state, then user already saw the message about that
11372 breakpoint being disabled, and don't want to see more
11373 errors. */
58438ac1 11374 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11375 && (b->condition_not_parsed
11376 || (b->loc && b->loc->shlib_disabled)
11377 || b->enable_state == bp_disabled))
11378 not_found_and_ok = 1;
11379
11380 if (!not_found_and_ok)
11381 {
11382 /* We surely don't want to warn about the same breakpoint
11383 10 times. One solution, implemented here, is disable
11384 the breakpoint on error. Another solution would be to
11385 have separate 'warning emitted' flag. Since this
11386 happens only when a binary has changed, I don't know
11387 which approach is better. */
11388 b->enable_state = bp_disabled;
11389 throw_exception (e);
11390 }
11391 }
11392
58438ac1 11393 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11394 {
11395 gdb_assert (sals.nelts == 1);
11396
11397 resolve_sal_pc (&sals.sals[0]);
11398 if (b->condition_not_parsed && s && s[0])
11399 {
11400 char *cond_string = 0;
11401 int thread = -1;
11402 int task = 0;
11403
11404 find_condition_and_thread (s, sals.sals[0].pc,
11405 &cond_string, &thread, &task);
11406 if (cond_string)
11407 b->cond_string = cond_string;
11408 b->thread = thread;
11409 b->task = task;
11410 b->condition_not_parsed = 0;
11411 }
11412
11413 if (b->type == bp_static_tracepoint && !marker_spec)
11414 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11415
58438ac1
TT
11416 *found = 1;
11417 }
11418 else
11419 *found = 0;
ef23e705
TJB
11420
11421 return sals;
11422}
11423
28010a5d
PA
11424void
11425breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11426{
11427 int found;
f1310107 11428 struct symtabs_and_lines sals, sals_end;
ef23e705 11429 struct symtabs_and_lines expanded = {0};
f1310107 11430 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11431
11432 sals = addr_string_to_sals (b, b->addr_string, &found);
11433 if (found)
11434 {
11435 make_cleanup (xfree, sals.sals);
11436 expanded = expand_line_sal_maybe (sals.sals[0]);
11437 }
11438
f1310107
TJB
11439 if (b->addr_string_range_end)
11440 {
11441 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11442 if (found)
11443 {
11444 make_cleanup (xfree, sals_end.sals);
11445 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11446 }
11447 }
11448
11449 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11450}
11451
11452/* Prepare the global context for a re-set of breakpoint B. */
11453
11454static struct cleanup *
11455prepare_re_set_context (struct breakpoint *b)
11456{
11457 struct cleanup *cleanups;
11458
11459 input_radix = b->input_radix;
11460 cleanups = save_current_space_and_thread ();
11461 switch_to_program_space_and_thread (b->pspace);
11462 set_language (b->language);
11463
11464 return cleanups;
ef23e705
TJB
11465}
11466
c906108c
SS
11467/* Reset a breakpoint given it's struct breakpoint * BINT.
11468 The value we return ends up being the return value from catch_errors.
11469 Unused in this case. */
11470
11471static int
4efb68b1 11472breakpoint_re_set_one (void *bint)
c906108c 11473{
4a64f543 11474 /* Get past catch_errs. */
53a5351d 11475 struct breakpoint *b = (struct breakpoint *) bint;
c906108c 11476
28010a5d
PA
11477 if (b->ops != NULL && b->ops->re_set != NULL)
11478 {
11479 struct cleanup *cleanups;
11480
11481 cleanups = prepare_re_set_context (b);
11482 b->ops->re_set (b);
11483 do_cleanups (cleanups);
11484
11485 return 0;
11486 }
11487
c906108c
SS
11488 switch (b->type)
11489 {
11490 case bp_none:
8a3fe4f8 11491 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 11492 b->number);
c906108c
SS
11493 return 0;
11494 case bp_breakpoint:
11495 case bp_hardware_breakpoint:
1042e4c0 11496 case bp_tracepoint:
7a697b8d 11497 case bp_fast_tracepoint:
0fb4aa4b 11498 case bp_static_tracepoint:
0e30163f 11499 case bp_gnu_ifunc_resolver:
8bea4e01
UW
11500 /* Do not attempt to re-set breakpoints disabled during startup. */
11501 if (b->enable_state == bp_startup_disabled)
11502 return 0;
11503
c906108c
SS
11504 if (b->addr_string == NULL)
11505 {
4a64f543 11506 /* Anything without a string can't be re-set. */
c906108c
SS
11507 delete_breakpoint (b);
11508 return 0;
11509 }
c906108c 11510
28010a5d
PA
11511 {
11512 struct cleanup *cleanups;
11513
11514 cleanups = prepare_re_set_context (b);
11515 breakpoint_re_set_default (b);
11516 do_cleanups (cleanups);
11517 }
c906108c
SS
11518 break;
11519
11520 case bp_watchpoint:
11521 case bp_hardware_watchpoint:
11522 case bp_read_watchpoint:
11523 case bp_access_watchpoint:
4a64f543
MS
11524 /* Watchpoint can be either on expression using entirely global
11525 variables, or it can be on local variables.
11526
11527 Watchpoints of the first kind are never auto-deleted, and
11528 even persist across program restarts. Since they can use
11529 variables from shared libraries, we need to reparse
11530 expression as libraries are loaded and unloaded.
11531
11532 Watchpoints on local variables can also change meaning as
11533 result of solib event. For example, if a watchpoint uses
11534 both a local and a global variables in expression, it's a
11535 local watchpoint, but unloading of a shared library will make
11536 the expression invalid. This is not a very common use case,
11537 but we still re-evaluate expression, to avoid surprises to
11538 the user.
0b3de036
VP
11539
11540 Note that for local watchpoints, we re-evaluate it only if
11541 watchpoints frame id is still valid. If it's not, it means
4a64f543
MS
11542 the watchpoint is out of scope and will be deleted soon. In
11543 fact, I'm not sure we'll ever be called in this case.
0b3de036
VP
11544
11545 If a local watchpoint's frame id is still valid, then
4a64f543 11546 b->exp_valid_block is likewise valid, and we can safely use it.
0b3de036
VP
11547
11548 Don't do anything about disabled watchpoints, since they will
11549 be reevaluated again when enabled. */
a5606eee 11550 update_watchpoint (b, 1 /* reparse */);
c906108c 11551 break;
c5aa993b
JM
11552 /* We needn't really do anything to reset these, since the mask
11553 that requests them is unaffected by e.g., new libraries being
4a64f543 11554 loaded. */
ce78b96d 11555 case bp_catchpoint:
c906108c 11556 break;
c5aa993b 11557
c906108c 11558 default:
a3f17187 11559 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 11560 /* fall through */
0fd8e87f
UW
11561 /* Delete overlay event and longjmp master breakpoints; they will be
11562 reset later by breakpoint_re_set. */
1900040c 11563 case bp_overlay_event:
0fd8e87f 11564 case bp_longjmp_master:
aa7d318d 11565 case bp_std_terminate_master:
186c406b 11566 case bp_exception_master:
c906108c
SS
11567 delete_breakpoint (b);
11568 break;
11569
c5aa993b
JM
11570 /* This breakpoint is special, it's set up when the inferior
11571 starts and we really don't want to touch it. */
c906108c
SS
11572 case bp_shlib_event:
11573
c4093a6a
JM
11574 /* Like bp_shlib_event, this breakpoint type is special.
11575 Once it is set up, we do not want to touch it. */
11576 case bp_thread_event:
11577
4a64f543
MS
11578 /* Keep temporary breakpoints, which can be encountered when we
11579 step over a dlopen call and SOLIB_ADD is resetting the
11580 breakpoints. Otherwise these should have been blown away via
11581 the cleanup chain or by breakpoint_init_inferior when we
11582 rerun the executable. */
c906108c
SS
11583 case bp_until:
11584 case bp_finish:
11585 case bp_watchpoint_scope:
11586 case bp_call_dummy:
aa7d318d 11587 case bp_std_terminate:
c906108c 11588 case bp_step_resume:
2c03e5be 11589 case bp_hp_step_resume:
611c83ae
PA
11590 case bp_longjmp:
11591 case bp_longjmp_resume:
186c406b
TT
11592 case bp_exception:
11593 case bp_exception_resume:
4efc6507 11594 case bp_jit_event:
0e30163f 11595 case bp_gnu_ifunc_resolver_return:
c906108c
SS
11596 break;
11597 }
11598
11599 return 0;
11600}
11601
69de3c6a 11602/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11603void
69de3c6a 11604breakpoint_re_set (void)
c906108c 11605{
35df4500 11606 struct breakpoint *b, *b_tmp;
c906108c
SS
11607 enum language save_language;
11608 int save_input_radix;
6c95b8df 11609 struct cleanup *old_chain;
c5aa993b 11610
c906108c
SS
11611 save_language = current_language->la_language;
11612 save_input_radix = input_radix;
6c95b8df
PA
11613 old_chain = save_current_program_space ();
11614
35df4500 11615 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11616 {
4a64f543 11617 /* Format possible error msg. */
fe3f5fa8 11618 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11619 b->number);
11620 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11621 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11622 do_cleanups (cleanups);
c5aa993b 11623 }
c906108c
SS
11624 set_language (save_language);
11625 input_radix = save_input_radix;
e62c965a 11626
0756c555 11627 jit_breakpoint_re_set ();
4efc6507 11628
6c95b8df
PA
11629 do_cleanups (old_chain);
11630
af02033e
PP
11631 create_overlay_event_breakpoint ();
11632 create_longjmp_master_breakpoint ();
11633 create_std_terminate_master_breakpoint ();
186c406b 11634 create_exception_master_breakpoint ();
c906108c
SS
11635}
11636\f
c906108c
SS
11637/* Reset the thread number of this breakpoint:
11638
11639 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11640 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11641void
fba45db2 11642breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11643{
11644 if (b->thread != -1)
11645 {
39f77062
KB
11646 if (in_thread_list (inferior_ptid))
11647 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11648
11649 /* We're being called after following a fork. The new fork is
11650 selected as current, and unless this was a vfork will have a
11651 different program space from the original thread. Reset that
11652 as well. */
11653 b->loc->pspace = current_program_space;
c906108c
SS
11654 }
11655}
11656
03ac34d5
MS
11657/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11658 If from_tty is nonzero, it prints a message to that effect,
11659 which ends with a period (no newline). */
11660
c906108c 11661void
fba45db2 11662set_ignore_count (int bptnum, int count, int from_tty)
c906108c 11663{
52f0bd74 11664 struct breakpoint *b;
c906108c
SS
11665
11666 if (count < 0)
11667 count = 0;
11668
11669 ALL_BREAKPOINTS (b)
11670 if (b->number == bptnum)
c5aa993b 11671 {
d77f58be
SS
11672 if (is_tracepoint (b))
11673 {
11674 if (from_tty && count != 0)
11675 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11676 bptnum);
11677 return;
11678 }
11679
c5aa993b 11680 b->ignore_count = count;
221ea385
KS
11681 if (from_tty)
11682 {
11683 if (count == 0)
3e43a32a
MS
11684 printf_filtered (_("Will stop next time "
11685 "breakpoint %d is reached."),
221ea385
KS
11686 bptnum);
11687 else if (count == 1)
a3f17187 11688 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
11689 bptnum);
11690 else
3e43a32a
MS
11691 printf_filtered (_("Will ignore next %d "
11692 "crossings of breakpoint %d."),
221ea385
KS
11693 count, bptnum);
11694 }
c5aa993b 11695 breakpoints_changed ();
8d3788bd 11696 observer_notify_breakpoint_modified (b);
c5aa993b
JM
11697 return;
11698 }
c906108c 11699
8a3fe4f8 11700 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
11701}
11702
c906108c
SS
11703/* Command to set ignore-count of breakpoint N to COUNT. */
11704
11705static void
fba45db2 11706ignore_command (char *args, int from_tty)
c906108c
SS
11707{
11708 char *p = args;
52f0bd74 11709 int num;
c906108c
SS
11710
11711 if (p == 0)
e2e0b3e5 11712 error_no_arg (_("a breakpoint number"));
c5aa993b 11713
c906108c 11714 num = get_number (&p);
5c44784c 11715 if (num == 0)
8a3fe4f8 11716 error (_("bad breakpoint number: '%s'"), args);
c906108c 11717 if (*p == 0)
8a3fe4f8 11718 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
11719
11720 set_ignore_count (num,
11721 longest_to_int (value_as_long (parse_and_eval (p))),
11722 from_tty);
221ea385
KS
11723 if (from_tty)
11724 printf_filtered ("\n");
c906108c
SS
11725}
11726\f
11727/* Call FUNCTION on each of the breakpoints
11728 whose numbers are given in ARGS. */
11729
11730static void
95a42b64
TT
11731map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
11732 void *),
11733 void *data)
c906108c 11734{
52f0bd74
AC
11735 int num;
11736 struct breakpoint *b, *tmp;
11cf8741 11737 int match;
197f0a60 11738 struct get_number_or_range_state state;
c906108c 11739
197f0a60 11740 if (args == 0)
e2e0b3e5 11741 error_no_arg (_("one or more breakpoint numbers"));
c906108c 11742
197f0a60
TT
11743 init_number_or_range (&state, args);
11744
11745 while (!state.finished)
c906108c 11746 {
197f0a60
TT
11747 char *p = state.string;
11748
11cf8741 11749 match = 0;
c5aa993b 11750
197f0a60 11751 num = get_number_or_range (&state);
5c44784c 11752 if (num == 0)
c5aa993b 11753 {
8a3fe4f8 11754 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
11755 }
11756 else
11757 {
11758 ALL_BREAKPOINTS_SAFE (b, tmp)
11759 if (b->number == num)
11760 {
11cf8741 11761 match = 1;
cdac0397 11762 function (b, data);
11cf8741 11763 break;
5c44784c 11764 }
11cf8741 11765 if (match == 0)
a3f17187 11766 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 11767 }
c906108c
SS
11768 }
11769}
11770
0d381245
VP
11771static struct bp_location *
11772find_location_by_number (char *number)
11773{
11774 char *dot = strchr (number, '.');
11775 char *p1;
11776 int bp_num;
11777 int loc_num;
11778 struct breakpoint *b;
11779 struct bp_location *loc;
11780
11781 *dot = '\0';
11782
11783 p1 = number;
197f0a60 11784 bp_num = get_number (&p1);
0d381245
VP
11785 if (bp_num == 0)
11786 error (_("Bad breakpoint number '%s'"), number);
11787
11788 ALL_BREAKPOINTS (b)
11789 if (b->number == bp_num)
11790 {
11791 break;
11792 }
11793
11794 if (!b || b->number != bp_num)
11795 error (_("Bad breakpoint number '%s'"), number);
11796
11797 p1 = dot+1;
197f0a60 11798 loc_num = get_number (&p1);
0d381245
VP
11799 if (loc_num == 0)
11800 error (_("Bad breakpoint location number '%s'"), number);
11801
11802 --loc_num;
11803 loc = b->loc;
11804 for (;loc_num && loc; --loc_num, loc = loc->next)
11805 ;
11806 if (!loc)
11807 error (_("Bad breakpoint location number '%s'"), dot+1);
11808
11809 return loc;
11810}
11811
11812
1900040c
MS
11813/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11814 If from_tty is nonzero, it prints a message to that effect,
11815 which ends with a period (no newline). */
11816
c906108c 11817void
fba45db2 11818disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
11819{
11820 /* Never disable a watchpoint scope breakpoint; we want to
11821 hit them when we leave scope so we can delete both the
11822 watchpoint and its scope breakpoint at that time. */
11823 if (bpt->type == bp_watchpoint_scope)
11824 return;
11825
c2c6d25f 11826 /* You can't disable permanent breakpoints. */
b5de0fa7 11827 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
11828 return;
11829
b5de0fa7 11830 bpt->enable_state = bp_disabled;
c906108c 11831
d248b706
KY
11832 if (target_supports_enable_disable_tracepoint ()
11833 && current_trace_status ()->running && is_tracepoint (bpt))
11834 {
11835 struct bp_location *location;
11836
11837 for (location = bpt->loc; location; location = location->next)
11838 target_disable_tracepoint (location);
11839 }
11840
b60e7edf 11841 update_global_location_list (0);
c906108c 11842
8d3788bd 11843 observer_notify_breakpoint_modified (bpt);
c906108c
SS
11844}
11845
51be5b68
PA
11846/* A callback for iterate_over_related_breakpoints. */
11847
11848static void
11849do_disable_breakpoint (struct breakpoint *b, void *ignore)
11850{
11851 disable_breakpoint (b);
11852}
11853
95a42b64
TT
11854/* A callback for map_breakpoint_numbers that calls
11855 disable_breakpoint. */
11856
11857static void
11858do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
11859{
51be5b68 11860 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
11861}
11862
c906108c 11863static void
fba45db2 11864disable_command (char *args, int from_tty)
c906108c 11865{
c906108c 11866 if (args == 0)
46c6471b
PA
11867 {
11868 struct breakpoint *bpt;
11869
11870 ALL_BREAKPOINTS (bpt)
11871 if (user_breakpoint_p (bpt))
11872 disable_breakpoint (bpt);
11873 }
0d381245
VP
11874 else if (strchr (args, '.'))
11875 {
11876 struct bp_location *loc = find_location_by_number (args);
11877 if (loc)
d248b706
KY
11878 {
11879 loc->enabled = 0;
11880 if (target_supports_enable_disable_tracepoint ()
11881 && current_trace_status ()->running && loc->owner
11882 && is_tracepoint (loc->owner))
11883 target_disable_tracepoint (loc);
11884 }
b60e7edf 11885 update_global_location_list (0);
0d381245 11886 }
c906108c 11887 else
95a42b64 11888 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
11889}
11890
11891static void
51be5b68 11892enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 11893{
afe38095 11894 int target_resources_ok;
c906108c
SS
11895
11896 if (bpt->type == bp_hardware_breakpoint)
11897 {
11898 int i;
c5aa993b 11899 i = hw_breakpoint_used_count ();
53a5351d 11900 target_resources_ok =
d92524f1 11901 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 11902 i + 1, 0);
c906108c 11903 if (target_resources_ok == 0)
8a3fe4f8 11904 error (_("No hardware breakpoint support in the target."));
c906108c 11905 else if (target_resources_ok < 0)
8a3fe4f8 11906 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
11907 }
11908
cc60f2e3 11909 if (is_watchpoint (bpt))
c906108c 11910 {
d07205c2
JK
11911 /* Initialize it just to avoid a GCC false warning. */
11912 enum enable_state orig_enable_state = 0;
dde02812
ES
11913 struct gdb_exception e;
11914
11915 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 11916 {
1e718ff1
TJB
11917 orig_enable_state = bpt->enable_state;
11918 bpt->enable_state = bp_enabled;
dde02812 11919 update_watchpoint (bpt, 1 /* reparse */);
c906108c 11920 }
dde02812 11921 if (e.reason < 0)
c5aa993b 11922 {
1e718ff1 11923 bpt->enable_state = orig_enable_state;
dde02812
ES
11924 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
11925 bpt->number);
11926 return;
c5aa993b 11927 }
c906108c 11928 }
0101ce28 11929
b4c291bb
KH
11930 if (bpt->enable_state != bp_permanent)
11931 bpt->enable_state = bp_enabled;
d248b706
KY
11932
11933 if (target_supports_enable_disable_tracepoint ()
11934 && current_trace_status ()->running && is_tracepoint (bpt))
11935 {
11936 struct bp_location *location;
11937
11938 for (location = bpt->loc; location; location = location->next)
11939 target_enable_tracepoint (location);
11940 }
11941
b4c291bb 11942 bpt->disposition = disposition;
b60e7edf 11943 update_global_location_list (1);
b4c291bb
KH
11944 breakpoints_changed ();
11945
8d3788bd 11946 observer_notify_breakpoint_modified (bpt);
c906108c
SS
11947}
11948
fe3f5fa8 11949
c906108c 11950void
fba45db2 11951enable_breakpoint (struct breakpoint *bpt)
c906108c 11952{
51be5b68
PA
11953 enable_breakpoint_disp (bpt, bpt->disposition);
11954}
11955
11956static void
11957do_enable_breakpoint (struct breakpoint *bpt, void *arg)
11958{
11959 enable_breakpoint (bpt);
c906108c
SS
11960}
11961
95a42b64
TT
11962/* A callback for map_breakpoint_numbers that calls
11963 enable_breakpoint. */
11964
11965static void
11966do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
11967{
51be5b68 11968 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
11969}
11970
c906108c
SS
11971/* The enable command enables the specified breakpoints (or all defined
11972 breakpoints) so they once again become (or continue to be) effective
1272ad14 11973 in stopping the inferior. */
c906108c 11974
c906108c 11975static void
fba45db2 11976enable_command (char *args, int from_tty)
c906108c 11977{
c906108c 11978 if (args == 0)
46c6471b
PA
11979 {
11980 struct breakpoint *bpt;
11981
11982 ALL_BREAKPOINTS (bpt)
11983 if (user_breakpoint_p (bpt))
11984 enable_breakpoint (bpt);
11985 }
0d381245
VP
11986 else if (strchr (args, '.'))
11987 {
11988 struct bp_location *loc = find_location_by_number (args);
11989 if (loc)
d248b706
KY
11990 {
11991 loc->enabled = 1;
11992 if (target_supports_enable_disable_tracepoint ()
11993 && current_trace_status ()->running && loc->owner
11994 && is_tracepoint (loc->owner))
11995 target_enable_tracepoint (loc);
11996 }
b60e7edf 11997 update_global_location_list (1);
0d381245 11998 }
c906108c 11999 else
95a42b64 12000 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12001}
12002
12003static void
51be5b68
PA
12004do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12005{
12006 enum bpdisp disp = *(enum bpdisp *) arg;
12007
12008 enable_breakpoint_disp (bpt, disp);
12009}
12010
12011static void
12012do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12013{
51be5b68
PA
12014 enum bpdisp disp = disp_disable;
12015
12016 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12017}
12018
c906108c 12019static void
fba45db2 12020enable_once_command (char *args, int from_tty)
c906108c 12021{
51be5b68 12022 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12023}
12024
12025static void
51be5b68 12026do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12027{
51be5b68
PA
12028 enum bpdisp disp = disp_del;
12029
12030 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12031}
12032
c906108c 12033static void
fba45db2 12034enable_delete_command (char *args, int from_tty)
c906108c 12035{
51be5b68 12036 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12037}
12038\f
fa8d40ab
JJ
12039static void
12040set_breakpoint_cmd (char *args, int from_tty)
12041{
12042}
12043
12044static void
12045show_breakpoint_cmd (char *args, int from_tty)
12046{
12047}
12048
1f3b5d1b
PP
12049/* Invalidate last known value of any hardware watchpoint if
12050 the memory which that value represents has been written to by
12051 GDB itself. */
12052
12053static void
12054invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12055 const bfd_byte *data)
12056{
12057 struct breakpoint *bp;
12058
12059 ALL_BREAKPOINTS (bp)
12060 if (bp->enable_state == bp_enabled
12061 && bp->type == bp_hardware_watchpoint
12062 && bp->val_valid && bp->val)
12063 {
12064 struct bp_location *loc;
12065
12066 for (loc = bp->loc; loc != NULL; loc = loc->next)
12067 if (loc->loc_type == bp_loc_hardware_watchpoint
12068 && loc->address + loc->length > addr
12069 && addr + len > loc->address)
12070 {
12071 value_free (bp->val);
12072 bp->val = NULL;
12073 bp->val_valid = 0;
12074 }
12075 }
12076}
12077
c906108c
SS
12078/* Use default_breakpoint_'s, or nothing if they aren't valid. */
12079
12080struct symtabs_and_lines
fba45db2 12081decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12082{
12083 struct symtabs_and_lines sals;
cc59ec59 12084
c906108c 12085 if (string == 0)
8a3fe4f8 12086 error (_("Empty line specification."));
c906108c
SS
12087 if (default_breakpoint_valid)
12088 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
12089 default_breakpoint_symtab,
12090 default_breakpoint_line,
58438ac1 12091 NULL);
c906108c
SS
12092 else
12093 sals = decode_line_1 (&string, funfirstline,
58438ac1 12094 (struct symtab *) NULL, 0, NULL);
c906108c 12095 if (*string)
8a3fe4f8 12096 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12097 return sals;
12098}
8181d85f
DJ
12099
12100/* Create and insert a raw software breakpoint at PC. Return an
12101 identifier, which should be used to remove the breakpoint later.
12102 In general, places which call this should be using something on the
12103 breakpoint chain instead; this function should be eliminated
12104 someday. */
12105
12106void *
6c95b8df
PA
12107deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12108 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12109{
12110 struct bp_target_info *bp_tgt;
12111
6c95b8df 12112 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12113
6c95b8df 12114 bp_tgt->placed_address_space = aspace;
8181d85f 12115 bp_tgt->placed_address = pc;
6c95b8df 12116
a6d9a66e 12117 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12118 {
12119 /* Could not insert the breakpoint. */
12120 xfree (bp_tgt);
12121 return NULL;
12122 }
12123
12124 return bp_tgt;
12125}
12126
4a64f543
MS
12127/* Remove a breakpoint BP inserted by
12128 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12129
12130int
a6d9a66e 12131deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12132{
12133 struct bp_target_info *bp_tgt = bp;
12134 int ret;
12135
a6d9a66e 12136 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12137 xfree (bp_tgt);
12138
12139 return ret;
12140}
12141
4a64f543
MS
12142/* One (or perhaps two) breakpoints used for software single
12143 stepping. */
8181d85f
DJ
12144
12145static void *single_step_breakpoints[2];
a6d9a66e 12146static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12147
12148/* Create and insert a breakpoint for software single step. */
12149
12150void
6c95b8df 12151insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12152 struct address_space *aspace,
12153 CORE_ADDR next_pc)
8181d85f
DJ
12154{
12155 void **bpt_p;
12156
12157 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12158 {
12159 bpt_p = &single_step_breakpoints[0];
12160 single_step_gdbarch[0] = gdbarch;
12161 }
8181d85f
DJ
12162 else
12163 {
12164 gdb_assert (single_step_breakpoints[1] == NULL);
12165 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12166 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12167 }
12168
4a64f543
MS
12169 /* NOTE drow/2006-04-11: A future improvement to this function would
12170 be to only create the breakpoints once, and actually put them on
12171 the breakpoint chain. That would let us use set_raw_breakpoint.
12172 We could adjust the addresses each time they were needed. Doing
12173 this requires corresponding changes elsewhere where single step
12174 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12175
6c95b8df 12176 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12177 if (*bpt_p == NULL)
5af949e3
UW
12178 error (_("Could not insert single-step breakpoint at %s"),
12179 paddress (gdbarch, next_pc));
8181d85f
DJ
12180}
12181
f02253f1
HZ
12182/* Check if the breakpoints used for software single stepping
12183 were inserted or not. */
12184
12185int
12186single_step_breakpoints_inserted (void)
12187{
12188 return (single_step_breakpoints[0] != NULL
12189 || single_step_breakpoints[1] != NULL);
12190}
12191
8181d85f
DJ
12192/* Remove and delete any breakpoints used for software single step. */
12193
12194void
12195remove_single_step_breakpoints (void)
12196{
12197 gdb_assert (single_step_breakpoints[0] != NULL);
12198
12199 /* See insert_single_step_breakpoint for more about this deprecated
12200 call. */
a6d9a66e
UW
12201 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12202 single_step_breakpoints[0]);
12203 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12204 single_step_breakpoints[0] = NULL;
12205
12206 if (single_step_breakpoints[1] != NULL)
12207 {
a6d9a66e
UW
12208 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12209 single_step_breakpoints[1]);
12210 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12211 single_step_breakpoints[1] = NULL;
12212 }
12213}
12214
d03285ec
UW
12215/* Delete software single step breakpoints without removing them from
12216 the inferior. This is intended to be used if the inferior's address
12217 space where they were inserted is already gone, e.g. after exit or
12218 exec. */
12219
12220void
12221cancel_single_step_breakpoints (void)
12222{
12223 int i;
12224
12225 for (i = 0; i < 2; i++)
12226 if (single_step_breakpoints[i])
12227 {
12228 xfree (single_step_breakpoints[i]);
12229 single_step_breakpoints[i] = NULL;
12230 single_step_gdbarch[i] = NULL;
12231 }
12232}
12233
12234/* Detach software single-step breakpoints from INFERIOR_PTID without
12235 removing them. */
12236
12237static void
12238detach_single_step_breakpoints (void)
12239{
12240 int i;
12241
12242 for (i = 0; i < 2; i++)
12243 if (single_step_breakpoints[i])
12244 target_remove_breakpoint (single_step_gdbarch[i],
12245 single_step_breakpoints[i]);
12246}
12247
4a64f543
MS
12248/* Check whether a software single-step breakpoint is inserted at
12249 PC. */
1aafd4da
UW
12250
12251static int
cc59ec59
MS
12252single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12253 CORE_ADDR pc)
1aafd4da
UW
12254{
12255 int i;
12256
12257 for (i = 0; i < 2; i++)
12258 {
12259 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12260 if (bp_tgt
12261 && breakpoint_address_match (bp_tgt->placed_address_space,
12262 bp_tgt->placed_address,
12263 aspace, pc))
1aafd4da
UW
12264 return 1;
12265 }
12266
12267 return 0;
12268}
12269
a96d9b2e
SDJ
12270/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12271 non-zero otherwise. */
12272static int
12273is_syscall_catchpoint_enabled (struct breakpoint *bp)
12274{
12275 if (syscall_catchpoint_p (bp)
12276 && bp->enable_state != bp_disabled
12277 && bp->enable_state != bp_call_disabled)
12278 return 1;
12279 else
12280 return 0;
12281}
12282
12283int
12284catch_syscall_enabled (void)
12285{
12286 struct inferior *inf = current_inferior ();
12287
12288 return inf->total_syscalls_count != 0;
12289}
12290
12291int
12292catching_syscall_number (int syscall_number)
12293{
12294 struct breakpoint *bp;
12295
12296 ALL_BREAKPOINTS (bp)
12297 if (is_syscall_catchpoint_enabled (bp))
12298 {
be5c67c1
PA
12299 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12300
12301 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12302 {
12303 int i, iter;
12304 for (i = 0;
be5c67c1 12305 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12306 i++)
12307 if (syscall_number == iter)
12308 return 1;
12309 }
12310 else
12311 return 1;
12312 }
12313
12314 return 0;
12315}
12316
12317/* Complete syscall names. Used by "catch syscall". */
12318static char **
12319catch_syscall_completer (struct cmd_list_element *cmd,
12320 char *text, char *word)
12321{
12322 const char **list = get_syscall_names ();
c38eea1a
MS
12323 char **retlist
12324 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12325
c38eea1a
MS
12326 xfree (list);
12327 return retlist;
a96d9b2e
SDJ
12328}
12329
1042e4c0
SS
12330/* Tracepoint-specific operations. */
12331
12332/* Set tracepoint count to NUM. */
12333static void
12334set_tracepoint_count (int num)
12335{
12336 tracepoint_count = num;
4fa62494 12337 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12338}
12339
12340void
12341trace_command (char *arg, int from_tty)
12342{
8cdf0e15
VP
12343 if (create_breakpoint (get_current_arch (),
12344 arg,
12345 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12346 0 /* tempflag */,
12347 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12348 0 /* Ignore count */,
12349 pending_break_support,
12350 NULL,
12351 from_tty,
84f4c1fe
PM
12352 1 /* enabled */,
12353 0 /* internal */))
fd9b8c24 12354 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12355}
12356
7a697b8d
SS
12357void
12358ftrace_command (char *arg, int from_tty)
12359{
8cdf0e15
VP
12360 if (create_breakpoint (get_current_arch (),
12361 arg,
12362 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12363 0 /* tempflag */,
12364 bp_fast_tracepoint /* type_wanted */,
12365 0 /* Ignore count */,
12366 pending_break_support,
12367 NULL,
12368 from_tty,
84f4c1fe
PM
12369 1 /* enabled */,
12370 0 /* internal */))
0fb4aa4b
PA
12371 set_tracepoint_count (breakpoint_count);
12372}
12373
12374/* strace command implementation. Creates a static tracepoint. */
12375
12376void
12377strace_command (char *arg, int from_tty)
12378{
12379 if (create_breakpoint (get_current_arch (),
12380 arg,
12381 NULL, 0, 1 /* parse arg */,
12382 0 /* tempflag */,
12383 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12384 0 /* Ignore count */,
12385 pending_break_support,
12386 NULL,
12387 from_tty,
84f4c1fe
PM
12388 1 /* enabled */,
12389 0 /* internal */))
fd9b8c24 12390 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12391}
12392
409873ef
SS
12393/* Set up a fake reader function that gets command lines from a linked
12394 list that was acquired during tracepoint uploading. */
12395
12396static struct uploaded_tp *this_utp;
3149d8c1 12397static int next_cmd;
409873ef
SS
12398
12399static char *
12400read_uploaded_action (void)
12401{
12402 char *rslt;
12403
3149d8c1 12404 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12405
3149d8c1 12406 next_cmd++;
409873ef
SS
12407
12408 return rslt;
12409}
12410
00bf0b85
SS
12411/* Given information about a tracepoint as recorded on a target (which
12412 can be either a live system or a trace file), attempt to create an
12413 equivalent GDB tracepoint. This is not a reliable process, since
12414 the target does not necessarily have all the information used when
12415 the tracepoint was originally defined. */
12416
12417struct breakpoint *
12418create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12419{
409873ef 12420 char *addr_str, small_buf[100];
d5551862 12421 struct breakpoint *tp;
fd9b8c24 12422
409873ef
SS
12423 if (utp->at_string)
12424 addr_str = utp->at_string;
12425 else
12426 {
12427 /* In the absence of a source location, fall back to raw
12428 address. Since there is no way to confirm that the address
12429 means the same thing as when the trace was started, warn the
12430 user. */
3e43a32a
MS
12431 warning (_("Uploaded tracepoint %d has no "
12432 "source location, using raw address"),
409873ef
SS
12433 utp->number);
12434 sprintf (small_buf, "*%s", hex_string (utp->addr));
12435 addr_str = small_buf;
12436 }
12437
12438 /* There's not much we can do with a sequence of bytecodes. */
12439 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12440 warning (_("Uploaded tracepoint %d condition "
12441 "has no source form, ignoring it"),
409873ef 12442 utp->number);
d5551862 12443
8cdf0e15 12444 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12445 addr_str,
12446 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12447 0 /* tempflag */,
0fb4aa4b 12448 utp->type /* type_wanted */,
8cdf0e15
VP
12449 0 /* Ignore count */,
12450 pending_break_support,
12451 NULL,
12452 0 /* from_tty */,
84f4c1fe
PM
12453 utp->enabled /* enabled */,
12454 0 /* internal */))
fd9b8c24
PA
12455 return NULL;
12456
00bf0b85
SS
12457 set_tracepoint_count (breakpoint_count);
12458
409873ef 12459 /* Get the tracepoint we just created. */
fd9b8c24
PA
12460 tp = get_tracepoint (tracepoint_count);
12461 gdb_assert (tp != NULL);
d5551862 12462
00bf0b85
SS
12463 if (utp->pass > 0)
12464 {
409873ef 12465 sprintf (small_buf, "%d %d", utp->pass, tp->number);
00bf0b85 12466
409873ef 12467 trace_pass_command (small_buf, 0);
00bf0b85
SS
12468 }
12469
409873ef
SS
12470 /* If we have uploaded versions of the original commands, set up a
12471 special-purpose "reader" function and call the usual command line
12472 reader, then pass the result to the breakpoint command-setting
12473 function. */
3149d8c1 12474 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12475 {
409873ef 12476 struct command_line *cmd_list;
00bf0b85 12477
409873ef 12478 this_utp = utp;
3149d8c1 12479 next_cmd = 0;
d5551862 12480
409873ef
SS
12481 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12482
12483 breakpoint_set_commands (tp, cmd_list);
00bf0b85 12484 }
3149d8c1
SS
12485 else if (!VEC_empty (char_ptr, utp->actions)
12486 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12487 warning (_("Uploaded tracepoint %d actions "
12488 "have no source form, ignoring them"),
409873ef 12489 utp->number);
00bf0b85
SS
12490
12491 return tp;
12492 }
12493
1042e4c0
SS
12494/* Print information on tracepoint number TPNUM_EXP, or all if
12495 omitted. */
12496
12497static void
e5a67952 12498tracepoints_info (char *args, int from_tty)
1042e4c0 12499{
e5a67952 12500 int num_printed;
1042e4c0 12501
e5a67952 12502 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12503
12504 if (num_printed == 0)
1042e4c0 12505 {
e5a67952 12506 if (args == NULL || *args == '\0')
d77f58be
SS
12507 ui_out_message (uiout, 0, "No tracepoints.\n");
12508 else
e5a67952 12509 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12510 }
ad443146
SS
12511
12512 default_collect_info ();
1042e4c0
SS
12513}
12514
4a64f543 12515/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12516 Not supported by all targets. */
12517static void
12518enable_trace_command (char *args, int from_tty)
12519{
12520 enable_command (args, from_tty);
12521}
12522
4a64f543 12523/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12524 Not supported by all targets. */
12525static void
12526disable_trace_command (char *args, int from_tty)
12527{
12528 disable_command (args, from_tty);
12529}
12530
4a64f543 12531/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12532static void
12533delete_trace_command (char *arg, int from_tty)
12534{
35df4500 12535 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12536
12537 dont_repeat ();
12538
12539 if (arg == 0)
12540 {
12541 int breaks_to_delete = 0;
12542
12543 /* Delete all breakpoints if no argument.
12544 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12545 have to be deleted with an explicit breakpoint number
12546 argument. */
1042e4c0 12547 ALL_TRACEPOINTS (b)
46c6471b 12548 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12549 {
12550 breaks_to_delete = 1;
12551 break;
12552 }
1042e4c0
SS
12553
12554 /* Ask user only if there are some breakpoints to delete. */
12555 if (!from_tty
12556 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12557 {
35df4500 12558 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12559 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12560 delete_breakpoint (b);
1042e4c0
SS
12561 }
12562 }
12563 else
51be5b68 12564 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12565}
12566
197f0a60
TT
12567/* Helper function for trace_pass_command. */
12568
12569static void
12570trace_pass_set_count (struct breakpoint *bp, int count, int from_tty)
12571{
12572 bp->pass_count = count;
12573 observer_notify_tracepoint_modified (bp->number);
12574 if (from_tty)
12575 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12576 bp->number, count);
12577}
12578
1042e4c0
SS
12579/* Set passcount for tracepoint.
12580
12581 First command argument is passcount, second is tracepoint number.
12582 If tracepoint number omitted, apply to most recently defined.
12583 Also accepts special argument "all". */
12584
12585static void
12586trace_pass_command (char *args, int from_tty)
12587{
197f0a60 12588 struct breakpoint *t1;
1042e4c0 12589 unsigned int count;
1042e4c0
SS
12590
12591 if (args == 0 || *args == 0)
3e43a32a
MS
12592 error (_("passcount command requires an "
12593 "argument (count + optional TP num)"));
1042e4c0 12594
4a64f543 12595 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12596
12597 while (*args && isspace ((int) *args))
12598 args++;
12599
12600 if (*args && strncasecmp (args, "all", 3) == 0)
12601 {
12602 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12603 if (*args)
12604 error (_("Junk at end of arguments."));
1042e4c0 12605
197f0a60
TT
12606 ALL_TRACEPOINTS (t1)
12607 {
12608 trace_pass_set_count (t1, count, from_tty);
12609 }
12610 }
12611 else if (*args == '\0')
1042e4c0 12612 {
197f0a60 12613 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12614 if (t1)
197f0a60
TT
12615 trace_pass_set_count (t1, count, from_tty);
12616 }
12617 else
12618 {
12619 struct get_number_or_range_state state;
12620
12621 init_number_or_range (&state, args);
12622 while (!state.finished)
1042e4c0 12623 {
197f0a60
TT
12624 t1 = get_tracepoint_by_number (&args, &state, 1);
12625 if (t1)
12626 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12627 }
12628 }
1042e4c0
SS
12629}
12630
12631struct breakpoint *
12632get_tracepoint (int num)
12633{
12634 struct breakpoint *t;
12635
12636 ALL_TRACEPOINTS (t)
12637 if (t->number == num)
12638 return t;
12639
12640 return NULL;
12641}
12642
d5551862
SS
12643/* Find the tracepoint with the given target-side number (which may be
12644 different from the tracepoint number after disconnecting and
12645 reconnecting). */
12646
12647struct breakpoint *
12648get_tracepoint_by_number_on_target (int num)
12649{
12650 struct breakpoint *t;
12651
12652 ALL_TRACEPOINTS (t)
12653 if (t->number_on_target == num)
12654 return t;
12655
12656 return NULL;
12657}
12658
1042e4c0 12659/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
12660 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12661 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0
SS
12662 recent tracepoint (tracepoint_count) is returned. */
12663struct breakpoint *
197f0a60
TT
12664get_tracepoint_by_number (char **arg,
12665 struct get_number_or_range_state *state,
12666 int optional_p)
1042e4c0
SS
12667{
12668 extern int tracepoint_count;
12669 struct breakpoint *t;
12670 int tpnum;
12671 char *instring = arg == NULL ? NULL : *arg;
12672
197f0a60
TT
12673 if (state)
12674 {
12675 gdb_assert (!state->finished);
12676 tpnum = get_number_or_range (state);
12677 }
12678 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
12679 {
12680 if (optional_p)
12681 tpnum = tracepoint_count;
12682 else
12683 error_no_arg (_("tracepoint number"));
12684 }
12685 else
197f0a60 12686 tpnum = get_number (arg);
1042e4c0
SS
12687
12688 if (tpnum <= 0)
12689 {
12690 if (instring && *instring)
12691 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12692 instring);
12693 else
3e43a32a
MS
12694 printf_filtered (_("Tracepoint argument missing "
12695 "and no previous tracepoint\n"));
1042e4c0
SS
12696 return NULL;
12697 }
12698
12699 ALL_TRACEPOINTS (t)
12700 if (t->number == tpnum)
12701 {
12702 return t;
12703 }
12704
1042e4c0
SS
12705 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
12706 return NULL;
12707}
12708
6149aea9
PA
12709/* Save information on user settable breakpoints (watchpoints, etc) to
12710 a new script file named FILENAME. If FILTER is non-NULL, call it
12711 on each breakpoint and only include the ones for which it returns
12712 non-zero. */
12713
1042e4c0 12714static void
6149aea9
PA
12715save_breakpoints (char *filename, int from_tty,
12716 int (*filter) (const struct breakpoint *))
1042e4c0
SS
12717{
12718 struct breakpoint *tp;
6149aea9 12719 int any = 0;
a7bdde9e 12720 char *pathname;
1042e4c0 12721 struct cleanup *cleanup;
a7bdde9e 12722 struct ui_file *fp;
6149aea9 12723 int extra_trace_bits = 0;
1042e4c0 12724
6149aea9
PA
12725 if (filename == 0 || *filename == 0)
12726 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
12727
12728 /* See if we have anything to save. */
6149aea9 12729 ALL_BREAKPOINTS (tp)
1042e4c0 12730 {
6149aea9 12731 /* Skip internal and momentary breakpoints. */
09d682a4 12732 if (!user_breakpoint_p (tp))
6149aea9
PA
12733 continue;
12734
12735 /* If we have a filter, only save the breakpoints it accepts. */
12736 if (filter && !filter (tp))
12737 continue;
12738
12739 any = 1;
12740
12741 if (is_tracepoint (tp))
12742 {
12743 extra_trace_bits = 1;
12744
12745 /* We can stop searching. */
12746 break;
12747 }
1042e4c0 12748 }
6149aea9
PA
12749
12750 if (!any)
1042e4c0 12751 {
6149aea9 12752 warning (_("Nothing to save."));
1042e4c0
SS
12753 return;
12754 }
12755
6149aea9 12756 pathname = tilde_expand (filename);
1042e4c0 12757 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 12758 fp = gdb_fopen (pathname, "w");
059fb39f 12759 if (!fp)
6149aea9
PA
12760 error (_("Unable to open file '%s' for saving (%s)"),
12761 filename, safe_strerror (errno));
a7bdde9e 12762 make_cleanup_ui_file_delete (fp);
8bf6485c 12763
6149aea9
PA
12764 if (extra_trace_bits)
12765 save_trace_state_variables (fp);
8bf6485c 12766
6149aea9 12767 ALL_BREAKPOINTS (tp)
1042e4c0 12768 {
6149aea9 12769 /* Skip internal and momentary breakpoints. */
09d682a4 12770 if (!user_breakpoint_p (tp))
6149aea9 12771 continue;
8bf6485c 12772
6149aea9
PA
12773 /* If we have a filter, only save the breakpoints it accepts. */
12774 if (filter && !filter (tp))
12775 continue;
12776
26063d49 12777 if (tp->ops != NULL && tp->ops->print_recreate != NULL)
6149aea9 12778 (tp->ops->print_recreate) (tp, fp);
1042e4c0
SS
12779 else
12780 {
6149aea9
PA
12781 if (tp->type == bp_fast_tracepoint)
12782 fprintf_unfiltered (fp, "ftrace");
0fb4aa4b
PA
12783 if (tp->type == bp_static_tracepoint)
12784 fprintf_unfiltered (fp, "strace");
6149aea9
PA
12785 else if (tp->type == bp_tracepoint)
12786 fprintf_unfiltered (fp, "trace");
12787 else if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12788 fprintf_unfiltered (fp, "tbreak");
12789 else if (tp->type == bp_breakpoint)
12790 fprintf_unfiltered (fp, "break");
12791 else if (tp->type == bp_hardware_breakpoint
12792 && tp->disposition == disp_del)
12793 fprintf_unfiltered (fp, "thbreak");
12794 else if (tp->type == bp_hardware_breakpoint)
12795 fprintf_unfiltered (fp, "hbreak");
12796 else if (tp->type == bp_watchpoint)
12797 fprintf_unfiltered (fp, "watch");
12798 else if (tp->type == bp_hardware_watchpoint)
12799 fprintf_unfiltered (fp, "watch");
12800 else if (tp->type == bp_read_watchpoint)
12801 fprintf_unfiltered (fp, "rwatch");
12802 else if (tp->type == bp_access_watchpoint)
12803 fprintf_unfiltered (fp, "awatch");
12804 else
12805 internal_error (__FILE__, __LINE__,
12806 _("unhandled breakpoint type %d"), (int) tp->type);
12807
12808 if (tp->exp_string)
12809 fprintf_unfiltered (fp, " %s", tp->exp_string);
12810 else if (tp->addr_string)
12811 fprintf_unfiltered (fp, " %s", tp->addr_string);
12812 else
12813 {
12814 char tmp[40];
12815
12816 sprintf_vma (tmp, tp->loc->address);
12817 fprintf_unfiltered (fp, " *0x%s", tmp);
12818 }
1042e4c0
SS
12819 }
12820
6149aea9
PA
12821 if (tp->thread != -1)
12822 fprintf_unfiltered (fp, " thread %d", tp->thread);
12823
12824 if (tp->task != 0)
12825 fprintf_unfiltered (fp, " task %d", tp->task);
8bf6485c
SS
12826
12827 fprintf_unfiltered (fp, "\n");
12828
6149aea9
PA
12829 /* Note, we can't rely on tp->number for anything, as we can't
12830 assume the recreated breakpoint numbers will match. Use $bpnum
12831 instead. */
12832
12833 if (tp->cond_string)
12834 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
12835
12836 if (tp->ignore_count)
12837 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
12838
1042e4c0 12839 if (tp->pass_count)
a7bdde9e 12840 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
1042e4c0 12841
a7bdde9e 12842 if (tp->commands)
1042e4c0 12843 {
a7bdde9e
VP
12844 volatile struct gdb_exception ex;
12845
6149aea9 12846 fprintf_unfiltered (fp, " commands\n");
a7bdde9e
VP
12847
12848 ui_out_redirect (uiout, fp);
14dba4b4 12849 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 12850 {
9add0f1b 12851 print_command_lines (uiout, tp->commands->commands, 2);
a7bdde9e
VP
12852 }
12853 ui_out_redirect (uiout, NULL);
1042e4c0 12854
a7bdde9e
VP
12855 if (ex.reason < 0)
12856 throw_exception (ex);
1042e4c0 12857
a7bdde9e 12858 fprintf_unfiltered (fp, " end\n");
1042e4c0 12859 }
6149aea9
PA
12860
12861 if (tp->enable_state == bp_disabled)
12862 fprintf_unfiltered (fp, "disable\n");
12863
12864 /* If this is a multi-location breakpoint, check if the locations
12865 should be individually disabled. Watchpoint locations are
12866 special, and not user visible. */
12867 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
12868 {
12869 struct bp_location *loc;
12870 int n = 1;
12871
12872 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
12873 if (!loc->enabled)
12874 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
12875 }
1042e4c0 12876 }
8bf6485c 12877
6149aea9 12878 if (extra_trace_bits && *default_collect)
8bf6485c
SS
12879 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
12880
1042e4c0
SS
12881 do_cleanups (cleanup);
12882 if (from_tty)
6149aea9
PA
12883 printf_filtered (_("Saved to file '%s'.\n"), filename);
12884}
12885
12886/* The `save breakpoints' command. */
12887
12888static void
12889save_breakpoints_command (char *args, int from_tty)
12890{
12891 save_breakpoints (args, from_tty, NULL);
12892}
12893
12894/* The `save tracepoints' command. */
12895
12896static void
12897save_tracepoints_command (char *args, int from_tty)
12898{
12899 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
12900}
12901
12902/* Create a vector of all tracepoints. */
12903
12904VEC(breakpoint_p) *
eeae04df 12905all_tracepoints (void)
1042e4c0
SS
12906{
12907 VEC(breakpoint_p) *tp_vec = 0;
12908 struct breakpoint *tp;
12909
12910 ALL_TRACEPOINTS (tp)
12911 {
12912 VEC_safe_push (breakpoint_p, tp_vec, tp);
12913 }
12914
12915 return tp_vec;
12916}
12917
c906108c 12918\f
4a64f543
MS
12919/* This help string is used for the break, hbreak, tbreak and thbreak
12920 commands. It is defined as a macro to prevent duplication.
12921 COMMAND should be a string constant containing the name of the
12922 command. */
31e2b00f
AS
12923#define BREAK_ARGS_HELP(command) \
12924command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
12925LOCATION may be a line number, function name, or \"*\" and an address.\n\
12926If a line number is specified, break at start of code for that line.\n\
12927If a function is specified, break at start of code for that function.\n\
12928If an address is specified, break at that exact address.\n\
dc10affe
PA
12929With no LOCATION, uses current execution address of the selected\n\
12930stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
12931\n\
12932THREADNUM is the number from \"info threads\".\n\
12933CONDITION is a boolean expression.\n\
12934\n\
d41c0fc8
PA
12935Multiple breakpoints at one place are permitted, and useful if their\n\
12936conditions are different.\n\
31e2b00f
AS
12937\n\
12938Do \"help breakpoints\" for info on other commands dealing with breakpoints."
12939
44feb3ce
TT
12940/* List of subcommands for "catch". */
12941static struct cmd_list_element *catch_cmdlist;
12942
12943/* List of subcommands for "tcatch". */
12944static struct cmd_list_element *tcatch_cmdlist;
12945
9ac4176b 12946void
44feb3ce
TT
12947add_catch_command (char *name, char *docstring,
12948 void (*sfunc) (char *args, int from_tty,
12949 struct cmd_list_element *command),
a96d9b2e
SDJ
12950 char **(*completer) (struct cmd_list_element *cmd,
12951 char *text, char *word),
44feb3ce
TT
12952 void *user_data_catch,
12953 void *user_data_tcatch)
12954{
12955 struct cmd_list_element *command;
12956
12957 command = add_cmd (name, class_breakpoint, NULL, docstring,
12958 &catch_cmdlist);
12959 set_cmd_sfunc (command, sfunc);
12960 set_cmd_context (command, user_data_catch);
a96d9b2e 12961 set_cmd_completer (command, completer);
44feb3ce
TT
12962
12963 command = add_cmd (name, class_breakpoint, NULL, docstring,
12964 &tcatch_cmdlist);
12965 set_cmd_sfunc (command, sfunc);
12966 set_cmd_context (command, user_data_tcatch);
a96d9b2e 12967 set_cmd_completer (command, completer);
44feb3ce
TT
12968}
12969
6c95b8df 12970static void
a79b8f6e 12971clear_syscall_counts (struct inferior *inf)
6c95b8df 12972{
6c95b8df
PA
12973 inf->total_syscalls_count = 0;
12974 inf->any_syscall_count = 0;
12975 VEC_free (int, inf->syscalls_counts);
12976}
12977
6149aea9
PA
12978static void
12979save_command (char *arg, int from_tty)
12980{
3e43a32a
MS
12981 printf_unfiltered (_("\"save\" must be followed by "
12982 "the name of a save subcommand.\n"));
6149aea9
PA
12983 help_list (save_cmdlist, "save ", -1, gdb_stdout);
12984}
12985
84f4c1fe
PM
12986struct breakpoint *
12987iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
12988 void *data)
12989{
35df4500 12990 struct breakpoint *b, *b_tmp;
84f4c1fe 12991
35df4500 12992 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
12993 {
12994 if ((*callback) (b, data))
12995 return b;
12996 }
12997
12998 return NULL;
12999}
13000
c906108c 13001void
fba45db2 13002_initialize_breakpoint (void)
c906108c
SS
13003{
13004 struct cmd_list_element *c;
13005
84acb35a 13006 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13007 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13008 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13009
17450429
PP
13010 breakpoint_objfile_key = register_objfile_data ();
13011
c906108c
SS
13012 breakpoint_chain = 0;
13013 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13014 before a breakpoint is set. */
13015 breakpoint_count = 0;
13016
1042e4c0
SS
13017 tracepoint_count = 0;
13018
1bedd215
AC
13019 add_com ("ignore", class_breakpoint, ignore_command, _("\
13020Set ignore-count of breakpoint number N to COUNT.\n\
13021Usage is `ignore N COUNT'."));
c906108c 13022 if (xdb_commands)
c5aa993b 13023 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13024
1bedd215
AC
13025 add_com ("commands", class_breakpoint, commands_command, _("\
13026Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13027Give breakpoint number as argument after \"commands\".\n\
13028With no argument, the targeted breakpoint is the last one set.\n\
13029The commands themselves follow starting on the next line.\n\
13030Type a line containing \"end\" to indicate the end of them.\n\
13031Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13032then no output is printed when it is hit, except what the commands print."));
c906108c 13033
1bedd215
AC
13034 add_com ("condition", class_breakpoint, condition_command, _("\
13035Specify breakpoint number N to break only if COND is true.\n\
c906108c 13036Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13037expression to be evaluated whenever breakpoint N is reached."));
c906108c 13038
1bedd215 13039 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13040Set a temporary breakpoint.\n\
c906108c
SS
13041Like \"break\" except the breakpoint is only temporary,\n\
13042so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13043by using \"enable delete\" on the breakpoint number.\n\
13044\n"
13045BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13046 set_cmd_completer (c, location_completer);
c94fdfd0 13047
1bedd215 13048 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13049Set a hardware assisted breakpoint.\n\
c906108c 13050Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13051some target hardware may not have this support.\n\
13052\n"
13053BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13054 set_cmd_completer (c, location_completer);
c906108c 13055
1bedd215 13056 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13057Set a temporary hardware assisted breakpoint.\n\
c906108c 13058Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13059so it will be deleted when hit.\n\
13060\n"
13061BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13062 set_cmd_completer (c, location_completer);
c906108c 13063
1bedd215
AC
13064 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13065Enable some breakpoints.\n\
c906108c
SS
13066Give breakpoint numbers (separated by spaces) as arguments.\n\
13067With no subcommand, breakpoints are enabled until you command otherwise.\n\
13068This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13069With a subcommand you can enable temporarily."),
c906108c
SS
13070 &enablelist, "enable ", 1, &cmdlist);
13071 if (xdb_commands)
1bedd215
AC
13072 add_com ("ab", class_breakpoint, enable_command, _("\
13073Enable some breakpoints.\n\
c906108c
SS
13074Give breakpoint numbers (separated by spaces) as arguments.\n\
13075With no subcommand, breakpoints are enabled until you command otherwise.\n\
13076This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13077With a subcommand you can enable temporarily."));
c906108c
SS
13078
13079 add_com_alias ("en", "enable", class_breakpoint, 1);
13080
84951ab5 13081 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13082Enable some breakpoints.\n\
c906108c
SS
13083Give breakpoint numbers (separated by spaces) as arguments.\n\
13084This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13085May be abbreviated to simply \"enable\".\n"),
c5aa993b 13086 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13087
1a966eab
AC
13088 add_cmd ("once", no_class, enable_once_command, _("\
13089Enable breakpoints for one hit. Give breakpoint numbers.\n\
13090If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13091 &enablebreaklist);
13092
1a966eab
AC
13093 add_cmd ("delete", no_class, enable_delete_command, _("\
13094Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13095If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13096 &enablebreaklist);
13097
1a966eab
AC
13098 add_cmd ("delete", no_class, enable_delete_command, _("\
13099Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13100If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13101 &enablelist);
13102
1a966eab
AC
13103 add_cmd ("once", no_class, enable_once_command, _("\
13104Enable breakpoints for one hit. Give breakpoint numbers.\n\
13105If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13106 &enablelist);
13107
1bedd215
AC
13108 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13109Disable some breakpoints.\n\
c906108c
SS
13110Arguments are breakpoint numbers with spaces in between.\n\
13111To disable all breakpoints, give no argument.\n\
64b9b334 13112A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13113 &disablelist, "disable ", 1, &cmdlist);
13114 add_com_alias ("dis", "disable", class_breakpoint, 1);
13115 add_com_alias ("disa", "disable", class_breakpoint, 1);
13116 if (xdb_commands)
1bedd215
AC
13117 add_com ("sb", class_breakpoint, disable_command, _("\
13118Disable some breakpoints.\n\
c906108c
SS
13119Arguments are breakpoint numbers with spaces in between.\n\
13120To disable all breakpoints, give no argument.\n\
64b9b334 13121A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13122
1a966eab
AC
13123 add_cmd ("breakpoints", class_alias, disable_command, _("\
13124Disable some breakpoints.\n\
c906108c
SS
13125Arguments are breakpoint numbers with spaces in between.\n\
13126To disable all breakpoints, give no argument.\n\
64b9b334 13127A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13128This command may be abbreviated \"disable\"."),
c906108c
SS
13129 &disablelist);
13130
1bedd215
AC
13131 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13132Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13133Arguments are breakpoint numbers with spaces in between.\n\
13134To delete all breakpoints, give no argument.\n\
13135\n\
13136Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13137The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13138 &deletelist, "delete ", 1, &cmdlist);
13139 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13140 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13141 if (xdb_commands)
1bedd215
AC
13142 add_com ("db", class_breakpoint, delete_command, _("\
13143Delete some breakpoints.\n\
c906108c 13144Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13145To delete all breakpoints, give no argument.\n"));
c906108c 13146
1a966eab
AC
13147 add_cmd ("breakpoints", class_alias, delete_command, _("\
13148Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13149Arguments are breakpoint numbers with spaces in between.\n\
13150To delete all breakpoints, give no argument.\n\
1a966eab 13151This command may be abbreviated \"delete\"."),
c906108c
SS
13152 &deletelist);
13153
1bedd215
AC
13154 add_com ("clear", class_breakpoint, clear_command, _("\
13155Clear breakpoint at specified line or function.\n\
c906108c
SS
13156Argument may be line number, function name, or \"*\" and an address.\n\
13157If line number is specified, all breakpoints in that line are cleared.\n\
13158If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13159If an address is specified, breakpoints at that address are cleared.\n\
13160\n\
13161With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13162is executing in.\n\
13163\n\
1bedd215 13164See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13165 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13166
1bedd215 13167 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13168Set breakpoint at specified line or function.\n"
13169BREAK_ARGS_HELP ("break")));
5ba2abeb 13170 set_cmd_completer (c, location_completer);
c94fdfd0 13171
c906108c
SS
13172 add_com_alias ("b", "break", class_run, 1);
13173 add_com_alias ("br", "break", class_run, 1);
13174 add_com_alias ("bre", "break", class_run, 1);
13175 add_com_alias ("brea", "break", class_run, 1);
13176
7681d515
PM
13177 if (xdb_commands)
13178 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13179
13180 if (dbx_commands)
13181 {
1bedd215
AC
13182 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13183Break in function/address or break at a line in the current file."),
c5aa993b
JM
13184 &stoplist, "stop ", 1, &cmdlist);
13185 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13186 _("Break in function or address."), &stoplist);
c5aa993b 13187 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13188 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13189 add_com ("status", class_info, breakpoints_info, _("\
13190Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13191The \"Type\" column indicates one of:\n\
13192\tbreakpoint - normal breakpoint\n\
13193\twatchpoint - watchpoint\n\
13194The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13195the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13196breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13197address and file/line number respectively.\n\
13198\n\
13199Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13200are set to the address of the last breakpoint listed unless the command\n\
13201is prefixed with \"server \".\n\n\
c906108c 13202Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13203breakpoint set."));
c906108c
SS
13204 }
13205
1bedd215 13206 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13207Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13208The \"Type\" column indicates one of:\n\
13209\tbreakpoint - normal breakpoint\n\
13210\twatchpoint - watchpoint\n\
13211The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13212the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13213breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13214address and file/line number respectively.\n\
13215\n\
13216Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13217are set to the address of the last breakpoint listed unless the command\n\
13218is prefixed with \"server \".\n\n\
c906108c 13219Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13220breakpoint set."));
c906108c 13221
6b04bdb7
MS
13222 add_info_alias ("b", "breakpoints", 1);
13223
c906108c 13224 if (xdb_commands)
1bedd215
AC
13225 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13226Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13227The \"Type\" column indicates one of:\n\
13228\tbreakpoint - normal breakpoint\n\
13229\twatchpoint - watchpoint\n\
13230The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13231the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13232breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13233address and file/line number respectively.\n\
13234\n\
13235Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13236are set to the address of the last breakpoint listed unless the command\n\
13237is prefixed with \"server \".\n\n\
c906108c 13238Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13239breakpoint set."));
c906108c 13240
1a966eab
AC
13241 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13242Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13243The \"Type\" column indicates one of:\n\
13244\tbreakpoint - normal breakpoint\n\
13245\twatchpoint - watchpoint\n\
13246\tlongjmp - internal breakpoint used to step through longjmp()\n\
13247\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13248\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13249\tfinish - internal breakpoint used by the \"finish\" command\n\
13250The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13251the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13252breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13253address and file/line number respectively.\n\
13254\n\
13255Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13256are set to the address of the last breakpoint listed unless the command\n\
13257is prefixed with \"server \".\n\n\
c906108c 13258Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13259breakpoint set."),
c906108c
SS
13260 &maintenanceinfolist);
13261
44feb3ce
TT
13262 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13263Set catchpoints to catch events."),
13264 &catch_cmdlist, "catch ",
13265 0/*allow-unknown*/, &cmdlist);
13266
13267 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13268Set temporary catchpoints to catch events."),
13269 &tcatch_cmdlist, "tcatch ",
13270 0/*allow-unknown*/, &cmdlist);
13271
13272 /* Add catch and tcatch sub-commands. */
13273 add_catch_command ("catch", _("\
13274Catch an exception, when caught.\n\
13275With an argument, catch only exceptions with the given name."),
13276 catch_catch_command,
a96d9b2e 13277 NULL,
44feb3ce
TT
13278 CATCH_PERMANENT,
13279 CATCH_TEMPORARY);
13280 add_catch_command ("throw", _("\
13281Catch an exception, when thrown.\n\
13282With an argument, catch only exceptions with the given name."),
13283 catch_throw_command,
a96d9b2e 13284 NULL,
44feb3ce
TT
13285 CATCH_PERMANENT,
13286 CATCH_TEMPORARY);
13287 add_catch_command ("fork", _("Catch calls to fork."),
13288 catch_fork_command_1,
a96d9b2e 13289 NULL,
44feb3ce
TT
13290 (void *) (uintptr_t) catch_fork_permanent,
13291 (void *) (uintptr_t) catch_fork_temporary);
13292 add_catch_command ("vfork", _("Catch calls to vfork."),
13293 catch_fork_command_1,
a96d9b2e 13294 NULL,
44feb3ce
TT
13295 (void *) (uintptr_t) catch_vfork_permanent,
13296 (void *) (uintptr_t) catch_vfork_temporary);
13297 add_catch_command ("exec", _("Catch calls to exec."),
13298 catch_exec_command_1,
a96d9b2e
SDJ
13299 NULL,
13300 CATCH_PERMANENT,
13301 CATCH_TEMPORARY);
13302 add_catch_command ("syscall", _("\
13303Catch system calls by their names and/or numbers.\n\
13304Arguments say which system calls to catch. If no arguments\n\
13305are given, every system call will be caught.\n\
13306Arguments, if given, should be one or more system call names\n\
13307(if your system supports that), or system call numbers."),
13308 catch_syscall_command_1,
13309 catch_syscall_completer,
44feb3ce
TT
13310 CATCH_PERMANENT,
13311 CATCH_TEMPORARY);
c5aa993b 13312
1bedd215
AC
13313 c = add_com ("watch", class_breakpoint, watch_command, _("\
13314Set a watchpoint for an expression.\n\
06a64a0b 13315Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13316A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13317an expression changes.\n\
13318If -l or -location is given, this evaluates EXPRESSION and watches\n\
13319the memory to which it refers."));
65d12d83 13320 set_cmd_completer (c, expression_completer);
c906108c 13321
1bedd215
AC
13322 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13323Set a read watchpoint for an expression.\n\
06a64a0b 13324Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13325A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13326an expression is read.\n\
13327If -l or -location is given, this evaluates EXPRESSION and watches\n\
13328the memory to which it refers."));
65d12d83 13329 set_cmd_completer (c, expression_completer);
c906108c 13330
1bedd215
AC
13331 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13332Set a watchpoint for an expression.\n\
06a64a0b 13333Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13334A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13335an expression is either read or written.\n\
13336If -l or -location is given, this evaluates EXPRESSION and watches\n\
13337the memory to which it refers."));
65d12d83 13338 set_cmd_completer (c, expression_completer);
c906108c 13339
d77f58be 13340 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13341Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13342
920d2a44
AC
13343 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13344 respond to changes - contrary to the description. */
85c07804
AC
13345 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13346 &can_use_hw_watchpoints, _("\
13347Set debugger's willingness to use watchpoint hardware."), _("\
13348Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13349If zero, gdb will not use hardware for new watchpoints, even if\n\
13350such is available. (However, any hardware watchpoints that were\n\
13351created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13352hardware.)"),
13353 NULL,
920d2a44 13354 show_can_use_hw_watchpoints,
85c07804 13355 &setlist, &showlist);
c906108c
SS
13356
13357 can_use_hw_watchpoints = 1;
fa8d40ab 13358
1042e4c0
SS
13359 /* Tracepoint manipulation commands. */
13360
13361 c = add_com ("trace", class_breakpoint, trace_command, _("\
13362Set a tracepoint at specified line or function.\n\
13363\n"
13364BREAK_ARGS_HELP ("trace") "\n\
13365Do \"help tracepoints\" for info on other tracepoint commands."));
13366 set_cmd_completer (c, location_completer);
13367
13368 add_com_alias ("tp", "trace", class_alias, 0);
13369 add_com_alias ("tr", "trace", class_alias, 1);
13370 add_com_alias ("tra", "trace", class_alias, 1);
13371 add_com_alias ("trac", "trace", class_alias, 1);
13372
7a697b8d
SS
13373 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13374Set a fast tracepoint at specified line or function.\n\
13375\n"
13376BREAK_ARGS_HELP ("ftrace") "\n\
13377Do \"help tracepoints\" for info on other tracepoint commands."));
13378 set_cmd_completer (c, location_completer);
13379
0fb4aa4b
PA
13380 c = add_com ("strace", class_breakpoint, strace_command, _("\
13381Set a static tracepoint at specified line, function or marker.\n\
13382\n\
13383strace [LOCATION] [if CONDITION]\n\
13384LOCATION may be a line number, function name, \"*\" and an address,\n\
13385or -m MARKER_ID.\n\
13386If a line number is specified, probe the marker at start of code\n\
13387for that line. If a function is specified, probe the marker at start\n\
13388of code for that function. If an address is specified, probe the marker\n\
13389at that exact address. If a marker id is specified, probe the marker\n\
13390with that name. With no LOCATION, uses current execution address of\n\
13391the selected stack frame.\n\
13392Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13393This collects arbitrary user data passed in the probe point call to the\n\
13394tracing library. You can inspect it when analyzing the trace buffer,\n\
13395by printing the $_sdata variable like any other convenience variable.\n\
13396\n\
13397CONDITION is a boolean expression.\n\
13398\n\
d41c0fc8
PA
13399Multiple tracepoints at one place are permitted, and useful if their\n\
13400conditions are different.\n\
0fb4aa4b
PA
13401\n\
13402Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13403Do \"help tracepoints\" for info on other tracepoint commands."));
13404 set_cmd_completer (c, location_completer);
13405
1042e4c0 13406 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13407Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13408Convenience variable \"$tpnum\" contains the number of the\n\
13409last tracepoint set."));
13410
13411 add_info_alias ("tp", "tracepoints", 1);
13412
13413 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13414Delete specified tracepoints.\n\
13415Arguments are tracepoint numbers, separated by spaces.\n\
13416No argument means delete all tracepoints."),
13417 &deletelist);
13418
13419 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13420Disable specified tracepoints.\n\
13421Arguments are tracepoint numbers, separated by spaces.\n\
13422No argument means disable all tracepoints."),
13423 &disablelist);
13424 deprecate_cmd (c, "disable");
13425
13426 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13427Enable specified tracepoints.\n\
13428Arguments are tracepoint numbers, separated by spaces.\n\
13429No argument means enable all tracepoints."),
13430 &enablelist);
13431 deprecate_cmd (c, "enable");
13432
13433 add_com ("passcount", class_trace, trace_pass_command, _("\
13434Set the passcount for a tracepoint.\n\
13435The trace will end when the tracepoint has been passed 'count' times.\n\
13436Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13437if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13438
6149aea9
PA
13439 add_prefix_cmd ("save", class_breakpoint, save_command,
13440 _("Save breakpoint definitions as a script."),
13441 &save_cmdlist, "save ",
13442 0/*allow-unknown*/, &cmdlist);
13443
13444 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13445Save current breakpoint definitions as a script.\n\
cce7e648 13446This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13447catchpoints, tracepoints). Use the 'source' command in another debug\n\
13448session to restore them."),
13449 &save_cmdlist);
13450 set_cmd_completer (c, filename_completer);
13451
13452 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13453Save current tracepoint definitions as a script.\n\
6149aea9
PA
13454Use the 'source' command in another debug session to restore them."),
13455 &save_cmdlist);
1042e4c0
SS
13456 set_cmd_completer (c, filename_completer);
13457
6149aea9
PA
13458 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13459 deprecate_cmd (c, "save tracepoints");
13460
1bedd215 13461 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13462Breakpoint specific settings\n\
13463Configure various breakpoint-specific variables such as\n\
1bedd215 13464pending breakpoint behavior"),
fa8d40ab
JJ
13465 &breakpoint_set_cmdlist, "set breakpoint ",
13466 0/*allow-unknown*/, &setlist);
1bedd215 13467 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13468Breakpoint specific settings\n\
13469Configure various breakpoint-specific variables such as\n\
1bedd215 13470pending breakpoint behavior"),
fa8d40ab
JJ
13471 &breakpoint_show_cmdlist, "show breakpoint ",
13472 0/*allow-unknown*/, &showlist);
13473
7915a72c
AC
13474 add_setshow_auto_boolean_cmd ("pending", no_class,
13475 &pending_break_support, _("\
13476Set debugger's behavior regarding pending breakpoints."), _("\
13477Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13478If on, an unrecognized breakpoint location will cause gdb to create a\n\
13479pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13480an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13481user-query to see if a pending breakpoint should be created."),
2c5b56ce 13482 NULL,
920d2a44 13483 show_pending_break_support,
6e1d7d6c
AC
13484 &breakpoint_set_cmdlist,
13485 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13486
13487 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13488
13489 add_setshow_boolean_cmd ("auto-hw", no_class,
13490 &automatic_hardware_breakpoints, _("\
13491Set automatic usage of hardware breakpoints."), _("\
13492Show automatic usage of hardware breakpoints."), _("\
13493If set, the debugger will automatically use hardware breakpoints for\n\
13494breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13495a warning will be emitted for such breakpoints."),
13496 NULL,
13497 show_automatic_hardware_breakpoints,
13498 &breakpoint_set_cmdlist,
13499 &breakpoint_show_cmdlist);
74960c60 13500
33e5cbd6
PA
13501 add_setshow_enum_cmd ("always-inserted", class_support,
13502 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
13503Set mode for inserting breakpoints."), _("\
13504Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
13505When this mode is off, breakpoints are inserted in inferior when it is\n\
13506resumed, and removed when execution stops. When this mode is on,\n\
13507breakpoints are inserted immediately and removed only when the user\n\
13508deletes the breakpoint. When this mode is auto (which is the default),\n\
13509the behaviour depends on the non-stop setting (see help set non-stop).\n\
13510In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13511behaves as if always-inserted mode is on; if gdb is controlling the\n\
13512inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
13513 NULL,
13514 &show_always_inserted_mode,
13515 &breakpoint_set_cmdlist,
13516 &breakpoint_show_cmdlist);
f1310107
TJB
13517
13518 add_com ("break-range", class_breakpoint, break_range_command, _("\
13519Set a breakpoint for an address range.\n\
13520break-range START-LOCATION, END-LOCATION\n\
13521where START-LOCATION and END-LOCATION can be one of the following:\n\
13522 LINENUM, for that line in the current file,\n\
13523 FILE:LINENUM, for that line in that file,\n\
13524 +OFFSET, for that number of lines after the current line\n\
13525 or the start of the range\n\
13526 FUNCTION, for the first line in that function,\n\
13527 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13528 *ADDRESS, for the instruction at that address.\n\
13529\n\
13530The breakpoint will stop execution of the inferior whenever it executes\n\
13531an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13532range (including START-LOCATION and END-LOCATION)."));
13533
765dc015 13534 automatic_hardware_breakpoints = 1;
f3b1572e
PA
13535
13536 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 13537}
This page took 2.069719 seconds and 4 git commands to generate.