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