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