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