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