* ada-valprint.c (ada_print_scalar): Accept NULL type argument
[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 {
edb3359d
DJ
2775 struct frame_info *frame = get_current_frame ();
2776 struct gdbarch *frame_arch = get_frame_arch (frame);
2777 CORE_ADDR frame_pc = get_frame_pc (frame);
2778
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. */
edb3359d
DJ
2784 if (within_current_scope)
2785 {
2786 struct symbol *function;
2787
2788 function = get_frame_function (fr);
2789 if (function == NULL
2790 || !contained_in (b->exp_valid_block,
2791 SYMBOL_BLOCK_VALUE (function)))
2792 within_current_scope = 0;
2793 }
69fbadd5 2794
c12260ac
CV
2795 /* in_function_epilogue_p() returns a non-zero value if we're still
2796 in the function but the stack frame has already been invalidated.
2797 Since we can't rely on the values of local variables after the
2798 stack has been destroyed, we are treating the watchpoint in that
edb3359d
DJ
2799 state as `not changed' without further checking. Don't mark
2800 watchpoints as changed if the current frame is in an epilogue -
2801 even if they are in some other frame, our view of the stack
2802 is likely to be wrong. */
2803 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
2804 return WP_VALUE_NOT_CHANGED;
fb14de7b 2805
edb3359d 2806 if (within_current_scope)
c906108c
SS
2807 /* If we end up stopping, the current frame will get selected
2808 in normal_stop. So this call to select_frame won't affect
2809 the user. */
0f7d239c 2810 select_frame (fr);
c906108c 2811 }
c5aa993b 2812
c906108c
SS
2813 if (within_current_scope)
2814 {
2815 /* We use value_{,free_to_}mark because it could be a
2816 *long* time before we return to the command level and
c5aa993b
JM
2817 call free_all_values. We can't call free_all_values because
2818 we might be in the middle of evaluating a function call. */
c906108c 2819
278cd55f 2820 struct value *mark = value_mark ();
fa4727a6
DJ
2821 struct value *new_val;
2822
2823 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
2824 if ((b->val != NULL) != (new_val != NULL)
2825 || (b->val != NULL && !value_equal (b->val, new_val)))
c906108c 2826 {
fa4727a6
DJ
2827 if (new_val != NULL)
2828 {
2829 release_value (new_val);
2830 value_free_to_mark (mark);
2831 }
c906108c
SS
2832 bs->old_val = b->val;
2833 b->val = new_val;
fa4727a6 2834 b->val_valid = 1;
c906108c
SS
2835 /* We will stop here */
2836 return WP_VALUE_CHANGED;
2837 }
2838 else
2839 {
2840 /* Nothing changed, don't do anything. */
2841 value_free_to_mark (mark);
2842 /* We won't stop here */
2843 return WP_VALUE_NOT_CHANGED;
2844 }
2845 }
2846 else
2847 {
2848 /* This seems like the only logical thing to do because
c5aa993b
JM
2849 if we temporarily ignored the watchpoint, then when
2850 we reenter the block in which it is valid it contains
2851 garbage (in the case of a function, it may have two
2852 garbage values, one before and one after the prologue).
2853 So we can't even detect the first assignment to it and
2854 watch after that (since the garbage may or may not equal
2855 the first value assigned). */
4ce44c66
JM
2856 /* We print all the stop information in print_it_typical(), but
2857 in this case, by the time we call print_it_typical() this bp
2858 will be deleted already. So we have no choice but print the
2859 information here. */
9dc5e2a9 2860 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
2861 ui_out_field_string
2862 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 2863 ui_out_text (uiout, "\nWatchpoint ");
4f8d1dc6 2864 ui_out_field_int (uiout, "wpnum", b->number);
8b93c638
JM
2865 ui_out_text (uiout, " deleted because the program has left the block in\n\
2866which its expression is valid.\n");
4ce44c66 2867
c906108c 2868 if (b->related_breakpoint)
b5de0fa7
EZ
2869 b->related_breakpoint->disposition = disp_del_at_next_stop;
2870 b->disposition = disp_del_at_next_stop;
c906108c
SS
2871
2872 return WP_DELETED;
2873 }
2874}
2875
18a18393
VP
2876/* Return true if it looks like target has stopped due to hitting
2877 breakpoint location BL. This function does not check if we
2878 should stop, only if BL explains the stop. */
2879static int
2880bpstat_check_location (const struct bp_location *bl, CORE_ADDR bp_addr)
2881{
2882 struct breakpoint *b = bl->owner;
2883
2884 if (b->type != bp_watchpoint
2885 && b->type != bp_hardware_watchpoint
2886 && b->type != bp_read_watchpoint
2887 && b->type != bp_access_watchpoint
2888 && b->type != bp_hardware_breakpoint
fe798b75 2889 && b->type != bp_catchpoint) /* a non-watchpoint bp */
18a18393
VP
2890 {
2891 if (bl->address != bp_addr) /* address doesn't match */
2892 return 0;
2893 if (overlay_debugging /* unmapped overlay section */
2894 && section_is_overlay (bl->section)
2895 && !section_is_mapped (bl->section))
2896 return 0;
2897 }
2898
2899 /* Continuable hardware watchpoints are treated as non-existent if the
2900 reason we stopped wasn't a hardware watchpoint (we didn't stop on
2901 some data address). Otherwise gdb won't stop on a break instruction
2902 in the code (not from a breakpoint) when a hardware watchpoint has
2903 been defined. Also skip watchpoints which we know did not trigger
2904 (did not match the data address). */
2905
2906 if ((b->type == bp_hardware_watchpoint
2907 || b->type == bp_read_watchpoint
2908 || b->type == bp_access_watchpoint)
2909 && b->watchpoint_triggered == watch_triggered_no)
2910 return 0;
2911
2912 if (b->type == bp_hardware_breakpoint)
2913 {
2914 if (bl->address != bp_addr)
2915 return 0;
2916 if (overlay_debugging /* unmapped overlay section */
2917 && section_is_overlay (bl->section)
2918 && !section_is_mapped (bl->section))
2919 return 0;
2920 }
ce78b96d 2921
ce78b96d
JB
2922 if (b->type == bp_catchpoint)
2923 {
2924 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
2925 if (!b->ops->breakpoint_hit (b))
2926 return 0;
2927 }
2928
18a18393
VP
2929 return 1;
2930}
2931
2932/* If BS refers to a watchpoint, determine if the watched values
2933 has actually changed, and we should stop. If not, set BS->stop
2934 to 0. */
2935static void
2936bpstat_check_watchpoint (bpstat bs)
2937{
2938 const struct bp_location *bl = bs->breakpoint_at;
2939 struct breakpoint *b = bl->owner;
2940
2941 if (b->type == bp_watchpoint
2942 || b->type == bp_read_watchpoint
2943 || b->type == bp_access_watchpoint
2944 || b->type == bp_hardware_watchpoint)
2945 {
2946 CORE_ADDR addr;
2947 struct value *v;
2948 int must_check_value = 0;
2949
2950 if (b->type == bp_watchpoint)
2951 /* For a software watchpoint, we must always check the
2952 watched value. */
2953 must_check_value = 1;
2954 else if (b->watchpoint_triggered == watch_triggered_yes)
2955 /* We have a hardware watchpoint (read, write, or access)
2956 and the target earlier reported an address watched by
2957 this watchpoint. */
2958 must_check_value = 1;
2959 else if (b->watchpoint_triggered == watch_triggered_unknown
2960 && b->type == bp_hardware_watchpoint)
2961 /* We were stopped by a hardware watchpoint, but the target could
2962 not report the data address. We must check the watchpoint's
2963 value. Access and read watchpoints are out of luck; without
2964 a data address, we can't figure it out. */
2965 must_check_value = 1;
2966
2967 if (must_check_value)
2968 {
2969 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
2970 b->number);
2971 struct cleanup *cleanups = make_cleanup (xfree, message);
2972 int e = catch_errors (watchpoint_check, bs, message,
2973 RETURN_MASK_ALL);
2974 do_cleanups (cleanups);
2975 switch (e)
2976 {
2977 case WP_DELETED:
2978 /* We've already printed what needs to be printed. */
2979 bs->print_it = print_it_done;
2980 /* Stop. */
2981 break;
2982 case WP_VALUE_CHANGED:
2983 if (b->type == bp_read_watchpoint)
2984 {
2985 /* Don't stop: read watchpoints shouldn't fire if
2986 the value has changed. This is for targets
2987 which cannot set read-only watchpoints. */
2988 bs->print_it = print_it_noop;
2989 bs->stop = 0;
2990 }
2991 break;
2992 case WP_VALUE_NOT_CHANGED:
2993 if (b->type == bp_hardware_watchpoint
2994 || b->type == bp_watchpoint)
2995 {
2996 /* Don't stop: write watchpoints shouldn't fire if
2997 the value hasn't changed. */
2998 bs->print_it = print_it_noop;
2999 bs->stop = 0;
3000 }
3001 /* Stop. */
3002 break;
3003 default:
3004 /* Can't happen. */
3005 case 0:
3006 /* Error from catch_errors. */
3007 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3008 if (b->related_breakpoint)
3009 b->related_breakpoint->disposition = disp_del_at_next_stop;
3010 b->disposition = disp_del_at_next_stop;
3011 /* We've already printed what needs to be printed. */
3012 bs->print_it = print_it_done;
3013 break;
3014 }
3015 }
3016 else /* must_check_value == 0 */
3017 {
3018 /* This is a case where some watchpoint(s) triggered, but
3019 not at the address of this watchpoint, or else no
3020 watchpoint triggered after all. So don't print
3021 anything for this watchpoint. */
3022 bs->print_it = print_it_noop;
3023 bs->stop = 0;
3024 }
3025 }
3026}
3027
3028
3029/* Check conditions (condition proper, frame, thread and ignore count)
3030 of breakpoint referred to by BS. If we should not stop for this
3031 breakpoint, set BS->stop to 0. */
3032static void
3033bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3034{
3035 int thread_id = pid_to_thread_id (ptid);
3036 const struct bp_location *bl = bs->breakpoint_at;
3037 struct breakpoint *b = bl->owner;
3038
3039 if (frame_id_p (b->frame_id)
edb3359d 3040 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
3041 bs->stop = 0;
3042 else if (bs->stop)
3043 {
3044 int value_is_zero = 0;
3045
3046 /* If this is a scope breakpoint, mark the associated
3047 watchpoint as triggered so that we will handle the
3048 out-of-scope event. We'll get to the watchpoint next
3049 iteration. */
3050 if (b->type == bp_watchpoint_scope)
3051 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
3052
3053 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
3054 {
c5bc3a77
DJ
3055 /* We use value_mark and value_free_to_mark because it could
3056 be a long time before we return to the command level and
3057 call free_all_values. We can't call free_all_values
3058 because we might be in the middle of evaluating a
3059 function call. */
3060 struct value *mark = value_mark ();
3061
edb3359d
DJ
3062 /* Need to select the frame, with all that implies so that
3063 the conditions will have the right context. Because we
3064 use the frame, we will not see an inlined function's
3065 variables when we arrive at a breakpoint at the start
3066 of the inlined function; the current frame will be the
3067 call site. */
18a18393
VP
3068 select_frame (get_current_frame ());
3069 value_is_zero
3070 = catch_errors (breakpoint_cond_eval, (bl->cond),
3071 "Error in testing breakpoint condition:\n",
3072 RETURN_MASK_ALL);
3073 /* FIXME-someday, should give breakpoint # */
c5bc3a77 3074 value_free_to_mark (mark);
18a18393
VP
3075 }
3076 if (bl->cond && value_is_zero)
3077 {
3078 bs->stop = 0;
3079 }
3080 else if (b->thread != -1 && b->thread != thread_id)
3081 {
3082 bs->stop = 0;
3083 }
3084 else if (b->ignore_count > 0)
3085 {
3086 b->ignore_count--;
3087 annotate_ignore_count_change ();
3088 bs->stop = 0;
3089 /* Increase the hit count even though we don't
3090 stop. */
3091 ++(b->hit_count);
3092 }
3093 }
3094}
3095
3096
9709f61c 3097/* Get a bpstat associated with having just stopped at address
d983da9c 3098 BP_ADDR in thread PTID.
c906108c 3099
d983da9c 3100 Determine whether we stopped at a breakpoint, etc, or whether we
c906108c
SS
3101 don't understand this stop. Result is a chain of bpstat's such that:
3102
c5aa993b 3103 if we don't understand the stop, the result is a null pointer.
c906108c 3104
c5aa993b 3105 if we understand why we stopped, the result is not null.
c906108c 3106
c5aa993b
JM
3107 Each element of the chain refers to a particular breakpoint or
3108 watchpoint at which we have stopped. (We may have stopped for
3109 several reasons concurrently.)
c906108c 3110
c5aa993b
JM
3111 Each element of the chain has valid next, breakpoint_at,
3112 commands, FIXME??? fields. */
c906108c
SS
3113
3114bpstat
d983da9c 3115bpstat_stop_status (CORE_ADDR bp_addr, ptid_t ptid)
c906108c 3116{
0d381245 3117 struct breakpoint *b = NULL;
89f9893c 3118 const struct bp_location *bl;
20874c92 3119 struct bp_location *loc;
c906108c
SS
3120 /* Root of the chain of bpstat's */
3121 struct bpstats root_bs[1];
3122 /* Pointer to the last thing in the chain currently. */
3123 bpstat bs = root_bs;
20874c92 3124 int ix;
d832cb68 3125 int need_remove_insert;
c906108c 3126
0d381245 3127 ALL_BP_LOCATIONS (bl)
c5aa993b 3128 {
0d381245
VP
3129 b = bl->owner;
3130 gdb_assert (b);
468d015d 3131 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
c5aa993b
JM
3132 continue;
3133
a5606eee
VP
3134 /* For hardware watchpoints, we look only at the first location.
3135 The watchpoint_check function will work on entire expression,
3136 not the individual locations. For read watchopints, the
3137 watchpoints_triggered function have checked all locations
3138 alrea
3139 */
3140 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3141 continue;
3142
18a18393
VP
3143 if (!bpstat_check_location (bl, bp_addr))
3144 continue;
3145
c5aa993b
JM
3146 /* Come here if it's a watchpoint, or if the break address matches */
3147
0d381245 3148 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
c5aa993b 3149
18a18393
VP
3150 /* Assume we stop. Should we find watchpoint that is not actually
3151 triggered, or if condition of breakpoint is false, we'll reset
3152 'stop' to 0. */
c5aa993b
JM
3153 bs->stop = 1;
3154 bs->print = 1;
3155
18a18393
VP
3156 bpstat_check_watchpoint (bs);
3157 if (!bs->stop)
3158 continue;
c5aa993b 3159
0fd8e87f
UW
3160 if (b->type == bp_thread_event || b->type == bp_overlay_event
3161 || b->type == bp_longjmp_master)
18a18393 3162 /* We do not stop for these. */
c5aa993b
JM
3163 bs->stop = 0;
3164 else
18a18393
VP
3165 bpstat_check_breakpoint_conditions (bs, ptid);
3166
3167 if (bs->stop)
c5aa993b 3168 {
18a18393 3169 ++(b->hit_count);
d983da9c 3170
18a18393
VP
3171 /* We will stop here */
3172 if (b->disposition == disp_disable)
c5aa993b 3173 {
514f746b
AR
3174 if (b->enable_state != bp_permanent)
3175 b->enable_state = bp_disabled;
b60e7edf 3176 update_global_location_list (0);
c5aa993b 3177 }
18a18393
VP
3178 if (b->silent)
3179 bs->print = 0;
3180 bs->commands = b->commands;
059fb39f
PM
3181 if (bs->commands
3182 && (strcmp ("silent", bs->commands->line) == 0
3183 || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
c5aa993b 3184 {
18a18393
VP
3185 bs->commands = bs->commands->next;
3186 bs->print = 0;
c5aa993b 3187 }
18a18393 3188 bs->commands = copy_command_lines (bs->commands);
c5aa993b 3189 }
18a18393 3190
c5aa993b
JM
3191 /* Print nothing for this entry if we dont stop or if we dont print. */
3192 if (bs->stop == 0 || bs->print == 0)
3193 bs->print_it = print_it_noop;
3194 }
c906108c 3195
20874c92
VP
3196 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3197 {
3198 if (loc->address == bp_addr)
3199 {
3200 bs = bpstat_alloc (loc, bs);
3201 /* For hits of moribund locations, we should just proceed. */
3202 bs->stop = 0;
3203 bs->print = 0;
3204 bs->print_it = print_it_noop;
3205 }
3206 }
3207
c906108c
SS
3208 bs->next = NULL; /* Terminate the chain */
3209 bs = root_bs->next; /* Re-grab the head of the chain */
3210
d983da9c
DJ
3211 /* If we aren't stopping, the value of some hardware watchpoint may
3212 not have changed, but the intermediate memory locations we are
3213 watching may have. Don't bother if we're stopping; this will get
3214 done later. */
3215 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3216 if (bs->stop)
3217 break;
3218
d832cb68 3219 need_remove_insert = 0;
d983da9c
DJ
3220 if (bs == NULL)
3221 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3222 if (!bs->stop
20874c92 3223 && bs->breakpoint_at->owner
d983da9c
DJ
3224 && (bs->breakpoint_at->owner->type == bp_hardware_watchpoint
3225 || bs->breakpoint_at->owner->type == bp_read_watchpoint
3226 || bs->breakpoint_at->owner->type == bp_access_watchpoint))
3227 {
a5606eee
VP
3228 /* remove/insert can invalidate bs->breakpoint_at, if this
3229 location is no longer used by the watchpoint. Prevent
3230 further code from trying to use it. */
3231 bs->breakpoint_at = NULL;
d832cb68 3232 need_remove_insert = 1;
d983da9c
DJ
3233 }
3234
d832cb68
DD
3235 if (need_remove_insert)
3236 {
3237 remove_breakpoints ();
3238 insert_breakpoints ();
3239 }
3240
d983da9c 3241 return root_bs->next;
c906108c
SS
3242}
3243\f
3244/* Tell what to do about this bpstat. */
3245struct bpstat_what
fba45db2 3246bpstat_what (bpstat bs)
c906108c
SS
3247{
3248 /* Classify each bpstat as one of the following. */
c5aa993b
JM
3249 enum class
3250 {
3251 /* This bpstat element has no effect on the main_action. */
3252 no_effect = 0,
3253
3254 /* There was a watchpoint, stop but don't print. */
3255 wp_silent,
c906108c 3256
c5aa993b
JM
3257 /* There was a watchpoint, stop and print. */
3258 wp_noisy,
c906108c 3259
c5aa993b
JM
3260 /* There was a breakpoint but we're not stopping. */
3261 bp_nostop,
c906108c 3262
c5aa993b
JM
3263 /* There was a breakpoint, stop but don't print. */
3264 bp_silent,
c906108c 3265
c5aa993b
JM
3266 /* There was a breakpoint, stop and print. */
3267 bp_noisy,
c906108c 3268
c5aa993b
JM
3269 /* We hit the longjmp breakpoint. */
3270 long_jump,
c906108c 3271
c5aa993b
JM
3272 /* We hit the longjmp_resume breakpoint. */
3273 long_resume,
c906108c 3274
c5aa993b
JM
3275 /* We hit the step_resume breakpoint. */
3276 step_resume,
c906108c 3277
c5aa993b
JM
3278 /* We hit the shared library event breakpoint. */
3279 shlib_event,
c906108c 3280
c5aa993b
JM
3281 /* This is just used to count how many enums there are. */
3282 class_last
c906108c
SS
3283 };
3284
3285 /* Here is the table which drives this routine. So that we can
3286 format it pretty, we define some abbreviations for the
3287 enum bpstat_what codes. */
3288#define kc BPSTAT_WHAT_KEEP_CHECKING
3289#define ss BPSTAT_WHAT_STOP_SILENT
3290#define sn BPSTAT_WHAT_STOP_NOISY
3291#define sgl BPSTAT_WHAT_SINGLE
3292#define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3293#define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
c906108c 3294#define sr BPSTAT_WHAT_STEP_RESUME
c906108c 3295#define shl BPSTAT_WHAT_CHECK_SHLIBS
c906108c
SS
3296
3297/* "Can't happen." Might want to print an error message.
3298 abort() is not out of the question, but chances are GDB is just
3299 a bit confused, not unusable. */
3300#define err BPSTAT_WHAT_STOP_NOISY
3301
3302 /* Given an old action and a class, come up with a new action. */
3303 /* One interesting property of this table is that wp_silent is the same
3304 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3305 after stopping, the check for whether to step over a breakpoint
3306 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
53a5351d
JM
3307 reference to how we stopped. We retain separate wp_silent and
3308 bp_silent codes in case we want to change that someday.
43ff13b4
JM
3309
3310 Another possibly interesting property of this table is that
3311 there's a partial ordering, priority-like, of the actions. Once
3312 you've decided that some action is appropriate, you'll never go
3313 back and decide something of a lower priority is better. The
3314 ordering is:
3315
d81191fc
JB
3316 kc < clr sgl shl slr sn sr ss
3317 sgl < shl slr sn sr ss
3318 slr < err shl sn sr ss
3319 clr < err shl sn sr ss
3320 ss < shl sn sr
3321 sn < shl sr
3322 shl < sr
4d5b2cd7 3323 sr <
c5aa993b 3324
43ff13b4
JM
3325 What I think this means is that we don't need a damned table
3326 here. If you just put the rows and columns in the right order,
3327 it'd look awfully regular. We could simply walk the bpstat list
3328 and choose the highest priority action we find, with a little
78b6a731 3329 logic to handle the 'err' cases. */
c906108c
SS
3330
3331 /* step_resume entries: a step resume breakpoint overrides another
3332 breakpoint of signal handling (see comment in wait_for_inferior
fcf70625 3333 at where we set the step_resume breakpoint). */
c906108c
SS
3334
3335 static const enum bpstat_what_main_action
c5aa993b
JM
3336 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3337 {
3338 /* old action */
d81191fc 3339 /* kc ss sn sgl slr clr sr shl
c5aa993b
JM
3340 */
3341/*no_effect */
d81191fc 3342 {kc, ss, sn, sgl, slr, clr, sr, shl},
c5aa993b 3343/*wp_silent */
d81191fc 3344 {ss, ss, sn, ss, ss, ss, sr, shl},
c5aa993b 3345/*wp_noisy */
d81191fc 3346 {sn, sn, sn, sn, sn, sn, sr, shl},
c5aa993b 3347/*bp_nostop */
d81191fc 3348 {sgl, ss, sn, sgl, slr, slr, sr, shl},
c5aa993b 3349/*bp_silent */
d81191fc 3350 {ss, ss, sn, ss, ss, ss, sr, shl},
c5aa993b 3351/*bp_noisy */
d81191fc 3352 {sn, sn, sn, sn, sn, sn, sr, shl},
c5aa993b 3353/*long_jump */
d81191fc 3354 {slr, ss, sn, slr, slr, err, sr, shl},
c5aa993b 3355/*long_resume */
d81191fc 3356 {clr, ss, sn, err, err, err, sr, shl},
c5aa993b 3357/*step_resume */
d81191fc 3358 {sr, sr, sr, sr, sr, sr, sr, sr},
c5aa993b 3359/*shlib */
d81191fc 3360 {shl, shl, shl, shl, shl, shl, sr, shl}
c5aa993b 3361 };
c906108c
SS
3362
3363#undef kc
3364#undef ss
3365#undef sn
3366#undef sgl
3367#undef slr
3368#undef clr
c906108c
SS
3369#undef err
3370#undef sr
3371#undef ts
3372#undef shl
c906108c
SS
3373 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3374 struct bpstat_what retval;
3375
3376 retval.call_dummy = 0;
3377 for (; bs != NULL; bs = bs->next)
3378 {
3379 enum class bs_class = no_effect;
3380 if (bs->breakpoint_at == NULL)
3381 /* I suspect this can happen if it was a momentary breakpoint
3382 which has since been deleted. */
3383 continue;
20874c92
VP
3384 if (bs->breakpoint_at->owner == NULL)
3385 bs_class = bp_nostop;
3386 else
4f8d1dc6 3387 switch (bs->breakpoint_at->owner->type)
c906108c
SS
3388 {
3389 case bp_none:
3390 continue;
3391
3392 case bp_breakpoint:
3393 case bp_hardware_breakpoint:
3394 case bp_until:
3395 case bp_finish:
3396 if (bs->stop)
3397 {
3398 if (bs->print)
3399 bs_class = bp_noisy;
3400 else
3401 bs_class = bp_silent;
3402 }
3403 else
3404 bs_class = bp_nostop;
3405 break;
3406 case bp_watchpoint:
3407 case bp_hardware_watchpoint:
3408 case bp_read_watchpoint:
3409 case bp_access_watchpoint:
3410 if (bs->stop)
3411 {
3412 if (bs->print)
3413 bs_class = wp_noisy;
3414 else
3415 bs_class = wp_silent;
3416 }
3417 else
53a5351d
JM
3418 /* There was a watchpoint, but we're not stopping.
3419 This requires no further action. */
c906108c
SS
3420 bs_class = no_effect;
3421 break;
3422 case bp_longjmp:
3423 bs_class = long_jump;
3424 break;
3425 case bp_longjmp_resume:
3426 bs_class = long_resume;
3427 break;
3428 case bp_step_resume:
3429 if (bs->stop)
3430 {
3431 bs_class = step_resume;
3432 }
3433 else
3434 /* It is for the wrong frame. */
3435 bs_class = bp_nostop;
3436 break;
c906108c
SS
3437 case bp_watchpoint_scope:
3438 bs_class = bp_nostop;
3439 break;
c5aa993b
JM
3440 case bp_shlib_event:
3441 bs_class = shlib_event;
3442 break;
c4093a6a 3443 case bp_thread_event:
1900040c 3444 case bp_overlay_event:
0fd8e87f 3445 case bp_longjmp_master:
c4093a6a
JM
3446 bs_class = bp_nostop;
3447 break;
ce78b96d 3448 case bp_catchpoint:
c5aa993b
JM
3449 if (bs->stop)
3450 {
3451 if (bs->print)
3452 bs_class = bp_noisy;
3453 else
3454 bs_class = bp_silent;
3455 }
3456 else
53a5351d
JM
3457 /* There was a catchpoint, but we're not stopping.
3458 This requires no further action. */
c5aa993b
JM
3459 bs_class = no_effect;
3460 break;
c906108c 3461 case bp_call_dummy:
53a5351d
JM
3462 /* Make sure the action is stop (silent or noisy),
3463 so infrun.c pops the dummy frame. */
c906108c
SS
3464 bs_class = bp_silent;
3465 retval.call_dummy = 1;
3466 break;
1042e4c0
SS
3467 case bp_tracepoint:
3468 /* Tracepoint hits should not be reported back to GDB, and
3469 if one got through somehow, it should have been filtered
3470 out already. */
3471 internal_error (__FILE__, __LINE__,
3472 _("bpstat_what: bp_tracepoint encountered"));
3473 break;
c906108c 3474 }
c5aa993b 3475 current_action = table[(int) bs_class][(int) current_action];
c906108c
SS
3476 }
3477 retval.main_action = current_action;
3478 return retval;
3479}
3480
3481/* Nonzero if we should step constantly (e.g. watchpoints on machines
3482 without hardware support). This isn't related to a specific bpstat,
3483 just to things like whether watchpoints are set. */
3484
c5aa993b 3485int
fba45db2 3486bpstat_should_step (void)
c906108c
SS
3487{
3488 struct breakpoint *b;
3489 ALL_BREAKPOINTS (b)
717a8278 3490 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 3491 return 1;
c906108c
SS
3492 return 0;
3493}
3494
c906108c 3495\f
c5aa993b 3496
0d381245
VP
3497static void print_breakpoint_location (struct breakpoint *b,
3498 struct bp_location *loc,
3499 char *wrap_indent,
3500 struct ui_stream *stb)
3501{
3502 if (b->source_file)
3503 {
3504 struct symbol *sym
3505 = find_pc_sect_function (loc->address, loc->section);
3506 if (sym)
3507 {
3508 ui_out_text (uiout, "in ");
3509 ui_out_field_string (uiout, "func",
3510 SYMBOL_PRINT_NAME (sym));
3511 ui_out_wrap_hint (uiout, wrap_indent);
3512 ui_out_text (uiout, " at ");
3513 }
3514 ui_out_field_string (uiout, "file", b->source_file);
3515 ui_out_text (uiout, ":");
3516
3517 if (ui_out_is_mi_like_p (uiout))
3518 {
3519 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3520 char *fullname = symtab_to_fullname (sal.symtab);
3521
3522 if (fullname)
3523 ui_out_field_string (uiout, "fullname", fullname);
3524 }
3525
3526 ui_out_field_int (uiout, "line", b->line_number);
3527 }
3528 else if (!b->loc)
3529 {
3530 ui_out_field_string (uiout, "pending", b->addr_string);
3531 }
3532 else
3533 {
3534 print_address_symbolic (loc->address, stb->stream, demangle, "");
3535 ui_out_field_stream (uiout, "at", stb);
3536 }
3537}
3538
c4093a6a 3539/* Print B to gdb_stdout. */
c906108c 3540static void
0d381245
VP
3541print_one_breakpoint_location (struct breakpoint *b,
3542 struct bp_location *loc,
3543 int loc_number,
3544 CORE_ADDR *last_addr)
c906108c 3545{
52f0bd74
AC
3546 struct command_line *l;
3547 struct symbol *sym;
c4093a6a
JM
3548 struct ep_type_description
3549 {
3550 enum bptype type;
3551 char *description;
3552 };
3553 static struct ep_type_description bptypes[] =
c906108c 3554 {
c5aa993b
JM
3555 {bp_none, "?deleted?"},
3556 {bp_breakpoint, "breakpoint"},
c906108c 3557 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
3558 {bp_until, "until"},
3559 {bp_finish, "finish"},
3560 {bp_watchpoint, "watchpoint"},
c906108c 3561 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
3562 {bp_read_watchpoint, "read watchpoint"},
3563 {bp_access_watchpoint, "acc watchpoint"},
3564 {bp_longjmp, "longjmp"},
3565 {bp_longjmp_resume, "longjmp resume"},
3566 {bp_step_resume, "step resume"},
c5aa993b
JM
3567 {bp_watchpoint_scope, "watchpoint scope"},
3568 {bp_call_dummy, "call dummy"},
3569 {bp_shlib_event, "shlib events"},
c4093a6a 3570 {bp_thread_event, "thread events"},
1900040c 3571 {bp_overlay_event, "overlay events"},
0fd8e87f 3572 {bp_longjmp_master, "longjmp master"},
ce78b96d 3573 {bp_catchpoint, "catchpoint"},
1042e4c0 3574 {bp_tracepoint, "tracepoint"},
c5aa993b 3575 };
c4093a6a 3576
c2c6d25f 3577 static char bpenables[] = "nynny";
c906108c 3578 char wrap_indent[80];
8b93c638
JM
3579 struct ui_stream *stb = ui_out_stream_new (uiout);
3580 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3b31d625 3581 struct cleanup *bkpt_chain;
c906108c 3582
0d381245
VP
3583 int header_of_multiple = 0;
3584 int part_of_multiple = (loc != NULL);
79a45b7d
TT
3585 struct value_print_options opts;
3586
3587 get_user_print_options (&opts);
0d381245
VP
3588
3589 gdb_assert (!loc || loc_number != 0);
3590 /* See comment in print_one_breakpoint concerning
3591 treatment of breakpoints with single disabled
3592 location. */
3593 if (loc == NULL
3594 && (b->loc != NULL
3595 && (b->loc->next != NULL || !b->loc->enabled)))
3596 header_of_multiple = 1;
3597 if (loc == NULL)
3598 loc = b->loc;
3599
c4093a6a 3600 annotate_record ();
3b31d625 3601 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
c4093a6a
JM
3602
3603 /* 1 */
3604 annotate_field (0);
0d381245
VP
3605 if (part_of_multiple)
3606 {
3607 char *formatted;
0c6773c1 3608 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
3609 ui_out_field_string (uiout, "number", formatted);
3610 xfree (formatted);
3611 }
3612 else
3613 {
3614 ui_out_field_int (uiout, "number", b->number);
3615 }
c4093a6a
JM
3616
3617 /* 2 */
3618 annotate_field (1);
0d381245
VP
3619 if (part_of_multiple)
3620 ui_out_field_skip (uiout, "type");
3621 else
3622 {
3623 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
3624 || ((int) b->type != bptypes[(int) b->type].type))
3625 internal_error (__FILE__, __LINE__,
3626 _("bptypes table does not describe type #%d."),
3627 (int) b->type);
3628 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3629 }
c4093a6a
JM
3630
3631 /* 3 */
3632 annotate_field (2);
0d381245
VP
3633 if (part_of_multiple)
3634 ui_out_field_skip (uiout, "disp");
3635 else
2cec12e5 3636 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 3637
c4093a6a
JM
3638
3639 /* 4 */
3640 annotate_field (3);
0d381245 3641 if (part_of_multiple)
54e52265 3642 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 3643 else
54e52265
VP
3644 ui_out_field_fmt (uiout, "enabled", "%c",
3645 bpenables[(int) b->enable_state]);
3646 ui_out_spaces (uiout, 2);
0d381245 3647
c4093a6a
JM
3648
3649 /* 5 and 6 */
3650 strcpy (wrap_indent, " ");
79a45b7d 3651 if (opts.addressprint)
75ac9d7b 3652 {
17a912b6 3653 if (gdbarch_addr_bit (current_gdbarch) <= 32)
75ac9d7b
MS
3654 strcat (wrap_indent, " ");
3655 else
3656 strcat (wrap_indent, " ");
3657 }
c906108c 3658
3086aeae 3659 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245
VP
3660 {
3661 /* Although the print_one can possibly print
3662 all locations, calling it here is not likely
3663 to get any nice result. So, make sure there's
3664 just one location. */
3665 gdb_assert (b->loc == NULL || b->loc->next == NULL);
3666 b->ops->print_one (b, last_addr);
3667 }
3086aeae
DJ
3668 else
3669 switch (b->type)
3670 {
3671 case bp_none:
3672 internal_error (__FILE__, __LINE__,
e2e0b3e5 3673 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 3674 break;
c906108c 3675
3086aeae
DJ
3676 case bp_watchpoint:
3677 case bp_hardware_watchpoint:
3678 case bp_read_watchpoint:
3679 case bp_access_watchpoint:
3680 /* Field 4, the address, is omitted (which makes the columns
3681 not line up too nicely with the headers, but the effect
3682 is relatively readable). */
79a45b7d 3683 if (opts.addressprint)
3086aeae
DJ
3684 ui_out_field_skip (uiout, "addr");
3685 annotate_field (5);
fa8a61dc 3686 ui_out_field_string (uiout, "what", b->exp_string);
3086aeae
DJ
3687 break;
3688
3086aeae
DJ
3689 case bp_breakpoint:
3690 case bp_hardware_breakpoint:
3691 case bp_until:
3692 case bp_finish:
3693 case bp_longjmp:
3694 case bp_longjmp_resume:
3695 case bp_step_resume:
3086aeae
DJ
3696 case bp_watchpoint_scope:
3697 case bp_call_dummy:
3698 case bp_shlib_event:
3699 case bp_thread_event:
3700 case bp_overlay_event:
0fd8e87f 3701 case bp_longjmp_master:
1042e4c0 3702 case bp_tracepoint:
79a45b7d 3703 if (opts.addressprint)
3086aeae
DJ
3704 {
3705 annotate_field (4);
54e52265 3706 if (header_of_multiple)
0d381245 3707 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 3708 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 3709 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 3710 else
0d381245 3711 ui_out_field_core_addr (uiout, "addr", loc->address);
3086aeae
DJ
3712 }
3713 annotate_field (5);
0d381245
VP
3714 if (!header_of_multiple)
3715 print_breakpoint_location (b, loc, wrap_indent, stb);
3716 if (b->loc)
3717 *last_addr = b->loc->address;
3086aeae
DJ
3718 break;
3719 }
c906108c 3720
4a306c9a 3721 if (!part_of_multiple)
c4093a6a 3722 {
4a306c9a
JB
3723 if (b->thread != -1)
3724 {
3725 /* FIXME: This seems to be redundant and lost here; see the
3726 "stop only in" line a little further down. */
3727 ui_out_text (uiout, " thread ");
3728 ui_out_field_int (uiout, "thread", b->thread);
3729 }
3730 else if (b->task != 0)
3731 {
3732 ui_out_text (uiout, " task ");
3733 ui_out_field_int (uiout, "task", b->task);
3734 }
c4093a6a
JM
3735 }
3736
8b93c638 3737 ui_out_text (uiout, "\n");
c4093a6a 3738
0d381245 3739 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
3740 {
3741 annotate_field (6);
8b93c638 3742 ui_out_text (uiout, "\tstop only in stack frame at ");
818dd999
AC
3743 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
3744 the frame ID. */
d0a55772 3745 ui_out_field_core_addr (uiout, "frame", b->frame_id.stack_addr);
8b93c638 3746 ui_out_text (uiout, "\n");
c4093a6a
JM
3747 }
3748
0d381245 3749 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
c4093a6a 3750 {
f7f9143b
JB
3751 /* We do not print the condition for Ada exception catchpoints
3752 because the condition is an internal implementation detail
3753 that we do not want to expose to the user. */
c4093a6a 3754 annotate_field (7);
1042e4c0
SS
3755 if (b->type == bp_tracepoint)
3756 ui_out_text (uiout, "\ttrace only if ");
3757 else
3758 ui_out_text (uiout, "\tstop only if ");
0101ce28
JJ
3759 ui_out_field_string (uiout, "cond", b->cond_string);
3760 ui_out_text (uiout, "\n");
3761 }
3762
0d381245 3763 if (!part_of_multiple && b->thread != -1)
c4093a6a
JM
3764 {
3765 /* FIXME should make an annotation for this */
8b93c638
JM
3766 ui_out_text (uiout, "\tstop only in thread ");
3767 ui_out_field_int (uiout, "thread", b->thread);
3768 ui_out_text (uiout, "\n");
c4093a6a
JM
3769 }
3770
63c715c6 3771 if (!part_of_multiple && b->hit_count)
c4093a6a
JM
3772 {
3773 /* FIXME should make an annotation for this */
8b93c638
JM
3774 if (ep_is_catchpoint (b))
3775 ui_out_text (uiout, "\tcatchpoint");
3776 else
3777 ui_out_text (uiout, "\tbreakpoint");
3778 ui_out_text (uiout, " already hit ");
3779 ui_out_field_int (uiout, "times", b->hit_count);
3780 if (b->hit_count == 1)
3781 ui_out_text (uiout, " time\n");
3782 else
3783 ui_out_text (uiout, " times\n");
c4093a6a
JM
3784 }
3785
fb40c209
AC
3786 /* Output the count also if it is zero, but only if this is
3787 mi. FIXME: Should have a better test for this. */
9dc5e2a9 3788 if (ui_out_is_mi_like_p (uiout))
63c715c6 3789 if (!part_of_multiple && b->hit_count == 0)
fb40c209 3790 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 3791
0d381245 3792 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
3793 {
3794 annotate_field (8);
8b93c638
JM
3795 ui_out_text (uiout, "\tignore next ");
3796 ui_out_field_int (uiout, "ignore", b->ignore_count);
3797 ui_out_text (uiout, " hits\n");
c4093a6a 3798 }
059fb39f
PM
3799
3800 l = b->commands;
3801 if (!part_of_multiple && l)
c4093a6a 3802 {
3b31d625
EZ
3803 struct cleanup *script_chain;
3804
c4093a6a 3805 annotate_field (9);
3b31d625 3806 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 3807 print_command_lines (uiout, l, 4);
3b31d625 3808 do_cleanups (script_chain);
c4093a6a 3809 }
d24317b4 3810
1042e4c0
SS
3811 if (!part_of_multiple && b->pass_count)
3812 {
3813 annotate_field (10);
3814 ui_out_text (uiout, "\tpass count ");
3815 ui_out_field_int (uiout, "pass", b->pass_count);
3816 ui_out_text (uiout, " \n");
3817 }
3818
3819 if (!part_of_multiple && b->step_count)
3820 {
3821 annotate_field (11);
3822 ui_out_text (uiout, "\tstep count ");
3823 ui_out_field_int (uiout, "step", b->step_count);
3824 ui_out_text (uiout, " \n");
3825 }
3826
3827 if (!part_of_multiple && b->actions)
3828 {
3829 struct action_line *action;
3830 annotate_field (12);
3831 for (action = b->actions; action; action = action->next)
3832 {
3833 ui_out_text (uiout, " A\t");
3834 ui_out_text (uiout, action->action);
3835 ui_out_text (uiout, "\n");
3836 }
3837 }
3838
d24317b4
VP
3839 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
3840 {
3841 if (b->addr_string)
3842 ui_out_field_string (uiout, "original-location", b->addr_string);
3843 else if (b->exp_string)
3844 ui_out_field_string (uiout, "original-location", b->exp_string);
3845 }
3846
3b31d625 3847 do_cleanups (bkpt_chain);
8b93c638 3848 do_cleanups (old_chain);
c4093a6a 3849}
c5aa993b 3850
0d381245
VP
3851static void
3852print_one_breakpoint (struct breakpoint *b,
3853 CORE_ADDR *last_addr)
3854{
3855 print_one_breakpoint_location (b, NULL, 0, last_addr);
3856
3857 /* If this breakpoint has custom print function,
3858 it's already printed. Otherwise, print individual
3859 locations, if any. */
3860 if (b->ops == NULL || b->ops->print_one == NULL)
3861 {
3862 /* If breakpoint has a single location that is
3863 disabled, we print it as if it had
3864 several locations, since otherwise it's hard to
3865 represent "breakpoint enabled, location disabled"
a5606eee
VP
3866 situation.
3867 Note that while hardware watchpoints have
3868 several locations internally, that's no a property
3869 exposed to user. */
0d381245 3870 if (b->loc
a5606eee 3871 && !is_hardware_watchpoint (b)
0d381245 3872 && (b->loc->next || !b->loc->enabled)
a5606eee 3873 && !ui_out_is_mi_like_p (uiout))
0d381245
VP
3874 {
3875 struct bp_location *loc;
3876 int n = 1;
3877 for (loc = b->loc; loc; loc = loc->next, ++n)
3878 print_one_breakpoint_location (b, loc, n, last_addr);
3879 }
3880 }
3881}
3882
3883
c4093a6a
JM
3884struct captured_breakpoint_query_args
3885 {
3886 int bnum;
3887 };
c5aa993b 3888
c4093a6a 3889static int
2b65245e 3890do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
3891{
3892 struct captured_breakpoint_query_args *args = data;
52f0bd74 3893 struct breakpoint *b;
c4093a6a
JM
3894 CORE_ADDR dummy_addr = 0;
3895 ALL_BREAKPOINTS (b)
3896 {
3897 if (args->bnum == b->number)
c5aa993b 3898 {
c4093a6a
JM
3899 print_one_breakpoint (b, &dummy_addr);
3900 return GDB_RC_OK;
c5aa993b 3901 }
c4093a6a
JM
3902 }
3903 return GDB_RC_NONE;
3904}
c5aa993b 3905
c4093a6a 3906enum gdb_rc
ce43223b 3907gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
c4093a6a
JM
3908{
3909 struct captured_breakpoint_query_args args;
3910 args.bnum = bnum;
3911 /* For the moment we don't trust print_one_breakpoint() to not throw
3912 an error. */
b0b13bb4
DJ
3913 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
3914 error_message, RETURN_MASK_ALL) < 0)
3915 return GDB_RC_FAIL;
3916 else
3917 return GDB_RC_OK;
c4093a6a 3918}
c5aa993b 3919
7f3b0473
AC
3920/* Return non-zero if B is user settable (breakpoints, watchpoints,
3921 catchpoints, et.al.). */
3922
3923static int
3924user_settable_breakpoint (const struct breakpoint *b)
3925{
3926 return (b->type == bp_breakpoint
ce78b96d 3927 || b->type == bp_catchpoint
7f3b0473 3928 || b->type == bp_hardware_breakpoint
1042e4c0 3929 || b->type == bp_tracepoint
7f3b0473
AC
3930 || b->type == bp_watchpoint
3931 || b->type == bp_read_watchpoint
3932 || b->type == bp_access_watchpoint
3933 || b->type == bp_hardware_watchpoint);
3934}
3935
3936/* Print information on user settable breakpoint (watchpoint, etc)
3937 number BNUM. If BNUM is -1 print all user settable breakpoints.
3938 If ALLFLAG is non-zero, include non- user settable breakpoints. */
c906108c 3939
c4093a6a 3940static void
fba45db2 3941breakpoint_1 (int bnum, int allflag)
c4093a6a 3942{
52f0bd74 3943 struct breakpoint *b;
c4093a6a 3944 CORE_ADDR last_addr = (CORE_ADDR) -1;
7f3b0473 3945 int nr_printable_breakpoints;
3b31d625 3946 struct cleanup *bkpttbl_chain;
79a45b7d 3947 struct value_print_options opts;
c4093a6a 3948
79a45b7d
TT
3949 get_user_print_options (&opts);
3950
7f3b0473
AC
3951 /* Compute the number of rows in the table. */
3952 nr_printable_breakpoints = 0;
3953 ALL_BREAKPOINTS (b)
3954 if (bnum == -1
3955 || bnum == b->number)
3956 {
3957 if (allflag || user_settable_breakpoint (b))
3958 nr_printable_breakpoints++;
3959 }
3960
79a45b7d 3961 if (opts.addressprint)
3b31d625
EZ
3962 bkpttbl_chain
3963 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
3964 "BreakpointTable");
8b93c638 3965 else
3b31d625
EZ
3966 bkpttbl_chain
3967 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
3968 "BreakpointTable");
8b93c638 3969
7f3b0473 3970 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
3971 annotate_breakpoints_headers ();
3972 if (nr_printable_breakpoints > 0)
3973 annotate_field (0);
0d381245 3974 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
3975 if (nr_printable_breakpoints > 0)
3976 annotate_field (1);
3977 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
3978 if (nr_printable_breakpoints > 0)
3979 annotate_field (2);
3980 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
3981 if (nr_printable_breakpoints > 0)
3982 annotate_field (3);
54e52265 3983 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 3984 if (opts.addressprint)
7f3b0473 3985 {
d7faa9e7
AC
3986 if (nr_printable_breakpoints > 0)
3987 annotate_field (4);
17a912b6 3988 if (gdbarch_addr_bit (current_gdbarch) <= 32)
b25959ec 3989 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
7f3b0473 3990 else
b25959ec 3991 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
7f3b0473 3992 }
d7faa9e7
AC
3993 if (nr_printable_breakpoints > 0)
3994 annotate_field (5);
3995 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
3996 ui_out_table_body (uiout);
3997 if (nr_printable_breakpoints > 0)
3998 annotate_breakpoints_table ();
7f3b0473 3999
c4093a6a
JM
4000 ALL_BREAKPOINTS (b)
4001 if (bnum == -1
4002 || bnum == b->number)
4003 {
4004 /* We only print out user settable breakpoints unless the
4005 allflag is set. */
7f3b0473
AC
4006 if (allflag || user_settable_breakpoint (b))
4007 print_one_breakpoint (b, &last_addr);
c4093a6a
JM
4008 }
4009
3b31d625 4010 do_cleanups (bkpttbl_chain);
698384cd 4011
7f3b0473 4012 if (nr_printable_breakpoints == 0)
c906108c 4013 {
8b93c638
JM
4014 if (bnum == -1)
4015 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
4016 else
4017 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
4018 bnum);
c906108c
SS
4019 }
4020 else
c4093a6a
JM
4021 {
4022 /* Compare against (CORE_ADDR)-1 in case some compiler decides
4023 that a comparison of an unsigned with -1 is always false. */
d1aa2f50 4024 if (last_addr != (CORE_ADDR) -1 && !server_command)
8b9b9e1a 4025 set_next_address (current_gdbarch, last_addr);
c4093a6a 4026 }
c906108c 4027
c4093a6a
JM
4028 /* FIXME? Should this be moved up so that it is only called when
4029 there have been breakpoints? */
c906108c
SS
4030 annotate_breakpoints_table_end ();
4031}
4032
c906108c 4033static void
fba45db2 4034breakpoints_info (char *bnum_exp, int from_tty)
c906108c
SS
4035{
4036 int bnum = -1;
4037
4038 if (bnum_exp)
bb518678 4039 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4040
4041 breakpoint_1 (bnum, 0);
4042}
4043
7a292a7a 4044static void
fba45db2 4045maintenance_info_breakpoints (char *bnum_exp, int from_tty)
c906108c
SS
4046{
4047 int bnum = -1;
4048
4049 if (bnum_exp)
bb518678 4050 bnum = parse_and_eval_long (bnum_exp);
c906108c
SS
4051
4052 breakpoint_1 (bnum, 1);
4053}
4054
0d381245 4055static int
714835d5
UW
4056breakpoint_has_pc (struct breakpoint *b,
4057 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
4058{
4059 struct bp_location *bl = b->loc;
4060 for (; bl; bl = bl->next)
4061 {
4062 if (bl->address == pc
4063 && (!overlay_debugging || bl->section == section))
4064 return 1;
4065 }
4066 return 0;
4067}
4068
c906108c
SS
4069/* Print a message describing any breakpoints set at PC. */
4070
4071static void
714835d5
UW
4072describe_other_breakpoints (CORE_ADDR pc, struct obj_section *section,
4073 int thread)
c906108c 4074{
52f0bd74
AC
4075 int others = 0;
4076 struct breakpoint *b;
c906108c
SS
4077
4078 ALL_BREAKPOINTS (b)
0d381245 4079 others += breakpoint_has_pc (b, pc, section);
c906108c
SS
4080 if (others > 0)
4081 {
a3f17187
AC
4082 if (others == 1)
4083 printf_filtered (_("Note: breakpoint "));
4084 else /* if (others == ???) */
4085 printf_filtered (_("Note: breakpoints "));
c906108c 4086 ALL_BREAKPOINTS (b)
0d381245
VP
4087 if (breakpoint_has_pc (b, pc, section))
4088 {
4089 others--;
4090 printf_filtered ("%d", b->number);
4091 if (b->thread == -1 && thread != -1)
4092 printf_filtered (" (all threads)");
4093 else if (b->thread != -1)
4094 printf_filtered (" (thread %d)", b->thread);
4095 printf_filtered ("%s%s ",
059fb39f
PM
4096 ((b->enable_state == bp_disabled
4097 || b->enable_state == bp_call_disabled)
0d381245
VP
4098 ? " (disabled)"
4099 : b->enable_state == bp_permanent
4100 ? " (permanent)"
4101 : ""),
4102 (others > 1) ? ","
4103 : ((others == 1) ? " and" : ""));
4104 }
a3f17187 4105 printf_filtered (_("also set at pc "));
ed49a04f 4106 fputs_filtered (paddress (pc), gdb_stdout);
c906108c
SS
4107 printf_filtered (".\n");
4108 }
4109}
4110\f
4111/* Set the default place to put a breakpoint
4112 for the `break' command with no arguments. */
4113
4114void
fba45db2
KB
4115set_default_breakpoint (int valid, CORE_ADDR addr, struct symtab *symtab,
4116 int line)
c906108c
SS
4117{
4118 default_breakpoint_valid = valid;
4119 default_breakpoint_address = addr;
4120 default_breakpoint_symtab = symtab;
4121 default_breakpoint_line = line;
4122}
4123
e4f237da
KB
4124/* Return true iff it is meaningful to use the address member of
4125 BPT. For some breakpoint types, the address member is irrelevant
4126 and it makes no sense to attempt to compare it to other addresses
4127 (or use it for any other purpose either).
4128
4129 More specifically, each of the following breakpoint types will always
4130 have a zero valued address and we don't want check_duplicates() to mark
4131 breakpoints of any of these types to be a duplicate of an actual
4132 breakpoint at address zero:
4133
4134 bp_watchpoint
4135 bp_hardware_watchpoint
4136 bp_read_watchpoint
4137 bp_access_watchpoint
fe798b75 4138 bp_catchpoint */
e4f237da
KB
4139
4140static int
4141breakpoint_address_is_meaningful (struct breakpoint *bpt)
4142{
4143 enum bptype type = bpt->type;
4144
4145 return (type != bp_watchpoint
4146 && type != bp_hardware_watchpoint
4147 && type != bp_read_watchpoint
4148 && type != bp_access_watchpoint
fe798b75 4149 && type != bp_catchpoint);
e4f237da
KB
4150}
4151
9f60f21b 4152/* Rescan breakpoints at the same address and section as BPT,
c906108c 4153 marking the first one as "first" and any others as "duplicates".
c2c6d25f 4154 This is so that the bpt instruction is only inserted once.
9f60f21b
JB
4155 If we have a permanent breakpoint at the same place as BPT, make
4156 that one the official one, and the rest as duplicates. */
c906108c
SS
4157
4158static void
714835d5 4159check_duplicates_for (CORE_ADDR address, struct obj_section *section)
c906108c 4160{
075f6582 4161 struct bp_location *b;
52f0bd74 4162 int count = 0;
075f6582 4163 struct bp_location *perm_bp = 0;
c906108c 4164
075f6582
DJ
4165 ALL_BP_LOCATIONS (b)
4166 if (b->owner->enable_state != bp_disabled
075f6582 4167 && b->owner->enable_state != bp_call_disabled
0d381245
VP
4168 && b->enabled
4169 && !b->shlib_disabled
075f6582
DJ
4170 && b->address == address /* address / overlay match */
4171 && (!overlay_debugging || b->section == section)
4172 && breakpoint_address_is_meaningful (b->owner))
c5aa993b 4173 {
c2c6d25f 4174 /* Have we found a permanent breakpoint? */
075f6582 4175 if (b->owner->enable_state == bp_permanent)
c2c6d25f
JM
4176 {
4177 perm_bp = b;
4178 break;
4179 }
4180
c5aa993b 4181 count++;
075f6582 4182 b->duplicate = count > 1;
c5aa993b 4183 }
c2c6d25f
JM
4184
4185 /* If we found a permanent breakpoint at this address, go over the
aae64587
PA
4186 list again and declare all the other breakpoints there (except
4187 other permanent breakpoints) to be the duplicates. */
c2c6d25f
JM
4188 if (perm_bp)
4189 {
075f6582 4190 perm_bp->duplicate = 0;
c2c6d25f
JM
4191
4192 /* Permanent breakpoint should always be inserted. */
075f6582 4193 if (! perm_bp->inserted)
8e65ff28 4194 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
4195 _("allegedly permanent breakpoint is not "
4196 "actually inserted"));
c2c6d25f 4197
075f6582 4198 ALL_BP_LOCATIONS (b)
c2c6d25f
JM
4199 if (b != perm_bp)
4200 {
aae64587
PA
4201 if (b->owner->enable_state != bp_permanent
4202 && b->owner->enable_state != bp_disabled
075f6582 4203 && b->owner->enable_state != bp_call_disabled
0d381245 4204 && b->enabled && !b->shlib_disabled
075f6582
DJ
4205 && b->address == address /* address / overlay match */
4206 && (!overlay_debugging || b->section == section)
4207 && breakpoint_address_is_meaningful (b->owner))
4208 {
4209 if (b->inserted)
4210 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
4211 _("another breakpoint was inserted on top of "
4212 "a permanent breakpoint"));
075f6582
DJ
4213
4214 b->duplicate = 1;
4215 }
c2c6d25f
JM
4216 }
4217 }
c906108c
SS
4218}
4219
0d381245
VP
4220static void
4221check_duplicates (struct breakpoint *bpt)
4222{
4223 struct bp_location *bl = bpt->loc;
4224
4225 if (! breakpoint_address_is_meaningful (bpt))
4226 return;
4227
4228 for (; bl; bl = bl->next)
4229 check_duplicates_for (bl->address, bl->section);
4230}
4231
76897487
KB
4232static void
4233breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4234 int bnum, int have_bnum)
4235{
4236 char astr1[40];
4237 char astr2[40];
4238
bb599908
PH
4239 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4240 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 4241 if (have_bnum)
8a3fe4f8 4242 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
4243 bnum, astr1, astr2);
4244 else
8a3fe4f8 4245 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
4246}
4247
4248/* Adjust a breakpoint's address to account for architectural constraints
4249 on breakpoint placement. Return the adjusted address. Note: Very
4250 few targets require this kind of adjustment. For most targets,
4251 this function is simply the identity function. */
4252
4253static CORE_ADDR
88f7da05 4254adjust_breakpoint_address (CORE_ADDR bpaddr, enum bptype bptype)
76897487
KB
4255{
4256 if (!gdbarch_adjust_breakpoint_address_p (current_gdbarch))
4257 {
4258 /* Very few targets need any kind of breakpoint adjustment. */
4259 return bpaddr;
4260 }
88f7da05
KB
4261 else if (bptype == bp_watchpoint
4262 || bptype == bp_hardware_watchpoint
4263 || bptype == bp_read_watchpoint
4264 || bptype == bp_access_watchpoint
fe798b75 4265 || bptype == bp_catchpoint)
88f7da05
KB
4266 {
4267 /* Watchpoints and the various bp_catch_* eventpoints should not
4268 have their addresses modified. */
4269 return bpaddr;
4270 }
76897487
KB
4271 else
4272 {
4273 CORE_ADDR adjusted_bpaddr;
4274
4275 /* Some targets have architectural constraints on the placement
4276 of breakpoint instructions. Obtain the adjusted address. */
4277 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (current_gdbarch,
4278 bpaddr);
4279
4280 /* An adjusted breakpoint address can significantly alter
4281 a user's expectations. Print a warning if an adjustment
4282 is required. */
4283 if (adjusted_bpaddr != bpaddr)
4284 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4285
4286 return adjusted_bpaddr;
4287 }
4288}
4289
7cc221ef
DJ
4290/* Allocate a struct bp_location. */
4291
26bb91f3 4292static struct bp_location *
39d61571 4293allocate_bp_location (struct breakpoint *bpt)
7cc221ef
DJ
4294{
4295 struct bp_location *loc, *loc_p;
4296
4297 loc = xmalloc (sizeof (struct bp_location));
4298 memset (loc, 0, sizeof (*loc));
4299
e049a4b5 4300 loc->owner = bpt;
511a6cd4 4301 loc->cond = NULL;
0d381245
VP
4302 loc->shlib_disabled = 0;
4303 loc->enabled = 1;
e049a4b5 4304
39d61571 4305 switch (bpt->type)
e049a4b5
DJ
4306 {
4307 case bp_breakpoint:
1042e4c0 4308 case bp_tracepoint:
e049a4b5
DJ
4309 case bp_until:
4310 case bp_finish:
4311 case bp_longjmp:
4312 case bp_longjmp_resume:
4313 case bp_step_resume:
e049a4b5
DJ
4314 case bp_watchpoint_scope:
4315 case bp_call_dummy:
4316 case bp_shlib_event:
4317 case bp_thread_event:
4318 case bp_overlay_event:
0fd8e87f 4319 case bp_longjmp_master:
e049a4b5
DJ
4320 loc->loc_type = bp_loc_software_breakpoint;
4321 break;
4322 case bp_hardware_breakpoint:
4323 loc->loc_type = bp_loc_hardware_breakpoint;
4324 break;
4325 case bp_hardware_watchpoint:
4326 case bp_read_watchpoint:
4327 case bp_access_watchpoint:
4328 loc->loc_type = bp_loc_hardware_watchpoint;
4329 break;
4330 case bp_watchpoint:
ce78b96d 4331 case bp_catchpoint:
e049a4b5
DJ
4332 loc->loc_type = bp_loc_other;
4333 break;
4334 default:
e2e0b3e5 4335 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
4336 }
4337
7cc221ef
DJ
4338 return loc;
4339}
4340
fe3f5fa8
VP
4341static void free_bp_location (struct bp_location *loc)
4342{
4343 if (loc->cond)
4344 xfree (loc->cond);
74960c60
VP
4345
4346 if (loc->function_name)
4347 xfree (loc->function_name);
4348
fe3f5fa8
VP
4349 xfree (loc);
4350}
4351
0d381245
VP
4352/* Helper to set_raw_breakpoint below. Creates a breakpoint
4353 that has type BPTYPE and has no locations as yet. */
c906108c 4354
c40e75cd 4355static struct breakpoint *
0d381245 4356set_raw_breakpoint_without_location (enum bptype bptype)
c906108c 4357{
52f0bd74 4358 struct breakpoint *b, *b1;
c906108c
SS
4359
4360 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4361 memset (b, 0, sizeof (*b));
2219d63c 4362
4d28f7a8 4363 b->type = bptype;
c906108c
SS
4364 b->language = current_language->la_language;
4365 b->input_radix = input_radix;
4366 b->thread = -1;
b5de0fa7 4367 b->enable_state = bp_enabled;
c906108c
SS
4368 b->next = 0;
4369 b->silent = 0;
4370 b->ignore_count = 0;
4371 b->commands = NULL;
818dd999 4372 b->frame_id = null_frame_id;
3a3e9ee3 4373 b->forked_inferior_pid = null_ptid;
c906108c 4374 b->exec_pathname = NULL;
3086aeae 4375 b->ops = NULL;
0d381245 4376 b->condition_not_parsed = 0;
c906108c
SS
4377
4378 /* Add this breakpoint to the end of the chain
4379 so that a list of breakpoints will come out in order
4380 of increasing numbers. */
4381
4382 b1 = breakpoint_chain;
4383 if (b1 == 0)
4384 breakpoint_chain = b;
4385 else
4386 {
4387 while (b1->next)
4388 b1 = b1->next;
4389 b1->next = b;
4390 }
0d381245
VP
4391 return b;
4392}
4393
4394/* Initialize loc->function_name. */
4395static void
4396set_breakpoint_location_function (struct bp_location *loc)
4397{
4398 if (loc->owner->type == bp_breakpoint
1042e4c0
SS
4399 || loc->owner->type == bp_hardware_breakpoint
4400 || loc->owner->type == bp_tracepoint)
0d381245
VP
4401 {
4402 find_pc_partial_function (loc->address, &(loc->function_name),
4403 NULL, NULL);
4404 if (loc->function_name)
4405 loc->function_name = xstrdup (loc->function_name);
4406 }
4407}
4408
4409/* set_raw_breakpoint is a low level routine for allocating and
4410 partially initializing a breakpoint of type BPTYPE. The newly
4411 created breakpoint's address, section, source file name, and line
4412 number are provided by SAL. The newly created and partially
4413 initialized breakpoint is added to the breakpoint chain and
4414 is also returned as the value of this function.
4415
4416 It is expected that the caller will complete the initialization of
4417 the newly created breakpoint struct as well as output any status
4418 information regarding the creation of a new breakpoint. In
4419 particular, set_raw_breakpoint does NOT set the breakpoint
4420 number! Care should be taken to not allow an error to occur
4421 prior to completing the initialization of the breakpoint. If this
4422 should happen, a bogus breakpoint will be left on the chain. */
4423
4424struct breakpoint *
4425set_raw_breakpoint (struct symtab_and_line sal, enum bptype bptype)
4426{
4427 struct breakpoint *b = set_raw_breakpoint_without_location (bptype);
4428 CORE_ADDR adjusted_address;
4429
4430 /* Adjust the breakpoint's address prior to allocating a location.
4431 Once we call allocate_bp_location(), that mostly uninitialized
4432 location will be placed on the location chain. Adjustment of the
8defab1a 4433 breakpoint may cause target_read_memory() to be called and we do
0d381245
VP
4434 not want its scan of the location chain to find a breakpoint and
4435 location that's only been partially initialized. */
39d61571 4436 adjusted_address = adjust_breakpoint_address (sal.pc, b->type);
0d381245 4437
39d61571 4438 b->loc = allocate_bp_location (b);
0d381245
VP
4439 b->loc->requested_address = sal.pc;
4440 b->loc->address = adjusted_address;
4441
4442 if (sal.symtab == NULL)
4443 b->source_file = NULL;
4444 else
1b36a34b 4445 b->source_file = xstrdup (sal.symtab->filename);
0d381245
VP
4446 b->loc->section = sal.section;
4447 b->line_number = sal.line;
4448
4449 set_breakpoint_location_function (b->loc);
c906108c 4450
c906108c
SS
4451 breakpoints_changed ();
4452
4453 return b;
4454}
4455
c2c6d25f
JM
4456
4457/* Note that the breakpoint object B describes a permanent breakpoint
4458 instruction, hard-wired into the inferior's code. */
4459void
4460make_breakpoint_permanent (struct breakpoint *b)
4461{
0d381245 4462 struct bp_location *bl;
b5de0fa7 4463 b->enable_state = bp_permanent;
c2c6d25f 4464
0d381245
VP
4465 /* By definition, permanent breakpoints are already present in the code.
4466 Mark all locations as inserted. For now, make_breakpoint_permanent
4467 is called in just one place, so it's hard to say if it's reasonable
4468 to have permanent breakpoint with multiple locations or not,
4469 but it's easy to implmement. */
4470 for (bl = b->loc; bl; bl = bl->next)
4471 bl->inserted = 1;
c2c6d25f
JM
4472}
4473
53a5351d 4474/* Call this routine when stepping and nexting to enable a breakpoint
0fd8e87f 4475 if we do a longjmp() in THREAD. When we hit that breakpoint, call
c906108c
SS
4476 set_longjmp_resume_breakpoint() to figure out where we are going. */
4477
4478void
0fd8e87f 4479set_longjmp_breakpoint (int thread)
c906108c 4480{
0fd8e87f
UW
4481 struct breakpoint *b, *temp;
4482
4483 /* To avoid having to rescan all objfile symbols at every step,
4484 we maintain a list of continually-inserted but always disabled
4485 longjmp "master" breakpoints. Here, we simply create momentary
4486 clones of those and enable them for the requested thread. */
4487 ALL_BREAKPOINTS_SAFE (b, temp)
4488 if (b->type == bp_longjmp_master)
4489 {
4490 struct breakpoint *clone = clone_momentary_breakpoint (b);
4491 clone->type = bp_longjmp;
4492 clone->thread = thread;
4493 }
c906108c
SS
4494}
4495
611c83ae 4496/* Delete all longjmp breakpoints from THREAD. */
c906108c 4497void
611c83ae 4498delete_longjmp_breakpoint (int thread)
c906108c 4499{
611c83ae 4500 struct breakpoint *b, *temp;
c906108c 4501
611c83ae
PA
4502 ALL_BREAKPOINTS_SAFE (b, temp)
4503 if (b->type == bp_longjmp)
4504 {
4505 if (b->thread == thread)
4506 delete_breakpoint (b);
4507 }
c906108c
SS
4508}
4509
1900040c
MS
4510void
4511enable_overlay_breakpoints (void)
4512{
52f0bd74 4513 struct breakpoint *b;
1900040c
MS
4514
4515 ALL_BREAKPOINTS (b)
4516 if (b->type == bp_overlay_event)
4517 {
4518 b->enable_state = bp_enabled;
b60e7edf 4519 update_global_location_list (1);
c02f5703 4520 overlay_events_enabled = 1;
1900040c
MS
4521 }
4522}
4523
4524void
4525disable_overlay_breakpoints (void)
4526{
52f0bd74 4527 struct breakpoint *b;
1900040c
MS
4528
4529 ALL_BREAKPOINTS (b)
4530 if (b->type == bp_overlay_event)
4531 {
4532 b->enable_state = bp_disabled;
b60e7edf 4533 update_global_location_list (0);
c02f5703 4534 overlay_events_enabled = 0;
1900040c
MS
4535 }
4536}
4537
c4093a6a 4538struct breakpoint *
fba45db2 4539create_thread_event_breakpoint (CORE_ADDR address)
c4093a6a
JM
4540{
4541 struct breakpoint *b;
c4093a6a 4542
1900040c 4543 b = create_internal_breakpoint (address, bp_thread_event);
c4093a6a 4544
b5de0fa7 4545 b->enable_state = bp_enabled;
c4093a6a 4546 /* addr_string has to be used or breakpoint_re_set will delete me. */
b435e160 4547 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
c4093a6a 4548
b60e7edf 4549 update_global_location_list_nothrow (1);
74960c60 4550
c4093a6a
JM
4551 return b;
4552}
4553
4554void
4555remove_thread_event_breakpoints (void)
4556{
4557 struct breakpoint *b, *temp;
4558
4559 ALL_BREAKPOINTS_SAFE (b, temp)
4560 if (b->type == bp_thread_event)
4561 delete_breakpoint (b);
4562}
4563
0101ce28
JJ
4564struct captured_parse_breakpoint_args
4565 {
4566 char **arg_p;
4567 struct symtabs_and_lines *sals_p;
4568 char ***addr_string_p;
4569 int *not_found_ptr;
4570 };
4571
4572struct lang_and_radix
4573 {
4574 enum language lang;
4575 int radix;
4576 };
4577
0101ce28 4578
cae688ec
JJ
4579void
4580remove_solib_event_breakpoints (void)
4581{
4582 struct breakpoint *b, *temp;
4583
4584 ALL_BREAKPOINTS_SAFE (b, temp)
4585 if (b->type == bp_shlib_event)
4586 delete_breakpoint (b);
4587}
4588
4589struct breakpoint *
4590create_solib_event_breakpoint (CORE_ADDR address)
4591{
4592 struct breakpoint *b;
4593
4594 b = create_internal_breakpoint (address, bp_shlib_event);
b60e7edf 4595 update_global_location_list_nothrow (1);
cae688ec
JJ
4596 return b;
4597}
4598
4599/* Disable any breakpoints that are on code in shared libraries. Only
4600 apply to enabled breakpoints, disabled ones can just stay disabled. */
4601
4602void
cb851954 4603disable_breakpoints_in_shlibs (void)
cae688ec 4604{
0d381245 4605 struct bp_location *loc;
cae688ec 4606
0d381245 4607 ALL_BP_LOCATIONS (loc)
cae688ec 4608 {
0d381245
VP
4609 struct breakpoint *b = loc->owner;
4610 /* We apply the check to all breakpoints, including disabled
4611 for those with loc->duplicate set. This is so that when breakpoint
4612 becomes enabled, or the duplicate is removed, gdb will try to insert
4613 all breakpoints. If we don't set shlib_disabled here, we'll try
4614 to insert those breakpoints and fail. */
1042e4c0
SS
4615 if (((b->type == bp_breakpoint)
4616 || (b->type == bp_hardware_breakpoint)
4617 || (b->type == bp_tracepoint))
0d381245 4618 && !loc->shlib_disabled
a77053c2 4619#ifdef PC_SOLIB
0d381245 4620 && PC_SOLIB (loc->address)
a77053c2 4621#else
f5c9a895 4622 && solib_name_from_address (loc->address)
a77053c2
MK
4623#endif
4624 )
0d381245
VP
4625 {
4626 loc->shlib_disabled = 1;
4627 }
cae688ec
JJ
4628 }
4629}
4630
84acb35a
JJ
4631/* Disable any breakpoints that are in in an unloaded shared library. Only
4632 apply to enabled breakpoints, disabled ones can just stay disabled. */
4633
75149521 4634static void
84acb35a
JJ
4635disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
4636{
0d381245 4637 struct bp_location *loc;
84acb35a
JJ
4638 int disabled_shlib_breaks = 0;
4639
c86cf029
VP
4640 /* SunOS a.out shared libraries are always mapped, so do not
4641 disable breakpoints; they will only be reported as unloaded
4642 through clear_solib when GDB discards its shared library
4643 list. See clear_solib for more information. */
4644 if (exec_bfd != NULL
4645 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
4646 return;
4647
0d381245 4648 ALL_BP_LOCATIONS (loc)
84acb35a 4649 {
0d381245
VP
4650 struct breakpoint *b = loc->owner;
4651 if ((loc->loc_type == bp_loc_hardware_breakpoint
4652 || loc->loc_type == bp_loc_software_breakpoint)
e2dd7057
PP
4653 && !loc->shlib_disabled
4654 && (b->type == bp_breakpoint || b->type == bp_hardware_breakpoint)
4655 && solib_contains_address_p (solib, loc->address))
84acb35a 4656 {
e2dd7057
PP
4657 loc->shlib_disabled = 1;
4658 /* At this point, we cannot rely on remove_breakpoint
4659 succeeding so we must mark the breakpoint as not inserted
4660 to prevent future errors occurring in remove_breakpoints. */
4661 loc->inserted = 0;
4662 if (!disabled_shlib_breaks)
4663 {
4664 target_terminal_ours_for_output ();
4665 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
4666 solib->so_name);
84acb35a 4667 }
e2dd7057 4668 disabled_shlib_breaks = 1;
84acb35a
JJ
4669 }
4670 }
84acb35a
JJ
4671}
4672
ce78b96d
JB
4673/* FORK & VFORK catchpoints. */
4674
4675/* Implement the "insert" breakpoint_ops method for fork catchpoints. */
4676
c906108c 4677static void
ce78b96d
JB
4678insert_catch_fork (struct breakpoint *b)
4679{
4680 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
4681}
4682
4683/* Implement the "remove" breakpoint_ops method for fork catchpoints. */
4684
4685static int
4686remove_catch_fork (struct breakpoint *b)
4687{
4688 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
4689}
4690
4691/* Implement the "breakpoint_hit" breakpoint_ops method for fork
4692 catchpoints. */
4693
4694static int
4695breakpoint_hit_catch_fork (struct breakpoint *b)
4696{
4697 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
4698}
4699
4700/* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
4701
4702static enum print_stop_action
4703print_it_catch_fork (struct breakpoint *b)
4704{
4705 annotate_catchpoint (b->number);
4706 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
4707 b->number, ptid_get_pid (b->forked_inferior_pid));
4708 return PRINT_SRC_AND_LOC;
4709}
4710
4711/* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
4712
4713static void
4714print_one_catch_fork (struct breakpoint *b, CORE_ADDR *last_addr)
4715{
79a45b7d
TT
4716 struct value_print_options opts;
4717
4718 get_user_print_options (&opts);
4719
ce78b96d
JB
4720 /* Field 4, the address, is omitted (which makes the columns
4721 not line up too nicely with the headers, but the effect
4722 is relatively readable). */
79a45b7d 4723 if (opts.addressprint)
ce78b96d
JB
4724 ui_out_field_skip (uiout, "addr");
4725 annotate_field (5);
4726 ui_out_text (uiout, "fork");
4727 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4728 {
4729 ui_out_text (uiout, ", process ");
4730 ui_out_field_int (uiout, "what",
4731 ptid_get_pid (b->forked_inferior_pid));
4732 ui_out_spaces (uiout, 1);
4733 }
4734}
4735
4736/* Implement the "print_mention" breakpoint_ops method for fork
4737 catchpoints. */
4738
4739static void
4740print_mention_catch_fork (struct breakpoint *b)
4741{
4742 printf_filtered (_("Catchpoint %d (fork)"), b->number);
4743}
4744
4745/* The breakpoint_ops structure to be used in fork catchpoints. */
4746
4747static struct breakpoint_ops catch_fork_breakpoint_ops =
4748{
4749 insert_catch_fork,
4750 remove_catch_fork,
4751 breakpoint_hit_catch_fork,
4752 print_it_catch_fork,
4753 print_one_catch_fork,
4754 print_mention_catch_fork
4755};
4756
4757/* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
4758
4759static void
4760insert_catch_vfork (struct breakpoint *b)
4761{
4762 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
4763}
4764
4765/* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
4766
4767static int
4768remove_catch_vfork (struct breakpoint *b)
4769{
4770 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
4771}
4772
4773/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
4774 catchpoints. */
4775
4776static int
4777breakpoint_hit_catch_vfork (struct breakpoint *b)
4778{
4779 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
4780}
4781
4782/* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
4783
4784static enum print_stop_action
4785print_it_catch_vfork (struct breakpoint *b)
4786{
4787 annotate_catchpoint (b->number);
4788 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
4789 b->number, ptid_get_pid (b->forked_inferior_pid));
4790 return PRINT_SRC_AND_LOC;
4791}
4792
4793/* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
4794
4795static void
4796print_one_catch_vfork (struct breakpoint *b, CORE_ADDR *last_addr)
4797{
79a45b7d
TT
4798 struct value_print_options opts;
4799
4800 get_user_print_options (&opts);
ce78b96d
JB
4801 /* Field 4, the address, is omitted (which makes the columns
4802 not line up too nicely with the headers, but the effect
4803 is relatively readable). */
79a45b7d 4804 if (opts.addressprint)
ce78b96d
JB
4805 ui_out_field_skip (uiout, "addr");
4806 annotate_field (5);
4807 ui_out_text (uiout, "vfork");
4808 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
4809 {
4810 ui_out_text (uiout, ", process ");
4811 ui_out_field_int (uiout, "what",
4812 ptid_get_pid (b->forked_inferior_pid));
4813 ui_out_spaces (uiout, 1);
4814 }
4815}
4816
4817/* Implement the "print_mention" breakpoint_ops method for vfork
4818 catchpoints. */
4819
4820static void
4821print_mention_catch_vfork (struct breakpoint *b)
4822{
4823 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
4824}
4825
4826/* The breakpoint_ops structure to be used in vfork catchpoints. */
4827
4828static struct breakpoint_ops catch_vfork_breakpoint_ops =
4829{
4830 insert_catch_vfork,
4831 remove_catch_vfork,
4832 breakpoint_hit_catch_vfork,
4833 print_it_catch_vfork,
4834 print_one_catch_vfork,
4835 print_mention_catch_vfork
4836};
4837
4838/* Create a new breakpoint of the bp_catchpoint kind and return it.
4839
4840 If TEMPFLAG is non-zero, then make the breakpoint temporary.
4841 If COND_STRING is not NULL, then store it in the breakpoint.
4842 OPS, if not NULL, is the breakpoint_ops structure associated
4843 to the catchpoint. */
4844
4845static struct breakpoint *
4846create_catchpoint (int tempflag, char *cond_string,
4847 struct breakpoint_ops *ops)
c906108c 4848{
c5aa993b
JM
4849 struct symtab_and_line sal;
4850 struct breakpoint *b;
c5aa993b 4851
fe39c653 4852 init_sal (&sal);
c906108c
SS
4853 sal.pc = 0;
4854 sal.symtab = NULL;
4855 sal.line = 0;
c5aa993b 4856
ce78b96d 4857 b = set_raw_breakpoint (sal, bp_catchpoint);
c906108c
SS
4858 set_breakpoint_count (breakpoint_count + 1);
4859 b->number = breakpoint_count;
ce78b96d 4860
1b36a34b 4861 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
ce78b96d 4862 b->thread = -1;
c906108c 4863 b->addr_string = NULL;
b5de0fa7
EZ
4864 b->enable_state = bp_enabled;
4865 b->disposition = tempflag ? disp_del : disp_donttouch;
ce78b96d 4866 b->ops = ops;
c5aa993b 4867
c906108c 4868 mention (b);
ce78b96d 4869 update_global_location_list (1);
c906108c 4870
ce78b96d 4871 return b;
c906108c 4872}
c5aa993b 4873
9b70b993 4874static void
ce78b96d
JB
4875create_fork_vfork_event_catchpoint (int tempflag, char *cond_string,
4876 struct breakpoint_ops *ops)
c906108c 4877{
ce78b96d
JB
4878 struct breakpoint *b = create_catchpoint (tempflag, cond_string, ops);
4879
4880 /* FIXME: We should put this information in a breakpoint private data
4881 area. */
4882 b->forked_inferior_pid = null_ptid;
c906108c
SS
4883}
4884
fe798b75
JB
4885/* Exec catchpoints. */
4886
9b70b993 4887static void
fe798b75 4888insert_catch_exec (struct breakpoint *b)
c906108c 4889{
fe798b75
JB
4890 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
4891}
c906108c 4892
fe798b75
JB
4893static int
4894remove_catch_exec (struct breakpoint *b)
4895{
4896 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
4897}
c906108c 4898
fe798b75
JB
4899static int
4900breakpoint_hit_catch_exec (struct breakpoint *b)
4901{
4902 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
4903}
c906108c 4904
fe798b75
JB
4905static enum print_stop_action
4906print_it_catch_exec (struct breakpoint *b)
4907{
4908 annotate_catchpoint (b->number);
4909 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
4910 b->exec_pathname);
4911 return PRINT_SRC_AND_LOC;
c906108c
SS
4912}
4913
fe798b75
JB
4914static void
4915print_one_catch_exec (struct breakpoint *b, CORE_ADDR *last_addr)
4916{
4917 struct value_print_options opts;
4918
4919 get_user_print_options (&opts);
4920
4921 /* Field 4, the address, is omitted (which makes the columns
4922 not line up too nicely with the headers, but the effect
4923 is relatively readable). */
4924 if (opts.addressprint)
4925 ui_out_field_skip (uiout, "addr");
4926 annotate_field (5);
4927 ui_out_text (uiout, "exec");
4928 if (b->exec_pathname != NULL)
4929 {
4930 ui_out_text (uiout, ", program \"");
4931 ui_out_field_string (uiout, "what", b->exec_pathname);
4932 ui_out_text (uiout, "\" ");
4933 }
4934}
4935
4936static void
4937print_mention_catch_exec (struct breakpoint *b)
4938{
4939 printf_filtered (_("Catchpoint %d (exec)"), b->number);
4940}
4941
4942static struct breakpoint_ops catch_exec_breakpoint_ops =
4943{
4944 insert_catch_exec,
4945 remove_catch_exec,
4946 breakpoint_hit_catch_exec,
4947 print_it_catch_exec,
4948 print_one_catch_exec,
4949 print_mention_catch_exec
4950};
4951
c906108c 4952static int
fba45db2 4953hw_breakpoint_used_count (void)
c906108c 4954{
52f0bd74 4955 struct breakpoint *b;
c906108c
SS
4956 int i = 0;
4957
4958 ALL_BREAKPOINTS (b)
c5aa993b 4959 {
d6b74ac4 4960 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
c5aa993b
JM
4961 i++;
4962 }
c906108c
SS
4963
4964 return i;
4965}
4966
4967static int
fba45db2 4968hw_watchpoint_used_count (enum bptype type, int *other_type_used)
c906108c 4969{
52f0bd74 4970 struct breakpoint *b;
c906108c
SS
4971 int i = 0;
4972
4973 *other_type_used = 0;
4974 ALL_BREAKPOINTS (b)
c5aa993b 4975 {
468d015d 4976 if (breakpoint_enabled (b))
c5aa993b
JM
4977 {
4978 if (b->type == type)
4979 i++;
059fb39f
PM
4980 else if ((b->type == bp_hardware_watchpoint
4981 || b->type == bp_read_watchpoint
4982 || b->type == bp_access_watchpoint))
c5aa993b
JM
4983 *other_type_used = 1;
4984 }
4985 }
c906108c
SS
4986 return i;
4987}
4988
c906108c 4989void
fba45db2 4990disable_watchpoints_before_interactive_call_start (void)
c906108c 4991{
c5aa993b 4992 struct breakpoint *b;
c906108c
SS
4993
4994 ALL_BREAKPOINTS (b)
c5aa993b
JM
4995 {
4996 if (((b->type == bp_watchpoint)
4997 || (b->type == bp_hardware_watchpoint)
4998 || (b->type == bp_read_watchpoint)
dfdfb3ca 4999 || (b->type == bp_access_watchpoint))
468d015d 5000 && breakpoint_enabled (b))
c5aa993b 5001 {
b5de0fa7 5002 b->enable_state = bp_call_disabled;
b60e7edf 5003 update_global_location_list (0);
c5aa993b
JM
5004 }
5005 }
c906108c
SS
5006}
5007
5008void
fba45db2 5009enable_watchpoints_after_interactive_call_stop (void)
c906108c 5010{
c5aa993b 5011 struct breakpoint *b;
c906108c
SS
5012
5013 ALL_BREAKPOINTS (b)
c5aa993b
JM
5014 {
5015 if (((b->type == bp_watchpoint)
5016 || (b->type == bp_hardware_watchpoint)
5017 || (b->type == bp_read_watchpoint)
dfdfb3ca 5018 || (b->type == bp_access_watchpoint))
b5de0fa7 5019 && (b->enable_state == bp_call_disabled))
c5aa993b 5020 {
b5de0fa7 5021 b->enable_state = bp_enabled;
b60e7edf 5022 update_global_location_list (1);
c5aa993b
JM
5023 }
5024 }
c906108c
SS
5025}
5026
5027
5028/* Set a breakpoint that will evaporate an end of command
5029 at address specified by SAL.
5030 Restrict it to frame FRAME if FRAME is nonzero. */
5031
5032struct breakpoint *
818dd999 5033set_momentary_breakpoint (struct symtab_and_line sal, struct frame_id frame_id,
fba45db2 5034 enum bptype type)
c906108c 5035{
52f0bd74 5036 struct breakpoint *b;
edb3359d
DJ
5037
5038 /* If FRAME_ID is valid, it should be a real frame, not an inlined
5039 one. */
5040 gdb_assert (!frame_id_inlined_p (frame_id));
5041
4d28f7a8 5042 b = set_raw_breakpoint (sal, type);
b5de0fa7
EZ
5043 b->enable_state = bp_enabled;
5044 b->disposition = disp_donttouch;
818dd999 5045 b->frame_id = frame_id;
c906108c
SS
5046
5047 /* If we're debugging a multi-threaded program, then we
5048 want momentary breakpoints to be active in only a
5049 single thread of control. */
39f77062
KB
5050 if (in_thread_list (inferior_ptid))
5051 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 5052
b60e7edf 5053 update_global_location_list_nothrow (1);
74960c60 5054
c906108c
SS
5055 return b;
5056}
611c83ae 5057
e58b0e63
PA
5058/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
5059 ORIG is NULL. */
5060
5061struct breakpoint *
5062clone_momentary_breakpoint (struct breakpoint *orig)
5063{
5064 struct breakpoint *copy;
5065
5066 /* If there's nothing to clone, then return nothing. */
5067 if (orig == NULL)
5068 return NULL;
5069
5070 copy = set_raw_breakpoint_without_location (orig->type);
5071 copy->loc = allocate_bp_location (copy);
5072 set_breakpoint_location_function (copy->loc);
5073
5074 copy->loc->requested_address = orig->loc->requested_address;
5075 copy->loc->address = orig->loc->address;
5076 copy->loc->section = orig->loc->section;
5077
5078 if (orig->source_file == NULL)
5079 copy->source_file = NULL;
5080 else
5081 copy->source_file = xstrdup (orig->source_file);
5082
5083 copy->line_number = orig->line_number;
5084 copy->frame_id = orig->frame_id;
5085 copy->thread = orig->thread;
5086
5087 copy->enable_state = bp_enabled;
5088 copy->disposition = disp_donttouch;
5089 copy->number = internal_breakpoint_number--;
5090
5091 update_global_location_list_nothrow (0);
5092 return copy;
5093}
5094
611c83ae
PA
5095struct breakpoint *
5096set_momentary_breakpoint_at_pc (CORE_ADDR pc, enum bptype type)
5097{
5098 struct symtab_and_line sal;
5099
5100 sal = find_pc_line (pc, 0);
5101 sal.pc = pc;
5102 sal.section = find_pc_overlay (pc);
5103 sal.explicit_pc = 1;
5104
5105 return set_momentary_breakpoint (sal, null_frame_id, type);
5106}
c906108c 5107\f
c5aa993b 5108
c906108c
SS
5109/* Tell the user we have just set a breakpoint B. */
5110
5111static void
fba45db2 5112mention (struct breakpoint *b)
c906108c
SS
5113{
5114 int say_where = 0;
fa8a61dc 5115 struct cleanup *ui_out_chain;
79a45b7d
TT
5116 struct value_print_options opts;
5117
5118 get_user_print_options (&opts);
8b93c638 5119
9a4105ab
AC
5120 /* FIXME: This is misplaced; mention() is called by things (like
5121 hitting a watchpoint) other than breakpoint creation. It should
5122 be possible to clean this up and at the same time replace the
7f4b89d1 5123 random calls to breakpoint_changed with this hook. */
383f836e 5124 observer_notify_breakpoint_created (b->number);
c906108c 5125
3086aeae
DJ
5126 if (b->ops != NULL && b->ops->print_mention != NULL)
5127 b->ops->print_mention (b);
5128 else
5129 switch (b->type)
5130 {
5131 case bp_none:
a3f17187 5132 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
3086aeae
DJ
5133 break;
5134 case bp_watchpoint:
5135 ui_out_text (uiout, "Watchpoint ");
5136 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5137 ui_out_field_int (uiout, "number", b->number);
5138 ui_out_text (uiout, ": ");
fa8a61dc 5139 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5140 do_cleanups (ui_out_chain);
5141 break;
5142 case bp_hardware_watchpoint:
5143 ui_out_text (uiout, "Hardware watchpoint ");
5144 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5145 ui_out_field_int (uiout, "number", b->number);
5146 ui_out_text (uiout, ": ");
fa8a61dc 5147 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5148 do_cleanups (ui_out_chain);
5149 break;
5150 case bp_read_watchpoint:
5151 ui_out_text (uiout, "Hardware read watchpoint ");
5152 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
5153 ui_out_field_int (uiout, "number", b->number);
5154 ui_out_text (uiout, ": ");
fa8a61dc 5155 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5156 do_cleanups (ui_out_chain);
5157 break;
5158 case bp_access_watchpoint:
5159 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
5160 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
5161 ui_out_field_int (uiout, "number", b->number);
5162 ui_out_text (uiout, ": ");
fa8a61dc 5163 ui_out_field_string (uiout, "exp", b->exp_string);
3086aeae
DJ
5164 do_cleanups (ui_out_chain);
5165 break;
5166 case bp_breakpoint:
5167 if (ui_out_is_mi_like_p (uiout))
5168 {
5169 say_where = 0;
5170 break;
5171 }
2cec12e5
AR
5172 if (b->disposition == disp_del)
5173 printf_filtered (_("Temporary breakpoint"));
5174 else
5175 printf_filtered (_("Breakpoint"));
5176 printf_filtered (_(" %d"), b->number);
3086aeae
DJ
5177 say_where = 1;
5178 break;
5179 case bp_hardware_breakpoint:
5180 if (ui_out_is_mi_like_p (uiout))
5181 {
5182 say_where = 0;
5183 break;
5184 }
a3f17187 5185 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
3086aeae
DJ
5186 say_where = 1;
5187 break;
1042e4c0
SS
5188 case bp_tracepoint:
5189 if (ui_out_is_mi_like_p (uiout))
5190 {
5191 say_where = 0;
5192 break;
5193 }
5194 printf_filtered (_("Tracepoint"));
5195 printf_filtered (_(" %d"), b->number);
5196 say_where = 1;
5197 break;
3086aeae
DJ
5198
5199 case bp_until:
5200 case bp_finish:
5201 case bp_longjmp:
5202 case bp_longjmp_resume:
5203 case bp_step_resume:
3086aeae
DJ
5204 case bp_call_dummy:
5205 case bp_watchpoint_scope:
5206 case bp_shlib_event:
5207 case bp_thread_event:
5208 case bp_overlay_event:
0fd8e87f 5209 case bp_longjmp_master:
3086aeae
DJ
5210 break;
5211 }
c906108c 5212
c906108c
SS
5213 if (say_where)
5214 {
a3f17187
AC
5215 /* i18n: cagney/2005-02-11: Below needs to be merged into a
5216 single string. */
0d381245 5217 if (b->loc == NULL)
c906108c 5218 {
a3f17187 5219 printf_filtered (_(" (%s) pending."), b->addr_string);
0101ce28
JJ
5220 }
5221 else
5222 {
79a45b7d 5223 if (opts.addressprint || b->source_file == NULL)
0101ce28
JJ
5224 {
5225 printf_filtered (" at ");
ed49a04f 5226 fputs_filtered (paddress (b->loc->address), gdb_stdout);
0101ce28
JJ
5227 }
5228 if (b->source_file)
5229 printf_filtered (": file %s, line %d.",
5230 b->source_file, b->line_number);
0d381245
VP
5231
5232 if (b->loc->next)
5233 {
5234 struct bp_location *loc = b->loc;
5235 int n = 0;
5236 for (; loc; loc = loc->next)
5237 ++n;
5238 printf_filtered (" (%d locations)", n);
5239 }
5240
c906108c 5241 }
c906108c 5242 }
9dc5e2a9 5243 if (ui_out_is_mi_like_p (uiout))
fb40c209 5244 return;
c906108c
SS
5245 printf_filtered ("\n");
5246}
c906108c 5247\f
c5aa993b 5248
0d381245 5249static struct bp_location *
39d61571 5250add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
5251 const struct symtab_and_line *sal)
5252{
5253 struct bp_location *loc, **tmp;
5254
39d61571 5255 loc = allocate_bp_location (b);
0d381245
VP
5256 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
5257 ;
5258 *tmp = loc;
5259 loc->requested_address = sal->pc;
39d61571 5260 loc->address = adjust_breakpoint_address (loc->requested_address, b->type);
0d381245
VP
5261 loc->section = sal->section;
5262
5263 set_breakpoint_location_function (loc);
5264 return loc;
5265}
514f746b
AR
5266\f
5267
5268/* Return 1 if LOC is pointing to a permanent breakpoint,
5269 return 0 otherwise. */
5270
5271static int
5272bp_loc_is_permanent (struct bp_location *loc)
5273{
5274 int len;
5275 CORE_ADDR addr;
5276 const gdb_byte *brk;
5277 gdb_byte *target_mem;
939c61fa
JK
5278 struct cleanup *cleanup;
5279 int retval = 0;
514f746b
AR
5280
5281 gdb_assert (loc != NULL);
5282
5283 addr = loc->address;
5284 brk = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &len);
5285
939c61fa
JK
5286 /* Software breakpoints unsupported? */
5287 if (brk == NULL)
5288 return 0;
5289
514f746b
AR
5290 target_mem = alloca (len);
5291
939c61fa
JK
5292 /* Enable the automatic memory restoration from breakpoints while
5293 we read the memory. Otherwise we could say about our temporary
5294 breakpoints they are permanent. */
5295 cleanup = make_show_memory_breakpoints_cleanup (0);
5296
514f746b
AR
5297 if (target_read_memory (loc->address, target_mem, len) == 0
5298 && memcmp (target_mem, brk, len) == 0)
939c61fa 5299 retval = 1;
514f746b 5300
939c61fa
JK
5301 do_cleanups (cleanup);
5302
5303 return retval;
514f746b
AR
5304}
5305
5306
c3f6f71d 5307
018d34a4
VP
5308/* Create a breakpoint with SAL as location. Use ADDR_STRING
5309 as textual description of the location, and COND_STRING
db107f19 5310 as condition expression. */
018d34a4
VP
5311
5312static void
0d381245 5313create_breakpoint (struct symtabs_and_lines sals, char *addr_string,
018d34a4
VP
5314 char *cond_string,
5315 enum bptype type, enum bpdisp disposition,
4a306c9a 5316 int thread, int task, int ignore_count,
41447f92 5317 struct breakpoint_ops *ops, int from_tty, int enabled)
018d34a4 5318{
0d381245
VP
5319 struct breakpoint *b = NULL;
5320 int i;
018d34a4
VP
5321
5322 if (type == bp_hardware_breakpoint)
5323 {
5324 int i = hw_breakpoint_used_count ();
5325 int target_resources_ok =
d92524f1 5326 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
5327 i + 1, 0);
5328 if (target_resources_ok == 0)
5329 error (_("No hardware breakpoint support in the target."));
5330 else if (target_resources_ok < 0)
5331 error (_("Hardware breakpoints used exceeds limit."));
5332 }
5333
0d381245
VP
5334 for (i = 0; i < sals.nelts; ++i)
5335 {
5336 struct symtab_and_line sal = sals.sals[i];
5337 struct bp_location *loc;
5338
5339 if (from_tty)
5340 describe_other_breakpoints (sal.pc, sal.section, thread);
5341
5342 if (i == 0)
5343 {
5344 b = set_raw_breakpoint (sal, type);
5345 set_breakpoint_count (breakpoint_count + 1);
5346 b->number = breakpoint_count;
5347 b->thread = thread;
4a306c9a 5348 b->task = task;
018d34a4 5349
0d381245
VP
5350 b->cond_string = cond_string;
5351 b->ignore_count = ignore_count;
41447f92 5352 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 5353 b->disposition = disposition;
018d34a4 5354
0d381245
VP
5355 loc = b->loc;
5356 }
5357 else
018d34a4 5358 {
39d61571 5359 loc = add_location_to_breakpoint (b, &sal);
0d381245
VP
5360 }
5361
514f746b
AR
5362 if (bp_loc_is_permanent (loc))
5363 make_breakpoint_permanent (b);
5364
0d381245
VP
5365 if (b->cond_string)
5366 {
5367 char *arg = b->cond_string;
d32a6982 5368 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 5369 if (*arg)
db107f19 5370 error (_("Garbage %s follows condition"), arg);
018d34a4 5371 }
0d381245 5372 }
018d34a4
VP
5373
5374 if (addr_string)
5375 b->addr_string = addr_string;
5376 else
5377 /* addr_string has to be used or breakpoint_re_set will delete
5378 me. */
5379 b->addr_string = xstrprintf ("*0x%s", paddr (b->loc->address));
5380
604133b5 5381 b->ops = ops;
018d34a4
VP
5382 mention (b);
5383}
5384
ed0616c6
VP
5385/* Remove element at INDEX_TO_REMOVE from SAL, shifting other
5386 elements to fill the void space. */
2c0b251b
PA
5387static void
5388remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
ed0616c6
VP
5389{
5390 int i = index_to_remove+1;
5391 int last_index = sal->nelts-1;
5392
5393 for (;i <= last_index; ++i)
5394 sal->sals[i-1] = sal->sals[i];
5395
5396 --(sal->nelts);
5397}
5398
5399/* If appropriate, obtains all sals that correspond
5400 to the same file and line as SAL. This is done
5401 only if SAL does not have explicit PC and has
5402 line and file information. If we got just a single
5403 expanded sal, return the original.
5404
5405 Otherwise, if SAL.explicit_line is not set, filter out
5406 all sals for which the name of enclosing function
5407 is different from SAL. This makes sure that if we have
5408 breakpoint originally set in template instantiation, say
5409 foo<int>(), we won't expand SAL to locations at the same
5410 line in all existing instantiations of 'foo'.
5411
5412*/
2c0b251b 5413static struct symtabs_and_lines
ed0616c6
VP
5414expand_line_sal_maybe (struct symtab_and_line sal)
5415{
5416 struct symtabs_and_lines expanded;
5417 CORE_ADDR original_pc = sal.pc;
5418 char *original_function = NULL;
5419 int found;
5420 int i;
5421
5422 /* If we have explicit pc, don't expand.
5423 If we have no line number, we can't expand. */
5424 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
5425 {
5426 expanded.nelts = 1;
5427 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5428 expanded.sals[0] = sal;
5429 return expanded;
5430 }
5431
5432 sal.pc = 0;
5433 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
5434
5435 expanded = expand_line_sal (sal);
5436 if (expanded.nelts == 1)
5437 {
5438 /* We had one sal, we got one sal. Without futher
5439 processing, just return the original sal. */
5440 xfree (expanded.sals);
5441 expanded.nelts = 1;
5442 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5443 sal.pc = original_pc;
5444 expanded.sals[0] = sal;
5445 return expanded;
5446 }
5447
5448 if (!sal.explicit_line)
5449 {
5450 CORE_ADDR func_addr, func_end;
5451 for (i = 0; i < expanded.nelts; ++i)
5452 {
5453 CORE_ADDR pc = expanded.sals[i].pc;
5454 char *this_function;
5455 if (find_pc_partial_function (pc, &this_function,
5456 &func_addr, &func_end))
5457 {
059fb39f
PM
5458 if (this_function
5459 && strcmp (this_function, original_function) != 0)
ed0616c6
VP
5460 {
5461 remove_sal (&expanded, i);
5462 --i;
5463 }
5464 else if (func_addr == pc)
5465 {
5466 /* We're at beginning of a function, and should
5467 skip prologue. */
5468 struct symbol *sym = find_pc_function (pc);
5469 if (sym)
5470 expanded.sals[i] = find_function_start_sal (sym, 1);
5471 else
d80b854b
UW
5472 {
5473 /* Since find_pc_partial_function returned true,
5474 we should really always find the section here. */
5475 struct obj_section *section = find_pc_section (pc);
5476 if (section)
5477 {
5478 struct gdbarch *gdbarch
5479 = get_objfile_arch (section->objfile);
5480 expanded.sals[i].pc
5481 = gdbarch_skip_prologue (gdbarch, pc);
5482 }
5483 }
ed0616c6
VP
5484 }
5485 }
5486 }
5487 }
3daf8fe5
JG
5488 else
5489 {
5490 for (i = 0; i < expanded.nelts; ++i)
5491 {
5492 /* If this SAL corresponds to a breakpoint inserted using a
5493 line number, then skip the function prologue if necessary. */
5494 skip_prologue_sal (&expanded.sals[i]);
5495 }
5496 }
ed0616c6
VP
5497
5498
5499 if (expanded.nelts <= 1)
5500 {
5501 /* This is un ugly workaround. If we get zero
5502 expanded sals then something is really wrong.
5503 Fix that by returnign the original sal. */
5504 xfree (expanded.sals);
5505 expanded.nelts = 1;
5506 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
5507 sal.pc = original_pc;
5508 expanded.sals[0] = sal;
5509 return expanded;
5510 }
5511
5512 if (original_pc)
5513 {
5514 found = 0;
5515 for (i = 0; i < expanded.nelts; ++i)
5516 if (expanded.sals[i].pc == original_pc)
5517 {
5518 found = 1;
5519 break;
5520 }
5521 gdb_assert (found);
5522 }
5523
5524 return expanded;
5525}
5526
018d34a4
VP
5527/* Add SALS.nelts breakpoints to the breakpoint table. For each
5528 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
5529 value. COND_STRING, if not NULL, specified the condition to be
5530 used for all breakpoints. Essentially the only case where
5531 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
5532 function. In that case, it's still not possible to specify
5533 separate conditions for different overloaded functions, so
5534 we take just a single condition string.
5535
c3f6f71d 5536 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 5537 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
5538 array contents). If the function fails (error() is called), the
5539 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
5540 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
5541
5542static void
c3f6f71d 5543create_breakpoints (struct symtabs_and_lines sals, char **addr_string,
018d34a4 5544 char *cond_string,
c3f6f71d 5545 enum bptype type, enum bpdisp disposition,
4a306c9a 5546 int thread, int task, int ignore_count,
41447f92
VP
5547 struct breakpoint_ops *ops, int from_tty,
5548 int enabled)
c906108c 5549{
018d34a4
VP
5550 int i;
5551 for (i = 0; i < sals.nelts; ++i)
c3f6f71d 5552 {
ed0616c6
VP
5553 struct symtabs_and_lines expanded =
5554 expand_line_sal_maybe (sals.sals[i]);
0d381245 5555
ed0616c6 5556 create_breakpoint (expanded, addr_string[i],
018d34a4 5557 cond_string, type, disposition,
4a306c9a 5558 thread, task, ignore_count, ops, from_tty, enabled);
c3f6f71d 5559 }
c3f6f71d 5560}
c906108c 5561
c3f6f71d
JM
5562/* Parse ARG which is assumed to be a SAL specification possibly
5563 followed by conditionals. On return, SALS contains an array of SAL
5564 addresses found. ADDR_STRING contains a vector of (canonical)
5565 address strings. ARG points to the end of the SAL. */
c906108c 5566
b9362cc7 5567static void
c3f6f71d
JM
5568parse_breakpoint_sals (char **address,
5569 struct symtabs_and_lines *sals,
0101ce28
JJ
5570 char ***addr_string,
5571 int *not_found_ptr)
c3f6f71d
JM
5572{
5573 char *addr_start = *address;
5574 *addr_string = NULL;
5575 /* If no arg given, or if first arg is 'if ', use the default
5576 breakpoint. */
5577 if ((*address) == NULL
5578 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c
SS
5579 {
5580 if (default_breakpoint_valid)
5581 {
c3f6f71d 5582 struct symtab_and_line sal;
fe39c653 5583 init_sal (&sal); /* initialize to zeroes */
c3f6f71d 5584 sals->sals = (struct symtab_and_line *)
c906108c
SS
5585 xmalloc (sizeof (struct symtab_and_line));
5586 sal.pc = default_breakpoint_address;
5587 sal.line = default_breakpoint_line;
5588 sal.symtab = default_breakpoint_symtab;
c5aa993b 5589 sal.section = find_pc_overlay (sal.pc);
00903456
JK
5590
5591 /* "break" without arguments is equivalent to "break *PC" where PC is
5592 the default_breakpoint_address. So make sure to set
5593 sal.explicit_pc to prevent GDB from trying to expand the list of
5594 sals to include all other instances with the same symtab and line.
5595 */
5596 sal.explicit_pc = 1;
5597
c3f6f71d
JM
5598 sals->sals[0] = sal;
5599 sals->nelts = 1;
c906108c
SS
5600 }
5601 else
8a3fe4f8 5602 error (_("No default breakpoint address now."));
c906108c
SS
5603 }
5604 else
5605 {
c906108c 5606 /* Force almost all breakpoints to be in terms of the
c5aa993b
JM
5607 current_source_symtab (which is decode_line_1's default). This
5608 should produce the results we want almost all of the time while
1aeae86e
AF
5609 leaving default_breakpoint_* alone.
5610 ObjC: However, don't match an Objective-C method name which
5611 may have a '+' or '-' succeeded by a '[' */
0378c332 5612
c214a6fd 5613 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
0378c332 5614
c906108c 5615 if (default_breakpoint_valid
0378c332 5616 && (!cursal.symtab
1aeae86e
AF
5617 || ((strchr ("+-", (*address)[0]) != NULL)
5618 && ((*address)[1] != '['))))
c3f6f71d 5619 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
0101ce28
JJ
5620 default_breakpoint_line, addr_string,
5621 not_found_ptr);
c906108c 5622 else
0101ce28
JJ
5623 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
5624 addr_string, not_found_ptr);
c906108c 5625 }
c3f6f71d
JM
5626 /* For any SAL that didn't have a canonical string, fill one in. */
5627 if (sals->nelts > 0 && *addr_string == NULL)
5628 *addr_string = xcalloc (sals->nelts, sizeof (char **));
5629 if (addr_start != (*address))
c906108c 5630 {
c3f6f71d
JM
5631 int i;
5632 for (i = 0; i < sals->nelts; i++)
c906108c 5633 {
c3f6f71d
JM
5634 /* Add the string if not present. */
5635 if ((*addr_string)[i] == NULL)
5636 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
c906108c
SS
5637 }
5638 }
c3f6f71d 5639}
c906108c 5640
c906108c 5641
c3f6f71d
JM
5642/* Convert each SAL into a real PC. Verify that the PC can be
5643 inserted as a breakpoint. If it can't throw an error. */
c906108c 5644
b9362cc7 5645static void
c3f6f71d
JM
5646breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
5647 char *address)
5648{
5649 int i;
5650 for (i = 0; i < sals->nelts; i++)
ee53e872 5651 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
5652}
5653
05ff989b 5654static void
0101ce28
JJ
5655do_captured_parse_breakpoint (struct ui_out *ui, void *data)
5656{
5657 struct captured_parse_breakpoint_args *args = data;
5658
5659 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
5660 args->not_found_ptr);
0101ce28
JJ
5661}
5662
018d34a4
VP
5663/* Given TOK, a string specification of condition and thread, as
5664 accepted by the 'break' command, extract the condition
5665 string and thread number and set *COND_STRING and *THREAD.
5666 PC identifies the context at which the condition should be parsed.
5667 If no condition is found, *COND_STRING is set to NULL.
5668 If no thread is found, *THREAD is set to -1. */
5669static void
5670find_condition_and_thread (char *tok, CORE_ADDR pc,
4a306c9a 5671 char **cond_string, int *thread, int *task)
018d34a4
VP
5672{
5673 *cond_string = NULL;
5674 *thread = -1;
5675 while (tok && *tok)
5676 {
5677 char *end_tok;
5678 int toklen;
5679 char *cond_start = NULL;
5680 char *cond_end = NULL;
5681 while (*tok == ' ' || *tok == '\t')
5682 tok++;
5683
5684 end_tok = tok;
5685
5686 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5687 end_tok++;
5688
5689 toklen = end_tok - tok;
5690
5691 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5692 {
f7545552
TT
5693 struct expression *expr;
5694
018d34a4 5695 tok = cond_start = end_tok + 1;
f7545552
TT
5696 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
5697 xfree (expr);
018d34a4
VP
5698 cond_end = tok;
5699 *cond_string = savestring (cond_start,
5700 cond_end - cond_start);
5701 }
5702 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5703 {
5704 char *tmptok;
5705
5706 tok = end_tok + 1;
5707 tmptok = tok;
5708 *thread = strtol (tok, &tok, 0);
5709 if (tok == tmptok)
5710 error (_("Junk after thread keyword."));
5711 if (!valid_thread_id (*thread))
5712 error (_("Unknown thread %d."), *thread);
5713 }
4a306c9a
JB
5714 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
5715 {
5716 char *tmptok;
5717
5718 tok = end_tok + 1;
5719 tmptok = tok;
5720 *task = strtol (tok, &tok, 0);
5721 if (tok == tmptok)
5722 error (_("Junk after task keyword."));
5723 if (!valid_task_id (*task))
5724 error (_("Unknown task %d\n"), *task);
5725 }
018d34a4
VP
5726 else
5727 error (_("Junk at end of arguments."));
5728 }
5729}
5730
72b2ff0e
VP
5731/* Set a breakpoint. This function is shared between
5732 CLI and MI functions for setting a breakpoint.
5733 This function has two major modes of operations,
5734 selected by the PARSE_CONDITION_AND_THREAD parameter.
5735 If non-zero, the function will parse arg, extracting
5736 breakpoint location, address and thread. Otherwise,
5737 ARG is just the location of breakpoint, with condition
5738 and thread specified by the COND_STRING and THREAD
5739 parameters. */
0101ce28 5740
98deb0da 5741static void
72b2ff0e 5742break_command_really (char *arg, char *cond_string, int thread,
60c46647 5743 int parse_condition_and_thread,
1042e4c0 5744 int tempflag, int hardwareflag, int traceflag,
60c46647
VP
5745 int ignore_count,
5746 enum auto_boolean pending_break_support,
604133b5 5747 struct breakpoint_ops *ops,
41447f92
VP
5748 int from_tty,
5749 int enabled)
c3f6f71d 5750{
71fff37b 5751 struct gdb_exception e;
c3f6f71d 5752 struct symtabs_and_lines sals;
0101ce28 5753 struct symtab_and_line pending_sal;
0101ce28
JJ
5754 char *copy_arg;
5755 char *err_msg;
c3f6f71d
JM
5756 char *addr_start = arg;
5757 char **addr_string;
5758 struct cleanup *old_chain;
80c99de1 5759 struct cleanup *bkpt_chain = NULL;
0101ce28 5760 struct captured_parse_breakpoint_args parse_args;
05ff989b 5761 int i;
0101ce28 5762 int pending = 0;
0101ce28 5763 int not_found = 0;
1042e4c0 5764 enum bptype type_wanted;
4a306c9a 5765 int task = 0;
c3f6f71d 5766
c3f6f71d
JM
5767 sals.sals = NULL;
5768 sals.nelts = 0;
5769 addr_string = NULL;
c3f6f71d 5770
0101ce28
JJ
5771 parse_args.arg_p = &arg;
5772 parse_args.sals_p = &sals;
5773 parse_args.addr_string_p = &addr_string;
5774 parse_args.not_found_ptr = &not_found;
5775
05ff989b
AC
5776 e = catch_exception (uiout, do_captured_parse_breakpoint,
5777 &parse_args, RETURN_MASK_ALL);
0101ce28
JJ
5778
5779 /* If caller is interested in rc value from parse, set value. */
05ff989b 5780 switch (e.reason)
0101ce28 5781 {
05ff989b 5782 case RETURN_QUIT:
98deb0da 5783 throw_exception (e);
05ff989b
AC
5784 case RETURN_ERROR:
5785 switch (e.error)
0101ce28 5786 {
05ff989b 5787 case NOT_FOUND_ERROR:
0101ce28 5788
05ff989b
AC
5789 /* If pending breakpoint support is turned off, throw
5790 error. */
fa8d40ab
JJ
5791
5792 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
5793 throw_exception (e);
5794
5795 exception_print (gdb_stderr, e);
fa8d40ab 5796
05ff989b
AC
5797 /* If pending breakpoint support is auto query and the user
5798 selects no, then simply return the error code. */
059fb39f
PM
5799 if (pending_break_support == AUTO_BOOLEAN_AUTO
5800 && !nquery ("Make breakpoint pending on future shared library load? "))
98deb0da 5801 return;
fa8d40ab 5802
05ff989b
AC
5803 /* At this point, either the user was queried about setting
5804 a pending breakpoint and selected yes, or pending
5805 breakpoint behavior is on and thus a pending breakpoint
5806 is defaulted on behalf of the user. */
0101ce28
JJ
5807 copy_arg = xstrdup (addr_start);
5808 addr_string = &copy_arg;
5809 sals.nelts = 1;
5810 sals.sals = &pending_sal;
5811 pending_sal.pc = 0;
5812 pending = 1;
05ff989b
AC
5813 break;
5814 default:
98deb0da 5815 throw_exception (e);
0101ce28 5816 }
05ff989b
AC
5817 default:
5818 if (!sals.nelts)
98deb0da 5819 return;
0101ce28 5820 }
c3f6f71d
JM
5821
5822 /* Create a chain of things that always need to be cleaned up. */
5823 old_chain = make_cleanup (null_cleanup, 0);
5824
0101ce28
JJ
5825 if (!pending)
5826 {
5827 /* Make sure that all storage allocated to SALS gets freed. */
5828 make_cleanup (xfree, sals.sals);
5829
5830 /* Cleanup the addr_string array but not its contents. */
5831 make_cleanup (xfree, addr_string);
5832 }
c3f6f71d 5833
c3f6f71d
JM
5834 /* ----------------------------- SNIP -----------------------------
5835 Anything added to the cleanup chain beyond this point is assumed
5836 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
5837 then the memory is not reclaimed. */
5838 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d
JM
5839
5840 /* Mark the contents of the addr_string for cleanup. These go on
80c99de1 5841 the bkpt_chain and only occur if the breakpoint create fails. */
c3f6f71d
JM
5842 for (i = 0; i < sals.nelts; i++)
5843 {
5844 if (addr_string[i] != NULL)
b8c9b27d 5845 make_cleanup (xfree, addr_string[i]);
c3f6f71d
JM
5846 }
5847
5848 /* Resolve all line numbers to PC's and verify that the addresses
5849 are ok for the target. */
0101ce28
JJ
5850 if (!pending)
5851 breakpoint_sals_to_pc (&sals, addr_start);
c3f6f71d 5852
1042e4c0
SS
5853 type_wanted = (traceflag
5854 ? bp_tracepoint
5855 : (hardwareflag ? bp_hardware_breakpoint : bp_breakpoint));
5856
c3f6f71d
JM
5857 /* Verify that condition can be parsed, before setting any
5858 breakpoints. Allocate a separate condition expression for each
5859 breakpoint. */
0101ce28 5860 if (!pending)
c3f6f71d 5861 {
2f069f6f 5862 if (parse_condition_and_thread)
72b2ff0e
VP
5863 {
5864 /* Here we only parse 'arg' to separate condition
5865 from thread number, so parsing in context of first
5866 sal is OK. When setting the breakpoint we'll
5867 re-parse it in context of each sal. */
5868 cond_string = NULL;
5869 thread = -1;
4a306c9a
JB
5870 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
5871 &thread, &task);
72b2ff0e
VP
5872 if (cond_string)
5873 make_cleanup (xfree, cond_string);
5874 }
2f069f6f 5875 else
72b2ff0e
VP
5876 {
5877 /* Create a private copy of condition string. */
5878 if (cond_string)
5879 {
5880 cond_string = xstrdup (cond_string);
5881 make_cleanup (xfree, cond_string);
5882 }
5883 }
1042e4c0 5884 create_breakpoints (sals, addr_string, cond_string, type_wanted,
0101ce28 5885 tempflag ? disp_del : disp_donttouch,
4a306c9a 5886 thread, task, ignore_count, ops, from_tty, enabled);
c906108c 5887 }
0101ce28
JJ
5888 else
5889 {
fe3f5fa8 5890 struct symtab_and_line sal = {0};
0101ce28
JJ
5891 struct breakpoint *b;
5892
0101ce28
JJ
5893 make_cleanup (xfree, copy_arg);
5894
1042e4c0 5895 b = set_raw_breakpoint_without_location (type_wanted);
0101ce28
JJ
5896 set_breakpoint_count (breakpoint_count + 1);
5897 b->number = breakpoint_count;
72b2ff0e 5898 b->thread = -1;
018d34a4 5899 b->addr_string = addr_string[0];
72b2ff0e 5900 b->cond_string = NULL;
0101ce28 5901 b->ignore_count = ignore_count;
0101ce28 5902 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 5903 b->condition_not_parsed = 1;
604133b5 5904 b->ops = ops;
41447f92 5905 b->enable_state = enabled ? bp_enabled : bp_disabled;
74960c60 5906
0101ce28
JJ
5907 mention (b);
5908 }
5909
c3f6f71d 5910 if (sals.nelts > 1)
8a3fe4f8
AC
5911 warning (_("Multiple breakpoints were set.\n"
5912 "Use the \"delete\" command to delete unwanted breakpoints."));
80c99de1
PA
5913 /* That's it. Discard the cleanups for data inserted into the
5914 breakpoint. */
5915 discard_cleanups (bkpt_chain);
5916 /* But cleanup everything else. */
c3f6f71d 5917 do_cleanups (old_chain);
217dc9e2 5918
80c99de1 5919 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 5920 update_global_location_list (1);
c3f6f71d 5921}
c906108c 5922
72b2ff0e
VP
5923/* Set a breakpoint.
5924 ARG is a string describing breakpoint address,
5925 condition, and thread.
5926 FLAG specifies if a breakpoint is hardware on,
5927 and if breakpoint is temporary, using BP_HARDWARE_FLAG
5928 and BP_TEMPFLAG. */
5929
98deb0da 5930static void
72b2ff0e 5931break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 5932{
72b2ff0e
VP
5933 int hardwareflag = flag & BP_HARDWAREFLAG;
5934 int tempflag = flag & BP_TEMPFLAG;
c3f6f71d 5935
98deb0da
VP
5936 break_command_really (arg,
5937 NULL, 0, 1 /* parse arg */,
1042e4c0 5938 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da 5939 0 /* Ignore count */,
604133b5
AR
5940 pending_break_support,
5941 NULL /* breakpoint_ops */,
41447f92
VP
5942 from_tty,
5943 1 /* enabled */);
c906108c
SS
5944}
5945
72b2ff0e 5946
98deb0da
VP
5947void
5948set_breakpoint (char *address, char *condition,
c3f6f71d 5949 int hardwareflag, int tempflag,
ce43223b 5950 int thread, int ignore_count,
41447f92 5951 int pending, int enabled)
c3f6f71d 5952{
98deb0da
VP
5953 break_command_really (address, condition, thread,
5954 0 /* condition and thread are valid. */,
1042e4c0 5955 tempflag, hardwareflag, 0 /* traceflag */,
98deb0da
VP
5956 ignore_count,
5957 pending
5958 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
41447f92 5959 NULL, 0, enabled);
c3f6f71d
JM
5960}
5961
6a048695
JB
5962/* Adjust SAL to the first instruction past the function prologue.
5963 The end of the prologue is determined using the line table from
3daf8fe5
JG
5964 the debugging information. explicit_pc and explicit_line are
5965 not modified.
6a048695
JB
5966
5967 If SAL is already past the prologue, then do nothing. */
5968
5969static void
5970skip_prologue_sal (struct symtab_and_line *sal)
5971{
5972 struct symbol *sym = find_pc_function (sal->pc);
5973 struct symtab_and_line start_sal;
5974
5975 if (sym == NULL)
5976 return;
5977
5978 start_sal = find_function_start_sal (sym, 1);
5979 if (sal->pc < start_sal.pc)
3daf8fe5
JG
5980 {
5981 start_sal.explicit_line = sal->explicit_line;
5982 start_sal.explicit_pc = sal->explicit_pc;
5983 *sal = start_sal;
5984 }
6a048695 5985}
c3f6f71d 5986
c906108c
SS
5987/* Helper function for break_command_1 and disassemble_command. */
5988
5989void
fba45db2 5990resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
5991{
5992 CORE_ADDR pc;
5993
5994 if (sal->pc == 0 && sal->symtab != NULL)
5995 {
5996 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 5997 error (_("No line %d in file \"%s\"."),
c906108c
SS
5998 sal->line, sal->symtab->filename);
5999 sal->pc = pc;
6a048695
JB
6000
6001 /* If this SAL corresponds to a breakpoint inserted using
6002 a line number, then skip the function prologue if necessary. */
6003 if (sal->explicit_line)
56ef84b1
TT
6004 {
6005 /* Preserve the original line number. */
6006 int saved_line = sal->line;
6007 skip_prologue_sal (sal);
6008 sal->line = saved_line;
6009 }
c906108c
SS
6010 }
6011
6012 if (sal->section == 0 && sal->symtab != NULL)
6013 {
6014 struct blockvector *bv;
c5aa993b
JM
6015 struct block *b;
6016 struct symbol *sym;
c906108c 6017
801e3a5b 6018 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
6019 if (bv != NULL)
6020 {
7f0df278 6021 sym = block_linkage_function (b);
c906108c
SS
6022 if (sym != NULL)
6023 {
6024 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 6025 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
6026 }
6027 else
6028 {
6029 /* It really is worthwhile to have the section, so we'll just
c5aa993b
JM
6030 have to look harder. This case can be executed if we have
6031 line numbers but no functions (as can happen in assembly
6032 source). */
c906108c 6033
c5aa993b 6034 struct minimal_symbol *msym;
c906108c
SS
6035
6036 msym = lookup_minimal_symbol_by_pc (sal->pc);
6037 if (msym)
714835d5 6038 sal->section = SYMBOL_OBJ_SECTION (msym);
c906108c
SS
6039 }
6040 }
6041 }
6042}
6043
6044void
fba45db2 6045break_command (char *arg, int from_tty)
c906108c 6046{
db107f19 6047 break_command_1 (arg, 0, from_tty);
c906108c
SS
6048}
6049
c906108c 6050void
fba45db2 6051tbreak_command (char *arg, int from_tty)
c906108c 6052{
db107f19 6053 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
6054}
6055
c906108c 6056static void
fba45db2 6057hbreak_command (char *arg, int from_tty)
c906108c 6058{
db107f19 6059 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
6060}
6061
6062static void
fba45db2 6063thbreak_command (char *arg, int from_tty)
c906108c 6064{
db107f19 6065 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
6066}
6067
6068static void
fba45db2 6069stop_command (char *arg, int from_tty)
c906108c 6070{
a3f17187 6071 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 6072Usage: stop in <function | address>\n\
a3f17187 6073 stop at <line>\n"));
c906108c
SS
6074}
6075
6076static void
fba45db2 6077stopin_command (char *arg, int from_tty)
c906108c
SS
6078{
6079 int badInput = 0;
6080
c5aa993b 6081 if (arg == (char *) NULL)
c906108c
SS
6082 badInput = 1;
6083 else if (*arg != '*')
6084 {
6085 char *argptr = arg;
6086 int hasColon = 0;
6087
53a5351d
JM
6088 /* look for a ':'. If this is a line number specification, then
6089 say it is bad, otherwise, it should be an address or
6090 function/method name */
c906108c 6091 while (*argptr && !hasColon)
c5aa993b
JM
6092 {
6093 hasColon = (*argptr == ':');
6094 argptr++;
6095 }
c906108c
SS
6096
6097 if (hasColon)
c5aa993b 6098 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 6099 else
c5aa993b 6100 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
6101 }
6102
6103 if (badInput)
a3f17187 6104 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 6105 else
db107f19 6106 break_command_1 (arg, 0, from_tty);
c906108c
SS
6107}
6108
6109static void
fba45db2 6110stopat_command (char *arg, int from_tty)
c906108c
SS
6111{
6112 int badInput = 0;
6113
c5aa993b 6114 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
6115 badInput = 1;
6116 else
6117 {
6118 char *argptr = arg;
6119 int hasColon = 0;
6120
6121 /* look for a ':'. If there is a '::' then get out, otherwise
c5aa993b 6122 it is probably a line number. */
c906108c 6123 while (*argptr && !hasColon)
c5aa993b
JM
6124 {
6125 hasColon = (*argptr == ':');
6126 argptr++;
6127 }
c906108c
SS
6128
6129 if (hasColon)
c5aa993b 6130 badInput = (*argptr == ':'); /* we have class::method */
c906108c 6131 else
c5aa993b 6132 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
6133 }
6134
6135 if (badInput)
a3f17187 6136 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 6137 else
db107f19 6138 break_command_1 (arg, 0, from_tty);
c906108c
SS
6139}
6140
53a5351d
JM
6141/* accessflag: hw_write: watch write,
6142 hw_read: watch read,
6143 hw_access: watch access (read or write) */
c906108c 6144static void
fba45db2 6145watch_command_1 (char *arg, int accessflag, int from_tty)
c906108c 6146{
d983da9c 6147 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c
SS
6148 struct symtab_and_line sal;
6149 struct expression *exp;
6150 struct block *exp_valid_block;
6151 struct value *val, *mark;
6152 struct frame_info *frame;
c906108c
SS
6153 char *exp_start = NULL;
6154 char *exp_end = NULL;
37e4754d 6155 char *tok, *id_tok_start, *end_tok;
c906108c
SS
6156 int toklen;
6157 char *cond_start = NULL;
6158 char *cond_end = NULL;
6159 struct expression *cond = NULL;
6160 int i, other_type_used, target_resources_ok = 0;
6161 enum bptype bp_type;
6162 int mem_cnt = 0;
37e4754d 6163 int thread = -1;
c906108c 6164
fe39c653 6165 init_sal (&sal); /* initialize to zeroes */
c5aa993b 6166
37e4754d
LM
6167 /* Make sure that we actually have parameters to parse. */
6168 if (arg != NULL && arg[0] != '\0')
6169 {
6170 toklen = strlen (arg); /* Size of argument list. */
6171
6172 /* Points tok to the end of the argument list. */
6173 tok = arg + toklen - 1;
6174
6175 /* Go backwards in the parameters list. Skip the last parameter.
6176 If we're expecting a 'thread <thread_num>' parameter, this should
6177 be the thread identifier. */
6178 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6179 tok--;
6180 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6181 tok--;
6182
6183 /* Points end_tok to the beginning of the last token. */
6184 id_tok_start = tok + 1;
6185
6186 /* Go backwards in the parameters list. Skip one more parameter.
6187 If we're expecting a 'thread <thread_num>' parameter, we should
6188 reach a "thread" token. */
6189 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6190 tok--;
6191
6192 end_tok = tok;
6193
6194 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6195 tok--;
6196
6197 /* Move the pointer forward to skip the whitespace and
6198 calculate the length of the token. */
6199 tok++;
6200 toklen = end_tok - tok;
6201
6202 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
6203 {
6204 /* At this point we've found a "thread" token, which means
6205 the user is trying to set a watchpoint that triggers
6206 only in a specific thread. */
6207 char *endp;
6208
6209 /* Extract the thread ID from the next token. */
6210 thread = strtol (id_tok_start, &endp, 0);
6211
6212 /* Check if the user provided a valid numeric value for the
6213 thread ID. */
6214 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
6215 error (_("Invalid thread ID specification %s."), id_tok_start);
6216
6217 /* Check if the thread actually exists. */
6218 if (!valid_thread_id (thread))
6219 error (_("Unknown thread %d."), thread);
6220
6221 /* Truncate the string and get rid of the thread <thread_num>
6222 parameter before the parameter list is parsed by the
6223 evaluate_expression() function. */
6224 *tok = '\0';
6225 }
6226 }
6227
6228 /* Parse the rest of the arguments. */
c906108c
SS
6229 innermost_block = NULL;
6230 exp_start = arg;
6231 exp = parse_exp_1 (&arg, 0, 0);
6232 exp_end = arg;
fa8a61dc
TT
6233 /* Remove trailing whitespace from the expression before saving it.
6234 This makes the eventual display of the expression string a bit
6235 prettier. */
6236 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
6237 --exp_end;
6238
c906108c
SS
6239 exp_valid_block = innermost_block;
6240 mark = value_mark ();
fa4727a6
DJ
6241 fetch_watchpoint_value (exp, &val, NULL, NULL);
6242 if (val != NULL)
6243 release_value (val);
c906108c
SS
6244
6245 tok = arg;
6246 while (*tok == ' ' || *tok == '\t')
6247 tok++;
6248 end_tok = tok;
6249
6250 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6251 end_tok++;
6252
6253 toklen = end_tok - tok;
6254 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6255 {
6256 tok = cond_start = end_tok + 1;
6257 cond = parse_exp_1 (&tok, 0, 0);
6258 cond_end = tok;
6259 }
6260 if (*tok)
8a3fe4f8 6261 error (_("Junk at end of command."));
c906108c 6262
53a5351d 6263 if (accessflag == hw_read)
c5aa993b 6264 bp_type = bp_read_watchpoint;
53a5351d 6265 else if (accessflag == hw_access)
c5aa993b
JM
6266 bp_type = bp_access_watchpoint;
6267 else
6268 bp_type = bp_hardware_watchpoint;
c906108c
SS
6269
6270 mem_cnt = can_use_hardware_watchpoint (val);
6271 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6272 error (_("Expression cannot be implemented with read/access watchpoint."));
c5aa993b
JM
6273 if (mem_cnt != 0)
6274 {
6275 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d 6276 target_resources_ok =
d92524f1 6277 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
53a5351d 6278 other_type_used);
c5aa993b 6279 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6280 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 6281
c5aa993b 6282 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 6283 error (_("Target can only support one kind of HW watchpoint at a time."));
c5aa993b 6284 }
c906108c 6285
4d28f7a8
KB
6286 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
6287 watchpoint could not be set. */
6288 if (!mem_cnt || target_resources_ok <= 0)
6289 bp_type = bp_watchpoint;
6290
d983da9c 6291 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
6292
6293 /* If the expression is "local", then set up a "watchpoint scope"
6294 breakpoint at the point where we've left the scope of the watchpoint
6295 expression. Create the scope breakpoint before the watchpoint, so
6296 that we will encounter it first in bpstat_stop_status. */
edb3359d 6297 if (innermost_block && frame)
d983da9c 6298 {
edb3359d
DJ
6299 if (frame_id_p (frame_unwind_caller_id (frame)))
6300 {
6301 scope_breakpoint
6302 = create_internal_breakpoint (frame_unwind_caller_pc (frame),
6303 bp_watchpoint_scope);
d983da9c 6304
edb3359d 6305 scope_breakpoint->enable_state = bp_enabled;
d983da9c 6306
edb3359d
DJ
6307 /* Automatically delete the breakpoint when it hits. */
6308 scope_breakpoint->disposition = disp_del;
d983da9c 6309
edb3359d
DJ
6310 /* Only break in the proper frame (help with recursion). */
6311 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 6312
edb3359d
DJ
6313 /* Set the address at which we will stop. */
6314 scope_breakpoint->loc->requested_address
6315 = frame_unwind_caller_pc (frame);
6316 scope_breakpoint->loc->address
6317 = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
6318 scope_breakpoint->type);
6319 }
d983da9c
DJ
6320 }
6321
c906108c 6322 /* Now set up the breakpoint. */
4d28f7a8 6323 b = set_raw_breakpoint (sal, bp_type);
c906108c
SS
6324 set_breakpoint_count (breakpoint_count + 1);
6325 b->number = breakpoint_count;
37e4754d 6326 b->thread = thread;
b5de0fa7 6327 b->disposition = disp_donttouch;
c906108c
SS
6328 b->exp = exp;
6329 b->exp_valid_block = exp_valid_block;
6330 b->exp_string = savestring (exp_start, exp_end - exp_start);
6331 b->val = val;
fa4727a6 6332 b->val_valid = 1;
511a6cd4 6333 b->loc->cond = cond;
c906108c
SS
6334 if (cond_start)
6335 b->cond_string = savestring (cond_start, cond_end - cond_start);
6336 else
6337 b->cond_string = 0;
c5aa993b 6338
c906108c 6339 if (frame)
d983da9c 6340 b->watchpoint_frame = get_frame_id (frame);
c906108c 6341 else
35a487f1 6342 b->watchpoint_frame = null_frame_id;
c906108c 6343
d983da9c 6344 if (scope_breakpoint != NULL)
c906108c 6345 {
d983da9c
DJ
6346 /* The scope breakpoint is related to the watchpoint. We will
6347 need to act on them together. */
6348 b->related_breakpoint = scope_breakpoint;
6349 scope_breakpoint->related_breakpoint = b;
c906108c 6350 }
d983da9c 6351
c906108c
SS
6352 value_free_to_mark (mark);
6353 mention (b);
b60e7edf 6354 update_global_location_list (1);
c906108c
SS
6355}
6356
6357/* Return count of locations need to be watched and can be handled
6358 in hardware. If the watchpoint can not be handled
6359 in hardware return zero. */
6360
c906108c 6361static int
fba45db2 6362can_use_hardware_watchpoint (struct value *v)
c906108c
SS
6363{
6364 int found_memory_cnt = 0;
2e70b7b9 6365 struct value *head = v;
c906108c
SS
6366
6367 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 6368 if (!can_use_hw_watchpoints)
c906108c 6369 return 0;
c5aa993b 6370
5c44784c
JM
6371 /* Make sure that the value of the expression depends only upon
6372 memory contents, and values computed from them within GDB. If we
6373 find any register references or function calls, we can't use a
6374 hardware watchpoint.
6375
6376 The idea here is that evaluating an expression generates a series
6377 of values, one holding the value of every subexpression. (The
6378 expression a*b+c has five subexpressions: a, b, a*b, c, and
6379 a*b+c.) GDB's values hold almost enough information to establish
6380 the criteria given above --- they identify memory lvalues,
6381 register lvalues, computed values, etcetera. So we can evaluate
6382 the expression, and then scan the chain of values that leaves
6383 behind to decide whether we can detect any possible change to the
6384 expression's final value using only hardware watchpoints.
6385
6386 However, I don't think that the values returned by inferior
6387 function calls are special in any way. So this function may not
6388 notice that an expression involving an inferior function call
6389 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 6390 for (; v; v = value_next (v))
c906108c 6391 {
5c44784c 6392 if (VALUE_LVAL (v) == lval_memory)
c906108c 6393 {
d69fe07e 6394 if (value_lazy (v))
5c44784c
JM
6395 /* A lazy memory lvalue is one that GDB never needed to fetch;
6396 we either just used its address (e.g., `a' in `a.b') or
6397 we never needed it at all (e.g., `a' in `a,b'). */
6398 ;
53a5351d 6399 else
5c44784c
JM
6400 {
6401 /* Ahh, memory we actually used! Check if we can cover
6402 it with hardware watchpoints. */
df407dfe 6403 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
6404
6405 /* We only watch structs and arrays if user asked for it
6406 explicitly, never if they just happen to appear in a
6407 middle of some value chain. */
6408 if (v == head
6409 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
6410 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
6411 {
42ae5230 6412 CORE_ADDR vaddr = value_address (v);
df407dfe 6413 int len = TYPE_LENGTH (value_type (v));
2e70b7b9 6414
d92524f1 6415 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
2e70b7b9
MS
6416 return 0;
6417 else
6418 found_memory_cnt++;
6419 }
5c44784c 6420 }
c5aa993b 6421 }
5086187c
AC
6422 else if (VALUE_LVAL (v) != not_lval
6423 && deprecated_value_modifiable (v) == 0)
53a5351d 6424 return 0; /* ??? What does this represent? */
5086187c 6425 else if (VALUE_LVAL (v) == lval_register)
53a5351d 6426 return 0; /* cannot watch a register with a HW watchpoint */
c906108c
SS
6427 }
6428
6429 /* The expression itself looks suitable for using a hardware
6430 watchpoint, but give the target machine a chance to reject it. */
6431 return found_memory_cnt;
6432}
6433
8b93c638 6434void
fba45db2 6435watch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6436{
6437 watch_command (arg, from_tty);
6438}
8926118c 6439
c5aa993b 6440static void
fba45db2 6441watch_command (char *arg, int from_tty)
c906108c 6442{
53a5351d 6443 watch_command_1 (arg, hw_write, from_tty);
c906108c
SS
6444}
6445
8b93c638 6446void
fba45db2 6447rwatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6448{
6449 rwatch_command (arg, from_tty);
6450}
8926118c 6451
c5aa993b 6452static void
fba45db2 6453rwatch_command (char *arg, int from_tty)
c906108c 6454{
53a5351d 6455 watch_command_1 (arg, hw_read, from_tty);
c906108c
SS
6456}
6457
8b93c638 6458void
fba45db2 6459awatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6460{
6461 awatch_command (arg, from_tty);
6462}
8926118c 6463
c5aa993b 6464static void
fba45db2 6465awatch_command (char *arg, int from_tty)
c906108c 6466{
53a5351d 6467 watch_command_1 (arg, hw_access, from_tty);
c906108c 6468}
c906108c 6469\f
c5aa993b 6470
43ff13b4 6471/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
6472 because it uses the mechanisms of breakpoints. */
6473
bfec99b2
PA
6474struct until_break_command_continuation_args
6475{
6476 struct breakpoint *breakpoint;
6477 struct breakpoint *breakpoint2;
6478};
6479
43ff13b4
JM
6480/* This function is called by fetch_inferior_event via the
6481 cmd_continuation pointer, to complete the until command. It takes
6482 care of cleaning up the temporary breakpoints set up by the until
6483 command. */
c2c6d25f 6484static void
604ead4a 6485until_break_command_continuation (void *arg)
43ff13b4 6486{
bfec99b2
PA
6487 struct until_break_command_continuation_args *a = arg;
6488
6489 delete_breakpoint (a->breakpoint);
6490 if (a->breakpoint2)
6491 delete_breakpoint (a->breakpoint2);
43ff13b4
JM
6492}
6493
c906108c 6494void
ae66c1fc 6495until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
6496{
6497 struct symtabs_and_lines sals;
6498 struct symtab_and_line sal;
206415a3 6499 struct frame_info *frame = get_selected_frame (NULL);
c906108c 6500 struct breakpoint *breakpoint;
f107f563 6501 struct breakpoint *breakpoint2 = NULL;
c906108c
SS
6502 struct cleanup *old_chain;
6503
6504 clear_proceed_status ();
6505
6506 /* Set a breakpoint where the user wants it and at return from
6507 this function */
c5aa993b 6508
c906108c
SS
6509 if (default_breakpoint_valid)
6510 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 6511 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 6512 else
53a5351d 6513 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 6514 0, (char ***) NULL, NULL);
c5aa993b 6515
c906108c 6516 if (sals.nelts != 1)
8a3fe4f8 6517 error (_("Couldn't get information on specified line."));
c5aa993b 6518
c906108c 6519 sal = sals.sals[0];
b8c9b27d 6520 xfree (sals.sals); /* malloc'd, so freed */
c5aa993b 6521
c906108c 6522 if (*arg)
8a3fe4f8 6523 error (_("Junk at end of arguments."));
c5aa993b 6524
c906108c 6525 resolve_sal_pc (&sal);
c5aa993b 6526
ae66c1fc
EZ
6527 if (anywhere)
6528 /* If the user told us to continue until a specified location,
6529 we don't specify a frame at which we need to stop. */
6530 breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
6531 else
edb3359d 6532 /* Otherwise, specify the selected frame, because we want to stop only
ae66c1fc 6533 at the very same frame. */
edb3359d 6534 breakpoint = set_momentary_breakpoint (sal, get_stack_frame_id (frame),
ae66c1fc 6535 bp_until);
c5aa993b 6536
f107f563 6537 old_chain = make_cleanup_delete_breakpoint (breakpoint);
c906108c 6538
ae66c1fc
EZ
6539 /* Keep within the current frame, or in frames called by the current
6540 one. */
edb3359d
DJ
6541
6542 if (frame_id_p (frame_unwind_caller_id (frame)))
c906108c 6543 {
edb3359d
DJ
6544 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
6545 sal.pc = frame_unwind_caller_pc (frame);
6546 breakpoint2 = set_momentary_breakpoint (sal,
6547 frame_unwind_caller_id (frame),
f107f563
VP
6548 bp_until);
6549 make_cleanup_delete_breakpoint (breakpoint2);
c906108c 6550 }
c5aa993b 6551
c906108c 6552 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563
VP
6553
6554 /* If we are running asynchronously, and proceed call above has actually
6555 managed to start the target, arrange for breakpoints to be
6556 deleted when the target stops. Otherwise, we're already stopped and
6557 delete breakpoints via cleanup chain. */
6558
8ea051c5 6559 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 6560 {
bfec99b2
PA
6561 struct until_break_command_continuation_args *args;
6562 args = xmalloc (sizeof (*args));
f107f563 6563
bfec99b2
PA
6564 args->breakpoint = breakpoint;
6565 args->breakpoint2 = breakpoint2;
f107f563
VP
6566
6567 discard_cleanups (old_chain);
95e54da7
PA
6568 add_continuation (inferior_thread (),
6569 until_break_command_continuation, args,
604ead4a 6570 xfree);
f107f563
VP
6571 }
6572 else
c5aa993b 6573 do_cleanups (old_chain);
c906108c 6574}
ae66c1fc 6575
c906108c 6576static void
fba45db2 6577ep_skip_leading_whitespace (char **s)
c906108c 6578{
c5aa993b
JM
6579 if ((s == NULL) || (*s == NULL))
6580 return;
6581 while (isspace (**s))
6582 *s += 1;
c906108c 6583}
c5aa993b 6584
c906108c
SS
6585/* This function attempts to parse an optional "if <cond>" clause
6586 from the arg string. If one is not found, it returns NULL.
c5aa993b 6587
c906108c
SS
6588 Else, it returns a pointer to the condition string. (It does not
6589 attempt to evaluate the string against a particular block.) And,
6590 it updates arg to point to the first character following the parsed
6591 if clause in the arg string. */
53a5351d 6592
c906108c 6593static char *
fba45db2 6594ep_parse_optional_if_clause (char **arg)
c906108c 6595{
c5aa993b
JM
6596 char *cond_string;
6597
6598 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 6599 return NULL;
c5aa993b 6600
c906108c
SS
6601 /* Skip the "if" keyword. */
6602 (*arg) += 2;
c5aa993b 6603
c906108c
SS
6604 /* Skip any extra leading whitespace, and record the start of the
6605 condition string. */
6606 ep_skip_leading_whitespace (arg);
6607 cond_string = *arg;
c5aa993b 6608
c906108c
SS
6609 /* Assume that the condition occupies the remainder of the arg string. */
6610 (*arg) += strlen (cond_string);
c5aa993b 6611
c906108c
SS
6612 return cond_string;
6613}
c5aa993b 6614
c906108c
SS
6615/* This function attempts to parse an optional filename from the arg
6616 string. If one is not found, it returns NULL.
c5aa993b 6617
c906108c
SS
6618 Else, it returns a pointer to the parsed filename. (This function
6619 makes no attempt to verify that a file of that name exists, or is
6620 accessible.) And, it updates arg to point to the first character
6621 following the parsed filename in the arg string.
c5aa993b 6622
c906108c
SS
6623 Note that clients needing to preserve the returned filename for
6624 future access should copy it to their own buffers. */
6625static char *
fba45db2 6626ep_parse_optional_filename (char **arg)
c906108c 6627{
c5aa993b
JM
6628 static char filename[1024];
6629 char *arg_p = *arg;
6630 int i;
6631 char c;
6632
c906108c
SS
6633 if ((*arg_p == '\0') || isspace (*arg_p))
6634 return NULL;
c5aa993b
JM
6635
6636 for (i = 0;; i++)
c906108c
SS
6637 {
6638 c = *arg_p;
6639 if (isspace (c))
c5aa993b 6640 c = '\0';
c906108c
SS
6641 filename[i] = c;
6642 if (c == '\0')
c5aa993b 6643 break;
c906108c
SS
6644 arg_p++;
6645 }
6646 *arg = arg_p;
c5aa993b 6647
c906108c
SS
6648 return filename;
6649}
c5aa993b 6650
c906108c
SS
6651/* Commands to deal with catching events, such as signals, exceptions,
6652 process start/exit, etc. */
c5aa993b
JM
6653
6654typedef enum
6655{
44feb3ce
TT
6656 catch_fork_temporary, catch_vfork_temporary,
6657 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
6658}
6659catch_fork_kind;
6660
c906108c 6661static void
44feb3ce 6662catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 6663{
c5aa993b 6664 char *cond_string = NULL;
44feb3ce
TT
6665 catch_fork_kind fork_kind;
6666 int tempflag;
6667
6668 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
6669 tempflag = (fork_kind == catch_fork_temporary
6670 || fork_kind == catch_vfork_temporary);
c5aa993b 6671
44feb3ce
TT
6672 if (!arg)
6673 arg = "";
c906108c 6674 ep_skip_leading_whitespace (&arg);
c5aa993b 6675
c906108c 6676 /* The allowed syntax is:
c5aa993b
JM
6677 catch [v]fork
6678 catch [v]fork if <cond>
6679
c906108c
SS
6680 First, check if there's an if clause. */
6681 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 6682
c906108c 6683 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6684 error (_("Junk at end of arguments."));
c5aa993b 6685
c906108c
SS
6686 /* If this target supports it, create a fork or vfork catchpoint
6687 and enable reporting of such events. */
c5aa993b
JM
6688 switch (fork_kind)
6689 {
44feb3ce
TT
6690 case catch_fork_temporary:
6691 case catch_fork_permanent:
ce78b96d
JB
6692 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6693 &catch_fork_breakpoint_ops);
c906108c 6694 break;
44feb3ce
TT
6695 case catch_vfork_temporary:
6696 case catch_vfork_permanent:
ce78b96d
JB
6697 create_fork_vfork_event_catchpoint (tempflag, cond_string,
6698 &catch_vfork_breakpoint_ops);
c906108c 6699 break;
c5aa993b 6700 default:
8a3fe4f8 6701 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 6702 break;
c5aa993b 6703 }
c906108c
SS
6704}
6705
6706static void
44feb3ce 6707catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
c906108c 6708{
44feb3ce 6709 int tempflag;
c5aa993b 6710 char *cond_string = NULL;
c906108c 6711
44feb3ce
TT
6712 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6713
6714 if (!arg)
6715 arg = "";
c906108c
SS
6716 ep_skip_leading_whitespace (&arg);
6717
6718 /* The allowed syntax is:
c5aa993b
JM
6719 catch exec
6720 catch exec if <cond>
c906108c
SS
6721
6722 First, check if there's an if clause. */
6723 cond_string = ep_parse_optional_if_clause (&arg);
6724
6725 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6726 error (_("Junk at end of arguments."));
c906108c
SS
6727
6728 /* If this target supports it, create an exec catchpoint
6729 and enable reporting of such events. */
fe798b75 6730 create_catchpoint (tempflag, cond_string, &catch_exec_breakpoint_ops);
c906108c 6731}
c5aa993b 6732
3086aeae
DJ
6733static enum print_stop_action
6734print_exception_catchpoint (struct breakpoint *b)
6735{
ade92717 6736 int bp_temp, bp_throw;
3086aeae 6737
ade92717 6738 annotate_catchpoint (b->number);
3086aeae 6739
ade92717
AR
6740 bp_throw = strstr (b->addr_string, "throw") != NULL;
6741 if (b->loc->address != b->loc->requested_address)
6742 breakpoint_adjustment_warning (b->loc->requested_address,
6743 b->loc->address,
6744 b->number, 1);
df2b6d2d 6745 bp_temp = b->disposition == disp_del;
ade92717
AR
6746 ui_out_text (uiout,
6747 bp_temp ? "Temporary catchpoint "
6748 : "Catchpoint ");
6749 if (!ui_out_is_mi_like_p (uiout))
6750 ui_out_field_int (uiout, "bkptno", b->number);
6751 ui_out_text (uiout,
c0b37c48
AR
6752 bp_throw ? " (exception thrown), "
6753 : " (exception caught), ");
ade92717
AR
6754 if (ui_out_is_mi_like_p (uiout))
6755 {
6756 ui_out_field_string (uiout, "reason",
6757 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
6758 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6759 ui_out_field_int (uiout, "bkptno", b->number);
6760 }
3086aeae
DJ
6761 return PRINT_SRC_AND_LOC;
6762}
6763
6764static void
6765print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6766{
79a45b7d
TT
6767 struct value_print_options opts;
6768 get_user_print_options (&opts);
6769 if (opts.addressprint)
3086aeae
DJ
6770 {
6771 annotate_field (4);
604133b5
AR
6772 if (b->loc == NULL || b->loc->shlib_disabled)
6773 ui_out_field_string (uiout, "addr", "<PENDING>");
6774 else
6775 ui_out_field_core_addr (uiout, "addr", b->loc->address);
3086aeae
DJ
6776 }
6777 annotate_field (5);
604133b5
AR
6778 if (b->loc)
6779 *last_addr = b->loc->address;
3086aeae
DJ
6780 if (strstr (b->addr_string, "throw") != NULL)
6781 ui_out_field_string (uiout, "what", "exception throw");
6782 else
6783 ui_out_field_string (uiout, "what", "exception catch");
6784}
6785
6786static void
6787print_mention_exception_catchpoint (struct breakpoint *b)
6788{
ade92717
AR
6789 int bp_temp;
6790 int bp_throw;
6791
df2b6d2d 6792 bp_temp = b->disposition == disp_del;
ade92717
AR
6793 bp_throw = strstr (b->addr_string, "throw") != NULL;
6794 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
6795 : _("Catchpoint "));
6796 ui_out_field_int (uiout, "bkptno", b->number);
6797 ui_out_text (uiout, bp_throw ? _(" (throw)")
6798 : _(" (catch)"));
3086aeae
DJ
6799}
6800
6801static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
ce78b96d
JB
6802 NULL, /* insert */
6803 NULL, /* remove */
6804 NULL, /* breakpoint_hit */
3086aeae
DJ
6805 print_exception_catchpoint,
6806 print_one_exception_catchpoint,
6807 print_mention_exception_catchpoint
6808};
6809
6810static int
6811handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6812 enum exception_event_kind ex_event, int from_tty)
6813{
604133b5
AR
6814 char *trigger_func_name;
6815
3086aeae 6816 if (ex_event == EX_EVENT_CATCH)
604133b5 6817 trigger_func_name = "__cxa_begin_catch";
3086aeae 6818 else
604133b5 6819 trigger_func_name = "__cxa_throw";
3086aeae 6820
604133b5
AR
6821 break_command_really (trigger_func_name, cond_string, -1,
6822 0 /* condition and thread are valid. */,
1042e4c0 6823 tempflag, 0, 0,
604133b5
AR
6824 0,
6825 AUTO_BOOLEAN_TRUE /* pending */,
41447f92
VP
6826 &gnu_v3_exception_catchpoint_ops, from_tty,
6827 1 /* enabled */);
3086aeae 6828
3086aeae
DJ
6829 return 1;
6830}
6831
c5aa993b 6832/* Deal with "catch catch" and "catch throw" commands */
c906108c
SS
6833
6834static void
fba45db2
KB
6835catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6836 int tempflag, int from_tty)
c906108c 6837{
c5aa993b
JM
6838 char *cond_string = NULL;
6839 struct symtab_and_line *sal = NULL;
6840
44feb3ce
TT
6841 if (!arg)
6842 arg = "";
c906108c 6843 ep_skip_leading_whitespace (&arg);
c5aa993b 6844
c906108c
SS
6845 cond_string = ep_parse_optional_if_clause (&arg);
6846
6847 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6848 error (_("Junk at end of arguments."));
c906108c 6849
059fb39f
PM
6850 if (ex_event != EX_EVENT_THROW
6851 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 6852 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 6853
3086aeae
DJ
6854 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6855 return;
6856
8a3fe4f8 6857 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
6858}
6859
44feb3ce
TT
6860/* Implementation of "catch catch" command. */
6861
6862static void
6863catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
6864{
6865 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6866 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
6867}
6868
6869/* Implementation of "catch throw" command. */
6870
6871static void
6872catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
6873{
6874 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6875 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
6876}
6877
f7f9143b
JB
6878/* Create a breakpoint struct for Ada exception catchpoints. */
6879
6880static void
6881create_ada_exception_breakpoint (struct symtab_and_line sal,
6882 char *addr_string,
6883 char *exp_string,
6884 char *cond_string,
6885 struct expression *cond,
6886 struct breakpoint_ops *ops,
6887 int tempflag,
6888 int from_tty)
6889{
6890 struct breakpoint *b;
6891
6892 if (from_tty)
6893 {
6894 describe_other_breakpoints (sal.pc, sal.section, -1);
6895 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
6896 version for exception catchpoints, because two catchpoints
6897 used for different exception names will use the same address.
6898 In this case, a "breakpoint ... also set at..." warning is
6899 unproductive. Besides. the warning phrasing is also a bit
6900 inapropriate, we should use the word catchpoint, and tell
6901 the user what type of catchpoint it is. The above is good
6902 enough for now, though. */
6903 }
6904
6905 b = set_raw_breakpoint (sal, bp_breakpoint);
6906 set_breakpoint_count (breakpoint_count + 1);
6907
6908 b->enable_state = bp_enabled;
6909 b->disposition = tempflag ? disp_del : disp_donttouch;
6910 b->number = breakpoint_count;
6911 b->ignore_count = 0;
511a6cd4 6912 b->loc->cond = cond;
f7f9143b
JB
6913 b->addr_string = addr_string;
6914 b->language = language_ada;
6915 b->cond_string = cond_string;
6916 b->exp_string = exp_string;
6917 b->thread = -1;
6918 b->ops = ops;
f7f9143b
JB
6919
6920 mention (b);
b60e7edf 6921 update_global_location_list (1);
f7f9143b
JB
6922}
6923
6924/* Implement the "catch exception" command. */
6925
6926static void
44feb3ce
TT
6927catch_ada_exception_command (char *arg, int from_tty,
6928 struct cmd_list_element *command)
f7f9143b 6929{
44feb3ce 6930 int tempflag;
f7f9143b
JB
6931 struct symtab_and_line sal;
6932 enum bptype type;
6933 char *addr_string = NULL;
6934 char *exp_string = NULL;
6935 char *cond_string = NULL;
6936 struct expression *cond = NULL;
6937 struct breakpoint_ops *ops = NULL;
6938
44feb3ce
TT
6939 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6940
6941 if (!arg)
6942 arg = "";
f7f9143b
JB
6943 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
6944 &cond_string, &cond, &ops);
6945 create_ada_exception_breakpoint (sal, addr_string, exp_string,
6946 cond_string, cond, ops, tempflag,
6947 from_tty);
6948}
6949
6950/* Implement the "catch assert" command. */
6951
6952static void
44feb3ce 6953catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
f7f9143b 6954{
44feb3ce 6955 int tempflag;
f7f9143b
JB
6956 struct symtab_and_line sal;
6957 char *addr_string = NULL;
6958 struct breakpoint_ops *ops = NULL;
6959
44feb3ce
TT
6960 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
6961
6962 if (!arg)
6963 arg = "";
f7f9143b
JB
6964 sal = ada_decode_assert_location (arg, &addr_string, &ops);
6965 create_ada_exception_breakpoint (sal, addr_string, NULL, NULL, NULL, ops,
6966 tempflag, from_tty);
6967}
6968
c906108c 6969static void
fba45db2 6970catch_command (char *arg, int from_tty)
c906108c 6971{
44feb3ce 6972 error (_("Catch requires an event name."));
c906108c
SS
6973}
6974\f
6975
6976static void
fba45db2 6977tcatch_command (char *arg, int from_tty)
c906108c 6978{
44feb3ce 6979 error (_("Catch requires an event name."));
c906108c
SS
6980}
6981
80f8a6eb 6982/* Delete breakpoints by address or line. */
c906108c
SS
6983
6984static void
fba45db2 6985clear_command (char *arg, int from_tty)
c906108c 6986{
d6e956e5
VP
6987 struct breakpoint *b;
6988 VEC(breakpoint_p) *found = 0;
6989 int ix;
c906108c
SS
6990 int default_match;
6991 struct symtabs_and_lines sals;
6992 struct symtab_and_line sal;
c906108c
SS
6993 int i;
6994
6995 if (arg)
6996 {
6997 sals = decode_line_spec (arg, 1);
6998 default_match = 0;
6999 }
7000 else
7001 {
c5aa993b 7002 sals.sals = (struct symtab_and_line *)
c906108c 7003 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 7004 make_cleanup (xfree, sals.sals);
fe39c653 7005 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
7006 sal.line = default_breakpoint_line;
7007 sal.symtab = default_breakpoint_symtab;
7008 sal.pc = default_breakpoint_address;
7009 if (sal.symtab == 0)
8a3fe4f8 7010 error (_("No source file specified."));
c906108c
SS
7011
7012 sals.sals[0] = sal;
7013 sals.nelts = 1;
7014
7015 default_match = 1;
7016 }
7017
ed0616c6
VP
7018 /* We don't call resolve_sal_pc here. That's not
7019 as bad as it seems, because all existing breakpoints
7020 typically have both file/line and pc set. So, if
7021 clear is given file/line, we can match this to existing
7022 breakpoint without obtaining pc at all.
7023
7024 We only support clearing given the address explicitly
7025 present in breakpoint table. Say, we've set breakpoint
7026 at file:line. There were several PC values for that file:line,
7027 due to optimization, all in one block.
7028 We've picked one PC value. If "clear" is issued with another
7029 PC corresponding to the same file:line, the breakpoint won't
7030 be cleared. We probably can still clear the breakpoint, but
7031 since the other PC value is never presented to user, user
7032 can only find it by guessing, and it does not seem important
7033 to support that. */
7034
c906108c 7035 /* For each line spec given, delete bps which correspond
80f8a6eb
MS
7036 to it. Do it in two passes, solely to preserve the current
7037 behavior that from_tty is forced true if we delete more than
7038 one breakpoint. */
c906108c 7039
80f8a6eb 7040 found = NULL;
c906108c
SS
7041 for (i = 0; i < sals.nelts; i++)
7042 {
7043 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
7044 If line given (pc == 0), clear all bpts on specified line.
7045 If defaulting, clear all bpts on default line
c906108c 7046 or at default pc.
c5aa993b
JM
7047
7048 defaulting sal.pc != 0 tests to do
7049
7050 0 1 pc
7051 1 1 pc _and_ line
7052 0 0 line
7053 1 0 <can't happen> */
c906108c
SS
7054
7055 sal = sals.sals[i];
c906108c 7056
d6e956e5
VP
7057 /* Find all matching breakpoints and add them to
7058 'found'. */
7059 ALL_BREAKPOINTS (b)
c5aa993b 7060 {
0d381245 7061 int match = 0;
80f8a6eb
MS
7062 /* Are we going to delete b? */
7063 if (b->type != bp_none
7064 && b->type != bp_watchpoint
7065 && b->type != bp_hardware_watchpoint
7066 && b->type != bp_read_watchpoint
0d381245
VP
7067 && b->type != bp_access_watchpoint)
7068 {
7069 struct bp_location *loc = b->loc;
7070 for (; loc; loc = loc->next)
7071 {
7072 int pc_match = sal.pc
7073 && (loc->address == sal.pc)
7074 && (!section_is_overlay (loc->section)
7075 || loc->section == sal.section);
7076 int line_match = ((default_match || (0 == sal.pc))
7077 && b->source_file != NULL
7078 && sal.symtab != NULL
7079 && strcmp (b->source_file, sal.symtab->filename) == 0
7080 && b->line_number == sal.line);
7081 if (pc_match || line_match)
7082 {
7083 match = 1;
7084 break;
7085 }
7086 }
7087 }
7088
7089 if (match)
d6e956e5 7090 VEC_safe_push(breakpoint_p, found, b);
c906108c 7091 }
80f8a6eb
MS
7092 }
7093 /* Now go thru the 'found' chain and delete them. */
d6e956e5 7094 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
7095 {
7096 if (arg)
8a3fe4f8 7097 error (_("No breakpoint at %s."), arg);
80f8a6eb 7098 else
8a3fe4f8 7099 error (_("No breakpoint at this line."));
80f8a6eb 7100 }
c906108c 7101
d6e956e5 7102 if (VEC_length(breakpoint_p, found) > 1)
80f8a6eb
MS
7103 from_tty = 1; /* Always report if deleted more than one */
7104 if (from_tty)
a3f17187 7105 {
d6e956e5 7106 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
7107 printf_unfiltered (_("Deleted breakpoint "));
7108 else
7109 printf_unfiltered (_("Deleted breakpoints "));
7110 }
80f8a6eb 7111 breakpoints_changed ();
d6e956e5
VP
7112
7113 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 7114 {
c5aa993b 7115 if (from_tty)
d6e956e5
VP
7116 printf_unfiltered ("%d ", b->number);
7117 delete_breakpoint (b);
c906108c 7118 }
80f8a6eb
MS
7119 if (from_tty)
7120 putchar_unfiltered ('\n');
c906108c
SS
7121}
7122\f
7123/* Delete breakpoint in BS if they are `delete' breakpoints and
7124 all breakpoints that are marked for deletion, whether hit or not.
7125 This is called after any breakpoint is hit, or after errors. */
7126
7127void
fba45db2 7128breakpoint_auto_delete (bpstat bs)
c906108c
SS
7129{
7130 struct breakpoint *b, *temp;
7131
7132 for (; bs; bs = bs->next)
20874c92
VP
7133 if (bs->breakpoint_at
7134 && bs->breakpoint_at->owner
7135 && bs->breakpoint_at->owner->disposition == disp_del
c906108c 7136 && bs->stop)
4f8d1dc6 7137 delete_breakpoint (bs->breakpoint_at->owner);
c906108c
SS
7138
7139 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7140 {
b5de0fa7 7141 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
7142 delete_breakpoint (b);
7143 }
c906108c
SS
7144}
7145
f7545552
TT
7146/* A cleanup function which destroys a vector. */
7147
7148static void
7149do_vec_free (void *p)
7150{
7151 VEC(bp_location_p) **vec = p;
7152 if (*vec)
7153 VEC_free (bp_location_p, *vec);
7154}
7155
4cd9bd08 7156/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
7157 into the inferior, only remove already-inserted locations that no
7158 longer should be inserted. Functions that delete a breakpoint or
7159 breakpoints should pass false, so that deleting a breakpoint
7160 doesn't have the side effect of inserting the locations of other
7161 breakpoints that are marked not-inserted, but should_be_inserted
7162 returns true on them.
7163
7164 This behaviour is useful is situations close to tear-down -- e.g.,
7165 after an exec, while the target still has execution, but breakpoint
7166 shadows of the previous executable image should *NOT* be restored
7167 to the new image; or before detaching, where the target still has
7168 execution and wants to delete breakpoints from GDB's lists, and all
7169 breakpoints had already been removed from the inferior. */
7170
0d381245 7171static void
b60e7edf 7172update_global_location_list (int should_insert)
0d381245 7173{
74960c60
VP
7174 struct breakpoint *b;
7175 struct bp_location **next = &bp_location_chain;
7176 struct bp_location *loc;
7177 struct bp_location *loc2;
74960c60
VP
7178 VEC(bp_location_p) *old_locations = NULL;
7179 int ret;
7180 int ix;
f7545552
TT
7181 struct cleanup *cleanups;
7182
7183 cleanups = make_cleanup (do_vec_free, &old_locations);
74960c60
VP
7184 /* Store old locations for future reference. */
7185 for (loc = bp_location_chain; loc; loc = loc->global_next)
7186 VEC_safe_push (bp_location_p, old_locations, loc);
0d381245 7187
74960c60
VP
7188 bp_location_chain = NULL;
7189 ALL_BREAKPOINTS (b)
0d381245 7190 {
74960c60 7191 for (loc = b->loc; loc; loc = loc->next)
0d381245 7192 {
74960c60
VP
7193 *next = loc;
7194 next = &(loc->global_next);
7195 *next = NULL;
0d381245 7196 }
74960c60
VP
7197 }
7198
7199 /* Identify bp_location instances that are no longer present in the new
7200 list, and therefore should be freed. Note that it's not necessary that
7201 those locations should be removed from inferior -- if there's another
7202 location at the same address (previously marked as duplicate),
7203 we don't need to remove/insert the location. */
7204 for (ix = 0; VEC_iterate(bp_location_p, old_locations, ix, loc); ++ix)
7205 {
7206 /* Tells if 'loc' is found amoung the new locations. If not, we
7207 have to free it. */
7208 int found_object = 0;
20874c92
VP
7209 /* Tells if the location should remain inserted in the target. */
7210 int keep_in_target = 0;
7211 int removed = 0;
74960c60
VP
7212 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7213 if (loc2 == loc)
7214 {
7215 found_object = 1;
7216 break;
7217 }
7218
7219 /* If this location is no longer present, and inserted, look if there's
7220 maybe a new location at the same address. If so, mark that one
7221 inserted, and don't remove this one. This is needed so that we
7222 don't have a time window where a breakpoint at certain location is not
7223 inserted. */
7224
7225 if (loc->inserted)
0d381245 7226 {
74960c60 7227 /* If the location is inserted now, we might have to remove it. */
74960c60
VP
7228
7229 if (found_object && should_be_inserted (loc))
7230 {
7231 /* The location is still present in the location list, and still
7232 should be inserted. Don't do anything. */
20874c92 7233 keep_in_target = 1;
74960c60
VP
7234 }
7235 else
7236 {
7237 /* The location is either no longer present, or got disabled.
7238 See if there's another location at the same address, in which
7239 case we don't need to remove this one from the target. */
7240 if (breakpoint_address_is_meaningful (loc->owner))
7241 for (loc2 = bp_location_chain; loc2; loc2 = loc2->global_next)
7242 {
7243 /* For the sake of should_insert_location. The
7244 call to check_duplicates will fix up this later. */
7245 loc2->duplicate = 0;
7246 if (should_be_inserted (loc2)
7247 && loc2 != loc && loc2->address == loc->address)
7248 {
7249 loc2->inserted = 1;
7250 loc2->target_info = loc->target_info;
20874c92 7251 keep_in_target = 1;
74960c60
VP
7252 break;
7253 }
7254 }
7255 }
7256
20874c92
VP
7257 if (!keep_in_target)
7258 {
7259 if (remove_breakpoint (loc, mark_uninserted))
7260 {
7261 /* This is just about all we can do. We could keep this
7262 location on the global list, and try to remove it next
7263 time, but there's no particular reason why we will
7264 succeed next time.
7265
7266 Note that at this point, loc->owner is still valid,
7267 as delete_breakpoint frees the breakpoint only
7268 after calling us. */
7269 printf_filtered (_("warning: Error removing breakpoint %d\n"),
7270 loc->owner->number);
7271 }
7272 removed = 1;
7273 }
0d381245 7274 }
74960c60
VP
7275
7276 if (!found_object)
1c5cfe86
PA
7277 {
7278 if (removed && non_stop)
20874c92
VP
7279 {
7280 /* This location was removed from the targets. In non-stop mode,
7281 a race condition is possible where we've removed a breakpoint,
7282 but stop events for that breakpoint are already queued and will
7283 arrive later. To suppress spurious SIGTRAPs reported to user,
7284 we keep this breakpoint location for a bit, and will retire it
7285 after we see 3 * thread_count events.
1c5cfe86 7286 The theory here is that reporting of events should,
20874c92
VP
7287 "on the average", be fair, so after that many event we'll see
7288 events from all threads that have anything of interest, and no
1c5cfe86 7289 longer need to keep this breakpoint. This is just a
20874c92 7290 heuristic, but if it's wrong, we'll report unexpected SIGTRAP,
1c5cfe86 7291 which is usability issue, but not a correctness problem. */
20874c92
VP
7292 loc->events_till_retirement = 3 * (thread_count () + 1);
7293 loc->owner = NULL;
20874c92 7294
1c5cfe86
PA
7295 VEC_safe_push (bp_location_p, moribund_locations, loc);
7296 }
7297 else
7298 free_bp_location (loc);
20874c92 7299 }
74960c60 7300 }
1c5cfe86 7301
74960c60
VP
7302 ALL_BREAKPOINTS (b)
7303 {
7304 check_duplicates (b);
0d381245 7305 }
74960c60 7306
50c71eaf 7307 if (breakpoints_always_inserted_mode () && should_insert
c35b1492 7308 && (have_live_inferiors ()
2567c7d9 7309 || (gdbarch_has_global_breakpoints (target_gdbarch))))
74960c60 7310 insert_breakpoint_locations ();
f7545552
TT
7311
7312 do_cleanups (cleanups);
74960c60
VP
7313}
7314
20874c92
VP
7315void
7316breakpoint_retire_moribund (void)
7317{
7318 struct bp_location *loc;
7319 int ix;
7320
7321 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
7322 if (--(loc->events_till_retirement) == 0)
7323 {
7324 free_bp_location (loc);
7325 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
7326 --ix;
7327 }
7328}
7329
74960c60 7330static void
b60e7edf 7331update_global_location_list_nothrow (int inserting)
74960c60
VP
7332{
7333 struct gdb_exception e;
7334 TRY_CATCH (e, RETURN_MASK_ERROR)
b60e7edf 7335 update_global_location_list (inserting);
0d381245
VP
7336}
7337
a474d7c2
PA
7338/* Clear BPT from a BPS. */
7339static void
7340bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
7341{
7342 bpstat bs;
7343 for (bs = bps; bs; bs = bs->next)
7344 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
7345 {
7346 bs->breakpoint_at = NULL;
7347 bs->old_val = NULL;
7348 /* bs->commands will be freed later. */
7349 }
7350}
7351
7352/* Callback for iterate_over_threads. */
7353static int
7354bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
7355{
7356 struct breakpoint *bpt = data;
7357 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
7358 return 0;
7359}
7360
53a5351d
JM
7361/* Delete a breakpoint and clean up all traces of it in the data
7362 structures. */
c906108c
SS
7363
7364void
fba45db2 7365delete_breakpoint (struct breakpoint *bpt)
c906108c 7366{
52f0bd74 7367 struct breakpoint *b;
74960c60 7368 struct bp_location *loc, *next;
c906108c 7369
8a3fe4f8 7370 gdb_assert (bpt != NULL);
c906108c
SS
7371
7372 /* Has this bp already been deleted? This can happen because multiple
7373 lists can hold pointers to bp's. bpstat lists are especial culprits.
7374
7375 One example of this happening is a watchpoint's scope bp. When the
7376 scope bp triggers, we notice that the watchpoint is out of scope, and
7377 delete it. We also delete its scope bp. But the scope bp is marked
7378 "auto-deleting", and is already on a bpstat. That bpstat is then
7379 checked for auto-deleting bp's, which are deleted.
7380
7381 A real solution to this problem might involve reference counts in bp's,
7382 and/or giving them pointers back to their referencing bpstat's, and
7383 teaching delete_breakpoint to only free a bp's storage when no more
1272ad14 7384 references were extent. A cheaper bandaid was chosen. */
c906108c
SS
7385 if (bpt->type == bp_none)
7386 return;
7387
383f836e 7388 observer_notify_breakpoint_deleted (bpt->number);
c906108c 7389
c906108c
SS
7390 if (breakpoint_chain == bpt)
7391 breakpoint_chain = bpt->next;
7392
c906108c
SS
7393 ALL_BREAKPOINTS (b)
7394 if (b->next == bpt)
c5aa993b
JM
7395 {
7396 b->next = bpt->next;
7397 break;
7398 }
c906108c 7399
c906108c 7400 free_command_lines (&bpt->commands);
c906108c 7401 if (bpt->cond_string != NULL)
b8c9b27d 7402 xfree (bpt->cond_string);
c906108c 7403 if (bpt->addr_string != NULL)
b8c9b27d 7404 xfree (bpt->addr_string);
c906108c 7405 if (bpt->exp != NULL)
b8c9b27d 7406 xfree (bpt->exp);
c906108c 7407 if (bpt->exp_string != NULL)
b8c9b27d 7408 xfree (bpt->exp_string);
c906108c
SS
7409 if (bpt->val != NULL)
7410 value_free (bpt->val);
7411 if (bpt->source_file != NULL)
b8c9b27d 7412 xfree (bpt->source_file);
c906108c 7413 if (bpt->exec_pathname != NULL)
b8c9b27d 7414 xfree (bpt->exec_pathname);
c906108c
SS
7415
7416 /* Be sure no bpstat's are pointing at it after it's been freed. */
7417 /* FIXME, how can we find all bpstat's?
198757a8
VP
7418 We just check stop_bpstat for now. Note that we cannot just
7419 remove bpstats pointing at bpt from the stop_bpstat list
7420 entirely, as breakpoint commands are associated with the bpstat;
7421 if we remove it here, then the later call to
7422 bpstat_do_actions (&stop_bpstat);
7423 in event-top.c won't do anything, and temporary breakpoints
7424 with commands won't work. */
a474d7c2 7425
a474d7c2 7426 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
74960c60
VP
7427
7428 /* Now that breakpoint is removed from breakpoint
7429 list, update the global location list. This
7430 will remove locations that used to belong to
7431 this breakpoint. Do this before freeing
7432 the breakpoint itself, since remove_breakpoint
7433 looks at location's owner. It might be better
7434 design to have location completely self-contained,
7435 but it's not the case now. */
b60e7edf 7436 update_global_location_list (0);
74960c60
VP
7437
7438
c906108c
SS
7439 /* On the chance that someone will soon try again to delete this same
7440 bp, we mark it as deleted before freeing its storage. */
7441 bpt->type = bp_none;
7442
b8c9b27d 7443 xfree (bpt);
c906108c
SS
7444}
7445
4d6140d9
AC
7446static void
7447do_delete_breakpoint_cleanup (void *b)
7448{
7449 delete_breakpoint (b);
7450}
7451
7452struct cleanup *
7453make_cleanup_delete_breakpoint (struct breakpoint *b)
7454{
7455 return make_cleanup (do_delete_breakpoint_cleanup, b);
7456}
7457
c906108c 7458void
fba45db2 7459delete_command (char *arg, int from_tty)
c906108c
SS
7460{
7461 struct breakpoint *b, *temp;
7462
ea9365bb
TT
7463 dont_repeat ();
7464
c906108c
SS
7465 if (arg == 0)
7466 {
7467 int breaks_to_delete = 0;
7468
7469 /* Delete all breakpoints if no argument.
c5aa993b
JM
7470 Do not delete internal or call-dummy breakpoints, these
7471 have to be deleted with an explicit breakpoint number argument. */
7472 ALL_BREAKPOINTS (b)
7473 {
059fb39f
PM
7474 if (b->type != bp_call_dummy
7475 && b->type != bp_shlib_event
7476 && b->type != bp_thread_event
7477 && b->type != bp_overlay_event
0fd8e87f 7478 && b->type != bp_longjmp_master
059fb39f 7479 && b->number >= 0)
973d738b
DJ
7480 {
7481 breaks_to_delete = 1;
7482 break;
7483 }
c5aa993b 7484 }
c906108c
SS
7485
7486 /* Ask user only if there are some breakpoints to delete. */
7487 if (!from_tty
e2e0b3e5 7488 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 7489 {
c5aa993b
JM
7490 ALL_BREAKPOINTS_SAFE (b, temp)
7491 {
059fb39f
PM
7492 if (b->type != bp_call_dummy
7493 && b->type != bp_shlib_event
7494 && b->type != bp_thread_event
7495 && b->type != bp_overlay_event
0fd8e87f 7496 && b->type != bp_longjmp_master
059fb39f 7497 && b->number >= 0)
c5aa993b
JM
7498 delete_breakpoint (b);
7499 }
c906108c
SS
7500 }
7501 }
7502 else
7503 map_breakpoint_numbers (arg, delete_breakpoint);
7504}
7505
0d381245
VP
7506static int
7507all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 7508{
0d381245
VP
7509 for (; loc; loc = loc->next)
7510 if (!loc->shlib_disabled)
7511 return 0;
7512 return 1;
fe3f5fa8
VP
7513}
7514
776592bf
DE
7515/* Subroutine of update_breakpoint_locations to simplify it.
7516 Return non-zero if multiple fns in list LOC have the same name.
7517 Null names are ignored. */
7518
7519static int
7520ambiguous_names_p (struct bp_location *loc)
7521{
7522 struct bp_location *l;
7523 htab_t htab = htab_create_alloc (13, htab_hash_string,
7524 (int (*) (const void *, const void *)) streq,
7525 NULL, xcalloc, xfree);
7526
7527 for (l = loc; l != NULL; l = l->next)
7528 {
7529 const char **slot;
7530 const char *name = l->function_name;
7531
7532 /* Allow for some names to be NULL, ignore them. */
7533 if (name == NULL)
7534 continue;
7535
7536 slot = (const char **) htab_find_slot (htab, (const void *) name,
7537 INSERT);
7538 /* NOTE: We can assume slot != NULL here because xcalloc never returns
7539 NULL. */
7540 if (*slot != NULL)
7541 {
7542 htab_delete (htab);
7543 return 1;
7544 }
7545 *slot = name;
7546 }
7547
7548 htab_delete (htab);
7549 return 0;
7550}
7551
fe3f5fa8 7552static void
0d381245
VP
7553update_breakpoint_locations (struct breakpoint *b,
7554 struct symtabs_and_lines sals)
fe3f5fa8
VP
7555{
7556 int i;
7557 char *s;
0d381245
VP
7558 struct bp_location *existing_locations = b->loc;
7559
7560 /* If there's no new locations, and all existing locations
7561 are pending, don't do anything. This optimizes
7562 the common case where all locations are in the same
7563 shared library, that was unloaded. We'd like to
7564 retain the location, so that when the library
7565 is loaded again, we don't loose the enabled/disabled
7566 status of the individual locations. */
7567 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
7568 return;
7569
fe3f5fa8
VP
7570 b->loc = NULL;
7571
0d381245 7572 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 7573 {
0d381245 7574 struct bp_location *new_loc =
39d61571 7575 add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 7576
0d381245
VP
7577 /* Reparse conditions, they might contain references to the
7578 old symtab. */
7579 if (b->cond_string != NULL)
7580 {
7581 struct gdb_exception e;
fe3f5fa8 7582
0d381245
VP
7583 s = b->cond_string;
7584 TRY_CATCH (e, RETURN_MASK_ERROR)
7585 {
7586 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7587 0);
7588 }
7589 if (e.reason < 0)
7590 {
7591 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7592 b->number, e.message);
7593 new_loc->enabled = 0;
7594 }
7595 }
fe3f5fa8 7596
0d381245
VP
7597 if (b->source_file != NULL)
7598 xfree (b->source_file);
7599 if (sals.sals[i].symtab == NULL)
7600 b->source_file = NULL;
7601 else
1b36a34b 7602 b->source_file = xstrdup (sals.sals[i].symtab->filename);
fe3f5fa8 7603
0d381245
VP
7604 if (b->line_number == 0)
7605 b->line_number = sals.sals[i].line;
7606 }
fe3f5fa8 7607
514f746b
AR
7608 /* Update locations of permanent breakpoints. */
7609 if (b->enable_state == bp_permanent)
7610 make_breakpoint_permanent (b);
7611
0d381245
VP
7612 /* If possible, carry over 'disable' status from existing breakpoints. */
7613 {
7614 struct bp_location *e = existing_locations;
776592bf
DE
7615 /* If there are multiple breakpoints with the same function name,
7616 e.g. for inline functions, comparing function names won't work.
7617 Instead compare pc addresses; this is just a heuristic as things
7618 may have moved, but in practice it gives the correct answer
7619 often enough until a better solution is found. */
7620 int have_ambiguous_names = ambiguous_names_p (b->loc);
7621
0d381245
VP
7622 for (; e; e = e->next)
7623 {
7624 if (!e->enabled && e->function_name)
7625 {
7626 struct bp_location *l = b->loc;
776592bf
DE
7627 if (have_ambiguous_names)
7628 {
7629 for (; l; l = l->next)
7630 if (e->address == l->address)
7631 {
7632 l->enabled = 0;
7633 break;
7634 }
7635 }
7636 else
7637 {
7638 for (; l; l = l->next)
7639 if (l->function_name
7640 && strcmp (e->function_name, l->function_name) == 0)
7641 {
7642 l->enabled = 0;
7643 break;
7644 }
7645 }
0d381245
VP
7646 }
7647 }
7648 }
fe3f5fa8 7649
b60e7edf 7650 update_global_location_list (1);
fe3f5fa8
VP
7651}
7652
7653
c906108c
SS
7654/* Reset a breakpoint given it's struct breakpoint * BINT.
7655 The value we return ends up being the return value from catch_errors.
7656 Unused in this case. */
7657
7658static int
4efb68b1 7659breakpoint_re_set_one (void *bint)
c906108c 7660{
53a5351d
JM
7661 /* get past catch_errs */
7662 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
7663 struct value *mark;
7664 int i;
fe3f5fa8
VP
7665 int not_found = 0;
7666 int *not_found_ptr = &not_found;
7667 struct symtabs_and_lines sals = {};
ed0616c6 7668 struct symtabs_and_lines expanded;
c906108c 7669 char *s;
b5de0fa7 7670 enum enable_state save_enable;
fe3f5fa8 7671 struct gdb_exception e;
f7545552 7672 struct cleanup *cleanups;
c906108c
SS
7673
7674 switch (b->type)
7675 {
7676 case bp_none:
8a3fe4f8 7677 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 7678 b->number);
c906108c
SS
7679 return 0;
7680 case bp_breakpoint:
7681 case bp_hardware_breakpoint:
1042e4c0 7682 case bp_tracepoint:
c906108c
SS
7683 if (b->addr_string == NULL)
7684 {
7685 /* Anything without a string can't be re-set. */
7686 delete_breakpoint (b);
7687 return 0;
7688 }
c906108c
SS
7689
7690 set_language (b->language);
7691 input_radix = b->input_radix;
7692 s = b->addr_string;
fe3f5fa8 7693 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 7694 {
fe3f5fa8
VP
7695 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7696 not_found_ptr);
7697 }
7698 if (e.reason < 0)
7699 {
7700 int not_found_and_ok = 0;
7701 /* For pending breakpoints, it's expected that parsing
7702 will fail until the right shared library is loaded.
7703 User has already told to create pending breakpoints and
7704 don't need extra messages. If breakpoint is in bp_shlib_disabled
7705 state, then user already saw the message about that breakpoint
7706 being disabled, and don't want to see more errors. */
0d381245
VP
7707 if (not_found
7708 && (b->condition_not_parsed
7709 || (b->loc && b->loc->shlib_disabled)
7710 || b->enable_state == bp_disabled))
fe3f5fa8
VP
7711 not_found_and_ok = 1;
7712
7713 if (!not_found_and_ok)
c906108c 7714 {
fe3f5fa8
VP
7715 /* We surely don't want to warn about the same breakpoint
7716 10 times. One solution, implemented here, is disable
7717 the breakpoint on error. Another solution would be to
7718 have separate 'warning emitted' flag. Since this
7719 happens only when a binary has changed, I don't know
7720 which approach is better. */
7721 b->enable_state = bp_disabled;
7722 throw_exception (e);
c906108c 7723 }
fe3f5fa8 7724 }
c906108c 7725
fe3f5fa8
VP
7726 if (not_found)
7727 break;
7728
7729 gdb_assert (sals.nelts == 1);
7730 resolve_sal_pc (&sals.sals[0]);
0d381245 7731 if (b->condition_not_parsed && s && s[0])
fe3f5fa8
VP
7732 {
7733 char *cond_string = 0;
7734 int thread = -1;
4a306c9a
JB
7735 int task = 0;
7736
fe3f5fa8 7737 find_condition_and_thread (s, sals.sals[0].pc,
4a306c9a 7738 &cond_string, &thread, &task);
fe3f5fa8
VP
7739 if (cond_string)
7740 b->cond_string = cond_string;
7741 b->thread = thread;
4a306c9a 7742 b->task = task;
0d381245 7743 b->condition_not_parsed = 0;
fe3f5fa8 7744 }
ed0616c6 7745 expanded = expand_line_sal_maybe (sals.sals[0]);
f7545552 7746 cleanups = make_cleanup (xfree, sals.sals);
ed0616c6 7747 update_breakpoint_locations (b, expanded);
f7545552 7748 do_cleanups (cleanups);
c906108c
SS
7749 break;
7750
7751 case bp_watchpoint:
7752 case bp_hardware_watchpoint:
7753 case bp_read_watchpoint:
7754 case bp_access_watchpoint:
0b3de036
VP
7755 /* Watchpoint can be either on expression using entirely global variables,
7756 or it can be on local variables.
7757
7758 Watchpoints of the first kind are never auto-deleted, and even persist
7759 across program restarts. Since they can use variables from shared
7760 libraries, we need to reparse expression as libraries are loaded
7761 and unloaded.
7762
7763 Watchpoints on local variables can also change meaning as result
7764 of solib event. For example, if a watchpoint uses both a local and
7765 a global variables in expression, it's a local watchpoint, but
7766 unloading of a shared library will make the expression invalid.
7767 This is not a very common use case, but we still re-evaluate
7768 expression, to avoid surprises to the user.
7769
7770 Note that for local watchpoints, we re-evaluate it only if
7771 watchpoints frame id is still valid. If it's not, it means
7772 the watchpoint is out of scope and will be deleted soon. In fact,
7773 I'm not sure we'll ever be called in this case.
7774
7775 If a local watchpoint's frame id is still valid, then
7776 b->exp_valid_block is likewise valid, and we can safely use it.
7777
7778 Don't do anything about disabled watchpoints, since they will
7779 be reevaluated again when enabled. */
a5606eee 7780 update_watchpoint (b, 1 /* reparse */);
c906108c 7781 break;
c5aa993b
JM
7782 /* We needn't really do anything to reset these, since the mask
7783 that requests them is unaffected by e.g., new libraries being
7784 loaded. */
ce78b96d 7785 case bp_catchpoint:
c906108c 7786 break;
c5aa993b 7787
c906108c 7788 default:
a3f17187 7789 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 7790 /* fall through */
0fd8e87f
UW
7791 /* Delete overlay event and longjmp master breakpoints; they will be
7792 reset later by breakpoint_re_set. */
1900040c 7793 case bp_overlay_event:
0fd8e87f 7794 case bp_longjmp_master:
c906108c
SS
7795 delete_breakpoint (b);
7796 break;
7797
c5aa993b
JM
7798 /* This breakpoint is special, it's set up when the inferior
7799 starts and we really don't want to touch it. */
c906108c
SS
7800 case bp_shlib_event:
7801
c4093a6a
JM
7802 /* Like bp_shlib_event, this breakpoint type is special.
7803 Once it is set up, we do not want to touch it. */
7804 case bp_thread_event:
7805
c5aa993b
JM
7806 /* Keep temporary breakpoints, which can be encountered when we step
7807 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7808 Otherwise these should have been blown away via the cleanup chain
7809 or by breakpoint_init_inferior when we rerun the executable. */
c906108c
SS
7810 case bp_until:
7811 case bp_finish:
7812 case bp_watchpoint_scope:
7813 case bp_call_dummy:
7814 case bp_step_resume:
611c83ae
PA
7815 case bp_longjmp:
7816 case bp_longjmp_resume:
c906108c
SS
7817 break;
7818 }
7819
7820 return 0;
7821}
7822
69de3c6a 7823/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 7824void
69de3c6a 7825breakpoint_re_set (void)
c906108c
SS
7826{
7827 struct breakpoint *b, *temp;
7828 enum language save_language;
7829 int save_input_radix;
c5aa993b 7830
c906108c
SS
7831 save_language = current_language->la_language;
7832 save_input_radix = input_radix;
7833 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7834 {
53a5351d 7835 /* Format possible error msg */
fe3f5fa8 7836 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
7837 b->number);
7838 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 7839 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 7840 do_cleanups (cleanups);
c5aa993b 7841 }
c906108c
SS
7842 set_language (save_language);
7843 input_radix = save_input_radix;
e62c965a 7844
69de3c6a 7845 create_overlay_event_breakpoint ("_ovly_debug_event");
0fd8e87f
UW
7846 create_longjmp_master_breakpoint ("longjmp");
7847 create_longjmp_master_breakpoint ("_longjmp");
7848 create_longjmp_master_breakpoint ("siglongjmp");
7849 create_longjmp_master_breakpoint ("_siglongjmp");
c906108c
SS
7850}
7851\f
c906108c
SS
7852/* Reset the thread number of this breakpoint:
7853
7854 - If the breakpoint is for all threads, leave it as-is.
39f77062 7855 - Else, reset it to the current thread for inferior_ptid. */
c906108c 7856void
fba45db2 7857breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
7858{
7859 if (b->thread != -1)
7860 {
39f77062
KB
7861 if (in_thread_list (inferior_ptid))
7862 b->thread = pid_to_thread_id (inferior_ptid);
c906108c
SS
7863 }
7864}
7865
03ac34d5
MS
7866/* Set ignore-count of breakpoint number BPTNUM to COUNT.
7867 If from_tty is nonzero, it prints a message to that effect,
7868 which ends with a period (no newline). */
7869
c906108c 7870void
fba45db2 7871set_ignore_count (int bptnum, int count, int from_tty)
c906108c 7872{
52f0bd74 7873 struct breakpoint *b;
c906108c
SS
7874
7875 if (count < 0)
7876 count = 0;
7877
7878 ALL_BREAKPOINTS (b)
7879 if (b->number == bptnum)
c5aa993b
JM
7880 {
7881 b->ignore_count = count;
221ea385
KS
7882 if (from_tty)
7883 {
7884 if (count == 0)
a3f17187 7885 printf_filtered (_("Will stop next time breakpoint %d is reached."),
221ea385
KS
7886 bptnum);
7887 else if (count == 1)
a3f17187 7888 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
7889 bptnum);
7890 else
a3f17187 7891 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
221ea385
KS
7892 count, bptnum);
7893 }
c5aa993b 7894 breakpoints_changed ();
383f836e 7895 observer_notify_breakpoint_modified (b->number);
c5aa993b
JM
7896 return;
7897 }
c906108c 7898
8a3fe4f8 7899 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
7900}
7901
b2175913
MS
7902void
7903make_breakpoint_silent (struct breakpoint *b)
7904{
7905 /* Silence the breakpoint. */
7906 b->silent = 1;
7907}
7908
c906108c
SS
7909/* Command to set ignore-count of breakpoint N to COUNT. */
7910
7911static void
fba45db2 7912ignore_command (char *args, int from_tty)
c906108c
SS
7913{
7914 char *p = args;
52f0bd74 7915 int num;
c906108c
SS
7916
7917 if (p == 0)
e2e0b3e5 7918 error_no_arg (_("a breakpoint number"));
c5aa993b 7919
c906108c 7920 num = get_number (&p);
5c44784c 7921 if (num == 0)
8a3fe4f8 7922 error (_("bad breakpoint number: '%s'"), args);
c906108c 7923 if (*p == 0)
8a3fe4f8 7924 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
7925
7926 set_ignore_count (num,
7927 longest_to_int (value_as_long (parse_and_eval (p))),
7928 from_tty);
221ea385
KS
7929 if (from_tty)
7930 printf_filtered ("\n");
c906108c
SS
7931}
7932\f
7933/* Call FUNCTION on each of the breakpoints
7934 whose numbers are given in ARGS. */
7935
7936static void
831662b3 7937map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
c906108c 7938{
52f0bd74 7939 char *p = args;
c906108c 7940 char *p1;
52f0bd74
AC
7941 int num;
7942 struct breakpoint *b, *tmp;
11cf8741 7943 int match;
c906108c
SS
7944
7945 if (p == 0)
e2e0b3e5 7946 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
7947
7948 while (*p)
7949 {
11cf8741 7950 match = 0;
c906108c 7951 p1 = p;
c5aa993b 7952
5c44784c
JM
7953 num = get_number_or_range (&p1);
7954 if (num == 0)
c5aa993b 7955 {
8a3fe4f8 7956 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
7957 }
7958 else
7959 {
7960 ALL_BREAKPOINTS_SAFE (b, tmp)
7961 if (b->number == num)
7962 {
7963 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 7964 match = 1;
5c44784c
JM
7965 function (b);
7966 if (related_breakpoint)
7967 function (related_breakpoint);
11cf8741 7968 break;
5c44784c 7969 }
11cf8741 7970 if (match == 0)
a3f17187 7971 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 7972 }
c906108c
SS
7973 p = p1;
7974 }
7975}
7976
0d381245
VP
7977static struct bp_location *
7978find_location_by_number (char *number)
7979{
7980 char *dot = strchr (number, '.');
7981 char *p1;
7982 int bp_num;
7983 int loc_num;
7984 struct breakpoint *b;
7985 struct bp_location *loc;
7986
7987 *dot = '\0';
7988
7989 p1 = number;
7990 bp_num = get_number_or_range (&p1);
7991 if (bp_num == 0)
7992 error (_("Bad breakpoint number '%s'"), number);
7993
7994 ALL_BREAKPOINTS (b)
7995 if (b->number == bp_num)
7996 {
7997 break;
7998 }
7999
8000 if (!b || b->number != bp_num)
8001 error (_("Bad breakpoint number '%s'"), number);
8002
8003 p1 = dot+1;
8004 loc_num = get_number_or_range (&p1);
8005 if (loc_num == 0)
8006 error (_("Bad breakpoint location number '%s'"), number);
8007
8008 --loc_num;
8009 loc = b->loc;
8010 for (;loc_num && loc; --loc_num, loc = loc->next)
8011 ;
8012 if (!loc)
8013 error (_("Bad breakpoint location number '%s'"), dot+1);
8014
8015 return loc;
8016}
8017
8018
1900040c
MS
8019/* Set ignore-count of breakpoint number BPTNUM to COUNT.
8020 If from_tty is nonzero, it prints a message to that effect,
8021 which ends with a period (no newline). */
8022
c906108c 8023void
fba45db2 8024disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
8025{
8026 /* Never disable a watchpoint scope breakpoint; we want to
8027 hit them when we leave scope so we can delete both the
8028 watchpoint and its scope breakpoint at that time. */
8029 if (bpt->type == bp_watchpoint_scope)
8030 return;
8031
c2c6d25f 8032 /* You can't disable permanent breakpoints. */
b5de0fa7 8033 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
8034 return;
8035
b5de0fa7 8036 bpt->enable_state = bp_disabled;
c906108c 8037
b60e7edf 8038 update_global_location_list (0);
c906108c 8039
383f836e 8040 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
8041}
8042
c906108c 8043static void
fba45db2 8044disable_command (char *args, int from_tty)
c906108c 8045{
52f0bd74 8046 struct breakpoint *bpt;
c906108c
SS
8047 if (args == 0)
8048 ALL_BREAKPOINTS (bpt)
8049 switch (bpt->type)
c5aa993b
JM
8050 {
8051 case bp_none:
8a3fe4f8 8052 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 8053 bpt->number);
c5aa993b
JM
8054 continue;
8055 case bp_breakpoint:
1042e4c0 8056 case bp_tracepoint:
ce78b96d 8057 case bp_catchpoint:
c5aa993b
JM
8058 case bp_hardware_breakpoint:
8059 case bp_watchpoint:
8060 case bp_hardware_watchpoint:
8061 case bp_read_watchpoint:
8062 case bp_access_watchpoint:
8063 disable_breakpoint (bpt);
8064 default:
8065 continue;
8066 }
0d381245
VP
8067 else if (strchr (args, '.'))
8068 {
8069 struct bp_location *loc = find_location_by_number (args);
8070 if (loc)
8071 loc->enabled = 0;
b60e7edf 8072 update_global_location_list (0);
0d381245 8073 }
c906108c
SS
8074 else
8075 map_breakpoint_numbers (args, disable_breakpoint);
8076}
8077
8078static void
fba45db2 8079do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 8080{
c906108c
SS
8081 int target_resources_ok, other_type_used;
8082 struct value *mark;
8083
8084 if (bpt->type == bp_hardware_breakpoint)
8085 {
8086 int i;
c5aa993b 8087 i = hw_breakpoint_used_count ();
53a5351d 8088 target_resources_ok =
d92524f1 8089 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 8090 i + 1, 0);
c906108c 8091 if (target_resources_ok == 0)
8a3fe4f8 8092 error (_("No hardware breakpoint support in the target."));
c906108c 8093 else if (target_resources_ok < 0)
8a3fe4f8 8094 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
8095 }
8096
059fb39f
PM
8097 if (bpt->type == bp_watchpoint
8098 || bpt->type == bp_hardware_watchpoint
8099 || bpt->type == bp_read_watchpoint
8100 || bpt->type == bp_access_watchpoint)
c906108c 8101 {
dde02812
ES
8102 struct gdb_exception e;
8103
8104 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 8105 {
dde02812 8106 update_watchpoint (bpt, 1 /* reparse */);
c906108c 8107 }
dde02812 8108 if (e.reason < 0)
c5aa993b 8109 {
dde02812
ES
8110 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
8111 bpt->number);
8112 return;
c5aa993b 8113 }
c906108c 8114 }
0101ce28 8115
b4c291bb
KH
8116 if (bpt->enable_state != bp_permanent)
8117 bpt->enable_state = bp_enabled;
8118 bpt->disposition = disposition;
b60e7edf 8119 update_global_location_list (1);
b4c291bb
KH
8120 breakpoints_changed ();
8121
383f836e 8122 observer_notify_breakpoint_modified (bpt->number);
c906108c
SS
8123}
8124
fe3f5fa8 8125
c906108c 8126void
fba45db2 8127enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
8128{
8129 do_enable_breakpoint (bpt, bpt->disposition);
8130}
8131
8132/* The enable command enables the specified breakpoints (or all defined
8133 breakpoints) so they once again become (or continue to be) effective
1272ad14 8134 in stopping the inferior. */
c906108c 8135
c906108c 8136static void
fba45db2 8137enable_command (char *args, int from_tty)
c906108c 8138{
52f0bd74 8139 struct breakpoint *bpt;
c906108c
SS
8140 if (args == 0)
8141 ALL_BREAKPOINTS (bpt)
8142 switch (bpt->type)
c5aa993b
JM
8143 {
8144 case bp_none:
8a3fe4f8 8145 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 8146 bpt->number);
c5aa993b
JM
8147 continue;
8148 case bp_breakpoint:
1042e4c0 8149 case bp_tracepoint:
ce78b96d 8150 case bp_catchpoint:
c5aa993b
JM
8151 case bp_hardware_breakpoint:
8152 case bp_watchpoint:
8153 case bp_hardware_watchpoint:
8154 case bp_read_watchpoint:
8155 case bp_access_watchpoint:
8156 enable_breakpoint (bpt);
8157 default:
8158 continue;
8159 }
0d381245
VP
8160 else if (strchr (args, '.'))
8161 {
8162 struct bp_location *loc = find_location_by_number (args);
8163 if (loc)
8164 loc->enabled = 1;
b60e7edf 8165 update_global_location_list (1);
0d381245 8166 }
c906108c
SS
8167 else
8168 map_breakpoint_numbers (args, enable_breakpoint);
8169}
8170
8171static void
fba45db2 8172enable_once_breakpoint (struct breakpoint *bpt)
c906108c 8173{
b5de0fa7 8174 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
8175}
8176
c906108c 8177static void
fba45db2 8178enable_once_command (char *args, int from_tty)
c906108c
SS
8179{
8180 map_breakpoint_numbers (args, enable_once_breakpoint);
8181}
8182
8183static void
fba45db2 8184enable_delete_breakpoint (struct breakpoint *bpt)
c906108c 8185{
b5de0fa7 8186 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
8187}
8188
c906108c 8189static void
fba45db2 8190enable_delete_command (char *args, int from_tty)
c906108c
SS
8191{
8192 map_breakpoint_numbers (args, enable_delete_breakpoint);
8193}
8194\f
fa8d40ab
JJ
8195static void
8196set_breakpoint_cmd (char *args, int from_tty)
8197{
8198}
8199
8200static void
8201show_breakpoint_cmd (char *args, int from_tty)
8202{
8203}
8204
c906108c
SS
8205/* Use default_breakpoint_'s, or nothing if they aren't valid. */
8206
8207struct symtabs_and_lines
fba45db2 8208decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
8209{
8210 struct symtabs_and_lines sals;
8211 if (string == 0)
8a3fe4f8 8212 error (_("Empty line specification."));
c906108c
SS
8213 if (default_breakpoint_valid)
8214 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
8215 default_breakpoint_symtab,
8216 default_breakpoint_line,
68219205 8217 (char ***) NULL, NULL);
c906108c
SS
8218 else
8219 sals = decode_line_1 (&string, funfirstline,
68219205 8220 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 8221 if (*string)
8a3fe4f8 8222 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
8223 return sals;
8224}
8181d85f
DJ
8225
8226/* Create and insert a raw software breakpoint at PC. Return an
8227 identifier, which should be used to remove the breakpoint later.
8228 In general, places which call this should be using something on the
8229 breakpoint chain instead; this function should be eliminated
8230 someday. */
8231
8232void *
8233deprecated_insert_raw_breakpoint (CORE_ADDR pc)
8234{
8235 struct bp_target_info *bp_tgt;
8236
8237 bp_tgt = xmalloc (sizeof (struct bp_target_info));
8238 memset (bp_tgt, 0, sizeof (struct bp_target_info));
8239
8240 bp_tgt->placed_address = pc;
8241 if (target_insert_breakpoint (bp_tgt) != 0)
8242 {
8243 /* Could not insert the breakpoint. */
8244 xfree (bp_tgt);
8245 return NULL;
8246 }
8247
8248 return bp_tgt;
8249}
8250
8251/* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
8252
8253int
8254deprecated_remove_raw_breakpoint (void *bp)
8255{
8256 struct bp_target_info *bp_tgt = bp;
8257 int ret;
8258
8259 ret = target_remove_breakpoint (bp_tgt);
8260 xfree (bp_tgt);
8261
8262 return ret;
8263}
8264
8265/* One (or perhaps two) breakpoints used for software single stepping. */
8266
8267static void *single_step_breakpoints[2];
8268
8269/* Create and insert a breakpoint for software single step. */
8270
8271void
8272insert_single_step_breakpoint (CORE_ADDR next_pc)
8273{
8274 void **bpt_p;
8275
8276 if (single_step_breakpoints[0] == NULL)
8277 bpt_p = &single_step_breakpoints[0];
8278 else
8279 {
8280 gdb_assert (single_step_breakpoints[1] == NULL);
8281 bpt_p = &single_step_breakpoints[1];
8282 }
8283
8284 /* NOTE drow/2006-04-11: A future improvement to this function would be
8285 to only create the breakpoints once, and actually put them on the
8286 breakpoint chain. That would let us use set_raw_breakpoint. We could
8287 adjust the addresses each time they were needed. Doing this requires
8288 corresponding changes elsewhere where single step breakpoints are
8289 handled, however. So, for now, we use this. */
8290
8291 *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
8292 if (*bpt_p == NULL)
1893a4c0 8293 error (_("Could not insert single-step breakpoint at 0x%s"),
8181d85f
DJ
8294 paddr_nz (next_pc));
8295}
8296
8297/* Remove and delete any breakpoints used for software single step. */
8298
8299void
8300remove_single_step_breakpoints (void)
8301{
8302 gdb_assert (single_step_breakpoints[0] != NULL);
8303
8304 /* See insert_single_step_breakpoint for more about this deprecated
8305 call. */
8306 deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
8307 single_step_breakpoints[0] = NULL;
8308
8309 if (single_step_breakpoints[1] != NULL)
8310 {
8311 deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
8312 single_step_breakpoints[1] = NULL;
8313 }
8314}
8315
1aafd4da
UW
8316/* Check whether a software single-step breakpoint is inserted at PC. */
8317
8318static int
8319single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
8320{
8321 int i;
8322
8323 for (i = 0; i < 2; i++)
8324 {
8325 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
8326 if (bp_tgt && bp_tgt->placed_address == pc)
8327 return 1;
8328 }
8329
8330 return 0;
8331}
8332
1042e4c0
SS
8333/* Tracepoint-specific operations. */
8334
8335/* Set tracepoint count to NUM. */
8336static void
8337set_tracepoint_count (int num)
8338{
8339 tracepoint_count = num;
4fa62494 8340 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
8341}
8342
8343void
8344trace_command (char *arg, int from_tty)
8345{
8346 break_command_really (arg,
8347 NULL, 0, 1 /* parse arg */,
8348 0 /* tempflag */, 0 /* hardwareflag */,
8349 1 /* traceflag */,
8350 0 /* Ignore count */,
8351 pending_break_support,
8352 NULL,
8353 from_tty,
8354 1 /* enabled */);
8355 set_tracepoint_count (breakpoint_count);
8356}
8357
8358/* Print information on tracepoint number TPNUM_EXP, or all if
8359 omitted. */
8360
8361static void
8362tracepoints_info (char *tpnum_exp, int from_tty)
8363{
8364 struct breakpoint *b;
8365 int tps_to_list = 0;
8366
8367 /* In the no-arguments case, say "No tracepoints" if none found. */
8368 if (tpnum_exp == 0)
8369 {
8370 ALL_TRACEPOINTS (b)
8371 {
8372 if (b->number >= 0)
8373 {
8374 tps_to_list = 1;
8375 break;
8376 }
8377 }
8378 if (!tps_to_list)
8379 {
8380 ui_out_message (uiout, 0, "No tracepoints.\n");
8381 return;
8382 }
8383 }
8384
8385 /* Otherwise be the same as "info break". */
8386 breakpoints_info (tpnum_exp, from_tty);
8387}
8388
8389/* The 'enable trace' command enables tracepoints.
8390 Not supported by all targets. */
8391static void
8392enable_trace_command (char *args, int from_tty)
8393{
8394 enable_command (args, from_tty);
8395}
8396
8397/* The 'disable trace' command disables tracepoints.
8398 Not supported by all targets. */
8399static void
8400disable_trace_command (char *args, int from_tty)
8401{
8402 disable_command (args, from_tty);
8403}
8404
8405/* Remove a tracepoint (or all if no argument) */
8406static void
8407delete_trace_command (char *arg, int from_tty)
8408{
8409 struct breakpoint *b, *temp;
8410
8411 dont_repeat ();
8412
8413 if (arg == 0)
8414 {
8415 int breaks_to_delete = 0;
8416
8417 /* Delete all breakpoints if no argument.
8418 Do not delete internal or call-dummy breakpoints, these
8419 have to be deleted with an explicit breakpoint number argument. */
8420 ALL_TRACEPOINTS (b)
8421 {
8422 if (b->number >= 0)
8423 {
8424 breaks_to_delete = 1;
8425 break;
8426 }
8427 }
8428
8429 /* Ask user only if there are some breakpoints to delete. */
8430 if (!from_tty
8431 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
8432 {
8433 ALL_BREAKPOINTS_SAFE (b, temp)
8434 {
059fb39f
PM
8435 if (b->type == bp_tracepoint
8436 && b->number >= 0)
1042e4c0
SS
8437 delete_breakpoint (b);
8438 }
8439 }
8440 }
8441 else
8442 map_breakpoint_numbers (arg, delete_breakpoint);
8443}
8444
8445/* Set passcount for tracepoint.
8446
8447 First command argument is passcount, second is tracepoint number.
8448 If tracepoint number omitted, apply to most recently defined.
8449 Also accepts special argument "all". */
8450
8451static void
8452trace_pass_command (char *args, int from_tty)
8453{
8454 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
8455 unsigned int count;
8456 int all = 0;
8457
8458 if (args == 0 || *args == 0)
8459 error (_("passcount command requires an argument (count + optional TP num)"));
8460
8461 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
8462
8463 while (*args && isspace ((int) *args))
8464 args++;
8465
8466 if (*args && strncasecmp (args, "all", 3) == 0)
8467 {
8468 args += 3; /* Skip special argument "all". */
8469 all = 1;
8470 if (*args)
8471 error (_("Junk at end of arguments."));
8472 }
8473 else
8474 t1 = get_tracepoint_by_number (&args, 1, 1);
8475
8476 do
8477 {
8478 if (t1)
8479 {
8480 ALL_TRACEPOINTS (t2)
8481 if (t1 == (struct breakpoint *) -1 || t1 == t2)
8482 {
8483 t2->pass_count = count;
8484 observer_notify_tracepoint_modified (t2->number);
8485 if (from_tty)
8486 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
8487 t2->number, count);
8488 }
8489 if (! all && *args)
8490 t1 = get_tracepoint_by_number (&args, 1, 0);
8491 }
8492 }
8493 while (*args);
8494}
8495
8496struct breakpoint *
8497get_tracepoint (int num)
8498{
8499 struct breakpoint *t;
8500
8501 ALL_TRACEPOINTS (t)
8502 if (t->number == num)
8503 return t;
8504
8505 return NULL;
8506}
8507
8508/* Utility: parse a tracepoint number and look it up in the list.
8509 If MULTI_P is true, there might be a range of tracepoints in ARG.
8510 if OPTIONAL_P is true, then if the argument is missing, the most
8511 recent tracepoint (tracepoint_count) is returned. */
8512struct breakpoint *
8513get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
8514{
8515 extern int tracepoint_count;
8516 struct breakpoint *t;
8517 int tpnum;
8518 char *instring = arg == NULL ? NULL : *arg;
8519
8520 if (arg == NULL || *arg == NULL || ! **arg)
8521 {
8522 if (optional_p)
8523 tpnum = tracepoint_count;
8524 else
8525 error_no_arg (_("tracepoint number"));
8526 }
8527 else
8528 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
8529
8530 if (tpnum <= 0)
8531 {
8532 if (instring && *instring)
8533 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
8534 instring);
8535 else
8536 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
8537 return NULL;
8538 }
8539
8540 ALL_TRACEPOINTS (t)
8541 if (t->number == tpnum)
8542 {
8543 return t;
8544 }
8545
8546 /* FIXME: if we are in the middle of a range we don't want to give
8547 a message. The current interface to get_number_or_range doesn't
8548 allow us to discover this. */
8549 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
8550 return NULL;
8551}
8552
8553/* save-tracepoints command */
8554static void
8555tracepoint_save_command (char *args, int from_tty)
8556{
8557 struct breakpoint *tp;
8558 int any_tp = 0;
8559 struct action_line *line;
8560 FILE *fp;
8561 char *i1 = " ", *i2 = " ";
8562 char *indent, *actionline, *pathname;
8563 char tmp[40];
8564 struct cleanup *cleanup;
8565
8566 if (args == 0 || *args == 0)
8567 error (_("Argument required (file name in which to save tracepoints)"));
8568
8569 /* See if we have anything to save. */
8570 ALL_TRACEPOINTS (tp)
8571 {
8572 any_tp = 1;
8573 break;
8574 }
8575 if (!any_tp)
8576 {
8577 warning (_("save-tracepoints: no tracepoints to save."));
8578 return;
8579 }
8580
8581 pathname = tilde_expand (args);
8582 cleanup = make_cleanup (xfree, pathname);
059fb39f
PM
8583 fp = fopen (pathname, "w");
8584 if (!fp)
1042e4c0
SS
8585 error (_("Unable to open file '%s' for saving tracepoints (%s)"),
8586 args, safe_strerror (errno));
8587 make_cleanup_fclose (fp);
8588
8589 ALL_TRACEPOINTS (tp)
8590 {
8591 if (tp->addr_string)
8592 fprintf (fp, "trace %s\n", tp->addr_string);
8593 else
8594 {
8595 sprintf_vma (tmp, tp->loc->address);
8596 fprintf (fp, "trace *0x%s\n", tmp);
8597 }
8598
8599 if (tp->pass_count)
8600 fprintf (fp, " passcount %d\n", tp->pass_count);
8601
8602 if (tp->actions)
8603 {
8604 fprintf (fp, " actions\n");
8605 indent = i1;
8606 for (line = tp->actions; line; line = line->next)
8607 {
8608 struct cmd_list_element *cmd;
8609
8610 QUIT; /* allow user to bail out with ^C */
8611 actionline = line->action;
8612 while (isspace ((int) *actionline))
8613 actionline++;
8614
8615 fprintf (fp, "%s%s\n", indent, actionline);
8616 if (*actionline != '#') /* skip for comment lines */
8617 {
8618 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
8619 if (cmd == 0)
8620 error (_("Bad action list item: %s"), actionline);
8621 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
8622 indent = i2;
8623 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
8624 indent = i1;
8625 }
8626 }
8627 }
8628 }
8629 do_cleanups (cleanup);
8630 if (from_tty)
8631 printf_filtered (_("Tracepoints saved to file '%s'.\n"), args);
8632 return;
8633}
8634
8635/* Create a vector of all tracepoints. */
8636
8637VEC(breakpoint_p) *
8638all_tracepoints ()
8639{
8640 VEC(breakpoint_p) *tp_vec = 0;
8641 struct breakpoint *tp;
8642
8643 ALL_TRACEPOINTS (tp)
8644 {
8645 VEC_safe_push (breakpoint_p, tp_vec, tp);
8646 }
8647
8648 return tp_vec;
8649}
8650
c906108c 8651\f
31e2b00f
AS
8652/* This help string is used for the break, hbreak, tbreak and thbreak commands.
8653 It is defined as a macro to prevent duplication.
8654 COMMAND should be a string constant containing the name of the command. */
8655#define BREAK_ARGS_HELP(command) \
8656command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
8657LOCATION may be a line number, function name, or \"*\" and an address.\n\
8658If a line number is specified, break at start of code for that line.\n\
8659If a function is specified, break at start of code for that function.\n\
8660If an address is specified, break at that exact address.\n\
8661With no LOCATION, uses current execution address of selected stack frame.\n\
8662This is useful for breaking on return to a stack frame.\n\
8663\n\
8664THREADNUM is the number from \"info threads\".\n\
8665CONDITION is a boolean expression.\n\
8666\n\
8667Multiple breakpoints at one place are permitted, and useful if conditional.\n\
8668\n\
8669Do \"help breakpoints\" for info on other commands dealing with breakpoints."
8670
44feb3ce
TT
8671/* List of subcommands for "catch". */
8672static struct cmd_list_element *catch_cmdlist;
8673
8674/* List of subcommands for "tcatch". */
8675static struct cmd_list_element *tcatch_cmdlist;
8676
8677/* Like add_cmd, but add the command to both the "catch" and "tcatch"
8678 lists, and pass some additional user data to the command function. */
8679static void
8680add_catch_command (char *name, char *docstring,
8681 void (*sfunc) (char *args, int from_tty,
8682 struct cmd_list_element *command),
8683 void *user_data_catch,
8684 void *user_data_tcatch)
8685{
8686 struct cmd_list_element *command;
8687
8688 command = add_cmd (name, class_breakpoint, NULL, docstring,
8689 &catch_cmdlist);
8690 set_cmd_sfunc (command, sfunc);
8691 set_cmd_context (command, user_data_catch);
8692
8693 command = add_cmd (name, class_breakpoint, NULL, docstring,
8694 &tcatch_cmdlist);
8695 set_cmd_sfunc (command, sfunc);
8696 set_cmd_context (command, user_data_tcatch);
8697}
8698
c906108c 8699void
fba45db2 8700_initialize_breakpoint (void)
c906108c 8701{
fa8d40ab
JJ
8702 static struct cmd_list_element *breakpoint_set_cmdlist;
8703 static struct cmd_list_element *breakpoint_show_cmdlist;
c906108c
SS
8704 struct cmd_list_element *c;
8705
84acb35a 8706 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
84acb35a 8707
c906108c
SS
8708 breakpoint_chain = 0;
8709 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
8710 before a breakpoint is set. */
8711 breakpoint_count = 0;
8712
1042e4c0
SS
8713 tracepoint_count = 0;
8714
1bedd215
AC
8715 add_com ("ignore", class_breakpoint, ignore_command, _("\
8716Set ignore-count of breakpoint number N to COUNT.\n\
8717Usage is `ignore N COUNT'."));
c906108c 8718 if (xdb_commands)
c5aa993b 8719 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 8720
1bedd215
AC
8721 add_com ("commands", class_breakpoint, commands_command, _("\
8722Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
8723Give breakpoint number as argument after \"commands\".\n\
8724With no argument, the targeted breakpoint is the last one set.\n\
8725The commands themselves follow starting on the next line.\n\
8726Type a line containing \"end\" to indicate the end of them.\n\
8727Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 8728then no output is printed when it is hit, except what the commands print."));
c906108c 8729
1bedd215
AC
8730 add_com ("condition", class_breakpoint, condition_command, _("\
8731Specify breakpoint number N to break only if COND is true.\n\
c906108c 8732Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 8733expression to be evaluated whenever breakpoint N is reached."));
c906108c 8734
1bedd215 8735 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 8736Set a temporary breakpoint.\n\
c906108c
SS
8737Like \"break\" except the breakpoint is only temporary,\n\
8738so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
8739by using \"enable delete\" on the breakpoint number.\n\
8740\n"
8741BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 8742 set_cmd_completer (c, location_completer);
c94fdfd0 8743
1bedd215 8744 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
31e2b00f 8745Set a hardware assisted breakpoint.\n\
c906108c 8746Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
8747some target hardware may not have this support.\n\
8748\n"
8749BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 8750 set_cmd_completer (c, location_completer);
c906108c 8751
1bedd215 8752 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 8753Set a temporary hardware assisted breakpoint.\n\
c906108c 8754Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
8755so it will be deleted when hit.\n\
8756\n"
8757BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 8758 set_cmd_completer (c, location_completer);
c906108c 8759
1bedd215
AC
8760 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
8761Enable some breakpoints.\n\
c906108c
SS
8762Give breakpoint numbers (separated by spaces) as arguments.\n\
8763With no subcommand, breakpoints are enabled until you command otherwise.\n\
8764This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8765With a subcommand you can enable temporarily."),
c906108c
SS
8766 &enablelist, "enable ", 1, &cmdlist);
8767 if (xdb_commands)
1bedd215
AC
8768 add_com ("ab", class_breakpoint, enable_command, _("\
8769Enable some breakpoints.\n\
c906108c
SS
8770Give breakpoint numbers (separated by spaces) as arguments.\n\
8771With no subcommand, breakpoints are enabled until you command otherwise.\n\
8772This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8773With a subcommand you can enable temporarily."));
c906108c
SS
8774
8775 add_com_alias ("en", "enable", class_breakpoint, 1);
8776
1bedd215
AC
8777 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8778Enable some breakpoints.\n\
c906108c
SS
8779Give breakpoint numbers (separated by spaces) as arguments.\n\
8780This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8781May be abbreviated to simply \"enable\".\n"),
c5aa993b 8782 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 8783
1a966eab
AC
8784 add_cmd ("once", no_class, enable_once_command, _("\
8785Enable breakpoints for one hit. Give breakpoint numbers.\n\
8786If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8787 &enablebreaklist);
8788
1a966eab
AC
8789 add_cmd ("delete", no_class, enable_delete_command, _("\
8790Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8791If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8792 &enablebreaklist);
8793
1a966eab
AC
8794 add_cmd ("delete", no_class, enable_delete_command, _("\
8795Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8796If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8797 &enablelist);
8798
1a966eab
AC
8799 add_cmd ("once", no_class, enable_once_command, _("\
8800Enable breakpoints for one hit. Give breakpoint numbers.\n\
8801If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8802 &enablelist);
8803
1bedd215
AC
8804 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8805Disable some breakpoints.\n\
c906108c
SS
8806Arguments are breakpoint numbers with spaces in between.\n\
8807To disable all breakpoints, give no argument.\n\
1bedd215 8808A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
8809 &disablelist, "disable ", 1, &cmdlist);
8810 add_com_alias ("dis", "disable", class_breakpoint, 1);
8811 add_com_alias ("disa", "disable", class_breakpoint, 1);
8812 if (xdb_commands)
1bedd215
AC
8813 add_com ("sb", class_breakpoint, disable_command, _("\
8814Disable some breakpoints.\n\
c906108c
SS
8815Arguments are breakpoint numbers with spaces in between.\n\
8816To disable all breakpoints, give no argument.\n\
1bedd215 8817A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 8818
1a966eab
AC
8819 add_cmd ("breakpoints", class_alias, disable_command, _("\
8820Disable some breakpoints.\n\
c906108c
SS
8821Arguments are breakpoint numbers with spaces in between.\n\
8822To disable all breakpoints, give no argument.\n\
8823A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 8824This command may be abbreviated \"disable\"."),
c906108c
SS
8825 &disablelist);
8826
1bedd215
AC
8827 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8828Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8829Arguments are breakpoint numbers with spaces in between.\n\
8830To delete all breakpoints, give no argument.\n\
8831\n\
8832Also a prefix command for deletion of other GDB objects.\n\
1bedd215 8833The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
8834 &deletelist, "delete ", 1, &cmdlist);
8835 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 8836 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 8837 if (xdb_commands)
1bedd215
AC
8838 add_com ("db", class_breakpoint, delete_command, _("\
8839Delete some breakpoints.\n\
c906108c 8840Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 8841To delete all breakpoints, give no argument.\n"));
c906108c 8842
1a966eab
AC
8843 add_cmd ("breakpoints", class_alias, delete_command, _("\
8844Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8845Arguments are breakpoint numbers with spaces in between.\n\
8846To delete all breakpoints, give no argument.\n\
1a966eab 8847This command may be abbreviated \"delete\"."),
c906108c
SS
8848 &deletelist);
8849
1bedd215
AC
8850 add_com ("clear", class_breakpoint, clear_command, _("\
8851Clear breakpoint at specified line or function.\n\
c906108c
SS
8852Argument may be line number, function name, or \"*\" and an address.\n\
8853If line number is specified, all breakpoints in that line are cleared.\n\
8854If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
8855If an address is specified, breakpoints at that address are cleared.\n\
8856\n\
8857With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
8858is executing in.\n\
8859\n\
1bedd215 8860See also the \"delete\" command which clears breakpoints by number."));
c906108c 8861
1bedd215 8862 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
8863Set breakpoint at specified line or function.\n"
8864BREAK_ARGS_HELP ("break")));
5ba2abeb 8865 set_cmd_completer (c, location_completer);
c94fdfd0 8866
c906108c
SS
8867 add_com_alias ("b", "break", class_run, 1);
8868 add_com_alias ("br", "break", class_run, 1);
8869 add_com_alias ("bre", "break", class_run, 1);
8870 add_com_alias ("brea", "break", class_run, 1);
8871
7681d515
PM
8872 if (xdb_commands)
8873 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
8874
8875 if (dbx_commands)
8876 {
1bedd215
AC
8877 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8878Break in function/address or break at a line in the current file."),
c5aa993b
JM
8879 &stoplist, "stop ", 1, &cmdlist);
8880 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 8881 _("Break in function or address."), &stoplist);
c5aa993b 8882 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 8883 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
8884 add_com ("status", class_info, breakpoints_info, _("\
8885Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8886The \"Type\" column indicates one of:\n\
8887\tbreakpoint - normal breakpoint\n\
8888\twatchpoint - watchpoint\n\
8889The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8890the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8891breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8892address and file/line number respectively.\n\
8893\n\
8894Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8895are set to the address of the last breakpoint listed unless the command\n\
8896is prefixed with \"server \".\n\n\
c906108c 8897Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8898breakpoint set."));
c906108c
SS
8899 }
8900
1bedd215
AC
8901 add_info ("breakpoints", breakpoints_info, _("\
8902Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8903The \"Type\" column indicates one of:\n\
8904\tbreakpoint - normal breakpoint\n\
8905\twatchpoint - watchpoint\n\
8906The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8907the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8908breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8909address and file/line number respectively.\n\
8910\n\
8911Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8912are set to the address of the last breakpoint listed unless the command\n\
8913is prefixed with \"server \".\n\n\
c906108c 8914Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8915breakpoint set."));
c906108c
SS
8916
8917 if (xdb_commands)
1bedd215
AC
8918 add_com ("lb", class_breakpoint, breakpoints_info, _("\
8919Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8920The \"Type\" column indicates one of:\n\
8921\tbreakpoint - normal breakpoint\n\
8922\twatchpoint - watchpoint\n\
8923The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8924the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8925breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8926address and file/line number respectively.\n\
8927\n\
8928Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8929are set to the address of the last breakpoint listed unless the command\n\
8930is prefixed with \"server \".\n\n\
c906108c 8931Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8932breakpoint set."));
c906108c 8933
1a966eab
AC
8934 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
8935Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8936The \"Type\" column indicates one of:\n\
8937\tbreakpoint - normal breakpoint\n\
8938\twatchpoint - watchpoint\n\
8939\tlongjmp - internal breakpoint used to step through longjmp()\n\
8940\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8941\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
8942\tfinish - internal breakpoint used by the \"finish\" command\n\
8943The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
8944the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8945breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
8946address and file/line number respectively.\n\
8947\n\
8948Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8949are set to the address of the last breakpoint listed unless the command\n\
8950is prefixed with \"server \".\n\n\
c906108c 8951Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 8952breakpoint set."),
c906108c
SS
8953 &maintenanceinfolist);
8954
44feb3ce
TT
8955 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
8956Set catchpoints to catch events."),
8957 &catch_cmdlist, "catch ",
8958 0/*allow-unknown*/, &cmdlist);
8959
8960 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
8961Set temporary catchpoints to catch events."),
8962 &tcatch_cmdlist, "tcatch ",
8963 0/*allow-unknown*/, &cmdlist);
8964
8965 /* Add catch and tcatch sub-commands. */
8966 add_catch_command ("catch", _("\
8967Catch an exception, when caught.\n\
8968With an argument, catch only exceptions with the given name."),
8969 catch_catch_command,
8970 CATCH_PERMANENT,
8971 CATCH_TEMPORARY);
8972 add_catch_command ("throw", _("\
8973Catch an exception, when thrown.\n\
8974With an argument, catch only exceptions with the given name."),
8975 catch_throw_command,
8976 CATCH_PERMANENT,
8977 CATCH_TEMPORARY);
8978 add_catch_command ("fork", _("Catch calls to fork."),
8979 catch_fork_command_1,
8980 (void *) (uintptr_t) catch_fork_permanent,
8981 (void *) (uintptr_t) catch_fork_temporary);
8982 add_catch_command ("vfork", _("Catch calls to vfork."),
8983 catch_fork_command_1,
8984 (void *) (uintptr_t) catch_vfork_permanent,
8985 (void *) (uintptr_t) catch_vfork_temporary);
8986 add_catch_command ("exec", _("Catch calls to exec."),
8987 catch_exec_command_1,
8988 CATCH_PERMANENT,
8989 CATCH_TEMPORARY);
44feb3ce
TT
8990 add_catch_command ("exception", _("\
8991Catch Ada exceptions, when raised.\n\
8992With an argument, catch only exceptions with the given name."),
8993 catch_ada_exception_command,
8994 CATCH_PERMANENT,
8995 CATCH_TEMPORARY);
8996 add_catch_command ("assert", _("\
8997Catch failed Ada assertions, when raised.\n\
8998With an argument, catch only exceptions with the given name."),
8999 catch_assert_command,
9000 CATCH_PERMANENT,
9001 CATCH_TEMPORARY);
c5aa993b 9002
1bedd215
AC
9003 c = add_com ("watch", class_breakpoint, watch_command, _("\
9004Set a watchpoint for an expression.\n\
c906108c 9005A watchpoint stops execution of your program whenever the value of\n\
1bedd215 9006an expression changes."));
65d12d83 9007 set_cmd_completer (c, expression_completer);
c906108c 9008
1bedd215
AC
9009 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
9010Set a read watchpoint for an expression.\n\
c906108c 9011A watchpoint stops execution of your program whenever the value of\n\
1bedd215 9012an expression is read."));
65d12d83 9013 set_cmd_completer (c, expression_completer);
c906108c 9014
1bedd215
AC
9015 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
9016Set a watchpoint for an expression.\n\
c906108c 9017A watchpoint stops execution of your program whenever the value of\n\
1bedd215 9018an expression is either read or written."));
65d12d83 9019 set_cmd_completer (c, expression_completer);
c906108c
SS
9020
9021 add_info ("watchpoints", breakpoints_info,
1bedd215 9022 _("Synonym for ``info breakpoints''."));
c906108c
SS
9023
9024
920d2a44
AC
9025 /* XXX: cagney/2005-02-23: This should be a boolean, and should
9026 respond to changes - contrary to the description. */
85c07804
AC
9027 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
9028 &can_use_hw_watchpoints, _("\
9029Set debugger's willingness to use watchpoint hardware."), _("\
9030Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
9031If zero, gdb will not use hardware for new watchpoints, even if\n\
9032such is available. (However, any hardware watchpoints that were\n\
9033created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
9034hardware.)"),
9035 NULL,
920d2a44 9036 show_can_use_hw_watchpoints,
85c07804 9037 &setlist, &showlist);
c906108c
SS
9038
9039 can_use_hw_watchpoints = 1;
fa8d40ab 9040
1042e4c0
SS
9041 /* Tracepoint manipulation commands. */
9042
9043 c = add_com ("trace", class_breakpoint, trace_command, _("\
9044Set a tracepoint at specified line or function.\n\
9045\n"
9046BREAK_ARGS_HELP ("trace") "\n\
9047Do \"help tracepoints\" for info on other tracepoint commands."));
9048 set_cmd_completer (c, location_completer);
9049
9050 add_com_alias ("tp", "trace", class_alias, 0);
9051 add_com_alias ("tr", "trace", class_alias, 1);
9052 add_com_alias ("tra", "trace", class_alias, 1);
9053 add_com_alias ("trac", "trace", class_alias, 1);
9054
9055 add_info ("tracepoints", tracepoints_info, _("\
9056Status of tracepoints, or tracepoint number NUMBER.\n\
9057Convenience variable \"$tpnum\" contains the number of the\n\
9058last tracepoint set."));
9059
9060 add_info_alias ("tp", "tracepoints", 1);
9061
9062 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
9063Delete specified tracepoints.\n\
9064Arguments are tracepoint numbers, separated by spaces.\n\
9065No argument means delete all tracepoints."),
9066 &deletelist);
9067
9068 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
9069Disable specified tracepoints.\n\
9070Arguments are tracepoint numbers, separated by spaces.\n\
9071No argument means disable all tracepoints."),
9072 &disablelist);
9073 deprecate_cmd (c, "disable");
9074
9075 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
9076Enable specified tracepoints.\n\
9077Arguments are tracepoint numbers, separated by spaces.\n\
9078No argument means enable all tracepoints."),
9079 &enablelist);
9080 deprecate_cmd (c, "enable");
9081
9082 add_com ("passcount", class_trace, trace_pass_command, _("\
9083Set the passcount for a tracepoint.\n\
9084The trace will end when the tracepoint has been passed 'count' times.\n\
9085Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
9086if TPNUM is omitted, passcount refers to the last tracepoint defined."));
9087
9088 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command, _("\
9089Save current tracepoint definitions as a script.\n\
9090Use the 'source' command in another debug session to restore them."));
9091 set_cmd_completer (c, filename_completer);
9092
1bedd215 9093 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
9094Breakpoint specific settings\n\
9095Configure various breakpoint-specific variables such as\n\
1bedd215 9096pending breakpoint behavior"),
fa8d40ab
JJ
9097 &breakpoint_set_cmdlist, "set breakpoint ",
9098 0/*allow-unknown*/, &setlist);
1bedd215 9099 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
9100Breakpoint specific settings\n\
9101Configure various breakpoint-specific variables such as\n\
1bedd215 9102pending breakpoint behavior"),
fa8d40ab
JJ
9103 &breakpoint_show_cmdlist, "show breakpoint ",
9104 0/*allow-unknown*/, &showlist);
9105
7915a72c
AC
9106 add_setshow_auto_boolean_cmd ("pending", no_class,
9107 &pending_break_support, _("\
9108Set debugger's behavior regarding pending breakpoints."), _("\
9109Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
9110If on, an unrecognized breakpoint location will cause gdb to create a\n\
9111pending breakpoint. If off, an unrecognized breakpoint location results in\n\
9112an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 9113user-query to see if a pending breakpoint should be created."),
2c5b56ce 9114 NULL,
920d2a44 9115 show_pending_break_support,
6e1d7d6c
AC
9116 &breakpoint_set_cmdlist,
9117 &breakpoint_show_cmdlist);
fa8d40ab
JJ
9118
9119 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
9120
9121 add_setshow_boolean_cmd ("auto-hw", no_class,
9122 &automatic_hardware_breakpoints, _("\
9123Set automatic usage of hardware breakpoints."), _("\
9124Show automatic usage of hardware breakpoints."), _("\
9125If set, the debugger will automatically use hardware breakpoints for\n\
9126breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
9127a warning will be emitted for such breakpoints."),
9128 NULL,
9129 show_automatic_hardware_breakpoints,
9130 &breakpoint_set_cmdlist,
9131 &breakpoint_show_cmdlist);
74960c60 9132
33e5cbd6
PA
9133 add_setshow_enum_cmd ("always-inserted", class_support,
9134 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
9135Set mode for inserting breakpoints."), _("\
9136Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
9137When this mode is off, breakpoints are inserted in inferior when it is\n\
9138resumed, and removed when execution stops. When this mode is on,\n\
9139breakpoints are inserted immediately and removed only when the user\n\
9140deletes the breakpoint. When this mode is auto (which is the default),\n\
9141the behaviour depends on the non-stop setting (see help set non-stop).\n\
9142In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
9143behaves as if always-inserted mode is on; if gdb is controlling the\n\
9144inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
9145 NULL,
9146 &show_always_inserted_mode,
9147 &breakpoint_set_cmdlist,
9148 &breakpoint_show_cmdlist);
765dc015
VP
9149
9150 automatic_hardware_breakpoints = 1;
f3b1572e
PA
9151
9152 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 9153}
This page took 1.371443 seconds and 4 git commands to generate.