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