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