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