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