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