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