2011-08-04 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include <ctype.h>
25 #include "hashtab.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb_string.h"
41 #include "demangle.h"
42 #include "filenames.h"
43 #include "annotate.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "source.h"
47 #include "linespec.h"
48 #include "completer.h"
49 #include "gdb.h"
50 #include "ui-out.h"
51 #include "cli/cli-script.h"
52 #include "gdb_assert.h"
53 #include "block.h"
54 #include "solib.h"
55 #include "solist.h"
56 #include "observer.h"
57 #include "exceptions.h"
58 #include "memattr.h"
59 #include "ada-lang.h"
60 #include "top.h"
61 #include "wrapper.h"
62 #include "valprint.h"
63 #include "jit.h"
64 #include "xml-syscall.h"
65 #include "parser-defs.h"
66 #include "cli/cli-utils.h"
67 #include "continuations.h"
68
69 /* readline include files */
70 #include "readline/readline.h"
71 #include "readline/history.h"
72
73 /* readline defines this. */
74 #undef savestring
75
76 #include "mi/mi-common.h"
77 #include "python/python.h"
78
79 /* 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 /* Cleanup function for a syscall filter list. */
9941 static void
9942 clean_up_filters (void *arg)
9943 {
9944 VEC(int) *iter = *(VEC(int) **) arg;
9945 VEC_free (int, iter);
9946 }
9947
9948 /* Splits the argument using space as delimiter. Returns an xmalloc'd
9949 filter list, or NULL if no filtering is required. */
9950 static VEC(int) *
9951 catch_syscall_split_args (char *arg)
9952 {
9953 VEC(int) *result = NULL;
9954 struct cleanup *cleanup = make_cleanup (clean_up_filters, &result);
9955
9956 while (*arg != '\0')
9957 {
9958 int i, syscall_number;
9959 char *endptr;
9960 char cur_name[128];
9961 struct syscall s;
9962
9963 /* Skip whitespace. */
9964 while (isspace (*arg))
9965 arg++;
9966
9967 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
9968 cur_name[i] = arg[i];
9969 cur_name[i] = '\0';
9970 arg += i;
9971
9972 /* Check if the user provided a syscall name or a number. */
9973 syscall_number = (int) strtol (cur_name, &endptr, 0);
9974 if (*endptr == '\0')
9975 get_syscall_by_number (syscall_number, &s);
9976 else
9977 {
9978 /* We have a name. Let's check if it's valid and convert it
9979 to a number. */
9980 get_syscall_by_name (cur_name, &s);
9981
9982 if (s.number == UNKNOWN_SYSCALL)
9983 /* Here we have to issue an error instead of a warning,
9984 because GDB cannot do anything useful if there's no
9985 syscall number to be caught. */
9986 error (_("Unknown syscall name '%s'."), cur_name);
9987 }
9988
9989 /* Ok, it's valid. */
9990 VEC_safe_push (int, result, s.number);
9991 }
9992
9993 discard_cleanups (cleanup);
9994 return result;
9995 }
9996
9997 /* Implement the "catch syscall" command. */
9998
9999 static void
10000 catch_syscall_command_1 (char *arg, int from_tty,
10001 struct cmd_list_element *command)
10002 {
10003 int tempflag;
10004 VEC(int) *filter;
10005 struct syscall s;
10006 struct gdbarch *gdbarch = get_current_arch ();
10007
10008 /* Checking if the feature if supported. */
10009 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
10010 error (_("The feature 'catch syscall' is not supported on \
10011 this architecture yet."));
10012
10013 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
10014
10015 arg = skip_spaces (arg);
10016
10017 /* We need to do this first "dummy" translation in order
10018 to get the syscall XML file loaded or, most important,
10019 to display a warning to the user if there's no XML file
10020 for his/her architecture. */
10021 get_syscall_by_number (0, &s);
10022
10023 /* The allowed syntax is:
10024 catch syscall
10025 catch syscall <name | number> [<name | number> ... <name | number>]
10026
10027 Let's check if there's a syscall name. */
10028
10029 if (arg != NULL)
10030 filter = catch_syscall_split_args (arg);
10031 else
10032 filter = NULL;
10033
10034 create_syscall_event_catchpoint (tempflag, filter,
10035 &catch_syscall_breakpoint_ops);
10036 }
10037
10038 static void
10039 catch_command (char *arg, int from_tty)
10040 {
10041 error (_("Catch requires an event name."));
10042 }
10043 \f
10044
10045 static void
10046 tcatch_command (char *arg, int from_tty)
10047 {
10048 error (_("Catch requires an event name."));
10049 }
10050
10051 /* Delete breakpoints by address or line. */
10052
10053 static void
10054 clear_command (char *arg, int from_tty)
10055 {
10056 struct breakpoint *b;
10057 VEC(breakpoint_p) *found = 0;
10058 int ix;
10059 int default_match;
10060 struct symtabs_and_lines sals;
10061 struct symtab_and_line sal;
10062 int i;
10063
10064 if (arg)
10065 {
10066 sals = decode_line_spec (arg, 1);
10067 default_match = 0;
10068 }
10069 else
10070 {
10071 sals.sals = (struct symtab_and_line *)
10072 xmalloc (sizeof (struct symtab_and_line));
10073 make_cleanup (xfree, sals.sals);
10074 init_sal (&sal); /* Initialize to zeroes. */
10075 sal.line = default_breakpoint_line;
10076 sal.symtab = default_breakpoint_symtab;
10077 sal.pc = default_breakpoint_address;
10078 sal.pspace = default_breakpoint_pspace;
10079 if (sal.symtab == 0)
10080 error (_("No source file specified."));
10081
10082 sals.sals[0] = sal;
10083 sals.nelts = 1;
10084
10085 default_match = 1;
10086 }
10087
10088 /* We don't call resolve_sal_pc here. That's not as bad as it
10089 seems, because all existing breakpoints typically have both
10090 file/line and pc set. So, if clear is given file/line, we can
10091 match this to existing breakpoint without obtaining pc at all.
10092
10093 We only support clearing given the address explicitly
10094 present in breakpoint table. Say, we've set breakpoint
10095 at file:line. There were several PC values for that file:line,
10096 due to optimization, all in one block.
10097
10098 We've picked one PC value. If "clear" is issued with another
10099 PC corresponding to the same file:line, the breakpoint won't
10100 be cleared. We probably can still clear the breakpoint, but
10101 since the other PC value is never presented to user, user
10102 can only find it by guessing, and it does not seem important
10103 to support that. */
10104
10105 /* For each line spec given, delete bps which correspond to it. Do
10106 it in two passes, solely to preserve the current behavior that
10107 from_tty is forced true if we delete more than one
10108 breakpoint. */
10109
10110 found = NULL;
10111 for (i = 0; i < sals.nelts; i++)
10112 {
10113 /* If exact pc given, clear bpts at that pc.
10114 If line given (pc == 0), clear all bpts on specified line.
10115 If defaulting, clear all bpts on default line
10116 or at default pc.
10117
10118 defaulting sal.pc != 0 tests to do
10119
10120 0 1 pc
10121 1 1 pc _and_ line
10122 0 0 line
10123 1 0 <can't happen> */
10124
10125 sal = sals.sals[i];
10126
10127 /* Find all matching breakpoints and add them to 'found'. */
10128 ALL_BREAKPOINTS (b)
10129 {
10130 int match = 0;
10131 /* Are we going to delete b? */
10132 if (b->type != bp_none && !is_watchpoint (b))
10133 {
10134 struct bp_location *loc = b->loc;
10135 for (; loc; loc = loc->next)
10136 {
10137 int pc_match = sal.pc
10138 && (loc->pspace == sal.pspace)
10139 && (loc->address == sal.pc)
10140 && (!section_is_overlay (loc->section)
10141 || loc->section == sal.section);
10142 int line_match = ((default_match || (0 == sal.pc))
10143 && b->source_file != NULL
10144 && sal.symtab != NULL
10145 && sal.pspace == loc->pspace
10146 && filename_cmp (b->source_file,
10147 sal.symtab->filename) == 0
10148 && b->line_number == sal.line);
10149 if (pc_match || line_match)
10150 {
10151 match = 1;
10152 break;
10153 }
10154 }
10155 }
10156
10157 if (match)
10158 VEC_safe_push(breakpoint_p, found, b);
10159 }
10160 }
10161 /* Now go thru the 'found' chain and delete them. */
10162 if (VEC_empty(breakpoint_p, found))
10163 {
10164 if (arg)
10165 error (_("No breakpoint at %s."), arg);
10166 else
10167 error (_("No breakpoint at this line."));
10168 }
10169
10170 if (VEC_length(breakpoint_p, found) > 1)
10171 from_tty = 1; /* Always report if deleted more than one. */
10172 if (from_tty)
10173 {
10174 if (VEC_length(breakpoint_p, found) == 1)
10175 printf_unfiltered (_("Deleted breakpoint "));
10176 else
10177 printf_unfiltered (_("Deleted breakpoints "));
10178 }
10179 breakpoints_changed ();
10180
10181 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
10182 {
10183 if (from_tty)
10184 printf_unfiltered ("%d ", b->number);
10185 delete_breakpoint (b);
10186 }
10187 if (from_tty)
10188 putchar_unfiltered ('\n');
10189 }
10190 \f
10191 /* Delete breakpoint in BS if they are `delete' breakpoints and
10192 all breakpoints that are marked for deletion, whether hit or not.
10193 This is called after any breakpoint is hit, or after errors. */
10194
10195 void
10196 breakpoint_auto_delete (bpstat bs)
10197 {
10198 struct breakpoint *b, *b_tmp;
10199
10200 for (; bs; bs = bs->next)
10201 if (bs->breakpoint_at
10202 && bs->breakpoint_at->disposition == disp_del
10203 && bs->stop)
10204 delete_breakpoint (bs->breakpoint_at);
10205
10206 ALL_BREAKPOINTS_SAFE (b, b_tmp)
10207 {
10208 if (b->disposition == disp_del_at_next_stop)
10209 delete_breakpoint (b);
10210 }
10211 }
10212
10213 /* A comparison function for bp_location AP and BP being interfaced to
10214 qsort. Sort elements primarily by their ADDRESS (no matter what
10215 does breakpoint_address_is_meaningful say for its OWNER),
10216 secondarily by ordering first bp_permanent OWNERed elements and
10217 terciarily just ensuring the array is sorted stable way despite
10218 qsort being an unstable algorithm. */
10219
10220 static int
10221 bp_location_compare (const void *ap, const void *bp)
10222 {
10223 struct bp_location *a = *(void **) ap;
10224 struct bp_location *b = *(void **) bp;
10225 /* A and B come from existing breakpoints having non-NULL OWNER. */
10226 int a_perm = a->owner->enable_state == bp_permanent;
10227 int b_perm = b->owner->enable_state == bp_permanent;
10228
10229 if (a->address != b->address)
10230 return (a->address > b->address) - (a->address < b->address);
10231
10232 /* Sort permanent breakpoints first. */
10233 if (a_perm != b_perm)
10234 return (a_perm < b_perm) - (a_perm > b_perm);
10235
10236 /* Make the user-visible order stable across GDB runs. Locations of
10237 the same breakpoint can be sorted in arbitrary order. */
10238
10239 if (a->owner->number != b->owner->number)
10240 return (a->owner->number > b->owner->number)
10241 - (a->owner->number < b->owner->number);
10242
10243 return (a > b) - (a < b);
10244 }
10245
10246 /* Set bp_location_placed_address_before_address_max and
10247 bp_location_shadow_len_after_address_max according to the current
10248 content of the bp_location array. */
10249
10250 static void
10251 bp_location_target_extensions_update (void)
10252 {
10253 struct bp_location *bl, **blp_tmp;
10254
10255 bp_location_placed_address_before_address_max = 0;
10256 bp_location_shadow_len_after_address_max = 0;
10257
10258 ALL_BP_LOCATIONS (bl, blp_tmp)
10259 {
10260 CORE_ADDR start, end, addr;
10261
10262 if (!bp_location_has_shadow (bl))
10263 continue;
10264
10265 start = bl->target_info.placed_address;
10266 end = start + bl->target_info.shadow_len;
10267
10268 gdb_assert (bl->address >= start);
10269 addr = bl->address - start;
10270 if (addr > bp_location_placed_address_before_address_max)
10271 bp_location_placed_address_before_address_max = addr;
10272
10273 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10274
10275 gdb_assert (bl->address < end);
10276 addr = end - bl->address;
10277 if (addr > bp_location_shadow_len_after_address_max)
10278 bp_location_shadow_len_after_address_max = addr;
10279 }
10280 }
10281
10282 /* Swap the insertion/duplication state between two locations. */
10283
10284 static void
10285 swap_insertion (struct bp_location *left, struct bp_location *right)
10286 {
10287 const int left_inserted = left->inserted;
10288 const int left_duplicate = left->duplicate;
10289 const struct bp_target_info left_target_info = left->target_info;
10290
10291 left->inserted = right->inserted;
10292 left->duplicate = right->duplicate;
10293 left->target_info = right->target_info;
10294 right->inserted = left_inserted;
10295 right->duplicate = left_duplicate;
10296 right->target_info = left_target_info;
10297 }
10298
10299 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
10300 into the inferior, only remove already-inserted locations that no
10301 longer should be inserted. Functions that delete a breakpoint or
10302 breakpoints should pass false, so that deleting a breakpoint
10303 doesn't have the side effect of inserting the locations of other
10304 breakpoints that are marked not-inserted, but should_be_inserted
10305 returns true on them.
10306
10307 This behaviour is useful is situations close to tear-down -- e.g.,
10308 after an exec, while the target still has execution, but breakpoint
10309 shadows of the previous executable image should *NOT* be restored
10310 to the new image; or before detaching, where the target still has
10311 execution and wants to delete breakpoints from GDB's lists, and all
10312 breakpoints had already been removed from the inferior. */
10313
10314 static void
10315 update_global_location_list (int should_insert)
10316 {
10317 struct breakpoint *b;
10318 struct bp_location **locp, *loc;
10319 struct cleanup *cleanups;
10320
10321 /* Used in the duplicates detection below. When iterating over all
10322 bp_locations, points to the first bp_location of a given address.
10323 Breakpoints and watchpoints of different types are never
10324 duplicates of each other. Keep one pointer for each type of
10325 breakpoint/watchpoint, so we only need to loop over all locations
10326 once. */
10327 struct bp_location *bp_loc_first; /* breakpoint */
10328 struct bp_location *wp_loc_first; /* hardware watchpoint */
10329 struct bp_location *awp_loc_first; /* access watchpoint */
10330 struct bp_location *rwp_loc_first; /* read watchpoint */
10331
10332 /* Saved former bp_location array which we compare against the newly
10333 built bp_location from the current state of ALL_BREAKPOINTS. */
10334 struct bp_location **old_location, **old_locp;
10335 unsigned old_location_count;
10336
10337 old_location = bp_location;
10338 old_location_count = bp_location_count;
10339 bp_location = NULL;
10340 bp_location_count = 0;
10341 cleanups = make_cleanup (xfree, old_location);
10342
10343 ALL_BREAKPOINTS (b)
10344 for (loc = b->loc; loc; loc = loc->next)
10345 bp_location_count++;
10346
10347 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
10348 locp = bp_location;
10349 ALL_BREAKPOINTS (b)
10350 for (loc = b->loc; loc; loc = loc->next)
10351 *locp++ = loc;
10352 qsort (bp_location, bp_location_count, sizeof (*bp_location),
10353 bp_location_compare);
10354
10355 bp_location_target_extensions_update ();
10356
10357 /* Identify bp_location instances that are no longer present in the
10358 new list, and therefore should be freed. Note that it's not
10359 necessary that those locations should be removed from inferior --
10360 if there's another location at the same address (previously
10361 marked as duplicate), we don't need to remove/insert the
10362 location.
10363
10364 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10365 and former bp_location array state respectively. */
10366
10367 locp = bp_location;
10368 for (old_locp = old_location; old_locp < old_location + old_location_count;
10369 old_locp++)
10370 {
10371 struct bp_location *old_loc = *old_locp;
10372 struct bp_location **loc2p;
10373
10374 /* Tells if 'old_loc' is found among the new locations. If
10375 not, we have to free it. */
10376 int found_object = 0;
10377 /* Tells if the location should remain inserted in the target. */
10378 int keep_in_target = 0;
10379 int removed = 0;
10380
10381 /* Skip LOCP entries which will definitely never be needed.
10382 Stop either at or being the one matching OLD_LOC. */
10383 while (locp < bp_location + bp_location_count
10384 && (*locp)->address < old_loc->address)
10385 locp++;
10386
10387 for (loc2p = locp;
10388 (loc2p < bp_location + bp_location_count
10389 && (*loc2p)->address == old_loc->address);
10390 loc2p++)
10391 {
10392 if (*loc2p == old_loc)
10393 {
10394 found_object = 1;
10395 break;
10396 }
10397 }
10398
10399 /* If this location is no longer present, and inserted, look if
10400 there's maybe a new location at the same address. If so,
10401 mark that one inserted, and don't remove this one. This is
10402 needed so that we don't have a time window where a breakpoint
10403 at certain location is not inserted. */
10404
10405 if (old_loc->inserted)
10406 {
10407 /* If the location is inserted now, we might have to remove
10408 it. */
10409
10410 if (found_object && should_be_inserted (old_loc))
10411 {
10412 /* The location is still present in the location list,
10413 and still should be inserted. Don't do anything. */
10414 keep_in_target = 1;
10415 }
10416 else
10417 {
10418 /* The location is either no longer present, or got
10419 disabled. See if there's another location at the
10420 same address, in which case we don't need to remove
10421 this one from the target. */
10422
10423 /* OLD_LOC comes from existing struct breakpoint. */
10424 if (breakpoint_address_is_meaningful (old_loc->owner))
10425 {
10426 for (loc2p = locp;
10427 (loc2p < bp_location + bp_location_count
10428 && (*loc2p)->address == old_loc->address);
10429 loc2p++)
10430 {
10431 struct bp_location *loc2 = *loc2p;
10432
10433 if (breakpoint_locations_match (loc2, old_loc))
10434 {
10435 /* Read watchpoint locations are switched to
10436 access watchpoints, if the former are not
10437 supported, but the latter are. */
10438 if (is_hardware_watchpoint (old_loc->owner))
10439 {
10440 gdb_assert (is_hardware_watchpoint (loc2->owner));
10441 loc2->watchpoint_type = old_loc->watchpoint_type;
10442 }
10443
10444 /* loc2 is a duplicated location. We need to check
10445 if it should be inserted in case it will be
10446 unduplicated. */
10447 if (loc2 != old_loc
10448 && unduplicated_should_be_inserted (loc2))
10449 {
10450 swap_insertion (old_loc, loc2);
10451 keep_in_target = 1;
10452 break;
10453 }
10454 }
10455 }
10456 }
10457 }
10458
10459 if (!keep_in_target)
10460 {
10461 if (remove_breakpoint (old_loc, mark_uninserted))
10462 {
10463 /* This is just about all we can do. We could keep
10464 this location on the global list, and try to
10465 remove it next time, but there's no particular
10466 reason why we will succeed next time.
10467
10468 Note that at this point, old_loc->owner is still
10469 valid, as delete_breakpoint frees the breakpoint
10470 only after calling us. */
10471 printf_filtered (_("warning: Error removing "
10472 "breakpoint %d\n"),
10473 old_loc->owner->number);
10474 }
10475 removed = 1;
10476 }
10477 }
10478
10479 if (!found_object)
10480 {
10481 if (removed && non_stop
10482 && breakpoint_address_is_meaningful (old_loc->owner)
10483 && !is_hardware_watchpoint (old_loc->owner))
10484 {
10485 /* This location was removed from the target. In
10486 non-stop mode, a race condition is possible where
10487 we've removed a breakpoint, but stop events for that
10488 breakpoint are already queued and will arrive later.
10489 We apply an heuristic to be able to distinguish such
10490 SIGTRAPs from other random SIGTRAPs: we keep this
10491 breakpoint location for a bit, and will retire it
10492 after we see some number of events. The theory here
10493 is that reporting of events should, "on the average",
10494 be fair, so after a while we'll see events from all
10495 threads that have anything of interest, and no longer
10496 need to keep this breakpoint location around. We
10497 don't hold locations forever so to reduce chances of
10498 mistaking a non-breakpoint SIGTRAP for a breakpoint
10499 SIGTRAP.
10500
10501 The heuristic failing can be disastrous on
10502 decr_pc_after_break targets.
10503
10504 On decr_pc_after_break targets, like e.g., x86-linux,
10505 if we fail to recognize a late breakpoint SIGTRAP,
10506 because events_till_retirement has reached 0 too
10507 soon, we'll fail to do the PC adjustment, and report
10508 a random SIGTRAP to the user. When the user resumes
10509 the inferior, it will most likely immediately crash
10510 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
10511 corrupted, because of being resumed e.g., in the
10512 middle of a multi-byte instruction, or skipped a
10513 one-byte instruction. This was actually seen happen
10514 on native x86-linux, and should be less rare on
10515 targets that do not support new thread events, like
10516 remote, due to the heuristic depending on
10517 thread_count.
10518
10519 Mistaking a random SIGTRAP for a breakpoint trap
10520 causes similar symptoms (PC adjustment applied when
10521 it shouldn't), but then again, playing with SIGTRAPs
10522 behind the debugger's back is asking for trouble.
10523
10524 Since hardware watchpoint traps are always
10525 distinguishable from other traps, so we don't need to
10526 apply keep hardware watchpoint moribund locations
10527 around. We simply always ignore hardware watchpoint
10528 traps we can no longer explain. */
10529
10530 old_loc->events_till_retirement = 3 * (thread_count () + 1);
10531 old_loc->owner = NULL;
10532
10533 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
10534 }
10535 else
10536 {
10537 old_loc->owner = NULL;
10538 decref_bp_location (&old_loc);
10539 }
10540 }
10541 }
10542
10543 /* Rescan breakpoints at the same address and section, marking the
10544 first one as "first" and any others as "duplicates". This is so
10545 that the bpt instruction is only inserted once. If we have a
10546 permanent breakpoint at the same place as BPT, make that one the
10547 official one, and the rest as duplicates. Permanent breakpoints
10548 are sorted first for the same address.
10549
10550 Do the same for hardware watchpoints, but also considering the
10551 watchpoint's type (regular/access/read) and length. */
10552
10553 bp_loc_first = NULL;
10554 wp_loc_first = NULL;
10555 awp_loc_first = NULL;
10556 rwp_loc_first = NULL;
10557 ALL_BP_LOCATIONS (loc, locp)
10558 {
10559 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
10560 non-NULL. */
10561 struct breakpoint *b = loc->owner;
10562 struct bp_location **loc_first_p;
10563
10564 if (b->enable_state == bp_disabled
10565 || b->enable_state == bp_call_disabled
10566 || b->enable_state == bp_startup_disabled
10567 || !loc->enabled
10568 || loc->shlib_disabled
10569 || !breakpoint_address_is_meaningful (b)
10570 || is_tracepoint (b))
10571 continue;
10572
10573 /* Permanent breakpoint should always be inserted. */
10574 if (b->enable_state == bp_permanent && ! loc->inserted)
10575 internal_error (__FILE__, __LINE__,
10576 _("allegedly permanent breakpoint is not "
10577 "actually inserted"));
10578
10579 if (b->type == bp_hardware_watchpoint)
10580 loc_first_p = &wp_loc_first;
10581 else if (b->type == bp_read_watchpoint)
10582 loc_first_p = &rwp_loc_first;
10583 else if (b->type == bp_access_watchpoint)
10584 loc_first_p = &awp_loc_first;
10585 else
10586 loc_first_p = &bp_loc_first;
10587
10588 if (*loc_first_p == NULL
10589 || (overlay_debugging && loc->section != (*loc_first_p)->section)
10590 || !breakpoint_locations_match (loc, *loc_first_p))
10591 {
10592 *loc_first_p = loc;
10593 loc->duplicate = 0;
10594 continue;
10595 }
10596
10597
10598 /* This and the above ensure the invariant that the first location
10599 is not duplicated, and is the inserted one.
10600 All following are marked as duplicated, and are not inserted. */
10601 if (loc->inserted)
10602 swap_insertion (loc, *loc_first_p);
10603 loc->duplicate = 1;
10604
10605 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
10606 && b->enable_state != bp_permanent)
10607 internal_error (__FILE__, __LINE__,
10608 _("another breakpoint was inserted on top of "
10609 "a permanent breakpoint"));
10610 }
10611
10612 if (breakpoints_always_inserted_mode () && should_insert
10613 && (have_live_inferiors ()
10614 || (gdbarch_has_global_breakpoints (target_gdbarch))))
10615 insert_breakpoint_locations ();
10616
10617 do_cleanups (cleanups);
10618 }
10619
10620 void
10621 breakpoint_retire_moribund (void)
10622 {
10623 struct bp_location *loc;
10624 int ix;
10625
10626 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
10627 if (--(loc->events_till_retirement) == 0)
10628 {
10629 decref_bp_location (&loc);
10630 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
10631 --ix;
10632 }
10633 }
10634
10635 static void
10636 update_global_location_list_nothrow (int inserting)
10637 {
10638 struct gdb_exception e;
10639
10640 TRY_CATCH (e, RETURN_MASK_ERROR)
10641 update_global_location_list (inserting);
10642 }
10643
10644 /* Clear BKP from a BPS. */
10645
10646 static void
10647 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
10648 {
10649 bpstat bs;
10650
10651 for (bs = bps; bs; bs = bs->next)
10652 if (bs->breakpoint_at == bpt)
10653 {
10654 bs->breakpoint_at = NULL;
10655 bs->old_val = NULL;
10656 /* bs->commands will be freed later. */
10657 }
10658 }
10659
10660 /* Callback for iterate_over_threads. */
10661 static int
10662 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
10663 {
10664 struct breakpoint *bpt = data;
10665
10666 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
10667 return 0;
10668 }
10669
10670 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
10671 callbacks. */
10672
10673 static void
10674 say_where (struct breakpoint *b)
10675 {
10676 struct ui_out *uiout = current_uiout;
10677 struct value_print_options opts;
10678
10679 get_user_print_options (&opts);
10680
10681 /* i18n: cagney/2005-02-11: Below needs to be merged into a
10682 single string. */
10683 if (b->loc == NULL)
10684 {
10685 printf_filtered (_(" (%s) pending."), b->addr_string);
10686 }
10687 else
10688 {
10689 if (opts.addressprint || b->source_file == NULL)
10690 {
10691 printf_filtered (" at ");
10692 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
10693 gdb_stdout);
10694 }
10695 if (b->source_file)
10696 printf_filtered (": file %s, line %d.",
10697 b->source_file, b->line_number);
10698
10699 if (b->loc->next)
10700 {
10701 struct bp_location *loc = b->loc;
10702 int n = 0;
10703 for (; loc; loc = loc->next)
10704 ++n;
10705 printf_filtered (" (%d locations)", n);
10706 }
10707 }
10708 }
10709
10710 /* Default bp_location_ops methods. */
10711
10712 static void
10713 bp_location_dtor (struct bp_location *self)
10714 {
10715 xfree (self->cond);
10716 xfree (self->function_name);
10717 }
10718
10719 static const struct bp_location_ops bp_location_ops =
10720 {
10721 bp_location_dtor
10722 };
10723
10724 /* Default breakpoint_ops methods all breakpoint_ops ultimately
10725 inherit from. */
10726
10727 static void
10728 base_breakpoint_dtor (struct breakpoint *self)
10729 {
10730 decref_counted_command_line (&self->commands);
10731 xfree (self->cond_string);
10732 xfree (self->addr_string);
10733 xfree (self->addr_string_range_end);
10734 xfree (self->source_file);
10735 }
10736
10737 static struct bp_location *
10738 base_breakpoint_allocate_location (struct breakpoint *self)
10739 {
10740 struct bp_location *loc;
10741
10742 loc = XNEW (struct bp_location);
10743 init_bp_location (loc, &bp_location_ops, self);
10744 return loc;
10745 }
10746
10747 static void
10748 base_breakpoint_re_set (struct breakpoint *b)
10749 {
10750 /* Nothing to re-set. */
10751 }
10752
10753 #define internal_error_pure_virtual_called() \
10754 gdb_assert_not_reached ("pure virtual function called")
10755
10756 static int
10757 base_breakpoint_insert_location (struct bp_location *bl)
10758 {
10759 internal_error_pure_virtual_called ();
10760 }
10761
10762 static int
10763 base_breakpoint_remove_location (struct bp_location *bl)
10764 {
10765 internal_error_pure_virtual_called ();
10766 }
10767
10768 static int
10769 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
10770 struct address_space *aspace,
10771 CORE_ADDR bp_addr)
10772 {
10773 internal_error_pure_virtual_called ();
10774 }
10775
10776 static void
10777 base_breakpoint_check_status (bpstat bs)
10778 {
10779 /* Always stop. */
10780 }
10781
10782 /* A "works_in_software_mode" breakpoint_ops method that just internal
10783 errors. */
10784
10785 static int
10786 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
10787 {
10788 internal_error_pure_virtual_called ();
10789 }
10790
10791 /* A "resources_needed" breakpoint_ops method that just internal
10792 errors. */
10793
10794 static int
10795 base_breakpoint_resources_needed (const struct bp_location *bl)
10796 {
10797 internal_error_pure_virtual_called ();
10798 }
10799
10800 static enum print_stop_action
10801 base_breakpoint_print_it (bpstat bs)
10802 {
10803 internal_error_pure_virtual_called ();
10804 }
10805
10806 static void
10807 base_breakpoint_print_one_detail (const struct breakpoint *self,
10808 struct ui_out *uiout)
10809 {
10810 /* nothing */
10811 }
10812
10813 static void
10814 base_breakpoint_print_mention (struct breakpoint *b)
10815 {
10816 internal_error_pure_virtual_called ();
10817 }
10818
10819 static void
10820 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
10821 {
10822 internal_error_pure_virtual_called ();
10823 }
10824
10825 static struct breakpoint_ops base_breakpoint_ops =
10826 {
10827 base_breakpoint_dtor,
10828 base_breakpoint_allocate_location,
10829 base_breakpoint_re_set,
10830 base_breakpoint_insert_location,
10831 base_breakpoint_remove_location,
10832 base_breakpoint_breakpoint_hit,
10833 base_breakpoint_check_status,
10834 base_breakpoint_resources_needed,
10835 base_breakpoint_works_in_software_mode,
10836 base_breakpoint_print_it,
10837 NULL,
10838 base_breakpoint_print_one_detail,
10839 base_breakpoint_print_mention,
10840 base_breakpoint_print_recreate
10841 };
10842
10843 /* Default breakpoint_ops methods. */
10844
10845 static void
10846 bkpt_re_set (struct breakpoint *b)
10847 {
10848 /* Do not attempt to re-set breakpoints disabled during startup. */
10849 if (b->enable_state == bp_startup_disabled)
10850 return;
10851
10852 /* FIXME: is this still reachable? */
10853 if (b->addr_string == NULL)
10854 {
10855 /* Anything without a string can't be re-set. */
10856 delete_breakpoint (b);
10857 return;
10858 }
10859
10860 breakpoint_re_set_default (b);
10861 }
10862
10863 static int
10864 bkpt_insert_location (struct bp_location *bl)
10865 {
10866 if (bl->loc_type == bp_loc_hardware_breakpoint)
10867 return target_insert_hw_breakpoint (bl->gdbarch,
10868 &bl->target_info);
10869 else
10870 return target_insert_breakpoint (bl->gdbarch,
10871 &bl->target_info);
10872 }
10873
10874 static int
10875 bkpt_remove_location (struct bp_location *bl)
10876 {
10877 if (bl->loc_type == bp_loc_hardware_breakpoint)
10878 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
10879 else
10880 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
10881 }
10882
10883 static int
10884 bkpt_breakpoint_hit (const struct bp_location *bl,
10885 struct address_space *aspace, CORE_ADDR bp_addr)
10886 {
10887 struct breakpoint *b = bl->owner;
10888
10889 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
10890 aspace, bp_addr))
10891 return 0;
10892
10893 if (overlay_debugging /* unmapped overlay section */
10894 && section_is_overlay (bl->section)
10895 && !section_is_mapped (bl->section))
10896 return 0;
10897
10898 return 1;
10899 }
10900
10901 static int
10902 bkpt_resources_needed (const struct bp_location *bl)
10903 {
10904 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
10905
10906 return 1;
10907 }
10908
10909 static enum print_stop_action
10910 bkpt_print_it (bpstat bs)
10911 {
10912 struct breakpoint *b;
10913 const struct bp_location *bl;
10914 int bp_temp;
10915 struct ui_out *uiout = current_uiout;
10916
10917 gdb_assert (bs->bp_location_at != NULL);
10918
10919 bl = bs->bp_location_at;
10920 b = bs->breakpoint_at;
10921
10922 bp_temp = b->disposition == disp_del;
10923 if (bl->address != bl->requested_address)
10924 breakpoint_adjustment_warning (bl->requested_address,
10925 bl->address,
10926 b->number, 1);
10927 annotate_breakpoint (b->number);
10928 if (bp_temp)
10929 ui_out_text (uiout, "\nTemporary breakpoint ");
10930 else
10931 ui_out_text (uiout, "\nBreakpoint ");
10932 if (ui_out_is_mi_like_p (uiout))
10933 {
10934 ui_out_field_string (uiout, "reason",
10935 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10936 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10937 }
10938 ui_out_field_int (uiout, "bkptno", b->number);
10939 ui_out_text (uiout, ", ");
10940
10941 return PRINT_SRC_AND_LOC;
10942 }
10943
10944 static void
10945 bkpt_print_mention (struct breakpoint *b)
10946 {
10947 if (ui_out_is_mi_like_p (current_uiout))
10948 return;
10949
10950 switch (b->type)
10951 {
10952 case bp_breakpoint:
10953 case bp_gnu_ifunc_resolver:
10954 if (b->disposition == disp_del)
10955 printf_filtered (_("Temporary breakpoint"));
10956 else
10957 printf_filtered (_("Breakpoint"));
10958 printf_filtered (_(" %d"), b->number);
10959 if (b->type == bp_gnu_ifunc_resolver)
10960 printf_filtered (_(" at gnu-indirect-function resolver"));
10961 break;
10962 case bp_hardware_breakpoint:
10963 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
10964 break;
10965 }
10966
10967 say_where (b);
10968 }
10969
10970 static void
10971 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
10972 {
10973 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
10974 fprintf_unfiltered (fp, "tbreak");
10975 else if (tp->type == bp_breakpoint)
10976 fprintf_unfiltered (fp, "break");
10977 else if (tp->type == bp_hardware_breakpoint
10978 && tp->disposition == disp_del)
10979 fprintf_unfiltered (fp, "thbreak");
10980 else if (tp->type == bp_hardware_breakpoint)
10981 fprintf_unfiltered (fp, "hbreak");
10982 else
10983 internal_error (__FILE__, __LINE__,
10984 _("unhandled breakpoint type %d"), (int) tp->type);
10985
10986 fprintf_unfiltered (fp, " %s", tp->addr_string);
10987 }
10988
10989 /* Virtual table for internal breakpoints. */
10990
10991 static void
10992 internal_bkpt_re_set (struct breakpoint *b)
10993 {
10994 switch (b->type)
10995 {
10996 /* Delete overlay event and longjmp master breakpoints; they
10997 will be reset later by breakpoint_re_set. */
10998 case bp_overlay_event:
10999 case bp_longjmp_master:
11000 case bp_std_terminate_master:
11001 case bp_exception_master:
11002 delete_breakpoint (b);
11003 break;
11004
11005 /* This breakpoint is special, it's set up when the inferior
11006 starts and we really don't want to touch it. */
11007 case bp_shlib_event:
11008
11009 /* Like bp_shlib_event, this breakpoint type is special. Once
11010 it is set up, we do not want to touch it. */
11011 case bp_thread_event:
11012 break;
11013 }
11014 }
11015
11016 static void
11017 internal_bkpt_check_status (bpstat bs)
11018 {
11019 /* We do not stop for these. */
11020 bs->stop = 0;
11021 }
11022
11023 static enum print_stop_action
11024 internal_bkpt_print_it (bpstat bs)
11025 {
11026 struct breakpoint *b;
11027
11028 b = bs->breakpoint_at;
11029
11030 switch (b->type)
11031 {
11032 case bp_shlib_event:
11033 /* Did we stop because the user set the stop_on_solib_events
11034 variable? (If so, we report this as a generic, "Stopped due
11035 to shlib event" message.) */
11036 printf_filtered (_("Stopped due to shared library event\n"));
11037 break;
11038
11039 case bp_thread_event:
11040 /* Not sure how we will get here.
11041 GDB should not stop for these breakpoints. */
11042 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
11043 break;
11044
11045 case bp_overlay_event:
11046 /* By analogy with the thread event, GDB should not stop for these. */
11047 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11048 break;
11049
11050 case bp_longjmp_master:
11051 /* These should never be enabled. */
11052 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11053 break;
11054
11055 case bp_std_terminate_master:
11056 /* These should never be enabled. */
11057 printf_filtered (_("std::terminate Master Breakpoint: "
11058 "gdb should not stop!\n"));
11059 break;
11060
11061 case bp_exception_master:
11062 /* These should never be enabled. */
11063 printf_filtered (_("Exception Master Breakpoint: "
11064 "gdb should not stop!\n"));
11065 break;
11066 }
11067
11068 return PRINT_NOTHING;
11069 }
11070
11071 static void
11072 internal_bkpt_print_mention (struct breakpoint *b)
11073 {
11074 /* Nothing to mention. These breakpoints are internal. */
11075 }
11076
11077 /* Virtual table for momentary breakpoints */
11078
11079 static void
11080 momentary_bkpt_re_set (struct breakpoint *b)
11081 {
11082 /* Keep temporary breakpoints, which can be encountered when we step
11083 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
11084 Otherwise these should have been blown away via the cleanup chain
11085 or by breakpoint_init_inferior when we rerun the executable. */
11086 }
11087
11088 static void
11089 momentary_bkpt_check_status (bpstat bs)
11090 {
11091 /* Nothing. The point of these breakpoints is causing a stop. */
11092 }
11093
11094 static enum print_stop_action
11095 momentary_bkpt_print_it (bpstat bs)
11096 {
11097 struct ui_out *uiout = current_uiout;
11098
11099 if (ui_out_is_mi_like_p (uiout))
11100 {
11101 struct breakpoint *b = bs->breakpoint_at;
11102
11103 switch (b->type)
11104 {
11105 case bp_finish:
11106 ui_out_field_string
11107 (uiout, "reason",
11108 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
11109 break;
11110
11111 case bp_until:
11112 ui_out_field_string
11113 (uiout, "reason",
11114 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
11115 break;
11116 }
11117 }
11118
11119 return PRINT_UNKNOWN;
11120 }
11121
11122 static void
11123 momentary_bkpt_print_mention (struct breakpoint *b)
11124 {
11125 /* Nothing to mention. These breakpoints are internal. */
11126 }
11127
11128 /* The breakpoint_ops structure to be used in tracepoints. */
11129
11130 static void
11131 tracepoint_re_set (struct breakpoint *b)
11132 {
11133 breakpoint_re_set_default (b);
11134 }
11135
11136 static int
11137 tracepoint_breakpoint_hit (const struct bp_location *bl,
11138 struct address_space *aspace, CORE_ADDR bp_addr)
11139 {
11140 /* By definition, the inferior does not report stops at
11141 tracepoints. */
11142 return 0;
11143 }
11144
11145 static void
11146 tracepoint_print_one_detail (const struct breakpoint *self,
11147 struct ui_out *uiout)
11148 {
11149 struct tracepoint *tp = (struct tracepoint *) self;
11150 if (tp->static_trace_marker_id)
11151 {
11152 gdb_assert (self->type == bp_static_tracepoint);
11153
11154 ui_out_text (uiout, "\tmarker id is ");
11155 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
11156 tp->static_trace_marker_id);
11157 ui_out_text (uiout, "\n");
11158 }
11159 }
11160
11161 static void
11162 tracepoint_print_mention (struct breakpoint *b)
11163 {
11164 if (ui_out_is_mi_like_p (current_uiout))
11165 return;
11166
11167 switch (b->type)
11168 {
11169 case bp_tracepoint:
11170 printf_filtered (_("Tracepoint"));
11171 printf_filtered (_(" %d"), b->number);
11172 break;
11173 case bp_fast_tracepoint:
11174 printf_filtered (_("Fast tracepoint"));
11175 printf_filtered (_(" %d"), b->number);
11176 break;
11177 case bp_static_tracepoint:
11178 printf_filtered (_("Static tracepoint"));
11179 printf_filtered (_(" %d"), b->number);
11180 break;
11181 default:
11182 internal_error (__FILE__, __LINE__,
11183 _("unhandled tracepoint type %d"), (int) b->type);
11184 }
11185
11186 say_where (b);
11187 }
11188
11189 static void
11190 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
11191 {
11192 struct tracepoint *tp = (struct tracepoint *) self;
11193
11194 if (self->type == bp_fast_tracepoint)
11195 fprintf_unfiltered (fp, "ftrace");
11196 if (self->type == bp_static_tracepoint)
11197 fprintf_unfiltered (fp, "strace");
11198 else if (self->type == bp_tracepoint)
11199 fprintf_unfiltered (fp, "trace");
11200 else
11201 internal_error (__FILE__, __LINE__,
11202 _("unhandled tracepoint type %d"), (int) self->type);
11203
11204 fprintf_unfiltered (fp, " %s", self->addr_string);
11205 print_recreate_thread (self, fp);
11206
11207 if (tp->pass_count)
11208 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
11209 }
11210
11211 struct breakpoint_ops tracepoint_breakpoint_ops;
11212
11213 /* Delete a breakpoint and clean up all traces of it in the data
11214 structures. */
11215
11216 void
11217 delete_breakpoint (struct breakpoint *bpt)
11218 {
11219 struct breakpoint *b;
11220
11221 gdb_assert (bpt != NULL);
11222
11223 /* Has this bp already been deleted? This can happen because
11224 multiple lists can hold pointers to bp's. bpstat lists are
11225 especial culprits.
11226
11227 One example of this happening is a watchpoint's scope bp. When
11228 the scope bp triggers, we notice that the watchpoint is out of
11229 scope, and delete it. We also delete its scope bp. But the
11230 scope bp is marked "auto-deleting", and is already on a bpstat.
11231 That bpstat is then checked for auto-deleting bp's, which are
11232 deleted.
11233
11234 A real solution to this problem might involve reference counts in
11235 bp's, and/or giving them pointers back to their referencing
11236 bpstat's, and teaching delete_breakpoint to only free a bp's
11237 storage when no more references were extent. A cheaper bandaid
11238 was chosen. */
11239 if (bpt->type == bp_none)
11240 return;
11241
11242 /* At least avoid this stale reference until the reference counting
11243 of breakpoints gets resolved. */
11244 if (bpt->related_breakpoint != bpt)
11245 {
11246 struct breakpoint *related;
11247 struct watchpoint *w;
11248
11249 if (bpt->type == bp_watchpoint_scope)
11250 w = (struct watchpoint *) bpt->related_breakpoint;
11251 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
11252 w = (struct watchpoint *) bpt;
11253 else
11254 w = NULL;
11255 if (w != NULL)
11256 watchpoint_del_at_next_stop (w);
11257
11258 /* Unlink bpt from the bpt->related_breakpoint ring. */
11259 for (related = bpt; related->related_breakpoint != bpt;
11260 related = related->related_breakpoint);
11261 related->related_breakpoint = bpt->related_breakpoint;
11262 bpt->related_breakpoint = bpt;
11263 }
11264
11265 /* watch_command_1 creates a watchpoint but only sets its number if
11266 update_watchpoint succeeds in creating its bp_locations. If there's
11267 a problem in that process, we'll be asked to delete the half-created
11268 watchpoint. In that case, don't announce the deletion. */
11269 if (bpt->number)
11270 observer_notify_breakpoint_deleted (bpt);
11271
11272 if (breakpoint_chain == bpt)
11273 breakpoint_chain = bpt->next;
11274
11275 ALL_BREAKPOINTS (b)
11276 if (b->next == bpt)
11277 {
11278 b->next = bpt->next;
11279 break;
11280 }
11281
11282 /* Be sure no bpstat's are pointing at the breakpoint after it's
11283 been freed. */
11284 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
11285 in all threads for now. Note that we cannot just remove bpstats
11286 pointing at bpt from the stop_bpstat list entirely, as breakpoint
11287 commands are associated with the bpstat; if we remove it here,
11288 then the later call to bpstat_do_actions (&stop_bpstat); in
11289 event-top.c won't do anything, and temporary breakpoints with
11290 commands won't work. */
11291
11292 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
11293
11294 /* Now that breakpoint is removed from breakpoint list, update the
11295 global location list. This will remove locations that used to
11296 belong to this breakpoint. Do this before freeing the breakpoint
11297 itself, since remove_breakpoint looks at location's owner. It
11298 might be better design to have location completely
11299 self-contained, but it's not the case now. */
11300 update_global_location_list (0);
11301
11302 bpt->ops->dtor (bpt);
11303 /* On the chance that someone will soon try again to delete this
11304 same bp, we mark it as deleted before freeing its storage. */
11305 bpt->type = bp_none;
11306 xfree (bpt);
11307 }
11308
11309 static void
11310 do_delete_breakpoint_cleanup (void *b)
11311 {
11312 delete_breakpoint (b);
11313 }
11314
11315 struct cleanup *
11316 make_cleanup_delete_breakpoint (struct breakpoint *b)
11317 {
11318 return make_cleanup (do_delete_breakpoint_cleanup, b);
11319 }
11320
11321 /* Iterator function to call a user-provided callback function once
11322 for each of B and its related breakpoints. */
11323
11324 static void
11325 iterate_over_related_breakpoints (struct breakpoint *b,
11326 void (*function) (struct breakpoint *,
11327 void *),
11328 void *data)
11329 {
11330 struct breakpoint *related;
11331
11332 related = b;
11333 do
11334 {
11335 struct breakpoint *next;
11336
11337 /* FUNCTION may delete RELATED. */
11338 next = related->related_breakpoint;
11339
11340 if (next == related)
11341 {
11342 /* RELATED is the last ring entry. */
11343 function (related, data);
11344
11345 /* FUNCTION may have deleted it, so we'd never reach back to
11346 B. There's nothing left to do anyway, so just break
11347 out. */
11348 break;
11349 }
11350 else
11351 function (related, data);
11352
11353 related = next;
11354 }
11355 while (related != b);
11356 }
11357
11358 static void
11359 do_delete_breakpoint (struct breakpoint *b, void *ignore)
11360 {
11361 delete_breakpoint (b);
11362 }
11363
11364 /* A callback for map_breakpoint_numbers that calls
11365 delete_breakpoint. */
11366
11367 static void
11368 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
11369 {
11370 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
11371 }
11372
11373 void
11374 delete_command (char *arg, int from_tty)
11375 {
11376 struct breakpoint *b, *b_tmp;
11377
11378 dont_repeat ();
11379
11380 if (arg == 0)
11381 {
11382 int breaks_to_delete = 0;
11383
11384 /* Delete all breakpoints if no argument. Do not delete
11385 internal breakpoints, these have to be deleted with an
11386 explicit breakpoint number argument. */
11387 ALL_BREAKPOINTS (b)
11388 if (user_breakpoint_p (b))
11389 {
11390 breaks_to_delete = 1;
11391 break;
11392 }
11393
11394 /* Ask user only if there are some breakpoints to delete. */
11395 if (!from_tty
11396 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
11397 {
11398 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11399 if (user_breakpoint_p (b))
11400 delete_breakpoint (b);
11401 }
11402 }
11403 else
11404 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
11405 }
11406
11407 static int
11408 all_locations_are_pending (struct bp_location *loc)
11409 {
11410 for (; loc; loc = loc->next)
11411 if (!loc->shlib_disabled)
11412 return 0;
11413 return 1;
11414 }
11415
11416 /* Subroutine of update_breakpoint_locations to simplify it.
11417 Return non-zero if multiple fns in list LOC have the same name.
11418 Null names are ignored. */
11419
11420 static int
11421 ambiguous_names_p (struct bp_location *loc)
11422 {
11423 struct bp_location *l;
11424 htab_t htab = htab_create_alloc (13, htab_hash_string,
11425 (int (*) (const void *,
11426 const void *)) streq,
11427 NULL, xcalloc, xfree);
11428
11429 for (l = loc; l != NULL; l = l->next)
11430 {
11431 const char **slot;
11432 const char *name = l->function_name;
11433
11434 /* Allow for some names to be NULL, ignore them. */
11435 if (name == NULL)
11436 continue;
11437
11438 slot = (const char **) htab_find_slot (htab, (const void *) name,
11439 INSERT);
11440 /* NOTE: We can assume slot != NULL here because xcalloc never
11441 returns NULL. */
11442 if (*slot != NULL)
11443 {
11444 htab_delete (htab);
11445 return 1;
11446 }
11447 *slot = name;
11448 }
11449
11450 htab_delete (htab);
11451 return 0;
11452 }
11453
11454 /* When symbols change, it probably means the sources changed as well,
11455 and it might mean the static tracepoint markers are no longer at
11456 the same address or line numbers they used to be at last we
11457 checked. Losing your static tracepoints whenever you rebuild is
11458 undesirable. This function tries to resync/rematch gdb static
11459 tracepoints with the markers on the target, for static tracepoints
11460 that have not been set by marker id. Static tracepoint that have
11461 been set by marker id are reset by marker id in breakpoint_re_set.
11462 The heuristic is:
11463
11464 1) For a tracepoint set at a specific address, look for a marker at
11465 the old PC. If one is found there, assume to be the same marker.
11466 If the name / string id of the marker found is different from the
11467 previous known name, assume that means the user renamed the marker
11468 in the sources, and output a warning.
11469
11470 2) For a tracepoint set at a given line number, look for a marker
11471 at the new address of the old line number. If one is found there,
11472 assume to be the same marker. If the name / string id of the
11473 marker found is different from the previous known name, assume that
11474 means the user renamed the marker in the sources, and output a
11475 warning.
11476
11477 3) If a marker is no longer found at the same address or line, it
11478 may mean the marker no longer exists. But it may also just mean
11479 the code changed a bit. Maybe the user added a few lines of code
11480 that made the marker move up or down (in line number terms). Ask
11481 the target for info about the marker with the string id as we knew
11482 it. If found, update line number and address in the matching
11483 static tracepoint. This will get confused if there's more than one
11484 marker with the same ID (possible in UST, although unadvised
11485 precisely because it confuses tools). */
11486
11487 static struct symtab_and_line
11488 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
11489 {
11490 struct tracepoint *tp = (struct tracepoint *) b;
11491 struct static_tracepoint_marker marker;
11492 CORE_ADDR pc;
11493 int i;
11494
11495 pc = sal.pc;
11496 if (sal.line)
11497 find_line_pc (sal.symtab, sal.line, &pc);
11498
11499 if (target_static_tracepoint_marker_at (pc, &marker))
11500 {
11501 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
11502 warning (_("static tracepoint %d changed probed marker from %s to %s"),
11503 b->number,
11504 tp->static_trace_marker_id, marker.str_id);
11505
11506 xfree (tp->static_trace_marker_id);
11507 tp->static_trace_marker_id = xstrdup (marker.str_id);
11508 release_static_tracepoint_marker (&marker);
11509
11510 return sal;
11511 }
11512
11513 /* Old marker wasn't found on target at lineno. Try looking it up
11514 by string ID. */
11515 if (!sal.explicit_pc
11516 && sal.line != 0
11517 && sal.symtab != NULL
11518 && tp->static_trace_marker_id != NULL)
11519 {
11520 VEC(static_tracepoint_marker_p) *markers;
11521
11522 markers
11523 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
11524
11525 if (!VEC_empty(static_tracepoint_marker_p, markers))
11526 {
11527 struct symtab_and_line sal;
11528 struct symbol *sym;
11529 struct static_tracepoint_marker *marker;
11530 struct ui_out *uiout = current_uiout;
11531
11532 marker = VEC_index (static_tracepoint_marker_p, markers, 0);
11533
11534 xfree (tp->static_trace_marker_id);
11535 tp->static_trace_marker_id = xstrdup (marker->str_id);
11536
11537 warning (_("marker for static tracepoint %d (%s) not "
11538 "found at previous line number"),
11539 b->number, tp->static_trace_marker_id);
11540
11541 init_sal (&sal);
11542
11543 sal.pc = marker->address;
11544
11545 sal = find_pc_line (marker->address, 0);
11546 sym = find_pc_sect_function (marker->address, NULL);
11547 ui_out_text (uiout, "Now in ");
11548 if (sym)
11549 {
11550 ui_out_field_string (uiout, "func",
11551 SYMBOL_PRINT_NAME (sym));
11552 ui_out_text (uiout, " at ");
11553 }
11554 ui_out_field_string (uiout, "file", sal.symtab->filename);
11555 ui_out_text (uiout, ":");
11556
11557 if (ui_out_is_mi_like_p (uiout))
11558 {
11559 char *fullname = symtab_to_fullname (sal.symtab);
11560
11561 if (fullname)
11562 ui_out_field_string (uiout, "fullname", fullname);
11563 }
11564
11565 ui_out_field_int (uiout, "line", sal.line);
11566 ui_out_text (uiout, "\n");
11567
11568 b->line_number = sal.line;
11569
11570 xfree (b->source_file);
11571 if (sym)
11572 b->source_file = xstrdup (sal.symtab->filename);
11573 else
11574 b->source_file = NULL;
11575
11576 xfree (b->addr_string);
11577 b->addr_string = xstrprintf ("%s:%d",
11578 sal.symtab->filename, b->line_number);
11579
11580 /* Might be nice to check if function changed, and warn if
11581 so. */
11582
11583 release_static_tracepoint_marker (marker);
11584 }
11585 }
11586 return sal;
11587 }
11588
11589 /* Returns 1 iff locations A and B are sufficiently same that
11590 we don't need to report breakpoint as changed. */
11591
11592 static int
11593 locations_are_equal (struct bp_location *a, struct bp_location *b)
11594 {
11595 while (a && b)
11596 {
11597 if (a->address != b->address)
11598 return 0;
11599
11600 if (a->shlib_disabled != b->shlib_disabled)
11601 return 0;
11602
11603 if (a->enabled != b->enabled)
11604 return 0;
11605
11606 a = a->next;
11607 b = b->next;
11608 }
11609
11610 if ((a == NULL) != (b == NULL))
11611 return 0;
11612
11613 return 1;
11614 }
11615
11616 /* Create new breakpoint locations for B (a hardware or software breakpoint)
11617 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
11618 a ranged breakpoint. */
11619
11620 void
11621 update_breakpoint_locations (struct breakpoint *b,
11622 struct symtabs_and_lines sals,
11623 struct symtabs_and_lines sals_end)
11624 {
11625 int i;
11626 struct bp_location *existing_locations = b->loc;
11627
11628 /* Ranged breakpoints have only one start location and one end location. */
11629 gdb_assert (sals_end.nelts == 0 || (sals.nelts == 1 && sals_end.nelts == 1));
11630
11631 /* If there's no new locations, and all existing locations are
11632 pending, don't do anything. This optimizes the common case where
11633 all locations are in the same shared library, that was unloaded.
11634 We'd like to retain the location, so that when the library is
11635 loaded again, we don't loose the enabled/disabled status of the
11636 individual locations. */
11637 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
11638 return;
11639
11640 b->loc = NULL;
11641
11642 for (i = 0; i < sals.nelts; ++i)
11643 {
11644 struct bp_location *new_loc =
11645 add_location_to_breakpoint (b, &(sals.sals[i]));
11646
11647 /* Reparse conditions, they might contain references to the
11648 old symtab. */
11649 if (b->cond_string != NULL)
11650 {
11651 char *s;
11652 struct gdb_exception e;
11653
11654 s = b->cond_string;
11655 TRY_CATCH (e, RETURN_MASK_ERROR)
11656 {
11657 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
11658 0);
11659 }
11660 if (e.reason < 0)
11661 {
11662 warning (_("failed to reevaluate condition "
11663 "for breakpoint %d: %s"),
11664 b->number, e.message);
11665 new_loc->enabled = 0;
11666 }
11667 }
11668
11669 if (b->source_file != NULL)
11670 xfree (b->source_file);
11671 if (sals.sals[i].symtab == NULL)
11672 b->source_file = NULL;
11673 else
11674 b->source_file = xstrdup (sals.sals[i].symtab->filename);
11675
11676 if (b->line_number == 0)
11677 b->line_number = sals.sals[i].line;
11678
11679 if (sals_end.nelts)
11680 {
11681 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
11682
11683 new_loc->length = end - sals.sals[0].pc + 1;
11684 }
11685 }
11686
11687 /* Update locations of permanent breakpoints. */
11688 if (b->enable_state == bp_permanent)
11689 make_breakpoint_permanent (b);
11690
11691 /* If possible, carry over 'disable' status from existing
11692 breakpoints. */
11693 {
11694 struct bp_location *e = existing_locations;
11695 /* If there are multiple breakpoints with the same function name,
11696 e.g. for inline functions, comparing function names won't work.
11697 Instead compare pc addresses; this is just a heuristic as things
11698 may have moved, but in practice it gives the correct answer
11699 often enough until a better solution is found. */
11700 int have_ambiguous_names = ambiguous_names_p (b->loc);
11701
11702 for (; e; e = e->next)
11703 {
11704 if (!e->enabled && e->function_name)
11705 {
11706 struct bp_location *l = b->loc;
11707 if (have_ambiguous_names)
11708 {
11709 for (; l; l = l->next)
11710 if (breakpoint_locations_match (e, l))
11711 {
11712 l->enabled = 0;
11713 break;
11714 }
11715 }
11716 else
11717 {
11718 for (; l; l = l->next)
11719 if (l->function_name
11720 && strcmp (e->function_name, l->function_name) == 0)
11721 {
11722 l->enabled = 0;
11723 break;
11724 }
11725 }
11726 }
11727 }
11728 }
11729
11730 if (!locations_are_equal (existing_locations, b->loc))
11731 observer_notify_breakpoint_modified (b);
11732
11733 update_global_location_list (1);
11734 }
11735
11736 /* Find the SaL locations corresponding to the given ADDR_STRING.
11737 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
11738
11739 static struct symtabs_and_lines
11740 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
11741 {
11742 char *s;
11743 int marker_spec;
11744 struct symtabs_and_lines sals = {0};
11745 struct gdb_exception e;
11746
11747 s = addr_string;
11748 marker_spec = b->type == bp_static_tracepoint && is_marker_spec (s);
11749
11750 TRY_CATCH (e, RETURN_MASK_ERROR)
11751 {
11752 if (marker_spec)
11753 {
11754 struct tracepoint *tp = (struct tracepoint *) b;
11755
11756 sals = decode_static_tracepoint_spec (&s);
11757 if (sals.nelts > tp->static_trace_marker_id_idx)
11758 {
11759 sals.sals[0] = sals.sals[tp->static_trace_marker_id_idx];
11760 sals.nelts = 1;
11761 }
11762 else
11763 error (_("marker %s not found"), tp->static_trace_marker_id);
11764 }
11765 else
11766 sals = decode_line_1 (&s, 1, (struct symtab *) NULL, 0, NULL);
11767 }
11768 if (e.reason < 0)
11769 {
11770 int not_found_and_ok = 0;
11771 /* For pending breakpoints, it's expected that parsing will
11772 fail until the right shared library is loaded. User has
11773 already told to create pending breakpoints and don't need
11774 extra messages. If breakpoint is in bp_shlib_disabled
11775 state, then user already saw the message about that
11776 breakpoint being disabled, and don't want to see more
11777 errors. */
11778 if (e.error == NOT_FOUND_ERROR
11779 && (b->condition_not_parsed
11780 || (b->loc && b->loc->shlib_disabled)
11781 || b->enable_state == bp_disabled))
11782 not_found_and_ok = 1;
11783
11784 if (!not_found_and_ok)
11785 {
11786 /* We surely don't want to warn about the same breakpoint
11787 10 times. One solution, implemented here, is disable
11788 the breakpoint on error. Another solution would be to
11789 have separate 'warning emitted' flag. Since this
11790 happens only when a binary has changed, I don't know
11791 which approach is better. */
11792 b->enable_state = bp_disabled;
11793 throw_exception (e);
11794 }
11795 }
11796
11797 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
11798 {
11799 gdb_assert (sals.nelts == 1);
11800
11801 resolve_sal_pc (&sals.sals[0]);
11802 if (b->condition_not_parsed && s && s[0])
11803 {
11804 char *cond_string = 0;
11805 int thread = -1;
11806 int task = 0;
11807
11808 find_condition_and_thread (s, sals.sals[0].pc,
11809 &cond_string, &thread, &task);
11810 if (cond_string)
11811 b->cond_string = cond_string;
11812 b->thread = thread;
11813 b->task = task;
11814 b->condition_not_parsed = 0;
11815 }
11816
11817 if (b->type == bp_static_tracepoint && !marker_spec)
11818 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
11819
11820 *found = 1;
11821 }
11822 else
11823 *found = 0;
11824
11825 return sals;
11826 }
11827
11828 /* The default re_set method, for typical hardware or software
11829 breakpoints. Reevaluate the breakpoint and recreate its
11830 locations. */
11831
11832 static void
11833 breakpoint_re_set_default (struct breakpoint *b)
11834 {
11835 int found;
11836 struct symtabs_and_lines sals, sals_end;
11837 struct symtabs_and_lines expanded = {0};
11838 struct symtabs_and_lines expanded_end = {0};
11839
11840 sals = addr_string_to_sals (b, b->addr_string, &found);
11841 if (found)
11842 {
11843 make_cleanup (xfree, sals.sals);
11844 expanded = expand_line_sal_maybe (sals.sals[0]);
11845 }
11846
11847 if (b->addr_string_range_end)
11848 {
11849 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
11850 if (found)
11851 {
11852 make_cleanup (xfree, sals_end.sals);
11853 expanded_end = expand_line_sal_maybe (sals_end.sals[0]);
11854 }
11855 }
11856
11857 update_breakpoint_locations (b, expanded, expanded_end);
11858 }
11859
11860 /* Prepare the global context for a re-set of breakpoint B. */
11861
11862 static struct cleanup *
11863 prepare_re_set_context (struct breakpoint *b)
11864 {
11865 struct cleanup *cleanups;
11866
11867 input_radix = b->input_radix;
11868 cleanups = save_current_space_and_thread ();
11869 switch_to_program_space_and_thread (b->pspace);
11870 set_language (b->language);
11871
11872 return cleanups;
11873 }
11874
11875 /* Reset a breakpoint given it's struct breakpoint * BINT.
11876 The value we return ends up being the return value from catch_errors.
11877 Unused in this case. */
11878
11879 static int
11880 breakpoint_re_set_one (void *bint)
11881 {
11882 /* Get past catch_errs. */
11883 struct breakpoint *b = (struct breakpoint *) bint;
11884 struct cleanup *cleanups;
11885
11886 cleanups = prepare_re_set_context (b);
11887 b->ops->re_set (b);
11888 do_cleanups (cleanups);
11889 return 0;
11890 }
11891
11892 /* Re-set all breakpoints after symbols have been re-loaded. */
11893 void
11894 breakpoint_re_set (void)
11895 {
11896 struct breakpoint *b, *b_tmp;
11897 enum language save_language;
11898 int save_input_radix;
11899 struct cleanup *old_chain;
11900
11901 save_language = current_language->la_language;
11902 save_input_radix = input_radix;
11903 old_chain = save_current_program_space ();
11904
11905 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11906 {
11907 /* Format possible error msg. */
11908 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
11909 b->number);
11910 struct cleanup *cleanups = make_cleanup (xfree, message);
11911 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
11912 do_cleanups (cleanups);
11913 }
11914 set_language (save_language);
11915 input_radix = save_input_radix;
11916
11917 jit_breakpoint_re_set ();
11918
11919 do_cleanups (old_chain);
11920
11921 create_overlay_event_breakpoint ();
11922 create_longjmp_master_breakpoint ();
11923 create_std_terminate_master_breakpoint ();
11924 create_exception_master_breakpoint ();
11925 }
11926 \f
11927 /* Reset the thread number of this breakpoint:
11928
11929 - If the breakpoint is for all threads, leave it as-is.
11930 - Else, reset it to the current thread for inferior_ptid. */
11931 void
11932 breakpoint_re_set_thread (struct breakpoint *b)
11933 {
11934 if (b->thread != -1)
11935 {
11936 if (in_thread_list (inferior_ptid))
11937 b->thread = pid_to_thread_id (inferior_ptid);
11938
11939 /* We're being called after following a fork. The new fork is
11940 selected as current, and unless this was a vfork will have a
11941 different program space from the original thread. Reset that
11942 as well. */
11943 b->loc->pspace = current_program_space;
11944 }
11945 }
11946
11947 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
11948 If from_tty is nonzero, it prints a message to that effect,
11949 which ends with a period (no newline). */
11950
11951 void
11952 set_ignore_count (int bptnum, int count, int from_tty)
11953 {
11954 struct breakpoint *b;
11955
11956 if (count < 0)
11957 count = 0;
11958
11959 ALL_BREAKPOINTS (b)
11960 if (b->number == bptnum)
11961 {
11962 if (is_tracepoint (b))
11963 {
11964 if (from_tty && count != 0)
11965 printf_filtered (_("Ignore count ignored for tracepoint %d."),
11966 bptnum);
11967 return;
11968 }
11969
11970 b->ignore_count = count;
11971 if (from_tty)
11972 {
11973 if (count == 0)
11974 printf_filtered (_("Will stop next time "
11975 "breakpoint %d is reached."),
11976 bptnum);
11977 else if (count == 1)
11978 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
11979 bptnum);
11980 else
11981 printf_filtered (_("Will ignore next %d "
11982 "crossings of breakpoint %d."),
11983 count, bptnum);
11984 }
11985 breakpoints_changed ();
11986 observer_notify_breakpoint_modified (b);
11987 return;
11988 }
11989
11990 error (_("No breakpoint number %d."), bptnum);
11991 }
11992
11993 /* Command to set ignore-count of breakpoint N to COUNT. */
11994
11995 static void
11996 ignore_command (char *args, int from_tty)
11997 {
11998 char *p = args;
11999 int num;
12000
12001 if (p == 0)
12002 error_no_arg (_("a breakpoint number"));
12003
12004 num = get_number (&p);
12005 if (num == 0)
12006 error (_("bad breakpoint number: '%s'"), args);
12007 if (*p == 0)
12008 error (_("Second argument (specified ignore-count) is missing."));
12009
12010 set_ignore_count (num,
12011 longest_to_int (value_as_long (parse_and_eval (p))),
12012 from_tty);
12013 if (from_tty)
12014 printf_filtered ("\n");
12015 }
12016 \f
12017 /* Call FUNCTION on each of the breakpoints
12018 whose numbers are given in ARGS. */
12019
12020 static void
12021 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
12022 void *),
12023 void *data)
12024 {
12025 int num;
12026 struct breakpoint *b, *tmp;
12027 int match;
12028 struct get_number_or_range_state state;
12029
12030 if (args == 0)
12031 error_no_arg (_("one or more breakpoint numbers"));
12032
12033 init_number_or_range (&state, args);
12034
12035 while (!state.finished)
12036 {
12037 char *p = state.string;
12038
12039 match = 0;
12040
12041 num = get_number_or_range (&state);
12042 if (num == 0)
12043 {
12044 warning (_("bad breakpoint number at or near '%s'"), p);
12045 }
12046 else
12047 {
12048 ALL_BREAKPOINTS_SAFE (b, tmp)
12049 if (b->number == num)
12050 {
12051 match = 1;
12052 function (b, data);
12053 break;
12054 }
12055 if (match == 0)
12056 printf_unfiltered (_("No breakpoint number %d.\n"), num);
12057 }
12058 }
12059 }
12060
12061 static struct bp_location *
12062 find_location_by_number (char *number)
12063 {
12064 char *dot = strchr (number, '.');
12065 char *p1;
12066 int bp_num;
12067 int loc_num;
12068 struct breakpoint *b;
12069 struct bp_location *loc;
12070
12071 *dot = '\0';
12072
12073 p1 = number;
12074 bp_num = get_number (&p1);
12075 if (bp_num == 0)
12076 error (_("Bad breakpoint number '%s'"), number);
12077
12078 ALL_BREAKPOINTS (b)
12079 if (b->number == bp_num)
12080 {
12081 break;
12082 }
12083
12084 if (!b || b->number != bp_num)
12085 error (_("Bad breakpoint number '%s'"), number);
12086
12087 p1 = dot+1;
12088 loc_num = get_number (&p1);
12089 if (loc_num == 0)
12090 error (_("Bad breakpoint location number '%s'"), number);
12091
12092 --loc_num;
12093 loc = b->loc;
12094 for (;loc_num && loc; --loc_num, loc = loc->next)
12095 ;
12096 if (!loc)
12097 error (_("Bad breakpoint location number '%s'"), dot+1);
12098
12099 return loc;
12100 }
12101
12102
12103 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12104 If from_tty is nonzero, it prints a message to that effect,
12105 which ends with a period (no newline). */
12106
12107 void
12108 disable_breakpoint (struct breakpoint *bpt)
12109 {
12110 /* Never disable a watchpoint scope breakpoint; we want to
12111 hit them when we leave scope so we can delete both the
12112 watchpoint and its scope breakpoint at that time. */
12113 if (bpt->type == bp_watchpoint_scope)
12114 return;
12115
12116 /* You can't disable permanent breakpoints. */
12117 if (bpt->enable_state == bp_permanent)
12118 return;
12119
12120 bpt->enable_state = bp_disabled;
12121
12122 if (target_supports_enable_disable_tracepoint ()
12123 && current_trace_status ()->running && is_tracepoint (bpt))
12124 {
12125 struct bp_location *location;
12126
12127 for (location = bpt->loc; location; location = location->next)
12128 target_disable_tracepoint (location);
12129 }
12130
12131 update_global_location_list (0);
12132
12133 observer_notify_breakpoint_modified (bpt);
12134 }
12135
12136 /* A callback for iterate_over_related_breakpoints. */
12137
12138 static void
12139 do_disable_breakpoint (struct breakpoint *b, void *ignore)
12140 {
12141 disable_breakpoint (b);
12142 }
12143
12144 /* A callback for map_breakpoint_numbers that calls
12145 disable_breakpoint. */
12146
12147 static void
12148 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
12149 {
12150 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
12151 }
12152
12153 static void
12154 disable_command (char *args, int from_tty)
12155 {
12156 if (args == 0)
12157 {
12158 struct breakpoint *bpt;
12159
12160 ALL_BREAKPOINTS (bpt)
12161 if (user_breakpoint_p (bpt))
12162 disable_breakpoint (bpt);
12163 }
12164 else if (strchr (args, '.'))
12165 {
12166 struct bp_location *loc = find_location_by_number (args);
12167 if (loc)
12168 {
12169 loc->enabled = 0;
12170 if (target_supports_enable_disable_tracepoint ()
12171 && current_trace_status ()->running && loc->owner
12172 && is_tracepoint (loc->owner))
12173 target_disable_tracepoint (loc);
12174 }
12175 update_global_location_list (0);
12176 }
12177 else
12178 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
12179 }
12180
12181 static void
12182 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition)
12183 {
12184 int target_resources_ok;
12185
12186 if (bpt->type == bp_hardware_breakpoint)
12187 {
12188 int i;
12189 i = hw_breakpoint_used_count ();
12190 target_resources_ok =
12191 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
12192 i + 1, 0);
12193 if (target_resources_ok == 0)
12194 error (_("No hardware breakpoint support in the target."));
12195 else if (target_resources_ok < 0)
12196 error (_("Hardware breakpoints used exceeds limit."));
12197 }
12198
12199 if (is_watchpoint (bpt))
12200 {
12201 /* Initialize it just to avoid a GCC false warning. */
12202 enum enable_state orig_enable_state = 0;
12203 struct gdb_exception e;
12204
12205 TRY_CATCH (e, RETURN_MASK_ALL)
12206 {
12207 struct watchpoint *w = (struct watchpoint *) bpt;
12208
12209 orig_enable_state = bpt->enable_state;
12210 bpt->enable_state = bp_enabled;
12211 update_watchpoint (w, 1 /* reparse */);
12212 }
12213 if (e.reason < 0)
12214 {
12215 bpt->enable_state = orig_enable_state;
12216 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
12217 bpt->number);
12218 return;
12219 }
12220 }
12221
12222 if (bpt->enable_state != bp_permanent)
12223 bpt->enable_state = bp_enabled;
12224
12225 if (target_supports_enable_disable_tracepoint ()
12226 && current_trace_status ()->running && is_tracepoint (bpt))
12227 {
12228 struct bp_location *location;
12229
12230 for (location = bpt->loc; location; location = location->next)
12231 target_enable_tracepoint (location);
12232 }
12233
12234 bpt->disposition = disposition;
12235 update_global_location_list (1);
12236 breakpoints_changed ();
12237
12238 observer_notify_breakpoint_modified (bpt);
12239 }
12240
12241
12242 void
12243 enable_breakpoint (struct breakpoint *bpt)
12244 {
12245 enable_breakpoint_disp (bpt, bpt->disposition);
12246 }
12247
12248 static void
12249 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
12250 {
12251 enable_breakpoint (bpt);
12252 }
12253
12254 /* A callback for map_breakpoint_numbers that calls
12255 enable_breakpoint. */
12256
12257 static void
12258 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
12259 {
12260 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
12261 }
12262
12263 /* The enable command enables the specified breakpoints (or all defined
12264 breakpoints) so they once again become (or continue to be) effective
12265 in stopping the inferior. */
12266
12267 static void
12268 enable_command (char *args, int from_tty)
12269 {
12270 if (args == 0)
12271 {
12272 struct breakpoint *bpt;
12273
12274 ALL_BREAKPOINTS (bpt)
12275 if (user_breakpoint_p (bpt))
12276 enable_breakpoint (bpt);
12277 }
12278 else if (strchr (args, '.'))
12279 {
12280 struct bp_location *loc = find_location_by_number (args);
12281 if (loc)
12282 {
12283 loc->enabled = 1;
12284 if (target_supports_enable_disable_tracepoint ()
12285 && current_trace_status ()->running && loc->owner
12286 && is_tracepoint (loc->owner))
12287 target_enable_tracepoint (loc);
12288 }
12289 update_global_location_list (1);
12290 }
12291 else
12292 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
12293 }
12294
12295 static void
12296 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
12297 {
12298 enum bpdisp disp = *(enum bpdisp *) arg;
12299
12300 enable_breakpoint_disp (bpt, disp);
12301 }
12302
12303 static void
12304 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
12305 {
12306 enum bpdisp disp = disp_disable;
12307
12308 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
12309 }
12310
12311 static void
12312 enable_once_command (char *args, int from_tty)
12313 {
12314 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
12315 }
12316
12317 static void
12318 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
12319 {
12320 enum bpdisp disp = disp_del;
12321
12322 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
12323 }
12324
12325 static void
12326 enable_delete_command (char *args, int from_tty)
12327 {
12328 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
12329 }
12330 \f
12331 static void
12332 set_breakpoint_cmd (char *args, int from_tty)
12333 {
12334 }
12335
12336 static void
12337 show_breakpoint_cmd (char *args, int from_tty)
12338 {
12339 }
12340
12341 /* Invalidate last known value of any hardware watchpoint if
12342 the memory which that value represents has been written to by
12343 GDB itself. */
12344
12345 static void
12346 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
12347 const bfd_byte *data)
12348 {
12349 struct breakpoint *bp;
12350
12351 ALL_BREAKPOINTS (bp)
12352 if (bp->enable_state == bp_enabled
12353 && bp->type == bp_hardware_watchpoint)
12354 {
12355 struct watchpoint *wp = (struct watchpoint *) bp;
12356
12357 if (wp->val_valid && wp->val)
12358 {
12359 struct bp_location *loc;
12360
12361 for (loc = bp->loc; loc != NULL; loc = loc->next)
12362 if (loc->loc_type == bp_loc_hardware_watchpoint
12363 && loc->address + loc->length > addr
12364 && addr + len > loc->address)
12365 {
12366 value_free (wp->val);
12367 wp->val = NULL;
12368 wp->val_valid = 0;
12369 }
12370 }
12371 }
12372 }
12373
12374 /* Use default_breakpoint_'s, or nothing if they aren't valid. */
12375
12376 struct symtabs_and_lines
12377 decode_line_spec_1 (char *string, int funfirstline)
12378 {
12379 struct symtabs_and_lines sals;
12380
12381 if (string == 0)
12382 error (_("Empty line specification."));
12383 if (default_breakpoint_valid)
12384 sals = decode_line_1 (&string, funfirstline,
12385 default_breakpoint_symtab,
12386 default_breakpoint_line,
12387 NULL);
12388 else
12389 sals = decode_line_1 (&string, funfirstline,
12390 (struct symtab *) NULL, 0, NULL);
12391 if (*string)
12392 error (_("Junk at end of line specification: %s"), string);
12393 return sals;
12394 }
12395
12396 /* Create and insert a raw software breakpoint at PC. Return an
12397 identifier, which should be used to remove the breakpoint later.
12398 In general, places which call this should be using something on the
12399 breakpoint chain instead; this function should be eliminated
12400 someday. */
12401
12402 void *
12403 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
12404 struct address_space *aspace, CORE_ADDR pc)
12405 {
12406 struct bp_target_info *bp_tgt;
12407
12408 bp_tgt = XZALLOC (struct bp_target_info);
12409
12410 bp_tgt->placed_address_space = aspace;
12411 bp_tgt->placed_address = pc;
12412
12413 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
12414 {
12415 /* Could not insert the breakpoint. */
12416 xfree (bp_tgt);
12417 return NULL;
12418 }
12419
12420 return bp_tgt;
12421 }
12422
12423 /* Remove a breakpoint BP inserted by
12424 deprecated_insert_raw_breakpoint. */
12425
12426 int
12427 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
12428 {
12429 struct bp_target_info *bp_tgt = bp;
12430 int ret;
12431
12432 ret = target_remove_breakpoint (gdbarch, bp_tgt);
12433 xfree (bp_tgt);
12434
12435 return ret;
12436 }
12437
12438 /* One (or perhaps two) breakpoints used for software single
12439 stepping. */
12440
12441 static void *single_step_breakpoints[2];
12442 static struct gdbarch *single_step_gdbarch[2];
12443
12444 /* Create and insert a breakpoint for software single step. */
12445
12446 void
12447 insert_single_step_breakpoint (struct gdbarch *gdbarch,
12448 struct address_space *aspace,
12449 CORE_ADDR next_pc)
12450 {
12451 void **bpt_p;
12452
12453 if (single_step_breakpoints[0] == NULL)
12454 {
12455 bpt_p = &single_step_breakpoints[0];
12456 single_step_gdbarch[0] = gdbarch;
12457 }
12458 else
12459 {
12460 gdb_assert (single_step_breakpoints[1] == NULL);
12461 bpt_p = &single_step_breakpoints[1];
12462 single_step_gdbarch[1] = gdbarch;
12463 }
12464
12465 /* NOTE drow/2006-04-11: A future improvement to this function would
12466 be to only create the breakpoints once, and actually put them on
12467 the breakpoint chain. That would let us use set_raw_breakpoint.
12468 We could adjust the addresses each time they were needed. Doing
12469 this requires corresponding changes elsewhere where single step
12470 breakpoints are handled, however. So, for now, we use this. */
12471
12472 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
12473 if (*bpt_p == NULL)
12474 error (_("Could not insert single-step breakpoint at %s"),
12475 paddress (gdbarch, next_pc));
12476 }
12477
12478 /* Check if the breakpoints used for software single stepping
12479 were inserted or not. */
12480
12481 int
12482 single_step_breakpoints_inserted (void)
12483 {
12484 return (single_step_breakpoints[0] != NULL
12485 || single_step_breakpoints[1] != NULL);
12486 }
12487
12488 /* Remove and delete any breakpoints used for software single step. */
12489
12490 void
12491 remove_single_step_breakpoints (void)
12492 {
12493 gdb_assert (single_step_breakpoints[0] != NULL);
12494
12495 /* See insert_single_step_breakpoint for more about this deprecated
12496 call. */
12497 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
12498 single_step_breakpoints[0]);
12499 single_step_gdbarch[0] = NULL;
12500 single_step_breakpoints[0] = NULL;
12501
12502 if (single_step_breakpoints[1] != NULL)
12503 {
12504 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
12505 single_step_breakpoints[1]);
12506 single_step_gdbarch[1] = NULL;
12507 single_step_breakpoints[1] = NULL;
12508 }
12509 }
12510
12511 /* Delete software single step breakpoints without removing them from
12512 the inferior. This is intended to be used if the inferior's address
12513 space where they were inserted is already gone, e.g. after exit or
12514 exec. */
12515
12516 void
12517 cancel_single_step_breakpoints (void)
12518 {
12519 int i;
12520
12521 for (i = 0; i < 2; i++)
12522 if (single_step_breakpoints[i])
12523 {
12524 xfree (single_step_breakpoints[i]);
12525 single_step_breakpoints[i] = NULL;
12526 single_step_gdbarch[i] = NULL;
12527 }
12528 }
12529
12530 /* Detach software single-step breakpoints from INFERIOR_PTID without
12531 removing them. */
12532
12533 static void
12534 detach_single_step_breakpoints (void)
12535 {
12536 int i;
12537
12538 for (i = 0; i < 2; i++)
12539 if (single_step_breakpoints[i])
12540 target_remove_breakpoint (single_step_gdbarch[i],
12541 single_step_breakpoints[i]);
12542 }
12543
12544 /* Check whether a software single-step breakpoint is inserted at
12545 PC. */
12546
12547 static int
12548 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
12549 CORE_ADDR pc)
12550 {
12551 int i;
12552
12553 for (i = 0; i < 2; i++)
12554 {
12555 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
12556 if (bp_tgt
12557 && breakpoint_address_match (bp_tgt->placed_address_space,
12558 bp_tgt->placed_address,
12559 aspace, pc))
12560 return 1;
12561 }
12562
12563 return 0;
12564 }
12565
12566 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
12567 non-zero otherwise. */
12568 static int
12569 is_syscall_catchpoint_enabled (struct breakpoint *bp)
12570 {
12571 if (syscall_catchpoint_p (bp)
12572 && bp->enable_state != bp_disabled
12573 && bp->enable_state != bp_call_disabled)
12574 return 1;
12575 else
12576 return 0;
12577 }
12578
12579 int
12580 catch_syscall_enabled (void)
12581 {
12582 struct inferior *inf = current_inferior ();
12583
12584 return inf->total_syscalls_count != 0;
12585 }
12586
12587 int
12588 catching_syscall_number (int syscall_number)
12589 {
12590 struct breakpoint *bp;
12591
12592 ALL_BREAKPOINTS (bp)
12593 if (is_syscall_catchpoint_enabled (bp))
12594 {
12595 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
12596
12597 if (c->syscalls_to_be_caught)
12598 {
12599 int i, iter;
12600 for (i = 0;
12601 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
12602 i++)
12603 if (syscall_number == iter)
12604 return 1;
12605 }
12606 else
12607 return 1;
12608 }
12609
12610 return 0;
12611 }
12612
12613 /* Complete syscall names. Used by "catch syscall". */
12614 static char **
12615 catch_syscall_completer (struct cmd_list_element *cmd,
12616 char *text, char *word)
12617 {
12618 const char **list = get_syscall_names ();
12619 char **retlist
12620 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
12621
12622 xfree (list);
12623 return retlist;
12624 }
12625
12626 /* Tracepoint-specific operations. */
12627
12628 /* Set tracepoint count to NUM. */
12629 static void
12630 set_tracepoint_count (int num)
12631 {
12632 tracepoint_count = num;
12633 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
12634 }
12635
12636 void
12637 trace_command (char *arg, int from_tty)
12638 {
12639 if (create_breakpoint (get_current_arch (),
12640 arg,
12641 NULL, 0, 1 /* parse arg */,
12642 0 /* tempflag */,
12643 bp_tracepoint /* type_wanted */,
12644 0 /* Ignore count */,
12645 pending_break_support,
12646 &tracepoint_breakpoint_ops,
12647 from_tty,
12648 1 /* enabled */,
12649 0 /* internal */))
12650 set_tracepoint_count (breakpoint_count);
12651 }
12652
12653 void
12654 ftrace_command (char *arg, int from_tty)
12655 {
12656 if (create_breakpoint (get_current_arch (),
12657 arg,
12658 NULL, 0, 1 /* parse arg */,
12659 0 /* tempflag */,
12660 bp_fast_tracepoint /* type_wanted */,
12661 0 /* Ignore count */,
12662 pending_break_support,
12663 &tracepoint_breakpoint_ops,
12664 from_tty,
12665 1 /* enabled */,
12666 0 /* internal */))
12667 set_tracepoint_count (breakpoint_count);
12668 }
12669
12670 /* strace command implementation. Creates a static tracepoint. */
12671
12672 void
12673 strace_command (char *arg, int from_tty)
12674 {
12675 if (create_breakpoint (get_current_arch (),
12676 arg,
12677 NULL, 0, 1 /* parse arg */,
12678 0 /* tempflag */,
12679 bp_static_tracepoint /* type_wanted */,
12680 0 /* Ignore count */,
12681 pending_break_support,
12682 &tracepoint_breakpoint_ops,
12683 from_tty,
12684 1 /* enabled */,
12685 0 /* internal */))
12686 set_tracepoint_count (breakpoint_count);
12687 }
12688
12689 /* Set up a fake reader function that gets command lines from a linked
12690 list that was acquired during tracepoint uploading. */
12691
12692 static struct uploaded_tp *this_utp;
12693 static int next_cmd;
12694
12695 static char *
12696 read_uploaded_action (void)
12697 {
12698 char *rslt;
12699
12700 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
12701
12702 next_cmd++;
12703
12704 return rslt;
12705 }
12706
12707 /* Given information about a tracepoint as recorded on a target (which
12708 can be either a live system or a trace file), attempt to create an
12709 equivalent GDB tracepoint. This is not a reliable process, since
12710 the target does not necessarily have all the information used when
12711 the tracepoint was originally defined. */
12712
12713 struct tracepoint *
12714 create_tracepoint_from_upload (struct uploaded_tp *utp)
12715 {
12716 char *addr_str, small_buf[100];
12717 struct tracepoint *tp;
12718
12719 if (utp->at_string)
12720 addr_str = utp->at_string;
12721 else
12722 {
12723 /* In the absence of a source location, fall back to raw
12724 address. Since there is no way to confirm that the address
12725 means the same thing as when the trace was started, warn the
12726 user. */
12727 warning (_("Uploaded tracepoint %d has no "
12728 "source location, using raw address"),
12729 utp->number);
12730 sprintf (small_buf, "*%s", hex_string (utp->addr));
12731 addr_str = small_buf;
12732 }
12733
12734 /* There's not much we can do with a sequence of bytecodes. */
12735 if (utp->cond && !utp->cond_string)
12736 warning (_("Uploaded tracepoint %d condition "
12737 "has no source form, ignoring it"),
12738 utp->number);
12739
12740 if (!create_breakpoint (get_current_arch (),
12741 addr_str,
12742 utp->cond_string, -1, 0 /* parse cond/thread */,
12743 0 /* tempflag */,
12744 utp->type /* type_wanted */,
12745 0 /* Ignore count */,
12746 pending_break_support,
12747 &tracepoint_breakpoint_ops,
12748 0 /* from_tty */,
12749 utp->enabled /* enabled */,
12750 0 /* internal */))
12751 return NULL;
12752
12753 set_tracepoint_count (breakpoint_count);
12754
12755 /* Get the tracepoint we just created. */
12756 tp = get_tracepoint (tracepoint_count);
12757 gdb_assert (tp != NULL);
12758
12759 if (utp->pass > 0)
12760 {
12761 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
12762
12763 trace_pass_command (small_buf, 0);
12764 }
12765
12766 /* If we have uploaded versions of the original commands, set up a
12767 special-purpose "reader" function and call the usual command line
12768 reader, then pass the result to the breakpoint command-setting
12769 function. */
12770 if (!VEC_empty (char_ptr, utp->cmd_strings))
12771 {
12772 struct command_line *cmd_list;
12773
12774 this_utp = utp;
12775 next_cmd = 0;
12776
12777 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
12778
12779 breakpoint_set_commands (&tp->base, cmd_list);
12780 }
12781 else if (!VEC_empty (char_ptr, utp->actions)
12782 || !VEC_empty (char_ptr, utp->step_actions))
12783 warning (_("Uploaded tracepoint %d actions "
12784 "have no source form, ignoring them"),
12785 utp->number);
12786
12787 return tp;
12788 }
12789
12790 /* Print information on tracepoint number TPNUM_EXP, or all if
12791 omitted. */
12792
12793 static void
12794 tracepoints_info (char *args, int from_tty)
12795 {
12796 struct ui_out *uiout = current_uiout;
12797 int num_printed;
12798
12799 num_printed = breakpoint_1 (args, 0, is_tracepoint);
12800
12801 if (num_printed == 0)
12802 {
12803 if (args == NULL || *args == '\0')
12804 ui_out_message (uiout, 0, "No tracepoints.\n");
12805 else
12806 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
12807 }
12808
12809 default_collect_info ();
12810 }
12811
12812 /* The 'enable trace' command enables tracepoints.
12813 Not supported by all targets. */
12814 static void
12815 enable_trace_command (char *args, int from_tty)
12816 {
12817 enable_command (args, from_tty);
12818 }
12819
12820 /* The 'disable trace' command disables tracepoints.
12821 Not supported by all targets. */
12822 static void
12823 disable_trace_command (char *args, int from_tty)
12824 {
12825 disable_command (args, from_tty);
12826 }
12827
12828 /* Remove a tracepoint (or all if no argument). */
12829 static void
12830 delete_trace_command (char *arg, int from_tty)
12831 {
12832 struct breakpoint *b, *b_tmp;
12833
12834 dont_repeat ();
12835
12836 if (arg == 0)
12837 {
12838 int breaks_to_delete = 0;
12839
12840 /* Delete all breakpoints if no argument.
12841 Do not delete internal or call-dummy breakpoints, these
12842 have to be deleted with an explicit breakpoint number
12843 argument. */
12844 ALL_TRACEPOINTS (b)
12845 if (is_tracepoint (b) && user_breakpoint_p (b))
12846 {
12847 breaks_to_delete = 1;
12848 break;
12849 }
12850
12851 /* Ask user only if there are some breakpoints to delete. */
12852 if (!from_tty
12853 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
12854 {
12855 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12856 if (is_tracepoint (b) && user_breakpoint_p (b))
12857 delete_breakpoint (b);
12858 }
12859 }
12860 else
12861 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
12862 }
12863
12864 /* Helper function for trace_pass_command. */
12865
12866 static void
12867 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
12868 {
12869 tp->pass_count = count;
12870 observer_notify_tracepoint_modified (tp->base.number);
12871 if (from_tty)
12872 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
12873 tp->base.number, count);
12874 }
12875
12876 /* Set passcount for tracepoint.
12877
12878 First command argument is passcount, second is tracepoint number.
12879 If tracepoint number omitted, apply to most recently defined.
12880 Also accepts special argument "all". */
12881
12882 static void
12883 trace_pass_command (char *args, int from_tty)
12884 {
12885 struct tracepoint *t1;
12886 unsigned int count;
12887
12888 if (args == 0 || *args == 0)
12889 error (_("passcount command requires an "
12890 "argument (count + optional TP num)"));
12891
12892 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
12893
12894 while (*args && isspace ((int) *args))
12895 args++;
12896
12897 if (*args && strncasecmp (args, "all", 3) == 0)
12898 {
12899 struct breakpoint *b;
12900
12901 args += 3; /* Skip special argument "all". */
12902 if (*args)
12903 error (_("Junk at end of arguments."));
12904
12905 ALL_TRACEPOINTS (b)
12906 {
12907 t1 = (struct tracepoint *) b;
12908 trace_pass_set_count (t1, count, from_tty);
12909 }
12910 }
12911 else if (*args == '\0')
12912 {
12913 t1 = get_tracepoint_by_number (&args, NULL, 1);
12914 if (t1)
12915 trace_pass_set_count (t1, count, from_tty);
12916 }
12917 else
12918 {
12919 struct get_number_or_range_state state;
12920
12921 init_number_or_range (&state, args);
12922 while (!state.finished)
12923 {
12924 t1 = get_tracepoint_by_number (&args, &state, 1);
12925 if (t1)
12926 trace_pass_set_count (t1, count, from_tty);
12927 }
12928 }
12929 }
12930
12931 struct tracepoint *
12932 get_tracepoint (int num)
12933 {
12934 struct breakpoint *t;
12935
12936 ALL_TRACEPOINTS (t)
12937 if (t->number == num)
12938 return (struct tracepoint *) t;
12939
12940 return NULL;
12941 }
12942
12943 /* Find the tracepoint with the given target-side number (which may be
12944 different from the tracepoint number after disconnecting and
12945 reconnecting). */
12946
12947 struct tracepoint *
12948 get_tracepoint_by_number_on_target (int num)
12949 {
12950 struct breakpoint *b;
12951
12952 ALL_TRACEPOINTS (b)
12953 {
12954 struct tracepoint *t = (struct tracepoint *) b;
12955
12956 if (t->number_on_target == num)
12957 return t;
12958 }
12959
12960 return NULL;
12961 }
12962
12963 /* Utility: parse a tracepoint number and look it up in the list.
12964 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
12965 If OPTIONAL_P is true, then if the argument is missing, the most
12966 recent tracepoint (tracepoint_count) is returned. */
12967 struct tracepoint *
12968 get_tracepoint_by_number (char **arg,
12969 struct get_number_or_range_state *state,
12970 int optional_p)
12971 {
12972 extern int tracepoint_count;
12973 struct breakpoint *t;
12974 int tpnum;
12975 char *instring = arg == NULL ? NULL : *arg;
12976
12977 if (state)
12978 {
12979 gdb_assert (!state->finished);
12980 tpnum = get_number_or_range (state);
12981 }
12982 else if (arg == NULL || *arg == NULL || ! **arg)
12983 {
12984 if (optional_p)
12985 tpnum = tracepoint_count;
12986 else
12987 error_no_arg (_("tracepoint number"));
12988 }
12989 else
12990 tpnum = get_number (arg);
12991
12992 if (tpnum <= 0)
12993 {
12994 if (instring && *instring)
12995 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
12996 instring);
12997 else
12998 printf_filtered (_("Tracepoint argument missing "
12999 "and no previous tracepoint\n"));
13000 return NULL;
13001 }
13002
13003 ALL_TRACEPOINTS (t)
13004 if (t->number == tpnum)
13005 {
13006 return (struct tracepoint *) t;
13007 }
13008
13009 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
13010 return NULL;
13011 }
13012
13013 void
13014 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
13015 {
13016 if (b->thread != -1)
13017 fprintf_unfiltered (fp, " thread %d", b->thread);
13018
13019 if (b->task != 0)
13020 fprintf_unfiltered (fp, " task %d", b->task);
13021
13022 fprintf_unfiltered (fp, "\n");
13023 }
13024
13025 /* Save information on user settable breakpoints (watchpoints, etc) to
13026 a new script file named FILENAME. If FILTER is non-NULL, call it
13027 on each breakpoint and only include the ones for which it returns
13028 non-zero. */
13029
13030 static void
13031 save_breakpoints (char *filename, int from_tty,
13032 int (*filter) (const struct breakpoint *))
13033 {
13034 struct breakpoint *tp;
13035 int any = 0;
13036 char *pathname;
13037 struct cleanup *cleanup;
13038 struct ui_file *fp;
13039 int extra_trace_bits = 0;
13040
13041 if (filename == 0 || *filename == 0)
13042 error (_("Argument required (file name in which to save)"));
13043
13044 /* See if we have anything to save. */
13045 ALL_BREAKPOINTS (tp)
13046 {
13047 /* Skip internal and momentary breakpoints. */
13048 if (!user_breakpoint_p (tp))
13049 continue;
13050
13051 /* If we have a filter, only save the breakpoints it accepts. */
13052 if (filter && !filter (tp))
13053 continue;
13054
13055 any = 1;
13056
13057 if (is_tracepoint (tp))
13058 {
13059 extra_trace_bits = 1;
13060
13061 /* We can stop searching. */
13062 break;
13063 }
13064 }
13065
13066 if (!any)
13067 {
13068 warning (_("Nothing to save."));
13069 return;
13070 }
13071
13072 pathname = tilde_expand (filename);
13073 cleanup = make_cleanup (xfree, pathname);
13074 fp = gdb_fopen (pathname, "w");
13075 if (!fp)
13076 error (_("Unable to open file '%s' for saving (%s)"),
13077 filename, safe_strerror (errno));
13078 make_cleanup_ui_file_delete (fp);
13079
13080 if (extra_trace_bits)
13081 save_trace_state_variables (fp);
13082
13083 ALL_BREAKPOINTS (tp)
13084 {
13085 /* Skip internal and momentary breakpoints. */
13086 if (!user_breakpoint_p (tp))
13087 continue;
13088
13089 /* If we have a filter, only save the breakpoints it accepts. */
13090 if (filter && !filter (tp))
13091 continue;
13092
13093 tp->ops->print_recreate (tp, fp);
13094
13095 /* Note, we can't rely on tp->number for anything, as we can't
13096 assume the recreated breakpoint numbers will match. Use $bpnum
13097 instead. */
13098
13099 if (tp->cond_string)
13100 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
13101
13102 if (tp->ignore_count)
13103 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
13104
13105 if (tp->commands)
13106 {
13107 volatile struct gdb_exception ex;
13108
13109 fprintf_unfiltered (fp, " commands\n");
13110
13111 ui_out_redirect (current_uiout, fp);
13112 TRY_CATCH (ex, RETURN_MASK_ALL)
13113 {
13114 print_command_lines (current_uiout, tp->commands->commands, 2);
13115 }
13116 ui_out_redirect (current_uiout, NULL);
13117
13118 if (ex.reason < 0)
13119 throw_exception (ex);
13120
13121 fprintf_unfiltered (fp, " end\n");
13122 }
13123
13124 if (tp->enable_state == bp_disabled)
13125 fprintf_unfiltered (fp, "disable\n");
13126
13127 /* If this is a multi-location breakpoint, check if the locations
13128 should be individually disabled. Watchpoint locations are
13129 special, and not user visible. */
13130 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
13131 {
13132 struct bp_location *loc;
13133 int n = 1;
13134
13135 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
13136 if (!loc->enabled)
13137 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
13138 }
13139 }
13140
13141 if (extra_trace_bits && *default_collect)
13142 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
13143
13144 do_cleanups (cleanup);
13145 if (from_tty)
13146 printf_filtered (_("Saved to file '%s'.\n"), filename);
13147 }
13148
13149 /* The `save breakpoints' command. */
13150
13151 static void
13152 save_breakpoints_command (char *args, int from_tty)
13153 {
13154 save_breakpoints (args, from_tty, NULL);
13155 }
13156
13157 /* The `save tracepoints' command. */
13158
13159 static void
13160 save_tracepoints_command (char *args, int from_tty)
13161 {
13162 save_breakpoints (args, from_tty, is_tracepoint);
13163 }
13164
13165 /* Create a vector of all tracepoints. */
13166
13167 VEC(breakpoint_p) *
13168 all_tracepoints (void)
13169 {
13170 VEC(breakpoint_p) *tp_vec = 0;
13171 struct breakpoint *tp;
13172
13173 ALL_TRACEPOINTS (tp)
13174 {
13175 VEC_safe_push (breakpoint_p, tp_vec, tp);
13176 }
13177
13178 return tp_vec;
13179 }
13180
13181 \f
13182 /* This help string is used for the break, hbreak, tbreak and thbreak
13183 commands. It is defined as a macro to prevent duplication.
13184 COMMAND should be a string constant containing the name of the
13185 command. */
13186 #define BREAK_ARGS_HELP(command) \
13187 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
13188 LOCATION may be a line number, function name, or \"*\" and an address.\n\
13189 If a line number is specified, break at start of code for that line.\n\
13190 If a function is specified, break at start of code for that function.\n\
13191 If an address is specified, break at that exact address.\n\
13192 With no LOCATION, uses current execution address of the selected\n\
13193 stack frame. This is useful for breaking on return to a stack frame.\n\
13194 \n\
13195 THREADNUM is the number from \"info threads\".\n\
13196 CONDITION is a boolean expression.\n\
13197 \n\
13198 Multiple breakpoints at one place are permitted, and useful if their\n\
13199 conditions are different.\n\
13200 \n\
13201 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
13202
13203 /* List of subcommands for "catch". */
13204 static struct cmd_list_element *catch_cmdlist;
13205
13206 /* List of subcommands for "tcatch". */
13207 static struct cmd_list_element *tcatch_cmdlist;
13208
13209 void
13210 add_catch_command (char *name, char *docstring,
13211 void (*sfunc) (char *args, int from_tty,
13212 struct cmd_list_element *command),
13213 char **(*completer) (struct cmd_list_element *cmd,
13214 char *text, char *word),
13215 void *user_data_catch,
13216 void *user_data_tcatch)
13217 {
13218 struct cmd_list_element *command;
13219
13220 command = add_cmd (name, class_breakpoint, NULL, docstring,
13221 &catch_cmdlist);
13222 set_cmd_sfunc (command, sfunc);
13223 set_cmd_context (command, user_data_catch);
13224 set_cmd_completer (command, completer);
13225
13226 command = add_cmd (name, class_breakpoint, NULL, docstring,
13227 &tcatch_cmdlist);
13228 set_cmd_sfunc (command, sfunc);
13229 set_cmd_context (command, user_data_tcatch);
13230 set_cmd_completer (command, completer);
13231 }
13232
13233 static void
13234 clear_syscall_counts (struct inferior *inf)
13235 {
13236 inf->total_syscalls_count = 0;
13237 inf->any_syscall_count = 0;
13238 VEC_free (int, inf->syscalls_counts);
13239 }
13240
13241 static void
13242 save_command (char *arg, int from_tty)
13243 {
13244 printf_unfiltered (_("\"save\" must be followed by "
13245 "the name of a save subcommand.\n"));
13246 help_list (save_cmdlist, "save ", -1, gdb_stdout);
13247 }
13248
13249 struct breakpoint *
13250 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
13251 void *data)
13252 {
13253 struct breakpoint *b, *b_tmp;
13254
13255 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13256 {
13257 if ((*callback) (b, data))
13258 return b;
13259 }
13260
13261 return NULL;
13262 }
13263
13264 void
13265 initialize_breakpoint_ops (void)
13266 {
13267 static int initialized = 0;
13268
13269 struct breakpoint_ops *ops;
13270
13271 if (initialized)
13272 return;
13273 initialized = 1;
13274
13275 /* The breakpoint_ops structure to be inherit by all kinds of
13276 breakpoints (real breakpoints, i.e., user "break" breakpoints,
13277 internal and momentary breakpoints, etc.). */
13278 ops = &bkpt_base_breakpoint_ops;
13279 *ops = base_breakpoint_ops;
13280 ops->re_set = bkpt_re_set;
13281 ops->insert_location = bkpt_insert_location;
13282 ops->remove_location = bkpt_remove_location;
13283 ops->breakpoint_hit = bkpt_breakpoint_hit;
13284
13285 /* The breakpoint_ops structure to be used in regular breakpoints. */
13286 ops = &bkpt_breakpoint_ops;
13287 *ops = bkpt_base_breakpoint_ops;
13288 ops->re_set = bkpt_re_set;
13289 ops->resources_needed = bkpt_resources_needed;
13290 ops->print_it = bkpt_print_it;
13291 ops->print_mention = bkpt_print_mention;
13292 ops->print_recreate = bkpt_print_recreate;
13293
13294 /* Ranged breakpoints. */
13295 ops = &ranged_breakpoint_ops;
13296 *ops = bkpt_breakpoint_ops;
13297 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
13298 ops->resources_needed = resources_needed_ranged_breakpoint;
13299 ops->print_it = print_it_ranged_breakpoint;
13300 ops->print_one = print_one_ranged_breakpoint;
13301 ops->print_one_detail = print_one_detail_ranged_breakpoint;
13302 ops->print_mention = print_mention_ranged_breakpoint;
13303 ops->print_recreate = print_recreate_ranged_breakpoint;
13304
13305 /* Internal breakpoints. */
13306 ops = &internal_breakpoint_ops;
13307 *ops = bkpt_base_breakpoint_ops;
13308 ops->re_set = internal_bkpt_re_set;
13309 ops->check_status = internal_bkpt_check_status;
13310 ops->print_it = internal_bkpt_print_it;
13311 ops->print_mention = internal_bkpt_print_mention;
13312
13313 /* Momentary breakpoints. */
13314 ops = &momentary_breakpoint_ops;
13315 *ops = bkpt_base_breakpoint_ops;
13316 ops->re_set = momentary_bkpt_re_set;
13317 ops->check_status = momentary_bkpt_check_status;
13318 ops->print_it = momentary_bkpt_print_it;
13319 ops->print_mention = momentary_bkpt_print_mention;
13320
13321 /* GNU v3 exception catchpoints. */
13322 ops = &gnu_v3_exception_catchpoint_ops;
13323 *ops = bkpt_breakpoint_ops;
13324 ops->print_it = print_it_exception_catchpoint;
13325 ops->print_one = print_one_exception_catchpoint;
13326 ops->print_mention = print_mention_exception_catchpoint;
13327 ops->print_recreate = print_recreate_exception_catchpoint;
13328
13329 /* Watchpoints. */
13330 ops = &watchpoint_breakpoint_ops;
13331 *ops = base_breakpoint_ops;
13332 ops->dtor = dtor_watchpoint;
13333 ops->re_set = re_set_watchpoint;
13334 ops->insert_location = insert_watchpoint;
13335 ops->remove_location = remove_watchpoint;
13336 ops->breakpoint_hit = breakpoint_hit_watchpoint;
13337 ops->check_status = check_status_watchpoint;
13338 ops->resources_needed = resources_needed_watchpoint;
13339 ops->works_in_software_mode = works_in_software_mode_watchpoint;
13340 ops->print_it = print_it_watchpoint;
13341 ops->print_mention = print_mention_watchpoint;
13342 ops->print_recreate = print_recreate_watchpoint;
13343
13344 /* Masked watchpoints. */
13345 ops = &masked_watchpoint_breakpoint_ops;
13346 *ops = watchpoint_breakpoint_ops;
13347 ops->insert_location = insert_masked_watchpoint;
13348 ops->remove_location = remove_masked_watchpoint;
13349 ops->resources_needed = resources_needed_masked_watchpoint;
13350 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
13351 ops->print_it = print_it_masked_watchpoint;
13352 ops->print_one_detail = print_one_detail_masked_watchpoint;
13353 ops->print_mention = print_mention_masked_watchpoint;
13354 ops->print_recreate = print_recreate_masked_watchpoint;
13355
13356 /* Tracepoints. */
13357 ops = &tracepoint_breakpoint_ops;
13358 *ops = base_breakpoint_ops;
13359 ops->re_set = tracepoint_re_set;
13360 ops->breakpoint_hit = tracepoint_breakpoint_hit;
13361 ops->print_one_detail = tracepoint_print_one_detail;
13362 ops->print_mention = tracepoint_print_mention;
13363 ops->print_recreate = tracepoint_print_recreate;
13364
13365 /* Fork catchpoints. */
13366 ops = &catch_fork_breakpoint_ops;
13367 *ops = base_breakpoint_ops;
13368 ops->insert_location = insert_catch_fork;
13369 ops->remove_location = remove_catch_fork;
13370 ops->breakpoint_hit = breakpoint_hit_catch_fork;
13371 ops->print_it = print_it_catch_fork;
13372 ops->print_one = print_one_catch_fork;
13373 ops->print_mention = print_mention_catch_fork;
13374 ops->print_recreate = print_recreate_catch_fork;
13375
13376 /* Vfork catchpoints. */
13377 ops = &catch_vfork_breakpoint_ops;
13378 *ops = base_breakpoint_ops;
13379 ops->insert_location = insert_catch_vfork;
13380 ops->remove_location = remove_catch_vfork;
13381 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
13382 ops->print_it = print_it_catch_vfork;
13383 ops->print_one = print_one_catch_vfork;
13384 ops->print_mention = print_mention_catch_vfork;
13385 ops->print_recreate = print_recreate_catch_vfork;
13386
13387 /* Exec catchpoints. */
13388 ops = &catch_exec_breakpoint_ops;
13389 *ops = base_breakpoint_ops;
13390 ops->dtor = dtor_catch_exec;
13391 ops->insert_location = insert_catch_exec;
13392 ops->remove_location = remove_catch_exec;
13393 ops->breakpoint_hit = breakpoint_hit_catch_exec;
13394 ops->print_it = print_it_catch_exec;
13395 ops->print_one = print_one_catch_exec;
13396 ops->print_mention = print_mention_catch_exec;
13397 ops->print_recreate = print_recreate_catch_exec;
13398
13399 /* Syscall catchpoints. */
13400 ops = &catch_syscall_breakpoint_ops;
13401 *ops = base_breakpoint_ops;
13402 ops->dtor = dtor_catch_syscall;
13403 ops->insert_location = insert_catch_syscall;
13404 ops->remove_location = remove_catch_syscall;
13405 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
13406 ops->print_it = print_it_catch_syscall;
13407 ops->print_one = print_one_catch_syscall;
13408 ops->print_mention = print_mention_catch_syscall;
13409 ops->print_recreate = print_recreate_catch_syscall;
13410 }
13411
13412 void
13413 _initialize_breakpoint (void)
13414 {
13415 struct cmd_list_element *c;
13416
13417 initialize_breakpoint_ops ();
13418
13419 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
13420 observer_attach_inferior_exit (clear_syscall_counts);
13421 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
13422
13423 breakpoint_objfile_key = register_objfile_data ();
13424
13425 breakpoint_chain = 0;
13426 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
13427 before a breakpoint is set. */
13428 breakpoint_count = 0;
13429
13430 tracepoint_count = 0;
13431
13432 add_com ("ignore", class_breakpoint, ignore_command, _("\
13433 Set ignore-count of breakpoint number N to COUNT.\n\
13434 Usage is `ignore N COUNT'."));
13435 if (xdb_commands)
13436 add_com_alias ("bc", "ignore", class_breakpoint, 1);
13437
13438 add_com ("commands", class_breakpoint, commands_command, _("\
13439 Set commands to be executed when a breakpoint is hit.\n\
13440 Give breakpoint number as argument after \"commands\".\n\
13441 With no argument, the targeted breakpoint is the last one set.\n\
13442 The commands themselves follow starting on the next line.\n\
13443 Type a line containing \"end\" to indicate the end of them.\n\
13444 Give \"silent\" as the first line to make the breakpoint silent;\n\
13445 then no output is printed when it is hit, except what the commands print."));
13446
13447 add_com ("condition", class_breakpoint, condition_command, _("\
13448 Specify breakpoint number N to break only if COND is true.\n\
13449 Usage is `condition N COND', where N is an integer and COND is an\n\
13450 expression to be evaluated whenever breakpoint N is reached."));
13451
13452 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
13453 Set a temporary breakpoint.\n\
13454 Like \"break\" except the breakpoint is only temporary,\n\
13455 so it will be deleted when hit. Equivalent to \"break\" followed\n\
13456 by using \"enable delete\" on the breakpoint number.\n\
13457 \n"
13458 BREAK_ARGS_HELP ("tbreak")));
13459 set_cmd_completer (c, location_completer);
13460
13461 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
13462 Set a hardware assisted breakpoint.\n\
13463 Like \"break\" except the breakpoint requires hardware support,\n\
13464 some target hardware may not have this support.\n\
13465 \n"
13466 BREAK_ARGS_HELP ("hbreak")));
13467 set_cmd_completer (c, location_completer);
13468
13469 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
13470 Set a temporary hardware assisted breakpoint.\n\
13471 Like \"hbreak\" except the breakpoint is only temporary,\n\
13472 so it will be deleted when hit.\n\
13473 \n"
13474 BREAK_ARGS_HELP ("thbreak")));
13475 set_cmd_completer (c, location_completer);
13476
13477 add_prefix_cmd ("enable", 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 &enablelist, "enable ", 1, &cmdlist);
13484 if (xdb_commands)
13485 add_com ("ab", class_breakpoint, enable_command, _("\
13486 Enable some breakpoints.\n\
13487 Give breakpoint numbers (separated by spaces) as arguments.\n\
13488 With no subcommand, breakpoints are enabled until you command otherwise.\n\
13489 This is used to cancel the effect of the \"disable\" command.\n\
13490 With a subcommand you can enable temporarily."));
13491
13492 add_com_alias ("en", "enable", class_breakpoint, 1);
13493
13494 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
13495 Enable some breakpoints.\n\
13496 Give breakpoint numbers (separated by spaces) as arguments.\n\
13497 This is used to cancel the effect of the \"disable\" command.\n\
13498 May be abbreviated to simply \"enable\".\n"),
13499 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
13500
13501 add_cmd ("once", no_class, enable_once_command, _("\
13502 Enable breakpoints for one hit. Give breakpoint numbers.\n\
13503 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
13504 &enablebreaklist);
13505
13506 add_cmd ("delete", no_class, enable_delete_command, _("\
13507 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13508 If a breakpoint is hit while enabled in this fashion, it is deleted."),
13509 &enablebreaklist);
13510
13511 add_cmd ("delete", no_class, enable_delete_command, _("\
13512 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
13513 If a breakpoint is hit while enabled in this fashion, it is deleted."),
13514 &enablelist);
13515
13516 add_cmd ("once", no_class, enable_once_command, _("\
13517 Enable breakpoints for one hit. Give breakpoint numbers.\n\
13518 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
13519 &enablelist);
13520
13521 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
13522 Disable some breakpoints.\n\
13523 Arguments are breakpoint numbers with spaces in between.\n\
13524 To disable all breakpoints, give no argument.\n\
13525 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
13526 &disablelist, "disable ", 1, &cmdlist);
13527 add_com_alias ("dis", "disable", class_breakpoint, 1);
13528 add_com_alias ("disa", "disable", class_breakpoint, 1);
13529 if (xdb_commands)
13530 add_com ("sb", class_breakpoint, disable_command, _("\
13531 Disable some breakpoints.\n\
13532 Arguments are breakpoint numbers with spaces in between.\n\
13533 To disable all breakpoints, give no argument.\n\
13534 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
13535
13536 add_cmd ("breakpoints", class_alias, disable_command, _("\
13537 Disable some breakpoints.\n\
13538 Arguments are breakpoint numbers with spaces in between.\n\
13539 To disable all breakpoints, give no argument.\n\
13540 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
13541 This command may be abbreviated \"disable\"."),
13542 &disablelist);
13543
13544 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
13545 Delete some breakpoints or auto-display expressions.\n\
13546 Arguments are breakpoint numbers with spaces in between.\n\
13547 To delete all breakpoints, give no argument.\n\
13548 \n\
13549 Also a prefix command for deletion of other GDB objects.\n\
13550 The \"unset\" command is also an alias for \"delete\"."),
13551 &deletelist, "delete ", 1, &cmdlist);
13552 add_com_alias ("d", "delete", class_breakpoint, 1);
13553 add_com_alias ("del", "delete", class_breakpoint, 1);
13554 if (xdb_commands)
13555 add_com ("db", class_breakpoint, delete_command, _("\
13556 Delete some breakpoints.\n\
13557 Arguments are breakpoint numbers with spaces in between.\n\
13558 To delete all breakpoints, give no argument.\n"));
13559
13560 add_cmd ("breakpoints", class_alias, delete_command, _("\
13561 Delete some breakpoints or auto-display expressions.\n\
13562 Arguments are breakpoint numbers with spaces in between.\n\
13563 To delete all breakpoints, give no argument.\n\
13564 This command may be abbreviated \"delete\"."),
13565 &deletelist);
13566
13567 add_com ("clear", class_breakpoint, clear_command, _("\
13568 Clear breakpoint at specified line or function.\n\
13569 Argument may be line number, function name, or \"*\" and an address.\n\
13570 If line number is specified, all breakpoints in that line are cleared.\n\
13571 If function is specified, breakpoints at beginning of function are cleared.\n\
13572 If an address is specified, breakpoints at that address are cleared.\n\
13573 \n\
13574 With no argument, clears all breakpoints in the line that the selected frame\n\
13575 is executing in.\n\
13576 \n\
13577 See also the \"delete\" command which clears breakpoints by number."));
13578 add_com_alias ("cl", "clear", class_breakpoint, 1);
13579
13580 c = add_com ("break", class_breakpoint, break_command, _("\
13581 Set breakpoint at specified line or function.\n"
13582 BREAK_ARGS_HELP ("break")));
13583 set_cmd_completer (c, location_completer);
13584
13585 add_com_alias ("b", "break", class_run, 1);
13586 add_com_alias ("br", "break", class_run, 1);
13587 add_com_alias ("bre", "break", class_run, 1);
13588 add_com_alias ("brea", "break", class_run, 1);
13589
13590 if (xdb_commands)
13591 add_com_alias ("ba", "break", class_breakpoint, 1);
13592
13593 if (dbx_commands)
13594 {
13595 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
13596 Break in function/address or break at a line in the current file."),
13597 &stoplist, "stop ", 1, &cmdlist);
13598 add_cmd ("in", class_breakpoint, stopin_command,
13599 _("Break in function or address."), &stoplist);
13600 add_cmd ("at", class_breakpoint, stopat_command,
13601 _("Break at a line in the current file."), &stoplist);
13602 add_com ("status", class_info, breakpoints_info, _("\
13603 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
13604 The \"Type\" column indicates one of:\n\
13605 \tbreakpoint - normal breakpoint\n\
13606 \twatchpoint - watchpoint\n\
13607 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13608 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13609 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
13610 address and file/line number respectively.\n\
13611 \n\
13612 Convenience variable \"$_\" and default examine address for \"x\"\n\
13613 are set to the address of the last breakpoint listed unless the command\n\
13614 is prefixed with \"server \".\n\n\
13615 Convenience variable \"$bpnum\" contains the number of the last\n\
13616 breakpoint set."));
13617 }
13618
13619 add_info ("breakpoints", breakpoints_info, _("\
13620 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
13621 The \"Type\" column indicates one of:\n\
13622 \tbreakpoint - normal breakpoint\n\
13623 \twatchpoint - watchpoint\n\
13624 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13625 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13626 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
13627 address and file/line number respectively.\n\
13628 \n\
13629 Convenience variable \"$_\" and default examine address for \"x\"\n\
13630 are set to the address of the last breakpoint listed unless the command\n\
13631 is prefixed with \"server \".\n\n\
13632 Convenience variable \"$bpnum\" contains the number of the last\n\
13633 breakpoint set."));
13634
13635 add_info_alias ("b", "breakpoints", 1);
13636
13637 if (xdb_commands)
13638 add_com ("lb", class_breakpoint, breakpoints_info, _("\
13639 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
13640 The \"Type\" column indicates one of:\n\
13641 \tbreakpoint - normal breakpoint\n\
13642 \twatchpoint - watchpoint\n\
13643 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13644 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13645 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
13646 address and file/line number respectively.\n\
13647 \n\
13648 Convenience variable \"$_\" and default examine address for \"x\"\n\
13649 are set to the address of the last breakpoint listed unless the command\n\
13650 is prefixed with \"server \".\n\n\
13651 Convenience variable \"$bpnum\" contains the number of the last\n\
13652 breakpoint set."));
13653
13654 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
13655 Status of all breakpoints, or breakpoint number NUMBER.\n\
13656 The \"Type\" column indicates one of:\n\
13657 \tbreakpoint - normal breakpoint\n\
13658 \twatchpoint - watchpoint\n\
13659 \tlongjmp - internal breakpoint used to step through longjmp()\n\
13660 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
13661 \tuntil - internal breakpoint used by the \"until\" command\n\
13662 \tfinish - internal breakpoint used by the \"finish\" command\n\
13663 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
13664 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
13665 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
13666 address and file/line number respectively.\n\
13667 \n\
13668 Convenience variable \"$_\" and default examine address for \"x\"\n\
13669 are set to the address of the last breakpoint listed unless the command\n\
13670 is prefixed with \"server \".\n\n\
13671 Convenience variable \"$bpnum\" contains the number of the last\n\
13672 breakpoint set."),
13673 &maintenanceinfolist);
13674
13675 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
13676 Set catchpoints to catch events."),
13677 &catch_cmdlist, "catch ",
13678 0/*allow-unknown*/, &cmdlist);
13679
13680 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
13681 Set temporary catchpoints to catch events."),
13682 &tcatch_cmdlist, "tcatch ",
13683 0/*allow-unknown*/, &cmdlist);
13684
13685 /* Add catch and tcatch sub-commands. */
13686 add_catch_command ("catch", _("\
13687 Catch an exception, when caught.\n\
13688 With an argument, catch only exceptions with the given name."),
13689 catch_catch_command,
13690 NULL,
13691 CATCH_PERMANENT,
13692 CATCH_TEMPORARY);
13693 add_catch_command ("throw", _("\
13694 Catch an exception, when thrown.\n\
13695 With an argument, catch only exceptions with the given name."),
13696 catch_throw_command,
13697 NULL,
13698 CATCH_PERMANENT,
13699 CATCH_TEMPORARY);
13700 add_catch_command ("fork", _("Catch calls to fork."),
13701 catch_fork_command_1,
13702 NULL,
13703 (void *) (uintptr_t) catch_fork_permanent,
13704 (void *) (uintptr_t) catch_fork_temporary);
13705 add_catch_command ("vfork", _("Catch calls to vfork."),
13706 catch_fork_command_1,
13707 NULL,
13708 (void *) (uintptr_t) catch_vfork_permanent,
13709 (void *) (uintptr_t) catch_vfork_temporary);
13710 add_catch_command ("exec", _("Catch calls to exec."),
13711 catch_exec_command_1,
13712 NULL,
13713 CATCH_PERMANENT,
13714 CATCH_TEMPORARY);
13715 add_catch_command ("syscall", _("\
13716 Catch system calls by their names and/or numbers.\n\
13717 Arguments say which system calls to catch. If no arguments\n\
13718 are given, every system call will be caught.\n\
13719 Arguments, if given, should be one or more system call names\n\
13720 (if your system supports that), or system call numbers."),
13721 catch_syscall_command_1,
13722 catch_syscall_completer,
13723 CATCH_PERMANENT,
13724 CATCH_TEMPORARY);
13725
13726 c = add_com ("watch", class_breakpoint, watch_command, _("\
13727 Set a watchpoint for an expression.\n\
13728 Usage: watch [-l|-location] EXPRESSION\n\
13729 A watchpoint stops execution of your program whenever the value of\n\
13730 an expression changes.\n\
13731 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13732 the memory to which it refers."));
13733 set_cmd_completer (c, expression_completer);
13734
13735 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
13736 Set a read watchpoint for an expression.\n\
13737 Usage: rwatch [-l|-location] EXPRESSION\n\
13738 A watchpoint stops execution of your program whenever the value of\n\
13739 an expression is read.\n\
13740 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13741 the memory to which it refers."));
13742 set_cmd_completer (c, expression_completer);
13743
13744 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
13745 Set a watchpoint for an expression.\n\
13746 Usage: awatch [-l|-location] EXPRESSION\n\
13747 A watchpoint stops execution of your program whenever the value of\n\
13748 an expression is either read or written.\n\
13749 If -l or -location is given, this evaluates EXPRESSION and watches\n\
13750 the memory to which it refers."));
13751 set_cmd_completer (c, expression_completer);
13752
13753 add_info ("watchpoints", watchpoints_info, _("\
13754 Status of specified watchpoints (all watchpoints if no argument)."));
13755
13756 /* XXX: cagney/2005-02-23: This should be a boolean, and should
13757 respond to changes - contrary to the description. */
13758 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
13759 &can_use_hw_watchpoints, _("\
13760 Set debugger's willingness to use watchpoint hardware."), _("\
13761 Show debugger's willingness to use watchpoint hardware."), _("\
13762 If zero, gdb will not use hardware for new watchpoints, even if\n\
13763 such is available. (However, any hardware watchpoints that were\n\
13764 created before setting this to nonzero, will continue to use watchpoint\n\
13765 hardware.)"),
13766 NULL,
13767 show_can_use_hw_watchpoints,
13768 &setlist, &showlist);
13769
13770 can_use_hw_watchpoints = 1;
13771
13772 /* Tracepoint manipulation commands. */
13773
13774 c = add_com ("trace", class_breakpoint, trace_command, _("\
13775 Set a tracepoint at specified line or function.\n\
13776 \n"
13777 BREAK_ARGS_HELP ("trace") "\n\
13778 Do \"help tracepoints\" for info on other tracepoint commands."));
13779 set_cmd_completer (c, location_completer);
13780
13781 add_com_alias ("tp", "trace", class_alias, 0);
13782 add_com_alias ("tr", "trace", class_alias, 1);
13783 add_com_alias ("tra", "trace", class_alias, 1);
13784 add_com_alias ("trac", "trace", class_alias, 1);
13785
13786 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
13787 Set a fast tracepoint at specified line or function.\n\
13788 \n"
13789 BREAK_ARGS_HELP ("ftrace") "\n\
13790 Do \"help tracepoints\" for info on other tracepoint commands."));
13791 set_cmd_completer (c, location_completer);
13792
13793 c = add_com ("strace", class_breakpoint, strace_command, _("\
13794 Set a static tracepoint at specified line, function or marker.\n\
13795 \n\
13796 strace [LOCATION] [if CONDITION]\n\
13797 LOCATION may be a line number, function name, \"*\" and an address,\n\
13798 or -m MARKER_ID.\n\
13799 If a line number is specified, probe the marker at start of code\n\
13800 for that line. If a function is specified, probe the marker at start\n\
13801 of code for that function. If an address is specified, probe the marker\n\
13802 at that exact address. If a marker id is specified, probe the marker\n\
13803 with that name. With no LOCATION, uses current execution address of\n\
13804 the selected stack frame.\n\
13805 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
13806 This collects arbitrary user data passed in the probe point call to the\n\
13807 tracing library. You can inspect it when analyzing the trace buffer,\n\
13808 by printing the $_sdata variable like any other convenience variable.\n\
13809 \n\
13810 CONDITION is a boolean expression.\n\
13811 \n\
13812 Multiple tracepoints at one place are permitted, and useful if their\n\
13813 conditions are different.\n\
13814 \n\
13815 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
13816 Do \"help tracepoints\" for info on other tracepoint commands."));
13817 set_cmd_completer (c, location_completer);
13818
13819 add_info ("tracepoints", tracepoints_info, _("\
13820 Status of specified tracepoints (all tracepoints if no argument).\n\
13821 Convenience variable \"$tpnum\" contains the number of the\n\
13822 last tracepoint set."));
13823
13824 add_info_alias ("tp", "tracepoints", 1);
13825
13826 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
13827 Delete specified tracepoints.\n\
13828 Arguments are tracepoint numbers, separated by spaces.\n\
13829 No argument means delete all tracepoints."),
13830 &deletelist);
13831
13832 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
13833 Disable specified tracepoints.\n\
13834 Arguments are tracepoint numbers, separated by spaces.\n\
13835 No argument means disable all tracepoints."),
13836 &disablelist);
13837 deprecate_cmd (c, "disable");
13838
13839 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
13840 Enable specified tracepoints.\n\
13841 Arguments are tracepoint numbers, separated by spaces.\n\
13842 No argument means enable all tracepoints."),
13843 &enablelist);
13844 deprecate_cmd (c, "enable");
13845
13846 add_com ("passcount", class_trace, trace_pass_command, _("\
13847 Set the passcount for a tracepoint.\n\
13848 The trace will end when the tracepoint has been passed 'count' times.\n\
13849 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
13850 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
13851
13852 add_prefix_cmd ("save", class_breakpoint, save_command,
13853 _("Save breakpoint definitions as a script."),
13854 &save_cmdlist, "save ",
13855 0/*allow-unknown*/, &cmdlist);
13856
13857 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
13858 Save current breakpoint definitions as a script.\n\
13859 This includes all types of breakpoints (breakpoints, watchpoints,\n\
13860 catchpoints, tracepoints). Use the 'source' command in another debug\n\
13861 session to restore them."),
13862 &save_cmdlist);
13863 set_cmd_completer (c, filename_completer);
13864
13865 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
13866 Save current tracepoint definitions as a script.\n\
13867 Use the 'source' command in another debug session to restore them."),
13868 &save_cmdlist);
13869 set_cmd_completer (c, filename_completer);
13870
13871 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
13872 deprecate_cmd (c, "save tracepoints");
13873
13874 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
13875 Breakpoint specific settings\n\
13876 Configure various breakpoint-specific variables such as\n\
13877 pending breakpoint behavior"),
13878 &breakpoint_set_cmdlist, "set breakpoint ",
13879 0/*allow-unknown*/, &setlist);
13880 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
13881 Breakpoint specific settings\n\
13882 Configure various breakpoint-specific variables such as\n\
13883 pending breakpoint behavior"),
13884 &breakpoint_show_cmdlist, "show breakpoint ",
13885 0/*allow-unknown*/, &showlist);
13886
13887 add_setshow_auto_boolean_cmd ("pending", no_class,
13888 &pending_break_support, _("\
13889 Set debugger's behavior regarding pending breakpoints."), _("\
13890 Show debugger's behavior regarding pending breakpoints."), _("\
13891 If on, an unrecognized breakpoint location will cause gdb to create a\n\
13892 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
13893 an error. If auto, an unrecognized breakpoint location results in a\n\
13894 user-query to see if a pending breakpoint should be created."),
13895 NULL,
13896 show_pending_break_support,
13897 &breakpoint_set_cmdlist,
13898 &breakpoint_show_cmdlist);
13899
13900 pending_break_support = AUTO_BOOLEAN_AUTO;
13901
13902 add_setshow_boolean_cmd ("auto-hw", no_class,
13903 &automatic_hardware_breakpoints, _("\
13904 Set automatic usage of hardware breakpoints."), _("\
13905 Show automatic usage of hardware breakpoints."), _("\
13906 If set, the debugger will automatically use hardware breakpoints for\n\
13907 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
13908 a warning will be emitted for such breakpoints."),
13909 NULL,
13910 show_automatic_hardware_breakpoints,
13911 &breakpoint_set_cmdlist,
13912 &breakpoint_show_cmdlist);
13913
13914 add_setshow_enum_cmd ("always-inserted", class_support,
13915 always_inserted_enums, &always_inserted_mode, _("\
13916 Set mode for inserting breakpoints."), _("\
13917 Show mode for inserting breakpoints."), _("\
13918 When this mode is off, breakpoints are inserted in inferior when it is\n\
13919 resumed, and removed when execution stops. When this mode is on,\n\
13920 breakpoints are inserted immediately and removed only when the user\n\
13921 deletes the breakpoint. When this mode is auto (which is the default),\n\
13922 the behaviour depends on the non-stop setting (see help set non-stop).\n\
13923 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
13924 behaves as if always-inserted mode is on; if gdb is controlling the\n\
13925 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
13926 NULL,
13927 &show_always_inserted_mode,
13928 &breakpoint_set_cmdlist,
13929 &breakpoint_show_cmdlist);
13930
13931 add_com ("break-range", class_breakpoint, break_range_command, _("\
13932 Set a breakpoint for an address range.\n\
13933 break-range START-LOCATION, END-LOCATION\n\
13934 where START-LOCATION and END-LOCATION can be one of the following:\n\
13935 LINENUM, for that line in the current file,\n\
13936 FILE:LINENUM, for that line in that file,\n\
13937 +OFFSET, for that number of lines after the current line\n\
13938 or the start of the range\n\
13939 FUNCTION, for the first line in that function,\n\
13940 FILE:FUNCTION, to distinguish among like-named static functions.\n\
13941 *ADDRESS, for the instruction at that address.\n\
13942 \n\
13943 The breakpoint will stop execution of the inferior whenever it executes\n\
13944 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
13945 range (including START-LOCATION and END-LOCATION)."));
13946
13947 automatic_hardware_breakpoints = 1;
13948
13949 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
13950 }
This page took 0.396287 seconds and 4 git commands to generate.