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