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