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