* defs.h (is_cplus_marker, set_demangling_style): Moved to ...
[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"
50f182aa 41#include "gdb-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"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.h"
c906108c 70
1042e4c0
SS
71/* readline include files */
72#include "readline/readline.h"
73#include "readline/history.h"
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
7371cf6d 79#include "python/python.h"
104c1213 80
4a64f543 81/* Prototypes for local functions. */
c906108c 82
a14ed312 83static void enable_delete_command (char *, int);
c906108c 84
a14ed312 85static void enable_once_command (char *, int);
c906108c 86
a14ed312 87static void disable_command (char *, int);
c906108c 88
a14ed312 89static void enable_command (char *, int);
c906108c 90
95a42b64
TT
91static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
92 void *),
93 void *);
c906108c 94
a14ed312 95static void ignore_command (char *, int);
c906108c 96
4efb68b1 97static int breakpoint_re_set_one (void *);
c906108c 98
348d480f
PA
99static void breakpoint_re_set_default (struct breakpoint *);
100
a14ed312 101static void clear_command (char *, int);
c906108c 102
a14ed312 103static void catch_command (char *, int);
c906108c 104
a9634178 105static int can_use_hardware_watchpoint (struct value *);
c906108c 106
98deb0da 107static void break_command_1 (char *, int, int);
c906108c 108
a14ed312 109static void mention (struct breakpoint *);
c906108c 110
348d480f
PA
111static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
112 enum bptype,
c0a91b2b 113 const struct breakpoint_ops *);
4a64f543
MS
114/* This function is used in gdbtk sources and thus can not be made
115 static. */
63c252f8 116struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 117 struct symtab_and_line,
c0a91b2b
TT
118 enum bptype,
119 const struct breakpoint_ops *);
c906108c 120
06edf0c0
PA
121static struct breakpoint *
122 momentary_breakpoint_from_master (struct breakpoint *orig,
123 enum bptype type,
c0a91b2b 124 const struct breakpoint_ops *ops);
06edf0c0 125
76897487
KB
126static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
127
a6d9a66e
UW
128static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
129 CORE_ADDR bpaddr,
88f7da05 130 enum bptype bptype);
76897487 131
6c95b8df
PA
132static void describe_other_breakpoints (struct gdbarch *,
133 struct program_space *, CORE_ADDR,
5af949e3 134 struct obj_section *, int);
c906108c 135
6c95b8df
PA
136static int breakpoint_address_match (struct address_space *aspace1,
137 CORE_ADDR addr1,
138 struct address_space *aspace2,
139 CORE_ADDR addr2);
140
85d721b8
PA
141static int watchpoint_locations_match (struct bp_location *loc1,
142 struct bp_location *loc2);
143
f1310107
TJB
144static int breakpoint_location_address_match (struct bp_location *bl,
145 struct address_space *aspace,
146 CORE_ADDR addr);
147
a14ed312 148static void breakpoints_info (char *, int);
c906108c 149
d77f58be
SS
150static void watchpoints_info (char *, int);
151
e5a67952
MS
152static int breakpoint_1 (char *, int,
153 int (*) (const struct breakpoint *));
c906108c 154
4efb68b1 155static int breakpoint_cond_eval (void *);
c906108c 156
4efb68b1 157static void cleanup_executing_breakpoints (void *);
c906108c 158
a14ed312 159static void commands_command (char *, int);
c906108c 160
a14ed312 161static void condition_command (char *, int);
c906108c 162
c5aa993b
JM
163typedef enum
164 {
165 mark_inserted,
166 mark_uninserted
167 }
168insertion_state_t;
c906108c 169
0bde7532 170static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 171static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 172
e514a9d6 173static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 174
4efb68b1 175static int watchpoint_check (void *);
c906108c 176
a14ed312 177static void maintenance_info_breakpoints (char *, int);
c906108c 178
a14ed312 179static int hw_breakpoint_used_count (void);
c906108c 180
a1398e0c
PA
181static int hw_watchpoint_use_count (struct breakpoint *);
182
183static int hw_watchpoint_used_count_others (struct breakpoint *except,
184 enum bptype type,
185 int *other_type_used);
c906108c 186
a14ed312 187static void hbreak_command (char *, int);
c906108c 188
a14ed312 189static void thbreak_command (char *, int);
c906108c 190
51be5b68 191static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp);
c906108c 192
a14ed312 193static void stop_command (char *arg, int from_tty);
7a292a7a 194
a14ed312 195static void stopin_command (char *arg, int from_tty);
7a292a7a 196
a14ed312 197static void stopat_command (char *arg, int from_tty);
7a292a7a 198
a14ed312 199static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 200
d85310f7
MS
201static void catch_exception_command_1 (enum exception_event_kind ex_event,
202 char *arg, int tempflag, int from_tty);
7a292a7a 203
a14ed312 204static void tcatch_command (char *arg, int from_tty);
7a292a7a 205
d03285ec
UW
206static void detach_single_step_breakpoints (void);
207
6c95b8df
PA
208static int single_step_breakpoint_inserted_here_p (struct address_space *,
209 CORE_ADDR pc);
1aafd4da 210
fe3f5fa8 211static void free_bp_location (struct bp_location *loc);
f431efe5
PA
212static void incref_bp_location (struct bp_location *loc);
213static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 214
39d61571 215static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 216
b60e7edf 217static void update_global_location_list (int);
a5606eee 218
b60e7edf 219static void update_global_location_list_nothrow (int);
74960c60 220
d77f58be 221static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
222
223static void insert_breakpoint_locations (void);
a5606eee 224
a96d9b2e
SDJ
225static int syscall_catchpoint_p (struct breakpoint *b);
226
1042e4c0
SS
227static void tracepoints_info (char *, int);
228
229static void delete_trace_command (char *, int);
230
231static void enable_trace_command (char *, int);
232
233static void disable_trace_command (char *, int);
234
235static void trace_pass_command (char *, int);
236
9c06b0b4
TJB
237static int is_masked_watchpoint (const struct breakpoint *b);
238
0fb4aa4b
PA
239/* Assuming we're creating a static tracepoint, does S look like a
240 static tracepoint marker spec ("-m MARKER_ID")? */
241#define is_marker_spec(s) \
f5a8e22b 242 (s != NULL && strncmp (s, "-m", 2) == 0 && ((s)[2] == ' ' || (s)[2] == '\t'))
0fb4aa4b 243
2060206e
PA
244/* The abstract base class all breakpoint_ops structures inherit
245 from. */
246static struct breakpoint_ops base_breakpoint_ops;
247
248/* The breakpoint_ops structure to be inherited by all breakpoint_ops
249 that are implemented on top of software or hardware breakpoints
250 (user breakpoints, internal and momentary breakpoints, etc.). */
251static struct breakpoint_ops bkpt_base_breakpoint_ops;
252
253/* Internal breakpoints class type. */
06edf0c0 254static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
255
256/* Momentary breakpoints class type. */
06edf0c0
PA
257static struct breakpoint_ops momentary_breakpoint_ops;
258
2060206e
PA
259/* The breakpoint_ops structure to be used in regular user created
260 breakpoints. */
261struct breakpoint_ops bkpt_breakpoint_ops;
262
5cea2a26
PA
263/* A reference-counted struct command_line. This lets multiple
264 breakpoints share a single command list. */
265struct counted_command_line
266{
267 /* The reference count. */
268 int refc;
269
270 /* The command list. */
271 struct command_line *commands;
272};
273
274struct command_line *
275breakpoint_commands (struct breakpoint *b)
276{
277 return b->commands ? b->commands->commands : NULL;
278}
3daf8fe5 279
f3b1572e
PA
280/* Flag indicating that a command has proceeded the inferior past the
281 current breakpoint. */
282
283static int breakpoint_proceeded;
284
956a9fb9 285const char *
2cec12e5
AR
286bpdisp_text (enum bpdisp disp)
287{
4a64f543
MS
288 /* NOTE: the following values are a part of MI protocol and
289 represent values of 'disp' field returned when inferior stops at
290 a breakpoint. */
bc043ef3 291 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 292
2cec12e5
AR
293 return bpdisps[(int) disp];
294}
c906108c 295
4a64f543 296/* Prototypes for exported functions. */
c906108c 297/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 298 if such is available. */
c906108c
SS
299static int can_use_hw_watchpoints;
300
920d2a44
AC
301static void
302show_can_use_hw_watchpoints (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 _("Debugger's willingness to use "
308 "watchpoint hardware is %s.\n"),
920d2a44
AC
309 value);
310}
311
fa8d40ab
JJ
312/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
313 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 314 for unrecognized breakpoint locations.
fa8d40ab
JJ
315 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
316static enum auto_boolean pending_break_support;
920d2a44
AC
317static void
318show_pending_break_support (struct ui_file *file, int from_tty,
319 struct cmd_list_element *c,
320 const char *value)
321{
3e43a32a
MS
322 fprintf_filtered (file,
323 _("Debugger's behavior regarding "
324 "pending breakpoints is %s.\n"),
920d2a44
AC
325 value);
326}
fa8d40ab 327
765dc015 328/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 329 set with "break" but falling in read-only memory.
765dc015
VP
330 If 0, gdb will warn about such breakpoints, but won't automatically
331 use hardware breakpoints. */
332static int automatic_hardware_breakpoints;
333static void
334show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
335 struct cmd_list_element *c,
336 const char *value)
337{
3e43a32a
MS
338 fprintf_filtered (file,
339 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
340 value);
341}
342
33e5cbd6
PA
343/* If on, gdb will keep breakpoints inserted even as inferior is
344 stopped, and immediately insert any new breakpoints. If off, gdb
345 will insert breakpoints into inferior only when resuming it, and
346 will remove breakpoints upon stop. If auto, GDB will behave as ON
347 if in non-stop mode, and as OFF if all-stop mode.*/
348
349static const char always_inserted_auto[] = "auto";
350static const char always_inserted_on[] = "on";
351static const char always_inserted_off[] = "off";
352static const char *always_inserted_enums[] = {
353 always_inserted_auto,
354 always_inserted_off,
355 always_inserted_on,
356 NULL
357};
358static const char *always_inserted_mode = always_inserted_auto;
359static void
74960c60 360show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 361 struct cmd_list_element *c, const char *value)
74960c60 362{
33e5cbd6 363 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
364 fprintf_filtered (file,
365 _("Always inserted breakpoint "
366 "mode is %s (currently %s).\n"),
33e5cbd6
PA
367 value,
368 breakpoints_always_inserted_mode () ? "on" : "off");
369 else
3e43a32a
MS
370 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
371 value);
74960c60
VP
372}
373
33e5cbd6
PA
374int
375breakpoints_always_inserted_mode (void)
376{
377 return (always_inserted_mode == always_inserted_on
378 || (always_inserted_mode == always_inserted_auto && non_stop));
379}
765dc015 380
a14ed312 381void _initialize_breakpoint (void);
c906108c 382
c906108c
SS
383/* Are we executing breakpoint commands? */
384static int executing_breakpoint_commands;
385
c02f5703
MS
386/* Are overlay event breakpoints enabled? */
387static int overlay_events_enabled;
388
e09342b5
TJB
389/* See description in breakpoint.h. */
390int target_exact_watchpoints = 0;
391
c906108c 392/* Walk the following statement or block through all breakpoints.
e5dd4106 393 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 394 current breakpoint. */
c906108c 395
5c44784c 396#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 397
5c44784c
JM
398#define ALL_BREAKPOINTS_SAFE(B,TMP) \
399 for (B = breakpoint_chain; \
400 B ? (TMP=B->next, 1): 0; \
401 B = TMP)
c906108c 402
4a64f543
MS
403/* Similar iterator for the low-level breakpoints. SAFE variant is
404 not provided so update_global_location_list must not be called
405 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 406
876fa593
JK
407#define ALL_BP_LOCATIONS(B,BP_TMP) \
408 for (BP_TMP = bp_location; \
409 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
410 BP_TMP++)
7cc221ef 411
1042e4c0
SS
412/* Iterator for tracepoints only. */
413
414#define ALL_TRACEPOINTS(B) \
415 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 416 if (is_tracepoint (B))
1042e4c0 417
7cc221ef 418/* Chains of all breakpoints defined. */
c906108c
SS
419
420struct breakpoint *breakpoint_chain;
421
876fa593
JK
422/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
423
424static struct bp_location **bp_location;
425
426/* Number of elements of BP_LOCATION. */
427
428static unsigned bp_location_count;
429
4a64f543
MS
430/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
431 ADDRESS for the current elements of BP_LOCATION which get a valid
432 result from bp_location_has_shadow. You can use it for roughly
433 limiting the subrange of BP_LOCATION to scan for shadow bytes for
434 an address you need to read. */
876fa593
JK
435
436static CORE_ADDR bp_location_placed_address_before_address_max;
437
4a64f543
MS
438/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
439 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
440 BP_LOCATION which get a valid result from bp_location_has_shadow.
441 You can use it for roughly limiting the subrange of BP_LOCATION to
442 scan for shadow bytes for an address you need to read. */
876fa593
JK
443
444static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 445
4a64f543
MS
446/* The locations that no longer correspond to any breakpoint, unlinked
447 from bp_location array, but for which a hit may still be reported
448 by a target. */
20874c92
VP
449VEC(bp_location_p) *moribund_locations = NULL;
450
c906108c
SS
451/* Number of last breakpoint made. */
452
95a42b64
TT
453static int breakpoint_count;
454
86b17b60
PA
455/* The value of `breakpoint_count' before the last command that
456 created breakpoints. If the last (break-like) command created more
457 than one breakpoint, then the difference between BREAKPOINT_COUNT
458 and PREV_BREAKPOINT_COUNT is more than one. */
459static int prev_breakpoint_count;
c906108c 460
1042e4c0
SS
461/* Number of last tracepoint made. */
462
95a42b64 463static int tracepoint_count;
1042e4c0 464
6149aea9
PA
465static struct cmd_list_element *breakpoint_set_cmdlist;
466static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 467struct cmd_list_element *save_cmdlist;
6149aea9 468
468d015d
JJ
469/* Return whether a breakpoint is an active enabled breakpoint. */
470static int
471breakpoint_enabled (struct breakpoint *b)
472{
0d381245 473 return (b->enable_state == bp_enabled);
468d015d
JJ
474}
475
c906108c
SS
476/* Set breakpoint count to NUM. */
477
95a42b64 478static void
fba45db2 479set_breakpoint_count (int num)
c906108c 480{
86b17b60 481 prev_breakpoint_count = breakpoint_count;
c906108c 482 breakpoint_count = num;
4fa62494 483 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
484}
485
86b17b60
PA
486/* Used by `start_rbreak_breakpoints' below, to record the current
487 breakpoint count before "rbreak" creates any breakpoint. */
488static int rbreak_start_breakpoint_count;
489
95a42b64
TT
490/* Called at the start an "rbreak" command to record the first
491 breakpoint made. */
86b17b60 492
95a42b64
TT
493void
494start_rbreak_breakpoints (void)
495{
86b17b60 496 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
497}
498
499/* Called at the end of an "rbreak" command to record the last
500 breakpoint made. */
86b17b60 501
95a42b64
TT
502void
503end_rbreak_breakpoints (void)
504{
86b17b60 505 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
506}
507
4a64f543 508/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
509
510void
fba45db2 511clear_breakpoint_hit_counts (void)
c906108c
SS
512{
513 struct breakpoint *b;
514
515 ALL_BREAKPOINTS (b)
516 b->hit_count = 0;
517}
518
9add0f1b
TT
519/* Allocate a new counted_command_line with reference count of 1.
520 The new structure owns COMMANDS. */
521
522static struct counted_command_line *
523alloc_counted_command_line (struct command_line *commands)
524{
525 struct counted_command_line *result
526 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 527
9add0f1b
TT
528 result->refc = 1;
529 result->commands = commands;
530 return result;
531}
532
533/* Increment reference count. This does nothing if CMD is NULL. */
534
535static void
536incref_counted_command_line (struct counted_command_line *cmd)
537{
538 if (cmd)
539 ++cmd->refc;
540}
541
542/* Decrement reference count. If the reference count reaches 0,
543 destroy the counted_command_line. Sets *CMDP to NULL. This does
544 nothing if *CMDP is NULL. */
545
546static void
547decref_counted_command_line (struct counted_command_line **cmdp)
548{
549 if (*cmdp)
550 {
551 if (--(*cmdp)->refc == 0)
552 {
553 free_command_lines (&(*cmdp)->commands);
554 xfree (*cmdp);
555 }
556 *cmdp = NULL;
557 }
558}
559
560/* A cleanup function that calls decref_counted_command_line. */
561
562static void
563do_cleanup_counted_command_line (void *arg)
564{
565 decref_counted_command_line (arg);
566}
567
568/* Create a cleanup that calls decref_counted_command_line on the
569 argument. */
570
571static struct cleanup *
572make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
573{
574 return make_cleanup (do_cleanup_counted_command_line, cmdp);
575}
576
c906108c 577\f
48cb2d85
VP
578/* Return the breakpoint with the specified number, or NULL
579 if the number does not refer to an existing breakpoint. */
580
581struct breakpoint *
582get_breakpoint (int num)
583{
584 struct breakpoint *b;
585
586 ALL_BREAKPOINTS (b)
587 if (b->number == num)
588 return b;
589
590 return NULL;
591}
5c44784c 592
c906108c 593\f
adc36818
PM
594
595void
596set_breakpoint_condition (struct breakpoint *b, char *exp,
597 int from_tty)
598{
3a5c3e22
PA
599 xfree (b->cond_string);
600 b->cond_string = NULL;
adc36818 601
3a5c3e22 602 if (is_watchpoint (b))
adc36818 603 {
3a5c3e22
PA
604 struct watchpoint *w = (struct watchpoint *) b;
605
606 xfree (w->cond_exp);
607 w->cond_exp = NULL;
608 }
609 else
610 {
611 struct bp_location *loc;
612
613 for (loc = b->loc; loc; loc = loc->next)
614 {
615 xfree (loc->cond);
616 loc->cond = NULL;
617 }
adc36818 618 }
adc36818
PM
619
620 if (*exp == 0)
621 {
622 if (from_tty)
623 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
624 }
625 else
626 {
627 char *arg = exp;
cc59ec59 628
adc36818
PM
629 /* I don't know if it matters whether this is the string the user
630 typed in or the decompiled expression. */
631 b->cond_string = xstrdup (arg);
632 b->condition_not_parsed = 0;
633
634 if (is_watchpoint (b))
635 {
3a5c3e22
PA
636 struct watchpoint *w = (struct watchpoint *) b;
637
adc36818
PM
638 innermost_block = NULL;
639 arg = exp;
3a5c3e22 640 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
641 if (*arg)
642 error (_("Junk at end of expression"));
3a5c3e22 643 w->cond_exp_valid_block = innermost_block;
adc36818
PM
644 }
645 else
646 {
3a5c3e22
PA
647 struct bp_location *loc;
648
adc36818
PM
649 for (loc = b->loc; loc; loc = loc->next)
650 {
651 arg = exp;
652 loc->cond =
653 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
654 if (*arg)
655 error (_("Junk at end of expression"));
656 }
657 }
658 }
659 breakpoints_changed ();
8d3788bd 660 observer_notify_breakpoint_modified (b);
adc36818
PM
661}
662
c906108c
SS
663/* condition N EXP -- set break condition of breakpoint N to EXP. */
664
665static void
fba45db2 666condition_command (char *arg, int from_tty)
c906108c 667{
52f0bd74 668 struct breakpoint *b;
c906108c 669 char *p;
52f0bd74 670 int bnum;
c906108c
SS
671
672 if (arg == 0)
e2e0b3e5 673 error_no_arg (_("breakpoint number"));
c906108c
SS
674
675 p = arg;
676 bnum = get_number (&p);
5c44784c 677 if (bnum == 0)
8a3fe4f8 678 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
679
680 ALL_BREAKPOINTS (b)
681 if (b->number == bnum)
2f069f6f 682 {
7371cf6d
PM
683 /* Check if this breakpoint has a Python object assigned to
684 it, and if it has a definition of the "stop"
685 method. This method and conditions entered into GDB from
686 the CLI are mutually exclusive. */
687 if (b->py_bp_object
688 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
689 error (_("Cannot set a condition where a Python 'stop' "
690 "method has been defined in the breakpoint."));
2566ad2d 691 set_breakpoint_condition (b, p, from_tty);
2f069f6f
JB
692 return;
693 }
c906108c 694
8a3fe4f8 695 error (_("No breakpoint number %d."), bnum);
c906108c
SS
696}
697
a7bdde9e
VP
698/* Check that COMMAND do not contain commands that are suitable
699 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
700 Throw if any such commands is found. */
701
a7bdde9e
VP
702static void
703check_no_tracepoint_commands (struct command_line *commands)
704{
705 struct command_line *c;
cc59ec59 706
a7bdde9e
VP
707 for (c = commands; c; c = c->next)
708 {
709 int i;
710
711 if (c->control_type == while_stepping_control)
3e43a32a
MS
712 error (_("The 'while-stepping' command can "
713 "only be used for tracepoints"));
a7bdde9e
VP
714
715 for (i = 0; i < c->body_count; ++i)
716 check_no_tracepoint_commands ((c->body_list)[i]);
717
718 /* Not that command parsing removes leading whitespace and comment
4a64f543 719 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
720 command directly. */
721 if (strstr (c->line, "collect ") == c->line)
722 error (_("The 'collect' command can only be used for tracepoints"));
723
51661e93
VP
724 if (strstr (c->line, "teval ") == c->line)
725 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
726 }
727}
728
d77f58be
SS
729/* Encapsulate tests for different types of tracepoints. */
730
d9b3f62e
PA
731static int
732is_tracepoint_type (enum bptype type)
733{
734 return (type == bp_tracepoint
735 || type == bp_fast_tracepoint
736 || type == bp_static_tracepoint);
737}
738
a7bdde9e 739int
d77f58be 740is_tracepoint (const struct breakpoint *b)
a7bdde9e 741{
d9b3f62e 742 return is_tracepoint_type (b->type);
a7bdde9e 743}
d9b3f62e 744
e5dd4106 745/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
746 breakpoint. This function will throw an exception if a problem is
747 found. */
48cb2d85 748
95a42b64
TT
749static void
750validate_commands_for_breakpoint (struct breakpoint *b,
751 struct command_line *commands)
48cb2d85 752{
d77f58be 753 if (is_tracepoint (b))
a7bdde9e 754 {
4a64f543
MS
755 /* We need to verify that each top-level element of commands is
756 valid for tracepoints, that there's at most one
757 while-stepping element, and that while-stepping's body has
758 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
759 struct command_line *c;
760 struct command_line *while_stepping = 0;
761 for (c = commands; c; c = c->next)
762 {
a7bdde9e
VP
763 if (c->control_type == while_stepping_control)
764 {
765 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
766 error (_("The 'while-stepping' command "
767 "cannot be used for fast tracepoint"));
0fb4aa4b 768 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
769 error (_("The 'while-stepping' command "
770 "cannot be used for static tracepoint"));
a7bdde9e
VP
771
772 if (while_stepping)
3e43a32a
MS
773 error (_("The 'while-stepping' command "
774 "can be used only once"));
a7bdde9e
VP
775 else
776 while_stepping = c;
777 }
778 }
779 if (while_stepping)
780 {
781 struct command_line *c2;
782
783 gdb_assert (while_stepping->body_count == 1);
784 c2 = while_stepping->body_list[0];
785 for (; c2; c2 = c2->next)
786 {
a7bdde9e
VP
787 if (c2->control_type == while_stepping_control)
788 error (_("The 'while-stepping' command cannot be nested"));
789 }
790 }
791 }
792 else
793 {
794 check_no_tracepoint_commands (commands);
795 }
95a42b64
TT
796}
797
0fb4aa4b
PA
798/* Return a vector of all the static tracepoints set at ADDR. The
799 caller is responsible for releasing the vector. */
800
801VEC(breakpoint_p) *
802static_tracepoints_here (CORE_ADDR addr)
803{
804 struct breakpoint *b;
805 VEC(breakpoint_p) *found = 0;
806 struct bp_location *loc;
807
808 ALL_BREAKPOINTS (b)
809 if (b->type == bp_static_tracepoint)
810 {
811 for (loc = b->loc; loc; loc = loc->next)
812 if (loc->address == addr)
813 VEC_safe_push(breakpoint_p, found, b);
814 }
815
816 return found;
817}
818
95a42b64 819/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 820 validate that only allowed commands are included. */
95a42b64
TT
821
822void
4a64f543
MS
823breakpoint_set_commands (struct breakpoint *b,
824 struct command_line *commands)
95a42b64
TT
825{
826 validate_commands_for_breakpoint (b, commands);
a7bdde9e 827
9add0f1b
TT
828 decref_counted_command_line (&b->commands);
829 b->commands = alloc_counted_command_line (commands);
48cb2d85 830 breakpoints_changed ();
8d3788bd 831 observer_notify_breakpoint_modified (b);
48cb2d85
VP
832}
833
45a43567
TT
834/* Set the internal `silent' flag on the breakpoint. Note that this
835 is not the same as the "silent" that may appear in the breakpoint's
836 commands. */
837
838void
839breakpoint_set_silent (struct breakpoint *b, int silent)
840{
841 int old_silent = b->silent;
842
843 b->silent = silent;
844 if (old_silent != silent)
8d3788bd 845 observer_notify_breakpoint_modified (b);
45a43567
TT
846}
847
848/* Set the thread for this breakpoint. If THREAD is -1, make the
849 breakpoint work for any thread. */
850
851void
852breakpoint_set_thread (struct breakpoint *b, int thread)
853{
854 int old_thread = b->thread;
855
856 b->thread = thread;
857 if (old_thread != thread)
8d3788bd 858 observer_notify_breakpoint_modified (b);
45a43567
TT
859}
860
861/* Set the task for this breakpoint. If TASK is 0, make the
862 breakpoint work for any task. */
863
864void
865breakpoint_set_task (struct breakpoint *b, int task)
866{
867 int old_task = b->task;
868
869 b->task = task;
870 if (old_task != task)
8d3788bd 871 observer_notify_breakpoint_modified (b);
45a43567
TT
872}
873
95a42b64
TT
874void
875check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
876{
877 struct breakpoint *b = closure;
cc59ec59 878
a7bdde9e
VP
879 validate_actionline (&line, b);
880}
881
95a42b64
TT
882/* A structure used to pass information through
883 map_breakpoint_numbers. */
884
885struct commands_info
886{
887 /* True if the command was typed at a tty. */
888 int from_tty;
86b17b60
PA
889
890 /* The breakpoint range spec. */
891 char *arg;
892
95a42b64
TT
893 /* Non-NULL if the body of the commands are being read from this
894 already-parsed command. */
895 struct command_line *control;
86b17b60 896
95a42b64
TT
897 /* The command lines read from the user, or NULL if they have not
898 yet been read. */
899 struct counted_command_line *cmd;
900};
901
902/* A callback for map_breakpoint_numbers that sets the commands for
903 commands_command. */
904
c906108c 905static void
95a42b64 906do_map_commands_command (struct breakpoint *b, void *data)
c906108c 907{
95a42b64 908 struct commands_info *info = data;
c906108c 909
95a42b64
TT
910 if (info->cmd == NULL)
911 {
912 struct command_line *l;
5c44784c 913
95a42b64
TT
914 if (info->control != NULL)
915 l = copy_command_lines (info->control->body_list[0]);
916 else
86b17b60
PA
917 {
918 struct cleanup *old_chain;
919 char *str;
c5aa993b 920
3e43a32a
MS
921 str = xstrprintf (_("Type commands for breakpoint(s) "
922 "%s, one per line."),
86b17b60
PA
923 info->arg);
924
925 old_chain = make_cleanup (xfree, str);
926
927 l = read_command_lines (str,
928 info->from_tty, 1,
d77f58be 929 (is_tracepoint (b)
86b17b60
PA
930 ? check_tracepoint_command : 0),
931 b);
932
933 do_cleanups (old_chain);
934 }
a7bdde9e 935
95a42b64
TT
936 info->cmd = alloc_counted_command_line (l);
937 }
938
939 /* If a breakpoint was on the list more than once, we don't need to
940 do anything. */
941 if (b->commands != info->cmd)
942 {
943 validate_commands_for_breakpoint (b, info->cmd->commands);
944 incref_counted_command_line (info->cmd);
945 decref_counted_command_line (&b->commands);
946 b->commands = info->cmd;
947 breakpoints_changed ();
8d3788bd 948 observer_notify_breakpoint_modified (b);
c5aa993b 949 }
95a42b64
TT
950}
951
952static void
4a64f543
MS
953commands_command_1 (char *arg, int from_tty,
954 struct command_line *control)
95a42b64
TT
955{
956 struct cleanup *cleanups;
957 struct commands_info info;
958
959 info.from_tty = from_tty;
960 info.control = control;
961 info.cmd = NULL;
962 /* If we read command lines from the user, then `info' will hold an
963 extra reference to the commands that we must clean up. */
964 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
965
966 if (arg == NULL || !*arg)
967 {
86b17b60 968 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
969 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
970 breakpoint_count);
95a42b64
TT
971 else if (breakpoint_count > 0)
972 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
973 else
974 {
975 /* So that we don't try to free the incoming non-NULL
976 argument in the cleanup below. Mapping breakpoint
977 numbers will fail in this case. */
978 arg = NULL;
979 }
95a42b64 980 }
9766ced4
SS
981 else
982 /* The command loop has some static state, so we need to preserve
983 our argument. */
984 arg = xstrdup (arg);
86b17b60
PA
985
986 if (arg != NULL)
987 make_cleanup (xfree, arg);
988
989 info.arg = arg;
95a42b64
TT
990
991 map_breakpoint_numbers (arg, do_map_commands_command, &info);
992
993 if (info.cmd == NULL)
994 error (_("No breakpoints specified."));
995
996 do_cleanups (cleanups);
997}
998
999static void
1000commands_command (char *arg, int from_tty)
1001{
1002 commands_command_1 (arg, from_tty, NULL);
c906108c 1003}
40c03ae8
EZ
1004
1005/* Like commands_command, but instead of reading the commands from
1006 input stream, takes them from an already parsed command structure.
1007
1008 This is used by cli-script.c to DTRT with breakpoint commands
1009 that are part of if and while bodies. */
1010enum command_control_type
1011commands_from_control_command (char *arg, struct command_line *cmd)
1012{
95a42b64
TT
1013 commands_command_1 (arg, 0, cmd);
1014 return simple_control;
40c03ae8 1015}
876fa593
JK
1016
1017/* Return non-zero if BL->TARGET_INFO contains valid information. */
1018
1019static int
1020bp_location_has_shadow (struct bp_location *bl)
1021{
1022 if (bl->loc_type != bp_loc_software_breakpoint)
1023 return 0;
1024 if (!bl->inserted)
1025 return 0;
1026 if (bl->target_info.shadow_len == 0)
e5dd4106 1027 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1028 return 0;
1029 return 1;
1030}
1031
8defab1a 1032/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1033 by replacing any memory breakpoints with their shadowed contents.
1034
1035 The range of shadowed area by each bp_location is:
35df4500
TJB
1036 bl->address - bp_location_placed_address_before_address_max
1037 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1038 The range we were requested to resolve shadows for is:
1039 memaddr ... memaddr + len
1040 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1041 memaddr + len <= (bl->address
1042 - bp_location_placed_address_before_address_max)
876fa593 1043 and:
35df4500 1044 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1045
8defab1a
DJ
1046void
1047breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 1048{
4a64f543
MS
1049 /* Left boundary, right boundary and median element of our binary
1050 search. */
876fa593
JK
1051 unsigned bc_l, bc_r, bc;
1052
4a64f543
MS
1053 /* Find BC_L which is a leftmost element which may affect BUF
1054 content. It is safe to report lower value but a failure to
1055 report higher one. */
876fa593
JK
1056
1057 bc_l = 0;
1058 bc_r = bp_location_count;
1059 while (bc_l + 1 < bc_r)
1060 {
35df4500 1061 struct bp_location *bl;
876fa593
JK
1062
1063 bc = (bc_l + bc_r) / 2;
35df4500 1064 bl = bp_location[bc];
876fa593 1065
4a64f543
MS
1066 /* Check first BL->ADDRESS will not overflow due to the added
1067 constant. Then advance the left boundary only if we are sure
1068 the BC element can in no way affect the BUF content (MEMADDR
1069 to MEMADDR + LEN range).
876fa593 1070
4a64f543
MS
1071 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1072 offset so that we cannot miss a breakpoint with its shadow
1073 range tail still reaching MEMADDR. */
c5aa993b 1074
35df4500
TJB
1075 if ((bl->address + bp_location_shadow_len_after_address_max
1076 >= bl->address)
1077 && (bl->address + bp_location_shadow_len_after_address_max
1078 <= memaddr))
876fa593
JK
1079 bc_l = bc;
1080 else
1081 bc_r = bc;
1082 }
1083
128070bb
PA
1084 /* Due to the binary search above, we need to make sure we pick the
1085 first location that's at BC_L's address. E.g., if there are
1086 multiple locations at the same address, BC_L may end up pointing
1087 at a duplicate location, and miss the "master"/"inserted"
1088 location. Say, given locations L1, L2 and L3 at addresses A and
1089 B:
1090
1091 L1@A, L2@A, L3@B, ...
1092
1093 BC_L could end up pointing at location L2, while the "master"
1094 location could be L1. Since the `loc->inserted' flag is only set
1095 on "master" locations, we'd forget to restore the shadow of L1
1096 and L2. */
1097 while (bc_l > 0
1098 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1099 bc_l--;
1100
876fa593
JK
1101 /* Now do full processing of the found relevant range of elements. */
1102
1103 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1104 {
35df4500 1105 struct bp_location *bl = bp_location[bc];
876fa593
JK
1106 CORE_ADDR bp_addr = 0;
1107 int bp_size = 0;
1108 int bptoffset = 0;
1109
35df4500
TJB
1110 /* bp_location array has BL->OWNER always non-NULL. */
1111 if (bl->owner->type == bp_none)
8a3fe4f8 1112 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1113 bl->owner->number);
ffce0d52 1114
e5dd4106 1115 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1116 content. */
1117
35df4500
TJB
1118 if (bl->address >= bp_location_placed_address_before_address_max
1119 && memaddr + len <= (bl->address
1120 - bp_location_placed_address_before_address_max))
876fa593
JK
1121 break;
1122
35df4500 1123 if (!bp_location_has_shadow (bl))
c5aa993b 1124 continue;
35df4500 1125 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1126 current_program_space->aspace, 0))
1127 continue;
1128
c5aa993b
JM
1129 /* Addresses and length of the part of the breakpoint that
1130 we need to copy. */
35df4500
TJB
1131 bp_addr = bl->target_info.placed_address;
1132 bp_size = bl->target_info.shadow_len;
8defab1a 1133
c5aa993b
JM
1134 if (bp_addr + bp_size <= memaddr)
1135 /* The breakpoint is entirely before the chunk of memory we
1136 are reading. */
1137 continue;
8defab1a 1138
c5aa993b
JM
1139 if (bp_addr >= memaddr + len)
1140 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1141 reading. */
c5aa993b 1142 continue;
c5aa993b 1143
8defab1a
DJ
1144 /* Offset within shadow_contents. */
1145 if (bp_addr < memaddr)
1146 {
1147 /* Only copy the second part of the breakpoint. */
1148 bp_size -= memaddr - bp_addr;
1149 bptoffset = memaddr - bp_addr;
1150 bp_addr = memaddr;
1151 }
c5aa993b 1152
8defab1a
DJ
1153 if (bp_addr + bp_size > memaddr + len)
1154 {
1155 /* Only copy the first part of the breakpoint. */
1156 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1157 }
c5aa993b 1158
8defab1a 1159 memcpy (buf + bp_addr - memaddr,
35df4500 1160 bl->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 1161 }
c906108c 1162}
c906108c 1163\f
c5aa993b 1164
60e1c644
PA
1165/* Return true if BPT is of any hardware watchpoint kind. */
1166
a5606eee 1167static int
d77f58be 1168is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1169{
1170 return (bpt->type == bp_hardware_watchpoint
1171 || bpt->type == bp_read_watchpoint
1172 || bpt->type == bp_access_watchpoint);
1173}
7270d8f2 1174
60e1c644
PA
1175/* Return true if BPT is of any watchpoint kind, hardware or
1176 software. */
1177
3a5c3e22 1178int
d77f58be 1179is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1180{
1181 return (is_hardware_watchpoint (bpt)
1182 || bpt->type == bp_watchpoint);
1183}
1184
3a5c3e22
PA
1185/* Returns true if the current thread and its running state are safe
1186 to evaluate or update watchpoint B. Watchpoints on local
1187 expressions need to be evaluated in the context of the thread that
1188 was current when the watchpoint was created, and, that thread needs
1189 to be stopped to be able to select the correct frame context.
1190 Watchpoints on global expressions can be evaluated on any thread,
1191 and in any state. It is presently left to the target allowing
1192 memory accesses when threads are running. */
f6bc2008
PA
1193
1194static int
3a5c3e22 1195watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008
PA
1196{
1197 return (ptid_equal (b->watchpoint_thread, null_ptid)
1198 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1199 && !is_executing (inferior_ptid)));
1200}
1201
d0fb5eae
JK
1202/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1203 associated bp_watchpoint_scope breakpoint. */
1204
1205static void
3a5c3e22 1206watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1207{
3a5c3e22 1208 struct breakpoint *b = &w->base;
d0fb5eae
JK
1209
1210 if (b->related_breakpoint != b)
1211 {
1212 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1213 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1214 b->related_breakpoint->disposition = disp_del_at_next_stop;
1215 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1216 b->related_breakpoint = b;
1217 }
1218 b->disposition = disp_del_at_next_stop;
1219}
1220
567e1b4e
JB
1221/* Assuming that B is a watchpoint:
1222 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1223 - Evaluate expression and store the result in B->val
567e1b4e
JB
1224 - Evaluate the condition if there is one, and store the result
1225 in b->loc->cond.
a5606eee
VP
1226 - Update the list of values that must be watched in B->loc.
1227
4a64f543
MS
1228 If the watchpoint disposition is disp_del_at_next_stop, then do
1229 nothing. If this is local watchpoint that is out of scope, delete
1230 it.
1231
1232 Even with `set breakpoint always-inserted on' the watchpoints are
1233 removed + inserted on each stop here. Normal breakpoints must
1234 never be removed because they might be missed by a running thread
1235 when debugging in non-stop mode. On the other hand, hardware
1236 watchpoints (is_hardware_watchpoint; processed here) are specific
1237 to each LWP since they are stored in each LWP's hardware debug
1238 registers. Therefore, such LWP must be stopped first in order to
1239 be able to modify its hardware watchpoints.
1240
1241 Hardware watchpoints must be reset exactly once after being
1242 presented to the user. It cannot be done sooner, because it would
1243 reset the data used to present the watchpoint hit to the user. And
1244 it must not be done later because it could display the same single
1245 watchpoint hit during multiple GDB stops. Note that the latter is
1246 relevant only to the hardware watchpoint types bp_read_watchpoint
1247 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1248 not user-visible - its hit is suppressed if the memory content has
1249 not changed.
1250
1251 The following constraints influence the location where we can reset
1252 hardware watchpoints:
1253
1254 * target_stopped_by_watchpoint and target_stopped_data_address are
1255 called several times when GDB stops.
1256
1257 [linux]
1258 * Multiple hardware watchpoints can be hit at the same time,
1259 causing GDB to stop. GDB only presents one hardware watchpoint
1260 hit at a time as the reason for stopping, and all the other hits
1261 are presented later, one after the other, each time the user
1262 requests the execution to be resumed. Execution is not resumed
1263 for the threads still having pending hit event stored in
1264 LWP_INFO->STATUS. While the watchpoint is already removed from
1265 the inferior on the first stop the thread hit event is kept being
1266 reported from its cached value by linux_nat_stopped_data_address
1267 until the real thread resume happens after the watchpoint gets
1268 presented and thus its LWP_INFO->STATUS gets reset.
1269
1270 Therefore the hardware watchpoint hit can get safely reset on the
1271 watchpoint removal from inferior. */
a79d3c27 1272
b40ce68a 1273static void
3a5c3e22 1274update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1275{
a5606eee 1276 int within_current_scope;
a5606eee 1277 struct frame_id saved_frame_id;
66076460 1278 int frame_saved;
a5606eee 1279
f6bc2008
PA
1280 /* If this is a local watchpoint, we only want to check if the
1281 watchpoint frame is in scope if the current thread is the thread
1282 that was used to create the watchpoint. */
1283 if (!watchpoint_in_thread_scope (b))
1284 return;
1285
3a5c3e22 1286 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1287 return;
1288
66076460 1289 frame_saved = 0;
a5606eee
VP
1290
1291 /* Determine if the watchpoint is within scope. */
1292 if (b->exp_valid_block == NULL)
1293 within_current_scope = 1;
1294 else
1295 {
b5db5dfc
UW
1296 struct frame_info *fi = get_current_frame ();
1297 struct gdbarch *frame_arch = get_frame_arch (fi);
1298 CORE_ADDR frame_pc = get_frame_pc (fi);
1299
1300 /* If we're in a function epilogue, unwinding may not work
1301 properly, so do not attempt to recreate locations at this
1302 point. See similar comments in watchpoint_check. */
1303 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1304 return;
66076460
DJ
1305
1306 /* Save the current frame's ID so we can restore it after
1307 evaluating the watchpoint expression on its own frame. */
1308 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1309 took a frame parameter, so that we didn't have to change the
1310 selected frame. */
1311 frame_saved = 1;
1312 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1313
a5606eee
VP
1314 fi = frame_find_by_id (b->watchpoint_frame);
1315 within_current_scope = (fi != NULL);
1316 if (within_current_scope)
1317 select_frame (fi);
1318 }
1319
b5db5dfc
UW
1320 /* We don't free locations. They are stored in the bp_location array
1321 and update_global_location_list will eventually delete them and
1322 remove breakpoints if needed. */
3a5c3e22 1323 b->base.loc = NULL;
b5db5dfc 1324
a5606eee
VP
1325 if (within_current_scope && reparse)
1326 {
1327 char *s;
d63d0675 1328
a5606eee
VP
1329 if (b->exp)
1330 {
1331 xfree (b->exp);
1332 b->exp = NULL;
1333 }
d63d0675 1334 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1335 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1336 /* If the meaning of expression itself changed, the old value is
1337 no longer relevant. We don't want to report a watchpoint hit
1338 to the user when the old value and the new value may actually
1339 be completely different objects. */
1340 value_free (b->val);
fa4727a6
DJ
1341 b->val = NULL;
1342 b->val_valid = 0;
60e1c644
PA
1343
1344 /* Note that unlike with breakpoints, the watchpoint's condition
1345 expression is stored in the breakpoint object, not in the
1346 locations (re)created below. */
3a5c3e22 1347 if (b->base.cond_string != NULL)
60e1c644
PA
1348 {
1349 if (b->cond_exp != NULL)
1350 {
1351 xfree (b->cond_exp);
1352 b->cond_exp = NULL;
1353 }
1354
3a5c3e22 1355 s = b->base.cond_string;
60e1c644
PA
1356 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1357 }
a5606eee 1358 }
a5606eee
VP
1359
1360 /* If we failed to parse the expression, for example because
1361 it refers to a global variable in a not-yet-loaded shared library,
1362 don't try to insert watchpoint. We don't automatically delete
1363 such watchpoint, though, since failure to parse expression
1364 is different from out-of-scope watchpoint. */
2d134ed3
PA
1365 if ( !target_has_execution)
1366 {
1367 /* Without execution, memory can't change. No use to try and
1368 set watchpoint locations. The watchpoint will be reset when
1369 the target gains execution, through breakpoint_re_set. */
1370 }
1371 else if (within_current_scope && b->exp)
a5606eee 1372 {
0cf6dd15 1373 int pc = 0;
fa4727a6 1374 struct value *val_chain, *v, *result, *next;
2d134ed3 1375 struct program_space *frame_pspace;
a5606eee 1376
0cf6dd15 1377 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1378
a5606eee
VP
1379 /* Avoid setting b->val if it's already set. The meaning of
1380 b->val is 'the last value' user saw, and we should update
1381 it only if we reported that last value to user. As it
9c06b0b4
TJB
1382 happens, the code that reports it updates b->val directly.
1383 We don't keep track of the memory value for masked
1384 watchpoints. */
3a5c3e22 1385 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1386 {
1387 b->val = v;
1388 b->val_valid = 1;
1389 }
a5606eee 1390
2d134ed3
PA
1391 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1392
a5606eee 1393 /* Look at each value on the value chain. */
9fa40276 1394 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1395 {
1396 /* If it's a memory location, and GDB actually needed
1397 its contents to evaluate the expression, then we
fa4727a6
DJ
1398 must watch it. If the first value returned is
1399 still lazy, that means an error occurred reading it;
1400 watch it anyway in case it becomes readable. */
a5606eee 1401 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1402 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1403 {
1404 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1405
a5606eee
VP
1406 /* We only watch structs and arrays if user asked
1407 for it explicitly, never if they just happen to
1408 appear in the middle of some value chain. */
fa4727a6 1409 if (v == result
a5606eee
VP
1410 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1411 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1412 {
1413 CORE_ADDR addr;
1414 int len, type;
1415 struct bp_location *loc, **tmp;
1416
42ae5230 1417 addr = value_address (v);
a5606eee
VP
1418 len = TYPE_LENGTH (value_type (v));
1419 type = hw_write;
3a5c3e22 1420 if (b->base.type == bp_read_watchpoint)
a5606eee 1421 type = hw_read;
3a5c3e22 1422 else if (b->base.type == bp_access_watchpoint)
a5606eee 1423 type = hw_access;
3a5c3e22
PA
1424
1425 loc = allocate_bp_location (&b->base);
1426 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1427 ;
1428 *tmp = loc;
a6d9a66e 1429 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1430
1431 loc->pspace = frame_pspace;
a5606eee
VP
1432 loc->address = addr;
1433 loc->length = len;
1434 loc->watchpoint_type = type;
1435 }
1436 }
9fa40276
TJB
1437 }
1438
1439 /* Change the type of breakpoint between hardware assisted or
1440 an ordinary watchpoint depending on the hardware support
1441 and free hardware slots. REPARSE is set when the inferior
1442 is started. */
a9634178 1443 if (reparse)
9fa40276 1444 {
e09342b5 1445 int reg_cnt;
9fa40276
TJB
1446 enum bp_loc_type loc_type;
1447 struct bp_location *bl;
a5606eee 1448
a9634178 1449 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1450
1451 if (reg_cnt)
9fa40276
TJB
1452 {
1453 int i, target_resources_ok, other_type_used;
a1398e0c 1454 enum bptype type;
9fa40276 1455
a9634178
TJB
1456 /* Use an exact watchpoint when there's only one memory region to be
1457 watched, and only one debug register is needed to watch it. */
1458 b->exact = target_exact_watchpoints && reg_cnt == 1;
1459
9fa40276 1460 /* We need to determine how many resources are already
e09342b5
TJB
1461 used for all other hardware watchpoints plus this one
1462 to see if we still have enough resources to also fit
a1398e0c
PA
1463 this watchpoint in as well. */
1464
1465 /* If this is a software watchpoint, we try to turn it
1466 to a hardware one -- count resources as if B was of
1467 hardware watchpoint type. */
1468 type = b->base.type;
1469 if (type == bp_watchpoint)
1470 type = bp_hardware_watchpoint;
1471
1472 /* This watchpoint may or may not have been placed on
1473 the list yet at this point (it won't be in the list
1474 if we're trying to create it for the first time,
1475 through watch_command), so always account for it
1476 manually. */
1477
1478 /* Count resources used by all watchpoints except B. */
1479 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1480
1481 /* Add in the resources needed for B. */
1482 i += hw_watchpoint_use_count (&b->base);
1483
1484 target_resources_ok
1485 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1486 if (target_resources_ok <= 0)
a9634178 1487 {
3a5c3e22 1488 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1489
1490 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1491 error (_("Target does not support this type of "
1492 "hardware watchpoint."));
9c06b0b4
TJB
1493 else if (target_resources_ok < 0 && !sw_mode)
1494 error (_("There are not enough available hardware "
1495 "resources for this watchpoint."));
a1398e0c
PA
1496
1497 /* Downgrade to software watchpoint. */
1498 b->base.type = bp_watchpoint;
1499 }
1500 else
1501 {
1502 /* If this was a software watchpoint, we've just
1503 found we have enough resources to turn it to a
1504 hardware watchpoint. Otherwise, this is a
1505 nop. */
1506 b->base.type = type;
a9634178 1507 }
9fa40276 1508 }
3a5c3e22 1509 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1510 error (_("Expression cannot be implemented with "
1511 "read/access watchpoint."));
9fa40276 1512 else
3a5c3e22 1513 b->base.type = bp_watchpoint;
9fa40276 1514
3a5c3e22 1515 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1516 : bp_loc_hardware_watchpoint);
3a5c3e22 1517 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1518 bl->loc_type = loc_type;
1519 }
1520
1521 for (v = val_chain; v; v = next)
1522 {
a5606eee
VP
1523 next = value_next (v);
1524 if (v != b->val)
1525 value_free (v);
1526 }
1527
c7437ca6
PA
1528 /* If a software watchpoint is not watching any memory, then the
1529 above left it without any location set up. But,
1530 bpstat_stop_status requires a location to be able to report
1531 stops, so make sure there's at least a dummy one. */
3a5c3e22 1532 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1533 {
3a5c3e22
PA
1534 struct breakpoint *base = &b->base;
1535 base->loc = allocate_bp_location (base);
1536 base->loc->pspace = frame_pspace;
1537 base->loc->address = -1;
1538 base->loc->length = -1;
1539 base->loc->watchpoint_type = -1;
c7437ca6 1540 }
a5606eee
VP
1541 }
1542 else if (!within_current_scope)
7270d8f2 1543 {
ac74f770
MS
1544 printf_filtered (_("\
1545Watchpoint %d deleted because the program has left the block\n\
1546in which its expression is valid.\n"),
3a5c3e22 1547 b->base.number);
d0fb5eae 1548 watchpoint_del_at_next_stop (b);
7270d8f2 1549 }
a5606eee
VP
1550
1551 /* Restore the selected frame. */
66076460
DJ
1552 if (frame_saved)
1553 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1554}
1555
a5606eee 1556
74960c60
VP
1557/* Returns 1 iff breakpoint location should be
1558 inserted in the inferior. */
1559static int
35df4500 1560should_be_inserted (struct bp_location *bl)
74960c60 1561{
35df4500 1562 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1563 return 0;
1564
35df4500 1565 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1566 return 0;
1567
35df4500 1568 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1569 return 0;
1570
56710373
PA
1571 /* This is set for example, when we're attached to the parent of a
1572 vfork, and have detached from the child. The child is running
1573 free, and we expect it to do an exec or exit, at which point the
1574 OS makes the parent schedulable again (and the target reports
1575 that the vfork is done). Until the child is done with the shared
1576 memory region, do not insert breakpoints in the parent, otherwise
1577 the child could still trip on the parent's breakpoints. Since
1578 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1579 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1580 return 0;
1581
1042e4c0
SS
1582 /* Tracepoints are inserted by the target at a time of its choosing,
1583 not by us. */
35df4500 1584 if (is_tracepoint (bl->owner))
1042e4c0
SS
1585 return 0;
1586
74960c60
VP
1587 return 1;
1588}
1589
934709f0
PW
1590/* Same as should_be_inserted but does the check assuming
1591 that the location is not duplicated. */
1592
1593static int
1594unduplicated_should_be_inserted (struct bp_location *bl)
1595{
1596 int result;
1597 const int save_duplicate = bl->duplicate;
1598
1599 bl->duplicate = 0;
1600 result = should_be_inserted (bl);
1601 bl->duplicate = save_duplicate;
1602 return result;
1603}
1604
35df4500
TJB
1605/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
1606 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 1607 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
1608 Returns 0 for success, 1 if the bp_location type is not supported or
1609 -1 for failure.
879bfdc2 1610
4a64f543
MS
1611 NOTE drow/2003-09-09: This routine could be broken down to an
1612 object-style method for each breakpoint or catchpoint type. */
26bb91f3 1613static int
35df4500 1614insert_bp_location (struct bp_location *bl,
26bb91f3 1615 struct ui_file *tmp_error_stream,
3fbb6ffa 1616 int *disabled_breaks,
26bb91f3 1617 int *hw_breakpoint_error)
879bfdc2
DJ
1618{
1619 int val = 0;
1620
35df4500 1621 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1622 return 0;
1623
8181d85f 1624 /* Initialize the target-specific information. */
35df4500
TJB
1625 memset (&bl->target_info, 0, sizeof (bl->target_info));
1626 bl->target_info.placed_address = bl->address;
1627 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 1628 bl->target_info.length = bl->length;
8181d85f 1629
35df4500
TJB
1630 if (bl->loc_type == bp_loc_software_breakpoint
1631 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 1632 {
35df4500 1633 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
1634 {
1635 /* If the explicitly specified breakpoint type
1636 is not hardware breakpoint, check the memory map to see
1637 if the breakpoint address is in read only memory or not.
4a64f543 1638
765dc015
VP
1639 Two important cases are:
1640 - location type is not hardware breakpoint, memory
1641 is readonly. We change the type of the location to
1642 hardware breakpoint.
4a64f543
MS
1643 - location type is hardware breakpoint, memory is
1644 read-write. This means we've previously made the
1645 location hardware one, but then the memory map changed,
1646 so we undo.
765dc015 1647
4a64f543
MS
1648 When breakpoints are removed, remove_breakpoints will use
1649 location types we've just set here, the only possible
1650 problem is that memory map has changed during running
1651 program, but it's not going to work anyway with current
1652 gdb. */
765dc015 1653 struct mem_region *mr
35df4500 1654 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
1655
1656 if (mr)
1657 {
1658 if (automatic_hardware_breakpoints)
1659 {
765dc015
VP
1660 enum bp_loc_type new_type;
1661
1662 if (mr->attrib.mode != MEM_RW)
1663 new_type = bp_loc_hardware_breakpoint;
1664 else
1665 new_type = bp_loc_software_breakpoint;
1666
35df4500 1667 if (new_type != bl->loc_type)
765dc015
VP
1668 {
1669 static int said = 0;
cc59ec59 1670
35df4500 1671 bl->loc_type = new_type;
765dc015
VP
1672 if (!said)
1673 {
3e43a32a
MS
1674 fprintf_filtered (gdb_stdout,
1675 _("Note: automatically using "
1676 "hardware breakpoints for "
1677 "read-only addresses.\n"));
765dc015
VP
1678 said = 1;
1679 }
1680 }
1681 }
35df4500 1682 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 1683 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
1684 warning (_("cannot set software breakpoint "
1685 "at readonly address %s"),
35df4500 1686 paddress (bl->gdbarch, bl->address));
765dc015
VP
1687 }
1688 }
1689
879bfdc2
DJ
1690 /* First check to see if we have to handle an overlay. */
1691 if (overlay_debugging == ovly_off
35df4500
TJB
1692 || bl->section == NULL
1693 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
1694 {
1695 /* No overlay handling: just set the breakpoint. */
1696
348d480f 1697 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1698 }
1699 else
1700 {
4a64f543 1701 /* This breakpoint is in an overlay section.
879bfdc2
DJ
1702 Shall we set a breakpoint at the LMA? */
1703 if (!overlay_events_enabled)
1704 {
1705 /* Yes -- overlay event support is not active,
1706 so we must try to set a breakpoint at the LMA.
1707 This will not work for a hardware breakpoint. */
35df4500 1708 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1709 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 1710 bl->owner->number);
879bfdc2
DJ
1711 else
1712 {
35df4500
TJB
1713 CORE_ADDR addr = overlay_unmapped_address (bl->address,
1714 bl->section);
879bfdc2 1715 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
1716 bl->overlay_target_info = bl->target_info;
1717 bl->overlay_target_info.placed_address = addr;
1718 val = target_insert_breakpoint (bl->gdbarch,
1719 &bl->overlay_target_info);
879bfdc2 1720 if (val != 0)
99361f52 1721 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
1722 "Overlay breakpoint %d "
1723 "failed: in ROM?\n",
35df4500 1724 bl->owner->number);
879bfdc2
DJ
1725 }
1726 }
1727 /* Shall we set a breakpoint at the VMA? */
35df4500 1728 if (section_is_mapped (bl->section))
879bfdc2
DJ
1729 {
1730 /* Yes. This overlay section is mapped into memory. */
348d480f 1731 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
1732 }
1733 else
1734 {
1735 /* No. This breakpoint will not be inserted.
1736 No error, but do not mark the bp as 'inserted'. */
1737 return 0;
1738 }
1739 }
1740
1741 if (val)
1742 {
1743 /* Can't set the breakpoint. */
35df4500 1744 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 1745 {
4a64f543 1746 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 1747 val = 0;
35df4500 1748 bl->shlib_disabled = 1;
8d3788bd 1749 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
1750 if (!*disabled_breaks)
1751 {
1752 fprintf_unfiltered (tmp_error_stream,
1753 "Cannot insert breakpoint %d.\n",
1754 bl->owner->number);
1755 fprintf_unfiltered (tmp_error_stream,
1756 "Temporarily disabling shared "
1757 "library breakpoints:\n");
1758 }
1759 *disabled_breaks = 1;
879bfdc2 1760 fprintf_unfiltered (tmp_error_stream,
35df4500 1761 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
1762 }
1763 else
879bfdc2 1764 {
35df4500 1765 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
1766 {
1767 *hw_breakpoint_error = 1;
3e43a32a
MS
1768 fprintf_unfiltered (tmp_error_stream,
1769 "Cannot insert hardware "
1770 "breakpoint %d.\n",
35df4500 1771 bl->owner->number);
879bfdc2
DJ
1772 }
1773 else
1774 {
1775 fprintf_unfiltered (tmp_error_stream,
1776 "Cannot insert breakpoint %d.\n",
35df4500 1777 bl->owner->number);
879bfdc2
DJ
1778 fprintf_filtered (tmp_error_stream,
1779 "Error accessing memory address ");
35df4500 1780 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 1781 tmp_error_stream);
879bfdc2
DJ
1782 fprintf_filtered (tmp_error_stream, ": %s.\n",
1783 safe_strerror (val));
1784 }
1785
1786 }
1787 }
1788 else
35df4500 1789 bl->inserted = 1;
879bfdc2
DJ
1790
1791 return val;
1792 }
1793
35df4500 1794 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 1795 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 1796 watchpoints. It's not clear that it's necessary... */
35df4500 1797 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 1798 {
77b06cd7
TJB
1799 gdb_assert (bl->owner->ops != NULL
1800 && bl->owner->ops->insert_location != NULL);
1801
1802 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
1803
1804 /* If trying to set a read-watchpoint, and it turns out it's not
1805 supported, try emulating one with an access watchpoint. */
35df4500 1806 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
1807 {
1808 struct bp_location *loc, **loc_temp;
1809
1810 /* But don't try to insert it, if there's already another
1811 hw_access location that would be considered a duplicate
1812 of this one. */
1813 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 1814 if (loc != bl
85d721b8 1815 && loc->watchpoint_type == hw_access
35df4500 1816 && watchpoint_locations_match (bl, loc))
85d721b8 1817 {
35df4500
TJB
1818 bl->duplicate = 1;
1819 bl->inserted = 1;
1820 bl->target_info = loc->target_info;
1821 bl->watchpoint_type = hw_access;
85d721b8
PA
1822 val = 0;
1823 break;
1824 }
1825
1826 if (val == 1)
1827 {
77b06cd7
TJB
1828 bl->watchpoint_type = hw_access;
1829 val = bl->owner->ops->insert_location (bl);
1830
1831 if (val)
1832 /* Back to the original value. */
1833 bl->watchpoint_type = hw_read;
85d721b8
PA
1834 }
1835 }
1836
35df4500 1837 bl->inserted = (val == 0);
879bfdc2
DJ
1838 }
1839
35df4500 1840 else if (bl->owner->type == bp_catchpoint)
879bfdc2 1841 {
77b06cd7
TJB
1842 gdb_assert (bl->owner->ops != NULL
1843 && bl->owner->ops->insert_location != NULL);
1844
1845 val = bl->owner->ops->insert_location (bl);
1846 if (val)
1847 {
1848 bl->owner->enable_state = bp_disabled;
1849
1850 if (val == 1)
1851 warning (_("\
1852Error inserting catchpoint %d: Your system does not support this type\n\
1853of catchpoint."), bl->owner->number);
1854 else
1855 warning (_("Error inserting catchpoint %d."), bl->owner->number);
1856 }
1857
1858 bl->inserted = (val == 0);
1640b821
DJ
1859
1860 /* We've already printed an error message if there was a problem
1861 inserting this catchpoint, and we've disabled the catchpoint,
1862 so just return success. */
1863 return 0;
879bfdc2
DJ
1864 }
1865
1866 return 0;
1867}
1868
6c95b8df
PA
1869/* This function is called when program space PSPACE is about to be
1870 deleted. It takes care of updating breakpoints to not reference
1871 PSPACE anymore. */
1872
1873void
1874breakpoint_program_space_exit (struct program_space *pspace)
1875{
1876 struct breakpoint *b, *b_temp;
876fa593 1877 struct bp_location *loc, **loc_temp;
6c95b8df
PA
1878
1879 /* Remove any breakpoint that was set through this program space. */
1880 ALL_BREAKPOINTS_SAFE (b, b_temp)
1881 {
1882 if (b->pspace == pspace)
1883 delete_breakpoint (b);
1884 }
1885
1886 /* Breakpoints set through other program spaces could have locations
1887 bound to PSPACE as well. Remove those. */
876fa593 1888 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
1889 {
1890 struct bp_location *tmp;
1891
1892 if (loc->pspace == pspace)
1893 {
2bdf28a0 1894 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
1895 if (loc->owner->loc == loc)
1896 loc->owner->loc = loc->next;
1897 else
1898 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1899 if (tmp->next == loc)
1900 {
1901 tmp->next = loc->next;
1902 break;
1903 }
1904 }
1905 }
1906
1907 /* Now update the global location list to permanently delete the
1908 removed locations above. */
1909 update_global_location_list (0);
1910}
1911
74960c60
VP
1912/* Make sure all breakpoints are inserted in inferior.
1913 Throws exception on any error.
1914 A breakpoint that is already inserted won't be inserted
1915 again, so calling this function twice is safe. */
1916void
1917insert_breakpoints (void)
1918{
1919 struct breakpoint *bpt;
1920
1921 ALL_BREAKPOINTS (bpt)
1922 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
1923 {
1924 struct watchpoint *w = (struct watchpoint *) bpt;
1925
1926 update_watchpoint (w, 0 /* don't reparse. */);
1927 }
74960c60 1928
b60e7edf 1929 update_global_location_list (1);
74960c60 1930
c35b1492
PA
1931 /* update_global_location_list does not insert breakpoints when
1932 always_inserted_mode is not enabled. Explicitly insert them
1933 now. */
1934 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1935 insert_breakpoint_locations ();
1936}
1937
c30eee59 1938/* Used when starting or continuing the program. */
c906108c 1939
74960c60
VP
1940static void
1941insert_breakpoint_locations (void)
c906108c 1942{
a5606eee 1943 struct breakpoint *bpt;
35df4500 1944 struct bp_location *bl, **blp_tmp;
e236ba44 1945 int error = 0;
c906108c 1946 int val = 0;
3fbb6ffa 1947 int disabled_breaks = 0;
81d0cc19 1948 int hw_breakpoint_error = 0;
c906108c 1949
81d0cc19 1950 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1951 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1952
81d0cc19
GS
1953 /* Explicitly mark the warning -- this will only be printed if
1954 there was an error. */
1955 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
1956
1957 save_current_space_and_thread ();
1958
35df4500 1959 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 1960 {
35df4500 1961 if (!should_be_inserted (bl) || bl->inserted)
879bfdc2
DJ
1962 continue;
1963
4a64f543
MS
1964 /* There is no point inserting thread-specific breakpoints if
1965 the thread no longer exists. ALL_BP_LOCATIONS bp_location
1966 has BL->OWNER always non-NULL. */
35df4500
TJB
1967 if (bl->owner->thread != -1
1968 && !valid_thread_id (bl->owner->thread))
f365de73
AS
1969 continue;
1970
35df4500 1971 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
1972
1973 /* For targets that support global breakpoints, there's no need
1974 to select an inferior to insert breakpoint to. In fact, even
1975 if we aren't attached to any process yet, we should still
1976 insert breakpoints. */
1977 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1978 && ptid_equal (inferior_ptid, null_ptid))
1979 continue;
1980
3fbb6ffa
TJB
1981 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
1982 &hw_breakpoint_error);
879bfdc2 1983 if (val)
e236ba44 1984 error = val;
879bfdc2 1985 }
c906108c 1986
4a64f543
MS
1987 /* If we failed to insert all locations of a watchpoint, remove
1988 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
1989 ALL_BREAKPOINTS (bpt)
1990 {
1991 int some_failed = 0;
1992 struct bp_location *loc;
1993
1994 if (!is_hardware_watchpoint (bpt))
1995 continue;
1996
d6b74ac4 1997 if (!breakpoint_enabled (bpt))
a5606eee 1998 continue;
74960c60
VP
1999
2000 if (bpt->disposition == disp_del_at_next_stop)
2001 continue;
a5606eee
VP
2002
2003 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2004 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2005 {
2006 some_failed = 1;
2007 break;
2008 }
2009 if (some_failed)
2010 {
2011 for (loc = bpt->loc; loc; loc = loc->next)
2012 if (loc->inserted)
2013 remove_breakpoint (loc, mark_uninserted);
2014
2015 hw_breakpoint_error = 1;
2016 fprintf_unfiltered (tmp_error_stream,
2017 "Could not insert hardware watchpoint %d.\n",
2018 bpt->number);
2019 error = -1;
2020 }
2021 }
2022
e236ba44 2023 if (error)
81d0cc19
GS
2024 {
2025 /* If a hardware breakpoint or watchpoint was inserted, add a
2026 message about possibly exhausted resources. */
879bfdc2 2027 if (hw_breakpoint_error)
81d0cc19 2028 {
c6510018
MS
2029 fprintf_unfiltered (tmp_error_stream,
2030 "Could not insert hardware breakpoints:\n\
2031You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2032 }
81d0cc19
GS
2033 target_terminal_ours_for_output ();
2034 error_stream (tmp_error_stream);
2035 }
f7545552
TT
2036
2037 do_cleanups (cleanups);
c906108c
SS
2038}
2039
c30eee59
TJB
2040/* Used when the program stops.
2041 Returns zero if successful, or non-zero if there was a problem
2042 removing a breakpoint location. */
2043
c906108c 2044int
fba45db2 2045remove_breakpoints (void)
c906108c 2046{
35df4500 2047 struct bp_location *bl, **blp_tmp;
3a1bae8e 2048 int val = 0;
c906108c 2049
35df4500 2050 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2051 {
35df4500
TJB
2052 if (bl->inserted)
2053 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2054 }
3a1bae8e 2055 return val;
c906108c
SS
2056}
2057
6c95b8df
PA
2058/* Remove breakpoints of process PID. */
2059
2060int
2061remove_breakpoints_pid (int pid)
2062{
35df4500 2063 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2064 int val;
2065 struct inferior *inf = find_inferior_pid (pid);
2066
35df4500 2067 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2068 {
35df4500 2069 if (bl->pspace != inf->pspace)
6c95b8df
PA
2070 continue;
2071
35df4500 2072 if (bl->inserted)
6c95b8df 2073 {
35df4500 2074 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2075 if (val != 0)
2076 return val;
2077 }
2078 }
2079 return 0;
2080}
2081
c906108c 2082int
fba45db2 2083reattach_breakpoints (int pid)
c906108c 2084{
6c95b8df 2085 struct cleanup *old_chain;
35df4500 2086 struct bp_location *bl, **blp_tmp;
c906108c 2087 int val;
86b887df 2088 struct ui_file *tmp_error_stream;
3fbb6ffa 2089 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2090 struct inferior *inf;
2091 struct thread_info *tp;
2092
2093 tp = any_live_thread_of_process (pid);
2094 if (tp == NULL)
2095 return 1;
2096
2097 inf = find_inferior_pid (pid);
2098 old_chain = save_inferior_ptid ();
2099
2100 inferior_ptid = tp->ptid;
a4954f26 2101
86b887df 2102 tmp_error_stream = mem_fileopen ();
a4954f26 2103 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2104
35df4500 2105 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2106 {
35df4500 2107 if (bl->pspace != inf->pspace)
6c95b8df
PA
2108 continue;
2109
35df4500 2110 if (bl->inserted)
c5aa993b 2111 {
35df4500 2112 bl->inserted = 0;
3fbb6ffa 2113 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2114 if (val != 0)
2115 {
ce696e05 2116 do_cleanups (old_chain);
c5aa993b
JM
2117 return val;
2118 }
2119 }
2120 }
ce696e05 2121 do_cleanups (old_chain);
c906108c
SS
2122 return 0;
2123}
2124
e58b0e63
PA
2125static int internal_breakpoint_number = -1;
2126
84f4c1fe
PM
2127/* Set the breakpoint number of B, depending on the value of INTERNAL.
2128 If INTERNAL is non-zero, the breakpoint number will be populated
2129 from internal_breakpoint_number and that variable decremented.
e5dd4106 2130 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2131 breakpoint_count and that value incremented. Internal breakpoints
2132 do not set the internal var bpnum. */
2133static void
2134set_breakpoint_number (int internal, struct breakpoint *b)
2135{
2136 if (internal)
2137 b->number = internal_breakpoint_number--;
2138 else
2139 {
2140 set_breakpoint_count (breakpoint_count + 1);
2141 b->number = breakpoint_count;
2142 }
2143}
2144
e62c965a 2145static struct breakpoint *
a6d9a66e 2146create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2147 CORE_ADDR address, enum bptype type,
c0a91b2b 2148 const struct breakpoint_ops *ops)
e62c965a 2149{
e62c965a
PP
2150 struct symtab_and_line sal;
2151 struct breakpoint *b;
2152
4a64f543 2153 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2154
2155 sal.pc = address;
2156 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2157 sal.pspace = current_program_space;
e62c965a 2158
06edf0c0 2159 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2160 b->number = internal_breakpoint_number--;
2161 b->disposition = disp_donttouch;
2162
2163 return b;
2164}
2165
17450429
PP
2166static const char *const longjmp_names[] =
2167 {
2168 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2169 };
2170#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2171
2172/* Per-objfile data private to breakpoint.c. */
2173struct breakpoint_objfile_data
2174{
2175 /* Minimal symbol for "_ovly_debug_event" (if any). */
2176 struct minimal_symbol *overlay_msym;
2177
2178 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2179 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2180
2181 /* Minimal symbol for "std::terminate()" (if any). */
2182 struct minimal_symbol *terminate_msym;
2183
2184 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2185 struct minimal_symbol *exception_msym;
2186};
2187
2188static const struct objfile_data *breakpoint_objfile_key;
2189
2190/* Minimal symbol not found sentinel. */
2191static struct minimal_symbol msym_not_found;
2192
2193/* Returns TRUE if MSYM point to the "not found" sentinel. */
2194
2195static int
2196msym_not_found_p (const struct minimal_symbol *msym)
2197{
2198 return msym == &msym_not_found;
2199}
2200
2201/* Return per-objfile data needed by breakpoint.c.
2202 Allocate the data if necessary. */
2203
2204static struct breakpoint_objfile_data *
2205get_breakpoint_objfile_data (struct objfile *objfile)
2206{
2207 struct breakpoint_objfile_data *bp_objfile_data;
2208
2209 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2210 if (bp_objfile_data == NULL)
2211 {
2212 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2213 sizeof (*bp_objfile_data));
2214
2215 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2216 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2217 }
2218 return bp_objfile_data;
2219}
2220
e62c965a 2221static void
af02033e 2222create_overlay_event_breakpoint (void)
e62c965a 2223{
69de3c6a 2224 struct objfile *objfile;
af02033e 2225 const char *const func_name = "_ovly_debug_event";
e62c965a 2226
69de3c6a
PP
2227 ALL_OBJFILES (objfile)
2228 {
2229 struct breakpoint *b;
17450429
PP
2230 struct breakpoint_objfile_data *bp_objfile_data;
2231 CORE_ADDR addr;
69de3c6a 2232
17450429
PP
2233 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2234
2235 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2236 continue;
2237
2238 if (bp_objfile_data->overlay_msym == NULL)
2239 {
2240 struct minimal_symbol *m;
2241
2242 m = lookup_minimal_symbol_text (func_name, objfile);
2243 if (m == NULL)
2244 {
2245 /* Avoid future lookups in this objfile. */
2246 bp_objfile_data->overlay_msym = &msym_not_found;
2247 continue;
2248 }
2249 bp_objfile_data->overlay_msym = m;
2250 }
e62c965a 2251
17450429
PP
2252 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2253 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2254 bp_overlay_event,
2255 &internal_breakpoint_ops);
69de3c6a 2256 b->addr_string = xstrdup (func_name);
e62c965a 2257
69de3c6a
PP
2258 if (overlay_debugging == ovly_auto)
2259 {
2260 b->enable_state = bp_enabled;
2261 overlay_events_enabled = 1;
2262 }
2263 else
2264 {
2265 b->enable_state = bp_disabled;
2266 overlay_events_enabled = 0;
2267 }
e62c965a
PP
2268 }
2269 update_global_location_list (1);
2270}
2271
0fd8e87f 2272static void
af02033e 2273create_longjmp_master_breakpoint (void)
0fd8e87f 2274{
6c95b8df 2275 struct program_space *pspace;
6c95b8df
PA
2276 struct cleanup *old_chain;
2277
2278 old_chain = save_current_program_space ();
0fd8e87f 2279
6c95b8df 2280 ALL_PSPACES (pspace)
af02033e
PP
2281 {
2282 struct objfile *objfile;
2283
2284 set_current_program_space (pspace);
2285
2286 ALL_OBJFILES (objfile)
0fd8e87f 2287 {
af02033e
PP
2288 int i;
2289 struct gdbarch *gdbarch;
17450429 2290 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2291
af02033e
PP
2292 gdbarch = get_objfile_arch (objfile);
2293 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2294 continue;
2295
17450429
PP
2296 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2297
2298 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2299 {
2300 struct breakpoint *b;
af02033e 2301 const char *func_name;
17450429 2302 CORE_ADDR addr;
6c95b8df 2303
17450429 2304 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2305 continue;
0fd8e87f 2306
17450429
PP
2307 func_name = longjmp_names[i];
2308 if (bp_objfile_data->longjmp_msym[i] == NULL)
2309 {
2310 struct minimal_symbol *m;
2311
2312 m = lookup_minimal_symbol_text (func_name, objfile);
2313 if (m == NULL)
2314 {
2315 /* Prevent future lookups in this objfile. */
2316 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2317 continue;
2318 }
2319 bp_objfile_data->longjmp_msym[i] = m;
2320 }
2321
2322 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2323 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2324 &internal_breakpoint_ops);
af02033e
PP
2325 b->addr_string = xstrdup (func_name);
2326 b->enable_state = bp_disabled;
2327 }
0fd8e87f 2328 }
af02033e 2329 }
0fd8e87f 2330 update_global_location_list (1);
6c95b8df
PA
2331
2332 do_cleanups (old_chain);
0fd8e87f
UW
2333}
2334
af02033e 2335/* Create a master std::terminate breakpoint. */
aa7d318d 2336static void
af02033e 2337create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2338{
2339 struct program_space *pspace;
aa7d318d 2340 struct cleanup *old_chain;
af02033e 2341 const char *const func_name = "std::terminate()";
aa7d318d
TT
2342
2343 old_chain = save_current_program_space ();
2344
2345 ALL_PSPACES (pspace)
17450429
PP
2346 {
2347 struct objfile *objfile;
2348 CORE_ADDR addr;
2349
2350 set_current_program_space (pspace);
2351
aa7d318d
TT
2352 ALL_OBJFILES (objfile)
2353 {
2354 struct breakpoint *b;
17450429 2355 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2356
17450429 2357 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2358
17450429
PP
2359 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2360 continue;
2361
2362 if (bp_objfile_data->terminate_msym == NULL)
2363 {
2364 struct minimal_symbol *m;
2365
2366 m = lookup_minimal_symbol (func_name, NULL, objfile);
2367 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2368 && MSYMBOL_TYPE (m) != mst_file_text))
2369 {
2370 /* Prevent future lookups in this objfile. */
2371 bp_objfile_data->terminate_msym = &msym_not_found;
2372 continue;
2373 }
2374 bp_objfile_data->terminate_msym = m;
2375 }
aa7d318d 2376
17450429
PP
2377 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
2378 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2379 bp_std_terminate_master,
2380 &internal_breakpoint_ops);
aa7d318d
TT
2381 b->addr_string = xstrdup (func_name);
2382 b->enable_state = bp_disabled;
2383 }
17450429
PP
2384 }
2385
aa7d318d
TT
2386 update_global_location_list (1);
2387
2388 do_cleanups (old_chain);
2389}
2390
186c406b
TT
2391/* Install a master breakpoint on the unwinder's debug hook. */
2392
2393void
2394create_exception_master_breakpoint (void)
2395{
2396 struct objfile *objfile;
17450429 2397 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
2398
2399 ALL_OBJFILES (objfile)
2400 {
17450429
PP
2401 struct breakpoint *b;
2402 struct gdbarch *gdbarch;
2403 struct breakpoint_objfile_data *bp_objfile_data;
2404 CORE_ADDR addr;
2405
2406 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2407
2408 if (msym_not_found_p (bp_objfile_data->exception_msym))
2409 continue;
2410
2411 gdbarch = get_objfile_arch (objfile);
186c406b 2412
17450429 2413 if (bp_objfile_data->exception_msym == NULL)
186c406b 2414 {
17450429 2415 struct minimal_symbol *debug_hook;
186c406b 2416
17450429
PP
2417 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
2418 if (debug_hook == NULL)
2419 {
2420 bp_objfile_data->exception_msym = &msym_not_found;
2421 continue;
2422 }
2423
2424 bp_objfile_data->exception_msym = debug_hook;
186c406b 2425 }
17450429
PP
2426
2427 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
2428 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
2429 &current_target);
06edf0c0
PA
2430 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
2431 &internal_breakpoint_ops);
17450429
PP
2432 b->addr_string = xstrdup (func_name);
2433 b->enable_state = bp_disabled;
186c406b
TT
2434 }
2435
2436 update_global_location_list (1);
2437}
2438
c906108c 2439void
fba45db2 2440update_breakpoints_after_exec (void)
c906108c 2441{
35df4500 2442 struct breakpoint *b, *b_tmp;
876fa593 2443 struct bp_location *bploc, **bplocp_tmp;
c906108c 2444
25b22b0a
PA
2445 /* We're about to delete breakpoints from GDB's lists. If the
2446 INSERTED flag is true, GDB will try to lift the breakpoints by
2447 writing the breakpoints' "shadow contents" back into memory. The
2448 "shadow contents" are NOT valid after an exec, so GDB should not
2449 do that. Instead, the target is responsible from marking
2450 breakpoints out as soon as it detects an exec. We don't do that
2451 here instead, because there may be other attempts to delete
2452 breakpoints after detecting an exec and before reaching here. */
876fa593 2453 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
2454 if (bploc->pspace == current_program_space)
2455 gdb_assert (!bploc->inserted);
c906108c 2456
35df4500 2457 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2458 {
6c95b8df
PA
2459 if (b->pspace != current_program_space)
2460 continue;
2461
4a64f543 2462 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
2463 if (b->type == bp_shlib_event)
2464 {
2465 delete_breakpoint (b);
2466 continue;
2467 }
c906108c 2468
4a64f543 2469 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
2470 if (b->type == bp_jit_event)
2471 {
2472 delete_breakpoint (b);
2473 continue;
2474 }
2475
1900040c 2476 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
2477 as must overlay event and longjmp master breakpoints. */
2478 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
2479 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
2480 || b->type == bp_exception_master)
c4093a6a
JM
2481 {
2482 delete_breakpoint (b);
2483 continue;
2484 }
2485
4a64f543 2486 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 2487 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
2488 {
2489 delete_breakpoint (b);
2490 continue;
2491 }
2492
611c83ae
PA
2493 /* Longjmp and longjmp-resume breakpoints are also meaningless
2494 after an exec. */
186c406b
TT
2495 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
2496 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
2497 {
2498 delete_breakpoint (b);
2499 continue;
2500 }
2501
ce78b96d
JB
2502 if (b->type == bp_catchpoint)
2503 {
2504 /* For now, none of the bp_catchpoint breakpoints need to
2505 do anything at this point. In the future, if some of
2506 the catchpoints need to something, we will need to add
2507 a new method, and call this method from here. */
2508 continue;
2509 }
2510
c5aa993b
JM
2511 /* bp_finish is a special case. The only way we ought to be able
2512 to see one of these when an exec() has happened, is if the user
2513 caught a vfork, and then said "finish". Ordinarily a finish just
2514 carries them to the call-site of the current callee, by setting
2515 a temporary bp there and resuming. But in this case, the finish
2516 will carry them entirely through the vfork & exec.
2517
2518 We don't want to allow a bp_finish to remain inserted now. But
2519 we can't safely delete it, 'cause finish_command has a handle to
2520 the bp on a bpstat, and will later want to delete it. There's a
2521 chance (and I've seen it happen) that if we delete the bp_finish
2522 here, that its storage will get reused by the time finish_command
2523 gets 'round to deleting the "use to be a bp_finish" breakpoint.
2524 We really must allow finish_command to delete a bp_finish.
2525
e5dd4106 2526 In the absence of a general solution for the "how do we know
53a5351d
JM
2527 it's safe to delete something others may have handles to?"
2528 problem, what we'll do here is just uninsert the bp_finish, and
2529 let finish_command delete it.
2530
2531 (We know the bp_finish is "doomed" in the sense that it's
2532 momentary, and will be deleted as soon as finish_command sees
2533 the inferior stopped. So it doesn't matter that the bp's
2534 address is probably bogus in the new a.out, unlike e.g., the
2535 solib breakpoints.) */
c5aa993b 2536
c5aa993b
JM
2537 if (b->type == bp_finish)
2538 {
2539 continue;
2540 }
2541
2542 /* Without a symbolic address, we have little hope of the
2543 pre-exec() address meaning the same thing in the post-exec()
4a64f543 2544 a.out. */
c5aa993b
JM
2545 if (b->addr_string == NULL)
2546 {
2547 delete_breakpoint (b);
2548 continue;
2549 }
c5aa993b 2550 }
1900040c 2551 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
2552 create_overlay_event_breakpoint ();
2553 create_longjmp_master_breakpoint ();
2554 create_std_terminate_master_breakpoint ();
186c406b 2555 create_exception_master_breakpoint ();
c906108c
SS
2556}
2557
2558int
fba45db2 2559detach_breakpoints (int pid)
c906108c 2560{
35df4500 2561 struct bp_location *bl, **blp_tmp;
3a1bae8e 2562 int val = 0;
ce696e05 2563 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 2564 struct inferior *inf = current_inferior ();
c5aa993b 2565
39f77062 2566 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 2567 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 2568
6c95b8df 2569 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 2570 inferior_ptid = pid_to_ptid (pid);
35df4500 2571 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2572 {
35df4500 2573 if (bl->pspace != inf->pspace)
6c95b8df
PA
2574 continue;
2575
35df4500
TJB
2576 if (bl->inserted)
2577 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 2578 }
d03285ec
UW
2579
2580 /* Detach single-step breakpoints as well. */
2581 detach_single_step_breakpoints ();
2582
ce696e05 2583 do_cleanups (old_chain);
3a1bae8e 2584 return val;
c906108c
SS
2585}
2586
35df4500 2587/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
2588 Note that this is used to detach breakpoints from a child fork.
2589 When we get here, the child isn't in the inferior list, and neither
2590 do we have objects to represent its address space --- we should
35df4500 2591 *not* look at bl->pspace->aspace here. */
6c95b8df 2592
c906108c 2593static int
35df4500 2594remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
2595{
2596 int val;
c5aa993b 2597
35df4500
TJB
2598 /* BL is never in moribund_locations by our callers. */
2599 gdb_assert (bl->owner != NULL);
2bdf28a0 2600
35df4500 2601 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
2602 /* Permanent breakpoints cannot be inserted or removed. */
2603 return 0;
2604
74960c60
VP
2605 /* The type of none suggests that owner is actually deleted.
2606 This should not ever happen. */
35df4500 2607 gdb_assert (bl->owner->type != bp_none);
0bde7532 2608
35df4500
TJB
2609 if (bl->loc_type == bp_loc_software_breakpoint
2610 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 2611 {
c02f5703
MS
2612 /* "Normal" instruction breakpoint: either the standard
2613 trap-instruction bp (bp_breakpoint), or a
2614 bp_hardware_breakpoint. */
2615
2616 /* First check to see if we have to handle an overlay. */
2617 if (overlay_debugging == ovly_off
35df4500
TJB
2618 || bl->section == NULL
2619 || !(section_is_overlay (bl->section)))
c02f5703
MS
2620 {
2621 /* No overlay handling: just remove the breakpoint. */
348d480f 2622 val = bl->owner->ops->remove_location (bl);
c02f5703 2623 }
c906108c
SS
2624 else
2625 {
4a64f543 2626 /* This breakpoint is in an overlay section.
c02f5703
MS
2627 Did we set a breakpoint at the LMA? */
2628 if (!overlay_events_enabled)
2629 {
2630 /* Yes -- overlay event support is not active, so we
2631 should have set a breakpoint at the LMA. Remove it.
2632 */
c02f5703
MS
2633 /* Ignore any failures: if the LMA is in ROM, we will
2634 have already warned when we failed to insert it. */
35df4500
TJB
2635 if (bl->loc_type == bp_loc_hardware_breakpoint)
2636 target_remove_hw_breakpoint (bl->gdbarch,
2637 &bl->overlay_target_info);
c02f5703 2638 else
35df4500
TJB
2639 target_remove_breakpoint (bl->gdbarch,
2640 &bl->overlay_target_info);
c02f5703
MS
2641 }
2642 /* Did we set a breakpoint at the VMA?
2643 If so, we will have marked the breakpoint 'inserted'. */
35df4500 2644 if (bl->inserted)
c906108c 2645 {
c02f5703
MS
2646 /* Yes -- remove it. Previously we did not bother to
2647 remove the breakpoint if the section had been
2648 unmapped, but let's not rely on that being safe. We
2649 don't know what the overlay manager might do. */
aa67235e
UW
2650
2651 /* However, we should remove *software* breakpoints only
2652 if the section is still mapped, or else we overwrite
2653 wrong code with the saved shadow contents. */
348d480f
PA
2654 if (bl->loc_type == bp_loc_hardware_breakpoint
2655 || section_is_mapped (bl->section))
2656 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
2657 else
2658 val = 0;
c906108c 2659 }
c02f5703
MS
2660 else
2661 {
2662 /* No -- not inserted, so no need to remove. No error. */
2663 val = 0;
2664 }
c906108c 2665 }
879d1e6b
UW
2666
2667 /* In some cases, we might not be able to remove a breakpoint
2668 in a shared library that has already been removed, but we
2669 have not yet processed the shlib unload event. */
35df4500 2670 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
2671 val = 0;
2672
c906108c
SS
2673 if (val)
2674 return val;
35df4500 2675 bl->inserted = (is == mark_inserted);
c906108c 2676 }
35df4500 2677 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 2678 {
77b06cd7
TJB
2679 gdb_assert (bl->owner->ops != NULL
2680 && bl->owner->ops->remove_location != NULL);
2681
35df4500 2682 bl->inserted = (is == mark_inserted);
77b06cd7 2683 bl->owner->ops->remove_location (bl);
2e70b7b9 2684
c906108c 2685 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 2686 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 2687 warning (_("Could not remove hardware watchpoint %d."),
35df4500 2688 bl->owner->number);
c906108c 2689 }
35df4500
TJB
2690 else if (bl->owner->type == bp_catchpoint
2691 && breakpoint_enabled (bl->owner)
2692 && !bl->duplicate)
ce78b96d 2693 {
77b06cd7
TJB
2694 gdb_assert (bl->owner->ops != NULL
2695 && bl->owner->ops->remove_location != NULL);
ce78b96d 2696
77b06cd7 2697 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
2698 if (val)
2699 return val;
77b06cd7 2700
35df4500 2701 bl->inserted = (is == mark_inserted);
ce78b96d 2702 }
c906108c
SS
2703
2704 return 0;
2705}
2706
6c95b8df 2707static int
35df4500 2708remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
2709{
2710 int ret;
2711 struct cleanup *old_chain;
2712
35df4500
TJB
2713 /* BL is never in moribund_locations by our callers. */
2714 gdb_assert (bl->owner != NULL);
2bdf28a0 2715
35df4500 2716 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
2717 /* Permanent breakpoints cannot be inserted or removed. */
2718 return 0;
2719
2720 /* The type of none suggests that owner is actually deleted.
2721 This should not ever happen. */
35df4500 2722 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
2723
2724 old_chain = save_current_space_and_thread ();
2725
35df4500 2726 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 2727
35df4500 2728 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
2729
2730 do_cleanups (old_chain);
2731 return ret;
2732}
2733
c906108c
SS
2734/* Clear the "inserted" flag in all breakpoints. */
2735
25b22b0a 2736void
fba45db2 2737mark_breakpoints_out (void)
c906108c 2738{
35df4500 2739 struct bp_location *bl, **blp_tmp;
c906108c 2740
35df4500
TJB
2741 ALL_BP_LOCATIONS (bl, blp_tmp)
2742 if (bl->pspace == current_program_space)
2743 bl->inserted = 0;
c906108c
SS
2744}
2745
53a5351d
JM
2746/* Clear the "inserted" flag in all breakpoints and delete any
2747 breakpoints which should go away between runs of the program.
c906108c
SS
2748
2749 Plus other such housekeeping that has to be done for breakpoints
2750 between runs.
2751
53a5351d
JM
2752 Note: this function gets called at the end of a run (by
2753 generic_mourn_inferior) and when a run begins (by
4a64f543 2754 init_wait_for_inferior). */
c906108c
SS
2755
2756
2757
2758void
fba45db2 2759breakpoint_init_inferior (enum inf_context context)
c906108c 2760{
35df4500
TJB
2761 struct breakpoint *b, *b_tmp;
2762 struct bp_location *bl, **blp_tmp;
1c5cfe86 2763 int ix;
6c95b8df 2764 struct program_space *pspace = current_program_space;
c906108c 2765
50c71eaf
PA
2766 /* If breakpoint locations are shared across processes, then there's
2767 nothing to do. */
2567c7d9 2768 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
2769 return;
2770
35df4500 2771 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2772 {
35df4500
TJB
2773 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
2774 if (bl->pspace == pspace
2775 && bl->owner->enable_state != bp_permanent)
2776 bl->inserted = 0;
6c95b8df 2777 }
075f6582 2778
35df4500 2779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 2780 {
6c95b8df
PA
2781 if (b->loc && b->loc->pspace != pspace)
2782 continue;
2783
c5aa993b
JM
2784 switch (b->type)
2785 {
2786 case bp_call_dummy:
c906108c 2787
c5aa993b 2788 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
2789 cause problems when the inferior is rerun, so we better get
2790 rid of it. */
2791
2792 case bp_watchpoint_scope:
2793
2794 /* Also get rid of scope breakpoints. */
2795
2796 case bp_shlib_event:
2797
2798 /* Also remove solib event breakpoints. Their addresses may
2799 have changed since the last time we ran the program.
2800 Actually we may now be debugging against different target;
2801 and so the solib backend that installed this breakpoint may
2802 not be used in by the target. E.g.,
2803
2804 (gdb) file prog-linux
2805 (gdb) run # native linux target
2806 ...
2807 (gdb) kill
2808 (gdb) file prog-win.exe
2809 (gdb) tar rem :9999 # remote Windows gdbserver.
2810 */
c906108c 2811
c5aa993b
JM
2812 delete_breakpoint (b);
2813 break;
c906108c 2814
c5aa993b
JM
2815 case bp_watchpoint:
2816 case bp_hardware_watchpoint:
2817 case bp_read_watchpoint:
2818 case bp_access_watchpoint:
3a5c3e22
PA
2819 {
2820 struct watchpoint *w = (struct watchpoint *) b;
c906108c 2821
3a5c3e22
PA
2822 /* Likewise for watchpoints on local expressions. */
2823 if (w->exp_valid_block != NULL)
2824 delete_breakpoint (b);
2825 else if (context == inf_starting)
2826 {
2827 /* Reset val field to force reread of starting value in
2828 insert_breakpoints. */
2829 if (w->val)
2830 value_free (w->val);
2831 w->val = NULL;
2832 w->val_valid = 0;
c860120c 2833 }
3a5c3e22 2834 }
c5aa993b
JM
2835 break;
2836 default:
c5aa993b
JM
2837 break;
2838 }
2839 }
1c5cfe86
PA
2840
2841 /* Get rid of the moribund locations. */
35df4500
TJB
2842 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
2843 decref_bp_location (&bl);
1c5cfe86 2844 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
2845}
2846
6c95b8df
PA
2847/* These functions concern about actual breakpoints inserted in the
2848 target --- to e.g. check if we need to do decr_pc adjustment or if
2849 we need to hop over the bkpt --- so we check for address space
2850 match, not program space. */
2851
c2c6d25f
JM
2852/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2853 exists at PC. It returns ordinary_breakpoint_here if it's an
2854 ordinary breakpoint, or permanent_breakpoint_here if it's a
2855 permanent breakpoint.
2856 - When continuing from a location with an ordinary breakpoint, we
2857 actually single step once before calling insert_breakpoints.
e5dd4106 2858 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
2859 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2860 the target, to advance the PC past the breakpoint. */
c906108c 2861
c2c6d25f 2862enum breakpoint_here
6c95b8df 2863breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 2864{
35df4500 2865 struct bp_location *bl, **blp_tmp;
c2c6d25f 2866 int any_breakpoint_here = 0;
c906108c 2867
35df4500 2868 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 2869 {
35df4500
TJB
2870 if (bl->loc_type != bp_loc_software_breakpoint
2871 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2872 continue;
2873
f1310107 2874 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
2875 if ((breakpoint_enabled (bl->owner)
2876 || bl->owner->enable_state == bp_permanent)
f1310107 2877 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2878 {
2879 if (overlay_debugging
35df4500
TJB
2880 && section_is_overlay (bl->section)
2881 && !section_is_mapped (bl->section))
075f6582 2882 continue; /* unmapped overlay -- can't be a match */
35df4500 2883 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
2884 return permanent_breakpoint_here;
2885 else
2886 any_breakpoint_here = 1;
2887 }
2888 }
c906108c 2889
c2c6d25f 2890 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
2891}
2892
1c5cfe86
PA
2893/* Return true if there's a moribund breakpoint at PC. */
2894
2895int
6c95b8df 2896moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
2897{
2898 struct bp_location *loc;
2899 int ix;
2900
2901 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 2902 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
2903 return 1;
2904
2905 return 0;
2906}
c2c6d25f 2907
c36b740a 2908/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
2909 inserted using regular breakpoint_chain / bp_location array
2910 mechanism. This does not check for single-step breakpoints, which
2911 are inserted and removed using direct target manipulation. */
c906108c
SS
2912
2913int
4a64f543
MS
2914regular_breakpoint_inserted_here_p (struct address_space *aspace,
2915 CORE_ADDR pc)
c906108c 2916{
35df4500 2917 struct bp_location *bl, **blp_tmp;
c906108c 2918
35df4500 2919 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2920 {
35df4500
TJB
2921 if (bl->loc_type != bp_loc_software_breakpoint
2922 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
2923 continue;
2924
35df4500 2925 if (bl->inserted
f1310107 2926 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
2927 {
2928 if (overlay_debugging
35df4500
TJB
2929 && section_is_overlay (bl->section)
2930 && !section_is_mapped (bl->section))
075f6582
DJ
2931 continue; /* unmapped overlay -- can't be a match */
2932 else
2933 return 1;
2934 }
c5aa993b 2935 }
c36b740a
VP
2936 return 0;
2937}
2938
2939/* Returns non-zero iff there's either regular breakpoint
2940 or a single step breakpoint inserted at PC. */
2941
2942int
6c95b8df 2943breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 2944{
6c95b8df 2945 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 2946 return 1;
c906108c 2947
6c95b8df 2948 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2949 return 1;
2950
c906108c
SS
2951 return 0;
2952}
2953
4fa8626c
DJ
2954/* This function returns non-zero iff there is a software breakpoint
2955 inserted at PC. */
2956
2957int
3e43a32a
MS
2958software_breakpoint_inserted_here_p (struct address_space *aspace,
2959 CORE_ADDR pc)
4fa8626c 2960{
35df4500 2961 struct bp_location *bl, **blp_tmp;
4fa8626c 2962
35df4500 2963 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 2964 {
35df4500 2965 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
2966 continue;
2967
35df4500
TJB
2968 if (bl->inserted
2969 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 2970 aspace, pc))
4fa8626c
DJ
2971 {
2972 if (overlay_debugging
35df4500
TJB
2973 && section_is_overlay (bl->section)
2974 && !section_is_mapped (bl->section))
4fa8626c
DJ
2975 continue; /* unmapped overlay -- can't be a match */
2976 else
2977 return 1;
2978 }
2979 }
2980
1aafd4da 2981 /* Also check for software single-step breakpoints. */
6c95b8df 2982 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
2983 return 1;
2984
4fa8626c
DJ
2985 return 0;
2986}
2987
9093389c
PA
2988int
2989hardware_watchpoint_inserted_in_range (struct address_space *aspace,
2990 CORE_ADDR addr, ULONGEST len)
2991{
2992 struct breakpoint *bpt;
2993
2994 ALL_BREAKPOINTS (bpt)
2995 {
2996 struct bp_location *loc;
2997
2998 if (bpt->type != bp_hardware_watchpoint
2999 && bpt->type != bp_access_watchpoint)
3000 continue;
3001
3002 if (!breakpoint_enabled (bpt))
3003 continue;
3004
3005 for (loc = bpt->loc; loc; loc = loc->next)
3006 if (loc->pspace->aspace == aspace && loc->inserted)
3007 {
3008 CORE_ADDR l, h;
3009
3010 /* Check for intersection. */
3011 l = max (loc->address, addr);
3012 h = min (loc->address + loc->length, addr + len);
3013 if (l < h)
3014 return 1;
3015 }
3016 }
3017 return 0;
3018}
3019
075f6582
DJ
3020/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3021 PC is valid for process/thread PTID. */
c906108c
SS
3022
3023int
6c95b8df
PA
3024breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3025 ptid_t ptid)
c906108c 3026{
35df4500 3027 struct bp_location *bl, **blp_tmp;
4a306c9a 3028 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3029 int thread = -1;
4a306c9a 3030 int task = 0;
a6f1cd96 3031
35df4500 3032 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3033 {
35df4500
TJB
3034 if (bl->loc_type != bp_loc_software_breakpoint
3035 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3036 continue;
3037
35df4500
TJB
3038 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3039 if (!breakpoint_enabled (bl->owner)
3040 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3041 continue;
3042
f1310107 3043 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3044 continue;
3045
35df4500 3046 if (bl->owner->thread != -1)
075f6582 3047 {
a6f1cd96
JB
3048 /* This is a thread-specific breakpoint. Check that ptid
3049 matches that thread. If thread hasn't been computed yet,
3050 it is now time to do so. */
3051 if (thread == -1)
3052 thread = pid_to_thread_id (ptid);
35df4500 3053 if (bl->owner->thread != thread)
a6f1cd96 3054 continue;
075f6582 3055 }
a6f1cd96 3056
35df4500 3057 if (bl->owner->task != 0)
4a306c9a
JB
3058 {
3059 /* This is a task-specific breakpoint. Check that ptid
3060 matches that task. If task hasn't been computed yet,
3061 it is now time to do so. */
3062 if (task == 0)
3063 task = ada_get_task_number (ptid);
35df4500 3064 if (bl->owner->task != task)
4a306c9a
JB
3065 continue;
3066 }
3067
a6f1cd96 3068 if (overlay_debugging
35df4500
TJB
3069 && section_is_overlay (bl->section)
3070 && !section_is_mapped (bl->section))
a6f1cd96
JB
3071 continue; /* unmapped overlay -- can't be a match */
3072
3073 return 1;
c5aa993b 3074 }
c906108c
SS
3075
3076 return 0;
3077}
c906108c 3078\f
c5aa993b 3079
c906108c
SS
3080/* bpstat stuff. External routines' interfaces are documented
3081 in breakpoint.h. */
3082
3083int
fba45db2 3084ep_is_catchpoint (struct breakpoint *ep)
c906108c 3085{
533be4dd 3086 return (ep->type == bp_catchpoint);
c906108c
SS
3087}
3088
f431efe5
PA
3089/* Frees any storage that is part of a bpstat. Does not walk the
3090 'next' chain. */
3091
3092static void
198757a8
VP
3093bpstat_free (bpstat bs)
3094{
3095 if (bs->old_val != NULL)
3096 value_free (bs->old_val);
9add0f1b 3097 decref_counted_command_line (&bs->commands);
f431efe5 3098 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3099 xfree (bs);
3100}
3101
c906108c
SS
3102/* Clear a bpstat so that it says we are not at any breakpoint.
3103 Also free any storage that is part of a bpstat. */
3104
3105void
fba45db2 3106bpstat_clear (bpstat *bsp)
c906108c
SS
3107{
3108 bpstat p;
3109 bpstat q;
3110
3111 if (bsp == 0)
3112 return;
3113 p = *bsp;
3114 while (p != NULL)
3115 {
3116 q = p->next;
198757a8 3117 bpstat_free (p);
c906108c
SS
3118 p = q;
3119 }
3120 *bsp = NULL;
3121}
3122
3123/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3124 is part of the bpstat is copied as well. */
3125
3126bpstat
fba45db2 3127bpstat_copy (bpstat bs)
c906108c
SS
3128{
3129 bpstat p = NULL;
3130 bpstat tmp;
3131 bpstat retval = NULL;
3132
3133 if (bs == NULL)
3134 return bs;
3135
3136 for (; bs != NULL; bs = bs->next)
3137 {
3138 tmp = (bpstat) xmalloc (sizeof (*tmp));
3139 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3140 incref_counted_command_line (tmp->commands);
f431efe5 3141 incref_bp_location (tmp->bp_location_at);
31cc81e9 3142 if (bs->old_val != NULL)
3c3185ac
JK
3143 {
3144 tmp->old_val = value_copy (bs->old_val);
3145 release_value (tmp->old_val);
3146 }
31cc81e9 3147
c906108c
SS
3148 if (p == NULL)
3149 /* This is the first thing in the chain. */
3150 retval = tmp;
3151 else
3152 p->next = tmp;
3153 p = tmp;
3154 }
3155 p->next = NULL;
3156 return retval;
3157}
3158
4a64f543 3159/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3160
3161bpstat
fba45db2 3162bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3163{
c5aa993b
JM
3164 if (bsp == NULL)
3165 return NULL;
c906108c 3166
c5aa993b
JM
3167 for (; bsp != NULL; bsp = bsp->next)
3168 {
f431efe5 3169 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3170 return bsp;
3171 }
c906108c
SS
3172 return NULL;
3173}
3174
4a64f543
MS
3175/* Put in *NUM the breakpoint number of the first breakpoint we are
3176 stopped at. *BSP upon return is a bpstat which points to the
3177 remaining breakpoints stopped at (but which is not guaranteed to be
3178 good for anything but further calls to bpstat_num).
3179
8671a17b
PA
3180 Return 0 if passed a bpstat which does not indicate any breakpoints.
3181 Return -1 if stopped at a breakpoint that has been deleted since
3182 we set it.
3183 Return 1 otherwise. */
c906108c
SS
3184
3185int
8671a17b 3186bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3187{
3188 struct breakpoint *b;
3189
3190 if ((*bsp) == NULL)
3191 return 0; /* No more breakpoint values */
8671a17b 3192
4a64f543
MS
3193 /* We assume we'll never have several bpstats that correspond to a
3194 single breakpoint -- otherwise, this function might return the
3195 same number more than once and this will look ugly. */
f431efe5 3196 b = (*bsp)->breakpoint_at;
8671a17b
PA
3197 *bsp = (*bsp)->next;
3198 if (b == NULL)
3199 return -1; /* breakpoint that's been deleted since */
3200
3201 *num = b->number; /* We have its number */
3202 return 1;
c906108c
SS
3203}
3204
e93ca019 3205/* See breakpoint.h. */
c906108c
SS
3206
3207void
e93ca019 3208bpstat_clear_actions (void)
c906108c 3209{
e93ca019
JK
3210 struct thread_info *tp;
3211 bpstat bs;
3212
3213 if (ptid_equal (inferior_ptid, null_ptid))
3214 return;
3215
3216 tp = find_thread_ptid (inferior_ptid);
3217 if (tp == NULL)
3218 return;
3219
3220 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3221 {
9add0f1b 3222 decref_counted_command_line (&bs->commands);
abf85f46 3223
c906108c
SS
3224 if (bs->old_val != NULL)
3225 {
3226 value_free (bs->old_val);
3227 bs->old_val = NULL;
3228 }
3229 }
3230}
3231
f3b1572e
PA
3232/* Called when a command is about to proceed the inferior. */
3233
3234static void
3235breakpoint_about_to_proceed (void)
3236{
3237 if (!ptid_equal (inferior_ptid, null_ptid))
3238 {
3239 struct thread_info *tp = inferior_thread ();
3240
3241 /* Allow inferior function calls in breakpoint commands to not
3242 interrupt the command list. When the call finishes
3243 successfully, the inferior will be standing at the same
3244 breakpoint as if nothing happened. */
16c381f0 3245 if (tp->control.in_infcall)
f3b1572e
PA
3246 return;
3247 }
3248
3249 breakpoint_proceeded = 1;
3250}
3251
4a64f543
MS
3252/* Stub for cleaning up our state if we error-out of a breakpoint
3253 command. */
c906108c 3254static void
4efb68b1 3255cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3256{
3257 executing_breakpoint_commands = 0;
3258}
3259
abf85f46
JK
3260/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3261 or its equivalent. */
3262
3263static int
3264command_line_is_silent (struct command_line *cmd)
3265{
3266 return cmd && (strcmp ("silent", cmd->line) == 0
3267 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3268}
3269
4a64f543
MS
3270/* Execute all the commands associated with all the breakpoints at
3271 this location. Any of these commands could cause the process to
3272 proceed beyond this point, etc. We look out for such changes by
3273 checking the global "breakpoint_proceeded" after each command.
c906108c 3274
347bddb7
PA
3275 Returns true if a breakpoint command resumed the inferior. In that
3276 case, it is the caller's responsibility to recall it again with the
3277 bpstat of the current thread. */
3278
3279static int
3280bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3281{
3282 bpstat bs;
3283 struct cleanup *old_chain;
347bddb7 3284 int again = 0;
c906108c
SS
3285
3286 /* Avoid endless recursion if a `source' command is contained
3287 in bs->commands. */
3288 if (executing_breakpoint_commands)
347bddb7 3289 return 0;
c906108c
SS
3290
3291 executing_breakpoint_commands = 1;
3292 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3293
cf6c5ffb
TT
3294 prevent_dont_repeat ();
3295
4a64f543 3296 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3297 bs = *bsp;
3298
3299 breakpoint_proceeded = 0;
3300 for (; bs != NULL; bs = bs->next)
3301 {
9add0f1b 3302 struct counted_command_line *ccmd;
6c50ab1c
JB
3303 struct command_line *cmd;
3304 struct cleanup *this_cmd_tree_chain;
3305
3306 /* Take ownership of the BSP's command tree, if it has one.
3307
3308 The command tree could legitimately contain commands like
3309 'step' and 'next', which call clear_proceed_status, which
3310 frees stop_bpstat's command tree. To make sure this doesn't
3311 free the tree we're executing out from under us, we need to
3312 take ownership of the tree ourselves. Since a given bpstat's
3313 commands are only executed once, we don't need to copy it; we
3314 can clear the pointer in the bpstat, and make sure we free
3315 the tree when we're done. */
9add0f1b
TT
3316 ccmd = bs->commands;
3317 bs->commands = NULL;
abf85f46
JK
3318 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3319 cmd = ccmd ? ccmd->commands : NULL;
3320 if (command_line_is_silent (cmd))
3321 {
3322 /* The action has been already done by bpstat_stop_status. */
3323 cmd = cmd->next;
3324 }
6c50ab1c 3325
c906108c
SS
3326 while (cmd != NULL)
3327 {
3328 execute_control_command (cmd);
3329
3330 if (breakpoint_proceeded)
3331 break;
3332 else
3333 cmd = cmd->next;
3334 }
6c50ab1c
JB
3335
3336 /* We can free this command tree now. */
3337 do_cleanups (this_cmd_tree_chain);
3338
c906108c 3339 if (breakpoint_proceeded)
32c1e744
VP
3340 {
3341 if (target_can_async_p ())
347bddb7
PA
3342 /* If we are in async mode, then the target might be still
3343 running, not stopped at any breakpoint, so nothing for
3344 us to do here -- just return to the event loop. */
3345 ;
32c1e744
VP
3346 else
3347 /* In sync mode, when execute_control_command returns
3348 we're already standing on the next breakpoint.
347bddb7
PA
3349 Breakpoint commands for that stop were not run, since
3350 execute_command does not run breakpoint commands --
3351 only command_line_handler does, but that one is not
3352 involved in execution of breakpoint commands. So, we
3353 can now execute breakpoint commands. It should be
3354 noted that making execute_command do bpstat actions is
3355 not an option -- in this case we'll have recursive
3356 invocation of bpstat for each breakpoint with a
3357 command, and can easily blow up GDB stack. Instead, we
3358 return true, which will trigger the caller to recall us
3359 with the new stop_bpstat. */
3360 again = 1;
3361 break;
32c1e744 3362 }
c906108c 3363 }
c2b8ed2c 3364 do_cleanups (old_chain);
347bddb7
PA
3365 return again;
3366}
3367
3368void
3369bpstat_do_actions (void)
3370{
353d1d73
JK
3371 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
3372
347bddb7
PA
3373 /* Do any commands attached to breakpoint we are stopped at. */
3374 while (!ptid_equal (inferior_ptid, null_ptid)
3375 && target_has_execution
3376 && !is_exited (inferior_ptid)
3377 && !is_executing (inferior_ptid))
3378 /* Since in sync mode, bpstat_do_actions may resume the inferior,
3379 and only return when it is stopped at the next breakpoint, we
3380 keep doing breakpoint actions until it returns false to
3381 indicate the inferior was not resumed. */
16c381f0 3382 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 3383 break;
353d1d73
JK
3384
3385 discard_cleanups (cleanup_if_error);
c906108c
SS
3386}
3387
fa4727a6
DJ
3388/* Print out the (old or new) value associated with a watchpoint. */
3389
3390static void
3391watchpoint_value_print (struct value *val, struct ui_file *stream)
3392{
3393 if (val == NULL)
3394 fprintf_unfiltered (stream, _("<unreadable>"));
3395 else
79a45b7d
TT
3396 {
3397 struct value_print_options opts;
3398 get_user_print_options (&opts);
3399 value_print (val, stream, &opts);
3400 }
fa4727a6
DJ
3401}
3402
e514a9d6 3403/* Generic routine for printing messages indicating why we
4a64f543 3404 stopped. The behavior of this function depends on the value
e514a9d6
JM
3405 'print_it' in the bpstat structure. Under some circumstances we
3406 may decide not to print anything here and delegate the task to
4a64f543 3407 normal_stop(). */
e514a9d6
JM
3408
3409static enum print_stop_action
3410print_bp_stop_message (bpstat bs)
3411{
3412 switch (bs->print_it)
3413 {
3414 case print_it_noop:
4a64f543 3415 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
3416 return PRINT_UNKNOWN;
3417 break;
3418
3419 case print_it_done:
3420 /* We still want to print the frame, but we already printed the
4a64f543 3421 relevant messages. */
e514a9d6
JM
3422 return PRINT_SRC_AND_LOC;
3423 break;
3424
3425 case print_it_normal:
4f8d1dc6 3426 {
f431efe5
PA
3427 struct breakpoint *b = bs->breakpoint_at;
3428
1a6a67de
TJB
3429 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
3430 which has since been deleted. */
3431 if (b == NULL)
3432 return PRINT_UNKNOWN;
3433
348d480f
PA
3434 /* Normal case. Call the breakpoint's print_it method. */
3435 return b->ops->print_it (bs);
4f8d1dc6 3436 }
348d480f 3437 break;
3086aeae 3438
e514a9d6 3439 default:
8e65ff28 3440 internal_error (__FILE__, __LINE__,
e2e0b3e5 3441 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 3442 break;
c906108c 3443 }
c906108c
SS
3444}
3445
e514a9d6
JM
3446/* Print a message indicating what happened. This is called from
3447 normal_stop(). The input to this routine is the head of the bpstat
3448 list - a list of the eventpoints that caused this stop. This
3449 routine calls the generic print routine for printing a message
3450 about reasons for stopping. This will print (for example) the
3451 "Breakpoint n," part of the output. The return value of this
3452 routine is one of:
c906108c 3453
4a64f543 3454 PRINT_UNKNOWN: Means we printed nothing.
917317f4 3455 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 3456 code to print the location. An example is
c5aa993b
JM
3457 "Breakpoint 1, " which should be followed by
3458 the location.
917317f4 3459 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
3460 to also print the location part of the message.
3461 An example is the catch/throw messages, which
4a64f543 3462 don't require a location appended to the end.
917317f4 3463 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 3464 further info to be printed. */
c906108c 3465
917317f4 3466enum print_stop_action
fba45db2 3467bpstat_print (bpstat bs)
c906108c
SS
3468{
3469 int val;
c5aa993b 3470
c906108c 3471 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
3472 (Currently all watchpoints go on the bpstat whether hit or not.
3473 That probably could (should) be changed, provided care is taken
c906108c 3474 with respect to bpstat_explains_signal). */
e514a9d6
JM
3475 for (; bs; bs = bs->next)
3476 {
3477 val = print_bp_stop_message (bs);
3478 if (val == PRINT_SRC_ONLY
3479 || val == PRINT_SRC_AND_LOC
3480 || val == PRINT_NOTHING)
3481 return val;
3482 }
c906108c 3483
e514a9d6 3484 /* We reached the end of the chain, or we got a null BS to start
4a64f543 3485 with and nothing was printed. */
917317f4 3486 return PRINT_UNKNOWN;
c906108c
SS
3487}
3488
4a64f543
MS
3489/* Evaluate the expression EXP and return 1 if value is zero. This is
3490 used inside a catch_errors to evaluate the breakpoint condition.
3491 The argument is a "struct expression *" that has been cast to a
3492 "char *" to make it pass through catch_errors. */
c906108c
SS
3493
3494static int
4efb68b1 3495breakpoint_cond_eval (void *exp)
c906108c 3496{
278cd55f 3497 struct value *mark = value_mark ();
c5aa993b 3498 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 3499
c906108c
SS
3500 value_free_to_mark (mark);
3501 return i;
3502}
3503
5760d0ab 3504/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
3505
3506static bpstat
5760d0ab 3507bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
3508{
3509 bpstat bs;
3510
3511 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
3512 bs->next = NULL;
3513 **bs_link_pointer = bs;
3514 *bs_link_pointer = &bs->next;
f431efe5
PA
3515 bs->breakpoint_at = bl->owner;
3516 bs->bp_location_at = bl;
3517 incref_bp_location (bl);
c906108c
SS
3518 /* If the condition is false, etc., don't do the commands. */
3519 bs->commands = NULL;
3520 bs->old_val = NULL;
3521 bs->print_it = print_it_normal;
3522 return bs;
3523}
3524\f
d983da9c
DJ
3525/* The target has stopped with waitstatus WS. Check if any hardware
3526 watchpoints have triggered, according to the target. */
3527
3528int
3529watchpoints_triggered (struct target_waitstatus *ws)
3530{
d92524f1 3531 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
3532 CORE_ADDR addr;
3533 struct breakpoint *b;
3534
3535 if (!stopped_by_watchpoint)
3536 {
3537 /* We were not stopped by a watchpoint. Mark all watchpoints
3538 as not triggered. */
3539 ALL_BREAKPOINTS (b)
cc60f2e3 3540 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3541 {
3542 struct watchpoint *w = (struct watchpoint *) b;
3543
3544 w->watchpoint_triggered = watch_triggered_no;
3545 }
d983da9c
DJ
3546
3547 return 0;
3548 }
3549
3550 if (!target_stopped_data_address (&current_target, &addr))
3551 {
3552 /* We were stopped by a watchpoint, but we don't know where.
3553 Mark all watchpoints as unknown. */
3554 ALL_BREAKPOINTS (b)
cc60f2e3 3555 if (is_hardware_watchpoint (b))
3a5c3e22
PA
3556 {
3557 struct watchpoint *w = (struct watchpoint *) b;
3558
3559 w->watchpoint_triggered = watch_triggered_unknown;
3560 }
d983da9c
DJ
3561
3562 return stopped_by_watchpoint;
3563 }
3564
3565 /* The target could report the data address. Mark watchpoints
3566 affected by this data address as triggered, and all others as not
3567 triggered. */
3568
3569 ALL_BREAKPOINTS (b)
cc60f2e3 3570 if (is_hardware_watchpoint (b))
d983da9c 3571 {
3a5c3e22 3572 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 3573 struct bp_location *loc;
d983da9c 3574
3a5c3e22 3575 w->watchpoint_triggered = watch_triggered_no;
a5606eee 3576 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 3577 {
3a5c3e22 3578 if (is_masked_watchpoint (b))
9c06b0b4 3579 {
3a5c3e22
PA
3580 CORE_ADDR newaddr = addr & w->hw_wp_mask;
3581 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
3582
3583 if (newaddr == start)
3584 {
3a5c3e22 3585 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3586 break;
3587 }
3588 }
3589 /* Exact match not required. Within range is sufficient. */
3590 else if (target_watchpoint_addr_within_range (&current_target,
3591 addr, loc->address,
3592 loc->length))
3593 {
3a5c3e22 3594 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
3595 break;
3596 }
3597 }
d983da9c
DJ
3598 }
3599
3600 return 1;
3601}
3602
c906108c
SS
3603/* Possible return values for watchpoint_check (this can't be an enum
3604 because of check_errors). */
3605/* The watchpoint has been deleted. */
3606#define WP_DELETED 1
3607/* The value has changed. */
3608#define WP_VALUE_CHANGED 2
3609/* The value has not changed. */
3610#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
3611/* Ignore this watchpoint, no matter if the value changed or not. */
3612#define WP_IGNORE 4
c906108c
SS
3613
3614#define BP_TEMPFLAG 1
3615#define BP_HARDWAREFLAG 2
3616
4a64f543
MS
3617/* Evaluate watchpoint condition expression and check if its value
3618 changed.
553e4c11
JB
3619
3620 P should be a pointer to struct bpstat, but is defined as a void *
3621 in order for this function to be usable with catch_errors. */
c906108c
SS
3622
3623static int
4efb68b1 3624watchpoint_check (void *p)
c906108c
SS
3625{
3626 bpstat bs = (bpstat) p;
3a5c3e22 3627 struct watchpoint *b;
c906108c
SS
3628 struct frame_info *fr;
3629 int within_current_scope;
3630
f431efe5 3631 /* BS is built from an existing struct breakpoint. */
2bdf28a0 3632 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 3633 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 3634
f6bc2008
PA
3635 /* If this is a local watchpoint, we only want to check if the
3636 watchpoint frame is in scope if the current thread is the thread
3637 that was used to create the watchpoint. */
3638 if (!watchpoint_in_thread_scope (b))
60e1c644 3639 return WP_IGNORE;
f6bc2008 3640
c906108c
SS
3641 if (b->exp_valid_block == NULL)
3642 within_current_scope = 1;
3643 else
3644 {
edb3359d
DJ
3645 struct frame_info *frame = get_current_frame ();
3646 struct gdbarch *frame_arch = get_frame_arch (frame);
3647 CORE_ADDR frame_pc = get_frame_pc (frame);
3648
4a64f543
MS
3649 /* in_function_epilogue_p() returns a non-zero value if we're
3650 still in the function but the stack frame has already been
3651 invalidated. Since we can't rely on the values of local
3652 variables after the stack has been destroyed, we are treating
3653 the watchpoint in that state as `not changed' without further
3654 checking. Don't mark watchpoints as changed if the current
3655 frame is in an epilogue - even if they are in some other
3656 frame, our view of the stack is likely to be wrong and
3657 frame_find_by_id could error out. */
a0f49112 3658 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 3659 return WP_IGNORE;
a0f49112 3660
101dcfbe 3661 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 3662 within_current_scope = (fr != NULL);
69fbadd5
DJ
3663
3664 /* If we've gotten confused in the unwinder, we might have
3665 returned a frame that can't describe this variable. */
edb3359d
DJ
3666 if (within_current_scope)
3667 {
3668 struct symbol *function;
3669
3670 function = get_frame_function (fr);
3671 if (function == NULL
3672 || !contained_in (b->exp_valid_block,
3673 SYMBOL_BLOCK_VALUE (function)))
3674 within_current_scope = 0;
3675 }
69fbadd5 3676
edb3359d 3677 if (within_current_scope)
c906108c
SS
3678 /* If we end up stopping, the current frame will get selected
3679 in normal_stop. So this call to select_frame won't affect
3680 the user. */
0f7d239c 3681 select_frame (fr);
c906108c 3682 }
c5aa993b 3683
c906108c
SS
3684 if (within_current_scope)
3685 {
4a64f543
MS
3686 /* We use value_{,free_to_}mark because it could be a *long*
3687 time before we return to the command level and call
3688 free_all_values. We can't call free_all_values because we
3689 might be in the middle of evaluating a function call. */
c906108c 3690
0cf6dd15 3691 int pc = 0;
9c06b0b4 3692 struct value *mark;
fa4727a6
DJ
3693 struct value *new_val;
3694
3a5c3e22 3695 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
3696 /* Since we don't know the exact trigger address (from
3697 stopped_data_address), just tell the user we've triggered
3698 a mask watchpoint. */
3699 return WP_VALUE_CHANGED;
3700
3701 mark = value_mark ();
0cf6dd15 3702 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 3703
4a64f543
MS
3704 /* We use value_equal_contents instead of value_equal because
3705 the latter coerces an array to a pointer, thus comparing just
3706 the address of the array instead of its contents. This is
3707 not what we want. */
fa4727a6 3708 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 3709 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 3710 {
fa4727a6
DJ
3711 if (new_val != NULL)
3712 {
3713 release_value (new_val);
3714 value_free_to_mark (mark);
3715 }
c906108c
SS
3716 bs->old_val = b->val;
3717 b->val = new_val;
fa4727a6 3718 b->val_valid = 1;
c906108c
SS
3719 return WP_VALUE_CHANGED;
3720 }
3721 else
3722 {
60e1c644 3723 /* Nothing changed. */
c906108c 3724 value_free_to_mark (mark);
c906108c
SS
3725 return WP_VALUE_NOT_CHANGED;
3726 }
3727 }
3728 else
3729 {
79a45e25
PA
3730 struct ui_out *uiout = current_uiout;
3731
c906108c 3732 /* This seems like the only logical thing to do because
c5aa993b
JM
3733 if we temporarily ignored the watchpoint, then when
3734 we reenter the block in which it is valid it contains
3735 garbage (in the case of a function, it may have two
3736 garbage values, one before and one after the prologue).
3737 So we can't even detect the first assignment to it and
3738 watch after that (since the garbage may or may not equal
3739 the first value assigned). */
348d480f
PA
3740 /* We print all the stop information in
3741 breakpoint_ops->print_it, but in this case, by the time we
3742 call breakpoint_ops->print_it this bp will be deleted
3743 already. So we have no choice but print the information
3744 here. */
9dc5e2a9 3745 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
3746 ui_out_field_string
3747 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 3748 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 3749 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
3750 ui_out_text (uiout,
3751 " deleted because the program has left the block in\n\
8b93c638 3752which its expression is valid.\n");
4ce44c66 3753
cdac0397 3754 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 3755 decref_counted_command_line (&b->base.commands);
d0fb5eae 3756 watchpoint_del_at_next_stop (b);
c906108c
SS
3757
3758 return WP_DELETED;
3759 }
3760}
3761
18a18393 3762/* Return true if it looks like target has stopped due to hitting
348d480f
PA
3763 breakpoint location BL. This function does not check if we should
3764 stop, only if BL explains the stop. */
3765
18a18393 3766static int
6c95b8df
PA
3767bpstat_check_location (const struct bp_location *bl,
3768 struct address_space *aspace, CORE_ADDR bp_addr)
18a18393
VP
3769{
3770 struct breakpoint *b = bl->owner;
3771
348d480f 3772 /* BL is from an existing breakpoint. */
2bdf28a0
JK
3773 gdb_assert (b != NULL);
3774
348d480f 3775 return b->ops->breakpoint_hit (bl, aspace, bp_addr);
18a18393
VP
3776}
3777
3a5c3e22
PA
3778/* Determine if the watched values have actually changed, and we
3779 should stop. If not, set BS->stop to 0. */
3780
18a18393
VP
3781static void
3782bpstat_check_watchpoint (bpstat bs)
3783{
2bdf28a0 3784 const struct bp_location *bl;
3a5c3e22 3785 struct watchpoint *b;
2bdf28a0
JK
3786
3787 /* BS is built for existing struct breakpoint. */
f431efe5 3788 bl = bs->bp_location_at;
2bdf28a0 3789 gdb_assert (bl != NULL);
3a5c3e22 3790 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 3791 gdb_assert (b != NULL);
18a18393 3792
18a18393 3793 {
18a18393
VP
3794 int must_check_value = 0;
3795
3a5c3e22 3796 if (b->base.type == bp_watchpoint)
18a18393
VP
3797 /* For a software watchpoint, we must always check the
3798 watched value. */
3799 must_check_value = 1;
3800 else if (b->watchpoint_triggered == watch_triggered_yes)
3801 /* We have a hardware watchpoint (read, write, or access)
3802 and the target earlier reported an address watched by
3803 this watchpoint. */
3804 must_check_value = 1;
3805 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 3806 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
3807 /* We were stopped by a hardware watchpoint, but the target could
3808 not report the data address. We must check the watchpoint's
3809 value. Access and read watchpoints are out of luck; without
3810 a data address, we can't figure it out. */
3811 must_check_value = 1;
3a5c3e22 3812
18a18393
VP
3813 if (must_check_value)
3814 {
3e43a32a
MS
3815 char *message
3816 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 3817 b->base.number);
18a18393
VP
3818 struct cleanup *cleanups = make_cleanup (xfree, message);
3819 int e = catch_errors (watchpoint_check, bs, message,
3820 RETURN_MASK_ALL);
3821 do_cleanups (cleanups);
3822 switch (e)
3823 {
3824 case WP_DELETED:
3825 /* We've already printed what needs to be printed. */
3826 bs->print_it = print_it_done;
3827 /* Stop. */
3828 break;
60e1c644
PA
3829 case WP_IGNORE:
3830 bs->print_it = print_it_noop;
3831 bs->stop = 0;
3832 break;
18a18393 3833 case WP_VALUE_CHANGED:
3a5c3e22 3834 if (b->base.type == bp_read_watchpoint)
18a18393 3835 {
85d721b8
PA
3836 /* There are two cases to consider here:
3837
4a64f543 3838 1. We're watching the triggered memory for reads.
85d721b8
PA
3839 In that case, trust the target, and always report
3840 the watchpoint hit to the user. Even though
3841 reads don't cause value changes, the value may
3842 have changed since the last time it was read, and
3843 since we're not trapping writes, we will not see
3844 those, and as such we should ignore our notion of
3845 old value.
3846
4a64f543 3847 2. We're watching the triggered memory for both
85d721b8
PA
3848 reads and writes. There are two ways this may
3849 happen:
3850
4a64f543 3851 2.1. This is a target that can't break on data
85d721b8
PA
3852 reads only, but can break on accesses (reads or
3853 writes), such as e.g., x86. We detect this case
3854 at the time we try to insert read watchpoints.
3855
4a64f543 3856 2.2. Otherwise, the target supports read
85d721b8
PA
3857 watchpoints, but, the user set an access or write
3858 watchpoint watching the same memory as this read
3859 watchpoint.
3860
3861 If we're watching memory writes as well as reads,
3862 ignore watchpoint hits when we find that the
3863 value hasn't changed, as reads don't cause
3864 changes. This still gives false positives when
3865 the program writes the same value to memory as
3866 what there was already in memory (we will confuse
3867 it for a read), but it's much better than
3868 nothing. */
3869
3870 int other_write_watchpoint = 0;
3871
3872 if (bl->watchpoint_type == hw_read)
3873 {
3874 struct breakpoint *other_b;
3875
3876 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
3877 if (other_b->type == bp_hardware_watchpoint
3878 || other_b->type == bp_access_watchpoint)
85d721b8 3879 {
3a5c3e22
PA
3880 struct watchpoint *other_w =
3881 (struct watchpoint *) other_b;
3882
3883 if (other_w->watchpoint_triggered
3884 == watch_triggered_yes)
3885 {
3886 other_write_watchpoint = 1;
3887 break;
3888 }
85d721b8
PA
3889 }
3890 }
3891
3892 if (other_write_watchpoint
3893 || bl->watchpoint_type == hw_access)
3894 {
3895 /* We're watching the same memory for writes,
3896 and the value changed since the last time we
3897 updated it, so this trap must be for a write.
3898 Ignore it. */
3899 bs->print_it = print_it_noop;
3900 bs->stop = 0;
3901 }
18a18393
VP
3902 }
3903 break;
3904 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
3905 if (b->base.type == bp_hardware_watchpoint
3906 || b->base.type == bp_watchpoint)
18a18393
VP
3907 {
3908 /* Don't stop: write watchpoints shouldn't fire if
3909 the value hasn't changed. */
3910 bs->print_it = print_it_noop;
3911 bs->stop = 0;
3912 }
3913 /* Stop. */
3914 break;
3915 default:
3916 /* Can't happen. */
3917 case 0:
3918 /* Error from catch_errors. */
3a5c3e22 3919 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 3920 watchpoint_del_at_next_stop (b);
18a18393
VP
3921 /* We've already printed what needs to be printed. */
3922 bs->print_it = print_it_done;
3923 break;
3924 }
3925 }
3926 else /* must_check_value == 0 */
3927 {
3928 /* This is a case where some watchpoint(s) triggered, but
3929 not at the address of this watchpoint, or else no
3930 watchpoint triggered after all. So don't print
3931 anything for this watchpoint. */
3932 bs->print_it = print_it_noop;
3933 bs->stop = 0;
3934 }
3935 }
3936}
3937
3938
3939/* Check conditions (condition proper, frame, thread and ignore count)
3940 of breakpoint referred to by BS. If we should not stop for this
3941 breakpoint, set BS->stop to 0. */
f431efe5 3942
18a18393
VP
3943static void
3944bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3945{
3946 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
3947 const struct bp_location *bl;
3948 struct breakpoint *b;
3949
3950 /* BS is built for existing struct breakpoint. */
f431efe5 3951 bl = bs->bp_location_at;
2bdf28a0 3952 gdb_assert (bl != NULL);
f431efe5 3953 b = bs->breakpoint_at;
2bdf28a0 3954 gdb_assert (b != NULL);
18a18393
VP
3955
3956 if (frame_id_p (b->frame_id)
edb3359d 3957 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3958 bs->stop = 0;
3959 else if (bs->stop)
3960 {
3961 int value_is_zero = 0;
60e1c644
PA
3962 struct expression *cond;
3963
7371cf6d
PM
3964 /* Evaluate Python breakpoints that have a "stop"
3965 method implemented. */
3966 if (b->py_bp_object)
3967 bs->stop = gdbpy_should_stop (b->py_bp_object);
3968
60e1c644 3969 if (is_watchpoint (b))
3a5c3e22
PA
3970 {
3971 struct watchpoint *w = (struct watchpoint *) b;
3972
3973 cond = w->cond_exp;
3974 }
60e1c644
PA
3975 else
3976 cond = bl->cond;
3977
f431efe5 3978 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 3979 {
60e1c644 3980 int within_current_scope = 1;
3a5c3e22 3981 struct watchpoint * w;
60e1c644 3982
c5bc3a77
DJ
3983 /* We use value_mark and value_free_to_mark because it could
3984 be a long time before we return to the command level and
3985 call free_all_values. We can't call free_all_values
3986 because we might be in the middle of evaluating a
3987 function call. */
3988 struct value *mark = value_mark ();
3989
3a5c3e22
PA
3990 if (is_watchpoint (b))
3991 w = (struct watchpoint *) b;
3992 else
3993 w = NULL;
3994
edb3359d
DJ
3995 /* Need to select the frame, with all that implies so that
3996 the conditions will have the right context. Because we
3997 use the frame, we will not see an inlined function's
3998 variables when we arrive at a breakpoint at the start
3999 of the inlined function; the current frame will be the
4000 call site. */
3a5c3e22 4001 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4002 select_frame (get_current_frame ());
4003 else
4004 {
4005 struct frame_info *frame;
4006
4007 /* For local watchpoint expressions, which particular
4008 instance of a local is being watched matters, so we
4009 keep track of the frame to evaluate the expression
4010 in. To evaluate the condition however, it doesn't
4011 really matter which instantiation of the function
4012 where the condition makes sense triggers the
4013 watchpoint. This allows an expression like "watch
4014 global if q > 10" set in `func', catch writes to
4015 global on all threads that call `func', or catch
4016 writes on all recursive calls of `func' by a single
4017 thread. We simply always evaluate the condition in
4018 the innermost frame that's executing where it makes
4019 sense to evaluate the condition. It seems
4020 intuitive. */
3a5c3e22 4021 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4022 if (frame != NULL)
4023 select_frame (frame);
4024 else
4025 within_current_scope = 0;
4026 }
4027 if (within_current_scope)
4028 value_is_zero
4029 = catch_errors (breakpoint_cond_eval, cond,
4030 "Error in testing breakpoint condition:\n",
4031 RETURN_MASK_ALL);
4032 else
4033 {
4034 warning (_("Watchpoint condition cannot be tested "
4035 "in the current scope"));
4036 /* If we failed to set the right context for this
4037 watchpoint, unconditionally report it. */
4038 value_is_zero = 0;
4039 }
4a64f543 4040 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4041 value_free_to_mark (mark);
18a18393 4042 }
60e1c644
PA
4043
4044 if (cond && value_is_zero)
18a18393
VP
4045 {
4046 bs->stop = 0;
4047 }
4048 else if (b->thread != -1 && b->thread != thread_id)
4049 {
4050 bs->stop = 0;
4051 }
4052 else if (b->ignore_count > 0)
4053 {
4054 b->ignore_count--;
4055 annotate_ignore_count_change ();
4056 bs->stop = 0;
4a64f543 4057 /* Increase the hit count even though we don't stop. */
18a18393 4058 ++(b->hit_count);
8d3788bd 4059 observer_notify_breakpoint_modified (b);
18a18393
VP
4060 }
4061 }
4062}
4063
4064
9709f61c 4065/* Get a bpstat associated with having just stopped at address
d983da9c 4066 BP_ADDR in thread PTID.
c906108c 4067
d983da9c 4068 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4069 don't understand this stop. Result is a chain of bpstat's such
4070 that:
c906108c 4071
c5aa993b 4072 if we don't understand the stop, the result is a null pointer.
c906108c 4073
c5aa993b 4074 if we understand why we stopped, the result is not null.
c906108c 4075
c5aa993b
JM
4076 Each element of the chain refers to a particular breakpoint or
4077 watchpoint at which we have stopped. (We may have stopped for
4078 several reasons concurrently.)
c906108c 4079
c5aa993b
JM
4080 Each element of the chain has valid next, breakpoint_at,
4081 commands, FIXME??? fields. */
c906108c
SS
4082
4083bpstat
6c95b8df
PA
4084bpstat_stop_status (struct address_space *aspace,
4085 CORE_ADDR bp_addr, ptid_t ptid)
c906108c 4086{
0d381245 4087 struct breakpoint *b = NULL;
afe38095 4088 struct bp_location *bl;
20874c92 4089 struct bp_location *loc;
5760d0ab
JK
4090 /* First item of allocated bpstat's. */
4091 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4092 /* Pointer to the last thing in the chain currently. */
5760d0ab 4093 bpstat bs;
20874c92 4094 int ix;
429374b8 4095 int need_remove_insert;
f431efe5 4096 int removed_any;
c906108c 4097
f431efe5
PA
4098 /* First, build the bpstat chain with locations that explain a
4099 target stop, while being careful to not set the target running,
4100 as that may invalidate locations (in particular watchpoint
4101 locations are recreated). Resuming will happen here with
4102 breakpoint conditions or watchpoint expressions that include
4103 inferior function calls. */
c5aa993b 4104
429374b8
JK
4105 ALL_BREAKPOINTS (b)
4106 {
4107 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4108 continue;
a5606eee 4109
429374b8
JK
4110 for (bl = b->loc; bl != NULL; bl = bl->next)
4111 {
4a64f543
MS
4112 /* For hardware watchpoints, we look only at the first
4113 location. The watchpoint_check function will work on the
4114 entire expression, not the individual locations. For
4115 read watchpoints, the watchpoints_triggered function has
4116 checked all locations already. */
429374b8
JK
4117 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4118 break;
18a18393 4119
429374b8
JK
4120 if (bl->shlib_disabled)
4121 continue;
c5aa993b 4122
429374b8
JK
4123 if (!bpstat_check_location (bl, aspace, bp_addr))
4124 continue;
c5aa993b 4125
4a64f543
MS
4126 /* Come here if it's a watchpoint, or if the break address
4127 matches. */
c5aa993b 4128
4a64f543
MS
4129 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4130 explain stop. */
c5aa993b 4131
f431efe5
PA
4132 /* Assume we stop. Should we find a watchpoint that is not
4133 actually triggered, or if the condition of the breakpoint
4134 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4135 bs->stop = 1;
4136 bs->print = 1;
d983da9c 4137
f431efe5
PA
4138 /* If this is a scope breakpoint, mark the associated
4139 watchpoint as triggered so that we will handle the
4140 out-of-scope event. We'll get to the watchpoint next
4141 iteration. */
d0fb5eae 4142 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4143 {
4144 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4145
4146 w->watchpoint_triggered = watch_triggered_yes;
4147 }
f431efe5
PA
4148 }
4149 }
4150
4151 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4152 {
f1310107 4153 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4154 {
5760d0ab 4155 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4156 /* For hits of moribund locations, we should just proceed. */
4157 bs->stop = 0;
4158 bs->print = 0;
4159 bs->print_it = print_it_noop;
4160 }
4161 }
4162
f431efe5
PA
4163 /* Now go through the locations that caused the target to stop, and
4164 check whether we're interested in reporting this stop to higher
4165 layers, or whether we should resume the target transparently. */
4166
4167 removed_any = 0;
4168
5760d0ab 4169 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4170 {
4171 if (!bs->stop)
4172 continue;
4173
f431efe5 4174 b = bs->breakpoint_at;
348d480f
PA
4175 b->ops->check_status (bs);
4176 if (bs->stop)
28010a5d 4177 {
348d480f 4178 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4179
429374b8
JK
4180 if (bs->stop)
4181 {
4182 ++(b->hit_count);
8d3788bd 4183 observer_notify_breakpoint_modified (b);
c906108c 4184
4a64f543 4185 /* We will stop here. */
429374b8
JK
4186 if (b->disposition == disp_disable)
4187 {
4188 if (b->enable_state != bp_permanent)
4189 b->enable_state = bp_disabled;
f431efe5 4190 removed_any = 1;
429374b8
JK
4191 }
4192 if (b->silent)
4193 bs->print = 0;
4194 bs->commands = b->commands;
9add0f1b 4195 incref_counted_command_line (bs->commands);
abf85f46
JK
4196 if (command_line_is_silent (bs->commands
4197 ? bs->commands->commands : NULL))
4198 bs->print = 0;
429374b8
JK
4199 }
4200
e5dd4106 4201 /* Print nothing for this entry if we don't stop or don't print. */
429374b8
JK
4202 if (bs->stop == 0 || bs->print == 0)
4203 bs->print_it = print_it_noop;
348d480f 4204 }
429374b8 4205 }
876fa593 4206
d983da9c
DJ
4207 /* If we aren't stopping, the value of some hardware watchpoint may
4208 not have changed, but the intermediate memory locations we are
4209 watching may have. Don't bother if we're stopping; this will get
4210 done later. */
d832cb68 4211 need_remove_insert = 0;
5760d0ab
JK
4212 if (! bpstat_causes_stop (bs_head))
4213 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4214 if (!bs->stop
f431efe5
PA
4215 && bs->breakpoint_at
4216 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4217 {
3a5c3e22
PA
4218 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4219
4220 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4221 need_remove_insert = 1;
d983da9c
DJ
4222 }
4223
d832cb68 4224 if (need_remove_insert)
2d134ed3 4225 update_global_location_list (1);
f431efe5
PA
4226 else if (removed_any)
4227 update_global_location_list (0);
d832cb68 4228
5760d0ab 4229 return bs_head;
c906108c 4230}
628fe4e4
JK
4231
4232static void
4233handle_jit_event (void)
4234{
4235 struct frame_info *frame;
4236 struct gdbarch *gdbarch;
4237
4238 /* Switch terminal for any messages produced by
4239 breakpoint_re_set. */
4240 target_terminal_ours_for_output ();
4241
4242 frame = get_current_frame ();
4243 gdbarch = get_frame_arch (frame);
4244
4245 jit_event_handler (gdbarch);
4246
4247 target_terminal_inferior ();
4248}
4249
4250/* Prepare WHAT final decision for infrun. */
4251
4252/* Decide what infrun needs to do with this bpstat. */
4253
c906108c 4254struct bpstat_what
0e30163f 4255bpstat_what (bpstat bs_head)
c906108c 4256{
c906108c 4257 struct bpstat_what retval;
628fe4e4
JK
4258 /* We need to defer calling `solib_add', as adding new symbols
4259 resets breakpoints, which in turn deletes breakpoint locations,
4260 and hence may clear unprocessed entries in the BS chain. */
4261 int shlib_event = 0;
4262 int jit_event = 0;
0e30163f 4263 bpstat bs;
c906108c 4264
628fe4e4 4265 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 4266 retval.call_dummy = STOP_NONE;
186c406b 4267 retval.is_longjmp = 0;
628fe4e4 4268
0e30163f 4269 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 4270 {
628fe4e4
JK
4271 /* Extract this BS's action. After processing each BS, we check
4272 if its action overrides all we've seem so far. */
4273 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
4274 enum bptype bptype;
4275
c906108c 4276 if (bs->breakpoint_at == NULL)
628fe4e4
JK
4277 {
4278 /* I suspect this can happen if it was a momentary
4279 breakpoint which has since been deleted. */
4280 bptype = bp_none;
4281 }
20874c92 4282 else
f431efe5 4283 bptype = bs->breakpoint_at->type;
628fe4e4
JK
4284
4285 switch (bptype)
c906108c
SS
4286 {
4287 case bp_none:
628fe4e4 4288 break;
c906108c
SS
4289 case bp_breakpoint:
4290 case bp_hardware_breakpoint:
4291 case bp_until:
4292 case bp_finish:
4293 if (bs->stop)
4294 {
4295 if (bs->print)
628fe4e4 4296 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4297 else
628fe4e4 4298 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4299 }
4300 else
628fe4e4 4301 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
4302 break;
4303 case bp_watchpoint:
4304 case bp_hardware_watchpoint:
4305 case bp_read_watchpoint:
4306 case bp_access_watchpoint:
4307 if (bs->stop)
4308 {
4309 if (bs->print)
628fe4e4 4310 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 4311 else
628fe4e4 4312 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
4313 }
4314 else
628fe4e4
JK
4315 {
4316 /* There was a watchpoint, but we're not stopping.
4317 This requires no further action. */
4318 }
c906108c
SS
4319 break;
4320 case bp_longjmp:
186c406b 4321 case bp_exception:
628fe4e4 4322 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 4323 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
4324 break;
4325 case bp_longjmp_resume:
186c406b 4326 case bp_exception_resume:
628fe4e4 4327 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 4328 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
4329 break;
4330 case bp_step_resume:
4331 if (bs->stop)
628fe4e4
JK
4332 this_action = BPSTAT_WHAT_STEP_RESUME;
4333 else
c906108c 4334 {
628fe4e4
JK
4335 /* It is for the wrong frame. */
4336 this_action = BPSTAT_WHAT_SINGLE;
c906108c 4337 }
c906108c 4338 break;
2c03e5be
PA
4339 case bp_hp_step_resume:
4340 if (bs->stop)
4341 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
4342 else
4343 {
4344 /* It is for the wrong frame. */
4345 this_action = BPSTAT_WHAT_SINGLE;
4346 }
4347 break;
c906108c 4348 case bp_watchpoint_scope:
c4093a6a 4349 case bp_thread_event:
1900040c 4350 case bp_overlay_event:
0fd8e87f 4351 case bp_longjmp_master:
aa7d318d 4352 case bp_std_terminate_master:
186c406b 4353 case bp_exception_master:
628fe4e4 4354 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 4355 break;
ce78b96d 4356 case bp_catchpoint:
c5aa993b
JM
4357 if (bs->stop)
4358 {
4359 if (bs->print)
628fe4e4 4360 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 4361 else
628fe4e4 4362 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
4363 }
4364 else
628fe4e4
JK
4365 {
4366 /* There was a catchpoint, but we're not stopping.
4367 This requires no further action. */
4368 }
4369 break;
4370 case bp_shlib_event:
4371 shlib_event = 1;
4372
4373 /* If requested, stop when the dynamic linker notifies GDB
4374 of events. This allows the user to get control and place
4375 breakpoints in initializer routines for dynamically
4376 loaded objects (among other things). */
4377 if (stop_on_solib_events)
4378 this_action = BPSTAT_WHAT_STOP_NOISY;
4379 else
4380 this_action = BPSTAT_WHAT_SINGLE;
4381 break;
4382 case bp_jit_event:
4383 jit_event = 1;
4384 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 4385 break;
c906108c 4386 case bp_call_dummy:
53a5351d
JM
4387 /* Make sure the action is stop (silent or noisy),
4388 so infrun.c pops the dummy frame. */
aa7d318d 4389 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 4390 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
4391 break;
4392 case bp_std_terminate:
4393 /* Make sure the action is stop (silent or noisy),
4394 so infrun.c pops the dummy frame. */
aa7d318d 4395 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 4396 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 4397 break;
1042e4c0 4398 case bp_tracepoint:
7a697b8d 4399 case bp_fast_tracepoint:
0fb4aa4b 4400 case bp_static_tracepoint:
1042e4c0
SS
4401 /* Tracepoint hits should not be reported back to GDB, and
4402 if one got through somehow, it should have been filtered
4403 out already. */
4404 internal_error (__FILE__, __LINE__,
7a697b8d 4405 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
4406 break;
4407 case bp_gnu_ifunc_resolver:
4408 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
4409 this_action = BPSTAT_WHAT_SINGLE;
4410 break;
4411 case bp_gnu_ifunc_resolver_return:
4412 /* The breakpoint will be removed, execution will restart from the
4413 PC of the former breakpoint. */
4414 this_action = BPSTAT_WHAT_KEEP_CHECKING;
4415 break;
628fe4e4
JK
4416 default:
4417 internal_error (__FILE__, __LINE__,
4418 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 4419 }
628fe4e4
JK
4420
4421 retval.main_action = max (retval.main_action, this_action);
c906108c 4422 }
628fe4e4 4423
0e30163f
JK
4424 /* These operations may affect the bs->breakpoint_at state so they are
4425 delayed after MAIN_ACTION is decided above. */
4426
628fe4e4
JK
4427 if (shlib_event)
4428 {
4429 if (debug_infrun)
4430 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_shlib_event\n");
4431
4432 /* Check for any newly added shared libraries if we're supposed
4433 to be adding them automatically. */
4434
4435 /* Switch terminal for any messages produced by
4436 breakpoint_re_set. */
4437 target_terminal_ours_for_output ();
4438
4439#ifdef SOLIB_ADD
4440 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
4441#else
4442 solib_add (NULL, 0, &current_target, auto_solib_add);
4443#endif
4444
4445 target_terminal_inferior ();
4446 }
4447
4448 if (jit_event)
4449 {
4450 if (debug_infrun)
4451 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
4452
4453 handle_jit_event ();
4454 }
4455
0e30163f
JK
4456 for (bs = bs_head; bs != NULL; bs = bs->next)
4457 {
4458 struct breakpoint *b = bs->breakpoint_at;
4459
4460 if (b == NULL)
4461 continue;
4462 switch (b->type)
4463 {
4464 case bp_gnu_ifunc_resolver:
4465 gnu_ifunc_resolver_stop (b);
4466 break;
4467 case bp_gnu_ifunc_resolver_return:
4468 gnu_ifunc_resolver_return_stop (b);
4469 break;
4470 }
4471 }
4472
c906108c
SS
4473 return retval;
4474}
4475
4476/* Nonzero if we should step constantly (e.g. watchpoints on machines
4477 without hardware support). This isn't related to a specific bpstat,
4478 just to things like whether watchpoints are set. */
4479
c5aa993b 4480int
fba45db2 4481bpstat_should_step (void)
c906108c
SS
4482{
4483 struct breakpoint *b;
cc59ec59 4484
c906108c 4485 ALL_BREAKPOINTS (b)
717a8278 4486 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 4487 return 1;
c906108c
SS
4488 return 0;
4489}
4490
67822962
PA
4491int
4492bpstat_causes_stop (bpstat bs)
4493{
4494 for (; bs != NULL; bs = bs->next)
4495 if (bs->stop)
4496 return 1;
4497
4498 return 0;
4499}
4500
c906108c 4501\f
c5aa993b 4502
170b53b2
UW
4503/* Compute a string of spaces suitable to indent the next line
4504 so it starts at the position corresponding to the table column
4505 named COL_NAME in the currently active table of UIOUT. */
4506
4507static char *
4508wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
4509{
4510 static char wrap_indent[80];
4511 int i, total_width, width, align;
4512 char *text;
4513
4514 total_width = 0;
4515 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
4516 {
4517 if (strcmp (text, col_name) == 0)
4518 {
4519 gdb_assert (total_width < sizeof wrap_indent);
4520 memset (wrap_indent, ' ', total_width);
4521 wrap_indent[total_width] = 0;
4522
4523 return wrap_indent;
4524 }
4525
4526 total_width += width + 1;
4527 }
4528
4529 return NULL;
4530}
4531
859825b8
JK
4532/* Print the LOC location out of the list of B->LOC locations. */
4533
170b53b2
UW
4534static void
4535print_breakpoint_location (struct breakpoint *b,
4536 struct bp_location *loc)
0d381245 4537{
79a45e25 4538 struct ui_out *uiout = current_uiout;
6c95b8df
PA
4539 struct cleanup *old_chain = save_current_program_space ();
4540
859825b8
JK
4541 if (loc != NULL && loc->shlib_disabled)
4542 loc = NULL;
4543
6c95b8df
PA
4544 if (loc != NULL)
4545 set_current_program_space (loc->pspace);
4546
56435ebe
TT
4547 if (b->display_canonical)
4548 ui_out_field_string (uiout, "what", b->addr_string);
4549 else if (b->source_file && loc)
0d381245
VP
4550 {
4551 struct symbol *sym
4552 = find_pc_sect_function (loc->address, loc->section);
4553 if (sym)
4554 {
4555 ui_out_text (uiout, "in ");
4556 ui_out_field_string (uiout, "func",
4557 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
4558 ui_out_text (uiout, " ");
4559 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
4560 ui_out_text (uiout, "at ");
0d381245
VP
4561 }
4562 ui_out_field_string (uiout, "file", b->source_file);
4563 ui_out_text (uiout, ":");
4564
4565 if (ui_out_is_mi_like_p (uiout))
4566 {
4567 struct symtab_and_line sal = find_pc_line (loc->address, 0);
4568 char *fullname = symtab_to_fullname (sal.symtab);
4569
4570 if (fullname)
4571 ui_out_field_string (uiout, "fullname", fullname);
4572 }
4573
4574 ui_out_field_int (uiout, "line", b->line_number);
4575 }
859825b8 4576 else if (loc)
0d381245 4577 {
170b53b2
UW
4578 struct ui_stream *stb = ui_out_stream_new (uiout);
4579 struct cleanup *stb_chain = make_cleanup_ui_out_stream_delete (stb);
4580
22e722e1
DJ
4581 print_address_symbolic (loc->gdbarch, loc->address, stb->stream,
4582 demangle, "");
0d381245 4583 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
4584
4585 do_cleanups (stb_chain);
0d381245 4586 }
859825b8
JK
4587 else
4588 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df
PA
4589
4590 do_cleanups (old_chain);
0d381245
VP
4591}
4592
269b11a2
PA
4593static const char *
4594bptype_string (enum bptype type)
c906108c 4595{
c4093a6a
JM
4596 struct ep_type_description
4597 {
4598 enum bptype type;
4599 char *description;
4600 };
4601 static struct ep_type_description bptypes[] =
c906108c 4602 {
c5aa993b
JM
4603 {bp_none, "?deleted?"},
4604 {bp_breakpoint, "breakpoint"},
c906108c 4605 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
4606 {bp_until, "until"},
4607 {bp_finish, "finish"},
4608 {bp_watchpoint, "watchpoint"},
c906108c 4609 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
4610 {bp_read_watchpoint, "read watchpoint"},
4611 {bp_access_watchpoint, "acc watchpoint"},
4612 {bp_longjmp, "longjmp"},
4613 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
4614 {bp_exception, "exception"},
4615 {bp_exception_resume, "exception resume"},
c5aa993b 4616 {bp_step_resume, "step resume"},
2c03e5be 4617 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
4618 {bp_watchpoint_scope, "watchpoint scope"},
4619 {bp_call_dummy, "call dummy"},
aa7d318d 4620 {bp_std_terminate, "std::terminate"},
c5aa993b 4621 {bp_shlib_event, "shlib events"},
c4093a6a 4622 {bp_thread_event, "thread events"},
1900040c 4623 {bp_overlay_event, "overlay events"},
0fd8e87f 4624 {bp_longjmp_master, "longjmp master"},
aa7d318d 4625 {bp_std_terminate_master, "std::terminate master"},
186c406b 4626 {bp_exception_master, "exception master"},
ce78b96d 4627 {bp_catchpoint, "catchpoint"},
1042e4c0 4628 {bp_tracepoint, "tracepoint"},
7a697b8d 4629 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 4630 {bp_static_tracepoint, "static tracepoint"},
4efc6507 4631 {bp_jit_event, "jit events"},
0e30163f
JK
4632 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
4633 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 4634 };
269b11a2
PA
4635
4636 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
4637 || ((int) type != bptypes[(int) type].type))
4638 internal_error (__FILE__, __LINE__,
4639 _("bptypes table does not describe type #%d."),
4640 (int) type);
4641
4642 return bptypes[(int) type].description;
4643}
4644
4645/* Print B to gdb_stdout. */
4646
4647static void
4648print_one_breakpoint_location (struct breakpoint *b,
4649 struct bp_location *loc,
4650 int loc_number,
4651 struct bp_location **last_loc,
269b11a2
PA
4652 int allflag)
4653{
4654 struct command_line *l;
c2c6d25f 4655 static char bpenables[] = "nynny";
c906108c 4656
79a45e25 4657 struct ui_out *uiout = current_uiout;
0d381245
VP
4658 int header_of_multiple = 0;
4659 int part_of_multiple = (loc != NULL);
79a45b7d
TT
4660 struct value_print_options opts;
4661
4662 get_user_print_options (&opts);
0d381245
VP
4663
4664 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
4665 /* See comment in print_one_breakpoint concerning treatment of
4666 breakpoints with single disabled location. */
0d381245
VP
4667 if (loc == NULL
4668 && (b->loc != NULL
4669 && (b->loc->next != NULL || !b->loc->enabled)))
4670 header_of_multiple = 1;
4671 if (loc == NULL)
4672 loc = b->loc;
4673
c4093a6a
JM
4674 annotate_record ();
4675
4676 /* 1 */
4677 annotate_field (0);
0d381245
VP
4678 if (part_of_multiple)
4679 {
4680 char *formatted;
0c6773c1 4681 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
4682 ui_out_field_string (uiout, "number", formatted);
4683 xfree (formatted);
4684 }
4685 else
4686 {
4687 ui_out_field_int (uiout, "number", b->number);
4688 }
c4093a6a
JM
4689
4690 /* 2 */
4691 annotate_field (1);
0d381245
VP
4692 if (part_of_multiple)
4693 ui_out_field_skip (uiout, "type");
269b11a2
PA
4694 else
4695 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
4696
4697 /* 3 */
4698 annotate_field (2);
0d381245
VP
4699 if (part_of_multiple)
4700 ui_out_field_skip (uiout, "disp");
4701 else
2cec12e5 4702 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 4703
c4093a6a
JM
4704
4705 /* 4 */
4706 annotate_field (3);
0d381245 4707 if (part_of_multiple)
54e52265 4708 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 4709 else
4a64f543
MS
4710 ui_out_field_fmt (uiout, "enabled", "%c",
4711 bpenables[(int) b->enable_state]);
54e52265 4712 ui_out_spaces (uiout, 2);
0d381245 4713
c4093a6a
JM
4714
4715 /* 5 and 6 */
3086aeae 4716 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 4717 {
4a64f543
MS
4718 /* Although the print_one can possibly print all locations,
4719 calling it here is not likely to get any nice result. So,
4720 make sure there's just one location. */
0d381245 4721 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 4722 b->ops->print_one (b, last_loc);
0d381245 4723 }
3086aeae
DJ
4724 else
4725 switch (b->type)
4726 {
4727 case bp_none:
4728 internal_error (__FILE__, __LINE__,
e2e0b3e5 4729 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 4730 break;
c906108c 4731
3086aeae
DJ
4732 case bp_watchpoint:
4733 case bp_hardware_watchpoint:
4734 case bp_read_watchpoint:
4735 case bp_access_watchpoint:
3a5c3e22
PA
4736 {
4737 struct watchpoint *w = (struct watchpoint *) b;
4738
4739 /* Field 4, the address, is omitted (which makes the columns
4740 not line up too nicely with the headers, but the effect
4741 is relatively readable). */
4742 if (opts.addressprint)
4743 ui_out_field_skip (uiout, "addr");
4744 annotate_field (5);
4745 ui_out_field_string (uiout, "what", w->exp_string);
4746 }
3086aeae
DJ
4747 break;
4748
3086aeae
DJ
4749 case bp_breakpoint:
4750 case bp_hardware_breakpoint:
4751 case bp_until:
4752 case bp_finish:
4753 case bp_longjmp:
4754 case bp_longjmp_resume:
186c406b
TT
4755 case bp_exception:
4756 case bp_exception_resume:
3086aeae 4757 case bp_step_resume:
2c03e5be 4758 case bp_hp_step_resume:
3086aeae
DJ
4759 case bp_watchpoint_scope:
4760 case bp_call_dummy:
aa7d318d 4761 case bp_std_terminate:
3086aeae
DJ
4762 case bp_shlib_event:
4763 case bp_thread_event:
4764 case bp_overlay_event:
0fd8e87f 4765 case bp_longjmp_master:
aa7d318d 4766 case bp_std_terminate_master:
186c406b 4767 case bp_exception_master:
1042e4c0 4768 case bp_tracepoint:
7a697b8d 4769 case bp_fast_tracepoint:
0fb4aa4b 4770 case bp_static_tracepoint:
4efc6507 4771 case bp_jit_event:
0e30163f
JK
4772 case bp_gnu_ifunc_resolver:
4773 case bp_gnu_ifunc_resolver_return:
79a45b7d 4774 if (opts.addressprint)
3086aeae
DJ
4775 {
4776 annotate_field (4);
54e52265 4777 if (header_of_multiple)
0d381245 4778 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 4779 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 4780 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 4781 else
5af949e3
UW
4782 ui_out_field_core_addr (uiout, "addr",
4783 loc->gdbarch, loc->address);
3086aeae
DJ
4784 }
4785 annotate_field (5);
0d381245 4786 if (!header_of_multiple)
170b53b2 4787 print_breakpoint_location (b, loc);
0d381245 4788 if (b->loc)
a6d9a66e 4789 *last_loc = b->loc;
3086aeae
DJ
4790 break;
4791 }
c906108c 4792
6c95b8df
PA
4793
4794 /* For backward compatibility, don't display inferiors unless there
4795 are several. */
4796 if (loc != NULL
4797 && !header_of_multiple
4798 && (allflag
4799 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4800 && (number_of_program_spaces () > 1
4801 || number_of_inferiors () > 1)
4a64f543
MS
4802 /* LOC is for existing B, it cannot be in
4803 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
4804 && loc->owner->type != bp_catchpoint)))
4805 {
4806 struct inferior *inf;
4807 int first = 1;
4808
4809 for (inf = inferior_list; inf != NULL; inf = inf->next)
4810 {
4811 if (inf->pspace == loc->pspace)
4812 {
4813 if (first)
4814 {
4815 first = 0;
4816 ui_out_text (uiout, " inf ");
4817 }
4818 else
4819 ui_out_text (uiout, ", ");
4820 ui_out_text (uiout, plongest (inf->num));
4821 }
4822 }
4823 }
4824
4a306c9a 4825 if (!part_of_multiple)
c4093a6a 4826 {
4a306c9a
JB
4827 if (b->thread != -1)
4828 {
4829 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 4830 "stop only in" line a little further down. */
4a306c9a
JB
4831 ui_out_text (uiout, " thread ");
4832 ui_out_field_int (uiout, "thread", b->thread);
4833 }
4834 else if (b->task != 0)
4835 {
4836 ui_out_text (uiout, " task ");
4837 ui_out_field_int (uiout, "task", b->task);
4838 }
c4093a6a 4839 }
f1310107 4840
8b93c638 4841 ui_out_text (uiout, "\n");
f1310107 4842
348d480f 4843 if (!part_of_multiple)
f1310107
TJB
4844 b->ops->print_one_detail (b, uiout);
4845
0d381245 4846 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
4847 {
4848 annotate_field (6);
8b93c638 4849 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 4850 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 4851 the frame ID. */
5af949e3
UW
4852 ui_out_field_core_addr (uiout, "frame",
4853 b->gdbarch, b->frame_id.stack_addr);
8b93c638 4854 ui_out_text (uiout, "\n");
c4093a6a
JM
4855 }
4856
28010a5d 4857 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
4858 {
4859 annotate_field (7);
d77f58be 4860 if (is_tracepoint (b))
1042e4c0
SS
4861 ui_out_text (uiout, "\ttrace only if ");
4862 else
4863 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
4864 ui_out_field_string (uiout, "cond", b->cond_string);
4865 ui_out_text (uiout, "\n");
4866 }
4867
0d381245 4868 if (!part_of_multiple && b->thread != -1)
c4093a6a 4869 {
4a64f543 4870 /* FIXME should make an annotation for this. */
8b93c638
JM
4871 ui_out_text (uiout, "\tstop only in thread ");
4872 ui_out_field_int (uiout, "thread", b->thread);
4873 ui_out_text (uiout, "\n");
c4093a6a
JM
4874 }
4875
63c715c6 4876 if (!part_of_multiple && b->hit_count)
c4093a6a 4877 {
4a64f543 4878 /* FIXME should make an annotation for this. */
8b93c638
JM
4879 if (ep_is_catchpoint (b))
4880 ui_out_text (uiout, "\tcatchpoint");
4881 else
4882 ui_out_text (uiout, "\tbreakpoint");
4883 ui_out_text (uiout, " already hit ");
4884 ui_out_field_int (uiout, "times", b->hit_count);
4885 if (b->hit_count == 1)
4886 ui_out_text (uiout, " time\n");
4887 else
4888 ui_out_text (uiout, " times\n");
c4093a6a
JM
4889 }
4890
4a64f543
MS
4891 /* Output the count also if it is zero, but only if this is mi.
4892 FIXME: Should have a better test for this. */
9dc5e2a9 4893 if (ui_out_is_mi_like_p (uiout))
63c715c6 4894 if (!part_of_multiple && b->hit_count == 0)
fb40c209 4895 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 4896
0d381245 4897 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
4898 {
4899 annotate_field (8);
8b93c638
JM
4900 ui_out_text (uiout, "\tignore next ");
4901 ui_out_field_int (uiout, "ignore", b->ignore_count);
4902 ui_out_text (uiout, " hits\n");
c4093a6a 4903 }
059fb39f 4904
9add0f1b 4905 l = b->commands ? b->commands->commands : NULL;
059fb39f 4906 if (!part_of_multiple && l)
c4093a6a 4907 {
3b31d625
EZ
4908 struct cleanup *script_chain;
4909
c4093a6a 4910 annotate_field (9);
3b31d625 4911 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 4912 print_command_lines (uiout, l, 4);
3b31d625 4913 do_cleanups (script_chain);
c4093a6a 4914 }
d24317b4 4915
d9b3f62e 4916 if (is_tracepoint (b))
1042e4c0 4917 {
d9b3f62e
PA
4918 struct tracepoint *t = (struct tracepoint *) b;
4919
4920 if (!part_of_multiple && t->pass_count)
4921 {
4922 annotate_field (10);
4923 ui_out_text (uiout, "\tpass count ");
4924 ui_out_field_int (uiout, "pass", t->pass_count);
4925 ui_out_text (uiout, " \n");
4926 }
1042e4c0
SS
4927 }
4928
d24317b4
VP
4929 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4930 {
3a5c3e22
PA
4931 if (is_watchpoint (b))
4932 {
4933 struct watchpoint *w = (struct watchpoint *) b;
4934
4935 ui_out_field_string (uiout, "original-location", w->exp_string);
4936 }
4937 else if (b->addr_string)
d24317b4 4938 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 4939 }
c4093a6a 4940}
c5aa993b 4941
0d381245
VP
4942static void
4943print_one_breakpoint (struct breakpoint *b,
4a64f543 4944 struct bp_location **last_loc,
6c95b8df 4945 int allflag)
0d381245 4946{
8d3788bd 4947 struct cleanup *bkpt_chain;
79a45e25 4948 struct ui_out *uiout = current_uiout;
8d3788bd
VP
4949
4950 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
4951
12c5a436 4952 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 4953 do_cleanups (bkpt_chain);
0d381245
VP
4954
4955 /* If this breakpoint has custom print function,
4956 it's already printed. Otherwise, print individual
4957 locations, if any. */
4958 if (b->ops == NULL || b->ops->print_one == NULL)
4959 {
4a64f543
MS
4960 /* If breakpoint has a single location that is disabled, we
4961 print it as if it had several locations, since otherwise it's
4962 hard to represent "breakpoint enabled, location disabled"
4963 situation.
4964
4965 Note that while hardware watchpoints have several locations
a3be7890 4966 internally, that's not a property exposed to user. */
0d381245 4967 if (b->loc
a5606eee 4968 && !is_hardware_watchpoint (b)
8d3788bd 4969 && (b->loc->next || !b->loc->enabled))
0d381245
VP
4970 {
4971 struct bp_location *loc;
4972 int n = 1;
8d3788bd 4973
0d381245 4974 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
4975 {
4976 struct cleanup *inner2 =
4977 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
4978 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
4979 do_cleanups (inner2);
4980 }
0d381245
VP
4981 }
4982 }
4983}
4984
a6d9a66e
UW
4985static int
4986breakpoint_address_bits (struct breakpoint *b)
4987{
4988 int print_address_bits = 0;
4989 struct bp_location *loc;
4990
4991 for (loc = b->loc; loc; loc = loc->next)
4992 {
c7437ca6
PA
4993 int addr_bit;
4994
4995 /* Software watchpoints that aren't watching memory don't have
4996 an address to print. */
4997 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
4998 continue;
4999
5000 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5001 if (addr_bit > print_address_bits)
5002 print_address_bits = addr_bit;
5003 }
5004
5005 return print_address_bits;
5006}
0d381245 5007
c4093a6a
JM
5008struct captured_breakpoint_query_args
5009 {
5010 int bnum;
5011 };
c5aa993b 5012
c4093a6a 5013static int
2b65245e 5014do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5015{
5016 struct captured_breakpoint_query_args *args = data;
52f0bd74 5017 struct breakpoint *b;
a6d9a66e 5018 struct bp_location *dummy_loc = NULL;
cc59ec59 5019
c4093a6a
JM
5020 ALL_BREAKPOINTS (b)
5021 {
5022 if (args->bnum == b->number)
c5aa993b 5023 {
12c5a436 5024 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5025 return GDB_RC_OK;
c5aa993b 5026 }
c4093a6a
JM
5027 }
5028 return GDB_RC_NONE;
5029}
c5aa993b 5030
c4093a6a 5031enum gdb_rc
4a64f543
MS
5032gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5033 char **error_message)
c4093a6a
JM
5034{
5035 struct captured_breakpoint_query_args args;
cc59ec59 5036
c4093a6a
JM
5037 args.bnum = bnum;
5038 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5039 an error. */
b0b13bb4
DJ
5040 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5041 error_message, RETURN_MASK_ALL) < 0)
5042 return GDB_RC_FAIL;
5043 else
5044 return GDB_RC_OK;
c4093a6a 5045}
c5aa993b 5046
09d682a4
TT
5047/* Return true if this breakpoint was set by the user, false if it is
5048 internal or momentary. */
5049
5050int
5051user_breakpoint_p (struct breakpoint *b)
5052{
46c6471b 5053 return b->number > 0;
09d682a4
TT
5054}
5055
7f3b0473 5056/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5057 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5058 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5059 FILTER is non-NULL, call it on each breakpoint and only include the
5060 ones for which it returns non-zero. Return the total number of
5061 breakpoints listed. */
c906108c 5062
d77f58be 5063static int
e5a67952 5064breakpoint_1 (char *args, int allflag,
4a64f543 5065 int (*filter) (const struct breakpoint *))
c4093a6a 5066{
52f0bd74 5067 struct breakpoint *b;
a6d9a66e 5068 struct bp_location *last_loc = NULL;
7f3b0473 5069 int nr_printable_breakpoints;
3b31d625 5070 struct cleanup *bkpttbl_chain;
79a45b7d 5071 struct value_print_options opts;
a6d9a66e 5072 int print_address_bits = 0;
269b11a2 5073 int print_type_col_width = 14;
79a45e25 5074 struct ui_out *uiout = current_uiout;
269b11a2 5075
79a45b7d
TT
5076 get_user_print_options (&opts);
5077
4a64f543
MS
5078 /* Compute the number of rows in the table, as well as the size
5079 required for address fields. */
7f3b0473
AC
5080 nr_printable_breakpoints = 0;
5081 ALL_BREAKPOINTS (b)
e5a67952
MS
5082 {
5083 /* If we have a filter, only list the breakpoints it accepts. */
5084 if (filter && !filter (b))
5085 continue;
5086
5087 /* If we have an "args" string, it is a list of breakpoints to
5088 accept. Skip the others. */
5089 if (args != NULL && *args != '\0')
5090 {
5091 if (allflag && parse_and_eval_long (args) != b->number)
5092 continue;
5093 if (!allflag && !number_is_in_list (args, b->number))
5094 continue;
5095 }
269b11a2 5096
e5a67952
MS
5097 if (allflag || user_breakpoint_p (b))
5098 {
5099 int addr_bit, type_len;
a6d9a66e 5100
e5a67952
MS
5101 addr_bit = breakpoint_address_bits (b);
5102 if (addr_bit > print_address_bits)
5103 print_address_bits = addr_bit;
269b11a2 5104
e5a67952
MS
5105 type_len = strlen (bptype_string (b->type));
5106 if (type_len > print_type_col_width)
5107 print_type_col_width = type_len;
5108
5109 nr_printable_breakpoints++;
5110 }
5111 }
7f3b0473 5112
79a45b7d 5113 if (opts.addressprint)
3b31d625 5114 bkpttbl_chain
3e43a32a
MS
5115 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5116 nr_printable_breakpoints,
3b31d625 5117 "BreakpointTable");
8b93c638 5118 else
3b31d625 5119 bkpttbl_chain
3e43a32a
MS
5120 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5121 nr_printable_breakpoints,
3b31d625 5122 "BreakpointTable");
8b93c638 5123
7f3b0473 5124 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5125 annotate_breakpoints_headers ();
5126 if (nr_printable_breakpoints > 0)
5127 annotate_field (0);
4a64f543 5128 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5129 if (nr_printable_breakpoints > 0)
5130 annotate_field (1);
269b11a2 5131 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 5132 "type", "Type"); /* 2 */
d7faa9e7
AC
5133 if (nr_printable_breakpoints > 0)
5134 annotate_field (2);
4a64f543 5135 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
5136 if (nr_printable_breakpoints > 0)
5137 annotate_field (3);
54e52265 5138 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 5139 if (opts.addressprint)
e5a67952
MS
5140 {
5141 if (nr_printable_breakpoints > 0)
5142 annotate_field (4);
5143 if (print_address_bits <= 32)
5144 ui_out_table_header (uiout, 10, ui_left,
5145 "addr", "Address"); /* 5 */
5146 else
5147 ui_out_table_header (uiout, 18, ui_left,
5148 "addr", "Address"); /* 5 */
5149 }
d7faa9e7
AC
5150 if (nr_printable_breakpoints > 0)
5151 annotate_field (5);
5152 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
5153 ui_out_table_body (uiout);
5154 if (nr_printable_breakpoints > 0)
5155 annotate_breakpoints_table ();
7f3b0473 5156
c4093a6a 5157 ALL_BREAKPOINTS (b)
e5a67952
MS
5158 {
5159 QUIT;
5160 /* If we have a filter, only list the breakpoints it accepts. */
5161 if (filter && !filter (b))
5162 continue;
5163
5164 /* If we have an "args" string, it is a list of breakpoints to
5165 accept. Skip the others. */
5166
5167 if (args != NULL && *args != '\0')
5168 {
5169 if (allflag) /* maintenance info breakpoint */
5170 {
5171 if (parse_and_eval_long (args) != b->number)
5172 continue;
5173 }
5174 else /* all others */
5175 {
5176 if (!number_is_in_list (args, b->number))
5177 continue;
5178 }
5179 }
5180 /* We only print out user settable breakpoints unless the
5181 allflag is set. */
5182 if (allflag || user_breakpoint_p (b))
12c5a436 5183 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
5184 }
5185
3b31d625 5186 do_cleanups (bkpttbl_chain);
698384cd 5187
7f3b0473 5188 if (nr_printable_breakpoints == 0)
c906108c 5189 {
4a64f543
MS
5190 /* If there's a filter, let the caller decide how to report
5191 empty list. */
d77f58be
SS
5192 if (!filter)
5193 {
e5a67952 5194 if (args == NULL || *args == '\0')
d77f58be
SS
5195 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
5196 else
4a64f543 5197 ui_out_message (uiout, 0,
e5a67952
MS
5198 "No breakpoint or watchpoint matching '%s'.\n",
5199 args);
d77f58be 5200 }
c906108c
SS
5201 }
5202 else
c4093a6a 5203 {
a6d9a66e
UW
5204 if (last_loc && !server_command)
5205 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 5206 }
c906108c 5207
4a64f543 5208 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 5209 there have been breakpoints? */
c906108c 5210 annotate_breakpoints_table_end ();
d77f58be
SS
5211
5212 return nr_printable_breakpoints;
c906108c
SS
5213}
5214
ad443146
SS
5215/* Display the value of default-collect in a way that is generally
5216 compatible with the breakpoint list. */
5217
5218static void
5219default_collect_info (void)
5220{
79a45e25
PA
5221 struct ui_out *uiout = current_uiout;
5222
ad443146
SS
5223 /* If it has no value (which is frequently the case), say nothing; a
5224 message like "No default-collect." gets in user's face when it's
5225 not wanted. */
5226 if (!*default_collect)
5227 return;
5228
5229 /* The following phrase lines up nicely with per-tracepoint collect
5230 actions. */
5231 ui_out_text (uiout, "default collect ");
5232 ui_out_field_string (uiout, "default-collect", default_collect);
5233 ui_out_text (uiout, " \n");
5234}
5235
c906108c 5236static void
e5a67952 5237breakpoints_info (char *args, int from_tty)
c906108c 5238{
e5a67952 5239 breakpoint_1 (args, 0, NULL);
ad443146
SS
5240
5241 default_collect_info ();
d77f58be
SS
5242}
5243
5244static void
e5a67952 5245watchpoints_info (char *args, int from_tty)
d77f58be 5246{
e5a67952 5247 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 5248 struct ui_out *uiout = current_uiout;
d77f58be
SS
5249
5250 if (num_printed == 0)
5251 {
e5a67952 5252 if (args == NULL || *args == '\0')
d77f58be
SS
5253 ui_out_message (uiout, 0, "No watchpoints.\n");
5254 else
e5a67952 5255 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 5256 }
c906108c
SS
5257}
5258
7a292a7a 5259static void
e5a67952 5260maintenance_info_breakpoints (char *args, int from_tty)
c906108c 5261{
e5a67952 5262 breakpoint_1 (args, 1, NULL);
ad443146
SS
5263
5264 default_collect_info ();
c906108c
SS
5265}
5266
0d381245 5267static int
714835d5 5268breakpoint_has_pc (struct breakpoint *b,
6c95b8df 5269 struct program_space *pspace,
714835d5 5270 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
5271{
5272 struct bp_location *bl = b->loc;
cc59ec59 5273
0d381245
VP
5274 for (; bl; bl = bl->next)
5275 {
6c95b8df
PA
5276 if (bl->pspace == pspace
5277 && bl->address == pc
0d381245
VP
5278 && (!overlay_debugging || bl->section == section))
5279 return 1;
5280 }
5281 return 0;
5282}
5283
672f9b60 5284/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
5285 concerns with logical breakpoints, so we match program spaces, not
5286 address spaces. */
c906108c
SS
5287
5288static void
6c95b8df
PA
5289describe_other_breakpoints (struct gdbarch *gdbarch,
5290 struct program_space *pspace, CORE_ADDR pc,
5af949e3 5291 struct obj_section *section, int thread)
c906108c 5292{
52f0bd74
AC
5293 int others = 0;
5294 struct breakpoint *b;
c906108c
SS
5295
5296 ALL_BREAKPOINTS (b)
672f9b60
KP
5297 others += (user_breakpoint_p (b)
5298 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
5299 if (others > 0)
5300 {
a3f17187
AC
5301 if (others == 1)
5302 printf_filtered (_("Note: breakpoint "));
5303 else /* if (others == ???) */
5304 printf_filtered (_("Note: breakpoints "));
c906108c 5305 ALL_BREAKPOINTS (b)
672f9b60 5306 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
5307 {
5308 others--;
5309 printf_filtered ("%d", b->number);
5310 if (b->thread == -1 && thread != -1)
5311 printf_filtered (" (all threads)");
5312 else if (b->thread != -1)
5313 printf_filtered (" (thread %d)", b->thread);
5314 printf_filtered ("%s%s ",
059fb39f 5315 ((b->enable_state == bp_disabled
8bea4e01
UW
5316 || b->enable_state == bp_call_disabled
5317 || b->enable_state == bp_startup_disabled)
0d381245
VP
5318 ? " (disabled)"
5319 : b->enable_state == bp_permanent
5320 ? " (permanent)"
5321 : ""),
5322 (others > 1) ? ","
5323 : ((others == 1) ? " and" : ""));
5324 }
a3f17187 5325 printf_filtered (_("also set at pc "));
5af949e3 5326 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
5327 printf_filtered (".\n");
5328 }
5329}
5330\f
c906108c 5331
e4f237da
KB
5332/* Return true iff it is meaningful to use the address member of
5333 BPT. For some breakpoint types, the address member is irrelevant
5334 and it makes no sense to attempt to compare it to other addresses
5335 (or use it for any other purpose either).
5336
4a64f543
MS
5337 More specifically, each of the following breakpoint types will
5338 always have a zero valued address and we don't want to mark
5339 breakpoints of any of these types to be a duplicate of an actual
5340 breakpoint at address zero:
e4f237da
KB
5341
5342 bp_watchpoint
2d134ed3
PA
5343 bp_catchpoint
5344
5345*/
e4f237da
KB
5346
5347static int
5348breakpoint_address_is_meaningful (struct breakpoint *bpt)
5349{
5350 enum bptype type = bpt->type;
5351
2d134ed3
PA
5352 return (type != bp_watchpoint && type != bp_catchpoint);
5353}
5354
5355/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
5356 true if LOC1 and LOC2 represent the same watchpoint location. */
5357
5358static int
4a64f543
MS
5359watchpoint_locations_match (struct bp_location *loc1,
5360 struct bp_location *loc2)
2d134ed3 5361{
3a5c3e22
PA
5362 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
5363 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
5364
5365 /* Both of them must exist. */
5366 gdb_assert (w1 != NULL);
5367 gdb_assert (w2 != NULL);
2bdf28a0 5368
4a64f543
MS
5369 /* If the target can evaluate the condition expression in hardware,
5370 then we we need to insert both watchpoints even if they are at
5371 the same place. Otherwise the watchpoint will only trigger when
5372 the condition of whichever watchpoint was inserted evaluates to
5373 true, not giving a chance for GDB to check the condition of the
5374 other watchpoint. */
3a5c3e22 5375 if ((w1->cond_exp
4a64f543
MS
5376 && target_can_accel_watchpoint_condition (loc1->address,
5377 loc1->length,
0cf6dd15 5378 loc1->watchpoint_type,
3a5c3e22
PA
5379 w1->cond_exp))
5380 || (w2->cond_exp
4a64f543
MS
5381 && target_can_accel_watchpoint_condition (loc2->address,
5382 loc2->length,
0cf6dd15 5383 loc2->watchpoint_type,
3a5c3e22 5384 w2->cond_exp)))
0cf6dd15
TJB
5385 return 0;
5386
85d721b8
PA
5387 /* Note that this checks the owner's type, not the location's. In
5388 case the target does not support read watchpoints, but does
5389 support access watchpoints, we'll have bp_read_watchpoint
5390 watchpoints with hw_access locations. Those should be considered
5391 duplicates of hw_read locations. The hw_read locations will
5392 become hw_access locations later. */
2d134ed3
PA
5393 return (loc1->owner->type == loc2->owner->type
5394 && loc1->pspace->aspace == loc2->pspace->aspace
5395 && loc1->address == loc2->address
5396 && loc1->length == loc2->length);
e4f237da
KB
5397}
5398
6c95b8df
PA
5399/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
5400 same breakpoint location. In most targets, this can only be true
5401 if ASPACE1 matches ASPACE2. On targets that have global
5402 breakpoints, the address space doesn't really matter. */
5403
5404static int
5405breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
5406 struct address_space *aspace2, CORE_ADDR addr2)
5407{
5408 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5409 || aspace1 == aspace2)
5410 && addr1 == addr2);
5411}
5412
f1310107
TJB
5413/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
5414 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
5415 matches ASPACE2. On targets that have global breakpoints, the address
5416 space doesn't really matter. */
5417
5418static int
5419breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
5420 int len1, struct address_space *aspace2,
5421 CORE_ADDR addr2)
5422{
5423 return ((gdbarch_has_global_breakpoints (target_gdbarch)
5424 || aspace1 == aspace2)
5425 && addr2 >= addr1 && addr2 < addr1 + len1);
5426}
5427
5428/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
5429 a ranged breakpoint. In most targets, a match happens only if ASPACE
5430 matches the breakpoint's address space. On targets that have global
5431 breakpoints, the address space doesn't really matter. */
5432
5433static int
5434breakpoint_location_address_match (struct bp_location *bl,
5435 struct address_space *aspace,
5436 CORE_ADDR addr)
5437{
5438 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
5439 aspace, addr)
5440 || (bl->length
5441 && breakpoint_address_match_range (bl->pspace->aspace,
5442 bl->address, bl->length,
5443 aspace, addr)));
5444}
5445
2d134ed3
PA
5446/* Assuming LOC1 and LOC2's types' have meaningful target addresses
5447 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
5448 represent the same location. */
5449
5450static int
4a64f543
MS
5451breakpoint_locations_match (struct bp_location *loc1,
5452 struct bp_location *loc2)
2d134ed3 5453{
2bdf28a0
JK
5454 int hw_point1, hw_point2;
5455
5456 /* Both of them must not be in moribund_locations. */
5457 gdb_assert (loc1->owner != NULL);
5458 gdb_assert (loc2->owner != NULL);
5459
5460 hw_point1 = is_hardware_watchpoint (loc1->owner);
5461 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
5462
5463 if (hw_point1 != hw_point2)
5464 return 0;
5465 else if (hw_point1)
5466 return watchpoint_locations_match (loc1, loc2);
5467 else
f1310107
TJB
5468 /* We compare bp_location.length in order to cover ranged breakpoints. */
5469 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
5470 loc2->pspace->aspace, loc2->address)
5471 && loc1->length == loc2->length);
2d134ed3
PA
5472}
5473
76897487
KB
5474static void
5475breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
5476 int bnum, int have_bnum)
5477{
f63fbe86
MS
5478 /* The longest string possibly returned by hex_string_custom
5479 is 50 chars. These must be at least that big for safety. */
5480 char astr1[64];
5481 char astr2[64];
76897487 5482
bb599908
PH
5483 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
5484 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 5485 if (have_bnum)
8a3fe4f8 5486 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
5487 bnum, astr1, astr2);
5488 else
8a3fe4f8 5489 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
5490}
5491
4a64f543
MS
5492/* Adjust a breakpoint's address to account for architectural
5493 constraints on breakpoint placement. Return the adjusted address.
5494 Note: Very few targets require this kind of adjustment. For most
5495 targets, this function is simply the identity function. */
76897487
KB
5496
5497static CORE_ADDR
a6d9a66e
UW
5498adjust_breakpoint_address (struct gdbarch *gdbarch,
5499 CORE_ADDR bpaddr, enum bptype bptype)
76897487 5500{
a6d9a66e 5501 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
5502 {
5503 /* Very few targets need any kind of breakpoint adjustment. */
5504 return bpaddr;
5505 }
88f7da05
KB
5506 else if (bptype == bp_watchpoint
5507 || bptype == bp_hardware_watchpoint
5508 || bptype == bp_read_watchpoint
5509 || bptype == bp_access_watchpoint
fe798b75 5510 || bptype == bp_catchpoint)
88f7da05
KB
5511 {
5512 /* Watchpoints and the various bp_catch_* eventpoints should not
5513 have their addresses modified. */
5514 return bpaddr;
5515 }
76897487
KB
5516 else
5517 {
5518 CORE_ADDR adjusted_bpaddr;
5519
5520 /* Some targets have architectural constraints on the placement
5521 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 5522 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
5523
5524 /* An adjusted breakpoint address can significantly alter
5525 a user's expectations. Print a warning if an adjustment
5526 is required. */
5527 if (adjusted_bpaddr != bpaddr)
5528 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
5529
5530 return adjusted_bpaddr;
5531 }
5532}
5533
28010a5d
PA
5534void
5535init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
5536 struct breakpoint *owner)
7cc221ef 5537{
7cc221ef
DJ
5538 memset (loc, 0, sizeof (*loc));
5539
348d480f
PA
5540 gdb_assert (ops != NULL);
5541
28010a5d
PA
5542 loc->ops = ops;
5543 loc->owner = owner;
511a6cd4 5544 loc->cond = NULL;
0d381245
VP
5545 loc->shlib_disabled = 0;
5546 loc->enabled = 1;
e049a4b5 5547
28010a5d 5548 switch (owner->type)
e049a4b5
DJ
5549 {
5550 case bp_breakpoint:
5551 case bp_until:
5552 case bp_finish:
5553 case bp_longjmp:
5554 case bp_longjmp_resume:
186c406b
TT
5555 case bp_exception:
5556 case bp_exception_resume:
e049a4b5 5557 case bp_step_resume:
2c03e5be 5558 case bp_hp_step_resume:
e049a4b5
DJ
5559 case bp_watchpoint_scope:
5560 case bp_call_dummy:
aa7d318d 5561 case bp_std_terminate:
e049a4b5
DJ
5562 case bp_shlib_event:
5563 case bp_thread_event:
5564 case bp_overlay_event:
4efc6507 5565 case bp_jit_event:
0fd8e87f 5566 case bp_longjmp_master:
aa7d318d 5567 case bp_std_terminate_master:
186c406b 5568 case bp_exception_master:
0e30163f
JK
5569 case bp_gnu_ifunc_resolver:
5570 case bp_gnu_ifunc_resolver_return:
e049a4b5
DJ
5571 loc->loc_type = bp_loc_software_breakpoint;
5572 break;
5573 case bp_hardware_breakpoint:
5574 loc->loc_type = bp_loc_hardware_breakpoint;
5575 break;
5576 case bp_hardware_watchpoint:
5577 case bp_read_watchpoint:
5578 case bp_access_watchpoint:
5579 loc->loc_type = bp_loc_hardware_watchpoint;
5580 break;
5581 case bp_watchpoint:
ce78b96d 5582 case bp_catchpoint:
15c3d785
PA
5583 case bp_tracepoint:
5584 case bp_fast_tracepoint:
0fb4aa4b 5585 case bp_static_tracepoint:
e049a4b5
DJ
5586 loc->loc_type = bp_loc_other;
5587 break;
5588 default:
e2e0b3e5 5589 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
5590 }
5591
f431efe5 5592 loc->refc = 1;
28010a5d
PA
5593}
5594
5595/* Allocate a struct bp_location. */
5596
5597static struct bp_location *
5598allocate_bp_location (struct breakpoint *bpt)
5599{
348d480f
PA
5600 return bpt->ops->allocate_location (bpt);
5601}
7cc221ef 5602
f431efe5
PA
5603static void
5604free_bp_location (struct bp_location *loc)
fe3f5fa8 5605{
348d480f 5606 loc->ops->dtor (loc);
fe3f5fa8
VP
5607 xfree (loc);
5608}
5609
f431efe5
PA
5610/* Increment reference count. */
5611
5612static void
5613incref_bp_location (struct bp_location *bl)
5614{
5615 ++bl->refc;
5616}
5617
5618/* Decrement reference count. If the reference count reaches 0,
5619 destroy the bp_location. Sets *BLP to NULL. */
5620
5621static void
5622decref_bp_location (struct bp_location **blp)
5623{
0807b50c
PA
5624 gdb_assert ((*blp)->refc > 0);
5625
f431efe5
PA
5626 if (--(*blp)->refc == 0)
5627 free_bp_location (*blp);
5628 *blp = NULL;
5629}
5630
346774a9 5631/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 5632
346774a9
PA
5633static void
5634add_to_breakpoint_chain (struct breakpoint *b)
c906108c 5635{
346774a9 5636 struct breakpoint *b1;
c906108c 5637
346774a9
PA
5638 /* Add this breakpoint to the end of the chain so that a list of
5639 breakpoints will come out in order of increasing numbers. */
5640
5641 b1 = breakpoint_chain;
5642 if (b1 == 0)
5643 breakpoint_chain = b;
5644 else
5645 {
5646 while (b1->next)
5647 b1 = b1->next;
5648 b1->next = b;
5649 }
5650}
5651
5652/* Initializes breakpoint B with type BPTYPE and no locations yet. */
5653
5654static void
5655init_raw_breakpoint_without_location (struct breakpoint *b,
5656 struct gdbarch *gdbarch,
28010a5d 5657 enum bptype bptype,
c0a91b2b 5658 const struct breakpoint_ops *ops)
346774a9 5659{
c906108c 5660 memset (b, 0, sizeof (*b));
2219d63c 5661
348d480f
PA
5662 gdb_assert (ops != NULL);
5663
28010a5d 5664 b->ops = ops;
4d28f7a8 5665 b->type = bptype;
a6d9a66e 5666 b->gdbarch = gdbarch;
c906108c
SS
5667 b->language = current_language->la_language;
5668 b->input_radix = input_radix;
5669 b->thread = -1;
b5de0fa7 5670 b->enable_state = bp_enabled;
c906108c
SS
5671 b->next = 0;
5672 b->silent = 0;
5673 b->ignore_count = 0;
5674 b->commands = NULL;
818dd999 5675 b->frame_id = null_frame_id;
0d381245 5676 b->condition_not_parsed = 0;
84f4c1fe 5677 b->py_bp_object = NULL;
d0fb5eae 5678 b->related_breakpoint = b;
346774a9
PA
5679}
5680
5681/* Helper to set_raw_breakpoint below. Creates a breakpoint
5682 that has type BPTYPE and has no locations as yet. */
346774a9
PA
5683
5684static struct breakpoint *
5685set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 5686 enum bptype bptype,
c0a91b2b 5687 const struct breakpoint_ops *ops)
346774a9
PA
5688{
5689 struct breakpoint *b = XNEW (struct breakpoint);
5690
348d480f 5691 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 5692 add_to_breakpoint_chain (b);
0d381245
VP
5693 return b;
5694}
5695
0e30163f
JK
5696/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
5697 resolutions should be made as the user specified the location explicitly
5698 enough. */
5699
0d381245 5700static void
0e30163f 5701set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 5702{
2bdf28a0
JK
5703 gdb_assert (loc->owner != NULL);
5704
0d381245 5705 if (loc->owner->type == bp_breakpoint
1042e4c0 5706 || loc->owner->type == bp_hardware_breakpoint
d77f58be 5707 || is_tracepoint (loc->owner))
0d381245 5708 {
0e30163f
JK
5709 int is_gnu_ifunc;
5710
5711 find_pc_partial_function_gnu_ifunc (loc->address, &loc->function_name,
5712 NULL, NULL, &is_gnu_ifunc);
5713
5714 if (is_gnu_ifunc && !explicit_loc)
5715 {
5716 struct breakpoint *b = loc->owner;
5717
5718 gdb_assert (loc->pspace == current_program_space);
5719 if (gnu_ifunc_resolve_name (loc->function_name,
5720 &loc->requested_address))
5721 {
5722 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
5723 loc->address = adjust_breakpoint_address (loc->gdbarch,
5724 loc->requested_address,
5725 b->type);
5726 }
5727 else if (b->type == bp_breakpoint && b->loc == loc
5728 && loc->next == NULL && b->related_breakpoint == b)
5729 {
5730 /* Create only the whole new breakpoint of this type but do not
5731 mess more complicated breakpoints with multiple locations. */
5732 b->type = bp_gnu_ifunc_resolver;
5733 }
5734 }
5735
0d381245
VP
5736 if (loc->function_name)
5737 loc->function_name = xstrdup (loc->function_name);
5738 }
5739}
5740
a6d9a66e 5741/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 5742struct gdbarch *
a6d9a66e
UW
5743get_sal_arch (struct symtab_and_line sal)
5744{
5745 if (sal.section)
5746 return get_objfile_arch (sal.section->objfile);
5747 if (sal.symtab)
5748 return get_objfile_arch (sal.symtab->objfile);
5749
5750 return NULL;
5751}
5752
346774a9
PA
5753/* Low level routine for partially initializing a breakpoint of type
5754 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 5755 file name, and line number are provided by SAL.
0d381245
VP
5756
5757 It is expected that the caller will complete the initialization of
5758 the newly created breakpoint struct as well as output any status
c56053d2 5759 information regarding the creation of a new breakpoint. */
0d381245 5760
346774a9
PA
5761static void
5762init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 5763 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5764 const struct breakpoint_ops *ops)
0d381245 5765{
0d381245 5766 CORE_ADDR adjusted_address;
a6d9a66e
UW
5767 struct gdbarch *loc_gdbarch;
5768
28010a5d 5769 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 5770
a6d9a66e
UW
5771 loc_gdbarch = get_sal_arch (sal);
5772 if (!loc_gdbarch)
5773 loc_gdbarch = b->gdbarch;
0d381245 5774
6c95b8df
PA
5775 if (bptype != bp_catchpoint)
5776 gdb_assert (sal.pspace != NULL);
5777
0d381245
VP
5778 /* Adjust the breakpoint's address prior to allocating a location.
5779 Once we call allocate_bp_location(), that mostly uninitialized
5780 location will be placed on the location chain. Adjustment of the
8defab1a 5781 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
5782 not want its scan of the location chain to find a breakpoint and
5783 location that's only been partially initialized. */
4a64f543
MS
5784 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
5785 sal.pc, b->type);
0d381245 5786
39d61571 5787 b->loc = allocate_bp_location (b);
a6d9a66e 5788 b->loc->gdbarch = loc_gdbarch;
0d381245
VP
5789 b->loc->requested_address = sal.pc;
5790 b->loc->address = adjusted_address;
6c95b8df
PA
5791 b->loc->pspace = sal.pspace;
5792
5793 /* Store the program space that was used to set the breakpoint, for
5794 breakpoint resetting. */
5795 b->pspace = sal.pspace;
0d381245
VP
5796
5797 if (sal.symtab == NULL)
5798 b->source_file = NULL;
5799 else
1b36a34b 5800 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
5801 b->loc->section = sal.section;
5802 b->line_number = sal.line;
5803
0e30163f
JK
5804 set_breakpoint_location_function (b->loc,
5805 sal.explicit_pc || sal.explicit_line);
c906108c 5806
c906108c 5807 breakpoints_changed ();
346774a9 5808}
c906108c 5809
346774a9
PA
5810/* set_raw_breakpoint is a low level routine for allocating and
5811 partially initializing a breakpoint of type BPTYPE. The newly
5812 created breakpoint's address, section, source file name, and line
5813 number are provided by SAL. The newly created and partially
5814 initialized breakpoint is added to the breakpoint chain and
5815 is also returned as the value of this function.
5816
5817 It is expected that the caller will complete the initialization of
5818 the newly created breakpoint struct as well as output any status
5819 information regarding the creation of a new breakpoint. In
5820 particular, set_raw_breakpoint does NOT set the breakpoint
5821 number! Care should be taken to not allow an error to occur
5822 prior to completing the initialization of the breakpoint. If this
5823 should happen, a bogus breakpoint will be left on the chain. */
5824
5825struct breakpoint *
5826set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 5827 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 5828 const struct breakpoint_ops *ops)
346774a9
PA
5829{
5830 struct breakpoint *b = XNEW (struct breakpoint);
5831
348d480f 5832 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 5833 add_to_breakpoint_chain (b);
c906108c
SS
5834 return b;
5835}
5836
c2c6d25f
JM
5837
5838/* Note that the breakpoint object B describes a permanent breakpoint
5839 instruction, hard-wired into the inferior's code. */
5840void
5841make_breakpoint_permanent (struct breakpoint *b)
5842{
0d381245 5843 struct bp_location *bl;
cc59ec59 5844
b5de0fa7 5845 b->enable_state = bp_permanent;
c2c6d25f 5846
4a64f543
MS
5847 /* By definition, permanent breakpoints are already present in the
5848 code. Mark all locations as inserted. For now,
5849 make_breakpoint_permanent is called in just one place, so it's
5850 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 5851 multiple locations or not, but it's easy to implement. */
0d381245
VP
5852 for (bl = b->loc; bl; bl = bl->next)
5853 bl->inserted = 1;
c2c6d25f
JM
5854}
5855
53a5351d 5856/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
5857 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
5858 initiated the operation. */
c906108c
SS
5859
5860void
186c406b 5861set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 5862{
35df4500 5863 struct breakpoint *b, *b_tmp;
186c406b 5864 int thread = tp->num;
0fd8e87f
UW
5865
5866 /* To avoid having to rescan all objfile symbols at every step,
5867 we maintain a list of continually-inserted but always disabled
5868 longjmp "master" breakpoints. Here, we simply create momentary
5869 clones of those and enable them for the requested thread. */
35df4500 5870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 5871 if (b->pspace == current_program_space
186c406b
TT
5872 && (b->type == bp_longjmp_master
5873 || b->type == bp_exception_master))
0fd8e87f 5874 {
06edf0c0
PA
5875 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
5876 struct breakpoint *clone;
cc59ec59 5877
06edf0c0
PA
5878 clone = momentary_breakpoint_from_master (b, type,
5879 &momentary_breakpoint_ops);
0fd8e87f
UW
5880 clone->thread = thread;
5881 }
186c406b
TT
5882
5883 tp->initiating_frame = frame;
c906108c
SS
5884}
5885
611c83ae 5886/* Delete all longjmp breakpoints from THREAD. */
c906108c 5887void
611c83ae 5888delete_longjmp_breakpoint (int thread)
c906108c 5889{
35df4500 5890 struct breakpoint *b, *b_tmp;
c906108c 5891
35df4500 5892 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 5893 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
5894 {
5895 if (b->thread == thread)
5896 delete_breakpoint (b);
5897 }
c906108c
SS
5898}
5899
1900040c
MS
5900void
5901enable_overlay_breakpoints (void)
5902{
52f0bd74 5903 struct breakpoint *b;
1900040c
MS
5904
5905 ALL_BREAKPOINTS (b)
5906 if (b->type == bp_overlay_event)
5907 {
5908 b->enable_state = bp_enabled;
b60e7edf 5909 update_global_location_list (1);
c02f5703 5910 overlay_events_enabled = 1;
1900040c
MS
5911 }
5912}
5913
5914void
5915disable_overlay_breakpoints (void)
5916{
52f0bd74 5917 struct breakpoint *b;
1900040c
MS
5918
5919 ALL_BREAKPOINTS (b)
5920 if (b->type == bp_overlay_event)
5921 {
5922 b->enable_state = bp_disabled;
b60e7edf 5923 update_global_location_list (0);
c02f5703 5924 overlay_events_enabled = 0;
1900040c
MS
5925 }
5926}
5927
aa7d318d
TT
5928/* Set an active std::terminate breakpoint for each std::terminate
5929 master breakpoint. */
5930void
5931set_std_terminate_breakpoint (void)
5932{
35df4500 5933 struct breakpoint *b, *b_tmp;
aa7d318d 5934
35df4500 5935 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5936 if (b->pspace == current_program_space
5937 && b->type == bp_std_terminate_master)
5938 {
06edf0c0
PA
5939 momentary_breakpoint_from_master (b, bp_std_terminate,
5940 &momentary_breakpoint_ops);
aa7d318d
TT
5941 }
5942}
5943
5944/* Delete all the std::terminate breakpoints. */
5945void
5946delete_std_terminate_breakpoint (void)
5947{
35df4500 5948 struct breakpoint *b, *b_tmp;
aa7d318d 5949
35df4500 5950 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
5951 if (b->type == bp_std_terminate)
5952 delete_breakpoint (b);
5953}
5954
c4093a6a 5955struct breakpoint *
a6d9a66e 5956create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
5957{
5958 struct breakpoint *b;
c4093a6a 5959
06edf0c0
PA
5960 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
5961 &internal_breakpoint_ops);
5962
b5de0fa7 5963 b->enable_state = bp_enabled;
c4093a6a 5964 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
5965 b->addr_string
5966 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 5967
b60e7edf 5968 update_global_location_list_nothrow (1);
74960c60 5969
c4093a6a
JM
5970 return b;
5971}
5972
5973void
5974remove_thread_event_breakpoints (void)
5975{
35df4500 5976 struct breakpoint *b, *b_tmp;
c4093a6a 5977
35df4500 5978 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
5979 if (b->type == bp_thread_event
5980 && b->loc->pspace == current_program_space)
c4093a6a
JM
5981 delete_breakpoint (b);
5982}
5983
0101ce28
JJ
5984struct lang_and_radix
5985 {
5986 enum language lang;
5987 int radix;
5988 };
5989
4efc6507
DE
5990/* Create a breakpoint for JIT code registration and unregistration. */
5991
5992struct breakpoint *
5993create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
5994{
5995 struct breakpoint *b;
5996
06edf0c0
PA
5997 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
5998 &internal_breakpoint_ops);
4efc6507
DE
5999 update_global_location_list_nothrow (1);
6000 return b;
6001}
0101ce28 6002
03673fc7
PP
6003/* Remove JIT code registration and unregistration breakpoint(s). */
6004
6005void
6006remove_jit_event_breakpoints (void)
6007{
6008 struct breakpoint *b, *b_tmp;
6009
6010 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6011 if (b->type == bp_jit_event
6012 && b->loc->pspace == current_program_space)
6013 delete_breakpoint (b);
6014}
6015
cae688ec
JJ
6016void
6017remove_solib_event_breakpoints (void)
6018{
35df4500 6019 struct breakpoint *b, *b_tmp;
cae688ec 6020
35df4500 6021 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6022 if (b->type == bp_shlib_event
6023 && b->loc->pspace == current_program_space)
cae688ec
JJ
6024 delete_breakpoint (b);
6025}
6026
6027struct breakpoint *
a6d9a66e 6028create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6029{
6030 struct breakpoint *b;
6031
06edf0c0
PA
6032 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6033 &internal_breakpoint_ops);
b60e7edf 6034 update_global_location_list_nothrow (1);
cae688ec
JJ
6035 return b;
6036}
6037
6038/* Disable any breakpoints that are on code in shared libraries. Only
6039 apply to enabled breakpoints, disabled ones can just stay disabled. */
6040
6041void
cb851954 6042disable_breakpoints_in_shlibs (void)
cae688ec 6043{
876fa593 6044 struct bp_location *loc, **locp_tmp;
cae688ec 6045
876fa593 6046 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6047 {
2bdf28a0 6048 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6049 struct breakpoint *b = loc->owner;
2bdf28a0 6050
4a64f543
MS
6051 /* We apply the check to all breakpoints, including disabled for
6052 those with loc->duplicate set. This is so that when breakpoint
6053 becomes enabled, or the duplicate is removed, gdb will try to
6054 insert all breakpoints. If we don't set shlib_disabled here,
6055 we'll try to insert those breakpoints and fail. */
1042e4c0 6056 if (((b->type == bp_breakpoint)
508ccb1f 6057 || (b->type == bp_jit_event)
1042e4c0 6058 || (b->type == bp_hardware_breakpoint)
d77f58be 6059 || (is_tracepoint (b)))
6c95b8df 6060 && loc->pspace == current_program_space
0d381245 6061 && !loc->shlib_disabled
a77053c2 6062#ifdef PC_SOLIB
0d381245 6063 && PC_SOLIB (loc->address)
a77053c2 6064#else
6c95b8df 6065 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6066#endif
6067 )
0d381245
VP
6068 {
6069 loc->shlib_disabled = 1;
6070 }
cae688ec
JJ
6071 }
6072}
6073
7a9dd1b2 6074/* Disable any breakpoints that are in an unloaded shared library.
4a64f543
MS
6075 Only apply to enabled breakpoints, disabled ones can just stay
6076 disabled. */
84acb35a 6077
75149521 6078static void
84acb35a
JJ
6079disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6080{
876fa593 6081 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6082 int disabled_shlib_breaks = 0;
6083
c86cf029
VP
6084 /* SunOS a.out shared libraries are always mapped, so do not
6085 disable breakpoints; they will only be reported as unloaded
6086 through clear_solib when GDB discards its shared library
6087 list. See clear_solib for more information. */
6088 if (exec_bfd != NULL
6089 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6090 return;
6091
876fa593 6092 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6093 {
2bdf28a0 6094 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6095 struct breakpoint *b = loc->owner;
cc59ec59 6096
0d381245
VP
6097 if ((loc->loc_type == bp_loc_hardware_breakpoint
6098 || loc->loc_type == bp_loc_software_breakpoint)
6c95b8df 6099 && solib->pspace == loc->pspace
e2dd7057 6100 && !loc->shlib_disabled
508ccb1f
TT
6101 && (b->type == bp_breakpoint
6102 || b->type == bp_jit_event
6103 || b->type == bp_hardware_breakpoint)
e2dd7057 6104 && solib_contains_address_p (solib, loc->address))
84acb35a 6105 {
e2dd7057
PP
6106 loc->shlib_disabled = 1;
6107 /* At this point, we cannot rely on remove_breakpoint
6108 succeeding so we must mark the breakpoint as not inserted
6109 to prevent future errors occurring in remove_breakpoints. */
6110 loc->inserted = 0;
8d3788bd
VP
6111
6112 /* This may cause duplicate notifications for the same breakpoint. */
6113 observer_notify_breakpoint_modified (b);
6114
e2dd7057
PP
6115 if (!disabled_shlib_breaks)
6116 {
6117 target_terminal_ours_for_output ();
3e43a32a
MS
6118 warning (_("Temporarily disabling breakpoints "
6119 "for unloaded shared library \"%s\""),
e2dd7057 6120 solib->so_name);
84acb35a 6121 }
e2dd7057 6122 disabled_shlib_breaks = 1;
84acb35a
JJ
6123 }
6124 }
84acb35a
JJ
6125}
6126
ce78b96d
JB
6127/* FORK & VFORK catchpoints. */
6128
e29a4733
PA
6129/* An instance of this type is used to represent a fork or vfork
6130 catchpoint. It includes a "struct breakpoint" as a kind of base
6131 class; users downcast to "struct breakpoint *" when needed. A
6132 breakpoint is really of this type iff its ops pointer points to
6133 CATCH_FORK_BREAKPOINT_OPS. */
6134
6135struct fork_catchpoint
6136{
6137 /* The base class. */
6138 struct breakpoint base;
6139
6140 /* Process id of a child process whose forking triggered this
6141 catchpoint. This field is only valid immediately after this
6142 catchpoint has triggered. */
6143 ptid_t forked_inferior_pid;
6144};
6145
4a64f543
MS
6146/* Implement the "insert" breakpoint_ops method for fork
6147 catchpoints. */
ce78b96d 6148
77b06cd7
TJB
6149static int
6150insert_catch_fork (struct bp_location *bl)
ce78b96d 6151{
77b06cd7 6152 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6153}
6154
4a64f543
MS
6155/* Implement the "remove" breakpoint_ops method for fork
6156 catchpoints. */
ce78b96d
JB
6157
6158static int
77b06cd7 6159remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
6160{
6161 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
6162}
6163
6164/* Implement the "breakpoint_hit" breakpoint_ops method for fork
6165 catchpoints. */
6166
6167static int
f1310107
TJB
6168breakpoint_hit_catch_fork (const struct bp_location *bl,
6169 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6170{
e29a4733
PA
6171 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6172
6173 return inferior_has_forked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6174}
6175
4a64f543
MS
6176/* Implement the "print_it" breakpoint_ops method for fork
6177 catchpoints. */
ce78b96d
JB
6178
6179static enum print_stop_action
348d480f 6180print_it_catch_fork (bpstat bs)
ce78b96d 6181{
348d480f
PA
6182 struct breakpoint *b = bs->breakpoint_at;
6183 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 6184
ce78b96d
JB
6185 annotate_catchpoint (b->number);
6186 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
e29a4733 6187 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6188 return PRINT_SRC_AND_LOC;
6189}
6190
4a64f543
MS
6191/* Implement the "print_one" breakpoint_ops method for fork
6192 catchpoints. */
ce78b96d
JB
6193
6194static void
a6d9a66e 6195print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6196{
e29a4733 6197 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6198 struct value_print_options opts;
79a45e25 6199 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6200
6201 get_user_print_options (&opts);
6202
4a64f543
MS
6203 /* Field 4, the address, is omitted (which makes the columns not
6204 line up too nicely with the headers, but the effect is relatively
6205 readable). */
79a45b7d 6206 if (opts.addressprint)
ce78b96d
JB
6207 ui_out_field_skip (uiout, "addr");
6208 annotate_field (5);
6209 ui_out_text (uiout, "fork");
e29a4733 6210 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6211 {
6212 ui_out_text (uiout, ", process ");
6213 ui_out_field_int (uiout, "what",
e29a4733 6214 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6215 ui_out_spaces (uiout, 1);
6216 }
6217}
6218
6219/* Implement the "print_mention" breakpoint_ops method for fork
6220 catchpoints. */
6221
6222static void
6223print_mention_catch_fork (struct breakpoint *b)
6224{
6225 printf_filtered (_("Catchpoint %d (fork)"), b->number);
6226}
6227
6149aea9
PA
6228/* Implement the "print_recreate" breakpoint_ops method for fork
6229 catchpoints. */
6230
6231static void
6232print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
6233{
6234 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 6235 print_recreate_thread (b, fp);
6149aea9
PA
6236}
6237
ce78b96d
JB
6238/* The breakpoint_ops structure to be used in fork catchpoints. */
6239
2060206e 6240static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 6241
4a64f543
MS
6242/* Implement the "insert" breakpoint_ops method for vfork
6243 catchpoints. */
ce78b96d 6244
77b06cd7
TJB
6245static int
6246insert_catch_vfork (struct bp_location *bl)
ce78b96d 6247{
77b06cd7 6248 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
6249}
6250
4a64f543
MS
6251/* Implement the "remove" breakpoint_ops method for vfork
6252 catchpoints. */
ce78b96d
JB
6253
6254static int
77b06cd7 6255remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
6256{
6257 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
6258}
6259
6260/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
6261 catchpoints. */
6262
6263static int
f1310107
TJB
6264breakpoint_hit_catch_vfork (const struct bp_location *bl,
6265 struct address_space *aspace, CORE_ADDR bp_addr)
ce78b96d 6266{
e29a4733
PA
6267 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
6268
6269 return inferior_has_vforked (inferior_ptid, &c->forked_inferior_pid);
ce78b96d
JB
6270}
6271
4a64f543
MS
6272/* Implement the "print_it" breakpoint_ops method for vfork
6273 catchpoints. */
ce78b96d
JB
6274
6275static enum print_stop_action
348d480f 6276print_it_catch_vfork (bpstat bs)
ce78b96d 6277{
348d480f 6278 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
6279 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
6280
ce78b96d
JB
6281 annotate_catchpoint (b->number);
6282 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
e29a4733 6283 b->number, ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6284 return PRINT_SRC_AND_LOC;
6285}
6286
4a64f543
MS
6287/* Implement the "print_one" breakpoint_ops method for vfork
6288 catchpoints. */
ce78b96d
JB
6289
6290static void
a6d9a66e 6291print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 6292{
e29a4733 6293 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 6294 struct value_print_options opts;
79a45e25 6295 struct ui_out *uiout = current_uiout;
79a45b7d
TT
6296
6297 get_user_print_options (&opts);
4a64f543
MS
6298 /* Field 4, the address, is omitted (which makes the columns not
6299 line up too nicely with the headers, but the effect is relatively
6300 readable). */
79a45b7d 6301 if (opts.addressprint)
ce78b96d
JB
6302 ui_out_field_skip (uiout, "addr");
6303 annotate_field (5);
6304 ui_out_text (uiout, "vfork");
e29a4733 6305 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
6306 {
6307 ui_out_text (uiout, ", process ");
6308 ui_out_field_int (uiout, "what",
e29a4733 6309 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
6310 ui_out_spaces (uiout, 1);
6311 }
6312}
6313
6314/* Implement the "print_mention" breakpoint_ops method for vfork
6315 catchpoints. */
6316
6317static void
6318print_mention_catch_vfork (struct breakpoint *b)
6319{
6320 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
6321}
6322
6149aea9
PA
6323/* Implement the "print_recreate" breakpoint_ops method for vfork
6324 catchpoints. */
6325
6326static void
6327print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
6328{
6329 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 6330 print_recreate_thread (b, fp);
6149aea9
PA
6331}
6332
ce78b96d
JB
6333/* The breakpoint_ops structure to be used in vfork catchpoints. */
6334
2060206e 6335static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 6336
be5c67c1
PA
6337/* An instance of this type is used to represent a syscall catchpoint.
6338 It includes a "struct breakpoint" as a kind of base class; users
6339 downcast to "struct breakpoint *" when needed. A breakpoint is
6340 really of this type iff its ops pointer points to
6341 CATCH_SYSCALL_BREAKPOINT_OPS. */
6342
6343struct syscall_catchpoint
6344{
6345 /* The base class. */
6346 struct breakpoint base;
6347
6348 /* Syscall numbers used for the 'catch syscall' feature. If no
6349 syscall has been specified for filtering, its value is NULL.
6350 Otherwise, it holds a list of all syscalls to be caught. The
6351 list elements are allocated with xmalloc. */
6352 VEC(int) *syscalls_to_be_caught;
6353};
6354
6355/* Implement the "dtor" breakpoint_ops method for syscall
6356 catchpoints. */
6357
6358static void
6359dtor_catch_syscall (struct breakpoint *b)
6360{
6361 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6362
6363 VEC_free (int, c->syscalls_to_be_caught);
348d480f 6364
2060206e 6365 base_breakpoint_ops.dtor (b);
be5c67c1
PA
6366}
6367
a96d9b2e
SDJ
6368/* Implement the "insert" breakpoint_ops method for syscall
6369 catchpoints. */
6370
77b06cd7
TJB
6371static int
6372insert_catch_syscall (struct bp_location *bl)
a96d9b2e 6373{
be5c67c1 6374 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6375 struct inferior *inf = current_inferior ();
6376
6377 ++inf->total_syscalls_count;
be5c67c1 6378 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6379 ++inf->any_syscall_count;
6380 else
6381 {
6382 int i, iter;
cc59ec59 6383
a96d9b2e 6384 for (i = 0;
be5c67c1 6385 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6386 i++)
6387 {
6388 int elem;
cc59ec59 6389
a96d9b2e
SDJ
6390 if (iter >= VEC_length (int, inf->syscalls_counts))
6391 {
6392 int old_size = VEC_length (int, inf->syscalls_counts);
3e43a32a
MS
6393 uintptr_t vec_addr_offset
6394 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e
SDJ
6395 uintptr_t vec_addr;
6396 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
6397 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
6398 vec_addr_offset;
6399 memset ((void *) vec_addr, 0,
6400 (iter + 1 - old_size) * sizeof (int));
6401 }
6402 elem = VEC_index (int, inf->syscalls_counts, iter);
6403 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
6404 }
6405 }
6406
77b06cd7
TJB
6407 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6408 inf->total_syscalls_count != 0,
6409 inf->any_syscall_count,
6410 VEC_length (int, inf->syscalls_counts),
6411 VEC_address (int, inf->syscalls_counts));
a96d9b2e
SDJ
6412}
6413
6414/* Implement the "remove" breakpoint_ops method for syscall
6415 catchpoints. */
6416
6417static int
77b06cd7 6418remove_catch_syscall (struct bp_location *bl)
a96d9b2e 6419{
be5c67c1 6420 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6421 struct inferior *inf = current_inferior ();
6422
6423 --inf->total_syscalls_count;
be5c67c1 6424 if (!c->syscalls_to_be_caught)
a96d9b2e
SDJ
6425 --inf->any_syscall_count;
6426 else
6427 {
6428 int i, iter;
cc59ec59 6429
a96d9b2e 6430 for (i = 0;
be5c67c1 6431 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6432 i++)
6433 {
6434 int elem;
6435 if (iter >= VEC_length (int, inf->syscalls_counts))
6436 /* Shouldn't happen. */
6437 continue;
6438 elem = VEC_index (int, inf->syscalls_counts, iter);
6439 VEC_replace (int, inf->syscalls_counts, iter, --elem);
6440 }
6441 }
6442
6443 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
6444 inf->total_syscalls_count != 0,
6445 inf->any_syscall_count,
6446 VEC_length (int, inf->syscalls_counts),
3e43a32a
MS
6447 VEC_address (int,
6448 inf->syscalls_counts));
a96d9b2e
SDJ
6449}
6450
6451/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
6452 catchpoints. */
6453
6454static int
f1310107
TJB
6455breakpoint_hit_catch_syscall (const struct bp_location *bl,
6456 struct address_space *aspace, CORE_ADDR bp_addr)
a96d9b2e 6457{
4a64f543
MS
6458 /* We must check if we are catching specific syscalls in this
6459 breakpoint. If we are, then we must guarantee that the called
6460 syscall is the same syscall we are catching. */
a96d9b2e 6461 int syscall_number = 0;
be5c67c1
PA
6462 const struct syscall_catchpoint *c
6463 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e
SDJ
6464
6465 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
6466 return 0;
6467
6468 /* Now, checking if the syscall is the same. */
be5c67c1 6469 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6470 {
6471 int i, iter;
cc59ec59 6472
a96d9b2e 6473 for (i = 0;
be5c67c1 6474 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6475 i++)
6476 if (syscall_number == iter)
6477 break;
6478 /* Not the same. */
6479 if (!iter)
6480 return 0;
6481 }
6482
6483 return 1;
6484}
6485
6486/* Implement the "print_it" breakpoint_ops method for syscall
6487 catchpoints. */
6488
6489static enum print_stop_action
348d480f 6490print_it_catch_syscall (bpstat bs)
a96d9b2e 6491{
348d480f 6492 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
6493 /* These are needed because we want to know in which state a
6494 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
6495 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
6496 must print "called syscall" or "returned from syscall". */
6497 ptid_t ptid;
6498 struct target_waitstatus last;
6499 struct syscall s;
6500 struct cleanup *old_chain;
6501 char *syscall_id;
6502
6503 get_last_target_status (&ptid, &last);
6504
6505 get_syscall_by_number (last.value.syscall_number, &s);
6506
6507 annotate_catchpoint (b->number);
6508
6509 if (s.name == NULL)
6510 syscall_id = xstrprintf ("%d", last.value.syscall_number);
6511 else
6512 syscall_id = xstrprintf ("'%s'", s.name);
6513
6514 old_chain = make_cleanup (xfree, syscall_id);
6515
6516 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
6517 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
6518 b->number, syscall_id);
6519 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
6520 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
6521 b->number, syscall_id);
6522
6523 do_cleanups (old_chain);
6524
6525 return PRINT_SRC_AND_LOC;
6526}
6527
6528/* Implement the "print_one" breakpoint_ops method for syscall
6529 catchpoints. */
6530
6531static void
6532print_one_catch_syscall (struct breakpoint *b,
f1310107 6533 struct bp_location **last_loc)
a96d9b2e 6534{
be5c67c1 6535 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 6536 struct value_print_options opts;
79a45e25 6537 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
6538
6539 get_user_print_options (&opts);
4a64f543
MS
6540 /* Field 4, the address, is omitted (which makes the columns not
6541 line up too nicely with the headers, but the effect is relatively
6542 readable). */
a96d9b2e
SDJ
6543 if (opts.addressprint)
6544 ui_out_field_skip (uiout, "addr");
6545 annotate_field (5);
6546
be5c67c1
PA
6547 if (c->syscalls_to_be_caught
6548 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6549 ui_out_text (uiout, "syscalls \"");
6550 else
6551 ui_out_text (uiout, "syscall \"");
6552
be5c67c1 6553 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6554 {
6555 int i, iter;
6556 char *text = xstrprintf ("%s", "");
cc59ec59 6557
a96d9b2e 6558 for (i = 0;
be5c67c1 6559 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6560 i++)
6561 {
6562 char *x = text;
6563 struct syscall s;
6564 get_syscall_by_number (iter, &s);
6565
6566 if (s.name != NULL)
6567 text = xstrprintf ("%s%s, ", text, s.name);
6568 else
6569 text = xstrprintf ("%s%d, ", text, iter);
6570
6571 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 6572 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
6573 on every call. */
6574 xfree (x);
6575 }
6576 /* Remove the last comma. */
6577 text[strlen (text) - 2] = '\0';
6578 ui_out_field_string (uiout, "what", text);
6579 }
6580 else
6581 ui_out_field_string (uiout, "what", "<any syscall>");
6582 ui_out_text (uiout, "\" ");
6583}
6584
6585/* Implement the "print_mention" breakpoint_ops method for syscall
6586 catchpoints. */
6587
6588static void
6589print_mention_catch_syscall (struct breakpoint *b)
6590{
be5c67c1
PA
6591 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6592
6593 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
6594 {
6595 int i, iter;
6596
be5c67c1 6597 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
6598 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
6599 else
6600 printf_filtered (_("Catchpoint %d (syscall"), b->number);
6601
6602 for (i = 0;
be5c67c1 6603 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
6604 i++)
6605 {
6606 struct syscall s;
6607 get_syscall_by_number (iter, &s);
6608
6609 if (s.name)
6610 printf_filtered (" '%s' [%d]", s.name, s.number);
6611 else
6612 printf_filtered (" %d", s.number);
6613 }
6614 printf_filtered (")");
6615 }
6616 else
6617 printf_filtered (_("Catchpoint %d (any syscall)"),
6618 b->number);
6619}
6620
6149aea9
PA
6621/* Implement the "print_recreate" breakpoint_ops method for syscall
6622 catchpoints. */
6623
6624static void
6625print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
6626{
be5c67c1
PA
6627 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
6628
6149aea9
PA
6629 fprintf_unfiltered (fp, "catch syscall");
6630
be5c67c1 6631 if (c->syscalls_to_be_caught)
6149aea9
PA
6632 {
6633 int i, iter;
6634
6635 for (i = 0;
be5c67c1 6636 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
6637 i++)
6638 {
6639 struct syscall s;
6640
6641 get_syscall_by_number (iter, &s);
6642 if (s.name)
6643 fprintf_unfiltered (fp, " %s", s.name);
6644 else
6645 fprintf_unfiltered (fp, " %d", s.number);
6646 }
6647 }
d9b3f62e 6648 print_recreate_thread (b, fp);
6149aea9
PA
6649}
6650
a96d9b2e
SDJ
6651/* The breakpoint_ops structure to be used in syscall catchpoints. */
6652
2060206e 6653static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
6654
6655/* Returns non-zero if 'b' is a syscall catchpoint. */
6656
6657static int
6658syscall_catchpoint_p (struct breakpoint *b)
6659{
6660 return (b->ops == &catch_syscall_breakpoint_ops);
6661}
6662
346774a9
PA
6663/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
6664 is non-zero, then make the breakpoint temporary. If COND_STRING is
6665 not NULL, then store it in the breakpoint. OPS, if not NULL, is
6666 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 6667
346774a9
PA
6668static void
6669init_catchpoint (struct breakpoint *b,
6670 struct gdbarch *gdbarch, int tempflag,
6671 char *cond_string,
c0a91b2b 6672 const struct breakpoint_ops *ops)
c906108c 6673{
c5aa993b 6674 struct symtab_and_line sal;
346774a9 6675
fe39c653 6676 init_sal (&sal);
6c95b8df 6677 sal.pspace = current_program_space;
c5aa993b 6678
28010a5d 6679 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 6680
1b36a34b 6681 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 6682 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
6683}
6684
28010a5d 6685void
3a5c3e22 6686install_breakpoint (int internal, struct breakpoint *b)
c56053d2
PA
6687{
6688 add_to_breakpoint_chain (b);
3a5c3e22
PA
6689 set_breakpoint_number (internal, b);
6690 if (!internal)
6691 mention (b);
c56053d2
PA
6692 observer_notify_breakpoint_created (b);
6693 update_global_location_list (1);
6694}
6695
9b70b993 6696static void
a6d9a66e
UW
6697create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
6698 int tempflag, char *cond_string,
c0a91b2b 6699 const struct breakpoint_ops *ops)
c906108c 6700{
e29a4733 6701 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 6702
e29a4733
PA
6703 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
6704
6705 c->forked_inferior_pid = null_ptid;
6706
3a5c3e22 6707 install_breakpoint (0, &c->base);
c906108c
SS
6708}
6709
fe798b75
JB
6710/* Exec catchpoints. */
6711
b4d90040
PA
6712/* An instance of this type is used to represent an exec catchpoint.
6713 It includes a "struct breakpoint" as a kind of base class; users
6714 downcast to "struct breakpoint *" when needed. A breakpoint is
6715 really of this type iff its ops pointer points to
6716 CATCH_EXEC_BREAKPOINT_OPS. */
6717
6718struct exec_catchpoint
6719{
6720 /* The base class. */
6721 struct breakpoint base;
6722
6723 /* Filename of a program whose exec triggered this catchpoint.
6724 This field is only valid immediately after this catchpoint has
6725 triggered. */
6726 char *exec_pathname;
6727};
6728
6729/* Implement the "dtor" breakpoint_ops method for exec
6730 catchpoints. */
6731
6732static void
6733dtor_catch_exec (struct breakpoint *b)
6734{
6735 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6736
6737 xfree (c->exec_pathname);
348d480f 6738
2060206e 6739 base_breakpoint_ops.dtor (b);
b4d90040
PA
6740}
6741
77b06cd7
TJB
6742static int
6743insert_catch_exec (struct bp_location *bl)
c906108c 6744{
77b06cd7 6745 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 6746}
c906108c 6747
fe798b75 6748static int
77b06cd7 6749remove_catch_exec (struct bp_location *bl)
fe798b75
JB
6750{
6751 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
6752}
c906108c 6753
fe798b75 6754static int
f1310107
TJB
6755breakpoint_hit_catch_exec (const struct bp_location *bl,
6756 struct address_space *aspace, CORE_ADDR bp_addr)
fe798b75 6757{
b4d90040
PA
6758 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
6759
6760 return inferior_has_execd (inferior_ptid, &c->exec_pathname);
fe798b75 6761}
c906108c 6762
fe798b75 6763static enum print_stop_action
348d480f 6764print_it_catch_exec (bpstat bs)
fe798b75 6765{
348d480f 6766 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
6767 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
6768
fe798b75
JB
6769 annotate_catchpoint (b->number);
6770 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
b4d90040 6771 c->exec_pathname);
fe798b75 6772 return PRINT_SRC_AND_LOC;
c906108c
SS
6773}
6774
fe798b75 6775static void
a6d9a66e 6776print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 6777{
b4d90040 6778 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 6779 struct value_print_options opts;
79a45e25 6780 struct ui_out *uiout = current_uiout;
fe798b75
JB
6781
6782 get_user_print_options (&opts);
6783
6784 /* Field 4, the address, is omitted (which makes the columns
6785 not line up too nicely with the headers, but the effect
6786 is relatively readable). */
6787 if (opts.addressprint)
6788 ui_out_field_skip (uiout, "addr");
6789 annotate_field (5);
6790 ui_out_text (uiout, "exec");
b4d90040 6791 if (c->exec_pathname != NULL)
fe798b75
JB
6792 {
6793 ui_out_text (uiout, ", program \"");
b4d90040 6794 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
6795 ui_out_text (uiout, "\" ");
6796 }
6797}
6798
6799static void
6800print_mention_catch_exec (struct breakpoint *b)
6801{
6802 printf_filtered (_("Catchpoint %d (exec)"), b->number);
6803}
6804
6149aea9
PA
6805/* Implement the "print_recreate" breakpoint_ops method for exec
6806 catchpoints. */
6807
6808static void
6809print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
6810{
6811 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 6812 print_recreate_thread (b, fp);
6149aea9
PA
6813}
6814
2060206e 6815static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 6816
a96d9b2e
SDJ
6817static void
6818create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 6819 const struct breakpoint_ops *ops)
a96d9b2e 6820{
be5c67c1 6821 struct syscall_catchpoint *c;
a96d9b2e 6822 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 6823
be5c67c1
PA
6824 c = XNEW (struct syscall_catchpoint);
6825 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
6826 c->syscalls_to_be_caught = filter;
a96d9b2e 6827
3a5c3e22 6828 install_breakpoint (0, &c->base);
a96d9b2e
SDJ
6829}
6830
c906108c 6831static int
fba45db2 6832hw_breakpoint_used_count (void)
c906108c 6833{
c906108c 6834 int i = 0;
f1310107
TJB
6835 struct breakpoint *b;
6836 struct bp_location *bl;
c906108c
SS
6837
6838 ALL_BREAKPOINTS (b)
c5aa993b 6839 {
d6b74ac4 6840 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
6841 for (bl = b->loc; bl; bl = bl->next)
6842 {
6843 /* Special types of hardware breakpoints may use more than
6844 one register. */
348d480f 6845 i += b->ops->resources_needed (bl);
f1310107 6846 }
c5aa993b 6847 }
c906108c
SS
6848
6849 return i;
6850}
6851
a1398e0c
PA
6852/* Returns the resources B would use if it were a hardware
6853 watchpoint. */
6854
c906108c 6855static int
a1398e0c 6856hw_watchpoint_use_count (struct breakpoint *b)
c906108c 6857{
c906108c 6858 int i = 0;
e09342b5 6859 struct bp_location *bl;
c906108c 6860
a1398e0c
PA
6861 if (!breakpoint_enabled (b))
6862 return 0;
6863
6864 for (bl = b->loc; bl; bl = bl->next)
6865 {
6866 /* Special types of hardware watchpoints may use more than
6867 one register. */
6868 i += b->ops->resources_needed (bl);
6869 }
6870
6871 return i;
6872}
6873
6874/* Returns the sum the used resources of all hardware watchpoints of
6875 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
6876 the sum of the used resources of all hardware watchpoints of other
6877 types _not_ TYPE. */
6878
6879static int
6880hw_watchpoint_used_count_others (struct breakpoint *except,
6881 enum bptype type, int *other_type_used)
6882{
6883 int i = 0;
6884 struct breakpoint *b;
6885
c906108c
SS
6886 *other_type_used = 0;
6887 ALL_BREAKPOINTS (b)
e09342b5 6888 {
a1398e0c
PA
6889 if (b == except)
6890 continue;
e09342b5
TJB
6891 if (!breakpoint_enabled (b))
6892 continue;
6893
a1398e0c
PA
6894 if (b->type == type)
6895 i += hw_watchpoint_use_count (b);
6896 else if (is_hardware_watchpoint (b))
6897 *other_type_used = 1;
e09342b5
TJB
6898 }
6899
c906108c
SS
6900 return i;
6901}
6902
c906108c 6903void
fba45db2 6904disable_watchpoints_before_interactive_call_start (void)
c906108c 6905{
c5aa993b 6906 struct breakpoint *b;
c906108c
SS
6907
6908 ALL_BREAKPOINTS (b)
c5aa993b 6909 {
cc60f2e3 6910 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 6911 {
b5de0fa7 6912 b->enable_state = bp_call_disabled;
b60e7edf 6913 update_global_location_list (0);
c5aa993b
JM
6914 }
6915 }
c906108c
SS
6916}
6917
6918void
fba45db2 6919enable_watchpoints_after_interactive_call_stop (void)
c906108c 6920{
c5aa993b 6921 struct breakpoint *b;
c906108c
SS
6922
6923 ALL_BREAKPOINTS (b)
c5aa993b 6924 {
cc60f2e3 6925 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 6926 {
b5de0fa7 6927 b->enable_state = bp_enabled;
b60e7edf 6928 update_global_location_list (1);
c5aa993b
JM
6929 }
6930 }
c906108c
SS
6931}
6932
8bea4e01
UW
6933void
6934disable_breakpoints_before_startup (void)
6935{
6936 struct breakpoint *b;
6937 int found = 0;
6938
6939 ALL_BREAKPOINTS (b)
6940 {
6c95b8df
PA
6941 if (b->pspace != current_program_space)
6942 continue;
6943
8bea4e01
UW
6944 if ((b->type == bp_breakpoint
6945 || b->type == bp_hardware_breakpoint)
6946 && breakpoint_enabled (b))
6947 {
6948 b->enable_state = bp_startup_disabled;
6949 found = 1;
6950 }
6951 }
6952
6953 if (found)
6954 update_global_location_list (0);
6955
6c95b8df 6956 current_program_space->executing_startup = 1;
8bea4e01
UW
6957}
6958
6959void
6960enable_breakpoints_after_startup (void)
6961{
6962 struct breakpoint *b;
6963 int found = 0;
6964
6c95b8df 6965 current_program_space->executing_startup = 0;
8bea4e01
UW
6966
6967 ALL_BREAKPOINTS (b)
6968 {
6c95b8df
PA
6969 if (b->pspace != current_program_space)
6970 continue;
6971
8bea4e01
UW
6972 if ((b->type == bp_breakpoint
6973 || b->type == bp_hardware_breakpoint)
6974 && b->enable_state == bp_startup_disabled)
6975 {
6976 b->enable_state = bp_enabled;
6977 found = 1;
6978 }
6979 }
6980
6981 if (found)
6982 breakpoint_re_set ();
6983}
6984
c906108c
SS
6985
6986/* Set a breakpoint that will evaporate an end of command
6987 at address specified by SAL.
6988 Restrict it to frame FRAME if FRAME is nonzero. */
6989
6990struct breakpoint *
a6d9a66e
UW
6991set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
6992 struct frame_id frame_id, enum bptype type)
c906108c 6993{
52f0bd74 6994 struct breakpoint *b;
edb3359d
DJ
6995
6996 /* If FRAME_ID is valid, it should be a real frame, not an inlined
6997 one. */
6998 gdb_assert (!frame_id_inlined_p (frame_id));
6999
06edf0c0 7000 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
7001 b->enable_state = bp_enabled;
7002 b->disposition = disp_donttouch;
818dd999 7003 b->frame_id = frame_id;
c906108c 7004
4a64f543
MS
7005 /* If we're debugging a multi-threaded program, then we want
7006 momentary breakpoints to be active in only a single thread of
7007 control. */
39f77062
KB
7008 if (in_thread_list (inferior_ptid))
7009 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 7010
b60e7edf 7011 update_global_location_list_nothrow (1);
74960c60 7012
c906108c
SS
7013 return b;
7014}
611c83ae 7015
06edf0c0
PA
7016/* Make a momentary breakpoint based on the master breakpoint ORIG.
7017 The new breakpoint will have type TYPE, and use OPS as it
7018 breakpoint_ops. */
e58b0e63 7019
06edf0c0
PA
7020static struct breakpoint *
7021momentary_breakpoint_from_master (struct breakpoint *orig,
7022 enum bptype type,
c0a91b2b 7023 const struct breakpoint_ops *ops)
e58b0e63
PA
7024{
7025 struct breakpoint *copy;
7026
06edf0c0 7027 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 7028 copy->loc = allocate_bp_location (copy);
0e30163f 7029 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 7030
a6d9a66e 7031 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
7032 copy->loc->requested_address = orig->loc->requested_address;
7033 copy->loc->address = orig->loc->address;
7034 copy->loc->section = orig->loc->section;
6c95b8df 7035 copy->loc->pspace = orig->loc->pspace;
e58b0e63
PA
7036
7037 if (orig->source_file == NULL)
7038 copy->source_file = NULL;
7039 else
7040 copy->source_file = xstrdup (orig->source_file);
7041
7042 copy->line_number = orig->line_number;
7043 copy->frame_id = orig->frame_id;
7044 copy->thread = orig->thread;
6c95b8df 7045 copy->pspace = orig->pspace;
e58b0e63
PA
7046
7047 copy->enable_state = bp_enabled;
7048 copy->disposition = disp_donttouch;
7049 copy->number = internal_breakpoint_number--;
7050
7051 update_global_location_list_nothrow (0);
7052 return copy;
7053}
7054
06edf0c0
PA
7055/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7056 ORIG is NULL. */
7057
7058struct breakpoint *
7059clone_momentary_breakpoint (struct breakpoint *orig)
7060{
7061 /* If there's nothing to clone, then return nothing. */
7062 if (orig == NULL)
7063 return NULL;
7064
7065 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
7066}
7067
611c83ae 7068struct breakpoint *
a6d9a66e
UW
7069set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
7070 enum bptype type)
611c83ae
PA
7071{
7072 struct symtab_and_line sal;
7073
7074 sal = find_pc_line (pc, 0);
7075 sal.pc = pc;
7076 sal.section = find_pc_overlay (pc);
7077 sal.explicit_pc = 1;
7078
a6d9a66e 7079 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 7080}
c906108c 7081\f
c5aa993b 7082
c906108c
SS
7083/* Tell the user we have just set a breakpoint B. */
7084
7085static void
fba45db2 7086mention (struct breakpoint *b)
c906108c 7087{
348d480f 7088 b->ops->print_mention (b);
79a45e25 7089 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 7090 return;
c906108c
SS
7091 printf_filtered ("\n");
7092}
c906108c 7093\f
c5aa993b 7094
0d381245 7095static struct bp_location *
39d61571 7096add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
7097 const struct symtab_and_line *sal)
7098{
7099 struct bp_location *loc, **tmp;
7100
39d61571 7101 loc = allocate_bp_location (b);
0d381245
VP
7102 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
7103 ;
7104 *tmp = loc;
a6d9a66e
UW
7105 loc->gdbarch = get_sal_arch (*sal);
7106 if (!loc->gdbarch)
7107 loc->gdbarch = b->gdbarch;
0d381245 7108 loc->requested_address = sal->pc;
a6d9a66e
UW
7109 loc->address = adjust_breakpoint_address (loc->gdbarch,
7110 loc->requested_address, b->type);
6c95b8df
PA
7111 loc->pspace = sal->pspace;
7112 gdb_assert (loc->pspace != NULL);
0d381245
VP
7113 loc->section = sal->section;
7114
0e30163f
JK
7115 set_breakpoint_location_function (loc,
7116 sal->explicit_pc || sal->explicit_line);
0d381245
VP
7117 return loc;
7118}
514f746b
AR
7119\f
7120
7121/* Return 1 if LOC is pointing to a permanent breakpoint,
7122 return 0 otherwise. */
7123
7124static int
7125bp_loc_is_permanent (struct bp_location *loc)
7126{
7127 int len;
7128 CORE_ADDR addr;
7129 const gdb_byte *brk;
7130 gdb_byte *target_mem;
939c61fa
JK
7131 struct cleanup *cleanup;
7132 int retval = 0;
514f746b
AR
7133
7134 gdb_assert (loc != NULL);
7135
7136 addr = loc->address;
a6d9a66e 7137 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 7138
939c61fa
JK
7139 /* Software breakpoints unsupported? */
7140 if (brk == NULL)
7141 return 0;
7142
514f746b
AR
7143 target_mem = alloca (len);
7144
939c61fa
JK
7145 /* Enable the automatic memory restoration from breakpoints while
7146 we read the memory. Otherwise we could say about our temporary
7147 breakpoints they are permanent. */
6c95b8df
PA
7148 cleanup = save_current_space_and_thread ();
7149
7150 switch_to_program_space_and_thread (loc->pspace);
7151 make_show_memory_breakpoints_cleanup (0);
939c61fa 7152
514f746b
AR
7153 if (target_read_memory (loc->address, target_mem, len) == 0
7154 && memcmp (target_mem, brk, len) == 0)
939c61fa 7155 retval = 1;
514f746b 7156
939c61fa
JK
7157 do_cleanups (cleanup);
7158
7159 return retval;
514f746b
AR
7160}
7161
7162
c3f6f71d 7163
018d34a4
VP
7164/* Create a breakpoint with SAL as location. Use ADDR_STRING
7165 as textual description of the location, and COND_STRING
db107f19 7166 as condition expression. */
018d34a4
VP
7167
7168static void
d9b3f62e
PA
7169init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
7170 struct symtabs_and_lines sals, char *addr_string,
7171 char *cond_string,
7172 enum bptype type, enum bpdisp disposition,
7173 int thread, int task, int ignore_count,
c0a91b2b 7174 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e 7175 int enabled, int internal, int display_canonical)
018d34a4 7176{
0d381245 7177 int i;
018d34a4
VP
7178
7179 if (type == bp_hardware_breakpoint)
7180 {
7181 int i = hw_breakpoint_used_count ();
7182 int target_resources_ok =
d92524f1 7183 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
7184 i + 1, 0);
7185 if (target_resources_ok == 0)
7186 error (_("No hardware breakpoint support in the target."));
7187 else if (target_resources_ok < 0)
7188 error (_("Hardware breakpoints used exceeds limit."));
7189 }
7190
6c95b8df
PA
7191 gdb_assert (sals.nelts > 0);
7192
0d381245
VP
7193 for (i = 0; i < sals.nelts; ++i)
7194 {
7195 struct symtab_and_line sal = sals.sals[i];
7196 struct bp_location *loc;
7197
7198 if (from_tty)
5af949e3
UW
7199 {
7200 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7201 if (!loc_gdbarch)
7202 loc_gdbarch = gdbarch;
7203
7204 describe_other_breakpoints (loc_gdbarch,
6c95b8df 7205 sal.pspace, sal.pc, sal.section, thread);
5af949e3 7206 }
0d381245
VP
7207
7208 if (i == 0)
7209 {
d9b3f62e 7210 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 7211 b->thread = thread;
4a306c9a 7212 b->task = task;
018d34a4 7213
0d381245
VP
7214 b->cond_string = cond_string;
7215 b->ignore_count = ignore_count;
41447f92 7216 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 7217 b->disposition = disposition;
6c95b8df
PA
7218 b->pspace = sals.sals[0].pspace;
7219
0fb4aa4b
PA
7220 if (type == bp_static_tracepoint)
7221 {
d9b3f62e 7222 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
7223 struct static_tracepoint_marker marker;
7224
7225 if (is_marker_spec (addr_string))
7226 {
7227 /* We already know the marker exists, otherwise, we
7228 wouldn't see a sal for it. */
7229 char *p = &addr_string[3];
7230 char *endp;
7231 char *marker_str;
7232 int i;
7233
e9cafbcc 7234 p = skip_spaces (p);
0fb4aa4b 7235
e9cafbcc 7236 endp = skip_to_space (p);
0fb4aa4b
PA
7237
7238 marker_str = savestring (p, endp - p);
d9b3f62e 7239 t->static_trace_marker_id = marker_str;
0fb4aa4b 7240
3e43a32a
MS
7241 printf_filtered (_("Probed static tracepoint "
7242 "marker \"%s\"\n"),
d9b3f62e 7243 t->static_trace_marker_id);
0fb4aa4b
PA
7244 }
7245 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
7246 {
d9b3f62e 7247 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
7248 release_static_tracepoint_marker (&marker);
7249
3e43a32a
MS
7250 printf_filtered (_("Probed static tracepoint "
7251 "marker \"%s\"\n"),
d9b3f62e 7252 t->static_trace_marker_id);
0fb4aa4b
PA
7253 }
7254 else
3e43a32a
MS
7255 warning (_("Couldn't determine the static "
7256 "tracepoint marker to probe"));
0fb4aa4b
PA
7257 }
7258
6c95b8df 7259 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7260 && (b->type == bp_breakpoint
7261 || b->type == bp_hardware_breakpoint))
7262 b->enable_state = bp_startup_disabled;
7263
0d381245
VP
7264 loc = b->loc;
7265 }
7266 else
018d34a4 7267 {
39d61571 7268 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
7269 }
7270
514f746b
AR
7271 if (bp_loc_is_permanent (loc))
7272 make_breakpoint_permanent (b);
7273
0d381245
VP
7274 if (b->cond_string)
7275 {
7276 char *arg = b->cond_string;
d32a6982 7277 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 7278 if (*arg)
db107f19 7279 error (_("Garbage %s follows condition"), arg);
018d34a4 7280 }
0d381245 7281 }
018d34a4 7282
56435ebe 7283 b->display_canonical = display_canonical;
018d34a4
VP
7284 if (addr_string)
7285 b->addr_string = addr_string;
7286 else
7287 /* addr_string has to be used or breakpoint_re_set will delete
7288 me. */
5af949e3
UW
7289 b->addr_string
7290 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
d9b3f62e 7291}
018d34a4 7292
d9b3f62e
PA
7293static void
7294create_breakpoint_sal (struct gdbarch *gdbarch,
7295 struct symtabs_and_lines sals, char *addr_string,
7296 char *cond_string,
7297 enum bptype type, enum bpdisp disposition,
7298 int thread, int task, int ignore_count,
c0a91b2b 7299 const struct breakpoint_ops *ops, int from_tty,
d9b3f62e
PA
7300 int enabled, int internal, int display_canonical)
7301{
7302 struct breakpoint *b;
7303 struct cleanup *old_chain;
7304
7305 if (is_tracepoint_type (type))
7306 {
7307 struct tracepoint *t;
7308
7309 t = XCNEW (struct tracepoint);
7310 b = &t->base;
7311 }
7312 else
7313 b = XNEW (struct breakpoint);
7314
7315 old_chain = make_cleanup (xfree, b);
7316
7317 init_breakpoint_sal (b, gdbarch,
7318 sals, addr_string,
7319 cond_string,
7320 type, disposition,
7321 thread, task, ignore_count,
7322 ops, from_tty,
7323 enabled, internal, display_canonical);
7324 discard_cleanups (old_chain);
7325
7326 install_breakpoint (internal, b);
018d34a4
VP
7327}
7328
ed0616c6
VP
7329/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
7330 elements to fill the void space. */
2c0b251b
PA
7331static void
7332remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
7333{
7334 int i = index_to_remove+1;
7335 int last_index = sal->nelts-1;
7336
7337 for (;i <= last_index; ++i)
7338 sal->sals[i-1] = sal->sals[i];
7339
7340 --(sal->nelts);
7341}
7342
6c95b8df
PA
7343/* If appropriate, obtains all sals that correspond to the same file
7344 and line as SAL, in all program spaces. Users debugging with IDEs,
7345 will want to set a breakpoint at foo.c:line, and not really care
7346 about program spaces. This is done only if SAL does not have
7347 explicit PC and has line and file information. If we got just a
7348 single expanded sal, return the original.
ed0616c6 7349
6c95b8df
PA
7350 Otherwise, if SAL.explicit_line is not set, filter out all sals for
7351 which the name of enclosing function is different from SAL. This
7352 makes sure that if we have breakpoint originally set in template
7353 instantiation, say foo<int>(), we won't expand SAL to locations at
7354 the same line in all existing instantiations of 'foo'. */
ed0616c6 7355
2c0b251b 7356static struct symtabs_and_lines
ed0616c6
VP
7357expand_line_sal_maybe (struct symtab_and_line sal)
7358{
7359 struct symtabs_and_lines expanded;
7360 CORE_ADDR original_pc = sal.pc;
7361 char *original_function = NULL;
7362 int found;
7363 int i;
6c95b8df 7364 struct cleanup *old_chain;
ed0616c6
VP
7365
7366 /* If we have explicit pc, don't expand.
7367 If we have no line number, we can't expand. */
7368 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
7369 {
7370 expanded.nelts = 1;
7371 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7372 expanded.sals[0] = sal;
7373 return expanded;
7374 }
7375
7376 sal.pc = 0;
6c95b8df
PA
7377
7378 old_chain = save_current_space_and_thread ();
7379
7380 switch_to_program_space_and_thread (sal.pspace);
7381
ed0616c6 7382 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6c95b8df
PA
7383
7384 /* Note that expand_line_sal visits *all* program spaces. */
ed0616c6 7385 expanded = expand_line_sal (sal);
6c95b8df 7386
ed0616c6
VP
7387 if (expanded.nelts == 1)
7388 {
3dba1c98
JB
7389 /* We had one sal, we got one sal. Return that sal, adjusting it
7390 past the function prologue if necessary. */
ed0616c6
VP
7391 xfree (expanded.sals);
7392 expanded.nelts = 1;
7393 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7394 sal.pc = original_pc;
7395 expanded.sals[0] = sal;
3dba1c98 7396 skip_prologue_sal (&expanded.sals[0]);
6c95b8df 7397 do_cleanups (old_chain);
ed0616c6
VP
7398 return expanded;
7399 }
7400
7401 if (!sal.explicit_line)
7402 {
7403 CORE_ADDR func_addr, func_end;
7404 for (i = 0; i < expanded.nelts; ++i)
7405 {
7406 CORE_ADDR pc = expanded.sals[i].pc;
7407 char *this_function;
6c95b8df
PA
7408
7409 /* We need to switch threads as well since we're about to
7410 read memory. */
7411 switch_to_program_space_and_thread (expanded.sals[i].pspace);
7412
ed0616c6
VP
7413 if (find_pc_partial_function (pc, &this_function,
7414 &func_addr, &func_end))
7415 {
059fb39f
PM
7416 if (this_function
7417 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
7418 {
7419 remove_sal (&expanded, i);
7420 --i;
7421 }
ed0616c6
VP
7422 }
7423 }
7424 }
059acae7
UW
7425
7426 /* Skip the function prologue if necessary. */
7427 for (i = 0; i < expanded.nelts; ++i)
7428 skip_prologue_sal (&expanded.sals[i]);
ed0616c6 7429
6c95b8df
PA
7430 do_cleanups (old_chain);
7431
ed0616c6
VP
7432 if (expanded.nelts <= 1)
7433 {
e5dd4106 7434 /* This is an ugly workaround. If we get zero expanded sals
4a64f543
MS
7435 then something is really wrong. Fix that by returning the
7436 original sal. */
7437
ed0616c6
VP
7438 xfree (expanded.sals);
7439 expanded.nelts = 1;
7440 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7441 sal.pc = original_pc;
7442 expanded.sals[0] = sal;
7443 return expanded;
7444 }
7445
7446 if (original_pc)
7447 {
7448 found = 0;
7449 for (i = 0; i < expanded.nelts; ++i)
7450 if (expanded.sals[i].pc == original_pc)
7451 {
7452 found = 1;
7453 break;
7454 }
7455 gdb_assert (found);
7456 }
7457
7458 return expanded;
7459}
7460
018d34a4
VP
7461/* Add SALS.nelts breakpoints to the breakpoint table. For each
7462 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
7463 value. COND_STRING, if not NULL, specified the condition to be
7464 used for all breakpoints. Essentially the only case where
7465 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
7466 function. In that case, it's still not possible to specify
7467 separate conditions for different overloaded functions, so
7468 we take just a single condition string.
7469
c3f6f71d 7470 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 7471 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
7472 array contents). If the function fails (error() is called), the
7473 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 7474 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
7475
7476static void
8cdf0e15 7477create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2
TT
7478 struct symtabs_and_lines sals,
7479 struct linespec_result *canonical,
8cdf0e15
VP
7480 char *cond_string,
7481 enum bptype type, enum bpdisp disposition,
7482 int thread, int task, int ignore_count,
c0a91b2b 7483 const struct breakpoint_ops *ops, int from_tty,
84f4c1fe 7484 int enabled, int internal)
c906108c 7485{
018d34a4 7486 int i;
cc59ec59 7487
018d34a4 7488 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 7489 {
ed0616c6
VP
7490 struct symtabs_and_lines expanded =
7491 expand_line_sal_maybe (sals.sals[i]);
0d381245 7492
7efd8fc2 7493 create_breakpoint_sal (gdbarch, expanded, canonical->canonical[i],
8cdf0e15 7494 cond_string, type, disposition,
84f4c1fe 7495 thread, task, ignore_count, ops,
56435ebe
TT
7496 from_tty, enabled, internal,
7497 canonical->special_display);
c3f6f71d 7498 }
c3f6f71d 7499}
c906108c 7500
9998af43 7501/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 7502 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 7503 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
7504 address strings. ADDRESS points to the end of the SAL.
7505
7506 The array and the line spec strings are allocated on the heap, it is
7507 the caller's responsibility to free them. */
c906108c 7508
b9362cc7 7509static void
c3f6f71d
JM
7510parse_breakpoint_sals (char **address,
7511 struct symtabs_and_lines *sals,
58438ac1 7512 struct linespec_result *canonical)
c3f6f71d
JM
7513{
7514 char *addr_start = *address;
cc59ec59 7515
c3f6f71d 7516 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 7517 breakpoint. */
c3f6f71d
JM
7518 if ((*address) == NULL
7519 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 7520 {
1bfeeb0f
JL
7521 /* The last displayed codepoint, if it's valid, is our default breakpoint
7522 address. */
7523 if (last_displayed_sal_is_valid ())
c906108c 7524 {
c3f6f71d 7525 struct symtab_and_line sal;
cc59ec59 7526
4a64f543 7527 init_sal (&sal); /* Initialize to zeroes. */
c3f6f71d 7528 sals->sals = (struct symtab_and_line *)
c906108c 7529 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
7530
7531 /* Set sal's pspace, pc, symtab, and line to the values
7532 corresponding to the last call to print_frame_info. */
7533 get_last_displayed_sal (&sal);
7534 sal.section = find_pc_overlay (sal.pc);
00903456 7535
4a64f543 7536 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
7537 where PC is the last displayed codepoint's address. So
7538 make sure to set sal.explicit_pc to prevent GDB from
7539 trying to expand the list of sals to include all other
7540 instances with the same symtab and line. */
00903456
JK
7541 sal.explicit_pc = 1;
7542
c3f6f71d
JM
7543 sals->sals[0] = sal;
7544 sals->nelts = 1;
c906108c
SS
7545 }
7546 else
8a3fe4f8 7547 error (_("No default breakpoint address now."));
c906108c
SS
7548 }
7549 else
7550 {
c906108c 7551 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
7552 current_source_symtab (which is decode_line_1's default).
7553 This should produce the results we want almost all of the
1bfeeb0f
JL
7554 time while leaving the last displayed codepoint pointers
7555 alone.
4a64f543 7556
1aeae86e
AF
7557 ObjC: However, don't match an Objective-C method name which
7558 may have a '+' or '-' succeeded by a '[' */
0378c332 7559
c214a6fd 7560 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 7561
1bfeeb0f 7562 if (last_displayed_sal_is_valid ()
0378c332 7563 && (!cursal.symtab
1aeae86e
AF
7564 || ((strchr ("+-", (*address)[0]) != NULL)
7565 && ((*address)[1] != '['))))
1bfeeb0f
JL
7566 *sals = decode_line_1 (address, 1,
7567 get_last_displayed_symtab (),
7568 get_last_displayed_line (),
7569 canonical);
c906108c 7570 else
0101ce28 7571 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
58438ac1 7572 canonical);
c906108c 7573 }
4a64f543 7574 /* For any SAL that didn't have a canonical string, fill one in. */
7efd8fc2 7575 if (sals->nelts > 0 && canonical->canonical == NULL)
38a714bb 7576 canonical->canonical = xcalloc (sals->nelts, sizeof (char *));
c3f6f71d 7577 if (addr_start != (*address))
c906108c 7578 {
c3f6f71d 7579 int i;
cc59ec59 7580
c3f6f71d 7581 for (i = 0; i < sals->nelts; i++)
c906108c 7582 {
4a64f543 7583 /* Add the string if not present. */
7efd8fc2
TT
7584 if (canonical->canonical[i] == NULL)
7585 canonical->canonical[i] = savestring (addr_start,
7586 (*address) - addr_start);
c906108c
SS
7587 }
7588 }
c3f6f71d 7589}
c906108c 7590
c906108c 7591
c3f6f71d 7592/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 7593 inserted as a breakpoint. If it can't throw an error. */
c906108c 7594
b9362cc7 7595static void
23e7acfb 7596breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
7597{
7598 int i;
cc59ec59 7599
c3f6f71d 7600 for (i = 0; i < sals->nelts; i++)
ee53e872 7601 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
7602}
7603
7a697b8d
SS
7604/* Fast tracepoints may have restrictions on valid locations. For
7605 instance, a fast tracepoint using a jump instead of a trap will
7606 likely have to overwrite more bytes than a trap would, and so can
7607 only be placed where the instruction is longer than the jump, or a
7608 multi-instruction sequence does not have a jump into the middle of
7609 it, etc. */
7610
7611static void
7612check_fast_tracepoint_sals (struct gdbarch *gdbarch,
7613 struct symtabs_and_lines *sals)
7614{
7615 int i, rslt;
7616 struct symtab_and_line *sal;
7617 char *msg;
7618 struct cleanup *old_chain;
7619
7620 for (i = 0; i < sals->nelts; i++)
7621 {
7622 sal = &sals->sals[i];
7623
7624 rslt = gdbarch_fast_tracepoint_valid_at (gdbarch, sal->pc,
7625 NULL, &msg);
7626 old_chain = make_cleanup (xfree, msg);
7627
7628 if (!rslt)
7629 error (_("May not have a fast tracepoint at 0x%s%s"),
7630 paddress (gdbarch, sal->pc), (msg ? msg : ""));
7631
7632 do_cleanups (old_chain);
7633 }
7634}
7635
018d34a4
VP
7636/* Given TOK, a string specification of condition and thread, as
7637 accepted by the 'break' command, extract the condition
7638 string and thread number and set *COND_STRING and *THREAD.
4a64f543 7639 PC identifies the context at which the condition should be parsed.
018d34a4
VP
7640 If no condition is found, *COND_STRING is set to NULL.
7641 If no thread is found, *THREAD is set to -1. */
7642static void
7643find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 7644 char **cond_string, int *thread, int *task)
018d34a4
VP
7645{
7646 *cond_string = NULL;
7647 *thread = -1;
7648 while (tok && *tok)
7649 {
7650 char *end_tok;
7651 int toklen;
7652 char *cond_start = NULL;
7653 char *cond_end = NULL;
cc59ec59 7654
e9cafbcc 7655 tok = skip_spaces (tok);
018d34a4 7656
e9cafbcc 7657 end_tok = skip_to_space (tok);
018d34a4
VP
7658
7659 toklen = end_tok - tok;
7660
7661 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7662 {
f7545552
TT
7663 struct expression *expr;
7664
018d34a4 7665 tok = cond_start = end_tok + 1;
f7545552
TT
7666 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
7667 xfree (expr);
018d34a4
VP
7668 cond_end = tok;
7669 *cond_string = savestring (cond_start,
7670 cond_end - cond_start);
7671 }
7672 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7673 {
7674 char *tmptok;
7675
7676 tok = end_tok + 1;
7677 tmptok = tok;
7678 *thread = strtol (tok, &tok, 0);
7679 if (tok == tmptok)
7680 error (_("Junk after thread keyword."));
7681 if (!valid_thread_id (*thread))
7682 error (_("Unknown thread %d."), *thread);
7683 }
4a306c9a
JB
7684 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
7685 {
7686 char *tmptok;
7687
7688 tok = end_tok + 1;
7689 tmptok = tok;
7690 *task = strtol (tok, &tok, 0);
7691 if (tok == tmptok)
7692 error (_("Junk after task keyword."));
7693 if (!valid_task_id (*task))
b6199126 7694 error (_("Unknown task %d."), *task);
4a306c9a 7695 }
018d34a4
VP
7696 else
7697 error (_("Junk at end of arguments."));
7698 }
7699}
7700
0fb4aa4b
PA
7701/* Decode a static tracepoint marker spec. */
7702
7703static struct symtabs_and_lines
7704decode_static_tracepoint_spec (char **arg_p)
7705{
7706 VEC(static_tracepoint_marker_p) *markers = NULL;
7707 struct symtabs_and_lines sals;
7708 struct symtab_and_line sal;
7709 struct symbol *sym;
7710 struct cleanup *old_chain;
7711 char *p = &(*arg_p)[3];
7712 char *endp;
7713 char *marker_str;
7714 int i;
7715
e9cafbcc 7716 p = skip_spaces (p);
0fb4aa4b 7717
e9cafbcc 7718 endp = skip_to_space (p);
0fb4aa4b
PA
7719
7720 marker_str = savestring (p, endp - p);
7721 old_chain = make_cleanup (xfree, marker_str);
7722
7723 markers = target_static_tracepoint_markers_by_strid (marker_str);
7724 if (VEC_empty(static_tracepoint_marker_p, markers))
7725 error (_("No known static tracepoint marker named %s"), marker_str);
7726
7727 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
7728 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
7729
7730 for (i = 0; i < sals.nelts; i++)
7731 {
7732 struct static_tracepoint_marker *marker;
7733
7734 marker = VEC_index (static_tracepoint_marker_p, markers, i);
7735
7736 init_sal (&sals.sals[i]);
7737
7738 sals.sals[i] = find_pc_line (marker->address, 0);
7739 sals.sals[i].pc = marker->address;
7740
7741 release_static_tracepoint_marker (marker);
7742 }
7743
7744 do_cleanups (old_chain);
7745
7746 *arg_p = endp;
7747 return sals;
7748}
7749
fd9b8c24
PA
7750/* Set a breakpoint. This function is shared between CLI and MI
7751 functions for setting a breakpoint. This function has two major
7752 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
7753 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
7754 breakpoint location, address and thread. Otherwise, ARG is just
7755 the location of breakpoint, with condition and thread specified by
7756 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
7757 the breakpoint number will be allocated from the internal
7758 breakpoint count. Returns true if any breakpoint was created;
7759 false otherwise. */
0101ce28 7760
8cdf0e15
VP
7761int
7762create_breakpoint (struct gdbarch *gdbarch,
7763 char *arg, char *cond_string, int thread,
7764 int parse_condition_and_thread,
0fb4aa4b 7765 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
7766 int ignore_count,
7767 enum auto_boolean pending_break_support,
c0a91b2b 7768 const struct breakpoint_ops *ops,
84f4c1fe 7769 int from_tty, int enabled, int internal)
c3f6f71d 7770{
b78a6381 7771 volatile struct gdb_exception e;
c3f6f71d 7772 struct symtabs_and_lines sals;
0101ce28 7773 struct symtab_and_line pending_sal;
0101ce28 7774 char *copy_arg;
c3f6f71d 7775 char *addr_start = arg;
7efd8fc2 7776 struct linespec_result canonical;
c3f6f71d 7777 struct cleanup *old_chain;
80c99de1 7778 struct cleanup *bkpt_chain = NULL;
05ff989b 7779 int i;
0101ce28 7780 int pending = 0;
4a306c9a 7781 int task = 0;
86b17b60 7782 int prev_bkpt_count = breakpoint_count;
c3f6f71d 7783
348d480f
PA
7784 gdb_assert (ops != NULL);
7785
c3f6f71d
JM
7786 sals.sals = NULL;
7787 sals.nelts = 0;
7efd8fc2 7788 init_linespec_result (&canonical);
c3f6f71d 7789
0fb4aa4b
PA
7790 if (type_wanted == bp_static_tracepoint && is_marker_spec (arg))
7791 {
7792 int i;
7793
7794 sals = decode_static_tracepoint_spec (&arg);
7795
7796 copy_arg = savestring (addr_start, arg - addr_start);
38a714bb 7797 canonical.canonical = xcalloc (sals.nelts, sizeof (char *));
0fb4aa4b 7798 for (i = 0; i < sals.nelts; i++)
7efd8fc2 7799 canonical.canonical[i] = xstrdup (copy_arg);
0fb4aa4b
PA
7800 goto done;
7801 }
7802
b78a6381
TT
7803 TRY_CATCH (e, RETURN_MASK_ALL)
7804 {
58438ac1 7805 parse_breakpoint_sals (&arg, &sals, &canonical);
b78a6381 7806 }
0101ce28
JJ
7807
7808 /* If caller is interested in rc value from parse, set value. */
05ff989b 7809 switch (e.reason)
0101ce28 7810 {
05ff989b 7811 case RETURN_QUIT:
98deb0da 7812 throw_exception (e);
05ff989b
AC
7813 case RETURN_ERROR:
7814 switch (e.error)
0101ce28 7815 {
05ff989b 7816 case NOT_FOUND_ERROR:
0101ce28 7817
05ff989b
AC
7818 /* If pending breakpoint support is turned off, throw
7819 error. */
fa8d40ab
JJ
7820
7821 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
7822 throw_exception (e);
7823
7824 exception_print (gdb_stderr, e);
fa8d40ab 7825
05ff989b
AC
7826 /* If pending breakpoint support is auto query and the user
7827 selects no, then simply return the error code. */
059fb39f 7828 if (pending_break_support == AUTO_BOOLEAN_AUTO
3e43a32a
MS
7829 && !nquery (_("Make breakpoint pending on "
7830 "future shared library load? ")))
fd9b8c24 7831 return 0;
fa8d40ab 7832
05ff989b
AC
7833 /* At this point, either the user was queried about setting
7834 a pending breakpoint and selected yes, or pending
7835 breakpoint behavior is on and thus a pending breakpoint
7836 is defaulted on behalf of the user. */
0101ce28 7837 copy_arg = xstrdup (addr_start);
7efd8fc2 7838 canonical.canonical = &copy_arg;
0101ce28
JJ
7839 sals.nelts = 1;
7840 sals.sals = &pending_sal;
7841 pending_sal.pc = 0;
7842 pending = 1;
05ff989b
AC
7843 break;
7844 default:
98deb0da 7845 throw_exception (e);
0101ce28 7846 }
2abae994 7847 break;
05ff989b
AC
7848 default:
7849 if (!sals.nelts)
fd9b8c24 7850 return 0;
0101ce28 7851 }
c3f6f71d 7852
0fb4aa4b
PA
7853 done:
7854
4a64f543 7855 /* Create a chain of things that always need to be cleaned up. */
c3f6f71d
JM
7856 old_chain = make_cleanup (null_cleanup, 0);
7857
0101ce28
JJ
7858 if (!pending)
7859 {
7860 /* Make sure that all storage allocated to SALS gets freed. */
7861 make_cleanup (xfree, sals.sals);
7862
7efd8fc2
TT
7863 /* Cleanup the canonical array but not its contents. */
7864 make_cleanup (xfree, canonical.canonical);
0101ce28 7865 }
c3f6f71d 7866
c3f6f71d
JM
7867 /* ----------------------------- SNIP -----------------------------
7868 Anything added to the cleanup chain beyond this point is assumed
7869 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
7870 then the memory is not reclaimed. */
7871 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 7872
7efd8fc2 7873 /* Mark the contents of the canonical for cleanup. These go on
80c99de1 7874 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
7875 for (i = 0; i < sals.nelts; i++)
7876 {
7efd8fc2
TT
7877 if (canonical.canonical[i] != NULL)
7878 make_cleanup (xfree, canonical.canonical[i]);
c3f6f71d
JM
7879 }
7880
7881 /* Resolve all line numbers to PC's and verify that the addresses
7882 are ok for the target. */
0101ce28 7883 if (!pending)
23e7acfb 7884 breakpoint_sals_to_pc (&sals);
c3f6f71d 7885
7a697b8d
SS
7886 /* Fast tracepoints may have additional restrictions on location. */
7887 if (type_wanted == bp_fast_tracepoint)
7888 check_fast_tracepoint_sals (gdbarch, &sals);
7889
c3f6f71d
JM
7890 /* Verify that condition can be parsed, before setting any
7891 breakpoints. Allocate a separate condition expression for each
4a64f543 7892 breakpoint. */
0101ce28 7893 if (!pending)
c3f6f71d 7894 {
2f069f6f 7895 if (parse_condition_and_thread)
72b2ff0e
VP
7896 {
7897 /* Here we only parse 'arg' to separate condition
7898 from thread number, so parsing in context of first
7899 sal is OK. When setting the breakpoint we'll
7900 re-parse it in context of each sal. */
7901 cond_string = NULL;
7902 thread = -1;
4a306c9a
JB
7903 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
7904 &thread, &task);
72b2ff0e
VP
7905 if (cond_string)
7906 make_cleanup (xfree, cond_string);
7907 }
2f069f6f 7908 else
72b2ff0e
VP
7909 {
7910 /* Create a private copy of condition string. */
7911 if (cond_string)
7912 {
7913 cond_string = xstrdup (cond_string);
7914 make_cleanup (xfree, cond_string);
7915 }
7916 }
0fb4aa4b
PA
7917
7918 /* If the user is creating a static tracepoint by marker id
7919 (strace -m MARKER_ID), then store the sals index, so that
7920 breakpoint_re_set can try to match up which of the newly
7921 found markers corresponds to this one, and, don't try to
7922 expand multiple locations for each sal, given than SALS
7923 already should contain all sals for MARKER_ID. */
7924 if (type_wanted == bp_static_tracepoint
7efd8fc2 7925 && is_marker_spec (canonical.canonical[0]))
0fb4aa4b
PA
7926 {
7927 int i;
7928
7929 for (i = 0; i < sals.nelts; ++i)
7930 {
7931 struct symtabs_and_lines expanded;
d9b3f62e 7932 struct tracepoint *tp;
0fb4aa4b
PA
7933 struct cleanup *old_chain;
7934
7935 expanded.nelts = 1;
7936 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
7937 expanded.sals[0] = sals.sals[i];
7938 old_chain = make_cleanup (xfree, expanded.sals);
7939
d9b3f62e
PA
7940 tp = XCNEW (struct tracepoint);
7941 init_breakpoint_sal (&tp->base, gdbarch, expanded,
7942 canonical.canonical[i],
7943 cond_string, type_wanted,
7944 tempflag ? disp_del : disp_donttouch,
7945 thread, task, ignore_count, ops,
7946 from_tty, enabled, internal,
7947 canonical.special_display);
0fb4aa4b
PA
7948 /* Given that its possible to have multiple markers with
7949 the same string id, if the user is creating a static
7950 tracepoint by marker id ("strace -m MARKER_ID"), then
7951 store the sals index, so that breakpoint_re_set can
7952 try to match up which of the newly found markers
7953 corresponds to this one */
7954 tp->static_trace_marker_id_idx = i;
d9b3f62e
PA
7955
7956 install_breakpoint (internal, &tp->base);
7957
7958 do_cleanups (old_chain);
0fb4aa4b
PA
7959 }
7960 }
7961 else
7efd8fc2 7962 create_breakpoints_sal (gdbarch, sals, &canonical, cond_string,
3e43a32a
MS
7963 type_wanted,
7964 tempflag ? disp_del : disp_donttouch,
0fb4aa4b 7965 thread, task, ignore_count, ops, from_tty,
84f4c1fe 7966 enabled, internal);
c906108c 7967 }
0101ce28
JJ
7968 else
7969 {
0101ce28
JJ
7970 struct breakpoint *b;
7971
0101ce28
JJ
7972 make_cleanup (xfree, copy_arg);
7973
348d480f 7974 b = set_raw_breakpoint_without_location (gdbarch, type_wanted, ops);
84f4c1fe 7975 set_breakpoint_number (internal, b);
72b2ff0e 7976 b->thread = -1;
7efd8fc2 7977 b->addr_string = canonical.canonical[0];
72b2ff0e 7978 b->cond_string = NULL;
0101ce28 7979 b->ignore_count = ignore_count;
0101ce28 7980 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 7981 b->condition_not_parsed = 1;
41447f92 7982 b->enable_state = enabled ? bp_enabled : bp_disabled;
6c95b8df 7983 b->pspace = current_program_space;
84f4c1fe 7984 b->py_bp_object = NULL;
74960c60 7985
6c95b8df 7986 if (enabled && b->pspace->executing_startup
8bea4e01
UW
7987 && (b->type == bp_breakpoint
7988 || b->type == bp_hardware_breakpoint))
7989 b->enable_state = bp_startup_disabled;
7990
8d3788bd 7991 if (!internal)
84f4c1fe
PM
7992 /* Do not mention breakpoints with a negative number,
7993 but do notify observers. */
8d3788bd
VP
7994 mention (b);
7995 observer_notify_breakpoint_created (b);
0101ce28
JJ
7996 }
7997
c3f6f71d 7998 if (sals.nelts > 1)
95a42b64 7999 {
3e43a32a
MS
8000 warning (_("Multiple breakpoints were set.\nUse the "
8001 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 8002 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
8003 }
8004
80c99de1
PA
8005 /* That's it. Discard the cleanups for data inserted into the
8006 breakpoint. */
8007 discard_cleanups (bkpt_chain);
8008 /* But cleanup everything else. */
c3f6f71d 8009 do_cleanups (old_chain);
217dc9e2 8010
80c99de1 8011 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 8012 update_global_location_list (1);
fd9b8c24
PA
8013
8014 return 1;
c3f6f71d 8015}
c906108c 8016
348d480f 8017/* Set a breakpoint.
72b2ff0e
VP
8018 ARG is a string describing breakpoint address,
8019 condition, and thread.
8020 FLAG specifies if a breakpoint is hardware on,
8021 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8022 and BP_TEMPFLAG. */
348d480f 8023
98deb0da 8024static void
72b2ff0e 8025break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 8026{
72b2ff0e 8027 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
8028 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8029 ? bp_hardware_breakpoint
8030 : bp_breakpoint);
c3f6f71d 8031
8cdf0e15
VP
8032 create_breakpoint (get_current_arch (),
8033 arg,
8034 NULL, 0, 1 /* parse arg */,
0fb4aa4b 8035 tempflag, type_wanted,
8cdf0e15
VP
8036 0 /* Ignore count */,
8037 pending_break_support,
348d480f 8038 &bkpt_breakpoint_ops,
8cdf0e15 8039 from_tty,
84f4c1fe
PM
8040 1 /* enabled */,
8041 0 /* internal */);
c906108c
SS
8042}
8043
c906108c
SS
8044/* Helper function for break_command_1 and disassemble_command. */
8045
8046void
fba45db2 8047resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
8048{
8049 CORE_ADDR pc;
8050
8051 if (sal->pc == 0 && sal->symtab != NULL)
8052 {
8053 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 8054 error (_("No line %d in file \"%s\"."),
c906108c
SS
8055 sal->line, sal->symtab->filename);
8056 sal->pc = pc;
6a048695 8057
4a64f543
MS
8058 /* If this SAL corresponds to a breakpoint inserted using a line
8059 number, then skip the function prologue if necessary. */
6a048695 8060 if (sal->explicit_line)
059acae7 8061 skip_prologue_sal (sal);
c906108c
SS
8062 }
8063
8064 if (sal->section == 0 && sal->symtab != NULL)
8065 {
8066 struct blockvector *bv;
c5aa993b
JM
8067 struct block *b;
8068 struct symbol *sym;
c906108c 8069
801e3a5b 8070 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
8071 if (bv != NULL)
8072 {
7f0df278 8073 sym = block_linkage_function (b);
c906108c
SS
8074 if (sym != NULL)
8075 {
8076 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 8077 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
8078 }
8079 else
8080 {
4a64f543
MS
8081 /* It really is worthwhile to have the section, so we'll
8082 just have to look harder. This case can be executed
8083 if we have line numbers but no functions (as can
8084 happen in assembly source). */
c906108c 8085
c5aa993b 8086 struct minimal_symbol *msym;
6c95b8df
PA
8087 struct cleanup *old_chain = save_current_space_and_thread ();
8088
8089 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
8090
8091 msym = lookup_minimal_symbol_by_pc (sal->pc);
8092 if (msym)
714835d5 8093 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
8094
8095 do_cleanups (old_chain);
c906108c
SS
8096 }
8097 }
8098 }
8099}
8100
8101void
fba45db2 8102break_command (char *arg, int from_tty)
c906108c 8103{
db107f19 8104 break_command_1 (arg, 0, from_tty);
c906108c
SS
8105}
8106
c906108c 8107void
fba45db2 8108tbreak_command (char *arg, int from_tty)
c906108c 8109{
db107f19 8110 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
8111}
8112
c906108c 8113static void
fba45db2 8114hbreak_command (char *arg, int from_tty)
c906108c 8115{
db107f19 8116 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
8117}
8118
8119static void
fba45db2 8120thbreak_command (char *arg, int from_tty)
c906108c 8121{
db107f19 8122 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
8123}
8124
8125static void
fba45db2 8126stop_command (char *arg, int from_tty)
c906108c 8127{
a3f17187 8128 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 8129Usage: stop in <function | address>\n\
a3f17187 8130 stop at <line>\n"));
c906108c
SS
8131}
8132
8133static void
fba45db2 8134stopin_command (char *arg, int from_tty)
c906108c
SS
8135{
8136 int badInput = 0;
8137
c5aa993b 8138 if (arg == (char *) NULL)
c906108c
SS
8139 badInput = 1;
8140 else if (*arg != '*')
8141 {
8142 char *argptr = arg;
8143 int hasColon = 0;
8144
4a64f543 8145 /* Look for a ':'. If this is a line number specification, then
53a5351d 8146 say it is bad, otherwise, it should be an address or
4a64f543 8147 function/method name. */
c906108c 8148 while (*argptr && !hasColon)
c5aa993b
JM
8149 {
8150 hasColon = (*argptr == ':');
8151 argptr++;
8152 }
c906108c
SS
8153
8154 if (hasColon)
c5aa993b 8155 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 8156 else
c5aa993b 8157 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
8158 }
8159
8160 if (badInput)
a3f17187 8161 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 8162 else
db107f19 8163 break_command_1 (arg, 0, from_tty);
c906108c
SS
8164}
8165
8166static void
fba45db2 8167stopat_command (char *arg, int from_tty)
c906108c
SS
8168{
8169 int badInput = 0;
8170
c5aa993b 8171 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
8172 badInput = 1;
8173 else
8174 {
8175 char *argptr = arg;
8176 int hasColon = 0;
8177
4a64f543
MS
8178 /* Look for a ':'. If there is a '::' then get out, otherwise
8179 it is probably a line number. */
c906108c 8180 while (*argptr && !hasColon)
c5aa993b
JM
8181 {
8182 hasColon = (*argptr == ':');
8183 argptr++;
8184 }
c906108c
SS
8185
8186 if (hasColon)
c5aa993b 8187 badInput = (*argptr == ':'); /* we have class::method */
c906108c 8188 else
c5aa993b 8189 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
8190 }
8191
8192 if (badInput)
a3f17187 8193 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 8194 else
db107f19 8195 break_command_1 (arg, 0, from_tty);
c906108c
SS
8196}
8197
f1310107
TJB
8198/* Implement the "breakpoint_hit" breakpoint_ops method for
8199 ranged breakpoints. */
8200
8201static int
8202breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
8203 struct address_space *aspace,
8204 CORE_ADDR bp_addr)
8205{
8206 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
8207 bl->length, aspace, bp_addr);
8208}
8209
8210/* Implement the "resources_needed" breakpoint_ops method for
8211 ranged breakpoints. */
8212
8213static int
8214resources_needed_ranged_breakpoint (const struct bp_location *bl)
8215{
8216 return target_ranged_break_num_registers ();
8217}
8218
8219/* Implement the "print_it" breakpoint_ops method for
8220 ranged breakpoints. */
8221
8222static enum print_stop_action
348d480f 8223print_it_ranged_breakpoint (bpstat bs)
f1310107 8224{
348d480f 8225 struct breakpoint *b = bs->breakpoint_at;
f1310107 8226 struct bp_location *bl = b->loc;
79a45e25 8227 struct ui_out *uiout = current_uiout;
f1310107
TJB
8228
8229 gdb_assert (b->type == bp_hardware_breakpoint);
8230
8231 /* Ranged breakpoints have only one location. */
8232 gdb_assert (bl && bl->next == NULL);
8233
8234 annotate_breakpoint (b->number);
8235 if (b->disposition == disp_del)
8236 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
8237 else
8238 ui_out_text (uiout, "\nRanged breakpoint ");
8239 if (ui_out_is_mi_like_p (uiout))
8240 {
8241 ui_out_field_string (uiout, "reason",
8242 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
8243 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8244 }
8245 ui_out_field_int (uiout, "bkptno", b->number);
8246 ui_out_text (uiout, ", ");
8247
8248 return PRINT_SRC_AND_LOC;
8249}
8250
8251/* Implement the "print_one" breakpoint_ops method for
8252 ranged breakpoints. */
8253
8254static void
8255print_one_ranged_breakpoint (struct breakpoint *b,
8256 struct bp_location **last_loc)
8257{
8258 struct bp_location *bl = b->loc;
8259 struct value_print_options opts;
79a45e25 8260 struct ui_out *uiout = current_uiout;
f1310107
TJB
8261
8262 /* Ranged breakpoints have only one location. */
8263 gdb_assert (bl && bl->next == NULL);
8264
8265 get_user_print_options (&opts);
8266
8267 if (opts.addressprint)
8268 /* We don't print the address range here, it will be printed later
8269 by print_one_detail_ranged_breakpoint. */
8270 ui_out_field_skip (uiout, "addr");
8271 annotate_field (5);
8272 print_breakpoint_location (b, bl);
8273 *last_loc = bl;
8274}
8275
8276/* Implement the "print_one_detail" breakpoint_ops method for
8277 ranged breakpoints. */
8278
8279static void
8280print_one_detail_ranged_breakpoint (const struct breakpoint *b,
8281 struct ui_out *uiout)
8282{
8283 CORE_ADDR address_start, address_end;
8284 struct bp_location *bl = b->loc;
8285 struct ui_stream *stb = ui_out_stream_new (uiout);
8286 struct cleanup *cleanup = make_cleanup_ui_out_stream_delete (stb);
8287
8288 gdb_assert (bl);
8289
8290 address_start = bl->address;
8291 address_end = address_start + bl->length - 1;
8292
8293 ui_out_text (uiout, "\taddress range: ");
8294 fprintf_unfiltered (stb->stream, "[%s, %s]",
8295 print_core_address (bl->gdbarch, address_start),
8296 print_core_address (bl->gdbarch, address_end));
8297 ui_out_field_stream (uiout, "addr", stb);
8298 ui_out_text (uiout, "\n");
8299
8300 do_cleanups (cleanup);
8301}
8302
8303/* Implement the "print_mention" breakpoint_ops method for
8304 ranged breakpoints. */
8305
8306static void
8307print_mention_ranged_breakpoint (struct breakpoint *b)
8308{
8309 struct bp_location *bl = b->loc;
79a45e25 8310 struct ui_out *uiout = current_uiout;
f1310107
TJB
8311
8312 gdb_assert (bl);
8313 gdb_assert (b->type == bp_hardware_breakpoint);
8314
8315 if (ui_out_is_mi_like_p (uiout))
8316 return;
8317
8318 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
8319 b->number, paddress (bl->gdbarch, bl->address),
8320 paddress (bl->gdbarch, bl->address + bl->length - 1));
8321}
8322
8323/* Implement the "print_recreate" breakpoint_ops method for
8324 ranged breakpoints. */
8325
8326static void
8327print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
8328{
8329 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
8330 b->addr_string_range_end);
d9b3f62e 8331 print_recreate_thread (b, fp);
f1310107
TJB
8332}
8333
8334/* The breakpoint_ops structure to be used in ranged breakpoints. */
8335
2060206e 8336static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
8337
8338/* Find the address where the end of the breakpoint range should be
8339 placed, given the SAL of the end of the range. This is so that if
8340 the user provides a line number, the end of the range is set to the
8341 last instruction of the given line. */
8342
8343static CORE_ADDR
8344find_breakpoint_range_end (struct symtab_and_line sal)
8345{
8346 CORE_ADDR end;
8347
8348 /* If the user provided a PC value, use it. Otherwise,
8349 find the address of the end of the given location. */
8350 if (sal.explicit_pc)
8351 end = sal.pc;
8352 else
8353 {
8354 int ret;
8355 CORE_ADDR start;
8356
8357 ret = find_line_pc_range (sal, &start, &end);
8358 if (!ret)
8359 error (_("Could not find location of the end of the range."));
8360
8361 /* find_line_pc_range returns the start of the next line. */
8362 end--;
8363 }
8364
8365 return end;
8366}
8367
8368/* Implement the "break-range" CLI command. */
8369
8370static void
8371break_range_command (char *arg, int from_tty)
8372{
8373 char *arg_start, *addr_string_start, *addr_string_end;
8374 struct linespec_result canonical_start, canonical_end;
8375 int bp_count, can_use_bp, length;
8376 CORE_ADDR end;
8377 struct breakpoint *b;
8378 struct symtab_and_line sal_start, sal_end;
8379 struct symtabs_and_lines sals_start, sals_end;
8380 struct cleanup *cleanup_bkpt;
8381
8382 /* We don't support software ranged breakpoints. */
8383 if (target_ranged_break_num_registers () < 0)
8384 error (_("This target does not support hardware ranged breakpoints."));
8385
8386 bp_count = hw_breakpoint_used_count ();
8387 bp_count += target_ranged_break_num_registers ();
8388 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8389 bp_count, 0);
8390 if (can_use_bp < 0)
8391 error (_("Hardware breakpoints used exceeds limit."));
8392
8393 if (arg == NULL || arg[0] == '\0')
8394 error(_("No address range specified."));
8395
8396 sals_start.sals = NULL;
8397 sals_start.nelts = 0;
8398 init_linespec_result (&canonical_start);
8399
8400 while (*arg == ' ' || *arg == '\t')
8401 arg++;
8402
58438ac1 8403 parse_breakpoint_sals (&arg, &sals_start, &canonical_start);
f1310107
TJB
8404
8405 sal_start = sals_start.sals[0];
8406 addr_string_start = canonical_start.canonical[0];
8407 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
8408 xfree (sals_start.sals);
8409 xfree (canonical_start.canonical);
8410
8411 if (arg[0] != ',')
8412 error (_("Too few arguments."));
8413 else if (sals_start.nelts == 0)
8414 error (_("Could not find location of the beginning of the range."));
8415 else if (sals_start.nelts != 1)
8416 error (_("Cannot create a ranged breakpoint with multiple locations."));
8417
8418 resolve_sal_pc (&sal_start);
8419
8420 arg++; /* Skip the comma. */
8421 while (*arg == ' ' || *arg == '\t')
8422 arg++;
8423
8424 /* Parse the end location. */
8425
8426 sals_end.sals = NULL;
8427 sals_end.nelts = 0;
8428 init_linespec_result (&canonical_end);
8429 arg_start = arg;
8430
423f41a5 8431 /* We call decode_line_1 directly here instead of using
f1310107
TJB
8432 parse_breakpoint_sals because we need to specify the start location's
8433 symtab and line as the default symtab and line for the end of the
8434 range. This makes it possible to have ranges like "foo.c:27, +14",
8435 where +14 means 14 lines from the start location. */
8436 sals_end = decode_line_1 (&arg, 1, sal_start.symtab, sal_start.line,
58438ac1 8437 &canonical_end);
f1310107
TJB
8438
8439 /* canonical_end can be NULL if it was of the form "*0xdeadbeef". */
8440 if (canonical_end.canonical == NULL)
38a714bb 8441 canonical_end.canonical = xcalloc (1, sizeof (char *));
f1310107
TJB
8442 /* Add the string if not present. */
8443 if (arg_start != arg && canonical_end.canonical[0] == NULL)
8444 canonical_end.canonical[0] = savestring (arg_start, arg - arg_start);
8445
8446 sal_end = sals_end.sals[0];
8447 addr_string_end = canonical_end.canonical[0];
8448 make_cleanup (xfree, addr_string_end);
8449 xfree (sals_end.sals);
8450 xfree (canonical_end.canonical);
8451
8452 if (sals_end.nelts == 0)
8453 error (_("Could not find location of the end of the range."));
8454 else if (sals_end.nelts != 1)
8455 error (_("Cannot create a ranged breakpoint with multiple locations."));
8456
8457 resolve_sal_pc (&sal_end);
8458
8459 end = find_breakpoint_range_end (sal_end);
8460 if (sal_start.pc > end)
177b42fe 8461 error (_("Invalid address range, end precedes start."));
f1310107
TJB
8462
8463 length = end - sal_start.pc + 1;
8464 if (length < 0)
8465 /* Length overflowed. */
8466 error (_("Address range too large."));
8467 else if (length == 1)
8468 {
8469 /* This range is simple enough to be handled by
8470 the `hbreak' command. */
8471 hbreak_command (addr_string_start, 1);
8472
8473 do_cleanups (cleanup_bkpt);
8474
8475 return;
8476 }
8477
8478 /* Now set up the breakpoint. */
8479 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 8480 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
8481 set_breakpoint_count (breakpoint_count + 1);
8482 b->number = breakpoint_count;
8483 b->disposition = disp_donttouch;
8484 b->addr_string = addr_string_start;
8485 b->addr_string_range_end = addr_string_end;
f1310107
TJB
8486 b->loc->length = length;
8487
8488 discard_cleanups (cleanup_bkpt);
8489
8490 mention (b);
8d3788bd 8491 observer_notify_breakpoint_created (b);
f1310107
TJB
8492 update_global_location_list (1);
8493}
8494
4a64f543
MS
8495/* Return non-zero if EXP is verified as constant. Returned zero
8496 means EXP is variable. Also the constant detection may fail for
8497 some constant expressions and in such case still falsely return
8498 zero. */
65d79d4b
SDJ
8499static int
8500watchpoint_exp_is_const (const struct expression *exp)
8501{
8502 int i = exp->nelts;
8503
8504 while (i > 0)
8505 {
8506 int oplenp, argsp;
8507
8508 /* We are only interested in the descriptor of each element. */
8509 operator_length (exp, i, &oplenp, &argsp);
8510 i -= oplenp;
8511
8512 switch (exp->elts[i].opcode)
8513 {
8514 case BINOP_ADD:
8515 case BINOP_SUB:
8516 case BINOP_MUL:
8517 case BINOP_DIV:
8518 case BINOP_REM:
8519 case BINOP_MOD:
8520 case BINOP_LSH:
8521 case BINOP_RSH:
8522 case BINOP_LOGICAL_AND:
8523 case BINOP_LOGICAL_OR:
8524 case BINOP_BITWISE_AND:
8525 case BINOP_BITWISE_IOR:
8526 case BINOP_BITWISE_XOR:
8527 case BINOP_EQUAL:
8528 case BINOP_NOTEQUAL:
8529 case BINOP_LESS:
8530 case BINOP_GTR:
8531 case BINOP_LEQ:
8532 case BINOP_GEQ:
8533 case BINOP_REPEAT:
8534 case BINOP_COMMA:
8535 case BINOP_EXP:
8536 case BINOP_MIN:
8537 case BINOP_MAX:
8538 case BINOP_INTDIV:
8539 case BINOP_CONCAT:
8540 case BINOP_IN:
8541 case BINOP_RANGE:
8542 case TERNOP_COND:
8543 case TERNOP_SLICE:
8544 case TERNOP_SLICE_COUNT:
8545
8546 case OP_LONG:
8547 case OP_DOUBLE:
8548 case OP_DECFLOAT:
8549 case OP_LAST:
8550 case OP_COMPLEX:
8551 case OP_STRING:
8552 case OP_BITSTRING:
8553 case OP_ARRAY:
8554 case OP_TYPE:
8555 case OP_NAME:
8556 case OP_OBJC_NSSTRING:
8557
8558 case UNOP_NEG:
8559 case UNOP_LOGICAL_NOT:
8560 case UNOP_COMPLEMENT:
8561 case UNOP_ADDR:
8562 case UNOP_HIGH:
4a64f543
MS
8563 /* Unary, binary and ternary operators: We have to check
8564 their operands. If they are constant, then so is the
8565 result of that operation. For instance, if A and B are
8566 determined to be constants, then so is "A + B".
8567
8568 UNOP_IND is one exception to the rule above, because the
8569 value of *ADDR is not necessarily a constant, even when
8570 ADDR is. */
65d79d4b
SDJ
8571 break;
8572
8573 case OP_VAR_VALUE:
8574 /* Check whether the associated symbol is a constant.
4a64f543 8575
65d79d4b 8576 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
8577 possible that a buggy compiler could mark a variable as
8578 constant even when it is not, and TYPE_CONST would return
8579 true in this case, while SYMBOL_CLASS wouldn't.
8580
8581 We also have to check for function symbols because they
8582 are always constant. */
65d79d4b
SDJ
8583 {
8584 struct symbol *s = exp->elts[i + 2].symbol;
8585
8586 if (SYMBOL_CLASS (s) != LOC_BLOCK
8587 && SYMBOL_CLASS (s) != LOC_CONST
8588 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
8589 return 0;
8590 break;
8591 }
8592
8593 /* The default action is to return 0 because we are using
8594 the optimistic approach here: If we don't know something,
8595 then it is not a constant. */
8596 default:
8597 return 0;
8598 }
8599 }
8600
8601 return 1;
8602}
8603
3a5c3e22
PA
8604/* Implement the "dtor" breakpoint_ops method for watchpoints. */
8605
8606static void
8607dtor_watchpoint (struct breakpoint *self)
8608{
8609 struct watchpoint *w = (struct watchpoint *) self;
8610
8611 xfree (w->cond_exp);
8612 xfree (w->exp);
8613 xfree (w->exp_string);
8614 xfree (w->exp_string_reparse);
8615 value_free (w->val);
8616
8617 base_breakpoint_ops.dtor (self);
8618}
8619
348d480f
PA
8620/* Implement the "re_set" breakpoint_ops method for watchpoints. */
8621
8622static void
8623re_set_watchpoint (struct breakpoint *b)
8624{
3a5c3e22
PA
8625 struct watchpoint *w = (struct watchpoint *) b;
8626
348d480f
PA
8627 /* Watchpoint can be either on expression using entirely global
8628 variables, or it can be on local variables.
8629
8630 Watchpoints of the first kind are never auto-deleted, and even
8631 persist across program restarts. Since they can use variables
8632 from shared libraries, we need to reparse expression as libraries
8633 are loaded and unloaded.
8634
8635 Watchpoints on local variables can also change meaning as result
8636 of solib event. For example, if a watchpoint uses both a local
8637 and a global variables in expression, it's a local watchpoint,
8638 but unloading of a shared library will make the expression
8639 invalid. This is not a very common use case, but we still
8640 re-evaluate expression, to avoid surprises to the user.
8641
8642 Note that for local watchpoints, we re-evaluate it only if
8643 watchpoints frame id is still valid. If it's not, it means the
8644 watchpoint is out of scope and will be deleted soon. In fact,
8645 I'm not sure we'll ever be called in this case.
8646
8647 If a local watchpoint's frame id is still valid, then
3a5c3e22 8648 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 8649
3a5c3e22
PA
8650 Don't do anything about disabled watchpoints, since they will be
8651 reevaluated again when enabled. */
8652 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
8653}
8654
77b06cd7
TJB
8655/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
8656
8657static int
8658insert_watchpoint (struct bp_location *bl)
8659{
3a5c3e22
PA
8660 struct watchpoint *w = (struct watchpoint *) bl->owner;
8661 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8662
8663 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8664 w->cond_exp);
77b06cd7
TJB
8665}
8666
8667/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
8668
8669static int
8670remove_watchpoint (struct bp_location *bl)
8671{
3a5c3e22
PA
8672 struct watchpoint *w = (struct watchpoint *) bl->owner;
8673 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
8674
8675 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 8676 w->cond_exp);
e09342b5
TJB
8677}
8678
e09342b5 8679static int
348d480f
PA
8680breakpoint_hit_watchpoint (const struct bp_location *bl,
8681 struct address_space *aspace, CORE_ADDR bp_addr)
e09342b5 8682{
348d480f 8683 struct breakpoint *b = bl->owner;
3a5c3e22 8684 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 8685
348d480f
PA
8686 /* Continuable hardware watchpoints are treated as non-existent if the
8687 reason we stopped wasn't a hardware watchpoint (we didn't stop on
8688 some data address). Otherwise gdb won't stop on a break instruction
8689 in the code (not from a breakpoint) when a hardware watchpoint has
8690 been defined. Also skip watchpoints which we know did not trigger
8691 (did not match the data address). */
8692 if (is_hardware_watchpoint (b)
3a5c3e22 8693 && w->watchpoint_triggered == watch_triggered_no)
348d480f 8694 return 0;
9c06b0b4 8695
348d480f 8696 return 1;
9c06b0b4
TJB
8697}
8698
348d480f
PA
8699static void
8700check_status_watchpoint (bpstat bs)
9c06b0b4 8701{
348d480f 8702 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 8703
348d480f 8704 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
8705}
8706
8707/* Implement the "resources_needed" breakpoint_ops method for
348d480f 8708 hardware watchpoints. */
9c06b0b4
TJB
8709
8710static int
348d480f 8711resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 8712{
3a5c3e22
PA
8713 struct watchpoint *w = (struct watchpoint *) bl->owner;
8714 int length = w->exact? 1 : bl->length;
348d480f
PA
8715
8716 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
8717}
8718
8719/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 8720 hardware watchpoints. */
9c06b0b4
TJB
8721
8722static int
348d480f 8723works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 8724{
efa80663
PA
8725 /* Read and access watchpoints only work with hardware support. */
8726 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
8727}
8728
9c06b0b4 8729static enum print_stop_action
348d480f 8730print_it_watchpoint (bpstat bs)
9c06b0b4 8731{
348d480f
PA
8732 struct cleanup *old_chain;
8733 struct breakpoint *b;
8734 const struct bp_location *bl;
8735 struct ui_stream *stb;
8736 enum print_stop_action result;
3a5c3e22 8737 struct watchpoint *w;
79a45e25 8738 struct ui_out *uiout = current_uiout;
348d480f
PA
8739
8740 gdb_assert (bs->bp_location_at != NULL);
8741
8742 bl = bs->bp_location_at;
8743 b = bs->breakpoint_at;
3a5c3e22 8744 w = (struct watchpoint *) b;
348d480f
PA
8745
8746 stb = ui_out_stream_new (uiout);
8747 old_chain = make_cleanup_ui_out_stream_delete (stb);
9c06b0b4
TJB
8748
8749 switch (b->type)
8750 {
348d480f 8751 case bp_watchpoint:
9c06b0b4
TJB
8752 case bp_hardware_watchpoint:
8753 annotate_watchpoint (b->number);
8754 if (ui_out_is_mi_like_p (uiout))
8755 ui_out_field_string
8756 (uiout, "reason",
8757 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
8758 mention (b);
8759 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8760 ui_out_text (uiout, "\nOld value = ");
8761 watchpoint_value_print (bs->old_val, stb->stream);
8762 ui_out_field_stream (uiout, "old", stb);
8763 ui_out_text (uiout, "\nNew value = ");
3a5c3e22 8764 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8765 ui_out_field_stream (uiout, "new", stb);
8766 ui_out_text (uiout, "\n");
8767 /* More than one watchpoint may have been triggered. */
8768 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8769 break;
8770
8771 case bp_read_watchpoint:
8772 if (ui_out_is_mi_like_p (uiout))
8773 ui_out_field_string
8774 (uiout, "reason",
8775 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
8776 mention (b);
8777 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8778 ui_out_text (uiout, "\nValue = ");
3a5c3e22 8779 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8780 ui_out_field_stream (uiout, "value", stb);
8781 ui_out_text (uiout, "\n");
8782 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8783 break;
8784
8785 case bp_access_watchpoint:
348d480f
PA
8786 if (bs->old_val != NULL)
8787 {
8788 annotate_watchpoint (b->number);
8789 if (ui_out_is_mi_like_p (uiout))
8790 ui_out_field_string
8791 (uiout, "reason",
8792 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8793 mention (b);
8794 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8795 ui_out_text (uiout, "\nOld value = ");
8796 watchpoint_value_print (bs->old_val, stb->stream);
8797 ui_out_field_stream (uiout, "old", stb);
8798 ui_out_text (uiout, "\nNew value = ");
8799 }
8800 else
8801 {
8802 mention (b);
8803 if (ui_out_is_mi_like_p (uiout))
8804 ui_out_field_string
8805 (uiout, "reason",
8806 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8807 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8808 ui_out_text (uiout, "\nValue = ");
8809 }
3a5c3e22 8810 watchpoint_value_print (w->val, stb->stream);
348d480f
PA
8811 ui_out_field_stream (uiout, "new", stb);
8812 ui_out_text (uiout, "\n");
8813 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8814 break;
8815 default:
348d480f 8816 result = PRINT_UNKNOWN;
9c06b0b4
TJB
8817 }
8818
348d480f
PA
8819 do_cleanups (old_chain);
8820 return result;
8821}
8822
8823/* Implement the "print_mention" breakpoint_ops method for hardware
8824 watchpoints. */
8825
8826static void
8827print_mention_watchpoint (struct breakpoint *b)
8828{
8829 struct cleanup *ui_out_chain;
3a5c3e22 8830 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 8831 struct ui_out *uiout = current_uiout;
348d480f
PA
8832
8833 switch (b->type)
8834 {
8835 case bp_watchpoint:
8836 ui_out_text (uiout, "Watchpoint ");
8837 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8838 break;
8839 case bp_hardware_watchpoint:
8840 ui_out_text (uiout, "Hardware watchpoint ");
8841 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
8842 break;
8843 case bp_read_watchpoint:
8844 ui_out_text (uiout, "Hardware read watchpoint ");
8845 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
8846 break;
8847 case bp_access_watchpoint:
8848 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
8849 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
8850 break;
8851 default:
8852 internal_error (__FILE__, __LINE__,
8853 _("Invalid hardware watchpoint type."));
8854 }
8855
8856 ui_out_field_int (uiout, "number", b->number);
8857 ui_out_text (uiout, ": ");
3a5c3e22 8858 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
8859 do_cleanups (ui_out_chain);
8860}
8861
8862/* Implement the "print_recreate" breakpoint_ops method for
8863 watchpoints. */
8864
8865static void
8866print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
8867{
3a5c3e22
PA
8868 struct watchpoint *w = (struct watchpoint *) b;
8869
348d480f
PA
8870 switch (b->type)
8871 {
8872 case bp_watchpoint:
8873 case bp_hardware_watchpoint:
8874 fprintf_unfiltered (fp, "watch");
8875 break;
8876 case bp_read_watchpoint:
8877 fprintf_unfiltered (fp, "rwatch");
8878 break;
8879 case bp_access_watchpoint:
8880 fprintf_unfiltered (fp, "awatch");
8881 break;
8882 default:
8883 internal_error (__FILE__, __LINE__,
8884 _("Invalid watchpoint type."));
8885 }
8886
3a5c3e22 8887 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 8888 print_recreate_thread (b, fp);
348d480f
PA
8889}
8890
8891/* The breakpoint_ops structure to be used in hardware watchpoints. */
8892
2060206e 8893static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
8894
8895/* Implement the "insert" breakpoint_ops method for
8896 masked hardware watchpoints. */
8897
8898static int
8899insert_masked_watchpoint (struct bp_location *bl)
8900{
3a5c3e22
PA
8901 struct watchpoint *w = (struct watchpoint *) bl->owner;
8902
8903 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8904 bl->watchpoint_type);
8905}
8906
8907/* Implement the "remove" breakpoint_ops method for
8908 masked hardware watchpoints. */
8909
8910static int
8911remove_masked_watchpoint (struct bp_location *bl)
8912{
3a5c3e22
PA
8913 struct watchpoint *w = (struct watchpoint *) bl->owner;
8914
8915 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
8916 bl->watchpoint_type);
8917}
8918
8919/* Implement the "resources_needed" breakpoint_ops method for
8920 masked hardware watchpoints. */
8921
8922static int
8923resources_needed_masked_watchpoint (const struct bp_location *bl)
8924{
3a5c3e22
PA
8925 struct watchpoint *w = (struct watchpoint *) bl->owner;
8926
8927 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
8928}
8929
8930/* Implement the "works_in_software_mode" breakpoint_ops method for
8931 masked hardware watchpoints. */
8932
8933static int
8934works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
8935{
8936 return 0;
8937}
8938
8939/* Implement the "print_it" breakpoint_ops method for
8940 masked hardware watchpoints. */
8941
8942static enum print_stop_action
8943print_it_masked_watchpoint (bpstat bs)
8944{
8945 struct breakpoint *b = bs->breakpoint_at;
79a45e25 8946 struct ui_out *uiout = current_uiout;
348d480f
PA
8947
8948 /* Masked watchpoints have only one location. */
8949 gdb_assert (b->loc && b->loc->next == NULL);
8950
8951 switch (b->type)
8952 {
8953 case bp_hardware_watchpoint:
8954 annotate_watchpoint (b->number);
8955 if (ui_out_is_mi_like_p (uiout))
8956 ui_out_field_string
8957 (uiout, "reason",
8958 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
8959 break;
8960
8961 case bp_read_watchpoint:
8962 if (ui_out_is_mi_like_p (uiout))
8963 ui_out_field_string
8964 (uiout, "reason",
8965 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
8966 break;
8967
8968 case bp_access_watchpoint:
8969 if (ui_out_is_mi_like_p (uiout))
8970 ui_out_field_string
8971 (uiout, "reason",
8972 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
8973 break;
8974 default:
8975 internal_error (__FILE__, __LINE__,
8976 _("Invalid hardware watchpoint type."));
8977 }
8978
8979 mention (b);
9c06b0b4
TJB
8980 ui_out_text (uiout, _("\n\
8981Check the underlying instruction at PC for the memory\n\
8982address and value which triggered this watchpoint.\n"));
8983 ui_out_text (uiout, "\n");
8984
8985 /* More than one watchpoint may have been triggered. */
8986 return PRINT_UNKNOWN;
8987}
8988
8989/* Implement the "print_one_detail" breakpoint_ops method for
8990 masked hardware watchpoints. */
8991
8992static void
8993print_one_detail_masked_watchpoint (const struct breakpoint *b,
8994 struct ui_out *uiout)
8995{
3a5c3e22
PA
8996 struct watchpoint *w = (struct watchpoint *) b;
8997
9c06b0b4
TJB
8998 /* Masked watchpoints have only one location. */
8999 gdb_assert (b->loc && b->loc->next == NULL);
9000
9001 ui_out_text (uiout, "\tmask ");
3a5c3e22 9002 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
9003 ui_out_text (uiout, "\n");
9004}
9005
9006/* Implement the "print_mention" breakpoint_ops method for
9007 masked hardware watchpoints. */
9008
9009static void
9010print_mention_masked_watchpoint (struct breakpoint *b)
9011{
3a5c3e22 9012 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 9013 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
9014 struct cleanup *ui_out_chain;
9015
9016 switch (b->type)
9017 {
9018 case bp_hardware_watchpoint:
9019 ui_out_text (uiout, "Masked hardware watchpoint ");
9020 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
9021 break;
9022 case bp_read_watchpoint:
9023 ui_out_text (uiout, "Masked hardware read watchpoint ");
9024 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
9025 break;
9026 case bp_access_watchpoint:
9027 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
9028 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
9029 break;
9030 default:
9031 internal_error (__FILE__, __LINE__,
9032 _("Invalid hardware watchpoint type."));
9033 }
9034
9035 ui_out_field_int (uiout, "number", b->number);
9036 ui_out_text (uiout, ": ");
3a5c3e22 9037 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
9038 do_cleanups (ui_out_chain);
9039}
9040
9041/* Implement the "print_recreate" breakpoint_ops method for
9042 masked hardware watchpoints. */
9043
9044static void
9045print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
9046{
3a5c3e22 9047 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
9048 char tmp[40];
9049
9050 switch (b->type)
9051 {
9052 case bp_hardware_watchpoint:
9053 fprintf_unfiltered (fp, "watch");
9054 break;
9055 case bp_read_watchpoint:
9056 fprintf_unfiltered (fp, "rwatch");
9057 break;
9058 case bp_access_watchpoint:
9059 fprintf_unfiltered (fp, "awatch");
9060 break;
9061 default:
9062 internal_error (__FILE__, __LINE__,
9063 _("Invalid hardware watchpoint type."));
9064 }
9065
3a5c3e22
PA
9066 sprintf_vma (tmp, w->hw_wp_mask);
9067 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 9068 print_recreate_thread (b, fp);
9c06b0b4
TJB
9069}
9070
9071/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
9072
2060206e 9073static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
9074
9075/* Tell whether the given watchpoint is a masked hardware watchpoint. */
9076
9077static int
9078is_masked_watchpoint (const struct breakpoint *b)
9079{
9080 return b->ops == &masked_watchpoint_breakpoint_ops;
9081}
9082
53a5351d
JM
9083/* accessflag: hw_write: watch write,
9084 hw_read: watch read,
9085 hw_access: watch access (read or write) */
c906108c 9086static void
84f4c1fe
PM
9087watch_command_1 (char *arg, int accessflag, int from_tty,
9088 int just_location, int internal)
c906108c 9089{
a9634178 9090 volatile struct gdb_exception e;
d983da9c 9091 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 9092 struct expression *exp;
60e1c644 9093 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 9094 struct value *val, *mark, *result;
c906108c 9095 struct frame_info *frame;
c906108c
SS
9096 char *exp_start = NULL;
9097 char *exp_end = NULL;
9c06b0b4
TJB
9098 char *tok, *end_tok;
9099 int toklen = -1;
c906108c
SS
9100 char *cond_start = NULL;
9101 char *cond_end = NULL;
c906108c 9102 enum bptype bp_type;
37e4754d 9103 int thread = -1;
0cf6dd15 9104 int pc = 0;
9c06b0b4
TJB
9105 /* Flag to indicate whether we are going to use masks for
9106 the hardware watchpoint. */
9107 int use_mask = 0;
9108 CORE_ADDR mask = 0;
3a5c3e22 9109 struct watchpoint *w;
c906108c 9110
37e4754d
LM
9111 /* Make sure that we actually have parameters to parse. */
9112 if (arg != NULL && arg[0] != '\0')
9113 {
9c06b0b4 9114 char *value_start;
37e4754d 9115
9c06b0b4
TJB
9116 /* Look for "parameter value" pairs at the end
9117 of the arguments string. */
9118 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
9119 {
9120 /* Skip whitespace at the end of the argument list. */
9121 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9122 tok--;
9123
9124 /* Find the beginning of the last token.
9125 This is the value of the parameter. */
9126 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9127 tok--;
9128 value_start = tok + 1;
9129
9130 /* Skip whitespace. */
9131 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9132 tok--;
9133
9134 end_tok = tok;
9135
9136 /* Find the beginning of the second to last token.
9137 This is the parameter itself. */
9138 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9139 tok--;
9140 tok++;
9141 toklen = end_tok - tok + 1;
9142
9143 if (toklen == 6 && !strncmp (tok, "thread", 6))
9144 {
9145 /* At this point we've found a "thread" token, which means
9146 the user is trying to set a watchpoint that triggers
9147 only in a specific thread. */
9148 char *endp;
37e4754d 9149
9c06b0b4
TJB
9150 if (thread != -1)
9151 error(_("You can specify only one thread."));
37e4754d 9152
9c06b0b4
TJB
9153 /* Extract the thread ID from the next token. */
9154 thread = strtol (value_start, &endp, 0);
37e4754d 9155
9c06b0b4
TJB
9156 /* Check if the user provided a valid numeric value for the
9157 thread ID. */
9158 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9159 error (_("Invalid thread ID specification %s."), value_start);
9160
9161 /* Check if the thread actually exists. */
9162 if (!valid_thread_id (thread))
9163 error (_("Unknown thread %d."), thread);
9164 }
9165 else if (toklen == 4 && !strncmp (tok, "mask", 4))
9166 {
9167 /* We've found a "mask" token, which means the user wants to
9168 create a hardware watchpoint that is going to have the mask
9169 facility. */
9170 struct value *mask_value, *mark;
37e4754d 9171
9c06b0b4
TJB
9172 if (use_mask)
9173 error(_("You can specify only one mask."));
37e4754d 9174
9c06b0b4 9175 use_mask = just_location = 1;
37e4754d 9176
9c06b0b4
TJB
9177 mark = value_mark ();
9178 mask_value = parse_to_comma_and_eval (&value_start);
9179 mask = value_as_address (mask_value);
9180 value_free_to_mark (mark);
9181 }
9182 else
9183 /* We didn't recognize what we found. We should stop here. */
9184 break;
37e4754d 9185
9c06b0b4
TJB
9186 /* Truncate the string and get rid of the "parameter value" pair before
9187 the arguments string is parsed by the parse_exp_1 function. */
9188 *tok = '\0';
9189 }
37e4754d
LM
9190 }
9191
9192 /* Parse the rest of the arguments. */
c906108c
SS
9193 innermost_block = NULL;
9194 exp_start = arg;
9195 exp = parse_exp_1 (&arg, 0, 0);
9196 exp_end = arg;
fa8a61dc
TT
9197 /* Remove trailing whitespace from the expression before saving it.
9198 This makes the eventual display of the expression string a bit
9199 prettier. */
9200 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9201 --exp_end;
9202
65d79d4b
SDJ
9203 /* Checking if the expression is not constant. */
9204 if (watchpoint_exp_is_const (exp))
9205 {
9206 int len;
9207
9208 len = exp_end - exp_start;
9209 while (len > 0 && isspace (exp_start[len - 1]))
9210 len--;
9211 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9212 }
9213
c906108c
SS
9214 exp_valid_block = innermost_block;
9215 mark = value_mark ();
a1442452 9216 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
9217
9218 if (just_location)
9219 {
9c06b0b4
TJB
9220 int ret;
9221
06a64a0b 9222 exp_valid_block = NULL;
a1442452 9223 val = value_addr (result);
06a64a0b
TT
9224 release_value (val);
9225 value_free_to_mark (mark);
9c06b0b4
TJB
9226
9227 if (use_mask)
9228 {
9229 ret = target_masked_watch_num_registers (value_as_address (val),
9230 mask);
9231 if (ret == -1)
9232 error (_("This target does not support masked watchpoints."));
9233 else if (ret == -2)
9234 error (_("Invalid mask or memory region."));
9235 }
06a64a0b
TT
9236 }
9237 else if (val != NULL)
fa4727a6 9238 release_value (val);
c906108c 9239
e9cafbcc
TT
9240 tok = skip_spaces (arg);
9241 end_tok = skip_to_space (tok);
c906108c
SS
9242
9243 toklen = end_tok - tok;
9244 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9245 {
2d134ed3
PA
9246 struct expression *cond;
9247
60e1c644 9248 innermost_block = NULL;
c906108c
SS
9249 tok = cond_start = end_tok + 1;
9250 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
9251
9252 /* The watchpoint expression may not be local, but the condition
9253 may still be. E.g.: `watch global if local > 0'. */
9254 cond_exp_valid_block = innermost_block;
9255
2d134ed3 9256 xfree (cond);
c906108c
SS
9257 cond_end = tok;
9258 }
9259 if (*tok)
8a3fe4f8 9260 error (_("Junk at end of command."));
c906108c 9261
53a5351d 9262 if (accessflag == hw_read)
c5aa993b 9263 bp_type = bp_read_watchpoint;
53a5351d 9264 else if (accessflag == hw_access)
c5aa993b
JM
9265 bp_type = bp_access_watchpoint;
9266 else
9267 bp_type = bp_hardware_watchpoint;
c906108c 9268
d983da9c 9269 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
9270
9271 /* If the expression is "local", then set up a "watchpoint scope"
9272 breakpoint at the point where we've left the scope of the watchpoint
9273 expression. Create the scope breakpoint before the watchpoint, so
9274 that we will encounter it first in bpstat_stop_status. */
60e1c644 9275 if (exp_valid_block && frame)
d983da9c 9276 {
edb3359d
DJ
9277 if (frame_id_p (frame_unwind_caller_id (frame)))
9278 {
9279 scope_breakpoint
a6d9a66e
UW
9280 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
9281 frame_unwind_caller_pc (frame),
06edf0c0
PA
9282 bp_watchpoint_scope,
9283 &momentary_breakpoint_ops);
d983da9c 9284
edb3359d 9285 scope_breakpoint->enable_state = bp_enabled;
d983da9c 9286
edb3359d
DJ
9287 /* Automatically delete the breakpoint when it hits. */
9288 scope_breakpoint->disposition = disp_del;
d983da9c 9289
edb3359d
DJ
9290 /* Only break in the proper frame (help with recursion). */
9291 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 9292
edb3359d 9293 /* Set the address at which we will stop. */
a6d9a66e
UW
9294 scope_breakpoint->loc->gdbarch
9295 = frame_unwind_caller_arch (frame);
edb3359d
DJ
9296 scope_breakpoint->loc->requested_address
9297 = frame_unwind_caller_pc (frame);
9298 scope_breakpoint->loc->address
a6d9a66e
UW
9299 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
9300 scope_breakpoint->loc->requested_address,
edb3359d
DJ
9301 scope_breakpoint->type);
9302 }
d983da9c
DJ
9303 }
9304
c906108c 9305 /* Now set up the breakpoint. */
3a5c3e22
PA
9306
9307 w = XCNEW (struct watchpoint);
9308 b = &w->base;
348d480f 9309 if (use_mask)
3a5c3e22
PA
9310 init_raw_breakpoint_without_location (b, NULL, bp_type,
9311 &masked_watchpoint_breakpoint_ops);
348d480f 9312 else
3a5c3e22
PA
9313 init_raw_breakpoint_without_location (b, NULL, bp_type,
9314 &watchpoint_breakpoint_ops);
37e4754d 9315 b->thread = thread;
b5de0fa7 9316 b->disposition = disp_donttouch;
348d480f 9317 b->pspace = current_program_space;
3a5c3e22
PA
9318 w->exp = exp;
9319 w->exp_valid_block = exp_valid_block;
9320 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
9321 if (just_location)
9322 {
9323 struct type *t = value_type (val);
9324 CORE_ADDR addr = value_as_address (val);
9325 char *name;
9326
9327 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
9328 name = type_to_string (t);
9329
3a5c3e22 9330 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 9331 core_addr_to_string (addr));
06a64a0b
TT
9332 xfree (name);
9333
3a5c3e22 9334 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
9335 (int) (exp_end - exp_start), exp_start);
9336
06a64a0b
TT
9337 /* The above expression is in C. */
9338 b->language = language_c;
9339 }
9340 else
3a5c3e22 9341 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
9342
9343 if (use_mask)
9344 {
3a5c3e22 9345 w->hw_wp_mask = mask;
9c06b0b4
TJB
9346 }
9347 else
9348 {
3a5c3e22
PA
9349 w->val = val;
9350 w->val_valid = 1;
9c06b0b4 9351 }
77b06cd7 9352
c906108c
SS
9353 if (cond_start)
9354 b->cond_string = savestring (cond_start, cond_end - cond_start);
9355 else
9356 b->cond_string = 0;
c5aa993b 9357
c906108c 9358 if (frame)
f6bc2008 9359 {
3a5c3e22
PA
9360 w->watchpoint_frame = get_frame_id (frame);
9361 w->watchpoint_thread = inferior_ptid;
f6bc2008 9362 }
c906108c 9363 else
f6bc2008 9364 {
3a5c3e22
PA
9365 w->watchpoint_frame = null_frame_id;
9366 w->watchpoint_thread = null_ptid;
f6bc2008 9367 }
c906108c 9368
d983da9c 9369 if (scope_breakpoint != NULL)
c906108c 9370 {
d983da9c
DJ
9371 /* The scope breakpoint is related to the watchpoint. We will
9372 need to act on them together. */
9373 b->related_breakpoint = scope_breakpoint;
9374 scope_breakpoint->related_breakpoint = b;
c906108c 9375 }
d983da9c 9376
06a64a0b
TT
9377 if (!just_location)
9378 value_free_to_mark (mark);
2d134ed3 9379
a9634178
TJB
9380 TRY_CATCH (e, RETURN_MASK_ALL)
9381 {
9382 /* Finally update the new watchpoint. This creates the locations
9383 that should be inserted. */
3a5c3e22 9384 update_watchpoint (w, 1);
a9634178
TJB
9385 }
9386 if (e.reason < 0)
9387 {
9388 delete_breakpoint (b);
9389 throw_exception (e);
9390 }
9391
3a5c3e22 9392 install_breakpoint (internal, b);
c906108c
SS
9393}
9394
e09342b5 9395/* Return count of debug registers needed to watch the given expression.
e09342b5 9396 If the watchpoint cannot be handled in hardware return zero. */
c906108c 9397
c906108c 9398static int
a9634178 9399can_use_hardware_watchpoint (struct value *v)
c906108c
SS
9400{
9401 int found_memory_cnt = 0;
2e70b7b9 9402 struct value *head = v;
c906108c
SS
9403
9404 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 9405 if (!can_use_hw_watchpoints)
c906108c 9406 return 0;
c5aa993b 9407
5c44784c
JM
9408 /* Make sure that the value of the expression depends only upon
9409 memory contents, and values computed from them within GDB. If we
9410 find any register references or function calls, we can't use a
9411 hardware watchpoint.
9412
9413 The idea here is that evaluating an expression generates a series
9414 of values, one holding the value of every subexpression. (The
9415 expression a*b+c has five subexpressions: a, b, a*b, c, and
9416 a*b+c.) GDB's values hold almost enough information to establish
9417 the criteria given above --- they identify memory lvalues,
9418 register lvalues, computed values, etcetera. So we can evaluate
9419 the expression, and then scan the chain of values that leaves
9420 behind to decide whether we can detect any possible change to the
9421 expression's final value using only hardware watchpoints.
9422
9423 However, I don't think that the values returned by inferior
9424 function calls are special in any way. So this function may not
9425 notice that an expression involving an inferior function call
9426 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 9427 for (; v; v = value_next (v))
c906108c 9428 {
5c44784c 9429 if (VALUE_LVAL (v) == lval_memory)
c906108c 9430 {
8464be76
DJ
9431 if (v != head && value_lazy (v))
9432 /* A lazy memory lvalue in the chain is one that GDB never
9433 needed to fetch; we either just used its address (e.g.,
9434 `a' in `a.b') or we never needed it at all (e.g., `a'
9435 in `a,b'). This doesn't apply to HEAD; if that is
9436 lazy then it was not readable, but watch it anyway. */
5c44784c 9437 ;
53a5351d 9438 else
5c44784c
JM
9439 {
9440 /* Ahh, memory we actually used! Check if we can cover
9441 it with hardware watchpoints. */
df407dfe 9442 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
9443
9444 /* We only watch structs and arrays if user asked for it
9445 explicitly, never if they just happen to appear in a
9446 middle of some value chain. */
9447 if (v == head
9448 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
9449 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
9450 {
42ae5230 9451 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
9452 int len;
9453 int num_regs;
9454
a9634178 9455 len = (target_exact_watchpoints
e09342b5
TJB
9456 && is_scalar_type_recursive (vtype))?
9457 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 9458
e09342b5
TJB
9459 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
9460 if (!num_regs)
2e70b7b9
MS
9461 return 0;
9462 else
e09342b5 9463 found_memory_cnt += num_regs;
2e70b7b9 9464 }
5c44784c 9465 }
c5aa993b 9466 }
5086187c
AC
9467 else if (VALUE_LVAL (v) != not_lval
9468 && deprecated_value_modifiable (v) == 0)
38b6c3b3 9469 return 0; /* These are values from the history (e.g., $1). */
5086187c 9470 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 9471 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
9472 }
9473
9474 /* The expression itself looks suitable for using a hardware
9475 watchpoint, but give the target machine a chance to reject it. */
9476 return found_memory_cnt;
9477}
9478
8b93c638 9479void
84f4c1fe 9480watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9481{
84f4c1fe 9482 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
9483}
9484
9485/* A helper function that looks for an argument at the start of a
9486 string. The argument must also either be at the end of the string,
9487 or be followed by whitespace. Returns 1 if it finds the argument,
9488 0 otherwise. If the argument is found, it updates *STR. */
9489
9490static int
9491check_for_argument (char **str, char *arg, int arg_len)
9492{
9493 if (strncmp (*str, arg, arg_len) == 0
9494 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
9495 {
9496 *str += arg_len;
9497 return 1;
9498 }
9499 return 0;
9500}
9501
9502/* A helper function that looks for the "-location" argument and then
9503 calls watch_command_1. */
9504
9505static void
9506watch_maybe_just_location (char *arg, int accessflag, int from_tty)
9507{
9508 int just_location = 0;
9509
9510 if (arg
9511 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
9512 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
9513 {
e9cafbcc 9514 arg = skip_spaces (arg);
06a64a0b
TT
9515 just_location = 1;
9516 }
9517
84f4c1fe 9518 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 9519}
8926118c 9520
c5aa993b 9521static void
fba45db2 9522watch_command (char *arg, int from_tty)
c906108c 9523{
06a64a0b 9524 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
9525}
9526
8b93c638 9527void
84f4c1fe 9528rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9529{
84f4c1fe 9530 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 9531}
8926118c 9532
c5aa993b 9533static void
fba45db2 9534rwatch_command (char *arg, int from_tty)
c906108c 9535{
06a64a0b 9536 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
9537}
9538
8b93c638 9539void
84f4c1fe 9540awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 9541{
84f4c1fe 9542 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 9543}
8926118c 9544
c5aa993b 9545static void
fba45db2 9546awatch_command (char *arg, int from_tty)
c906108c 9547{
06a64a0b 9548 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 9549}
c906108c 9550\f
c5aa993b 9551
43ff13b4 9552/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
9553 because it uses the mechanisms of breakpoints. */
9554
bfec99b2
PA
9555struct until_break_command_continuation_args
9556{
9557 struct breakpoint *breakpoint;
9558 struct breakpoint *breakpoint2;
186c406b 9559 int thread_num;
bfec99b2
PA
9560};
9561
43ff13b4 9562/* This function is called by fetch_inferior_event via the
4a64f543 9563 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 9564 care of cleaning up the temporary breakpoints set up by the until
4a64f543 9565 command. */
c2c6d25f 9566static void
fa4cd53f 9567until_break_command_continuation (void *arg, int err)
43ff13b4 9568{
bfec99b2
PA
9569 struct until_break_command_continuation_args *a = arg;
9570
9571 delete_breakpoint (a->breakpoint);
9572 if (a->breakpoint2)
9573 delete_breakpoint (a->breakpoint2);
186c406b 9574 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
9575}
9576
c906108c 9577void
ae66c1fc 9578until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
9579{
9580 struct symtabs_and_lines sals;
9581 struct symtab_and_line sal;
206415a3 9582 struct frame_info *frame = get_selected_frame (NULL);
c906108c 9583 struct breakpoint *breakpoint;
f107f563 9584 struct breakpoint *breakpoint2 = NULL;
c906108c 9585 struct cleanup *old_chain;
186c406b
TT
9586 int thread;
9587 struct thread_info *tp;
c906108c
SS
9588
9589 clear_proceed_status ();
9590
9591 /* Set a breakpoint where the user wants it and at return from
4a64f543 9592 this function. */
c5aa993b 9593
1bfeeb0f
JL
9594 if (last_displayed_sal_is_valid ())
9595 sals = decode_line_1 (&arg, 1,
9596 get_last_displayed_symtab (),
9597 get_last_displayed_line (),
9598 NULL);
c906108c 9599 else
58438ac1 9600 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, NULL);
c5aa993b 9601
c906108c 9602 if (sals.nelts != 1)
8a3fe4f8 9603 error (_("Couldn't get information on specified line."));
c5aa993b 9604
c906108c 9605 sal = sals.sals[0];
4a64f543 9606 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 9607
c906108c 9608 if (*arg)
8a3fe4f8 9609 error (_("Junk at end of arguments."));
c5aa993b 9610
c906108c 9611 resolve_sal_pc (&sal);
c5aa993b 9612
ae66c1fc
EZ
9613 if (anywhere)
9614 /* If the user told us to continue until a specified location,
9615 we don't specify a frame at which we need to stop. */
a6d9a66e
UW
9616 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9617 null_frame_id, bp_until);
ae66c1fc 9618 else
4a64f543
MS
9619 /* Otherwise, specify the selected frame, because we want to stop
9620 only at the very same frame. */
a6d9a66e
UW
9621 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
9622 get_stack_frame_id (frame),
ae66c1fc 9623 bp_until);
c5aa993b 9624
f107f563 9625 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 9626
186c406b
TT
9627 tp = inferior_thread ();
9628 thread = tp->num;
9629
ae66c1fc
EZ
9630 /* Keep within the current frame, or in frames called by the current
9631 one. */
edb3359d
DJ
9632
9633 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 9634 {
edb3359d
DJ
9635 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
9636 sal.pc = frame_unwind_caller_pc (frame);
a6d9a66e
UW
9637 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
9638 sal,
edb3359d 9639 frame_unwind_caller_id (frame),
f107f563
VP
9640 bp_until);
9641 make_cleanup_delete_breakpoint (breakpoint2);
186c406b
TT
9642
9643 set_longjmp_breakpoint (tp, frame_unwind_caller_id (frame));
9644 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 9645 }
c5aa993b 9646
c906108c 9647 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 9648
4a64f543
MS
9649 /* If we are running asynchronously, and proceed call above has
9650 actually managed to start the target, arrange for breakpoints to
9651 be deleted when the target stops. Otherwise, we're already
9652 stopped and delete breakpoints via cleanup chain. */
f107f563 9653
8ea051c5 9654 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 9655 {
bfec99b2
PA
9656 struct until_break_command_continuation_args *args;
9657 args = xmalloc (sizeof (*args));
f107f563 9658
bfec99b2
PA
9659 args->breakpoint = breakpoint;
9660 args->breakpoint2 = breakpoint2;
186c406b 9661 args->thread_num = thread;
f107f563
VP
9662
9663 discard_cleanups (old_chain);
95e54da7
PA
9664 add_continuation (inferior_thread (),
9665 until_break_command_continuation, args,
604ead4a 9666 xfree);
f107f563
VP
9667 }
9668 else
c5aa993b 9669 do_cleanups (old_chain);
c906108c 9670}
ae66c1fc 9671
c906108c
SS
9672/* This function attempts to parse an optional "if <cond>" clause
9673 from the arg string. If one is not found, it returns NULL.
c5aa993b 9674
c906108c
SS
9675 Else, it returns a pointer to the condition string. (It does not
9676 attempt to evaluate the string against a particular block.) And,
9677 it updates arg to point to the first character following the parsed
4a64f543 9678 if clause in the arg string. */
53a5351d 9679
c906108c 9680static char *
fba45db2 9681ep_parse_optional_if_clause (char **arg)
c906108c 9682{
c5aa993b
JM
9683 char *cond_string;
9684
9685 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 9686 return NULL;
c5aa993b 9687
4a64f543 9688 /* Skip the "if" keyword. */
c906108c 9689 (*arg) += 2;
c5aa993b 9690
c906108c 9691 /* Skip any extra leading whitespace, and record the start of the
4a64f543 9692 condition string. */
e9cafbcc 9693 *arg = skip_spaces (*arg);
c906108c 9694 cond_string = *arg;
c5aa993b 9695
4a64f543
MS
9696 /* Assume that the condition occupies the remainder of the arg
9697 string. */
c906108c 9698 (*arg) += strlen (cond_string);
c5aa993b 9699
c906108c
SS
9700 return cond_string;
9701}
c5aa993b 9702
c906108c
SS
9703/* Commands to deal with catching events, such as signals, exceptions,
9704 process start/exit, etc. */
c5aa993b
JM
9705
9706typedef enum
9707{
44feb3ce
TT
9708 catch_fork_temporary, catch_vfork_temporary,
9709 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
9710}
9711catch_fork_kind;
9712
c906108c 9713static void
cc59ec59
MS
9714catch_fork_command_1 (char *arg, int from_tty,
9715 struct cmd_list_element *command)
c906108c 9716{
a6d9a66e 9717 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 9718 char *cond_string = NULL;
44feb3ce
TT
9719 catch_fork_kind fork_kind;
9720 int tempflag;
9721
9722 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
9723 tempflag = (fork_kind == catch_fork_temporary
9724 || fork_kind == catch_vfork_temporary);
c5aa993b 9725
44feb3ce
TT
9726 if (!arg)
9727 arg = "";
e9cafbcc 9728 arg = skip_spaces (arg);
c5aa993b 9729
c906108c 9730 /* The allowed syntax is:
c5aa993b
JM
9731 catch [v]fork
9732 catch [v]fork if <cond>
9733
4a64f543 9734 First, check if there's an if clause. */
c906108c 9735 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 9736
c906108c 9737 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9738 error (_("Junk at end of arguments."));
c5aa993b 9739
c906108c 9740 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 9741 and enable reporting of such events. */
c5aa993b
JM
9742 switch (fork_kind)
9743 {
44feb3ce
TT
9744 case catch_fork_temporary:
9745 case catch_fork_permanent:
a6d9a66e 9746 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9747 &catch_fork_breakpoint_ops);
c906108c 9748 break;
44feb3ce
TT
9749 case catch_vfork_temporary:
9750 case catch_vfork_permanent:
a6d9a66e 9751 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 9752 &catch_vfork_breakpoint_ops);
c906108c 9753 break;
c5aa993b 9754 default:
8a3fe4f8 9755 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 9756 break;
c5aa993b 9757 }
c906108c
SS
9758}
9759
9760static void
cc59ec59
MS
9761catch_exec_command_1 (char *arg, int from_tty,
9762 struct cmd_list_element *command)
c906108c 9763{
b4d90040 9764 struct exec_catchpoint *c;
a6d9a66e 9765 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 9766 int tempflag;
c5aa993b 9767 char *cond_string = NULL;
c906108c 9768
44feb3ce
TT
9769 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
9770
9771 if (!arg)
9772 arg = "";
e9cafbcc 9773 arg = skip_spaces (arg);
c906108c
SS
9774
9775 /* The allowed syntax is:
c5aa993b
JM
9776 catch exec
9777 catch exec if <cond>
c906108c 9778
4a64f543 9779 First, check if there's an if clause. */
c906108c
SS
9780 cond_string = ep_parse_optional_if_clause (&arg);
9781
9782 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9783 error (_("Junk at end of arguments."));
c906108c 9784
b4d90040
PA
9785 c = XNEW (struct exec_catchpoint);
9786 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
9787 &catch_exec_breakpoint_ops);
9788 c->exec_pathname = NULL;
9789
3a5c3e22 9790 install_breakpoint (0, &c->base);
c906108c 9791}
c5aa993b 9792
3086aeae 9793static enum print_stop_action
348d480f 9794print_it_exception_catchpoint (bpstat bs)
3086aeae 9795{
79a45e25 9796 struct ui_out *uiout = current_uiout;
348d480f 9797 struct breakpoint *b = bs->breakpoint_at;
ade92717 9798 int bp_temp, bp_throw;
3086aeae 9799
ade92717 9800 annotate_catchpoint (b->number);
3086aeae 9801
ade92717
AR
9802 bp_throw = strstr (b->addr_string, "throw") != NULL;
9803 if (b->loc->address != b->loc->requested_address)
9804 breakpoint_adjustment_warning (b->loc->requested_address,
9805 b->loc->address,
9806 b->number, 1);
df2b6d2d 9807 bp_temp = b->disposition == disp_del;
ade92717
AR
9808 ui_out_text (uiout,
9809 bp_temp ? "Temporary catchpoint "
9810 : "Catchpoint ");
9811 if (!ui_out_is_mi_like_p (uiout))
9812 ui_out_field_int (uiout, "bkptno", b->number);
9813 ui_out_text (uiout,
c0b37c48
AR
9814 bp_throw ? " (exception thrown), "
9815 : " (exception caught), ");
ade92717
AR
9816 if (ui_out_is_mi_like_p (uiout))
9817 {
9818 ui_out_field_string (uiout, "reason",
9819 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9820 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9821 ui_out_field_int (uiout, "bkptno", b->number);
9822 }
3086aeae
DJ
9823 return PRINT_SRC_AND_LOC;
9824}
9825
9826static void
cc59ec59
MS
9827print_one_exception_catchpoint (struct breakpoint *b,
9828 struct bp_location **last_loc)
3086aeae 9829{
79a45b7d 9830 struct value_print_options opts;
79a45e25 9831 struct ui_out *uiout = current_uiout;
cc59ec59 9832
79a45b7d
TT
9833 get_user_print_options (&opts);
9834 if (opts.addressprint)
3086aeae
DJ
9835 {
9836 annotate_field (4);
604133b5
AR
9837 if (b->loc == NULL || b->loc->shlib_disabled)
9838 ui_out_field_string (uiout, "addr", "<PENDING>");
9839 else
5af949e3
UW
9840 ui_out_field_core_addr (uiout, "addr",
9841 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
9842 }
9843 annotate_field (5);
604133b5 9844 if (b->loc)
a6d9a66e 9845 *last_loc = b->loc;
3086aeae
DJ
9846 if (strstr (b->addr_string, "throw") != NULL)
9847 ui_out_field_string (uiout, "what", "exception throw");
9848 else
9849 ui_out_field_string (uiout, "what", "exception catch");
9850}
9851
9852static void
9853print_mention_exception_catchpoint (struct breakpoint *b)
9854{
79a45e25 9855 struct ui_out *uiout = current_uiout;
ade92717
AR
9856 int bp_temp;
9857 int bp_throw;
9858
df2b6d2d 9859 bp_temp = b->disposition == disp_del;
ade92717
AR
9860 bp_throw = strstr (b->addr_string, "throw") != NULL;
9861 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
9862 : _("Catchpoint "));
9863 ui_out_field_int (uiout, "bkptno", b->number);
9864 ui_out_text (uiout, bp_throw ? _(" (throw)")
9865 : _(" (catch)"));
3086aeae
DJ
9866}
9867
6149aea9
PA
9868/* Implement the "print_recreate" breakpoint_ops method for throw and
9869 catch catchpoints. */
9870
9871static void
4a64f543
MS
9872print_recreate_exception_catchpoint (struct breakpoint *b,
9873 struct ui_file *fp)
6149aea9
PA
9874{
9875 int bp_temp;
9876 int bp_throw;
9877
9878 bp_temp = b->disposition == disp_del;
9879 bp_throw = strstr (b->addr_string, "throw") != NULL;
9880 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
9881 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 9882 print_recreate_thread (b, fp);
6149aea9
PA
9883}
9884
2060206e 9885static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
9886
9887static int
9888handle_gnu_v3_exceptions (int tempflag, char *cond_string,
9889 enum exception_event_kind ex_event, int from_tty)
9890{
604133b5
AR
9891 char *trigger_func_name;
9892
3086aeae 9893 if (ex_event == EX_EVENT_CATCH)
604133b5 9894 trigger_func_name = "__cxa_begin_catch";
3086aeae 9895 else
604133b5 9896 trigger_func_name = "__cxa_throw";
3086aeae 9897
8cdf0e15
VP
9898 create_breakpoint (get_current_arch (),
9899 trigger_func_name, cond_string, -1,
9900 0 /* condition and thread are valid. */,
0fb4aa4b 9901 tempflag, bp_breakpoint,
8cdf0e15
VP
9902 0,
9903 AUTO_BOOLEAN_TRUE /* pending */,
9904 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe
PM
9905 1 /* enabled */,
9906 0 /* internal */);
3086aeae 9907
3086aeae
DJ
9908 return 1;
9909}
9910
4a64f543 9911/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
9912
9913static void
fba45db2
KB
9914catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
9915 int tempflag, int from_tty)
c906108c 9916{
c5aa993b 9917 char *cond_string = NULL;
c5aa993b 9918
44feb3ce
TT
9919 if (!arg)
9920 arg = "";
e9cafbcc 9921 arg = skip_spaces (arg);
c5aa993b 9922
c906108c
SS
9923 cond_string = ep_parse_optional_if_clause (&arg);
9924
9925 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 9926 error (_("Junk at end of arguments."));
c906108c 9927
059fb39f
PM
9928 if (ex_event != EX_EVENT_THROW
9929 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 9930 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 9931
3086aeae
DJ
9932 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
9933 return;
9934
8a3fe4f8 9935 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
9936}
9937
44feb3ce
TT
9938/* Implementation of "catch catch" command. */
9939
9940static void
9941catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
9942{
9943 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9944
44feb3ce
TT
9945 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
9946}
9947
9948/* Implementation of "catch throw" command. */
9949
9950static void
9951catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
9952{
9953 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 9954
44feb3ce
TT
9955 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
9956}
9957
9ac4176b 9958void
28010a5d
PA
9959init_ada_exception_breakpoint (struct breakpoint *b,
9960 struct gdbarch *gdbarch,
9961 struct symtab_and_line sal,
9962 char *addr_string,
c0a91b2b 9963 const struct breakpoint_ops *ops,
28010a5d
PA
9964 int tempflag,
9965 int from_tty)
f7f9143b 9966{
f7f9143b
JB
9967 if (from_tty)
9968 {
5af949e3
UW
9969 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9970 if (!loc_gdbarch)
9971 loc_gdbarch = gdbarch;
9972
6c95b8df
PA
9973 describe_other_breakpoints (loc_gdbarch,
9974 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
9975 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
9976 version for exception catchpoints, because two catchpoints
9977 used for different exception names will use the same address.
9978 In this case, a "breakpoint ... also set at..." warning is
4a64f543 9979 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 9980 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
9981 the user what type of catchpoint it is. The above is good
9982 enough for now, though. */
9983 }
9984
28010a5d 9985 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
9986
9987 b->enable_state = bp_enabled;
9988 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
9989 b->addr_string = addr_string;
9990 b->language = language_ada;
f7f9143b
JB
9991}
9992
a96d9b2e
SDJ
9993/* Splits the argument using space as delimiter. Returns an xmalloc'd
9994 filter list, or NULL if no filtering is required. */
9995static VEC(int) *
9996catch_syscall_split_args (char *arg)
9997{
9998 VEC(int) *result = NULL;
29d0bb3d 9999 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
10000
10001 while (*arg != '\0')
10002 {
10003 int i, syscall_number;
10004 char *endptr;
10005 char cur_name[128];
10006 struct syscall s;
10007
10008 /* Skip whitespace. */
10009 while (isspace (*arg))
10010 arg++;
10011
10012 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
10013 cur_name[i] = arg[i];
10014 cur_name[i] = '\0';
10015 arg += i;
10016
10017 /* Check if the user provided a syscall name or a number. */
10018 syscall_number = (int) strtol (cur_name, &endptr, 0);
10019 if (*endptr == '\0')
bccd0dd2 10020 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
10021 else
10022 {
10023 /* We have a name. Let's check if it's valid and convert it
10024 to a number. */
10025 get_syscall_by_name (cur_name, &s);
10026
10027 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
10028 /* Here we have to issue an error instead of a warning,
10029 because GDB cannot do anything useful if there's no
10030 syscall number to be caught. */
a96d9b2e
SDJ
10031 error (_("Unknown syscall name '%s'."), cur_name);
10032 }
10033
10034 /* Ok, it's valid. */
10035 VEC_safe_push (int, result, s.number);
10036 }
10037
10038 discard_cleanups (cleanup);
10039 return result;
10040}
10041
10042/* Implement the "catch syscall" command. */
10043
10044static void
cc59ec59
MS
10045catch_syscall_command_1 (char *arg, int from_tty,
10046 struct cmd_list_element *command)
a96d9b2e
SDJ
10047{
10048 int tempflag;
10049 VEC(int) *filter;
10050 struct syscall s;
10051 struct gdbarch *gdbarch = get_current_arch ();
10052
10053 /* Checking if the feature if supported. */
10054 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10055 error (_("The feature 'catch syscall' is not supported on \
ea666128 10056this architecture yet."));
a96d9b2e
SDJ
10057
10058 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10059
e9cafbcc 10060 arg = skip_spaces (arg);
a96d9b2e
SDJ
10061
10062 /* We need to do this first "dummy" translation in order
10063 to get the syscall XML file loaded or, most important,
10064 to display a warning to the user if there's no XML file
10065 for his/her architecture. */
10066 get_syscall_by_number (0, &s);
10067
10068 /* The allowed syntax is:
10069 catch syscall
10070 catch syscall <name | number> [<name | number> ... <name | number>]
10071
10072 Let's check if there's a syscall name. */
10073
10074 if (arg != NULL)
10075 filter = catch_syscall_split_args (arg);
10076 else
10077 filter = NULL;
10078
10079 create_syscall_event_catchpoint (tempflag, filter,
10080 &catch_syscall_breakpoint_ops);
10081}
10082
c906108c 10083static void
fba45db2 10084catch_command (char *arg, int from_tty)
c906108c 10085{
44feb3ce 10086 error (_("Catch requires an event name."));
c906108c
SS
10087}
10088\f
10089
10090static void
fba45db2 10091tcatch_command (char *arg, int from_tty)
c906108c 10092{
44feb3ce 10093 error (_("Catch requires an event name."));
c906108c
SS
10094}
10095
80f8a6eb 10096/* Delete breakpoints by address or line. */
c906108c
SS
10097
10098static void
fba45db2 10099clear_command (char *arg, int from_tty)
c906108c 10100{
d6e956e5
VP
10101 struct breakpoint *b;
10102 VEC(breakpoint_p) *found = 0;
10103 int ix;
c906108c
SS
10104 int default_match;
10105 struct symtabs_and_lines sals;
10106 struct symtab_and_line sal;
c906108c
SS
10107 int i;
10108
10109 if (arg)
10110 {
10111 sals = decode_line_spec (arg, 1);
10112 default_match = 0;
10113 }
10114 else
10115 {
c5aa993b 10116 sals.sals = (struct symtab_and_line *)
c906108c 10117 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 10118 make_cleanup (xfree, sals.sals);
4a64f543 10119 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
10120
10121 /* Set sal's line, symtab, pc, and pspace to the values
10122 corresponding to the last call to print_frame_info. If the
10123 codepoint is not valid, this will set all the fields to 0. */
10124 get_last_displayed_sal (&sal);
c906108c 10125 if (sal.symtab == 0)
8a3fe4f8 10126 error (_("No source file specified."));
c906108c
SS
10127
10128 sals.sals[0] = sal;
10129 sals.nelts = 1;
10130
10131 default_match = 1;
10132 }
10133
4a64f543
MS
10134 /* We don't call resolve_sal_pc here. That's not as bad as it
10135 seems, because all existing breakpoints typically have both
10136 file/line and pc set. So, if clear is given file/line, we can
10137 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
10138
10139 We only support clearing given the address explicitly
10140 present in breakpoint table. Say, we've set breakpoint
4a64f543 10141 at file:line. There were several PC values for that file:line,
ed0616c6 10142 due to optimization, all in one block.
4a64f543
MS
10143
10144 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
10145 PC corresponding to the same file:line, the breakpoint won't
10146 be cleared. We probably can still clear the breakpoint, but
10147 since the other PC value is never presented to user, user
10148 can only find it by guessing, and it does not seem important
10149 to support that. */
10150
4a64f543
MS
10151 /* For each line spec given, delete bps which correspond to it. Do
10152 it in two passes, solely to preserve the current behavior that
10153 from_tty is forced true if we delete more than one
10154 breakpoint. */
c906108c 10155
80f8a6eb 10156 found = NULL;
c906108c
SS
10157 for (i = 0; i < sals.nelts; i++)
10158 {
10159 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
10160 If line given (pc == 0), clear all bpts on specified line.
10161 If defaulting, clear all bpts on default line
c906108c 10162 or at default pc.
c5aa993b
JM
10163
10164 defaulting sal.pc != 0 tests to do
10165
10166 0 1 pc
10167 1 1 pc _and_ line
10168 0 0 line
10169 1 0 <can't happen> */
c906108c
SS
10170
10171 sal = sals.sals[i];
c906108c 10172
4a64f543 10173 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 10174 ALL_BREAKPOINTS (b)
c5aa993b 10175 {
0d381245 10176 int match = 0;
4a64f543 10177 /* Are we going to delete b? */
cc60f2e3 10178 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
10179 {
10180 struct bp_location *loc = b->loc;
10181 for (; loc; loc = loc->next)
10182 {
6c95b8df
PA
10183 int pc_match = sal.pc
10184 && (loc->pspace == sal.pspace)
0d381245
VP
10185 && (loc->address == sal.pc)
10186 && (!section_is_overlay (loc->section)
10187 || loc->section == sal.section);
10188 int line_match = ((default_match || (0 == sal.pc))
10189 && b->source_file != NULL
10190 && sal.symtab != NULL
6c95b8df 10191 && sal.pspace == loc->pspace
0ba1096a
KT
10192 && filename_cmp (b->source_file,
10193 sal.symtab->filename) == 0
0d381245
VP
10194 && b->line_number == sal.line);
10195 if (pc_match || line_match)
10196 {
10197 match = 1;
10198 break;
10199 }
10200 }
10201 }
10202
10203 if (match)
d6e956e5 10204 VEC_safe_push(breakpoint_p, found, b);
c906108c 10205 }
80f8a6eb
MS
10206 }
10207 /* Now go thru the 'found' chain and delete them. */
d6e956e5 10208 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
10209 {
10210 if (arg)
8a3fe4f8 10211 error (_("No breakpoint at %s."), arg);
80f8a6eb 10212 else
8a3fe4f8 10213 error (_("No breakpoint at this line."));
80f8a6eb 10214 }
c906108c 10215
d6e956e5 10216 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 10217 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 10218 if (from_tty)
a3f17187 10219 {
d6e956e5 10220 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
10221 printf_unfiltered (_("Deleted breakpoint "));
10222 else
10223 printf_unfiltered (_("Deleted breakpoints "));
10224 }
80f8a6eb 10225 breakpoints_changed ();
d6e956e5
VP
10226
10227 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 10228 {
c5aa993b 10229 if (from_tty)
d6e956e5
VP
10230 printf_unfiltered ("%d ", b->number);
10231 delete_breakpoint (b);
c906108c 10232 }
80f8a6eb
MS
10233 if (from_tty)
10234 putchar_unfiltered ('\n');
c906108c
SS
10235}
10236\f
10237/* Delete breakpoint in BS if they are `delete' breakpoints and
10238 all breakpoints that are marked for deletion, whether hit or not.
10239 This is called after any breakpoint is hit, or after errors. */
10240
10241void
fba45db2 10242breakpoint_auto_delete (bpstat bs)
c906108c 10243{
35df4500 10244 struct breakpoint *b, *b_tmp;
c906108c
SS
10245
10246 for (; bs; bs = bs->next)
f431efe5
PA
10247 if (bs->breakpoint_at
10248 && bs->breakpoint_at->disposition == disp_del
c906108c 10249 && bs->stop)
f431efe5 10250 delete_breakpoint (bs->breakpoint_at);
c906108c 10251
35df4500 10252 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 10253 {
b5de0fa7 10254 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
10255 delete_breakpoint (b);
10256 }
c906108c
SS
10257}
10258
4a64f543
MS
10259/* A comparison function for bp_location AP and BP being interfaced to
10260 qsort. Sort elements primarily by their ADDRESS (no matter what
10261 does breakpoint_address_is_meaningful say for its OWNER),
10262 secondarily by ordering first bp_permanent OWNERed elements and
10263 terciarily just ensuring the array is sorted stable way despite
e5dd4106 10264 qsort being an unstable algorithm. */
876fa593
JK
10265
10266static int
494cfb0f 10267bp_location_compare (const void *ap, const void *bp)
876fa593 10268{
494cfb0f
JK
10269 struct bp_location *a = *(void **) ap;
10270 struct bp_location *b = *(void **) bp;
2bdf28a0 10271 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
10272 int a_perm = a->owner->enable_state == bp_permanent;
10273 int b_perm = b->owner->enable_state == bp_permanent;
10274
10275 if (a->address != b->address)
10276 return (a->address > b->address) - (a->address < b->address);
10277
10278 /* Sort permanent breakpoints first. */
10279 if (a_perm != b_perm)
10280 return (a_perm < b_perm) - (a_perm > b_perm);
10281
4a64f543
MS
10282 /* Make the user-visible order stable across GDB runs. Locations of
10283 the same breakpoint can be sorted in arbitrary order. */
876fa593
JK
10284
10285 if (a->owner->number != b->owner->number)
10286 return (a->owner->number > b->owner->number)
10287 - (a->owner->number < b->owner->number);
10288
10289 return (a > b) - (a < b);
10290}
10291
876fa593 10292/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
10293 bp_location_shadow_len_after_address_max according to the current
10294 content of the bp_location array. */
f7545552
TT
10295
10296static void
876fa593 10297bp_location_target_extensions_update (void)
f7545552 10298{
876fa593
JK
10299 struct bp_location *bl, **blp_tmp;
10300
10301 bp_location_placed_address_before_address_max = 0;
10302 bp_location_shadow_len_after_address_max = 0;
10303
10304 ALL_BP_LOCATIONS (bl, blp_tmp)
10305 {
10306 CORE_ADDR start, end, addr;
10307
10308 if (!bp_location_has_shadow (bl))
10309 continue;
10310
10311 start = bl->target_info.placed_address;
10312 end = start + bl->target_info.shadow_len;
10313
10314 gdb_assert (bl->address >= start);
10315 addr = bl->address - start;
10316 if (addr > bp_location_placed_address_before_address_max)
10317 bp_location_placed_address_before_address_max = addr;
10318
10319 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10320
10321 gdb_assert (bl->address < end);
10322 addr = end - bl->address;
10323 if (addr > bp_location_shadow_len_after_address_max)
10324 bp_location_shadow_len_after_address_max = addr;
10325 }
f7545552
TT
10326}
10327
934709f0
PW
10328/* Swap the insertion/duplication state between two locations. */
10329
10330static void
10331swap_insertion (struct bp_location *left, struct bp_location *right)
10332{
10333 const int left_inserted = left->inserted;
10334 const int left_duplicate = left->duplicate;
10335 const struct bp_target_info left_target_info = left->target_info;
10336
10337 left->inserted = right->inserted;
10338 left->duplicate = right->duplicate;
10339 left->target_info = right->target_info;
10340 right->inserted = left_inserted;
10341 right->duplicate = left_duplicate;
10342 right->target_info = left_target_info;
10343}
10344
4cd9bd08 10345/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
10346 into the inferior, only remove already-inserted locations that no
10347 longer should be inserted. Functions that delete a breakpoint or
10348 breakpoints should pass false, so that deleting a breakpoint
10349 doesn't have the side effect of inserting the locations of other
10350 breakpoints that are marked not-inserted, but should_be_inserted
10351 returns true on them.
10352
10353 This behaviour is useful is situations close to tear-down -- e.g.,
10354 after an exec, while the target still has execution, but breakpoint
10355 shadows of the previous executable image should *NOT* be restored
10356 to the new image; or before detaching, where the target still has
10357 execution and wants to delete breakpoints from GDB's lists, and all
10358 breakpoints had already been removed from the inferior. */
10359
0d381245 10360static void
b60e7edf 10361update_global_location_list (int should_insert)
0d381245 10362{
74960c60 10363 struct breakpoint *b;
876fa593 10364 struct bp_location **locp, *loc;
f7545552
TT
10365 struct cleanup *cleanups;
10366
2d134ed3
PA
10367 /* Used in the duplicates detection below. When iterating over all
10368 bp_locations, points to the first bp_location of a given address.
10369 Breakpoints and watchpoints of different types are never
10370 duplicates of each other. Keep one pointer for each type of
10371 breakpoint/watchpoint, so we only need to loop over all locations
10372 once. */
10373 struct bp_location *bp_loc_first; /* breakpoint */
10374 struct bp_location *wp_loc_first; /* hardware watchpoint */
10375 struct bp_location *awp_loc_first; /* access watchpoint */
10376 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 10377
4a64f543
MS
10378 /* Saved former bp_location array which we compare against the newly
10379 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
10380 struct bp_location **old_location, **old_locp;
10381 unsigned old_location_count;
10382
10383 old_location = bp_location;
10384 old_location_count = bp_location_count;
10385 bp_location = NULL;
10386 bp_location_count = 0;
10387 cleanups = make_cleanup (xfree, old_location);
0d381245 10388
74960c60 10389 ALL_BREAKPOINTS (b)
876fa593
JK
10390 for (loc = b->loc; loc; loc = loc->next)
10391 bp_location_count++;
10392
10393 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10394 locp = bp_location;
10395 ALL_BREAKPOINTS (b)
10396 for (loc = b->loc; loc; loc = loc->next)
10397 *locp++ = loc;
10398 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 10399 bp_location_compare);
876fa593
JK
10400
10401 bp_location_target_extensions_update ();
74960c60 10402
4a64f543
MS
10403 /* Identify bp_location instances that are no longer present in the
10404 new list, and therefore should be freed. Note that it's not
10405 necessary that those locations should be removed from inferior --
10406 if there's another location at the same address (previously
10407 marked as duplicate), we don't need to remove/insert the
10408 location.
876fa593 10409
4a64f543
MS
10410 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10411 and former bp_location array state respectively. */
876fa593
JK
10412
10413 locp = bp_location;
10414 for (old_locp = old_location; old_locp < old_location + old_location_count;
10415 old_locp++)
74960c60 10416 {
876fa593 10417 struct bp_location *old_loc = *old_locp;
c7d46a38 10418 struct bp_location **loc2p;
876fa593 10419
e5dd4106 10420 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 10421 not, we have to free it. */
c7d46a38 10422 int found_object = 0;
20874c92
VP
10423 /* Tells if the location should remain inserted in the target. */
10424 int keep_in_target = 0;
10425 int removed = 0;
876fa593 10426
4a64f543
MS
10427 /* Skip LOCP entries which will definitely never be needed.
10428 Stop either at or being the one matching OLD_LOC. */
876fa593 10429 while (locp < bp_location + bp_location_count
c7d46a38 10430 && (*locp)->address < old_loc->address)
876fa593 10431 locp++;
c7d46a38
PA
10432
10433 for (loc2p = locp;
10434 (loc2p < bp_location + bp_location_count
10435 && (*loc2p)->address == old_loc->address);
10436 loc2p++)
10437 {
10438 if (*loc2p == old_loc)
10439 {
10440 found_object = 1;
10441 break;
10442 }
10443 }
74960c60 10444
4a64f543
MS
10445 /* If this location is no longer present, and inserted, look if
10446 there's maybe a new location at the same address. If so,
10447 mark that one inserted, and don't remove this one. This is
10448 needed so that we don't have a time window where a breakpoint
10449 at certain location is not inserted. */
74960c60 10450
876fa593 10451 if (old_loc->inserted)
0d381245 10452 {
4a64f543
MS
10453 /* If the location is inserted now, we might have to remove
10454 it. */
74960c60 10455
876fa593 10456 if (found_object && should_be_inserted (old_loc))
74960c60 10457 {
4a64f543
MS
10458 /* The location is still present in the location list,
10459 and still should be inserted. Don't do anything. */
20874c92 10460 keep_in_target = 1;
74960c60
VP
10461 }
10462 else
10463 {
4a64f543
MS
10464 /* The location is either no longer present, or got
10465 disabled. See if there's another location at the
10466 same address, in which case we don't need to remove
10467 this one from the target. */
876fa593 10468
2bdf28a0 10469 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
10470 if (breakpoint_address_is_meaningful (old_loc->owner))
10471 {
876fa593 10472 for (loc2p = locp;
c7d46a38
PA
10473 (loc2p < bp_location + bp_location_count
10474 && (*loc2p)->address == old_loc->address);
876fa593
JK
10475 loc2p++)
10476 {
10477 struct bp_location *loc2 = *loc2p;
10478
2d134ed3 10479 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 10480 {
85d721b8
PA
10481 /* Read watchpoint locations are switched to
10482 access watchpoints, if the former are not
10483 supported, but the latter are. */
10484 if (is_hardware_watchpoint (old_loc->owner))
10485 {
10486 gdb_assert (is_hardware_watchpoint (loc2->owner));
10487 loc2->watchpoint_type = old_loc->watchpoint_type;
10488 }
10489
934709f0
PW
10490 /* loc2 is a duplicated location. We need to check
10491 if it should be inserted in case it will be
10492 unduplicated. */
10493 if (loc2 != old_loc
10494 && unduplicated_should_be_inserted (loc2))
c7d46a38 10495 {
934709f0 10496 swap_insertion (old_loc, loc2);
c7d46a38
PA
10497 keep_in_target = 1;
10498 break;
10499 }
876fa593
JK
10500 }
10501 }
10502 }
74960c60
VP
10503 }
10504
20874c92
VP
10505 if (!keep_in_target)
10506 {
876fa593 10507 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 10508 {
4a64f543
MS
10509 /* This is just about all we can do. We could keep
10510 this location on the global list, and try to
10511 remove it next time, but there's no particular
10512 reason why we will succeed next time.
20874c92 10513
4a64f543
MS
10514 Note that at this point, old_loc->owner is still
10515 valid, as delete_breakpoint frees the breakpoint
10516 only after calling us. */
3e43a32a
MS
10517 printf_filtered (_("warning: Error removing "
10518 "breakpoint %d\n"),
876fa593 10519 old_loc->owner->number);
20874c92
VP
10520 }
10521 removed = 1;
10522 }
0d381245 10523 }
74960c60
VP
10524
10525 if (!found_object)
1c5cfe86 10526 {
db82e815
PA
10527 if (removed && non_stop
10528 && breakpoint_address_is_meaningful (old_loc->owner)
10529 && !is_hardware_watchpoint (old_loc->owner))
20874c92 10530 {
db82e815
PA
10531 /* This location was removed from the target. In
10532 non-stop mode, a race condition is possible where
10533 we've removed a breakpoint, but stop events for that
10534 breakpoint are already queued and will arrive later.
10535 We apply an heuristic to be able to distinguish such
10536 SIGTRAPs from other random SIGTRAPs: we keep this
10537 breakpoint location for a bit, and will retire it
10538 after we see some number of events. The theory here
10539 is that reporting of events should, "on the average",
10540 be fair, so after a while we'll see events from all
10541 threads that have anything of interest, and no longer
10542 need to keep this breakpoint location around. We
10543 don't hold locations forever so to reduce chances of
10544 mistaking a non-breakpoint SIGTRAP for a breakpoint
10545 SIGTRAP.
10546
10547 The heuristic failing can be disastrous on
10548 decr_pc_after_break targets.
10549
10550 On decr_pc_after_break targets, like e.g., x86-linux,
10551 if we fail to recognize a late breakpoint SIGTRAP,
10552 because events_till_retirement has reached 0 too
10553 soon, we'll fail to do the PC adjustment, and report
10554 a random SIGTRAP to the user. When the user resumes
10555 the inferior, it will most likely immediately crash
2dec564e 10556 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
10557 corrupted, because of being resumed e.g., in the
10558 middle of a multi-byte instruction, or skipped a
10559 one-byte instruction. This was actually seen happen
10560 on native x86-linux, and should be less rare on
10561 targets that do not support new thread events, like
10562 remote, due to the heuristic depending on
10563 thread_count.
10564
10565 Mistaking a random SIGTRAP for a breakpoint trap
10566 causes similar symptoms (PC adjustment applied when
10567 it shouldn't), but then again, playing with SIGTRAPs
10568 behind the debugger's back is asking for trouble.
10569
10570 Since hardware watchpoint traps are always
10571 distinguishable from other traps, so we don't need to
10572 apply keep hardware watchpoint moribund locations
10573 around. We simply always ignore hardware watchpoint
10574 traps we can no longer explain. */
10575
876fa593
JK
10576 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10577 old_loc->owner = NULL;
20874c92 10578
876fa593 10579 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
10580 }
10581 else
f431efe5
PA
10582 {
10583 old_loc->owner = NULL;
10584 decref_bp_location (&old_loc);
10585 }
20874c92 10586 }
74960c60 10587 }
1c5cfe86 10588
348d480f
PA
10589 /* Rescan breakpoints at the same address and section, marking the
10590 first one as "first" and any others as "duplicates". This is so
10591 that the bpt instruction is only inserted once. If we have a
10592 permanent breakpoint at the same place as BPT, make that one the
10593 official one, and the rest as duplicates. Permanent breakpoints
10594 are sorted first for the same address.
10595
10596 Do the same for hardware watchpoints, but also considering the
10597 watchpoint's type (regular/access/read) and length. */
10598
10599 bp_loc_first = NULL;
10600 wp_loc_first = NULL;
10601 awp_loc_first = NULL;
10602 rwp_loc_first = NULL;
10603 ALL_BP_LOCATIONS (loc, locp)
10604 {
10605 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10606 non-NULL. */
10607 struct breakpoint *b = loc->owner;
10608 struct bp_location **loc_first_p;
10609
10610 if (b->enable_state == bp_disabled
10611 || b->enable_state == bp_call_disabled
10612 || b->enable_state == bp_startup_disabled
10613 || !loc->enabled
10614 || loc->shlib_disabled
10615 || !breakpoint_address_is_meaningful (b)
10616 || is_tracepoint (b))
10617 continue;
10618
10619 /* Permanent breakpoint should always be inserted. */
10620 if (b->enable_state == bp_permanent && ! loc->inserted)
10621 internal_error (__FILE__, __LINE__,
10622 _("allegedly permanent breakpoint is not "
10623 "actually inserted"));
10624
10625 if (b->type == bp_hardware_watchpoint)
10626 loc_first_p = &wp_loc_first;
10627 else if (b->type == bp_read_watchpoint)
10628 loc_first_p = &rwp_loc_first;
10629 else if (b->type == bp_access_watchpoint)
10630 loc_first_p = &awp_loc_first;
10631 else
10632 loc_first_p = &bp_loc_first;
10633
10634 if (*loc_first_p == NULL
10635 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10636 || !breakpoint_locations_match (loc, *loc_first_p))
10637 {
10638 *loc_first_p = loc;
10639 loc->duplicate = 0;
10640 continue;
10641 }
10642
934709f0
PW
10643
10644 /* This and the above ensure the invariant that the first location
10645 is not duplicated, and is the inserted one.
10646 All following are marked as duplicated, and are not inserted. */
10647 if (loc->inserted)
10648 swap_insertion (loc, *loc_first_p);
348d480f
PA
10649 loc->duplicate = 1;
10650
10651 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10652 && b->enable_state != bp_permanent)
10653 internal_error (__FILE__, __LINE__,
10654 _("another breakpoint was inserted on top of "
10655 "a permanent breakpoint"));
10656 }
10657
10658 if (breakpoints_always_inserted_mode () && should_insert
10659 && (have_live_inferiors ()
10660 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10661 insert_breakpoint_locations ();
10662
10663 do_cleanups (cleanups);
10664}
10665
10666void
10667breakpoint_retire_moribund (void)
10668{
10669 struct bp_location *loc;
10670 int ix;
10671
10672 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10673 if (--(loc->events_till_retirement) == 0)
10674 {
10675 decref_bp_location (&loc);
10676 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10677 --ix;
10678 }
10679}
10680
10681static void
10682update_global_location_list_nothrow (int inserting)
10683{
10684 struct gdb_exception e;
10685
10686 TRY_CATCH (e, RETURN_MASK_ERROR)
10687 update_global_location_list (inserting);
10688}
10689
10690/* Clear BKP from a BPS. */
10691
10692static void
10693bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10694{
10695 bpstat bs;
10696
10697 for (bs = bps; bs; bs = bs->next)
10698 if (bs->breakpoint_at == bpt)
10699 {
10700 bs->breakpoint_at = NULL;
10701 bs->old_val = NULL;
10702 /* bs->commands will be freed later. */
10703 }
10704}
10705
10706/* Callback for iterate_over_threads. */
10707static int
10708bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10709{
10710 struct breakpoint *bpt = data;
10711
10712 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10713 return 0;
10714}
10715
10716/* Helper for breakpoint and tracepoint breakpoint_ops->mention
10717 callbacks. */
10718
10719static void
10720say_where (struct breakpoint *b)
10721{
79a45e25 10722 struct ui_out *uiout = current_uiout;
348d480f
PA
10723 struct value_print_options opts;
10724
10725 get_user_print_options (&opts);
10726
10727 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10728 single string. */
10729 if (b->loc == NULL)
10730 {
10731 printf_filtered (_(" (%s) pending."), b->addr_string);
10732 }
10733 else
10734 {
10735 if (opts.addressprint || b->source_file == NULL)
10736 {
10737 printf_filtered (" at ");
10738 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10739 gdb_stdout);
10740 }
10741 if (b->source_file)
10742 printf_filtered (": file %s, line %d.",
10743 b->source_file, b->line_number);
10744
10745 if (b->loc->next)
10746 {
10747 struct bp_location *loc = b->loc;
10748 int n = 0;
10749 for (; loc; loc = loc->next)
10750 ++n;
10751 printf_filtered (" (%d locations)", n);
10752 }
10753 }
10754}
10755
348d480f
PA
10756/* Default bp_location_ops methods. */
10757
10758static void
10759bp_location_dtor (struct bp_location *self)
10760{
10761 xfree (self->cond);
10762 xfree (self->function_name);
10763}
10764
10765static const struct bp_location_ops bp_location_ops =
10766{
10767 bp_location_dtor
10768};
10769
2060206e
PA
10770/* Default breakpoint_ops methods all breakpoint_ops ultimately
10771 inherit from. */
348d480f 10772
2060206e
PA
10773static void
10774base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
10775{
10776 decref_counted_command_line (&self->commands);
10777 xfree (self->cond_string);
348d480f
PA
10778 xfree (self->addr_string);
10779 xfree (self->addr_string_range_end);
348d480f
PA
10780 xfree (self->source_file);
10781}
10782
2060206e
PA
10783static struct bp_location *
10784base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
10785{
10786 struct bp_location *loc;
10787
10788 loc = XNEW (struct bp_location);
10789 init_bp_location (loc, &bp_location_ops, self);
10790 return loc;
10791}
10792
2060206e
PA
10793static void
10794base_breakpoint_re_set (struct breakpoint *b)
10795{
10796 /* Nothing to re-set. */
10797}
10798
10799#define internal_error_pure_virtual_called() \
10800 gdb_assert_not_reached ("pure virtual function called")
10801
10802static int
10803base_breakpoint_insert_location (struct bp_location *bl)
10804{
10805 internal_error_pure_virtual_called ();
10806}
10807
10808static int
10809base_breakpoint_remove_location (struct bp_location *bl)
10810{
10811 internal_error_pure_virtual_called ();
10812}
10813
10814static int
10815base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10816 struct address_space *aspace,
10817 CORE_ADDR bp_addr)
10818{
10819 internal_error_pure_virtual_called ();
10820}
10821
10822static void
10823base_breakpoint_check_status (bpstat bs)
10824{
10825 /* Always stop. */
10826}
10827
10828/* A "works_in_software_mode" breakpoint_ops method that just internal
10829 errors. */
10830
10831static int
10832base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10833{
10834 internal_error_pure_virtual_called ();
10835}
10836
10837/* A "resources_needed" breakpoint_ops method that just internal
10838 errors. */
10839
10840static int
10841base_breakpoint_resources_needed (const struct bp_location *bl)
10842{
10843 internal_error_pure_virtual_called ();
10844}
10845
10846static enum print_stop_action
10847base_breakpoint_print_it (bpstat bs)
10848{
10849 internal_error_pure_virtual_called ();
10850}
10851
10852static void
10853base_breakpoint_print_one_detail (const struct breakpoint *self,
10854 struct ui_out *uiout)
10855{
10856 /* nothing */
10857}
10858
10859static void
10860base_breakpoint_print_mention (struct breakpoint *b)
10861{
10862 internal_error_pure_virtual_called ();
10863}
10864
10865static void
10866base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10867{
10868 internal_error_pure_virtual_called ();
10869}
10870
10871static struct breakpoint_ops base_breakpoint_ops =
10872{
10873 base_breakpoint_dtor,
10874 base_breakpoint_allocate_location,
10875 base_breakpoint_re_set,
10876 base_breakpoint_insert_location,
10877 base_breakpoint_remove_location,
10878 base_breakpoint_breakpoint_hit,
10879 base_breakpoint_check_status,
10880 base_breakpoint_resources_needed,
10881 base_breakpoint_works_in_software_mode,
10882 base_breakpoint_print_it,
10883 NULL,
10884 base_breakpoint_print_one_detail,
10885 base_breakpoint_print_mention,
10886 base_breakpoint_print_recreate
10887};
10888
10889/* Default breakpoint_ops methods. */
10890
10891static void
348d480f
PA
10892bkpt_re_set (struct breakpoint *b)
10893{
06edf0c0
PA
10894 /* Do not attempt to re-set breakpoints disabled during startup. */
10895 if (b->enable_state == bp_startup_disabled)
10896 return;
348d480f 10897
06edf0c0
PA
10898 /* FIXME: is this still reachable? */
10899 if (b->addr_string == NULL)
10900 {
10901 /* Anything without a string can't be re-set. */
348d480f 10902 delete_breakpoint (b);
06edf0c0 10903 return;
348d480f 10904 }
06edf0c0
PA
10905
10906 breakpoint_re_set_default (b);
348d480f
PA
10907}
10908
2060206e 10909static int
348d480f
PA
10910bkpt_insert_location (struct bp_location *bl)
10911{
10912 if (bl->loc_type == bp_loc_hardware_breakpoint)
10913 return target_insert_hw_breakpoint (bl->gdbarch,
10914 &bl->target_info);
10915 else
10916 return target_insert_breakpoint (bl->gdbarch,
10917 &bl->target_info);
10918}
10919
2060206e 10920static int
348d480f
PA
10921bkpt_remove_location (struct bp_location *bl)
10922{
10923 if (bl->loc_type == bp_loc_hardware_breakpoint)
10924 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10925 else
10926 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10927}
10928
2060206e 10929static int
348d480f
PA
10930bkpt_breakpoint_hit (const struct bp_location *bl,
10931 struct address_space *aspace, CORE_ADDR bp_addr)
10932{
10933 struct breakpoint *b = bl->owner;
10934
10935 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10936 aspace, bp_addr))
10937 return 0;
10938
10939 if (overlay_debugging /* unmapped overlay section */
10940 && section_is_overlay (bl->section)
10941 && !section_is_mapped (bl->section))
10942 return 0;
10943
10944 return 1;
10945}
10946
2060206e 10947static int
348d480f
PA
10948bkpt_resources_needed (const struct bp_location *bl)
10949{
10950 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10951
10952 return 1;
10953}
10954
2060206e 10955static enum print_stop_action
348d480f
PA
10956bkpt_print_it (bpstat bs)
10957{
348d480f
PA
10958 struct breakpoint *b;
10959 const struct bp_location *bl;
001c8c33 10960 int bp_temp;
79a45e25 10961 struct ui_out *uiout = current_uiout;
348d480f
PA
10962
10963 gdb_assert (bs->bp_location_at != NULL);
10964
10965 bl = bs->bp_location_at;
10966 b = bs->breakpoint_at;
10967
001c8c33
PA
10968 bp_temp = b->disposition == disp_del;
10969 if (bl->address != bl->requested_address)
10970 breakpoint_adjustment_warning (bl->requested_address,
10971 bl->address,
10972 b->number, 1);
10973 annotate_breakpoint (b->number);
10974 if (bp_temp)
10975 ui_out_text (uiout, "\nTemporary breakpoint ");
10976 else
10977 ui_out_text (uiout, "\nBreakpoint ");
10978 if (ui_out_is_mi_like_p (uiout))
348d480f 10979 {
001c8c33
PA
10980 ui_out_field_string (uiout, "reason",
10981 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10982 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 10983 }
001c8c33
PA
10984 ui_out_field_int (uiout, "bkptno", b->number);
10985 ui_out_text (uiout, ", ");
06edf0c0 10986
001c8c33 10987 return PRINT_SRC_AND_LOC;
06edf0c0
PA
10988}
10989
2060206e 10990static void
06edf0c0
PA
10991bkpt_print_mention (struct breakpoint *b)
10992{
79a45e25 10993 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
10994 return;
10995
10996 switch (b->type)
10997 {
10998 case bp_breakpoint:
10999 case bp_gnu_ifunc_resolver:
11000 if (b->disposition == disp_del)
11001 printf_filtered (_("Temporary breakpoint"));
11002 else
11003 printf_filtered (_("Breakpoint"));
11004 printf_filtered (_(" %d"), b->number);
11005 if (b->type == bp_gnu_ifunc_resolver)
11006 printf_filtered (_(" at gnu-indirect-function resolver"));
11007 break;
11008 case bp_hardware_breakpoint:
11009 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
11010 break;
11011 }
11012
11013 say_where (b);
11014}
11015
2060206e 11016static void
06edf0c0
PA
11017bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11018{
11019 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11020 fprintf_unfiltered (fp, "tbreak");
11021 else if (tp->type == bp_breakpoint)
11022 fprintf_unfiltered (fp, "break");
11023 else if (tp->type == bp_hardware_breakpoint
11024 && tp->disposition == disp_del)
11025 fprintf_unfiltered (fp, "thbreak");
11026 else if (tp->type == bp_hardware_breakpoint)
11027 fprintf_unfiltered (fp, "hbreak");
11028 else
11029 internal_error (__FILE__, __LINE__,
11030 _("unhandled breakpoint type %d"), (int) tp->type);
11031
2060206e 11032 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 11033 print_recreate_thread (tp, fp);
06edf0c0
PA
11034}
11035
06edf0c0
PA
11036/* Virtual table for internal breakpoints. */
11037
11038static void
11039internal_bkpt_re_set (struct breakpoint *b)
11040{
11041 switch (b->type)
11042 {
11043 /* Delete overlay event and longjmp master breakpoints; they
11044 will be reset later by breakpoint_re_set. */
11045 case bp_overlay_event:
11046 case bp_longjmp_master:
11047 case bp_std_terminate_master:
11048 case bp_exception_master:
11049 delete_breakpoint (b);
11050 break;
11051
11052 /* This breakpoint is special, it's set up when the inferior
11053 starts and we really don't want to touch it. */
11054 case bp_shlib_event:
11055
11056 /* Like bp_shlib_event, this breakpoint type is special. Once
11057 it is set up, we do not want to touch it. */
11058 case bp_thread_event:
11059 break;
11060 }
11061}
11062
11063static void
11064internal_bkpt_check_status (bpstat bs)
11065{
11066 /* We do not stop for these. */
11067 bs->stop = 0;
11068}
11069
11070static enum print_stop_action
11071internal_bkpt_print_it (bpstat bs)
11072{
06edf0c0 11073 struct breakpoint *b;
06edf0c0 11074
06edf0c0
PA
11075 b = bs->breakpoint_at;
11076
06edf0c0
PA
11077 switch (b->type)
11078 {
348d480f
PA
11079 case bp_shlib_event:
11080 /* Did we stop because the user set the stop_on_solib_events
11081 variable? (If so, we report this as a generic, "Stopped due
11082 to shlib event" message.) */
11083 printf_filtered (_("Stopped due to shared library event\n"));
348d480f
PA
11084 break;
11085
11086 case bp_thread_event:
11087 /* Not sure how we will get here.
11088 GDB should not stop for these breakpoints. */
11089 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11090 break;
11091
11092 case bp_overlay_event:
11093 /* By analogy with the thread event, GDB should not stop for these. */
11094 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
11095 break;
11096
11097 case bp_longjmp_master:
11098 /* These should never be enabled. */
11099 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
11100 break;
11101
11102 case bp_std_terminate_master:
11103 /* These should never be enabled. */
11104 printf_filtered (_("std::terminate Master Breakpoint: "
11105 "gdb should not stop!\n"));
348d480f
PA
11106 break;
11107
11108 case bp_exception_master:
11109 /* These should never be enabled. */
11110 printf_filtered (_("Exception Master Breakpoint: "
11111 "gdb should not stop!\n"));
06edf0c0
PA
11112 break;
11113 }
11114
001c8c33 11115 return PRINT_NOTHING;
06edf0c0
PA
11116}
11117
11118static void
11119internal_bkpt_print_mention (struct breakpoint *b)
11120{
11121 /* Nothing to mention. These breakpoints are internal. */
11122}
11123
06edf0c0
PA
11124/* Virtual table for momentary breakpoints */
11125
11126static void
11127momentary_bkpt_re_set (struct breakpoint *b)
11128{
11129 /* Keep temporary breakpoints, which can be encountered when we step
11130 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11131 Otherwise these should have been blown away via the cleanup chain
11132 or by breakpoint_init_inferior when we rerun the executable. */
11133}
11134
11135static void
11136momentary_bkpt_check_status (bpstat bs)
11137{
11138 /* Nothing. The point of these breakpoints is causing a stop. */
11139}
11140
11141static enum print_stop_action
11142momentary_bkpt_print_it (bpstat bs)
11143{
79a45e25
PA
11144 struct ui_out *uiout = current_uiout;
11145
001c8c33 11146 if (ui_out_is_mi_like_p (uiout))
06edf0c0 11147 {
001c8c33 11148 struct breakpoint *b = bs->breakpoint_at;
348d480f 11149
001c8c33
PA
11150 switch (b->type)
11151 {
11152 case bp_finish:
11153 ui_out_field_string
11154 (uiout, "reason",
11155 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11156 break;
348d480f 11157
001c8c33
PA
11158 case bp_until:
11159 ui_out_field_string
11160 (uiout, "reason",
11161 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11162 break;
11163 }
348d480f
PA
11164 }
11165
001c8c33 11166 return PRINT_UNKNOWN;
348d480f
PA
11167}
11168
06edf0c0
PA
11169static void
11170momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 11171{
06edf0c0 11172 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
11173}
11174
348d480f 11175/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 11176
348d480f
PA
11177static void
11178tracepoint_re_set (struct breakpoint *b)
11179{
11180 breakpoint_re_set_default (b);
11181}
876fa593 11182
348d480f
PA
11183static int
11184tracepoint_breakpoint_hit (const struct bp_location *bl,
11185 struct address_space *aspace, CORE_ADDR bp_addr)
11186{
11187 /* By definition, the inferior does not report stops at
11188 tracepoints. */
11189 return 0;
74960c60
VP
11190}
11191
11192static void
348d480f
PA
11193tracepoint_print_one_detail (const struct breakpoint *self,
11194 struct ui_out *uiout)
74960c60 11195{
d9b3f62e
PA
11196 struct tracepoint *tp = (struct tracepoint *) self;
11197 if (tp->static_trace_marker_id)
348d480f
PA
11198 {
11199 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 11200
348d480f
PA
11201 ui_out_text (uiout, "\tmarker id is ");
11202 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 11203 tp->static_trace_marker_id);
348d480f
PA
11204 ui_out_text (uiout, "\n");
11205 }
0d381245
VP
11206}
11207
a474d7c2 11208static void
348d480f 11209tracepoint_print_mention (struct breakpoint *b)
a474d7c2 11210{
79a45e25 11211 if (ui_out_is_mi_like_p (current_uiout))
348d480f 11212 return;
cc59ec59 11213
348d480f
PA
11214 switch (b->type)
11215 {
11216 case bp_tracepoint:
11217 printf_filtered (_("Tracepoint"));
11218 printf_filtered (_(" %d"), b->number);
11219 break;
11220 case bp_fast_tracepoint:
11221 printf_filtered (_("Fast tracepoint"));
11222 printf_filtered (_(" %d"), b->number);
11223 break;
11224 case bp_static_tracepoint:
11225 printf_filtered (_("Static tracepoint"));
11226 printf_filtered (_(" %d"), b->number);
11227 break;
11228 default:
11229 internal_error (__FILE__, __LINE__,
11230 _("unhandled tracepoint type %d"), (int) b->type);
11231 }
11232
11233 say_where (b);
a474d7c2
PA
11234}
11235
348d480f 11236static void
d9b3f62e 11237tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 11238{
d9b3f62e
PA
11239 struct tracepoint *tp = (struct tracepoint *) self;
11240
11241 if (self->type == bp_fast_tracepoint)
348d480f 11242 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 11243 if (self->type == bp_static_tracepoint)
348d480f 11244 fprintf_unfiltered (fp, "strace");
d9b3f62e 11245 else if (self->type == bp_tracepoint)
348d480f
PA
11246 fprintf_unfiltered (fp, "trace");
11247 else
11248 internal_error (__FILE__, __LINE__,
d9b3f62e 11249 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 11250
d9b3f62e
PA
11251 fprintf_unfiltered (fp, " %s", self->addr_string);
11252 print_recreate_thread (self, fp);
11253
11254 if (tp->pass_count)
11255 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
11256}
11257
2060206e 11258struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 11259
53a5351d 11260/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 11261 structures. */
c906108c
SS
11262
11263void
fba45db2 11264delete_breakpoint (struct breakpoint *bpt)
c906108c 11265{
52f0bd74 11266 struct breakpoint *b;
c906108c 11267
8a3fe4f8 11268 gdb_assert (bpt != NULL);
c906108c 11269
4a64f543
MS
11270 /* Has this bp already been deleted? This can happen because
11271 multiple lists can hold pointers to bp's. bpstat lists are
11272 especial culprits.
11273
11274 One example of this happening is a watchpoint's scope bp. When
11275 the scope bp triggers, we notice that the watchpoint is out of
11276 scope, and delete it. We also delete its scope bp. But the
11277 scope bp is marked "auto-deleting", and is already on a bpstat.
11278 That bpstat is then checked for auto-deleting bp's, which are
11279 deleted.
11280
11281 A real solution to this problem might involve reference counts in
11282 bp's, and/or giving them pointers back to their referencing
11283 bpstat's, and teaching delete_breakpoint to only free a bp's
11284 storage when no more references were extent. A cheaper bandaid
11285 was chosen. */
c906108c
SS
11286 if (bpt->type == bp_none)
11287 return;
11288
4a64f543
MS
11289 /* At least avoid this stale reference until the reference counting
11290 of breakpoints gets resolved. */
d0fb5eae 11291 if (bpt->related_breakpoint != bpt)
e5a0a904 11292 {
d0fb5eae 11293 struct breakpoint *related;
3a5c3e22 11294 struct watchpoint *w;
d0fb5eae
JK
11295
11296 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 11297 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 11298 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
11299 w = (struct watchpoint *) bpt;
11300 else
11301 w = NULL;
11302 if (w != NULL)
11303 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
11304
11305 /* Unlink bpt from the bpt->related_breakpoint ring. */
11306 for (related = bpt; related->related_breakpoint != bpt;
11307 related = related->related_breakpoint);
11308 related->related_breakpoint = bpt->related_breakpoint;
11309 bpt->related_breakpoint = bpt;
e5a0a904
JK
11310 }
11311
a9634178
TJB
11312 /* watch_command_1 creates a watchpoint but only sets its number if
11313 update_watchpoint succeeds in creating its bp_locations. If there's
11314 a problem in that process, we'll be asked to delete the half-created
11315 watchpoint. In that case, don't announce the deletion. */
11316 if (bpt->number)
11317 observer_notify_breakpoint_deleted (bpt);
c906108c 11318
c906108c
SS
11319 if (breakpoint_chain == bpt)
11320 breakpoint_chain = bpt->next;
11321
c906108c
SS
11322 ALL_BREAKPOINTS (b)
11323 if (b->next == bpt)
c5aa993b
JM
11324 {
11325 b->next = bpt->next;
11326 break;
11327 }
c906108c 11328
f431efe5
PA
11329 /* Be sure no bpstat's are pointing at the breakpoint after it's
11330 been freed. */
11331 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 11332 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
11333 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11334 commands are associated with the bpstat; if we remove it here,
11335 then the later call to bpstat_do_actions (&stop_bpstat); in
11336 event-top.c won't do anything, and temporary breakpoints with
11337 commands won't work. */
11338
11339 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11340
4a64f543
MS
11341 /* Now that breakpoint is removed from breakpoint list, update the
11342 global location list. This will remove locations that used to
11343 belong to this breakpoint. Do this before freeing the breakpoint
11344 itself, since remove_breakpoint looks at location's owner. It
11345 might be better design to have location completely
11346 self-contained, but it's not the case now. */
b60e7edf 11347 update_global_location_list (0);
74960c60 11348
348d480f 11349 bpt->ops->dtor (bpt);
4a64f543
MS
11350 /* On the chance that someone will soon try again to delete this
11351 same bp, we mark it as deleted before freeing its storage. */
c906108c 11352 bpt->type = bp_none;
b8c9b27d 11353 xfree (bpt);
c906108c
SS
11354}
11355
4d6140d9
AC
11356static void
11357do_delete_breakpoint_cleanup (void *b)
11358{
11359 delete_breakpoint (b);
11360}
11361
11362struct cleanup *
11363make_cleanup_delete_breakpoint (struct breakpoint *b)
11364{
11365 return make_cleanup (do_delete_breakpoint_cleanup, b);
11366}
11367
51be5b68
PA
11368/* Iterator function to call a user-provided callback function once
11369 for each of B and its related breakpoints. */
11370
11371static void
11372iterate_over_related_breakpoints (struct breakpoint *b,
11373 void (*function) (struct breakpoint *,
11374 void *),
11375 void *data)
11376{
11377 struct breakpoint *related;
11378
11379 related = b;
11380 do
11381 {
11382 struct breakpoint *next;
11383
11384 /* FUNCTION may delete RELATED. */
11385 next = related->related_breakpoint;
11386
11387 if (next == related)
11388 {
11389 /* RELATED is the last ring entry. */
11390 function (related, data);
11391
11392 /* FUNCTION may have deleted it, so we'd never reach back to
11393 B. There's nothing left to do anyway, so just break
11394 out. */
11395 break;
11396 }
11397 else
11398 function (related, data);
11399
11400 related = next;
11401 }
11402 while (related != b);
11403}
95a42b64
TT
11404
11405static void
11406do_delete_breakpoint (struct breakpoint *b, void *ignore)
11407{
11408 delete_breakpoint (b);
11409}
11410
51be5b68
PA
11411/* A callback for map_breakpoint_numbers that calls
11412 delete_breakpoint. */
11413
11414static void
11415do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11416{
11417 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11418}
11419
c906108c 11420void
fba45db2 11421delete_command (char *arg, int from_tty)
c906108c 11422{
35df4500 11423 struct breakpoint *b, *b_tmp;
c906108c 11424
ea9365bb
TT
11425 dont_repeat ();
11426
c906108c
SS
11427 if (arg == 0)
11428 {
11429 int breaks_to_delete = 0;
11430
46c6471b
PA
11431 /* Delete all breakpoints if no argument. Do not delete
11432 internal breakpoints, these have to be deleted with an
11433 explicit breakpoint number argument. */
c5aa993b 11434 ALL_BREAKPOINTS (b)
46c6471b 11435 if (user_breakpoint_p (b))
973d738b
DJ
11436 {
11437 breaks_to_delete = 1;
11438 break;
11439 }
c906108c
SS
11440
11441 /* Ask user only if there are some breakpoints to delete. */
11442 if (!from_tty
e2e0b3e5 11443 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 11444 {
35df4500 11445 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 11446 if (user_breakpoint_p (b))
c5aa993b 11447 delete_breakpoint (b);
c906108c
SS
11448 }
11449 }
11450 else
51be5b68 11451 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
11452}
11453
0d381245
VP
11454static int
11455all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 11456{
0d381245
VP
11457 for (; loc; loc = loc->next)
11458 if (!loc->shlib_disabled)
11459 return 0;
11460 return 1;
fe3f5fa8
VP
11461}
11462
776592bf
DE
11463/* Subroutine of update_breakpoint_locations to simplify it.
11464 Return non-zero if multiple fns in list LOC have the same name.
11465 Null names are ignored. */
11466
11467static int
11468ambiguous_names_p (struct bp_location *loc)
11469{
11470 struct bp_location *l;
11471 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
11472 (int (*) (const void *,
11473 const void *)) streq,
776592bf
DE
11474 NULL, xcalloc, xfree);
11475
11476 for (l = loc; l != NULL; l = l->next)
11477 {
11478 const char **slot;
11479 const char *name = l->function_name;
11480
11481 /* Allow for some names to be NULL, ignore them. */
11482 if (name == NULL)
11483 continue;
11484
11485 slot = (const char **) htab_find_slot (htab, (const void *) name,
11486 INSERT);
4a64f543
MS
11487 /* NOTE: We can assume slot != NULL here because xcalloc never
11488 returns NULL. */
776592bf
DE
11489 if (*slot != NULL)
11490 {
11491 htab_delete (htab);
11492 return 1;
11493 }
11494 *slot = name;
11495 }
11496
11497 htab_delete (htab);
11498 return 0;
11499}
11500
0fb4aa4b
PA
11501/* When symbols change, it probably means the sources changed as well,
11502 and it might mean the static tracepoint markers are no longer at
11503 the same address or line numbers they used to be at last we
11504 checked. Losing your static tracepoints whenever you rebuild is
11505 undesirable. This function tries to resync/rematch gdb static
11506 tracepoints with the markers on the target, for static tracepoints
11507 that have not been set by marker id. Static tracepoint that have
11508 been set by marker id are reset by marker id in breakpoint_re_set.
11509 The heuristic is:
11510
11511 1) For a tracepoint set at a specific address, look for a marker at
11512 the old PC. If one is found there, assume to be the same marker.
11513 If the name / string id of the marker found is different from the
11514 previous known name, assume that means the user renamed the marker
11515 in the sources, and output a warning.
11516
11517 2) For a tracepoint set at a given line number, look for a marker
11518 at the new address of the old line number. If one is found there,
11519 assume to be the same marker. If the name / string id of the
11520 marker found is different from the previous known name, assume that
11521 means the user renamed the marker in the sources, and output a
11522 warning.
11523
11524 3) If a marker is no longer found at the same address or line, it
11525 may mean the marker no longer exists. But it may also just mean
11526 the code changed a bit. Maybe the user added a few lines of code
11527 that made the marker move up or down (in line number terms). Ask
11528 the target for info about the marker with the string id as we knew
11529 it. If found, update line number and address in the matching
11530 static tracepoint. This will get confused if there's more than one
11531 marker with the same ID (possible in UST, although unadvised
11532 precisely because it confuses tools). */
11533
11534static struct symtab_and_line
11535update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11536{
d9b3f62e 11537 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
11538 struct static_tracepoint_marker marker;
11539 CORE_ADDR pc;
11540 int i;
11541
11542 pc = sal.pc;
11543 if (sal.line)
11544 find_line_pc (sal.symtab, sal.line, &pc);
11545
11546 if (target_static_tracepoint_marker_at (pc, &marker))
11547 {
d9b3f62e 11548 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
11549 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11550 b->number,
d9b3f62e 11551 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 11552
d9b3f62e
PA
11553 xfree (tp->static_trace_marker_id);
11554 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
11555 release_static_tracepoint_marker (&marker);
11556
11557 return sal;
11558 }
11559
11560 /* Old marker wasn't found on target at lineno. Try looking it up
11561 by string ID. */
11562 if (!sal.explicit_pc
11563 && sal.line != 0
11564 && sal.symtab != NULL
d9b3f62e 11565 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
11566 {
11567 VEC(static_tracepoint_marker_p) *markers;
11568
11569 markers
d9b3f62e 11570 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
11571
11572 if (!VEC_empty(static_tracepoint_marker_p, markers))
11573 {
11574 struct symtab_and_line sal;
11575 struct symbol *sym;
11576 struct static_tracepoint_marker *marker;
79a45e25 11577 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
11578
11579 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11580
d9b3f62e
PA
11581 xfree (tp->static_trace_marker_id);
11582 tp->static_trace_marker_id = xstrdup (marker->str_id);
0fb4aa4b
PA
11583
11584 warning (_("marker for static tracepoint %d (%s) not "
11585 "found at previous line number"),
d9b3f62e 11586 b->number, tp->static_trace_marker_id);
0fb4aa4b
PA
11587
11588 init_sal (&sal);
11589
11590 sal.pc = marker->address;
11591
11592 sal = find_pc_line (marker->address, 0);
11593 sym = find_pc_sect_function (marker->address, NULL);
11594 ui_out_text (uiout, "Now in ");
11595 if (sym)
11596 {
11597 ui_out_field_string (uiout, "func",
11598 SYMBOL_PRINT_NAME (sym));
11599 ui_out_text (uiout, " at ");
11600 }
11601 ui_out_field_string (uiout, "file", sal.symtab->filename);
11602 ui_out_text (uiout, ":");
11603
11604 if (ui_out_is_mi_like_p (uiout))
11605 {
11606 char *fullname = symtab_to_fullname (sal.symtab);
11607
11608 if (fullname)
11609 ui_out_field_string (uiout, "fullname", fullname);
11610 }
11611
11612 ui_out_field_int (uiout, "line", sal.line);
11613 ui_out_text (uiout, "\n");
11614
11615 b->line_number = sal.line;
11616
11617 xfree (b->source_file);
11618 if (sym)
11619 b->source_file = xstrdup (sal.symtab->filename);
11620 else
11621 b->source_file = NULL;
11622
11623 xfree (b->addr_string);
11624 b->addr_string = xstrprintf ("%s:%d",
11625 sal.symtab->filename, b->line_number);
11626
11627 /* Might be nice to check if function changed, and warn if
11628 so. */
11629
11630 release_static_tracepoint_marker (marker);
11631 }
11632 }
11633 return sal;
11634}
11635
8d3788bd
VP
11636/* Returns 1 iff locations A and B are sufficiently same that
11637 we don't need to report breakpoint as changed. */
11638
11639static int
11640locations_are_equal (struct bp_location *a, struct bp_location *b)
11641{
11642 while (a && b)
11643 {
11644 if (a->address != b->address)
11645 return 0;
11646
11647 if (a->shlib_disabled != b->shlib_disabled)
11648 return 0;
11649
11650 if (a->enabled != b->enabled)
11651 return 0;
11652
11653 a = a->next;
11654 b = b->next;
11655 }
11656
11657 if ((a == NULL) != (b == NULL))
11658 return 0;
11659
11660 return 1;
11661}
11662
f1310107
TJB
11663/* Create new breakpoint locations for B (a hardware or software breakpoint)
11664 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11665 a ranged breakpoint. */
11666
0e30163f 11667void
0d381245 11668update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
11669 struct symtabs_and_lines sals,
11670 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
11671{
11672 int i;
0d381245
VP
11673 struct bp_location *existing_locations = b->loc;
11674
f1310107
TJB
11675 /* Ranged breakpoints have only one start location and one end location. */
11676 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11677
4a64f543
MS
11678 /* If there's no new locations, and all existing locations are
11679 pending, don't do anything. This optimizes the common case where
11680 all locations are in the same shared library, that was unloaded.
11681 We'd like to retain the location, so that when the library is
11682 loaded again, we don't loose the enabled/disabled status of the
11683 individual locations. */
0d381245 11684 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
11685 return;
11686
fe3f5fa8
VP
11687 b->loc = NULL;
11688
0d381245 11689 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 11690 {
0d381245 11691 struct bp_location *new_loc =
39d61571 11692 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 11693
0d381245
VP
11694 /* Reparse conditions, they might contain references to the
11695 old symtab. */
11696 if (b->cond_string != NULL)
11697 {
f1310107 11698 char *s;
0d381245 11699 struct gdb_exception e;
fe3f5fa8 11700
0d381245
VP
11701 s = b->cond_string;
11702 TRY_CATCH (e, RETURN_MASK_ERROR)
11703 {
11704 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11705 0);
11706 }
11707 if (e.reason < 0)
11708 {
3e43a32a
MS
11709 warning (_("failed to reevaluate condition "
11710 "for breakpoint %d: %s"),
0d381245
VP
11711 b->number, e.message);
11712 new_loc->enabled = 0;
11713 }
11714 }
fe3f5fa8 11715
0d381245
VP
11716 if (b->source_file != NULL)
11717 xfree (b->source_file);
11718 if (sals.sals[i].symtab == NULL)
11719 b->source_file = NULL;
11720 else
1b36a34b 11721 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 11722
0d381245
VP
11723 if (b->line_number == 0)
11724 b->line_number = sals.sals[i].line;
f1310107
TJB
11725
11726 if (sals_end.nelts)
11727 {
11728 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11729
11730 new_loc->length = end - sals.sals[0].pc + 1;
11731 }
0d381245 11732 }
fe3f5fa8 11733
514f746b
AR
11734 /* Update locations of permanent breakpoints. */
11735 if (b->enable_state == bp_permanent)
11736 make_breakpoint_permanent (b);
11737
4a64f543
MS
11738 /* If possible, carry over 'disable' status from existing
11739 breakpoints. */
0d381245
VP
11740 {
11741 struct bp_location *e = existing_locations;
776592bf
DE
11742 /* If there are multiple breakpoints with the same function name,
11743 e.g. for inline functions, comparing function names won't work.
11744 Instead compare pc addresses; this is just a heuristic as things
11745 may have moved, but in practice it gives the correct answer
11746 often enough until a better solution is found. */
11747 int have_ambiguous_names = ambiguous_names_p (b->loc);
11748
0d381245
VP
11749 for (; e; e = e->next)
11750 {
11751 if (!e->enabled && e->function_name)
11752 {
11753 struct bp_location *l = b->loc;
776592bf
DE
11754 if (have_ambiguous_names)
11755 {
11756 for (; l; l = l->next)
f1310107 11757 if (breakpoint_locations_match (e, l))
776592bf
DE
11758 {
11759 l->enabled = 0;
11760 break;
11761 }
11762 }
11763 else
11764 {
11765 for (; l; l = l->next)
11766 if (l->function_name
11767 && strcmp (e->function_name, l->function_name) == 0)
11768 {
11769 l->enabled = 0;
11770 break;
11771 }
11772 }
0d381245
VP
11773 }
11774 }
11775 }
fe3f5fa8 11776
8d3788bd
VP
11777 if (!locations_are_equal (existing_locations, b->loc))
11778 observer_notify_breakpoint_modified (b);
11779
b60e7edf 11780 update_global_location_list (1);
fe3f5fa8
VP
11781}
11782
ef23e705
TJB
11783/* Find the SaL locations corresponding to the given ADDR_STRING.
11784 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11785
11786static struct symtabs_and_lines
11787addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11788{
11789 char *s;
58438ac1 11790 int marker_spec;
02d20e4a 11791 struct symtabs_and_lines sals = {0};
ef23e705
TJB
11792 struct gdb_exception e;
11793
11794 s = addr_string;
11795 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11796
11797 TRY_CATCH (e, RETURN_MASK_ERROR)
11798 {
11799 if (marker_spec)
11800 {
d9b3f62e
PA
11801 struct tracepoint *tp = (struct tracepoint *) b;
11802
ef23e705 11803 sals = decode_static_tracepoint_spec (&s);
d9b3f62e 11804 if (sals.nelts > tp->static_trace_marker_id_idx)
ef23e705 11805 {
d9b3f62e 11806 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
ef23e705
TJB
11807 sals.nelts = 1;
11808 }
11809 else
d9b3f62e 11810 error (_("marker %s not found"), tp->static_trace_marker_id);
ef23e705
TJB
11811 }
11812 else
58438ac1 11813 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
ef23e705
TJB
11814 }
11815 if (e.reason < 0)
11816 {
11817 int not_found_and_ok = 0;
11818 /* For pending breakpoints, it's expected that parsing will
11819 fail until the right shared library is loaded. User has
11820 already told to create pending breakpoints and don't need
11821 extra messages. If breakpoint is in bp_shlib_disabled
11822 state, then user already saw the message about that
11823 breakpoint being disabled, and don't want to see more
11824 errors. */
58438ac1 11825 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
11826 && (b->condition_not_parsed
11827 || (b->loc && b->loc->shlib_disabled)
11828 || b->enable_state == bp_disabled))
11829 not_found_and_ok = 1;
11830
11831 if (!not_found_and_ok)
11832 {
11833 /* We surely don't want to warn about the same breakpoint
11834 10 times. One solution, implemented here, is disable
11835 the breakpoint on error. Another solution would be to
11836 have separate 'warning emitted' flag. Since this
11837 happens only when a binary has changed, I don't know
11838 which approach is better. */
11839 b->enable_state = bp_disabled;
11840 throw_exception (e);
11841 }
11842 }
11843
58438ac1 11844 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705
TJB
11845 {
11846 gdb_assert (sals.nelts == 1);
11847
11848 resolve_sal_pc (&sals.sals[0]);
11849 if (b->condition_not_parsed && s && s[0])
11850 {
11851 char *cond_string = 0;
11852 int thread = -1;
11853 int task = 0;
11854
11855 find_condition_and_thread (s, sals.sals[0].pc,
11856 &cond_string, &thread, &task);
11857 if (cond_string)
11858 b->cond_string = cond_string;
11859 b->thread = thread;
11860 b->task = task;
11861 b->condition_not_parsed = 0;
11862 }
11863
11864 if (b->type == bp_static_tracepoint && !marker_spec)
11865 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 11866
58438ac1
TT
11867 *found = 1;
11868 }
11869 else
11870 *found = 0;
ef23e705
TJB
11871
11872 return sals;
11873}
11874
348d480f
PA
11875/* The default re_set method, for typical hardware or software
11876 breakpoints. Reevaluate the breakpoint and recreate its
11877 locations. */
11878
11879static void
28010a5d 11880breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
11881{
11882 int found;
f1310107 11883 struct symtabs_and_lines sals, sals_end;
ef23e705 11884 struct symtabs_and_lines expanded = {0};
f1310107 11885 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
11886
11887 sals = addr_string_to_sals (b, b->addr_string, &found);
11888 if (found)
11889 {
11890 make_cleanup (xfree, sals.sals);
11891 expanded = expand_line_sal_maybe (sals.sals[0]);
11892 }
11893
f1310107
TJB
11894 if (b->addr_string_range_end)
11895 {
11896 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11897 if (found)
11898 {
11899 make_cleanup (xfree, sals_end.sals);
11900 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11901 }
11902 }
11903
11904 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
11905}
11906
11907/* Prepare the global context for a re-set of breakpoint B. */
11908
11909static struct cleanup *
11910prepare_re_set_context (struct breakpoint *b)
11911{
11912 struct cleanup *cleanups;
11913
11914 input_radix = b->input_radix;
11915 cleanups = save_current_space_and_thread ();
11916 switch_to_program_space_and_thread (b->pspace);
11917 set_language (b->language);
11918
11919 return cleanups;
ef23e705
TJB
11920}
11921
c906108c
SS
11922/* Reset a breakpoint given it's struct breakpoint * BINT.
11923 The value we return ends up being the return value from catch_errors.
11924 Unused in this case. */
11925
11926static int
4efb68b1 11927breakpoint_re_set_one (void *bint)
c906108c 11928{
4a64f543 11929 /* Get past catch_errs. */
53a5351d 11930 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 11931 struct cleanup *cleanups;
c906108c 11932
348d480f
PA
11933 cleanups = prepare_re_set_context (b);
11934 b->ops->re_set (b);
11935 do_cleanups (cleanups);
c906108c
SS
11936 return 0;
11937}
11938
69de3c6a 11939/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 11940void
69de3c6a 11941breakpoint_re_set (void)
c906108c 11942{
35df4500 11943 struct breakpoint *b, *b_tmp;
c906108c
SS
11944 enum language save_language;
11945 int save_input_radix;
6c95b8df 11946 struct cleanup *old_chain;
c5aa993b 11947
c906108c
SS
11948 save_language = current_language->la_language;
11949 save_input_radix = input_radix;
6c95b8df
PA
11950 old_chain = save_current_program_space ();
11951
35df4500 11952 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11953 {
4a64f543 11954 /* Format possible error msg. */
fe3f5fa8 11955 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
11956 b->number);
11957 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 11958 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 11959 do_cleanups (cleanups);
c5aa993b 11960 }
c906108c
SS
11961 set_language (save_language);
11962 input_radix = save_input_radix;
e62c965a 11963
0756c555 11964 jit_breakpoint_re_set ();
4efc6507 11965
6c95b8df
PA
11966 do_cleanups (old_chain);
11967
af02033e
PP
11968 create_overlay_event_breakpoint ();
11969 create_longjmp_master_breakpoint ();
11970 create_std_terminate_master_breakpoint ();
186c406b 11971 create_exception_master_breakpoint ();
1bfeeb0f
JL
11972
11973 /* While we're at it, reset the skip list too. */
11974 skip_re_set ();
c906108c
SS
11975}
11976\f
c906108c
SS
11977/* Reset the thread number of this breakpoint:
11978
11979 - If the breakpoint is for all threads, leave it as-is.
4a64f543 11980 - Else, reset it to the current thread for inferior_ptid. */
c906108c 11981void
fba45db2 11982breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
11983{
11984 if (b->thread != -1)
11985 {
39f77062
KB
11986 if (in_thread_list (inferior_ptid))
11987 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
11988
11989 /* We're being called after following a fork. The new fork is
11990 selected as current, and unless this was a vfork will have a
11991 different program space from the original thread. Reset that
11992 as well. */
11993 b->loc->pspace = current_program_space;
c906108c
SS
11994 }
11995}
11996
03ac34d5
MS
11997/* Set ignore-count of breakpoint number BPTNUM to COUNT.
11998 If from_tty is nonzero, it prints a message to that effect,
11999 which ends with a period (no newline). */
12000
c906108c 12001void
fba45db2 12002set_ignore_count (int bptnum, int count, int from_tty)
c906108c 12003{
52f0bd74 12004 struct breakpoint *b;
c906108c
SS
12005
12006 if (count < 0)
12007 count = 0;
12008
12009 ALL_BREAKPOINTS (b)
12010 if (b->number == bptnum)
c5aa993b 12011 {
d77f58be
SS
12012 if (is_tracepoint (b))
12013 {
12014 if (from_tty && count != 0)
12015 printf_filtered (_("Ignore count ignored for tracepoint %d."),
12016 bptnum);
12017 return;
12018 }
12019
c5aa993b 12020 b->ignore_count = count;
221ea385
KS
12021 if (from_tty)
12022 {
12023 if (count == 0)
3e43a32a
MS
12024 printf_filtered (_("Will stop next time "
12025 "breakpoint %d is reached."),
221ea385
KS
12026 bptnum);
12027 else if (count == 1)
a3f17187 12028 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
12029 bptnum);
12030 else
3e43a32a
MS
12031 printf_filtered (_("Will ignore next %d "
12032 "crossings of breakpoint %d."),
221ea385
KS
12033 count, bptnum);
12034 }
c5aa993b 12035 breakpoints_changed ();
8d3788bd 12036 observer_notify_breakpoint_modified (b);
c5aa993b
JM
12037 return;
12038 }
c906108c 12039
8a3fe4f8 12040 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
12041}
12042
c906108c
SS
12043/* Command to set ignore-count of breakpoint N to COUNT. */
12044
12045static void
fba45db2 12046ignore_command (char *args, int from_tty)
c906108c
SS
12047{
12048 char *p = args;
52f0bd74 12049 int num;
c906108c
SS
12050
12051 if (p == 0)
e2e0b3e5 12052 error_no_arg (_("a breakpoint number"));
c5aa993b 12053
c906108c 12054 num = get_number (&p);
5c44784c 12055 if (num == 0)
8a3fe4f8 12056 error (_("bad breakpoint number: '%s'"), args);
c906108c 12057 if (*p == 0)
8a3fe4f8 12058 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
12059
12060 set_ignore_count (num,
12061 longest_to_int (value_as_long (parse_and_eval (p))),
12062 from_tty);
221ea385
KS
12063 if (from_tty)
12064 printf_filtered ("\n");
c906108c
SS
12065}
12066\f
12067/* Call FUNCTION on each of the breakpoints
12068 whose numbers are given in ARGS. */
12069
12070static void
95a42b64
TT
12071map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12072 void *),
12073 void *data)
c906108c 12074{
52f0bd74
AC
12075 int num;
12076 struct breakpoint *b, *tmp;
11cf8741 12077 int match;
197f0a60 12078 struct get_number_or_range_state state;
c906108c 12079
197f0a60 12080 if (args == 0)
e2e0b3e5 12081 error_no_arg (_("one or more breakpoint numbers"));
c906108c 12082
197f0a60
TT
12083 init_number_or_range (&state, args);
12084
12085 while (!state.finished)
c906108c 12086 {
197f0a60
TT
12087 char *p = state.string;
12088
11cf8741 12089 match = 0;
c5aa993b 12090
197f0a60 12091 num = get_number_or_range (&state);
5c44784c 12092 if (num == 0)
c5aa993b 12093 {
8a3fe4f8 12094 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
12095 }
12096 else
12097 {
12098 ALL_BREAKPOINTS_SAFE (b, tmp)
12099 if (b->number == num)
12100 {
11cf8741 12101 match = 1;
cdac0397 12102 function (b, data);
11cf8741 12103 break;
5c44784c 12104 }
11cf8741 12105 if (match == 0)
a3f17187 12106 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 12107 }
c906108c
SS
12108 }
12109}
12110
0d381245
VP
12111static struct bp_location *
12112find_location_by_number (char *number)
12113{
12114 char *dot = strchr (number, '.');
12115 char *p1;
12116 int bp_num;
12117 int loc_num;
12118 struct breakpoint *b;
12119 struct bp_location *loc;
12120
12121 *dot = '\0';
12122
12123 p1 = number;
197f0a60 12124 bp_num = get_number (&p1);
0d381245
VP
12125 if (bp_num == 0)
12126 error (_("Bad breakpoint number '%s'"), number);
12127
12128 ALL_BREAKPOINTS (b)
12129 if (b->number == bp_num)
12130 {
12131 break;
12132 }
12133
12134 if (!b || b->number != bp_num)
12135 error (_("Bad breakpoint number '%s'"), number);
12136
12137 p1 = dot+1;
197f0a60 12138 loc_num = get_number (&p1);
0d381245
VP
12139 if (loc_num == 0)
12140 error (_("Bad breakpoint location number '%s'"), number);
12141
12142 --loc_num;
12143 loc = b->loc;
12144 for (;loc_num && loc; --loc_num, loc = loc->next)
12145 ;
12146 if (!loc)
12147 error (_("Bad breakpoint location number '%s'"), dot+1);
12148
12149 return loc;
12150}
12151
12152
1900040c
MS
12153/* Set ignore-count of breakpoint number BPTNUM to COUNT.
12154 If from_tty is nonzero, it prints a message to that effect,
12155 which ends with a period (no newline). */
12156
c906108c 12157void
fba45db2 12158disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
12159{
12160 /* Never disable a watchpoint scope breakpoint; we want to
12161 hit them when we leave scope so we can delete both the
12162 watchpoint and its scope breakpoint at that time. */
12163 if (bpt->type == bp_watchpoint_scope)
12164 return;
12165
c2c6d25f 12166 /* You can't disable permanent breakpoints. */
b5de0fa7 12167 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
12168 return;
12169
b5de0fa7 12170 bpt->enable_state = bp_disabled;
c906108c 12171
d248b706
KY
12172 if (target_supports_enable_disable_tracepoint ()
12173 && current_trace_status ()->running && is_tracepoint (bpt))
12174 {
12175 struct bp_location *location;
12176
12177 for (location = bpt->loc; location; location = location->next)
12178 target_disable_tracepoint (location);
12179 }
12180
b60e7edf 12181 update_global_location_list (0);
c906108c 12182
8d3788bd 12183 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12184}
12185
51be5b68
PA
12186/* A callback for iterate_over_related_breakpoints. */
12187
12188static void
12189do_disable_breakpoint (struct breakpoint *b, void *ignore)
12190{
12191 disable_breakpoint (b);
12192}
12193
95a42b64
TT
12194/* A callback for map_breakpoint_numbers that calls
12195 disable_breakpoint. */
12196
12197static void
12198do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12199{
51be5b68 12200 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
12201}
12202
c906108c 12203static void
fba45db2 12204disable_command (char *args, int from_tty)
c906108c 12205{
c906108c 12206 if (args == 0)
46c6471b
PA
12207 {
12208 struct breakpoint *bpt;
12209
12210 ALL_BREAKPOINTS (bpt)
12211 if (user_breakpoint_p (bpt))
12212 disable_breakpoint (bpt);
12213 }
0d381245
VP
12214 else if (strchr (args, '.'))
12215 {
12216 struct bp_location *loc = find_location_by_number (args);
12217 if (loc)
d248b706
KY
12218 {
12219 loc->enabled = 0;
12220 if (target_supports_enable_disable_tracepoint ()
12221 && current_trace_status ()->running && loc->owner
12222 && is_tracepoint (loc->owner))
12223 target_disable_tracepoint (loc);
12224 }
b60e7edf 12225 update_global_location_list (0);
0d381245 12226 }
c906108c 12227 else
95a42b64 12228 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
12229}
12230
12231static void
51be5b68 12232enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 12233{
afe38095 12234 int target_resources_ok;
c906108c
SS
12235
12236 if (bpt->type == bp_hardware_breakpoint)
12237 {
12238 int i;
c5aa993b 12239 i = hw_breakpoint_used_count ();
53a5351d 12240 target_resources_ok =
d92524f1 12241 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 12242 i + 1, 0);
c906108c 12243 if (target_resources_ok == 0)
8a3fe4f8 12244 error (_("No hardware breakpoint support in the target."));
c906108c 12245 else if (target_resources_ok < 0)
8a3fe4f8 12246 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
12247 }
12248
cc60f2e3 12249 if (is_watchpoint (bpt))
c906108c 12250 {
d07205c2
JK
12251 /* Initialize it just to avoid a GCC false warning. */
12252 enum enable_state orig_enable_state = 0;
dde02812
ES
12253 struct gdb_exception e;
12254
12255 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 12256 {
3a5c3e22
PA
12257 struct watchpoint *w = (struct watchpoint *) bpt;
12258
1e718ff1
TJB
12259 orig_enable_state = bpt->enable_state;
12260 bpt->enable_state = bp_enabled;
3a5c3e22 12261 update_watchpoint (w, 1 /* reparse */);
c906108c 12262 }
dde02812 12263 if (e.reason < 0)
c5aa993b 12264 {
1e718ff1 12265 bpt->enable_state = orig_enable_state;
dde02812
ES
12266 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12267 bpt->number);
12268 return;
c5aa993b 12269 }
c906108c 12270 }
0101ce28 12271
b4c291bb
KH
12272 if (bpt->enable_state != bp_permanent)
12273 bpt->enable_state = bp_enabled;
d248b706
KY
12274
12275 if (target_supports_enable_disable_tracepoint ()
12276 && current_trace_status ()->running && is_tracepoint (bpt))
12277 {
12278 struct bp_location *location;
12279
12280 for (location = bpt->loc; location; location = location->next)
12281 target_enable_tracepoint (location);
12282 }
12283
b4c291bb 12284 bpt->disposition = disposition;
b60e7edf 12285 update_global_location_list (1);
b4c291bb
KH
12286 breakpoints_changed ();
12287
8d3788bd 12288 observer_notify_breakpoint_modified (bpt);
c906108c
SS
12289}
12290
fe3f5fa8 12291
c906108c 12292void
fba45db2 12293enable_breakpoint (struct breakpoint *bpt)
c906108c 12294{
51be5b68
PA
12295 enable_breakpoint_disp (bpt, bpt->disposition);
12296}
12297
12298static void
12299do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12300{
12301 enable_breakpoint (bpt);
c906108c
SS
12302}
12303
95a42b64
TT
12304/* A callback for map_breakpoint_numbers that calls
12305 enable_breakpoint. */
12306
12307static void
12308do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12309{
51be5b68 12310 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
12311}
12312
c906108c
SS
12313/* The enable command enables the specified breakpoints (or all defined
12314 breakpoints) so they once again become (or continue to be) effective
1272ad14 12315 in stopping the inferior. */
c906108c 12316
c906108c 12317static void
fba45db2 12318enable_command (char *args, int from_tty)
c906108c 12319{
c906108c 12320 if (args == 0)
46c6471b
PA
12321 {
12322 struct breakpoint *bpt;
12323
12324 ALL_BREAKPOINTS (bpt)
12325 if (user_breakpoint_p (bpt))
12326 enable_breakpoint (bpt);
12327 }
0d381245
VP
12328 else if (strchr (args, '.'))
12329 {
12330 struct bp_location *loc = find_location_by_number (args);
12331 if (loc)
d248b706
KY
12332 {
12333 loc->enabled = 1;
12334 if (target_supports_enable_disable_tracepoint ()
12335 && current_trace_status ()->running && loc->owner
12336 && is_tracepoint (loc->owner))
12337 target_enable_tracepoint (loc);
12338 }
b60e7edf 12339 update_global_location_list (1);
0d381245 12340 }
c906108c 12341 else
95a42b64 12342 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
12343}
12344
12345static void
51be5b68
PA
12346do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12347{
12348 enum bpdisp disp = *(enum bpdisp *) arg;
12349
12350 enable_breakpoint_disp (bpt, disp);
12351}
12352
12353static void
12354do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12355{
51be5b68
PA
12356 enum bpdisp disp = disp_disable;
12357
12358 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12359}
12360
c906108c 12361static void
fba45db2 12362enable_once_command (char *args, int from_tty)
c906108c 12363{
51be5b68 12364 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
12365}
12366
12367static void
51be5b68 12368do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 12369{
51be5b68
PA
12370 enum bpdisp disp = disp_del;
12371
12372 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
12373}
12374
c906108c 12375static void
fba45db2 12376enable_delete_command (char *args, int from_tty)
c906108c 12377{
51be5b68 12378 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
12379}
12380\f
fa8d40ab
JJ
12381static void
12382set_breakpoint_cmd (char *args, int from_tty)
12383{
12384}
12385
12386static void
12387show_breakpoint_cmd (char *args, int from_tty)
12388{
12389}
12390
1f3b5d1b
PP
12391/* Invalidate last known value of any hardware watchpoint if
12392 the memory which that value represents has been written to by
12393 GDB itself. */
12394
12395static void
12396invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12397 const bfd_byte *data)
12398{
12399 struct breakpoint *bp;
12400
12401 ALL_BREAKPOINTS (bp)
12402 if (bp->enable_state == bp_enabled
3a5c3e22 12403 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 12404 {
3a5c3e22 12405 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 12406
3a5c3e22
PA
12407 if (wp->val_valid && wp->val)
12408 {
12409 struct bp_location *loc;
12410
12411 for (loc = bp->loc; loc != NULL; loc = loc->next)
12412 if (loc->loc_type == bp_loc_hardware_watchpoint
12413 && loc->address + loc->length > addr
12414 && addr + len > loc->address)
12415 {
12416 value_free (wp->val);
12417 wp->val = NULL;
12418 wp->val_valid = 0;
12419 }
12420 }
1f3b5d1b
PP
12421 }
12422}
12423
1bfeeb0f
JL
12424/* Use the last displayed codepoint's values, or nothing
12425 if they aren't valid. */
c906108c
SS
12426
12427struct symtabs_and_lines
fba45db2 12428decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
12429{
12430 struct symtabs_and_lines sals;
cc59ec59 12431
c906108c 12432 if (string == 0)
8a3fe4f8 12433 error (_("Empty line specification."));
1bfeeb0f 12434 if (last_displayed_sal_is_valid ())
c906108c 12435 sals = decode_line_1 (&string, funfirstline,
1bfeeb0f
JL
12436 get_last_displayed_symtab (),
12437 get_last_displayed_line (),
58438ac1 12438 NULL);
c906108c
SS
12439 else
12440 sals = decode_line_1 (&string, funfirstline,
58438ac1 12441 (struct symtab *) NULL, 0, NULL);
c906108c 12442 if (*string)
8a3fe4f8 12443 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
12444 return sals;
12445}
8181d85f
DJ
12446
12447/* Create and insert a raw software breakpoint at PC. Return an
12448 identifier, which should be used to remove the breakpoint later.
12449 In general, places which call this should be using something on the
12450 breakpoint chain instead; this function should be eliminated
12451 someday. */
12452
12453void *
6c95b8df
PA
12454deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12455 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
12456{
12457 struct bp_target_info *bp_tgt;
12458
6c95b8df 12459 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 12460
6c95b8df 12461 bp_tgt->placed_address_space = aspace;
8181d85f 12462 bp_tgt->placed_address = pc;
6c95b8df 12463
a6d9a66e 12464 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
12465 {
12466 /* Could not insert the breakpoint. */
12467 xfree (bp_tgt);
12468 return NULL;
12469 }
12470
12471 return bp_tgt;
12472}
12473
4a64f543
MS
12474/* Remove a breakpoint BP inserted by
12475 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
12476
12477int
a6d9a66e 12478deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
12479{
12480 struct bp_target_info *bp_tgt = bp;
12481 int ret;
12482
a6d9a66e 12483 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
12484 xfree (bp_tgt);
12485
12486 return ret;
12487}
12488
4a64f543
MS
12489/* One (or perhaps two) breakpoints used for software single
12490 stepping. */
8181d85f
DJ
12491
12492static void *single_step_breakpoints[2];
a6d9a66e 12493static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
12494
12495/* Create and insert a breakpoint for software single step. */
12496
12497void
6c95b8df 12498insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
12499 struct address_space *aspace,
12500 CORE_ADDR next_pc)
8181d85f
DJ
12501{
12502 void **bpt_p;
12503
12504 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
12505 {
12506 bpt_p = &single_step_breakpoints[0];
12507 single_step_gdbarch[0] = gdbarch;
12508 }
8181d85f
DJ
12509 else
12510 {
12511 gdb_assert (single_step_breakpoints[1] == NULL);
12512 bpt_p = &single_step_breakpoints[1];
a6d9a66e 12513 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
12514 }
12515
4a64f543
MS
12516 /* NOTE drow/2006-04-11: A future improvement to this function would
12517 be to only create the breakpoints once, and actually put them on
12518 the breakpoint chain. That would let us use set_raw_breakpoint.
12519 We could adjust the addresses each time they were needed. Doing
12520 this requires corresponding changes elsewhere where single step
12521 breakpoints are handled, however. So, for now, we use this. */
8181d85f 12522
6c95b8df 12523 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 12524 if (*bpt_p == NULL)
5af949e3
UW
12525 error (_("Could not insert single-step breakpoint at %s"),
12526 paddress (gdbarch, next_pc));
8181d85f
DJ
12527}
12528
f02253f1
HZ
12529/* Check if the breakpoints used for software single stepping
12530 were inserted or not. */
12531
12532int
12533single_step_breakpoints_inserted (void)
12534{
12535 return (single_step_breakpoints[0] != NULL
12536 || single_step_breakpoints[1] != NULL);
12537}
12538
8181d85f
DJ
12539/* Remove and delete any breakpoints used for software single step. */
12540
12541void
12542remove_single_step_breakpoints (void)
12543{
12544 gdb_assert (single_step_breakpoints[0] != NULL);
12545
12546 /* See insert_single_step_breakpoint for more about this deprecated
12547 call. */
a6d9a66e
UW
12548 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12549 single_step_breakpoints[0]);
12550 single_step_gdbarch[0] = NULL;
8181d85f
DJ
12551 single_step_breakpoints[0] = NULL;
12552
12553 if (single_step_breakpoints[1] != NULL)
12554 {
a6d9a66e
UW
12555 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12556 single_step_breakpoints[1]);
12557 single_step_gdbarch[1] = NULL;
8181d85f
DJ
12558 single_step_breakpoints[1] = NULL;
12559 }
12560}
12561
d03285ec
UW
12562/* Delete software single step breakpoints without removing them from
12563 the inferior. This is intended to be used if the inferior's address
12564 space where they were inserted is already gone, e.g. after exit or
12565 exec. */
12566
12567void
12568cancel_single_step_breakpoints (void)
12569{
12570 int i;
12571
12572 for (i = 0; i < 2; i++)
12573 if (single_step_breakpoints[i])
12574 {
12575 xfree (single_step_breakpoints[i]);
12576 single_step_breakpoints[i] = NULL;
12577 single_step_gdbarch[i] = NULL;
12578 }
12579}
12580
12581/* Detach software single-step breakpoints from INFERIOR_PTID without
12582 removing them. */
12583
12584static void
12585detach_single_step_breakpoints (void)
12586{
12587 int i;
12588
12589 for (i = 0; i < 2; i++)
12590 if (single_step_breakpoints[i])
12591 target_remove_breakpoint (single_step_gdbarch[i],
12592 single_step_breakpoints[i]);
12593}
12594
4a64f543
MS
12595/* Check whether a software single-step breakpoint is inserted at
12596 PC. */
1aafd4da
UW
12597
12598static int
cc59ec59
MS
12599single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12600 CORE_ADDR pc)
1aafd4da
UW
12601{
12602 int i;
12603
12604 for (i = 0; i < 2; i++)
12605 {
12606 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
12607 if (bp_tgt
12608 && breakpoint_address_match (bp_tgt->placed_address_space,
12609 bp_tgt->placed_address,
12610 aspace, pc))
1aafd4da
UW
12611 return 1;
12612 }
12613
12614 return 0;
12615}
12616
a96d9b2e
SDJ
12617/* Returns 0 if 'bp' is NOT a syscall catchpoint,
12618 non-zero otherwise. */
12619static int
12620is_syscall_catchpoint_enabled (struct breakpoint *bp)
12621{
12622 if (syscall_catchpoint_p (bp)
12623 && bp->enable_state != bp_disabled
12624 && bp->enable_state != bp_call_disabled)
12625 return 1;
12626 else
12627 return 0;
12628}
12629
12630int
12631catch_syscall_enabled (void)
12632{
12633 struct inferior *inf = current_inferior ();
12634
12635 return inf->total_syscalls_count != 0;
12636}
12637
12638int
12639catching_syscall_number (int syscall_number)
12640{
12641 struct breakpoint *bp;
12642
12643 ALL_BREAKPOINTS (bp)
12644 if (is_syscall_catchpoint_enabled (bp))
12645 {
be5c67c1
PA
12646 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12647
12648 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
12649 {
12650 int i, iter;
12651 for (i = 0;
be5c67c1 12652 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
12653 i++)
12654 if (syscall_number == iter)
12655 return 1;
12656 }
12657 else
12658 return 1;
12659 }
12660
12661 return 0;
12662}
12663
12664/* Complete syscall names. Used by "catch syscall". */
12665static char **
12666catch_syscall_completer (struct cmd_list_element *cmd,
12667 char *text, char *word)
12668{
12669 const char **list = get_syscall_names ();
c38eea1a
MS
12670 char **retlist
12671 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 12672
c38eea1a
MS
12673 xfree (list);
12674 return retlist;
a96d9b2e
SDJ
12675}
12676
1042e4c0
SS
12677/* Tracepoint-specific operations. */
12678
12679/* Set tracepoint count to NUM. */
12680static void
12681set_tracepoint_count (int num)
12682{
12683 tracepoint_count = num;
4fa62494 12684 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
12685}
12686
12687void
12688trace_command (char *arg, int from_tty)
12689{
8cdf0e15
VP
12690 if (create_breakpoint (get_current_arch (),
12691 arg,
12692 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12693 0 /* tempflag */,
12694 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
12695 0 /* Ignore count */,
12696 pending_break_support,
348d480f 12697 &tracepoint_breakpoint_ops,
8cdf0e15 12698 from_tty,
84f4c1fe
PM
12699 1 /* enabled */,
12700 0 /* internal */))
fd9b8c24 12701 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
12702}
12703
7a697b8d
SS
12704void
12705ftrace_command (char *arg, int from_tty)
12706{
8cdf0e15
VP
12707 if (create_breakpoint (get_current_arch (),
12708 arg,
12709 NULL, 0, 1 /* parse arg */,
0fb4aa4b
PA
12710 0 /* tempflag */,
12711 bp_fast_tracepoint /* type_wanted */,
12712 0 /* Ignore count */,
12713 pending_break_support,
348d480f 12714 &tracepoint_breakpoint_ops,
0fb4aa4b 12715 from_tty,
84f4c1fe
PM
12716 1 /* enabled */,
12717 0 /* internal */))
0fb4aa4b
PA
12718 set_tracepoint_count (breakpoint_count);
12719}
12720
12721/* strace command implementation. Creates a static tracepoint. */
12722
12723void
12724strace_command (char *arg, int from_tty)
12725{
12726 if (create_breakpoint (get_current_arch (),
12727 arg,
12728 NULL, 0, 1 /* parse arg */,
12729 0 /* tempflag */,
12730 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
12731 0 /* Ignore count */,
12732 pending_break_support,
348d480f 12733 &tracepoint_breakpoint_ops,
8cdf0e15 12734 from_tty,
84f4c1fe
PM
12735 1 /* enabled */,
12736 0 /* internal */))
fd9b8c24 12737 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
12738}
12739
409873ef
SS
12740/* Set up a fake reader function that gets command lines from a linked
12741 list that was acquired during tracepoint uploading. */
12742
12743static struct uploaded_tp *this_utp;
3149d8c1 12744static int next_cmd;
409873ef
SS
12745
12746static char *
12747read_uploaded_action (void)
12748{
12749 char *rslt;
12750
3149d8c1 12751 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 12752
3149d8c1 12753 next_cmd++;
409873ef
SS
12754
12755 return rslt;
12756}
12757
00bf0b85
SS
12758/* Given information about a tracepoint as recorded on a target (which
12759 can be either a live system or a trace file), attempt to create an
12760 equivalent GDB tracepoint. This is not a reliable process, since
12761 the target does not necessarily have all the information used when
12762 the tracepoint was originally defined. */
12763
d9b3f62e 12764struct tracepoint *
00bf0b85 12765create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 12766{
409873ef 12767 char *addr_str, small_buf[100];
d9b3f62e 12768 struct tracepoint *tp;
fd9b8c24 12769
409873ef
SS
12770 if (utp->at_string)
12771 addr_str = utp->at_string;
12772 else
12773 {
12774 /* In the absence of a source location, fall back to raw
12775 address. Since there is no way to confirm that the address
12776 means the same thing as when the trace was started, warn the
12777 user. */
3e43a32a
MS
12778 warning (_("Uploaded tracepoint %d has no "
12779 "source location, using raw address"),
409873ef
SS
12780 utp->number);
12781 sprintf (small_buf, "*%s", hex_string (utp->addr));
12782 addr_str = small_buf;
12783 }
12784
12785 /* There's not much we can do with a sequence of bytecodes. */
12786 if (utp->cond && !utp->cond_string)
3e43a32a
MS
12787 warning (_("Uploaded tracepoint %d condition "
12788 "has no source form, ignoring it"),
409873ef 12789 utp->number);
d5551862 12790
8cdf0e15 12791 if (!create_breakpoint (get_current_arch (),
409873ef
SS
12792 addr_str,
12793 utp->cond_string, -1, 0 /* parse cond/thread */,
8cdf0e15 12794 0 /* tempflag */,
0fb4aa4b 12795 utp->type /* type_wanted */,
8cdf0e15
VP
12796 0 /* Ignore count */,
12797 pending_break_support,
348d480f 12798 &tracepoint_breakpoint_ops,
8cdf0e15 12799 0 /* from_tty */,
84f4c1fe
PM
12800 utp->enabled /* enabled */,
12801 0 /* internal */))
fd9b8c24
PA
12802 return NULL;
12803
00bf0b85
SS
12804 set_tracepoint_count (breakpoint_count);
12805
409873ef 12806 /* Get the tracepoint we just created. */
fd9b8c24
PA
12807 tp = get_tracepoint (tracepoint_count);
12808 gdb_assert (tp != NULL);
d5551862 12809
00bf0b85
SS
12810 if (utp->pass > 0)
12811 {
d9b3f62e 12812 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 12813
409873ef 12814 trace_pass_command (small_buf, 0);
00bf0b85
SS
12815 }
12816
409873ef
SS
12817 /* If we have uploaded versions of the original commands, set up a
12818 special-purpose "reader" function and call the usual command line
12819 reader, then pass the result to the breakpoint command-setting
12820 function. */
3149d8c1 12821 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 12822 {
409873ef 12823 struct command_line *cmd_list;
00bf0b85 12824
409873ef 12825 this_utp = utp;
3149d8c1 12826 next_cmd = 0;
d5551862 12827
409873ef
SS
12828 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12829
d9b3f62e 12830 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 12831 }
3149d8c1
SS
12832 else if (!VEC_empty (char_ptr, utp->actions)
12833 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
12834 warning (_("Uploaded tracepoint %d actions "
12835 "have no source form, ignoring them"),
409873ef 12836 utp->number);
00bf0b85
SS
12837
12838 return tp;
d9b3f62e 12839}
00bf0b85 12840
1042e4c0
SS
12841/* Print information on tracepoint number TPNUM_EXP, or all if
12842 omitted. */
12843
12844static void
e5a67952 12845tracepoints_info (char *args, int from_tty)
1042e4c0 12846{
79a45e25 12847 struct ui_out *uiout = current_uiout;
e5a67952 12848 int num_printed;
1042e4c0 12849
e5a67952 12850 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
12851
12852 if (num_printed == 0)
1042e4c0 12853 {
e5a67952 12854 if (args == NULL || *args == '\0')
d77f58be
SS
12855 ui_out_message (uiout, 0, "No tracepoints.\n");
12856 else
e5a67952 12857 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 12858 }
ad443146
SS
12859
12860 default_collect_info ();
1042e4c0
SS
12861}
12862
4a64f543 12863/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
12864 Not supported by all targets. */
12865static void
12866enable_trace_command (char *args, int from_tty)
12867{
12868 enable_command (args, from_tty);
12869}
12870
4a64f543 12871/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
12872 Not supported by all targets. */
12873static void
12874disable_trace_command (char *args, int from_tty)
12875{
12876 disable_command (args, from_tty);
12877}
12878
4a64f543 12879/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
12880static void
12881delete_trace_command (char *arg, int from_tty)
12882{
35df4500 12883 struct breakpoint *b, *b_tmp;
1042e4c0
SS
12884
12885 dont_repeat ();
12886
12887 if (arg == 0)
12888 {
12889 int breaks_to_delete = 0;
12890
12891 /* Delete all breakpoints if no argument.
12892 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
12893 have to be deleted with an explicit breakpoint number
12894 argument. */
1042e4c0 12895 ALL_TRACEPOINTS (b)
46c6471b 12896 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
12897 {
12898 breaks_to_delete = 1;
12899 break;
12900 }
1042e4c0
SS
12901
12902 /* Ask user only if there are some breakpoints to delete. */
12903 if (!from_tty
12904 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12905 {
35df4500 12906 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 12907 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 12908 delete_breakpoint (b);
1042e4c0
SS
12909 }
12910 }
12911 else
51be5b68 12912 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
12913}
12914
197f0a60
TT
12915/* Helper function for trace_pass_command. */
12916
12917static void
d9b3f62e 12918trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 12919{
d9b3f62e
PA
12920 tp->pass_count = count;
12921 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
12922 if (from_tty)
12923 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 12924 tp->base.number, count);
197f0a60
TT
12925}
12926
1042e4c0
SS
12927/* Set passcount for tracepoint.
12928
12929 First command argument is passcount, second is tracepoint number.
12930 If tracepoint number omitted, apply to most recently defined.
12931 Also accepts special argument "all". */
12932
12933static void
12934trace_pass_command (char *args, int from_tty)
12935{
d9b3f62e 12936 struct tracepoint *t1;
1042e4c0 12937 unsigned int count;
1042e4c0
SS
12938
12939 if (args == 0 || *args == 0)
3e43a32a
MS
12940 error (_("passcount command requires an "
12941 "argument (count + optional TP num)"));
1042e4c0 12942
4a64f543 12943 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
12944
12945 while (*args && isspace ((int) *args))
12946 args++;
12947
12948 if (*args && strncasecmp (args, "all", 3) == 0)
12949 {
d9b3f62e
PA
12950 struct breakpoint *b;
12951
1042e4c0 12952 args += 3; /* Skip special argument "all". */
1042e4c0
SS
12953 if (*args)
12954 error (_("Junk at end of arguments."));
1042e4c0 12955
d9b3f62e 12956 ALL_TRACEPOINTS (b)
197f0a60 12957 {
d9b3f62e 12958 t1 = (struct tracepoint *) b;
197f0a60
TT
12959 trace_pass_set_count (t1, count, from_tty);
12960 }
12961 }
12962 else if (*args == '\0')
1042e4c0 12963 {
197f0a60 12964 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 12965 if (t1)
197f0a60
TT
12966 trace_pass_set_count (t1, count, from_tty);
12967 }
12968 else
12969 {
12970 struct get_number_or_range_state state;
12971
12972 init_number_or_range (&state, args);
12973 while (!state.finished)
1042e4c0 12974 {
197f0a60
TT
12975 t1 = get_tracepoint_by_number (&args, &state, 1);
12976 if (t1)
12977 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
12978 }
12979 }
1042e4c0
SS
12980}
12981
d9b3f62e 12982struct tracepoint *
1042e4c0
SS
12983get_tracepoint (int num)
12984{
12985 struct breakpoint *t;
12986
12987 ALL_TRACEPOINTS (t)
12988 if (t->number == num)
d9b3f62e 12989 return (struct tracepoint *) t;
1042e4c0
SS
12990
12991 return NULL;
12992}
12993
d5551862
SS
12994/* Find the tracepoint with the given target-side number (which may be
12995 different from the tracepoint number after disconnecting and
12996 reconnecting). */
12997
d9b3f62e 12998struct tracepoint *
d5551862
SS
12999get_tracepoint_by_number_on_target (int num)
13000{
d9b3f62e 13001 struct breakpoint *b;
d5551862 13002
d9b3f62e
PA
13003 ALL_TRACEPOINTS (b)
13004 {
13005 struct tracepoint *t = (struct tracepoint *) b;
13006
13007 if (t->number_on_target == num)
13008 return t;
13009 }
d5551862
SS
13010
13011 return NULL;
13012}
13013
1042e4c0 13014/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
13015 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13016 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 13017 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 13018struct tracepoint *
197f0a60
TT
13019get_tracepoint_by_number (char **arg,
13020 struct get_number_or_range_state *state,
13021 int optional_p)
1042e4c0
SS
13022{
13023 extern int tracepoint_count;
13024 struct breakpoint *t;
13025 int tpnum;
13026 char *instring = arg == NULL ? NULL : *arg;
13027
197f0a60
TT
13028 if (state)
13029 {
13030 gdb_assert (!state->finished);
13031 tpnum = get_number_or_range (state);
13032 }
13033 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
13034 {
13035 if (optional_p)
13036 tpnum = tracepoint_count;
13037 else
13038 error_no_arg (_("tracepoint number"));
13039 }
13040 else
197f0a60 13041 tpnum = get_number (arg);
1042e4c0
SS
13042
13043 if (tpnum <= 0)
13044 {
13045 if (instring && *instring)
13046 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
13047 instring);
13048 else
3e43a32a
MS
13049 printf_filtered (_("Tracepoint argument missing "
13050 "and no previous tracepoint\n"));
1042e4c0
SS
13051 return NULL;
13052 }
13053
13054 ALL_TRACEPOINTS (t)
13055 if (t->number == tpnum)
13056 {
d9b3f62e 13057 return (struct tracepoint *) t;
1042e4c0
SS
13058 }
13059
1042e4c0
SS
13060 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13061 return NULL;
13062}
13063
d9b3f62e
PA
13064void
13065print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13066{
13067 if (b->thread != -1)
13068 fprintf_unfiltered (fp, " thread %d", b->thread);
13069
13070 if (b->task != 0)
13071 fprintf_unfiltered (fp, " task %d", b->task);
13072
13073 fprintf_unfiltered (fp, "\n");
13074}
13075
6149aea9
PA
13076/* Save information on user settable breakpoints (watchpoints, etc) to
13077 a new script file named FILENAME. If FILTER is non-NULL, call it
13078 on each breakpoint and only include the ones for which it returns
13079 non-zero. */
13080
1042e4c0 13081static void
6149aea9
PA
13082save_breakpoints (char *filename, int from_tty,
13083 int (*filter) (const struct breakpoint *))
1042e4c0
SS
13084{
13085 struct breakpoint *tp;
6149aea9 13086 int any = 0;
a7bdde9e 13087 char *pathname;
1042e4c0 13088 struct cleanup *cleanup;
a7bdde9e 13089 struct ui_file *fp;
6149aea9 13090 int extra_trace_bits = 0;
1042e4c0 13091
6149aea9
PA
13092 if (filename == 0 || *filename == 0)
13093 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
13094
13095 /* See if we have anything to save. */
6149aea9 13096 ALL_BREAKPOINTS (tp)
1042e4c0 13097 {
6149aea9 13098 /* Skip internal and momentary breakpoints. */
09d682a4 13099 if (!user_breakpoint_p (tp))
6149aea9
PA
13100 continue;
13101
13102 /* If we have a filter, only save the breakpoints it accepts. */
13103 if (filter && !filter (tp))
13104 continue;
13105
13106 any = 1;
13107
13108 if (is_tracepoint (tp))
13109 {
13110 extra_trace_bits = 1;
13111
13112 /* We can stop searching. */
13113 break;
13114 }
1042e4c0 13115 }
6149aea9
PA
13116
13117 if (!any)
1042e4c0 13118 {
6149aea9 13119 warning (_("Nothing to save."));
1042e4c0
SS
13120 return;
13121 }
13122
6149aea9 13123 pathname = tilde_expand (filename);
1042e4c0 13124 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 13125 fp = gdb_fopen (pathname, "w");
059fb39f 13126 if (!fp)
6149aea9
PA
13127 error (_("Unable to open file '%s' for saving (%s)"),
13128 filename, safe_strerror (errno));
a7bdde9e 13129 make_cleanup_ui_file_delete (fp);
8bf6485c 13130
6149aea9
PA
13131 if (extra_trace_bits)
13132 save_trace_state_variables (fp);
8bf6485c 13133
6149aea9 13134 ALL_BREAKPOINTS (tp)
1042e4c0 13135 {
6149aea9 13136 /* Skip internal and momentary breakpoints. */
09d682a4 13137 if (!user_breakpoint_p (tp))
6149aea9 13138 continue;
8bf6485c 13139
6149aea9
PA
13140 /* If we have a filter, only save the breakpoints it accepts. */
13141 if (filter && !filter (tp))
13142 continue;
13143
348d480f 13144 tp->ops->print_recreate (tp, fp);
1042e4c0 13145
6149aea9
PA
13146 /* Note, we can't rely on tp->number for anything, as we can't
13147 assume the recreated breakpoint numbers will match. Use $bpnum
13148 instead. */
13149
13150 if (tp->cond_string)
13151 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13152
13153 if (tp->ignore_count)
13154 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13155
a7bdde9e 13156 if (tp->commands)
1042e4c0 13157 {
a7bdde9e
VP
13158 volatile struct gdb_exception ex;
13159
6149aea9 13160 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 13161
79a45e25 13162 ui_out_redirect (current_uiout, fp);
14dba4b4 13163 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 13164 {
79a45e25 13165 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 13166 }
79a45e25 13167 ui_out_redirect (current_uiout, NULL);
1042e4c0 13168
a7bdde9e
VP
13169 if (ex.reason < 0)
13170 throw_exception (ex);
1042e4c0 13171
a7bdde9e 13172 fprintf_unfiltered (fp, " end\n");
1042e4c0 13173 }
6149aea9
PA
13174
13175 if (tp->enable_state == bp_disabled)
13176 fprintf_unfiltered (fp, "disable\n");
13177
13178 /* If this is a multi-location breakpoint, check if the locations
13179 should be individually disabled. Watchpoint locations are
13180 special, and not user visible. */
13181 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13182 {
13183 struct bp_location *loc;
13184 int n = 1;
13185
13186 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13187 if (!loc->enabled)
13188 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13189 }
1042e4c0 13190 }
8bf6485c 13191
6149aea9 13192 if (extra_trace_bits && *default_collect)
8bf6485c
SS
13193 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13194
1042e4c0
SS
13195 do_cleanups (cleanup);
13196 if (from_tty)
6149aea9
PA
13197 printf_filtered (_("Saved to file '%s'.\n"), filename);
13198}
13199
13200/* The `save breakpoints' command. */
13201
13202static void
13203save_breakpoints_command (char *args, int from_tty)
13204{
13205 save_breakpoints (args, from_tty, NULL);
13206}
13207
13208/* The `save tracepoints' command. */
13209
13210static void
13211save_tracepoints_command (char *args, int from_tty)
13212{
13213 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
13214}
13215
13216/* Create a vector of all tracepoints. */
13217
13218VEC(breakpoint_p) *
eeae04df 13219all_tracepoints (void)
1042e4c0
SS
13220{
13221 VEC(breakpoint_p) *tp_vec = 0;
13222 struct breakpoint *tp;
13223
13224 ALL_TRACEPOINTS (tp)
13225 {
13226 VEC_safe_push (breakpoint_p, tp_vec, tp);
13227 }
13228
13229 return tp_vec;
13230}
13231
c906108c 13232\f
4a64f543
MS
13233/* This help string is used for the break, hbreak, tbreak and thbreak
13234 commands. It is defined as a macro to prevent duplication.
13235 COMMAND should be a string constant containing the name of the
13236 command. */
31e2b00f
AS
13237#define BREAK_ARGS_HELP(command) \
13238command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13239LOCATION may be a line number, function name, or \"*\" and an address.\n\
13240If a line number is specified, break at start of code for that line.\n\
13241If a function is specified, break at start of code for that function.\n\
13242If an address is specified, break at that exact address.\n\
dc10affe
PA
13243With no LOCATION, uses current execution address of the selected\n\
13244stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
13245\n\
13246THREADNUM is the number from \"info threads\".\n\
13247CONDITION is a boolean expression.\n\
13248\n\
d41c0fc8
PA
13249Multiple breakpoints at one place are permitted, and useful if their\n\
13250conditions are different.\n\
31e2b00f
AS
13251\n\
13252Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13253
44feb3ce
TT
13254/* List of subcommands for "catch". */
13255static struct cmd_list_element *catch_cmdlist;
13256
13257/* List of subcommands for "tcatch". */
13258static struct cmd_list_element *tcatch_cmdlist;
13259
9ac4176b 13260void
44feb3ce
TT
13261add_catch_command (char *name, char *docstring,
13262 void (*sfunc) (char *args, int from_tty,
13263 struct cmd_list_element *command),
a96d9b2e
SDJ
13264 char **(*completer) (struct cmd_list_element *cmd,
13265 char *text, char *word),
44feb3ce
TT
13266 void *user_data_catch,
13267 void *user_data_tcatch)
13268{
13269 struct cmd_list_element *command;
13270
13271 command = add_cmd (name, class_breakpoint, NULL, docstring,
13272 &catch_cmdlist);
13273 set_cmd_sfunc (command, sfunc);
13274 set_cmd_context (command, user_data_catch);
a96d9b2e 13275 set_cmd_completer (command, completer);
44feb3ce
TT
13276
13277 command = add_cmd (name, class_breakpoint, NULL, docstring,
13278 &tcatch_cmdlist);
13279 set_cmd_sfunc (command, sfunc);
13280 set_cmd_context (command, user_data_tcatch);
a96d9b2e 13281 set_cmd_completer (command, completer);
44feb3ce
TT
13282}
13283
6c95b8df 13284static void
a79b8f6e 13285clear_syscall_counts (struct inferior *inf)
6c95b8df 13286{
6c95b8df
PA
13287 inf->total_syscalls_count = 0;
13288 inf->any_syscall_count = 0;
13289 VEC_free (int, inf->syscalls_counts);
13290}
13291
6149aea9
PA
13292static void
13293save_command (char *arg, int from_tty)
13294{
3e43a32a
MS
13295 printf_unfiltered (_("\"save\" must be followed by "
13296 "the name of a save subcommand.\n"));
6149aea9
PA
13297 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13298}
13299
84f4c1fe
PM
13300struct breakpoint *
13301iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13302 void *data)
13303{
35df4500 13304 struct breakpoint *b, *b_tmp;
84f4c1fe 13305
35df4500 13306 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
13307 {
13308 if ((*callback) (b, data))
13309 return b;
13310 }
13311
13312 return NULL;
13313}
13314
0574c78f
GB
13315/* Zero if any of the breakpoint's locations could be a location where
13316 functions have been inlined, nonzero otherwise. */
13317
13318static int
13319is_non_inline_function (struct breakpoint *b)
13320{
13321 /* The shared library event breakpoint is set on the address of a
13322 non-inline function. */
13323 if (b->type == bp_shlib_event)
13324 return 1;
13325
13326 return 0;
13327}
13328
13329/* Nonzero if the specified PC cannot be a location where functions
13330 have been inlined. */
13331
13332int
13333pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc)
13334{
13335 struct breakpoint *b;
13336 struct bp_location *bl;
13337
13338 ALL_BREAKPOINTS (b)
13339 {
13340 if (!is_non_inline_function (b))
13341 continue;
13342
13343 for (bl = b->loc; bl != NULL; bl = bl->next)
13344 {
13345 if (!bl->shlib_disabled
13346 && bpstat_check_location (bl, aspace, pc))
13347 return 1;
13348 }
13349 }
13350
13351 return 0;
13352}
13353
2060206e
PA
13354void
13355initialize_breakpoint_ops (void)
13356{
13357 static int initialized = 0;
13358
13359 struct breakpoint_ops *ops;
13360
13361 if (initialized)
13362 return;
13363 initialized = 1;
13364
13365 /* The breakpoint_ops structure to be inherit by all kinds of
13366 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13367 internal and momentary breakpoints, etc.). */
13368 ops = &bkpt_base_breakpoint_ops;
13369 *ops = base_breakpoint_ops;
13370 ops->re_set = bkpt_re_set;
13371 ops->insert_location = bkpt_insert_location;
13372 ops->remove_location = bkpt_remove_location;
13373 ops->breakpoint_hit = bkpt_breakpoint_hit;
13374
13375 /* The breakpoint_ops structure to be used in regular breakpoints. */
13376 ops = &bkpt_breakpoint_ops;
13377 *ops = bkpt_base_breakpoint_ops;
13378 ops->re_set = bkpt_re_set;
13379 ops->resources_needed = bkpt_resources_needed;
13380 ops->print_it = bkpt_print_it;
13381 ops->print_mention = bkpt_print_mention;
13382 ops->print_recreate = bkpt_print_recreate;
13383
13384 /* Ranged breakpoints. */
13385 ops = &ranged_breakpoint_ops;
13386 *ops = bkpt_breakpoint_ops;
13387 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13388 ops->resources_needed = resources_needed_ranged_breakpoint;
13389 ops->print_it = print_it_ranged_breakpoint;
13390 ops->print_one = print_one_ranged_breakpoint;
13391 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13392 ops->print_mention = print_mention_ranged_breakpoint;
13393 ops->print_recreate = print_recreate_ranged_breakpoint;
13394
13395 /* Internal breakpoints. */
13396 ops = &internal_breakpoint_ops;
13397 *ops = bkpt_base_breakpoint_ops;
13398 ops->re_set = internal_bkpt_re_set;
13399 ops->check_status = internal_bkpt_check_status;
13400 ops->print_it = internal_bkpt_print_it;
13401 ops->print_mention = internal_bkpt_print_mention;
13402
13403 /* Momentary breakpoints. */
13404 ops = &momentary_breakpoint_ops;
13405 *ops = bkpt_base_breakpoint_ops;
13406 ops->re_set = momentary_bkpt_re_set;
13407 ops->check_status = momentary_bkpt_check_status;
13408 ops->print_it = momentary_bkpt_print_it;
13409 ops->print_mention = momentary_bkpt_print_mention;
13410
13411 /* GNU v3 exception catchpoints. */
13412 ops = &gnu_v3_exception_catchpoint_ops;
13413 *ops = bkpt_breakpoint_ops;
13414 ops->print_it = print_it_exception_catchpoint;
13415 ops->print_one = print_one_exception_catchpoint;
13416 ops->print_mention = print_mention_exception_catchpoint;
13417 ops->print_recreate = print_recreate_exception_catchpoint;
13418
13419 /* Watchpoints. */
13420 ops = &watchpoint_breakpoint_ops;
13421 *ops = base_breakpoint_ops;
3a5c3e22 13422 ops->dtor = dtor_watchpoint;
2060206e
PA
13423 ops->re_set = re_set_watchpoint;
13424 ops->insert_location = insert_watchpoint;
13425 ops->remove_location = remove_watchpoint;
13426 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13427 ops->check_status = check_status_watchpoint;
13428 ops->resources_needed = resources_needed_watchpoint;
13429 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13430 ops->print_it = print_it_watchpoint;
13431 ops->print_mention = print_mention_watchpoint;
13432 ops->print_recreate = print_recreate_watchpoint;
13433
13434 /* Masked watchpoints. */
13435 ops = &masked_watchpoint_breakpoint_ops;
13436 *ops = watchpoint_breakpoint_ops;
13437 ops->insert_location = insert_masked_watchpoint;
13438 ops->remove_location = remove_masked_watchpoint;
13439 ops->resources_needed = resources_needed_masked_watchpoint;
13440 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13441 ops->print_it = print_it_masked_watchpoint;
13442 ops->print_one_detail = print_one_detail_masked_watchpoint;
13443 ops->print_mention = print_mention_masked_watchpoint;
13444 ops->print_recreate = print_recreate_masked_watchpoint;
13445
13446 /* Tracepoints. */
13447 ops = &tracepoint_breakpoint_ops;
13448 *ops = base_breakpoint_ops;
13449 ops->re_set = tracepoint_re_set;
13450 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13451 ops->print_one_detail = tracepoint_print_one_detail;
13452 ops->print_mention = tracepoint_print_mention;
13453 ops->print_recreate = tracepoint_print_recreate;
13454
13455 /* Fork catchpoints. */
13456 ops = &catch_fork_breakpoint_ops;
13457 *ops = base_breakpoint_ops;
13458 ops->insert_location = insert_catch_fork;
13459 ops->remove_location = remove_catch_fork;
13460 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13461 ops->print_it = print_it_catch_fork;
13462 ops->print_one = print_one_catch_fork;
13463 ops->print_mention = print_mention_catch_fork;
13464 ops->print_recreate = print_recreate_catch_fork;
13465
13466 /* Vfork catchpoints. */
13467 ops = &catch_vfork_breakpoint_ops;
13468 *ops = base_breakpoint_ops;
13469 ops->insert_location = insert_catch_vfork;
13470 ops->remove_location = remove_catch_vfork;
13471 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13472 ops->print_it = print_it_catch_vfork;
13473 ops->print_one = print_one_catch_vfork;
13474 ops->print_mention = print_mention_catch_vfork;
13475 ops->print_recreate = print_recreate_catch_vfork;
13476
13477 /* Exec catchpoints. */
13478 ops = &catch_exec_breakpoint_ops;
13479 *ops = base_breakpoint_ops;
13480 ops->dtor = dtor_catch_exec;
13481 ops->insert_location = insert_catch_exec;
13482 ops->remove_location = remove_catch_exec;
13483 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13484 ops->print_it = print_it_catch_exec;
13485 ops->print_one = print_one_catch_exec;
13486 ops->print_mention = print_mention_catch_exec;
13487 ops->print_recreate = print_recreate_catch_exec;
13488
13489 /* Syscall catchpoints. */
13490 ops = &catch_syscall_breakpoint_ops;
13491 *ops = base_breakpoint_ops;
13492 ops->dtor = dtor_catch_syscall;
13493 ops->insert_location = insert_catch_syscall;
13494 ops->remove_location = remove_catch_syscall;
13495 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13496 ops->print_it = print_it_catch_syscall;
13497 ops->print_one = print_one_catch_syscall;
13498 ops->print_mention = print_mention_catch_syscall;
13499 ops->print_recreate = print_recreate_catch_syscall;
13500}
13501
c906108c 13502void
fba45db2 13503_initialize_breakpoint (void)
c906108c
SS
13504{
13505 struct cmd_list_element *c;
13506
2060206e
PA
13507 initialize_breakpoint_ops ();
13508
84acb35a 13509 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 13510 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 13511 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 13512
17450429
PP
13513 breakpoint_objfile_key = register_objfile_data ();
13514
c906108c
SS
13515 breakpoint_chain = 0;
13516 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13517 before a breakpoint is set. */
13518 breakpoint_count = 0;
13519
1042e4c0
SS
13520 tracepoint_count = 0;
13521
1bedd215
AC
13522 add_com ("ignore", class_breakpoint, ignore_command, _("\
13523Set ignore-count of breakpoint number N to COUNT.\n\
13524Usage is `ignore N COUNT'."));
c906108c 13525 if (xdb_commands)
c5aa993b 13526 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 13527
1bedd215
AC
13528 add_com ("commands", class_breakpoint, commands_command, _("\
13529Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
13530Give breakpoint number as argument after \"commands\".\n\
13531With no argument, the targeted breakpoint is the last one set.\n\
13532The commands themselves follow starting on the next line.\n\
13533Type a line containing \"end\" to indicate the end of them.\n\
13534Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 13535then no output is printed when it is hit, except what the commands print."));
c906108c 13536
1bedd215
AC
13537 add_com ("condition", class_breakpoint, condition_command, _("\
13538Specify breakpoint number N to break only if COND is true.\n\
c906108c 13539Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 13540expression to be evaluated whenever breakpoint N is reached."));
c906108c 13541
1bedd215 13542 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 13543Set a temporary breakpoint.\n\
c906108c
SS
13544Like \"break\" except the breakpoint is only temporary,\n\
13545so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
13546by using \"enable delete\" on the breakpoint number.\n\
13547\n"
13548BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 13549 set_cmd_completer (c, location_completer);
c94fdfd0 13550
1bedd215 13551 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 13552Set a hardware assisted breakpoint.\n\
c906108c 13553Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
13554some target hardware may not have this support.\n\
13555\n"
13556BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 13557 set_cmd_completer (c, location_completer);
c906108c 13558
1bedd215 13559 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 13560Set a temporary hardware assisted breakpoint.\n\
c906108c 13561Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
13562so it will be deleted when hit.\n\
13563\n"
13564BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 13565 set_cmd_completer (c, location_completer);
c906108c 13566
1bedd215
AC
13567 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
13568Enable some breakpoints.\n\
c906108c
SS
13569Give breakpoint numbers (separated by spaces) as arguments.\n\
13570With no subcommand, breakpoints are enabled until you command otherwise.\n\
13571This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13572With a subcommand you can enable temporarily."),
c906108c
SS
13573 &enablelist, "enable ", 1, &cmdlist);
13574 if (xdb_commands)
1bedd215
AC
13575 add_com ("ab", class_breakpoint, enable_command, _("\
13576Enable some breakpoints.\n\
c906108c
SS
13577Give breakpoint numbers (separated by spaces) as arguments.\n\
13578With no subcommand, breakpoints are enabled until you command otherwise.\n\
13579This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13580With a subcommand you can enable temporarily."));
c906108c
SS
13581
13582 add_com_alias ("en", "enable", class_breakpoint, 1);
13583
84951ab5 13584 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 13585Enable some breakpoints.\n\
c906108c
SS
13586Give breakpoint numbers (separated by spaces) as arguments.\n\
13587This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 13588May be abbreviated to simply \"enable\".\n"),
c5aa993b 13589 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 13590
1a966eab
AC
13591 add_cmd ("once", no_class, enable_once_command, _("\
13592Enable breakpoints for one hit. Give breakpoint numbers.\n\
13593If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13594 &enablebreaklist);
13595
1a966eab
AC
13596 add_cmd ("delete", no_class, enable_delete_command, _("\
13597Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13598If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13599 &enablebreaklist);
13600
1a966eab
AC
13601 add_cmd ("delete", no_class, enable_delete_command, _("\
13602Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13603If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
13604 &enablelist);
13605
1a966eab
AC
13606 add_cmd ("once", no_class, enable_once_command, _("\
13607Enable breakpoints for one hit. Give breakpoint numbers.\n\
13608If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
13609 &enablelist);
13610
1bedd215
AC
13611 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13612Disable some breakpoints.\n\
c906108c
SS
13613Arguments are breakpoint numbers with spaces in between.\n\
13614To disable all breakpoints, give no argument.\n\
64b9b334 13615A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
13616 &disablelist, "disable ", 1, &cmdlist);
13617 add_com_alias ("dis", "disable", class_breakpoint, 1);
13618 add_com_alias ("disa", "disable", class_breakpoint, 1);
13619 if (xdb_commands)
1bedd215
AC
13620 add_com ("sb", class_breakpoint, disable_command, _("\
13621Disable some breakpoints.\n\
c906108c
SS
13622Arguments are breakpoint numbers with spaces in between.\n\
13623To disable all breakpoints, give no argument.\n\
64b9b334 13624A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 13625
1a966eab
AC
13626 add_cmd ("breakpoints", class_alias, disable_command, _("\
13627Disable some breakpoints.\n\
c906108c
SS
13628Arguments are breakpoint numbers with spaces in between.\n\
13629To disable all breakpoints, give no argument.\n\
64b9b334 13630A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 13631This command may be abbreviated \"disable\"."),
c906108c
SS
13632 &disablelist);
13633
1bedd215
AC
13634 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13635Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13636Arguments are breakpoint numbers with spaces in between.\n\
13637To delete all breakpoints, give no argument.\n\
13638\n\
13639Also a prefix command for deletion of other GDB objects.\n\
1bedd215 13640The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
13641 &deletelist, "delete ", 1, &cmdlist);
13642 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 13643 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 13644 if (xdb_commands)
1bedd215
AC
13645 add_com ("db", class_breakpoint, delete_command, _("\
13646Delete some breakpoints.\n\
c906108c 13647Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 13648To delete all breakpoints, give no argument.\n"));
c906108c 13649
1a966eab
AC
13650 add_cmd ("breakpoints", class_alias, delete_command, _("\
13651Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
13652Arguments are breakpoint numbers with spaces in between.\n\
13653To delete all breakpoints, give no argument.\n\
1a966eab 13654This command may be abbreviated \"delete\"."),
c906108c
SS
13655 &deletelist);
13656
1bedd215
AC
13657 add_com ("clear", class_breakpoint, clear_command, _("\
13658Clear breakpoint at specified line or function.\n\
c906108c
SS
13659Argument may be line number, function name, or \"*\" and an address.\n\
13660If line number is specified, all breakpoints in that line are cleared.\n\
13661If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
13662If an address is specified, breakpoints at that address are cleared.\n\
13663\n\
13664With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
13665is executing in.\n\
13666\n\
1bedd215 13667See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 13668 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 13669
1bedd215 13670 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
13671Set breakpoint at specified line or function.\n"
13672BREAK_ARGS_HELP ("break")));
5ba2abeb 13673 set_cmd_completer (c, location_completer);
c94fdfd0 13674
c906108c
SS
13675 add_com_alias ("b", "break", class_run, 1);
13676 add_com_alias ("br", "break", class_run, 1);
13677 add_com_alias ("bre", "break", class_run, 1);
13678 add_com_alias ("brea", "break", class_run, 1);
13679
7681d515
PM
13680 if (xdb_commands)
13681 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
13682
13683 if (dbx_commands)
13684 {
1bedd215
AC
13685 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13686Break in function/address or break at a line in the current file."),
c5aa993b
JM
13687 &stoplist, "stop ", 1, &cmdlist);
13688 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 13689 _("Break in function or address."), &stoplist);
c5aa993b 13690 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 13691 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
13692 add_com ("status", class_info, breakpoints_info, _("\
13693Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13694The \"Type\" column indicates one of:\n\
13695\tbreakpoint - normal breakpoint\n\
13696\twatchpoint - watchpoint\n\
13697The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13698the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13699breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13700address and file/line number respectively.\n\
13701\n\
13702Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13703are set to the address of the last breakpoint listed unless the command\n\
13704is prefixed with \"server \".\n\n\
c906108c 13705Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13706breakpoint set."));
c906108c
SS
13707 }
13708
1bedd215 13709 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 13710Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
13711The \"Type\" column indicates one of:\n\
13712\tbreakpoint - normal breakpoint\n\
13713\twatchpoint - watchpoint\n\
13714The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13715the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13716breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13717address and file/line number respectively.\n\
13718\n\
13719Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13720are set to the address of the last breakpoint listed unless the command\n\
13721is prefixed with \"server \".\n\n\
c906108c 13722Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13723breakpoint set."));
c906108c 13724
6b04bdb7
MS
13725 add_info_alias ("b", "breakpoints", 1);
13726
c906108c 13727 if (xdb_commands)
1bedd215
AC
13728 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13729Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13730The \"Type\" column indicates one of:\n\
13731\tbreakpoint - normal breakpoint\n\
13732\twatchpoint - watchpoint\n\
13733The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13734the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13735breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
13736address and file/line number respectively.\n\
13737\n\
13738Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13739are set to the address of the last breakpoint listed unless the command\n\
13740is prefixed with \"server \".\n\n\
c906108c 13741Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 13742breakpoint set."));
c906108c 13743
1a966eab
AC
13744 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13745Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
13746The \"Type\" column indicates one of:\n\
13747\tbreakpoint - normal breakpoint\n\
13748\twatchpoint - watchpoint\n\
13749\tlongjmp - internal breakpoint used to step through longjmp()\n\
13750\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13751\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
13752\tfinish - internal breakpoint used by the \"finish\" command\n\
13753The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
13754the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13755breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
13756address and file/line number respectively.\n\
13757\n\
13758Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
13759are set to the address of the last breakpoint listed unless the command\n\
13760is prefixed with \"server \".\n\n\
c906108c 13761Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 13762breakpoint set."),
c906108c
SS
13763 &maintenanceinfolist);
13764
44feb3ce
TT
13765 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13766Set catchpoints to catch events."),
13767 &catch_cmdlist, "catch ",
13768 0/*allow-unknown*/, &cmdlist);
13769
13770 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13771Set temporary catchpoints to catch events."),
13772 &tcatch_cmdlist, "tcatch ",
13773 0/*allow-unknown*/, &cmdlist);
13774
13775 /* Add catch and tcatch sub-commands. */
13776 add_catch_command ("catch", _("\
13777Catch an exception, when caught.\n\
13778With an argument, catch only exceptions with the given name."),
13779 catch_catch_command,
a96d9b2e 13780 NULL,
44feb3ce
TT
13781 CATCH_PERMANENT,
13782 CATCH_TEMPORARY);
13783 add_catch_command ("throw", _("\
13784Catch an exception, when thrown.\n\
13785With an argument, catch only exceptions with the given name."),
13786 catch_throw_command,
a96d9b2e 13787 NULL,
44feb3ce
TT
13788 CATCH_PERMANENT,
13789 CATCH_TEMPORARY);
13790 add_catch_command ("fork", _("Catch calls to fork."),
13791 catch_fork_command_1,
a96d9b2e 13792 NULL,
44feb3ce
TT
13793 (void *) (uintptr_t) catch_fork_permanent,
13794 (void *) (uintptr_t) catch_fork_temporary);
13795 add_catch_command ("vfork", _("Catch calls to vfork."),
13796 catch_fork_command_1,
a96d9b2e 13797 NULL,
44feb3ce
TT
13798 (void *) (uintptr_t) catch_vfork_permanent,
13799 (void *) (uintptr_t) catch_vfork_temporary);
13800 add_catch_command ("exec", _("Catch calls to exec."),
13801 catch_exec_command_1,
a96d9b2e
SDJ
13802 NULL,
13803 CATCH_PERMANENT,
13804 CATCH_TEMPORARY);
13805 add_catch_command ("syscall", _("\
13806Catch system calls by their names and/or numbers.\n\
13807Arguments say which system calls to catch. If no arguments\n\
13808are given, every system call will be caught.\n\
13809Arguments, if given, should be one or more system call names\n\
13810(if your system supports that), or system call numbers."),
13811 catch_syscall_command_1,
13812 catch_syscall_completer,
44feb3ce
TT
13813 CATCH_PERMANENT,
13814 CATCH_TEMPORARY);
c5aa993b 13815
1bedd215
AC
13816 c = add_com ("watch", class_breakpoint, watch_command, _("\
13817Set a watchpoint for an expression.\n\
06a64a0b 13818Usage: watch [-l|-location] EXPRESSION\n\
c906108c 13819A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13820an expression changes.\n\
13821If -l or -location is given, this evaluates EXPRESSION and watches\n\
13822the memory to which it refers."));
65d12d83 13823 set_cmd_completer (c, expression_completer);
c906108c 13824
1bedd215
AC
13825 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13826Set a read watchpoint for an expression.\n\
06a64a0b 13827Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 13828A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13829an expression is read.\n\
13830If -l or -location is given, this evaluates EXPRESSION and watches\n\
13831the memory to which it refers."));
65d12d83 13832 set_cmd_completer (c, expression_completer);
c906108c 13833
1bedd215
AC
13834 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13835Set a watchpoint for an expression.\n\
06a64a0b 13836Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 13837A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
13838an expression is either read or written.\n\
13839If -l or -location is given, this evaluates EXPRESSION and watches\n\
13840the memory to which it refers."));
65d12d83 13841 set_cmd_completer (c, expression_completer);
c906108c 13842
d77f58be 13843 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 13844Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 13845
920d2a44
AC
13846 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13847 respond to changes - contrary to the description. */
85c07804
AC
13848 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13849 &can_use_hw_watchpoints, _("\
13850Set debugger's willingness to use watchpoint hardware."), _("\
13851Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
13852If zero, gdb will not use hardware for new watchpoints, even if\n\
13853such is available. (However, any hardware watchpoints that were\n\
13854created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
13855hardware.)"),
13856 NULL,
920d2a44 13857 show_can_use_hw_watchpoints,
85c07804 13858 &setlist, &showlist);
c906108c
SS
13859
13860 can_use_hw_watchpoints = 1;
fa8d40ab 13861
1042e4c0
SS
13862 /* Tracepoint manipulation commands. */
13863
13864 c = add_com ("trace", class_breakpoint, trace_command, _("\
13865Set a tracepoint at specified line or function.\n\
13866\n"
13867BREAK_ARGS_HELP ("trace") "\n\
13868Do \"help tracepoints\" for info on other tracepoint commands."));
13869 set_cmd_completer (c, location_completer);
13870
13871 add_com_alias ("tp", "trace", class_alias, 0);
13872 add_com_alias ("tr", "trace", class_alias, 1);
13873 add_com_alias ("tra", "trace", class_alias, 1);
13874 add_com_alias ("trac", "trace", class_alias, 1);
13875
7a697b8d
SS
13876 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13877Set a fast tracepoint at specified line or function.\n\
13878\n"
13879BREAK_ARGS_HELP ("ftrace") "\n\
13880Do \"help tracepoints\" for info on other tracepoint commands."));
13881 set_cmd_completer (c, location_completer);
13882
0fb4aa4b
PA
13883 c = add_com ("strace", class_breakpoint, strace_command, _("\
13884Set a static tracepoint at specified line, function or marker.\n\
13885\n\
13886strace [LOCATION] [if CONDITION]\n\
13887LOCATION may be a line number, function name, \"*\" and an address,\n\
13888or -m MARKER_ID.\n\
13889If a line number is specified, probe the marker at start of code\n\
13890for that line. If a function is specified, probe the marker at start\n\
13891of code for that function. If an address is specified, probe the marker\n\
13892at that exact address. If a marker id is specified, probe the marker\n\
13893with that name. With no LOCATION, uses current execution address of\n\
13894the selected stack frame.\n\
13895Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13896This collects arbitrary user data passed in the probe point call to the\n\
13897tracing library. You can inspect it when analyzing the trace buffer,\n\
13898by printing the $_sdata variable like any other convenience variable.\n\
13899\n\
13900CONDITION is a boolean expression.\n\
13901\n\
d41c0fc8
PA
13902Multiple tracepoints at one place are permitted, and useful if their\n\
13903conditions are different.\n\
0fb4aa4b
PA
13904\n\
13905Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13906Do \"help tracepoints\" for info on other tracepoint commands."));
13907 set_cmd_completer (c, location_completer);
13908
1042e4c0 13909 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 13910Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
13911Convenience variable \"$tpnum\" contains the number of the\n\
13912last tracepoint set."));
13913
13914 add_info_alias ("tp", "tracepoints", 1);
13915
13916 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13917Delete specified tracepoints.\n\
13918Arguments are tracepoint numbers, separated by spaces.\n\
13919No argument means delete all tracepoints."),
13920 &deletelist);
13921
13922 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13923Disable specified tracepoints.\n\
13924Arguments are tracepoint numbers, separated by spaces.\n\
13925No argument means disable all tracepoints."),
13926 &disablelist);
13927 deprecate_cmd (c, "disable");
13928
13929 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13930Enable specified tracepoints.\n\
13931Arguments are tracepoint numbers, separated by spaces.\n\
13932No argument means enable all tracepoints."),
13933 &enablelist);
13934 deprecate_cmd (c, "enable");
13935
13936 add_com ("passcount", class_trace, trace_pass_command, _("\
13937Set the passcount for a tracepoint.\n\
13938The trace will end when the tracepoint has been passed 'count' times.\n\
13939Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13940if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13941
6149aea9
PA
13942 add_prefix_cmd ("save", class_breakpoint, save_command,
13943 _("Save breakpoint definitions as a script."),
13944 &save_cmdlist, "save ",
13945 0/*allow-unknown*/, &cmdlist);
13946
13947 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13948Save current breakpoint definitions as a script.\n\
cce7e648 13949This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
13950catchpoints, tracepoints). Use the 'source' command in another debug\n\
13951session to restore them."),
13952 &save_cmdlist);
13953 set_cmd_completer (c, filename_completer);
13954
13955 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 13956Save current tracepoint definitions as a script.\n\
6149aea9
PA
13957Use the 'source' command in another debug session to restore them."),
13958 &save_cmdlist);
1042e4c0
SS
13959 set_cmd_completer (c, filename_completer);
13960
6149aea9
PA
13961 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13962 deprecate_cmd (c, "save tracepoints");
13963
1bedd215 13964 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
13965Breakpoint specific settings\n\
13966Configure various breakpoint-specific variables such as\n\
1bedd215 13967pending breakpoint behavior"),
fa8d40ab
JJ
13968 &breakpoint_set_cmdlist, "set breakpoint ",
13969 0/*allow-unknown*/, &setlist);
1bedd215 13970 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
13971Breakpoint specific settings\n\
13972Configure various breakpoint-specific variables such as\n\
1bedd215 13973pending breakpoint behavior"),
fa8d40ab
JJ
13974 &breakpoint_show_cmdlist, "show breakpoint ",
13975 0/*allow-unknown*/, &showlist);
13976
7915a72c
AC
13977 add_setshow_auto_boolean_cmd ("pending", no_class,
13978 &pending_break_support, _("\
13979Set debugger's behavior regarding pending breakpoints."), _("\
13980Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
13981If on, an unrecognized breakpoint location will cause gdb to create a\n\
13982pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13983an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 13984user-query to see if a pending breakpoint should be created."),
2c5b56ce 13985 NULL,
920d2a44 13986 show_pending_break_support,
6e1d7d6c
AC
13987 &breakpoint_set_cmdlist,
13988 &breakpoint_show_cmdlist);
fa8d40ab
JJ
13989
13990 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
13991
13992 add_setshow_boolean_cmd ("auto-hw", no_class,
13993 &automatic_hardware_breakpoints, _("\
13994Set automatic usage of hardware breakpoints."), _("\
13995Show automatic usage of hardware breakpoints."), _("\
13996If set, the debugger will automatically use hardware breakpoints for\n\
13997breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13998a warning will be emitted for such breakpoints."),
13999 NULL,
14000 show_automatic_hardware_breakpoints,
14001 &breakpoint_set_cmdlist,
14002 &breakpoint_show_cmdlist);
74960c60 14003
33e5cbd6
PA
14004 add_setshow_enum_cmd ("always-inserted", class_support,
14005 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
14006Set mode for inserting breakpoints."), _("\
14007Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
14008When this mode is off, breakpoints are inserted in inferior when it is\n\
14009resumed, and removed when execution stops. When this mode is on,\n\
14010breakpoints are inserted immediately and removed only when the user\n\
14011deletes the breakpoint. When this mode is auto (which is the default),\n\
14012the behaviour depends on the non-stop setting (see help set non-stop).\n\
14013In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
14014behaves as if always-inserted mode is on; if gdb is controlling the\n\
14015inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
14016 NULL,
14017 &show_always_inserted_mode,
14018 &breakpoint_set_cmdlist,
14019 &breakpoint_show_cmdlist);
f1310107
TJB
14020
14021 add_com ("break-range", class_breakpoint, break_range_command, _("\
14022Set a breakpoint for an address range.\n\
14023break-range START-LOCATION, END-LOCATION\n\
14024where START-LOCATION and END-LOCATION can be one of the following:\n\
14025 LINENUM, for that line in the current file,\n\
14026 FILE:LINENUM, for that line in that file,\n\
14027 +OFFSET, for that number of lines after the current line\n\
14028 or the start of the range\n\
14029 FUNCTION, for the first line in that function,\n\
14030 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14031 *ADDRESS, for the instruction at that address.\n\
14032\n\
14033The breakpoint will stop execution of the inferior whenever it executes\n\
14034an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14035range (including START-LOCATION and END-LOCATION)."));
14036
765dc015 14037 automatic_hardware_breakpoints = 1;
f3b1572e
PA
14038
14039 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 14040}
This page took 2.180759 seconds and 4 git commands to generate.