* breakpoint.c (should_be_inserted): Don't insert breakpoints if
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009 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 "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "demangle.h"
42 #include "annotate.h"
43 #include "symfile.h"
44 #include "objfiles.h"
45 #include "source.h"
46 #include "linespec.h"
47 #include "completer.h"
48 #include "gdb.h"
49 #include "ui-out.h"
50 #include "cli/cli-script.h"
51 #include "gdb_assert.h"
52 #include "block.h"
53 #include "solib.h"
54 #include "solist.h"
55 #include "observer.h"
56 #include "exceptions.h"
57 #include "memattr.h"
58 #include "ada-lang.h"
59 #include "top.h"
60 #include "wrapper.h"
61 #include "valprint.h"
62 #include "jit.h"
63 #include "xml-syscall.h"
64
65 /* readline include files */
66 #include "readline/readline.h"
67 #include "readline/history.h"
68
69 /* readline defines this. */
70 #undef savestring
71
72 #include "mi/mi-common.h"
73
74 /* Arguments to pass as context to some catch command handlers. */
75 #define CATCH_PERMANENT ((void *) (uintptr_t) 0)
76 #define CATCH_TEMPORARY ((void *) (uintptr_t) 1)
77
78 /* Prototypes for local functions. */
79
80 static void enable_delete_command (char *, int);
81
82 static void enable_delete_breakpoint (struct breakpoint *);
83
84 static void enable_once_command (char *, int);
85
86 static void enable_once_breakpoint (struct breakpoint *);
87
88 static void disable_command (char *, int);
89
90 static void enable_command (char *, int);
91
92 static void map_breakpoint_numbers (char *, void (*)(struct breakpoint *));
93
94 static void ignore_command (char *, int);
95
96 static int breakpoint_re_set_one (void *);
97
98 static void clear_command (char *, int);
99
100 static void catch_command (char *, int);
101
102 static void watch_command (char *, int);
103
104 static int can_use_hardware_watchpoint (struct value *);
105
106 static void break_command_1 (char *, int, int);
107
108 static void mention (struct breakpoint *);
109
110 /* This function is used in gdbtk sources and thus can not be made static. */
111 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
112 struct symtab_and_line,
113 enum bptype);
114
115 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
116
117 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
118 CORE_ADDR bpaddr,
119 enum bptype bptype);
120
121 static void describe_other_breakpoints (struct gdbarch *,
122 struct program_space *, CORE_ADDR,
123 struct obj_section *, int);
124
125 static int breakpoint_address_match (struct address_space *aspace1,
126 CORE_ADDR addr1,
127 struct address_space *aspace2,
128 CORE_ADDR addr2);
129
130 static void breakpoints_info (char *, int);
131
132 static void breakpoint_1 (int, int);
133
134 static bpstat bpstat_alloc (const struct bp_location *, bpstat);
135
136 static int breakpoint_cond_eval (void *);
137
138 static void cleanup_executing_breakpoints (void *);
139
140 static void commands_command (char *, int);
141
142 static void condition_command (char *, int);
143
144 static int get_number_trailer (char **, int);
145
146 void set_breakpoint_count (int);
147
148 typedef enum
149 {
150 mark_inserted,
151 mark_uninserted
152 }
153 insertion_state_t;
154
155 static int remove_breakpoint (struct bp_location *, insertion_state_t);
156 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
157
158 static enum print_stop_action print_it_typical (bpstat);
159
160 static enum print_stop_action print_bp_stop_message (bpstat bs);
161
162 static int watchpoint_check (void *);
163
164 static void maintenance_info_breakpoints (char *, int);
165
166 static int hw_breakpoint_used_count (void);
167
168 static int hw_watchpoint_used_count (enum bptype, int *);
169
170 static void hbreak_command (char *, int);
171
172 static void thbreak_command (char *, int);
173
174 static void watch_command_1 (char *, int, int);
175
176 static void rwatch_command (char *, int);
177
178 static void awatch_command (char *, int);
179
180 static void do_enable_breakpoint (struct breakpoint *, enum bpdisp);
181
182 static void stop_command (char *arg, int from_tty);
183
184 static void stopin_command (char *arg, int from_tty);
185
186 static void stopat_command (char *arg, int from_tty);
187
188 static char *ep_parse_optional_if_clause (char **arg);
189
190 static char *ep_parse_optional_filename (char **arg);
191
192 static void catch_exception_command_1 (enum exception_event_kind ex_event,
193 char *arg, int tempflag, int from_tty);
194
195 static void tcatch_command (char *arg, int from_tty);
196
197 static void ep_skip_leading_whitespace (char **s);
198
199 static int single_step_breakpoint_inserted_here_p (struct address_space *,
200 CORE_ADDR pc);
201
202 static void free_bp_location (struct bp_location *loc);
203
204 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
205
206 static void update_global_location_list (int);
207
208 static void update_global_location_list_nothrow (int);
209
210 static int is_hardware_watchpoint (struct breakpoint *bpt);
211
212 static void insert_breakpoint_locations (void);
213
214 static int syscall_catchpoint_p (struct breakpoint *b);
215
216 static void tracepoints_info (char *, int);
217
218 static void delete_trace_command (char *, int);
219
220 static void enable_trace_command (char *, int);
221
222 static void disable_trace_command (char *, int);
223
224 static void trace_pass_command (char *, int);
225
226 static void skip_prologue_sal (struct symtab_and_line *sal);
227
228
229 /* Flag indicating that a command has proceeded the inferior past the
230 current breakpoint. */
231
232 static int breakpoint_proceeded;
233
234 static const char *
235 bpdisp_text (enum bpdisp disp)
236 {
237 /* NOTE: the following values are a part of MI protocol and represent
238 values of 'disp' field returned when inferior stops at a breakpoint. */
239 static char *bpdisps[] = {"del", "dstp", "dis", "keep"};
240 return bpdisps[(int) disp];
241 }
242
243 /* Prototypes for exported functions. */
244 /* If FALSE, gdb will not use hardware support for watchpoints, even
245 if such is available. */
246 static int can_use_hw_watchpoints;
247
248 static void
249 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
250 struct cmd_list_element *c,
251 const char *value)
252 {
253 fprintf_filtered (file, _("\
254 Debugger's willingness to use watchpoint hardware is %s.\n"),
255 value);
256 }
257
258 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
259 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
260 for unrecognized breakpoint locations.
261 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
262 static enum auto_boolean pending_break_support;
263 static void
264 show_pending_break_support (struct ui_file *file, int from_tty,
265 struct cmd_list_element *c,
266 const char *value)
267 {
268 fprintf_filtered (file, _("\
269 Debugger's behavior regarding pending breakpoints is %s.\n"),
270 value);
271 }
272
273 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
274 set with "break" but falling in read-only memory.
275 If 0, gdb will warn about such breakpoints, but won't automatically
276 use hardware breakpoints. */
277 static int automatic_hardware_breakpoints;
278 static void
279 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
280 struct cmd_list_element *c,
281 const char *value)
282 {
283 fprintf_filtered (file, _("\
284 Automatic usage of hardware breakpoints is %s.\n"),
285 value);
286 }
287
288 /* If on, gdb will keep breakpoints inserted even as inferior is
289 stopped, and immediately insert any new breakpoints. If off, gdb
290 will insert breakpoints into inferior only when resuming it, and
291 will remove breakpoints upon stop. If auto, GDB will behave as ON
292 if in non-stop mode, and as OFF if all-stop mode.*/
293
294 static const char always_inserted_auto[] = "auto";
295 static const char always_inserted_on[] = "on";
296 static const char always_inserted_off[] = "off";
297 static const char *always_inserted_enums[] = {
298 always_inserted_auto,
299 always_inserted_off,
300 always_inserted_on,
301 NULL
302 };
303 static const char *always_inserted_mode = always_inserted_auto;
304 static void
305 show_always_inserted_mode (struct ui_file *file, int from_tty,
306 struct cmd_list_element *c, const char *value)
307 {
308 if (always_inserted_mode == always_inserted_auto)
309 fprintf_filtered (file, _("\
310 Always inserted breakpoint mode is %s (currently %s).\n"),
311 value,
312 breakpoints_always_inserted_mode () ? "on" : "off");
313 else
314 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"), value);
315 }
316
317 int
318 breakpoints_always_inserted_mode (void)
319 {
320 return (always_inserted_mode == always_inserted_on
321 || (always_inserted_mode == always_inserted_auto && non_stop));
322 }
323
324 void _initialize_breakpoint (void);
325
326 /* Are we executing breakpoint commands? */
327 static int executing_breakpoint_commands;
328
329 /* Are overlay event breakpoints enabled? */
330 static int overlay_events_enabled;
331
332 /* Walk the following statement or block through all breakpoints.
333 ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
334 breakpoint. */
335
336 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
337
338 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
339 for (B = breakpoint_chain; \
340 B ? (TMP=B->next, 1): 0; \
341 B = TMP)
342
343 /* Similar iterator for the low-level breakpoints. SAFE variant is not
344 provided so update_global_location_list must not be called while executing
345 the block of ALL_BP_LOCATIONS. */
346
347 #define ALL_BP_LOCATIONS(B,BP_TMP) \
348 for (BP_TMP = bp_location; \
349 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
350 BP_TMP++)
351
352 /* Iterator for tracepoints only. */
353
354 #define ALL_TRACEPOINTS(B) \
355 for (B = breakpoint_chain; B; B = B->next) \
356 if ((B)->type == bp_tracepoint)
357
358 /* Chains of all breakpoints defined. */
359
360 struct breakpoint *breakpoint_chain;
361
362 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
363
364 static struct bp_location **bp_location;
365
366 /* Number of elements of BP_LOCATION. */
367
368 static unsigned bp_location_count;
369
370 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and ADDRESS
371 for the current elements of BP_LOCATION which get a valid result from
372 bp_location_has_shadow. You can use it for roughly limiting the subrange of
373 BP_LOCATION to scan for shadow bytes for an address you need to read. */
374
375 static CORE_ADDR bp_location_placed_address_before_address_max;
376
377 /* Maximum offset plus alignment between
378 bp_target_info.PLACED_ADDRESS + bp_target_info.SHADOW_LEN and ADDRESS for
379 the current elements of BP_LOCATION which get a valid result from
380 bp_location_has_shadow. You can use it for roughly limiting the subrange of
381 BP_LOCATION to scan for shadow bytes for an address you need to read. */
382
383 static CORE_ADDR bp_location_shadow_len_after_address_max;
384
385 /* The locations that no longer correspond to any breakpoint,
386 unlinked from bp_location array, but for which a hit
387 may still be reported by a target. */
388 VEC(bp_location_p) *moribund_locations = NULL;
389
390 /* Number of last breakpoint made. */
391
392 int breakpoint_count;
393
394 /* Number of last tracepoint made. */
395
396 int tracepoint_count;
397
398 /* Return whether a breakpoint is an active enabled breakpoint. */
399 static int
400 breakpoint_enabled (struct breakpoint *b)
401 {
402 return (b->enable_state == bp_enabled);
403 }
404
405 /* Set breakpoint count to NUM. */
406
407 void
408 set_breakpoint_count (int num)
409 {
410 breakpoint_count = num;
411 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
412 }
413
414 /* Used in run_command to zero the hit count when a new run starts. */
415
416 void
417 clear_breakpoint_hit_counts (void)
418 {
419 struct breakpoint *b;
420
421 ALL_BREAKPOINTS (b)
422 b->hit_count = 0;
423 }
424
425 /* Default address, symtab and line to put a breakpoint at
426 for "break" command with no arg.
427 if default_breakpoint_valid is zero, the other three are
428 not valid, and "break" with no arg is an error.
429
430 This set by print_stack_frame, which calls set_default_breakpoint. */
431
432 int default_breakpoint_valid;
433 CORE_ADDR default_breakpoint_address;
434 struct symtab *default_breakpoint_symtab;
435 int default_breakpoint_line;
436 struct program_space *default_breakpoint_pspace;
437
438 \f
439 /* *PP is a string denoting a breakpoint. Get the number of the breakpoint.
440 Advance *PP after the string and any trailing whitespace.
441
442 Currently the string can either be a number or "$" followed by the name
443 of a convenience variable. Making it an expression wouldn't work well
444 for map_breakpoint_numbers (e.g. "4 + 5 + 6").
445
446 If the string is a NULL pointer, that denotes the last breakpoint.
447
448 TRAILER is a character which can be found after the number; most
449 commonly this is `-'. If you don't want a trailer, use \0. */
450 static int
451 get_number_trailer (char **pp, int trailer)
452 {
453 int retval = 0; /* default */
454 char *p = *pp;
455
456 if (p == NULL)
457 /* Empty line means refer to the last breakpoint. */
458 return breakpoint_count;
459 else if (*p == '$')
460 {
461 /* Make a copy of the name, so we can null-terminate it
462 to pass to lookup_internalvar(). */
463 char *varname;
464 char *start = ++p;
465 LONGEST val;
466
467 while (isalnum (*p) || *p == '_')
468 p++;
469 varname = (char *) alloca (p - start + 1);
470 strncpy (varname, start, p - start);
471 varname[p - start] = '\0';
472 if (get_internalvar_integer (lookup_internalvar (varname), &val))
473 retval = (int) val;
474 else
475 {
476 printf_filtered (_("Convenience variable must have integer value.\n"));
477 retval = 0;
478 }
479 }
480 else
481 {
482 if (*p == '-')
483 ++p;
484 while (*p >= '0' && *p <= '9')
485 ++p;
486 if (p == *pp)
487 /* There is no number here. (e.g. "cond a == b"). */
488 {
489 /* Skip non-numeric token */
490 while (*p && !isspace((int) *p))
491 ++p;
492 /* Return zero, which caller must interpret as error. */
493 retval = 0;
494 }
495 else
496 retval = atoi (*pp);
497 }
498 if (!(isspace (*p) || *p == '\0' || *p == trailer))
499 {
500 /* Trailing junk: return 0 and let caller print error msg. */
501 while (!(isspace (*p) || *p == '\0' || *p == trailer))
502 ++p;
503 retval = 0;
504 }
505 while (isspace (*p))
506 p++;
507 *pp = p;
508 return retval;
509 }
510
511
512 /* Like get_number_trailer, but don't allow a trailer. */
513 int
514 get_number (char **pp)
515 {
516 return get_number_trailer (pp, '\0');
517 }
518
519 /* Parse a number or a range.
520 * A number will be of the form handled by get_number.
521 * A range will be of the form <number1> - <number2>, and
522 * will represent all the integers between number1 and number2,
523 * inclusive.
524 *
525 * While processing a range, this fuction is called iteratively;
526 * At each call it will return the next value in the range.
527 *
528 * At the beginning of parsing a range, the char pointer PP will
529 * be advanced past <number1> and left pointing at the '-' token.
530 * Subsequent calls will not advance the pointer until the range
531 * is completed. The call that completes the range will advance
532 * pointer PP past <number2>.
533 */
534
535 int
536 get_number_or_range (char **pp)
537 {
538 static int last_retval, end_value;
539 static char *end_ptr;
540 static int in_range = 0;
541
542 if (**pp != '-')
543 {
544 /* Default case: pp is pointing either to a solo number,
545 or to the first number of a range. */
546 last_retval = get_number_trailer (pp, '-');
547 if (**pp == '-')
548 {
549 char **temp;
550
551 /* This is the start of a range (<number1> - <number2>).
552 Skip the '-', parse and remember the second number,
553 and also remember the end of the final token. */
554
555 temp = &end_ptr;
556 end_ptr = *pp + 1;
557 while (isspace ((int) *end_ptr))
558 end_ptr++; /* skip white space */
559 end_value = get_number (temp);
560 if (end_value < last_retval)
561 {
562 error (_("inverted range"));
563 }
564 else if (end_value == last_retval)
565 {
566 /* degenerate range (number1 == number2). Advance the
567 token pointer so that the range will be treated as a
568 single number. */
569 *pp = end_ptr;
570 }
571 else
572 in_range = 1;
573 }
574 }
575 else if (! in_range)
576 error (_("negative value"));
577 else
578 {
579 /* pp points to the '-' that betokens a range. All
580 number-parsing has already been done. Return the next
581 integer value (one greater than the saved previous value).
582 Do not advance the token pointer 'pp' until the end of range
583 is reached. */
584
585 if (++last_retval == end_value)
586 {
587 /* End of range reached; advance token pointer. */
588 *pp = end_ptr;
589 in_range = 0;
590 }
591 }
592 return last_retval;
593 }
594
595 /* Return the breakpoint with the specified number, or NULL
596 if the number does not refer to an existing breakpoint. */
597
598 struct breakpoint *
599 get_breakpoint (int num)
600 {
601 struct breakpoint *b;
602
603 ALL_BREAKPOINTS (b)
604 if (b->number == num)
605 return b;
606
607 return NULL;
608 }
609
610 \f
611 /* condition N EXP -- set break condition of breakpoint N to EXP. */
612
613 static void
614 condition_command (char *arg, int from_tty)
615 {
616 struct breakpoint *b;
617 char *p;
618 int bnum;
619
620 if (arg == 0)
621 error_no_arg (_("breakpoint number"));
622
623 p = arg;
624 bnum = get_number (&p);
625 if (bnum == 0)
626 error (_("Bad breakpoint argument: '%s'"), arg);
627
628 ALL_BREAKPOINTS (b)
629 if (b->number == bnum)
630 {
631 struct bp_location *loc = b->loc;
632 for (; loc; loc = loc->next)
633 {
634 if (loc->cond)
635 {
636 xfree (loc->cond);
637 loc->cond = 0;
638 }
639 }
640 if (b->cond_string != NULL)
641 xfree (b->cond_string);
642
643 if (*p == 0)
644 {
645 b->cond_string = NULL;
646 if (from_tty)
647 printf_filtered (_("Breakpoint %d now unconditional.\n"), bnum);
648 }
649 else
650 {
651 arg = p;
652 /* I don't know if it matters whether this is the string the user
653 typed in or the decompiled expression. */
654 b->cond_string = xstrdup (arg);
655 b->condition_not_parsed = 0;
656 for (loc = b->loc; loc; loc = loc->next)
657 {
658 arg = p;
659 loc->cond =
660 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
661 if (*arg)
662 error (_("Junk at end of expression"));
663 }
664 }
665 breakpoints_changed ();
666 observer_notify_breakpoint_modified (b->number);
667 return;
668 }
669
670 error (_("No breakpoint number %d."), bnum);
671 }
672
673 /* Set the command list of B to COMMANDS. */
674
675 void
676 breakpoint_set_commands (struct breakpoint *b, struct command_line *commands)
677 {
678 free_command_lines (&b->commands);
679 b->commands = commands;
680 breakpoints_changed ();
681 observer_notify_breakpoint_modified (b->number);
682 }
683
684 static void
685 commands_command (char *arg, int from_tty)
686 {
687 struct breakpoint *b;
688 char *p;
689 int bnum;
690 struct command_line *l;
691
692 /* If we allowed this, we would have problems with when to
693 free the storage, if we change the commands currently
694 being read from. */
695
696 if (executing_breakpoint_commands)
697 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
698
699 p = arg;
700 bnum = get_number (&p);
701
702 if (p && *p)
703 error (_("Unexpected extra arguments following breakpoint number."));
704
705 ALL_BREAKPOINTS (b)
706 if (b->number == bnum)
707 {
708 char *tmpbuf = xstrprintf ("Type commands for when breakpoint %d is hit, one per line.",
709 bnum);
710 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
711 l = read_command_lines (tmpbuf, from_tty, 1);
712 do_cleanups (cleanups);
713 breakpoint_set_commands (b, l);
714 return;
715 }
716 error (_("No breakpoint number %d."), bnum);
717 }
718
719 /* Like commands_command, but instead of reading the commands from
720 input stream, takes them from an already parsed command structure.
721
722 This is used by cli-script.c to DTRT with breakpoint commands
723 that are part of if and while bodies. */
724 enum command_control_type
725 commands_from_control_command (char *arg, struct command_line *cmd)
726 {
727 struct breakpoint *b;
728 char *p;
729 int bnum;
730
731 /* If we allowed this, we would have problems with when to
732 free the storage, if we change the commands currently
733 being read from. */
734
735 if (executing_breakpoint_commands)
736 error (_("Can't use the \"commands\" command among a breakpoint's commands."));
737
738 /* An empty string for the breakpoint number means the last
739 breakpoint, but get_number expects a NULL pointer. */
740 if (arg && !*arg)
741 p = NULL;
742 else
743 p = arg;
744 bnum = get_number (&p);
745
746 if (p && *p)
747 error (_("Unexpected extra arguments following breakpoint number."));
748
749 ALL_BREAKPOINTS (b)
750 if (b->number == bnum)
751 {
752 free_command_lines (&b->commands);
753 if (cmd->body_count != 1)
754 error (_("Invalid \"commands\" block structure."));
755 /* We need to copy the commands because if/while will free the
756 list after it finishes execution. */
757 b->commands = copy_command_lines (cmd->body_list[0]);
758 breakpoints_changed ();
759 observer_notify_breakpoint_modified (b->number);
760 return simple_control;
761 }
762 error (_("No breakpoint number %d."), bnum);
763 }
764
765 /* Return non-zero if BL->TARGET_INFO contains valid information. */
766
767 static int
768 bp_location_has_shadow (struct bp_location *bl)
769 {
770 if (bl->loc_type != bp_loc_software_breakpoint)
771 return 0;
772 if (!bl->inserted)
773 return 0;
774 if (bl->target_info.shadow_len == 0)
775 /* bp isn't valid, or doesn't shadow memory. */
776 return 0;
777 return 1;
778 }
779
780 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
781 by replacing any memory breakpoints with their shadowed contents.
782
783 The range of shadowed area by each bp_location is:
784 b->address - bp_location_placed_address_before_address_max
785 up to b->address + bp_location_shadow_len_after_address_max
786 The range we were requested to resolve shadows for is:
787 memaddr ... memaddr + len
788 Thus the safe cutoff boundaries for performance optimization are
789 memaddr + len <= b->address - bp_location_placed_address_before_address_max
790 and:
791 b->address + bp_location_shadow_len_after_address_max <= memaddr */
792
793 void
794 breakpoint_restore_shadows (gdb_byte *buf, ULONGEST memaddr, LONGEST len)
795 {
796 /* Left boundary, right boundary and median element of our binary search. */
797 unsigned bc_l, bc_r, bc;
798
799 /* Find BC_L which is a leftmost element which may affect BUF content. It is
800 safe to report lower value but a failure to report higher one. */
801
802 bc_l = 0;
803 bc_r = bp_location_count;
804 while (bc_l + 1 < bc_r)
805 {
806 struct bp_location *b;
807
808 bc = (bc_l + bc_r) / 2;
809 b = bp_location[bc];
810
811 /* Check first B->ADDRESS will not overflow due to the added constant.
812 Then advance the left boundary only if we are sure the BC element can
813 in no way affect the BUF content (MEMADDR to MEMADDR + LEN range).
814
815 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety offset so that
816 we cannot miss a breakpoint with its shadow range tail still reaching
817 MEMADDR. */
818
819 if (b->address + bp_location_shadow_len_after_address_max >= b->address
820 && b->address + bp_location_shadow_len_after_address_max <= memaddr)
821 bc_l = bc;
822 else
823 bc_r = bc;
824 }
825
826 /* Now do full processing of the found relevant range of elements. */
827
828 for (bc = bc_l; bc < bp_location_count; bc++)
829 {
830 struct bp_location *b = bp_location[bc];
831 CORE_ADDR bp_addr = 0;
832 int bp_size = 0;
833 int bptoffset = 0;
834
835 if (b->owner->type == bp_none)
836 warning (_("reading through apparently deleted breakpoint #%d?"),
837 b->owner->number);
838
839 /* Performance optimization: any futher element can no longer affect BUF
840 content. */
841
842 if (b->address >= bp_location_placed_address_before_address_max
843 && memaddr + len <= b->address
844 - bp_location_placed_address_before_address_max)
845 break;
846
847 if (!bp_location_has_shadow (b))
848 continue;
849 if (!breakpoint_address_match (b->target_info.placed_address_space, 0,
850 current_program_space->aspace, 0))
851 continue;
852
853 /* Addresses and length of the part of the breakpoint that
854 we need to copy. */
855 bp_addr = b->target_info.placed_address;
856 bp_size = b->target_info.shadow_len;
857
858 if (bp_addr + bp_size <= memaddr)
859 /* The breakpoint is entirely before the chunk of memory we
860 are reading. */
861 continue;
862
863 if (bp_addr >= memaddr + len)
864 /* The breakpoint is entirely after the chunk of memory we are
865 reading. */
866 continue;
867
868 /* Offset within shadow_contents. */
869 if (bp_addr < memaddr)
870 {
871 /* Only copy the second part of the breakpoint. */
872 bp_size -= memaddr - bp_addr;
873 bptoffset = memaddr - bp_addr;
874 bp_addr = memaddr;
875 }
876
877 if (bp_addr + bp_size > memaddr + len)
878 {
879 /* Only copy the first part of the breakpoint. */
880 bp_size -= (bp_addr + bp_size) - (memaddr + len);
881 }
882
883 memcpy (buf + bp_addr - memaddr,
884 b->target_info.shadow_contents + bptoffset, bp_size);
885 }
886 }
887 \f
888
889 /* A wrapper function for inserting catchpoints. */
890 static void
891 insert_catchpoint (struct ui_out *uo, void *args)
892 {
893 struct breakpoint *b = (struct breakpoint *) args;
894 int val = -1;
895
896 gdb_assert (b->type == bp_catchpoint);
897 gdb_assert (b->ops != NULL && b->ops->insert != NULL);
898
899 b->ops->insert (b);
900 }
901
902 static int
903 is_hardware_watchpoint (struct breakpoint *bpt)
904 {
905 return (bpt->type == bp_hardware_watchpoint
906 || bpt->type == bp_read_watchpoint
907 || bpt->type == bp_access_watchpoint);
908 }
909
910 /* Find the current value of a watchpoint on EXP. Return the value in
911 *VALP and *RESULTP and the chain of intermediate and final values
912 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
913 not need them.
914
915 If a memory error occurs while evaluating the expression, *RESULTP will
916 be set to NULL. *RESULTP may be a lazy value, if the result could
917 not be read from memory. It is used to determine whether a value
918 is user-specified (we should watch the whole value) or intermediate
919 (we should watch only the bit used to locate the final value).
920
921 If the final value, or any intermediate value, could not be read
922 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
923 set to any referenced values. *VALP will never be a lazy value.
924 This is the value which we store in struct breakpoint.
925
926 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
927 value chain. The caller must free the values individually. If
928 VAL_CHAIN is NULL, all generated values will be left on the value
929 chain. */
930
931 static void
932 fetch_watchpoint_value (struct expression *exp, struct value **valp,
933 struct value **resultp, struct value **val_chain)
934 {
935 struct value *mark, *new_mark, *result;
936 volatile struct gdb_exception ex;
937
938 *valp = NULL;
939 if (resultp)
940 *resultp = NULL;
941 if (val_chain)
942 *val_chain = NULL;
943
944 /* Evaluate the expression. */
945 mark = value_mark ();
946 result = NULL;
947
948 TRY_CATCH (ex, RETURN_MASK_ALL)
949 {
950 result = evaluate_expression (exp);
951 }
952 if (ex.reason < 0)
953 {
954 /* Ignore memory errors, we want watchpoints pointing at
955 inaccessible memory to still be created; otherwise, throw the
956 error to some higher catcher. */
957 switch (ex.error)
958 {
959 case MEMORY_ERROR:
960 break;
961 default:
962 throw_exception (ex);
963 break;
964 }
965 }
966
967 new_mark = value_mark ();
968 if (mark == new_mark)
969 return;
970 if (resultp)
971 *resultp = result;
972
973 /* Make sure it's not lazy, so that after the target stops again we
974 have a non-lazy previous value to compare with. */
975 if (result != NULL
976 && (!value_lazy (result) || gdb_value_fetch_lazy (result)))
977 *valp = result;
978
979 if (val_chain)
980 {
981 /* Return the chain of intermediate values. We use this to
982 decide which addresses to watch. */
983 *val_chain = new_mark;
984 value_release_to_mark (mark);
985 }
986 }
987
988 /* Assuming that B is a watchpoint:
989 - Reparse watchpoint expression, if REPARSE is non-zero
990 - Evaluate expression and store the result in B->val
991 - Evaluate the condition if there is one, and store the result
992 in b->loc->cond.
993 - Update the list of values that must be watched in B->loc.
994
995 If the watchpoint disposition is disp_del_at_next_stop, then do nothing.
996 If this is local watchpoint that is out of scope, delete it. */
997 static void
998 update_watchpoint (struct breakpoint *b, int reparse)
999 {
1000 int within_current_scope;
1001 struct frame_id saved_frame_id;
1002 struct bp_location *loc;
1003 int frame_saved;
1004 bpstat bs;
1005 struct program_space *frame_pspace;
1006
1007 /* We don't free locations. They are stored in bp_location array and
1008 update_global_locations will eventually delete them and remove
1009 breakpoints if needed. */
1010 b->loc = NULL;
1011
1012 if (b->disposition == disp_del_at_next_stop)
1013 return;
1014
1015 frame_saved = 0;
1016
1017 /* Determine if the watchpoint is within scope. */
1018 if (b->exp_valid_block == NULL)
1019 within_current_scope = 1;
1020 else
1021 {
1022 struct frame_info *fi;
1023
1024 /* Save the current frame's ID so we can restore it after
1025 evaluating the watchpoint expression on its own frame. */
1026 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1027 took a frame parameter, so that we didn't have to change the
1028 selected frame. */
1029 frame_saved = 1;
1030 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1031
1032 fi = frame_find_by_id (b->watchpoint_frame);
1033 within_current_scope = (fi != NULL);
1034 if (within_current_scope)
1035 select_frame (fi);
1036 }
1037
1038 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1039
1040 if (within_current_scope && reparse)
1041 {
1042 char *s;
1043 if (b->exp)
1044 {
1045 xfree (b->exp);
1046 b->exp = NULL;
1047 }
1048 s = b->exp_string;
1049 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1050 /* If the meaning of expression itself changed, the old value is
1051 no longer relevant. We don't want to report a watchpoint hit
1052 to the user when the old value and the new value may actually
1053 be completely different objects. */
1054 value_free (b->val);
1055 b->val = NULL;
1056 b->val_valid = 0;
1057 }
1058
1059 /* If we failed to parse the expression, for example because
1060 it refers to a global variable in a not-yet-loaded shared library,
1061 don't try to insert watchpoint. We don't automatically delete
1062 such watchpoint, though, since failure to parse expression
1063 is different from out-of-scope watchpoint. */
1064 if (within_current_scope && b->exp)
1065 {
1066 struct value *val_chain, *v, *result, *next;
1067
1068 fetch_watchpoint_value (b->exp, &v, &result, &val_chain);
1069
1070 /* Avoid setting b->val if it's already set. The meaning of
1071 b->val is 'the last value' user saw, and we should update
1072 it only if we reported that last value to user. As it
1073 happens, the code that reports it updates b->val directly. */
1074 if (!b->val_valid)
1075 {
1076 b->val = v;
1077 b->val_valid = 1;
1078 }
1079
1080 /* Change the type of breakpoint between hardware assisted or an
1081 ordinary watchpoint depending on the hardware support and free
1082 hardware slots. REPARSE is set when the inferior is started. */
1083 if ((b->type == bp_watchpoint || b->type == bp_hardware_watchpoint)
1084 && reparse)
1085 {
1086 int i, mem_cnt, other_type_used;
1087
1088 i = hw_watchpoint_used_count (bp_hardware_watchpoint,
1089 &other_type_used);
1090 mem_cnt = can_use_hardware_watchpoint (val_chain);
1091
1092 if (!mem_cnt)
1093 b->type = bp_watchpoint;
1094 else
1095 {
1096 int target_resources_ok = target_can_use_hardware_watchpoint
1097 (bp_hardware_watchpoint, i + mem_cnt, other_type_used);
1098 if (target_resources_ok <= 0)
1099 b->type = bp_watchpoint;
1100 else
1101 b->type = bp_hardware_watchpoint;
1102 }
1103 }
1104
1105 /* Look at each value on the value chain. */
1106 for (v = val_chain; v; v = next)
1107 {
1108 /* If it's a memory location, and GDB actually needed
1109 its contents to evaluate the expression, then we
1110 must watch it. If the first value returned is
1111 still lazy, that means an error occurred reading it;
1112 watch it anyway in case it becomes readable. */
1113 if (VALUE_LVAL (v) == lval_memory
1114 && (v == val_chain || ! value_lazy (v)))
1115 {
1116 struct type *vtype = check_typedef (value_type (v));
1117
1118 /* We only watch structs and arrays if user asked
1119 for it explicitly, never if they just happen to
1120 appear in the middle of some value chain. */
1121 if (v == result
1122 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1123 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1124 {
1125 CORE_ADDR addr;
1126 int len, type;
1127 struct bp_location *loc, **tmp;
1128
1129 addr = value_address (v);
1130 len = TYPE_LENGTH (value_type (v));
1131 type = hw_write;
1132 if (b->type == bp_read_watchpoint)
1133 type = hw_read;
1134 else if (b->type == bp_access_watchpoint)
1135 type = hw_access;
1136
1137 loc = allocate_bp_location (b);
1138 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1139 ;
1140 *tmp = loc;
1141 loc->gdbarch = get_type_arch (value_type (v));
1142
1143 loc->pspace = frame_pspace;
1144 loc->address = addr;
1145 loc->length = len;
1146 loc->watchpoint_type = type;
1147 }
1148 }
1149
1150 next = value_next (v);
1151 if (v != b->val)
1152 value_free (v);
1153 }
1154
1155 /* We just regenerated the list of breakpoint locations.
1156 The new location does not have its condition field set to anything
1157 and therefore, we must always reparse the cond_string, independently
1158 of the value of the reparse flag. */
1159 if (b->cond_string != NULL)
1160 {
1161 char *s = b->cond_string;
1162 b->loc->cond = parse_exp_1 (&s, b->exp_valid_block, 0);
1163 }
1164 }
1165 else if (!within_current_scope)
1166 {
1167 printf_filtered (_("\
1168 Watchpoint %d deleted because the program has left the block \n\
1169 in which its expression is valid.\n"),
1170 b->number);
1171 if (b->related_breakpoint)
1172 b->related_breakpoint->disposition = disp_del_at_next_stop;
1173 b->disposition = disp_del_at_next_stop;
1174 }
1175
1176 /* Restore the selected frame. */
1177 if (frame_saved)
1178 select_frame (frame_find_by_id (saved_frame_id));
1179 }
1180
1181
1182 /* Returns 1 iff breakpoint location should be
1183 inserted in the inferior. */
1184 static int
1185 should_be_inserted (struct bp_location *bpt)
1186 {
1187 if (!breakpoint_enabled (bpt->owner))
1188 return 0;
1189
1190 if (bpt->owner->disposition == disp_del_at_next_stop)
1191 return 0;
1192
1193 if (!bpt->enabled || bpt->shlib_disabled || bpt->duplicate)
1194 return 0;
1195
1196 /* This is set for example, when we're attached to the parent of a
1197 vfork, and have detached from the child. The child is running
1198 free, and we expect it to do an exec or exit, at which point the
1199 OS makes the parent schedulable again (and the target reports
1200 that the vfork is done). Until the child is done with the shared
1201 memory region, do not insert breakpoints in the parent, otherwise
1202 the child could still trip on the parent's breakpoints. Since
1203 the parent is blocked anyway, it won't miss any breakpoint. */
1204 if (bpt->pspace->breakpoints_not_allowed)
1205 return 0;
1206
1207 /* Tracepoints are inserted by the target at a time of its choosing,
1208 not by us. */
1209 if (bpt->owner->type == bp_tracepoint)
1210 return 0;
1211
1212 return 1;
1213 }
1214
1215 /* Insert a low-level "breakpoint" of some type. BPT is the breakpoint.
1216 Any error messages are printed to TMP_ERROR_STREAM; and DISABLED_BREAKS,
1217 and HW_BREAKPOINT_ERROR are used to report problems.
1218
1219 NOTE drow/2003-09-09: This routine could be broken down to an object-style
1220 method for each breakpoint or catchpoint type. */
1221 static int
1222 insert_bp_location (struct bp_location *bpt,
1223 struct ui_file *tmp_error_stream,
1224 int *disabled_breaks,
1225 int *hw_breakpoint_error)
1226 {
1227 int val = 0;
1228
1229 if (!should_be_inserted (bpt) || bpt->inserted)
1230 return 0;
1231
1232 /* Initialize the target-specific information. */
1233 memset (&bpt->target_info, 0, sizeof (bpt->target_info));
1234 bpt->target_info.placed_address = bpt->address;
1235 bpt->target_info.placed_address_space = bpt->pspace->aspace;
1236
1237 if (bpt->loc_type == bp_loc_software_breakpoint
1238 || bpt->loc_type == bp_loc_hardware_breakpoint)
1239 {
1240 if (bpt->owner->type != bp_hardware_breakpoint)
1241 {
1242 /* If the explicitly specified breakpoint type
1243 is not hardware breakpoint, check the memory map to see
1244 if the breakpoint address is in read only memory or not.
1245 Two important cases are:
1246 - location type is not hardware breakpoint, memory
1247 is readonly. We change the type of the location to
1248 hardware breakpoint.
1249 - location type is hardware breakpoint, memory is read-write.
1250 This means we've previously made the location hardware one, but
1251 then the memory map changed, so we undo.
1252
1253 When breakpoints are removed, remove_breakpoints will
1254 use location types we've just set here, the only possible
1255 problem is that memory map has changed during running program,
1256 but it's not going to work anyway with current gdb. */
1257 struct mem_region *mr
1258 = lookup_mem_region (bpt->target_info.placed_address);
1259
1260 if (mr)
1261 {
1262 if (automatic_hardware_breakpoints)
1263 {
1264 int changed = 0;
1265 enum bp_loc_type new_type;
1266
1267 if (mr->attrib.mode != MEM_RW)
1268 new_type = bp_loc_hardware_breakpoint;
1269 else
1270 new_type = bp_loc_software_breakpoint;
1271
1272 if (new_type != bpt->loc_type)
1273 {
1274 static int said = 0;
1275 bpt->loc_type = new_type;
1276 if (!said)
1277 {
1278 fprintf_filtered (gdb_stdout, _("\
1279 Note: automatically using hardware breakpoints for read-only addresses.\n"));
1280 said = 1;
1281 }
1282 }
1283 }
1284 else if (bpt->loc_type == bp_loc_software_breakpoint
1285 && mr->attrib.mode != MEM_RW)
1286 warning (_("cannot set software breakpoint at readonly address %s"),
1287 paddress (bpt->gdbarch, bpt->address));
1288 }
1289 }
1290
1291 /* First check to see if we have to handle an overlay. */
1292 if (overlay_debugging == ovly_off
1293 || bpt->section == NULL
1294 || !(section_is_overlay (bpt->section)))
1295 {
1296 /* No overlay handling: just set the breakpoint. */
1297
1298 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1299 val = target_insert_hw_breakpoint (bpt->gdbarch,
1300 &bpt->target_info);
1301 else
1302 val = target_insert_breakpoint (bpt->gdbarch,
1303 &bpt->target_info);
1304 }
1305 else
1306 {
1307 /* This breakpoint is in an overlay section.
1308 Shall we set a breakpoint at the LMA? */
1309 if (!overlay_events_enabled)
1310 {
1311 /* Yes -- overlay event support is not active,
1312 so we must try to set a breakpoint at the LMA.
1313 This will not work for a hardware breakpoint. */
1314 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1315 warning (_("hardware breakpoint %d not supported in overlay!"),
1316 bpt->owner->number);
1317 else
1318 {
1319 CORE_ADDR addr = overlay_unmapped_address (bpt->address,
1320 bpt->section);
1321 /* Set a software (trap) breakpoint at the LMA. */
1322 bpt->overlay_target_info = bpt->target_info;
1323 bpt->overlay_target_info.placed_address = addr;
1324 val = target_insert_breakpoint (bpt->gdbarch,
1325 &bpt->overlay_target_info);
1326 if (val != 0)
1327 fprintf_unfiltered (tmp_error_stream,
1328 "Overlay breakpoint %d failed: in ROM?\n",
1329 bpt->owner->number);
1330 }
1331 }
1332 /* Shall we set a breakpoint at the VMA? */
1333 if (section_is_mapped (bpt->section))
1334 {
1335 /* Yes. This overlay section is mapped into memory. */
1336 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1337 val = target_insert_hw_breakpoint (bpt->gdbarch,
1338 &bpt->target_info);
1339 else
1340 val = target_insert_breakpoint (bpt->gdbarch,
1341 &bpt->target_info);
1342 }
1343 else
1344 {
1345 /* No. This breakpoint will not be inserted.
1346 No error, but do not mark the bp as 'inserted'. */
1347 return 0;
1348 }
1349 }
1350
1351 if (val)
1352 {
1353 /* Can't set the breakpoint. */
1354 if (solib_name_from_address (bpt->pspace, bpt->address))
1355 {
1356 /* See also: disable_breakpoints_in_shlibs. */
1357 val = 0;
1358 bpt->shlib_disabled = 1;
1359 if (!*disabled_breaks)
1360 {
1361 fprintf_unfiltered (tmp_error_stream,
1362 "Cannot insert breakpoint %d.\n",
1363 bpt->owner->number);
1364 fprintf_unfiltered (tmp_error_stream,
1365 "Temporarily disabling shared library breakpoints:\n");
1366 }
1367 *disabled_breaks = 1;
1368 fprintf_unfiltered (tmp_error_stream,
1369 "breakpoint #%d\n", bpt->owner->number);
1370 }
1371 else
1372 {
1373 if (bpt->loc_type == bp_loc_hardware_breakpoint)
1374 {
1375 *hw_breakpoint_error = 1;
1376 fprintf_unfiltered (tmp_error_stream,
1377 "Cannot insert hardware breakpoint %d.\n",
1378 bpt->owner->number);
1379 }
1380 else
1381 {
1382 fprintf_unfiltered (tmp_error_stream,
1383 "Cannot insert breakpoint %d.\n",
1384 bpt->owner->number);
1385 fprintf_filtered (tmp_error_stream,
1386 "Error accessing memory address ");
1387 fputs_filtered (paddress (bpt->gdbarch, bpt->address),
1388 tmp_error_stream);
1389 fprintf_filtered (tmp_error_stream, ": %s.\n",
1390 safe_strerror (val));
1391 }
1392
1393 }
1394 }
1395 else
1396 bpt->inserted = 1;
1397
1398 return val;
1399 }
1400
1401 else if (bpt->loc_type == bp_loc_hardware_watchpoint
1402 /* NOTE drow/2003-09-08: This state only exists for removing
1403 watchpoints. It's not clear that it's necessary... */
1404 && bpt->owner->disposition != disp_del_at_next_stop)
1405 {
1406 val = target_insert_watchpoint (bpt->address,
1407 bpt->length,
1408 bpt->watchpoint_type);
1409 bpt->inserted = (val != -1);
1410 }
1411
1412 else if (bpt->owner->type == bp_catchpoint)
1413 {
1414 struct gdb_exception e = catch_exception (uiout, insert_catchpoint,
1415 bpt->owner, RETURN_MASK_ERROR);
1416 exception_fprintf (gdb_stderr, e, "warning: inserting catchpoint %d: ",
1417 bpt->owner->number);
1418 if (e.reason < 0)
1419 bpt->owner->enable_state = bp_disabled;
1420 else
1421 bpt->inserted = 1;
1422
1423 /* We've already printed an error message if there was a problem
1424 inserting this catchpoint, and we've disabled the catchpoint,
1425 so just return success. */
1426 return 0;
1427 }
1428
1429 return 0;
1430 }
1431
1432 /* This function is called when program space PSPACE is about to be
1433 deleted. It takes care of updating breakpoints to not reference
1434 PSPACE anymore. */
1435
1436 void
1437 breakpoint_program_space_exit (struct program_space *pspace)
1438 {
1439 struct breakpoint *b, *b_temp;
1440 struct bp_location *loc, **loc_temp;
1441
1442 /* Remove any breakpoint that was set through this program space. */
1443 ALL_BREAKPOINTS_SAFE (b, b_temp)
1444 {
1445 if (b->pspace == pspace)
1446 delete_breakpoint (b);
1447 }
1448
1449 /* Breakpoints set through other program spaces could have locations
1450 bound to PSPACE as well. Remove those. */
1451 ALL_BP_LOCATIONS (loc, loc_temp)
1452 {
1453 struct bp_location *tmp;
1454
1455 if (loc->pspace == pspace)
1456 {
1457 if (loc->owner->loc == loc)
1458 loc->owner->loc = loc->next;
1459 else
1460 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
1461 if (tmp->next == loc)
1462 {
1463 tmp->next = loc->next;
1464 break;
1465 }
1466 }
1467 }
1468
1469 /* Now update the global location list to permanently delete the
1470 removed locations above. */
1471 update_global_location_list (0);
1472 }
1473
1474 /* Make sure all breakpoints are inserted in inferior.
1475 Throws exception on any error.
1476 A breakpoint that is already inserted won't be inserted
1477 again, so calling this function twice is safe. */
1478 void
1479 insert_breakpoints (void)
1480 {
1481 struct breakpoint *bpt;
1482
1483 ALL_BREAKPOINTS (bpt)
1484 if (is_hardware_watchpoint (bpt))
1485 update_watchpoint (bpt, 0 /* don't reparse. */);
1486
1487 update_global_location_list (1);
1488
1489 /* update_global_location_list does not insert breakpoints when
1490 always_inserted_mode is not enabled. Explicitly insert them
1491 now. */
1492 if (!breakpoints_always_inserted_mode ())
1493 insert_breakpoint_locations ();
1494 }
1495
1496 /* insert_breakpoints is used when starting or continuing the program.
1497 remove_breakpoints is used when the program stops.
1498 Both return zero if successful,
1499 or an `errno' value if could not write the inferior. */
1500
1501 static void
1502 insert_breakpoint_locations (void)
1503 {
1504 struct breakpoint *bpt;
1505 struct bp_location *b, **bp_tmp;
1506 int error = 0;
1507 int val = 0;
1508 int disabled_breaks = 0;
1509 int hw_breakpoint_error = 0;
1510
1511 struct ui_file *tmp_error_stream = mem_fileopen ();
1512 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
1513
1514 /* Explicitly mark the warning -- this will only be printed if
1515 there was an error. */
1516 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
1517
1518 save_current_space_and_thread ();
1519
1520 ALL_BP_LOCATIONS (b, bp_tmp)
1521 {
1522 struct thread_info *tp;
1523 CORE_ADDR last_addr;
1524
1525 if (!should_be_inserted (b) || b->inserted)
1526 continue;
1527
1528 /* There is no point inserting thread-specific breakpoints if the
1529 thread no longer exists. */
1530 if (b->owner->thread != -1
1531 && !valid_thread_id (b->owner->thread))
1532 continue;
1533
1534 switch_to_program_space_and_thread (b->pspace);
1535
1536 /* For targets that support global breakpoints, there's no need
1537 to select an inferior to insert breakpoint to. In fact, even
1538 if we aren't attached to any process yet, we should still
1539 insert breakpoints. */
1540 if (!gdbarch_has_global_breakpoints (target_gdbarch)
1541 && ptid_equal (inferior_ptid, null_ptid))
1542 continue;
1543
1544 val = insert_bp_location (b, tmp_error_stream,
1545 &disabled_breaks,
1546 &hw_breakpoint_error);
1547 if (val)
1548 error = val;
1549 }
1550
1551 /* If we failed to insert all locations of a watchpoint,
1552 remove them, as half-inserted watchpoint is of limited use. */
1553 ALL_BREAKPOINTS (bpt)
1554 {
1555 int some_failed = 0;
1556 struct bp_location *loc;
1557
1558 if (!is_hardware_watchpoint (bpt))
1559 continue;
1560
1561 if (!breakpoint_enabled (bpt))
1562 continue;
1563
1564 if (bpt->disposition == disp_del_at_next_stop)
1565 continue;
1566
1567 for (loc = bpt->loc; loc; loc = loc->next)
1568 if (!loc->inserted && should_be_inserted (loc))
1569 {
1570 some_failed = 1;
1571 break;
1572 }
1573 if (some_failed)
1574 {
1575 for (loc = bpt->loc; loc; loc = loc->next)
1576 if (loc->inserted)
1577 remove_breakpoint (loc, mark_uninserted);
1578
1579 hw_breakpoint_error = 1;
1580 fprintf_unfiltered (tmp_error_stream,
1581 "Could not insert hardware watchpoint %d.\n",
1582 bpt->number);
1583 error = -1;
1584 }
1585 }
1586
1587 if (error)
1588 {
1589 /* If a hardware breakpoint or watchpoint was inserted, add a
1590 message about possibly exhausted resources. */
1591 if (hw_breakpoint_error)
1592 {
1593 fprintf_unfiltered (tmp_error_stream,
1594 "Could not insert hardware breakpoints:\n\
1595 You may have requested too many hardware breakpoints/watchpoints.\n");
1596 }
1597 target_terminal_ours_for_output ();
1598 error_stream (tmp_error_stream);
1599 }
1600
1601 do_cleanups (cleanups);
1602 }
1603
1604 int
1605 remove_breakpoints (void)
1606 {
1607 struct bp_location *b, **bp_tmp;
1608 int val = 0;
1609
1610 ALL_BP_LOCATIONS (b, bp_tmp)
1611 {
1612 if (b->inserted)
1613 val |= remove_breakpoint (b, mark_uninserted);
1614 }
1615 return val;
1616 }
1617
1618 /* Remove breakpoints of process PID. */
1619
1620 int
1621 remove_breakpoints_pid (int pid)
1622 {
1623 struct bp_location *b, **b_tmp;
1624 int val;
1625 struct inferior *inf = find_inferior_pid (pid);
1626
1627 ALL_BP_LOCATIONS (b, b_tmp)
1628 {
1629 if (b->pspace != inf->pspace)
1630 continue;
1631
1632 if (b->inserted)
1633 {
1634 val = remove_breakpoint (b, mark_uninserted);
1635 if (val != 0)
1636 return val;
1637 }
1638 }
1639 return 0;
1640 }
1641
1642 int
1643 remove_hw_watchpoints (void)
1644 {
1645 struct bp_location *b, **bp_tmp;
1646 int val = 0;
1647
1648 ALL_BP_LOCATIONS (b, bp_tmp)
1649 {
1650 if (b->inserted && b->loc_type == bp_loc_hardware_watchpoint)
1651 val |= remove_breakpoint (b, mark_uninserted);
1652 }
1653 return val;
1654 }
1655
1656 int
1657 reattach_breakpoints (int pid)
1658 {
1659 struct cleanup *old_chain;
1660 struct bp_location *b, **bp_tmp;
1661 int val;
1662 struct ui_file *tmp_error_stream = mem_fileopen ();
1663 int dummy1 = 0, dummy2 = 0;
1664 struct inferior *inf;
1665 struct thread_info *tp;
1666
1667 tp = any_live_thread_of_process (pid);
1668 if (tp == NULL)
1669 return 1;
1670
1671 inf = find_inferior_pid (pid);
1672 old_chain = save_inferior_ptid ();
1673
1674 inferior_ptid = tp->ptid;
1675
1676 make_cleanup_ui_file_delete (tmp_error_stream);
1677
1678 ALL_BP_LOCATIONS (b, bp_tmp)
1679 {
1680 if (b->pspace != inf->pspace)
1681 continue;
1682
1683 if (b->inserted)
1684 {
1685 b->inserted = 0;
1686 val = insert_bp_location (b, tmp_error_stream,
1687 &dummy1, &dummy2);
1688 if (val != 0)
1689 {
1690 do_cleanups (old_chain);
1691 return val;
1692 }
1693 }
1694 }
1695 do_cleanups (old_chain);
1696 return 0;
1697 }
1698
1699 static int internal_breakpoint_number = -1;
1700
1701 static struct breakpoint *
1702 create_internal_breakpoint (struct gdbarch *gdbarch,
1703 CORE_ADDR address, enum bptype type)
1704 {
1705 struct symtab_and_line sal;
1706 struct breakpoint *b;
1707
1708 init_sal (&sal); /* initialize to zeroes */
1709
1710 sal.pc = address;
1711 sal.section = find_pc_overlay (sal.pc);
1712 sal.pspace = current_program_space;
1713
1714 b = set_raw_breakpoint (gdbarch, sal, type);
1715 b->number = internal_breakpoint_number--;
1716 b->disposition = disp_donttouch;
1717
1718 return b;
1719 }
1720
1721 static void
1722 create_overlay_event_breakpoint (char *func_name)
1723 {
1724 struct objfile *objfile;
1725
1726 ALL_OBJFILES (objfile)
1727 {
1728 struct breakpoint *b;
1729 struct minimal_symbol *m;
1730
1731 m = lookup_minimal_symbol_text (func_name, objfile);
1732 if (m == NULL)
1733 continue;
1734
1735 b = create_internal_breakpoint (get_objfile_arch (objfile),
1736 SYMBOL_VALUE_ADDRESS (m),
1737 bp_overlay_event);
1738 b->addr_string = xstrdup (func_name);
1739
1740 if (overlay_debugging == ovly_auto)
1741 {
1742 b->enable_state = bp_enabled;
1743 overlay_events_enabled = 1;
1744 }
1745 else
1746 {
1747 b->enable_state = bp_disabled;
1748 overlay_events_enabled = 0;
1749 }
1750 }
1751 update_global_location_list (1);
1752 }
1753
1754 static void
1755 create_longjmp_master_breakpoint (char *func_name)
1756 {
1757 struct program_space *pspace;
1758 struct objfile *objfile;
1759 struct cleanup *old_chain;
1760
1761 old_chain = save_current_program_space ();
1762
1763 ALL_PSPACES (pspace)
1764 ALL_OBJFILES (objfile)
1765 {
1766 struct breakpoint *b;
1767 struct minimal_symbol *m;
1768
1769 if (!gdbarch_get_longjmp_target_p (get_objfile_arch (objfile)))
1770 continue;
1771
1772 set_current_program_space (pspace);
1773
1774 m = lookup_minimal_symbol_text (func_name, objfile);
1775 if (m == NULL)
1776 continue;
1777
1778 b = create_internal_breakpoint (get_objfile_arch (objfile),
1779 SYMBOL_VALUE_ADDRESS (m),
1780 bp_longjmp_master);
1781 b->addr_string = xstrdup (func_name);
1782 b->enable_state = bp_disabled;
1783 }
1784 update_global_location_list (1);
1785
1786 do_cleanups (old_chain);
1787 }
1788
1789 void
1790 update_breakpoints_after_exec (void)
1791 {
1792 struct breakpoint *b;
1793 struct breakpoint *temp;
1794 struct bp_location *bploc, **bplocp_tmp;
1795
1796 /* We're about to delete breakpoints from GDB's lists. If the
1797 INSERTED flag is true, GDB will try to lift the breakpoints by
1798 writing the breakpoints' "shadow contents" back into memory. The
1799 "shadow contents" are NOT valid after an exec, so GDB should not
1800 do that. Instead, the target is responsible from marking
1801 breakpoints out as soon as it detects an exec. We don't do that
1802 here instead, because there may be other attempts to delete
1803 breakpoints after detecting an exec and before reaching here. */
1804 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
1805 if (bploc->pspace == current_program_space)
1806 gdb_assert (!bploc->inserted);
1807
1808 ALL_BREAKPOINTS_SAFE (b, temp)
1809 {
1810 if (b->pspace != current_program_space)
1811 continue;
1812
1813 /* Solib breakpoints must be explicitly reset after an exec(). */
1814 if (b->type == bp_shlib_event)
1815 {
1816 delete_breakpoint (b);
1817 continue;
1818 }
1819
1820 /* JIT breakpoints must be explicitly reset after an exec(). */
1821 if (b->type == bp_jit_event)
1822 {
1823 delete_breakpoint (b);
1824 continue;
1825 }
1826
1827 /* Thread event breakpoints must be set anew after an exec(),
1828 as must overlay event and longjmp master breakpoints. */
1829 if (b->type == bp_thread_event || b->type == bp_overlay_event
1830 || b->type == bp_longjmp_master)
1831 {
1832 delete_breakpoint (b);
1833 continue;
1834 }
1835
1836 /* Step-resume breakpoints are meaningless after an exec(). */
1837 if (b->type == bp_step_resume)
1838 {
1839 delete_breakpoint (b);
1840 continue;
1841 }
1842
1843 /* Longjmp and longjmp-resume breakpoints are also meaningless
1844 after an exec. */
1845 if (b->type == bp_longjmp || b->type == bp_longjmp_resume)
1846 {
1847 delete_breakpoint (b);
1848 continue;
1849 }
1850
1851 if (b->type == bp_catchpoint)
1852 {
1853 /* For now, none of the bp_catchpoint breakpoints need to
1854 do anything at this point. In the future, if some of
1855 the catchpoints need to something, we will need to add
1856 a new method, and call this method from here. */
1857 continue;
1858 }
1859
1860 /* bp_finish is a special case. The only way we ought to be able
1861 to see one of these when an exec() has happened, is if the user
1862 caught a vfork, and then said "finish". Ordinarily a finish just
1863 carries them to the call-site of the current callee, by setting
1864 a temporary bp there and resuming. But in this case, the finish
1865 will carry them entirely through the vfork & exec.
1866
1867 We don't want to allow a bp_finish to remain inserted now. But
1868 we can't safely delete it, 'cause finish_command has a handle to
1869 the bp on a bpstat, and will later want to delete it. There's a
1870 chance (and I've seen it happen) that if we delete the bp_finish
1871 here, that its storage will get reused by the time finish_command
1872 gets 'round to deleting the "use to be a bp_finish" breakpoint.
1873 We really must allow finish_command to delete a bp_finish.
1874
1875 In the absense of a general solution for the "how do we know
1876 it's safe to delete something others may have handles to?"
1877 problem, what we'll do here is just uninsert the bp_finish, and
1878 let finish_command delete it.
1879
1880 (We know the bp_finish is "doomed" in the sense that it's
1881 momentary, and will be deleted as soon as finish_command sees
1882 the inferior stopped. So it doesn't matter that the bp's
1883 address is probably bogus in the new a.out, unlike e.g., the
1884 solib breakpoints.) */
1885
1886 if (b->type == bp_finish)
1887 {
1888 continue;
1889 }
1890
1891 /* Without a symbolic address, we have little hope of the
1892 pre-exec() address meaning the same thing in the post-exec()
1893 a.out. */
1894 if (b->addr_string == NULL)
1895 {
1896 delete_breakpoint (b);
1897 continue;
1898 }
1899 }
1900 /* FIXME what about longjmp breakpoints? Re-create them here? */
1901 create_overlay_event_breakpoint ("_ovly_debug_event");
1902 create_longjmp_master_breakpoint ("longjmp");
1903 create_longjmp_master_breakpoint ("_longjmp");
1904 create_longjmp_master_breakpoint ("siglongjmp");
1905 create_longjmp_master_breakpoint ("_siglongjmp");
1906 }
1907
1908 int
1909 detach_breakpoints (int pid)
1910 {
1911 struct bp_location *b, **bp_tmp;
1912 int val = 0;
1913 struct cleanup *old_chain = save_inferior_ptid ();
1914 struct inferior *inf = current_inferior ();
1915
1916 if (pid == PIDGET (inferior_ptid))
1917 error (_("Cannot detach breakpoints of inferior_ptid"));
1918
1919 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
1920 inferior_ptid = pid_to_ptid (pid);
1921 ALL_BP_LOCATIONS (b, bp_tmp)
1922 {
1923 if (b->pspace != inf->pspace)
1924 continue;
1925
1926 if (b->inserted)
1927 val |= remove_breakpoint_1 (b, mark_inserted);
1928 }
1929 do_cleanups (old_chain);
1930 return val;
1931 }
1932
1933 /* Remove the breakpoint location B from the current address space.
1934 Note that this is used to detach breakpoints from a child fork.
1935 When we get here, the child isn't in the inferior list, and neither
1936 do we have objects to represent its address space --- we should
1937 *not* look at b->pspace->aspace here. */
1938
1939 static int
1940 remove_breakpoint_1 (struct bp_location *b, insertion_state_t is)
1941 {
1942 int val;
1943 struct cleanup *old_chain;
1944
1945 if (b->owner->enable_state == bp_permanent)
1946 /* Permanent breakpoints cannot be inserted or removed. */
1947 return 0;
1948
1949 /* The type of none suggests that owner is actually deleted.
1950 This should not ever happen. */
1951 gdb_assert (b->owner->type != bp_none);
1952
1953 if (b->loc_type == bp_loc_software_breakpoint
1954 || b->loc_type == bp_loc_hardware_breakpoint)
1955 {
1956 /* "Normal" instruction breakpoint: either the standard
1957 trap-instruction bp (bp_breakpoint), or a
1958 bp_hardware_breakpoint. */
1959
1960 /* First check to see if we have to handle an overlay. */
1961 if (overlay_debugging == ovly_off
1962 || b->section == NULL
1963 || !(section_is_overlay (b->section)))
1964 {
1965 /* No overlay handling: just remove the breakpoint. */
1966
1967 if (b->loc_type == bp_loc_hardware_breakpoint)
1968 val = target_remove_hw_breakpoint (b->gdbarch, &b->target_info);
1969 else
1970 val = target_remove_breakpoint (b->gdbarch, &b->target_info);
1971 }
1972 else
1973 {
1974 /* This breakpoint is in an overlay section.
1975 Did we set a breakpoint at the LMA? */
1976 if (!overlay_events_enabled)
1977 {
1978 /* Yes -- overlay event support is not active, so we
1979 should have set a breakpoint at the LMA. Remove it.
1980 */
1981 /* Ignore any failures: if the LMA is in ROM, we will
1982 have already warned when we failed to insert it. */
1983 if (b->loc_type == bp_loc_hardware_breakpoint)
1984 target_remove_hw_breakpoint (b->gdbarch,
1985 &b->overlay_target_info);
1986 else
1987 target_remove_breakpoint (b->gdbarch,
1988 &b->overlay_target_info);
1989 }
1990 /* Did we set a breakpoint at the VMA?
1991 If so, we will have marked the breakpoint 'inserted'. */
1992 if (b->inserted)
1993 {
1994 /* Yes -- remove it. Previously we did not bother to
1995 remove the breakpoint if the section had been
1996 unmapped, but let's not rely on that being safe. We
1997 don't know what the overlay manager might do. */
1998 if (b->loc_type == bp_loc_hardware_breakpoint)
1999 val = target_remove_hw_breakpoint (b->gdbarch,
2000 &b->target_info);
2001
2002 /* However, we should remove *software* breakpoints only
2003 if the section is still mapped, or else we overwrite
2004 wrong code with the saved shadow contents. */
2005 else if (section_is_mapped (b->section))
2006 val = target_remove_breakpoint (b->gdbarch,
2007 &b->target_info);
2008 else
2009 val = 0;
2010 }
2011 else
2012 {
2013 /* No -- not inserted, so no need to remove. No error. */
2014 val = 0;
2015 }
2016 }
2017
2018 /* In some cases, we might not be able to remove a breakpoint
2019 in a shared library that has already been removed, but we
2020 have not yet processed the shlib unload event. */
2021 if (val && solib_name_from_address (b->pspace, b->address))
2022 val = 0;
2023
2024 if (val)
2025 return val;
2026 b->inserted = (is == mark_inserted);
2027 }
2028 else if (b->loc_type == bp_loc_hardware_watchpoint)
2029 {
2030 struct value *v;
2031 struct value *n;
2032
2033 b->inserted = (is == mark_inserted);
2034 val = target_remove_watchpoint (b->address, b->length,
2035 b->watchpoint_type);
2036
2037 /* Failure to remove any of the hardware watchpoints comes here. */
2038 if ((is == mark_uninserted) && (b->inserted))
2039 warning (_("Could not remove hardware watchpoint %d."),
2040 b->owner->number);
2041 }
2042 else if (b->owner->type == bp_catchpoint
2043 && breakpoint_enabled (b->owner)
2044 && !b->duplicate)
2045 {
2046 gdb_assert (b->owner->ops != NULL && b->owner->ops->remove != NULL);
2047
2048 val = b->owner->ops->remove (b->owner);
2049 if (val)
2050 return val;
2051 b->inserted = (is == mark_inserted);
2052 }
2053
2054 return 0;
2055 }
2056
2057 static int
2058 remove_breakpoint (struct bp_location *b, insertion_state_t is)
2059 {
2060 int ret;
2061 struct cleanup *old_chain;
2062
2063 if (b->owner->enable_state == bp_permanent)
2064 /* Permanent breakpoints cannot be inserted or removed. */
2065 return 0;
2066
2067 /* The type of none suggests that owner is actually deleted.
2068 This should not ever happen. */
2069 gdb_assert (b->owner->type != bp_none);
2070
2071 old_chain = save_current_space_and_thread ();
2072
2073 switch_to_program_space_and_thread (b->pspace);
2074
2075 ret = remove_breakpoint_1 (b, is);
2076
2077 do_cleanups (old_chain);
2078 return ret;
2079 }
2080
2081 /* Clear the "inserted" flag in all breakpoints. */
2082
2083 void
2084 mark_breakpoints_out (void)
2085 {
2086 struct bp_location *bpt, **bptp_tmp;
2087
2088 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2089 if (bpt->pspace == current_program_space)
2090 bpt->inserted = 0;
2091 }
2092
2093 /* Clear the "inserted" flag in all breakpoints and delete any
2094 breakpoints which should go away between runs of the program.
2095
2096 Plus other such housekeeping that has to be done for breakpoints
2097 between runs.
2098
2099 Note: this function gets called at the end of a run (by
2100 generic_mourn_inferior) and when a run begins (by
2101 init_wait_for_inferior). */
2102
2103
2104
2105 void
2106 breakpoint_init_inferior (enum inf_context context)
2107 {
2108 struct breakpoint *b, *temp;
2109 struct bp_location *bpt, **bptp_tmp;
2110 int ix;
2111 struct program_space *pspace = current_program_space;
2112
2113 /* If breakpoint locations are shared across processes, then there's
2114 nothing to do. */
2115 if (gdbarch_has_global_breakpoints (target_gdbarch))
2116 return;
2117
2118 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2119 {
2120 if (bpt->pspace == pspace
2121 && bpt->owner->enable_state != bp_permanent)
2122 bpt->inserted = 0;
2123 }
2124
2125 ALL_BREAKPOINTS_SAFE (b, temp)
2126 {
2127 if (b->loc && b->loc->pspace != pspace)
2128 continue;
2129
2130 switch (b->type)
2131 {
2132 case bp_call_dummy:
2133 case bp_watchpoint_scope:
2134
2135 /* If the call dummy breakpoint is at the entry point it will
2136 cause problems when the inferior is rerun, so we better
2137 get rid of it.
2138
2139 Also get rid of scope breakpoints. */
2140 delete_breakpoint (b);
2141 break;
2142
2143 case bp_watchpoint:
2144 case bp_hardware_watchpoint:
2145 case bp_read_watchpoint:
2146 case bp_access_watchpoint:
2147
2148 /* Likewise for watchpoints on local expressions. */
2149 if (b->exp_valid_block != NULL)
2150 delete_breakpoint (b);
2151 else if (context == inf_starting)
2152 {
2153 /* Reset val field to force reread of starting value
2154 in insert_breakpoints. */
2155 if (b->val)
2156 value_free (b->val);
2157 b->val = NULL;
2158 b->val_valid = 0;
2159 }
2160 break;
2161 default:
2162 break;
2163 }
2164 }
2165
2166 /* Get rid of the moribund locations. */
2167 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bpt); ++ix)
2168 free_bp_location (bpt);
2169 VEC_free (bp_location_p, moribund_locations);
2170 }
2171
2172 /* These functions concern about actual breakpoints inserted in the
2173 target --- to e.g. check if we need to do decr_pc adjustment or if
2174 we need to hop over the bkpt --- so we check for address space
2175 match, not program space. */
2176
2177 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
2178 exists at PC. It returns ordinary_breakpoint_here if it's an
2179 ordinary breakpoint, or permanent_breakpoint_here if it's a
2180 permanent breakpoint.
2181 - When continuing from a location with an ordinary breakpoint, we
2182 actually single step once before calling insert_breakpoints.
2183 - When continuing from a localion with a permanent breakpoint, we
2184 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
2185 the target, to advance the PC past the breakpoint. */
2186
2187 enum breakpoint_here
2188 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2189 {
2190 struct bp_location *bpt, **bptp_tmp;
2191 int any_breakpoint_here = 0;
2192
2193 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2194 {
2195 if (bpt->loc_type != bp_loc_software_breakpoint
2196 && bpt->loc_type != bp_loc_hardware_breakpoint)
2197 continue;
2198
2199 if ((breakpoint_enabled (bpt->owner)
2200 || bpt->owner->enable_state == bp_permanent)
2201 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2202 aspace, pc))
2203 {
2204 if (overlay_debugging
2205 && section_is_overlay (bpt->section)
2206 && !section_is_mapped (bpt->section))
2207 continue; /* unmapped overlay -- can't be a match */
2208 else if (bpt->owner->enable_state == bp_permanent)
2209 return permanent_breakpoint_here;
2210 else
2211 any_breakpoint_here = 1;
2212 }
2213 }
2214
2215 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
2216 }
2217
2218 /* Return true if there's a moribund breakpoint at PC. */
2219
2220 int
2221 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
2222 {
2223 struct bp_location *loc;
2224 int ix;
2225
2226 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
2227 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
2228 aspace, pc))
2229 return 1;
2230
2231 return 0;
2232 }
2233
2234 /* Returns non-zero if there's a breakpoint inserted at PC, which is
2235 inserted using regular breakpoint_chain / bp_location array mechanism.
2236 This does not check for single-step breakpoints, which are
2237 inserted and removed using direct target manipulation. */
2238
2239 int
2240 regular_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2241 {
2242 struct bp_location *bpt, **bptp_tmp;
2243
2244 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2245 {
2246 if (bpt->loc_type != bp_loc_software_breakpoint
2247 && bpt->loc_type != bp_loc_hardware_breakpoint)
2248 continue;
2249
2250 if (bpt->inserted
2251 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2252 aspace, pc))
2253 {
2254 if (overlay_debugging
2255 && section_is_overlay (bpt->section)
2256 && !section_is_mapped (bpt->section))
2257 continue; /* unmapped overlay -- can't be a match */
2258 else
2259 return 1;
2260 }
2261 }
2262 return 0;
2263 }
2264
2265 /* Returns non-zero iff there's either regular breakpoint
2266 or a single step breakpoint inserted at PC. */
2267
2268 int
2269 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2270 {
2271 if (regular_breakpoint_inserted_here_p (aspace, pc))
2272 return 1;
2273
2274 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2275 return 1;
2276
2277 return 0;
2278 }
2279
2280 /* This function returns non-zero iff there is a software breakpoint
2281 inserted at PC. */
2282
2283 int
2284 software_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
2285 {
2286 struct bp_location *bpt, **bptp_tmp;
2287 int any_breakpoint_here = 0;
2288
2289 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2290 {
2291 if (bpt->loc_type != bp_loc_software_breakpoint)
2292 continue;
2293
2294 if (bpt->inserted
2295 && breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2296 aspace, pc))
2297 {
2298 if (overlay_debugging
2299 && section_is_overlay (bpt->section)
2300 && !section_is_mapped (bpt->section))
2301 continue; /* unmapped overlay -- can't be a match */
2302 else
2303 return 1;
2304 }
2305 }
2306
2307 /* Also check for software single-step breakpoints. */
2308 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2309 return 1;
2310
2311 return 0;
2312 }
2313
2314 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
2315 PC is valid for process/thread PTID. */
2316
2317 int
2318 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
2319 ptid_t ptid)
2320 {
2321 struct bp_location *bpt, **bptp_tmp;
2322 /* The thread and task IDs associated to PTID, computed lazily. */
2323 int thread = -1;
2324 int task = 0;
2325
2326 ALL_BP_LOCATIONS (bpt, bptp_tmp)
2327 {
2328 if (bpt->loc_type != bp_loc_software_breakpoint
2329 && bpt->loc_type != bp_loc_hardware_breakpoint)
2330 continue;
2331
2332 if (!breakpoint_enabled (bpt->owner)
2333 && bpt->owner->enable_state != bp_permanent)
2334 continue;
2335
2336 if (!breakpoint_address_match (bpt->pspace->aspace, bpt->address,
2337 aspace, pc))
2338 continue;
2339
2340 if (bpt->owner->thread != -1)
2341 {
2342 /* This is a thread-specific breakpoint. Check that ptid
2343 matches that thread. If thread hasn't been computed yet,
2344 it is now time to do so. */
2345 if (thread == -1)
2346 thread = pid_to_thread_id (ptid);
2347 if (bpt->owner->thread != thread)
2348 continue;
2349 }
2350
2351 if (bpt->owner->task != 0)
2352 {
2353 /* This is a task-specific breakpoint. Check that ptid
2354 matches that task. If task hasn't been computed yet,
2355 it is now time to do so. */
2356 if (task == 0)
2357 task = ada_get_task_number (ptid);
2358 if (bpt->owner->task != task)
2359 continue;
2360 }
2361
2362 if (overlay_debugging
2363 && section_is_overlay (bpt->section)
2364 && !section_is_mapped (bpt->section))
2365 continue; /* unmapped overlay -- can't be a match */
2366
2367 return 1;
2368 }
2369
2370 return 0;
2371 }
2372 \f
2373
2374 /* bpstat stuff. External routines' interfaces are documented
2375 in breakpoint.h. */
2376
2377 int
2378 ep_is_catchpoint (struct breakpoint *ep)
2379 {
2380 return (ep->type == bp_catchpoint);
2381 }
2382
2383 void
2384 bpstat_free (bpstat bs)
2385 {
2386 if (bs->old_val != NULL)
2387 value_free (bs->old_val);
2388 free_command_lines (&bs->commands);
2389 xfree (bs);
2390 }
2391
2392 /* Clear a bpstat so that it says we are not at any breakpoint.
2393 Also free any storage that is part of a bpstat. */
2394
2395 void
2396 bpstat_clear (bpstat *bsp)
2397 {
2398 bpstat p;
2399 bpstat q;
2400
2401 if (bsp == 0)
2402 return;
2403 p = *bsp;
2404 while (p != NULL)
2405 {
2406 q = p->next;
2407 bpstat_free (p);
2408 p = q;
2409 }
2410 *bsp = NULL;
2411 }
2412
2413 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
2414 is part of the bpstat is copied as well. */
2415
2416 bpstat
2417 bpstat_copy (bpstat bs)
2418 {
2419 bpstat p = NULL;
2420 bpstat tmp;
2421 bpstat retval = NULL;
2422
2423 if (bs == NULL)
2424 return bs;
2425
2426 for (; bs != NULL; bs = bs->next)
2427 {
2428 tmp = (bpstat) xmalloc (sizeof (*tmp));
2429 memcpy (tmp, bs, sizeof (*tmp));
2430 if (bs->commands != NULL)
2431 tmp->commands = copy_command_lines (bs->commands);
2432 if (bs->old_val != NULL)
2433 {
2434 tmp->old_val = value_copy (bs->old_val);
2435 release_value (tmp->old_val);
2436 }
2437
2438 if (p == NULL)
2439 /* This is the first thing in the chain. */
2440 retval = tmp;
2441 else
2442 p->next = tmp;
2443 p = tmp;
2444 }
2445 p->next = NULL;
2446 return retval;
2447 }
2448
2449 /* Find the bpstat associated with this breakpoint */
2450
2451 bpstat
2452 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
2453 {
2454 if (bsp == NULL)
2455 return NULL;
2456
2457 for (; bsp != NULL; bsp = bsp->next)
2458 {
2459 if (bsp->breakpoint_at && bsp->breakpoint_at->owner == breakpoint)
2460 return bsp;
2461 }
2462 return NULL;
2463 }
2464
2465 /* Find a step_resume breakpoint associated with this bpstat.
2466 (If there are multiple step_resume bp's on the list, this function
2467 will arbitrarily pick one.)
2468
2469 It is an error to use this function if BPSTAT doesn't contain a
2470 step_resume breakpoint.
2471
2472 See wait_for_inferior's use of this function. */
2473 struct breakpoint *
2474 bpstat_find_step_resume_breakpoint (bpstat bsp)
2475 {
2476 int current_thread;
2477
2478 gdb_assert (bsp != NULL);
2479
2480 current_thread = pid_to_thread_id (inferior_ptid);
2481
2482 for (; bsp != NULL; bsp = bsp->next)
2483 {
2484 if ((bsp->breakpoint_at != NULL)
2485 && (bsp->breakpoint_at->owner->type == bp_step_resume)
2486 && (bsp->breakpoint_at->owner->thread == current_thread
2487 || bsp->breakpoint_at->owner->thread == -1))
2488 return bsp->breakpoint_at->owner;
2489 }
2490
2491 internal_error (__FILE__, __LINE__, _("No step_resume breakpoint found."));
2492 }
2493
2494
2495 /* Put in *NUM the breakpoint number of the first breakpoint we are stopped
2496 at. *BSP upon return is a bpstat which points to the remaining
2497 breakpoints stopped at (but which is not guaranteed to be good for
2498 anything but further calls to bpstat_num).
2499 Return 0 if passed a bpstat which does not indicate any breakpoints.
2500 Return -1 if stopped at a breakpoint that has been deleted since
2501 we set it.
2502 Return 1 otherwise. */
2503
2504 int
2505 bpstat_num (bpstat *bsp, int *num)
2506 {
2507 struct breakpoint *b;
2508
2509 if ((*bsp) == NULL)
2510 return 0; /* No more breakpoint values */
2511
2512 /* We assume we'll never have several bpstats that
2513 correspond to a single breakpoint -- otherwise,
2514 this function might return the same number more
2515 than once and this will look ugly. */
2516 b = (*bsp)->breakpoint_at ? (*bsp)->breakpoint_at->owner : NULL;
2517 *bsp = (*bsp)->next;
2518 if (b == NULL)
2519 return -1; /* breakpoint that's been deleted since */
2520
2521 *num = b->number; /* We have its number */
2522 return 1;
2523 }
2524
2525 /* Modify BS so that the actions will not be performed. */
2526
2527 void
2528 bpstat_clear_actions (bpstat bs)
2529 {
2530 for (; bs != NULL; bs = bs->next)
2531 {
2532 free_command_lines (&bs->commands);
2533 if (bs->old_val != NULL)
2534 {
2535 value_free (bs->old_val);
2536 bs->old_val = NULL;
2537 }
2538 }
2539 }
2540
2541 /* Called when a command is about to proceed the inferior. */
2542
2543 static void
2544 breakpoint_about_to_proceed (void)
2545 {
2546 if (!ptid_equal (inferior_ptid, null_ptid))
2547 {
2548 struct thread_info *tp = inferior_thread ();
2549
2550 /* Allow inferior function calls in breakpoint commands to not
2551 interrupt the command list. When the call finishes
2552 successfully, the inferior will be standing at the same
2553 breakpoint as if nothing happened. */
2554 if (tp->in_infcall)
2555 return;
2556 }
2557
2558 breakpoint_proceeded = 1;
2559 }
2560
2561 /* Stub for cleaning up our state if we error-out of a breakpoint command */
2562 static void
2563 cleanup_executing_breakpoints (void *ignore)
2564 {
2565 executing_breakpoint_commands = 0;
2566 }
2567
2568 /* Execute all the commands associated with all the breakpoints at this
2569 location. Any of these commands could cause the process to proceed
2570 beyond this point, etc. We look out for such changes by checking
2571 the global "breakpoint_proceeded" after each command.
2572
2573 Returns true if a breakpoint command resumed the inferior. In that
2574 case, it is the caller's responsibility to recall it again with the
2575 bpstat of the current thread. */
2576
2577 static int
2578 bpstat_do_actions_1 (bpstat *bsp)
2579 {
2580 bpstat bs;
2581 struct cleanup *old_chain;
2582 int again = 0;
2583
2584 /* Avoid endless recursion if a `source' command is contained
2585 in bs->commands. */
2586 if (executing_breakpoint_commands)
2587 return 0;
2588
2589 executing_breakpoint_commands = 1;
2590 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
2591
2592 /* This pointer will iterate over the list of bpstat's. */
2593 bs = *bsp;
2594
2595 breakpoint_proceeded = 0;
2596 for (; bs != NULL; bs = bs->next)
2597 {
2598 struct command_line *cmd;
2599 struct cleanup *this_cmd_tree_chain;
2600
2601 /* Take ownership of the BSP's command tree, if it has one.
2602
2603 The command tree could legitimately contain commands like
2604 'step' and 'next', which call clear_proceed_status, which
2605 frees stop_bpstat's command tree. To make sure this doesn't
2606 free the tree we're executing out from under us, we need to
2607 take ownership of the tree ourselves. Since a given bpstat's
2608 commands are only executed once, we don't need to copy it; we
2609 can clear the pointer in the bpstat, and make sure we free
2610 the tree when we're done. */
2611 cmd = bs->commands;
2612 bs->commands = 0;
2613 this_cmd_tree_chain = make_cleanup_free_command_lines (&cmd);
2614
2615 while (cmd != NULL)
2616 {
2617 execute_control_command (cmd);
2618
2619 if (breakpoint_proceeded)
2620 break;
2621 else
2622 cmd = cmd->next;
2623 }
2624
2625 /* We can free this command tree now. */
2626 do_cleanups (this_cmd_tree_chain);
2627
2628 if (breakpoint_proceeded)
2629 {
2630 if (target_can_async_p ())
2631 /* If we are in async mode, then the target might be still
2632 running, not stopped at any breakpoint, so nothing for
2633 us to do here -- just return to the event loop. */
2634 ;
2635 else
2636 /* In sync mode, when execute_control_command returns
2637 we're already standing on the next breakpoint.
2638 Breakpoint commands for that stop were not run, since
2639 execute_command does not run breakpoint commands --
2640 only command_line_handler does, but that one is not
2641 involved in execution of breakpoint commands. So, we
2642 can now execute breakpoint commands. It should be
2643 noted that making execute_command do bpstat actions is
2644 not an option -- in this case we'll have recursive
2645 invocation of bpstat for each breakpoint with a
2646 command, and can easily blow up GDB stack. Instead, we
2647 return true, which will trigger the caller to recall us
2648 with the new stop_bpstat. */
2649 again = 1;
2650 break;
2651 }
2652 }
2653 do_cleanups (old_chain);
2654 return again;
2655 }
2656
2657 void
2658 bpstat_do_actions (void)
2659 {
2660 /* Do any commands attached to breakpoint we are stopped at. */
2661 while (!ptid_equal (inferior_ptid, null_ptid)
2662 && target_has_execution
2663 && !is_exited (inferior_ptid)
2664 && !is_executing (inferior_ptid))
2665 /* Since in sync mode, bpstat_do_actions may resume the inferior,
2666 and only return when it is stopped at the next breakpoint, we
2667 keep doing breakpoint actions until it returns false to
2668 indicate the inferior was not resumed. */
2669 if (!bpstat_do_actions_1 (&inferior_thread ()->stop_bpstat))
2670 break;
2671 }
2672
2673 /* Print out the (old or new) value associated with a watchpoint. */
2674
2675 static void
2676 watchpoint_value_print (struct value *val, struct ui_file *stream)
2677 {
2678 if (val == NULL)
2679 fprintf_unfiltered (stream, _("<unreadable>"));
2680 else
2681 {
2682 struct value_print_options opts;
2683 get_user_print_options (&opts);
2684 value_print (val, stream, &opts);
2685 }
2686 }
2687
2688 /* This is the normal print function for a bpstat. In the future,
2689 much of this logic could (should?) be moved to bpstat_stop_status,
2690 by having it set different print_it values.
2691
2692 Current scheme: When we stop, bpstat_print() is called. It loops
2693 through the bpstat list of things causing this stop, calling the
2694 print_bp_stop_message function on each one. The behavior of the
2695 print_bp_stop_message function depends on the print_it field of
2696 bpstat. If such field so indicates, call this function here.
2697
2698 Return values from this routine (ultimately used by bpstat_print()
2699 and normal_stop() to decide what to do):
2700 PRINT_NOTHING: Means we already printed all we needed to print,
2701 don't print anything else.
2702 PRINT_SRC_ONLY: Means we printed something, and we do *not* desire
2703 that something to be followed by a location.
2704 PRINT_SCR_AND_LOC: Means we printed something, and we *do* desire
2705 that something to be followed by a location.
2706 PRINT_UNKNOWN: Means we printed nothing or we need to do some more
2707 analysis. */
2708
2709 static enum print_stop_action
2710 print_it_typical (bpstat bs)
2711 {
2712 struct cleanup *old_chain;
2713 struct breakpoint *b;
2714 const struct bp_location *bl;
2715 struct ui_stream *stb;
2716 int bp_temp = 0;
2717 enum print_stop_action result;
2718
2719 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
2720 which has since been deleted. */
2721 if (bs->breakpoint_at == NULL)
2722 return PRINT_UNKNOWN;
2723 bl = bs->breakpoint_at;
2724 b = bl->owner;
2725
2726 stb = ui_out_stream_new (uiout);
2727 old_chain = make_cleanup_ui_out_stream_delete (stb);
2728
2729 switch (b->type)
2730 {
2731 case bp_breakpoint:
2732 case bp_hardware_breakpoint:
2733 bp_temp = bs->breakpoint_at->owner->disposition == disp_del;
2734 if (bl->address != bl->requested_address)
2735 breakpoint_adjustment_warning (bl->requested_address,
2736 bl->address,
2737 b->number, 1);
2738 annotate_breakpoint (b->number);
2739 if (bp_temp)
2740 ui_out_text (uiout, "\nTemporary breakpoint ");
2741 else
2742 ui_out_text (uiout, "\nBreakpoint ");
2743 if (ui_out_is_mi_like_p (uiout))
2744 {
2745 ui_out_field_string (uiout, "reason",
2746 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
2747 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
2748 }
2749 ui_out_field_int (uiout, "bkptno", b->number);
2750 ui_out_text (uiout, ", ");
2751 result = PRINT_SRC_AND_LOC;
2752 break;
2753
2754 case bp_shlib_event:
2755 /* Did we stop because the user set the stop_on_solib_events
2756 variable? (If so, we report this as a generic, "Stopped due
2757 to shlib event" message.) */
2758 printf_filtered (_("Stopped due to shared library event\n"));
2759 result = PRINT_NOTHING;
2760 break;
2761
2762 case bp_thread_event:
2763 /* Not sure how we will get here.
2764 GDB should not stop for these breakpoints. */
2765 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
2766 result = PRINT_NOTHING;
2767 break;
2768
2769 case bp_overlay_event:
2770 /* By analogy with the thread event, GDB should not stop for these. */
2771 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
2772 result = PRINT_NOTHING;
2773 break;
2774
2775 case bp_longjmp_master:
2776 /* These should never be enabled. */
2777 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
2778 result = PRINT_NOTHING;
2779 break;
2780
2781 case bp_watchpoint:
2782 case bp_hardware_watchpoint:
2783 annotate_watchpoint (b->number);
2784 if (ui_out_is_mi_like_p (uiout))
2785 ui_out_field_string
2786 (uiout, "reason",
2787 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
2788 mention (b);
2789 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2790 ui_out_text (uiout, "\nOld value = ");
2791 watchpoint_value_print (bs->old_val, stb->stream);
2792 ui_out_field_stream (uiout, "old", stb);
2793 ui_out_text (uiout, "\nNew value = ");
2794 watchpoint_value_print (b->val, stb->stream);
2795 ui_out_field_stream (uiout, "new", stb);
2796 ui_out_text (uiout, "\n");
2797 /* More than one watchpoint may have been triggered. */
2798 result = PRINT_UNKNOWN;
2799 break;
2800
2801 case bp_read_watchpoint:
2802 if (ui_out_is_mi_like_p (uiout))
2803 ui_out_field_string
2804 (uiout, "reason",
2805 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
2806 mention (b);
2807 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2808 ui_out_text (uiout, "\nValue = ");
2809 watchpoint_value_print (b->val, stb->stream);
2810 ui_out_field_stream (uiout, "value", stb);
2811 ui_out_text (uiout, "\n");
2812 result = PRINT_UNKNOWN;
2813 break;
2814
2815 case bp_access_watchpoint:
2816 if (bs->old_val != NULL)
2817 {
2818 annotate_watchpoint (b->number);
2819 if (ui_out_is_mi_like_p (uiout))
2820 ui_out_field_string
2821 (uiout, "reason",
2822 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2823 mention (b);
2824 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2825 ui_out_text (uiout, "\nOld value = ");
2826 watchpoint_value_print (bs->old_val, stb->stream);
2827 ui_out_field_stream (uiout, "old", stb);
2828 ui_out_text (uiout, "\nNew value = ");
2829 }
2830 else
2831 {
2832 mention (b);
2833 if (ui_out_is_mi_like_p (uiout))
2834 ui_out_field_string
2835 (uiout, "reason",
2836 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
2837 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
2838 ui_out_text (uiout, "\nValue = ");
2839 }
2840 watchpoint_value_print (b->val, stb->stream);
2841 ui_out_field_stream (uiout, "new", stb);
2842 ui_out_text (uiout, "\n");
2843 result = PRINT_UNKNOWN;
2844 break;
2845
2846 /* Fall through, we don't deal with these types of breakpoints
2847 here. */
2848
2849 case bp_finish:
2850 if (ui_out_is_mi_like_p (uiout))
2851 ui_out_field_string
2852 (uiout, "reason",
2853 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
2854 result = PRINT_UNKNOWN;
2855 break;
2856
2857 case bp_until:
2858 if (ui_out_is_mi_like_p (uiout))
2859 ui_out_field_string
2860 (uiout, "reason",
2861 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
2862 result = PRINT_UNKNOWN;
2863 break;
2864
2865 case bp_none:
2866 case bp_longjmp:
2867 case bp_longjmp_resume:
2868 case bp_step_resume:
2869 case bp_watchpoint_scope:
2870 case bp_call_dummy:
2871 case bp_tracepoint:
2872 case bp_jit_event:
2873 default:
2874 result = PRINT_UNKNOWN;
2875 break;
2876 }
2877
2878 do_cleanups (old_chain);
2879 return result;
2880 }
2881
2882 /* Generic routine for printing messages indicating why we
2883 stopped. The behavior of this function depends on the value
2884 'print_it' in the bpstat structure. Under some circumstances we
2885 may decide not to print anything here and delegate the task to
2886 normal_stop(). */
2887
2888 static enum print_stop_action
2889 print_bp_stop_message (bpstat bs)
2890 {
2891 switch (bs->print_it)
2892 {
2893 case print_it_noop:
2894 /* Nothing should be printed for this bpstat entry. */
2895 return PRINT_UNKNOWN;
2896 break;
2897
2898 case print_it_done:
2899 /* We still want to print the frame, but we already printed the
2900 relevant messages. */
2901 return PRINT_SRC_AND_LOC;
2902 break;
2903
2904 case print_it_normal:
2905 {
2906 const struct bp_location *bl = bs->breakpoint_at;
2907 struct breakpoint *b = bl ? bl->owner : NULL;
2908
2909 /* Normal case. Call the breakpoint's print_it method, or
2910 print_it_typical. */
2911 /* FIXME: how breakpoint can ever be NULL here? */
2912 if (b != NULL && b->ops != NULL && b->ops->print_it != NULL)
2913 return b->ops->print_it (b);
2914 else
2915 return print_it_typical (bs);
2916 }
2917 break;
2918
2919 default:
2920 internal_error (__FILE__, __LINE__,
2921 _("print_bp_stop_message: unrecognized enum value"));
2922 break;
2923 }
2924 }
2925
2926 /* Print a message indicating what happened. This is called from
2927 normal_stop(). The input to this routine is the head of the bpstat
2928 list - a list of the eventpoints that caused this stop. This
2929 routine calls the generic print routine for printing a message
2930 about reasons for stopping. This will print (for example) the
2931 "Breakpoint n," part of the output. The return value of this
2932 routine is one of:
2933
2934 PRINT_UNKNOWN: Means we printed nothing
2935 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
2936 code to print the location. An example is
2937 "Breakpoint 1, " which should be followed by
2938 the location.
2939 PRINT_SRC_ONLY: Means we printed something, but there is no need
2940 to also print the location part of the message.
2941 An example is the catch/throw messages, which
2942 don't require a location appended to the end.
2943 PRINT_NOTHING: We have done some printing and we don't need any
2944 further info to be printed.*/
2945
2946 enum print_stop_action
2947 bpstat_print (bpstat bs)
2948 {
2949 int val;
2950
2951 /* Maybe another breakpoint in the chain caused us to stop.
2952 (Currently all watchpoints go on the bpstat whether hit or not.
2953 That probably could (should) be changed, provided care is taken
2954 with respect to bpstat_explains_signal). */
2955 for (; bs; bs = bs->next)
2956 {
2957 val = print_bp_stop_message (bs);
2958 if (val == PRINT_SRC_ONLY
2959 || val == PRINT_SRC_AND_LOC
2960 || val == PRINT_NOTHING)
2961 return val;
2962 }
2963
2964 /* We reached the end of the chain, or we got a null BS to start
2965 with and nothing was printed. */
2966 return PRINT_UNKNOWN;
2967 }
2968
2969 /* Evaluate the expression EXP and return 1 if value is zero.
2970 This is used inside a catch_errors to evaluate the breakpoint condition.
2971 The argument is a "struct expression *" that has been cast to char * to
2972 make it pass through catch_errors. */
2973
2974 static int
2975 breakpoint_cond_eval (void *exp)
2976 {
2977 struct value *mark = value_mark ();
2978 int i = !value_true (evaluate_expression ((struct expression *) exp));
2979 value_free_to_mark (mark);
2980 return i;
2981 }
2982
2983 /* Allocate a new bpstat and chain it to the current one. */
2984
2985 static bpstat
2986 bpstat_alloc (const struct bp_location *bl, bpstat cbs /* Current "bs" value */ )
2987 {
2988 bpstat bs;
2989
2990 bs = (bpstat) xmalloc (sizeof (*bs));
2991 cbs->next = bs;
2992 bs->breakpoint_at = bl;
2993 /* If the condition is false, etc., don't do the commands. */
2994 bs->commands = NULL;
2995 bs->old_val = NULL;
2996 bs->print_it = print_it_normal;
2997 return bs;
2998 }
2999 \f
3000 /* The target has stopped with waitstatus WS. Check if any hardware
3001 watchpoints have triggered, according to the target. */
3002
3003 int
3004 watchpoints_triggered (struct target_waitstatus *ws)
3005 {
3006 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
3007 CORE_ADDR addr;
3008 struct breakpoint *b;
3009
3010 if (!stopped_by_watchpoint)
3011 {
3012 /* We were not stopped by a watchpoint. Mark all watchpoints
3013 as not triggered. */
3014 ALL_BREAKPOINTS (b)
3015 if (b->type == bp_hardware_watchpoint
3016 || b->type == bp_read_watchpoint
3017 || b->type == bp_access_watchpoint)
3018 b->watchpoint_triggered = watch_triggered_no;
3019
3020 return 0;
3021 }
3022
3023 if (!target_stopped_data_address (&current_target, &addr))
3024 {
3025 /* We were stopped by a watchpoint, but we don't know where.
3026 Mark all watchpoints as unknown. */
3027 ALL_BREAKPOINTS (b)
3028 if (b->type == bp_hardware_watchpoint
3029 || b->type == bp_read_watchpoint
3030 || b->type == bp_access_watchpoint)
3031 b->watchpoint_triggered = watch_triggered_unknown;
3032
3033 return stopped_by_watchpoint;
3034 }
3035
3036 /* The target could report the data address. Mark watchpoints
3037 affected by this data address as triggered, and all others as not
3038 triggered. */
3039
3040 ALL_BREAKPOINTS (b)
3041 if (b->type == bp_hardware_watchpoint
3042 || b->type == bp_read_watchpoint
3043 || b->type == bp_access_watchpoint)
3044 {
3045 struct bp_location *loc;
3046 struct value *v;
3047
3048 b->watchpoint_triggered = watch_triggered_no;
3049 for (loc = b->loc; loc; loc = loc->next)
3050 /* Exact match not required. Within range is
3051 sufficient. */
3052 if (target_watchpoint_addr_within_range (&current_target,
3053 addr, loc->address,
3054 loc->length))
3055 {
3056 b->watchpoint_triggered = watch_triggered_yes;
3057 break;
3058 }
3059 }
3060
3061 return 1;
3062 }
3063
3064 /* Possible return values for watchpoint_check (this can't be an enum
3065 because of check_errors). */
3066 /* The watchpoint has been deleted. */
3067 #define WP_DELETED 1
3068 /* The value has changed. */
3069 #define WP_VALUE_CHANGED 2
3070 /* The value has not changed. */
3071 #define WP_VALUE_NOT_CHANGED 3
3072
3073 #define BP_TEMPFLAG 1
3074 #define BP_HARDWAREFLAG 2
3075
3076 /* Check watchpoint condition. */
3077
3078 static int
3079 watchpoint_check (void *p)
3080 {
3081 bpstat bs = (bpstat) p;
3082 struct breakpoint *b;
3083 struct frame_info *fr;
3084 int within_current_scope;
3085
3086 b = bs->breakpoint_at->owner;
3087
3088 if (b->exp_valid_block == NULL)
3089 within_current_scope = 1;
3090 else
3091 {
3092 struct frame_info *frame = get_current_frame ();
3093 struct gdbarch *frame_arch = get_frame_arch (frame);
3094 CORE_ADDR frame_pc = get_frame_pc (frame);
3095
3096 fr = frame_find_by_id (b->watchpoint_frame);
3097 within_current_scope = (fr != NULL);
3098
3099 /* If we've gotten confused in the unwinder, we might have
3100 returned a frame that can't describe this variable. */
3101 if (within_current_scope)
3102 {
3103 struct symbol *function;
3104
3105 function = get_frame_function (fr);
3106 if (function == NULL
3107 || !contained_in (b->exp_valid_block,
3108 SYMBOL_BLOCK_VALUE (function)))
3109 within_current_scope = 0;
3110 }
3111
3112 /* in_function_epilogue_p() returns a non-zero value if we're still
3113 in the function but the stack frame has already been invalidated.
3114 Since we can't rely on the values of local variables after the
3115 stack has been destroyed, we are treating the watchpoint in that
3116 state as `not changed' without further checking. Don't mark
3117 watchpoints as changed if the current frame is in an epilogue -
3118 even if they are in some other frame, our view of the stack
3119 is likely to be wrong. */
3120 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
3121 return WP_VALUE_NOT_CHANGED;
3122
3123 if (within_current_scope)
3124 /* If we end up stopping, the current frame will get selected
3125 in normal_stop. So this call to select_frame won't affect
3126 the user. */
3127 select_frame (fr);
3128 }
3129
3130 if (within_current_scope)
3131 {
3132 /* We use value_{,free_to_}mark because it could be a
3133 *long* time before we return to the command level and
3134 call free_all_values. We can't call free_all_values because
3135 we might be in the middle of evaluating a function call. */
3136
3137 struct value *mark = value_mark ();
3138 struct value *new_val;
3139
3140 fetch_watchpoint_value (b->exp, &new_val, NULL, NULL);
3141 if ((b->val != NULL) != (new_val != NULL)
3142 || (b->val != NULL && !value_equal (b->val, new_val)))
3143 {
3144 if (new_val != NULL)
3145 {
3146 release_value (new_val);
3147 value_free_to_mark (mark);
3148 }
3149 bs->old_val = b->val;
3150 b->val = new_val;
3151 b->val_valid = 1;
3152 /* We will stop here */
3153 return WP_VALUE_CHANGED;
3154 }
3155 else
3156 {
3157 /* Nothing changed, don't do anything. */
3158 value_free_to_mark (mark);
3159 /* We won't stop here */
3160 return WP_VALUE_NOT_CHANGED;
3161 }
3162 }
3163 else
3164 {
3165 /* This seems like the only logical thing to do because
3166 if we temporarily ignored the watchpoint, then when
3167 we reenter the block in which it is valid it contains
3168 garbage (in the case of a function, it may have two
3169 garbage values, one before and one after the prologue).
3170 So we can't even detect the first assignment to it and
3171 watch after that (since the garbage may or may not equal
3172 the first value assigned). */
3173 /* We print all the stop information in print_it_typical(), but
3174 in this case, by the time we call print_it_typical() this bp
3175 will be deleted already. So we have no choice but print the
3176 information here. */
3177 if (ui_out_is_mi_like_p (uiout))
3178 ui_out_field_string
3179 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
3180 ui_out_text (uiout, "\nWatchpoint ");
3181 ui_out_field_int (uiout, "wpnum", b->number);
3182 ui_out_text (uiout, " deleted because the program has left the block in\n\
3183 which its expression is valid.\n");
3184
3185 if (b->related_breakpoint)
3186 b->related_breakpoint->disposition = disp_del_at_next_stop;
3187 b->disposition = disp_del_at_next_stop;
3188
3189 return WP_DELETED;
3190 }
3191 }
3192
3193 /* Return true if it looks like target has stopped due to hitting
3194 breakpoint location BL. This function does not check if we
3195 should stop, only if BL explains the stop. */
3196 static int
3197 bpstat_check_location (const struct bp_location *bl,
3198 struct address_space *aspace, CORE_ADDR bp_addr)
3199 {
3200 struct breakpoint *b = bl->owner;
3201
3202 if (b->type != bp_watchpoint
3203 && b->type != bp_hardware_watchpoint
3204 && b->type != bp_read_watchpoint
3205 && b->type != bp_access_watchpoint
3206 && b->type != bp_hardware_breakpoint
3207 && b->type != bp_catchpoint) /* a non-watchpoint bp */
3208 {
3209 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
3210 aspace, bp_addr))
3211 return 0;
3212 if (overlay_debugging /* unmapped overlay section */
3213 && section_is_overlay (bl->section)
3214 && !section_is_mapped (bl->section))
3215 return 0;
3216 }
3217
3218 /* Continuable hardware watchpoints are treated as non-existent if the
3219 reason we stopped wasn't a hardware watchpoint (we didn't stop on
3220 some data address). Otherwise gdb won't stop on a break instruction
3221 in the code (not from a breakpoint) when a hardware watchpoint has
3222 been defined. Also skip watchpoints which we know did not trigger
3223 (did not match the data address). */
3224
3225 if ((b->type == bp_hardware_watchpoint
3226 || b->type == bp_read_watchpoint
3227 || b->type == bp_access_watchpoint)
3228 && b->watchpoint_triggered == watch_triggered_no)
3229 return 0;
3230
3231 if (b->type == bp_hardware_breakpoint)
3232 {
3233 if (bl->address != bp_addr)
3234 return 0;
3235 if (overlay_debugging /* unmapped overlay section */
3236 && section_is_overlay (bl->section)
3237 && !section_is_mapped (bl->section))
3238 return 0;
3239 }
3240
3241 if (b->type == bp_catchpoint)
3242 {
3243 gdb_assert (b->ops != NULL && b->ops->breakpoint_hit != NULL);
3244 if (!b->ops->breakpoint_hit (b))
3245 return 0;
3246 }
3247
3248 return 1;
3249 }
3250
3251 /* If BS refers to a watchpoint, determine if the watched values
3252 has actually changed, and we should stop. If not, set BS->stop
3253 to 0. */
3254 static void
3255 bpstat_check_watchpoint (bpstat bs)
3256 {
3257 const struct bp_location *bl = bs->breakpoint_at;
3258 struct breakpoint *b = bl->owner;
3259
3260 if (b->type == bp_watchpoint
3261 || b->type == bp_read_watchpoint
3262 || b->type == bp_access_watchpoint
3263 || b->type == bp_hardware_watchpoint)
3264 {
3265 CORE_ADDR addr;
3266 struct value *v;
3267 int must_check_value = 0;
3268
3269 if (b->type == bp_watchpoint)
3270 /* For a software watchpoint, we must always check the
3271 watched value. */
3272 must_check_value = 1;
3273 else if (b->watchpoint_triggered == watch_triggered_yes)
3274 /* We have a hardware watchpoint (read, write, or access)
3275 and the target earlier reported an address watched by
3276 this watchpoint. */
3277 must_check_value = 1;
3278 else if (b->watchpoint_triggered == watch_triggered_unknown
3279 && b->type == bp_hardware_watchpoint)
3280 /* We were stopped by a hardware watchpoint, but the target could
3281 not report the data address. We must check the watchpoint's
3282 value. Access and read watchpoints are out of luck; without
3283 a data address, we can't figure it out. */
3284 must_check_value = 1;
3285
3286 if (must_check_value)
3287 {
3288 char *message = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3289 b->number);
3290 struct cleanup *cleanups = make_cleanup (xfree, message);
3291 int e = catch_errors (watchpoint_check, bs, message,
3292 RETURN_MASK_ALL);
3293 do_cleanups (cleanups);
3294 switch (e)
3295 {
3296 case WP_DELETED:
3297 /* We've already printed what needs to be printed. */
3298 bs->print_it = print_it_done;
3299 /* Stop. */
3300 break;
3301 case WP_VALUE_CHANGED:
3302 if (b->type == bp_read_watchpoint)
3303 {
3304 /* Don't stop: read watchpoints shouldn't fire if
3305 the value has changed. This is for targets
3306 which cannot set read-only watchpoints. */
3307 bs->print_it = print_it_noop;
3308 bs->stop = 0;
3309 }
3310 break;
3311 case WP_VALUE_NOT_CHANGED:
3312 if (b->type == bp_hardware_watchpoint
3313 || b->type == bp_watchpoint)
3314 {
3315 /* Don't stop: write watchpoints shouldn't fire if
3316 the value hasn't changed. */
3317 bs->print_it = print_it_noop;
3318 bs->stop = 0;
3319 }
3320 /* Stop. */
3321 break;
3322 default:
3323 /* Can't happen. */
3324 case 0:
3325 /* Error from catch_errors. */
3326 printf_filtered (_("Watchpoint %d deleted.\n"), b->number);
3327 if (b->related_breakpoint)
3328 b->related_breakpoint->disposition = disp_del_at_next_stop;
3329 b->disposition = disp_del_at_next_stop;
3330 /* We've already printed what needs to be printed. */
3331 bs->print_it = print_it_done;
3332 break;
3333 }
3334 }
3335 else /* must_check_value == 0 */
3336 {
3337 /* This is a case where some watchpoint(s) triggered, but
3338 not at the address of this watchpoint, or else no
3339 watchpoint triggered after all. So don't print
3340 anything for this watchpoint. */
3341 bs->print_it = print_it_noop;
3342 bs->stop = 0;
3343 }
3344 }
3345 }
3346
3347
3348 /* Check conditions (condition proper, frame, thread and ignore count)
3349 of breakpoint referred to by BS. If we should not stop for this
3350 breakpoint, set BS->stop to 0. */
3351 static void
3352 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
3353 {
3354 int thread_id = pid_to_thread_id (ptid);
3355 const struct bp_location *bl = bs->breakpoint_at;
3356 struct breakpoint *b = bl->owner;
3357
3358 if (frame_id_p (b->frame_id)
3359 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
3360 bs->stop = 0;
3361 else if (bs->stop)
3362 {
3363 int value_is_zero = 0;
3364
3365 /* If this is a scope breakpoint, mark the associated
3366 watchpoint as triggered so that we will handle the
3367 out-of-scope event. We'll get to the watchpoint next
3368 iteration. */
3369 if (b->type == bp_watchpoint_scope)
3370 b->related_breakpoint->watchpoint_triggered = watch_triggered_yes;
3371
3372 if (bl->cond && bl->owner->disposition != disp_del_at_next_stop)
3373 {
3374 /* We use value_mark and value_free_to_mark because it could
3375 be a long time before we return to the command level and
3376 call free_all_values. We can't call free_all_values
3377 because we might be in the middle of evaluating a
3378 function call. */
3379 struct value *mark = value_mark ();
3380
3381 /* Need to select the frame, with all that implies so that
3382 the conditions will have the right context. Because we
3383 use the frame, we will not see an inlined function's
3384 variables when we arrive at a breakpoint at the start
3385 of the inlined function; the current frame will be the
3386 call site. */
3387 select_frame (get_current_frame ());
3388 value_is_zero
3389 = catch_errors (breakpoint_cond_eval, (bl->cond),
3390 "Error in testing breakpoint condition:\n",
3391 RETURN_MASK_ALL);
3392 /* FIXME-someday, should give breakpoint # */
3393 value_free_to_mark (mark);
3394 }
3395 if (bl->cond && value_is_zero)
3396 {
3397 bs->stop = 0;
3398 }
3399 else if (b->thread != -1 && b->thread != thread_id)
3400 {
3401 bs->stop = 0;
3402 }
3403 else if (b->ignore_count > 0)
3404 {
3405 b->ignore_count--;
3406 annotate_ignore_count_change ();
3407 bs->stop = 0;
3408 /* Increase the hit count even though we don't
3409 stop. */
3410 ++(b->hit_count);
3411 }
3412 }
3413 }
3414
3415
3416 /* Get a bpstat associated with having just stopped at address
3417 BP_ADDR in thread PTID.
3418
3419 Determine whether we stopped at a breakpoint, etc, or whether we
3420 don't understand this stop. Result is a chain of bpstat's such that:
3421
3422 if we don't understand the stop, the result is a null pointer.
3423
3424 if we understand why we stopped, the result is not null.
3425
3426 Each element of the chain refers to a particular breakpoint or
3427 watchpoint at which we have stopped. (We may have stopped for
3428 several reasons concurrently.)
3429
3430 Each element of the chain has valid next, breakpoint_at,
3431 commands, FIXME??? fields. */
3432
3433 bpstat
3434 bpstat_stop_status (struct address_space *aspace,
3435 CORE_ADDR bp_addr, ptid_t ptid)
3436 {
3437 struct breakpoint *b = NULL;
3438 struct bp_location *bl, **blp_tmp;
3439 struct bp_location *loc;
3440 /* Root of the chain of bpstat's */
3441 struct bpstats root_bs[1];
3442 /* Pointer to the last thing in the chain currently. */
3443 bpstat bs = root_bs;
3444 int ix;
3445 int need_remove_insert, update_locations = 0;
3446
3447 ALL_BP_LOCATIONS (bl, blp_tmp)
3448 {
3449 b = bl->owner;
3450 gdb_assert (b);
3451 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
3452 continue;
3453
3454 /* For hardware watchpoints, we look only at the first location.
3455 The watchpoint_check function will work on entire expression,
3456 not the individual locations. For read watchopints, the
3457 watchpoints_triggered function have checked all locations
3458 already. */
3459 if (b->type == bp_hardware_watchpoint && bl != b->loc)
3460 continue;
3461
3462 if (!bpstat_check_location (bl, aspace, bp_addr))
3463 continue;
3464
3465 /* Come here if it's a watchpoint, or if the break address matches */
3466
3467 bs = bpstat_alloc (bl, bs); /* Alloc a bpstat to explain stop */
3468
3469 /* Assume we stop. Should we find watchpoint that is not actually
3470 triggered, or if condition of breakpoint is false, we'll reset
3471 'stop' to 0. */
3472 bs->stop = 1;
3473 bs->print = 1;
3474
3475 bpstat_check_watchpoint (bs);
3476 if (!bs->stop)
3477 continue;
3478
3479 if (b->type == bp_thread_event || b->type == bp_overlay_event
3480 || b->type == bp_longjmp_master)
3481 /* We do not stop for these. */
3482 bs->stop = 0;
3483 else
3484 bpstat_check_breakpoint_conditions (bs, ptid);
3485
3486 if (bs->stop)
3487 {
3488 if (b->enable_state != bp_disabled)
3489 ++(b->hit_count);
3490
3491 /* We will stop here */
3492 if (b->disposition == disp_disable)
3493 {
3494 if (b->enable_state != bp_permanent)
3495 b->enable_state = bp_disabled;
3496 update_locations = 1;
3497 }
3498 if (b->silent)
3499 bs->print = 0;
3500 bs->commands = b->commands;
3501 if (bs->commands
3502 && (strcmp ("silent", bs->commands->line) == 0
3503 || (xdb_commands && strcmp ("Q", bs->commands->line) == 0)))
3504 {
3505 bs->commands = bs->commands->next;
3506 bs->print = 0;
3507 }
3508 bs->commands = copy_command_lines (bs->commands);
3509 }
3510
3511 /* Print nothing for this entry if we dont stop or if we dont print. */
3512 if (bs->stop == 0 || bs->print == 0)
3513 bs->print_it = print_it_noop;
3514 }
3515
3516 /* Delay this call which would break the ALL_BP_LOCATIONS iteration above. */
3517 if (update_locations)
3518 update_global_location_list (0);
3519
3520 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3521 {
3522 if (breakpoint_address_match (loc->pspace->aspace, loc->address,
3523 aspace, bp_addr))
3524 {
3525 bs = bpstat_alloc (loc, bs);
3526 /* For hits of moribund locations, we should just proceed. */
3527 bs->stop = 0;
3528 bs->print = 0;
3529 bs->print_it = print_it_noop;
3530 }
3531 }
3532
3533 bs->next = NULL; /* Terminate the chain */
3534 bs = root_bs->next; /* Re-grab the head of the chain */
3535
3536 /* If we aren't stopping, the value of some hardware watchpoint may
3537 not have changed, but the intermediate memory locations we are
3538 watching may have. Don't bother if we're stopping; this will get
3539 done later. */
3540 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3541 if (bs->stop)
3542 break;
3543
3544 need_remove_insert = 0;
3545 if (bs == NULL)
3546 for (bs = root_bs->next; bs != NULL; bs = bs->next)
3547 if (!bs->stop
3548 && bs->breakpoint_at->owner
3549 && (bs->breakpoint_at->owner->type == bp_hardware_watchpoint
3550 || bs->breakpoint_at->owner->type == bp_read_watchpoint
3551 || bs->breakpoint_at->owner->type == bp_access_watchpoint))
3552 {
3553 /* remove/insert can invalidate bs->breakpoint_at, if this
3554 location is no longer used by the watchpoint. Prevent
3555 further code from trying to use it. */
3556 bs->breakpoint_at = NULL;
3557 need_remove_insert = 1;
3558 }
3559
3560 if (need_remove_insert)
3561 {
3562 remove_breakpoints ();
3563 insert_breakpoints ();
3564 }
3565
3566 return root_bs->next;
3567 }
3568 \f
3569 /* Tell what to do about this bpstat. */
3570 struct bpstat_what
3571 bpstat_what (bpstat bs)
3572 {
3573 /* Classify each bpstat as one of the following. */
3574 enum class
3575 {
3576 /* This bpstat element has no effect on the main_action. */
3577 no_effect = 0,
3578
3579 /* There was a watchpoint, stop but don't print. */
3580 wp_silent,
3581
3582 /* There was a watchpoint, stop and print. */
3583 wp_noisy,
3584
3585 /* There was a breakpoint but we're not stopping. */
3586 bp_nostop,
3587
3588 /* There was a breakpoint, stop but don't print. */
3589 bp_silent,
3590
3591 /* There was a breakpoint, stop and print. */
3592 bp_noisy,
3593
3594 /* We hit the longjmp breakpoint. */
3595 long_jump,
3596
3597 /* We hit the longjmp_resume breakpoint. */
3598 long_resume,
3599
3600 /* We hit the step_resume breakpoint. */
3601 step_resume,
3602
3603 /* We hit the shared library event breakpoint. */
3604 shlib_event,
3605
3606 /* We hit the jit event breakpoint. */
3607 jit_event,
3608
3609 /* This is just used to count how many enums there are. */
3610 class_last
3611 };
3612
3613 /* Here is the table which drives this routine. So that we can
3614 format it pretty, we define some abbreviations for the
3615 enum bpstat_what codes. */
3616 #define kc BPSTAT_WHAT_KEEP_CHECKING
3617 #define ss BPSTAT_WHAT_STOP_SILENT
3618 #define sn BPSTAT_WHAT_STOP_NOISY
3619 #define sgl BPSTAT_WHAT_SINGLE
3620 #define slr BPSTAT_WHAT_SET_LONGJMP_RESUME
3621 #define clr BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
3622 #define sr BPSTAT_WHAT_STEP_RESUME
3623 #define shl BPSTAT_WHAT_CHECK_SHLIBS
3624 #define jit BPSTAT_WHAT_CHECK_JIT
3625
3626 /* "Can't happen." Might want to print an error message.
3627 abort() is not out of the question, but chances are GDB is just
3628 a bit confused, not unusable. */
3629 #define err BPSTAT_WHAT_STOP_NOISY
3630
3631 /* Given an old action and a class, come up with a new action. */
3632 /* One interesting property of this table is that wp_silent is the same
3633 as bp_silent and wp_noisy is the same as bp_noisy. That is because
3634 after stopping, the check for whether to step over a breakpoint
3635 (BPSTAT_WHAT_SINGLE type stuff) is handled in proceed() without
3636 reference to how we stopped. We retain separate wp_silent and
3637 bp_silent codes in case we want to change that someday.
3638
3639 Another possibly interesting property of this table is that
3640 there's a partial ordering, priority-like, of the actions. Once
3641 you've decided that some action is appropriate, you'll never go
3642 back and decide something of a lower priority is better. The
3643 ordering is:
3644
3645 kc < jit clr sgl shl slr sn sr ss
3646 sgl < jit shl slr sn sr ss
3647 slr < jit err shl sn sr ss
3648 clr < jit err shl sn sr ss
3649 ss < jit shl sn sr
3650 sn < jit shl sr
3651 jit < shl sr
3652 shl < sr
3653 sr <
3654
3655 What I think this means is that we don't need a damned table
3656 here. If you just put the rows and columns in the right order,
3657 it'd look awfully regular. We could simply walk the bpstat list
3658 and choose the highest priority action we find, with a little
3659 logic to handle the 'err' cases. */
3660
3661 /* step_resume entries: a step resume breakpoint overrides another
3662 breakpoint of signal handling (see comment in wait_for_inferior
3663 at where we set the step_resume breakpoint). */
3664
3665 static const enum bpstat_what_main_action
3666 table[(int) class_last][(int) BPSTAT_WHAT_LAST] =
3667 {
3668 /* old action */
3669 /* kc ss sn sgl slr clr sr shl jit */
3670 /* no_effect */ {kc, ss, sn, sgl, slr, clr, sr, shl, jit},
3671 /* wp_silent */ {ss, ss, sn, ss, ss, ss, sr, shl, jit},
3672 /* wp_noisy */ {sn, sn, sn, sn, sn, sn, sr, shl, jit},
3673 /* bp_nostop */ {sgl, ss, sn, sgl, slr, slr, sr, shl, jit},
3674 /* bp_silent */ {ss, ss, sn, ss, ss, ss, sr, shl, jit},
3675 /* bp_noisy */ {sn, sn, sn, sn, sn, sn, sr, shl, jit},
3676 /* long_jump */ {slr, ss, sn, slr, slr, err, sr, shl, jit},
3677 /* long_resume */ {clr, ss, sn, err, err, err, sr, shl, jit},
3678 /* step_resume */ {sr, sr, sr, sr, sr, sr, sr, sr, sr },
3679 /* shlib */ {shl, shl, shl, shl, shl, shl, sr, shl, shl},
3680 /* jit_event */ {jit, jit, jit, jit, jit, jit, sr, jit, jit}
3681 };
3682
3683 #undef kc
3684 #undef ss
3685 #undef sn
3686 #undef sgl
3687 #undef slr
3688 #undef clr
3689 #undef err
3690 #undef sr
3691 #undef ts
3692 #undef shl
3693 #undef jit
3694 enum bpstat_what_main_action current_action = BPSTAT_WHAT_KEEP_CHECKING;
3695 struct bpstat_what retval;
3696
3697 retval.call_dummy = 0;
3698 for (; bs != NULL; bs = bs->next)
3699 {
3700 enum class bs_class = no_effect;
3701 if (bs->breakpoint_at == NULL)
3702 /* I suspect this can happen if it was a momentary breakpoint
3703 which has since been deleted. */
3704 continue;
3705 if (bs->breakpoint_at->owner == NULL)
3706 bs_class = bp_nostop;
3707 else
3708 switch (bs->breakpoint_at->owner->type)
3709 {
3710 case bp_none:
3711 continue;
3712
3713 case bp_breakpoint:
3714 case bp_hardware_breakpoint:
3715 case bp_until:
3716 case bp_finish:
3717 if (bs->stop)
3718 {
3719 if (bs->print)
3720 bs_class = bp_noisy;
3721 else
3722 bs_class = bp_silent;
3723 }
3724 else
3725 bs_class = bp_nostop;
3726 break;
3727 case bp_watchpoint:
3728 case bp_hardware_watchpoint:
3729 case bp_read_watchpoint:
3730 case bp_access_watchpoint:
3731 if (bs->stop)
3732 {
3733 if (bs->print)
3734 bs_class = wp_noisy;
3735 else
3736 bs_class = wp_silent;
3737 }
3738 else
3739 /* There was a watchpoint, but we're not stopping.
3740 This requires no further action. */
3741 bs_class = no_effect;
3742 break;
3743 case bp_longjmp:
3744 bs_class = long_jump;
3745 break;
3746 case bp_longjmp_resume:
3747 bs_class = long_resume;
3748 break;
3749 case bp_step_resume:
3750 if (bs->stop)
3751 {
3752 bs_class = step_resume;
3753 }
3754 else
3755 /* It is for the wrong frame. */
3756 bs_class = bp_nostop;
3757 break;
3758 case bp_watchpoint_scope:
3759 bs_class = bp_nostop;
3760 break;
3761 case bp_shlib_event:
3762 bs_class = shlib_event;
3763 break;
3764 case bp_jit_event:
3765 bs_class = jit_event;
3766 break;
3767 case bp_thread_event:
3768 case bp_overlay_event:
3769 case bp_longjmp_master:
3770 bs_class = bp_nostop;
3771 break;
3772 case bp_catchpoint:
3773 if (bs->stop)
3774 {
3775 if (bs->print)
3776 bs_class = bp_noisy;
3777 else
3778 bs_class = bp_silent;
3779 }
3780 else
3781 /* There was a catchpoint, but we're not stopping.
3782 This requires no further action. */
3783 bs_class = no_effect;
3784 break;
3785 case bp_call_dummy:
3786 /* Make sure the action is stop (silent or noisy),
3787 so infrun.c pops the dummy frame. */
3788 bs_class = bp_silent;
3789 retval.call_dummy = 1;
3790 break;
3791 case bp_tracepoint:
3792 /* Tracepoint hits should not be reported back to GDB, and
3793 if one got through somehow, it should have been filtered
3794 out already. */
3795 internal_error (__FILE__, __LINE__,
3796 _("bpstat_what: bp_tracepoint encountered"));
3797 break;
3798 }
3799 current_action = table[(int) bs_class][(int) current_action];
3800 }
3801 retval.main_action = current_action;
3802 return retval;
3803 }
3804
3805 /* Nonzero if we should step constantly (e.g. watchpoints on machines
3806 without hardware support). This isn't related to a specific bpstat,
3807 just to things like whether watchpoints are set. */
3808
3809 int
3810 bpstat_should_step (void)
3811 {
3812 struct breakpoint *b;
3813 ALL_BREAKPOINTS (b)
3814 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3815 return 1;
3816 return 0;
3817 }
3818
3819 \f
3820
3821 static void print_breakpoint_location (struct breakpoint *b,
3822 struct bp_location *loc,
3823 char *wrap_indent,
3824 struct ui_stream *stb)
3825 {
3826 struct cleanup *old_chain = save_current_program_space ();
3827
3828 if (loc != NULL)
3829 set_current_program_space (loc->pspace);
3830
3831 if (b->source_file)
3832 {
3833 struct symbol *sym
3834 = find_pc_sect_function (loc->address, loc->section);
3835 if (sym)
3836 {
3837 ui_out_text (uiout, "in ");
3838 ui_out_field_string (uiout, "func",
3839 SYMBOL_PRINT_NAME (sym));
3840 ui_out_wrap_hint (uiout, wrap_indent);
3841 ui_out_text (uiout, " at ");
3842 }
3843 ui_out_field_string (uiout, "file", b->source_file);
3844 ui_out_text (uiout, ":");
3845
3846 if (ui_out_is_mi_like_p (uiout))
3847 {
3848 struct symtab_and_line sal = find_pc_line (loc->address, 0);
3849 char *fullname = symtab_to_fullname (sal.symtab);
3850
3851 if (fullname)
3852 ui_out_field_string (uiout, "fullname", fullname);
3853 }
3854
3855 ui_out_field_int (uiout, "line", b->line_number);
3856 }
3857 else if (!b->loc)
3858 {
3859 ui_out_field_string (uiout, "pending", b->addr_string);
3860 }
3861 else
3862 {
3863 print_address_symbolic (loc->address, stb->stream, demangle, "");
3864 ui_out_field_stream (uiout, "at", stb);
3865 }
3866
3867 do_cleanups (old_chain);
3868 }
3869
3870 /* Print B to gdb_stdout. */
3871 static void
3872 print_one_breakpoint_location (struct breakpoint *b,
3873 struct bp_location *loc,
3874 int loc_number,
3875 struct bp_location **last_loc,
3876 int print_address_bits,
3877 int allflag)
3878 {
3879 struct command_line *l;
3880 struct symbol *sym;
3881 struct ep_type_description
3882 {
3883 enum bptype type;
3884 char *description;
3885 };
3886 static struct ep_type_description bptypes[] =
3887 {
3888 {bp_none, "?deleted?"},
3889 {bp_breakpoint, "breakpoint"},
3890 {bp_hardware_breakpoint, "hw breakpoint"},
3891 {bp_until, "until"},
3892 {bp_finish, "finish"},
3893 {bp_watchpoint, "watchpoint"},
3894 {bp_hardware_watchpoint, "hw watchpoint"},
3895 {bp_read_watchpoint, "read watchpoint"},
3896 {bp_access_watchpoint, "acc watchpoint"},
3897 {bp_longjmp, "longjmp"},
3898 {bp_longjmp_resume, "longjmp resume"},
3899 {bp_step_resume, "step resume"},
3900 {bp_watchpoint_scope, "watchpoint scope"},
3901 {bp_call_dummy, "call dummy"},
3902 {bp_shlib_event, "shlib events"},
3903 {bp_thread_event, "thread events"},
3904 {bp_overlay_event, "overlay events"},
3905 {bp_longjmp_master, "longjmp master"},
3906 {bp_catchpoint, "catchpoint"},
3907 {bp_tracepoint, "tracepoint"},
3908 {bp_jit_event, "jit events"},
3909 };
3910
3911 static char bpenables[] = "nynny";
3912 char wrap_indent[80];
3913 struct ui_stream *stb = ui_out_stream_new (uiout);
3914 struct cleanup *old_chain = make_cleanup_ui_out_stream_delete (stb);
3915 struct cleanup *bkpt_chain;
3916
3917 int header_of_multiple = 0;
3918 int part_of_multiple = (loc != NULL);
3919 struct value_print_options opts;
3920
3921 get_user_print_options (&opts);
3922
3923 gdb_assert (!loc || loc_number != 0);
3924 /* See comment in print_one_breakpoint concerning
3925 treatment of breakpoints with single disabled
3926 location. */
3927 if (loc == NULL
3928 && (b->loc != NULL
3929 && (b->loc->next != NULL || !b->loc->enabled)))
3930 header_of_multiple = 1;
3931 if (loc == NULL)
3932 loc = b->loc;
3933
3934 annotate_record ();
3935 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
3936
3937 /* 1 */
3938 annotate_field (0);
3939 if (part_of_multiple)
3940 {
3941 char *formatted;
3942 formatted = xstrprintf ("%d.%d", b->number, loc_number);
3943 ui_out_field_string (uiout, "number", formatted);
3944 xfree (formatted);
3945 }
3946 else
3947 {
3948 ui_out_field_int (uiout, "number", b->number);
3949 }
3950
3951 /* 2 */
3952 annotate_field (1);
3953 if (part_of_multiple)
3954 ui_out_field_skip (uiout, "type");
3955 else
3956 {
3957 if (((int) b->type >= (sizeof (bptypes) / sizeof (bptypes[0])))
3958 || ((int) b->type != bptypes[(int) b->type].type))
3959 internal_error (__FILE__, __LINE__,
3960 _("bptypes table does not describe type #%d."),
3961 (int) b->type);
3962 ui_out_field_string (uiout, "type", bptypes[(int) b->type].description);
3963 }
3964
3965 /* 3 */
3966 annotate_field (2);
3967 if (part_of_multiple)
3968 ui_out_field_skip (uiout, "disp");
3969 else
3970 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
3971
3972
3973 /* 4 */
3974 annotate_field (3);
3975 if (part_of_multiple)
3976 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
3977 else
3978 ui_out_field_fmt (uiout, "enabled", "%c",
3979 bpenables[(int) b->enable_state]);
3980 ui_out_spaces (uiout, 2);
3981
3982
3983 /* 5 and 6 */
3984 strcpy (wrap_indent, " ");
3985 if (opts.addressprint)
3986 {
3987 if (print_address_bits <= 32)
3988 strcat (wrap_indent, " ");
3989 else
3990 strcat (wrap_indent, " ");
3991 }
3992
3993 if (b->ops != NULL && b->ops->print_one != NULL)
3994 {
3995 /* Although the print_one can possibly print
3996 all locations, calling it here is not likely
3997 to get any nice result. So, make sure there's
3998 just one location. */
3999 gdb_assert (b->loc == NULL || b->loc->next == NULL);
4000 b->ops->print_one (b, last_loc);
4001 }
4002 else
4003 switch (b->type)
4004 {
4005 case bp_none:
4006 internal_error (__FILE__, __LINE__,
4007 _("print_one_breakpoint: bp_none encountered\n"));
4008 break;
4009
4010 case bp_watchpoint:
4011 case bp_hardware_watchpoint:
4012 case bp_read_watchpoint:
4013 case bp_access_watchpoint:
4014 /* Field 4, the address, is omitted (which makes the columns
4015 not line up too nicely with the headers, but the effect
4016 is relatively readable). */
4017 if (opts.addressprint)
4018 ui_out_field_skip (uiout, "addr");
4019 annotate_field (5);
4020 ui_out_field_string (uiout, "what", b->exp_string);
4021 break;
4022
4023 case bp_breakpoint:
4024 case bp_hardware_breakpoint:
4025 case bp_until:
4026 case bp_finish:
4027 case bp_longjmp:
4028 case bp_longjmp_resume:
4029 case bp_step_resume:
4030 case bp_watchpoint_scope:
4031 case bp_call_dummy:
4032 case bp_shlib_event:
4033 case bp_thread_event:
4034 case bp_overlay_event:
4035 case bp_longjmp_master:
4036 case bp_tracepoint:
4037 case bp_jit_event:
4038 if (opts.addressprint)
4039 {
4040 annotate_field (4);
4041 if (header_of_multiple)
4042 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
4043 else if (b->loc == NULL || loc->shlib_disabled)
4044 ui_out_field_string (uiout, "addr", "<PENDING>");
4045 else
4046 ui_out_field_core_addr (uiout, "addr",
4047 loc->gdbarch, loc->address);
4048 }
4049 annotate_field (5);
4050 if (!header_of_multiple)
4051 print_breakpoint_location (b, loc, wrap_indent, stb);
4052 if (b->loc)
4053 *last_loc = b->loc;
4054 break;
4055 }
4056
4057
4058 /* For backward compatibility, don't display inferiors unless there
4059 are several. */
4060 if (loc != NULL
4061 && !header_of_multiple
4062 && (allflag
4063 || (!gdbarch_has_global_breakpoints (target_gdbarch)
4064 && (number_of_program_spaces () > 1
4065 || number_of_inferiors () > 1)
4066 && loc->owner->type != bp_catchpoint)))
4067 {
4068 struct inferior *inf;
4069 int first = 1;
4070
4071 for (inf = inferior_list; inf != NULL; inf = inf->next)
4072 {
4073 if (inf->pspace == loc->pspace)
4074 {
4075 if (first)
4076 {
4077 first = 0;
4078 ui_out_text (uiout, " inf ");
4079 }
4080 else
4081 ui_out_text (uiout, ", ");
4082 ui_out_text (uiout, plongest (inf->num));
4083 }
4084 }
4085 }
4086
4087 if (!part_of_multiple)
4088 {
4089 if (b->thread != -1)
4090 {
4091 /* FIXME: This seems to be redundant and lost here; see the
4092 "stop only in" line a little further down. */
4093 ui_out_text (uiout, " thread ");
4094 ui_out_field_int (uiout, "thread", b->thread);
4095 }
4096 else if (b->task != 0)
4097 {
4098 ui_out_text (uiout, " task ");
4099 ui_out_field_int (uiout, "task", b->task);
4100 }
4101 }
4102
4103 ui_out_text (uiout, "\n");
4104
4105 if (part_of_multiple && frame_id_p (b->frame_id))
4106 {
4107 annotate_field (6);
4108 ui_out_text (uiout, "\tstop only in stack frame at ");
4109 /* FIXME: cagney/2002-12-01: Shouldn't be poeking around inside
4110 the frame ID. */
4111 ui_out_field_core_addr (uiout, "frame",
4112 b->gdbarch, b->frame_id.stack_addr);
4113 ui_out_text (uiout, "\n");
4114 }
4115
4116 if (!part_of_multiple && b->cond_string && !ada_exception_catchpoint_p (b))
4117 {
4118 /* We do not print the condition for Ada exception catchpoints
4119 because the condition is an internal implementation detail
4120 that we do not want to expose to the user. */
4121 annotate_field (7);
4122 if (b->type == bp_tracepoint)
4123 ui_out_text (uiout, "\ttrace only if ");
4124 else
4125 ui_out_text (uiout, "\tstop only if ");
4126 ui_out_field_string (uiout, "cond", b->cond_string);
4127 ui_out_text (uiout, "\n");
4128 }
4129
4130 if (!part_of_multiple && b->thread != -1)
4131 {
4132 /* FIXME should make an annotation for this */
4133 ui_out_text (uiout, "\tstop only in thread ");
4134 ui_out_field_int (uiout, "thread", b->thread);
4135 ui_out_text (uiout, "\n");
4136 }
4137
4138 if (!part_of_multiple && b->hit_count)
4139 {
4140 /* FIXME should make an annotation for this */
4141 if (ep_is_catchpoint (b))
4142 ui_out_text (uiout, "\tcatchpoint");
4143 else
4144 ui_out_text (uiout, "\tbreakpoint");
4145 ui_out_text (uiout, " already hit ");
4146 ui_out_field_int (uiout, "times", b->hit_count);
4147 if (b->hit_count == 1)
4148 ui_out_text (uiout, " time\n");
4149 else
4150 ui_out_text (uiout, " times\n");
4151 }
4152
4153 /* Output the count also if it is zero, but only if this is
4154 mi. FIXME: Should have a better test for this. */
4155 if (ui_out_is_mi_like_p (uiout))
4156 if (!part_of_multiple && b->hit_count == 0)
4157 ui_out_field_int (uiout, "times", b->hit_count);
4158
4159 if (!part_of_multiple && b->ignore_count)
4160 {
4161 annotate_field (8);
4162 ui_out_text (uiout, "\tignore next ");
4163 ui_out_field_int (uiout, "ignore", b->ignore_count);
4164 ui_out_text (uiout, " hits\n");
4165 }
4166
4167 l = b->commands;
4168 if (!part_of_multiple && l)
4169 {
4170 struct cleanup *script_chain;
4171
4172 annotate_field (9);
4173 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
4174 print_command_lines (uiout, l, 4);
4175 do_cleanups (script_chain);
4176 }
4177
4178 if (!part_of_multiple && b->pass_count)
4179 {
4180 annotate_field (10);
4181 ui_out_text (uiout, "\tpass count ");
4182 ui_out_field_int (uiout, "pass", b->pass_count);
4183 ui_out_text (uiout, " \n");
4184 }
4185
4186 if (!part_of_multiple && b->step_count)
4187 {
4188 annotate_field (11);
4189 ui_out_text (uiout, "\tstep count ");
4190 ui_out_field_int (uiout, "step", b->step_count);
4191 ui_out_text (uiout, " \n");
4192 }
4193
4194 if (!part_of_multiple && b->actions)
4195 {
4196 struct action_line *action;
4197 annotate_field (12);
4198 for (action = b->actions; action; action = action->next)
4199 {
4200 ui_out_text (uiout, " A\t");
4201 ui_out_text (uiout, action->action);
4202 ui_out_text (uiout, "\n");
4203 }
4204 }
4205
4206 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
4207 {
4208 if (b->addr_string)
4209 ui_out_field_string (uiout, "original-location", b->addr_string);
4210 else if (b->exp_string)
4211 ui_out_field_string (uiout, "original-location", b->exp_string);
4212 }
4213
4214 do_cleanups (bkpt_chain);
4215 do_cleanups (old_chain);
4216 }
4217
4218 static void
4219 print_one_breakpoint (struct breakpoint *b,
4220 struct bp_location **last_loc, int print_address_bits,
4221 int allflag)
4222 {
4223 print_one_breakpoint_location (b, NULL, 0, last_loc,
4224 print_address_bits, allflag);
4225
4226 /* If this breakpoint has custom print function,
4227 it's already printed. Otherwise, print individual
4228 locations, if any. */
4229 if (b->ops == NULL || b->ops->print_one == NULL)
4230 {
4231 /* If breakpoint has a single location that is
4232 disabled, we print it as if it had
4233 several locations, since otherwise it's hard to
4234 represent "breakpoint enabled, location disabled"
4235 situation.
4236 Note that while hardware watchpoints have
4237 several locations internally, that's no a property
4238 exposed to user. */
4239 if (b->loc
4240 && !is_hardware_watchpoint (b)
4241 && (b->loc->next || !b->loc->enabled)
4242 && !ui_out_is_mi_like_p (uiout))
4243 {
4244 struct bp_location *loc;
4245 int n = 1;
4246 for (loc = b->loc; loc; loc = loc->next, ++n)
4247 print_one_breakpoint_location (b, loc, n, last_loc,
4248 print_address_bits, allflag);
4249 }
4250 }
4251 }
4252
4253 static int
4254 breakpoint_address_bits (struct breakpoint *b)
4255 {
4256 int print_address_bits = 0;
4257 struct bp_location *loc;
4258
4259 for (loc = b->loc; loc; loc = loc->next)
4260 {
4261 int addr_bit = gdbarch_addr_bit (b->gdbarch);
4262 if (addr_bit > print_address_bits)
4263 print_address_bits = addr_bit;
4264 }
4265
4266 return print_address_bits;
4267 }
4268
4269 struct captured_breakpoint_query_args
4270 {
4271 int bnum;
4272 };
4273
4274 static int
4275 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
4276 {
4277 struct captured_breakpoint_query_args *args = data;
4278 struct breakpoint *b;
4279 struct bp_location *dummy_loc = NULL;
4280 ALL_BREAKPOINTS (b)
4281 {
4282 if (args->bnum == b->number)
4283 {
4284 int print_address_bits = breakpoint_address_bits (b);
4285 print_one_breakpoint (b, &dummy_loc, print_address_bits, 0);
4286 return GDB_RC_OK;
4287 }
4288 }
4289 return GDB_RC_NONE;
4290 }
4291
4292 enum gdb_rc
4293 gdb_breakpoint_query (struct ui_out *uiout, int bnum, char **error_message)
4294 {
4295 struct captured_breakpoint_query_args args;
4296 args.bnum = bnum;
4297 /* For the moment we don't trust print_one_breakpoint() to not throw
4298 an error. */
4299 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
4300 error_message, RETURN_MASK_ALL) < 0)
4301 return GDB_RC_FAIL;
4302 else
4303 return GDB_RC_OK;
4304 }
4305
4306 /* Return non-zero if B is user settable (breakpoints, watchpoints,
4307 catchpoints, et.al.). */
4308
4309 static int
4310 user_settable_breakpoint (const struct breakpoint *b)
4311 {
4312 return (b->type == bp_breakpoint
4313 || b->type == bp_catchpoint
4314 || b->type == bp_hardware_breakpoint
4315 || b->type == bp_tracepoint
4316 || b->type == bp_watchpoint
4317 || b->type == bp_read_watchpoint
4318 || b->type == bp_access_watchpoint
4319 || b->type == bp_hardware_watchpoint);
4320 }
4321
4322 /* Print information on user settable breakpoint (watchpoint, etc)
4323 number BNUM. If BNUM is -1 print all user settable breakpoints.
4324 If ALLFLAG is non-zero, include non- user settable breakpoints. */
4325
4326 static void
4327 breakpoint_1 (int bnum, int allflag)
4328 {
4329 struct breakpoint *b;
4330 struct bp_location *last_loc = NULL;
4331 int nr_printable_breakpoints;
4332 struct cleanup *bkpttbl_chain;
4333 struct value_print_options opts;
4334 int print_address_bits = 0;
4335
4336 get_user_print_options (&opts);
4337
4338 /* Compute the number of rows in the table, as well as the
4339 size required for address fields. */
4340 nr_printable_breakpoints = 0;
4341 ALL_BREAKPOINTS (b)
4342 if (bnum == -1
4343 || bnum == b->number)
4344 {
4345 if (allflag || user_settable_breakpoint (b))
4346 {
4347 int addr_bit = breakpoint_address_bits (b);
4348 if (addr_bit > print_address_bits)
4349 print_address_bits = addr_bit;
4350
4351 nr_printable_breakpoints++;
4352 }
4353 }
4354
4355 if (opts.addressprint)
4356 bkpttbl_chain
4357 = make_cleanup_ui_out_table_begin_end (uiout, 6, nr_printable_breakpoints,
4358 "BreakpointTable");
4359 else
4360 bkpttbl_chain
4361 = make_cleanup_ui_out_table_begin_end (uiout, 5, nr_printable_breakpoints,
4362 "BreakpointTable");
4363
4364 if (nr_printable_breakpoints > 0)
4365 annotate_breakpoints_headers ();
4366 if (nr_printable_breakpoints > 0)
4367 annotate_field (0);
4368 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
4369 if (nr_printable_breakpoints > 0)
4370 annotate_field (1);
4371 ui_out_table_header (uiout, 14, ui_left, "type", "Type"); /* 2 */
4372 if (nr_printable_breakpoints > 0)
4373 annotate_field (2);
4374 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
4375 if (nr_printable_breakpoints > 0)
4376 annotate_field (3);
4377 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
4378 if (opts.addressprint)
4379 {
4380 if (nr_printable_breakpoints > 0)
4381 annotate_field (4);
4382 if (print_address_bits <= 32)
4383 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");/* 5 */
4384 else
4385 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");/* 5 */
4386 }
4387 if (nr_printable_breakpoints > 0)
4388 annotate_field (5);
4389 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
4390 ui_out_table_body (uiout);
4391 if (nr_printable_breakpoints > 0)
4392 annotate_breakpoints_table ();
4393
4394 ALL_BREAKPOINTS (b)
4395 if (bnum == -1
4396 || bnum == b->number)
4397 {
4398 /* We only print out user settable breakpoints unless the
4399 allflag is set. */
4400 if (allflag || user_settable_breakpoint (b))
4401 print_one_breakpoint (b, &last_loc, print_address_bits, allflag);
4402 }
4403
4404 do_cleanups (bkpttbl_chain);
4405
4406 if (nr_printable_breakpoints == 0)
4407 {
4408 if (bnum == -1)
4409 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
4410 else
4411 ui_out_message (uiout, 0, "No breakpoint or watchpoint number %d.\n",
4412 bnum);
4413 }
4414 else
4415 {
4416 if (last_loc && !server_command)
4417 set_next_address (last_loc->gdbarch, last_loc->address);
4418 }
4419
4420 /* FIXME? Should this be moved up so that it is only called when
4421 there have been breakpoints? */
4422 annotate_breakpoints_table_end ();
4423 }
4424
4425 static void
4426 breakpoints_info (char *bnum_exp, int from_tty)
4427 {
4428 int bnum = -1;
4429
4430 if (bnum_exp)
4431 bnum = parse_and_eval_long (bnum_exp);
4432
4433 breakpoint_1 (bnum, 0);
4434 }
4435
4436 static void
4437 maintenance_info_breakpoints (char *bnum_exp, int from_tty)
4438 {
4439 int bnum = -1;
4440
4441 if (bnum_exp)
4442 bnum = parse_and_eval_long (bnum_exp);
4443
4444 breakpoint_1 (bnum, 1);
4445 }
4446
4447 static int
4448 breakpoint_has_pc (struct breakpoint *b,
4449 struct program_space *pspace,
4450 CORE_ADDR pc, struct obj_section *section)
4451 {
4452 struct bp_location *bl = b->loc;
4453 for (; bl; bl = bl->next)
4454 {
4455 if (bl->pspace == pspace
4456 && bl->address == pc
4457 && (!overlay_debugging || bl->section == section))
4458 return 1;
4459 }
4460 return 0;
4461 }
4462
4463 /* Print a message describing any breakpoints set at PC. This
4464 concerns with logical breakpoints, so we match program spaces, not
4465 address spaces. */
4466
4467 static void
4468 describe_other_breakpoints (struct gdbarch *gdbarch,
4469 struct program_space *pspace, CORE_ADDR pc,
4470 struct obj_section *section, int thread)
4471 {
4472 int others = 0;
4473 struct breakpoint *b;
4474
4475 ALL_BREAKPOINTS (b)
4476 others += breakpoint_has_pc (b, pspace, pc, section);
4477 if (others > 0)
4478 {
4479 if (others == 1)
4480 printf_filtered (_("Note: breakpoint "));
4481 else /* if (others == ???) */
4482 printf_filtered (_("Note: breakpoints "));
4483 ALL_BREAKPOINTS (b)
4484 if (breakpoint_has_pc (b, pspace, pc, section))
4485 {
4486 others--;
4487 printf_filtered ("%d", b->number);
4488 if (b->thread == -1 && thread != -1)
4489 printf_filtered (" (all threads)");
4490 else if (b->thread != -1)
4491 printf_filtered (" (thread %d)", b->thread);
4492 printf_filtered ("%s%s ",
4493 ((b->enable_state == bp_disabled
4494 || b->enable_state == bp_call_disabled
4495 || b->enable_state == bp_startup_disabled)
4496 ? " (disabled)"
4497 : b->enable_state == bp_permanent
4498 ? " (permanent)"
4499 : ""),
4500 (others > 1) ? ","
4501 : ((others == 1) ? " and" : ""));
4502 }
4503 printf_filtered (_("also set at pc "));
4504 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
4505 printf_filtered (".\n");
4506 }
4507 }
4508 \f
4509 /* Set the default place to put a breakpoint
4510 for the `break' command with no arguments. */
4511
4512 void
4513 set_default_breakpoint (int valid, struct program_space *pspace,
4514 CORE_ADDR addr, struct symtab *symtab,
4515 int line)
4516 {
4517 default_breakpoint_valid = valid;
4518 default_breakpoint_pspace = pspace;
4519 default_breakpoint_address = addr;
4520 default_breakpoint_symtab = symtab;
4521 default_breakpoint_line = line;
4522 }
4523
4524 /* Return true iff it is meaningful to use the address member of
4525 BPT. For some breakpoint types, the address member is irrelevant
4526 and it makes no sense to attempt to compare it to other addresses
4527 (or use it for any other purpose either).
4528
4529 More specifically, each of the following breakpoint types will always
4530 have a zero valued address and we don't want to mark breakpoints of any of
4531 these types to be a duplicate of an actual breakpoint at address zero:
4532
4533 bp_watchpoint
4534 bp_hardware_watchpoint
4535 bp_read_watchpoint
4536 bp_access_watchpoint
4537 bp_catchpoint */
4538
4539 static int
4540 breakpoint_address_is_meaningful (struct breakpoint *bpt)
4541 {
4542 enum bptype type = bpt->type;
4543
4544 return (type != bp_watchpoint
4545 && type != bp_hardware_watchpoint
4546 && type != bp_read_watchpoint
4547 && type != bp_access_watchpoint
4548 && type != bp_catchpoint);
4549 }
4550
4551 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
4552 same breakpoint location. In most targets, this can only be true
4553 if ASPACE1 matches ASPACE2. On targets that have global
4554 breakpoints, the address space doesn't really matter. */
4555
4556 static int
4557 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
4558 struct address_space *aspace2, CORE_ADDR addr2)
4559 {
4560 return ((gdbarch_has_global_breakpoints (target_gdbarch)
4561 || aspace1 == aspace2)
4562 && addr1 == addr2);
4563 }
4564
4565 static void
4566 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
4567 int bnum, int have_bnum)
4568 {
4569 char astr1[40];
4570 char astr2[40];
4571
4572 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
4573 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
4574 if (have_bnum)
4575 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
4576 bnum, astr1, astr2);
4577 else
4578 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
4579 }
4580
4581 /* Adjust a breakpoint's address to account for architectural constraints
4582 on breakpoint placement. Return the adjusted address. Note: Very
4583 few targets require this kind of adjustment. For most targets,
4584 this function is simply the identity function. */
4585
4586 static CORE_ADDR
4587 adjust_breakpoint_address (struct gdbarch *gdbarch,
4588 CORE_ADDR bpaddr, enum bptype bptype)
4589 {
4590 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
4591 {
4592 /* Very few targets need any kind of breakpoint adjustment. */
4593 return bpaddr;
4594 }
4595 else if (bptype == bp_watchpoint
4596 || bptype == bp_hardware_watchpoint
4597 || bptype == bp_read_watchpoint
4598 || bptype == bp_access_watchpoint
4599 || bptype == bp_catchpoint)
4600 {
4601 /* Watchpoints and the various bp_catch_* eventpoints should not
4602 have their addresses modified. */
4603 return bpaddr;
4604 }
4605 else
4606 {
4607 CORE_ADDR adjusted_bpaddr;
4608
4609 /* Some targets have architectural constraints on the placement
4610 of breakpoint instructions. Obtain the adjusted address. */
4611 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
4612
4613 /* An adjusted breakpoint address can significantly alter
4614 a user's expectations. Print a warning if an adjustment
4615 is required. */
4616 if (adjusted_bpaddr != bpaddr)
4617 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
4618
4619 return adjusted_bpaddr;
4620 }
4621 }
4622
4623 /* Allocate a struct bp_location. */
4624
4625 static struct bp_location *
4626 allocate_bp_location (struct breakpoint *bpt)
4627 {
4628 struct bp_location *loc, *loc_p;
4629
4630 loc = xmalloc (sizeof (struct bp_location));
4631 memset (loc, 0, sizeof (*loc));
4632
4633 loc->owner = bpt;
4634 loc->cond = NULL;
4635 loc->shlib_disabled = 0;
4636 loc->enabled = 1;
4637
4638 switch (bpt->type)
4639 {
4640 case bp_breakpoint:
4641 case bp_tracepoint:
4642 case bp_until:
4643 case bp_finish:
4644 case bp_longjmp:
4645 case bp_longjmp_resume:
4646 case bp_step_resume:
4647 case bp_watchpoint_scope:
4648 case bp_call_dummy:
4649 case bp_shlib_event:
4650 case bp_thread_event:
4651 case bp_overlay_event:
4652 case bp_jit_event:
4653 case bp_longjmp_master:
4654 loc->loc_type = bp_loc_software_breakpoint;
4655 break;
4656 case bp_hardware_breakpoint:
4657 loc->loc_type = bp_loc_hardware_breakpoint;
4658 break;
4659 case bp_hardware_watchpoint:
4660 case bp_read_watchpoint:
4661 case bp_access_watchpoint:
4662 loc->loc_type = bp_loc_hardware_watchpoint;
4663 break;
4664 case bp_watchpoint:
4665 case bp_catchpoint:
4666 loc->loc_type = bp_loc_other;
4667 break;
4668 default:
4669 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
4670 }
4671
4672 return loc;
4673 }
4674
4675 static void free_bp_location (struct bp_location *loc)
4676 {
4677 if (loc->cond)
4678 xfree (loc->cond);
4679
4680 if (loc->function_name)
4681 xfree (loc->function_name);
4682
4683 xfree (loc);
4684 }
4685
4686 /* Helper to set_raw_breakpoint below. Creates a breakpoint
4687 that has type BPTYPE and has no locations as yet. */
4688 /* This function is used in gdbtk sources and thus can not be made static. */
4689
4690 static struct breakpoint *
4691 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
4692 enum bptype bptype)
4693 {
4694 struct breakpoint *b, *b1;
4695
4696 b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
4697 memset (b, 0, sizeof (*b));
4698
4699 b->type = bptype;
4700 b->gdbarch = gdbarch;
4701 b->language = current_language->la_language;
4702 b->input_radix = input_radix;
4703 b->thread = -1;
4704 b->enable_state = bp_enabled;
4705 b->next = 0;
4706 b->silent = 0;
4707 b->ignore_count = 0;
4708 b->commands = NULL;
4709 b->frame_id = null_frame_id;
4710 b->forked_inferior_pid = null_ptid;
4711 b->exec_pathname = NULL;
4712 b->syscalls_to_be_caught = NULL;
4713 b->ops = NULL;
4714 b->condition_not_parsed = 0;
4715
4716 /* Add this breakpoint to the end of the chain
4717 so that a list of breakpoints will come out in order
4718 of increasing numbers. */
4719
4720 b1 = breakpoint_chain;
4721 if (b1 == 0)
4722 breakpoint_chain = b;
4723 else
4724 {
4725 while (b1->next)
4726 b1 = b1->next;
4727 b1->next = b;
4728 }
4729 return b;
4730 }
4731
4732 /* Initialize loc->function_name. */
4733 static void
4734 set_breakpoint_location_function (struct bp_location *loc)
4735 {
4736 if (loc->owner->type == bp_breakpoint
4737 || loc->owner->type == bp_hardware_breakpoint
4738 || loc->owner->type == bp_tracepoint)
4739 {
4740 find_pc_partial_function (loc->address, &(loc->function_name),
4741 NULL, NULL);
4742 if (loc->function_name)
4743 loc->function_name = xstrdup (loc->function_name);
4744 }
4745 }
4746
4747 /* Attempt to determine architecture of location identified by SAL. */
4748 static struct gdbarch *
4749 get_sal_arch (struct symtab_and_line sal)
4750 {
4751 if (sal.section)
4752 return get_objfile_arch (sal.section->objfile);
4753 if (sal.symtab)
4754 return get_objfile_arch (sal.symtab->objfile);
4755
4756 return NULL;
4757 }
4758
4759 /* set_raw_breakpoint is a low level routine for allocating and
4760 partially initializing a breakpoint of type BPTYPE. The newly
4761 created breakpoint's address, section, source file name, and line
4762 number are provided by SAL. The newly created and partially
4763 initialized breakpoint is added to the breakpoint chain and
4764 is also returned as the value of this function.
4765
4766 It is expected that the caller will complete the initialization of
4767 the newly created breakpoint struct as well as output any status
4768 information regarding the creation of a new breakpoint. In
4769 particular, set_raw_breakpoint does NOT set the breakpoint
4770 number! Care should be taken to not allow an error to occur
4771 prior to completing the initialization of the breakpoint. If this
4772 should happen, a bogus breakpoint will be left on the chain. */
4773
4774 struct breakpoint *
4775 set_raw_breakpoint (struct gdbarch *gdbarch,
4776 struct symtab_and_line sal, enum bptype bptype)
4777 {
4778 struct breakpoint *b = set_raw_breakpoint_without_location (gdbarch, bptype);
4779 CORE_ADDR adjusted_address;
4780 struct gdbarch *loc_gdbarch;
4781
4782 loc_gdbarch = get_sal_arch (sal);
4783 if (!loc_gdbarch)
4784 loc_gdbarch = b->gdbarch;
4785
4786 if (bptype != bp_catchpoint)
4787 gdb_assert (sal.pspace != NULL);
4788
4789 /* Adjust the breakpoint's address prior to allocating a location.
4790 Once we call allocate_bp_location(), that mostly uninitialized
4791 location will be placed on the location chain. Adjustment of the
4792 breakpoint may cause target_read_memory() to be called and we do
4793 not want its scan of the location chain to find a breakpoint and
4794 location that's only been partially initialized. */
4795 adjusted_address = adjust_breakpoint_address (loc_gdbarch, sal.pc, b->type);
4796
4797 b->loc = allocate_bp_location (b);
4798 b->loc->gdbarch = loc_gdbarch;
4799 b->loc->requested_address = sal.pc;
4800 b->loc->address = adjusted_address;
4801 b->loc->pspace = sal.pspace;
4802
4803 /* Store the program space that was used to set the breakpoint, for
4804 breakpoint resetting. */
4805 b->pspace = sal.pspace;
4806
4807 if (sal.symtab == NULL)
4808 b->source_file = NULL;
4809 else
4810 b->source_file = xstrdup (sal.symtab->filename);
4811 b->loc->section = sal.section;
4812 b->line_number = sal.line;
4813
4814 set_breakpoint_location_function (b->loc);
4815
4816 breakpoints_changed ();
4817
4818 return b;
4819 }
4820
4821
4822 /* Note that the breakpoint object B describes a permanent breakpoint
4823 instruction, hard-wired into the inferior's code. */
4824 void
4825 make_breakpoint_permanent (struct breakpoint *b)
4826 {
4827 struct bp_location *bl;
4828 b->enable_state = bp_permanent;
4829
4830 /* By definition, permanent breakpoints are already present in the code.
4831 Mark all locations as inserted. For now, make_breakpoint_permanent
4832 is called in just one place, so it's hard to say if it's reasonable
4833 to have permanent breakpoint with multiple locations or not,
4834 but it's easy to implmement. */
4835 for (bl = b->loc; bl; bl = bl->next)
4836 bl->inserted = 1;
4837 }
4838
4839 /* Call this routine when stepping and nexting to enable a breakpoint
4840 if we do a longjmp() in THREAD. When we hit that breakpoint, call
4841 set_longjmp_resume_breakpoint() to figure out where we are going. */
4842
4843 void
4844 set_longjmp_breakpoint (int thread)
4845 {
4846 struct breakpoint *b, *temp;
4847
4848 /* To avoid having to rescan all objfile symbols at every step,
4849 we maintain a list of continually-inserted but always disabled
4850 longjmp "master" breakpoints. Here, we simply create momentary
4851 clones of those and enable them for the requested thread. */
4852 ALL_BREAKPOINTS_SAFE (b, temp)
4853 if (b->pspace == current_program_space
4854 && b->type == bp_longjmp_master)
4855 {
4856 struct breakpoint *clone = clone_momentary_breakpoint (b);
4857 clone->type = bp_longjmp;
4858 clone->thread = thread;
4859 }
4860 }
4861
4862 /* Delete all longjmp breakpoints from THREAD. */
4863 void
4864 delete_longjmp_breakpoint (int thread)
4865 {
4866 struct breakpoint *b, *temp;
4867
4868 ALL_BREAKPOINTS_SAFE (b, temp)
4869 if (b->type == bp_longjmp)
4870 {
4871 if (b->thread == thread)
4872 delete_breakpoint (b);
4873 }
4874 }
4875
4876 void
4877 enable_overlay_breakpoints (void)
4878 {
4879 struct breakpoint *b;
4880
4881 ALL_BREAKPOINTS (b)
4882 if (b->type == bp_overlay_event)
4883 {
4884 b->enable_state = bp_enabled;
4885 update_global_location_list (1);
4886 overlay_events_enabled = 1;
4887 }
4888 }
4889
4890 void
4891 disable_overlay_breakpoints (void)
4892 {
4893 struct breakpoint *b;
4894
4895 ALL_BREAKPOINTS (b)
4896 if (b->type == bp_overlay_event)
4897 {
4898 b->enable_state = bp_disabled;
4899 update_global_location_list (0);
4900 overlay_events_enabled = 0;
4901 }
4902 }
4903
4904 struct breakpoint *
4905 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
4906 {
4907 struct breakpoint *b;
4908
4909 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
4910
4911 b->enable_state = bp_enabled;
4912 /* addr_string has to be used or breakpoint_re_set will delete me. */
4913 b->addr_string
4914 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
4915
4916 update_global_location_list_nothrow (1);
4917
4918 return b;
4919 }
4920
4921 void
4922 remove_thread_event_breakpoints (void)
4923 {
4924 struct breakpoint *b, *temp;
4925
4926 ALL_BREAKPOINTS_SAFE (b, temp)
4927 if (b->type == bp_thread_event
4928 && b->loc->pspace == current_program_space)
4929 delete_breakpoint (b);
4930 }
4931
4932 struct captured_parse_breakpoint_args
4933 {
4934 char **arg_p;
4935 struct symtabs_and_lines *sals_p;
4936 char ***addr_string_p;
4937 int *not_found_ptr;
4938 };
4939
4940 struct lang_and_radix
4941 {
4942 enum language lang;
4943 int radix;
4944 };
4945
4946 /* Create a breakpoint for JIT code registration and unregistration. */
4947
4948 struct breakpoint *
4949 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
4950 {
4951 struct breakpoint *b;
4952
4953 b = create_internal_breakpoint (gdbarch, address, bp_jit_event);
4954 update_global_location_list_nothrow (1);
4955 return b;
4956 }
4957
4958 void
4959 remove_solib_event_breakpoints (void)
4960 {
4961 struct breakpoint *b, *temp;
4962
4963 ALL_BREAKPOINTS_SAFE (b, temp)
4964 if (b->type == bp_shlib_event
4965 && b->loc->pspace == current_program_space)
4966 delete_breakpoint (b);
4967 }
4968
4969 struct breakpoint *
4970 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
4971 {
4972 struct breakpoint *b;
4973
4974 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
4975 update_global_location_list_nothrow (1);
4976 return b;
4977 }
4978
4979 /* Disable any breakpoints that are on code in shared libraries. Only
4980 apply to enabled breakpoints, disabled ones can just stay disabled. */
4981
4982 void
4983 disable_breakpoints_in_shlibs (void)
4984 {
4985 struct bp_location *loc, **locp_tmp;
4986
4987 ALL_BP_LOCATIONS (loc, locp_tmp)
4988 {
4989 struct breakpoint *b = loc->owner;
4990 /* We apply the check to all breakpoints, including disabled
4991 for those with loc->duplicate set. This is so that when breakpoint
4992 becomes enabled, or the duplicate is removed, gdb will try to insert
4993 all breakpoints. If we don't set shlib_disabled here, we'll try
4994 to insert those breakpoints and fail. */
4995 if (((b->type == bp_breakpoint)
4996 || (b->type == bp_hardware_breakpoint)
4997 || (b->type == bp_tracepoint))
4998 && loc->pspace == current_program_space
4999 && !loc->shlib_disabled
5000 #ifdef PC_SOLIB
5001 && PC_SOLIB (loc->address)
5002 #else
5003 && solib_name_from_address (loc->pspace, loc->address)
5004 #endif
5005 )
5006 {
5007 loc->shlib_disabled = 1;
5008 }
5009 }
5010 }
5011
5012 /* Disable any breakpoints that are in in an unloaded shared library. Only
5013 apply to enabled breakpoints, disabled ones can just stay disabled. */
5014
5015 static void
5016 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
5017 {
5018 struct bp_location *loc, **locp_tmp;
5019 int disabled_shlib_breaks = 0;
5020
5021 /* SunOS a.out shared libraries are always mapped, so do not
5022 disable breakpoints; they will only be reported as unloaded
5023 through clear_solib when GDB discards its shared library
5024 list. See clear_solib for more information. */
5025 if (exec_bfd != NULL
5026 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
5027 return;
5028
5029 ALL_BP_LOCATIONS (loc, locp_tmp)
5030 {
5031 struct breakpoint *b = loc->owner;
5032 if ((loc->loc_type == bp_loc_hardware_breakpoint
5033 || loc->loc_type == bp_loc_software_breakpoint)
5034 && solib->pspace == loc->pspace
5035 && !loc->shlib_disabled
5036 && (b->type == bp_breakpoint || b->type == bp_hardware_breakpoint)
5037 && solib_contains_address_p (solib, loc->address))
5038 {
5039 loc->shlib_disabled = 1;
5040 /* At this point, we cannot rely on remove_breakpoint
5041 succeeding so we must mark the breakpoint as not inserted
5042 to prevent future errors occurring in remove_breakpoints. */
5043 loc->inserted = 0;
5044 if (!disabled_shlib_breaks)
5045 {
5046 target_terminal_ours_for_output ();
5047 warning (_("Temporarily disabling breakpoints for unloaded shared library \"%s\""),
5048 solib->so_name);
5049 }
5050 disabled_shlib_breaks = 1;
5051 }
5052 }
5053 }
5054
5055 /* FORK & VFORK catchpoints. */
5056
5057 /* Implement the "insert" breakpoint_ops method for fork catchpoints. */
5058
5059 static void
5060 insert_catch_fork (struct breakpoint *b)
5061 {
5062 target_insert_fork_catchpoint (PIDGET (inferior_ptid));
5063 }
5064
5065 /* Implement the "remove" breakpoint_ops method for fork catchpoints. */
5066
5067 static int
5068 remove_catch_fork (struct breakpoint *b)
5069 {
5070 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
5071 }
5072
5073 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
5074 catchpoints. */
5075
5076 static int
5077 breakpoint_hit_catch_fork (struct breakpoint *b)
5078 {
5079 return inferior_has_forked (inferior_ptid, &b->forked_inferior_pid);
5080 }
5081
5082 /* Implement the "print_it" breakpoint_ops method for fork catchpoints. */
5083
5084 static enum print_stop_action
5085 print_it_catch_fork (struct breakpoint *b)
5086 {
5087 annotate_catchpoint (b->number);
5088 printf_filtered (_("\nCatchpoint %d (forked process %d), "),
5089 b->number, ptid_get_pid (b->forked_inferior_pid));
5090 return PRINT_SRC_AND_LOC;
5091 }
5092
5093 /* Implement the "print_one" breakpoint_ops method for fork catchpoints. */
5094
5095 static void
5096 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
5097 {
5098 struct value_print_options opts;
5099
5100 get_user_print_options (&opts);
5101
5102 /* Field 4, the address, is omitted (which makes the columns
5103 not line up too nicely with the headers, but the effect
5104 is relatively readable). */
5105 if (opts.addressprint)
5106 ui_out_field_skip (uiout, "addr");
5107 annotate_field (5);
5108 ui_out_text (uiout, "fork");
5109 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
5110 {
5111 ui_out_text (uiout, ", process ");
5112 ui_out_field_int (uiout, "what",
5113 ptid_get_pid (b->forked_inferior_pid));
5114 ui_out_spaces (uiout, 1);
5115 }
5116 }
5117
5118 /* Implement the "print_mention" breakpoint_ops method for fork
5119 catchpoints. */
5120
5121 static void
5122 print_mention_catch_fork (struct breakpoint *b)
5123 {
5124 printf_filtered (_("Catchpoint %d (fork)"), b->number);
5125 }
5126
5127 /* The breakpoint_ops structure to be used in fork catchpoints. */
5128
5129 static struct breakpoint_ops catch_fork_breakpoint_ops =
5130 {
5131 insert_catch_fork,
5132 remove_catch_fork,
5133 breakpoint_hit_catch_fork,
5134 print_it_catch_fork,
5135 print_one_catch_fork,
5136 print_mention_catch_fork
5137 };
5138
5139 /* Implement the "insert" breakpoint_ops method for vfork catchpoints. */
5140
5141 static void
5142 insert_catch_vfork (struct breakpoint *b)
5143 {
5144 target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
5145 }
5146
5147 /* Implement the "remove" breakpoint_ops method for vfork catchpoints. */
5148
5149 static int
5150 remove_catch_vfork (struct breakpoint *b)
5151 {
5152 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
5153 }
5154
5155 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
5156 catchpoints. */
5157
5158 static int
5159 breakpoint_hit_catch_vfork (struct breakpoint *b)
5160 {
5161 return inferior_has_vforked (inferior_ptid, &b->forked_inferior_pid);
5162 }
5163
5164 /* Implement the "print_it" breakpoint_ops method for vfork catchpoints. */
5165
5166 static enum print_stop_action
5167 print_it_catch_vfork (struct breakpoint *b)
5168 {
5169 annotate_catchpoint (b->number);
5170 printf_filtered (_("\nCatchpoint %d (vforked process %d), "),
5171 b->number, ptid_get_pid (b->forked_inferior_pid));
5172 return PRINT_SRC_AND_LOC;
5173 }
5174
5175 /* Implement the "print_one" breakpoint_ops method for vfork catchpoints. */
5176
5177 static void
5178 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
5179 {
5180 struct value_print_options opts;
5181
5182 get_user_print_options (&opts);
5183 /* Field 4, the address, is omitted (which makes the columns
5184 not line up too nicely with the headers, but the effect
5185 is relatively readable). */
5186 if (opts.addressprint)
5187 ui_out_field_skip (uiout, "addr");
5188 annotate_field (5);
5189 ui_out_text (uiout, "vfork");
5190 if (!ptid_equal (b->forked_inferior_pid, null_ptid))
5191 {
5192 ui_out_text (uiout, ", process ");
5193 ui_out_field_int (uiout, "what",
5194 ptid_get_pid (b->forked_inferior_pid));
5195 ui_out_spaces (uiout, 1);
5196 }
5197 }
5198
5199 /* Implement the "print_mention" breakpoint_ops method for vfork
5200 catchpoints. */
5201
5202 static void
5203 print_mention_catch_vfork (struct breakpoint *b)
5204 {
5205 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
5206 }
5207
5208 /* The breakpoint_ops structure to be used in vfork catchpoints. */
5209
5210 static struct breakpoint_ops catch_vfork_breakpoint_ops =
5211 {
5212 insert_catch_vfork,
5213 remove_catch_vfork,
5214 breakpoint_hit_catch_vfork,
5215 print_it_catch_vfork,
5216 print_one_catch_vfork,
5217 print_mention_catch_vfork
5218 };
5219
5220 /* Implement the "insert" breakpoint_ops method for syscall
5221 catchpoints. */
5222
5223 static void
5224 insert_catch_syscall (struct breakpoint *b)
5225 {
5226 struct inferior *inf = current_inferior ();
5227
5228 ++inf->total_syscalls_count;
5229 if (!b->syscalls_to_be_caught)
5230 ++inf->any_syscall_count;
5231 else
5232 {
5233 int i, iter;
5234 for (i = 0;
5235 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5236 i++)
5237 {
5238 int elem;
5239 if (iter >= VEC_length (int, inf->syscalls_counts))
5240 {
5241 int old_size = VEC_length (int, inf->syscalls_counts);
5242 uintptr_t vec_addr_offset = old_size * ((uintptr_t) sizeof (int));
5243 uintptr_t vec_addr;
5244 VEC_safe_grow (int, inf->syscalls_counts, iter + 1);
5245 vec_addr = (uintptr_t) VEC_address (int, inf->syscalls_counts) +
5246 vec_addr_offset;
5247 memset ((void *) vec_addr, 0,
5248 (iter + 1 - old_size) * sizeof (int));
5249 }
5250 elem = VEC_index (int, inf->syscalls_counts, iter);
5251 VEC_replace (int, inf->syscalls_counts, iter, ++elem);
5252 }
5253 }
5254
5255 target_set_syscall_catchpoint (PIDGET (inferior_ptid),
5256 inf->total_syscalls_count != 0,
5257 inf->any_syscall_count,
5258 VEC_length (int, inf->syscalls_counts),
5259 VEC_address (int, inf->syscalls_counts));
5260 }
5261
5262 /* Implement the "remove" breakpoint_ops method for syscall
5263 catchpoints. */
5264
5265 static int
5266 remove_catch_syscall (struct breakpoint *b)
5267 {
5268 struct inferior *inf = current_inferior ();
5269
5270 --inf->total_syscalls_count;
5271 if (!b->syscalls_to_be_caught)
5272 --inf->any_syscall_count;
5273 else
5274 {
5275 int i, iter;
5276 for (i = 0;
5277 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5278 i++)
5279 {
5280 int elem;
5281 if (iter >= VEC_length (int, inf->syscalls_counts))
5282 /* Shouldn't happen. */
5283 continue;
5284 elem = VEC_index (int, inf->syscalls_counts, iter);
5285 VEC_replace (int, inf->syscalls_counts, iter, --elem);
5286 }
5287 }
5288
5289 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
5290 inf->total_syscalls_count != 0,
5291 inf->any_syscall_count,
5292 VEC_length (int, inf->syscalls_counts),
5293 VEC_address (int, inf->syscalls_counts));
5294 }
5295
5296 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
5297 catchpoints. */
5298
5299 static int
5300 breakpoint_hit_catch_syscall (struct breakpoint *b)
5301 {
5302 /* We must check if we are catching specific syscalls in this breakpoint.
5303 If we are, then we must guarantee that the called syscall is the same
5304 syscall we are catching. */
5305 int syscall_number = 0;
5306
5307 if (!inferior_has_called_syscall (inferior_ptid, &syscall_number))
5308 return 0;
5309
5310 /* Now, checking if the syscall is the same. */
5311 if (b->syscalls_to_be_caught)
5312 {
5313 int i, iter;
5314 for (i = 0;
5315 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5316 i++)
5317 if (syscall_number == iter)
5318 break;
5319 /* Not the same. */
5320 if (!iter)
5321 return 0;
5322 }
5323
5324 return 1;
5325 }
5326
5327 /* Implement the "print_it" breakpoint_ops method for syscall
5328 catchpoints. */
5329
5330 static enum print_stop_action
5331 print_it_catch_syscall (struct breakpoint *b)
5332 {
5333 /* These are needed because we want to know in which state a
5334 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
5335 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
5336 must print "called syscall" or "returned from syscall". */
5337 ptid_t ptid;
5338 struct target_waitstatus last;
5339 struct syscall s;
5340 struct cleanup *old_chain;
5341 char *syscall_id;
5342
5343 get_last_target_status (&ptid, &last);
5344
5345 get_syscall_by_number (last.value.syscall_number, &s);
5346
5347 annotate_catchpoint (b->number);
5348
5349 if (s.name == NULL)
5350 syscall_id = xstrprintf ("%d", last.value.syscall_number);
5351 else
5352 syscall_id = xstrprintf ("'%s'", s.name);
5353
5354 old_chain = make_cleanup (xfree, syscall_id);
5355
5356 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
5357 printf_filtered (_("\nCatchpoint %d (call to syscall %s), "),
5358 b->number, syscall_id);
5359 else if (last.kind == TARGET_WAITKIND_SYSCALL_RETURN)
5360 printf_filtered (_("\nCatchpoint %d (returned from syscall %s), "),
5361 b->number, syscall_id);
5362
5363 do_cleanups (old_chain);
5364
5365 return PRINT_SRC_AND_LOC;
5366 }
5367
5368 /* Implement the "print_one" breakpoint_ops method for syscall
5369 catchpoints. */
5370
5371 static void
5372 print_one_catch_syscall (struct breakpoint *b,
5373 struct bp_location **last_loc)
5374 {
5375 struct value_print_options opts;
5376
5377 get_user_print_options (&opts);
5378 /* Field 4, the address, is omitted (which makes the columns
5379 not line up too nicely with the headers, but the effect
5380 is relatively readable). */
5381 if (opts.addressprint)
5382 ui_out_field_skip (uiout, "addr");
5383 annotate_field (5);
5384
5385 if (b->syscalls_to_be_caught
5386 && VEC_length (int, b->syscalls_to_be_caught) > 1)
5387 ui_out_text (uiout, "syscalls \"");
5388 else
5389 ui_out_text (uiout, "syscall \"");
5390
5391 if (b->syscalls_to_be_caught)
5392 {
5393 int i, iter;
5394 char *text = xstrprintf ("%s", "");
5395 for (i = 0;
5396 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5397 i++)
5398 {
5399 char *x = text;
5400 struct syscall s;
5401 get_syscall_by_number (iter, &s);
5402
5403 if (s.name != NULL)
5404 text = xstrprintf ("%s%s, ", text, s.name);
5405 else
5406 text = xstrprintf ("%s%d, ", text, iter);
5407
5408 /* We have to xfree the last 'text' (now stored at 'x')
5409 because xstrprintf dinamically allocates new space for it
5410 on every call. */
5411 xfree (x);
5412 }
5413 /* Remove the last comma. */
5414 text[strlen (text) - 2] = '\0';
5415 ui_out_field_string (uiout, "what", text);
5416 }
5417 else
5418 ui_out_field_string (uiout, "what", "<any syscall>");
5419 ui_out_text (uiout, "\" ");
5420 }
5421
5422 /* Implement the "print_mention" breakpoint_ops method for syscall
5423 catchpoints. */
5424
5425 static void
5426 print_mention_catch_syscall (struct breakpoint *b)
5427 {
5428 if (b->syscalls_to_be_caught)
5429 {
5430 int i, iter;
5431
5432 if (VEC_length (int, b->syscalls_to_be_caught) > 1)
5433 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
5434 else
5435 printf_filtered (_("Catchpoint %d (syscall"), b->number);
5436
5437 for (i = 0;
5438 VEC_iterate (int, b->syscalls_to_be_caught, i, iter);
5439 i++)
5440 {
5441 struct syscall s;
5442 get_syscall_by_number (iter, &s);
5443
5444 if (s.name)
5445 printf_filtered (" '%s' [%d]", s.name, s.number);
5446 else
5447 printf_filtered (" %d", s.number);
5448 }
5449 printf_filtered (")");
5450 }
5451 else
5452 printf_filtered (_("Catchpoint %d (any syscall)"),
5453 b->number);
5454 }
5455
5456 /* The breakpoint_ops structure to be used in syscall catchpoints. */
5457
5458 static struct breakpoint_ops catch_syscall_breakpoint_ops =
5459 {
5460 insert_catch_syscall,
5461 remove_catch_syscall,
5462 breakpoint_hit_catch_syscall,
5463 print_it_catch_syscall,
5464 print_one_catch_syscall,
5465 print_mention_catch_syscall
5466 };
5467
5468 /* Returns non-zero if 'b' is a syscall catchpoint. */
5469
5470 static int
5471 syscall_catchpoint_p (struct breakpoint *b)
5472 {
5473 return (b->ops == &catch_syscall_breakpoint_ops);
5474 }
5475
5476 /* Create a new breakpoint of the bp_catchpoint kind and return it,
5477 but does NOT mention it nor update the global location list.
5478 This is useful if you need to fill more fields in the
5479 struct breakpoint before calling mention.
5480
5481 If TEMPFLAG is non-zero, then make the breakpoint temporary.
5482 If COND_STRING is not NULL, then store it in the breakpoint.
5483 OPS, if not NULL, is the breakpoint_ops structure associated
5484 to the catchpoint. */
5485
5486 static struct breakpoint *
5487 create_catchpoint_without_mention (struct gdbarch *gdbarch, int tempflag,
5488 char *cond_string,
5489 struct breakpoint_ops *ops)
5490 {
5491 struct symtab_and_line sal;
5492 struct breakpoint *b;
5493
5494 init_sal (&sal);
5495 sal.pspace = current_program_space;
5496
5497 b = set_raw_breakpoint (gdbarch, sal, bp_catchpoint);
5498 set_breakpoint_count (breakpoint_count + 1);
5499 b->number = breakpoint_count;
5500
5501 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
5502 b->thread = -1;
5503 b->addr_string = NULL;
5504 b->enable_state = bp_enabled;
5505 b->disposition = tempflag ? disp_del : disp_donttouch;
5506 b->ops = ops;
5507
5508 return b;
5509 }
5510
5511 /* Create a new breakpoint of the bp_catchpoint kind and return it.
5512
5513 If TEMPFLAG is non-zero, then make the breakpoint temporary.
5514 If COND_STRING is not NULL, then store it in the breakpoint.
5515 OPS, if not NULL, is the breakpoint_ops structure associated
5516 to the catchpoint. */
5517
5518 static struct breakpoint *
5519 create_catchpoint (struct gdbarch *gdbarch, int tempflag,
5520 char *cond_string, struct breakpoint_ops *ops)
5521 {
5522 struct breakpoint *b =
5523 create_catchpoint_without_mention (gdbarch, tempflag, cond_string, ops);
5524
5525 mention (b);
5526 update_global_location_list (1);
5527
5528 return b;
5529 }
5530
5531 static void
5532 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
5533 int tempflag, char *cond_string,
5534 struct breakpoint_ops *ops)
5535 {
5536 struct breakpoint *b
5537 = create_catchpoint (gdbarch, tempflag, cond_string, ops);
5538
5539 /* FIXME: We should put this information in a breakpoint private data
5540 area. */
5541 b->forked_inferior_pid = null_ptid;
5542 }
5543
5544 /* Exec catchpoints. */
5545
5546 static void
5547 insert_catch_exec (struct breakpoint *b)
5548 {
5549 target_insert_exec_catchpoint (PIDGET (inferior_ptid));
5550 }
5551
5552 static int
5553 remove_catch_exec (struct breakpoint *b)
5554 {
5555 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
5556 }
5557
5558 static int
5559 breakpoint_hit_catch_exec (struct breakpoint *b)
5560 {
5561 return inferior_has_execd (inferior_ptid, &b->exec_pathname);
5562 }
5563
5564 static enum print_stop_action
5565 print_it_catch_exec (struct breakpoint *b)
5566 {
5567 annotate_catchpoint (b->number);
5568 printf_filtered (_("\nCatchpoint %d (exec'd %s), "), b->number,
5569 b->exec_pathname);
5570 return PRINT_SRC_AND_LOC;
5571 }
5572
5573 static void
5574 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
5575 {
5576 struct value_print_options opts;
5577
5578 get_user_print_options (&opts);
5579
5580 /* Field 4, the address, is omitted (which makes the columns
5581 not line up too nicely with the headers, but the effect
5582 is relatively readable). */
5583 if (opts.addressprint)
5584 ui_out_field_skip (uiout, "addr");
5585 annotate_field (5);
5586 ui_out_text (uiout, "exec");
5587 if (b->exec_pathname != NULL)
5588 {
5589 ui_out_text (uiout, ", program \"");
5590 ui_out_field_string (uiout, "what", b->exec_pathname);
5591 ui_out_text (uiout, "\" ");
5592 }
5593 }
5594
5595 static void
5596 print_mention_catch_exec (struct breakpoint *b)
5597 {
5598 printf_filtered (_("Catchpoint %d (exec)"), b->number);
5599 }
5600
5601 static struct breakpoint_ops catch_exec_breakpoint_ops =
5602 {
5603 insert_catch_exec,
5604 remove_catch_exec,
5605 breakpoint_hit_catch_exec,
5606 print_it_catch_exec,
5607 print_one_catch_exec,
5608 print_mention_catch_exec
5609 };
5610
5611 static void
5612 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
5613 struct breakpoint_ops *ops)
5614 {
5615 struct gdbarch *gdbarch = get_current_arch ();
5616 struct breakpoint *b =
5617 create_catchpoint_without_mention (gdbarch, tempflag, NULL, ops);
5618
5619 b->syscalls_to_be_caught = filter;
5620
5621 /* Now, we have to mention the breakpoint and update the global
5622 location list. */
5623 mention (b);
5624 update_global_location_list (1);
5625 }
5626
5627 static int
5628 hw_breakpoint_used_count (void)
5629 {
5630 struct breakpoint *b;
5631 int i = 0;
5632
5633 ALL_BREAKPOINTS (b)
5634 {
5635 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
5636 i++;
5637 }
5638
5639 return i;
5640 }
5641
5642 static int
5643 hw_watchpoint_used_count (enum bptype type, int *other_type_used)
5644 {
5645 struct breakpoint *b;
5646 int i = 0;
5647
5648 *other_type_used = 0;
5649 ALL_BREAKPOINTS (b)
5650 {
5651 if (breakpoint_enabled (b))
5652 {
5653 if (b->type == type)
5654 i++;
5655 else if ((b->type == bp_hardware_watchpoint
5656 || b->type == bp_read_watchpoint
5657 || b->type == bp_access_watchpoint))
5658 *other_type_used = 1;
5659 }
5660 }
5661 return i;
5662 }
5663
5664 void
5665 disable_watchpoints_before_interactive_call_start (void)
5666 {
5667 struct breakpoint *b;
5668
5669 ALL_BREAKPOINTS (b)
5670 {
5671 if (((b->type == bp_watchpoint)
5672 || (b->type == bp_hardware_watchpoint)
5673 || (b->type == bp_read_watchpoint)
5674 || (b->type == bp_access_watchpoint))
5675 && breakpoint_enabled (b))
5676 {
5677 b->enable_state = bp_call_disabled;
5678 update_global_location_list (0);
5679 }
5680 }
5681 }
5682
5683 void
5684 enable_watchpoints_after_interactive_call_stop (void)
5685 {
5686 struct breakpoint *b;
5687
5688 ALL_BREAKPOINTS (b)
5689 {
5690 if (((b->type == bp_watchpoint)
5691 || (b->type == bp_hardware_watchpoint)
5692 || (b->type == bp_read_watchpoint)
5693 || (b->type == bp_access_watchpoint))
5694 && (b->enable_state == bp_call_disabled))
5695 {
5696 b->enable_state = bp_enabled;
5697 update_global_location_list (1);
5698 }
5699 }
5700 }
5701
5702 void
5703 disable_breakpoints_before_startup (void)
5704 {
5705 struct breakpoint *b;
5706 int found = 0;
5707
5708 ALL_BREAKPOINTS (b)
5709 {
5710 if (b->pspace != current_program_space)
5711 continue;
5712
5713 if ((b->type == bp_breakpoint
5714 || b->type == bp_hardware_breakpoint)
5715 && breakpoint_enabled (b))
5716 {
5717 b->enable_state = bp_startup_disabled;
5718 found = 1;
5719 }
5720 }
5721
5722 if (found)
5723 update_global_location_list (0);
5724
5725 current_program_space->executing_startup = 1;
5726 }
5727
5728 void
5729 enable_breakpoints_after_startup (void)
5730 {
5731 struct breakpoint *b;
5732 int found = 0;
5733
5734 current_program_space->executing_startup = 0;
5735
5736 ALL_BREAKPOINTS (b)
5737 {
5738 if (b->pspace != current_program_space)
5739 continue;
5740
5741 if ((b->type == bp_breakpoint
5742 || b->type == bp_hardware_breakpoint)
5743 && b->enable_state == bp_startup_disabled)
5744 {
5745 b->enable_state = bp_enabled;
5746 found = 1;
5747 }
5748 }
5749
5750 if (found)
5751 breakpoint_re_set ();
5752 }
5753
5754
5755 /* Set a breakpoint that will evaporate an end of command
5756 at address specified by SAL.
5757 Restrict it to frame FRAME if FRAME is nonzero. */
5758
5759 struct breakpoint *
5760 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
5761 struct frame_id frame_id, enum bptype type)
5762 {
5763 struct breakpoint *b;
5764
5765 /* If FRAME_ID is valid, it should be a real frame, not an inlined
5766 one. */
5767 gdb_assert (!frame_id_inlined_p (frame_id));
5768
5769 b = set_raw_breakpoint (gdbarch, sal, type);
5770 b->enable_state = bp_enabled;
5771 b->disposition = disp_donttouch;
5772 b->frame_id = frame_id;
5773
5774 /* If we're debugging a multi-threaded program, then we
5775 want momentary breakpoints to be active in only a
5776 single thread of control. */
5777 if (in_thread_list (inferior_ptid))
5778 b->thread = pid_to_thread_id (inferior_ptid);
5779
5780 update_global_location_list_nothrow (1);
5781
5782 return b;
5783 }
5784
5785 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
5786 ORIG is NULL. */
5787
5788 struct breakpoint *
5789 clone_momentary_breakpoint (struct breakpoint *orig)
5790 {
5791 struct breakpoint *copy;
5792
5793 /* If there's nothing to clone, then return nothing. */
5794 if (orig == NULL)
5795 return NULL;
5796
5797 copy = set_raw_breakpoint_without_location (orig->gdbarch, orig->type);
5798 copy->loc = allocate_bp_location (copy);
5799 set_breakpoint_location_function (copy->loc);
5800
5801 copy->loc->gdbarch = orig->loc->gdbarch;
5802 copy->loc->requested_address = orig->loc->requested_address;
5803 copy->loc->address = orig->loc->address;
5804 copy->loc->section = orig->loc->section;
5805 copy->loc->pspace = orig->loc->pspace;
5806
5807 if (orig->source_file == NULL)
5808 copy->source_file = NULL;
5809 else
5810 copy->source_file = xstrdup (orig->source_file);
5811
5812 copy->line_number = orig->line_number;
5813 copy->frame_id = orig->frame_id;
5814 copy->thread = orig->thread;
5815 copy->pspace = orig->pspace;
5816
5817 copy->enable_state = bp_enabled;
5818 copy->disposition = disp_donttouch;
5819 copy->number = internal_breakpoint_number--;
5820
5821 update_global_location_list_nothrow (0);
5822 return copy;
5823 }
5824
5825 struct breakpoint *
5826 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
5827 enum bptype type)
5828 {
5829 struct symtab_and_line sal;
5830
5831 sal = find_pc_line (pc, 0);
5832 sal.pc = pc;
5833 sal.section = find_pc_overlay (pc);
5834 sal.explicit_pc = 1;
5835
5836 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
5837 }
5838 \f
5839
5840 /* Tell the user we have just set a breakpoint B. */
5841
5842 static void
5843 mention (struct breakpoint *b)
5844 {
5845 int say_where = 0;
5846 struct cleanup *ui_out_chain;
5847 struct value_print_options opts;
5848
5849 get_user_print_options (&opts);
5850
5851 /* FIXME: This is misplaced; mention() is called by things (like
5852 hitting a watchpoint) other than breakpoint creation. It should
5853 be possible to clean this up and at the same time replace the
5854 random calls to breakpoint_changed with this hook. */
5855 observer_notify_breakpoint_created (b->number);
5856
5857 if (b->ops != NULL && b->ops->print_mention != NULL)
5858 b->ops->print_mention (b);
5859 else
5860 switch (b->type)
5861 {
5862 case bp_none:
5863 printf_filtered (_("(apparently deleted?) Eventpoint %d: "), b->number);
5864 break;
5865 case bp_watchpoint:
5866 ui_out_text (uiout, "Watchpoint ");
5867 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5868 ui_out_field_int (uiout, "number", b->number);
5869 ui_out_text (uiout, ": ");
5870 ui_out_field_string (uiout, "exp", b->exp_string);
5871 do_cleanups (ui_out_chain);
5872 break;
5873 case bp_hardware_watchpoint:
5874 ui_out_text (uiout, "Hardware watchpoint ");
5875 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
5876 ui_out_field_int (uiout, "number", b->number);
5877 ui_out_text (uiout, ": ");
5878 ui_out_field_string (uiout, "exp", b->exp_string);
5879 do_cleanups (ui_out_chain);
5880 break;
5881 case bp_read_watchpoint:
5882 ui_out_text (uiout, "Hardware read watchpoint ");
5883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
5884 ui_out_field_int (uiout, "number", b->number);
5885 ui_out_text (uiout, ": ");
5886 ui_out_field_string (uiout, "exp", b->exp_string);
5887 do_cleanups (ui_out_chain);
5888 break;
5889 case bp_access_watchpoint:
5890 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
5891 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
5892 ui_out_field_int (uiout, "number", b->number);
5893 ui_out_text (uiout, ": ");
5894 ui_out_field_string (uiout, "exp", b->exp_string);
5895 do_cleanups (ui_out_chain);
5896 break;
5897 case bp_breakpoint:
5898 if (ui_out_is_mi_like_p (uiout))
5899 {
5900 say_where = 0;
5901 break;
5902 }
5903 if (b->disposition == disp_del)
5904 printf_filtered (_("Temporary breakpoint"));
5905 else
5906 printf_filtered (_("Breakpoint"));
5907 printf_filtered (_(" %d"), b->number);
5908 say_where = 1;
5909 break;
5910 case bp_hardware_breakpoint:
5911 if (ui_out_is_mi_like_p (uiout))
5912 {
5913 say_where = 0;
5914 break;
5915 }
5916 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
5917 say_where = 1;
5918 break;
5919 case bp_tracepoint:
5920 if (ui_out_is_mi_like_p (uiout))
5921 {
5922 say_where = 0;
5923 break;
5924 }
5925 printf_filtered (_("Tracepoint"));
5926 printf_filtered (_(" %d"), b->number);
5927 say_where = 1;
5928 break;
5929
5930 case bp_until:
5931 case bp_finish:
5932 case bp_longjmp:
5933 case bp_longjmp_resume:
5934 case bp_step_resume:
5935 case bp_call_dummy:
5936 case bp_watchpoint_scope:
5937 case bp_shlib_event:
5938 case bp_thread_event:
5939 case bp_overlay_event:
5940 case bp_jit_event:
5941 case bp_longjmp_master:
5942 break;
5943 }
5944
5945 if (say_where)
5946 {
5947 /* i18n: cagney/2005-02-11: Below needs to be merged into a
5948 single string. */
5949 if (b->loc == NULL)
5950 {
5951 printf_filtered (_(" (%s) pending."), b->addr_string);
5952 }
5953 else
5954 {
5955 if (opts.addressprint || b->source_file == NULL)
5956 {
5957 printf_filtered (" at ");
5958 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
5959 gdb_stdout);
5960 }
5961 if (b->source_file)
5962 printf_filtered (": file %s, line %d.",
5963 b->source_file, b->line_number);
5964
5965 if (b->loc->next)
5966 {
5967 struct bp_location *loc = b->loc;
5968 int n = 0;
5969 for (; loc; loc = loc->next)
5970 ++n;
5971 printf_filtered (" (%d locations)", n);
5972 }
5973
5974 }
5975 }
5976 if (ui_out_is_mi_like_p (uiout))
5977 return;
5978 printf_filtered ("\n");
5979 }
5980 \f
5981
5982 static struct bp_location *
5983 add_location_to_breakpoint (struct breakpoint *b,
5984 const struct symtab_and_line *sal)
5985 {
5986 struct bp_location *loc, **tmp;
5987
5988 loc = allocate_bp_location (b);
5989 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
5990 ;
5991 *tmp = loc;
5992 loc->gdbarch = get_sal_arch (*sal);
5993 if (!loc->gdbarch)
5994 loc->gdbarch = b->gdbarch;
5995 loc->requested_address = sal->pc;
5996 loc->address = adjust_breakpoint_address (loc->gdbarch,
5997 loc->requested_address, b->type);
5998 loc->pspace = sal->pspace;
5999 gdb_assert (loc->pspace != NULL);
6000 loc->section = sal->section;
6001
6002 set_breakpoint_location_function (loc);
6003 return loc;
6004 }
6005 \f
6006
6007 /* Return 1 if LOC is pointing to a permanent breakpoint,
6008 return 0 otherwise. */
6009
6010 static int
6011 bp_loc_is_permanent (struct bp_location *loc)
6012 {
6013 int len;
6014 CORE_ADDR addr;
6015 const gdb_byte *brk;
6016 gdb_byte *target_mem;
6017 struct cleanup *cleanup;
6018 int retval = 0;
6019
6020 gdb_assert (loc != NULL);
6021
6022 addr = loc->address;
6023 brk = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
6024
6025 /* Software breakpoints unsupported? */
6026 if (brk == NULL)
6027 return 0;
6028
6029 target_mem = alloca (len);
6030
6031 /* Enable the automatic memory restoration from breakpoints while
6032 we read the memory. Otherwise we could say about our temporary
6033 breakpoints they are permanent. */
6034 cleanup = save_current_space_and_thread ();
6035
6036 switch_to_program_space_and_thread (loc->pspace);
6037 make_show_memory_breakpoints_cleanup (0);
6038
6039 if (target_read_memory (loc->address, target_mem, len) == 0
6040 && memcmp (target_mem, brk, len) == 0)
6041 retval = 1;
6042
6043 do_cleanups (cleanup);
6044
6045 return retval;
6046 }
6047
6048
6049
6050 /* Create a breakpoint with SAL as location. Use ADDR_STRING
6051 as textual description of the location, and COND_STRING
6052 as condition expression. */
6053
6054 static void
6055 create_breakpoint (struct gdbarch *gdbarch,
6056 struct symtabs_and_lines sals, char *addr_string,
6057 char *cond_string,
6058 enum bptype type, enum bpdisp disposition,
6059 int thread, int task, int ignore_count,
6060 struct breakpoint_ops *ops, int from_tty, int enabled)
6061 {
6062 struct breakpoint *b = NULL;
6063 int i;
6064
6065 if (type == bp_hardware_breakpoint)
6066 {
6067 int i = hw_breakpoint_used_count ();
6068 int target_resources_ok =
6069 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
6070 i + 1, 0);
6071 if (target_resources_ok == 0)
6072 error (_("No hardware breakpoint support in the target."));
6073 else if (target_resources_ok < 0)
6074 error (_("Hardware breakpoints used exceeds limit."));
6075 }
6076
6077 gdb_assert (sals.nelts > 0);
6078
6079 for (i = 0; i < sals.nelts; ++i)
6080 {
6081 struct symtab_and_line sal = sals.sals[i];
6082 struct bp_location *loc;
6083
6084 if (from_tty)
6085 {
6086 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
6087 if (!loc_gdbarch)
6088 loc_gdbarch = gdbarch;
6089
6090 describe_other_breakpoints (loc_gdbarch,
6091 sal.pspace, sal.pc, sal.section, thread);
6092 }
6093
6094 if (i == 0)
6095 {
6096 b = set_raw_breakpoint (gdbarch, sal, type);
6097 set_breakpoint_count (breakpoint_count + 1);
6098 b->number = breakpoint_count;
6099 b->thread = thread;
6100 b->task = task;
6101
6102 b->cond_string = cond_string;
6103 b->ignore_count = ignore_count;
6104 b->enable_state = enabled ? bp_enabled : bp_disabled;
6105 b->disposition = disposition;
6106
6107 b->pspace = sals.sals[0].pspace;
6108
6109 if (enabled && b->pspace->executing_startup
6110 && (b->type == bp_breakpoint
6111 || b->type == bp_hardware_breakpoint))
6112 b->enable_state = bp_startup_disabled;
6113
6114 loc = b->loc;
6115 }
6116 else
6117 {
6118 loc = add_location_to_breakpoint (b, &sal);
6119 }
6120
6121 if (bp_loc_is_permanent (loc))
6122 make_breakpoint_permanent (b);
6123
6124 if (b->cond_string)
6125 {
6126 char *arg = b->cond_string;
6127 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
6128 if (*arg)
6129 error (_("Garbage %s follows condition"), arg);
6130 }
6131 }
6132
6133 if (addr_string)
6134 b->addr_string = addr_string;
6135 else
6136 /* addr_string has to be used or breakpoint_re_set will delete
6137 me. */
6138 b->addr_string
6139 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6140
6141 b->ops = ops;
6142 mention (b);
6143 }
6144
6145 /* Remove element at INDEX_TO_REMOVE from SAL, shifting other
6146 elements to fill the void space. */
6147 static void
6148 remove_sal (struct symtabs_and_lines *sal, int index_to_remove)
6149 {
6150 int i = index_to_remove+1;
6151 int last_index = sal->nelts-1;
6152
6153 for (;i <= last_index; ++i)
6154 sal->sals[i-1] = sal->sals[i];
6155
6156 --(sal->nelts);
6157 }
6158
6159 /* If appropriate, obtains all sals that correspond to the same file
6160 and line as SAL, in all program spaces. Users debugging with IDEs,
6161 will want to set a breakpoint at foo.c:line, and not really care
6162 about program spaces. This is done only if SAL does not have
6163 explicit PC and has line and file information. If we got just a
6164 single expanded sal, return the original.
6165
6166 Otherwise, if SAL.explicit_line is not set, filter out all sals for
6167 which the name of enclosing function is different from SAL. This
6168 makes sure that if we have breakpoint originally set in template
6169 instantiation, say foo<int>(), we won't expand SAL to locations at
6170 the same line in all existing instantiations of 'foo'. */
6171
6172 static struct symtabs_and_lines
6173 expand_line_sal_maybe (struct symtab_and_line sal)
6174 {
6175 struct symtabs_and_lines expanded;
6176 CORE_ADDR original_pc = sal.pc;
6177 char *original_function = NULL;
6178 int found;
6179 int i;
6180 struct cleanup *old_chain;
6181
6182 /* If we have explicit pc, don't expand.
6183 If we have no line number, we can't expand. */
6184 if (sal.explicit_pc || sal.line == 0 || sal.symtab == NULL)
6185 {
6186 expanded.nelts = 1;
6187 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6188 expanded.sals[0] = sal;
6189 return expanded;
6190 }
6191
6192 sal.pc = 0;
6193
6194 old_chain = save_current_space_and_thread ();
6195
6196 switch_to_program_space_and_thread (sal.pspace);
6197
6198 find_pc_partial_function (original_pc, &original_function, NULL, NULL);
6199
6200 /* Note that expand_line_sal visits *all* program spaces. */
6201 expanded = expand_line_sal (sal);
6202
6203 if (expanded.nelts == 1)
6204 {
6205 /* We had one sal, we got one sal. Without futher
6206 processing, just return the original sal. */
6207 xfree (expanded.sals);
6208 expanded.nelts = 1;
6209 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6210 sal.pc = original_pc;
6211 expanded.sals[0] = sal;
6212 do_cleanups (old_chain);
6213 return expanded;
6214 }
6215
6216 if (!sal.explicit_line)
6217 {
6218 CORE_ADDR func_addr, func_end;
6219 for (i = 0; i < expanded.nelts; ++i)
6220 {
6221 CORE_ADDR pc = expanded.sals[i].pc;
6222 char *this_function;
6223
6224 /* We need to switch threads as well since we're about to
6225 read memory. */
6226 switch_to_program_space_and_thread (expanded.sals[i].pspace);
6227
6228 if (find_pc_partial_function (pc, &this_function,
6229 &func_addr, &func_end))
6230 {
6231 if (this_function
6232 && strcmp (this_function, original_function) != 0)
6233 {
6234 remove_sal (&expanded, i);
6235 --i;
6236 }
6237 else if (func_addr == pc)
6238 {
6239 /* We're at beginning of a function, and should
6240 skip prologue. */
6241 struct symbol *sym = find_pc_function (pc);
6242 if (sym)
6243 expanded.sals[i] = find_function_start_sal (sym, 1);
6244 else
6245 {
6246 /* Since find_pc_partial_function returned true,
6247 we should really always find the section here. */
6248 struct obj_section *section = find_pc_section (pc);
6249 if (section)
6250 {
6251 struct gdbarch *gdbarch
6252 = get_objfile_arch (section->objfile);
6253 expanded.sals[i].pc
6254 = gdbarch_skip_prologue (gdbarch, pc);
6255 }
6256 }
6257 }
6258 }
6259 }
6260 }
6261 else
6262 {
6263 for (i = 0; i < expanded.nelts; ++i)
6264 {
6265 /* If this SAL corresponds to a breakpoint inserted using a
6266 line number, then skip the function prologue if necessary. */
6267 skip_prologue_sal (&expanded.sals[i]);
6268 }
6269 }
6270
6271 do_cleanups (old_chain);
6272
6273 if (expanded.nelts <= 1)
6274 {
6275 /* This is un ugly workaround. If we get zero
6276 expanded sals then something is really wrong.
6277 Fix that by returnign the original sal. */
6278 xfree (expanded.sals);
6279 expanded.nelts = 1;
6280 expanded.sals = xmalloc (sizeof (struct symtab_and_line));
6281 sal.pc = original_pc;
6282 expanded.sals[0] = sal;
6283 return expanded;
6284 }
6285
6286 if (original_pc)
6287 {
6288 found = 0;
6289 for (i = 0; i < expanded.nelts; ++i)
6290 if (expanded.sals[i].pc == original_pc)
6291 {
6292 found = 1;
6293 break;
6294 }
6295 gdb_assert (found);
6296 }
6297
6298 return expanded;
6299 }
6300
6301 /* Add SALS.nelts breakpoints to the breakpoint table. For each
6302 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
6303 value. COND_STRING, if not NULL, specified the condition to be
6304 used for all breakpoints. Essentially the only case where
6305 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
6306 function. In that case, it's still not possible to specify
6307 separate conditions for different overloaded functions, so
6308 we take just a single condition string.
6309
6310 NOTE: If the function succeeds, the caller is expected to cleanup
6311 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
6312 array contents). If the function fails (error() is called), the
6313 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
6314 COND and SALS arrays and each of those arrays contents. */
6315
6316 static void
6317 create_breakpoints (struct gdbarch *gdbarch,
6318 struct symtabs_and_lines sals, char **addr_string,
6319 char *cond_string,
6320 enum bptype type, enum bpdisp disposition,
6321 int thread, int task, int ignore_count,
6322 struct breakpoint_ops *ops, int from_tty,
6323 int enabled)
6324 {
6325 int i;
6326 for (i = 0; i < sals.nelts; ++i)
6327 {
6328 struct symtabs_and_lines expanded =
6329 expand_line_sal_maybe (sals.sals[i]);
6330
6331 create_breakpoint (gdbarch, expanded, addr_string[i],
6332 cond_string, type, disposition,
6333 thread, task, ignore_count, ops, from_tty, enabled);
6334 }
6335 }
6336
6337 /* Parse ARG which is assumed to be a SAL specification possibly
6338 followed by conditionals. On return, SALS contains an array of SAL
6339 addresses found. ADDR_STRING contains a vector of (canonical)
6340 address strings. ARG points to the end of the SAL. */
6341
6342 static void
6343 parse_breakpoint_sals (char **address,
6344 struct symtabs_and_lines *sals,
6345 char ***addr_string,
6346 int *not_found_ptr)
6347 {
6348 char *addr_start = *address;
6349 *addr_string = NULL;
6350 /* If no arg given, or if first arg is 'if ', use the default
6351 breakpoint. */
6352 if ((*address) == NULL
6353 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
6354 {
6355 if (default_breakpoint_valid)
6356 {
6357 struct symtab_and_line sal;
6358 init_sal (&sal); /* initialize to zeroes */
6359 sals->sals = (struct symtab_and_line *)
6360 xmalloc (sizeof (struct symtab_and_line));
6361 sal.pc = default_breakpoint_address;
6362 sal.line = default_breakpoint_line;
6363 sal.symtab = default_breakpoint_symtab;
6364 sal.pspace = default_breakpoint_pspace;
6365 sal.section = find_pc_overlay (sal.pc);
6366
6367 /* "break" without arguments is equivalent to "break *PC" where PC is
6368 the default_breakpoint_address. So make sure to set
6369 sal.explicit_pc to prevent GDB from trying to expand the list of
6370 sals to include all other instances with the same symtab and line.
6371 */
6372 sal.explicit_pc = 1;
6373
6374 sals->sals[0] = sal;
6375 sals->nelts = 1;
6376 }
6377 else
6378 error (_("No default breakpoint address now."));
6379 }
6380 else
6381 {
6382 /* Force almost all breakpoints to be in terms of the
6383 current_source_symtab (which is decode_line_1's default). This
6384 should produce the results we want almost all of the time while
6385 leaving default_breakpoint_* alone.
6386 ObjC: However, don't match an Objective-C method name which
6387 may have a '+' or '-' succeeded by a '[' */
6388
6389 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
6390
6391 if (default_breakpoint_valid
6392 && (!cursal.symtab
6393 || ((strchr ("+-", (*address)[0]) != NULL)
6394 && ((*address)[1] != '['))))
6395 *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
6396 default_breakpoint_line, addr_string,
6397 not_found_ptr);
6398 else
6399 *sals = decode_line_1 (address, 1, (struct symtab *) NULL, 0,
6400 addr_string, not_found_ptr);
6401 }
6402 /* For any SAL that didn't have a canonical string, fill one in. */
6403 if (sals->nelts > 0 && *addr_string == NULL)
6404 *addr_string = xcalloc (sals->nelts, sizeof (char **));
6405 if (addr_start != (*address))
6406 {
6407 int i;
6408 for (i = 0; i < sals->nelts; i++)
6409 {
6410 /* Add the string if not present. */
6411 if ((*addr_string)[i] == NULL)
6412 (*addr_string)[i] = savestring (addr_start, (*address) - addr_start);
6413 }
6414 }
6415 }
6416
6417
6418 /* Convert each SAL into a real PC. Verify that the PC can be
6419 inserted as a breakpoint. If it can't throw an error. */
6420
6421 static void
6422 breakpoint_sals_to_pc (struct symtabs_and_lines *sals,
6423 char *address)
6424 {
6425 int i;
6426 for (i = 0; i < sals->nelts; i++)
6427 resolve_sal_pc (&sals->sals[i]);
6428 }
6429
6430 static void
6431 do_captured_parse_breakpoint (struct ui_out *ui, void *data)
6432 {
6433 struct captured_parse_breakpoint_args *args = data;
6434
6435 parse_breakpoint_sals (args->arg_p, args->sals_p, args->addr_string_p,
6436 args->not_found_ptr);
6437 }
6438
6439 /* Given TOK, a string specification of condition and thread, as
6440 accepted by the 'break' command, extract the condition
6441 string and thread number and set *COND_STRING and *THREAD.
6442 PC identifies the context at which the condition should be parsed.
6443 If no condition is found, *COND_STRING is set to NULL.
6444 If no thread is found, *THREAD is set to -1. */
6445 static void
6446 find_condition_and_thread (char *tok, CORE_ADDR pc,
6447 char **cond_string, int *thread, int *task)
6448 {
6449 *cond_string = NULL;
6450 *thread = -1;
6451 while (tok && *tok)
6452 {
6453 char *end_tok;
6454 int toklen;
6455 char *cond_start = NULL;
6456 char *cond_end = NULL;
6457 while (*tok == ' ' || *tok == '\t')
6458 tok++;
6459
6460 end_tok = tok;
6461
6462 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
6463 end_tok++;
6464
6465 toklen = end_tok - tok;
6466
6467 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
6468 {
6469 struct expression *expr;
6470
6471 tok = cond_start = end_tok + 1;
6472 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
6473 xfree (expr);
6474 cond_end = tok;
6475 *cond_string = savestring (cond_start,
6476 cond_end - cond_start);
6477 }
6478 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
6479 {
6480 char *tmptok;
6481
6482 tok = end_tok + 1;
6483 tmptok = tok;
6484 *thread = strtol (tok, &tok, 0);
6485 if (tok == tmptok)
6486 error (_("Junk after thread keyword."));
6487 if (!valid_thread_id (*thread))
6488 error (_("Unknown thread %d."), *thread);
6489 }
6490 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
6491 {
6492 char *tmptok;
6493
6494 tok = end_tok + 1;
6495 tmptok = tok;
6496 *task = strtol (tok, &tok, 0);
6497 if (tok == tmptok)
6498 error (_("Junk after task keyword."));
6499 if (!valid_task_id (*task))
6500 error (_("Unknown task %d\n"), *task);
6501 }
6502 else
6503 error (_("Junk at end of arguments."));
6504 }
6505 }
6506
6507 /* Set a breakpoint. This function is shared between
6508 CLI and MI functions for setting a breakpoint.
6509 This function has two major modes of operations,
6510 selected by the PARSE_CONDITION_AND_THREAD parameter.
6511 If non-zero, the function will parse arg, extracting
6512 breakpoint location, address and thread. Otherwise,
6513 ARG is just the location of breakpoint, with condition
6514 and thread specified by the COND_STRING and THREAD
6515 parameters. */
6516
6517 static void
6518 break_command_really (struct gdbarch *gdbarch,
6519 char *arg, char *cond_string, int thread,
6520 int parse_condition_and_thread,
6521 int tempflag, int hardwareflag, int traceflag,
6522 int ignore_count,
6523 enum auto_boolean pending_break_support,
6524 struct breakpoint_ops *ops,
6525 int from_tty,
6526 int enabled)
6527 {
6528 struct gdb_exception e;
6529 struct symtabs_and_lines sals;
6530 struct symtab_and_line pending_sal;
6531 char *copy_arg;
6532 char *err_msg;
6533 char *addr_start = arg;
6534 char **addr_string;
6535 struct cleanup *old_chain;
6536 struct cleanup *bkpt_chain = NULL;
6537 struct captured_parse_breakpoint_args parse_args;
6538 int i;
6539 int pending = 0;
6540 int not_found = 0;
6541 enum bptype type_wanted;
6542 int task = 0;
6543
6544 sals.sals = NULL;
6545 sals.nelts = 0;
6546 addr_string = NULL;
6547
6548 parse_args.arg_p = &arg;
6549 parse_args.sals_p = &sals;
6550 parse_args.addr_string_p = &addr_string;
6551 parse_args.not_found_ptr = &not_found;
6552
6553 e = catch_exception (uiout, do_captured_parse_breakpoint,
6554 &parse_args, RETURN_MASK_ALL);
6555
6556 /* If caller is interested in rc value from parse, set value. */
6557 switch (e.reason)
6558 {
6559 case RETURN_QUIT:
6560 throw_exception (e);
6561 case RETURN_ERROR:
6562 switch (e.error)
6563 {
6564 case NOT_FOUND_ERROR:
6565
6566 /* If pending breakpoint support is turned off, throw
6567 error. */
6568
6569 if (pending_break_support == AUTO_BOOLEAN_FALSE)
6570 throw_exception (e);
6571
6572 exception_print (gdb_stderr, e);
6573
6574 /* If pending breakpoint support is auto query and the user
6575 selects no, then simply return the error code. */
6576 if (pending_break_support == AUTO_BOOLEAN_AUTO
6577 && !nquery ("Make breakpoint pending on future shared library load? "))
6578 return;
6579
6580 /* At this point, either the user was queried about setting
6581 a pending breakpoint and selected yes, or pending
6582 breakpoint behavior is on and thus a pending breakpoint
6583 is defaulted on behalf of the user. */
6584 copy_arg = xstrdup (addr_start);
6585 addr_string = &copy_arg;
6586 sals.nelts = 1;
6587 sals.sals = &pending_sal;
6588 pending_sal.pc = 0;
6589 pending = 1;
6590 break;
6591 default:
6592 throw_exception (e);
6593 }
6594 default:
6595 if (!sals.nelts)
6596 return;
6597 }
6598
6599 /* Create a chain of things that always need to be cleaned up. */
6600 old_chain = make_cleanup (null_cleanup, 0);
6601
6602 if (!pending)
6603 {
6604 /* Make sure that all storage allocated to SALS gets freed. */
6605 make_cleanup (xfree, sals.sals);
6606
6607 /* Cleanup the addr_string array but not its contents. */
6608 make_cleanup (xfree, addr_string);
6609 }
6610
6611 /* ----------------------------- SNIP -----------------------------
6612 Anything added to the cleanup chain beyond this point is assumed
6613 to be part of a breakpoint. If the breakpoint create succeeds
6614 then the memory is not reclaimed. */
6615 bkpt_chain = make_cleanup (null_cleanup, 0);
6616
6617 /* Mark the contents of the addr_string for cleanup. These go on
6618 the bkpt_chain and only occur if the breakpoint create fails. */
6619 for (i = 0; i < sals.nelts; i++)
6620 {
6621 if (addr_string[i] != NULL)
6622 make_cleanup (xfree, addr_string[i]);
6623 }
6624
6625 /* Resolve all line numbers to PC's and verify that the addresses
6626 are ok for the target. */
6627 if (!pending)
6628 breakpoint_sals_to_pc (&sals, addr_start);
6629
6630 type_wanted = (traceflag
6631 ? bp_tracepoint
6632 : (hardwareflag ? bp_hardware_breakpoint : bp_breakpoint));
6633
6634 /* Verify that condition can be parsed, before setting any
6635 breakpoints. Allocate a separate condition expression for each
6636 breakpoint. */
6637 if (!pending)
6638 {
6639 if (parse_condition_and_thread)
6640 {
6641 /* Here we only parse 'arg' to separate condition
6642 from thread number, so parsing in context of first
6643 sal is OK. When setting the breakpoint we'll
6644 re-parse it in context of each sal. */
6645 cond_string = NULL;
6646 thread = -1;
6647 find_condition_and_thread (arg, sals.sals[0].pc, &cond_string,
6648 &thread, &task);
6649 if (cond_string)
6650 make_cleanup (xfree, cond_string);
6651 }
6652 else
6653 {
6654 /* Create a private copy of condition string. */
6655 if (cond_string)
6656 {
6657 cond_string = xstrdup (cond_string);
6658 make_cleanup (xfree, cond_string);
6659 }
6660 }
6661 create_breakpoints (gdbarch, sals, addr_string, cond_string, type_wanted,
6662 tempflag ? disp_del : disp_donttouch,
6663 thread, task, ignore_count, ops, from_tty, enabled);
6664 }
6665 else
6666 {
6667 struct symtab_and_line sal = {0};
6668 struct breakpoint *b;
6669
6670 make_cleanup (xfree, copy_arg);
6671
6672 b = set_raw_breakpoint_without_location (gdbarch, type_wanted);
6673 set_breakpoint_count (breakpoint_count + 1);
6674 b->number = breakpoint_count;
6675 b->thread = -1;
6676 b->addr_string = addr_string[0];
6677 b->cond_string = NULL;
6678 b->ignore_count = ignore_count;
6679 b->disposition = tempflag ? disp_del : disp_donttouch;
6680 b->condition_not_parsed = 1;
6681 b->ops = ops;
6682 b->enable_state = enabled ? bp_enabled : bp_disabled;
6683 b->pspace = current_program_space;
6684
6685 if (enabled && b->pspace->executing_startup
6686 && (b->type == bp_breakpoint
6687 || b->type == bp_hardware_breakpoint))
6688 b->enable_state = bp_startup_disabled;
6689
6690 mention (b);
6691 }
6692
6693 if (sals.nelts > 1)
6694 warning (_("Multiple breakpoints were set.\n"
6695 "Use the \"delete\" command to delete unwanted breakpoints."));
6696 /* That's it. Discard the cleanups for data inserted into the
6697 breakpoint. */
6698 discard_cleanups (bkpt_chain);
6699 /* But cleanup everything else. */
6700 do_cleanups (old_chain);
6701
6702 /* error call may happen here - have BKPT_CHAIN already discarded. */
6703 update_global_location_list (1);
6704 }
6705
6706 /* Set a breakpoint.
6707 ARG is a string describing breakpoint address,
6708 condition, and thread.
6709 FLAG specifies if a breakpoint is hardware on,
6710 and if breakpoint is temporary, using BP_HARDWARE_FLAG
6711 and BP_TEMPFLAG. */
6712
6713 static void
6714 break_command_1 (char *arg, int flag, int from_tty)
6715 {
6716 int hardwareflag = flag & BP_HARDWAREFLAG;
6717 int tempflag = flag & BP_TEMPFLAG;
6718
6719 break_command_really (get_current_arch (),
6720 arg,
6721 NULL, 0, 1 /* parse arg */,
6722 tempflag, hardwareflag, 0 /* traceflag */,
6723 0 /* Ignore count */,
6724 pending_break_support,
6725 NULL /* breakpoint_ops */,
6726 from_tty,
6727 1 /* enabled */);
6728 }
6729
6730
6731 void
6732 set_breakpoint (struct gdbarch *gdbarch,
6733 char *address, char *condition,
6734 int hardwareflag, int tempflag,
6735 int thread, int ignore_count,
6736 int pending, int enabled)
6737 {
6738 break_command_really (gdbarch,
6739 address, condition, thread,
6740 0 /* condition and thread are valid. */,
6741 tempflag, hardwareflag, 0 /* traceflag */,
6742 ignore_count,
6743 pending
6744 ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
6745 NULL, 0, enabled);
6746 }
6747
6748 /* Adjust SAL to the first instruction past the function prologue.
6749 The end of the prologue is determined using the line table from
6750 the debugging information. explicit_pc and explicit_line are
6751 not modified.
6752
6753 If SAL is already past the prologue, then do nothing. */
6754
6755 static void
6756 skip_prologue_sal (struct symtab_and_line *sal)
6757 {
6758 struct symbol *sym;
6759 struct symtab_and_line start_sal;
6760 struct cleanup *old_chain;
6761
6762 old_chain = save_current_space_and_thread ();
6763
6764 sym = find_pc_function (sal->pc);
6765 if (sym != NULL)
6766 {
6767 start_sal = find_function_start_sal (sym, 1);
6768 if (sal->pc < start_sal.pc)
6769 {
6770 start_sal.explicit_line = sal->explicit_line;
6771 start_sal.explicit_pc = sal->explicit_pc;
6772 *sal = start_sal;
6773 }
6774 }
6775
6776 do_cleanups (old_chain);
6777 }
6778
6779 /* Helper function for break_command_1 and disassemble_command. */
6780
6781 void
6782 resolve_sal_pc (struct symtab_and_line *sal)
6783 {
6784 CORE_ADDR pc;
6785
6786 if (sal->pc == 0 && sal->symtab != NULL)
6787 {
6788 if (!find_line_pc (sal->symtab, sal->line, &pc))
6789 error (_("No line %d in file \"%s\"."),
6790 sal->line, sal->symtab->filename);
6791 sal->pc = pc;
6792
6793 /* If this SAL corresponds to a breakpoint inserted using
6794 a line number, then skip the function prologue if necessary. */
6795 if (sal->explicit_line)
6796 {
6797 /* Preserve the original line number. */
6798 int saved_line = sal->line;
6799 skip_prologue_sal (sal);
6800 sal->line = saved_line;
6801 }
6802 }
6803
6804 if (sal->section == 0 && sal->symtab != NULL)
6805 {
6806 struct blockvector *bv;
6807 struct block *b;
6808 struct symbol *sym;
6809
6810 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
6811 if (bv != NULL)
6812 {
6813 sym = block_linkage_function (b);
6814 if (sym != NULL)
6815 {
6816 fixup_symbol_section (sym, sal->symtab->objfile);
6817 sal->section = SYMBOL_OBJ_SECTION (sym);
6818 }
6819 else
6820 {
6821 /* It really is worthwhile to have the section, so we'll just
6822 have to look harder. This case can be executed if we have
6823 line numbers but no functions (as can happen in assembly
6824 source). */
6825
6826 struct minimal_symbol *msym;
6827 struct cleanup *old_chain = save_current_space_and_thread ();
6828
6829 switch_to_program_space_and_thread (sal->pspace);
6830
6831 msym = lookup_minimal_symbol_by_pc (sal->pc);
6832 if (msym)
6833 sal->section = SYMBOL_OBJ_SECTION (msym);
6834
6835 do_cleanups (old_chain);
6836 }
6837 }
6838 }
6839 }
6840
6841 void
6842 break_command (char *arg, int from_tty)
6843 {
6844 break_command_1 (arg, 0, from_tty);
6845 }
6846
6847 void
6848 tbreak_command (char *arg, int from_tty)
6849 {
6850 break_command_1 (arg, BP_TEMPFLAG, from_tty);
6851 }
6852
6853 static void
6854 hbreak_command (char *arg, int from_tty)
6855 {
6856 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
6857 }
6858
6859 static void
6860 thbreak_command (char *arg, int from_tty)
6861 {
6862 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
6863 }
6864
6865 static void
6866 stop_command (char *arg, int from_tty)
6867 {
6868 printf_filtered (_("Specify the type of breakpoint to set.\n\
6869 Usage: stop in <function | address>\n\
6870 stop at <line>\n"));
6871 }
6872
6873 static void
6874 stopin_command (char *arg, int from_tty)
6875 {
6876 int badInput = 0;
6877
6878 if (arg == (char *) NULL)
6879 badInput = 1;
6880 else if (*arg != '*')
6881 {
6882 char *argptr = arg;
6883 int hasColon = 0;
6884
6885 /* look for a ':'. If this is a line number specification, then
6886 say it is bad, otherwise, it should be an address or
6887 function/method name */
6888 while (*argptr && !hasColon)
6889 {
6890 hasColon = (*argptr == ':');
6891 argptr++;
6892 }
6893
6894 if (hasColon)
6895 badInput = (*argptr != ':'); /* Not a class::method */
6896 else
6897 badInput = isdigit (*arg); /* a simple line number */
6898 }
6899
6900 if (badInput)
6901 printf_filtered (_("Usage: stop in <function | address>\n"));
6902 else
6903 break_command_1 (arg, 0, from_tty);
6904 }
6905
6906 static void
6907 stopat_command (char *arg, int from_tty)
6908 {
6909 int badInput = 0;
6910
6911 if (arg == (char *) NULL || *arg == '*') /* no line number */
6912 badInput = 1;
6913 else
6914 {
6915 char *argptr = arg;
6916 int hasColon = 0;
6917
6918 /* look for a ':'. If there is a '::' then get out, otherwise
6919 it is probably a line number. */
6920 while (*argptr && !hasColon)
6921 {
6922 hasColon = (*argptr == ':');
6923 argptr++;
6924 }
6925
6926 if (hasColon)
6927 badInput = (*argptr == ':'); /* we have class::method */
6928 else
6929 badInput = !isdigit (*arg); /* not a line number */
6930 }
6931
6932 if (badInput)
6933 printf_filtered (_("Usage: stop at <line>\n"));
6934 else
6935 break_command_1 (arg, 0, from_tty);
6936 }
6937
6938 /* accessflag: hw_write: watch write,
6939 hw_read: watch read,
6940 hw_access: watch access (read or write) */
6941 static void
6942 watch_command_1 (char *arg, int accessflag, int from_tty)
6943 {
6944 struct gdbarch *gdbarch = get_current_arch ();
6945 struct breakpoint *b, *scope_breakpoint = NULL;
6946 struct symtab_and_line sal;
6947 struct expression *exp;
6948 struct block *exp_valid_block;
6949 struct value *val, *mark;
6950 struct frame_info *frame;
6951 char *exp_start = NULL;
6952 char *exp_end = NULL;
6953 char *tok, *id_tok_start, *end_tok;
6954 int toklen;
6955 char *cond_start = NULL;
6956 char *cond_end = NULL;
6957 struct expression *cond = NULL;
6958 int i, other_type_used, target_resources_ok = 0;
6959 enum bptype bp_type;
6960 int mem_cnt = 0;
6961 int thread = -1;
6962
6963 init_sal (&sal); /* initialize to zeroes */
6964
6965 /* Make sure that we actually have parameters to parse. */
6966 if (arg != NULL && arg[0] != '\0')
6967 {
6968 toklen = strlen (arg); /* Size of argument list. */
6969
6970 /* Points tok to the end of the argument list. */
6971 tok = arg + toklen - 1;
6972
6973 /* Go backwards in the parameters list. Skip the last parameter.
6974 If we're expecting a 'thread <thread_num>' parameter, this should
6975 be the thread identifier. */
6976 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6977 tok--;
6978 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6979 tok--;
6980
6981 /* Points end_tok to the beginning of the last token. */
6982 id_tok_start = tok + 1;
6983
6984 /* Go backwards in the parameters list. Skip one more parameter.
6985 If we're expecting a 'thread <thread_num>' parameter, we should
6986 reach a "thread" token. */
6987 while (tok > arg && (*tok == ' ' || *tok == '\t'))
6988 tok--;
6989
6990 end_tok = tok;
6991
6992 while (tok > arg && (*tok != ' ' && *tok != '\t'))
6993 tok--;
6994
6995 /* Move the pointer forward to skip the whitespace and
6996 calculate the length of the token. */
6997 tok++;
6998 toklen = end_tok - tok;
6999
7000 if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
7001 {
7002 /* At this point we've found a "thread" token, which means
7003 the user is trying to set a watchpoint that triggers
7004 only in a specific thread. */
7005 char *endp;
7006
7007 /* Extract the thread ID from the next token. */
7008 thread = strtol (id_tok_start, &endp, 0);
7009
7010 /* Check if the user provided a valid numeric value for the
7011 thread ID. */
7012 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
7013 error (_("Invalid thread ID specification %s."), id_tok_start);
7014
7015 /* Check if the thread actually exists. */
7016 if (!valid_thread_id (thread))
7017 error (_("Unknown thread %d."), thread);
7018
7019 /* Truncate the string and get rid of the thread <thread_num>
7020 parameter before the parameter list is parsed by the
7021 evaluate_expression() function. */
7022 *tok = '\0';
7023 }
7024 }
7025
7026 sal.pspace = current_program_space;
7027
7028 /* Parse the rest of the arguments. */
7029 innermost_block = NULL;
7030 exp_start = arg;
7031 exp = parse_exp_1 (&arg, 0, 0);
7032 exp_end = arg;
7033 /* Remove trailing whitespace from the expression before saving it.
7034 This makes the eventual display of the expression string a bit
7035 prettier. */
7036 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
7037 --exp_end;
7038
7039 exp_valid_block = innermost_block;
7040 mark = value_mark ();
7041 fetch_watchpoint_value (exp, &val, NULL, NULL);
7042 if (val != NULL)
7043 release_value (val);
7044
7045 tok = arg;
7046 while (*tok == ' ' || *tok == '\t')
7047 tok++;
7048 end_tok = tok;
7049
7050 while (*end_tok != ' ' && *end_tok != '\t' && *end_tok != '\000')
7051 end_tok++;
7052
7053 toklen = end_tok - tok;
7054 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
7055 {
7056 tok = cond_start = end_tok + 1;
7057 cond = parse_exp_1 (&tok, 0, 0);
7058 cond_end = tok;
7059 }
7060 if (*tok)
7061 error (_("Junk at end of command."));
7062
7063 if (accessflag == hw_read)
7064 bp_type = bp_read_watchpoint;
7065 else if (accessflag == hw_access)
7066 bp_type = bp_access_watchpoint;
7067 else
7068 bp_type = bp_hardware_watchpoint;
7069
7070 mem_cnt = can_use_hardware_watchpoint (val);
7071 if (mem_cnt == 0 && bp_type != bp_hardware_watchpoint)
7072 error (_("Expression cannot be implemented with read/access watchpoint."));
7073 if (mem_cnt != 0)
7074 {
7075 i = hw_watchpoint_used_count (bp_type, &other_type_used);
7076 target_resources_ok =
7077 target_can_use_hardware_watchpoint (bp_type, i + mem_cnt,
7078 other_type_used);
7079 if (target_resources_ok == 0 && bp_type != bp_hardware_watchpoint)
7080 error (_("Target does not support this type of hardware watchpoint."));
7081
7082 if (target_resources_ok < 0 && bp_type != bp_hardware_watchpoint)
7083 error (_("Target can only support one kind of HW watchpoint at a time."));
7084 }
7085
7086 /* Change the type of breakpoint to an ordinary watchpoint if a hardware
7087 watchpoint could not be set. */
7088 if (!mem_cnt || target_resources_ok <= 0)
7089 bp_type = bp_watchpoint;
7090
7091 frame = block_innermost_frame (exp_valid_block);
7092
7093 /* If the expression is "local", then set up a "watchpoint scope"
7094 breakpoint at the point where we've left the scope of the watchpoint
7095 expression. Create the scope breakpoint before the watchpoint, so
7096 that we will encounter it first in bpstat_stop_status. */
7097 if (innermost_block && frame)
7098 {
7099 if (frame_id_p (frame_unwind_caller_id (frame)))
7100 {
7101 scope_breakpoint
7102 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
7103 frame_unwind_caller_pc (frame),
7104 bp_watchpoint_scope);
7105
7106 scope_breakpoint->enable_state = bp_enabled;
7107
7108 /* Automatically delete the breakpoint when it hits. */
7109 scope_breakpoint->disposition = disp_del;
7110
7111 /* Only break in the proper frame (help with recursion). */
7112 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
7113
7114 /* Set the address at which we will stop. */
7115 scope_breakpoint->loc->gdbarch
7116 = frame_unwind_caller_arch (frame);
7117 scope_breakpoint->loc->requested_address
7118 = frame_unwind_caller_pc (frame);
7119 scope_breakpoint->loc->address
7120 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
7121 scope_breakpoint->loc->requested_address,
7122 scope_breakpoint->type);
7123 }
7124 }
7125
7126 /* Now set up the breakpoint. */
7127 b = set_raw_breakpoint (gdbarch, sal, bp_type);
7128 set_breakpoint_count (breakpoint_count + 1);
7129 b->number = breakpoint_count;
7130 b->thread = thread;
7131 b->disposition = disp_donttouch;
7132 b->exp = exp;
7133 b->exp_valid_block = exp_valid_block;
7134 b->exp_string = savestring (exp_start, exp_end - exp_start);
7135 b->val = val;
7136 b->val_valid = 1;
7137 b->loc->cond = cond;
7138 if (cond_start)
7139 b->cond_string = savestring (cond_start, cond_end - cond_start);
7140 else
7141 b->cond_string = 0;
7142
7143 if (frame)
7144 b->watchpoint_frame = get_frame_id (frame);
7145 else
7146 b->watchpoint_frame = null_frame_id;
7147
7148 if (scope_breakpoint != NULL)
7149 {
7150 /* The scope breakpoint is related to the watchpoint. We will
7151 need to act on them together. */
7152 b->related_breakpoint = scope_breakpoint;
7153 scope_breakpoint->related_breakpoint = b;
7154 }
7155
7156 value_free_to_mark (mark);
7157 mention (b);
7158 update_global_location_list (1);
7159 }
7160
7161 /* Return count of locations need to be watched and can be handled
7162 in hardware. If the watchpoint can not be handled
7163 in hardware return zero. */
7164
7165 static int
7166 can_use_hardware_watchpoint (struct value *v)
7167 {
7168 int found_memory_cnt = 0;
7169 struct value *head = v;
7170
7171 /* Did the user specifically forbid us to use hardware watchpoints? */
7172 if (!can_use_hw_watchpoints)
7173 return 0;
7174
7175 /* Make sure that the value of the expression depends only upon
7176 memory contents, and values computed from them within GDB. If we
7177 find any register references or function calls, we can't use a
7178 hardware watchpoint.
7179
7180 The idea here is that evaluating an expression generates a series
7181 of values, one holding the value of every subexpression. (The
7182 expression a*b+c has five subexpressions: a, b, a*b, c, and
7183 a*b+c.) GDB's values hold almost enough information to establish
7184 the criteria given above --- they identify memory lvalues,
7185 register lvalues, computed values, etcetera. So we can evaluate
7186 the expression, and then scan the chain of values that leaves
7187 behind to decide whether we can detect any possible change to the
7188 expression's final value using only hardware watchpoints.
7189
7190 However, I don't think that the values returned by inferior
7191 function calls are special in any way. So this function may not
7192 notice that an expression involving an inferior function call
7193 can't be watched with hardware watchpoints. FIXME. */
7194 for (; v; v = value_next (v))
7195 {
7196 if (VALUE_LVAL (v) == lval_memory)
7197 {
7198 if (value_lazy (v))
7199 /* A lazy memory lvalue is one that GDB never needed to fetch;
7200 we either just used its address (e.g., `a' in `a.b') or
7201 we never needed it at all (e.g., `a' in `a,b'). */
7202 ;
7203 else
7204 {
7205 /* Ahh, memory we actually used! Check if we can cover
7206 it with hardware watchpoints. */
7207 struct type *vtype = check_typedef (value_type (v));
7208
7209 /* We only watch structs and arrays if user asked for it
7210 explicitly, never if they just happen to appear in a
7211 middle of some value chain. */
7212 if (v == head
7213 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
7214 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
7215 {
7216 CORE_ADDR vaddr = value_address (v);
7217 int len = TYPE_LENGTH (value_type (v));
7218
7219 if (!target_region_ok_for_hw_watchpoint (vaddr, len))
7220 return 0;
7221 else
7222 found_memory_cnt++;
7223 }
7224 }
7225 }
7226 else if (VALUE_LVAL (v) != not_lval
7227 && deprecated_value_modifiable (v) == 0)
7228 return 0; /* ??? What does this represent? */
7229 else if (VALUE_LVAL (v) == lval_register)
7230 return 0; /* cannot watch a register with a HW watchpoint */
7231 }
7232
7233 /* The expression itself looks suitable for using a hardware
7234 watchpoint, but give the target machine a chance to reject it. */
7235 return found_memory_cnt;
7236 }
7237
7238 void
7239 watch_command_wrapper (char *arg, int from_tty)
7240 {
7241 watch_command (arg, from_tty);
7242 }
7243
7244 static void
7245 watch_command (char *arg, int from_tty)
7246 {
7247 watch_command_1 (arg, hw_write, from_tty);
7248 }
7249
7250 void
7251 rwatch_command_wrapper (char *arg, int from_tty)
7252 {
7253 rwatch_command (arg, from_tty);
7254 }
7255
7256 static void
7257 rwatch_command (char *arg, int from_tty)
7258 {
7259 watch_command_1 (arg, hw_read, from_tty);
7260 }
7261
7262 void
7263 awatch_command_wrapper (char *arg, int from_tty)
7264 {
7265 awatch_command (arg, from_tty);
7266 }
7267
7268 static void
7269 awatch_command (char *arg, int from_tty)
7270 {
7271 watch_command_1 (arg, hw_access, from_tty);
7272 }
7273 \f
7274
7275 /* Helper routines for the until_command routine in infcmd.c. Here
7276 because it uses the mechanisms of breakpoints. */
7277
7278 struct until_break_command_continuation_args
7279 {
7280 struct breakpoint *breakpoint;
7281 struct breakpoint *breakpoint2;
7282 };
7283
7284 /* This function is called by fetch_inferior_event via the
7285 cmd_continuation pointer, to complete the until command. It takes
7286 care of cleaning up the temporary breakpoints set up by the until
7287 command. */
7288 static void
7289 until_break_command_continuation (void *arg)
7290 {
7291 struct until_break_command_continuation_args *a = arg;
7292
7293 delete_breakpoint (a->breakpoint);
7294 if (a->breakpoint2)
7295 delete_breakpoint (a->breakpoint2);
7296 }
7297
7298 void
7299 until_break_command (char *arg, int from_tty, int anywhere)
7300 {
7301 struct symtabs_and_lines sals;
7302 struct symtab_and_line sal;
7303 struct frame_info *frame = get_selected_frame (NULL);
7304 struct breakpoint *breakpoint;
7305 struct breakpoint *breakpoint2 = NULL;
7306 struct cleanup *old_chain;
7307
7308 clear_proceed_status ();
7309
7310 /* Set a breakpoint where the user wants it and at return from
7311 this function */
7312
7313 if (default_breakpoint_valid)
7314 sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
7315 default_breakpoint_line, (char ***) NULL, NULL);
7316 else
7317 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL,
7318 0, (char ***) NULL, NULL);
7319
7320 if (sals.nelts != 1)
7321 error (_("Couldn't get information on specified line."));
7322
7323 sal = sals.sals[0];
7324 xfree (sals.sals); /* malloc'd, so freed */
7325
7326 if (*arg)
7327 error (_("Junk at end of arguments."));
7328
7329 resolve_sal_pc (&sal);
7330
7331 if (anywhere)
7332 /* If the user told us to continue until a specified location,
7333 we don't specify a frame at which we need to stop. */
7334 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
7335 null_frame_id, bp_until);
7336 else
7337 /* Otherwise, specify the selected frame, because we want to stop only
7338 at the very same frame. */
7339 breakpoint = set_momentary_breakpoint (get_frame_arch (frame), sal,
7340 get_stack_frame_id (frame),
7341 bp_until);
7342
7343 old_chain = make_cleanup_delete_breakpoint (breakpoint);
7344
7345 /* Keep within the current frame, or in frames called by the current
7346 one. */
7347
7348 if (frame_id_p (frame_unwind_caller_id (frame)))
7349 {
7350 sal = find_pc_line (frame_unwind_caller_pc (frame), 0);
7351 sal.pc = frame_unwind_caller_pc (frame);
7352 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
7353 sal,
7354 frame_unwind_caller_id (frame),
7355 bp_until);
7356 make_cleanup_delete_breakpoint (breakpoint2);
7357 }
7358
7359 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
7360
7361 /* If we are running asynchronously, and proceed call above has actually
7362 managed to start the target, arrange for breakpoints to be
7363 deleted when the target stops. Otherwise, we're already stopped and
7364 delete breakpoints via cleanup chain. */
7365
7366 if (target_can_async_p () && is_running (inferior_ptid))
7367 {
7368 struct until_break_command_continuation_args *args;
7369 args = xmalloc (sizeof (*args));
7370
7371 args->breakpoint = breakpoint;
7372 args->breakpoint2 = breakpoint2;
7373
7374 discard_cleanups (old_chain);
7375 add_continuation (inferior_thread (),
7376 until_break_command_continuation, args,
7377 xfree);
7378 }
7379 else
7380 do_cleanups (old_chain);
7381 }
7382
7383 static void
7384 ep_skip_leading_whitespace (char **s)
7385 {
7386 if ((s == NULL) || (*s == NULL))
7387 return;
7388 while (isspace (**s))
7389 *s += 1;
7390 }
7391
7392 /* This function attempts to parse an optional "if <cond>" clause
7393 from the arg string. If one is not found, it returns NULL.
7394
7395 Else, it returns a pointer to the condition string. (It does not
7396 attempt to evaluate the string against a particular block.) And,
7397 it updates arg to point to the first character following the parsed
7398 if clause in the arg string. */
7399
7400 static char *
7401 ep_parse_optional_if_clause (char **arg)
7402 {
7403 char *cond_string;
7404
7405 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
7406 return NULL;
7407
7408 /* Skip the "if" keyword. */
7409 (*arg) += 2;
7410
7411 /* Skip any extra leading whitespace, and record the start of the
7412 condition string. */
7413 ep_skip_leading_whitespace (arg);
7414 cond_string = *arg;
7415
7416 /* Assume that the condition occupies the remainder of the arg string. */
7417 (*arg) += strlen (cond_string);
7418
7419 return cond_string;
7420 }
7421
7422 /* This function attempts to parse an optional filename from the arg
7423 string. If one is not found, it returns NULL.
7424
7425 Else, it returns a pointer to the parsed filename. (This function
7426 makes no attempt to verify that a file of that name exists, or is
7427 accessible.) And, it updates arg to point to the first character
7428 following the parsed filename in the arg string.
7429
7430 Note that clients needing to preserve the returned filename for
7431 future access should copy it to their own buffers. */
7432 static char *
7433 ep_parse_optional_filename (char **arg)
7434 {
7435 static char filename[1024];
7436 char *arg_p = *arg;
7437 int i;
7438 char c;
7439
7440 if ((*arg_p == '\0') || isspace (*arg_p))
7441 return NULL;
7442
7443 for (i = 0;; i++)
7444 {
7445 c = *arg_p;
7446 if (isspace (c))
7447 c = '\0';
7448 filename[i] = c;
7449 if (c == '\0')
7450 break;
7451 arg_p++;
7452 }
7453 *arg = arg_p;
7454
7455 return filename;
7456 }
7457
7458 /* Commands to deal with catching events, such as signals, exceptions,
7459 process start/exit, etc. */
7460
7461 typedef enum
7462 {
7463 catch_fork_temporary, catch_vfork_temporary,
7464 catch_fork_permanent, catch_vfork_permanent
7465 }
7466 catch_fork_kind;
7467
7468 static void
7469 catch_fork_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
7470 {
7471 struct gdbarch *gdbarch = get_current_arch ();
7472 char *cond_string = NULL;
7473 catch_fork_kind fork_kind;
7474 int tempflag;
7475
7476 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
7477 tempflag = (fork_kind == catch_fork_temporary
7478 || fork_kind == catch_vfork_temporary);
7479
7480 if (!arg)
7481 arg = "";
7482 ep_skip_leading_whitespace (&arg);
7483
7484 /* The allowed syntax is:
7485 catch [v]fork
7486 catch [v]fork if <cond>
7487
7488 First, check if there's an if clause. */
7489 cond_string = ep_parse_optional_if_clause (&arg);
7490
7491 if ((*arg != '\0') && !isspace (*arg))
7492 error (_("Junk at end of arguments."));
7493
7494 /* If this target supports it, create a fork or vfork catchpoint
7495 and enable reporting of such events. */
7496 switch (fork_kind)
7497 {
7498 case catch_fork_temporary:
7499 case catch_fork_permanent:
7500 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
7501 &catch_fork_breakpoint_ops);
7502 break;
7503 case catch_vfork_temporary:
7504 case catch_vfork_permanent:
7505 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
7506 &catch_vfork_breakpoint_ops);
7507 break;
7508 default:
7509 error (_("unsupported or unknown fork kind; cannot catch it"));
7510 break;
7511 }
7512 }
7513
7514 static void
7515 catch_exec_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
7516 {
7517 struct gdbarch *gdbarch = get_current_arch ();
7518 int tempflag;
7519 char *cond_string = NULL;
7520
7521 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7522
7523 if (!arg)
7524 arg = "";
7525 ep_skip_leading_whitespace (&arg);
7526
7527 /* The allowed syntax is:
7528 catch exec
7529 catch exec if <cond>
7530
7531 First, check if there's an if clause. */
7532 cond_string = ep_parse_optional_if_clause (&arg);
7533
7534 if ((*arg != '\0') && !isspace (*arg))
7535 error (_("Junk at end of arguments."));
7536
7537 /* If this target supports it, create an exec catchpoint
7538 and enable reporting of such events. */
7539 create_catchpoint (gdbarch, tempflag, cond_string,
7540 &catch_exec_breakpoint_ops);
7541 }
7542
7543 static enum print_stop_action
7544 print_exception_catchpoint (struct breakpoint *b)
7545 {
7546 int bp_temp, bp_throw;
7547
7548 annotate_catchpoint (b->number);
7549
7550 bp_throw = strstr (b->addr_string, "throw") != NULL;
7551 if (b->loc->address != b->loc->requested_address)
7552 breakpoint_adjustment_warning (b->loc->requested_address,
7553 b->loc->address,
7554 b->number, 1);
7555 bp_temp = b->disposition == disp_del;
7556 ui_out_text (uiout,
7557 bp_temp ? "Temporary catchpoint "
7558 : "Catchpoint ");
7559 if (!ui_out_is_mi_like_p (uiout))
7560 ui_out_field_int (uiout, "bkptno", b->number);
7561 ui_out_text (uiout,
7562 bp_throw ? " (exception thrown), "
7563 : " (exception caught), ");
7564 if (ui_out_is_mi_like_p (uiout))
7565 {
7566 ui_out_field_string (uiout, "reason",
7567 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
7568 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7569 ui_out_field_int (uiout, "bkptno", b->number);
7570 }
7571 return PRINT_SRC_AND_LOC;
7572 }
7573
7574 static void
7575 print_one_exception_catchpoint (struct breakpoint *b, struct bp_location **last_loc)
7576 {
7577 struct value_print_options opts;
7578 get_user_print_options (&opts);
7579 if (opts.addressprint)
7580 {
7581 annotate_field (4);
7582 if (b->loc == NULL || b->loc->shlib_disabled)
7583 ui_out_field_string (uiout, "addr", "<PENDING>");
7584 else
7585 ui_out_field_core_addr (uiout, "addr",
7586 b->loc->gdbarch, b->loc->address);
7587 }
7588 annotate_field (5);
7589 if (b->loc)
7590 *last_loc = b->loc;
7591 if (strstr (b->addr_string, "throw") != NULL)
7592 ui_out_field_string (uiout, "what", "exception throw");
7593 else
7594 ui_out_field_string (uiout, "what", "exception catch");
7595 }
7596
7597 static void
7598 print_mention_exception_catchpoint (struct breakpoint *b)
7599 {
7600 int bp_temp;
7601 int bp_throw;
7602
7603 bp_temp = b->disposition == disp_del;
7604 bp_throw = strstr (b->addr_string, "throw") != NULL;
7605 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
7606 : _("Catchpoint "));
7607 ui_out_field_int (uiout, "bkptno", b->number);
7608 ui_out_text (uiout, bp_throw ? _(" (throw)")
7609 : _(" (catch)"));
7610 }
7611
7612 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops = {
7613 NULL, /* insert */
7614 NULL, /* remove */
7615 NULL, /* breakpoint_hit */
7616 print_exception_catchpoint,
7617 print_one_exception_catchpoint,
7618 print_mention_exception_catchpoint
7619 };
7620
7621 static int
7622 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
7623 enum exception_event_kind ex_event, int from_tty)
7624 {
7625 char *trigger_func_name;
7626
7627 if (ex_event == EX_EVENT_CATCH)
7628 trigger_func_name = "__cxa_begin_catch";
7629 else
7630 trigger_func_name = "__cxa_throw";
7631
7632 break_command_really (get_current_arch (),
7633 trigger_func_name, cond_string, -1,
7634 0 /* condition and thread are valid. */,
7635 tempflag, 0, 0,
7636 0,
7637 AUTO_BOOLEAN_TRUE /* pending */,
7638 &gnu_v3_exception_catchpoint_ops, from_tty,
7639 1 /* enabled */);
7640
7641 return 1;
7642 }
7643
7644 /* Deal with "catch catch" and "catch throw" commands */
7645
7646 static void
7647 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
7648 int tempflag, int from_tty)
7649 {
7650 char *cond_string = NULL;
7651 struct symtab_and_line *sal = NULL;
7652
7653 if (!arg)
7654 arg = "";
7655 ep_skip_leading_whitespace (&arg);
7656
7657 cond_string = ep_parse_optional_if_clause (&arg);
7658
7659 if ((*arg != '\0') && !isspace (*arg))
7660 error (_("Junk at end of arguments."));
7661
7662 if (ex_event != EX_EVENT_THROW
7663 && ex_event != EX_EVENT_CATCH)
7664 error (_("Unsupported or unknown exception event; cannot catch it"));
7665
7666 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
7667 return;
7668
7669 warning (_("Unsupported with this platform/compiler combination."));
7670 }
7671
7672 /* Implementation of "catch catch" command. */
7673
7674 static void
7675 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
7676 {
7677 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7678 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
7679 }
7680
7681 /* Implementation of "catch throw" command. */
7682
7683 static void
7684 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
7685 {
7686 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7687 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
7688 }
7689
7690 /* Create a breakpoint struct for Ada exception catchpoints. */
7691
7692 static void
7693 create_ada_exception_breakpoint (struct gdbarch *gdbarch,
7694 struct symtab_and_line sal,
7695 char *addr_string,
7696 char *exp_string,
7697 char *cond_string,
7698 struct expression *cond,
7699 struct breakpoint_ops *ops,
7700 int tempflag,
7701 int from_tty)
7702 {
7703 struct breakpoint *b;
7704
7705 if (from_tty)
7706 {
7707 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
7708 if (!loc_gdbarch)
7709 loc_gdbarch = gdbarch;
7710
7711 describe_other_breakpoints (loc_gdbarch,
7712 sal.pspace, sal.pc, sal.section, -1);
7713 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
7714 version for exception catchpoints, because two catchpoints
7715 used for different exception names will use the same address.
7716 In this case, a "breakpoint ... also set at..." warning is
7717 unproductive. Besides. the warning phrasing is also a bit
7718 inapropriate, we should use the word catchpoint, and tell
7719 the user what type of catchpoint it is. The above is good
7720 enough for now, though. */
7721 }
7722
7723 b = set_raw_breakpoint (gdbarch, sal, bp_breakpoint);
7724 set_breakpoint_count (breakpoint_count + 1);
7725
7726 b->enable_state = bp_enabled;
7727 b->disposition = tempflag ? disp_del : disp_donttouch;
7728 b->number = breakpoint_count;
7729 b->ignore_count = 0;
7730 b->loc->cond = cond;
7731 b->addr_string = addr_string;
7732 b->language = language_ada;
7733 b->cond_string = cond_string;
7734 b->exp_string = exp_string;
7735 b->thread = -1;
7736 b->ops = ops;
7737
7738 mention (b);
7739 update_global_location_list (1);
7740 }
7741
7742 /* Implement the "catch exception" command. */
7743
7744 static void
7745 catch_ada_exception_command (char *arg, int from_tty,
7746 struct cmd_list_element *command)
7747 {
7748 struct gdbarch *gdbarch = get_current_arch ();
7749 int tempflag;
7750 struct symtab_and_line sal;
7751 enum bptype type;
7752 char *addr_string = NULL;
7753 char *exp_string = NULL;
7754 char *cond_string = NULL;
7755 struct expression *cond = NULL;
7756 struct breakpoint_ops *ops = NULL;
7757
7758 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7759
7760 if (!arg)
7761 arg = "";
7762 sal = ada_decode_exception_location (arg, &addr_string, &exp_string,
7763 &cond_string, &cond, &ops);
7764 create_ada_exception_breakpoint (gdbarch, sal, addr_string, exp_string,
7765 cond_string, cond, ops, tempflag,
7766 from_tty);
7767 }
7768
7769 /* Cleanup function for a syscall filter list. */
7770 static void
7771 clean_up_filters (void *arg)
7772 {
7773 VEC(int) *iter = *(VEC(int) **) arg;
7774 VEC_free (int, iter);
7775 }
7776
7777 /* Splits the argument using space as delimiter. Returns an xmalloc'd
7778 filter list, or NULL if no filtering is required. */
7779 static VEC(int) *
7780 catch_syscall_split_args (char *arg)
7781 {
7782 VEC(int) *result = NULL;
7783 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
7784
7785 while (*arg != '\0')
7786 {
7787 int i, syscall_number;
7788 char *endptr;
7789 char cur_name[128];
7790 struct syscall s;
7791
7792 /* Skip whitespace. */
7793 while (isspace (*arg))
7794 arg++;
7795
7796 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
7797 cur_name[i] = arg[i];
7798 cur_name[i] = '\0';
7799 arg += i;
7800
7801 /* Check if the user provided a syscall name or a number. */
7802 syscall_number = (int) strtol (cur_name, &endptr, 0);
7803 if (*endptr == '\0')
7804 get_syscall_by_number (syscall_number, &s);
7805 else
7806 {
7807 /* We have a name. Let's check if it's valid and convert it
7808 to a number. */
7809 get_syscall_by_name (cur_name, &s);
7810
7811 if (s.number == UNKNOWN_SYSCALL)
7812 /* Here we have to issue an error instead of a warning, because
7813 GDB cannot do anything useful if there's no syscall number to
7814 be caught. */
7815 error (_("Unknown syscall name '%s'."), cur_name);
7816 }
7817
7818 /* Ok, it's valid. */
7819 VEC_safe_push (int, result, s.number);
7820 }
7821
7822 discard_cleanups (cleanup);
7823 return result;
7824 }
7825
7826 /* Implement the "catch syscall" command. */
7827
7828 static void
7829 catch_syscall_command_1 (char *arg, int from_tty, struct cmd_list_element *command)
7830 {
7831 int tempflag;
7832 VEC(int) *filter;
7833 struct syscall s;
7834 struct gdbarch *gdbarch = get_current_arch ();
7835
7836 /* Checking if the feature if supported. */
7837 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
7838 error (_("The feature 'catch syscall' is not supported on \
7839 this architeture yet."));
7840
7841 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7842
7843 ep_skip_leading_whitespace (&arg);
7844
7845 /* We need to do this first "dummy" translation in order
7846 to get the syscall XML file loaded or, most important,
7847 to display a warning to the user if there's no XML file
7848 for his/her architecture. */
7849 get_syscall_by_number (0, &s);
7850
7851 /* The allowed syntax is:
7852 catch syscall
7853 catch syscall <name | number> [<name | number> ... <name | number>]
7854
7855 Let's check if there's a syscall name. */
7856
7857 if (arg != NULL)
7858 filter = catch_syscall_split_args (arg);
7859 else
7860 filter = NULL;
7861
7862 create_syscall_event_catchpoint (tempflag, filter,
7863 &catch_syscall_breakpoint_ops);
7864 }
7865
7866 /* Implement the "catch assert" command. */
7867
7868 static void
7869 catch_assert_command (char *arg, int from_tty, struct cmd_list_element *command)
7870 {
7871 struct gdbarch *gdbarch = get_current_arch ();
7872 int tempflag;
7873 struct symtab_and_line sal;
7874 char *addr_string = NULL;
7875 struct breakpoint_ops *ops = NULL;
7876
7877 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7878
7879 if (!arg)
7880 arg = "";
7881 sal = ada_decode_assert_location (arg, &addr_string, &ops);
7882 create_ada_exception_breakpoint (gdbarch, sal, addr_string, NULL, NULL, NULL,
7883 ops, tempflag, from_tty);
7884 }
7885
7886 static void
7887 catch_command (char *arg, int from_tty)
7888 {
7889 error (_("Catch requires an event name."));
7890 }
7891 \f
7892
7893 static void
7894 tcatch_command (char *arg, int from_tty)
7895 {
7896 error (_("Catch requires an event name."));
7897 }
7898
7899 /* Delete breakpoints by address or line. */
7900
7901 static void
7902 clear_command (char *arg, int from_tty)
7903 {
7904 struct breakpoint *b;
7905 VEC(breakpoint_p) *found = 0;
7906 int ix;
7907 int default_match;
7908 struct symtabs_and_lines sals;
7909 struct symtab_and_line sal;
7910 int i;
7911
7912 if (arg)
7913 {
7914 sals = decode_line_spec (arg, 1);
7915 default_match = 0;
7916 }
7917 else
7918 {
7919 sals.sals = (struct symtab_and_line *)
7920 xmalloc (sizeof (struct symtab_and_line));
7921 make_cleanup (xfree, sals.sals);
7922 init_sal (&sal); /* initialize to zeroes */
7923 sal.line = default_breakpoint_line;
7924 sal.symtab = default_breakpoint_symtab;
7925 sal.pc = default_breakpoint_address;
7926 sal.pspace = default_breakpoint_pspace;
7927 if (sal.symtab == 0)
7928 error (_("No source file specified."));
7929
7930 sals.sals[0] = sal;
7931 sals.nelts = 1;
7932
7933 default_match = 1;
7934 }
7935
7936 /* We don't call resolve_sal_pc here. That's not
7937 as bad as it seems, because all existing breakpoints
7938 typically have both file/line and pc set. So, if
7939 clear is given file/line, we can match this to existing
7940 breakpoint without obtaining pc at all.
7941
7942 We only support clearing given the address explicitly
7943 present in breakpoint table. Say, we've set breakpoint
7944 at file:line. There were several PC values for that file:line,
7945 due to optimization, all in one block.
7946 We've picked one PC value. If "clear" is issued with another
7947 PC corresponding to the same file:line, the breakpoint won't
7948 be cleared. We probably can still clear the breakpoint, but
7949 since the other PC value is never presented to user, user
7950 can only find it by guessing, and it does not seem important
7951 to support that. */
7952
7953 /* For each line spec given, delete bps which correspond
7954 to it. Do it in two passes, solely to preserve the current
7955 behavior that from_tty is forced true if we delete more than
7956 one breakpoint. */
7957
7958 found = NULL;
7959 for (i = 0; i < sals.nelts; i++)
7960 {
7961 /* If exact pc given, clear bpts at that pc.
7962 If line given (pc == 0), clear all bpts on specified line.
7963 If defaulting, clear all bpts on default line
7964 or at default pc.
7965
7966 defaulting sal.pc != 0 tests to do
7967
7968 0 1 pc
7969 1 1 pc _and_ line
7970 0 0 line
7971 1 0 <can't happen> */
7972
7973 sal = sals.sals[i];
7974
7975 /* Find all matching breakpoints and add them to
7976 'found'. */
7977 ALL_BREAKPOINTS (b)
7978 {
7979 int match = 0;
7980 /* Are we going to delete b? */
7981 if (b->type != bp_none
7982 && b->type != bp_watchpoint
7983 && b->type != bp_hardware_watchpoint
7984 && b->type != bp_read_watchpoint
7985 && b->type != bp_access_watchpoint)
7986 {
7987 struct bp_location *loc = b->loc;
7988 for (; loc; loc = loc->next)
7989 {
7990 int pc_match = sal.pc
7991 && (loc->pspace == sal.pspace)
7992 && (loc->address == sal.pc)
7993 && (!section_is_overlay (loc->section)
7994 || loc->section == sal.section);
7995 int line_match = ((default_match || (0 == sal.pc))
7996 && b->source_file != NULL
7997 && sal.symtab != NULL
7998 && sal.pspace == loc->pspace
7999 && strcmp (b->source_file, sal.symtab->filename) == 0
8000 && b->line_number == sal.line);
8001 if (pc_match || line_match)
8002 {
8003 match = 1;
8004 break;
8005 }
8006 }
8007 }
8008
8009 if (match)
8010 VEC_safe_push(breakpoint_p, found, b);
8011 }
8012 }
8013 /* Now go thru the 'found' chain and delete them. */
8014 if (VEC_empty(breakpoint_p, found))
8015 {
8016 if (arg)
8017 error (_("No breakpoint at %s."), arg);
8018 else
8019 error (_("No breakpoint at this line."));
8020 }
8021
8022 if (VEC_length(breakpoint_p, found) > 1)
8023 from_tty = 1; /* Always report if deleted more than one */
8024 if (from_tty)
8025 {
8026 if (VEC_length(breakpoint_p, found) == 1)
8027 printf_unfiltered (_("Deleted breakpoint "));
8028 else
8029 printf_unfiltered (_("Deleted breakpoints "));
8030 }
8031 breakpoints_changed ();
8032
8033 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
8034 {
8035 if (from_tty)
8036 printf_unfiltered ("%d ", b->number);
8037 delete_breakpoint (b);
8038 }
8039 if (from_tty)
8040 putchar_unfiltered ('\n');
8041 }
8042 \f
8043 /* Delete breakpoint in BS if they are `delete' breakpoints and
8044 all breakpoints that are marked for deletion, whether hit or not.
8045 This is called after any breakpoint is hit, or after errors. */
8046
8047 void
8048 breakpoint_auto_delete (bpstat bs)
8049 {
8050 struct breakpoint *b, *temp;
8051
8052 for (; bs; bs = bs->next)
8053 if (bs->breakpoint_at
8054 && bs->breakpoint_at->owner
8055 && bs->breakpoint_at->owner->disposition == disp_del
8056 && bs->stop)
8057 delete_breakpoint (bs->breakpoint_at->owner);
8058
8059 ALL_BREAKPOINTS_SAFE (b, temp)
8060 {
8061 if (b->disposition == disp_del_at_next_stop)
8062 delete_breakpoint (b);
8063 }
8064 }
8065
8066 /* A comparison function for bp_location A and B being interfaced to qsort.
8067 Sort elements primarily by their ADDRESS (no matter what does
8068 breakpoint_address_is_meaningful say for its OWNER), secondarily by ordering
8069 first bp_permanent OWNERed elements and terciarily just ensuring the array
8070 is sorted stable way despite qsort being an instable algorithm. */
8071
8072 static int
8073 bp_location_compare (struct bp_location *a, struct bp_location *b)
8074 {
8075 int a_perm = a->owner->enable_state == bp_permanent;
8076 int b_perm = b->owner->enable_state == bp_permanent;
8077
8078 if (a->address != b->address)
8079 return (a->address > b->address) - (a->address < b->address);
8080
8081 /* Sort permanent breakpoints first. */
8082 if (a_perm != b_perm)
8083 return (a_perm < b_perm) - (a_perm > b_perm);
8084
8085 /* Make the user-visible order stable across GDB runs. Locations of the same
8086 breakpoint can be sorted in arbitrary order. */
8087
8088 if (a->owner->number != b->owner->number)
8089 return (a->owner->number > b->owner->number)
8090 - (a->owner->number < b->owner->number);
8091
8092 return (a > b) - (a < b);
8093 }
8094
8095 /* Interface bp_location_compare as the COMPAR parameter of qsort function. */
8096
8097 static int
8098 bp_location_compare_for_qsort (const void *ap, const void *bp)
8099 {
8100 struct bp_location *a = *(void **) ap;
8101 struct bp_location *b = *(void **) bp;
8102
8103 return bp_location_compare (a, b);
8104 }
8105
8106 /* Set bp_location_placed_address_before_address_max and
8107 bp_location_shadow_len_after_address_max according to the current content of
8108 the bp_location array. */
8109
8110 static void
8111 bp_location_target_extensions_update (void)
8112 {
8113 struct bp_location *bl, **blp_tmp;
8114
8115 bp_location_placed_address_before_address_max = 0;
8116 bp_location_shadow_len_after_address_max = 0;
8117
8118 ALL_BP_LOCATIONS (bl, blp_tmp)
8119 {
8120 CORE_ADDR start, end, addr;
8121
8122 if (!bp_location_has_shadow (bl))
8123 continue;
8124
8125 start = bl->target_info.placed_address;
8126 end = start + bl->target_info.shadow_len;
8127
8128 gdb_assert (bl->address >= start);
8129 addr = bl->address - start;
8130 if (addr > bp_location_placed_address_before_address_max)
8131 bp_location_placed_address_before_address_max = addr;
8132
8133 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
8134
8135 gdb_assert (bl->address < end);
8136 addr = end - bl->address;
8137 if (addr > bp_location_shadow_len_after_address_max)
8138 bp_location_shadow_len_after_address_max = addr;
8139 }
8140 }
8141
8142 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
8143 into the inferior, only remove already-inserted locations that no
8144 longer should be inserted. Functions that delete a breakpoint or
8145 breakpoints should pass false, so that deleting a breakpoint
8146 doesn't have the side effect of inserting the locations of other
8147 breakpoints that are marked not-inserted, but should_be_inserted
8148 returns true on them.
8149
8150 This behaviour is useful is situations close to tear-down -- e.g.,
8151 after an exec, while the target still has execution, but breakpoint
8152 shadows of the previous executable image should *NOT* be restored
8153 to the new image; or before detaching, where the target still has
8154 execution and wants to delete breakpoints from GDB's lists, and all
8155 breakpoints had already been removed from the inferior. */
8156
8157 static void
8158 update_global_location_list (int should_insert)
8159 {
8160 struct breakpoint *b;
8161 struct bp_location **locp, *loc;
8162 struct cleanup *cleanups;
8163
8164 /* The first bp_location being the only one non-DUPLICATE for the current run
8165 of the same ADDRESS. */
8166 struct bp_location *loc_first;
8167
8168 /* Saved former bp_location array which we compare against the newly built
8169 bp_location from the current state of ALL_BREAKPOINTS. */
8170 struct bp_location **old_location, **old_locp;
8171 unsigned old_location_count;
8172
8173 old_location = bp_location;
8174 old_location_count = bp_location_count;
8175 bp_location = NULL;
8176 bp_location_count = 0;
8177 cleanups = make_cleanup (xfree, old_location);
8178
8179 ALL_BREAKPOINTS (b)
8180 for (loc = b->loc; loc; loc = loc->next)
8181 bp_location_count++;
8182
8183 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
8184 locp = bp_location;
8185 ALL_BREAKPOINTS (b)
8186 for (loc = b->loc; loc; loc = loc->next)
8187 *locp++ = loc;
8188 qsort (bp_location, bp_location_count, sizeof (*bp_location),
8189 bp_location_compare_for_qsort);
8190
8191 bp_location_target_extensions_update ();
8192
8193 /* Identify bp_location instances that are no longer present in the new
8194 list, and therefore should be freed. Note that it's not necessary that
8195 those locations should be removed from inferior -- if there's another
8196 location at the same address (previously marked as duplicate),
8197 we don't need to remove/insert the location.
8198
8199 LOCP is kept in sync with OLD_LOCP, each pointing to the current and
8200 former bp_location array state respectively. */
8201
8202 locp = bp_location;
8203 for (old_locp = old_location; old_locp < old_location + old_location_count;
8204 old_locp++)
8205 {
8206 struct bp_location *old_loc = *old_locp;
8207
8208 /* Tells if 'old_loc' is found amoung the new locations. If not, we
8209 have to free it. */
8210 int found_object;
8211 /* Tells if the location should remain inserted in the target. */
8212 int keep_in_target = 0;
8213 int removed = 0;
8214
8215 /* Skip LOCP entries which will definitely never be needed. Stop either
8216 at or being the one matching OLD_LOC. */
8217 while (locp < bp_location + bp_location_count
8218 && bp_location_compare (*locp, old_loc) < 0)
8219 locp++;
8220 found_object = locp < bp_location + bp_location_count && *locp == old_loc;
8221
8222 /* If this location is no longer present, and inserted, look if there's
8223 maybe a new location at the same address. If so, mark that one
8224 inserted, and don't remove this one. This is needed so that we
8225 don't have a time window where a breakpoint at certain location is not
8226 inserted. */
8227
8228 if (old_loc->inserted)
8229 {
8230 /* If the location is inserted now, we might have to remove it. */
8231
8232 if (found_object && should_be_inserted (old_loc))
8233 {
8234 /* The location is still present in the location list, and still
8235 should be inserted. Don't do anything. */
8236 keep_in_target = 1;
8237 }
8238 else
8239 {
8240 /* The location is either no longer present, or got disabled.
8241 See if there's another location at the same address, in which
8242 case we don't need to remove this one from the target. */
8243
8244 if (breakpoint_address_is_meaningful (old_loc->owner))
8245 {
8246 struct bp_location **loc2p;
8247
8248 for (loc2p = locp;
8249 loc2p < bp_location + bp_location_count
8250 && breakpoint_address_match ((*loc2p)->pspace->aspace,
8251 (*loc2p)->address,
8252 old_loc->pspace->aspace,
8253 old_loc->address);
8254 loc2p++)
8255 {
8256 struct bp_location *loc2 = *loc2p;
8257
8258 /* For the sake of should_be_inserted.
8259 Duplicates check below will fix up this later. */
8260 loc2->duplicate = 0;
8261 if (loc2 != old_loc && should_be_inserted (loc2))
8262 {
8263 loc2->inserted = 1;
8264 loc2->target_info = old_loc->target_info;
8265 keep_in_target = 1;
8266 break;
8267 }
8268 }
8269 }
8270 }
8271
8272 if (!keep_in_target)
8273 {
8274 if (remove_breakpoint (old_loc, mark_uninserted))
8275 {
8276 /* This is just about all we can do. We could keep this
8277 location on the global list, and try to remove it next
8278 time, but there's no particular reason why we will
8279 succeed next time.
8280
8281 Note that at this point, old_loc->owner is still valid,
8282 as delete_breakpoint frees the breakpoint only
8283 after calling us. */
8284 printf_filtered (_("warning: Error removing breakpoint %d\n"),
8285 old_loc->owner->number);
8286 }
8287 removed = 1;
8288 }
8289 }
8290
8291 if (!found_object)
8292 {
8293 if (removed && non_stop)
8294 {
8295 /* This location was removed from the targets. In non-stop mode,
8296 a race condition is possible where we've removed a breakpoint,
8297 but stop events for that breakpoint are already queued and will
8298 arrive later. To suppress spurious SIGTRAPs reported to user,
8299 we keep this breakpoint location for a bit, and will retire it
8300 after we see 3 * thread_count events.
8301 The theory here is that reporting of events should,
8302 "on the average", be fair, so after that many event we'll see
8303 events from all threads that have anything of interest, and no
8304 longer need to keep this breakpoint. This is just a
8305 heuristic, but if it's wrong, we'll report unexpected SIGTRAP,
8306 which is usability issue, but not a correctness problem. */
8307 old_loc->events_till_retirement = 3 * (thread_count () + 1);
8308 old_loc->owner = NULL;
8309
8310 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
8311 }
8312 else
8313 free_bp_location (old_loc);
8314 }
8315 }
8316
8317 /* Rescan breakpoints at the same address and section,
8318 marking the first one as "first" and any others as "duplicates".
8319 This is so that the bpt instruction is only inserted once.
8320 If we have a permanent breakpoint at the same place as BPT, make
8321 that one the official one, and the rest as duplicates. Permanent
8322 breakpoints are sorted first for the same address. */
8323
8324 loc_first = NULL;
8325 ALL_BP_LOCATIONS (loc, locp)
8326 {
8327 struct breakpoint *b = loc->owner;
8328
8329 if (b->enable_state == bp_disabled
8330 || b->enable_state == bp_call_disabled
8331 || b->enable_state == bp_startup_disabled
8332 || !loc->enabled
8333 || loc->shlib_disabled
8334 || !breakpoint_address_is_meaningful (b))
8335 continue;
8336
8337 /* Permanent breakpoint should always be inserted. */
8338 if (b->enable_state == bp_permanent && ! loc->inserted)
8339 internal_error (__FILE__, __LINE__,
8340 _("allegedly permanent breakpoint is not "
8341 "actually inserted"));
8342
8343 if (loc_first == NULL
8344 || (overlay_debugging && loc->section != loc_first->section)
8345 || !breakpoint_address_match (loc->pspace->aspace, loc->address,
8346 loc_first->pspace->aspace,
8347 loc_first->address))
8348 {
8349 loc_first = loc;
8350 loc->duplicate = 0;
8351 continue;
8352 }
8353
8354 loc->duplicate = 1;
8355
8356 if (loc_first->owner->enable_state == bp_permanent && loc->inserted
8357 && b->enable_state != bp_permanent)
8358 internal_error (__FILE__, __LINE__,
8359 _("another breakpoint was inserted on top of "
8360 "a permanent breakpoint"));
8361 }
8362
8363 if (breakpoints_always_inserted_mode () && should_insert
8364 && (have_live_inferiors ()
8365 || (gdbarch_has_global_breakpoints (target_gdbarch))))
8366 insert_breakpoint_locations ();
8367
8368 do_cleanups (cleanups);
8369 }
8370
8371 void
8372 breakpoint_retire_moribund (void)
8373 {
8374 struct bp_location *loc;
8375 int ix;
8376
8377 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
8378 if (--(loc->events_till_retirement) == 0)
8379 {
8380 free_bp_location (loc);
8381 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
8382 --ix;
8383 }
8384 }
8385
8386 static void
8387 update_global_location_list_nothrow (int inserting)
8388 {
8389 struct gdb_exception e;
8390 TRY_CATCH (e, RETURN_MASK_ERROR)
8391 update_global_location_list (inserting);
8392 }
8393
8394 /* Clear BPT from a BPS. */
8395 static void
8396 bpstat_remove_breakpoint (bpstat bps, struct breakpoint *bpt)
8397 {
8398 bpstat bs;
8399 for (bs = bps; bs; bs = bs->next)
8400 if (bs->breakpoint_at && bs->breakpoint_at->owner == bpt)
8401 {
8402 bs->breakpoint_at = NULL;
8403 bs->old_val = NULL;
8404 /* bs->commands will be freed later. */
8405 }
8406 }
8407
8408 /* Callback for iterate_over_threads. */
8409 static int
8410 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
8411 {
8412 struct breakpoint *bpt = data;
8413 bpstat_remove_breakpoint (th->stop_bpstat, bpt);
8414 return 0;
8415 }
8416
8417 /* Delete a breakpoint and clean up all traces of it in the data
8418 structures. */
8419
8420 void
8421 delete_breakpoint (struct breakpoint *bpt)
8422 {
8423 struct breakpoint *b;
8424 struct bp_location *loc, *next;
8425
8426 gdb_assert (bpt != NULL);
8427
8428 /* Has this bp already been deleted? This can happen because multiple
8429 lists can hold pointers to bp's. bpstat lists are especial culprits.
8430
8431 One example of this happening is a watchpoint's scope bp. When the
8432 scope bp triggers, we notice that the watchpoint is out of scope, and
8433 delete it. We also delete its scope bp. But the scope bp is marked
8434 "auto-deleting", and is already on a bpstat. That bpstat is then
8435 checked for auto-deleting bp's, which are deleted.
8436
8437 A real solution to this problem might involve reference counts in bp's,
8438 and/or giving them pointers back to their referencing bpstat's, and
8439 teaching delete_breakpoint to only free a bp's storage when no more
8440 references were extent. A cheaper bandaid was chosen. */
8441 if (bpt->type == bp_none)
8442 return;
8443
8444 observer_notify_breakpoint_deleted (bpt->number);
8445
8446 if (breakpoint_chain == bpt)
8447 breakpoint_chain = bpt->next;
8448
8449 ALL_BREAKPOINTS (b)
8450 if (b->next == bpt)
8451 {
8452 b->next = bpt->next;
8453 break;
8454 }
8455
8456 free_command_lines (&bpt->commands);
8457 if (bpt->cond_string != NULL)
8458 xfree (bpt->cond_string);
8459 if (bpt->addr_string != NULL)
8460 xfree (bpt->addr_string);
8461 if (bpt->exp != NULL)
8462 xfree (bpt->exp);
8463 if (bpt->exp_string != NULL)
8464 xfree (bpt->exp_string);
8465 if (bpt->val != NULL)
8466 value_free (bpt->val);
8467 if (bpt->source_file != NULL)
8468 xfree (bpt->source_file);
8469 if (bpt->exec_pathname != NULL)
8470 xfree (bpt->exec_pathname);
8471 clean_up_filters (&bpt->syscalls_to_be_caught);
8472
8473 /* Be sure no bpstat's are pointing at it after it's been freed. */
8474 /* FIXME, how can we find all bpstat's?
8475 We just check stop_bpstat for now. Note that we cannot just
8476 remove bpstats pointing at bpt from the stop_bpstat list
8477 entirely, as breakpoint commands are associated with the bpstat;
8478 if we remove it here, then the later call to
8479 bpstat_do_actions (&stop_bpstat);
8480 in event-top.c won't do anything, and temporary breakpoints
8481 with commands won't work. */
8482
8483 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
8484
8485 /* Now that breakpoint is removed from breakpoint
8486 list, update the global location list. This
8487 will remove locations that used to belong to
8488 this breakpoint. Do this before freeing
8489 the breakpoint itself, since remove_breakpoint
8490 looks at location's owner. It might be better
8491 design to have location completely self-contained,
8492 but it's not the case now. */
8493 update_global_location_list (0);
8494
8495
8496 /* On the chance that someone will soon try again to delete this same
8497 bp, we mark it as deleted before freeing its storage. */
8498 bpt->type = bp_none;
8499
8500 xfree (bpt);
8501 }
8502
8503 static void
8504 do_delete_breakpoint_cleanup (void *b)
8505 {
8506 delete_breakpoint (b);
8507 }
8508
8509 struct cleanup *
8510 make_cleanup_delete_breakpoint (struct breakpoint *b)
8511 {
8512 return make_cleanup (do_delete_breakpoint_cleanup, b);
8513 }
8514
8515 void
8516 delete_command (char *arg, int from_tty)
8517 {
8518 struct breakpoint *b, *temp;
8519
8520 dont_repeat ();
8521
8522 if (arg == 0)
8523 {
8524 int breaks_to_delete = 0;
8525
8526 /* Delete all breakpoints if no argument.
8527 Do not delete internal or call-dummy breakpoints, these
8528 have to be deleted with an explicit breakpoint number argument. */
8529 ALL_BREAKPOINTS (b)
8530 {
8531 if (b->type != bp_call_dummy
8532 && b->type != bp_shlib_event
8533 && b->type != bp_jit_event
8534 && b->type != bp_thread_event
8535 && b->type != bp_overlay_event
8536 && b->type != bp_longjmp_master
8537 && b->number >= 0)
8538 {
8539 breaks_to_delete = 1;
8540 break;
8541 }
8542 }
8543
8544 /* Ask user only if there are some breakpoints to delete. */
8545 if (!from_tty
8546 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
8547 {
8548 ALL_BREAKPOINTS_SAFE (b, temp)
8549 {
8550 if (b->type != bp_call_dummy
8551 && b->type != bp_shlib_event
8552 && b->type != bp_thread_event
8553 && b->type != bp_jit_event
8554 && b->type != bp_overlay_event
8555 && b->type != bp_longjmp_master
8556 && b->number >= 0)
8557 delete_breakpoint (b);
8558 }
8559 }
8560 }
8561 else
8562 map_breakpoint_numbers (arg, delete_breakpoint);
8563 }
8564
8565 static int
8566 all_locations_are_pending (struct bp_location *loc)
8567 {
8568 for (; loc; loc = loc->next)
8569 if (!loc->shlib_disabled)
8570 return 0;
8571 return 1;
8572 }
8573
8574 /* Subroutine of update_breakpoint_locations to simplify it.
8575 Return non-zero if multiple fns in list LOC have the same name.
8576 Null names are ignored. */
8577
8578 static int
8579 ambiguous_names_p (struct bp_location *loc)
8580 {
8581 struct bp_location *l;
8582 htab_t htab = htab_create_alloc (13, htab_hash_string,
8583 (int (*) (const void *, const void *)) streq,
8584 NULL, xcalloc, xfree);
8585
8586 for (l = loc; l != NULL; l = l->next)
8587 {
8588 const char **slot;
8589 const char *name = l->function_name;
8590
8591 /* Allow for some names to be NULL, ignore them. */
8592 if (name == NULL)
8593 continue;
8594
8595 slot = (const char **) htab_find_slot (htab, (const void *) name,
8596 INSERT);
8597 /* NOTE: We can assume slot != NULL here because xcalloc never returns
8598 NULL. */
8599 if (*slot != NULL)
8600 {
8601 htab_delete (htab);
8602 return 1;
8603 }
8604 *slot = name;
8605 }
8606
8607 htab_delete (htab);
8608 return 0;
8609 }
8610
8611 static void
8612 update_breakpoint_locations (struct breakpoint *b,
8613 struct symtabs_and_lines sals)
8614 {
8615 int i;
8616 char *s;
8617 struct bp_location *existing_locations = b->loc;
8618
8619 /* If there's no new locations, and all existing locations
8620 are pending, don't do anything. This optimizes
8621 the common case where all locations are in the same
8622 shared library, that was unloaded. We'd like to
8623 retain the location, so that when the library
8624 is loaded again, we don't loose the enabled/disabled
8625 status of the individual locations. */
8626 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
8627 return;
8628
8629 b->loc = NULL;
8630
8631 for (i = 0; i < sals.nelts; ++i)
8632 {
8633 struct bp_location *new_loc =
8634 add_location_to_breakpoint (b, &(sals.sals[i]));
8635
8636 /* Reparse conditions, they might contain references to the
8637 old symtab. */
8638 if (b->cond_string != NULL)
8639 {
8640 struct gdb_exception e;
8641
8642 s = b->cond_string;
8643 TRY_CATCH (e, RETURN_MASK_ERROR)
8644 {
8645 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
8646 0);
8647 }
8648 if (e.reason < 0)
8649 {
8650 warning (_("failed to reevaluate condition for breakpoint %d: %s"),
8651 b->number, e.message);
8652 new_loc->enabled = 0;
8653 }
8654 }
8655
8656 if (b->source_file != NULL)
8657 xfree (b->source_file);
8658 if (sals.sals[i].symtab == NULL)
8659 b->source_file = NULL;
8660 else
8661 b->source_file = xstrdup (sals.sals[i].symtab->filename);
8662
8663 if (b->line_number == 0)
8664 b->line_number = sals.sals[i].line;
8665 }
8666
8667 /* Update locations of permanent breakpoints. */
8668 if (b->enable_state == bp_permanent)
8669 make_breakpoint_permanent (b);
8670
8671 /* If possible, carry over 'disable' status from existing breakpoints. */
8672 {
8673 struct bp_location *e = existing_locations;
8674 /* If there are multiple breakpoints with the same function name,
8675 e.g. for inline functions, comparing function names won't work.
8676 Instead compare pc addresses; this is just a heuristic as things
8677 may have moved, but in practice it gives the correct answer
8678 often enough until a better solution is found. */
8679 int have_ambiguous_names = ambiguous_names_p (b->loc);
8680
8681 for (; e; e = e->next)
8682 {
8683 if (!e->enabled && e->function_name)
8684 {
8685 struct bp_location *l = b->loc;
8686 if (have_ambiguous_names)
8687 {
8688 for (; l; l = l->next)
8689 if (breakpoint_address_match (e->pspace->aspace, e->address,
8690 l->pspace->aspace, l->address))
8691 {
8692 l->enabled = 0;
8693 break;
8694 }
8695 }
8696 else
8697 {
8698 for (; l; l = l->next)
8699 if (l->function_name
8700 && strcmp (e->function_name, l->function_name) == 0)
8701 {
8702 l->enabled = 0;
8703 break;
8704 }
8705 }
8706 }
8707 }
8708 }
8709
8710 update_global_location_list (1);
8711 }
8712
8713
8714 /* Reset a breakpoint given it's struct breakpoint * BINT.
8715 The value we return ends up being the return value from catch_errors.
8716 Unused in this case. */
8717
8718 static int
8719 breakpoint_re_set_one (void *bint)
8720 {
8721 /* get past catch_errs */
8722 struct breakpoint *b = (struct breakpoint *) bint;
8723 struct value *mark;
8724 int i;
8725 int not_found = 0;
8726 int *not_found_ptr = &not_found;
8727 struct symtabs_and_lines sals = {0};
8728 struct symtabs_and_lines expanded = {0};
8729 char *s;
8730 enum enable_state save_enable;
8731 struct gdb_exception e;
8732 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
8733
8734 switch (b->type)
8735 {
8736 case bp_none:
8737 warning (_("attempted to reset apparently deleted breakpoint #%d?"),
8738 b->number);
8739 return 0;
8740 case bp_breakpoint:
8741 case bp_hardware_breakpoint:
8742 case bp_tracepoint:
8743 /* Do not attempt to re-set breakpoints disabled during startup. */
8744 if (b->enable_state == bp_startup_disabled)
8745 return 0;
8746
8747 if (b->addr_string == NULL)
8748 {
8749 /* Anything without a string can't be re-set. */
8750 delete_breakpoint (b);
8751 return 0;
8752 }
8753
8754 set_language (b->language);
8755 input_radix = b->input_radix;
8756 s = b->addr_string;
8757
8758 save_current_space_and_thread ();
8759 switch_to_program_space_and_thread (b->pspace);
8760
8761 TRY_CATCH (e, RETURN_MASK_ERROR)
8762 {
8763 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, (char ***) NULL,
8764 not_found_ptr);
8765 }
8766 if (e.reason < 0)
8767 {
8768 int not_found_and_ok = 0;
8769 /* For pending breakpoints, it's expected that parsing
8770 will fail until the right shared library is loaded.
8771 User has already told to create pending breakpoints and
8772 don't need extra messages. If breakpoint is in bp_shlib_disabled
8773 state, then user already saw the message about that breakpoint
8774 being disabled, and don't want to see more errors. */
8775 if (not_found
8776 && (b->condition_not_parsed
8777 || (b->loc && b->loc->shlib_disabled)
8778 || b->enable_state == bp_disabled))
8779 not_found_and_ok = 1;
8780
8781 if (!not_found_and_ok)
8782 {
8783 /* We surely don't want to warn about the same breakpoint
8784 10 times. One solution, implemented here, is disable
8785 the breakpoint on error. Another solution would be to
8786 have separate 'warning emitted' flag. Since this
8787 happens only when a binary has changed, I don't know
8788 which approach is better. */
8789 b->enable_state = bp_disabled;
8790 throw_exception (e);
8791 }
8792 }
8793
8794 if (!not_found)
8795 {
8796 gdb_assert (sals.nelts == 1);
8797
8798 resolve_sal_pc (&sals.sals[0]);
8799 if (b->condition_not_parsed && s && s[0])
8800 {
8801 char *cond_string = 0;
8802 int thread = -1;
8803 int task = 0;
8804
8805 find_condition_and_thread (s, sals.sals[0].pc,
8806 &cond_string, &thread, &task);
8807 if (cond_string)
8808 b->cond_string = cond_string;
8809 b->thread = thread;
8810 b->task = task;
8811 b->condition_not_parsed = 0;
8812 }
8813
8814 expanded = expand_line_sal_maybe (sals.sals[0]);
8815 }
8816
8817 make_cleanup (xfree, sals.sals);
8818 update_breakpoint_locations (b, expanded);
8819 break;
8820
8821 case bp_watchpoint:
8822 case bp_hardware_watchpoint:
8823 case bp_read_watchpoint:
8824 case bp_access_watchpoint:
8825 /* Watchpoint can be either on expression using entirely global variables,
8826 or it can be on local variables.
8827
8828 Watchpoints of the first kind are never auto-deleted, and even persist
8829 across program restarts. Since they can use variables from shared
8830 libraries, we need to reparse expression as libraries are loaded
8831 and unloaded.
8832
8833 Watchpoints on local variables can also change meaning as result
8834 of solib event. For example, if a watchpoint uses both a local and
8835 a global variables in expression, it's a local watchpoint, but
8836 unloading of a shared library will make the expression invalid.
8837 This is not a very common use case, but we still re-evaluate
8838 expression, to avoid surprises to the user.
8839
8840 Note that for local watchpoints, we re-evaluate it only if
8841 watchpoints frame id is still valid. If it's not, it means
8842 the watchpoint is out of scope and will be deleted soon. In fact,
8843 I'm not sure we'll ever be called in this case.
8844
8845 If a local watchpoint's frame id is still valid, then
8846 b->exp_valid_block is likewise valid, and we can safely use it.
8847
8848 Don't do anything about disabled watchpoints, since they will
8849 be reevaluated again when enabled. */
8850 update_watchpoint (b, 1 /* reparse */);
8851 break;
8852 /* We needn't really do anything to reset these, since the mask
8853 that requests them is unaffected by e.g., new libraries being
8854 loaded. */
8855 case bp_catchpoint:
8856 break;
8857
8858 default:
8859 printf_filtered (_("Deleting unknown breakpoint type %d\n"), b->type);
8860 /* fall through */
8861 /* Delete overlay event and longjmp master breakpoints; they will be
8862 reset later by breakpoint_re_set. */
8863 case bp_overlay_event:
8864 case bp_longjmp_master:
8865 delete_breakpoint (b);
8866 break;
8867
8868 /* This breakpoint is special, it's set up when the inferior
8869 starts and we really don't want to touch it. */
8870 case bp_shlib_event:
8871
8872 /* Like bp_shlib_event, this breakpoint type is special.
8873 Once it is set up, we do not want to touch it. */
8874 case bp_thread_event:
8875
8876 /* Keep temporary breakpoints, which can be encountered when we step
8877 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
8878 Otherwise these should have been blown away via the cleanup chain
8879 or by breakpoint_init_inferior when we rerun the executable. */
8880 case bp_until:
8881 case bp_finish:
8882 case bp_watchpoint_scope:
8883 case bp_call_dummy:
8884 case bp_step_resume:
8885 case bp_longjmp:
8886 case bp_longjmp_resume:
8887 case bp_jit_event:
8888 break;
8889 }
8890
8891 do_cleanups (cleanups);
8892 return 0;
8893 }
8894
8895 /* Re-set all breakpoints after symbols have been re-loaded. */
8896 void
8897 breakpoint_re_set (void)
8898 {
8899 struct breakpoint *b, *temp;
8900 enum language save_language;
8901 int save_input_radix;
8902 struct cleanup *old_chain;
8903
8904 save_language = current_language->la_language;
8905 save_input_radix = input_radix;
8906 old_chain = save_current_program_space ();
8907
8908 ALL_BREAKPOINTS_SAFE (b, temp)
8909 {
8910 /* Format possible error msg */
8911 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
8912 b->number);
8913 struct cleanup *cleanups = make_cleanup (xfree, message);
8914 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
8915 do_cleanups (cleanups);
8916 }
8917 set_language (save_language);
8918 input_radix = save_input_radix;
8919
8920 jit_breakpoint_re_set ();
8921
8922 do_cleanups (old_chain);
8923
8924 create_overlay_event_breakpoint ("_ovly_debug_event");
8925 create_longjmp_master_breakpoint ("longjmp");
8926 create_longjmp_master_breakpoint ("_longjmp");
8927 create_longjmp_master_breakpoint ("siglongjmp");
8928 create_longjmp_master_breakpoint ("_siglongjmp");
8929 }
8930 \f
8931 /* Reset the thread number of this breakpoint:
8932
8933 - If the breakpoint is for all threads, leave it as-is.
8934 - Else, reset it to the current thread for inferior_ptid. */
8935 void
8936 breakpoint_re_set_thread (struct breakpoint *b)
8937 {
8938 if (b->thread != -1)
8939 {
8940 if (in_thread_list (inferior_ptid))
8941 b->thread = pid_to_thread_id (inferior_ptid);
8942
8943 /* We're being called after following a fork. The new fork is
8944 selected as current, and unless this was a vfork will have a
8945 different program space from the original thread. Reset that
8946 as well. */
8947 b->loc->pspace = current_program_space;
8948 }
8949 }
8950
8951 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
8952 If from_tty is nonzero, it prints a message to that effect,
8953 which ends with a period (no newline). */
8954
8955 void
8956 set_ignore_count (int bptnum, int count, int from_tty)
8957 {
8958 struct breakpoint *b;
8959
8960 if (count < 0)
8961 count = 0;
8962
8963 ALL_BREAKPOINTS (b)
8964 if (b->number == bptnum)
8965 {
8966 b->ignore_count = count;
8967 if (from_tty)
8968 {
8969 if (count == 0)
8970 printf_filtered (_("Will stop next time breakpoint %d is reached."),
8971 bptnum);
8972 else if (count == 1)
8973 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
8974 bptnum);
8975 else
8976 printf_filtered (_("Will ignore next %d crossings of breakpoint %d."),
8977 count, bptnum);
8978 }
8979 breakpoints_changed ();
8980 observer_notify_breakpoint_modified (b->number);
8981 return;
8982 }
8983
8984 error (_("No breakpoint number %d."), bptnum);
8985 }
8986
8987 void
8988 make_breakpoint_silent (struct breakpoint *b)
8989 {
8990 /* Silence the breakpoint. */
8991 b->silent = 1;
8992 }
8993
8994 /* Command to set ignore-count of breakpoint N to COUNT. */
8995
8996 static void
8997 ignore_command (char *args, int from_tty)
8998 {
8999 char *p = args;
9000 int num;
9001
9002 if (p == 0)
9003 error_no_arg (_("a breakpoint number"));
9004
9005 num = get_number (&p);
9006 if (num == 0)
9007 error (_("bad breakpoint number: '%s'"), args);
9008 if (*p == 0)
9009 error (_("Second argument (specified ignore-count) is missing."));
9010
9011 set_ignore_count (num,
9012 longest_to_int (value_as_long (parse_and_eval (p))),
9013 from_tty);
9014 if (from_tty)
9015 printf_filtered ("\n");
9016 }
9017 \f
9018 /* Call FUNCTION on each of the breakpoints
9019 whose numbers are given in ARGS. */
9020
9021 static void
9022 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *))
9023 {
9024 char *p = args;
9025 char *p1;
9026 int num;
9027 struct breakpoint *b, *tmp;
9028 int match;
9029
9030 if (p == 0)
9031 error_no_arg (_("one or more breakpoint numbers"));
9032
9033 while (*p)
9034 {
9035 match = 0;
9036 p1 = p;
9037
9038 num = get_number_or_range (&p1);
9039 if (num == 0)
9040 {
9041 warning (_("bad breakpoint number at or near '%s'"), p);
9042 }
9043 else
9044 {
9045 ALL_BREAKPOINTS_SAFE (b, tmp)
9046 if (b->number == num)
9047 {
9048 struct breakpoint *related_breakpoint = b->related_breakpoint;
9049 match = 1;
9050 function (b);
9051 if (related_breakpoint)
9052 function (related_breakpoint);
9053 break;
9054 }
9055 if (match == 0)
9056 printf_unfiltered (_("No breakpoint number %d.\n"), num);
9057 }
9058 p = p1;
9059 }
9060 }
9061
9062 static struct bp_location *
9063 find_location_by_number (char *number)
9064 {
9065 char *dot = strchr (number, '.');
9066 char *p1;
9067 int bp_num;
9068 int loc_num;
9069 struct breakpoint *b;
9070 struct bp_location *loc;
9071
9072 *dot = '\0';
9073
9074 p1 = number;
9075 bp_num = get_number_or_range (&p1);
9076 if (bp_num == 0)
9077 error (_("Bad breakpoint number '%s'"), number);
9078
9079 ALL_BREAKPOINTS (b)
9080 if (b->number == bp_num)
9081 {
9082 break;
9083 }
9084
9085 if (!b || b->number != bp_num)
9086 error (_("Bad breakpoint number '%s'"), number);
9087
9088 p1 = dot+1;
9089 loc_num = get_number_or_range (&p1);
9090 if (loc_num == 0)
9091 error (_("Bad breakpoint location number '%s'"), number);
9092
9093 --loc_num;
9094 loc = b->loc;
9095 for (;loc_num && loc; --loc_num, loc = loc->next)
9096 ;
9097 if (!loc)
9098 error (_("Bad breakpoint location number '%s'"), dot+1);
9099
9100 return loc;
9101 }
9102
9103
9104 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
9105 If from_tty is nonzero, it prints a message to that effect,
9106 which ends with a period (no newline). */
9107
9108 void
9109 disable_breakpoint (struct breakpoint *bpt)
9110 {
9111 /* Never disable a watchpoint scope breakpoint; we want to
9112 hit them when we leave scope so we can delete both the
9113 watchpoint and its scope breakpoint at that time. */
9114 if (bpt->type == bp_watchpoint_scope)
9115 return;
9116
9117 /* You can't disable permanent breakpoints. */
9118 if (bpt->enable_state == bp_permanent)
9119 return;
9120
9121 bpt->enable_state = bp_disabled;
9122
9123 update_global_location_list (0);
9124
9125 observer_notify_breakpoint_modified (bpt->number);
9126 }
9127
9128 static void
9129 disable_command (char *args, int from_tty)
9130 {
9131 struct breakpoint *bpt;
9132 if (args == 0)
9133 ALL_BREAKPOINTS (bpt)
9134 switch (bpt->type)
9135 {
9136 case bp_none:
9137 warning (_("attempted to disable apparently deleted breakpoint #%d?"),
9138 bpt->number);
9139 continue;
9140 case bp_breakpoint:
9141 case bp_tracepoint:
9142 case bp_catchpoint:
9143 case bp_hardware_breakpoint:
9144 case bp_watchpoint:
9145 case bp_hardware_watchpoint:
9146 case bp_read_watchpoint:
9147 case bp_access_watchpoint:
9148 disable_breakpoint (bpt);
9149 default:
9150 continue;
9151 }
9152 else if (strchr (args, '.'))
9153 {
9154 struct bp_location *loc = find_location_by_number (args);
9155 if (loc)
9156 loc->enabled = 0;
9157 update_global_location_list (0);
9158 }
9159 else
9160 map_breakpoint_numbers (args, disable_breakpoint);
9161 }
9162
9163 static void
9164 do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
9165 {
9166 int target_resources_ok, other_type_used;
9167 struct value *mark;
9168
9169 if (bpt->type == bp_hardware_breakpoint)
9170 {
9171 int i;
9172 i = hw_breakpoint_used_count ();
9173 target_resources_ok =
9174 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9175 i + 1, 0);
9176 if (target_resources_ok == 0)
9177 error (_("No hardware breakpoint support in the target."));
9178 else if (target_resources_ok < 0)
9179 error (_("Hardware breakpoints used exceeds limit."));
9180 }
9181
9182 if (bpt->type == bp_watchpoint
9183 || bpt->type == bp_hardware_watchpoint
9184 || bpt->type == bp_read_watchpoint
9185 || bpt->type == bp_access_watchpoint)
9186 {
9187 struct gdb_exception e;
9188
9189 TRY_CATCH (e, RETURN_MASK_ALL)
9190 {
9191 update_watchpoint (bpt, 1 /* reparse */);
9192 }
9193 if (e.reason < 0)
9194 {
9195 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
9196 bpt->number);
9197 return;
9198 }
9199 }
9200
9201 if (bpt->enable_state != bp_permanent)
9202 bpt->enable_state = bp_enabled;
9203 bpt->disposition = disposition;
9204 update_global_location_list (1);
9205 breakpoints_changed ();
9206
9207 observer_notify_breakpoint_modified (bpt->number);
9208 }
9209
9210
9211 void
9212 enable_breakpoint (struct breakpoint *bpt)
9213 {
9214 do_enable_breakpoint (bpt, bpt->disposition);
9215 }
9216
9217 /* The enable command enables the specified breakpoints (or all defined
9218 breakpoints) so they once again become (or continue to be) effective
9219 in stopping the inferior. */
9220
9221 static void
9222 enable_command (char *args, int from_tty)
9223 {
9224 struct breakpoint *bpt;
9225 if (args == 0)
9226 ALL_BREAKPOINTS (bpt)
9227 switch (bpt->type)
9228 {
9229 case bp_none:
9230 warning (_("attempted to enable apparently deleted breakpoint #%d?"),
9231 bpt->number);
9232 continue;
9233 case bp_breakpoint:
9234 case bp_tracepoint:
9235 case bp_catchpoint:
9236 case bp_hardware_breakpoint:
9237 case bp_watchpoint:
9238 case bp_hardware_watchpoint:
9239 case bp_read_watchpoint:
9240 case bp_access_watchpoint:
9241 enable_breakpoint (bpt);
9242 default:
9243 continue;
9244 }
9245 else if (strchr (args, '.'))
9246 {
9247 struct bp_location *loc = find_location_by_number (args);
9248 if (loc)
9249 loc->enabled = 1;
9250 update_global_location_list (1);
9251 }
9252 else
9253 map_breakpoint_numbers (args, enable_breakpoint);
9254 }
9255
9256 static void
9257 enable_once_breakpoint (struct breakpoint *bpt)
9258 {
9259 do_enable_breakpoint (bpt, disp_disable);
9260 }
9261
9262 static void
9263 enable_once_command (char *args, int from_tty)
9264 {
9265 map_breakpoint_numbers (args, enable_once_breakpoint);
9266 }
9267
9268 static void
9269 enable_delete_breakpoint (struct breakpoint *bpt)
9270 {
9271 do_enable_breakpoint (bpt, disp_del);
9272 }
9273
9274 static void
9275 enable_delete_command (char *args, int from_tty)
9276 {
9277 map_breakpoint_numbers (args, enable_delete_breakpoint);
9278 }
9279 \f
9280 static void
9281 set_breakpoint_cmd (char *args, int from_tty)
9282 {
9283 }
9284
9285 static void
9286 show_breakpoint_cmd (char *args, int from_tty)
9287 {
9288 }
9289
9290 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
9291
9292 struct symtabs_and_lines
9293 decode_line_spec_1 (char *string, int funfirstline)
9294 {
9295 struct symtabs_and_lines sals;
9296 if (string == 0)
9297 error (_("Empty line specification."));
9298 if (default_breakpoint_valid)
9299 sals = decode_line_1 (&string, funfirstline,
9300 default_breakpoint_symtab,
9301 default_breakpoint_line,
9302 (char ***) NULL, NULL);
9303 else
9304 sals = decode_line_1 (&string, funfirstline,
9305 (struct symtab *) NULL, 0, (char ***) NULL, NULL);
9306 if (*string)
9307 error (_("Junk at end of line specification: %s"), string);
9308 return sals;
9309 }
9310
9311 /* Create and insert a raw software breakpoint at PC. Return an
9312 identifier, which should be used to remove the breakpoint later.
9313 In general, places which call this should be using something on the
9314 breakpoint chain instead; this function should be eliminated
9315 someday. */
9316
9317 void *
9318 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
9319 struct address_space *aspace, CORE_ADDR pc)
9320 {
9321 struct bp_target_info *bp_tgt;
9322
9323 bp_tgt = XZALLOC (struct bp_target_info);
9324
9325 bp_tgt->placed_address_space = aspace;
9326 bp_tgt->placed_address = pc;
9327
9328 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
9329 {
9330 /* Could not insert the breakpoint. */
9331 xfree (bp_tgt);
9332 return NULL;
9333 }
9334
9335 return bp_tgt;
9336 }
9337
9338 /* Remove a breakpoint BP inserted by deprecated_insert_raw_breakpoint. */
9339
9340 int
9341 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
9342 {
9343 struct bp_target_info *bp_tgt = bp;
9344 int ret;
9345
9346 ret = target_remove_breakpoint (gdbarch, bp_tgt);
9347 xfree (bp_tgt);
9348
9349 return ret;
9350 }
9351
9352 /* One (or perhaps two) breakpoints used for software single stepping. */
9353
9354 static void *single_step_breakpoints[2];
9355 static struct gdbarch *single_step_gdbarch[2];
9356
9357 /* Create and insert a breakpoint for software single step. */
9358
9359 void
9360 insert_single_step_breakpoint (struct gdbarch *gdbarch,
9361 struct address_space *aspace, CORE_ADDR next_pc)
9362 {
9363 void **bpt_p;
9364
9365 if (single_step_breakpoints[0] == NULL)
9366 {
9367 bpt_p = &single_step_breakpoints[0];
9368 single_step_gdbarch[0] = gdbarch;
9369 }
9370 else
9371 {
9372 gdb_assert (single_step_breakpoints[1] == NULL);
9373 bpt_p = &single_step_breakpoints[1];
9374 single_step_gdbarch[1] = gdbarch;
9375 }
9376
9377 /* NOTE drow/2006-04-11: A future improvement to this function would be
9378 to only create the breakpoints once, and actually put them on the
9379 breakpoint chain. That would let us use set_raw_breakpoint. We could
9380 adjust the addresses each time they were needed. Doing this requires
9381 corresponding changes elsewhere where single step breakpoints are
9382 handled, however. So, for now, we use this. */
9383
9384 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
9385 if (*bpt_p == NULL)
9386 error (_("Could not insert single-step breakpoint at %s"),
9387 paddress (gdbarch, next_pc));
9388 }
9389
9390 /* Remove and delete any breakpoints used for software single step. */
9391
9392 void
9393 remove_single_step_breakpoints (void)
9394 {
9395 gdb_assert (single_step_breakpoints[0] != NULL);
9396
9397 /* See insert_single_step_breakpoint for more about this deprecated
9398 call. */
9399 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
9400 single_step_breakpoints[0]);
9401 single_step_gdbarch[0] = NULL;
9402 single_step_breakpoints[0] = NULL;
9403
9404 if (single_step_breakpoints[1] != NULL)
9405 {
9406 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
9407 single_step_breakpoints[1]);
9408 single_step_gdbarch[1] = NULL;
9409 single_step_breakpoints[1] = NULL;
9410 }
9411 }
9412
9413 /* Check whether a software single-step breakpoint is inserted at PC. */
9414
9415 static int
9416 single_step_breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
9417 {
9418 int i;
9419
9420 for (i = 0; i < 2; i++)
9421 {
9422 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
9423 if (bp_tgt
9424 && breakpoint_address_match (bp_tgt->placed_address_space,
9425 bp_tgt->placed_address,
9426 aspace, pc))
9427 return 1;
9428 }
9429
9430 return 0;
9431 }
9432
9433 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
9434 non-zero otherwise. */
9435 static int
9436 is_syscall_catchpoint_enabled (struct breakpoint *bp)
9437 {
9438 if (syscall_catchpoint_p (bp)
9439 && bp->enable_state != bp_disabled
9440 && bp->enable_state != bp_call_disabled)
9441 return 1;
9442 else
9443 return 0;
9444 }
9445
9446 int
9447 catch_syscall_enabled (void)
9448 {
9449 struct inferior *inf = current_inferior ();
9450
9451 return inf->total_syscalls_count != 0;
9452 }
9453
9454 int
9455 catching_syscall_number (int syscall_number)
9456 {
9457 struct breakpoint *bp;
9458
9459 ALL_BREAKPOINTS (bp)
9460 if (is_syscall_catchpoint_enabled (bp))
9461 {
9462 if (bp->syscalls_to_be_caught)
9463 {
9464 int i, iter;
9465 for (i = 0;
9466 VEC_iterate (int, bp->syscalls_to_be_caught, i, iter);
9467 i++)
9468 if (syscall_number == iter)
9469 return 1;
9470 }
9471 else
9472 return 1;
9473 }
9474
9475 return 0;
9476 }
9477
9478 /* Complete syscall names. Used by "catch syscall". */
9479 static char **
9480 catch_syscall_completer (struct cmd_list_element *cmd,
9481 char *text, char *word)
9482 {
9483 const char **list = get_syscall_names ();
9484 return (list == NULL) ? NULL : complete_on_enum (list, text, word);
9485 }
9486
9487 /* Tracepoint-specific operations. */
9488
9489 /* Set tracepoint count to NUM. */
9490 static void
9491 set_tracepoint_count (int num)
9492 {
9493 tracepoint_count = num;
9494 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
9495 }
9496
9497 void
9498 trace_command (char *arg, int from_tty)
9499 {
9500 break_command_really (get_current_arch (),
9501 arg,
9502 NULL, 0, 1 /* parse arg */,
9503 0 /* tempflag */, 0 /* hardwareflag */,
9504 1 /* traceflag */,
9505 0 /* Ignore count */,
9506 pending_break_support,
9507 NULL,
9508 from_tty,
9509 1 /* enabled */);
9510 set_tracepoint_count (breakpoint_count);
9511 }
9512
9513 /* Print information on tracepoint number TPNUM_EXP, or all if
9514 omitted. */
9515
9516 static void
9517 tracepoints_info (char *tpnum_exp, int from_tty)
9518 {
9519 struct breakpoint *b;
9520 int tps_to_list = 0;
9521
9522 /* In the no-arguments case, say "No tracepoints" if none found. */
9523 if (tpnum_exp == 0)
9524 {
9525 ALL_TRACEPOINTS (b)
9526 {
9527 if (b->number >= 0)
9528 {
9529 tps_to_list = 1;
9530 break;
9531 }
9532 }
9533 if (!tps_to_list)
9534 {
9535 ui_out_message (uiout, 0, "No tracepoints.\n");
9536 return;
9537 }
9538 }
9539
9540 /* Otherwise be the same as "info break". */
9541 breakpoints_info (tpnum_exp, from_tty);
9542 }
9543
9544 /* The 'enable trace' command enables tracepoints.
9545 Not supported by all targets. */
9546 static void
9547 enable_trace_command (char *args, int from_tty)
9548 {
9549 enable_command (args, from_tty);
9550 }
9551
9552 /* The 'disable trace' command disables tracepoints.
9553 Not supported by all targets. */
9554 static void
9555 disable_trace_command (char *args, int from_tty)
9556 {
9557 disable_command (args, from_tty);
9558 }
9559
9560 /* Remove a tracepoint (or all if no argument) */
9561 static void
9562 delete_trace_command (char *arg, int from_tty)
9563 {
9564 struct breakpoint *b, *temp;
9565
9566 dont_repeat ();
9567
9568 if (arg == 0)
9569 {
9570 int breaks_to_delete = 0;
9571
9572 /* Delete all breakpoints if no argument.
9573 Do not delete internal or call-dummy breakpoints, these
9574 have to be deleted with an explicit breakpoint number argument. */
9575 ALL_TRACEPOINTS (b)
9576 {
9577 if (b->number >= 0)
9578 {
9579 breaks_to_delete = 1;
9580 break;
9581 }
9582 }
9583
9584 /* Ask user only if there are some breakpoints to delete. */
9585 if (!from_tty
9586 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
9587 {
9588 ALL_BREAKPOINTS_SAFE (b, temp)
9589 {
9590 if (b->type == bp_tracepoint
9591 && b->number >= 0)
9592 delete_breakpoint (b);
9593 }
9594 }
9595 }
9596 else
9597 map_breakpoint_numbers (arg, delete_breakpoint);
9598 }
9599
9600 /* Set passcount for tracepoint.
9601
9602 First command argument is passcount, second is tracepoint number.
9603 If tracepoint number omitted, apply to most recently defined.
9604 Also accepts special argument "all". */
9605
9606 static void
9607 trace_pass_command (char *args, int from_tty)
9608 {
9609 struct breakpoint *t1 = (struct breakpoint *) -1, *t2;
9610 unsigned int count;
9611 int all = 0;
9612
9613 if (args == 0 || *args == 0)
9614 error (_("passcount command requires an argument (count + optional TP num)"));
9615
9616 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
9617
9618 while (*args && isspace ((int) *args))
9619 args++;
9620
9621 if (*args && strncasecmp (args, "all", 3) == 0)
9622 {
9623 args += 3; /* Skip special argument "all". */
9624 all = 1;
9625 if (*args)
9626 error (_("Junk at end of arguments."));
9627 }
9628 else
9629 t1 = get_tracepoint_by_number (&args, 1, 1);
9630
9631 do
9632 {
9633 if (t1)
9634 {
9635 ALL_TRACEPOINTS (t2)
9636 if (t1 == (struct breakpoint *) -1 || t1 == t2)
9637 {
9638 t2->pass_count = count;
9639 observer_notify_tracepoint_modified (t2->number);
9640 if (from_tty)
9641 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
9642 t2->number, count);
9643 }
9644 if (! all && *args)
9645 t1 = get_tracepoint_by_number (&args, 1, 0);
9646 }
9647 }
9648 while (*args);
9649 }
9650
9651 struct breakpoint *
9652 get_tracepoint (int num)
9653 {
9654 struct breakpoint *t;
9655
9656 ALL_TRACEPOINTS (t)
9657 if (t->number == num)
9658 return t;
9659
9660 return NULL;
9661 }
9662
9663 /* Utility: parse a tracepoint number and look it up in the list.
9664 If MULTI_P is true, there might be a range of tracepoints in ARG.
9665 if OPTIONAL_P is true, then if the argument is missing, the most
9666 recent tracepoint (tracepoint_count) is returned. */
9667 struct breakpoint *
9668 get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
9669 {
9670 extern int tracepoint_count;
9671 struct breakpoint *t;
9672 int tpnum;
9673 char *instring = arg == NULL ? NULL : *arg;
9674
9675 if (arg == NULL || *arg == NULL || ! **arg)
9676 {
9677 if (optional_p)
9678 tpnum = tracepoint_count;
9679 else
9680 error_no_arg (_("tracepoint number"));
9681 }
9682 else
9683 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
9684
9685 if (tpnum <= 0)
9686 {
9687 if (instring && *instring)
9688 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
9689 instring);
9690 else
9691 printf_filtered (_("Tracepoint argument missing and no previous tracepoint\n"));
9692 return NULL;
9693 }
9694
9695 ALL_TRACEPOINTS (t)
9696 if (t->number == tpnum)
9697 {
9698 return t;
9699 }
9700
9701 /* FIXME: if we are in the middle of a range we don't want to give
9702 a message. The current interface to get_number_or_range doesn't
9703 allow us to discover this. */
9704 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
9705 return NULL;
9706 }
9707
9708 /* save-tracepoints command */
9709 static void
9710 tracepoint_save_command (char *args, int from_tty)
9711 {
9712 struct breakpoint *tp;
9713 int any_tp = 0;
9714 struct action_line *line;
9715 FILE *fp;
9716 char *i1 = " ", *i2 = " ";
9717 char *indent, *actionline, *pathname;
9718 char tmp[40];
9719 struct cleanup *cleanup;
9720
9721 if (args == 0 || *args == 0)
9722 error (_("Argument required (file name in which to save tracepoints)"));
9723
9724 /* See if we have anything to save. */
9725 ALL_TRACEPOINTS (tp)
9726 {
9727 any_tp = 1;
9728 break;
9729 }
9730 if (!any_tp)
9731 {
9732 warning (_("save-tracepoints: no tracepoints to save."));
9733 return;
9734 }
9735
9736 pathname = tilde_expand (args);
9737 cleanup = make_cleanup (xfree, pathname);
9738 fp = fopen (pathname, "w");
9739 if (!fp)
9740 error (_("Unable to open file '%s' for saving tracepoints (%s)"),
9741 args, safe_strerror (errno));
9742 make_cleanup_fclose (fp);
9743
9744 ALL_TRACEPOINTS (tp)
9745 {
9746 if (tp->addr_string)
9747 fprintf (fp, "trace %s\n", tp->addr_string);
9748 else
9749 {
9750 sprintf_vma (tmp, tp->loc->address);
9751 fprintf (fp, "trace *0x%s\n", tmp);
9752 }
9753
9754 if (tp->pass_count)
9755 fprintf (fp, " passcount %d\n", tp->pass_count);
9756
9757 if (tp->actions)
9758 {
9759 fprintf (fp, " actions\n");
9760 indent = i1;
9761 for (line = tp->actions; line; line = line->next)
9762 {
9763 struct cmd_list_element *cmd;
9764
9765 QUIT; /* allow user to bail out with ^C */
9766 actionline = line->action;
9767 while (isspace ((int) *actionline))
9768 actionline++;
9769
9770 fprintf (fp, "%s%s\n", indent, actionline);
9771 if (*actionline != '#') /* skip for comment lines */
9772 {
9773 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
9774 if (cmd == 0)
9775 error (_("Bad action list item: %s"), actionline);
9776 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
9777 indent = i2;
9778 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
9779 indent = i1;
9780 }
9781 }
9782 }
9783 }
9784 do_cleanups (cleanup);
9785 if (from_tty)
9786 printf_filtered (_("Tracepoints saved to file '%s'.\n"), args);
9787 return;
9788 }
9789
9790 /* Create a vector of all tracepoints. */
9791
9792 VEC(breakpoint_p) *
9793 all_tracepoints ()
9794 {
9795 VEC(breakpoint_p) *tp_vec = 0;
9796 struct breakpoint *tp;
9797
9798 ALL_TRACEPOINTS (tp)
9799 {
9800 VEC_safe_push (breakpoint_p, tp_vec, tp);
9801 }
9802
9803 return tp_vec;
9804 }
9805
9806 \f
9807 /* This help string is used for the break, hbreak, tbreak and thbreak commands.
9808 It is defined as a macro to prevent duplication.
9809 COMMAND should be a string constant containing the name of the command. */
9810 #define BREAK_ARGS_HELP(command) \
9811 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
9812 LOCATION may be a line number, function name, or \"*\" and an address.\n\
9813 If a line number is specified, break at start of code for that line.\n\
9814 If a function is specified, break at start of code for that function.\n\
9815 If an address is specified, break at that exact address.\n\
9816 With no LOCATION, uses current execution address of selected stack frame.\n\
9817 This is useful for breaking on return to a stack frame.\n\
9818 \n\
9819 THREADNUM is the number from \"info threads\".\n\
9820 CONDITION is a boolean expression.\n\
9821 \n\
9822 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
9823 \n\
9824 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
9825
9826 /* List of subcommands for "catch". */
9827 static struct cmd_list_element *catch_cmdlist;
9828
9829 /* List of subcommands for "tcatch". */
9830 static struct cmd_list_element *tcatch_cmdlist;
9831
9832 /* Like add_cmd, but add the command to both the "catch" and "tcatch"
9833 lists, and pass some additional user data to the command function. */
9834 static void
9835 add_catch_command (char *name, char *docstring,
9836 void (*sfunc) (char *args, int from_tty,
9837 struct cmd_list_element *command),
9838 char **(*completer) (struct cmd_list_element *cmd,
9839 char *text, char *word),
9840 void *user_data_catch,
9841 void *user_data_tcatch)
9842 {
9843 struct cmd_list_element *command;
9844
9845 command = add_cmd (name, class_breakpoint, NULL, docstring,
9846 &catch_cmdlist);
9847 set_cmd_sfunc (command, sfunc);
9848 set_cmd_context (command, user_data_catch);
9849 set_cmd_completer (command, completer);
9850
9851 command = add_cmd (name, class_breakpoint, NULL, docstring,
9852 &tcatch_cmdlist);
9853 set_cmd_sfunc (command, sfunc);
9854 set_cmd_context (command, user_data_tcatch);
9855 set_cmd_completer (command, completer);
9856 }
9857
9858 static void
9859 clear_syscall_counts (int pid)
9860 {
9861 struct inferior *inf = find_inferior_pid (pid);
9862
9863 inf->total_syscalls_count = 0;
9864 inf->any_syscall_count = 0;
9865 VEC_free (int, inf->syscalls_counts);
9866 }
9867
9868 void
9869 _initialize_breakpoint (void)
9870 {
9871 static struct cmd_list_element *breakpoint_set_cmdlist;
9872 static struct cmd_list_element *breakpoint_show_cmdlist;
9873 struct cmd_list_element *c;
9874
9875 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
9876 observer_attach_inferior_exit (clear_syscall_counts);
9877
9878 breakpoint_chain = 0;
9879 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
9880 before a breakpoint is set. */
9881 breakpoint_count = 0;
9882
9883 tracepoint_count = 0;
9884
9885 add_com ("ignore", class_breakpoint, ignore_command, _("\
9886 Set ignore-count of breakpoint number N to COUNT.\n\
9887 Usage is `ignore N COUNT'."));
9888 if (xdb_commands)
9889 add_com_alias ("bc", "ignore", class_breakpoint, 1);
9890
9891 add_com ("commands", class_breakpoint, commands_command, _("\
9892 Set commands to be executed when a breakpoint is hit.\n\
9893 Give breakpoint number as argument after \"commands\".\n\
9894 With no argument, the targeted breakpoint is the last one set.\n\
9895 The commands themselves follow starting on the next line.\n\
9896 Type a line containing \"end\" to indicate the end of them.\n\
9897 Give \"silent\" as the first line to make the breakpoint silent;\n\
9898 then no output is printed when it is hit, except what the commands print."));
9899
9900 add_com ("condition", class_breakpoint, condition_command, _("\
9901 Specify breakpoint number N to break only if COND is true.\n\
9902 Usage is `condition N COND', where N is an integer and COND is an\n\
9903 expression to be evaluated whenever breakpoint N is reached."));
9904
9905 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
9906 Set a temporary breakpoint.\n\
9907 Like \"break\" except the breakpoint is only temporary,\n\
9908 so it will be deleted when hit. Equivalent to \"break\" followed\n\
9909 by using \"enable delete\" on the breakpoint number.\n\
9910 \n"
9911 BREAK_ARGS_HELP ("tbreak")));
9912 set_cmd_completer (c, location_completer);
9913
9914 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
9915 Set a hardware assisted breakpoint.\n\
9916 Like \"break\" except the breakpoint requires hardware support,\n\
9917 some target hardware may not have this support.\n\
9918 \n"
9919 BREAK_ARGS_HELP ("hbreak")));
9920 set_cmd_completer (c, location_completer);
9921
9922 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
9923 Set a temporary hardware assisted breakpoint.\n\
9924 Like \"hbreak\" except the breakpoint is only temporary,\n\
9925 so it will be deleted when hit.\n\
9926 \n"
9927 BREAK_ARGS_HELP ("thbreak")));
9928 set_cmd_completer (c, location_completer);
9929
9930 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
9931 Enable some breakpoints.\n\
9932 Give breakpoint numbers (separated by spaces) as arguments.\n\
9933 With no subcommand, breakpoints are enabled until you command otherwise.\n\
9934 This is used to cancel the effect of the \"disable\" command.\n\
9935 With a subcommand you can enable temporarily."),
9936 &enablelist, "enable ", 1, &cmdlist);
9937 if (xdb_commands)
9938 add_com ("ab", class_breakpoint, enable_command, _("\
9939 Enable some breakpoints.\n\
9940 Give breakpoint numbers (separated by spaces) as arguments.\n\
9941 With no subcommand, breakpoints are enabled until you command otherwise.\n\
9942 This is used to cancel the effect of the \"disable\" command.\n\
9943 With a subcommand you can enable temporarily."));
9944
9945 add_com_alias ("en", "enable", class_breakpoint, 1);
9946
9947 add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
9948 Enable some breakpoints.\n\
9949 Give breakpoint numbers (separated by spaces) as arguments.\n\
9950 This is used to cancel the effect of the \"disable\" command.\n\
9951 May be abbreviated to simply \"enable\".\n"),
9952 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
9953
9954 add_cmd ("once", no_class, enable_once_command, _("\
9955 Enable breakpoints for one hit. Give breakpoint numbers.\n\
9956 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
9957 &enablebreaklist);
9958
9959 add_cmd ("delete", no_class, enable_delete_command, _("\
9960 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
9961 If a breakpoint is hit while enabled in this fashion, it is deleted."),
9962 &enablebreaklist);
9963
9964 add_cmd ("delete", no_class, enable_delete_command, _("\
9965 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
9966 If a breakpoint is hit while enabled in this fashion, it is deleted."),
9967 &enablelist);
9968
9969 add_cmd ("once", no_class, enable_once_command, _("\
9970 Enable breakpoints for one hit. Give breakpoint numbers.\n\
9971 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
9972 &enablelist);
9973
9974 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
9975 Disable some breakpoints.\n\
9976 Arguments are breakpoint numbers with spaces in between.\n\
9977 To disable all breakpoints, give no argument.\n\
9978 A disabled breakpoint is not forgotten, but has no effect until reenabled."),
9979 &disablelist, "disable ", 1, &cmdlist);
9980 add_com_alias ("dis", "disable", class_breakpoint, 1);
9981 add_com_alias ("disa", "disable", class_breakpoint, 1);
9982 if (xdb_commands)
9983 add_com ("sb", class_breakpoint, disable_command, _("\
9984 Disable some breakpoints.\n\
9985 Arguments are breakpoint numbers with spaces in between.\n\
9986 To disable all breakpoints, give no argument.\n\
9987 A disabled breakpoint is not forgotten, but has no effect until reenabled."));
9988
9989 add_cmd ("breakpoints", class_alias, disable_command, _("\
9990 Disable some breakpoints.\n\
9991 Arguments are breakpoint numbers with spaces in between.\n\
9992 To disable all breakpoints, give no argument.\n\
9993 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
9994 This command may be abbreviated \"disable\"."),
9995 &disablelist);
9996
9997 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
9998 Delete some breakpoints or auto-display expressions.\n\
9999 Arguments are breakpoint numbers with spaces in between.\n\
10000 To delete all breakpoints, give no argument.\n\
10001 \n\
10002 Also a prefix command for deletion of other GDB objects.\n\
10003 The \"unset\" command is also an alias for \"delete\"."),
10004 &deletelist, "delete ", 1, &cmdlist);
10005 add_com_alias ("d", "delete", class_breakpoint, 1);
10006 add_com_alias ("del", "delete", class_breakpoint, 1);
10007 if (xdb_commands)
10008 add_com ("db", class_breakpoint, delete_command, _("\
10009 Delete some breakpoints.\n\
10010 Arguments are breakpoint numbers with spaces in between.\n\
10011 To delete all breakpoints, give no argument.\n"));
10012
10013 add_cmd ("breakpoints", class_alias, delete_command, _("\
10014 Delete some breakpoints or auto-display expressions.\n\
10015 Arguments are breakpoint numbers with spaces in between.\n\
10016 To delete all breakpoints, give no argument.\n\
10017 This command may be abbreviated \"delete\"."),
10018 &deletelist);
10019
10020 add_com ("clear", class_breakpoint, clear_command, _("\
10021 Clear breakpoint at specified line or function.\n\
10022 Argument may be line number, function name, or \"*\" and an address.\n\
10023 If line number is specified, all breakpoints in that line are cleared.\n\
10024 If function is specified, breakpoints at beginning of function are cleared.\n\
10025 If an address is specified, breakpoints at that address are cleared.\n\
10026 \n\
10027 With no argument, clears all breakpoints in the line that the selected frame\n\
10028 is executing in.\n\
10029 \n\
10030 See also the \"delete\" command which clears breakpoints by number."));
10031
10032 c = add_com ("break", class_breakpoint, break_command, _("\
10033 Set breakpoint at specified line or function.\n"
10034 BREAK_ARGS_HELP ("break")));
10035 set_cmd_completer (c, location_completer);
10036
10037 add_com_alias ("b", "break", class_run, 1);
10038 add_com_alias ("br", "break", class_run, 1);
10039 add_com_alias ("bre", "break", class_run, 1);
10040 add_com_alias ("brea", "break", class_run, 1);
10041
10042 if (xdb_commands)
10043 add_com_alias ("ba", "break", class_breakpoint, 1);
10044
10045 if (dbx_commands)
10046 {
10047 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
10048 Break in function/address or break at a line in the current file."),
10049 &stoplist, "stop ", 1, &cmdlist);
10050 add_cmd ("in", class_breakpoint, stopin_command,
10051 _("Break in function or address."), &stoplist);
10052 add_cmd ("at", class_breakpoint, stopat_command,
10053 _("Break at a line in the current file."), &stoplist);
10054 add_com ("status", class_info, breakpoints_info, _("\
10055 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
10056 The \"Type\" column indicates one of:\n\
10057 \tbreakpoint - normal breakpoint\n\
10058 \twatchpoint - watchpoint\n\
10059 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10060 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10061 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
10062 address and file/line number respectively.\n\
10063 \n\
10064 Convenience variable \"$_\" and default examine address for \"x\"\n\
10065 are set to the address of the last breakpoint listed unless the command\n\
10066 is prefixed with \"server \".\n\n\
10067 Convenience variable \"$bpnum\" contains the number of the last\n\
10068 breakpoint set."));
10069 }
10070
10071 add_info ("breakpoints", breakpoints_info, _("\
10072 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
10073 The \"Type\" column indicates one of:\n\
10074 \tbreakpoint - normal breakpoint\n\
10075 \twatchpoint - watchpoint\n\
10076 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10077 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10078 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
10079 address and file/line number respectively.\n\
10080 \n\
10081 Convenience variable \"$_\" and default examine address for \"x\"\n\
10082 are set to the address of the last breakpoint listed unless the command\n\
10083 is prefixed with \"server \".\n\n\
10084 Convenience variable \"$bpnum\" contains the number of the last\n\
10085 breakpoint set."));
10086
10087 if (xdb_commands)
10088 add_com ("lb", class_breakpoint, breakpoints_info, _("\
10089 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
10090 The \"Type\" column indicates one of:\n\
10091 \tbreakpoint - normal breakpoint\n\
10092 \twatchpoint - watchpoint\n\
10093 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10094 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10095 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
10096 address and file/line number respectively.\n\
10097 \n\
10098 Convenience variable \"$_\" and default examine address for \"x\"\n\
10099 are set to the address of the last breakpoint listed unless the command\n\
10100 is prefixed with \"server \".\n\n\
10101 Convenience variable \"$bpnum\" contains the number of the last\n\
10102 breakpoint set."));
10103
10104 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
10105 Status of all breakpoints, or breakpoint number NUMBER.\n\
10106 The \"Type\" column indicates one of:\n\
10107 \tbreakpoint - normal breakpoint\n\
10108 \twatchpoint - watchpoint\n\
10109 \tlongjmp - internal breakpoint used to step through longjmp()\n\
10110 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
10111 \tuntil - internal breakpoint used by the \"until\" command\n\
10112 \tfinish - internal breakpoint used by the \"finish\" command\n\
10113 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
10114 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
10115 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
10116 address and file/line number respectively.\n\
10117 \n\
10118 Convenience variable \"$_\" and default examine address for \"x\"\n\
10119 are set to the address of the last breakpoint listed unless the command\n\
10120 is prefixed with \"server \".\n\n\
10121 Convenience variable \"$bpnum\" contains the number of the last\n\
10122 breakpoint set."),
10123 &maintenanceinfolist);
10124
10125 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
10126 Set catchpoints to catch events."),
10127 &catch_cmdlist, "catch ",
10128 0/*allow-unknown*/, &cmdlist);
10129
10130 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
10131 Set temporary catchpoints to catch events."),
10132 &tcatch_cmdlist, "tcatch ",
10133 0/*allow-unknown*/, &cmdlist);
10134
10135 /* Add catch and tcatch sub-commands. */
10136 add_catch_command ("catch", _("\
10137 Catch an exception, when caught.\n\
10138 With an argument, catch only exceptions with the given name."),
10139 catch_catch_command,
10140 NULL,
10141 CATCH_PERMANENT,
10142 CATCH_TEMPORARY);
10143 add_catch_command ("throw", _("\
10144 Catch an exception, when thrown.\n\
10145 With an argument, catch only exceptions with the given name."),
10146 catch_throw_command,
10147 NULL,
10148 CATCH_PERMANENT,
10149 CATCH_TEMPORARY);
10150 add_catch_command ("fork", _("Catch calls to fork."),
10151 catch_fork_command_1,
10152 NULL,
10153 (void *) (uintptr_t) catch_fork_permanent,
10154 (void *) (uintptr_t) catch_fork_temporary);
10155 add_catch_command ("vfork", _("Catch calls to vfork."),
10156 catch_fork_command_1,
10157 NULL,
10158 (void *) (uintptr_t) catch_vfork_permanent,
10159 (void *) (uintptr_t) catch_vfork_temporary);
10160 add_catch_command ("exec", _("Catch calls to exec."),
10161 catch_exec_command_1,
10162 NULL,
10163 CATCH_PERMANENT,
10164 CATCH_TEMPORARY);
10165 add_catch_command ("syscall", _("\
10166 Catch system calls by their names and/or numbers.\n\
10167 Arguments say which system calls to catch. If no arguments\n\
10168 are given, every system call will be caught.\n\
10169 Arguments, if given, should be one or more system call names\n\
10170 (if your system supports that), or system call numbers."),
10171 catch_syscall_command_1,
10172 catch_syscall_completer,
10173 CATCH_PERMANENT,
10174 CATCH_TEMPORARY);
10175 add_catch_command ("exception", _("\
10176 Catch Ada exceptions, when raised.\n\
10177 With an argument, catch only exceptions with the given name."),
10178 catch_ada_exception_command,
10179 NULL,
10180 CATCH_PERMANENT,
10181 CATCH_TEMPORARY);
10182 add_catch_command ("assert", _("\
10183 Catch failed Ada assertions, when raised.\n\
10184 With an argument, catch only exceptions with the given name."),
10185 catch_assert_command,
10186 NULL,
10187 CATCH_PERMANENT,
10188 CATCH_TEMPORARY);
10189
10190 c = add_com ("watch", class_breakpoint, watch_command, _("\
10191 Set a watchpoint for an expression.\n\
10192 A watchpoint stops execution of your program whenever the value of\n\
10193 an expression changes."));
10194 set_cmd_completer (c, expression_completer);
10195
10196 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
10197 Set a read watchpoint for an expression.\n\
10198 A watchpoint stops execution of your program whenever the value of\n\
10199 an expression is read."));
10200 set_cmd_completer (c, expression_completer);
10201
10202 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
10203 Set a watchpoint for an expression.\n\
10204 A watchpoint stops execution of your program whenever the value of\n\
10205 an expression is either read or written."));
10206 set_cmd_completer (c, expression_completer);
10207
10208 add_info ("watchpoints", breakpoints_info,
10209 _("Synonym for ``info breakpoints''."));
10210
10211
10212 /* XXX: cagney/2005-02-23: This should be a boolean, and should
10213 respond to changes - contrary to the description. */
10214 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
10215 &can_use_hw_watchpoints, _("\
10216 Set debugger's willingness to use watchpoint hardware."), _("\
10217 Show debugger's willingness to use watchpoint hardware."), _("\
10218 If zero, gdb will not use hardware for new watchpoints, even if\n\
10219 such is available. (However, any hardware watchpoints that were\n\
10220 created before setting this to nonzero, will continue to use watchpoint\n\
10221 hardware.)"),
10222 NULL,
10223 show_can_use_hw_watchpoints,
10224 &setlist, &showlist);
10225
10226 can_use_hw_watchpoints = 1;
10227
10228 /* Tracepoint manipulation commands. */
10229
10230 c = add_com ("trace", class_breakpoint, trace_command, _("\
10231 Set a tracepoint at specified line or function.\n\
10232 \n"
10233 BREAK_ARGS_HELP ("trace") "\n\
10234 Do \"help tracepoints\" for info on other tracepoint commands."));
10235 set_cmd_completer (c, location_completer);
10236
10237 add_com_alias ("tp", "trace", class_alias, 0);
10238 add_com_alias ("tr", "trace", class_alias, 1);
10239 add_com_alias ("tra", "trace", class_alias, 1);
10240 add_com_alias ("trac", "trace", class_alias, 1);
10241
10242 add_info ("tracepoints", tracepoints_info, _("\
10243 Status of tracepoints, or tracepoint number NUMBER.\n\
10244 Convenience variable \"$tpnum\" contains the number of the\n\
10245 last tracepoint set."));
10246
10247 add_info_alias ("tp", "tracepoints", 1);
10248
10249 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
10250 Delete specified tracepoints.\n\
10251 Arguments are tracepoint numbers, separated by spaces.\n\
10252 No argument means delete all tracepoints."),
10253 &deletelist);
10254
10255 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
10256 Disable specified tracepoints.\n\
10257 Arguments are tracepoint numbers, separated by spaces.\n\
10258 No argument means disable all tracepoints."),
10259 &disablelist);
10260 deprecate_cmd (c, "disable");
10261
10262 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
10263 Enable specified tracepoints.\n\
10264 Arguments are tracepoint numbers, separated by spaces.\n\
10265 No argument means enable all tracepoints."),
10266 &enablelist);
10267 deprecate_cmd (c, "enable");
10268
10269 add_com ("passcount", class_trace, trace_pass_command, _("\
10270 Set the passcount for a tracepoint.\n\
10271 The trace will end when the tracepoint has been passed 'count' times.\n\
10272 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
10273 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
10274
10275 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command, _("\
10276 Save current tracepoint definitions as a script.\n\
10277 Use the 'source' command in another debug session to restore them."));
10278 set_cmd_completer (c, filename_completer);
10279
10280 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
10281 Breakpoint specific settings\n\
10282 Configure various breakpoint-specific variables such as\n\
10283 pending breakpoint behavior"),
10284 &breakpoint_set_cmdlist, "set breakpoint ",
10285 0/*allow-unknown*/, &setlist);
10286 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
10287 Breakpoint specific settings\n\
10288 Configure various breakpoint-specific variables such as\n\
10289 pending breakpoint behavior"),
10290 &breakpoint_show_cmdlist, "show breakpoint ",
10291 0/*allow-unknown*/, &showlist);
10292
10293 add_setshow_auto_boolean_cmd ("pending", no_class,
10294 &pending_break_support, _("\
10295 Set debugger's behavior regarding pending breakpoints."), _("\
10296 Show debugger's behavior regarding pending breakpoints."), _("\
10297 If on, an unrecognized breakpoint location will cause gdb to create a\n\
10298 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
10299 an error. If auto, an unrecognized breakpoint location results in a\n\
10300 user-query to see if a pending breakpoint should be created."),
10301 NULL,
10302 show_pending_break_support,
10303 &breakpoint_set_cmdlist,
10304 &breakpoint_show_cmdlist);
10305
10306 pending_break_support = AUTO_BOOLEAN_AUTO;
10307
10308 add_setshow_boolean_cmd ("auto-hw", no_class,
10309 &automatic_hardware_breakpoints, _("\
10310 Set automatic usage of hardware breakpoints."), _("\
10311 Show automatic usage of hardware breakpoints."), _("\
10312 If set, the debugger will automatically use hardware breakpoints for\n\
10313 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
10314 a warning will be emitted for such breakpoints."),
10315 NULL,
10316 show_automatic_hardware_breakpoints,
10317 &breakpoint_set_cmdlist,
10318 &breakpoint_show_cmdlist);
10319
10320 add_setshow_enum_cmd ("always-inserted", class_support,
10321 always_inserted_enums, &always_inserted_mode, _("\
10322 Set mode for inserting breakpoints."), _("\
10323 Show mode for inserting breakpoints."), _("\
10324 When this mode is off, breakpoints are inserted in inferior when it is\n\
10325 resumed, and removed when execution stops. When this mode is on,\n\
10326 breakpoints are inserted immediately and removed only when the user\n\
10327 deletes the breakpoint. When this mode is auto (which is the default),\n\
10328 the behaviour depends on the non-stop setting (see help set non-stop).\n\
10329 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
10330 behaves as if always-inserted mode is on; if gdb is controlling the\n\
10331 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
10332 NULL,
10333 &show_always_inserted_mode,
10334 &breakpoint_set_cmdlist,
10335 &breakpoint_show_cmdlist);
10336
10337 automatic_hardware_breakpoints = 1;
10338
10339 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
10340 }
This page took 0.280267 seconds and 4 git commands to generate.