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