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