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