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