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