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