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