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