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