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