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