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