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