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