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