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