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