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