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