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