Use multiple locations for hardware watchpoints.
[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 int 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 int
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 exception_print (gdb_stderr, e);
5327 return e.reason;
5328 case RETURN_ERROR:
5329 switch (e.error)
5330 {
5331 case NOT_FOUND_ERROR:
5332
5333 exception_print (gdb_stderr, e);
5334
5335 /* If pending breakpoint support is turned off, throw
5336 error. */
5337
5338 if (pending_break_support == AUTO_BOOLEAN_FALSE)
5339 deprecated_throw_reason (RETURN_ERROR);
5340
5341 /* If pending breakpoint support is auto query and the user
5342 selects no, then simply return the error code. */
5343 if (pending_break_support == AUTO_BOOLEAN_AUTO &&
5344 !nquery ("Make breakpoint pending on future shared library load? "))
5345 return e.reason;
5346
5347 /* At this point, either the user was queried about setting
5348 a pending breakpoint and selected yes, or pending
5349 breakpoint behavior is on and thus a pending breakpoint
5350 is defaulted on behalf of the user. */
5351 copy_arg = xstrdup (addr_start);
5352 addr_string = &copy_arg;
5353 sals.nelts = 1;
5354 sals.sals = &pending_sal;
5355 pending_sal.pc = 0;
5356 pending = 1;
5357 break;
5358 default:
5359 exception_print (gdb_stderr, e);
5360 return e.reason;
5361 }
5362 default:
5363 if (!sals.nelts)
5364 return GDB_RC_FAIL;
5365 }
5366
5367 /* Create a chain of things that always need to be cleaned up. */
5368 old_chain = make_cleanup (null_cleanup, 0);
5369
5370 if (!pending)
5371 {
5372 /* Make sure that all storage allocated to SALS gets freed. */
5373 make_cleanup (xfree, sals.sals);
5374
5375 /* Cleanup the addr_string array but not its contents. */
5376 make_cleanup (xfree, addr_string);
5377 }
5378
5379 /* ----------------------------- SNIP -----------------------------
5380 Anything added to the cleanup chain beyond this point is assumed
5381 to be part of a breakpoint. If the breakpoint create succeeds
5382 then the memory is not reclaimed. */
5383 breakpoint_chain = make_cleanup (null_cleanup, 0);
5384
5385 /* Mark the contents of the addr_string for cleanup. These go on
5386 the breakpoint_chain and only occure if the breakpoint create
5387 fails. */
5388 for (i = 0; i < sals.nelts; i++)
5389 {
5390 if (addr_string[i] != NULL)
5391 make_cleanup (xfree, addr_string[i]);
5392 }
5393
5394 /* Resolve all line numbers to PC's and verify that the addresses
5395 are ok for the target. */
5396 if (!pending)
5397 breakpoint_sals_to_pc (&sals, addr_start);
5398
5399 /* Verify that condition can be parsed, before setting any
5400 breakpoints. Allocate a separate condition expression for each
5401 breakpoint. */
5402 if (!pending)
5403 {
5404 if (parse_condition_and_thread)
5405 {
5406 /* Here we only parse 'arg' to separate condition
5407 from thread number, so parsing in context of first
5408 sal is OK. When setting the breakpoint we'll
5409 re-parse it in context of each sal. */
5410 cond_string = NULL;
5411 thread = -1;
5412 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string, &thread);
5413 if (cond_string)
5414 make_cleanup (xfree, cond_string);
5415 }
5416 else
5417 {
5418 /* Create a private copy of condition string. */
5419 if (cond_string)
5420 {
5421 cond_string = xstrdup (cond_string);
5422 make_cleanup (xfree, cond_string);
5423 }
5424 }
5425 create_breakpoints (sals, addr_string, cond_string,
5426 hardwareflag ? bp_hardware_breakpoint
5427 : bp_breakpoint,
5428 tempflag ? disp_del : disp_donttouch,
5429 thread, ignore_count, from_tty);
5430 }
5431 else
5432 {
5433 struct symtab_and_line sal = {0};
5434 struct breakpoint *b;
5435
5436 make_cleanup (xfree, copy_arg);
5437
5438 b = set_raw_breakpoint_without_location (hardwareflag
5439 ? bp_hardware_breakpoint
5440 : bp_breakpoint);
5441 set_breakpoint_count (breakpoint_count + 1);
5442 b->number = breakpoint_count;
5443 b->thread = -1;
5444 b->addr_string = addr_string[0];
5445 b->cond_string = NULL;
5446 b->ignore_count = ignore_count;
5447 b->disposition = tempflag ? disp_del : disp_donttouch;
5448 b->condition_not_parsed = 1;
5449 mention (b);
5450 }
5451
5452 if (sals.nelts > 1)
5453 warning (_("Multiple breakpoints were set.\n"
5454 "Use the \"delete\" command to delete unwanted breakpoints."));
5455 /* That's it. Discard the cleanups for data inserted into the
5456 breakpoint. */
5457 discard_cleanups (breakpoint_chain);
5458 /* But cleanup everything else. */
5459 do_cleanups (old_chain);
5460
5461 return GDB_RC_OK;
5462 }
5463
5464 /* Set a breakpoint.
5465 ARG is a string describing breakpoint address,
5466 condition, and thread.
5467 FLAG specifies if a breakpoint is hardware on,
5468 and if breakpoint is temporary, using BP_HARDWARE_FLAG
5469 and BP_TEMPFLAG. */
5470
5471 static int
5472 break_command_1 (char *arg, int flag, int from_tty)
5473 {
5474 int hardwareflag = flag & BP_HARDWAREFLAG;
5475 int tempflag = flag & BP_TEMPFLAG;
5476
5477 return break_command_really (arg,
5478 NULL, 0, 1 /* parse arg */,
5479 tempflag, hardwareflag,
5480 0 /* Ignore count */,
5481 pending_break_support, from_tty);
5482 }
5483
5484
5485 enum gdb_rc
5486 gdb_breakpoint (char *address, char *condition,
5487 int hardwareflag, int tempflag,
5488 int thread, int ignore_count,
5489 int pending,
5490 char **error_message)
5491 {
5492 return break_command_really (address, condition, thread,
5493 0 /* condition and thread are valid. */,
5494 tempflag, hardwareflag,
5495 ignore_count,
5496 pending
5497 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
5498 0);
5499 }
5500
5501
5502 /* Helper function for break_command_1 and disassemble_command. */
5503
5504 void
5505 resolve_sal_pc (struct symtab_and_line *sal)
5506 {
5507 CORE_ADDR pc;
5508
5509 if (sal->pc == 0 && sal->symtab != NULL)
5510 {
5511 if (!find_line_pc (sal->symtab, sal->line, &pc))
5512 error (_("No line %d in file \"%s\"."),
5513 sal->line, sal->symtab->filename);
5514 sal->pc = pc;
5515 }
5516
5517 if (sal->section == 0 && sal->symtab != NULL)
5518 {
5519 struct blockvector *bv;
5520 struct block *b;
5521 struct symbol *sym;
5522
5523 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
5524 if (bv != NULL)
5525 {
5526 sym = block_function (b);
5527 if (sym != NULL)
5528 {
5529 fixup_symbol_section (sym, sal->symtab->objfile);
5530 sal->section = SYMBOL_BFD_SECTION (sym);
5531 }
5532 else
5533 {
5534 /* It really is worthwhile to have the section, so we'll just
5535 have to look harder. This case can be executed if we have
5536 line numbers but no functions (as can happen in assembly
5537 source). */
5538
5539 struct minimal_symbol *msym;
5540
5541 msym = lookup_minimal_symbol_by_pc (sal->pc);
5542 if (msym)
5543 sal->section = SYMBOL_BFD_SECTION (msym);
5544 }
5545 }
5546 }
5547 }
5548
5549 void
5550 break_command (char *arg, int from_tty)
5551 {
5552 break_command_1 (arg, 0, from_tty);
5553 }
5554
5555 void
5556 tbreak_command (char *arg, int from_tty)
5557 {
5558 break_command_1 (arg, BP_TEMPFLAG, from_tty);
5559 }
5560
5561 static void
5562 hbreak_command (char *arg, int from_tty)
5563 {
5564 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
5565 }
5566
5567 static void
5568 thbreak_command (char *arg, int from_tty)
5569 {
5570 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
5571 }
5572
5573 static void
5574 stop_command (char *arg, int from_tty)
5575 {
5576 printf_filtered (_("Specify the type of breakpoint to set.\n\
5577 Usage: stop in <function | address>\n\
5578 stop at <line>\n"));
5579 }
5580
5581 static void
5582 stopin_command (char *arg, int from_tty)
5583 {
5584 int badInput = 0;
5585
5586 if (arg == (char *) NULL)
5587 badInput = 1;
5588 else if (*arg != '*')
5589 {
5590 char *argptr = arg;
5591 int hasColon = 0;
5592
5593 /* look for a ':'. If this is a line number specification, then
5594 say it is bad, otherwise, it should be an address or
5595 function/method name */
5596 while (*argptr && !hasColon)
5597 {
5598 hasColon = (*argptr == ':');
5599 argptr++;
5600 }
5601
5602 if (hasColon)
5603 badInput = (*argptr != ':'); /* Not a class::method */
5604 else
5605 badInput = isdigit (*arg); /* a simple line number */
5606 }
5607
5608 if (badInput)
5609 printf_filtered (_("Usage: stop in <function | address>\n"));
5610 else
5611 break_command_1 (arg, 0, from_tty);
5612 }
5613
5614 static void
5615 stopat_command (char *arg, int from_tty)
5616 {
5617 int badInput = 0;
5618
5619 if (arg == (char *) NULL || *arg == '*') /* no line number */
5620 badInput = 1;
5621 else
5622 {
5623 char *argptr = arg;
5624 int hasColon = 0;
5625
5626 /* look for a ':'. If there is a '::' then get out, otherwise
5627 it is probably a line number. */
5628 while (*argptr && !hasColon)
5629 {
5630 hasColon = (*argptr == ':');
5631 argptr++;
5632 }
5633
5634 if (hasColon)
5635 badInput = (*argptr == ':'); /* we have class::method */
5636 else
5637 badInput = !isdigit (*arg); /* not a line number */
5638 }
5639
5640 if (badInput)
5641 printf_filtered (_("Usage: stop at <line>\n"));
5642 else
5643 break_command_1 (arg, 0, from_tty);
5644 }
5645
5646 /* accessflag: hw_write: watch write,
5647 hw_read: watch read,
5648 hw_access: watch access (read or write) */
5649 static void
5650 watch_command_1 (char *arg, int accessflag, int from_tty)
5651 {
5652 struct breakpoint *b, *scope_breakpoint = NULL;
5653 struct symtab_and_line sal;
5654 struct expression *exp;
5655 struct block *exp_valid_block;
5656 struct value *val, *mark;
5657 struct frame_info *frame;
5658 struct frame_info *prev_frame = NULL;
5659 char *exp_start = NULL;
5660 char *exp_end = NULL;
5661 char *tok, *id_tok_start, *end_tok;
5662 int toklen;
5663 char *cond_start = NULL;
5664 char *cond_end = NULL;
5665 struct expression *cond = NULL;
5666 int i, other_type_used, target_resources_ok = 0;
5667 enum bptype bp_type;
5668 int mem_cnt = 0;
5669 int thread = -1;
5670
5671 init_sal (&sal); /* initialize to zeroes */
5672
5673 /* Make sure that we actually have parameters to parse. */
5674 if (arg != NULL && arg[0] != '\0')
5675 {
5676 toklen = strlen (arg); /* Size of argument list. */
5677
5678 /* Points tok to the end of the argument list. */
5679 tok = arg + toklen - 1;
5680
5681 /* Go backwards in the parameters list. Skip the last parameter.
5682 If we're expecting a 'thread <thread_num>' parameter, this should
5683 be the thread identifier. */
5684 while (tok > arg && (*tok == ' ' || *tok == '\t'))
5685 tok--;
5686 while (tok > arg && (*tok != ' ' && *tok != '\t'))
5687 tok--;
5688
5689 /* Points end_tok to the beginning of the last token. */
5690 id_tok_start = tok + 1;
5691
5692 /* Go backwards in the parameters list. Skip one more parameter.
5693 If we're expecting a 'thread <thread_num>' parameter, we should
5694 reach a "thread" token. */
5695 while (tok > arg && (*tok == ' ' || *tok == '\t'))
5696 tok--;
5697
5698 end_tok = tok;
5699
5700 while (tok > arg && (*tok != ' ' && *tok != '\t'))
5701 tok--;
5702
5703 /* Move the pointer forward to skip the whitespace and
5704 calculate the length of the token. */
5705 tok++;
5706 toklen = end_tok - tok;
5707
5708 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
5709 {
5710 /* At this point we've found a "thread" token, which means
5711 the user is trying to set a watchpoint that triggers
5712 only in a specific thread. */
5713 char *endp;
5714
5715 /* Extract the thread ID from the next token. */
5716 thread = strtol (id_tok_start, &endp, 0);
5717
5718 /* Check if the user provided a valid numeric value for the
5719 thread ID. */
5720 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5721 error (_("Invalid thread ID specification %s."), id_tok_start);
5722
5723 /* Check if the thread actually exists. */
5724 if (!valid_thread_id (thread))
5725 error (_("Unknown thread %d."), thread);
5726
5727 /* Truncate the string and get rid of the thread <thread_num>
5728 parameter before the parameter list is parsed by the
5729 evaluate_expression() function. */
5730 *tok = '\0';
5731 }
5732 }
5733
5734 /* Parse the rest of the arguments. */
5735 innermost_block = NULL;
5736 exp_start = arg;
5737 exp = parse_exp_1 (&arg, 0, 0);
5738 exp_end = arg;
5739 exp_valid_block = innermost_block;
5740 mark = value_mark ();
5741 val = evaluate_expression (exp);
5742 release_value (val);
5743 if (value_lazy (val))
5744 value_fetch_lazy (val);
5745
5746 tok = arg;
5747 while (*tok == ' ' || *tok == '\t')
5748 tok++;
5749 end_tok = tok;
5750
5751 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
5752 end_tok++;
5753
5754 toklen = end_tok - tok;
5755 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
5756 {
5757 tok = cond_start = end_tok + 1;
5758 cond = parse_exp_1 (&tok, 0, 0);
5759 cond_end = tok;
5760 }
5761 if (*tok)
5762 error (_("Junk at end of command."));
5763
5764 if (accessflag == hw_read)
5765 bp_type = bp_read_watchpoint;
5766 else if (accessflag == hw_access)
5767 bp_type = bp_access_watchpoint;
5768 else
5769 bp_type = bp_hardware_watchpoint;
5770
5771 mem_cnt = can_use_hardware_watchpoint (val);
5772 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
5773 error (_("Expression cannot be implemented with read/access watchpoint."));
5774 if (mem_cnt != 0)
5775 {
5776 i = hw_watchpoint_used_count (bp_type, &other_type_used);
5777 target_resources_ok =
5778 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_type, i + mem_cnt,
5779 other_type_used);
5780 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
5781 error (_("Target does not support this type of hardware watchpoint."));
5782
5783 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
5784 error (_("Target can only support one kind of HW watchpoint at a time."));
5785 }
5786
5787 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
5788 watchpoint could not be set. */
5789 if (!mem_cnt || target_resources_ok <= 0)
5790 bp_type = bp_watchpoint;
5791
5792 frame = block_innermost_frame (exp_valid_block);
5793 if (frame)
5794 prev_frame = get_prev_frame (frame);
5795 else
5796 prev_frame = NULL;
5797
5798 /* If the expression is "local", then set up a "watchpoint scope"
5799 breakpoint at the point where we've left the scope of the watchpoint
5800 expression. Create the scope breakpoint before the watchpoint, so
5801 that we will encounter it first in bpstat_stop_status. */
5802 if (innermost_block && prev_frame)
5803 {
5804 scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
5805 bp_watchpoint_scope);
5806
5807 scope_breakpoint->enable_state = bp_enabled;
5808
5809 /* Automatically delete the breakpoint when it hits. */
5810 scope_breakpoint->disposition = disp_del;
5811
5812 /* Only break in the proper frame (help with recursion). */
5813 scope_breakpoint->frame_id = get_frame_id (prev_frame);
5814
5815 /* Set the address at which we will stop. */
5816 scope_breakpoint->loc->requested_address
5817 = get_frame_pc (prev_frame);
5818 scope_breakpoint->loc->address
5819 = adjust_breakpoint_address (scope_breakpoint->loc->requested_address,
5820 scope_breakpoint->type);
5821 }
5822
5823 /* Now set up the breakpoint. */
5824 b = set_raw_breakpoint (sal, bp_type);
5825 set_breakpoint_count (breakpoint_count + 1);
5826 b->number = breakpoint_count;
5827 b->thread = thread;
5828 b->disposition = disp_donttouch;
5829 b->exp = exp;
5830 b->exp_valid_block = exp_valid_block;
5831 b->exp_string = savestring (exp_start, exp_end - exp_start);
5832 b->val = val;
5833 b->loc->cond = cond;
5834 if (cond_start)
5835 b->cond_string = savestring (cond_start, cond_end - cond_start);
5836 else
5837 b->cond_string = 0;
5838
5839 if (frame)
5840 b->watchpoint_frame = get_frame_id (frame);
5841 else
5842 b->watchpoint_frame = null_frame_id;
5843
5844 if (scope_breakpoint != NULL)
5845 {
5846 /* The scope breakpoint is related to the watchpoint. We will
5847 need to act on them together. */
5848 b->related_breakpoint = scope_breakpoint;
5849 scope_breakpoint->related_breakpoint = b;
5850 }
5851
5852 value_free_to_mark (mark);
5853 mention (b);
5854 }
5855
5856 /* Return count of locations need to be watched and can be handled
5857 in hardware. If the watchpoint can not be handled
5858 in hardware return zero. */
5859
5860 static int
5861 can_use_hardware_watchpoint (struct value *v)
5862 {
5863 int found_memory_cnt = 0;
5864 struct value *head = v;
5865
5866 /* Did the user specifically forbid us to use hardware watchpoints? */
5867 if (!can_use_hw_watchpoints)
5868 return 0;
5869
5870 /* Make sure that the value of the expression depends only upon
5871 memory contents, and values computed from them within GDB. If we
5872 find any register references or function calls, we can't use a
5873 hardware watchpoint.
5874
5875 The idea here is that evaluating an expression generates a series
5876 of values, one holding the value of every subexpression. (The
5877 expression a*b+c has five subexpressions: a, b, a*b, c, and
5878 a*b+c.) GDB's values hold almost enough information to establish
5879 the criteria given above --- they identify memory lvalues,
5880 register lvalues, computed values, etcetera. So we can evaluate
5881 the expression, and then scan the chain of values that leaves
5882 behind to decide whether we can detect any possible change to the
5883 expression's final value using only hardware watchpoints.
5884
5885 However, I don't think that the values returned by inferior
5886 function calls are special in any way. So this function may not
5887 notice that an expression involving an inferior function call
5888 can't be watched with hardware watchpoints. FIXME. */
5889 for (; v; v = value_next (v))
5890 {
5891 if (VALUE_LVAL (v) == lval_memory)
5892 {
5893 if (value_lazy (v))
5894 /* A lazy memory lvalue is one that GDB never needed to fetch;
5895 we either just used its address (e.g., `a' in `a.b') or
5896 we never needed it at all (e.g., `a' in `a,b'). */
5897 ;
5898 else
5899 {
5900 /* Ahh, memory we actually used! Check if we can cover
5901 it with hardware watchpoints. */
5902 struct type *vtype = check_typedef (value_type (v));
5903
5904 /* We only watch structs and arrays if user asked for it
5905 explicitly, never if they just happen to appear in a
5906 middle of some value chain. */
5907 if (v == head
5908 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
5909 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
5910 {
5911 CORE_ADDR vaddr = VALUE_ADDRESS (v) + value_offset (v);
5912 int len = TYPE_LENGTH (value_type (v));
5913
5914 if (!TARGET_REGION_OK_FOR_HW_WATCHPOINT (vaddr, len))
5915 return 0;
5916 else
5917 found_memory_cnt++;
5918 }
5919 }
5920 }
5921 else if (VALUE_LVAL (v) != not_lval
5922 && deprecated_value_modifiable (v) == 0)
5923 return 0; /* ??? What does this represent? */
5924 else if (VALUE_LVAL (v) == lval_register)
5925 return 0; /* cannot watch a register with a HW watchpoint */
5926 }
5927
5928 /* The expression itself looks suitable for using a hardware
5929 watchpoint, but give the target machine a chance to reject it. */
5930 return found_memory_cnt;
5931 }
5932
5933 void
5934 watch_command_wrapper (char *arg, int from_tty)
5935 {
5936 watch_command (arg, from_tty);
5937 }
5938
5939 static void
5940 watch_command (char *arg, int from_tty)
5941 {
5942 watch_command_1 (arg, hw_write, from_tty);
5943 }
5944
5945 void
5946 rwatch_command_wrapper (char *arg, int from_tty)
5947 {
5948 rwatch_command (arg, from_tty);
5949 }
5950
5951 static void
5952 rwatch_command (char *arg, int from_tty)
5953 {
5954 watch_command_1 (arg, hw_read, from_tty);
5955 }
5956
5957 void
5958 awatch_command_wrapper (char *arg, int from_tty)
5959 {
5960 awatch_command (arg, from_tty);
5961 }
5962
5963 static void
5964 awatch_command (char *arg, int from_tty)
5965 {
5966 watch_command_1 (arg, hw_access, from_tty);
5967 }
5968 \f
5969
5970 /* Helper routines for the until_command routine in infcmd.c. Here
5971 because it uses the mechanisms of breakpoints. */
5972
5973 /* This function is called by fetch_inferior_event via the
5974 cmd_continuation pointer, to complete the until command. It takes
5975 care of cleaning up the temporary breakpoints set up by the until
5976 command. */
5977 static void
5978 until_break_command_continuation (struct continuation_arg *arg)
5979 {
5980 struct cleanup *cleanups;
5981
5982 cleanups = (struct cleanup *) arg->data.pointer;
5983 do_exec_cleanups (cleanups);
5984 }
5985
5986 void
5987 until_break_command (char *arg, int from_tty, int anywhere)
5988 {
5989 struct symtabs_and_lines sals;
5990 struct symtab_and_line sal;
5991 struct frame_info *frame = get_selected_frame (NULL);
5992 struct frame_info *prev_frame = get_prev_frame (frame);
5993 struct breakpoint *breakpoint;
5994 struct cleanup *old_chain;
5995 struct continuation_arg *arg1;
5996
5997
5998 clear_proceed_status ();
5999
6000 /* Set a breakpoint where the user wants it and at return from
6001 this function */
6002
6003 if (default_breakpoint_valid)
6004 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
6005 default_breakpoint_line, (char ***) NULL, NULL);
6006 else
6007 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
6008 0, (char ***) NULL, NULL);
6009
6010 if (sals.nelts != 1)
6011 error (_("Couldn't get information on specified line."));
6012
6013 sal = sals.sals[0];
6014 xfree (sals.sals); /* malloc'd, so freed */
6015
6016 if (*arg)
6017 error (_("Junk at end of arguments."));
6018
6019 resolve_sal_pc (&sal);
6020
6021 if (anywhere)
6022 /* If the user told us to continue until a specified location,
6023 we don't specify a frame at which we need to stop. */
6024 breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_until);
6025 else
6026 /* Otherwise, specify the current frame, because we want to stop only
6027 at the very same frame. */
6028 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame),
6029 bp_until);
6030
6031 if (!target_can_async_p ())
6032 old_chain = make_cleanup_delete_breakpoint (breakpoint);
6033 else
6034 old_chain = make_exec_cleanup_delete_breakpoint (breakpoint);
6035
6036 /* If we are running asynchronously, and the target supports async
6037 execution, we are not waiting for the target to stop, in the call
6038 tp proceed, below. This means that we cannot delete the
6039 brekpoints until the target has actually stopped. The only place
6040 where we get a chance to do that is in fetch_inferior_event, so
6041 we must set things up for that. */
6042
6043 if (target_can_async_p ())
6044 {
6045 /* In this case the arg for the continuation is just the point
6046 in the exec_cleanups chain from where to start doing
6047 cleanups, because all the continuation does is the cleanups in
6048 the exec_cleanup_chain. */
6049 arg1 =
6050 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
6051 arg1->next = NULL;
6052 arg1->data.pointer = old_chain;
6053
6054 add_continuation (until_break_command_continuation, arg1);
6055 }
6056
6057 /* Keep within the current frame, or in frames called by the current
6058 one. */
6059 if (prev_frame)
6060 {
6061 sal = find_pc_line (get_frame_pc (prev_frame), 0);
6062 sal.pc = get_frame_pc (prev_frame);
6063 breakpoint = set_momentary_breakpoint (sal, get_frame_id (prev_frame),
6064 bp_until);
6065 if (!target_can_async_p ())
6066 make_cleanup_delete_breakpoint (breakpoint);
6067 else
6068 make_exec_cleanup_delete_breakpoint (breakpoint);
6069 }
6070
6071 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
6072 /* Do the cleanups now, anly if we are not running asynchronously,
6073 of if we are, but the target is still synchronous. */
6074 if (!target_can_async_p ())
6075 do_cleanups (old_chain);
6076 }
6077
6078 static void
6079 ep_skip_leading_whitespace (char **s)
6080 {
6081 if ((s == NULL) || (*s == NULL))
6082 return;
6083 while (isspace (**s))
6084 *s += 1;
6085 }
6086
6087 /* This function examines a string, and attempts to find a token
6088 that might be an event name in the leading characters. If a
6089 possible match is found, a pointer to the last character of
6090 the token is returned. Else, NULL is returned. */
6091
6092 static char *
6093 ep_find_event_name_end (char *arg)
6094 {
6095 char *s = arg;
6096 char *event_name_end = NULL;
6097
6098 /* If we could depend upon the presense of strrpbrk, we'd use that... */
6099 if (arg == NULL)
6100 return NULL;
6101
6102 /* We break out of the loop when we find a token delimiter.
6103 Basically, we're looking for alphanumerics and underscores;
6104 anything else delimites the token. */
6105 while (*s != '\0')
6106 {
6107 if (!isalnum (*s) && (*s != '_'))
6108 break;
6109 event_name_end = s;
6110 s++;
6111 }
6112
6113 return event_name_end;
6114 }
6115
6116
6117 /* This function attempts to parse an optional "if <cond>" clause
6118 from the arg string. If one is not found, it returns NULL.
6119
6120 Else, it returns a pointer to the condition string. (It does not
6121 attempt to evaluate the string against a particular block.) And,
6122 it updates arg to point to the first character following the parsed
6123 if clause in the arg string. */
6124
6125 static char *
6126 ep_parse_optional_if_clause (char **arg)
6127 {
6128 char *cond_string;
6129
6130 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
6131 return NULL;
6132
6133 /* Skip the "if" keyword. */
6134 (*arg) += 2;
6135
6136 /* Skip any extra leading whitespace, and record the start of the
6137 condition string. */
6138 ep_skip_leading_whitespace (arg);
6139 cond_string = *arg;
6140
6141 /* Assume that the condition occupies the remainder of the arg string. */
6142 (*arg) += strlen (cond_string);
6143
6144 return cond_string;
6145 }
6146
6147 /* This function attempts to parse an optional filename from the arg
6148 string. If one is not found, it returns NULL.
6149
6150 Else, it returns a pointer to the parsed filename. (This function
6151 makes no attempt to verify that a file of that name exists, or is
6152 accessible.) And, it updates arg to point to the first character
6153 following the parsed filename in the arg string.
6154
6155 Note that clients needing to preserve the returned filename for
6156 future access should copy it to their own buffers. */
6157 static char *
6158 ep_parse_optional_filename (char **arg)
6159 {
6160 static char filename[1024];
6161 char *arg_p = *arg;
6162 int i;
6163 char c;
6164
6165 if ((*arg_p == '\0') || isspace (*arg_p))
6166 return NULL;
6167
6168 for (i = 0;; i++)
6169 {
6170 c = *arg_p;
6171 if (isspace (c))
6172 c = '\0';
6173 filename[i] = c;
6174 if (c == '\0')
6175 break;
6176 arg_p++;
6177 }
6178 *arg = arg_p;
6179
6180 return filename;
6181 }
6182
6183 /* Commands to deal with catching events, such as signals, exceptions,
6184 process start/exit, etc. */
6185
6186 typedef enum
6187 {
6188 catch_fork, catch_vfork
6189 }
6190 catch_fork_kind;
6191
6192 static void
6193 catch_fork_command_1 (catch_fork_kind fork_kind, char *arg, int tempflag,
6194 int from_tty)
6195 {
6196 char *cond_string = NULL;
6197
6198 ep_skip_leading_whitespace (&arg);
6199
6200 /* The allowed syntax is:
6201 catch [v]fork
6202 catch [v]fork if <cond>
6203
6204 First, check if there's an if clause. */
6205 cond_string = ep_parse_optional_if_clause (&arg);
6206
6207 if ((*arg != '\0') && !isspace (*arg))
6208 error (_("Junk at end of arguments."));
6209
6210 /* If this target supports it, create a fork or vfork catchpoint
6211 and enable reporting of such events. */
6212 switch (fork_kind)
6213 {
6214 case catch_fork:
6215 create_fork_event_catchpoint (tempflag, cond_string);
6216 break;
6217 case catch_vfork:
6218 create_vfork_event_catchpoint (tempflag, cond_string);
6219 break;
6220 default:
6221 error (_("unsupported or unknown fork kind; cannot catch it"));
6222 break;
6223 }
6224 }
6225
6226 static void
6227 catch_exec_command_1 (char *arg, int tempflag, int from_tty)
6228 {
6229 char *cond_string = NULL;
6230
6231 ep_skip_leading_whitespace (&arg);
6232
6233 /* The allowed syntax is:
6234 catch exec
6235 catch exec if <cond>
6236
6237 First, check if there's an if clause. */
6238 cond_string = ep_parse_optional_if_clause (&arg);
6239
6240 if ((*arg != '\0') && !isspace (*arg))
6241 error (_("Junk at end of arguments."));
6242
6243 /* If this target supports it, create an exec catchpoint
6244 and enable reporting of such events. */
6245 create_exec_event_catchpoint (tempflag, cond_string);
6246 }
6247
6248 static void
6249 catch_load_command_1 (char *arg, int tempflag, int from_tty)
6250 {
6251 char *dll_pathname = NULL;
6252 char *cond_string = NULL;
6253
6254 ep_skip_leading_whitespace (&arg);
6255
6256 /* The allowed syntax is:
6257 catch load
6258 catch load if <cond>
6259 catch load <filename>
6260 catch load <filename> if <cond>
6261
6262 The user is not allowed to specify the <filename> after an
6263 if clause.
6264
6265 We'll ignore the pathological case of a file named "if".
6266
6267 First, check if there's an if clause. If so, then there
6268 cannot be a filename. */
6269 cond_string = ep_parse_optional_if_clause (&arg);
6270
6271 /* If there was an if clause, then there cannot be a filename.
6272 Else, there might be a filename and an if clause. */
6273 if (cond_string == NULL)
6274 {
6275 dll_pathname = ep_parse_optional_filename (&arg);
6276 ep_skip_leading_whitespace (&arg);
6277 cond_string = ep_parse_optional_if_clause (&arg);
6278 }
6279
6280 if ((*arg != '\0') && !isspace (*arg))
6281 error (_("Junk at end of arguments."));
6282
6283 /* Create a load breakpoint that only triggers when a load of
6284 the specified dll (or any dll, if no pathname was specified)
6285 occurs. */
6286 SOLIB_CREATE_CATCH_LOAD_HOOK (PIDGET (inferior_ptid), tempflag,
6287 dll_pathname, cond_string);
6288 }
6289
6290 static void
6291 catch_unload_command_1 (char *arg, int tempflag, int from_tty)
6292 {
6293 char *dll_pathname = NULL;
6294 char *cond_string = NULL;
6295
6296 ep_skip_leading_whitespace (&arg);
6297
6298 /* The allowed syntax is:
6299 catch unload
6300 catch unload if <cond>
6301 catch unload <filename>
6302 catch unload <filename> if <cond>
6303
6304 The user is not allowed to specify the <filename> after an
6305 if clause.
6306
6307 We'll ignore the pathological case of a file named "if".
6308
6309 First, check if there's an if clause. If so, then there
6310 cannot be a filename. */
6311 cond_string = ep_parse_optional_if_clause (&arg);
6312
6313 /* If there was an if clause, then there cannot be a filename.
6314 Else, there might be a filename and an if clause. */
6315 if (cond_string == NULL)
6316 {
6317 dll_pathname = ep_parse_optional_filename (&arg);
6318 ep_skip_leading_whitespace (&arg);
6319 cond_string = ep_parse_optional_if_clause (&arg);
6320 }
6321
6322 if ((*arg != '\0') && !isspace (*arg))
6323 error (_("Junk at end of arguments."));
6324
6325 /* Create an unload breakpoint that only triggers when an unload of
6326 the specified dll (or any dll, if no pathname was specified)
6327 occurs. */
6328 SOLIB_CREATE_CATCH_UNLOAD_HOOK (PIDGET (inferior_ptid), tempflag,
6329 dll_pathname, cond_string);
6330 }
6331
6332 static enum print_stop_action
6333 print_exception_catchpoint (struct breakpoint *b)
6334 {
6335 annotate_catchpoint (b->number);
6336
6337 if (strstr (b->addr_string, "throw") != NULL)
6338 printf_filtered (_("\nCatchpoint %d (exception thrown)\n"),
6339 b->number);
6340 else
6341 printf_filtered (_("\nCatchpoint %d (exception caught)\n"),
6342 b->number);
6343
6344 return PRINT_SRC_AND_LOC;
6345 }
6346
6347 static void
6348 print_one_exception_catchpoint (struct breakpoint *b, CORE_ADDR *last_addr)
6349 {
6350 if (addressprint)
6351 {
6352 annotate_field (4);
6353 ui_out_field_core_addr (uiout, "addr", b->loc->address);
6354 }
6355 annotate_field (5);
6356 *last_addr = b->loc->address;
6357 if (strstr (b->addr_string, "throw") != NULL)
6358 ui_out_field_string (uiout, "what", "exception throw");
6359 else
6360 ui_out_field_string (uiout, "what", "exception catch");
6361 }
6362
6363 static void
6364 print_mention_exception_catchpoint (struct breakpoint *b)
6365 {
6366 if (strstr (b->addr_string, "throw") != NULL)
6367 printf_filtered (_("Catchpoint %d (throw)"), b->number);
6368 else
6369 printf_filtered (_("Catchpoint %d (catch)"), b->number);
6370 }
6371
6372 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
6373 print_exception_catchpoint,
6374 print_one_exception_catchpoint,
6375 print_mention_exception_catchpoint
6376 };
6377
6378 static int
6379 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
6380 enum exception_event_kind ex_event, int from_tty)
6381 {
6382 char *trigger_func_name, *nameptr;
6383 struct symtabs_and_lines sals;
6384 struct breakpoint *b;
6385
6386 if (ex_event == EX_EVENT_CATCH)
6387 trigger_func_name = xstrdup ("__cxa_begin_catch");
6388 else
6389 trigger_func_name = xstrdup ("__cxa_throw");
6390
6391 nameptr = trigger_func_name;
6392 sals = decode_line_1 (&nameptr, 1, NULL, 0, NULL, NULL);
6393 if (sals.nelts == 0)
6394 {
6395 xfree (trigger_func_name);
6396 return 0;
6397 }
6398
6399 b = set_raw_breakpoint (sals.sals[0], bp_breakpoint);
6400 set_breakpoint_count (breakpoint_count + 1);
6401 b->number = breakpoint_count;
6402 b->cond_string = (cond_string == NULL) ?
6403 NULL : savestring (cond_string, strlen (cond_string));
6404 b->thread = -1;
6405 b->addr_string = trigger_func_name;
6406 b->enable_state = bp_enabled;
6407 b->disposition = tempflag ? disp_del : disp_donttouch;
6408 b->ops = &gnu_v3_exception_catchpoint_ops;
6409
6410 xfree (sals.sals);
6411 mention (b);
6412 return 1;
6413 }
6414
6415 /* Deal with "catch catch" and "catch throw" commands */
6416
6417 static void
6418 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
6419 int tempflag, int from_tty)
6420 {
6421 char *cond_string = NULL;
6422 struct symtab_and_line *sal = NULL;
6423
6424 ep_skip_leading_whitespace (&arg);
6425
6426 cond_string = ep_parse_optional_if_clause (&arg);
6427
6428 if ((*arg != '\0') && !isspace (*arg))
6429 error (_("Junk at end of arguments."));
6430
6431 if ((ex_event != EX_EVENT_THROW) &&
6432 (ex_event != EX_EVENT_CATCH))
6433 error (_("Unsupported or unknown exception event; cannot catch it"));
6434
6435 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
6436 return;
6437
6438 warning (_("Unsupported with this platform/compiler combination."));
6439 }
6440
6441 /* Create a breakpoint struct for Ada exception catchpoints. */
6442
6443 static void
6444 create_ada_exception_breakpoint (struct symtab_and_line sal,
6445 char *addr_string,
6446 char *exp_string,
6447 char *cond_string,
6448 struct expression *cond,
6449 struct breakpoint_ops *ops,
6450 int tempflag,
6451 int from_tty)
6452 {
6453 struct breakpoint *b;
6454
6455 if (from_tty)
6456 {
6457 describe_other_breakpoints (sal.pc, sal.section, -1);
6458 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
6459 version for exception catchpoints, because two catchpoints
6460 used for different exception names will use the same address.
6461 In this case, a "breakpoint ... also set at..." warning is
6462 unproductive. Besides. the warning phrasing is also a bit
6463 inapropriate, we should use the word catchpoint, and tell
6464 the user what type of catchpoint it is. The above is good
6465 enough for now, though. */
6466 }
6467
6468 b = set_raw_breakpoint (sal, bp_breakpoint);
6469 set_breakpoint_count (breakpoint_count + 1);
6470
6471 b->enable_state = bp_enabled;
6472 b->disposition = tempflag ? disp_del : disp_donttouch;
6473 b->number = breakpoint_count;
6474 b->ignore_count = 0;
6475 b->loc->cond = cond;
6476 b->addr_string = addr_string;
6477 b->language = language_ada;
6478 b->cond_string = cond_string;
6479 b->exp_string = exp_string;
6480 b->thread = -1;
6481 b->ops = ops;
6482
6483 mention (b);
6484 }
6485
6486 /* Implement the "catch exception" command. */
6487
6488 static void
6489 catch_ada_exception_command (char *arg, int tempflag, int from_tty)
6490 {
6491 struct symtab_and_line sal;
6492 enum bptype type;
6493 char *addr_string = NULL;
6494 char *exp_string = NULL;
6495 char *cond_string = NULL;
6496 struct expression *cond = NULL;
6497 struct breakpoint_ops *ops = NULL;
6498
6499 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
6500 &cond_string, &cond, &ops);
6501 create_ada_exception_breakpoint (sal, addr_string, exp_string,
6502 cond_string, cond, ops, tempflag,
6503 from_tty);
6504 }
6505
6506 /* Implement the "catch assert" command. */
6507
6508 static void
6509 catch_assert_command (char *arg, int tempflag, int from_tty)
6510 {
6511 struct symtab_and_line sal;
6512 char *addr_string = NULL;
6513 struct breakpoint_ops *ops = NULL;
6514
6515 sal = ada_decode_assert_location (arg, &addr_string, &ops);
6516 create_ada_exception_breakpoint (sal, addr_string, NULL, NULL, NULL, ops,
6517 tempflag, from_tty);
6518 }
6519
6520 static void
6521 catch_command_1 (char *arg, int tempflag, int from_tty)
6522 {
6523
6524 /* The first argument may be an event name, such as "start" or "load".
6525 If so, then handle it as such. If it doesn't match an event name,
6526 then attempt to interpret it as an exception name. (This latter is
6527 the v4.16-and-earlier GDB meaning of the "catch" command.)
6528
6529 First, try to find the bounds of what might be an event name. */
6530 char *arg1_start = arg;
6531 char *arg1_end;
6532 int arg1_length;
6533
6534 if (arg1_start == NULL)
6535 {
6536 /* Old behaviour was to use pre-v-4.16 syntax */
6537 /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
6538 /* return; */
6539 /* Now, this is not allowed */
6540 error (_("Catch requires an event name."));
6541
6542 }
6543 arg1_end = ep_find_event_name_end (arg1_start);
6544 if (arg1_end == NULL)
6545 error (_("catch requires an event"));
6546 arg1_length = arg1_end + 1 - arg1_start;
6547
6548 /* Try to match what we found against known event names. */
6549 if (strncmp (arg1_start, "signal", arg1_length) == 0)
6550 {
6551 error (_("Catch of signal not yet implemented"));
6552 }
6553 else if (strncmp (arg1_start, "catch", arg1_length) == 0)
6554 {
6555 catch_exception_command_1 (EX_EVENT_CATCH, arg1_end + 1,
6556 tempflag, from_tty);
6557 }
6558 else if (strncmp (arg1_start, "throw", arg1_length) == 0)
6559 {
6560 catch_exception_command_1 (EX_EVENT_THROW, arg1_end + 1,
6561 tempflag, from_tty);
6562 }
6563 else if (strncmp (arg1_start, "thread_start", arg1_length) == 0)
6564 {
6565 error (_("Catch of thread_start not yet implemented"));
6566 }
6567 else if (strncmp (arg1_start, "thread_exit", arg1_length) == 0)
6568 {
6569 error (_("Catch of thread_exit not yet implemented"));
6570 }
6571 else if (strncmp (arg1_start, "thread_join", arg1_length) == 0)
6572 {
6573 error (_("Catch of thread_join not yet implemented"));
6574 }
6575 else if (strncmp (arg1_start, "start", arg1_length) == 0)
6576 {
6577 error (_("Catch of start not yet implemented"));
6578 }
6579 else if (strncmp (arg1_start, "exit", arg1_length) == 0)
6580 {
6581 error (_("Catch of exit not yet implemented"));
6582 }
6583 else if (strncmp (arg1_start, "fork", arg1_length) == 0)
6584 {
6585 catch_fork_command_1 (catch_fork, arg1_end + 1, tempflag, from_tty);
6586 }
6587 else if (strncmp (arg1_start, "vfork", arg1_length) == 0)
6588 {
6589 catch_fork_command_1 (catch_vfork, arg1_end + 1, tempflag, from_tty);
6590 }
6591 else if (strncmp (arg1_start, "exec", arg1_length) == 0)
6592 {
6593 catch_exec_command_1 (arg1_end + 1, tempflag, from_tty);
6594 }
6595 else if (strncmp (arg1_start, "load", arg1_length) == 0)
6596 {
6597 catch_load_command_1 (arg1_end + 1, tempflag, from_tty);
6598 }
6599 else if (strncmp (arg1_start, "unload", arg1_length) == 0)
6600 {
6601 catch_unload_command_1 (arg1_end + 1, tempflag, from_tty);
6602 }
6603 else if (strncmp (arg1_start, "stop", arg1_length) == 0)
6604 {
6605 error (_("Catch of stop not yet implemented"));
6606 }
6607 else if (strncmp (arg1_start, "exception", arg1_length) == 0)
6608 {
6609 catch_ada_exception_command (arg1_end + 1, tempflag, from_tty);
6610 }
6611
6612 else if (strncmp (arg1_start, "assert", arg1_length) == 0)
6613 {
6614 catch_assert_command (arg1_end + 1, tempflag, from_tty);
6615 }
6616
6617 /* This doesn't appear to be an event name */
6618
6619 else
6620 {
6621 /* Pre-v.4.16 behaviour was to treat the argument
6622 as the name of an exception */
6623 /* catch_throw_command_1 (arg1_start, tempflag, from_tty); */
6624 /* Now this is not allowed */
6625 error (_("Unknown event kind specified for catch"));
6626
6627 }
6628 }
6629
6630 static void
6631 catch_command (char *arg, int from_tty)
6632 {
6633 catch_command_1 (arg, 0, from_tty);
6634 }
6635 \f
6636
6637 static void
6638 tcatch_command (char *arg, int from_tty)
6639 {
6640 catch_command_1 (arg, 1, from_tty);
6641 }
6642
6643 /* Delete breakpoints by address or line. */
6644
6645 static void
6646 clear_command (char *arg, int from_tty)
6647 {
6648 struct breakpoint *b;
6649 VEC(breakpoint_p) *found = 0;
6650 int ix;
6651 int default_match;
6652 struct symtabs_and_lines sals;
6653 struct symtab_and_line sal;
6654 int i;
6655
6656 if (arg)
6657 {
6658 sals = decode_line_spec (arg, 1);
6659 default_match = 0;
6660 }
6661 else
6662 {
6663 sals.sals = (struct symtab_and_line *)
6664 xmalloc (sizeof (struct symtab_and_line));
6665 make_cleanup (xfree, sals.sals);
6666 init_sal (&sal); /* initialize to zeroes */
6667 sal.line = default_breakpoint_line;
6668 sal.symtab = default_breakpoint_symtab;
6669 sal.pc = default_breakpoint_address;
6670 if (sal.symtab == 0)
6671 error (_("No source file specified."));
6672
6673 sals.sals[0] = sal;
6674 sals.nelts = 1;
6675
6676 default_match = 1;
6677 }
6678
6679 /* We don't call resolve_sal_pc here. That's not
6680 as bad as it seems, because all existing breakpoints
6681 typically have both file/line and pc set. So, if
6682 clear is given file/line, we can match this to existing
6683 breakpoint without obtaining pc at all.
6684
6685 We only support clearing given the address explicitly
6686 present in breakpoint table. Say, we've set breakpoint
6687 at file:line. There were several PC values for that file:line,
6688 due to optimization, all in one block.
6689 We've picked one PC value. If "clear" is issued with another
6690 PC corresponding to the same file:line, the breakpoint won't
6691 be cleared. We probably can still clear the breakpoint, but
6692 since the other PC value is never presented to user, user
6693 can only find it by guessing, and it does not seem important
6694 to support that. */
6695
6696 /* For each line spec given, delete bps which correspond
6697 to it. Do it in two passes, solely to preserve the current
6698 behavior that from_tty is forced true if we delete more than
6699 one breakpoint. */
6700
6701 found = NULL;
6702 for (i = 0; i < sals.nelts; i++)
6703 {
6704 /* If exact pc given, clear bpts at that pc.
6705 If line given (pc == 0), clear all bpts on specified line.
6706 If defaulting, clear all bpts on default line
6707 or at default pc.
6708
6709 defaulting sal.pc != 0 tests to do
6710
6711 0 1 pc
6712 1 1 pc _and_ line
6713 0 0 line
6714 1 0 <can't happen> */
6715
6716 sal = sals.sals[i];
6717
6718 /* Find all matching breakpoints and add them to
6719 'found'. */
6720 ALL_BREAKPOINTS (b)
6721 {
6722 int match = 0;
6723 /* Are we going to delete b? */
6724 if (b->type != bp_none
6725 && b->type != bp_watchpoint
6726 && b->type != bp_hardware_watchpoint
6727 && b->type != bp_read_watchpoint
6728 && b->type != bp_access_watchpoint)
6729 {
6730 struct bp_location *loc = b->loc;
6731 for (; loc; loc = loc->next)
6732 {
6733 int pc_match = sal.pc
6734 && (loc->address == sal.pc)
6735 && (!section_is_overlay (loc->section)
6736 || loc->section == sal.section);
6737 int line_match = ((default_match || (0 == sal.pc))
6738 && b->source_file != NULL
6739 && sal.symtab != NULL
6740 && strcmp (b->source_file, sal.symtab->filename) == 0
6741 && b->line_number == sal.line);
6742 if (pc_match || line_match)
6743 {
6744 match = 1;
6745 break;
6746 }
6747 }
6748 }
6749
6750 if (match)
6751 VEC_safe_push(breakpoint_p, found, b);
6752 }
6753 }
6754 /* Now go thru the 'found' chain and delete them. */
6755 if (VEC_empty(breakpoint_p, found))
6756 {
6757 if (arg)
6758 error (_("No breakpoint at %s."), arg);
6759 else
6760 error (_("No breakpoint at this line."));
6761 }
6762
6763 if (VEC_length(breakpoint_p, found) > 1)
6764 from_tty = 1; /* Always report if deleted more than one */
6765 if (from_tty)
6766 {
6767 if (VEC_length(breakpoint_p, found) == 1)
6768 printf_unfiltered (_("Deleted breakpoint "));
6769 else
6770 printf_unfiltered (_("Deleted breakpoints "));
6771 }
6772 breakpoints_changed ();
6773
6774 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
6775 {
6776 if (from_tty)
6777 printf_unfiltered ("%d ", b->number);
6778 delete_breakpoint (b);
6779 }
6780 if (from_tty)
6781 putchar_unfiltered ('\n');
6782 }
6783 \f
6784 /* Delete breakpoint in BS if they are `delete' breakpoints and
6785 all breakpoints that are marked for deletion, whether hit or not.
6786 This is called after any breakpoint is hit, or after errors. */
6787
6788 void
6789 breakpoint_auto_delete (bpstat bs)
6790 {
6791 struct breakpoint *b, *temp;
6792
6793 for (; bs; bs = bs->next)
6794 if (bs->breakpoint_at && bs->breakpoint_at->owner->disposition == disp_del
6795 && bs->stop)
6796 delete_breakpoint (bs->breakpoint_at->owner);
6797
6798 ALL_BREAKPOINTS_SAFE (b, temp)
6799 {
6800 if (b->disposition == disp_del_at_next_stop)
6801 delete_breakpoint (b);
6802 }
6803 }
6804
6805 /* Remove locations of breakpoint BPT from
6806 the global list of breakpoint locations. */
6807
6808 static void
6809 unlink_locations_from_global_list (struct breakpoint *bpt)
6810 {
6811 /* This code assumes that the locations
6812 of a breakpoint are found in the global list
6813 in the same order, but not necessary adjacent. */
6814 struct bp_location **tmp = &bp_location_chain;
6815 struct bp_location *here = bpt->loc;
6816
6817 if (here == NULL)
6818 return;
6819
6820 for (; *tmp && here;)
6821 {
6822 if (*tmp == here)
6823 {
6824 *tmp = here->global_next;
6825 here = here->next;
6826 }
6827 else
6828 {
6829 tmp = &((*tmp)->global_next);
6830 }
6831 }
6832 }
6833
6834 /* Delete a breakpoint and clean up all traces of it in the data
6835 structures. */
6836
6837 void
6838 delete_breakpoint (struct breakpoint *bpt)
6839 {
6840 struct breakpoint *b;
6841 bpstat bs;
6842 struct bp_location *loc;
6843
6844 gdb_assert (bpt != NULL);
6845
6846 /* Has this bp already been deleted? This can happen because multiple
6847 lists can hold pointers to bp's. bpstat lists are especial culprits.
6848
6849 One example of this happening is a watchpoint's scope bp. When the
6850 scope bp triggers, we notice that the watchpoint is out of scope, and
6851 delete it. We also delete its scope bp. But the scope bp is marked
6852 "auto-deleting", and is already on a bpstat. That bpstat is then
6853 checked for auto-deleting bp's, which are deleted.
6854
6855 A real solution to this problem might involve reference counts in bp's,
6856 and/or giving them pointers back to their referencing bpstat's, and
6857 teaching delete_breakpoint to only free a bp's storage when no more
6858 references were extent. A cheaper bandaid was chosen. */
6859 if (bpt->type == bp_none)
6860 return;
6861
6862 if (deprecated_delete_breakpoint_hook)
6863 deprecated_delete_breakpoint_hook (bpt);
6864 breakpoint_delete_event (bpt->number);
6865
6866 for (loc = bpt->loc; loc; loc = loc->next)
6867 {
6868 if (loc->inserted)
6869 remove_breakpoint (loc, mark_inserted);
6870
6871 if (loc->cond)
6872 xfree (loc->cond);
6873
6874 if (loc->function_name)
6875 xfree (loc->function_name);
6876 }
6877
6878 if (breakpoint_chain == bpt)
6879 breakpoint_chain = bpt->next;
6880
6881 ALL_BREAKPOINTS (b)
6882 if (b->next == bpt)
6883 {
6884 b->next = bpt->next;
6885 break;
6886 }
6887
6888 unlink_locations_from_global_list (bpt);
6889
6890 check_duplicates (bpt);
6891
6892 if (bpt->type != bp_hardware_watchpoint
6893 && bpt->type != bp_read_watchpoint
6894 && bpt->type != bp_access_watchpoint
6895 && bpt->type != bp_catch_fork
6896 && bpt->type != bp_catch_vfork
6897 && bpt->type != bp_catch_exec)
6898 for (loc = bpt->loc; loc; loc = loc->next)
6899 {
6900 /* If this breakpoint location was inserted, and there is
6901 another breakpoint at the same address, we need to
6902 insert the other breakpoint. */
6903 if (loc->inserted)
6904 {
6905 struct bp_location *loc2;
6906 ALL_BP_LOCATIONS (loc2)
6907 if (loc2->address == loc->address
6908 && loc2->section == loc->section
6909 && !loc->duplicate
6910 && loc2->owner->enable_state != bp_disabled
6911 && loc2->enabled
6912 && !loc2->shlib_disabled
6913 && loc2->owner->enable_state != bp_call_disabled)
6914 {
6915 int val;
6916
6917 /* We should never reach this point if there is a permanent
6918 breakpoint at the same address as the one being deleted.
6919 If there is a permanent breakpoint somewhere, it should
6920 always be the only one inserted. */
6921 if (loc2->owner->enable_state == bp_permanent)
6922 internal_error (__FILE__, __LINE__,
6923 _("another breakpoint was inserted on top of "
6924 "a permanent breakpoint"));
6925
6926 memset (&loc2->target_info, 0, sizeof (loc2->target_info));
6927 loc2->target_info.placed_address = loc2->address;
6928 if (b->type == bp_hardware_breakpoint)
6929 val = target_insert_hw_breakpoint (&loc2->target_info);
6930 else
6931 val = target_insert_breakpoint (&loc2->target_info);
6932
6933 /* If there was an error in the insert, print a message, then stop execution. */
6934 if (val != 0)
6935 {
6936 struct ui_file *tmp_error_stream = mem_fileopen ();
6937 make_cleanup_ui_file_delete (tmp_error_stream);
6938
6939
6940 if (b->type == bp_hardware_breakpoint)
6941 {
6942 fprintf_unfiltered (tmp_error_stream,
6943 "Cannot insert hardware breakpoint %d.\n"
6944 "You may have requested too many hardware breakpoints.\n",
6945 b->number);
6946 }
6947 else
6948 {
6949 fprintf_unfiltered (tmp_error_stream, "Cannot insert breakpoint %d.\n", b->number);
6950 fprintf_filtered (tmp_error_stream, "Error accessing memory address ");
6951 fputs_filtered (paddress (loc2->address),
6952 tmp_error_stream);
6953 fprintf_filtered (tmp_error_stream, ": %s.\n",
6954 safe_strerror (val));
6955 }
6956
6957 fprintf_unfiltered (tmp_error_stream,"The same program may be running in another process.");
6958 target_terminal_ours_for_output ();
6959 error_stream(tmp_error_stream);
6960 }
6961 else
6962 loc2->inserted = 1;
6963 }
6964 }
6965 }
6966
6967 free_command_lines (&bpt->commands);
6968 if (bpt->cond_string != NULL)
6969 xfree (bpt->cond_string);
6970 if (bpt->addr_string != NULL)
6971 xfree (bpt->addr_string);
6972 if (bpt->exp != NULL)
6973 xfree (bpt->exp);
6974 if (bpt->exp_string != NULL)
6975 xfree (bpt->exp_string);
6976 if (bpt->val != NULL)
6977 value_free (bpt->val);
6978 if (bpt->source_file != NULL)
6979 xfree (bpt->source_file);
6980 if (bpt->dll_pathname != NULL)
6981 xfree (bpt->dll_pathname);
6982 if (bpt->triggered_dll_pathname != NULL)
6983 xfree (bpt->triggered_dll_pathname);
6984 if (bpt->exec_pathname != NULL)
6985 xfree (bpt->exec_pathname);
6986
6987 /* Be sure no bpstat's are pointing at it after it's been freed. */
6988 /* FIXME, how can we find all bpstat's?
6989 We just check stop_bpstat for now. Note that we cannot just
6990 remove bpstats pointing at bpt from the stop_bpstat list
6991 entirely, as breakpoint commands are associated with the bpstat;
6992 if we remove it here, then the later call to
6993 bpstat_do_actions (&stop_bpstat);
6994 in event-top.c won't do anything, and temporary breakpoints
6995 with commands won't work. */
6996 for (bs = stop_bpstat; bs; bs = bs->next)
6997 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
6998 {
6999 bs->breakpoint_at = NULL;
7000 bs->old_val = NULL;
7001 /* bs->commands will be freed later. */
7002 }
7003 /* On the chance that someone will soon try again to delete this same
7004 bp, we mark it as deleted before freeing its storage. */
7005 bpt->type = bp_none;
7006
7007 for (loc = bpt->loc; loc;)
7008 {
7009 struct bp_location *loc_next = loc->next;
7010 xfree (loc);
7011 loc = loc_next;
7012 }
7013 xfree (bpt);
7014 }
7015
7016 static void
7017 do_delete_breakpoint_cleanup (void *b)
7018 {
7019 delete_breakpoint (b);
7020 }
7021
7022 struct cleanup *
7023 make_cleanup_delete_breakpoint (struct breakpoint *b)
7024 {
7025 return make_cleanup (do_delete_breakpoint_cleanup, b);
7026 }
7027
7028 struct cleanup *
7029 make_exec_cleanup_delete_breakpoint (struct breakpoint *b)
7030 {
7031 return make_exec_cleanup (do_delete_breakpoint_cleanup, b);
7032 }
7033
7034 void
7035 delete_command (char *arg, int from_tty)
7036 {
7037 struct breakpoint *b, *temp;
7038
7039 dont_repeat ();
7040
7041 if (arg == 0)
7042 {
7043 int breaks_to_delete = 0;
7044
7045 /* Delete all breakpoints if no argument.
7046 Do not delete internal or call-dummy breakpoints, these
7047 have to be deleted with an explicit breakpoint number argument. */
7048 ALL_BREAKPOINTS (b)
7049 {
7050 if (b->type != bp_call_dummy &&
7051 b->type != bp_shlib_event &&
7052 b->type != bp_thread_event &&
7053 b->type != bp_overlay_event &&
7054 b->number >= 0)
7055 {
7056 breaks_to_delete = 1;
7057 break;
7058 }
7059 }
7060
7061 /* Ask user only if there are some breakpoints to delete. */
7062 if (!from_tty
7063 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
7064 {
7065 ALL_BREAKPOINTS_SAFE (b, temp)
7066 {
7067 if (b->type != bp_call_dummy &&
7068 b->type != bp_shlib_event &&
7069 b->type != bp_thread_event &&
7070 b->type != bp_overlay_event &&
7071 b->number >= 0)
7072 delete_breakpoint (b);
7073 }
7074 }
7075 }
7076 else
7077 map_breakpoint_numbers (arg, delete_breakpoint);
7078 }
7079
7080 static int
7081 all_locations_are_pending (struct bp_location *loc)
7082 {
7083 for (; loc; loc = loc->next)
7084 if (!loc->shlib_disabled)
7085 return 0;
7086 return 1;
7087 }
7088
7089 static void
7090 update_breakpoint_locations (struct breakpoint *b,
7091 struct symtabs_and_lines sals)
7092 {
7093 int i;
7094 char *s;
7095 struct bp_location *existing_locations = b->loc;
7096
7097 /* If there's no new locations, and all existing locations
7098 are pending, don't do anything. This optimizes
7099 the common case where all locations are in the same
7100 shared library, that was unloaded. We'd like to
7101 retain the location, so that when the library
7102 is loaded again, we don't loose the enabled/disabled
7103 status of the individual locations. */
7104 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
7105 return;
7106
7107 unlink_locations_from_global_list (b);
7108 b->loc = NULL;
7109
7110 for (i = 0; i < sals.nelts; ++i)
7111 {
7112 struct bp_location *new_loc =
7113 add_location_to_breakpoint (b, b->type, &(sals.sals[i]));
7114
7115 /* Reparse conditions, they might contain references to the
7116 old symtab. */
7117 if (b->cond_string != NULL)
7118 {
7119 struct gdb_exception e;
7120
7121 s = b->cond_string;
7122 TRY_CATCH (e, RETURN_MASK_ERROR)
7123 {
7124 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
7125 0);
7126 }
7127 if (e.reason < 0)
7128 {
7129 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
7130 b->number, e.message);
7131 new_loc->enabled = 0;
7132 }
7133 }
7134
7135 if (b->source_file != NULL)
7136 xfree (b->source_file);
7137 if (sals.sals[i].symtab == NULL)
7138 b->source_file = NULL;
7139 else
7140 b->source_file =
7141 savestring (sals.sals[i].symtab->filename,
7142 strlen (sals.sals[i].symtab->filename));
7143
7144 if (b->line_number == 0)
7145 b->line_number = sals.sals[i].line;
7146 }
7147
7148 /* If possible, carry over 'disable' status from existing breakpoints. */
7149 {
7150 struct bp_location *e = existing_locations;
7151 for (; e; e = e->next)
7152 {
7153 if (!e->enabled && e->function_name)
7154 {
7155 struct bp_location *l = b->loc;
7156 for (; l; l = l->next)
7157 if (l->function_name
7158 && strcmp (e->function_name, l->function_name) == 0)
7159 {
7160 l->enabled = 0;
7161 break;
7162 }
7163 }
7164 }
7165 }
7166
7167 while (existing_locations)
7168 {
7169 struct bp_location *next = existing_locations->next;
7170 free_bp_location (existing_locations);
7171 existing_locations = next;
7172 }
7173 }
7174
7175
7176 /* Reset a breakpoint given it's struct breakpoint * BINT.
7177 The value we return ends up being the return value from catch_errors.
7178 Unused in this case. */
7179
7180 static int
7181 breakpoint_re_set_one (void *bint)
7182 {
7183 /* get past catch_errs */
7184 struct breakpoint *b = (struct breakpoint *) bint;
7185 struct value *mark;
7186 int i;
7187 int not_found = 0;
7188 int *not_found_ptr = &not_found;
7189 struct symtabs_and_lines sals = {};
7190 struct symtabs_and_lines expanded;
7191 char *s;
7192 enum enable_state save_enable;
7193 struct gdb_exception e;
7194
7195
7196 switch (b->type)
7197 {
7198 case bp_none:
7199 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
7200 b->number);
7201 return 0;
7202 case bp_breakpoint:
7203 case bp_hardware_breakpoint:
7204 case bp_catch_load:
7205 case bp_catch_unload:
7206 if (b->addr_string == NULL)
7207 {
7208 /* Anything without a string can't be re-set. */
7209 delete_breakpoint (b);
7210 return 0;
7211 }
7212
7213 set_language (b->language);
7214 input_radix = b->input_radix;
7215 s = b->addr_string;
7216 TRY_CATCH (e, RETURN_MASK_ERROR)
7217 {
7218 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
7219 not_found_ptr);
7220 }
7221 if (e.reason < 0)
7222 {
7223 int not_found_and_ok = 0;
7224 /* For pending breakpoints, it's expected that parsing
7225 will fail until the right shared library is loaded.
7226 User has already told to create pending breakpoints and
7227 don't need extra messages. If breakpoint is in bp_shlib_disabled
7228 state, then user already saw the message about that breakpoint
7229 being disabled, and don't want to see more errors. */
7230 if (not_found
7231 && (b->condition_not_parsed
7232 || (b->loc && b->loc->shlib_disabled)
7233 || b->enable_state == bp_disabled))
7234 not_found_and_ok = 1;
7235
7236 if (!not_found_and_ok)
7237 {
7238 /* We surely don't want to warn about the same breakpoint
7239 10 times. One solution, implemented here, is disable
7240 the breakpoint on error. Another solution would be to
7241 have separate 'warning emitted' flag. Since this
7242 happens only when a binary has changed, I don't know
7243 which approach is better. */
7244 b->enable_state = bp_disabled;
7245 throw_exception (e);
7246 }
7247 }
7248
7249 if (not_found)
7250 break;
7251
7252 gdb_assert (sals.nelts == 1);
7253 resolve_sal_pc (&sals.sals[0]);
7254 if (b->condition_not_parsed && s && s[0])
7255 {
7256 char *cond_string = 0;
7257 int thread = -1;
7258 find_condition_and_thread (s, sals.sals[0].pc,
7259 &cond_string, &thread);
7260 if (cond_string)
7261 b->cond_string = cond_string;
7262 b->thread = thread;
7263 b->condition_not_parsed = 0;
7264 }
7265 expanded = expand_line_sal_maybe (sals.sals[0]);
7266 update_breakpoint_locations (b, expanded);
7267
7268 /* Now that this is re-enabled, check_duplicates
7269 can be used. */
7270 check_duplicates (b);
7271
7272 xfree (sals.sals);
7273 break;
7274
7275 case bp_watchpoint:
7276 case bp_hardware_watchpoint:
7277 case bp_read_watchpoint:
7278 case bp_access_watchpoint:
7279 /* Watchpoint can be either on expression using entirely global variables,
7280 or it can be on local variables.
7281
7282 Watchpoints of the first kind are never auto-deleted, and even persist
7283 across program restarts. Since they can use variables from shared
7284 libraries, we need to reparse expression as libraries are loaded
7285 and unloaded.
7286
7287 Watchpoints on local variables can also change meaning as result
7288 of solib event. For example, if a watchpoint uses both a local and
7289 a global variables in expression, it's a local watchpoint, but
7290 unloading of a shared library will make the expression invalid.
7291 This is not a very common use case, but we still re-evaluate
7292 expression, to avoid surprises to the user.
7293
7294 Note that for local watchpoints, we re-evaluate it only if
7295 watchpoints frame id is still valid. If it's not, it means
7296 the watchpoint is out of scope and will be deleted soon. In fact,
7297 I'm not sure we'll ever be called in this case.
7298
7299 If a local watchpoint's frame id is still valid, then
7300 b->exp_valid_block is likewise valid, and we can safely use it.
7301
7302 Don't do anything about disabled watchpoints, since they will
7303 be reevaluated again when enabled. */
7304 update_watchpoint (b, 1 /* reparse */);
7305 break;
7306 /* We needn't really do anything to reset these, since the mask
7307 that requests them is unaffected by e.g., new libraries being
7308 loaded. */
7309 case bp_catch_fork:
7310 case bp_catch_vfork:
7311 case bp_catch_exec:
7312 break;
7313
7314 default:
7315 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
7316 /* fall through */
7317 /* Delete longjmp and overlay event breakpoints; they will be
7318 reset later by breakpoint_re_set. */
7319 case bp_longjmp:
7320 case bp_longjmp_resume:
7321 case bp_overlay_event:
7322 delete_breakpoint (b);
7323 break;
7324
7325 /* This breakpoint is special, it's set up when the inferior
7326 starts and we really don't want to touch it. */
7327 case bp_shlib_event:
7328
7329 /* Like bp_shlib_event, this breakpoint type is special.
7330 Once it is set up, we do not want to touch it. */
7331 case bp_thread_event:
7332
7333 /* Keep temporary breakpoints, which can be encountered when we step
7334 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
7335 Otherwise these should have been blown away via the cleanup chain
7336 or by breakpoint_init_inferior when we rerun the executable. */
7337 case bp_until:
7338 case bp_finish:
7339 case bp_watchpoint_scope:
7340 case bp_call_dummy:
7341 case bp_step_resume:
7342 break;
7343 }
7344
7345 return 0;
7346 }
7347
7348 /* Re-set all breakpoints after symbols have been re-loaded. */
7349 void
7350 breakpoint_re_set (void)
7351 {
7352 struct breakpoint *b, *temp;
7353 enum language save_language;
7354 int save_input_radix;
7355
7356 save_language = current_language->la_language;
7357 save_input_radix = input_radix;
7358 ALL_BREAKPOINTS_SAFE (b, temp)
7359 {
7360 /* Format possible error msg */
7361 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
7362 b->number);
7363 struct cleanup *cleanups = make_cleanup (xfree, message);
7364 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
7365 do_cleanups (cleanups);
7366 }
7367 set_language (save_language);
7368 input_radix = save_input_radix;
7369
7370 if (gdbarch_get_longjmp_target_p (current_gdbarch))
7371 {
7372 create_longjmp_breakpoint ("longjmp");
7373 create_longjmp_breakpoint ("_longjmp");
7374 create_longjmp_breakpoint ("siglongjmp");
7375 create_longjmp_breakpoint ("_siglongjmp");
7376 create_longjmp_breakpoint (NULL);
7377 }
7378
7379 create_overlay_event_breakpoint ("_ovly_debug_event");
7380 }
7381 \f
7382 /* Reset the thread number of this breakpoint:
7383
7384 - If the breakpoint is for all threads, leave it as-is.
7385 - Else, reset it to the current thread for inferior_ptid. */
7386 void
7387 breakpoint_re_set_thread (struct breakpoint *b)
7388 {
7389 if (b->thread != -1)
7390 {
7391 if (in_thread_list (inferior_ptid))
7392 b->thread = pid_to_thread_id (inferior_ptid);
7393 }
7394 }
7395
7396 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7397 If from_tty is nonzero, it prints a message to that effect,
7398 which ends with a period (no newline). */
7399
7400 void
7401 set_ignore_count (int bptnum, int count, int from_tty)
7402 {
7403 struct breakpoint *b;
7404
7405 if (count < 0)
7406 count = 0;
7407
7408 ALL_BREAKPOINTS (b)
7409 if (b->number == bptnum)
7410 {
7411 b->ignore_count = count;
7412 if (from_tty)
7413 {
7414 if (count == 0)
7415 printf_filtered (_("Will stop next time breakpoint %d is reached."),
7416 bptnum);
7417 else if (count == 1)
7418 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
7419 bptnum);
7420 else
7421 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
7422 count, bptnum);
7423 }
7424 breakpoints_changed ();
7425 breakpoint_modify_event (b->number);
7426 return;
7427 }
7428
7429 error (_("No breakpoint number %d."), bptnum);
7430 }
7431
7432 /* Clear the ignore counts of all breakpoints. */
7433 void
7434 breakpoint_clear_ignore_counts (void)
7435 {
7436 struct breakpoint *b;
7437
7438 ALL_BREAKPOINTS (b)
7439 b->ignore_count = 0;
7440 }
7441
7442 /* Command to set ignore-count of breakpoint N to COUNT. */
7443
7444 static void
7445 ignore_command (char *args, int from_tty)
7446 {
7447 char *p = args;
7448 int num;
7449
7450 if (p == 0)
7451 error_no_arg (_("a breakpoint number"));
7452
7453 num = get_number (&p);
7454 if (num == 0)
7455 error (_("bad breakpoint number: '%s'"), args);
7456 if (*p == 0)
7457 error (_("Second argument (specified ignore-count) is missing."));
7458
7459 set_ignore_count (num,
7460 longest_to_int (value_as_long (parse_and_eval (p))),
7461 from_tty);
7462 if (from_tty)
7463 printf_filtered ("\n");
7464 }
7465 \f
7466 /* Call FUNCTION on each of the breakpoints
7467 whose numbers are given in ARGS. */
7468
7469 static void
7470 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
7471 {
7472 char *p = args;
7473 char *p1;
7474 int num;
7475 struct breakpoint *b, *tmp;
7476 int match;
7477
7478 if (p == 0)
7479 error_no_arg (_("one or more breakpoint numbers"));
7480
7481 while (*p)
7482 {
7483 match = 0;
7484 p1 = p;
7485
7486 num = get_number_or_range (&p1);
7487 if (num == 0)
7488 {
7489 warning (_("bad breakpoint number at or near '%s'"), p);
7490 }
7491 else
7492 {
7493 ALL_BREAKPOINTS_SAFE (b, tmp)
7494 if (b->number == num)
7495 {
7496 struct breakpoint *related_breakpoint = b->related_breakpoint;
7497 match = 1;
7498 function (b);
7499 if (related_breakpoint)
7500 function (related_breakpoint);
7501 break;
7502 }
7503 if (match == 0)
7504 printf_unfiltered (_("No breakpoint number %d.\n"), num);
7505 }
7506 p = p1;
7507 }
7508 }
7509
7510 static struct bp_location *
7511 find_location_by_number (char *number)
7512 {
7513 char *dot = strchr (number, '.');
7514 char *p1;
7515 int bp_num;
7516 int loc_num;
7517 struct breakpoint *b;
7518 struct bp_location *loc;
7519
7520 *dot = '\0';
7521
7522 p1 = number;
7523 bp_num = get_number_or_range (&p1);
7524 if (bp_num == 0)
7525 error (_("Bad breakpoint number '%s'"), number);
7526
7527 ALL_BREAKPOINTS (b)
7528 if (b->number == bp_num)
7529 {
7530 break;
7531 }
7532
7533 if (!b || b->number != bp_num)
7534 error (_("Bad breakpoint number '%s'"), number);
7535
7536 p1 = dot+1;
7537 loc_num = get_number_or_range (&p1);
7538 if (loc_num == 0)
7539 error (_("Bad breakpoint location number '%s'"), number);
7540
7541 --loc_num;
7542 loc = b->loc;
7543 for (;loc_num && loc; --loc_num, loc = loc->next)
7544 ;
7545 if (!loc)
7546 error (_("Bad breakpoint location number '%s'"), dot+1);
7547
7548 return loc;
7549 }
7550
7551
7552 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
7553 If from_tty is nonzero, it prints a message to that effect,
7554 which ends with a period (no newline). */
7555
7556 void
7557 disable_breakpoint (struct breakpoint *bpt)
7558 {
7559 /* Never disable a watchpoint scope breakpoint; we want to
7560 hit them when we leave scope so we can delete both the
7561 watchpoint and its scope breakpoint at that time. */
7562 if (bpt->type == bp_watchpoint_scope)
7563 return;
7564
7565 /* You can't disable permanent breakpoints. */
7566 if (bpt->enable_state == bp_permanent)
7567 return;
7568
7569 bpt->enable_state = bp_disabled;
7570
7571 check_duplicates (bpt);
7572
7573 if (deprecated_modify_breakpoint_hook)
7574 deprecated_modify_breakpoint_hook (bpt);
7575 breakpoint_modify_event (bpt->number);
7576 }
7577
7578 static void
7579 disable_command (char *args, int from_tty)
7580 {
7581 struct breakpoint *bpt;
7582 if (args == 0)
7583 ALL_BREAKPOINTS (bpt)
7584 switch (bpt->type)
7585 {
7586 case bp_none:
7587 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
7588 bpt->number);
7589 continue;
7590 case bp_breakpoint:
7591 case bp_catch_load:
7592 case bp_catch_unload:
7593 case bp_catch_fork:
7594 case bp_catch_vfork:
7595 case bp_catch_exec:
7596 case bp_hardware_breakpoint:
7597 case bp_watchpoint:
7598 case bp_hardware_watchpoint:
7599 case bp_read_watchpoint:
7600 case bp_access_watchpoint:
7601 disable_breakpoint (bpt);
7602 default:
7603 continue;
7604 }
7605 else if (strchr (args, '.'))
7606 {
7607 struct bp_location *loc = find_location_by_number (args);
7608 if (loc)
7609 loc->enabled = 0;
7610 check_duplicates (loc->owner);
7611 }
7612 else
7613 map_breakpoint_numbers (args, disable_breakpoint);
7614 }
7615
7616 static void
7617 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
7618 {
7619 int target_resources_ok, other_type_used;
7620 struct value *mark;
7621
7622 if (bpt->type == bp_hardware_breakpoint)
7623 {
7624 int i;
7625 i = hw_breakpoint_used_count ();
7626 target_resources_ok =
7627 TARGET_CAN_USE_HARDWARE_WATCHPOINT (bp_hardware_breakpoint,
7628 i + 1, 0);
7629 if (target_resources_ok == 0)
7630 error (_("No hardware breakpoint support in the target."));
7631 else if (target_resources_ok < 0)
7632 error (_("Hardware breakpoints used exceeds limit."));
7633 }
7634
7635 if (bpt->type == bp_watchpoint ||
7636 bpt->type == bp_hardware_watchpoint ||
7637 bpt->type == bp_read_watchpoint ||
7638 bpt->type == bp_access_watchpoint)
7639 {
7640 struct frame_id saved_frame_id;
7641
7642 saved_frame_id = get_frame_id (get_selected_frame (NULL));
7643 if (bpt->exp_valid_block != NULL)
7644 {
7645 struct frame_info *fr =
7646 fr = frame_find_by_id (bpt->watchpoint_frame);
7647 if (fr == NULL)
7648 {
7649 printf_filtered (_("\
7650 Cannot enable watchpoint %d because the block in which its expression\n\
7651 is valid is not currently in scope.\n"), bpt->number);
7652 return;
7653 }
7654 select_frame (fr);
7655 }
7656
7657 if (bpt->val)
7658 value_free (bpt->val);
7659 mark = value_mark ();
7660 bpt->val = evaluate_expression (bpt->exp);
7661 release_value (bpt->val);
7662 if (value_lazy (bpt->val))
7663 value_fetch_lazy (bpt->val);
7664
7665 if (bpt->type == bp_hardware_watchpoint ||
7666 bpt->type == bp_read_watchpoint ||
7667 bpt->type == bp_access_watchpoint)
7668 {
7669 int i = hw_watchpoint_used_count (bpt->type, &other_type_used);
7670 int mem_cnt = can_use_hardware_watchpoint (bpt->val);
7671
7672 /* Hack around 'unused var' error for some targets here */
7673 (void) mem_cnt, (void) i;
7674 target_resources_ok = TARGET_CAN_USE_HARDWARE_WATCHPOINT (
7675 bpt->type, i + mem_cnt, other_type_used);
7676 /* we can consider of type is bp_hardware_watchpoint, convert to
7677 bp_watchpoint in the following condition */
7678 if (target_resources_ok < 0)
7679 {
7680 printf_filtered (_("\
7681 Cannot enable watchpoint %d because target watch resources\n\
7682 have been allocated for other watchpoints.\n"), bpt->number);
7683 value_free_to_mark (mark);
7684 return;
7685 }
7686 }
7687
7688 select_frame (frame_find_by_id (saved_frame_id));
7689 value_free_to_mark (mark);
7690 }
7691
7692 if (bpt->enable_state != bp_permanent)
7693 bpt->enable_state = bp_enabled;
7694 bpt->disposition = disposition;
7695 check_duplicates (bpt);
7696 breakpoints_changed ();
7697
7698 if (deprecated_modify_breakpoint_hook)
7699 deprecated_modify_breakpoint_hook (bpt);
7700 breakpoint_modify_event (bpt->number);
7701 }
7702
7703
7704 void
7705 enable_breakpoint (struct breakpoint *bpt)
7706 {
7707 do_enable_breakpoint (bpt, bpt->disposition);
7708 }
7709
7710 /* The enable command enables the specified breakpoints (or all defined
7711 breakpoints) so they once again become (or continue to be) effective
7712 in stopping the inferior. */
7713
7714 static void
7715 enable_command (char *args, int from_tty)
7716 {
7717 struct breakpoint *bpt;
7718 if (args == 0)
7719 ALL_BREAKPOINTS (bpt)
7720 switch (bpt->type)
7721 {
7722 case bp_none:
7723 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
7724 bpt->number);
7725 continue;
7726 case bp_breakpoint:
7727 case bp_catch_load:
7728 case bp_catch_unload:
7729 case bp_catch_fork:
7730 case bp_catch_vfork:
7731 case bp_catch_exec:
7732 case bp_hardware_breakpoint:
7733 case bp_watchpoint:
7734 case bp_hardware_watchpoint:
7735 case bp_read_watchpoint:
7736 case bp_access_watchpoint:
7737 enable_breakpoint (bpt);
7738 default:
7739 continue;
7740 }
7741 else if (strchr (args, '.'))
7742 {
7743 struct bp_location *loc = find_location_by_number (args);
7744 if (loc)
7745 loc->enabled = 1;
7746 check_duplicates (loc->owner);
7747 }
7748 else
7749 map_breakpoint_numbers (args, enable_breakpoint);
7750 }
7751
7752 static void
7753 enable_once_breakpoint (struct breakpoint *bpt)
7754 {
7755 do_enable_breakpoint (bpt, disp_disable);
7756 }
7757
7758 static void
7759 enable_once_command (char *args, int from_tty)
7760 {
7761 map_breakpoint_numbers (args, enable_once_breakpoint);
7762 }
7763
7764 static void
7765 enable_delete_breakpoint (struct breakpoint *bpt)
7766 {
7767 do_enable_breakpoint (bpt, disp_del);
7768 }
7769
7770 static void
7771 enable_delete_command (char *args, int from_tty)
7772 {
7773 map_breakpoint_numbers (args, enable_delete_breakpoint);
7774 }
7775 \f
7776 static void
7777 set_breakpoint_cmd (char *args, int from_tty)
7778 {
7779 }
7780
7781 static void
7782 show_breakpoint_cmd (char *args, int from_tty)
7783 {
7784 }
7785
7786 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
7787
7788 struct symtabs_and_lines
7789 decode_line_spec_1 (char *string, int funfirstline)
7790 {
7791 struct symtabs_and_lines sals;
7792 if (string == 0)
7793 error (_("Empty line specification."));
7794 if (default_breakpoint_valid)
7795 sals = decode_line_1 (&string, funfirstline,
7796 default_breakpoint_symtab,
7797 default_breakpoint_line,
7798 (char ***) NULL, NULL);
7799 else
7800 sals = decode_line_1 (&string, funfirstline,
7801 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
7802 if (*string)
7803 error (_("Junk at end of line specification: %s"), string);
7804 return sals;
7805 }
7806
7807 /* Create and insert a raw software breakpoint at PC. Return an
7808 identifier, which should be used to remove the breakpoint later.
7809 In general, places which call this should be using something on the
7810 breakpoint chain instead; this function should be eliminated
7811 someday. */
7812
7813 void *
7814 deprecated_insert_raw_breakpoint (CORE_ADDR pc)
7815 {
7816 struct bp_target_info *bp_tgt;
7817
7818 bp_tgt = xmalloc (sizeof (struct bp_target_info));
7819 memset (bp_tgt, 0, sizeof (struct bp_target_info));
7820
7821 bp_tgt->placed_address = pc;
7822 if (target_insert_breakpoint (bp_tgt) != 0)
7823 {
7824 /* Could not insert the breakpoint. */
7825 xfree (bp_tgt);
7826 return NULL;
7827 }
7828
7829 return bp_tgt;
7830 }
7831
7832 /* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
7833
7834 int
7835 deprecated_remove_raw_breakpoint (void *bp)
7836 {
7837 struct bp_target_info *bp_tgt = bp;
7838 int ret;
7839
7840 ret = target_remove_breakpoint (bp_tgt);
7841 xfree (bp_tgt);
7842
7843 return ret;
7844 }
7845
7846 /* One (or perhaps two) breakpoints used for software single stepping. */
7847
7848 static void *single_step_breakpoints[2];
7849
7850 /* Create and insert a breakpoint for software single step. */
7851
7852 void
7853 insert_single_step_breakpoint (CORE_ADDR next_pc)
7854 {
7855 void **bpt_p;
7856
7857 if (single_step_breakpoints[0] == NULL)
7858 bpt_p = &single_step_breakpoints[0];
7859 else
7860 {
7861 gdb_assert (single_step_breakpoints[1] == NULL);
7862 bpt_p = &single_step_breakpoints[1];
7863 }
7864
7865 /* NOTE drow/2006-04-11: A future improvement to this function would be
7866 to only create the breakpoints once, and actually put them on the
7867 breakpoint chain. That would let us use set_raw_breakpoint. We could
7868 adjust the addresses each time they were needed. Doing this requires
7869 corresponding changes elsewhere where single step breakpoints are
7870 handled, however. So, for now, we use this. */
7871
7872 *bpt_p = deprecated_insert_raw_breakpoint (next_pc);
7873 if (*bpt_p == NULL)
7874 error (_("Could not insert single-step breakpoint at 0x%s"),
7875 paddr_nz (next_pc));
7876 }
7877
7878 /* Remove and delete any breakpoints used for software single step. */
7879
7880 void
7881 remove_single_step_breakpoints (void)
7882 {
7883 gdb_assert (single_step_breakpoints[0] != NULL);
7884
7885 /* See insert_single_step_breakpoint for more about this deprecated
7886 call. */
7887 deprecated_remove_raw_breakpoint (single_step_breakpoints[0]);
7888 single_step_breakpoints[0] = NULL;
7889
7890 if (single_step_breakpoints[1] != NULL)
7891 {
7892 deprecated_remove_raw_breakpoint (single_step_breakpoints[1]);
7893 single_step_breakpoints[1] = NULL;
7894 }
7895 }
7896
7897 /* Check whether a software single-step breakpoint is inserted at PC. */
7898
7899 static int
7900 single_step_breakpoint_inserted_here_p (CORE_ADDR pc)
7901 {
7902 int i;
7903
7904 for (i = 0; i < 2; i++)
7905 {
7906 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
7907 if (bp_tgt && bp_tgt->placed_address == pc)
7908 return 1;
7909 }
7910
7911 return 0;
7912 }
7913
7914 \f
7915 /* This help string is used for the break, hbreak, tbreak and thbreak commands.
7916 It is defined as a macro to prevent duplication.
7917 COMMAND should be a string constant containing the name of the command. */
7918 #define BREAK_ARGS_HELP(command) \
7919 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
7920 LOCATION may be a line number, function name, or \"*\" and an address.\n\
7921 If a line number is specified, break at start of code for that line.\n\
7922 If a function is specified, break at start of code for that function.\n\
7923 If an address is specified, break at that exact address.\n\
7924 With no LOCATION, uses current execution address of selected stack frame.\n\
7925 This is useful for breaking on return to a stack frame.\n\
7926 \n\
7927 THREADNUM is the number from \"info threads\".\n\
7928 CONDITION is a boolean expression.\n\
7929 \n\
7930 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
7931 \n\
7932 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
7933
7934 void
7935 _initialize_breakpoint (void)
7936 {
7937 static struct cmd_list_element *breakpoint_set_cmdlist;
7938 static struct cmd_list_element *breakpoint_show_cmdlist;
7939 struct cmd_list_element *c;
7940
7941 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
7942
7943 breakpoint_chain = 0;
7944 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
7945 before a breakpoint is set. */
7946 breakpoint_count = 0;
7947
7948 add_com ("ignore", class_breakpoint, ignore_command, _("\
7949 Set ignore-count of breakpoint number N to COUNT.\n\
7950 Usage is `ignore N COUNT'."));
7951 if (xdb_commands)
7952 add_com_alias ("bc", "ignore", class_breakpoint, 1);
7953
7954 add_com ("commands", class_breakpoint, commands_command, _("\
7955 Set commands to be executed when a breakpoint is hit.\n\
7956 Give breakpoint number as argument after \"commands\".\n\
7957 With no argument, the targeted breakpoint is the last one set.\n\
7958 The commands themselves follow starting on the next line.\n\
7959 Type a line containing \"end\" to indicate the end of them.\n\
7960 Give \"silent\" as the first line to make the breakpoint silent;\n\
7961 then no output is printed when it is hit, except what the commands print."));
7962
7963 add_com ("condition", class_breakpoint, condition_command, _("\
7964 Specify breakpoint number N to break only if COND is true.\n\
7965 Usage is `condition N COND', where N is an integer and COND is an\n\
7966 expression to be evaluated whenever breakpoint N is reached."));
7967
7968 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
7969 Set a temporary breakpoint.\n\
7970 Like \"break\" except the breakpoint is only temporary,\n\
7971 so it will be deleted when hit. Equivalent to \"break\" followed\n\
7972 by using \"enable delete\" on the breakpoint number.\n\
7973 \n"
7974 BREAK_ARGS_HELP ("tbreak")));
7975 set_cmd_completer (c, location_completer);
7976
7977 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
7978 Set a hardware assisted breakpoint.\n\
7979 Like \"break\" except the breakpoint requires hardware support,\n\
7980 some target hardware may not have this support.\n\
7981 \n"
7982 BREAK_ARGS_HELP ("hbreak")));
7983 set_cmd_completer (c, location_completer);
7984
7985 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
7986 Set a temporary hardware assisted breakpoint.\n\
7987 Like \"hbreak\" except the breakpoint is only temporary,\n\
7988 so it will be deleted when hit.\n\
7989 \n"
7990 BREAK_ARGS_HELP ("thbreak")));
7991 set_cmd_completer (c, location_completer);
7992
7993 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
7994 Enable some breakpoints.\n\
7995 Give breakpoint numbers (separated by spaces) as arguments.\n\
7996 With no subcommand, breakpoints are enabled until you command otherwise.\n\
7997 This is used to cancel the effect of the \"disable\" command.\n\
7998 With a subcommand you can enable temporarily."),
7999 &enablelist, "enable ", 1, &cmdlist);
8000 if (xdb_commands)
8001 add_com ("ab", class_breakpoint, enable_command, _("\
8002 Enable some breakpoints.\n\
8003 Give breakpoint numbers (separated by spaces) as arguments.\n\
8004 With no subcommand, breakpoints are enabled until you command otherwise.\n\
8005 This is used to cancel the effect of the \"disable\" command.\n\
8006 With a subcommand you can enable temporarily."));
8007
8008 add_com_alias ("en", "enable", class_breakpoint, 1);
8009
8010 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
8011 Enable some breakpoints.\n\
8012 Give breakpoint numbers (separated by spaces) as arguments.\n\
8013 This is used to cancel the effect of the \"disable\" command.\n\
8014 May be abbreviated to simply \"enable\".\n"),
8015 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
8016
8017 add_cmd ("once", no_class, enable_once_command, _("\
8018 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8019 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
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 &enablebreaklist);
8026
8027 add_cmd ("delete", no_class, enable_delete_command, _("\
8028 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
8029 If a breakpoint is hit while enabled in this fashion, it is deleted."),
8030 &enablelist);
8031
8032 add_cmd ("once", no_class, enable_once_command, _("\
8033 Enable breakpoints for one hit. Give breakpoint numbers.\n\
8034 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
8035 &enablelist);
8036
8037 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
8038 Disable some breakpoints.\n\
8039 Arguments are breakpoint numbers with spaces in between.\n\
8040 To disable all breakpoints, give no argument.\n\
8041 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
8042 &disablelist, "disable ", 1, &cmdlist);
8043 add_com_alias ("dis", "disable", class_breakpoint, 1);
8044 add_com_alias ("disa", "disable", class_breakpoint, 1);
8045 if (xdb_commands)
8046 add_com ("sb", class_breakpoint, disable_command, _("\
8047 Disable some breakpoints.\n\
8048 Arguments are breakpoint numbers with spaces in between.\n\
8049 To disable all breakpoints, give no argument.\n\
8050 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
8051
8052 add_cmd ("breakpoints", class_alias, disable_command, _("\
8053 Disable some breakpoints.\n\
8054 Arguments are breakpoint numbers with spaces in between.\n\
8055 To disable all breakpoints, give no argument.\n\
8056 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
8057 This command may be abbreviated \"disable\"."),
8058 &disablelist);
8059
8060 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
8061 Delete some breakpoints or auto-display expressions.\n\
8062 Arguments are breakpoint numbers with spaces in between.\n\
8063 To delete all breakpoints, give no argument.\n\
8064 \n\
8065 Also a prefix command for deletion of other GDB objects.\n\
8066 The \"unset\" command is also an alias for \"delete\"."),
8067 &deletelist, "delete ", 1, &cmdlist);
8068 add_com_alias ("d", "delete", class_breakpoint, 1);
8069 add_com_alias ("del", "delete", class_breakpoint, 1);
8070 if (xdb_commands)
8071 add_com ("db", class_breakpoint, delete_command, _("\
8072 Delete some breakpoints.\n\
8073 Arguments are breakpoint numbers with spaces in between.\n\
8074 To delete all breakpoints, give no argument.\n"));
8075
8076 add_cmd ("breakpoints", class_alias, delete_command, _("\
8077 Delete some breakpoints or auto-display expressions.\n\
8078 Arguments are breakpoint numbers with spaces in between.\n\
8079 To delete all breakpoints, give no argument.\n\
8080 This command may be abbreviated \"delete\"."),
8081 &deletelist);
8082
8083 add_com ("clear", class_breakpoint, clear_command, _("\
8084 Clear breakpoint at specified line or function.\n\
8085 Argument may be line number, function name, or \"*\" and an address.\n\
8086 If line number is specified, all breakpoints in that line are cleared.\n\
8087 If function is specified, breakpoints at beginning of function are cleared.\n\
8088 If an address is specified, breakpoints at that address are cleared.\n\
8089 \n\
8090 With no argument, clears all breakpoints in the line that the selected frame\n\
8091 is executing in.\n\
8092 \n\
8093 See also the \"delete\" command which clears breakpoints by number."));
8094
8095 c = add_com ("break", class_breakpoint, break_command, _("\
8096 Set breakpoint at specified line or function.\n"
8097 BREAK_ARGS_HELP ("break")));
8098 set_cmd_completer (c, location_completer);
8099
8100 add_com_alias ("b", "break", class_run, 1);
8101 add_com_alias ("br", "break", class_run, 1);
8102 add_com_alias ("bre", "break", class_run, 1);
8103 add_com_alias ("brea", "break", class_run, 1);
8104
8105 if (xdb_commands)
8106 {
8107 add_com_alias ("ba", "break", class_breakpoint, 1);
8108 add_com_alias ("bu", "ubreak", class_breakpoint, 1);
8109 }
8110
8111 if (dbx_commands)
8112 {
8113 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
8114 Break in function/address or break at a line in the current file."),
8115 &stoplist, "stop ", 1, &cmdlist);
8116 add_cmd ("in", class_breakpoint, stopin_command,
8117 _("Break in function or address."), &stoplist);
8118 add_cmd ("at", class_breakpoint, stopat_command,
8119 _("Break at a line in the current file."), &stoplist);
8120 add_com ("status", class_info, breakpoints_info, _("\
8121 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8122 The \"Type\" column indicates one of:\n\
8123 \tbreakpoint - normal breakpoint\n\
8124 \twatchpoint - watchpoint\n\
8125 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8126 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8127 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8128 address and file/line number respectively.\n\
8129 \n\
8130 Convenience variable \"$_\" and default examine address for \"x\"\n\
8131 are set to the address of the last breakpoint listed unless the command\n\
8132 is prefixed with \"server \".\n\n\
8133 Convenience variable \"$bpnum\" contains the number of the last\n\
8134 breakpoint set."));
8135 }
8136
8137 add_info ("breakpoints", breakpoints_info, _("\
8138 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8139 The \"Type\" column indicates one of:\n\
8140 \tbreakpoint - normal breakpoint\n\
8141 \twatchpoint - watchpoint\n\
8142 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8143 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8144 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8145 address and file/line number respectively.\n\
8146 \n\
8147 Convenience variable \"$_\" and default examine address for \"x\"\n\
8148 are set to the address of the last breakpoint listed unless the command\n\
8149 is prefixed with \"server \".\n\n\
8150 Convenience variable \"$bpnum\" contains the number of the last\n\
8151 breakpoint set."));
8152
8153 if (xdb_commands)
8154 add_com ("lb", class_breakpoint, breakpoints_info, _("\
8155 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
8156 The \"Type\" column indicates one of:\n\
8157 \tbreakpoint - normal breakpoint\n\
8158 \twatchpoint - watchpoint\n\
8159 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8160 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8161 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8162 address and file/line number respectively.\n\
8163 \n\
8164 Convenience variable \"$_\" and default examine address for \"x\"\n\
8165 are set to the address of the last breakpoint listed unless the command\n\
8166 is prefixed with \"server \".\n\n\
8167 Convenience variable \"$bpnum\" contains the number of the last\n\
8168 breakpoint set."));
8169
8170 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
8171 Status of all breakpoints, or breakpoint number NUMBER.\n\
8172 The \"Type\" column indicates one of:\n\
8173 \tbreakpoint - normal breakpoint\n\
8174 \twatchpoint - watchpoint\n\
8175 \tlongjmp - internal breakpoint used to step through longjmp()\n\
8176 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
8177 \tuntil - internal breakpoint used by the \"until\" command\n\
8178 \tfinish - internal breakpoint used by the \"finish\" command\n\
8179 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
8180 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
8181 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
8182 address and file/line number respectively.\n\
8183 \n\
8184 Convenience variable \"$_\" and default examine address for \"x\"\n\
8185 are set to the address of the last breakpoint listed unless the command\n\
8186 is prefixed with \"server \".\n\n\
8187 Convenience variable \"$bpnum\" contains the number of the last\n\
8188 breakpoint set."),
8189 &maintenanceinfolist);
8190
8191 add_com ("catch", class_breakpoint, catch_command, _("\
8192 Set catchpoints to catch events.\n\
8193 Raised signals may be caught:\n\
8194 \tcatch signal - all signals\n\
8195 \tcatch signal <signame> - a particular signal\n\
8196 Raised exceptions may be caught:\n\
8197 \tcatch throw - all exceptions, when thrown\n\
8198 \tcatch throw <exceptname> - a particular exception, when thrown\n\
8199 \tcatch catch - all exceptions, when caught\n\
8200 \tcatch catch <exceptname> - a particular exception, when caught\n\
8201 Thread or process events may be caught:\n\
8202 \tcatch thread_start - any threads, just after creation\n\
8203 \tcatch thread_exit - any threads, just before expiration\n\
8204 \tcatch thread_join - any threads, just after joins\n\
8205 Process events may be caught:\n\
8206 \tcatch start - any processes, just after creation\n\
8207 \tcatch exit - any processes, just before expiration\n\
8208 \tcatch fork - calls to fork()\n\
8209 \tcatch vfork - calls to vfork()\n\
8210 \tcatch exec - calls to exec()\n\
8211 Dynamically-linked library events may be caught:\n\
8212 \tcatch load - loads of any library\n\
8213 \tcatch load <libname> - loads of a particular library\n\
8214 \tcatch unload - unloads of any library\n\
8215 \tcatch unload <libname> - unloads of a particular library\n\
8216 The act of your program's execution stopping may also be caught:\n\
8217 \tcatch stop\n\n\
8218 C++ exceptions may be caught:\n\
8219 \tcatch throw - all exceptions, when thrown\n\
8220 \tcatch catch - all exceptions, when caught\n\
8221 Ada exceptions may be caught:\n\
8222 \tcatch exception - all exceptions, when raised\n\
8223 \tcatch exception <name> - a particular exception, when raised\n\
8224 \tcatch exception unhandled - all unhandled exceptions, when raised\n\
8225 \tcatch assert - all failed assertions, when raised\n\
8226 \n\
8227 Do \"help set follow-fork-mode\" for info on debugging your program\n\
8228 after a fork or vfork is caught.\n\n\
8229 Do \"help breakpoints\" for info on other commands dealing with breakpoints."));
8230
8231 add_com ("tcatch", class_breakpoint, tcatch_command, _("\
8232 Set temporary catchpoints to catch events.\n\
8233 Args like \"catch\" command.\n\
8234 Like \"catch\" except the catchpoint is only temporary,\n\
8235 so it will be deleted when hit. Equivalent to \"catch\" followed\n\
8236 by using \"enable delete\" on the catchpoint number."));
8237
8238 c = add_com ("watch", class_breakpoint, watch_command, _("\
8239 Set a watchpoint for an expression.\n\
8240 A watchpoint stops execution of your program whenever the value of\n\
8241 an expression changes."));
8242 set_cmd_completer (c, location_completer);
8243
8244 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
8245 Set a read watchpoint for an expression.\n\
8246 A watchpoint stops execution of your program whenever the value of\n\
8247 an expression is read."));
8248 set_cmd_completer (c, location_completer);
8249
8250 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
8251 Set a watchpoint for an expression.\n\
8252 A watchpoint stops execution of your program whenever the value of\n\
8253 an expression is either read or written."));
8254 set_cmd_completer (c, location_completer);
8255
8256 add_info ("watchpoints", breakpoints_info,
8257 _("Synonym for ``info breakpoints''."));
8258
8259
8260 /* XXX: cagney/2005-02-23: This should be a boolean, and should
8261 respond to changes - contrary to the description. */
8262 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
8263 &can_use_hw_watchpoints, _("\
8264 Set debugger's willingness to use watchpoint hardware."), _("\
8265 Show debugger's willingness to use watchpoint hardware."), _("\
8266 If zero, gdb will not use hardware for new watchpoints, even if\n\
8267 such is available. (However, any hardware watchpoints that were\n\
8268 created before setting this to nonzero, will continue to use watchpoint\n\
8269 hardware.)"),
8270 NULL,
8271 show_can_use_hw_watchpoints,
8272 &setlist, &showlist);
8273
8274 can_use_hw_watchpoints = 1;
8275
8276 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
8277 Breakpoint specific settings\n\
8278 Configure various breakpoint-specific variables such as\n\
8279 pending breakpoint behavior"),
8280 &breakpoint_set_cmdlist, "set breakpoint ",
8281 0/*allow-unknown*/, &setlist);
8282 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
8283 Breakpoint specific settings\n\
8284 Configure various breakpoint-specific variables such as\n\
8285 pending breakpoint behavior"),
8286 &breakpoint_show_cmdlist, "show breakpoint ",
8287 0/*allow-unknown*/, &showlist);
8288
8289 add_setshow_auto_boolean_cmd ("pending", no_class,
8290 &pending_break_support, _("\
8291 Set debugger's behavior regarding pending breakpoints."), _("\
8292 Show debugger's behavior regarding pending breakpoints."), _("\
8293 If on, an unrecognized breakpoint location will cause gdb to create a\n\
8294 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
8295 an error. If auto, an unrecognized breakpoint location results in a\n\
8296 user-query to see if a pending breakpoint should be created."),
8297 NULL,
8298 show_pending_break_support,
8299 &breakpoint_set_cmdlist,
8300 &breakpoint_show_cmdlist);
8301
8302 pending_break_support = AUTO_BOOLEAN_AUTO;
8303
8304 add_setshow_boolean_cmd ("auto-hw", no_class,
8305 &automatic_hardware_breakpoints, _("\
8306 Set automatic usage of hardware breakpoints."), _("\
8307 Show automatic usage of hardware breakpoints."), _("\
8308 If set, the debugger will automatically use hardware breakpoints for\n\
8309 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
8310 a warning will be emitted for such breakpoints."),
8311 NULL,
8312 show_automatic_hardware_breakpoints,
8313 &breakpoint_set_cmdlist,
8314 &breakpoint_show_cmdlist);
8315
8316 automatic_hardware_breakpoints = 1;
8317 }