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