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