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