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