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