* frame.c (frame_unwind_id): Renamed to ...
[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,
0fb0cc75 5 2008, 2009 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"
23#include <ctype.h>
776592bf 24#include "hashtab.h"
c906108c
SS
25#include "symtab.h"
26#include "frame.h"
27#include "breakpoint.h"
1042e4c0 28#include "tracepoint.h"
c906108c
SS
29#include "gdbtypes.h"
30#include "expression.h"
31#include "gdbcore.h"
32#include "gdbcmd.h"
33#include "value.h"
34#include "command.h"
35#include "inferior.h"
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
39#include "gdb_string.h"
40#include "demangle.h"
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
fa4727a6 59#include "wrapper.h"
79a45b7d 60#include "valprint.h"
c906108c 61
1042e4c0
SS
62/* readline include files */
63#include "readline/readline.h"
64#include "readline/history.h"
65
66/* readline defines this. */
67#undef savestring
68
034dad6f 69#include "mi/mi-common.h"
104c1213 70
44feb3ce
TT
71/* Arguments to pass as context to some catch command handlers. */
72#define CATCH_PERMANENT ((void *) (uintptr_t) 0)
73#define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
c906108c 74
44feb3ce 75/* Prototypes for local functions. */
c906108c 76
a14ed312 77static void enable_delete_command (char *, int);
c906108c 78
a14ed312 79static void enable_delete_breakpoint (struct breakpoint *);
c906108c 80
a14ed312 81static void enable_once_command (char *, int);
c906108c 82
a14ed312 83static void enable_once_breakpoint (struct breakpoint *);
c906108c 84
a14ed312 85static void disable_command (char *, int);
c906108c 86
a14ed312 87static void enable_command (char *, int);
c906108c 88
a14ed312 89static void map_breakpoint_numbers (char *, void (*)(struct breakpoint *));
c906108c 90
a14ed312 91static void ignore_command (char *, int);
c906108c 92
4efb68b1 93static int breakpoint_re_set_one (void *);
c906108c 94
a14ed312 95static void clear_command (char *, int);
c906108c 96
a14ed312 97static void catch_command (char *, int);
c906108c 98
a14ed312 99static void watch_command (char *, int);
c906108c 100
a14ed312 101static int can_use_hardware_watchpoint (struct value *);
c906108c 102
98deb0da 103static void break_command_1 (char *, int, int);
c906108c 104
a14ed312 105static void mention (struct breakpoint *);
c906108c 106
4d28f7a8 107struct breakpoint *set_raw_breakpoint (struct symtab_and_line, enum bptype);
c906108c 108
9f60f21b 109static void check_duplicates (struct breakpoint *);
c906108c 110
76897487
KB
111static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
112
88f7da05
KB
113static CORE_ADDR adjust_breakpoint_address (CORE_ADDR bpaddr,
114 enum bptype bptype);
76897487 115
714835d5 116static void describe_other_breakpoints (CORE_ADDR, struct obj_section *, int);
c906108c 117
a14ed312 118static void breakpoints_info (char *, int);
c906108c 119
a14ed312 120static void breakpoint_1 (int, int);
c906108c 121
89f9893c 122static bpstat bpstat_alloc (const struct bp_location *, bpstat);
c906108c 123
4efb68b1 124static int breakpoint_cond_eval (void *);
c906108c 125
4efb68b1 126static void cleanup_executing_breakpoints (void *);
c906108c 127
a14ed312 128static void commands_command (char *, int);
c906108c 129
a14ed312 130static void condition_command (char *, int);
c906108c 131
a14ed312 132static int get_number_trailer (char **, int);
c906108c 133
a14ed312 134void set_breakpoint_count (int);
c906108c 135
c5aa993b
JM
136typedef enum
137 {
138 mark_inserted,
139 mark_uninserted
140 }
141insertion_state_t;
c906108c 142
0bde7532 143static int remove_breakpoint (struct bp_location *, insertion_state_t);
c906108c 144
a14ed312 145static enum print_stop_action print_it_typical (bpstat);
e514a9d6
JM
146
147static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 148
4efb68b1 149static int watchpoint_check (void *);
c906108c 150
a14ed312 151static void maintenance_info_breakpoints (char *, int);
c906108c 152
a14ed312 153static int hw_breakpoint_used_count (void);
c906108c 154
a14ed312 155static int hw_watchpoint_used_count (enum bptype, int *);
c906108c 156
a14ed312 157static void hbreak_command (char *, int);
c906108c 158
a14ed312 159static void thbreak_command (char *, int);
c906108c 160
a14ed312 161static void watch_command_1 (char *, int, int);
c906108c 162
a14ed312 163static void rwatch_command (char *, int);
c906108c 164
a14ed312 165static void awatch_command (char *, int);
c906108c 166
a14ed312 167static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
c906108c 168
a14ed312 169static void stop_command (char *arg, int from_tty);
7a292a7a 170
a14ed312 171static void stopin_command (char *arg, int from_tty);
7a292a7a 172
a14ed312 173static void stopat_command (char *arg, int from_tty);
7a292a7a 174
a14ed312 175static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 176
a14ed312 177static char *ep_parse_optional_filename (char **arg);
7a292a7a 178
d85310f7
MS
179static void catch_exception_command_1 (enum exception_event_kind ex_event,
180 char *arg, int tempflag, int from_tty);
7a292a7a 181
a14ed312 182static void tcatch_command (char *arg, int from_tty);
7a292a7a 183
a14ed312 184static void ep_skip_leading_whitespace (char **s);
7a292a7a 185
1aafd4da
UW
186static int single_step_breakpoint_inserted_here_p (CORE_ADDR pc);
187
fe3f5fa8
VP
188static void free_bp_location (struct bp_location *loc);
189
39d61571 190static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 191
b60e7edf 192static void update_global_location_list (int);
a5606eee 193
b60e7edf 194static void update_global_location_list_nothrow (int);
74960c60
VP
195
196static int is_hardware_watchpoint (struct breakpoint *bpt);
197
198static void insert_breakpoint_locations (void);
a5606eee 199
1042e4c0
SS
200static void tracepoints_info (char *, int);
201
202static void delete_trace_command (char *, int);
203
204static void enable_trace_command (char *, int);
205
206static void disable_trace_command (char *, int);
207
208static void trace_pass_command (char *, int);
209
3daf8fe5
JG
210static void skip_prologue_sal (struct symtab_and_line *sal);
211
212
f3b1572e
PA
213/* Flag indicating that a command has proceeded the inferior past the
214 current breakpoint. */
215
216static int breakpoint_proceeded;
217
2cec12e5
AR
218static const char *
219bpdisp_text (enum bpdisp disp)
220{
221 /* NOTE: the following values are a part of MI protocol and represent
222 values of 'disp' field returned when inferior stops at a breakpoint. */
223 static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
224 return bpdisps[(int) disp];
225}
c906108c 226
2cec12e5 227/* Prototypes for exported functions. */
c906108c
SS
228/* If FALSE, gdb will not use hardware support for watchpoints, even
229 if such is available. */
230static int can_use_hw_watchpoints;
231
920d2a44
AC
232static void
233show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
234 struct cmd_list_element *c,
235 const char *value)
236{
237 fprintf_filtered (file, _("\
238Debugger's willingness to use watchpoint hardware is %s.\n"),
239 value);
240}
241
fa8d40ab
JJ
242/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
243 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
244 for unrecognized breakpoint locations.
245 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
246static enum auto_boolean pending_break_support;
920d2a44
AC
247static void
248show_pending_break_support (struct ui_file *file, int from_tty,
249 struct cmd_list_element *c,
250 const char *value)
251{
252 fprintf_filtered (file, _("\
253Debugger's behavior regarding pending breakpoints is %s.\n"),
254 value);
255}
fa8d40ab 256
765dc015
VP
257/* If 1, gdb will automatically use hardware breakpoints for breakpoints
258 set with "break" but falling in read-only memory.
259 If 0, gdb will warn about such breakpoints, but won't automatically
260 use hardware breakpoints. */
261static int automatic_hardware_breakpoints;
262static void
263show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
264 struct cmd_list_element *c,
265 const char *value)
266{
267 fprintf_filtered (file, _("\
268Automatic usage of hardware breakpoints is %s.\n"),
269 value);
270}
271
33e5cbd6
PA
272/* If on, gdb will keep breakpoints inserted even as inferior is
273 stopped, and immediately insert any new breakpoints. If off, gdb
274 will insert breakpoints into inferior only when resuming it, and
275 will remove breakpoints upon stop. If auto, GDB will behave as ON
276 if in non-stop mode, and as OFF if all-stop mode.*/
277
278static const char always_inserted_auto[] = "auto";
279static const char always_inserted_on[] = "on";
280static const char always_inserted_off[] = "off";
281static const char *always_inserted_enums[] = {
282 always_inserted_auto,
283 always_inserted_off,
284 always_inserted_on,
285 NULL
286};
287static const char *always_inserted_mode = always_inserted_auto;
288static void
74960c60 289show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 290 struct cmd_list_element *c, const char *value)
74960c60 291{
33e5cbd6
PA
292 if (always_inserted_mode == always_inserted_auto)
293 fprintf_filtered (file, _("\
294Always inserted breakpoint mode is %s (currently %s).\n"),
295 value,
296 breakpoints_always_inserted_mode () ? "on" : "off");
297 else
298 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
74960c60
VP
299}
300
33e5cbd6
PA
301int
302breakpoints_always_inserted_mode (void)
303{
304 return (always_inserted_mode == always_inserted_on
305 || (always_inserted_mode == always_inserted_auto && non_stop));
306}
765dc015 307
a14ed312 308void _initialize_breakpoint (void);
c906108c 309
c906108c
SS
310/* Are we executing breakpoint commands? */
311static int executing_breakpoint_commands;
312
c02f5703
MS
313/* Are overlay event breakpoints enabled? */
314static int overlay_events_enabled;
315
c906108c
SS
316/* Walk the following statement or block through all breakpoints.
317 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
318 breakpoint. */
319
5c44784c 320#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 321
5c44784c
JM
322#define ALL_BREAKPOINTS_SAFE(B,TMP) \
323 for (B = breakpoint_chain; \
324 B ? (TMP=B->next, 1): 0; \
325 B = TMP)
c906108c 326
7cc221ef
DJ
327/* Similar iterators for the low-level breakpoints. */
328
0d381245 329#define ALL_BP_LOCATIONS(B) for (B = bp_location_chain; B; B = B->global_next)
7cc221ef
DJ
330
331#define ALL_BP_LOCATIONS_SAFE(B,TMP) \
332 for (B = bp_location_chain; \
0d381245 333 B ? (TMP=B->global_next, 1): 0; \
7cc221ef
DJ
334 B = TMP)
335
1042e4c0
SS
336/* Iterator for tracepoints only. */
337
338#define ALL_TRACEPOINTS(B) \
339 for (B = breakpoint_chain; B; B = B->next) \
340 if ((B)->type == bp_tracepoint)
341
7cc221ef 342/* Chains of all breakpoints defined. */
c906108c
SS
343
344struct breakpoint *breakpoint_chain;
345
7cc221ef
DJ
346struct bp_location *bp_location_chain;
347
20874c92
VP
348/* The locations that no longer correspond to any breakpoint,
349 unlinked from bp_location_chain, but for which a hit
350 may still be reported by a target. */
351VEC(bp_location_p) *moribund_locations = NULL;
352
c906108c
SS
353/* Number of last breakpoint made. */
354
355int breakpoint_count;
356
1042e4c0
SS
357/* Number of last tracepoint made. */
358
359int tracepoint_count;
360
468d015d
JJ
361/* Return whether a breakpoint is an active enabled breakpoint. */
362static int
363breakpoint_enabled (struct breakpoint *b)
364{
0d381245 365 return (b->enable_state == bp_enabled);
468d015d
JJ
366}
367
c906108c
SS
368/* Set breakpoint count to NUM. */
369
370void
fba45db2 371set_breakpoint_count (int num)
c906108c
SS
372{
373 breakpoint_count = num;
4fa62494 374 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
375}
376
377/* Used in run_command to zero the hit count when a new run starts. */
378
379void
fba45db2 380clear_breakpoint_hit_counts (void)
c906108c
SS
381{
382 struct breakpoint *b;
383
384 ALL_BREAKPOINTS (b)
385 b->hit_count = 0;
386}
387
388/* Default address, symtab and line to put a breakpoint at
389 for "break" command with no arg.
390 if default_breakpoint_valid is zero, the other three are
391 not valid, and "break" with no arg is an error.
392
393 This set by print_stack_frame, which calls set_default_breakpoint. */
394
395int default_breakpoint_valid;
396CORE_ADDR default_breakpoint_address;
397struct symtab *default_breakpoint_symtab;
398int default_breakpoint_line;
399\f
400/* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
401 Advance *PP after the string and any trailing whitespace.
402
403 Currently the string can either be a number or "$" followed by the name
404 of a convenience variable. Making it an expression wouldn't work well
5c44784c 405 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
40c03ae8
EZ
406
407 If the string is a NULL pointer, that denotes the last breakpoint.
5c44784c
JM
408
409 TRAILER is a character which can be found after the number; most
410 commonly this is `-'. If you don't want a trailer, use \0. */
c906108c 411static int
fba45db2 412get_number_trailer (char **pp, int trailer)
c906108c 413{
5c44784c 414 int retval = 0; /* default */
c906108c
SS
415 char *p = *pp;
416
417 if (p == NULL)
418 /* Empty line means refer to the last breakpoint. */
419 return breakpoint_count;
420 else if (*p == '$')
421 {
422 /* Make a copy of the name, so we can null-terminate it
c5aa993b 423 to pass to lookup_internalvar(). */
c906108c
SS
424 char *varname;
425 char *start = ++p;
4fa62494 426 LONGEST val;
c906108c
SS
427
428 while (isalnum (*p) || *p == '_')
429 p++;
430 varname = (char *) alloca (p - start + 1);
431 strncpy (varname, start, p - start);
432 varname[p - start] = '\0';
4fa62494
UW
433 if (get_internalvar_integer (lookup_internalvar (varname), &val))
434 retval = (int) val;
5c44784c
JM
435 else
436 {
a3f17187 437 printf_filtered (_("Convenience variable must have integer value.\n"));
5c44784c
JM
438 retval = 0;
439 }
c906108c
SS
440 }
441 else
442 {
443 if (*p == '-')
444 ++p;
445 while (*p >= '0' && *p <= '9')
446 ++p;
447 if (p == *pp)
448 /* There is no number here. (e.g. "cond a == b"). */
5c44784c
JM
449 {
450 /* Skip non-numeric token */
451 while (*p && !isspace((int) *p))
452 ++p;
453 /* Return zero, which caller must interpret as error. */
454 retval = 0;
455 }
456 else
457 retval = atoi (*pp);
458 }
459 if (!(isspace (*p) || *p == '\0' || *p == trailer))
460 {
461 /* Trailing junk: return 0 and let caller print error msg. */
462 while (!(isspace (*p) || *p == '\0' || *p == trailer))
463 ++p;
464 retval = 0;
c906108c 465 }
c906108c
SS
466 while (isspace (*p))
467 p++;
468 *pp = p;
469 return retval;
470}
5c44784c 471
11cf8741 472
5c44784c
JM
473/* Like get_number_trailer, but don't allow a trailer. */
474int
fba45db2 475get_number (char **pp)
5c44784c
JM
476{
477 return get_number_trailer (pp, '\0');
478}
479
480/* Parse a number or a range.
481 * A number will be of the form handled by get_number.
482 * A range will be of the form <number1> - <number2>, and
483 * will represent all the integers between number1 and number2,
484 * inclusive.
485 *
486 * While processing a range, this fuction is called iteratively;
487 * At each call it will return the next value in the range.
488 *
489 * At the beginning of parsing a range, the char pointer PP will
490 * be advanced past <number1> and left pointing at the '-' token.
491 * Subsequent calls will not advance the pointer until the range
492 * is completed. The call that completes the range will advance
493 * pointer PP past <number2>.
494 */
495
496int
fba45db2 497get_number_or_range (char **pp)
5c44784c
JM
498{
499 static int last_retval, end_value;
500 static char *end_ptr;
501 static int in_range = 0;
502
503 if (**pp != '-')
504 {
505 /* Default case: pp is pointing either to a solo number,
506 or to the first number of a range. */
507 last_retval = get_number_trailer (pp, '-');
508 if (**pp == '-')
509 {
510 char **temp;
511
512 /* This is the start of a range (<number1> - <number2>).
513 Skip the '-', parse and remember the second number,
514 and also remember the end of the final token. */
515
516 temp = &end_ptr;
517 end_ptr = *pp + 1;
518 while (isspace ((int) *end_ptr))
519 end_ptr++; /* skip white space */
520 end_value = get_number (temp);
521 if (end_value < last_retval)
522 {
8a3fe4f8 523 error (_("inverted range"));
5c44784c
JM
524 }
525 else if (end_value == last_retval)
526 {
527 /* degenerate range (number1 == number2). Advance the
528 token pointer so that the range will be treated as a
529 single number. */
530 *pp = end_ptr;
531 }
532 else
533 in_range = 1;
534 }
535 }
536 else if (! in_range)
8a3fe4f8 537 error (_("negative value"));
5c44784c
JM
538 else
539 {
540 /* pp points to the '-' that betokens a range. All
541 number-parsing has already been done. Return the next
542 integer value (one greater than the saved previous value).
543 Do not advance the token pointer 'pp' until the end of range
544 is reached. */
545
546 if (++last_retval == end_value)
547 {
548 /* End of range reached; advance token pointer. */
549 *pp = end_ptr;
550 in_range = 0;
551 }
552 }
553 return last_retval;
554}
555
556
c906108c
SS
557\f
558/* condition N EXP -- set break condition of breakpoint N to EXP. */
559
560static void
fba45db2 561condition_command (char *arg, int from_tty)
c906108c 562{
52f0bd74 563 struct breakpoint *b;
c906108c 564 char *p;
52f0bd74 565 int bnum;
c906108c
SS
566
567 if (arg == 0)
e2e0b3e5 568 error_no_arg (_("breakpoint number"));
c906108c
SS
569
570 p = arg;
571 bnum = get_number (&p);
5c44784c 572 if (bnum == 0)
8a3fe4f8 573 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
574
575 ALL_BREAKPOINTS (b)
576 if (b->number == bnum)
2f069f6f
JB
577 {
578 struct bp_location *loc = b->loc;
579 for (; loc; loc = loc->next)
580 {
581 if (loc->cond)
582 {
583 xfree (loc->cond);
584 loc->cond = 0;
585 }
586 }
587 if (b->cond_string != NULL)
588 xfree (b->cond_string);
c906108c 589
2f069f6f
JB
590 if (*p == 0)
591 {
592 b->cond_string = NULL;
593 if (from_tty)
594 printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
595 }
596 else
597 {
598 arg = p;
599 /* I don't know if it matters whether this is the string the user
600 typed in or the decompiled expression. */
1b36a34b 601 b->cond_string = xstrdup (arg);
2f069f6f
JB
602 b->condition_not_parsed = 0;
603 for (loc = b->loc; loc; loc = loc->next)
604 {
605 arg = p;
606 loc->cond =
607 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
608 if (*arg)
609 error (_("Junk at end of expression"));
610 }
611 }
612 breakpoints_changed ();
383f836e 613 observer_notify_breakpoint_modified (b->number);
2f069f6f
JB
614 return;
615 }
c906108c 616
8a3fe4f8 617 error (_("No breakpoint number %d."), bnum);
c906108c
SS
618}
619
c906108c 620static void
fba45db2 621commands_command (char *arg, int from_tty)
c906108c 622{
52f0bd74 623 struct breakpoint *b;
c906108c 624 char *p;
52f0bd74 625 int bnum;
c906108c
SS
626 struct command_line *l;
627
628 /* If we allowed this, we would have problems with when to
629 free the storage, if we change the commands currently
630 being read from. */
631
632 if (executing_breakpoint_commands)
8a3fe4f8 633 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
c906108c
SS
634
635 p = arg;
636 bnum = get_number (&p);
5c44784c 637
c906108c 638 if (p && *p)
8a3fe4f8 639 error (_("Unexpected extra arguments following breakpoint number."));
c5aa993b 640
c906108c
SS
641 ALL_BREAKPOINTS (b)
642 if (b->number == bnum)
9ebf4acf
AC
643 {
644 char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.",
645 bnum);
646 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
311a4e6b 647 l = read_command_lines (tmpbuf, from_tty, 1);
9ebf4acf
AC
648 do_cleanups (cleanups);
649 free_command_lines (&b->commands);
650 b->commands = l;
651 breakpoints_changed ();
383f836e 652 observer_notify_breakpoint_modified (b->number);
9ebf4acf 653 return;
c5aa993b 654 }
8a3fe4f8 655 error (_("No breakpoint number %d."), bnum);
c906108c 656}
40c03ae8
EZ
657
658/* Like commands_command, but instead of reading the commands from
659 input stream, takes them from an already parsed command structure.
660
661 This is used by cli-script.c to DTRT with breakpoint commands
662 that are part of if and while bodies. */
663enum command_control_type
664commands_from_control_command (char *arg, struct command_line *cmd)
665{
666 struct breakpoint *b;
667 char *p;
668 int bnum;
669
670 /* If we allowed this, we would have problems with when to
671 free the storage, if we change the commands currently
672 being read from. */
673
674 if (executing_breakpoint_commands)
675 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
676
677 /* An empty string for the breakpoint number means the last
678 breakpoint, but get_number expects a NULL pointer. */
679 if (arg && !*arg)
680 p = NULL;
681 else
682 p = arg;
683 bnum = get_number (&p);
684
685 if (p && *p)
686 error (_("Unexpected extra arguments following breakpoint number."));
687
688 ALL_BREAKPOINTS (b)
689 if (b->number == bnum)
690 {
691 free_command_lines (&b->commands);
692 if (cmd->body_count != 1)
693 error (_("Invalid \"commands\" block structure."));
694 /* We need to copy the commands because if/while will free the
695 list after it finishes execution. */
696 b->commands = copy_command_lines (cmd->body_list[0]);
697 breakpoints_changed ();
383f836e 698 observer_notify_breakpoint_modified (b->number);
40c03ae8 699 return simple_control;
2f069f6f 700 }
40c03ae8
EZ
701 error (_("No breakpoint number %d."), bnum);
702}
c906108c 703\f
8defab1a
DJ
704/* Update BUF, which is LEN bytes read from the target address MEMADDR,
705 by replacing any memory breakpoints with their shadowed contents. */
c906108c 706
8defab1a
DJ
707void
708breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
c906108c 709{
8defab1a 710 struct bp_location *b;
c906108c
SS
711 CORE_ADDR bp_addr = 0;
712 int bp_size = 0;
8defab1a 713 int bptoffset = 0;
c5aa993b 714
ffce0d52 715 ALL_BP_LOCATIONS (b)
c5aa993b 716 {
ffce0d52 717 if (b->owner->type == bp_none)
8a3fe4f8 718 warning (_("reading through apparently deleted breakpoint #%d?"),
ffce0d52
DJ
719 b->owner->number);
720
721 if (b->loc_type != bp_loc_software_breakpoint)
c5aa993b 722 continue;
ffce0d52 723 if (!b->inserted)
c5aa993b
JM
724 continue;
725 /* Addresses and length of the part of the breakpoint that
726 we need to copy. */
8181d85f
DJ
727 bp_addr = b->target_info.placed_address;
728 bp_size = b->target_info.shadow_len;
c5aa993b 729 if (bp_size == 0)
8181d85f 730 /* bp isn't valid, or doesn't shadow memory. */
c5aa993b 731 continue;
8defab1a 732
c5aa993b
JM
733 if (bp_addr + bp_size <= memaddr)
734 /* The breakpoint is entirely before the chunk of memory we
735 are reading. */
736 continue;
8defab1a 737
c5aa993b
JM
738 if (bp_addr >= memaddr + len)
739 /* The breakpoint is entirely after the chunk of memory we are
740 reading. */
741 continue;
c5aa993b 742
8defab1a
DJ
743 /* Offset within shadow_contents. */
744 if (bp_addr < memaddr)
745 {
746 /* Only copy the second part of the breakpoint. */
747 bp_size -= memaddr - bp_addr;
748 bptoffset = memaddr - bp_addr;
749 bp_addr = memaddr;
750 }
c5aa993b 751
8defab1a
DJ
752 if (bp_addr + bp_size > memaddr + len)
753 {
754 /* Only copy the first part of the breakpoint. */
755 bp_size -= (bp_addr + bp_size) - (memaddr + len);
756 }
c5aa993b 757
8defab1a
DJ
758 memcpy (buf + bp_addr - memaddr,
759 b->target_info.shadow_contents + bptoffset, bp_size);
c5aa993b 760 }
c906108c 761}
c906108c 762\f
c5aa993b 763
687595f9 764/* A wrapper function for inserting catchpoints. */
9cbc821d 765static void
687595f9
DJ
766insert_catchpoint (struct ui_out *uo, void *args)
767{
768 struct breakpoint *b = (struct breakpoint *) args;
769 int val = -1;
770
fe798b75
JB
771 gdb_assert (b->type == bp_catchpoint);
772 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
773
774 b->ops->insert (b);
687595f9
DJ
775}
776
a5606eee
VP
777static int
778is_hardware_watchpoint (struct breakpoint *bpt)
779{
780 return (bpt->type == bp_hardware_watchpoint
781 || bpt->type == bp_read_watchpoint
782 || bpt->type == bp_access_watchpoint);
783}
7270d8f2 784
fa4727a6
DJ
785/* Find the current value of a watchpoint on EXP. Return the value in
786 *VALP and *RESULTP and the chain of intermediate and final values
787 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
788 not need them.
789
ccc57cf9 790 If a memory error occurs while evaluating the expression, *RESULTP will
fa4727a6
DJ
791 be set to NULL. *RESULTP may be a lazy value, if the result could
792 not be read from memory. It is used to determine whether a value
793 is user-specified (we should watch the whole value) or intermediate
794 (we should watch only the bit used to locate the final value).
795
796 If the final value, or any intermediate value, could not be read
797 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
798 set to any referenced values. *VALP will never be a lazy value.
799 This is the value which we store in struct breakpoint.
800
801 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
802 value chain. The caller must free the values individually. If
803 VAL_CHAIN is NULL, all generated values will be left on the value
804 chain. */
805
806static void
807fetch_watchpoint_value (struct expression *exp, struct value **valp,
808 struct value **resultp, struct value **val_chain)
809{
810 struct value *mark, *new_mark, *result;
ccc57cf9 811 volatile struct gdb_exception ex;
fa4727a6
DJ
812
813 *valp = NULL;
814 if (resultp)
815 *resultp = NULL;
816 if (val_chain)
817 *val_chain = NULL;
818
819 /* Evaluate the expression. */
820 mark = value_mark ();
821 result = NULL;
ccc57cf9
PA
822
823 TRY_CATCH (ex, RETURN_MASK_ALL)
824 {
825 result = evaluate_expression (exp);
826 }
827 if (ex.reason < 0)
828 {
829 /* Ignore memory errors, we want watchpoints pointing at
830 inaccessible memory to still be created; otherwise, throw the
831 error to some higher catcher. */
832 switch (ex.error)
833 {
834 case MEMORY_ERROR:
835 break;
836 default:
837 throw_exception (ex);
838 break;
839 }
840 }
841
fa4727a6
DJ
842 new_mark = value_mark ();
843 if (mark == new_mark)
844 return;
845 if (resultp)
846 *resultp = result;
847
848 /* Make sure it's not lazy, so that after the target stops again we
849 have a non-lazy previous value to compare with. */
850 if (result != NULL
851 && (!value_lazy (result) || gdb_value_fetch_lazy (result)))
852 *valp = result;
853
854 if (val_chain)
855 {
856 /* Return the chain of intermediate values. We use this to
857 decide which addresses to watch. */
858 *val_chain = new_mark;
859 value_release_to_mark (mark);
860 }
861}
862
567e1b4e
JB
863/* Assuming that B is a watchpoint:
864 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 865 - Evaluate expression and store the result in B->val
567e1b4e
JB
866 - Evaluate the condition if there is one, and store the result
867 in b->loc->cond.
a5606eee
VP
868 - Update the list of values that must be watched in B->loc.
869
bfa149ac
JB
870 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
871 If this is local watchpoint that is out of scope, delete it. */
b40ce68a 872static void
a5606eee 873update_watchpoint (struct breakpoint *b, int reparse)
7270d8f2 874{
a5606eee 875 int within_current_scope;
a5606eee
VP
876 struct frame_id saved_frame_id;
877 struct bp_location *loc;
878 bpstat bs;
879
567e1b4e
JB
880 /* We don't free locations. They are stored in bp_location_chain and
881 update_global_locations will eventually delete them and remove
882 breakpoints if needed. */
a5606eee
VP
883 b->loc = NULL;
884
885 if (b->disposition == disp_del_at_next_stop)
886 return;
887
888 /* Save the current frame's ID so we can restore it after
889 evaluating the watchpoint expression on its own frame. */
890 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
891 took a frame parameter, so that we didn't have to change the
892 selected frame. */
893 saved_frame_id = get_frame_id (get_selected_frame (NULL));
894
895 /* Determine if the watchpoint is within scope. */
896 if (b->exp_valid_block == NULL)
897 within_current_scope = 1;
898 else
899 {
900 struct frame_info *fi;
901 fi = frame_find_by_id (b->watchpoint_frame);
902 within_current_scope = (fi != NULL);
903 if (within_current_scope)
904 select_frame (fi);
905 }
906
907 if (within_current_scope && reparse)
908 {
909 char *s;
910 if (b->exp)
911 {
912 xfree (b->exp);
913 b->exp = NULL;
914 }
915 s = b->exp_string;
916 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
917 /* If the meaning of expression itself changed, the old value is
918 no longer relevant. We don't want to report a watchpoint hit
919 to the user when the old value and the new value may actually
920 be completely different objects. */
921 value_free (b->val);
fa4727a6
DJ
922 b->val = NULL;
923 b->val_valid = 0;
a5606eee 924 }
a5606eee
VP
925
926 /* If we failed to parse the expression, for example because
927 it refers to a global variable in a not-yet-loaded shared library,
928 don't try to insert watchpoint. We don't automatically delete
929 such watchpoint, though, since failure to parse expression
930 is different from out-of-scope watchpoint. */
931 if (within_current_scope && b->exp)
932 {
fa4727a6 933 struct value *val_chain, *v, *result, *next;
a5606eee 934
fa4727a6 935 fetch_watchpoint_value (b->exp, &v, &result, &val_chain);
a5606eee 936
a5606eee
VP
937 /* Avoid setting b->val if it's already set. The meaning of
938 b->val is 'the last value' user saw, and we should update
939 it only if we reported that last value to user. As it
940 happens, the code that reports it updates b->val directly. */
fa4727a6
DJ
941 if (!b->val_valid)
942 {
943 b->val = v;
944 b->val_valid = 1;
945 }
a5606eee 946
db2ad4c3
JK
947 /* Change the type of breakpoint between hardware assisted or an
948 ordinary watchpoint depending on the hardware support and free
949 hardware slots. REPARSE is set when the inferior is started. */
950 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
951 && reparse)
952 {
ca2d49e8 953 int i, mem_cnt, other_type_used;
db2ad4c3
JK
954
955 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
956 &other_type_used);
957 mem_cnt = can_use_hardware_watchpoint (val_chain);
958
ca2d49e8 959 if (!mem_cnt)
db2ad4c3
JK
960 b->type = bp_watchpoint;
961 else
ca2d49e8 962 {
d92524f1 963 int target_resources_ok = target_can_use_hardware_watchpoint
ca2d49e8
SL
964 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
965 if (target_resources_ok <= 0)
966 b->type = bp_watchpoint;
967 else
968 b->type = bp_hardware_watchpoint;
969 }
db2ad4c3
JK
970 }
971
a5606eee 972 /* Look at each value on the value chain. */
fa4727a6 973 for (v = val_chain; v; v = next)
a5606eee
VP
974 {
975 /* If it's a memory location, and GDB actually needed
976 its contents to evaluate the expression, then we
fa4727a6
DJ
977 must watch it. If the first value returned is
978 still lazy, that means an error occurred reading it;
979 watch it anyway in case it becomes readable. */
a5606eee 980 if (VALUE_LVAL (v) == lval_memory
fa4727a6 981 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
982 {
983 struct type *vtype = check_typedef (value_type (v));
7270d8f2 984
a5606eee
VP
985 /* We only watch structs and arrays if user asked
986 for it explicitly, never if they just happen to
987 appear in the middle of some value chain. */
fa4727a6 988 if (v == result
a5606eee
VP
989 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
990 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
991 {
992 CORE_ADDR addr;
993 int len, type;
994 struct bp_location *loc, **tmp;
995
42ae5230 996 addr = value_address (v);
a5606eee
VP
997 len = TYPE_LENGTH (value_type (v));
998 type = hw_write;
999 if (b->type == bp_read_watchpoint)
1000 type = hw_read;
1001 else if (b->type == bp_access_watchpoint)
1002 type = hw_access;
1003
39d61571 1004 loc = allocate_bp_location (b);
a5606eee
VP
1005 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1006 ;
1007 *tmp = loc;
1008 loc->address = addr;
1009 loc->length = len;
1010 loc->watchpoint_type = type;
1011 }
1012 }
1013
1014 next = value_next (v);
1015 if (v != b->val)
1016 value_free (v);
1017 }
1018
2ec93238
MK
1019 /* We just regenerated the list of breakpoint locations.
1020 The new location does not have its condition field set to anything
1021 and therefore, we must always reparse the cond_string, independently
1022 of the value of the reparse flag. */
1023 if (b->cond_string != NULL)
a5606eee
VP
1024 {
1025 char *s = b->cond_string;
a5606eee
VP
1026 b->loc->cond = parse_exp_1 (&s, b->exp_valid_block, 0);
1027 }
1028 }
1029 else if (!within_current_scope)
7270d8f2 1030 {
a5606eee 1031 printf_filtered (_("\
567e1b4e 1032Watchpoint %d deleted because the program has left the block \n\
a5606eee
VP
1033in which its expression is valid.\n"),
1034 b->number);
1035 if (b->related_breakpoint)
1036 b->related_breakpoint->disposition = disp_del_at_next_stop;
1037 b->disposition = disp_del_at_next_stop;
7270d8f2 1038 }
a5606eee
VP
1039
1040 /* Restore the selected frame. */
1041 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1042}
1043
a5606eee 1044
74960c60
VP
1045/* Returns 1 iff breakpoint location should be
1046 inserted in the inferior. */
1047static int
1048should_be_inserted (struct bp_location *bpt)
1049{
1050 if (!breakpoint_enabled (bpt->owner))
1051 return 0;
1052
1053 if (bpt->owner->disposition == disp_del_at_next_stop)
1054 return 0;
1055
1056 if (!bpt->enabled || bpt->shlib_disabled || bpt->duplicate)
1057 return 0;
1058
1042e4c0
SS
1059 /* Tracepoints are inserted by the target at a time of its choosing,
1060 not by us. */
1061 if (bpt->owner->type == bp_tracepoint)
1062 return 0;
1063
74960c60
VP
1064 return 1;
1065}
1066
879bfdc2
DJ
1067/* Insert a low-level "breakpoint" of some type. BPT is the breakpoint.
1068 Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
fa3a767f 1069 and HW_BREAKPOINT_ERROR are used to report problems.
879bfdc2
DJ
1070
1071 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1072 method for each breakpoint or catchpoint type. */
26bb91f3 1073static int
879bfdc2 1074insert_bp_location (struct bp_location *bpt,
26bb91f3 1075 struct ui_file *tmp_error_stream,
fa3a767f 1076 int *disabled_breaks,
26bb91f3 1077 int *hw_breakpoint_error)
879bfdc2
DJ
1078{
1079 int val = 0;
1080
74960c60 1081 if (!should_be_inserted (bpt) || bpt->inserted)
879bfdc2
DJ
1082 return 0;
1083
8181d85f
DJ
1084 /* Initialize the target-specific information. */
1085 memset (&bpt->target_info, 0, sizeof (bpt->target_info));
1086 bpt->target_info.placed_address = bpt->address;
1087
879bfdc2
DJ
1088 if (bpt->loc_type == bp_loc_software_breakpoint
1089 || bpt->loc_type == bp_loc_hardware_breakpoint)
1090 {
765dc015
VP
1091 if (bpt->owner->type != bp_hardware_breakpoint)
1092 {
1093 /* If the explicitly specified breakpoint type
1094 is not hardware breakpoint, check the memory map to see
1095 if the breakpoint address is in read only memory or not.
1096 Two important cases are:
1097 - location type is not hardware breakpoint, memory
1098 is readonly. We change the type of the location to
1099 hardware breakpoint.
1100 - location type is hardware breakpoint, memory is read-write.
1101 This means we've previously made the location hardware one, but
1102 then the memory map changed, so we undo.
1103
1104 When breakpoints are removed, remove_breakpoints will
1105 use location types we've just set here, the only possible
1106 problem is that memory map has changed during running program,
1107 but it's not going to work anyway with current gdb. */
1108 struct mem_region *mr
1109 = lookup_mem_region (bpt->target_info.placed_address);
1110
1111 if (mr)
1112 {
1113 if (automatic_hardware_breakpoints)
1114 {
1115 int changed = 0;
1116 enum bp_loc_type new_type;
1117
1118 if (mr->attrib.mode != MEM_RW)
1119 new_type = bp_loc_hardware_breakpoint;
1120 else
1121 new_type = bp_loc_software_breakpoint;
1122
1123 if (new_type != bpt->loc_type)
1124 {
1125 static int said = 0;
1126 bpt->loc_type = new_type;
1127 if (!said)
1128 {
1129 fprintf_filtered (gdb_stdout, _("\
0767c96d 1130Note: automatically using hardware breakpoints for read-only addresses.\n"));
765dc015
VP
1131 said = 1;
1132 }
1133 }
1134 }
1135 else if (bpt->loc_type == bp_loc_software_breakpoint
1136 && mr->attrib.mode != MEM_RW)
1137 warning (_("cannot set software breakpoint at readonly address %s"),
1138 paddr (bpt->address));
1139 }
1140 }
1141
879bfdc2
DJ
1142 /* First check to see if we have to handle an overlay. */
1143 if (overlay_debugging == ovly_off
1144 || bpt->section == NULL
1145 || !(section_is_overlay (bpt->section)))
1146 {
1147 /* No overlay handling: just set the breakpoint. */
1148
1149 if (bpt->loc_type == bp_loc_hardware_breakpoint)
8181d85f 1150 val = target_insert_hw_breakpoint (&bpt->target_info);
879bfdc2 1151 else
8181d85f 1152 val = target_insert_breakpoint (&bpt->target_info);
879bfdc2
DJ
1153 }
1154 else
1155 {
1156 /* This breakpoint is in an overlay section.
1157 Shall we set a breakpoint at the LMA? */
1158 if (!overlay_events_enabled)
1159 {
1160 /* Yes -- overlay event support is not active,
1161 so we must try to set a breakpoint at the LMA.
1162 This will not work for a hardware breakpoint. */
1163 if (bpt->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 1164 warning (_("hardware breakpoint %d not supported in overlay!"),
879bfdc2
DJ
1165 bpt->owner->number);
1166 else
1167 {
1168 CORE_ADDR addr = overlay_unmapped_address (bpt->address,
1169 bpt->section);
1170 /* Set a software (trap) breakpoint at the LMA. */
8181d85f
DJ
1171 bpt->overlay_target_info = bpt->target_info;
1172 bpt->overlay_target_info.placed_address = addr;
1173 val = target_insert_breakpoint (&bpt->overlay_target_info);
879bfdc2 1174 if (val != 0)
99361f52
DE
1175 fprintf_unfiltered (tmp_error_stream,
1176 "Overlay breakpoint %d failed: in ROM?\n",
879bfdc2
DJ
1177 bpt->owner->number);
1178 }
1179 }
1180 /* Shall we set a breakpoint at the VMA? */
1181 if (section_is_mapped (bpt->section))
1182 {
1183 /* Yes. This overlay section is mapped into memory. */
1184 if (bpt->loc_type == bp_loc_hardware_breakpoint)
8181d85f 1185 val = target_insert_hw_breakpoint (&bpt->target_info);
879bfdc2 1186 else
8181d85f 1187 val = target_insert_breakpoint (&bpt->target_info);
879bfdc2
DJ
1188 }
1189 else
1190 {
1191 /* No. This breakpoint will not be inserted.
1192 No error, but do not mark the bp as 'inserted'. */
1193 return 0;
1194 }
1195 }
1196
1197 if (val)
1198 {
1199 /* Can't set the breakpoint. */
f5c9a895 1200 if (solib_name_from_address (bpt->address))
879bfdc2
DJ
1201 {
1202 /* See also: disable_breakpoints_in_shlibs. */
1203 val = 0;
0d381245 1204 bpt->shlib_disabled = 1;
879bfdc2
DJ
1205 if (!*disabled_breaks)
1206 {
1207 fprintf_unfiltered (tmp_error_stream,
1208 "Cannot insert breakpoint %d.\n",
1209 bpt->owner->number);
1210 fprintf_unfiltered (tmp_error_stream,
1211 "Temporarily disabling shared library breakpoints:\n");
1212 }
1213 *disabled_breaks = 1;
1214 fprintf_unfiltered (tmp_error_stream,
1215 "breakpoint #%d\n", bpt->owner->number);
1216 }
1217 else
879bfdc2 1218 {
879bfdc2
DJ
1219 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1220 {
1221 *hw_breakpoint_error = 1;
1222 fprintf_unfiltered (tmp_error_stream,
1223 "Cannot insert hardware breakpoint %d.\n",
1224 bpt->owner->number);
1225 }
1226 else
1227 {
1228 fprintf_unfiltered (tmp_error_stream,
1229 "Cannot insert breakpoint %d.\n",
1230 bpt->owner->number);
1231 fprintf_filtered (tmp_error_stream,
1232 "Error accessing memory address ");
ed49a04f 1233 fputs_filtered (paddress (bpt->address), tmp_error_stream);
879bfdc2
DJ
1234 fprintf_filtered (tmp_error_stream, ": %s.\n",
1235 safe_strerror (val));
1236 }
1237
1238 }
1239 }
1240 else
1241 bpt->inserted = 1;
1242
1243 return val;
1244 }
1245
1246 else if (bpt->loc_type == bp_loc_hardware_watchpoint
1247 /* NOTE drow/2003-09-08: This state only exists for removing
1248 watchpoints. It's not clear that it's necessary... */
1249 && bpt->owner->disposition != disp_del_at_next_stop)
1250 {
a5606eee
VP
1251 val = target_insert_watchpoint (bpt->address,
1252 bpt->length,
1253 bpt->watchpoint_type);
1254 bpt->inserted = (val != -1);
879bfdc2
DJ
1255 }
1256
fe798b75 1257 else if (bpt->owner->type == bp_catchpoint)
879bfdc2 1258 {
71fff37b
AC
1259 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1260 bpt->owner, RETURN_MASK_ERROR);
9cbc821d
AC
1261 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1262 bpt->owner->number);
1263 if (e.reason < 0)
879bfdc2
DJ
1264 bpt->owner->enable_state = bp_disabled;
1265 else
1266 bpt->inserted = 1;
1640b821
DJ
1267
1268 /* We've already printed an error message if there was a problem
1269 inserting this catchpoint, and we've disabled the catchpoint,
1270 so just return success. */
1271 return 0;
879bfdc2
DJ
1272 }
1273
1274 return 0;
1275}
1276
74960c60
VP
1277/* Make sure all breakpoints are inserted in inferior.
1278 Throws exception on any error.
1279 A breakpoint that is already inserted won't be inserted
1280 again, so calling this function twice is safe. */
1281void
1282insert_breakpoints (void)
1283{
1284 struct breakpoint *bpt;
1285
1286 ALL_BREAKPOINTS (bpt)
1287 if (is_hardware_watchpoint (bpt))
1288 update_watchpoint (bpt, 0 /* don't reparse. */);
1289
b60e7edf 1290 update_global_location_list (1);
74960c60 1291
c35b1492
PA
1292 /* update_global_location_list does not insert breakpoints when
1293 always_inserted_mode is not enabled. Explicitly insert them
1294 now. */
1295 if (!breakpoints_always_inserted_mode ())
74960c60
VP
1296 insert_breakpoint_locations ();
1297}
1298
c906108c
SS
1299/* insert_breakpoints is used when starting or continuing the program.
1300 remove_breakpoints is used when the program stops.
1301 Both return zero if successful,
1302 or an `errno' value if could not write the inferior. */
1303
74960c60
VP
1304static void
1305insert_breakpoint_locations (void)
c906108c 1306{
a5606eee 1307 struct breakpoint *bpt;
879bfdc2 1308 struct bp_location *b, *temp;
e236ba44 1309 int error = 0;
c906108c
SS
1310 int val = 0;
1311 int disabled_breaks = 0;
81d0cc19 1312 int hw_breakpoint_error = 0;
c906108c 1313
81d0cc19 1314 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 1315 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 1316
81d0cc19
GS
1317 /* Explicitly mark the warning -- this will only be printed if
1318 there was an error. */
1319 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
a5606eee 1320
879bfdc2
DJ
1321 ALL_BP_LOCATIONS_SAFE (b, temp)
1322 {
74960c60 1323 if (!should_be_inserted (b) || b->inserted)
879bfdc2
DJ
1324 continue;
1325
f365de73
AS
1326 /* There is no point inserting thread-specific breakpoints if the
1327 thread no longer exists. */
1328 if (b->owner->thread != -1
1329 && !valid_thread_id (b->owner->thread))
1330 continue;
1331
879bfdc2 1332 val = insert_bp_location (b, tmp_error_stream,
fa3a767f 1333 &disabled_breaks,
879bfdc2
DJ
1334 &hw_breakpoint_error);
1335 if (val)
e236ba44 1336 error = val;
879bfdc2 1337 }
c906108c 1338
a5606eee
VP
1339 /* If we failed to insert all locations of a watchpoint,
1340 remove them, as half-inserted watchpoint is of limited use. */
1341 ALL_BREAKPOINTS (bpt)
1342 {
1343 int some_failed = 0;
1344 struct bp_location *loc;
1345
1346 if (!is_hardware_watchpoint (bpt))
1347 continue;
1348
d6b74ac4 1349 if (!breakpoint_enabled (bpt))
a5606eee 1350 continue;
74960c60
VP
1351
1352 if (bpt->disposition == disp_del_at_next_stop)
1353 continue;
a5606eee
VP
1354
1355 for (loc = bpt->loc; loc; loc = loc->next)
1356 if (!loc->inserted)
1357 {
1358 some_failed = 1;
1359 break;
1360 }
1361 if (some_failed)
1362 {
1363 for (loc = bpt->loc; loc; loc = loc->next)
1364 if (loc->inserted)
1365 remove_breakpoint (loc, mark_uninserted);
1366
1367 hw_breakpoint_error = 1;
1368 fprintf_unfiltered (tmp_error_stream,
1369 "Could not insert hardware watchpoint %d.\n",
1370 bpt->number);
1371 error = -1;
1372 }
1373 }
1374
e236ba44 1375 if (error)
81d0cc19
GS
1376 {
1377 /* If a hardware breakpoint or watchpoint was inserted, add a
1378 message about possibly exhausted resources. */
879bfdc2 1379 if (hw_breakpoint_error)
81d0cc19 1380 {
c6510018
MS
1381 fprintf_unfiltered (tmp_error_stream,
1382 "Could not insert hardware breakpoints:\n\
1383You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 1384 }
81d0cc19
GS
1385 target_terminal_ours_for_output ();
1386 error_stream (tmp_error_stream);
1387 }
f7545552
TT
1388
1389 do_cleanups (cleanups);
c906108c
SS
1390}
1391
c906108c 1392int
fba45db2 1393remove_breakpoints (void)
c906108c 1394{
0bde7532 1395 struct bp_location *b;
c906108c
SS
1396 int val;
1397
0bde7532 1398 ALL_BP_LOCATIONS (b)
c5aa993b 1399 {
0bde7532 1400 if (b->inserted)
c5aa993b
JM
1401 {
1402 val = remove_breakpoint (b, mark_uninserted);
1403 if (val != 0)
1404 return val;
1405 }
1406 }
c906108c
SS
1407 return 0;
1408}
1409
692590c1 1410int
80ce1ecb 1411remove_hw_watchpoints (void)
692590c1 1412{
0bde7532 1413 struct bp_location *b;
692590c1
MS
1414 int val;
1415
0bde7532 1416 ALL_BP_LOCATIONS (b)
692590c1 1417 {
0bde7532 1418 if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
692590c1
MS
1419 {
1420 val = remove_breakpoint (b, mark_uninserted);
1421 if (val != 0)
1422 return val;
1423 }
1424 }
1425 return 0;
1426}
1427
c906108c 1428int
fba45db2 1429reattach_breakpoints (int pid)
c906108c 1430{
0bde7532 1431 struct bp_location *b;
c906108c 1432 int val;
ce696e05 1433 struct cleanup *old_chain = save_inferior_ptid ();
a4954f26 1434 struct ui_file *tmp_error_stream = mem_fileopen ();
fa3a767f 1435 int dummy1 = 0, dummy2 = 0;
a4954f26
DJ
1436
1437 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 1438
ce696e05 1439 inferior_ptid = pid_to_ptid (pid);
0bde7532 1440 ALL_BP_LOCATIONS (b)
c5aa993b 1441 {
0bde7532 1442 if (b->inserted)
c5aa993b 1443 {
a4954f26
DJ
1444 b->inserted = 0;
1445 val = insert_bp_location (b, tmp_error_stream,
fa3a767f 1446 &dummy1, &dummy2);
c5aa993b
JM
1447 if (val != 0)
1448 {
ce696e05 1449 do_cleanups (old_chain);
c5aa993b
JM
1450 return val;
1451 }
1452 }
1453 }
ce696e05 1454 do_cleanups (old_chain);
c906108c
SS
1455 return 0;
1456}
1457
e58b0e63
PA
1458static int internal_breakpoint_number = -1;
1459
e62c965a
PP
1460static struct breakpoint *
1461create_internal_breakpoint (CORE_ADDR address, enum bptype type)
1462{
e62c965a
PP
1463 struct symtab_and_line sal;
1464 struct breakpoint *b;
1465
1466 init_sal (&sal); /* initialize to zeroes */
1467
1468 sal.pc = address;
1469 sal.section = find_pc_overlay (sal.pc);
1470
1471 b = set_raw_breakpoint (sal, type);
1472 b->number = internal_breakpoint_number--;
1473 b->disposition = disp_donttouch;
1474
1475 return b;
1476}
1477
1478static void
69de3c6a 1479create_overlay_event_breakpoint (char *func_name)
e62c965a 1480{
69de3c6a 1481 struct objfile *objfile;
e62c965a 1482
69de3c6a
PP
1483 ALL_OBJFILES (objfile)
1484 {
1485 struct breakpoint *b;
1486 struct minimal_symbol *m;
1487
1488 m = lookup_minimal_symbol_text (func_name, objfile);
1489 if (m == NULL)
1490 continue;
e62c965a 1491
69de3c6a
PP
1492 b = create_internal_breakpoint (SYMBOL_VALUE_ADDRESS (m),
1493 bp_overlay_event);
1494 b->addr_string = xstrdup (func_name);
e62c965a 1495
69de3c6a
PP
1496 if (overlay_debugging == ovly_auto)
1497 {
1498 b->enable_state = bp_enabled;
1499 overlay_events_enabled = 1;
1500 }
1501 else
1502 {
1503 b->enable_state = bp_disabled;
1504 overlay_events_enabled = 0;
1505 }
e62c965a
PP
1506 }
1507 update_global_location_list (1);
1508}
1509
0fd8e87f
UW
1510static void
1511create_longjmp_master_breakpoint (char *func_name)
1512{
1513 struct objfile *objfile;
1514
1515 ALL_OBJFILES (objfile)
1516 {
1517 struct breakpoint *b;
1518 struct minimal_symbol *m;
1519
1520 if (!gdbarch_get_longjmp_target_p (get_objfile_arch (objfile)))
1521 continue;
1522
1523 m = lookup_minimal_symbol_text (func_name, objfile);
1524 if (m == NULL)
1525 continue;
1526
1527 b = create_internal_breakpoint (SYMBOL_VALUE_ADDRESS (m),
1528 bp_longjmp_master);
1529 b->addr_string = xstrdup (func_name);
1530 b->enable_state = bp_disabled;
1531 }
1532 update_global_location_list (1);
1533}
1534
c906108c 1535void
fba45db2 1536update_breakpoints_after_exec (void)
c906108c 1537{
c5aa993b
JM
1538 struct breakpoint *b;
1539 struct breakpoint *temp;
25b22b0a 1540 struct bp_location *bploc;
c906108c 1541
25b22b0a
PA
1542 /* We're about to delete breakpoints from GDB's lists. If the
1543 INSERTED flag is true, GDB will try to lift the breakpoints by
1544 writing the breakpoints' "shadow contents" back into memory. The
1545 "shadow contents" are NOT valid after an exec, so GDB should not
1546 do that. Instead, the target is responsible from marking
1547 breakpoints out as soon as it detects an exec. We don't do that
1548 here instead, because there may be other attempts to delete
1549 breakpoints after detecting an exec and before reaching here. */
1550 ALL_BP_LOCATIONS (bploc)
1551 gdb_assert (!bploc->inserted);
c906108c
SS
1552
1553 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b
JM
1554 {
1555 /* Solib breakpoints must be explicitly reset after an exec(). */
1556 if (b->type == bp_shlib_event)
1557 {
1558 delete_breakpoint (b);
1559 continue;
1560 }
c906108c 1561
1900040c 1562 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
1563 as must overlay event and longjmp master breakpoints. */
1564 if (b->type == bp_thread_event || b->type == bp_overlay_event
1565 || b->type == bp_longjmp_master)
c4093a6a
JM
1566 {
1567 delete_breakpoint (b);
1568 continue;
1569 }
1570
c5aa993b
JM
1571 /* Step-resume breakpoints are meaningless after an exec(). */
1572 if (b->type == bp_step_resume)
1573 {
1574 delete_breakpoint (b);
1575 continue;
1576 }
1577
611c83ae
PA
1578 /* Longjmp and longjmp-resume breakpoints are also meaningless
1579 after an exec. */
1580 if (b->type == bp_longjmp || b->type == bp_longjmp_resume)
1581 {
1582 delete_breakpoint (b);
1583 continue;
1584 }
1585
ce78b96d
JB
1586 if (b->type == bp_catchpoint)
1587 {
1588 /* For now, none of the bp_catchpoint breakpoints need to
1589 do anything at this point. In the future, if some of
1590 the catchpoints need to something, we will need to add
1591 a new method, and call this method from here. */
1592 continue;
1593 }
1594
c5aa993b
JM
1595 /* bp_finish is a special case. The only way we ought to be able
1596 to see one of these when an exec() has happened, is if the user
1597 caught a vfork, and then said "finish". Ordinarily a finish just
1598 carries them to the call-site of the current callee, by setting
1599 a temporary bp there and resuming. But in this case, the finish
1600 will carry them entirely through the vfork & exec.
1601
1602 We don't want to allow a bp_finish to remain inserted now. But
1603 we can't safely delete it, 'cause finish_command has a handle to
1604 the bp on a bpstat, and will later want to delete it. There's a
1605 chance (and I've seen it happen) that if we delete the bp_finish
1606 here, that its storage will get reused by the time finish_command
1607 gets 'round to deleting the "use to be a bp_finish" breakpoint.
1608 We really must allow finish_command to delete a bp_finish.
1609
53a5351d
JM
1610 In the absense of a general solution for the "how do we know
1611 it's safe to delete something others may have handles to?"
1612 problem, what we'll do here is just uninsert the bp_finish, and
1613 let finish_command delete it.
1614
1615 (We know the bp_finish is "doomed" in the sense that it's
1616 momentary, and will be deleted as soon as finish_command sees
1617 the inferior stopped. So it doesn't matter that the bp's
1618 address is probably bogus in the new a.out, unlike e.g., the
1619 solib breakpoints.) */
c5aa993b 1620
c5aa993b
JM
1621 if (b->type == bp_finish)
1622 {
1623 continue;
1624 }
1625
1626 /* Without a symbolic address, we have little hope of the
1627 pre-exec() address meaning the same thing in the post-exec()
1628 a.out. */
1629 if (b->addr_string == NULL)
1630 {
1631 delete_breakpoint (b);
1632 continue;
1633 }
c5aa993b 1634 }
1900040c 1635 /* FIXME what about longjmp breakpoints? Re-create them here? */
69de3c6a 1636 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
1637 create_longjmp_master_breakpoint ("longjmp");
1638 create_longjmp_master_breakpoint ("_longjmp");
1639 create_longjmp_master_breakpoint ("siglongjmp");
1640 create_longjmp_master_breakpoint ("_siglongjmp");
c906108c
SS
1641}
1642
1643int
fba45db2 1644detach_breakpoints (int pid)
c906108c 1645{
0bde7532 1646 struct bp_location *b;
c906108c 1647 int val;
ce696e05 1648 struct cleanup *old_chain = save_inferior_ptid ();
c5aa993b 1649
39f77062 1650 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 1651 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 1652
ce696e05
KB
1653 /* Set inferior_ptid; remove_breakpoint uses this global. */
1654 inferior_ptid = pid_to_ptid (pid);
0bde7532 1655 ALL_BP_LOCATIONS (b)
c5aa993b 1656 {
0bde7532 1657 if (b->inserted)
c5aa993b
JM
1658 {
1659 val = remove_breakpoint (b, mark_inserted);
1660 if (val != 0)
1661 {
ce696e05 1662 do_cleanups (old_chain);
c5aa993b
JM
1663 return val;
1664 }
1665 }
1666 }
ce696e05 1667 do_cleanups (old_chain);
c906108c
SS
1668 return 0;
1669}
1670
1671static int
0bde7532 1672remove_breakpoint (struct bp_location *b, insertion_state_t is)
c906108c
SS
1673{
1674 int val;
c5aa993b 1675
0bde7532 1676 if (b->owner->enable_state == bp_permanent)
c2c6d25f
JM
1677 /* Permanent breakpoints cannot be inserted or removed. */
1678 return 0;
1679
74960c60
VP
1680 /* The type of none suggests that owner is actually deleted.
1681 This should not ever happen. */
1682 gdb_assert (b->owner->type != bp_none);
0bde7532
DJ
1683
1684 if (b->loc_type == bp_loc_software_breakpoint
1685 || b->loc_type == bp_loc_hardware_breakpoint)
c906108c 1686 {
c02f5703
MS
1687 /* "Normal" instruction breakpoint: either the standard
1688 trap-instruction bp (bp_breakpoint), or a
1689 bp_hardware_breakpoint. */
1690
1691 /* First check to see if we have to handle an overlay. */
1692 if (overlay_debugging == ovly_off
0bde7532
DJ
1693 || b->section == NULL
1694 || !(section_is_overlay (b->section)))
c02f5703
MS
1695 {
1696 /* No overlay handling: just remove the breakpoint. */
1697
0bde7532 1698 if (b->loc_type == bp_loc_hardware_breakpoint)
8181d85f 1699 val = target_remove_hw_breakpoint (&b->target_info);
c02f5703 1700 else
8181d85f 1701 val = target_remove_breakpoint (&b->target_info);
c02f5703 1702 }
c906108c
SS
1703 else
1704 {
c02f5703
MS
1705 /* This breakpoint is in an overlay section.
1706 Did we set a breakpoint at the LMA? */
1707 if (!overlay_events_enabled)
1708 {
1709 /* Yes -- overlay event support is not active, so we
1710 should have set a breakpoint at the LMA. Remove it.
1711 */
c02f5703
MS
1712 /* Ignore any failures: if the LMA is in ROM, we will
1713 have already warned when we failed to insert it. */
0bde7532 1714 if (b->loc_type == bp_loc_hardware_breakpoint)
8181d85f 1715 target_remove_hw_breakpoint (&b->overlay_target_info);
c02f5703 1716 else
8181d85f 1717 target_remove_breakpoint (&b->overlay_target_info);
c02f5703
MS
1718 }
1719 /* Did we set a breakpoint at the VMA?
1720 If so, we will have marked the breakpoint 'inserted'. */
0bde7532 1721 if (b->inserted)
c906108c 1722 {
c02f5703
MS
1723 /* Yes -- remove it. Previously we did not bother to
1724 remove the breakpoint if the section had been
1725 unmapped, but let's not rely on that being safe. We
1726 don't know what the overlay manager might do. */
0bde7532 1727 if (b->loc_type == bp_loc_hardware_breakpoint)
8181d85f 1728 val = target_remove_hw_breakpoint (&b->target_info);
aa67235e
UW
1729
1730 /* However, we should remove *software* breakpoints only
1731 if the section is still mapped, or else we overwrite
1732 wrong code with the saved shadow contents. */
1733 else if (section_is_mapped (b->section))
8181d85f 1734 val = target_remove_breakpoint (&b->target_info);
aa67235e
UW
1735 else
1736 val = 0;
c906108c 1737 }
c02f5703
MS
1738 else
1739 {
1740 /* No -- not inserted, so no need to remove. No error. */
1741 val = 0;
1742 }
c906108c 1743 }
879d1e6b
UW
1744
1745 /* In some cases, we might not be able to remove a breakpoint
1746 in a shared library that has already been removed, but we
1747 have not yet processed the shlib unload event. */
f5c9a895 1748 if (val && solib_name_from_address (b->address))
879d1e6b
UW
1749 val = 0;
1750
c906108c
SS
1751 if (val)
1752 return val;
0bde7532 1753 b->inserted = (is == mark_inserted);
c906108c 1754 }
a5606eee 1755 else if (b->loc_type == bp_loc_hardware_watchpoint)
c906108c 1756 {
278cd55f
AC
1757 struct value *v;
1758 struct value *n;
c5aa993b 1759
0bde7532 1760 b->inserted = (is == mark_inserted);
a5606eee
VP
1761 val = target_remove_watchpoint (b->address, b->length,
1762 b->watchpoint_type);
2e70b7b9 1763
c906108c 1764 /* Failure to remove any of the hardware watchpoints comes here. */
0bde7532 1765 if ((is == mark_uninserted) && (b->inserted))
8a3fe4f8 1766 warning (_("Could not remove hardware watchpoint %d."),
0bde7532 1767 b->owner->number);
c906108c 1768 }
ce78b96d
JB
1769 else if (b->owner->type == bp_catchpoint
1770 && breakpoint_enabled (b->owner)
1771 && !b->duplicate)
1772 {
1773 gdb_assert (b->owner->ops != NULL && b->owner->ops->remove != NULL);
1774
1775 val = b->owner->ops->remove (b->owner);
1776 if (val)
1777 return val;
1778 b->inserted = (is == mark_inserted);
1779 }
c906108c
SS
1780
1781 return 0;
1782}
1783
1784/* Clear the "inserted" flag in all breakpoints. */
1785
25b22b0a 1786void
fba45db2 1787mark_breakpoints_out (void)
c906108c 1788{
075f6582 1789 struct bp_location *bpt;
c906108c 1790
075f6582
DJ
1791 ALL_BP_LOCATIONS (bpt)
1792 bpt->inserted = 0;
c906108c
SS
1793}
1794
53a5351d
JM
1795/* Clear the "inserted" flag in all breakpoints and delete any
1796 breakpoints which should go away between runs of the program.
c906108c
SS
1797
1798 Plus other such housekeeping that has to be done for breakpoints
1799 between runs.
1800
53a5351d
JM
1801 Note: this function gets called at the end of a run (by
1802 generic_mourn_inferior) and when a run begins (by
1803 init_wait_for_inferior). */
c906108c
SS
1804
1805
1806
1807void
fba45db2 1808breakpoint_init_inferior (enum inf_context context)
c906108c 1809{
52f0bd74 1810 struct breakpoint *b, *temp;
075f6582 1811 struct bp_location *bpt;
1c5cfe86 1812 int ix;
c906108c 1813
50c71eaf
PA
1814 /* If breakpoint locations are shared across processes, then there's
1815 nothing to do. */
2567c7d9 1816 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
1817 return;
1818
075f6582 1819 ALL_BP_LOCATIONS (bpt)
514f746b
AR
1820 if (bpt->owner->enable_state != bp_permanent)
1821 bpt->inserted = 0;
075f6582 1822
c906108c 1823 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 1824 {
c5aa993b
JM
1825 switch (b->type)
1826 {
1827 case bp_call_dummy:
1828 case bp_watchpoint_scope:
c906108c 1829
c5aa993b
JM
1830 /* If the call dummy breakpoint is at the entry point it will
1831 cause problems when the inferior is rerun, so we better
1832 get rid of it.
c906108c 1833
c5aa993b
JM
1834 Also get rid of scope breakpoints. */
1835 delete_breakpoint (b);
1836 break;
c906108c 1837
c5aa993b
JM
1838 case bp_watchpoint:
1839 case bp_hardware_watchpoint:
1840 case bp_read_watchpoint:
1841 case bp_access_watchpoint:
c906108c 1842
c5aa993b
JM
1843 /* Likewise for watchpoints on local expressions. */
1844 if (b->exp_valid_block != NULL)
1845 delete_breakpoint (b);
967af18d 1846 else if (context == inf_starting)
c860120c
PM
1847 {
1848 /* Reset val field to force reread of starting value
1849 in insert_breakpoints. */
1850 if (b->val)
1851 value_free (b->val);
1852 b->val = NULL;
fa4727a6 1853 b->val_valid = 0;
c860120c 1854 }
c5aa993b
JM
1855 break;
1856 default:
c5aa993b
JM
1857 break;
1858 }
1859 }
1c5cfe86
PA
1860
1861 /* Get rid of the moribund locations. */
1862 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bpt); ++ix)
1863 free_bp_location (bpt);
1864 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
1865}
1866
c2c6d25f
JM
1867/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
1868 exists at PC. It returns ordinary_breakpoint_here if it's an
1869 ordinary breakpoint, or permanent_breakpoint_here if it's a
1870 permanent breakpoint.
1871 - When continuing from a location with an ordinary breakpoint, we
1872 actually single step once before calling insert_breakpoints.
1873 - When continuing from a localion with a permanent breakpoint, we
1874 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
1875 the target, to advance the PC past the breakpoint. */
c906108c 1876
c2c6d25f 1877enum breakpoint_here
fba45db2 1878breakpoint_here_p (CORE_ADDR pc)
c906108c 1879{
89f9893c 1880 const struct bp_location *bpt;
c2c6d25f 1881 int any_breakpoint_here = 0;
c906108c 1882
075f6582
DJ
1883 ALL_BP_LOCATIONS (bpt)
1884 {
1885 if (bpt->loc_type != bp_loc_software_breakpoint
1886 && bpt->loc_type != bp_loc_hardware_breakpoint)
1887 continue;
1888
468d015d 1889 if ((breakpoint_enabled (bpt->owner)
075f6582
DJ
1890 || bpt->owner->enable_state == bp_permanent)
1891 && bpt->address == pc) /* bp is enabled and matches pc */
1892 {
1893 if (overlay_debugging
1894 && section_is_overlay (bpt->section)
1895 && !section_is_mapped (bpt->section))
1896 continue; /* unmapped overlay -- can't be a match */
1897 else if (bpt->owner->enable_state == bp_permanent)
1898 return permanent_breakpoint_here;
1899 else
1900 any_breakpoint_here = 1;
1901 }
1902 }
c906108c 1903
c2c6d25f 1904 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
1905}
1906
1c5cfe86
PA
1907/* Return true if there's a moribund breakpoint at PC. */
1908
1909int
1910moribund_breakpoint_here_p (CORE_ADDR pc)
1911{
1912 struct bp_location *loc;
1913 int ix;
1914
1915 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
1916 if (loc->address == pc)
1917 return 1;
1918
1919 return 0;
1920}
c2c6d25f 1921
c36b740a
VP
1922/* Returns non-zero if there's a breakpoint inserted at PC, which is
1923 inserted using regular breakpoint_chain/bp_location_chain mechanism.
1924 This does not check for single-step breakpoints, which are
1925 inserted and removed using direct target manipulation. */
c906108c
SS
1926
1927int
c36b740a 1928regular_breakpoint_inserted_here_p (CORE_ADDR pc)
c906108c 1929{
89f9893c 1930 const struct bp_location *bpt;
c906108c 1931
075f6582 1932 ALL_BP_LOCATIONS (bpt)
c5aa993b 1933 {
075f6582
DJ
1934 if (bpt->loc_type != bp_loc_software_breakpoint
1935 && bpt->loc_type != bp_loc_hardware_breakpoint)
1936 continue;
1937
1938 if (bpt->inserted
1939 && bpt->address == pc) /* bp is inserted and matches pc */
1940 {
1941 if (overlay_debugging
1942 && section_is_overlay (bpt->section)
1943 && !section_is_mapped (bpt->section))
1944 continue; /* unmapped overlay -- can't be a match */
1945 else
1946 return 1;
1947 }
c5aa993b 1948 }
c36b740a
VP
1949 return 0;
1950}
1951
1952/* Returns non-zero iff there's either regular breakpoint
1953 or a single step breakpoint inserted at PC. */
1954
1955int
1956breakpoint_inserted_here_p (CORE_ADDR pc)
1957{
1958 if (regular_breakpoint_inserted_here_p (pc))
1959 return 1;
c906108c 1960
1aafd4da
UW
1961 if (single_step_breakpoint_inserted_here_p (pc))
1962 return 1;
1963
c906108c
SS
1964 return 0;
1965}
1966
4fa8626c
DJ
1967/* This function returns non-zero iff there is a software breakpoint
1968 inserted at PC. */
1969
1970int
1971software_breakpoint_inserted_here_p (CORE_ADDR pc)
1972{
89f9893c 1973 const struct bp_location *bpt;
4fa8626c
DJ
1974 int any_breakpoint_here = 0;
1975
1976 ALL_BP_LOCATIONS (bpt)
1977 {
1978 if (bpt->loc_type != bp_loc_software_breakpoint)
1979 continue;
1980
0d381245 1981 if (bpt->inserted
4fa8626c
DJ
1982 && bpt->address == pc) /* bp is enabled and matches pc */
1983 {
1984 if (overlay_debugging
1985 && section_is_overlay (bpt->section)
1986 && !section_is_mapped (bpt->section))
1987 continue; /* unmapped overlay -- can't be a match */
1988 else
1989 return 1;
1990 }
1991 }
1992
1aafd4da
UW
1993 /* Also check for software single-step breakpoints. */
1994 if (single_step_breakpoint_inserted_here_p (pc))
1995 return 1;
1996
4fa8626c
DJ
1997 return 0;
1998}
1999
075f6582
DJ
2000/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2001 PC is valid for process/thread PTID. */
c906108c
SS
2002
2003int
39f77062 2004breakpoint_thread_match (CORE_ADDR pc, ptid_t ptid)
c906108c 2005{
89f9893c 2006 const struct bp_location *bpt;
4a306c9a 2007 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 2008 int thread = -1;
4a306c9a 2009 int task = 0;
a6f1cd96 2010
075f6582 2011 ALL_BP_LOCATIONS (bpt)
c5aa993b 2012 {
075f6582
DJ
2013 if (bpt->loc_type != bp_loc_software_breakpoint
2014 && bpt->loc_type != bp_loc_hardware_breakpoint)
2015 continue;
2016
a6f1cd96
JB
2017 if (!breakpoint_enabled (bpt->owner)
2018 && bpt->owner->enable_state != bp_permanent)
2019 continue;
2020
2021 if (bpt->address != pc)
2022 continue;
2023
2024 if (bpt->owner->thread != -1)
075f6582 2025 {
a6f1cd96
JB
2026 /* This is a thread-specific breakpoint. Check that ptid
2027 matches that thread. If thread hasn't been computed yet,
2028 it is now time to do so. */
2029 if (thread == -1)
2030 thread = pid_to_thread_id (ptid);
2031 if (bpt->owner->thread != thread)
2032 continue;
075f6582 2033 }
a6f1cd96 2034
4a306c9a
JB
2035 if (bpt->owner->task != 0)
2036 {
2037 /* This is a task-specific breakpoint. Check that ptid
2038 matches that task. If task hasn't been computed yet,
2039 it is now time to do so. */
2040 if (task == 0)
2041 task = ada_get_task_number (ptid);
2042 if (bpt->owner->task != task)
2043 continue;
2044 }
2045
a6f1cd96
JB
2046 if (overlay_debugging
2047 && section_is_overlay (bpt->section)
2048 && !section_is_mapped (bpt->section))
2049 continue; /* unmapped overlay -- can't be a match */
2050
2051 return 1;
c5aa993b 2052 }
c906108c
SS
2053
2054 return 0;
2055}
c906108c 2056\f
c5aa993b 2057
c906108c
SS
2058/* bpstat stuff. External routines' interfaces are documented
2059 in breakpoint.h. */
2060
2061int
fba45db2 2062ep_is_catchpoint (struct breakpoint *ep)
c906108c 2063{
533be4dd 2064 return (ep->type == bp_catchpoint);
c906108c
SS
2065}
2066
198757a8
VP
2067void
2068bpstat_free (bpstat bs)
2069{
2070 if (bs->old_val != NULL)
2071 value_free (bs->old_val);
2072 free_command_lines (&bs->commands);
2073 xfree (bs);
2074}
2075
c906108c
SS
2076/* Clear a bpstat so that it says we are not at any breakpoint.
2077 Also free any storage that is part of a bpstat. */
2078
2079void
fba45db2 2080bpstat_clear (bpstat *bsp)
c906108c
SS
2081{
2082 bpstat p;
2083 bpstat q;
2084
2085 if (bsp == 0)
2086 return;
2087 p = *bsp;
2088 while (p != NULL)
2089 {
2090 q = p->next;
198757a8 2091 bpstat_free (p);
c906108c
SS
2092 p = q;
2093 }
2094 *bsp = NULL;
2095}
2096
2097/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
2098 is part of the bpstat is copied as well. */
2099
2100bpstat
fba45db2 2101bpstat_copy (bpstat bs)
c906108c
SS
2102{
2103 bpstat p = NULL;
2104 bpstat tmp;
2105 bpstat retval = NULL;
2106
2107 if (bs == NULL)
2108 return bs;
2109
2110 for (; bs != NULL; bs = bs->next)
2111 {
2112 tmp = (bpstat) xmalloc (sizeof (*tmp));
2113 memcpy (tmp, bs, sizeof (*tmp));
31cc81e9
DJ
2114 if (bs->commands != NULL)
2115 tmp->commands = copy_command_lines (bs->commands);
2116 if (bs->old_val != NULL)
3c3185ac
JK
2117 {
2118 tmp->old_val = value_copy (bs->old_val);
2119 release_value (tmp->old_val);
2120 }
31cc81e9 2121
c906108c
SS
2122 if (p == NULL)
2123 /* This is the first thing in the chain. */
2124 retval = tmp;
2125 else
2126 p->next = tmp;
2127 p = tmp;
2128 }
2129 p->next = NULL;
2130 return retval;
2131}
2132
2133/* Find the bpstat associated with this breakpoint */
2134
2135bpstat
fba45db2 2136bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 2137{
c5aa993b
JM
2138 if (bsp == NULL)
2139 return NULL;
c906108c 2140
c5aa993b
JM
2141 for (; bsp != NULL; bsp = bsp->next)
2142 {
4f8d1dc6 2143 if (bsp->breakpoint_at && bsp->breakpoint_at->owner == breakpoint)
c5aa993b
JM
2144 return bsp;
2145 }
c906108c
SS
2146 return NULL;
2147}
2148
2149/* Find a step_resume breakpoint associated with this bpstat.
2150 (If there are multiple step_resume bp's on the list, this function
2151 will arbitrarily pick one.)
2152
2153 It is an error to use this function if BPSTAT doesn't contain a
2154 step_resume breakpoint.
2155
2156 See wait_for_inferior's use of this function. */
2157struct breakpoint *
fba45db2 2158bpstat_find_step_resume_breakpoint (bpstat bsp)
c906108c 2159{
8601f500
MS
2160 int current_thread;
2161
8a3fe4f8 2162 gdb_assert (bsp != NULL);
c906108c 2163
8601f500
MS
2164 current_thread = pid_to_thread_id (inferior_ptid);
2165
c906108c
SS
2166 for (; bsp != NULL; bsp = bsp->next)
2167 {
059fb39f
PM
2168 if ((bsp->breakpoint_at != NULL)
2169 && (bsp->breakpoint_at->owner->type == bp_step_resume)
2170 && (bsp->breakpoint_at->owner->thread == current_thread
2171 || bsp->breakpoint_at->owner->thread == -1))
4f8d1dc6 2172 return bsp->breakpoint_at->owner;
c906108c
SS
2173 }
2174
8a3fe4f8 2175 internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
c906108c
SS
2176}
2177
2178
8671a17b 2179/* Put in *NUM the breakpoint number of the first breakpoint we are stopped
c906108c
SS
2180 at. *BSP upon return is a bpstat which points to the remaining
2181 breakpoints stopped at (but which is not guaranteed to be good for
2182 anything but further calls to bpstat_num).
8671a17b
PA
2183 Return 0 if passed a bpstat which does not indicate any breakpoints.
2184 Return -1 if stopped at a breakpoint that has been deleted since
2185 we set it.
2186 Return 1 otherwise. */
c906108c
SS
2187
2188int
8671a17b 2189bpstat_num (bpstat *bsp, int *num)
c906108c
SS
2190{
2191 struct breakpoint *b;
2192
2193 if ((*bsp) == NULL)
2194 return 0; /* No more breakpoint values */
8671a17b 2195
4f8d1dc6
VP
2196 /* We assume we'll never have several bpstats that
2197 correspond to a single breakpoint -- otherwise,
2198 this function might return the same number more
2199 than once and this will look ugly. */
2200 b = (*bsp)->breakpoint_at ? (*bsp)->breakpoint_at->owner : NULL;
8671a17b
PA
2201 *bsp = (*bsp)->next;
2202 if (b == NULL)
2203 return -1; /* breakpoint that's been deleted since */
2204
2205 *num = b->number; /* We have its number */
2206 return 1;
c906108c
SS
2207}
2208
2209/* Modify BS so that the actions will not be performed. */
2210
2211void
fba45db2 2212bpstat_clear_actions (bpstat bs)
c906108c
SS
2213{
2214 for (; bs != NULL; bs = bs->next)
2215 {
c2b8ed2c 2216 free_command_lines (&bs->commands);
c906108c
SS
2217 if (bs->old_val != NULL)
2218 {
2219 value_free (bs->old_val);
2220 bs->old_val = NULL;
2221 }
2222 }
2223}
2224
f3b1572e
PA
2225/* Called when a command is about to proceed the inferior. */
2226
2227static void
2228breakpoint_about_to_proceed (void)
2229{
2230 if (!ptid_equal (inferior_ptid, null_ptid))
2231 {
2232 struct thread_info *tp = inferior_thread ();
2233
2234 /* Allow inferior function calls in breakpoint commands to not
2235 interrupt the command list. When the call finishes
2236 successfully, the inferior will be standing at the same
2237 breakpoint as if nothing happened. */
2238 if (tp->in_infcall)
2239 return;
2240 }
2241
2242 breakpoint_proceeded = 1;
2243}
2244
c906108c 2245/* Stub for cleaning up our state if we error-out of a breakpoint command */
c906108c 2246static void
4efb68b1 2247cleanup_executing_breakpoints (void *ignore)
c906108c
SS
2248{
2249 executing_breakpoint_commands = 0;
2250}
2251
2252/* Execute all the commands associated with all the breakpoints at this
2253 location. Any of these commands could cause the process to proceed
2254 beyond this point, etc. We look out for such changes by checking
347bddb7 2255 the global "breakpoint_proceeded" after each command.
c906108c 2256
347bddb7
PA
2257 Returns true if a breakpoint command resumed the inferior. In that
2258 case, it is the caller's responsibility to recall it again with the
2259 bpstat of the current thread. */
2260
2261static int
2262bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
2263{
2264 bpstat bs;
2265 struct cleanup *old_chain;
347bddb7 2266 int again = 0;
c906108c
SS
2267
2268 /* Avoid endless recursion if a `source' command is contained
2269 in bs->commands. */
2270 if (executing_breakpoint_commands)
347bddb7 2271 return 0;
c906108c
SS
2272
2273 executing_breakpoint_commands = 1;
2274 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2275
c906108c
SS
2276 /* This pointer will iterate over the list of bpstat's. */
2277 bs = *bsp;
2278
2279 breakpoint_proceeded = 0;
2280 for (; bs != NULL; bs = bs->next)
2281 {
6c50ab1c
JB
2282 struct command_line *cmd;
2283 struct cleanup *this_cmd_tree_chain;
2284
2285 /* Take ownership of the BSP's command tree, if it has one.
2286
2287 The command tree could legitimately contain commands like
2288 'step' and 'next', which call clear_proceed_status, which
2289 frees stop_bpstat's command tree. To make sure this doesn't
2290 free the tree we're executing out from under us, we need to
2291 take ownership of the tree ourselves. Since a given bpstat's
2292 commands are only executed once, we don't need to copy it; we
2293 can clear the pointer in the bpstat, and make sure we free
2294 the tree when we're done. */
c906108c 2295 cmd = bs->commands;
6c50ab1c
JB
2296 bs->commands = 0;
2297 this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2298
c906108c
SS
2299 while (cmd != NULL)
2300 {
2301 execute_control_command (cmd);
2302
2303 if (breakpoint_proceeded)
2304 break;
2305 else
2306 cmd = cmd->next;
2307 }
6c50ab1c
JB
2308
2309 /* We can free this command tree now. */
2310 do_cleanups (this_cmd_tree_chain);
2311
c906108c 2312 if (breakpoint_proceeded)
32c1e744
VP
2313 {
2314 if (target_can_async_p ())
347bddb7
PA
2315 /* If we are in async mode, then the target might be still
2316 running, not stopped at any breakpoint, so nothing for
2317 us to do here -- just return to the event loop. */
2318 ;
32c1e744
VP
2319 else
2320 /* In sync mode, when execute_control_command returns
2321 we're already standing on the next breakpoint.
347bddb7
PA
2322 Breakpoint commands for that stop were not run, since
2323 execute_command does not run breakpoint commands --
2324 only command_line_handler does, but that one is not
2325 involved in execution of breakpoint commands. So, we
2326 can now execute breakpoint commands. It should be
2327 noted that making execute_command do bpstat actions is
2328 not an option -- in this case we'll have recursive
2329 invocation of bpstat for each breakpoint with a
2330 command, and can easily blow up GDB stack. Instead, we
2331 return true, which will trigger the caller to recall us
2332 with the new stop_bpstat. */
2333 again = 1;
2334 break;
32c1e744 2335 }
c906108c 2336 }
c2b8ed2c 2337 do_cleanups (old_chain);
347bddb7
PA
2338 return again;
2339}
2340
2341void
2342bpstat_do_actions (void)
2343{
2344 /* Do any commands attached to breakpoint we are stopped at. */
2345 while (!ptid_equal (inferior_ptid, null_ptid)
2346 && target_has_execution
2347 && !is_exited (inferior_ptid)
2348 && !is_executing (inferior_ptid))
2349 /* Since in sync mode, bpstat_do_actions may resume the inferior,
2350 and only return when it is stopped at the next breakpoint, we
2351 keep doing breakpoint actions until it returns false to
2352 indicate the inferior was not resumed. */
2353 if (!bpstat_do_actions_1 (&inferior_thread ()->stop_bpstat))
2354 break;
c906108c
SS
2355}
2356
fa4727a6
DJ
2357/* Print out the (old or new) value associated with a watchpoint. */
2358
2359static void
2360watchpoint_value_print (struct value *val, struct ui_file *stream)
2361{
2362 if (val == NULL)
2363 fprintf_unfiltered (stream, _("<unreadable>"));
2364 else
79a45b7d
TT
2365 {
2366 struct value_print_options opts;
2367 get_user_print_options (&opts);
2368 value_print (val, stream, &opts);
2369 }
fa4727a6
DJ
2370}
2371
e514a9d6 2372/* This is the normal print function for a bpstat. In the future,
c906108c 2373 much of this logic could (should?) be moved to bpstat_stop_status,
e514a9d6
JM
2374 by having it set different print_it values.
2375
2376 Current scheme: When we stop, bpstat_print() is called. It loops
2377 through the bpstat list of things causing this stop, calling the
2378 print_bp_stop_message function on each one. The behavior of the
2379 print_bp_stop_message function depends on the print_it field of
2380 bpstat. If such field so indicates, call this function here.
2381
2382 Return values from this routine (ultimately used by bpstat_print()
2383 and normal_stop() to decide what to do):
2384 PRINT_NOTHING: Means we already printed all we needed to print,
2385 don't print anything else.
2386 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2387 that something to be followed by a location.
2388 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2389 that something to be followed by a location.
2390 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2391 analysis. */
c906108c 2392
917317f4 2393static enum print_stop_action
fba45db2 2394print_it_typical (bpstat bs)
c906108c 2395{
f7545552 2396 struct cleanup *old_chain;
4f8d1dc6 2397 struct breakpoint *b;
89f9893c 2398 const struct bp_location *bl;
8b93c638 2399 struct ui_stream *stb;
f7545552
TT
2400 int bp_temp = 0;
2401 enum print_stop_action result;
2402
c906108c
SS
2403 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2404 which has since been deleted. */
e514a9d6 2405 if (bs->breakpoint_at == NULL)
917317f4 2406 return PRINT_UNKNOWN;
0d381245
VP
2407 bl = bs->breakpoint_at;
2408 b = bl->owner;
c906108c 2409
f7545552
TT
2410 stb = ui_out_stream_new (uiout);
2411 old_chain = make_cleanup_ui_out_stream_delete (stb);
2412
4f8d1dc6 2413 switch (b->type)
c906108c 2414 {
e514a9d6
JM
2415 case bp_breakpoint:
2416 case bp_hardware_breakpoint:
2cec12e5 2417 bp_temp = bs->breakpoint_at->owner->disposition == disp_del;
0d381245
VP
2418 if (bl->address != bl->requested_address)
2419 breakpoint_adjustment_warning (bl->requested_address,
2420 bl->address,
4f8d1dc6
VP
2421 b->number, 1);
2422 annotate_breakpoint (b->number);
2cec12e5
AR
2423 if (bp_temp)
2424 ui_out_text (uiout, "\nTemporary breakpoint ");
2425 else
2426 ui_out_text (uiout, "\nBreakpoint ");
9dc5e2a9 2427 if (ui_out_is_mi_like_p (uiout))
2cec12e5
AR
2428 {
2429 ui_out_field_string (uiout, "reason",
2430 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2431 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
2432 }
4f8d1dc6 2433 ui_out_field_int (uiout, "bkptno", b->number);
8b93c638 2434 ui_out_text (uiout, ", ");
f7545552 2435 result = PRINT_SRC_AND_LOC;
e514a9d6
JM
2436 break;
2437
2438 case bp_shlib_event:
917317f4
JM
2439 /* Did we stop because the user set the stop_on_solib_events
2440 variable? (If so, we report this as a generic, "Stopped due
2441 to shlib event" message.) */
a3f17187 2442 printf_filtered (_("Stopped due to shared library event\n"));
f7545552 2443 result = PRINT_NOTHING;
e514a9d6
JM
2444 break;
2445
c4093a6a
JM
2446 case bp_thread_event:
2447 /* Not sure how we will get here.
2448 GDB should not stop for these breakpoints. */
a3f17187 2449 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
f7545552 2450 result = PRINT_NOTHING;
c4093a6a
JM
2451 break;
2452
1900040c
MS
2453 case bp_overlay_event:
2454 /* By analogy with the thread event, GDB should not stop for these. */
a3f17187 2455 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
f7545552 2456 result = PRINT_NOTHING;
1900040c
MS
2457 break;
2458
0fd8e87f
UW
2459 case bp_longjmp_master:
2460 /* These should never be enabled. */
2461 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
2462 result = PRINT_NOTHING;
2463 break;
2464
e514a9d6
JM
2465 case bp_watchpoint:
2466 case bp_hardware_watchpoint:
fa4727a6
DJ
2467 annotate_watchpoint (b->number);
2468 if (ui_out_is_mi_like_p (uiout))
2469 ui_out_field_string
2470 (uiout, "reason",
2471 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2472 mention (b);
f7545552 2473 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
fa4727a6
DJ
2474 ui_out_text (uiout, "\nOld value = ");
2475 watchpoint_value_print (bs->old_val, stb->stream);
2476 ui_out_field_stream (uiout, "old", stb);
2477 ui_out_text (uiout, "\nNew value = ");
2478 watchpoint_value_print (b->val, stb->stream);
2479 ui_out_field_stream (uiout, "new", stb);
fa4727a6 2480 ui_out_text (uiout, "\n");
e514a9d6 2481 /* More than one watchpoint may have been triggered. */
f7545552 2482 result = PRINT_UNKNOWN;
e514a9d6
JM
2483 break;
2484
2485 case bp_read_watchpoint:
9dc5e2a9 2486 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2487 ui_out_field_string
2488 (uiout, "reason",
2489 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
4f8d1dc6 2490 mention (b);
f7545552 2491 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 2492 ui_out_text (uiout, "\nValue = ");
fa4727a6 2493 watchpoint_value_print (b->val, stb->stream);
8b93c638 2494 ui_out_field_stream (uiout, "value", stb);
8b93c638 2495 ui_out_text (uiout, "\n");
f7545552 2496 result = PRINT_UNKNOWN;
e514a9d6
JM
2497 break;
2498
2499 case bp_access_watchpoint:
fa4727a6 2500 if (bs->old_val != NULL)
8b93c638 2501 {
4f8d1dc6 2502 annotate_watchpoint (b->number);
9dc5e2a9 2503 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2504 ui_out_field_string
2505 (uiout, "reason",
2506 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
4f8d1dc6 2507 mention (b);
f7545552 2508 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638 2509 ui_out_text (uiout, "\nOld value = ");
fa4727a6 2510 watchpoint_value_print (bs->old_val, stb->stream);
8b93c638 2511 ui_out_field_stream (uiout, "old", stb);
8b93c638
JM
2512 ui_out_text (uiout, "\nNew value = ");
2513 }
2514 else
2515 {
4f8d1dc6 2516 mention (b);
9dc5e2a9 2517 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2518 ui_out_field_string
2519 (uiout, "reason",
2520 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
f7545552 2521 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
8b93c638
JM
2522 ui_out_text (uiout, "\nValue = ");
2523 }
fa4727a6 2524 watchpoint_value_print (b->val, stb->stream);
8b93c638 2525 ui_out_field_stream (uiout, "new", stb);
8b93c638 2526 ui_out_text (uiout, "\n");
f7545552 2527 result = PRINT_UNKNOWN;
e514a9d6 2528 break;
4ce44c66 2529
e514a9d6
JM
2530 /* Fall through, we don't deal with these types of breakpoints
2531 here. */
2532
11cf8741 2533 case bp_finish:
9dc5e2a9 2534 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2535 ui_out_field_string
2536 (uiout, "reason",
2537 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
f7545552 2538 result = PRINT_UNKNOWN;
8b93c638
JM
2539 break;
2540
e514a9d6 2541 case bp_until:
9dc5e2a9 2542 if (ui_out_is_mi_like_p (uiout))
1fbc2a49
NR
2543 ui_out_field_string
2544 (uiout, "reason",
2545 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
f7545552 2546 result = PRINT_UNKNOWN;
8b93c638
JM
2547 break;
2548
c2d11a7d 2549 case bp_none:
e514a9d6
JM
2550 case bp_longjmp:
2551 case bp_longjmp_resume:
2552 case bp_step_resume:
e514a9d6
JM
2553 case bp_watchpoint_scope:
2554 case bp_call_dummy:
1042e4c0 2555 case bp_tracepoint:
e514a9d6 2556 default:
f7545552
TT
2557 result = PRINT_UNKNOWN;
2558 break;
e514a9d6 2559 }
f7545552
TT
2560
2561 do_cleanups (old_chain);
2562 return result;
e514a9d6
JM
2563}
2564
2565/* Generic routine for printing messages indicating why we
2566 stopped. The behavior of this function depends on the value
2567 'print_it' in the bpstat structure. Under some circumstances we
2568 may decide not to print anything here and delegate the task to
2569 normal_stop(). */
2570
2571static enum print_stop_action
2572print_bp_stop_message (bpstat bs)
2573{
2574 switch (bs->print_it)
2575 {
2576 case print_it_noop:
2577 /* Nothing should be printed for this bpstat entry. */
2578 return PRINT_UNKNOWN;
2579 break;
2580
2581 case print_it_done:
2582 /* We still want to print the frame, but we already printed the
2583 relevant messages. */
2584 return PRINT_SRC_AND_LOC;
2585 break;
2586
2587 case print_it_normal:
4f8d1dc6 2588 {
89f9893c 2589 const struct bp_location *bl = bs->breakpoint_at;
4f8d1dc6
VP
2590 struct breakpoint *b = bl ? bl->owner : NULL;
2591
2592 /* Normal case. Call the breakpoint's print_it method, or
2593 print_it_typical. */
2594 /* FIXME: how breakpoint can ever be NULL here? */
2595 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
2596 return b->ops->print_it (b);
2597 else
2598 return print_it_typical (bs);
2599 }
2600 break;
3086aeae 2601
e514a9d6 2602 default:
8e65ff28 2603 internal_error (__FILE__, __LINE__,
e2e0b3e5 2604 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 2605 break;
c906108c 2606 }
c906108c
SS
2607}
2608
e514a9d6
JM
2609/* Print a message indicating what happened. This is called from
2610 normal_stop(). The input to this routine is the head of the bpstat
2611 list - a list of the eventpoints that caused this stop. This
2612 routine calls the generic print routine for printing a message
2613 about reasons for stopping. This will print (for example) the
2614 "Breakpoint n," part of the output. The return value of this
2615 routine is one of:
c906108c 2616
917317f4
JM
2617 PRINT_UNKNOWN: Means we printed nothing
2618 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
c5aa993b
JM
2619 code to print the location. An example is
2620 "Breakpoint 1, " which should be followed by
2621 the location.
917317f4 2622 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
2623 to also print the location part of the message.
2624 An example is the catch/throw messages, which
917317f4
JM
2625 don't require a location appended to the end.
2626 PRINT_NOTHING: We have done some printing and we don't need any
2627 further info to be printed.*/
c906108c 2628
917317f4 2629enum print_stop_action
fba45db2 2630bpstat_print (bpstat bs)
c906108c
SS
2631{
2632 int val;
c5aa993b 2633
c906108c 2634 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
2635 (Currently all watchpoints go on the bpstat whether hit or not.
2636 That probably could (should) be changed, provided care is taken
c906108c 2637 with respect to bpstat_explains_signal). */
e514a9d6
JM
2638 for (; bs; bs = bs->next)
2639 {
2640 val = print_bp_stop_message (bs);
2641 if (val == PRINT_SRC_ONLY
2642 || val == PRINT_SRC_AND_LOC
2643 || val == PRINT_NOTHING)
2644 return val;
2645 }
c906108c 2646
e514a9d6
JM
2647 /* We reached the end of the chain, or we got a null BS to start
2648 with and nothing was printed. */
917317f4 2649 return PRINT_UNKNOWN;
c906108c
SS
2650}
2651
2652/* Evaluate the expression EXP and return 1 if value is zero.
2653 This is used inside a catch_errors to evaluate the breakpoint condition.
2654 The argument is a "struct expression *" that has been cast to char * to
2655 make it pass through catch_errors. */
2656
2657static int
4efb68b1 2658breakpoint_cond_eval (void *exp)
c906108c 2659{
278cd55f 2660 struct value *mark = value_mark ();
c5aa993b 2661 int i = !value_true (evaluate_expression ((struct expression *) exp));
c906108c
SS
2662 value_free_to_mark (mark);
2663 return i;
2664}
2665
2666/* Allocate a new bpstat and chain it to the current one. */
2667
2668static bpstat
89f9893c 2669bpstat_alloc (const struct bp_location *bl, bpstat cbs /* Current "bs" value */ )
c906108c
SS
2670{
2671 bpstat bs;
2672
2673 bs = (bpstat) xmalloc (sizeof (*bs));
2674 cbs->next = bs;
4f8d1dc6 2675 bs->breakpoint_at = bl;
c906108c
SS
2676 /* If the condition is false, etc., don't do the commands. */
2677 bs->commands = NULL;
2678 bs->old_val = NULL;
2679 bs->print_it = print_it_normal;
2680 return bs;
2681}
2682\f
d983da9c
DJ
2683/* The target has stopped with waitstatus WS. Check if any hardware
2684 watchpoints have triggered, according to the target. */
2685
2686int
2687watchpoints_triggered (struct target_waitstatus *ws)
2688{
d92524f1 2689 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
2690 CORE_ADDR addr;
2691 struct breakpoint *b;
2692
2693 if (!stopped_by_watchpoint)
2694 {
2695 /* We were not stopped by a watchpoint. Mark all watchpoints
2696 as not triggered. */
2697 ALL_BREAKPOINTS (b)
2698 if (b->type == bp_hardware_watchpoint
2699 || b->type == bp_read_watchpoint
2700 || b->type == bp_access_watchpoint)
2701 b->watchpoint_triggered = watch_triggered_no;
2702
2703 return 0;
2704 }
2705
2706 if (!target_stopped_data_address (&current_target, &addr))
2707 {
2708 /* We were stopped by a watchpoint, but we don't know where.
2709 Mark all watchpoints as unknown. */
2710 ALL_BREAKPOINTS (b)
2711 if (b->type == bp_hardware_watchpoint
2712 || b->type == bp_read_watchpoint
2713 || b->type == bp_access_watchpoint)
2714 b->watchpoint_triggered = watch_triggered_unknown;
2715
2716 return stopped_by_watchpoint;
2717 }
2718
2719 /* The target could report the data address. Mark watchpoints
2720 affected by this data address as triggered, and all others as not
2721 triggered. */
2722
2723 ALL_BREAKPOINTS (b)
2724 if (b->type == bp_hardware_watchpoint
2725 || b->type == bp_read_watchpoint
2726 || b->type == bp_access_watchpoint)
2727 {
a5606eee 2728 struct bp_location *loc;
d983da9c
DJ
2729 struct value *v;
2730
2731 b->watchpoint_triggered = watch_triggered_no;
a5606eee
VP
2732 for (loc = b->loc; loc; loc = loc->next)
2733 /* Exact match not required. Within range is
2734 sufficient. */
5009afc5
AS
2735 if (target_watchpoint_addr_within_range (&current_target,
2736 addr, loc->address,
2737 loc->length))
a5606eee
VP
2738 {
2739 b->watchpoint_triggered = watch_triggered_yes;
2740 break;
2741 }
d983da9c
DJ
2742 }
2743
2744 return 1;
2745}
2746
c906108c
SS
2747/* Possible return values for watchpoint_check (this can't be an enum
2748 because of check_errors). */
2749/* The watchpoint has been deleted. */
2750#define WP_DELETED 1
2751/* The value has changed. */
2752#define WP_VALUE_CHANGED 2
2753/* The value has not changed. */
2754#define WP_VALUE_NOT_CHANGED 3
2755
2756#define BP_TEMPFLAG 1
2757#define BP_HARDWAREFLAG 2
2758
2759/* Check watchpoint condition. */
2760
2761static int
4efb68b1 2762watchpoint_check (void *p)
c906108c
SS
2763{
2764 bpstat bs = (bpstat) p;
2765 struct breakpoint *b;
2766 struct frame_info *fr;
2767 int within_current_scope;
2768
4f8d1dc6 2769 b = bs->breakpoint_at->owner;
c906108c
SS
2770
2771 if (b->exp_valid_block == NULL)
2772 within_current_scope = 1;
2773 else
2774 {
2775 /* There is no current frame at this moment. If we're going to have
c5aa993b
JM
2776 any chance of handling watchpoints on local variables, we'll need
2777 the frame chain (so we can determine if we're in scope). */
2778 reinit_frame_cache ();
101dcfbe 2779 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 2780 within_current_scope = (fr != NULL);
69fbadd5
DJ
2781
2782 /* If we've gotten confused in the unwinder, we might have
2783 returned a frame that can't describe this variable. */
2784 if (within_current_scope
7f0df278
DJ
2785 && (block_linkage_function (b->exp_valid_block)
2786 != get_frame_function (fr)))
69fbadd5
DJ
2787 within_current_scope = 0;
2788
c12260ac
CV
2789 /* in_function_epilogue_p() returns a non-zero value if we're still
2790 in the function but the stack frame has already been invalidated.
2791 Since we can't rely on the values of local variables after the
2792 stack has been destroyed, we are treating the watchpoint in that
a957e642
CV
2793 state as `not changed' without further checking.
2794
2795 vinschen/2003-09-04: The former implementation left out the case
2796 that the watchpoint frame couldn't be found by frame_find_by_id()
2797 because the current PC is currently in an epilogue. Calling
2798 gdbarch_in_function_epilogue_p() also when fr == NULL fixes that. */
fb14de7b
UW
2799 if (!within_current_scope || fr == get_current_frame ())
2800 {
2801 struct frame_info *frame = get_current_frame ();
2802 struct gdbarch *frame_arch = get_frame_arch (frame);
2803 CORE_ADDR frame_pc = get_frame_pc (frame);
2804
2805 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
2806 return WP_VALUE_NOT_CHANGED;
2807 }
a6fbcf2f 2808 if (fr && within_current_scope)
c906108c
SS
2809 /* If we end up stopping, the current frame will get selected
2810 in normal_stop. So this call to select_frame won't affect
2811 the user. */
0f7d239c 2812 select_frame (fr);
c906108c 2813 }
c5aa993b 2814
c906108c
SS
2815 if (within_current_scope)
2816 {
2817 /* We use value_{,free_to_}mark because it could be a
2818 *long* time before we return to the command level and
c5aa993b
JM
2819 call free_all_values. We can't call free_all_values because
2820 we might be in the middle of evaluating a function call. */
c906108c 2821
278cd55f 2822 struct value *mark = value_mark ();
fa4727a6
DJ
2823 struct value *new_val;
2824
2825 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
2826 if ((b->val != NULL) != (new_val != NULL)
2827 || (b->val != NULL && !value_equal (b->val, new_val)))
c906108c 2828 {
fa4727a6
DJ
2829 if (new_val != NULL)
2830 {
2831 release_value (new_val);
2832 value_free_to_mark (mark);
2833 }
c906108c
SS
2834 bs->old_val = b->val;
2835 b->val = new_val;
fa4727a6 2836 b->val_valid = 1;
c906108c
SS
2837 /* We will stop here */
2838 return WP_VALUE_CHANGED;
2839 }
2840 else
2841 {
2842 /* Nothing changed, don't do anything. */
2843 value_free_to_mark (mark);
2844 /* We won't stop here */
2845 return WP_VALUE_NOT_CHANGED;
2846 }
2847 }
2848 else
2849 {
2850 /* This seems like the only logical thing to do because
c5aa993b
JM
2851 if we temporarily ignored the watchpoint, then when
2852 we reenter the block in which it is valid it contains
2853 garbage (in the case of a function, it may have two
2854 garbage values, one before and one after the prologue).
2855 So we can't even detect the first assignment to it and
2856 watch after that (since the garbage may or may not equal
2857 the first value assigned). */
4ce44c66
JM
2858 /* We print all the stop information in print_it_typical(), but
2859 in this case, by the time we call print_it_typical() this bp
2860 will be deleted already. So we have no choice but print the
2861 information here. */
9dc5e2a9 2862 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2863 ui_out_field_string
2864 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 2865 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 2866 ui_out_field_int (uiout, "wpnum", b->number);
8b93c638
JM
2867 ui_out_text (uiout, " deleted because the program has left the block in\n\
2868which its expression is valid.\n");
4ce44c66 2869
c906108c 2870 if (b->related_breakpoint)
b5de0fa7
EZ
2871 b->related_breakpoint->disposition = disp_del_at_next_stop;
2872 b->disposition = disp_del_at_next_stop;
c906108c
SS
2873
2874 return WP_DELETED;
2875 }
2876}
2877
18a18393
VP
2878/* Return true if it looks like target has stopped due to hitting
2879 breakpoint location BL. This function does not check if we
2880 should stop, only if BL explains the stop. */
2881static int
2882bpstat_check_location (const struct bp_location *bl, CORE_ADDR bp_addr)
2883{
2884 struct breakpoint *b = bl->owner;
2885
2886 if (b->type != bp_watchpoint
2887 && b->type != bp_hardware_watchpoint
2888 && b->type != bp_read_watchpoint
2889 && b->type != bp_access_watchpoint
2890 && b->type != bp_hardware_breakpoint
fe798b75 2891 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393
VP
2892 {
2893 if (bl->address != bp_addr) /* address doesn't match */
2894 return 0;
2895 if (overlay_debugging /* unmapped overlay section */
2896 && section_is_overlay (bl->section)
2897 && !section_is_mapped (bl->section))
2898 return 0;
2899 }
2900
2901 /* Continuable hardware watchpoints are treated as non-existent if the
2902 reason we stopped wasn't a hardware watchpoint (we didn't stop on
2903 some data address). Otherwise gdb won't stop on a break instruction
2904 in the code (not from a breakpoint) when a hardware watchpoint has
2905 been defined. Also skip watchpoints which we know did not trigger
2906 (did not match the data address). */
2907
2908 if ((b->type == bp_hardware_watchpoint
2909 || b->type == bp_read_watchpoint
2910 || b->type == bp_access_watchpoint)
2911 && b->watchpoint_triggered == watch_triggered_no)
2912 return 0;
2913
2914 if (b->type == bp_hardware_breakpoint)
2915 {
2916 if (bl->address != bp_addr)
2917 return 0;
2918 if (overlay_debugging /* unmapped overlay section */
2919 && section_is_overlay (bl->section)
2920 && !section_is_mapped (bl->section))
2921 return 0;
2922 }
ce78b96d 2923
ce78b96d
JB
2924 if (b->type == bp_catchpoint)
2925 {
2926 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
2927 if (!b->ops->breakpoint_hit (b))
2928 return 0;
2929 }
2930
18a18393
VP
2931 return 1;
2932}
2933
2934/* If BS refers to a watchpoint, determine if the watched values
2935 has actually changed, and we should stop. If not, set BS->stop
2936 to 0. */
2937static void
2938bpstat_check_watchpoint (bpstat bs)
2939{
2940 const struct bp_location *bl = bs->breakpoint_at;
2941 struct breakpoint *b = bl->owner;
2942
2943 if (b->type == bp_watchpoint
2944 || b->type == bp_read_watchpoint
2945 || b->type == bp_access_watchpoint
2946 || b->type == bp_hardware_watchpoint)
2947 {
2948 CORE_ADDR addr;
2949 struct value *v;
2950 int must_check_value = 0;
2951
2952 if (b->type == bp_watchpoint)
2953 /* For a software watchpoint, we must always check the
2954 watched value. */
2955 must_check_value = 1;
2956 else if (b->watchpoint_triggered == watch_triggered_yes)
2957 /* We have a hardware watchpoint (read, write, or access)
2958 and the target earlier reported an address watched by
2959 this watchpoint. */
2960 must_check_value = 1;
2961 else if (b->watchpoint_triggered == watch_triggered_unknown
2962 && b->type == bp_hardware_watchpoint)
2963 /* We were stopped by a hardware watchpoint, but the target could
2964 not report the data address. We must check the watchpoint's
2965 value. Access and read watchpoints are out of luck; without
2966 a data address, we can't figure it out. */
2967 must_check_value = 1;
2968
2969 if (must_check_value)
2970 {
2971 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2972 b->number);
2973 struct cleanup *cleanups = make_cleanup (xfree, message);
2974 int e = catch_errors (watchpoint_check, bs, message,
2975 RETURN_MASK_ALL);
2976 do_cleanups (cleanups);
2977 switch (e)
2978 {
2979 case WP_DELETED:
2980 /* We've already printed what needs to be printed. */
2981 bs->print_it = print_it_done;
2982 /* Stop. */
2983 break;
2984 case WP_VALUE_CHANGED:
2985 if (b->type == bp_read_watchpoint)
2986 {
2987 /* Don't stop: read watchpoints shouldn't fire if
2988 the value has changed. This is for targets
2989 which cannot set read-only watchpoints. */
2990 bs->print_it = print_it_noop;
2991 bs->stop = 0;
2992 }
2993 break;
2994 case WP_VALUE_NOT_CHANGED:
2995 if (b->type == bp_hardware_watchpoint
2996 || b->type == bp_watchpoint)
2997 {
2998 /* Don't stop: write watchpoints shouldn't fire if
2999 the value hasn't changed. */
3000 bs->print_it = print_it_noop;
3001 bs->stop = 0;
3002 }
3003 /* Stop. */
3004 break;
3005 default:
3006 /* Can't happen. */
3007 case 0:
3008 /* Error from catch_errors. */
3009 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3010 if (b->related_breakpoint)
3011 b->related_breakpoint->disposition = disp_del_at_next_stop;
3012 b->disposition = disp_del_at_next_stop;
3013 /* We've already printed what needs to be printed. */
3014 bs->print_it = print_it_done;
3015 break;
3016 }
3017 }
3018 else /* must_check_value == 0 */
3019 {
3020 /* This is a case where some watchpoint(s) triggered, but
3021 not at the address of this watchpoint, or else no
3022 watchpoint triggered after all. So don't print
3023 anything for this watchpoint. */
3024 bs->print_it = print_it_noop;
3025 bs->stop = 0;
3026 }
3027 }
3028}
3029
3030
3031/* Check conditions (condition proper, frame, thread and ignore count)
3032 of breakpoint referred to by BS. If we should not stop for this
3033 breakpoint, set BS->stop to 0. */
3034static void
3035bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3036{
3037 int thread_id = pid_to_thread_id (ptid);
3038 const struct bp_location *bl = bs->breakpoint_at;
3039 struct breakpoint *b = bl->owner;
3040
3041 if (frame_id_p (b->frame_id)
3042 && !frame_id_eq (b->frame_id, get_frame_id (get_current_frame ())))
3043 bs->stop = 0;
3044 else if (bs->stop)
3045 {
3046 int value_is_zero = 0;
3047
3048 /* If this is a scope breakpoint, mark the associated
3049 watchpoint as triggered so that we will handle the
3050 out-of-scope event. We'll get to the watchpoint next
3051 iteration. */
3052 if (b->type == bp_watchpoint_scope)
3053 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
3054
3055 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
3056 {
c5bc3a77
DJ
3057 /* We use value_mark and value_free_to_mark because it could
3058 be a long time before we return to the command level and
3059 call free_all_values. We can't call free_all_values
3060 because we might be in the middle of evaluating a
3061 function call. */
3062 struct value *mark = value_mark ();
3063
18a18393
VP
3064 /* Need to select the frame, with all that implies
3065 so that the conditions will have the right context. */
3066 select_frame (get_current_frame ());
3067 value_is_zero
3068 = catch_errors (breakpoint_cond_eval, (bl->cond),
3069 "Error in testing breakpoint condition:\n",
3070 RETURN_MASK_ALL);
3071 /* FIXME-someday, should give breakpoint # */
c5bc3a77 3072 value_free_to_mark (mark);
18a18393
VP
3073 }
3074 if (bl->cond && value_is_zero)
3075 {
3076 bs->stop = 0;
3077 }
3078 else if (b->thread != -1 && b->thread != thread_id)
3079 {
3080 bs->stop = 0;
3081 }
3082 else if (b->ignore_count > 0)
3083 {
3084 b->ignore_count--;
3085 annotate_ignore_count_change ();
3086 bs->stop = 0;
3087 /* Increase the hit count even though we don't
3088 stop. */
3089 ++(b->hit_count);
3090 }
3091 }
3092}
3093
3094
9709f61c 3095/* Get a bpstat associated with having just stopped at address
d983da9c 3096 BP_ADDR in thread PTID.
c906108c 3097
d983da9c 3098 Determine whether we stopped at a breakpoint, etc, or whether we
c906108c
SS
3099 don't understand this stop. Result is a chain of bpstat's such that:
3100
c5aa993b 3101 if we don't understand the stop, the result is a null pointer.
c906108c 3102
c5aa993b 3103 if we understand why we stopped, the result is not null.
c906108c 3104
c5aa993b
JM
3105 Each element of the chain refers to a particular breakpoint or
3106 watchpoint at which we have stopped. (We may have stopped for
3107 several reasons concurrently.)
c906108c 3108
c5aa993b
JM
3109 Each element of the chain has valid next, breakpoint_at,
3110 commands, FIXME??? fields. */
c906108c
SS
3111
3112bpstat
d983da9c 3113bpstat_stop_status (CORE_ADDR bp_addr, ptid_t ptid)
c906108c 3114{
0d381245 3115 struct breakpoint *b = NULL;
89f9893c 3116 const struct bp_location *bl;
20874c92 3117 struct bp_location *loc;
c906108c
SS
3118 /* Root of the chain of bpstat's */
3119 struct bpstats root_bs[1];
3120 /* Pointer to the last thing in the chain currently. */
3121 bpstat bs = root_bs;
20874c92 3122 int ix;
d832cb68 3123 int need_remove_insert;
c906108c 3124
0d381245 3125 ALL_BP_LOCATIONS (bl)
c5aa993b 3126 {
0d381245
VP
3127 b = bl->owner;
3128 gdb_assert (b);
468d015d 3129 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
c5aa993b
JM
3130 continue;
3131
a5606eee
VP
3132 /* For hardware watchpoints, we look only at the first location.
3133 The watchpoint_check function will work on entire expression,
3134 not the individual locations. For read watchopints, the
3135 watchpoints_triggered function have checked all locations
3136 alrea
3137 */
3138 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3139 continue;
3140
18a18393
VP
3141 if (!bpstat_check_location (bl, bp_addr))
3142 continue;
3143
c5aa993b
JM
3144 /* Come here if it's a watchpoint, or if the break address matches */
3145
0d381245 3146 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
c5aa993b 3147
18a18393
VP
3148 /* Assume we stop. Should we find watchpoint that is not actually
3149 triggered, or if condition of breakpoint is false, we'll reset
3150 'stop' to 0. */
c5aa993b
JM
3151 bs->stop = 1;
3152 bs->print = 1;
3153
18a18393
VP
3154 bpstat_check_watchpoint (bs);
3155 if (!bs->stop)
3156 continue;
c5aa993b 3157
0fd8e87f
UW
3158 if (b->type == bp_thread_event || b->type == bp_overlay_event
3159 || b->type == bp_longjmp_master)
18a18393 3160 /* We do not stop for these. */
c5aa993b
JM
3161 bs->stop = 0;
3162 else
18a18393
VP
3163 bpstat_check_breakpoint_conditions (bs, ptid);
3164
3165 if (bs->stop)
c5aa993b 3166 {
18a18393 3167 ++(b->hit_count);
d983da9c 3168
18a18393
VP
3169 /* We will stop here */
3170 if (b->disposition == disp_disable)
c5aa993b 3171 {
514f746b
AR
3172 if (b->enable_state != bp_permanent)
3173 b->enable_state = bp_disabled;
b60e7edf 3174 update_global_location_list (0);
c5aa993b 3175 }
18a18393
VP
3176 if (b->silent)
3177 bs->print = 0;
3178 bs->commands = b->commands;
059fb39f
PM
3179 if (bs->commands
3180 && (strcmp ("silent", bs->commands->line) == 0
3181 || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
c5aa993b 3182 {
18a18393
VP
3183 bs->commands = bs->commands->next;
3184 bs->print = 0;
c5aa993b 3185 }
18a18393 3186 bs->commands = copy_command_lines (bs->commands);
c5aa993b 3187 }
18a18393 3188
c5aa993b
JM
3189 /* Print nothing for this entry if we dont stop or if we dont print. */
3190 if (bs->stop == 0 || bs->print == 0)
3191 bs->print_it = print_it_noop;
3192 }
c906108c 3193
20874c92
VP
3194 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3195 {
3196 if (loc->address == bp_addr)
3197 {
3198 bs = bpstat_alloc (loc, bs);
3199 /* For hits of moribund locations, we should just proceed. */
3200 bs->stop = 0;
3201 bs->print = 0;
3202 bs->print_it = print_it_noop;
3203 }
3204 }
3205
c906108c
SS
3206 bs->next = NULL; /* Terminate the chain */
3207 bs = root_bs->next; /* Re-grab the head of the chain */
3208
d983da9c
DJ
3209 /* If we aren't stopping, the value of some hardware watchpoint may
3210 not have changed, but the intermediate memory locations we are
3211 watching may have. Don't bother if we're stopping; this will get
3212 done later. */
3213 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3214 if (bs->stop)
3215 break;
3216
d832cb68 3217 need_remove_insert = 0;
d983da9c
DJ
3218 if (bs == NULL)
3219 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3220 if (!bs->stop
20874c92 3221 && bs->breakpoint_at->owner
d983da9c
DJ
3222 && (bs->breakpoint_at->owner->type == bp_hardware_watchpoint
3223 || bs->breakpoint_at->owner->type == bp_read_watchpoint
3224 || bs->breakpoint_at->owner->type == bp_access_watchpoint))
3225 {
a5606eee
VP
3226 /* remove/insert can invalidate bs->breakpoint_at, if this
3227 location is no longer used by the watchpoint. Prevent
3228 further code from trying to use it. */
3229 bs->breakpoint_at = NULL;
d832cb68 3230 need_remove_insert = 1;
d983da9c
DJ
3231 }
3232
d832cb68
DD
3233 if (need_remove_insert)
3234 {
3235 remove_breakpoints ();
3236 insert_breakpoints ();
3237 }
3238
d983da9c 3239 return root_bs->next;
c906108c
SS
3240}
3241\f
3242/* Tell what to do about this bpstat. */
3243struct bpstat_what
fba45db2 3244bpstat_what (bpstat bs)
c906108c
SS
3245{
3246 /* Classify each bpstat as one of the following. */
c5aa993b
JM
3247 enum class
3248 {
3249 /* This bpstat element has no effect on the main_action. */
3250 no_effect = 0,
3251
3252 /* There was a watchpoint, stop but don't print. */
3253 wp_silent,
c906108c 3254
c5aa993b
JM
3255 /* There was a watchpoint, stop and print. */
3256 wp_noisy,
c906108c 3257
c5aa993b
JM
3258 /* There was a breakpoint but we're not stopping. */
3259 bp_nostop,
c906108c 3260
c5aa993b
JM
3261 /* There was a breakpoint, stop but don't print. */
3262 bp_silent,
c906108c 3263
c5aa993b
JM
3264 /* There was a breakpoint, stop and print. */
3265 bp_noisy,
c906108c 3266
c5aa993b
JM
3267 /* We hit the longjmp breakpoint. */
3268 long_jump,
c906108c 3269
c5aa993b
JM
3270 /* We hit the longjmp_resume breakpoint. */
3271 long_resume,
c906108c 3272
c5aa993b
JM
3273 /* We hit the step_resume breakpoint. */
3274 step_resume,
c906108c 3275
c5aa993b
JM
3276 /* We hit the shared library event breakpoint. */
3277 shlib_event,
c906108c 3278
c5aa993b
JM
3279 /* This is just used to count how many enums there are. */
3280 class_last
c906108c
SS
3281 };
3282
3283 /* Here is the table which drives this routine. So that we can
3284 format it pretty, we define some abbreviations for the
3285 enum bpstat_what codes. */
3286#define kc BPSTAT_WHAT_KEEP_CHECKING
3287#define ss BPSTAT_WHAT_STOP_SILENT
3288#define sn BPSTAT_WHAT_STOP_NOISY
3289#define sgl BPSTAT_WHAT_SINGLE
3290#define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3291#define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
c906108c 3292#define sr BPSTAT_WHAT_STEP_RESUME
c906108c 3293#define shl BPSTAT_WHAT_CHECK_SHLIBS
c906108c
SS
3294
3295/* "Can't happen." Might want to print an error message.
3296 abort() is not out of the question, but chances are GDB is just
3297 a bit confused, not unusable. */
3298#define err BPSTAT_WHAT_STOP_NOISY
3299
3300 /* Given an old action and a class, come up with a new action. */
3301 /* One interesting property of this table is that wp_silent is the same
3302 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3303 after stopping, the check for whether to step over a breakpoint
3304 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
53a5351d
JM
3305 reference to how we stopped. We retain separate wp_silent and
3306 bp_silent codes in case we want to change that someday.
43ff13b4
JM
3307
3308 Another possibly interesting property of this table is that
3309 there's a partial ordering, priority-like, of the actions. Once
3310 you've decided that some action is appropriate, you'll never go
3311 back and decide something of a lower priority is better. The
3312 ordering is:
3313
d81191fc
JB
3314 kc < clr sgl shl slr sn sr ss
3315 sgl < shl slr sn sr ss
3316 slr < err shl sn sr ss
3317 clr < err shl sn sr ss
3318 ss < shl sn sr
3319 sn < shl sr
3320 shl < sr
4d5b2cd7 3321 sr <
c5aa993b 3322
43ff13b4
JM
3323 What I think this means is that we don't need a damned table
3324 here. If you just put the rows and columns in the right order,
3325 it'd look awfully regular. We could simply walk the bpstat list
3326 and choose the highest priority action we find, with a little
78b6a731 3327 logic to handle the 'err' cases. */
c906108c
SS
3328
3329 /* step_resume entries: a step resume breakpoint overrides another
3330 breakpoint of signal handling (see comment in wait_for_inferior
fcf70625 3331 at where we set the step_resume breakpoint). */
c906108c
SS
3332
3333 static const enum bpstat_what_main_action
c5aa993b
JM
3334 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3335 {
3336 /* old action */
d81191fc 3337 /* kc ss sn sgl slr clr sr shl
c5aa993b
JM
3338 */
3339/*no_effect */
d81191fc 3340 {kc, ss, sn, sgl, slr, clr, sr, shl},
c5aa993b 3341/*wp_silent */
d81191fc 3342 {ss, ss, sn, ss, ss, ss, sr, shl},
c5aa993b 3343/*wp_noisy */
d81191fc 3344 {sn, sn, sn, sn, sn, sn, sr, shl},
c5aa993b 3345/*bp_nostop */
d81191fc 3346 {sgl, ss, sn, sgl, slr, slr, sr, shl},
c5aa993b 3347/*bp_silent */
d81191fc 3348 {ss, ss, sn, ss, ss, ss, sr, shl},
c5aa993b 3349/*bp_noisy */
d81191fc 3350 {sn, sn, sn, sn, sn, sn, sr, shl},
c5aa993b 3351/*long_jump */
d81191fc 3352 {slr, ss, sn, slr, slr, err, sr, shl},
c5aa993b 3353/*long_resume */
d81191fc 3354 {clr, ss, sn, err, err, err, sr, shl},
c5aa993b 3355/*step_resume */
d81191fc 3356 {sr, sr, sr, sr, sr, sr, sr, sr},
c5aa993b 3357/*shlib */
d81191fc 3358 {shl, shl, shl, shl, shl, shl, sr, shl}
c5aa993b 3359 };
c906108c
SS
3360
3361#undef kc
3362#undef ss
3363#undef sn
3364#undef sgl
3365#undef slr
3366#undef clr
c906108c
SS
3367#undef err
3368#undef sr
3369#undef ts
3370#undef shl
c906108c
SS
3371 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3372 struct bpstat_what retval;
3373
3374 retval.call_dummy = 0;
3375 for (; bs != NULL; bs = bs->next)
3376 {
3377 enum class bs_class = no_effect;
3378 if (bs->breakpoint_at == NULL)
3379 /* I suspect this can happen if it was a momentary breakpoint
3380 which has since been deleted. */
3381 continue;
20874c92
VP
3382 if (bs->breakpoint_at->owner == NULL)
3383 bs_class = bp_nostop;
3384 else
4f8d1dc6 3385 switch (bs->breakpoint_at->owner->type)
c906108c
SS
3386 {
3387 case bp_none:
3388 continue;
3389
3390 case bp_breakpoint:
3391 case bp_hardware_breakpoint:
3392 case bp_until:
3393 case bp_finish:
3394 if (bs->stop)
3395 {
3396 if (bs->print)
3397 bs_class = bp_noisy;
3398 else
3399 bs_class = bp_silent;
3400 }
3401 else
3402 bs_class = bp_nostop;
3403 break;
3404 case bp_watchpoint:
3405 case bp_hardware_watchpoint:
3406 case bp_read_watchpoint:
3407 case bp_access_watchpoint:
3408 if (bs->stop)
3409 {
3410 if (bs->print)
3411 bs_class = wp_noisy;
3412 else
3413 bs_class = wp_silent;
3414 }
3415 else
53a5351d
JM
3416 /* There was a watchpoint, but we're not stopping.
3417 This requires no further action. */
c906108c
SS
3418 bs_class = no_effect;
3419 break;
3420 case bp_longjmp:
3421 bs_class = long_jump;
3422 break;
3423 case bp_longjmp_resume:
3424 bs_class = long_resume;
3425 break;
3426 case bp_step_resume:
3427 if (bs->stop)
3428 {
3429 bs_class = step_resume;
3430 }
3431 else
3432 /* It is for the wrong frame. */
3433 bs_class = bp_nostop;
3434 break;
c906108c
SS
3435 case bp_watchpoint_scope:
3436 bs_class = bp_nostop;
3437 break;
c5aa993b
JM
3438 case bp_shlib_event:
3439 bs_class = shlib_event;
3440 break;
c4093a6a 3441 case bp_thread_event:
1900040c 3442 case bp_overlay_event:
0fd8e87f 3443 case bp_longjmp_master:
c4093a6a
JM
3444 bs_class = bp_nostop;
3445 break;
ce78b96d 3446 case bp_catchpoint:
c5aa993b
JM
3447 if (bs->stop)
3448 {
3449 if (bs->print)
3450 bs_class = bp_noisy;
3451 else
3452 bs_class = bp_silent;
3453 }
3454 else
53a5351d
JM
3455 /* There was a catchpoint, but we're not stopping.
3456 This requires no further action. */
c5aa993b
JM
3457 bs_class = no_effect;
3458 break;
c906108c 3459 case bp_call_dummy:
53a5351d
JM
3460 /* Make sure the action is stop (silent or noisy),
3461 so infrun.c pops the dummy frame. */
c906108c
SS
3462 bs_class = bp_silent;
3463 retval.call_dummy = 1;
3464 break;
1042e4c0
SS
3465 case bp_tracepoint:
3466 /* Tracepoint hits should not be reported back to GDB, and
3467 if one got through somehow, it should have been filtered
3468 out already. */
3469 internal_error (__FILE__, __LINE__,
3470 _("bpstat_what: bp_tracepoint encountered"));
3471 break;
c906108c 3472 }
c5aa993b 3473 current_action = table[(int) bs_class][(int) current_action];
c906108c
SS
3474 }
3475 retval.main_action = current_action;
3476 return retval;
3477}
3478
3479/* Nonzero if we should step constantly (e.g. watchpoints on machines
3480 without hardware support). This isn't related to a specific bpstat,
3481 just to things like whether watchpoints are set. */
3482
c5aa993b 3483int
fba45db2 3484bpstat_should_step (void)
c906108c
SS
3485{
3486 struct breakpoint *b;
3487 ALL_BREAKPOINTS (b)
717a8278 3488 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 3489 return 1;
c906108c
SS
3490 return 0;
3491}
3492
c906108c 3493\f
c5aa993b 3494
0d381245
VP
3495static void print_breakpoint_location (struct breakpoint *b,
3496 struct bp_location *loc,
3497 char *wrap_indent,
3498 struct ui_stream *stb)
3499{
3500 if (b->source_file)
3501 {
3502 struct symbol *sym
3503 = find_pc_sect_function (loc->address, loc->section);
3504 if (sym)
3505 {
3506 ui_out_text (uiout, "in ");
3507 ui_out_field_string (uiout, "func",
3508 SYMBOL_PRINT_NAME (sym));
3509 ui_out_wrap_hint (uiout, wrap_indent);
3510 ui_out_text (uiout, " at ");
3511 }
3512 ui_out_field_string (uiout, "file", b->source_file);
3513 ui_out_text (uiout, ":");
3514
3515 if (ui_out_is_mi_like_p (uiout))
3516 {
3517 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3518 char *fullname = symtab_to_fullname (sal.symtab);
3519
3520 if (fullname)
3521 ui_out_field_string (uiout, "fullname", fullname);
3522 }
3523
3524 ui_out_field_int (uiout, "line", b->line_number);
3525 }
3526 else if (!b->loc)
3527 {
3528 ui_out_field_string (uiout, "pending", b->addr_string);
3529 }
3530 else
3531 {
3532 print_address_symbolic (loc->address, stb->stream, demangle, "");
3533 ui_out_field_stream (uiout, "at", stb);
3534 }
3535}
3536
c4093a6a 3537/* Print B to gdb_stdout. */
c906108c 3538static void
0d381245
VP
3539print_one_breakpoint_location (struct breakpoint *b,
3540 struct bp_location *loc,
3541 int loc_number,
3542 CORE_ADDR *last_addr)
c906108c 3543{
52f0bd74
AC
3544 struct command_line *l;
3545 struct symbol *sym;
c4093a6a
JM
3546 struct ep_type_description
3547 {
3548 enum bptype type;
3549 char *description;
3550 };
3551 static struct ep_type_description bptypes[] =
c906108c 3552 {
c5aa993b
JM
3553 {bp_none, "?deleted?"},
3554 {bp_breakpoint, "breakpoint"},
c906108c 3555 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
3556 {bp_until, "until"},
3557 {bp_finish, "finish"},
3558 {bp_watchpoint, "watchpoint"},
c906108c 3559 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
3560 {bp_read_watchpoint, "read watchpoint"},
3561 {bp_access_watchpoint, "acc watchpoint"},
3562 {bp_longjmp, "longjmp"},
3563 {bp_longjmp_resume, "longjmp resume"},
3564 {bp_step_resume, "step resume"},
c5aa993b
JM
3565 {bp_watchpoint_scope, "watchpoint scope"},
3566 {bp_call_dummy, "call dummy"},
3567 {bp_shlib_event, "shlib events"},
c4093a6a 3568 {bp_thread_event, "thread events"},
1900040c 3569 {bp_overlay_event, "overlay events"},
0fd8e87f 3570 {bp_longjmp_master, "longjmp master"},
ce78b96d 3571 {bp_catchpoint, "catchpoint"},
1042e4c0 3572 {bp_tracepoint, "tracepoint"},
c5aa993b 3573 };
c4093a6a 3574
c2c6d25f 3575 static char bpenables[] = "nynny";
c906108c 3576 char wrap_indent[80];
8b93c638
JM
3577 struct ui_stream *stb = ui_out_stream_new (uiout);
3578 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3b31d625 3579 struct cleanup *bkpt_chain;
c906108c 3580
0d381245
VP
3581 int header_of_multiple = 0;
3582 int part_of_multiple = (loc != NULL);
79a45b7d
TT
3583 struct value_print_options opts;
3584
3585 get_user_print_options (&opts);
0d381245
VP
3586
3587 gdb_assert (!loc || loc_number != 0);
3588 /* See comment in print_one_breakpoint concerning
3589 treatment of breakpoints with single disabled
3590 location. */
3591 if (loc == NULL
3592 && (b->loc != NULL
3593 && (b->loc->next != NULL || !b->loc->enabled)))
3594 header_of_multiple = 1;
3595 if (loc == NULL)
3596 loc = b->loc;
3597
c4093a6a 3598 annotate_record ();
3b31d625 3599 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
3600
3601 /* 1 */
3602 annotate_field (0);
0d381245
VP
3603 if (part_of_multiple)
3604 {
3605 char *formatted;
0c6773c1 3606 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
3607 ui_out_field_string (uiout, "number", formatted);
3608 xfree (formatted);
3609 }
3610 else
3611 {
3612 ui_out_field_int (uiout, "number", b->number);
3613 }
c4093a6a
JM
3614
3615 /* 2 */
3616 annotate_field (1);
0d381245
VP
3617 if (part_of_multiple)
3618 ui_out_field_skip (uiout, "type");
3619 else
3620 {
3621 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
3622 || ((int) b->type != bptypes[(int) b->type].type))
3623 internal_error (__FILE__, __LINE__,
3624 _("bptypes table does not describe type #%d."),
3625 (int) b->type);
3626 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3627 }
c4093a6a
JM
3628
3629 /* 3 */
3630 annotate_field (2);
0d381245
VP
3631 if (part_of_multiple)
3632 ui_out_field_skip (uiout, "disp");
3633 else
2cec12e5 3634 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 3635
c4093a6a
JM
3636
3637 /* 4 */
3638 annotate_field (3);
0d381245 3639 if (part_of_multiple)
54e52265 3640 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 3641 else
54e52265
VP
3642 ui_out_field_fmt (uiout, "enabled", "%c",
3643 bpenables[(int) b->enable_state]);
3644 ui_out_spaces (uiout, 2);
0d381245 3645
c4093a6a
JM
3646
3647 /* 5 and 6 */
3648 strcpy (wrap_indent, " ");
79a45b7d 3649 if (opts.addressprint)
75ac9d7b 3650 {
17a912b6 3651 if (gdbarch_addr_bit (current_gdbarch) <= 32)
75ac9d7b
MS
3652 strcat (wrap_indent, " ");
3653 else
3654 strcat (wrap_indent, " ");
3655 }
c906108c 3656
3086aeae 3657 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245
VP
3658 {
3659 /* Although the print_one can possibly print
3660 all locations, calling it here is not likely
3661 to get any nice result. So, make sure there's
3662 just one location. */
3663 gdb_assert (b->loc == NULL || b->loc->next == NULL);
3664 b->ops->print_one (b, last_addr);
3665 }
3086aeae
DJ
3666 else
3667 switch (b->type)
3668 {
3669 case bp_none:
3670 internal_error (__FILE__, __LINE__,
e2e0b3e5 3671 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 3672 break;
c906108c 3673
3086aeae
DJ
3674 case bp_watchpoint:
3675 case bp_hardware_watchpoint:
3676 case bp_read_watchpoint:
3677 case bp_access_watchpoint:
3678 /* Field 4, the address, is omitted (which makes the columns
3679 not line up too nicely with the headers, but the effect
3680 is relatively readable). */
79a45b7d 3681 if (opts.addressprint)
3086aeae
DJ
3682 ui_out_field_skip (uiout, "addr");
3683 annotate_field (5);
fa8a61dc 3684 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
3685 break;
3686
3086aeae
DJ
3687 case bp_breakpoint:
3688 case bp_hardware_breakpoint:
3689 case bp_until:
3690 case bp_finish:
3691 case bp_longjmp:
3692 case bp_longjmp_resume:
3693 case bp_step_resume:
3086aeae
DJ
3694 case bp_watchpoint_scope:
3695 case bp_call_dummy:
3696 case bp_shlib_event:
3697 case bp_thread_event:
3698 case bp_overlay_event:
0fd8e87f 3699 case bp_longjmp_master:
1042e4c0 3700 case bp_tracepoint:
79a45b7d 3701 if (opts.addressprint)
3086aeae
DJ
3702 {
3703 annotate_field (4);
54e52265 3704 if (header_of_multiple)
0d381245 3705 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 3706 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 3707 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 3708 else
0d381245 3709 ui_out_field_core_addr (uiout, "addr", loc->address);
3086aeae
DJ
3710 }
3711 annotate_field (5);
0d381245
VP
3712 if (!header_of_multiple)
3713 print_breakpoint_location (b, loc, wrap_indent, stb);
3714 if (b->loc)
3715 *last_addr = b->loc->address;
3086aeae
DJ
3716 break;
3717 }
c906108c 3718
4a306c9a 3719 if (!part_of_multiple)
c4093a6a 3720 {
4a306c9a
JB
3721 if (b->thread != -1)
3722 {
3723 /* FIXME: This seems to be redundant and lost here; see the
3724 "stop only in" line a little further down. */
3725 ui_out_text (uiout, " thread ");
3726 ui_out_field_int (uiout, "thread", b->thread);
3727 }
3728 else if (b->task != 0)
3729 {
3730 ui_out_text (uiout, " task ");
3731 ui_out_field_int (uiout, "task", b->task);
3732 }
c4093a6a
JM
3733 }
3734
8b93c638 3735 ui_out_text (uiout, "\n");
c4093a6a 3736
0d381245 3737 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
3738 {
3739 annotate_field (6);
8b93c638 3740 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
3741 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
3742 the frame ID. */
d0a55772 3743 ui_out_field_core_addr (uiout, "frame", b->frame_id.stack_addr);
8b93c638 3744 ui_out_text (uiout, "\n");
c4093a6a
JM
3745 }
3746
0d381245 3747 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 3748 {
f7f9143b
JB
3749 /* We do not print the condition for Ada exception catchpoints
3750 because the condition is an internal implementation detail
3751 that we do not want to expose to the user. */
c4093a6a 3752 annotate_field (7);
1042e4c0
SS
3753 if (b->type == bp_tracepoint)
3754 ui_out_text (uiout, "\ttrace only if ");
3755 else
3756 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
3757 ui_out_field_string (uiout, "cond", b->cond_string);
3758 ui_out_text (uiout, "\n");
3759 }
3760
0d381245 3761 if (!part_of_multiple && b->thread != -1)
c4093a6a
JM
3762 {
3763 /* FIXME should make an annotation for this */
8b93c638
JM
3764 ui_out_text (uiout, "\tstop only in thread ");
3765 ui_out_field_int (uiout, "thread", b->thread);
3766 ui_out_text (uiout, "\n");
c4093a6a
JM
3767 }
3768
63c715c6 3769 if (!part_of_multiple && b->hit_count)
c4093a6a
JM
3770 {
3771 /* FIXME should make an annotation for this */
8b93c638
JM
3772 if (ep_is_catchpoint (b))
3773 ui_out_text (uiout, "\tcatchpoint");
3774 else
3775 ui_out_text (uiout, "\tbreakpoint");
3776 ui_out_text (uiout, " already hit ");
3777 ui_out_field_int (uiout, "times", b->hit_count);
3778 if (b->hit_count == 1)
3779 ui_out_text (uiout, " time\n");
3780 else
3781 ui_out_text (uiout, " times\n");
c4093a6a
JM
3782 }
3783
fb40c209
AC
3784 /* Output the count also if it is zero, but only if this is
3785 mi. FIXME: Should have a better test for this. */
9dc5e2a9 3786 if (ui_out_is_mi_like_p (uiout))
63c715c6 3787 if (!part_of_multiple && b->hit_count == 0)
fb40c209 3788 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 3789
0d381245 3790 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
3791 {
3792 annotate_field (8);
8b93c638
JM
3793 ui_out_text (uiout, "\tignore next ");
3794 ui_out_field_int (uiout, "ignore", b->ignore_count);
3795 ui_out_text (uiout, " hits\n");
c4093a6a 3796 }
059fb39f
PM
3797
3798 l = b->commands;
3799 if (!part_of_multiple && l)
c4093a6a 3800 {
3b31d625
EZ
3801 struct cleanup *script_chain;
3802
c4093a6a 3803 annotate_field (9);
3b31d625 3804 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 3805 print_command_lines (uiout, l, 4);
3b31d625 3806 do_cleanups (script_chain);
c4093a6a 3807 }
d24317b4 3808
1042e4c0
SS
3809 if (!part_of_multiple && b->pass_count)
3810 {
3811 annotate_field (10);
3812 ui_out_text (uiout, "\tpass count ");
3813 ui_out_field_int (uiout, "pass", b->pass_count);
3814 ui_out_text (uiout, " \n");
3815 }
3816
3817 if (!part_of_multiple && b->step_count)
3818 {
3819 annotate_field (11);
3820 ui_out_text (uiout, "\tstep count ");
3821 ui_out_field_int (uiout, "step", b->step_count);
3822 ui_out_text (uiout, " \n");
3823 }
3824
3825 if (!part_of_multiple && b->actions)
3826 {
3827 struct action_line *action;
3828 annotate_field (12);
3829 for (action = b->actions; action; action = action->next)
3830 {
3831 ui_out_text (uiout, " A\t");
3832 ui_out_text (uiout, action->action);
3833 ui_out_text (uiout, "\n");
3834 }
3835 }
3836
d24317b4
VP
3837 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
3838 {
3839 if (b->addr_string)
3840 ui_out_field_string (uiout, "original-location", b->addr_string);
3841 else if (b->exp_string)
3842 ui_out_field_string (uiout, "original-location", b->exp_string);
3843 }
3844
3b31d625 3845 do_cleanups (bkpt_chain);
8b93c638 3846 do_cleanups (old_chain);
c4093a6a 3847}
c5aa993b 3848
0d381245
VP
3849static void
3850print_one_breakpoint (struct breakpoint *b,
3851 CORE_ADDR *last_addr)
3852{
3853 print_one_breakpoint_location (b, NULL, 0, last_addr);
3854
3855 /* If this breakpoint has custom print function,
3856 it's already printed. Otherwise, print individual
3857 locations, if any. */
3858 if (b->ops == NULL || b->ops->print_one == NULL)
3859 {
3860 /* If breakpoint has a single location that is
3861 disabled, we print it as if it had
3862 several locations, since otherwise it's hard to
3863 represent "breakpoint enabled, location disabled"
a5606eee
VP
3864 situation.
3865 Note that while hardware watchpoints have
3866 several locations internally, that's no a property
3867 exposed to user. */
0d381245 3868 if (b->loc
a5606eee 3869 && !is_hardware_watchpoint (b)
0d381245 3870 && (b->loc->next || !b->loc->enabled)
a5606eee 3871 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
3872 {
3873 struct bp_location *loc;
3874 int n = 1;
3875 for (loc = b->loc; loc; loc = loc->next, ++n)
3876 print_one_breakpoint_location (b, loc, n, last_addr);
3877 }
3878 }
3879}
3880
3881
c4093a6a
JM
3882struct captured_breakpoint_query_args
3883 {
3884 int bnum;
3885 };
c5aa993b 3886
c4093a6a 3887static int
2b65245e 3888do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
3889{
3890 struct captured_breakpoint_query_args *args = data;
52f0bd74 3891 struct breakpoint *b;
c4093a6a
JM
3892 CORE_ADDR dummy_addr = 0;
3893 ALL_BREAKPOINTS (b)
3894 {
3895 if (args->bnum == b->number)
c5aa993b 3896 {
c4093a6a
JM
3897 print_one_breakpoint (b, &dummy_addr);
3898 return GDB_RC_OK;
c5aa993b 3899 }
c4093a6a
JM
3900 }
3901 return GDB_RC_NONE;
3902}
c5aa993b 3903
c4093a6a 3904enum gdb_rc
ce43223b 3905gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
c4093a6a
JM
3906{
3907 struct captured_breakpoint_query_args args;
3908 args.bnum = bnum;
3909 /* For the moment we don't trust print_one_breakpoint() to not throw
3910 an error. */
b0b13bb4
DJ
3911 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
3912 error_message, RETURN_MASK_ALL) < 0)
3913 return GDB_RC_FAIL;
3914 else
3915 return GDB_RC_OK;
c4093a6a 3916}
c5aa993b 3917
7f3b0473
AC
3918/* Return non-zero if B is user settable (breakpoints, watchpoints,
3919 catchpoints, et.al.). */
3920
3921static int
3922user_settable_breakpoint (const struct breakpoint *b)
3923{
3924 return (b->type == bp_breakpoint
ce78b96d 3925 || b->type == bp_catchpoint
7f3b0473 3926 || b->type == bp_hardware_breakpoint
1042e4c0 3927 || b->type == bp_tracepoint
7f3b0473
AC
3928 || b->type == bp_watchpoint
3929 || b->type == bp_read_watchpoint
3930 || b->type == bp_access_watchpoint
3931 || b->type == bp_hardware_watchpoint);
3932}
3933
3934/* Print information on user settable breakpoint (watchpoint, etc)
3935 number BNUM. If BNUM is -1 print all user settable breakpoints.
3936 If ALLFLAG is non-zero, include non- user settable breakpoints. */
c906108c 3937
c4093a6a 3938static void
fba45db2 3939breakpoint_1 (int bnum, int allflag)
c4093a6a 3940{
52f0bd74 3941 struct breakpoint *b;
c4093a6a 3942 CORE_ADDR last_addr = (CORE_ADDR) -1;
7f3b0473 3943 int nr_printable_breakpoints;
3b31d625 3944 struct cleanup *bkpttbl_chain;
79a45b7d 3945 struct value_print_options opts;
c4093a6a 3946
79a45b7d
TT
3947 get_user_print_options (&opts);
3948
7f3b0473
AC
3949 /* Compute the number of rows in the table. */
3950 nr_printable_breakpoints = 0;
3951 ALL_BREAKPOINTS (b)
3952 if (bnum == -1
3953 || bnum == b->number)
3954 {
3955 if (allflag || user_settable_breakpoint (b))
3956 nr_printable_breakpoints++;
3957 }
3958
79a45b7d 3959 if (opts.addressprint)
3b31d625
EZ
3960 bkpttbl_chain
3961 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
3962 "BreakpointTable");
8b93c638 3963 else
3b31d625
EZ
3964 bkpttbl_chain
3965 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
3966 "BreakpointTable");
8b93c638 3967
7f3b0473 3968 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
3969 annotate_breakpoints_headers ();
3970 if (nr_printable_breakpoints > 0)
3971 annotate_field (0);
0d381245 3972 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
3973 if (nr_printable_breakpoints > 0)
3974 annotate_field (1);
3975 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
3976 if (nr_printable_breakpoints > 0)
3977 annotate_field (2);
3978 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
3979 if (nr_printable_breakpoints > 0)
3980 annotate_field (3);
54e52265 3981 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 3982 if (opts.addressprint)
7f3b0473 3983 {
d7faa9e7
AC
3984 if (nr_printable_breakpoints > 0)
3985 annotate_field (4);
17a912b6 3986 if (gdbarch_addr_bit (current_gdbarch) <= 32)
b25959ec 3987 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
7f3b0473 3988 else
b25959ec 3989 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
7f3b0473 3990 }
d7faa9e7
AC
3991 if (nr_printable_breakpoints > 0)
3992 annotate_field (5);
3993 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
3994 ui_out_table_body (uiout);
3995 if (nr_printable_breakpoints > 0)
3996 annotate_breakpoints_table ();
7f3b0473 3997
c4093a6a
JM
3998 ALL_BREAKPOINTS (b)
3999 if (bnum == -1
4000 || bnum == b->number)
4001 {
4002 /* We only print out user settable breakpoints unless the
4003 allflag is set. */
7f3b0473
AC
4004 if (allflag || user_settable_breakpoint (b))
4005 print_one_breakpoint (b, &last_addr);
c4093a6a
JM
4006 }
4007
3b31d625 4008 do_cleanups (bkpttbl_chain);
698384cd 4009
7f3b0473 4010 if (nr_printable_breakpoints == 0)
c906108c 4011 {
8b93c638
JM
4012 if (bnum == -1)
4013 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
4014 else
4015 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
4016 bnum);
c906108c
SS
4017 }
4018 else
c4093a6a
JM
4019 {
4020 /* Compare against (CORE_ADDR)-1 in case some compiler decides
4021 that a comparison of an unsigned with -1 is always false. */
d1aa2f50 4022 if (last_addr != (CORE_ADDR) -1 && !server_command)
8b9b9e1a 4023 set_next_address (current_gdbarch, last_addr);
c4093a6a 4024 }
c906108c 4025
c4093a6a
JM
4026 /* FIXME? Should this be moved up so that it is only called when
4027 there have been breakpoints? */
c906108c
SS
4028 annotate_breakpoints_table_end ();
4029}
4030
c906108c 4031static void
fba45db2 4032breakpoints_info (char *bnum_exp, int from_tty)
c906108c
SS
4033{
4034 int bnum = -1;
4035
4036 if (bnum_exp)
bb518678 4037 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4038
4039 breakpoint_1 (bnum, 0);
4040}
4041
7a292a7a 4042static void
fba45db2 4043maintenance_info_breakpoints (char *bnum_exp, int from_tty)
c906108c
SS
4044{
4045 int bnum = -1;
4046
4047 if (bnum_exp)
bb518678 4048 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4049
4050 breakpoint_1 (bnum, 1);
4051}
4052
0d381245 4053static int
714835d5
UW
4054breakpoint_has_pc (struct breakpoint *b,
4055 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
4056{
4057 struct bp_location *bl = b->loc;
4058 for (; bl; bl = bl->next)
4059 {
4060 if (bl->address == pc
4061 && (!overlay_debugging || bl->section == section))
4062 return 1;
4063 }
4064 return 0;
4065}
4066
c906108c
SS
4067/* Print a message describing any breakpoints set at PC. */
4068
4069static void
714835d5
UW
4070describe_other_breakpoints (CORE_ADDR pc, struct obj_section *section,
4071 int thread)
c906108c 4072{
52f0bd74
AC
4073 int others = 0;
4074 struct breakpoint *b;
c906108c
SS
4075
4076 ALL_BREAKPOINTS (b)
0d381245 4077 others += breakpoint_has_pc (b, pc, section);
c906108c
SS
4078 if (others > 0)
4079 {
a3f17187
AC
4080 if (others == 1)
4081 printf_filtered (_("Note: breakpoint "));
4082 else /* if (others == ???) */
4083 printf_filtered (_("Note: breakpoints "));
c906108c 4084 ALL_BREAKPOINTS (b)
0d381245
VP
4085 if (breakpoint_has_pc (b, pc, section))
4086 {
4087 others--;
4088 printf_filtered ("%d", b->number);
4089 if (b->thread == -1 && thread != -1)
4090 printf_filtered (" (all threads)");
4091 else if (b->thread != -1)
4092 printf_filtered (" (thread %d)", b->thread);
4093 printf_filtered ("%s%s ",
059fb39f
PM
4094 ((b->enable_state == bp_disabled
4095 || b->enable_state == bp_call_disabled)
0d381245
VP
4096 ? " (disabled)"
4097 : b->enable_state == bp_permanent
4098 ? " (permanent)"
4099 : ""),
4100 (others > 1) ? ","
4101 : ((others == 1) ? " and" : ""));
4102 }
a3f17187 4103 printf_filtered (_("also set at pc "));
ed49a04f 4104 fputs_filtered (paddress (pc), gdb_stdout);
c906108c
SS
4105 printf_filtered (".\n");
4106 }
4107}
4108\f
4109/* Set the default place to put a breakpoint
4110 for the `break' command with no arguments. */
4111
4112void
fba45db2
KB
4113set_default_breakpoint (int valid, CORE_ADDR addr, struct symtab *symtab,
4114 int line)
c906108c
SS
4115{
4116 default_breakpoint_valid = valid;
4117 default_breakpoint_address = addr;
4118 default_breakpoint_symtab = symtab;
4119 default_breakpoint_line = line;
4120}
4121
e4f237da
KB
4122/* Return true iff it is meaningful to use the address member of
4123 BPT. For some breakpoint types, the address member is irrelevant
4124 and it makes no sense to attempt to compare it to other addresses
4125 (or use it for any other purpose either).
4126
4127 More specifically, each of the following breakpoint types will always
4128 have a zero valued address and we don't want check_duplicates() to mark
4129 breakpoints of any of these types to be a duplicate of an actual
4130 breakpoint at address zero:
4131
4132 bp_watchpoint
4133 bp_hardware_watchpoint
4134 bp_read_watchpoint
4135 bp_access_watchpoint
fe798b75 4136 bp_catchpoint */
e4f237da
KB
4137
4138static int
4139breakpoint_address_is_meaningful (struct breakpoint *bpt)
4140{
4141 enum bptype type = bpt->type;
4142
4143 return (type != bp_watchpoint
4144 && type != bp_hardware_watchpoint
4145 && type != bp_read_watchpoint
4146 && type != bp_access_watchpoint
fe798b75 4147 && type != bp_catchpoint);
e4f237da
KB
4148}
4149
9f60f21b 4150/* Rescan breakpoints at the same address and section as BPT,
c906108c 4151 marking the first one as "first" and any others as "duplicates".
c2c6d25f 4152 This is so that the bpt instruction is only inserted once.
9f60f21b
JB
4153 If we have a permanent breakpoint at the same place as BPT, make
4154 that one the official one, and the rest as duplicates. */
c906108c
SS
4155
4156static void
714835d5 4157check_duplicates_for (CORE_ADDR address, struct obj_section *section)
c906108c 4158{
075f6582 4159 struct bp_location *b;
52f0bd74 4160 int count = 0;
075f6582 4161 struct bp_location *perm_bp = 0;
c906108c 4162
075f6582
DJ
4163 ALL_BP_LOCATIONS (b)
4164 if (b->owner->enable_state != bp_disabled
075f6582 4165 && b->owner->enable_state != bp_call_disabled
0d381245
VP
4166 && b->enabled
4167 && !b->shlib_disabled
075f6582
DJ
4168 && b->address == address /* address / overlay match */
4169 && (!overlay_debugging || b->section == section)
4170 && breakpoint_address_is_meaningful (b->owner))
c5aa993b 4171 {
c2c6d25f 4172 /* Have we found a permanent breakpoint? */
075f6582 4173 if (b->owner->enable_state == bp_permanent)
c2c6d25f
JM
4174 {
4175 perm_bp = b;
4176 break;
4177 }
4178
c5aa993b 4179 count++;
075f6582 4180 b->duplicate = count > 1;
c5aa993b 4181 }
c2c6d25f
JM
4182
4183 /* If we found a permanent breakpoint at this address, go over the
aae64587
PA
4184 list again and declare all the other breakpoints there (except
4185 other permanent breakpoints) to be the duplicates. */
c2c6d25f
JM
4186 if (perm_bp)
4187 {
075f6582 4188 perm_bp->duplicate = 0;
c2c6d25f
JM
4189
4190 /* Permanent breakpoint should always be inserted. */
075f6582 4191 if (! perm_bp->inserted)
8e65ff28 4192 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
4193 _("allegedly permanent breakpoint is not "
4194 "actually inserted"));
c2c6d25f 4195
075f6582 4196 ALL_BP_LOCATIONS (b)
c2c6d25f
JM
4197 if (b != perm_bp)
4198 {
aae64587
PA
4199 if (b->owner->enable_state != bp_permanent
4200 && b->owner->enable_state != bp_disabled
075f6582 4201 && b->owner->enable_state != bp_call_disabled
0d381245 4202 && b->enabled && !b->shlib_disabled
075f6582
DJ
4203 && b->address == address /* address / overlay match */
4204 && (!overlay_debugging || b->section == section)
4205 && breakpoint_address_is_meaningful (b->owner))
4206 {
4207 if (b->inserted)
4208 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
4209 _("another breakpoint was inserted on top of "
4210 "a permanent breakpoint"));
075f6582
DJ
4211
4212 b->duplicate = 1;
4213 }
c2c6d25f
JM
4214 }
4215 }
c906108c
SS
4216}
4217
0d381245
VP
4218static void
4219check_duplicates (struct breakpoint *bpt)
4220{
4221 struct bp_location *bl = bpt->loc;
4222
4223 if (! breakpoint_address_is_meaningful (bpt))
4224 return;
4225
4226 for (; bl; bl = bl->next)
4227 check_duplicates_for (bl->address, bl->section);
4228}
4229
76897487
KB
4230static void
4231breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4232 int bnum, int have_bnum)
4233{
4234 char astr1[40];
4235 char astr2[40];
4236
bb599908
PH
4237 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4238 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 4239 if (have_bnum)
8a3fe4f8 4240 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
4241 bnum, astr1, astr2);
4242 else
8a3fe4f8 4243 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
4244}
4245
4246/* Adjust a breakpoint's address to account for architectural constraints
4247 on breakpoint placement. Return the adjusted address. Note: Very
4248 few targets require this kind of adjustment. For most targets,
4249 this function is simply the identity function. */
4250
4251static CORE_ADDR
88f7da05 4252adjust_breakpoint_address (CORE_ADDR bpaddr, enum bptype bptype)
76897487
KB
4253{
4254 if (!gdbarch_adjust_breakpoint_address_p (current_gdbarch))
4255 {
4256 /* Very few targets need any kind of breakpoint adjustment. */
4257 return bpaddr;
4258 }
88f7da05
KB
4259 else if (bptype == bp_watchpoint
4260 || bptype == bp_hardware_watchpoint
4261 || bptype == bp_read_watchpoint
4262 || bptype == bp_access_watchpoint
fe798b75 4263 || bptype == bp_catchpoint)
88f7da05
KB
4264 {
4265 /* Watchpoints and the various bp_catch_* eventpoints should not
4266 have their addresses modified. */
4267 return bpaddr;
4268 }
76897487
KB
4269 else
4270 {
4271 CORE_ADDR adjusted_bpaddr;
4272
4273 /* Some targets have architectural constraints on the placement
4274 of breakpoint instructions. Obtain the adjusted address. */
4275 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (current_gdbarch,
4276 bpaddr);
4277
4278 /* An adjusted breakpoint address can significantly alter
4279 a user's expectations. Print a warning if an adjustment
4280 is required. */
4281 if (adjusted_bpaddr != bpaddr)
4282 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4283
4284 return adjusted_bpaddr;
4285 }
4286}
4287
7cc221ef
DJ
4288/* Allocate a struct bp_location. */
4289
26bb91f3 4290static struct bp_location *
39d61571 4291allocate_bp_location (struct breakpoint *bpt)
7cc221ef
DJ
4292{
4293 struct bp_location *loc, *loc_p;
4294
4295 loc = xmalloc (sizeof (struct bp_location));
4296 memset (loc, 0, sizeof (*loc));
4297
e049a4b5 4298 loc->owner = bpt;
511a6cd4 4299 loc->cond = NULL;
0d381245
VP
4300 loc->shlib_disabled = 0;
4301 loc->enabled = 1;
e049a4b5 4302
39d61571 4303 switch (bpt->type)
e049a4b5
DJ
4304 {
4305 case bp_breakpoint:
1042e4c0 4306 case bp_tracepoint:
e049a4b5
DJ
4307 case bp_until:
4308 case bp_finish:
4309 case bp_longjmp:
4310 case bp_longjmp_resume:
4311 case bp_step_resume:
e049a4b5
DJ
4312 case bp_watchpoint_scope:
4313 case bp_call_dummy:
4314 case bp_shlib_event:
4315 case bp_thread_event:
4316 case bp_overlay_event:
0fd8e87f 4317 case bp_longjmp_master:
e049a4b5
DJ
4318 loc->loc_type = bp_loc_software_breakpoint;
4319 break;
4320 case bp_hardware_breakpoint:
4321 loc->loc_type = bp_loc_hardware_breakpoint;
4322 break;
4323 case bp_hardware_watchpoint:
4324 case bp_read_watchpoint:
4325 case bp_access_watchpoint:
4326 loc->loc_type = bp_loc_hardware_watchpoint;
4327 break;
4328 case bp_watchpoint:
ce78b96d 4329 case bp_catchpoint:
e049a4b5
DJ
4330 loc->loc_type = bp_loc_other;
4331 break;
4332 default:
e2e0b3e5 4333 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
4334 }
4335
7cc221ef
DJ
4336 return loc;
4337}
4338
fe3f5fa8
VP
4339static void free_bp_location (struct bp_location *loc)
4340{
4341 if (loc->cond)
4342 xfree (loc->cond);
74960c60
VP
4343
4344 if (loc->function_name)
4345 xfree (loc->function_name);
4346
fe3f5fa8
VP
4347 xfree (loc);
4348}
4349
0d381245
VP
4350/* Helper to set_raw_breakpoint below. Creates a breakpoint
4351 that has type BPTYPE and has no locations as yet. */
c906108c 4352
c40e75cd 4353static struct breakpoint *
0d381245 4354set_raw_breakpoint_without_location (enum bptype bptype)
c906108c 4355{
52f0bd74 4356 struct breakpoint *b, *b1;
c906108c
SS
4357
4358 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4359 memset (b, 0, sizeof (*b));
2219d63c 4360
4d28f7a8 4361 b->type = bptype;
c906108c
SS
4362 b->language = current_language->la_language;
4363 b->input_radix = input_radix;
4364 b->thread = -1;
b5de0fa7 4365 b->enable_state = bp_enabled;
c906108c
SS
4366 b->next = 0;
4367 b->silent = 0;
4368 b->ignore_count = 0;
4369 b->commands = NULL;
818dd999 4370 b->frame_id = null_frame_id;
3a3e9ee3 4371 b->forked_inferior_pid = null_ptid;
c906108c 4372 b->exec_pathname = NULL;
3086aeae 4373 b->ops = NULL;
0d381245 4374 b->condition_not_parsed = 0;
c906108c
SS
4375
4376 /* Add this breakpoint to the end of the chain
4377 so that a list of breakpoints will come out in order
4378 of increasing numbers. */
4379
4380 b1 = breakpoint_chain;
4381 if (b1 == 0)
4382 breakpoint_chain = b;
4383 else
4384 {
4385 while (b1->next)
4386 b1 = b1->next;
4387 b1->next = b;
4388 }
0d381245
VP
4389 return b;
4390}
4391
4392/* Initialize loc->function_name. */
4393static void
4394set_breakpoint_location_function (struct bp_location *loc)
4395{
4396 if (loc->owner->type == bp_breakpoint
1042e4c0
SS
4397 || loc->owner->type == bp_hardware_breakpoint
4398 || loc->owner->type == bp_tracepoint)
0d381245
VP
4399 {
4400 find_pc_partial_function (loc->address, &(loc->function_name),
4401 NULL, NULL);
4402 if (loc->function_name)
4403 loc->function_name = xstrdup (loc->function_name);
4404 }
4405}
4406
4407/* set_raw_breakpoint is a low level routine for allocating and
4408 partially initializing a breakpoint of type BPTYPE. The newly
4409 created breakpoint's address, section, source file name, and line
4410 number are provided by SAL. The newly created and partially
4411 initialized breakpoint is added to the breakpoint chain and
4412 is also returned as the value of this function.
4413
4414 It is expected that the caller will complete the initialization of
4415 the newly created breakpoint struct as well as output any status
4416 information regarding the creation of a new breakpoint. In
4417 particular, set_raw_breakpoint does NOT set the breakpoint
4418 number! Care should be taken to not allow an error to occur
4419 prior to completing the initialization of the breakpoint. If this
4420 should happen, a bogus breakpoint will be left on the chain. */
4421
4422struct breakpoint *
4423set_raw_breakpoint (struct symtab_and_line sal, enum bptype bptype)
4424{
4425 struct breakpoint *b = set_raw_breakpoint_without_location (bptype);
4426 CORE_ADDR adjusted_address;
4427
4428 /* Adjust the breakpoint's address prior to allocating a location.
4429 Once we call allocate_bp_location(), that mostly uninitialized
4430 location will be placed on the location chain. Adjustment of the
8defab1a 4431 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
4432 not want its scan of the location chain to find a breakpoint and
4433 location that's only been partially initialized. */
39d61571 4434 adjusted_address = adjust_breakpoint_address (sal.pc, b->type);
0d381245 4435
39d61571 4436 b->loc = allocate_bp_location (b);
0d381245
VP
4437 b->loc->requested_address = sal.pc;
4438 b->loc->address = adjusted_address;
4439
4440 if (sal.symtab == NULL)
4441 b->source_file = NULL;
4442 else
1b36a34b 4443 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
4444 b->loc->section = sal.section;
4445 b->line_number = sal.line;
4446
4447 set_breakpoint_location_function (b->loc);
c906108c 4448
c906108c
SS
4449 breakpoints_changed ();
4450
4451 return b;
4452}
4453
c2c6d25f
JM
4454
4455/* Note that the breakpoint object B describes a permanent breakpoint
4456 instruction, hard-wired into the inferior's code. */
4457void
4458make_breakpoint_permanent (struct breakpoint *b)
4459{
0d381245 4460 struct bp_location *bl;
b5de0fa7 4461 b->enable_state = bp_permanent;
c2c6d25f 4462
0d381245
VP
4463 /* By definition, permanent breakpoints are already present in the code.
4464 Mark all locations as inserted. For now, make_breakpoint_permanent
4465 is called in just one place, so it's hard to say if it's reasonable
4466 to have permanent breakpoint with multiple locations or not,
4467 but it's easy to implmement. */
4468 for (bl = b->loc; bl; bl = bl->next)
4469 bl->inserted = 1;
c2c6d25f
JM
4470}
4471
53a5351d 4472/* Call this routine when stepping and nexting to enable a breakpoint
0fd8e87f 4473 if we do a longjmp() in THREAD. When we hit that breakpoint, call
c906108c
SS
4474 set_longjmp_resume_breakpoint() to figure out where we are going. */
4475
4476void
0fd8e87f 4477set_longjmp_breakpoint (int thread)
c906108c 4478{
0fd8e87f
UW
4479 struct breakpoint *b, *temp;
4480
4481 /* To avoid having to rescan all objfile symbols at every step,
4482 we maintain a list of continually-inserted but always disabled
4483 longjmp "master" breakpoints. Here, we simply create momentary
4484 clones of those and enable them for the requested thread. */
4485 ALL_BREAKPOINTS_SAFE (b, temp)
4486 if (b->type == bp_longjmp_master)
4487 {
4488 struct breakpoint *clone = clone_momentary_breakpoint (b);
4489 clone->type = bp_longjmp;
4490 clone->thread = thread;
4491 }
c906108c
SS
4492}
4493
611c83ae 4494/* Delete all longjmp breakpoints from THREAD. */
c906108c 4495void
611c83ae 4496delete_longjmp_breakpoint (int thread)
c906108c 4497{
611c83ae 4498 struct breakpoint *b, *temp;
c906108c 4499
611c83ae
PA
4500 ALL_BREAKPOINTS_SAFE (b, temp)
4501 if (b->type == bp_longjmp)
4502 {
4503 if (b->thread == thread)
4504 delete_breakpoint (b);
4505 }
c906108c
SS
4506}
4507
1900040c
MS
4508void
4509enable_overlay_breakpoints (void)
4510{
52f0bd74 4511 struct breakpoint *b;
1900040c
MS
4512
4513 ALL_BREAKPOINTS (b)
4514 if (b->type == bp_overlay_event)
4515 {
4516 b->enable_state = bp_enabled;
b60e7edf 4517 update_global_location_list (1);
c02f5703 4518 overlay_events_enabled = 1;
1900040c
MS
4519 }
4520}
4521
4522void
4523disable_overlay_breakpoints (void)
4524{
52f0bd74 4525 struct breakpoint *b;
1900040c
MS
4526
4527 ALL_BREAKPOINTS (b)
4528 if (b->type == bp_overlay_event)
4529 {
4530 b->enable_state = bp_disabled;
b60e7edf 4531 update_global_location_list (0);
c02f5703 4532 overlay_events_enabled = 0;
1900040c
MS
4533 }
4534}
4535
c4093a6a 4536struct breakpoint *
fba45db2 4537create_thread_event_breakpoint (CORE_ADDR address)
c4093a6a
JM
4538{
4539 struct breakpoint *b;
c4093a6a 4540
1900040c 4541 b = create_internal_breakpoint (address, bp_thread_event);
c4093a6a 4542
b5de0fa7 4543 b->enable_state = bp_enabled;
c4093a6a 4544 /* addr_string has to be used or breakpoint_re_set will delete me. */
b435e160 4545 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
c4093a6a 4546
b60e7edf 4547 update_global_location_list_nothrow (1);
74960c60 4548
c4093a6a
JM
4549 return b;
4550}
4551
4552void
4553remove_thread_event_breakpoints (void)
4554{
4555 struct breakpoint *b, *temp;
4556
4557 ALL_BREAKPOINTS_SAFE (b, temp)
4558 if (b->type == bp_thread_event)
4559 delete_breakpoint (b);
4560}
4561
0101ce28
JJ
4562struct captured_parse_breakpoint_args
4563 {
4564 char **arg_p;
4565 struct symtabs_and_lines *sals_p;
4566 char ***addr_string_p;
4567 int *not_found_ptr;
4568 };
4569
4570struct lang_and_radix
4571 {
4572 enum language lang;
4573 int radix;
4574 };
4575
0101ce28 4576
cae688ec
JJ
4577void
4578remove_solib_event_breakpoints (void)
4579{
4580 struct breakpoint *b, *temp;
4581
4582 ALL_BREAKPOINTS_SAFE (b, temp)
4583 if (b->type == bp_shlib_event)
4584 delete_breakpoint (b);
4585}
4586
4587struct breakpoint *
4588create_solib_event_breakpoint (CORE_ADDR address)
4589{
4590 struct breakpoint *b;
4591
4592 b = create_internal_breakpoint (address, bp_shlib_event);
b60e7edf 4593 update_global_location_list_nothrow (1);
cae688ec
JJ
4594 return b;
4595}
4596
4597/* Disable any breakpoints that are on code in shared libraries. Only
4598 apply to enabled breakpoints, disabled ones can just stay disabled. */
4599
4600void
cb851954 4601disable_breakpoints_in_shlibs (void)
cae688ec 4602{
0d381245 4603 struct bp_location *loc;
cae688ec 4604
0d381245 4605 ALL_BP_LOCATIONS (loc)
cae688ec 4606 {
0d381245
VP
4607 struct breakpoint *b = loc->owner;
4608 /* We apply the check to all breakpoints, including disabled
4609 for those with loc->duplicate set. This is so that when breakpoint
4610 becomes enabled, or the duplicate is removed, gdb will try to insert
4611 all breakpoints. If we don't set shlib_disabled here, we'll try
4612 to insert those breakpoints and fail. */
1042e4c0
SS
4613 if (((b->type == bp_breakpoint)
4614 || (b->type == bp_hardware_breakpoint)
4615 || (b->type == bp_tracepoint))
0d381245 4616 && !loc->shlib_disabled
a77053c2 4617#ifdef PC_SOLIB
0d381245 4618 && PC_SOLIB (loc->address)
a77053c2 4619#else
f5c9a895 4620 && solib_name_from_address (loc->address)
a77053c2
MK
4621#endif
4622 )
0d381245
VP
4623 {
4624 loc->shlib_disabled = 1;
4625 }
cae688ec
JJ
4626 }
4627}
4628
84acb35a
JJ
4629/* Disable any breakpoints that are in in an unloaded shared library. Only
4630 apply to enabled breakpoints, disabled ones can just stay disabled. */
4631
75149521 4632static void
84acb35a
JJ
4633disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
4634{
0d381245 4635 struct bp_location *loc;
84acb35a
JJ
4636 int disabled_shlib_breaks = 0;
4637
c86cf029
VP
4638 /* SunOS a.out shared libraries are always mapped, so do not
4639 disable breakpoints; they will only be reported as unloaded
4640 through clear_solib when GDB discards its shared library
4641 list. See clear_solib for more information. */
4642 if (exec_bfd != NULL
4643 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
4644 return;
4645
0d381245 4646 ALL_BP_LOCATIONS (loc)
84acb35a 4647 {
0d381245
VP
4648 struct breakpoint *b = loc->owner;
4649 if ((loc->loc_type == bp_loc_hardware_breakpoint
4650 || loc->loc_type == bp_loc_software_breakpoint)
e2dd7057
PP
4651 && !loc->shlib_disabled
4652 && (b->type == bp_breakpoint || b->type == bp_hardware_breakpoint)
4653 && solib_contains_address_p (solib, loc->address))
84acb35a 4654 {
e2dd7057
PP
4655 loc->shlib_disabled = 1;
4656 /* At this point, we cannot rely on remove_breakpoint
4657 succeeding so we must mark the breakpoint as not inserted
4658 to prevent future errors occurring in remove_breakpoints. */
4659 loc->inserted = 0;
4660 if (!disabled_shlib_breaks)
4661 {
4662 target_terminal_ours_for_output ();
4663 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
4664 solib->so_name);
84acb35a 4665 }
e2dd7057 4666 disabled_shlib_breaks = 1;
84acb35a
JJ
4667 }
4668 }
84acb35a
JJ
4669}
4670
ce78b96d
JB
4671/* FORK & VFORK catchpoints. */
4672
4673/* Implement the "insert" breakpoint_ops method for fork catchpoints. */
4674
c906108c 4675static void
ce78b96d
JB
4676insert_catch_fork (struct breakpoint *b)
4677{
4678 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
4679}
4680
4681/* Implement the "remove" breakpoint_ops method for fork catchpoints. */
4682
4683static int
4684remove_catch_fork (struct breakpoint *b)
4685{
4686 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
4687}
4688
4689/* Implement the "breakpoint_hit" breakpoint_ops method for fork
4690 catchpoints. */
4691
4692static int
4693breakpoint_hit_catch_fork (struct breakpoint *b)
4694{
4695 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
4696}
4697
4698/* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
4699
4700static enum print_stop_action
4701print_it_catch_fork (struct breakpoint *b)
4702{
4703 annotate_catchpoint (b->number);
4704 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
4705 b->number, ptid_get_pid (b->forked_inferior_pid));
4706 return PRINT_SRC_AND_LOC;
4707}
4708
4709/* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
4710
4711static void
4712print_one_catch_fork (struct breakpoint *b, CORE_ADDR *last_addr)
4713{
79a45b7d
TT
4714 struct value_print_options opts;
4715
4716 get_user_print_options (&opts);
4717
ce78b96d
JB
4718 /* Field 4, the address, is omitted (which makes the columns
4719 not line up too nicely with the headers, but the effect
4720 is relatively readable). */
79a45b7d 4721 if (opts.addressprint)
ce78b96d
JB
4722 ui_out_field_skip (uiout, "addr");
4723 annotate_field (5);
4724 ui_out_text (uiout, "fork");
4725 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4726 {
4727 ui_out_text (uiout, ", process ");
4728 ui_out_field_int (uiout, "what",
4729 ptid_get_pid (b->forked_inferior_pid));
4730 ui_out_spaces (uiout, 1);
4731 }
4732}
4733
4734/* Implement the "print_mention" breakpoint_ops method for fork
4735 catchpoints. */
4736
4737static void
4738print_mention_catch_fork (struct breakpoint *b)
4739{
4740 printf_filtered (_("Catchpoint %d (fork)"), b->number);
4741}
4742
4743/* The breakpoint_ops structure to be used in fork catchpoints. */
4744
4745static struct breakpoint_ops catch_fork_breakpoint_ops =
4746{
4747 insert_catch_fork,
4748 remove_catch_fork,
4749 breakpoint_hit_catch_fork,
4750 print_it_catch_fork,
4751 print_one_catch_fork,
4752 print_mention_catch_fork
4753};
4754
4755/* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
4756
4757static void
4758insert_catch_vfork (struct breakpoint *b)
4759{
4760 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
4761}
4762
4763/* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
4764
4765static int
4766remove_catch_vfork (struct breakpoint *b)
4767{
4768 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
4769}
4770
4771/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
4772 catchpoints. */
4773
4774static int
4775breakpoint_hit_catch_vfork (struct breakpoint *b)
4776{
4777 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
4778}
4779
4780/* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
4781
4782static enum print_stop_action
4783print_it_catch_vfork (struct breakpoint *b)
4784{
4785 annotate_catchpoint (b->number);
4786 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
4787 b->number, ptid_get_pid (b->forked_inferior_pid));
4788 return PRINT_SRC_AND_LOC;
4789}
4790
4791/* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
4792
4793static void
4794print_one_catch_vfork (struct breakpoint *b, CORE_ADDR *last_addr)
4795{
79a45b7d
TT
4796 struct value_print_options opts;
4797
4798 get_user_print_options (&opts);
ce78b96d
JB
4799 /* Field 4, the address, is omitted (which makes the columns
4800 not line up too nicely with the headers, but the effect
4801 is relatively readable). */
79a45b7d 4802 if (opts.addressprint)
ce78b96d
JB
4803 ui_out_field_skip (uiout, "addr");
4804 annotate_field (5);
4805 ui_out_text (uiout, "vfork");
4806 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4807 {
4808 ui_out_text (uiout, ", process ");
4809 ui_out_field_int (uiout, "what",
4810 ptid_get_pid (b->forked_inferior_pid));
4811 ui_out_spaces (uiout, 1);
4812 }
4813}
4814
4815/* Implement the "print_mention" breakpoint_ops method for vfork
4816 catchpoints. */
4817
4818static void
4819print_mention_catch_vfork (struct breakpoint *b)
4820{
4821 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
4822}
4823
4824/* The breakpoint_ops structure to be used in vfork catchpoints. */
4825
4826static struct breakpoint_ops catch_vfork_breakpoint_ops =
4827{
4828 insert_catch_vfork,
4829 remove_catch_vfork,
4830 breakpoint_hit_catch_vfork,
4831 print_it_catch_vfork,
4832 print_one_catch_vfork,
4833 print_mention_catch_vfork
4834};
4835
4836/* Create a new breakpoint of the bp_catchpoint kind and return it.
4837
4838 If TEMPFLAG is non-zero, then make the breakpoint temporary.
4839 If COND_STRING is not NULL, then store it in the breakpoint.
4840 OPS, if not NULL, is the breakpoint_ops structure associated
4841 to the catchpoint. */
4842
4843static struct breakpoint *
4844create_catchpoint (int tempflag, char *cond_string,
4845 struct breakpoint_ops *ops)
c906108c 4846{
c5aa993b
JM
4847 struct symtab_and_line sal;
4848 struct breakpoint *b;
c5aa993b 4849
fe39c653 4850 init_sal (&sal);
c906108c
SS
4851 sal.pc = 0;
4852 sal.symtab = NULL;
4853 sal.line = 0;
c5aa993b 4854
ce78b96d 4855 b = set_raw_breakpoint (sal, bp_catchpoint);
c906108c
SS
4856 set_breakpoint_count (breakpoint_count + 1);
4857 b->number = breakpoint_count;
ce78b96d 4858
1b36a34b 4859 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 4860 b->thread = -1;
c906108c 4861 b->addr_string = NULL;
b5de0fa7
EZ
4862 b->enable_state = bp_enabled;
4863 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 4864 b->ops = ops;
c5aa993b 4865
c906108c 4866 mention (b);
ce78b96d 4867 update_global_location_list (1);
c906108c 4868
ce78b96d 4869 return b;
c906108c 4870}
c5aa993b 4871
9b70b993 4872static void
ce78b96d
JB
4873create_fork_vfork_event_catchpoint (int tempflag, char *cond_string,
4874 struct breakpoint_ops *ops)
c906108c 4875{
ce78b96d
JB
4876 struct breakpoint *b = create_catchpoint (tempflag, cond_string, ops);
4877
4878 /* FIXME: We should put this information in a breakpoint private data
4879 area. */
4880 b->forked_inferior_pid = null_ptid;
c906108c
SS
4881}
4882
fe798b75
JB
4883/* Exec catchpoints. */
4884
9b70b993 4885static void
fe798b75 4886insert_catch_exec (struct breakpoint *b)
c906108c 4887{
fe798b75
JB
4888 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
4889}
c906108c 4890
fe798b75
JB
4891static int
4892remove_catch_exec (struct breakpoint *b)
4893{
4894 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
4895}
c906108c 4896
fe798b75
JB
4897static int
4898breakpoint_hit_catch_exec (struct breakpoint *b)
4899{
4900 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
4901}
c906108c 4902
fe798b75
JB
4903static enum print_stop_action
4904print_it_catch_exec (struct breakpoint *b)
4905{
4906 annotate_catchpoint (b->number);
4907 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
4908 b->exec_pathname);
4909 return PRINT_SRC_AND_LOC;
c906108c
SS
4910}
4911
fe798b75
JB
4912static void
4913print_one_catch_exec (struct breakpoint *b, CORE_ADDR *last_addr)
4914{
4915 struct value_print_options opts;
4916
4917 get_user_print_options (&opts);
4918
4919 /* Field 4, the address, is omitted (which makes the columns
4920 not line up too nicely with the headers, but the effect
4921 is relatively readable). */
4922 if (opts.addressprint)
4923 ui_out_field_skip (uiout, "addr");
4924 annotate_field (5);
4925 ui_out_text (uiout, "exec");
4926 if (b->exec_pathname != NULL)
4927 {
4928 ui_out_text (uiout, ", program \"");
4929 ui_out_field_string (uiout, "what", b->exec_pathname);
4930 ui_out_text (uiout, "\" ");
4931 }
4932}
4933
4934static void
4935print_mention_catch_exec (struct breakpoint *b)
4936{
4937 printf_filtered (_("Catchpoint %d (exec)"), b->number);
4938}
4939
4940static struct breakpoint_ops catch_exec_breakpoint_ops =
4941{
4942 insert_catch_exec,
4943 remove_catch_exec,
4944 breakpoint_hit_catch_exec,
4945 print_it_catch_exec,
4946 print_one_catch_exec,
4947 print_mention_catch_exec
4948};
4949
c906108c 4950static int
fba45db2 4951hw_breakpoint_used_count (void)
c906108c 4952{
52f0bd74 4953 struct breakpoint *b;
c906108c
SS
4954 int i = 0;
4955
4956 ALL_BREAKPOINTS (b)
c5aa993b 4957 {
d6b74ac4 4958 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
4959 i++;
4960 }
c906108c
SS
4961
4962 return i;
4963}
4964
4965static int
fba45db2 4966hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 4967{
52f0bd74 4968 struct breakpoint *b;
c906108c
SS
4969 int i = 0;
4970
4971 *other_type_used = 0;
4972 ALL_BREAKPOINTS (b)
c5aa993b 4973 {
468d015d 4974 if (breakpoint_enabled (b))
c5aa993b
JM
4975 {
4976 if (b->type == type)
4977 i++;
059fb39f
PM
4978 else if ((b->type == bp_hardware_watchpoint
4979 || b->type == bp_read_watchpoint
4980 || b->type == bp_access_watchpoint))
c5aa993b
JM
4981 *other_type_used = 1;
4982 }
4983 }
c906108c
SS
4984 return i;
4985}
4986
c906108c 4987void
fba45db2 4988disable_watchpoints_before_interactive_call_start (void)
c906108c 4989{
c5aa993b 4990 struct breakpoint *b;
c906108c
SS
4991
4992 ALL_BREAKPOINTS (b)
c5aa993b
JM
4993 {
4994 if (((b->type == bp_watchpoint)
4995 || (b->type == bp_hardware_watchpoint)
4996 || (b->type == bp_read_watchpoint)
dfdfb3ca 4997 || (b->type == bp_access_watchpoint))
468d015d 4998 && breakpoint_enabled (b))
c5aa993b 4999 {
b5de0fa7 5000 b->enable_state = bp_call_disabled;
b60e7edf 5001 update_global_location_list (0);
c5aa993b
JM
5002 }
5003 }
c906108c
SS
5004}
5005
5006void
fba45db2 5007enable_watchpoints_after_interactive_call_stop (void)
c906108c 5008{
c5aa993b 5009 struct breakpoint *b;
c906108c
SS
5010
5011 ALL_BREAKPOINTS (b)
c5aa993b
JM
5012 {
5013 if (((b->type == bp_watchpoint)
5014 || (b->type == bp_hardware_watchpoint)
5015 || (b->type == bp_read_watchpoint)
dfdfb3ca 5016 || (b->type == bp_access_watchpoint))
b5de0fa7 5017 && (b->enable_state == bp_call_disabled))
c5aa993b 5018 {
b5de0fa7 5019 b->enable_state = bp_enabled;
b60e7edf 5020 update_global_location_list (1);
c5aa993b
JM
5021 }
5022 }
c906108c
SS
5023}
5024
5025
5026/* Set a breakpoint that will evaporate an end of command
5027 at address specified by SAL.
5028 Restrict it to frame FRAME if FRAME is nonzero. */
5029
5030struct breakpoint *
818dd999 5031set_momentary_breakpoint (struct symtab_and_line sal, struct frame_id frame_id,
fba45db2 5032 enum bptype type)
c906108c 5033{
52f0bd74 5034 struct breakpoint *b;
4d28f7a8 5035 b = set_raw_breakpoint (sal, type);
b5de0fa7
EZ
5036 b->enable_state = bp_enabled;
5037 b->disposition = disp_donttouch;
818dd999 5038 b->frame_id = frame_id;
c906108c
SS
5039
5040 /* If we're debugging a multi-threaded program, then we
5041 want momentary breakpoints to be active in only a
5042 single thread of control. */
39f77062
KB
5043 if (in_thread_list (inferior_ptid))
5044 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 5045
b60e7edf 5046 update_global_location_list_nothrow (1);
74960c60 5047
c906108c
SS
5048 return b;
5049}
611c83ae 5050
e58b0e63
PA
5051/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
5052 ORIG is NULL. */
5053
5054struct breakpoint *
5055clone_momentary_breakpoint (struct breakpoint *orig)
5056{
5057 struct breakpoint *copy;
5058
5059 /* If there's nothing to clone, then return nothing. */
5060 if (orig == NULL)
5061 return NULL;
5062
5063 copy = set_raw_breakpoint_without_location (orig->type);
5064 copy->loc = allocate_bp_location (copy);
5065 set_breakpoint_location_function (copy->loc);
5066
5067 copy->loc->requested_address = orig->loc->requested_address;
5068 copy->loc->address = orig->loc->address;
5069 copy->loc->section = orig->loc->section;
5070
5071 if (orig->source_file == NULL)
5072 copy->source_file = NULL;
5073 else
5074 copy->source_file = xstrdup (orig->source_file);
5075
5076 copy->line_number = orig->line_number;
5077 copy->frame_id = orig->frame_id;
5078 copy->thread = orig->thread;
5079
5080 copy->enable_state = bp_enabled;
5081 copy->disposition = disp_donttouch;
5082 copy->number = internal_breakpoint_number--;
5083
5084 update_global_location_list_nothrow (0);
5085 return copy;
5086}
5087
611c83ae
PA
5088struct breakpoint *
5089set_momentary_breakpoint_at_pc (CORE_ADDR pc, enum bptype type)
5090{
5091 struct symtab_and_line sal;
5092
5093 sal = find_pc_line (pc, 0);
5094 sal.pc = pc;
5095 sal.section = find_pc_overlay (pc);
5096 sal.explicit_pc = 1;
5097
5098 return set_momentary_breakpoint (sal, null_frame_id, type);
5099}
c906108c 5100\f
c5aa993b 5101
c906108c
SS
5102/* Tell the user we have just set a breakpoint B. */
5103
5104static void
fba45db2 5105mention (struct breakpoint *b)
c906108c
SS
5106{
5107 int say_where = 0;
fa8a61dc 5108 struct cleanup *ui_out_chain;
79a45b7d
TT
5109 struct value_print_options opts;
5110
5111 get_user_print_options (&opts);
8b93c638 5112
9a4105ab
AC
5113 /* FIXME: This is misplaced; mention() is called by things (like
5114 hitting a watchpoint) other than breakpoint creation. It should
5115 be possible to clean this up and at the same time replace the
7f4b89d1 5116 random calls to breakpoint_changed with this hook. */
383f836e 5117 observer_notify_breakpoint_created (b->number);
c906108c 5118
3086aeae
DJ
5119 if (b->ops != NULL && b->ops->print_mention != NULL)
5120 b->ops->print_mention (b);
5121 else
5122 switch (b->type)
5123 {
5124 case bp_none:
a3f17187 5125 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
3086aeae
DJ
5126 break;
5127 case bp_watchpoint:
5128 ui_out_text (uiout, "Watchpoint ");
5129 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5130 ui_out_field_int (uiout, "number", b->number);
5131 ui_out_text (uiout, ": ");
fa8a61dc 5132 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5133 do_cleanups (ui_out_chain);
5134 break;
5135 case bp_hardware_watchpoint:
5136 ui_out_text (uiout, "Hardware watchpoint ");
5137 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5138 ui_out_field_int (uiout, "number", b->number);
5139 ui_out_text (uiout, ": ");
fa8a61dc 5140 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5141 do_cleanups (ui_out_chain);
5142 break;
5143 case bp_read_watchpoint:
5144 ui_out_text (uiout, "Hardware read watchpoint ");
5145 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
5146 ui_out_field_int (uiout, "number", b->number);
5147 ui_out_text (uiout, ": ");
fa8a61dc 5148 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5149 do_cleanups (ui_out_chain);
5150 break;
5151 case bp_access_watchpoint:
5152 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
5153 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
5154 ui_out_field_int (uiout, "number", b->number);
5155 ui_out_text (uiout, ": ");
fa8a61dc 5156 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5157 do_cleanups (ui_out_chain);
5158 break;
5159 case bp_breakpoint:
5160 if (ui_out_is_mi_like_p (uiout))
5161 {
5162 say_where = 0;
5163 break;
5164 }
2cec12e5
AR
5165 if (b->disposition == disp_del)
5166 printf_filtered (_("Temporary breakpoint"));
5167 else
5168 printf_filtered (_("Breakpoint"));
5169 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
5170 say_where = 1;
5171 break;
5172 case bp_hardware_breakpoint:
5173 if (ui_out_is_mi_like_p (uiout))
5174 {
5175 say_where = 0;
5176 break;
5177 }
a3f17187 5178 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
5179 say_where = 1;
5180 break;
1042e4c0
SS
5181 case bp_tracepoint:
5182 if (ui_out_is_mi_like_p (uiout))
5183 {
5184 say_where = 0;
5185 break;
5186 }
5187 printf_filtered (_("Tracepoint"));
5188 printf_filtered (_(" %d"), b->number);
5189 say_where = 1;
5190 break;
3086aeae
DJ
5191
5192 case bp_until:
5193 case bp_finish:
5194 case bp_longjmp:
5195 case bp_longjmp_resume:
5196 case bp_step_resume:
3086aeae
DJ
5197 case bp_call_dummy:
5198 case bp_watchpoint_scope:
5199 case bp_shlib_event:
5200 case bp_thread_event:
5201 case bp_overlay_event:
0fd8e87f 5202 case bp_longjmp_master:
3086aeae
DJ
5203 break;
5204 }
c906108c 5205
c906108c
SS
5206 if (say_where)
5207 {
a3f17187
AC
5208 /* i18n: cagney/2005-02-11: Below needs to be merged into a
5209 single string. */
0d381245 5210 if (b->loc == NULL)
c906108c 5211 {
a3f17187 5212 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
5213 }
5214 else
5215 {
79a45b7d 5216 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
5217 {
5218 printf_filtered (" at ");
ed49a04f 5219 fputs_filtered (paddress (b->loc->address), gdb_stdout);
0101ce28
JJ
5220 }
5221 if (b->source_file)
5222 printf_filtered (": file %s, line %d.",
5223 b->source_file, b->line_number);
0d381245
VP
5224
5225 if (b->loc->next)
5226 {
5227 struct bp_location *loc = b->loc;
5228 int n = 0;
5229 for (; loc; loc = loc->next)
5230 ++n;
5231 printf_filtered (" (%d locations)", n);
5232 }
5233
c906108c 5234 }
c906108c 5235 }
9dc5e2a9 5236 if (ui_out_is_mi_like_p (uiout))
fb40c209 5237 return;
c906108c
SS
5238 printf_filtered ("\n");
5239}
c906108c 5240\f
c5aa993b 5241
0d381245 5242static struct bp_location *
39d61571 5243add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
5244 const struct symtab_and_line *sal)
5245{
5246 struct bp_location *loc, **tmp;
5247
39d61571 5248 loc = allocate_bp_location (b);
0d381245
VP
5249 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
5250 ;
5251 *tmp = loc;
5252 loc->requested_address = sal->pc;
39d61571 5253 loc->address = adjust_breakpoint_address (loc->requested_address, b->type);
0d381245
VP
5254 loc->section = sal->section;
5255
5256 set_breakpoint_location_function (loc);
5257 return loc;
5258}
514f746b
AR
5259\f
5260
5261/* Return 1 if LOC is pointing to a permanent breakpoint,
5262 return 0 otherwise. */
5263
5264static int
5265bp_loc_is_permanent (struct bp_location *loc)
5266{
5267 int len;
5268 CORE_ADDR addr;
5269 const gdb_byte *brk;
5270 gdb_byte *target_mem;
939c61fa
JK
5271 struct cleanup *cleanup;
5272 int retval = 0;
514f746b
AR
5273
5274 gdb_assert (loc != NULL);
5275
5276 addr = loc->address;
5277 brk = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &len);
5278
939c61fa
JK
5279 /* Software breakpoints unsupported? */
5280 if (brk == NULL)
5281 return 0;
5282
514f746b
AR
5283 target_mem = alloca (len);
5284
939c61fa
JK
5285 /* Enable the automatic memory restoration from breakpoints while
5286 we read the memory. Otherwise we could say about our temporary
5287 breakpoints they are permanent. */
5288 cleanup = make_show_memory_breakpoints_cleanup (0);
5289
514f746b
AR
5290 if (target_read_memory (loc->address, target_mem, len) == 0
5291 && memcmp (target_mem, brk, len) == 0)
939c61fa 5292 retval = 1;
514f746b 5293
939c61fa
JK
5294 do_cleanups (cleanup);
5295
5296 return retval;
514f746b
AR
5297}
5298
5299
c3f6f71d 5300
018d34a4
VP
5301/* Create a breakpoint with SAL as location. Use ADDR_STRING
5302 as textual description of the location, and COND_STRING
db107f19 5303 as condition expression. */
018d34a4
VP
5304
5305static void
0d381245 5306create_breakpoint (struct symtabs_and_lines sals, char *addr_string,
018d34a4
VP
5307 char *cond_string,
5308 enum bptype type, enum bpdisp disposition,
4a306c9a 5309 int thread, int task, int ignore_count,
41447f92 5310 struct breakpoint_ops *ops, int from_tty, int enabled)
018d34a4 5311{
0d381245
VP
5312 struct breakpoint *b = NULL;
5313 int i;
018d34a4
VP
5314
5315 if (type == bp_hardware_breakpoint)
5316 {
5317 int i = hw_breakpoint_used_count ();
5318 int target_resources_ok =
d92524f1 5319 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
5320 i + 1, 0);
5321 if (target_resources_ok == 0)
5322 error (_("No hardware breakpoint support in the target."));
5323 else if (target_resources_ok < 0)
5324 error (_("Hardware breakpoints used exceeds limit."));
5325 }
5326
0d381245
VP
5327 for (i = 0; i < sals.nelts; ++i)
5328 {
5329 struct symtab_and_line sal = sals.sals[i];
5330 struct bp_location *loc;
5331
5332 if (from_tty)
5333 describe_other_breakpoints (sal.pc, sal.section, thread);
5334
5335 if (i == 0)
5336 {
5337 b = set_raw_breakpoint (sal, type);
5338 set_breakpoint_count (breakpoint_count + 1);
5339 b->number = breakpoint_count;
5340 b->thread = thread;
4a306c9a 5341 b->task = task;
018d34a4 5342
0d381245
VP
5343 b->cond_string = cond_string;
5344 b->ignore_count = ignore_count;
41447f92 5345 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 5346 b->disposition = disposition;
018d34a4 5347
0d381245
VP
5348 loc = b->loc;
5349 }
5350 else
018d34a4 5351 {
39d61571 5352 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
5353 }
5354
514f746b
AR
5355 if (bp_loc_is_permanent (loc))
5356 make_breakpoint_permanent (b);
5357
0d381245
VP
5358 if (b->cond_string)
5359 {
5360 char *arg = b->cond_string;
d32a6982 5361 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 5362 if (*arg)
db107f19 5363 error (_("Garbage %s follows condition"), arg);
018d34a4 5364 }
0d381245 5365 }
018d34a4
VP
5366
5367 if (addr_string)
5368 b->addr_string = addr_string;
5369 else
5370 /* addr_string has to be used or breakpoint_re_set will delete
5371 me. */
5372 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
5373
604133b5 5374 b->ops = ops;
018d34a4
VP
5375 mention (b);
5376}
5377
ed0616c6
VP
5378/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
5379 elements to fill the void space. */
2c0b251b
PA
5380static void
5381remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
5382{
5383 int i = index_to_remove+1;
5384 int last_index = sal->nelts-1;
5385
5386 for (;i <= last_index; ++i)
5387 sal->sals[i-1] = sal->sals[i];
5388
5389 --(sal->nelts);
5390}
5391
5392/* If appropriate, obtains all sals that correspond
5393 to the same file and line as SAL. This is done
5394 only if SAL does not have explicit PC and has
5395 line and file information. If we got just a single
5396 expanded sal, return the original.
5397
5398 Otherwise, if SAL.explicit_line is not set, filter out
5399 all sals for which the name of enclosing function
5400 is different from SAL. This makes sure that if we have
5401 breakpoint originally set in template instantiation, say
5402 foo<int>(), we won't expand SAL to locations at the same
5403 line in all existing instantiations of 'foo'.
5404
5405*/
2c0b251b 5406static struct symtabs_and_lines
ed0616c6
VP
5407expand_line_sal_maybe (struct symtab_and_line sal)
5408{
5409 struct symtabs_and_lines expanded;
5410 CORE_ADDR original_pc = sal.pc;
5411 char *original_function = NULL;
5412 int found;
5413 int i;
5414
5415 /* If we have explicit pc, don't expand.
5416 If we have no line number, we can't expand. */
5417 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
5418 {
5419 expanded.nelts = 1;
5420 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5421 expanded.sals[0] = sal;
5422 return expanded;
5423 }
5424
5425 sal.pc = 0;
5426 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
5427
5428 expanded = expand_line_sal (sal);
5429 if (expanded.nelts == 1)
5430 {
5431 /* We had one sal, we got one sal. Without futher
5432 processing, just return the original sal. */
5433 xfree (expanded.sals);
5434 expanded.nelts = 1;
5435 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5436 sal.pc = original_pc;
5437 expanded.sals[0] = sal;
5438 return expanded;
5439 }
5440
5441 if (!sal.explicit_line)
5442 {
5443 CORE_ADDR func_addr, func_end;
5444 for (i = 0; i < expanded.nelts; ++i)
5445 {
5446 CORE_ADDR pc = expanded.sals[i].pc;
5447 char *this_function;
5448 if (find_pc_partial_function (pc, &this_function,
5449 &func_addr, &func_end))
5450 {
059fb39f
PM
5451 if (this_function
5452 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
5453 {
5454 remove_sal (&expanded, i);
5455 --i;
5456 }
5457 else if (func_addr == pc)
5458 {
5459 /* We're at beginning of a function, and should
5460 skip prologue. */
5461 struct symbol *sym = find_pc_function (pc);
5462 if (sym)
5463 expanded.sals[i] = find_function_start_sal (sym, 1);
5464 else
d80b854b
UW
5465 {
5466 /* Since find_pc_partial_function returned true,
5467 we should really always find the section here. */
5468 struct obj_section *section = find_pc_section (pc);
5469 if (section)
5470 {
5471 struct gdbarch *gdbarch
5472 = get_objfile_arch (section->objfile);
5473 expanded.sals[i].pc
5474 = gdbarch_skip_prologue (gdbarch, pc);
5475 }
5476 }
ed0616c6
VP
5477 }
5478 }
5479 }
5480 }
3daf8fe5
JG
5481 else
5482 {
5483 for (i = 0; i < expanded.nelts; ++i)
5484 {
5485 /* If this SAL corresponds to a breakpoint inserted using a
5486 line number, then skip the function prologue if necessary. */
5487 skip_prologue_sal (&expanded.sals[i]);
5488 }
5489 }
ed0616c6
VP
5490
5491
5492 if (expanded.nelts <= 1)
5493 {
5494 /* This is un ugly workaround. If we get zero
5495 expanded sals then something is really wrong.
5496 Fix that by returnign the original sal. */
5497 xfree (expanded.sals);
5498 expanded.nelts = 1;
5499 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5500 sal.pc = original_pc;
5501 expanded.sals[0] = sal;
5502 return expanded;
5503 }
5504
5505 if (original_pc)
5506 {
5507 found = 0;
5508 for (i = 0; i < expanded.nelts; ++i)
5509 if (expanded.sals[i].pc == original_pc)
5510 {
5511 found = 1;
5512 break;
5513 }
5514 gdb_assert (found);
5515 }
5516
5517 return expanded;
5518}
5519
018d34a4
VP
5520/* Add SALS.nelts breakpoints to the breakpoint table. For each
5521 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
5522 value. COND_STRING, if not NULL, specified the condition to be
5523 used for all breakpoints. Essentially the only case where
5524 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
5525 function. In that case, it's still not possible to specify
5526 separate conditions for different overloaded functions, so
5527 we take just a single condition string.
5528
c3f6f71d 5529 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 5530 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
5531 array contents). If the function fails (error() is called), the
5532 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
5533 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
5534
5535static void
c3f6f71d 5536create_breakpoints (struct symtabs_and_lines sals, char **addr_string,
018d34a4 5537 char *cond_string,
c3f6f71d 5538 enum bptype type, enum bpdisp disposition,
4a306c9a 5539 int thread, int task, int ignore_count,
41447f92
VP
5540 struct breakpoint_ops *ops, int from_tty,
5541 int enabled)
c906108c 5542{
018d34a4
VP
5543 int i;
5544 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 5545 {
ed0616c6
VP
5546 struct symtabs_and_lines expanded =
5547 expand_line_sal_maybe (sals.sals[i]);
0d381245 5548
ed0616c6 5549 create_breakpoint (expanded, addr_string[i],
018d34a4 5550 cond_string, type, disposition,
4a306c9a 5551 thread, task, ignore_count, ops, from_tty, enabled);
c3f6f71d 5552 }
c3f6f71d 5553}
c906108c 5554
c3f6f71d
JM
5555/* Parse ARG which is assumed to be a SAL specification possibly
5556 followed by conditionals. On return, SALS contains an array of SAL
5557 addresses found. ADDR_STRING contains a vector of (canonical)
5558 address strings. ARG points to the end of the SAL. */
c906108c 5559
b9362cc7 5560static void
c3f6f71d
JM
5561parse_breakpoint_sals (char **address,
5562 struct symtabs_and_lines *sals,
0101ce28
JJ
5563 char ***addr_string,
5564 int *not_found_ptr)
c3f6f71d
JM
5565{
5566 char *addr_start = *address;
5567 *addr_string = NULL;
5568 /* If no arg given, or if first arg is 'if ', use the default
5569 breakpoint. */
5570 if ((*address) == NULL
5571 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
5572 {
5573 if (default_breakpoint_valid)
5574 {
c3f6f71d 5575 struct symtab_and_line sal;
fe39c653 5576 init_sal (&sal); /* initialize to zeroes */
c3f6f71d 5577 sals->sals = (struct symtab_and_line *)
c906108c
SS
5578 xmalloc (sizeof (struct symtab_and_line));
5579 sal.pc = default_breakpoint_address;
5580 sal.line = default_breakpoint_line;
5581 sal.symtab = default_breakpoint_symtab;
c5aa993b 5582 sal.section = find_pc_overlay (sal.pc);
00903456
JK
5583
5584 /* "break" without arguments is equivalent to "break *PC" where PC is
5585 the default_breakpoint_address. So make sure to set
5586 sal.explicit_pc to prevent GDB from trying to expand the list of
5587 sals to include all other instances with the same symtab and line.
5588 */
5589 sal.explicit_pc = 1;
5590
c3f6f71d
JM
5591 sals->sals[0] = sal;
5592 sals->nelts = 1;
c906108c
SS
5593 }
5594 else
8a3fe4f8 5595 error (_("No default breakpoint address now."));
c906108c
SS
5596 }
5597 else
5598 {
c906108c 5599 /* Force almost all breakpoints to be in terms of the
c5aa993b
JM
5600 current_source_symtab (which is decode_line_1's default). This
5601 should produce the results we want almost all of the time while
1aeae86e
AF
5602 leaving default_breakpoint_* alone.
5603 ObjC: However, don't match an Objective-C method name which
5604 may have a '+' or '-' succeeded by a '[' */
0378c332 5605
c214a6fd 5606 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 5607
c906108c 5608 if (default_breakpoint_valid
0378c332 5609 && (!cursal.symtab
1aeae86e
AF
5610 || ((strchr ("+-", (*address)[0]) != NULL)
5611 && ((*address)[1] != '['))))
c3f6f71d 5612 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
5613 default_breakpoint_line, addr_string,
5614 not_found_ptr);
c906108c 5615 else
0101ce28
JJ
5616 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
5617 addr_string, not_found_ptr);
c906108c 5618 }
c3f6f71d
JM
5619 /* For any SAL that didn't have a canonical string, fill one in. */
5620 if (sals->nelts > 0 && *addr_string == NULL)
5621 *addr_string = xcalloc (sals->nelts, sizeof (char **));
5622 if (addr_start != (*address))
c906108c 5623 {
c3f6f71d
JM
5624 int i;
5625 for (i = 0; i < sals->nelts; i++)
c906108c 5626 {
c3f6f71d
JM
5627 /* Add the string if not present. */
5628 if ((*addr_string)[i] == NULL)
5629 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
c906108c
SS
5630 }
5631 }
c3f6f71d 5632}
c906108c 5633
c906108c 5634
c3f6f71d
JM
5635/* Convert each SAL into a real PC. Verify that the PC can be
5636 inserted as a breakpoint. If it can't throw an error. */
c906108c 5637
b9362cc7 5638static void
c3f6f71d
JM
5639breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
5640 char *address)
5641{
5642 int i;
5643 for (i = 0; i < sals->nelts; i++)
ee53e872 5644 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
5645}
5646
05ff989b 5647static void
0101ce28
JJ
5648do_captured_parse_breakpoint (struct ui_out *ui, void *data)
5649{
5650 struct captured_parse_breakpoint_args *args = data;
5651
5652 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
5653 args->not_found_ptr);
0101ce28
JJ
5654}
5655
018d34a4
VP
5656/* Given TOK, a string specification of condition and thread, as
5657 accepted by the 'break' command, extract the condition
5658 string and thread number and set *COND_STRING and *THREAD.
5659 PC identifies the context at which the condition should be parsed.
5660 If no condition is found, *COND_STRING is set to NULL.
5661 If no thread is found, *THREAD is set to -1. */
5662static void
5663find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 5664 char **cond_string, int *thread, int *task)
018d34a4
VP
5665{
5666 *cond_string = NULL;
5667 *thread = -1;
5668 while (tok && *tok)
5669 {
5670 char *end_tok;
5671 int toklen;
5672 char *cond_start = NULL;
5673 char *cond_end = NULL;
5674 while (*tok == ' ' || *tok == '\t')
5675 tok++;
5676
5677 end_tok = tok;
5678
5679 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5680 end_tok++;
5681
5682 toklen = end_tok - tok;
5683
5684 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5685 {
f7545552
TT
5686 struct expression *expr;
5687
018d34a4 5688 tok = cond_start = end_tok + 1;
f7545552
TT
5689 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
5690 xfree (expr);
018d34a4
VP
5691 cond_end = tok;
5692 *cond_string = savestring (cond_start,
5693 cond_end - cond_start);
5694 }
5695 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5696 {
5697 char *tmptok;
5698
5699 tok = end_tok + 1;
5700 tmptok = tok;
5701 *thread = strtol (tok, &tok, 0);
5702 if (tok == tmptok)
5703 error (_("Junk after thread keyword."));
5704 if (!valid_thread_id (*thread))
5705 error (_("Unknown thread %d."), *thread);
5706 }
4a306c9a
JB
5707 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
5708 {
5709 char *tmptok;
5710
5711 tok = end_tok + 1;
5712 tmptok = tok;
5713 *task = strtol (tok, &tok, 0);
5714 if (tok == tmptok)
5715 error (_("Junk after task keyword."));
5716 if (!valid_task_id (*task))
5717 error (_("Unknown task %d\n"), *task);
5718 }
018d34a4
VP
5719 else
5720 error (_("Junk at end of arguments."));
5721 }
5722}
5723
72b2ff0e
VP
5724/* Set a breakpoint. This function is shared between
5725 CLI and MI functions for setting a breakpoint.
5726 This function has two major modes of operations,
5727 selected by the PARSE_CONDITION_AND_THREAD parameter.
5728 If non-zero, the function will parse arg, extracting
5729 breakpoint location, address and thread. Otherwise,
5730 ARG is just the location of breakpoint, with condition
5731 and thread specified by the COND_STRING and THREAD
5732 parameters. */
0101ce28 5733
98deb0da 5734static void
72b2ff0e 5735break_command_really (char *arg, char *cond_string, int thread,
60c46647 5736 int parse_condition_and_thread,
1042e4c0 5737 int tempflag, int hardwareflag, int traceflag,
60c46647
VP
5738 int ignore_count,
5739 enum auto_boolean pending_break_support,
604133b5 5740 struct breakpoint_ops *ops,
41447f92
VP
5741 int from_tty,
5742 int enabled)
c3f6f71d 5743{
71fff37b 5744 struct gdb_exception e;
c3f6f71d 5745 struct symtabs_and_lines sals;
0101ce28 5746 struct symtab_and_line pending_sal;
0101ce28
JJ
5747 char *copy_arg;
5748 char *err_msg;
c3f6f71d
JM
5749 char *addr_start = arg;
5750 char **addr_string;
5751 struct cleanup *old_chain;
80c99de1 5752 struct cleanup *bkpt_chain = NULL;
0101ce28 5753 struct captured_parse_breakpoint_args parse_args;
05ff989b 5754 int i;
0101ce28 5755 int pending = 0;
0101ce28 5756 int not_found = 0;
1042e4c0 5757 enum bptype type_wanted;
4a306c9a 5758 int task = 0;
c3f6f71d 5759
c3f6f71d
JM
5760 sals.sals = NULL;
5761 sals.nelts = 0;
5762 addr_string = NULL;
c3f6f71d 5763
0101ce28
JJ
5764 parse_args.arg_p = &arg;
5765 parse_args.sals_p = &sals;
5766 parse_args.addr_string_p = &addr_string;
5767 parse_args.not_found_ptr = &not_found;
5768
05ff989b
AC
5769 e = catch_exception (uiout, do_captured_parse_breakpoint,
5770 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
5771
5772 /* If caller is interested in rc value from parse, set value. */
05ff989b 5773 switch (e.reason)
0101ce28 5774 {
05ff989b 5775 case RETURN_QUIT:
98deb0da 5776 throw_exception (e);
05ff989b
AC
5777 case RETURN_ERROR:
5778 switch (e.error)
0101ce28 5779 {
05ff989b 5780 case NOT_FOUND_ERROR:
0101ce28 5781
05ff989b
AC
5782 /* If pending breakpoint support is turned off, throw
5783 error. */
fa8d40ab
JJ
5784
5785 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
5786 throw_exception (e);
5787
5788 exception_print (gdb_stderr, e);
fa8d40ab 5789
05ff989b
AC
5790 /* If pending breakpoint support is auto query and the user
5791 selects no, then simply return the error code. */
059fb39f
PM
5792 if (pending_break_support == AUTO_BOOLEAN_AUTO
5793 && !nquery ("Make breakpoint pending on future shared library load? "))
98deb0da 5794 return;
fa8d40ab 5795
05ff989b
AC
5796 /* At this point, either the user was queried about setting
5797 a pending breakpoint and selected yes, or pending
5798 breakpoint behavior is on and thus a pending breakpoint
5799 is defaulted on behalf of the user. */
0101ce28
JJ
5800 copy_arg = xstrdup (addr_start);
5801 addr_string = &copy_arg;
5802 sals.nelts = 1;
5803 sals.sals = &pending_sal;
5804 pending_sal.pc = 0;
5805 pending = 1;
05ff989b
AC
5806 break;
5807 default:
98deb0da 5808 throw_exception (e);
0101ce28 5809 }
05ff989b
AC
5810 default:
5811 if (!sals.nelts)
98deb0da 5812 return;
0101ce28 5813 }
c3f6f71d
JM
5814
5815 /* Create a chain of things that always need to be cleaned up. */
5816 old_chain = make_cleanup (null_cleanup, 0);
5817
0101ce28
JJ
5818 if (!pending)
5819 {
5820 /* Make sure that all storage allocated to SALS gets freed. */
5821 make_cleanup (xfree, sals.sals);
5822
5823 /* Cleanup the addr_string array but not its contents. */
5824 make_cleanup (xfree, addr_string);
5825 }
c3f6f71d 5826
c3f6f71d
JM
5827 /* ----------------------------- SNIP -----------------------------
5828 Anything added to the cleanup chain beyond this point is assumed
5829 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
5830 then the memory is not reclaimed. */
5831 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
5832
5833 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 5834 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
5835 for (i = 0; i < sals.nelts; i++)
5836 {
5837 if (addr_string[i] != NULL)
b8c9b27d 5838 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
5839 }
5840
5841 /* Resolve all line numbers to PC's and verify that the addresses
5842 are ok for the target. */
0101ce28
JJ
5843 if (!pending)
5844 breakpoint_sals_to_pc (&sals, addr_start);
c3f6f71d 5845
1042e4c0
SS
5846 type_wanted = (traceflag
5847 ? bp_tracepoint
5848 : (hardwareflag ? bp_hardware_breakpoint : bp_breakpoint));
5849
c3f6f71d
JM
5850 /* Verify that condition can be parsed, before setting any
5851 breakpoints. Allocate a separate condition expression for each
5852 breakpoint. */
0101ce28 5853 if (!pending)
c3f6f71d 5854 {
2f069f6f 5855 if (parse_condition_and_thread)
72b2ff0e
VP
5856 {
5857 /* Here we only parse 'arg' to separate condition
5858 from thread number, so parsing in context of first
5859 sal is OK. When setting the breakpoint we'll
5860 re-parse it in context of each sal. */
5861 cond_string = NULL;
5862 thread = -1;
4a306c9a
JB
5863 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
5864 &thread, &task);
72b2ff0e
VP
5865 if (cond_string)
5866 make_cleanup (xfree, cond_string);
5867 }
2f069f6f 5868 else
72b2ff0e
VP
5869 {
5870 /* Create a private copy of condition string. */
5871 if (cond_string)
5872 {
5873 cond_string = xstrdup (cond_string);
5874 make_cleanup (xfree, cond_string);
5875 }
5876 }
1042e4c0 5877 create_breakpoints (sals, addr_string, cond_string, type_wanted,
0101ce28 5878 tempflag ? disp_del : disp_donttouch,
4a306c9a 5879 thread, task, ignore_count, ops, from_tty, enabled);
c906108c 5880 }
0101ce28
JJ
5881 else
5882 {
fe3f5fa8 5883 struct symtab_and_line sal = {0};
0101ce28
JJ
5884 struct breakpoint *b;
5885
0101ce28
JJ
5886 make_cleanup (xfree, copy_arg);
5887
1042e4c0 5888 b = set_raw_breakpoint_without_location (type_wanted);
0101ce28
JJ
5889 set_breakpoint_count (breakpoint_count + 1);
5890 b->number = breakpoint_count;
72b2ff0e 5891 b->thread = -1;
018d34a4 5892 b->addr_string = addr_string[0];
72b2ff0e 5893 b->cond_string = NULL;
0101ce28 5894 b->ignore_count = ignore_count;
0101ce28 5895 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 5896 b->condition_not_parsed = 1;
604133b5 5897 b->ops = ops;
41447f92 5898 b->enable_state = enabled ? bp_enabled : bp_disabled;
74960c60 5899
0101ce28
JJ
5900 mention (b);
5901 }
5902
c3f6f71d 5903 if (sals.nelts > 1)
8a3fe4f8
AC
5904 warning (_("Multiple breakpoints were set.\n"
5905 "Use the \"delete\" command to delete unwanted breakpoints."));
80c99de1
PA
5906 /* That's it. Discard the cleanups for data inserted into the
5907 breakpoint. */
5908 discard_cleanups (bkpt_chain);
5909 /* But cleanup everything else. */
c3f6f71d 5910 do_cleanups (old_chain);
217dc9e2 5911
80c99de1 5912 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 5913 update_global_location_list (1);
c3f6f71d 5914}
c906108c 5915
72b2ff0e
VP
5916/* Set a breakpoint.
5917 ARG is a string describing breakpoint address,
5918 condition, and thread.
5919 FLAG specifies if a breakpoint is hardware on,
5920 and if breakpoint is temporary, using BP_HARDWARE_FLAG
5921 and BP_TEMPFLAG. */
5922
98deb0da 5923static void
72b2ff0e 5924break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 5925{
72b2ff0e
VP
5926 int hardwareflag = flag & BP_HARDWAREFLAG;
5927 int tempflag = flag & BP_TEMPFLAG;
c3f6f71d 5928
98deb0da
VP
5929 break_command_really (arg,
5930 NULL, 0, 1 /* parse arg */,
1042e4c0 5931 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da 5932 0 /* Ignore count */,
604133b5
AR
5933 pending_break_support,
5934 NULL /* breakpoint_ops */,
41447f92
VP
5935 from_tty,
5936 1 /* enabled */);
c906108c
SS
5937}
5938
72b2ff0e 5939
98deb0da
VP
5940void
5941set_breakpoint (char *address, char *condition,
c3f6f71d 5942 int hardwareflag, int tempflag,
ce43223b 5943 int thread, int ignore_count,
41447f92 5944 int pending, int enabled)
c3f6f71d 5945{
98deb0da
VP
5946 break_command_really (address, condition, thread,
5947 0 /* condition and thread are valid. */,
1042e4c0 5948 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da
VP
5949 ignore_count,
5950 pending
5951 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
41447f92 5952 NULL, 0, enabled);
c3f6f71d
JM
5953}
5954
6a048695
JB
5955/* Adjust SAL to the first instruction past the function prologue.
5956 The end of the prologue is determined using the line table from
3daf8fe5
JG
5957 the debugging information. explicit_pc and explicit_line are
5958 not modified.
6a048695
JB
5959
5960 If SAL is already past the prologue, then do nothing. */
5961
5962static void
5963skip_prologue_sal (struct symtab_and_line *sal)
5964{
5965 struct symbol *sym = find_pc_function (sal->pc);
5966 struct symtab_and_line start_sal;
5967
5968 if (sym == NULL)
5969 return;
5970
5971 start_sal = find_function_start_sal (sym, 1);
5972 if (sal->pc < start_sal.pc)
3daf8fe5
JG
5973 {
5974 start_sal.explicit_line = sal->explicit_line;
5975 start_sal.explicit_pc = sal->explicit_pc;
5976 *sal = start_sal;
5977 }
6a048695 5978}
c3f6f71d 5979
c906108c
SS
5980/* Helper function for break_command_1 and disassemble_command. */
5981
5982void
fba45db2 5983resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
5984{
5985 CORE_ADDR pc;
5986
5987 if (sal->pc == 0 && sal->symtab != NULL)
5988 {
5989 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 5990 error (_("No line %d in file \"%s\"."),
c906108c
SS
5991 sal->line, sal->symtab->filename);
5992 sal->pc = pc;
6a048695
JB
5993
5994 /* If this SAL corresponds to a breakpoint inserted using
5995 a line number, then skip the function prologue if necessary. */
5996 if (sal->explicit_line)
56ef84b1
TT
5997 {
5998 /* Preserve the original line number. */
5999 int saved_line = sal->line;
6000 skip_prologue_sal (sal);
6001 sal->line = saved_line;
6002 }
c906108c
SS
6003 }
6004
6005 if (sal->section == 0 && sal->symtab != NULL)
6006 {
6007 struct blockvector *bv;
c5aa993b
JM
6008 struct block *b;
6009 struct symbol *sym;
c906108c 6010
801e3a5b 6011 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
6012 if (bv != NULL)
6013 {
7f0df278 6014 sym = block_linkage_function (b);
c906108c
SS
6015 if (sym != NULL)
6016 {
6017 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 6018 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
6019 }
6020 else
6021 {
6022 /* It really is worthwhile to have the section, so we'll just
c5aa993b
JM
6023 have to look harder. This case can be executed if we have
6024 line numbers but no functions (as can happen in assembly
6025 source). */
c906108c 6026
c5aa993b 6027 struct minimal_symbol *msym;
c906108c
SS
6028
6029 msym = lookup_minimal_symbol_by_pc (sal->pc);
6030 if (msym)
714835d5 6031 sal->section = SYMBOL_OBJ_SECTION (msym);
c906108c
SS
6032 }
6033 }
6034 }
6035}
6036
6037void
fba45db2 6038break_command (char *arg, int from_tty)
c906108c 6039{
db107f19 6040 break_command_1 (arg, 0, from_tty);
c906108c
SS
6041}
6042
c906108c 6043void
fba45db2 6044tbreak_command (char *arg, int from_tty)
c906108c 6045{
db107f19 6046 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
6047}
6048
c906108c 6049static void
fba45db2 6050hbreak_command (char *arg, int from_tty)
c906108c 6051{
db107f19 6052 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
6053}
6054
6055static void
fba45db2 6056thbreak_command (char *arg, int from_tty)
c906108c 6057{
db107f19 6058 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
6059}
6060
6061static void
fba45db2 6062stop_command (char *arg, int from_tty)
c906108c 6063{
a3f17187 6064 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 6065Usage: stop in <function | address>\n\
a3f17187 6066 stop at <line>\n"));
c906108c
SS
6067}
6068
6069static void
fba45db2 6070stopin_command (char *arg, int from_tty)
c906108c
SS
6071{
6072 int badInput = 0;
6073
c5aa993b 6074 if (arg == (char *) NULL)
c906108c
SS
6075 badInput = 1;
6076 else if (*arg != '*')
6077 {
6078 char *argptr = arg;
6079 int hasColon = 0;
6080
53a5351d
JM
6081 /* look for a ':'. If this is a line number specification, then
6082 say it is bad, otherwise, it should be an address or
6083 function/method name */
c906108c 6084 while (*argptr && !hasColon)
c5aa993b
JM
6085 {
6086 hasColon = (*argptr == ':');
6087 argptr++;
6088 }
c906108c
SS
6089
6090 if (hasColon)
c5aa993b 6091 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 6092 else
c5aa993b 6093 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
6094 }
6095
6096 if (badInput)
a3f17187 6097 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 6098 else
db107f19 6099 break_command_1 (arg, 0, from_tty);
c906108c
SS
6100}
6101
6102static void
fba45db2 6103stopat_command (char *arg, int from_tty)
c906108c
SS
6104{
6105 int badInput = 0;
6106
c5aa993b 6107 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
6108 badInput = 1;
6109 else
6110 {
6111 char *argptr = arg;
6112 int hasColon = 0;
6113
6114 /* look for a ':'. If there is a '::' then get out, otherwise
c5aa993b 6115 it is probably a line number. */
c906108c 6116 while (*argptr && !hasColon)
c5aa993b
JM
6117 {
6118 hasColon = (*argptr == ':');
6119 argptr++;
6120 }
c906108c
SS
6121
6122 if (hasColon)
c5aa993b 6123 badInput = (*argptr == ':'); /* we have class::method */
c906108c 6124 else
c5aa993b 6125 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
6126 }
6127
6128 if (badInput)
a3f17187 6129 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 6130 else
db107f19 6131 break_command_1 (arg, 0, from_tty);
c906108c
SS
6132}
6133
53a5351d
JM
6134/* accessflag: hw_write: watch write,
6135 hw_read: watch read,
6136 hw_access: watch access (read or write) */
c906108c 6137static void
fba45db2 6138watch_command_1 (char *arg, int accessflag, int from_tty)
c906108c 6139{
d983da9c 6140 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c
SS
6141 struct symtab_and_line sal;
6142 struct expression *exp;
6143 struct block *exp_valid_block;
6144 struct value *val, *mark;
6145 struct frame_info *frame;
6146 struct frame_info *prev_frame = NULL;
6147 char *exp_start = NULL;
6148 char *exp_end = NULL;
37e4754d 6149 char *tok, *id_tok_start, *end_tok;
c906108c
SS
6150 int toklen;
6151 char *cond_start = NULL;
6152 char *cond_end = NULL;
6153 struct expression *cond = NULL;
6154 int i, other_type_used, target_resources_ok = 0;
6155 enum bptype bp_type;
6156 int mem_cnt = 0;
37e4754d 6157 int thread = -1;
c906108c 6158
fe39c653 6159 init_sal (&sal); /* initialize to zeroes */
c5aa993b 6160
37e4754d
LM
6161 /* Make sure that we actually have parameters to parse. */
6162 if (arg != NULL && arg[0] != '\0')
6163 {
6164 toklen = strlen (arg); /* Size of argument list. */
6165
6166 /* Points tok to the end of the argument list. */
6167 tok = arg + toklen - 1;
6168
6169 /* Go backwards in the parameters list. Skip the last parameter.
6170 If we're expecting a 'thread <thread_num>' parameter, this should
6171 be the thread identifier. */
6172 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6173 tok--;
6174 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6175 tok--;
6176
6177 /* Points end_tok to the beginning of the last token. */
6178 id_tok_start = tok + 1;
6179
6180 /* Go backwards in the parameters list. Skip one more parameter.
6181 If we're expecting a 'thread <thread_num>' parameter, we should
6182 reach a "thread" token. */
6183 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6184 tok--;
6185
6186 end_tok = tok;
6187
6188 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6189 tok--;
6190
6191 /* Move the pointer forward to skip the whitespace and
6192 calculate the length of the token. */
6193 tok++;
6194 toklen = end_tok - tok;
6195
6196 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
6197 {
6198 /* At this point we've found a "thread" token, which means
6199 the user is trying to set a watchpoint that triggers
6200 only in a specific thread. */
6201 char *endp;
6202
6203 /* Extract the thread ID from the next token. */
6204 thread = strtol (id_tok_start, &endp, 0);
6205
6206 /* Check if the user provided a valid numeric value for the
6207 thread ID. */
6208 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
6209 error (_("Invalid thread ID specification %s."), id_tok_start);
6210
6211 /* Check if the thread actually exists. */
6212 if (!valid_thread_id (thread))
6213 error (_("Unknown thread %d."), thread);
6214
6215 /* Truncate the string and get rid of the thread <thread_num>
6216 parameter before the parameter list is parsed by the
6217 evaluate_expression() function. */
6218 *tok = '\0';
6219 }
6220 }
6221
6222 /* Parse the rest of the arguments. */
c906108c
SS
6223 innermost_block = NULL;
6224 exp_start = arg;
6225 exp = parse_exp_1 (&arg, 0, 0);
6226 exp_end = arg;
fa8a61dc
TT
6227 /* Remove trailing whitespace from the expression before saving it.
6228 This makes the eventual display of the expression string a bit
6229 prettier. */
6230 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
6231 --exp_end;
6232
c906108c
SS
6233 exp_valid_block = innermost_block;
6234 mark = value_mark ();
fa4727a6
DJ
6235 fetch_watchpoint_value (exp, &val, NULL, NULL);
6236 if (val != NULL)
6237 release_value (val);
c906108c
SS
6238
6239 tok = arg;
6240 while (*tok == ' ' || *tok == '\t')
6241 tok++;
6242 end_tok = tok;
6243
6244 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6245 end_tok++;
6246
6247 toklen = end_tok - tok;
6248 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6249 {
6250 tok = cond_start = end_tok + 1;
6251 cond = parse_exp_1 (&tok, 0, 0);
6252 cond_end = tok;
6253 }
6254 if (*tok)
8a3fe4f8 6255 error (_("Junk at end of command."));
c906108c 6256
53a5351d 6257 if (accessflag == hw_read)
c5aa993b 6258 bp_type = bp_read_watchpoint;
53a5351d 6259 else if (accessflag == hw_access)
c5aa993b
JM
6260 bp_type = bp_access_watchpoint;
6261 else
6262 bp_type = bp_hardware_watchpoint;
c906108c
SS
6263
6264 mem_cnt = can_use_hardware_watchpoint (val);
6265 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6266 error (_("Expression cannot be implemented with read/access watchpoint."));
c5aa993b
JM
6267 if (mem_cnt != 0)
6268 {
6269 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 6270 target_resources_ok =
d92524f1 6271 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
53a5351d 6272 other_type_used);
c5aa993b 6273 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6274 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 6275
c5aa993b 6276 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6277 error (_("Target can only support one kind of HW watchpoint at a time."));
c5aa993b 6278 }
c906108c 6279
4d28f7a8
KB
6280 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
6281 watchpoint could not be set. */
6282 if (!mem_cnt || target_resources_ok <= 0)
6283 bp_type = bp_watchpoint;
6284
d983da9c
DJ
6285 frame = block_innermost_frame (exp_valid_block);
6286 if (frame)
6287 prev_frame = get_prev_frame (frame);
6288 else
6289 prev_frame = NULL;
6290
6291 /* If the expression is "local", then set up a "watchpoint scope"
6292 breakpoint at the point where we've left the scope of the watchpoint
6293 expression. Create the scope breakpoint before the watchpoint, so
6294 that we will encounter it first in bpstat_stop_status. */
6295 if (innermost_block && prev_frame)
6296 {
6297 scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
6298 bp_watchpoint_scope);
6299
6300 scope_breakpoint->enable_state = bp_enabled;
6301
6302 /* Automatically delete the breakpoint when it hits. */
6303 scope_breakpoint->disposition = disp_del;
6304
6305 /* Only break in the proper frame (help with recursion). */
6306 scope_breakpoint->frame_id = get_frame_id (prev_frame);
6307
6308 /* Set the address at which we will stop. */
6309 scope_breakpoint->loc->requested_address
6310 = get_frame_pc (prev_frame);
6311 scope_breakpoint->loc->address
6312 = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
6313 scope_breakpoint->type);
6314 }
6315
c906108c 6316 /* Now set up the breakpoint. */
4d28f7a8 6317 b = set_raw_breakpoint (sal, bp_type);
c906108c
SS
6318 set_breakpoint_count (breakpoint_count + 1);
6319 b->number = breakpoint_count;
37e4754d 6320 b->thread = thread;
b5de0fa7 6321 b->disposition = disp_donttouch;
c906108c
SS
6322 b->exp = exp;
6323 b->exp_valid_block = exp_valid_block;
6324 b->exp_string = savestring (exp_start, exp_end - exp_start);
6325 b->val = val;
fa4727a6 6326 b->val_valid = 1;
511a6cd4 6327 b->loc->cond = cond;
c906108c
SS
6328 if (cond_start)
6329 b->cond_string = savestring (cond_start, cond_end - cond_start);
6330 else
6331 b->cond_string = 0;
c5aa993b 6332
c906108c 6333 if (frame)
d983da9c 6334 b->watchpoint_frame = get_frame_id (frame);
c906108c 6335 else
35a487f1 6336 b->watchpoint_frame = null_frame_id;
c906108c 6337
d983da9c 6338 if (scope_breakpoint != NULL)
c906108c 6339 {
d983da9c
DJ
6340 /* The scope breakpoint is related to the watchpoint. We will
6341 need to act on them together. */
6342 b->related_breakpoint = scope_breakpoint;
6343 scope_breakpoint->related_breakpoint = b;
c906108c 6344 }
d983da9c 6345
c906108c
SS
6346 value_free_to_mark (mark);
6347 mention (b);
b60e7edf 6348 update_global_location_list (1);
c906108c
SS
6349}
6350
6351/* Return count of locations need to be watched and can be handled
6352 in hardware. If the watchpoint can not be handled
6353 in hardware return zero. */
6354
c906108c 6355static int
fba45db2 6356can_use_hardware_watchpoint (struct value *v)
c906108c
SS
6357{
6358 int found_memory_cnt = 0;
2e70b7b9 6359 struct value *head = v;
c906108c
SS
6360
6361 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 6362 if (!can_use_hw_watchpoints)
c906108c 6363 return 0;
c5aa993b 6364
5c44784c
JM
6365 /* Make sure that the value of the expression depends only upon
6366 memory contents, and values computed from them within GDB. If we
6367 find any register references or function calls, we can't use a
6368 hardware watchpoint.
6369
6370 The idea here is that evaluating an expression generates a series
6371 of values, one holding the value of every subexpression. (The
6372 expression a*b+c has five subexpressions: a, b, a*b, c, and
6373 a*b+c.) GDB's values hold almost enough information to establish
6374 the criteria given above --- they identify memory lvalues,
6375 register lvalues, computed values, etcetera. So we can evaluate
6376 the expression, and then scan the chain of values that leaves
6377 behind to decide whether we can detect any possible change to the
6378 expression's final value using only hardware watchpoints.
6379
6380 However, I don't think that the values returned by inferior
6381 function calls are special in any way. So this function may not
6382 notice that an expression involving an inferior function call
6383 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 6384 for (; v; v = value_next (v))
c906108c 6385 {
5c44784c 6386 if (VALUE_LVAL (v) == lval_memory)
c906108c 6387 {
d69fe07e 6388 if (value_lazy (v))
5c44784c
JM
6389 /* A lazy memory lvalue is one that GDB never needed to fetch;
6390 we either just used its address (e.g., `a' in `a.b') or
6391 we never needed it at all (e.g., `a' in `a,b'). */
6392 ;
53a5351d 6393 else
5c44784c
JM
6394 {
6395 /* Ahh, memory we actually used! Check if we can cover
6396 it with hardware watchpoints. */
df407dfe 6397 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
6398
6399 /* We only watch structs and arrays if user asked for it
6400 explicitly, never if they just happen to appear in a
6401 middle of some value chain. */
6402 if (v == head
6403 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
6404 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
6405 {
42ae5230 6406 CORE_ADDR vaddr = value_address (v);
df407dfe 6407 int len = TYPE_LENGTH (value_type (v));
2e70b7b9 6408
d92524f1 6409 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
2e70b7b9
MS
6410 return 0;
6411 else
6412 found_memory_cnt++;
6413 }
5c44784c 6414 }
c5aa993b 6415 }
5086187c
AC
6416 else if (VALUE_LVAL (v) != not_lval
6417 && deprecated_value_modifiable (v) == 0)
53a5351d 6418 return 0; /* ??? What does this represent? */
5086187c 6419 else if (VALUE_LVAL (v) == lval_register)
53a5351d 6420 return 0; /* cannot watch a register with a HW watchpoint */
c906108c
SS
6421 }
6422
6423 /* The expression itself looks suitable for using a hardware
6424 watchpoint, but give the target machine a chance to reject it. */
6425 return found_memory_cnt;
6426}
6427
8b93c638 6428void
fba45db2 6429watch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6430{
6431 watch_command (arg, from_tty);
6432}
8926118c 6433
c5aa993b 6434static void
fba45db2 6435watch_command (char *arg, int from_tty)
c906108c 6436{
53a5351d 6437 watch_command_1 (arg, hw_write, from_tty);
c906108c
SS
6438}
6439
8b93c638 6440void
fba45db2 6441rwatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6442{
6443 rwatch_command (arg, from_tty);
6444}
8926118c 6445
c5aa993b 6446static void
fba45db2 6447rwatch_command (char *arg, int from_tty)
c906108c 6448{
53a5351d 6449 watch_command_1 (arg, hw_read, from_tty);
c906108c
SS
6450}
6451
8b93c638 6452void
fba45db2 6453awatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6454{
6455 awatch_command (arg, from_tty);
6456}
8926118c 6457
c5aa993b 6458static void
fba45db2 6459awatch_command (char *arg, int from_tty)
c906108c 6460{
53a5351d 6461 watch_command_1 (arg, hw_access, from_tty);
c906108c 6462}
c906108c 6463\f
c5aa993b 6464
43ff13b4 6465/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
6466 because it uses the mechanisms of breakpoints. */
6467
bfec99b2
PA
6468struct until_break_command_continuation_args
6469{
6470 struct breakpoint *breakpoint;
6471 struct breakpoint *breakpoint2;
6472};
6473
43ff13b4
JM
6474/* This function is called by fetch_inferior_event via the
6475 cmd_continuation pointer, to complete the until command. It takes
6476 care of cleaning up the temporary breakpoints set up by the until
6477 command. */
c2c6d25f 6478static void
604ead4a 6479until_break_command_continuation (void *arg)
43ff13b4 6480{
bfec99b2
PA
6481 struct until_break_command_continuation_args *a = arg;
6482
6483 delete_breakpoint (a->breakpoint);
6484 if (a->breakpoint2)
6485 delete_breakpoint (a->breakpoint2);
43ff13b4
JM
6486}
6487
c906108c 6488void
ae66c1fc 6489until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
6490{
6491 struct symtabs_and_lines sals;
6492 struct symtab_and_line sal;
206415a3
DJ
6493 struct frame_info *frame = get_selected_frame (NULL);
6494 struct frame_info *prev_frame = get_prev_frame (frame);
c906108c 6495 struct breakpoint *breakpoint;
f107f563 6496 struct breakpoint *breakpoint2 = NULL;
c906108c
SS
6497 struct cleanup *old_chain;
6498
6499 clear_proceed_status ();
6500
6501 /* Set a breakpoint where the user wants it and at return from
6502 this function */
c5aa993b 6503
c906108c
SS
6504 if (default_breakpoint_valid)
6505 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 6506 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 6507 else
53a5351d 6508 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 6509 0, (char ***) NULL, NULL);
c5aa993b 6510
c906108c 6511 if (sals.nelts != 1)
8a3fe4f8 6512 error (_("Couldn't get information on specified line."));
c5aa993b 6513
c906108c 6514 sal = sals.sals[0];
b8c9b27d 6515 xfree (sals.sals); /* malloc'd, so freed */
c5aa993b 6516
c906108c 6517 if (*arg)
8a3fe4f8 6518 error (_("Junk at end of arguments."));
c5aa993b 6519
c906108c 6520 resolve_sal_pc (&sal);
c5aa993b 6521
ae66c1fc
EZ
6522 if (anywhere)
6523 /* If the user told us to continue until a specified location,
6524 we don't specify a frame at which we need to stop. */
6525 breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
6526 else
6527 /* Otherwise, specify the current frame, because we want to stop only
6528 at the very same frame. */
206415a3 6529 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame),
ae66c1fc 6530 bp_until);
c5aa993b 6531
f107f563 6532 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 6533
ae66c1fc
EZ
6534 /* Keep within the current frame, or in frames called by the current
6535 one. */
c906108c
SS
6536 if (prev_frame)
6537 {
30f7db39
AC
6538 sal = find_pc_line (get_frame_pc (prev_frame), 0);
6539 sal.pc = get_frame_pc (prev_frame);
f107f563
VP
6540 breakpoint2 = set_momentary_breakpoint (sal, get_frame_id (prev_frame),
6541 bp_until);
6542 make_cleanup_delete_breakpoint (breakpoint2);
c906108c 6543 }
c5aa993b 6544
c906108c 6545 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563
VP
6546
6547 /* If we are running asynchronously, and proceed call above has actually
6548 managed to start the target, arrange for breakpoints to be
6549 deleted when the target stops. Otherwise, we're already stopped and
6550 delete breakpoints via cleanup chain. */
6551
8ea051c5 6552 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 6553 {
bfec99b2
PA
6554 struct until_break_command_continuation_args *args;
6555 args = xmalloc (sizeof (*args));
f107f563 6556
bfec99b2
PA
6557 args->breakpoint = breakpoint;
6558 args->breakpoint2 = breakpoint2;
f107f563
VP
6559
6560 discard_cleanups (old_chain);
95e54da7
PA
6561 add_continuation (inferior_thread (),
6562 until_break_command_continuation, args,
604ead4a 6563 xfree);
f107f563
VP
6564 }
6565 else
c5aa993b 6566 do_cleanups (old_chain);
c906108c 6567}
ae66c1fc 6568
c906108c 6569static void
fba45db2 6570ep_skip_leading_whitespace (char **s)
c906108c 6571{
c5aa993b
JM
6572 if ((s == NULL) || (*s == NULL))
6573 return;
6574 while (isspace (**s))
6575 *s += 1;
c906108c 6576}
c5aa993b 6577
c906108c
SS
6578/* This function attempts to parse an optional "if <cond>" clause
6579 from the arg string. If one is not found, it returns NULL.
c5aa993b 6580
c906108c
SS
6581 Else, it returns a pointer to the condition string. (It does not
6582 attempt to evaluate the string against a particular block.) And,
6583 it updates arg to point to the first character following the parsed
6584 if clause in the arg string. */
53a5351d 6585
c906108c 6586static char *
fba45db2 6587ep_parse_optional_if_clause (char **arg)
c906108c 6588{
c5aa993b
JM
6589 char *cond_string;
6590
6591 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 6592 return NULL;
c5aa993b 6593
c906108c
SS
6594 /* Skip the "if" keyword. */
6595 (*arg) += 2;
c5aa993b 6596
c906108c
SS
6597 /* Skip any extra leading whitespace, and record the start of the
6598 condition string. */
6599 ep_skip_leading_whitespace (arg);
6600 cond_string = *arg;
c5aa993b 6601
c906108c
SS
6602 /* Assume that the condition occupies the remainder of the arg string. */
6603 (*arg) += strlen (cond_string);
c5aa993b 6604
c906108c
SS
6605 return cond_string;
6606}
c5aa993b 6607
c906108c
SS
6608/* This function attempts to parse an optional filename from the arg
6609 string. If one is not found, it returns NULL.
c5aa993b 6610
c906108c
SS
6611 Else, it returns a pointer to the parsed filename. (This function
6612 makes no attempt to verify that a file of that name exists, or is
6613 accessible.) And, it updates arg to point to the first character
6614 following the parsed filename in the arg string.
c5aa993b 6615
c906108c
SS
6616 Note that clients needing to preserve the returned filename for
6617 future access should copy it to their own buffers. */
6618static char *
fba45db2 6619ep_parse_optional_filename (char **arg)
c906108c 6620{
c5aa993b
JM
6621 static char filename[1024];
6622 char *arg_p = *arg;
6623 int i;
6624 char c;
6625
c906108c
SS
6626 if ((*arg_p == '\0') || isspace (*arg_p))
6627 return NULL;
c5aa993b
JM
6628
6629 for (i = 0;; i++)
c906108c
SS
6630 {
6631 c = *arg_p;
6632 if (isspace (c))
c5aa993b 6633 c = '\0';
c906108c
SS
6634 filename[i] = c;
6635 if (c == '\0')
c5aa993b 6636 break;
c906108c
SS
6637 arg_p++;
6638 }
6639 *arg = arg_p;
c5aa993b 6640
c906108c
SS
6641 return filename;
6642}
c5aa993b 6643
c906108c
SS
6644/* Commands to deal with catching events, such as signals, exceptions,
6645 process start/exit, etc. */
c5aa993b
JM
6646
6647typedef enum
6648{
44feb3ce
TT
6649 catch_fork_temporary, catch_vfork_temporary,
6650 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
6651}
6652catch_fork_kind;
6653
c906108c 6654static void
44feb3ce 6655catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 6656{
c5aa993b 6657 char *cond_string = NULL;
44feb3ce
TT
6658 catch_fork_kind fork_kind;
6659 int tempflag;
6660
6661 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
6662 tempflag = (fork_kind == catch_fork_temporary
6663 || fork_kind == catch_vfork_temporary);
c5aa993b 6664
44feb3ce
TT
6665 if (!arg)
6666 arg = "";
c906108c 6667 ep_skip_leading_whitespace (&arg);
c5aa993b 6668
c906108c 6669 /* The allowed syntax is:
c5aa993b
JM
6670 catch [v]fork
6671 catch [v]fork if <cond>
6672
c906108c
SS
6673 First, check if there's an if clause. */
6674 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 6675
c906108c 6676 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6677 error (_("Junk at end of arguments."));
c5aa993b 6678
c906108c
SS
6679 /* If this target supports it, create a fork or vfork catchpoint
6680 and enable reporting of such events. */
c5aa993b
JM
6681 switch (fork_kind)
6682 {
44feb3ce
TT
6683 case catch_fork_temporary:
6684 case catch_fork_permanent:
ce78b96d
JB
6685 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6686 &catch_fork_breakpoint_ops);
c906108c 6687 break;
44feb3ce
TT
6688 case catch_vfork_temporary:
6689 case catch_vfork_permanent:
ce78b96d
JB
6690 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6691 &catch_vfork_breakpoint_ops);
c906108c 6692 break;
c5aa993b 6693 default:
8a3fe4f8 6694 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 6695 break;
c5aa993b 6696 }
c906108c
SS
6697}
6698
6699static void
44feb3ce 6700catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 6701{
44feb3ce 6702 int tempflag;
c5aa993b 6703 char *cond_string = NULL;
c906108c 6704
44feb3ce
TT
6705 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6706
6707 if (!arg)
6708 arg = "";
c906108c
SS
6709 ep_skip_leading_whitespace (&arg);
6710
6711 /* The allowed syntax is:
c5aa993b
JM
6712 catch exec
6713 catch exec if <cond>
c906108c
SS
6714
6715 First, check if there's an if clause. */
6716 cond_string = ep_parse_optional_if_clause (&arg);
6717
6718 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6719 error (_("Junk at end of arguments."));
c906108c
SS
6720
6721 /* If this target supports it, create an exec catchpoint
6722 and enable reporting of such events. */
fe798b75 6723 create_catchpoint (tempflag, cond_string, &catch_exec_breakpoint_ops);
c906108c 6724}
c5aa993b 6725
3086aeae
DJ
6726static enum print_stop_action
6727print_exception_catchpoint (struct breakpoint *b)
6728{
ade92717 6729 int bp_temp, bp_throw;
3086aeae 6730
ade92717 6731 annotate_catchpoint (b->number);
3086aeae 6732
ade92717
AR
6733 bp_throw = strstr (b->addr_string, "throw") != NULL;
6734 if (b->loc->address != b->loc->requested_address)
6735 breakpoint_adjustment_warning (b->loc->requested_address,
6736 b->loc->address,
6737 b->number, 1);
df2b6d2d 6738 bp_temp = b->disposition == disp_del;
ade92717
AR
6739 ui_out_text (uiout,
6740 bp_temp ? "Temporary catchpoint "
6741 : "Catchpoint ");
6742 if (!ui_out_is_mi_like_p (uiout))
6743 ui_out_field_int (uiout, "bkptno", b->number);
6744 ui_out_text (uiout,
c0b37c48
AR
6745 bp_throw ? " (exception thrown), "
6746 : " (exception caught), ");
ade92717
AR
6747 if (ui_out_is_mi_like_p (uiout))
6748 {
6749 ui_out_field_string (uiout, "reason",
6750 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
6751 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6752 ui_out_field_int (uiout, "bkptno", b->number);
6753 }
3086aeae
DJ
6754 return PRINT_SRC_AND_LOC;
6755}
6756
6757static void
6758print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6759{
79a45b7d
TT
6760 struct value_print_options opts;
6761 get_user_print_options (&opts);
6762 if (opts.addressprint)
3086aeae
DJ
6763 {
6764 annotate_field (4);
604133b5
AR
6765 if (b->loc == NULL || b->loc->shlib_disabled)
6766 ui_out_field_string (uiout, "addr", "<PENDING>");
6767 else
6768 ui_out_field_core_addr (uiout, "addr", b->loc->address);
3086aeae
DJ
6769 }
6770 annotate_field (5);
604133b5
AR
6771 if (b->loc)
6772 *last_addr = b->loc->address;
3086aeae
DJ
6773 if (strstr (b->addr_string, "throw") != NULL)
6774 ui_out_field_string (uiout, "what", "exception throw");
6775 else
6776 ui_out_field_string (uiout, "what", "exception catch");
6777}
6778
6779static void
6780print_mention_exception_catchpoint (struct breakpoint *b)
6781{
ade92717
AR
6782 int bp_temp;
6783 int bp_throw;
6784
df2b6d2d 6785 bp_temp = b->disposition == disp_del;
ade92717
AR
6786 bp_throw = strstr (b->addr_string, "throw") != NULL;
6787 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
6788 : _("Catchpoint "));
6789 ui_out_field_int (uiout, "bkptno", b->number);
6790 ui_out_text (uiout, bp_throw ? _(" (throw)")
6791 : _(" (catch)"));
3086aeae
DJ
6792}
6793
6794static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
6795 NULL, /* insert */
6796 NULL, /* remove */
6797 NULL, /* breakpoint_hit */
3086aeae
DJ
6798 print_exception_catchpoint,
6799 print_one_exception_catchpoint,
6800 print_mention_exception_catchpoint
6801};
6802
6803static int
6804handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6805 enum exception_event_kind ex_event, int from_tty)
6806{
604133b5
AR
6807 char *trigger_func_name;
6808
3086aeae 6809 if (ex_event == EX_EVENT_CATCH)
604133b5 6810 trigger_func_name = "__cxa_begin_catch";
3086aeae 6811 else
604133b5 6812 trigger_func_name = "__cxa_throw";
3086aeae 6813
604133b5
AR
6814 break_command_really (trigger_func_name, cond_string, -1,
6815 0 /* condition and thread are valid. */,
1042e4c0 6816 tempflag, 0, 0,
604133b5
AR
6817 0,
6818 AUTO_BOOLEAN_TRUE /* pending */,
41447f92
VP
6819 &gnu_v3_exception_catchpoint_ops, from_tty,
6820 1 /* enabled */);
3086aeae 6821
3086aeae
DJ
6822 return 1;
6823}
6824
c5aa993b 6825/* Deal with "catch catch" and "catch throw" commands */
c906108c
SS
6826
6827static void
fba45db2
KB
6828catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6829 int tempflag, int from_tty)
c906108c 6830{
c5aa993b
JM
6831 char *cond_string = NULL;
6832 struct symtab_and_line *sal = NULL;
6833
44feb3ce
TT
6834 if (!arg)
6835 arg = "";
c906108c 6836 ep_skip_leading_whitespace (&arg);
c5aa993b 6837
c906108c
SS
6838 cond_string = ep_parse_optional_if_clause (&arg);
6839
6840 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6841 error (_("Junk at end of arguments."));
c906108c 6842
059fb39f
PM
6843 if (ex_event != EX_EVENT_THROW
6844 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 6845 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 6846
3086aeae
DJ
6847 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6848 return;
6849
8a3fe4f8 6850 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
6851}
6852
44feb3ce
TT
6853/* Implementation of "catch catch" command. */
6854
6855static void
6856catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
6857{
6858 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6859 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
6860}
6861
6862/* Implementation of "catch throw" command. */
6863
6864static void
6865catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
6866{
6867 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6868 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
6869}
6870
f7f9143b
JB
6871/* Create a breakpoint struct for Ada exception catchpoints. */
6872
6873static void
6874create_ada_exception_breakpoint (struct symtab_and_line sal,
6875 char *addr_string,
6876 char *exp_string,
6877 char *cond_string,
6878 struct expression *cond,
6879 struct breakpoint_ops *ops,
6880 int tempflag,
6881 int from_tty)
6882{
6883 struct breakpoint *b;
6884
6885 if (from_tty)
6886 {
6887 describe_other_breakpoints (sal.pc, sal.section, -1);
6888 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
6889 version for exception catchpoints, because two catchpoints
6890 used for different exception names will use the same address.
6891 In this case, a "breakpoint ... also set at..." warning is
6892 unproductive. Besides. the warning phrasing is also a bit
6893 inapropriate, we should use the word catchpoint, and tell
6894 the user what type of catchpoint it is. The above is good
6895 enough for now, though. */
6896 }
6897
6898 b = set_raw_breakpoint (sal, bp_breakpoint);
6899 set_breakpoint_count (breakpoint_count + 1);
6900
6901 b->enable_state = bp_enabled;
6902 b->disposition = tempflag ? disp_del : disp_donttouch;
6903 b->number = breakpoint_count;
6904 b->ignore_count = 0;
511a6cd4 6905 b->loc->cond = cond;
f7f9143b
JB
6906 b->addr_string = addr_string;
6907 b->language = language_ada;
6908 b->cond_string = cond_string;
6909 b->exp_string = exp_string;
6910 b->thread = -1;
6911 b->ops = ops;
f7f9143b
JB
6912
6913 mention (b);
b60e7edf 6914 update_global_location_list (1);
f7f9143b
JB
6915}
6916
6917/* Implement the "catch exception" command. */
6918
6919static void
44feb3ce
TT
6920catch_ada_exception_command (char *arg, int from_tty,
6921 struct cmd_list_element *command)
f7f9143b 6922{
44feb3ce 6923 int tempflag;
f7f9143b
JB
6924 struct symtab_and_line sal;
6925 enum bptype type;
6926 char *addr_string = NULL;
6927 char *exp_string = NULL;
6928 char *cond_string = NULL;
6929 struct expression *cond = NULL;
6930 struct breakpoint_ops *ops = NULL;
6931
44feb3ce
TT
6932 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6933
6934 if (!arg)
6935 arg = "";
f7f9143b
JB
6936 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
6937 &cond_string, &cond, &ops);
6938 create_ada_exception_breakpoint (sal, addr_string, exp_string,
6939 cond_string, cond, ops, tempflag,
6940 from_tty);
6941}
6942
6943/* Implement the "catch assert" command. */
6944
6945static void
44feb3ce 6946catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
f7f9143b 6947{
44feb3ce 6948 int tempflag;
f7f9143b
JB
6949 struct symtab_and_line sal;
6950 char *addr_string = NULL;
6951 struct breakpoint_ops *ops = NULL;
6952
44feb3ce
TT
6953 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6954
6955 if (!arg)
6956 arg = "";
f7f9143b
JB
6957 sal = ada_decode_assert_location (arg, &addr_string, &ops);
6958 create_ada_exception_breakpoint (sal, addr_string, NULL, NULL, NULL, ops,
6959 tempflag, from_tty);
6960}
6961
c906108c 6962static void
fba45db2 6963catch_command (char *arg, int from_tty)
c906108c 6964{
44feb3ce 6965 error (_("Catch requires an event name."));
c906108c
SS
6966}
6967\f
6968
6969static void
fba45db2 6970tcatch_command (char *arg, int from_tty)
c906108c 6971{
44feb3ce 6972 error (_("Catch requires an event name."));
c906108c
SS
6973}
6974
80f8a6eb 6975/* Delete breakpoints by address or line. */
c906108c
SS
6976
6977static void
fba45db2 6978clear_command (char *arg, int from_tty)
c906108c 6979{
d6e956e5
VP
6980 struct breakpoint *b;
6981 VEC(breakpoint_p) *found = 0;
6982 int ix;
c906108c
SS
6983 int default_match;
6984 struct symtabs_and_lines sals;
6985 struct symtab_and_line sal;
c906108c
SS
6986 int i;
6987
6988 if (arg)
6989 {
6990 sals = decode_line_spec (arg, 1);
6991 default_match = 0;
6992 }
6993 else
6994 {
c5aa993b 6995 sals.sals = (struct symtab_and_line *)
c906108c 6996 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 6997 make_cleanup (xfree, sals.sals);
fe39c653 6998 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
6999 sal.line = default_breakpoint_line;
7000 sal.symtab = default_breakpoint_symtab;
7001 sal.pc = default_breakpoint_address;
7002 if (sal.symtab == 0)
8a3fe4f8 7003 error (_("No source file specified."));
c906108c
SS
7004
7005 sals.sals[0] = sal;
7006 sals.nelts = 1;
7007
7008 default_match = 1;
7009 }
7010
ed0616c6
VP
7011 /* We don't call resolve_sal_pc here. That's not
7012 as bad as it seems, because all existing breakpoints
7013 typically have both file/line and pc set. So, if
7014 clear is given file/line, we can match this to existing
7015 breakpoint without obtaining pc at all.
7016
7017 We only support clearing given the address explicitly
7018 present in breakpoint table. Say, we've set breakpoint
7019 at file:line. There were several PC values for that file:line,
7020 due to optimization, all in one block.
7021 We've picked one PC value. If "clear" is issued with another
7022 PC corresponding to the same file:line, the breakpoint won't
7023 be cleared. We probably can still clear the breakpoint, but
7024 since the other PC value is never presented to user, user
7025 can only find it by guessing, and it does not seem important
7026 to support that. */
7027
c906108c 7028 /* For each line spec given, delete bps which correspond
80f8a6eb
MS
7029 to it. Do it in two passes, solely to preserve the current
7030 behavior that from_tty is forced true if we delete more than
7031 one breakpoint. */
c906108c 7032
80f8a6eb 7033 found = NULL;
c906108c
SS
7034 for (i = 0; i < sals.nelts; i++)
7035 {
7036 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
7037 If line given (pc == 0), clear all bpts on specified line.
7038 If defaulting, clear all bpts on default line
c906108c 7039 or at default pc.
c5aa993b
JM
7040
7041 defaulting sal.pc != 0 tests to do
7042
7043 0 1 pc
7044 1 1 pc _and_ line
7045 0 0 line
7046 1 0 <can't happen> */
c906108c
SS
7047
7048 sal = sals.sals[i];
c906108c 7049
d6e956e5
VP
7050 /* Find all matching breakpoints and add them to
7051 'found'. */
7052 ALL_BREAKPOINTS (b)
c5aa993b 7053 {
0d381245 7054 int match = 0;
80f8a6eb
MS
7055 /* Are we going to delete b? */
7056 if (b->type != bp_none
7057 && b->type != bp_watchpoint
7058 && b->type != bp_hardware_watchpoint
7059 && b->type != bp_read_watchpoint
0d381245
VP
7060 && b->type != bp_access_watchpoint)
7061 {
7062 struct bp_location *loc = b->loc;
7063 for (; loc; loc = loc->next)
7064 {
7065 int pc_match = sal.pc
7066 && (loc->address == sal.pc)
7067 && (!section_is_overlay (loc->section)
7068 || loc->section == sal.section);
7069 int line_match = ((default_match || (0 == sal.pc))
7070 && b->source_file != NULL
7071 && sal.symtab != NULL
7072 && strcmp (b->source_file, sal.symtab->filename) == 0
7073 && b->line_number == sal.line);
7074 if (pc_match || line_match)
7075 {
7076 match = 1;
7077 break;
7078 }
7079 }
7080 }
7081
7082 if (match)
d6e956e5 7083 VEC_safe_push(breakpoint_p, found, b);
c906108c 7084 }
80f8a6eb
MS
7085 }
7086 /* Now go thru the 'found' chain and delete them. */
d6e956e5 7087 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
7088 {
7089 if (arg)
8a3fe4f8 7090 error (_("No breakpoint at %s."), arg);
80f8a6eb 7091 else
8a3fe4f8 7092 error (_("No breakpoint at this line."));
80f8a6eb 7093 }
c906108c 7094
d6e956e5 7095 if (VEC_length(breakpoint_p, found) > 1)
80f8a6eb
MS
7096 from_tty = 1; /* Always report if deleted more than one */
7097 if (from_tty)
a3f17187 7098 {
d6e956e5 7099 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
7100 printf_unfiltered (_("Deleted breakpoint "));
7101 else
7102 printf_unfiltered (_("Deleted breakpoints "));
7103 }
80f8a6eb 7104 breakpoints_changed ();
d6e956e5
VP
7105
7106 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 7107 {
c5aa993b 7108 if (from_tty)
d6e956e5
VP
7109 printf_unfiltered ("%d ", b->number);
7110 delete_breakpoint (b);
c906108c 7111 }
80f8a6eb
MS
7112 if (from_tty)
7113 putchar_unfiltered ('\n');
c906108c
SS
7114}
7115\f
7116/* Delete breakpoint in BS if they are `delete' breakpoints and
7117 all breakpoints that are marked for deletion, whether hit or not.
7118 This is called after any breakpoint is hit, or after errors. */
7119
7120void
fba45db2 7121breakpoint_auto_delete (bpstat bs)
c906108c
SS
7122{
7123 struct breakpoint *b, *temp;
7124
7125 for (; bs; bs = bs->next)
20874c92
VP
7126 if (bs->breakpoint_at
7127 && bs->breakpoint_at->owner
7128 && bs->breakpoint_at->owner->disposition == disp_del
c906108c 7129 && bs->stop)
4f8d1dc6 7130 delete_breakpoint (bs->breakpoint_at->owner);
c906108c
SS
7131
7132 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7133 {
b5de0fa7 7134 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
7135 delete_breakpoint (b);
7136 }
c906108c
SS
7137}
7138
f7545552
TT
7139/* A cleanup function which destroys a vector. */
7140
7141static void
7142do_vec_free (void *p)
7143{
7144 VEC(bp_location_p) **vec = p;
7145 if (*vec)
7146 VEC_free (bp_location_p, *vec);
7147}
7148
4cd9bd08 7149/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
7150 into the inferior, only remove already-inserted locations that no
7151 longer should be inserted. Functions that delete a breakpoint or
7152 breakpoints should pass false, so that deleting a breakpoint
7153 doesn't have the side effect of inserting the locations of other
7154 breakpoints that are marked not-inserted, but should_be_inserted
7155 returns true on them.
7156
7157 This behaviour is useful is situations close to tear-down -- e.g.,
7158 after an exec, while the target still has execution, but breakpoint
7159 shadows of the previous executable image should *NOT* be restored
7160 to the new image; or before detaching, where the target still has
7161 execution and wants to delete breakpoints from GDB's lists, and all
7162 breakpoints had already been removed from the inferior. */
7163
0d381245 7164static void
b60e7edf 7165update_global_location_list (int should_insert)
0d381245 7166{
74960c60
VP
7167 struct breakpoint *b;
7168 struct bp_location **next = &bp_location_chain;
7169 struct bp_location *loc;
7170 struct bp_location *loc2;
74960c60
VP
7171 VEC(bp_location_p) *old_locations = NULL;
7172 int ret;
7173 int ix;
f7545552
TT
7174 struct cleanup *cleanups;
7175
7176 cleanups = make_cleanup (do_vec_free, &old_locations);
74960c60
VP
7177 /* Store old locations for future reference. */
7178 for (loc = bp_location_chain; loc; loc = loc->global_next)
7179 VEC_safe_push (bp_location_p, old_locations, loc);
0d381245 7180
74960c60
VP
7181 bp_location_chain = NULL;
7182 ALL_BREAKPOINTS (b)
0d381245 7183 {
74960c60 7184 for (loc = b->loc; loc; loc = loc->next)
0d381245 7185 {
74960c60
VP
7186 *next = loc;
7187 next = &(loc->global_next);
7188 *next = NULL;
0d381245 7189 }
74960c60
VP
7190 }
7191
7192 /* Identify bp_location instances that are no longer present in the new
7193 list, and therefore should be freed. Note that it's not necessary that
7194 those locations should be removed from inferior -- if there's another
7195 location at the same address (previously marked as duplicate),
7196 we don't need to remove/insert the location. */
7197 for (ix = 0; VEC_iterate(bp_location_p, old_locations, ix, loc); ++ix)
7198 {
7199 /* Tells if 'loc' is found amoung the new locations. If not, we
7200 have to free it. */
7201 int found_object = 0;
20874c92
VP
7202 /* Tells if the location should remain inserted in the target. */
7203 int keep_in_target = 0;
7204 int removed = 0;
74960c60
VP
7205 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7206 if (loc2 == loc)
7207 {
7208 found_object = 1;
7209 break;
7210 }
7211
7212 /* If this location is no longer present, and inserted, look if there's
7213 maybe a new location at the same address. If so, mark that one
7214 inserted, and don't remove this one. This is needed so that we
7215 don't have a time window where a breakpoint at certain location is not
7216 inserted. */
7217
7218 if (loc->inserted)
0d381245 7219 {
74960c60 7220 /* If the location is inserted now, we might have to remove it. */
74960c60
VP
7221
7222 if (found_object && should_be_inserted (loc))
7223 {
7224 /* The location is still present in the location list, and still
7225 should be inserted. Don't do anything. */
20874c92 7226 keep_in_target = 1;
74960c60
VP
7227 }
7228 else
7229 {
7230 /* The location is either no longer present, or got disabled.
7231 See if there's another location at the same address, in which
7232 case we don't need to remove this one from the target. */
7233 if (breakpoint_address_is_meaningful (loc->owner))
7234 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7235 {
7236 /* For the sake of should_insert_location. The
7237 call to check_duplicates will fix up this later. */
7238 loc2->duplicate = 0;
7239 if (should_be_inserted (loc2)
7240 && loc2 != loc && loc2->address == loc->address)
7241 {
7242 loc2->inserted = 1;
7243 loc2->target_info = loc->target_info;
20874c92 7244 keep_in_target = 1;
74960c60
VP
7245 break;
7246 }
7247 }
7248 }
7249
20874c92
VP
7250 if (!keep_in_target)
7251 {
7252 if (remove_breakpoint (loc, mark_uninserted))
7253 {
7254 /* This is just about all we can do. We could keep this
7255 location on the global list, and try to remove it next
7256 time, but there's no particular reason why we will
7257 succeed next time.
7258
7259 Note that at this point, loc->owner is still valid,
7260 as delete_breakpoint frees the breakpoint only
7261 after calling us. */
7262 printf_filtered (_("warning: Error removing breakpoint %d\n"),
7263 loc->owner->number);
7264 }
7265 removed = 1;
7266 }
0d381245 7267 }
74960c60
VP
7268
7269 if (!found_object)
1c5cfe86
PA
7270 {
7271 if (removed && non_stop)
20874c92
VP
7272 {
7273 /* This location was removed from the targets. In non-stop mode,
7274 a race condition is possible where we've removed a breakpoint,
7275 but stop events for that breakpoint are already queued and will
7276 arrive later. To suppress spurious SIGTRAPs reported to user,
7277 we keep this breakpoint location for a bit, and will retire it
7278 after we see 3 * thread_count events.
1c5cfe86 7279 The theory here is that reporting of events should,
20874c92
VP
7280 "on the average", be fair, so after that many event we'll see
7281 events from all threads that have anything of interest, and no
1c5cfe86 7282 longer need to keep this breakpoint. This is just a
20874c92 7283 heuristic, but if it's wrong, we'll report unexpected SIGTRAP,
1c5cfe86 7284 which is usability issue, but not a correctness problem. */
20874c92
VP
7285 loc->events_till_retirement = 3 * (thread_count () + 1);
7286 loc->owner = NULL;
20874c92 7287
1c5cfe86
PA
7288 VEC_safe_push (bp_location_p, moribund_locations, loc);
7289 }
7290 else
7291 free_bp_location (loc);
20874c92 7292 }
74960c60 7293 }
1c5cfe86 7294
74960c60
VP
7295 ALL_BREAKPOINTS (b)
7296 {
7297 check_duplicates (b);
0d381245 7298 }
74960c60 7299
50c71eaf 7300 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 7301 && (have_live_inferiors ()
2567c7d9 7302 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 7303 insert_breakpoint_locations ();
f7545552
TT
7304
7305 do_cleanups (cleanups);
74960c60
VP
7306}
7307
20874c92
VP
7308void
7309breakpoint_retire_moribund (void)
7310{
7311 struct bp_location *loc;
7312 int ix;
7313
7314 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
7315 if (--(loc->events_till_retirement) == 0)
7316 {
7317 free_bp_location (loc);
7318 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
7319 --ix;
7320 }
7321}
7322
74960c60 7323static void
b60e7edf 7324update_global_location_list_nothrow (int inserting)
74960c60
VP
7325{
7326 struct gdb_exception e;
7327 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 7328 update_global_location_list (inserting);
0d381245
VP
7329}
7330
a474d7c2
PA
7331/* Clear BPT from a BPS. */
7332static void
7333bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
7334{
7335 bpstat bs;
7336 for (bs = bps; bs; bs = bs->next)
7337 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
7338 {
7339 bs->breakpoint_at = NULL;
7340 bs->old_val = NULL;
7341 /* bs->commands will be freed later. */
7342 }
7343}
7344
7345/* Callback for iterate_over_threads. */
7346static int
7347bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
7348{
7349 struct breakpoint *bpt = data;
7350 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
7351 return 0;
7352}
7353
53a5351d
JM
7354/* Delete a breakpoint and clean up all traces of it in the data
7355 structures. */
c906108c
SS
7356
7357void
fba45db2 7358delete_breakpoint (struct breakpoint *bpt)
c906108c 7359{
52f0bd74 7360 struct breakpoint *b;
74960c60 7361 struct bp_location *loc, *next;
c906108c 7362
8a3fe4f8 7363 gdb_assert (bpt != NULL);
c906108c
SS
7364
7365 /* Has this bp already been deleted? This can happen because multiple
7366 lists can hold pointers to bp's. bpstat lists are especial culprits.
7367
7368 One example of this happening is a watchpoint's scope bp. When the
7369 scope bp triggers, we notice that the watchpoint is out of scope, and
7370 delete it. We also delete its scope bp. But the scope bp is marked
7371 "auto-deleting", and is already on a bpstat. That bpstat is then
7372 checked for auto-deleting bp's, which are deleted.
7373
7374 A real solution to this problem might involve reference counts in bp's,
7375 and/or giving them pointers back to their referencing bpstat's, and
7376 teaching delete_breakpoint to only free a bp's storage when no more
1272ad14 7377 references were extent. A cheaper bandaid was chosen. */
c906108c
SS
7378 if (bpt->type == bp_none)
7379 return;
7380
383f836e 7381 observer_notify_breakpoint_deleted (bpt->number);
c906108c 7382
c906108c
SS
7383 if (breakpoint_chain == bpt)
7384 breakpoint_chain = bpt->next;
7385
c906108c
SS
7386 ALL_BREAKPOINTS (b)
7387 if (b->next == bpt)
c5aa993b
JM
7388 {
7389 b->next = bpt->next;
7390 break;
7391 }
c906108c 7392
c906108c 7393 free_command_lines (&bpt->commands);
c906108c 7394 if (bpt->cond_string != NULL)
b8c9b27d 7395 xfree (bpt->cond_string);
c906108c 7396 if (bpt->addr_string != NULL)
b8c9b27d 7397 xfree (bpt->addr_string);
c906108c 7398 if (bpt->exp != NULL)
b8c9b27d 7399 xfree (bpt->exp);
c906108c 7400 if (bpt->exp_string != NULL)
b8c9b27d 7401 xfree (bpt->exp_string);
c906108c
SS
7402 if (bpt->val != NULL)
7403 value_free (bpt->val);
7404 if (bpt->source_file != NULL)
b8c9b27d 7405 xfree (bpt->source_file);
c906108c 7406 if (bpt->exec_pathname != NULL)
b8c9b27d 7407 xfree (bpt->exec_pathname);
c906108c
SS
7408
7409 /* Be sure no bpstat's are pointing at it after it's been freed. */
7410 /* FIXME, how can we find all bpstat's?
198757a8
VP
7411 We just check stop_bpstat for now. Note that we cannot just
7412 remove bpstats pointing at bpt from the stop_bpstat list
7413 entirely, as breakpoint commands are associated with the bpstat;
7414 if we remove it here, then the later call to
7415 bpstat_do_actions (&stop_bpstat);
7416 in event-top.c won't do anything, and temporary breakpoints
7417 with commands won't work. */
a474d7c2 7418
a474d7c2 7419 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
74960c60
VP
7420
7421 /* Now that breakpoint is removed from breakpoint
7422 list, update the global location list. This
7423 will remove locations that used to belong to
7424 this breakpoint. Do this before freeing
7425 the breakpoint itself, since remove_breakpoint
7426 looks at location's owner. It might be better
7427 design to have location completely self-contained,
7428 but it's not the case now. */
b60e7edf 7429 update_global_location_list (0);
74960c60
VP
7430
7431
c906108c
SS
7432 /* On the chance that someone will soon try again to delete this same
7433 bp, we mark it as deleted before freeing its storage. */
7434 bpt->type = bp_none;
7435
b8c9b27d 7436 xfree (bpt);
c906108c
SS
7437}
7438
4d6140d9
AC
7439static void
7440do_delete_breakpoint_cleanup (void *b)
7441{
7442 delete_breakpoint (b);
7443}
7444
7445struct cleanup *
7446make_cleanup_delete_breakpoint (struct breakpoint *b)
7447{
7448 return make_cleanup (do_delete_breakpoint_cleanup, b);
7449}
7450
c906108c 7451void
fba45db2 7452delete_command (char *arg, int from_tty)
c906108c
SS
7453{
7454 struct breakpoint *b, *temp;
7455
ea9365bb
TT
7456 dont_repeat ();
7457
c906108c
SS
7458 if (arg == 0)
7459 {
7460 int breaks_to_delete = 0;
7461
7462 /* Delete all breakpoints if no argument.
c5aa993b
JM
7463 Do not delete internal or call-dummy breakpoints, these
7464 have to be deleted with an explicit breakpoint number argument. */
7465 ALL_BREAKPOINTS (b)
7466 {
059fb39f
PM
7467 if (b->type != bp_call_dummy
7468 && b->type != bp_shlib_event
7469 && b->type != bp_thread_event
7470 && b->type != bp_overlay_event
0fd8e87f 7471 && b->type != bp_longjmp_master
059fb39f 7472 && b->number >= 0)
973d738b
DJ
7473 {
7474 breaks_to_delete = 1;
7475 break;
7476 }
c5aa993b 7477 }
c906108c
SS
7478
7479 /* Ask user only if there are some breakpoints to delete. */
7480 if (!from_tty
e2e0b3e5 7481 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 7482 {
c5aa993b
JM
7483 ALL_BREAKPOINTS_SAFE (b, temp)
7484 {
059fb39f
PM
7485 if (b->type != bp_call_dummy
7486 && b->type != bp_shlib_event
7487 && b->type != bp_thread_event
7488 && b->type != bp_overlay_event
0fd8e87f 7489 && b->type != bp_longjmp_master
059fb39f 7490 && b->number >= 0)
c5aa993b
JM
7491 delete_breakpoint (b);
7492 }
c906108c
SS
7493 }
7494 }
7495 else
7496 map_breakpoint_numbers (arg, delete_breakpoint);
7497}
7498
0d381245
VP
7499static int
7500all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 7501{
0d381245
VP
7502 for (; loc; loc = loc->next)
7503 if (!loc->shlib_disabled)
7504 return 0;
7505 return 1;
fe3f5fa8
VP
7506}
7507
776592bf
DE
7508/* Subroutine of update_breakpoint_locations to simplify it.
7509 Return non-zero if multiple fns in list LOC have the same name.
7510 Null names are ignored. */
7511
7512static int
7513ambiguous_names_p (struct bp_location *loc)
7514{
7515 struct bp_location *l;
7516 htab_t htab = htab_create_alloc (13, htab_hash_string,
7517 (int (*) (const void *, const void *)) streq,
7518 NULL, xcalloc, xfree);
7519
7520 for (l = loc; l != NULL; l = l->next)
7521 {
7522 const char **slot;
7523 const char *name = l->function_name;
7524
7525 /* Allow for some names to be NULL, ignore them. */
7526 if (name == NULL)
7527 continue;
7528
7529 slot = (const char **) htab_find_slot (htab, (const void *) name,
7530 INSERT);
7531 /* NOTE: We can assume slot != NULL here because xcalloc never returns
7532 NULL. */
7533 if (*slot != NULL)
7534 {
7535 htab_delete (htab);
7536 return 1;
7537 }
7538 *slot = name;
7539 }
7540
7541 htab_delete (htab);
7542 return 0;
7543}
7544
fe3f5fa8 7545static void
0d381245
VP
7546update_breakpoint_locations (struct breakpoint *b,
7547 struct symtabs_and_lines sals)
fe3f5fa8
VP
7548{
7549 int i;
7550 char *s;
0d381245
VP
7551 struct bp_location *existing_locations = b->loc;
7552
7553 /* If there's no new locations, and all existing locations
7554 are pending, don't do anything. This optimizes
7555 the common case where all locations are in the same
7556 shared library, that was unloaded. We'd like to
7557 retain the location, so that when the library
7558 is loaded again, we don't loose the enabled/disabled
7559 status of the individual locations. */
7560 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
7561 return;
7562
fe3f5fa8
VP
7563 b->loc = NULL;
7564
0d381245 7565 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 7566 {
0d381245 7567 struct bp_location *new_loc =
39d61571 7568 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 7569
0d381245
VP
7570 /* Reparse conditions, they might contain references to the
7571 old symtab. */
7572 if (b->cond_string != NULL)
7573 {
7574 struct gdb_exception e;
fe3f5fa8 7575
0d381245
VP
7576 s = b->cond_string;
7577 TRY_CATCH (e, RETURN_MASK_ERROR)
7578 {
7579 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7580 0);
7581 }
7582 if (e.reason < 0)
7583 {
7584 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7585 b->number, e.message);
7586 new_loc->enabled = 0;
7587 }
7588 }
fe3f5fa8 7589
0d381245
VP
7590 if (b->source_file != NULL)
7591 xfree (b->source_file);
7592 if (sals.sals[i].symtab == NULL)
7593 b->source_file = NULL;
7594 else
1b36a34b 7595 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 7596
0d381245
VP
7597 if (b->line_number == 0)
7598 b->line_number = sals.sals[i].line;
7599 }
fe3f5fa8 7600
514f746b
AR
7601 /* Update locations of permanent breakpoints. */
7602 if (b->enable_state == bp_permanent)
7603 make_breakpoint_permanent (b);
7604
0d381245
VP
7605 /* If possible, carry over 'disable' status from existing breakpoints. */
7606 {
7607 struct bp_location *e = existing_locations;
776592bf
DE
7608 /* If there are multiple breakpoints with the same function name,
7609 e.g. for inline functions, comparing function names won't work.
7610 Instead compare pc addresses; this is just a heuristic as things
7611 may have moved, but in practice it gives the correct answer
7612 often enough until a better solution is found. */
7613 int have_ambiguous_names = ambiguous_names_p (b->loc);
7614
0d381245
VP
7615 for (; e; e = e->next)
7616 {
7617 if (!e->enabled && e->function_name)
7618 {
7619 struct bp_location *l = b->loc;
776592bf
DE
7620 if (have_ambiguous_names)
7621 {
7622 for (; l; l = l->next)
7623 if (e->address == l->address)
7624 {
7625 l->enabled = 0;
7626 break;
7627 }
7628 }
7629 else
7630 {
7631 for (; l; l = l->next)
7632 if (l->function_name
7633 && strcmp (e->function_name, l->function_name) == 0)
7634 {
7635 l->enabled = 0;
7636 break;
7637 }
7638 }
0d381245
VP
7639 }
7640 }
7641 }
fe3f5fa8 7642
b60e7edf 7643 update_global_location_list (1);
fe3f5fa8
VP
7644}
7645
7646
c906108c
SS
7647/* Reset a breakpoint given it's struct breakpoint * BINT.
7648 The value we return ends up being the return value from catch_errors.
7649 Unused in this case. */
7650
7651static int
4efb68b1 7652breakpoint_re_set_one (void *bint)
c906108c 7653{
53a5351d
JM
7654 /* get past catch_errs */
7655 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
7656 struct value *mark;
7657 int i;
fe3f5fa8
VP
7658 int not_found = 0;
7659 int *not_found_ptr = &not_found;
7660 struct symtabs_and_lines sals = {};
ed0616c6 7661 struct symtabs_and_lines expanded;
c906108c 7662 char *s;
b5de0fa7 7663 enum enable_state save_enable;
fe3f5fa8 7664 struct gdb_exception e;
f7545552 7665 struct cleanup *cleanups;
c906108c
SS
7666
7667 switch (b->type)
7668 {
7669 case bp_none:
8a3fe4f8 7670 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 7671 b->number);
c906108c
SS
7672 return 0;
7673 case bp_breakpoint:
7674 case bp_hardware_breakpoint:
1042e4c0 7675 case bp_tracepoint:
c906108c
SS
7676 if (b->addr_string == NULL)
7677 {
7678 /* Anything without a string can't be re-set. */
7679 delete_breakpoint (b);
7680 return 0;
7681 }
c906108c
SS
7682
7683 set_language (b->language);
7684 input_radix = b->input_radix;
7685 s = b->addr_string;
fe3f5fa8 7686 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 7687 {
fe3f5fa8
VP
7688 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7689 not_found_ptr);
7690 }
7691 if (e.reason < 0)
7692 {
7693 int not_found_and_ok = 0;
7694 /* For pending breakpoints, it's expected that parsing
7695 will fail until the right shared library is loaded.
7696 User has already told to create pending breakpoints and
7697 don't need extra messages. If breakpoint is in bp_shlib_disabled
7698 state, then user already saw the message about that breakpoint
7699 being disabled, and don't want to see more errors. */
0d381245
VP
7700 if (not_found
7701 && (b->condition_not_parsed
7702 || (b->loc && b->loc->shlib_disabled)
7703 || b->enable_state == bp_disabled))
fe3f5fa8
VP
7704 not_found_and_ok = 1;
7705
7706 if (!not_found_and_ok)
c906108c 7707 {
fe3f5fa8
VP
7708 /* We surely don't want to warn about the same breakpoint
7709 10 times. One solution, implemented here, is disable
7710 the breakpoint on error. Another solution would be to
7711 have separate 'warning emitted' flag. Since this
7712 happens only when a binary has changed, I don't know
7713 which approach is better. */
7714 b->enable_state = bp_disabled;
7715 throw_exception (e);
c906108c 7716 }
fe3f5fa8 7717 }
c906108c 7718
fe3f5fa8
VP
7719 if (not_found)
7720 break;
7721
7722 gdb_assert (sals.nelts == 1);
7723 resolve_sal_pc (&sals.sals[0]);
0d381245 7724 if (b->condition_not_parsed && s && s[0])
fe3f5fa8
VP
7725 {
7726 char *cond_string = 0;
7727 int thread = -1;
4a306c9a
JB
7728 int task = 0;
7729
fe3f5fa8 7730 find_condition_and_thread (s, sals.sals[0].pc,
4a306c9a 7731 &cond_string, &thread, &task);
fe3f5fa8
VP
7732 if (cond_string)
7733 b->cond_string = cond_string;
7734 b->thread = thread;
4a306c9a 7735 b->task = task;
0d381245 7736 b->condition_not_parsed = 0;
fe3f5fa8 7737 }
ed0616c6 7738 expanded = expand_line_sal_maybe (sals.sals[0]);
f7545552 7739 cleanups = make_cleanup (xfree, sals.sals);
ed0616c6 7740 update_breakpoint_locations (b, expanded);
f7545552 7741 do_cleanups (cleanups);
c906108c
SS
7742 break;
7743
7744 case bp_watchpoint:
7745 case bp_hardware_watchpoint:
7746 case bp_read_watchpoint:
7747 case bp_access_watchpoint:
0b3de036
VP
7748 /* Watchpoint can be either on expression using entirely global variables,
7749 or it can be on local variables.
7750
7751 Watchpoints of the first kind are never auto-deleted, and even persist
7752 across program restarts. Since they can use variables from shared
7753 libraries, we need to reparse expression as libraries are loaded
7754 and unloaded.
7755
7756 Watchpoints on local variables can also change meaning as result
7757 of solib event. For example, if a watchpoint uses both a local and
7758 a global variables in expression, it's a local watchpoint, but
7759 unloading of a shared library will make the expression invalid.
7760 This is not a very common use case, but we still re-evaluate
7761 expression, to avoid surprises to the user.
7762
7763 Note that for local watchpoints, we re-evaluate it only if
7764 watchpoints frame id is still valid. If it's not, it means
7765 the watchpoint is out of scope and will be deleted soon. In fact,
7766 I'm not sure we'll ever be called in this case.
7767
7768 If a local watchpoint's frame id is still valid, then
7769 b->exp_valid_block is likewise valid, and we can safely use it.
7770
7771 Don't do anything about disabled watchpoints, since they will
7772 be reevaluated again when enabled. */
a5606eee 7773 update_watchpoint (b, 1 /* reparse */);
c906108c 7774 break;
c5aa993b
JM
7775 /* We needn't really do anything to reset these, since the mask
7776 that requests them is unaffected by e.g., new libraries being
7777 loaded. */
ce78b96d 7778 case bp_catchpoint:
c906108c 7779 break;
c5aa993b 7780
c906108c 7781 default:
a3f17187 7782 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 7783 /* fall through */
0fd8e87f
UW
7784 /* Delete overlay event and longjmp master breakpoints; they will be
7785 reset later by breakpoint_re_set. */
1900040c 7786 case bp_overlay_event:
0fd8e87f 7787 case bp_longjmp_master:
c906108c
SS
7788 delete_breakpoint (b);
7789 break;
7790
c5aa993b
JM
7791 /* This breakpoint is special, it's set up when the inferior
7792 starts and we really don't want to touch it. */
c906108c
SS
7793 case bp_shlib_event:
7794
c4093a6a
JM
7795 /* Like bp_shlib_event, this breakpoint type is special.
7796 Once it is set up, we do not want to touch it. */
7797 case bp_thread_event:
7798
c5aa993b
JM
7799 /* Keep temporary breakpoints, which can be encountered when we step
7800 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7801 Otherwise these should have been blown away via the cleanup chain
7802 or by breakpoint_init_inferior when we rerun the executable. */
c906108c
SS
7803 case bp_until:
7804 case bp_finish:
7805 case bp_watchpoint_scope:
7806 case bp_call_dummy:
7807 case bp_step_resume:
611c83ae
PA
7808 case bp_longjmp:
7809 case bp_longjmp_resume:
c906108c
SS
7810 break;
7811 }
7812
7813 return 0;
7814}
7815
69de3c6a 7816/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 7817void
69de3c6a 7818breakpoint_re_set (void)
c906108c
SS
7819{
7820 struct breakpoint *b, *temp;
7821 enum language save_language;
7822 int save_input_radix;
c5aa993b 7823
c906108c
SS
7824 save_language = current_language->la_language;
7825 save_input_radix = input_radix;
7826 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7827 {
53a5351d 7828 /* Format possible error msg */
fe3f5fa8 7829 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
7830 b->number);
7831 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 7832 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 7833 do_cleanups (cleanups);
c5aa993b 7834 }
c906108c
SS
7835 set_language (save_language);
7836 input_radix = save_input_radix;
e62c965a 7837
69de3c6a 7838 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
7839 create_longjmp_master_breakpoint ("longjmp");
7840 create_longjmp_master_breakpoint ("_longjmp");
7841 create_longjmp_master_breakpoint ("siglongjmp");
7842 create_longjmp_master_breakpoint ("_siglongjmp");
c906108c
SS
7843}
7844\f
c906108c
SS
7845/* Reset the thread number of this breakpoint:
7846
7847 - If the breakpoint is for all threads, leave it as-is.
39f77062 7848 - Else, reset it to the current thread for inferior_ptid. */
c906108c 7849void
fba45db2 7850breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
7851{
7852 if (b->thread != -1)
7853 {
39f77062
KB
7854 if (in_thread_list (inferior_ptid))
7855 b->thread = pid_to_thread_id (inferior_ptid);
c906108c
SS
7856 }
7857}
7858
03ac34d5
MS
7859/* Set ignore-count of breakpoint number BPTNUM to COUNT.
7860 If from_tty is nonzero, it prints a message to that effect,
7861 which ends with a period (no newline). */
7862
c906108c 7863void
fba45db2 7864set_ignore_count (int bptnum, int count, int from_tty)
c906108c 7865{
52f0bd74 7866 struct breakpoint *b;
c906108c
SS
7867
7868 if (count < 0)
7869 count = 0;
7870
7871 ALL_BREAKPOINTS (b)
7872 if (b->number == bptnum)
c5aa993b
JM
7873 {
7874 b->ignore_count = count;
221ea385
KS
7875 if (from_tty)
7876 {
7877 if (count == 0)
a3f17187 7878 printf_filtered (_("Will stop next time breakpoint %d is reached."),
221ea385
KS
7879 bptnum);
7880 else if (count == 1)
a3f17187 7881 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
7882 bptnum);
7883 else
a3f17187 7884 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
221ea385
KS
7885 count, bptnum);
7886 }
c5aa993b 7887 breakpoints_changed ();
383f836e 7888 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
7889 return;
7890 }
c906108c 7891
8a3fe4f8 7892 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
7893}
7894
b2175913
MS
7895void
7896make_breakpoint_silent (struct breakpoint *b)
7897{
7898 /* Silence the breakpoint. */
7899 b->silent = 1;
7900}
7901
c906108c
SS
7902/* Command to set ignore-count of breakpoint N to COUNT. */
7903
7904static void
fba45db2 7905ignore_command (char *args, int from_tty)
c906108c
SS
7906{
7907 char *p = args;
52f0bd74 7908 int num;
c906108c
SS
7909
7910 if (p == 0)
e2e0b3e5 7911 error_no_arg (_("a breakpoint number"));
c5aa993b 7912
c906108c 7913 num = get_number (&p);
5c44784c 7914 if (num == 0)
8a3fe4f8 7915 error (_("bad breakpoint number: '%s'"), args);
c906108c 7916 if (*p == 0)
8a3fe4f8 7917 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
7918
7919 set_ignore_count (num,
7920 longest_to_int (value_as_long (parse_and_eval (p))),
7921 from_tty);
221ea385
KS
7922 if (from_tty)
7923 printf_filtered ("\n");
c906108c
SS
7924}
7925\f
7926/* Call FUNCTION on each of the breakpoints
7927 whose numbers are given in ARGS. */
7928
7929static void
831662b3 7930map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
c906108c 7931{
52f0bd74 7932 char *p = args;
c906108c 7933 char *p1;
52f0bd74
AC
7934 int num;
7935 struct breakpoint *b, *tmp;
11cf8741 7936 int match;
c906108c
SS
7937
7938 if (p == 0)
e2e0b3e5 7939 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
7940
7941 while (*p)
7942 {
11cf8741 7943 match = 0;
c906108c 7944 p1 = p;
c5aa993b 7945
5c44784c
JM
7946 num = get_number_or_range (&p1);
7947 if (num == 0)
c5aa993b 7948 {
8a3fe4f8 7949 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
7950 }
7951 else
7952 {
7953 ALL_BREAKPOINTS_SAFE (b, tmp)
7954 if (b->number == num)
7955 {
7956 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 7957 match = 1;
5c44784c
JM
7958 function (b);
7959 if (related_breakpoint)
7960 function (related_breakpoint);
11cf8741 7961 break;
5c44784c 7962 }
11cf8741 7963 if (match == 0)
a3f17187 7964 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 7965 }
c906108c
SS
7966 p = p1;
7967 }
7968}
7969
0d381245
VP
7970static struct bp_location *
7971find_location_by_number (char *number)
7972{
7973 char *dot = strchr (number, '.');
7974 char *p1;
7975 int bp_num;
7976 int loc_num;
7977 struct breakpoint *b;
7978 struct bp_location *loc;
7979
7980 *dot = '\0';
7981
7982 p1 = number;
7983 bp_num = get_number_or_range (&p1);
7984 if (bp_num == 0)
7985 error (_("Bad breakpoint number '%s'"), number);
7986
7987 ALL_BREAKPOINTS (b)
7988 if (b->number == bp_num)
7989 {
7990 break;
7991 }
7992
7993 if (!b || b->number != bp_num)
7994 error (_("Bad breakpoint number '%s'"), number);
7995
7996 p1 = dot+1;
7997 loc_num = get_number_or_range (&p1);
7998 if (loc_num == 0)
7999 error (_("Bad breakpoint location number '%s'"), number);
8000
8001 --loc_num;
8002 loc = b->loc;
8003 for (;loc_num && loc; --loc_num, loc = loc->next)
8004 ;
8005 if (!loc)
8006 error (_("Bad breakpoint location number '%s'"), dot+1);
8007
8008 return loc;
8009}
8010
8011
1900040c
MS
8012/* Set ignore-count of breakpoint number BPTNUM to COUNT.
8013 If from_tty is nonzero, it prints a message to that effect,
8014 which ends with a period (no newline). */
8015
c906108c 8016void
fba45db2 8017disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
8018{
8019 /* Never disable a watchpoint scope breakpoint; we want to
8020 hit them when we leave scope so we can delete both the
8021 watchpoint and its scope breakpoint at that time. */
8022 if (bpt->type == bp_watchpoint_scope)
8023 return;
8024
c2c6d25f 8025 /* You can't disable permanent breakpoints. */
b5de0fa7 8026 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
8027 return;
8028
b5de0fa7 8029 bpt->enable_state = bp_disabled;
c906108c 8030
b60e7edf 8031 update_global_location_list (0);
c906108c 8032
383f836e 8033 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
8034}
8035
c906108c 8036static void
fba45db2 8037disable_command (char *args, int from_tty)
c906108c 8038{
52f0bd74 8039 struct breakpoint *bpt;
c906108c
SS
8040 if (args == 0)
8041 ALL_BREAKPOINTS (bpt)
8042 switch (bpt->type)
c5aa993b
JM
8043 {
8044 case bp_none:
8a3fe4f8 8045 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 8046 bpt->number);
c5aa993b
JM
8047 continue;
8048 case bp_breakpoint:
1042e4c0 8049 case bp_tracepoint:
ce78b96d 8050 case bp_catchpoint:
c5aa993b
JM
8051 case bp_hardware_breakpoint:
8052 case bp_watchpoint:
8053 case bp_hardware_watchpoint:
8054 case bp_read_watchpoint:
8055 case bp_access_watchpoint:
8056 disable_breakpoint (bpt);
8057 default:
8058 continue;
8059 }
0d381245
VP
8060 else if (strchr (args, '.'))
8061 {
8062 struct bp_location *loc = find_location_by_number (args);
8063 if (loc)
8064 loc->enabled = 0;
b60e7edf 8065 update_global_location_list (0);
0d381245 8066 }
c906108c
SS
8067 else
8068 map_breakpoint_numbers (args, disable_breakpoint);
8069}
8070
8071static void
fba45db2 8072do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 8073{
c906108c
SS
8074 int target_resources_ok, other_type_used;
8075 struct value *mark;
8076
8077 if (bpt->type == bp_hardware_breakpoint)
8078 {
8079 int i;
c5aa993b 8080 i = hw_breakpoint_used_count ();
53a5351d 8081 target_resources_ok =
d92524f1 8082 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 8083 i + 1, 0);
c906108c 8084 if (target_resources_ok == 0)
8a3fe4f8 8085 error (_("No hardware breakpoint support in the target."));
c906108c 8086 else if (target_resources_ok < 0)
8a3fe4f8 8087 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
8088 }
8089
059fb39f
PM
8090 if (bpt->type == bp_watchpoint
8091 || bpt->type == bp_hardware_watchpoint
8092 || bpt->type == bp_read_watchpoint
8093 || bpt->type == bp_access_watchpoint)
c906108c 8094 {
dde02812
ES
8095 struct gdb_exception e;
8096
8097 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 8098 {
dde02812 8099 update_watchpoint (bpt, 1 /* reparse */);
c906108c 8100 }
dde02812 8101 if (e.reason < 0)
c5aa993b 8102 {
dde02812
ES
8103 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
8104 bpt->number);
8105 return;
c5aa993b 8106 }
c906108c 8107 }
0101ce28 8108
b4c291bb
KH
8109 if (bpt->enable_state != bp_permanent)
8110 bpt->enable_state = bp_enabled;
8111 bpt->disposition = disposition;
b60e7edf 8112 update_global_location_list (1);
b4c291bb
KH
8113 breakpoints_changed ();
8114
383f836e 8115 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
8116}
8117
fe3f5fa8 8118
c906108c 8119void
fba45db2 8120enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
8121{
8122 do_enable_breakpoint (bpt, bpt->disposition);
8123}
8124
8125/* The enable command enables the specified breakpoints (or all defined
8126 breakpoints) so they once again become (or continue to be) effective
1272ad14 8127 in stopping the inferior. */
c906108c 8128
c906108c 8129static void
fba45db2 8130enable_command (char *args, int from_tty)
c906108c 8131{
52f0bd74 8132 struct breakpoint *bpt;
c906108c
SS
8133 if (args == 0)
8134 ALL_BREAKPOINTS (bpt)
8135 switch (bpt->type)
c5aa993b
JM
8136 {
8137 case bp_none:
8a3fe4f8 8138 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 8139 bpt->number);
c5aa993b
JM
8140 continue;
8141 case bp_breakpoint:
1042e4c0 8142 case bp_tracepoint:
ce78b96d 8143 case bp_catchpoint:
c5aa993b
JM
8144 case bp_hardware_breakpoint:
8145 case bp_watchpoint:
8146 case bp_hardware_watchpoint:
8147 case bp_read_watchpoint:
8148 case bp_access_watchpoint:
8149 enable_breakpoint (bpt);
8150 default:
8151 continue;
8152 }
0d381245
VP
8153 else if (strchr (args, '.'))
8154 {
8155 struct bp_location *loc = find_location_by_number (args);
8156 if (loc)
8157 loc->enabled = 1;
b60e7edf 8158 update_global_location_list (1);
0d381245 8159 }
c906108c
SS
8160 else
8161 map_breakpoint_numbers (args, enable_breakpoint);
8162}
8163
8164static void
fba45db2 8165enable_once_breakpoint (struct breakpoint *bpt)
c906108c 8166{
b5de0fa7 8167 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
8168}
8169
c906108c 8170static void
fba45db2 8171enable_once_command (char *args, int from_tty)
c906108c
SS
8172{
8173 map_breakpoint_numbers (args, enable_once_breakpoint);
8174}
8175
8176static void
fba45db2 8177enable_delete_breakpoint (struct breakpoint *bpt)
c906108c 8178{
b5de0fa7 8179 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
8180}
8181
c906108c 8182static void
fba45db2 8183enable_delete_command (char *args, int from_tty)
c906108c
SS
8184{
8185 map_breakpoint_numbers (args, enable_delete_breakpoint);
8186}
8187\f
fa8d40ab
JJ
8188static void
8189set_breakpoint_cmd (char *args, int from_tty)
8190{
8191}
8192
8193static void
8194show_breakpoint_cmd (char *args, int from_tty)
8195{
8196}
8197
c906108c
SS
8198/* Use default_breakpoint_'s, or nothing if they aren't valid. */
8199
8200struct symtabs_and_lines
fba45db2 8201decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
8202{
8203 struct symtabs_and_lines sals;
8204 if (string == 0)
8a3fe4f8 8205 error (_("Empty line specification."));
c906108c
SS
8206 if (default_breakpoint_valid)
8207 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
8208 default_breakpoint_symtab,
8209 default_breakpoint_line,
68219205 8210 (char ***) NULL, NULL);
c906108c
SS
8211 else
8212 sals = decode_line_1 (&string, funfirstline,
68219205 8213 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 8214 if (*string)
8a3fe4f8 8215 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
8216 return sals;
8217}
8181d85f
DJ
8218
8219/* Create and insert a raw software breakpoint at PC. Return an
8220 identifier, which should be used to remove the breakpoint later.
8221 In general, places which call this should be using something on the
8222 breakpoint chain instead; this function should be eliminated
8223 someday. */
8224
8225void *
8226deprecated_insert_raw_breakpoint (CORE_ADDR pc)
8227{
8228 struct bp_target_info *bp_tgt;
8229
8230 bp_tgt = xmalloc (sizeof (struct bp_target_info));
8231 memset (bp_tgt, 0, sizeof (struct bp_target_info));
8232
8233 bp_tgt->placed_address = pc;
8234 if (target_insert_breakpoint (bp_tgt) != 0)
8235 {
8236 /* Could not insert the breakpoint. */
8237 xfree (bp_tgt);
8238 return NULL;
8239 }
8240
8241 return bp_tgt;
8242}
8243
8244/* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
8245
8246int
8247deprecated_remove_raw_breakpoint (void *bp)
8248{
8249 struct bp_target_info *bp_tgt = bp;
8250 int ret;
8251
8252 ret = target_remove_breakpoint (bp_tgt);
8253 xfree (bp_tgt);
8254
8255 return ret;
8256}
8257
8258/* One (or perhaps two) breakpoints used for software single stepping. */
8259
8260static void *single_step_breakpoints[2];
8261
8262/* Create and insert a breakpoint for software single step. */
8263
8264void
8265insert_single_step_breakpoint (CORE_ADDR next_pc)
8266{
8267 void **bpt_p;
8268
8269 if (single_step_breakpoints[0] == NULL)
8270 bpt_p = &single_step_breakpoints[0];
8271 else
8272 {
8273 gdb_assert (single_step_breakpoints[1] == NULL);
8274 bpt_p = &single_step_breakpoints[1];
8275 }
8276
8277 /* NOTE drow/2006-04-11: A future improvement to this function would be
8278 to only create the breakpoints once, and actually put them on the
8279 breakpoint chain. That would let us use set_raw_breakpoint. We could
8280 adjust the addresses each time they were needed. Doing this requires
8281 corresponding changes elsewhere where single step breakpoints are
8282 handled, however. So, for now, we use this. */
8283
8284 *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
8285 if (*bpt_p == NULL)
1893a4c0 8286 error (_("Could not insert single-step breakpoint at 0x%s"),
8181d85f
DJ
8287 paddr_nz (next_pc));
8288}
8289
8290/* Remove and delete any breakpoints used for software single step. */
8291
8292void
8293remove_single_step_breakpoints (void)
8294{
8295 gdb_assert (single_step_breakpoints[0] != NULL);
8296
8297 /* See insert_single_step_breakpoint for more about this deprecated
8298 call. */
8299 deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
8300 single_step_breakpoints[0] = NULL;
8301
8302 if (single_step_breakpoints[1] != NULL)
8303 {
8304 deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
8305 single_step_breakpoints[1] = NULL;
8306 }
8307}
8308
1aafd4da
UW
8309/* Check whether a software single-step breakpoint is inserted at PC. */
8310
8311static int
8312single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
8313{
8314 int i;
8315
8316 for (i = 0; i < 2; i++)
8317 {
8318 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
8319 if (bp_tgt && bp_tgt->placed_address == pc)
8320 return 1;
8321 }
8322
8323 return 0;
8324}
8325
1042e4c0
SS
8326/* Tracepoint-specific operations. */
8327
8328/* Set tracepoint count to NUM. */
8329static void
8330set_tracepoint_count (int num)
8331{
8332 tracepoint_count = num;
4fa62494 8333 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
8334}
8335
8336void
8337trace_command (char *arg, int from_tty)
8338{
8339 break_command_really (arg,
8340 NULL, 0, 1 /* parse arg */,
8341 0 /* tempflag */, 0 /* hardwareflag */,
8342 1 /* traceflag */,
8343 0 /* Ignore count */,
8344 pending_break_support,
8345 NULL,
8346 from_tty,
8347 1 /* enabled */);
8348 set_tracepoint_count (breakpoint_count);
8349}
8350
8351/* Print information on tracepoint number TPNUM_EXP, or all if
8352 omitted. */
8353
8354static void
8355tracepoints_info (char *tpnum_exp, int from_tty)
8356{
8357 struct breakpoint *b;
8358 int tps_to_list = 0;
8359
8360 /* In the no-arguments case, say "No tracepoints" if none found. */
8361 if (tpnum_exp == 0)
8362 {
8363 ALL_TRACEPOINTS (b)
8364 {
8365 if (b->number >= 0)
8366 {
8367 tps_to_list = 1;
8368 break;
8369 }
8370 }
8371 if (!tps_to_list)
8372 {
8373 ui_out_message (uiout, 0, "No tracepoints.\n");
8374 return;
8375 }
8376 }
8377
8378 /* Otherwise be the same as "info break". */
8379 breakpoints_info (tpnum_exp, from_tty);
8380}
8381
8382/* The 'enable trace' command enables tracepoints.
8383 Not supported by all targets. */
8384static void
8385enable_trace_command (char *args, int from_tty)
8386{
8387 enable_command (args, from_tty);
8388}
8389
8390/* The 'disable trace' command disables tracepoints.
8391 Not supported by all targets. */
8392static void
8393disable_trace_command (char *args, int from_tty)
8394{
8395 disable_command (args, from_tty);
8396}
8397
8398/* Remove a tracepoint (or all if no argument) */
8399static void
8400delete_trace_command (char *arg, int from_tty)
8401{
8402 struct breakpoint *b, *temp;
8403
8404 dont_repeat ();
8405
8406 if (arg == 0)
8407 {
8408 int breaks_to_delete = 0;
8409
8410 /* Delete all breakpoints if no argument.
8411 Do not delete internal or call-dummy breakpoints, these
8412 have to be deleted with an explicit breakpoint number argument. */
8413 ALL_TRACEPOINTS (b)
8414 {
8415 if (b->number >= 0)
8416 {
8417 breaks_to_delete = 1;
8418 break;
8419 }
8420 }
8421
8422 /* Ask user only if there are some breakpoints to delete. */
8423 if (!from_tty
8424 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
8425 {
8426 ALL_BREAKPOINTS_SAFE (b, temp)
8427 {
059fb39f
PM
8428 if (b->type == bp_tracepoint
8429 && b->number >= 0)
1042e4c0
SS
8430 delete_breakpoint (b);
8431 }
8432 }
8433 }
8434 else
8435 map_breakpoint_numbers (arg, delete_breakpoint);
8436}
8437
8438/* Set passcount for tracepoint.
8439
8440 First command argument is passcount, second is tracepoint number.
8441 If tracepoint number omitted, apply to most recently defined.
8442 Also accepts special argument "all". */
8443
8444static void
8445trace_pass_command (char *args, int from_tty)
8446{
8447 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
8448 unsigned int count;
8449 int all = 0;
8450
8451 if (args == 0 || *args == 0)
8452 error (_("passcount command requires an argument (count + optional TP num)"));
8453
8454 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
8455
8456 while (*args && isspace ((int) *args))
8457 args++;
8458
8459 if (*args && strncasecmp (args, "all", 3) == 0)
8460 {
8461 args += 3; /* Skip special argument "all". */
8462 all = 1;
8463 if (*args)
8464 error (_("Junk at end of arguments."));
8465 }
8466 else
8467 t1 = get_tracepoint_by_number (&args, 1, 1);
8468
8469 do
8470 {
8471 if (t1)
8472 {
8473 ALL_TRACEPOINTS (t2)
8474 if (t1 == (struct breakpoint *) -1 || t1 == t2)
8475 {
8476 t2->pass_count = count;
8477 observer_notify_tracepoint_modified (t2->number);
8478 if (from_tty)
8479 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
8480 t2->number, count);
8481 }
8482 if (! all && *args)
8483 t1 = get_tracepoint_by_number (&args, 1, 0);
8484 }
8485 }
8486 while (*args);
8487}
8488
8489struct breakpoint *
8490get_tracepoint (int num)
8491{
8492 struct breakpoint *t;
8493
8494 ALL_TRACEPOINTS (t)
8495 if (t->number == num)
8496 return t;
8497
8498 return NULL;
8499}
8500
8501/* Utility: parse a tracepoint number and look it up in the list.
8502 If MULTI_P is true, there might be a range of tracepoints in ARG.
8503 if OPTIONAL_P is true, then if the argument is missing, the most
8504 recent tracepoint (tracepoint_count) is returned. */
8505struct breakpoint *
8506get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
8507{
8508 extern int tracepoint_count;
8509 struct breakpoint *t;
8510 int tpnum;
8511 char *instring = arg == NULL ? NULL : *arg;
8512
8513 if (arg == NULL || *arg == NULL || ! **arg)
8514 {
8515 if (optional_p)
8516 tpnum = tracepoint_count;
8517 else
8518 error_no_arg (_("tracepoint number"));
8519 }
8520 else
8521 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
8522
8523 if (tpnum <= 0)
8524 {
8525 if (instring && *instring)
8526 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
8527 instring);
8528 else
8529 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
8530 return NULL;
8531 }
8532
8533 ALL_TRACEPOINTS (t)
8534 if (t->number == tpnum)
8535 {
8536 return t;
8537 }
8538
8539 /* FIXME: if we are in the middle of a range we don't want to give
8540 a message. The current interface to get_number_or_range doesn't
8541 allow us to discover this. */
8542 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
8543 return NULL;
8544}
8545
8546/* save-tracepoints command */
8547static void
8548tracepoint_save_command (char *args, int from_tty)
8549{
8550 struct breakpoint *tp;
8551 int any_tp = 0;
8552 struct action_line *line;
8553 FILE *fp;
8554 char *i1 = " ", *i2 = " ";
8555 char *indent, *actionline, *pathname;
8556 char tmp[40];
8557 struct cleanup *cleanup;
8558
8559 if (args == 0 || *args == 0)
8560 error (_("Argument required (file name in which to save tracepoints)"));
8561
8562 /* See if we have anything to save. */
8563 ALL_TRACEPOINTS (tp)
8564 {
8565 any_tp = 1;
8566 break;
8567 }
8568 if (!any_tp)
8569 {
8570 warning (_("save-tracepoints: no tracepoints to save."));
8571 return;
8572 }
8573
8574 pathname = tilde_expand (args);
8575 cleanup = make_cleanup (xfree, pathname);
059fb39f
PM
8576 fp = fopen (pathname, "w");
8577 if (!fp)
1042e4c0
SS
8578 error (_("Unable to open file '%s' for saving tracepoints (%s)"),
8579 args, safe_strerror (errno));
8580 make_cleanup_fclose (fp);
8581
8582 ALL_TRACEPOINTS (tp)
8583 {
8584 if (tp->addr_string)
8585 fprintf (fp, "trace %s\n", tp->addr_string);
8586 else
8587 {
8588 sprintf_vma (tmp, tp->loc->address);
8589 fprintf (fp, "trace *0x%s\n", tmp);
8590 }
8591
8592 if (tp->pass_count)
8593 fprintf (fp, " passcount %d\n", tp->pass_count);
8594
8595 if (tp->actions)
8596 {
8597 fprintf (fp, " actions\n");
8598 indent = i1;
8599 for (line = tp->actions; line; line = line->next)
8600 {
8601 struct cmd_list_element *cmd;
8602
8603 QUIT; /* allow user to bail out with ^C */
8604 actionline = line->action;
8605 while (isspace ((int) *actionline))
8606 actionline++;
8607
8608 fprintf (fp, "%s%s\n", indent, actionline);
8609 if (*actionline != '#') /* skip for comment lines */
8610 {
8611 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
8612 if (cmd == 0)
8613 error (_("Bad action list item: %s"), actionline);
8614 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
8615 indent = i2;
8616 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
8617 indent = i1;
8618 }
8619 }
8620 }
8621 }
8622 do_cleanups (cleanup);
8623 if (from_tty)
8624 printf_filtered (_("Tracepoints saved to file '%s'.\n"), args);
8625 return;
8626}
8627
8628/* Create a vector of all tracepoints. */
8629
8630VEC(breakpoint_p) *
8631all_tracepoints ()
8632{
8633 VEC(breakpoint_p) *tp_vec = 0;
8634 struct breakpoint *tp;
8635
8636 ALL_TRACEPOINTS (tp)
8637 {
8638 VEC_safe_push (breakpoint_p, tp_vec, tp);
8639 }
8640
8641 return tp_vec;
8642}
8643
c906108c 8644\f
31e2b00f
AS
8645/* This help string is used for the break, hbreak, tbreak and thbreak commands.
8646 It is defined as a macro to prevent duplication.
8647 COMMAND should be a string constant containing the name of the command. */
8648#define BREAK_ARGS_HELP(command) \
8649command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
8650LOCATION may be a line number, function name, or \"*\" and an address.\n\
8651If a line number is specified, break at start of code for that line.\n\
8652If a function is specified, break at start of code for that function.\n\
8653If an address is specified, break at that exact address.\n\
8654With no LOCATION, uses current execution address of selected stack frame.\n\
8655This is useful for breaking on return to a stack frame.\n\
8656\n\
8657THREADNUM is the number from \"info threads\".\n\
8658CONDITION is a boolean expression.\n\
8659\n\
8660Multiple breakpoints at one place are permitted, and useful if conditional.\n\
8661\n\
8662Do \"help breakpoints\" for info on other commands dealing with breakpoints."
8663
44feb3ce
TT
8664/* List of subcommands for "catch". */
8665static struct cmd_list_element *catch_cmdlist;
8666
8667/* List of subcommands for "tcatch". */
8668static struct cmd_list_element *tcatch_cmdlist;
8669
8670/* Like add_cmd, but add the command to both the "catch" and "tcatch"
8671 lists, and pass some additional user data to the command function. */
8672static void
8673add_catch_command (char *name, char *docstring,
8674 void (*sfunc) (char *args, int from_tty,
8675 struct cmd_list_element *command),
8676 void *user_data_catch,
8677 void *user_data_tcatch)
8678{
8679 struct cmd_list_element *command;
8680
8681 command = add_cmd (name, class_breakpoint, NULL, docstring,
8682 &catch_cmdlist);
8683 set_cmd_sfunc (command, sfunc);
8684 set_cmd_context (command, user_data_catch);
8685
8686 command = add_cmd (name, class_breakpoint, NULL, docstring,
8687 &tcatch_cmdlist);
8688 set_cmd_sfunc (command, sfunc);
8689 set_cmd_context (command, user_data_tcatch);
8690}
8691
c906108c 8692void
fba45db2 8693_initialize_breakpoint (void)
c906108c 8694{
fa8d40ab
JJ
8695 static struct cmd_list_element *breakpoint_set_cmdlist;
8696 static struct cmd_list_element *breakpoint_show_cmdlist;
c906108c
SS
8697 struct cmd_list_element *c;
8698
84acb35a 8699 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
84acb35a 8700
c906108c
SS
8701 breakpoint_chain = 0;
8702 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
8703 before a breakpoint is set. */
8704 breakpoint_count = 0;
8705
1042e4c0
SS
8706 tracepoint_count = 0;
8707
1bedd215
AC
8708 add_com ("ignore", class_breakpoint, ignore_command, _("\
8709Set ignore-count of breakpoint number N to COUNT.\n\
8710Usage is `ignore N COUNT'."));
c906108c 8711 if (xdb_commands)
c5aa993b 8712 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 8713
1bedd215
AC
8714 add_com ("commands", class_breakpoint, commands_command, _("\
8715Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
8716Give breakpoint number as argument after \"commands\".\n\
8717With no argument, the targeted breakpoint is the last one set.\n\
8718The commands themselves follow starting on the next line.\n\
8719Type a line containing \"end\" to indicate the end of them.\n\
8720Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 8721then no output is printed when it is hit, except what the commands print."));
c906108c 8722
1bedd215
AC
8723 add_com ("condition", class_breakpoint, condition_command, _("\
8724Specify breakpoint number N to break only if COND is true.\n\
c906108c 8725Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 8726expression to be evaluated whenever breakpoint N is reached."));
c906108c 8727
1bedd215 8728 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 8729Set a temporary breakpoint.\n\
c906108c
SS
8730Like \"break\" except the breakpoint is only temporary,\n\
8731so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
8732by using \"enable delete\" on the breakpoint number.\n\
8733\n"
8734BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 8735 set_cmd_completer (c, location_completer);
c94fdfd0 8736
1bedd215 8737 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
31e2b00f 8738Set a hardware assisted breakpoint.\n\
c906108c 8739Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
8740some target hardware may not have this support.\n\
8741\n"
8742BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 8743 set_cmd_completer (c, location_completer);
c906108c 8744
1bedd215 8745 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 8746Set a temporary hardware assisted breakpoint.\n\
c906108c 8747Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
8748so it will be deleted when hit.\n\
8749\n"
8750BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 8751 set_cmd_completer (c, location_completer);
c906108c 8752
1bedd215
AC
8753 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
8754Enable some breakpoints.\n\
c906108c
SS
8755Give breakpoint numbers (separated by spaces) as arguments.\n\
8756With no subcommand, breakpoints are enabled until you command otherwise.\n\
8757This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8758With a subcommand you can enable temporarily."),
c906108c
SS
8759 &enablelist, "enable ", 1, &cmdlist);
8760 if (xdb_commands)
1bedd215
AC
8761 add_com ("ab", class_breakpoint, enable_command, _("\
8762Enable some breakpoints.\n\
c906108c
SS
8763Give breakpoint numbers (separated by spaces) as arguments.\n\
8764With no subcommand, breakpoints are enabled until you command otherwise.\n\
8765This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8766With a subcommand you can enable temporarily."));
c906108c
SS
8767
8768 add_com_alias ("en", "enable", class_breakpoint, 1);
8769
1bedd215
AC
8770 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8771Enable some breakpoints.\n\
c906108c
SS
8772Give breakpoint numbers (separated by spaces) as arguments.\n\
8773This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8774May be abbreviated to simply \"enable\".\n"),
c5aa993b 8775 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 8776
1a966eab
AC
8777 add_cmd ("once", no_class, enable_once_command, _("\
8778Enable breakpoints for one hit. Give breakpoint numbers.\n\
8779If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8780 &enablebreaklist);
8781
1a966eab
AC
8782 add_cmd ("delete", no_class, enable_delete_command, _("\
8783Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8784If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8785 &enablebreaklist);
8786
1a966eab
AC
8787 add_cmd ("delete", no_class, enable_delete_command, _("\
8788Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8789If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8790 &enablelist);
8791
1a966eab
AC
8792 add_cmd ("once", no_class, enable_once_command, _("\
8793Enable breakpoints for one hit. Give breakpoint numbers.\n\
8794If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8795 &enablelist);
8796
1bedd215
AC
8797 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8798Disable some breakpoints.\n\
c906108c
SS
8799Arguments are breakpoint numbers with spaces in between.\n\
8800To disable all breakpoints, give no argument.\n\
1bedd215 8801A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
8802 &disablelist, "disable ", 1, &cmdlist);
8803 add_com_alias ("dis", "disable", class_breakpoint, 1);
8804 add_com_alias ("disa", "disable", class_breakpoint, 1);
8805 if (xdb_commands)
1bedd215
AC
8806 add_com ("sb", class_breakpoint, disable_command, _("\
8807Disable some breakpoints.\n\
c906108c
SS
8808Arguments are breakpoint numbers with spaces in between.\n\
8809To disable all breakpoints, give no argument.\n\
1bedd215 8810A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 8811
1a966eab
AC
8812 add_cmd ("breakpoints", class_alias, disable_command, _("\
8813Disable some breakpoints.\n\
c906108c
SS
8814Arguments are breakpoint numbers with spaces in between.\n\
8815To disable all breakpoints, give no argument.\n\
8816A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 8817This command may be abbreviated \"disable\"."),
c906108c
SS
8818 &disablelist);
8819
1bedd215
AC
8820 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8821Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8822Arguments are breakpoint numbers with spaces in between.\n\
8823To delete all breakpoints, give no argument.\n\
8824\n\
8825Also a prefix command for deletion of other GDB objects.\n\
1bedd215 8826The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
8827 &deletelist, "delete ", 1, &cmdlist);
8828 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 8829 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 8830 if (xdb_commands)
1bedd215
AC
8831 add_com ("db", class_breakpoint, delete_command, _("\
8832Delete some breakpoints.\n\
c906108c 8833Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 8834To delete all breakpoints, give no argument.\n"));
c906108c 8835
1a966eab
AC
8836 add_cmd ("breakpoints", class_alias, delete_command, _("\
8837Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8838Arguments are breakpoint numbers with spaces in between.\n\
8839To delete all breakpoints, give no argument.\n\
1a966eab 8840This command may be abbreviated \"delete\"."),
c906108c
SS
8841 &deletelist);
8842
1bedd215
AC
8843 add_com ("clear", class_breakpoint, clear_command, _("\
8844Clear breakpoint at specified line or function.\n\
c906108c
SS
8845Argument may be line number, function name, or \"*\" and an address.\n\
8846If line number is specified, all breakpoints in that line are cleared.\n\
8847If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
8848If an address is specified, breakpoints at that address are cleared.\n\
8849\n\
8850With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
8851is executing in.\n\
8852\n\
1bedd215 8853See also the \"delete\" command which clears breakpoints by number."));
c906108c 8854
1bedd215 8855 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
8856Set breakpoint at specified line or function.\n"
8857BREAK_ARGS_HELP ("break")));
5ba2abeb 8858 set_cmd_completer (c, location_completer);
c94fdfd0 8859
c906108c
SS
8860 add_com_alias ("b", "break", class_run, 1);
8861 add_com_alias ("br", "break", class_run, 1);
8862 add_com_alias ("bre", "break", class_run, 1);
8863 add_com_alias ("brea", "break", class_run, 1);
8864
7681d515
PM
8865 if (xdb_commands)
8866 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
8867
8868 if (dbx_commands)
8869 {
1bedd215
AC
8870 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8871Break in function/address or break at a line in the current file."),
c5aa993b
JM
8872 &stoplist, "stop ", 1, &cmdlist);
8873 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 8874 _("Break in function or address."), &stoplist);
c5aa993b 8875 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 8876 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
8877 add_com ("status", class_info, breakpoints_info, _("\
8878Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8879The \"Type\" column indicates one of:\n\
8880\tbreakpoint - normal breakpoint\n\
8881\twatchpoint - watchpoint\n\
8882The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8883the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8884breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8885address and file/line number respectively.\n\
8886\n\
8887Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8888are set to the address of the last breakpoint listed unless the command\n\
8889is prefixed with \"server \".\n\n\
c906108c 8890Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8891breakpoint set."));
c906108c
SS
8892 }
8893
1bedd215
AC
8894 add_info ("breakpoints", breakpoints_info, _("\
8895Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8896The \"Type\" column indicates one of:\n\
8897\tbreakpoint - normal breakpoint\n\
8898\twatchpoint - watchpoint\n\
8899The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8900the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8901breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8902address and file/line number respectively.\n\
8903\n\
8904Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8905are set to the address of the last breakpoint listed unless the command\n\
8906is prefixed with \"server \".\n\n\
c906108c 8907Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8908breakpoint set."));
c906108c
SS
8909
8910 if (xdb_commands)
1bedd215
AC
8911 add_com ("lb", class_breakpoint, breakpoints_info, _("\
8912Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8913The \"Type\" column indicates one of:\n\
8914\tbreakpoint - normal breakpoint\n\
8915\twatchpoint - watchpoint\n\
8916The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8917the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8918breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8919address and file/line number respectively.\n\
8920\n\
8921Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8922are set to the address of the last breakpoint listed unless the command\n\
8923is prefixed with \"server \".\n\n\
c906108c 8924Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8925breakpoint set."));
c906108c 8926
1a966eab
AC
8927 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
8928Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8929The \"Type\" column indicates one of:\n\
8930\tbreakpoint - normal breakpoint\n\
8931\twatchpoint - watchpoint\n\
8932\tlongjmp - internal breakpoint used to step through longjmp()\n\
8933\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8934\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
8935\tfinish - internal breakpoint used by the \"finish\" command\n\
8936The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
8937the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8938breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
8939address and file/line number respectively.\n\
8940\n\
8941Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8942are set to the address of the last breakpoint listed unless the command\n\
8943is prefixed with \"server \".\n\n\
c906108c 8944Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 8945breakpoint set."),
c906108c
SS
8946 &maintenanceinfolist);
8947
44feb3ce
TT
8948 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
8949Set catchpoints to catch events."),
8950 &catch_cmdlist, "catch ",
8951 0/*allow-unknown*/, &cmdlist);
8952
8953 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
8954Set temporary catchpoints to catch events."),
8955 &tcatch_cmdlist, "tcatch ",
8956 0/*allow-unknown*/, &cmdlist);
8957
8958 /* Add catch and tcatch sub-commands. */
8959 add_catch_command ("catch", _("\
8960Catch an exception, when caught.\n\
8961With an argument, catch only exceptions with the given name."),
8962 catch_catch_command,
8963 CATCH_PERMANENT,
8964 CATCH_TEMPORARY);
8965 add_catch_command ("throw", _("\
8966Catch an exception, when thrown.\n\
8967With an argument, catch only exceptions with the given name."),
8968 catch_throw_command,
8969 CATCH_PERMANENT,
8970 CATCH_TEMPORARY);
8971 add_catch_command ("fork", _("Catch calls to fork."),
8972 catch_fork_command_1,
8973 (void *) (uintptr_t) catch_fork_permanent,
8974 (void *) (uintptr_t) catch_fork_temporary);
8975 add_catch_command ("vfork", _("Catch calls to vfork."),
8976 catch_fork_command_1,
8977 (void *) (uintptr_t) catch_vfork_permanent,
8978 (void *) (uintptr_t) catch_vfork_temporary);
8979 add_catch_command ("exec", _("Catch calls to exec."),
8980 catch_exec_command_1,
8981 CATCH_PERMANENT,
8982 CATCH_TEMPORARY);
44feb3ce
TT
8983 add_catch_command ("exception", _("\
8984Catch Ada exceptions, when raised.\n\
8985With an argument, catch only exceptions with the given name."),
8986 catch_ada_exception_command,
8987 CATCH_PERMANENT,
8988 CATCH_TEMPORARY);
8989 add_catch_command ("assert", _("\
8990Catch failed Ada assertions, when raised.\n\
8991With an argument, catch only exceptions with the given name."),
8992 catch_assert_command,
8993 CATCH_PERMANENT,
8994 CATCH_TEMPORARY);
c5aa993b 8995
1bedd215
AC
8996 c = add_com ("watch", class_breakpoint, watch_command, _("\
8997Set a watchpoint for an expression.\n\
c906108c 8998A watchpoint stops execution of your program whenever the value of\n\
1bedd215 8999an expression changes."));
65d12d83 9000 set_cmd_completer (c, expression_completer);
c906108c 9001
1bedd215
AC
9002 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
9003Set a read watchpoint for an expression.\n\
c906108c 9004A watchpoint stops execution of your program whenever the value of\n\
1bedd215 9005an expression is read."));
65d12d83 9006 set_cmd_completer (c, expression_completer);
c906108c 9007
1bedd215
AC
9008 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
9009Set a watchpoint for an expression.\n\
c906108c 9010A watchpoint stops execution of your program whenever the value of\n\
1bedd215 9011an expression is either read or written."));
65d12d83 9012 set_cmd_completer (c, expression_completer);
c906108c
SS
9013
9014 add_info ("watchpoints", breakpoints_info,
1bedd215 9015 _("Synonym for ``info breakpoints''."));
c906108c
SS
9016
9017
920d2a44
AC
9018 /* XXX: cagney/2005-02-23: This should be a boolean, and should
9019 respond to changes - contrary to the description. */
85c07804
AC
9020 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
9021 &can_use_hw_watchpoints, _("\
9022Set debugger's willingness to use watchpoint hardware."), _("\
9023Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
9024If zero, gdb will not use hardware for new watchpoints, even if\n\
9025such is available. (However, any hardware watchpoints that were\n\
9026created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
9027hardware.)"),
9028 NULL,
920d2a44 9029 show_can_use_hw_watchpoints,
85c07804 9030 &setlist, &showlist);
c906108c
SS
9031
9032 can_use_hw_watchpoints = 1;
fa8d40ab 9033
1042e4c0
SS
9034 /* Tracepoint manipulation commands. */
9035
9036 c = add_com ("trace", class_breakpoint, trace_command, _("\
9037Set a tracepoint at specified line or function.\n\
9038\n"
9039BREAK_ARGS_HELP ("trace") "\n\
9040Do \"help tracepoints\" for info on other tracepoint commands."));
9041 set_cmd_completer (c, location_completer);
9042
9043 add_com_alias ("tp", "trace", class_alias, 0);
9044 add_com_alias ("tr", "trace", class_alias, 1);
9045 add_com_alias ("tra", "trace", class_alias, 1);
9046 add_com_alias ("trac", "trace", class_alias, 1);
9047
9048 add_info ("tracepoints", tracepoints_info, _("\
9049Status of tracepoints, or tracepoint number NUMBER.\n\
9050Convenience variable \"$tpnum\" contains the number of the\n\
9051last tracepoint set."));
9052
9053 add_info_alias ("tp", "tracepoints", 1);
9054
9055 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
9056Delete specified tracepoints.\n\
9057Arguments are tracepoint numbers, separated by spaces.\n\
9058No argument means delete all tracepoints."),
9059 &deletelist);
9060
9061 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
9062Disable specified tracepoints.\n\
9063Arguments are tracepoint numbers, separated by spaces.\n\
9064No argument means disable all tracepoints."),
9065 &disablelist);
9066 deprecate_cmd (c, "disable");
9067
9068 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
9069Enable specified tracepoints.\n\
9070Arguments are tracepoint numbers, separated by spaces.\n\
9071No argument means enable all tracepoints."),
9072 &enablelist);
9073 deprecate_cmd (c, "enable");
9074
9075 add_com ("passcount", class_trace, trace_pass_command, _("\
9076Set the passcount for a tracepoint.\n\
9077The trace will end when the tracepoint has been passed 'count' times.\n\
9078Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
9079if TPNUM is omitted, passcount refers to the last tracepoint defined."));
9080
9081 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command, _("\
9082Save current tracepoint definitions as a script.\n\
9083Use the 'source' command in another debug session to restore them."));
9084 set_cmd_completer (c, filename_completer);
9085
1bedd215 9086 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
9087Breakpoint specific settings\n\
9088Configure various breakpoint-specific variables such as\n\
1bedd215 9089pending breakpoint behavior"),
fa8d40ab
JJ
9090 &breakpoint_set_cmdlist, "set breakpoint ",
9091 0/*allow-unknown*/, &setlist);
1bedd215 9092 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
9093Breakpoint specific settings\n\
9094Configure various breakpoint-specific variables such as\n\
1bedd215 9095pending breakpoint behavior"),
fa8d40ab
JJ
9096 &breakpoint_show_cmdlist, "show breakpoint ",
9097 0/*allow-unknown*/, &showlist);
9098
7915a72c
AC
9099 add_setshow_auto_boolean_cmd ("pending", no_class,
9100 &pending_break_support, _("\
9101Set debugger's behavior regarding pending breakpoints."), _("\
9102Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
9103If on, an unrecognized breakpoint location will cause gdb to create a\n\
9104pending breakpoint. If off, an unrecognized breakpoint location results in\n\
9105an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 9106user-query to see if a pending breakpoint should be created."),
2c5b56ce 9107 NULL,
920d2a44 9108 show_pending_break_support,
6e1d7d6c
AC
9109 &breakpoint_set_cmdlist,
9110 &breakpoint_show_cmdlist);
fa8d40ab
JJ
9111
9112 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
9113
9114 add_setshow_boolean_cmd ("auto-hw", no_class,
9115 &automatic_hardware_breakpoints, _("\
9116Set automatic usage of hardware breakpoints."), _("\
9117Show automatic usage of hardware breakpoints."), _("\
9118If set, the debugger will automatically use hardware breakpoints for\n\
9119breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
9120a warning will be emitted for such breakpoints."),
9121 NULL,
9122 show_automatic_hardware_breakpoints,
9123 &breakpoint_set_cmdlist,
9124 &breakpoint_show_cmdlist);
74960c60 9125
33e5cbd6
PA
9126 add_setshow_enum_cmd ("always-inserted", class_support,
9127 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
9128Set mode for inserting breakpoints."), _("\
9129Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
9130When this mode is off, breakpoints are inserted in inferior when it is\n\
9131resumed, and removed when execution stops. When this mode is on,\n\
9132breakpoints are inserted immediately and removed only when the user\n\
9133deletes the breakpoint. When this mode is auto (which is the default),\n\
9134the behaviour depends on the non-stop setting (see help set non-stop).\n\
9135In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
9136behaves as if always-inserted mode is on; if gdb is controlling the\n\
9137inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
9138 NULL,
9139 &show_always_inserted_mode,
9140 &breakpoint_set_cmdlist,
9141 &breakpoint_show_cmdlist);
765dc015
VP
9142
9143 automatic_hardware_breakpoints = 1;
f3b1572e
PA
9144
9145 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 9146}
This page took 1.766305 seconds and 4 git commands to generate.