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