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