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