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