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