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