Don't memset non-POD types: struct bp_location
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2017 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 "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "thread-fsm.h"
71 #include "tid-parse.h"
72
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83
84 /* Enums for exception-handling support. */
85 enum exception_event_kind
86 {
87 EX_EVENT_THROW,
88 EX_EVENT_RETHROW,
89 EX_EVENT_CATCH
90 };
91
92 /* Prototypes for local functions. */
93
94 static void enable_delete_command (char *, int);
95
96 static void enable_once_command (char *, int);
97
98 static void enable_count_command (char *, int);
99
100 static void disable_command (char *, int);
101
102 static void enable_command (char *, int);
103
104 static void map_breakpoint_numbers (const char *,
105 void (*) (struct breakpoint *,
106 void *),
107 void *);
108
109 static void ignore_command (char *, int);
110
111 static int breakpoint_re_set_one (void *);
112
113 static void breakpoint_re_set_default (struct breakpoint *);
114
115 static void
116 create_sals_from_location_default (const struct event_location *location,
117 struct linespec_result *canonical,
118 enum bptype type_wanted);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
122 char *, char *, enum bptype,
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
126 int, int, int, unsigned);
127
128 static void decode_location_default (struct breakpoint *b,
129 const struct event_location *location,
130 struct program_space *search_pspace,
131 struct symtabs_and_lines *sals);
132
133 static void clear_command (char *, int);
134
135 static void catch_command (char *, int);
136
137 static int can_use_hardware_watchpoint (struct value *);
138
139 static void break_command_1 (char *, int, int);
140
141 static void mention (struct breakpoint *);
142
143 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
145 const struct breakpoint_ops *);
146 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
149 /* This function is used in gdbtk sources and thus can not be made
150 static. */
151 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
152 struct symtab_and_line,
153 enum bptype,
154 const struct breakpoint_ops *);
155
156 static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
161
162 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
164 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
166 enum bptype bptype);
167
168 static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
170 struct obj_section *, int);
171
172 static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
175 static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
179 static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
183 static void breakpoints_info (char *, int);
184
185 static void watchpoints_info (char *, int);
186
187 static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
189
190 static int breakpoint_cond_eval (void *);
191
192 static void cleanup_executing_breakpoints (void *);
193
194 static void commands_command (char *, int);
195
196 static void condition_command (char *, int);
197
198 static int remove_breakpoint (struct bp_location *);
199 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
200
201 static enum print_stop_action print_bp_stop_message (bpstat bs);
202
203 static int watchpoint_check (void *);
204
205 static void maintenance_info_breakpoints (char *, int);
206
207 static int hw_breakpoint_used_count (void);
208
209 static int hw_watchpoint_use_count (struct breakpoint *);
210
211 static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
214
215 static void hbreak_command (char *, int);
216
217 static void thbreak_command (char *, int);
218
219 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
221
222 static void stop_command (char *arg, int from_tty);
223
224 static void stopin_command (char *arg, int from_tty);
225
226 static void stopat_command (char *arg, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void free_bp_location (struct bp_location *loc);
231 static void incref_bp_location (struct bp_location *loc);
232 static void decref_bp_location (struct bp_location **loc);
233
234 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
235
236 /* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238 enum ugll_insert_mode
239 {
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
259 UGLL_MAY_INSERT,
260
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
268 UGLL_INSERT
269 };
270
271 static void update_global_location_list (enum ugll_insert_mode);
272
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274
275 static int is_hardware_watchpoint (const struct breakpoint *bpt);
276
277 static void insert_breakpoint_locations (void);
278
279 static void tracepoints_info (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type. */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type. */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312 static struct breakpoint_ops longjmp_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function;
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel;
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 struct thread_info *tp;
475
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
483 if (threads_are_executing ())
484 return 1;
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
492 }
493 return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation. */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation. */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to. */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552 }
553
554 void _initialize_breakpoint (void);
555
556 /* Are we executing breakpoint commands? */
557 static int executing_breakpoint_commands;
558
559 /* Are overlay event breakpoints enabled? */
560 static int overlay_events_enabled;
561
562 /* See description in breakpoint.h. */
563 int target_exact_watchpoints = 0;
564
565 /* Walk the following statement or block through all breakpoints.
566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
567 current breakpoint. */
568
569 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
570
571 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
575
576 /* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
579
580 #define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_locations; \
582 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
583 BP_TMP++)
584
585 /* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
599 /* Iterator for tracepoints only. */
600
601 #define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
603 if (is_tracepoint (B))
604
605 /* Chains of all breakpoints defined. */
606
607 struct breakpoint *breakpoint_chain;
608
609 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
610
611 static struct bp_location **bp_locations;
612
613 /* Number of elements of BP_LOCATIONS. */
614
615 static unsigned bp_locations_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATIONS which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
621 an address you need to read. */
622
623 static CORE_ADDR bp_locations_placed_address_before_address_max;
624
625 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATIONS to
629 scan for shadow bytes for an address you need to read. */
630
631 static CORE_ADDR bp_locations_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634 from the bp_locations array, but for which a hit may still be
635 reported by a target. */
636 VEC(bp_location_p) *moribund_locations = NULL;
637
638 /* Number of last breakpoint made. */
639
640 static int breakpoint_count;
641
642 /* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646 static int prev_breakpoint_count;
647
648 /* Number of last tracepoint made. */
649
650 static int tracepoint_count;
651
652 static struct cmd_list_element *breakpoint_set_cmdlist;
653 static struct cmd_list_element *breakpoint_show_cmdlist;
654 struct cmd_list_element *save_cmdlist;
655
656 /* See declaration at breakpoint.h. */
657
658 struct breakpoint *
659 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661 {
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671 }
672
673 /* Return whether a breakpoint is an active enabled breakpoint. */
674 static int
675 breakpoint_enabled (struct breakpoint *b)
676 {
677 return (b->enable_state == bp_enabled);
678 }
679
680 /* Set breakpoint count to NUM. */
681
682 static void
683 set_breakpoint_count (int num)
684 {
685 prev_breakpoint_count = breakpoint_count;
686 breakpoint_count = num;
687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
688 }
689
690 /* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692 static int rbreak_start_breakpoint_count;
693
694 /* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
696
697 void
698 start_rbreak_breakpoints (void)
699 {
700 rbreak_start_breakpoint_count = breakpoint_count;
701 }
702
703 /* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
705
706 void
707 end_rbreak_breakpoints (void)
708 {
709 prev_breakpoint_count = rbreak_start_breakpoint_count;
710 }
711
712 /* Used in run_command to zero the hit count when a new run starts. */
713
714 void
715 clear_breakpoint_hit_counts (void)
716 {
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721 }
722
723 /* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726 static struct counted_command_line *
727 alloc_counted_command_line (struct command_line *commands)
728 {
729 struct counted_command_line *result = XNEW (struct counted_command_line);
730
731 result->refc = 1;
732 result->commands = commands;
733
734 return result;
735 }
736
737 /* Increment reference count. This does nothing if CMD is NULL. */
738
739 static void
740 incref_counted_command_line (struct counted_command_line *cmd)
741 {
742 if (cmd)
743 ++cmd->refc;
744 }
745
746 /* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750 static void
751 decref_counted_command_line (struct counted_command_line **cmdp)
752 {
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762 }
763
764 /* A cleanup function that calls decref_counted_command_line. */
765
766 static void
767 do_cleanup_counted_command_line (void *arg)
768 {
769 decref_counted_command_line ((struct counted_command_line **) arg);
770 }
771
772 /* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775 static struct cleanup *
776 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777 {
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779 }
780
781 \f
782 /* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785 struct breakpoint *
786 get_breakpoint (int num)
787 {
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795 }
796
797 \f
798
799 /* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802 static void
803 mark_breakpoint_modified (struct breakpoint *b)
804 {
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820 }
821
822 /* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825 static void
826 mark_breakpoint_location_modified (struct bp_location *loc)
827 {
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841 }
842
843 /* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846 static void
847 set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849 {
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
897 update_global_location_list (UGLL_MAY_INSERT);
898 }
899
900 return;
901 }
902
903 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906 static void
907 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909 {
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919 }
920
921 /* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_locations_compare function. */
924
925 static int
926 bp_locations_compare_addrs (const void *ap, const void *bp)
927 {
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935 }
936
937 /* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942 static struct bp_location **
943 get_first_locp_gte_addr (CORE_ADDR address)
944 {
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
953 locp_found = ((struct bp_location **)
954 bsearch (&dummy_locp, bp_locations, bp_locations_count,
955 sizeof (struct bp_location **),
956 bp_locations_compare_addrs));
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
964 while ((locp_found - 1) >= bp_locations
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969 }
970
971 void
972 set_breakpoint_condition (struct breakpoint *b, const char *exp,
973 int from_tty)
974 {
975 xfree (b->cond_string);
976 b->cond_string = NULL;
977
978 if (is_watchpoint (b))
979 {
980 struct watchpoint *w = (struct watchpoint *) b;
981
982 w->cond_exp.reset ();
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
990 loc->cond.reset ();
991
992 /* No need to free the condition agent expression
993 bytecode (if we have one). We will handle this
994 when we go through update_global_location_list. */
995 }
996 }
997
998 if (*exp == 0)
999 {
1000 if (from_tty)
1001 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1002 }
1003 else
1004 {
1005 const char *arg = exp;
1006
1007 /* I don't know if it matters whether this is the string the user
1008 typed in or the decompiled expression. */
1009 b->cond_string = xstrdup (arg);
1010 b->condition_not_parsed = 0;
1011
1012 if (is_watchpoint (b))
1013 {
1014 struct watchpoint *w = (struct watchpoint *) b;
1015
1016 innermost_block = NULL;
1017 arg = exp;
1018 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1019 if (*arg)
1020 error (_("Junk at end of expression"));
1021 w->cond_exp_valid_block = innermost_block;
1022 }
1023 else
1024 {
1025 struct bp_location *loc;
1026
1027 for (loc = b->loc; loc; loc = loc->next)
1028 {
1029 arg = exp;
1030 loc->cond =
1031 parse_exp_1 (&arg, loc->address,
1032 block_for_pc (loc->address), 0);
1033 if (*arg)
1034 error (_("Junk at end of expression"));
1035 }
1036 }
1037 }
1038 mark_breakpoint_modified (b);
1039
1040 observer_notify_breakpoint_modified (b);
1041 }
1042
1043 /* Completion for the "condition" command. */
1044
1045 static VEC (char_ptr) *
1046 condition_completer (struct cmd_list_element *cmd,
1047 const char *text, const char *word)
1048 {
1049 const char *space;
1050
1051 text = skip_spaces_const (text);
1052 space = skip_to_space_const (text);
1053 if (*space == '\0')
1054 {
1055 int len;
1056 struct breakpoint *b;
1057 VEC (char_ptr) *result = NULL;
1058
1059 if (text[0] == '$')
1060 {
1061 /* We don't support completion of history indices. */
1062 if (isdigit (text[1]))
1063 return NULL;
1064 return complete_internalvar (&text[1]);
1065 }
1066
1067 /* We're completing the breakpoint number. */
1068 len = strlen (text);
1069
1070 ALL_BREAKPOINTS (b)
1071 {
1072 char number[50];
1073
1074 xsnprintf (number, sizeof (number), "%d", b->number);
1075
1076 if (strncmp (number, text, len) == 0)
1077 VEC_safe_push (char_ptr, result, xstrdup (number));
1078 }
1079
1080 return result;
1081 }
1082
1083 /* We're completing the expression part. */
1084 text = skip_spaces_const (space);
1085 return expression_completer (cmd, text, word);
1086 }
1087
1088 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1089
1090 static void
1091 condition_command (char *arg, int from_tty)
1092 {
1093 struct breakpoint *b;
1094 char *p;
1095 int bnum;
1096
1097 if (arg == 0)
1098 error_no_arg (_("breakpoint number"));
1099
1100 p = arg;
1101 bnum = get_number (&p);
1102 if (bnum == 0)
1103 error (_("Bad breakpoint argument: '%s'"), arg);
1104
1105 ALL_BREAKPOINTS (b)
1106 if (b->number == bnum)
1107 {
1108 /* Check if this breakpoint has a "stop" method implemented in an
1109 extension language. This method and conditions entered into GDB
1110 from the CLI are mutually exclusive. */
1111 const struct extension_language_defn *extlang
1112 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1113
1114 if (extlang != NULL)
1115 {
1116 error (_("Only one stop condition allowed. There is currently"
1117 " a %s stop condition defined for this breakpoint."),
1118 ext_lang_capitalized_name (extlang));
1119 }
1120 set_breakpoint_condition (b, p, from_tty);
1121
1122 if (is_breakpoint (b))
1123 update_global_location_list (UGLL_MAY_INSERT);
1124
1125 return;
1126 }
1127
1128 error (_("No breakpoint number %d."), bnum);
1129 }
1130
1131 /* Check that COMMAND do not contain commands that are suitable
1132 only for tracepoints and not suitable for ordinary breakpoints.
1133 Throw if any such commands is found. */
1134
1135 static void
1136 check_no_tracepoint_commands (struct command_line *commands)
1137 {
1138 struct command_line *c;
1139
1140 for (c = commands; c; c = c->next)
1141 {
1142 int i;
1143
1144 if (c->control_type == while_stepping_control)
1145 error (_("The 'while-stepping' command can "
1146 "only be used for tracepoints"));
1147
1148 for (i = 0; i < c->body_count; ++i)
1149 check_no_tracepoint_commands ((c->body_list)[i]);
1150
1151 /* Not that command parsing removes leading whitespace and comment
1152 lines and also empty lines. So, we only need to check for
1153 command directly. */
1154 if (strstr (c->line, "collect ") == c->line)
1155 error (_("The 'collect' command can only be used for tracepoints"));
1156
1157 if (strstr (c->line, "teval ") == c->line)
1158 error (_("The 'teval' command can only be used for tracepoints"));
1159 }
1160 }
1161
1162 /* Encapsulate tests for different types of tracepoints. */
1163
1164 static int
1165 is_tracepoint_type (enum bptype type)
1166 {
1167 return (type == bp_tracepoint
1168 || type == bp_fast_tracepoint
1169 || type == bp_static_tracepoint);
1170 }
1171
1172 int
1173 is_tracepoint (const struct breakpoint *b)
1174 {
1175 return is_tracepoint_type (b->type);
1176 }
1177
1178 /* A helper function that validates that COMMANDS are valid for a
1179 breakpoint. This function will throw an exception if a problem is
1180 found. */
1181
1182 static void
1183 validate_commands_for_breakpoint (struct breakpoint *b,
1184 struct command_line *commands)
1185 {
1186 if (is_tracepoint (b))
1187 {
1188 struct tracepoint *t = (struct tracepoint *) b;
1189 struct command_line *c;
1190 struct command_line *while_stepping = 0;
1191
1192 /* Reset the while-stepping step count. The previous commands
1193 might have included a while-stepping action, while the new
1194 ones might not. */
1195 t->step_count = 0;
1196
1197 /* We need to verify that each top-level element of commands is
1198 valid for tracepoints, that there's at most one
1199 while-stepping element, and that the while-stepping's body
1200 has valid tracing commands excluding nested while-stepping.
1201 We also need to validate the tracepoint action line in the
1202 context of the tracepoint --- validate_actionline actually
1203 has side effects, like setting the tracepoint's
1204 while-stepping STEP_COUNT, in addition to checking if the
1205 collect/teval actions parse and make sense in the
1206 tracepoint's context. */
1207 for (c = commands; c; c = c->next)
1208 {
1209 if (c->control_type == while_stepping_control)
1210 {
1211 if (b->type == bp_fast_tracepoint)
1212 error (_("The 'while-stepping' command "
1213 "cannot be used for fast tracepoint"));
1214 else if (b->type == bp_static_tracepoint)
1215 error (_("The 'while-stepping' command "
1216 "cannot be used for static tracepoint"));
1217
1218 if (while_stepping)
1219 error (_("The 'while-stepping' command "
1220 "can be used only once"));
1221 else
1222 while_stepping = c;
1223 }
1224
1225 validate_actionline (c->line, b);
1226 }
1227 if (while_stepping)
1228 {
1229 struct command_line *c2;
1230
1231 gdb_assert (while_stepping->body_count == 1);
1232 c2 = while_stepping->body_list[0];
1233 for (; c2; c2 = c2->next)
1234 {
1235 if (c2->control_type == while_stepping_control)
1236 error (_("The 'while-stepping' command cannot be nested"));
1237 }
1238 }
1239 }
1240 else
1241 {
1242 check_no_tracepoint_commands (commands);
1243 }
1244 }
1245
1246 /* Return a vector of all the static tracepoints set at ADDR. The
1247 caller is responsible for releasing the vector. */
1248
1249 VEC(breakpoint_p) *
1250 static_tracepoints_here (CORE_ADDR addr)
1251 {
1252 struct breakpoint *b;
1253 VEC(breakpoint_p) *found = 0;
1254 struct bp_location *loc;
1255
1256 ALL_BREAKPOINTS (b)
1257 if (b->type == bp_static_tracepoint)
1258 {
1259 for (loc = b->loc; loc; loc = loc->next)
1260 if (loc->address == addr)
1261 VEC_safe_push(breakpoint_p, found, b);
1262 }
1263
1264 return found;
1265 }
1266
1267 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1268 validate that only allowed commands are included. */
1269
1270 void
1271 breakpoint_set_commands (struct breakpoint *b,
1272 command_line_up &&commands)
1273 {
1274 validate_commands_for_breakpoint (b, commands.get ());
1275
1276 decref_counted_command_line (&b->commands);
1277 b->commands = alloc_counted_command_line (commands.release ());
1278 observer_notify_breakpoint_modified (b);
1279 }
1280
1281 /* Set the internal `silent' flag on the breakpoint. Note that this
1282 is not the same as the "silent" that may appear in the breakpoint's
1283 commands. */
1284
1285 void
1286 breakpoint_set_silent (struct breakpoint *b, int silent)
1287 {
1288 int old_silent = b->silent;
1289
1290 b->silent = silent;
1291 if (old_silent != silent)
1292 observer_notify_breakpoint_modified (b);
1293 }
1294
1295 /* Set the thread for this breakpoint. If THREAD is -1, make the
1296 breakpoint work for any thread. */
1297
1298 void
1299 breakpoint_set_thread (struct breakpoint *b, int thread)
1300 {
1301 int old_thread = b->thread;
1302
1303 b->thread = thread;
1304 if (old_thread != thread)
1305 observer_notify_breakpoint_modified (b);
1306 }
1307
1308 /* Set the task for this breakpoint. If TASK is 0, make the
1309 breakpoint work for any task. */
1310
1311 void
1312 breakpoint_set_task (struct breakpoint *b, int task)
1313 {
1314 int old_task = b->task;
1315
1316 b->task = task;
1317 if (old_task != task)
1318 observer_notify_breakpoint_modified (b);
1319 }
1320
1321 void
1322 check_tracepoint_command (char *line, void *closure)
1323 {
1324 struct breakpoint *b = (struct breakpoint *) closure;
1325
1326 validate_actionline (line, b);
1327 }
1328
1329 /* A structure used to pass information through
1330 map_breakpoint_numbers. */
1331
1332 struct commands_info
1333 {
1334 /* True if the command was typed at a tty. */
1335 int from_tty;
1336
1337 /* The breakpoint range spec. */
1338 const char *arg;
1339
1340 /* Non-NULL if the body of the commands are being read from this
1341 already-parsed command. */
1342 struct command_line *control;
1343
1344 /* The command lines read from the user, or NULL if they have not
1345 yet been read. */
1346 struct counted_command_line *cmd;
1347 };
1348
1349 /* A callback for map_breakpoint_numbers that sets the commands for
1350 commands_command. */
1351
1352 static void
1353 do_map_commands_command (struct breakpoint *b, void *data)
1354 {
1355 struct commands_info *info = (struct commands_info *) data;
1356
1357 if (info->cmd == NULL)
1358 {
1359 command_line_up l;
1360
1361 if (info->control != NULL)
1362 l = copy_command_lines (info->control->body_list[0]);
1363 else
1364 {
1365 struct cleanup *old_chain;
1366 char *str;
1367
1368 str = xstrprintf (_("Type commands for breakpoint(s) "
1369 "%s, one per line."),
1370 info->arg);
1371
1372 old_chain = make_cleanup (xfree, str);
1373
1374 l = read_command_lines (str,
1375 info->from_tty, 1,
1376 (is_tracepoint (b)
1377 ? check_tracepoint_command : 0),
1378 b);
1379
1380 do_cleanups (old_chain);
1381 }
1382
1383 info->cmd = alloc_counted_command_line (l.release ());
1384 }
1385
1386 /* If a breakpoint was on the list more than once, we don't need to
1387 do anything. */
1388 if (b->commands != info->cmd)
1389 {
1390 validate_commands_for_breakpoint (b, info->cmd->commands);
1391 incref_counted_command_line (info->cmd);
1392 decref_counted_command_line (&b->commands);
1393 b->commands = info->cmd;
1394 observer_notify_breakpoint_modified (b);
1395 }
1396 }
1397
1398 static void
1399 commands_command_1 (const char *arg, int from_tty,
1400 struct command_line *control)
1401 {
1402 struct cleanup *cleanups;
1403 struct commands_info info;
1404
1405 info.from_tty = from_tty;
1406 info.control = control;
1407 info.cmd = NULL;
1408 /* If we read command lines from the user, then `info' will hold an
1409 extra reference to the commands that we must clean up. */
1410 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1411
1412 std::string new_arg;
1413
1414 if (arg == NULL || !*arg)
1415 {
1416 if (breakpoint_count - prev_breakpoint_count > 1)
1417 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1418 breakpoint_count);
1419 else if (breakpoint_count > 0)
1420 new_arg = string_printf ("%d", breakpoint_count);
1421 }
1422 else
1423 new_arg = arg;
1424
1425 info.arg = new_arg.c_str ();
1426
1427 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1428
1429 if (info.cmd == NULL)
1430 error (_("No breakpoints specified."));
1431
1432 do_cleanups (cleanups);
1433 }
1434
1435 static void
1436 commands_command (char *arg, int from_tty)
1437 {
1438 commands_command_1 (arg, from_tty, NULL);
1439 }
1440
1441 /* Like commands_command, but instead of reading the commands from
1442 input stream, takes them from an already parsed command structure.
1443
1444 This is used by cli-script.c to DTRT with breakpoint commands
1445 that are part of if and while bodies. */
1446 enum command_control_type
1447 commands_from_control_command (const char *arg, struct command_line *cmd)
1448 {
1449 commands_command_1 (arg, 0, cmd);
1450 return simple_control;
1451 }
1452
1453 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1454
1455 static int
1456 bp_location_has_shadow (struct bp_location *bl)
1457 {
1458 if (bl->loc_type != bp_loc_software_breakpoint)
1459 return 0;
1460 if (!bl->inserted)
1461 return 0;
1462 if (bl->target_info.shadow_len == 0)
1463 /* BL isn't valid, or doesn't shadow memory. */
1464 return 0;
1465 return 1;
1466 }
1467
1468 /* Update BUF, which is LEN bytes read from the target address
1469 MEMADDR, by replacing a memory breakpoint with its shadowed
1470 contents.
1471
1472 If READBUF is not NULL, this buffer must not overlap with the of
1473 the breakpoint location's shadow_contents buffer. Otherwise, a
1474 failed assertion internal error will be raised. */
1475
1476 static void
1477 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1478 const gdb_byte *writebuf_org,
1479 ULONGEST memaddr, LONGEST len,
1480 struct bp_target_info *target_info,
1481 struct gdbarch *gdbarch)
1482 {
1483 /* Now do full processing of the found relevant range of elements. */
1484 CORE_ADDR bp_addr = 0;
1485 int bp_size = 0;
1486 int bptoffset = 0;
1487
1488 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1489 current_program_space->aspace, 0))
1490 {
1491 /* The breakpoint is inserted in a different address space. */
1492 return;
1493 }
1494
1495 /* Addresses and length of the part of the breakpoint that
1496 we need to copy. */
1497 bp_addr = target_info->placed_address;
1498 bp_size = target_info->shadow_len;
1499
1500 if (bp_addr + bp_size <= memaddr)
1501 {
1502 /* The breakpoint is entirely before the chunk of memory we are
1503 reading. */
1504 return;
1505 }
1506
1507 if (bp_addr >= memaddr + len)
1508 {
1509 /* The breakpoint is entirely after the chunk of memory we are
1510 reading. */
1511 return;
1512 }
1513
1514 /* Offset within shadow_contents. */
1515 if (bp_addr < memaddr)
1516 {
1517 /* Only copy the second part of the breakpoint. */
1518 bp_size -= memaddr - bp_addr;
1519 bptoffset = memaddr - bp_addr;
1520 bp_addr = memaddr;
1521 }
1522
1523 if (bp_addr + bp_size > memaddr + len)
1524 {
1525 /* Only copy the first part of the breakpoint. */
1526 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1527 }
1528
1529 if (readbuf != NULL)
1530 {
1531 /* Verify that the readbuf buffer does not overlap with the
1532 shadow_contents buffer. */
1533 gdb_assert (target_info->shadow_contents >= readbuf + len
1534 || readbuf >= (target_info->shadow_contents
1535 + target_info->shadow_len));
1536
1537 /* Update the read buffer with this inserted breakpoint's
1538 shadow. */
1539 memcpy (readbuf + bp_addr - memaddr,
1540 target_info->shadow_contents + bptoffset, bp_size);
1541 }
1542 else
1543 {
1544 const unsigned char *bp;
1545 CORE_ADDR addr = target_info->reqstd_address;
1546 int placed_size;
1547
1548 /* Update the shadow with what we want to write to memory. */
1549 memcpy (target_info->shadow_contents + bptoffset,
1550 writebuf_org + bp_addr - memaddr, bp_size);
1551
1552 /* Determine appropriate breakpoint contents and size for this
1553 address. */
1554 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1555
1556 /* Update the final write buffer with this inserted
1557 breakpoint's INSN. */
1558 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1559 }
1560 }
1561
1562 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1563 by replacing any memory breakpoints with their shadowed contents.
1564
1565 If READBUF is not NULL, this buffer must not overlap with any of
1566 the breakpoint location's shadow_contents buffers. Otherwise,
1567 a failed assertion internal error will be raised.
1568
1569 The range of shadowed area by each bp_location is:
1570 bl->address - bp_locations_placed_address_before_address_max
1571 up to bl->address + bp_locations_shadow_len_after_address_max
1572 The range we were requested to resolve shadows for is:
1573 memaddr ... memaddr + len
1574 Thus the safe cutoff boundaries for performance optimization are
1575 memaddr + len <= (bl->address
1576 - bp_locations_placed_address_before_address_max)
1577 and:
1578 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1579
1580 void
1581 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1582 const gdb_byte *writebuf_org,
1583 ULONGEST memaddr, LONGEST len)
1584 {
1585 /* Left boundary, right boundary and median element of our binary
1586 search. */
1587 unsigned bc_l, bc_r, bc;
1588
1589 /* Find BC_L which is a leftmost element which may affect BUF
1590 content. It is safe to report lower value but a failure to
1591 report higher one. */
1592
1593 bc_l = 0;
1594 bc_r = bp_locations_count;
1595 while (bc_l + 1 < bc_r)
1596 {
1597 struct bp_location *bl;
1598
1599 bc = (bc_l + bc_r) / 2;
1600 bl = bp_locations[bc];
1601
1602 /* Check first BL->ADDRESS will not overflow due to the added
1603 constant. Then advance the left boundary only if we are sure
1604 the BC element can in no way affect the BUF content (MEMADDR
1605 to MEMADDR + LEN range).
1606
1607 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1608 offset so that we cannot miss a breakpoint with its shadow
1609 range tail still reaching MEMADDR. */
1610
1611 if ((bl->address + bp_locations_shadow_len_after_address_max
1612 >= bl->address)
1613 && (bl->address + bp_locations_shadow_len_after_address_max
1614 <= memaddr))
1615 bc_l = bc;
1616 else
1617 bc_r = bc;
1618 }
1619
1620 /* Due to the binary search above, we need to make sure we pick the
1621 first location that's at BC_L's address. E.g., if there are
1622 multiple locations at the same address, BC_L may end up pointing
1623 at a duplicate location, and miss the "master"/"inserted"
1624 location. Say, given locations L1, L2 and L3 at addresses A and
1625 B:
1626
1627 L1@A, L2@A, L3@B, ...
1628
1629 BC_L could end up pointing at location L2, while the "master"
1630 location could be L1. Since the `loc->inserted' flag is only set
1631 on "master" locations, we'd forget to restore the shadow of L1
1632 and L2. */
1633 while (bc_l > 0
1634 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1635 bc_l--;
1636
1637 /* Now do full processing of the found relevant range of elements. */
1638
1639 for (bc = bc_l; bc < bp_locations_count; bc++)
1640 {
1641 struct bp_location *bl = bp_locations[bc];
1642
1643 /* bp_location array has BL->OWNER always non-NULL. */
1644 if (bl->owner->type == bp_none)
1645 warning (_("reading through apparently deleted breakpoint #%d?"),
1646 bl->owner->number);
1647
1648 /* Performance optimization: any further element can no longer affect BUF
1649 content. */
1650
1651 if (bl->address >= bp_locations_placed_address_before_address_max
1652 && memaddr + len <= (bl->address
1653 - bp_locations_placed_address_before_address_max))
1654 break;
1655
1656 if (!bp_location_has_shadow (bl))
1657 continue;
1658
1659 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1660 memaddr, len, &bl->target_info, bl->gdbarch);
1661 }
1662 }
1663
1664 \f
1665
1666 /* Return true if BPT is either a software breakpoint or a hardware
1667 breakpoint. */
1668
1669 int
1670 is_breakpoint (const struct breakpoint *bpt)
1671 {
1672 return (bpt->type == bp_breakpoint
1673 || bpt->type == bp_hardware_breakpoint
1674 || bpt->type == bp_dprintf);
1675 }
1676
1677 /* Return true if BPT is of any hardware watchpoint kind. */
1678
1679 static int
1680 is_hardware_watchpoint (const struct breakpoint *bpt)
1681 {
1682 return (bpt->type == bp_hardware_watchpoint
1683 || bpt->type == bp_read_watchpoint
1684 || bpt->type == bp_access_watchpoint);
1685 }
1686
1687 /* Return true if BPT is of any watchpoint kind, hardware or
1688 software. */
1689
1690 int
1691 is_watchpoint (const struct breakpoint *bpt)
1692 {
1693 return (is_hardware_watchpoint (bpt)
1694 || bpt->type == bp_watchpoint);
1695 }
1696
1697 /* Returns true if the current thread and its running state are safe
1698 to evaluate or update watchpoint B. Watchpoints on local
1699 expressions need to be evaluated in the context of the thread that
1700 was current when the watchpoint was created, and, that thread needs
1701 to be stopped to be able to select the correct frame context.
1702 Watchpoints on global expressions can be evaluated on any thread,
1703 and in any state. It is presently left to the target allowing
1704 memory accesses when threads are running. */
1705
1706 static int
1707 watchpoint_in_thread_scope (struct watchpoint *b)
1708 {
1709 return (b->base.pspace == current_program_space
1710 && (ptid_equal (b->watchpoint_thread, null_ptid)
1711 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1712 && !is_executing (inferior_ptid))));
1713 }
1714
1715 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1716 associated bp_watchpoint_scope breakpoint. */
1717
1718 static void
1719 watchpoint_del_at_next_stop (struct watchpoint *w)
1720 {
1721 struct breakpoint *b = &w->base;
1722
1723 if (b->related_breakpoint != b)
1724 {
1725 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1726 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1727 b->related_breakpoint->disposition = disp_del_at_next_stop;
1728 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1729 b->related_breakpoint = b;
1730 }
1731 b->disposition = disp_del_at_next_stop;
1732 }
1733
1734 /* Extract a bitfield value from value VAL using the bit parameters contained in
1735 watchpoint W. */
1736
1737 static struct value *
1738 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1739 {
1740 struct value *bit_val;
1741
1742 if (val == NULL)
1743 return NULL;
1744
1745 bit_val = allocate_value (value_type (val));
1746
1747 unpack_value_bitfield (bit_val,
1748 w->val_bitpos,
1749 w->val_bitsize,
1750 value_contents_for_printing (val),
1751 value_offset (val),
1752 val);
1753
1754 return bit_val;
1755 }
1756
1757 /* Allocate a dummy location and add it to B, which must be a software
1758 watchpoint. This is required because even if a software watchpoint
1759 is not watching any memory, bpstat_stop_status requires a location
1760 to be able to report stops. */
1761
1762 static void
1763 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1764 struct program_space *pspace)
1765 {
1766 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1767
1768 b->loc = allocate_bp_location (b);
1769 b->loc->pspace = pspace;
1770 b->loc->address = -1;
1771 b->loc->length = -1;
1772 }
1773
1774 /* Returns true if B is a software watchpoint that is not watching any
1775 memory (e.g., "watch $pc"). */
1776
1777 static int
1778 is_no_memory_software_watchpoint (struct breakpoint *b)
1779 {
1780 return (b->type == bp_watchpoint
1781 && b->loc != NULL
1782 && b->loc->next == NULL
1783 && b->loc->address == -1
1784 && b->loc->length == -1);
1785 }
1786
1787 /* Assuming that B is a watchpoint:
1788 - Reparse watchpoint expression, if REPARSE is non-zero
1789 - Evaluate expression and store the result in B->val
1790 - Evaluate the condition if there is one, and store the result
1791 in b->loc->cond.
1792 - Update the list of values that must be watched in B->loc.
1793
1794 If the watchpoint disposition is disp_del_at_next_stop, then do
1795 nothing. If this is local watchpoint that is out of scope, delete
1796 it.
1797
1798 Even with `set breakpoint always-inserted on' the watchpoints are
1799 removed + inserted on each stop here. Normal breakpoints must
1800 never be removed because they might be missed by a running thread
1801 when debugging in non-stop mode. On the other hand, hardware
1802 watchpoints (is_hardware_watchpoint; processed here) are specific
1803 to each LWP since they are stored in each LWP's hardware debug
1804 registers. Therefore, such LWP must be stopped first in order to
1805 be able to modify its hardware watchpoints.
1806
1807 Hardware watchpoints must be reset exactly once after being
1808 presented to the user. It cannot be done sooner, because it would
1809 reset the data used to present the watchpoint hit to the user. And
1810 it must not be done later because it could display the same single
1811 watchpoint hit during multiple GDB stops. Note that the latter is
1812 relevant only to the hardware watchpoint types bp_read_watchpoint
1813 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1814 not user-visible - its hit is suppressed if the memory content has
1815 not changed.
1816
1817 The following constraints influence the location where we can reset
1818 hardware watchpoints:
1819
1820 * target_stopped_by_watchpoint and target_stopped_data_address are
1821 called several times when GDB stops.
1822
1823 [linux]
1824 * Multiple hardware watchpoints can be hit at the same time,
1825 causing GDB to stop. GDB only presents one hardware watchpoint
1826 hit at a time as the reason for stopping, and all the other hits
1827 are presented later, one after the other, each time the user
1828 requests the execution to be resumed. Execution is not resumed
1829 for the threads still having pending hit event stored in
1830 LWP_INFO->STATUS. While the watchpoint is already removed from
1831 the inferior on the first stop the thread hit event is kept being
1832 reported from its cached value by linux_nat_stopped_data_address
1833 until the real thread resume happens after the watchpoint gets
1834 presented and thus its LWP_INFO->STATUS gets reset.
1835
1836 Therefore the hardware watchpoint hit can get safely reset on the
1837 watchpoint removal from inferior. */
1838
1839 static void
1840 update_watchpoint (struct watchpoint *b, int reparse)
1841 {
1842 int within_current_scope;
1843 struct frame_id saved_frame_id;
1844 int frame_saved;
1845
1846 /* If this is a local watchpoint, we only want to check if the
1847 watchpoint frame is in scope if the current thread is the thread
1848 that was used to create the watchpoint. */
1849 if (!watchpoint_in_thread_scope (b))
1850 return;
1851
1852 if (b->base.disposition == disp_del_at_next_stop)
1853 return;
1854
1855 frame_saved = 0;
1856
1857 /* Determine if the watchpoint is within scope. */
1858 if (b->exp_valid_block == NULL)
1859 within_current_scope = 1;
1860 else
1861 {
1862 struct frame_info *fi = get_current_frame ();
1863 struct gdbarch *frame_arch = get_frame_arch (fi);
1864 CORE_ADDR frame_pc = get_frame_pc (fi);
1865
1866 /* If we're at a point where the stack has been destroyed
1867 (e.g. in a function epilogue), unwinding may not work
1868 properly. Do not attempt to recreate locations at this
1869 point. See similar comments in watchpoint_check. */
1870 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1871 return;
1872
1873 /* Save the current frame's ID so we can restore it after
1874 evaluating the watchpoint expression on its own frame. */
1875 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1876 took a frame parameter, so that we didn't have to change the
1877 selected frame. */
1878 frame_saved = 1;
1879 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1880
1881 fi = frame_find_by_id (b->watchpoint_frame);
1882 within_current_scope = (fi != NULL);
1883 if (within_current_scope)
1884 select_frame (fi);
1885 }
1886
1887 /* We don't free locations. They are stored in the bp_location array
1888 and update_global_location_list will eventually delete them and
1889 remove breakpoints if needed. */
1890 b->base.loc = NULL;
1891
1892 if (within_current_scope && reparse)
1893 {
1894 const char *s;
1895
1896 b->exp.reset ();
1897 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1898 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1899 /* If the meaning of expression itself changed, the old value is
1900 no longer relevant. We don't want to report a watchpoint hit
1901 to the user when the old value and the new value may actually
1902 be completely different objects. */
1903 value_free (b->val);
1904 b->val = NULL;
1905 b->val_valid = 0;
1906
1907 /* Note that unlike with breakpoints, the watchpoint's condition
1908 expression is stored in the breakpoint object, not in the
1909 locations (re)created below. */
1910 if (b->base.cond_string != NULL)
1911 {
1912 b->cond_exp.reset ();
1913
1914 s = b->base.cond_string;
1915 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1916 }
1917 }
1918
1919 /* If we failed to parse the expression, for example because
1920 it refers to a global variable in a not-yet-loaded shared library,
1921 don't try to insert watchpoint. We don't automatically delete
1922 such watchpoint, though, since failure to parse expression
1923 is different from out-of-scope watchpoint. */
1924 if (!target_has_execution)
1925 {
1926 /* Without execution, memory can't change. No use to try and
1927 set watchpoint locations. The watchpoint will be reset when
1928 the target gains execution, through breakpoint_re_set. */
1929 if (!can_use_hw_watchpoints)
1930 {
1931 if (b->base.ops->works_in_software_mode (&b->base))
1932 b->base.type = bp_watchpoint;
1933 else
1934 error (_("Can't set read/access watchpoint when "
1935 "hardware watchpoints are disabled."));
1936 }
1937 }
1938 else if (within_current_scope && b->exp)
1939 {
1940 int pc = 0;
1941 struct value *val_chain, *v, *result, *next;
1942 struct program_space *frame_pspace;
1943
1944 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1945
1946 /* Avoid setting b->val if it's already set. The meaning of
1947 b->val is 'the last value' user saw, and we should update
1948 it only if we reported that last value to user. As it
1949 happens, the code that reports it updates b->val directly.
1950 We don't keep track of the memory value for masked
1951 watchpoints. */
1952 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1953 {
1954 if (b->val_bitsize != 0)
1955 {
1956 v = extract_bitfield_from_watchpoint_value (b, v);
1957 if (v != NULL)
1958 release_value (v);
1959 }
1960 b->val = v;
1961 b->val_valid = 1;
1962 }
1963
1964 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1965
1966 /* Look at each value on the value chain. */
1967 for (v = val_chain; v; v = value_next (v))
1968 {
1969 /* If it's a memory location, and GDB actually needed
1970 its contents to evaluate the expression, then we
1971 must watch it. If the first value returned is
1972 still lazy, that means an error occurred reading it;
1973 watch it anyway in case it becomes readable. */
1974 if (VALUE_LVAL (v) == lval_memory
1975 && (v == val_chain || ! value_lazy (v)))
1976 {
1977 struct type *vtype = check_typedef (value_type (v));
1978
1979 /* We only watch structs and arrays if user asked
1980 for it explicitly, never if they just happen to
1981 appear in the middle of some value chain. */
1982 if (v == result
1983 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1984 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1985 {
1986 CORE_ADDR addr;
1987 enum target_hw_bp_type type;
1988 struct bp_location *loc, **tmp;
1989 int bitpos = 0, bitsize = 0;
1990
1991 if (value_bitsize (v) != 0)
1992 {
1993 /* Extract the bit parameters out from the bitfield
1994 sub-expression. */
1995 bitpos = value_bitpos (v);
1996 bitsize = value_bitsize (v);
1997 }
1998 else if (v == result && b->val_bitsize != 0)
1999 {
2000 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2001 lvalue whose bit parameters are saved in the fields
2002 VAL_BITPOS and VAL_BITSIZE. */
2003 bitpos = b->val_bitpos;
2004 bitsize = b->val_bitsize;
2005 }
2006
2007 addr = value_address (v);
2008 if (bitsize != 0)
2009 {
2010 /* Skip the bytes that don't contain the bitfield. */
2011 addr += bitpos / 8;
2012 }
2013
2014 type = hw_write;
2015 if (b->base.type == bp_read_watchpoint)
2016 type = hw_read;
2017 else if (b->base.type == bp_access_watchpoint)
2018 type = hw_access;
2019
2020 loc = allocate_bp_location (&b->base);
2021 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2022 ;
2023 *tmp = loc;
2024 loc->gdbarch = get_type_arch (value_type (v));
2025
2026 loc->pspace = frame_pspace;
2027 loc->address = addr;
2028
2029 if (bitsize != 0)
2030 {
2031 /* Just cover the bytes that make up the bitfield. */
2032 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2033 }
2034 else
2035 loc->length = TYPE_LENGTH (value_type (v));
2036
2037 loc->watchpoint_type = type;
2038 }
2039 }
2040 }
2041
2042 /* Change the type of breakpoint between hardware assisted or
2043 an ordinary watchpoint depending on the hardware support
2044 and free hardware slots. REPARSE is set when the inferior
2045 is started. */
2046 if (reparse)
2047 {
2048 int reg_cnt;
2049 enum bp_loc_type loc_type;
2050 struct bp_location *bl;
2051
2052 reg_cnt = can_use_hardware_watchpoint (val_chain);
2053
2054 if (reg_cnt)
2055 {
2056 int i, target_resources_ok, other_type_used;
2057 enum bptype type;
2058
2059 /* Use an exact watchpoint when there's only one memory region to be
2060 watched, and only one debug register is needed to watch it. */
2061 b->exact = target_exact_watchpoints && reg_cnt == 1;
2062
2063 /* We need to determine how many resources are already
2064 used for all other hardware watchpoints plus this one
2065 to see if we still have enough resources to also fit
2066 this watchpoint in as well. */
2067
2068 /* If this is a software watchpoint, we try to turn it
2069 to a hardware one -- count resources as if B was of
2070 hardware watchpoint type. */
2071 type = b->base.type;
2072 if (type == bp_watchpoint)
2073 type = bp_hardware_watchpoint;
2074
2075 /* This watchpoint may or may not have been placed on
2076 the list yet at this point (it won't be in the list
2077 if we're trying to create it for the first time,
2078 through watch_command), so always account for it
2079 manually. */
2080
2081 /* Count resources used by all watchpoints except B. */
2082 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2083
2084 /* Add in the resources needed for B. */
2085 i += hw_watchpoint_use_count (&b->base);
2086
2087 target_resources_ok
2088 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2089 if (target_resources_ok <= 0)
2090 {
2091 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2092
2093 if (target_resources_ok == 0 && !sw_mode)
2094 error (_("Target does not support this type of "
2095 "hardware watchpoint."));
2096 else if (target_resources_ok < 0 && !sw_mode)
2097 error (_("There are not enough available hardware "
2098 "resources for this watchpoint."));
2099
2100 /* Downgrade to software watchpoint. */
2101 b->base.type = bp_watchpoint;
2102 }
2103 else
2104 {
2105 /* If this was a software watchpoint, we've just
2106 found we have enough resources to turn it to a
2107 hardware watchpoint. Otherwise, this is a
2108 nop. */
2109 b->base.type = type;
2110 }
2111 }
2112 else if (!b->base.ops->works_in_software_mode (&b->base))
2113 {
2114 if (!can_use_hw_watchpoints)
2115 error (_("Can't set read/access watchpoint when "
2116 "hardware watchpoints are disabled."));
2117 else
2118 error (_("Expression cannot be implemented with "
2119 "read/access watchpoint."));
2120 }
2121 else
2122 b->base.type = bp_watchpoint;
2123
2124 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2125 : bp_loc_hardware_watchpoint);
2126 for (bl = b->base.loc; bl; bl = bl->next)
2127 bl->loc_type = loc_type;
2128 }
2129
2130 for (v = val_chain; v; v = next)
2131 {
2132 next = value_next (v);
2133 if (v != b->val)
2134 value_free (v);
2135 }
2136
2137 /* If a software watchpoint is not watching any memory, then the
2138 above left it without any location set up. But,
2139 bpstat_stop_status requires a location to be able to report
2140 stops, so make sure there's at least a dummy one. */
2141 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2142 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2143 }
2144 else if (!within_current_scope)
2145 {
2146 printf_filtered (_("\
2147 Watchpoint %d deleted because the program has left the block\n\
2148 in which its expression is valid.\n"),
2149 b->base.number);
2150 watchpoint_del_at_next_stop (b);
2151 }
2152
2153 /* Restore the selected frame. */
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
2156 }
2157
2158
2159 /* Returns 1 iff breakpoint location should be
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
2164 static int
2165 should_be_inserted (struct bp_location *bl)
2166 {
2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2168 return 0;
2169
2170 if (bl->owner->disposition == disp_del_at_next_stop)
2171 return 0;
2172
2173 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2174 return 0;
2175
2176 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2177 return 0;
2178
2179 /* This is set for example, when we're attached to the parent of a
2180 vfork, and have detached from the child. The child is running
2181 free, and we expect it to do an exec or exit, at which point the
2182 OS makes the parent schedulable again (and the target reports
2183 that the vfork is done). Until the child is done with the shared
2184 memory region, do not insert breakpoints in the parent, otherwise
2185 the child could still trip on the parent's breakpoints. Since
2186 the parent is blocked anyway, it won't miss any breakpoint. */
2187 if (bl->pspace->breakpoints_not_allowed)
2188 return 0;
2189
2190 /* Don't insert a breakpoint if we're trying to step past its
2191 location, except if the breakpoint is a single-step breakpoint,
2192 and the breakpoint's thread is the thread which is stepping past
2193 a breakpoint. */
2194 if ((bl->loc_type == bp_loc_software_breakpoint
2195 || bl->loc_type == bp_loc_hardware_breakpoint)
2196 && stepping_past_instruction_at (bl->pspace->aspace,
2197 bl->address)
2198 /* The single-step breakpoint may be inserted at the location
2199 we're trying to step if the instruction branches to itself.
2200 However, the instruction won't be executed at all and it may
2201 break the semantics of the instruction, for example, the
2202 instruction is a conditional branch or updates some flags.
2203 We can't fix it unless GDB is able to emulate the instruction
2204 or switch to displaced stepping. */
2205 && !(bl->owner->type == bp_single_step
2206 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2207 {
2208 if (debug_infrun)
2209 {
2210 fprintf_unfiltered (gdb_stdlog,
2211 "infrun: skipping breakpoint: "
2212 "stepping past insn at: %s\n",
2213 paddress (bl->gdbarch, bl->address));
2214 }
2215 return 0;
2216 }
2217
2218 /* Don't insert watchpoints if we're trying to step past the
2219 instruction that triggered one. */
2220 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2221 && stepping_past_nonsteppable_watchpoint ())
2222 {
2223 if (debug_infrun)
2224 {
2225 fprintf_unfiltered (gdb_stdlog,
2226 "infrun: stepping past non-steppable watchpoint. "
2227 "skipping watchpoint at %s:%d\n",
2228 paddress (bl->gdbarch, bl->address),
2229 bl->length);
2230 }
2231 return 0;
2232 }
2233
2234 return 1;
2235 }
2236
2237 /* Same as should_be_inserted but does the check assuming
2238 that the location is not duplicated. */
2239
2240 static int
2241 unduplicated_should_be_inserted (struct bp_location *bl)
2242 {
2243 int result;
2244 const int save_duplicate = bl->duplicate;
2245
2246 bl->duplicate = 0;
2247 result = should_be_inserted (bl);
2248 bl->duplicate = save_duplicate;
2249 return result;
2250 }
2251
2252 /* Parses a conditional described by an expression COND into an
2253 agent expression bytecode suitable for evaluation
2254 by the bytecode interpreter. Return NULL if there was
2255 any error during parsing. */
2256
2257 static agent_expr_up
2258 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2259 {
2260 if (cond == NULL)
2261 return NULL;
2262
2263 agent_expr_up aexpr;
2264
2265 /* We don't want to stop processing, so catch any errors
2266 that may show up. */
2267 TRY
2268 {
2269 aexpr = gen_eval_for_expr (scope, cond);
2270 }
2271
2272 CATCH (ex, RETURN_MASK_ERROR)
2273 {
2274 /* If we got here, it means the condition could not be parsed to a valid
2275 bytecode expression and thus can't be evaluated on the target's side.
2276 It's no use iterating through the conditions. */
2277 }
2278 END_CATCH
2279
2280 /* We have a valid agent expression. */
2281 return aexpr;
2282 }
2283
2284 /* Based on location BL, create a list of breakpoint conditions to be
2285 passed on to the target. If we have duplicated locations with different
2286 conditions, we will add such conditions to the list. The idea is that the
2287 target will evaluate the list of conditions and will only notify GDB when
2288 one of them is true. */
2289
2290 static void
2291 build_target_condition_list (struct bp_location *bl)
2292 {
2293 struct bp_location **locp = NULL, **loc2p;
2294 int null_condition_or_parse_error = 0;
2295 int modified = bl->needs_update;
2296 struct bp_location *loc;
2297
2298 /* Release conditions left over from a previous insert. */
2299 bl->target_info.conditions.clear ();
2300
2301 /* This is only meaningful if the target is
2302 evaluating conditions and if the user has
2303 opted for condition evaluation on the target's
2304 side. */
2305 if (gdb_evaluates_breakpoint_condition_p ()
2306 || !target_supports_evaluation_of_breakpoint_conditions ())
2307 return;
2308
2309 /* Do a first pass to check for locations with no assigned
2310 conditions or conditions that fail to parse to a valid agent expression
2311 bytecode. If any of these happen, then it's no use to send conditions
2312 to the target since this location will always trigger and generate a
2313 response back to GDB. */
2314 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2315 {
2316 loc = (*loc2p);
2317 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2318 {
2319 if (modified)
2320 {
2321 /* Re-parse the conditions since something changed. In that
2322 case we already freed the condition bytecodes (see
2323 force_breakpoint_reinsertion). We just
2324 need to parse the condition to bytecodes again. */
2325 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2326 loc->cond.get ());
2327 }
2328
2329 /* If we have a NULL bytecode expression, it means something
2330 went wrong or we have a null condition expression. */
2331 if (!loc->cond_bytecode)
2332 {
2333 null_condition_or_parse_error = 1;
2334 break;
2335 }
2336 }
2337 }
2338
2339 /* If any of these happened, it means we will have to evaluate the conditions
2340 for the location's address on gdb's side. It is no use keeping bytecodes
2341 for all the other duplicate locations, thus we free all of them here.
2342
2343 This is so we have a finer control over which locations' conditions are
2344 being evaluated by GDB or the remote stub. */
2345 if (null_condition_or_parse_error)
2346 {
2347 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348 {
2349 loc = (*loc2p);
2350 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2351 {
2352 /* Only go as far as the first NULL bytecode is
2353 located. */
2354 if (!loc->cond_bytecode)
2355 return;
2356
2357 loc->cond_bytecode.reset ();
2358 }
2359 }
2360 }
2361
2362 /* No NULL conditions or failed bytecode generation. Build a condition list
2363 for this location's address. */
2364 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2365 {
2366 loc = (*loc2p);
2367 if (loc->cond
2368 && is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->enable_state == bp_enabled
2371 && loc->enabled)
2372 {
2373 /* Add the condition to the vector. This will be used later
2374 to send the conditions to the target. */
2375 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2376 }
2377 }
2378
2379 return;
2380 }
2381
2382 /* Parses a command described by string CMD into an agent expression
2383 bytecode suitable for evaluation by the bytecode interpreter.
2384 Return NULL if there was any error during parsing. */
2385
2386 static agent_expr_up
2387 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2388 {
2389 struct cleanup *old_cleanups = 0;
2390 struct expression **argvec;
2391 const char *cmdrest;
2392 const char *format_start, *format_end;
2393 struct format_piece *fpieces;
2394 int nargs;
2395 struct gdbarch *gdbarch = get_current_arch ();
2396
2397 if (cmd == NULL)
2398 return NULL;
2399
2400 cmdrest = cmd;
2401
2402 if (*cmdrest == ',')
2403 ++cmdrest;
2404 cmdrest = skip_spaces_const (cmdrest);
2405
2406 if (*cmdrest++ != '"')
2407 error (_("No format string following the location"));
2408
2409 format_start = cmdrest;
2410
2411 fpieces = parse_format_string (&cmdrest);
2412
2413 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2414
2415 format_end = cmdrest;
2416
2417 if (*cmdrest++ != '"')
2418 error (_("Bad format string, non-terminated '\"'."));
2419
2420 cmdrest = skip_spaces_const (cmdrest);
2421
2422 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2423 error (_("Invalid argument syntax"));
2424
2425 if (*cmdrest == ',')
2426 cmdrest++;
2427 cmdrest = skip_spaces_const (cmdrest);
2428
2429 /* For each argument, make an expression. */
2430
2431 argvec = (struct expression **) alloca (strlen (cmd)
2432 * sizeof (struct expression *));
2433
2434 nargs = 0;
2435 while (*cmdrest != '\0')
2436 {
2437 const char *cmd1;
2438
2439 cmd1 = cmdrest;
2440 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2441 argvec[nargs++] = expr.release ();
2442 cmdrest = cmd1;
2443 if (*cmdrest == ',')
2444 ++cmdrest;
2445 }
2446
2447 agent_expr_up aexpr;
2448
2449 /* We don't want to stop processing, so catch any errors
2450 that may show up. */
2451 TRY
2452 {
2453 aexpr = gen_printf (scope, gdbarch, 0, 0,
2454 format_start, format_end - format_start,
2455 fpieces, nargs, argvec);
2456 }
2457 CATCH (ex, RETURN_MASK_ERROR)
2458 {
2459 /* If we got here, it means the command could not be parsed to a valid
2460 bytecode expression and thus can't be evaluated on the target's side.
2461 It's no use iterating through the other commands. */
2462 }
2463 END_CATCH
2464
2465 do_cleanups (old_cleanups);
2466
2467 /* We have a valid agent expression, return it. */
2468 return aexpr;
2469 }
2470
2471 /* Based on location BL, create a list of breakpoint commands to be
2472 passed on to the target. If we have duplicated locations with
2473 different commands, we will add any such to the list. */
2474
2475 static void
2476 build_target_command_list (struct bp_location *bl)
2477 {
2478 struct bp_location **locp = NULL, **loc2p;
2479 int null_command_or_parse_error = 0;
2480 int modified = bl->needs_update;
2481 struct bp_location *loc;
2482
2483 /* Clear commands left over from a previous insert. */
2484 bl->target_info.tcommands.clear ();
2485
2486 if (!target_can_run_breakpoint_commands ())
2487 return;
2488
2489 /* For now, limit to agent-style dprintf breakpoints. */
2490 if (dprintf_style != dprintf_style_agent)
2491 return;
2492
2493 /* For now, if we have any duplicate location that isn't a dprintf,
2494 don't install the target-side commands, as that would make the
2495 breakpoint not be reported to the core, and we'd lose
2496 control. */
2497 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2498 {
2499 loc = (*loc2p);
2500 if (is_breakpoint (loc->owner)
2501 && loc->pspace->num == bl->pspace->num
2502 && loc->owner->type != bp_dprintf)
2503 return;
2504 }
2505
2506 /* Do a first pass to check for locations with no assigned
2507 conditions or conditions that fail to parse to a valid agent expression
2508 bytecode. If any of these happen, then it's no use to send conditions
2509 to the target since this location will always trigger and generate a
2510 response back to GDB. */
2511 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2512 {
2513 loc = (*loc2p);
2514 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2515 {
2516 if (modified)
2517 {
2518 /* Re-parse the commands since something changed. In that
2519 case we already freed the command bytecodes (see
2520 force_breakpoint_reinsertion). We just
2521 need to parse the command to bytecodes again. */
2522 loc->cmd_bytecode
2523 = parse_cmd_to_aexpr (bl->address,
2524 loc->owner->extra_string);
2525 }
2526
2527 /* If we have a NULL bytecode expression, it means something
2528 went wrong or we have a null command expression. */
2529 if (!loc->cmd_bytecode)
2530 {
2531 null_command_or_parse_error = 1;
2532 break;
2533 }
2534 }
2535 }
2536
2537 /* If anything failed, then we're not doing target-side commands,
2538 and so clean up. */
2539 if (null_command_or_parse_error)
2540 {
2541 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2542 {
2543 loc = (*loc2p);
2544 if (is_breakpoint (loc->owner)
2545 && loc->pspace->num == bl->pspace->num)
2546 {
2547 /* Only go as far as the first NULL bytecode is
2548 located. */
2549 if (loc->cmd_bytecode == NULL)
2550 return;
2551
2552 loc->cmd_bytecode.reset ();
2553 }
2554 }
2555 }
2556
2557 /* No NULL commands or failed bytecode generation. Build a command list
2558 for this location's address. */
2559 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2560 {
2561 loc = (*loc2p);
2562 if (loc->owner->extra_string
2563 && is_breakpoint (loc->owner)
2564 && loc->pspace->num == bl->pspace->num
2565 && loc->owner->enable_state == bp_enabled
2566 && loc->enabled)
2567 {
2568 /* Add the command to the vector. This will be used later
2569 to send the commands to the target. */
2570 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2571 }
2572 }
2573
2574 bl->target_info.persist = 0;
2575 /* Maybe flag this location as persistent. */
2576 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2577 bl->target_info.persist = 1;
2578 }
2579
2580 /* Return the kind of breakpoint on address *ADDR. Get the kind
2581 of breakpoint according to ADDR except single-step breakpoint.
2582 Get the kind of single-step breakpoint according to the current
2583 registers state. */
2584
2585 static int
2586 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2587 {
2588 if (bl->owner->type == bp_single_step)
2589 {
2590 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2591 struct regcache *regcache;
2592
2593 regcache = get_thread_regcache (thr->ptid);
2594
2595 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2596 regcache, addr);
2597 }
2598 else
2599 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2600 }
2601
2602 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2603 location. Any error messages are printed to TMP_ERROR_STREAM; and
2604 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2605 Returns 0 for success, 1 if the bp_location type is not supported or
2606 -1 for failure.
2607
2608 NOTE drow/2003-09-09: This routine could be broken down to an
2609 object-style method for each breakpoint or catchpoint type. */
2610 static int
2611 insert_bp_location (struct bp_location *bl,
2612 struct ui_file *tmp_error_stream,
2613 int *disabled_breaks,
2614 int *hw_breakpoint_error,
2615 int *hw_bp_error_explained_already)
2616 {
2617 enum errors bp_err = GDB_NO_ERROR;
2618 const char *bp_err_message = NULL;
2619
2620 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2621 return 0;
2622
2623 /* Note we don't initialize bl->target_info, as that wipes out
2624 the breakpoint location's shadow_contents if the breakpoint
2625 is still inserted at that location. This in turn breaks
2626 target_read_memory which depends on these buffers when
2627 a memory read is requested at the breakpoint location:
2628 Once the target_info has been wiped, we fail to see that
2629 we have a breakpoint inserted at that address and thus
2630 read the breakpoint instead of returning the data saved in
2631 the breakpoint location's shadow contents. */
2632 bl->target_info.reqstd_address = bl->address;
2633 bl->target_info.placed_address_space = bl->pspace->aspace;
2634 bl->target_info.length = bl->length;
2635
2636 /* When working with target-side conditions, we must pass all the conditions
2637 for the same breakpoint address down to the target since GDB will not
2638 insert those locations. With a list of breakpoint conditions, the target
2639 can decide when to stop and notify GDB. */
2640
2641 if (is_breakpoint (bl->owner))
2642 {
2643 build_target_condition_list (bl);
2644 build_target_command_list (bl);
2645 /* Reset the modification marker. */
2646 bl->needs_update = 0;
2647 }
2648
2649 if (bl->loc_type == bp_loc_software_breakpoint
2650 || bl->loc_type == bp_loc_hardware_breakpoint)
2651 {
2652 if (bl->owner->type != bp_hardware_breakpoint)
2653 {
2654 /* If the explicitly specified breakpoint type
2655 is not hardware breakpoint, check the memory map to see
2656 if the breakpoint address is in read only memory or not.
2657
2658 Two important cases are:
2659 - location type is not hardware breakpoint, memory
2660 is readonly. We change the type of the location to
2661 hardware breakpoint.
2662 - location type is hardware breakpoint, memory is
2663 read-write. This means we've previously made the
2664 location hardware one, but then the memory map changed,
2665 so we undo.
2666
2667 When breakpoints are removed, remove_breakpoints will use
2668 location types we've just set here, the only possible
2669 problem is that memory map has changed during running
2670 program, but it's not going to work anyway with current
2671 gdb. */
2672 struct mem_region *mr
2673 = lookup_mem_region (bl->target_info.reqstd_address);
2674
2675 if (mr)
2676 {
2677 if (automatic_hardware_breakpoints)
2678 {
2679 enum bp_loc_type new_type;
2680
2681 if (mr->attrib.mode != MEM_RW)
2682 new_type = bp_loc_hardware_breakpoint;
2683 else
2684 new_type = bp_loc_software_breakpoint;
2685
2686 if (new_type != bl->loc_type)
2687 {
2688 static int said = 0;
2689
2690 bl->loc_type = new_type;
2691 if (!said)
2692 {
2693 fprintf_filtered (gdb_stdout,
2694 _("Note: automatically using "
2695 "hardware breakpoints for "
2696 "read-only addresses.\n"));
2697 said = 1;
2698 }
2699 }
2700 }
2701 else if (bl->loc_type == bp_loc_software_breakpoint
2702 && mr->attrib.mode != MEM_RW)
2703 {
2704 fprintf_unfiltered (tmp_error_stream,
2705 _("Cannot insert breakpoint %d.\n"
2706 "Cannot set software breakpoint "
2707 "at read-only address %s\n"),
2708 bl->owner->number,
2709 paddress (bl->gdbarch, bl->address));
2710 return 1;
2711 }
2712 }
2713 }
2714
2715 /* First check to see if we have to handle an overlay. */
2716 if (overlay_debugging == ovly_off
2717 || bl->section == NULL
2718 || !(section_is_overlay (bl->section)))
2719 {
2720 /* No overlay handling: just set the breakpoint. */
2721 TRY
2722 {
2723 int val;
2724
2725 val = bl->owner->ops->insert_location (bl);
2726 if (val)
2727 bp_err = GENERIC_ERROR;
2728 }
2729 CATCH (e, RETURN_MASK_ALL)
2730 {
2731 bp_err = e.error;
2732 bp_err_message = e.message;
2733 }
2734 END_CATCH
2735 }
2736 else
2737 {
2738 /* This breakpoint is in an overlay section.
2739 Shall we set a breakpoint at the LMA? */
2740 if (!overlay_events_enabled)
2741 {
2742 /* Yes -- overlay event support is not active,
2743 so we must try to set a breakpoint at the LMA.
2744 This will not work for a hardware breakpoint. */
2745 if (bl->loc_type == bp_loc_hardware_breakpoint)
2746 warning (_("hardware breakpoint %d not supported in overlay!"),
2747 bl->owner->number);
2748 else
2749 {
2750 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2751 bl->section);
2752 /* Set a software (trap) breakpoint at the LMA. */
2753 bl->overlay_target_info = bl->target_info;
2754 bl->overlay_target_info.reqstd_address = addr;
2755
2756 /* No overlay handling: just set the breakpoint. */
2757 TRY
2758 {
2759 int val;
2760
2761 bl->overlay_target_info.kind
2762 = breakpoint_kind (bl, &addr);
2763 bl->overlay_target_info.placed_address = addr;
2764 val = target_insert_breakpoint (bl->gdbarch,
2765 &bl->overlay_target_info);
2766 if (val)
2767 bp_err = GENERIC_ERROR;
2768 }
2769 CATCH (e, RETURN_MASK_ALL)
2770 {
2771 bp_err = e.error;
2772 bp_err_message = e.message;
2773 }
2774 END_CATCH
2775
2776 if (bp_err != GDB_NO_ERROR)
2777 fprintf_unfiltered (tmp_error_stream,
2778 "Overlay breakpoint %d "
2779 "failed: in ROM?\n",
2780 bl->owner->number);
2781 }
2782 }
2783 /* Shall we set a breakpoint at the VMA? */
2784 if (section_is_mapped (bl->section))
2785 {
2786 /* Yes. This overlay section is mapped into memory. */
2787 TRY
2788 {
2789 int val;
2790
2791 val = bl->owner->ops->insert_location (bl);
2792 if (val)
2793 bp_err = GENERIC_ERROR;
2794 }
2795 CATCH (e, RETURN_MASK_ALL)
2796 {
2797 bp_err = e.error;
2798 bp_err_message = e.message;
2799 }
2800 END_CATCH
2801 }
2802 else
2803 {
2804 /* No. This breakpoint will not be inserted.
2805 No error, but do not mark the bp as 'inserted'. */
2806 return 0;
2807 }
2808 }
2809
2810 if (bp_err != GDB_NO_ERROR)
2811 {
2812 /* Can't set the breakpoint. */
2813
2814 /* In some cases, we might not be able to insert a
2815 breakpoint in a shared library that has already been
2816 removed, but we have not yet processed the shlib unload
2817 event. Unfortunately, some targets that implement
2818 breakpoint insertion themselves can't tell why the
2819 breakpoint insertion failed (e.g., the remote target
2820 doesn't define error codes), so we must treat generic
2821 errors as memory errors. */
2822 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2823 && bl->loc_type == bp_loc_software_breakpoint
2824 && (solib_name_from_address (bl->pspace, bl->address)
2825 || shared_objfile_contains_address_p (bl->pspace,
2826 bl->address)))
2827 {
2828 /* See also: disable_breakpoints_in_shlibs. */
2829 bl->shlib_disabled = 1;
2830 observer_notify_breakpoint_modified (bl->owner);
2831 if (!*disabled_breaks)
2832 {
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n",
2835 bl->owner->number);
2836 fprintf_unfiltered (tmp_error_stream,
2837 "Temporarily disabling shared "
2838 "library breakpoints:\n");
2839 }
2840 *disabled_breaks = 1;
2841 fprintf_unfiltered (tmp_error_stream,
2842 "breakpoint #%d\n", bl->owner->number);
2843 return 0;
2844 }
2845 else
2846 {
2847 if (bl->loc_type == bp_loc_hardware_breakpoint)
2848 {
2849 *hw_breakpoint_error = 1;
2850 *hw_bp_error_explained_already = bp_err_message != NULL;
2851 fprintf_unfiltered (tmp_error_stream,
2852 "Cannot insert hardware breakpoint %d%s",
2853 bl->owner->number, bp_err_message ? ":" : ".\n");
2854 if (bp_err_message != NULL)
2855 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2856 }
2857 else
2858 {
2859 if (bp_err_message == NULL)
2860 {
2861 char *message
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
2864 struct cleanup *old_chain = make_cleanup (xfree, message);
2865
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Cannot insert breakpoint %d.\n"
2868 "%s\n",
2869 bl->owner->number, message);
2870 do_cleanups (old_chain);
2871 }
2872 else
2873 {
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d: %s\n",
2876 bl->owner->number,
2877 bp_err_message);
2878 }
2879 }
2880 return 1;
2881
2882 }
2883 }
2884 else
2885 bl->inserted = 1;
2886
2887 return 0;
2888 }
2889
2890 else if (bl->loc_type == bp_loc_hardware_watchpoint
2891 /* NOTE drow/2003-09-08: This state only exists for removing
2892 watchpoints. It's not clear that it's necessary... */
2893 && bl->owner->disposition != disp_del_at_next_stop)
2894 {
2895 int val;
2896
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
2901
2902 /* If trying to set a read-watchpoint, and it turns out it's not
2903 supported, try emulating one with an access watchpoint. */
2904 if (val == 1 && bl->watchpoint_type == hw_read)
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* But don't try to insert it, if there's already another
2909 hw_access location that would be considered a duplicate
2910 of this one. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
2912 if (loc != bl
2913 && loc->watchpoint_type == hw_access
2914 && watchpoint_locations_match (bl, loc))
2915 {
2916 bl->duplicate = 1;
2917 bl->inserted = 1;
2918 bl->target_info = loc->target_info;
2919 bl->watchpoint_type = hw_access;
2920 val = 0;
2921 break;
2922 }
2923
2924 if (val == 1)
2925 {
2926 bl->watchpoint_type = hw_access;
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 if (val)
2930 /* Back to the original value. */
2931 bl->watchpoint_type = hw_read;
2932 }
2933 }
2934
2935 bl->inserted = (val == 0);
2936 }
2937
2938 else if (bl->owner->type == bp_catchpoint)
2939 {
2940 int val;
2941
2942 gdb_assert (bl->owner->ops != NULL
2943 && bl->owner->ops->insert_location != NULL);
2944
2945 val = bl->owner->ops->insert_location (bl);
2946 if (val)
2947 {
2948 bl->owner->enable_state = bp_disabled;
2949
2950 if (val == 1)
2951 warning (_("\
2952 Error inserting catchpoint %d: Your system does not support this type\n\
2953 of catchpoint."), bl->owner->number);
2954 else
2955 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2956 }
2957
2958 bl->inserted = (val == 0);
2959
2960 /* We've already printed an error message if there was a problem
2961 inserting this catchpoint, and we've disabled the catchpoint,
2962 so just return success. */
2963 return 0;
2964 }
2965
2966 return 0;
2967 }
2968
2969 /* This function is called when program space PSPACE is about to be
2970 deleted. It takes care of updating breakpoints to not reference
2971 PSPACE anymore. */
2972
2973 void
2974 breakpoint_program_space_exit (struct program_space *pspace)
2975 {
2976 struct breakpoint *b, *b_temp;
2977 struct bp_location *loc, **loc_temp;
2978
2979 /* Remove any breakpoint that was set through this program space. */
2980 ALL_BREAKPOINTS_SAFE (b, b_temp)
2981 {
2982 if (b->pspace == pspace)
2983 delete_breakpoint (b);
2984 }
2985
2986 /* Breakpoints set through other program spaces could have locations
2987 bound to PSPACE as well. Remove those. */
2988 ALL_BP_LOCATIONS (loc, loc_temp)
2989 {
2990 struct bp_location *tmp;
2991
2992 if (loc->pspace == pspace)
2993 {
2994 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2995 if (loc->owner->loc == loc)
2996 loc->owner->loc = loc->next;
2997 else
2998 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2999 if (tmp->next == loc)
3000 {
3001 tmp->next = loc->next;
3002 break;
3003 }
3004 }
3005 }
3006
3007 /* Now update the global location list to permanently delete the
3008 removed locations above. */
3009 update_global_location_list (UGLL_DONT_INSERT);
3010 }
3011
3012 /* Make sure all breakpoints are inserted in inferior.
3013 Throws exception on any error.
3014 A breakpoint that is already inserted won't be inserted
3015 again, so calling this function twice is safe. */
3016 void
3017 insert_breakpoints (void)
3018 {
3019 struct breakpoint *bpt;
3020
3021 ALL_BREAKPOINTS (bpt)
3022 if (is_hardware_watchpoint (bpt))
3023 {
3024 struct watchpoint *w = (struct watchpoint *) bpt;
3025
3026 update_watchpoint (w, 0 /* don't reparse. */);
3027 }
3028
3029 /* Updating watchpoints creates new locations, so update the global
3030 location list. Explicitly tell ugll to insert locations and
3031 ignore breakpoints_always_inserted_mode. */
3032 update_global_location_list (UGLL_INSERT);
3033 }
3034
3035 /* Invoke CALLBACK for each of bp_location. */
3036
3037 void
3038 iterate_over_bp_locations (walk_bp_location_callback callback)
3039 {
3040 struct bp_location *loc, **loc_tmp;
3041
3042 ALL_BP_LOCATIONS (loc, loc_tmp)
3043 {
3044 callback (loc, NULL);
3045 }
3046 }
3047
3048 /* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3051
3052 static void
3053 update_inserted_breakpoint_locations (void)
3054 {
3055 struct bp_location *bl, **blp_tmp;
3056 int error_flag = 0;
3057 int val = 0;
3058 int disabled_breaks = 0;
3059 int hw_breakpoint_error = 0;
3060 int hw_bp_details_reported = 0;
3061
3062 string_file tmp_error_stream;
3063
3064 /* Explicitly mark the warning -- this will only be printed if
3065 there was an error. */
3066 tmp_error_stream.puts ("Warning:\n");
3067
3068 struct cleanup *cleanups = save_current_space_and_thread ();
3069
3070 ALL_BP_LOCATIONS (bl, blp_tmp)
3071 {
3072 /* We only want to update software breakpoints and hardware
3073 breakpoints. */
3074 if (!is_breakpoint (bl->owner))
3075 continue;
3076
3077 /* We only want to update locations that are already inserted
3078 and need updating. This is to avoid unwanted insertion during
3079 deletion of breakpoints. */
3080 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3081 continue;
3082
3083 switch_to_program_space_and_thread (bl->pspace);
3084
3085 /* For targets that support global breakpoints, there's no need
3086 to select an inferior to insert breakpoint to. In fact, even
3087 if we aren't attached to any process yet, we should still
3088 insert breakpoints. */
3089 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3090 && ptid_equal (inferior_ptid, null_ptid))
3091 continue;
3092
3093 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3094 &hw_breakpoint_error, &hw_bp_details_reported);
3095 if (val)
3096 error_flag = val;
3097 }
3098
3099 if (error_flag)
3100 {
3101 target_terminal_ours_for_output ();
3102 error_stream (tmp_error_stream);
3103 }
3104
3105 do_cleanups (cleanups);
3106 }
3107
3108 /* Used when starting or continuing the program. */
3109
3110 static void
3111 insert_breakpoint_locations (void)
3112 {
3113 struct breakpoint *bpt;
3114 struct bp_location *bl, **blp_tmp;
3115 int error_flag = 0;
3116 int val = 0;
3117 int disabled_breaks = 0;
3118 int hw_breakpoint_error = 0;
3119 int hw_bp_error_explained_already = 0;
3120
3121 string_file tmp_error_stream;
3122
3123 /* Explicitly mark the warning -- this will only be printed if
3124 there was an error. */
3125 tmp_error_stream.puts ("Warning:\n");
3126
3127 struct cleanup *cleanups = save_current_space_and_thread ();
3128
3129 ALL_BP_LOCATIONS (bl, blp_tmp)
3130 {
3131 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3132 continue;
3133
3134 /* There is no point inserting thread-specific breakpoints if
3135 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3136 has BL->OWNER always non-NULL. */
3137 if (bl->owner->thread != -1
3138 && !valid_global_thread_id (bl->owner->thread))
3139 continue;
3140
3141 switch_to_program_space_and_thread (bl->pspace);
3142
3143 /* For targets that support global breakpoints, there's no need
3144 to select an inferior to insert breakpoint to. In fact, even
3145 if we aren't attached to any process yet, we should still
3146 insert breakpoints. */
3147 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3148 && ptid_equal (inferior_ptid, null_ptid))
3149 continue;
3150
3151 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3152 &hw_breakpoint_error, &hw_bp_error_explained_already);
3153 if (val)
3154 error_flag = val;
3155 }
3156
3157 /* If we failed to insert all locations of a watchpoint, remove
3158 them, as half-inserted watchpoint is of limited use. */
3159 ALL_BREAKPOINTS (bpt)
3160 {
3161 int some_failed = 0;
3162 struct bp_location *loc;
3163
3164 if (!is_hardware_watchpoint (bpt))
3165 continue;
3166
3167 if (!breakpoint_enabled (bpt))
3168 continue;
3169
3170 if (bpt->disposition == disp_del_at_next_stop)
3171 continue;
3172
3173 for (loc = bpt->loc; loc; loc = loc->next)
3174 if (!loc->inserted && should_be_inserted (loc))
3175 {
3176 some_failed = 1;
3177 break;
3178 }
3179 if (some_failed)
3180 {
3181 for (loc = bpt->loc; loc; loc = loc->next)
3182 if (loc->inserted)
3183 remove_breakpoint (loc);
3184
3185 hw_breakpoint_error = 1;
3186 tmp_error_stream.printf ("Could not insert "
3187 "hardware watchpoint %d.\n",
3188 bpt->number);
3189 error_flag = -1;
3190 }
3191 }
3192
3193 if (error_flag)
3194 {
3195 /* If a hardware breakpoint or watchpoint was inserted, add a
3196 message about possibly exhausted resources. */
3197 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3198 {
3199 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3200 You may have requested too many hardware breakpoints/watchpoints.\n");
3201 }
3202 target_terminal_ours_for_output ();
3203 error_stream (tmp_error_stream);
3204 }
3205
3206 do_cleanups (cleanups);
3207 }
3208
3209 /* Used when the program stops.
3210 Returns zero if successful, or non-zero if there was a problem
3211 removing a breakpoint location. */
3212
3213 int
3214 remove_breakpoints (void)
3215 {
3216 struct bp_location *bl, **blp_tmp;
3217 int val = 0;
3218
3219 ALL_BP_LOCATIONS (bl, blp_tmp)
3220 {
3221 if (bl->inserted && !is_tracepoint (bl->owner))
3222 val |= remove_breakpoint (bl);
3223 }
3224 return val;
3225 }
3226
3227 /* When a thread exits, remove breakpoints that are related to
3228 that thread. */
3229
3230 static void
3231 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3232 {
3233 struct breakpoint *b, *b_tmp;
3234
3235 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3236 {
3237 if (b->thread == tp->global_num && user_breakpoint_p (b))
3238 {
3239 b->disposition = disp_del_at_next_stop;
3240
3241 printf_filtered (_("\
3242 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3243 b->number, print_thread_id (tp));
3244
3245 /* Hide it from the user. */
3246 b->number = 0;
3247 }
3248 }
3249 }
3250
3251 /* Remove breakpoints of process PID. */
3252
3253 int
3254 remove_breakpoints_pid (int pid)
3255 {
3256 struct bp_location *bl, **blp_tmp;
3257 int val;
3258 struct inferior *inf = find_inferior_pid (pid);
3259
3260 ALL_BP_LOCATIONS (bl, blp_tmp)
3261 {
3262 if (bl->pspace != inf->pspace)
3263 continue;
3264
3265 if (bl->inserted && !bl->target_info.persist)
3266 {
3267 val = remove_breakpoint (bl);
3268 if (val != 0)
3269 return val;
3270 }
3271 }
3272 return 0;
3273 }
3274
3275 int
3276 reattach_breakpoints (int pid)
3277 {
3278 struct cleanup *old_chain;
3279 struct bp_location *bl, **blp_tmp;
3280 int val;
3281 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3282 struct inferior *inf;
3283 struct thread_info *tp;
3284
3285 tp = any_live_thread_of_process (pid);
3286 if (tp == NULL)
3287 return 1;
3288
3289 inf = find_inferior_pid (pid);
3290 old_chain = save_inferior_ptid ();
3291
3292 inferior_ptid = tp->ptid;
3293
3294 string_file tmp_error_stream;
3295
3296 ALL_BP_LOCATIONS (bl, blp_tmp)
3297 {
3298 if (bl->pspace != inf->pspace)
3299 continue;
3300
3301 if (bl->inserted)
3302 {
3303 bl->inserted = 0;
3304 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3305 if (val != 0)
3306 {
3307 do_cleanups (old_chain);
3308 return val;
3309 }
3310 }
3311 }
3312 do_cleanups (old_chain);
3313 return 0;
3314 }
3315
3316 static int internal_breakpoint_number = -1;
3317
3318 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3319 If INTERNAL is non-zero, the breakpoint number will be populated
3320 from internal_breakpoint_number and that variable decremented.
3321 Otherwise the breakpoint number will be populated from
3322 breakpoint_count and that value incremented. Internal breakpoints
3323 do not set the internal var bpnum. */
3324 static void
3325 set_breakpoint_number (int internal, struct breakpoint *b)
3326 {
3327 if (internal)
3328 b->number = internal_breakpoint_number--;
3329 else
3330 {
3331 set_breakpoint_count (breakpoint_count + 1);
3332 b->number = breakpoint_count;
3333 }
3334 }
3335
3336 static struct breakpoint *
3337 create_internal_breakpoint (struct gdbarch *gdbarch,
3338 CORE_ADDR address, enum bptype type,
3339 const struct breakpoint_ops *ops)
3340 {
3341 struct symtab_and_line sal;
3342 struct breakpoint *b;
3343
3344 init_sal (&sal); /* Initialize to zeroes. */
3345
3346 sal.pc = address;
3347 sal.section = find_pc_overlay (sal.pc);
3348 sal.pspace = current_program_space;
3349
3350 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3351 b->number = internal_breakpoint_number--;
3352 b->disposition = disp_donttouch;
3353
3354 return b;
3355 }
3356
3357 static const char *const longjmp_names[] =
3358 {
3359 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3360 };
3361 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3362
3363 /* Per-objfile data private to breakpoint.c. */
3364 struct breakpoint_objfile_data
3365 {
3366 /* Minimal symbol for "_ovly_debug_event" (if any). */
3367 struct bound_minimal_symbol overlay_msym;
3368
3369 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3370 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3371
3372 /* True if we have looked for longjmp probes. */
3373 int longjmp_searched;
3374
3375 /* SystemTap probe points for longjmp (if any). */
3376 VEC (probe_p) *longjmp_probes;
3377
3378 /* Minimal symbol for "std::terminate()" (if any). */
3379 struct bound_minimal_symbol terminate_msym;
3380
3381 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3382 struct bound_minimal_symbol exception_msym;
3383
3384 /* True if we have looked for exception probes. */
3385 int exception_searched;
3386
3387 /* SystemTap probe points for unwinding (if any). */
3388 VEC (probe_p) *exception_probes;
3389 };
3390
3391 static const struct objfile_data *breakpoint_objfile_key;
3392
3393 /* Minimal symbol not found sentinel. */
3394 static struct minimal_symbol msym_not_found;
3395
3396 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3397
3398 static int
3399 msym_not_found_p (const struct minimal_symbol *msym)
3400 {
3401 return msym == &msym_not_found;
3402 }
3403
3404 /* Return per-objfile data needed by breakpoint.c.
3405 Allocate the data if necessary. */
3406
3407 static struct breakpoint_objfile_data *
3408 get_breakpoint_objfile_data (struct objfile *objfile)
3409 {
3410 struct breakpoint_objfile_data *bp_objfile_data;
3411
3412 bp_objfile_data = ((struct breakpoint_objfile_data *)
3413 objfile_data (objfile, breakpoint_objfile_key));
3414 if (bp_objfile_data == NULL)
3415 {
3416 bp_objfile_data =
3417 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3418
3419 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3420 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3421 }
3422 return bp_objfile_data;
3423 }
3424
3425 static void
3426 free_breakpoint_probes (struct objfile *obj, void *data)
3427 {
3428 struct breakpoint_objfile_data *bp_objfile_data
3429 = (struct breakpoint_objfile_data *) data;
3430
3431 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3432 VEC_free (probe_p, bp_objfile_data->exception_probes);
3433 }
3434
3435 static void
3436 create_overlay_event_breakpoint (void)
3437 {
3438 struct objfile *objfile;
3439 const char *const func_name = "_ovly_debug_event";
3440
3441 ALL_OBJFILES (objfile)
3442 {
3443 struct breakpoint *b;
3444 struct breakpoint_objfile_data *bp_objfile_data;
3445 CORE_ADDR addr;
3446 struct explicit_location explicit_loc;
3447
3448 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3449
3450 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3451 continue;
3452
3453 if (bp_objfile_data->overlay_msym.minsym == NULL)
3454 {
3455 struct bound_minimal_symbol m;
3456
3457 m = lookup_minimal_symbol_text (func_name, objfile);
3458 if (m.minsym == NULL)
3459 {
3460 /* Avoid future lookups in this objfile. */
3461 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3462 continue;
3463 }
3464 bp_objfile_data->overlay_msym = m;
3465 }
3466
3467 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3468 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3469 bp_overlay_event,
3470 &internal_breakpoint_ops);
3471 initialize_explicit_location (&explicit_loc);
3472 explicit_loc.function_name = ASTRDUP (func_name);
3473 b->location = new_explicit_location (&explicit_loc);
3474
3475 if (overlay_debugging == ovly_auto)
3476 {
3477 b->enable_state = bp_enabled;
3478 overlay_events_enabled = 1;
3479 }
3480 else
3481 {
3482 b->enable_state = bp_disabled;
3483 overlay_events_enabled = 0;
3484 }
3485 }
3486 }
3487
3488 static void
3489 create_longjmp_master_breakpoint (void)
3490 {
3491 struct program_space *pspace;
3492 struct cleanup *old_chain;
3493
3494 old_chain = save_current_program_space ();
3495
3496 ALL_PSPACES (pspace)
3497 {
3498 struct objfile *objfile;
3499
3500 set_current_program_space (pspace);
3501
3502 ALL_OBJFILES (objfile)
3503 {
3504 int i;
3505 struct gdbarch *gdbarch;
3506 struct breakpoint_objfile_data *bp_objfile_data;
3507
3508 gdbarch = get_objfile_arch (objfile);
3509
3510 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3511
3512 if (!bp_objfile_data->longjmp_searched)
3513 {
3514 VEC (probe_p) *ret;
3515
3516 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3517 if (ret != NULL)
3518 {
3519 /* We are only interested in checking one element. */
3520 struct probe *p = VEC_index (probe_p, ret, 0);
3521
3522 if (!can_evaluate_probe_arguments (p))
3523 {
3524 /* We cannot use the probe interface here, because it does
3525 not know how to evaluate arguments. */
3526 VEC_free (probe_p, ret);
3527 ret = NULL;
3528 }
3529 }
3530 bp_objfile_data->longjmp_probes = ret;
3531 bp_objfile_data->longjmp_searched = 1;
3532 }
3533
3534 if (bp_objfile_data->longjmp_probes != NULL)
3535 {
3536 int i;
3537 struct probe *probe;
3538 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3539
3540 for (i = 0;
3541 VEC_iterate (probe_p,
3542 bp_objfile_data->longjmp_probes,
3543 i, probe);
3544 ++i)
3545 {
3546 struct breakpoint *b;
3547
3548 b = create_internal_breakpoint (gdbarch,
3549 get_probe_address (probe,
3550 objfile),
3551 bp_longjmp_master,
3552 &internal_breakpoint_ops);
3553 b->location = new_probe_location ("-probe-stap libc:longjmp");
3554 b->enable_state = bp_disabled;
3555 }
3556
3557 continue;
3558 }
3559
3560 if (!gdbarch_get_longjmp_target_p (gdbarch))
3561 continue;
3562
3563 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3564 {
3565 struct breakpoint *b;
3566 const char *func_name;
3567 CORE_ADDR addr;
3568 struct explicit_location explicit_loc;
3569
3570 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3571 continue;
3572
3573 func_name = longjmp_names[i];
3574 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3575 {
3576 struct bound_minimal_symbol m;
3577
3578 m = lookup_minimal_symbol_text (func_name, objfile);
3579 if (m.minsym == NULL)
3580 {
3581 /* Prevent future lookups in this objfile. */
3582 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3583 continue;
3584 }
3585 bp_objfile_data->longjmp_msym[i] = m;
3586 }
3587
3588 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3589 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3590 &internal_breakpoint_ops);
3591 initialize_explicit_location (&explicit_loc);
3592 explicit_loc.function_name = ASTRDUP (func_name);
3593 b->location = new_explicit_location (&explicit_loc);
3594 b->enable_state = bp_disabled;
3595 }
3596 }
3597 }
3598
3599 do_cleanups (old_chain);
3600 }
3601
3602 /* Create a master std::terminate breakpoint. */
3603 static void
3604 create_std_terminate_master_breakpoint (void)
3605 {
3606 struct program_space *pspace;
3607 struct cleanup *old_chain;
3608 const char *const func_name = "std::terminate()";
3609
3610 old_chain = save_current_program_space ();
3611
3612 ALL_PSPACES (pspace)
3613 {
3614 struct objfile *objfile;
3615 CORE_ADDR addr;
3616
3617 set_current_program_space (pspace);
3618
3619 ALL_OBJFILES (objfile)
3620 {
3621 struct breakpoint *b;
3622 struct breakpoint_objfile_data *bp_objfile_data;
3623 struct explicit_location explicit_loc;
3624
3625 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3626
3627 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3628 continue;
3629
3630 if (bp_objfile_data->terminate_msym.minsym == NULL)
3631 {
3632 struct bound_minimal_symbol m;
3633
3634 m = lookup_minimal_symbol (func_name, NULL, objfile);
3635 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3636 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3637 {
3638 /* Prevent future lookups in this objfile. */
3639 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3640 continue;
3641 }
3642 bp_objfile_data->terminate_msym = m;
3643 }
3644
3645 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3646 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3647 bp_std_terminate_master,
3648 &internal_breakpoint_ops);
3649 initialize_explicit_location (&explicit_loc);
3650 explicit_loc.function_name = ASTRDUP (func_name);
3651 b->location = new_explicit_location (&explicit_loc);
3652 b->enable_state = bp_disabled;
3653 }
3654 }
3655
3656 do_cleanups (old_chain);
3657 }
3658
3659 /* Install a master breakpoint on the unwinder's debug hook. */
3660
3661 static void
3662 create_exception_master_breakpoint (void)
3663 {
3664 struct objfile *objfile;
3665 const char *const func_name = "_Unwind_DebugHook";
3666
3667 ALL_OBJFILES (objfile)
3668 {
3669 struct breakpoint *b;
3670 struct gdbarch *gdbarch;
3671 struct breakpoint_objfile_data *bp_objfile_data;
3672 CORE_ADDR addr;
3673 struct explicit_location explicit_loc;
3674
3675 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3676
3677 /* We prefer the SystemTap probe point if it exists. */
3678 if (!bp_objfile_data->exception_searched)
3679 {
3680 VEC (probe_p) *ret;
3681
3682 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3683
3684 if (ret != NULL)
3685 {
3686 /* We are only interested in checking one element. */
3687 struct probe *p = VEC_index (probe_p, ret, 0);
3688
3689 if (!can_evaluate_probe_arguments (p))
3690 {
3691 /* We cannot use the probe interface here, because it does
3692 not know how to evaluate arguments. */
3693 VEC_free (probe_p, ret);
3694 ret = NULL;
3695 }
3696 }
3697 bp_objfile_data->exception_probes = ret;
3698 bp_objfile_data->exception_searched = 1;
3699 }
3700
3701 if (bp_objfile_data->exception_probes != NULL)
3702 {
3703 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3704 int i;
3705 struct probe *probe;
3706
3707 for (i = 0;
3708 VEC_iterate (probe_p,
3709 bp_objfile_data->exception_probes,
3710 i, probe);
3711 ++i)
3712 {
3713 struct breakpoint *b;
3714
3715 b = create_internal_breakpoint (gdbarch,
3716 get_probe_address (probe,
3717 objfile),
3718 bp_exception_master,
3719 &internal_breakpoint_ops);
3720 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3721 b->enable_state = bp_disabled;
3722 }
3723
3724 continue;
3725 }
3726
3727 /* Otherwise, try the hook function. */
3728
3729 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3730 continue;
3731
3732 gdbarch = get_objfile_arch (objfile);
3733
3734 if (bp_objfile_data->exception_msym.minsym == NULL)
3735 {
3736 struct bound_minimal_symbol debug_hook;
3737
3738 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3739 if (debug_hook.minsym == NULL)
3740 {
3741 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3742 continue;
3743 }
3744
3745 bp_objfile_data->exception_msym = debug_hook;
3746 }
3747
3748 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3749 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3750 &current_target);
3751 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3752 &internal_breakpoint_ops);
3753 initialize_explicit_location (&explicit_loc);
3754 explicit_loc.function_name = ASTRDUP (func_name);
3755 b->location = new_explicit_location (&explicit_loc);
3756 b->enable_state = bp_disabled;
3757 }
3758 }
3759
3760 /* Does B have a location spec? */
3761
3762 static int
3763 breakpoint_event_location_empty_p (const struct breakpoint *b)
3764 {
3765 return b->location != NULL && event_location_empty_p (b->location.get ());
3766 }
3767
3768 void
3769 update_breakpoints_after_exec (void)
3770 {
3771 struct breakpoint *b, *b_tmp;
3772 struct bp_location *bploc, **bplocp_tmp;
3773
3774 /* We're about to delete breakpoints from GDB's lists. If the
3775 INSERTED flag is true, GDB will try to lift the breakpoints by
3776 writing the breakpoints' "shadow contents" back into memory. The
3777 "shadow contents" are NOT valid after an exec, so GDB should not
3778 do that. Instead, the target is responsible from marking
3779 breakpoints out as soon as it detects an exec. We don't do that
3780 here instead, because there may be other attempts to delete
3781 breakpoints after detecting an exec and before reaching here. */
3782 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3783 if (bploc->pspace == current_program_space)
3784 gdb_assert (!bploc->inserted);
3785
3786 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3787 {
3788 if (b->pspace != current_program_space)
3789 continue;
3790
3791 /* Solib breakpoints must be explicitly reset after an exec(). */
3792 if (b->type == bp_shlib_event)
3793 {
3794 delete_breakpoint (b);
3795 continue;
3796 }
3797
3798 /* JIT breakpoints must be explicitly reset after an exec(). */
3799 if (b->type == bp_jit_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
3804
3805 /* Thread event breakpoints must be set anew after an exec(),
3806 as must overlay event and longjmp master breakpoints. */
3807 if (b->type == bp_thread_event || b->type == bp_overlay_event
3808 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3809 || b->type == bp_exception_master)
3810 {
3811 delete_breakpoint (b);
3812 continue;
3813 }
3814
3815 /* Step-resume breakpoints are meaningless after an exec(). */
3816 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
3822 /* Just like single-step breakpoints. */
3823 if (b->type == bp_single_step)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
3829 /* Longjmp and longjmp-resume breakpoints are also meaningless
3830 after an exec. */
3831 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3832 || b->type == bp_longjmp_call_dummy
3833 || b->type == bp_exception || b->type == bp_exception_resume)
3834 {
3835 delete_breakpoint (b);
3836 continue;
3837 }
3838
3839 if (b->type == bp_catchpoint)
3840 {
3841 /* For now, none of the bp_catchpoint breakpoints need to
3842 do anything at this point. In the future, if some of
3843 the catchpoints need to something, we will need to add
3844 a new method, and call this method from here. */
3845 continue;
3846 }
3847
3848 /* bp_finish is a special case. The only way we ought to be able
3849 to see one of these when an exec() has happened, is if the user
3850 caught a vfork, and then said "finish". Ordinarily a finish just
3851 carries them to the call-site of the current callee, by setting
3852 a temporary bp there and resuming. But in this case, the finish
3853 will carry them entirely through the vfork & exec.
3854
3855 We don't want to allow a bp_finish to remain inserted now. But
3856 we can't safely delete it, 'cause finish_command has a handle to
3857 the bp on a bpstat, and will later want to delete it. There's a
3858 chance (and I've seen it happen) that if we delete the bp_finish
3859 here, that its storage will get reused by the time finish_command
3860 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3861 We really must allow finish_command to delete a bp_finish.
3862
3863 In the absence of a general solution for the "how do we know
3864 it's safe to delete something others may have handles to?"
3865 problem, what we'll do here is just uninsert the bp_finish, and
3866 let finish_command delete it.
3867
3868 (We know the bp_finish is "doomed" in the sense that it's
3869 momentary, and will be deleted as soon as finish_command sees
3870 the inferior stopped. So it doesn't matter that the bp's
3871 address is probably bogus in the new a.out, unlike e.g., the
3872 solib breakpoints.) */
3873
3874 if (b->type == bp_finish)
3875 {
3876 continue;
3877 }
3878
3879 /* Without a symbolic address, we have little hope of the
3880 pre-exec() address meaning the same thing in the post-exec()
3881 a.out. */
3882 if (breakpoint_event_location_empty_p (b))
3883 {
3884 delete_breakpoint (b);
3885 continue;
3886 }
3887 }
3888 }
3889
3890 int
3891 detach_breakpoints (ptid_t ptid)
3892 {
3893 struct bp_location *bl, **blp_tmp;
3894 int val = 0;
3895 struct cleanup *old_chain = save_inferior_ptid ();
3896 struct inferior *inf = current_inferior ();
3897
3898 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3899 error (_("Cannot detach breakpoints of inferior_ptid"));
3900
3901 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3902 inferior_ptid = ptid;
3903 ALL_BP_LOCATIONS (bl, blp_tmp)
3904 {
3905 if (bl->pspace != inf->pspace)
3906 continue;
3907
3908 /* This function must physically remove breakpoints locations
3909 from the specified ptid, without modifying the breakpoint
3910 package's state. Locations of type bp_loc_other are only
3911 maintained at GDB side. So, there is no need to remove
3912 these bp_loc_other locations. Moreover, removing these
3913 would modify the breakpoint package's state. */
3914 if (bl->loc_type == bp_loc_other)
3915 continue;
3916
3917 if (bl->inserted)
3918 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3919 }
3920
3921 do_cleanups (old_chain);
3922 return val;
3923 }
3924
3925 /* Remove the breakpoint location BL from the current address space.
3926 Note that this is used to detach breakpoints from a child fork.
3927 When we get here, the child isn't in the inferior list, and neither
3928 do we have objects to represent its address space --- we should
3929 *not* look at bl->pspace->aspace here. */
3930
3931 static int
3932 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3933 {
3934 int val;
3935
3936 /* BL is never in moribund_locations by our callers. */
3937 gdb_assert (bl->owner != NULL);
3938
3939 /* The type of none suggests that owner is actually deleted.
3940 This should not ever happen. */
3941 gdb_assert (bl->owner->type != bp_none);
3942
3943 if (bl->loc_type == bp_loc_software_breakpoint
3944 || bl->loc_type == bp_loc_hardware_breakpoint)
3945 {
3946 /* "Normal" instruction breakpoint: either the standard
3947 trap-instruction bp (bp_breakpoint), or a
3948 bp_hardware_breakpoint. */
3949
3950 /* First check to see if we have to handle an overlay. */
3951 if (overlay_debugging == ovly_off
3952 || bl->section == NULL
3953 || !(section_is_overlay (bl->section)))
3954 {
3955 /* No overlay handling: just remove the breakpoint. */
3956
3957 /* If we're trying to uninsert a memory breakpoint that we
3958 know is set in a dynamic object that is marked
3959 shlib_disabled, then either the dynamic object was
3960 removed with "remove-symbol-file" or with
3961 "nosharedlibrary". In the former case, we don't know
3962 whether another dynamic object might have loaded over the
3963 breakpoint's address -- the user might well let us know
3964 about it next with add-symbol-file (the whole point of
3965 add-symbol-file is letting the user manually maintain a
3966 list of dynamically loaded objects). If we have the
3967 breakpoint's shadow memory, that is, this is a software
3968 breakpoint managed by GDB, check whether the breakpoint
3969 is still inserted in memory, to avoid overwriting wrong
3970 code with stale saved shadow contents. Note that HW
3971 breakpoints don't have shadow memory, as they're
3972 implemented using a mechanism that is not dependent on
3973 being able to modify the target's memory, and as such
3974 they should always be removed. */
3975 if (bl->shlib_disabled
3976 && bl->target_info.shadow_len != 0
3977 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3978 val = 0;
3979 else
3980 val = bl->owner->ops->remove_location (bl, reason);
3981 }
3982 else
3983 {
3984 /* This breakpoint is in an overlay section.
3985 Did we set a breakpoint at the LMA? */
3986 if (!overlay_events_enabled)
3987 {
3988 /* Yes -- overlay event support is not active, so we
3989 should have set a breakpoint at the LMA. Remove it.
3990 */
3991 /* Ignore any failures: if the LMA is in ROM, we will
3992 have already warned when we failed to insert it. */
3993 if (bl->loc_type == bp_loc_hardware_breakpoint)
3994 target_remove_hw_breakpoint (bl->gdbarch,
3995 &bl->overlay_target_info);
3996 else
3997 target_remove_breakpoint (bl->gdbarch,
3998 &bl->overlay_target_info,
3999 reason);
4000 }
4001 /* Did we set a breakpoint at the VMA?
4002 If so, we will have marked the breakpoint 'inserted'. */
4003 if (bl->inserted)
4004 {
4005 /* Yes -- remove it. Previously we did not bother to
4006 remove the breakpoint if the section had been
4007 unmapped, but let's not rely on that being safe. We
4008 don't know what the overlay manager might do. */
4009
4010 /* However, we should remove *software* breakpoints only
4011 if the section is still mapped, or else we overwrite
4012 wrong code with the saved shadow contents. */
4013 if (bl->loc_type == bp_loc_hardware_breakpoint
4014 || section_is_mapped (bl->section))
4015 val = bl->owner->ops->remove_location (bl, reason);
4016 else
4017 val = 0;
4018 }
4019 else
4020 {
4021 /* No -- not inserted, so no need to remove. No error. */
4022 val = 0;
4023 }
4024 }
4025
4026 /* In some cases, we might not be able to remove a breakpoint in
4027 a shared library that has already been removed, but we have
4028 not yet processed the shlib unload event. Similarly for an
4029 unloaded add-symbol-file object - the user might not yet have
4030 had the chance to remove-symbol-file it. shlib_disabled will
4031 be set if the library/object has already been removed, but
4032 the breakpoint hasn't been uninserted yet, e.g., after
4033 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4034 always-inserted mode. */
4035 if (val
4036 && (bl->loc_type == bp_loc_software_breakpoint
4037 && (bl->shlib_disabled
4038 || solib_name_from_address (bl->pspace, bl->address)
4039 || shared_objfile_contains_address_p (bl->pspace,
4040 bl->address))))
4041 val = 0;
4042
4043 if (val)
4044 return val;
4045 bl->inserted = (reason == DETACH_BREAKPOINT);
4046 }
4047 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4048 {
4049 gdb_assert (bl->owner->ops != NULL
4050 && bl->owner->ops->remove_location != NULL);
4051
4052 bl->inserted = (reason == DETACH_BREAKPOINT);
4053 bl->owner->ops->remove_location (bl, reason);
4054
4055 /* Failure to remove any of the hardware watchpoints comes here. */
4056 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4057 warning (_("Could not remove hardware watchpoint %d."),
4058 bl->owner->number);
4059 }
4060 else if (bl->owner->type == bp_catchpoint
4061 && breakpoint_enabled (bl->owner)
4062 && !bl->duplicate)
4063 {
4064 gdb_assert (bl->owner->ops != NULL
4065 && bl->owner->ops->remove_location != NULL);
4066
4067 val = bl->owner->ops->remove_location (bl, reason);
4068 if (val)
4069 return val;
4070
4071 bl->inserted = (reason == DETACH_BREAKPOINT);
4072 }
4073
4074 return 0;
4075 }
4076
4077 static int
4078 remove_breakpoint (struct bp_location *bl)
4079 {
4080 int ret;
4081 struct cleanup *old_chain;
4082
4083 /* BL is never in moribund_locations by our callers. */
4084 gdb_assert (bl->owner != NULL);
4085
4086 /* The type of none suggests that owner is actually deleted.
4087 This should not ever happen. */
4088 gdb_assert (bl->owner->type != bp_none);
4089
4090 old_chain = save_current_space_and_thread ();
4091
4092 switch_to_program_space_and_thread (bl->pspace);
4093
4094 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4095
4096 do_cleanups (old_chain);
4097 return ret;
4098 }
4099
4100 /* Clear the "inserted" flag in all breakpoints. */
4101
4102 void
4103 mark_breakpoints_out (void)
4104 {
4105 struct bp_location *bl, **blp_tmp;
4106
4107 ALL_BP_LOCATIONS (bl, blp_tmp)
4108 if (bl->pspace == current_program_space)
4109 bl->inserted = 0;
4110 }
4111
4112 /* Clear the "inserted" flag in all breakpoints and delete any
4113 breakpoints which should go away between runs of the program.
4114
4115 Plus other such housekeeping that has to be done for breakpoints
4116 between runs.
4117
4118 Note: this function gets called at the end of a run (by
4119 generic_mourn_inferior) and when a run begins (by
4120 init_wait_for_inferior). */
4121
4122
4123
4124 void
4125 breakpoint_init_inferior (enum inf_context context)
4126 {
4127 struct breakpoint *b, *b_tmp;
4128 struct bp_location *bl;
4129 int ix;
4130 struct program_space *pspace = current_program_space;
4131
4132 /* If breakpoint locations are shared across processes, then there's
4133 nothing to do. */
4134 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4135 return;
4136
4137 mark_breakpoints_out ();
4138
4139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4140 {
4141 if (b->loc && b->loc->pspace != pspace)
4142 continue;
4143
4144 switch (b->type)
4145 {
4146 case bp_call_dummy:
4147 case bp_longjmp_call_dummy:
4148
4149 /* If the call dummy breakpoint is at the entry point it will
4150 cause problems when the inferior is rerun, so we better get
4151 rid of it. */
4152
4153 case bp_watchpoint_scope:
4154
4155 /* Also get rid of scope breakpoints. */
4156
4157 case bp_shlib_event:
4158
4159 /* Also remove solib event breakpoints. Their addresses may
4160 have changed since the last time we ran the program.
4161 Actually we may now be debugging against different target;
4162 and so the solib backend that installed this breakpoint may
4163 not be used in by the target. E.g.,
4164
4165 (gdb) file prog-linux
4166 (gdb) run # native linux target
4167 ...
4168 (gdb) kill
4169 (gdb) file prog-win.exe
4170 (gdb) tar rem :9999 # remote Windows gdbserver.
4171 */
4172
4173 case bp_step_resume:
4174
4175 /* Also remove step-resume breakpoints. */
4176
4177 case bp_single_step:
4178
4179 /* Also remove single-step breakpoints. */
4180
4181 delete_breakpoint (b);
4182 break;
4183
4184 case bp_watchpoint:
4185 case bp_hardware_watchpoint:
4186 case bp_read_watchpoint:
4187 case bp_access_watchpoint:
4188 {
4189 struct watchpoint *w = (struct watchpoint *) b;
4190
4191 /* Likewise for watchpoints on local expressions. */
4192 if (w->exp_valid_block != NULL)
4193 delete_breakpoint (b);
4194 else
4195 {
4196 /* Get rid of existing locations, which are no longer
4197 valid. New ones will be created in
4198 update_watchpoint, when the inferior is restarted.
4199 The next update_global_location_list call will
4200 garbage collect them. */
4201 b->loc = NULL;
4202
4203 if (context == inf_starting)
4204 {
4205 /* Reset val field to force reread of starting value in
4206 insert_breakpoints. */
4207 if (w->val)
4208 value_free (w->val);
4209 w->val = NULL;
4210 w->val_valid = 0;
4211 }
4212 }
4213 }
4214 break;
4215 default:
4216 break;
4217 }
4218 }
4219
4220 /* Get rid of the moribund locations. */
4221 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4222 decref_bp_location (&bl);
4223 VEC_free (bp_location_p, moribund_locations);
4224 }
4225
4226 /* These functions concern about actual breakpoints inserted in the
4227 target --- to e.g. check if we need to do decr_pc adjustment or if
4228 we need to hop over the bkpt --- so we check for address space
4229 match, not program space. */
4230
4231 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4232 exists at PC. It returns ordinary_breakpoint_here if it's an
4233 ordinary breakpoint, or permanent_breakpoint_here if it's a
4234 permanent breakpoint.
4235 - When continuing from a location with an ordinary breakpoint, we
4236 actually single step once before calling insert_breakpoints.
4237 - When continuing from a location with a permanent breakpoint, we
4238 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4239 the target, to advance the PC past the breakpoint. */
4240
4241 enum breakpoint_here
4242 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4243 {
4244 struct bp_location *bl, **blp_tmp;
4245 int any_breakpoint_here = 0;
4246
4247 ALL_BP_LOCATIONS (bl, blp_tmp)
4248 {
4249 if (bl->loc_type != bp_loc_software_breakpoint
4250 && bl->loc_type != bp_loc_hardware_breakpoint)
4251 continue;
4252
4253 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4254 if ((breakpoint_enabled (bl->owner)
4255 || bl->permanent)
4256 && breakpoint_location_address_match (bl, aspace, pc))
4257 {
4258 if (overlay_debugging
4259 && section_is_overlay (bl->section)
4260 && !section_is_mapped (bl->section))
4261 continue; /* unmapped overlay -- can't be a match */
4262 else if (bl->permanent)
4263 return permanent_breakpoint_here;
4264 else
4265 any_breakpoint_here = 1;
4266 }
4267 }
4268
4269 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4270 }
4271
4272 /* See breakpoint.h. */
4273
4274 int
4275 breakpoint_in_range_p (struct address_space *aspace,
4276 CORE_ADDR addr, ULONGEST len)
4277 {
4278 struct bp_location *bl, **blp_tmp;
4279
4280 ALL_BP_LOCATIONS (bl, blp_tmp)
4281 {
4282 if (bl->loc_type != bp_loc_software_breakpoint
4283 && bl->loc_type != bp_loc_hardware_breakpoint)
4284 continue;
4285
4286 if ((breakpoint_enabled (bl->owner)
4287 || bl->permanent)
4288 && breakpoint_location_address_range_overlap (bl, aspace,
4289 addr, len))
4290 {
4291 if (overlay_debugging
4292 && section_is_overlay (bl->section)
4293 && !section_is_mapped (bl->section))
4294 {
4295 /* Unmapped overlay -- can't be a match. */
4296 continue;
4297 }
4298
4299 return 1;
4300 }
4301 }
4302
4303 return 0;
4304 }
4305
4306 /* Return true if there's a moribund breakpoint at PC. */
4307
4308 int
4309 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4310 {
4311 struct bp_location *loc;
4312 int ix;
4313
4314 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4315 if (breakpoint_location_address_match (loc, aspace, pc))
4316 return 1;
4317
4318 return 0;
4319 }
4320
4321 /* Returns non-zero iff BL is inserted at PC, in address space
4322 ASPACE. */
4323
4324 static int
4325 bp_location_inserted_here_p (struct bp_location *bl,
4326 struct address_space *aspace, CORE_ADDR pc)
4327 {
4328 if (bl->inserted
4329 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4330 aspace, pc))
4331 {
4332 if (overlay_debugging
4333 && section_is_overlay (bl->section)
4334 && !section_is_mapped (bl->section))
4335 return 0; /* unmapped overlay -- can't be a match */
4336 else
4337 return 1;
4338 }
4339 return 0;
4340 }
4341
4342 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4343
4344 int
4345 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4346 {
4347 struct bp_location **blp, **blp_tmp = NULL;
4348
4349 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4350 {
4351 struct bp_location *bl = *blp;
4352
4353 if (bl->loc_type != bp_loc_software_breakpoint
4354 && bl->loc_type != bp_loc_hardware_breakpoint)
4355 continue;
4356
4357 if (bp_location_inserted_here_p (bl, aspace, pc))
4358 return 1;
4359 }
4360 return 0;
4361 }
4362
4363 /* This function returns non-zero iff there is a software breakpoint
4364 inserted at PC. */
4365
4366 int
4367 software_breakpoint_inserted_here_p (struct address_space *aspace,
4368 CORE_ADDR pc)
4369 {
4370 struct bp_location **blp, **blp_tmp = NULL;
4371
4372 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4373 {
4374 struct bp_location *bl = *blp;
4375
4376 if (bl->loc_type != bp_loc_software_breakpoint)
4377 continue;
4378
4379 if (bp_location_inserted_here_p (bl, aspace, pc))
4380 return 1;
4381 }
4382
4383 return 0;
4384 }
4385
4386 /* See breakpoint.h. */
4387
4388 int
4389 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4390 CORE_ADDR pc)
4391 {
4392 struct bp_location **blp, **blp_tmp = NULL;
4393
4394 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4395 {
4396 struct bp_location *bl = *blp;
4397
4398 if (bl->loc_type != bp_loc_hardware_breakpoint)
4399 continue;
4400
4401 if (bp_location_inserted_here_p (bl, aspace, pc))
4402 return 1;
4403 }
4404
4405 return 0;
4406 }
4407
4408 int
4409 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4410 CORE_ADDR addr, ULONGEST len)
4411 {
4412 struct breakpoint *bpt;
4413
4414 ALL_BREAKPOINTS (bpt)
4415 {
4416 struct bp_location *loc;
4417
4418 if (bpt->type != bp_hardware_watchpoint
4419 && bpt->type != bp_access_watchpoint)
4420 continue;
4421
4422 if (!breakpoint_enabled (bpt))
4423 continue;
4424
4425 for (loc = bpt->loc; loc; loc = loc->next)
4426 if (loc->pspace->aspace == aspace && loc->inserted)
4427 {
4428 CORE_ADDR l, h;
4429
4430 /* Check for intersection. */
4431 l = std::max<CORE_ADDR> (loc->address, addr);
4432 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4433 if (l < h)
4434 return 1;
4435 }
4436 }
4437 return 0;
4438 }
4439 \f
4440
4441 /* bpstat stuff. External routines' interfaces are documented
4442 in breakpoint.h. */
4443
4444 int
4445 is_catchpoint (struct breakpoint *ep)
4446 {
4447 return (ep->type == bp_catchpoint);
4448 }
4449
4450 /* Frees any storage that is part of a bpstat. Does not walk the
4451 'next' chain. */
4452
4453 static void
4454 bpstat_free (bpstat bs)
4455 {
4456 if (bs->old_val != NULL)
4457 value_free (bs->old_val);
4458 decref_counted_command_line (&bs->commands);
4459 decref_bp_location (&bs->bp_location_at);
4460 xfree (bs);
4461 }
4462
4463 /* Clear a bpstat so that it says we are not at any breakpoint.
4464 Also free any storage that is part of a bpstat. */
4465
4466 void
4467 bpstat_clear (bpstat *bsp)
4468 {
4469 bpstat p;
4470 bpstat q;
4471
4472 if (bsp == 0)
4473 return;
4474 p = *bsp;
4475 while (p != NULL)
4476 {
4477 q = p->next;
4478 bpstat_free (p);
4479 p = q;
4480 }
4481 *bsp = NULL;
4482 }
4483
4484 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4485 is part of the bpstat is copied as well. */
4486
4487 bpstat
4488 bpstat_copy (bpstat bs)
4489 {
4490 bpstat p = NULL;
4491 bpstat tmp;
4492 bpstat retval = NULL;
4493
4494 if (bs == NULL)
4495 return bs;
4496
4497 for (; bs != NULL; bs = bs->next)
4498 {
4499 tmp = (bpstat) xmalloc (sizeof (*tmp));
4500 memcpy (tmp, bs, sizeof (*tmp));
4501 incref_counted_command_line (tmp->commands);
4502 incref_bp_location (tmp->bp_location_at);
4503 if (bs->old_val != NULL)
4504 {
4505 tmp->old_val = value_copy (bs->old_val);
4506 release_value (tmp->old_val);
4507 }
4508
4509 if (p == NULL)
4510 /* This is the first thing in the chain. */
4511 retval = tmp;
4512 else
4513 p->next = tmp;
4514 p = tmp;
4515 }
4516 p->next = NULL;
4517 return retval;
4518 }
4519
4520 /* Find the bpstat associated with this breakpoint. */
4521
4522 bpstat
4523 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4524 {
4525 if (bsp == NULL)
4526 return NULL;
4527
4528 for (; bsp != NULL; bsp = bsp->next)
4529 {
4530 if (bsp->breakpoint_at == breakpoint)
4531 return bsp;
4532 }
4533 return NULL;
4534 }
4535
4536 /* See breakpoint.h. */
4537
4538 int
4539 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4540 {
4541 for (; bsp != NULL; bsp = bsp->next)
4542 {
4543 if (bsp->breakpoint_at == NULL)
4544 {
4545 /* A moribund location can never explain a signal other than
4546 GDB_SIGNAL_TRAP. */
4547 if (sig == GDB_SIGNAL_TRAP)
4548 return 1;
4549 }
4550 else
4551 {
4552 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4553 sig))
4554 return 1;
4555 }
4556 }
4557
4558 return 0;
4559 }
4560
4561 /* Put in *NUM the breakpoint number of the first breakpoint we are
4562 stopped at. *BSP upon return is a bpstat which points to the
4563 remaining breakpoints stopped at (but which is not guaranteed to be
4564 good for anything but further calls to bpstat_num).
4565
4566 Return 0 if passed a bpstat which does not indicate any breakpoints.
4567 Return -1 if stopped at a breakpoint that has been deleted since
4568 we set it.
4569 Return 1 otherwise. */
4570
4571 int
4572 bpstat_num (bpstat *bsp, int *num)
4573 {
4574 struct breakpoint *b;
4575
4576 if ((*bsp) == NULL)
4577 return 0; /* No more breakpoint values */
4578
4579 /* We assume we'll never have several bpstats that correspond to a
4580 single breakpoint -- otherwise, this function might return the
4581 same number more than once and this will look ugly. */
4582 b = (*bsp)->breakpoint_at;
4583 *bsp = (*bsp)->next;
4584 if (b == NULL)
4585 return -1; /* breakpoint that's been deleted since */
4586
4587 *num = b->number; /* We have its number */
4588 return 1;
4589 }
4590
4591 /* See breakpoint.h. */
4592
4593 void
4594 bpstat_clear_actions (void)
4595 {
4596 struct thread_info *tp;
4597 bpstat bs;
4598
4599 if (ptid_equal (inferior_ptid, null_ptid))
4600 return;
4601
4602 tp = find_thread_ptid (inferior_ptid);
4603 if (tp == NULL)
4604 return;
4605
4606 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4607 {
4608 decref_counted_command_line (&bs->commands);
4609
4610 if (bs->old_val != NULL)
4611 {
4612 value_free (bs->old_val);
4613 bs->old_val = NULL;
4614 }
4615 }
4616 }
4617
4618 /* Called when a command is about to proceed the inferior. */
4619
4620 static void
4621 breakpoint_about_to_proceed (void)
4622 {
4623 if (!ptid_equal (inferior_ptid, null_ptid))
4624 {
4625 struct thread_info *tp = inferior_thread ();
4626
4627 /* Allow inferior function calls in breakpoint commands to not
4628 interrupt the command list. When the call finishes
4629 successfully, the inferior will be standing at the same
4630 breakpoint as if nothing happened. */
4631 if (tp->control.in_infcall)
4632 return;
4633 }
4634
4635 breakpoint_proceeded = 1;
4636 }
4637
4638 /* Stub for cleaning up our state if we error-out of a breakpoint
4639 command. */
4640 static void
4641 cleanup_executing_breakpoints (void *ignore)
4642 {
4643 executing_breakpoint_commands = 0;
4644 }
4645
4646 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4647 or its equivalent. */
4648
4649 static int
4650 command_line_is_silent (struct command_line *cmd)
4651 {
4652 return cmd && (strcmp ("silent", 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 scoped_restore preventer = 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 (current_ui->async)
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 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4789 debugging multiple threads. */
4790
4791 void
4792 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4793 {
4794 if (uiout->is_mi_like_p ())
4795 return;
4796
4797 uiout->text ("\n");
4798
4799 if (show_thread_that_caused_stop ())
4800 {
4801 const char *name;
4802 struct thread_info *thr = inferior_thread ();
4803
4804 uiout->text ("Thread ");
4805 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4806
4807 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4808 if (name != NULL)
4809 {
4810 uiout->text (" \"");
4811 uiout->field_fmt ("name", "%s", name);
4812 uiout->text ("\"");
4813 }
4814
4815 uiout->text (" hit ");
4816 }
4817 }
4818
4819 /* Generic routine for printing messages indicating why we
4820 stopped. The behavior of this function depends on the value
4821 'print_it' in the bpstat structure. Under some circumstances we
4822 may decide not to print anything here and delegate the task to
4823 normal_stop(). */
4824
4825 static enum print_stop_action
4826 print_bp_stop_message (bpstat bs)
4827 {
4828 switch (bs->print_it)
4829 {
4830 case print_it_noop:
4831 /* Nothing should be printed for this bpstat entry. */
4832 return PRINT_UNKNOWN;
4833 break;
4834
4835 case print_it_done:
4836 /* We still want to print the frame, but we already printed the
4837 relevant messages. */
4838 return PRINT_SRC_AND_LOC;
4839 break;
4840
4841 case print_it_normal:
4842 {
4843 struct breakpoint *b = bs->breakpoint_at;
4844
4845 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4846 which has since been deleted. */
4847 if (b == NULL)
4848 return PRINT_UNKNOWN;
4849
4850 /* Normal case. Call the breakpoint's print_it method. */
4851 return b->ops->print_it (bs);
4852 }
4853 break;
4854
4855 default:
4856 internal_error (__FILE__, __LINE__,
4857 _("print_bp_stop_message: unrecognized enum value"));
4858 break;
4859 }
4860 }
4861
4862 /* A helper function that prints a shared library stopped event. */
4863
4864 static void
4865 print_solib_event (int is_catchpoint)
4866 {
4867 int any_deleted
4868 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4869 int any_added
4870 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4871
4872 if (!is_catchpoint)
4873 {
4874 if (any_added || any_deleted)
4875 current_uiout->text (_("Stopped due to shared library event:\n"));
4876 else
4877 current_uiout->text (_("Stopped due to shared library event (no "
4878 "libraries added or removed)\n"));
4879 }
4880
4881 if (current_uiout->is_mi_like_p ())
4882 current_uiout->field_string ("reason",
4883 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4884
4885 if (any_deleted)
4886 {
4887 char *name;
4888 int ix;
4889
4890 current_uiout->text (_(" Inferior unloaded "));
4891 ui_out_emit_list list_emitter (current_uiout, "removed");
4892 for (ix = 0;
4893 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4894 ix, name);
4895 ++ix)
4896 {
4897 if (ix > 0)
4898 current_uiout->text (" ");
4899 current_uiout->field_string ("library", name);
4900 current_uiout->text ("\n");
4901 }
4902 }
4903
4904 if (any_added)
4905 {
4906 struct so_list *iter;
4907 int ix;
4908
4909 current_uiout->text (_(" Inferior loaded "));
4910 ui_out_emit_list list_emitter (current_uiout, "added");
4911 for (ix = 0;
4912 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4913 ix, iter);
4914 ++ix)
4915 {
4916 if (ix > 0)
4917 current_uiout->text (" ");
4918 current_uiout->field_string ("library", iter->so_name);
4919 current_uiout->text ("\n");
4920 }
4921 }
4922 }
4923
4924 /* Print a message indicating what happened. This is called from
4925 normal_stop(). The input to this routine is the head of the bpstat
4926 list - a list of the eventpoints that caused this stop. KIND is
4927 the target_waitkind for the stopping event. This
4928 routine calls the generic print routine for printing a message
4929 about reasons for stopping. This will print (for example) the
4930 "Breakpoint n," part of the output. The return value of this
4931 routine is one of:
4932
4933 PRINT_UNKNOWN: Means we printed nothing.
4934 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4935 code to print the location. An example is
4936 "Breakpoint 1, " which should be followed by
4937 the location.
4938 PRINT_SRC_ONLY: Means we printed something, but there is no need
4939 to also print the location part of the message.
4940 An example is the catch/throw messages, which
4941 don't require a location appended to the end.
4942 PRINT_NOTHING: We have done some printing and we don't need any
4943 further info to be printed. */
4944
4945 enum print_stop_action
4946 bpstat_print (bpstat bs, int kind)
4947 {
4948 enum print_stop_action val;
4949
4950 /* Maybe another breakpoint in the chain caused us to stop.
4951 (Currently all watchpoints go on the bpstat whether hit or not.
4952 That probably could (should) be changed, provided care is taken
4953 with respect to bpstat_explains_signal). */
4954 for (; bs; bs = bs->next)
4955 {
4956 val = print_bp_stop_message (bs);
4957 if (val == PRINT_SRC_ONLY
4958 || val == PRINT_SRC_AND_LOC
4959 || val == PRINT_NOTHING)
4960 return val;
4961 }
4962
4963 /* If we had hit a shared library event breakpoint,
4964 print_bp_stop_message would print out this message. If we hit an
4965 OS-level shared library event, do the same thing. */
4966 if (kind == TARGET_WAITKIND_LOADED)
4967 {
4968 print_solib_event (0);
4969 return PRINT_NOTHING;
4970 }
4971
4972 /* We reached the end of the chain, or we got a null BS to start
4973 with and nothing was printed. */
4974 return PRINT_UNKNOWN;
4975 }
4976
4977 /* Evaluate the expression EXP and return 1 if value is zero.
4978 This returns the inverse of the condition because it is called
4979 from catch_errors which returns 0 if an exception happened, and if an
4980 exception happens we want execution to stop.
4981 The argument is a "struct expression *" that has been cast to a
4982 "void *" to make it pass through catch_errors. */
4983
4984 static int
4985 breakpoint_cond_eval (void *exp)
4986 {
4987 struct value *mark = value_mark ();
4988 int i = !value_true (evaluate_expression ((struct expression *) exp));
4989
4990 value_free_to_mark (mark);
4991 return i;
4992 }
4993
4994 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4995
4996 static bpstat
4997 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4998 {
4999 bpstat bs;
5000
5001 bs = (bpstat) xmalloc (sizeof (*bs));
5002 bs->next = NULL;
5003 **bs_link_pointer = bs;
5004 *bs_link_pointer = &bs->next;
5005 bs->breakpoint_at = bl->owner;
5006 bs->bp_location_at = bl;
5007 incref_bp_location (bl);
5008 /* If the condition is false, etc., don't do the commands. */
5009 bs->commands = NULL;
5010 bs->old_val = NULL;
5011 bs->print_it = print_it_normal;
5012 return bs;
5013 }
5014 \f
5015 /* The target has stopped with waitstatus WS. Check if any hardware
5016 watchpoints have triggered, according to the target. */
5017
5018 int
5019 watchpoints_triggered (struct target_waitstatus *ws)
5020 {
5021 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5022 CORE_ADDR addr;
5023 struct breakpoint *b;
5024
5025 if (!stopped_by_watchpoint)
5026 {
5027 /* We were not stopped by a watchpoint. Mark all watchpoints
5028 as not triggered. */
5029 ALL_BREAKPOINTS (b)
5030 if (is_hardware_watchpoint (b))
5031 {
5032 struct watchpoint *w = (struct watchpoint *) b;
5033
5034 w->watchpoint_triggered = watch_triggered_no;
5035 }
5036
5037 return 0;
5038 }
5039
5040 if (!target_stopped_data_address (&current_target, &addr))
5041 {
5042 /* We were stopped by a watchpoint, but we don't know where.
5043 Mark all watchpoints as unknown. */
5044 ALL_BREAKPOINTS (b)
5045 if (is_hardware_watchpoint (b))
5046 {
5047 struct watchpoint *w = (struct watchpoint *) b;
5048
5049 w->watchpoint_triggered = watch_triggered_unknown;
5050 }
5051
5052 return 1;
5053 }
5054
5055 /* The target could report the data address. Mark watchpoints
5056 affected by this data address as triggered, and all others as not
5057 triggered. */
5058
5059 ALL_BREAKPOINTS (b)
5060 if (is_hardware_watchpoint (b))
5061 {
5062 struct watchpoint *w = (struct watchpoint *) b;
5063 struct bp_location *loc;
5064
5065 w->watchpoint_triggered = watch_triggered_no;
5066 for (loc = b->loc; loc; loc = loc->next)
5067 {
5068 if (is_masked_watchpoint (b))
5069 {
5070 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5071 CORE_ADDR start = loc->address & w->hw_wp_mask;
5072
5073 if (newaddr == start)
5074 {
5075 w->watchpoint_triggered = watch_triggered_yes;
5076 break;
5077 }
5078 }
5079 /* Exact match not required. Within range is sufficient. */
5080 else if (target_watchpoint_addr_within_range (&current_target,
5081 addr, loc->address,
5082 loc->length))
5083 {
5084 w->watchpoint_triggered = watch_triggered_yes;
5085 break;
5086 }
5087 }
5088 }
5089
5090 return 1;
5091 }
5092
5093 /* Possible return values for watchpoint_check (this can't be an enum
5094 because of check_errors). */
5095 /* The watchpoint has been deleted. */
5096 #define WP_DELETED 1
5097 /* The value has changed. */
5098 #define WP_VALUE_CHANGED 2
5099 /* The value has not changed. */
5100 #define WP_VALUE_NOT_CHANGED 3
5101 /* Ignore this watchpoint, no matter if the value changed or not. */
5102 #define WP_IGNORE 4
5103
5104 #define BP_TEMPFLAG 1
5105 #define BP_HARDWAREFLAG 2
5106
5107 /* Evaluate watchpoint condition expression and check if its value
5108 changed.
5109
5110 P should be a pointer to struct bpstat, but is defined as a void *
5111 in order for this function to be usable with catch_errors. */
5112
5113 static int
5114 watchpoint_check (void *p)
5115 {
5116 bpstat bs = (bpstat) p;
5117 struct watchpoint *b;
5118 struct frame_info *fr;
5119 int within_current_scope;
5120
5121 /* BS is built from an existing struct breakpoint. */
5122 gdb_assert (bs->breakpoint_at != NULL);
5123 b = (struct watchpoint *) bs->breakpoint_at;
5124
5125 /* If this is a local watchpoint, we only want to check if the
5126 watchpoint frame is in scope if the current thread is the thread
5127 that was used to create the watchpoint. */
5128 if (!watchpoint_in_thread_scope (b))
5129 return WP_IGNORE;
5130
5131 if (b->exp_valid_block == NULL)
5132 within_current_scope = 1;
5133 else
5134 {
5135 struct frame_info *frame = get_current_frame ();
5136 struct gdbarch *frame_arch = get_frame_arch (frame);
5137 CORE_ADDR frame_pc = get_frame_pc (frame);
5138
5139 /* stack_frame_destroyed_p() returns a non-zero value if we're
5140 still in the function but the stack frame has already been
5141 invalidated. Since we can't rely on the values of local
5142 variables after the stack has been destroyed, we are treating
5143 the watchpoint in that state as `not changed' without further
5144 checking. Don't mark watchpoints as changed if the current
5145 frame is in an epilogue - even if they are in some other
5146 frame, our view of the stack is likely to be wrong and
5147 frame_find_by_id could error out. */
5148 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5149 return WP_IGNORE;
5150
5151 fr = frame_find_by_id (b->watchpoint_frame);
5152 within_current_scope = (fr != NULL);
5153
5154 /* If we've gotten confused in the unwinder, we might have
5155 returned a frame that can't describe this variable. */
5156 if (within_current_scope)
5157 {
5158 struct symbol *function;
5159
5160 function = get_frame_function (fr);
5161 if (function == NULL
5162 || !contained_in (b->exp_valid_block,
5163 SYMBOL_BLOCK_VALUE (function)))
5164 within_current_scope = 0;
5165 }
5166
5167 if (within_current_scope)
5168 /* If we end up stopping, the current frame will get selected
5169 in normal_stop. So this call to select_frame won't affect
5170 the user. */
5171 select_frame (fr);
5172 }
5173
5174 if (within_current_scope)
5175 {
5176 /* We use value_{,free_to_}mark because it could be a *long*
5177 time before we return to the command level and call
5178 free_all_values. We can't call free_all_values because we
5179 might be in the middle of evaluating a function call. */
5180
5181 int pc = 0;
5182 struct value *mark;
5183 struct value *new_val;
5184
5185 if (is_masked_watchpoint (&b->base))
5186 /* Since we don't know the exact trigger address (from
5187 stopped_data_address), just tell the user we've triggered
5188 a mask watchpoint. */
5189 return WP_VALUE_CHANGED;
5190
5191 mark = value_mark ();
5192 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5193
5194 if (b->val_bitsize != 0)
5195 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5196
5197 /* We use value_equal_contents instead of value_equal because
5198 the latter coerces an array to a pointer, thus comparing just
5199 the address of the array instead of its contents. This is
5200 not what we want. */
5201 if ((b->val != NULL) != (new_val != NULL)
5202 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5203 {
5204 if (new_val != NULL)
5205 {
5206 release_value (new_val);
5207 value_free_to_mark (mark);
5208 }
5209 bs->old_val = b->val;
5210 b->val = new_val;
5211 b->val_valid = 1;
5212 return WP_VALUE_CHANGED;
5213 }
5214 else
5215 {
5216 /* Nothing changed. */
5217 value_free_to_mark (mark);
5218 return WP_VALUE_NOT_CHANGED;
5219 }
5220 }
5221 else
5222 {
5223 /* This seems like the only logical thing to do because
5224 if we temporarily ignored the watchpoint, then when
5225 we reenter the block in which it is valid it contains
5226 garbage (in the case of a function, it may have two
5227 garbage values, one before and one after the prologue).
5228 So we can't even detect the first assignment to it and
5229 watch after that (since the garbage may or may not equal
5230 the first value assigned). */
5231 /* We print all the stop information in
5232 breakpoint_ops->print_it, but in this case, by the time we
5233 call breakpoint_ops->print_it this bp will be deleted
5234 already. So we have no choice but print the information
5235 here. */
5236
5237 SWITCH_THRU_ALL_UIS ()
5238 {
5239 struct ui_out *uiout = current_uiout;
5240
5241 if (uiout->is_mi_like_p ())
5242 uiout->field_string
5243 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5244 uiout->text ("\nWatchpoint ");
5245 uiout->field_int ("wpnum", b->base.number);
5246 uiout->text (" deleted because the program has left the block in\n"
5247 "which its expression is valid.\n");
5248 }
5249
5250 /* Make sure the watchpoint's commands aren't executed. */
5251 decref_counted_command_line (&b->base.commands);
5252 watchpoint_del_at_next_stop (b);
5253
5254 return WP_DELETED;
5255 }
5256 }
5257
5258 /* Return true if it looks like target has stopped due to hitting
5259 breakpoint location BL. This function does not check if we should
5260 stop, only if BL explains the stop. */
5261
5262 static int
5263 bpstat_check_location (const struct bp_location *bl,
5264 struct address_space *aspace, CORE_ADDR bp_addr,
5265 const struct target_waitstatus *ws)
5266 {
5267 struct breakpoint *b = bl->owner;
5268
5269 /* BL is from an existing breakpoint. */
5270 gdb_assert (b != NULL);
5271
5272 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5273 }
5274
5275 /* Determine if the watched values have actually changed, and we
5276 should stop. If not, set BS->stop to 0. */
5277
5278 static void
5279 bpstat_check_watchpoint (bpstat bs)
5280 {
5281 const struct bp_location *bl;
5282 struct watchpoint *b;
5283
5284 /* BS is built for existing struct breakpoint. */
5285 bl = bs->bp_location_at;
5286 gdb_assert (bl != NULL);
5287 b = (struct watchpoint *) bs->breakpoint_at;
5288 gdb_assert (b != NULL);
5289
5290 {
5291 int must_check_value = 0;
5292
5293 if (b->base.type == bp_watchpoint)
5294 /* For a software watchpoint, we must always check the
5295 watched value. */
5296 must_check_value = 1;
5297 else if (b->watchpoint_triggered == watch_triggered_yes)
5298 /* We have a hardware watchpoint (read, write, or access)
5299 and the target earlier reported an address watched by
5300 this watchpoint. */
5301 must_check_value = 1;
5302 else if (b->watchpoint_triggered == watch_triggered_unknown
5303 && b->base.type == bp_hardware_watchpoint)
5304 /* We were stopped by a hardware watchpoint, but the target could
5305 not report the data address. We must check the watchpoint's
5306 value. Access and read watchpoints are out of luck; without
5307 a data address, we can't figure it out. */
5308 must_check_value = 1;
5309
5310 if (must_check_value)
5311 {
5312 char *message
5313 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5314 b->base.number);
5315 struct cleanup *cleanups = make_cleanup (xfree, message);
5316 int e = catch_errors (watchpoint_check, bs, message,
5317 RETURN_MASK_ALL);
5318 do_cleanups (cleanups);
5319 switch (e)
5320 {
5321 case WP_DELETED:
5322 /* We've already printed what needs to be printed. */
5323 bs->print_it = print_it_done;
5324 /* Stop. */
5325 break;
5326 case WP_IGNORE:
5327 bs->print_it = print_it_noop;
5328 bs->stop = 0;
5329 break;
5330 case WP_VALUE_CHANGED:
5331 if (b->base.type == bp_read_watchpoint)
5332 {
5333 /* There are two cases to consider here:
5334
5335 1. We're watching the triggered memory for reads.
5336 In that case, trust the target, and always report
5337 the watchpoint hit to the user. Even though
5338 reads don't cause value changes, the value may
5339 have changed since the last time it was read, and
5340 since we're not trapping writes, we will not see
5341 those, and as such we should ignore our notion of
5342 old value.
5343
5344 2. We're watching the triggered memory for both
5345 reads and writes. There are two ways this may
5346 happen:
5347
5348 2.1. This is a target that can't break on data
5349 reads only, but can break on accesses (reads or
5350 writes), such as e.g., x86. We detect this case
5351 at the time we try to insert read watchpoints.
5352
5353 2.2. Otherwise, the target supports read
5354 watchpoints, but, the user set an access or write
5355 watchpoint watching the same memory as this read
5356 watchpoint.
5357
5358 If we're watching memory writes as well as reads,
5359 ignore watchpoint hits when we find that the
5360 value hasn't changed, as reads don't cause
5361 changes. This still gives false positives when
5362 the program writes the same value to memory as
5363 what there was already in memory (we will confuse
5364 it for a read), but it's much better than
5365 nothing. */
5366
5367 int other_write_watchpoint = 0;
5368
5369 if (bl->watchpoint_type == hw_read)
5370 {
5371 struct breakpoint *other_b;
5372
5373 ALL_BREAKPOINTS (other_b)
5374 if (other_b->type == bp_hardware_watchpoint
5375 || other_b->type == bp_access_watchpoint)
5376 {
5377 struct watchpoint *other_w =
5378 (struct watchpoint *) other_b;
5379
5380 if (other_w->watchpoint_triggered
5381 == watch_triggered_yes)
5382 {
5383 other_write_watchpoint = 1;
5384 break;
5385 }
5386 }
5387 }
5388
5389 if (other_write_watchpoint
5390 || bl->watchpoint_type == hw_access)
5391 {
5392 /* We're watching the same memory for writes,
5393 and the value changed since the last time we
5394 updated it, so this trap must be for a write.
5395 Ignore it. */
5396 bs->print_it = print_it_noop;
5397 bs->stop = 0;
5398 }
5399 }
5400 break;
5401 case WP_VALUE_NOT_CHANGED:
5402 if (b->base.type == bp_hardware_watchpoint
5403 || b->base.type == bp_watchpoint)
5404 {
5405 /* Don't stop: write watchpoints shouldn't fire if
5406 the value hasn't changed. */
5407 bs->print_it = print_it_noop;
5408 bs->stop = 0;
5409 }
5410 /* Stop. */
5411 break;
5412 default:
5413 /* Can't happen. */
5414 case 0:
5415 /* Error from catch_errors. */
5416 {
5417 SWITCH_THRU_ALL_UIS ()
5418 {
5419 printf_filtered (_("Watchpoint %d deleted.\n"),
5420 b->base.number);
5421 }
5422 watchpoint_del_at_next_stop (b);
5423 /* We've already printed what needs to be printed. */
5424 bs->print_it = print_it_done;
5425 }
5426 break;
5427 }
5428 }
5429 else /* must_check_value == 0 */
5430 {
5431 /* This is a case where some watchpoint(s) triggered, but
5432 not at the address of this watchpoint, or else no
5433 watchpoint triggered after all. So don't print
5434 anything for this watchpoint. */
5435 bs->print_it = print_it_noop;
5436 bs->stop = 0;
5437 }
5438 }
5439 }
5440
5441 /* For breakpoints that are currently marked as telling gdb to stop,
5442 check conditions (condition proper, frame, thread and ignore count)
5443 of breakpoint referred to by BS. If we should not stop for this
5444 breakpoint, set BS->stop to 0. */
5445
5446 static void
5447 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5448 {
5449 const struct bp_location *bl;
5450 struct breakpoint *b;
5451 int value_is_zero = 0;
5452 struct expression *cond;
5453
5454 gdb_assert (bs->stop);
5455
5456 /* BS is built for existing struct breakpoint. */
5457 bl = bs->bp_location_at;
5458 gdb_assert (bl != NULL);
5459 b = bs->breakpoint_at;
5460 gdb_assert (b != NULL);
5461
5462 /* Even if the target evaluated the condition on its end and notified GDB, we
5463 need to do so again since GDB does not know if we stopped due to a
5464 breakpoint or a single step breakpoint. */
5465
5466 if (frame_id_p (b->frame_id)
5467 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5468 {
5469 bs->stop = 0;
5470 return;
5471 }
5472
5473 /* If this is a thread/task-specific breakpoint, don't waste cpu
5474 evaluating the condition if this isn't the specified
5475 thread/task. */
5476 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5477 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5478
5479 {
5480 bs->stop = 0;
5481 return;
5482 }
5483
5484 /* Evaluate extension language breakpoints that have a "stop" method
5485 implemented. */
5486 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5487
5488 if (is_watchpoint (b))
5489 {
5490 struct watchpoint *w = (struct watchpoint *) b;
5491
5492 cond = w->cond_exp.get ();
5493 }
5494 else
5495 cond = bl->cond.get ();
5496
5497 if (cond && b->disposition != disp_del_at_next_stop)
5498 {
5499 int within_current_scope = 1;
5500 struct watchpoint * w;
5501
5502 /* We use value_mark and value_free_to_mark because it could
5503 be a long time before we return to the command level and
5504 call free_all_values. We can't call free_all_values
5505 because we might be in the middle of evaluating a
5506 function call. */
5507 struct value *mark = value_mark ();
5508
5509 if (is_watchpoint (b))
5510 w = (struct watchpoint *) b;
5511 else
5512 w = NULL;
5513
5514 /* Need to select the frame, with all that implies so that
5515 the conditions will have the right context. Because we
5516 use the frame, we will not see an inlined function's
5517 variables when we arrive at a breakpoint at the start
5518 of the inlined function; the current frame will be the
5519 call site. */
5520 if (w == NULL || w->cond_exp_valid_block == NULL)
5521 select_frame (get_current_frame ());
5522 else
5523 {
5524 struct frame_info *frame;
5525
5526 /* For local watchpoint expressions, which particular
5527 instance of a local is being watched matters, so we
5528 keep track of the frame to evaluate the expression
5529 in. To evaluate the condition however, it doesn't
5530 really matter which instantiation of the function
5531 where the condition makes sense triggers the
5532 watchpoint. This allows an expression like "watch
5533 global if q > 10" set in `func', catch writes to
5534 global on all threads that call `func', or catch
5535 writes on all recursive calls of `func' by a single
5536 thread. We simply always evaluate the condition in
5537 the innermost frame that's executing where it makes
5538 sense to evaluate the condition. It seems
5539 intuitive. */
5540 frame = block_innermost_frame (w->cond_exp_valid_block);
5541 if (frame != NULL)
5542 select_frame (frame);
5543 else
5544 within_current_scope = 0;
5545 }
5546 if (within_current_scope)
5547 value_is_zero
5548 = catch_errors (breakpoint_cond_eval, cond,
5549 "Error in testing breakpoint condition:\n",
5550 RETURN_MASK_ALL);
5551 else
5552 {
5553 warning (_("Watchpoint condition cannot be tested "
5554 "in the current scope"));
5555 /* If we failed to set the right context for this
5556 watchpoint, unconditionally report it. */
5557 value_is_zero = 0;
5558 }
5559 /* FIXME-someday, should give breakpoint #. */
5560 value_free_to_mark (mark);
5561 }
5562
5563 if (cond && value_is_zero)
5564 {
5565 bs->stop = 0;
5566 }
5567 else if (b->ignore_count > 0)
5568 {
5569 b->ignore_count--;
5570 bs->stop = 0;
5571 /* Increase the hit count even though we don't stop. */
5572 ++(b->hit_count);
5573 observer_notify_breakpoint_modified (b);
5574 }
5575 }
5576
5577 /* Returns true if we need to track moribund locations of LOC's type
5578 on the current target. */
5579
5580 static int
5581 need_moribund_for_location_type (struct bp_location *loc)
5582 {
5583 return ((loc->loc_type == bp_loc_software_breakpoint
5584 && !target_supports_stopped_by_sw_breakpoint ())
5585 || (loc->loc_type == bp_loc_hardware_breakpoint
5586 && !target_supports_stopped_by_hw_breakpoint ()));
5587 }
5588
5589
5590 /* Get a bpstat associated with having just stopped at address
5591 BP_ADDR in thread PTID.
5592
5593 Determine whether we stopped at a breakpoint, etc, or whether we
5594 don't understand this stop. Result is a chain of bpstat's such
5595 that:
5596
5597 if we don't understand the stop, the result is a null pointer.
5598
5599 if we understand why we stopped, the result is not null.
5600
5601 Each element of the chain refers to a particular breakpoint or
5602 watchpoint at which we have stopped. (We may have stopped for
5603 several reasons concurrently.)
5604
5605 Each element of the chain has valid next, breakpoint_at,
5606 commands, FIXME??? fields. */
5607
5608 bpstat
5609 bpstat_stop_status (struct address_space *aspace,
5610 CORE_ADDR bp_addr, ptid_t ptid,
5611 const struct target_waitstatus *ws)
5612 {
5613 struct breakpoint *b = NULL;
5614 struct bp_location *bl;
5615 struct bp_location *loc;
5616 /* First item of allocated bpstat's. */
5617 bpstat bs_head = NULL, *bs_link = &bs_head;
5618 /* Pointer to the last thing in the chain currently. */
5619 bpstat bs;
5620 int ix;
5621 int need_remove_insert;
5622 int removed_any;
5623
5624 /* First, build the bpstat chain with locations that explain a
5625 target stop, while being careful to not set the target running,
5626 as that may invalidate locations (in particular watchpoint
5627 locations are recreated). Resuming will happen here with
5628 breakpoint conditions or watchpoint expressions that include
5629 inferior function calls. */
5630
5631 ALL_BREAKPOINTS (b)
5632 {
5633 if (!breakpoint_enabled (b))
5634 continue;
5635
5636 for (bl = b->loc; bl != NULL; bl = bl->next)
5637 {
5638 /* For hardware watchpoints, we look only at the first
5639 location. The watchpoint_check function will work on the
5640 entire expression, not the individual locations. For
5641 read watchpoints, the watchpoints_triggered function has
5642 checked all locations already. */
5643 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5644 break;
5645
5646 if (!bl->enabled || bl->shlib_disabled)
5647 continue;
5648
5649 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5650 continue;
5651
5652 /* Come here if it's a watchpoint, or if the break address
5653 matches. */
5654
5655 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5656 explain stop. */
5657
5658 /* Assume we stop. Should we find a watchpoint that is not
5659 actually triggered, or if the condition of the breakpoint
5660 evaluates as false, we'll reset 'stop' to 0. */
5661 bs->stop = 1;
5662 bs->print = 1;
5663
5664 /* If this is a scope breakpoint, mark the associated
5665 watchpoint as triggered so that we will handle the
5666 out-of-scope event. We'll get to the watchpoint next
5667 iteration. */
5668 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5669 {
5670 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5671
5672 w->watchpoint_triggered = watch_triggered_yes;
5673 }
5674 }
5675 }
5676
5677 /* Check if a moribund breakpoint explains the stop. */
5678 if (!target_supports_stopped_by_sw_breakpoint ()
5679 || !target_supports_stopped_by_hw_breakpoint ())
5680 {
5681 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5682 {
5683 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5684 && need_moribund_for_location_type (loc))
5685 {
5686 bs = bpstat_alloc (loc, &bs_link);
5687 /* For hits of moribund locations, we should just proceed. */
5688 bs->stop = 0;
5689 bs->print = 0;
5690 bs->print_it = print_it_noop;
5691 }
5692 }
5693 }
5694
5695 /* A bit of special processing for shlib breakpoints. We need to
5696 process solib loading here, so that the lists of loaded and
5697 unloaded libraries are correct before we handle "catch load" and
5698 "catch unload". */
5699 for (bs = bs_head; bs != NULL; bs = bs->next)
5700 {
5701 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5702 {
5703 handle_solib_event ();
5704 break;
5705 }
5706 }
5707
5708 /* Now go through the locations that caused the target to stop, and
5709 check whether we're interested in reporting this stop to higher
5710 layers, or whether we should resume the target transparently. */
5711
5712 removed_any = 0;
5713
5714 for (bs = bs_head; bs != NULL; bs = bs->next)
5715 {
5716 if (!bs->stop)
5717 continue;
5718
5719 b = bs->breakpoint_at;
5720 b->ops->check_status (bs);
5721 if (bs->stop)
5722 {
5723 bpstat_check_breakpoint_conditions (bs, ptid);
5724
5725 if (bs->stop)
5726 {
5727 ++(b->hit_count);
5728 observer_notify_breakpoint_modified (b);
5729
5730 /* We will stop here. */
5731 if (b->disposition == disp_disable)
5732 {
5733 --(b->enable_count);
5734 if (b->enable_count <= 0)
5735 b->enable_state = bp_disabled;
5736 removed_any = 1;
5737 }
5738 if (b->silent)
5739 bs->print = 0;
5740 bs->commands = b->commands;
5741 incref_counted_command_line (bs->commands);
5742 if (command_line_is_silent (bs->commands
5743 ? bs->commands->commands : NULL))
5744 bs->print = 0;
5745
5746 b->ops->after_condition_true (bs);
5747 }
5748
5749 }
5750
5751 /* Print nothing for this entry if we don't stop or don't
5752 print. */
5753 if (!bs->stop || !bs->print)
5754 bs->print_it = print_it_noop;
5755 }
5756
5757 /* If we aren't stopping, the value of some hardware watchpoint may
5758 not have changed, but the intermediate memory locations we are
5759 watching may have. Don't bother if we're stopping; this will get
5760 done later. */
5761 need_remove_insert = 0;
5762 if (! bpstat_causes_stop (bs_head))
5763 for (bs = bs_head; bs != NULL; bs = bs->next)
5764 if (!bs->stop
5765 && bs->breakpoint_at
5766 && is_hardware_watchpoint (bs->breakpoint_at))
5767 {
5768 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5769
5770 update_watchpoint (w, 0 /* don't reparse. */);
5771 need_remove_insert = 1;
5772 }
5773
5774 if (need_remove_insert)
5775 update_global_location_list (UGLL_MAY_INSERT);
5776 else if (removed_any)
5777 update_global_location_list (UGLL_DONT_INSERT);
5778
5779 return bs_head;
5780 }
5781
5782 static void
5783 handle_jit_event (void)
5784 {
5785 struct frame_info *frame;
5786 struct gdbarch *gdbarch;
5787
5788 if (debug_infrun)
5789 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5790
5791 /* Switch terminal for any messages produced by
5792 breakpoint_re_set. */
5793 target_terminal_ours_for_output ();
5794
5795 frame = get_current_frame ();
5796 gdbarch = get_frame_arch (frame);
5797
5798 jit_event_handler (gdbarch);
5799
5800 target_terminal_inferior ();
5801 }
5802
5803 /* Prepare WHAT final decision for infrun. */
5804
5805 /* Decide what infrun needs to do with this bpstat. */
5806
5807 struct bpstat_what
5808 bpstat_what (bpstat bs_head)
5809 {
5810 struct bpstat_what retval;
5811 bpstat bs;
5812
5813 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5814 retval.call_dummy = STOP_NONE;
5815 retval.is_longjmp = 0;
5816
5817 for (bs = bs_head; bs != NULL; bs = bs->next)
5818 {
5819 /* Extract this BS's action. After processing each BS, we check
5820 if its action overrides all we've seem so far. */
5821 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5822 enum bptype bptype;
5823
5824 if (bs->breakpoint_at == NULL)
5825 {
5826 /* I suspect this can happen if it was a momentary
5827 breakpoint which has since been deleted. */
5828 bptype = bp_none;
5829 }
5830 else
5831 bptype = bs->breakpoint_at->type;
5832
5833 switch (bptype)
5834 {
5835 case bp_none:
5836 break;
5837 case bp_breakpoint:
5838 case bp_hardware_breakpoint:
5839 case bp_single_step:
5840 case bp_until:
5841 case bp_finish:
5842 case bp_shlib_event:
5843 if (bs->stop)
5844 {
5845 if (bs->print)
5846 this_action = BPSTAT_WHAT_STOP_NOISY;
5847 else
5848 this_action = BPSTAT_WHAT_STOP_SILENT;
5849 }
5850 else
5851 this_action = BPSTAT_WHAT_SINGLE;
5852 break;
5853 case bp_watchpoint:
5854 case bp_hardware_watchpoint:
5855 case bp_read_watchpoint:
5856 case bp_access_watchpoint:
5857 if (bs->stop)
5858 {
5859 if (bs->print)
5860 this_action = BPSTAT_WHAT_STOP_NOISY;
5861 else
5862 this_action = BPSTAT_WHAT_STOP_SILENT;
5863 }
5864 else
5865 {
5866 /* There was a watchpoint, but we're not stopping.
5867 This requires no further action. */
5868 }
5869 break;
5870 case bp_longjmp:
5871 case bp_longjmp_call_dummy:
5872 case bp_exception:
5873 if (bs->stop)
5874 {
5875 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5876 retval.is_longjmp = bptype != bp_exception;
5877 }
5878 else
5879 this_action = BPSTAT_WHAT_SINGLE;
5880 break;
5881 case bp_longjmp_resume:
5882 case bp_exception_resume:
5883 if (bs->stop)
5884 {
5885 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5886 retval.is_longjmp = bptype == bp_longjmp_resume;
5887 }
5888 else
5889 this_action = BPSTAT_WHAT_SINGLE;
5890 break;
5891 case bp_step_resume:
5892 if (bs->stop)
5893 this_action = BPSTAT_WHAT_STEP_RESUME;
5894 else
5895 {
5896 /* It is for the wrong frame. */
5897 this_action = BPSTAT_WHAT_SINGLE;
5898 }
5899 break;
5900 case bp_hp_step_resume:
5901 if (bs->stop)
5902 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5903 else
5904 {
5905 /* It is for the wrong frame. */
5906 this_action = BPSTAT_WHAT_SINGLE;
5907 }
5908 break;
5909 case bp_watchpoint_scope:
5910 case bp_thread_event:
5911 case bp_overlay_event:
5912 case bp_longjmp_master:
5913 case bp_std_terminate_master:
5914 case bp_exception_master:
5915 this_action = BPSTAT_WHAT_SINGLE;
5916 break;
5917 case bp_catchpoint:
5918 if (bs->stop)
5919 {
5920 if (bs->print)
5921 this_action = BPSTAT_WHAT_STOP_NOISY;
5922 else
5923 this_action = BPSTAT_WHAT_STOP_SILENT;
5924 }
5925 else
5926 {
5927 /* There was a catchpoint, but we're not stopping.
5928 This requires no further action. */
5929 }
5930 break;
5931 case bp_jit_event:
5932 this_action = BPSTAT_WHAT_SINGLE;
5933 break;
5934 case bp_call_dummy:
5935 /* Make sure the action is stop (silent or noisy),
5936 so infrun.c pops the dummy frame. */
5937 retval.call_dummy = STOP_STACK_DUMMY;
5938 this_action = BPSTAT_WHAT_STOP_SILENT;
5939 break;
5940 case bp_std_terminate:
5941 /* Make sure the action is stop (silent or noisy),
5942 so infrun.c pops the dummy frame. */
5943 retval.call_dummy = STOP_STD_TERMINATE;
5944 this_action = BPSTAT_WHAT_STOP_SILENT;
5945 break;
5946 case bp_tracepoint:
5947 case bp_fast_tracepoint:
5948 case bp_static_tracepoint:
5949 /* Tracepoint hits should not be reported back to GDB, and
5950 if one got through somehow, it should have been filtered
5951 out already. */
5952 internal_error (__FILE__, __LINE__,
5953 _("bpstat_what: tracepoint encountered"));
5954 break;
5955 case bp_gnu_ifunc_resolver:
5956 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5957 this_action = BPSTAT_WHAT_SINGLE;
5958 break;
5959 case bp_gnu_ifunc_resolver_return:
5960 /* The breakpoint will be removed, execution will restart from the
5961 PC of the former breakpoint. */
5962 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5963 break;
5964
5965 case bp_dprintf:
5966 if (bs->stop)
5967 this_action = BPSTAT_WHAT_STOP_SILENT;
5968 else
5969 this_action = BPSTAT_WHAT_SINGLE;
5970 break;
5971
5972 default:
5973 internal_error (__FILE__, __LINE__,
5974 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5975 }
5976
5977 retval.main_action = std::max (retval.main_action, this_action);
5978 }
5979
5980 return retval;
5981 }
5982
5983 void
5984 bpstat_run_callbacks (bpstat bs_head)
5985 {
5986 bpstat bs;
5987
5988 for (bs = bs_head; bs != NULL; bs = bs->next)
5989 {
5990 struct breakpoint *b = bs->breakpoint_at;
5991
5992 if (b == NULL)
5993 continue;
5994 switch (b->type)
5995 {
5996 case bp_jit_event:
5997 handle_jit_event ();
5998 break;
5999 case bp_gnu_ifunc_resolver:
6000 gnu_ifunc_resolver_stop (b);
6001 break;
6002 case bp_gnu_ifunc_resolver_return:
6003 gnu_ifunc_resolver_return_stop (b);
6004 break;
6005 }
6006 }
6007 }
6008
6009 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6010 without hardware support). This isn't related to a specific bpstat,
6011 just to things like whether watchpoints are set. */
6012
6013 int
6014 bpstat_should_step (void)
6015 {
6016 struct breakpoint *b;
6017
6018 ALL_BREAKPOINTS (b)
6019 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6020 return 1;
6021 return 0;
6022 }
6023
6024 int
6025 bpstat_causes_stop (bpstat bs)
6026 {
6027 for (; bs != NULL; bs = bs->next)
6028 if (bs->stop)
6029 return 1;
6030
6031 return 0;
6032 }
6033
6034 \f
6035
6036 /* Compute a string of spaces suitable to indent the next line
6037 so it starts at the position corresponding to the table column
6038 named COL_NAME in the currently active table of UIOUT. */
6039
6040 static char *
6041 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6042 {
6043 static char wrap_indent[80];
6044 int i, total_width, width, align;
6045 const char *text;
6046
6047 total_width = 0;
6048 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6049 {
6050 if (strcmp (text, col_name) == 0)
6051 {
6052 gdb_assert (total_width < sizeof wrap_indent);
6053 memset (wrap_indent, ' ', total_width);
6054 wrap_indent[total_width] = 0;
6055
6056 return wrap_indent;
6057 }
6058
6059 total_width += width + 1;
6060 }
6061
6062 return NULL;
6063 }
6064
6065 /* Determine if the locations of this breakpoint will have their conditions
6066 evaluated by the target, host or a mix of both. Returns the following:
6067
6068 "host": Host evals condition.
6069 "host or target": Host or Target evals condition.
6070 "target": Target evals condition.
6071 */
6072
6073 static const char *
6074 bp_condition_evaluator (struct breakpoint *b)
6075 {
6076 struct bp_location *bl;
6077 char host_evals = 0;
6078 char target_evals = 0;
6079
6080 if (!b)
6081 return NULL;
6082
6083 if (!is_breakpoint (b))
6084 return NULL;
6085
6086 if (gdb_evaluates_breakpoint_condition_p ()
6087 || !target_supports_evaluation_of_breakpoint_conditions ())
6088 return condition_evaluation_host;
6089
6090 for (bl = b->loc; bl; bl = bl->next)
6091 {
6092 if (bl->cond_bytecode)
6093 target_evals++;
6094 else
6095 host_evals++;
6096 }
6097
6098 if (host_evals && target_evals)
6099 return condition_evaluation_both;
6100 else if (target_evals)
6101 return condition_evaluation_target;
6102 else
6103 return condition_evaluation_host;
6104 }
6105
6106 /* Determine the breakpoint location's condition evaluator. This is
6107 similar to bp_condition_evaluator, but for locations. */
6108
6109 static const char *
6110 bp_location_condition_evaluator (struct bp_location *bl)
6111 {
6112 if (bl && !is_breakpoint (bl->owner))
6113 return NULL;
6114
6115 if (gdb_evaluates_breakpoint_condition_p ()
6116 || !target_supports_evaluation_of_breakpoint_conditions ())
6117 return condition_evaluation_host;
6118
6119 if (bl && bl->cond_bytecode)
6120 return condition_evaluation_target;
6121 else
6122 return condition_evaluation_host;
6123 }
6124
6125 /* Print the LOC location out of the list of B->LOC locations. */
6126
6127 static void
6128 print_breakpoint_location (struct breakpoint *b,
6129 struct bp_location *loc)
6130 {
6131 struct ui_out *uiout = current_uiout;
6132 struct cleanup *old_chain = save_current_program_space ();
6133
6134 if (loc != NULL && loc->shlib_disabled)
6135 loc = NULL;
6136
6137 if (loc != NULL)
6138 set_current_program_space (loc->pspace);
6139
6140 if (b->display_canonical)
6141 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6142 else if (loc && loc->symtab)
6143 {
6144 struct symbol *sym
6145 = find_pc_sect_function (loc->address, loc->section);
6146 if (sym)
6147 {
6148 uiout->text ("in ");
6149 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6150 uiout->text (" ");
6151 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6152 uiout->text ("at ");
6153 }
6154 uiout->field_string ("file",
6155 symtab_to_filename_for_display (loc->symtab));
6156 uiout->text (":");
6157
6158 if (uiout->is_mi_like_p ())
6159 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6160
6161 uiout->field_int ("line", loc->line_number);
6162 }
6163 else if (loc)
6164 {
6165 string_file stb;
6166
6167 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6168 demangle, "");
6169 uiout->field_stream ("at", stb);
6170 }
6171 else
6172 {
6173 uiout->field_string ("pending",
6174 event_location_to_string (b->location.get ()));
6175 /* If extra_string is available, it could be holding a condition
6176 or dprintf arguments. In either case, make sure it is printed,
6177 too, but only for non-MI streams. */
6178 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6179 {
6180 if (b->type == bp_dprintf)
6181 uiout->text (",");
6182 else
6183 uiout->text (" ");
6184 uiout->text (b->extra_string);
6185 }
6186 }
6187
6188 if (loc && is_breakpoint (b)
6189 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6190 && bp_condition_evaluator (b) == condition_evaluation_both)
6191 {
6192 uiout->text (" (");
6193 uiout->field_string ("evaluated-by",
6194 bp_location_condition_evaluator (loc));
6195 uiout->text (")");
6196 }
6197
6198 do_cleanups (old_chain);
6199 }
6200
6201 static const char *
6202 bptype_string (enum bptype type)
6203 {
6204 struct ep_type_description
6205 {
6206 enum bptype type;
6207 const char *description;
6208 };
6209 static struct ep_type_description bptypes[] =
6210 {
6211 {bp_none, "?deleted?"},
6212 {bp_breakpoint, "breakpoint"},
6213 {bp_hardware_breakpoint, "hw breakpoint"},
6214 {bp_single_step, "sw single-step"},
6215 {bp_until, "until"},
6216 {bp_finish, "finish"},
6217 {bp_watchpoint, "watchpoint"},
6218 {bp_hardware_watchpoint, "hw watchpoint"},
6219 {bp_read_watchpoint, "read watchpoint"},
6220 {bp_access_watchpoint, "acc watchpoint"},
6221 {bp_longjmp, "longjmp"},
6222 {bp_longjmp_resume, "longjmp resume"},
6223 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6224 {bp_exception, "exception"},
6225 {bp_exception_resume, "exception resume"},
6226 {bp_step_resume, "step resume"},
6227 {bp_hp_step_resume, "high-priority step resume"},
6228 {bp_watchpoint_scope, "watchpoint scope"},
6229 {bp_call_dummy, "call dummy"},
6230 {bp_std_terminate, "std::terminate"},
6231 {bp_shlib_event, "shlib events"},
6232 {bp_thread_event, "thread events"},
6233 {bp_overlay_event, "overlay events"},
6234 {bp_longjmp_master, "longjmp master"},
6235 {bp_std_terminate_master, "std::terminate master"},
6236 {bp_exception_master, "exception master"},
6237 {bp_catchpoint, "catchpoint"},
6238 {bp_tracepoint, "tracepoint"},
6239 {bp_fast_tracepoint, "fast tracepoint"},
6240 {bp_static_tracepoint, "static tracepoint"},
6241 {bp_dprintf, "dprintf"},
6242 {bp_jit_event, "jit events"},
6243 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6244 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6245 };
6246
6247 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6248 || ((int) type != bptypes[(int) type].type))
6249 internal_error (__FILE__, __LINE__,
6250 _("bptypes table does not describe type #%d."),
6251 (int) type);
6252
6253 return bptypes[(int) type].description;
6254 }
6255
6256 /* For MI, output a field named 'thread-groups' with a list as the value.
6257 For CLI, prefix the list with the string 'inf'. */
6258
6259 static void
6260 output_thread_groups (struct ui_out *uiout,
6261 const char *field_name,
6262 VEC(int) *inf_num,
6263 int mi_only)
6264 {
6265 int is_mi = uiout->is_mi_like_p ();
6266 int inf;
6267 int i;
6268
6269 /* For backward compatibility, don't display inferiors in CLI unless
6270 there are several. Always display them for MI. */
6271 if (!is_mi && mi_only)
6272 return;
6273
6274 ui_out_emit_list list_emitter (uiout, field_name);
6275
6276 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6277 {
6278 if (is_mi)
6279 {
6280 char mi_group[10];
6281
6282 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6283 uiout->field_string (NULL, mi_group);
6284 }
6285 else
6286 {
6287 if (i == 0)
6288 uiout->text (" inf ");
6289 else
6290 uiout->text (", ");
6291
6292 uiout->text (plongest (inf));
6293 }
6294 }
6295 }
6296
6297 /* Print B to gdb_stdout. */
6298
6299 static void
6300 print_one_breakpoint_location (struct breakpoint *b,
6301 struct bp_location *loc,
6302 int loc_number,
6303 struct bp_location **last_loc,
6304 int allflag)
6305 {
6306 struct command_line *l;
6307 static char bpenables[] = "nynny";
6308
6309 struct ui_out *uiout = current_uiout;
6310 int header_of_multiple = 0;
6311 int part_of_multiple = (loc != NULL);
6312 struct value_print_options opts;
6313
6314 get_user_print_options (&opts);
6315
6316 gdb_assert (!loc || loc_number != 0);
6317 /* See comment in print_one_breakpoint concerning treatment of
6318 breakpoints with single disabled location. */
6319 if (loc == NULL
6320 && (b->loc != NULL
6321 && (b->loc->next != NULL || !b->loc->enabled)))
6322 header_of_multiple = 1;
6323 if (loc == NULL)
6324 loc = b->loc;
6325
6326 annotate_record ();
6327
6328 /* 1 */
6329 annotate_field (0);
6330 if (part_of_multiple)
6331 {
6332 char *formatted;
6333 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6334 uiout->field_string ("number", formatted);
6335 xfree (formatted);
6336 }
6337 else
6338 {
6339 uiout->field_int ("number", b->number);
6340 }
6341
6342 /* 2 */
6343 annotate_field (1);
6344 if (part_of_multiple)
6345 uiout->field_skip ("type");
6346 else
6347 uiout->field_string ("type", bptype_string (b->type));
6348
6349 /* 3 */
6350 annotate_field (2);
6351 if (part_of_multiple)
6352 uiout->field_skip ("disp");
6353 else
6354 uiout->field_string ("disp", bpdisp_text (b->disposition));
6355
6356
6357 /* 4 */
6358 annotate_field (3);
6359 if (part_of_multiple)
6360 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6361 else
6362 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6363 uiout->spaces (2);
6364
6365
6366 /* 5 and 6 */
6367 if (b->ops != NULL && b->ops->print_one != NULL)
6368 {
6369 /* Although the print_one can possibly print all locations,
6370 calling it here is not likely to get any nice result. So,
6371 make sure there's just one location. */
6372 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6373 b->ops->print_one (b, last_loc);
6374 }
6375 else
6376 switch (b->type)
6377 {
6378 case bp_none:
6379 internal_error (__FILE__, __LINE__,
6380 _("print_one_breakpoint: bp_none encountered\n"));
6381 break;
6382
6383 case bp_watchpoint:
6384 case bp_hardware_watchpoint:
6385 case bp_read_watchpoint:
6386 case bp_access_watchpoint:
6387 {
6388 struct watchpoint *w = (struct watchpoint *) b;
6389
6390 /* Field 4, the address, is omitted (which makes the columns
6391 not line up too nicely with the headers, but the effect
6392 is relatively readable). */
6393 if (opts.addressprint)
6394 uiout->field_skip ("addr");
6395 annotate_field (5);
6396 uiout->field_string ("what", w->exp_string);
6397 }
6398 break;
6399
6400 case bp_breakpoint:
6401 case bp_hardware_breakpoint:
6402 case bp_single_step:
6403 case bp_until:
6404 case bp_finish:
6405 case bp_longjmp:
6406 case bp_longjmp_resume:
6407 case bp_longjmp_call_dummy:
6408 case bp_exception:
6409 case bp_exception_resume:
6410 case bp_step_resume:
6411 case bp_hp_step_resume:
6412 case bp_watchpoint_scope:
6413 case bp_call_dummy:
6414 case bp_std_terminate:
6415 case bp_shlib_event:
6416 case bp_thread_event:
6417 case bp_overlay_event:
6418 case bp_longjmp_master:
6419 case bp_std_terminate_master:
6420 case bp_exception_master:
6421 case bp_tracepoint:
6422 case bp_fast_tracepoint:
6423 case bp_static_tracepoint:
6424 case bp_dprintf:
6425 case bp_jit_event:
6426 case bp_gnu_ifunc_resolver:
6427 case bp_gnu_ifunc_resolver_return:
6428 if (opts.addressprint)
6429 {
6430 annotate_field (4);
6431 if (header_of_multiple)
6432 uiout->field_string ("addr", "<MULTIPLE>");
6433 else if (b->loc == NULL || loc->shlib_disabled)
6434 uiout->field_string ("addr", "<PENDING>");
6435 else
6436 uiout->field_core_addr ("addr",
6437 loc->gdbarch, loc->address);
6438 }
6439 annotate_field (5);
6440 if (!header_of_multiple)
6441 print_breakpoint_location (b, loc);
6442 if (b->loc)
6443 *last_loc = b->loc;
6444 break;
6445 }
6446
6447
6448 if (loc != NULL && !header_of_multiple)
6449 {
6450 struct inferior *inf;
6451 VEC(int) *inf_num = NULL;
6452 int mi_only = 1;
6453
6454 ALL_INFERIORS (inf)
6455 {
6456 if (inf->pspace == loc->pspace)
6457 VEC_safe_push (int, inf_num, inf->num);
6458 }
6459
6460 /* For backward compatibility, don't display inferiors in CLI unless
6461 there are several. Always display for MI. */
6462 if (allflag
6463 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6464 && (number_of_program_spaces () > 1
6465 || number_of_inferiors () > 1)
6466 /* LOC is for existing B, it cannot be in
6467 moribund_locations and thus having NULL OWNER. */
6468 && loc->owner->type != bp_catchpoint))
6469 mi_only = 0;
6470 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6471 VEC_free (int, inf_num);
6472 }
6473
6474 if (!part_of_multiple)
6475 {
6476 if (b->thread != -1)
6477 {
6478 /* FIXME: This seems to be redundant and lost here; see the
6479 "stop only in" line a little further down. */
6480 uiout->text (" thread ");
6481 uiout->field_int ("thread", b->thread);
6482 }
6483 else if (b->task != 0)
6484 {
6485 uiout->text (" task ");
6486 uiout->field_int ("task", b->task);
6487 }
6488 }
6489
6490 uiout->text ("\n");
6491
6492 if (!part_of_multiple)
6493 b->ops->print_one_detail (b, uiout);
6494
6495 if (part_of_multiple && frame_id_p (b->frame_id))
6496 {
6497 annotate_field (6);
6498 uiout->text ("\tstop only in stack frame at ");
6499 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6500 the frame ID. */
6501 uiout->field_core_addr ("frame",
6502 b->gdbarch, b->frame_id.stack_addr);
6503 uiout->text ("\n");
6504 }
6505
6506 if (!part_of_multiple && b->cond_string)
6507 {
6508 annotate_field (7);
6509 if (is_tracepoint (b))
6510 uiout->text ("\ttrace only if ");
6511 else
6512 uiout->text ("\tstop only if ");
6513 uiout->field_string ("cond", b->cond_string);
6514
6515 /* Print whether the target is doing the breakpoint's condition
6516 evaluation. If GDB is doing the evaluation, don't print anything. */
6517 if (is_breakpoint (b)
6518 && breakpoint_condition_evaluation_mode ()
6519 == condition_evaluation_target)
6520 {
6521 uiout->text (" (");
6522 uiout->field_string ("evaluated-by",
6523 bp_condition_evaluator (b));
6524 uiout->text (" evals)");
6525 }
6526 uiout->text ("\n");
6527 }
6528
6529 if (!part_of_multiple && b->thread != -1)
6530 {
6531 /* FIXME should make an annotation for this. */
6532 uiout->text ("\tstop only in thread ");
6533 if (uiout->is_mi_like_p ())
6534 uiout->field_int ("thread", b->thread);
6535 else
6536 {
6537 struct thread_info *thr = find_thread_global_id (b->thread);
6538
6539 uiout->field_string ("thread", print_thread_id (thr));
6540 }
6541 uiout->text ("\n");
6542 }
6543
6544 if (!part_of_multiple)
6545 {
6546 if (b->hit_count)
6547 {
6548 /* FIXME should make an annotation for this. */
6549 if (is_catchpoint (b))
6550 uiout->text ("\tcatchpoint");
6551 else if (is_tracepoint (b))
6552 uiout->text ("\ttracepoint");
6553 else
6554 uiout->text ("\tbreakpoint");
6555 uiout->text (" already hit ");
6556 uiout->field_int ("times", b->hit_count);
6557 if (b->hit_count == 1)
6558 uiout->text (" time\n");
6559 else
6560 uiout->text (" times\n");
6561 }
6562 else
6563 {
6564 /* Output the count also if it is zero, but only if this is mi. */
6565 if (uiout->is_mi_like_p ())
6566 uiout->field_int ("times", b->hit_count);
6567 }
6568 }
6569
6570 if (!part_of_multiple && b->ignore_count)
6571 {
6572 annotate_field (8);
6573 uiout->text ("\tignore next ");
6574 uiout->field_int ("ignore", b->ignore_count);
6575 uiout->text (" hits\n");
6576 }
6577
6578 /* Note that an enable count of 1 corresponds to "enable once"
6579 behavior, which is reported by the combination of enablement and
6580 disposition, so we don't need to mention it here. */
6581 if (!part_of_multiple && b->enable_count > 1)
6582 {
6583 annotate_field (8);
6584 uiout->text ("\tdisable after ");
6585 /* Tweak the wording to clarify that ignore and enable counts
6586 are distinct, and have additive effect. */
6587 if (b->ignore_count)
6588 uiout->text ("additional ");
6589 else
6590 uiout->text ("next ");
6591 uiout->field_int ("enable", b->enable_count);
6592 uiout->text (" hits\n");
6593 }
6594
6595 if (!part_of_multiple && is_tracepoint (b))
6596 {
6597 struct tracepoint *tp = (struct tracepoint *) b;
6598
6599 if (tp->traceframe_usage)
6600 {
6601 uiout->text ("\ttrace buffer usage ");
6602 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6603 uiout->text (" bytes\n");
6604 }
6605 }
6606
6607 l = b->commands ? b->commands->commands : NULL;
6608 if (!part_of_multiple && l)
6609 {
6610 annotate_field (9);
6611 ui_out_emit_tuple tuple_emitter (uiout, "script");
6612 print_command_lines (uiout, l, 4);
6613 }
6614
6615 if (is_tracepoint (b))
6616 {
6617 struct tracepoint *t = (struct tracepoint *) b;
6618
6619 if (!part_of_multiple && t->pass_count)
6620 {
6621 annotate_field (10);
6622 uiout->text ("\tpass count ");
6623 uiout->field_int ("pass", t->pass_count);
6624 uiout->text (" \n");
6625 }
6626
6627 /* Don't display it when tracepoint or tracepoint location is
6628 pending. */
6629 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6630 {
6631 annotate_field (11);
6632
6633 if (uiout->is_mi_like_p ())
6634 uiout->field_string ("installed",
6635 loc->inserted ? "y" : "n");
6636 else
6637 {
6638 if (loc->inserted)
6639 uiout->text ("\t");
6640 else
6641 uiout->text ("\tnot ");
6642 uiout->text ("installed on target\n");
6643 }
6644 }
6645 }
6646
6647 if (uiout->is_mi_like_p () && !part_of_multiple)
6648 {
6649 if (is_watchpoint (b))
6650 {
6651 struct watchpoint *w = (struct watchpoint *) b;
6652
6653 uiout->field_string ("original-location", w->exp_string);
6654 }
6655 else if (b->location != NULL
6656 && event_location_to_string (b->location.get ()) != NULL)
6657 uiout->field_string ("original-location",
6658 event_location_to_string (b->location.get ()));
6659 }
6660 }
6661
6662 static void
6663 print_one_breakpoint (struct breakpoint *b,
6664 struct bp_location **last_loc,
6665 int allflag)
6666 {
6667 struct ui_out *uiout = current_uiout;
6668
6669 {
6670 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6671
6672 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6673 }
6674
6675 /* If this breakpoint has custom print function,
6676 it's already printed. Otherwise, print individual
6677 locations, if any. */
6678 if (b->ops == NULL || b->ops->print_one == NULL)
6679 {
6680 /* If breakpoint has a single location that is disabled, we
6681 print it as if it had several locations, since otherwise it's
6682 hard to represent "breakpoint enabled, location disabled"
6683 situation.
6684
6685 Note that while hardware watchpoints have several locations
6686 internally, that's not a property exposed to user. */
6687 if (b->loc
6688 && !is_hardware_watchpoint (b)
6689 && (b->loc->next || !b->loc->enabled))
6690 {
6691 struct bp_location *loc;
6692 int n = 1;
6693
6694 for (loc = b->loc; loc; loc = loc->next, ++n)
6695 {
6696 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6697 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6698 }
6699 }
6700 }
6701 }
6702
6703 static int
6704 breakpoint_address_bits (struct breakpoint *b)
6705 {
6706 int print_address_bits = 0;
6707 struct bp_location *loc;
6708
6709 /* Software watchpoints that aren't watching memory don't have an
6710 address to print. */
6711 if (is_no_memory_software_watchpoint (b))
6712 return 0;
6713
6714 for (loc = b->loc; loc; loc = loc->next)
6715 {
6716 int addr_bit;
6717
6718 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6719 if (addr_bit > print_address_bits)
6720 print_address_bits = addr_bit;
6721 }
6722
6723 return print_address_bits;
6724 }
6725
6726 struct captured_breakpoint_query_args
6727 {
6728 int bnum;
6729 };
6730
6731 static int
6732 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6733 {
6734 struct captured_breakpoint_query_args *args
6735 = (struct captured_breakpoint_query_args *) data;
6736 struct breakpoint *b;
6737 struct bp_location *dummy_loc = NULL;
6738
6739 ALL_BREAKPOINTS (b)
6740 {
6741 if (args->bnum == b->number)
6742 {
6743 print_one_breakpoint (b, &dummy_loc, 0);
6744 return GDB_RC_OK;
6745 }
6746 }
6747 return GDB_RC_NONE;
6748 }
6749
6750 enum gdb_rc
6751 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6752 char **error_message)
6753 {
6754 struct captured_breakpoint_query_args args;
6755
6756 args.bnum = bnum;
6757 /* For the moment we don't trust print_one_breakpoint() to not throw
6758 an error. */
6759 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6760 error_message, RETURN_MASK_ALL) < 0)
6761 return GDB_RC_FAIL;
6762 else
6763 return GDB_RC_OK;
6764 }
6765
6766 /* Return true if this breakpoint was set by the user, false if it is
6767 internal or momentary. */
6768
6769 int
6770 user_breakpoint_p (struct breakpoint *b)
6771 {
6772 return b->number > 0;
6773 }
6774
6775 /* See breakpoint.h. */
6776
6777 int
6778 pending_breakpoint_p (struct breakpoint *b)
6779 {
6780 return b->loc == NULL;
6781 }
6782
6783 /* Print information on user settable breakpoint (watchpoint, etc)
6784 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6785 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6786 FILTER is non-NULL, call it on each breakpoint and only include the
6787 ones for which it returns non-zero. Return the total number of
6788 breakpoints listed. */
6789
6790 static int
6791 breakpoint_1 (char *args, int allflag,
6792 int (*filter) (const struct breakpoint *))
6793 {
6794 struct breakpoint *b;
6795 struct bp_location *last_loc = NULL;
6796 int nr_printable_breakpoints;
6797 struct cleanup *bkpttbl_chain;
6798 struct value_print_options opts;
6799 int print_address_bits = 0;
6800 int print_type_col_width = 14;
6801 struct ui_out *uiout = current_uiout;
6802
6803 get_user_print_options (&opts);
6804
6805 /* Compute the number of rows in the table, as well as the size
6806 required for address fields. */
6807 nr_printable_breakpoints = 0;
6808 ALL_BREAKPOINTS (b)
6809 {
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 if (args != NULL && *args != '\0')
6817 {
6818 if (allflag && parse_and_eval_long (args) != b->number)
6819 continue;
6820 if (!allflag && !number_is_in_list (args, b->number))
6821 continue;
6822 }
6823
6824 if (allflag || user_breakpoint_p (b))
6825 {
6826 int addr_bit, type_len;
6827
6828 addr_bit = breakpoint_address_bits (b);
6829 if (addr_bit > print_address_bits)
6830 print_address_bits = addr_bit;
6831
6832 type_len = strlen (bptype_string (b->type));
6833 if (type_len > print_type_col_width)
6834 print_type_col_width = type_len;
6835
6836 nr_printable_breakpoints++;
6837 }
6838 }
6839
6840 if (opts.addressprint)
6841 bkpttbl_chain
6842 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6843 nr_printable_breakpoints,
6844 "BreakpointTable");
6845 else
6846 bkpttbl_chain
6847 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6848 nr_printable_breakpoints,
6849 "BreakpointTable");
6850
6851 if (nr_printable_breakpoints > 0)
6852 annotate_breakpoints_headers ();
6853 if (nr_printable_breakpoints > 0)
6854 annotate_field (0);
6855 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6856 if (nr_printable_breakpoints > 0)
6857 annotate_field (1);
6858 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6859 if (nr_printable_breakpoints > 0)
6860 annotate_field (2);
6861 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (3);
6864 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6865 if (opts.addressprint)
6866 {
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (4);
6869 if (print_address_bits <= 32)
6870 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6871 else
6872 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6873 }
6874 if (nr_printable_breakpoints > 0)
6875 annotate_field (5);
6876 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6877 uiout->table_body ();
6878 if (nr_printable_breakpoints > 0)
6879 annotate_breakpoints_table ();
6880
6881 ALL_BREAKPOINTS (b)
6882 {
6883 QUIT;
6884 /* If we have a filter, only list the breakpoints it accepts. */
6885 if (filter && !filter (b))
6886 continue;
6887
6888 /* If we have an "args" string, it is a list of breakpoints to
6889 accept. Skip the others. */
6890
6891 if (args != NULL && *args != '\0')
6892 {
6893 if (allflag) /* maintenance info breakpoint */
6894 {
6895 if (parse_and_eval_long (args) != b->number)
6896 continue;
6897 }
6898 else /* all others */
6899 {
6900 if (!number_is_in_list (args, b->number))
6901 continue;
6902 }
6903 }
6904 /* We only print out user settable breakpoints unless the
6905 allflag is set. */
6906 if (allflag || user_breakpoint_p (b))
6907 print_one_breakpoint (b, &last_loc, allflag);
6908 }
6909
6910 do_cleanups (bkpttbl_chain);
6911
6912 if (nr_printable_breakpoints == 0)
6913 {
6914 /* If there's a filter, let the caller decide how to report
6915 empty list. */
6916 if (!filter)
6917 {
6918 if (args == NULL || *args == '\0')
6919 uiout->message ("No breakpoints or watchpoints.\n");
6920 else
6921 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6922 args);
6923 }
6924 }
6925 else
6926 {
6927 if (last_loc && !server_command)
6928 set_next_address (last_loc->gdbarch, last_loc->address);
6929 }
6930
6931 /* FIXME? Should this be moved up so that it is only called when
6932 there have been breakpoints? */
6933 annotate_breakpoints_table_end ();
6934
6935 return nr_printable_breakpoints;
6936 }
6937
6938 /* Display the value of default-collect in a way that is generally
6939 compatible with the breakpoint list. */
6940
6941 static void
6942 default_collect_info (void)
6943 {
6944 struct ui_out *uiout = current_uiout;
6945
6946 /* If it has no value (which is frequently the case), say nothing; a
6947 message like "No default-collect." gets in user's face when it's
6948 not wanted. */
6949 if (!*default_collect)
6950 return;
6951
6952 /* The following phrase lines up nicely with per-tracepoint collect
6953 actions. */
6954 uiout->text ("default collect ");
6955 uiout->field_string ("default-collect", default_collect);
6956 uiout->text (" \n");
6957 }
6958
6959 static void
6960 breakpoints_info (char *args, int from_tty)
6961 {
6962 breakpoint_1 (args, 0, NULL);
6963
6964 default_collect_info ();
6965 }
6966
6967 static void
6968 watchpoints_info (char *args, int from_tty)
6969 {
6970 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6971 struct ui_out *uiout = current_uiout;
6972
6973 if (num_printed == 0)
6974 {
6975 if (args == NULL || *args == '\0')
6976 uiout->message ("No watchpoints.\n");
6977 else
6978 uiout->message ("No watchpoint matching '%s'.\n", args);
6979 }
6980 }
6981
6982 static void
6983 maintenance_info_breakpoints (char *args, int from_tty)
6984 {
6985 breakpoint_1 (args, 1, NULL);
6986
6987 default_collect_info ();
6988 }
6989
6990 static int
6991 breakpoint_has_pc (struct breakpoint *b,
6992 struct program_space *pspace,
6993 CORE_ADDR pc, struct obj_section *section)
6994 {
6995 struct bp_location *bl = b->loc;
6996
6997 for (; bl; bl = bl->next)
6998 {
6999 if (bl->pspace == pspace
7000 && bl->address == pc
7001 && (!overlay_debugging || bl->section == section))
7002 return 1;
7003 }
7004 return 0;
7005 }
7006
7007 /* Print a message describing any user-breakpoints set at PC. This
7008 concerns with logical breakpoints, so we match program spaces, not
7009 address spaces. */
7010
7011 static void
7012 describe_other_breakpoints (struct gdbarch *gdbarch,
7013 struct program_space *pspace, CORE_ADDR pc,
7014 struct obj_section *section, int thread)
7015 {
7016 int others = 0;
7017 struct breakpoint *b;
7018
7019 ALL_BREAKPOINTS (b)
7020 others += (user_breakpoint_p (b)
7021 && breakpoint_has_pc (b, pspace, pc, section));
7022 if (others > 0)
7023 {
7024 if (others == 1)
7025 printf_filtered (_("Note: breakpoint "));
7026 else /* if (others == ???) */
7027 printf_filtered (_("Note: breakpoints "));
7028 ALL_BREAKPOINTS (b)
7029 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7030 {
7031 others--;
7032 printf_filtered ("%d", b->number);
7033 if (b->thread == -1 && thread != -1)
7034 printf_filtered (" (all threads)");
7035 else if (b->thread != -1)
7036 printf_filtered (" (thread %d)", b->thread);
7037 printf_filtered ("%s%s ",
7038 ((b->enable_state == bp_disabled
7039 || b->enable_state == bp_call_disabled)
7040 ? " (disabled)"
7041 : ""),
7042 (others > 1) ? ","
7043 : ((others == 1) ? " and" : ""));
7044 }
7045 printf_filtered (_("also set at pc "));
7046 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7047 printf_filtered (".\n");
7048 }
7049 }
7050 \f
7051
7052 /* Return true iff it is meaningful to use the address member of
7053 BPT locations. For some breakpoint types, the locations' address members
7054 are irrelevant and it makes no sense to attempt to compare them to other
7055 addresses (or use them for any other purpose either).
7056
7057 More specifically, each of the following breakpoint types will
7058 always have a zero valued location address and we don't want to mark
7059 breakpoints of any of these types to be a duplicate of an actual
7060 breakpoint location at address zero:
7061
7062 bp_watchpoint
7063 bp_catchpoint
7064
7065 */
7066
7067 static int
7068 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7069 {
7070 enum bptype type = bpt->type;
7071
7072 return (type != bp_watchpoint && type != bp_catchpoint);
7073 }
7074
7075 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7076 true if LOC1 and LOC2 represent the same watchpoint location. */
7077
7078 static int
7079 watchpoint_locations_match (struct bp_location *loc1,
7080 struct bp_location *loc2)
7081 {
7082 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7083 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7084
7085 /* Both of them must exist. */
7086 gdb_assert (w1 != NULL);
7087 gdb_assert (w2 != NULL);
7088
7089 /* If the target can evaluate the condition expression in hardware,
7090 then we we need to insert both watchpoints even if they are at
7091 the same place. Otherwise the watchpoint will only trigger when
7092 the condition of whichever watchpoint was inserted evaluates to
7093 true, not giving a chance for GDB to check the condition of the
7094 other watchpoint. */
7095 if ((w1->cond_exp
7096 && target_can_accel_watchpoint_condition (loc1->address,
7097 loc1->length,
7098 loc1->watchpoint_type,
7099 w1->cond_exp.get ()))
7100 || (w2->cond_exp
7101 && target_can_accel_watchpoint_condition (loc2->address,
7102 loc2->length,
7103 loc2->watchpoint_type,
7104 w2->cond_exp.get ())))
7105 return 0;
7106
7107 /* Note that this checks the owner's type, not the location's. In
7108 case the target does not support read watchpoints, but does
7109 support access watchpoints, we'll have bp_read_watchpoint
7110 watchpoints with hw_access locations. Those should be considered
7111 duplicates of hw_read locations. The hw_read locations will
7112 become hw_access locations later. */
7113 return (loc1->owner->type == loc2->owner->type
7114 && loc1->pspace->aspace == loc2->pspace->aspace
7115 && loc1->address == loc2->address
7116 && loc1->length == loc2->length);
7117 }
7118
7119 /* See breakpoint.h. */
7120
7121 int
7122 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7123 struct address_space *aspace2, CORE_ADDR addr2)
7124 {
7125 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7126 || aspace1 == aspace2)
7127 && addr1 == addr2);
7128 }
7129
7130 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7131 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7132 matches ASPACE2. On targets that have global breakpoints, the address
7133 space doesn't really matter. */
7134
7135 static int
7136 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7137 int len1, struct address_space *aspace2,
7138 CORE_ADDR addr2)
7139 {
7140 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7141 || aspace1 == aspace2)
7142 && addr2 >= addr1 && addr2 < addr1 + len1);
7143 }
7144
7145 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7146 a ranged breakpoint. In most targets, a match happens only if ASPACE
7147 matches the breakpoint's address space. On targets that have global
7148 breakpoints, the address space doesn't really matter. */
7149
7150 static int
7151 breakpoint_location_address_match (struct bp_location *bl,
7152 struct address_space *aspace,
7153 CORE_ADDR addr)
7154 {
7155 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7156 aspace, addr)
7157 || (bl->length
7158 && breakpoint_address_match_range (bl->pspace->aspace,
7159 bl->address, bl->length,
7160 aspace, addr)));
7161 }
7162
7163 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7164 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7165 match happens only if ASPACE matches the breakpoint's address
7166 space. On targets that have global breakpoints, the address space
7167 doesn't really matter. */
7168
7169 static int
7170 breakpoint_location_address_range_overlap (struct bp_location *bl,
7171 struct address_space *aspace,
7172 CORE_ADDR addr, int len)
7173 {
7174 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7175 || bl->pspace->aspace == aspace)
7176 {
7177 int bl_len = bl->length != 0 ? bl->length : 1;
7178
7179 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7180 return 1;
7181 }
7182 return 0;
7183 }
7184
7185 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7186 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7187 true, otherwise returns false. */
7188
7189 static int
7190 tracepoint_locations_match (struct bp_location *loc1,
7191 struct bp_location *loc2)
7192 {
7193 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7194 /* Since tracepoint locations are never duplicated with others', tracepoint
7195 locations at the same address of different tracepoints are regarded as
7196 different locations. */
7197 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7198 else
7199 return 0;
7200 }
7201
7202 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7203 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7204 represent the same location. */
7205
7206 static int
7207 breakpoint_locations_match (struct bp_location *loc1,
7208 struct bp_location *loc2)
7209 {
7210 int hw_point1, hw_point2;
7211
7212 /* Both of them must not be in moribund_locations. */
7213 gdb_assert (loc1->owner != NULL);
7214 gdb_assert (loc2->owner != NULL);
7215
7216 hw_point1 = is_hardware_watchpoint (loc1->owner);
7217 hw_point2 = is_hardware_watchpoint (loc2->owner);
7218
7219 if (hw_point1 != hw_point2)
7220 return 0;
7221 else if (hw_point1)
7222 return watchpoint_locations_match (loc1, loc2);
7223 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7224 return tracepoint_locations_match (loc1, loc2);
7225 else
7226 /* We compare bp_location.length in order to cover ranged breakpoints. */
7227 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7228 loc2->pspace->aspace, loc2->address)
7229 && loc1->length == loc2->length);
7230 }
7231
7232 static void
7233 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7234 int bnum, int have_bnum)
7235 {
7236 /* The longest string possibly returned by hex_string_custom
7237 is 50 chars. These must be at least that big for safety. */
7238 char astr1[64];
7239 char astr2[64];
7240
7241 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7242 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7243 if (have_bnum)
7244 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7245 bnum, astr1, astr2);
7246 else
7247 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7248 }
7249
7250 /* Adjust a breakpoint's address to account for architectural
7251 constraints on breakpoint placement. Return the adjusted address.
7252 Note: Very few targets require this kind of adjustment. For most
7253 targets, this function is simply the identity function. */
7254
7255 static CORE_ADDR
7256 adjust_breakpoint_address (struct gdbarch *gdbarch,
7257 CORE_ADDR bpaddr, enum bptype bptype)
7258 {
7259 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7260 {
7261 /* Very few targets need any kind of breakpoint adjustment. */
7262 return bpaddr;
7263 }
7264 else if (bptype == bp_watchpoint
7265 || bptype == bp_hardware_watchpoint
7266 || bptype == bp_read_watchpoint
7267 || bptype == bp_access_watchpoint
7268 || bptype == bp_catchpoint)
7269 {
7270 /* Watchpoints and the various bp_catch_* eventpoints should not
7271 have their addresses modified. */
7272 return bpaddr;
7273 }
7274 else if (bptype == bp_single_step)
7275 {
7276 /* Single-step breakpoints should not have their addresses
7277 modified. If there's any architectural constrain that
7278 applies to this address, then it should have already been
7279 taken into account when the breakpoint was created in the
7280 first place. If we didn't do this, stepping through e.g.,
7281 Thumb-2 IT blocks would break. */
7282 return bpaddr;
7283 }
7284 else
7285 {
7286 CORE_ADDR adjusted_bpaddr;
7287
7288 /* Some targets have architectural constraints on the placement
7289 of breakpoint instructions. Obtain the adjusted address. */
7290 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7291
7292 /* An adjusted breakpoint address can significantly alter
7293 a user's expectations. Print a warning if an adjustment
7294 is required. */
7295 if (adjusted_bpaddr != bpaddr)
7296 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7297
7298 return adjusted_bpaddr;
7299 }
7300 }
7301
7302 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7303 {
7304 bp_location *loc = this;
7305
7306 gdb_assert (ops != NULL);
7307
7308 loc->ops = ops;
7309 loc->owner = owner;
7310 loc->cond_bytecode = NULL;
7311 loc->shlib_disabled = 0;
7312 loc->enabled = 1;
7313
7314 switch (owner->type)
7315 {
7316 case bp_breakpoint:
7317 case bp_single_step:
7318 case bp_until:
7319 case bp_finish:
7320 case bp_longjmp:
7321 case bp_longjmp_resume:
7322 case bp_longjmp_call_dummy:
7323 case bp_exception:
7324 case bp_exception_resume:
7325 case bp_step_resume:
7326 case bp_hp_step_resume:
7327 case bp_watchpoint_scope:
7328 case bp_call_dummy:
7329 case bp_std_terminate:
7330 case bp_shlib_event:
7331 case bp_thread_event:
7332 case bp_overlay_event:
7333 case bp_jit_event:
7334 case bp_longjmp_master:
7335 case bp_std_terminate_master:
7336 case bp_exception_master:
7337 case bp_gnu_ifunc_resolver:
7338 case bp_gnu_ifunc_resolver_return:
7339 case bp_dprintf:
7340 loc->loc_type = bp_loc_software_breakpoint;
7341 mark_breakpoint_location_modified (loc);
7342 break;
7343 case bp_hardware_breakpoint:
7344 loc->loc_type = bp_loc_hardware_breakpoint;
7345 mark_breakpoint_location_modified (loc);
7346 break;
7347 case bp_hardware_watchpoint:
7348 case bp_read_watchpoint:
7349 case bp_access_watchpoint:
7350 loc->loc_type = bp_loc_hardware_watchpoint;
7351 break;
7352 case bp_watchpoint:
7353 case bp_catchpoint:
7354 case bp_tracepoint:
7355 case bp_fast_tracepoint:
7356 case bp_static_tracepoint:
7357 loc->loc_type = bp_loc_other;
7358 break;
7359 default:
7360 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7361 }
7362
7363 loc->refc = 1;
7364 }
7365
7366 /* Allocate a struct bp_location. */
7367
7368 static struct bp_location *
7369 allocate_bp_location (struct breakpoint *bpt)
7370 {
7371 return bpt->ops->allocate_location (bpt);
7372 }
7373
7374 static void
7375 free_bp_location (struct bp_location *loc)
7376 {
7377 loc->ops->dtor (loc);
7378 delete loc;
7379 }
7380
7381 /* Increment reference count. */
7382
7383 static void
7384 incref_bp_location (struct bp_location *bl)
7385 {
7386 ++bl->refc;
7387 }
7388
7389 /* Decrement reference count. If the reference count reaches 0,
7390 destroy the bp_location. Sets *BLP to NULL. */
7391
7392 static void
7393 decref_bp_location (struct bp_location **blp)
7394 {
7395 gdb_assert ((*blp)->refc > 0);
7396
7397 if (--(*blp)->refc == 0)
7398 free_bp_location (*blp);
7399 *blp = NULL;
7400 }
7401
7402 /* Add breakpoint B at the end of the global breakpoint chain. */
7403
7404 static void
7405 add_to_breakpoint_chain (struct breakpoint *b)
7406 {
7407 struct breakpoint *b1;
7408
7409 /* Add this breakpoint to the end of the chain so that a list of
7410 breakpoints will come out in order of increasing numbers. */
7411
7412 b1 = breakpoint_chain;
7413 if (b1 == 0)
7414 breakpoint_chain = b;
7415 else
7416 {
7417 while (b1->next)
7418 b1 = b1->next;
7419 b1->next = b;
7420 }
7421 }
7422
7423 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7424
7425 static void
7426 init_raw_breakpoint_without_location (struct breakpoint *b,
7427 struct gdbarch *gdbarch,
7428 enum bptype bptype,
7429 const struct breakpoint_ops *ops)
7430 {
7431 memset (b, 0, sizeof (*b));
7432
7433 gdb_assert (ops != NULL);
7434
7435 b->ops = ops;
7436 b->type = bptype;
7437 b->gdbarch = gdbarch;
7438 b->language = current_language->la_language;
7439 b->input_radix = input_radix;
7440 b->thread = -1;
7441 b->enable_state = bp_enabled;
7442 b->next = 0;
7443 b->silent = 0;
7444 b->ignore_count = 0;
7445 b->commands = NULL;
7446 b->frame_id = null_frame_id;
7447 b->condition_not_parsed = 0;
7448 b->py_bp_object = NULL;
7449 b->related_breakpoint = b;
7450 b->location = NULL;
7451 }
7452
7453 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7454 that has type BPTYPE and has no locations as yet. */
7455
7456 static struct breakpoint *
7457 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7458 enum bptype bptype,
7459 const struct breakpoint_ops *ops)
7460 {
7461 struct breakpoint *b = new breakpoint ();
7462
7463 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7464 add_to_breakpoint_chain (b);
7465 return b;
7466 }
7467
7468 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7469 resolutions should be made as the user specified the location explicitly
7470 enough. */
7471
7472 static void
7473 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7474 {
7475 gdb_assert (loc->owner != NULL);
7476
7477 if (loc->owner->type == bp_breakpoint
7478 || loc->owner->type == bp_hardware_breakpoint
7479 || is_tracepoint (loc->owner))
7480 {
7481 int is_gnu_ifunc;
7482 const char *function_name;
7483 CORE_ADDR func_addr;
7484
7485 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7486 &func_addr, NULL, &is_gnu_ifunc);
7487
7488 if (is_gnu_ifunc && !explicit_loc)
7489 {
7490 struct breakpoint *b = loc->owner;
7491
7492 gdb_assert (loc->pspace == current_program_space);
7493 if (gnu_ifunc_resolve_name (function_name,
7494 &loc->requested_address))
7495 {
7496 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7497 loc->address = adjust_breakpoint_address (loc->gdbarch,
7498 loc->requested_address,
7499 b->type);
7500 }
7501 else if (b->type == bp_breakpoint && b->loc == loc
7502 && loc->next == NULL && b->related_breakpoint == b)
7503 {
7504 /* Create only the whole new breakpoint of this type but do not
7505 mess more complicated breakpoints with multiple locations. */
7506 b->type = bp_gnu_ifunc_resolver;
7507 /* Remember the resolver's address for use by the return
7508 breakpoint. */
7509 loc->related_address = func_addr;
7510 }
7511 }
7512
7513 if (function_name)
7514 loc->function_name = xstrdup (function_name);
7515 }
7516 }
7517
7518 /* Attempt to determine architecture of location identified by SAL. */
7519 struct gdbarch *
7520 get_sal_arch (struct symtab_and_line sal)
7521 {
7522 if (sal.section)
7523 return get_objfile_arch (sal.section->objfile);
7524 if (sal.symtab)
7525 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7526
7527 return NULL;
7528 }
7529
7530 /* Low level routine for partially initializing a breakpoint of type
7531 BPTYPE. The newly created breakpoint's address, section, source
7532 file name, and line number are provided by SAL.
7533
7534 It is expected that the caller will complete the initialization of
7535 the newly created breakpoint struct as well as output any status
7536 information regarding the creation of a new breakpoint. */
7537
7538 static void
7539 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7540 struct symtab_and_line sal, enum bptype bptype,
7541 const struct breakpoint_ops *ops)
7542 {
7543 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7544
7545 add_location_to_breakpoint (b, &sal);
7546
7547 if (bptype != bp_catchpoint)
7548 gdb_assert (sal.pspace != NULL);
7549
7550 /* Store the program space that was used to set the breakpoint,
7551 except for ordinary breakpoints, which are independent of the
7552 program space. */
7553 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7554 b->pspace = sal.pspace;
7555 }
7556
7557 /* set_raw_breakpoint is a low level routine for allocating and
7558 partially initializing a breakpoint of type BPTYPE. The newly
7559 created breakpoint's address, section, source file name, and line
7560 number are provided by SAL. The newly created and partially
7561 initialized breakpoint is added to the breakpoint chain and
7562 is also returned as the value of this function.
7563
7564 It is expected that the caller will complete the initialization of
7565 the newly created breakpoint struct as well as output any status
7566 information regarding the creation of a new breakpoint. In
7567 particular, set_raw_breakpoint does NOT set the breakpoint
7568 number! Care should be taken to not allow an error to occur
7569 prior to completing the initialization of the breakpoint. If this
7570 should happen, a bogus breakpoint will be left on the chain. */
7571
7572 struct breakpoint *
7573 set_raw_breakpoint (struct gdbarch *gdbarch,
7574 struct symtab_and_line sal, enum bptype bptype,
7575 const struct breakpoint_ops *ops)
7576 {
7577 struct breakpoint *b = new breakpoint ();
7578
7579 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7580 add_to_breakpoint_chain (b);
7581 return b;
7582 }
7583
7584 /* Call this routine when stepping and nexting to enable a breakpoint
7585 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7586 initiated the operation. */
7587
7588 void
7589 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7590 {
7591 struct breakpoint *b, *b_tmp;
7592 int thread = tp->global_num;
7593
7594 /* To avoid having to rescan all objfile symbols at every step,
7595 we maintain a list of continually-inserted but always disabled
7596 longjmp "master" breakpoints. Here, we simply create momentary
7597 clones of those and enable them for the requested thread. */
7598 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7599 if (b->pspace == current_program_space
7600 && (b->type == bp_longjmp_master
7601 || b->type == bp_exception_master))
7602 {
7603 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7604 struct breakpoint *clone;
7605
7606 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7607 after their removal. */
7608 clone = momentary_breakpoint_from_master (b, type,
7609 &longjmp_breakpoint_ops, 1);
7610 clone->thread = thread;
7611 }
7612
7613 tp->initiating_frame = frame;
7614 }
7615
7616 /* Delete all longjmp breakpoints from THREAD. */
7617 void
7618 delete_longjmp_breakpoint (int thread)
7619 {
7620 struct breakpoint *b, *b_tmp;
7621
7622 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7623 if (b->type == bp_longjmp || b->type == bp_exception)
7624 {
7625 if (b->thread == thread)
7626 delete_breakpoint (b);
7627 }
7628 }
7629
7630 void
7631 delete_longjmp_breakpoint_at_next_stop (int thread)
7632 {
7633 struct breakpoint *b, *b_tmp;
7634
7635 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7636 if (b->type == bp_longjmp || b->type == bp_exception)
7637 {
7638 if (b->thread == thread)
7639 b->disposition = disp_del_at_next_stop;
7640 }
7641 }
7642
7643 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7644 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7645 pointer to any of them. Return NULL if this system cannot place longjmp
7646 breakpoints. */
7647
7648 struct breakpoint *
7649 set_longjmp_breakpoint_for_call_dummy (void)
7650 {
7651 struct breakpoint *b, *retval = NULL;
7652
7653 ALL_BREAKPOINTS (b)
7654 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7655 {
7656 struct breakpoint *new_b;
7657
7658 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7659 &momentary_breakpoint_ops,
7660 1);
7661 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7662
7663 /* Link NEW_B into the chain of RETVAL breakpoints. */
7664
7665 gdb_assert (new_b->related_breakpoint == new_b);
7666 if (retval == NULL)
7667 retval = new_b;
7668 new_b->related_breakpoint = retval;
7669 while (retval->related_breakpoint != new_b->related_breakpoint)
7670 retval = retval->related_breakpoint;
7671 retval->related_breakpoint = new_b;
7672 }
7673
7674 return retval;
7675 }
7676
7677 /* Verify all existing dummy frames and their associated breakpoints for
7678 TP. Remove those which can no longer be found in the current frame
7679 stack.
7680
7681 You should call this function only at places where it is safe to currently
7682 unwind the whole stack. Failed stack unwind would discard live dummy
7683 frames. */
7684
7685 void
7686 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7687 {
7688 struct breakpoint *b, *b_tmp;
7689
7690 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7691 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7692 {
7693 struct breakpoint *dummy_b = b->related_breakpoint;
7694
7695 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7696 dummy_b = dummy_b->related_breakpoint;
7697 if (dummy_b->type != bp_call_dummy
7698 || frame_find_by_id (dummy_b->frame_id) != NULL)
7699 continue;
7700
7701 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7702
7703 while (b->related_breakpoint != b)
7704 {
7705 if (b_tmp == b->related_breakpoint)
7706 b_tmp = b->related_breakpoint->next;
7707 delete_breakpoint (b->related_breakpoint);
7708 }
7709 delete_breakpoint (b);
7710 }
7711 }
7712
7713 void
7714 enable_overlay_breakpoints (void)
7715 {
7716 struct breakpoint *b;
7717
7718 ALL_BREAKPOINTS (b)
7719 if (b->type == bp_overlay_event)
7720 {
7721 b->enable_state = bp_enabled;
7722 update_global_location_list (UGLL_MAY_INSERT);
7723 overlay_events_enabled = 1;
7724 }
7725 }
7726
7727 void
7728 disable_overlay_breakpoints (void)
7729 {
7730 struct breakpoint *b;
7731
7732 ALL_BREAKPOINTS (b)
7733 if (b->type == bp_overlay_event)
7734 {
7735 b->enable_state = bp_disabled;
7736 update_global_location_list (UGLL_DONT_INSERT);
7737 overlay_events_enabled = 0;
7738 }
7739 }
7740
7741 /* Set an active std::terminate breakpoint for each std::terminate
7742 master breakpoint. */
7743 void
7744 set_std_terminate_breakpoint (void)
7745 {
7746 struct breakpoint *b, *b_tmp;
7747
7748 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7749 if (b->pspace == current_program_space
7750 && b->type == bp_std_terminate_master)
7751 {
7752 momentary_breakpoint_from_master (b, bp_std_terminate,
7753 &momentary_breakpoint_ops, 1);
7754 }
7755 }
7756
7757 /* Delete all the std::terminate breakpoints. */
7758 void
7759 delete_std_terminate_breakpoint (void)
7760 {
7761 struct breakpoint *b, *b_tmp;
7762
7763 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7764 if (b->type == bp_std_terminate)
7765 delete_breakpoint (b);
7766 }
7767
7768 struct breakpoint *
7769 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7770 {
7771 struct breakpoint *b;
7772
7773 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7774 &internal_breakpoint_ops);
7775
7776 b->enable_state = bp_enabled;
7777 /* location has to be used or breakpoint_re_set will delete me. */
7778 b->location = new_address_location (b->loc->address, NULL, 0);
7779
7780 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7781
7782 return b;
7783 }
7784
7785 struct lang_and_radix
7786 {
7787 enum language lang;
7788 int radix;
7789 };
7790
7791 /* Create a breakpoint for JIT code registration and unregistration. */
7792
7793 struct breakpoint *
7794 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7795 {
7796 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7797 &internal_breakpoint_ops);
7798 }
7799
7800 /* Remove JIT code registration and unregistration breakpoint(s). */
7801
7802 void
7803 remove_jit_event_breakpoints (void)
7804 {
7805 struct breakpoint *b, *b_tmp;
7806
7807 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7808 if (b->type == bp_jit_event
7809 && b->loc->pspace == current_program_space)
7810 delete_breakpoint (b);
7811 }
7812
7813 void
7814 remove_solib_event_breakpoints (void)
7815 {
7816 struct breakpoint *b, *b_tmp;
7817
7818 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7819 if (b->type == bp_shlib_event
7820 && b->loc->pspace == current_program_space)
7821 delete_breakpoint (b);
7822 }
7823
7824 /* See breakpoint.h. */
7825
7826 void
7827 remove_solib_event_breakpoints_at_next_stop (void)
7828 {
7829 struct breakpoint *b, *b_tmp;
7830
7831 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7832 if (b->type == bp_shlib_event
7833 && b->loc->pspace == current_program_space)
7834 b->disposition = disp_del_at_next_stop;
7835 }
7836
7837 /* Helper for create_solib_event_breakpoint /
7838 create_and_insert_solib_event_breakpoint. Allows specifying which
7839 INSERT_MODE to pass through to update_global_location_list. */
7840
7841 static struct breakpoint *
7842 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7843 enum ugll_insert_mode insert_mode)
7844 {
7845 struct breakpoint *b;
7846
7847 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7848 &internal_breakpoint_ops);
7849 update_global_location_list_nothrow (insert_mode);
7850 return b;
7851 }
7852
7853 struct breakpoint *
7854 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7855 {
7856 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7857 }
7858
7859 /* See breakpoint.h. */
7860
7861 struct breakpoint *
7862 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7863 {
7864 struct breakpoint *b;
7865
7866 /* Explicitly tell update_global_location_list to insert
7867 locations. */
7868 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7869 if (!b->loc->inserted)
7870 {
7871 delete_breakpoint (b);
7872 return NULL;
7873 }
7874 return b;
7875 }
7876
7877 /* Disable any breakpoints that are on code in shared libraries. Only
7878 apply to enabled breakpoints, disabled ones can just stay disabled. */
7879
7880 void
7881 disable_breakpoints_in_shlibs (void)
7882 {
7883 struct bp_location *loc, **locp_tmp;
7884
7885 ALL_BP_LOCATIONS (loc, locp_tmp)
7886 {
7887 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7888 struct breakpoint *b = loc->owner;
7889
7890 /* We apply the check to all breakpoints, including disabled for
7891 those with loc->duplicate set. This is so that when breakpoint
7892 becomes enabled, or the duplicate is removed, gdb will try to
7893 insert all breakpoints. If we don't set shlib_disabled here,
7894 we'll try to insert those breakpoints and fail. */
7895 if (((b->type == bp_breakpoint)
7896 || (b->type == bp_jit_event)
7897 || (b->type == bp_hardware_breakpoint)
7898 || (is_tracepoint (b)))
7899 && loc->pspace == current_program_space
7900 && !loc->shlib_disabled
7901 && solib_name_from_address (loc->pspace, loc->address)
7902 )
7903 {
7904 loc->shlib_disabled = 1;
7905 }
7906 }
7907 }
7908
7909 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7910 notification of unloaded_shlib. Only apply to enabled breakpoints,
7911 disabled ones can just stay disabled. */
7912
7913 static void
7914 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7915 {
7916 struct bp_location *loc, **locp_tmp;
7917 int disabled_shlib_breaks = 0;
7918
7919 ALL_BP_LOCATIONS (loc, locp_tmp)
7920 {
7921 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7922 struct breakpoint *b = loc->owner;
7923
7924 if (solib->pspace == loc->pspace
7925 && !loc->shlib_disabled
7926 && (((b->type == bp_breakpoint
7927 || b->type == bp_jit_event
7928 || b->type == bp_hardware_breakpoint)
7929 && (loc->loc_type == bp_loc_hardware_breakpoint
7930 || loc->loc_type == bp_loc_software_breakpoint))
7931 || is_tracepoint (b))
7932 && solib_contains_address_p (solib, loc->address))
7933 {
7934 loc->shlib_disabled = 1;
7935 /* At this point, we cannot rely on remove_breakpoint
7936 succeeding so we must mark the breakpoint as not inserted
7937 to prevent future errors occurring in remove_breakpoints. */
7938 loc->inserted = 0;
7939
7940 /* This may cause duplicate notifications for the same breakpoint. */
7941 observer_notify_breakpoint_modified (b);
7942
7943 if (!disabled_shlib_breaks)
7944 {
7945 target_terminal_ours_for_output ();
7946 warning (_("Temporarily disabling breakpoints "
7947 "for unloaded shared library \"%s\""),
7948 solib->so_name);
7949 }
7950 disabled_shlib_breaks = 1;
7951 }
7952 }
7953 }
7954
7955 /* Disable any breakpoints and tracepoints in OBJFILE upon
7956 notification of free_objfile. Only apply to enabled breakpoints,
7957 disabled ones can just stay disabled. */
7958
7959 static void
7960 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7961 {
7962 struct breakpoint *b;
7963
7964 if (objfile == NULL)
7965 return;
7966
7967 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7968 managed by the user with add-symbol-file/remove-symbol-file.
7969 Similarly to how breakpoints in shared libraries are handled in
7970 response to "nosharedlibrary", mark breakpoints in such modules
7971 shlib_disabled so they end up uninserted on the next global
7972 location list update. Shared libraries not loaded by the user
7973 aren't handled here -- they're already handled in
7974 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7975 solib_unloaded observer. We skip objfiles that are not
7976 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7977 main objfile). */
7978 if ((objfile->flags & OBJF_SHARED) == 0
7979 || (objfile->flags & OBJF_USERLOADED) == 0)
7980 return;
7981
7982 ALL_BREAKPOINTS (b)
7983 {
7984 struct bp_location *loc;
7985 int bp_modified = 0;
7986
7987 if (!is_breakpoint (b) && !is_tracepoint (b))
7988 continue;
7989
7990 for (loc = b->loc; loc != NULL; loc = loc->next)
7991 {
7992 CORE_ADDR loc_addr = loc->address;
7993
7994 if (loc->loc_type != bp_loc_hardware_breakpoint
7995 && loc->loc_type != bp_loc_software_breakpoint)
7996 continue;
7997
7998 if (loc->shlib_disabled != 0)
7999 continue;
8000
8001 if (objfile->pspace != loc->pspace)
8002 continue;
8003
8004 if (loc->loc_type != bp_loc_hardware_breakpoint
8005 && loc->loc_type != bp_loc_software_breakpoint)
8006 continue;
8007
8008 if (is_addr_in_objfile (loc_addr, objfile))
8009 {
8010 loc->shlib_disabled = 1;
8011 /* At this point, we don't know whether the object was
8012 unmapped from the inferior or not, so leave the
8013 inserted flag alone. We'll handle failure to
8014 uninsert quietly, in case the object was indeed
8015 unmapped. */
8016
8017 mark_breakpoint_location_modified (loc);
8018
8019 bp_modified = 1;
8020 }
8021 }
8022
8023 if (bp_modified)
8024 observer_notify_breakpoint_modified (b);
8025 }
8026 }
8027
8028 /* FORK & VFORK catchpoints. */
8029
8030 /* An instance of this type is used to represent a fork or vfork
8031 catchpoint. It includes a "struct breakpoint" as a kind of base
8032 class; users downcast to "struct breakpoint *" when needed. A
8033 breakpoint is really of this type iff its ops pointer points to
8034 CATCH_FORK_BREAKPOINT_OPS. */
8035
8036 struct fork_catchpoint
8037 {
8038 /* The base class. */
8039 struct breakpoint base;
8040
8041 /* Process id of a child process whose forking triggered this
8042 catchpoint. This field is only valid immediately after this
8043 catchpoint has triggered. */
8044 ptid_t forked_inferior_pid;
8045 };
8046
8047 /* Implement the "insert" breakpoint_ops method for fork
8048 catchpoints. */
8049
8050 static int
8051 insert_catch_fork (struct bp_location *bl)
8052 {
8053 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8054 }
8055
8056 /* Implement the "remove" breakpoint_ops method for fork
8057 catchpoints. */
8058
8059 static int
8060 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8061 {
8062 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8063 }
8064
8065 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8066 catchpoints. */
8067
8068 static int
8069 breakpoint_hit_catch_fork (const struct bp_location *bl,
8070 struct address_space *aspace, CORE_ADDR bp_addr,
8071 const struct target_waitstatus *ws)
8072 {
8073 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8074
8075 if (ws->kind != TARGET_WAITKIND_FORKED)
8076 return 0;
8077
8078 c->forked_inferior_pid = ws->value.related_pid;
8079 return 1;
8080 }
8081
8082 /* Implement the "print_it" breakpoint_ops method for fork
8083 catchpoints. */
8084
8085 static enum print_stop_action
8086 print_it_catch_fork (bpstat bs)
8087 {
8088 struct ui_out *uiout = current_uiout;
8089 struct breakpoint *b = bs->breakpoint_at;
8090 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8091
8092 annotate_catchpoint (b->number);
8093 maybe_print_thread_hit_breakpoint (uiout);
8094 if (b->disposition == disp_del)
8095 uiout->text ("Temporary catchpoint ");
8096 else
8097 uiout->text ("Catchpoint ");
8098 if (uiout->is_mi_like_p ())
8099 {
8100 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8101 uiout->field_string ("disp", bpdisp_text (b->disposition));
8102 }
8103 uiout->field_int ("bkptno", b->number);
8104 uiout->text (" (forked process ");
8105 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8106 uiout->text ("), ");
8107 return PRINT_SRC_AND_LOC;
8108 }
8109
8110 /* Implement the "print_one" breakpoint_ops method for fork
8111 catchpoints. */
8112
8113 static void
8114 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8115 {
8116 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8117 struct value_print_options opts;
8118 struct ui_out *uiout = current_uiout;
8119
8120 get_user_print_options (&opts);
8121
8122 /* Field 4, the address, is omitted (which makes the columns not
8123 line up too nicely with the headers, but the effect is relatively
8124 readable). */
8125 if (opts.addressprint)
8126 uiout->field_skip ("addr");
8127 annotate_field (5);
8128 uiout->text ("fork");
8129 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8130 {
8131 uiout->text (", process ");
8132 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8133 uiout->spaces (1);
8134 }
8135
8136 if (uiout->is_mi_like_p ())
8137 uiout->field_string ("catch-type", "fork");
8138 }
8139
8140 /* Implement the "print_mention" breakpoint_ops method for fork
8141 catchpoints. */
8142
8143 static void
8144 print_mention_catch_fork (struct breakpoint *b)
8145 {
8146 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8147 }
8148
8149 /* Implement the "print_recreate" breakpoint_ops method for fork
8150 catchpoints. */
8151
8152 static void
8153 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8154 {
8155 fprintf_unfiltered (fp, "catch fork");
8156 print_recreate_thread (b, fp);
8157 }
8158
8159 /* The breakpoint_ops structure to be used in fork catchpoints. */
8160
8161 static struct breakpoint_ops catch_fork_breakpoint_ops;
8162
8163 /* Implement the "insert" breakpoint_ops method for vfork
8164 catchpoints. */
8165
8166 static int
8167 insert_catch_vfork (struct bp_location *bl)
8168 {
8169 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8170 }
8171
8172 /* Implement the "remove" breakpoint_ops method for vfork
8173 catchpoints. */
8174
8175 static int
8176 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8177 {
8178 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8179 }
8180
8181 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8182 catchpoints. */
8183
8184 static int
8185 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8186 struct address_space *aspace, CORE_ADDR bp_addr,
8187 const struct target_waitstatus *ws)
8188 {
8189 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8190
8191 if (ws->kind != TARGET_WAITKIND_VFORKED)
8192 return 0;
8193
8194 c->forked_inferior_pid = ws->value.related_pid;
8195 return 1;
8196 }
8197
8198 /* Implement the "print_it" breakpoint_ops method for vfork
8199 catchpoints. */
8200
8201 static enum print_stop_action
8202 print_it_catch_vfork (bpstat bs)
8203 {
8204 struct ui_out *uiout = current_uiout;
8205 struct breakpoint *b = bs->breakpoint_at;
8206 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8207
8208 annotate_catchpoint (b->number);
8209 maybe_print_thread_hit_breakpoint (uiout);
8210 if (b->disposition == disp_del)
8211 uiout->text ("Temporary catchpoint ");
8212 else
8213 uiout->text ("Catchpoint ");
8214 if (uiout->is_mi_like_p ())
8215 {
8216 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8217 uiout->field_string ("disp", bpdisp_text (b->disposition));
8218 }
8219 uiout->field_int ("bkptno", b->number);
8220 uiout->text (" (vforked process ");
8221 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8222 uiout->text ("), ");
8223 return PRINT_SRC_AND_LOC;
8224 }
8225
8226 /* Implement the "print_one" breakpoint_ops method for vfork
8227 catchpoints. */
8228
8229 static void
8230 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8231 {
8232 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8233 struct value_print_options opts;
8234 struct ui_out *uiout = current_uiout;
8235
8236 get_user_print_options (&opts);
8237 /* Field 4, the address, is omitted (which makes the columns not
8238 line up too nicely with the headers, but the effect is relatively
8239 readable). */
8240 if (opts.addressprint)
8241 uiout->field_skip ("addr");
8242 annotate_field (5);
8243 uiout->text ("vfork");
8244 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8245 {
8246 uiout->text (", process ");
8247 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8248 uiout->spaces (1);
8249 }
8250
8251 if (uiout->is_mi_like_p ())
8252 uiout->field_string ("catch-type", "vfork");
8253 }
8254
8255 /* Implement the "print_mention" breakpoint_ops method for vfork
8256 catchpoints. */
8257
8258 static void
8259 print_mention_catch_vfork (struct breakpoint *b)
8260 {
8261 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8262 }
8263
8264 /* Implement the "print_recreate" breakpoint_ops method for vfork
8265 catchpoints. */
8266
8267 static void
8268 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8269 {
8270 fprintf_unfiltered (fp, "catch vfork");
8271 print_recreate_thread (b, fp);
8272 }
8273
8274 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8275
8276 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8277
8278 /* An instance of this type is used to represent an solib catchpoint.
8279 It includes a "struct breakpoint" as a kind of base class; users
8280 downcast to "struct breakpoint *" when needed. A breakpoint is
8281 really of this type iff its ops pointer points to
8282 CATCH_SOLIB_BREAKPOINT_OPS. */
8283
8284 struct solib_catchpoint
8285 {
8286 /* The base class. */
8287 struct breakpoint base;
8288
8289 /* True for "catch load", false for "catch unload". */
8290 unsigned char is_load;
8291
8292 /* Regular expression to match, if any. COMPILED is only valid when
8293 REGEX is non-NULL. */
8294 char *regex;
8295 regex_t compiled;
8296 };
8297
8298 static void
8299 dtor_catch_solib (struct breakpoint *b)
8300 {
8301 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8302
8303 if (self->regex)
8304 regfree (&self->compiled);
8305 xfree (self->regex);
8306
8307 base_breakpoint_ops.dtor (b);
8308 }
8309
8310 static int
8311 insert_catch_solib (struct bp_location *ignore)
8312 {
8313 return 0;
8314 }
8315
8316 static int
8317 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8318 {
8319 return 0;
8320 }
8321
8322 static int
8323 breakpoint_hit_catch_solib (const struct bp_location *bl,
8324 struct address_space *aspace,
8325 CORE_ADDR bp_addr,
8326 const struct target_waitstatus *ws)
8327 {
8328 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8329 struct breakpoint *other;
8330
8331 if (ws->kind == TARGET_WAITKIND_LOADED)
8332 return 1;
8333
8334 ALL_BREAKPOINTS (other)
8335 {
8336 struct bp_location *other_bl;
8337
8338 if (other == bl->owner)
8339 continue;
8340
8341 if (other->type != bp_shlib_event)
8342 continue;
8343
8344 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8345 continue;
8346
8347 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8348 {
8349 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8350 return 1;
8351 }
8352 }
8353
8354 return 0;
8355 }
8356
8357 static void
8358 check_status_catch_solib (struct bpstats *bs)
8359 {
8360 struct solib_catchpoint *self
8361 = (struct solib_catchpoint *) bs->breakpoint_at;
8362 int ix;
8363
8364 if (self->is_load)
8365 {
8366 struct so_list *iter;
8367
8368 for (ix = 0;
8369 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8370 ix, iter);
8371 ++ix)
8372 {
8373 if (!self->regex
8374 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8375 return;
8376 }
8377 }
8378 else
8379 {
8380 char *iter;
8381
8382 for (ix = 0;
8383 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8384 ix, iter);
8385 ++ix)
8386 {
8387 if (!self->regex
8388 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8389 return;
8390 }
8391 }
8392
8393 bs->stop = 0;
8394 bs->print_it = print_it_noop;
8395 }
8396
8397 static enum print_stop_action
8398 print_it_catch_solib (bpstat bs)
8399 {
8400 struct breakpoint *b = bs->breakpoint_at;
8401 struct ui_out *uiout = current_uiout;
8402
8403 annotate_catchpoint (b->number);
8404 maybe_print_thread_hit_breakpoint (uiout);
8405 if (b->disposition == disp_del)
8406 uiout->text ("Temporary catchpoint ");
8407 else
8408 uiout->text ("Catchpoint ");
8409 uiout->field_int ("bkptno", b->number);
8410 uiout->text ("\n");
8411 if (uiout->is_mi_like_p ())
8412 uiout->field_string ("disp", bpdisp_text (b->disposition));
8413 print_solib_event (1);
8414 return PRINT_SRC_AND_LOC;
8415 }
8416
8417 static void
8418 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8419 {
8420 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8421 struct value_print_options opts;
8422 struct ui_out *uiout = current_uiout;
8423 char *msg;
8424
8425 get_user_print_options (&opts);
8426 /* Field 4, the address, is omitted (which makes the columns not
8427 line up too nicely with the headers, but the effect is relatively
8428 readable). */
8429 if (opts.addressprint)
8430 {
8431 annotate_field (4);
8432 uiout->field_skip ("addr");
8433 }
8434
8435 annotate_field (5);
8436 if (self->is_load)
8437 {
8438 if (self->regex)
8439 msg = xstrprintf (_("load of library matching %s"), self->regex);
8440 else
8441 msg = xstrdup (_("load of library"));
8442 }
8443 else
8444 {
8445 if (self->regex)
8446 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8447 else
8448 msg = xstrdup (_("unload of library"));
8449 }
8450 uiout->field_string ("what", msg);
8451 xfree (msg);
8452
8453 if (uiout->is_mi_like_p ())
8454 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8455 }
8456
8457 static void
8458 print_mention_catch_solib (struct breakpoint *b)
8459 {
8460 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8461
8462 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8463 self->is_load ? "load" : "unload");
8464 }
8465
8466 static void
8467 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8468 {
8469 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8470
8471 fprintf_unfiltered (fp, "%s %s",
8472 b->disposition == disp_del ? "tcatch" : "catch",
8473 self->is_load ? "load" : "unload");
8474 if (self->regex)
8475 fprintf_unfiltered (fp, " %s", self->regex);
8476 fprintf_unfiltered (fp, "\n");
8477 }
8478
8479 static struct breakpoint_ops catch_solib_breakpoint_ops;
8480
8481 /* Shared helper function (MI and CLI) for creating and installing
8482 a shared object event catchpoint. If IS_LOAD is non-zero then
8483 the events to be caught are load events, otherwise they are
8484 unload events. If IS_TEMP is non-zero the catchpoint is a
8485 temporary one. If ENABLED is non-zero the catchpoint is
8486 created in an enabled state. */
8487
8488 void
8489 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8490 {
8491 struct solib_catchpoint *c;
8492 struct gdbarch *gdbarch = get_current_arch ();
8493 struct cleanup *cleanup;
8494
8495 if (!arg)
8496 arg = "";
8497 arg = skip_spaces_const (arg);
8498
8499 c = new solib_catchpoint ();
8500 cleanup = make_cleanup (xfree, c);
8501
8502 if (*arg != '\0')
8503 {
8504 int errcode;
8505
8506 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8507 if (errcode != 0)
8508 {
8509 char *err = get_regcomp_error (errcode, &c->compiled);
8510
8511 make_cleanup (xfree, err);
8512 error (_("Invalid regexp (%s): %s"), err, arg);
8513 }
8514 c->regex = xstrdup (arg);
8515 }
8516
8517 c->is_load = is_load;
8518 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8519 &catch_solib_breakpoint_ops);
8520
8521 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8522
8523 discard_cleanups (cleanup);
8524 install_breakpoint (0, &c->base, 1);
8525 }
8526
8527 /* A helper function that does all the work for "catch load" and
8528 "catch unload". */
8529
8530 static void
8531 catch_load_or_unload (char *arg, int from_tty, int is_load,
8532 struct cmd_list_element *command)
8533 {
8534 int tempflag;
8535 const int enabled = 1;
8536
8537 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8538
8539 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8540 }
8541
8542 static void
8543 catch_load_command_1 (char *arg, int from_tty,
8544 struct cmd_list_element *command)
8545 {
8546 catch_load_or_unload (arg, from_tty, 1, command);
8547 }
8548
8549 static void
8550 catch_unload_command_1 (char *arg, int from_tty,
8551 struct cmd_list_element *command)
8552 {
8553 catch_load_or_unload (arg, from_tty, 0, command);
8554 }
8555
8556 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8557 is non-zero, then make the breakpoint temporary. If COND_STRING is
8558 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8559 the breakpoint_ops structure associated to the catchpoint. */
8560
8561 void
8562 init_catchpoint (struct breakpoint *b,
8563 struct gdbarch *gdbarch, int tempflag,
8564 const char *cond_string,
8565 const struct breakpoint_ops *ops)
8566 {
8567 struct symtab_and_line sal;
8568
8569 init_sal (&sal);
8570 sal.pspace = current_program_space;
8571
8572 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8573
8574 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8575 b->disposition = tempflag ? disp_del : disp_donttouch;
8576 }
8577
8578 void
8579 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8580 {
8581 add_to_breakpoint_chain (b);
8582 set_breakpoint_number (internal, b);
8583 if (is_tracepoint (b))
8584 set_tracepoint_count (breakpoint_count);
8585 if (!internal)
8586 mention (b);
8587 observer_notify_breakpoint_created (b);
8588
8589 if (update_gll)
8590 update_global_location_list (UGLL_MAY_INSERT);
8591 }
8592
8593 static void
8594 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8595 int tempflag, const char *cond_string,
8596 const struct breakpoint_ops *ops)
8597 {
8598 struct fork_catchpoint *c = new fork_catchpoint ();
8599
8600 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8601
8602 c->forked_inferior_pid = null_ptid;
8603
8604 install_breakpoint (0, &c->base, 1);
8605 }
8606
8607 /* Exec catchpoints. */
8608
8609 /* An instance of this type is used to represent an exec catchpoint.
8610 It includes a "struct breakpoint" as a kind of base class; users
8611 downcast to "struct breakpoint *" when needed. A breakpoint is
8612 really of this type iff its ops pointer points to
8613 CATCH_EXEC_BREAKPOINT_OPS. */
8614
8615 struct exec_catchpoint
8616 {
8617 /* The base class. */
8618 struct breakpoint base;
8619
8620 /* Filename of a program whose exec triggered this catchpoint.
8621 This field is only valid immediately after this catchpoint has
8622 triggered. */
8623 char *exec_pathname;
8624 };
8625
8626 /* Implement the "dtor" breakpoint_ops method for exec
8627 catchpoints. */
8628
8629 static void
8630 dtor_catch_exec (struct breakpoint *b)
8631 {
8632 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8633
8634 xfree (c->exec_pathname);
8635
8636 base_breakpoint_ops.dtor (b);
8637 }
8638
8639 static int
8640 insert_catch_exec (struct bp_location *bl)
8641 {
8642 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8643 }
8644
8645 static int
8646 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8647 {
8648 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8649 }
8650
8651 static int
8652 breakpoint_hit_catch_exec (const struct bp_location *bl,
8653 struct address_space *aspace, CORE_ADDR bp_addr,
8654 const struct target_waitstatus *ws)
8655 {
8656 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8657
8658 if (ws->kind != TARGET_WAITKIND_EXECD)
8659 return 0;
8660
8661 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8662 return 1;
8663 }
8664
8665 static enum print_stop_action
8666 print_it_catch_exec (bpstat bs)
8667 {
8668 struct ui_out *uiout = current_uiout;
8669 struct breakpoint *b = bs->breakpoint_at;
8670 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8671
8672 annotate_catchpoint (b->number);
8673 maybe_print_thread_hit_breakpoint (uiout);
8674 if (b->disposition == disp_del)
8675 uiout->text ("Temporary catchpoint ");
8676 else
8677 uiout->text ("Catchpoint ");
8678 if (uiout->is_mi_like_p ())
8679 {
8680 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8681 uiout->field_string ("disp", bpdisp_text (b->disposition));
8682 }
8683 uiout->field_int ("bkptno", b->number);
8684 uiout->text (" (exec'd ");
8685 uiout->field_string ("new-exec", c->exec_pathname);
8686 uiout->text ("), ");
8687
8688 return PRINT_SRC_AND_LOC;
8689 }
8690
8691 static void
8692 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8693 {
8694 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8695 struct value_print_options opts;
8696 struct ui_out *uiout = current_uiout;
8697
8698 get_user_print_options (&opts);
8699
8700 /* Field 4, the address, is omitted (which makes the columns
8701 not line up too nicely with the headers, but the effect
8702 is relatively readable). */
8703 if (opts.addressprint)
8704 uiout->field_skip ("addr");
8705 annotate_field (5);
8706 uiout->text ("exec");
8707 if (c->exec_pathname != NULL)
8708 {
8709 uiout->text (", program \"");
8710 uiout->field_string ("what", c->exec_pathname);
8711 uiout->text ("\" ");
8712 }
8713
8714 if (uiout->is_mi_like_p ())
8715 uiout->field_string ("catch-type", "exec");
8716 }
8717
8718 static void
8719 print_mention_catch_exec (struct breakpoint *b)
8720 {
8721 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8722 }
8723
8724 /* Implement the "print_recreate" breakpoint_ops method for exec
8725 catchpoints. */
8726
8727 static void
8728 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8729 {
8730 fprintf_unfiltered (fp, "catch exec");
8731 print_recreate_thread (b, fp);
8732 }
8733
8734 static struct breakpoint_ops catch_exec_breakpoint_ops;
8735
8736 static int
8737 hw_breakpoint_used_count (void)
8738 {
8739 int i = 0;
8740 struct breakpoint *b;
8741 struct bp_location *bl;
8742
8743 ALL_BREAKPOINTS (b)
8744 {
8745 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8746 for (bl = b->loc; bl; bl = bl->next)
8747 {
8748 /* Special types of hardware breakpoints may use more than
8749 one register. */
8750 i += b->ops->resources_needed (bl);
8751 }
8752 }
8753
8754 return i;
8755 }
8756
8757 /* Returns the resources B would use if it were a hardware
8758 watchpoint. */
8759
8760 static int
8761 hw_watchpoint_use_count (struct breakpoint *b)
8762 {
8763 int i = 0;
8764 struct bp_location *bl;
8765
8766 if (!breakpoint_enabled (b))
8767 return 0;
8768
8769 for (bl = b->loc; bl; bl = bl->next)
8770 {
8771 /* Special types of hardware watchpoints may use more than
8772 one register. */
8773 i += b->ops->resources_needed (bl);
8774 }
8775
8776 return i;
8777 }
8778
8779 /* Returns the sum the used resources of all hardware watchpoints of
8780 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8781 the sum of the used resources of all hardware watchpoints of other
8782 types _not_ TYPE. */
8783
8784 static int
8785 hw_watchpoint_used_count_others (struct breakpoint *except,
8786 enum bptype type, int *other_type_used)
8787 {
8788 int i = 0;
8789 struct breakpoint *b;
8790
8791 *other_type_used = 0;
8792 ALL_BREAKPOINTS (b)
8793 {
8794 if (b == except)
8795 continue;
8796 if (!breakpoint_enabled (b))
8797 continue;
8798
8799 if (b->type == type)
8800 i += hw_watchpoint_use_count (b);
8801 else if (is_hardware_watchpoint (b))
8802 *other_type_used = 1;
8803 }
8804
8805 return i;
8806 }
8807
8808 void
8809 disable_watchpoints_before_interactive_call_start (void)
8810 {
8811 struct breakpoint *b;
8812
8813 ALL_BREAKPOINTS (b)
8814 {
8815 if (is_watchpoint (b) && breakpoint_enabled (b))
8816 {
8817 b->enable_state = bp_call_disabled;
8818 update_global_location_list (UGLL_DONT_INSERT);
8819 }
8820 }
8821 }
8822
8823 void
8824 enable_watchpoints_after_interactive_call_stop (void)
8825 {
8826 struct breakpoint *b;
8827
8828 ALL_BREAKPOINTS (b)
8829 {
8830 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8831 {
8832 b->enable_state = bp_enabled;
8833 update_global_location_list (UGLL_MAY_INSERT);
8834 }
8835 }
8836 }
8837
8838 void
8839 disable_breakpoints_before_startup (void)
8840 {
8841 current_program_space->executing_startup = 1;
8842 update_global_location_list (UGLL_DONT_INSERT);
8843 }
8844
8845 void
8846 enable_breakpoints_after_startup (void)
8847 {
8848 current_program_space->executing_startup = 0;
8849 breakpoint_re_set ();
8850 }
8851
8852 /* Create a new single-step breakpoint for thread THREAD, with no
8853 locations. */
8854
8855 static struct breakpoint *
8856 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8857 {
8858 struct breakpoint *b = new breakpoint ();
8859
8860 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8861 &momentary_breakpoint_ops);
8862
8863 b->disposition = disp_donttouch;
8864 b->frame_id = null_frame_id;
8865
8866 b->thread = thread;
8867 gdb_assert (b->thread != 0);
8868
8869 add_to_breakpoint_chain (b);
8870
8871 return b;
8872 }
8873
8874 /* Set a momentary breakpoint of type TYPE at address specified by
8875 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8876 frame. */
8877
8878 struct breakpoint *
8879 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8880 struct frame_id frame_id, enum bptype type)
8881 {
8882 struct breakpoint *b;
8883
8884 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8885 tail-called one. */
8886 gdb_assert (!frame_id_artificial_p (frame_id));
8887
8888 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8889 b->enable_state = bp_enabled;
8890 b->disposition = disp_donttouch;
8891 b->frame_id = frame_id;
8892
8893 /* If we're debugging a multi-threaded program, then we want
8894 momentary breakpoints to be active in only a single thread of
8895 control. */
8896 if (in_thread_list (inferior_ptid))
8897 b->thread = ptid_to_global_thread_id (inferior_ptid);
8898
8899 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8900
8901 return b;
8902 }
8903
8904 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8905 The new breakpoint will have type TYPE, use OPS as its
8906 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8907
8908 static struct breakpoint *
8909 momentary_breakpoint_from_master (struct breakpoint *orig,
8910 enum bptype type,
8911 const struct breakpoint_ops *ops,
8912 int loc_enabled)
8913 {
8914 struct breakpoint *copy;
8915
8916 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8917 copy->loc = allocate_bp_location (copy);
8918 set_breakpoint_location_function (copy->loc, 1);
8919
8920 copy->loc->gdbarch = orig->loc->gdbarch;
8921 copy->loc->requested_address = orig->loc->requested_address;
8922 copy->loc->address = orig->loc->address;
8923 copy->loc->section = orig->loc->section;
8924 copy->loc->pspace = orig->loc->pspace;
8925 copy->loc->probe = orig->loc->probe;
8926 copy->loc->line_number = orig->loc->line_number;
8927 copy->loc->symtab = orig->loc->symtab;
8928 copy->loc->enabled = loc_enabled;
8929 copy->frame_id = orig->frame_id;
8930 copy->thread = orig->thread;
8931 copy->pspace = orig->pspace;
8932
8933 copy->enable_state = bp_enabled;
8934 copy->disposition = disp_donttouch;
8935 copy->number = internal_breakpoint_number--;
8936
8937 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8938 return copy;
8939 }
8940
8941 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8942 ORIG is NULL. */
8943
8944 struct breakpoint *
8945 clone_momentary_breakpoint (struct breakpoint *orig)
8946 {
8947 /* If there's nothing to clone, then return nothing. */
8948 if (orig == NULL)
8949 return NULL;
8950
8951 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8952 }
8953
8954 struct breakpoint *
8955 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8956 enum bptype type)
8957 {
8958 struct symtab_and_line sal;
8959
8960 sal = find_pc_line (pc, 0);
8961 sal.pc = pc;
8962 sal.section = find_pc_overlay (pc);
8963 sal.explicit_pc = 1;
8964
8965 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8966 }
8967 \f
8968
8969 /* Tell the user we have just set a breakpoint B. */
8970
8971 static void
8972 mention (struct breakpoint *b)
8973 {
8974 b->ops->print_mention (b);
8975 if (current_uiout->is_mi_like_p ())
8976 return;
8977 printf_filtered ("\n");
8978 }
8979 \f
8980
8981 static int bp_loc_is_permanent (struct bp_location *loc);
8982
8983 static struct bp_location *
8984 add_location_to_breakpoint (struct breakpoint *b,
8985 const struct symtab_and_line *sal)
8986 {
8987 struct bp_location *loc, **tmp;
8988 CORE_ADDR adjusted_address;
8989 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8990
8991 if (loc_gdbarch == NULL)
8992 loc_gdbarch = b->gdbarch;
8993
8994 /* Adjust the breakpoint's address prior to allocating a location.
8995 Once we call allocate_bp_location(), that mostly uninitialized
8996 location will be placed on the location chain. Adjustment of the
8997 breakpoint may cause target_read_memory() to be called and we do
8998 not want its scan of the location chain to find a breakpoint and
8999 location that's only been partially initialized. */
9000 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9001 sal->pc, b->type);
9002
9003 /* Sort the locations by their ADDRESS. */
9004 loc = allocate_bp_location (b);
9005 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9006 tmp = &((*tmp)->next))
9007 ;
9008 loc->next = *tmp;
9009 *tmp = loc;
9010
9011 loc->requested_address = sal->pc;
9012 loc->address = adjusted_address;
9013 loc->pspace = sal->pspace;
9014 loc->probe.probe = sal->probe;
9015 loc->probe.objfile = sal->objfile;
9016 gdb_assert (loc->pspace != NULL);
9017 loc->section = sal->section;
9018 loc->gdbarch = loc_gdbarch;
9019 loc->line_number = sal->line;
9020 loc->symtab = sal->symtab;
9021
9022 set_breakpoint_location_function (loc,
9023 sal->explicit_pc || sal->explicit_line);
9024
9025 /* While by definition, permanent breakpoints are already present in the
9026 code, we don't mark the location as inserted. Normally one would expect
9027 that GDB could rely on that breakpoint instruction to stop the program,
9028 thus removing the need to insert its own breakpoint, except that executing
9029 the breakpoint instruction can kill the target instead of reporting a
9030 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9031 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9032 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9033 breakpoint be inserted normally results in QEMU knowing about the GDB
9034 breakpoint, and thus trap before the breakpoint instruction is executed.
9035 (If GDB later needs to continue execution past the permanent breakpoint,
9036 it manually increments the PC, thus avoiding executing the breakpoint
9037 instruction.) */
9038 if (bp_loc_is_permanent (loc))
9039 loc->permanent = 1;
9040
9041 return loc;
9042 }
9043 \f
9044
9045 /* See breakpoint.h. */
9046
9047 int
9048 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9049 {
9050 int len;
9051 CORE_ADDR addr;
9052 const gdb_byte *bpoint;
9053 gdb_byte *target_mem;
9054 struct cleanup *cleanup;
9055 int retval = 0;
9056
9057 addr = address;
9058 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9059
9060 /* Software breakpoints unsupported? */
9061 if (bpoint == NULL)
9062 return 0;
9063
9064 target_mem = (gdb_byte *) alloca (len);
9065
9066 /* Enable the automatic memory restoration from breakpoints while
9067 we read the memory. Otherwise we could say about our temporary
9068 breakpoints they are permanent. */
9069 cleanup = make_show_memory_breakpoints_cleanup (0);
9070
9071 if (target_read_memory (address, target_mem, len) == 0
9072 && memcmp (target_mem, bpoint, len) == 0)
9073 retval = 1;
9074
9075 do_cleanups (cleanup);
9076
9077 return retval;
9078 }
9079
9080 /* Return 1 if LOC is pointing to a permanent breakpoint,
9081 return 0 otherwise. */
9082
9083 static int
9084 bp_loc_is_permanent (struct bp_location *loc)
9085 {
9086 struct cleanup *cleanup;
9087 int retval;
9088
9089 gdb_assert (loc != NULL);
9090
9091 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9092 attempt to read from the addresses the locations of these breakpoint types
9093 point to. program_breakpoint_here_p, below, will attempt to read
9094 memory. */
9095 if (!breakpoint_address_is_meaningful (loc->owner))
9096 return 0;
9097
9098 cleanup = save_current_space_and_thread ();
9099 switch_to_program_space_and_thread (loc->pspace);
9100
9101 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9102
9103 do_cleanups (cleanup);
9104
9105 return retval;
9106 }
9107
9108 /* Build a command list for the dprintf corresponding to the current
9109 settings of the dprintf style options. */
9110
9111 static void
9112 update_dprintf_command_list (struct breakpoint *b)
9113 {
9114 char *dprintf_args = b->extra_string;
9115 char *printf_line = NULL;
9116
9117 if (!dprintf_args)
9118 return;
9119
9120 dprintf_args = skip_spaces (dprintf_args);
9121
9122 /* Allow a comma, as it may have terminated a location, but don't
9123 insist on it. */
9124 if (*dprintf_args == ',')
9125 ++dprintf_args;
9126 dprintf_args = skip_spaces (dprintf_args);
9127
9128 if (*dprintf_args != '"')
9129 error (_("Bad format string, missing '\"'."));
9130
9131 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9132 printf_line = xstrprintf ("printf %s", dprintf_args);
9133 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9134 {
9135 if (!dprintf_function)
9136 error (_("No function supplied for dprintf call"));
9137
9138 if (dprintf_channel && strlen (dprintf_channel) > 0)
9139 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9140 dprintf_function,
9141 dprintf_channel,
9142 dprintf_args);
9143 else
9144 printf_line = xstrprintf ("call (void) %s (%s)",
9145 dprintf_function,
9146 dprintf_args);
9147 }
9148 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9149 {
9150 if (target_can_run_breakpoint_commands ())
9151 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9152 else
9153 {
9154 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9155 printf_line = xstrprintf ("printf %s", dprintf_args);
9156 }
9157 }
9158 else
9159 internal_error (__FILE__, __LINE__,
9160 _("Invalid dprintf style."));
9161
9162 gdb_assert (printf_line != NULL);
9163 /* Manufacture a printf sequence. */
9164 {
9165 struct command_line *printf_cmd_line = XNEW (struct command_line);
9166
9167 printf_cmd_line->control_type = simple_control;
9168 printf_cmd_line->body_count = 0;
9169 printf_cmd_line->body_list = NULL;
9170 printf_cmd_line->next = NULL;
9171 printf_cmd_line->line = printf_line;
9172
9173 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9174 }
9175 }
9176
9177 /* Update all dprintf commands, making their command lists reflect
9178 current style settings. */
9179
9180 static void
9181 update_dprintf_commands (char *args, int from_tty,
9182 struct cmd_list_element *c)
9183 {
9184 struct breakpoint *b;
9185
9186 ALL_BREAKPOINTS (b)
9187 {
9188 if (b->type == bp_dprintf)
9189 update_dprintf_command_list (b);
9190 }
9191 }
9192
9193 /* Create a breakpoint with SAL as location. Use LOCATION
9194 as a description of the location, and COND_STRING
9195 as condition expression. If LOCATION is NULL then create an
9196 "address location" from the address in the SAL. */
9197
9198 static void
9199 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9200 struct symtabs_and_lines sals,
9201 event_location_up &&location,
9202 char *filter, char *cond_string,
9203 char *extra_string,
9204 enum bptype type, enum bpdisp disposition,
9205 int thread, int task, int ignore_count,
9206 const struct breakpoint_ops *ops, int from_tty,
9207 int enabled, int internal, unsigned flags,
9208 int display_canonical)
9209 {
9210 int i;
9211
9212 if (type == bp_hardware_breakpoint)
9213 {
9214 int target_resources_ok;
9215
9216 i = hw_breakpoint_used_count ();
9217 target_resources_ok =
9218 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9219 i + 1, 0);
9220 if (target_resources_ok == 0)
9221 error (_("No hardware breakpoint support in the target."));
9222 else if (target_resources_ok < 0)
9223 error (_("Hardware breakpoints used exceeds limit."));
9224 }
9225
9226 gdb_assert (sals.nelts > 0);
9227
9228 for (i = 0; i < sals.nelts; ++i)
9229 {
9230 struct symtab_and_line sal = sals.sals[i];
9231 struct bp_location *loc;
9232
9233 if (from_tty)
9234 {
9235 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9236 if (!loc_gdbarch)
9237 loc_gdbarch = gdbarch;
9238
9239 describe_other_breakpoints (loc_gdbarch,
9240 sal.pspace, sal.pc, sal.section, thread);
9241 }
9242
9243 if (i == 0)
9244 {
9245 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9246 b->thread = thread;
9247 b->task = task;
9248
9249 b->cond_string = cond_string;
9250 b->extra_string = extra_string;
9251 b->ignore_count = ignore_count;
9252 b->enable_state = enabled ? bp_enabled : bp_disabled;
9253 b->disposition = disposition;
9254
9255 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9256 b->loc->inserted = 1;
9257
9258 if (type == bp_static_tracepoint)
9259 {
9260 struct tracepoint *t = (struct tracepoint *) b;
9261 struct static_tracepoint_marker marker;
9262
9263 if (strace_marker_p (b))
9264 {
9265 /* We already know the marker exists, otherwise, we
9266 wouldn't see a sal for it. */
9267 const char *p
9268 = &event_location_to_string (b->location.get ())[3];
9269 const char *endp;
9270 char *marker_str;
9271
9272 p = skip_spaces_const (p);
9273
9274 endp = skip_to_space_const (p);
9275
9276 marker_str = savestring (p, endp - p);
9277 t->static_trace_marker_id = marker_str;
9278
9279 printf_filtered (_("Probed static tracepoint "
9280 "marker \"%s\"\n"),
9281 t->static_trace_marker_id);
9282 }
9283 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9284 {
9285 t->static_trace_marker_id = xstrdup (marker.str_id);
9286 release_static_tracepoint_marker (&marker);
9287
9288 printf_filtered (_("Probed static tracepoint "
9289 "marker \"%s\"\n"),
9290 t->static_trace_marker_id);
9291 }
9292 else
9293 warning (_("Couldn't determine the static "
9294 "tracepoint marker to probe"));
9295 }
9296
9297 loc = b->loc;
9298 }
9299 else
9300 {
9301 loc = add_location_to_breakpoint (b, &sal);
9302 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9303 loc->inserted = 1;
9304 }
9305
9306 if (b->cond_string)
9307 {
9308 const char *arg = b->cond_string;
9309
9310 loc->cond = parse_exp_1 (&arg, loc->address,
9311 block_for_pc (loc->address), 0);
9312 if (*arg)
9313 error (_("Garbage '%s' follows condition"), arg);
9314 }
9315
9316 /* Dynamic printf requires and uses additional arguments on the
9317 command line, otherwise it's an error. */
9318 if (type == bp_dprintf)
9319 {
9320 if (b->extra_string)
9321 update_dprintf_command_list (b);
9322 else
9323 error (_("Format string required"));
9324 }
9325 else if (b->extra_string)
9326 error (_("Garbage '%s' at end of command"), b->extra_string);
9327 }
9328
9329 b->display_canonical = display_canonical;
9330 if (location != NULL)
9331 b->location = std::move (location);
9332 else
9333 b->location = new_address_location (b->loc->address, NULL, 0);
9334 b->filter = filter;
9335 }
9336
9337 static void
9338 create_breakpoint_sal (struct gdbarch *gdbarch,
9339 struct symtabs_and_lines sals,
9340 event_location_up &&location,
9341 char *filter, char *cond_string,
9342 char *extra_string,
9343 enum bptype type, enum bpdisp disposition,
9344 int thread, int task, int ignore_count,
9345 const struct breakpoint_ops *ops, int from_tty,
9346 int enabled, int internal, unsigned flags,
9347 int display_canonical)
9348 {
9349 struct breakpoint *b;
9350 struct cleanup *old_chain;
9351
9352 if (is_tracepoint_type (type))
9353 {
9354 struct tracepoint *t;
9355
9356 t = new tracepoint ();
9357 b = &t->base;
9358 }
9359 else
9360 b = new breakpoint ();
9361
9362 old_chain = make_cleanup (xfree, b);
9363
9364 init_breakpoint_sal (b, gdbarch,
9365 sals, std::move (location),
9366 filter, cond_string, extra_string,
9367 type, disposition,
9368 thread, task, ignore_count,
9369 ops, from_tty,
9370 enabled, internal, flags,
9371 display_canonical);
9372 discard_cleanups (old_chain);
9373
9374 install_breakpoint (internal, b, 0);
9375 }
9376
9377 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9378 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9379 value. COND_STRING, if not NULL, specified the condition to be
9380 used for all breakpoints. Essentially the only case where
9381 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9382 function. In that case, it's still not possible to specify
9383 separate conditions for different overloaded functions, so
9384 we take just a single condition string.
9385
9386 NOTE: If the function succeeds, the caller is expected to cleanup
9387 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9388 array contents). If the function fails (error() is called), the
9389 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9390 COND and SALS arrays and each of those arrays contents. */
9391
9392 static void
9393 create_breakpoints_sal (struct gdbarch *gdbarch,
9394 struct linespec_result *canonical,
9395 char *cond_string, char *extra_string,
9396 enum bptype type, enum bpdisp disposition,
9397 int thread, int task, int ignore_count,
9398 const struct breakpoint_ops *ops, int from_tty,
9399 int enabled, int internal, unsigned flags)
9400 {
9401 int i;
9402 struct linespec_sals *lsal;
9403
9404 if (canonical->pre_expanded)
9405 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9406
9407 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9408 {
9409 /* Note that 'location' can be NULL in the case of a plain
9410 'break', without arguments. */
9411 event_location_up location
9412 = (canonical->location != NULL
9413 ? copy_event_location (canonical->location.get ()) : NULL);
9414 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9415
9416 make_cleanup (xfree, filter_string);
9417 create_breakpoint_sal (gdbarch, lsal->sals,
9418 std::move (location),
9419 filter_string,
9420 cond_string, extra_string,
9421 type, disposition,
9422 thread, task, ignore_count, ops,
9423 from_tty, enabled, internal, flags,
9424 canonical->special_display);
9425 }
9426 }
9427
9428 /* Parse LOCATION which is assumed to be a SAL specification possibly
9429 followed by conditionals. On return, SALS contains an array of SAL
9430 addresses found. LOCATION points to the end of the SAL (for
9431 linespec locations).
9432
9433 The array and the line spec strings are allocated on the heap, it is
9434 the caller's responsibility to free them. */
9435
9436 static void
9437 parse_breakpoint_sals (const struct event_location *location,
9438 struct linespec_result *canonical)
9439 {
9440 struct symtab_and_line cursal;
9441
9442 if (event_location_type (location) == LINESPEC_LOCATION)
9443 {
9444 const char *address = get_linespec_location (location);
9445
9446 if (address == NULL)
9447 {
9448 /* The last displayed codepoint, if it's valid, is our default
9449 breakpoint address. */
9450 if (last_displayed_sal_is_valid ())
9451 {
9452 struct linespec_sals lsal;
9453 struct symtab_and_line sal;
9454 CORE_ADDR pc;
9455
9456 init_sal (&sal); /* Initialize to zeroes. */
9457 lsal.sals.sals = XNEW (struct symtab_and_line);
9458
9459 /* Set sal's pspace, pc, symtab, and line to the values
9460 corresponding to the last call to print_frame_info.
9461 Be sure to reinitialize LINE with NOTCURRENT == 0
9462 as the breakpoint line number is inappropriate otherwise.
9463 find_pc_line would adjust PC, re-set it back. */
9464 get_last_displayed_sal (&sal);
9465 pc = sal.pc;
9466 sal = find_pc_line (pc, 0);
9467
9468 /* "break" without arguments is equivalent to "break *PC"
9469 where PC is the last displayed codepoint's address. So
9470 make sure to set sal.explicit_pc to prevent GDB from
9471 trying to expand the list of sals to include all other
9472 instances with the same symtab and line. */
9473 sal.pc = pc;
9474 sal.explicit_pc = 1;
9475
9476 lsal.sals.sals[0] = sal;
9477 lsal.sals.nelts = 1;
9478 lsal.canonical = NULL;
9479
9480 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9481 return;
9482 }
9483 else
9484 error (_("No default breakpoint address now."));
9485 }
9486 }
9487
9488 /* Force almost all breakpoints to be in terms of the
9489 current_source_symtab (which is decode_line_1's default).
9490 This should produce the results we want almost all of the
9491 time while leaving default_breakpoint_* alone.
9492
9493 ObjC: However, don't match an Objective-C method name which
9494 may have a '+' or '-' succeeded by a '['. */
9495 cursal = get_current_source_symtab_and_line ();
9496 if (last_displayed_sal_is_valid ())
9497 {
9498 const char *address = NULL;
9499
9500 if (event_location_type (location) == LINESPEC_LOCATION)
9501 address = get_linespec_location (location);
9502
9503 if (!cursal.symtab
9504 || (address != NULL
9505 && strchr ("+-", address[0]) != NULL
9506 && address[1] != '['))
9507 {
9508 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9509 get_last_displayed_symtab (),
9510 get_last_displayed_line (),
9511 canonical, NULL, NULL);
9512 return;
9513 }
9514 }
9515
9516 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9517 cursal.symtab, cursal.line, canonical, NULL, NULL);
9518 }
9519
9520
9521 /* Convert each SAL into a real PC. Verify that the PC can be
9522 inserted as a breakpoint. If it can't throw an error. */
9523
9524 static void
9525 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9526 {
9527 int i;
9528
9529 for (i = 0; i < sals->nelts; i++)
9530 resolve_sal_pc (&sals->sals[i]);
9531 }
9532
9533 /* Fast tracepoints may have restrictions on valid locations. For
9534 instance, a fast tracepoint using a jump instead of a trap will
9535 likely have to overwrite more bytes than a trap would, and so can
9536 only be placed where the instruction is longer than the jump, or a
9537 multi-instruction sequence does not have a jump into the middle of
9538 it, etc. */
9539
9540 static void
9541 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9542 struct symtabs_and_lines *sals)
9543 {
9544 int i, rslt;
9545 struct symtab_and_line *sal;
9546 char *msg;
9547 struct cleanup *old_chain;
9548
9549 for (i = 0; i < sals->nelts; i++)
9550 {
9551 struct gdbarch *sarch;
9552
9553 sal = &sals->sals[i];
9554
9555 sarch = get_sal_arch (*sal);
9556 /* We fall back to GDBARCH if there is no architecture
9557 associated with SAL. */
9558 if (sarch == NULL)
9559 sarch = gdbarch;
9560 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9561 old_chain = make_cleanup (xfree, msg);
9562
9563 if (!rslt)
9564 error (_("May not have a fast tracepoint at %s%s"),
9565 paddress (sarch, sal->pc), (msg ? msg : ""));
9566
9567 do_cleanups (old_chain);
9568 }
9569 }
9570
9571 /* Given TOK, a string specification of condition and thread, as
9572 accepted by the 'break' command, extract the condition
9573 string and thread number and set *COND_STRING and *THREAD.
9574 PC identifies the context at which the condition should be parsed.
9575 If no condition is found, *COND_STRING is set to NULL.
9576 If no thread is found, *THREAD is set to -1. */
9577
9578 static void
9579 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9580 char **cond_string, int *thread, int *task,
9581 char **rest)
9582 {
9583 *cond_string = NULL;
9584 *thread = -1;
9585 *task = 0;
9586 *rest = NULL;
9587
9588 while (tok && *tok)
9589 {
9590 const char *end_tok;
9591 int toklen;
9592 const char *cond_start = NULL;
9593 const char *cond_end = NULL;
9594
9595 tok = skip_spaces_const (tok);
9596
9597 if ((*tok == '"' || *tok == ',') && rest)
9598 {
9599 *rest = savestring (tok, strlen (tok));
9600 return;
9601 }
9602
9603 end_tok = skip_to_space_const (tok);
9604
9605 toklen = end_tok - tok;
9606
9607 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9608 {
9609 tok = cond_start = end_tok + 1;
9610 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9611 cond_end = tok;
9612 *cond_string = savestring (cond_start, cond_end - cond_start);
9613 }
9614 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9615 {
9616 const char *tmptok;
9617 struct thread_info *thr;
9618
9619 tok = end_tok + 1;
9620 thr = parse_thread_id (tok, &tmptok);
9621 if (tok == tmptok)
9622 error (_("Junk after thread keyword."));
9623 *thread = thr->global_num;
9624 tok = tmptok;
9625 }
9626 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9627 {
9628 char *tmptok;
9629
9630 tok = end_tok + 1;
9631 *task = strtol (tok, &tmptok, 0);
9632 if (tok == tmptok)
9633 error (_("Junk after task keyword."));
9634 if (!valid_task_id (*task))
9635 error (_("Unknown task %d."), *task);
9636 tok = tmptok;
9637 }
9638 else if (rest)
9639 {
9640 *rest = savestring (tok, strlen (tok));
9641 return;
9642 }
9643 else
9644 error (_("Junk at end of arguments."));
9645 }
9646 }
9647
9648 /* Decode a static tracepoint marker spec. */
9649
9650 static struct symtabs_and_lines
9651 decode_static_tracepoint_spec (const char **arg_p)
9652 {
9653 VEC(static_tracepoint_marker_p) *markers = NULL;
9654 struct symtabs_and_lines sals;
9655 struct cleanup *old_chain;
9656 const char *p = &(*arg_p)[3];
9657 const char *endp;
9658 char *marker_str;
9659 int i;
9660
9661 p = skip_spaces_const (p);
9662
9663 endp = skip_to_space_const (p);
9664
9665 marker_str = savestring (p, endp - p);
9666 old_chain = make_cleanup (xfree, marker_str);
9667
9668 markers = target_static_tracepoint_markers_by_strid (marker_str);
9669 if (VEC_empty(static_tracepoint_marker_p, markers))
9670 error (_("No known static tracepoint marker named %s"), marker_str);
9671
9672 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9673 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9674
9675 for (i = 0; i < sals.nelts; i++)
9676 {
9677 struct static_tracepoint_marker *marker;
9678
9679 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9680
9681 init_sal (&sals.sals[i]);
9682
9683 sals.sals[i] = find_pc_line (marker->address, 0);
9684 sals.sals[i].pc = marker->address;
9685
9686 release_static_tracepoint_marker (marker);
9687 }
9688
9689 do_cleanups (old_chain);
9690
9691 *arg_p = endp;
9692 return sals;
9693 }
9694
9695 /* See breakpoint.h. */
9696
9697 int
9698 create_breakpoint (struct gdbarch *gdbarch,
9699 const struct event_location *location, char *cond_string,
9700 int thread, char *extra_string,
9701 int parse_extra,
9702 int tempflag, enum bptype type_wanted,
9703 int ignore_count,
9704 enum auto_boolean pending_break_support,
9705 const struct breakpoint_ops *ops,
9706 int from_tty, int enabled, int internal,
9707 unsigned flags)
9708 {
9709 struct linespec_result canonical;
9710 struct cleanup *bkpt_chain = NULL;
9711 int pending = 0;
9712 int task = 0;
9713 int prev_bkpt_count = breakpoint_count;
9714
9715 gdb_assert (ops != NULL);
9716
9717 /* If extra_string isn't useful, set it to NULL. */
9718 if (extra_string != NULL && *extra_string == '\0')
9719 extra_string = NULL;
9720
9721 TRY
9722 {
9723 ops->create_sals_from_location (location, &canonical, type_wanted);
9724 }
9725 CATCH (e, RETURN_MASK_ERROR)
9726 {
9727 /* If caller is interested in rc value from parse, set
9728 value. */
9729 if (e.error == NOT_FOUND_ERROR)
9730 {
9731 /* If pending breakpoint support is turned off, throw
9732 error. */
9733
9734 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9735 throw_exception (e);
9736
9737 exception_print (gdb_stderr, e);
9738
9739 /* If pending breakpoint support is auto query and the user
9740 selects no, then simply return the error code. */
9741 if (pending_break_support == AUTO_BOOLEAN_AUTO
9742 && !nquery (_("Make %s pending on future shared library load? "),
9743 bptype_string (type_wanted)))
9744 return 0;
9745
9746 /* At this point, either the user was queried about setting
9747 a pending breakpoint and selected yes, or pending
9748 breakpoint behavior is on and thus a pending breakpoint
9749 is defaulted on behalf of the user. */
9750 pending = 1;
9751 }
9752 else
9753 throw_exception (e);
9754 }
9755 END_CATCH
9756
9757 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9758 return 0;
9759
9760 /* ----------------------------- SNIP -----------------------------
9761 Anything added to the cleanup chain beyond this point is assumed
9762 to be part of a breakpoint. If the breakpoint create succeeds
9763 then the memory is not reclaimed. */
9764 bkpt_chain = make_cleanup (null_cleanup, 0);
9765
9766 /* Resolve all line numbers to PC's and verify that the addresses
9767 are ok for the target. */
9768 if (!pending)
9769 {
9770 int ix;
9771 struct linespec_sals *iter;
9772
9773 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9774 breakpoint_sals_to_pc (&iter->sals);
9775 }
9776
9777 /* Fast tracepoints may have additional restrictions on location. */
9778 if (!pending && type_wanted == bp_fast_tracepoint)
9779 {
9780 int ix;
9781 struct linespec_sals *iter;
9782
9783 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9784 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9785 }
9786
9787 /* Verify that condition can be parsed, before setting any
9788 breakpoints. Allocate a separate condition expression for each
9789 breakpoint. */
9790 if (!pending)
9791 {
9792 if (parse_extra)
9793 {
9794 char *rest;
9795 struct linespec_sals *lsal;
9796
9797 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9798
9799 /* Here we only parse 'arg' to separate condition
9800 from thread number, so parsing in context of first
9801 sal is OK. When setting the breakpoint we'll
9802 re-parse it in context of each sal. */
9803
9804 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9805 &cond_string, &thread, &task, &rest);
9806 if (cond_string)
9807 make_cleanup (xfree, cond_string);
9808 if (rest)
9809 make_cleanup (xfree, rest);
9810 if (rest)
9811 extra_string = rest;
9812 else
9813 extra_string = NULL;
9814 }
9815 else
9816 {
9817 if (type_wanted != bp_dprintf
9818 && extra_string != NULL && *extra_string != '\0')
9819 error (_("Garbage '%s' at end of location"), extra_string);
9820
9821 /* Create a private copy of condition string. */
9822 if (cond_string)
9823 {
9824 cond_string = xstrdup (cond_string);
9825 make_cleanup (xfree, cond_string);
9826 }
9827 /* Create a private copy of any extra string. */
9828 if (extra_string)
9829 {
9830 extra_string = xstrdup (extra_string);
9831 make_cleanup (xfree, extra_string);
9832 }
9833 }
9834
9835 ops->create_breakpoints_sal (gdbarch, &canonical,
9836 cond_string, extra_string, type_wanted,
9837 tempflag ? disp_del : disp_donttouch,
9838 thread, task, ignore_count, ops,
9839 from_tty, enabled, internal, flags);
9840 }
9841 else
9842 {
9843 struct breakpoint *b;
9844
9845 if (is_tracepoint_type (type_wanted))
9846 {
9847 struct tracepoint *t;
9848
9849 t = new tracepoint ();
9850 b = &t->base;
9851 }
9852 else
9853 b = new breakpoint ();
9854
9855 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9856 b->location = copy_event_location (location);
9857
9858 if (parse_extra)
9859 b->cond_string = NULL;
9860 else
9861 {
9862 /* Create a private copy of condition string. */
9863 if (cond_string)
9864 {
9865 cond_string = xstrdup (cond_string);
9866 make_cleanup (xfree, cond_string);
9867 }
9868 b->cond_string = cond_string;
9869 b->thread = thread;
9870 }
9871
9872 /* Create a private copy of any extra string. */
9873 if (extra_string != NULL)
9874 {
9875 extra_string = xstrdup (extra_string);
9876 make_cleanup (xfree, extra_string);
9877 }
9878 b->extra_string = extra_string;
9879 b->ignore_count = ignore_count;
9880 b->disposition = tempflag ? disp_del : disp_donttouch;
9881 b->condition_not_parsed = 1;
9882 b->enable_state = enabled ? bp_enabled : bp_disabled;
9883 if ((type_wanted != bp_breakpoint
9884 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9885 b->pspace = current_program_space;
9886
9887 install_breakpoint (internal, b, 0);
9888 }
9889
9890 if (VEC_length (linespec_sals, canonical.sals) > 1)
9891 {
9892 warning (_("Multiple breakpoints were set.\nUse the "
9893 "\"delete\" command to delete unwanted breakpoints."));
9894 prev_breakpoint_count = prev_bkpt_count;
9895 }
9896
9897 /* That's it. Discard the cleanups for data inserted into the
9898 breakpoint. */
9899 discard_cleanups (bkpt_chain);
9900
9901 /* error call may happen here - have BKPT_CHAIN already discarded. */
9902 update_global_location_list (UGLL_MAY_INSERT);
9903
9904 return 1;
9905 }
9906
9907 /* Set a breakpoint.
9908 ARG is a string describing breakpoint address,
9909 condition, and thread.
9910 FLAG specifies if a breakpoint is hardware on,
9911 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9912 and BP_TEMPFLAG. */
9913
9914 static void
9915 break_command_1 (char *arg, int flag, int from_tty)
9916 {
9917 int tempflag = flag & BP_TEMPFLAG;
9918 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9919 ? bp_hardware_breakpoint
9920 : bp_breakpoint);
9921 struct breakpoint_ops *ops;
9922
9923 event_location_up location = string_to_event_location (&arg, current_language);
9924
9925 /* Matching breakpoints on probes. */
9926 if (location != NULL
9927 && event_location_type (location.get ()) == PROBE_LOCATION)
9928 ops = &bkpt_probe_breakpoint_ops;
9929 else
9930 ops = &bkpt_breakpoint_ops;
9931
9932 create_breakpoint (get_current_arch (),
9933 location.get (),
9934 NULL, 0, arg, 1 /* parse arg */,
9935 tempflag, type_wanted,
9936 0 /* Ignore count */,
9937 pending_break_support,
9938 ops,
9939 from_tty,
9940 1 /* enabled */,
9941 0 /* internal */,
9942 0);
9943 }
9944
9945 /* Helper function for break_command_1 and disassemble_command. */
9946
9947 void
9948 resolve_sal_pc (struct symtab_and_line *sal)
9949 {
9950 CORE_ADDR pc;
9951
9952 if (sal->pc == 0 && sal->symtab != NULL)
9953 {
9954 if (!find_line_pc (sal->symtab, sal->line, &pc))
9955 error (_("No line %d in file \"%s\"."),
9956 sal->line, symtab_to_filename_for_display (sal->symtab));
9957 sal->pc = pc;
9958
9959 /* If this SAL corresponds to a breakpoint inserted using a line
9960 number, then skip the function prologue if necessary. */
9961 if (sal->explicit_line)
9962 skip_prologue_sal (sal);
9963 }
9964
9965 if (sal->section == 0 && sal->symtab != NULL)
9966 {
9967 const struct blockvector *bv;
9968 const struct block *b;
9969 struct symbol *sym;
9970
9971 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9972 SYMTAB_COMPUNIT (sal->symtab));
9973 if (bv != NULL)
9974 {
9975 sym = block_linkage_function (b);
9976 if (sym != NULL)
9977 {
9978 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9979 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9980 sym);
9981 }
9982 else
9983 {
9984 /* It really is worthwhile to have the section, so we'll
9985 just have to look harder. This case can be executed
9986 if we have line numbers but no functions (as can
9987 happen in assembly source). */
9988
9989 struct bound_minimal_symbol msym;
9990 struct cleanup *old_chain = save_current_space_and_thread ();
9991
9992 switch_to_program_space_and_thread (sal->pspace);
9993
9994 msym = lookup_minimal_symbol_by_pc (sal->pc);
9995 if (msym.minsym)
9996 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9997
9998 do_cleanups (old_chain);
9999 }
10000 }
10001 }
10002 }
10003
10004 void
10005 break_command (char *arg, int from_tty)
10006 {
10007 break_command_1 (arg, 0, from_tty);
10008 }
10009
10010 void
10011 tbreak_command (char *arg, int from_tty)
10012 {
10013 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10014 }
10015
10016 static void
10017 hbreak_command (char *arg, int from_tty)
10018 {
10019 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10020 }
10021
10022 static void
10023 thbreak_command (char *arg, int from_tty)
10024 {
10025 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10026 }
10027
10028 static void
10029 stop_command (char *arg, int from_tty)
10030 {
10031 printf_filtered (_("Specify the type of breakpoint to set.\n\
10032 Usage: stop in <function | address>\n\
10033 stop at <line>\n"));
10034 }
10035
10036 static void
10037 stopin_command (char *arg, int from_tty)
10038 {
10039 int badInput = 0;
10040
10041 if (arg == (char *) NULL)
10042 badInput = 1;
10043 else if (*arg != '*')
10044 {
10045 char *argptr = arg;
10046 int hasColon = 0;
10047
10048 /* Look for a ':'. If this is a line number specification, then
10049 say it is bad, otherwise, it should be an address or
10050 function/method name. */
10051 while (*argptr && !hasColon)
10052 {
10053 hasColon = (*argptr == ':');
10054 argptr++;
10055 }
10056
10057 if (hasColon)
10058 badInput = (*argptr != ':'); /* Not a class::method */
10059 else
10060 badInput = isdigit (*arg); /* a simple line number */
10061 }
10062
10063 if (badInput)
10064 printf_filtered (_("Usage: stop in <function | address>\n"));
10065 else
10066 break_command_1 (arg, 0, from_tty);
10067 }
10068
10069 static void
10070 stopat_command (char *arg, int from_tty)
10071 {
10072 int badInput = 0;
10073
10074 if (arg == (char *) NULL || *arg == '*') /* no line number */
10075 badInput = 1;
10076 else
10077 {
10078 char *argptr = arg;
10079 int hasColon = 0;
10080
10081 /* Look for a ':'. If there is a '::' then get out, otherwise
10082 it is probably a line number. */
10083 while (*argptr && !hasColon)
10084 {
10085 hasColon = (*argptr == ':');
10086 argptr++;
10087 }
10088
10089 if (hasColon)
10090 badInput = (*argptr == ':'); /* we have class::method */
10091 else
10092 badInput = !isdigit (*arg); /* not a line number */
10093 }
10094
10095 if (badInput)
10096 printf_filtered (_("Usage: stop at <line>\n"));
10097 else
10098 break_command_1 (arg, 0, from_tty);
10099 }
10100
10101 /* The dynamic printf command is mostly like a regular breakpoint, but
10102 with a prewired command list consisting of a single output command,
10103 built from extra arguments supplied on the dprintf command
10104 line. */
10105
10106 static void
10107 dprintf_command (char *arg, int from_tty)
10108 {
10109 event_location_up location = string_to_event_location (&arg, current_language);
10110
10111 /* If non-NULL, ARG should have been advanced past the location;
10112 the next character must be ','. */
10113 if (arg != NULL)
10114 {
10115 if (arg[0] != ',' || arg[1] == '\0')
10116 error (_("Format string required"));
10117 else
10118 {
10119 /* Skip the comma. */
10120 ++arg;
10121 }
10122 }
10123
10124 create_breakpoint (get_current_arch (),
10125 location.get (),
10126 NULL, 0, arg, 1 /* parse arg */,
10127 0, bp_dprintf,
10128 0 /* Ignore count */,
10129 pending_break_support,
10130 &dprintf_breakpoint_ops,
10131 from_tty,
10132 1 /* enabled */,
10133 0 /* internal */,
10134 0);
10135 }
10136
10137 static void
10138 agent_printf_command (char *arg, int from_tty)
10139 {
10140 error (_("May only run agent-printf on the target"));
10141 }
10142
10143 /* Implement the "breakpoint_hit" breakpoint_ops method for
10144 ranged breakpoints. */
10145
10146 static int
10147 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10148 struct address_space *aspace,
10149 CORE_ADDR bp_addr,
10150 const struct target_waitstatus *ws)
10151 {
10152 if (ws->kind != TARGET_WAITKIND_STOPPED
10153 || ws->value.sig != GDB_SIGNAL_TRAP)
10154 return 0;
10155
10156 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10157 bl->length, aspace, bp_addr);
10158 }
10159
10160 /* Implement the "resources_needed" breakpoint_ops method for
10161 ranged breakpoints. */
10162
10163 static int
10164 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10165 {
10166 return target_ranged_break_num_registers ();
10167 }
10168
10169 /* Implement the "print_it" breakpoint_ops method for
10170 ranged breakpoints. */
10171
10172 static enum print_stop_action
10173 print_it_ranged_breakpoint (bpstat bs)
10174 {
10175 struct breakpoint *b = bs->breakpoint_at;
10176 struct bp_location *bl = b->loc;
10177 struct ui_out *uiout = current_uiout;
10178
10179 gdb_assert (b->type == bp_hardware_breakpoint);
10180
10181 /* Ranged breakpoints have only one location. */
10182 gdb_assert (bl && bl->next == NULL);
10183
10184 annotate_breakpoint (b->number);
10185
10186 maybe_print_thread_hit_breakpoint (uiout);
10187
10188 if (b->disposition == disp_del)
10189 uiout->text ("Temporary ranged breakpoint ");
10190 else
10191 uiout->text ("Ranged breakpoint ");
10192 if (uiout->is_mi_like_p ())
10193 {
10194 uiout->field_string ("reason",
10195 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10196 uiout->field_string ("disp", bpdisp_text (b->disposition));
10197 }
10198 uiout->field_int ("bkptno", b->number);
10199 uiout->text (", ");
10200
10201 return PRINT_SRC_AND_LOC;
10202 }
10203
10204 /* Implement the "print_one" breakpoint_ops method for
10205 ranged breakpoints. */
10206
10207 static void
10208 print_one_ranged_breakpoint (struct breakpoint *b,
10209 struct bp_location **last_loc)
10210 {
10211 struct bp_location *bl = b->loc;
10212 struct value_print_options opts;
10213 struct ui_out *uiout = current_uiout;
10214
10215 /* Ranged breakpoints have only one location. */
10216 gdb_assert (bl && bl->next == NULL);
10217
10218 get_user_print_options (&opts);
10219
10220 if (opts.addressprint)
10221 /* We don't print the address range here, it will be printed later
10222 by print_one_detail_ranged_breakpoint. */
10223 uiout->field_skip ("addr");
10224 annotate_field (5);
10225 print_breakpoint_location (b, bl);
10226 *last_loc = bl;
10227 }
10228
10229 /* Implement the "print_one_detail" breakpoint_ops method for
10230 ranged breakpoints. */
10231
10232 static void
10233 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10234 struct ui_out *uiout)
10235 {
10236 CORE_ADDR address_start, address_end;
10237 struct bp_location *bl = b->loc;
10238 string_file stb;
10239
10240 gdb_assert (bl);
10241
10242 address_start = bl->address;
10243 address_end = address_start + bl->length - 1;
10244
10245 uiout->text ("\taddress range: ");
10246 stb.printf ("[%s, %s]",
10247 print_core_address (bl->gdbarch, address_start),
10248 print_core_address (bl->gdbarch, address_end));
10249 uiout->field_stream ("addr", stb);
10250 uiout->text ("\n");
10251 }
10252
10253 /* Implement the "print_mention" breakpoint_ops method for
10254 ranged breakpoints. */
10255
10256 static void
10257 print_mention_ranged_breakpoint (struct breakpoint *b)
10258 {
10259 struct bp_location *bl = b->loc;
10260 struct ui_out *uiout = current_uiout;
10261
10262 gdb_assert (bl);
10263 gdb_assert (b->type == bp_hardware_breakpoint);
10264
10265 if (uiout->is_mi_like_p ())
10266 return;
10267
10268 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10269 b->number, paddress (bl->gdbarch, bl->address),
10270 paddress (bl->gdbarch, bl->address + bl->length - 1));
10271 }
10272
10273 /* Implement the "print_recreate" breakpoint_ops method for
10274 ranged breakpoints. */
10275
10276 static void
10277 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10278 {
10279 fprintf_unfiltered (fp, "break-range %s, %s",
10280 event_location_to_string (b->location.get ()),
10281 event_location_to_string (b->location_range_end.get ()));
10282 print_recreate_thread (b, fp);
10283 }
10284
10285 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10286
10287 static struct breakpoint_ops ranged_breakpoint_ops;
10288
10289 /* Find the address where the end of the breakpoint range should be
10290 placed, given the SAL of the end of the range. This is so that if
10291 the user provides a line number, the end of the range is set to the
10292 last instruction of the given line. */
10293
10294 static CORE_ADDR
10295 find_breakpoint_range_end (struct symtab_and_line sal)
10296 {
10297 CORE_ADDR end;
10298
10299 /* If the user provided a PC value, use it. Otherwise,
10300 find the address of the end of the given location. */
10301 if (sal.explicit_pc)
10302 end = sal.pc;
10303 else
10304 {
10305 int ret;
10306 CORE_ADDR start;
10307
10308 ret = find_line_pc_range (sal, &start, &end);
10309 if (!ret)
10310 error (_("Could not find location of the end of the range."));
10311
10312 /* find_line_pc_range returns the start of the next line. */
10313 end--;
10314 }
10315
10316 return end;
10317 }
10318
10319 /* Implement the "break-range" CLI command. */
10320
10321 static void
10322 break_range_command (char *arg, int from_tty)
10323 {
10324 char *arg_start, *addr_string_start;
10325 struct linespec_result canonical_start, canonical_end;
10326 int bp_count, can_use_bp, length;
10327 CORE_ADDR end;
10328 struct breakpoint *b;
10329 struct symtab_and_line sal_start, sal_end;
10330 struct cleanup *cleanup_bkpt;
10331 struct linespec_sals *lsal_start, *lsal_end;
10332
10333 /* We don't support software ranged breakpoints. */
10334 if (target_ranged_break_num_registers () < 0)
10335 error (_("This target does not support hardware ranged breakpoints."));
10336
10337 bp_count = hw_breakpoint_used_count ();
10338 bp_count += target_ranged_break_num_registers ();
10339 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10340 bp_count, 0);
10341 if (can_use_bp < 0)
10342 error (_("Hardware breakpoints used exceeds limit."));
10343
10344 arg = skip_spaces (arg);
10345 if (arg == NULL || arg[0] == '\0')
10346 error(_("No address range specified."));
10347
10348 arg_start = arg;
10349 event_location_up start_location = string_to_event_location (&arg,
10350 current_language);
10351 parse_breakpoint_sals (start_location.get (), &canonical_start);
10352
10353 if (arg[0] != ',')
10354 error (_("Too few arguments."));
10355 else if (VEC_empty (linespec_sals, canonical_start.sals))
10356 error (_("Could not find location of the beginning of the range."));
10357
10358 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10359
10360 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10361 || lsal_start->sals.nelts != 1)
10362 error (_("Cannot create a ranged breakpoint with multiple locations."));
10363
10364 sal_start = lsal_start->sals.sals[0];
10365 addr_string_start = savestring (arg_start, arg - arg_start);
10366 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10367
10368 arg++; /* Skip the comma. */
10369 arg = skip_spaces (arg);
10370
10371 /* Parse the end location. */
10372
10373 arg_start = arg;
10374
10375 /* We call decode_line_full directly here instead of using
10376 parse_breakpoint_sals because we need to specify the start location's
10377 symtab and line as the default symtab and line for the end of the
10378 range. This makes it possible to have ranges like "foo.c:27, +14",
10379 where +14 means 14 lines from the start location. */
10380 event_location_up end_location = string_to_event_location (&arg,
10381 current_language);
10382 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10383 sal_start.symtab, sal_start.line,
10384 &canonical_end, NULL, NULL);
10385
10386 if (VEC_empty (linespec_sals, canonical_end.sals))
10387 error (_("Could not find location of the end of the range."));
10388
10389 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10390 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10391 || lsal_end->sals.nelts != 1)
10392 error (_("Cannot create a ranged breakpoint with multiple locations."));
10393
10394 sal_end = lsal_end->sals.sals[0];
10395
10396 end = find_breakpoint_range_end (sal_end);
10397 if (sal_start.pc > end)
10398 error (_("Invalid address range, end precedes start."));
10399
10400 length = end - sal_start.pc + 1;
10401 if (length < 0)
10402 /* Length overflowed. */
10403 error (_("Address range too large."));
10404 else if (length == 1)
10405 {
10406 /* This range is simple enough to be handled by
10407 the `hbreak' command. */
10408 hbreak_command (addr_string_start, 1);
10409
10410 do_cleanups (cleanup_bkpt);
10411
10412 return;
10413 }
10414
10415 /* Now set up the breakpoint. */
10416 b = set_raw_breakpoint (get_current_arch (), sal_start,
10417 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10418 set_breakpoint_count (breakpoint_count + 1);
10419 b->number = breakpoint_count;
10420 b->disposition = disp_donttouch;
10421 b->location = std::move (start_location);
10422 b->location_range_end = std::move (end_location);
10423 b->loc->length = length;
10424
10425 do_cleanups (cleanup_bkpt);
10426
10427 mention (b);
10428 observer_notify_breakpoint_created (b);
10429 update_global_location_list (UGLL_MAY_INSERT);
10430 }
10431
10432 /* Return non-zero if EXP is verified as constant. Returned zero
10433 means EXP is variable. Also the constant detection may fail for
10434 some constant expressions and in such case still falsely return
10435 zero. */
10436
10437 static int
10438 watchpoint_exp_is_const (const struct expression *exp)
10439 {
10440 int i = exp->nelts;
10441
10442 while (i > 0)
10443 {
10444 int oplenp, argsp;
10445
10446 /* We are only interested in the descriptor of each element. */
10447 operator_length (exp, i, &oplenp, &argsp);
10448 i -= oplenp;
10449
10450 switch (exp->elts[i].opcode)
10451 {
10452 case BINOP_ADD:
10453 case BINOP_SUB:
10454 case BINOP_MUL:
10455 case BINOP_DIV:
10456 case BINOP_REM:
10457 case BINOP_MOD:
10458 case BINOP_LSH:
10459 case BINOP_RSH:
10460 case BINOP_LOGICAL_AND:
10461 case BINOP_LOGICAL_OR:
10462 case BINOP_BITWISE_AND:
10463 case BINOP_BITWISE_IOR:
10464 case BINOP_BITWISE_XOR:
10465 case BINOP_EQUAL:
10466 case BINOP_NOTEQUAL:
10467 case BINOP_LESS:
10468 case BINOP_GTR:
10469 case BINOP_LEQ:
10470 case BINOP_GEQ:
10471 case BINOP_REPEAT:
10472 case BINOP_COMMA:
10473 case BINOP_EXP:
10474 case BINOP_MIN:
10475 case BINOP_MAX:
10476 case BINOP_INTDIV:
10477 case BINOP_CONCAT:
10478 case TERNOP_COND:
10479 case TERNOP_SLICE:
10480
10481 case OP_LONG:
10482 case OP_DOUBLE:
10483 case OP_DECFLOAT:
10484 case OP_LAST:
10485 case OP_COMPLEX:
10486 case OP_STRING:
10487 case OP_ARRAY:
10488 case OP_TYPE:
10489 case OP_TYPEOF:
10490 case OP_DECLTYPE:
10491 case OP_TYPEID:
10492 case OP_NAME:
10493 case OP_OBJC_NSSTRING:
10494
10495 case UNOP_NEG:
10496 case UNOP_LOGICAL_NOT:
10497 case UNOP_COMPLEMENT:
10498 case UNOP_ADDR:
10499 case UNOP_HIGH:
10500 case UNOP_CAST:
10501
10502 case UNOP_CAST_TYPE:
10503 case UNOP_REINTERPRET_CAST:
10504 case UNOP_DYNAMIC_CAST:
10505 /* Unary, binary and ternary operators: We have to check
10506 their operands. If they are constant, then so is the
10507 result of that operation. For instance, if A and B are
10508 determined to be constants, then so is "A + B".
10509
10510 UNOP_IND is one exception to the rule above, because the
10511 value of *ADDR is not necessarily a constant, even when
10512 ADDR is. */
10513 break;
10514
10515 case OP_VAR_VALUE:
10516 /* Check whether the associated symbol is a constant.
10517
10518 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10519 possible that a buggy compiler could mark a variable as
10520 constant even when it is not, and TYPE_CONST would return
10521 true in this case, while SYMBOL_CLASS wouldn't.
10522
10523 We also have to check for function symbols because they
10524 are always constant. */
10525 {
10526 struct symbol *s = exp->elts[i + 2].symbol;
10527
10528 if (SYMBOL_CLASS (s) != LOC_BLOCK
10529 && SYMBOL_CLASS (s) != LOC_CONST
10530 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10531 return 0;
10532 break;
10533 }
10534
10535 /* The default action is to return 0 because we are using
10536 the optimistic approach here: If we don't know something,
10537 then it is not a constant. */
10538 default:
10539 return 0;
10540 }
10541 }
10542
10543 return 1;
10544 }
10545
10546 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10547
10548 static void
10549 dtor_watchpoint (struct breakpoint *self)
10550 {
10551 struct watchpoint *w = (struct watchpoint *) self;
10552
10553 xfree (w->exp_string);
10554 xfree (w->exp_string_reparse);
10555 value_free (w->val);
10556
10557 base_breakpoint_ops.dtor (self);
10558 }
10559
10560 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10561
10562 static void
10563 re_set_watchpoint (struct breakpoint *b)
10564 {
10565 struct watchpoint *w = (struct watchpoint *) b;
10566
10567 /* Watchpoint can be either on expression using entirely global
10568 variables, or it can be on local variables.
10569
10570 Watchpoints of the first kind are never auto-deleted, and even
10571 persist across program restarts. Since they can use variables
10572 from shared libraries, we need to reparse expression as libraries
10573 are loaded and unloaded.
10574
10575 Watchpoints on local variables can also change meaning as result
10576 of solib event. For example, if a watchpoint uses both a local
10577 and a global variables in expression, it's a local watchpoint,
10578 but unloading of a shared library will make the expression
10579 invalid. This is not a very common use case, but we still
10580 re-evaluate expression, to avoid surprises to the user.
10581
10582 Note that for local watchpoints, we re-evaluate it only if
10583 watchpoints frame id is still valid. If it's not, it means the
10584 watchpoint is out of scope and will be deleted soon. In fact,
10585 I'm not sure we'll ever be called in this case.
10586
10587 If a local watchpoint's frame id is still valid, then
10588 w->exp_valid_block is likewise valid, and we can safely use it.
10589
10590 Don't do anything about disabled watchpoints, since they will be
10591 reevaluated again when enabled. */
10592 update_watchpoint (w, 1 /* reparse */);
10593 }
10594
10595 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10596
10597 static int
10598 insert_watchpoint (struct bp_location *bl)
10599 {
10600 struct watchpoint *w = (struct watchpoint *) bl->owner;
10601 int length = w->exact ? 1 : bl->length;
10602
10603 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10604 w->cond_exp.get ());
10605 }
10606
10607 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10608
10609 static int
10610 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10611 {
10612 struct watchpoint *w = (struct watchpoint *) bl->owner;
10613 int length = w->exact ? 1 : bl->length;
10614
10615 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10616 w->cond_exp.get ());
10617 }
10618
10619 static int
10620 breakpoint_hit_watchpoint (const struct bp_location *bl,
10621 struct address_space *aspace, CORE_ADDR bp_addr,
10622 const struct target_waitstatus *ws)
10623 {
10624 struct breakpoint *b = bl->owner;
10625 struct watchpoint *w = (struct watchpoint *) b;
10626
10627 /* Continuable hardware watchpoints are treated as non-existent if the
10628 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10629 some data address). Otherwise gdb won't stop on a break instruction
10630 in the code (not from a breakpoint) when a hardware watchpoint has
10631 been defined. Also skip watchpoints which we know did not trigger
10632 (did not match the data address). */
10633 if (is_hardware_watchpoint (b)
10634 && w->watchpoint_triggered == watch_triggered_no)
10635 return 0;
10636
10637 return 1;
10638 }
10639
10640 static void
10641 check_status_watchpoint (bpstat bs)
10642 {
10643 gdb_assert (is_watchpoint (bs->breakpoint_at));
10644
10645 bpstat_check_watchpoint (bs);
10646 }
10647
10648 /* Implement the "resources_needed" breakpoint_ops method for
10649 hardware watchpoints. */
10650
10651 static int
10652 resources_needed_watchpoint (const struct bp_location *bl)
10653 {
10654 struct watchpoint *w = (struct watchpoint *) bl->owner;
10655 int length = w->exact? 1 : bl->length;
10656
10657 return target_region_ok_for_hw_watchpoint (bl->address, length);
10658 }
10659
10660 /* Implement the "works_in_software_mode" breakpoint_ops method for
10661 hardware watchpoints. */
10662
10663 static int
10664 works_in_software_mode_watchpoint (const struct breakpoint *b)
10665 {
10666 /* Read and access watchpoints only work with hardware support. */
10667 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10668 }
10669
10670 static enum print_stop_action
10671 print_it_watchpoint (bpstat bs)
10672 {
10673 struct cleanup *old_chain;
10674 struct breakpoint *b;
10675 enum print_stop_action result;
10676 struct watchpoint *w;
10677 struct ui_out *uiout = current_uiout;
10678
10679 gdb_assert (bs->bp_location_at != NULL);
10680
10681 b = bs->breakpoint_at;
10682 w = (struct watchpoint *) b;
10683
10684 old_chain = make_cleanup (null_cleanup, NULL);
10685
10686 annotate_watchpoint (b->number);
10687 maybe_print_thread_hit_breakpoint (uiout);
10688
10689 string_file stb;
10690
10691 switch (b->type)
10692 {
10693 case bp_watchpoint:
10694 case bp_hardware_watchpoint:
10695 if (uiout->is_mi_like_p ())
10696 uiout->field_string
10697 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10698 mention (b);
10699 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10700 uiout->text ("\nOld value = ");
10701 watchpoint_value_print (bs->old_val, &stb);
10702 uiout->field_stream ("old", stb);
10703 uiout->text ("\nNew value = ");
10704 watchpoint_value_print (w->val, &stb);
10705 uiout->field_stream ("new", stb);
10706 uiout->text ("\n");
10707 /* More than one watchpoint may have been triggered. */
10708 result = PRINT_UNKNOWN;
10709 break;
10710
10711 case bp_read_watchpoint:
10712 if (uiout->is_mi_like_p ())
10713 uiout->field_string
10714 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10715 mention (b);
10716 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10717 uiout->text ("\nValue = ");
10718 watchpoint_value_print (w->val, &stb);
10719 uiout->field_stream ("value", stb);
10720 uiout->text ("\n");
10721 result = PRINT_UNKNOWN;
10722 break;
10723
10724 case bp_access_watchpoint:
10725 if (bs->old_val != NULL)
10726 {
10727 if (uiout->is_mi_like_p ())
10728 uiout->field_string
10729 ("reason",
10730 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10731 mention (b);
10732 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10733 uiout->text ("\nOld value = ");
10734 watchpoint_value_print (bs->old_val, &stb);
10735 uiout->field_stream ("old", stb);
10736 uiout->text ("\nNew value = ");
10737 }
10738 else
10739 {
10740 mention (b);
10741 if (uiout->is_mi_like_p ())
10742 uiout->field_string
10743 ("reason",
10744 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10745 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10746 uiout->text ("\nValue = ");
10747 }
10748 watchpoint_value_print (w->val, &stb);
10749 uiout->field_stream ("new", stb);
10750 uiout->text ("\n");
10751 result = PRINT_UNKNOWN;
10752 break;
10753 default:
10754 result = PRINT_UNKNOWN;
10755 }
10756
10757 do_cleanups (old_chain);
10758 return result;
10759 }
10760
10761 /* Implement the "print_mention" breakpoint_ops method for hardware
10762 watchpoints. */
10763
10764 static void
10765 print_mention_watchpoint (struct breakpoint *b)
10766 {
10767 struct watchpoint *w = (struct watchpoint *) b;
10768 struct ui_out *uiout = current_uiout;
10769 const char *tuple_name;
10770
10771 switch (b->type)
10772 {
10773 case bp_watchpoint:
10774 uiout->text ("Watchpoint ");
10775 tuple_name = "wpt";
10776 break;
10777 case bp_hardware_watchpoint:
10778 uiout->text ("Hardware watchpoint ");
10779 tuple_name = "wpt";
10780 break;
10781 case bp_read_watchpoint:
10782 uiout->text ("Hardware read watchpoint ");
10783 tuple_name = "hw-rwpt";
10784 break;
10785 case bp_access_watchpoint:
10786 uiout->text ("Hardware access (read/write) watchpoint ");
10787 tuple_name = "hw-awpt";
10788 break;
10789 default:
10790 internal_error (__FILE__, __LINE__,
10791 _("Invalid hardware watchpoint type."));
10792 }
10793
10794 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10795 uiout->field_int ("number", b->number);
10796 uiout->text (": ");
10797 uiout->field_string ("exp", w->exp_string);
10798 }
10799
10800 /* Implement the "print_recreate" breakpoint_ops method for
10801 watchpoints. */
10802
10803 static void
10804 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10805 {
10806 struct watchpoint *w = (struct watchpoint *) b;
10807
10808 switch (b->type)
10809 {
10810 case bp_watchpoint:
10811 case bp_hardware_watchpoint:
10812 fprintf_unfiltered (fp, "watch");
10813 break;
10814 case bp_read_watchpoint:
10815 fprintf_unfiltered (fp, "rwatch");
10816 break;
10817 case bp_access_watchpoint:
10818 fprintf_unfiltered (fp, "awatch");
10819 break;
10820 default:
10821 internal_error (__FILE__, __LINE__,
10822 _("Invalid watchpoint type."));
10823 }
10824
10825 fprintf_unfiltered (fp, " %s", w->exp_string);
10826 print_recreate_thread (b, fp);
10827 }
10828
10829 /* Implement the "explains_signal" breakpoint_ops method for
10830 watchpoints. */
10831
10832 static int
10833 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10834 {
10835 /* A software watchpoint cannot cause a signal other than
10836 GDB_SIGNAL_TRAP. */
10837 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10838 return 0;
10839
10840 return 1;
10841 }
10842
10843 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10844
10845 static struct breakpoint_ops watchpoint_breakpoint_ops;
10846
10847 /* Implement the "insert" breakpoint_ops method for
10848 masked hardware watchpoints. */
10849
10850 static int
10851 insert_masked_watchpoint (struct bp_location *bl)
10852 {
10853 struct watchpoint *w = (struct watchpoint *) bl->owner;
10854
10855 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10856 bl->watchpoint_type);
10857 }
10858
10859 /* Implement the "remove" breakpoint_ops method for
10860 masked hardware watchpoints. */
10861
10862 static int
10863 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10864 {
10865 struct watchpoint *w = (struct watchpoint *) bl->owner;
10866
10867 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10868 bl->watchpoint_type);
10869 }
10870
10871 /* Implement the "resources_needed" breakpoint_ops method for
10872 masked hardware watchpoints. */
10873
10874 static int
10875 resources_needed_masked_watchpoint (const struct bp_location *bl)
10876 {
10877 struct watchpoint *w = (struct watchpoint *) bl->owner;
10878
10879 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10880 }
10881
10882 /* Implement the "works_in_software_mode" breakpoint_ops method for
10883 masked hardware watchpoints. */
10884
10885 static int
10886 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10887 {
10888 return 0;
10889 }
10890
10891 /* Implement the "print_it" breakpoint_ops method for
10892 masked hardware watchpoints. */
10893
10894 static enum print_stop_action
10895 print_it_masked_watchpoint (bpstat bs)
10896 {
10897 struct breakpoint *b = bs->breakpoint_at;
10898 struct ui_out *uiout = current_uiout;
10899
10900 /* Masked watchpoints have only one location. */
10901 gdb_assert (b->loc && b->loc->next == NULL);
10902
10903 annotate_watchpoint (b->number);
10904 maybe_print_thread_hit_breakpoint (uiout);
10905
10906 switch (b->type)
10907 {
10908 case bp_hardware_watchpoint:
10909 if (uiout->is_mi_like_p ())
10910 uiout->field_string
10911 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10912 break;
10913
10914 case bp_read_watchpoint:
10915 if (uiout->is_mi_like_p ())
10916 uiout->field_string
10917 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10918 break;
10919
10920 case bp_access_watchpoint:
10921 if (uiout->is_mi_like_p ())
10922 uiout->field_string
10923 ("reason",
10924 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10925 break;
10926 default:
10927 internal_error (__FILE__, __LINE__,
10928 _("Invalid hardware watchpoint type."));
10929 }
10930
10931 mention (b);
10932 uiout->text (_("\n\
10933 Check the underlying instruction at PC for the memory\n\
10934 address and value which triggered this watchpoint.\n"));
10935 uiout->text ("\n");
10936
10937 /* More than one watchpoint may have been triggered. */
10938 return PRINT_UNKNOWN;
10939 }
10940
10941 /* Implement the "print_one_detail" breakpoint_ops method for
10942 masked hardware watchpoints. */
10943
10944 static void
10945 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10946 struct ui_out *uiout)
10947 {
10948 struct watchpoint *w = (struct watchpoint *) b;
10949
10950 /* Masked watchpoints have only one location. */
10951 gdb_assert (b->loc && b->loc->next == NULL);
10952
10953 uiout->text ("\tmask ");
10954 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10955 uiout->text ("\n");
10956 }
10957
10958 /* Implement the "print_mention" breakpoint_ops method for
10959 masked hardware watchpoints. */
10960
10961 static void
10962 print_mention_masked_watchpoint (struct breakpoint *b)
10963 {
10964 struct watchpoint *w = (struct watchpoint *) b;
10965 struct ui_out *uiout = current_uiout;
10966 const char *tuple_name;
10967
10968 switch (b->type)
10969 {
10970 case bp_hardware_watchpoint:
10971 uiout->text ("Masked hardware watchpoint ");
10972 tuple_name = "wpt";
10973 break;
10974 case bp_read_watchpoint:
10975 uiout->text ("Masked hardware read watchpoint ");
10976 tuple_name = "hw-rwpt";
10977 break;
10978 case bp_access_watchpoint:
10979 uiout->text ("Masked hardware access (read/write) watchpoint ");
10980 tuple_name = "hw-awpt";
10981 break;
10982 default:
10983 internal_error (__FILE__, __LINE__,
10984 _("Invalid hardware watchpoint type."));
10985 }
10986
10987 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10988 uiout->field_int ("number", b->number);
10989 uiout->text (": ");
10990 uiout->field_string ("exp", w->exp_string);
10991 }
10992
10993 /* Implement the "print_recreate" breakpoint_ops method for
10994 masked hardware watchpoints. */
10995
10996 static void
10997 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10998 {
10999 struct watchpoint *w = (struct watchpoint *) b;
11000 char tmp[40];
11001
11002 switch (b->type)
11003 {
11004 case bp_hardware_watchpoint:
11005 fprintf_unfiltered (fp, "watch");
11006 break;
11007 case bp_read_watchpoint:
11008 fprintf_unfiltered (fp, "rwatch");
11009 break;
11010 case bp_access_watchpoint:
11011 fprintf_unfiltered (fp, "awatch");
11012 break;
11013 default:
11014 internal_error (__FILE__, __LINE__,
11015 _("Invalid hardware watchpoint type."));
11016 }
11017
11018 sprintf_vma (tmp, w->hw_wp_mask);
11019 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11020 print_recreate_thread (b, fp);
11021 }
11022
11023 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11024
11025 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11026
11027 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11028
11029 static int
11030 is_masked_watchpoint (const struct breakpoint *b)
11031 {
11032 return b->ops == &masked_watchpoint_breakpoint_ops;
11033 }
11034
11035 /* accessflag: hw_write: watch write,
11036 hw_read: watch read,
11037 hw_access: watch access (read or write) */
11038 static void
11039 watch_command_1 (const char *arg, int accessflag, int from_tty,
11040 int just_location, int internal)
11041 {
11042 struct breakpoint *b, *scope_breakpoint = NULL;
11043 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11044 struct value *val, *mark, *result;
11045 int saved_bitpos = 0, saved_bitsize = 0;
11046 const char *exp_start = NULL;
11047 const char *exp_end = NULL;
11048 const char *tok, *end_tok;
11049 int toklen = -1;
11050 const char *cond_start = NULL;
11051 const char *cond_end = NULL;
11052 enum bptype bp_type;
11053 int thread = -1;
11054 int pc = 0;
11055 /* Flag to indicate whether we are going to use masks for
11056 the hardware watchpoint. */
11057 int use_mask = 0;
11058 CORE_ADDR mask = 0;
11059 struct watchpoint *w;
11060 char *expression;
11061 struct cleanup *back_to;
11062
11063 /* Make sure that we actually have parameters to parse. */
11064 if (arg != NULL && arg[0] != '\0')
11065 {
11066 const char *value_start;
11067
11068 exp_end = arg + strlen (arg);
11069
11070 /* Look for "parameter value" pairs at the end
11071 of the arguments string. */
11072 for (tok = exp_end - 1; tok > arg; tok--)
11073 {
11074 /* Skip whitespace at the end of the argument list. */
11075 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11076 tok--;
11077
11078 /* Find the beginning of the last token.
11079 This is the value of the parameter. */
11080 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11081 tok--;
11082 value_start = tok + 1;
11083
11084 /* Skip whitespace. */
11085 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11086 tok--;
11087
11088 end_tok = tok;
11089
11090 /* Find the beginning of the second to last token.
11091 This is the parameter itself. */
11092 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11093 tok--;
11094 tok++;
11095 toklen = end_tok - tok + 1;
11096
11097 if (toklen == 6 && startswith (tok, "thread"))
11098 {
11099 struct thread_info *thr;
11100 /* At this point we've found a "thread" token, which means
11101 the user is trying to set a watchpoint that triggers
11102 only in a specific thread. */
11103 const char *endp;
11104
11105 if (thread != -1)
11106 error(_("You can specify only one thread."));
11107
11108 /* Extract the thread ID from the next token. */
11109 thr = parse_thread_id (value_start, &endp);
11110
11111 /* Check if the user provided a valid thread ID. */
11112 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11113 invalid_thread_id_error (value_start);
11114
11115 thread = thr->global_num;
11116 }
11117 else if (toklen == 4 && startswith (tok, "mask"))
11118 {
11119 /* We've found a "mask" token, which means the user wants to
11120 create a hardware watchpoint that is going to have the mask
11121 facility. */
11122 struct value *mask_value, *mark;
11123
11124 if (use_mask)
11125 error(_("You can specify only one mask."));
11126
11127 use_mask = just_location = 1;
11128
11129 mark = value_mark ();
11130 mask_value = parse_to_comma_and_eval (&value_start);
11131 mask = value_as_address (mask_value);
11132 value_free_to_mark (mark);
11133 }
11134 else
11135 /* We didn't recognize what we found. We should stop here. */
11136 break;
11137
11138 /* Truncate the string and get rid of the "parameter value" pair before
11139 the arguments string is parsed by the parse_exp_1 function. */
11140 exp_end = tok;
11141 }
11142 }
11143 else
11144 exp_end = arg;
11145
11146 /* Parse the rest of the arguments. From here on out, everything
11147 is in terms of a newly allocated string instead of the original
11148 ARG. */
11149 innermost_block = NULL;
11150 expression = savestring (arg, exp_end - arg);
11151 back_to = make_cleanup (xfree, expression);
11152 exp_start = arg = expression;
11153 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11154 exp_end = arg;
11155 /* Remove trailing whitespace from the expression before saving it.
11156 This makes the eventual display of the expression string a bit
11157 prettier. */
11158 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11159 --exp_end;
11160
11161 /* Checking if the expression is not constant. */
11162 if (watchpoint_exp_is_const (exp.get ()))
11163 {
11164 int len;
11165
11166 len = exp_end - exp_start;
11167 while (len > 0 && isspace (exp_start[len - 1]))
11168 len--;
11169 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11170 }
11171
11172 exp_valid_block = innermost_block;
11173 mark = value_mark ();
11174 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11175
11176 if (val != NULL && just_location)
11177 {
11178 saved_bitpos = value_bitpos (val);
11179 saved_bitsize = value_bitsize (val);
11180 }
11181
11182 if (just_location)
11183 {
11184 int ret;
11185
11186 exp_valid_block = NULL;
11187 val = value_addr (result);
11188 release_value (val);
11189 value_free_to_mark (mark);
11190
11191 if (use_mask)
11192 {
11193 ret = target_masked_watch_num_registers (value_as_address (val),
11194 mask);
11195 if (ret == -1)
11196 error (_("This target does not support masked watchpoints."));
11197 else if (ret == -2)
11198 error (_("Invalid mask or memory region."));
11199 }
11200 }
11201 else if (val != NULL)
11202 release_value (val);
11203
11204 tok = skip_spaces_const (arg);
11205 end_tok = skip_to_space_const (tok);
11206
11207 toklen = end_tok - tok;
11208 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11209 {
11210 innermost_block = NULL;
11211 tok = cond_start = end_tok + 1;
11212 parse_exp_1 (&tok, 0, 0, 0);
11213
11214 /* The watchpoint expression may not be local, but the condition
11215 may still be. E.g.: `watch global if local > 0'. */
11216 cond_exp_valid_block = innermost_block;
11217
11218 cond_end = tok;
11219 }
11220 if (*tok)
11221 error (_("Junk at end of command."));
11222
11223 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11224
11225 /* Save this because create_internal_breakpoint below invalidates
11226 'wp_frame'. */
11227 frame_id watchpoint_frame = get_frame_id (wp_frame);
11228
11229 /* If the expression is "local", then set up a "watchpoint scope"
11230 breakpoint at the point where we've left the scope of the watchpoint
11231 expression. Create the scope breakpoint before the watchpoint, so
11232 that we will encounter it first in bpstat_stop_status. */
11233 if (exp_valid_block != NULL && wp_frame != NULL)
11234 {
11235 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11236
11237 if (frame_id_p (caller_frame_id))
11238 {
11239 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11240 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11241
11242 scope_breakpoint
11243 = create_internal_breakpoint (caller_arch, caller_pc,
11244 bp_watchpoint_scope,
11245 &momentary_breakpoint_ops);
11246
11247 /* create_internal_breakpoint could invalidate WP_FRAME. */
11248 wp_frame = NULL;
11249
11250 scope_breakpoint->enable_state = bp_enabled;
11251
11252 /* Automatically delete the breakpoint when it hits. */
11253 scope_breakpoint->disposition = disp_del;
11254
11255 /* Only break in the proper frame (help with recursion). */
11256 scope_breakpoint->frame_id = caller_frame_id;
11257
11258 /* Set the address at which we will stop. */
11259 scope_breakpoint->loc->gdbarch = caller_arch;
11260 scope_breakpoint->loc->requested_address = caller_pc;
11261 scope_breakpoint->loc->address
11262 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11263 scope_breakpoint->loc->requested_address,
11264 scope_breakpoint->type);
11265 }
11266 }
11267
11268 /* Now set up the breakpoint. We create all watchpoints as hardware
11269 watchpoints here even if hardware watchpoints are turned off, a call
11270 to update_watchpoint later in this function will cause the type to
11271 drop back to bp_watchpoint (software watchpoint) if required. */
11272
11273 if (accessflag == hw_read)
11274 bp_type = bp_read_watchpoint;
11275 else if (accessflag == hw_access)
11276 bp_type = bp_access_watchpoint;
11277 else
11278 bp_type = bp_hardware_watchpoint;
11279
11280 w = new watchpoint ();
11281 b = &w->base;
11282 if (use_mask)
11283 init_raw_breakpoint_without_location (b, NULL, bp_type,
11284 &masked_watchpoint_breakpoint_ops);
11285 else
11286 init_raw_breakpoint_without_location (b, NULL, bp_type,
11287 &watchpoint_breakpoint_ops);
11288 b->thread = thread;
11289 b->disposition = disp_donttouch;
11290 b->pspace = current_program_space;
11291 w->exp = std::move (exp);
11292 w->exp_valid_block = exp_valid_block;
11293 w->cond_exp_valid_block = cond_exp_valid_block;
11294 if (just_location)
11295 {
11296 struct type *t = value_type (val);
11297 CORE_ADDR addr = value_as_address (val);
11298
11299 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11300
11301 std::string name = type_to_string (t);
11302
11303 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11304 core_addr_to_string (addr));
11305
11306 w->exp_string = xstrprintf ("-location %.*s",
11307 (int) (exp_end - exp_start), exp_start);
11308
11309 /* The above expression is in C. */
11310 b->language = language_c;
11311 }
11312 else
11313 w->exp_string = savestring (exp_start, exp_end - exp_start);
11314
11315 if (use_mask)
11316 {
11317 w->hw_wp_mask = mask;
11318 }
11319 else
11320 {
11321 w->val = val;
11322 w->val_bitpos = saved_bitpos;
11323 w->val_bitsize = saved_bitsize;
11324 w->val_valid = 1;
11325 }
11326
11327 if (cond_start)
11328 b->cond_string = savestring (cond_start, cond_end - cond_start);
11329 else
11330 b->cond_string = 0;
11331
11332 if (frame_id_p (watchpoint_frame))
11333 {
11334 w->watchpoint_frame = watchpoint_frame;
11335 w->watchpoint_thread = inferior_ptid;
11336 }
11337 else
11338 {
11339 w->watchpoint_frame = null_frame_id;
11340 w->watchpoint_thread = null_ptid;
11341 }
11342
11343 if (scope_breakpoint != NULL)
11344 {
11345 /* The scope breakpoint is related to the watchpoint. We will
11346 need to act on them together. */
11347 b->related_breakpoint = scope_breakpoint;
11348 scope_breakpoint->related_breakpoint = b;
11349 }
11350
11351 if (!just_location)
11352 value_free_to_mark (mark);
11353
11354 TRY
11355 {
11356 /* Finally update the new watchpoint. This creates the locations
11357 that should be inserted. */
11358 update_watchpoint (w, 1);
11359 }
11360 CATCH (e, RETURN_MASK_ALL)
11361 {
11362 delete_breakpoint (b);
11363 throw_exception (e);
11364 }
11365 END_CATCH
11366
11367 install_breakpoint (internal, b, 1);
11368 do_cleanups (back_to);
11369 }
11370
11371 /* Return count of debug registers needed to watch the given expression.
11372 If the watchpoint cannot be handled in hardware return zero. */
11373
11374 static int
11375 can_use_hardware_watchpoint (struct value *v)
11376 {
11377 int found_memory_cnt = 0;
11378 struct value *head = v;
11379
11380 /* Did the user specifically forbid us to use hardware watchpoints? */
11381 if (!can_use_hw_watchpoints)
11382 return 0;
11383
11384 /* Make sure that the value of the expression depends only upon
11385 memory contents, and values computed from them within GDB. If we
11386 find any register references or function calls, we can't use a
11387 hardware watchpoint.
11388
11389 The idea here is that evaluating an expression generates a series
11390 of values, one holding the value of every subexpression. (The
11391 expression a*b+c has five subexpressions: a, b, a*b, c, and
11392 a*b+c.) GDB's values hold almost enough information to establish
11393 the criteria given above --- they identify memory lvalues,
11394 register lvalues, computed values, etcetera. So we can evaluate
11395 the expression, and then scan the chain of values that leaves
11396 behind to decide whether we can detect any possible change to the
11397 expression's final value using only hardware watchpoints.
11398
11399 However, I don't think that the values returned by inferior
11400 function calls are special in any way. So this function may not
11401 notice that an expression involving an inferior function call
11402 can't be watched with hardware watchpoints. FIXME. */
11403 for (; v; v = value_next (v))
11404 {
11405 if (VALUE_LVAL (v) == lval_memory)
11406 {
11407 if (v != head && value_lazy (v))
11408 /* A lazy memory lvalue in the chain is one that GDB never
11409 needed to fetch; we either just used its address (e.g.,
11410 `a' in `a.b') or we never needed it at all (e.g., `a'
11411 in `a,b'). This doesn't apply to HEAD; if that is
11412 lazy then it was not readable, but watch it anyway. */
11413 ;
11414 else
11415 {
11416 /* Ahh, memory we actually used! Check if we can cover
11417 it with hardware watchpoints. */
11418 struct type *vtype = check_typedef (value_type (v));
11419
11420 /* We only watch structs and arrays if user asked for it
11421 explicitly, never if they just happen to appear in a
11422 middle of some value chain. */
11423 if (v == head
11424 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11425 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11426 {
11427 CORE_ADDR vaddr = value_address (v);
11428 int len;
11429 int num_regs;
11430
11431 len = (target_exact_watchpoints
11432 && is_scalar_type_recursive (vtype))?
11433 1 : TYPE_LENGTH (value_type (v));
11434
11435 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11436 if (!num_regs)
11437 return 0;
11438 else
11439 found_memory_cnt += num_regs;
11440 }
11441 }
11442 }
11443 else if (VALUE_LVAL (v) != not_lval
11444 && deprecated_value_modifiable (v) == 0)
11445 return 0; /* These are values from the history (e.g., $1). */
11446 else if (VALUE_LVAL (v) == lval_register)
11447 return 0; /* Cannot watch a register with a HW watchpoint. */
11448 }
11449
11450 /* The expression itself looks suitable for using a hardware
11451 watchpoint, but give the target machine a chance to reject it. */
11452 return found_memory_cnt;
11453 }
11454
11455 void
11456 watch_command_wrapper (char *arg, int from_tty, int internal)
11457 {
11458 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11459 }
11460
11461 /* A helper function that looks for the "-location" argument and then
11462 calls watch_command_1. */
11463
11464 static void
11465 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11466 {
11467 int just_location = 0;
11468
11469 if (arg
11470 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11471 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11472 {
11473 arg = skip_spaces (arg);
11474 just_location = 1;
11475 }
11476
11477 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11478 }
11479
11480 static void
11481 watch_command (char *arg, int from_tty)
11482 {
11483 watch_maybe_just_location (arg, hw_write, from_tty);
11484 }
11485
11486 void
11487 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11488 {
11489 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11490 }
11491
11492 static void
11493 rwatch_command (char *arg, int from_tty)
11494 {
11495 watch_maybe_just_location (arg, hw_read, from_tty);
11496 }
11497
11498 void
11499 awatch_command_wrapper (char *arg, int from_tty, int internal)
11500 {
11501 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11502 }
11503
11504 static void
11505 awatch_command (char *arg, int from_tty)
11506 {
11507 watch_maybe_just_location (arg, hw_access, from_tty);
11508 }
11509 \f
11510
11511 /* Data for the FSM that manages the until(location)/advance commands
11512 in infcmd.c. Here because it uses the mechanisms of
11513 breakpoints. */
11514
11515 struct until_break_fsm
11516 {
11517 /* The base class. */
11518 struct thread_fsm thread_fsm;
11519
11520 /* The thread that as current when the command was executed. */
11521 int thread;
11522
11523 /* The breakpoint set at the destination location. */
11524 struct breakpoint *location_breakpoint;
11525
11526 /* Breakpoint set at the return address in the caller frame. May be
11527 NULL. */
11528 struct breakpoint *caller_breakpoint;
11529 };
11530
11531 static void until_break_fsm_clean_up (struct thread_fsm *self,
11532 struct thread_info *thread);
11533 static int until_break_fsm_should_stop (struct thread_fsm *self,
11534 struct thread_info *thread);
11535 static enum async_reply_reason
11536 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11537
11538 /* until_break_fsm's vtable. */
11539
11540 static struct thread_fsm_ops until_break_fsm_ops =
11541 {
11542 NULL, /* dtor */
11543 until_break_fsm_clean_up,
11544 until_break_fsm_should_stop,
11545 NULL, /* return_value */
11546 until_break_fsm_async_reply_reason,
11547 };
11548
11549 /* Allocate a new until_break_command_fsm. */
11550
11551 static struct until_break_fsm *
11552 new_until_break_fsm (struct interp *cmd_interp, int thread,
11553 struct breakpoint *location_breakpoint,
11554 struct breakpoint *caller_breakpoint)
11555 {
11556 struct until_break_fsm *sm;
11557
11558 sm = XCNEW (struct until_break_fsm);
11559 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11560
11561 sm->thread = thread;
11562 sm->location_breakpoint = location_breakpoint;
11563 sm->caller_breakpoint = caller_breakpoint;
11564
11565 return sm;
11566 }
11567
11568 /* Implementation of the 'should_stop' FSM method for the
11569 until(location)/advance commands. */
11570
11571 static int
11572 until_break_fsm_should_stop (struct thread_fsm *self,
11573 struct thread_info *tp)
11574 {
11575 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11576
11577 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11578 sm->location_breakpoint) != NULL
11579 || (sm->caller_breakpoint != NULL
11580 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11581 sm->caller_breakpoint) != NULL))
11582 thread_fsm_set_finished (self);
11583
11584 return 1;
11585 }
11586
11587 /* Implementation of the 'clean_up' FSM method for the
11588 until(location)/advance commands. */
11589
11590 static void
11591 until_break_fsm_clean_up (struct thread_fsm *self,
11592 struct thread_info *thread)
11593 {
11594 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11595
11596 /* Clean up our temporary breakpoints. */
11597 if (sm->location_breakpoint != NULL)
11598 {
11599 delete_breakpoint (sm->location_breakpoint);
11600 sm->location_breakpoint = NULL;
11601 }
11602 if (sm->caller_breakpoint != NULL)
11603 {
11604 delete_breakpoint (sm->caller_breakpoint);
11605 sm->caller_breakpoint = NULL;
11606 }
11607 delete_longjmp_breakpoint (sm->thread);
11608 }
11609
11610 /* Implementation of the 'async_reply_reason' FSM method for the
11611 until(location)/advance commands. */
11612
11613 static enum async_reply_reason
11614 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11615 {
11616 return EXEC_ASYNC_LOCATION_REACHED;
11617 }
11618
11619 void
11620 until_break_command (char *arg, int from_tty, int anywhere)
11621 {
11622 struct symtabs_and_lines sals;
11623 struct symtab_and_line sal;
11624 struct frame_info *frame;
11625 struct gdbarch *frame_gdbarch;
11626 struct frame_id stack_frame_id;
11627 struct frame_id caller_frame_id;
11628 struct breakpoint *location_breakpoint;
11629 struct breakpoint *caller_breakpoint = NULL;
11630 struct cleanup *old_chain;
11631 int thread;
11632 struct thread_info *tp;
11633 struct until_break_fsm *sm;
11634
11635 clear_proceed_status (0);
11636
11637 /* Set a breakpoint where the user wants it and at return from
11638 this function. */
11639
11640 event_location_up location = string_to_event_location (&arg, current_language);
11641
11642 if (last_displayed_sal_is_valid ())
11643 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11644 get_last_displayed_symtab (),
11645 get_last_displayed_line ());
11646 else
11647 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11648 NULL, (struct symtab *) NULL, 0);
11649
11650 if (sals.nelts != 1)
11651 error (_("Couldn't get information on specified line."));
11652
11653 sal = sals.sals[0];
11654 xfree (sals.sals); /* malloc'd, so freed. */
11655
11656 if (*arg)
11657 error (_("Junk at end of arguments."));
11658
11659 resolve_sal_pc (&sal);
11660
11661 tp = inferior_thread ();
11662 thread = tp->global_num;
11663
11664 old_chain = make_cleanup (null_cleanup, NULL);
11665
11666 /* Note linespec handling above invalidates the frame chain.
11667 Installing a breakpoint also invalidates the frame chain (as it
11668 may need to switch threads), so do any frame handling before
11669 that. */
11670
11671 frame = get_selected_frame (NULL);
11672 frame_gdbarch = get_frame_arch (frame);
11673 stack_frame_id = get_stack_frame_id (frame);
11674 caller_frame_id = frame_unwind_caller_id (frame);
11675
11676 /* Keep within the current frame, or in frames called by the current
11677 one. */
11678
11679 if (frame_id_p (caller_frame_id))
11680 {
11681 struct symtab_and_line sal2;
11682 struct gdbarch *caller_gdbarch;
11683
11684 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11685 sal2.pc = frame_unwind_caller_pc (frame);
11686 caller_gdbarch = frame_unwind_caller_arch (frame);
11687 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11688 sal2,
11689 caller_frame_id,
11690 bp_until);
11691 make_cleanup_delete_breakpoint (caller_breakpoint);
11692
11693 set_longjmp_breakpoint (tp, caller_frame_id);
11694 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11695 }
11696
11697 /* set_momentary_breakpoint could invalidate FRAME. */
11698 frame = NULL;
11699
11700 if (anywhere)
11701 /* If the user told us to continue until a specified location,
11702 we don't specify a frame at which we need to stop. */
11703 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11704 null_frame_id, bp_until);
11705 else
11706 /* Otherwise, specify the selected frame, because we want to stop
11707 only at the very same frame. */
11708 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11709 stack_frame_id, bp_until);
11710 make_cleanup_delete_breakpoint (location_breakpoint);
11711
11712 sm = new_until_break_fsm (command_interp (), tp->global_num,
11713 location_breakpoint, caller_breakpoint);
11714 tp->thread_fsm = &sm->thread_fsm;
11715
11716 discard_cleanups (old_chain);
11717
11718 proceed (-1, GDB_SIGNAL_DEFAULT);
11719 }
11720
11721 /* This function attempts to parse an optional "if <cond>" clause
11722 from the arg string. If one is not found, it returns NULL.
11723
11724 Else, it returns a pointer to the condition string. (It does not
11725 attempt to evaluate the string against a particular block.) And,
11726 it updates arg to point to the first character following the parsed
11727 if clause in the arg string. */
11728
11729 const char *
11730 ep_parse_optional_if_clause (const char **arg)
11731 {
11732 const char *cond_string;
11733
11734 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11735 return NULL;
11736
11737 /* Skip the "if" keyword. */
11738 (*arg) += 2;
11739
11740 /* Skip any extra leading whitespace, and record the start of the
11741 condition string. */
11742 *arg = skip_spaces_const (*arg);
11743 cond_string = *arg;
11744
11745 /* Assume that the condition occupies the remainder of the arg
11746 string. */
11747 (*arg) += strlen (cond_string);
11748
11749 return cond_string;
11750 }
11751
11752 /* Commands to deal with catching events, such as signals, exceptions,
11753 process start/exit, etc. */
11754
11755 typedef enum
11756 {
11757 catch_fork_temporary, catch_vfork_temporary,
11758 catch_fork_permanent, catch_vfork_permanent
11759 }
11760 catch_fork_kind;
11761
11762 static void
11763 catch_fork_command_1 (char *arg_entry, int from_tty,
11764 struct cmd_list_element *command)
11765 {
11766 const char *arg = arg_entry;
11767 struct gdbarch *gdbarch = get_current_arch ();
11768 const char *cond_string = NULL;
11769 catch_fork_kind fork_kind;
11770 int tempflag;
11771
11772 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11773 tempflag = (fork_kind == catch_fork_temporary
11774 || fork_kind == catch_vfork_temporary);
11775
11776 if (!arg)
11777 arg = "";
11778 arg = skip_spaces_const (arg);
11779
11780 /* The allowed syntax is:
11781 catch [v]fork
11782 catch [v]fork if <cond>
11783
11784 First, check if there's an if clause. */
11785 cond_string = ep_parse_optional_if_clause (&arg);
11786
11787 if ((*arg != '\0') && !isspace (*arg))
11788 error (_("Junk at end of arguments."));
11789
11790 /* If this target supports it, create a fork or vfork catchpoint
11791 and enable reporting of such events. */
11792 switch (fork_kind)
11793 {
11794 case catch_fork_temporary:
11795 case catch_fork_permanent:
11796 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11797 &catch_fork_breakpoint_ops);
11798 break;
11799 case catch_vfork_temporary:
11800 case catch_vfork_permanent:
11801 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11802 &catch_vfork_breakpoint_ops);
11803 break;
11804 default:
11805 error (_("unsupported or unknown fork kind; cannot catch it"));
11806 break;
11807 }
11808 }
11809
11810 static void
11811 catch_exec_command_1 (char *arg_entry, int from_tty,
11812 struct cmd_list_element *command)
11813 {
11814 const char *arg = arg_entry;
11815 struct exec_catchpoint *c;
11816 struct gdbarch *gdbarch = get_current_arch ();
11817 int tempflag;
11818 const char *cond_string = NULL;
11819
11820 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11821
11822 if (!arg)
11823 arg = "";
11824 arg = skip_spaces_const (arg);
11825
11826 /* The allowed syntax is:
11827 catch exec
11828 catch exec if <cond>
11829
11830 First, check if there's an if clause. */
11831 cond_string = ep_parse_optional_if_clause (&arg);
11832
11833 if ((*arg != '\0') && !isspace (*arg))
11834 error (_("Junk at end of arguments."));
11835
11836 c = new exec_catchpoint ();
11837 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11838 &catch_exec_breakpoint_ops);
11839 c->exec_pathname = NULL;
11840
11841 install_breakpoint (0, &c->base, 1);
11842 }
11843
11844 void
11845 init_ada_exception_breakpoint (struct breakpoint *b,
11846 struct gdbarch *gdbarch,
11847 struct symtab_and_line sal,
11848 char *addr_string,
11849 const struct breakpoint_ops *ops,
11850 int tempflag,
11851 int enabled,
11852 int from_tty)
11853 {
11854 if (from_tty)
11855 {
11856 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11857 if (!loc_gdbarch)
11858 loc_gdbarch = gdbarch;
11859
11860 describe_other_breakpoints (loc_gdbarch,
11861 sal.pspace, sal.pc, sal.section, -1);
11862 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11863 version for exception catchpoints, because two catchpoints
11864 used for different exception names will use the same address.
11865 In this case, a "breakpoint ... also set at..." warning is
11866 unproductive. Besides, the warning phrasing is also a bit
11867 inappropriate, we should use the word catchpoint, and tell
11868 the user what type of catchpoint it is. The above is good
11869 enough for now, though. */
11870 }
11871
11872 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11873
11874 b->enable_state = enabled ? bp_enabled : bp_disabled;
11875 b->disposition = tempflag ? disp_del : disp_donttouch;
11876 b->location = string_to_event_location (&addr_string,
11877 language_def (language_ada));
11878 b->language = language_ada;
11879 }
11880
11881 static void
11882 catch_command (char *arg, int from_tty)
11883 {
11884 error (_("Catch requires an event name."));
11885 }
11886 \f
11887
11888 static void
11889 tcatch_command (char *arg, int from_tty)
11890 {
11891 error (_("Catch requires an event name."));
11892 }
11893
11894 /* A qsort comparison function that sorts breakpoints in order. */
11895
11896 static int
11897 compare_breakpoints (const void *a, const void *b)
11898 {
11899 const breakpoint_p *ba = (const breakpoint_p *) a;
11900 uintptr_t ua = (uintptr_t) *ba;
11901 const breakpoint_p *bb = (const breakpoint_p *) b;
11902 uintptr_t ub = (uintptr_t) *bb;
11903
11904 if ((*ba)->number < (*bb)->number)
11905 return -1;
11906 else if ((*ba)->number > (*bb)->number)
11907 return 1;
11908
11909 /* Now sort by address, in case we see, e..g, two breakpoints with
11910 the number 0. */
11911 if (ua < ub)
11912 return -1;
11913 return ua > ub ? 1 : 0;
11914 }
11915
11916 /* Delete breakpoints by address or line. */
11917
11918 static void
11919 clear_command (char *arg, int from_tty)
11920 {
11921 struct breakpoint *b, *prev;
11922 VEC(breakpoint_p) *found = 0;
11923 int ix;
11924 int default_match;
11925 struct symtabs_and_lines sals;
11926 struct symtab_and_line sal;
11927 int i;
11928 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11929
11930 if (arg)
11931 {
11932 sals = decode_line_with_current_source (arg,
11933 (DECODE_LINE_FUNFIRSTLINE
11934 | DECODE_LINE_LIST_MODE));
11935 make_cleanup (xfree, sals.sals);
11936 default_match = 0;
11937 }
11938 else
11939 {
11940 sals.sals = XNEW (struct symtab_and_line);
11941 make_cleanup (xfree, sals.sals);
11942 init_sal (&sal); /* Initialize to zeroes. */
11943
11944 /* Set sal's line, symtab, pc, and pspace to the values
11945 corresponding to the last call to print_frame_info. If the
11946 codepoint is not valid, this will set all the fields to 0. */
11947 get_last_displayed_sal (&sal);
11948 if (sal.symtab == 0)
11949 error (_("No source file specified."));
11950
11951 sals.sals[0] = sal;
11952 sals.nelts = 1;
11953
11954 default_match = 1;
11955 }
11956
11957 /* We don't call resolve_sal_pc here. That's not as bad as it
11958 seems, because all existing breakpoints typically have both
11959 file/line and pc set. So, if clear is given file/line, we can
11960 match this to existing breakpoint without obtaining pc at all.
11961
11962 We only support clearing given the address explicitly
11963 present in breakpoint table. Say, we've set breakpoint
11964 at file:line. There were several PC values for that file:line,
11965 due to optimization, all in one block.
11966
11967 We've picked one PC value. If "clear" is issued with another
11968 PC corresponding to the same file:line, the breakpoint won't
11969 be cleared. We probably can still clear the breakpoint, but
11970 since the other PC value is never presented to user, user
11971 can only find it by guessing, and it does not seem important
11972 to support that. */
11973
11974 /* For each line spec given, delete bps which correspond to it. Do
11975 it in two passes, solely to preserve the current behavior that
11976 from_tty is forced true if we delete more than one
11977 breakpoint. */
11978
11979 found = NULL;
11980 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11981 for (i = 0; i < sals.nelts; i++)
11982 {
11983 const char *sal_fullname;
11984
11985 /* If exact pc given, clear bpts at that pc.
11986 If line given (pc == 0), clear all bpts on specified line.
11987 If defaulting, clear all bpts on default line
11988 or at default pc.
11989
11990 defaulting sal.pc != 0 tests to do
11991
11992 0 1 pc
11993 1 1 pc _and_ line
11994 0 0 line
11995 1 0 <can't happen> */
11996
11997 sal = sals.sals[i];
11998 sal_fullname = (sal.symtab == NULL
11999 ? NULL : symtab_to_fullname (sal.symtab));
12000
12001 /* Find all matching breakpoints and add them to 'found'. */
12002 ALL_BREAKPOINTS (b)
12003 {
12004 int match = 0;
12005 /* Are we going to delete b? */
12006 if (b->type != bp_none && !is_watchpoint (b))
12007 {
12008 struct bp_location *loc = b->loc;
12009 for (; loc; loc = loc->next)
12010 {
12011 /* If the user specified file:line, don't allow a PC
12012 match. This matches historical gdb behavior. */
12013 int pc_match = (!sal.explicit_line
12014 && sal.pc
12015 && (loc->pspace == sal.pspace)
12016 && (loc->address == sal.pc)
12017 && (!section_is_overlay (loc->section)
12018 || loc->section == sal.section));
12019 int line_match = 0;
12020
12021 if ((default_match || sal.explicit_line)
12022 && loc->symtab != NULL
12023 && sal_fullname != NULL
12024 && sal.pspace == loc->pspace
12025 && loc->line_number == sal.line
12026 && filename_cmp (symtab_to_fullname (loc->symtab),
12027 sal_fullname) == 0)
12028 line_match = 1;
12029
12030 if (pc_match || line_match)
12031 {
12032 match = 1;
12033 break;
12034 }
12035 }
12036 }
12037
12038 if (match)
12039 VEC_safe_push(breakpoint_p, found, b);
12040 }
12041 }
12042
12043 /* Now go thru the 'found' chain and delete them. */
12044 if (VEC_empty(breakpoint_p, found))
12045 {
12046 if (arg)
12047 error (_("No breakpoint at %s."), arg);
12048 else
12049 error (_("No breakpoint at this line."));
12050 }
12051
12052 /* Remove duplicates from the vec. */
12053 qsort (VEC_address (breakpoint_p, found),
12054 VEC_length (breakpoint_p, found),
12055 sizeof (breakpoint_p),
12056 compare_breakpoints);
12057 prev = VEC_index (breakpoint_p, found, 0);
12058 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12059 {
12060 if (b == prev)
12061 {
12062 VEC_ordered_remove (breakpoint_p, found, ix);
12063 --ix;
12064 }
12065 }
12066
12067 if (VEC_length(breakpoint_p, found) > 1)
12068 from_tty = 1; /* Always report if deleted more than one. */
12069 if (from_tty)
12070 {
12071 if (VEC_length(breakpoint_p, found) == 1)
12072 printf_unfiltered (_("Deleted breakpoint "));
12073 else
12074 printf_unfiltered (_("Deleted breakpoints "));
12075 }
12076
12077 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12078 {
12079 if (from_tty)
12080 printf_unfiltered ("%d ", b->number);
12081 delete_breakpoint (b);
12082 }
12083 if (from_tty)
12084 putchar_unfiltered ('\n');
12085
12086 do_cleanups (cleanups);
12087 }
12088 \f
12089 /* Delete breakpoint in BS if they are `delete' breakpoints and
12090 all breakpoints that are marked for deletion, whether hit or not.
12091 This is called after any breakpoint is hit, or after errors. */
12092
12093 void
12094 breakpoint_auto_delete (bpstat bs)
12095 {
12096 struct breakpoint *b, *b_tmp;
12097
12098 for (; bs; bs = bs->next)
12099 if (bs->breakpoint_at
12100 && bs->breakpoint_at->disposition == disp_del
12101 && bs->stop)
12102 delete_breakpoint (bs->breakpoint_at);
12103
12104 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12105 {
12106 if (b->disposition == disp_del_at_next_stop)
12107 delete_breakpoint (b);
12108 }
12109 }
12110
12111 /* A comparison function for bp_location AP and BP being interfaced to
12112 qsort. Sort elements primarily by their ADDRESS (no matter what
12113 does breakpoint_address_is_meaningful say for its OWNER),
12114 secondarily by ordering first permanent elements and
12115 terciarily just ensuring the array is sorted stable way despite
12116 qsort being an unstable algorithm. */
12117
12118 static int
12119 bp_locations_compare (const void *ap, const void *bp)
12120 {
12121 const struct bp_location *a = *(const struct bp_location **) ap;
12122 const struct bp_location *b = *(const struct bp_location **) bp;
12123
12124 if (a->address != b->address)
12125 return (a->address > b->address) - (a->address < b->address);
12126
12127 /* Sort locations at the same address by their pspace number, keeping
12128 locations of the same inferior (in a multi-inferior environment)
12129 grouped. */
12130
12131 if (a->pspace->num != b->pspace->num)
12132 return ((a->pspace->num > b->pspace->num)
12133 - (a->pspace->num < b->pspace->num));
12134
12135 /* Sort permanent breakpoints first. */
12136 if (a->permanent != b->permanent)
12137 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12138
12139 /* Make the internal GDB representation stable across GDB runs
12140 where A and B memory inside GDB can differ. Breakpoint locations of
12141 the same type at the same address can be sorted in arbitrary order. */
12142
12143 if (a->owner->number != b->owner->number)
12144 return ((a->owner->number > b->owner->number)
12145 - (a->owner->number < b->owner->number));
12146
12147 return (a > b) - (a < b);
12148 }
12149
12150 /* Set bp_locations_placed_address_before_address_max and
12151 bp_locations_shadow_len_after_address_max according to the current
12152 content of the bp_locations array. */
12153
12154 static void
12155 bp_locations_target_extensions_update (void)
12156 {
12157 struct bp_location *bl, **blp_tmp;
12158
12159 bp_locations_placed_address_before_address_max = 0;
12160 bp_locations_shadow_len_after_address_max = 0;
12161
12162 ALL_BP_LOCATIONS (bl, blp_tmp)
12163 {
12164 CORE_ADDR start, end, addr;
12165
12166 if (!bp_location_has_shadow (bl))
12167 continue;
12168
12169 start = bl->target_info.placed_address;
12170 end = start + bl->target_info.shadow_len;
12171
12172 gdb_assert (bl->address >= start);
12173 addr = bl->address - start;
12174 if (addr > bp_locations_placed_address_before_address_max)
12175 bp_locations_placed_address_before_address_max = addr;
12176
12177 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12178
12179 gdb_assert (bl->address < end);
12180 addr = end - bl->address;
12181 if (addr > bp_locations_shadow_len_after_address_max)
12182 bp_locations_shadow_len_after_address_max = addr;
12183 }
12184 }
12185
12186 /* Download tracepoint locations if they haven't been. */
12187
12188 static void
12189 download_tracepoint_locations (void)
12190 {
12191 struct breakpoint *b;
12192 struct cleanup *old_chain;
12193 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12194
12195 old_chain = save_current_space_and_thread ();
12196
12197 ALL_TRACEPOINTS (b)
12198 {
12199 struct bp_location *bl;
12200 struct tracepoint *t;
12201 int bp_location_downloaded = 0;
12202
12203 if ((b->type == bp_fast_tracepoint
12204 ? !may_insert_fast_tracepoints
12205 : !may_insert_tracepoints))
12206 continue;
12207
12208 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12209 {
12210 if (target_can_download_tracepoint ())
12211 can_download_tracepoint = TRIBOOL_TRUE;
12212 else
12213 can_download_tracepoint = TRIBOOL_FALSE;
12214 }
12215
12216 if (can_download_tracepoint == TRIBOOL_FALSE)
12217 break;
12218
12219 for (bl = b->loc; bl; bl = bl->next)
12220 {
12221 /* In tracepoint, locations are _never_ duplicated, so
12222 should_be_inserted is equivalent to
12223 unduplicated_should_be_inserted. */
12224 if (!should_be_inserted (bl) || bl->inserted)
12225 continue;
12226
12227 switch_to_program_space_and_thread (bl->pspace);
12228
12229 target_download_tracepoint (bl);
12230
12231 bl->inserted = 1;
12232 bp_location_downloaded = 1;
12233 }
12234 t = (struct tracepoint *) b;
12235 t->number_on_target = b->number;
12236 if (bp_location_downloaded)
12237 observer_notify_breakpoint_modified (b);
12238 }
12239
12240 do_cleanups (old_chain);
12241 }
12242
12243 /* Swap the insertion/duplication state between two locations. */
12244
12245 static void
12246 swap_insertion (struct bp_location *left, struct bp_location *right)
12247 {
12248 const int left_inserted = left->inserted;
12249 const int left_duplicate = left->duplicate;
12250 const int left_needs_update = left->needs_update;
12251 const struct bp_target_info left_target_info = left->target_info;
12252
12253 /* Locations of tracepoints can never be duplicated. */
12254 if (is_tracepoint (left->owner))
12255 gdb_assert (!left->duplicate);
12256 if (is_tracepoint (right->owner))
12257 gdb_assert (!right->duplicate);
12258
12259 left->inserted = right->inserted;
12260 left->duplicate = right->duplicate;
12261 left->needs_update = right->needs_update;
12262 left->target_info = right->target_info;
12263 right->inserted = left_inserted;
12264 right->duplicate = left_duplicate;
12265 right->needs_update = left_needs_update;
12266 right->target_info = left_target_info;
12267 }
12268
12269 /* Force the re-insertion of the locations at ADDRESS. This is called
12270 once a new/deleted/modified duplicate location is found and we are evaluating
12271 conditions on the target's side. Such conditions need to be updated on
12272 the target. */
12273
12274 static void
12275 force_breakpoint_reinsertion (struct bp_location *bl)
12276 {
12277 struct bp_location **locp = NULL, **loc2p;
12278 struct bp_location *loc;
12279 CORE_ADDR address = 0;
12280 int pspace_num;
12281
12282 address = bl->address;
12283 pspace_num = bl->pspace->num;
12284
12285 /* This is only meaningful if the target is
12286 evaluating conditions and if the user has
12287 opted for condition evaluation on the target's
12288 side. */
12289 if (gdb_evaluates_breakpoint_condition_p ()
12290 || !target_supports_evaluation_of_breakpoint_conditions ())
12291 return;
12292
12293 /* Flag all breakpoint locations with this address and
12294 the same program space as the location
12295 as "its condition has changed". We need to
12296 update the conditions on the target's side. */
12297 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12298 {
12299 loc = *loc2p;
12300
12301 if (!is_breakpoint (loc->owner)
12302 || pspace_num != loc->pspace->num)
12303 continue;
12304
12305 /* Flag the location appropriately. We use a different state to
12306 let everyone know that we already updated the set of locations
12307 with addr bl->address and program space bl->pspace. This is so
12308 we don't have to keep calling these functions just to mark locations
12309 that have already been marked. */
12310 loc->condition_changed = condition_updated;
12311
12312 /* Free the agent expression bytecode as well. We will compute
12313 it later on. */
12314 loc->cond_bytecode.reset ();
12315 }
12316 }
12317 /* Called whether new breakpoints are created, or existing breakpoints
12318 deleted, to update the global location list and recompute which
12319 locations are duplicate of which.
12320
12321 The INSERT_MODE flag determines whether locations may not, may, or
12322 shall be inserted now. See 'enum ugll_insert_mode' for more
12323 info. */
12324
12325 static void
12326 update_global_location_list (enum ugll_insert_mode insert_mode)
12327 {
12328 struct breakpoint *b;
12329 struct bp_location **locp, *loc;
12330 struct cleanup *cleanups;
12331 /* Last breakpoint location address that was marked for update. */
12332 CORE_ADDR last_addr = 0;
12333 /* Last breakpoint location program space that was marked for update. */
12334 int last_pspace_num = -1;
12335
12336 /* Used in the duplicates detection below. When iterating over all
12337 bp_locations, points to the first bp_location of a given address.
12338 Breakpoints and watchpoints of different types are never
12339 duplicates of each other. Keep one pointer for each type of
12340 breakpoint/watchpoint, so we only need to loop over all locations
12341 once. */
12342 struct bp_location *bp_loc_first; /* breakpoint */
12343 struct bp_location *wp_loc_first; /* hardware watchpoint */
12344 struct bp_location *awp_loc_first; /* access watchpoint */
12345 struct bp_location *rwp_loc_first; /* read watchpoint */
12346
12347 /* Saved former bp_locations array which we compare against the newly
12348 built bp_locations from the current state of ALL_BREAKPOINTS. */
12349 struct bp_location **old_locations, **old_locp;
12350 unsigned old_locations_count;
12351
12352 old_locations = bp_locations;
12353 old_locations_count = bp_locations_count;
12354 bp_locations = NULL;
12355 bp_locations_count = 0;
12356 cleanups = make_cleanup (xfree, old_locations);
12357
12358 ALL_BREAKPOINTS (b)
12359 for (loc = b->loc; loc; loc = loc->next)
12360 bp_locations_count++;
12361
12362 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12363 locp = bp_locations;
12364 ALL_BREAKPOINTS (b)
12365 for (loc = b->loc; loc; loc = loc->next)
12366 *locp++ = loc;
12367 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12368 bp_locations_compare);
12369
12370 bp_locations_target_extensions_update ();
12371
12372 /* Identify bp_location instances that are no longer present in the
12373 new list, and therefore should be freed. Note that it's not
12374 necessary that those locations should be removed from inferior --
12375 if there's another location at the same address (previously
12376 marked as duplicate), we don't need to remove/insert the
12377 location.
12378
12379 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12380 and former bp_location array state respectively. */
12381
12382 locp = bp_locations;
12383 for (old_locp = old_locations;
12384 old_locp < old_locations + old_locations_count;
12385 old_locp++)
12386 {
12387 struct bp_location *old_loc = *old_locp;
12388 struct bp_location **loc2p;
12389
12390 /* Tells if 'old_loc' is found among the new locations. If
12391 not, we have to free it. */
12392 int found_object = 0;
12393 /* Tells if the location should remain inserted in the target. */
12394 int keep_in_target = 0;
12395 int removed = 0;
12396
12397 /* Skip LOCP entries which will definitely never be needed.
12398 Stop either at or being the one matching OLD_LOC. */
12399 while (locp < bp_locations + bp_locations_count
12400 && (*locp)->address < old_loc->address)
12401 locp++;
12402
12403 for (loc2p = locp;
12404 (loc2p < bp_locations + bp_locations_count
12405 && (*loc2p)->address == old_loc->address);
12406 loc2p++)
12407 {
12408 /* Check if this is a new/duplicated location or a duplicated
12409 location that had its condition modified. If so, we want to send
12410 its condition to the target if evaluation of conditions is taking
12411 place there. */
12412 if ((*loc2p)->condition_changed == condition_modified
12413 && (last_addr != old_loc->address
12414 || last_pspace_num != old_loc->pspace->num))
12415 {
12416 force_breakpoint_reinsertion (*loc2p);
12417 last_pspace_num = old_loc->pspace->num;
12418 }
12419
12420 if (*loc2p == old_loc)
12421 found_object = 1;
12422 }
12423
12424 /* We have already handled this address, update it so that we don't
12425 have to go through updates again. */
12426 last_addr = old_loc->address;
12427
12428 /* Target-side condition evaluation: Handle deleted locations. */
12429 if (!found_object)
12430 force_breakpoint_reinsertion (old_loc);
12431
12432 /* If this location is no longer present, and inserted, look if
12433 there's maybe a new location at the same address. If so,
12434 mark that one inserted, and don't remove this one. This is
12435 needed so that we don't have a time window where a breakpoint
12436 at certain location is not inserted. */
12437
12438 if (old_loc->inserted)
12439 {
12440 /* If the location is inserted now, we might have to remove
12441 it. */
12442
12443 if (found_object && should_be_inserted (old_loc))
12444 {
12445 /* The location is still present in the location list,
12446 and still should be inserted. Don't do anything. */
12447 keep_in_target = 1;
12448 }
12449 else
12450 {
12451 /* This location still exists, but it won't be kept in the
12452 target since it may have been disabled. We proceed to
12453 remove its target-side condition. */
12454
12455 /* The location is either no longer present, or got
12456 disabled. See if there's another location at the
12457 same address, in which case we don't need to remove
12458 this one from the target. */
12459
12460 /* OLD_LOC comes from existing struct breakpoint. */
12461 if (breakpoint_address_is_meaningful (old_loc->owner))
12462 {
12463 for (loc2p = locp;
12464 (loc2p < bp_locations + bp_locations_count
12465 && (*loc2p)->address == old_loc->address);
12466 loc2p++)
12467 {
12468 struct bp_location *loc2 = *loc2p;
12469
12470 if (breakpoint_locations_match (loc2, old_loc))
12471 {
12472 /* Read watchpoint locations are switched to
12473 access watchpoints, if the former are not
12474 supported, but the latter are. */
12475 if (is_hardware_watchpoint (old_loc->owner))
12476 {
12477 gdb_assert (is_hardware_watchpoint (loc2->owner));
12478 loc2->watchpoint_type = old_loc->watchpoint_type;
12479 }
12480
12481 /* loc2 is a duplicated location. We need to check
12482 if it should be inserted in case it will be
12483 unduplicated. */
12484 if (loc2 != old_loc
12485 && unduplicated_should_be_inserted (loc2))
12486 {
12487 swap_insertion (old_loc, loc2);
12488 keep_in_target = 1;
12489 break;
12490 }
12491 }
12492 }
12493 }
12494 }
12495
12496 if (!keep_in_target)
12497 {
12498 if (remove_breakpoint (old_loc))
12499 {
12500 /* This is just about all we can do. We could keep
12501 this location on the global list, and try to
12502 remove it next time, but there's no particular
12503 reason why we will succeed next time.
12504
12505 Note that at this point, old_loc->owner is still
12506 valid, as delete_breakpoint frees the breakpoint
12507 only after calling us. */
12508 printf_filtered (_("warning: Error removing "
12509 "breakpoint %d\n"),
12510 old_loc->owner->number);
12511 }
12512 removed = 1;
12513 }
12514 }
12515
12516 if (!found_object)
12517 {
12518 if (removed && target_is_non_stop_p ()
12519 && need_moribund_for_location_type (old_loc))
12520 {
12521 /* This location was removed from the target. In
12522 non-stop mode, a race condition is possible where
12523 we've removed a breakpoint, but stop events for that
12524 breakpoint are already queued and will arrive later.
12525 We apply an heuristic to be able to distinguish such
12526 SIGTRAPs from other random SIGTRAPs: we keep this
12527 breakpoint location for a bit, and will retire it
12528 after we see some number of events. The theory here
12529 is that reporting of events should, "on the average",
12530 be fair, so after a while we'll see events from all
12531 threads that have anything of interest, and no longer
12532 need to keep this breakpoint location around. We
12533 don't hold locations forever so to reduce chances of
12534 mistaking a non-breakpoint SIGTRAP for a breakpoint
12535 SIGTRAP.
12536
12537 The heuristic failing can be disastrous on
12538 decr_pc_after_break targets.
12539
12540 On decr_pc_after_break targets, like e.g., x86-linux,
12541 if we fail to recognize a late breakpoint SIGTRAP,
12542 because events_till_retirement has reached 0 too
12543 soon, we'll fail to do the PC adjustment, and report
12544 a random SIGTRAP to the user. When the user resumes
12545 the inferior, it will most likely immediately crash
12546 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12547 corrupted, because of being resumed e.g., in the
12548 middle of a multi-byte instruction, or skipped a
12549 one-byte instruction. This was actually seen happen
12550 on native x86-linux, and should be less rare on
12551 targets that do not support new thread events, like
12552 remote, due to the heuristic depending on
12553 thread_count.
12554
12555 Mistaking a random SIGTRAP for a breakpoint trap
12556 causes similar symptoms (PC adjustment applied when
12557 it shouldn't), but then again, playing with SIGTRAPs
12558 behind the debugger's back is asking for trouble.
12559
12560 Since hardware watchpoint traps are always
12561 distinguishable from other traps, so we don't need to
12562 apply keep hardware watchpoint moribund locations
12563 around. We simply always ignore hardware watchpoint
12564 traps we can no longer explain. */
12565
12566 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12567 old_loc->owner = NULL;
12568
12569 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12570 }
12571 else
12572 {
12573 old_loc->owner = NULL;
12574 decref_bp_location (&old_loc);
12575 }
12576 }
12577 }
12578
12579 /* Rescan breakpoints at the same address and section, marking the
12580 first one as "first" and any others as "duplicates". This is so
12581 that the bpt instruction is only inserted once. If we have a
12582 permanent breakpoint at the same place as BPT, make that one the
12583 official one, and the rest as duplicates. Permanent breakpoints
12584 are sorted first for the same address.
12585
12586 Do the same for hardware watchpoints, but also considering the
12587 watchpoint's type (regular/access/read) and length. */
12588
12589 bp_loc_first = NULL;
12590 wp_loc_first = NULL;
12591 awp_loc_first = NULL;
12592 rwp_loc_first = NULL;
12593 ALL_BP_LOCATIONS (loc, locp)
12594 {
12595 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12596 non-NULL. */
12597 struct bp_location **loc_first_p;
12598 b = loc->owner;
12599
12600 if (!unduplicated_should_be_inserted (loc)
12601 || !breakpoint_address_is_meaningful (b)
12602 /* Don't detect duplicate for tracepoint locations because they are
12603 never duplicated. See the comments in field `duplicate' of
12604 `struct bp_location'. */
12605 || is_tracepoint (b))
12606 {
12607 /* Clear the condition modification flag. */
12608 loc->condition_changed = condition_unchanged;
12609 continue;
12610 }
12611
12612 if (b->type == bp_hardware_watchpoint)
12613 loc_first_p = &wp_loc_first;
12614 else if (b->type == bp_read_watchpoint)
12615 loc_first_p = &rwp_loc_first;
12616 else if (b->type == bp_access_watchpoint)
12617 loc_first_p = &awp_loc_first;
12618 else
12619 loc_first_p = &bp_loc_first;
12620
12621 if (*loc_first_p == NULL
12622 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12623 || !breakpoint_locations_match (loc, *loc_first_p))
12624 {
12625 *loc_first_p = loc;
12626 loc->duplicate = 0;
12627
12628 if (is_breakpoint (loc->owner) && loc->condition_changed)
12629 {
12630 loc->needs_update = 1;
12631 /* Clear the condition modification flag. */
12632 loc->condition_changed = condition_unchanged;
12633 }
12634 continue;
12635 }
12636
12637
12638 /* This and the above ensure the invariant that the first location
12639 is not duplicated, and is the inserted one.
12640 All following are marked as duplicated, and are not inserted. */
12641 if (loc->inserted)
12642 swap_insertion (loc, *loc_first_p);
12643 loc->duplicate = 1;
12644
12645 /* Clear the condition modification flag. */
12646 loc->condition_changed = condition_unchanged;
12647 }
12648
12649 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12650 {
12651 if (insert_mode != UGLL_DONT_INSERT)
12652 insert_breakpoint_locations ();
12653 else
12654 {
12655 /* Even though the caller told us to not insert new
12656 locations, we may still need to update conditions on the
12657 target's side of breakpoints that were already inserted
12658 if the target is evaluating breakpoint conditions. We
12659 only update conditions for locations that are marked
12660 "needs_update". */
12661 update_inserted_breakpoint_locations ();
12662 }
12663 }
12664
12665 if (insert_mode != UGLL_DONT_INSERT)
12666 download_tracepoint_locations ();
12667
12668 do_cleanups (cleanups);
12669 }
12670
12671 void
12672 breakpoint_retire_moribund (void)
12673 {
12674 struct bp_location *loc;
12675 int ix;
12676
12677 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12678 if (--(loc->events_till_retirement) == 0)
12679 {
12680 decref_bp_location (&loc);
12681 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12682 --ix;
12683 }
12684 }
12685
12686 static void
12687 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12688 {
12689
12690 TRY
12691 {
12692 update_global_location_list (insert_mode);
12693 }
12694 CATCH (e, RETURN_MASK_ERROR)
12695 {
12696 }
12697 END_CATCH
12698 }
12699
12700 /* Clear BKP from a BPS. */
12701
12702 static void
12703 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12704 {
12705 bpstat bs;
12706
12707 for (bs = bps; bs; bs = bs->next)
12708 if (bs->breakpoint_at == bpt)
12709 {
12710 bs->breakpoint_at = NULL;
12711 bs->old_val = NULL;
12712 /* bs->commands will be freed later. */
12713 }
12714 }
12715
12716 /* Callback for iterate_over_threads. */
12717 static int
12718 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12719 {
12720 struct breakpoint *bpt = (struct breakpoint *) data;
12721
12722 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12723 return 0;
12724 }
12725
12726 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12727 callbacks. */
12728
12729 static void
12730 say_where (struct breakpoint *b)
12731 {
12732 struct value_print_options opts;
12733
12734 get_user_print_options (&opts);
12735
12736 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12737 single string. */
12738 if (b->loc == NULL)
12739 {
12740 /* For pending locations, the output differs slightly based
12741 on b->extra_string. If this is non-NULL, it contains either
12742 a condition or dprintf arguments. */
12743 if (b->extra_string == NULL)
12744 {
12745 printf_filtered (_(" (%s) pending."),
12746 event_location_to_string (b->location.get ()));
12747 }
12748 else if (b->type == bp_dprintf)
12749 {
12750 printf_filtered (_(" (%s,%s) pending."),
12751 event_location_to_string (b->location.get ()),
12752 b->extra_string);
12753 }
12754 else
12755 {
12756 printf_filtered (_(" (%s %s) pending."),
12757 event_location_to_string (b->location.get ()),
12758 b->extra_string);
12759 }
12760 }
12761 else
12762 {
12763 if (opts.addressprint || b->loc->symtab == NULL)
12764 {
12765 printf_filtered (" at ");
12766 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12767 gdb_stdout);
12768 }
12769 if (b->loc->symtab != NULL)
12770 {
12771 /* If there is a single location, we can print the location
12772 more nicely. */
12773 if (b->loc->next == NULL)
12774 printf_filtered (": file %s, line %d.",
12775 symtab_to_filename_for_display (b->loc->symtab),
12776 b->loc->line_number);
12777 else
12778 /* This is not ideal, but each location may have a
12779 different file name, and this at least reflects the
12780 real situation somewhat. */
12781 printf_filtered (": %s.",
12782 event_location_to_string (b->location.get ()));
12783 }
12784
12785 if (b->loc->next)
12786 {
12787 struct bp_location *loc = b->loc;
12788 int n = 0;
12789 for (; loc; loc = loc->next)
12790 ++n;
12791 printf_filtered (" (%d locations)", n);
12792 }
12793 }
12794 }
12795
12796 /* Default bp_location_ops methods. */
12797
12798 static void
12799 bp_location_dtor (struct bp_location *self)
12800 {
12801 xfree (self->function_name);
12802 }
12803
12804 static const struct bp_location_ops bp_location_ops =
12805 {
12806 bp_location_dtor
12807 };
12808
12809 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12810 inherit from. */
12811
12812 static void
12813 base_breakpoint_dtor (struct breakpoint *self)
12814 {
12815 decref_counted_command_line (&self->commands);
12816 xfree (self->cond_string);
12817 xfree (self->extra_string);
12818 xfree (self->filter);
12819 }
12820
12821 static struct bp_location *
12822 base_breakpoint_allocate_location (struct breakpoint *self)
12823 {
12824 return new bp_location (&bp_location_ops, self);
12825 }
12826
12827 static void
12828 base_breakpoint_re_set (struct breakpoint *b)
12829 {
12830 /* Nothing to re-set. */
12831 }
12832
12833 #define internal_error_pure_virtual_called() \
12834 gdb_assert_not_reached ("pure virtual function called")
12835
12836 static int
12837 base_breakpoint_insert_location (struct bp_location *bl)
12838 {
12839 internal_error_pure_virtual_called ();
12840 }
12841
12842 static int
12843 base_breakpoint_remove_location (struct bp_location *bl,
12844 enum remove_bp_reason reason)
12845 {
12846 internal_error_pure_virtual_called ();
12847 }
12848
12849 static int
12850 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12851 struct address_space *aspace,
12852 CORE_ADDR bp_addr,
12853 const struct target_waitstatus *ws)
12854 {
12855 internal_error_pure_virtual_called ();
12856 }
12857
12858 static void
12859 base_breakpoint_check_status (bpstat bs)
12860 {
12861 /* Always stop. */
12862 }
12863
12864 /* A "works_in_software_mode" breakpoint_ops method that just internal
12865 errors. */
12866
12867 static int
12868 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12869 {
12870 internal_error_pure_virtual_called ();
12871 }
12872
12873 /* A "resources_needed" breakpoint_ops method that just internal
12874 errors. */
12875
12876 static int
12877 base_breakpoint_resources_needed (const struct bp_location *bl)
12878 {
12879 internal_error_pure_virtual_called ();
12880 }
12881
12882 static enum print_stop_action
12883 base_breakpoint_print_it (bpstat bs)
12884 {
12885 internal_error_pure_virtual_called ();
12886 }
12887
12888 static void
12889 base_breakpoint_print_one_detail (const struct breakpoint *self,
12890 struct ui_out *uiout)
12891 {
12892 /* nothing */
12893 }
12894
12895 static void
12896 base_breakpoint_print_mention (struct breakpoint *b)
12897 {
12898 internal_error_pure_virtual_called ();
12899 }
12900
12901 static void
12902 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12903 {
12904 internal_error_pure_virtual_called ();
12905 }
12906
12907 static void
12908 base_breakpoint_create_sals_from_location
12909 (const struct event_location *location,
12910 struct linespec_result *canonical,
12911 enum bptype type_wanted)
12912 {
12913 internal_error_pure_virtual_called ();
12914 }
12915
12916 static void
12917 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12918 struct linespec_result *c,
12919 char *cond_string,
12920 char *extra_string,
12921 enum bptype type_wanted,
12922 enum bpdisp disposition,
12923 int thread,
12924 int task, int ignore_count,
12925 const struct breakpoint_ops *o,
12926 int from_tty, int enabled,
12927 int internal, unsigned flags)
12928 {
12929 internal_error_pure_virtual_called ();
12930 }
12931
12932 static void
12933 base_breakpoint_decode_location (struct breakpoint *b,
12934 const struct event_location *location,
12935 struct program_space *search_pspace,
12936 struct symtabs_and_lines *sals)
12937 {
12938 internal_error_pure_virtual_called ();
12939 }
12940
12941 /* The default 'explains_signal' method. */
12942
12943 static int
12944 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12945 {
12946 return 1;
12947 }
12948
12949 /* The default "after_condition_true" method. */
12950
12951 static void
12952 base_breakpoint_after_condition_true (struct bpstats *bs)
12953 {
12954 /* Nothing to do. */
12955 }
12956
12957 struct breakpoint_ops base_breakpoint_ops =
12958 {
12959 base_breakpoint_dtor,
12960 base_breakpoint_allocate_location,
12961 base_breakpoint_re_set,
12962 base_breakpoint_insert_location,
12963 base_breakpoint_remove_location,
12964 base_breakpoint_breakpoint_hit,
12965 base_breakpoint_check_status,
12966 base_breakpoint_resources_needed,
12967 base_breakpoint_works_in_software_mode,
12968 base_breakpoint_print_it,
12969 NULL,
12970 base_breakpoint_print_one_detail,
12971 base_breakpoint_print_mention,
12972 base_breakpoint_print_recreate,
12973 base_breakpoint_create_sals_from_location,
12974 base_breakpoint_create_breakpoints_sal,
12975 base_breakpoint_decode_location,
12976 base_breakpoint_explains_signal,
12977 base_breakpoint_after_condition_true,
12978 };
12979
12980 /* Default breakpoint_ops methods. */
12981
12982 static void
12983 bkpt_re_set (struct breakpoint *b)
12984 {
12985 /* FIXME: is this still reachable? */
12986 if (breakpoint_event_location_empty_p (b))
12987 {
12988 /* Anything without a location can't be re-set. */
12989 delete_breakpoint (b);
12990 return;
12991 }
12992
12993 breakpoint_re_set_default (b);
12994 }
12995
12996 static int
12997 bkpt_insert_location (struct bp_location *bl)
12998 {
12999 CORE_ADDR addr = bl->target_info.reqstd_address;
13000
13001 bl->target_info.kind = breakpoint_kind (bl, &addr);
13002 bl->target_info.placed_address = addr;
13003
13004 if (bl->loc_type == bp_loc_hardware_breakpoint)
13005 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13006 else
13007 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13008 }
13009
13010 static int
13011 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13012 {
13013 if (bl->loc_type == bp_loc_hardware_breakpoint)
13014 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13015 else
13016 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13017 }
13018
13019 static int
13020 bkpt_breakpoint_hit (const struct bp_location *bl,
13021 struct address_space *aspace, CORE_ADDR bp_addr,
13022 const struct target_waitstatus *ws)
13023 {
13024 if (ws->kind != TARGET_WAITKIND_STOPPED
13025 || ws->value.sig != GDB_SIGNAL_TRAP)
13026 return 0;
13027
13028 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13029 aspace, bp_addr))
13030 return 0;
13031
13032 if (overlay_debugging /* unmapped overlay section */
13033 && section_is_overlay (bl->section)
13034 && !section_is_mapped (bl->section))
13035 return 0;
13036
13037 return 1;
13038 }
13039
13040 static int
13041 dprintf_breakpoint_hit (const struct bp_location *bl,
13042 struct address_space *aspace, CORE_ADDR bp_addr,
13043 const struct target_waitstatus *ws)
13044 {
13045 if (dprintf_style == dprintf_style_agent
13046 && target_can_run_breakpoint_commands ())
13047 {
13048 /* An agent-style dprintf never causes a stop. If we see a trap
13049 for this address it must be for a breakpoint that happens to
13050 be set at the same address. */
13051 return 0;
13052 }
13053
13054 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13055 }
13056
13057 static int
13058 bkpt_resources_needed (const struct bp_location *bl)
13059 {
13060 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13061
13062 return 1;
13063 }
13064
13065 static enum print_stop_action
13066 bkpt_print_it (bpstat bs)
13067 {
13068 struct breakpoint *b;
13069 const struct bp_location *bl;
13070 int bp_temp;
13071 struct ui_out *uiout = current_uiout;
13072
13073 gdb_assert (bs->bp_location_at != NULL);
13074
13075 bl = bs->bp_location_at;
13076 b = bs->breakpoint_at;
13077
13078 bp_temp = b->disposition == disp_del;
13079 if (bl->address != bl->requested_address)
13080 breakpoint_adjustment_warning (bl->requested_address,
13081 bl->address,
13082 b->number, 1);
13083 annotate_breakpoint (b->number);
13084 maybe_print_thread_hit_breakpoint (uiout);
13085
13086 if (bp_temp)
13087 uiout->text ("Temporary breakpoint ");
13088 else
13089 uiout->text ("Breakpoint ");
13090 if (uiout->is_mi_like_p ())
13091 {
13092 uiout->field_string ("reason",
13093 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13094 uiout->field_string ("disp", bpdisp_text (b->disposition));
13095 }
13096 uiout->field_int ("bkptno", b->number);
13097 uiout->text (", ");
13098
13099 return PRINT_SRC_AND_LOC;
13100 }
13101
13102 static void
13103 bkpt_print_mention (struct breakpoint *b)
13104 {
13105 if (current_uiout->is_mi_like_p ())
13106 return;
13107
13108 switch (b->type)
13109 {
13110 case bp_breakpoint:
13111 case bp_gnu_ifunc_resolver:
13112 if (b->disposition == disp_del)
13113 printf_filtered (_("Temporary breakpoint"));
13114 else
13115 printf_filtered (_("Breakpoint"));
13116 printf_filtered (_(" %d"), b->number);
13117 if (b->type == bp_gnu_ifunc_resolver)
13118 printf_filtered (_(" at gnu-indirect-function resolver"));
13119 break;
13120 case bp_hardware_breakpoint:
13121 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13122 break;
13123 case bp_dprintf:
13124 printf_filtered (_("Dprintf %d"), b->number);
13125 break;
13126 }
13127
13128 say_where (b);
13129 }
13130
13131 static void
13132 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13133 {
13134 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13135 fprintf_unfiltered (fp, "tbreak");
13136 else if (tp->type == bp_breakpoint)
13137 fprintf_unfiltered (fp, "break");
13138 else if (tp->type == bp_hardware_breakpoint
13139 && tp->disposition == disp_del)
13140 fprintf_unfiltered (fp, "thbreak");
13141 else if (tp->type == bp_hardware_breakpoint)
13142 fprintf_unfiltered (fp, "hbreak");
13143 else
13144 internal_error (__FILE__, __LINE__,
13145 _("unhandled breakpoint type %d"), (int) tp->type);
13146
13147 fprintf_unfiltered (fp, " %s",
13148 event_location_to_string (tp->location.get ()));
13149
13150 /* Print out extra_string if this breakpoint is pending. It might
13151 contain, for example, conditions that were set by the user. */
13152 if (tp->loc == NULL && tp->extra_string != NULL)
13153 fprintf_unfiltered (fp, " %s", tp->extra_string);
13154
13155 print_recreate_thread (tp, fp);
13156 }
13157
13158 static void
13159 bkpt_create_sals_from_location (const struct event_location *location,
13160 struct linespec_result *canonical,
13161 enum bptype type_wanted)
13162 {
13163 create_sals_from_location_default (location, canonical, type_wanted);
13164 }
13165
13166 static void
13167 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13168 struct linespec_result *canonical,
13169 char *cond_string,
13170 char *extra_string,
13171 enum bptype type_wanted,
13172 enum bpdisp disposition,
13173 int thread,
13174 int task, int ignore_count,
13175 const struct breakpoint_ops *ops,
13176 int from_tty, int enabled,
13177 int internal, unsigned flags)
13178 {
13179 create_breakpoints_sal_default (gdbarch, canonical,
13180 cond_string, extra_string,
13181 type_wanted,
13182 disposition, thread, task,
13183 ignore_count, ops, from_tty,
13184 enabled, internal, flags);
13185 }
13186
13187 static void
13188 bkpt_decode_location (struct breakpoint *b,
13189 const struct event_location *location,
13190 struct program_space *search_pspace,
13191 struct symtabs_and_lines *sals)
13192 {
13193 decode_location_default (b, location, search_pspace, sals);
13194 }
13195
13196 /* Virtual table for internal breakpoints. */
13197
13198 static void
13199 internal_bkpt_re_set (struct breakpoint *b)
13200 {
13201 switch (b->type)
13202 {
13203 /* Delete overlay event and longjmp master breakpoints; they
13204 will be reset later by breakpoint_re_set. */
13205 case bp_overlay_event:
13206 case bp_longjmp_master:
13207 case bp_std_terminate_master:
13208 case bp_exception_master:
13209 delete_breakpoint (b);
13210 break;
13211
13212 /* This breakpoint is special, it's set up when the inferior
13213 starts and we really don't want to touch it. */
13214 case bp_shlib_event:
13215
13216 /* Like bp_shlib_event, this breakpoint type is special. Once
13217 it is set up, we do not want to touch it. */
13218 case bp_thread_event:
13219 break;
13220 }
13221 }
13222
13223 static void
13224 internal_bkpt_check_status (bpstat bs)
13225 {
13226 if (bs->breakpoint_at->type == bp_shlib_event)
13227 {
13228 /* If requested, stop when the dynamic linker notifies GDB of
13229 events. This allows the user to get control and place
13230 breakpoints in initializer routines for dynamically loaded
13231 objects (among other things). */
13232 bs->stop = stop_on_solib_events;
13233 bs->print = stop_on_solib_events;
13234 }
13235 else
13236 bs->stop = 0;
13237 }
13238
13239 static enum print_stop_action
13240 internal_bkpt_print_it (bpstat bs)
13241 {
13242 struct breakpoint *b;
13243
13244 b = bs->breakpoint_at;
13245
13246 switch (b->type)
13247 {
13248 case bp_shlib_event:
13249 /* Did we stop because the user set the stop_on_solib_events
13250 variable? (If so, we report this as a generic, "Stopped due
13251 to shlib event" message.) */
13252 print_solib_event (0);
13253 break;
13254
13255 case bp_thread_event:
13256 /* Not sure how we will get here.
13257 GDB should not stop for these breakpoints. */
13258 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13259 break;
13260
13261 case bp_overlay_event:
13262 /* By analogy with the thread event, GDB should not stop for these. */
13263 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13264 break;
13265
13266 case bp_longjmp_master:
13267 /* These should never be enabled. */
13268 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13269 break;
13270
13271 case bp_std_terminate_master:
13272 /* These should never be enabled. */
13273 printf_filtered (_("std::terminate Master Breakpoint: "
13274 "gdb should not stop!\n"));
13275 break;
13276
13277 case bp_exception_master:
13278 /* These should never be enabled. */
13279 printf_filtered (_("Exception Master Breakpoint: "
13280 "gdb should not stop!\n"));
13281 break;
13282 }
13283
13284 return PRINT_NOTHING;
13285 }
13286
13287 static void
13288 internal_bkpt_print_mention (struct breakpoint *b)
13289 {
13290 /* Nothing to mention. These breakpoints are internal. */
13291 }
13292
13293 /* Virtual table for momentary breakpoints */
13294
13295 static void
13296 momentary_bkpt_re_set (struct breakpoint *b)
13297 {
13298 /* Keep temporary breakpoints, which can be encountered when we step
13299 over a dlopen call and solib_add is resetting the breakpoints.
13300 Otherwise these should have been blown away via the cleanup chain
13301 or by breakpoint_init_inferior when we rerun the executable. */
13302 }
13303
13304 static void
13305 momentary_bkpt_check_status (bpstat bs)
13306 {
13307 /* Nothing. The point of these breakpoints is causing a stop. */
13308 }
13309
13310 static enum print_stop_action
13311 momentary_bkpt_print_it (bpstat bs)
13312 {
13313 return PRINT_UNKNOWN;
13314 }
13315
13316 static void
13317 momentary_bkpt_print_mention (struct breakpoint *b)
13318 {
13319 /* Nothing to mention. These breakpoints are internal. */
13320 }
13321
13322 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13323
13324 It gets cleared already on the removal of the first one of such placed
13325 breakpoints. This is OK as they get all removed altogether. */
13326
13327 static void
13328 longjmp_bkpt_dtor (struct breakpoint *self)
13329 {
13330 struct thread_info *tp = find_thread_global_id (self->thread);
13331
13332 if (tp)
13333 tp->initiating_frame = null_frame_id;
13334
13335 momentary_breakpoint_ops.dtor (self);
13336 }
13337
13338 /* Specific methods for probe breakpoints. */
13339
13340 static int
13341 bkpt_probe_insert_location (struct bp_location *bl)
13342 {
13343 int v = bkpt_insert_location (bl);
13344
13345 if (v == 0)
13346 {
13347 /* The insertion was successful, now let's set the probe's semaphore
13348 if needed. */
13349 if (bl->probe.probe->pops->set_semaphore != NULL)
13350 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13351 bl->probe.objfile,
13352 bl->gdbarch);
13353 }
13354
13355 return v;
13356 }
13357
13358 static int
13359 bkpt_probe_remove_location (struct bp_location *bl,
13360 enum remove_bp_reason reason)
13361 {
13362 /* Let's clear the semaphore before removing the location. */
13363 if (bl->probe.probe->pops->clear_semaphore != NULL)
13364 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13365 bl->probe.objfile,
13366 bl->gdbarch);
13367
13368 return bkpt_remove_location (bl, reason);
13369 }
13370
13371 static void
13372 bkpt_probe_create_sals_from_location (const struct event_location *location,
13373 struct linespec_result *canonical,
13374 enum bptype type_wanted)
13375 {
13376 struct linespec_sals lsal;
13377
13378 lsal.sals = parse_probes (location, NULL, canonical);
13379 lsal.canonical
13380 = xstrdup (event_location_to_string (canonical->location.get ()));
13381 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13382 }
13383
13384 static void
13385 bkpt_probe_decode_location (struct breakpoint *b,
13386 const struct event_location *location,
13387 struct program_space *search_pspace,
13388 struct symtabs_and_lines *sals)
13389 {
13390 *sals = parse_probes (location, search_pspace, NULL);
13391 if (!sals->sals)
13392 error (_("probe not found"));
13393 }
13394
13395 /* The breakpoint_ops structure to be used in tracepoints. */
13396
13397 static void
13398 tracepoint_re_set (struct breakpoint *b)
13399 {
13400 breakpoint_re_set_default (b);
13401 }
13402
13403 static int
13404 tracepoint_breakpoint_hit (const struct bp_location *bl,
13405 struct address_space *aspace, CORE_ADDR bp_addr,
13406 const struct target_waitstatus *ws)
13407 {
13408 /* By definition, the inferior does not report stops at
13409 tracepoints. */
13410 return 0;
13411 }
13412
13413 static void
13414 tracepoint_print_one_detail (const struct breakpoint *self,
13415 struct ui_out *uiout)
13416 {
13417 struct tracepoint *tp = (struct tracepoint *) self;
13418 if (tp->static_trace_marker_id)
13419 {
13420 gdb_assert (self->type == bp_static_tracepoint);
13421
13422 uiout->text ("\tmarker id is ");
13423 uiout->field_string ("static-tracepoint-marker-string-id",
13424 tp->static_trace_marker_id);
13425 uiout->text ("\n");
13426 }
13427 }
13428
13429 static void
13430 tracepoint_print_mention (struct breakpoint *b)
13431 {
13432 if (current_uiout->is_mi_like_p ())
13433 return;
13434
13435 switch (b->type)
13436 {
13437 case bp_tracepoint:
13438 printf_filtered (_("Tracepoint"));
13439 printf_filtered (_(" %d"), b->number);
13440 break;
13441 case bp_fast_tracepoint:
13442 printf_filtered (_("Fast tracepoint"));
13443 printf_filtered (_(" %d"), b->number);
13444 break;
13445 case bp_static_tracepoint:
13446 printf_filtered (_("Static tracepoint"));
13447 printf_filtered (_(" %d"), b->number);
13448 break;
13449 default:
13450 internal_error (__FILE__, __LINE__,
13451 _("unhandled tracepoint type %d"), (int) b->type);
13452 }
13453
13454 say_where (b);
13455 }
13456
13457 static void
13458 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13459 {
13460 struct tracepoint *tp = (struct tracepoint *) self;
13461
13462 if (self->type == bp_fast_tracepoint)
13463 fprintf_unfiltered (fp, "ftrace");
13464 else if (self->type == bp_static_tracepoint)
13465 fprintf_unfiltered (fp, "strace");
13466 else if (self->type == bp_tracepoint)
13467 fprintf_unfiltered (fp, "trace");
13468 else
13469 internal_error (__FILE__, __LINE__,
13470 _("unhandled tracepoint type %d"), (int) self->type);
13471
13472 fprintf_unfiltered (fp, " %s",
13473 event_location_to_string (self->location.get ()));
13474 print_recreate_thread (self, fp);
13475
13476 if (tp->pass_count)
13477 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13478 }
13479
13480 static void
13481 tracepoint_create_sals_from_location (const struct event_location *location,
13482 struct linespec_result *canonical,
13483 enum bptype type_wanted)
13484 {
13485 create_sals_from_location_default (location, canonical, type_wanted);
13486 }
13487
13488 static void
13489 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13490 struct linespec_result *canonical,
13491 char *cond_string,
13492 char *extra_string,
13493 enum bptype type_wanted,
13494 enum bpdisp disposition,
13495 int thread,
13496 int task, int ignore_count,
13497 const struct breakpoint_ops *ops,
13498 int from_tty, int enabled,
13499 int internal, unsigned flags)
13500 {
13501 create_breakpoints_sal_default (gdbarch, canonical,
13502 cond_string, extra_string,
13503 type_wanted,
13504 disposition, thread, task,
13505 ignore_count, ops, from_tty,
13506 enabled, internal, flags);
13507 }
13508
13509 static void
13510 tracepoint_decode_location (struct breakpoint *b,
13511 const struct event_location *location,
13512 struct program_space *search_pspace,
13513 struct symtabs_and_lines *sals)
13514 {
13515 decode_location_default (b, location, search_pspace, sals);
13516 }
13517
13518 struct breakpoint_ops tracepoint_breakpoint_ops;
13519
13520 /* The breakpoint_ops structure to be use on tracepoints placed in a
13521 static probe. */
13522
13523 static void
13524 tracepoint_probe_create_sals_from_location
13525 (const struct event_location *location,
13526 struct linespec_result *canonical,
13527 enum bptype type_wanted)
13528 {
13529 /* We use the same method for breakpoint on probes. */
13530 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13531 }
13532
13533 static void
13534 tracepoint_probe_decode_location (struct breakpoint *b,
13535 const struct event_location *location,
13536 struct program_space *search_pspace,
13537 struct symtabs_and_lines *sals)
13538 {
13539 /* We use the same method for breakpoint on probes. */
13540 bkpt_probe_decode_location (b, location, search_pspace, sals);
13541 }
13542
13543 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13544
13545 /* Dprintf breakpoint_ops methods. */
13546
13547 static void
13548 dprintf_re_set (struct breakpoint *b)
13549 {
13550 breakpoint_re_set_default (b);
13551
13552 /* extra_string should never be non-NULL for dprintf. */
13553 gdb_assert (b->extra_string != NULL);
13554
13555 /* 1 - connect to target 1, that can run breakpoint commands.
13556 2 - create a dprintf, which resolves fine.
13557 3 - disconnect from target 1
13558 4 - connect to target 2, that can NOT run breakpoint commands.
13559
13560 After steps #3/#4, you'll want the dprintf command list to
13561 be updated, because target 1 and 2 may well return different
13562 answers for target_can_run_breakpoint_commands().
13563 Given absence of finer grained resetting, we get to do
13564 it all the time. */
13565 if (b->extra_string != NULL)
13566 update_dprintf_command_list (b);
13567 }
13568
13569 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13570
13571 static void
13572 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13573 {
13574 fprintf_unfiltered (fp, "dprintf %s,%s",
13575 event_location_to_string (tp->location.get ()),
13576 tp->extra_string);
13577 print_recreate_thread (tp, fp);
13578 }
13579
13580 /* Implement the "after_condition_true" breakpoint_ops method for
13581 dprintf.
13582
13583 dprintf's are implemented with regular commands in their command
13584 list, but we run the commands here instead of before presenting the
13585 stop to the user, as dprintf's don't actually cause a stop. This
13586 also makes it so that the commands of multiple dprintfs at the same
13587 address are all handled. */
13588
13589 static void
13590 dprintf_after_condition_true (struct bpstats *bs)
13591 {
13592 struct cleanup *old_chain;
13593 struct bpstats tmp_bs = { NULL };
13594 struct bpstats *tmp_bs_p = &tmp_bs;
13595
13596 /* dprintf's never cause a stop. This wasn't set in the
13597 check_status hook instead because that would make the dprintf's
13598 condition not be evaluated. */
13599 bs->stop = 0;
13600
13601 /* Run the command list here. Take ownership of it instead of
13602 copying. We never want these commands to run later in
13603 bpstat_do_actions, if a breakpoint that causes a stop happens to
13604 be set at same address as this dprintf, or even if running the
13605 commands here throws. */
13606 tmp_bs.commands = bs->commands;
13607 bs->commands = NULL;
13608 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13609
13610 bpstat_do_actions_1 (&tmp_bs_p);
13611
13612 /* 'tmp_bs.commands' will usually be NULL by now, but
13613 bpstat_do_actions_1 may return early without processing the whole
13614 list. */
13615 do_cleanups (old_chain);
13616 }
13617
13618 /* The breakpoint_ops structure to be used on static tracepoints with
13619 markers (`-m'). */
13620
13621 static void
13622 strace_marker_create_sals_from_location (const struct event_location *location,
13623 struct linespec_result *canonical,
13624 enum bptype type_wanted)
13625 {
13626 struct linespec_sals lsal;
13627 const char *arg_start, *arg;
13628 char *str;
13629 struct cleanup *cleanup;
13630
13631 arg = arg_start = get_linespec_location (location);
13632 lsal.sals = decode_static_tracepoint_spec (&arg);
13633
13634 str = savestring (arg_start, arg - arg_start);
13635 cleanup = make_cleanup (xfree, str);
13636 canonical->location = new_linespec_location (&str);
13637 do_cleanups (cleanup);
13638
13639 lsal.canonical
13640 = xstrdup (event_location_to_string (canonical->location.get ()));
13641 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13642 }
13643
13644 static void
13645 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13646 struct linespec_result *canonical,
13647 char *cond_string,
13648 char *extra_string,
13649 enum bptype type_wanted,
13650 enum bpdisp disposition,
13651 int thread,
13652 int task, int ignore_count,
13653 const struct breakpoint_ops *ops,
13654 int from_tty, int enabled,
13655 int internal, unsigned flags)
13656 {
13657 int i;
13658 struct linespec_sals *lsal = VEC_index (linespec_sals,
13659 canonical->sals, 0);
13660
13661 /* If the user is creating a static tracepoint by marker id
13662 (strace -m MARKER_ID), then store the sals index, so that
13663 breakpoint_re_set can try to match up which of the newly
13664 found markers corresponds to this one, and, don't try to
13665 expand multiple locations for each sal, given than SALS
13666 already should contain all sals for MARKER_ID. */
13667
13668 for (i = 0; i < lsal->sals.nelts; ++i)
13669 {
13670 struct symtabs_and_lines expanded;
13671 struct tracepoint *tp;
13672 event_location_up location;
13673
13674 expanded.nelts = 1;
13675 expanded.sals = &lsal->sals.sals[i];
13676
13677 location = copy_event_location (canonical->location.get ());
13678
13679 tp = new tracepoint ();
13680 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13681 std::move (location), NULL,
13682 cond_string, extra_string,
13683 type_wanted, disposition,
13684 thread, task, ignore_count, ops,
13685 from_tty, enabled, internal, flags,
13686 canonical->special_display);
13687 /* Given that its possible to have multiple markers with
13688 the same string id, if the user is creating a static
13689 tracepoint by marker id ("strace -m MARKER_ID"), then
13690 store the sals index, so that breakpoint_re_set can
13691 try to match up which of the newly found markers
13692 corresponds to this one */
13693 tp->static_trace_marker_id_idx = i;
13694
13695 install_breakpoint (internal, &tp->base, 0);
13696 }
13697 }
13698
13699 static void
13700 strace_marker_decode_location (struct breakpoint *b,
13701 const struct event_location *location,
13702 struct program_space *search_pspace,
13703 struct symtabs_and_lines *sals)
13704 {
13705 struct tracepoint *tp = (struct tracepoint *) b;
13706 const char *s = get_linespec_location (location);
13707
13708 *sals = decode_static_tracepoint_spec (&s);
13709 if (sals->nelts > tp->static_trace_marker_id_idx)
13710 {
13711 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13712 sals->nelts = 1;
13713 }
13714 else
13715 error (_("marker %s not found"), tp->static_trace_marker_id);
13716 }
13717
13718 static struct breakpoint_ops strace_marker_breakpoint_ops;
13719
13720 static int
13721 strace_marker_p (struct breakpoint *b)
13722 {
13723 return b->ops == &strace_marker_breakpoint_ops;
13724 }
13725
13726 /* Delete a breakpoint and clean up all traces of it in the data
13727 structures. */
13728
13729 void
13730 delete_breakpoint (struct breakpoint *bpt)
13731 {
13732 struct breakpoint *b;
13733
13734 gdb_assert (bpt != NULL);
13735
13736 /* Has this bp already been deleted? This can happen because
13737 multiple lists can hold pointers to bp's. bpstat lists are
13738 especial culprits.
13739
13740 One example of this happening is a watchpoint's scope bp. When
13741 the scope bp triggers, we notice that the watchpoint is out of
13742 scope, and delete it. We also delete its scope bp. But the
13743 scope bp is marked "auto-deleting", and is already on a bpstat.
13744 That bpstat is then checked for auto-deleting bp's, which are
13745 deleted.
13746
13747 A real solution to this problem might involve reference counts in
13748 bp's, and/or giving them pointers back to their referencing
13749 bpstat's, and teaching delete_breakpoint to only free a bp's
13750 storage when no more references were extent. A cheaper bandaid
13751 was chosen. */
13752 if (bpt->type == bp_none)
13753 return;
13754
13755 /* At least avoid this stale reference until the reference counting
13756 of breakpoints gets resolved. */
13757 if (bpt->related_breakpoint != bpt)
13758 {
13759 struct breakpoint *related;
13760 struct watchpoint *w;
13761
13762 if (bpt->type == bp_watchpoint_scope)
13763 w = (struct watchpoint *) bpt->related_breakpoint;
13764 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13765 w = (struct watchpoint *) bpt;
13766 else
13767 w = NULL;
13768 if (w != NULL)
13769 watchpoint_del_at_next_stop (w);
13770
13771 /* Unlink bpt from the bpt->related_breakpoint ring. */
13772 for (related = bpt; related->related_breakpoint != bpt;
13773 related = related->related_breakpoint);
13774 related->related_breakpoint = bpt->related_breakpoint;
13775 bpt->related_breakpoint = bpt;
13776 }
13777
13778 /* watch_command_1 creates a watchpoint but only sets its number if
13779 update_watchpoint succeeds in creating its bp_locations. If there's
13780 a problem in that process, we'll be asked to delete the half-created
13781 watchpoint. In that case, don't announce the deletion. */
13782 if (bpt->number)
13783 observer_notify_breakpoint_deleted (bpt);
13784
13785 if (breakpoint_chain == bpt)
13786 breakpoint_chain = bpt->next;
13787
13788 ALL_BREAKPOINTS (b)
13789 if (b->next == bpt)
13790 {
13791 b->next = bpt->next;
13792 break;
13793 }
13794
13795 /* Be sure no bpstat's are pointing at the breakpoint after it's
13796 been freed. */
13797 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13798 in all threads for now. Note that we cannot just remove bpstats
13799 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13800 commands are associated with the bpstat; if we remove it here,
13801 then the later call to bpstat_do_actions (&stop_bpstat); in
13802 event-top.c won't do anything, and temporary breakpoints with
13803 commands won't work. */
13804
13805 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13806
13807 /* Now that breakpoint is removed from breakpoint list, update the
13808 global location list. This will remove locations that used to
13809 belong to this breakpoint. Do this before freeing the breakpoint
13810 itself, since remove_breakpoint looks at location's owner. It
13811 might be better design to have location completely
13812 self-contained, but it's not the case now. */
13813 update_global_location_list (UGLL_DONT_INSERT);
13814
13815 bpt->ops->dtor (bpt);
13816 /* On the chance that someone will soon try again to delete this
13817 same bp, we mark it as deleted before freeing its storage. */
13818 bpt->type = bp_none;
13819 delete bpt;
13820 }
13821
13822 static void
13823 do_delete_breakpoint_cleanup (void *b)
13824 {
13825 delete_breakpoint ((struct breakpoint *) b);
13826 }
13827
13828 struct cleanup *
13829 make_cleanup_delete_breakpoint (struct breakpoint *b)
13830 {
13831 return make_cleanup (do_delete_breakpoint_cleanup, b);
13832 }
13833
13834 /* Iterator function to call a user-provided callback function once
13835 for each of B and its related breakpoints. */
13836
13837 static void
13838 iterate_over_related_breakpoints (struct breakpoint *b,
13839 void (*function) (struct breakpoint *,
13840 void *),
13841 void *data)
13842 {
13843 struct breakpoint *related;
13844
13845 related = b;
13846 do
13847 {
13848 struct breakpoint *next;
13849
13850 /* FUNCTION may delete RELATED. */
13851 next = related->related_breakpoint;
13852
13853 if (next == related)
13854 {
13855 /* RELATED is the last ring entry. */
13856 function (related, data);
13857
13858 /* FUNCTION may have deleted it, so we'd never reach back to
13859 B. There's nothing left to do anyway, so just break
13860 out. */
13861 break;
13862 }
13863 else
13864 function (related, data);
13865
13866 related = next;
13867 }
13868 while (related != b);
13869 }
13870
13871 static void
13872 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13873 {
13874 delete_breakpoint (b);
13875 }
13876
13877 /* A callback for map_breakpoint_numbers that calls
13878 delete_breakpoint. */
13879
13880 static void
13881 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13882 {
13883 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13884 }
13885
13886 void
13887 delete_command (char *arg, int from_tty)
13888 {
13889 struct breakpoint *b, *b_tmp;
13890
13891 dont_repeat ();
13892
13893 if (arg == 0)
13894 {
13895 int breaks_to_delete = 0;
13896
13897 /* Delete all breakpoints if no argument. Do not delete
13898 internal breakpoints, these have to be deleted with an
13899 explicit breakpoint number argument. */
13900 ALL_BREAKPOINTS (b)
13901 if (user_breakpoint_p (b))
13902 {
13903 breaks_to_delete = 1;
13904 break;
13905 }
13906
13907 /* Ask user only if there are some breakpoints to delete. */
13908 if (!from_tty
13909 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13910 {
13911 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13912 if (user_breakpoint_p (b))
13913 delete_breakpoint (b);
13914 }
13915 }
13916 else
13917 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13918 }
13919
13920 /* Return true if all locations of B bound to PSPACE are pending. If
13921 PSPACE is NULL, all locations of all program spaces are
13922 considered. */
13923
13924 static int
13925 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13926 {
13927 struct bp_location *loc;
13928
13929 for (loc = b->loc; loc != NULL; loc = loc->next)
13930 if ((pspace == NULL
13931 || loc->pspace == pspace)
13932 && !loc->shlib_disabled
13933 && !loc->pspace->executing_startup)
13934 return 0;
13935 return 1;
13936 }
13937
13938 /* Subroutine of update_breakpoint_locations to simplify it.
13939 Return non-zero if multiple fns in list LOC have the same name.
13940 Null names are ignored. */
13941
13942 static int
13943 ambiguous_names_p (struct bp_location *loc)
13944 {
13945 struct bp_location *l;
13946 htab_t htab = htab_create_alloc (13, htab_hash_string,
13947 (int (*) (const void *,
13948 const void *)) streq,
13949 NULL, xcalloc, xfree);
13950
13951 for (l = loc; l != NULL; l = l->next)
13952 {
13953 const char **slot;
13954 const char *name = l->function_name;
13955
13956 /* Allow for some names to be NULL, ignore them. */
13957 if (name == NULL)
13958 continue;
13959
13960 slot = (const char **) htab_find_slot (htab, (const void *) name,
13961 INSERT);
13962 /* NOTE: We can assume slot != NULL here because xcalloc never
13963 returns NULL. */
13964 if (*slot != NULL)
13965 {
13966 htab_delete (htab);
13967 return 1;
13968 }
13969 *slot = name;
13970 }
13971
13972 htab_delete (htab);
13973 return 0;
13974 }
13975
13976 /* When symbols change, it probably means the sources changed as well,
13977 and it might mean the static tracepoint markers are no longer at
13978 the same address or line numbers they used to be at last we
13979 checked. Losing your static tracepoints whenever you rebuild is
13980 undesirable. This function tries to resync/rematch gdb static
13981 tracepoints with the markers on the target, for static tracepoints
13982 that have not been set by marker id. Static tracepoint that have
13983 been set by marker id are reset by marker id in breakpoint_re_set.
13984 The heuristic is:
13985
13986 1) For a tracepoint set at a specific address, look for a marker at
13987 the old PC. If one is found there, assume to be the same marker.
13988 If the name / string id of the marker found is different from the
13989 previous known name, assume that means the user renamed the marker
13990 in the sources, and output a warning.
13991
13992 2) For a tracepoint set at a given line number, look for a marker
13993 at the new address of the old line number. If one is found there,
13994 assume to be the same marker. If the name / string id of the
13995 marker found is different from the previous known name, assume that
13996 means the user renamed the marker in the sources, and output a
13997 warning.
13998
13999 3) If a marker is no longer found at the same address or line, it
14000 may mean the marker no longer exists. But it may also just mean
14001 the code changed a bit. Maybe the user added a few lines of code
14002 that made the marker move up or down (in line number terms). Ask
14003 the target for info about the marker with the string id as we knew
14004 it. If found, update line number and address in the matching
14005 static tracepoint. This will get confused if there's more than one
14006 marker with the same ID (possible in UST, although unadvised
14007 precisely because it confuses tools). */
14008
14009 static struct symtab_and_line
14010 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14011 {
14012 struct tracepoint *tp = (struct tracepoint *) b;
14013 struct static_tracepoint_marker marker;
14014 CORE_ADDR pc;
14015
14016 pc = sal.pc;
14017 if (sal.line)
14018 find_line_pc (sal.symtab, sal.line, &pc);
14019
14020 if (target_static_tracepoint_marker_at (pc, &marker))
14021 {
14022 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14023 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14024 b->number,
14025 tp->static_trace_marker_id, marker.str_id);
14026
14027 xfree (tp->static_trace_marker_id);
14028 tp->static_trace_marker_id = xstrdup (marker.str_id);
14029 release_static_tracepoint_marker (&marker);
14030
14031 return sal;
14032 }
14033
14034 /* Old marker wasn't found on target at lineno. Try looking it up
14035 by string ID. */
14036 if (!sal.explicit_pc
14037 && sal.line != 0
14038 && sal.symtab != NULL
14039 && tp->static_trace_marker_id != NULL)
14040 {
14041 VEC(static_tracepoint_marker_p) *markers;
14042
14043 markers
14044 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14045
14046 if (!VEC_empty(static_tracepoint_marker_p, markers))
14047 {
14048 struct symtab_and_line sal2;
14049 struct symbol *sym;
14050 struct static_tracepoint_marker *tpmarker;
14051 struct ui_out *uiout = current_uiout;
14052 struct explicit_location explicit_loc;
14053
14054 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14055
14056 xfree (tp->static_trace_marker_id);
14057 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14058
14059 warning (_("marker for static tracepoint %d (%s) not "
14060 "found at previous line number"),
14061 b->number, tp->static_trace_marker_id);
14062
14063 init_sal (&sal2);
14064
14065 sal2.pc = tpmarker->address;
14066
14067 sal2 = find_pc_line (tpmarker->address, 0);
14068 sym = find_pc_sect_function (tpmarker->address, NULL);
14069 uiout->text ("Now in ");
14070 if (sym)
14071 {
14072 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14073 uiout->text (" at ");
14074 }
14075 uiout->field_string ("file",
14076 symtab_to_filename_for_display (sal2.symtab));
14077 uiout->text (":");
14078
14079 if (uiout->is_mi_like_p ())
14080 {
14081 const char *fullname = symtab_to_fullname (sal2.symtab);
14082
14083 uiout->field_string ("fullname", fullname);
14084 }
14085
14086 uiout->field_int ("line", sal2.line);
14087 uiout->text ("\n");
14088
14089 b->loc->line_number = sal2.line;
14090 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14091
14092 b->location.reset (NULL);
14093 initialize_explicit_location (&explicit_loc);
14094 explicit_loc.source_filename
14095 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14096 explicit_loc.line_offset.offset = b->loc->line_number;
14097 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14098 b->location = new_explicit_location (&explicit_loc);
14099
14100 /* Might be nice to check if function changed, and warn if
14101 so. */
14102
14103 release_static_tracepoint_marker (tpmarker);
14104 }
14105 }
14106 return sal;
14107 }
14108
14109 /* Returns 1 iff locations A and B are sufficiently same that
14110 we don't need to report breakpoint as changed. */
14111
14112 static int
14113 locations_are_equal (struct bp_location *a, struct bp_location *b)
14114 {
14115 while (a && b)
14116 {
14117 if (a->address != b->address)
14118 return 0;
14119
14120 if (a->shlib_disabled != b->shlib_disabled)
14121 return 0;
14122
14123 if (a->enabled != b->enabled)
14124 return 0;
14125
14126 a = a->next;
14127 b = b->next;
14128 }
14129
14130 if ((a == NULL) != (b == NULL))
14131 return 0;
14132
14133 return 1;
14134 }
14135
14136 /* Split all locations of B that are bound to PSPACE out of B's
14137 location list to a separate list and return that list's head. If
14138 PSPACE is NULL, hoist out all locations of B. */
14139
14140 static struct bp_location *
14141 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14142 {
14143 struct bp_location head;
14144 struct bp_location *i = b->loc;
14145 struct bp_location **i_link = &b->loc;
14146 struct bp_location *hoisted = &head;
14147
14148 if (pspace == NULL)
14149 {
14150 i = b->loc;
14151 b->loc = NULL;
14152 return i;
14153 }
14154
14155 head.next = NULL;
14156
14157 while (i != NULL)
14158 {
14159 if (i->pspace == pspace)
14160 {
14161 *i_link = i->next;
14162 i->next = NULL;
14163 hoisted->next = i;
14164 hoisted = i;
14165 }
14166 else
14167 i_link = &i->next;
14168 i = *i_link;
14169 }
14170
14171 return head.next;
14172 }
14173
14174 /* Create new breakpoint locations for B (a hardware or software
14175 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14176 zero, then B is a ranged breakpoint. Only recreates locations for
14177 FILTER_PSPACE. Locations of other program spaces are left
14178 untouched. */
14179
14180 void
14181 update_breakpoint_locations (struct breakpoint *b,
14182 struct program_space *filter_pspace,
14183 struct symtabs_and_lines sals,
14184 struct symtabs_and_lines sals_end)
14185 {
14186 int i;
14187 struct bp_location *existing_locations;
14188
14189 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14190 {
14191 /* Ranged breakpoints have only one start location and one end
14192 location. */
14193 b->enable_state = bp_disabled;
14194 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14195 "multiple locations found\n"),
14196 b->number);
14197 return;
14198 }
14199
14200 /* If there's no new locations, and all existing locations are
14201 pending, don't do anything. This optimizes the common case where
14202 all locations are in the same shared library, that was unloaded.
14203 We'd like to retain the location, so that when the library is
14204 loaded again, we don't loose the enabled/disabled status of the
14205 individual locations. */
14206 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14207 return;
14208
14209 existing_locations = hoist_existing_locations (b, filter_pspace);
14210
14211 for (i = 0; i < sals.nelts; ++i)
14212 {
14213 struct bp_location *new_loc;
14214
14215 switch_to_program_space_and_thread (sals.sals[i].pspace);
14216
14217 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14218
14219 /* Reparse conditions, they might contain references to the
14220 old symtab. */
14221 if (b->cond_string != NULL)
14222 {
14223 const char *s;
14224
14225 s = b->cond_string;
14226 TRY
14227 {
14228 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14229 block_for_pc (sals.sals[i].pc),
14230 0);
14231 }
14232 CATCH (e, RETURN_MASK_ERROR)
14233 {
14234 warning (_("failed to reevaluate condition "
14235 "for breakpoint %d: %s"),
14236 b->number, e.message);
14237 new_loc->enabled = 0;
14238 }
14239 END_CATCH
14240 }
14241
14242 if (sals_end.nelts)
14243 {
14244 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14245
14246 new_loc->length = end - sals.sals[0].pc + 1;
14247 }
14248 }
14249
14250 /* If possible, carry over 'disable' status from existing
14251 breakpoints. */
14252 {
14253 struct bp_location *e = existing_locations;
14254 /* If there are multiple breakpoints with the same function name,
14255 e.g. for inline functions, comparing function names won't work.
14256 Instead compare pc addresses; this is just a heuristic as things
14257 may have moved, but in practice it gives the correct answer
14258 often enough until a better solution is found. */
14259 int have_ambiguous_names = ambiguous_names_p (b->loc);
14260
14261 for (; e; e = e->next)
14262 {
14263 if (!e->enabled && e->function_name)
14264 {
14265 struct bp_location *l = b->loc;
14266 if (have_ambiguous_names)
14267 {
14268 for (; l; l = l->next)
14269 if (breakpoint_locations_match (e, l))
14270 {
14271 l->enabled = 0;
14272 break;
14273 }
14274 }
14275 else
14276 {
14277 for (; l; l = l->next)
14278 if (l->function_name
14279 && strcmp (e->function_name, l->function_name) == 0)
14280 {
14281 l->enabled = 0;
14282 break;
14283 }
14284 }
14285 }
14286 }
14287 }
14288
14289 if (!locations_are_equal (existing_locations, b->loc))
14290 observer_notify_breakpoint_modified (b);
14291 }
14292
14293 /* Find the SaL locations corresponding to the given LOCATION.
14294 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14295
14296 static struct symtabs_and_lines
14297 location_to_sals (struct breakpoint *b, struct event_location *location,
14298 struct program_space *search_pspace, int *found)
14299 {
14300 struct symtabs_and_lines sals = {0};
14301 struct gdb_exception exception = exception_none;
14302
14303 gdb_assert (b->ops != NULL);
14304
14305 TRY
14306 {
14307 b->ops->decode_location (b, location, search_pspace, &sals);
14308 }
14309 CATCH (e, RETURN_MASK_ERROR)
14310 {
14311 int not_found_and_ok = 0;
14312
14313 exception = e;
14314
14315 /* For pending breakpoints, it's expected that parsing will
14316 fail until the right shared library is loaded. User has
14317 already told to create pending breakpoints and don't need
14318 extra messages. If breakpoint is in bp_shlib_disabled
14319 state, then user already saw the message about that
14320 breakpoint being disabled, and don't want to see more
14321 errors. */
14322 if (e.error == NOT_FOUND_ERROR
14323 && (b->condition_not_parsed
14324 || (b->loc != NULL
14325 && search_pspace != NULL
14326 && b->loc->pspace != search_pspace)
14327 || (b->loc && b->loc->shlib_disabled)
14328 || (b->loc && b->loc->pspace->executing_startup)
14329 || b->enable_state == bp_disabled))
14330 not_found_and_ok = 1;
14331
14332 if (!not_found_and_ok)
14333 {
14334 /* We surely don't want to warn about the same breakpoint
14335 10 times. One solution, implemented here, is disable
14336 the breakpoint on error. Another solution would be to
14337 have separate 'warning emitted' flag. Since this
14338 happens only when a binary has changed, I don't know
14339 which approach is better. */
14340 b->enable_state = bp_disabled;
14341 throw_exception (e);
14342 }
14343 }
14344 END_CATCH
14345
14346 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14347 {
14348 int i;
14349
14350 for (i = 0; i < sals.nelts; ++i)
14351 resolve_sal_pc (&sals.sals[i]);
14352 if (b->condition_not_parsed && b->extra_string != NULL)
14353 {
14354 char *cond_string, *extra_string;
14355 int thread, task;
14356
14357 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14358 &cond_string, &thread, &task,
14359 &extra_string);
14360 gdb_assert (b->cond_string == NULL);
14361 if (cond_string)
14362 b->cond_string = cond_string;
14363 b->thread = thread;
14364 b->task = task;
14365 if (extra_string)
14366 {
14367 xfree (b->extra_string);
14368 b->extra_string = extra_string;
14369 }
14370 b->condition_not_parsed = 0;
14371 }
14372
14373 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14374 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14375
14376 *found = 1;
14377 }
14378 else
14379 *found = 0;
14380
14381 return sals;
14382 }
14383
14384 /* The default re_set method, for typical hardware or software
14385 breakpoints. Reevaluate the breakpoint and recreate its
14386 locations. */
14387
14388 static void
14389 breakpoint_re_set_default (struct breakpoint *b)
14390 {
14391 int found;
14392 struct symtabs_and_lines sals, sals_end;
14393 struct symtabs_and_lines expanded = {0};
14394 struct symtabs_and_lines expanded_end = {0};
14395 struct program_space *filter_pspace = current_program_space;
14396
14397 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14398 if (found)
14399 {
14400 make_cleanup (xfree, sals.sals);
14401 expanded = sals;
14402 }
14403
14404 if (b->location_range_end != NULL)
14405 {
14406 sals_end = location_to_sals (b, b->location_range_end.get (),
14407 filter_pspace, &found);
14408 if (found)
14409 {
14410 make_cleanup (xfree, sals_end.sals);
14411 expanded_end = sals_end;
14412 }
14413 }
14414
14415 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14416 }
14417
14418 /* Default method for creating SALs from an address string. It basically
14419 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14420
14421 static void
14422 create_sals_from_location_default (const struct event_location *location,
14423 struct linespec_result *canonical,
14424 enum bptype type_wanted)
14425 {
14426 parse_breakpoint_sals (location, canonical);
14427 }
14428
14429 /* Call create_breakpoints_sal for the given arguments. This is the default
14430 function for the `create_breakpoints_sal' method of
14431 breakpoint_ops. */
14432
14433 static void
14434 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14435 struct linespec_result *canonical,
14436 char *cond_string,
14437 char *extra_string,
14438 enum bptype type_wanted,
14439 enum bpdisp disposition,
14440 int thread,
14441 int task, int ignore_count,
14442 const struct breakpoint_ops *ops,
14443 int from_tty, int enabled,
14444 int internal, unsigned flags)
14445 {
14446 create_breakpoints_sal (gdbarch, canonical, cond_string,
14447 extra_string,
14448 type_wanted, disposition,
14449 thread, task, ignore_count, ops, from_tty,
14450 enabled, internal, flags);
14451 }
14452
14453 /* Decode the line represented by S by calling decode_line_full. This is the
14454 default function for the `decode_location' method of breakpoint_ops. */
14455
14456 static void
14457 decode_location_default (struct breakpoint *b,
14458 const struct event_location *location,
14459 struct program_space *search_pspace,
14460 struct symtabs_and_lines *sals)
14461 {
14462 struct linespec_result canonical;
14463
14464 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14465 (struct symtab *) NULL, 0,
14466 &canonical, multiple_symbols_all,
14467 b->filter);
14468
14469 /* We should get 0 or 1 resulting SALs. */
14470 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14471
14472 if (VEC_length (linespec_sals, canonical.sals) > 0)
14473 {
14474 struct linespec_sals *lsal;
14475
14476 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14477 *sals = lsal->sals;
14478 /* Arrange it so the destructor does not free the
14479 contents. */
14480 lsal->sals.sals = NULL;
14481 }
14482 }
14483
14484 /* Prepare the global context for a re-set of breakpoint B. */
14485
14486 static struct cleanup *
14487 prepare_re_set_context (struct breakpoint *b)
14488 {
14489 input_radix = b->input_radix;
14490 set_language (b->language);
14491
14492 return make_cleanup (null_cleanup, NULL);
14493 }
14494
14495 /* Reset a breakpoint given it's struct breakpoint * BINT.
14496 The value we return ends up being the return value from catch_errors.
14497 Unused in this case. */
14498
14499 static int
14500 breakpoint_re_set_one (void *bint)
14501 {
14502 /* Get past catch_errs. */
14503 struct breakpoint *b = (struct breakpoint *) bint;
14504 struct cleanup *cleanups;
14505
14506 cleanups = prepare_re_set_context (b);
14507 b->ops->re_set (b);
14508 do_cleanups (cleanups);
14509 return 0;
14510 }
14511
14512 /* Re-set breakpoint locations for the current program space.
14513 Locations bound to other program spaces are left untouched. */
14514
14515 void
14516 breakpoint_re_set (void)
14517 {
14518 struct breakpoint *b, *b_tmp;
14519 enum language save_language;
14520 int save_input_radix;
14521 struct cleanup *old_chain;
14522
14523 save_language = current_language->la_language;
14524 save_input_radix = input_radix;
14525 old_chain = save_current_space_and_thread ();
14526
14527 /* Note: we must not try to insert locations until after all
14528 breakpoints have been re-set. Otherwise, e.g., when re-setting
14529 breakpoint 1, we'd insert the locations of breakpoint 2, which
14530 hadn't been re-set yet, and thus may have stale locations. */
14531
14532 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14533 {
14534 /* Format possible error msg. */
14535 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14536 b->number);
14537 struct cleanup *cleanups = make_cleanup (xfree, message);
14538 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14539 do_cleanups (cleanups);
14540 }
14541 set_language (save_language);
14542 input_radix = save_input_radix;
14543
14544 jit_breakpoint_re_set ();
14545
14546 do_cleanups (old_chain);
14547
14548 create_overlay_event_breakpoint ();
14549 create_longjmp_master_breakpoint ();
14550 create_std_terminate_master_breakpoint ();
14551 create_exception_master_breakpoint ();
14552
14553 /* Now we can insert. */
14554 update_global_location_list (UGLL_MAY_INSERT);
14555 }
14556 \f
14557 /* Reset the thread number of this breakpoint:
14558
14559 - If the breakpoint is for all threads, leave it as-is.
14560 - Else, reset it to the current thread for inferior_ptid. */
14561 void
14562 breakpoint_re_set_thread (struct breakpoint *b)
14563 {
14564 if (b->thread != -1)
14565 {
14566 if (in_thread_list (inferior_ptid))
14567 b->thread = ptid_to_global_thread_id (inferior_ptid);
14568
14569 /* We're being called after following a fork. The new fork is
14570 selected as current, and unless this was a vfork will have a
14571 different program space from the original thread. Reset that
14572 as well. */
14573 b->loc->pspace = current_program_space;
14574 }
14575 }
14576
14577 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14578 If from_tty is nonzero, it prints a message to that effect,
14579 which ends with a period (no newline). */
14580
14581 void
14582 set_ignore_count (int bptnum, int count, int from_tty)
14583 {
14584 struct breakpoint *b;
14585
14586 if (count < 0)
14587 count = 0;
14588
14589 ALL_BREAKPOINTS (b)
14590 if (b->number == bptnum)
14591 {
14592 if (is_tracepoint (b))
14593 {
14594 if (from_tty && count != 0)
14595 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14596 bptnum);
14597 return;
14598 }
14599
14600 b->ignore_count = count;
14601 if (from_tty)
14602 {
14603 if (count == 0)
14604 printf_filtered (_("Will stop next time "
14605 "breakpoint %d is reached."),
14606 bptnum);
14607 else if (count == 1)
14608 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14609 bptnum);
14610 else
14611 printf_filtered (_("Will ignore next %d "
14612 "crossings of breakpoint %d."),
14613 count, bptnum);
14614 }
14615 observer_notify_breakpoint_modified (b);
14616 return;
14617 }
14618
14619 error (_("No breakpoint number %d."), bptnum);
14620 }
14621
14622 /* Command to set ignore-count of breakpoint N to COUNT. */
14623
14624 static void
14625 ignore_command (char *args, int from_tty)
14626 {
14627 char *p = args;
14628 int num;
14629
14630 if (p == 0)
14631 error_no_arg (_("a breakpoint number"));
14632
14633 num = get_number (&p);
14634 if (num == 0)
14635 error (_("bad breakpoint number: '%s'"), args);
14636 if (*p == 0)
14637 error (_("Second argument (specified ignore-count) is missing."));
14638
14639 set_ignore_count (num,
14640 longest_to_int (value_as_long (parse_and_eval (p))),
14641 from_tty);
14642 if (from_tty)
14643 printf_filtered ("\n");
14644 }
14645 \f
14646 /* Call FUNCTION on each of the breakpoints
14647 whose numbers are given in ARGS. */
14648
14649 static void
14650 map_breakpoint_numbers (const char *args,
14651 void (*function) (struct breakpoint *,
14652 void *),
14653 void *data)
14654 {
14655 int num;
14656 struct breakpoint *b, *tmp;
14657
14658 if (args == 0 || *args == '\0')
14659 error_no_arg (_("one or more breakpoint numbers"));
14660
14661 number_or_range_parser parser (args);
14662
14663 while (!parser.finished ())
14664 {
14665 const char *p = parser.cur_tok ();
14666 bool match = false;
14667
14668 num = parser.get_number ();
14669 if (num == 0)
14670 {
14671 warning (_("bad breakpoint number at or near '%s'"), p);
14672 }
14673 else
14674 {
14675 ALL_BREAKPOINTS_SAFE (b, tmp)
14676 if (b->number == num)
14677 {
14678 match = true;
14679 function (b, data);
14680 break;
14681 }
14682 if (!match)
14683 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14684 }
14685 }
14686 }
14687
14688 static struct bp_location *
14689 find_location_by_number (char *number)
14690 {
14691 char *dot = strchr (number, '.');
14692 char *p1;
14693 int bp_num;
14694 int loc_num;
14695 struct breakpoint *b;
14696 struct bp_location *loc;
14697
14698 *dot = '\0';
14699
14700 p1 = number;
14701 bp_num = get_number (&p1);
14702 if (bp_num == 0)
14703 error (_("Bad breakpoint number '%s'"), number);
14704
14705 ALL_BREAKPOINTS (b)
14706 if (b->number == bp_num)
14707 {
14708 break;
14709 }
14710
14711 if (!b || b->number != bp_num)
14712 error (_("Bad breakpoint number '%s'"), number);
14713
14714 p1 = dot+1;
14715 loc_num = get_number (&p1);
14716 if (loc_num == 0)
14717 error (_("Bad breakpoint location number '%s'"), number);
14718
14719 --loc_num;
14720 loc = b->loc;
14721 for (;loc_num && loc; --loc_num, loc = loc->next)
14722 ;
14723 if (!loc)
14724 error (_("Bad breakpoint location number '%s'"), dot+1);
14725
14726 return loc;
14727 }
14728
14729
14730 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14731 If from_tty is nonzero, it prints a message to that effect,
14732 which ends with a period (no newline). */
14733
14734 void
14735 disable_breakpoint (struct breakpoint *bpt)
14736 {
14737 /* Never disable a watchpoint scope breakpoint; we want to
14738 hit them when we leave scope so we can delete both the
14739 watchpoint and its scope breakpoint at that time. */
14740 if (bpt->type == bp_watchpoint_scope)
14741 return;
14742
14743 bpt->enable_state = bp_disabled;
14744
14745 /* Mark breakpoint locations modified. */
14746 mark_breakpoint_modified (bpt);
14747
14748 if (target_supports_enable_disable_tracepoint ()
14749 && current_trace_status ()->running && is_tracepoint (bpt))
14750 {
14751 struct bp_location *location;
14752
14753 for (location = bpt->loc; location; location = location->next)
14754 target_disable_tracepoint (location);
14755 }
14756
14757 update_global_location_list (UGLL_DONT_INSERT);
14758
14759 observer_notify_breakpoint_modified (bpt);
14760 }
14761
14762 /* A callback for iterate_over_related_breakpoints. */
14763
14764 static void
14765 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14766 {
14767 disable_breakpoint (b);
14768 }
14769
14770 /* A callback for map_breakpoint_numbers that calls
14771 disable_breakpoint. */
14772
14773 static void
14774 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14775 {
14776 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14777 }
14778
14779 static void
14780 disable_command (char *args, int from_tty)
14781 {
14782 if (args == 0)
14783 {
14784 struct breakpoint *bpt;
14785
14786 ALL_BREAKPOINTS (bpt)
14787 if (user_breakpoint_p (bpt))
14788 disable_breakpoint (bpt);
14789 }
14790 else
14791 {
14792 char *num = extract_arg (&args);
14793
14794 while (num)
14795 {
14796 if (strchr (num, '.'))
14797 {
14798 struct bp_location *loc = find_location_by_number (num);
14799
14800 if (loc)
14801 {
14802 if (loc->enabled)
14803 {
14804 loc->enabled = 0;
14805 mark_breakpoint_location_modified (loc);
14806 }
14807 if (target_supports_enable_disable_tracepoint ()
14808 && current_trace_status ()->running && loc->owner
14809 && is_tracepoint (loc->owner))
14810 target_disable_tracepoint (loc);
14811 }
14812 update_global_location_list (UGLL_DONT_INSERT);
14813 }
14814 else
14815 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14816 num = extract_arg (&args);
14817 }
14818 }
14819 }
14820
14821 static void
14822 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14823 int count)
14824 {
14825 int target_resources_ok;
14826
14827 if (bpt->type == bp_hardware_breakpoint)
14828 {
14829 int i;
14830 i = hw_breakpoint_used_count ();
14831 target_resources_ok =
14832 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14833 i + 1, 0);
14834 if (target_resources_ok == 0)
14835 error (_("No hardware breakpoint support in the target."));
14836 else if (target_resources_ok < 0)
14837 error (_("Hardware breakpoints used exceeds limit."));
14838 }
14839
14840 if (is_watchpoint (bpt))
14841 {
14842 /* Initialize it just to avoid a GCC false warning. */
14843 enum enable_state orig_enable_state = bp_disabled;
14844
14845 TRY
14846 {
14847 struct watchpoint *w = (struct watchpoint *) bpt;
14848
14849 orig_enable_state = bpt->enable_state;
14850 bpt->enable_state = bp_enabled;
14851 update_watchpoint (w, 1 /* reparse */);
14852 }
14853 CATCH (e, RETURN_MASK_ALL)
14854 {
14855 bpt->enable_state = orig_enable_state;
14856 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14857 bpt->number);
14858 return;
14859 }
14860 END_CATCH
14861 }
14862
14863 bpt->enable_state = bp_enabled;
14864
14865 /* Mark breakpoint locations modified. */
14866 mark_breakpoint_modified (bpt);
14867
14868 if (target_supports_enable_disable_tracepoint ()
14869 && current_trace_status ()->running && is_tracepoint (bpt))
14870 {
14871 struct bp_location *location;
14872
14873 for (location = bpt->loc; location; location = location->next)
14874 target_enable_tracepoint (location);
14875 }
14876
14877 bpt->disposition = disposition;
14878 bpt->enable_count = count;
14879 update_global_location_list (UGLL_MAY_INSERT);
14880
14881 observer_notify_breakpoint_modified (bpt);
14882 }
14883
14884
14885 void
14886 enable_breakpoint (struct breakpoint *bpt)
14887 {
14888 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14889 }
14890
14891 static void
14892 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14893 {
14894 enable_breakpoint (bpt);
14895 }
14896
14897 /* A callback for map_breakpoint_numbers that calls
14898 enable_breakpoint. */
14899
14900 static void
14901 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14902 {
14903 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14904 }
14905
14906 /* The enable command enables the specified breakpoints (or all defined
14907 breakpoints) so they once again become (or continue to be) effective
14908 in stopping the inferior. */
14909
14910 static void
14911 enable_command (char *args, int from_tty)
14912 {
14913 if (args == 0)
14914 {
14915 struct breakpoint *bpt;
14916
14917 ALL_BREAKPOINTS (bpt)
14918 if (user_breakpoint_p (bpt))
14919 enable_breakpoint (bpt);
14920 }
14921 else
14922 {
14923 char *num = extract_arg (&args);
14924
14925 while (num)
14926 {
14927 if (strchr (num, '.'))
14928 {
14929 struct bp_location *loc = find_location_by_number (num);
14930
14931 if (loc)
14932 {
14933 if (!loc->enabled)
14934 {
14935 loc->enabled = 1;
14936 mark_breakpoint_location_modified (loc);
14937 }
14938 if (target_supports_enable_disable_tracepoint ()
14939 && current_trace_status ()->running && loc->owner
14940 && is_tracepoint (loc->owner))
14941 target_enable_tracepoint (loc);
14942 }
14943 update_global_location_list (UGLL_MAY_INSERT);
14944 }
14945 else
14946 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14947 num = extract_arg (&args);
14948 }
14949 }
14950 }
14951
14952 /* This struct packages up disposition data for application to multiple
14953 breakpoints. */
14954
14955 struct disp_data
14956 {
14957 enum bpdisp disp;
14958 int count;
14959 };
14960
14961 static void
14962 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14963 {
14964 struct disp_data disp_data = *(struct disp_data *) arg;
14965
14966 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14967 }
14968
14969 static void
14970 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14971 {
14972 struct disp_data disp = { disp_disable, 1 };
14973
14974 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14975 }
14976
14977 static void
14978 enable_once_command (char *args, int from_tty)
14979 {
14980 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14981 }
14982
14983 static void
14984 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14985 {
14986 struct disp_data disp = { disp_disable, *(int *) countptr };
14987
14988 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14989 }
14990
14991 static void
14992 enable_count_command (char *args, int from_tty)
14993 {
14994 int count;
14995
14996 if (args == NULL)
14997 error_no_arg (_("hit count"));
14998
14999 count = get_number (&args);
15000
15001 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15002 }
15003
15004 static void
15005 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15006 {
15007 struct disp_data disp = { disp_del, 1 };
15008
15009 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15010 }
15011
15012 static void
15013 enable_delete_command (char *args, int from_tty)
15014 {
15015 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15016 }
15017 \f
15018 static void
15019 set_breakpoint_cmd (char *args, int from_tty)
15020 {
15021 }
15022
15023 static void
15024 show_breakpoint_cmd (char *args, int from_tty)
15025 {
15026 }
15027
15028 /* Invalidate last known value of any hardware watchpoint if
15029 the memory which that value represents has been written to by
15030 GDB itself. */
15031
15032 static void
15033 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15034 CORE_ADDR addr, ssize_t len,
15035 const bfd_byte *data)
15036 {
15037 struct breakpoint *bp;
15038
15039 ALL_BREAKPOINTS (bp)
15040 if (bp->enable_state == bp_enabled
15041 && bp->type == bp_hardware_watchpoint)
15042 {
15043 struct watchpoint *wp = (struct watchpoint *) bp;
15044
15045 if (wp->val_valid && wp->val)
15046 {
15047 struct bp_location *loc;
15048
15049 for (loc = bp->loc; loc != NULL; loc = loc->next)
15050 if (loc->loc_type == bp_loc_hardware_watchpoint
15051 && loc->address + loc->length > addr
15052 && addr + len > loc->address)
15053 {
15054 value_free (wp->val);
15055 wp->val = NULL;
15056 wp->val_valid = 0;
15057 }
15058 }
15059 }
15060 }
15061
15062 /* Create and insert a breakpoint for software single step. */
15063
15064 void
15065 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15066 struct address_space *aspace,
15067 CORE_ADDR next_pc)
15068 {
15069 struct thread_info *tp = inferior_thread ();
15070 struct symtab_and_line sal;
15071 CORE_ADDR pc = next_pc;
15072
15073 if (tp->control.single_step_breakpoints == NULL)
15074 {
15075 tp->control.single_step_breakpoints
15076 = new_single_step_breakpoint (tp->global_num, gdbarch);
15077 }
15078
15079 sal = find_pc_line (pc, 0);
15080 sal.pc = pc;
15081 sal.section = find_pc_overlay (pc);
15082 sal.explicit_pc = 1;
15083 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15084
15085 update_global_location_list (UGLL_INSERT);
15086 }
15087
15088 /* Insert single step breakpoints according to the current state. */
15089
15090 int
15091 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15092 {
15093 struct regcache *regcache = get_current_regcache ();
15094 VEC (CORE_ADDR) * next_pcs;
15095
15096 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15097
15098 if (next_pcs != NULL)
15099 {
15100 int i;
15101 CORE_ADDR pc;
15102 struct frame_info *frame = get_current_frame ();
15103 struct address_space *aspace = get_frame_address_space (frame);
15104
15105 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15106 insert_single_step_breakpoint (gdbarch, aspace, pc);
15107
15108 VEC_free (CORE_ADDR, next_pcs);
15109
15110 return 1;
15111 }
15112 else
15113 return 0;
15114 }
15115
15116 /* See breakpoint.h. */
15117
15118 int
15119 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15120 struct address_space *aspace,
15121 CORE_ADDR pc)
15122 {
15123 struct bp_location *loc;
15124
15125 for (loc = bp->loc; loc != NULL; loc = loc->next)
15126 if (loc->inserted
15127 && breakpoint_location_address_match (loc, aspace, pc))
15128 return 1;
15129
15130 return 0;
15131 }
15132
15133 /* Check whether a software single-step breakpoint is inserted at
15134 PC. */
15135
15136 int
15137 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15138 CORE_ADDR pc)
15139 {
15140 struct breakpoint *bpt;
15141
15142 ALL_BREAKPOINTS (bpt)
15143 {
15144 if (bpt->type == bp_single_step
15145 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15146 return 1;
15147 }
15148 return 0;
15149 }
15150
15151 /* Tracepoint-specific operations. */
15152
15153 /* Set tracepoint count to NUM. */
15154 static void
15155 set_tracepoint_count (int num)
15156 {
15157 tracepoint_count = num;
15158 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15159 }
15160
15161 static void
15162 trace_command (char *arg, int from_tty)
15163 {
15164 struct breakpoint_ops *ops;
15165
15166 event_location_up location = string_to_event_location (&arg,
15167 current_language);
15168 if (location != NULL
15169 && event_location_type (location.get ()) == PROBE_LOCATION)
15170 ops = &tracepoint_probe_breakpoint_ops;
15171 else
15172 ops = &tracepoint_breakpoint_ops;
15173
15174 create_breakpoint (get_current_arch (),
15175 location.get (),
15176 NULL, 0, arg, 1 /* parse arg */,
15177 0 /* tempflag */,
15178 bp_tracepoint /* type_wanted */,
15179 0 /* Ignore count */,
15180 pending_break_support,
15181 ops,
15182 from_tty,
15183 1 /* enabled */,
15184 0 /* internal */, 0);
15185 }
15186
15187 static void
15188 ftrace_command (char *arg, int from_tty)
15189 {
15190 event_location_up location = string_to_event_location (&arg,
15191 current_language);
15192 create_breakpoint (get_current_arch (),
15193 location.get (),
15194 NULL, 0, arg, 1 /* parse arg */,
15195 0 /* tempflag */,
15196 bp_fast_tracepoint /* type_wanted */,
15197 0 /* Ignore count */,
15198 pending_break_support,
15199 &tracepoint_breakpoint_ops,
15200 from_tty,
15201 1 /* enabled */,
15202 0 /* internal */, 0);
15203 }
15204
15205 /* strace command implementation. Creates a static tracepoint. */
15206
15207 static void
15208 strace_command (char *arg, int from_tty)
15209 {
15210 struct breakpoint_ops *ops;
15211 event_location_up location;
15212 struct cleanup *back_to;
15213
15214 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15215 or with a normal static tracepoint. */
15216 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15217 {
15218 ops = &strace_marker_breakpoint_ops;
15219 location = new_linespec_location (&arg);
15220 }
15221 else
15222 {
15223 ops = &tracepoint_breakpoint_ops;
15224 location = string_to_event_location (&arg, current_language);
15225 }
15226
15227 create_breakpoint (get_current_arch (),
15228 location.get (),
15229 NULL, 0, arg, 1 /* parse arg */,
15230 0 /* tempflag */,
15231 bp_static_tracepoint /* type_wanted */,
15232 0 /* Ignore count */,
15233 pending_break_support,
15234 ops,
15235 from_tty,
15236 1 /* enabled */,
15237 0 /* internal */, 0);
15238 }
15239
15240 /* Set up a fake reader function that gets command lines from a linked
15241 list that was acquired during tracepoint uploading. */
15242
15243 static struct uploaded_tp *this_utp;
15244 static int next_cmd;
15245
15246 static char *
15247 read_uploaded_action (void)
15248 {
15249 char *rslt;
15250
15251 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15252
15253 next_cmd++;
15254
15255 return rslt;
15256 }
15257
15258 /* Given information about a tracepoint as recorded on a target (which
15259 can be either a live system or a trace file), attempt to create an
15260 equivalent GDB tracepoint. This is not a reliable process, since
15261 the target does not necessarily have all the information used when
15262 the tracepoint was originally defined. */
15263
15264 struct tracepoint *
15265 create_tracepoint_from_upload (struct uploaded_tp *utp)
15266 {
15267 char *addr_str, small_buf[100];
15268 struct tracepoint *tp;
15269
15270 if (utp->at_string)
15271 addr_str = utp->at_string;
15272 else
15273 {
15274 /* In the absence of a source location, fall back to raw
15275 address. Since there is no way to confirm that the address
15276 means the same thing as when the trace was started, warn the
15277 user. */
15278 warning (_("Uploaded tracepoint %d has no "
15279 "source location, using raw address"),
15280 utp->number);
15281 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15282 addr_str = small_buf;
15283 }
15284
15285 /* There's not much we can do with a sequence of bytecodes. */
15286 if (utp->cond && !utp->cond_string)
15287 warning (_("Uploaded tracepoint %d condition "
15288 "has no source form, ignoring it"),
15289 utp->number);
15290
15291 event_location_up location = string_to_event_location (&addr_str,
15292 current_language);
15293 if (!create_breakpoint (get_current_arch (),
15294 location.get (),
15295 utp->cond_string, -1, addr_str,
15296 0 /* parse cond/thread */,
15297 0 /* tempflag */,
15298 utp->type /* type_wanted */,
15299 0 /* Ignore count */,
15300 pending_break_support,
15301 &tracepoint_breakpoint_ops,
15302 0 /* from_tty */,
15303 utp->enabled /* enabled */,
15304 0 /* internal */,
15305 CREATE_BREAKPOINT_FLAGS_INSERTED))
15306 return NULL;
15307
15308 /* Get the tracepoint we just created. */
15309 tp = get_tracepoint (tracepoint_count);
15310 gdb_assert (tp != NULL);
15311
15312 if (utp->pass > 0)
15313 {
15314 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15315 tp->base.number);
15316
15317 trace_pass_command (small_buf, 0);
15318 }
15319
15320 /* If we have uploaded versions of the original commands, set up a
15321 special-purpose "reader" function and call the usual command line
15322 reader, then pass the result to the breakpoint command-setting
15323 function. */
15324 if (!VEC_empty (char_ptr, utp->cmd_strings))
15325 {
15326 command_line_up cmd_list;
15327
15328 this_utp = utp;
15329 next_cmd = 0;
15330
15331 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15332
15333 breakpoint_set_commands (&tp->base, std::move (cmd_list));
15334 }
15335 else if (!VEC_empty (char_ptr, utp->actions)
15336 || !VEC_empty (char_ptr, utp->step_actions))
15337 warning (_("Uploaded tracepoint %d actions "
15338 "have no source form, ignoring them"),
15339 utp->number);
15340
15341 /* Copy any status information that might be available. */
15342 tp->base.hit_count = utp->hit_count;
15343 tp->traceframe_usage = utp->traceframe_usage;
15344
15345 return tp;
15346 }
15347
15348 /* Print information on tracepoint number TPNUM_EXP, or all if
15349 omitted. */
15350
15351 static void
15352 tracepoints_info (char *args, int from_tty)
15353 {
15354 struct ui_out *uiout = current_uiout;
15355 int num_printed;
15356
15357 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15358
15359 if (num_printed == 0)
15360 {
15361 if (args == NULL || *args == '\0')
15362 uiout->message ("No tracepoints.\n");
15363 else
15364 uiout->message ("No tracepoint matching '%s'.\n", args);
15365 }
15366
15367 default_collect_info ();
15368 }
15369
15370 /* The 'enable trace' command enables tracepoints.
15371 Not supported by all targets. */
15372 static void
15373 enable_trace_command (char *args, int from_tty)
15374 {
15375 enable_command (args, from_tty);
15376 }
15377
15378 /* The 'disable trace' command disables tracepoints.
15379 Not supported by all targets. */
15380 static void
15381 disable_trace_command (char *args, int from_tty)
15382 {
15383 disable_command (args, from_tty);
15384 }
15385
15386 /* Remove a tracepoint (or all if no argument). */
15387 static void
15388 delete_trace_command (char *arg, int from_tty)
15389 {
15390 struct breakpoint *b, *b_tmp;
15391
15392 dont_repeat ();
15393
15394 if (arg == 0)
15395 {
15396 int breaks_to_delete = 0;
15397
15398 /* Delete all breakpoints if no argument.
15399 Do not delete internal or call-dummy breakpoints, these
15400 have to be deleted with an explicit breakpoint number
15401 argument. */
15402 ALL_TRACEPOINTS (b)
15403 if (is_tracepoint (b) && user_breakpoint_p (b))
15404 {
15405 breaks_to_delete = 1;
15406 break;
15407 }
15408
15409 /* Ask user only if there are some breakpoints to delete. */
15410 if (!from_tty
15411 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15412 {
15413 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15414 if (is_tracepoint (b) && user_breakpoint_p (b))
15415 delete_breakpoint (b);
15416 }
15417 }
15418 else
15419 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15420 }
15421
15422 /* Helper function for trace_pass_command. */
15423
15424 static void
15425 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15426 {
15427 tp->pass_count = count;
15428 observer_notify_breakpoint_modified (&tp->base);
15429 if (from_tty)
15430 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15431 tp->base.number, count);
15432 }
15433
15434 /* Set passcount for tracepoint.
15435
15436 First command argument is passcount, second is tracepoint number.
15437 If tracepoint number omitted, apply to most recently defined.
15438 Also accepts special argument "all". */
15439
15440 static void
15441 trace_pass_command (char *args, int from_tty)
15442 {
15443 struct tracepoint *t1;
15444 unsigned int count;
15445
15446 if (args == 0 || *args == 0)
15447 error (_("passcount command requires an "
15448 "argument (count + optional TP num)"));
15449
15450 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15451
15452 args = skip_spaces (args);
15453 if (*args && strncasecmp (args, "all", 3) == 0)
15454 {
15455 struct breakpoint *b;
15456
15457 args += 3; /* Skip special argument "all". */
15458 if (*args)
15459 error (_("Junk at end of arguments."));
15460
15461 ALL_TRACEPOINTS (b)
15462 {
15463 t1 = (struct tracepoint *) b;
15464 trace_pass_set_count (t1, count, from_tty);
15465 }
15466 }
15467 else if (*args == '\0')
15468 {
15469 t1 = get_tracepoint_by_number (&args, NULL);
15470 if (t1)
15471 trace_pass_set_count (t1, count, from_tty);
15472 }
15473 else
15474 {
15475 number_or_range_parser parser (args);
15476 while (!parser.finished ())
15477 {
15478 t1 = get_tracepoint_by_number (&args, &parser);
15479 if (t1)
15480 trace_pass_set_count (t1, count, from_tty);
15481 }
15482 }
15483 }
15484
15485 struct tracepoint *
15486 get_tracepoint (int num)
15487 {
15488 struct breakpoint *t;
15489
15490 ALL_TRACEPOINTS (t)
15491 if (t->number == num)
15492 return (struct tracepoint *) t;
15493
15494 return NULL;
15495 }
15496
15497 /* Find the tracepoint with the given target-side number (which may be
15498 different from the tracepoint number after disconnecting and
15499 reconnecting). */
15500
15501 struct tracepoint *
15502 get_tracepoint_by_number_on_target (int num)
15503 {
15504 struct breakpoint *b;
15505
15506 ALL_TRACEPOINTS (b)
15507 {
15508 struct tracepoint *t = (struct tracepoint *) b;
15509
15510 if (t->number_on_target == num)
15511 return t;
15512 }
15513
15514 return NULL;
15515 }
15516
15517 /* Utility: parse a tracepoint number and look it up in the list.
15518 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15519 If the argument is missing, the most recent tracepoint
15520 (tracepoint_count) is returned. */
15521
15522 struct tracepoint *
15523 get_tracepoint_by_number (char **arg,
15524 number_or_range_parser *parser)
15525 {
15526 struct breakpoint *t;
15527 int tpnum;
15528 char *instring = arg == NULL ? NULL : *arg;
15529
15530 if (parser != NULL)
15531 {
15532 gdb_assert (!parser->finished ());
15533 tpnum = parser->get_number ();
15534 }
15535 else if (arg == NULL || *arg == NULL || ! **arg)
15536 tpnum = tracepoint_count;
15537 else
15538 tpnum = get_number (arg);
15539
15540 if (tpnum <= 0)
15541 {
15542 if (instring && *instring)
15543 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15544 instring);
15545 else
15546 printf_filtered (_("No previous tracepoint\n"));
15547 return NULL;
15548 }
15549
15550 ALL_TRACEPOINTS (t)
15551 if (t->number == tpnum)
15552 {
15553 return (struct tracepoint *) t;
15554 }
15555
15556 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15557 return NULL;
15558 }
15559
15560 void
15561 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15562 {
15563 if (b->thread != -1)
15564 fprintf_unfiltered (fp, " thread %d", b->thread);
15565
15566 if (b->task != 0)
15567 fprintf_unfiltered (fp, " task %d", b->task);
15568
15569 fprintf_unfiltered (fp, "\n");
15570 }
15571
15572 /* Save information on user settable breakpoints (watchpoints, etc) to
15573 a new script file named FILENAME. If FILTER is non-NULL, call it
15574 on each breakpoint and only include the ones for which it returns
15575 non-zero. */
15576
15577 static void
15578 save_breakpoints (char *filename, int from_tty,
15579 int (*filter) (const struct breakpoint *))
15580 {
15581 struct breakpoint *tp;
15582 int any = 0;
15583 struct cleanup *cleanup;
15584 int extra_trace_bits = 0;
15585
15586 if (filename == 0 || *filename == 0)
15587 error (_("Argument required (file name in which to save)"));
15588
15589 /* See if we have anything to save. */
15590 ALL_BREAKPOINTS (tp)
15591 {
15592 /* Skip internal and momentary breakpoints. */
15593 if (!user_breakpoint_p (tp))
15594 continue;
15595
15596 /* If we have a filter, only save the breakpoints it accepts. */
15597 if (filter && !filter (tp))
15598 continue;
15599
15600 any = 1;
15601
15602 if (is_tracepoint (tp))
15603 {
15604 extra_trace_bits = 1;
15605
15606 /* We can stop searching. */
15607 break;
15608 }
15609 }
15610
15611 if (!any)
15612 {
15613 warning (_("Nothing to save."));
15614 return;
15615 }
15616
15617 filename = tilde_expand (filename);
15618 cleanup = make_cleanup (xfree, filename);
15619
15620 stdio_file fp;
15621
15622 if (!fp.open (filename, "w"))
15623 error (_("Unable to open file '%s' for saving (%s)"),
15624 filename, safe_strerror (errno));
15625
15626 if (extra_trace_bits)
15627 save_trace_state_variables (&fp);
15628
15629 ALL_BREAKPOINTS (tp)
15630 {
15631 /* Skip internal and momentary breakpoints. */
15632 if (!user_breakpoint_p (tp))
15633 continue;
15634
15635 /* If we have a filter, only save the breakpoints it accepts. */
15636 if (filter && !filter (tp))
15637 continue;
15638
15639 tp->ops->print_recreate (tp, &fp);
15640
15641 /* Note, we can't rely on tp->number for anything, as we can't
15642 assume the recreated breakpoint numbers will match. Use $bpnum
15643 instead. */
15644
15645 if (tp->cond_string)
15646 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15647
15648 if (tp->ignore_count)
15649 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15650
15651 if (tp->type != bp_dprintf && tp->commands)
15652 {
15653 fp.puts (" commands\n");
15654
15655 current_uiout->redirect (&fp);
15656 TRY
15657 {
15658 print_command_lines (current_uiout, tp->commands->commands, 2);
15659 }
15660 CATCH (ex, RETURN_MASK_ALL)
15661 {
15662 current_uiout->redirect (NULL);
15663 throw_exception (ex);
15664 }
15665 END_CATCH
15666
15667 current_uiout->redirect (NULL);
15668 fp.puts (" end\n");
15669 }
15670
15671 if (tp->enable_state == bp_disabled)
15672 fp.puts ("disable $bpnum\n");
15673
15674 /* If this is a multi-location breakpoint, check if the locations
15675 should be individually disabled. Watchpoint locations are
15676 special, and not user visible. */
15677 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15678 {
15679 struct bp_location *loc;
15680 int n = 1;
15681
15682 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15683 if (!loc->enabled)
15684 fp.printf ("disable $bpnum.%d\n", n);
15685 }
15686 }
15687
15688 if (extra_trace_bits && *default_collect)
15689 fp.printf ("set default-collect %s\n", default_collect);
15690
15691 if (from_tty)
15692 printf_filtered (_("Saved to file '%s'.\n"), filename);
15693 do_cleanups (cleanup);
15694 }
15695
15696 /* The `save breakpoints' command. */
15697
15698 static void
15699 save_breakpoints_command (char *args, int from_tty)
15700 {
15701 save_breakpoints (args, from_tty, NULL);
15702 }
15703
15704 /* The `save tracepoints' command. */
15705
15706 static void
15707 save_tracepoints_command (char *args, int from_tty)
15708 {
15709 save_breakpoints (args, from_tty, is_tracepoint);
15710 }
15711
15712 /* Create a vector of all tracepoints. */
15713
15714 VEC(breakpoint_p) *
15715 all_tracepoints (void)
15716 {
15717 VEC(breakpoint_p) *tp_vec = 0;
15718 struct breakpoint *tp;
15719
15720 ALL_TRACEPOINTS (tp)
15721 {
15722 VEC_safe_push (breakpoint_p, tp_vec, tp);
15723 }
15724
15725 return tp_vec;
15726 }
15727
15728 \f
15729 /* This help string is used to consolidate all the help string for specifying
15730 locations used by several commands. */
15731
15732 #define LOCATION_HELP_STRING \
15733 "Linespecs are colon-separated lists of location parameters, such as\n\
15734 source filename, function name, label name, and line number.\n\
15735 Example: To specify the start of a label named \"the_top\" in the\n\
15736 function \"fact\" in the file \"factorial.c\", use\n\
15737 \"factorial.c:fact:the_top\".\n\
15738 \n\
15739 Address locations begin with \"*\" and specify an exact address in the\n\
15740 program. Example: To specify the fourth byte past the start function\n\
15741 \"main\", use \"*main + 4\".\n\
15742 \n\
15743 Explicit locations are similar to linespecs but use an option/argument\n\
15744 syntax to specify location parameters.\n\
15745 Example: To specify the start of the label named \"the_top\" in the\n\
15746 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15747 -function fact -label the_top\".\n"
15748
15749 /* This help string is used for the break, hbreak, tbreak and thbreak
15750 commands. It is defined as a macro to prevent duplication.
15751 COMMAND should be a string constant containing the name of the
15752 command. */
15753
15754 #define BREAK_ARGS_HELP(command) \
15755 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15756 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15757 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15758 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15759 `-probe-dtrace' (for a DTrace probe).\n\
15760 LOCATION may be a linespec, address, or explicit location as described\n\
15761 below.\n\
15762 \n\
15763 With no LOCATION, uses current execution address of the selected\n\
15764 stack frame. This is useful for breaking on return to a stack frame.\n\
15765 \n\
15766 THREADNUM is the number from \"info threads\".\n\
15767 CONDITION is a boolean expression.\n\
15768 \n" LOCATION_HELP_STRING "\n\
15769 Multiple breakpoints at one place are permitted, and useful if their\n\
15770 conditions are different.\n\
15771 \n\
15772 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15773
15774 /* List of subcommands for "catch". */
15775 static struct cmd_list_element *catch_cmdlist;
15776
15777 /* List of subcommands for "tcatch". */
15778 static struct cmd_list_element *tcatch_cmdlist;
15779
15780 void
15781 add_catch_command (const char *name, const char *docstring,
15782 cmd_sfunc_ftype *sfunc,
15783 completer_ftype *completer,
15784 void *user_data_catch,
15785 void *user_data_tcatch)
15786 {
15787 struct cmd_list_element *command;
15788
15789 command = add_cmd (name, class_breakpoint, NULL, docstring,
15790 &catch_cmdlist);
15791 set_cmd_sfunc (command, sfunc);
15792 set_cmd_context (command, user_data_catch);
15793 set_cmd_completer (command, completer);
15794
15795 command = add_cmd (name, class_breakpoint, NULL, docstring,
15796 &tcatch_cmdlist);
15797 set_cmd_sfunc (command, sfunc);
15798 set_cmd_context (command, user_data_tcatch);
15799 set_cmd_completer (command, completer);
15800 }
15801
15802 static void
15803 save_command (char *arg, int from_tty)
15804 {
15805 printf_unfiltered (_("\"save\" must be followed by "
15806 "the name of a save subcommand.\n"));
15807 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15808 }
15809
15810 struct breakpoint *
15811 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15812 void *data)
15813 {
15814 struct breakpoint *b, *b_tmp;
15815
15816 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15817 {
15818 if ((*callback) (b, data))
15819 return b;
15820 }
15821
15822 return NULL;
15823 }
15824
15825 /* Zero if any of the breakpoint's locations could be a location where
15826 functions have been inlined, nonzero otherwise. */
15827
15828 static int
15829 is_non_inline_function (struct breakpoint *b)
15830 {
15831 /* The shared library event breakpoint is set on the address of a
15832 non-inline function. */
15833 if (b->type == bp_shlib_event)
15834 return 1;
15835
15836 return 0;
15837 }
15838
15839 /* Nonzero if the specified PC cannot be a location where functions
15840 have been inlined. */
15841
15842 int
15843 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15844 const struct target_waitstatus *ws)
15845 {
15846 struct breakpoint *b;
15847 struct bp_location *bl;
15848
15849 ALL_BREAKPOINTS (b)
15850 {
15851 if (!is_non_inline_function (b))
15852 continue;
15853
15854 for (bl = b->loc; bl != NULL; bl = bl->next)
15855 {
15856 if (!bl->shlib_disabled
15857 && bpstat_check_location (bl, aspace, pc, ws))
15858 return 1;
15859 }
15860 }
15861
15862 return 0;
15863 }
15864
15865 /* Remove any references to OBJFILE which is going to be freed. */
15866
15867 void
15868 breakpoint_free_objfile (struct objfile *objfile)
15869 {
15870 struct bp_location **locp, *loc;
15871
15872 ALL_BP_LOCATIONS (loc, locp)
15873 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15874 loc->symtab = NULL;
15875 }
15876
15877 void
15878 initialize_breakpoint_ops (void)
15879 {
15880 static int initialized = 0;
15881
15882 struct breakpoint_ops *ops;
15883
15884 if (initialized)
15885 return;
15886 initialized = 1;
15887
15888 /* The breakpoint_ops structure to be inherit by all kinds of
15889 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15890 internal and momentary breakpoints, etc.). */
15891 ops = &bkpt_base_breakpoint_ops;
15892 *ops = base_breakpoint_ops;
15893 ops->re_set = bkpt_re_set;
15894 ops->insert_location = bkpt_insert_location;
15895 ops->remove_location = bkpt_remove_location;
15896 ops->breakpoint_hit = bkpt_breakpoint_hit;
15897 ops->create_sals_from_location = bkpt_create_sals_from_location;
15898 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15899 ops->decode_location = bkpt_decode_location;
15900
15901 /* The breakpoint_ops structure to be used in regular breakpoints. */
15902 ops = &bkpt_breakpoint_ops;
15903 *ops = bkpt_base_breakpoint_ops;
15904 ops->re_set = bkpt_re_set;
15905 ops->resources_needed = bkpt_resources_needed;
15906 ops->print_it = bkpt_print_it;
15907 ops->print_mention = bkpt_print_mention;
15908 ops->print_recreate = bkpt_print_recreate;
15909
15910 /* Ranged breakpoints. */
15911 ops = &ranged_breakpoint_ops;
15912 *ops = bkpt_breakpoint_ops;
15913 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15914 ops->resources_needed = resources_needed_ranged_breakpoint;
15915 ops->print_it = print_it_ranged_breakpoint;
15916 ops->print_one = print_one_ranged_breakpoint;
15917 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15918 ops->print_mention = print_mention_ranged_breakpoint;
15919 ops->print_recreate = print_recreate_ranged_breakpoint;
15920
15921 /* Internal breakpoints. */
15922 ops = &internal_breakpoint_ops;
15923 *ops = bkpt_base_breakpoint_ops;
15924 ops->re_set = internal_bkpt_re_set;
15925 ops->check_status = internal_bkpt_check_status;
15926 ops->print_it = internal_bkpt_print_it;
15927 ops->print_mention = internal_bkpt_print_mention;
15928
15929 /* Momentary breakpoints. */
15930 ops = &momentary_breakpoint_ops;
15931 *ops = bkpt_base_breakpoint_ops;
15932 ops->re_set = momentary_bkpt_re_set;
15933 ops->check_status = momentary_bkpt_check_status;
15934 ops->print_it = momentary_bkpt_print_it;
15935 ops->print_mention = momentary_bkpt_print_mention;
15936
15937 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15938 ops = &longjmp_breakpoint_ops;
15939 *ops = momentary_breakpoint_ops;
15940 ops->dtor = longjmp_bkpt_dtor;
15941
15942 /* Probe breakpoints. */
15943 ops = &bkpt_probe_breakpoint_ops;
15944 *ops = bkpt_breakpoint_ops;
15945 ops->insert_location = bkpt_probe_insert_location;
15946 ops->remove_location = bkpt_probe_remove_location;
15947 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15948 ops->decode_location = bkpt_probe_decode_location;
15949
15950 /* Watchpoints. */
15951 ops = &watchpoint_breakpoint_ops;
15952 *ops = base_breakpoint_ops;
15953 ops->dtor = dtor_watchpoint;
15954 ops->re_set = re_set_watchpoint;
15955 ops->insert_location = insert_watchpoint;
15956 ops->remove_location = remove_watchpoint;
15957 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15958 ops->check_status = check_status_watchpoint;
15959 ops->resources_needed = resources_needed_watchpoint;
15960 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15961 ops->print_it = print_it_watchpoint;
15962 ops->print_mention = print_mention_watchpoint;
15963 ops->print_recreate = print_recreate_watchpoint;
15964 ops->explains_signal = explains_signal_watchpoint;
15965
15966 /* Masked watchpoints. */
15967 ops = &masked_watchpoint_breakpoint_ops;
15968 *ops = watchpoint_breakpoint_ops;
15969 ops->insert_location = insert_masked_watchpoint;
15970 ops->remove_location = remove_masked_watchpoint;
15971 ops->resources_needed = resources_needed_masked_watchpoint;
15972 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15973 ops->print_it = print_it_masked_watchpoint;
15974 ops->print_one_detail = print_one_detail_masked_watchpoint;
15975 ops->print_mention = print_mention_masked_watchpoint;
15976 ops->print_recreate = print_recreate_masked_watchpoint;
15977
15978 /* Tracepoints. */
15979 ops = &tracepoint_breakpoint_ops;
15980 *ops = base_breakpoint_ops;
15981 ops->re_set = tracepoint_re_set;
15982 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15983 ops->print_one_detail = tracepoint_print_one_detail;
15984 ops->print_mention = tracepoint_print_mention;
15985 ops->print_recreate = tracepoint_print_recreate;
15986 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15987 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15988 ops->decode_location = tracepoint_decode_location;
15989
15990 /* Probe tracepoints. */
15991 ops = &tracepoint_probe_breakpoint_ops;
15992 *ops = tracepoint_breakpoint_ops;
15993 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15994 ops->decode_location = tracepoint_probe_decode_location;
15995
15996 /* Static tracepoints with marker (`-m'). */
15997 ops = &strace_marker_breakpoint_ops;
15998 *ops = tracepoint_breakpoint_ops;
15999 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16000 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16001 ops->decode_location = strace_marker_decode_location;
16002
16003 /* Fork catchpoints. */
16004 ops = &catch_fork_breakpoint_ops;
16005 *ops = base_breakpoint_ops;
16006 ops->insert_location = insert_catch_fork;
16007 ops->remove_location = remove_catch_fork;
16008 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16009 ops->print_it = print_it_catch_fork;
16010 ops->print_one = print_one_catch_fork;
16011 ops->print_mention = print_mention_catch_fork;
16012 ops->print_recreate = print_recreate_catch_fork;
16013
16014 /* Vfork catchpoints. */
16015 ops = &catch_vfork_breakpoint_ops;
16016 *ops = base_breakpoint_ops;
16017 ops->insert_location = insert_catch_vfork;
16018 ops->remove_location = remove_catch_vfork;
16019 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16020 ops->print_it = print_it_catch_vfork;
16021 ops->print_one = print_one_catch_vfork;
16022 ops->print_mention = print_mention_catch_vfork;
16023 ops->print_recreate = print_recreate_catch_vfork;
16024
16025 /* Exec catchpoints. */
16026 ops = &catch_exec_breakpoint_ops;
16027 *ops = base_breakpoint_ops;
16028 ops->dtor = dtor_catch_exec;
16029 ops->insert_location = insert_catch_exec;
16030 ops->remove_location = remove_catch_exec;
16031 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16032 ops->print_it = print_it_catch_exec;
16033 ops->print_one = print_one_catch_exec;
16034 ops->print_mention = print_mention_catch_exec;
16035 ops->print_recreate = print_recreate_catch_exec;
16036
16037 /* Solib-related catchpoints. */
16038 ops = &catch_solib_breakpoint_ops;
16039 *ops = base_breakpoint_ops;
16040 ops->dtor = dtor_catch_solib;
16041 ops->insert_location = insert_catch_solib;
16042 ops->remove_location = remove_catch_solib;
16043 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16044 ops->check_status = check_status_catch_solib;
16045 ops->print_it = print_it_catch_solib;
16046 ops->print_one = print_one_catch_solib;
16047 ops->print_mention = print_mention_catch_solib;
16048 ops->print_recreate = print_recreate_catch_solib;
16049
16050 ops = &dprintf_breakpoint_ops;
16051 *ops = bkpt_base_breakpoint_ops;
16052 ops->re_set = dprintf_re_set;
16053 ops->resources_needed = bkpt_resources_needed;
16054 ops->print_it = bkpt_print_it;
16055 ops->print_mention = bkpt_print_mention;
16056 ops->print_recreate = dprintf_print_recreate;
16057 ops->after_condition_true = dprintf_after_condition_true;
16058 ops->breakpoint_hit = dprintf_breakpoint_hit;
16059 }
16060
16061 /* Chain containing all defined "enable breakpoint" subcommands. */
16062
16063 static struct cmd_list_element *enablebreaklist = NULL;
16064
16065 void
16066 _initialize_breakpoint (void)
16067 {
16068 struct cmd_list_element *c;
16069
16070 initialize_breakpoint_ops ();
16071
16072 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16073 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16074 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16075
16076 breakpoint_objfile_key
16077 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16078
16079 breakpoint_chain = 0;
16080 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16081 before a breakpoint is set. */
16082 breakpoint_count = 0;
16083
16084 tracepoint_count = 0;
16085
16086 add_com ("ignore", class_breakpoint, ignore_command, _("\
16087 Set ignore-count of breakpoint number N to COUNT.\n\
16088 Usage is `ignore N COUNT'."));
16089
16090 add_com ("commands", class_breakpoint, commands_command, _("\
16091 Set commands to be executed when the given breakpoints are hit.\n\
16092 Give a space-separated breakpoint list as argument after \"commands\".\n\
16093 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16094 (e.g. `5-7').\n\
16095 With no argument, the targeted breakpoint is the last one set.\n\
16096 The commands themselves follow starting on the next line.\n\
16097 Type a line containing \"end\" to indicate the end of them.\n\
16098 Give \"silent\" as the first line to make the breakpoint silent;\n\
16099 then no output is printed when it is hit, except what the commands print."));
16100
16101 c = add_com ("condition", class_breakpoint, condition_command, _("\
16102 Specify breakpoint number N to break only if COND is true.\n\
16103 Usage is `condition N COND', where N is an integer and COND is an\n\
16104 expression to be evaluated whenever breakpoint N is reached."));
16105 set_cmd_completer (c, condition_completer);
16106
16107 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16108 Set a temporary breakpoint.\n\
16109 Like \"break\" except the breakpoint is only temporary,\n\
16110 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16111 by using \"enable delete\" on the breakpoint number.\n\
16112 \n"
16113 BREAK_ARGS_HELP ("tbreak")));
16114 set_cmd_completer (c, location_completer);
16115
16116 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16117 Set a hardware assisted breakpoint.\n\
16118 Like \"break\" except the breakpoint requires hardware support,\n\
16119 some target hardware may not have this support.\n\
16120 \n"
16121 BREAK_ARGS_HELP ("hbreak")));
16122 set_cmd_completer (c, location_completer);
16123
16124 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16125 Set a temporary hardware assisted breakpoint.\n\
16126 Like \"hbreak\" except the breakpoint is only temporary,\n\
16127 so it will be deleted when hit.\n\
16128 \n"
16129 BREAK_ARGS_HELP ("thbreak")));
16130 set_cmd_completer (c, location_completer);
16131
16132 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16133 Enable some breakpoints.\n\
16134 Give breakpoint numbers (separated by spaces) as arguments.\n\
16135 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16136 This is used to cancel the effect of the \"disable\" command.\n\
16137 With a subcommand you can enable temporarily."),
16138 &enablelist, "enable ", 1, &cmdlist);
16139
16140 add_com_alias ("en", "enable", class_breakpoint, 1);
16141
16142 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16143 Enable some breakpoints.\n\
16144 Give breakpoint numbers (separated by spaces) as arguments.\n\
16145 This is used to cancel the effect of the \"disable\" command.\n\
16146 May be abbreviated to simply \"enable\".\n"),
16147 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16148
16149 add_cmd ("once", no_class, enable_once_command, _("\
16150 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16151 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16152 &enablebreaklist);
16153
16154 add_cmd ("delete", no_class, enable_delete_command, _("\
16155 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16156 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16157 &enablebreaklist);
16158
16159 add_cmd ("count", no_class, enable_count_command, _("\
16160 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16161 If a breakpoint is hit while enabled in this fashion,\n\
16162 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16163 &enablebreaklist);
16164
16165 add_cmd ("delete", no_class, enable_delete_command, _("\
16166 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16167 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16168 &enablelist);
16169
16170 add_cmd ("once", no_class, enable_once_command, _("\
16171 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16172 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16173 &enablelist);
16174
16175 add_cmd ("count", no_class, enable_count_command, _("\
16176 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16177 If a breakpoint is hit while enabled in this fashion,\n\
16178 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16179 &enablelist);
16180
16181 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16182 Disable some breakpoints.\n\
16183 Arguments are breakpoint numbers with spaces in between.\n\
16184 To disable all breakpoints, give no argument.\n\
16185 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16186 &disablelist, "disable ", 1, &cmdlist);
16187 add_com_alias ("dis", "disable", class_breakpoint, 1);
16188 add_com_alias ("disa", "disable", class_breakpoint, 1);
16189
16190 add_cmd ("breakpoints", class_alias, disable_command, _("\
16191 Disable some breakpoints.\n\
16192 Arguments are breakpoint numbers with spaces in between.\n\
16193 To disable all breakpoints, give no argument.\n\
16194 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16195 This command may be abbreviated \"disable\"."),
16196 &disablelist);
16197
16198 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16199 Delete some breakpoints or auto-display expressions.\n\
16200 Arguments are breakpoint numbers with spaces in between.\n\
16201 To delete all breakpoints, give no argument.\n\
16202 \n\
16203 Also a prefix command for deletion of other GDB objects.\n\
16204 The \"unset\" command is also an alias for \"delete\"."),
16205 &deletelist, "delete ", 1, &cmdlist);
16206 add_com_alias ("d", "delete", class_breakpoint, 1);
16207 add_com_alias ("del", "delete", class_breakpoint, 1);
16208
16209 add_cmd ("breakpoints", class_alias, delete_command, _("\
16210 Delete some breakpoints or auto-display expressions.\n\
16211 Arguments are breakpoint numbers with spaces in between.\n\
16212 To delete all breakpoints, give no argument.\n\
16213 This command may be abbreviated \"delete\"."),
16214 &deletelist);
16215
16216 add_com ("clear", class_breakpoint, clear_command, _("\
16217 Clear breakpoint at specified location.\n\
16218 Argument may be a linespec, explicit, or address location as described below.\n\
16219 \n\
16220 With no argument, clears all breakpoints in the line that the selected frame\n\
16221 is executing in.\n"
16222 "\n" LOCATION_HELP_STRING "\n\
16223 See also the \"delete\" command which clears breakpoints by number."));
16224 add_com_alias ("cl", "clear", class_breakpoint, 1);
16225
16226 c = add_com ("break", class_breakpoint, break_command, _("\
16227 Set breakpoint at specified location.\n"
16228 BREAK_ARGS_HELP ("break")));
16229 set_cmd_completer (c, location_completer);
16230
16231 add_com_alias ("b", "break", class_run, 1);
16232 add_com_alias ("br", "break", class_run, 1);
16233 add_com_alias ("bre", "break", class_run, 1);
16234 add_com_alias ("brea", "break", class_run, 1);
16235
16236 if (dbx_commands)
16237 {
16238 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16239 Break in function/address or break at a line in the current file."),
16240 &stoplist, "stop ", 1, &cmdlist);
16241 add_cmd ("in", class_breakpoint, stopin_command,
16242 _("Break in function or address."), &stoplist);
16243 add_cmd ("at", class_breakpoint, stopat_command,
16244 _("Break at a line in the current file."), &stoplist);
16245 add_com ("status", class_info, breakpoints_info, _("\
16246 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16247 The \"Type\" column indicates one of:\n\
16248 \tbreakpoint - normal breakpoint\n\
16249 \twatchpoint - watchpoint\n\
16250 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16251 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16252 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16253 address and file/line number respectively.\n\
16254 \n\
16255 Convenience variable \"$_\" and default examine address for \"x\"\n\
16256 are set to the address of the last breakpoint listed unless the command\n\
16257 is prefixed with \"server \".\n\n\
16258 Convenience variable \"$bpnum\" contains the number of the last\n\
16259 breakpoint set."));
16260 }
16261
16262 add_info ("breakpoints", breakpoints_info, _("\
16263 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16264 The \"Type\" column indicates one of:\n\
16265 \tbreakpoint - normal breakpoint\n\
16266 \twatchpoint - watchpoint\n\
16267 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16268 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16269 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16270 address and file/line number respectively.\n\
16271 \n\
16272 Convenience variable \"$_\" and default examine address for \"x\"\n\
16273 are set to the address of the last breakpoint listed unless the command\n\
16274 is prefixed with \"server \".\n\n\
16275 Convenience variable \"$bpnum\" contains the number of the last\n\
16276 breakpoint set."));
16277
16278 add_info_alias ("b", "breakpoints", 1);
16279
16280 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16281 Status of all breakpoints, or breakpoint number NUMBER.\n\
16282 The \"Type\" column indicates one of:\n\
16283 \tbreakpoint - normal breakpoint\n\
16284 \twatchpoint - watchpoint\n\
16285 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16286 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16287 \tuntil - internal breakpoint used by the \"until\" command\n\
16288 \tfinish - internal breakpoint used by the \"finish\" command\n\
16289 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16290 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16291 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16292 address and file/line number respectively.\n\
16293 \n\
16294 Convenience variable \"$_\" and default examine address for \"x\"\n\
16295 are set to the address of the last breakpoint listed unless the command\n\
16296 is prefixed with \"server \".\n\n\
16297 Convenience variable \"$bpnum\" contains the number of the last\n\
16298 breakpoint set."),
16299 &maintenanceinfolist);
16300
16301 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16302 Set catchpoints to catch events."),
16303 &catch_cmdlist, "catch ",
16304 0/*allow-unknown*/, &cmdlist);
16305
16306 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16307 Set temporary catchpoints to catch events."),
16308 &tcatch_cmdlist, "tcatch ",
16309 0/*allow-unknown*/, &cmdlist);
16310
16311 add_catch_command ("fork", _("Catch calls to fork."),
16312 catch_fork_command_1,
16313 NULL,
16314 (void *) (uintptr_t) catch_fork_permanent,
16315 (void *) (uintptr_t) catch_fork_temporary);
16316 add_catch_command ("vfork", _("Catch calls to vfork."),
16317 catch_fork_command_1,
16318 NULL,
16319 (void *) (uintptr_t) catch_vfork_permanent,
16320 (void *) (uintptr_t) catch_vfork_temporary);
16321 add_catch_command ("exec", _("Catch calls to exec."),
16322 catch_exec_command_1,
16323 NULL,
16324 CATCH_PERMANENT,
16325 CATCH_TEMPORARY);
16326 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16327 Usage: catch load [REGEX]\n\
16328 If REGEX is given, only stop for libraries matching the regular expression."),
16329 catch_load_command_1,
16330 NULL,
16331 CATCH_PERMANENT,
16332 CATCH_TEMPORARY);
16333 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16334 Usage: catch unload [REGEX]\n\
16335 If REGEX is given, only stop for libraries matching the regular expression."),
16336 catch_unload_command_1,
16337 NULL,
16338 CATCH_PERMANENT,
16339 CATCH_TEMPORARY);
16340
16341 c = add_com ("watch", class_breakpoint, watch_command, _("\
16342 Set a watchpoint for an expression.\n\
16343 Usage: watch [-l|-location] EXPRESSION\n\
16344 A watchpoint stops execution of your program whenever the value of\n\
16345 an expression changes.\n\
16346 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16347 the memory to which it refers."));
16348 set_cmd_completer (c, expression_completer);
16349
16350 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16351 Set a read watchpoint for an expression.\n\
16352 Usage: rwatch [-l|-location] EXPRESSION\n\
16353 A watchpoint stops execution of your program whenever the value of\n\
16354 an expression is read.\n\
16355 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16356 the memory to which it refers."));
16357 set_cmd_completer (c, expression_completer);
16358
16359 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16360 Set a watchpoint for an expression.\n\
16361 Usage: awatch [-l|-location] EXPRESSION\n\
16362 A watchpoint stops execution of your program whenever the value of\n\
16363 an expression is either read or written.\n\
16364 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16365 the memory to which it refers."));
16366 set_cmd_completer (c, expression_completer);
16367
16368 add_info ("watchpoints", watchpoints_info, _("\
16369 Status of specified watchpoints (all watchpoints if no argument)."));
16370
16371 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16372 respond to changes - contrary to the description. */
16373 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16374 &can_use_hw_watchpoints, _("\
16375 Set debugger's willingness to use watchpoint hardware."), _("\
16376 Show debugger's willingness to use watchpoint hardware."), _("\
16377 If zero, gdb will not use hardware for new watchpoints, even if\n\
16378 such is available. (However, any hardware watchpoints that were\n\
16379 created before setting this to nonzero, will continue to use watchpoint\n\
16380 hardware.)"),
16381 NULL,
16382 show_can_use_hw_watchpoints,
16383 &setlist, &showlist);
16384
16385 can_use_hw_watchpoints = 1;
16386
16387 /* Tracepoint manipulation commands. */
16388
16389 c = add_com ("trace", class_breakpoint, trace_command, _("\
16390 Set a tracepoint at specified location.\n\
16391 \n"
16392 BREAK_ARGS_HELP ("trace") "\n\
16393 Do \"help tracepoints\" for info on other tracepoint commands."));
16394 set_cmd_completer (c, location_completer);
16395
16396 add_com_alias ("tp", "trace", class_alias, 0);
16397 add_com_alias ("tr", "trace", class_alias, 1);
16398 add_com_alias ("tra", "trace", class_alias, 1);
16399 add_com_alias ("trac", "trace", class_alias, 1);
16400
16401 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16402 Set a fast tracepoint at specified location.\n\
16403 \n"
16404 BREAK_ARGS_HELP ("ftrace") "\n\
16405 Do \"help tracepoints\" for info on other tracepoint commands."));
16406 set_cmd_completer (c, location_completer);
16407
16408 c = add_com ("strace", class_breakpoint, strace_command, _("\
16409 Set a static tracepoint at location or marker.\n\
16410 \n\
16411 strace [LOCATION] [if CONDITION]\n\
16412 LOCATION may be a linespec, explicit, or address location (described below) \n\
16413 or -m MARKER_ID.\n\n\
16414 If a marker id is specified, probe the marker with that name. With\n\
16415 no LOCATION, uses current execution address of the selected stack frame.\n\
16416 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16417 This collects arbitrary user data passed in the probe point call to the\n\
16418 tracing library. You can inspect it when analyzing the trace buffer,\n\
16419 by printing the $_sdata variable like any other convenience variable.\n\
16420 \n\
16421 CONDITION is a boolean expression.\n\
16422 \n" LOCATION_HELP_STRING "\n\
16423 Multiple tracepoints at one place are permitted, and useful if their\n\
16424 conditions are different.\n\
16425 \n\
16426 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16427 Do \"help tracepoints\" for info on other tracepoint commands."));
16428 set_cmd_completer (c, location_completer);
16429
16430 add_info ("tracepoints", tracepoints_info, _("\
16431 Status of specified tracepoints (all tracepoints if no argument).\n\
16432 Convenience variable \"$tpnum\" contains the number of the\n\
16433 last tracepoint set."));
16434
16435 add_info_alias ("tp", "tracepoints", 1);
16436
16437 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16438 Delete specified tracepoints.\n\
16439 Arguments are tracepoint numbers, separated by spaces.\n\
16440 No argument means delete all tracepoints."),
16441 &deletelist);
16442 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16443
16444 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16445 Disable specified tracepoints.\n\
16446 Arguments are tracepoint numbers, separated by spaces.\n\
16447 No argument means disable all tracepoints."),
16448 &disablelist);
16449 deprecate_cmd (c, "disable");
16450
16451 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16452 Enable specified tracepoints.\n\
16453 Arguments are tracepoint numbers, separated by spaces.\n\
16454 No argument means enable all tracepoints."),
16455 &enablelist);
16456 deprecate_cmd (c, "enable");
16457
16458 add_com ("passcount", class_trace, trace_pass_command, _("\
16459 Set the passcount for a tracepoint.\n\
16460 The trace will end when the tracepoint has been passed 'count' times.\n\
16461 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16462 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16463
16464 add_prefix_cmd ("save", class_breakpoint, save_command,
16465 _("Save breakpoint definitions as a script."),
16466 &save_cmdlist, "save ",
16467 0/*allow-unknown*/, &cmdlist);
16468
16469 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16470 Save current breakpoint definitions as a script.\n\
16471 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16472 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16473 session to restore them."),
16474 &save_cmdlist);
16475 set_cmd_completer (c, filename_completer);
16476
16477 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16478 Save current tracepoint definitions as a script.\n\
16479 Use the 'source' command in another debug session to restore them."),
16480 &save_cmdlist);
16481 set_cmd_completer (c, filename_completer);
16482
16483 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16484 deprecate_cmd (c, "save tracepoints");
16485
16486 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16487 Breakpoint specific settings\n\
16488 Configure various breakpoint-specific variables such as\n\
16489 pending breakpoint behavior"),
16490 &breakpoint_set_cmdlist, "set breakpoint ",
16491 0/*allow-unknown*/, &setlist);
16492 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16493 Breakpoint specific settings\n\
16494 Configure various breakpoint-specific variables such as\n\
16495 pending breakpoint behavior"),
16496 &breakpoint_show_cmdlist, "show breakpoint ",
16497 0/*allow-unknown*/, &showlist);
16498
16499 add_setshow_auto_boolean_cmd ("pending", no_class,
16500 &pending_break_support, _("\
16501 Set debugger's behavior regarding pending breakpoints."), _("\
16502 Show debugger's behavior regarding pending breakpoints."), _("\
16503 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16504 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16505 an error. If auto, an unrecognized breakpoint location results in a\n\
16506 user-query to see if a pending breakpoint should be created."),
16507 NULL,
16508 show_pending_break_support,
16509 &breakpoint_set_cmdlist,
16510 &breakpoint_show_cmdlist);
16511
16512 pending_break_support = AUTO_BOOLEAN_AUTO;
16513
16514 add_setshow_boolean_cmd ("auto-hw", no_class,
16515 &automatic_hardware_breakpoints, _("\
16516 Set automatic usage of hardware breakpoints."), _("\
16517 Show automatic usage of hardware breakpoints."), _("\
16518 If set, the debugger will automatically use hardware breakpoints for\n\
16519 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16520 a warning will be emitted for such breakpoints."),
16521 NULL,
16522 show_automatic_hardware_breakpoints,
16523 &breakpoint_set_cmdlist,
16524 &breakpoint_show_cmdlist);
16525
16526 add_setshow_boolean_cmd ("always-inserted", class_support,
16527 &always_inserted_mode, _("\
16528 Set mode for inserting breakpoints."), _("\
16529 Show mode for inserting breakpoints."), _("\
16530 When this mode is on, breakpoints are inserted immediately as soon as\n\
16531 they're created, kept inserted even when execution stops, and removed\n\
16532 only when the user deletes them. When this mode is off (the default),\n\
16533 breakpoints are inserted only when execution continues, and removed\n\
16534 when execution stops."),
16535 NULL,
16536 &show_always_inserted_mode,
16537 &breakpoint_set_cmdlist,
16538 &breakpoint_show_cmdlist);
16539
16540 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16541 condition_evaluation_enums,
16542 &condition_evaluation_mode_1, _("\
16543 Set mode of breakpoint condition evaluation."), _("\
16544 Show mode of breakpoint condition evaluation."), _("\
16545 When this is set to \"host\", breakpoint conditions will be\n\
16546 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16547 breakpoint conditions will be downloaded to the target (if the target\n\
16548 supports such feature) and conditions will be evaluated on the target's side.\n\
16549 If this is set to \"auto\" (default), this will be automatically set to\n\
16550 \"target\" if it supports condition evaluation, otherwise it will\n\
16551 be set to \"gdb\""),
16552 &set_condition_evaluation_mode,
16553 &show_condition_evaluation_mode,
16554 &breakpoint_set_cmdlist,
16555 &breakpoint_show_cmdlist);
16556
16557 add_com ("break-range", class_breakpoint, break_range_command, _("\
16558 Set a breakpoint for an address range.\n\
16559 break-range START-LOCATION, END-LOCATION\n\
16560 where START-LOCATION and END-LOCATION can be one of the following:\n\
16561 LINENUM, for that line in the current file,\n\
16562 FILE:LINENUM, for that line in that file,\n\
16563 +OFFSET, for that number of lines after the current line\n\
16564 or the start of the range\n\
16565 FUNCTION, for the first line in that function,\n\
16566 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16567 *ADDRESS, for the instruction at that address.\n\
16568 \n\
16569 The breakpoint will stop execution of the inferior whenever it executes\n\
16570 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16571 range (including START-LOCATION and END-LOCATION)."));
16572
16573 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16574 Set a dynamic printf at specified location.\n\
16575 dprintf location,format string,arg1,arg2,...\n\
16576 location may be a linespec, explicit, or address location.\n"
16577 "\n" LOCATION_HELP_STRING));
16578 set_cmd_completer (c, location_completer);
16579
16580 add_setshow_enum_cmd ("dprintf-style", class_support,
16581 dprintf_style_enums, &dprintf_style, _("\
16582 Set the style of usage for dynamic printf."), _("\
16583 Show the style of usage for dynamic printf."), _("\
16584 This setting chooses how GDB will do a dynamic printf.\n\
16585 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16586 console, as with the \"printf\" command.\n\
16587 If the value is \"call\", the print is done by calling a function in your\n\
16588 program; by default printf(), but you can choose a different function or\n\
16589 output stream by setting dprintf-function and dprintf-channel."),
16590 update_dprintf_commands, NULL,
16591 &setlist, &showlist);
16592
16593 dprintf_function = xstrdup ("printf");
16594 add_setshow_string_cmd ("dprintf-function", class_support,
16595 &dprintf_function, _("\
16596 Set the function to use for dynamic printf"), _("\
16597 Show the function to use for dynamic printf"), NULL,
16598 update_dprintf_commands, NULL,
16599 &setlist, &showlist);
16600
16601 dprintf_channel = xstrdup ("");
16602 add_setshow_string_cmd ("dprintf-channel", class_support,
16603 &dprintf_channel, _("\
16604 Set the channel to use for dynamic printf"), _("\
16605 Show the channel to use for dynamic printf"), NULL,
16606 update_dprintf_commands, NULL,
16607 &setlist, &showlist);
16608
16609 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16610 &disconnected_dprintf, _("\
16611 Set whether dprintf continues after GDB disconnects."), _("\
16612 Show whether dprintf continues after GDB disconnects."), _("\
16613 Use this to let dprintf commands continue to hit and produce output\n\
16614 even if GDB disconnects or detaches from the target."),
16615 NULL,
16616 NULL,
16617 &setlist, &showlist);
16618
16619 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16620 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16621 (target agent only) This is useful for formatted output in user-defined commands."));
16622
16623 automatic_hardware_breakpoints = 1;
16624
16625 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16626 observer_attach_thread_exit (remove_threaded_breakpoints);
16627 }
This page took 0.362333 seconds and 5 git commands to generate.