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