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