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