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