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