Rewrite non-continuable watchpoints handling
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "xml-syscall.h"
60 #include "parser-defs.h"
61 #include "gdb_regex.h"
62 #include "probe.h"
63 #include "cli/cli-utils.h"
64 #include "continuations.h"
65 #include "stack.h"
66 #include "skip.h"
67 #include "ax-gdb.h"
68 #include "dummy-frame.h"
69
70 #include "format.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 "extension.h"
81
82 /* Enums for exception-handling support. */
83 enum exception_event_kind
84 {
85 EX_EVENT_THROW,
86 EX_EVENT_RETHROW,
87 EX_EVENT_CATCH
88 };
89
90 /* Prototypes for local functions. */
91
92 static void enable_delete_command (char *, int);
93
94 static void enable_once_command (char *, int);
95
96 static void enable_count_command (char *, int);
97
98 static void disable_command (char *, int);
99
100 static void enable_command (char *, int);
101
102 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
105
106 static void ignore_command (char *, int);
107
108 static int breakpoint_re_set_one (void *);
109
110 static void breakpoint_re_set_default (struct breakpoint *);
111
112 static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117 static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
119 char *, char *, enum bptype,
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
123 int, int, int, unsigned);
124
125 static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
128 static void clear_command (char *, int);
129
130 static void catch_command (char *, int);
131
132 static int can_use_hardware_watchpoint (struct value *);
133
134 static void break_command_1 (char *, int, int);
135
136 static void mention (struct breakpoint *);
137
138 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
140 const struct breakpoint_ops *);
141 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
144 /* This function is used in gdbtk sources and thus can not be made
145 static. */
146 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
147 struct symtab_and_line,
148 enum bptype,
149 const struct breakpoint_ops *);
150
151 static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
156
157 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
159 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
161 enum bptype bptype);
162
163 static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
165 struct obj_section *, int);
166
167 static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
170 static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
174 static void breakpoints_info (char *, int);
175
176 static void watchpoints_info (char *, int);
177
178 static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
180
181 static int breakpoint_cond_eval (void *);
182
183 static void cleanup_executing_breakpoints (void *);
184
185 static void commands_command (char *, int);
186
187 static void condition_command (char *, int);
188
189 typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194 insertion_state_t;
195
196 static int remove_breakpoint (struct bp_location *, insertion_state_t);
197 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
198
199 static enum print_stop_action print_bp_stop_message (bpstat bs);
200
201 static int watchpoint_check (void *);
202
203 static void maintenance_info_breakpoints (char *, int);
204
205 static int hw_breakpoint_used_count (void);
206
207 static int hw_watchpoint_use_count (struct breakpoint *);
208
209 static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
212
213 static void hbreak_command (char *, int);
214
215 static void thbreak_command (char *, int);
216
217 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
219
220 static void stop_command (char *arg, int from_tty);
221
222 static void stopin_command (char *arg, int from_tty);
223
224 static void stopat_command (char *arg, int from_tty);
225
226 static void tcatch_command (char *arg, int from_tty);
227
228 static void detach_single_step_breakpoints (void);
229
230 static int find_single_step_breakpoint (struct address_space *aspace,
231 CORE_ADDR pc);
232
233 static void free_bp_location (struct bp_location *loc);
234 static void incref_bp_location (struct bp_location *loc);
235 static void decref_bp_location (struct bp_location **loc);
236
237 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
238
239 /* update_global_location_list's modes of operation wrt to whether to
240 insert locations now. */
241 enum ugll_insert_mode
242 {
243 /* Don't insert any breakpoint locations into the inferior, only
244 remove already-inserted locations that no longer should be
245 inserted. Functions that delete a breakpoint or breakpoints
246 should specify this mode, so that deleting a breakpoint doesn't
247 have the side effect of inserting the locations of other
248 breakpoints that are marked not-inserted, but should_be_inserted
249 returns true on them.
250
251 This behavior is useful is situations close to tear-down -- e.g.,
252 after an exec, while the target still has execution, but
253 breakpoint shadows of the previous executable image should *NOT*
254 be restored to the new image; or before detaching, where the
255 target still has execution and wants to delete breakpoints from
256 GDB's lists, and all breakpoints had already been removed from
257 the inferior. */
258 UGLL_DONT_INSERT,
259
260 /* May insert breakpoints iff breakpoints_should_be_inserted_now
261 claims breakpoints should be inserted now. */
262 UGLL_MAY_INSERT,
263
264 /* Insert locations now, irrespective of
265 breakpoints_should_be_inserted_now. E.g., say all threads are
266 stopped right now, and the user did "continue". We need to
267 insert breakpoints _before_ resuming the target, but
268 UGLL_MAY_INSERT wouldn't insert them, because
269 breakpoints_should_be_inserted_now returns false at that point,
270 as no thread is running yet. */
271 UGLL_INSERT
272 };
273
274 static void update_global_location_list (enum ugll_insert_mode);
275
276 static void update_global_location_list_nothrow (enum ugll_insert_mode);
277
278 static int is_hardware_watchpoint (const struct breakpoint *bpt);
279
280 static void insert_breakpoint_locations (void);
281
282 static int syscall_catchpoint_p (struct breakpoint *b);
283
284 static void tracepoints_info (char *, int);
285
286 static void delete_trace_command (char *, int);
287
288 static void enable_trace_command (char *, int);
289
290 static void disable_trace_command (char *, int);
291
292 static void trace_pass_command (char *, int);
293
294 static void set_tracepoint_count (int num);
295
296 static int is_masked_watchpoint (const struct breakpoint *b);
297
298 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
299
300 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
301 otherwise. */
302
303 static int strace_marker_p (struct breakpoint *b);
304
305 /* The abstract base class all breakpoint_ops structures inherit
306 from. */
307 struct breakpoint_ops base_breakpoint_ops;
308
309 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
310 that are implemented on top of software or hardware breakpoints
311 (user breakpoints, internal and momentary breakpoints, etc.). */
312 static struct breakpoint_ops bkpt_base_breakpoint_ops;
313
314 /* Internal breakpoints class type. */
315 static struct breakpoint_ops internal_breakpoint_ops;
316
317 /* Momentary breakpoints class type. */
318 static struct breakpoint_ops momentary_breakpoint_ops;
319
320 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
321 static struct breakpoint_ops longjmp_breakpoint_ops;
322
323 /* The breakpoint_ops structure to be used in regular user created
324 breakpoints. */
325 struct breakpoint_ops bkpt_breakpoint_ops;
326
327 /* Breakpoints set on probes. */
328 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
329
330 /* Dynamic printf class type. */
331 struct breakpoint_ops dprintf_breakpoint_ops;
332
333 /* One (or perhaps two) breakpoints used for software single
334 stepping. */
335
336 static void *single_step_breakpoints[2];
337 static struct gdbarch *single_step_gdbarch[2];
338
339 /* The style in which to perform a dynamic printf. This is a user
340 option because different output options have different tradeoffs;
341 if GDB does the printing, there is better error handling if there
342 is a problem with any of the arguments, but using an inferior
343 function lets you have special-purpose printers and sending of
344 output to the same place as compiled-in print functions. */
345
346 static const char dprintf_style_gdb[] = "gdb";
347 static const char dprintf_style_call[] = "call";
348 static const char dprintf_style_agent[] = "agent";
349 static const char *const dprintf_style_enums[] = {
350 dprintf_style_gdb,
351 dprintf_style_call,
352 dprintf_style_agent,
353 NULL
354 };
355 static const char *dprintf_style = dprintf_style_gdb;
356
357 /* The function to use for dynamic printf if the preferred style is to
358 call into the inferior. The value is simply a string that is
359 copied into the command, so it can be anything that GDB can
360 evaluate to a callable address, not necessarily a function name. */
361
362 static char *dprintf_function = "";
363
364 /* The channel to use for dynamic printf if the preferred style is to
365 call into the inferior; if a nonempty string, it will be passed to
366 the call as the first argument, with the format string as the
367 second. As with the dprintf function, this can be anything that
368 GDB knows how to evaluate, so in addition to common choices like
369 "stderr", this could be an app-specific expression like
370 "mystreams[curlogger]". */
371
372 static char *dprintf_channel = "";
373
374 /* True if dprintf commands should continue to operate even if GDB
375 has disconnected. */
376 static int disconnected_dprintf = 1;
377
378 /* A reference-counted struct command_line. This lets multiple
379 breakpoints share a single command list. */
380 struct counted_command_line
381 {
382 /* The reference count. */
383 int refc;
384
385 /* The command list. */
386 struct command_line *commands;
387 };
388
389 struct command_line *
390 breakpoint_commands (struct breakpoint *b)
391 {
392 return b->commands ? b->commands->commands : NULL;
393 }
394
395 /* Flag indicating that a command has proceeded the inferior past the
396 current breakpoint. */
397
398 static int breakpoint_proceeded;
399
400 const char *
401 bpdisp_text (enum bpdisp disp)
402 {
403 /* NOTE: the following values are a part of MI protocol and
404 represent values of 'disp' field returned when inferior stops at
405 a breakpoint. */
406 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
407
408 return bpdisps[(int) disp];
409 }
410
411 /* Prototypes for exported functions. */
412 /* If FALSE, gdb will not use hardware support for watchpoints, even
413 if such is available. */
414 static int can_use_hw_watchpoints;
415
416 static void
417 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420 {
421 fprintf_filtered (file,
422 _("Debugger's willingness to use "
423 "watchpoint hardware is %s.\n"),
424 value);
425 }
426
427 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
428 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
429 for unrecognized breakpoint locations.
430 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
431 static enum auto_boolean pending_break_support;
432 static void
433 show_pending_break_support (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c,
435 const char *value)
436 {
437 fprintf_filtered (file,
438 _("Debugger's behavior regarding "
439 "pending breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
444 set with "break" but falling in read-only memory.
445 If 0, gdb will warn about such breakpoints, but won't automatically
446 use hardware breakpoints. */
447 static int automatic_hardware_breakpoints;
448 static void
449 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
450 struct cmd_list_element *c,
451 const char *value)
452 {
453 fprintf_filtered (file,
454 _("Automatic usage of hardware breakpoints is %s.\n"),
455 value);
456 }
457
458 /* If on, GDB keeps breakpoints inserted even if the inferior is
459 stopped, and immediately inserts any new breakpoints as soon as
460 they're created. If off (default), GDB keeps breakpoints off of
461 the target as long as possible. That is, it delays inserting
462 breakpoints until the next resume, and removes them again when the
463 target fully stops. This is a bit safer in case GDB crashes while
464 processing user input. */
465 static int always_inserted_mode = 0;
466
467 static void
468 show_always_inserted_mode (struct ui_file *file, int from_tty,
469 struct cmd_list_element *c, const char *value)
470 {
471 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
472 value);
473 }
474
475 /* See breakpoint.h. */
476
477 int
478 breakpoints_should_be_inserted_now (void)
479 {
480 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
481 {
482 /* If breakpoints are global, they should be inserted even if no
483 thread under gdb's control is running, or even if there are
484 no threads under GDB's control yet. */
485 return 1;
486 }
487 else if (target_has_execution)
488 {
489 if (always_inserted_mode)
490 {
491 /* The user wants breakpoints inserted even if all threads
492 are stopped. */
493 return 1;
494 }
495
496 if (threads_are_executing ())
497 return 1;
498 }
499 return 0;
500 }
501
502 static const char condition_evaluation_both[] = "host or target";
503
504 /* Modes for breakpoint condition evaluation. */
505 static const char condition_evaluation_auto[] = "auto";
506 static const char condition_evaluation_host[] = "host";
507 static const char condition_evaluation_target[] = "target";
508 static const char *const condition_evaluation_enums[] = {
509 condition_evaluation_auto,
510 condition_evaluation_host,
511 condition_evaluation_target,
512 NULL
513 };
514
515 /* Global that holds the current mode for breakpoint condition evaluation. */
516 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
517
518 /* Global that we use to display information to the user (gets its value from
519 condition_evaluation_mode_1. */
520 static const char *condition_evaluation_mode = condition_evaluation_auto;
521
522 /* Translate a condition evaluation mode MODE into either "host"
523 or "target". This is used mostly to translate from "auto" to the
524 real setting that is being used. It returns the translated
525 evaluation mode. */
526
527 static const char *
528 translate_condition_evaluation_mode (const char *mode)
529 {
530 if (mode == condition_evaluation_auto)
531 {
532 if (target_supports_evaluation_of_breakpoint_conditions ())
533 return condition_evaluation_target;
534 else
535 return condition_evaluation_host;
536 }
537 else
538 return mode;
539 }
540
541 /* Discovers what condition_evaluation_auto translates to. */
542
543 static const char *
544 breakpoint_condition_evaluation_mode (void)
545 {
546 return translate_condition_evaluation_mode (condition_evaluation_mode);
547 }
548
549 /* Return true if GDB should evaluate breakpoint conditions or false
550 otherwise. */
551
552 static int
553 gdb_evaluates_breakpoint_condition_p (void)
554 {
555 const char *mode = breakpoint_condition_evaluation_mode ();
556
557 return (mode == condition_evaluation_host);
558 }
559
560 void _initialize_breakpoint (void);
561
562 /* Are we executing breakpoint commands? */
563 static int executing_breakpoint_commands;
564
565 /* Are overlay event breakpoints enabled? */
566 static int overlay_events_enabled;
567
568 /* See description in breakpoint.h. */
569 int target_exact_watchpoints = 0;
570
571 /* Walk the following statement or block through all breakpoints.
572 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
573 current breakpoint. */
574
575 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
576
577 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
578 for (B = breakpoint_chain; \
579 B ? (TMP=B->next, 1): 0; \
580 B = TMP)
581
582 /* Similar iterator for the low-level breakpoints. SAFE variant is
583 not provided so update_global_location_list must not be called
584 while executing the block of ALL_BP_LOCATIONS. */
585
586 #define ALL_BP_LOCATIONS(B,BP_TMP) \
587 for (BP_TMP = bp_location; \
588 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
589 BP_TMP++)
590
591 /* Iterates through locations with address ADDRESS for the currently selected
592 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
593 to where the loop should start from.
594 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
595 appropriate location to start with. */
596
597 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
598 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
599 BP_LOCP_TMP = BP_LOCP_START; \
600 BP_LOCP_START \
601 && (BP_LOCP_TMP < bp_location + bp_location_count \
602 && (*BP_LOCP_TMP)->address == ADDRESS); \
603 BP_LOCP_TMP++)
604
605 /* Iterator for tracepoints only. */
606
607 #define ALL_TRACEPOINTS(B) \
608 for (B = breakpoint_chain; B; B = B->next) \
609 if (is_tracepoint (B))
610
611 /* Chains of all breakpoints defined. */
612
613 struct breakpoint *breakpoint_chain;
614
615 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
616
617 static struct bp_location **bp_location;
618
619 /* Number of elements of BP_LOCATION. */
620
621 static unsigned bp_location_count;
622
623 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
624 ADDRESS for the current elements of BP_LOCATION which get a valid
625 result from bp_location_has_shadow. You can use it for roughly
626 limiting the subrange of BP_LOCATION to scan for shadow bytes for
627 an address you need to read. */
628
629 static CORE_ADDR bp_location_placed_address_before_address_max;
630
631 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
632 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
633 BP_LOCATION which get a valid result from bp_location_has_shadow.
634 You can use it for roughly limiting the subrange of BP_LOCATION to
635 scan for shadow bytes for an address you need to read. */
636
637 static CORE_ADDR bp_location_shadow_len_after_address_max;
638
639 /* The locations that no longer correspond to any breakpoint, unlinked
640 from bp_location array, but for which a hit may still be reported
641 by a target. */
642 VEC(bp_location_p) *moribund_locations = NULL;
643
644 /* Number of last breakpoint made. */
645
646 static int breakpoint_count;
647
648 /* The value of `breakpoint_count' before the last command that
649 created breakpoints. If the last (break-like) command created more
650 than one breakpoint, then the difference between BREAKPOINT_COUNT
651 and PREV_BREAKPOINT_COUNT is more than one. */
652 static int prev_breakpoint_count;
653
654 /* Number of last tracepoint made. */
655
656 static int tracepoint_count;
657
658 static struct cmd_list_element *breakpoint_set_cmdlist;
659 static struct cmd_list_element *breakpoint_show_cmdlist;
660 struct cmd_list_element *save_cmdlist;
661
662 /* Return whether a breakpoint is an active enabled breakpoint. */
663 static int
664 breakpoint_enabled (struct breakpoint *b)
665 {
666 return (b->enable_state == bp_enabled);
667 }
668
669 /* Set breakpoint count to NUM. */
670
671 static void
672 set_breakpoint_count (int num)
673 {
674 prev_breakpoint_count = breakpoint_count;
675 breakpoint_count = num;
676 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
677 }
678
679 /* Used by `start_rbreak_breakpoints' below, to record the current
680 breakpoint count before "rbreak" creates any breakpoint. */
681 static int rbreak_start_breakpoint_count;
682
683 /* Called at the start an "rbreak" command to record the first
684 breakpoint made. */
685
686 void
687 start_rbreak_breakpoints (void)
688 {
689 rbreak_start_breakpoint_count = breakpoint_count;
690 }
691
692 /* Called at the end of an "rbreak" command to record the last
693 breakpoint made. */
694
695 void
696 end_rbreak_breakpoints (void)
697 {
698 prev_breakpoint_count = rbreak_start_breakpoint_count;
699 }
700
701 /* Used in run_command to zero the hit count when a new run starts. */
702
703 void
704 clear_breakpoint_hit_counts (void)
705 {
706 struct breakpoint *b;
707
708 ALL_BREAKPOINTS (b)
709 b->hit_count = 0;
710 }
711
712 /* Allocate a new counted_command_line with reference count of 1.
713 The new structure owns COMMANDS. */
714
715 static struct counted_command_line *
716 alloc_counted_command_line (struct command_line *commands)
717 {
718 struct counted_command_line *result
719 = xmalloc (sizeof (struct counted_command_line));
720
721 result->refc = 1;
722 result->commands = commands;
723 return result;
724 }
725
726 /* Increment reference count. This does nothing if CMD is NULL. */
727
728 static void
729 incref_counted_command_line (struct counted_command_line *cmd)
730 {
731 if (cmd)
732 ++cmd->refc;
733 }
734
735 /* Decrement reference count. If the reference count reaches 0,
736 destroy the counted_command_line. Sets *CMDP to NULL. This does
737 nothing if *CMDP is NULL. */
738
739 static void
740 decref_counted_command_line (struct counted_command_line **cmdp)
741 {
742 if (*cmdp)
743 {
744 if (--(*cmdp)->refc == 0)
745 {
746 free_command_lines (&(*cmdp)->commands);
747 xfree (*cmdp);
748 }
749 *cmdp = NULL;
750 }
751 }
752
753 /* A cleanup function that calls decref_counted_command_line. */
754
755 static void
756 do_cleanup_counted_command_line (void *arg)
757 {
758 decref_counted_command_line (arg);
759 }
760
761 /* Create a cleanup that calls decref_counted_command_line on the
762 argument. */
763
764 static struct cleanup *
765 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
766 {
767 return make_cleanup (do_cleanup_counted_command_line, cmdp);
768 }
769
770 \f
771 /* Return the breakpoint with the specified number, or NULL
772 if the number does not refer to an existing breakpoint. */
773
774 struct breakpoint *
775 get_breakpoint (int num)
776 {
777 struct breakpoint *b;
778
779 ALL_BREAKPOINTS (b)
780 if (b->number == num)
781 return b;
782
783 return NULL;
784 }
785
786 \f
787
788 /* Mark locations as "conditions have changed" in case the target supports
789 evaluating conditions on its side. */
790
791 static void
792 mark_breakpoint_modified (struct breakpoint *b)
793 {
794 struct bp_location *loc;
795
796 /* This is only meaningful if the target is
797 evaluating conditions and if the user has
798 opted for condition evaluation on the target's
799 side. */
800 if (gdb_evaluates_breakpoint_condition_p ()
801 || !target_supports_evaluation_of_breakpoint_conditions ())
802 return;
803
804 if (!is_breakpoint (b))
805 return;
806
807 for (loc = b->loc; loc; loc = loc->next)
808 loc->condition_changed = condition_modified;
809 }
810
811 /* Mark location as "conditions have changed" in case the target supports
812 evaluating conditions on its side. */
813
814 static void
815 mark_breakpoint_location_modified (struct bp_location *loc)
816 {
817 /* This is only meaningful if the target is
818 evaluating conditions and if the user has
819 opted for condition evaluation on the target's
820 side. */
821 if (gdb_evaluates_breakpoint_condition_p ()
822 || !target_supports_evaluation_of_breakpoint_conditions ())
823
824 return;
825
826 if (!is_breakpoint (loc->owner))
827 return;
828
829 loc->condition_changed = condition_modified;
830 }
831
832 /* Sets the condition-evaluation mode using the static global
833 condition_evaluation_mode. */
834
835 static void
836 set_condition_evaluation_mode (char *args, int from_tty,
837 struct cmd_list_element *c)
838 {
839 const char *old_mode, *new_mode;
840
841 if ((condition_evaluation_mode_1 == condition_evaluation_target)
842 && !target_supports_evaluation_of_breakpoint_conditions ())
843 {
844 condition_evaluation_mode_1 = condition_evaluation_mode;
845 warning (_("Target does not support breakpoint condition evaluation.\n"
846 "Using host evaluation mode instead."));
847 return;
848 }
849
850 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
851 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
852
853 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
854 settings was "auto". */
855 condition_evaluation_mode = condition_evaluation_mode_1;
856
857 /* Only update the mode if the user picked a different one. */
858 if (new_mode != old_mode)
859 {
860 struct bp_location *loc, **loc_tmp;
861 /* If the user switched to a different evaluation mode, we
862 need to synch the changes with the target as follows:
863
864 "host" -> "target": Send all (valid) conditions to the target.
865 "target" -> "host": Remove all the conditions from the target.
866 */
867
868 if (new_mode == condition_evaluation_target)
869 {
870 /* Mark everything modified and synch conditions with the
871 target. */
872 ALL_BP_LOCATIONS (loc, loc_tmp)
873 mark_breakpoint_location_modified (loc);
874 }
875 else
876 {
877 /* Manually mark non-duplicate locations to synch conditions
878 with the target. We do this to remove all the conditions the
879 target knows about. */
880 ALL_BP_LOCATIONS (loc, loc_tmp)
881 if (is_breakpoint (loc->owner) && loc->inserted)
882 loc->needs_update = 1;
883 }
884
885 /* Do the update. */
886 update_global_location_list (UGLL_MAY_INSERT);
887 }
888
889 return;
890 }
891
892 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
893 what "auto" is translating to. */
894
895 static void
896 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
897 struct cmd_list_element *c, const char *value)
898 {
899 if (condition_evaluation_mode == condition_evaluation_auto)
900 fprintf_filtered (file,
901 _("Breakpoint condition evaluation "
902 "mode is %s (currently %s).\n"),
903 value,
904 breakpoint_condition_evaluation_mode ());
905 else
906 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
907 value);
908 }
909
910 /* A comparison function for bp_location AP and BP that is used by
911 bsearch. This comparison function only cares about addresses, unlike
912 the more general bp_location_compare function. */
913
914 static int
915 bp_location_compare_addrs (const void *ap, const void *bp)
916 {
917 struct bp_location *a = *(void **) ap;
918 struct bp_location *b = *(void **) bp;
919
920 if (a->address == b->address)
921 return 0;
922 else
923 return ((a->address > b->address) - (a->address < b->address));
924 }
925
926 /* Helper function to skip all bp_locations with addresses
927 less than ADDRESS. It returns the first bp_location that
928 is greater than or equal to ADDRESS. If none is found, just
929 return NULL. */
930
931 static struct bp_location **
932 get_first_locp_gte_addr (CORE_ADDR address)
933 {
934 struct bp_location dummy_loc;
935 struct bp_location *dummy_locp = &dummy_loc;
936 struct bp_location **locp_found = NULL;
937
938 /* Initialize the dummy location's address field. */
939 memset (&dummy_loc, 0, sizeof (struct bp_location));
940 dummy_loc.address = address;
941
942 /* Find a close match to the first location at ADDRESS. */
943 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
944 sizeof (struct bp_location **),
945 bp_location_compare_addrs);
946
947 /* Nothing was found, nothing left to do. */
948 if (locp_found == NULL)
949 return NULL;
950
951 /* We may have found a location that is at ADDRESS but is not the first in the
952 location's list. Go backwards (if possible) and locate the first one. */
953 while ((locp_found - 1) >= bp_location
954 && (*(locp_found - 1))->address == address)
955 locp_found--;
956
957 return locp_found;
958 }
959
960 void
961 set_breakpoint_condition (struct breakpoint *b, char *exp,
962 int from_tty)
963 {
964 xfree (b->cond_string);
965 b->cond_string = NULL;
966
967 if (is_watchpoint (b))
968 {
969 struct watchpoint *w = (struct watchpoint *) b;
970
971 xfree (w->cond_exp);
972 w->cond_exp = NULL;
973 }
974 else
975 {
976 struct bp_location *loc;
977
978 for (loc = b->loc; loc; loc = loc->next)
979 {
980 xfree (loc->cond);
981 loc->cond = NULL;
982
983 /* No need to free the condition agent expression
984 bytecode (if we have one). We will handle this
985 when we go through update_global_location_list. */
986 }
987 }
988
989 if (*exp == 0)
990 {
991 if (from_tty)
992 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
993 }
994 else
995 {
996 const char *arg = exp;
997
998 /* I don't know if it matters whether this is the string the user
999 typed in or the decompiled expression. */
1000 b->cond_string = xstrdup (arg);
1001 b->condition_not_parsed = 0;
1002
1003 if (is_watchpoint (b))
1004 {
1005 struct watchpoint *w = (struct watchpoint *) b;
1006
1007 innermost_block = NULL;
1008 arg = exp;
1009 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1010 if (*arg)
1011 error (_("Junk at end of expression"));
1012 w->cond_exp_valid_block = innermost_block;
1013 }
1014 else
1015 {
1016 struct bp_location *loc;
1017
1018 for (loc = b->loc; loc; loc = loc->next)
1019 {
1020 arg = exp;
1021 loc->cond =
1022 parse_exp_1 (&arg, loc->address,
1023 block_for_pc (loc->address), 0);
1024 if (*arg)
1025 error (_("Junk at end of expression"));
1026 }
1027 }
1028 }
1029 mark_breakpoint_modified (b);
1030
1031 observer_notify_breakpoint_modified (b);
1032 }
1033
1034 /* Completion for the "condition" command. */
1035
1036 static VEC (char_ptr) *
1037 condition_completer (struct cmd_list_element *cmd,
1038 const char *text, const char *word)
1039 {
1040 const char *space;
1041
1042 text = skip_spaces_const (text);
1043 space = skip_to_space_const (text);
1044 if (*space == '\0')
1045 {
1046 int len;
1047 struct breakpoint *b;
1048 VEC (char_ptr) *result = NULL;
1049
1050 if (text[0] == '$')
1051 {
1052 /* We don't support completion of history indices. */
1053 if (isdigit (text[1]))
1054 return NULL;
1055 return complete_internalvar (&text[1]);
1056 }
1057
1058 /* We're completing the breakpoint number. */
1059 len = strlen (text);
1060
1061 ALL_BREAKPOINTS (b)
1062 {
1063 char number[50];
1064
1065 xsnprintf (number, sizeof (number), "%d", b->number);
1066
1067 if (strncmp (number, text, len) == 0)
1068 VEC_safe_push (char_ptr, result, xstrdup (number));
1069 }
1070
1071 return result;
1072 }
1073
1074 /* We're completing the expression part. */
1075 text = skip_spaces_const (space);
1076 return expression_completer (cmd, text, word);
1077 }
1078
1079 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1080
1081 static void
1082 condition_command (char *arg, int from_tty)
1083 {
1084 struct breakpoint *b;
1085 char *p;
1086 int bnum;
1087
1088 if (arg == 0)
1089 error_no_arg (_("breakpoint number"));
1090
1091 p = arg;
1092 bnum = get_number (&p);
1093 if (bnum == 0)
1094 error (_("Bad breakpoint argument: '%s'"), arg);
1095
1096 ALL_BREAKPOINTS (b)
1097 if (b->number == bnum)
1098 {
1099 /* Check if this breakpoint has a "stop" method implemented in an
1100 extension language. This method and conditions entered into GDB
1101 from the CLI are mutually exclusive. */
1102 const struct extension_language_defn *extlang
1103 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1104
1105 if (extlang != NULL)
1106 {
1107 error (_("Only one stop condition allowed. There is currently"
1108 " a %s stop condition defined for this breakpoint."),
1109 ext_lang_capitalized_name (extlang));
1110 }
1111 set_breakpoint_condition (b, p, from_tty);
1112
1113 if (is_breakpoint (b))
1114 update_global_location_list (UGLL_MAY_INSERT);
1115
1116 return;
1117 }
1118
1119 error (_("No breakpoint number %d."), bnum);
1120 }
1121
1122 /* Check that COMMAND do not contain commands that are suitable
1123 only for tracepoints and not suitable for ordinary breakpoints.
1124 Throw if any such commands is found. */
1125
1126 static void
1127 check_no_tracepoint_commands (struct command_line *commands)
1128 {
1129 struct command_line *c;
1130
1131 for (c = commands; c; c = c->next)
1132 {
1133 int i;
1134
1135 if (c->control_type == while_stepping_control)
1136 error (_("The 'while-stepping' command can "
1137 "only be used for tracepoints"));
1138
1139 for (i = 0; i < c->body_count; ++i)
1140 check_no_tracepoint_commands ((c->body_list)[i]);
1141
1142 /* Not that command parsing removes leading whitespace and comment
1143 lines and also empty lines. So, we only need to check for
1144 command directly. */
1145 if (strstr (c->line, "collect ") == c->line)
1146 error (_("The 'collect' command can only be used for tracepoints"));
1147
1148 if (strstr (c->line, "teval ") == c->line)
1149 error (_("The 'teval' command can only be used for tracepoints"));
1150 }
1151 }
1152
1153 /* Encapsulate tests for different types of tracepoints. */
1154
1155 static int
1156 is_tracepoint_type (enum bptype type)
1157 {
1158 return (type == bp_tracepoint
1159 || type == bp_fast_tracepoint
1160 || type == bp_static_tracepoint);
1161 }
1162
1163 int
1164 is_tracepoint (const struct breakpoint *b)
1165 {
1166 return is_tracepoint_type (b->type);
1167 }
1168
1169 /* A helper function that validates that COMMANDS are valid for a
1170 breakpoint. This function will throw an exception if a problem is
1171 found. */
1172
1173 static void
1174 validate_commands_for_breakpoint (struct breakpoint *b,
1175 struct command_line *commands)
1176 {
1177 if (is_tracepoint (b))
1178 {
1179 struct tracepoint *t = (struct tracepoint *) b;
1180 struct command_line *c;
1181 struct command_line *while_stepping = 0;
1182
1183 /* Reset the while-stepping step count. The previous commands
1184 might have included a while-stepping action, while the new
1185 ones might not. */
1186 t->step_count = 0;
1187
1188 /* We need to verify that each top-level element of commands is
1189 valid for tracepoints, that there's at most one
1190 while-stepping element, and that the while-stepping's body
1191 has valid tracing commands excluding nested while-stepping.
1192 We also need to validate the tracepoint action line in the
1193 context of the tracepoint --- validate_actionline actually
1194 has side effects, like setting the tracepoint's
1195 while-stepping STEP_COUNT, in addition to checking if the
1196 collect/teval actions parse and make sense in the
1197 tracepoint's context. */
1198 for (c = commands; c; c = c->next)
1199 {
1200 if (c->control_type == while_stepping_control)
1201 {
1202 if (b->type == bp_fast_tracepoint)
1203 error (_("The 'while-stepping' command "
1204 "cannot be used for fast tracepoint"));
1205 else if (b->type == bp_static_tracepoint)
1206 error (_("The 'while-stepping' command "
1207 "cannot be used for static tracepoint"));
1208
1209 if (while_stepping)
1210 error (_("The 'while-stepping' command "
1211 "can be used only once"));
1212 else
1213 while_stepping = c;
1214 }
1215
1216 validate_actionline (c->line, b);
1217 }
1218 if (while_stepping)
1219 {
1220 struct command_line *c2;
1221
1222 gdb_assert (while_stepping->body_count == 1);
1223 c2 = while_stepping->body_list[0];
1224 for (; c2; c2 = c2->next)
1225 {
1226 if (c2->control_type == while_stepping_control)
1227 error (_("The 'while-stepping' command cannot be nested"));
1228 }
1229 }
1230 }
1231 else
1232 {
1233 check_no_tracepoint_commands (commands);
1234 }
1235 }
1236
1237 /* Return a vector of all the static tracepoints set at ADDR. The
1238 caller is responsible for releasing the vector. */
1239
1240 VEC(breakpoint_p) *
1241 static_tracepoints_here (CORE_ADDR addr)
1242 {
1243 struct breakpoint *b;
1244 VEC(breakpoint_p) *found = 0;
1245 struct bp_location *loc;
1246
1247 ALL_BREAKPOINTS (b)
1248 if (b->type == bp_static_tracepoint)
1249 {
1250 for (loc = b->loc; loc; loc = loc->next)
1251 if (loc->address == addr)
1252 VEC_safe_push(breakpoint_p, found, b);
1253 }
1254
1255 return found;
1256 }
1257
1258 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1259 validate that only allowed commands are included. */
1260
1261 void
1262 breakpoint_set_commands (struct breakpoint *b,
1263 struct command_line *commands)
1264 {
1265 validate_commands_for_breakpoint (b, commands);
1266
1267 decref_counted_command_line (&b->commands);
1268 b->commands = alloc_counted_command_line (commands);
1269 observer_notify_breakpoint_modified (b);
1270 }
1271
1272 /* Set the internal `silent' flag on the breakpoint. Note that this
1273 is not the same as the "silent" that may appear in the breakpoint's
1274 commands. */
1275
1276 void
1277 breakpoint_set_silent (struct breakpoint *b, int silent)
1278 {
1279 int old_silent = b->silent;
1280
1281 b->silent = silent;
1282 if (old_silent != silent)
1283 observer_notify_breakpoint_modified (b);
1284 }
1285
1286 /* Set the thread for this breakpoint. If THREAD is -1, make the
1287 breakpoint work for any thread. */
1288
1289 void
1290 breakpoint_set_thread (struct breakpoint *b, int thread)
1291 {
1292 int old_thread = b->thread;
1293
1294 b->thread = thread;
1295 if (old_thread != thread)
1296 observer_notify_breakpoint_modified (b);
1297 }
1298
1299 /* Set the task for this breakpoint. If TASK is 0, make the
1300 breakpoint work for any task. */
1301
1302 void
1303 breakpoint_set_task (struct breakpoint *b, int task)
1304 {
1305 int old_task = b->task;
1306
1307 b->task = task;
1308 if (old_task != task)
1309 observer_notify_breakpoint_modified (b);
1310 }
1311
1312 void
1313 check_tracepoint_command (char *line, void *closure)
1314 {
1315 struct breakpoint *b = closure;
1316
1317 validate_actionline (line, b);
1318 }
1319
1320 /* A structure used to pass information through
1321 map_breakpoint_numbers. */
1322
1323 struct commands_info
1324 {
1325 /* True if the command was typed at a tty. */
1326 int from_tty;
1327
1328 /* The breakpoint range spec. */
1329 char *arg;
1330
1331 /* Non-NULL if the body of the commands are being read from this
1332 already-parsed command. */
1333 struct command_line *control;
1334
1335 /* The command lines read from the user, or NULL if they have not
1336 yet been read. */
1337 struct counted_command_line *cmd;
1338 };
1339
1340 /* A callback for map_breakpoint_numbers that sets the commands for
1341 commands_command. */
1342
1343 static void
1344 do_map_commands_command (struct breakpoint *b, void *data)
1345 {
1346 struct commands_info *info = data;
1347
1348 if (info->cmd == NULL)
1349 {
1350 struct command_line *l;
1351
1352 if (info->control != NULL)
1353 l = copy_command_lines (info->control->body_list[0]);
1354 else
1355 {
1356 struct cleanup *old_chain;
1357 char *str;
1358
1359 str = xstrprintf (_("Type commands for breakpoint(s) "
1360 "%s, one per line."),
1361 info->arg);
1362
1363 old_chain = make_cleanup (xfree, str);
1364
1365 l = read_command_lines (str,
1366 info->from_tty, 1,
1367 (is_tracepoint (b)
1368 ? check_tracepoint_command : 0),
1369 b);
1370
1371 do_cleanups (old_chain);
1372 }
1373
1374 info->cmd = alloc_counted_command_line (l);
1375 }
1376
1377 /* If a breakpoint was on the list more than once, we don't need to
1378 do anything. */
1379 if (b->commands != info->cmd)
1380 {
1381 validate_commands_for_breakpoint (b, info->cmd->commands);
1382 incref_counted_command_line (info->cmd);
1383 decref_counted_command_line (&b->commands);
1384 b->commands = info->cmd;
1385 observer_notify_breakpoint_modified (b);
1386 }
1387 }
1388
1389 static void
1390 commands_command_1 (char *arg, int from_tty,
1391 struct command_line *control)
1392 {
1393 struct cleanup *cleanups;
1394 struct commands_info info;
1395
1396 info.from_tty = from_tty;
1397 info.control = control;
1398 info.cmd = NULL;
1399 /* If we read command lines from the user, then `info' will hold an
1400 extra reference to the commands that we must clean up. */
1401 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1402
1403 if (arg == NULL || !*arg)
1404 {
1405 if (breakpoint_count - prev_breakpoint_count > 1)
1406 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1407 breakpoint_count);
1408 else if (breakpoint_count > 0)
1409 arg = xstrprintf ("%d", breakpoint_count);
1410 else
1411 {
1412 /* So that we don't try to free the incoming non-NULL
1413 argument in the cleanup below. Mapping breakpoint
1414 numbers will fail in this case. */
1415 arg = NULL;
1416 }
1417 }
1418 else
1419 /* The command loop has some static state, so we need to preserve
1420 our argument. */
1421 arg = xstrdup (arg);
1422
1423 if (arg != NULL)
1424 make_cleanup (xfree, arg);
1425
1426 info.arg = arg;
1427
1428 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1429
1430 if (info.cmd == NULL)
1431 error (_("No breakpoints specified."));
1432
1433 do_cleanups (cleanups);
1434 }
1435
1436 static void
1437 commands_command (char *arg, int from_tty)
1438 {
1439 commands_command_1 (arg, from_tty, NULL);
1440 }
1441
1442 /* Like commands_command, but instead of reading the commands from
1443 input stream, takes them from an already parsed command structure.
1444
1445 This is used by cli-script.c to DTRT with breakpoint commands
1446 that are part of if and while bodies. */
1447 enum command_control_type
1448 commands_from_control_command (char *arg, struct command_line *cmd)
1449 {
1450 commands_command_1 (arg, 0, cmd);
1451 return simple_control;
1452 }
1453
1454 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1455
1456 static int
1457 bp_location_has_shadow (struct bp_location *bl)
1458 {
1459 if (bl->loc_type != bp_loc_software_breakpoint)
1460 return 0;
1461 if (!bl->inserted)
1462 return 0;
1463 if (bl->target_info.shadow_len == 0)
1464 /* BL isn't valid, or doesn't shadow memory. */
1465 return 0;
1466 return 1;
1467 }
1468
1469 /* Update BUF, which is LEN bytes read from the target address
1470 MEMADDR, by replacing a memory breakpoint with its shadowed
1471 contents.
1472
1473 If READBUF is not NULL, this buffer must not overlap with the of
1474 the breakpoint location's shadow_contents buffer. Otherwise, a
1475 failed assertion internal error will be raised. */
1476
1477 static void
1478 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1479 const gdb_byte *writebuf_org,
1480 ULONGEST memaddr, LONGEST len,
1481 struct bp_target_info *target_info,
1482 struct gdbarch *gdbarch)
1483 {
1484 /* Now do full processing of the found relevant range of elements. */
1485 CORE_ADDR bp_addr = 0;
1486 int bp_size = 0;
1487 int bptoffset = 0;
1488
1489 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1490 current_program_space->aspace, 0))
1491 {
1492 /* The breakpoint is inserted in a different address space. */
1493 return;
1494 }
1495
1496 /* Addresses and length of the part of the breakpoint that
1497 we need to copy. */
1498 bp_addr = target_info->placed_address;
1499 bp_size = target_info->shadow_len;
1500
1501 if (bp_addr + bp_size <= memaddr)
1502 {
1503 /* The breakpoint is entirely before the chunk of memory we are
1504 reading. */
1505 return;
1506 }
1507
1508 if (bp_addr >= memaddr + len)
1509 {
1510 /* The breakpoint is entirely after the chunk of memory we are
1511 reading. */
1512 return;
1513 }
1514
1515 /* Offset within shadow_contents. */
1516 if (bp_addr < memaddr)
1517 {
1518 /* Only copy the second part of the breakpoint. */
1519 bp_size -= memaddr - bp_addr;
1520 bptoffset = memaddr - bp_addr;
1521 bp_addr = memaddr;
1522 }
1523
1524 if (bp_addr + bp_size > memaddr + len)
1525 {
1526 /* Only copy the first part of the breakpoint. */
1527 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1528 }
1529
1530 if (readbuf != NULL)
1531 {
1532 /* Verify that the readbuf buffer does not overlap with the
1533 shadow_contents buffer. */
1534 gdb_assert (target_info->shadow_contents >= readbuf + len
1535 || readbuf >= (target_info->shadow_contents
1536 + target_info->shadow_len));
1537
1538 /* Update the read buffer with this inserted breakpoint's
1539 shadow. */
1540 memcpy (readbuf + bp_addr - memaddr,
1541 target_info->shadow_contents + bptoffset, bp_size);
1542 }
1543 else
1544 {
1545 const unsigned char *bp;
1546 CORE_ADDR addr = target_info->reqstd_address;
1547 int placed_size;
1548
1549 /* Update the shadow with what we want to write to memory. */
1550 memcpy (target_info->shadow_contents + bptoffset,
1551 writebuf_org + bp_addr - memaddr, bp_size);
1552
1553 /* Determine appropriate breakpoint contents and size for this
1554 address. */
1555 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1556
1557 /* Update the final write buffer with this inserted
1558 breakpoint's INSN. */
1559 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1560 }
1561 }
1562
1563 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1564 by replacing any memory breakpoints with their shadowed contents.
1565
1566 If READBUF is not NULL, this buffer must not overlap with any of
1567 the breakpoint location's shadow_contents buffers. Otherwise,
1568 a failed assertion internal error will be raised.
1569
1570 The range of shadowed area by each bp_location is:
1571 bl->address - bp_location_placed_address_before_address_max
1572 up to bl->address + bp_location_shadow_len_after_address_max
1573 The range we were requested to resolve shadows for is:
1574 memaddr ... memaddr + len
1575 Thus the safe cutoff boundaries for performance optimization are
1576 memaddr + len <= (bl->address
1577 - bp_location_placed_address_before_address_max)
1578 and:
1579 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1580
1581 void
1582 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1583 const gdb_byte *writebuf_org,
1584 ULONGEST memaddr, LONGEST len)
1585 {
1586 /* Left boundary, right boundary and median element of our binary
1587 search. */
1588 unsigned bc_l, bc_r, bc;
1589 size_t i;
1590
1591 /* Find BC_L which is a leftmost element which may affect BUF
1592 content. It is safe to report lower value but a failure to
1593 report higher one. */
1594
1595 bc_l = 0;
1596 bc_r = bp_location_count;
1597 while (bc_l + 1 < bc_r)
1598 {
1599 struct bp_location *bl;
1600
1601 bc = (bc_l + bc_r) / 2;
1602 bl = bp_location[bc];
1603
1604 /* Check first BL->ADDRESS will not overflow due to the added
1605 constant. Then advance the left boundary only if we are sure
1606 the BC element can in no way affect the BUF content (MEMADDR
1607 to MEMADDR + LEN range).
1608
1609 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1610 offset so that we cannot miss a breakpoint with its shadow
1611 range tail still reaching MEMADDR. */
1612
1613 if ((bl->address + bp_location_shadow_len_after_address_max
1614 >= bl->address)
1615 && (bl->address + bp_location_shadow_len_after_address_max
1616 <= memaddr))
1617 bc_l = bc;
1618 else
1619 bc_r = bc;
1620 }
1621
1622 /* Due to the binary search above, we need to make sure we pick the
1623 first location that's at BC_L's address. E.g., if there are
1624 multiple locations at the same address, BC_L may end up pointing
1625 at a duplicate location, and miss the "master"/"inserted"
1626 location. Say, given locations L1, L2 and L3 at addresses A and
1627 B:
1628
1629 L1@A, L2@A, L3@B, ...
1630
1631 BC_L could end up pointing at location L2, while the "master"
1632 location could be L1. Since the `loc->inserted' flag is only set
1633 on "master" locations, we'd forget to restore the shadow of L1
1634 and L2. */
1635 while (bc_l > 0
1636 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1637 bc_l--;
1638
1639 /* Now do full processing of the found relevant range of elements. */
1640
1641 for (bc = bc_l; bc < bp_location_count; bc++)
1642 {
1643 struct bp_location *bl = bp_location[bc];
1644 CORE_ADDR bp_addr = 0;
1645 int bp_size = 0;
1646 int bptoffset = 0;
1647
1648 /* bp_location array has BL->OWNER always non-NULL. */
1649 if (bl->owner->type == bp_none)
1650 warning (_("reading through apparently deleted breakpoint #%d?"),
1651 bl->owner->number);
1652
1653 /* Performance optimization: any further element can no longer affect BUF
1654 content. */
1655
1656 if (bl->address >= bp_location_placed_address_before_address_max
1657 && memaddr + len <= (bl->address
1658 - bp_location_placed_address_before_address_max))
1659 break;
1660
1661 if (!bp_location_has_shadow (bl))
1662 continue;
1663
1664 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1665 memaddr, len, &bl->target_info, bl->gdbarch);
1666 }
1667
1668 /* Now process single-step breakpoints. These are not found in the
1669 bp_location array. */
1670 for (i = 0; i < 2; i++)
1671 {
1672 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
1673
1674 if (bp_tgt != NULL)
1675 {
1676 struct gdbarch *gdbarch = single_step_gdbarch[i];
1677
1678 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1679 memaddr, len, bp_tgt, gdbarch);
1680 }
1681 }
1682 }
1683
1684 \f
1685
1686 /* Return true if BPT is either a software breakpoint or a hardware
1687 breakpoint. */
1688
1689 int
1690 is_breakpoint (const struct breakpoint *bpt)
1691 {
1692 return (bpt->type == bp_breakpoint
1693 || bpt->type == bp_hardware_breakpoint
1694 || bpt->type == bp_dprintf);
1695 }
1696
1697 /* Return true if BPT is of any hardware watchpoint kind. */
1698
1699 static int
1700 is_hardware_watchpoint (const struct breakpoint *bpt)
1701 {
1702 return (bpt->type == bp_hardware_watchpoint
1703 || bpt->type == bp_read_watchpoint
1704 || bpt->type == bp_access_watchpoint);
1705 }
1706
1707 /* Return true if BPT is of any watchpoint kind, hardware or
1708 software. */
1709
1710 int
1711 is_watchpoint (const struct breakpoint *bpt)
1712 {
1713 return (is_hardware_watchpoint (bpt)
1714 || bpt->type == bp_watchpoint);
1715 }
1716
1717 /* Returns true if the current thread and its running state are safe
1718 to evaluate or update watchpoint B. Watchpoints on local
1719 expressions need to be evaluated in the context of the thread that
1720 was current when the watchpoint was created, and, that thread needs
1721 to be stopped to be able to select the correct frame context.
1722 Watchpoints on global expressions can be evaluated on any thread,
1723 and in any state. It is presently left to the target allowing
1724 memory accesses when threads are running. */
1725
1726 static int
1727 watchpoint_in_thread_scope (struct watchpoint *b)
1728 {
1729 return (b->base.pspace == current_program_space
1730 && (ptid_equal (b->watchpoint_thread, null_ptid)
1731 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1732 && !is_executing (inferior_ptid))));
1733 }
1734
1735 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1736 associated bp_watchpoint_scope breakpoint. */
1737
1738 static void
1739 watchpoint_del_at_next_stop (struct watchpoint *w)
1740 {
1741 struct breakpoint *b = &w->base;
1742
1743 if (b->related_breakpoint != b)
1744 {
1745 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1746 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1747 b->related_breakpoint->disposition = disp_del_at_next_stop;
1748 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1749 b->related_breakpoint = b;
1750 }
1751 b->disposition = disp_del_at_next_stop;
1752 }
1753
1754 /* Extract a bitfield value from value VAL using the bit parameters contained in
1755 watchpoint W. */
1756
1757 static struct value *
1758 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1759 {
1760 struct value *bit_val;
1761
1762 if (val == NULL)
1763 return NULL;
1764
1765 bit_val = allocate_value (value_type (val));
1766
1767 unpack_value_bitfield (bit_val,
1768 w->val_bitpos,
1769 w->val_bitsize,
1770 value_contents_for_printing (val),
1771 value_offset (val),
1772 val);
1773
1774 return bit_val;
1775 }
1776
1777 /* Assuming that B is a watchpoint:
1778 - Reparse watchpoint expression, if REPARSE is non-zero
1779 - Evaluate expression and store the result in B->val
1780 - Evaluate the condition if there is one, and store the result
1781 in b->loc->cond.
1782 - Update the list of values that must be watched in B->loc.
1783
1784 If the watchpoint disposition is disp_del_at_next_stop, then do
1785 nothing. If this is local watchpoint that is out of scope, delete
1786 it.
1787
1788 Even with `set breakpoint always-inserted on' the watchpoints are
1789 removed + inserted on each stop here. Normal breakpoints must
1790 never be removed because they might be missed by a running thread
1791 when debugging in non-stop mode. On the other hand, hardware
1792 watchpoints (is_hardware_watchpoint; processed here) are specific
1793 to each LWP since they are stored in each LWP's hardware debug
1794 registers. Therefore, such LWP must be stopped first in order to
1795 be able to modify its hardware watchpoints.
1796
1797 Hardware watchpoints must be reset exactly once after being
1798 presented to the user. It cannot be done sooner, because it would
1799 reset the data used to present the watchpoint hit to the user. And
1800 it must not be done later because it could display the same single
1801 watchpoint hit during multiple GDB stops. Note that the latter is
1802 relevant only to the hardware watchpoint types bp_read_watchpoint
1803 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1804 not user-visible - its hit is suppressed if the memory content has
1805 not changed.
1806
1807 The following constraints influence the location where we can reset
1808 hardware watchpoints:
1809
1810 * target_stopped_by_watchpoint and target_stopped_data_address are
1811 called several times when GDB stops.
1812
1813 [linux]
1814 * Multiple hardware watchpoints can be hit at the same time,
1815 causing GDB to stop. GDB only presents one hardware watchpoint
1816 hit at a time as the reason for stopping, and all the other hits
1817 are presented later, one after the other, each time the user
1818 requests the execution to be resumed. Execution is not resumed
1819 for the threads still having pending hit event stored in
1820 LWP_INFO->STATUS. While the watchpoint is already removed from
1821 the inferior on the first stop the thread hit event is kept being
1822 reported from its cached value by linux_nat_stopped_data_address
1823 until the real thread resume happens after the watchpoint gets
1824 presented and thus its LWP_INFO->STATUS gets reset.
1825
1826 Therefore the hardware watchpoint hit can get safely reset on the
1827 watchpoint removal from inferior. */
1828
1829 static void
1830 update_watchpoint (struct watchpoint *b, int reparse)
1831 {
1832 int within_current_scope;
1833 struct frame_id saved_frame_id;
1834 int frame_saved;
1835
1836 /* If this is a local watchpoint, we only want to check if the
1837 watchpoint frame is in scope if the current thread is the thread
1838 that was used to create the watchpoint. */
1839 if (!watchpoint_in_thread_scope (b))
1840 return;
1841
1842 if (b->base.disposition == disp_del_at_next_stop)
1843 return;
1844
1845 frame_saved = 0;
1846
1847 /* Determine if the watchpoint is within scope. */
1848 if (b->exp_valid_block == NULL)
1849 within_current_scope = 1;
1850 else
1851 {
1852 struct frame_info *fi = get_current_frame ();
1853 struct gdbarch *frame_arch = get_frame_arch (fi);
1854 CORE_ADDR frame_pc = get_frame_pc (fi);
1855
1856 /* If we're in a function epilogue, unwinding may not work
1857 properly, so do not attempt to recreate locations at this
1858 point. See similar comments in watchpoint_check. */
1859 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1860 return;
1861
1862 /* Save the current frame's ID so we can restore it after
1863 evaluating the watchpoint expression on its own frame. */
1864 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1865 took a frame parameter, so that we didn't have to change the
1866 selected frame. */
1867 frame_saved = 1;
1868 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1869
1870 fi = frame_find_by_id (b->watchpoint_frame);
1871 within_current_scope = (fi != NULL);
1872 if (within_current_scope)
1873 select_frame (fi);
1874 }
1875
1876 /* We don't free locations. They are stored in the bp_location array
1877 and update_global_location_list will eventually delete them and
1878 remove breakpoints if needed. */
1879 b->base.loc = NULL;
1880
1881 if (within_current_scope && reparse)
1882 {
1883 const char *s;
1884
1885 if (b->exp)
1886 {
1887 xfree (b->exp);
1888 b->exp = NULL;
1889 }
1890 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1891 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1892 /* If the meaning of expression itself changed, the old value is
1893 no longer relevant. We don't want to report a watchpoint hit
1894 to the user when the old value and the new value may actually
1895 be completely different objects. */
1896 value_free (b->val);
1897 b->val = NULL;
1898 b->val_valid = 0;
1899
1900 /* Note that unlike with breakpoints, the watchpoint's condition
1901 expression is stored in the breakpoint object, not in the
1902 locations (re)created below. */
1903 if (b->base.cond_string != NULL)
1904 {
1905 if (b->cond_exp != NULL)
1906 {
1907 xfree (b->cond_exp);
1908 b->cond_exp = NULL;
1909 }
1910
1911 s = b->base.cond_string;
1912 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1913 }
1914 }
1915
1916 /* If we failed to parse the expression, for example because
1917 it refers to a global variable in a not-yet-loaded shared library,
1918 don't try to insert watchpoint. We don't automatically delete
1919 such watchpoint, though, since failure to parse expression
1920 is different from out-of-scope watchpoint. */
1921 if (!target_has_execution)
1922 {
1923 /* Without execution, memory can't change. No use to try and
1924 set watchpoint locations. The watchpoint will be reset when
1925 the target gains execution, through breakpoint_re_set. */
1926 if (!can_use_hw_watchpoints)
1927 {
1928 if (b->base.ops->works_in_software_mode (&b->base))
1929 b->base.type = bp_watchpoint;
1930 else
1931 error (_("Can't set read/access watchpoint when "
1932 "hardware watchpoints are disabled."));
1933 }
1934 }
1935 else if (within_current_scope && b->exp)
1936 {
1937 int pc = 0;
1938 struct value *val_chain, *v, *result, *next;
1939 struct program_space *frame_pspace;
1940
1941 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1942
1943 /* Avoid setting b->val if it's already set. The meaning of
1944 b->val is 'the last value' user saw, and we should update
1945 it only if we reported that last value to user. As it
1946 happens, the code that reports it updates b->val directly.
1947 We don't keep track of the memory value for masked
1948 watchpoints. */
1949 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1950 {
1951 if (b->val_bitsize != 0)
1952 {
1953 v = extract_bitfield_from_watchpoint_value (b, v);
1954 if (v != NULL)
1955 release_value (v);
1956 }
1957 b->val = v;
1958 b->val_valid = 1;
1959 }
1960
1961 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1962
1963 /* Look at each value on the value chain. */
1964 for (v = val_chain; v; v = value_next (v))
1965 {
1966 /* If it's a memory location, and GDB actually needed
1967 its contents to evaluate the expression, then we
1968 must watch it. If the first value returned is
1969 still lazy, that means an error occurred reading it;
1970 watch it anyway in case it becomes readable. */
1971 if (VALUE_LVAL (v) == lval_memory
1972 && (v == val_chain || ! value_lazy (v)))
1973 {
1974 struct type *vtype = check_typedef (value_type (v));
1975
1976 /* We only watch structs and arrays if user asked
1977 for it explicitly, never if they just happen to
1978 appear in the middle of some value chain. */
1979 if (v == result
1980 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1981 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1982 {
1983 CORE_ADDR addr;
1984 int type;
1985 struct bp_location *loc, **tmp;
1986 int bitpos = 0, bitsize = 0;
1987
1988 if (value_bitsize (v) != 0)
1989 {
1990 /* Extract the bit parameters out from the bitfield
1991 sub-expression. */
1992 bitpos = value_bitpos (v);
1993 bitsize = value_bitsize (v);
1994 }
1995 else if (v == result && b->val_bitsize != 0)
1996 {
1997 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1998 lvalue whose bit parameters are saved in the fields
1999 VAL_BITPOS and VAL_BITSIZE. */
2000 bitpos = b->val_bitpos;
2001 bitsize = b->val_bitsize;
2002 }
2003
2004 addr = value_address (v);
2005 if (bitsize != 0)
2006 {
2007 /* Skip the bytes that don't contain the bitfield. */
2008 addr += bitpos / 8;
2009 }
2010
2011 type = hw_write;
2012 if (b->base.type == bp_read_watchpoint)
2013 type = hw_read;
2014 else if (b->base.type == bp_access_watchpoint)
2015 type = hw_access;
2016
2017 loc = allocate_bp_location (&b->base);
2018 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2019 ;
2020 *tmp = loc;
2021 loc->gdbarch = get_type_arch (value_type (v));
2022
2023 loc->pspace = frame_pspace;
2024 loc->address = addr;
2025
2026 if (bitsize != 0)
2027 {
2028 /* Just cover the bytes that make up the bitfield. */
2029 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2030 }
2031 else
2032 loc->length = TYPE_LENGTH (value_type (v));
2033
2034 loc->watchpoint_type = type;
2035 }
2036 }
2037 }
2038
2039 /* Change the type of breakpoint between hardware assisted or
2040 an ordinary watchpoint depending on the hardware support
2041 and free hardware slots. REPARSE is set when the inferior
2042 is started. */
2043 if (reparse)
2044 {
2045 int reg_cnt;
2046 enum bp_loc_type loc_type;
2047 struct bp_location *bl;
2048
2049 reg_cnt = can_use_hardware_watchpoint (val_chain);
2050
2051 if (reg_cnt)
2052 {
2053 int i, target_resources_ok, other_type_used;
2054 enum bptype type;
2055
2056 /* Use an exact watchpoint when there's only one memory region to be
2057 watched, and only one debug register is needed to watch it. */
2058 b->exact = target_exact_watchpoints && reg_cnt == 1;
2059
2060 /* We need to determine how many resources are already
2061 used for all other hardware watchpoints plus this one
2062 to see if we still have enough resources to also fit
2063 this watchpoint in as well. */
2064
2065 /* If this is a software watchpoint, we try to turn it
2066 to a hardware one -- count resources as if B was of
2067 hardware watchpoint type. */
2068 type = b->base.type;
2069 if (type == bp_watchpoint)
2070 type = bp_hardware_watchpoint;
2071
2072 /* This watchpoint may or may not have been placed on
2073 the list yet at this point (it won't be in the list
2074 if we're trying to create it for the first time,
2075 through watch_command), so always account for it
2076 manually. */
2077
2078 /* Count resources used by all watchpoints except B. */
2079 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2080
2081 /* Add in the resources needed for B. */
2082 i += hw_watchpoint_use_count (&b->base);
2083
2084 target_resources_ok
2085 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2086 if (target_resources_ok <= 0)
2087 {
2088 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2089
2090 if (target_resources_ok == 0 && !sw_mode)
2091 error (_("Target does not support this type of "
2092 "hardware watchpoint."));
2093 else if (target_resources_ok < 0 && !sw_mode)
2094 error (_("There are not enough available hardware "
2095 "resources for this watchpoint."));
2096
2097 /* Downgrade to software watchpoint. */
2098 b->base.type = bp_watchpoint;
2099 }
2100 else
2101 {
2102 /* If this was a software watchpoint, we've just
2103 found we have enough resources to turn it to a
2104 hardware watchpoint. Otherwise, this is a
2105 nop. */
2106 b->base.type = type;
2107 }
2108 }
2109 else if (!b->base.ops->works_in_software_mode (&b->base))
2110 {
2111 if (!can_use_hw_watchpoints)
2112 error (_("Can't set read/access watchpoint when "
2113 "hardware watchpoints are disabled."));
2114 else
2115 error (_("Expression cannot be implemented with "
2116 "read/access watchpoint."));
2117 }
2118 else
2119 b->base.type = bp_watchpoint;
2120
2121 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2122 : bp_loc_hardware_watchpoint);
2123 for (bl = b->base.loc; bl; bl = bl->next)
2124 bl->loc_type = loc_type;
2125 }
2126
2127 for (v = val_chain; v; v = next)
2128 {
2129 next = value_next (v);
2130 if (v != b->val)
2131 value_free (v);
2132 }
2133
2134 /* If a software watchpoint is not watching any memory, then the
2135 above left it without any location set up. But,
2136 bpstat_stop_status requires a location to be able to report
2137 stops, so make sure there's at least a dummy one. */
2138 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2139 {
2140 struct breakpoint *base = &b->base;
2141 base->loc = allocate_bp_location (base);
2142 base->loc->pspace = frame_pspace;
2143 base->loc->address = -1;
2144 base->loc->length = -1;
2145 base->loc->watchpoint_type = -1;
2146 }
2147 }
2148 else if (!within_current_scope)
2149 {
2150 printf_filtered (_("\
2151 Watchpoint %d deleted because the program has left the block\n\
2152 in which its expression is valid.\n"),
2153 b->base.number);
2154 watchpoint_del_at_next_stop (b);
2155 }
2156
2157 /* Restore the selected frame. */
2158 if (frame_saved)
2159 select_frame (frame_find_by_id (saved_frame_id));
2160 }
2161
2162
2163 /* Returns 1 iff breakpoint location should be
2164 inserted in the inferior. We don't differentiate the type of BL's owner
2165 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2166 breakpoint_ops is not defined, because in insert_bp_location,
2167 tracepoint's insert_location will not be called. */
2168 static int
2169 should_be_inserted (struct bp_location *bl)
2170 {
2171 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2172 return 0;
2173
2174 if (bl->owner->disposition == disp_del_at_next_stop)
2175 return 0;
2176
2177 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2178 return 0;
2179
2180 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2181 return 0;
2182
2183 /* This is set for example, when we're attached to the parent of a
2184 vfork, and have detached from the child. The child is running
2185 free, and we expect it to do an exec or exit, at which point the
2186 OS makes the parent schedulable again (and the target reports
2187 that the vfork is done). Until the child is done with the shared
2188 memory region, do not insert breakpoints in the parent, otherwise
2189 the child could still trip on the parent's breakpoints. Since
2190 the parent is blocked anyway, it won't miss any breakpoint. */
2191 if (bl->pspace->breakpoints_not_allowed)
2192 return 0;
2193
2194 /* Don't insert a breakpoint if we're trying to step past its
2195 location. */
2196 if ((bl->loc_type == bp_loc_software_breakpoint
2197 || bl->loc_type == bp_loc_hardware_breakpoint)
2198 && stepping_past_instruction_at (bl->pspace->aspace,
2199 bl->address))
2200 {
2201 if (debug_infrun)
2202 {
2203 fprintf_unfiltered (gdb_stdlog,
2204 "infrun: skipping breakpoint: "
2205 "stepping past insn at: %s\n",
2206 paddress (bl->gdbarch, bl->address));
2207 }
2208 return 0;
2209 }
2210
2211 /* Don't insert watchpoints if we're trying to step past the
2212 instruction that triggered one. */
2213 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2214 && stepping_past_nonsteppable_watchpoint ())
2215 {
2216 if (debug_infrun)
2217 {
2218 fprintf_unfiltered (gdb_stdlog,
2219 "infrun: stepping past non-steppable watchpoint. "
2220 "skipping watchpoint at %s:%d\n",
2221 paddress (bl->gdbarch, bl->address),
2222 bl->length);
2223 }
2224 return 0;
2225 }
2226
2227 return 1;
2228 }
2229
2230 /* Same as should_be_inserted but does the check assuming
2231 that the location is not duplicated. */
2232
2233 static int
2234 unduplicated_should_be_inserted (struct bp_location *bl)
2235 {
2236 int result;
2237 const int save_duplicate = bl->duplicate;
2238
2239 bl->duplicate = 0;
2240 result = should_be_inserted (bl);
2241 bl->duplicate = save_duplicate;
2242 return result;
2243 }
2244
2245 /* Parses a conditional described by an expression COND into an
2246 agent expression bytecode suitable for evaluation
2247 by the bytecode interpreter. Return NULL if there was
2248 any error during parsing. */
2249
2250 static struct agent_expr *
2251 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2252 {
2253 struct agent_expr *aexpr = NULL;
2254 volatile struct gdb_exception ex;
2255
2256 if (!cond)
2257 return NULL;
2258
2259 /* We don't want to stop processing, so catch any errors
2260 that may show up. */
2261 TRY_CATCH (ex, RETURN_MASK_ERROR)
2262 {
2263 aexpr = gen_eval_for_expr (scope, cond);
2264 }
2265
2266 if (ex.reason < 0)
2267 {
2268 /* If we got here, it means the condition could not be parsed to a valid
2269 bytecode expression and thus can't be evaluated on the target's side.
2270 It's no use iterating through the conditions. */
2271 return NULL;
2272 }
2273
2274 /* We have a valid agent expression. */
2275 return aexpr;
2276 }
2277
2278 /* Based on location BL, create a list of breakpoint conditions to be
2279 passed on to the target. If we have duplicated locations with different
2280 conditions, we will add such conditions to the list. The idea is that the
2281 target will evaluate the list of conditions and will only notify GDB when
2282 one of them is true. */
2283
2284 static void
2285 build_target_condition_list (struct bp_location *bl)
2286 {
2287 struct bp_location **locp = NULL, **loc2p;
2288 int null_condition_or_parse_error = 0;
2289 int modified = bl->needs_update;
2290 struct bp_location *loc;
2291
2292 /* Release conditions left over from a previous insert. */
2293 VEC_free (agent_expr_p, bl->target_info.conditions);
2294
2295 /* This is only meaningful if the target is
2296 evaluating conditions and if the user has
2297 opted for condition evaluation on the target's
2298 side. */
2299 if (gdb_evaluates_breakpoint_condition_p ()
2300 || !target_supports_evaluation_of_breakpoint_conditions ())
2301 return;
2302
2303 /* Do a first pass to check for locations with no assigned
2304 conditions or conditions that fail to parse to a valid agent expression
2305 bytecode. If any of these happen, then it's no use to send conditions
2306 to the target since this location will always trigger and generate a
2307 response back to GDB. */
2308 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2309 {
2310 loc = (*loc2p);
2311 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2312 {
2313 if (modified)
2314 {
2315 struct agent_expr *aexpr;
2316
2317 /* Re-parse the conditions since something changed. In that
2318 case we already freed the condition bytecodes (see
2319 force_breakpoint_reinsertion). We just
2320 need to parse the condition to bytecodes again. */
2321 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2322 loc->cond_bytecode = aexpr;
2323
2324 /* Check if we managed to parse the conditional expression
2325 correctly. If not, we will not send this condition
2326 to the target. */
2327 if (aexpr)
2328 continue;
2329 }
2330
2331 /* If we have a NULL bytecode expression, it means something
2332 went wrong or we have a null condition expression. */
2333 if (!loc->cond_bytecode)
2334 {
2335 null_condition_or_parse_error = 1;
2336 break;
2337 }
2338 }
2339 }
2340
2341 /* If any of these happened, it means we will have to evaluate the conditions
2342 for the location's address on gdb's side. It is no use keeping bytecodes
2343 for all the other duplicate locations, thus we free all of them here.
2344
2345 This is so we have a finer control over which locations' conditions are
2346 being evaluated by GDB or the remote stub. */
2347 if (null_condition_or_parse_error)
2348 {
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2353 {
2354 /* Only go as far as the first NULL bytecode is
2355 located. */
2356 if (!loc->cond_bytecode)
2357 return;
2358
2359 free_agent_expr (loc->cond_bytecode);
2360 loc->cond_bytecode = NULL;
2361 }
2362 }
2363 }
2364
2365 /* No NULL conditions or failed bytecode generation. Build a condition list
2366 for this location's address. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (loc->cond
2371 && is_breakpoint (loc->owner)
2372 && loc->pspace->num == bl->pspace->num
2373 && loc->owner->enable_state == bp_enabled
2374 && loc->enabled)
2375 /* Add the condition to the vector. This will be used later to send the
2376 conditions to the target. */
2377 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2378 loc->cond_bytecode);
2379 }
2380
2381 return;
2382 }
2383
2384 /* Parses a command described by string CMD into an agent expression
2385 bytecode suitable for evaluation by the bytecode interpreter.
2386 Return NULL if there was any error during parsing. */
2387
2388 static struct agent_expr *
2389 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2390 {
2391 struct cleanup *old_cleanups = 0;
2392 struct expression *expr, **argvec;
2393 struct agent_expr *aexpr = NULL;
2394 volatile struct gdb_exception ex;
2395 const char *cmdrest;
2396 const char *format_start, *format_end;
2397 struct format_piece *fpieces;
2398 int nargs;
2399 struct gdbarch *gdbarch = get_current_arch ();
2400
2401 if (!cmd)
2402 return NULL;
2403
2404 cmdrest = cmd;
2405
2406 if (*cmdrest == ',')
2407 ++cmdrest;
2408 cmdrest = skip_spaces_const (cmdrest);
2409
2410 if (*cmdrest++ != '"')
2411 error (_("No format string following the location"));
2412
2413 format_start = cmdrest;
2414
2415 fpieces = parse_format_string (&cmdrest);
2416
2417 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2418
2419 format_end = cmdrest;
2420
2421 if (*cmdrest++ != '"')
2422 error (_("Bad format string, non-terminated '\"'."));
2423
2424 cmdrest = skip_spaces_const (cmdrest);
2425
2426 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2427 error (_("Invalid argument syntax"));
2428
2429 if (*cmdrest == ',')
2430 cmdrest++;
2431 cmdrest = skip_spaces_const (cmdrest);
2432
2433 /* For each argument, make an expression. */
2434
2435 argvec = (struct expression **) alloca (strlen (cmd)
2436 * sizeof (struct expression *));
2437
2438 nargs = 0;
2439 while (*cmdrest != '\0')
2440 {
2441 const char *cmd1;
2442
2443 cmd1 = cmdrest;
2444 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2445 argvec[nargs++] = expr;
2446 cmdrest = cmd1;
2447 if (*cmdrest == ',')
2448 ++cmdrest;
2449 }
2450
2451 /* We don't want to stop processing, so catch any errors
2452 that may show up. */
2453 TRY_CATCH (ex, RETURN_MASK_ERROR)
2454 {
2455 aexpr = gen_printf (scope, gdbarch, 0, 0,
2456 format_start, format_end - format_start,
2457 fpieces, nargs, argvec);
2458 }
2459
2460 do_cleanups (old_cleanups);
2461
2462 if (ex.reason < 0)
2463 {
2464 /* If we got here, it means the command could not be parsed to a valid
2465 bytecode expression and thus can't be evaluated on the target's side.
2466 It's no use iterating through the other commands. */
2467 return NULL;
2468 }
2469
2470 /* We have a valid agent expression, return it. */
2471 return aexpr;
2472 }
2473
2474 /* Based on location BL, create a list of breakpoint commands to be
2475 passed on to the target. If we have duplicated locations with
2476 different commands, we will add any such to the list. */
2477
2478 static void
2479 build_target_command_list (struct bp_location *bl)
2480 {
2481 struct bp_location **locp = NULL, **loc2p;
2482 int null_command_or_parse_error = 0;
2483 int modified = bl->needs_update;
2484 struct bp_location *loc;
2485
2486 /* Release commands left over from a previous insert. */
2487 VEC_free (agent_expr_p, bl->target_info.tcommands);
2488
2489 if (!target_can_run_breakpoint_commands ())
2490 return;
2491
2492 /* For now, limit to agent-style dprintf breakpoints. */
2493 if (dprintf_style != dprintf_style_agent)
2494 return;
2495
2496 /* For now, if we have any duplicate location that isn't a dprintf,
2497 don't install the target-side commands, as that would make the
2498 breakpoint not be reported to the core, and we'd lose
2499 control. */
2500 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2501 {
2502 loc = (*loc2p);
2503 if (is_breakpoint (loc->owner)
2504 && loc->pspace->num == bl->pspace->num
2505 && loc->owner->type != bp_dprintf)
2506 return;
2507 }
2508
2509 /* Do a first pass to check for locations with no assigned
2510 conditions or conditions that fail to parse to a valid agent expression
2511 bytecode. If any of these happen, then it's no use to send conditions
2512 to the target since this location will always trigger and generate a
2513 response back to GDB. */
2514 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2515 {
2516 loc = (*loc2p);
2517 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2518 {
2519 if (modified)
2520 {
2521 struct agent_expr *aexpr;
2522
2523 /* Re-parse the commands since something changed. In that
2524 case we already freed the command bytecodes (see
2525 force_breakpoint_reinsertion). We just
2526 need to parse the command to bytecodes again. */
2527 aexpr = parse_cmd_to_aexpr (bl->address,
2528 loc->owner->extra_string);
2529 loc->cmd_bytecode = aexpr;
2530
2531 if (!aexpr)
2532 continue;
2533 }
2534
2535 /* If we have a NULL bytecode expression, it means something
2536 went wrong or we have a null command expression. */
2537 if (!loc->cmd_bytecode)
2538 {
2539 null_command_or_parse_error = 1;
2540 break;
2541 }
2542 }
2543 }
2544
2545 /* If anything failed, then we're not doing target-side commands,
2546 and so clean up. */
2547 if (null_command_or_parse_error)
2548 {
2549 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2550 {
2551 loc = (*loc2p);
2552 if (is_breakpoint (loc->owner)
2553 && loc->pspace->num == bl->pspace->num)
2554 {
2555 /* Only go as far as the first NULL bytecode is
2556 located. */
2557 if (loc->cmd_bytecode == NULL)
2558 return;
2559
2560 free_agent_expr (loc->cmd_bytecode);
2561 loc->cmd_bytecode = NULL;
2562 }
2563 }
2564 }
2565
2566 /* No NULL commands or failed bytecode generation. Build a command list
2567 for this location's address. */
2568 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2569 {
2570 loc = (*loc2p);
2571 if (loc->owner->extra_string
2572 && is_breakpoint (loc->owner)
2573 && loc->pspace->num == bl->pspace->num
2574 && loc->owner->enable_state == bp_enabled
2575 && loc->enabled)
2576 /* Add the command to the vector. This will be used later
2577 to send the commands to the target. */
2578 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2579 loc->cmd_bytecode);
2580 }
2581
2582 bl->target_info.persist = 0;
2583 /* Maybe flag this location as persistent. */
2584 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2585 bl->target_info.persist = 1;
2586 }
2587
2588 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2589 location. Any error messages are printed to TMP_ERROR_STREAM; and
2590 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2591 Returns 0 for success, 1 if the bp_location type is not supported or
2592 -1 for failure.
2593
2594 NOTE drow/2003-09-09: This routine could be broken down to an
2595 object-style method for each breakpoint or catchpoint type. */
2596 static int
2597 insert_bp_location (struct bp_location *bl,
2598 struct ui_file *tmp_error_stream,
2599 int *disabled_breaks,
2600 int *hw_breakpoint_error,
2601 int *hw_bp_error_explained_already)
2602 {
2603 enum errors bp_err = GDB_NO_ERROR;
2604 const char *bp_err_message = NULL;
2605 volatile struct gdb_exception e;
2606
2607 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2608 return 0;
2609
2610 /* Note we don't initialize bl->target_info, as that wipes out
2611 the breakpoint location's shadow_contents if the breakpoint
2612 is still inserted at that location. This in turn breaks
2613 target_read_memory which depends on these buffers when
2614 a memory read is requested at the breakpoint location:
2615 Once the target_info has been wiped, we fail to see that
2616 we have a breakpoint inserted at that address and thus
2617 read the breakpoint instead of returning the data saved in
2618 the breakpoint location's shadow contents. */
2619 bl->target_info.reqstd_address = bl->address;
2620 bl->target_info.placed_address_space = bl->pspace->aspace;
2621 bl->target_info.length = bl->length;
2622
2623 /* When working with target-side conditions, we must pass all the conditions
2624 for the same breakpoint address down to the target since GDB will not
2625 insert those locations. With a list of breakpoint conditions, the target
2626 can decide when to stop and notify GDB. */
2627
2628 if (is_breakpoint (bl->owner))
2629 {
2630 build_target_condition_list (bl);
2631 build_target_command_list (bl);
2632 /* Reset the modification marker. */
2633 bl->needs_update = 0;
2634 }
2635
2636 if (bl->loc_type == bp_loc_software_breakpoint
2637 || bl->loc_type == bp_loc_hardware_breakpoint)
2638 {
2639 if (bl->owner->type != bp_hardware_breakpoint)
2640 {
2641 /* If the explicitly specified breakpoint type
2642 is not hardware breakpoint, check the memory map to see
2643 if the breakpoint address is in read only memory or not.
2644
2645 Two important cases are:
2646 - location type is not hardware breakpoint, memory
2647 is readonly. We change the type of the location to
2648 hardware breakpoint.
2649 - location type is hardware breakpoint, memory is
2650 read-write. This means we've previously made the
2651 location hardware one, but then the memory map changed,
2652 so we undo.
2653
2654 When breakpoints are removed, remove_breakpoints will use
2655 location types we've just set here, the only possible
2656 problem is that memory map has changed during running
2657 program, but it's not going to work anyway with current
2658 gdb. */
2659 struct mem_region *mr
2660 = lookup_mem_region (bl->target_info.reqstd_address);
2661
2662 if (mr)
2663 {
2664 if (automatic_hardware_breakpoints)
2665 {
2666 enum bp_loc_type new_type;
2667
2668 if (mr->attrib.mode != MEM_RW)
2669 new_type = bp_loc_hardware_breakpoint;
2670 else
2671 new_type = bp_loc_software_breakpoint;
2672
2673 if (new_type != bl->loc_type)
2674 {
2675 static int said = 0;
2676
2677 bl->loc_type = new_type;
2678 if (!said)
2679 {
2680 fprintf_filtered (gdb_stdout,
2681 _("Note: automatically using "
2682 "hardware breakpoints for "
2683 "read-only addresses.\n"));
2684 said = 1;
2685 }
2686 }
2687 }
2688 else if (bl->loc_type == bp_loc_software_breakpoint
2689 && mr->attrib.mode != MEM_RW)
2690 {
2691 fprintf_unfiltered (tmp_error_stream,
2692 _("Cannot insert breakpoint %d.\n"
2693 "Cannot set software breakpoint "
2694 "at read-only address %s\n"),
2695 bl->owner->number,
2696 paddress (bl->gdbarch, bl->address));
2697 return 1;
2698 }
2699 }
2700 }
2701
2702 /* First check to see if we have to handle an overlay. */
2703 if (overlay_debugging == ovly_off
2704 || bl->section == NULL
2705 || !(section_is_overlay (bl->section)))
2706 {
2707 /* No overlay handling: just set the breakpoint. */
2708 TRY_CATCH (e, RETURN_MASK_ALL)
2709 {
2710 int val;
2711
2712 val = bl->owner->ops->insert_location (bl);
2713 if (val)
2714 bp_err = GENERIC_ERROR;
2715 }
2716 if (e.reason < 0)
2717 {
2718 bp_err = e.error;
2719 bp_err_message = e.message;
2720 }
2721 }
2722 else
2723 {
2724 /* This breakpoint is in an overlay section.
2725 Shall we set a breakpoint at the LMA? */
2726 if (!overlay_events_enabled)
2727 {
2728 /* Yes -- overlay event support is not active,
2729 so we must try to set a breakpoint at the LMA.
2730 This will not work for a hardware breakpoint. */
2731 if (bl->loc_type == bp_loc_hardware_breakpoint)
2732 warning (_("hardware breakpoint %d not supported in overlay!"),
2733 bl->owner->number);
2734 else
2735 {
2736 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2737 bl->section);
2738 /* Set a software (trap) breakpoint at the LMA. */
2739 bl->overlay_target_info = bl->target_info;
2740 bl->overlay_target_info.reqstd_address = addr;
2741
2742 /* No overlay handling: just set the breakpoint. */
2743 TRY_CATCH (e, RETURN_MASK_ALL)
2744 {
2745 int val;
2746
2747 val = target_insert_breakpoint (bl->gdbarch,
2748 &bl->overlay_target_info);
2749 if (val)
2750 bp_err = GENERIC_ERROR;
2751 }
2752 if (e.reason < 0)
2753 {
2754 bp_err = e.error;
2755 bp_err_message = e.message;
2756 }
2757
2758 if (bp_err != GDB_NO_ERROR)
2759 fprintf_unfiltered (tmp_error_stream,
2760 "Overlay breakpoint %d "
2761 "failed: in ROM?\n",
2762 bl->owner->number);
2763 }
2764 }
2765 /* Shall we set a breakpoint at the VMA? */
2766 if (section_is_mapped (bl->section))
2767 {
2768 /* Yes. This overlay section is mapped into memory. */
2769 TRY_CATCH (e, RETURN_MASK_ALL)
2770 {
2771 int val;
2772
2773 val = bl->owner->ops->insert_location (bl);
2774 if (val)
2775 bp_err = GENERIC_ERROR;
2776 }
2777 if (e.reason < 0)
2778 {
2779 bp_err = e.error;
2780 bp_err_message = e.message;
2781 }
2782 }
2783 else
2784 {
2785 /* No. This breakpoint will not be inserted.
2786 No error, but do not mark the bp as 'inserted'. */
2787 return 0;
2788 }
2789 }
2790
2791 if (bp_err != GDB_NO_ERROR)
2792 {
2793 /* Can't set the breakpoint. */
2794
2795 /* In some cases, we might not be able to insert a
2796 breakpoint in a shared library that has already been
2797 removed, but we have not yet processed the shlib unload
2798 event. Unfortunately, some targets that implement
2799 breakpoint insertion themselves can't tell why the
2800 breakpoint insertion failed (e.g., the remote target
2801 doesn't define error codes), so we must treat generic
2802 errors as memory errors. */
2803 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2804 && bl->loc_type == bp_loc_software_breakpoint
2805 && (solib_name_from_address (bl->pspace, bl->address)
2806 || shared_objfile_contains_address_p (bl->pspace,
2807 bl->address)))
2808 {
2809 /* See also: disable_breakpoints_in_shlibs. */
2810 bl->shlib_disabled = 1;
2811 observer_notify_breakpoint_modified (bl->owner);
2812 if (!*disabled_breaks)
2813 {
2814 fprintf_unfiltered (tmp_error_stream,
2815 "Cannot insert breakpoint %d.\n",
2816 bl->owner->number);
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Temporarily disabling shared "
2819 "library breakpoints:\n");
2820 }
2821 *disabled_breaks = 1;
2822 fprintf_unfiltered (tmp_error_stream,
2823 "breakpoint #%d\n", bl->owner->number);
2824 return 0;
2825 }
2826 else
2827 {
2828 if (bl->loc_type == bp_loc_hardware_breakpoint)
2829 {
2830 *hw_breakpoint_error = 1;
2831 *hw_bp_error_explained_already = bp_err_message != NULL;
2832 fprintf_unfiltered (tmp_error_stream,
2833 "Cannot insert hardware breakpoint %d%s",
2834 bl->owner->number, bp_err_message ? ":" : ".\n");
2835 if (bp_err_message != NULL)
2836 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2837 }
2838 else
2839 {
2840 if (bp_err_message == NULL)
2841 {
2842 char *message
2843 = memory_error_message (TARGET_XFER_E_IO,
2844 bl->gdbarch, bl->address);
2845 struct cleanup *old_chain = make_cleanup (xfree, message);
2846
2847 fprintf_unfiltered (tmp_error_stream,
2848 "Cannot insert breakpoint %d.\n"
2849 "%s\n",
2850 bl->owner->number, message);
2851 do_cleanups (old_chain);
2852 }
2853 else
2854 {
2855 fprintf_unfiltered (tmp_error_stream,
2856 "Cannot insert breakpoint %d: %s\n",
2857 bl->owner->number,
2858 bp_err_message);
2859 }
2860 }
2861 return 1;
2862
2863 }
2864 }
2865 else
2866 bl->inserted = 1;
2867
2868 return 0;
2869 }
2870
2871 else if (bl->loc_type == bp_loc_hardware_watchpoint
2872 /* NOTE drow/2003-09-08: This state only exists for removing
2873 watchpoints. It's not clear that it's necessary... */
2874 && bl->owner->disposition != disp_del_at_next_stop)
2875 {
2876 int val;
2877
2878 gdb_assert (bl->owner->ops != NULL
2879 && bl->owner->ops->insert_location != NULL);
2880
2881 val = bl->owner->ops->insert_location (bl);
2882
2883 /* If trying to set a read-watchpoint, and it turns out it's not
2884 supported, try emulating one with an access watchpoint. */
2885 if (val == 1 && bl->watchpoint_type == hw_read)
2886 {
2887 struct bp_location *loc, **loc_temp;
2888
2889 /* But don't try to insert it, if there's already another
2890 hw_access location that would be considered a duplicate
2891 of this one. */
2892 ALL_BP_LOCATIONS (loc, loc_temp)
2893 if (loc != bl
2894 && loc->watchpoint_type == hw_access
2895 && watchpoint_locations_match (bl, loc))
2896 {
2897 bl->duplicate = 1;
2898 bl->inserted = 1;
2899 bl->target_info = loc->target_info;
2900 bl->watchpoint_type = hw_access;
2901 val = 0;
2902 break;
2903 }
2904
2905 if (val == 1)
2906 {
2907 bl->watchpoint_type = hw_access;
2908 val = bl->owner->ops->insert_location (bl);
2909
2910 if (val)
2911 /* Back to the original value. */
2912 bl->watchpoint_type = hw_read;
2913 }
2914 }
2915
2916 bl->inserted = (val == 0);
2917 }
2918
2919 else if (bl->owner->type == bp_catchpoint)
2920 {
2921 int val;
2922
2923 gdb_assert (bl->owner->ops != NULL
2924 && bl->owner->ops->insert_location != NULL);
2925
2926 val = bl->owner->ops->insert_location (bl);
2927 if (val)
2928 {
2929 bl->owner->enable_state = bp_disabled;
2930
2931 if (val == 1)
2932 warning (_("\
2933 Error inserting catchpoint %d: Your system does not support this type\n\
2934 of catchpoint."), bl->owner->number);
2935 else
2936 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2937 }
2938
2939 bl->inserted = (val == 0);
2940
2941 /* We've already printed an error message if there was a problem
2942 inserting this catchpoint, and we've disabled the catchpoint,
2943 so just return success. */
2944 return 0;
2945 }
2946
2947 return 0;
2948 }
2949
2950 /* This function is called when program space PSPACE is about to be
2951 deleted. It takes care of updating breakpoints to not reference
2952 PSPACE anymore. */
2953
2954 void
2955 breakpoint_program_space_exit (struct program_space *pspace)
2956 {
2957 struct breakpoint *b, *b_temp;
2958 struct bp_location *loc, **loc_temp;
2959
2960 /* Remove any breakpoint that was set through this program space. */
2961 ALL_BREAKPOINTS_SAFE (b, b_temp)
2962 {
2963 if (b->pspace == pspace)
2964 delete_breakpoint (b);
2965 }
2966
2967 /* Breakpoints set through other program spaces could have locations
2968 bound to PSPACE as well. Remove those. */
2969 ALL_BP_LOCATIONS (loc, loc_temp)
2970 {
2971 struct bp_location *tmp;
2972
2973 if (loc->pspace == pspace)
2974 {
2975 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2976 if (loc->owner->loc == loc)
2977 loc->owner->loc = loc->next;
2978 else
2979 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2980 if (tmp->next == loc)
2981 {
2982 tmp->next = loc->next;
2983 break;
2984 }
2985 }
2986 }
2987
2988 /* Now update the global location list to permanently delete the
2989 removed locations above. */
2990 update_global_location_list (UGLL_DONT_INSERT);
2991 }
2992
2993 /* Make sure all breakpoints are inserted in inferior.
2994 Throws exception on any error.
2995 A breakpoint that is already inserted won't be inserted
2996 again, so calling this function twice is safe. */
2997 void
2998 insert_breakpoints (void)
2999 {
3000 struct breakpoint *bpt;
3001
3002 ALL_BREAKPOINTS (bpt)
3003 if (is_hardware_watchpoint (bpt))
3004 {
3005 struct watchpoint *w = (struct watchpoint *) bpt;
3006
3007 update_watchpoint (w, 0 /* don't reparse. */);
3008 }
3009
3010 /* Updating watchpoints creates new locations, so update the global
3011 location list. Explicitly tell ugll to insert locations and
3012 ignore breakpoints_always_inserted_mode. */
3013 update_global_location_list (UGLL_INSERT);
3014 }
3015
3016 /* Invoke CALLBACK for each of bp_location. */
3017
3018 void
3019 iterate_over_bp_locations (walk_bp_location_callback callback)
3020 {
3021 struct bp_location *loc, **loc_tmp;
3022
3023 ALL_BP_LOCATIONS (loc, loc_tmp)
3024 {
3025 callback (loc, NULL);
3026 }
3027 }
3028
3029 /* This is used when we need to synch breakpoint conditions between GDB and the
3030 target. It is the case with deleting and disabling of breakpoints when using
3031 always-inserted mode. */
3032
3033 static void
3034 update_inserted_breakpoint_locations (void)
3035 {
3036 struct bp_location *bl, **blp_tmp;
3037 int error_flag = 0;
3038 int val = 0;
3039 int disabled_breaks = 0;
3040 int hw_breakpoint_error = 0;
3041 int hw_bp_details_reported = 0;
3042
3043 struct ui_file *tmp_error_stream = mem_fileopen ();
3044 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3045
3046 /* Explicitly mark the warning -- this will only be printed if
3047 there was an error. */
3048 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3049
3050 save_current_space_and_thread ();
3051
3052 ALL_BP_LOCATIONS (bl, blp_tmp)
3053 {
3054 /* We only want to update software breakpoints and hardware
3055 breakpoints. */
3056 if (!is_breakpoint (bl->owner))
3057 continue;
3058
3059 /* We only want to update locations that are already inserted
3060 and need updating. This is to avoid unwanted insertion during
3061 deletion of breakpoints. */
3062 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3063 continue;
3064
3065 switch_to_program_space_and_thread (bl->pspace);
3066
3067 /* For targets that support global breakpoints, there's no need
3068 to select an inferior to insert breakpoint to. In fact, even
3069 if we aren't attached to any process yet, we should still
3070 insert breakpoints. */
3071 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3072 && ptid_equal (inferior_ptid, null_ptid))
3073 continue;
3074
3075 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3076 &hw_breakpoint_error, &hw_bp_details_reported);
3077 if (val)
3078 error_flag = val;
3079 }
3080
3081 if (error_flag)
3082 {
3083 target_terminal_ours_for_output ();
3084 error_stream (tmp_error_stream);
3085 }
3086
3087 do_cleanups (cleanups);
3088 }
3089
3090 /* Used when starting or continuing the program. */
3091
3092 static void
3093 insert_breakpoint_locations (void)
3094 {
3095 struct breakpoint *bpt;
3096 struct bp_location *bl, **blp_tmp;
3097 int error_flag = 0;
3098 int val = 0;
3099 int disabled_breaks = 0;
3100 int hw_breakpoint_error = 0;
3101 int hw_bp_error_explained_already = 0;
3102
3103 struct ui_file *tmp_error_stream = mem_fileopen ();
3104 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3105
3106 /* Explicitly mark the warning -- this will only be printed if
3107 there was an error. */
3108 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3109
3110 save_current_space_and_thread ();
3111
3112 ALL_BP_LOCATIONS (bl, blp_tmp)
3113 {
3114 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3115 continue;
3116
3117 /* There is no point inserting thread-specific breakpoints if
3118 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3119 has BL->OWNER always non-NULL. */
3120 if (bl->owner->thread != -1
3121 && !valid_thread_id (bl->owner->thread))
3122 continue;
3123
3124 switch_to_program_space_and_thread (bl->pspace);
3125
3126 /* For targets that support global breakpoints, there's no need
3127 to select an inferior to insert breakpoint to. In fact, even
3128 if we aren't attached to any process yet, we should still
3129 insert breakpoints. */
3130 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3131 && ptid_equal (inferior_ptid, null_ptid))
3132 continue;
3133
3134 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3135 &hw_breakpoint_error, &hw_bp_error_explained_already);
3136 if (val)
3137 error_flag = val;
3138 }
3139
3140 /* If we failed to insert all locations of a watchpoint, remove
3141 them, as half-inserted watchpoint is of limited use. */
3142 ALL_BREAKPOINTS (bpt)
3143 {
3144 int some_failed = 0;
3145 struct bp_location *loc;
3146
3147 if (!is_hardware_watchpoint (bpt))
3148 continue;
3149
3150 if (!breakpoint_enabled (bpt))
3151 continue;
3152
3153 if (bpt->disposition == disp_del_at_next_stop)
3154 continue;
3155
3156 for (loc = bpt->loc; loc; loc = loc->next)
3157 if (!loc->inserted && should_be_inserted (loc))
3158 {
3159 some_failed = 1;
3160 break;
3161 }
3162 if (some_failed)
3163 {
3164 for (loc = bpt->loc; loc; loc = loc->next)
3165 if (loc->inserted)
3166 remove_breakpoint (loc, mark_uninserted);
3167
3168 hw_breakpoint_error = 1;
3169 fprintf_unfiltered (tmp_error_stream,
3170 "Could not insert hardware watchpoint %d.\n",
3171 bpt->number);
3172 error_flag = -1;
3173 }
3174 }
3175
3176 if (error_flag)
3177 {
3178 /* If a hardware breakpoint or watchpoint was inserted, add a
3179 message about possibly exhausted resources. */
3180 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3181 {
3182 fprintf_unfiltered (tmp_error_stream,
3183 "Could not insert hardware breakpoints:\n\
3184 You may have requested too many hardware breakpoints/watchpoints.\n");
3185 }
3186 target_terminal_ours_for_output ();
3187 error_stream (tmp_error_stream);
3188 }
3189
3190 do_cleanups (cleanups);
3191 }
3192
3193 /* Used when the program stops.
3194 Returns zero if successful, or non-zero if there was a problem
3195 removing a breakpoint location. */
3196
3197 int
3198 remove_breakpoints (void)
3199 {
3200 struct bp_location *bl, **blp_tmp;
3201 int val = 0;
3202
3203 ALL_BP_LOCATIONS (bl, blp_tmp)
3204 {
3205 if (bl->inserted && !is_tracepoint (bl->owner))
3206 val |= remove_breakpoint (bl, mark_uninserted);
3207 }
3208 return val;
3209 }
3210
3211 /* When a thread exits, remove breakpoints that are related to
3212 that thread. */
3213
3214 static void
3215 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3216 {
3217 struct breakpoint *b, *b_tmp;
3218
3219 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3220 {
3221 if (b->thread == tp->num && user_breakpoint_p (b))
3222 {
3223 b->disposition = disp_del_at_next_stop;
3224
3225 printf_filtered (_("\
3226 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3227 b->number, tp->num);
3228
3229 /* Hide it from the user. */
3230 b->number = 0;
3231 }
3232 }
3233 }
3234
3235 /* Remove breakpoints of process PID. */
3236
3237 int
3238 remove_breakpoints_pid (int pid)
3239 {
3240 struct bp_location *bl, **blp_tmp;
3241 int val;
3242 struct inferior *inf = find_inferior_pid (pid);
3243
3244 ALL_BP_LOCATIONS (bl, blp_tmp)
3245 {
3246 if (bl->pspace != inf->pspace)
3247 continue;
3248
3249 if (bl->owner->type == bp_dprintf)
3250 continue;
3251
3252 if (bl->inserted)
3253 {
3254 val = remove_breakpoint (bl, mark_uninserted);
3255 if (val != 0)
3256 return val;
3257 }
3258 }
3259 return 0;
3260 }
3261
3262 int
3263 reattach_breakpoints (int pid)
3264 {
3265 struct cleanup *old_chain;
3266 struct bp_location *bl, **blp_tmp;
3267 int val;
3268 struct ui_file *tmp_error_stream;
3269 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3270 struct inferior *inf;
3271 struct thread_info *tp;
3272
3273 tp = any_live_thread_of_process (pid);
3274 if (tp == NULL)
3275 return 1;
3276
3277 inf = find_inferior_pid (pid);
3278 old_chain = save_inferior_ptid ();
3279
3280 inferior_ptid = tp->ptid;
3281
3282 tmp_error_stream = mem_fileopen ();
3283 make_cleanup_ui_file_delete (tmp_error_stream);
3284
3285 ALL_BP_LOCATIONS (bl, blp_tmp)
3286 {
3287 if (bl->pspace != inf->pspace)
3288 continue;
3289
3290 if (bl->inserted)
3291 {
3292 bl->inserted = 0;
3293 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3294 if (val != 0)
3295 {
3296 do_cleanups (old_chain);
3297 return val;
3298 }
3299 }
3300 }
3301 do_cleanups (old_chain);
3302 return 0;
3303 }
3304
3305 static int internal_breakpoint_number = -1;
3306
3307 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3308 If INTERNAL is non-zero, the breakpoint number will be populated
3309 from internal_breakpoint_number and that variable decremented.
3310 Otherwise the breakpoint number will be populated from
3311 breakpoint_count and that value incremented. Internal breakpoints
3312 do not set the internal var bpnum. */
3313 static void
3314 set_breakpoint_number (int internal, struct breakpoint *b)
3315 {
3316 if (internal)
3317 b->number = internal_breakpoint_number--;
3318 else
3319 {
3320 set_breakpoint_count (breakpoint_count + 1);
3321 b->number = breakpoint_count;
3322 }
3323 }
3324
3325 static struct breakpoint *
3326 create_internal_breakpoint (struct gdbarch *gdbarch,
3327 CORE_ADDR address, enum bptype type,
3328 const struct breakpoint_ops *ops)
3329 {
3330 struct symtab_and_line sal;
3331 struct breakpoint *b;
3332
3333 init_sal (&sal); /* Initialize to zeroes. */
3334
3335 sal.pc = address;
3336 sal.section = find_pc_overlay (sal.pc);
3337 sal.pspace = current_program_space;
3338
3339 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3340 b->number = internal_breakpoint_number--;
3341 b->disposition = disp_donttouch;
3342
3343 return b;
3344 }
3345
3346 static const char *const longjmp_names[] =
3347 {
3348 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3349 };
3350 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3351
3352 /* Per-objfile data private to breakpoint.c. */
3353 struct breakpoint_objfile_data
3354 {
3355 /* Minimal symbol for "_ovly_debug_event" (if any). */
3356 struct bound_minimal_symbol overlay_msym;
3357
3358 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3359 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3360
3361 /* True if we have looked for longjmp probes. */
3362 int longjmp_searched;
3363
3364 /* SystemTap probe points for longjmp (if any). */
3365 VEC (probe_p) *longjmp_probes;
3366
3367 /* Minimal symbol for "std::terminate()" (if any). */
3368 struct bound_minimal_symbol terminate_msym;
3369
3370 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3371 struct bound_minimal_symbol exception_msym;
3372
3373 /* True if we have looked for exception probes. */
3374 int exception_searched;
3375
3376 /* SystemTap probe points for unwinding (if any). */
3377 VEC (probe_p) *exception_probes;
3378 };
3379
3380 static const struct objfile_data *breakpoint_objfile_key;
3381
3382 /* Minimal symbol not found sentinel. */
3383 static struct minimal_symbol msym_not_found;
3384
3385 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3386
3387 static int
3388 msym_not_found_p (const struct minimal_symbol *msym)
3389 {
3390 return msym == &msym_not_found;
3391 }
3392
3393 /* Return per-objfile data needed by breakpoint.c.
3394 Allocate the data if necessary. */
3395
3396 static struct breakpoint_objfile_data *
3397 get_breakpoint_objfile_data (struct objfile *objfile)
3398 {
3399 struct breakpoint_objfile_data *bp_objfile_data;
3400
3401 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3402 if (bp_objfile_data == NULL)
3403 {
3404 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3405 sizeof (*bp_objfile_data));
3406
3407 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3408 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3409 }
3410 return bp_objfile_data;
3411 }
3412
3413 static void
3414 free_breakpoint_probes (struct objfile *obj, void *data)
3415 {
3416 struct breakpoint_objfile_data *bp_objfile_data = data;
3417
3418 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3419 VEC_free (probe_p, bp_objfile_data->exception_probes);
3420 }
3421
3422 static void
3423 create_overlay_event_breakpoint (void)
3424 {
3425 struct objfile *objfile;
3426 const char *const func_name = "_ovly_debug_event";
3427
3428 ALL_OBJFILES (objfile)
3429 {
3430 struct breakpoint *b;
3431 struct breakpoint_objfile_data *bp_objfile_data;
3432 CORE_ADDR addr;
3433
3434 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3435
3436 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3437 continue;
3438
3439 if (bp_objfile_data->overlay_msym.minsym == NULL)
3440 {
3441 struct bound_minimal_symbol m;
3442
3443 m = lookup_minimal_symbol_text (func_name, objfile);
3444 if (m.minsym == NULL)
3445 {
3446 /* Avoid future lookups in this objfile. */
3447 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3448 continue;
3449 }
3450 bp_objfile_data->overlay_msym = m;
3451 }
3452
3453 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3454 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3455 bp_overlay_event,
3456 &internal_breakpoint_ops);
3457 b->addr_string = xstrdup (func_name);
3458
3459 if (overlay_debugging == ovly_auto)
3460 {
3461 b->enable_state = bp_enabled;
3462 overlay_events_enabled = 1;
3463 }
3464 else
3465 {
3466 b->enable_state = bp_disabled;
3467 overlay_events_enabled = 0;
3468 }
3469 }
3470 update_global_location_list (UGLL_MAY_INSERT);
3471 }
3472
3473 static void
3474 create_longjmp_master_breakpoint (void)
3475 {
3476 struct program_space *pspace;
3477 struct cleanup *old_chain;
3478
3479 old_chain = save_current_program_space ();
3480
3481 ALL_PSPACES (pspace)
3482 {
3483 struct objfile *objfile;
3484
3485 set_current_program_space (pspace);
3486
3487 ALL_OBJFILES (objfile)
3488 {
3489 int i;
3490 struct gdbarch *gdbarch;
3491 struct breakpoint_objfile_data *bp_objfile_data;
3492
3493 gdbarch = get_objfile_arch (objfile);
3494
3495 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3496
3497 if (!bp_objfile_data->longjmp_searched)
3498 {
3499 VEC (probe_p) *ret;
3500
3501 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3502 if (ret != NULL)
3503 {
3504 /* We are only interested in checking one element. */
3505 struct probe *p = VEC_index (probe_p, ret, 0);
3506
3507 if (!can_evaluate_probe_arguments (p))
3508 {
3509 /* We cannot use the probe interface here, because it does
3510 not know how to evaluate arguments. */
3511 VEC_free (probe_p, ret);
3512 ret = NULL;
3513 }
3514 }
3515 bp_objfile_data->longjmp_probes = ret;
3516 bp_objfile_data->longjmp_searched = 1;
3517 }
3518
3519 if (bp_objfile_data->longjmp_probes != NULL)
3520 {
3521 int i;
3522 struct probe *probe;
3523 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3524
3525 for (i = 0;
3526 VEC_iterate (probe_p,
3527 bp_objfile_data->longjmp_probes,
3528 i, probe);
3529 ++i)
3530 {
3531 struct breakpoint *b;
3532
3533 b = create_internal_breakpoint (gdbarch,
3534 get_probe_address (probe,
3535 objfile),
3536 bp_longjmp_master,
3537 &internal_breakpoint_ops);
3538 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3539 b->enable_state = bp_disabled;
3540 }
3541
3542 continue;
3543 }
3544
3545 if (!gdbarch_get_longjmp_target_p (gdbarch))
3546 continue;
3547
3548 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3549 {
3550 struct breakpoint *b;
3551 const char *func_name;
3552 CORE_ADDR addr;
3553
3554 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3555 continue;
3556
3557 func_name = longjmp_names[i];
3558 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3559 {
3560 struct bound_minimal_symbol m;
3561
3562 m = lookup_minimal_symbol_text (func_name, objfile);
3563 if (m.minsym == NULL)
3564 {
3565 /* Prevent future lookups in this objfile. */
3566 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3567 continue;
3568 }
3569 bp_objfile_data->longjmp_msym[i] = m;
3570 }
3571
3572 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3573 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3574 &internal_breakpoint_ops);
3575 b->addr_string = xstrdup (func_name);
3576 b->enable_state = bp_disabled;
3577 }
3578 }
3579 }
3580 update_global_location_list (UGLL_MAY_INSERT);
3581
3582 do_cleanups (old_chain);
3583 }
3584
3585 /* Create a master std::terminate breakpoint. */
3586 static void
3587 create_std_terminate_master_breakpoint (void)
3588 {
3589 struct program_space *pspace;
3590 struct cleanup *old_chain;
3591 const char *const func_name = "std::terminate()";
3592
3593 old_chain = save_current_program_space ();
3594
3595 ALL_PSPACES (pspace)
3596 {
3597 struct objfile *objfile;
3598 CORE_ADDR addr;
3599
3600 set_current_program_space (pspace);
3601
3602 ALL_OBJFILES (objfile)
3603 {
3604 struct breakpoint *b;
3605 struct breakpoint_objfile_data *bp_objfile_data;
3606
3607 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3608
3609 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3610 continue;
3611
3612 if (bp_objfile_data->terminate_msym.minsym == NULL)
3613 {
3614 struct bound_minimal_symbol m;
3615
3616 m = lookup_minimal_symbol (func_name, NULL, objfile);
3617 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3618 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3619 {
3620 /* Prevent future lookups in this objfile. */
3621 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3622 continue;
3623 }
3624 bp_objfile_data->terminate_msym = m;
3625 }
3626
3627 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3628 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3629 bp_std_terminate_master,
3630 &internal_breakpoint_ops);
3631 b->addr_string = xstrdup (func_name);
3632 b->enable_state = bp_disabled;
3633 }
3634 }
3635
3636 update_global_location_list (UGLL_MAY_INSERT);
3637
3638 do_cleanups (old_chain);
3639 }
3640
3641 /* Install a master breakpoint on the unwinder's debug hook. */
3642
3643 static void
3644 create_exception_master_breakpoint (void)
3645 {
3646 struct objfile *objfile;
3647 const char *const func_name = "_Unwind_DebugHook";
3648
3649 ALL_OBJFILES (objfile)
3650 {
3651 struct breakpoint *b;
3652 struct gdbarch *gdbarch;
3653 struct breakpoint_objfile_data *bp_objfile_data;
3654 CORE_ADDR addr;
3655
3656 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3657
3658 /* We prefer the SystemTap probe point if it exists. */
3659 if (!bp_objfile_data->exception_searched)
3660 {
3661 VEC (probe_p) *ret;
3662
3663 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3664
3665 if (ret != NULL)
3666 {
3667 /* We are only interested in checking one element. */
3668 struct probe *p = VEC_index (probe_p, ret, 0);
3669
3670 if (!can_evaluate_probe_arguments (p))
3671 {
3672 /* We cannot use the probe interface here, because it does
3673 not know how to evaluate arguments. */
3674 VEC_free (probe_p, ret);
3675 ret = NULL;
3676 }
3677 }
3678 bp_objfile_data->exception_probes = ret;
3679 bp_objfile_data->exception_searched = 1;
3680 }
3681
3682 if (bp_objfile_data->exception_probes != NULL)
3683 {
3684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3685 int i;
3686 struct probe *probe;
3687
3688 for (i = 0;
3689 VEC_iterate (probe_p,
3690 bp_objfile_data->exception_probes,
3691 i, probe);
3692 ++i)
3693 {
3694 struct breakpoint *b;
3695
3696 b = create_internal_breakpoint (gdbarch,
3697 get_probe_address (probe,
3698 objfile),
3699 bp_exception_master,
3700 &internal_breakpoint_ops);
3701 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3702 b->enable_state = bp_disabled;
3703 }
3704
3705 continue;
3706 }
3707
3708 /* Otherwise, try the hook function. */
3709
3710 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3711 continue;
3712
3713 gdbarch = get_objfile_arch (objfile);
3714
3715 if (bp_objfile_data->exception_msym.minsym == NULL)
3716 {
3717 struct bound_minimal_symbol debug_hook;
3718
3719 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3720 if (debug_hook.minsym == NULL)
3721 {
3722 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3723 continue;
3724 }
3725
3726 bp_objfile_data->exception_msym = debug_hook;
3727 }
3728
3729 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3730 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3731 &current_target);
3732 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3733 &internal_breakpoint_ops);
3734 b->addr_string = xstrdup (func_name);
3735 b->enable_state = bp_disabled;
3736 }
3737
3738 update_global_location_list (UGLL_MAY_INSERT);
3739 }
3740
3741 void
3742 update_breakpoints_after_exec (void)
3743 {
3744 struct breakpoint *b, *b_tmp;
3745 struct bp_location *bploc, **bplocp_tmp;
3746
3747 /* We're about to delete breakpoints from GDB's lists. If the
3748 INSERTED flag is true, GDB will try to lift the breakpoints by
3749 writing the breakpoints' "shadow contents" back into memory. The
3750 "shadow contents" are NOT valid after an exec, so GDB should not
3751 do that. Instead, the target is responsible from marking
3752 breakpoints out as soon as it detects an exec. We don't do that
3753 here instead, because there may be other attempts to delete
3754 breakpoints after detecting an exec and before reaching here. */
3755 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3756 if (bploc->pspace == current_program_space)
3757 gdb_assert (!bploc->inserted);
3758
3759 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3760 {
3761 if (b->pspace != current_program_space)
3762 continue;
3763
3764 /* Solib breakpoints must be explicitly reset after an exec(). */
3765 if (b->type == bp_shlib_event)
3766 {
3767 delete_breakpoint (b);
3768 continue;
3769 }
3770
3771 /* JIT breakpoints must be explicitly reset after an exec(). */
3772 if (b->type == bp_jit_event)
3773 {
3774 delete_breakpoint (b);
3775 continue;
3776 }
3777
3778 /* Thread event breakpoints must be set anew after an exec(),
3779 as must overlay event and longjmp master breakpoints. */
3780 if (b->type == bp_thread_event || b->type == bp_overlay_event
3781 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3782 || b->type == bp_exception_master)
3783 {
3784 delete_breakpoint (b);
3785 continue;
3786 }
3787
3788 /* Step-resume breakpoints are meaningless after an exec(). */
3789 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3790 {
3791 delete_breakpoint (b);
3792 continue;
3793 }
3794
3795 /* Longjmp and longjmp-resume breakpoints are also meaningless
3796 after an exec. */
3797 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3798 || b->type == bp_longjmp_call_dummy
3799 || b->type == bp_exception || b->type == bp_exception_resume)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
3804
3805 if (b->type == bp_catchpoint)
3806 {
3807 /* For now, none of the bp_catchpoint breakpoints need to
3808 do anything at this point. In the future, if some of
3809 the catchpoints need to something, we will need to add
3810 a new method, and call this method from here. */
3811 continue;
3812 }
3813
3814 /* bp_finish is a special case. The only way we ought to be able
3815 to see one of these when an exec() has happened, is if the user
3816 caught a vfork, and then said "finish". Ordinarily a finish just
3817 carries them to the call-site of the current callee, by setting
3818 a temporary bp there and resuming. But in this case, the finish
3819 will carry them entirely through the vfork & exec.
3820
3821 We don't want to allow a bp_finish to remain inserted now. But
3822 we can't safely delete it, 'cause finish_command has a handle to
3823 the bp on a bpstat, and will later want to delete it. There's a
3824 chance (and I've seen it happen) that if we delete the bp_finish
3825 here, that its storage will get reused by the time finish_command
3826 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3827 We really must allow finish_command to delete a bp_finish.
3828
3829 In the absence of a general solution for the "how do we know
3830 it's safe to delete something others may have handles to?"
3831 problem, what we'll do here is just uninsert the bp_finish, and
3832 let finish_command delete it.
3833
3834 (We know the bp_finish is "doomed" in the sense that it's
3835 momentary, and will be deleted as soon as finish_command sees
3836 the inferior stopped. So it doesn't matter that the bp's
3837 address is probably bogus in the new a.out, unlike e.g., the
3838 solib breakpoints.) */
3839
3840 if (b->type == bp_finish)
3841 {
3842 continue;
3843 }
3844
3845 /* Without a symbolic address, we have little hope of the
3846 pre-exec() address meaning the same thing in the post-exec()
3847 a.out. */
3848 if (b->addr_string == NULL)
3849 {
3850 delete_breakpoint (b);
3851 continue;
3852 }
3853 }
3854 }
3855
3856 int
3857 detach_breakpoints (ptid_t ptid)
3858 {
3859 struct bp_location *bl, **blp_tmp;
3860 int val = 0;
3861 struct cleanup *old_chain = save_inferior_ptid ();
3862 struct inferior *inf = current_inferior ();
3863
3864 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3865 error (_("Cannot detach breakpoints of inferior_ptid"));
3866
3867 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3868 inferior_ptid = ptid;
3869 ALL_BP_LOCATIONS (bl, blp_tmp)
3870 {
3871 if (bl->pspace != inf->pspace)
3872 continue;
3873
3874 /* This function must physically remove breakpoints locations
3875 from the specified ptid, without modifying the breakpoint
3876 package's state. Locations of type bp_loc_other are only
3877 maintained at GDB side. So, there is no need to remove
3878 these bp_loc_other locations. Moreover, removing these
3879 would modify the breakpoint package's state. */
3880 if (bl->loc_type == bp_loc_other)
3881 continue;
3882
3883 if (bl->inserted)
3884 val |= remove_breakpoint_1 (bl, mark_inserted);
3885 }
3886
3887 /* Detach single-step breakpoints as well. */
3888 detach_single_step_breakpoints ();
3889
3890 do_cleanups (old_chain);
3891 return val;
3892 }
3893
3894 /* Remove the breakpoint location BL from the current address space.
3895 Note that this is used to detach breakpoints from a child fork.
3896 When we get here, the child isn't in the inferior list, and neither
3897 do we have objects to represent its address space --- we should
3898 *not* look at bl->pspace->aspace here. */
3899
3900 static int
3901 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3902 {
3903 int val;
3904
3905 /* BL is never in moribund_locations by our callers. */
3906 gdb_assert (bl->owner != NULL);
3907
3908 if (bl->owner->enable_state == bp_permanent)
3909 /* Permanent breakpoints cannot be inserted or removed. */
3910 return 0;
3911
3912 /* The type of none suggests that owner is actually deleted.
3913 This should not ever happen. */
3914 gdb_assert (bl->owner->type != bp_none);
3915
3916 if (bl->loc_type == bp_loc_software_breakpoint
3917 || bl->loc_type == bp_loc_hardware_breakpoint)
3918 {
3919 /* "Normal" instruction breakpoint: either the standard
3920 trap-instruction bp (bp_breakpoint), or a
3921 bp_hardware_breakpoint. */
3922
3923 /* First check to see if we have to handle an overlay. */
3924 if (overlay_debugging == ovly_off
3925 || bl->section == NULL
3926 || !(section_is_overlay (bl->section)))
3927 {
3928 /* No overlay handling: just remove the breakpoint. */
3929
3930 /* If we're trying to uninsert a memory breakpoint that we
3931 know is set in a dynamic object that is marked
3932 shlib_disabled, then either the dynamic object was
3933 removed with "remove-symbol-file" or with
3934 "nosharedlibrary". In the former case, we don't know
3935 whether another dynamic object might have loaded over the
3936 breakpoint's address -- the user might well let us know
3937 about it next with add-symbol-file (the whole point of
3938 add-symbol-file is letting the user manually maintain a
3939 list of dynamically loaded objects). If we have the
3940 breakpoint's shadow memory, that is, this is a software
3941 breakpoint managed by GDB, check whether the breakpoint
3942 is still inserted in memory, to avoid overwriting wrong
3943 code with stale saved shadow contents. Note that HW
3944 breakpoints don't have shadow memory, as they're
3945 implemented using a mechanism that is not dependent on
3946 being able to modify the target's memory, and as such
3947 they should always be removed. */
3948 if (bl->shlib_disabled
3949 && bl->target_info.shadow_len != 0
3950 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3951 val = 0;
3952 else
3953 val = bl->owner->ops->remove_location (bl);
3954 }
3955 else
3956 {
3957 /* This breakpoint is in an overlay section.
3958 Did we set a breakpoint at the LMA? */
3959 if (!overlay_events_enabled)
3960 {
3961 /* Yes -- overlay event support is not active, so we
3962 should have set a breakpoint at the LMA. Remove it.
3963 */
3964 /* Ignore any failures: if the LMA is in ROM, we will
3965 have already warned when we failed to insert it. */
3966 if (bl->loc_type == bp_loc_hardware_breakpoint)
3967 target_remove_hw_breakpoint (bl->gdbarch,
3968 &bl->overlay_target_info);
3969 else
3970 target_remove_breakpoint (bl->gdbarch,
3971 &bl->overlay_target_info);
3972 }
3973 /* Did we set a breakpoint at the VMA?
3974 If so, we will have marked the breakpoint 'inserted'. */
3975 if (bl->inserted)
3976 {
3977 /* Yes -- remove it. Previously we did not bother to
3978 remove the breakpoint if the section had been
3979 unmapped, but let's not rely on that being safe. We
3980 don't know what the overlay manager might do. */
3981
3982 /* However, we should remove *software* breakpoints only
3983 if the section is still mapped, or else we overwrite
3984 wrong code with the saved shadow contents. */
3985 if (bl->loc_type == bp_loc_hardware_breakpoint
3986 || section_is_mapped (bl->section))
3987 val = bl->owner->ops->remove_location (bl);
3988 else
3989 val = 0;
3990 }
3991 else
3992 {
3993 /* No -- not inserted, so no need to remove. No error. */
3994 val = 0;
3995 }
3996 }
3997
3998 /* In some cases, we might not be able to remove a breakpoint in
3999 a shared library that has already been removed, but we have
4000 not yet processed the shlib unload event. Similarly for an
4001 unloaded add-symbol-file object - the user might not yet have
4002 had the chance to remove-symbol-file it. shlib_disabled will
4003 be set if the library/object has already been removed, but
4004 the breakpoint hasn't been uninserted yet, e.g., after
4005 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4006 always-inserted mode. */
4007 if (val
4008 && (bl->loc_type == bp_loc_software_breakpoint
4009 && (bl->shlib_disabled
4010 || solib_name_from_address (bl->pspace, bl->address)
4011 || shared_objfile_contains_address_p (bl->pspace,
4012 bl->address))))
4013 val = 0;
4014
4015 if (val)
4016 return val;
4017 bl->inserted = (is == mark_inserted);
4018 }
4019 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4020 {
4021 gdb_assert (bl->owner->ops != NULL
4022 && bl->owner->ops->remove_location != NULL);
4023
4024 bl->inserted = (is == mark_inserted);
4025 bl->owner->ops->remove_location (bl);
4026
4027 /* Failure to remove any of the hardware watchpoints comes here. */
4028 if ((is == mark_uninserted) && (bl->inserted))
4029 warning (_("Could not remove hardware watchpoint %d."),
4030 bl->owner->number);
4031 }
4032 else if (bl->owner->type == bp_catchpoint
4033 && breakpoint_enabled (bl->owner)
4034 && !bl->duplicate)
4035 {
4036 gdb_assert (bl->owner->ops != NULL
4037 && bl->owner->ops->remove_location != NULL);
4038
4039 val = bl->owner->ops->remove_location (bl);
4040 if (val)
4041 return val;
4042
4043 bl->inserted = (is == mark_inserted);
4044 }
4045
4046 return 0;
4047 }
4048
4049 static int
4050 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4051 {
4052 int ret;
4053 struct cleanup *old_chain;
4054
4055 /* BL is never in moribund_locations by our callers. */
4056 gdb_assert (bl->owner != NULL);
4057
4058 if (bl->owner->enable_state == bp_permanent)
4059 /* Permanent breakpoints cannot be inserted or removed. */
4060 return 0;
4061
4062 /* The type of none suggests that owner is actually deleted.
4063 This should not ever happen. */
4064 gdb_assert (bl->owner->type != bp_none);
4065
4066 old_chain = save_current_space_and_thread ();
4067
4068 switch_to_program_space_and_thread (bl->pspace);
4069
4070 ret = remove_breakpoint_1 (bl, is);
4071
4072 do_cleanups (old_chain);
4073 return ret;
4074 }
4075
4076 /* Clear the "inserted" flag in all breakpoints. */
4077
4078 void
4079 mark_breakpoints_out (void)
4080 {
4081 struct bp_location *bl, **blp_tmp;
4082
4083 ALL_BP_LOCATIONS (bl, blp_tmp)
4084 if (bl->pspace == current_program_space)
4085 bl->inserted = 0;
4086 }
4087
4088 /* Clear the "inserted" flag in all breakpoints and delete any
4089 breakpoints which should go away between runs of the program.
4090
4091 Plus other such housekeeping that has to be done for breakpoints
4092 between runs.
4093
4094 Note: this function gets called at the end of a run (by
4095 generic_mourn_inferior) and when a run begins (by
4096 init_wait_for_inferior). */
4097
4098
4099
4100 void
4101 breakpoint_init_inferior (enum inf_context context)
4102 {
4103 struct breakpoint *b, *b_tmp;
4104 struct bp_location *bl, **blp_tmp;
4105 int ix;
4106 struct program_space *pspace = current_program_space;
4107
4108 /* If breakpoint locations are shared across processes, then there's
4109 nothing to do. */
4110 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4111 return;
4112
4113 ALL_BP_LOCATIONS (bl, blp_tmp)
4114 {
4115 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4116 if (bl->pspace == pspace
4117 && bl->owner->enable_state != bp_permanent)
4118 bl->inserted = 0;
4119 }
4120
4121 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4122 {
4123 if (b->loc && b->loc->pspace != pspace)
4124 continue;
4125
4126 switch (b->type)
4127 {
4128 case bp_call_dummy:
4129 case bp_longjmp_call_dummy:
4130
4131 /* If the call dummy breakpoint is at the entry point it will
4132 cause problems when the inferior is rerun, so we better get
4133 rid of it. */
4134
4135 case bp_watchpoint_scope:
4136
4137 /* Also get rid of scope breakpoints. */
4138
4139 case bp_shlib_event:
4140
4141 /* Also remove solib event breakpoints. Their addresses may
4142 have changed since the last time we ran the program.
4143 Actually we may now be debugging against different target;
4144 and so the solib backend that installed this breakpoint may
4145 not be used in by the target. E.g.,
4146
4147 (gdb) file prog-linux
4148 (gdb) run # native linux target
4149 ...
4150 (gdb) kill
4151 (gdb) file prog-win.exe
4152 (gdb) tar rem :9999 # remote Windows gdbserver.
4153 */
4154
4155 case bp_step_resume:
4156
4157 /* Also remove step-resume breakpoints. */
4158
4159 delete_breakpoint (b);
4160 break;
4161
4162 case bp_watchpoint:
4163 case bp_hardware_watchpoint:
4164 case bp_read_watchpoint:
4165 case bp_access_watchpoint:
4166 {
4167 struct watchpoint *w = (struct watchpoint *) b;
4168
4169 /* Likewise for watchpoints on local expressions. */
4170 if (w->exp_valid_block != NULL)
4171 delete_breakpoint (b);
4172 else if (context == inf_starting)
4173 {
4174 /* Reset val field to force reread of starting value in
4175 insert_breakpoints. */
4176 if (w->val)
4177 value_free (w->val);
4178 w->val = NULL;
4179 w->val_valid = 0;
4180 }
4181 }
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Get rid of the moribund locations. */
4189 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4190 decref_bp_location (&bl);
4191 VEC_free (bp_location_p, moribund_locations);
4192 }
4193
4194 /* These functions concern about actual breakpoints inserted in the
4195 target --- to e.g. check if we need to do decr_pc adjustment or if
4196 we need to hop over the bkpt --- so we check for address space
4197 match, not program space. */
4198
4199 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4200 exists at PC. It returns ordinary_breakpoint_here if it's an
4201 ordinary breakpoint, or permanent_breakpoint_here if it's a
4202 permanent breakpoint.
4203 - When continuing from a location with an ordinary breakpoint, we
4204 actually single step once before calling insert_breakpoints.
4205 - When continuing from a location with a permanent breakpoint, we
4206 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4207 the target, to advance the PC past the breakpoint. */
4208
4209 enum breakpoint_here
4210 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4211 {
4212 struct bp_location *bl, **blp_tmp;
4213 int any_breakpoint_here = 0;
4214
4215 ALL_BP_LOCATIONS (bl, blp_tmp)
4216 {
4217 if (bl->loc_type != bp_loc_software_breakpoint
4218 && bl->loc_type != bp_loc_hardware_breakpoint)
4219 continue;
4220
4221 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4222 if ((breakpoint_enabled (bl->owner)
4223 || bl->owner->enable_state == bp_permanent)
4224 && breakpoint_location_address_match (bl, aspace, pc))
4225 {
4226 if (overlay_debugging
4227 && section_is_overlay (bl->section)
4228 && !section_is_mapped (bl->section))
4229 continue; /* unmapped overlay -- can't be a match */
4230 else if (bl->owner->enable_state == bp_permanent)
4231 return permanent_breakpoint_here;
4232 else
4233 any_breakpoint_here = 1;
4234 }
4235 }
4236
4237 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
4238 }
4239
4240 /* Return true if there's a moribund breakpoint at PC. */
4241
4242 int
4243 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4244 {
4245 struct bp_location *loc;
4246 int ix;
4247
4248 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4249 if (breakpoint_location_address_match (loc, aspace, pc))
4250 return 1;
4251
4252 return 0;
4253 }
4254
4255 /* Returns non-zero if there's a breakpoint inserted at PC, which is
4256 inserted using regular breakpoint_chain / bp_location array
4257 mechanism. This does not check for single-step breakpoints, which
4258 are inserted and removed using direct target manipulation. */
4259
4260 int
4261 regular_breakpoint_inserted_here_p (struct address_space *aspace,
4262 CORE_ADDR pc)
4263 {
4264 struct bp_location *bl, **blp_tmp;
4265
4266 ALL_BP_LOCATIONS (bl, blp_tmp)
4267 {
4268 if (bl->loc_type != bp_loc_software_breakpoint
4269 && bl->loc_type != bp_loc_hardware_breakpoint)
4270 continue;
4271
4272 if (bl->inserted
4273 && breakpoint_location_address_match (bl, aspace, pc))
4274 {
4275 if (overlay_debugging
4276 && section_is_overlay (bl->section)
4277 && !section_is_mapped (bl->section))
4278 continue; /* unmapped overlay -- can't be a match */
4279 else
4280 return 1;
4281 }
4282 }
4283 return 0;
4284 }
4285
4286 /* Returns non-zero iff there's either regular breakpoint
4287 or a single step breakpoint inserted at PC. */
4288
4289 int
4290 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4291 {
4292 if (regular_breakpoint_inserted_here_p (aspace, pc))
4293 return 1;
4294
4295 if (single_step_breakpoint_inserted_here_p (aspace, pc))
4296 return 1;
4297
4298 return 0;
4299 }
4300
4301 /* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4302 software breakpoint inserted at PC. */
4303
4304 static struct bp_location *
4305 find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4306 CORE_ADDR pc)
4307 {
4308 struct bp_location *bl, **blp_tmp;
4309
4310 ALL_BP_LOCATIONS (bl, blp_tmp)
4311 {
4312 if (bl->loc_type != bp_loc_software_breakpoint)
4313 continue;
4314
4315 if (bl->inserted
4316 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4317 aspace, pc))
4318 {
4319 if (overlay_debugging
4320 && section_is_overlay (bl->section)
4321 && !section_is_mapped (bl->section))
4322 continue; /* unmapped overlay -- can't be a match */
4323 else
4324 return bl;
4325 }
4326 }
4327
4328 return NULL;
4329 }
4330
4331 /* This function returns non-zero iff there is a software breakpoint
4332 inserted at PC. */
4333
4334 int
4335 software_breakpoint_inserted_here_p (struct address_space *aspace,
4336 CORE_ADDR pc)
4337 {
4338 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4339 return 1;
4340
4341 /* Also check for software single-step breakpoints. */
4342 if (single_step_breakpoint_inserted_here_p (aspace, pc))
4343 return 1;
4344
4345 return 0;
4346 }
4347
4348 int
4349 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4350 CORE_ADDR addr, ULONGEST len)
4351 {
4352 struct breakpoint *bpt;
4353
4354 ALL_BREAKPOINTS (bpt)
4355 {
4356 struct bp_location *loc;
4357
4358 if (bpt->type != bp_hardware_watchpoint
4359 && bpt->type != bp_access_watchpoint)
4360 continue;
4361
4362 if (!breakpoint_enabled (bpt))
4363 continue;
4364
4365 for (loc = bpt->loc; loc; loc = loc->next)
4366 if (loc->pspace->aspace == aspace && loc->inserted)
4367 {
4368 CORE_ADDR l, h;
4369
4370 /* Check for intersection. */
4371 l = max (loc->address, addr);
4372 h = min (loc->address + loc->length, addr + len);
4373 if (l < h)
4374 return 1;
4375 }
4376 }
4377 return 0;
4378 }
4379
4380 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4381 PC is valid for process/thread PTID. */
4382
4383 int
4384 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4385 ptid_t ptid)
4386 {
4387 struct bp_location *bl, **blp_tmp;
4388 /* The thread and task IDs associated to PTID, computed lazily. */
4389 int thread = -1;
4390 int task = 0;
4391
4392 ALL_BP_LOCATIONS (bl, blp_tmp)
4393 {
4394 if (bl->loc_type != bp_loc_software_breakpoint
4395 && bl->loc_type != bp_loc_hardware_breakpoint)
4396 continue;
4397
4398 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4399 if (!breakpoint_enabled (bl->owner)
4400 && bl->owner->enable_state != bp_permanent)
4401 continue;
4402
4403 if (!breakpoint_location_address_match (bl, aspace, pc))
4404 continue;
4405
4406 if (bl->owner->thread != -1)
4407 {
4408 /* This is a thread-specific breakpoint. Check that ptid
4409 matches that thread. If thread hasn't been computed yet,
4410 it is now time to do so. */
4411 if (thread == -1)
4412 thread = pid_to_thread_id (ptid);
4413 if (bl->owner->thread != thread)
4414 continue;
4415 }
4416
4417 if (bl->owner->task != 0)
4418 {
4419 /* This is a task-specific breakpoint. Check that ptid
4420 matches that task. If task hasn't been computed yet,
4421 it is now time to do so. */
4422 if (task == 0)
4423 task = ada_get_task_number (ptid);
4424 if (bl->owner->task != task)
4425 continue;
4426 }
4427
4428 if (overlay_debugging
4429 && section_is_overlay (bl->section)
4430 && !section_is_mapped (bl->section))
4431 continue; /* unmapped overlay -- can't be a match */
4432
4433 return 1;
4434 }
4435
4436 return 0;
4437 }
4438 \f
4439
4440 /* bpstat stuff. External routines' interfaces are documented
4441 in breakpoint.h. */
4442
4443 int
4444 is_catchpoint (struct breakpoint *ep)
4445 {
4446 return (ep->type == bp_catchpoint);
4447 }
4448
4449 /* Frees any storage that is part of a bpstat. Does not walk the
4450 'next' chain. */
4451
4452 static void
4453 bpstat_free (bpstat bs)
4454 {
4455 if (bs->old_val != NULL)
4456 value_free (bs->old_val);
4457 decref_counted_command_line (&bs->commands);
4458 decref_bp_location (&bs->bp_location_at);
4459 xfree (bs);
4460 }
4461
4462 /* Clear a bpstat so that it says we are not at any breakpoint.
4463 Also free any storage that is part of a bpstat. */
4464
4465 void
4466 bpstat_clear (bpstat *bsp)
4467 {
4468 bpstat p;
4469 bpstat q;
4470
4471 if (bsp == 0)
4472 return;
4473 p = *bsp;
4474 while (p != NULL)
4475 {
4476 q = p->next;
4477 bpstat_free (p);
4478 p = q;
4479 }
4480 *bsp = NULL;
4481 }
4482
4483 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4484 is part of the bpstat is copied as well. */
4485
4486 bpstat
4487 bpstat_copy (bpstat bs)
4488 {
4489 bpstat p = NULL;
4490 bpstat tmp;
4491 bpstat retval = NULL;
4492
4493 if (bs == NULL)
4494 return bs;
4495
4496 for (; bs != NULL; bs = bs->next)
4497 {
4498 tmp = (bpstat) xmalloc (sizeof (*tmp));
4499 memcpy (tmp, bs, sizeof (*tmp));
4500 incref_counted_command_line (tmp->commands);
4501 incref_bp_location (tmp->bp_location_at);
4502 if (bs->old_val != NULL)
4503 {
4504 tmp->old_val = value_copy (bs->old_val);
4505 release_value (tmp->old_val);
4506 }
4507
4508 if (p == NULL)
4509 /* This is the first thing in the chain. */
4510 retval = tmp;
4511 else
4512 p->next = tmp;
4513 p = tmp;
4514 }
4515 p->next = NULL;
4516 return retval;
4517 }
4518
4519 /* Find the bpstat associated with this breakpoint. */
4520
4521 bpstat
4522 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4523 {
4524 if (bsp == NULL)
4525 return NULL;
4526
4527 for (; bsp != NULL; bsp = bsp->next)
4528 {
4529 if (bsp->breakpoint_at == breakpoint)
4530 return bsp;
4531 }
4532 return NULL;
4533 }
4534
4535 /* See breakpoint.h. */
4536
4537 int
4538 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4539 {
4540 for (; bsp != NULL; bsp = bsp->next)
4541 {
4542 if (bsp->breakpoint_at == NULL)
4543 {
4544 /* A moribund location can never explain a signal other than
4545 GDB_SIGNAL_TRAP. */
4546 if (sig == GDB_SIGNAL_TRAP)
4547 return 1;
4548 }
4549 else
4550 {
4551 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4552 sig))
4553 return 1;
4554 }
4555 }
4556
4557 return 0;
4558 }
4559
4560 /* Put in *NUM the breakpoint number of the first breakpoint we are
4561 stopped at. *BSP upon return is a bpstat which points to the
4562 remaining breakpoints stopped at (but which is not guaranteed to be
4563 good for anything but further calls to bpstat_num).
4564
4565 Return 0 if passed a bpstat which does not indicate any breakpoints.
4566 Return -1 if stopped at a breakpoint that has been deleted since
4567 we set it.
4568 Return 1 otherwise. */
4569
4570 int
4571 bpstat_num (bpstat *bsp, int *num)
4572 {
4573 struct breakpoint *b;
4574
4575 if ((*bsp) == NULL)
4576 return 0; /* No more breakpoint values */
4577
4578 /* We assume we'll never have several bpstats that correspond to a
4579 single breakpoint -- otherwise, this function might return the
4580 same number more than once and this will look ugly. */
4581 b = (*bsp)->breakpoint_at;
4582 *bsp = (*bsp)->next;
4583 if (b == NULL)
4584 return -1; /* breakpoint that's been deleted since */
4585
4586 *num = b->number; /* We have its number */
4587 return 1;
4588 }
4589
4590 /* See breakpoint.h. */
4591
4592 void
4593 bpstat_clear_actions (void)
4594 {
4595 struct thread_info *tp;
4596 bpstat bs;
4597
4598 if (ptid_equal (inferior_ptid, null_ptid))
4599 return;
4600
4601 tp = find_thread_ptid (inferior_ptid);
4602 if (tp == NULL)
4603 return;
4604
4605 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4606 {
4607 decref_counted_command_line (&bs->commands);
4608
4609 if (bs->old_val != NULL)
4610 {
4611 value_free (bs->old_val);
4612 bs->old_val = NULL;
4613 }
4614 }
4615 }
4616
4617 /* Called when a command is about to proceed the inferior. */
4618
4619 static void
4620 breakpoint_about_to_proceed (void)
4621 {
4622 if (!ptid_equal (inferior_ptid, null_ptid))
4623 {
4624 struct thread_info *tp = inferior_thread ();
4625
4626 /* Allow inferior function calls in breakpoint commands to not
4627 interrupt the command list. When the call finishes
4628 successfully, the inferior will be standing at the same
4629 breakpoint as if nothing happened. */
4630 if (tp->control.in_infcall)
4631 return;
4632 }
4633
4634 breakpoint_proceeded = 1;
4635 }
4636
4637 /* Stub for cleaning up our state if we error-out of a breakpoint
4638 command. */
4639 static void
4640 cleanup_executing_breakpoints (void *ignore)
4641 {
4642 executing_breakpoint_commands = 0;
4643 }
4644
4645 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4646 or its equivalent. */
4647
4648 static int
4649 command_line_is_silent (struct command_line *cmd)
4650 {
4651 return cmd && (strcmp ("silent", cmd->line) == 0
4652 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4653 }
4654
4655 /* Execute all the commands associated with all the breakpoints at
4656 this location. Any of these commands could cause the process to
4657 proceed beyond this point, etc. We look out for such changes by
4658 checking the global "breakpoint_proceeded" after each command.
4659
4660 Returns true if a breakpoint command resumed the inferior. In that
4661 case, it is the caller's responsibility to recall it again with the
4662 bpstat of the current thread. */
4663
4664 static int
4665 bpstat_do_actions_1 (bpstat *bsp)
4666 {
4667 bpstat bs;
4668 struct cleanup *old_chain;
4669 int again = 0;
4670
4671 /* Avoid endless recursion if a `source' command is contained
4672 in bs->commands. */
4673 if (executing_breakpoint_commands)
4674 return 0;
4675
4676 executing_breakpoint_commands = 1;
4677 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4678
4679 prevent_dont_repeat ();
4680
4681 /* This pointer will iterate over the list of bpstat's. */
4682 bs = *bsp;
4683
4684 breakpoint_proceeded = 0;
4685 for (; bs != NULL; bs = bs->next)
4686 {
4687 struct counted_command_line *ccmd;
4688 struct command_line *cmd;
4689 struct cleanup *this_cmd_tree_chain;
4690
4691 /* Take ownership of the BSP's command tree, if it has one.
4692
4693 The command tree could legitimately contain commands like
4694 'step' and 'next', which call clear_proceed_status, which
4695 frees stop_bpstat's command tree. To make sure this doesn't
4696 free the tree we're executing out from under us, we need to
4697 take ownership of the tree ourselves. Since a given bpstat's
4698 commands are only executed once, we don't need to copy it; we
4699 can clear the pointer in the bpstat, and make sure we free
4700 the tree when we're done. */
4701 ccmd = bs->commands;
4702 bs->commands = NULL;
4703 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4704 cmd = ccmd ? ccmd->commands : NULL;
4705 if (command_line_is_silent (cmd))
4706 {
4707 /* The action has been already done by bpstat_stop_status. */
4708 cmd = cmd->next;
4709 }
4710
4711 while (cmd != NULL)
4712 {
4713 execute_control_command (cmd);
4714
4715 if (breakpoint_proceeded)
4716 break;
4717 else
4718 cmd = cmd->next;
4719 }
4720
4721 /* We can free this command tree now. */
4722 do_cleanups (this_cmd_tree_chain);
4723
4724 if (breakpoint_proceeded)
4725 {
4726 if (target_can_async_p ())
4727 /* If we are in async mode, then the target might be still
4728 running, not stopped at any breakpoint, so nothing for
4729 us to do here -- just return to the event loop. */
4730 ;
4731 else
4732 /* In sync mode, when execute_control_command returns
4733 we're already standing on the next breakpoint.
4734 Breakpoint commands for that stop were not run, since
4735 execute_command does not run breakpoint commands --
4736 only command_line_handler does, but that one is not
4737 involved in execution of breakpoint commands. So, we
4738 can now execute breakpoint commands. It should be
4739 noted that making execute_command do bpstat actions is
4740 not an option -- in this case we'll have recursive
4741 invocation of bpstat for each breakpoint with a
4742 command, and can easily blow up GDB stack. Instead, we
4743 return true, which will trigger the caller to recall us
4744 with the new stop_bpstat. */
4745 again = 1;
4746 break;
4747 }
4748 }
4749 do_cleanups (old_chain);
4750 return again;
4751 }
4752
4753 void
4754 bpstat_do_actions (void)
4755 {
4756 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4757
4758 /* Do any commands attached to breakpoint we are stopped at. */
4759 while (!ptid_equal (inferior_ptid, null_ptid)
4760 && target_has_execution
4761 && !is_exited (inferior_ptid)
4762 && !is_executing (inferior_ptid))
4763 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4764 and only return when it is stopped at the next breakpoint, we
4765 keep doing breakpoint actions until it returns false to
4766 indicate the inferior was not resumed. */
4767 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4768 break;
4769
4770 discard_cleanups (cleanup_if_error);
4771 }
4772
4773 /* Print out the (old or new) value associated with a watchpoint. */
4774
4775 static void
4776 watchpoint_value_print (struct value *val, struct ui_file *stream)
4777 {
4778 if (val == NULL)
4779 fprintf_unfiltered (stream, _("<unreadable>"));
4780 else
4781 {
4782 struct value_print_options opts;
4783 get_user_print_options (&opts);
4784 value_print (val, stream, &opts);
4785 }
4786 }
4787
4788 /* Generic routine for printing messages indicating why we
4789 stopped. The behavior of this function depends on the value
4790 'print_it' in the bpstat structure. Under some circumstances we
4791 may decide not to print anything here and delegate the task to
4792 normal_stop(). */
4793
4794 static enum print_stop_action
4795 print_bp_stop_message (bpstat bs)
4796 {
4797 switch (bs->print_it)
4798 {
4799 case print_it_noop:
4800 /* Nothing should be printed for this bpstat entry. */
4801 return PRINT_UNKNOWN;
4802 break;
4803
4804 case print_it_done:
4805 /* We still want to print the frame, but we already printed the
4806 relevant messages. */
4807 return PRINT_SRC_AND_LOC;
4808 break;
4809
4810 case print_it_normal:
4811 {
4812 struct breakpoint *b = bs->breakpoint_at;
4813
4814 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4815 which has since been deleted. */
4816 if (b == NULL)
4817 return PRINT_UNKNOWN;
4818
4819 /* Normal case. Call the breakpoint's print_it method. */
4820 return b->ops->print_it (bs);
4821 }
4822 break;
4823
4824 default:
4825 internal_error (__FILE__, __LINE__,
4826 _("print_bp_stop_message: unrecognized enum value"));
4827 break;
4828 }
4829 }
4830
4831 /* A helper function that prints a shared library stopped event. */
4832
4833 static void
4834 print_solib_event (int is_catchpoint)
4835 {
4836 int any_deleted
4837 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4838 int any_added
4839 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4840
4841 if (!is_catchpoint)
4842 {
4843 if (any_added || any_deleted)
4844 ui_out_text (current_uiout,
4845 _("Stopped due to shared library event:\n"));
4846 else
4847 ui_out_text (current_uiout,
4848 _("Stopped due to shared library event (no "
4849 "libraries added or removed)\n"));
4850 }
4851
4852 if (ui_out_is_mi_like_p (current_uiout))
4853 ui_out_field_string (current_uiout, "reason",
4854 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4855
4856 if (any_deleted)
4857 {
4858 struct cleanup *cleanup;
4859 char *name;
4860 int ix;
4861
4862 ui_out_text (current_uiout, _(" Inferior unloaded "));
4863 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4864 "removed");
4865 for (ix = 0;
4866 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4867 ix, name);
4868 ++ix)
4869 {
4870 if (ix > 0)
4871 ui_out_text (current_uiout, " ");
4872 ui_out_field_string (current_uiout, "library", name);
4873 ui_out_text (current_uiout, "\n");
4874 }
4875
4876 do_cleanups (cleanup);
4877 }
4878
4879 if (any_added)
4880 {
4881 struct so_list *iter;
4882 int ix;
4883 struct cleanup *cleanup;
4884
4885 ui_out_text (current_uiout, _(" Inferior loaded "));
4886 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4887 "added");
4888 for (ix = 0;
4889 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4890 ix, iter);
4891 ++ix)
4892 {
4893 if (ix > 0)
4894 ui_out_text (current_uiout, " ");
4895 ui_out_field_string (current_uiout, "library", iter->so_name);
4896 ui_out_text (current_uiout, "\n");
4897 }
4898
4899 do_cleanups (cleanup);
4900 }
4901 }
4902
4903 /* Print a message indicating what happened. This is called from
4904 normal_stop(). The input to this routine is the head of the bpstat
4905 list - a list of the eventpoints that caused this stop. KIND is
4906 the target_waitkind for the stopping event. This
4907 routine calls the generic print routine for printing a message
4908 about reasons for stopping. This will print (for example) the
4909 "Breakpoint n," part of the output. The return value of this
4910 routine is one of:
4911
4912 PRINT_UNKNOWN: Means we printed nothing.
4913 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4914 code to print the location. An example is
4915 "Breakpoint 1, " which should be followed by
4916 the location.
4917 PRINT_SRC_ONLY: Means we printed something, but there is no need
4918 to also print the location part of the message.
4919 An example is the catch/throw messages, which
4920 don't require a location appended to the end.
4921 PRINT_NOTHING: We have done some printing and we don't need any
4922 further info to be printed. */
4923
4924 enum print_stop_action
4925 bpstat_print (bpstat bs, int kind)
4926 {
4927 int val;
4928
4929 /* Maybe another breakpoint in the chain caused us to stop.
4930 (Currently all watchpoints go on the bpstat whether hit or not.
4931 That probably could (should) be changed, provided care is taken
4932 with respect to bpstat_explains_signal). */
4933 for (; bs; bs = bs->next)
4934 {
4935 val = print_bp_stop_message (bs);
4936 if (val == PRINT_SRC_ONLY
4937 || val == PRINT_SRC_AND_LOC
4938 || val == PRINT_NOTHING)
4939 return val;
4940 }
4941
4942 /* If we had hit a shared library event breakpoint,
4943 print_bp_stop_message would print out this message. If we hit an
4944 OS-level shared library event, do the same thing. */
4945 if (kind == TARGET_WAITKIND_LOADED)
4946 {
4947 print_solib_event (0);
4948 return PRINT_NOTHING;
4949 }
4950
4951 /* We reached the end of the chain, or we got a null BS to start
4952 with and nothing was printed. */
4953 return PRINT_UNKNOWN;
4954 }
4955
4956 /* Evaluate the expression EXP and return 1 if value is zero.
4957 This returns the inverse of the condition because it is called
4958 from catch_errors which returns 0 if an exception happened, and if an
4959 exception happens we want execution to stop.
4960 The argument is a "struct expression *" that has been cast to a
4961 "void *" to make it pass through catch_errors. */
4962
4963 static int
4964 breakpoint_cond_eval (void *exp)
4965 {
4966 struct value *mark = value_mark ();
4967 int i = !value_true (evaluate_expression ((struct expression *) exp));
4968
4969 value_free_to_mark (mark);
4970 return i;
4971 }
4972
4973 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4974
4975 static bpstat
4976 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4977 {
4978 bpstat bs;
4979
4980 bs = (bpstat) xmalloc (sizeof (*bs));
4981 bs->next = NULL;
4982 **bs_link_pointer = bs;
4983 *bs_link_pointer = &bs->next;
4984 bs->breakpoint_at = bl->owner;
4985 bs->bp_location_at = bl;
4986 incref_bp_location (bl);
4987 /* If the condition is false, etc., don't do the commands. */
4988 bs->commands = NULL;
4989 bs->old_val = NULL;
4990 bs->print_it = print_it_normal;
4991 return bs;
4992 }
4993 \f
4994 /* The target has stopped with waitstatus WS. Check if any hardware
4995 watchpoints have triggered, according to the target. */
4996
4997 int
4998 watchpoints_triggered (struct target_waitstatus *ws)
4999 {
5000 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5001 CORE_ADDR addr;
5002 struct breakpoint *b;
5003
5004 if (!stopped_by_watchpoint)
5005 {
5006 /* We were not stopped by a watchpoint. Mark all watchpoints
5007 as not triggered. */
5008 ALL_BREAKPOINTS (b)
5009 if (is_hardware_watchpoint (b))
5010 {
5011 struct watchpoint *w = (struct watchpoint *) b;
5012
5013 w->watchpoint_triggered = watch_triggered_no;
5014 }
5015
5016 return 0;
5017 }
5018
5019 if (!target_stopped_data_address (&current_target, &addr))
5020 {
5021 /* We were stopped by a watchpoint, but we don't know where.
5022 Mark all watchpoints as unknown. */
5023 ALL_BREAKPOINTS (b)
5024 if (is_hardware_watchpoint (b))
5025 {
5026 struct watchpoint *w = (struct watchpoint *) b;
5027
5028 w->watchpoint_triggered = watch_triggered_unknown;
5029 }
5030
5031 return 1;
5032 }
5033
5034 /* The target could report the data address. Mark watchpoints
5035 affected by this data address as triggered, and all others as not
5036 triggered. */
5037
5038 ALL_BREAKPOINTS (b)
5039 if (is_hardware_watchpoint (b))
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042 struct bp_location *loc;
5043
5044 w->watchpoint_triggered = watch_triggered_no;
5045 for (loc = b->loc; loc; loc = loc->next)
5046 {
5047 if (is_masked_watchpoint (b))
5048 {
5049 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5050 CORE_ADDR start = loc->address & w->hw_wp_mask;
5051
5052 if (newaddr == start)
5053 {
5054 w->watchpoint_triggered = watch_triggered_yes;
5055 break;
5056 }
5057 }
5058 /* Exact match not required. Within range is sufficient. */
5059 else if (target_watchpoint_addr_within_range (&current_target,
5060 addr, loc->address,
5061 loc->length))
5062 {
5063 w->watchpoint_triggered = watch_triggered_yes;
5064 break;
5065 }
5066 }
5067 }
5068
5069 return 1;
5070 }
5071
5072 /* Possible return values for watchpoint_check (this can't be an enum
5073 because of check_errors). */
5074 /* The watchpoint has been deleted. */
5075 #define WP_DELETED 1
5076 /* The value has changed. */
5077 #define WP_VALUE_CHANGED 2
5078 /* The value has not changed. */
5079 #define WP_VALUE_NOT_CHANGED 3
5080 /* Ignore this watchpoint, no matter if the value changed or not. */
5081 #define WP_IGNORE 4
5082
5083 #define BP_TEMPFLAG 1
5084 #define BP_HARDWAREFLAG 2
5085
5086 /* Evaluate watchpoint condition expression and check if its value
5087 changed.
5088
5089 P should be a pointer to struct bpstat, but is defined as a void *
5090 in order for this function to be usable with catch_errors. */
5091
5092 static int
5093 watchpoint_check (void *p)
5094 {
5095 bpstat bs = (bpstat) p;
5096 struct watchpoint *b;
5097 struct frame_info *fr;
5098 int within_current_scope;
5099
5100 /* BS is built from an existing struct breakpoint. */
5101 gdb_assert (bs->breakpoint_at != NULL);
5102 b = (struct watchpoint *) bs->breakpoint_at;
5103
5104 /* If this is a local watchpoint, we only want to check if the
5105 watchpoint frame is in scope if the current thread is the thread
5106 that was used to create the watchpoint. */
5107 if (!watchpoint_in_thread_scope (b))
5108 return WP_IGNORE;
5109
5110 if (b->exp_valid_block == NULL)
5111 within_current_scope = 1;
5112 else
5113 {
5114 struct frame_info *frame = get_current_frame ();
5115 struct gdbarch *frame_arch = get_frame_arch (frame);
5116 CORE_ADDR frame_pc = get_frame_pc (frame);
5117
5118 /* in_function_epilogue_p() returns a non-zero value if we're
5119 still in the function but the stack frame has already been
5120 invalidated. Since we can't rely on the values of local
5121 variables after the stack has been destroyed, we are treating
5122 the watchpoint in that state as `not changed' without further
5123 checking. Don't mark watchpoints as changed if the current
5124 frame is in an epilogue - even if they are in some other
5125 frame, our view of the stack is likely to be wrong and
5126 frame_find_by_id could error out. */
5127 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
5128 return WP_IGNORE;
5129
5130 fr = frame_find_by_id (b->watchpoint_frame);
5131 within_current_scope = (fr != NULL);
5132
5133 /* If we've gotten confused in the unwinder, we might have
5134 returned a frame that can't describe this variable. */
5135 if (within_current_scope)
5136 {
5137 struct symbol *function;
5138
5139 function = get_frame_function (fr);
5140 if (function == NULL
5141 || !contained_in (b->exp_valid_block,
5142 SYMBOL_BLOCK_VALUE (function)))
5143 within_current_scope = 0;
5144 }
5145
5146 if (within_current_scope)
5147 /* If we end up stopping, the current frame will get selected
5148 in normal_stop. So this call to select_frame won't affect
5149 the user. */
5150 select_frame (fr);
5151 }
5152
5153 if (within_current_scope)
5154 {
5155 /* We use value_{,free_to_}mark because it could be a *long*
5156 time before we return to the command level and call
5157 free_all_values. We can't call free_all_values because we
5158 might be in the middle of evaluating a function call. */
5159
5160 int pc = 0;
5161 struct value *mark;
5162 struct value *new_val;
5163
5164 if (is_masked_watchpoint (&b->base))
5165 /* Since we don't know the exact trigger address (from
5166 stopped_data_address), just tell the user we've triggered
5167 a mask watchpoint. */
5168 return WP_VALUE_CHANGED;
5169
5170 mark = value_mark ();
5171 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5172
5173 if (b->val_bitsize != 0)
5174 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5175
5176 /* We use value_equal_contents instead of value_equal because
5177 the latter coerces an array to a pointer, thus comparing just
5178 the address of the array instead of its contents. This is
5179 not what we want. */
5180 if ((b->val != NULL) != (new_val != NULL)
5181 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5182 {
5183 if (new_val != NULL)
5184 {
5185 release_value (new_val);
5186 value_free_to_mark (mark);
5187 }
5188 bs->old_val = b->val;
5189 b->val = new_val;
5190 b->val_valid = 1;
5191 return WP_VALUE_CHANGED;
5192 }
5193 else
5194 {
5195 /* Nothing changed. */
5196 value_free_to_mark (mark);
5197 return WP_VALUE_NOT_CHANGED;
5198 }
5199 }
5200 else
5201 {
5202 struct ui_out *uiout = current_uiout;
5203
5204 /* This seems like the only logical thing to do because
5205 if we temporarily ignored the watchpoint, then when
5206 we reenter the block in which it is valid it contains
5207 garbage (in the case of a function, it may have two
5208 garbage values, one before and one after the prologue).
5209 So we can't even detect the first assignment to it and
5210 watch after that (since the garbage may or may not equal
5211 the first value assigned). */
5212 /* We print all the stop information in
5213 breakpoint_ops->print_it, but in this case, by the time we
5214 call breakpoint_ops->print_it this bp will be deleted
5215 already. So we have no choice but print the information
5216 here. */
5217 if (ui_out_is_mi_like_p (uiout))
5218 ui_out_field_string
5219 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5220 ui_out_text (uiout, "\nWatchpoint ");
5221 ui_out_field_int (uiout, "wpnum", b->base.number);
5222 ui_out_text (uiout,
5223 " deleted because the program has left the block in\n\
5224 which its expression is valid.\n");
5225
5226 /* Make sure the watchpoint's commands aren't executed. */
5227 decref_counted_command_line (&b->base.commands);
5228 watchpoint_del_at_next_stop (b);
5229
5230 return WP_DELETED;
5231 }
5232 }
5233
5234 /* Return true if it looks like target has stopped due to hitting
5235 breakpoint location BL. This function does not check if we should
5236 stop, only if BL explains the stop. */
5237
5238 static int
5239 bpstat_check_location (const struct bp_location *bl,
5240 struct address_space *aspace, CORE_ADDR bp_addr,
5241 const struct target_waitstatus *ws)
5242 {
5243 struct breakpoint *b = bl->owner;
5244
5245 /* BL is from an existing breakpoint. */
5246 gdb_assert (b != NULL);
5247
5248 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5249 }
5250
5251 /* Determine if the watched values have actually changed, and we
5252 should stop. If not, set BS->stop to 0. */
5253
5254 static void
5255 bpstat_check_watchpoint (bpstat bs)
5256 {
5257 const struct bp_location *bl;
5258 struct watchpoint *b;
5259
5260 /* BS is built for existing struct breakpoint. */
5261 bl = bs->bp_location_at;
5262 gdb_assert (bl != NULL);
5263 b = (struct watchpoint *) bs->breakpoint_at;
5264 gdb_assert (b != NULL);
5265
5266 {
5267 int must_check_value = 0;
5268
5269 if (b->base.type == bp_watchpoint)
5270 /* For a software watchpoint, we must always check the
5271 watched value. */
5272 must_check_value = 1;
5273 else if (b->watchpoint_triggered == watch_triggered_yes)
5274 /* We have a hardware watchpoint (read, write, or access)
5275 and the target earlier reported an address watched by
5276 this watchpoint. */
5277 must_check_value = 1;
5278 else if (b->watchpoint_triggered == watch_triggered_unknown
5279 && b->base.type == bp_hardware_watchpoint)
5280 /* We were stopped by a hardware watchpoint, but the target could
5281 not report the data address. We must check the watchpoint's
5282 value. Access and read watchpoints are out of luck; without
5283 a data address, we can't figure it out. */
5284 must_check_value = 1;
5285
5286 if (must_check_value)
5287 {
5288 char *message
5289 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5290 b->base.number);
5291 struct cleanup *cleanups = make_cleanup (xfree, message);
5292 int e = catch_errors (watchpoint_check, bs, message,
5293 RETURN_MASK_ALL);
5294 do_cleanups (cleanups);
5295 switch (e)
5296 {
5297 case WP_DELETED:
5298 /* We've already printed what needs to be printed. */
5299 bs->print_it = print_it_done;
5300 /* Stop. */
5301 break;
5302 case WP_IGNORE:
5303 bs->print_it = print_it_noop;
5304 bs->stop = 0;
5305 break;
5306 case WP_VALUE_CHANGED:
5307 if (b->base.type == bp_read_watchpoint)
5308 {
5309 /* There are two cases to consider here:
5310
5311 1. We're watching the triggered memory for reads.
5312 In that case, trust the target, and always report
5313 the watchpoint hit to the user. Even though
5314 reads don't cause value changes, the value may
5315 have changed since the last time it was read, and
5316 since we're not trapping writes, we will not see
5317 those, and as such we should ignore our notion of
5318 old value.
5319
5320 2. We're watching the triggered memory for both
5321 reads and writes. There are two ways this may
5322 happen:
5323
5324 2.1. This is a target that can't break on data
5325 reads only, but can break on accesses (reads or
5326 writes), such as e.g., x86. We detect this case
5327 at the time we try to insert read watchpoints.
5328
5329 2.2. Otherwise, the target supports read
5330 watchpoints, but, the user set an access or write
5331 watchpoint watching the same memory as this read
5332 watchpoint.
5333
5334 If we're watching memory writes as well as reads,
5335 ignore watchpoint hits when we find that the
5336 value hasn't changed, as reads don't cause
5337 changes. This still gives false positives when
5338 the program writes the same value to memory as
5339 what there was already in memory (we will confuse
5340 it for a read), but it's much better than
5341 nothing. */
5342
5343 int other_write_watchpoint = 0;
5344
5345 if (bl->watchpoint_type == hw_read)
5346 {
5347 struct breakpoint *other_b;
5348
5349 ALL_BREAKPOINTS (other_b)
5350 if (other_b->type == bp_hardware_watchpoint
5351 || other_b->type == bp_access_watchpoint)
5352 {
5353 struct watchpoint *other_w =
5354 (struct watchpoint *) other_b;
5355
5356 if (other_w->watchpoint_triggered
5357 == watch_triggered_yes)
5358 {
5359 other_write_watchpoint = 1;
5360 break;
5361 }
5362 }
5363 }
5364
5365 if (other_write_watchpoint
5366 || bl->watchpoint_type == hw_access)
5367 {
5368 /* We're watching the same memory for writes,
5369 and the value changed since the last time we
5370 updated it, so this trap must be for a write.
5371 Ignore it. */
5372 bs->print_it = print_it_noop;
5373 bs->stop = 0;
5374 }
5375 }
5376 break;
5377 case WP_VALUE_NOT_CHANGED:
5378 if (b->base.type == bp_hardware_watchpoint
5379 || b->base.type == bp_watchpoint)
5380 {
5381 /* Don't stop: write watchpoints shouldn't fire if
5382 the value hasn't changed. */
5383 bs->print_it = print_it_noop;
5384 bs->stop = 0;
5385 }
5386 /* Stop. */
5387 break;
5388 default:
5389 /* Can't happen. */
5390 case 0:
5391 /* Error from catch_errors. */
5392 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5393 watchpoint_del_at_next_stop (b);
5394 /* We've already printed what needs to be printed. */
5395 bs->print_it = print_it_done;
5396 break;
5397 }
5398 }
5399 else /* must_check_value == 0 */
5400 {
5401 /* This is a case where some watchpoint(s) triggered, but
5402 not at the address of this watchpoint, or else no
5403 watchpoint triggered after all. So don't print
5404 anything for this watchpoint. */
5405 bs->print_it = print_it_noop;
5406 bs->stop = 0;
5407 }
5408 }
5409 }
5410
5411 /* For breakpoints that are currently marked as telling gdb to stop,
5412 check conditions (condition proper, frame, thread and ignore count)
5413 of breakpoint referred to by BS. If we should not stop for this
5414 breakpoint, set BS->stop to 0. */
5415
5416 static void
5417 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5418 {
5419 const struct bp_location *bl;
5420 struct breakpoint *b;
5421 int value_is_zero = 0;
5422 struct expression *cond;
5423
5424 gdb_assert (bs->stop);
5425
5426 /* BS is built for existing struct breakpoint. */
5427 bl = bs->bp_location_at;
5428 gdb_assert (bl != NULL);
5429 b = bs->breakpoint_at;
5430 gdb_assert (b != NULL);
5431
5432 /* Even if the target evaluated the condition on its end and notified GDB, we
5433 need to do so again since GDB does not know if we stopped due to a
5434 breakpoint or a single step breakpoint. */
5435
5436 if (frame_id_p (b->frame_id)
5437 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5438 {
5439 bs->stop = 0;
5440 return;
5441 }
5442
5443 /* If this is a thread/task-specific breakpoint, don't waste cpu
5444 evaluating the condition if this isn't the specified
5445 thread/task. */
5446 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5447 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5448
5449 {
5450 bs->stop = 0;
5451 return;
5452 }
5453
5454 /* Evaluate extension language breakpoints that have a "stop" method
5455 implemented. */
5456 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5457
5458 if (is_watchpoint (b))
5459 {
5460 struct watchpoint *w = (struct watchpoint *) b;
5461
5462 cond = w->cond_exp;
5463 }
5464 else
5465 cond = bl->cond;
5466
5467 if (cond && b->disposition != disp_del_at_next_stop)
5468 {
5469 int within_current_scope = 1;
5470 struct watchpoint * w;
5471
5472 /* We use value_mark and value_free_to_mark because it could
5473 be a long time before we return to the command level and
5474 call free_all_values. We can't call free_all_values
5475 because we might be in the middle of evaluating a
5476 function call. */
5477 struct value *mark = value_mark ();
5478
5479 if (is_watchpoint (b))
5480 w = (struct watchpoint *) b;
5481 else
5482 w = NULL;
5483
5484 /* Need to select the frame, with all that implies so that
5485 the conditions will have the right context. Because we
5486 use the frame, we will not see an inlined function's
5487 variables when we arrive at a breakpoint at the start
5488 of the inlined function; the current frame will be the
5489 call site. */
5490 if (w == NULL || w->cond_exp_valid_block == NULL)
5491 select_frame (get_current_frame ());
5492 else
5493 {
5494 struct frame_info *frame;
5495
5496 /* For local watchpoint expressions, which particular
5497 instance of a local is being watched matters, so we
5498 keep track of the frame to evaluate the expression
5499 in. To evaluate the condition however, it doesn't
5500 really matter which instantiation of the function
5501 where the condition makes sense triggers the
5502 watchpoint. This allows an expression like "watch
5503 global if q > 10" set in `func', catch writes to
5504 global on all threads that call `func', or catch
5505 writes on all recursive calls of `func' by a single
5506 thread. We simply always evaluate the condition in
5507 the innermost frame that's executing where it makes
5508 sense to evaluate the condition. It seems
5509 intuitive. */
5510 frame = block_innermost_frame (w->cond_exp_valid_block);
5511 if (frame != NULL)
5512 select_frame (frame);
5513 else
5514 within_current_scope = 0;
5515 }
5516 if (within_current_scope)
5517 value_is_zero
5518 = catch_errors (breakpoint_cond_eval, cond,
5519 "Error in testing breakpoint condition:\n",
5520 RETURN_MASK_ALL);
5521 else
5522 {
5523 warning (_("Watchpoint condition cannot be tested "
5524 "in the current scope"));
5525 /* If we failed to set the right context for this
5526 watchpoint, unconditionally report it. */
5527 value_is_zero = 0;
5528 }
5529 /* FIXME-someday, should give breakpoint #. */
5530 value_free_to_mark (mark);
5531 }
5532
5533 if (cond && value_is_zero)
5534 {
5535 bs->stop = 0;
5536 }
5537 else if (b->ignore_count > 0)
5538 {
5539 b->ignore_count--;
5540 bs->stop = 0;
5541 /* Increase the hit count even though we don't stop. */
5542 ++(b->hit_count);
5543 observer_notify_breakpoint_modified (b);
5544 }
5545 }
5546
5547
5548 /* Get a bpstat associated with having just stopped at address
5549 BP_ADDR in thread PTID.
5550
5551 Determine whether we stopped at a breakpoint, etc, or whether we
5552 don't understand this stop. Result is a chain of bpstat's such
5553 that:
5554
5555 if we don't understand the stop, the result is a null pointer.
5556
5557 if we understand why we stopped, the result is not null.
5558
5559 Each element of the chain refers to a particular breakpoint or
5560 watchpoint at which we have stopped. (We may have stopped for
5561 several reasons concurrently.)
5562
5563 Each element of the chain has valid next, breakpoint_at,
5564 commands, FIXME??? fields. */
5565
5566 bpstat
5567 bpstat_stop_status (struct address_space *aspace,
5568 CORE_ADDR bp_addr, ptid_t ptid,
5569 const struct target_waitstatus *ws)
5570 {
5571 struct breakpoint *b = NULL;
5572 struct bp_location *bl;
5573 struct bp_location *loc;
5574 /* First item of allocated bpstat's. */
5575 bpstat bs_head = NULL, *bs_link = &bs_head;
5576 /* Pointer to the last thing in the chain currently. */
5577 bpstat bs;
5578 int ix;
5579 int need_remove_insert;
5580 int removed_any;
5581
5582 /* First, build the bpstat chain with locations that explain a
5583 target stop, while being careful to not set the target running,
5584 as that may invalidate locations (in particular watchpoint
5585 locations are recreated). Resuming will happen here with
5586 breakpoint conditions or watchpoint expressions that include
5587 inferior function calls. */
5588
5589 ALL_BREAKPOINTS (b)
5590 {
5591 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5592 continue;
5593
5594 for (bl = b->loc; bl != NULL; bl = bl->next)
5595 {
5596 /* For hardware watchpoints, we look only at the first
5597 location. The watchpoint_check function will work on the
5598 entire expression, not the individual locations. For
5599 read watchpoints, the watchpoints_triggered function has
5600 checked all locations already. */
5601 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5602 break;
5603
5604 if (!bl->enabled || bl->shlib_disabled)
5605 continue;
5606
5607 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5608 continue;
5609
5610 /* Come here if it's a watchpoint, or if the break address
5611 matches. */
5612
5613 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5614 explain stop. */
5615
5616 /* Assume we stop. Should we find a watchpoint that is not
5617 actually triggered, or if the condition of the breakpoint
5618 evaluates as false, we'll reset 'stop' to 0. */
5619 bs->stop = 1;
5620 bs->print = 1;
5621
5622 /* If this is a scope breakpoint, mark the associated
5623 watchpoint as triggered so that we will handle the
5624 out-of-scope event. We'll get to the watchpoint next
5625 iteration. */
5626 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5627 {
5628 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5629
5630 w->watchpoint_triggered = watch_triggered_yes;
5631 }
5632 }
5633 }
5634
5635 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5636 {
5637 if (breakpoint_location_address_match (loc, aspace, bp_addr))
5638 {
5639 bs = bpstat_alloc (loc, &bs_link);
5640 /* For hits of moribund locations, we should just proceed. */
5641 bs->stop = 0;
5642 bs->print = 0;
5643 bs->print_it = print_it_noop;
5644 }
5645 }
5646
5647 /* A bit of special processing for shlib breakpoints. We need to
5648 process solib loading here, so that the lists of loaded and
5649 unloaded libraries are correct before we handle "catch load" and
5650 "catch unload". */
5651 for (bs = bs_head; bs != NULL; bs = bs->next)
5652 {
5653 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5654 {
5655 handle_solib_event ();
5656 break;
5657 }
5658 }
5659
5660 /* Now go through the locations that caused the target to stop, and
5661 check whether we're interested in reporting this stop to higher
5662 layers, or whether we should resume the target transparently. */
5663
5664 removed_any = 0;
5665
5666 for (bs = bs_head; bs != NULL; bs = bs->next)
5667 {
5668 if (!bs->stop)
5669 continue;
5670
5671 b = bs->breakpoint_at;
5672 b->ops->check_status (bs);
5673 if (bs->stop)
5674 {
5675 bpstat_check_breakpoint_conditions (bs, ptid);
5676
5677 if (bs->stop)
5678 {
5679 ++(b->hit_count);
5680 observer_notify_breakpoint_modified (b);
5681
5682 /* We will stop here. */
5683 if (b->disposition == disp_disable)
5684 {
5685 --(b->enable_count);
5686 if (b->enable_count <= 0
5687 && b->enable_state != bp_permanent)
5688 b->enable_state = bp_disabled;
5689 removed_any = 1;
5690 }
5691 if (b->silent)
5692 bs->print = 0;
5693 bs->commands = b->commands;
5694 incref_counted_command_line (bs->commands);
5695 if (command_line_is_silent (bs->commands
5696 ? bs->commands->commands : NULL))
5697 bs->print = 0;
5698
5699 b->ops->after_condition_true (bs);
5700 }
5701
5702 }
5703
5704 /* Print nothing for this entry if we don't stop or don't
5705 print. */
5706 if (!bs->stop || !bs->print)
5707 bs->print_it = print_it_noop;
5708 }
5709
5710 /* If we aren't stopping, the value of some hardware watchpoint may
5711 not have changed, but the intermediate memory locations we are
5712 watching may have. Don't bother if we're stopping; this will get
5713 done later. */
5714 need_remove_insert = 0;
5715 if (! bpstat_causes_stop (bs_head))
5716 for (bs = bs_head; bs != NULL; bs = bs->next)
5717 if (!bs->stop
5718 && bs->breakpoint_at
5719 && is_hardware_watchpoint (bs->breakpoint_at))
5720 {
5721 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5722
5723 update_watchpoint (w, 0 /* don't reparse. */);
5724 need_remove_insert = 1;
5725 }
5726
5727 if (need_remove_insert)
5728 update_global_location_list (UGLL_MAY_INSERT);
5729 else if (removed_any)
5730 update_global_location_list (UGLL_DONT_INSERT);
5731
5732 return bs_head;
5733 }
5734
5735 static void
5736 handle_jit_event (void)
5737 {
5738 struct frame_info *frame;
5739 struct gdbarch *gdbarch;
5740
5741 /* Switch terminal for any messages produced by
5742 breakpoint_re_set. */
5743 target_terminal_ours_for_output ();
5744
5745 frame = get_current_frame ();
5746 gdbarch = get_frame_arch (frame);
5747
5748 jit_event_handler (gdbarch);
5749
5750 target_terminal_inferior ();
5751 }
5752
5753 /* Prepare WHAT final decision for infrun. */
5754
5755 /* Decide what infrun needs to do with this bpstat. */
5756
5757 struct bpstat_what
5758 bpstat_what (bpstat bs_head)
5759 {
5760 struct bpstat_what retval;
5761 int jit_event = 0;
5762 bpstat bs;
5763
5764 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5765 retval.call_dummy = STOP_NONE;
5766 retval.is_longjmp = 0;
5767
5768 for (bs = bs_head; bs != NULL; bs = bs->next)
5769 {
5770 /* Extract this BS's action. After processing each BS, we check
5771 if its action overrides all we've seem so far. */
5772 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5773 enum bptype bptype;
5774
5775 if (bs->breakpoint_at == NULL)
5776 {
5777 /* I suspect this can happen if it was a momentary
5778 breakpoint which has since been deleted. */
5779 bptype = bp_none;
5780 }
5781 else
5782 bptype = bs->breakpoint_at->type;
5783
5784 switch (bptype)
5785 {
5786 case bp_none:
5787 break;
5788 case bp_breakpoint:
5789 case bp_hardware_breakpoint:
5790 case bp_until:
5791 case bp_finish:
5792 case bp_shlib_event:
5793 if (bs->stop)
5794 {
5795 if (bs->print)
5796 this_action = BPSTAT_WHAT_STOP_NOISY;
5797 else
5798 this_action = BPSTAT_WHAT_STOP_SILENT;
5799 }
5800 else
5801 this_action = BPSTAT_WHAT_SINGLE;
5802 break;
5803 case bp_watchpoint:
5804 case bp_hardware_watchpoint:
5805 case bp_read_watchpoint:
5806 case bp_access_watchpoint:
5807 if (bs->stop)
5808 {
5809 if (bs->print)
5810 this_action = BPSTAT_WHAT_STOP_NOISY;
5811 else
5812 this_action = BPSTAT_WHAT_STOP_SILENT;
5813 }
5814 else
5815 {
5816 /* There was a watchpoint, but we're not stopping.
5817 This requires no further action. */
5818 }
5819 break;
5820 case bp_longjmp:
5821 case bp_longjmp_call_dummy:
5822 case bp_exception:
5823 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5824 retval.is_longjmp = bptype != bp_exception;
5825 break;
5826 case bp_longjmp_resume:
5827 case bp_exception_resume:
5828 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5829 retval.is_longjmp = bptype == bp_longjmp_resume;
5830 break;
5831 case bp_step_resume:
5832 if (bs->stop)
5833 this_action = BPSTAT_WHAT_STEP_RESUME;
5834 else
5835 {
5836 /* It is for the wrong frame. */
5837 this_action = BPSTAT_WHAT_SINGLE;
5838 }
5839 break;
5840 case bp_hp_step_resume:
5841 if (bs->stop)
5842 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5843 else
5844 {
5845 /* It is for the wrong frame. */
5846 this_action = BPSTAT_WHAT_SINGLE;
5847 }
5848 break;
5849 case bp_watchpoint_scope:
5850 case bp_thread_event:
5851 case bp_overlay_event:
5852 case bp_longjmp_master:
5853 case bp_std_terminate_master:
5854 case bp_exception_master:
5855 this_action = BPSTAT_WHAT_SINGLE;
5856 break;
5857 case bp_catchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
5861 this_action = BPSTAT_WHAT_STOP_NOISY;
5862 else
5863 this_action = BPSTAT_WHAT_STOP_SILENT;
5864 }
5865 else
5866 {
5867 /* There was a catchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
5870 break;
5871 case bp_jit_event:
5872 jit_event = 1;
5873 this_action = BPSTAT_WHAT_SINGLE;
5874 break;
5875 case bp_call_dummy:
5876 /* Make sure the action is stop (silent or noisy),
5877 so infrun.c pops the dummy frame. */
5878 retval.call_dummy = STOP_STACK_DUMMY;
5879 this_action = BPSTAT_WHAT_STOP_SILENT;
5880 break;
5881 case bp_std_terminate:
5882 /* Make sure the action is stop (silent or noisy),
5883 so infrun.c pops the dummy frame. */
5884 retval.call_dummy = STOP_STD_TERMINATE;
5885 this_action = BPSTAT_WHAT_STOP_SILENT;
5886 break;
5887 case bp_tracepoint:
5888 case bp_fast_tracepoint:
5889 case bp_static_tracepoint:
5890 /* Tracepoint hits should not be reported back to GDB, and
5891 if one got through somehow, it should have been filtered
5892 out already. */
5893 internal_error (__FILE__, __LINE__,
5894 _("bpstat_what: tracepoint encountered"));
5895 break;
5896 case bp_gnu_ifunc_resolver:
5897 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 break;
5900 case bp_gnu_ifunc_resolver_return:
5901 /* The breakpoint will be removed, execution will restart from the
5902 PC of the former breakpoint. */
5903 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5904 break;
5905
5906 case bp_dprintf:
5907 if (bs->stop)
5908 this_action = BPSTAT_WHAT_STOP_SILENT;
5909 else
5910 this_action = BPSTAT_WHAT_SINGLE;
5911 break;
5912
5913 default:
5914 internal_error (__FILE__, __LINE__,
5915 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5916 }
5917
5918 retval.main_action = max (retval.main_action, this_action);
5919 }
5920
5921 /* These operations may affect the bs->breakpoint_at state so they are
5922 delayed after MAIN_ACTION is decided above. */
5923
5924 if (jit_event)
5925 {
5926 if (debug_infrun)
5927 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5928
5929 handle_jit_event ();
5930 }
5931
5932 for (bs = bs_head; bs != NULL; bs = bs->next)
5933 {
5934 struct breakpoint *b = bs->breakpoint_at;
5935
5936 if (b == NULL)
5937 continue;
5938 switch (b->type)
5939 {
5940 case bp_gnu_ifunc_resolver:
5941 gnu_ifunc_resolver_stop (b);
5942 break;
5943 case bp_gnu_ifunc_resolver_return:
5944 gnu_ifunc_resolver_return_stop (b);
5945 break;
5946 }
5947 }
5948
5949 return retval;
5950 }
5951
5952 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5953 without hardware support). This isn't related to a specific bpstat,
5954 just to things like whether watchpoints are set. */
5955
5956 int
5957 bpstat_should_step (void)
5958 {
5959 struct breakpoint *b;
5960
5961 ALL_BREAKPOINTS (b)
5962 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5963 return 1;
5964 return 0;
5965 }
5966
5967 int
5968 bpstat_causes_stop (bpstat bs)
5969 {
5970 for (; bs != NULL; bs = bs->next)
5971 if (bs->stop)
5972 return 1;
5973
5974 return 0;
5975 }
5976
5977 \f
5978
5979 /* Compute a string of spaces suitable to indent the next line
5980 so it starts at the position corresponding to the table column
5981 named COL_NAME in the currently active table of UIOUT. */
5982
5983 static char *
5984 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5985 {
5986 static char wrap_indent[80];
5987 int i, total_width, width, align;
5988 char *text;
5989
5990 total_width = 0;
5991 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5992 {
5993 if (strcmp (text, col_name) == 0)
5994 {
5995 gdb_assert (total_width < sizeof wrap_indent);
5996 memset (wrap_indent, ' ', total_width);
5997 wrap_indent[total_width] = 0;
5998
5999 return wrap_indent;
6000 }
6001
6002 total_width += width + 1;
6003 }
6004
6005 return NULL;
6006 }
6007
6008 /* Determine if the locations of this breakpoint will have their conditions
6009 evaluated by the target, host or a mix of both. Returns the following:
6010
6011 "host": Host evals condition.
6012 "host or target": Host or Target evals condition.
6013 "target": Target evals condition.
6014 */
6015
6016 static const char *
6017 bp_condition_evaluator (struct breakpoint *b)
6018 {
6019 struct bp_location *bl;
6020 char host_evals = 0;
6021 char target_evals = 0;
6022
6023 if (!b)
6024 return NULL;
6025
6026 if (!is_breakpoint (b))
6027 return NULL;
6028
6029 if (gdb_evaluates_breakpoint_condition_p ()
6030 || !target_supports_evaluation_of_breakpoint_conditions ())
6031 return condition_evaluation_host;
6032
6033 for (bl = b->loc; bl; bl = bl->next)
6034 {
6035 if (bl->cond_bytecode)
6036 target_evals++;
6037 else
6038 host_evals++;
6039 }
6040
6041 if (host_evals && target_evals)
6042 return condition_evaluation_both;
6043 else if (target_evals)
6044 return condition_evaluation_target;
6045 else
6046 return condition_evaluation_host;
6047 }
6048
6049 /* Determine the breakpoint location's condition evaluator. This is
6050 similar to bp_condition_evaluator, but for locations. */
6051
6052 static const char *
6053 bp_location_condition_evaluator (struct bp_location *bl)
6054 {
6055 if (bl && !is_breakpoint (bl->owner))
6056 return NULL;
6057
6058 if (gdb_evaluates_breakpoint_condition_p ()
6059 || !target_supports_evaluation_of_breakpoint_conditions ())
6060 return condition_evaluation_host;
6061
6062 if (bl && bl->cond_bytecode)
6063 return condition_evaluation_target;
6064 else
6065 return condition_evaluation_host;
6066 }
6067
6068 /* Print the LOC location out of the list of B->LOC locations. */
6069
6070 static void
6071 print_breakpoint_location (struct breakpoint *b,
6072 struct bp_location *loc)
6073 {
6074 struct ui_out *uiout = current_uiout;
6075 struct cleanup *old_chain = save_current_program_space ();
6076
6077 if (loc != NULL && loc->shlib_disabled)
6078 loc = NULL;
6079
6080 if (loc != NULL)
6081 set_current_program_space (loc->pspace);
6082
6083 if (b->display_canonical)
6084 ui_out_field_string (uiout, "what", b->addr_string);
6085 else if (loc && loc->symtab)
6086 {
6087 struct symbol *sym
6088 = find_pc_sect_function (loc->address, loc->section);
6089 if (sym)
6090 {
6091 ui_out_text (uiout, "in ");
6092 ui_out_field_string (uiout, "func",
6093 SYMBOL_PRINT_NAME (sym));
6094 ui_out_text (uiout, " ");
6095 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6096 ui_out_text (uiout, "at ");
6097 }
6098 ui_out_field_string (uiout, "file",
6099 symtab_to_filename_for_display (loc->symtab));
6100 ui_out_text (uiout, ":");
6101
6102 if (ui_out_is_mi_like_p (uiout))
6103 ui_out_field_string (uiout, "fullname",
6104 symtab_to_fullname (loc->symtab));
6105
6106 ui_out_field_int (uiout, "line", loc->line_number);
6107 }
6108 else if (loc)
6109 {
6110 struct ui_file *stb = mem_fileopen ();
6111 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6112
6113 print_address_symbolic (loc->gdbarch, loc->address, stb,
6114 demangle, "");
6115 ui_out_field_stream (uiout, "at", stb);
6116
6117 do_cleanups (stb_chain);
6118 }
6119 else
6120 ui_out_field_string (uiout, "pending", b->addr_string);
6121
6122 if (loc && is_breakpoint (b)
6123 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6124 && bp_condition_evaluator (b) == condition_evaluation_both)
6125 {
6126 ui_out_text (uiout, " (");
6127 ui_out_field_string (uiout, "evaluated-by",
6128 bp_location_condition_evaluator (loc));
6129 ui_out_text (uiout, ")");
6130 }
6131
6132 do_cleanups (old_chain);
6133 }
6134
6135 static const char *
6136 bptype_string (enum bptype type)
6137 {
6138 struct ep_type_description
6139 {
6140 enum bptype type;
6141 char *description;
6142 };
6143 static struct ep_type_description bptypes[] =
6144 {
6145 {bp_none, "?deleted?"},
6146 {bp_breakpoint, "breakpoint"},
6147 {bp_hardware_breakpoint, "hw breakpoint"},
6148 {bp_until, "until"},
6149 {bp_finish, "finish"},
6150 {bp_watchpoint, "watchpoint"},
6151 {bp_hardware_watchpoint, "hw watchpoint"},
6152 {bp_read_watchpoint, "read watchpoint"},
6153 {bp_access_watchpoint, "acc watchpoint"},
6154 {bp_longjmp, "longjmp"},
6155 {bp_longjmp_resume, "longjmp resume"},
6156 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6157 {bp_exception, "exception"},
6158 {bp_exception_resume, "exception resume"},
6159 {bp_step_resume, "step resume"},
6160 {bp_hp_step_resume, "high-priority step resume"},
6161 {bp_watchpoint_scope, "watchpoint scope"},
6162 {bp_call_dummy, "call dummy"},
6163 {bp_std_terminate, "std::terminate"},
6164 {bp_shlib_event, "shlib events"},
6165 {bp_thread_event, "thread events"},
6166 {bp_overlay_event, "overlay events"},
6167 {bp_longjmp_master, "longjmp master"},
6168 {bp_std_terminate_master, "std::terminate master"},
6169 {bp_exception_master, "exception master"},
6170 {bp_catchpoint, "catchpoint"},
6171 {bp_tracepoint, "tracepoint"},
6172 {bp_fast_tracepoint, "fast tracepoint"},
6173 {bp_static_tracepoint, "static tracepoint"},
6174 {bp_dprintf, "dprintf"},
6175 {bp_jit_event, "jit events"},
6176 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6177 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6178 };
6179
6180 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6181 || ((int) type != bptypes[(int) type].type))
6182 internal_error (__FILE__, __LINE__,
6183 _("bptypes table does not describe type #%d."),
6184 (int) type);
6185
6186 return bptypes[(int) type].description;
6187 }
6188
6189 /* For MI, output a field named 'thread-groups' with a list as the value.
6190 For CLI, prefix the list with the string 'inf'. */
6191
6192 static void
6193 output_thread_groups (struct ui_out *uiout,
6194 const char *field_name,
6195 VEC(int) *inf_num,
6196 int mi_only)
6197 {
6198 struct cleanup *back_to;
6199 int is_mi = ui_out_is_mi_like_p (uiout);
6200 int inf;
6201 int i;
6202
6203 /* For backward compatibility, don't display inferiors in CLI unless
6204 there are several. Always display them for MI. */
6205 if (!is_mi && mi_only)
6206 return;
6207
6208 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6209
6210 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6211 {
6212 if (is_mi)
6213 {
6214 char mi_group[10];
6215
6216 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6217 ui_out_field_string (uiout, NULL, mi_group);
6218 }
6219 else
6220 {
6221 if (i == 0)
6222 ui_out_text (uiout, " inf ");
6223 else
6224 ui_out_text (uiout, ", ");
6225
6226 ui_out_text (uiout, plongest (inf));
6227 }
6228 }
6229
6230 do_cleanups (back_to);
6231 }
6232
6233 /* Print B to gdb_stdout. */
6234
6235 static void
6236 print_one_breakpoint_location (struct breakpoint *b,
6237 struct bp_location *loc,
6238 int loc_number,
6239 struct bp_location **last_loc,
6240 int allflag)
6241 {
6242 struct command_line *l;
6243 static char bpenables[] = "nynny";
6244
6245 struct ui_out *uiout = current_uiout;
6246 int header_of_multiple = 0;
6247 int part_of_multiple = (loc != NULL);
6248 struct value_print_options opts;
6249
6250 get_user_print_options (&opts);
6251
6252 gdb_assert (!loc || loc_number != 0);
6253 /* See comment in print_one_breakpoint concerning treatment of
6254 breakpoints with single disabled location. */
6255 if (loc == NULL
6256 && (b->loc != NULL
6257 && (b->loc->next != NULL || !b->loc->enabled)))
6258 header_of_multiple = 1;
6259 if (loc == NULL)
6260 loc = b->loc;
6261
6262 annotate_record ();
6263
6264 /* 1 */
6265 annotate_field (0);
6266 if (part_of_multiple)
6267 {
6268 char *formatted;
6269 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6270 ui_out_field_string (uiout, "number", formatted);
6271 xfree (formatted);
6272 }
6273 else
6274 {
6275 ui_out_field_int (uiout, "number", b->number);
6276 }
6277
6278 /* 2 */
6279 annotate_field (1);
6280 if (part_of_multiple)
6281 ui_out_field_skip (uiout, "type");
6282 else
6283 ui_out_field_string (uiout, "type", bptype_string (b->type));
6284
6285 /* 3 */
6286 annotate_field (2);
6287 if (part_of_multiple)
6288 ui_out_field_skip (uiout, "disp");
6289 else
6290 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6291
6292
6293 /* 4 */
6294 annotate_field (3);
6295 if (part_of_multiple)
6296 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6297 else
6298 ui_out_field_fmt (uiout, "enabled", "%c",
6299 bpenables[(int) b->enable_state]);
6300 ui_out_spaces (uiout, 2);
6301
6302
6303 /* 5 and 6 */
6304 if (b->ops != NULL && b->ops->print_one != NULL)
6305 {
6306 /* Although the print_one can possibly print all locations,
6307 calling it here is not likely to get any nice result. So,
6308 make sure there's just one location. */
6309 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6310 b->ops->print_one (b, last_loc);
6311 }
6312 else
6313 switch (b->type)
6314 {
6315 case bp_none:
6316 internal_error (__FILE__, __LINE__,
6317 _("print_one_breakpoint: bp_none encountered\n"));
6318 break;
6319
6320 case bp_watchpoint:
6321 case bp_hardware_watchpoint:
6322 case bp_read_watchpoint:
6323 case bp_access_watchpoint:
6324 {
6325 struct watchpoint *w = (struct watchpoint *) b;
6326
6327 /* Field 4, the address, is omitted (which makes the columns
6328 not line up too nicely with the headers, but the effect
6329 is relatively readable). */
6330 if (opts.addressprint)
6331 ui_out_field_skip (uiout, "addr");
6332 annotate_field (5);
6333 ui_out_field_string (uiout, "what", w->exp_string);
6334 }
6335 break;
6336
6337 case bp_breakpoint:
6338 case bp_hardware_breakpoint:
6339 case bp_until:
6340 case bp_finish:
6341 case bp_longjmp:
6342 case bp_longjmp_resume:
6343 case bp_longjmp_call_dummy:
6344 case bp_exception:
6345 case bp_exception_resume:
6346 case bp_step_resume:
6347 case bp_hp_step_resume:
6348 case bp_watchpoint_scope:
6349 case bp_call_dummy:
6350 case bp_std_terminate:
6351 case bp_shlib_event:
6352 case bp_thread_event:
6353 case bp_overlay_event:
6354 case bp_longjmp_master:
6355 case bp_std_terminate_master:
6356 case bp_exception_master:
6357 case bp_tracepoint:
6358 case bp_fast_tracepoint:
6359 case bp_static_tracepoint:
6360 case bp_dprintf:
6361 case bp_jit_event:
6362 case bp_gnu_ifunc_resolver:
6363 case bp_gnu_ifunc_resolver_return:
6364 if (opts.addressprint)
6365 {
6366 annotate_field (4);
6367 if (header_of_multiple)
6368 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6369 else if (b->loc == NULL || loc->shlib_disabled)
6370 ui_out_field_string (uiout, "addr", "<PENDING>");
6371 else
6372 ui_out_field_core_addr (uiout, "addr",
6373 loc->gdbarch, loc->address);
6374 }
6375 annotate_field (5);
6376 if (!header_of_multiple)
6377 print_breakpoint_location (b, loc);
6378 if (b->loc)
6379 *last_loc = b->loc;
6380 break;
6381 }
6382
6383
6384 if (loc != NULL && !header_of_multiple)
6385 {
6386 struct inferior *inf;
6387 VEC(int) *inf_num = NULL;
6388 int mi_only = 1;
6389
6390 ALL_INFERIORS (inf)
6391 {
6392 if (inf->pspace == loc->pspace)
6393 VEC_safe_push (int, inf_num, inf->num);
6394 }
6395
6396 /* For backward compatibility, don't display inferiors in CLI unless
6397 there are several. Always display for MI. */
6398 if (allflag
6399 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6400 && (number_of_program_spaces () > 1
6401 || number_of_inferiors () > 1)
6402 /* LOC is for existing B, it cannot be in
6403 moribund_locations and thus having NULL OWNER. */
6404 && loc->owner->type != bp_catchpoint))
6405 mi_only = 0;
6406 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6407 VEC_free (int, inf_num);
6408 }
6409
6410 if (!part_of_multiple)
6411 {
6412 if (b->thread != -1)
6413 {
6414 /* FIXME: This seems to be redundant and lost here; see the
6415 "stop only in" line a little further down. */
6416 ui_out_text (uiout, " thread ");
6417 ui_out_field_int (uiout, "thread", b->thread);
6418 }
6419 else if (b->task != 0)
6420 {
6421 ui_out_text (uiout, " task ");
6422 ui_out_field_int (uiout, "task", b->task);
6423 }
6424 }
6425
6426 ui_out_text (uiout, "\n");
6427
6428 if (!part_of_multiple)
6429 b->ops->print_one_detail (b, uiout);
6430
6431 if (part_of_multiple && frame_id_p (b->frame_id))
6432 {
6433 annotate_field (6);
6434 ui_out_text (uiout, "\tstop only in stack frame at ");
6435 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6436 the frame ID. */
6437 ui_out_field_core_addr (uiout, "frame",
6438 b->gdbarch, b->frame_id.stack_addr);
6439 ui_out_text (uiout, "\n");
6440 }
6441
6442 if (!part_of_multiple && b->cond_string)
6443 {
6444 annotate_field (7);
6445 if (is_tracepoint (b))
6446 ui_out_text (uiout, "\ttrace only if ");
6447 else
6448 ui_out_text (uiout, "\tstop only if ");
6449 ui_out_field_string (uiout, "cond", b->cond_string);
6450
6451 /* Print whether the target is doing the breakpoint's condition
6452 evaluation. If GDB is doing the evaluation, don't print anything. */
6453 if (is_breakpoint (b)
6454 && breakpoint_condition_evaluation_mode ()
6455 == condition_evaluation_target)
6456 {
6457 ui_out_text (uiout, " (");
6458 ui_out_field_string (uiout, "evaluated-by",
6459 bp_condition_evaluator (b));
6460 ui_out_text (uiout, " evals)");
6461 }
6462 ui_out_text (uiout, "\n");
6463 }
6464
6465 if (!part_of_multiple && b->thread != -1)
6466 {
6467 /* FIXME should make an annotation for this. */
6468 ui_out_text (uiout, "\tstop only in thread ");
6469 ui_out_field_int (uiout, "thread", b->thread);
6470 ui_out_text (uiout, "\n");
6471 }
6472
6473 if (!part_of_multiple)
6474 {
6475 if (b->hit_count)
6476 {
6477 /* FIXME should make an annotation for this. */
6478 if (is_catchpoint (b))
6479 ui_out_text (uiout, "\tcatchpoint");
6480 else if (is_tracepoint (b))
6481 ui_out_text (uiout, "\ttracepoint");
6482 else
6483 ui_out_text (uiout, "\tbreakpoint");
6484 ui_out_text (uiout, " already hit ");
6485 ui_out_field_int (uiout, "times", b->hit_count);
6486 if (b->hit_count == 1)
6487 ui_out_text (uiout, " time\n");
6488 else
6489 ui_out_text (uiout, " times\n");
6490 }
6491 else
6492 {
6493 /* Output the count also if it is zero, but only if this is mi. */
6494 if (ui_out_is_mi_like_p (uiout))
6495 ui_out_field_int (uiout, "times", b->hit_count);
6496 }
6497 }
6498
6499 if (!part_of_multiple && b->ignore_count)
6500 {
6501 annotate_field (8);
6502 ui_out_text (uiout, "\tignore next ");
6503 ui_out_field_int (uiout, "ignore", b->ignore_count);
6504 ui_out_text (uiout, " hits\n");
6505 }
6506
6507 /* Note that an enable count of 1 corresponds to "enable once"
6508 behavior, which is reported by the combination of enablement and
6509 disposition, so we don't need to mention it here. */
6510 if (!part_of_multiple && b->enable_count > 1)
6511 {
6512 annotate_field (8);
6513 ui_out_text (uiout, "\tdisable after ");
6514 /* Tweak the wording to clarify that ignore and enable counts
6515 are distinct, and have additive effect. */
6516 if (b->ignore_count)
6517 ui_out_text (uiout, "additional ");
6518 else
6519 ui_out_text (uiout, "next ");
6520 ui_out_field_int (uiout, "enable", b->enable_count);
6521 ui_out_text (uiout, " hits\n");
6522 }
6523
6524 if (!part_of_multiple && is_tracepoint (b))
6525 {
6526 struct tracepoint *tp = (struct tracepoint *) b;
6527
6528 if (tp->traceframe_usage)
6529 {
6530 ui_out_text (uiout, "\ttrace buffer usage ");
6531 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6532 ui_out_text (uiout, " bytes\n");
6533 }
6534 }
6535
6536 l = b->commands ? b->commands->commands : NULL;
6537 if (!part_of_multiple && l)
6538 {
6539 struct cleanup *script_chain;
6540
6541 annotate_field (9);
6542 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6543 print_command_lines (uiout, l, 4);
6544 do_cleanups (script_chain);
6545 }
6546
6547 if (is_tracepoint (b))
6548 {
6549 struct tracepoint *t = (struct tracepoint *) b;
6550
6551 if (!part_of_multiple && t->pass_count)
6552 {
6553 annotate_field (10);
6554 ui_out_text (uiout, "\tpass count ");
6555 ui_out_field_int (uiout, "pass", t->pass_count);
6556 ui_out_text (uiout, " \n");
6557 }
6558
6559 /* Don't display it when tracepoint or tracepoint location is
6560 pending. */
6561 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6562 {
6563 annotate_field (11);
6564
6565 if (ui_out_is_mi_like_p (uiout))
6566 ui_out_field_string (uiout, "installed",
6567 loc->inserted ? "y" : "n");
6568 else
6569 {
6570 if (loc->inserted)
6571 ui_out_text (uiout, "\t");
6572 else
6573 ui_out_text (uiout, "\tnot ");
6574 ui_out_text (uiout, "installed on target\n");
6575 }
6576 }
6577 }
6578
6579 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6580 {
6581 if (is_watchpoint (b))
6582 {
6583 struct watchpoint *w = (struct watchpoint *) b;
6584
6585 ui_out_field_string (uiout, "original-location", w->exp_string);
6586 }
6587 else if (b->addr_string)
6588 ui_out_field_string (uiout, "original-location", b->addr_string);
6589 }
6590 }
6591
6592 static void
6593 print_one_breakpoint (struct breakpoint *b,
6594 struct bp_location **last_loc,
6595 int allflag)
6596 {
6597 struct cleanup *bkpt_chain;
6598 struct ui_out *uiout = current_uiout;
6599
6600 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6601
6602 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6603 do_cleanups (bkpt_chain);
6604
6605 /* If this breakpoint has custom print function,
6606 it's already printed. Otherwise, print individual
6607 locations, if any. */
6608 if (b->ops == NULL || b->ops->print_one == NULL)
6609 {
6610 /* If breakpoint has a single location that is disabled, we
6611 print it as if it had several locations, since otherwise it's
6612 hard to represent "breakpoint enabled, location disabled"
6613 situation.
6614
6615 Note that while hardware watchpoints have several locations
6616 internally, that's not a property exposed to user. */
6617 if (b->loc
6618 && !is_hardware_watchpoint (b)
6619 && (b->loc->next || !b->loc->enabled))
6620 {
6621 struct bp_location *loc;
6622 int n = 1;
6623
6624 for (loc = b->loc; loc; loc = loc->next, ++n)
6625 {
6626 struct cleanup *inner2 =
6627 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6628 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6629 do_cleanups (inner2);
6630 }
6631 }
6632 }
6633 }
6634
6635 static int
6636 breakpoint_address_bits (struct breakpoint *b)
6637 {
6638 int print_address_bits = 0;
6639 struct bp_location *loc;
6640
6641 for (loc = b->loc; loc; loc = loc->next)
6642 {
6643 int addr_bit;
6644
6645 /* Software watchpoints that aren't watching memory don't have
6646 an address to print. */
6647 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6648 continue;
6649
6650 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6651 if (addr_bit > print_address_bits)
6652 print_address_bits = addr_bit;
6653 }
6654
6655 return print_address_bits;
6656 }
6657
6658 struct captured_breakpoint_query_args
6659 {
6660 int bnum;
6661 };
6662
6663 static int
6664 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6665 {
6666 struct captured_breakpoint_query_args *args = data;
6667 struct breakpoint *b;
6668 struct bp_location *dummy_loc = NULL;
6669
6670 ALL_BREAKPOINTS (b)
6671 {
6672 if (args->bnum == b->number)
6673 {
6674 print_one_breakpoint (b, &dummy_loc, 0);
6675 return GDB_RC_OK;
6676 }
6677 }
6678 return GDB_RC_NONE;
6679 }
6680
6681 enum gdb_rc
6682 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6683 char **error_message)
6684 {
6685 struct captured_breakpoint_query_args args;
6686
6687 args.bnum = bnum;
6688 /* For the moment we don't trust print_one_breakpoint() to not throw
6689 an error. */
6690 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6691 error_message, RETURN_MASK_ALL) < 0)
6692 return GDB_RC_FAIL;
6693 else
6694 return GDB_RC_OK;
6695 }
6696
6697 /* Return true if this breakpoint was set by the user, false if it is
6698 internal or momentary. */
6699
6700 int
6701 user_breakpoint_p (struct breakpoint *b)
6702 {
6703 return b->number > 0;
6704 }
6705
6706 /* Print information on user settable breakpoint (watchpoint, etc)
6707 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6708 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6709 FILTER is non-NULL, call it on each breakpoint and only include the
6710 ones for which it returns non-zero. Return the total number of
6711 breakpoints listed. */
6712
6713 static int
6714 breakpoint_1 (char *args, int allflag,
6715 int (*filter) (const struct breakpoint *))
6716 {
6717 struct breakpoint *b;
6718 struct bp_location *last_loc = NULL;
6719 int nr_printable_breakpoints;
6720 struct cleanup *bkpttbl_chain;
6721 struct value_print_options opts;
6722 int print_address_bits = 0;
6723 int print_type_col_width = 14;
6724 struct ui_out *uiout = current_uiout;
6725
6726 get_user_print_options (&opts);
6727
6728 /* Compute the number of rows in the table, as well as the size
6729 required for address fields. */
6730 nr_printable_breakpoints = 0;
6731 ALL_BREAKPOINTS (b)
6732 {
6733 /* If we have a filter, only list the breakpoints it accepts. */
6734 if (filter && !filter (b))
6735 continue;
6736
6737 /* If we have an "args" string, it is a list of breakpoints to
6738 accept. Skip the others. */
6739 if (args != NULL && *args != '\0')
6740 {
6741 if (allflag && parse_and_eval_long (args) != b->number)
6742 continue;
6743 if (!allflag && !number_is_in_list (args, b->number))
6744 continue;
6745 }
6746
6747 if (allflag || user_breakpoint_p (b))
6748 {
6749 int addr_bit, type_len;
6750
6751 addr_bit = breakpoint_address_bits (b);
6752 if (addr_bit > print_address_bits)
6753 print_address_bits = addr_bit;
6754
6755 type_len = strlen (bptype_string (b->type));
6756 if (type_len > print_type_col_width)
6757 print_type_col_width = type_len;
6758
6759 nr_printable_breakpoints++;
6760 }
6761 }
6762
6763 if (opts.addressprint)
6764 bkpttbl_chain
6765 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6766 nr_printable_breakpoints,
6767 "BreakpointTable");
6768 else
6769 bkpttbl_chain
6770 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6771 nr_printable_breakpoints,
6772 "BreakpointTable");
6773
6774 if (nr_printable_breakpoints > 0)
6775 annotate_breakpoints_headers ();
6776 if (nr_printable_breakpoints > 0)
6777 annotate_field (0);
6778 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6779 if (nr_printable_breakpoints > 0)
6780 annotate_field (1);
6781 ui_out_table_header (uiout, print_type_col_width, ui_left,
6782 "type", "Type"); /* 2 */
6783 if (nr_printable_breakpoints > 0)
6784 annotate_field (2);
6785 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6786 if (nr_printable_breakpoints > 0)
6787 annotate_field (3);
6788 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6789 if (opts.addressprint)
6790 {
6791 if (nr_printable_breakpoints > 0)
6792 annotate_field (4);
6793 if (print_address_bits <= 32)
6794 ui_out_table_header (uiout, 10, ui_left,
6795 "addr", "Address"); /* 5 */
6796 else
6797 ui_out_table_header (uiout, 18, ui_left,
6798 "addr", "Address"); /* 5 */
6799 }
6800 if (nr_printable_breakpoints > 0)
6801 annotate_field (5);
6802 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6803 ui_out_table_body (uiout);
6804 if (nr_printable_breakpoints > 0)
6805 annotate_breakpoints_table ();
6806
6807 ALL_BREAKPOINTS (b)
6808 {
6809 QUIT;
6810 /* If we have a filter, only list the breakpoints it accepts. */
6811 if (filter && !filter (b))
6812 continue;
6813
6814 /* If we have an "args" string, it is a list of breakpoints to
6815 accept. Skip the others. */
6816
6817 if (args != NULL && *args != '\0')
6818 {
6819 if (allflag) /* maintenance info breakpoint */
6820 {
6821 if (parse_and_eval_long (args) != b->number)
6822 continue;
6823 }
6824 else /* all others */
6825 {
6826 if (!number_is_in_list (args, b->number))
6827 continue;
6828 }
6829 }
6830 /* We only print out user settable breakpoints unless the
6831 allflag is set. */
6832 if (allflag || user_breakpoint_p (b))
6833 print_one_breakpoint (b, &last_loc, allflag);
6834 }
6835
6836 do_cleanups (bkpttbl_chain);
6837
6838 if (nr_printable_breakpoints == 0)
6839 {
6840 /* If there's a filter, let the caller decide how to report
6841 empty list. */
6842 if (!filter)
6843 {
6844 if (args == NULL || *args == '\0')
6845 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6846 else
6847 ui_out_message (uiout, 0,
6848 "No breakpoint or watchpoint matching '%s'.\n",
6849 args);
6850 }
6851 }
6852 else
6853 {
6854 if (last_loc && !server_command)
6855 set_next_address (last_loc->gdbarch, last_loc->address);
6856 }
6857
6858 /* FIXME? Should this be moved up so that it is only called when
6859 there have been breakpoints? */
6860 annotate_breakpoints_table_end ();
6861
6862 return nr_printable_breakpoints;
6863 }
6864
6865 /* Display the value of default-collect in a way that is generally
6866 compatible with the breakpoint list. */
6867
6868 static void
6869 default_collect_info (void)
6870 {
6871 struct ui_out *uiout = current_uiout;
6872
6873 /* If it has no value (which is frequently the case), say nothing; a
6874 message like "No default-collect." gets in user's face when it's
6875 not wanted. */
6876 if (!*default_collect)
6877 return;
6878
6879 /* The following phrase lines up nicely with per-tracepoint collect
6880 actions. */
6881 ui_out_text (uiout, "default collect ");
6882 ui_out_field_string (uiout, "default-collect", default_collect);
6883 ui_out_text (uiout, " \n");
6884 }
6885
6886 static void
6887 breakpoints_info (char *args, int from_tty)
6888 {
6889 breakpoint_1 (args, 0, NULL);
6890
6891 default_collect_info ();
6892 }
6893
6894 static void
6895 watchpoints_info (char *args, int from_tty)
6896 {
6897 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6898 struct ui_out *uiout = current_uiout;
6899
6900 if (num_printed == 0)
6901 {
6902 if (args == NULL || *args == '\0')
6903 ui_out_message (uiout, 0, "No watchpoints.\n");
6904 else
6905 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6906 }
6907 }
6908
6909 static void
6910 maintenance_info_breakpoints (char *args, int from_tty)
6911 {
6912 breakpoint_1 (args, 1, NULL);
6913
6914 default_collect_info ();
6915 }
6916
6917 static int
6918 breakpoint_has_pc (struct breakpoint *b,
6919 struct program_space *pspace,
6920 CORE_ADDR pc, struct obj_section *section)
6921 {
6922 struct bp_location *bl = b->loc;
6923
6924 for (; bl; bl = bl->next)
6925 {
6926 if (bl->pspace == pspace
6927 && bl->address == pc
6928 && (!overlay_debugging || bl->section == section))
6929 return 1;
6930 }
6931 return 0;
6932 }
6933
6934 /* Print a message describing any user-breakpoints set at PC. This
6935 concerns with logical breakpoints, so we match program spaces, not
6936 address spaces. */
6937
6938 static void
6939 describe_other_breakpoints (struct gdbarch *gdbarch,
6940 struct program_space *pspace, CORE_ADDR pc,
6941 struct obj_section *section, int thread)
6942 {
6943 int others = 0;
6944 struct breakpoint *b;
6945
6946 ALL_BREAKPOINTS (b)
6947 others += (user_breakpoint_p (b)
6948 && breakpoint_has_pc (b, pspace, pc, section));
6949 if (others > 0)
6950 {
6951 if (others == 1)
6952 printf_filtered (_("Note: breakpoint "));
6953 else /* if (others == ???) */
6954 printf_filtered (_("Note: breakpoints "));
6955 ALL_BREAKPOINTS (b)
6956 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6957 {
6958 others--;
6959 printf_filtered ("%d", b->number);
6960 if (b->thread == -1 && thread != -1)
6961 printf_filtered (" (all threads)");
6962 else if (b->thread != -1)
6963 printf_filtered (" (thread %d)", b->thread);
6964 printf_filtered ("%s%s ",
6965 ((b->enable_state == bp_disabled
6966 || b->enable_state == bp_call_disabled)
6967 ? " (disabled)"
6968 : b->enable_state == bp_permanent
6969 ? " (permanent)"
6970 : ""),
6971 (others > 1) ? ","
6972 : ((others == 1) ? " and" : ""));
6973 }
6974 printf_filtered (_("also set at pc "));
6975 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6976 printf_filtered (".\n");
6977 }
6978 }
6979 \f
6980
6981 /* Return true iff it is meaningful to use the address member of
6982 BPT. For some breakpoint types, the address member is irrelevant
6983 and it makes no sense to attempt to compare it to other addresses
6984 (or use it for any other purpose either).
6985
6986 More specifically, each of the following breakpoint types will
6987 always have a zero valued address and we don't want to mark
6988 breakpoints of any of these types to be a duplicate of an actual
6989 breakpoint at address zero:
6990
6991 bp_watchpoint
6992 bp_catchpoint
6993
6994 */
6995
6996 static int
6997 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6998 {
6999 enum bptype type = bpt->type;
7000
7001 return (type != bp_watchpoint && type != bp_catchpoint);
7002 }
7003
7004 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7005 true if LOC1 and LOC2 represent the same watchpoint location. */
7006
7007 static int
7008 watchpoint_locations_match (struct bp_location *loc1,
7009 struct bp_location *loc2)
7010 {
7011 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7012 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7013
7014 /* Both of them must exist. */
7015 gdb_assert (w1 != NULL);
7016 gdb_assert (w2 != NULL);
7017
7018 /* If the target can evaluate the condition expression in hardware,
7019 then we we need to insert both watchpoints even if they are at
7020 the same place. Otherwise the watchpoint will only trigger when
7021 the condition of whichever watchpoint was inserted evaluates to
7022 true, not giving a chance for GDB to check the condition of the
7023 other watchpoint. */
7024 if ((w1->cond_exp
7025 && target_can_accel_watchpoint_condition (loc1->address,
7026 loc1->length,
7027 loc1->watchpoint_type,
7028 w1->cond_exp))
7029 || (w2->cond_exp
7030 && target_can_accel_watchpoint_condition (loc2->address,
7031 loc2->length,
7032 loc2->watchpoint_type,
7033 w2->cond_exp)))
7034 return 0;
7035
7036 /* Note that this checks the owner's type, not the location's. In
7037 case the target does not support read watchpoints, but does
7038 support access watchpoints, we'll have bp_read_watchpoint
7039 watchpoints with hw_access locations. Those should be considered
7040 duplicates of hw_read locations. The hw_read locations will
7041 become hw_access locations later. */
7042 return (loc1->owner->type == loc2->owner->type
7043 && loc1->pspace->aspace == loc2->pspace->aspace
7044 && loc1->address == loc2->address
7045 && loc1->length == loc2->length);
7046 }
7047
7048 /* See breakpoint.h. */
7049
7050 int
7051 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7052 struct address_space *aspace2, CORE_ADDR addr2)
7053 {
7054 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7055 || aspace1 == aspace2)
7056 && addr1 == addr2);
7057 }
7058
7059 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7060 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7061 matches ASPACE2. On targets that have global breakpoints, the address
7062 space doesn't really matter. */
7063
7064 static int
7065 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7066 int len1, struct address_space *aspace2,
7067 CORE_ADDR addr2)
7068 {
7069 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7070 || aspace1 == aspace2)
7071 && addr2 >= addr1 && addr2 < addr1 + len1);
7072 }
7073
7074 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7075 a ranged breakpoint. In most targets, a match happens only if ASPACE
7076 matches the breakpoint's address space. On targets that have global
7077 breakpoints, the address space doesn't really matter. */
7078
7079 static int
7080 breakpoint_location_address_match (struct bp_location *bl,
7081 struct address_space *aspace,
7082 CORE_ADDR addr)
7083 {
7084 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7085 aspace, addr)
7086 || (bl->length
7087 && breakpoint_address_match_range (bl->pspace->aspace,
7088 bl->address, bl->length,
7089 aspace, addr)));
7090 }
7091
7092 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7093 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7094 true, otherwise returns false. */
7095
7096 static int
7097 tracepoint_locations_match (struct bp_location *loc1,
7098 struct bp_location *loc2)
7099 {
7100 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7101 /* Since tracepoint locations are never duplicated with others', tracepoint
7102 locations at the same address of different tracepoints are regarded as
7103 different locations. */
7104 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7105 else
7106 return 0;
7107 }
7108
7109 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7110 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7111 represent the same location. */
7112
7113 static int
7114 breakpoint_locations_match (struct bp_location *loc1,
7115 struct bp_location *loc2)
7116 {
7117 int hw_point1, hw_point2;
7118
7119 /* Both of them must not be in moribund_locations. */
7120 gdb_assert (loc1->owner != NULL);
7121 gdb_assert (loc2->owner != NULL);
7122
7123 hw_point1 = is_hardware_watchpoint (loc1->owner);
7124 hw_point2 = is_hardware_watchpoint (loc2->owner);
7125
7126 if (hw_point1 != hw_point2)
7127 return 0;
7128 else if (hw_point1)
7129 return watchpoint_locations_match (loc1, loc2);
7130 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7131 return tracepoint_locations_match (loc1, loc2);
7132 else
7133 /* We compare bp_location.length in order to cover ranged breakpoints. */
7134 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7135 loc2->pspace->aspace, loc2->address)
7136 && loc1->length == loc2->length);
7137 }
7138
7139 static void
7140 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7141 int bnum, int have_bnum)
7142 {
7143 /* The longest string possibly returned by hex_string_custom
7144 is 50 chars. These must be at least that big for safety. */
7145 char astr1[64];
7146 char astr2[64];
7147
7148 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7149 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7150 if (have_bnum)
7151 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7152 bnum, astr1, astr2);
7153 else
7154 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7155 }
7156
7157 /* Adjust a breakpoint's address to account for architectural
7158 constraints on breakpoint placement. Return the adjusted address.
7159 Note: Very few targets require this kind of adjustment. For most
7160 targets, this function is simply the identity function. */
7161
7162 static CORE_ADDR
7163 adjust_breakpoint_address (struct gdbarch *gdbarch,
7164 CORE_ADDR bpaddr, enum bptype bptype)
7165 {
7166 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7167 {
7168 /* Very few targets need any kind of breakpoint adjustment. */
7169 return bpaddr;
7170 }
7171 else if (bptype == bp_watchpoint
7172 || bptype == bp_hardware_watchpoint
7173 || bptype == bp_read_watchpoint
7174 || bptype == bp_access_watchpoint
7175 || bptype == bp_catchpoint)
7176 {
7177 /* Watchpoints and the various bp_catch_* eventpoints should not
7178 have their addresses modified. */
7179 return bpaddr;
7180 }
7181 else
7182 {
7183 CORE_ADDR adjusted_bpaddr;
7184
7185 /* Some targets have architectural constraints on the placement
7186 of breakpoint instructions. Obtain the adjusted address. */
7187 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7188
7189 /* An adjusted breakpoint address can significantly alter
7190 a user's expectations. Print a warning if an adjustment
7191 is required. */
7192 if (adjusted_bpaddr != bpaddr)
7193 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7194
7195 return adjusted_bpaddr;
7196 }
7197 }
7198
7199 void
7200 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7201 struct breakpoint *owner)
7202 {
7203 memset (loc, 0, sizeof (*loc));
7204
7205 gdb_assert (ops != NULL);
7206
7207 loc->ops = ops;
7208 loc->owner = owner;
7209 loc->cond = NULL;
7210 loc->cond_bytecode = NULL;
7211 loc->shlib_disabled = 0;
7212 loc->enabled = 1;
7213
7214 switch (owner->type)
7215 {
7216 case bp_breakpoint:
7217 case bp_until:
7218 case bp_finish:
7219 case bp_longjmp:
7220 case bp_longjmp_resume:
7221 case bp_longjmp_call_dummy:
7222 case bp_exception:
7223 case bp_exception_resume:
7224 case bp_step_resume:
7225 case bp_hp_step_resume:
7226 case bp_watchpoint_scope:
7227 case bp_call_dummy:
7228 case bp_std_terminate:
7229 case bp_shlib_event:
7230 case bp_thread_event:
7231 case bp_overlay_event:
7232 case bp_jit_event:
7233 case bp_longjmp_master:
7234 case bp_std_terminate_master:
7235 case bp_exception_master:
7236 case bp_gnu_ifunc_resolver:
7237 case bp_gnu_ifunc_resolver_return:
7238 case bp_dprintf:
7239 loc->loc_type = bp_loc_software_breakpoint;
7240 mark_breakpoint_location_modified (loc);
7241 break;
7242 case bp_hardware_breakpoint:
7243 loc->loc_type = bp_loc_hardware_breakpoint;
7244 mark_breakpoint_location_modified (loc);
7245 break;
7246 case bp_hardware_watchpoint:
7247 case bp_read_watchpoint:
7248 case bp_access_watchpoint:
7249 loc->loc_type = bp_loc_hardware_watchpoint;
7250 break;
7251 case bp_watchpoint:
7252 case bp_catchpoint:
7253 case bp_tracepoint:
7254 case bp_fast_tracepoint:
7255 case bp_static_tracepoint:
7256 loc->loc_type = bp_loc_other;
7257 break;
7258 default:
7259 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7260 }
7261
7262 loc->refc = 1;
7263 }
7264
7265 /* Allocate a struct bp_location. */
7266
7267 static struct bp_location *
7268 allocate_bp_location (struct breakpoint *bpt)
7269 {
7270 return bpt->ops->allocate_location (bpt);
7271 }
7272
7273 static void
7274 free_bp_location (struct bp_location *loc)
7275 {
7276 loc->ops->dtor (loc);
7277 xfree (loc);
7278 }
7279
7280 /* Increment reference count. */
7281
7282 static void
7283 incref_bp_location (struct bp_location *bl)
7284 {
7285 ++bl->refc;
7286 }
7287
7288 /* Decrement reference count. If the reference count reaches 0,
7289 destroy the bp_location. Sets *BLP to NULL. */
7290
7291 static void
7292 decref_bp_location (struct bp_location **blp)
7293 {
7294 gdb_assert ((*blp)->refc > 0);
7295
7296 if (--(*blp)->refc == 0)
7297 free_bp_location (*blp);
7298 *blp = NULL;
7299 }
7300
7301 /* Add breakpoint B at the end of the global breakpoint chain. */
7302
7303 static void
7304 add_to_breakpoint_chain (struct breakpoint *b)
7305 {
7306 struct breakpoint *b1;
7307
7308 /* Add this breakpoint to the end of the chain so that a list of
7309 breakpoints will come out in order of increasing numbers. */
7310
7311 b1 = breakpoint_chain;
7312 if (b1 == 0)
7313 breakpoint_chain = b;
7314 else
7315 {
7316 while (b1->next)
7317 b1 = b1->next;
7318 b1->next = b;
7319 }
7320 }
7321
7322 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7323
7324 static void
7325 init_raw_breakpoint_without_location (struct breakpoint *b,
7326 struct gdbarch *gdbarch,
7327 enum bptype bptype,
7328 const struct breakpoint_ops *ops)
7329 {
7330 memset (b, 0, sizeof (*b));
7331
7332 gdb_assert (ops != NULL);
7333
7334 b->ops = ops;
7335 b->type = bptype;
7336 b->gdbarch = gdbarch;
7337 b->language = current_language->la_language;
7338 b->input_radix = input_radix;
7339 b->thread = -1;
7340 b->enable_state = bp_enabled;
7341 b->next = 0;
7342 b->silent = 0;
7343 b->ignore_count = 0;
7344 b->commands = NULL;
7345 b->frame_id = null_frame_id;
7346 b->condition_not_parsed = 0;
7347 b->py_bp_object = NULL;
7348 b->related_breakpoint = b;
7349 }
7350
7351 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7352 that has type BPTYPE and has no locations as yet. */
7353
7354 static struct breakpoint *
7355 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7356 enum bptype bptype,
7357 const struct breakpoint_ops *ops)
7358 {
7359 struct breakpoint *b = XNEW (struct breakpoint);
7360
7361 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7362 add_to_breakpoint_chain (b);
7363 return b;
7364 }
7365
7366 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7367 resolutions should be made as the user specified the location explicitly
7368 enough. */
7369
7370 static void
7371 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7372 {
7373 gdb_assert (loc->owner != NULL);
7374
7375 if (loc->owner->type == bp_breakpoint
7376 || loc->owner->type == bp_hardware_breakpoint
7377 || is_tracepoint (loc->owner))
7378 {
7379 int is_gnu_ifunc;
7380 const char *function_name;
7381 CORE_ADDR func_addr;
7382
7383 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7384 &func_addr, NULL, &is_gnu_ifunc);
7385
7386 if (is_gnu_ifunc && !explicit_loc)
7387 {
7388 struct breakpoint *b = loc->owner;
7389
7390 gdb_assert (loc->pspace == current_program_space);
7391 if (gnu_ifunc_resolve_name (function_name,
7392 &loc->requested_address))
7393 {
7394 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7395 loc->address = adjust_breakpoint_address (loc->gdbarch,
7396 loc->requested_address,
7397 b->type);
7398 }
7399 else if (b->type == bp_breakpoint && b->loc == loc
7400 && loc->next == NULL && b->related_breakpoint == b)
7401 {
7402 /* Create only the whole new breakpoint of this type but do not
7403 mess more complicated breakpoints with multiple locations. */
7404 b->type = bp_gnu_ifunc_resolver;
7405 /* Remember the resolver's address for use by the return
7406 breakpoint. */
7407 loc->related_address = func_addr;
7408 }
7409 }
7410
7411 if (function_name)
7412 loc->function_name = xstrdup (function_name);
7413 }
7414 }
7415
7416 /* Attempt to determine architecture of location identified by SAL. */
7417 struct gdbarch *
7418 get_sal_arch (struct symtab_and_line sal)
7419 {
7420 if (sal.section)
7421 return get_objfile_arch (sal.section->objfile);
7422 if (sal.symtab)
7423 return get_objfile_arch (sal.symtab->objfile);
7424
7425 return NULL;
7426 }
7427
7428 /* Low level routine for partially initializing a breakpoint of type
7429 BPTYPE. The newly created breakpoint's address, section, source
7430 file name, and line number are provided by SAL.
7431
7432 It is expected that the caller will complete the initialization of
7433 the newly created breakpoint struct as well as output any status
7434 information regarding the creation of a new breakpoint. */
7435
7436 static void
7437 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7438 struct symtab_and_line sal, enum bptype bptype,
7439 const struct breakpoint_ops *ops)
7440 {
7441 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7442
7443 add_location_to_breakpoint (b, &sal);
7444
7445 if (bptype != bp_catchpoint)
7446 gdb_assert (sal.pspace != NULL);
7447
7448 /* Store the program space that was used to set the breakpoint,
7449 except for ordinary breakpoints, which are independent of the
7450 program space. */
7451 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7452 b->pspace = sal.pspace;
7453 }
7454
7455 /* set_raw_breakpoint is a low level routine for allocating and
7456 partially initializing a breakpoint of type BPTYPE. The newly
7457 created breakpoint's address, section, source file name, and line
7458 number are provided by SAL. The newly created and partially
7459 initialized breakpoint is added to the breakpoint chain and
7460 is also returned as the value of this function.
7461
7462 It is expected that the caller will complete the initialization of
7463 the newly created breakpoint struct as well as output any status
7464 information regarding the creation of a new breakpoint. In
7465 particular, set_raw_breakpoint does NOT set the breakpoint
7466 number! Care should be taken to not allow an error to occur
7467 prior to completing the initialization of the breakpoint. If this
7468 should happen, a bogus breakpoint will be left on the chain. */
7469
7470 struct breakpoint *
7471 set_raw_breakpoint (struct gdbarch *gdbarch,
7472 struct symtab_and_line sal, enum bptype bptype,
7473 const struct breakpoint_ops *ops)
7474 {
7475 struct breakpoint *b = XNEW (struct breakpoint);
7476
7477 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7478 add_to_breakpoint_chain (b);
7479 return b;
7480 }
7481
7482
7483 /* Note that the breakpoint object B describes a permanent breakpoint
7484 instruction, hard-wired into the inferior's code. */
7485 void
7486 make_breakpoint_permanent (struct breakpoint *b)
7487 {
7488 struct bp_location *bl;
7489
7490 b->enable_state = bp_permanent;
7491
7492 /* By definition, permanent breakpoints are already present in the
7493 code. Mark all locations as inserted. For now,
7494 make_breakpoint_permanent is called in just one place, so it's
7495 hard to say if it's reasonable to have permanent breakpoint with
7496 multiple locations or not, but it's easy to implement. */
7497 for (bl = b->loc; bl; bl = bl->next)
7498 bl->inserted = 1;
7499 }
7500
7501 /* Call this routine when stepping and nexting to enable a breakpoint
7502 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7503 initiated the operation. */
7504
7505 void
7506 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7507 {
7508 struct breakpoint *b, *b_tmp;
7509 int thread = tp->num;
7510
7511 /* To avoid having to rescan all objfile symbols at every step,
7512 we maintain a list of continually-inserted but always disabled
7513 longjmp "master" breakpoints. Here, we simply create momentary
7514 clones of those and enable them for the requested thread. */
7515 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7516 if (b->pspace == current_program_space
7517 && (b->type == bp_longjmp_master
7518 || b->type == bp_exception_master))
7519 {
7520 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7521 struct breakpoint *clone;
7522
7523 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7524 after their removal. */
7525 clone = momentary_breakpoint_from_master (b, type,
7526 &longjmp_breakpoint_ops, 1);
7527 clone->thread = thread;
7528 }
7529
7530 tp->initiating_frame = frame;
7531 }
7532
7533 /* Delete all longjmp breakpoints from THREAD. */
7534 void
7535 delete_longjmp_breakpoint (int thread)
7536 {
7537 struct breakpoint *b, *b_tmp;
7538
7539 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7540 if (b->type == bp_longjmp || b->type == bp_exception)
7541 {
7542 if (b->thread == thread)
7543 delete_breakpoint (b);
7544 }
7545 }
7546
7547 void
7548 delete_longjmp_breakpoint_at_next_stop (int thread)
7549 {
7550 struct breakpoint *b, *b_tmp;
7551
7552 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7553 if (b->type == bp_longjmp || b->type == bp_exception)
7554 {
7555 if (b->thread == thread)
7556 b->disposition = disp_del_at_next_stop;
7557 }
7558 }
7559
7560 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7561 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7562 pointer to any of them. Return NULL if this system cannot place longjmp
7563 breakpoints. */
7564
7565 struct breakpoint *
7566 set_longjmp_breakpoint_for_call_dummy (void)
7567 {
7568 struct breakpoint *b, *retval = NULL;
7569
7570 ALL_BREAKPOINTS (b)
7571 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7572 {
7573 struct breakpoint *new_b;
7574
7575 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7576 &momentary_breakpoint_ops,
7577 1);
7578 new_b->thread = pid_to_thread_id (inferior_ptid);
7579
7580 /* Link NEW_B into the chain of RETVAL breakpoints. */
7581
7582 gdb_assert (new_b->related_breakpoint == new_b);
7583 if (retval == NULL)
7584 retval = new_b;
7585 new_b->related_breakpoint = retval;
7586 while (retval->related_breakpoint != new_b->related_breakpoint)
7587 retval = retval->related_breakpoint;
7588 retval->related_breakpoint = new_b;
7589 }
7590
7591 return retval;
7592 }
7593
7594 /* Verify all existing dummy frames and their associated breakpoints for
7595 TP. Remove those which can no longer be found in the current frame
7596 stack.
7597
7598 You should call this function only at places where it is safe to currently
7599 unwind the whole stack. Failed stack unwind would discard live dummy
7600 frames. */
7601
7602 void
7603 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7604 {
7605 struct breakpoint *b, *b_tmp;
7606
7607 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7608 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7609 {
7610 struct breakpoint *dummy_b = b->related_breakpoint;
7611
7612 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7613 dummy_b = dummy_b->related_breakpoint;
7614 if (dummy_b->type != bp_call_dummy
7615 || frame_find_by_id (dummy_b->frame_id) != NULL)
7616 continue;
7617
7618 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7619
7620 while (b->related_breakpoint != b)
7621 {
7622 if (b_tmp == b->related_breakpoint)
7623 b_tmp = b->related_breakpoint->next;
7624 delete_breakpoint (b->related_breakpoint);
7625 }
7626 delete_breakpoint (b);
7627 }
7628 }
7629
7630 void
7631 enable_overlay_breakpoints (void)
7632 {
7633 struct breakpoint *b;
7634
7635 ALL_BREAKPOINTS (b)
7636 if (b->type == bp_overlay_event)
7637 {
7638 b->enable_state = bp_enabled;
7639 update_global_location_list (UGLL_MAY_INSERT);
7640 overlay_events_enabled = 1;
7641 }
7642 }
7643
7644 void
7645 disable_overlay_breakpoints (void)
7646 {
7647 struct breakpoint *b;
7648
7649 ALL_BREAKPOINTS (b)
7650 if (b->type == bp_overlay_event)
7651 {
7652 b->enable_state = bp_disabled;
7653 update_global_location_list (UGLL_DONT_INSERT);
7654 overlay_events_enabled = 0;
7655 }
7656 }
7657
7658 /* Set an active std::terminate breakpoint for each std::terminate
7659 master breakpoint. */
7660 void
7661 set_std_terminate_breakpoint (void)
7662 {
7663 struct breakpoint *b, *b_tmp;
7664
7665 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7666 if (b->pspace == current_program_space
7667 && b->type == bp_std_terminate_master)
7668 {
7669 momentary_breakpoint_from_master (b, bp_std_terminate,
7670 &momentary_breakpoint_ops, 1);
7671 }
7672 }
7673
7674 /* Delete all the std::terminate breakpoints. */
7675 void
7676 delete_std_terminate_breakpoint (void)
7677 {
7678 struct breakpoint *b, *b_tmp;
7679
7680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7681 if (b->type == bp_std_terminate)
7682 delete_breakpoint (b);
7683 }
7684
7685 struct breakpoint *
7686 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7687 {
7688 struct breakpoint *b;
7689
7690 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7691 &internal_breakpoint_ops);
7692
7693 b->enable_state = bp_enabled;
7694 /* addr_string has to be used or breakpoint_re_set will delete me. */
7695 b->addr_string
7696 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7697
7698 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7699
7700 return b;
7701 }
7702
7703 void
7704 remove_thread_event_breakpoints (void)
7705 {
7706 struct breakpoint *b, *b_tmp;
7707
7708 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7709 if (b->type == bp_thread_event
7710 && b->loc->pspace == current_program_space)
7711 delete_breakpoint (b);
7712 }
7713
7714 struct lang_and_radix
7715 {
7716 enum language lang;
7717 int radix;
7718 };
7719
7720 /* Create a breakpoint for JIT code registration and unregistration. */
7721
7722 struct breakpoint *
7723 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7724 {
7725 struct breakpoint *b;
7726
7727 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7728 &internal_breakpoint_ops);
7729 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7730 return b;
7731 }
7732
7733 /* Remove JIT code registration and unregistration breakpoint(s). */
7734
7735 void
7736 remove_jit_event_breakpoints (void)
7737 {
7738 struct breakpoint *b, *b_tmp;
7739
7740 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7741 if (b->type == bp_jit_event
7742 && b->loc->pspace == current_program_space)
7743 delete_breakpoint (b);
7744 }
7745
7746 void
7747 remove_solib_event_breakpoints (void)
7748 {
7749 struct breakpoint *b, *b_tmp;
7750
7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7752 if (b->type == bp_shlib_event
7753 && b->loc->pspace == current_program_space)
7754 delete_breakpoint (b);
7755 }
7756
7757 /* See breakpoint.h. */
7758
7759 void
7760 remove_solib_event_breakpoints_at_next_stop (void)
7761 {
7762 struct breakpoint *b, *b_tmp;
7763
7764 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7765 if (b->type == bp_shlib_event
7766 && b->loc->pspace == current_program_space)
7767 b->disposition = disp_del_at_next_stop;
7768 }
7769
7770 /* Helper for create_solib_event_breakpoint /
7771 create_and_insert_solib_event_breakpoint. Allows specifying which
7772 INSERT_MODE to pass through to update_global_location_list. */
7773
7774 static struct breakpoint *
7775 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7776 enum ugll_insert_mode insert_mode)
7777 {
7778 struct breakpoint *b;
7779
7780 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7781 &internal_breakpoint_ops);
7782 update_global_location_list_nothrow (insert_mode);
7783 return b;
7784 }
7785
7786 struct breakpoint *
7787 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7788 {
7789 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7790 }
7791
7792 /* See breakpoint.h. */
7793
7794 struct breakpoint *
7795 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7796 {
7797 struct breakpoint *b;
7798
7799 /* Explicitly tell update_global_location_list to insert
7800 locations. */
7801 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7802 if (!b->loc->inserted)
7803 {
7804 delete_breakpoint (b);
7805 return NULL;
7806 }
7807 return b;
7808 }
7809
7810 /* Disable any breakpoints that are on code in shared libraries. Only
7811 apply to enabled breakpoints, disabled ones can just stay disabled. */
7812
7813 void
7814 disable_breakpoints_in_shlibs (void)
7815 {
7816 struct bp_location *loc, **locp_tmp;
7817
7818 ALL_BP_LOCATIONS (loc, locp_tmp)
7819 {
7820 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7821 struct breakpoint *b = loc->owner;
7822
7823 /* We apply the check to all breakpoints, including disabled for
7824 those with loc->duplicate set. This is so that when breakpoint
7825 becomes enabled, or the duplicate is removed, gdb will try to
7826 insert all breakpoints. If we don't set shlib_disabled here,
7827 we'll try to insert those breakpoints and fail. */
7828 if (((b->type == bp_breakpoint)
7829 || (b->type == bp_jit_event)
7830 || (b->type == bp_hardware_breakpoint)
7831 || (is_tracepoint (b)))
7832 && loc->pspace == current_program_space
7833 && !loc->shlib_disabled
7834 && solib_name_from_address (loc->pspace, loc->address)
7835 )
7836 {
7837 loc->shlib_disabled = 1;
7838 }
7839 }
7840 }
7841
7842 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7843 notification of unloaded_shlib. Only apply to enabled breakpoints,
7844 disabled ones can just stay disabled. */
7845
7846 static void
7847 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7848 {
7849 struct bp_location *loc, **locp_tmp;
7850 int disabled_shlib_breaks = 0;
7851
7852 /* SunOS a.out shared libraries are always mapped, so do not
7853 disable breakpoints; they will only be reported as unloaded
7854 through clear_solib when GDB discards its shared library
7855 list. See clear_solib for more information. */
7856 if (exec_bfd != NULL
7857 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7858 return;
7859
7860 ALL_BP_LOCATIONS (loc, locp_tmp)
7861 {
7862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7863 struct breakpoint *b = loc->owner;
7864
7865 if (solib->pspace == loc->pspace
7866 && !loc->shlib_disabled
7867 && (((b->type == bp_breakpoint
7868 || b->type == bp_jit_event
7869 || b->type == bp_hardware_breakpoint)
7870 && (loc->loc_type == bp_loc_hardware_breakpoint
7871 || loc->loc_type == bp_loc_software_breakpoint))
7872 || is_tracepoint (b))
7873 && solib_contains_address_p (solib, loc->address))
7874 {
7875 loc->shlib_disabled = 1;
7876 /* At this point, we cannot rely on remove_breakpoint
7877 succeeding so we must mark the breakpoint as not inserted
7878 to prevent future errors occurring in remove_breakpoints. */
7879 loc->inserted = 0;
7880
7881 /* This may cause duplicate notifications for the same breakpoint. */
7882 observer_notify_breakpoint_modified (b);
7883
7884 if (!disabled_shlib_breaks)
7885 {
7886 target_terminal_ours_for_output ();
7887 warning (_("Temporarily disabling breakpoints "
7888 "for unloaded shared library \"%s\""),
7889 solib->so_name);
7890 }
7891 disabled_shlib_breaks = 1;
7892 }
7893 }
7894 }
7895
7896 /* Disable any breakpoints and tracepoints in OBJFILE upon
7897 notification of free_objfile. Only apply to enabled breakpoints,
7898 disabled ones can just stay disabled. */
7899
7900 static void
7901 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7902 {
7903 struct breakpoint *b;
7904
7905 if (objfile == NULL)
7906 return;
7907
7908 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7909 managed by the user with add-symbol-file/remove-symbol-file.
7910 Similarly to how breakpoints in shared libraries are handled in
7911 response to "nosharedlibrary", mark breakpoints in such modules
7912 shlib_disabled so they end up uninserted on the next global
7913 location list update. Shared libraries not loaded by the user
7914 aren't handled here -- they're already handled in
7915 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7916 solib_unloaded observer. We skip objfiles that are not
7917 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7918 main objfile). */
7919 if ((objfile->flags & OBJF_SHARED) == 0
7920 || (objfile->flags & OBJF_USERLOADED) == 0)
7921 return;
7922
7923 ALL_BREAKPOINTS (b)
7924 {
7925 struct bp_location *loc;
7926 int bp_modified = 0;
7927
7928 if (!is_breakpoint (b) && !is_tracepoint (b))
7929 continue;
7930
7931 for (loc = b->loc; loc != NULL; loc = loc->next)
7932 {
7933 CORE_ADDR loc_addr = loc->address;
7934
7935 if (loc->loc_type != bp_loc_hardware_breakpoint
7936 && loc->loc_type != bp_loc_software_breakpoint)
7937 continue;
7938
7939 if (loc->shlib_disabled != 0)
7940 continue;
7941
7942 if (objfile->pspace != loc->pspace)
7943 continue;
7944
7945 if (loc->loc_type != bp_loc_hardware_breakpoint
7946 && loc->loc_type != bp_loc_software_breakpoint)
7947 continue;
7948
7949 if (is_addr_in_objfile (loc_addr, objfile))
7950 {
7951 loc->shlib_disabled = 1;
7952 /* At this point, we don't know whether the object was
7953 unmapped from the inferior or not, so leave the
7954 inserted flag alone. We'll handle failure to
7955 uninsert quietly, in case the object was indeed
7956 unmapped. */
7957
7958 mark_breakpoint_location_modified (loc);
7959
7960 bp_modified = 1;
7961 }
7962 }
7963
7964 if (bp_modified)
7965 observer_notify_breakpoint_modified (b);
7966 }
7967 }
7968
7969 /* FORK & VFORK catchpoints. */
7970
7971 /* An instance of this type is used to represent a fork or vfork
7972 catchpoint. It includes a "struct breakpoint" as a kind of base
7973 class; users downcast to "struct breakpoint *" when needed. A
7974 breakpoint is really of this type iff its ops pointer points to
7975 CATCH_FORK_BREAKPOINT_OPS. */
7976
7977 struct fork_catchpoint
7978 {
7979 /* The base class. */
7980 struct breakpoint base;
7981
7982 /* Process id of a child process whose forking triggered this
7983 catchpoint. This field is only valid immediately after this
7984 catchpoint has triggered. */
7985 ptid_t forked_inferior_pid;
7986 };
7987
7988 /* Implement the "insert" breakpoint_ops method for fork
7989 catchpoints. */
7990
7991 static int
7992 insert_catch_fork (struct bp_location *bl)
7993 {
7994 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7995 }
7996
7997 /* Implement the "remove" breakpoint_ops method for fork
7998 catchpoints. */
7999
8000 static int
8001 remove_catch_fork (struct bp_location *bl)
8002 {
8003 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8004 }
8005
8006 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8007 catchpoints. */
8008
8009 static int
8010 breakpoint_hit_catch_fork (const struct bp_location *bl,
8011 struct address_space *aspace, CORE_ADDR bp_addr,
8012 const struct target_waitstatus *ws)
8013 {
8014 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8015
8016 if (ws->kind != TARGET_WAITKIND_FORKED)
8017 return 0;
8018
8019 c->forked_inferior_pid = ws->value.related_pid;
8020 return 1;
8021 }
8022
8023 /* Implement the "print_it" breakpoint_ops method for fork
8024 catchpoints. */
8025
8026 static enum print_stop_action
8027 print_it_catch_fork (bpstat bs)
8028 {
8029 struct ui_out *uiout = current_uiout;
8030 struct breakpoint *b = bs->breakpoint_at;
8031 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8032
8033 annotate_catchpoint (b->number);
8034 if (b->disposition == disp_del)
8035 ui_out_text (uiout, "\nTemporary catchpoint ");
8036 else
8037 ui_out_text (uiout, "\nCatchpoint ");
8038 if (ui_out_is_mi_like_p (uiout))
8039 {
8040 ui_out_field_string (uiout, "reason",
8041 async_reason_lookup (EXEC_ASYNC_FORK));
8042 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8043 }
8044 ui_out_field_int (uiout, "bkptno", b->number);
8045 ui_out_text (uiout, " (forked process ");
8046 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8047 ui_out_text (uiout, "), ");
8048 return PRINT_SRC_AND_LOC;
8049 }
8050
8051 /* Implement the "print_one" breakpoint_ops method for fork
8052 catchpoints. */
8053
8054 static void
8055 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8056 {
8057 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8058 struct value_print_options opts;
8059 struct ui_out *uiout = current_uiout;
8060
8061 get_user_print_options (&opts);
8062
8063 /* Field 4, the address, is omitted (which makes the columns not
8064 line up too nicely with the headers, but the effect is relatively
8065 readable). */
8066 if (opts.addressprint)
8067 ui_out_field_skip (uiout, "addr");
8068 annotate_field (5);
8069 ui_out_text (uiout, "fork");
8070 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8071 {
8072 ui_out_text (uiout, ", process ");
8073 ui_out_field_int (uiout, "what",
8074 ptid_get_pid (c->forked_inferior_pid));
8075 ui_out_spaces (uiout, 1);
8076 }
8077
8078 if (ui_out_is_mi_like_p (uiout))
8079 ui_out_field_string (uiout, "catch-type", "fork");
8080 }
8081
8082 /* Implement the "print_mention" breakpoint_ops method for fork
8083 catchpoints. */
8084
8085 static void
8086 print_mention_catch_fork (struct breakpoint *b)
8087 {
8088 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8089 }
8090
8091 /* Implement the "print_recreate" breakpoint_ops method for fork
8092 catchpoints. */
8093
8094 static void
8095 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8096 {
8097 fprintf_unfiltered (fp, "catch fork");
8098 print_recreate_thread (b, fp);
8099 }
8100
8101 /* The breakpoint_ops structure to be used in fork catchpoints. */
8102
8103 static struct breakpoint_ops catch_fork_breakpoint_ops;
8104
8105 /* Implement the "insert" breakpoint_ops method for vfork
8106 catchpoints. */
8107
8108 static int
8109 insert_catch_vfork (struct bp_location *bl)
8110 {
8111 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8112 }
8113
8114 /* Implement the "remove" breakpoint_ops method for vfork
8115 catchpoints. */
8116
8117 static int
8118 remove_catch_vfork (struct bp_location *bl)
8119 {
8120 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8121 }
8122
8123 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8124 catchpoints. */
8125
8126 static int
8127 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8128 struct address_space *aspace, CORE_ADDR bp_addr,
8129 const struct target_waitstatus *ws)
8130 {
8131 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8132
8133 if (ws->kind != TARGET_WAITKIND_VFORKED)
8134 return 0;
8135
8136 c->forked_inferior_pid = ws->value.related_pid;
8137 return 1;
8138 }
8139
8140 /* Implement the "print_it" breakpoint_ops method for vfork
8141 catchpoints. */
8142
8143 static enum print_stop_action
8144 print_it_catch_vfork (bpstat bs)
8145 {
8146 struct ui_out *uiout = current_uiout;
8147 struct breakpoint *b = bs->breakpoint_at;
8148 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8149
8150 annotate_catchpoint (b->number);
8151 if (b->disposition == disp_del)
8152 ui_out_text (uiout, "\nTemporary catchpoint ");
8153 else
8154 ui_out_text (uiout, "\nCatchpoint ");
8155 if (ui_out_is_mi_like_p (uiout))
8156 {
8157 ui_out_field_string (uiout, "reason",
8158 async_reason_lookup (EXEC_ASYNC_VFORK));
8159 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8160 }
8161 ui_out_field_int (uiout, "bkptno", b->number);
8162 ui_out_text (uiout, " (vforked process ");
8163 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8164 ui_out_text (uiout, "), ");
8165 return PRINT_SRC_AND_LOC;
8166 }
8167
8168 /* Implement the "print_one" breakpoint_ops method for vfork
8169 catchpoints. */
8170
8171 static void
8172 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8173 {
8174 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8175 struct value_print_options opts;
8176 struct ui_out *uiout = current_uiout;
8177
8178 get_user_print_options (&opts);
8179 /* Field 4, the address, is omitted (which makes the columns not
8180 line up too nicely with the headers, but the effect is relatively
8181 readable). */
8182 if (opts.addressprint)
8183 ui_out_field_skip (uiout, "addr");
8184 annotate_field (5);
8185 ui_out_text (uiout, "vfork");
8186 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8187 {
8188 ui_out_text (uiout, ", process ");
8189 ui_out_field_int (uiout, "what",
8190 ptid_get_pid (c->forked_inferior_pid));
8191 ui_out_spaces (uiout, 1);
8192 }
8193
8194 if (ui_out_is_mi_like_p (uiout))
8195 ui_out_field_string (uiout, "catch-type", "vfork");
8196 }
8197
8198 /* Implement the "print_mention" breakpoint_ops method for vfork
8199 catchpoints. */
8200
8201 static void
8202 print_mention_catch_vfork (struct breakpoint *b)
8203 {
8204 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8205 }
8206
8207 /* Implement the "print_recreate" breakpoint_ops method for vfork
8208 catchpoints. */
8209
8210 static void
8211 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8212 {
8213 fprintf_unfiltered (fp, "catch vfork");
8214 print_recreate_thread (b, fp);
8215 }
8216
8217 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8218
8219 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8220
8221 /* An instance of this type is used to represent an solib catchpoint.
8222 It includes a "struct breakpoint" as a kind of base class; users
8223 downcast to "struct breakpoint *" when needed. A breakpoint is
8224 really of this type iff its ops pointer points to
8225 CATCH_SOLIB_BREAKPOINT_OPS. */
8226
8227 struct solib_catchpoint
8228 {
8229 /* The base class. */
8230 struct breakpoint base;
8231
8232 /* True for "catch load", false for "catch unload". */
8233 unsigned char is_load;
8234
8235 /* Regular expression to match, if any. COMPILED is only valid when
8236 REGEX is non-NULL. */
8237 char *regex;
8238 regex_t compiled;
8239 };
8240
8241 static void
8242 dtor_catch_solib (struct breakpoint *b)
8243 {
8244 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8245
8246 if (self->regex)
8247 regfree (&self->compiled);
8248 xfree (self->regex);
8249
8250 base_breakpoint_ops.dtor (b);
8251 }
8252
8253 static int
8254 insert_catch_solib (struct bp_location *ignore)
8255 {
8256 return 0;
8257 }
8258
8259 static int
8260 remove_catch_solib (struct bp_location *ignore)
8261 {
8262 return 0;
8263 }
8264
8265 static int
8266 breakpoint_hit_catch_solib (const struct bp_location *bl,
8267 struct address_space *aspace,
8268 CORE_ADDR bp_addr,
8269 const struct target_waitstatus *ws)
8270 {
8271 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8272 struct breakpoint *other;
8273
8274 if (ws->kind == TARGET_WAITKIND_LOADED)
8275 return 1;
8276
8277 ALL_BREAKPOINTS (other)
8278 {
8279 struct bp_location *other_bl;
8280
8281 if (other == bl->owner)
8282 continue;
8283
8284 if (other->type != bp_shlib_event)
8285 continue;
8286
8287 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8288 continue;
8289
8290 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8291 {
8292 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8293 return 1;
8294 }
8295 }
8296
8297 return 0;
8298 }
8299
8300 static void
8301 check_status_catch_solib (struct bpstats *bs)
8302 {
8303 struct solib_catchpoint *self
8304 = (struct solib_catchpoint *) bs->breakpoint_at;
8305 int ix;
8306
8307 if (self->is_load)
8308 {
8309 struct so_list *iter;
8310
8311 for (ix = 0;
8312 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8313 ix, iter);
8314 ++ix)
8315 {
8316 if (!self->regex
8317 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8318 return;
8319 }
8320 }
8321 else
8322 {
8323 char *iter;
8324
8325 for (ix = 0;
8326 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8327 ix, iter);
8328 ++ix)
8329 {
8330 if (!self->regex
8331 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8332 return;
8333 }
8334 }
8335
8336 bs->stop = 0;
8337 bs->print_it = print_it_noop;
8338 }
8339
8340 static enum print_stop_action
8341 print_it_catch_solib (bpstat bs)
8342 {
8343 struct breakpoint *b = bs->breakpoint_at;
8344 struct ui_out *uiout = current_uiout;
8345
8346 annotate_catchpoint (b->number);
8347 if (b->disposition == disp_del)
8348 ui_out_text (uiout, "\nTemporary catchpoint ");
8349 else
8350 ui_out_text (uiout, "\nCatchpoint ");
8351 ui_out_field_int (uiout, "bkptno", b->number);
8352 ui_out_text (uiout, "\n");
8353 if (ui_out_is_mi_like_p (uiout))
8354 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8355 print_solib_event (1);
8356 return PRINT_SRC_AND_LOC;
8357 }
8358
8359 static void
8360 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8361 {
8362 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8363 struct value_print_options opts;
8364 struct ui_out *uiout = current_uiout;
8365 char *msg;
8366
8367 get_user_print_options (&opts);
8368 /* Field 4, the address, is omitted (which makes the columns not
8369 line up too nicely with the headers, but the effect is relatively
8370 readable). */
8371 if (opts.addressprint)
8372 {
8373 annotate_field (4);
8374 ui_out_field_skip (uiout, "addr");
8375 }
8376
8377 annotate_field (5);
8378 if (self->is_load)
8379 {
8380 if (self->regex)
8381 msg = xstrprintf (_("load of library matching %s"), self->regex);
8382 else
8383 msg = xstrdup (_("load of library"));
8384 }
8385 else
8386 {
8387 if (self->regex)
8388 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8389 else
8390 msg = xstrdup (_("unload of library"));
8391 }
8392 ui_out_field_string (uiout, "what", msg);
8393 xfree (msg);
8394
8395 if (ui_out_is_mi_like_p (uiout))
8396 ui_out_field_string (uiout, "catch-type",
8397 self->is_load ? "load" : "unload");
8398 }
8399
8400 static void
8401 print_mention_catch_solib (struct breakpoint *b)
8402 {
8403 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8404
8405 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8406 self->is_load ? "load" : "unload");
8407 }
8408
8409 static void
8410 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8411 {
8412 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8413
8414 fprintf_unfiltered (fp, "%s %s",
8415 b->disposition == disp_del ? "tcatch" : "catch",
8416 self->is_load ? "load" : "unload");
8417 if (self->regex)
8418 fprintf_unfiltered (fp, " %s", self->regex);
8419 fprintf_unfiltered (fp, "\n");
8420 }
8421
8422 static struct breakpoint_ops catch_solib_breakpoint_ops;
8423
8424 /* Shared helper function (MI and CLI) for creating and installing
8425 a shared object event catchpoint. If IS_LOAD is non-zero then
8426 the events to be caught are load events, otherwise they are
8427 unload events. If IS_TEMP is non-zero the catchpoint is a
8428 temporary one. If ENABLED is non-zero the catchpoint is
8429 created in an enabled state. */
8430
8431 void
8432 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8433 {
8434 struct solib_catchpoint *c;
8435 struct gdbarch *gdbarch = get_current_arch ();
8436 struct cleanup *cleanup;
8437
8438 if (!arg)
8439 arg = "";
8440 arg = skip_spaces (arg);
8441
8442 c = XCNEW (struct solib_catchpoint);
8443 cleanup = make_cleanup (xfree, c);
8444
8445 if (*arg != '\0')
8446 {
8447 int errcode;
8448
8449 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8450 if (errcode != 0)
8451 {
8452 char *err = get_regcomp_error (errcode, &c->compiled);
8453
8454 make_cleanup (xfree, err);
8455 error (_("Invalid regexp (%s): %s"), err, arg);
8456 }
8457 c->regex = xstrdup (arg);
8458 }
8459
8460 c->is_load = is_load;
8461 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8462 &catch_solib_breakpoint_ops);
8463
8464 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8465
8466 discard_cleanups (cleanup);
8467 install_breakpoint (0, &c->base, 1);
8468 }
8469
8470 /* A helper function that does all the work for "catch load" and
8471 "catch unload". */
8472
8473 static void
8474 catch_load_or_unload (char *arg, int from_tty, int is_load,
8475 struct cmd_list_element *command)
8476 {
8477 int tempflag;
8478 const int enabled = 1;
8479
8480 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8481
8482 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8483 }
8484
8485 static void
8486 catch_load_command_1 (char *arg, int from_tty,
8487 struct cmd_list_element *command)
8488 {
8489 catch_load_or_unload (arg, from_tty, 1, command);
8490 }
8491
8492 static void
8493 catch_unload_command_1 (char *arg, int from_tty,
8494 struct cmd_list_element *command)
8495 {
8496 catch_load_or_unload (arg, from_tty, 0, command);
8497 }
8498
8499 /* An instance of this type is used to represent a syscall catchpoint.
8500 It includes a "struct breakpoint" as a kind of base class; users
8501 downcast to "struct breakpoint *" when needed. A breakpoint is
8502 really of this type iff its ops pointer points to
8503 CATCH_SYSCALL_BREAKPOINT_OPS. */
8504
8505 struct syscall_catchpoint
8506 {
8507 /* The base class. */
8508 struct breakpoint base;
8509
8510 /* Syscall numbers used for the 'catch syscall' feature. If no
8511 syscall has been specified for filtering, its value is NULL.
8512 Otherwise, it holds a list of all syscalls to be caught. The
8513 list elements are allocated with xmalloc. */
8514 VEC(int) *syscalls_to_be_caught;
8515 };
8516
8517 /* Implement the "dtor" breakpoint_ops method for syscall
8518 catchpoints. */
8519
8520 static void
8521 dtor_catch_syscall (struct breakpoint *b)
8522 {
8523 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8524
8525 VEC_free (int, c->syscalls_to_be_caught);
8526
8527 base_breakpoint_ops.dtor (b);
8528 }
8529
8530 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8531
8532 struct catch_syscall_inferior_data
8533 {
8534 /* We keep a count of the number of times the user has requested a
8535 particular syscall to be tracked, and pass this information to the
8536 target. This lets capable targets implement filtering directly. */
8537
8538 /* Number of times that "any" syscall is requested. */
8539 int any_syscall_count;
8540
8541 /* Count of each system call. */
8542 VEC(int) *syscalls_counts;
8543
8544 /* This counts all syscall catch requests, so we can readily determine
8545 if any catching is necessary. */
8546 int total_syscalls_count;
8547 };
8548
8549 static struct catch_syscall_inferior_data*
8550 get_catch_syscall_inferior_data (struct inferior *inf)
8551 {
8552 struct catch_syscall_inferior_data *inf_data;
8553
8554 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8555 if (inf_data == NULL)
8556 {
8557 inf_data = XCNEW (struct catch_syscall_inferior_data);
8558 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8559 }
8560
8561 return inf_data;
8562 }
8563
8564 static void
8565 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8566 {
8567 xfree (arg);
8568 }
8569
8570
8571 /* Implement the "insert" breakpoint_ops method for syscall
8572 catchpoints. */
8573
8574 static int
8575 insert_catch_syscall (struct bp_location *bl)
8576 {
8577 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8578 struct inferior *inf = current_inferior ();
8579 struct catch_syscall_inferior_data *inf_data
8580 = get_catch_syscall_inferior_data (inf);
8581
8582 ++inf_data->total_syscalls_count;
8583 if (!c->syscalls_to_be_caught)
8584 ++inf_data->any_syscall_count;
8585 else
8586 {
8587 int i, iter;
8588
8589 for (i = 0;
8590 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8591 i++)
8592 {
8593 int elem;
8594
8595 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8596 {
8597 int old_size = VEC_length (int, inf_data->syscalls_counts);
8598 uintptr_t vec_addr_offset
8599 = old_size * ((uintptr_t) sizeof (int));
8600 uintptr_t vec_addr;
8601 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8602 vec_addr = ((uintptr_t) VEC_address (int,
8603 inf_data->syscalls_counts)
8604 + vec_addr_offset);
8605 memset ((void *) vec_addr, 0,
8606 (iter + 1 - old_size) * sizeof (int));
8607 }
8608 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8609 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8610 }
8611 }
8612
8613 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8614 inf_data->total_syscalls_count != 0,
8615 inf_data->any_syscall_count,
8616 VEC_length (int,
8617 inf_data->syscalls_counts),
8618 VEC_address (int,
8619 inf_data->syscalls_counts));
8620 }
8621
8622 /* Implement the "remove" breakpoint_ops method for syscall
8623 catchpoints. */
8624
8625 static int
8626 remove_catch_syscall (struct bp_location *bl)
8627 {
8628 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8629 struct inferior *inf = current_inferior ();
8630 struct catch_syscall_inferior_data *inf_data
8631 = get_catch_syscall_inferior_data (inf);
8632
8633 --inf_data->total_syscalls_count;
8634 if (!c->syscalls_to_be_caught)
8635 --inf_data->any_syscall_count;
8636 else
8637 {
8638 int i, iter;
8639
8640 for (i = 0;
8641 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8642 i++)
8643 {
8644 int elem;
8645 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8646 /* Shouldn't happen. */
8647 continue;
8648 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8649 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8650 }
8651 }
8652
8653 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8654 inf_data->total_syscalls_count != 0,
8655 inf_data->any_syscall_count,
8656 VEC_length (int,
8657 inf_data->syscalls_counts),
8658 VEC_address (int,
8659 inf_data->syscalls_counts));
8660 }
8661
8662 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8663 catchpoints. */
8664
8665 static int
8666 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8667 struct address_space *aspace, CORE_ADDR bp_addr,
8668 const struct target_waitstatus *ws)
8669 {
8670 /* We must check if we are catching specific syscalls in this
8671 breakpoint. If we are, then we must guarantee that the called
8672 syscall is the same syscall we are catching. */
8673 int syscall_number = 0;
8674 const struct syscall_catchpoint *c
8675 = (const struct syscall_catchpoint *) bl->owner;
8676
8677 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8678 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8679 return 0;
8680
8681 syscall_number = ws->value.syscall_number;
8682
8683 /* Now, checking if the syscall is the same. */
8684 if (c->syscalls_to_be_caught)
8685 {
8686 int i, iter;
8687
8688 for (i = 0;
8689 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8690 i++)
8691 if (syscall_number == iter)
8692 return 1;
8693
8694 return 0;
8695 }
8696
8697 return 1;
8698 }
8699
8700 /* Implement the "print_it" breakpoint_ops method for syscall
8701 catchpoints. */
8702
8703 static enum print_stop_action
8704 print_it_catch_syscall (bpstat bs)
8705 {
8706 struct ui_out *uiout = current_uiout;
8707 struct breakpoint *b = bs->breakpoint_at;
8708 /* These are needed because we want to know in which state a
8709 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8710 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8711 must print "called syscall" or "returned from syscall". */
8712 ptid_t ptid;
8713 struct target_waitstatus last;
8714 struct syscall s;
8715
8716 get_last_target_status (&ptid, &last);
8717
8718 get_syscall_by_number (last.value.syscall_number, &s);
8719
8720 annotate_catchpoint (b->number);
8721
8722 if (b->disposition == disp_del)
8723 ui_out_text (uiout, "\nTemporary catchpoint ");
8724 else
8725 ui_out_text (uiout, "\nCatchpoint ");
8726 if (ui_out_is_mi_like_p (uiout))
8727 {
8728 ui_out_field_string (uiout, "reason",
8729 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8730 ? EXEC_ASYNC_SYSCALL_ENTRY
8731 : EXEC_ASYNC_SYSCALL_RETURN));
8732 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8733 }
8734 ui_out_field_int (uiout, "bkptno", b->number);
8735
8736 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8737 ui_out_text (uiout, " (call to syscall ");
8738 else
8739 ui_out_text (uiout, " (returned from syscall ");
8740
8741 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8742 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8743 if (s.name != NULL)
8744 ui_out_field_string (uiout, "syscall-name", s.name);
8745
8746 ui_out_text (uiout, "), ");
8747
8748 return PRINT_SRC_AND_LOC;
8749 }
8750
8751 /* Implement the "print_one" breakpoint_ops method for syscall
8752 catchpoints. */
8753
8754 static void
8755 print_one_catch_syscall (struct breakpoint *b,
8756 struct bp_location **last_loc)
8757 {
8758 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8759 struct value_print_options opts;
8760 struct ui_out *uiout = current_uiout;
8761
8762 get_user_print_options (&opts);
8763 /* Field 4, the address, is omitted (which makes the columns not
8764 line up too nicely with the headers, but the effect is relatively
8765 readable). */
8766 if (opts.addressprint)
8767 ui_out_field_skip (uiout, "addr");
8768 annotate_field (5);
8769
8770 if (c->syscalls_to_be_caught
8771 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8772 ui_out_text (uiout, "syscalls \"");
8773 else
8774 ui_out_text (uiout, "syscall \"");
8775
8776 if (c->syscalls_to_be_caught)
8777 {
8778 int i, iter;
8779 char *text = xstrprintf ("%s", "");
8780
8781 for (i = 0;
8782 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8783 i++)
8784 {
8785 char *x = text;
8786 struct syscall s;
8787 get_syscall_by_number (iter, &s);
8788
8789 if (s.name != NULL)
8790 text = xstrprintf ("%s%s, ", text, s.name);
8791 else
8792 text = xstrprintf ("%s%d, ", text, iter);
8793
8794 /* We have to xfree the last 'text' (now stored at 'x')
8795 because xstrprintf dynamically allocates new space for it
8796 on every call. */
8797 xfree (x);
8798 }
8799 /* Remove the last comma. */
8800 text[strlen (text) - 2] = '\0';
8801 ui_out_field_string (uiout, "what", text);
8802 }
8803 else
8804 ui_out_field_string (uiout, "what", "<any syscall>");
8805 ui_out_text (uiout, "\" ");
8806
8807 if (ui_out_is_mi_like_p (uiout))
8808 ui_out_field_string (uiout, "catch-type", "syscall");
8809 }
8810
8811 /* Implement the "print_mention" breakpoint_ops method for syscall
8812 catchpoints. */
8813
8814 static void
8815 print_mention_catch_syscall (struct breakpoint *b)
8816 {
8817 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8818
8819 if (c->syscalls_to_be_caught)
8820 {
8821 int i, iter;
8822
8823 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8824 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8825 else
8826 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8827
8828 for (i = 0;
8829 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8830 i++)
8831 {
8832 struct syscall s;
8833 get_syscall_by_number (iter, &s);
8834
8835 if (s.name)
8836 printf_filtered (" '%s' [%d]", s.name, s.number);
8837 else
8838 printf_filtered (" %d", s.number);
8839 }
8840 printf_filtered (")");
8841 }
8842 else
8843 printf_filtered (_("Catchpoint %d (any syscall)"),
8844 b->number);
8845 }
8846
8847 /* Implement the "print_recreate" breakpoint_ops method for syscall
8848 catchpoints. */
8849
8850 static void
8851 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8852 {
8853 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8854
8855 fprintf_unfiltered (fp, "catch syscall");
8856
8857 if (c->syscalls_to_be_caught)
8858 {
8859 int i, iter;
8860
8861 for (i = 0;
8862 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8863 i++)
8864 {
8865 struct syscall s;
8866
8867 get_syscall_by_number (iter, &s);
8868 if (s.name)
8869 fprintf_unfiltered (fp, " %s", s.name);
8870 else
8871 fprintf_unfiltered (fp, " %d", s.number);
8872 }
8873 }
8874 print_recreate_thread (b, fp);
8875 }
8876
8877 /* The breakpoint_ops structure to be used in syscall catchpoints. */
8878
8879 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8880
8881 /* Returns non-zero if 'b' is a syscall catchpoint. */
8882
8883 static int
8884 syscall_catchpoint_p (struct breakpoint *b)
8885 {
8886 return (b->ops == &catch_syscall_breakpoint_ops);
8887 }
8888
8889 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8890 is non-zero, then make the breakpoint temporary. If COND_STRING is
8891 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8892 the breakpoint_ops structure associated to the catchpoint. */
8893
8894 void
8895 init_catchpoint (struct breakpoint *b,
8896 struct gdbarch *gdbarch, int tempflag,
8897 char *cond_string,
8898 const struct breakpoint_ops *ops)
8899 {
8900 struct symtab_and_line sal;
8901
8902 init_sal (&sal);
8903 sal.pspace = current_program_space;
8904
8905 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8906
8907 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8908 b->disposition = tempflag ? disp_del : disp_donttouch;
8909 }
8910
8911 void
8912 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8913 {
8914 add_to_breakpoint_chain (b);
8915 set_breakpoint_number (internal, b);
8916 if (is_tracepoint (b))
8917 set_tracepoint_count (breakpoint_count);
8918 if (!internal)
8919 mention (b);
8920 observer_notify_breakpoint_created (b);
8921
8922 if (update_gll)
8923 update_global_location_list (UGLL_MAY_INSERT);
8924 }
8925
8926 static void
8927 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8928 int tempflag, char *cond_string,
8929 const struct breakpoint_ops *ops)
8930 {
8931 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8932
8933 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8934
8935 c->forked_inferior_pid = null_ptid;
8936
8937 install_breakpoint (0, &c->base, 1);
8938 }
8939
8940 /* Exec catchpoints. */
8941
8942 /* An instance of this type is used to represent an exec catchpoint.
8943 It includes a "struct breakpoint" as a kind of base class; users
8944 downcast to "struct breakpoint *" when needed. A breakpoint is
8945 really of this type iff its ops pointer points to
8946 CATCH_EXEC_BREAKPOINT_OPS. */
8947
8948 struct exec_catchpoint
8949 {
8950 /* The base class. */
8951 struct breakpoint base;
8952
8953 /* Filename of a program whose exec triggered this catchpoint.
8954 This field is only valid immediately after this catchpoint has
8955 triggered. */
8956 char *exec_pathname;
8957 };
8958
8959 /* Implement the "dtor" breakpoint_ops method for exec
8960 catchpoints. */
8961
8962 static void
8963 dtor_catch_exec (struct breakpoint *b)
8964 {
8965 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8966
8967 xfree (c->exec_pathname);
8968
8969 base_breakpoint_ops.dtor (b);
8970 }
8971
8972 static int
8973 insert_catch_exec (struct bp_location *bl)
8974 {
8975 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8976 }
8977
8978 static int
8979 remove_catch_exec (struct bp_location *bl)
8980 {
8981 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8982 }
8983
8984 static int
8985 breakpoint_hit_catch_exec (const struct bp_location *bl,
8986 struct address_space *aspace, CORE_ADDR bp_addr,
8987 const struct target_waitstatus *ws)
8988 {
8989 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8990
8991 if (ws->kind != TARGET_WAITKIND_EXECD)
8992 return 0;
8993
8994 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8995 return 1;
8996 }
8997
8998 static enum print_stop_action
8999 print_it_catch_exec (bpstat bs)
9000 {
9001 struct ui_out *uiout = current_uiout;
9002 struct breakpoint *b = bs->breakpoint_at;
9003 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
9004
9005 annotate_catchpoint (b->number);
9006 if (b->disposition == disp_del)
9007 ui_out_text (uiout, "\nTemporary catchpoint ");
9008 else
9009 ui_out_text (uiout, "\nCatchpoint ");
9010 if (ui_out_is_mi_like_p (uiout))
9011 {
9012 ui_out_field_string (uiout, "reason",
9013 async_reason_lookup (EXEC_ASYNC_EXEC));
9014 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9015 }
9016 ui_out_field_int (uiout, "bkptno", b->number);
9017 ui_out_text (uiout, " (exec'd ");
9018 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
9019 ui_out_text (uiout, "), ");
9020
9021 return PRINT_SRC_AND_LOC;
9022 }
9023
9024 static void
9025 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
9026 {
9027 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
9028 struct value_print_options opts;
9029 struct ui_out *uiout = current_uiout;
9030
9031 get_user_print_options (&opts);
9032
9033 /* Field 4, the address, is omitted (which makes the columns
9034 not line up too nicely with the headers, but the effect
9035 is relatively readable). */
9036 if (opts.addressprint)
9037 ui_out_field_skip (uiout, "addr");
9038 annotate_field (5);
9039 ui_out_text (uiout, "exec");
9040 if (c->exec_pathname != NULL)
9041 {
9042 ui_out_text (uiout, ", program \"");
9043 ui_out_field_string (uiout, "what", c->exec_pathname);
9044 ui_out_text (uiout, "\" ");
9045 }
9046
9047 if (ui_out_is_mi_like_p (uiout))
9048 ui_out_field_string (uiout, "catch-type", "exec");
9049 }
9050
9051 static void
9052 print_mention_catch_exec (struct breakpoint *b)
9053 {
9054 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9055 }
9056
9057 /* Implement the "print_recreate" breakpoint_ops method for exec
9058 catchpoints. */
9059
9060 static void
9061 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9062 {
9063 fprintf_unfiltered (fp, "catch exec");
9064 print_recreate_thread (b, fp);
9065 }
9066
9067 static struct breakpoint_ops catch_exec_breakpoint_ops;
9068
9069 static void
9070 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
9071 const struct breakpoint_ops *ops)
9072 {
9073 struct syscall_catchpoint *c;
9074 struct gdbarch *gdbarch = get_current_arch ();
9075
9076 c = XNEW (struct syscall_catchpoint);
9077 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9078 c->syscalls_to_be_caught = filter;
9079
9080 install_breakpoint (0, &c->base, 1);
9081 }
9082
9083 static int
9084 hw_breakpoint_used_count (void)
9085 {
9086 int i = 0;
9087 struct breakpoint *b;
9088 struct bp_location *bl;
9089
9090 ALL_BREAKPOINTS (b)
9091 {
9092 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
9093 for (bl = b->loc; bl; bl = bl->next)
9094 {
9095 /* Special types of hardware breakpoints may use more than
9096 one register. */
9097 i += b->ops->resources_needed (bl);
9098 }
9099 }
9100
9101 return i;
9102 }
9103
9104 /* Returns the resources B would use if it were a hardware
9105 watchpoint. */
9106
9107 static int
9108 hw_watchpoint_use_count (struct breakpoint *b)
9109 {
9110 int i = 0;
9111 struct bp_location *bl;
9112
9113 if (!breakpoint_enabled (b))
9114 return 0;
9115
9116 for (bl = b->loc; bl; bl = bl->next)
9117 {
9118 /* Special types of hardware watchpoints may use more than
9119 one register. */
9120 i += b->ops->resources_needed (bl);
9121 }
9122
9123 return i;
9124 }
9125
9126 /* Returns the sum the used resources of all hardware watchpoints of
9127 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9128 the sum of the used resources of all hardware watchpoints of other
9129 types _not_ TYPE. */
9130
9131 static int
9132 hw_watchpoint_used_count_others (struct breakpoint *except,
9133 enum bptype type, int *other_type_used)
9134 {
9135 int i = 0;
9136 struct breakpoint *b;
9137
9138 *other_type_used = 0;
9139 ALL_BREAKPOINTS (b)
9140 {
9141 if (b == except)
9142 continue;
9143 if (!breakpoint_enabled (b))
9144 continue;
9145
9146 if (b->type == type)
9147 i += hw_watchpoint_use_count (b);
9148 else if (is_hardware_watchpoint (b))
9149 *other_type_used = 1;
9150 }
9151
9152 return i;
9153 }
9154
9155 void
9156 disable_watchpoints_before_interactive_call_start (void)
9157 {
9158 struct breakpoint *b;
9159
9160 ALL_BREAKPOINTS (b)
9161 {
9162 if (is_watchpoint (b) && breakpoint_enabled (b))
9163 {
9164 b->enable_state = bp_call_disabled;
9165 update_global_location_list (UGLL_DONT_INSERT);
9166 }
9167 }
9168 }
9169
9170 void
9171 enable_watchpoints_after_interactive_call_stop (void)
9172 {
9173 struct breakpoint *b;
9174
9175 ALL_BREAKPOINTS (b)
9176 {
9177 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
9178 {
9179 b->enable_state = bp_enabled;
9180 update_global_location_list (UGLL_MAY_INSERT);
9181 }
9182 }
9183 }
9184
9185 void
9186 disable_breakpoints_before_startup (void)
9187 {
9188 current_program_space->executing_startup = 1;
9189 update_global_location_list (UGLL_DONT_INSERT);
9190 }
9191
9192 void
9193 enable_breakpoints_after_startup (void)
9194 {
9195 current_program_space->executing_startup = 0;
9196 breakpoint_re_set ();
9197 }
9198
9199
9200 /* Set a breakpoint that will evaporate an end of command
9201 at address specified by SAL.
9202 Restrict it to frame FRAME if FRAME is nonzero. */
9203
9204 struct breakpoint *
9205 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9206 struct frame_id frame_id, enum bptype type)
9207 {
9208 struct breakpoint *b;
9209
9210 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9211 tail-called one. */
9212 gdb_assert (!frame_id_artificial_p (frame_id));
9213
9214 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
9215 b->enable_state = bp_enabled;
9216 b->disposition = disp_donttouch;
9217 b->frame_id = frame_id;
9218
9219 /* If we're debugging a multi-threaded program, then we want
9220 momentary breakpoints to be active in only a single thread of
9221 control. */
9222 if (in_thread_list (inferior_ptid))
9223 b->thread = pid_to_thread_id (inferior_ptid);
9224
9225 update_global_location_list_nothrow (UGLL_MAY_INSERT);
9226
9227 return b;
9228 }
9229
9230 /* Make a momentary breakpoint based on the master breakpoint ORIG.
9231 The new breakpoint will have type TYPE, use OPS as its
9232 breakpoint_ops, and will set enabled to LOC_ENABLED. */
9233
9234 static struct breakpoint *
9235 momentary_breakpoint_from_master (struct breakpoint *orig,
9236 enum bptype type,
9237 const struct breakpoint_ops *ops,
9238 int loc_enabled)
9239 {
9240 struct breakpoint *copy;
9241
9242 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
9243 copy->loc = allocate_bp_location (copy);
9244 set_breakpoint_location_function (copy->loc, 1);
9245
9246 copy->loc->gdbarch = orig->loc->gdbarch;
9247 copy->loc->requested_address = orig->loc->requested_address;
9248 copy->loc->address = orig->loc->address;
9249 copy->loc->section = orig->loc->section;
9250 copy->loc->pspace = orig->loc->pspace;
9251 copy->loc->probe = orig->loc->probe;
9252 copy->loc->line_number = orig->loc->line_number;
9253 copy->loc->symtab = orig->loc->symtab;
9254 copy->loc->enabled = loc_enabled;
9255 copy->frame_id = orig->frame_id;
9256 copy->thread = orig->thread;
9257 copy->pspace = orig->pspace;
9258
9259 copy->enable_state = bp_enabled;
9260 copy->disposition = disp_donttouch;
9261 copy->number = internal_breakpoint_number--;
9262
9263 update_global_location_list_nothrow (UGLL_DONT_INSERT);
9264 return copy;
9265 }
9266
9267 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9268 ORIG is NULL. */
9269
9270 struct breakpoint *
9271 clone_momentary_breakpoint (struct breakpoint *orig)
9272 {
9273 /* If there's nothing to clone, then return nothing. */
9274 if (orig == NULL)
9275 return NULL;
9276
9277 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9278 }
9279
9280 struct breakpoint *
9281 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9282 enum bptype type)
9283 {
9284 struct symtab_and_line sal;
9285
9286 sal = find_pc_line (pc, 0);
9287 sal.pc = pc;
9288 sal.section = find_pc_overlay (pc);
9289 sal.explicit_pc = 1;
9290
9291 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9292 }
9293 \f
9294
9295 /* Tell the user we have just set a breakpoint B. */
9296
9297 static void
9298 mention (struct breakpoint *b)
9299 {
9300 b->ops->print_mention (b);
9301 if (ui_out_is_mi_like_p (current_uiout))
9302 return;
9303 printf_filtered ("\n");
9304 }
9305 \f
9306
9307 static struct bp_location *
9308 add_location_to_breakpoint (struct breakpoint *b,
9309 const struct symtab_and_line *sal)
9310 {
9311 struct bp_location *loc, **tmp;
9312 CORE_ADDR adjusted_address;
9313 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9314
9315 if (loc_gdbarch == NULL)
9316 loc_gdbarch = b->gdbarch;
9317
9318 /* Adjust the breakpoint's address prior to allocating a location.
9319 Once we call allocate_bp_location(), that mostly uninitialized
9320 location will be placed on the location chain. Adjustment of the
9321 breakpoint may cause target_read_memory() to be called and we do
9322 not want its scan of the location chain to find a breakpoint and
9323 location that's only been partially initialized. */
9324 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9325 sal->pc, b->type);
9326
9327 /* Sort the locations by their ADDRESS. */
9328 loc = allocate_bp_location (b);
9329 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9330 tmp = &((*tmp)->next))
9331 ;
9332 loc->next = *tmp;
9333 *tmp = loc;
9334
9335 loc->requested_address = sal->pc;
9336 loc->address = adjusted_address;
9337 loc->pspace = sal->pspace;
9338 loc->probe.probe = sal->probe;
9339 loc->probe.objfile = sal->objfile;
9340 gdb_assert (loc->pspace != NULL);
9341 loc->section = sal->section;
9342 loc->gdbarch = loc_gdbarch;
9343 loc->line_number = sal->line;
9344 loc->symtab = sal->symtab;
9345
9346 set_breakpoint_location_function (loc,
9347 sal->explicit_pc || sal->explicit_line);
9348 return loc;
9349 }
9350 \f
9351
9352 /* Return 1 if LOC is pointing to a permanent breakpoint,
9353 return 0 otherwise. */
9354
9355 static int
9356 bp_loc_is_permanent (struct bp_location *loc)
9357 {
9358 int len;
9359 CORE_ADDR addr;
9360 const gdb_byte *bpoint;
9361 gdb_byte *target_mem;
9362 struct cleanup *cleanup;
9363 int retval = 0;
9364
9365 gdb_assert (loc != NULL);
9366
9367 addr = loc->address;
9368 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
9369
9370 /* Software breakpoints unsupported? */
9371 if (bpoint == NULL)
9372 return 0;
9373
9374 target_mem = alloca (len);
9375
9376 /* Enable the automatic memory restoration from breakpoints while
9377 we read the memory. Otherwise we could say about our temporary
9378 breakpoints they are permanent. */
9379 cleanup = save_current_space_and_thread ();
9380
9381 switch_to_program_space_and_thread (loc->pspace);
9382 make_show_memory_breakpoints_cleanup (0);
9383
9384 if (target_read_memory (loc->address, target_mem, len) == 0
9385 && memcmp (target_mem, bpoint, len) == 0)
9386 retval = 1;
9387
9388 do_cleanups (cleanup);
9389
9390 return retval;
9391 }
9392
9393 /* Build a command list for the dprintf corresponding to the current
9394 settings of the dprintf style options. */
9395
9396 static void
9397 update_dprintf_command_list (struct breakpoint *b)
9398 {
9399 char *dprintf_args = b->extra_string;
9400 char *printf_line = NULL;
9401
9402 if (!dprintf_args)
9403 return;
9404
9405 dprintf_args = skip_spaces (dprintf_args);
9406
9407 /* Allow a comma, as it may have terminated a location, but don't
9408 insist on it. */
9409 if (*dprintf_args == ',')
9410 ++dprintf_args;
9411 dprintf_args = skip_spaces (dprintf_args);
9412
9413 if (*dprintf_args != '"')
9414 error (_("Bad format string, missing '\"'."));
9415
9416 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9417 printf_line = xstrprintf ("printf %s", dprintf_args);
9418 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9419 {
9420 if (!dprintf_function)
9421 error (_("No function supplied for dprintf call"));
9422
9423 if (dprintf_channel && strlen (dprintf_channel) > 0)
9424 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9425 dprintf_function,
9426 dprintf_channel,
9427 dprintf_args);
9428 else
9429 printf_line = xstrprintf ("call (void) %s (%s)",
9430 dprintf_function,
9431 dprintf_args);
9432 }
9433 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9434 {
9435 if (target_can_run_breakpoint_commands ())
9436 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9437 else
9438 {
9439 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9440 printf_line = xstrprintf ("printf %s", dprintf_args);
9441 }
9442 }
9443 else
9444 internal_error (__FILE__, __LINE__,
9445 _("Invalid dprintf style."));
9446
9447 gdb_assert (printf_line != NULL);
9448 /* Manufacture a printf sequence. */
9449 {
9450 struct command_line *printf_cmd_line
9451 = xmalloc (sizeof (struct command_line));
9452
9453 printf_cmd_line = xmalloc (sizeof (struct command_line));
9454 printf_cmd_line->control_type = simple_control;
9455 printf_cmd_line->body_count = 0;
9456 printf_cmd_line->body_list = NULL;
9457 printf_cmd_line->next = NULL;
9458 printf_cmd_line->line = printf_line;
9459
9460 breakpoint_set_commands (b, printf_cmd_line);
9461 }
9462 }
9463
9464 /* Update all dprintf commands, making their command lists reflect
9465 current style settings. */
9466
9467 static void
9468 update_dprintf_commands (char *args, int from_tty,
9469 struct cmd_list_element *c)
9470 {
9471 struct breakpoint *b;
9472
9473 ALL_BREAKPOINTS (b)
9474 {
9475 if (b->type == bp_dprintf)
9476 update_dprintf_command_list (b);
9477 }
9478 }
9479
9480 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9481 as textual description of the location, and COND_STRING
9482 as condition expression. */
9483
9484 static void
9485 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9486 struct symtabs_and_lines sals, char *addr_string,
9487 char *filter, char *cond_string,
9488 char *extra_string,
9489 enum bptype type, enum bpdisp disposition,
9490 int thread, int task, int ignore_count,
9491 const struct breakpoint_ops *ops, int from_tty,
9492 int enabled, int internal, unsigned flags,
9493 int display_canonical)
9494 {
9495 int i;
9496
9497 if (type == bp_hardware_breakpoint)
9498 {
9499 int target_resources_ok;
9500
9501 i = hw_breakpoint_used_count ();
9502 target_resources_ok =
9503 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9504 i + 1, 0);
9505 if (target_resources_ok == 0)
9506 error (_("No hardware breakpoint support in the target."));
9507 else if (target_resources_ok < 0)
9508 error (_("Hardware breakpoints used exceeds limit."));
9509 }
9510
9511 gdb_assert (sals.nelts > 0);
9512
9513 for (i = 0; i < sals.nelts; ++i)
9514 {
9515 struct symtab_and_line sal = sals.sals[i];
9516 struct bp_location *loc;
9517
9518 if (from_tty)
9519 {
9520 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9521 if (!loc_gdbarch)
9522 loc_gdbarch = gdbarch;
9523
9524 describe_other_breakpoints (loc_gdbarch,
9525 sal.pspace, sal.pc, sal.section, thread);
9526 }
9527
9528 if (i == 0)
9529 {
9530 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9531 b->thread = thread;
9532 b->task = task;
9533
9534 b->cond_string = cond_string;
9535 b->extra_string = extra_string;
9536 b->ignore_count = ignore_count;
9537 b->enable_state = enabled ? bp_enabled : bp_disabled;
9538 b->disposition = disposition;
9539
9540 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9541 b->loc->inserted = 1;
9542
9543 if (type == bp_static_tracepoint)
9544 {
9545 struct tracepoint *t = (struct tracepoint *) b;
9546 struct static_tracepoint_marker marker;
9547
9548 if (strace_marker_p (b))
9549 {
9550 /* We already know the marker exists, otherwise, we
9551 wouldn't see a sal for it. */
9552 char *p = &addr_string[3];
9553 char *endp;
9554 char *marker_str;
9555
9556 p = skip_spaces (p);
9557
9558 endp = skip_to_space (p);
9559
9560 marker_str = savestring (p, endp - p);
9561 t->static_trace_marker_id = marker_str;
9562
9563 printf_filtered (_("Probed static tracepoint "
9564 "marker \"%s\"\n"),
9565 t->static_trace_marker_id);
9566 }
9567 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9568 {
9569 t->static_trace_marker_id = xstrdup (marker.str_id);
9570 release_static_tracepoint_marker (&marker);
9571
9572 printf_filtered (_("Probed static tracepoint "
9573 "marker \"%s\"\n"),
9574 t->static_trace_marker_id);
9575 }
9576 else
9577 warning (_("Couldn't determine the static "
9578 "tracepoint marker to probe"));
9579 }
9580
9581 loc = b->loc;
9582 }
9583 else
9584 {
9585 loc = add_location_to_breakpoint (b, &sal);
9586 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9587 loc->inserted = 1;
9588 }
9589
9590 if (bp_loc_is_permanent (loc))
9591 make_breakpoint_permanent (b);
9592
9593 if (b->cond_string)
9594 {
9595 const char *arg = b->cond_string;
9596
9597 loc->cond = parse_exp_1 (&arg, loc->address,
9598 block_for_pc (loc->address), 0);
9599 if (*arg)
9600 error (_("Garbage '%s' follows condition"), arg);
9601 }
9602
9603 /* Dynamic printf requires and uses additional arguments on the
9604 command line, otherwise it's an error. */
9605 if (type == bp_dprintf)
9606 {
9607 if (b->extra_string)
9608 update_dprintf_command_list (b);
9609 else
9610 error (_("Format string required"));
9611 }
9612 else if (b->extra_string)
9613 error (_("Garbage '%s' at end of command"), b->extra_string);
9614 }
9615
9616 b->display_canonical = display_canonical;
9617 if (addr_string)
9618 b->addr_string = addr_string;
9619 else
9620 /* addr_string has to be used or breakpoint_re_set will delete
9621 me. */
9622 b->addr_string
9623 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9624 b->filter = filter;
9625 }
9626
9627 static void
9628 create_breakpoint_sal (struct gdbarch *gdbarch,
9629 struct symtabs_and_lines sals, char *addr_string,
9630 char *filter, char *cond_string,
9631 char *extra_string,
9632 enum bptype type, enum bpdisp disposition,
9633 int thread, int task, int ignore_count,
9634 const struct breakpoint_ops *ops, int from_tty,
9635 int enabled, int internal, unsigned flags,
9636 int display_canonical)
9637 {
9638 struct breakpoint *b;
9639 struct cleanup *old_chain;
9640
9641 if (is_tracepoint_type (type))
9642 {
9643 struct tracepoint *t;
9644
9645 t = XCNEW (struct tracepoint);
9646 b = &t->base;
9647 }
9648 else
9649 b = XNEW (struct breakpoint);
9650
9651 old_chain = make_cleanup (xfree, b);
9652
9653 init_breakpoint_sal (b, gdbarch,
9654 sals, addr_string,
9655 filter, cond_string, extra_string,
9656 type, disposition,
9657 thread, task, ignore_count,
9658 ops, from_tty,
9659 enabled, internal, flags,
9660 display_canonical);
9661 discard_cleanups (old_chain);
9662
9663 install_breakpoint (internal, b, 0);
9664 }
9665
9666 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9667 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9668 value. COND_STRING, if not NULL, specified the condition to be
9669 used for all breakpoints. Essentially the only case where
9670 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9671 function. In that case, it's still not possible to specify
9672 separate conditions for different overloaded functions, so
9673 we take just a single condition string.
9674
9675 NOTE: If the function succeeds, the caller is expected to cleanup
9676 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9677 array contents). If the function fails (error() is called), the
9678 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9679 COND and SALS arrays and each of those arrays contents. */
9680
9681 static void
9682 create_breakpoints_sal (struct gdbarch *gdbarch,
9683 struct linespec_result *canonical,
9684 char *cond_string, char *extra_string,
9685 enum bptype type, enum bpdisp disposition,
9686 int thread, int task, int ignore_count,
9687 const struct breakpoint_ops *ops, int from_tty,
9688 int enabled, int internal, unsigned flags)
9689 {
9690 int i;
9691 struct linespec_sals *lsal;
9692
9693 if (canonical->pre_expanded)
9694 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9695
9696 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9697 {
9698 /* Note that 'addr_string' can be NULL in the case of a plain
9699 'break', without arguments. */
9700 char *addr_string = (canonical->addr_string
9701 ? xstrdup (canonical->addr_string)
9702 : NULL);
9703 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9704 struct cleanup *inner = make_cleanup (xfree, addr_string);
9705
9706 make_cleanup (xfree, filter_string);
9707 create_breakpoint_sal (gdbarch, lsal->sals,
9708 addr_string,
9709 filter_string,
9710 cond_string, extra_string,
9711 type, disposition,
9712 thread, task, ignore_count, ops,
9713 from_tty, enabled, internal, flags,
9714 canonical->special_display);
9715 discard_cleanups (inner);
9716 }
9717 }
9718
9719 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9720 followed by conditionals. On return, SALS contains an array of SAL
9721 addresses found. ADDR_STRING contains a vector of (canonical)
9722 address strings. ADDRESS points to the end of the SAL.
9723
9724 The array and the line spec strings are allocated on the heap, it is
9725 the caller's responsibility to free them. */
9726
9727 static void
9728 parse_breakpoint_sals (char **address,
9729 struct linespec_result *canonical)
9730 {
9731 /* If no arg given, or if first arg is 'if ', use the default
9732 breakpoint. */
9733 if ((*address) == NULL
9734 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9735 {
9736 /* The last displayed codepoint, if it's valid, is our default breakpoint
9737 address. */
9738 if (last_displayed_sal_is_valid ())
9739 {
9740 struct linespec_sals lsal;
9741 struct symtab_and_line sal;
9742 CORE_ADDR pc;
9743
9744 init_sal (&sal); /* Initialize to zeroes. */
9745 lsal.sals.sals = (struct symtab_and_line *)
9746 xmalloc (sizeof (struct symtab_and_line));
9747
9748 /* Set sal's pspace, pc, symtab, and line to the values
9749 corresponding to the last call to print_frame_info.
9750 Be sure to reinitialize LINE with NOTCURRENT == 0
9751 as the breakpoint line number is inappropriate otherwise.
9752 find_pc_line would adjust PC, re-set it back. */
9753 get_last_displayed_sal (&sal);
9754 pc = sal.pc;
9755 sal = find_pc_line (pc, 0);
9756
9757 /* "break" without arguments is equivalent to "break *PC"
9758 where PC is the last displayed codepoint's address. So
9759 make sure to set sal.explicit_pc to prevent GDB from
9760 trying to expand the list of sals to include all other
9761 instances with the same symtab and line. */
9762 sal.pc = pc;
9763 sal.explicit_pc = 1;
9764
9765 lsal.sals.sals[0] = sal;
9766 lsal.sals.nelts = 1;
9767 lsal.canonical = NULL;
9768
9769 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9770 }
9771 else
9772 error (_("No default breakpoint address now."));
9773 }
9774 else
9775 {
9776 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9777
9778 /* Force almost all breakpoints to be in terms of the
9779 current_source_symtab (which is decode_line_1's default).
9780 This should produce the results we want almost all of the
9781 time while leaving default_breakpoint_* alone.
9782
9783 ObjC: However, don't match an Objective-C method name which
9784 may have a '+' or '-' succeeded by a '['. */
9785 if (last_displayed_sal_is_valid ()
9786 && (!cursal.symtab
9787 || ((strchr ("+-", (*address)[0]) != NULL)
9788 && ((*address)[1] != '['))))
9789 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9790 get_last_displayed_symtab (),
9791 get_last_displayed_line (),
9792 canonical, NULL, NULL);
9793 else
9794 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9795 cursal.symtab, cursal.line, canonical, NULL, NULL);
9796 }
9797 }
9798
9799
9800 /* Convert each SAL into a real PC. Verify that the PC can be
9801 inserted as a breakpoint. If it can't throw an error. */
9802
9803 static void
9804 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9805 {
9806 int i;
9807
9808 for (i = 0; i < sals->nelts; i++)
9809 resolve_sal_pc (&sals->sals[i]);
9810 }
9811
9812 /* Fast tracepoints may have restrictions on valid locations. For
9813 instance, a fast tracepoint using a jump instead of a trap will
9814 likely have to overwrite more bytes than a trap would, and so can
9815 only be placed where the instruction is longer than the jump, or a
9816 multi-instruction sequence does not have a jump into the middle of
9817 it, etc. */
9818
9819 static void
9820 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9821 struct symtabs_and_lines *sals)
9822 {
9823 int i, rslt;
9824 struct symtab_and_line *sal;
9825 char *msg;
9826 struct cleanup *old_chain;
9827
9828 for (i = 0; i < sals->nelts; i++)
9829 {
9830 struct gdbarch *sarch;
9831
9832 sal = &sals->sals[i];
9833
9834 sarch = get_sal_arch (*sal);
9835 /* We fall back to GDBARCH if there is no architecture
9836 associated with SAL. */
9837 if (sarch == NULL)
9838 sarch = gdbarch;
9839 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9840 NULL, &msg);
9841 old_chain = make_cleanup (xfree, msg);
9842
9843 if (!rslt)
9844 error (_("May not have a fast tracepoint at 0x%s%s"),
9845 paddress (sarch, sal->pc), (msg ? msg : ""));
9846
9847 do_cleanups (old_chain);
9848 }
9849 }
9850
9851 /* Issue an invalid thread ID error. */
9852
9853 static void ATTRIBUTE_NORETURN
9854 invalid_thread_id_error (int id)
9855 {
9856 error (_("Unknown thread %d."), id);
9857 }
9858
9859 /* Given TOK, a string specification of condition and thread, as
9860 accepted by the 'break' command, extract the condition
9861 string and thread number and set *COND_STRING and *THREAD.
9862 PC identifies the context at which the condition should be parsed.
9863 If no condition is found, *COND_STRING is set to NULL.
9864 If no thread is found, *THREAD is set to -1. */
9865
9866 static void
9867 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9868 char **cond_string, int *thread, int *task,
9869 char **rest)
9870 {
9871 *cond_string = NULL;
9872 *thread = -1;
9873 *task = 0;
9874 *rest = NULL;
9875
9876 while (tok && *tok)
9877 {
9878 const char *end_tok;
9879 int toklen;
9880 const char *cond_start = NULL;
9881 const char *cond_end = NULL;
9882
9883 tok = skip_spaces_const (tok);
9884
9885 if ((*tok == '"' || *tok == ',') && rest)
9886 {
9887 *rest = savestring (tok, strlen (tok));
9888 return;
9889 }
9890
9891 end_tok = skip_to_space_const (tok);
9892
9893 toklen = end_tok - tok;
9894
9895 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9896 {
9897 struct expression *expr;
9898
9899 tok = cond_start = end_tok + 1;
9900 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9901 xfree (expr);
9902 cond_end = tok;
9903 *cond_string = savestring (cond_start, cond_end - cond_start);
9904 }
9905 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9906 {
9907 char *tmptok;
9908
9909 tok = end_tok + 1;
9910 *thread = strtol (tok, &tmptok, 0);
9911 if (tok == tmptok)
9912 error (_("Junk after thread keyword."));
9913 if (!valid_thread_id (*thread))
9914 invalid_thread_id_error (*thread);
9915 tok = tmptok;
9916 }
9917 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9918 {
9919 char *tmptok;
9920
9921 tok = end_tok + 1;
9922 *task = strtol (tok, &tmptok, 0);
9923 if (tok == tmptok)
9924 error (_("Junk after task keyword."));
9925 if (!valid_task_id (*task))
9926 error (_("Unknown task %d."), *task);
9927 tok = tmptok;
9928 }
9929 else if (rest)
9930 {
9931 *rest = savestring (tok, strlen (tok));
9932 return;
9933 }
9934 else
9935 error (_("Junk at end of arguments."));
9936 }
9937 }
9938
9939 /* Decode a static tracepoint marker spec. */
9940
9941 static struct symtabs_and_lines
9942 decode_static_tracepoint_spec (char **arg_p)
9943 {
9944 VEC(static_tracepoint_marker_p) *markers = NULL;
9945 struct symtabs_and_lines sals;
9946 struct cleanup *old_chain;
9947 char *p = &(*arg_p)[3];
9948 char *endp;
9949 char *marker_str;
9950 int i;
9951
9952 p = skip_spaces (p);
9953
9954 endp = skip_to_space (p);
9955
9956 marker_str = savestring (p, endp - p);
9957 old_chain = make_cleanup (xfree, marker_str);
9958
9959 markers = target_static_tracepoint_markers_by_strid (marker_str);
9960 if (VEC_empty(static_tracepoint_marker_p, markers))
9961 error (_("No known static tracepoint marker named %s"), marker_str);
9962
9963 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9964 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9965
9966 for (i = 0; i < sals.nelts; i++)
9967 {
9968 struct static_tracepoint_marker *marker;
9969
9970 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9971
9972 init_sal (&sals.sals[i]);
9973
9974 sals.sals[i] = find_pc_line (marker->address, 0);
9975 sals.sals[i].pc = marker->address;
9976
9977 release_static_tracepoint_marker (marker);
9978 }
9979
9980 do_cleanups (old_chain);
9981
9982 *arg_p = endp;
9983 return sals;
9984 }
9985
9986 /* Set a breakpoint. This function is shared between CLI and MI
9987 functions for setting a breakpoint. This function has two major
9988 modes of operations, selected by the PARSE_ARG parameter. If
9989 non-zero, the function will parse ARG, extracting location,
9990 condition, thread and extra string. Otherwise, ARG is just the
9991 breakpoint's location, with condition, thread, and extra string
9992 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9993 If INTERNAL is non-zero, the breakpoint number will be allocated
9994 from the internal breakpoint count. Returns true if any breakpoint
9995 was created; false otherwise. */
9996
9997 int
9998 create_breakpoint (struct gdbarch *gdbarch,
9999 char *arg, char *cond_string,
10000 int thread, char *extra_string,
10001 int parse_arg,
10002 int tempflag, enum bptype type_wanted,
10003 int ignore_count,
10004 enum auto_boolean pending_break_support,
10005 const struct breakpoint_ops *ops,
10006 int from_tty, int enabled, int internal,
10007 unsigned flags)
10008 {
10009 volatile struct gdb_exception e;
10010 char *copy_arg = NULL;
10011 char *addr_start = arg;
10012 struct linespec_result canonical;
10013 struct cleanup *old_chain;
10014 struct cleanup *bkpt_chain = NULL;
10015 int pending = 0;
10016 int task = 0;
10017 int prev_bkpt_count = breakpoint_count;
10018
10019 gdb_assert (ops != NULL);
10020
10021 init_linespec_result (&canonical);
10022
10023 TRY_CATCH (e, RETURN_MASK_ALL)
10024 {
10025 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10026 addr_start, &copy_arg);
10027 }
10028
10029 /* If caller is interested in rc value from parse, set value. */
10030 switch (e.reason)
10031 {
10032 case GDB_NO_ERROR:
10033 if (VEC_empty (linespec_sals, canonical.sals))
10034 return 0;
10035 break;
10036 case RETURN_ERROR:
10037 switch (e.error)
10038 {
10039 case NOT_FOUND_ERROR:
10040
10041 /* If pending breakpoint support is turned off, throw
10042 error. */
10043
10044 if (pending_break_support == AUTO_BOOLEAN_FALSE)
10045 throw_exception (e);
10046
10047 exception_print (gdb_stderr, e);
10048
10049 /* If pending breakpoint support is auto query and the user
10050 selects no, then simply return the error code. */
10051 if (pending_break_support == AUTO_BOOLEAN_AUTO
10052 && !nquery (_("Make %s pending on future shared library load? "),
10053 bptype_string (type_wanted)))
10054 return 0;
10055
10056 /* At this point, either the user was queried about setting
10057 a pending breakpoint and selected yes, or pending
10058 breakpoint behavior is on and thus a pending breakpoint
10059 is defaulted on behalf of the user. */
10060 {
10061 struct linespec_sals lsal;
10062
10063 copy_arg = xstrdup (addr_start);
10064 lsal.canonical = xstrdup (copy_arg);
10065 lsal.sals.nelts = 1;
10066 lsal.sals.sals = XNEW (struct symtab_and_line);
10067 init_sal (&lsal.sals.sals[0]);
10068 pending = 1;
10069 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10070 }
10071 break;
10072 default:
10073 throw_exception (e);
10074 }
10075 break;
10076 default:
10077 throw_exception (e);
10078 }
10079
10080 /* Create a chain of things that always need to be cleaned up. */
10081 old_chain = make_cleanup_destroy_linespec_result (&canonical);
10082
10083 /* ----------------------------- SNIP -----------------------------
10084 Anything added to the cleanup chain beyond this point is assumed
10085 to be part of a breakpoint. If the breakpoint create succeeds
10086 then the memory is not reclaimed. */
10087 bkpt_chain = make_cleanup (null_cleanup, 0);
10088
10089 /* Resolve all line numbers to PC's and verify that the addresses
10090 are ok for the target. */
10091 if (!pending)
10092 {
10093 int ix;
10094 struct linespec_sals *iter;
10095
10096 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10097 breakpoint_sals_to_pc (&iter->sals);
10098 }
10099
10100 /* Fast tracepoints may have additional restrictions on location. */
10101 if (!pending && type_wanted == bp_fast_tracepoint)
10102 {
10103 int ix;
10104 struct linespec_sals *iter;
10105
10106 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10107 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10108 }
10109
10110 /* Verify that condition can be parsed, before setting any
10111 breakpoints. Allocate a separate condition expression for each
10112 breakpoint. */
10113 if (!pending)
10114 {
10115 if (parse_arg)
10116 {
10117 char *rest;
10118 struct linespec_sals *lsal;
10119
10120 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10121
10122 /* Here we only parse 'arg' to separate condition
10123 from thread number, so parsing in context of first
10124 sal is OK. When setting the breakpoint we'll
10125 re-parse it in context of each sal. */
10126
10127 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10128 &thread, &task, &rest);
10129 if (cond_string)
10130 make_cleanup (xfree, cond_string);
10131 if (rest)
10132 make_cleanup (xfree, rest);
10133 if (rest)
10134 extra_string = rest;
10135 }
10136 else
10137 {
10138 if (*arg != '\0')
10139 error (_("Garbage '%s' at end of location"), arg);
10140
10141 /* Create a private copy of condition string. */
10142 if (cond_string)
10143 {
10144 cond_string = xstrdup (cond_string);
10145 make_cleanup (xfree, cond_string);
10146 }
10147 /* Create a private copy of any extra string. */
10148 if (extra_string)
10149 {
10150 extra_string = xstrdup (extra_string);
10151 make_cleanup (xfree, extra_string);
10152 }
10153 }
10154
10155 ops->create_breakpoints_sal (gdbarch, &canonical,
10156 cond_string, extra_string, type_wanted,
10157 tempflag ? disp_del : disp_donttouch,
10158 thread, task, ignore_count, ops,
10159 from_tty, enabled, internal, flags);
10160 }
10161 else
10162 {
10163 struct breakpoint *b;
10164
10165 make_cleanup (xfree, copy_arg);
10166
10167 if (is_tracepoint_type (type_wanted))
10168 {
10169 struct tracepoint *t;
10170
10171 t = XCNEW (struct tracepoint);
10172 b = &t->base;
10173 }
10174 else
10175 b = XNEW (struct breakpoint);
10176
10177 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10178
10179 b->addr_string = copy_arg;
10180 if (parse_arg)
10181 b->cond_string = NULL;
10182 else
10183 {
10184 /* Create a private copy of condition string. */
10185 if (cond_string)
10186 {
10187 cond_string = xstrdup (cond_string);
10188 make_cleanup (xfree, cond_string);
10189 }
10190 b->cond_string = cond_string;
10191 }
10192 b->extra_string = NULL;
10193 b->ignore_count = ignore_count;
10194 b->disposition = tempflag ? disp_del : disp_donttouch;
10195 b->condition_not_parsed = 1;
10196 b->enable_state = enabled ? bp_enabled : bp_disabled;
10197 if ((type_wanted != bp_breakpoint
10198 && type_wanted != bp_hardware_breakpoint) || thread != -1)
10199 b->pspace = current_program_space;
10200
10201 install_breakpoint (internal, b, 0);
10202 }
10203
10204 if (VEC_length (linespec_sals, canonical.sals) > 1)
10205 {
10206 warning (_("Multiple breakpoints were set.\nUse the "
10207 "\"delete\" command to delete unwanted breakpoints."));
10208 prev_breakpoint_count = prev_bkpt_count;
10209 }
10210
10211 /* That's it. Discard the cleanups for data inserted into the
10212 breakpoint. */
10213 discard_cleanups (bkpt_chain);
10214 /* But cleanup everything else. */
10215 do_cleanups (old_chain);
10216
10217 /* error call may happen here - have BKPT_CHAIN already discarded. */
10218 update_global_location_list (UGLL_MAY_INSERT);
10219
10220 return 1;
10221 }
10222
10223 /* Set a breakpoint.
10224 ARG is a string describing breakpoint address,
10225 condition, and thread.
10226 FLAG specifies if a breakpoint is hardware on,
10227 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10228 and BP_TEMPFLAG. */
10229
10230 static void
10231 break_command_1 (char *arg, int flag, int from_tty)
10232 {
10233 int tempflag = flag & BP_TEMPFLAG;
10234 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10235 ? bp_hardware_breakpoint
10236 : bp_breakpoint);
10237 struct breakpoint_ops *ops;
10238 const char *arg_cp = arg;
10239
10240 /* Matching breakpoints on probes. */
10241 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10242 ops = &bkpt_probe_breakpoint_ops;
10243 else
10244 ops = &bkpt_breakpoint_ops;
10245
10246 create_breakpoint (get_current_arch (),
10247 arg,
10248 NULL, 0, NULL, 1 /* parse arg */,
10249 tempflag, type_wanted,
10250 0 /* Ignore count */,
10251 pending_break_support,
10252 ops,
10253 from_tty,
10254 1 /* enabled */,
10255 0 /* internal */,
10256 0);
10257 }
10258
10259 /* Helper function for break_command_1 and disassemble_command. */
10260
10261 void
10262 resolve_sal_pc (struct symtab_and_line *sal)
10263 {
10264 CORE_ADDR pc;
10265
10266 if (sal->pc == 0 && sal->symtab != NULL)
10267 {
10268 if (!find_line_pc (sal->symtab, sal->line, &pc))
10269 error (_("No line %d in file \"%s\"."),
10270 sal->line, symtab_to_filename_for_display (sal->symtab));
10271 sal->pc = pc;
10272
10273 /* If this SAL corresponds to a breakpoint inserted using a line
10274 number, then skip the function prologue if necessary. */
10275 if (sal->explicit_line)
10276 skip_prologue_sal (sal);
10277 }
10278
10279 if (sal->section == 0 && sal->symtab != NULL)
10280 {
10281 const struct blockvector *bv;
10282 const struct block *b;
10283 struct symbol *sym;
10284
10285 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
10286 if (bv != NULL)
10287 {
10288 sym = block_linkage_function (b);
10289 if (sym != NULL)
10290 {
10291 fixup_symbol_section (sym, sal->symtab->objfile);
10292 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
10293 }
10294 else
10295 {
10296 /* It really is worthwhile to have the section, so we'll
10297 just have to look harder. This case can be executed
10298 if we have line numbers but no functions (as can
10299 happen in assembly source). */
10300
10301 struct bound_minimal_symbol msym;
10302 struct cleanup *old_chain = save_current_space_and_thread ();
10303
10304 switch_to_program_space_and_thread (sal->pspace);
10305
10306 msym = lookup_minimal_symbol_by_pc (sal->pc);
10307 if (msym.minsym)
10308 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10309
10310 do_cleanups (old_chain);
10311 }
10312 }
10313 }
10314 }
10315
10316 void
10317 break_command (char *arg, int from_tty)
10318 {
10319 break_command_1 (arg, 0, from_tty);
10320 }
10321
10322 void
10323 tbreak_command (char *arg, int from_tty)
10324 {
10325 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10326 }
10327
10328 static void
10329 hbreak_command (char *arg, int from_tty)
10330 {
10331 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10332 }
10333
10334 static void
10335 thbreak_command (char *arg, int from_tty)
10336 {
10337 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10338 }
10339
10340 static void
10341 stop_command (char *arg, int from_tty)
10342 {
10343 printf_filtered (_("Specify the type of breakpoint to set.\n\
10344 Usage: stop in <function | address>\n\
10345 stop at <line>\n"));
10346 }
10347
10348 static void
10349 stopin_command (char *arg, int from_tty)
10350 {
10351 int badInput = 0;
10352
10353 if (arg == (char *) NULL)
10354 badInput = 1;
10355 else if (*arg != '*')
10356 {
10357 char *argptr = arg;
10358 int hasColon = 0;
10359
10360 /* Look for a ':'. If this is a line number specification, then
10361 say it is bad, otherwise, it should be an address or
10362 function/method name. */
10363 while (*argptr && !hasColon)
10364 {
10365 hasColon = (*argptr == ':');
10366 argptr++;
10367 }
10368
10369 if (hasColon)
10370 badInput = (*argptr != ':'); /* Not a class::method */
10371 else
10372 badInput = isdigit (*arg); /* a simple line number */
10373 }
10374
10375 if (badInput)
10376 printf_filtered (_("Usage: stop in <function | address>\n"));
10377 else
10378 break_command_1 (arg, 0, from_tty);
10379 }
10380
10381 static void
10382 stopat_command (char *arg, int from_tty)
10383 {
10384 int badInput = 0;
10385
10386 if (arg == (char *) NULL || *arg == '*') /* no line number */
10387 badInput = 1;
10388 else
10389 {
10390 char *argptr = arg;
10391 int hasColon = 0;
10392
10393 /* Look for a ':'. If there is a '::' then get out, otherwise
10394 it is probably a line number. */
10395 while (*argptr && !hasColon)
10396 {
10397 hasColon = (*argptr == ':');
10398 argptr++;
10399 }
10400
10401 if (hasColon)
10402 badInput = (*argptr == ':'); /* we have class::method */
10403 else
10404 badInput = !isdigit (*arg); /* not a line number */
10405 }
10406
10407 if (badInput)
10408 printf_filtered (_("Usage: stop at <line>\n"));
10409 else
10410 break_command_1 (arg, 0, from_tty);
10411 }
10412
10413 /* The dynamic printf command is mostly like a regular breakpoint, but
10414 with a prewired command list consisting of a single output command,
10415 built from extra arguments supplied on the dprintf command
10416 line. */
10417
10418 static void
10419 dprintf_command (char *arg, int from_tty)
10420 {
10421 create_breakpoint (get_current_arch (),
10422 arg,
10423 NULL, 0, NULL, 1 /* parse arg */,
10424 0, bp_dprintf,
10425 0 /* Ignore count */,
10426 pending_break_support,
10427 &dprintf_breakpoint_ops,
10428 from_tty,
10429 1 /* enabled */,
10430 0 /* internal */,
10431 0);
10432 }
10433
10434 static void
10435 agent_printf_command (char *arg, int from_tty)
10436 {
10437 error (_("May only run agent-printf on the target"));
10438 }
10439
10440 /* Implement the "breakpoint_hit" breakpoint_ops method for
10441 ranged breakpoints. */
10442
10443 static int
10444 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10445 struct address_space *aspace,
10446 CORE_ADDR bp_addr,
10447 const struct target_waitstatus *ws)
10448 {
10449 if (ws->kind != TARGET_WAITKIND_STOPPED
10450 || ws->value.sig != GDB_SIGNAL_TRAP)
10451 return 0;
10452
10453 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10454 bl->length, aspace, bp_addr);
10455 }
10456
10457 /* Implement the "resources_needed" breakpoint_ops method for
10458 ranged breakpoints. */
10459
10460 static int
10461 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10462 {
10463 return target_ranged_break_num_registers ();
10464 }
10465
10466 /* Implement the "print_it" breakpoint_ops method for
10467 ranged breakpoints. */
10468
10469 static enum print_stop_action
10470 print_it_ranged_breakpoint (bpstat bs)
10471 {
10472 struct breakpoint *b = bs->breakpoint_at;
10473 struct bp_location *bl = b->loc;
10474 struct ui_out *uiout = current_uiout;
10475
10476 gdb_assert (b->type == bp_hardware_breakpoint);
10477
10478 /* Ranged breakpoints have only one location. */
10479 gdb_assert (bl && bl->next == NULL);
10480
10481 annotate_breakpoint (b->number);
10482 if (b->disposition == disp_del)
10483 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10484 else
10485 ui_out_text (uiout, "\nRanged breakpoint ");
10486 if (ui_out_is_mi_like_p (uiout))
10487 {
10488 ui_out_field_string (uiout, "reason",
10489 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10490 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10491 }
10492 ui_out_field_int (uiout, "bkptno", b->number);
10493 ui_out_text (uiout, ", ");
10494
10495 return PRINT_SRC_AND_LOC;
10496 }
10497
10498 /* Implement the "print_one" breakpoint_ops method for
10499 ranged breakpoints. */
10500
10501 static void
10502 print_one_ranged_breakpoint (struct breakpoint *b,
10503 struct bp_location **last_loc)
10504 {
10505 struct bp_location *bl = b->loc;
10506 struct value_print_options opts;
10507 struct ui_out *uiout = current_uiout;
10508
10509 /* Ranged breakpoints have only one location. */
10510 gdb_assert (bl && bl->next == NULL);
10511
10512 get_user_print_options (&opts);
10513
10514 if (opts.addressprint)
10515 /* We don't print the address range here, it will be printed later
10516 by print_one_detail_ranged_breakpoint. */
10517 ui_out_field_skip (uiout, "addr");
10518 annotate_field (5);
10519 print_breakpoint_location (b, bl);
10520 *last_loc = bl;
10521 }
10522
10523 /* Implement the "print_one_detail" breakpoint_ops method for
10524 ranged breakpoints. */
10525
10526 static void
10527 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10528 struct ui_out *uiout)
10529 {
10530 CORE_ADDR address_start, address_end;
10531 struct bp_location *bl = b->loc;
10532 struct ui_file *stb = mem_fileopen ();
10533 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10534
10535 gdb_assert (bl);
10536
10537 address_start = bl->address;
10538 address_end = address_start + bl->length - 1;
10539
10540 ui_out_text (uiout, "\taddress range: ");
10541 fprintf_unfiltered (stb, "[%s, %s]",
10542 print_core_address (bl->gdbarch, address_start),
10543 print_core_address (bl->gdbarch, address_end));
10544 ui_out_field_stream (uiout, "addr", stb);
10545 ui_out_text (uiout, "\n");
10546
10547 do_cleanups (cleanup);
10548 }
10549
10550 /* Implement the "print_mention" breakpoint_ops method for
10551 ranged breakpoints. */
10552
10553 static void
10554 print_mention_ranged_breakpoint (struct breakpoint *b)
10555 {
10556 struct bp_location *bl = b->loc;
10557 struct ui_out *uiout = current_uiout;
10558
10559 gdb_assert (bl);
10560 gdb_assert (b->type == bp_hardware_breakpoint);
10561
10562 if (ui_out_is_mi_like_p (uiout))
10563 return;
10564
10565 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10566 b->number, paddress (bl->gdbarch, bl->address),
10567 paddress (bl->gdbarch, bl->address + bl->length - 1));
10568 }
10569
10570 /* Implement the "print_recreate" breakpoint_ops method for
10571 ranged breakpoints. */
10572
10573 static void
10574 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10575 {
10576 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10577 b->addr_string_range_end);
10578 print_recreate_thread (b, fp);
10579 }
10580
10581 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10582
10583 static struct breakpoint_ops ranged_breakpoint_ops;
10584
10585 /* Find the address where the end of the breakpoint range should be
10586 placed, given the SAL of the end of the range. This is so that if
10587 the user provides a line number, the end of the range is set to the
10588 last instruction of the given line. */
10589
10590 static CORE_ADDR
10591 find_breakpoint_range_end (struct symtab_and_line sal)
10592 {
10593 CORE_ADDR end;
10594
10595 /* If the user provided a PC value, use it. Otherwise,
10596 find the address of the end of the given location. */
10597 if (sal.explicit_pc)
10598 end = sal.pc;
10599 else
10600 {
10601 int ret;
10602 CORE_ADDR start;
10603
10604 ret = find_line_pc_range (sal, &start, &end);
10605 if (!ret)
10606 error (_("Could not find location of the end of the range."));
10607
10608 /* find_line_pc_range returns the start of the next line. */
10609 end--;
10610 }
10611
10612 return end;
10613 }
10614
10615 /* Implement the "break-range" CLI command. */
10616
10617 static void
10618 break_range_command (char *arg, int from_tty)
10619 {
10620 char *arg_start, *addr_string_start, *addr_string_end;
10621 struct linespec_result canonical_start, canonical_end;
10622 int bp_count, can_use_bp, length;
10623 CORE_ADDR end;
10624 struct breakpoint *b;
10625 struct symtab_and_line sal_start, sal_end;
10626 struct cleanup *cleanup_bkpt;
10627 struct linespec_sals *lsal_start, *lsal_end;
10628
10629 /* We don't support software ranged breakpoints. */
10630 if (target_ranged_break_num_registers () < 0)
10631 error (_("This target does not support hardware ranged breakpoints."));
10632
10633 bp_count = hw_breakpoint_used_count ();
10634 bp_count += target_ranged_break_num_registers ();
10635 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10636 bp_count, 0);
10637 if (can_use_bp < 0)
10638 error (_("Hardware breakpoints used exceeds limit."));
10639
10640 arg = skip_spaces (arg);
10641 if (arg == NULL || arg[0] == '\0')
10642 error(_("No address range specified."));
10643
10644 init_linespec_result (&canonical_start);
10645
10646 arg_start = arg;
10647 parse_breakpoint_sals (&arg, &canonical_start);
10648
10649 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10650
10651 if (arg[0] != ',')
10652 error (_("Too few arguments."));
10653 else if (VEC_empty (linespec_sals, canonical_start.sals))
10654 error (_("Could not find location of the beginning of the range."));
10655
10656 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10657
10658 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10659 || lsal_start->sals.nelts != 1)
10660 error (_("Cannot create a ranged breakpoint with multiple locations."));
10661
10662 sal_start = lsal_start->sals.sals[0];
10663 addr_string_start = savestring (arg_start, arg - arg_start);
10664 make_cleanup (xfree, addr_string_start);
10665
10666 arg++; /* Skip the comma. */
10667 arg = skip_spaces (arg);
10668
10669 /* Parse the end location. */
10670
10671 init_linespec_result (&canonical_end);
10672 arg_start = arg;
10673
10674 /* We call decode_line_full directly here instead of using
10675 parse_breakpoint_sals because we need to specify the start location's
10676 symtab and line as the default symtab and line for the end of the
10677 range. This makes it possible to have ranges like "foo.c:27, +14",
10678 where +14 means 14 lines from the start location. */
10679 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10680 sal_start.symtab, sal_start.line,
10681 &canonical_end, NULL, NULL);
10682
10683 make_cleanup_destroy_linespec_result (&canonical_end);
10684
10685 if (VEC_empty (linespec_sals, canonical_end.sals))
10686 error (_("Could not find location of the end of the range."));
10687
10688 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10689 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10690 || lsal_end->sals.nelts != 1)
10691 error (_("Cannot create a ranged breakpoint with multiple locations."));
10692
10693 sal_end = lsal_end->sals.sals[0];
10694 addr_string_end = savestring (arg_start, arg - arg_start);
10695 make_cleanup (xfree, addr_string_end);
10696
10697 end = find_breakpoint_range_end (sal_end);
10698 if (sal_start.pc > end)
10699 error (_("Invalid address range, end precedes start."));
10700
10701 length = end - sal_start.pc + 1;
10702 if (length < 0)
10703 /* Length overflowed. */
10704 error (_("Address range too large."));
10705 else if (length == 1)
10706 {
10707 /* This range is simple enough to be handled by
10708 the `hbreak' command. */
10709 hbreak_command (addr_string_start, 1);
10710
10711 do_cleanups (cleanup_bkpt);
10712
10713 return;
10714 }
10715
10716 /* Now set up the breakpoint. */
10717 b = set_raw_breakpoint (get_current_arch (), sal_start,
10718 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10719 set_breakpoint_count (breakpoint_count + 1);
10720 b->number = breakpoint_count;
10721 b->disposition = disp_donttouch;
10722 b->addr_string = xstrdup (addr_string_start);
10723 b->addr_string_range_end = xstrdup (addr_string_end);
10724 b->loc->length = length;
10725
10726 do_cleanups (cleanup_bkpt);
10727
10728 mention (b);
10729 observer_notify_breakpoint_created (b);
10730 update_global_location_list (UGLL_MAY_INSERT);
10731 }
10732
10733 /* Return non-zero if EXP is verified as constant. Returned zero
10734 means EXP is variable. Also the constant detection may fail for
10735 some constant expressions and in such case still falsely return
10736 zero. */
10737
10738 static int
10739 watchpoint_exp_is_const (const struct expression *exp)
10740 {
10741 int i = exp->nelts;
10742
10743 while (i > 0)
10744 {
10745 int oplenp, argsp;
10746
10747 /* We are only interested in the descriptor of each element. */
10748 operator_length (exp, i, &oplenp, &argsp);
10749 i -= oplenp;
10750
10751 switch (exp->elts[i].opcode)
10752 {
10753 case BINOP_ADD:
10754 case BINOP_SUB:
10755 case BINOP_MUL:
10756 case BINOP_DIV:
10757 case BINOP_REM:
10758 case BINOP_MOD:
10759 case BINOP_LSH:
10760 case BINOP_RSH:
10761 case BINOP_LOGICAL_AND:
10762 case BINOP_LOGICAL_OR:
10763 case BINOP_BITWISE_AND:
10764 case BINOP_BITWISE_IOR:
10765 case BINOP_BITWISE_XOR:
10766 case BINOP_EQUAL:
10767 case BINOP_NOTEQUAL:
10768 case BINOP_LESS:
10769 case BINOP_GTR:
10770 case BINOP_LEQ:
10771 case BINOP_GEQ:
10772 case BINOP_REPEAT:
10773 case BINOP_COMMA:
10774 case BINOP_EXP:
10775 case BINOP_MIN:
10776 case BINOP_MAX:
10777 case BINOP_INTDIV:
10778 case BINOP_CONCAT:
10779 case TERNOP_COND:
10780 case TERNOP_SLICE:
10781
10782 case OP_LONG:
10783 case OP_DOUBLE:
10784 case OP_DECFLOAT:
10785 case OP_LAST:
10786 case OP_COMPLEX:
10787 case OP_STRING:
10788 case OP_ARRAY:
10789 case OP_TYPE:
10790 case OP_TYPEOF:
10791 case OP_DECLTYPE:
10792 case OP_TYPEID:
10793 case OP_NAME:
10794 case OP_OBJC_NSSTRING:
10795
10796 case UNOP_NEG:
10797 case UNOP_LOGICAL_NOT:
10798 case UNOP_COMPLEMENT:
10799 case UNOP_ADDR:
10800 case UNOP_HIGH:
10801 case UNOP_CAST:
10802
10803 case UNOP_CAST_TYPE:
10804 case UNOP_REINTERPRET_CAST:
10805 case UNOP_DYNAMIC_CAST:
10806 /* Unary, binary and ternary operators: We have to check
10807 their operands. If they are constant, then so is the
10808 result of that operation. For instance, if A and B are
10809 determined to be constants, then so is "A + B".
10810
10811 UNOP_IND is one exception to the rule above, because the
10812 value of *ADDR is not necessarily a constant, even when
10813 ADDR is. */
10814 break;
10815
10816 case OP_VAR_VALUE:
10817 /* Check whether the associated symbol is a constant.
10818
10819 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10820 possible that a buggy compiler could mark a variable as
10821 constant even when it is not, and TYPE_CONST would return
10822 true in this case, while SYMBOL_CLASS wouldn't.
10823
10824 We also have to check for function symbols because they
10825 are always constant. */
10826 {
10827 struct symbol *s = exp->elts[i + 2].symbol;
10828
10829 if (SYMBOL_CLASS (s) != LOC_BLOCK
10830 && SYMBOL_CLASS (s) != LOC_CONST
10831 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10832 return 0;
10833 break;
10834 }
10835
10836 /* The default action is to return 0 because we are using
10837 the optimistic approach here: If we don't know something,
10838 then it is not a constant. */
10839 default:
10840 return 0;
10841 }
10842 }
10843
10844 return 1;
10845 }
10846
10847 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10848
10849 static void
10850 dtor_watchpoint (struct breakpoint *self)
10851 {
10852 struct watchpoint *w = (struct watchpoint *) self;
10853
10854 xfree (w->cond_exp);
10855 xfree (w->exp);
10856 xfree (w->exp_string);
10857 xfree (w->exp_string_reparse);
10858 value_free (w->val);
10859
10860 base_breakpoint_ops.dtor (self);
10861 }
10862
10863 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10864
10865 static void
10866 re_set_watchpoint (struct breakpoint *b)
10867 {
10868 struct watchpoint *w = (struct watchpoint *) b;
10869
10870 /* Watchpoint can be either on expression using entirely global
10871 variables, or it can be on local variables.
10872
10873 Watchpoints of the first kind are never auto-deleted, and even
10874 persist across program restarts. Since they can use variables
10875 from shared libraries, we need to reparse expression as libraries
10876 are loaded and unloaded.
10877
10878 Watchpoints on local variables can also change meaning as result
10879 of solib event. For example, if a watchpoint uses both a local
10880 and a global variables in expression, it's a local watchpoint,
10881 but unloading of a shared library will make the expression
10882 invalid. This is not a very common use case, but we still
10883 re-evaluate expression, to avoid surprises to the user.
10884
10885 Note that for local watchpoints, we re-evaluate it only if
10886 watchpoints frame id is still valid. If it's not, it means the
10887 watchpoint is out of scope and will be deleted soon. In fact,
10888 I'm not sure we'll ever be called in this case.
10889
10890 If a local watchpoint's frame id is still valid, then
10891 w->exp_valid_block is likewise valid, and we can safely use it.
10892
10893 Don't do anything about disabled watchpoints, since they will be
10894 reevaluated again when enabled. */
10895 update_watchpoint (w, 1 /* reparse */);
10896 }
10897
10898 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10899
10900 static int
10901 insert_watchpoint (struct bp_location *bl)
10902 {
10903 struct watchpoint *w = (struct watchpoint *) bl->owner;
10904 int length = w->exact ? 1 : bl->length;
10905
10906 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10907 w->cond_exp);
10908 }
10909
10910 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10911
10912 static int
10913 remove_watchpoint (struct bp_location *bl)
10914 {
10915 struct watchpoint *w = (struct watchpoint *) bl->owner;
10916 int length = w->exact ? 1 : bl->length;
10917
10918 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10919 w->cond_exp);
10920 }
10921
10922 static int
10923 breakpoint_hit_watchpoint (const struct bp_location *bl,
10924 struct address_space *aspace, CORE_ADDR bp_addr,
10925 const struct target_waitstatus *ws)
10926 {
10927 struct breakpoint *b = bl->owner;
10928 struct watchpoint *w = (struct watchpoint *) b;
10929
10930 /* Continuable hardware watchpoints are treated as non-existent if the
10931 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10932 some data address). Otherwise gdb won't stop on a break instruction
10933 in the code (not from a breakpoint) when a hardware watchpoint has
10934 been defined. Also skip watchpoints which we know did not trigger
10935 (did not match the data address). */
10936 if (is_hardware_watchpoint (b)
10937 && w->watchpoint_triggered == watch_triggered_no)
10938 return 0;
10939
10940 return 1;
10941 }
10942
10943 static void
10944 check_status_watchpoint (bpstat bs)
10945 {
10946 gdb_assert (is_watchpoint (bs->breakpoint_at));
10947
10948 bpstat_check_watchpoint (bs);
10949 }
10950
10951 /* Implement the "resources_needed" breakpoint_ops method for
10952 hardware watchpoints. */
10953
10954 static int
10955 resources_needed_watchpoint (const struct bp_location *bl)
10956 {
10957 struct watchpoint *w = (struct watchpoint *) bl->owner;
10958 int length = w->exact? 1 : bl->length;
10959
10960 return target_region_ok_for_hw_watchpoint (bl->address, length);
10961 }
10962
10963 /* Implement the "works_in_software_mode" breakpoint_ops method for
10964 hardware watchpoints. */
10965
10966 static int
10967 works_in_software_mode_watchpoint (const struct breakpoint *b)
10968 {
10969 /* Read and access watchpoints only work with hardware support. */
10970 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10971 }
10972
10973 static enum print_stop_action
10974 print_it_watchpoint (bpstat bs)
10975 {
10976 struct cleanup *old_chain;
10977 struct breakpoint *b;
10978 struct ui_file *stb;
10979 enum print_stop_action result;
10980 struct watchpoint *w;
10981 struct ui_out *uiout = current_uiout;
10982
10983 gdb_assert (bs->bp_location_at != NULL);
10984
10985 b = bs->breakpoint_at;
10986 w = (struct watchpoint *) b;
10987
10988 stb = mem_fileopen ();
10989 old_chain = make_cleanup_ui_file_delete (stb);
10990
10991 switch (b->type)
10992 {
10993 case bp_watchpoint:
10994 case bp_hardware_watchpoint:
10995 annotate_watchpoint (b->number);
10996 if (ui_out_is_mi_like_p (uiout))
10997 ui_out_field_string
10998 (uiout, "reason",
10999 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11000 mention (b);
11001 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11002 ui_out_text (uiout, "\nOld value = ");
11003 watchpoint_value_print (bs->old_val, stb);
11004 ui_out_field_stream (uiout, "old", stb);
11005 ui_out_text (uiout, "\nNew value = ");
11006 watchpoint_value_print (w->val, stb);
11007 ui_out_field_stream (uiout, "new", stb);
11008 ui_out_text (uiout, "\n");
11009 /* More than one watchpoint may have been triggered. */
11010 result = PRINT_UNKNOWN;
11011 break;
11012
11013 case bp_read_watchpoint:
11014 if (ui_out_is_mi_like_p (uiout))
11015 ui_out_field_string
11016 (uiout, "reason",
11017 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11018 mention (b);
11019 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11020 ui_out_text (uiout, "\nValue = ");
11021 watchpoint_value_print (w->val, stb);
11022 ui_out_field_stream (uiout, "value", stb);
11023 ui_out_text (uiout, "\n");
11024 result = PRINT_UNKNOWN;
11025 break;
11026
11027 case bp_access_watchpoint:
11028 if (bs->old_val != NULL)
11029 {
11030 annotate_watchpoint (b->number);
11031 if (ui_out_is_mi_like_p (uiout))
11032 ui_out_field_string
11033 (uiout, "reason",
11034 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11035 mention (b);
11036 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11037 ui_out_text (uiout, "\nOld value = ");
11038 watchpoint_value_print (bs->old_val, stb);
11039 ui_out_field_stream (uiout, "old", stb);
11040 ui_out_text (uiout, "\nNew value = ");
11041 }
11042 else
11043 {
11044 mention (b);
11045 if (ui_out_is_mi_like_p (uiout))
11046 ui_out_field_string
11047 (uiout, "reason",
11048 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11049 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11050 ui_out_text (uiout, "\nValue = ");
11051 }
11052 watchpoint_value_print (w->val, stb);
11053 ui_out_field_stream (uiout, "new", stb);
11054 ui_out_text (uiout, "\n");
11055 result = PRINT_UNKNOWN;
11056 break;
11057 default:
11058 result = PRINT_UNKNOWN;
11059 }
11060
11061 do_cleanups (old_chain);
11062 return result;
11063 }
11064
11065 /* Implement the "print_mention" breakpoint_ops method for hardware
11066 watchpoints. */
11067
11068 static void
11069 print_mention_watchpoint (struct breakpoint *b)
11070 {
11071 struct cleanup *ui_out_chain;
11072 struct watchpoint *w = (struct watchpoint *) b;
11073 struct ui_out *uiout = current_uiout;
11074
11075 switch (b->type)
11076 {
11077 case bp_watchpoint:
11078 ui_out_text (uiout, "Watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11080 break;
11081 case bp_hardware_watchpoint:
11082 ui_out_text (uiout, "Hardware watchpoint ");
11083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11084 break;
11085 case bp_read_watchpoint:
11086 ui_out_text (uiout, "Hardware read watchpoint ");
11087 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11088 break;
11089 case bp_access_watchpoint:
11090 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11091 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11092 break;
11093 default:
11094 internal_error (__FILE__, __LINE__,
11095 _("Invalid hardware watchpoint type."));
11096 }
11097
11098 ui_out_field_int (uiout, "number", b->number);
11099 ui_out_text (uiout, ": ");
11100 ui_out_field_string (uiout, "exp", w->exp_string);
11101 do_cleanups (ui_out_chain);
11102 }
11103
11104 /* Implement the "print_recreate" breakpoint_ops method for
11105 watchpoints. */
11106
11107 static void
11108 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11109 {
11110 struct watchpoint *w = (struct watchpoint *) b;
11111
11112 switch (b->type)
11113 {
11114 case bp_watchpoint:
11115 case bp_hardware_watchpoint:
11116 fprintf_unfiltered (fp, "watch");
11117 break;
11118 case bp_read_watchpoint:
11119 fprintf_unfiltered (fp, "rwatch");
11120 break;
11121 case bp_access_watchpoint:
11122 fprintf_unfiltered (fp, "awatch");
11123 break;
11124 default:
11125 internal_error (__FILE__, __LINE__,
11126 _("Invalid watchpoint type."));
11127 }
11128
11129 fprintf_unfiltered (fp, " %s", w->exp_string);
11130 print_recreate_thread (b, fp);
11131 }
11132
11133 /* Implement the "explains_signal" breakpoint_ops method for
11134 watchpoints. */
11135
11136 static int
11137 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11138 {
11139 /* A software watchpoint cannot cause a signal other than
11140 GDB_SIGNAL_TRAP. */
11141 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
11142 return 0;
11143
11144 return 1;
11145 }
11146
11147 /* The breakpoint_ops structure to be used in hardware watchpoints. */
11148
11149 static struct breakpoint_ops watchpoint_breakpoint_ops;
11150
11151 /* Implement the "insert" breakpoint_ops method for
11152 masked hardware watchpoints. */
11153
11154 static int
11155 insert_masked_watchpoint (struct bp_location *bl)
11156 {
11157 struct watchpoint *w = (struct watchpoint *) bl->owner;
11158
11159 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
11160 bl->watchpoint_type);
11161 }
11162
11163 /* Implement the "remove" breakpoint_ops method for
11164 masked hardware watchpoints. */
11165
11166 static int
11167 remove_masked_watchpoint (struct bp_location *bl)
11168 {
11169 struct watchpoint *w = (struct watchpoint *) bl->owner;
11170
11171 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
11172 bl->watchpoint_type);
11173 }
11174
11175 /* Implement the "resources_needed" breakpoint_ops method for
11176 masked hardware watchpoints. */
11177
11178 static int
11179 resources_needed_masked_watchpoint (const struct bp_location *bl)
11180 {
11181 struct watchpoint *w = (struct watchpoint *) bl->owner;
11182
11183 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
11184 }
11185
11186 /* Implement the "works_in_software_mode" breakpoint_ops method for
11187 masked hardware watchpoints. */
11188
11189 static int
11190 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11191 {
11192 return 0;
11193 }
11194
11195 /* Implement the "print_it" breakpoint_ops method for
11196 masked hardware watchpoints. */
11197
11198 static enum print_stop_action
11199 print_it_masked_watchpoint (bpstat bs)
11200 {
11201 struct breakpoint *b = bs->breakpoint_at;
11202 struct ui_out *uiout = current_uiout;
11203
11204 /* Masked watchpoints have only one location. */
11205 gdb_assert (b->loc && b->loc->next == NULL);
11206
11207 switch (b->type)
11208 {
11209 case bp_hardware_watchpoint:
11210 annotate_watchpoint (b->number);
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_read_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11222 break;
11223
11224 case bp_access_watchpoint:
11225 if (ui_out_is_mi_like_p (uiout))
11226 ui_out_field_string
11227 (uiout, "reason",
11228 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11229 break;
11230 default:
11231 internal_error (__FILE__, __LINE__,
11232 _("Invalid hardware watchpoint type."));
11233 }
11234
11235 mention (b);
11236 ui_out_text (uiout, _("\n\
11237 Check the underlying instruction at PC for the memory\n\
11238 address and value which triggered this watchpoint.\n"));
11239 ui_out_text (uiout, "\n");
11240
11241 /* More than one watchpoint may have been triggered. */
11242 return PRINT_UNKNOWN;
11243 }
11244
11245 /* Implement the "print_one_detail" breakpoint_ops method for
11246 masked hardware watchpoints. */
11247
11248 static void
11249 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11250 struct ui_out *uiout)
11251 {
11252 struct watchpoint *w = (struct watchpoint *) b;
11253
11254 /* Masked watchpoints have only one location. */
11255 gdb_assert (b->loc && b->loc->next == NULL);
11256
11257 ui_out_text (uiout, "\tmask ");
11258 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11259 ui_out_text (uiout, "\n");
11260 }
11261
11262 /* Implement the "print_mention" breakpoint_ops method for
11263 masked hardware watchpoints. */
11264
11265 static void
11266 print_mention_masked_watchpoint (struct breakpoint *b)
11267 {
11268 struct watchpoint *w = (struct watchpoint *) b;
11269 struct ui_out *uiout = current_uiout;
11270 struct cleanup *ui_out_chain;
11271
11272 switch (b->type)
11273 {
11274 case bp_hardware_watchpoint:
11275 ui_out_text (uiout, "Masked hardware watchpoint ");
11276 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11277 break;
11278 case bp_read_watchpoint:
11279 ui_out_text (uiout, "Masked hardware read watchpoint ");
11280 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11281 break;
11282 case bp_access_watchpoint:
11283 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11284 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11285 break;
11286 default:
11287 internal_error (__FILE__, __LINE__,
11288 _("Invalid hardware watchpoint type."));
11289 }
11290
11291 ui_out_field_int (uiout, "number", b->number);
11292 ui_out_text (uiout, ": ");
11293 ui_out_field_string (uiout, "exp", w->exp_string);
11294 do_cleanups (ui_out_chain);
11295 }
11296
11297 /* Implement the "print_recreate" breakpoint_ops method for
11298 masked hardware watchpoints. */
11299
11300 static void
11301 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11302 {
11303 struct watchpoint *w = (struct watchpoint *) b;
11304 char tmp[40];
11305
11306 switch (b->type)
11307 {
11308 case bp_hardware_watchpoint:
11309 fprintf_unfiltered (fp, "watch");
11310 break;
11311 case bp_read_watchpoint:
11312 fprintf_unfiltered (fp, "rwatch");
11313 break;
11314 case bp_access_watchpoint:
11315 fprintf_unfiltered (fp, "awatch");
11316 break;
11317 default:
11318 internal_error (__FILE__, __LINE__,
11319 _("Invalid hardware watchpoint type."));
11320 }
11321
11322 sprintf_vma (tmp, w->hw_wp_mask);
11323 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11324 print_recreate_thread (b, fp);
11325 }
11326
11327 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11328
11329 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11330
11331 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11332
11333 static int
11334 is_masked_watchpoint (const struct breakpoint *b)
11335 {
11336 return b->ops == &masked_watchpoint_breakpoint_ops;
11337 }
11338
11339 /* accessflag: hw_write: watch write,
11340 hw_read: watch read,
11341 hw_access: watch access (read or write) */
11342 static void
11343 watch_command_1 (const char *arg, int accessflag, int from_tty,
11344 int just_location, int internal)
11345 {
11346 volatile struct gdb_exception e;
11347 struct breakpoint *b, *scope_breakpoint = NULL;
11348 struct expression *exp;
11349 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11350 struct value *val, *mark, *result;
11351 int saved_bitpos = 0, saved_bitsize = 0;
11352 struct frame_info *frame;
11353 const char *exp_start = NULL;
11354 const char *exp_end = NULL;
11355 const char *tok, *end_tok;
11356 int toklen = -1;
11357 const char *cond_start = NULL;
11358 const char *cond_end = NULL;
11359 enum bptype bp_type;
11360 int thread = -1;
11361 int pc = 0;
11362 /* Flag to indicate whether we are going to use masks for
11363 the hardware watchpoint. */
11364 int use_mask = 0;
11365 CORE_ADDR mask = 0;
11366 struct watchpoint *w;
11367 char *expression;
11368 struct cleanup *back_to;
11369
11370 /* Make sure that we actually have parameters to parse. */
11371 if (arg != NULL && arg[0] != '\0')
11372 {
11373 const char *value_start;
11374
11375 exp_end = arg + strlen (arg);
11376
11377 /* Look for "parameter value" pairs at the end
11378 of the arguments string. */
11379 for (tok = exp_end - 1; tok > arg; tok--)
11380 {
11381 /* Skip whitespace at the end of the argument list. */
11382 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11383 tok--;
11384
11385 /* Find the beginning of the last token.
11386 This is the value of the parameter. */
11387 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11388 tok--;
11389 value_start = tok + 1;
11390
11391 /* Skip whitespace. */
11392 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11393 tok--;
11394
11395 end_tok = tok;
11396
11397 /* Find the beginning of the second to last token.
11398 This is the parameter itself. */
11399 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11400 tok--;
11401 tok++;
11402 toklen = end_tok - tok + 1;
11403
11404 if (toklen == 6 && !strncmp (tok, "thread", 6))
11405 {
11406 /* At this point we've found a "thread" token, which means
11407 the user is trying to set a watchpoint that triggers
11408 only in a specific thread. */
11409 char *endp;
11410
11411 if (thread != -1)
11412 error(_("You can specify only one thread."));
11413
11414 /* Extract the thread ID from the next token. */
11415 thread = strtol (value_start, &endp, 0);
11416
11417 /* Check if the user provided a valid numeric value for the
11418 thread ID. */
11419 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11420 error (_("Invalid thread ID specification %s."), value_start);
11421
11422 /* Check if the thread actually exists. */
11423 if (!valid_thread_id (thread))
11424 invalid_thread_id_error (thread);
11425 }
11426 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11427 {
11428 /* We've found a "mask" token, which means the user wants to
11429 create a hardware watchpoint that is going to have the mask
11430 facility. */
11431 struct value *mask_value, *mark;
11432
11433 if (use_mask)
11434 error(_("You can specify only one mask."));
11435
11436 use_mask = just_location = 1;
11437
11438 mark = value_mark ();
11439 mask_value = parse_to_comma_and_eval (&value_start);
11440 mask = value_as_address (mask_value);
11441 value_free_to_mark (mark);
11442 }
11443 else
11444 /* We didn't recognize what we found. We should stop here. */
11445 break;
11446
11447 /* Truncate the string and get rid of the "parameter value" pair before
11448 the arguments string is parsed by the parse_exp_1 function. */
11449 exp_end = tok;
11450 }
11451 }
11452 else
11453 exp_end = arg;
11454
11455 /* Parse the rest of the arguments. From here on out, everything
11456 is in terms of a newly allocated string instead of the original
11457 ARG. */
11458 innermost_block = NULL;
11459 expression = savestring (arg, exp_end - arg);
11460 back_to = make_cleanup (xfree, expression);
11461 exp_start = arg = expression;
11462 exp = parse_exp_1 (&arg, 0, 0, 0);
11463 exp_end = arg;
11464 /* Remove trailing whitespace from the expression before saving it.
11465 This makes the eventual display of the expression string a bit
11466 prettier. */
11467 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11468 --exp_end;
11469
11470 /* Checking if the expression is not constant. */
11471 if (watchpoint_exp_is_const (exp))
11472 {
11473 int len;
11474
11475 len = exp_end - exp_start;
11476 while (len > 0 && isspace (exp_start[len - 1]))
11477 len--;
11478 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11479 }
11480
11481 exp_valid_block = innermost_block;
11482 mark = value_mark ();
11483 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11484
11485 if (val != NULL && just_location)
11486 {
11487 saved_bitpos = value_bitpos (val);
11488 saved_bitsize = value_bitsize (val);
11489 }
11490
11491 if (just_location)
11492 {
11493 int ret;
11494
11495 exp_valid_block = NULL;
11496 val = value_addr (result);
11497 release_value (val);
11498 value_free_to_mark (mark);
11499
11500 if (use_mask)
11501 {
11502 ret = target_masked_watch_num_registers (value_as_address (val),
11503 mask);
11504 if (ret == -1)
11505 error (_("This target does not support masked watchpoints."));
11506 else if (ret == -2)
11507 error (_("Invalid mask or memory region."));
11508 }
11509 }
11510 else if (val != NULL)
11511 release_value (val);
11512
11513 tok = skip_spaces_const (arg);
11514 end_tok = skip_to_space_const (tok);
11515
11516 toklen = end_tok - tok;
11517 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11518 {
11519 struct expression *cond;
11520
11521 innermost_block = NULL;
11522 tok = cond_start = end_tok + 1;
11523 cond = parse_exp_1 (&tok, 0, 0, 0);
11524
11525 /* The watchpoint expression may not be local, but the condition
11526 may still be. E.g.: `watch global if local > 0'. */
11527 cond_exp_valid_block = innermost_block;
11528
11529 xfree (cond);
11530 cond_end = tok;
11531 }
11532 if (*tok)
11533 error (_("Junk at end of command."));
11534
11535 frame = block_innermost_frame (exp_valid_block);
11536
11537 /* If the expression is "local", then set up a "watchpoint scope"
11538 breakpoint at the point where we've left the scope of the watchpoint
11539 expression. Create the scope breakpoint before the watchpoint, so
11540 that we will encounter it first in bpstat_stop_status. */
11541 if (exp_valid_block && frame)
11542 {
11543 if (frame_id_p (frame_unwind_caller_id (frame)))
11544 {
11545 scope_breakpoint
11546 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11547 frame_unwind_caller_pc (frame),
11548 bp_watchpoint_scope,
11549 &momentary_breakpoint_ops);
11550
11551 scope_breakpoint->enable_state = bp_enabled;
11552
11553 /* Automatically delete the breakpoint when it hits. */
11554 scope_breakpoint->disposition = disp_del;
11555
11556 /* Only break in the proper frame (help with recursion). */
11557 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11558
11559 /* Set the address at which we will stop. */
11560 scope_breakpoint->loc->gdbarch
11561 = frame_unwind_caller_arch (frame);
11562 scope_breakpoint->loc->requested_address
11563 = frame_unwind_caller_pc (frame);
11564 scope_breakpoint->loc->address
11565 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11566 scope_breakpoint->loc->requested_address,
11567 scope_breakpoint->type);
11568 }
11569 }
11570
11571 /* Now set up the breakpoint. We create all watchpoints as hardware
11572 watchpoints here even if hardware watchpoints are turned off, a call
11573 to update_watchpoint later in this function will cause the type to
11574 drop back to bp_watchpoint (software watchpoint) if required. */
11575
11576 if (accessflag == hw_read)
11577 bp_type = bp_read_watchpoint;
11578 else if (accessflag == hw_access)
11579 bp_type = bp_access_watchpoint;
11580 else
11581 bp_type = bp_hardware_watchpoint;
11582
11583 w = XCNEW (struct watchpoint);
11584 b = &w->base;
11585 if (use_mask)
11586 init_raw_breakpoint_without_location (b, NULL, bp_type,
11587 &masked_watchpoint_breakpoint_ops);
11588 else
11589 init_raw_breakpoint_without_location (b, NULL, bp_type,
11590 &watchpoint_breakpoint_ops);
11591 b->thread = thread;
11592 b->disposition = disp_donttouch;
11593 b->pspace = current_program_space;
11594 w->exp = exp;
11595 w->exp_valid_block = exp_valid_block;
11596 w->cond_exp_valid_block = cond_exp_valid_block;
11597 if (just_location)
11598 {
11599 struct type *t = value_type (val);
11600 CORE_ADDR addr = value_as_address (val);
11601 char *name;
11602
11603 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11604 name = type_to_string (t);
11605
11606 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11607 core_addr_to_string (addr));
11608 xfree (name);
11609
11610 w->exp_string = xstrprintf ("-location %.*s",
11611 (int) (exp_end - exp_start), exp_start);
11612
11613 /* The above expression is in C. */
11614 b->language = language_c;
11615 }
11616 else
11617 w->exp_string = savestring (exp_start, exp_end - exp_start);
11618
11619 if (use_mask)
11620 {
11621 w->hw_wp_mask = mask;
11622 }
11623 else
11624 {
11625 w->val = val;
11626 w->val_bitpos = saved_bitpos;
11627 w->val_bitsize = saved_bitsize;
11628 w->val_valid = 1;
11629 }
11630
11631 if (cond_start)
11632 b->cond_string = savestring (cond_start, cond_end - cond_start);
11633 else
11634 b->cond_string = 0;
11635
11636 if (frame)
11637 {
11638 w->watchpoint_frame = get_frame_id (frame);
11639 w->watchpoint_thread = inferior_ptid;
11640 }
11641 else
11642 {
11643 w->watchpoint_frame = null_frame_id;
11644 w->watchpoint_thread = null_ptid;
11645 }
11646
11647 if (scope_breakpoint != NULL)
11648 {
11649 /* The scope breakpoint is related to the watchpoint. We will
11650 need to act on them together. */
11651 b->related_breakpoint = scope_breakpoint;
11652 scope_breakpoint->related_breakpoint = b;
11653 }
11654
11655 if (!just_location)
11656 value_free_to_mark (mark);
11657
11658 TRY_CATCH (e, RETURN_MASK_ALL)
11659 {
11660 /* Finally update the new watchpoint. This creates the locations
11661 that should be inserted. */
11662 update_watchpoint (w, 1);
11663 }
11664 if (e.reason < 0)
11665 {
11666 delete_breakpoint (b);
11667 throw_exception (e);
11668 }
11669
11670 install_breakpoint (internal, b, 1);
11671 do_cleanups (back_to);
11672 }
11673
11674 /* Return count of debug registers needed to watch the given expression.
11675 If the watchpoint cannot be handled in hardware return zero. */
11676
11677 static int
11678 can_use_hardware_watchpoint (struct value *v)
11679 {
11680 int found_memory_cnt = 0;
11681 struct value *head = v;
11682
11683 /* Did the user specifically forbid us to use hardware watchpoints? */
11684 if (!can_use_hw_watchpoints)
11685 return 0;
11686
11687 /* Make sure that the value of the expression depends only upon
11688 memory contents, and values computed from them within GDB. If we
11689 find any register references or function calls, we can't use a
11690 hardware watchpoint.
11691
11692 The idea here is that evaluating an expression generates a series
11693 of values, one holding the value of every subexpression. (The
11694 expression a*b+c has five subexpressions: a, b, a*b, c, and
11695 a*b+c.) GDB's values hold almost enough information to establish
11696 the criteria given above --- they identify memory lvalues,
11697 register lvalues, computed values, etcetera. So we can evaluate
11698 the expression, and then scan the chain of values that leaves
11699 behind to decide whether we can detect any possible change to the
11700 expression's final value using only hardware watchpoints.
11701
11702 However, I don't think that the values returned by inferior
11703 function calls are special in any way. So this function may not
11704 notice that an expression involving an inferior function call
11705 can't be watched with hardware watchpoints. FIXME. */
11706 for (; v; v = value_next (v))
11707 {
11708 if (VALUE_LVAL (v) == lval_memory)
11709 {
11710 if (v != head && value_lazy (v))
11711 /* A lazy memory lvalue in the chain is one that GDB never
11712 needed to fetch; we either just used its address (e.g.,
11713 `a' in `a.b') or we never needed it at all (e.g., `a'
11714 in `a,b'). This doesn't apply to HEAD; if that is
11715 lazy then it was not readable, but watch it anyway. */
11716 ;
11717 else
11718 {
11719 /* Ahh, memory we actually used! Check if we can cover
11720 it with hardware watchpoints. */
11721 struct type *vtype = check_typedef (value_type (v));
11722
11723 /* We only watch structs and arrays if user asked for it
11724 explicitly, never if they just happen to appear in a
11725 middle of some value chain. */
11726 if (v == head
11727 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11728 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11729 {
11730 CORE_ADDR vaddr = value_address (v);
11731 int len;
11732 int num_regs;
11733
11734 len = (target_exact_watchpoints
11735 && is_scalar_type_recursive (vtype))?
11736 1 : TYPE_LENGTH (value_type (v));
11737
11738 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11739 if (!num_regs)
11740 return 0;
11741 else
11742 found_memory_cnt += num_regs;
11743 }
11744 }
11745 }
11746 else if (VALUE_LVAL (v) != not_lval
11747 && deprecated_value_modifiable (v) == 0)
11748 return 0; /* These are values from the history (e.g., $1). */
11749 else if (VALUE_LVAL (v) == lval_register)
11750 return 0; /* Cannot watch a register with a HW watchpoint. */
11751 }
11752
11753 /* The expression itself looks suitable for using a hardware
11754 watchpoint, but give the target machine a chance to reject it. */
11755 return found_memory_cnt;
11756 }
11757
11758 void
11759 watch_command_wrapper (char *arg, int from_tty, int internal)
11760 {
11761 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11762 }
11763
11764 /* A helper function that looks for the "-location" argument and then
11765 calls watch_command_1. */
11766
11767 static void
11768 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11769 {
11770 int just_location = 0;
11771
11772 if (arg
11773 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11774 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11775 {
11776 arg = skip_spaces (arg);
11777 just_location = 1;
11778 }
11779
11780 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11781 }
11782
11783 static void
11784 watch_command (char *arg, int from_tty)
11785 {
11786 watch_maybe_just_location (arg, hw_write, from_tty);
11787 }
11788
11789 void
11790 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11791 {
11792 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11793 }
11794
11795 static void
11796 rwatch_command (char *arg, int from_tty)
11797 {
11798 watch_maybe_just_location (arg, hw_read, from_tty);
11799 }
11800
11801 void
11802 awatch_command_wrapper (char *arg, int from_tty, int internal)
11803 {
11804 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11805 }
11806
11807 static void
11808 awatch_command (char *arg, int from_tty)
11809 {
11810 watch_maybe_just_location (arg, hw_access, from_tty);
11811 }
11812 \f
11813
11814 /* Helper routines for the until_command routine in infcmd.c. Here
11815 because it uses the mechanisms of breakpoints. */
11816
11817 struct until_break_command_continuation_args
11818 {
11819 struct breakpoint *breakpoint;
11820 struct breakpoint *breakpoint2;
11821 int thread_num;
11822 };
11823
11824 /* This function is called by fetch_inferior_event via the
11825 cmd_continuation pointer, to complete the until command. It takes
11826 care of cleaning up the temporary breakpoints set up by the until
11827 command. */
11828 static void
11829 until_break_command_continuation (void *arg, int err)
11830 {
11831 struct until_break_command_continuation_args *a = arg;
11832
11833 delete_breakpoint (a->breakpoint);
11834 if (a->breakpoint2)
11835 delete_breakpoint (a->breakpoint2);
11836 delete_longjmp_breakpoint (a->thread_num);
11837 }
11838
11839 void
11840 until_break_command (char *arg, int from_tty, int anywhere)
11841 {
11842 struct symtabs_and_lines sals;
11843 struct symtab_and_line sal;
11844 struct frame_info *frame;
11845 struct gdbarch *frame_gdbarch;
11846 struct frame_id stack_frame_id;
11847 struct frame_id caller_frame_id;
11848 struct breakpoint *breakpoint;
11849 struct breakpoint *breakpoint2 = NULL;
11850 struct cleanup *old_chain;
11851 int thread;
11852 struct thread_info *tp;
11853
11854 clear_proceed_status (0);
11855
11856 /* Set a breakpoint where the user wants it and at return from
11857 this function. */
11858
11859 if (last_displayed_sal_is_valid ())
11860 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11861 get_last_displayed_symtab (),
11862 get_last_displayed_line ());
11863 else
11864 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11865 (struct symtab *) NULL, 0);
11866
11867 if (sals.nelts != 1)
11868 error (_("Couldn't get information on specified line."));
11869
11870 sal = sals.sals[0];
11871 xfree (sals.sals); /* malloc'd, so freed. */
11872
11873 if (*arg)
11874 error (_("Junk at end of arguments."));
11875
11876 resolve_sal_pc (&sal);
11877
11878 tp = inferior_thread ();
11879 thread = tp->num;
11880
11881 old_chain = make_cleanup (null_cleanup, NULL);
11882
11883 /* Note linespec handling above invalidates the frame chain.
11884 Installing a breakpoint also invalidates the frame chain (as it
11885 may need to switch threads), so do any frame handling before
11886 that. */
11887
11888 frame = get_selected_frame (NULL);
11889 frame_gdbarch = get_frame_arch (frame);
11890 stack_frame_id = get_stack_frame_id (frame);
11891 caller_frame_id = frame_unwind_caller_id (frame);
11892
11893 /* Keep within the current frame, or in frames called by the current
11894 one. */
11895
11896 if (frame_id_p (caller_frame_id))
11897 {
11898 struct symtab_and_line sal2;
11899
11900 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11901 sal2.pc = frame_unwind_caller_pc (frame);
11902 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11903 sal2,
11904 caller_frame_id,
11905 bp_until);
11906 make_cleanup_delete_breakpoint (breakpoint2);
11907
11908 set_longjmp_breakpoint (tp, caller_frame_id);
11909 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11910 }
11911
11912 /* set_momentary_breakpoint could invalidate FRAME. */
11913 frame = NULL;
11914
11915 if (anywhere)
11916 /* If the user told us to continue until a specified location,
11917 we don't specify a frame at which we need to stop. */
11918 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11919 null_frame_id, bp_until);
11920 else
11921 /* Otherwise, specify the selected frame, because we want to stop
11922 only at the very same frame. */
11923 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11924 stack_frame_id, bp_until);
11925 make_cleanup_delete_breakpoint (breakpoint);
11926
11927 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11928
11929 /* If we are running asynchronously, and proceed call above has
11930 actually managed to start the target, arrange for breakpoints to
11931 be deleted when the target stops. Otherwise, we're already
11932 stopped and delete breakpoints via cleanup chain. */
11933
11934 if (target_can_async_p () && is_running (inferior_ptid))
11935 {
11936 struct until_break_command_continuation_args *args;
11937 args = xmalloc (sizeof (*args));
11938
11939 args->breakpoint = breakpoint;
11940 args->breakpoint2 = breakpoint2;
11941 args->thread_num = thread;
11942
11943 discard_cleanups (old_chain);
11944 add_continuation (inferior_thread (),
11945 until_break_command_continuation, args,
11946 xfree);
11947 }
11948 else
11949 do_cleanups (old_chain);
11950 }
11951
11952 /* This function attempts to parse an optional "if <cond>" clause
11953 from the arg string. If one is not found, it returns NULL.
11954
11955 Else, it returns a pointer to the condition string. (It does not
11956 attempt to evaluate the string against a particular block.) And,
11957 it updates arg to point to the first character following the parsed
11958 if clause in the arg string. */
11959
11960 char *
11961 ep_parse_optional_if_clause (char **arg)
11962 {
11963 char *cond_string;
11964
11965 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11966 return NULL;
11967
11968 /* Skip the "if" keyword. */
11969 (*arg) += 2;
11970
11971 /* Skip any extra leading whitespace, and record the start of the
11972 condition string. */
11973 *arg = skip_spaces (*arg);
11974 cond_string = *arg;
11975
11976 /* Assume that the condition occupies the remainder of the arg
11977 string. */
11978 (*arg) += strlen (cond_string);
11979
11980 return cond_string;
11981 }
11982
11983 /* Commands to deal with catching events, such as signals, exceptions,
11984 process start/exit, etc. */
11985
11986 typedef enum
11987 {
11988 catch_fork_temporary, catch_vfork_temporary,
11989 catch_fork_permanent, catch_vfork_permanent
11990 }
11991 catch_fork_kind;
11992
11993 static void
11994 catch_fork_command_1 (char *arg, int from_tty,
11995 struct cmd_list_element *command)
11996 {
11997 struct gdbarch *gdbarch = get_current_arch ();
11998 char *cond_string = NULL;
11999 catch_fork_kind fork_kind;
12000 int tempflag;
12001
12002 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
12003 tempflag = (fork_kind == catch_fork_temporary
12004 || fork_kind == catch_vfork_temporary);
12005
12006 if (!arg)
12007 arg = "";
12008 arg = skip_spaces (arg);
12009
12010 /* The allowed syntax is:
12011 catch [v]fork
12012 catch [v]fork if <cond>
12013
12014 First, check if there's an if clause. */
12015 cond_string = ep_parse_optional_if_clause (&arg);
12016
12017 if ((*arg != '\0') && !isspace (*arg))
12018 error (_("Junk at end of arguments."));
12019
12020 /* If this target supports it, create a fork or vfork catchpoint
12021 and enable reporting of such events. */
12022 switch (fork_kind)
12023 {
12024 case catch_fork_temporary:
12025 case catch_fork_permanent:
12026 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12027 &catch_fork_breakpoint_ops);
12028 break;
12029 case catch_vfork_temporary:
12030 case catch_vfork_permanent:
12031 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12032 &catch_vfork_breakpoint_ops);
12033 break;
12034 default:
12035 error (_("unsupported or unknown fork kind; cannot catch it"));
12036 break;
12037 }
12038 }
12039
12040 static void
12041 catch_exec_command_1 (char *arg, int from_tty,
12042 struct cmd_list_element *command)
12043 {
12044 struct exec_catchpoint *c;
12045 struct gdbarch *gdbarch = get_current_arch ();
12046 int tempflag;
12047 char *cond_string = NULL;
12048
12049 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12050
12051 if (!arg)
12052 arg = "";
12053 arg = skip_spaces (arg);
12054
12055 /* The allowed syntax is:
12056 catch exec
12057 catch exec if <cond>
12058
12059 First, check if there's an if clause. */
12060 cond_string = ep_parse_optional_if_clause (&arg);
12061
12062 if ((*arg != '\0') && !isspace (*arg))
12063 error (_("Junk at end of arguments."));
12064
12065 c = XNEW (struct exec_catchpoint);
12066 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12067 &catch_exec_breakpoint_ops);
12068 c->exec_pathname = NULL;
12069
12070 install_breakpoint (0, &c->base, 1);
12071 }
12072
12073 void
12074 init_ada_exception_breakpoint (struct breakpoint *b,
12075 struct gdbarch *gdbarch,
12076 struct symtab_and_line sal,
12077 char *addr_string,
12078 const struct breakpoint_ops *ops,
12079 int tempflag,
12080 int enabled,
12081 int from_tty)
12082 {
12083 if (from_tty)
12084 {
12085 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12086 if (!loc_gdbarch)
12087 loc_gdbarch = gdbarch;
12088
12089 describe_other_breakpoints (loc_gdbarch,
12090 sal.pspace, sal.pc, sal.section, -1);
12091 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12092 version for exception catchpoints, because two catchpoints
12093 used for different exception names will use the same address.
12094 In this case, a "breakpoint ... also set at..." warning is
12095 unproductive. Besides, the warning phrasing is also a bit
12096 inappropriate, we should use the word catchpoint, and tell
12097 the user what type of catchpoint it is. The above is good
12098 enough for now, though. */
12099 }
12100
12101 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
12102
12103 b->enable_state = enabled ? bp_enabled : bp_disabled;
12104 b->disposition = tempflag ? disp_del : disp_donttouch;
12105 b->addr_string = addr_string;
12106 b->language = language_ada;
12107 }
12108
12109 /* Splits the argument using space as delimiter. Returns an xmalloc'd
12110 filter list, or NULL if no filtering is required. */
12111 static VEC(int) *
12112 catch_syscall_split_args (char *arg)
12113 {
12114 VEC(int) *result = NULL;
12115 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
12116
12117 while (*arg != '\0')
12118 {
12119 int i, syscall_number;
12120 char *endptr;
12121 char cur_name[128];
12122 struct syscall s;
12123
12124 /* Skip whitespace. */
12125 arg = skip_spaces (arg);
12126
12127 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12128 cur_name[i] = arg[i];
12129 cur_name[i] = '\0';
12130 arg += i;
12131
12132 /* Check if the user provided a syscall name or a number. */
12133 syscall_number = (int) strtol (cur_name, &endptr, 0);
12134 if (*endptr == '\0')
12135 get_syscall_by_number (syscall_number, &s);
12136 else
12137 {
12138 /* We have a name. Let's check if it's valid and convert it
12139 to a number. */
12140 get_syscall_by_name (cur_name, &s);
12141
12142 if (s.number == UNKNOWN_SYSCALL)
12143 /* Here we have to issue an error instead of a warning,
12144 because GDB cannot do anything useful if there's no
12145 syscall number to be caught. */
12146 error (_("Unknown syscall name '%s'."), cur_name);
12147 }
12148
12149 /* Ok, it's valid. */
12150 VEC_safe_push (int, result, s.number);
12151 }
12152
12153 discard_cleanups (cleanup);
12154 return result;
12155 }
12156
12157 /* Implement the "catch syscall" command. */
12158
12159 static void
12160 catch_syscall_command_1 (char *arg, int from_tty,
12161 struct cmd_list_element *command)
12162 {
12163 int tempflag;
12164 VEC(int) *filter;
12165 struct syscall s;
12166 struct gdbarch *gdbarch = get_current_arch ();
12167
12168 /* Checking if the feature if supported. */
12169 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12170 error (_("The feature 'catch syscall' is not supported on \
12171 this architecture yet."));
12172
12173 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12174
12175 arg = skip_spaces (arg);
12176
12177 /* We need to do this first "dummy" translation in order
12178 to get the syscall XML file loaded or, most important,
12179 to display a warning to the user if there's no XML file
12180 for his/her architecture. */
12181 get_syscall_by_number (0, &s);
12182
12183 /* The allowed syntax is:
12184 catch syscall
12185 catch syscall <name | number> [<name | number> ... <name | number>]
12186
12187 Let's check if there's a syscall name. */
12188
12189 if (arg != NULL)
12190 filter = catch_syscall_split_args (arg);
12191 else
12192 filter = NULL;
12193
12194 create_syscall_event_catchpoint (tempflag, filter,
12195 &catch_syscall_breakpoint_ops);
12196 }
12197
12198 static void
12199 catch_command (char *arg, int from_tty)
12200 {
12201 error (_("Catch requires an event name."));
12202 }
12203 \f
12204
12205 static void
12206 tcatch_command (char *arg, int from_tty)
12207 {
12208 error (_("Catch requires an event name."));
12209 }
12210
12211 /* A qsort comparison function that sorts breakpoints in order. */
12212
12213 static int
12214 compare_breakpoints (const void *a, const void *b)
12215 {
12216 const breakpoint_p *ba = a;
12217 uintptr_t ua = (uintptr_t) *ba;
12218 const breakpoint_p *bb = b;
12219 uintptr_t ub = (uintptr_t) *bb;
12220
12221 if ((*ba)->number < (*bb)->number)
12222 return -1;
12223 else if ((*ba)->number > (*bb)->number)
12224 return 1;
12225
12226 /* Now sort by address, in case we see, e..g, two breakpoints with
12227 the number 0. */
12228 if (ua < ub)
12229 return -1;
12230 return ua > ub ? 1 : 0;
12231 }
12232
12233 /* Delete breakpoints by address or line. */
12234
12235 static void
12236 clear_command (char *arg, int from_tty)
12237 {
12238 struct breakpoint *b, *prev;
12239 VEC(breakpoint_p) *found = 0;
12240 int ix;
12241 int default_match;
12242 struct symtabs_and_lines sals;
12243 struct symtab_and_line sal;
12244 int i;
12245 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12246
12247 if (arg)
12248 {
12249 sals = decode_line_with_current_source (arg,
12250 (DECODE_LINE_FUNFIRSTLINE
12251 | DECODE_LINE_LIST_MODE));
12252 make_cleanup (xfree, sals.sals);
12253 default_match = 0;
12254 }
12255 else
12256 {
12257 sals.sals = (struct symtab_and_line *)
12258 xmalloc (sizeof (struct symtab_and_line));
12259 make_cleanup (xfree, sals.sals);
12260 init_sal (&sal); /* Initialize to zeroes. */
12261
12262 /* Set sal's line, symtab, pc, and pspace to the values
12263 corresponding to the last call to print_frame_info. If the
12264 codepoint is not valid, this will set all the fields to 0. */
12265 get_last_displayed_sal (&sal);
12266 if (sal.symtab == 0)
12267 error (_("No source file specified."));
12268
12269 sals.sals[0] = sal;
12270 sals.nelts = 1;
12271
12272 default_match = 1;
12273 }
12274
12275 /* We don't call resolve_sal_pc here. That's not as bad as it
12276 seems, because all existing breakpoints typically have both
12277 file/line and pc set. So, if clear is given file/line, we can
12278 match this to existing breakpoint without obtaining pc at all.
12279
12280 We only support clearing given the address explicitly
12281 present in breakpoint table. Say, we've set breakpoint
12282 at file:line. There were several PC values for that file:line,
12283 due to optimization, all in one block.
12284
12285 We've picked one PC value. If "clear" is issued with another
12286 PC corresponding to the same file:line, the breakpoint won't
12287 be cleared. We probably can still clear the breakpoint, but
12288 since the other PC value is never presented to user, user
12289 can only find it by guessing, and it does not seem important
12290 to support that. */
12291
12292 /* For each line spec given, delete bps which correspond to it. Do
12293 it in two passes, solely to preserve the current behavior that
12294 from_tty is forced true if we delete more than one
12295 breakpoint. */
12296
12297 found = NULL;
12298 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12299 for (i = 0; i < sals.nelts; i++)
12300 {
12301 const char *sal_fullname;
12302
12303 /* If exact pc given, clear bpts at that pc.
12304 If line given (pc == 0), clear all bpts on specified line.
12305 If defaulting, clear all bpts on default line
12306 or at default pc.
12307
12308 defaulting sal.pc != 0 tests to do
12309
12310 0 1 pc
12311 1 1 pc _and_ line
12312 0 0 line
12313 1 0 <can't happen> */
12314
12315 sal = sals.sals[i];
12316 sal_fullname = (sal.symtab == NULL
12317 ? NULL : symtab_to_fullname (sal.symtab));
12318
12319 /* Find all matching breakpoints and add them to 'found'. */
12320 ALL_BREAKPOINTS (b)
12321 {
12322 int match = 0;
12323 /* Are we going to delete b? */
12324 if (b->type != bp_none && !is_watchpoint (b))
12325 {
12326 struct bp_location *loc = b->loc;
12327 for (; loc; loc = loc->next)
12328 {
12329 /* If the user specified file:line, don't allow a PC
12330 match. This matches historical gdb behavior. */
12331 int pc_match = (!sal.explicit_line
12332 && sal.pc
12333 && (loc->pspace == sal.pspace)
12334 && (loc->address == sal.pc)
12335 && (!section_is_overlay (loc->section)
12336 || loc->section == sal.section));
12337 int line_match = 0;
12338
12339 if ((default_match || sal.explicit_line)
12340 && loc->symtab != NULL
12341 && sal_fullname != NULL
12342 && sal.pspace == loc->pspace
12343 && loc->line_number == sal.line
12344 && filename_cmp (symtab_to_fullname (loc->symtab),
12345 sal_fullname) == 0)
12346 line_match = 1;
12347
12348 if (pc_match || line_match)
12349 {
12350 match = 1;
12351 break;
12352 }
12353 }
12354 }
12355
12356 if (match)
12357 VEC_safe_push(breakpoint_p, found, b);
12358 }
12359 }
12360
12361 /* Now go thru the 'found' chain and delete them. */
12362 if (VEC_empty(breakpoint_p, found))
12363 {
12364 if (arg)
12365 error (_("No breakpoint at %s."), arg);
12366 else
12367 error (_("No breakpoint at this line."));
12368 }
12369
12370 /* Remove duplicates from the vec. */
12371 qsort (VEC_address (breakpoint_p, found),
12372 VEC_length (breakpoint_p, found),
12373 sizeof (breakpoint_p),
12374 compare_breakpoints);
12375 prev = VEC_index (breakpoint_p, found, 0);
12376 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12377 {
12378 if (b == prev)
12379 {
12380 VEC_ordered_remove (breakpoint_p, found, ix);
12381 --ix;
12382 }
12383 }
12384
12385 if (VEC_length(breakpoint_p, found) > 1)
12386 from_tty = 1; /* Always report if deleted more than one. */
12387 if (from_tty)
12388 {
12389 if (VEC_length(breakpoint_p, found) == 1)
12390 printf_unfiltered (_("Deleted breakpoint "));
12391 else
12392 printf_unfiltered (_("Deleted breakpoints "));
12393 }
12394
12395 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12396 {
12397 if (from_tty)
12398 printf_unfiltered ("%d ", b->number);
12399 delete_breakpoint (b);
12400 }
12401 if (from_tty)
12402 putchar_unfiltered ('\n');
12403
12404 do_cleanups (cleanups);
12405 }
12406 \f
12407 /* Delete breakpoint in BS if they are `delete' breakpoints and
12408 all breakpoints that are marked for deletion, whether hit or not.
12409 This is called after any breakpoint is hit, or after errors. */
12410
12411 void
12412 breakpoint_auto_delete (bpstat bs)
12413 {
12414 struct breakpoint *b, *b_tmp;
12415
12416 for (; bs; bs = bs->next)
12417 if (bs->breakpoint_at
12418 && bs->breakpoint_at->disposition == disp_del
12419 && bs->stop)
12420 delete_breakpoint (bs->breakpoint_at);
12421
12422 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12423 {
12424 if (b->disposition == disp_del_at_next_stop)
12425 delete_breakpoint (b);
12426 }
12427 }
12428
12429 /* A comparison function for bp_location AP and BP being interfaced to
12430 qsort. Sort elements primarily by their ADDRESS (no matter what
12431 does breakpoint_address_is_meaningful say for its OWNER),
12432 secondarily by ordering first bp_permanent OWNERed elements and
12433 terciarily just ensuring the array is sorted stable way despite
12434 qsort being an unstable algorithm. */
12435
12436 static int
12437 bp_location_compare (const void *ap, const void *bp)
12438 {
12439 struct bp_location *a = *(void **) ap;
12440 struct bp_location *b = *(void **) bp;
12441 /* A and B come from existing breakpoints having non-NULL OWNER. */
12442 int a_perm = a->owner->enable_state == bp_permanent;
12443 int b_perm = b->owner->enable_state == bp_permanent;
12444
12445 if (a->address != b->address)
12446 return (a->address > b->address) - (a->address < b->address);
12447
12448 /* Sort locations at the same address by their pspace number, keeping
12449 locations of the same inferior (in a multi-inferior environment)
12450 grouped. */
12451
12452 if (a->pspace->num != b->pspace->num)
12453 return ((a->pspace->num > b->pspace->num)
12454 - (a->pspace->num < b->pspace->num));
12455
12456 /* Sort permanent breakpoints first. */
12457 if (a_perm != b_perm)
12458 return (a_perm < b_perm) - (a_perm > b_perm);
12459
12460 /* Make the internal GDB representation stable across GDB runs
12461 where A and B memory inside GDB can differ. Breakpoint locations of
12462 the same type at the same address can be sorted in arbitrary order. */
12463
12464 if (a->owner->number != b->owner->number)
12465 return ((a->owner->number > b->owner->number)
12466 - (a->owner->number < b->owner->number));
12467
12468 return (a > b) - (a < b);
12469 }
12470
12471 /* Set bp_location_placed_address_before_address_max and
12472 bp_location_shadow_len_after_address_max according to the current
12473 content of the bp_location array. */
12474
12475 static void
12476 bp_location_target_extensions_update (void)
12477 {
12478 struct bp_location *bl, **blp_tmp;
12479
12480 bp_location_placed_address_before_address_max = 0;
12481 bp_location_shadow_len_after_address_max = 0;
12482
12483 ALL_BP_LOCATIONS (bl, blp_tmp)
12484 {
12485 CORE_ADDR start, end, addr;
12486
12487 if (!bp_location_has_shadow (bl))
12488 continue;
12489
12490 start = bl->target_info.placed_address;
12491 end = start + bl->target_info.shadow_len;
12492
12493 gdb_assert (bl->address >= start);
12494 addr = bl->address - start;
12495 if (addr > bp_location_placed_address_before_address_max)
12496 bp_location_placed_address_before_address_max = addr;
12497
12498 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12499
12500 gdb_assert (bl->address < end);
12501 addr = end - bl->address;
12502 if (addr > bp_location_shadow_len_after_address_max)
12503 bp_location_shadow_len_after_address_max = addr;
12504 }
12505 }
12506
12507 /* Download tracepoint locations if they haven't been. */
12508
12509 static void
12510 download_tracepoint_locations (void)
12511 {
12512 struct breakpoint *b;
12513 struct cleanup *old_chain;
12514
12515 if (!target_can_download_tracepoint ())
12516 return;
12517
12518 old_chain = save_current_space_and_thread ();
12519
12520 ALL_TRACEPOINTS (b)
12521 {
12522 struct bp_location *bl;
12523 struct tracepoint *t;
12524 int bp_location_downloaded = 0;
12525
12526 if ((b->type == bp_fast_tracepoint
12527 ? !may_insert_fast_tracepoints
12528 : !may_insert_tracepoints))
12529 continue;
12530
12531 for (bl = b->loc; bl; bl = bl->next)
12532 {
12533 /* In tracepoint, locations are _never_ duplicated, so
12534 should_be_inserted is equivalent to
12535 unduplicated_should_be_inserted. */
12536 if (!should_be_inserted (bl) || bl->inserted)
12537 continue;
12538
12539 switch_to_program_space_and_thread (bl->pspace);
12540
12541 target_download_tracepoint (bl);
12542
12543 bl->inserted = 1;
12544 bp_location_downloaded = 1;
12545 }
12546 t = (struct tracepoint *) b;
12547 t->number_on_target = b->number;
12548 if (bp_location_downloaded)
12549 observer_notify_breakpoint_modified (b);
12550 }
12551
12552 do_cleanups (old_chain);
12553 }
12554
12555 /* Swap the insertion/duplication state between two locations. */
12556
12557 static void
12558 swap_insertion (struct bp_location *left, struct bp_location *right)
12559 {
12560 const int left_inserted = left->inserted;
12561 const int left_duplicate = left->duplicate;
12562 const int left_needs_update = left->needs_update;
12563 const struct bp_target_info left_target_info = left->target_info;
12564
12565 /* Locations of tracepoints can never be duplicated. */
12566 if (is_tracepoint (left->owner))
12567 gdb_assert (!left->duplicate);
12568 if (is_tracepoint (right->owner))
12569 gdb_assert (!right->duplicate);
12570
12571 left->inserted = right->inserted;
12572 left->duplicate = right->duplicate;
12573 left->needs_update = right->needs_update;
12574 left->target_info = right->target_info;
12575 right->inserted = left_inserted;
12576 right->duplicate = left_duplicate;
12577 right->needs_update = left_needs_update;
12578 right->target_info = left_target_info;
12579 }
12580
12581 /* Force the re-insertion of the locations at ADDRESS. This is called
12582 once a new/deleted/modified duplicate location is found and we are evaluating
12583 conditions on the target's side. Such conditions need to be updated on
12584 the target. */
12585
12586 static void
12587 force_breakpoint_reinsertion (struct bp_location *bl)
12588 {
12589 struct bp_location **locp = NULL, **loc2p;
12590 struct bp_location *loc;
12591 CORE_ADDR address = 0;
12592 int pspace_num;
12593
12594 address = bl->address;
12595 pspace_num = bl->pspace->num;
12596
12597 /* This is only meaningful if the target is
12598 evaluating conditions and if the user has
12599 opted for condition evaluation on the target's
12600 side. */
12601 if (gdb_evaluates_breakpoint_condition_p ()
12602 || !target_supports_evaluation_of_breakpoint_conditions ())
12603 return;
12604
12605 /* Flag all breakpoint locations with this address and
12606 the same program space as the location
12607 as "its condition has changed". We need to
12608 update the conditions on the target's side. */
12609 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12610 {
12611 loc = *loc2p;
12612
12613 if (!is_breakpoint (loc->owner)
12614 || pspace_num != loc->pspace->num)
12615 continue;
12616
12617 /* Flag the location appropriately. We use a different state to
12618 let everyone know that we already updated the set of locations
12619 with addr bl->address and program space bl->pspace. This is so
12620 we don't have to keep calling these functions just to mark locations
12621 that have already been marked. */
12622 loc->condition_changed = condition_updated;
12623
12624 /* Free the agent expression bytecode as well. We will compute
12625 it later on. */
12626 if (loc->cond_bytecode)
12627 {
12628 free_agent_expr (loc->cond_bytecode);
12629 loc->cond_bytecode = NULL;
12630 }
12631 }
12632 }
12633 /* Called whether new breakpoints are created, or existing breakpoints
12634 deleted, to update the global location list and recompute which
12635 locations are duplicate of which.
12636
12637 The INSERT_MODE flag determines whether locations may not, may, or
12638 shall be inserted now. See 'enum ugll_insert_mode' for more
12639 info. */
12640
12641 static void
12642 update_global_location_list (enum ugll_insert_mode insert_mode)
12643 {
12644 struct breakpoint *b;
12645 struct bp_location **locp, *loc;
12646 struct cleanup *cleanups;
12647 /* Last breakpoint location address that was marked for update. */
12648 CORE_ADDR last_addr = 0;
12649 /* Last breakpoint location program space that was marked for update. */
12650 int last_pspace_num = -1;
12651
12652 /* Used in the duplicates detection below. When iterating over all
12653 bp_locations, points to the first bp_location of a given address.
12654 Breakpoints and watchpoints of different types are never
12655 duplicates of each other. Keep one pointer for each type of
12656 breakpoint/watchpoint, so we only need to loop over all locations
12657 once. */
12658 struct bp_location *bp_loc_first; /* breakpoint */
12659 struct bp_location *wp_loc_first; /* hardware watchpoint */
12660 struct bp_location *awp_loc_first; /* access watchpoint */
12661 struct bp_location *rwp_loc_first; /* read watchpoint */
12662
12663 /* Saved former bp_location array which we compare against the newly
12664 built bp_location from the current state of ALL_BREAKPOINTS. */
12665 struct bp_location **old_location, **old_locp;
12666 unsigned old_location_count;
12667
12668 old_location = bp_location;
12669 old_location_count = bp_location_count;
12670 bp_location = NULL;
12671 bp_location_count = 0;
12672 cleanups = make_cleanup (xfree, old_location);
12673
12674 ALL_BREAKPOINTS (b)
12675 for (loc = b->loc; loc; loc = loc->next)
12676 bp_location_count++;
12677
12678 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12679 locp = bp_location;
12680 ALL_BREAKPOINTS (b)
12681 for (loc = b->loc; loc; loc = loc->next)
12682 *locp++ = loc;
12683 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12684 bp_location_compare);
12685
12686 bp_location_target_extensions_update ();
12687
12688 /* Identify bp_location instances that are no longer present in the
12689 new list, and therefore should be freed. Note that it's not
12690 necessary that those locations should be removed from inferior --
12691 if there's another location at the same address (previously
12692 marked as duplicate), we don't need to remove/insert the
12693 location.
12694
12695 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12696 and former bp_location array state respectively. */
12697
12698 locp = bp_location;
12699 for (old_locp = old_location; old_locp < old_location + old_location_count;
12700 old_locp++)
12701 {
12702 struct bp_location *old_loc = *old_locp;
12703 struct bp_location **loc2p;
12704
12705 /* Tells if 'old_loc' is found among the new locations. If
12706 not, we have to free it. */
12707 int found_object = 0;
12708 /* Tells if the location should remain inserted in the target. */
12709 int keep_in_target = 0;
12710 int removed = 0;
12711
12712 /* Skip LOCP entries which will definitely never be needed.
12713 Stop either at or being the one matching OLD_LOC. */
12714 while (locp < bp_location + bp_location_count
12715 && (*locp)->address < old_loc->address)
12716 locp++;
12717
12718 for (loc2p = locp;
12719 (loc2p < bp_location + bp_location_count
12720 && (*loc2p)->address == old_loc->address);
12721 loc2p++)
12722 {
12723 /* Check if this is a new/duplicated location or a duplicated
12724 location that had its condition modified. If so, we want to send
12725 its condition to the target if evaluation of conditions is taking
12726 place there. */
12727 if ((*loc2p)->condition_changed == condition_modified
12728 && (last_addr != old_loc->address
12729 || last_pspace_num != old_loc->pspace->num))
12730 {
12731 force_breakpoint_reinsertion (*loc2p);
12732 last_pspace_num = old_loc->pspace->num;
12733 }
12734
12735 if (*loc2p == old_loc)
12736 found_object = 1;
12737 }
12738
12739 /* We have already handled this address, update it so that we don't
12740 have to go through updates again. */
12741 last_addr = old_loc->address;
12742
12743 /* Target-side condition evaluation: Handle deleted locations. */
12744 if (!found_object)
12745 force_breakpoint_reinsertion (old_loc);
12746
12747 /* If this location is no longer present, and inserted, look if
12748 there's maybe a new location at the same address. If so,
12749 mark that one inserted, and don't remove this one. This is
12750 needed so that we don't have a time window where a breakpoint
12751 at certain location is not inserted. */
12752
12753 if (old_loc->inserted)
12754 {
12755 /* If the location is inserted now, we might have to remove
12756 it. */
12757
12758 if (found_object && should_be_inserted (old_loc))
12759 {
12760 /* The location is still present in the location list,
12761 and still should be inserted. Don't do anything. */
12762 keep_in_target = 1;
12763 }
12764 else
12765 {
12766 /* This location still exists, but it won't be kept in the
12767 target since it may have been disabled. We proceed to
12768 remove its target-side condition. */
12769
12770 /* The location is either no longer present, or got
12771 disabled. See if there's another location at the
12772 same address, in which case we don't need to remove
12773 this one from the target. */
12774
12775 /* OLD_LOC comes from existing struct breakpoint. */
12776 if (breakpoint_address_is_meaningful (old_loc->owner))
12777 {
12778 for (loc2p = locp;
12779 (loc2p < bp_location + bp_location_count
12780 && (*loc2p)->address == old_loc->address);
12781 loc2p++)
12782 {
12783 struct bp_location *loc2 = *loc2p;
12784
12785 if (breakpoint_locations_match (loc2, old_loc))
12786 {
12787 /* Read watchpoint locations are switched to
12788 access watchpoints, if the former are not
12789 supported, but the latter are. */
12790 if (is_hardware_watchpoint (old_loc->owner))
12791 {
12792 gdb_assert (is_hardware_watchpoint (loc2->owner));
12793 loc2->watchpoint_type = old_loc->watchpoint_type;
12794 }
12795
12796 /* loc2 is a duplicated location. We need to check
12797 if it should be inserted in case it will be
12798 unduplicated. */
12799 if (loc2 != old_loc
12800 && unduplicated_should_be_inserted (loc2))
12801 {
12802 swap_insertion (old_loc, loc2);
12803 keep_in_target = 1;
12804 break;
12805 }
12806 }
12807 }
12808 }
12809 }
12810
12811 if (!keep_in_target)
12812 {
12813 if (remove_breakpoint (old_loc, mark_uninserted))
12814 {
12815 /* This is just about all we can do. We could keep
12816 this location on the global list, and try to
12817 remove it next time, but there's no particular
12818 reason why we will succeed next time.
12819
12820 Note that at this point, old_loc->owner is still
12821 valid, as delete_breakpoint frees the breakpoint
12822 only after calling us. */
12823 printf_filtered (_("warning: Error removing "
12824 "breakpoint %d\n"),
12825 old_loc->owner->number);
12826 }
12827 removed = 1;
12828 }
12829 }
12830
12831 if (!found_object)
12832 {
12833 if (removed && non_stop
12834 && breakpoint_address_is_meaningful (old_loc->owner)
12835 && !is_hardware_watchpoint (old_loc->owner))
12836 {
12837 /* This location was removed from the target. In
12838 non-stop mode, a race condition is possible where
12839 we've removed a breakpoint, but stop events for that
12840 breakpoint are already queued and will arrive later.
12841 We apply an heuristic to be able to distinguish such
12842 SIGTRAPs from other random SIGTRAPs: we keep this
12843 breakpoint location for a bit, and will retire it
12844 after we see some number of events. The theory here
12845 is that reporting of events should, "on the average",
12846 be fair, so after a while we'll see events from all
12847 threads that have anything of interest, and no longer
12848 need to keep this breakpoint location around. We
12849 don't hold locations forever so to reduce chances of
12850 mistaking a non-breakpoint SIGTRAP for a breakpoint
12851 SIGTRAP.
12852
12853 The heuristic failing can be disastrous on
12854 decr_pc_after_break targets.
12855
12856 On decr_pc_after_break targets, like e.g., x86-linux,
12857 if we fail to recognize a late breakpoint SIGTRAP,
12858 because events_till_retirement has reached 0 too
12859 soon, we'll fail to do the PC adjustment, and report
12860 a random SIGTRAP to the user. When the user resumes
12861 the inferior, it will most likely immediately crash
12862 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12863 corrupted, because of being resumed e.g., in the
12864 middle of a multi-byte instruction, or skipped a
12865 one-byte instruction. This was actually seen happen
12866 on native x86-linux, and should be less rare on
12867 targets that do not support new thread events, like
12868 remote, due to the heuristic depending on
12869 thread_count.
12870
12871 Mistaking a random SIGTRAP for a breakpoint trap
12872 causes similar symptoms (PC adjustment applied when
12873 it shouldn't), but then again, playing with SIGTRAPs
12874 behind the debugger's back is asking for trouble.
12875
12876 Since hardware watchpoint traps are always
12877 distinguishable from other traps, so we don't need to
12878 apply keep hardware watchpoint moribund locations
12879 around. We simply always ignore hardware watchpoint
12880 traps we can no longer explain. */
12881
12882 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12883 old_loc->owner = NULL;
12884
12885 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12886 }
12887 else
12888 {
12889 old_loc->owner = NULL;
12890 decref_bp_location (&old_loc);
12891 }
12892 }
12893 }
12894
12895 /* Rescan breakpoints at the same address and section, marking the
12896 first one as "first" and any others as "duplicates". This is so
12897 that the bpt instruction is only inserted once. If we have a
12898 permanent breakpoint at the same place as BPT, make that one the
12899 official one, and the rest as duplicates. Permanent breakpoints
12900 are sorted first for the same address.
12901
12902 Do the same for hardware watchpoints, but also considering the
12903 watchpoint's type (regular/access/read) and length. */
12904
12905 bp_loc_first = NULL;
12906 wp_loc_first = NULL;
12907 awp_loc_first = NULL;
12908 rwp_loc_first = NULL;
12909 ALL_BP_LOCATIONS (loc, locp)
12910 {
12911 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12912 non-NULL. */
12913 struct bp_location **loc_first_p;
12914 b = loc->owner;
12915
12916 if (!unduplicated_should_be_inserted (loc)
12917 || !breakpoint_address_is_meaningful (b)
12918 /* Don't detect duplicate for tracepoint locations because they are
12919 never duplicated. See the comments in field `duplicate' of
12920 `struct bp_location'. */
12921 || is_tracepoint (b))
12922 {
12923 /* Clear the condition modification flag. */
12924 loc->condition_changed = condition_unchanged;
12925 continue;
12926 }
12927
12928 /* Permanent breakpoint should always be inserted. */
12929 if (b->enable_state == bp_permanent && ! loc->inserted)
12930 internal_error (__FILE__, __LINE__,
12931 _("allegedly permanent breakpoint is not "
12932 "actually inserted"));
12933
12934 if (b->type == bp_hardware_watchpoint)
12935 loc_first_p = &wp_loc_first;
12936 else if (b->type == bp_read_watchpoint)
12937 loc_first_p = &rwp_loc_first;
12938 else if (b->type == bp_access_watchpoint)
12939 loc_first_p = &awp_loc_first;
12940 else
12941 loc_first_p = &bp_loc_first;
12942
12943 if (*loc_first_p == NULL
12944 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12945 || !breakpoint_locations_match (loc, *loc_first_p))
12946 {
12947 *loc_first_p = loc;
12948 loc->duplicate = 0;
12949
12950 if (is_breakpoint (loc->owner) && loc->condition_changed)
12951 {
12952 loc->needs_update = 1;
12953 /* Clear the condition modification flag. */
12954 loc->condition_changed = condition_unchanged;
12955 }
12956 continue;
12957 }
12958
12959
12960 /* This and the above ensure the invariant that the first location
12961 is not duplicated, and is the inserted one.
12962 All following are marked as duplicated, and are not inserted. */
12963 if (loc->inserted)
12964 swap_insertion (loc, *loc_first_p);
12965 loc->duplicate = 1;
12966
12967 /* Clear the condition modification flag. */
12968 loc->condition_changed = condition_unchanged;
12969
12970 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12971 && b->enable_state != bp_permanent)
12972 internal_error (__FILE__, __LINE__,
12973 _("another breakpoint was inserted on top of "
12974 "a permanent breakpoint"));
12975 }
12976
12977 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12978 {
12979 if (insert_mode != UGLL_DONT_INSERT)
12980 insert_breakpoint_locations ();
12981 else
12982 {
12983 /* Even though the caller told us to not insert new
12984 locations, we may still need to update conditions on the
12985 target's side of breakpoints that were already inserted
12986 if the target is evaluating breakpoint conditions. We
12987 only update conditions for locations that are marked
12988 "needs_update". */
12989 update_inserted_breakpoint_locations ();
12990 }
12991 }
12992
12993 if (insert_mode != UGLL_DONT_INSERT)
12994 download_tracepoint_locations ();
12995
12996 do_cleanups (cleanups);
12997 }
12998
12999 void
13000 breakpoint_retire_moribund (void)
13001 {
13002 struct bp_location *loc;
13003 int ix;
13004
13005 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
13006 if (--(loc->events_till_retirement) == 0)
13007 {
13008 decref_bp_location (&loc);
13009 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13010 --ix;
13011 }
13012 }
13013
13014 static void
13015 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
13016 {
13017 volatile struct gdb_exception e;
13018
13019 TRY_CATCH (e, RETURN_MASK_ERROR)
13020 update_global_location_list (insert_mode);
13021 }
13022
13023 /* Clear BKP from a BPS. */
13024
13025 static void
13026 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13027 {
13028 bpstat bs;
13029
13030 for (bs = bps; bs; bs = bs->next)
13031 if (bs->breakpoint_at == bpt)
13032 {
13033 bs->breakpoint_at = NULL;
13034 bs->old_val = NULL;
13035 /* bs->commands will be freed later. */
13036 }
13037 }
13038
13039 /* Callback for iterate_over_threads. */
13040 static int
13041 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13042 {
13043 struct breakpoint *bpt = data;
13044
13045 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13046 return 0;
13047 }
13048
13049 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
13050 callbacks. */
13051
13052 static void
13053 say_where (struct breakpoint *b)
13054 {
13055 struct value_print_options opts;
13056
13057 get_user_print_options (&opts);
13058
13059 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13060 single string. */
13061 if (b->loc == NULL)
13062 {
13063 printf_filtered (_(" (%s) pending."), b->addr_string);
13064 }
13065 else
13066 {
13067 if (opts.addressprint || b->loc->symtab == NULL)
13068 {
13069 printf_filtered (" at ");
13070 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13071 gdb_stdout);
13072 }
13073 if (b->loc->symtab != NULL)
13074 {
13075 /* If there is a single location, we can print the location
13076 more nicely. */
13077 if (b->loc->next == NULL)
13078 printf_filtered (": file %s, line %d.",
13079 symtab_to_filename_for_display (b->loc->symtab),
13080 b->loc->line_number);
13081 else
13082 /* This is not ideal, but each location may have a
13083 different file name, and this at least reflects the
13084 real situation somewhat. */
13085 printf_filtered (": %s.", b->addr_string);
13086 }
13087
13088 if (b->loc->next)
13089 {
13090 struct bp_location *loc = b->loc;
13091 int n = 0;
13092 for (; loc; loc = loc->next)
13093 ++n;
13094 printf_filtered (" (%d locations)", n);
13095 }
13096 }
13097 }
13098
13099 /* Default bp_location_ops methods. */
13100
13101 static void
13102 bp_location_dtor (struct bp_location *self)
13103 {
13104 xfree (self->cond);
13105 if (self->cond_bytecode)
13106 free_agent_expr (self->cond_bytecode);
13107 xfree (self->function_name);
13108
13109 VEC_free (agent_expr_p, self->target_info.conditions);
13110 VEC_free (agent_expr_p, self->target_info.tcommands);
13111 }
13112
13113 static const struct bp_location_ops bp_location_ops =
13114 {
13115 bp_location_dtor
13116 };
13117
13118 /* Default breakpoint_ops methods all breakpoint_ops ultimately
13119 inherit from. */
13120
13121 static void
13122 base_breakpoint_dtor (struct breakpoint *self)
13123 {
13124 decref_counted_command_line (&self->commands);
13125 xfree (self->cond_string);
13126 xfree (self->extra_string);
13127 xfree (self->addr_string);
13128 xfree (self->filter);
13129 xfree (self->addr_string_range_end);
13130 }
13131
13132 static struct bp_location *
13133 base_breakpoint_allocate_location (struct breakpoint *self)
13134 {
13135 struct bp_location *loc;
13136
13137 loc = XNEW (struct bp_location);
13138 init_bp_location (loc, &bp_location_ops, self);
13139 return loc;
13140 }
13141
13142 static void
13143 base_breakpoint_re_set (struct breakpoint *b)
13144 {
13145 /* Nothing to re-set. */
13146 }
13147
13148 #define internal_error_pure_virtual_called() \
13149 gdb_assert_not_reached ("pure virtual function called")
13150
13151 static int
13152 base_breakpoint_insert_location (struct bp_location *bl)
13153 {
13154 internal_error_pure_virtual_called ();
13155 }
13156
13157 static int
13158 base_breakpoint_remove_location (struct bp_location *bl)
13159 {
13160 internal_error_pure_virtual_called ();
13161 }
13162
13163 static int
13164 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13165 struct address_space *aspace,
13166 CORE_ADDR bp_addr,
13167 const struct target_waitstatus *ws)
13168 {
13169 internal_error_pure_virtual_called ();
13170 }
13171
13172 static void
13173 base_breakpoint_check_status (bpstat bs)
13174 {
13175 /* Always stop. */
13176 }
13177
13178 /* A "works_in_software_mode" breakpoint_ops method that just internal
13179 errors. */
13180
13181 static int
13182 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13183 {
13184 internal_error_pure_virtual_called ();
13185 }
13186
13187 /* A "resources_needed" breakpoint_ops method that just internal
13188 errors. */
13189
13190 static int
13191 base_breakpoint_resources_needed (const struct bp_location *bl)
13192 {
13193 internal_error_pure_virtual_called ();
13194 }
13195
13196 static enum print_stop_action
13197 base_breakpoint_print_it (bpstat bs)
13198 {
13199 internal_error_pure_virtual_called ();
13200 }
13201
13202 static void
13203 base_breakpoint_print_one_detail (const struct breakpoint *self,
13204 struct ui_out *uiout)
13205 {
13206 /* nothing */
13207 }
13208
13209 static void
13210 base_breakpoint_print_mention (struct breakpoint *b)
13211 {
13212 internal_error_pure_virtual_called ();
13213 }
13214
13215 static void
13216 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13217 {
13218 internal_error_pure_virtual_called ();
13219 }
13220
13221 static void
13222 base_breakpoint_create_sals_from_address (char **arg,
13223 struct linespec_result *canonical,
13224 enum bptype type_wanted,
13225 char *addr_start,
13226 char **copy_arg)
13227 {
13228 internal_error_pure_virtual_called ();
13229 }
13230
13231 static void
13232 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13233 struct linespec_result *c,
13234 char *cond_string,
13235 char *extra_string,
13236 enum bptype type_wanted,
13237 enum bpdisp disposition,
13238 int thread,
13239 int task, int ignore_count,
13240 const struct breakpoint_ops *o,
13241 int from_tty, int enabled,
13242 int internal, unsigned flags)
13243 {
13244 internal_error_pure_virtual_called ();
13245 }
13246
13247 static void
13248 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13249 struct symtabs_and_lines *sals)
13250 {
13251 internal_error_pure_virtual_called ();
13252 }
13253
13254 /* The default 'explains_signal' method. */
13255
13256 static int
13257 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13258 {
13259 return 1;
13260 }
13261
13262 /* The default "after_condition_true" method. */
13263
13264 static void
13265 base_breakpoint_after_condition_true (struct bpstats *bs)
13266 {
13267 /* Nothing to do. */
13268 }
13269
13270 struct breakpoint_ops base_breakpoint_ops =
13271 {
13272 base_breakpoint_dtor,
13273 base_breakpoint_allocate_location,
13274 base_breakpoint_re_set,
13275 base_breakpoint_insert_location,
13276 base_breakpoint_remove_location,
13277 base_breakpoint_breakpoint_hit,
13278 base_breakpoint_check_status,
13279 base_breakpoint_resources_needed,
13280 base_breakpoint_works_in_software_mode,
13281 base_breakpoint_print_it,
13282 NULL,
13283 base_breakpoint_print_one_detail,
13284 base_breakpoint_print_mention,
13285 base_breakpoint_print_recreate,
13286 base_breakpoint_create_sals_from_address,
13287 base_breakpoint_create_breakpoints_sal,
13288 base_breakpoint_decode_linespec,
13289 base_breakpoint_explains_signal,
13290 base_breakpoint_after_condition_true,
13291 };
13292
13293 /* Default breakpoint_ops methods. */
13294
13295 static void
13296 bkpt_re_set (struct breakpoint *b)
13297 {
13298 /* FIXME: is this still reachable? */
13299 if (b->addr_string == NULL)
13300 {
13301 /* Anything without a string can't be re-set. */
13302 delete_breakpoint (b);
13303 return;
13304 }
13305
13306 breakpoint_re_set_default (b);
13307 }
13308
13309 /* Copy SRC's shadow buffer and whatever else we'd set if we actually
13310 inserted DEST, so we can remove it later, in case SRC is removed
13311 first. */
13312
13313 static void
13314 bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13315 const struct bp_target_info *src)
13316 {
13317 dest->shadow_len = src->shadow_len;
13318 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
13319 dest->placed_address = src->placed_address;
13320 dest->placed_size = src->placed_size;
13321 }
13322
13323 static int
13324 bkpt_insert_location (struct bp_location *bl)
13325 {
13326 if (bl->loc_type == bp_loc_hardware_breakpoint)
13327 return target_insert_hw_breakpoint (bl->gdbarch,
13328 &bl->target_info);
13329 else
13330 {
13331 struct bp_target_info *bp_tgt = &bl->target_info;
13332 int ret;
13333 int sss_slot;
13334
13335 /* There is no need to insert a breakpoint if an unconditional
13336 raw/sss breakpoint is already inserted at that location. */
13337 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
13338 bp_tgt->reqstd_address);
13339 if (sss_slot >= 0)
13340 {
13341 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13342
13343 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13344 return 0;
13345 }
13346
13347 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13348 }
13349 }
13350
13351 static int
13352 bkpt_remove_location (struct bp_location *bl)
13353 {
13354 if (bl->loc_type == bp_loc_hardware_breakpoint)
13355 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13356 else
13357 {
13358 struct bp_target_info *bp_tgt = &bl->target_info;
13359 struct address_space *aspace = bp_tgt->placed_address_space;
13360 CORE_ADDR address = bp_tgt->reqstd_address;
13361
13362 /* Only remove the breakpoint if there is no raw/sss breakpoint
13363 still inserted at this location. Otherwise, we would be
13364 effectively disabling the raw/sss breakpoint. */
13365 if (single_step_breakpoint_inserted_here_p (aspace, address))
13366 return 0;
13367
13368 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13369 }
13370 }
13371
13372 static int
13373 bkpt_breakpoint_hit (const struct bp_location *bl,
13374 struct address_space *aspace, CORE_ADDR bp_addr,
13375 const struct target_waitstatus *ws)
13376 {
13377 if (ws->kind != TARGET_WAITKIND_STOPPED
13378 || ws->value.sig != GDB_SIGNAL_TRAP)
13379 return 0;
13380
13381 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13382 aspace, bp_addr))
13383 return 0;
13384
13385 if (overlay_debugging /* unmapped overlay section */
13386 && section_is_overlay (bl->section)
13387 && !section_is_mapped (bl->section))
13388 return 0;
13389
13390 return 1;
13391 }
13392
13393 static int
13394 dprintf_breakpoint_hit (const struct bp_location *bl,
13395 struct address_space *aspace, CORE_ADDR bp_addr,
13396 const struct target_waitstatus *ws)
13397 {
13398 if (dprintf_style == dprintf_style_agent
13399 && target_can_run_breakpoint_commands ())
13400 {
13401 /* An agent-style dprintf never causes a stop. If we see a trap
13402 for this address it must be for a breakpoint that happens to
13403 be set at the same address. */
13404 return 0;
13405 }
13406
13407 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13408 }
13409
13410 static int
13411 bkpt_resources_needed (const struct bp_location *bl)
13412 {
13413 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13414
13415 return 1;
13416 }
13417
13418 static enum print_stop_action
13419 bkpt_print_it (bpstat bs)
13420 {
13421 struct breakpoint *b;
13422 const struct bp_location *bl;
13423 int bp_temp;
13424 struct ui_out *uiout = current_uiout;
13425
13426 gdb_assert (bs->bp_location_at != NULL);
13427
13428 bl = bs->bp_location_at;
13429 b = bs->breakpoint_at;
13430
13431 bp_temp = b->disposition == disp_del;
13432 if (bl->address != bl->requested_address)
13433 breakpoint_adjustment_warning (bl->requested_address,
13434 bl->address,
13435 b->number, 1);
13436 annotate_breakpoint (b->number);
13437 if (bp_temp)
13438 ui_out_text (uiout, "\nTemporary breakpoint ");
13439 else
13440 ui_out_text (uiout, "\nBreakpoint ");
13441 if (ui_out_is_mi_like_p (uiout))
13442 {
13443 ui_out_field_string (uiout, "reason",
13444 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13445 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13446 }
13447 ui_out_field_int (uiout, "bkptno", b->number);
13448 ui_out_text (uiout, ", ");
13449
13450 return PRINT_SRC_AND_LOC;
13451 }
13452
13453 static void
13454 bkpt_print_mention (struct breakpoint *b)
13455 {
13456 if (ui_out_is_mi_like_p (current_uiout))
13457 return;
13458
13459 switch (b->type)
13460 {
13461 case bp_breakpoint:
13462 case bp_gnu_ifunc_resolver:
13463 if (b->disposition == disp_del)
13464 printf_filtered (_("Temporary breakpoint"));
13465 else
13466 printf_filtered (_("Breakpoint"));
13467 printf_filtered (_(" %d"), b->number);
13468 if (b->type == bp_gnu_ifunc_resolver)
13469 printf_filtered (_(" at gnu-indirect-function resolver"));
13470 break;
13471 case bp_hardware_breakpoint:
13472 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13473 break;
13474 case bp_dprintf:
13475 printf_filtered (_("Dprintf %d"), b->number);
13476 break;
13477 }
13478
13479 say_where (b);
13480 }
13481
13482 static void
13483 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13484 {
13485 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13486 fprintf_unfiltered (fp, "tbreak");
13487 else if (tp->type == bp_breakpoint)
13488 fprintf_unfiltered (fp, "break");
13489 else if (tp->type == bp_hardware_breakpoint
13490 && tp->disposition == disp_del)
13491 fprintf_unfiltered (fp, "thbreak");
13492 else if (tp->type == bp_hardware_breakpoint)
13493 fprintf_unfiltered (fp, "hbreak");
13494 else
13495 internal_error (__FILE__, __LINE__,
13496 _("unhandled breakpoint type %d"), (int) tp->type);
13497
13498 fprintf_unfiltered (fp, " %s", tp->addr_string);
13499 print_recreate_thread (tp, fp);
13500 }
13501
13502 static void
13503 bkpt_create_sals_from_address (char **arg,
13504 struct linespec_result *canonical,
13505 enum bptype type_wanted,
13506 char *addr_start, char **copy_arg)
13507 {
13508 create_sals_from_address_default (arg, canonical, type_wanted,
13509 addr_start, copy_arg);
13510 }
13511
13512 static void
13513 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13514 struct linespec_result *canonical,
13515 char *cond_string,
13516 char *extra_string,
13517 enum bptype type_wanted,
13518 enum bpdisp disposition,
13519 int thread,
13520 int task, int ignore_count,
13521 const struct breakpoint_ops *ops,
13522 int from_tty, int enabled,
13523 int internal, unsigned flags)
13524 {
13525 create_breakpoints_sal_default (gdbarch, canonical,
13526 cond_string, extra_string,
13527 type_wanted,
13528 disposition, thread, task,
13529 ignore_count, ops, from_tty,
13530 enabled, internal, flags);
13531 }
13532
13533 static void
13534 bkpt_decode_linespec (struct breakpoint *b, char **s,
13535 struct symtabs_and_lines *sals)
13536 {
13537 decode_linespec_default (b, s, sals);
13538 }
13539
13540 /* Virtual table for internal breakpoints. */
13541
13542 static void
13543 internal_bkpt_re_set (struct breakpoint *b)
13544 {
13545 switch (b->type)
13546 {
13547 /* Delete overlay event and longjmp master breakpoints; they
13548 will be reset later by breakpoint_re_set. */
13549 case bp_overlay_event:
13550 case bp_longjmp_master:
13551 case bp_std_terminate_master:
13552 case bp_exception_master:
13553 delete_breakpoint (b);
13554 break;
13555
13556 /* This breakpoint is special, it's set up when the inferior
13557 starts and we really don't want to touch it. */
13558 case bp_shlib_event:
13559
13560 /* Like bp_shlib_event, this breakpoint type is special. Once
13561 it is set up, we do not want to touch it. */
13562 case bp_thread_event:
13563 break;
13564 }
13565 }
13566
13567 static void
13568 internal_bkpt_check_status (bpstat bs)
13569 {
13570 if (bs->breakpoint_at->type == bp_shlib_event)
13571 {
13572 /* If requested, stop when the dynamic linker notifies GDB of
13573 events. This allows the user to get control and place
13574 breakpoints in initializer routines for dynamically loaded
13575 objects (among other things). */
13576 bs->stop = stop_on_solib_events;
13577 bs->print = stop_on_solib_events;
13578 }
13579 else
13580 bs->stop = 0;
13581 }
13582
13583 static enum print_stop_action
13584 internal_bkpt_print_it (bpstat bs)
13585 {
13586 struct breakpoint *b;
13587
13588 b = bs->breakpoint_at;
13589
13590 switch (b->type)
13591 {
13592 case bp_shlib_event:
13593 /* Did we stop because the user set the stop_on_solib_events
13594 variable? (If so, we report this as a generic, "Stopped due
13595 to shlib event" message.) */
13596 print_solib_event (0);
13597 break;
13598
13599 case bp_thread_event:
13600 /* Not sure how we will get here.
13601 GDB should not stop for these breakpoints. */
13602 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13603 break;
13604
13605 case bp_overlay_event:
13606 /* By analogy with the thread event, GDB should not stop for these. */
13607 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13608 break;
13609
13610 case bp_longjmp_master:
13611 /* These should never be enabled. */
13612 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13613 break;
13614
13615 case bp_std_terminate_master:
13616 /* These should never be enabled. */
13617 printf_filtered (_("std::terminate Master Breakpoint: "
13618 "gdb should not stop!\n"));
13619 break;
13620
13621 case bp_exception_master:
13622 /* These should never be enabled. */
13623 printf_filtered (_("Exception Master Breakpoint: "
13624 "gdb should not stop!\n"));
13625 break;
13626 }
13627
13628 return PRINT_NOTHING;
13629 }
13630
13631 static void
13632 internal_bkpt_print_mention (struct breakpoint *b)
13633 {
13634 /* Nothing to mention. These breakpoints are internal. */
13635 }
13636
13637 /* Virtual table for momentary breakpoints */
13638
13639 static void
13640 momentary_bkpt_re_set (struct breakpoint *b)
13641 {
13642 /* Keep temporary breakpoints, which can be encountered when we step
13643 over a dlopen call and solib_add is resetting the breakpoints.
13644 Otherwise these should have been blown away via the cleanup chain
13645 or by breakpoint_init_inferior when we rerun the executable. */
13646 }
13647
13648 static void
13649 momentary_bkpt_check_status (bpstat bs)
13650 {
13651 /* Nothing. The point of these breakpoints is causing a stop. */
13652 }
13653
13654 static enum print_stop_action
13655 momentary_bkpt_print_it (bpstat bs)
13656 {
13657 struct ui_out *uiout = current_uiout;
13658
13659 if (ui_out_is_mi_like_p (uiout))
13660 {
13661 struct breakpoint *b = bs->breakpoint_at;
13662
13663 switch (b->type)
13664 {
13665 case bp_finish:
13666 ui_out_field_string
13667 (uiout, "reason",
13668 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13669 break;
13670
13671 case bp_until:
13672 ui_out_field_string
13673 (uiout, "reason",
13674 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13675 break;
13676 }
13677 }
13678
13679 return PRINT_UNKNOWN;
13680 }
13681
13682 static void
13683 momentary_bkpt_print_mention (struct breakpoint *b)
13684 {
13685 /* Nothing to mention. These breakpoints are internal. */
13686 }
13687
13688 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13689
13690 It gets cleared already on the removal of the first one of such placed
13691 breakpoints. This is OK as they get all removed altogether. */
13692
13693 static void
13694 longjmp_bkpt_dtor (struct breakpoint *self)
13695 {
13696 struct thread_info *tp = find_thread_id (self->thread);
13697
13698 if (tp)
13699 tp->initiating_frame = null_frame_id;
13700
13701 momentary_breakpoint_ops.dtor (self);
13702 }
13703
13704 /* Specific methods for probe breakpoints. */
13705
13706 static int
13707 bkpt_probe_insert_location (struct bp_location *bl)
13708 {
13709 int v = bkpt_insert_location (bl);
13710
13711 if (v == 0)
13712 {
13713 /* The insertion was successful, now let's set the probe's semaphore
13714 if needed. */
13715 if (bl->probe.probe->pops->set_semaphore != NULL)
13716 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13717 bl->probe.objfile,
13718 bl->gdbarch);
13719 }
13720
13721 return v;
13722 }
13723
13724 static int
13725 bkpt_probe_remove_location (struct bp_location *bl)
13726 {
13727 /* Let's clear the semaphore before removing the location. */
13728 if (bl->probe.probe->pops->clear_semaphore != NULL)
13729 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13730 bl->probe.objfile,
13731 bl->gdbarch);
13732
13733 return bkpt_remove_location (bl);
13734 }
13735
13736 static void
13737 bkpt_probe_create_sals_from_address (char **arg,
13738 struct linespec_result *canonical,
13739 enum bptype type_wanted,
13740 char *addr_start, char **copy_arg)
13741 {
13742 struct linespec_sals lsal;
13743
13744 lsal.sals = parse_probes (arg, canonical);
13745
13746 *copy_arg = xstrdup (canonical->addr_string);
13747 lsal.canonical = xstrdup (*copy_arg);
13748
13749 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13750 }
13751
13752 static void
13753 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13754 struct symtabs_and_lines *sals)
13755 {
13756 *sals = parse_probes (s, NULL);
13757 if (!sals->sals)
13758 error (_("probe not found"));
13759 }
13760
13761 /* The breakpoint_ops structure to be used in tracepoints. */
13762
13763 static void
13764 tracepoint_re_set (struct breakpoint *b)
13765 {
13766 breakpoint_re_set_default (b);
13767 }
13768
13769 static int
13770 tracepoint_breakpoint_hit (const struct bp_location *bl,
13771 struct address_space *aspace, CORE_ADDR bp_addr,
13772 const struct target_waitstatus *ws)
13773 {
13774 /* By definition, the inferior does not report stops at
13775 tracepoints. */
13776 return 0;
13777 }
13778
13779 static void
13780 tracepoint_print_one_detail (const struct breakpoint *self,
13781 struct ui_out *uiout)
13782 {
13783 struct tracepoint *tp = (struct tracepoint *) self;
13784 if (tp->static_trace_marker_id)
13785 {
13786 gdb_assert (self->type == bp_static_tracepoint);
13787
13788 ui_out_text (uiout, "\tmarker id is ");
13789 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13790 tp->static_trace_marker_id);
13791 ui_out_text (uiout, "\n");
13792 }
13793 }
13794
13795 static void
13796 tracepoint_print_mention (struct breakpoint *b)
13797 {
13798 if (ui_out_is_mi_like_p (current_uiout))
13799 return;
13800
13801 switch (b->type)
13802 {
13803 case bp_tracepoint:
13804 printf_filtered (_("Tracepoint"));
13805 printf_filtered (_(" %d"), b->number);
13806 break;
13807 case bp_fast_tracepoint:
13808 printf_filtered (_("Fast tracepoint"));
13809 printf_filtered (_(" %d"), b->number);
13810 break;
13811 case bp_static_tracepoint:
13812 printf_filtered (_("Static tracepoint"));
13813 printf_filtered (_(" %d"), b->number);
13814 break;
13815 default:
13816 internal_error (__FILE__, __LINE__,
13817 _("unhandled tracepoint type %d"), (int) b->type);
13818 }
13819
13820 say_where (b);
13821 }
13822
13823 static void
13824 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13825 {
13826 struct tracepoint *tp = (struct tracepoint *) self;
13827
13828 if (self->type == bp_fast_tracepoint)
13829 fprintf_unfiltered (fp, "ftrace");
13830 if (self->type == bp_static_tracepoint)
13831 fprintf_unfiltered (fp, "strace");
13832 else if (self->type == bp_tracepoint)
13833 fprintf_unfiltered (fp, "trace");
13834 else
13835 internal_error (__FILE__, __LINE__,
13836 _("unhandled tracepoint type %d"), (int) self->type);
13837
13838 fprintf_unfiltered (fp, " %s", self->addr_string);
13839 print_recreate_thread (self, fp);
13840
13841 if (tp->pass_count)
13842 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13843 }
13844
13845 static void
13846 tracepoint_create_sals_from_address (char **arg,
13847 struct linespec_result *canonical,
13848 enum bptype type_wanted,
13849 char *addr_start, char **copy_arg)
13850 {
13851 create_sals_from_address_default (arg, canonical, type_wanted,
13852 addr_start, copy_arg);
13853 }
13854
13855 static void
13856 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13857 struct linespec_result *canonical,
13858 char *cond_string,
13859 char *extra_string,
13860 enum bptype type_wanted,
13861 enum bpdisp disposition,
13862 int thread,
13863 int task, int ignore_count,
13864 const struct breakpoint_ops *ops,
13865 int from_tty, int enabled,
13866 int internal, unsigned flags)
13867 {
13868 create_breakpoints_sal_default (gdbarch, canonical,
13869 cond_string, extra_string,
13870 type_wanted,
13871 disposition, thread, task,
13872 ignore_count, ops, from_tty,
13873 enabled, internal, flags);
13874 }
13875
13876 static void
13877 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13878 struct symtabs_and_lines *sals)
13879 {
13880 decode_linespec_default (b, s, sals);
13881 }
13882
13883 struct breakpoint_ops tracepoint_breakpoint_ops;
13884
13885 /* The breakpoint_ops structure to be use on tracepoints placed in a
13886 static probe. */
13887
13888 static void
13889 tracepoint_probe_create_sals_from_address (char **arg,
13890 struct linespec_result *canonical,
13891 enum bptype type_wanted,
13892 char *addr_start, char **copy_arg)
13893 {
13894 /* We use the same method for breakpoint on probes. */
13895 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13896 addr_start, copy_arg);
13897 }
13898
13899 static void
13900 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13901 struct symtabs_and_lines *sals)
13902 {
13903 /* We use the same method for breakpoint on probes. */
13904 bkpt_probe_decode_linespec (b, s, sals);
13905 }
13906
13907 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13908
13909 /* Dprintf breakpoint_ops methods. */
13910
13911 static void
13912 dprintf_re_set (struct breakpoint *b)
13913 {
13914 breakpoint_re_set_default (b);
13915
13916 /* This breakpoint could have been pending, and be resolved now, and
13917 if so, we should now have the extra string. If we don't, the
13918 dprintf was malformed when created, but we couldn't tell because
13919 we can't extract the extra string until the location is
13920 resolved. */
13921 if (b->loc != NULL && b->extra_string == NULL)
13922 error (_("Format string required"));
13923
13924 /* 1 - connect to target 1, that can run breakpoint commands.
13925 2 - create a dprintf, which resolves fine.
13926 3 - disconnect from target 1
13927 4 - connect to target 2, that can NOT run breakpoint commands.
13928
13929 After steps #3/#4, you'll want the dprintf command list to
13930 be updated, because target 1 and 2 may well return different
13931 answers for target_can_run_breakpoint_commands().
13932 Given absence of finer grained resetting, we get to do
13933 it all the time. */
13934 if (b->extra_string != NULL)
13935 update_dprintf_command_list (b);
13936 }
13937
13938 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13939
13940 static void
13941 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13942 {
13943 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13944 tp->extra_string);
13945 print_recreate_thread (tp, fp);
13946 }
13947
13948 /* Implement the "after_condition_true" breakpoint_ops method for
13949 dprintf.
13950
13951 dprintf's are implemented with regular commands in their command
13952 list, but we run the commands here instead of before presenting the
13953 stop to the user, as dprintf's don't actually cause a stop. This
13954 also makes it so that the commands of multiple dprintfs at the same
13955 address are all handled. */
13956
13957 static void
13958 dprintf_after_condition_true (struct bpstats *bs)
13959 {
13960 struct cleanup *old_chain;
13961 struct bpstats tmp_bs = { NULL };
13962 struct bpstats *tmp_bs_p = &tmp_bs;
13963
13964 /* dprintf's never cause a stop. This wasn't set in the
13965 check_status hook instead because that would make the dprintf's
13966 condition not be evaluated. */
13967 bs->stop = 0;
13968
13969 /* Run the command list here. Take ownership of it instead of
13970 copying. We never want these commands to run later in
13971 bpstat_do_actions, if a breakpoint that causes a stop happens to
13972 be set at same address as this dprintf, or even if running the
13973 commands here throws. */
13974 tmp_bs.commands = bs->commands;
13975 bs->commands = NULL;
13976 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13977
13978 bpstat_do_actions_1 (&tmp_bs_p);
13979
13980 /* 'tmp_bs.commands' will usually be NULL by now, but
13981 bpstat_do_actions_1 may return early without processing the whole
13982 list. */
13983 do_cleanups (old_chain);
13984 }
13985
13986 /* The breakpoint_ops structure to be used on static tracepoints with
13987 markers (`-m'). */
13988
13989 static void
13990 strace_marker_create_sals_from_address (char **arg,
13991 struct linespec_result *canonical,
13992 enum bptype type_wanted,
13993 char *addr_start, char **copy_arg)
13994 {
13995 struct linespec_sals lsal;
13996
13997 lsal.sals = decode_static_tracepoint_spec (arg);
13998
13999 *copy_arg = savestring (addr_start, *arg - addr_start);
14000
14001 canonical->addr_string = xstrdup (*copy_arg);
14002 lsal.canonical = xstrdup (*copy_arg);
14003 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
14004 }
14005
14006 static void
14007 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
14008 struct linespec_result *canonical,
14009 char *cond_string,
14010 char *extra_string,
14011 enum bptype type_wanted,
14012 enum bpdisp disposition,
14013 int thread,
14014 int task, int ignore_count,
14015 const struct breakpoint_ops *ops,
14016 int from_tty, int enabled,
14017 int internal, unsigned flags)
14018 {
14019 int i;
14020 struct linespec_sals *lsal = VEC_index (linespec_sals,
14021 canonical->sals, 0);
14022
14023 /* If the user is creating a static tracepoint by marker id
14024 (strace -m MARKER_ID), then store the sals index, so that
14025 breakpoint_re_set can try to match up which of the newly
14026 found markers corresponds to this one, and, don't try to
14027 expand multiple locations for each sal, given than SALS
14028 already should contain all sals for MARKER_ID. */
14029
14030 for (i = 0; i < lsal->sals.nelts; ++i)
14031 {
14032 struct symtabs_and_lines expanded;
14033 struct tracepoint *tp;
14034 struct cleanup *old_chain;
14035 char *addr_string;
14036
14037 expanded.nelts = 1;
14038 expanded.sals = &lsal->sals.sals[i];
14039
14040 addr_string = xstrdup (canonical->addr_string);
14041 old_chain = make_cleanup (xfree, addr_string);
14042
14043 tp = XCNEW (struct tracepoint);
14044 init_breakpoint_sal (&tp->base, gdbarch, expanded,
14045 addr_string, NULL,
14046 cond_string, extra_string,
14047 type_wanted, disposition,
14048 thread, task, ignore_count, ops,
14049 from_tty, enabled, internal, flags,
14050 canonical->special_display);
14051 /* Given that its possible to have multiple markers with
14052 the same string id, if the user is creating a static
14053 tracepoint by marker id ("strace -m MARKER_ID"), then
14054 store the sals index, so that breakpoint_re_set can
14055 try to match up which of the newly found markers
14056 corresponds to this one */
14057 tp->static_trace_marker_id_idx = i;
14058
14059 install_breakpoint (internal, &tp->base, 0);
14060
14061 discard_cleanups (old_chain);
14062 }
14063 }
14064
14065 static void
14066 strace_marker_decode_linespec (struct breakpoint *b, char **s,
14067 struct symtabs_and_lines *sals)
14068 {
14069 struct tracepoint *tp = (struct tracepoint *) b;
14070
14071 *sals = decode_static_tracepoint_spec (s);
14072 if (sals->nelts > tp->static_trace_marker_id_idx)
14073 {
14074 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14075 sals->nelts = 1;
14076 }
14077 else
14078 error (_("marker %s not found"), tp->static_trace_marker_id);
14079 }
14080
14081 static struct breakpoint_ops strace_marker_breakpoint_ops;
14082
14083 static int
14084 strace_marker_p (struct breakpoint *b)
14085 {
14086 return b->ops == &strace_marker_breakpoint_ops;
14087 }
14088
14089 /* Delete a breakpoint and clean up all traces of it in the data
14090 structures. */
14091
14092 void
14093 delete_breakpoint (struct breakpoint *bpt)
14094 {
14095 struct breakpoint *b;
14096
14097 gdb_assert (bpt != NULL);
14098
14099 /* Has this bp already been deleted? This can happen because
14100 multiple lists can hold pointers to bp's. bpstat lists are
14101 especial culprits.
14102
14103 One example of this happening is a watchpoint's scope bp. When
14104 the scope bp triggers, we notice that the watchpoint is out of
14105 scope, and delete it. We also delete its scope bp. But the
14106 scope bp is marked "auto-deleting", and is already on a bpstat.
14107 That bpstat is then checked for auto-deleting bp's, which are
14108 deleted.
14109
14110 A real solution to this problem might involve reference counts in
14111 bp's, and/or giving them pointers back to their referencing
14112 bpstat's, and teaching delete_breakpoint to only free a bp's
14113 storage when no more references were extent. A cheaper bandaid
14114 was chosen. */
14115 if (bpt->type == bp_none)
14116 return;
14117
14118 /* At least avoid this stale reference until the reference counting
14119 of breakpoints gets resolved. */
14120 if (bpt->related_breakpoint != bpt)
14121 {
14122 struct breakpoint *related;
14123 struct watchpoint *w;
14124
14125 if (bpt->type == bp_watchpoint_scope)
14126 w = (struct watchpoint *) bpt->related_breakpoint;
14127 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
14128 w = (struct watchpoint *) bpt;
14129 else
14130 w = NULL;
14131 if (w != NULL)
14132 watchpoint_del_at_next_stop (w);
14133
14134 /* Unlink bpt from the bpt->related_breakpoint ring. */
14135 for (related = bpt; related->related_breakpoint != bpt;
14136 related = related->related_breakpoint);
14137 related->related_breakpoint = bpt->related_breakpoint;
14138 bpt->related_breakpoint = bpt;
14139 }
14140
14141 /* watch_command_1 creates a watchpoint but only sets its number if
14142 update_watchpoint succeeds in creating its bp_locations. If there's
14143 a problem in that process, we'll be asked to delete the half-created
14144 watchpoint. In that case, don't announce the deletion. */
14145 if (bpt->number)
14146 observer_notify_breakpoint_deleted (bpt);
14147
14148 if (breakpoint_chain == bpt)
14149 breakpoint_chain = bpt->next;
14150
14151 ALL_BREAKPOINTS (b)
14152 if (b->next == bpt)
14153 {
14154 b->next = bpt->next;
14155 break;
14156 }
14157
14158 /* Be sure no bpstat's are pointing at the breakpoint after it's
14159 been freed. */
14160 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
14161 in all threads for now. Note that we cannot just remove bpstats
14162 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14163 commands are associated with the bpstat; if we remove it here,
14164 then the later call to bpstat_do_actions (&stop_bpstat); in
14165 event-top.c won't do anything, and temporary breakpoints with
14166 commands won't work. */
14167
14168 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14169
14170 /* Now that breakpoint is removed from breakpoint list, update the
14171 global location list. This will remove locations that used to
14172 belong to this breakpoint. Do this before freeing the breakpoint
14173 itself, since remove_breakpoint looks at location's owner. It
14174 might be better design to have location completely
14175 self-contained, but it's not the case now. */
14176 update_global_location_list (UGLL_DONT_INSERT);
14177
14178 bpt->ops->dtor (bpt);
14179 /* On the chance that someone will soon try again to delete this
14180 same bp, we mark it as deleted before freeing its storage. */
14181 bpt->type = bp_none;
14182 xfree (bpt);
14183 }
14184
14185 static void
14186 do_delete_breakpoint_cleanup (void *b)
14187 {
14188 delete_breakpoint (b);
14189 }
14190
14191 struct cleanup *
14192 make_cleanup_delete_breakpoint (struct breakpoint *b)
14193 {
14194 return make_cleanup (do_delete_breakpoint_cleanup, b);
14195 }
14196
14197 /* Iterator function to call a user-provided callback function once
14198 for each of B and its related breakpoints. */
14199
14200 static void
14201 iterate_over_related_breakpoints (struct breakpoint *b,
14202 void (*function) (struct breakpoint *,
14203 void *),
14204 void *data)
14205 {
14206 struct breakpoint *related;
14207
14208 related = b;
14209 do
14210 {
14211 struct breakpoint *next;
14212
14213 /* FUNCTION may delete RELATED. */
14214 next = related->related_breakpoint;
14215
14216 if (next == related)
14217 {
14218 /* RELATED is the last ring entry. */
14219 function (related, data);
14220
14221 /* FUNCTION may have deleted it, so we'd never reach back to
14222 B. There's nothing left to do anyway, so just break
14223 out. */
14224 break;
14225 }
14226 else
14227 function (related, data);
14228
14229 related = next;
14230 }
14231 while (related != b);
14232 }
14233
14234 static void
14235 do_delete_breakpoint (struct breakpoint *b, void *ignore)
14236 {
14237 delete_breakpoint (b);
14238 }
14239
14240 /* A callback for map_breakpoint_numbers that calls
14241 delete_breakpoint. */
14242
14243 static void
14244 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14245 {
14246 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14247 }
14248
14249 void
14250 delete_command (char *arg, int from_tty)
14251 {
14252 struct breakpoint *b, *b_tmp;
14253
14254 dont_repeat ();
14255
14256 if (arg == 0)
14257 {
14258 int breaks_to_delete = 0;
14259
14260 /* Delete all breakpoints if no argument. Do not delete
14261 internal breakpoints, these have to be deleted with an
14262 explicit breakpoint number argument. */
14263 ALL_BREAKPOINTS (b)
14264 if (user_breakpoint_p (b))
14265 {
14266 breaks_to_delete = 1;
14267 break;
14268 }
14269
14270 /* Ask user only if there are some breakpoints to delete. */
14271 if (!from_tty
14272 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14273 {
14274 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14275 if (user_breakpoint_p (b))
14276 delete_breakpoint (b);
14277 }
14278 }
14279 else
14280 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14281 }
14282
14283 static int
14284 all_locations_are_pending (struct bp_location *loc)
14285 {
14286 for (; loc; loc = loc->next)
14287 if (!loc->shlib_disabled
14288 && !loc->pspace->executing_startup)
14289 return 0;
14290 return 1;
14291 }
14292
14293 /* Subroutine of update_breakpoint_locations to simplify it.
14294 Return non-zero if multiple fns in list LOC have the same name.
14295 Null names are ignored. */
14296
14297 static int
14298 ambiguous_names_p (struct bp_location *loc)
14299 {
14300 struct bp_location *l;
14301 htab_t htab = htab_create_alloc (13, htab_hash_string,
14302 (int (*) (const void *,
14303 const void *)) streq,
14304 NULL, xcalloc, xfree);
14305
14306 for (l = loc; l != NULL; l = l->next)
14307 {
14308 const char **slot;
14309 const char *name = l->function_name;
14310
14311 /* Allow for some names to be NULL, ignore them. */
14312 if (name == NULL)
14313 continue;
14314
14315 slot = (const char **) htab_find_slot (htab, (const void *) name,
14316 INSERT);
14317 /* NOTE: We can assume slot != NULL here because xcalloc never
14318 returns NULL. */
14319 if (*slot != NULL)
14320 {
14321 htab_delete (htab);
14322 return 1;
14323 }
14324 *slot = name;
14325 }
14326
14327 htab_delete (htab);
14328 return 0;
14329 }
14330
14331 /* When symbols change, it probably means the sources changed as well,
14332 and it might mean the static tracepoint markers are no longer at
14333 the same address or line numbers they used to be at last we
14334 checked. Losing your static tracepoints whenever you rebuild is
14335 undesirable. This function tries to resync/rematch gdb static
14336 tracepoints with the markers on the target, for static tracepoints
14337 that have not been set by marker id. Static tracepoint that have
14338 been set by marker id are reset by marker id in breakpoint_re_set.
14339 The heuristic is:
14340
14341 1) For a tracepoint set at a specific address, look for a marker at
14342 the old PC. If one is found there, assume to be the same marker.
14343 If the name / string id of the marker found is different from the
14344 previous known name, assume that means the user renamed the marker
14345 in the sources, and output a warning.
14346
14347 2) For a tracepoint set at a given line number, look for a marker
14348 at the new address of the old line number. If one is found there,
14349 assume to be the same marker. If the name / string id of the
14350 marker found is different from the previous known name, assume that
14351 means the user renamed the marker in the sources, and output a
14352 warning.
14353
14354 3) If a marker is no longer found at the same address or line, it
14355 may mean the marker no longer exists. But it may also just mean
14356 the code changed a bit. Maybe the user added a few lines of code
14357 that made the marker move up or down (in line number terms). Ask
14358 the target for info about the marker with the string id as we knew
14359 it. If found, update line number and address in the matching
14360 static tracepoint. This will get confused if there's more than one
14361 marker with the same ID (possible in UST, although unadvised
14362 precisely because it confuses tools). */
14363
14364 static struct symtab_and_line
14365 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14366 {
14367 struct tracepoint *tp = (struct tracepoint *) b;
14368 struct static_tracepoint_marker marker;
14369 CORE_ADDR pc;
14370
14371 pc = sal.pc;
14372 if (sal.line)
14373 find_line_pc (sal.symtab, sal.line, &pc);
14374
14375 if (target_static_tracepoint_marker_at (pc, &marker))
14376 {
14377 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14378 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14379 b->number,
14380 tp->static_trace_marker_id, marker.str_id);
14381
14382 xfree (tp->static_trace_marker_id);
14383 tp->static_trace_marker_id = xstrdup (marker.str_id);
14384 release_static_tracepoint_marker (&marker);
14385
14386 return sal;
14387 }
14388
14389 /* Old marker wasn't found on target at lineno. Try looking it up
14390 by string ID. */
14391 if (!sal.explicit_pc
14392 && sal.line != 0
14393 && sal.symtab != NULL
14394 && tp->static_trace_marker_id != NULL)
14395 {
14396 VEC(static_tracepoint_marker_p) *markers;
14397
14398 markers
14399 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14400
14401 if (!VEC_empty(static_tracepoint_marker_p, markers))
14402 {
14403 struct symtab_and_line sal2;
14404 struct symbol *sym;
14405 struct static_tracepoint_marker *tpmarker;
14406 struct ui_out *uiout = current_uiout;
14407
14408 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14409
14410 xfree (tp->static_trace_marker_id);
14411 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14412
14413 warning (_("marker for static tracepoint %d (%s) not "
14414 "found at previous line number"),
14415 b->number, tp->static_trace_marker_id);
14416
14417 init_sal (&sal2);
14418
14419 sal2.pc = tpmarker->address;
14420
14421 sal2 = find_pc_line (tpmarker->address, 0);
14422 sym = find_pc_sect_function (tpmarker->address, NULL);
14423 ui_out_text (uiout, "Now in ");
14424 if (sym)
14425 {
14426 ui_out_field_string (uiout, "func",
14427 SYMBOL_PRINT_NAME (sym));
14428 ui_out_text (uiout, " at ");
14429 }
14430 ui_out_field_string (uiout, "file",
14431 symtab_to_filename_for_display (sal2.symtab));
14432 ui_out_text (uiout, ":");
14433
14434 if (ui_out_is_mi_like_p (uiout))
14435 {
14436 const char *fullname = symtab_to_fullname (sal2.symtab);
14437
14438 ui_out_field_string (uiout, "fullname", fullname);
14439 }
14440
14441 ui_out_field_int (uiout, "line", sal2.line);
14442 ui_out_text (uiout, "\n");
14443
14444 b->loc->line_number = sal2.line;
14445 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14446
14447 xfree (b->addr_string);
14448 b->addr_string = xstrprintf ("%s:%d",
14449 symtab_to_filename_for_display (sal2.symtab),
14450 b->loc->line_number);
14451
14452 /* Might be nice to check if function changed, and warn if
14453 so. */
14454
14455 release_static_tracepoint_marker (tpmarker);
14456 }
14457 }
14458 return sal;
14459 }
14460
14461 /* Returns 1 iff locations A and B are sufficiently same that
14462 we don't need to report breakpoint as changed. */
14463
14464 static int
14465 locations_are_equal (struct bp_location *a, struct bp_location *b)
14466 {
14467 while (a && b)
14468 {
14469 if (a->address != b->address)
14470 return 0;
14471
14472 if (a->shlib_disabled != b->shlib_disabled)
14473 return 0;
14474
14475 if (a->enabled != b->enabled)
14476 return 0;
14477
14478 a = a->next;
14479 b = b->next;
14480 }
14481
14482 if ((a == NULL) != (b == NULL))
14483 return 0;
14484
14485 return 1;
14486 }
14487
14488 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14489 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14490 a ranged breakpoint. */
14491
14492 void
14493 update_breakpoint_locations (struct breakpoint *b,
14494 struct symtabs_and_lines sals,
14495 struct symtabs_and_lines sals_end)
14496 {
14497 int i;
14498 struct bp_location *existing_locations = b->loc;
14499
14500 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14501 {
14502 /* Ranged breakpoints have only one start location and one end
14503 location. */
14504 b->enable_state = bp_disabled;
14505 update_global_location_list (UGLL_MAY_INSERT);
14506 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14507 "multiple locations found\n"),
14508 b->number);
14509 return;
14510 }
14511
14512 /* If there's no new locations, and all existing locations are
14513 pending, don't do anything. This optimizes the common case where
14514 all locations are in the same shared library, that was unloaded.
14515 We'd like to retain the location, so that when the library is
14516 loaded again, we don't loose the enabled/disabled status of the
14517 individual locations. */
14518 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14519 return;
14520
14521 b->loc = NULL;
14522
14523 for (i = 0; i < sals.nelts; ++i)
14524 {
14525 struct bp_location *new_loc;
14526
14527 switch_to_program_space_and_thread (sals.sals[i].pspace);
14528
14529 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14530
14531 /* Reparse conditions, they might contain references to the
14532 old symtab. */
14533 if (b->cond_string != NULL)
14534 {
14535 const char *s;
14536 volatile struct gdb_exception e;
14537
14538 s = b->cond_string;
14539 TRY_CATCH (e, RETURN_MASK_ERROR)
14540 {
14541 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14542 block_for_pc (sals.sals[i].pc),
14543 0);
14544 }
14545 if (e.reason < 0)
14546 {
14547 warning (_("failed to reevaluate condition "
14548 "for breakpoint %d: %s"),
14549 b->number, e.message);
14550 new_loc->enabled = 0;
14551 }
14552 }
14553
14554 if (sals_end.nelts)
14555 {
14556 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14557
14558 new_loc->length = end - sals.sals[0].pc + 1;
14559 }
14560 }
14561
14562 /* Update locations of permanent breakpoints. */
14563 if (b->enable_state == bp_permanent)
14564 make_breakpoint_permanent (b);
14565
14566 /* If possible, carry over 'disable' status from existing
14567 breakpoints. */
14568 {
14569 struct bp_location *e = existing_locations;
14570 /* If there are multiple breakpoints with the same function name,
14571 e.g. for inline functions, comparing function names won't work.
14572 Instead compare pc addresses; this is just a heuristic as things
14573 may have moved, but in practice it gives the correct answer
14574 often enough until a better solution is found. */
14575 int have_ambiguous_names = ambiguous_names_p (b->loc);
14576
14577 for (; e; e = e->next)
14578 {
14579 if (!e->enabled && e->function_name)
14580 {
14581 struct bp_location *l = b->loc;
14582 if (have_ambiguous_names)
14583 {
14584 for (; l; l = l->next)
14585 if (breakpoint_locations_match (e, l))
14586 {
14587 l->enabled = 0;
14588 break;
14589 }
14590 }
14591 else
14592 {
14593 for (; l; l = l->next)
14594 if (l->function_name
14595 && strcmp (e->function_name, l->function_name) == 0)
14596 {
14597 l->enabled = 0;
14598 break;
14599 }
14600 }
14601 }
14602 }
14603 }
14604
14605 if (!locations_are_equal (existing_locations, b->loc))
14606 observer_notify_breakpoint_modified (b);
14607
14608 update_global_location_list (UGLL_MAY_INSERT);
14609 }
14610
14611 /* Find the SaL locations corresponding to the given ADDR_STRING.
14612 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14613
14614 static struct symtabs_and_lines
14615 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14616 {
14617 char *s;
14618 struct symtabs_and_lines sals = {0};
14619 volatile struct gdb_exception e;
14620
14621 gdb_assert (b->ops != NULL);
14622 s = addr_string;
14623
14624 TRY_CATCH (e, RETURN_MASK_ERROR)
14625 {
14626 b->ops->decode_linespec (b, &s, &sals);
14627 }
14628 if (e.reason < 0)
14629 {
14630 int not_found_and_ok = 0;
14631 /* For pending breakpoints, it's expected that parsing will
14632 fail until the right shared library is loaded. User has
14633 already told to create pending breakpoints and don't need
14634 extra messages. If breakpoint is in bp_shlib_disabled
14635 state, then user already saw the message about that
14636 breakpoint being disabled, and don't want to see more
14637 errors. */
14638 if (e.error == NOT_FOUND_ERROR
14639 && (b->condition_not_parsed
14640 || (b->loc && b->loc->shlib_disabled)
14641 || (b->loc && b->loc->pspace->executing_startup)
14642 || b->enable_state == bp_disabled))
14643 not_found_and_ok = 1;
14644
14645 if (!not_found_and_ok)
14646 {
14647 /* We surely don't want to warn about the same breakpoint
14648 10 times. One solution, implemented here, is disable
14649 the breakpoint on error. Another solution would be to
14650 have separate 'warning emitted' flag. Since this
14651 happens only when a binary has changed, I don't know
14652 which approach is better. */
14653 b->enable_state = bp_disabled;
14654 throw_exception (e);
14655 }
14656 }
14657
14658 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14659 {
14660 int i;
14661
14662 for (i = 0; i < sals.nelts; ++i)
14663 resolve_sal_pc (&sals.sals[i]);
14664 if (b->condition_not_parsed && s && s[0])
14665 {
14666 char *cond_string, *extra_string;
14667 int thread, task;
14668
14669 find_condition_and_thread (s, sals.sals[0].pc,
14670 &cond_string, &thread, &task,
14671 &extra_string);
14672 if (cond_string)
14673 b->cond_string = cond_string;
14674 b->thread = thread;
14675 b->task = task;
14676 if (extra_string)
14677 b->extra_string = extra_string;
14678 b->condition_not_parsed = 0;
14679 }
14680
14681 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14682 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14683
14684 *found = 1;
14685 }
14686 else
14687 *found = 0;
14688
14689 return sals;
14690 }
14691
14692 /* The default re_set method, for typical hardware or software
14693 breakpoints. Reevaluate the breakpoint and recreate its
14694 locations. */
14695
14696 static void
14697 breakpoint_re_set_default (struct breakpoint *b)
14698 {
14699 int found;
14700 struct symtabs_and_lines sals, sals_end;
14701 struct symtabs_and_lines expanded = {0};
14702 struct symtabs_and_lines expanded_end = {0};
14703
14704 sals = addr_string_to_sals (b, b->addr_string, &found);
14705 if (found)
14706 {
14707 make_cleanup (xfree, sals.sals);
14708 expanded = sals;
14709 }
14710
14711 if (b->addr_string_range_end)
14712 {
14713 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14714 if (found)
14715 {
14716 make_cleanup (xfree, sals_end.sals);
14717 expanded_end = sals_end;
14718 }
14719 }
14720
14721 update_breakpoint_locations (b, expanded, expanded_end);
14722 }
14723
14724 /* Default method for creating SALs from an address string. It basically
14725 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14726
14727 static void
14728 create_sals_from_address_default (char **arg,
14729 struct linespec_result *canonical,
14730 enum bptype type_wanted,
14731 char *addr_start, char **copy_arg)
14732 {
14733 parse_breakpoint_sals (arg, canonical);
14734 }
14735
14736 /* Call create_breakpoints_sal for the given arguments. This is the default
14737 function for the `create_breakpoints_sal' method of
14738 breakpoint_ops. */
14739
14740 static void
14741 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14742 struct linespec_result *canonical,
14743 char *cond_string,
14744 char *extra_string,
14745 enum bptype type_wanted,
14746 enum bpdisp disposition,
14747 int thread,
14748 int task, int ignore_count,
14749 const struct breakpoint_ops *ops,
14750 int from_tty, int enabled,
14751 int internal, unsigned flags)
14752 {
14753 create_breakpoints_sal (gdbarch, canonical, cond_string,
14754 extra_string,
14755 type_wanted, disposition,
14756 thread, task, ignore_count, ops, from_tty,
14757 enabled, internal, flags);
14758 }
14759
14760 /* Decode the line represented by S by calling decode_line_full. This is the
14761 default function for the `decode_linespec' method of breakpoint_ops. */
14762
14763 static void
14764 decode_linespec_default (struct breakpoint *b, char **s,
14765 struct symtabs_and_lines *sals)
14766 {
14767 struct linespec_result canonical;
14768
14769 init_linespec_result (&canonical);
14770 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14771 (struct symtab *) NULL, 0,
14772 &canonical, multiple_symbols_all,
14773 b->filter);
14774
14775 /* We should get 0 or 1 resulting SALs. */
14776 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14777
14778 if (VEC_length (linespec_sals, canonical.sals) > 0)
14779 {
14780 struct linespec_sals *lsal;
14781
14782 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14783 *sals = lsal->sals;
14784 /* Arrange it so the destructor does not free the
14785 contents. */
14786 lsal->sals.sals = NULL;
14787 }
14788
14789 destroy_linespec_result (&canonical);
14790 }
14791
14792 /* Prepare the global context for a re-set of breakpoint B. */
14793
14794 static struct cleanup *
14795 prepare_re_set_context (struct breakpoint *b)
14796 {
14797 struct cleanup *cleanups;
14798
14799 input_radix = b->input_radix;
14800 cleanups = save_current_space_and_thread ();
14801 if (b->pspace != NULL)
14802 switch_to_program_space_and_thread (b->pspace);
14803 set_language (b->language);
14804
14805 return cleanups;
14806 }
14807
14808 /* Reset a breakpoint given it's struct breakpoint * BINT.
14809 The value we return ends up being the return value from catch_errors.
14810 Unused in this case. */
14811
14812 static int
14813 breakpoint_re_set_one (void *bint)
14814 {
14815 /* Get past catch_errs. */
14816 struct breakpoint *b = (struct breakpoint *) bint;
14817 struct cleanup *cleanups;
14818
14819 cleanups = prepare_re_set_context (b);
14820 b->ops->re_set (b);
14821 do_cleanups (cleanups);
14822 return 0;
14823 }
14824
14825 /* Re-set all breakpoints after symbols have been re-loaded. */
14826 void
14827 breakpoint_re_set (void)
14828 {
14829 struct breakpoint *b, *b_tmp;
14830 enum language save_language;
14831 int save_input_radix;
14832 struct cleanup *old_chain;
14833
14834 save_language = current_language->la_language;
14835 save_input_radix = input_radix;
14836 old_chain = save_current_program_space ();
14837
14838 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14839 {
14840 /* Format possible error msg. */
14841 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14842 b->number);
14843 struct cleanup *cleanups = make_cleanup (xfree, message);
14844 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14845 do_cleanups (cleanups);
14846 }
14847 set_language (save_language);
14848 input_radix = save_input_radix;
14849
14850 jit_breakpoint_re_set ();
14851
14852 do_cleanups (old_chain);
14853
14854 create_overlay_event_breakpoint ();
14855 create_longjmp_master_breakpoint ();
14856 create_std_terminate_master_breakpoint ();
14857 create_exception_master_breakpoint ();
14858 }
14859 \f
14860 /* Reset the thread number of this breakpoint:
14861
14862 - If the breakpoint is for all threads, leave it as-is.
14863 - Else, reset it to the current thread for inferior_ptid. */
14864 void
14865 breakpoint_re_set_thread (struct breakpoint *b)
14866 {
14867 if (b->thread != -1)
14868 {
14869 if (in_thread_list (inferior_ptid))
14870 b->thread = pid_to_thread_id (inferior_ptid);
14871
14872 /* We're being called after following a fork. The new fork is
14873 selected as current, and unless this was a vfork will have a
14874 different program space from the original thread. Reset that
14875 as well. */
14876 b->loc->pspace = current_program_space;
14877 }
14878 }
14879
14880 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14881 If from_tty is nonzero, it prints a message to that effect,
14882 which ends with a period (no newline). */
14883
14884 void
14885 set_ignore_count (int bptnum, int count, int from_tty)
14886 {
14887 struct breakpoint *b;
14888
14889 if (count < 0)
14890 count = 0;
14891
14892 ALL_BREAKPOINTS (b)
14893 if (b->number == bptnum)
14894 {
14895 if (is_tracepoint (b))
14896 {
14897 if (from_tty && count != 0)
14898 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14899 bptnum);
14900 return;
14901 }
14902
14903 b->ignore_count = count;
14904 if (from_tty)
14905 {
14906 if (count == 0)
14907 printf_filtered (_("Will stop next time "
14908 "breakpoint %d is reached."),
14909 bptnum);
14910 else if (count == 1)
14911 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14912 bptnum);
14913 else
14914 printf_filtered (_("Will ignore next %d "
14915 "crossings of breakpoint %d."),
14916 count, bptnum);
14917 }
14918 observer_notify_breakpoint_modified (b);
14919 return;
14920 }
14921
14922 error (_("No breakpoint number %d."), bptnum);
14923 }
14924
14925 /* Command to set ignore-count of breakpoint N to COUNT. */
14926
14927 static void
14928 ignore_command (char *args, int from_tty)
14929 {
14930 char *p = args;
14931 int num;
14932
14933 if (p == 0)
14934 error_no_arg (_("a breakpoint number"));
14935
14936 num = get_number (&p);
14937 if (num == 0)
14938 error (_("bad breakpoint number: '%s'"), args);
14939 if (*p == 0)
14940 error (_("Second argument (specified ignore-count) is missing."));
14941
14942 set_ignore_count (num,
14943 longest_to_int (value_as_long (parse_and_eval (p))),
14944 from_tty);
14945 if (from_tty)
14946 printf_filtered ("\n");
14947 }
14948 \f
14949 /* Call FUNCTION on each of the breakpoints
14950 whose numbers are given in ARGS. */
14951
14952 static void
14953 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14954 void *),
14955 void *data)
14956 {
14957 int num;
14958 struct breakpoint *b, *tmp;
14959 int match;
14960 struct get_number_or_range_state state;
14961
14962 if (args == 0)
14963 error_no_arg (_("one or more breakpoint numbers"));
14964
14965 init_number_or_range (&state, args);
14966
14967 while (!state.finished)
14968 {
14969 const char *p = state.string;
14970
14971 match = 0;
14972
14973 num = get_number_or_range (&state);
14974 if (num == 0)
14975 {
14976 warning (_("bad breakpoint number at or near '%s'"), p);
14977 }
14978 else
14979 {
14980 ALL_BREAKPOINTS_SAFE (b, tmp)
14981 if (b->number == num)
14982 {
14983 match = 1;
14984 function (b, data);
14985 break;
14986 }
14987 if (match == 0)
14988 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14989 }
14990 }
14991 }
14992
14993 static struct bp_location *
14994 find_location_by_number (char *number)
14995 {
14996 char *dot = strchr (number, '.');
14997 char *p1;
14998 int bp_num;
14999 int loc_num;
15000 struct breakpoint *b;
15001 struct bp_location *loc;
15002
15003 *dot = '\0';
15004
15005 p1 = number;
15006 bp_num = get_number (&p1);
15007 if (bp_num == 0)
15008 error (_("Bad breakpoint number '%s'"), number);
15009
15010 ALL_BREAKPOINTS (b)
15011 if (b->number == bp_num)
15012 {
15013 break;
15014 }
15015
15016 if (!b || b->number != bp_num)
15017 error (_("Bad breakpoint number '%s'"), number);
15018
15019 p1 = dot+1;
15020 loc_num = get_number (&p1);
15021 if (loc_num == 0)
15022 error (_("Bad breakpoint location number '%s'"), number);
15023
15024 --loc_num;
15025 loc = b->loc;
15026 for (;loc_num && loc; --loc_num, loc = loc->next)
15027 ;
15028 if (!loc)
15029 error (_("Bad breakpoint location number '%s'"), dot+1);
15030
15031 return loc;
15032 }
15033
15034
15035 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
15036 If from_tty is nonzero, it prints a message to that effect,
15037 which ends with a period (no newline). */
15038
15039 void
15040 disable_breakpoint (struct breakpoint *bpt)
15041 {
15042 /* Never disable a watchpoint scope breakpoint; we want to
15043 hit them when we leave scope so we can delete both the
15044 watchpoint and its scope breakpoint at that time. */
15045 if (bpt->type == bp_watchpoint_scope)
15046 return;
15047
15048 /* You can't disable permanent breakpoints. */
15049 if (bpt->enable_state == bp_permanent)
15050 return;
15051
15052 bpt->enable_state = bp_disabled;
15053
15054 /* Mark breakpoint locations modified. */
15055 mark_breakpoint_modified (bpt);
15056
15057 if (target_supports_enable_disable_tracepoint ()
15058 && current_trace_status ()->running && is_tracepoint (bpt))
15059 {
15060 struct bp_location *location;
15061
15062 for (location = bpt->loc; location; location = location->next)
15063 target_disable_tracepoint (location);
15064 }
15065
15066 update_global_location_list (UGLL_DONT_INSERT);
15067
15068 observer_notify_breakpoint_modified (bpt);
15069 }
15070
15071 /* A callback for iterate_over_related_breakpoints. */
15072
15073 static void
15074 do_disable_breakpoint (struct breakpoint *b, void *ignore)
15075 {
15076 disable_breakpoint (b);
15077 }
15078
15079 /* A callback for map_breakpoint_numbers that calls
15080 disable_breakpoint. */
15081
15082 static void
15083 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15084 {
15085 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
15086 }
15087
15088 static void
15089 disable_command (char *args, int from_tty)
15090 {
15091 if (args == 0)
15092 {
15093 struct breakpoint *bpt;
15094
15095 ALL_BREAKPOINTS (bpt)
15096 if (user_breakpoint_p (bpt))
15097 disable_breakpoint (bpt);
15098 }
15099 else
15100 {
15101 char *num = extract_arg (&args);
15102
15103 while (num)
15104 {
15105 if (strchr (num, '.'))
15106 {
15107 struct bp_location *loc = find_location_by_number (num);
15108
15109 if (loc)
15110 {
15111 if (loc->enabled)
15112 {
15113 loc->enabled = 0;
15114 mark_breakpoint_location_modified (loc);
15115 }
15116 if (target_supports_enable_disable_tracepoint ()
15117 && current_trace_status ()->running && loc->owner
15118 && is_tracepoint (loc->owner))
15119 target_disable_tracepoint (loc);
15120 }
15121 update_global_location_list (UGLL_DONT_INSERT);
15122 }
15123 else
15124 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15125 num = extract_arg (&args);
15126 }
15127 }
15128 }
15129
15130 static void
15131 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15132 int count)
15133 {
15134 int target_resources_ok;
15135
15136 if (bpt->type == bp_hardware_breakpoint)
15137 {
15138 int i;
15139 i = hw_breakpoint_used_count ();
15140 target_resources_ok =
15141 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
15142 i + 1, 0);
15143 if (target_resources_ok == 0)
15144 error (_("No hardware breakpoint support in the target."));
15145 else if (target_resources_ok < 0)
15146 error (_("Hardware breakpoints used exceeds limit."));
15147 }
15148
15149 if (is_watchpoint (bpt))
15150 {
15151 /* Initialize it just to avoid a GCC false warning. */
15152 enum enable_state orig_enable_state = 0;
15153 volatile struct gdb_exception e;
15154
15155 TRY_CATCH (e, RETURN_MASK_ALL)
15156 {
15157 struct watchpoint *w = (struct watchpoint *) bpt;
15158
15159 orig_enable_state = bpt->enable_state;
15160 bpt->enable_state = bp_enabled;
15161 update_watchpoint (w, 1 /* reparse */);
15162 }
15163 if (e.reason < 0)
15164 {
15165 bpt->enable_state = orig_enable_state;
15166 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15167 bpt->number);
15168 return;
15169 }
15170 }
15171
15172 if (bpt->enable_state != bp_permanent)
15173 bpt->enable_state = bp_enabled;
15174
15175 bpt->enable_state = bp_enabled;
15176
15177 /* Mark breakpoint locations modified. */
15178 mark_breakpoint_modified (bpt);
15179
15180 if (target_supports_enable_disable_tracepoint ()
15181 && current_trace_status ()->running && is_tracepoint (bpt))
15182 {
15183 struct bp_location *location;
15184
15185 for (location = bpt->loc; location; location = location->next)
15186 target_enable_tracepoint (location);
15187 }
15188
15189 bpt->disposition = disposition;
15190 bpt->enable_count = count;
15191 update_global_location_list (UGLL_MAY_INSERT);
15192
15193 observer_notify_breakpoint_modified (bpt);
15194 }
15195
15196
15197 void
15198 enable_breakpoint (struct breakpoint *bpt)
15199 {
15200 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15201 }
15202
15203 static void
15204 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15205 {
15206 enable_breakpoint (bpt);
15207 }
15208
15209 /* A callback for map_breakpoint_numbers that calls
15210 enable_breakpoint. */
15211
15212 static void
15213 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15214 {
15215 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15216 }
15217
15218 /* The enable command enables the specified breakpoints (or all defined
15219 breakpoints) so they once again become (or continue to be) effective
15220 in stopping the inferior. */
15221
15222 static void
15223 enable_command (char *args, int from_tty)
15224 {
15225 if (args == 0)
15226 {
15227 struct breakpoint *bpt;
15228
15229 ALL_BREAKPOINTS (bpt)
15230 if (user_breakpoint_p (bpt))
15231 enable_breakpoint (bpt);
15232 }
15233 else
15234 {
15235 char *num = extract_arg (&args);
15236
15237 while (num)
15238 {
15239 if (strchr (num, '.'))
15240 {
15241 struct bp_location *loc = find_location_by_number (num);
15242
15243 if (loc)
15244 {
15245 if (!loc->enabled)
15246 {
15247 loc->enabled = 1;
15248 mark_breakpoint_location_modified (loc);
15249 }
15250 if (target_supports_enable_disable_tracepoint ()
15251 && current_trace_status ()->running && loc->owner
15252 && is_tracepoint (loc->owner))
15253 target_enable_tracepoint (loc);
15254 }
15255 update_global_location_list (UGLL_MAY_INSERT);
15256 }
15257 else
15258 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15259 num = extract_arg (&args);
15260 }
15261 }
15262 }
15263
15264 /* This struct packages up disposition data for application to multiple
15265 breakpoints. */
15266
15267 struct disp_data
15268 {
15269 enum bpdisp disp;
15270 int count;
15271 };
15272
15273 static void
15274 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15275 {
15276 struct disp_data disp_data = *(struct disp_data *) arg;
15277
15278 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15279 }
15280
15281 static void
15282 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15283 {
15284 struct disp_data disp = { disp_disable, 1 };
15285
15286 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15287 }
15288
15289 static void
15290 enable_once_command (char *args, int from_tty)
15291 {
15292 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15293 }
15294
15295 static void
15296 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15297 {
15298 struct disp_data disp = { disp_disable, *(int *) countptr };
15299
15300 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15301 }
15302
15303 static void
15304 enable_count_command (char *args, int from_tty)
15305 {
15306 int count = get_number (&args);
15307
15308 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15309 }
15310
15311 static void
15312 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15313 {
15314 struct disp_data disp = { disp_del, 1 };
15315
15316 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15317 }
15318
15319 static void
15320 enable_delete_command (char *args, int from_tty)
15321 {
15322 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15323 }
15324 \f
15325 static void
15326 set_breakpoint_cmd (char *args, int from_tty)
15327 {
15328 }
15329
15330 static void
15331 show_breakpoint_cmd (char *args, int from_tty)
15332 {
15333 }
15334
15335 /* Invalidate last known value of any hardware watchpoint if
15336 the memory which that value represents has been written to by
15337 GDB itself. */
15338
15339 static void
15340 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15341 CORE_ADDR addr, ssize_t len,
15342 const bfd_byte *data)
15343 {
15344 struct breakpoint *bp;
15345
15346 ALL_BREAKPOINTS (bp)
15347 if (bp->enable_state == bp_enabled
15348 && bp->type == bp_hardware_watchpoint)
15349 {
15350 struct watchpoint *wp = (struct watchpoint *) bp;
15351
15352 if (wp->val_valid && wp->val)
15353 {
15354 struct bp_location *loc;
15355
15356 for (loc = bp->loc; loc != NULL; loc = loc->next)
15357 if (loc->loc_type == bp_loc_hardware_watchpoint
15358 && loc->address + loc->length > addr
15359 && addr + len > loc->address)
15360 {
15361 value_free (wp->val);
15362 wp->val = NULL;
15363 wp->val_valid = 0;
15364 }
15365 }
15366 }
15367 }
15368
15369 /* Create and insert a raw software breakpoint at PC. Return an
15370 identifier, which should be used to remove the breakpoint later.
15371 In general, places which call this should be using something on the
15372 breakpoint chain instead; this function should be eliminated
15373 someday. */
15374
15375 void *
15376 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15377 struct address_space *aspace, CORE_ADDR pc)
15378 {
15379 struct bp_target_info *bp_tgt;
15380 struct bp_location *bl;
15381
15382 bp_tgt = XCNEW (struct bp_target_info);
15383
15384 bp_tgt->placed_address_space = aspace;
15385 bp_tgt->reqstd_address = pc;
15386
15387 /* If an unconditional non-raw breakpoint is already inserted at
15388 that location, there's no need to insert another. However, with
15389 target-side evaluation of breakpoint conditions, if the
15390 breakpoint that is currently inserted on the target is
15391 conditional, we need to make it unconditional. Note that a
15392 breakpoint with target-side commands is not reported even if
15393 unconditional, so we need to remove the commands from the target
15394 as well. */
15395 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15396 if (bl != NULL
15397 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15398 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15399 {
15400 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15401 return bp_tgt;
15402 }
15403
15404 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
15405 {
15406 /* Could not insert the breakpoint. */
15407 xfree (bp_tgt);
15408 return NULL;
15409 }
15410
15411 return bp_tgt;
15412 }
15413
15414 /* Remove a breakpoint BP inserted by
15415 deprecated_insert_raw_breakpoint. */
15416
15417 int
15418 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
15419 {
15420 struct bp_target_info *bp_tgt = bp;
15421 struct address_space *aspace = bp_tgt->placed_address_space;
15422 CORE_ADDR address = bp_tgt->reqstd_address;
15423 struct bp_location *bl;
15424 int ret;
15425
15426 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15427
15428 /* Only remove the raw breakpoint if there are no other non-raw
15429 breakpoints still inserted at this location. Otherwise, we would
15430 be effectively disabling those breakpoints. */
15431 if (bl == NULL)
15432 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15433 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15434 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15435 {
15436 /* The target is evaluating conditions, and when we inserted the
15437 software single-step breakpoint, we had made the breakpoint
15438 unconditional and command-less on the target side. Reinsert
15439 to restore the conditions/commands. */
15440 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15441 }
15442 else
15443 ret = 0;
15444
15445 xfree (bp_tgt);
15446
15447 return ret;
15448 }
15449
15450 /* Create and insert a breakpoint for software single step. */
15451
15452 void
15453 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15454 struct address_space *aspace,
15455 CORE_ADDR next_pc)
15456 {
15457 void **bpt_p;
15458
15459 if (single_step_breakpoints[0] == NULL)
15460 {
15461 bpt_p = &single_step_breakpoints[0];
15462 single_step_gdbarch[0] = gdbarch;
15463 }
15464 else
15465 {
15466 gdb_assert (single_step_breakpoints[1] == NULL);
15467 bpt_p = &single_step_breakpoints[1];
15468 single_step_gdbarch[1] = gdbarch;
15469 }
15470
15471 /* NOTE drow/2006-04-11: A future improvement to this function would
15472 be to only create the breakpoints once, and actually put them on
15473 the breakpoint chain. That would let us use set_raw_breakpoint.
15474 We could adjust the addresses each time they were needed. Doing
15475 this requires corresponding changes elsewhere where single step
15476 breakpoints are handled, however. So, for now, we use this. */
15477
15478 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15479 if (*bpt_p == NULL)
15480 error (_("Could not insert single-step breakpoint at %s"),
15481 paddress (gdbarch, next_pc));
15482 }
15483
15484 /* Check if the breakpoints used for software single stepping
15485 were inserted or not. */
15486
15487 int
15488 single_step_breakpoints_inserted (void)
15489 {
15490 return (single_step_breakpoints[0] != NULL
15491 || single_step_breakpoints[1] != NULL);
15492 }
15493
15494 /* Remove and delete any breakpoints used for software single step. */
15495
15496 void
15497 remove_single_step_breakpoints (void)
15498 {
15499 gdb_assert (single_step_breakpoints[0] != NULL);
15500
15501 /* See insert_single_step_breakpoint for more about this deprecated
15502 call. */
15503 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15504 single_step_breakpoints[0]);
15505 single_step_gdbarch[0] = NULL;
15506 single_step_breakpoints[0] = NULL;
15507
15508 if (single_step_breakpoints[1] != NULL)
15509 {
15510 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15511 single_step_breakpoints[1]);
15512 single_step_gdbarch[1] = NULL;
15513 single_step_breakpoints[1] = NULL;
15514 }
15515 }
15516
15517 /* Delete software single step breakpoints without removing them from
15518 the inferior. This is intended to be used if the inferior's address
15519 space where they were inserted is already gone, e.g. after exit or
15520 exec. */
15521
15522 void
15523 cancel_single_step_breakpoints (void)
15524 {
15525 int i;
15526
15527 for (i = 0; i < 2; i++)
15528 if (single_step_breakpoints[i])
15529 {
15530 xfree (single_step_breakpoints[i]);
15531 single_step_breakpoints[i] = NULL;
15532 single_step_gdbarch[i] = NULL;
15533 }
15534 }
15535
15536 /* Detach software single-step breakpoints from INFERIOR_PTID without
15537 removing them. */
15538
15539 static void
15540 detach_single_step_breakpoints (void)
15541 {
15542 int i;
15543
15544 for (i = 0; i < 2; i++)
15545 if (single_step_breakpoints[i])
15546 target_remove_breakpoint (single_step_gdbarch[i],
15547 single_step_breakpoints[i]);
15548 }
15549
15550 /* Find the software single-step breakpoint that inserted at PC.
15551 Returns its slot if found, and -1 if not found. */
15552
15553 static int
15554 find_single_step_breakpoint (struct address_space *aspace,
15555 CORE_ADDR pc)
15556 {
15557 int i;
15558
15559 for (i = 0; i < 2; i++)
15560 {
15561 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15562 if (bp_tgt
15563 && breakpoint_address_match (bp_tgt->placed_address_space,
15564 bp_tgt->reqstd_address,
15565 aspace, pc))
15566 return i;
15567 }
15568
15569 return -1;
15570 }
15571
15572 /* Check whether a software single-step breakpoint is inserted at
15573 PC. */
15574
15575 int
15576 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15577 CORE_ADDR pc)
15578 {
15579 return find_single_step_breakpoint (aspace, pc) >= 0;
15580 }
15581
15582 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15583 non-zero otherwise. */
15584 static int
15585 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15586 {
15587 if (syscall_catchpoint_p (bp)
15588 && bp->enable_state != bp_disabled
15589 && bp->enable_state != bp_call_disabled)
15590 return 1;
15591 else
15592 return 0;
15593 }
15594
15595 int
15596 catch_syscall_enabled (void)
15597 {
15598 struct catch_syscall_inferior_data *inf_data
15599 = get_catch_syscall_inferior_data (current_inferior ());
15600
15601 return inf_data->total_syscalls_count != 0;
15602 }
15603
15604 int
15605 catching_syscall_number (int syscall_number)
15606 {
15607 struct breakpoint *bp;
15608
15609 ALL_BREAKPOINTS (bp)
15610 if (is_syscall_catchpoint_enabled (bp))
15611 {
15612 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15613
15614 if (c->syscalls_to_be_caught)
15615 {
15616 int i, iter;
15617 for (i = 0;
15618 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15619 i++)
15620 if (syscall_number == iter)
15621 return 1;
15622 }
15623 else
15624 return 1;
15625 }
15626
15627 return 0;
15628 }
15629
15630 /* Complete syscall names. Used by "catch syscall". */
15631 static VEC (char_ptr) *
15632 catch_syscall_completer (struct cmd_list_element *cmd,
15633 const char *text, const char *word)
15634 {
15635 const char **list = get_syscall_names ();
15636 VEC (char_ptr) *retlist
15637 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15638
15639 xfree (list);
15640 return retlist;
15641 }
15642
15643 /* Tracepoint-specific operations. */
15644
15645 /* Set tracepoint count to NUM. */
15646 static void
15647 set_tracepoint_count (int num)
15648 {
15649 tracepoint_count = num;
15650 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15651 }
15652
15653 static void
15654 trace_command (char *arg, int from_tty)
15655 {
15656 struct breakpoint_ops *ops;
15657 const char *arg_cp = arg;
15658
15659 if (arg && probe_linespec_to_ops (&arg_cp))
15660 ops = &tracepoint_probe_breakpoint_ops;
15661 else
15662 ops = &tracepoint_breakpoint_ops;
15663
15664 create_breakpoint (get_current_arch (),
15665 arg,
15666 NULL, 0, NULL, 1 /* parse arg */,
15667 0 /* tempflag */,
15668 bp_tracepoint /* type_wanted */,
15669 0 /* Ignore count */,
15670 pending_break_support,
15671 ops,
15672 from_tty,
15673 1 /* enabled */,
15674 0 /* internal */, 0);
15675 }
15676
15677 static void
15678 ftrace_command (char *arg, int from_tty)
15679 {
15680 create_breakpoint (get_current_arch (),
15681 arg,
15682 NULL, 0, NULL, 1 /* parse arg */,
15683 0 /* tempflag */,
15684 bp_fast_tracepoint /* type_wanted */,
15685 0 /* Ignore count */,
15686 pending_break_support,
15687 &tracepoint_breakpoint_ops,
15688 from_tty,
15689 1 /* enabled */,
15690 0 /* internal */, 0);
15691 }
15692
15693 /* strace command implementation. Creates a static tracepoint. */
15694
15695 static void
15696 strace_command (char *arg, int from_tty)
15697 {
15698 struct breakpoint_ops *ops;
15699
15700 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15701 or with a normal static tracepoint. */
15702 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15703 ops = &strace_marker_breakpoint_ops;
15704 else
15705 ops = &tracepoint_breakpoint_ops;
15706
15707 create_breakpoint (get_current_arch (),
15708 arg,
15709 NULL, 0, NULL, 1 /* parse arg */,
15710 0 /* tempflag */,
15711 bp_static_tracepoint /* type_wanted */,
15712 0 /* Ignore count */,
15713 pending_break_support,
15714 ops,
15715 from_tty,
15716 1 /* enabled */,
15717 0 /* internal */, 0);
15718 }
15719
15720 /* Set up a fake reader function that gets command lines from a linked
15721 list that was acquired during tracepoint uploading. */
15722
15723 static struct uploaded_tp *this_utp;
15724 static int next_cmd;
15725
15726 static char *
15727 read_uploaded_action (void)
15728 {
15729 char *rslt;
15730
15731 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15732
15733 next_cmd++;
15734
15735 return rslt;
15736 }
15737
15738 /* Given information about a tracepoint as recorded on a target (which
15739 can be either a live system or a trace file), attempt to create an
15740 equivalent GDB tracepoint. This is not a reliable process, since
15741 the target does not necessarily have all the information used when
15742 the tracepoint was originally defined. */
15743
15744 struct tracepoint *
15745 create_tracepoint_from_upload (struct uploaded_tp *utp)
15746 {
15747 char *addr_str, small_buf[100];
15748 struct tracepoint *tp;
15749
15750 if (utp->at_string)
15751 addr_str = utp->at_string;
15752 else
15753 {
15754 /* In the absence of a source location, fall back to raw
15755 address. Since there is no way to confirm that the address
15756 means the same thing as when the trace was started, warn the
15757 user. */
15758 warning (_("Uploaded tracepoint %d has no "
15759 "source location, using raw address"),
15760 utp->number);
15761 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15762 addr_str = small_buf;
15763 }
15764
15765 /* There's not much we can do with a sequence of bytecodes. */
15766 if (utp->cond && !utp->cond_string)
15767 warning (_("Uploaded tracepoint %d condition "
15768 "has no source form, ignoring it"),
15769 utp->number);
15770
15771 if (!create_breakpoint (get_current_arch (),
15772 addr_str,
15773 utp->cond_string, -1, NULL,
15774 0 /* parse cond/thread */,
15775 0 /* tempflag */,
15776 utp->type /* type_wanted */,
15777 0 /* Ignore count */,
15778 pending_break_support,
15779 &tracepoint_breakpoint_ops,
15780 0 /* from_tty */,
15781 utp->enabled /* enabled */,
15782 0 /* internal */,
15783 CREATE_BREAKPOINT_FLAGS_INSERTED))
15784 return NULL;
15785
15786 /* Get the tracepoint we just created. */
15787 tp = get_tracepoint (tracepoint_count);
15788 gdb_assert (tp != NULL);
15789
15790 if (utp->pass > 0)
15791 {
15792 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15793 tp->base.number);
15794
15795 trace_pass_command (small_buf, 0);
15796 }
15797
15798 /* If we have uploaded versions of the original commands, set up a
15799 special-purpose "reader" function and call the usual command line
15800 reader, then pass the result to the breakpoint command-setting
15801 function. */
15802 if (!VEC_empty (char_ptr, utp->cmd_strings))
15803 {
15804 struct command_line *cmd_list;
15805
15806 this_utp = utp;
15807 next_cmd = 0;
15808
15809 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15810
15811 breakpoint_set_commands (&tp->base, cmd_list);
15812 }
15813 else if (!VEC_empty (char_ptr, utp->actions)
15814 || !VEC_empty (char_ptr, utp->step_actions))
15815 warning (_("Uploaded tracepoint %d actions "
15816 "have no source form, ignoring them"),
15817 utp->number);
15818
15819 /* Copy any status information that might be available. */
15820 tp->base.hit_count = utp->hit_count;
15821 tp->traceframe_usage = utp->traceframe_usage;
15822
15823 return tp;
15824 }
15825
15826 /* Print information on tracepoint number TPNUM_EXP, or all if
15827 omitted. */
15828
15829 static void
15830 tracepoints_info (char *args, int from_tty)
15831 {
15832 struct ui_out *uiout = current_uiout;
15833 int num_printed;
15834
15835 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15836
15837 if (num_printed == 0)
15838 {
15839 if (args == NULL || *args == '\0')
15840 ui_out_message (uiout, 0, "No tracepoints.\n");
15841 else
15842 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15843 }
15844
15845 default_collect_info ();
15846 }
15847
15848 /* The 'enable trace' command enables tracepoints.
15849 Not supported by all targets. */
15850 static void
15851 enable_trace_command (char *args, int from_tty)
15852 {
15853 enable_command (args, from_tty);
15854 }
15855
15856 /* The 'disable trace' command disables tracepoints.
15857 Not supported by all targets. */
15858 static void
15859 disable_trace_command (char *args, int from_tty)
15860 {
15861 disable_command (args, from_tty);
15862 }
15863
15864 /* Remove a tracepoint (or all if no argument). */
15865 static void
15866 delete_trace_command (char *arg, int from_tty)
15867 {
15868 struct breakpoint *b, *b_tmp;
15869
15870 dont_repeat ();
15871
15872 if (arg == 0)
15873 {
15874 int breaks_to_delete = 0;
15875
15876 /* Delete all breakpoints if no argument.
15877 Do not delete internal or call-dummy breakpoints, these
15878 have to be deleted with an explicit breakpoint number
15879 argument. */
15880 ALL_TRACEPOINTS (b)
15881 if (is_tracepoint (b) && user_breakpoint_p (b))
15882 {
15883 breaks_to_delete = 1;
15884 break;
15885 }
15886
15887 /* Ask user only if there are some breakpoints to delete. */
15888 if (!from_tty
15889 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15890 {
15891 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15892 if (is_tracepoint (b) && user_breakpoint_p (b))
15893 delete_breakpoint (b);
15894 }
15895 }
15896 else
15897 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15898 }
15899
15900 /* Helper function for trace_pass_command. */
15901
15902 static void
15903 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15904 {
15905 tp->pass_count = count;
15906 observer_notify_breakpoint_modified (&tp->base);
15907 if (from_tty)
15908 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15909 tp->base.number, count);
15910 }
15911
15912 /* Set passcount for tracepoint.
15913
15914 First command argument is passcount, second is tracepoint number.
15915 If tracepoint number omitted, apply to most recently defined.
15916 Also accepts special argument "all". */
15917
15918 static void
15919 trace_pass_command (char *args, int from_tty)
15920 {
15921 struct tracepoint *t1;
15922 unsigned int count;
15923
15924 if (args == 0 || *args == 0)
15925 error (_("passcount command requires an "
15926 "argument (count + optional TP num)"));
15927
15928 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15929
15930 args = skip_spaces (args);
15931 if (*args && strncasecmp (args, "all", 3) == 0)
15932 {
15933 struct breakpoint *b;
15934
15935 args += 3; /* Skip special argument "all". */
15936 if (*args)
15937 error (_("Junk at end of arguments."));
15938
15939 ALL_TRACEPOINTS (b)
15940 {
15941 t1 = (struct tracepoint *) b;
15942 trace_pass_set_count (t1, count, from_tty);
15943 }
15944 }
15945 else if (*args == '\0')
15946 {
15947 t1 = get_tracepoint_by_number (&args, NULL);
15948 if (t1)
15949 trace_pass_set_count (t1, count, from_tty);
15950 }
15951 else
15952 {
15953 struct get_number_or_range_state state;
15954
15955 init_number_or_range (&state, args);
15956 while (!state.finished)
15957 {
15958 t1 = get_tracepoint_by_number (&args, &state);
15959 if (t1)
15960 trace_pass_set_count (t1, count, from_tty);
15961 }
15962 }
15963 }
15964
15965 struct tracepoint *
15966 get_tracepoint (int num)
15967 {
15968 struct breakpoint *t;
15969
15970 ALL_TRACEPOINTS (t)
15971 if (t->number == num)
15972 return (struct tracepoint *) t;
15973
15974 return NULL;
15975 }
15976
15977 /* Find the tracepoint with the given target-side number (which may be
15978 different from the tracepoint number after disconnecting and
15979 reconnecting). */
15980
15981 struct tracepoint *
15982 get_tracepoint_by_number_on_target (int num)
15983 {
15984 struct breakpoint *b;
15985
15986 ALL_TRACEPOINTS (b)
15987 {
15988 struct tracepoint *t = (struct tracepoint *) b;
15989
15990 if (t->number_on_target == num)
15991 return t;
15992 }
15993
15994 return NULL;
15995 }
15996
15997 /* Utility: parse a tracepoint number and look it up in the list.
15998 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15999 If the argument is missing, the most recent tracepoint
16000 (tracepoint_count) is returned. */
16001
16002 struct tracepoint *
16003 get_tracepoint_by_number (char **arg,
16004 struct get_number_or_range_state *state)
16005 {
16006 struct breakpoint *t;
16007 int tpnum;
16008 char *instring = arg == NULL ? NULL : *arg;
16009
16010 if (state)
16011 {
16012 gdb_assert (!state->finished);
16013 tpnum = get_number_or_range (state);
16014 }
16015 else if (arg == NULL || *arg == NULL || ! **arg)
16016 tpnum = tracepoint_count;
16017 else
16018 tpnum = get_number (arg);
16019
16020 if (tpnum <= 0)
16021 {
16022 if (instring && *instring)
16023 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
16024 instring);
16025 else
16026 printf_filtered (_("No previous tracepoint\n"));
16027 return NULL;
16028 }
16029
16030 ALL_TRACEPOINTS (t)
16031 if (t->number == tpnum)
16032 {
16033 return (struct tracepoint *) t;
16034 }
16035
16036 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
16037 return NULL;
16038 }
16039
16040 void
16041 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
16042 {
16043 if (b->thread != -1)
16044 fprintf_unfiltered (fp, " thread %d", b->thread);
16045
16046 if (b->task != 0)
16047 fprintf_unfiltered (fp, " task %d", b->task);
16048
16049 fprintf_unfiltered (fp, "\n");
16050 }
16051
16052 /* Save information on user settable breakpoints (watchpoints, etc) to
16053 a new script file named FILENAME. If FILTER is non-NULL, call it
16054 on each breakpoint and only include the ones for which it returns
16055 non-zero. */
16056
16057 static void
16058 save_breakpoints (char *filename, int from_tty,
16059 int (*filter) (const struct breakpoint *))
16060 {
16061 struct breakpoint *tp;
16062 int any = 0;
16063 struct cleanup *cleanup;
16064 struct ui_file *fp;
16065 int extra_trace_bits = 0;
16066
16067 if (filename == 0 || *filename == 0)
16068 error (_("Argument required (file name in which to save)"));
16069
16070 /* See if we have anything to save. */
16071 ALL_BREAKPOINTS (tp)
16072 {
16073 /* Skip internal and momentary breakpoints. */
16074 if (!user_breakpoint_p (tp))
16075 continue;
16076
16077 /* If we have a filter, only save the breakpoints it accepts. */
16078 if (filter && !filter (tp))
16079 continue;
16080
16081 any = 1;
16082
16083 if (is_tracepoint (tp))
16084 {
16085 extra_trace_bits = 1;
16086
16087 /* We can stop searching. */
16088 break;
16089 }
16090 }
16091
16092 if (!any)
16093 {
16094 warning (_("Nothing to save."));
16095 return;
16096 }
16097
16098 filename = tilde_expand (filename);
16099 cleanup = make_cleanup (xfree, filename);
16100 fp = gdb_fopen (filename, "w");
16101 if (!fp)
16102 error (_("Unable to open file '%s' for saving (%s)"),
16103 filename, safe_strerror (errno));
16104 make_cleanup_ui_file_delete (fp);
16105
16106 if (extra_trace_bits)
16107 save_trace_state_variables (fp);
16108
16109 ALL_BREAKPOINTS (tp)
16110 {
16111 /* Skip internal and momentary breakpoints. */
16112 if (!user_breakpoint_p (tp))
16113 continue;
16114
16115 /* If we have a filter, only save the breakpoints it accepts. */
16116 if (filter && !filter (tp))
16117 continue;
16118
16119 tp->ops->print_recreate (tp, fp);
16120
16121 /* Note, we can't rely on tp->number for anything, as we can't
16122 assume the recreated breakpoint numbers will match. Use $bpnum
16123 instead. */
16124
16125 if (tp->cond_string)
16126 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
16127
16128 if (tp->ignore_count)
16129 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
16130
16131 if (tp->type != bp_dprintf && tp->commands)
16132 {
16133 volatile struct gdb_exception ex;
16134
16135 fprintf_unfiltered (fp, " commands\n");
16136
16137 ui_out_redirect (current_uiout, fp);
16138 TRY_CATCH (ex, RETURN_MASK_ALL)
16139 {
16140 print_command_lines (current_uiout, tp->commands->commands, 2);
16141 }
16142 ui_out_redirect (current_uiout, NULL);
16143
16144 if (ex.reason < 0)
16145 throw_exception (ex);
16146
16147 fprintf_unfiltered (fp, " end\n");
16148 }
16149
16150 if (tp->enable_state == bp_disabled)
16151 fprintf_unfiltered (fp, "disable $bpnum\n");
16152
16153 /* If this is a multi-location breakpoint, check if the locations
16154 should be individually disabled. Watchpoint locations are
16155 special, and not user visible. */
16156 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
16157 {
16158 struct bp_location *loc;
16159 int n = 1;
16160
16161 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
16162 if (!loc->enabled)
16163 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
16164 }
16165 }
16166
16167 if (extra_trace_bits && *default_collect)
16168 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
16169
16170 if (from_tty)
16171 printf_filtered (_("Saved to file '%s'.\n"), filename);
16172 do_cleanups (cleanup);
16173 }
16174
16175 /* The `save breakpoints' command. */
16176
16177 static void
16178 save_breakpoints_command (char *args, int from_tty)
16179 {
16180 save_breakpoints (args, from_tty, NULL);
16181 }
16182
16183 /* The `save tracepoints' command. */
16184
16185 static void
16186 save_tracepoints_command (char *args, int from_tty)
16187 {
16188 save_breakpoints (args, from_tty, is_tracepoint);
16189 }
16190
16191 /* Create a vector of all tracepoints. */
16192
16193 VEC(breakpoint_p) *
16194 all_tracepoints (void)
16195 {
16196 VEC(breakpoint_p) *tp_vec = 0;
16197 struct breakpoint *tp;
16198
16199 ALL_TRACEPOINTS (tp)
16200 {
16201 VEC_safe_push (breakpoint_p, tp_vec, tp);
16202 }
16203
16204 return tp_vec;
16205 }
16206
16207 \f
16208 /* This help string is used for the break, hbreak, tbreak and thbreak
16209 commands. It is defined as a macro to prevent duplication.
16210 COMMAND should be a string constant containing the name of the
16211 command. */
16212 #define BREAK_ARGS_HELP(command) \
16213 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16214 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16215 probe point. Accepted values are `-probe' (for a generic, automatically\n\
16216 guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
16217 LOCATION may be a line number, function name, or \"*\" and an address.\n\
16218 If a line number is specified, break at start of code for that line.\n\
16219 If a function is specified, break at start of code for that function.\n\
16220 If an address is specified, break at that exact address.\n\
16221 With no LOCATION, uses current execution address of the selected\n\
16222 stack frame. This is useful for breaking on return to a stack frame.\n\
16223 \n\
16224 THREADNUM is the number from \"info threads\".\n\
16225 CONDITION is a boolean expression.\n\
16226 \n\
16227 Multiple breakpoints at one place are permitted, and useful if their\n\
16228 conditions are different.\n\
16229 \n\
16230 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16231
16232 /* List of subcommands for "catch". */
16233 static struct cmd_list_element *catch_cmdlist;
16234
16235 /* List of subcommands for "tcatch". */
16236 static struct cmd_list_element *tcatch_cmdlist;
16237
16238 void
16239 add_catch_command (char *name, char *docstring,
16240 cmd_sfunc_ftype *sfunc,
16241 completer_ftype *completer,
16242 void *user_data_catch,
16243 void *user_data_tcatch)
16244 {
16245 struct cmd_list_element *command;
16246
16247 command = add_cmd (name, class_breakpoint, NULL, docstring,
16248 &catch_cmdlist);
16249 set_cmd_sfunc (command, sfunc);
16250 set_cmd_context (command, user_data_catch);
16251 set_cmd_completer (command, completer);
16252
16253 command = add_cmd (name, class_breakpoint, NULL, docstring,
16254 &tcatch_cmdlist);
16255 set_cmd_sfunc (command, sfunc);
16256 set_cmd_context (command, user_data_tcatch);
16257 set_cmd_completer (command, completer);
16258 }
16259
16260 static void
16261 clear_syscall_counts (struct inferior *inf)
16262 {
16263 struct catch_syscall_inferior_data *inf_data
16264 = get_catch_syscall_inferior_data (inf);
16265
16266 inf_data->total_syscalls_count = 0;
16267 inf_data->any_syscall_count = 0;
16268 VEC_free (int, inf_data->syscalls_counts);
16269 }
16270
16271 static void
16272 save_command (char *arg, int from_tty)
16273 {
16274 printf_unfiltered (_("\"save\" must be followed by "
16275 "the name of a save subcommand.\n"));
16276 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
16277 }
16278
16279 struct breakpoint *
16280 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16281 void *data)
16282 {
16283 struct breakpoint *b, *b_tmp;
16284
16285 ALL_BREAKPOINTS_SAFE (b, b_tmp)
16286 {
16287 if ((*callback) (b, data))
16288 return b;
16289 }
16290
16291 return NULL;
16292 }
16293
16294 /* Zero if any of the breakpoint's locations could be a location where
16295 functions have been inlined, nonzero otherwise. */
16296
16297 static int
16298 is_non_inline_function (struct breakpoint *b)
16299 {
16300 /* The shared library event breakpoint is set on the address of a
16301 non-inline function. */
16302 if (b->type == bp_shlib_event)
16303 return 1;
16304
16305 return 0;
16306 }
16307
16308 /* Nonzero if the specified PC cannot be a location where functions
16309 have been inlined. */
16310
16311 int
16312 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16313 const struct target_waitstatus *ws)
16314 {
16315 struct breakpoint *b;
16316 struct bp_location *bl;
16317
16318 ALL_BREAKPOINTS (b)
16319 {
16320 if (!is_non_inline_function (b))
16321 continue;
16322
16323 for (bl = b->loc; bl != NULL; bl = bl->next)
16324 {
16325 if (!bl->shlib_disabled
16326 && bpstat_check_location (bl, aspace, pc, ws))
16327 return 1;
16328 }
16329 }
16330
16331 return 0;
16332 }
16333
16334 /* Remove any references to OBJFILE which is going to be freed. */
16335
16336 void
16337 breakpoint_free_objfile (struct objfile *objfile)
16338 {
16339 struct bp_location **locp, *loc;
16340
16341 ALL_BP_LOCATIONS (loc, locp)
16342 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16343 loc->symtab = NULL;
16344 }
16345
16346 void
16347 initialize_breakpoint_ops (void)
16348 {
16349 static int initialized = 0;
16350
16351 struct breakpoint_ops *ops;
16352
16353 if (initialized)
16354 return;
16355 initialized = 1;
16356
16357 /* The breakpoint_ops structure to be inherit by all kinds of
16358 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16359 internal and momentary breakpoints, etc.). */
16360 ops = &bkpt_base_breakpoint_ops;
16361 *ops = base_breakpoint_ops;
16362 ops->re_set = bkpt_re_set;
16363 ops->insert_location = bkpt_insert_location;
16364 ops->remove_location = bkpt_remove_location;
16365 ops->breakpoint_hit = bkpt_breakpoint_hit;
16366 ops->create_sals_from_address = bkpt_create_sals_from_address;
16367 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16368 ops->decode_linespec = bkpt_decode_linespec;
16369
16370 /* The breakpoint_ops structure to be used in regular breakpoints. */
16371 ops = &bkpt_breakpoint_ops;
16372 *ops = bkpt_base_breakpoint_ops;
16373 ops->re_set = bkpt_re_set;
16374 ops->resources_needed = bkpt_resources_needed;
16375 ops->print_it = bkpt_print_it;
16376 ops->print_mention = bkpt_print_mention;
16377 ops->print_recreate = bkpt_print_recreate;
16378
16379 /* Ranged breakpoints. */
16380 ops = &ranged_breakpoint_ops;
16381 *ops = bkpt_breakpoint_ops;
16382 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16383 ops->resources_needed = resources_needed_ranged_breakpoint;
16384 ops->print_it = print_it_ranged_breakpoint;
16385 ops->print_one = print_one_ranged_breakpoint;
16386 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16387 ops->print_mention = print_mention_ranged_breakpoint;
16388 ops->print_recreate = print_recreate_ranged_breakpoint;
16389
16390 /* Internal breakpoints. */
16391 ops = &internal_breakpoint_ops;
16392 *ops = bkpt_base_breakpoint_ops;
16393 ops->re_set = internal_bkpt_re_set;
16394 ops->check_status = internal_bkpt_check_status;
16395 ops->print_it = internal_bkpt_print_it;
16396 ops->print_mention = internal_bkpt_print_mention;
16397
16398 /* Momentary breakpoints. */
16399 ops = &momentary_breakpoint_ops;
16400 *ops = bkpt_base_breakpoint_ops;
16401 ops->re_set = momentary_bkpt_re_set;
16402 ops->check_status = momentary_bkpt_check_status;
16403 ops->print_it = momentary_bkpt_print_it;
16404 ops->print_mention = momentary_bkpt_print_mention;
16405
16406 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16407 ops = &longjmp_breakpoint_ops;
16408 *ops = momentary_breakpoint_ops;
16409 ops->dtor = longjmp_bkpt_dtor;
16410
16411 /* Probe breakpoints. */
16412 ops = &bkpt_probe_breakpoint_ops;
16413 *ops = bkpt_breakpoint_ops;
16414 ops->insert_location = bkpt_probe_insert_location;
16415 ops->remove_location = bkpt_probe_remove_location;
16416 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16417 ops->decode_linespec = bkpt_probe_decode_linespec;
16418
16419 /* Watchpoints. */
16420 ops = &watchpoint_breakpoint_ops;
16421 *ops = base_breakpoint_ops;
16422 ops->dtor = dtor_watchpoint;
16423 ops->re_set = re_set_watchpoint;
16424 ops->insert_location = insert_watchpoint;
16425 ops->remove_location = remove_watchpoint;
16426 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16427 ops->check_status = check_status_watchpoint;
16428 ops->resources_needed = resources_needed_watchpoint;
16429 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16430 ops->print_it = print_it_watchpoint;
16431 ops->print_mention = print_mention_watchpoint;
16432 ops->print_recreate = print_recreate_watchpoint;
16433 ops->explains_signal = explains_signal_watchpoint;
16434
16435 /* Masked watchpoints. */
16436 ops = &masked_watchpoint_breakpoint_ops;
16437 *ops = watchpoint_breakpoint_ops;
16438 ops->insert_location = insert_masked_watchpoint;
16439 ops->remove_location = remove_masked_watchpoint;
16440 ops->resources_needed = resources_needed_masked_watchpoint;
16441 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16442 ops->print_it = print_it_masked_watchpoint;
16443 ops->print_one_detail = print_one_detail_masked_watchpoint;
16444 ops->print_mention = print_mention_masked_watchpoint;
16445 ops->print_recreate = print_recreate_masked_watchpoint;
16446
16447 /* Tracepoints. */
16448 ops = &tracepoint_breakpoint_ops;
16449 *ops = base_breakpoint_ops;
16450 ops->re_set = tracepoint_re_set;
16451 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16452 ops->print_one_detail = tracepoint_print_one_detail;
16453 ops->print_mention = tracepoint_print_mention;
16454 ops->print_recreate = tracepoint_print_recreate;
16455 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16456 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16457 ops->decode_linespec = tracepoint_decode_linespec;
16458
16459 /* Probe tracepoints. */
16460 ops = &tracepoint_probe_breakpoint_ops;
16461 *ops = tracepoint_breakpoint_ops;
16462 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16463 ops->decode_linespec = tracepoint_probe_decode_linespec;
16464
16465 /* Static tracepoints with marker (`-m'). */
16466 ops = &strace_marker_breakpoint_ops;
16467 *ops = tracepoint_breakpoint_ops;
16468 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16469 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16470 ops->decode_linespec = strace_marker_decode_linespec;
16471
16472 /* Fork catchpoints. */
16473 ops = &catch_fork_breakpoint_ops;
16474 *ops = base_breakpoint_ops;
16475 ops->insert_location = insert_catch_fork;
16476 ops->remove_location = remove_catch_fork;
16477 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16478 ops->print_it = print_it_catch_fork;
16479 ops->print_one = print_one_catch_fork;
16480 ops->print_mention = print_mention_catch_fork;
16481 ops->print_recreate = print_recreate_catch_fork;
16482
16483 /* Vfork catchpoints. */
16484 ops = &catch_vfork_breakpoint_ops;
16485 *ops = base_breakpoint_ops;
16486 ops->insert_location = insert_catch_vfork;
16487 ops->remove_location = remove_catch_vfork;
16488 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16489 ops->print_it = print_it_catch_vfork;
16490 ops->print_one = print_one_catch_vfork;
16491 ops->print_mention = print_mention_catch_vfork;
16492 ops->print_recreate = print_recreate_catch_vfork;
16493
16494 /* Exec catchpoints. */
16495 ops = &catch_exec_breakpoint_ops;
16496 *ops = base_breakpoint_ops;
16497 ops->dtor = dtor_catch_exec;
16498 ops->insert_location = insert_catch_exec;
16499 ops->remove_location = remove_catch_exec;
16500 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16501 ops->print_it = print_it_catch_exec;
16502 ops->print_one = print_one_catch_exec;
16503 ops->print_mention = print_mention_catch_exec;
16504 ops->print_recreate = print_recreate_catch_exec;
16505
16506 /* Syscall catchpoints. */
16507 ops = &catch_syscall_breakpoint_ops;
16508 *ops = base_breakpoint_ops;
16509 ops->dtor = dtor_catch_syscall;
16510 ops->insert_location = insert_catch_syscall;
16511 ops->remove_location = remove_catch_syscall;
16512 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16513 ops->print_it = print_it_catch_syscall;
16514 ops->print_one = print_one_catch_syscall;
16515 ops->print_mention = print_mention_catch_syscall;
16516 ops->print_recreate = print_recreate_catch_syscall;
16517
16518 /* Solib-related catchpoints. */
16519 ops = &catch_solib_breakpoint_ops;
16520 *ops = base_breakpoint_ops;
16521 ops->dtor = dtor_catch_solib;
16522 ops->insert_location = insert_catch_solib;
16523 ops->remove_location = remove_catch_solib;
16524 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16525 ops->check_status = check_status_catch_solib;
16526 ops->print_it = print_it_catch_solib;
16527 ops->print_one = print_one_catch_solib;
16528 ops->print_mention = print_mention_catch_solib;
16529 ops->print_recreate = print_recreate_catch_solib;
16530
16531 ops = &dprintf_breakpoint_ops;
16532 *ops = bkpt_base_breakpoint_ops;
16533 ops->re_set = dprintf_re_set;
16534 ops->resources_needed = bkpt_resources_needed;
16535 ops->print_it = bkpt_print_it;
16536 ops->print_mention = bkpt_print_mention;
16537 ops->print_recreate = dprintf_print_recreate;
16538 ops->after_condition_true = dprintf_after_condition_true;
16539 ops->breakpoint_hit = dprintf_breakpoint_hit;
16540 }
16541
16542 /* Chain containing all defined "enable breakpoint" subcommands. */
16543
16544 static struct cmd_list_element *enablebreaklist = NULL;
16545
16546 void
16547 _initialize_breakpoint (void)
16548 {
16549 struct cmd_list_element *c;
16550
16551 initialize_breakpoint_ops ();
16552
16553 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16554 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16555 observer_attach_inferior_exit (clear_syscall_counts);
16556 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16557
16558 breakpoint_objfile_key
16559 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16560
16561 catch_syscall_inferior_data
16562 = register_inferior_data_with_cleanup (NULL,
16563 catch_syscall_inferior_data_cleanup);
16564
16565 breakpoint_chain = 0;
16566 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16567 before a breakpoint is set. */
16568 breakpoint_count = 0;
16569
16570 tracepoint_count = 0;
16571
16572 add_com ("ignore", class_breakpoint, ignore_command, _("\
16573 Set ignore-count of breakpoint number N to COUNT.\n\
16574 Usage is `ignore N COUNT'."));
16575 if (xdb_commands)
16576 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16577
16578 add_com ("commands", class_breakpoint, commands_command, _("\
16579 Set commands to be executed when a breakpoint is hit.\n\
16580 Give breakpoint number as argument after \"commands\".\n\
16581 With no argument, the targeted breakpoint is the last one set.\n\
16582 The commands themselves follow starting on the next line.\n\
16583 Type a line containing \"end\" to indicate the end of them.\n\
16584 Give \"silent\" as the first line to make the breakpoint silent;\n\
16585 then no output is printed when it is hit, except what the commands print."));
16586
16587 c = add_com ("condition", class_breakpoint, condition_command, _("\
16588 Specify breakpoint number N to break only if COND is true.\n\
16589 Usage is `condition N COND', where N is an integer and COND is an\n\
16590 expression to be evaluated whenever breakpoint N is reached."));
16591 set_cmd_completer (c, condition_completer);
16592
16593 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16594 Set a temporary breakpoint.\n\
16595 Like \"break\" except the breakpoint is only temporary,\n\
16596 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16597 by using \"enable delete\" on the breakpoint number.\n\
16598 \n"
16599 BREAK_ARGS_HELP ("tbreak")));
16600 set_cmd_completer (c, location_completer);
16601
16602 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16603 Set a hardware assisted breakpoint.\n\
16604 Like \"break\" except the breakpoint requires hardware support,\n\
16605 some target hardware may not have this support.\n\
16606 \n"
16607 BREAK_ARGS_HELP ("hbreak")));
16608 set_cmd_completer (c, location_completer);
16609
16610 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16611 Set a temporary hardware assisted breakpoint.\n\
16612 Like \"hbreak\" except the breakpoint is only temporary,\n\
16613 so it will be deleted when hit.\n\
16614 \n"
16615 BREAK_ARGS_HELP ("thbreak")));
16616 set_cmd_completer (c, location_completer);
16617
16618 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16619 Enable some breakpoints.\n\
16620 Give breakpoint numbers (separated by spaces) as arguments.\n\
16621 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16622 This is used to cancel the effect of the \"disable\" command.\n\
16623 With a subcommand you can enable temporarily."),
16624 &enablelist, "enable ", 1, &cmdlist);
16625 if (xdb_commands)
16626 add_com ("ab", class_breakpoint, enable_command, _("\
16627 Enable some breakpoints.\n\
16628 Give breakpoint numbers (separated by spaces) as arguments.\n\
16629 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16630 This is used to cancel the effect of the \"disable\" command.\n\
16631 With a subcommand you can enable temporarily."));
16632
16633 add_com_alias ("en", "enable", class_breakpoint, 1);
16634
16635 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16636 Enable some breakpoints.\n\
16637 Give breakpoint numbers (separated by spaces) as arguments.\n\
16638 This is used to cancel the effect of the \"disable\" command.\n\
16639 May be abbreviated to simply \"enable\".\n"),
16640 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16641
16642 add_cmd ("once", no_class, enable_once_command, _("\
16643 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16644 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16645 &enablebreaklist);
16646
16647 add_cmd ("delete", no_class, enable_delete_command, _("\
16648 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16649 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16650 &enablebreaklist);
16651
16652 add_cmd ("count", no_class, enable_count_command, _("\
16653 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16654 If a breakpoint is hit while enabled in this fashion,\n\
16655 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16656 &enablebreaklist);
16657
16658 add_cmd ("delete", no_class, enable_delete_command, _("\
16659 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16660 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16661 &enablelist);
16662
16663 add_cmd ("once", no_class, enable_once_command, _("\
16664 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16665 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16666 &enablelist);
16667
16668 add_cmd ("count", no_class, enable_count_command, _("\
16669 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16670 If a breakpoint is hit while enabled in this fashion,\n\
16671 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16672 &enablelist);
16673
16674 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16675 Disable some breakpoints.\n\
16676 Arguments are breakpoint numbers with spaces in between.\n\
16677 To disable all breakpoints, give no argument.\n\
16678 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16679 &disablelist, "disable ", 1, &cmdlist);
16680 add_com_alias ("dis", "disable", class_breakpoint, 1);
16681 add_com_alias ("disa", "disable", class_breakpoint, 1);
16682 if (xdb_commands)
16683 add_com ("sb", class_breakpoint, disable_command, _("\
16684 Disable some breakpoints.\n\
16685 Arguments are breakpoint numbers with spaces in between.\n\
16686 To disable all breakpoints, give no argument.\n\
16687 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16688
16689 add_cmd ("breakpoints", class_alias, disable_command, _("\
16690 Disable some breakpoints.\n\
16691 Arguments are breakpoint numbers with spaces in between.\n\
16692 To disable all breakpoints, give no argument.\n\
16693 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16694 This command may be abbreviated \"disable\"."),
16695 &disablelist);
16696
16697 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16698 Delete some breakpoints or auto-display expressions.\n\
16699 Arguments are breakpoint numbers with spaces in between.\n\
16700 To delete all breakpoints, give no argument.\n\
16701 \n\
16702 Also a prefix command for deletion of other GDB objects.\n\
16703 The \"unset\" command is also an alias for \"delete\"."),
16704 &deletelist, "delete ", 1, &cmdlist);
16705 add_com_alias ("d", "delete", class_breakpoint, 1);
16706 add_com_alias ("del", "delete", class_breakpoint, 1);
16707 if (xdb_commands)
16708 add_com ("db", class_breakpoint, delete_command, _("\
16709 Delete some breakpoints.\n\
16710 Arguments are breakpoint numbers with spaces in between.\n\
16711 To delete all breakpoints, give no argument.\n"));
16712
16713 add_cmd ("breakpoints", class_alias, delete_command, _("\
16714 Delete some breakpoints or auto-display expressions.\n\
16715 Arguments are breakpoint numbers with spaces in between.\n\
16716 To delete all breakpoints, give no argument.\n\
16717 This command may be abbreviated \"delete\"."),
16718 &deletelist);
16719
16720 add_com ("clear", class_breakpoint, clear_command, _("\
16721 Clear breakpoint at specified line or function.\n\
16722 Argument may be line number, function name, or \"*\" and an address.\n\
16723 If line number is specified, all breakpoints in that line are cleared.\n\
16724 If function is specified, breakpoints at beginning of function are cleared.\n\
16725 If an address is specified, breakpoints at that address are cleared.\n\
16726 \n\
16727 With no argument, clears all breakpoints in the line that the selected frame\n\
16728 is executing in.\n\
16729 \n\
16730 See also the \"delete\" command which clears breakpoints by number."));
16731 add_com_alias ("cl", "clear", class_breakpoint, 1);
16732
16733 c = add_com ("break", class_breakpoint, break_command, _("\
16734 Set breakpoint at specified line or function.\n"
16735 BREAK_ARGS_HELP ("break")));
16736 set_cmd_completer (c, location_completer);
16737
16738 add_com_alias ("b", "break", class_run, 1);
16739 add_com_alias ("br", "break", class_run, 1);
16740 add_com_alias ("bre", "break", class_run, 1);
16741 add_com_alias ("brea", "break", class_run, 1);
16742
16743 if (xdb_commands)
16744 add_com_alias ("ba", "break", class_breakpoint, 1);
16745
16746 if (dbx_commands)
16747 {
16748 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16749 Break in function/address or break at a line in the current file."),
16750 &stoplist, "stop ", 1, &cmdlist);
16751 add_cmd ("in", class_breakpoint, stopin_command,
16752 _("Break in function or address."), &stoplist);
16753 add_cmd ("at", class_breakpoint, stopat_command,
16754 _("Break at a line in the current file."), &stoplist);
16755 add_com ("status", class_info, breakpoints_info, _("\
16756 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16757 The \"Type\" column indicates one of:\n\
16758 \tbreakpoint - normal breakpoint\n\
16759 \twatchpoint - watchpoint\n\
16760 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16761 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16762 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16763 address and file/line number respectively.\n\
16764 \n\
16765 Convenience variable \"$_\" and default examine address for \"x\"\n\
16766 are set to the address of the last breakpoint listed unless the command\n\
16767 is prefixed with \"server \".\n\n\
16768 Convenience variable \"$bpnum\" contains the number of the last\n\
16769 breakpoint set."));
16770 }
16771
16772 add_info ("breakpoints", breakpoints_info, _("\
16773 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16774 The \"Type\" column indicates one of:\n\
16775 \tbreakpoint - normal breakpoint\n\
16776 \twatchpoint - watchpoint\n\
16777 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16778 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16779 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16780 address and file/line number respectively.\n\
16781 \n\
16782 Convenience variable \"$_\" and default examine address for \"x\"\n\
16783 are set to the address of the last breakpoint listed unless the command\n\
16784 is prefixed with \"server \".\n\n\
16785 Convenience variable \"$bpnum\" contains the number of the last\n\
16786 breakpoint set."));
16787
16788 add_info_alias ("b", "breakpoints", 1);
16789
16790 if (xdb_commands)
16791 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16792 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16793 The \"Type\" column indicates one of:\n\
16794 \tbreakpoint - normal breakpoint\n\
16795 \twatchpoint - watchpoint\n\
16796 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16797 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16798 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16799 address and file/line number respectively.\n\
16800 \n\
16801 Convenience variable \"$_\" and default examine address for \"x\"\n\
16802 are set to the address of the last breakpoint listed unless the command\n\
16803 is prefixed with \"server \".\n\n\
16804 Convenience variable \"$bpnum\" contains the number of the last\n\
16805 breakpoint set."));
16806
16807 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16808 Status of all breakpoints, or breakpoint number NUMBER.\n\
16809 The \"Type\" column indicates one of:\n\
16810 \tbreakpoint - normal breakpoint\n\
16811 \twatchpoint - watchpoint\n\
16812 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16813 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16814 \tuntil - internal breakpoint used by the \"until\" command\n\
16815 \tfinish - internal breakpoint used by the \"finish\" command\n\
16816 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16817 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16818 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16819 address and file/line number respectively.\n\
16820 \n\
16821 Convenience variable \"$_\" and default examine address for \"x\"\n\
16822 are set to the address of the last breakpoint listed unless the command\n\
16823 is prefixed with \"server \".\n\n\
16824 Convenience variable \"$bpnum\" contains the number of the last\n\
16825 breakpoint set."),
16826 &maintenanceinfolist);
16827
16828 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16829 Set catchpoints to catch events."),
16830 &catch_cmdlist, "catch ",
16831 0/*allow-unknown*/, &cmdlist);
16832
16833 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16834 Set temporary catchpoints to catch events."),
16835 &tcatch_cmdlist, "tcatch ",
16836 0/*allow-unknown*/, &cmdlist);
16837
16838 add_catch_command ("fork", _("Catch calls to fork."),
16839 catch_fork_command_1,
16840 NULL,
16841 (void *) (uintptr_t) catch_fork_permanent,
16842 (void *) (uintptr_t) catch_fork_temporary);
16843 add_catch_command ("vfork", _("Catch calls to vfork."),
16844 catch_fork_command_1,
16845 NULL,
16846 (void *) (uintptr_t) catch_vfork_permanent,
16847 (void *) (uintptr_t) catch_vfork_temporary);
16848 add_catch_command ("exec", _("Catch calls to exec."),
16849 catch_exec_command_1,
16850 NULL,
16851 CATCH_PERMANENT,
16852 CATCH_TEMPORARY);
16853 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16854 Usage: catch load [REGEX]\n\
16855 If REGEX is given, only stop for libraries matching the regular expression."),
16856 catch_load_command_1,
16857 NULL,
16858 CATCH_PERMANENT,
16859 CATCH_TEMPORARY);
16860 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16861 Usage: catch unload [REGEX]\n\
16862 If REGEX is given, only stop for libraries matching the regular expression."),
16863 catch_unload_command_1,
16864 NULL,
16865 CATCH_PERMANENT,
16866 CATCH_TEMPORARY);
16867 add_catch_command ("syscall", _("\
16868 Catch system calls by their names and/or numbers.\n\
16869 Arguments say which system calls to catch. If no arguments\n\
16870 are given, every system call will be caught.\n\
16871 Arguments, if given, should be one or more system call names\n\
16872 (if your system supports that), or system call numbers."),
16873 catch_syscall_command_1,
16874 catch_syscall_completer,
16875 CATCH_PERMANENT,
16876 CATCH_TEMPORARY);
16877
16878 c = add_com ("watch", class_breakpoint, watch_command, _("\
16879 Set a watchpoint for an expression.\n\
16880 Usage: watch [-l|-location] EXPRESSION\n\
16881 A watchpoint stops execution of your program whenever the value of\n\
16882 an expression changes.\n\
16883 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16884 the memory to which it refers."));
16885 set_cmd_completer (c, expression_completer);
16886
16887 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16888 Set a read watchpoint for an expression.\n\
16889 Usage: rwatch [-l|-location] EXPRESSION\n\
16890 A watchpoint stops execution of your program whenever the value of\n\
16891 an expression is read.\n\
16892 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16893 the memory to which it refers."));
16894 set_cmd_completer (c, expression_completer);
16895
16896 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16897 Set a watchpoint for an expression.\n\
16898 Usage: awatch [-l|-location] EXPRESSION\n\
16899 A watchpoint stops execution of your program whenever the value of\n\
16900 an expression is either read or written.\n\
16901 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16902 the memory to which it refers."));
16903 set_cmd_completer (c, expression_completer);
16904
16905 add_info ("watchpoints", watchpoints_info, _("\
16906 Status of specified watchpoints (all watchpoints if no argument)."));
16907
16908 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16909 respond to changes - contrary to the description. */
16910 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16911 &can_use_hw_watchpoints, _("\
16912 Set debugger's willingness to use watchpoint hardware."), _("\
16913 Show debugger's willingness to use watchpoint hardware."), _("\
16914 If zero, gdb will not use hardware for new watchpoints, even if\n\
16915 such is available. (However, any hardware watchpoints that were\n\
16916 created before setting this to nonzero, will continue to use watchpoint\n\
16917 hardware.)"),
16918 NULL,
16919 show_can_use_hw_watchpoints,
16920 &setlist, &showlist);
16921
16922 can_use_hw_watchpoints = 1;
16923
16924 /* Tracepoint manipulation commands. */
16925
16926 c = add_com ("trace", class_breakpoint, trace_command, _("\
16927 Set a tracepoint at specified line or function.\n\
16928 \n"
16929 BREAK_ARGS_HELP ("trace") "\n\
16930 Do \"help tracepoints\" for info on other tracepoint commands."));
16931 set_cmd_completer (c, location_completer);
16932
16933 add_com_alias ("tp", "trace", class_alias, 0);
16934 add_com_alias ("tr", "trace", class_alias, 1);
16935 add_com_alias ("tra", "trace", class_alias, 1);
16936 add_com_alias ("trac", "trace", class_alias, 1);
16937
16938 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16939 Set a fast tracepoint at specified line or function.\n\
16940 \n"
16941 BREAK_ARGS_HELP ("ftrace") "\n\
16942 Do \"help tracepoints\" for info on other tracepoint commands."));
16943 set_cmd_completer (c, location_completer);
16944
16945 c = add_com ("strace", class_breakpoint, strace_command, _("\
16946 Set a static tracepoint at specified line, function or marker.\n\
16947 \n\
16948 strace [LOCATION] [if CONDITION]\n\
16949 LOCATION may be a line number, function name, \"*\" and an address,\n\
16950 or -m MARKER_ID.\n\
16951 If a line number is specified, probe the marker at start of code\n\
16952 for that line. If a function is specified, probe the marker at start\n\
16953 of code for that function. If an address is specified, probe the marker\n\
16954 at that exact address. If a marker id is specified, probe the marker\n\
16955 with that name. With no LOCATION, uses current execution address of\n\
16956 the selected stack frame.\n\
16957 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16958 This collects arbitrary user data passed in the probe point call to the\n\
16959 tracing library. You can inspect it when analyzing the trace buffer,\n\
16960 by printing the $_sdata variable like any other convenience variable.\n\
16961 \n\
16962 CONDITION is a boolean expression.\n\
16963 \n\
16964 Multiple tracepoints at one place are permitted, and useful if their\n\
16965 conditions are different.\n\
16966 \n\
16967 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16968 Do \"help tracepoints\" for info on other tracepoint commands."));
16969 set_cmd_completer (c, location_completer);
16970
16971 add_info ("tracepoints", tracepoints_info, _("\
16972 Status of specified tracepoints (all tracepoints if no argument).\n\
16973 Convenience variable \"$tpnum\" contains the number of the\n\
16974 last tracepoint set."));
16975
16976 add_info_alias ("tp", "tracepoints", 1);
16977
16978 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16979 Delete specified tracepoints.\n\
16980 Arguments are tracepoint numbers, separated by spaces.\n\
16981 No argument means delete all tracepoints."),
16982 &deletelist);
16983 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16984
16985 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16986 Disable specified tracepoints.\n\
16987 Arguments are tracepoint numbers, separated by spaces.\n\
16988 No argument means disable all tracepoints."),
16989 &disablelist);
16990 deprecate_cmd (c, "disable");
16991
16992 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16993 Enable specified tracepoints.\n\
16994 Arguments are tracepoint numbers, separated by spaces.\n\
16995 No argument means enable all tracepoints."),
16996 &enablelist);
16997 deprecate_cmd (c, "enable");
16998
16999 add_com ("passcount", class_trace, trace_pass_command, _("\
17000 Set the passcount for a tracepoint.\n\
17001 The trace will end when the tracepoint has been passed 'count' times.\n\
17002 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
17003 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
17004
17005 add_prefix_cmd ("save", class_breakpoint, save_command,
17006 _("Save breakpoint definitions as a script."),
17007 &save_cmdlist, "save ",
17008 0/*allow-unknown*/, &cmdlist);
17009
17010 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
17011 Save current breakpoint definitions as a script.\n\
17012 This includes all types of breakpoints (breakpoints, watchpoints,\n\
17013 catchpoints, tracepoints). Use the 'source' command in another debug\n\
17014 session to restore them."),
17015 &save_cmdlist);
17016 set_cmd_completer (c, filename_completer);
17017
17018 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
17019 Save current tracepoint definitions as a script.\n\
17020 Use the 'source' command in another debug session to restore them."),
17021 &save_cmdlist);
17022 set_cmd_completer (c, filename_completer);
17023
17024 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
17025 deprecate_cmd (c, "save tracepoints");
17026
17027 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
17028 Breakpoint specific settings\n\
17029 Configure various breakpoint-specific variables such as\n\
17030 pending breakpoint behavior"),
17031 &breakpoint_set_cmdlist, "set breakpoint ",
17032 0/*allow-unknown*/, &setlist);
17033 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
17034 Breakpoint specific settings\n\
17035 Configure various breakpoint-specific variables such as\n\
17036 pending breakpoint behavior"),
17037 &breakpoint_show_cmdlist, "show breakpoint ",
17038 0/*allow-unknown*/, &showlist);
17039
17040 add_setshow_auto_boolean_cmd ("pending", no_class,
17041 &pending_break_support, _("\
17042 Set debugger's behavior regarding pending breakpoints."), _("\
17043 Show debugger's behavior regarding pending breakpoints."), _("\
17044 If on, an unrecognized breakpoint location will cause gdb to create a\n\
17045 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
17046 an error. If auto, an unrecognized breakpoint location results in a\n\
17047 user-query to see if a pending breakpoint should be created."),
17048 NULL,
17049 show_pending_break_support,
17050 &breakpoint_set_cmdlist,
17051 &breakpoint_show_cmdlist);
17052
17053 pending_break_support = AUTO_BOOLEAN_AUTO;
17054
17055 add_setshow_boolean_cmd ("auto-hw", no_class,
17056 &automatic_hardware_breakpoints, _("\
17057 Set automatic usage of hardware breakpoints."), _("\
17058 Show automatic usage of hardware breakpoints."), _("\
17059 If set, the debugger will automatically use hardware breakpoints for\n\
17060 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
17061 a warning will be emitted for such breakpoints."),
17062 NULL,
17063 show_automatic_hardware_breakpoints,
17064 &breakpoint_set_cmdlist,
17065 &breakpoint_show_cmdlist);
17066
17067 add_setshow_boolean_cmd ("always-inserted", class_support,
17068 &always_inserted_mode, _("\
17069 Set mode for inserting breakpoints."), _("\
17070 Show mode for inserting breakpoints."), _("\
17071 When this mode is on, breakpoints are inserted immediately as soon as\n\
17072 they're created, kept inserted even when execution stops, and removed\n\
17073 only when the user deletes them. When this mode is off (the default),\n\
17074 breakpoints are inserted only when execution continues, and removed\n\
17075 when execution stops."),
17076 NULL,
17077 &show_always_inserted_mode,
17078 &breakpoint_set_cmdlist,
17079 &breakpoint_show_cmdlist);
17080
17081 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
17082 condition_evaluation_enums,
17083 &condition_evaluation_mode_1, _("\
17084 Set mode of breakpoint condition evaluation."), _("\
17085 Show mode of breakpoint condition evaluation."), _("\
17086 When this is set to \"host\", breakpoint conditions will be\n\
17087 evaluated on the host's side by GDB. When it is set to \"target\",\n\
17088 breakpoint conditions will be downloaded to the target (if the target\n\
17089 supports such feature) and conditions will be evaluated on the target's side.\n\
17090 If this is set to \"auto\" (default), this will be automatically set to\n\
17091 \"target\" if it supports condition evaluation, otherwise it will\n\
17092 be set to \"gdb\""),
17093 &set_condition_evaluation_mode,
17094 &show_condition_evaluation_mode,
17095 &breakpoint_set_cmdlist,
17096 &breakpoint_show_cmdlist);
17097
17098 add_com ("break-range", class_breakpoint, break_range_command, _("\
17099 Set a breakpoint for an address range.\n\
17100 break-range START-LOCATION, END-LOCATION\n\
17101 where START-LOCATION and END-LOCATION can be one of the following:\n\
17102 LINENUM, for that line in the current file,\n\
17103 FILE:LINENUM, for that line in that file,\n\
17104 +OFFSET, for that number of lines after the current line\n\
17105 or the start of the range\n\
17106 FUNCTION, for the first line in that function,\n\
17107 FILE:FUNCTION, to distinguish among like-named static functions.\n\
17108 *ADDRESS, for the instruction at that address.\n\
17109 \n\
17110 The breakpoint will stop execution of the inferior whenever it executes\n\
17111 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
17112 range (including START-LOCATION and END-LOCATION)."));
17113
17114 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
17115 Set a dynamic printf at specified line or function.\n\
17116 dprintf location,format string,arg1,arg2,...\n\
17117 location may be a line number, function name, or \"*\" and an address.\n\
17118 If a line number is specified, break at start of code for that line.\n\
17119 If a function is specified, break at start of code for that function."));
17120 set_cmd_completer (c, location_completer);
17121
17122 add_setshow_enum_cmd ("dprintf-style", class_support,
17123 dprintf_style_enums, &dprintf_style, _("\
17124 Set the style of usage for dynamic printf."), _("\
17125 Show the style of usage for dynamic printf."), _("\
17126 This setting chooses how GDB will do a dynamic printf.\n\
17127 If the value is \"gdb\", then the printing is done by GDB to its own\n\
17128 console, as with the \"printf\" command.\n\
17129 If the value is \"call\", the print is done by calling a function in your\n\
17130 program; by default printf(), but you can choose a different function or\n\
17131 output stream by setting dprintf-function and dprintf-channel."),
17132 update_dprintf_commands, NULL,
17133 &setlist, &showlist);
17134
17135 dprintf_function = xstrdup ("printf");
17136 add_setshow_string_cmd ("dprintf-function", class_support,
17137 &dprintf_function, _("\
17138 Set the function to use for dynamic printf"), _("\
17139 Show the function to use for dynamic printf"), NULL,
17140 update_dprintf_commands, NULL,
17141 &setlist, &showlist);
17142
17143 dprintf_channel = xstrdup ("");
17144 add_setshow_string_cmd ("dprintf-channel", class_support,
17145 &dprintf_channel, _("\
17146 Set the channel to use for dynamic printf"), _("\
17147 Show the channel to use for dynamic printf"), NULL,
17148 update_dprintf_commands, NULL,
17149 &setlist, &showlist);
17150
17151 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
17152 &disconnected_dprintf, _("\
17153 Set whether dprintf continues after GDB disconnects."), _("\
17154 Show whether dprintf continues after GDB disconnects."), _("\
17155 Use this to let dprintf commands continue to hit and produce output\n\
17156 even if GDB disconnects or detaches from the target."),
17157 NULL,
17158 NULL,
17159 &setlist, &showlist);
17160
17161 add_com ("agent-printf", class_vars, agent_printf_command, _("\
17162 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
17163 (target agent only) This is useful for formatted output in user-defined commands."));
17164
17165 automatic_hardware_breakpoints = 1;
17166
17167 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
17168 observer_attach_thread_exit (remove_threaded_breakpoints);
17169 }
This page took 0.379907 seconds and 5 git commands to generate.