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