*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
6aba47ca
DJ
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
e74f0f02 5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
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;
c906108c 5750
801e3a5b 5751 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
5752 if (bv != NULL)
5753 {
c906108c
SS
5754 sym = block_function (b);
5755 if (sym != NULL)
5756 {
5757 fixup_symbol_section (sym, sal->symtab->objfile);
5758 sal->section = SYMBOL_BFD_SECTION (sym);
5759 }
5760 else
5761 {
5762 /* It really is worthwhile to have the section, so we'll just
c5aa993b
JM
5763 have to look harder. This case can be executed if we have
5764 line numbers but no functions (as can happen in assembly
5765 source). */
c906108c 5766
c5aa993b 5767 struct minimal_symbol *msym;
c906108c
SS
5768
5769 msym = lookup_minimal_symbol_by_pc (sal->pc);
5770 if (msym)
5771 sal->section = SYMBOL_BFD_SECTION (msym);
5772 }
5773 }
5774 }
5775}
5776
5777void
fba45db2 5778break_command (char *arg, int from_tty)
c906108c 5779{
db107f19 5780 break_command_1 (arg, 0, from_tty);
c906108c
SS
5781}
5782
c906108c 5783void
fba45db2 5784tbreak_command (char *arg, int from_tty)
c906108c 5785{
db107f19 5786 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
5787}
5788
c906108c 5789static void
fba45db2 5790hbreak_command (char *arg, int from_tty)
c906108c 5791{
db107f19 5792 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
5793}
5794
5795static void
fba45db2 5796thbreak_command (char *arg, int from_tty)
c906108c 5797{
db107f19 5798 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
5799}
5800
5801static void
fba45db2 5802stop_command (char *arg, int from_tty)
c906108c 5803{
a3f17187 5804 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 5805Usage: stop in <function | address>\n\
a3f17187 5806 stop at <line>\n"));
c906108c
SS
5807}
5808
5809static void
fba45db2 5810stopin_command (char *arg, int from_tty)
c906108c
SS
5811{
5812 int badInput = 0;
5813
c5aa993b 5814 if (arg == (char *) NULL)
c906108c
SS
5815 badInput = 1;
5816 else if (*arg != '*')
5817 {
5818 char *argptr = arg;
5819 int hasColon = 0;
5820
53a5351d
JM
5821 /* look for a ':'. If this is a line number specification, then
5822 say it is bad, otherwise, it should be an address or
5823 function/method name */
c906108c 5824 while (*argptr && !hasColon)
c5aa993b
JM
5825 {
5826 hasColon = (*argptr == ':');
5827 argptr++;
5828 }
c906108c
SS
5829
5830 if (hasColon)
c5aa993b 5831 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 5832 else
c5aa993b 5833 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
5834 }
5835
5836 if (badInput)
a3f17187 5837 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 5838 else
db107f19 5839 break_command_1 (arg, 0, from_tty);
c906108c
SS
5840}
5841
5842static void
fba45db2 5843stopat_command (char *arg, int from_tty)
c906108c
SS
5844{
5845 int badInput = 0;
5846
c5aa993b 5847 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
5848 badInput = 1;
5849 else
5850 {
5851 char *argptr = arg;
5852 int hasColon = 0;
5853
5854 /* look for a ':'. If there is a '::' then get out, otherwise
c5aa993b 5855 it is probably a line number. */
c906108c 5856 while (*argptr && !hasColon)
c5aa993b
JM
5857 {
5858 hasColon = (*argptr == ':');
5859 argptr++;
5860 }
c906108c
SS
5861
5862 if (hasColon)
c5aa993b 5863 badInput = (*argptr == ':'); /* we have class::method */
c906108c 5864 else
c5aa993b 5865 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
5866 }
5867
5868 if (badInput)
a3f17187 5869 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 5870 else
db107f19 5871 break_command_1 (arg, 0, from_tty);
c906108c
SS
5872}
5873
53a5351d
JM
5874/* accessflag: hw_write: watch write,
5875 hw_read: watch read,
5876 hw_access: watch access (read or write) */
c906108c 5877static void
fba45db2 5878watch_command_1 (char *arg, int accessflag, int from_tty)
c906108c 5879{
d983da9c 5880 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c
SS
5881 struct symtab_and_line sal;
5882 struct expression *exp;
5883 struct block *exp_valid_block;
5884 struct value *val, *mark;
5885 struct frame_info *frame;
5886 struct frame_info *prev_frame = NULL;
5887 char *exp_start = NULL;
5888 char *exp_end = NULL;
5889 char *tok, *end_tok;
5890 int toklen;
5891 char *cond_start = NULL;
5892 char *cond_end = NULL;
5893 struct expression *cond = NULL;
5894 int i, other_type_used, target_resources_ok = 0;
5895 enum bptype bp_type;
5896 int mem_cnt = 0;
5897
fe39c653 5898 init_sal (&sal); /* initialize to zeroes */
c5aa993b 5899
c906108c
SS
5900 /* Parse arguments. */
5901 innermost_block = NULL;
5902 exp_start = arg;
5903 exp = parse_exp_1 (&arg, 0, 0);
5904 exp_end = arg;
5905 exp_valid_block = innermost_block;
5906 mark = value_mark ();
5907 val = evaluate_expression (exp);
5908 release_value (val);
d69fe07e 5909 if (value_lazy (val))
c906108c
SS
5910 value_fetch_lazy (val);
5911
5912 tok = arg;
5913 while (*tok == ' ' || *tok == '\t')
5914 tok++;
5915 end_tok = tok;
5916
5917 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5918 end_tok++;
5919
5920 toklen = end_tok - tok;
5921 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5922 {
5923 tok = cond_start = end_tok + 1;
5924 cond = parse_exp_1 (&tok, 0, 0);
5925 cond_end = tok;
5926 }
5927 if (*tok)
8a3fe4f8 5928 error (_("Junk at end of command."));
c906108c 5929
53a5351d 5930 if (accessflag == hw_read)
c5aa993b 5931 bp_type = bp_read_watchpoint;
53a5351d 5932 else if (accessflag == hw_access)
c5aa993b
JM
5933 bp_type = bp_access_watchpoint;
5934 else
5935 bp_type = bp_hardware_watchpoint;
c906108c
SS
5936
5937 mem_cnt = can_use_hardware_watchpoint (val);
5938 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 5939 error (_("Expression cannot be implemented with read/access watchpoint."));
c5aa993b
JM
5940 if (mem_cnt != 0)
5941 {
5942 i = hw_watchpoint_used_count (bp_type, &other_type_used);
53a5351d
JM
5943 target_resources_ok =
5944 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt,
5945 other_type_used);
c5aa993b 5946 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 5947 error (_("Target does not support this type of hardware watchpoint."));
53a5351d 5948
c5aa993b 5949 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
8a3fe4f8 5950 error (_("Target can only support one kind of HW watchpoint at a time."));
c5aa993b 5951 }
c906108c 5952
4d28f7a8
KB
5953 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
5954 watchpoint could not be set. */
5955 if (!mem_cnt || target_resources_ok <= 0)
5956 bp_type = bp_watchpoint;
5957
d983da9c
DJ
5958 frame = block_innermost_frame (exp_valid_block);
5959 if (frame)
5960 prev_frame = get_prev_frame (frame);
5961 else
5962 prev_frame = NULL;
5963
5964 /* If the expression is "local", then set up a "watchpoint scope"
5965 breakpoint at the point where we've left the scope of the watchpoint
5966 expression. Create the scope breakpoint before the watchpoint, so
5967 that we will encounter it first in bpstat_stop_status. */
5968 if (innermost_block && prev_frame)
5969 {
5970 scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
5971 bp_watchpoint_scope);
5972
5973 scope_breakpoint->enable_state = bp_enabled;
5974
5975 /* Automatically delete the breakpoint when it hits. */
5976 scope_breakpoint->disposition = disp_del;
5977
5978 /* Only break in the proper frame (help with recursion). */
5979 scope_breakpoint->frame_id = get_frame_id (prev_frame);
5980
5981 /* Set the address at which we will stop. */
5982 scope_breakpoint->loc->requested_address
5983 = get_frame_pc (prev_frame);
5984 scope_breakpoint->loc->address
5985 = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
5986 scope_breakpoint->type);
5987 }
5988
c906108c 5989 /* Now set up the breakpoint. */
4d28f7a8 5990 b = set_raw_breakpoint (sal, bp_type);
c906108c
SS
5991 set_breakpoint_count (breakpoint_count + 1);
5992 b->number = breakpoint_count;
b5de0fa7 5993 b->disposition = disp_donttouch;
c906108c
SS
5994 b->exp = exp;
5995 b->exp_valid_block = exp_valid_block;
5996 b->exp_string = savestring (exp_start, exp_end - exp_start);
5997 b->val = val;
511a6cd4 5998 b->loc->cond = cond;
c906108c
SS
5999 if (cond_start)
6000 b->cond_string = savestring (cond_start, cond_end - cond_start);
6001 else
6002 b->cond_string = 0;
c5aa993b 6003
c906108c 6004 if (frame)
d983da9c 6005 b->watchpoint_frame = get_frame_id (frame);
c906108c 6006 else
35a487f1 6007 b->watchpoint_frame = null_frame_id;
c906108c 6008
d983da9c 6009 if (scope_breakpoint != NULL)
c906108c 6010 {
d983da9c
DJ
6011 /* The scope breakpoint is related to the watchpoint. We will
6012 need to act on them together. */
6013 b->related_breakpoint = scope_breakpoint;
6014 scope_breakpoint->related_breakpoint = b;
c906108c 6015 }
d983da9c 6016
c906108c
SS
6017 value_free_to_mark (mark);
6018 mention (b);
6019}
6020
6021/* Return count of locations need to be watched and can be handled
6022 in hardware. If the watchpoint can not be handled
6023 in hardware return zero. */
6024
c906108c 6025static int
fba45db2 6026can_use_hardware_watchpoint (struct value *v)
c906108c
SS
6027{
6028 int found_memory_cnt = 0;
2e70b7b9 6029 struct value *head = v;
c906108c
SS
6030
6031 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 6032 if (!can_use_hw_watchpoints)
c906108c 6033 return 0;
c5aa993b 6034
5c44784c
JM
6035 /* Make sure that the value of the expression depends only upon
6036 memory contents, and values computed from them within GDB. If we
6037 find any register references or function calls, we can't use a
6038 hardware watchpoint.
6039
6040 The idea here is that evaluating an expression generates a series
6041 of values, one holding the value of every subexpression. (The
6042 expression a*b+c has five subexpressions: a, b, a*b, c, and
6043 a*b+c.) GDB's values hold almost enough information to establish
6044 the criteria given above --- they identify memory lvalues,
6045 register lvalues, computed values, etcetera. So we can evaluate
6046 the expression, and then scan the chain of values that leaves
6047 behind to decide whether we can detect any possible change to the
6048 expression's final value using only hardware watchpoints.
6049
6050 However, I don't think that the values returned by inferior
6051 function calls are special in any way. So this function may not
6052 notice that an expression involving an inferior function call
6053 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 6054 for (; v; v = value_next (v))
c906108c 6055 {
5c44784c 6056 if (VALUE_LVAL (v) == lval_memory)
c906108c 6057 {
d69fe07e 6058 if (value_lazy (v))
5c44784c
JM
6059 /* A lazy memory lvalue is one that GDB never needed to fetch;
6060 we either just used its address (e.g., `a' in `a.b') or
6061 we never needed it at all (e.g., `a' in `a,b'). */
6062 ;
53a5351d 6063 else
5c44784c
JM
6064 {
6065 /* Ahh, memory we actually used! Check if we can cover
6066 it with hardware watchpoints. */
df407dfe 6067 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
6068
6069 /* We only watch structs and arrays if user asked for it
6070 explicitly, never if they just happen to appear in a
6071 middle of some value chain. */
6072 if (v == head
6073 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
6074 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
6075 {
df407dfe
AC
6076 CORE_ADDR vaddr = VALUE_ADDRESS (v) + value_offset (v);
6077 int len = TYPE_LENGTH (value_type (v));
2e70b7b9
MS
6078
6079 if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
6080 return 0;
6081 else
6082 found_memory_cnt++;
6083 }
5c44784c 6084 }
c5aa993b 6085 }
5086187c
AC
6086 else if (VALUE_LVAL (v) != not_lval
6087 && deprecated_value_modifiable (v) == 0)
53a5351d 6088 return 0; /* ??? What does this represent? */
5086187c 6089 else if (VALUE_LVAL (v) == lval_register)
53a5351d 6090 return 0; /* cannot watch a register with a HW watchpoint */
c906108c
SS
6091 }
6092
6093 /* The expression itself looks suitable for using a hardware
6094 watchpoint, but give the target machine a chance to reject it. */
6095 return found_memory_cnt;
6096}
6097
8b93c638 6098void
fba45db2 6099watch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6100{
6101 watch_command (arg, from_tty);
6102}
8926118c 6103
c5aa993b 6104static void
fba45db2 6105watch_command (char *arg, int from_tty)
c906108c 6106{
53a5351d 6107 watch_command_1 (arg, hw_write, from_tty);
c906108c
SS
6108}
6109
8b93c638 6110void
fba45db2 6111rwatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6112{
6113 rwatch_command (arg, from_tty);
6114}
8926118c 6115
c5aa993b 6116static void
fba45db2 6117rwatch_command (char *arg, int from_tty)
c906108c 6118{
53a5351d 6119 watch_command_1 (arg, hw_read, from_tty);
c906108c
SS
6120}
6121
8b93c638 6122void
fba45db2 6123awatch_command_wrapper (char *arg, int from_tty)
8b93c638
JM
6124{
6125 awatch_command (arg, from_tty);
6126}
8926118c 6127
c5aa993b 6128static void
fba45db2 6129awatch_command (char *arg, int from_tty)
c906108c 6130{
53a5351d 6131 watch_command_1 (arg, hw_access, from_tty);
c906108c 6132}
c906108c 6133\f
c5aa993b 6134
43ff13b4 6135/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
6136 because it uses the mechanisms of breakpoints. */
6137
43ff13b4
JM
6138/* This function is called by fetch_inferior_event via the
6139 cmd_continuation pointer, to complete the until command. It takes
6140 care of cleaning up the temporary breakpoints set up by the until
6141 command. */
c2c6d25f
JM
6142static void
6143until_break_command_continuation (struct continuation_arg *arg)
43ff13b4 6144{
0d06e24b
JM
6145 struct cleanup *cleanups;
6146
57e687d9 6147 cleanups = (struct cleanup *) arg->data.pointer;
0d06e24b 6148 do_exec_cleanups (cleanups);
43ff13b4
JM
6149}
6150
c906108c 6151void
ae66c1fc 6152until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
6153{
6154 struct symtabs_and_lines sals;
6155 struct symtab_and_line sal;
206415a3
DJ
6156 struct frame_info *frame = get_selected_frame (NULL);
6157 struct frame_info *prev_frame = get_prev_frame (frame);
c906108c
SS
6158 struct breakpoint *breakpoint;
6159 struct cleanup *old_chain;
0d06e24b
JM
6160 struct continuation_arg *arg1;
6161
c906108c
SS
6162
6163 clear_proceed_status ();
6164
6165 /* Set a breakpoint where the user wants it and at return from
6166 this function */
c5aa993b 6167
c906108c
SS
6168 if (default_breakpoint_valid)
6169 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
68219205 6170 default_breakpoint_line, (char ***) NULL, NULL);
c906108c 6171 else
53a5351d 6172 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
68219205 6173 0, (char ***) NULL, NULL);
c5aa993b 6174
c906108c 6175 if (sals.nelts != 1)
8a3fe4f8 6176 error (_("Couldn't get information on specified line."));
c5aa993b 6177
c906108c 6178 sal = sals.sals[0];
b8c9b27d 6179 xfree (sals.sals); /* malloc'd, so freed */
c5aa993b 6180
c906108c 6181 if (*arg)
8a3fe4f8 6182 error (_("Junk at end of arguments."));
c5aa993b 6183
c906108c 6184 resolve_sal_pc (&sal);
c5aa993b 6185
ae66c1fc
EZ
6186 if (anywhere)
6187 /* If the user told us to continue until a specified location,
6188 we don't specify a frame at which we need to stop. */
6189 breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
6190 else
6191 /* Otherwise, specify the current frame, because we want to stop only
6192 at the very same frame. */
206415a3 6193 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame),
ae66c1fc 6194 bp_until);
c5aa993b 6195
362646f5 6196 if (!target_can_async_p ())
4d6140d9 6197 old_chain = make_cleanup_delete_breakpoint (breakpoint);
43ff13b4 6198 else
4d6140d9 6199 old_chain = make_exec_cleanup_delete_breakpoint (breakpoint);
43ff13b4
JM
6200
6201 /* If we are running asynchronously, and the target supports async
6202 execution, we are not waiting for the target to stop, in the call
6203 tp proceed, below. This means that we cannot delete the
6204 brekpoints until the target has actually stopped. The only place
6205 where we get a chance to do that is in fetch_inferior_event, so
6206 we must set things up for that. */
6207
362646f5 6208 if (target_can_async_p ())
43ff13b4 6209 {
0d06e24b
JM
6210 /* In this case the arg for the continuation is just the point
6211 in the exec_cleanups chain from where to start doing
6212 cleanups, because all the continuation does is the cleanups in
6213 the exec_cleanup_chain. */
6214 arg1 =
6215 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
57e687d9
MS
6216 arg1->next = NULL;
6217 arg1->data.pointer = old_chain;
0d06e24b
JM
6218
6219 add_continuation (until_break_command_continuation, arg1);
43ff13b4 6220 }
c906108c 6221
ae66c1fc
EZ
6222 /* Keep within the current frame, or in frames called by the current
6223 one. */
c906108c
SS
6224 if (prev_frame)
6225 {
30f7db39
AC
6226 sal = find_pc_line (get_frame_pc (prev_frame), 0);
6227 sal.pc = get_frame_pc (prev_frame);
818dd999
AC
6228 breakpoint = set_momentary_breakpoint (sal, get_frame_id (prev_frame),
6229 bp_until);
362646f5 6230 if (!target_can_async_p ())
4d6140d9 6231 make_cleanup_delete_breakpoint (breakpoint);
43ff13b4 6232 else
4d6140d9 6233 make_exec_cleanup_delete_breakpoint (breakpoint);
c906108c 6234 }
c5aa993b 6235
c906108c 6236 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
43ff13b4
JM
6237 /* Do the cleanups now, anly if we are not running asynchronously,
6238 of if we are, but the target is still synchronous. */
362646f5 6239 if (!target_can_async_p ())
c5aa993b 6240 do_cleanups (old_chain);
c906108c 6241}
ae66c1fc 6242
c906108c 6243static void
fba45db2 6244ep_skip_leading_whitespace (char **s)
c906108c 6245{
c5aa993b
JM
6246 if ((s == NULL) || (*s == NULL))
6247 return;
6248 while (isspace (**s))
6249 *s += 1;
c906108c 6250}
c5aa993b 6251
c906108c
SS
6252/* This function examines a string, and attempts to find a token
6253 that might be an event name in the leading characters. If a
6254 possible match is found, a pointer to the last character of
6255 the token is returned. Else, NULL is returned. */
53a5351d 6256
c906108c 6257static char *
fba45db2 6258ep_find_event_name_end (char *arg)
c906108c 6259{
c5aa993b
JM
6260 char *s = arg;
6261 char *event_name_end = NULL;
6262
c906108c
SS
6263 /* If we could depend upon the presense of strrpbrk, we'd use that... */
6264 if (arg == NULL)
6265 return NULL;
c5aa993b 6266
c906108c 6267 /* We break out of the loop when we find a token delimiter.
c5aa993b
JM
6268 Basically, we're looking for alphanumerics and underscores;
6269 anything else delimites the token. */
c906108c
SS
6270 while (*s != '\0')
6271 {
c5aa993b
JM
6272 if (!isalnum (*s) && (*s != '_'))
6273 break;
c906108c
SS
6274 event_name_end = s;
6275 s++;
6276 }
c5aa993b 6277
c906108c
SS
6278 return event_name_end;
6279}
6280
c5aa993b 6281
c906108c
SS
6282/* This function attempts to parse an optional "if <cond>" clause
6283 from the arg string. If one is not found, it returns NULL.
c5aa993b 6284
c906108c
SS
6285 Else, it returns a pointer to the condition string. (It does not
6286 attempt to evaluate the string against a particular block.) And,
6287 it updates arg to point to the first character following the parsed
6288 if clause in the arg string. */
53a5351d 6289
c906108c 6290static char *
fba45db2 6291ep_parse_optional_if_clause (char **arg)
c906108c 6292{
c5aa993b
JM
6293 char *cond_string;
6294
6295 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 6296 return NULL;
c5aa993b 6297
c906108c
SS
6298 /* Skip the "if" keyword. */
6299 (*arg) += 2;
c5aa993b 6300
c906108c
SS
6301 /* Skip any extra leading whitespace, and record the start of the
6302 condition string. */
6303 ep_skip_leading_whitespace (arg);
6304 cond_string = *arg;
c5aa993b 6305
c906108c
SS
6306 /* Assume that the condition occupies the remainder of the arg string. */
6307 (*arg) += strlen (cond_string);
c5aa993b 6308
c906108c
SS
6309 return cond_string;
6310}
c5aa993b 6311
c906108c
SS
6312/* This function attempts to parse an optional filename from the arg
6313 string. If one is not found, it returns NULL.
c5aa993b 6314
c906108c
SS
6315 Else, it returns a pointer to the parsed filename. (This function
6316 makes no attempt to verify that a file of that name exists, or is
6317 accessible.) And, it updates arg to point to the first character
6318 following the parsed filename in the arg string.
c5aa993b 6319
c906108c
SS
6320 Note that clients needing to preserve the returned filename for
6321 future access should copy it to their own buffers. */
6322static char *
fba45db2 6323ep_parse_optional_filename (char **arg)
c906108c 6324{
c5aa993b
JM
6325 static char filename[1024];
6326 char *arg_p = *arg;
6327 int i;
6328 char c;
6329
c906108c
SS
6330 if ((*arg_p == '\0') || isspace (*arg_p))
6331 return NULL;
c5aa993b
JM
6332
6333 for (i = 0;; i++)
c906108c
SS
6334 {
6335 c = *arg_p;
6336 if (isspace (c))
c5aa993b 6337 c = '\0';
c906108c
SS
6338 filename[i] = c;
6339 if (c == '\0')
c5aa993b 6340 break;
c906108c
SS
6341 arg_p++;
6342 }
6343 *arg = arg_p;
c5aa993b 6344
c906108c
SS
6345 return filename;
6346}
c5aa993b 6347
c906108c
SS
6348/* Commands to deal with catching events, such as signals, exceptions,
6349 process start/exit, etc. */
c5aa993b
JM
6350
6351typedef enum
6352{
6353 catch_fork, catch_vfork
6354}
6355catch_fork_kind;
6356
c906108c 6357static void
fba45db2
KB
6358catch_fork_command_1 (catch_fork_kind fork_kind, char *arg, int tempflag,
6359 int from_tty)
c906108c 6360{
c5aa993b
JM
6361 char *cond_string = NULL;
6362
c906108c 6363 ep_skip_leading_whitespace (&arg);
c5aa993b 6364
c906108c 6365 /* The allowed syntax is:
c5aa993b
JM
6366 catch [v]fork
6367 catch [v]fork if <cond>
6368
c906108c
SS
6369 First, check if there's an if clause. */
6370 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 6371
c906108c 6372 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6373 error (_("Junk at end of arguments."));
c5aa993b 6374
c906108c
SS
6375 /* If this target supports it, create a fork or vfork catchpoint
6376 and enable reporting of such events. */
c5aa993b
JM
6377 switch (fork_kind)
6378 {
6379 case catch_fork:
c906108c
SS
6380 create_fork_event_catchpoint (tempflag, cond_string);
6381 break;
c5aa993b 6382 case catch_vfork:
c906108c
SS
6383 create_vfork_event_catchpoint (tempflag, cond_string);
6384 break;
c5aa993b 6385 default:
8a3fe4f8 6386 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 6387 break;
c5aa993b 6388 }
c906108c
SS
6389}
6390
6391static void
fba45db2 6392catch_exec_command_1 (char *arg, int tempflag, int from_tty)
c906108c 6393{
c5aa993b 6394 char *cond_string = NULL;
c906108c
SS
6395
6396 ep_skip_leading_whitespace (&arg);
6397
6398 /* The allowed syntax is:
c5aa993b
JM
6399 catch exec
6400 catch exec if <cond>
c906108c
SS
6401
6402 First, check if there's an if clause. */
6403 cond_string = ep_parse_optional_if_clause (&arg);
6404
6405 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6406 error (_("Junk at end of arguments."));
c906108c
SS
6407
6408 /* If this target supports it, create an exec catchpoint
6409 and enable reporting of such events. */
6410 create_exec_event_catchpoint (tempflag, cond_string);
6411}
c5aa993b 6412
c906108c 6413static void
fba45db2 6414catch_load_command_1 (char *arg, int tempflag, int from_tty)
c906108c 6415{
c5aa993b
JM
6416 char *dll_pathname = NULL;
6417 char *cond_string = NULL;
6418
c906108c 6419 ep_skip_leading_whitespace (&arg);
c5aa993b 6420
c906108c 6421 /* The allowed syntax is:
c5aa993b
JM
6422 catch load
6423 catch load if <cond>
6424 catch load <filename>
6425 catch load <filename> if <cond>
6426
c906108c
SS
6427 The user is not allowed to specify the <filename> after an
6428 if clause.
c5aa993b 6429
c906108c 6430 We'll ignore the pathological case of a file named "if".
c5aa993b 6431
c906108c
SS
6432 First, check if there's an if clause. If so, then there
6433 cannot be a filename. */
6434 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 6435
c906108c
SS
6436 /* If there was an if clause, then there cannot be a filename.
6437 Else, there might be a filename and an if clause. */
6438 if (cond_string == NULL)
6439 {
6440 dll_pathname = ep_parse_optional_filename (&arg);
6441 ep_skip_leading_whitespace (&arg);
6442 cond_string = ep_parse_optional_if_clause (&arg);
6443 }
c5aa993b 6444
c906108c 6445 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6446 error (_("Junk at end of arguments."));
c5aa993b 6447
c906108c
SS
6448 /* Create a load breakpoint that only triggers when a load of
6449 the specified dll (or any dll, if no pathname was specified)
6450 occurs. */
39f77062 6451 SOLIB_CREATE_CATCH_LOAD_HOOK (PIDGET (inferior_ptid), tempflag,
53a5351d 6452 dll_pathname, cond_string);
c906108c 6453}
c5aa993b 6454
c906108c 6455static void
fba45db2 6456catch_unload_command_1 (char *arg, int tempflag, int from_tty)
c906108c 6457{
c5aa993b
JM
6458 char *dll_pathname = NULL;
6459 char *cond_string = NULL;
6460
c906108c 6461 ep_skip_leading_whitespace (&arg);
c5aa993b 6462
c906108c 6463 /* The allowed syntax is:
c5aa993b
JM
6464 catch unload
6465 catch unload if <cond>
6466 catch unload <filename>
6467 catch unload <filename> if <cond>
6468
c906108c
SS
6469 The user is not allowed to specify the <filename> after an
6470 if clause.
c5aa993b 6471
c906108c 6472 We'll ignore the pathological case of a file named "if".
c5aa993b 6473
c906108c
SS
6474 First, check if there's an if clause. If so, then there
6475 cannot be a filename. */
6476 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 6477
c906108c
SS
6478 /* If there was an if clause, then there cannot be a filename.
6479 Else, there might be a filename and an if clause. */
6480 if (cond_string == NULL)
6481 {
6482 dll_pathname = ep_parse_optional_filename (&arg);
6483 ep_skip_leading_whitespace (&arg);
6484 cond_string = ep_parse_optional_if_clause (&arg);
6485 }
c5aa993b 6486
c906108c 6487 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6488 error (_("Junk at end of arguments."));
c5aa993b 6489
c906108c
SS
6490 /* Create an unload breakpoint that only triggers when an unload of
6491 the specified dll (or any dll, if no pathname was specified)
6492 occurs. */
39f77062 6493 SOLIB_CREATE_CATCH_UNLOAD_HOOK (PIDGET (inferior_ptid), tempflag,
53a5351d 6494 dll_pathname, cond_string);
c906108c 6495}
c906108c
SS
6496
6497/* Commands to deal with catching exceptions. */
6498
6499/* Set a breakpoint at the specified callback routine for an
c5aa993b 6500 exception event callback */
c906108c
SS
6501
6502static void
fba45db2
KB
6503create_exception_catchpoint (int tempflag, char *cond_string,
6504 enum exception_event_kind ex_event,
6505 struct symtab_and_line *sal)
c906108c 6506{
c5aa993b 6507 struct breakpoint *b;
c5aa993b 6508 int thread = -1; /* All threads. */
4d28f7a8 6509 enum bptype bptype;
c906108c 6510
c5aa993b 6511 if (!sal) /* no exception support? */
c906108c
SS
6512 return;
6513
c906108c
SS
6514 switch (ex_event)
6515 {
c5aa993b 6516 case EX_EVENT_THROW:
4d28f7a8 6517 bptype = bp_catch_throw;
c5aa993b
JM
6518 break;
6519 case EX_EVENT_CATCH:
4d28f7a8 6520 bptype = bp_catch_catch;
c5aa993b
JM
6521 break;
6522 default: /* error condition */
8a3fe4f8 6523 error (_("Internal error -- invalid catchpoint kind"));
c906108c 6524 }
4d28f7a8
KB
6525
6526 b = set_raw_breakpoint (*sal, bptype);
6527 set_breakpoint_count (breakpoint_count + 1);
6528 b->number = breakpoint_count;
4d28f7a8
KB
6529 b->cond_string = (cond_string == NULL) ?
6530 NULL : savestring (cond_string, strlen (cond_string));
6531 b->thread = thread;
6532 b->addr_string = NULL;
b5de0fa7
EZ
6533 b->enable_state = bp_enabled;
6534 b->disposition = tempflag ? disp_del : disp_donttouch;
c906108c
SS
6535 mention (b);
6536}
6537
3086aeae
DJ
6538static enum print_stop_action
6539print_exception_catchpoint (struct breakpoint *b)
6540{
6541 annotate_catchpoint (b->number);
6542
6543 if (strstr (b->addr_string, "throw") != NULL)
a3f17187 6544 printf_filtered (_("\nCatchpoint %d (exception thrown)\n"),
3086aeae
DJ
6545 b->number);
6546 else
a3f17187 6547 printf_filtered (_("\nCatchpoint %d (exception caught)\n"),
3086aeae
DJ
6548 b->number);
6549
6550 return PRINT_SRC_AND_LOC;
6551}
6552
6553static void
6554print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6555{
6556 if (addressprint)
6557 {
6558 annotate_field (4);
5cab636d 6559 ui_out_field_core_addr (uiout, "addr", b->loc->address);
3086aeae
DJ
6560 }
6561 annotate_field (5);
5cab636d 6562 *last_addr = b->loc->address;
3086aeae
DJ
6563 if (strstr (b->addr_string, "throw") != NULL)
6564 ui_out_field_string (uiout, "what", "exception throw");
6565 else
6566 ui_out_field_string (uiout, "what", "exception catch");
6567}
6568
6569static void
6570print_mention_exception_catchpoint (struct breakpoint *b)
6571{
6572 if (strstr (b->addr_string, "throw") != NULL)
a3f17187 6573 printf_filtered (_("Catchpoint %d (throw)"), b->number);
3086aeae 6574 else
a3f17187 6575 printf_filtered (_("Catchpoint %d (catch)"), b->number);
3086aeae
DJ
6576}
6577
6578static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
6579 print_exception_catchpoint,
6580 print_one_exception_catchpoint,
6581 print_mention_exception_catchpoint
6582};
6583
6584static int
6585handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6586 enum exception_event_kind ex_event, int from_tty)
6587{
6588 char *trigger_func_name, *nameptr;
6589 struct symtabs_and_lines sals;
6590 struct breakpoint *b;
6591
6592 if (ex_event == EX_EVENT_CATCH)
6593 trigger_func_name = xstrdup ("__cxa_begin_catch");
6594 else
6595 trigger_func_name = xstrdup ("__cxa_throw");
6596
6597 nameptr = trigger_func_name;
68219205 6598 sals = decode_line_1 (&nameptr, 1, NULL, 0, NULL, NULL);
3086aeae
DJ
6599 if (sals.nelts == 0)
6600 {
b59661bd 6601 xfree (trigger_func_name);
3086aeae
DJ
6602 return 0;
6603 }
6604
6605 b = set_raw_breakpoint (sals.sals[0], bp_breakpoint);
6606 set_breakpoint_count (breakpoint_count + 1);
6607 b->number = breakpoint_count;
3086aeae
DJ
6608 b->cond_string = (cond_string == NULL) ?
6609 NULL : savestring (cond_string, strlen (cond_string));
6610 b->thread = -1;
6611 b->addr_string = trigger_func_name;
6612 b->enable_state = bp_enabled;
6613 b->disposition = tempflag ? disp_del : disp_donttouch;
6614 b->ops = &gnu_v3_exception_catchpoint_ops;
6615
b59661bd 6616 xfree (sals.sals);
3086aeae
DJ
6617 mention (b);
6618 return 1;
6619}
6620
c5aa993b 6621/* Deal with "catch catch" and "catch throw" commands */
c906108c
SS
6622
6623static void
fba45db2
KB
6624catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6625 int tempflag, int from_tty)
c906108c 6626{
c5aa993b
JM
6627 char *cond_string = NULL;
6628 struct symtab_and_line *sal = NULL;
6629
c906108c 6630 ep_skip_leading_whitespace (&arg);
c5aa993b 6631
c906108c
SS
6632 cond_string = ep_parse_optional_if_clause (&arg);
6633
6634 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 6635 error (_("Junk at end of arguments."));
c906108c
SS
6636
6637 if ((ex_event != EX_EVENT_THROW) &&
6638 (ex_event != EX_EVENT_CATCH))
8a3fe4f8 6639 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 6640
3086aeae
DJ
6641 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6642 return;
6643
c906108c
SS
6644 /* See if we can find a callback routine */
6645 sal = target_enable_exception_callback (ex_event, 1);
6646
c5aa993b 6647 if (sal)
c906108c
SS
6648 {
6649 /* We have callbacks from the runtime system for exceptions.
c5aa993b 6650 Set a breakpoint on the sal found, if no errors */
c906108c 6651 if (sal != (struct symtab_and_line *) -1)
c5aa993b 6652 create_exception_catchpoint (tempflag, cond_string, ex_event, sal);
c906108c 6653 else
53a5351d 6654 return; /* something went wrong with setting up callbacks */
c906108c 6655 }
c5aa993b 6656
8a3fe4f8 6657 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
6658}
6659
f7f9143b
JB
6660/* Create a breakpoint struct for Ada exception catchpoints. */
6661
6662static void
6663create_ada_exception_breakpoint (struct symtab_and_line sal,
6664 char *addr_string,
6665 char *exp_string,
6666 char *cond_string,
6667 struct expression *cond,
6668 struct breakpoint_ops *ops,
6669 int tempflag,
6670 int from_tty)
6671{
6672 struct breakpoint *b;
6673
6674 if (from_tty)
6675 {
6676 describe_other_breakpoints (sal.pc, sal.section, -1);
6677 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
6678 version for exception catchpoints, because two catchpoints
6679 used for different exception names will use the same address.
6680 In this case, a "breakpoint ... also set at..." warning is
6681 unproductive. Besides. the warning phrasing is also a bit
6682 inapropriate, we should use the word catchpoint, and tell
6683 the user what type of catchpoint it is. The above is good
6684 enough for now, though. */
6685 }
6686
6687 b = set_raw_breakpoint (sal, bp_breakpoint);
6688 set_breakpoint_count (breakpoint_count + 1);
6689
6690 b->enable_state = bp_enabled;
6691 b->disposition = tempflag ? disp_del : disp_donttouch;
6692 b->number = breakpoint_count;
6693 b->ignore_count = 0;
511a6cd4 6694 b->loc->cond = cond;
f7f9143b
JB
6695 b->addr_string = addr_string;
6696 b->language = language_ada;
6697 b->cond_string = cond_string;
6698 b->exp_string = exp_string;
6699 b->thread = -1;
6700 b->ops = ops;
f7f9143b
JB
6701
6702 mention (b);
6703}
6704
6705/* Implement the "catch exception" command. */
6706
6707static void
6708catch_ada_exception_command (char *arg, int tempflag, int from_tty)
6709{
6710 struct symtab_and_line sal;
6711 enum bptype type;
6712 char *addr_string = NULL;
6713 char *exp_string = NULL;
6714 char *cond_string = NULL;
6715 struct expression *cond = NULL;
6716 struct breakpoint_ops *ops = NULL;
6717
6718 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
6719 &cond_string, &cond, &ops);
6720 create_ada_exception_breakpoint (sal, addr_string, exp_string,
6721 cond_string, cond, ops, tempflag,
6722 from_tty);
6723}
6724
6725/* Implement the "catch assert" command. */
6726
6727static void
6728catch_assert_command (char *arg, int tempflag, int from_tty)
6729{
6730 struct symtab_and_line sal;
6731 char *addr_string = NULL;
6732 struct breakpoint_ops *ops = NULL;
6733
6734 sal = ada_decode_assert_location (arg, &addr_string, &ops);
6735 create_ada_exception_breakpoint (sal, addr_string, NULL, NULL, NULL, ops,
6736 tempflag, from_tty);
6737}
6738
c906108c
SS
6739/* Cover routine to allow wrapping target_enable_exception_catchpoints
6740 inside a catch_errors */
6741
6742static int
4efb68b1 6743cover_target_enable_exception_callback (void *arg)
c906108c
SS
6744{
6745 args_for_catchpoint_enable *args = arg;
6746 struct symtab_and_line *sal;
b5de0fa7 6747 sal = target_enable_exception_callback (args->kind, args->enable_p);
c906108c
SS
6748 if (sal == NULL)
6749 return 0;
6750 else if (sal == (struct symtab_and_line *) -1)
6751 return -1;
6752 else
c5aa993b 6753 return 1; /*is valid */
c906108c
SS
6754}
6755
c906108c 6756static void
fba45db2 6757catch_command_1 (char *arg, int tempflag, int from_tty)
c906108c 6758{
c5aa993b 6759
c906108c
SS
6760 /* The first argument may be an event name, such as "start" or "load".
6761 If so, then handle it as such. If it doesn't match an event name,
6762 then attempt to interpret it as an exception name. (This latter is
6763 the v4.16-and-earlier GDB meaning of the "catch" command.)
c5aa993b 6764
c906108c 6765 First, try to find the bounds of what might be an event name. */
c5aa993b
JM
6766 char *arg1_start = arg;
6767 char *arg1_end;
6768 int arg1_length;
6769
c906108c
SS
6770 if (arg1_start == NULL)
6771 {
c5aa993b 6772 /* Old behaviour was to use pre-v-4.16 syntax */
c906108c
SS
6773 /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
6774 /* return; */
c5aa993b 6775 /* Now, this is not allowed */
8a3fe4f8 6776 error (_("Catch requires an event name."));
c906108c
SS
6777
6778 }
6779 arg1_end = ep_find_event_name_end (arg1_start);
6780 if (arg1_end == NULL)
8a3fe4f8 6781 error (_("catch requires an event"));
c906108c 6782 arg1_length = arg1_end + 1 - arg1_start;
c5aa993b 6783
c906108c
SS
6784 /* Try to match what we found against known event names. */
6785 if (strncmp (arg1_start, "signal", arg1_length) == 0)
6786 {
8a3fe4f8 6787 error (_("Catch of signal not yet implemented"));
c906108c
SS
6788 }
6789 else if (strncmp (arg1_start, "catch", arg1_length) == 0)
6790 {
53a5351d
JM
6791 catch_exception_command_1 (EX_EVENT_CATCH, arg1_end + 1,
6792 tempflag, from_tty);
c906108c
SS
6793 }
6794 else if (strncmp (arg1_start, "throw", arg1_length) == 0)
6795 {
53a5351d
JM
6796 catch_exception_command_1 (EX_EVENT_THROW, arg1_end + 1,
6797 tempflag, from_tty);
c906108c
SS
6798 }
6799 else if (strncmp (arg1_start, "thread_start", arg1_length) == 0)
6800 {
8a3fe4f8 6801 error (_("Catch of thread_start not yet implemented"));
c906108c
SS
6802 }
6803 else if (strncmp (arg1_start, "thread_exit", arg1_length) == 0)
6804 {
8a3fe4f8 6805 error (_("Catch of thread_exit not yet implemented"));
c906108c
SS
6806 }
6807 else if (strncmp (arg1_start, "thread_join", arg1_length) == 0)
6808 {
8a3fe4f8 6809 error (_("Catch of thread_join not yet implemented"));
c906108c
SS
6810 }
6811 else if (strncmp (arg1_start, "start", arg1_length) == 0)
6812 {
8a3fe4f8 6813 error (_("Catch of start not yet implemented"));
c906108c
SS
6814 }
6815 else if (strncmp (arg1_start, "exit", arg1_length) == 0)
6816 {
8a3fe4f8 6817 error (_("Catch of exit not yet implemented"));
c906108c
SS
6818 }
6819 else if (strncmp (arg1_start, "fork", arg1_length) == 0)
6820 {
c5aa993b 6821 catch_fork_command_1 (catch_fork, arg1_end + 1, tempflag, from_tty);
c906108c
SS
6822 }
6823 else if (strncmp (arg1_start, "vfork", arg1_length) == 0)
6824 {
c5aa993b 6825 catch_fork_command_1 (catch_vfork, arg1_end + 1, tempflag, from_tty);
c906108c
SS
6826 }
6827 else if (strncmp (arg1_start, "exec", arg1_length) == 0)
6828 {
c5aa993b 6829 catch_exec_command_1 (arg1_end + 1, tempflag, from_tty);
c906108c
SS
6830 }
6831 else if (strncmp (arg1_start, "load", arg1_length) == 0)
6832 {
c5aa993b 6833 catch_load_command_1 (arg1_end + 1, tempflag, from_tty);
c906108c
SS
6834 }
6835 else if (strncmp (arg1_start, "unload", arg1_length) == 0)
6836 {
c5aa993b 6837 catch_unload_command_1 (arg1_end + 1, tempflag, from_tty);
c906108c
SS
6838 }
6839 else if (strncmp (arg1_start, "stop", arg1_length) == 0)
6840 {
8a3fe4f8 6841 error (_("Catch of stop not yet implemented"));
c906108c 6842 }
f7f9143b
JB
6843 else if (strncmp (arg1_start, "exception", arg1_length) == 0)
6844 {
6845 catch_ada_exception_command (arg1_end + 1, tempflag, from_tty);
6846 }
6847
6848 else if (strncmp (arg1_start, "assert", arg1_length) == 0)
6849 {
6850 catch_assert_command (arg1_end + 1, tempflag, from_tty);
6851 }
c5aa993b 6852
c906108c
SS
6853 /* This doesn't appear to be an event name */
6854
6855 else
6856 {
6857 /* Pre-v.4.16 behaviour was to treat the argument
c5aa993b 6858 as the name of an exception */
c906108c 6859 /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
c5aa993b 6860 /* Now this is not allowed */
8a3fe4f8 6861 error (_("Unknown event kind specified for catch"));
c906108c
SS
6862
6863 }
6864}
6865
c906108c 6866static void
fba45db2 6867catch_command (char *arg, int from_tty)
c906108c
SS
6868{
6869 catch_command_1 (arg, 0, from_tty);
6870}
6871\f
6872
6873static void
fba45db2 6874tcatch_command (char *arg, int from_tty)
c906108c
SS
6875{
6876 catch_command_1 (arg, 1, from_tty);
6877}
6878
80f8a6eb 6879/* Delete breakpoints by address or line. */
c906108c
SS
6880
6881static void
fba45db2 6882clear_command (char *arg, int from_tty)
c906108c 6883{
d6e956e5
VP
6884 struct breakpoint *b;
6885 VEC(breakpoint_p) *found = 0;
6886 int ix;
c906108c
SS
6887 int default_match;
6888 struct symtabs_and_lines sals;
6889 struct symtab_and_line sal;
c906108c
SS
6890 int i;
6891
6892 if (arg)
6893 {
6894 sals = decode_line_spec (arg, 1);
6895 default_match = 0;
6896 }
6897 else
6898 {
c5aa993b 6899 sals.sals = (struct symtab_and_line *)
c906108c 6900 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 6901 make_cleanup (xfree, sals.sals);
fe39c653 6902 init_sal (&sal); /* initialize to zeroes */
c906108c
SS
6903 sal.line = default_breakpoint_line;
6904 sal.symtab = default_breakpoint_symtab;
6905 sal.pc = default_breakpoint_address;
6906 if (sal.symtab == 0)
8a3fe4f8 6907 error (_("No source file specified."));
c906108c
SS
6908
6909 sals.sals[0] = sal;
6910 sals.nelts = 1;
6911
6912 default_match = 1;
6913 }
6914
ed0616c6
VP
6915 /* We don't call resolve_sal_pc here. That's not
6916 as bad as it seems, because all existing breakpoints
6917 typically have both file/line and pc set. So, if
6918 clear is given file/line, we can match this to existing
6919 breakpoint without obtaining pc at all.
6920
6921 We only support clearing given the address explicitly
6922 present in breakpoint table. Say, we've set breakpoint
6923 at file:line. There were several PC values for that file:line,
6924 due to optimization, all in one block.
6925 We've picked one PC value. If "clear" is issued with another
6926 PC corresponding to the same file:line, the breakpoint won't
6927 be cleared. We probably can still clear the breakpoint, but
6928 since the other PC value is never presented to user, user
6929 can only find it by guessing, and it does not seem important
6930 to support that. */
6931
c906108c 6932 /* For each line spec given, delete bps which correspond
80f8a6eb
MS
6933 to it. Do it in two passes, solely to preserve the current
6934 behavior that from_tty is forced true if we delete more than
6935 one breakpoint. */
c906108c 6936
80f8a6eb 6937 found = NULL;
c906108c
SS
6938 for (i = 0; i < sals.nelts; i++)
6939 {
6940 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
6941 If line given (pc == 0), clear all bpts on specified line.
6942 If defaulting, clear all bpts on default line
c906108c 6943 or at default pc.
c5aa993b
JM
6944
6945 defaulting sal.pc != 0 tests to do
6946
6947 0 1 pc
6948 1 1 pc _and_ line
6949 0 0 line
6950 1 0 <can't happen> */
c906108c
SS
6951
6952 sal = sals.sals[i];
c906108c 6953
d6e956e5
VP
6954 /* Find all matching breakpoints and add them to
6955 'found'. */
6956 ALL_BREAKPOINTS (b)
c5aa993b 6957 {
0d381245 6958 int match = 0;
80f8a6eb
MS
6959 /* Are we going to delete b? */
6960 if (b->type != bp_none
6961 && b->type != bp_watchpoint
6962 && b->type != bp_hardware_watchpoint
6963 && b->type != bp_read_watchpoint
0d381245
VP
6964 && b->type != bp_access_watchpoint)
6965 {
6966 struct bp_location *loc = b->loc;
6967 for (; loc; loc = loc->next)
6968 {
6969 int pc_match = sal.pc
6970 && (loc->address == sal.pc)
6971 && (!section_is_overlay (loc->section)
6972 || loc->section == sal.section);
6973 int line_match = ((default_match || (0 == sal.pc))
6974 && b->source_file != NULL
6975 && sal.symtab != NULL
6976 && strcmp (b->source_file, sal.symtab->filename) == 0
6977 && b->line_number == sal.line);
6978 if (pc_match || line_match)
6979 {
6980 match = 1;
6981 break;
6982 }
6983 }
6984 }
6985
6986 if (match)
d6e956e5 6987 VEC_safe_push(breakpoint_p, found, b);
c906108c 6988 }
80f8a6eb
MS
6989 }
6990 /* Now go thru the 'found' chain and delete them. */
d6e956e5 6991 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
6992 {
6993 if (arg)
8a3fe4f8 6994 error (_("No breakpoint at %s."), arg);
80f8a6eb 6995 else
8a3fe4f8 6996 error (_("No breakpoint at this line."));
80f8a6eb 6997 }
c906108c 6998
d6e956e5 6999 if (VEC_length(breakpoint_p, found) > 1)
80f8a6eb
MS
7000 from_tty = 1; /* Always report if deleted more than one */
7001 if (from_tty)
a3f17187 7002 {
d6e956e5 7003 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
7004 printf_unfiltered (_("Deleted breakpoint "));
7005 else
7006 printf_unfiltered (_("Deleted breakpoints "));
7007 }
80f8a6eb 7008 breakpoints_changed ();
d6e956e5
VP
7009
7010 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 7011 {
c5aa993b 7012 if (from_tty)
d6e956e5
VP
7013 printf_unfiltered ("%d ", b->number);
7014 delete_breakpoint (b);
c906108c 7015 }
80f8a6eb
MS
7016 if (from_tty)
7017 putchar_unfiltered ('\n');
c906108c
SS
7018}
7019\f
7020/* Delete breakpoint in BS if they are `delete' breakpoints and
7021 all breakpoints that are marked for deletion, whether hit or not.
7022 This is called after any breakpoint is hit, or after errors. */
7023
7024void
fba45db2 7025breakpoint_auto_delete (bpstat bs)
c906108c
SS
7026{
7027 struct breakpoint *b, *temp;
7028
7029 for (; bs; bs = bs->next)
4f8d1dc6 7030 if (bs->breakpoint_at && bs->breakpoint_at->owner->disposition == disp_del
c906108c 7031 && bs->stop)
4f8d1dc6 7032 delete_breakpoint (bs->breakpoint_at->owner);
c906108c
SS
7033
7034 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7035 {
b5de0fa7 7036 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
7037 delete_breakpoint (b);
7038 }
c906108c
SS
7039}
7040
0d381245
VP
7041/* Remove locations of breakpoint BPT from
7042 the global list of breakpoint locations. */
7043
7044static void
7045unlink_locations_from_global_list (struct breakpoint *bpt)
7046{
7047 /* This code assumes that the locations
7048 of a breakpoint are found in the global list
7049 in the same order, but not necessary adjacent. */
7050 struct bp_location **tmp = &bp_location_chain;
7051 struct bp_location *here = bpt->loc;
7052
7053 if (here == NULL)
7054 return;
7055
7056 for (; *tmp && here;)
7057 {
7058 if (*tmp == here)
7059 {
7060 *tmp = here->global_next;
7061 here = here->next;
7062 }
7063 else
7064 {
7065 tmp = &((*tmp)->global_next);
7066 }
7067 }
7068}
7069
53a5351d
JM
7070/* Delete a breakpoint and clean up all traces of it in the data
7071 structures. */
c906108c
SS
7072
7073void
fba45db2 7074delete_breakpoint (struct breakpoint *bpt)
c906108c 7075{
52f0bd74
AC
7076 struct breakpoint *b;
7077 bpstat bs;
7cc221ef 7078 struct bp_location *loc;
c906108c 7079
8a3fe4f8 7080 gdb_assert (bpt != NULL);
c906108c
SS
7081
7082 /* Has this bp already been deleted? This can happen because multiple
7083 lists can hold pointers to bp's. bpstat lists are especial culprits.
7084
7085 One example of this happening is a watchpoint's scope bp. When the
7086 scope bp triggers, we notice that the watchpoint is out of scope, and
7087 delete it. We also delete its scope bp. But the scope bp is marked
7088 "auto-deleting", and is already on a bpstat. That bpstat is then
7089 checked for auto-deleting bp's, which are deleted.
7090
7091 A real solution to this problem might involve reference counts in bp's,
7092 and/or giving them pointers back to their referencing bpstat's, and
7093 teaching delete_breakpoint to only free a bp's storage when no more
1272ad14 7094 references were extent. A cheaper bandaid was chosen. */
c906108c
SS
7095 if (bpt->type == bp_none)
7096 return;
7097
9a4105ab
AC
7098 if (deprecated_delete_breakpoint_hook)
7099 deprecated_delete_breakpoint_hook (bpt);
104c1213 7100 breakpoint_delete_event (bpt->number);
c906108c 7101
0d381245
VP
7102 for (loc = bpt->loc; loc; loc = loc->next)
7103 {
7104 if (loc->inserted)
7105 remove_breakpoint (loc, mark_inserted);
7106
7107 free_valchain (loc);
7108
7109 if (loc->cond)
7110 xfree (loc->cond);
c5aa993b 7111
0d381245
VP
7112 if (loc->function_name)
7113 xfree (loc->function_name);
7114 }
7270d8f2 7115
c906108c
SS
7116 if (breakpoint_chain == bpt)
7117 breakpoint_chain = bpt->next;
7118
7119 /* If we have callback-style exception catchpoints, don't go through
7120 the adjustments to the C++ runtime library etc. if the inferior
7121 isn't actually running. target_enable_exception_callback for a
7122 null target ops vector gives an undesirable error message, so we
7123 check here and avoid it. Since currently (1997-09-17) only HP-UX aCC's
1272ad14 7124 exceptions are supported in this way, it's OK for now. FIXME */
c906108c
SS
7125 if (ep_is_exception_catchpoint (bpt) && target_has_execution)
7126 {
53a5351d 7127 /* Format possible error msg */
9ebf4acf
AC
7128 char *message = xstrprintf ("Error in deleting catchpoint %d:\n",
7129 bpt->number);
7130 struct cleanup *cleanups = make_cleanup (xfree, message);
7131 args_for_catchpoint_enable args;
53a5351d
JM
7132 args.kind = bpt->type == bp_catch_catch ?
7133 EX_EVENT_CATCH : EX_EVENT_THROW;
b5de0fa7 7134 args.enable_p = 0;
c906108c
SS
7135 catch_errors (cover_target_enable_exception_callback, &args,
7136 message, RETURN_MASK_ALL);
9ebf4acf 7137 do_cleanups (cleanups);
c906108c
SS
7138 }
7139
7140
7141 ALL_BREAKPOINTS (b)
7142 if (b->next == bpt)
c5aa993b
JM
7143 {
7144 b->next = bpt->next;
7145 break;
7146 }
c906108c 7147
0d381245 7148 unlink_locations_from_global_list (bpt);
7cc221ef 7149
9f60f21b 7150 check_duplicates (bpt);
0d381245
VP
7151
7152 if (bpt->type != bp_hardware_watchpoint
c906108c
SS
7153 && bpt->type != bp_read_watchpoint
7154 && bpt->type != bp_access_watchpoint
7155 && bpt->type != bp_catch_fork
7156 && bpt->type != bp_catch_vfork
7157 && bpt->type != bp_catch_exec)
0d381245
VP
7158 for (loc = bpt->loc; loc; loc = loc->next)
7159 {
7160 /* If this breakpoint location was inserted, and there is
7161 another breakpoint at the same address, we need to
7162 insert the other breakpoint. */
7163 if (loc->inserted)
7164 {
7165 struct bp_location *loc2;
7166 ALL_BP_LOCATIONS (loc2)
7167 if (loc2->address == loc->address
7168 && loc2->section == loc->section
7169 && !loc->duplicate
7170 && loc2->owner->enable_state != bp_disabled
7171 && loc2->enabled
7172 && !loc2->shlib_disabled
7173 && loc2->owner->enable_state != bp_call_disabled)
81d0cc19 7174 {
0d381245
VP
7175 int val;
7176
7177 /* We should never reach this point if there is a permanent
7178 breakpoint at the same address as the one being deleted.
7179 If there is a permanent breakpoint somewhere, it should
7180 always be the only one inserted. */
7181 if (loc2->owner->enable_state == bp_permanent)
7182 internal_error (__FILE__, __LINE__,
7183 _("another breakpoint was inserted on top of "
7184 "a permanent breakpoint"));
7185
7186 memset (&loc2->target_info, 0, sizeof (loc2->target_info));
7187 loc2->target_info.placed_address = loc2->address;
7188 if (b->type == bp_hardware_breakpoint)
7189 val = target_insert_hw_breakpoint (&loc2->target_info);
7190 else
7191 val = target_insert_breakpoint (&loc2->target_info);
7192
7193 /* If there was an error in the insert, print a message, then stop execution. */
7194 if (val != 0)
7195 {
7196 struct ui_file *tmp_error_stream = mem_fileopen ();
7197 make_cleanup_ui_file_delete (tmp_error_stream);
7198
7199
7200 if (b->type == bp_hardware_breakpoint)
7201 {
7202 fprintf_unfiltered (tmp_error_stream,
7203 "Cannot insert hardware breakpoint %d.\n"
7204 "You may have requested too many hardware breakpoints.\n",
7205 b->number);
7206 }
7207 else
7208 {
7209 fprintf_unfiltered (tmp_error_stream, "Cannot insert breakpoint %d.\n", b->number);
7210 fprintf_filtered (tmp_error_stream, "Error accessing memory address ");
7211 deprecated_print_address_numeric (loc2->address, 1, tmp_error_stream);
7212 fprintf_filtered (tmp_error_stream, ": %s.\n",
7213 safe_strerror (val));
7214 }
7215
7216 fprintf_unfiltered (tmp_error_stream,"The same program may be running in another process.");
7217 target_terminal_ours_for_output ();
7218 error_stream(tmp_error_stream);
7219 }
7220 else
7221 loc2->inserted = 1;
7222 }
7223 }
7224 }
c906108c
SS
7225
7226 free_command_lines (&bpt->commands);
c906108c 7227 if (bpt->cond_string != NULL)
b8c9b27d 7228 xfree (bpt->cond_string);
c906108c 7229 if (bpt->addr_string != NULL)
b8c9b27d 7230 xfree (bpt->addr_string);
c906108c 7231 if (bpt->exp != NULL)
b8c9b27d 7232 xfree (bpt->exp);
c906108c 7233 if (bpt->exp_string != NULL)
b8c9b27d 7234 xfree (bpt->exp_string);
c906108c
SS
7235 if (bpt->val != NULL)
7236 value_free (bpt->val);
7237 if (bpt->source_file != NULL)
b8c9b27d 7238 xfree (bpt->source_file);
c906108c 7239 if (bpt->dll_pathname != NULL)
b8c9b27d 7240 xfree (bpt->dll_pathname);
c906108c 7241 if (bpt->triggered_dll_pathname != NULL)
b8c9b27d 7242 xfree (bpt->triggered_dll_pathname);
c906108c 7243 if (bpt->exec_pathname != NULL)
b8c9b27d 7244 xfree (bpt->exec_pathname);
c906108c
SS
7245
7246 /* Be sure no bpstat's are pointing at it after it's been freed. */
7247 /* FIXME, how can we find all bpstat's?
198757a8
VP
7248 We just check stop_bpstat for now. Note that we cannot just
7249 remove bpstats pointing at bpt from the stop_bpstat list
7250 entirely, as breakpoint commands are associated with the bpstat;
7251 if we remove it here, then the later call to
7252 bpstat_do_actions (&stop_bpstat);
7253 in event-top.c won't do anything, and temporary breakpoints
7254 with commands won't work. */
c906108c 7255 for (bs = stop_bpstat; bs; bs = bs->next)
4f8d1dc6 7256 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
c906108c
SS
7257 {
7258 bs->breakpoint_at = NULL;
c906108c 7259 bs->old_val = NULL;
c2b8ed2c 7260 /* bs->commands will be freed later. */
c906108c
SS
7261 }
7262 /* On the chance that someone will soon try again to delete this same
7263 bp, we mark it as deleted before freeing its storage. */
7264 bpt->type = bp_none;
7265
0d381245
VP
7266 for (loc = bpt->loc; loc;)
7267 {
7268 struct bp_location *loc_next = loc->next;
7269 xfree (loc);
7270 loc = loc_next;
7271 }
b8c9b27d 7272 xfree (bpt);
c906108c
SS
7273}
7274
4d6140d9
AC
7275static void
7276do_delete_breakpoint_cleanup (void *b)
7277{
7278 delete_breakpoint (b);
7279}
7280
7281struct cleanup *
7282make_cleanup_delete_breakpoint (struct breakpoint *b)
7283{
7284 return make_cleanup (do_delete_breakpoint_cleanup, b);
7285}
7286
7287struct cleanup *
7288make_exec_cleanup_delete_breakpoint (struct breakpoint *b)
7289{
7290 return make_exec_cleanup (do_delete_breakpoint_cleanup, b);
7291}
7292
c906108c 7293void
fba45db2 7294delete_command (char *arg, int from_tty)
c906108c
SS
7295{
7296 struct breakpoint *b, *temp;
7297
ea9365bb
TT
7298 dont_repeat ();
7299
c906108c
SS
7300 if (arg == 0)
7301 {
7302 int breaks_to_delete = 0;
7303
7304 /* Delete all breakpoints if no argument.
c5aa993b
JM
7305 Do not delete internal or call-dummy breakpoints, these
7306 have to be deleted with an explicit breakpoint number argument. */
7307 ALL_BREAKPOINTS (b)
7308 {
7309 if (b->type != bp_call_dummy &&
7310 b->type != bp_shlib_event &&
c4093a6a 7311 b->type != bp_thread_event &&
1900040c 7312 b->type != bp_overlay_event &&
c5aa993b 7313 b->number >= 0)
973d738b
DJ
7314 {
7315 breaks_to_delete = 1;
7316 break;
7317 }
c5aa993b 7318 }
c906108c
SS
7319
7320 /* Ask user only if there are some breakpoints to delete. */
7321 if (!from_tty
e2e0b3e5 7322 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 7323 {
c5aa993b
JM
7324 ALL_BREAKPOINTS_SAFE (b, temp)
7325 {
7326 if (b->type != bp_call_dummy &&
7327 b->type != bp_shlib_event &&
c4093a6a 7328 b->type != bp_thread_event &&
1900040c 7329 b->type != bp_overlay_event &&
c5aa993b
JM
7330 b->number >= 0)
7331 delete_breakpoint (b);
7332 }
c906108c
SS
7333 }
7334 }
7335 else
7336 map_breakpoint_numbers (arg, delete_breakpoint);
7337}
7338
0d381245
VP
7339static int
7340all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 7341{
0d381245
VP
7342 for (; loc; loc = loc->next)
7343 if (!loc->shlib_disabled)
7344 return 0;
7345 return 1;
fe3f5fa8
VP
7346}
7347
fe3f5fa8 7348static void
0d381245
VP
7349update_breakpoint_locations (struct breakpoint *b,
7350 struct symtabs_and_lines sals)
fe3f5fa8
VP
7351{
7352 int i;
7353 char *s;
0d381245
VP
7354 struct bp_location *existing_locations = b->loc;
7355
7356 /* If there's no new locations, and all existing locations
7357 are pending, don't do anything. This optimizes
7358 the common case where all locations are in the same
7359 shared library, that was unloaded. We'd like to
7360 retain the location, so that when the library
7361 is loaded again, we don't loose the enabled/disabled
7362 status of the individual locations. */
7363 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
7364 return;
7365
7366 unlink_locations_from_global_list (b);
7367 b->loc = NULL;
7368
0d381245 7369 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 7370 {
0d381245
VP
7371 struct bp_location *new_loc =
7372 add_location_to_breakpoint (b, b->type, &(sals.sals[i]));
fe3f5fa8 7373
0d381245
VP
7374 /* Reparse conditions, they might contain references to the
7375 old symtab. */
7376 if (b->cond_string != NULL)
7377 {
7378 struct gdb_exception e;
fe3f5fa8 7379
0d381245
VP
7380 s = b->cond_string;
7381 TRY_CATCH (e, RETURN_MASK_ERROR)
7382 {
7383 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7384 0);
7385 }
7386 if (e.reason < 0)
7387 {
7388 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7389 b->number, e.message);
7390 new_loc->enabled = 0;
7391 }
7392 }
fe3f5fa8 7393
0d381245
VP
7394 if (b->source_file != NULL)
7395 xfree (b->source_file);
7396 if (sals.sals[i].symtab == NULL)
7397 b->source_file = NULL;
7398 else
7399 b->source_file =
7400 savestring (sals.sals[i].symtab->filename,
7401 strlen (sals.sals[i].symtab->filename));
fe3f5fa8 7402
0d381245
VP
7403 if (b->line_number == 0)
7404 b->line_number = sals.sals[i].line;
7405 }
fe3f5fa8 7406
0d381245
VP
7407 /* If possible, carry over 'disable' status from existing breakpoints. */
7408 {
7409 struct bp_location *e = existing_locations;
7410 for (; e; e = e->next)
7411 {
7412 if (!e->enabled && e->function_name)
7413 {
7414 struct bp_location *l = b->loc;
7415 for (; l; l = l->next)
7416 if (l->function_name
7417 && strcmp (e->function_name, l->function_name) == 0)
7418 {
7419 l->enabled = 0;
7420 break;
7421 }
7422 }
7423 }
7424 }
fe3f5fa8 7425
ed0616c6
VP
7426 while (existing_locations)
7427 {
7428 struct bp_location *next = existing_locations->next;
7429 free_bp_location (existing_locations);
7430 existing_locations = next;
7431 }
fe3f5fa8
VP
7432}
7433
7434
c906108c
SS
7435/* Reset a breakpoint given it's struct breakpoint * BINT.
7436 The value we return ends up being the return value from catch_errors.
7437 Unused in this case. */
7438
7439static int
4efb68b1 7440breakpoint_re_set_one (void *bint)
c906108c 7441{
53a5351d
JM
7442 /* get past catch_errs */
7443 struct breakpoint *b = (struct breakpoint *) bint;
c906108c
SS
7444 struct value *mark;
7445 int i;
fe3f5fa8
VP
7446 int not_found = 0;
7447 int *not_found_ptr = &not_found;
7448 struct symtabs_and_lines sals = {};
ed0616c6 7449 struct symtabs_and_lines expanded;
c906108c 7450 char *s;
b5de0fa7 7451 enum enable_state save_enable;
fe3f5fa8
VP
7452 struct gdb_exception e;
7453
c906108c
SS
7454
7455 switch (b->type)
7456 {
7457 case bp_none:
8a3fe4f8 7458 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
53a5351d 7459 b->number);
c906108c
SS
7460 return 0;
7461 case bp_breakpoint:
7462 case bp_hardware_breakpoint:
7463 case bp_catch_load:
7464 case bp_catch_unload:
7465 if (b->addr_string == NULL)
7466 {
7467 /* Anything without a string can't be re-set. */
7468 delete_breakpoint (b);
7469 return 0;
7470 }
c906108c
SS
7471
7472 set_language (b->language);
7473 input_radix = b->input_radix;
7474 s = b->addr_string;
fe3f5fa8 7475 TRY_CATCH (e, RETURN_MASK_ERROR)
c906108c 7476 {
fe3f5fa8
VP
7477 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7478 not_found_ptr);
7479 }
7480 if (e.reason < 0)
7481 {
7482 int not_found_and_ok = 0;
7483 /* For pending breakpoints, it's expected that parsing
7484 will fail until the right shared library is loaded.
7485 User has already told to create pending breakpoints and
7486 don't need extra messages. If breakpoint is in bp_shlib_disabled
7487 state, then user already saw the message about that breakpoint
7488 being disabled, and don't want to see more errors. */
0d381245
VP
7489 if (not_found
7490 && (b->condition_not_parsed
7491 || (b->loc && b->loc->shlib_disabled)
7492 || b->enable_state == bp_disabled))
fe3f5fa8
VP
7493 not_found_and_ok = 1;
7494
7495 if (!not_found_and_ok)
c906108c 7496 {
fe3f5fa8
VP
7497 /* We surely don't want to warn about the same breakpoint
7498 10 times. One solution, implemented here, is disable
7499 the breakpoint on error. Another solution would be to
7500 have separate 'warning emitted' flag. Since this
7501 happens only when a binary has changed, I don't know
7502 which approach is better. */
7503 b->enable_state = bp_disabled;
7504 throw_exception (e);
c906108c 7505 }
fe3f5fa8 7506 }
c906108c 7507
fe3f5fa8
VP
7508 if (not_found)
7509 break;
7510
7511 gdb_assert (sals.nelts == 1);
7512 resolve_sal_pc (&sals.sals[0]);
0d381245 7513 if (b->condition_not_parsed && s && s[0])
fe3f5fa8
VP
7514 {
7515 char *cond_string = 0;
7516 int thread = -1;
7517 find_condition_and_thread (s, sals.sals[0].pc,
7518 &cond_string, &thread);
7519 if (cond_string)
7520 b->cond_string = cond_string;
7521 b->thread = thread;
0d381245 7522 b->condition_not_parsed = 0;
fe3f5fa8 7523 }
ed0616c6
VP
7524 expanded = expand_line_sal_maybe (sals.sals[0]);
7525 update_breakpoint_locations (b, expanded);
0d381245
VP
7526
7527 /* Now that this is re-enabled, check_duplicates
7528 can be used. */
7529 check_duplicates (b);
c906108c 7530
b8c9b27d 7531 xfree (sals.sals);
c906108c
SS
7532 break;
7533
7534 case bp_watchpoint:
7535 case bp_hardware_watchpoint:
7536 case bp_read_watchpoint:
7537 case bp_access_watchpoint:
7538 innermost_block = NULL;
53a5351d
JM
7539 /* The issue arises of what context to evaluate this in. The
7540 same one as when it was set, but what does that mean when
7541 symbols have been re-read? We could save the filename and
7542 functionname, but if the context is more local than that, the
7543 best we could do would be something like how many levels deep
7544 and which index at that particular level, but that's going to
7545 be less stable than filenames or function names. */
7546
c906108c
SS
7547 /* So for now, just use a global context. */
7548 if (b->exp)
a355c7de
AC
7549 {
7550 xfree (b->exp);
7551 /* Avoid re-freeing b->exp if an error during the call to
7552 parse_expression. */
7553 b->exp = NULL;
7554 }
c906108c
SS
7555 b->exp = parse_expression (b->exp_string);
7556 b->exp_valid_block = innermost_block;
7557 mark = value_mark ();
7558 if (b->val)
a355c7de
AC
7559 {
7560 value_free (b->val);
7561 /* Avoid re-freeing b->val if an error during the call to
7562 evaluate_expression. */
7563 b->val = NULL;
7564 }
c906108c
SS
7565 b->val = evaluate_expression (b->exp);
7566 release_value (b->val);
d69fe07e 7567 if (value_lazy (b->val) && breakpoint_enabled (b))
c906108c
SS
7568 value_fetch_lazy (b->val);
7569
7570 if (b->cond_string != NULL)
7571 {
7572 s = b->cond_string;
511a6cd4 7573 if (b->loc->cond)
a355c7de 7574 {
511a6cd4 7575 xfree (b->loc->cond);
a355c7de
AC
7576 /* Avoid re-freeing b->exp if an error during the call
7577 to parse_exp_1. */
511a6cd4 7578 b->loc->cond = NULL;
a355c7de 7579 }
511a6cd4 7580 b->loc->cond = parse_exp_1 (&s, (struct block *) 0, 0);
c906108c 7581 }
468d015d 7582 if (breakpoint_enabled (b))
c906108c
SS
7583 mention (b);
7584 value_free_to_mark (mark);
7585 break;
c5aa993b
JM
7586 case bp_catch_catch:
7587 case bp_catch_throw:
c906108c 7588 break;
c5aa993b
JM
7589 /* We needn't really do anything to reset these, since the mask
7590 that requests them is unaffected by e.g., new libraries being
7591 loaded. */
c906108c
SS
7592 case bp_catch_fork:
7593 case bp_catch_vfork:
7594 case bp_catch_exec:
7595 break;
c5aa993b 7596
c906108c 7597 default:
a3f17187 7598 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
c906108c 7599 /* fall through */
1900040c
MS
7600 /* Delete longjmp and overlay event breakpoints; they will be
7601 reset later by breakpoint_re_set. */
c906108c
SS
7602 case bp_longjmp:
7603 case bp_longjmp_resume:
1900040c 7604 case bp_overlay_event:
c906108c
SS
7605 delete_breakpoint (b);
7606 break;
7607
c5aa993b
JM
7608 /* This breakpoint is special, it's set up when the inferior
7609 starts and we really don't want to touch it. */
c906108c
SS
7610 case bp_shlib_event:
7611
c4093a6a
JM
7612 /* Like bp_shlib_event, this breakpoint type is special.
7613 Once it is set up, we do not want to touch it. */
7614 case bp_thread_event:
7615
c5aa993b
JM
7616 /* Keep temporary breakpoints, which can be encountered when we step
7617 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7618 Otherwise these should have been blown away via the cleanup chain
7619 or by breakpoint_init_inferior when we rerun the executable. */
c906108c
SS
7620 case bp_until:
7621 case bp_finish:
7622 case bp_watchpoint_scope:
7623 case bp_call_dummy:
7624 case bp_step_resume:
7625 break;
7626 }
7627
7628 return 0;
7629}
7630
7631/* Re-set all breakpoints after symbols have been re-loaded. */
7632void
fba45db2 7633breakpoint_re_set (void)
c906108c
SS
7634{
7635 struct breakpoint *b, *temp;
7636 enum language save_language;
7637 int save_input_radix;
c5aa993b 7638
c906108c
SS
7639 save_language = current_language->la_language;
7640 save_input_radix = input_radix;
7641 ALL_BREAKPOINTS_SAFE (b, temp)
c5aa993b 7642 {
53a5351d 7643 /* Format possible error msg */
fe3f5fa8 7644 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
7645 b->number);
7646 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 7647 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 7648 do_cleanups (cleanups);
c5aa993b 7649 }
c906108c
SS
7650 set_language (save_language);
7651 input_radix = save_input_radix;
7652
91104499 7653 if (gdbarch_get_longjmp_target_p (current_gdbarch))
9df628e0
RE
7654 {
7655 create_longjmp_breakpoint ("longjmp");
7656 create_longjmp_breakpoint ("_longjmp");
7657 create_longjmp_breakpoint ("siglongjmp");
7658 create_longjmp_breakpoint ("_siglongjmp");
7659 create_longjmp_breakpoint (NULL);
7660 }
1900040c
MS
7661
7662 create_overlay_event_breakpoint ("_ovly_debug_event");
c906108c
SS
7663}
7664\f
c906108c
SS
7665/* Reset the thread number of this breakpoint:
7666
7667 - If the breakpoint is for all threads, leave it as-is.
39f77062 7668 - Else, reset it to the current thread for inferior_ptid. */
c906108c 7669void
fba45db2 7670breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
7671{
7672 if (b->thread != -1)
7673 {
39f77062
KB
7674 if (in_thread_list (inferior_ptid))
7675 b->thread = pid_to_thread_id (inferior_ptid);
c906108c
SS
7676 }
7677}
7678
03ac34d5
MS
7679/* Set ignore-count of breakpoint number BPTNUM to COUNT.
7680 If from_tty is nonzero, it prints a message to that effect,
7681 which ends with a period (no newline). */
7682
c906108c 7683void
fba45db2 7684set_ignore_count (int bptnum, int count, int from_tty)
c906108c 7685{
52f0bd74 7686 struct breakpoint *b;
c906108c
SS
7687
7688 if (count < 0)
7689 count = 0;
7690
7691 ALL_BREAKPOINTS (b)
7692 if (b->number == bptnum)
c5aa993b
JM
7693 {
7694 b->ignore_count = count;
221ea385
KS
7695 if (from_tty)
7696 {
7697 if (count == 0)
a3f17187 7698 printf_filtered (_("Will stop next time breakpoint %d is reached."),
221ea385
KS
7699 bptnum);
7700 else if (count == 1)
a3f17187 7701 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
7702 bptnum);
7703 else
a3f17187 7704 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
221ea385
KS
7705 count, bptnum);
7706 }
c5aa993b 7707 breakpoints_changed ();
221ea385 7708 breakpoint_modify_event (b->number);
c5aa993b
JM
7709 return;
7710 }
c906108c 7711
8a3fe4f8 7712 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
7713}
7714
7715/* Clear the ignore counts of all breakpoints. */
7716void
fba45db2 7717breakpoint_clear_ignore_counts (void)
c906108c
SS
7718{
7719 struct breakpoint *b;
7720
7721 ALL_BREAKPOINTS (b)
7722 b->ignore_count = 0;
7723}
7724
7725/* Command to set ignore-count of breakpoint N to COUNT. */
7726
7727static void
fba45db2 7728ignore_command (char *args, int from_tty)
c906108c
SS
7729{
7730 char *p = args;
52f0bd74 7731 int num;
c906108c
SS
7732
7733 if (p == 0)
e2e0b3e5 7734 error_no_arg (_("a breakpoint number"));
c5aa993b 7735
c906108c 7736 num = get_number (&p);
5c44784c 7737 if (num == 0)
8a3fe4f8 7738 error (_("bad breakpoint number: '%s'"), args);
c906108c 7739 if (*p == 0)
8a3fe4f8 7740 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
7741
7742 set_ignore_count (num,
7743 longest_to_int (value_as_long (parse_and_eval (p))),
7744 from_tty);
221ea385
KS
7745 if (from_tty)
7746 printf_filtered ("\n");
c906108c
SS
7747}
7748\f
7749/* Call FUNCTION on each of the breakpoints
7750 whose numbers are given in ARGS. */
7751
7752static void
831662b3 7753map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
c906108c 7754{
52f0bd74 7755 char *p = args;
c906108c 7756 char *p1;
52f0bd74
AC
7757 int num;
7758 struct breakpoint *b, *tmp;
11cf8741 7759 int match;
c906108c
SS
7760
7761 if (p == 0)
e2e0b3e5 7762 error_no_arg (_("one or more breakpoint numbers"));
c906108c
SS
7763
7764 while (*p)
7765 {
11cf8741 7766 match = 0;
c906108c 7767 p1 = p;
c5aa993b 7768
5c44784c
JM
7769 num = get_number_or_range (&p1);
7770 if (num == 0)
c5aa993b 7771 {
8a3fe4f8 7772 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
7773 }
7774 else
7775 {
7776 ALL_BREAKPOINTS_SAFE (b, tmp)
7777 if (b->number == num)
7778 {
7779 struct breakpoint *related_breakpoint = b->related_breakpoint;
11cf8741 7780 match = 1;
5c44784c
JM
7781 function (b);
7782 if (related_breakpoint)
7783 function (related_breakpoint);
11cf8741 7784 break;
5c44784c 7785 }
11cf8741 7786 if (match == 0)
a3f17187 7787 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 7788 }
c906108c
SS
7789 p = p1;
7790 }
7791}
7792
0d381245
VP
7793static struct bp_location *
7794find_location_by_number (char *number)
7795{
7796 char *dot = strchr (number, '.');
7797 char *p1;
7798 int bp_num;
7799 int loc_num;
7800 struct breakpoint *b;
7801 struct bp_location *loc;
7802
7803 *dot = '\0';
7804
7805 p1 = number;
7806 bp_num = get_number_or_range (&p1);
7807 if (bp_num == 0)
7808 error (_("Bad breakpoint number '%s'"), number);
7809
7810 ALL_BREAKPOINTS (b)
7811 if (b->number == bp_num)
7812 {
7813 break;
7814 }
7815
7816 if (!b || b->number != bp_num)
7817 error (_("Bad breakpoint number '%s'"), number);
7818
7819 p1 = dot+1;
7820 loc_num = get_number_or_range (&p1);
7821 if (loc_num == 0)
7822 error (_("Bad breakpoint location number '%s'"), number);
7823
7824 --loc_num;
7825 loc = b->loc;
7826 for (;loc_num && loc; --loc_num, loc = loc->next)
7827 ;
7828 if (!loc)
7829 error (_("Bad breakpoint location number '%s'"), dot+1);
7830
7831 return loc;
7832}
7833
7834
1900040c
MS
7835/* Set ignore-count of breakpoint number BPTNUM to COUNT.
7836 If from_tty is nonzero, it prints a message to that effect,
7837 which ends with a period (no newline). */
7838
c906108c 7839void
fba45db2 7840disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
7841{
7842 /* Never disable a watchpoint scope breakpoint; we want to
7843 hit them when we leave scope so we can delete both the
7844 watchpoint and its scope breakpoint at that time. */
7845 if (bpt->type == bp_watchpoint_scope)
7846 return;
7847
c2c6d25f 7848 /* You can't disable permanent breakpoints. */
b5de0fa7 7849 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
7850 return;
7851
b5de0fa7 7852 bpt->enable_state = bp_disabled;
c906108c 7853
9f60f21b 7854 check_duplicates (bpt);
c906108c 7855
9a4105ab
AC
7856 if (deprecated_modify_breakpoint_hook)
7857 deprecated_modify_breakpoint_hook (bpt);
104c1213 7858 breakpoint_modify_event (bpt->number);
c906108c
SS
7859}
7860
c906108c 7861static void
fba45db2 7862disable_command (char *args, int from_tty)
c906108c 7863{
52f0bd74 7864 struct breakpoint *bpt;
c906108c
SS
7865 if (args == 0)
7866 ALL_BREAKPOINTS (bpt)
7867 switch (bpt->type)
c5aa993b
JM
7868 {
7869 case bp_none:
8a3fe4f8 7870 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
53a5351d 7871 bpt->number);
c5aa993b
JM
7872 continue;
7873 case bp_breakpoint:
7874 case bp_catch_load:
7875 case bp_catch_unload:
7876 case bp_catch_fork:
7877 case bp_catch_vfork:
7878 case bp_catch_exec:
7879 case bp_catch_catch:
7880 case bp_catch_throw:
7881 case bp_hardware_breakpoint:
7882 case bp_watchpoint:
7883 case bp_hardware_watchpoint:
7884 case bp_read_watchpoint:
7885 case bp_access_watchpoint:
7886 disable_breakpoint (bpt);
7887 default:
7888 continue;
7889 }
0d381245
VP
7890 else if (strchr (args, '.'))
7891 {
7892 struct bp_location *loc = find_location_by_number (args);
7893 if (loc)
7894 loc->enabled = 0;
7895 check_duplicates (loc->owner);
7896 }
c906108c
SS
7897 else
7898 map_breakpoint_numbers (args, disable_breakpoint);
7899}
7900
7901static void
fba45db2 7902do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
c906108c 7903{
c906108c
SS
7904 int target_resources_ok, other_type_used;
7905 struct value *mark;
7906
7907 if (bpt->type == bp_hardware_breakpoint)
7908 {
7909 int i;
c5aa993b 7910 i = hw_breakpoint_used_count ();
53a5351d
JM
7911 target_resources_ok =
7912 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint,
7913 i + 1, 0);
c906108c 7914 if (target_resources_ok == 0)
8a3fe4f8 7915 error (_("No hardware breakpoint support in the target."));
c906108c 7916 else if (target_resources_ok < 0)
8a3fe4f8 7917 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
7918 }
7919
fe3f5fa8
VP
7920 if (bpt->type == bp_watchpoint ||
7921 bpt->type == bp_hardware_watchpoint ||
7922 bpt->type == bp_read_watchpoint ||
7923 bpt->type == bp_access_watchpoint)
c906108c 7924 {
fe3f5fa8
VP
7925 struct frame_id saved_frame_id;
7926
7927 saved_frame_id = get_frame_id (get_selected_frame (NULL));
7928 if (bpt->exp_valid_block != NULL)
c906108c 7929 {
fe3f5fa8
VP
7930 struct frame_info *fr =
7931 fr = frame_find_by_id (bpt->watchpoint_frame);
7932 if (fr == NULL)
c906108c 7933 {
fe3f5fa8
VP
7934 printf_filtered (_("\
7935Cannot enable watchpoint %d because the block in which its expression\n\
7936is valid is not currently in scope.\n"), bpt->number);
c906108c
SS
7937 return;
7938 }
fe3f5fa8 7939 select_frame (fr);
c906108c 7940 }
0101ce28 7941
b4c291bb
KH
7942 if (bpt->val)
7943 value_free (bpt->val);
fe3f5fa8
VP
7944 mark = value_mark ();
7945 bpt->val = evaluate_expression (bpt->exp);
7946 release_value (bpt->val);
7947 if (value_lazy (bpt->val))
7948 value_fetch_lazy (bpt->val);
7949
7950 if (bpt->type == bp_hardware_watchpoint ||
7951 bpt->type == bp_read_watchpoint ||
c5aa993b
JM
7952 bpt->type == bp_access_watchpoint)
7953 {
fe3f5fa8
VP
7954 int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
7955 int mem_cnt = can_use_hardware_watchpoint (bpt->val);
0101ce28 7956
fe3f5fa8
VP
7957 /* Hack around 'unused var' error for some targets here */
7958 (void) mem_cnt, (void) i;
7959 target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
7960 bpt->type, i + mem_cnt, other_type_used);
7961 /* we can consider of type is bp_hardware_watchpoint, convert to
7962 bp_watchpoint in the following condition */
7963 if (target_resources_ok < 0)
c5aa993b 7964 {
fe3f5fa8 7965 printf_filtered (_("\
c906108c 7966Cannot enable watchpoint %d because target watch resources\n\
a3f17187 7967have been allocated for other watchpoints.\n"), bpt->number);
fe3f5fa8
VP
7968 value_free_to_mark (mark);
7969 return;
c5aa993b
JM
7970 }
7971 }
fe3f5fa8
VP
7972
7973 select_frame (frame_find_by_id (saved_frame_id));
7974 value_free_to_mark (mark);
c906108c 7975 }
0101ce28 7976
b4c291bb
KH
7977 if (bpt->enable_state != bp_permanent)
7978 bpt->enable_state = bp_enabled;
7979 bpt->disposition = disposition;
7980 check_duplicates (bpt);
7981 breakpoints_changed ();
7982
9a4105ab
AC
7983 if (deprecated_modify_breakpoint_hook)
7984 deprecated_modify_breakpoint_hook (bpt);
104c1213 7985 breakpoint_modify_event (bpt->number);
c906108c
SS
7986}
7987
fe3f5fa8 7988
c906108c 7989void
fba45db2 7990enable_breakpoint (struct breakpoint *bpt)
c906108c
SS
7991{
7992 do_enable_breakpoint (bpt, bpt->disposition);
7993}
7994
7995/* The enable command enables the specified breakpoints (or all defined
7996 breakpoints) so they once again become (or continue to be) effective
1272ad14 7997 in stopping the inferior. */
c906108c 7998
c906108c 7999static void
fba45db2 8000enable_command (char *args, int from_tty)
c906108c 8001{
52f0bd74 8002 struct breakpoint *bpt;
c906108c
SS
8003 if (args == 0)
8004 ALL_BREAKPOINTS (bpt)
8005 switch (bpt->type)
c5aa993b
JM
8006 {
8007 case bp_none:
8a3fe4f8 8008 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
53a5351d 8009 bpt->number);
c5aa993b
JM
8010 continue;
8011 case bp_breakpoint:
8012 case bp_catch_load:
8013 case bp_catch_unload:
8014 case bp_catch_fork:
8015 case bp_catch_vfork:
8016 case bp_catch_exec:
8017 case bp_catch_catch:
8018 case bp_catch_throw:
8019 case bp_hardware_breakpoint:
8020 case bp_watchpoint:
8021 case bp_hardware_watchpoint:
8022 case bp_read_watchpoint:
8023 case bp_access_watchpoint:
8024 enable_breakpoint (bpt);
8025 default:
8026 continue;
8027 }
0d381245
VP
8028 else if (strchr (args, '.'))
8029 {
8030 struct bp_location *loc = find_location_by_number (args);
8031 if (loc)
8032 loc->enabled = 1;
8033 check_duplicates (loc->owner);
8034 }
c906108c
SS
8035 else
8036 map_breakpoint_numbers (args, enable_breakpoint);
8037}
8038
8039static void
fba45db2 8040enable_once_breakpoint (struct breakpoint *bpt)
c906108c 8041{
b5de0fa7 8042 do_enable_breakpoint (bpt, disp_disable);
c906108c
SS
8043}
8044
c906108c 8045static void
fba45db2 8046enable_once_command (char *args, int from_tty)
c906108c
SS
8047{
8048 map_breakpoint_numbers (args, enable_once_breakpoint);
8049}
8050
8051static void
fba45db2 8052enable_delete_breakpoint (struct breakpoint *bpt)
c906108c 8053{
b5de0fa7 8054 do_enable_breakpoint (bpt, disp_del);
c906108c
SS
8055}
8056
c906108c 8057static void
fba45db2 8058enable_delete_command (char *args, int from_tty)
c906108c
SS
8059{
8060 map_breakpoint_numbers (args, enable_delete_breakpoint);
8061}
8062\f
fa8d40ab
JJ
8063static void
8064set_breakpoint_cmd (char *args, int from_tty)
8065{
8066}
8067
8068static void
8069show_breakpoint_cmd (char *args, int from_tty)
8070{
8071}
8072
c906108c
SS
8073/* Use default_breakpoint_'s, or nothing if they aren't valid. */
8074
8075struct symtabs_and_lines
fba45db2 8076decode_line_spec_1 (char *string, int funfirstline)
c906108c
SS
8077{
8078 struct symtabs_and_lines sals;
8079 if (string == 0)
8a3fe4f8 8080 error (_("Empty line specification."));
c906108c
SS
8081 if (default_breakpoint_valid)
8082 sals = decode_line_1 (&string, funfirstline,
53a5351d
JM
8083 default_breakpoint_symtab,
8084 default_breakpoint_line,
68219205 8085 (char ***) NULL, NULL);
c906108c
SS
8086 else
8087 sals = decode_line_1 (&string, funfirstline,
68219205 8088 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
c906108c 8089 if (*string)
8a3fe4f8 8090 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
8091 return sals;
8092}
8181d85f
DJ
8093
8094/* Create and insert a raw software breakpoint at PC. Return an
8095 identifier, which should be used to remove the breakpoint later.
8096 In general, places which call this should be using something on the
8097 breakpoint chain instead; this function should be eliminated
8098 someday. */
8099
8100void *
8101deprecated_insert_raw_breakpoint (CORE_ADDR pc)
8102{
8103 struct bp_target_info *bp_tgt;
8104
8105 bp_tgt = xmalloc (sizeof (struct bp_target_info));
8106 memset (bp_tgt, 0, sizeof (struct bp_target_info));
8107
8108 bp_tgt->placed_address = pc;
8109 if (target_insert_breakpoint (bp_tgt) != 0)
8110 {
8111 /* Could not insert the breakpoint. */
8112 xfree (bp_tgt);
8113 return NULL;
8114 }
8115
8116 return bp_tgt;
8117}
8118
8119/* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
8120
8121int
8122deprecated_remove_raw_breakpoint (void *bp)
8123{
8124 struct bp_target_info *bp_tgt = bp;
8125 int ret;
8126
8127 ret = target_remove_breakpoint (bp_tgt);
8128 xfree (bp_tgt);
8129
8130 return ret;
8131}
8132
8133/* One (or perhaps two) breakpoints used for software single stepping. */
8134
8135static void *single_step_breakpoints[2];
8136
8137/* Create and insert a breakpoint for software single step. */
8138
8139void
8140insert_single_step_breakpoint (CORE_ADDR next_pc)
8141{
8142 void **bpt_p;
8143
8144 if (single_step_breakpoints[0] == NULL)
8145 bpt_p = &single_step_breakpoints[0];
8146 else
8147 {
8148 gdb_assert (single_step_breakpoints[1] == NULL);
8149 bpt_p = &single_step_breakpoints[1];
8150 }
8151
8152 /* NOTE drow/2006-04-11: A future improvement to this function would be
8153 to only create the breakpoints once, and actually put them on the
8154 breakpoint chain. That would let us use set_raw_breakpoint. We could
8155 adjust the addresses each time they were needed. Doing this requires
8156 corresponding changes elsewhere where single step breakpoints are
8157 handled, however. So, for now, we use this. */
8158
8159 *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
8160 if (*bpt_p == NULL)
1893a4c0 8161 error (_("Could not insert single-step breakpoint at 0x%s"),
8181d85f
DJ
8162 paddr_nz (next_pc));
8163}
8164
8165/* Remove and delete any breakpoints used for software single step. */
8166
8167void
8168remove_single_step_breakpoints (void)
8169{
8170 gdb_assert (single_step_breakpoints[0] != NULL);
8171
8172 /* See insert_single_step_breakpoint for more about this deprecated
8173 call. */
8174 deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
8175 single_step_breakpoints[0] = NULL;
8176
8177 if (single_step_breakpoints[1] != NULL)
8178 {
8179 deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
8180 single_step_breakpoints[1] = NULL;
8181 }
8182}
8183
1aafd4da
UW
8184/* Check whether a software single-step breakpoint is inserted at PC. */
8185
8186static int
8187single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
8188{
8189 int i;
8190
8191 for (i = 0; i < 2; i++)
8192 {
8193 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
8194 if (bp_tgt && bp_tgt->placed_address == pc)
8195 return 1;
8196 }
8197
8198 return 0;
8199}
8200
c906108c 8201\f
31e2b00f
AS
8202/* This help string is used for the break, hbreak, tbreak and thbreak commands.
8203 It is defined as a macro to prevent duplication.
8204 COMMAND should be a string constant containing the name of the command. */
8205#define BREAK_ARGS_HELP(command) \
8206command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
8207LOCATION may be a line number, function name, or \"*\" and an address.\n\
8208If a line number is specified, break at start of code for that line.\n\
8209If a function is specified, break at start of code for that function.\n\
8210If an address is specified, break at that exact address.\n\
8211With no LOCATION, uses current execution address of selected stack frame.\n\
8212This is useful for breaking on return to a stack frame.\n\
8213\n\
8214THREADNUM is the number from \"info threads\".\n\
8215CONDITION is a boolean expression.\n\
8216\n\
8217Multiple breakpoints at one place are permitted, and useful if conditional.\n\
8218\n\
8219Do \"help breakpoints\" for info on other commands dealing with breakpoints."
8220
c906108c 8221void
fba45db2 8222_initialize_breakpoint (void)
c906108c 8223{
fa8d40ab
JJ
8224 static struct cmd_list_element *breakpoint_set_cmdlist;
8225 static struct cmd_list_element *breakpoint_show_cmdlist;
c906108c
SS
8226 struct cmd_list_element *c;
8227
84acb35a 8228 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
84acb35a 8229
c906108c
SS
8230 breakpoint_chain = 0;
8231 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
8232 before a breakpoint is set. */
8233 breakpoint_count = 0;
8234
1bedd215
AC
8235 add_com ("ignore", class_breakpoint, ignore_command, _("\
8236Set ignore-count of breakpoint number N to COUNT.\n\
8237Usage is `ignore N COUNT'."));
c906108c 8238 if (xdb_commands)
c5aa993b 8239 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 8240
1bedd215
AC
8241 add_com ("commands", class_breakpoint, commands_command, _("\
8242Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
8243Give breakpoint number as argument after \"commands\".\n\
8244With no argument, the targeted breakpoint is the last one set.\n\
8245The commands themselves follow starting on the next line.\n\
8246Type a line containing \"end\" to indicate the end of them.\n\
8247Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 8248then no output is printed when it is hit, except what the commands print."));
c906108c 8249
1bedd215
AC
8250 add_com ("condition", class_breakpoint, condition_command, _("\
8251Specify breakpoint number N to break only if COND is true.\n\
c906108c 8252Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 8253expression to be evaluated whenever breakpoint N is reached."));
c906108c 8254
1bedd215 8255 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 8256Set a temporary breakpoint.\n\
c906108c
SS
8257Like \"break\" except the breakpoint is only temporary,\n\
8258so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
8259by using \"enable delete\" on the breakpoint number.\n\
8260\n"
8261BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 8262 set_cmd_completer (c, location_completer);
c94fdfd0 8263
1bedd215 8264 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
31e2b00f 8265Set a hardware assisted breakpoint.\n\
c906108c 8266Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
8267some target hardware may not have this support.\n\
8268\n"
8269BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 8270 set_cmd_completer (c, location_completer);
c906108c 8271
1bedd215 8272 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 8273Set a temporary hardware assisted breakpoint.\n\
c906108c 8274Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
8275so it will be deleted when hit.\n\
8276\n"
8277BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 8278 set_cmd_completer (c, location_completer);
c906108c 8279
1bedd215
AC
8280 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
8281Enable some breakpoints.\n\
c906108c
SS
8282Give breakpoint numbers (separated by spaces) as arguments.\n\
8283With no subcommand, breakpoints are enabled until you command otherwise.\n\
8284This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8285With a subcommand you can enable temporarily."),
c906108c
SS
8286 &enablelist, "enable ", 1, &cmdlist);
8287 if (xdb_commands)
1bedd215
AC
8288 add_com ("ab", class_breakpoint, enable_command, _("\
8289Enable some breakpoints.\n\
c906108c
SS
8290Give breakpoint numbers (separated by spaces) as arguments.\n\
8291With no subcommand, breakpoints are enabled until you command otherwise.\n\
8292This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8293With a subcommand you can enable temporarily."));
c906108c
SS
8294
8295 add_com_alias ("en", "enable", class_breakpoint, 1);
8296
1bedd215
AC
8297 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8298Enable some breakpoints.\n\
c906108c
SS
8299Give breakpoint numbers (separated by spaces) as arguments.\n\
8300This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 8301May be abbreviated to simply \"enable\".\n"),
c5aa993b 8302 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 8303
1a966eab
AC
8304 add_cmd ("once", no_class, enable_once_command, _("\
8305Enable breakpoints for one hit. Give breakpoint numbers.\n\
8306If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8307 &enablebreaklist);
8308
1a966eab
AC
8309 add_cmd ("delete", no_class, enable_delete_command, _("\
8310Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8311If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8312 &enablebreaklist);
8313
1a966eab
AC
8314 add_cmd ("delete", no_class, enable_delete_command, _("\
8315Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8316If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
8317 &enablelist);
8318
1a966eab
AC
8319 add_cmd ("once", no_class, enable_once_command, _("\
8320Enable breakpoints for one hit. Give breakpoint numbers.\n\
8321If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
8322 &enablelist);
8323
1bedd215
AC
8324 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8325Disable some breakpoints.\n\
c906108c
SS
8326Arguments are breakpoint numbers with spaces in between.\n\
8327To disable all breakpoints, give no argument.\n\
1bedd215 8328A disabled breakpoint is not forgotten, but has no effect until reenabled."),
c906108c
SS
8329 &disablelist, "disable ", 1, &cmdlist);
8330 add_com_alias ("dis", "disable", class_breakpoint, 1);
8331 add_com_alias ("disa", "disable", class_breakpoint, 1);
8332 if (xdb_commands)
1bedd215
AC
8333 add_com ("sb", class_breakpoint, disable_command, _("\
8334Disable some breakpoints.\n\
c906108c
SS
8335Arguments are breakpoint numbers with spaces in between.\n\
8336To disable all breakpoints, give no argument.\n\
1bedd215 8337A disabled breakpoint is not forgotten, but has no effect until reenabled."));
c906108c 8338
1a966eab
AC
8339 add_cmd ("breakpoints", class_alias, disable_command, _("\
8340Disable some breakpoints.\n\
c906108c
SS
8341Arguments are breakpoint numbers with spaces in between.\n\
8342To disable all breakpoints, give no argument.\n\
8343A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1a966eab 8344This command may be abbreviated \"disable\"."),
c906108c
SS
8345 &disablelist);
8346
1bedd215
AC
8347 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8348Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8349Arguments are breakpoint numbers with spaces in between.\n\
8350To delete all breakpoints, give no argument.\n\
8351\n\
8352Also a prefix command for deletion of other GDB objects.\n\
1bedd215 8353The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
8354 &deletelist, "delete ", 1, &cmdlist);
8355 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 8356 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 8357 if (xdb_commands)
1bedd215
AC
8358 add_com ("db", class_breakpoint, delete_command, _("\
8359Delete some breakpoints.\n\
c906108c 8360Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 8361To delete all breakpoints, give no argument.\n"));
c906108c 8362
1a966eab
AC
8363 add_cmd ("breakpoints", class_alias, delete_command, _("\
8364Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
8365Arguments are breakpoint numbers with spaces in between.\n\
8366To delete all breakpoints, give no argument.\n\
1a966eab 8367This command may be abbreviated \"delete\"."),
c906108c
SS
8368 &deletelist);
8369
1bedd215
AC
8370 add_com ("clear", class_breakpoint, clear_command, _("\
8371Clear breakpoint at specified line or function.\n\
c906108c
SS
8372Argument may be line number, function name, or \"*\" and an address.\n\
8373If line number is specified, all breakpoints in that line are cleared.\n\
8374If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
8375If an address is specified, breakpoints at that address are cleared.\n\
8376\n\
8377With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
8378is executing in.\n\
8379\n\
1bedd215 8380See also the \"delete\" command which clears breakpoints by number."));
c906108c 8381
1bedd215 8382 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
8383Set breakpoint at specified line or function.\n"
8384BREAK_ARGS_HELP ("break")));
5ba2abeb 8385 set_cmd_completer (c, location_completer);
c94fdfd0 8386
c906108c
SS
8387 add_com_alias ("b", "break", class_run, 1);
8388 add_com_alias ("br", "break", class_run, 1);
8389 add_com_alias ("bre", "break", class_run, 1);
8390 add_com_alias ("brea", "break", class_run, 1);
8391
502fd408 8392 if (xdb_commands)
c906108c
SS
8393 {
8394 add_com_alias ("ba", "break", class_breakpoint, 1);
8395 add_com_alias ("bu", "ubreak", class_breakpoint, 1);
c906108c
SS
8396 }
8397
8398 if (dbx_commands)
8399 {
1bedd215
AC
8400 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8401Break in function/address or break at a line in the current file."),
c5aa993b
JM
8402 &stoplist, "stop ", 1, &cmdlist);
8403 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 8404 _("Break in function or address."), &stoplist);
c5aa993b 8405 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 8406 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
8407 add_com ("status", class_info, breakpoints_info, _("\
8408Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8409The \"Type\" column indicates one of:\n\
8410\tbreakpoint - normal breakpoint\n\
8411\twatchpoint - watchpoint\n\
8412The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8413the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8414breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8415address and file/line number respectively.\n\
8416\n\
8417Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8418are set to the address of the last breakpoint listed unless the command\n\
8419is prefixed with \"server \".\n\n\
c906108c 8420Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8421breakpoint set."));
c906108c
SS
8422 }
8423
1bedd215
AC
8424 add_info ("breakpoints", breakpoints_info, _("\
8425Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8426The \"Type\" column indicates one of:\n\
8427\tbreakpoint - normal breakpoint\n\
8428\twatchpoint - watchpoint\n\
8429The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8430the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8431breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8432address and file/line number respectively.\n\
8433\n\
8434Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8435are set to the address of the last breakpoint listed unless the command\n\
8436is prefixed with \"server \".\n\n\
c906108c 8437Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8438breakpoint set."));
c906108c
SS
8439
8440 if (xdb_commands)
1bedd215
AC
8441 add_com ("lb", class_breakpoint, breakpoints_info, _("\
8442Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8443The \"Type\" column indicates one of:\n\
8444\tbreakpoint - normal breakpoint\n\
8445\twatchpoint - watchpoint\n\
8446The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8447the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8448breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
8449address and file/line number respectively.\n\
8450\n\
8451Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8452are set to the address of the last breakpoint listed unless the command\n\
8453is prefixed with \"server \".\n\n\
c906108c 8454Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 8455breakpoint set."));
c906108c 8456
1a966eab
AC
8457 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
8458Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
8459The \"Type\" column indicates one of:\n\
8460\tbreakpoint - normal breakpoint\n\
8461\twatchpoint - watchpoint\n\
8462\tlongjmp - internal breakpoint used to step through longjmp()\n\
8463\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8464\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
8465\tfinish - internal breakpoint used by the \"finish\" command\n\
8466The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
8467the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8468breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
8469address and file/line number respectively.\n\
8470\n\
8471Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
8472are set to the address of the last breakpoint listed unless the command\n\
8473is prefixed with \"server \".\n\n\
c906108c 8474Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 8475breakpoint set."),
c906108c
SS
8476 &maintenanceinfolist);
8477
1bedd215
AC
8478 add_com ("catch", class_breakpoint, catch_command, _("\
8479Set catchpoints to catch events.\n\
c906108c
SS
8480Raised signals may be caught:\n\
8481\tcatch signal - all signals\n\
8482\tcatch signal <signame> - a particular signal\n\
8483Raised exceptions may be caught:\n\
8484\tcatch throw - all exceptions, when thrown\n\
8485\tcatch throw <exceptname> - a particular exception, when thrown\n\
8486\tcatch catch - all exceptions, when caught\n\
8487\tcatch catch <exceptname> - a particular exception, when caught\n\
8488Thread or process events may be caught:\n\
8489\tcatch thread_start - any threads, just after creation\n\
8490\tcatch thread_exit - any threads, just before expiration\n\
8491\tcatch thread_join - any threads, just after joins\n\
8492Process events may be caught:\n\
8493\tcatch start - any processes, just after creation\n\
8494\tcatch exit - any processes, just before expiration\n\
8495\tcatch fork - calls to fork()\n\
8496\tcatch vfork - calls to vfork()\n\
8497\tcatch exec - calls to exec()\n\
8498Dynamically-linked library events may be caught:\n\
8499\tcatch load - loads of any library\n\
8500\tcatch load <libname> - loads of a particular library\n\
8501\tcatch unload - unloads of any library\n\
8502\tcatch unload <libname> - unloads of a particular library\n\
8503The act of your program's execution stopping may also be caught:\n\
8504\tcatch stop\n\n\
8505C++ exceptions may be caught:\n\
8506\tcatch throw - all exceptions, when thrown\n\
8507\tcatch catch - all exceptions, when caught\n\
f7f9143b
JB
8508Ada exceptions may be caught:\n\
8509\tcatch exception - all exceptions, when raised\n\
8510\tcatch exception <name> - a particular exception, when raised\n\
8511\tcatch exception unhandled - all unhandled exceptions, when raised\n\
8512\tcatch assert - all failed assertions, when raised\n\
c906108c
SS
8513\n\
8514Do \"help set follow-fork-mode\" for info on debugging your program\n\
8515after a fork or vfork is caught.\n\n\
1bedd215 8516Do \"help breakpoints\" for info on other commands dealing with breakpoints."));
c5aa993b 8517
1bedd215
AC
8518 add_com ("tcatch", class_breakpoint, tcatch_command, _("\
8519Set temporary catchpoints to catch events.\n\
c906108c
SS
8520Args like \"catch\" command.\n\
8521Like \"catch\" except the catchpoint is only temporary,\n\
8522so it will be deleted when hit. Equivalent to \"catch\" followed\n\
1bedd215 8523by using \"enable delete\" on the catchpoint number."));
c5aa993b 8524
1bedd215
AC
8525 c = add_com ("watch", class_breakpoint, watch_command, _("\
8526Set a watchpoint for an expression.\n\
c906108c 8527A watchpoint stops execution of your program whenever the value of\n\
1bedd215 8528an expression changes."));
5ba2abeb 8529 set_cmd_completer (c, location_completer);
c906108c 8530
1bedd215
AC
8531 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
8532Set a read watchpoint for an expression.\n\
c906108c 8533A watchpoint stops execution of your program whenever the value of\n\
1bedd215 8534an expression is read."));
5ba2abeb 8535 set_cmd_completer (c, location_completer);
c906108c 8536
1bedd215
AC
8537 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
8538Set a watchpoint for an expression.\n\
c906108c 8539A watchpoint stops execution of your program whenever the value of\n\
1bedd215 8540an expression is either read or written."));
5ba2abeb 8541 set_cmd_completer (c, location_completer);
c906108c
SS
8542
8543 add_info ("watchpoints", breakpoints_info,
1bedd215 8544 _("Synonym for ``info breakpoints''."));
c906108c
SS
8545
8546
920d2a44
AC
8547 /* XXX: cagney/2005-02-23: This should be a boolean, and should
8548 respond to changes - contrary to the description. */
85c07804
AC
8549 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
8550 &can_use_hw_watchpoints, _("\
8551Set debugger's willingness to use watchpoint hardware."), _("\
8552Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
8553If zero, gdb will not use hardware for new watchpoints, even if\n\
8554such is available. (However, any hardware watchpoints that were\n\
8555created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
8556hardware.)"),
8557 NULL,
920d2a44 8558 show_can_use_hw_watchpoints,
85c07804 8559 &setlist, &showlist);
c906108c
SS
8560
8561 can_use_hw_watchpoints = 1;
fa8d40ab 8562
1bedd215 8563 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
8564Breakpoint specific settings\n\
8565Configure various breakpoint-specific variables such as\n\
1bedd215 8566pending breakpoint behavior"),
fa8d40ab
JJ
8567 &breakpoint_set_cmdlist, "set breakpoint ",
8568 0/*allow-unknown*/, &setlist);
1bedd215 8569 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
8570Breakpoint specific settings\n\
8571Configure various breakpoint-specific variables such as\n\
1bedd215 8572pending breakpoint behavior"),
fa8d40ab
JJ
8573 &breakpoint_show_cmdlist, "show breakpoint ",
8574 0/*allow-unknown*/, &showlist);
8575
7915a72c
AC
8576 add_setshow_auto_boolean_cmd ("pending", no_class,
8577 &pending_break_support, _("\
8578Set debugger's behavior regarding pending breakpoints."), _("\
8579Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
8580If on, an unrecognized breakpoint location will cause gdb to create a\n\
8581pending breakpoint. If off, an unrecognized breakpoint location results in\n\
8582an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 8583user-query to see if a pending breakpoint should be created."),
2c5b56ce 8584 NULL,
920d2a44 8585 show_pending_break_support,
6e1d7d6c
AC
8586 &breakpoint_set_cmdlist,
8587 &breakpoint_show_cmdlist);
fa8d40ab
JJ
8588
8589 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
8590
8591 add_setshow_boolean_cmd ("auto-hw", no_class,
8592 &automatic_hardware_breakpoints, _("\
8593Set automatic usage of hardware breakpoints."), _("\
8594Show automatic usage of hardware breakpoints."), _("\
8595If set, the debugger will automatically use hardware breakpoints for\n\
8596breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
8597a warning will be emitted for such breakpoints."),
8598 NULL,
8599 show_automatic_hardware_breakpoints,
8600 &breakpoint_set_cmdlist,
8601 &breakpoint_show_cmdlist);
8602
8603 automatic_hardware_breakpoints = 1;
c906108c 8604}
This page took 1.092976 seconds and 4 git commands to generate.