Change return type of gdbarch_software_single_step to vector<CORE_ADDR>
[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 gdb_assert (ops != NULL);
7432
7433 b->ops = ops;
7434 b->type = bptype;
7435 b->gdbarch = gdbarch;
7436 b->language = current_language->la_language;
7437 b->input_radix = input_radix;
7438 b->related_breakpoint = b;
7439 }
7440
7441 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7442 that has type BPTYPE and has no locations as yet. */
7443
7444 static struct breakpoint *
7445 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7446 enum bptype bptype,
7447 const struct breakpoint_ops *ops)
7448 {
7449 struct breakpoint *b = new breakpoint ();
7450
7451 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7452 add_to_breakpoint_chain (b);
7453 return b;
7454 }
7455
7456 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7457 resolutions should be made as the user specified the location explicitly
7458 enough. */
7459
7460 static void
7461 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7462 {
7463 gdb_assert (loc->owner != NULL);
7464
7465 if (loc->owner->type == bp_breakpoint
7466 || loc->owner->type == bp_hardware_breakpoint
7467 || is_tracepoint (loc->owner))
7468 {
7469 int is_gnu_ifunc;
7470 const char *function_name;
7471 CORE_ADDR func_addr;
7472
7473 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7474 &func_addr, NULL, &is_gnu_ifunc);
7475
7476 if (is_gnu_ifunc && !explicit_loc)
7477 {
7478 struct breakpoint *b = loc->owner;
7479
7480 gdb_assert (loc->pspace == current_program_space);
7481 if (gnu_ifunc_resolve_name (function_name,
7482 &loc->requested_address))
7483 {
7484 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7485 loc->address = adjust_breakpoint_address (loc->gdbarch,
7486 loc->requested_address,
7487 b->type);
7488 }
7489 else if (b->type == bp_breakpoint && b->loc == loc
7490 && loc->next == NULL && b->related_breakpoint == b)
7491 {
7492 /* Create only the whole new breakpoint of this type but do not
7493 mess more complicated breakpoints with multiple locations. */
7494 b->type = bp_gnu_ifunc_resolver;
7495 /* Remember the resolver's address for use by the return
7496 breakpoint. */
7497 loc->related_address = func_addr;
7498 }
7499 }
7500
7501 if (function_name)
7502 loc->function_name = xstrdup (function_name);
7503 }
7504 }
7505
7506 /* Attempt to determine architecture of location identified by SAL. */
7507 struct gdbarch *
7508 get_sal_arch (struct symtab_and_line sal)
7509 {
7510 if (sal.section)
7511 return get_objfile_arch (sal.section->objfile);
7512 if (sal.symtab)
7513 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7514
7515 return NULL;
7516 }
7517
7518 /* Low level routine for partially initializing a breakpoint of type
7519 BPTYPE. The newly created breakpoint's address, section, source
7520 file name, and line number are provided by SAL.
7521
7522 It is expected that the caller will complete the initialization of
7523 the newly created breakpoint struct as well as output any status
7524 information regarding the creation of a new breakpoint. */
7525
7526 static void
7527 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7528 struct symtab_and_line sal, enum bptype bptype,
7529 const struct breakpoint_ops *ops)
7530 {
7531 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7532
7533 add_location_to_breakpoint (b, &sal);
7534
7535 if (bptype != bp_catchpoint)
7536 gdb_assert (sal.pspace != NULL);
7537
7538 /* Store the program space that was used to set the breakpoint,
7539 except for ordinary breakpoints, which are independent of the
7540 program space. */
7541 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7542 b->pspace = sal.pspace;
7543 }
7544
7545 /* set_raw_breakpoint is a low level routine for allocating and
7546 partially initializing a breakpoint of type BPTYPE. The newly
7547 created breakpoint's address, section, source file name, and line
7548 number are provided by SAL. The newly created and partially
7549 initialized breakpoint is added to the breakpoint chain and
7550 is also returned as the value of this function.
7551
7552 It is expected that the caller will complete the initialization of
7553 the newly created breakpoint struct as well as output any status
7554 information regarding the creation of a new breakpoint. In
7555 particular, set_raw_breakpoint does NOT set the breakpoint
7556 number! Care should be taken to not allow an error to occur
7557 prior to completing the initialization of the breakpoint. If this
7558 should happen, a bogus breakpoint will be left on the chain. */
7559
7560 struct breakpoint *
7561 set_raw_breakpoint (struct gdbarch *gdbarch,
7562 struct symtab_and_line sal, enum bptype bptype,
7563 const struct breakpoint_ops *ops)
7564 {
7565 struct breakpoint *b = new breakpoint ();
7566
7567 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7568 add_to_breakpoint_chain (b);
7569 return b;
7570 }
7571
7572 /* Call this routine when stepping and nexting to enable a breakpoint
7573 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7574 initiated the operation. */
7575
7576 void
7577 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7578 {
7579 struct breakpoint *b, *b_tmp;
7580 int thread = tp->global_num;
7581
7582 /* To avoid having to rescan all objfile symbols at every step,
7583 we maintain a list of continually-inserted but always disabled
7584 longjmp "master" breakpoints. Here, we simply create momentary
7585 clones of those and enable them for the requested thread. */
7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7587 if (b->pspace == current_program_space
7588 && (b->type == bp_longjmp_master
7589 || b->type == bp_exception_master))
7590 {
7591 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7592 struct breakpoint *clone;
7593
7594 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7595 after their removal. */
7596 clone = momentary_breakpoint_from_master (b, type,
7597 &longjmp_breakpoint_ops, 1);
7598 clone->thread = thread;
7599 }
7600
7601 tp->initiating_frame = frame;
7602 }
7603
7604 /* Delete all longjmp breakpoints from THREAD. */
7605 void
7606 delete_longjmp_breakpoint (int thread)
7607 {
7608 struct breakpoint *b, *b_tmp;
7609
7610 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7611 if (b->type == bp_longjmp || b->type == bp_exception)
7612 {
7613 if (b->thread == thread)
7614 delete_breakpoint (b);
7615 }
7616 }
7617
7618 void
7619 delete_longjmp_breakpoint_at_next_stop (int thread)
7620 {
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_longjmp || b->type == bp_exception)
7625 {
7626 if (b->thread == thread)
7627 b->disposition = disp_del_at_next_stop;
7628 }
7629 }
7630
7631 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7632 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7633 pointer to any of them. Return NULL if this system cannot place longjmp
7634 breakpoints. */
7635
7636 struct breakpoint *
7637 set_longjmp_breakpoint_for_call_dummy (void)
7638 {
7639 struct breakpoint *b, *retval = NULL;
7640
7641 ALL_BREAKPOINTS (b)
7642 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7643 {
7644 struct breakpoint *new_b;
7645
7646 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7647 &momentary_breakpoint_ops,
7648 1);
7649 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7650
7651 /* Link NEW_B into the chain of RETVAL breakpoints. */
7652
7653 gdb_assert (new_b->related_breakpoint == new_b);
7654 if (retval == NULL)
7655 retval = new_b;
7656 new_b->related_breakpoint = retval;
7657 while (retval->related_breakpoint != new_b->related_breakpoint)
7658 retval = retval->related_breakpoint;
7659 retval->related_breakpoint = new_b;
7660 }
7661
7662 return retval;
7663 }
7664
7665 /* Verify all existing dummy frames and their associated breakpoints for
7666 TP. Remove those which can no longer be found in the current frame
7667 stack.
7668
7669 You should call this function only at places where it is safe to currently
7670 unwind the whole stack. Failed stack unwind would discard live dummy
7671 frames. */
7672
7673 void
7674 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7675 {
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7679 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7680 {
7681 struct breakpoint *dummy_b = b->related_breakpoint;
7682
7683 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7684 dummy_b = dummy_b->related_breakpoint;
7685 if (dummy_b->type != bp_call_dummy
7686 || frame_find_by_id (dummy_b->frame_id) != NULL)
7687 continue;
7688
7689 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7690
7691 while (b->related_breakpoint != b)
7692 {
7693 if (b_tmp == b->related_breakpoint)
7694 b_tmp = b->related_breakpoint->next;
7695 delete_breakpoint (b->related_breakpoint);
7696 }
7697 delete_breakpoint (b);
7698 }
7699 }
7700
7701 void
7702 enable_overlay_breakpoints (void)
7703 {
7704 struct breakpoint *b;
7705
7706 ALL_BREAKPOINTS (b)
7707 if (b->type == bp_overlay_event)
7708 {
7709 b->enable_state = bp_enabled;
7710 update_global_location_list (UGLL_MAY_INSERT);
7711 overlay_events_enabled = 1;
7712 }
7713 }
7714
7715 void
7716 disable_overlay_breakpoints (void)
7717 {
7718 struct breakpoint *b;
7719
7720 ALL_BREAKPOINTS (b)
7721 if (b->type == bp_overlay_event)
7722 {
7723 b->enable_state = bp_disabled;
7724 update_global_location_list (UGLL_DONT_INSERT);
7725 overlay_events_enabled = 0;
7726 }
7727 }
7728
7729 /* Set an active std::terminate breakpoint for each std::terminate
7730 master breakpoint. */
7731 void
7732 set_std_terminate_breakpoint (void)
7733 {
7734 struct breakpoint *b, *b_tmp;
7735
7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7737 if (b->pspace == current_program_space
7738 && b->type == bp_std_terminate_master)
7739 {
7740 momentary_breakpoint_from_master (b, bp_std_terminate,
7741 &momentary_breakpoint_ops, 1);
7742 }
7743 }
7744
7745 /* Delete all the std::terminate breakpoints. */
7746 void
7747 delete_std_terminate_breakpoint (void)
7748 {
7749 struct breakpoint *b, *b_tmp;
7750
7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7752 if (b->type == bp_std_terminate)
7753 delete_breakpoint (b);
7754 }
7755
7756 struct breakpoint *
7757 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7758 {
7759 struct breakpoint *b;
7760
7761 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7762 &internal_breakpoint_ops);
7763
7764 b->enable_state = bp_enabled;
7765 /* location has to be used or breakpoint_re_set will delete me. */
7766 b->location = new_address_location (b->loc->address, NULL, 0);
7767
7768 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7769
7770 return b;
7771 }
7772
7773 struct lang_and_radix
7774 {
7775 enum language lang;
7776 int radix;
7777 };
7778
7779 /* Create a breakpoint for JIT code registration and unregistration. */
7780
7781 struct breakpoint *
7782 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7783 {
7784 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7785 &internal_breakpoint_ops);
7786 }
7787
7788 /* Remove JIT code registration and unregistration breakpoint(s). */
7789
7790 void
7791 remove_jit_event_breakpoints (void)
7792 {
7793 struct breakpoint *b, *b_tmp;
7794
7795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7796 if (b->type == bp_jit_event
7797 && b->loc->pspace == current_program_space)
7798 delete_breakpoint (b);
7799 }
7800
7801 void
7802 remove_solib_event_breakpoints (void)
7803 {
7804 struct breakpoint *b, *b_tmp;
7805
7806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7807 if (b->type == bp_shlib_event
7808 && b->loc->pspace == current_program_space)
7809 delete_breakpoint (b);
7810 }
7811
7812 /* See breakpoint.h. */
7813
7814 void
7815 remove_solib_event_breakpoints_at_next_stop (void)
7816 {
7817 struct breakpoint *b, *b_tmp;
7818
7819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7820 if (b->type == bp_shlib_event
7821 && b->loc->pspace == current_program_space)
7822 b->disposition = disp_del_at_next_stop;
7823 }
7824
7825 /* Helper for create_solib_event_breakpoint /
7826 create_and_insert_solib_event_breakpoint. Allows specifying which
7827 INSERT_MODE to pass through to update_global_location_list. */
7828
7829 static struct breakpoint *
7830 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7831 enum ugll_insert_mode insert_mode)
7832 {
7833 struct breakpoint *b;
7834
7835 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7836 &internal_breakpoint_ops);
7837 update_global_location_list_nothrow (insert_mode);
7838 return b;
7839 }
7840
7841 struct breakpoint *
7842 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7843 {
7844 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7845 }
7846
7847 /* See breakpoint.h. */
7848
7849 struct breakpoint *
7850 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7851 {
7852 struct breakpoint *b;
7853
7854 /* Explicitly tell update_global_location_list to insert
7855 locations. */
7856 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7857 if (!b->loc->inserted)
7858 {
7859 delete_breakpoint (b);
7860 return NULL;
7861 }
7862 return b;
7863 }
7864
7865 /* Disable any breakpoints that are on code in shared libraries. Only
7866 apply to enabled breakpoints, disabled ones can just stay disabled. */
7867
7868 void
7869 disable_breakpoints_in_shlibs (void)
7870 {
7871 struct bp_location *loc, **locp_tmp;
7872
7873 ALL_BP_LOCATIONS (loc, locp_tmp)
7874 {
7875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7876 struct breakpoint *b = loc->owner;
7877
7878 /* We apply the check to all breakpoints, including disabled for
7879 those with loc->duplicate set. This is so that when breakpoint
7880 becomes enabled, or the duplicate is removed, gdb will try to
7881 insert all breakpoints. If we don't set shlib_disabled here,
7882 we'll try to insert those breakpoints and fail. */
7883 if (((b->type == bp_breakpoint)
7884 || (b->type == bp_jit_event)
7885 || (b->type == bp_hardware_breakpoint)
7886 || (is_tracepoint (b)))
7887 && loc->pspace == current_program_space
7888 && !loc->shlib_disabled
7889 && solib_name_from_address (loc->pspace, loc->address)
7890 )
7891 {
7892 loc->shlib_disabled = 1;
7893 }
7894 }
7895 }
7896
7897 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7898 notification of unloaded_shlib. Only apply to enabled breakpoints,
7899 disabled ones can just stay disabled. */
7900
7901 static void
7902 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7903 {
7904 struct bp_location *loc, **locp_tmp;
7905 int disabled_shlib_breaks = 0;
7906
7907 ALL_BP_LOCATIONS (loc, locp_tmp)
7908 {
7909 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7910 struct breakpoint *b = loc->owner;
7911
7912 if (solib->pspace == loc->pspace
7913 && !loc->shlib_disabled
7914 && (((b->type == bp_breakpoint
7915 || b->type == bp_jit_event
7916 || b->type == bp_hardware_breakpoint)
7917 && (loc->loc_type == bp_loc_hardware_breakpoint
7918 || loc->loc_type == bp_loc_software_breakpoint))
7919 || is_tracepoint (b))
7920 && solib_contains_address_p (solib, loc->address))
7921 {
7922 loc->shlib_disabled = 1;
7923 /* At this point, we cannot rely on remove_breakpoint
7924 succeeding so we must mark the breakpoint as not inserted
7925 to prevent future errors occurring in remove_breakpoints. */
7926 loc->inserted = 0;
7927
7928 /* This may cause duplicate notifications for the same breakpoint. */
7929 observer_notify_breakpoint_modified (b);
7930
7931 if (!disabled_shlib_breaks)
7932 {
7933 target_terminal_ours_for_output ();
7934 warning (_("Temporarily disabling breakpoints "
7935 "for unloaded shared library \"%s\""),
7936 solib->so_name);
7937 }
7938 disabled_shlib_breaks = 1;
7939 }
7940 }
7941 }
7942
7943 /* Disable any breakpoints and tracepoints in OBJFILE upon
7944 notification of free_objfile. Only apply to enabled breakpoints,
7945 disabled ones can just stay disabled. */
7946
7947 static void
7948 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7949 {
7950 struct breakpoint *b;
7951
7952 if (objfile == NULL)
7953 return;
7954
7955 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7956 managed by the user with add-symbol-file/remove-symbol-file.
7957 Similarly to how breakpoints in shared libraries are handled in
7958 response to "nosharedlibrary", mark breakpoints in such modules
7959 shlib_disabled so they end up uninserted on the next global
7960 location list update. Shared libraries not loaded by the user
7961 aren't handled here -- they're already handled in
7962 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7963 solib_unloaded observer. We skip objfiles that are not
7964 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7965 main objfile). */
7966 if ((objfile->flags & OBJF_SHARED) == 0
7967 || (objfile->flags & OBJF_USERLOADED) == 0)
7968 return;
7969
7970 ALL_BREAKPOINTS (b)
7971 {
7972 struct bp_location *loc;
7973 int bp_modified = 0;
7974
7975 if (!is_breakpoint (b) && !is_tracepoint (b))
7976 continue;
7977
7978 for (loc = b->loc; loc != NULL; loc = loc->next)
7979 {
7980 CORE_ADDR loc_addr = loc->address;
7981
7982 if (loc->loc_type != bp_loc_hardware_breakpoint
7983 && loc->loc_type != bp_loc_software_breakpoint)
7984 continue;
7985
7986 if (loc->shlib_disabled != 0)
7987 continue;
7988
7989 if (objfile->pspace != loc->pspace)
7990 continue;
7991
7992 if (loc->loc_type != bp_loc_hardware_breakpoint
7993 && loc->loc_type != bp_loc_software_breakpoint)
7994 continue;
7995
7996 if (is_addr_in_objfile (loc_addr, objfile))
7997 {
7998 loc->shlib_disabled = 1;
7999 /* At this point, we don't know whether the object was
8000 unmapped from the inferior or not, so leave the
8001 inserted flag alone. We'll handle failure to
8002 uninsert quietly, in case the object was indeed
8003 unmapped. */
8004
8005 mark_breakpoint_location_modified (loc);
8006
8007 bp_modified = 1;
8008 }
8009 }
8010
8011 if (bp_modified)
8012 observer_notify_breakpoint_modified (b);
8013 }
8014 }
8015
8016 /* FORK & VFORK catchpoints. */
8017
8018 /* An instance of this type is used to represent a fork or vfork
8019 catchpoint. It includes a "struct breakpoint" as a kind of base
8020 class; users downcast to "struct breakpoint *" when needed. A
8021 breakpoint is really of this type iff its ops pointer points to
8022 CATCH_FORK_BREAKPOINT_OPS. */
8023
8024 struct fork_catchpoint
8025 {
8026 /* The base class. */
8027 struct breakpoint base;
8028
8029 /* Process id of a child process whose forking triggered this
8030 catchpoint. This field is only valid immediately after this
8031 catchpoint has triggered. */
8032 ptid_t forked_inferior_pid;
8033 };
8034
8035 /* Implement the "insert" breakpoint_ops method for fork
8036 catchpoints. */
8037
8038 static int
8039 insert_catch_fork (struct bp_location *bl)
8040 {
8041 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8042 }
8043
8044 /* Implement the "remove" breakpoint_ops method for fork
8045 catchpoints. */
8046
8047 static int
8048 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8049 {
8050 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8051 }
8052
8053 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8054 catchpoints. */
8055
8056 static int
8057 breakpoint_hit_catch_fork (const struct bp_location *bl,
8058 struct address_space *aspace, CORE_ADDR bp_addr,
8059 const struct target_waitstatus *ws)
8060 {
8061 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8062
8063 if (ws->kind != TARGET_WAITKIND_FORKED)
8064 return 0;
8065
8066 c->forked_inferior_pid = ws->value.related_pid;
8067 return 1;
8068 }
8069
8070 /* Implement the "print_it" breakpoint_ops method for fork
8071 catchpoints. */
8072
8073 static enum print_stop_action
8074 print_it_catch_fork (bpstat bs)
8075 {
8076 struct ui_out *uiout = current_uiout;
8077 struct breakpoint *b = bs->breakpoint_at;
8078 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8079
8080 annotate_catchpoint (b->number);
8081 maybe_print_thread_hit_breakpoint (uiout);
8082 if (b->disposition == disp_del)
8083 uiout->text ("Temporary catchpoint ");
8084 else
8085 uiout->text ("Catchpoint ");
8086 if (uiout->is_mi_like_p ())
8087 {
8088 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8089 uiout->field_string ("disp", bpdisp_text (b->disposition));
8090 }
8091 uiout->field_int ("bkptno", b->number);
8092 uiout->text (" (forked process ");
8093 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8094 uiout->text ("), ");
8095 return PRINT_SRC_AND_LOC;
8096 }
8097
8098 /* Implement the "print_one" breakpoint_ops method for fork
8099 catchpoints. */
8100
8101 static void
8102 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8103 {
8104 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8105 struct value_print_options opts;
8106 struct ui_out *uiout = current_uiout;
8107
8108 get_user_print_options (&opts);
8109
8110 /* Field 4, the address, is omitted (which makes the columns not
8111 line up too nicely with the headers, but the effect is relatively
8112 readable). */
8113 if (opts.addressprint)
8114 uiout->field_skip ("addr");
8115 annotate_field (5);
8116 uiout->text ("fork");
8117 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8118 {
8119 uiout->text (", process ");
8120 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8121 uiout->spaces (1);
8122 }
8123
8124 if (uiout->is_mi_like_p ())
8125 uiout->field_string ("catch-type", "fork");
8126 }
8127
8128 /* Implement the "print_mention" breakpoint_ops method for fork
8129 catchpoints. */
8130
8131 static void
8132 print_mention_catch_fork (struct breakpoint *b)
8133 {
8134 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8135 }
8136
8137 /* Implement the "print_recreate" breakpoint_ops method for fork
8138 catchpoints. */
8139
8140 static void
8141 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8142 {
8143 fprintf_unfiltered (fp, "catch fork");
8144 print_recreate_thread (b, fp);
8145 }
8146
8147 /* The breakpoint_ops structure to be used in fork catchpoints. */
8148
8149 static struct breakpoint_ops catch_fork_breakpoint_ops;
8150
8151 /* Implement the "insert" breakpoint_ops method for vfork
8152 catchpoints. */
8153
8154 static int
8155 insert_catch_vfork (struct bp_location *bl)
8156 {
8157 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8158 }
8159
8160 /* Implement the "remove" breakpoint_ops method for vfork
8161 catchpoints. */
8162
8163 static int
8164 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8165 {
8166 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8167 }
8168
8169 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8170 catchpoints. */
8171
8172 static int
8173 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8174 struct address_space *aspace, CORE_ADDR bp_addr,
8175 const struct target_waitstatus *ws)
8176 {
8177 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8178
8179 if (ws->kind != TARGET_WAITKIND_VFORKED)
8180 return 0;
8181
8182 c->forked_inferior_pid = ws->value.related_pid;
8183 return 1;
8184 }
8185
8186 /* Implement the "print_it" breakpoint_ops method for vfork
8187 catchpoints. */
8188
8189 static enum print_stop_action
8190 print_it_catch_vfork (bpstat bs)
8191 {
8192 struct ui_out *uiout = current_uiout;
8193 struct breakpoint *b = bs->breakpoint_at;
8194 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8195
8196 annotate_catchpoint (b->number);
8197 maybe_print_thread_hit_breakpoint (uiout);
8198 if (b->disposition == disp_del)
8199 uiout->text ("Temporary catchpoint ");
8200 else
8201 uiout->text ("Catchpoint ");
8202 if (uiout->is_mi_like_p ())
8203 {
8204 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8205 uiout->field_string ("disp", bpdisp_text (b->disposition));
8206 }
8207 uiout->field_int ("bkptno", b->number);
8208 uiout->text (" (vforked process ");
8209 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8210 uiout->text ("), ");
8211 return PRINT_SRC_AND_LOC;
8212 }
8213
8214 /* Implement the "print_one" breakpoint_ops method for vfork
8215 catchpoints. */
8216
8217 static void
8218 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8219 {
8220 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8221 struct value_print_options opts;
8222 struct ui_out *uiout = current_uiout;
8223
8224 get_user_print_options (&opts);
8225 /* Field 4, the address, is omitted (which makes the columns not
8226 line up too nicely with the headers, but the effect is relatively
8227 readable). */
8228 if (opts.addressprint)
8229 uiout->field_skip ("addr");
8230 annotate_field (5);
8231 uiout->text ("vfork");
8232 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8233 {
8234 uiout->text (", process ");
8235 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8236 uiout->spaces (1);
8237 }
8238
8239 if (uiout->is_mi_like_p ())
8240 uiout->field_string ("catch-type", "vfork");
8241 }
8242
8243 /* Implement the "print_mention" breakpoint_ops method for vfork
8244 catchpoints. */
8245
8246 static void
8247 print_mention_catch_vfork (struct breakpoint *b)
8248 {
8249 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8250 }
8251
8252 /* Implement the "print_recreate" breakpoint_ops method for vfork
8253 catchpoints. */
8254
8255 static void
8256 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8257 {
8258 fprintf_unfiltered (fp, "catch vfork");
8259 print_recreate_thread (b, fp);
8260 }
8261
8262 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8263
8264 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8265
8266 /* An instance of this type is used to represent an solib catchpoint.
8267 It includes a "struct breakpoint" as a kind of base class; users
8268 downcast to "struct breakpoint *" when needed. A breakpoint is
8269 really of this type iff its ops pointer points to
8270 CATCH_SOLIB_BREAKPOINT_OPS. */
8271
8272 struct solib_catchpoint
8273 {
8274 /* The base class. */
8275 struct breakpoint base;
8276
8277 /* True for "catch load", false for "catch unload". */
8278 unsigned char is_load;
8279
8280 /* Regular expression to match, if any. COMPILED is only valid when
8281 REGEX is non-NULL. */
8282 char *regex;
8283 regex_t compiled;
8284 };
8285
8286 static void
8287 dtor_catch_solib (struct breakpoint *b)
8288 {
8289 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8290
8291 if (self->regex)
8292 regfree (&self->compiled);
8293 xfree (self->regex);
8294
8295 base_breakpoint_ops.dtor (b);
8296 }
8297
8298 static int
8299 insert_catch_solib (struct bp_location *ignore)
8300 {
8301 return 0;
8302 }
8303
8304 static int
8305 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8306 {
8307 return 0;
8308 }
8309
8310 static int
8311 breakpoint_hit_catch_solib (const struct bp_location *bl,
8312 struct address_space *aspace,
8313 CORE_ADDR bp_addr,
8314 const struct target_waitstatus *ws)
8315 {
8316 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8317 struct breakpoint *other;
8318
8319 if (ws->kind == TARGET_WAITKIND_LOADED)
8320 return 1;
8321
8322 ALL_BREAKPOINTS (other)
8323 {
8324 struct bp_location *other_bl;
8325
8326 if (other == bl->owner)
8327 continue;
8328
8329 if (other->type != bp_shlib_event)
8330 continue;
8331
8332 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8333 continue;
8334
8335 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8336 {
8337 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8338 return 1;
8339 }
8340 }
8341
8342 return 0;
8343 }
8344
8345 static void
8346 check_status_catch_solib (struct bpstats *bs)
8347 {
8348 struct solib_catchpoint *self
8349 = (struct solib_catchpoint *) bs->breakpoint_at;
8350 int ix;
8351
8352 if (self->is_load)
8353 {
8354 struct so_list *iter;
8355
8356 for (ix = 0;
8357 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8358 ix, iter);
8359 ++ix)
8360 {
8361 if (!self->regex
8362 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8363 return;
8364 }
8365 }
8366 else
8367 {
8368 char *iter;
8369
8370 for (ix = 0;
8371 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8372 ix, iter);
8373 ++ix)
8374 {
8375 if (!self->regex
8376 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8377 return;
8378 }
8379 }
8380
8381 bs->stop = 0;
8382 bs->print_it = print_it_noop;
8383 }
8384
8385 static enum print_stop_action
8386 print_it_catch_solib (bpstat bs)
8387 {
8388 struct breakpoint *b = bs->breakpoint_at;
8389 struct ui_out *uiout = current_uiout;
8390
8391 annotate_catchpoint (b->number);
8392 maybe_print_thread_hit_breakpoint (uiout);
8393 if (b->disposition == disp_del)
8394 uiout->text ("Temporary catchpoint ");
8395 else
8396 uiout->text ("Catchpoint ");
8397 uiout->field_int ("bkptno", b->number);
8398 uiout->text ("\n");
8399 if (uiout->is_mi_like_p ())
8400 uiout->field_string ("disp", bpdisp_text (b->disposition));
8401 print_solib_event (1);
8402 return PRINT_SRC_AND_LOC;
8403 }
8404
8405 static void
8406 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8407 {
8408 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8409 struct value_print_options opts;
8410 struct ui_out *uiout = current_uiout;
8411 char *msg;
8412
8413 get_user_print_options (&opts);
8414 /* Field 4, the address, is omitted (which makes the columns not
8415 line up too nicely with the headers, but the effect is relatively
8416 readable). */
8417 if (opts.addressprint)
8418 {
8419 annotate_field (4);
8420 uiout->field_skip ("addr");
8421 }
8422
8423 annotate_field (5);
8424 if (self->is_load)
8425 {
8426 if (self->regex)
8427 msg = xstrprintf (_("load of library matching %s"), self->regex);
8428 else
8429 msg = xstrdup (_("load of library"));
8430 }
8431 else
8432 {
8433 if (self->regex)
8434 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8435 else
8436 msg = xstrdup (_("unload of library"));
8437 }
8438 uiout->field_string ("what", msg);
8439 xfree (msg);
8440
8441 if (uiout->is_mi_like_p ())
8442 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8443 }
8444
8445 static void
8446 print_mention_catch_solib (struct breakpoint *b)
8447 {
8448 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8449
8450 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8451 self->is_load ? "load" : "unload");
8452 }
8453
8454 static void
8455 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8456 {
8457 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8458
8459 fprintf_unfiltered (fp, "%s %s",
8460 b->disposition == disp_del ? "tcatch" : "catch",
8461 self->is_load ? "load" : "unload");
8462 if (self->regex)
8463 fprintf_unfiltered (fp, " %s", self->regex);
8464 fprintf_unfiltered (fp, "\n");
8465 }
8466
8467 static struct breakpoint_ops catch_solib_breakpoint_ops;
8468
8469 /* Shared helper function (MI and CLI) for creating and installing
8470 a shared object event catchpoint. If IS_LOAD is non-zero then
8471 the events to be caught are load events, otherwise they are
8472 unload events. If IS_TEMP is non-zero the catchpoint is a
8473 temporary one. If ENABLED is non-zero the catchpoint is
8474 created in an enabled state. */
8475
8476 void
8477 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8478 {
8479 struct solib_catchpoint *c;
8480 struct gdbarch *gdbarch = get_current_arch ();
8481 struct cleanup *cleanup;
8482
8483 if (!arg)
8484 arg = "";
8485 arg = skip_spaces_const (arg);
8486
8487 c = new solib_catchpoint ();
8488 cleanup = make_cleanup (xfree, c);
8489
8490 if (*arg != '\0')
8491 {
8492 int errcode;
8493
8494 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8495 if (errcode != 0)
8496 {
8497 char *err = get_regcomp_error (errcode, &c->compiled);
8498
8499 make_cleanup (xfree, err);
8500 error (_("Invalid regexp (%s): %s"), err, arg);
8501 }
8502 c->regex = xstrdup (arg);
8503 }
8504
8505 c->is_load = is_load;
8506 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8507 &catch_solib_breakpoint_ops);
8508
8509 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8510
8511 discard_cleanups (cleanup);
8512 install_breakpoint (0, &c->base, 1);
8513 }
8514
8515 /* A helper function that does all the work for "catch load" and
8516 "catch unload". */
8517
8518 static void
8519 catch_load_or_unload (char *arg, int from_tty, int is_load,
8520 struct cmd_list_element *command)
8521 {
8522 int tempflag;
8523 const int enabled = 1;
8524
8525 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8526
8527 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8528 }
8529
8530 static void
8531 catch_load_command_1 (char *arg, int from_tty,
8532 struct cmd_list_element *command)
8533 {
8534 catch_load_or_unload (arg, from_tty, 1, command);
8535 }
8536
8537 static void
8538 catch_unload_command_1 (char *arg, int from_tty,
8539 struct cmd_list_element *command)
8540 {
8541 catch_load_or_unload (arg, from_tty, 0, command);
8542 }
8543
8544 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8545 is non-zero, then make the breakpoint temporary. If COND_STRING is
8546 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8547 the breakpoint_ops structure associated to the catchpoint. */
8548
8549 void
8550 init_catchpoint (struct breakpoint *b,
8551 struct gdbarch *gdbarch, int tempflag,
8552 const char *cond_string,
8553 const struct breakpoint_ops *ops)
8554 {
8555 struct symtab_and_line sal;
8556
8557 init_sal (&sal);
8558 sal.pspace = current_program_space;
8559
8560 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8561
8562 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8563 b->disposition = tempflag ? disp_del : disp_donttouch;
8564 }
8565
8566 void
8567 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8568 {
8569 add_to_breakpoint_chain (b);
8570 set_breakpoint_number (internal, b);
8571 if (is_tracepoint (b))
8572 set_tracepoint_count (breakpoint_count);
8573 if (!internal)
8574 mention (b);
8575 observer_notify_breakpoint_created (b);
8576
8577 if (update_gll)
8578 update_global_location_list (UGLL_MAY_INSERT);
8579 }
8580
8581 static void
8582 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8583 int tempflag, const char *cond_string,
8584 const struct breakpoint_ops *ops)
8585 {
8586 struct fork_catchpoint *c = new fork_catchpoint ();
8587
8588 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8589
8590 c->forked_inferior_pid = null_ptid;
8591
8592 install_breakpoint (0, &c->base, 1);
8593 }
8594
8595 /* Exec catchpoints. */
8596
8597 /* An instance of this type is used to represent an exec catchpoint.
8598 It includes a "struct breakpoint" as a kind of base class; users
8599 downcast to "struct breakpoint *" when needed. A breakpoint is
8600 really of this type iff its ops pointer points to
8601 CATCH_EXEC_BREAKPOINT_OPS. */
8602
8603 struct exec_catchpoint
8604 {
8605 /* The base class. */
8606 struct breakpoint base;
8607
8608 /* Filename of a program whose exec triggered this catchpoint.
8609 This field is only valid immediately after this catchpoint has
8610 triggered. */
8611 char *exec_pathname;
8612 };
8613
8614 /* Implement the "dtor" breakpoint_ops method for exec
8615 catchpoints. */
8616
8617 static void
8618 dtor_catch_exec (struct breakpoint *b)
8619 {
8620 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8621
8622 xfree (c->exec_pathname);
8623
8624 base_breakpoint_ops.dtor (b);
8625 }
8626
8627 static int
8628 insert_catch_exec (struct bp_location *bl)
8629 {
8630 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8631 }
8632
8633 static int
8634 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8635 {
8636 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8637 }
8638
8639 static int
8640 breakpoint_hit_catch_exec (const struct bp_location *bl,
8641 struct address_space *aspace, CORE_ADDR bp_addr,
8642 const struct target_waitstatus *ws)
8643 {
8644 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8645
8646 if (ws->kind != TARGET_WAITKIND_EXECD)
8647 return 0;
8648
8649 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8650 return 1;
8651 }
8652
8653 static enum print_stop_action
8654 print_it_catch_exec (bpstat bs)
8655 {
8656 struct ui_out *uiout = current_uiout;
8657 struct breakpoint *b = bs->breakpoint_at;
8658 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8659
8660 annotate_catchpoint (b->number);
8661 maybe_print_thread_hit_breakpoint (uiout);
8662 if (b->disposition == disp_del)
8663 uiout->text ("Temporary catchpoint ");
8664 else
8665 uiout->text ("Catchpoint ");
8666 if (uiout->is_mi_like_p ())
8667 {
8668 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8669 uiout->field_string ("disp", bpdisp_text (b->disposition));
8670 }
8671 uiout->field_int ("bkptno", b->number);
8672 uiout->text (" (exec'd ");
8673 uiout->field_string ("new-exec", c->exec_pathname);
8674 uiout->text ("), ");
8675
8676 return PRINT_SRC_AND_LOC;
8677 }
8678
8679 static void
8680 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8681 {
8682 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8683 struct value_print_options opts;
8684 struct ui_out *uiout = current_uiout;
8685
8686 get_user_print_options (&opts);
8687
8688 /* Field 4, the address, is omitted (which makes the columns
8689 not line up too nicely with the headers, but the effect
8690 is relatively readable). */
8691 if (opts.addressprint)
8692 uiout->field_skip ("addr");
8693 annotate_field (5);
8694 uiout->text ("exec");
8695 if (c->exec_pathname != NULL)
8696 {
8697 uiout->text (", program \"");
8698 uiout->field_string ("what", c->exec_pathname);
8699 uiout->text ("\" ");
8700 }
8701
8702 if (uiout->is_mi_like_p ())
8703 uiout->field_string ("catch-type", "exec");
8704 }
8705
8706 static void
8707 print_mention_catch_exec (struct breakpoint *b)
8708 {
8709 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8710 }
8711
8712 /* Implement the "print_recreate" breakpoint_ops method for exec
8713 catchpoints. */
8714
8715 static void
8716 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8717 {
8718 fprintf_unfiltered (fp, "catch exec");
8719 print_recreate_thread (b, fp);
8720 }
8721
8722 static struct breakpoint_ops catch_exec_breakpoint_ops;
8723
8724 static int
8725 hw_breakpoint_used_count (void)
8726 {
8727 int i = 0;
8728 struct breakpoint *b;
8729 struct bp_location *bl;
8730
8731 ALL_BREAKPOINTS (b)
8732 {
8733 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8734 for (bl = b->loc; bl; bl = bl->next)
8735 {
8736 /* Special types of hardware breakpoints may use more than
8737 one register. */
8738 i += b->ops->resources_needed (bl);
8739 }
8740 }
8741
8742 return i;
8743 }
8744
8745 /* Returns the resources B would use if it were a hardware
8746 watchpoint. */
8747
8748 static int
8749 hw_watchpoint_use_count (struct breakpoint *b)
8750 {
8751 int i = 0;
8752 struct bp_location *bl;
8753
8754 if (!breakpoint_enabled (b))
8755 return 0;
8756
8757 for (bl = b->loc; bl; bl = bl->next)
8758 {
8759 /* Special types of hardware watchpoints may use more than
8760 one register. */
8761 i += b->ops->resources_needed (bl);
8762 }
8763
8764 return i;
8765 }
8766
8767 /* Returns the sum the used resources of all hardware watchpoints of
8768 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8769 the sum of the used resources of all hardware watchpoints of other
8770 types _not_ TYPE. */
8771
8772 static int
8773 hw_watchpoint_used_count_others (struct breakpoint *except,
8774 enum bptype type, int *other_type_used)
8775 {
8776 int i = 0;
8777 struct breakpoint *b;
8778
8779 *other_type_used = 0;
8780 ALL_BREAKPOINTS (b)
8781 {
8782 if (b == except)
8783 continue;
8784 if (!breakpoint_enabled (b))
8785 continue;
8786
8787 if (b->type == type)
8788 i += hw_watchpoint_use_count (b);
8789 else if (is_hardware_watchpoint (b))
8790 *other_type_used = 1;
8791 }
8792
8793 return i;
8794 }
8795
8796 void
8797 disable_watchpoints_before_interactive_call_start (void)
8798 {
8799 struct breakpoint *b;
8800
8801 ALL_BREAKPOINTS (b)
8802 {
8803 if (is_watchpoint (b) && breakpoint_enabled (b))
8804 {
8805 b->enable_state = bp_call_disabled;
8806 update_global_location_list (UGLL_DONT_INSERT);
8807 }
8808 }
8809 }
8810
8811 void
8812 enable_watchpoints_after_interactive_call_stop (void)
8813 {
8814 struct breakpoint *b;
8815
8816 ALL_BREAKPOINTS (b)
8817 {
8818 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8819 {
8820 b->enable_state = bp_enabled;
8821 update_global_location_list (UGLL_MAY_INSERT);
8822 }
8823 }
8824 }
8825
8826 void
8827 disable_breakpoints_before_startup (void)
8828 {
8829 current_program_space->executing_startup = 1;
8830 update_global_location_list (UGLL_DONT_INSERT);
8831 }
8832
8833 void
8834 enable_breakpoints_after_startup (void)
8835 {
8836 current_program_space->executing_startup = 0;
8837 breakpoint_re_set ();
8838 }
8839
8840 /* Create a new single-step breakpoint for thread THREAD, with no
8841 locations. */
8842
8843 static struct breakpoint *
8844 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8845 {
8846 struct breakpoint *b = new breakpoint ();
8847
8848 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8849 &momentary_breakpoint_ops);
8850
8851 b->disposition = disp_donttouch;
8852 b->frame_id = null_frame_id;
8853
8854 b->thread = thread;
8855 gdb_assert (b->thread != 0);
8856
8857 add_to_breakpoint_chain (b);
8858
8859 return b;
8860 }
8861
8862 /* Set a momentary breakpoint of type TYPE at address specified by
8863 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8864 frame. */
8865
8866 struct breakpoint *
8867 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8868 struct frame_id frame_id, enum bptype type)
8869 {
8870 struct breakpoint *b;
8871
8872 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8873 tail-called one. */
8874 gdb_assert (!frame_id_artificial_p (frame_id));
8875
8876 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8877 b->enable_state = bp_enabled;
8878 b->disposition = disp_donttouch;
8879 b->frame_id = frame_id;
8880
8881 /* If we're debugging a multi-threaded program, then we want
8882 momentary breakpoints to be active in only a single thread of
8883 control. */
8884 if (in_thread_list (inferior_ptid))
8885 b->thread = ptid_to_global_thread_id (inferior_ptid);
8886
8887 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8888
8889 return b;
8890 }
8891
8892 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8893 The new breakpoint will have type TYPE, use OPS as its
8894 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8895
8896 static struct breakpoint *
8897 momentary_breakpoint_from_master (struct breakpoint *orig,
8898 enum bptype type,
8899 const struct breakpoint_ops *ops,
8900 int loc_enabled)
8901 {
8902 struct breakpoint *copy;
8903
8904 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8905 copy->loc = allocate_bp_location (copy);
8906 set_breakpoint_location_function (copy->loc, 1);
8907
8908 copy->loc->gdbarch = orig->loc->gdbarch;
8909 copy->loc->requested_address = orig->loc->requested_address;
8910 copy->loc->address = orig->loc->address;
8911 copy->loc->section = orig->loc->section;
8912 copy->loc->pspace = orig->loc->pspace;
8913 copy->loc->probe = orig->loc->probe;
8914 copy->loc->line_number = orig->loc->line_number;
8915 copy->loc->symtab = orig->loc->symtab;
8916 copy->loc->enabled = loc_enabled;
8917 copy->frame_id = orig->frame_id;
8918 copy->thread = orig->thread;
8919 copy->pspace = orig->pspace;
8920
8921 copy->enable_state = bp_enabled;
8922 copy->disposition = disp_donttouch;
8923 copy->number = internal_breakpoint_number--;
8924
8925 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8926 return copy;
8927 }
8928
8929 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8930 ORIG is NULL. */
8931
8932 struct breakpoint *
8933 clone_momentary_breakpoint (struct breakpoint *orig)
8934 {
8935 /* If there's nothing to clone, then return nothing. */
8936 if (orig == NULL)
8937 return NULL;
8938
8939 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8940 }
8941
8942 struct breakpoint *
8943 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8944 enum bptype type)
8945 {
8946 struct symtab_and_line sal;
8947
8948 sal = find_pc_line (pc, 0);
8949 sal.pc = pc;
8950 sal.section = find_pc_overlay (pc);
8951 sal.explicit_pc = 1;
8952
8953 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8954 }
8955 \f
8956
8957 /* Tell the user we have just set a breakpoint B. */
8958
8959 static void
8960 mention (struct breakpoint *b)
8961 {
8962 b->ops->print_mention (b);
8963 if (current_uiout->is_mi_like_p ())
8964 return;
8965 printf_filtered ("\n");
8966 }
8967 \f
8968
8969 static int bp_loc_is_permanent (struct bp_location *loc);
8970
8971 static struct bp_location *
8972 add_location_to_breakpoint (struct breakpoint *b,
8973 const struct symtab_and_line *sal)
8974 {
8975 struct bp_location *loc, **tmp;
8976 CORE_ADDR adjusted_address;
8977 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8978
8979 if (loc_gdbarch == NULL)
8980 loc_gdbarch = b->gdbarch;
8981
8982 /* Adjust the breakpoint's address prior to allocating a location.
8983 Once we call allocate_bp_location(), that mostly uninitialized
8984 location will be placed on the location chain. Adjustment of the
8985 breakpoint may cause target_read_memory() to be called and we do
8986 not want its scan of the location chain to find a breakpoint and
8987 location that's only been partially initialized. */
8988 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8989 sal->pc, b->type);
8990
8991 /* Sort the locations by their ADDRESS. */
8992 loc = allocate_bp_location (b);
8993 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8994 tmp = &((*tmp)->next))
8995 ;
8996 loc->next = *tmp;
8997 *tmp = loc;
8998
8999 loc->requested_address = sal->pc;
9000 loc->address = adjusted_address;
9001 loc->pspace = sal->pspace;
9002 loc->probe.probe = sal->probe;
9003 loc->probe.objfile = sal->objfile;
9004 gdb_assert (loc->pspace != NULL);
9005 loc->section = sal->section;
9006 loc->gdbarch = loc_gdbarch;
9007 loc->line_number = sal->line;
9008 loc->symtab = sal->symtab;
9009
9010 set_breakpoint_location_function (loc,
9011 sal->explicit_pc || sal->explicit_line);
9012
9013 /* While by definition, permanent breakpoints are already present in the
9014 code, we don't mark the location as inserted. Normally one would expect
9015 that GDB could rely on that breakpoint instruction to stop the program,
9016 thus removing the need to insert its own breakpoint, except that executing
9017 the breakpoint instruction can kill the target instead of reporting a
9018 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9019 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9020 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9021 breakpoint be inserted normally results in QEMU knowing about the GDB
9022 breakpoint, and thus trap before the breakpoint instruction is executed.
9023 (If GDB later needs to continue execution past the permanent breakpoint,
9024 it manually increments the PC, thus avoiding executing the breakpoint
9025 instruction.) */
9026 if (bp_loc_is_permanent (loc))
9027 loc->permanent = 1;
9028
9029 return loc;
9030 }
9031 \f
9032
9033 /* See breakpoint.h. */
9034
9035 int
9036 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9037 {
9038 int len;
9039 CORE_ADDR addr;
9040 const gdb_byte *bpoint;
9041 gdb_byte *target_mem;
9042 struct cleanup *cleanup;
9043 int retval = 0;
9044
9045 addr = address;
9046 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9047
9048 /* Software breakpoints unsupported? */
9049 if (bpoint == NULL)
9050 return 0;
9051
9052 target_mem = (gdb_byte *) alloca (len);
9053
9054 /* Enable the automatic memory restoration from breakpoints while
9055 we read the memory. Otherwise we could say about our temporary
9056 breakpoints they are permanent. */
9057 cleanup = make_show_memory_breakpoints_cleanup (0);
9058
9059 if (target_read_memory (address, target_mem, len) == 0
9060 && memcmp (target_mem, bpoint, len) == 0)
9061 retval = 1;
9062
9063 do_cleanups (cleanup);
9064
9065 return retval;
9066 }
9067
9068 /* Return 1 if LOC is pointing to a permanent breakpoint,
9069 return 0 otherwise. */
9070
9071 static int
9072 bp_loc_is_permanent (struct bp_location *loc)
9073 {
9074 struct cleanup *cleanup;
9075 int retval;
9076
9077 gdb_assert (loc != NULL);
9078
9079 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9080 attempt to read from the addresses the locations of these breakpoint types
9081 point to. program_breakpoint_here_p, below, will attempt to read
9082 memory. */
9083 if (!breakpoint_address_is_meaningful (loc->owner))
9084 return 0;
9085
9086 cleanup = save_current_space_and_thread ();
9087 switch_to_program_space_and_thread (loc->pspace);
9088
9089 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9090
9091 do_cleanups (cleanup);
9092
9093 return retval;
9094 }
9095
9096 /* Build a command list for the dprintf corresponding to the current
9097 settings of the dprintf style options. */
9098
9099 static void
9100 update_dprintf_command_list (struct breakpoint *b)
9101 {
9102 char *dprintf_args = b->extra_string;
9103 char *printf_line = NULL;
9104
9105 if (!dprintf_args)
9106 return;
9107
9108 dprintf_args = skip_spaces (dprintf_args);
9109
9110 /* Allow a comma, as it may have terminated a location, but don't
9111 insist on it. */
9112 if (*dprintf_args == ',')
9113 ++dprintf_args;
9114 dprintf_args = skip_spaces (dprintf_args);
9115
9116 if (*dprintf_args != '"')
9117 error (_("Bad format string, missing '\"'."));
9118
9119 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9120 printf_line = xstrprintf ("printf %s", dprintf_args);
9121 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9122 {
9123 if (!dprintf_function)
9124 error (_("No function supplied for dprintf call"));
9125
9126 if (dprintf_channel && strlen (dprintf_channel) > 0)
9127 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9128 dprintf_function,
9129 dprintf_channel,
9130 dprintf_args);
9131 else
9132 printf_line = xstrprintf ("call (void) %s (%s)",
9133 dprintf_function,
9134 dprintf_args);
9135 }
9136 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9137 {
9138 if (target_can_run_breakpoint_commands ())
9139 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9140 else
9141 {
9142 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9143 printf_line = xstrprintf ("printf %s", dprintf_args);
9144 }
9145 }
9146 else
9147 internal_error (__FILE__, __LINE__,
9148 _("Invalid dprintf style."));
9149
9150 gdb_assert (printf_line != NULL);
9151 /* Manufacture a printf sequence. */
9152 {
9153 struct command_line *printf_cmd_line = XNEW (struct command_line);
9154
9155 printf_cmd_line->control_type = simple_control;
9156 printf_cmd_line->body_count = 0;
9157 printf_cmd_line->body_list = NULL;
9158 printf_cmd_line->next = NULL;
9159 printf_cmd_line->line = printf_line;
9160
9161 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9162 }
9163 }
9164
9165 /* Update all dprintf commands, making their command lists reflect
9166 current style settings. */
9167
9168 static void
9169 update_dprintf_commands (char *args, int from_tty,
9170 struct cmd_list_element *c)
9171 {
9172 struct breakpoint *b;
9173
9174 ALL_BREAKPOINTS (b)
9175 {
9176 if (b->type == bp_dprintf)
9177 update_dprintf_command_list (b);
9178 }
9179 }
9180
9181 /* Create a breakpoint with SAL as location. Use LOCATION
9182 as a description of the location, and COND_STRING
9183 as condition expression. If LOCATION is NULL then create an
9184 "address location" from the address in the SAL. */
9185
9186 static void
9187 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9188 struct symtabs_and_lines sals,
9189 event_location_up &&location,
9190 char *filter, char *cond_string,
9191 char *extra_string,
9192 enum bptype type, enum bpdisp disposition,
9193 int thread, int task, int ignore_count,
9194 const struct breakpoint_ops *ops, int from_tty,
9195 int enabled, int internal, unsigned flags,
9196 int display_canonical)
9197 {
9198 int i;
9199
9200 if (type == bp_hardware_breakpoint)
9201 {
9202 int target_resources_ok;
9203
9204 i = hw_breakpoint_used_count ();
9205 target_resources_ok =
9206 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9207 i + 1, 0);
9208 if (target_resources_ok == 0)
9209 error (_("No hardware breakpoint support in the target."));
9210 else if (target_resources_ok < 0)
9211 error (_("Hardware breakpoints used exceeds limit."));
9212 }
9213
9214 gdb_assert (sals.nelts > 0);
9215
9216 for (i = 0; i < sals.nelts; ++i)
9217 {
9218 struct symtab_and_line sal = sals.sals[i];
9219 struct bp_location *loc;
9220
9221 if (from_tty)
9222 {
9223 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9224 if (!loc_gdbarch)
9225 loc_gdbarch = gdbarch;
9226
9227 describe_other_breakpoints (loc_gdbarch,
9228 sal.pspace, sal.pc, sal.section, thread);
9229 }
9230
9231 if (i == 0)
9232 {
9233 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9234 b->thread = thread;
9235 b->task = task;
9236
9237 b->cond_string = cond_string;
9238 b->extra_string = extra_string;
9239 b->ignore_count = ignore_count;
9240 b->enable_state = enabled ? bp_enabled : bp_disabled;
9241 b->disposition = disposition;
9242
9243 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9244 b->loc->inserted = 1;
9245
9246 if (type == bp_static_tracepoint)
9247 {
9248 struct tracepoint *t = (struct tracepoint *) b;
9249 struct static_tracepoint_marker marker;
9250
9251 if (strace_marker_p (b))
9252 {
9253 /* We already know the marker exists, otherwise, we
9254 wouldn't see a sal for it. */
9255 const char *p
9256 = &event_location_to_string (b->location.get ())[3];
9257 const char *endp;
9258 char *marker_str;
9259
9260 p = skip_spaces_const (p);
9261
9262 endp = skip_to_space_const (p);
9263
9264 marker_str = savestring (p, endp - p);
9265 t->static_trace_marker_id = marker_str;
9266
9267 printf_filtered (_("Probed static tracepoint "
9268 "marker \"%s\"\n"),
9269 t->static_trace_marker_id);
9270 }
9271 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9272 {
9273 t->static_trace_marker_id = xstrdup (marker.str_id);
9274 release_static_tracepoint_marker (&marker);
9275
9276 printf_filtered (_("Probed static tracepoint "
9277 "marker \"%s\"\n"),
9278 t->static_trace_marker_id);
9279 }
9280 else
9281 warning (_("Couldn't determine the static "
9282 "tracepoint marker to probe"));
9283 }
9284
9285 loc = b->loc;
9286 }
9287 else
9288 {
9289 loc = add_location_to_breakpoint (b, &sal);
9290 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9291 loc->inserted = 1;
9292 }
9293
9294 if (b->cond_string)
9295 {
9296 const char *arg = b->cond_string;
9297
9298 loc->cond = parse_exp_1 (&arg, loc->address,
9299 block_for_pc (loc->address), 0);
9300 if (*arg)
9301 error (_("Garbage '%s' follows condition"), arg);
9302 }
9303
9304 /* Dynamic printf requires and uses additional arguments on the
9305 command line, otherwise it's an error. */
9306 if (type == bp_dprintf)
9307 {
9308 if (b->extra_string)
9309 update_dprintf_command_list (b);
9310 else
9311 error (_("Format string required"));
9312 }
9313 else if (b->extra_string)
9314 error (_("Garbage '%s' at end of command"), b->extra_string);
9315 }
9316
9317 b->display_canonical = display_canonical;
9318 if (location != NULL)
9319 b->location = std::move (location);
9320 else
9321 b->location = new_address_location (b->loc->address, NULL, 0);
9322 b->filter = filter;
9323 }
9324
9325 static void
9326 create_breakpoint_sal (struct gdbarch *gdbarch,
9327 struct symtabs_and_lines sals,
9328 event_location_up &&location,
9329 char *filter, char *cond_string,
9330 char *extra_string,
9331 enum bptype type, enum bpdisp disposition,
9332 int thread, int task, int ignore_count,
9333 const struct breakpoint_ops *ops, int from_tty,
9334 int enabled, int internal, unsigned flags,
9335 int display_canonical)
9336 {
9337 struct breakpoint *b;
9338 struct cleanup *old_chain;
9339
9340 if (is_tracepoint_type (type))
9341 {
9342 struct tracepoint *t;
9343
9344 t = new tracepoint ();
9345 b = &t->base;
9346 }
9347 else
9348 b = new breakpoint ();
9349
9350 old_chain = make_cleanup (xfree, b);
9351
9352 init_breakpoint_sal (b, gdbarch,
9353 sals, std::move (location),
9354 filter, cond_string, extra_string,
9355 type, disposition,
9356 thread, task, ignore_count,
9357 ops, from_tty,
9358 enabled, internal, flags,
9359 display_canonical);
9360 discard_cleanups (old_chain);
9361
9362 install_breakpoint (internal, b, 0);
9363 }
9364
9365 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9366 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9367 value. COND_STRING, if not NULL, specified the condition to be
9368 used for all breakpoints. Essentially the only case where
9369 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9370 function. In that case, it's still not possible to specify
9371 separate conditions for different overloaded functions, so
9372 we take just a single condition string.
9373
9374 NOTE: If the function succeeds, the caller is expected to cleanup
9375 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9376 array contents). If the function fails (error() is called), the
9377 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9378 COND and SALS arrays and each of those arrays contents. */
9379
9380 static void
9381 create_breakpoints_sal (struct gdbarch *gdbarch,
9382 struct linespec_result *canonical,
9383 char *cond_string, char *extra_string,
9384 enum bptype type, enum bpdisp disposition,
9385 int thread, int task, int ignore_count,
9386 const struct breakpoint_ops *ops, int from_tty,
9387 int enabled, int internal, unsigned flags)
9388 {
9389 int i;
9390 struct linespec_sals *lsal;
9391
9392 if (canonical->pre_expanded)
9393 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9394
9395 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9396 {
9397 /* Note that 'location' can be NULL in the case of a plain
9398 'break', without arguments. */
9399 event_location_up location
9400 = (canonical->location != NULL
9401 ? copy_event_location (canonical->location.get ()) : NULL);
9402 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9403
9404 make_cleanup (xfree, filter_string);
9405 create_breakpoint_sal (gdbarch, lsal->sals,
9406 std::move (location),
9407 filter_string,
9408 cond_string, extra_string,
9409 type, disposition,
9410 thread, task, ignore_count, ops,
9411 from_tty, enabled, internal, flags,
9412 canonical->special_display);
9413 }
9414 }
9415
9416 /* Parse LOCATION which is assumed to be a SAL specification possibly
9417 followed by conditionals. On return, SALS contains an array of SAL
9418 addresses found. LOCATION points to the end of the SAL (for
9419 linespec locations).
9420
9421 The array and the line spec strings are allocated on the heap, it is
9422 the caller's responsibility to free them. */
9423
9424 static void
9425 parse_breakpoint_sals (const struct event_location *location,
9426 struct linespec_result *canonical)
9427 {
9428 struct symtab_and_line cursal;
9429
9430 if (event_location_type (location) == LINESPEC_LOCATION)
9431 {
9432 const char *address = get_linespec_location (location);
9433
9434 if (address == NULL)
9435 {
9436 /* The last displayed codepoint, if it's valid, is our default
9437 breakpoint address. */
9438 if (last_displayed_sal_is_valid ())
9439 {
9440 struct linespec_sals lsal;
9441 struct symtab_and_line sal;
9442 CORE_ADDR pc;
9443
9444 init_sal (&sal); /* Initialize to zeroes. */
9445 lsal.sals.sals = XNEW (struct symtab_and_line);
9446
9447 /* Set sal's pspace, pc, symtab, and line to the values
9448 corresponding to the last call to print_frame_info.
9449 Be sure to reinitialize LINE with NOTCURRENT == 0
9450 as the breakpoint line number is inappropriate otherwise.
9451 find_pc_line would adjust PC, re-set it back. */
9452 get_last_displayed_sal (&sal);
9453 pc = sal.pc;
9454 sal = find_pc_line (pc, 0);
9455
9456 /* "break" without arguments is equivalent to "break *PC"
9457 where PC is the last displayed codepoint's address. So
9458 make sure to set sal.explicit_pc to prevent GDB from
9459 trying to expand the list of sals to include all other
9460 instances with the same symtab and line. */
9461 sal.pc = pc;
9462 sal.explicit_pc = 1;
9463
9464 lsal.sals.sals[0] = sal;
9465 lsal.sals.nelts = 1;
9466 lsal.canonical = NULL;
9467
9468 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9469 return;
9470 }
9471 else
9472 error (_("No default breakpoint address now."));
9473 }
9474 }
9475
9476 /* Force almost all breakpoints to be in terms of the
9477 current_source_symtab (which is decode_line_1's default).
9478 This should produce the results we want almost all of the
9479 time while leaving default_breakpoint_* alone.
9480
9481 ObjC: However, don't match an Objective-C method name which
9482 may have a '+' or '-' succeeded by a '['. */
9483 cursal = get_current_source_symtab_and_line ();
9484 if (last_displayed_sal_is_valid ())
9485 {
9486 const char *address = NULL;
9487
9488 if (event_location_type (location) == LINESPEC_LOCATION)
9489 address = get_linespec_location (location);
9490
9491 if (!cursal.symtab
9492 || (address != NULL
9493 && strchr ("+-", address[0]) != NULL
9494 && address[1] != '['))
9495 {
9496 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9497 get_last_displayed_symtab (),
9498 get_last_displayed_line (),
9499 canonical, NULL, NULL);
9500 return;
9501 }
9502 }
9503
9504 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9505 cursal.symtab, cursal.line, canonical, NULL, NULL);
9506 }
9507
9508
9509 /* Convert each SAL into a real PC. Verify that the PC can be
9510 inserted as a breakpoint. If it can't throw an error. */
9511
9512 static void
9513 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9514 {
9515 int i;
9516
9517 for (i = 0; i < sals->nelts; i++)
9518 resolve_sal_pc (&sals->sals[i]);
9519 }
9520
9521 /* Fast tracepoints may have restrictions on valid locations. For
9522 instance, a fast tracepoint using a jump instead of a trap will
9523 likely have to overwrite more bytes than a trap would, and so can
9524 only be placed where the instruction is longer than the jump, or a
9525 multi-instruction sequence does not have a jump into the middle of
9526 it, etc. */
9527
9528 static void
9529 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9530 struct symtabs_and_lines *sals)
9531 {
9532 int i, rslt;
9533 struct symtab_and_line *sal;
9534 char *msg;
9535 struct cleanup *old_chain;
9536
9537 for (i = 0; i < sals->nelts; i++)
9538 {
9539 struct gdbarch *sarch;
9540
9541 sal = &sals->sals[i];
9542
9543 sarch = get_sal_arch (*sal);
9544 /* We fall back to GDBARCH if there is no architecture
9545 associated with SAL. */
9546 if (sarch == NULL)
9547 sarch = gdbarch;
9548 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9549 old_chain = make_cleanup (xfree, msg);
9550
9551 if (!rslt)
9552 error (_("May not have a fast tracepoint at %s%s"),
9553 paddress (sarch, sal->pc), (msg ? msg : ""));
9554
9555 do_cleanups (old_chain);
9556 }
9557 }
9558
9559 /* Given TOK, a string specification of condition and thread, as
9560 accepted by the 'break' command, extract the condition
9561 string and thread number and set *COND_STRING and *THREAD.
9562 PC identifies the context at which the condition should be parsed.
9563 If no condition is found, *COND_STRING is set to NULL.
9564 If no thread is found, *THREAD is set to -1. */
9565
9566 static void
9567 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9568 char **cond_string, int *thread, int *task,
9569 char **rest)
9570 {
9571 *cond_string = NULL;
9572 *thread = -1;
9573 *task = 0;
9574 *rest = NULL;
9575
9576 while (tok && *tok)
9577 {
9578 const char *end_tok;
9579 int toklen;
9580 const char *cond_start = NULL;
9581 const char *cond_end = NULL;
9582
9583 tok = skip_spaces_const (tok);
9584
9585 if ((*tok == '"' || *tok == ',') && rest)
9586 {
9587 *rest = savestring (tok, strlen (tok));
9588 return;
9589 }
9590
9591 end_tok = skip_to_space_const (tok);
9592
9593 toklen = end_tok - tok;
9594
9595 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9596 {
9597 tok = cond_start = end_tok + 1;
9598 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9599 cond_end = tok;
9600 *cond_string = savestring (cond_start, cond_end - cond_start);
9601 }
9602 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9603 {
9604 const char *tmptok;
9605 struct thread_info *thr;
9606
9607 tok = end_tok + 1;
9608 thr = parse_thread_id (tok, &tmptok);
9609 if (tok == tmptok)
9610 error (_("Junk after thread keyword."));
9611 *thread = thr->global_num;
9612 tok = tmptok;
9613 }
9614 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9615 {
9616 char *tmptok;
9617
9618 tok = end_tok + 1;
9619 *task = strtol (tok, &tmptok, 0);
9620 if (tok == tmptok)
9621 error (_("Junk after task keyword."));
9622 if (!valid_task_id (*task))
9623 error (_("Unknown task %d."), *task);
9624 tok = tmptok;
9625 }
9626 else if (rest)
9627 {
9628 *rest = savestring (tok, strlen (tok));
9629 return;
9630 }
9631 else
9632 error (_("Junk at end of arguments."));
9633 }
9634 }
9635
9636 /* Decode a static tracepoint marker spec. */
9637
9638 static struct symtabs_and_lines
9639 decode_static_tracepoint_spec (const char **arg_p)
9640 {
9641 VEC(static_tracepoint_marker_p) *markers = NULL;
9642 struct symtabs_and_lines sals;
9643 struct cleanup *old_chain;
9644 const char *p = &(*arg_p)[3];
9645 const char *endp;
9646 char *marker_str;
9647 int i;
9648
9649 p = skip_spaces_const (p);
9650
9651 endp = skip_to_space_const (p);
9652
9653 marker_str = savestring (p, endp - p);
9654 old_chain = make_cleanup (xfree, marker_str);
9655
9656 markers = target_static_tracepoint_markers_by_strid (marker_str);
9657 if (VEC_empty(static_tracepoint_marker_p, markers))
9658 error (_("No known static tracepoint marker named %s"), marker_str);
9659
9660 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9661 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9662
9663 for (i = 0; i < sals.nelts; i++)
9664 {
9665 struct static_tracepoint_marker *marker;
9666
9667 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9668
9669 init_sal (&sals.sals[i]);
9670
9671 sals.sals[i] = find_pc_line (marker->address, 0);
9672 sals.sals[i].pc = marker->address;
9673
9674 release_static_tracepoint_marker (marker);
9675 }
9676
9677 do_cleanups (old_chain);
9678
9679 *arg_p = endp;
9680 return sals;
9681 }
9682
9683 /* See breakpoint.h. */
9684
9685 int
9686 create_breakpoint (struct gdbarch *gdbarch,
9687 const struct event_location *location, char *cond_string,
9688 int thread, char *extra_string,
9689 int parse_extra,
9690 int tempflag, enum bptype type_wanted,
9691 int ignore_count,
9692 enum auto_boolean pending_break_support,
9693 const struct breakpoint_ops *ops,
9694 int from_tty, int enabled, int internal,
9695 unsigned flags)
9696 {
9697 struct linespec_result canonical;
9698 struct cleanup *bkpt_chain = NULL;
9699 int pending = 0;
9700 int task = 0;
9701 int prev_bkpt_count = breakpoint_count;
9702
9703 gdb_assert (ops != NULL);
9704
9705 /* If extra_string isn't useful, set it to NULL. */
9706 if (extra_string != NULL && *extra_string == '\0')
9707 extra_string = NULL;
9708
9709 TRY
9710 {
9711 ops->create_sals_from_location (location, &canonical, type_wanted);
9712 }
9713 CATCH (e, RETURN_MASK_ERROR)
9714 {
9715 /* If caller is interested in rc value from parse, set
9716 value. */
9717 if (e.error == NOT_FOUND_ERROR)
9718 {
9719 /* If pending breakpoint support is turned off, throw
9720 error. */
9721
9722 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9723 throw_exception (e);
9724
9725 exception_print (gdb_stderr, e);
9726
9727 /* If pending breakpoint support is auto query and the user
9728 selects no, then simply return the error code. */
9729 if (pending_break_support == AUTO_BOOLEAN_AUTO
9730 && !nquery (_("Make %s pending on future shared library load? "),
9731 bptype_string (type_wanted)))
9732 return 0;
9733
9734 /* At this point, either the user was queried about setting
9735 a pending breakpoint and selected yes, or pending
9736 breakpoint behavior is on and thus a pending breakpoint
9737 is defaulted on behalf of the user. */
9738 pending = 1;
9739 }
9740 else
9741 throw_exception (e);
9742 }
9743 END_CATCH
9744
9745 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9746 return 0;
9747
9748 /* ----------------------------- SNIP -----------------------------
9749 Anything added to the cleanup chain beyond this point is assumed
9750 to be part of a breakpoint. If the breakpoint create succeeds
9751 then the memory is not reclaimed. */
9752 bkpt_chain = make_cleanup (null_cleanup, 0);
9753
9754 /* Resolve all line numbers to PC's and verify that the addresses
9755 are ok for the target. */
9756 if (!pending)
9757 {
9758 int ix;
9759 struct linespec_sals *iter;
9760
9761 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9762 breakpoint_sals_to_pc (&iter->sals);
9763 }
9764
9765 /* Fast tracepoints may have additional restrictions on location. */
9766 if (!pending && type_wanted == bp_fast_tracepoint)
9767 {
9768 int ix;
9769 struct linespec_sals *iter;
9770
9771 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9772 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9773 }
9774
9775 /* Verify that condition can be parsed, before setting any
9776 breakpoints. Allocate a separate condition expression for each
9777 breakpoint. */
9778 if (!pending)
9779 {
9780 if (parse_extra)
9781 {
9782 char *rest;
9783 struct linespec_sals *lsal;
9784
9785 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9786
9787 /* Here we only parse 'arg' to separate condition
9788 from thread number, so parsing in context of first
9789 sal is OK. When setting the breakpoint we'll
9790 re-parse it in context of each sal. */
9791
9792 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9793 &cond_string, &thread, &task, &rest);
9794 if (cond_string)
9795 make_cleanup (xfree, cond_string);
9796 if (rest)
9797 make_cleanup (xfree, rest);
9798 if (rest)
9799 extra_string = rest;
9800 else
9801 extra_string = NULL;
9802 }
9803 else
9804 {
9805 if (type_wanted != bp_dprintf
9806 && extra_string != NULL && *extra_string != '\0')
9807 error (_("Garbage '%s' at end of location"), extra_string);
9808
9809 /* Create a private copy of condition string. */
9810 if (cond_string)
9811 {
9812 cond_string = xstrdup (cond_string);
9813 make_cleanup (xfree, cond_string);
9814 }
9815 /* Create a private copy of any extra string. */
9816 if (extra_string)
9817 {
9818 extra_string = xstrdup (extra_string);
9819 make_cleanup (xfree, extra_string);
9820 }
9821 }
9822
9823 ops->create_breakpoints_sal (gdbarch, &canonical,
9824 cond_string, extra_string, type_wanted,
9825 tempflag ? disp_del : disp_donttouch,
9826 thread, task, ignore_count, ops,
9827 from_tty, enabled, internal, flags);
9828 }
9829 else
9830 {
9831 struct breakpoint *b;
9832
9833 if (is_tracepoint_type (type_wanted))
9834 {
9835 struct tracepoint *t;
9836
9837 t = new tracepoint ();
9838 b = &t->base;
9839 }
9840 else
9841 b = new breakpoint ();
9842
9843 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9844 b->location = copy_event_location (location);
9845
9846 if (parse_extra)
9847 b->cond_string = NULL;
9848 else
9849 {
9850 /* Create a private copy of condition string. */
9851 if (cond_string)
9852 {
9853 cond_string = xstrdup (cond_string);
9854 make_cleanup (xfree, cond_string);
9855 }
9856 b->cond_string = cond_string;
9857 b->thread = thread;
9858 }
9859
9860 /* Create a private copy of any extra string. */
9861 if (extra_string != NULL)
9862 {
9863 extra_string = xstrdup (extra_string);
9864 make_cleanup (xfree, extra_string);
9865 }
9866 b->extra_string = extra_string;
9867 b->ignore_count = ignore_count;
9868 b->disposition = tempflag ? disp_del : disp_donttouch;
9869 b->condition_not_parsed = 1;
9870 b->enable_state = enabled ? bp_enabled : bp_disabled;
9871 if ((type_wanted != bp_breakpoint
9872 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9873 b->pspace = current_program_space;
9874
9875 install_breakpoint (internal, b, 0);
9876 }
9877
9878 if (VEC_length (linespec_sals, canonical.sals) > 1)
9879 {
9880 warning (_("Multiple breakpoints were set.\nUse the "
9881 "\"delete\" command to delete unwanted breakpoints."));
9882 prev_breakpoint_count = prev_bkpt_count;
9883 }
9884
9885 /* That's it. Discard the cleanups for data inserted into the
9886 breakpoint. */
9887 discard_cleanups (bkpt_chain);
9888
9889 /* error call may happen here - have BKPT_CHAIN already discarded. */
9890 update_global_location_list (UGLL_MAY_INSERT);
9891
9892 return 1;
9893 }
9894
9895 /* Set a breakpoint.
9896 ARG is a string describing breakpoint address,
9897 condition, and thread.
9898 FLAG specifies if a breakpoint is hardware on,
9899 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9900 and BP_TEMPFLAG. */
9901
9902 static void
9903 break_command_1 (char *arg, int flag, int from_tty)
9904 {
9905 int tempflag = flag & BP_TEMPFLAG;
9906 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9907 ? bp_hardware_breakpoint
9908 : bp_breakpoint);
9909 struct breakpoint_ops *ops;
9910
9911 event_location_up location = string_to_event_location (&arg, current_language);
9912
9913 /* Matching breakpoints on probes. */
9914 if (location != NULL
9915 && event_location_type (location.get ()) == PROBE_LOCATION)
9916 ops = &bkpt_probe_breakpoint_ops;
9917 else
9918 ops = &bkpt_breakpoint_ops;
9919
9920 create_breakpoint (get_current_arch (),
9921 location.get (),
9922 NULL, 0, arg, 1 /* parse arg */,
9923 tempflag, type_wanted,
9924 0 /* Ignore count */,
9925 pending_break_support,
9926 ops,
9927 from_tty,
9928 1 /* enabled */,
9929 0 /* internal */,
9930 0);
9931 }
9932
9933 /* Helper function for break_command_1 and disassemble_command. */
9934
9935 void
9936 resolve_sal_pc (struct symtab_and_line *sal)
9937 {
9938 CORE_ADDR pc;
9939
9940 if (sal->pc == 0 && sal->symtab != NULL)
9941 {
9942 if (!find_line_pc (sal->symtab, sal->line, &pc))
9943 error (_("No line %d in file \"%s\"."),
9944 sal->line, symtab_to_filename_for_display (sal->symtab));
9945 sal->pc = pc;
9946
9947 /* If this SAL corresponds to a breakpoint inserted using a line
9948 number, then skip the function prologue if necessary. */
9949 if (sal->explicit_line)
9950 skip_prologue_sal (sal);
9951 }
9952
9953 if (sal->section == 0 && sal->symtab != NULL)
9954 {
9955 const struct blockvector *bv;
9956 const struct block *b;
9957 struct symbol *sym;
9958
9959 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9960 SYMTAB_COMPUNIT (sal->symtab));
9961 if (bv != NULL)
9962 {
9963 sym = block_linkage_function (b);
9964 if (sym != NULL)
9965 {
9966 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9967 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9968 sym);
9969 }
9970 else
9971 {
9972 /* It really is worthwhile to have the section, so we'll
9973 just have to look harder. This case can be executed
9974 if we have line numbers but no functions (as can
9975 happen in assembly source). */
9976
9977 struct bound_minimal_symbol msym;
9978 struct cleanup *old_chain = save_current_space_and_thread ();
9979
9980 switch_to_program_space_and_thread (sal->pspace);
9981
9982 msym = lookup_minimal_symbol_by_pc (sal->pc);
9983 if (msym.minsym)
9984 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9985
9986 do_cleanups (old_chain);
9987 }
9988 }
9989 }
9990 }
9991
9992 void
9993 break_command (char *arg, int from_tty)
9994 {
9995 break_command_1 (arg, 0, from_tty);
9996 }
9997
9998 void
9999 tbreak_command (char *arg, int from_tty)
10000 {
10001 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10002 }
10003
10004 static void
10005 hbreak_command (char *arg, int from_tty)
10006 {
10007 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10008 }
10009
10010 static void
10011 thbreak_command (char *arg, int from_tty)
10012 {
10013 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10014 }
10015
10016 static void
10017 stop_command (char *arg, int from_tty)
10018 {
10019 printf_filtered (_("Specify the type of breakpoint to set.\n\
10020 Usage: stop in <function | address>\n\
10021 stop at <line>\n"));
10022 }
10023
10024 static void
10025 stopin_command (char *arg, int from_tty)
10026 {
10027 int badInput = 0;
10028
10029 if (arg == (char *) NULL)
10030 badInput = 1;
10031 else if (*arg != '*')
10032 {
10033 char *argptr = arg;
10034 int hasColon = 0;
10035
10036 /* Look for a ':'. If this is a line number specification, then
10037 say it is bad, otherwise, it should be an address or
10038 function/method name. */
10039 while (*argptr && !hasColon)
10040 {
10041 hasColon = (*argptr == ':');
10042 argptr++;
10043 }
10044
10045 if (hasColon)
10046 badInput = (*argptr != ':'); /* Not a class::method */
10047 else
10048 badInput = isdigit (*arg); /* a simple line number */
10049 }
10050
10051 if (badInput)
10052 printf_filtered (_("Usage: stop in <function | address>\n"));
10053 else
10054 break_command_1 (arg, 0, from_tty);
10055 }
10056
10057 static void
10058 stopat_command (char *arg, int from_tty)
10059 {
10060 int badInput = 0;
10061
10062 if (arg == (char *) NULL || *arg == '*') /* no line number */
10063 badInput = 1;
10064 else
10065 {
10066 char *argptr = arg;
10067 int hasColon = 0;
10068
10069 /* Look for a ':'. If there is a '::' then get out, otherwise
10070 it is probably a line number. */
10071 while (*argptr && !hasColon)
10072 {
10073 hasColon = (*argptr == ':');
10074 argptr++;
10075 }
10076
10077 if (hasColon)
10078 badInput = (*argptr == ':'); /* we have class::method */
10079 else
10080 badInput = !isdigit (*arg); /* not a line number */
10081 }
10082
10083 if (badInput)
10084 printf_filtered (_("Usage: stop at <line>\n"));
10085 else
10086 break_command_1 (arg, 0, from_tty);
10087 }
10088
10089 /* The dynamic printf command is mostly like a regular breakpoint, but
10090 with a prewired command list consisting of a single output command,
10091 built from extra arguments supplied on the dprintf command
10092 line. */
10093
10094 static void
10095 dprintf_command (char *arg, int from_tty)
10096 {
10097 event_location_up location = string_to_event_location (&arg, current_language);
10098
10099 /* If non-NULL, ARG should have been advanced past the location;
10100 the next character must be ','. */
10101 if (arg != NULL)
10102 {
10103 if (arg[0] != ',' || arg[1] == '\0')
10104 error (_("Format string required"));
10105 else
10106 {
10107 /* Skip the comma. */
10108 ++arg;
10109 }
10110 }
10111
10112 create_breakpoint (get_current_arch (),
10113 location.get (),
10114 NULL, 0, arg, 1 /* parse arg */,
10115 0, bp_dprintf,
10116 0 /* Ignore count */,
10117 pending_break_support,
10118 &dprintf_breakpoint_ops,
10119 from_tty,
10120 1 /* enabled */,
10121 0 /* internal */,
10122 0);
10123 }
10124
10125 static void
10126 agent_printf_command (char *arg, int from_tty)
10127 {
10128 error (_("May only run agent-printf on the target"));
10129 }
10130
10131 /* Implement the "breakpoint_hit" breakpoint_ops method for
10132 ranged breakpoints. */
10133
10134 static int
10135 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10136 struct address_space *aspace,
10137 CORE_ADDR bp_addr,
10138 const struct target_waitstatus *ws)
10139 {
10140 if (ws->kind != TARGET_WAITKIND_STOPPED
10141 || ws->value.sig != GDB_SIGNAL_TRAP)
10142 return 0;
10143
10144 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10145 bl->length, aspace, bp_addr);
10146 }
10147
10148 /* Implement the "resources_needed" breakpoint_ops method for
10149 ranged breakpoints. */
10150
10151 static int
10152 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10153 {
10154 return target_ranged_break_num_registers ();
10155 }
10156
10157 /* Implement the "print_it" breakpoint_ops method for
10158 ranged breakpoints. */
10159
10160 static enum print_stop_action
10161 print_it_ranged_breakpoint (bpstat bs)
10162 {
10163 struct breakpoint *b = bs->breakpoint_at;
10164 struct bp_location *bl = b->loc;
10165 struct ui_out *uiout = current_uiout;
10166
10167 gdb_assert (b->type == bp_hardware_breakpoint);
10168
10169 /* Ranged breakpoints have only one location. */
10170 gdb_assert (bl && bl->next == NULL);
10171
10172 annotate_breakpoint (b->number);
10173
10174 maybe_print_thread_hit_breakpoint (uiout);
10175
10176 if (b->disposition == disp_del)
10177 uiout->text ("Temporary ranged breakpoint ");
10178 else
10179 uiout->text ("Ranged breakpoint ");
10180 if (uiout->is_mi_like_p ())
10181 {
10182 uiout->field_string ("reason",
10183 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10184 uiout->field_string ("disp", bpdisp_text (b->disposition));
10185 }
10186 uiout->field_int ("bkptno", b->number);
10187 uiout->text (", ");
10188
10189 return PRINT_SRC_AND_LOC;
10190 }
10191
10192 /* Implement the "print_one" breakpoint_ops method for
10193 ranged breakpoints. */
10194
10195 static void
10196 print_one_ranged_breakpoint (struct breakpoint *b,
10197 struct bp_location **last_loc)
10198 {
10199 struct bp_location *bl = b->loc;
10200 struct value_print_options opts;
10201 struct ui_out *uiout = current_uiout;
10202
10203 /* Ranged breakpoints have only one location. */
10204 gdb_assert (bl && bl->next == NULL);
10205
10206 get_user_print_options (&opts);
10207
10208 if (opts.addressprint)
10209 /* We don't print the address range here, it will be printed later
10210 by print_one_detail_ranged_breakpoint. */
10211 uiout->field_skip ("addr");
10212 annotate_field (5);
10213 print_breakpoint_location (b, bl);
10214 *last_loc = bl;
10215 }
10216
10217 /* Implement the "print_one_detail" breakpoint_ops method for
10218 ranged breakpoints. */
10219
10220 static void
10221 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10222 struct ui_out *uiout)
10223 {
10224 CORE_ADDR address_start, address_end;
10225 struct bp_location *bl = b->loc;
10226 string_file stb;
10227
10228 gdb_assert (bl);
10229
10230 address_start = bl->address;
10231 address_end = address_start + bl->length - 1;
10232
10233 uiout->text ("\taddress range: ");
10234 stb.printf ("[%s, %s]",
10235 print_core_address (bl->gdbarch, address_start),
10236 print_core_address (bl->gdbarch, address_end));
10237 uiout->field_stream ("addr", stb);
10238 uiout->text ("\n");
10239 }
10240
10241 /* Implement the "print_mention" breakpoint_ops method for
10242 ranged breakpoints. */
10243
10244 static void
10245 print_mention_ranged_breakpoint (struct breakpoint *b)
10246 {
10247 struct bp_location *bl = b->loc;
10248 struct ui_out *uiout = current_uiout;
10249
10250 gdb_assert (bl);
10251 gdb_assert (b->type == bp_hardware_breakpoint);
10252
10253 if (uiout->is_mi_like_p ())
10254 return;
10255
10256 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10257 b->number, paddress (bl->gdbarch, bl->address),
10258 paddress (bl->gdbarch, bl->address + bl->length - 1));
10259 }
10260
10261 /* Implement the "print_recreate" breakpoint_ops method for
10262 ranged breakpoints. */
10263
10264 static void
10265 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10266 {
10267 fprintf_unfiltered (fp, "break-range %s, %s",
10268 event_location_to_string (b->location.get ()),
10269 event_location_to_string (b->location_range_end.get ()));
10270 print_recreate_thread (b, fp);
10271 }
10272
10273 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10274
10275 static struct breakpoint_ops ranged_breakpoint_ops;
10276
10277 /* Find the address where the end of the breakpoint range should be
10278 placed, given the SAL of the end of the range. This is so that if
10279 the user provides a line number, the end of the range is set to the
10280 last instruction of the given line. */
10281
10282 static CORE_ADDR
10283 find_breakpoint_range_end (struct symtab_and_line sal)
10284 {
10285 CORE_ADDR end;
10286
10287 /* If the user provided a PC value, use it. Otherwise,
10288 find the address of the end of the given location. */
10289 if (sal.explicit_pc)
10290 end = sal.pc;
10291 else
10292 {
10293 int ret;
10294 CORE_ADDR start;
10295
10296 ret = find_line_pc_range (sal, &start, &end);
10297 if (!ret)
10298 error (_("Could not find location of the end of the range."));
10299
10300 /* find_line_pc_range returns the start of the next line. */
10301 end--;
10302 }
10303
10304 return end;
10305 }
10306
10307 /* Implement the "break-range" CLI command. */
10308
10309 static void
10310 break_range_command (char *arg, int from_tty)
10311 {
10312 char *arg_start, *addr_string_start;
10313 struct linespec_result canonical_start, canonical_end;
10314 int bp_count, can_use_bp, length;
10315 CORE_ADDR end;
10316 struct breakpoint *b;
10317 struct symtab_and_line sal_start, sal_end;
10318 struct cleanup *cleanup_bkpt;
10319 struct linespec_sals *lsal_start, *lsal_end;
10320
10321 /* We don't support software ranged breakpoints. */
10322 if (target_ranged_break_num_registers () < 0)
10323 error (_("This target does not support hardware ranged breakpoints."));
10324
10325 bp_count = hw_breakpoint_used_count ();
10326 bp_count += target_ranged_break_num_registers ();
10327 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10328 bp_count, 0);
10329 if (can_use_bp < 0)
10330 error (_("Hardware breakpoints used exceeds limit."));
10331
10332 arg = skip_spaces (arg);
10333 if (arg == NULL || arg[0] == '\0')
10334 error(_("No address range specified."));
10335
10336 arg_start = arg;
10337 event_location_up start_location = string_to_event_location (&arg,
10338 current_language);
10339 parse_breakpoint_sals (start_location.get (), &canonical_start);
10340
10341 if (arg[0] != ',')
10342 error (_("Too few arguments."));
10343 else if (VEC_empty (linespec_sals, canonical_start.sals))
10344 error (_("Could not find location of the beginning of the range."));
10345
10346 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10347
10348 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10349 || lsal_start->sals.nelts != 1)
10350 error (_("Cannot create a ranged breakpoint with multiple locations."));
10351
10352 sal_start = lsal_start->sals.sals[0];
10353 addr_string_start = savestring (arg_start, arg - arg_start);
10354 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10355
10356 arg++; /* Skip the comma. */
10357 arg = skip_spaces (arg);
10358
10359 /* Parse the end location. */
10360
10361 arg_start = arg;
10362
10363 /* We call decode_line_full directly here instead of using
10364 parse_breakpoint_sals because we need to specify the start location's
10365 symtab and line as the default symtab and line for the end of the
10366 range. This makes it possible to have ranges like "foo.c:27, +14",
10367 where +14 means 14 lines from the start location. */
10368 event_location_up end_location = string_to_event_location (&arg,
10369 current_language);
10370 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10371 sal_start.symtab, sal_start.line,
10372 &canonical_end, NULL, NULL);
10373
10374 if (VEC_empty (linespec_sals, canonical_end.sals))
10375 error (_("Could not find location of the end of the range."));
10376
10377 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10378 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10379 || lsal_end->sals.nelts != 1)
10380 error (_("Cannot create a ranged breakpoint with multiple locations."));
10381
10382 sal_end = lsal_end->sals.sals[0];
10383
10384 end = find_breakpoint_range_end (sal_end);
10385 if (sal_start.pc > end)
10386 error (_("Invalid address range, end precedes start."));
10387
10388 length = end - sal_start.pc + 1;
10389 if (length < 0)
10390 /* Length overflowed. */
10391 error (_("Address range too large."));
10392 else if (length == 1)
10393 {
10394 /* This range is simple enough to be handled by
10395 the `hbreak' command. */
10396 hbreak_command (addr_string_start, 1);
10397
10398 do_cleanups (cleanup_bkpt);
10399
10400 return;
10401 }
10402
10403 /* Now set up the breakpoint. */
10404 b = set_raw_breakpoint (get_current_arch (), sal_start,
10405 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10406 set_breakpoint_count (breakpoint_count + 1);
10407 b->number = breakpoint_count;
10408 b->disposition = disp_donttouch;
10409 b->location = std::move (start_location);
10410 b->location_range_end = std::move (end_location);
10411 b->loc->length = length;
10412
10413 do_cleanups (cleanup_bkpt);
10414
10415 mention (b);
10416 observer_notify_breakpoint_created (b);
10417 update_global_location_list (UGLL_MAY_INSERT);
10418 }
10419
10420 /* Return non-zero if EXP is verified as constant. Returned zero
10421 means EXP is variable. Also the constant detection may fail for
10422 some constant expressions and in such case still falsely return
10423 zero. */
10424
10425 static int
10426 watchpoint_exp_is_const (const struct expression *exp)
10427 {
10428 int i = exp->nelts;
10429
10430 while (i > 0)
10431 {
10432 int oplenp, argsp;
10433
10434 /* We are only interested in the descriptor of each element. */
10435 operator_length (exp, i, &oplenp, &argsp);
10436 i -= oplenp;
10437
10438 switch (exp->elts[i].opcode)
10439 {
10440 case BINOP_ADD:
10441 case BINOP_SUB:
10442 case BINOP_MUL:
10443 case BINOP_DIV:
10444 case BINOP_REM:
10445 case BINOP_MOD:
10446 case BINOP_LSH:
10447 case BINOP_RSH:
10448 case BINOP_LOGICAL_AND:
10449 case BINOP_LOGICAL_OR:
10450 case BINOP_BITWISE_AND:
10451 case BINOP_BITWISE_IOR:
10452 case BINOP_BITWISE_XOR:
10453 case BINOP_EQUAL:
10454 case BINOP_NOTEQUAL:
10455 case BINOP_LESS:
10456 case BINOP_GTR:
10457 case BINOP_LEQ:
10458 case BINOP_GEQ:
10459 case BINOP_REPEAT:
10460 case BINOP_COMMA:
10461 case BINOP_EXP:
10462 case BINOP_MIN:
10463 case BINOP_MAX:
10464 case BINOP_INTDIV:
10465 case BINOP_CONCAT:
10466 case TERNOP_COND:
10467 case TERNOP_SLICE:
10468
10469 case OP_LONG:
10470 case OP_DOUBLE:
10471 case OP_DECFLOAT:
10472 case OP_LAST:
10473 case OP_COMPLEX:
10474 case OP_STRING:
10475 case OP_ARRAY:
10476 case OP_TYPE:
10477 case OP_TYPEOF:
10478 case OP_DECLTYPE:
10479 case OP_TYPEID:
10480 case OP_NAME:
10481 case OP_OBJC_NSSTRING:
10482
10483 case UNOP_NEG:
10484 case UNOP_LOGICAL_NOT:
10485 case UNOP_COMPLEMENT:
10486 case UNOP_ADDR:
10487 case UNOP_HIGH:
10488 case UNOP_CAST:
10489
10490 case UNOP_CAST_TYPE:
10491 case UNOP_REINTERPRET_CAST:
10492 case UNOP_DYNAMIC_CAST:
10493 /* Unary, binary and ternary operators: We have to check
10494 their operands. If they are constant, then so is the
10495 result of that operation. For instance, if A and B are
10496 determined to be constants, then so is "A + B".
10497
10498 UNOP_IND is one exception to the rule above, because the
10499 value of *ADDR is not necessarily a constant, even when
10500 ADDR is. */
10501 break;
10502
10503 case OP_VAR_VALUE:
10504 /* Check whether the associated symbol is a constant.
10505
10506 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10507 possible that a buggy compiler could mark a variable as
10508 constant even when it is not, and TYPE_CONST would return
10509 true in this case, while SYMBOL_CLASS wouldn't.
10510
10511 We also have to check for function symbols because they
10512 are always constant. */
10513 {
10514 struct symbol *s = exp->elts[i + 2].symbol;
10515
10516 if (SYMBOL_CLASS (s) != LOC_BLOCK
10517 && SYMBOL_CLASS (s) != LOC_CONST
10518 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10519 return 0;
10520 break;
10521 }
10522
10523 /* The default action is to return 0 because we are using
10524 the optimistic approach here: If we don't know something,
10525 then it is not a constant. */
10526 default:
10527 return 0;
10528 }
10529 }
10530
10531 return 1;
10532 }
10533
10534 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10535
10536 static void
10537 dtor_watchpoint (struct breakpoint *self)
10538 {
10539 struct watchpoint *w = (struct watchpoint *) self;
10540
10541 xfree (w->exp_string);
10542 xfree (w->exp_string_reparse);
10543 value_free (w->val);
10544
10545 base_breakpoint_ops.dtor (self);
10546 }
10547
10548 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10549
10550 static void
10551 re_set_watchpoint (struct breakpoint *b)
10552 {
10553 struct watchpoint *w = (struct watchpoint *) b;
10554
10555 /* Watchpoint can be either on expression using entirely global
10556 variables, or it can be on local variables.
10557
10558 Watchpoints of the first kind are never auto-deleted, and even
10559 persist across program restarts. Since they can use variables
10560 from shared libraries, we need to reparse expression as libraries
10561 are loaded and unloaded.
10562
10563 Watchpoints on local variables can also change meaning as result
10564 of solib event. For example, if a watchpoint uses both a local
10565 and a global variables in expression, it's a local watchpoint,
10566 but unloading of a shared library will make the expression
10567 invalid. This is not a very common use case, but we still
10568 re-evaluate expression, to avoid surprises to the user.
10569
10570 Note that for local watchpoints, we re-evaluate it only if
10571 watchpoints frame id is still valid. If it's not, it means the
10572 watchpoint is out of scope and will be deleted soon. In fact,
10573 I'm not sure we'll ever be called in this case.
10574
10575 If a local watchpoint's frame id is still valid, then
10576 w->exp_valid_block is likewise valid, and we can safely use it.
10577
10578 Don't do anything about disabled watchpoints, since they will be
10579 reevaluated again when enabled. */
10580 update_watchpoint (w, 1 /* reparse */);
10581 }
10582
10583 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10584
10585 static int
10586 insert_watchpoint (struct bp_location *bl)
10587 {
10588 struct watchpoint *w = (struct watchpoint *) bl->owner;
10589 int length = w->exact ? 1 : bl->length;
10590
10591 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10592 w->cond_exp.get ());
10593 }
10594
10595 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10596
10597 static int
10598 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10599 {
10600 struct watchpoint *w = (struct watchpoint *) bl->owner;
10601 int length = w->exact ? 1 : bl->length;
10602
10603 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10604 w->cond_exp.get ());
10605 }
10606
10607 static int
10608 breakpoint_hit_watchpoint (const struct bp_location *bl,
10609 struct address_space *aspace, CORE_ADDR bp_addr,
10610 const struct target_waitstatus *ws)
10611 {
10612 struct breakpoint *b = bl->owner;
10613 struct watchpoint *w = (struct watchpoint *) b;
10614
10615 /* Continuable hardware watchpoints are treated as non-existent if the
10616 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10617 some data address). Otherwise gdb won't stop on a break instruction
10618 in the code (not from a breakpoint) when a hardware watchpoint has
10619 been defined. Also skip watchpoints which we know did not trigger
10620 (did not match the data address). */
10621 if (is_hardware_watchpoint (b)
10622 && w->watchpoint_triggered == watch_triggered_no)
10623 return 0;
10624
10625 return 1;
10626 }
10627
10628 static void
10629 check_status_watchpoint (bpstat bs)
10630 {
10631 gdb_assert (is_watchpoint (bs->breakpoint_at));
10632
10633 bpstat_check_watchpoint (bs);
10634 }
10635
10636 /* Implement the "resources_needed" breakpoint_ops method for
10637 hardware watchpoints. */
10638
10639 static int
10640 resources_needed_watchpoint (const struct bp_location *bl)
10641 {
10642 struct watchpoint *w = (struct watchpoint *) bl->owner;
10643 int length = w->exact? 1 : bl->length;
10644
10645 return target_region_ok_for_hw_watchpoint (bl->address, length);
10646 }
10647
10648 /* Implement the "works_in_software_mode" breakpoint_ops method for
10649 hardware watchpoints. */
10650
10651 static int
10652 works_in_software_mode_watchpoint (const struct breakpoint *b)
10653 {
10654 /* Read and access watchpoints only work with hardware support. */
10655 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10656 }
10657
10658 static enum print_stop_action
10659 print_it_watchpoint (bpstat bs)
10660 {
10661 struct cleanup *old_chain;
10662 struct breakpoint *b;
10663 enum print_stop_action result;
10664 struct watchpoint *w;
10665 struct ui_out *uiout = current_uiout;
10666
10667 gdb_assert (bs->bp_location_at != NULL);
10668
10669 b = bs->breakpoint_at;
10670 w = (struct watchpoint *) b;
10671
10672 old_chain = make_cleanup (null_cleanup, NULL);
10673
10674 annotate_watchpoint (b->number);
10675 maybe_print_thread_hit_breakpoint (uiout);
10676
10677 string_file stb;
10678
10679 switch (b->type)
10680 {
10681 case bp_watchpoint:
10682 case bp_hardware_watchpoint:
10683 if (uiout->is_mi_like_p ())
10684 uiout->field_string
10685 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10686 mention (b);
10687 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10688 uiout->text ("\nOld value = ");
10689 watchpoint_value_print (bs->old_val, &stb);
10690 uiout->field_stream ("old", stb);
10691 uiout->text ("\nNew value = ");
10692 watchpoint_value_print (w->val, &stb);
10693 uiout->field_stream ("new", stb);
10694 uiout->text ("\n");
10695 /* More than one watchpoint may have been triggered. */
10696 result = PRINT_UNKNOWN;
10697 break;
10698
10699 case bp_read_watchpoint:
10700 if (uiout->is_mi_like_p ())
10701 uiout->field_string
10702 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10703 mention (b);
10704 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10705 uiout->text ("\nValue = ");
10706 watchpoint_value_print (w->val, &stb);
10707 uiout->field_stream ("value", stb);
10708 uiout->text ("\n");
10709 result = PRINT_UNKNOWN;
10710 break;
10711
10712 case bp_access_watchpoint:
10713 if (bs->old_val != NULL)
10714 {
10715 if (uiout->is_mi_like_p ())
10716 uiout->field_string
10717 ("reason",
10718 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10719 mention (b);
10720 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10721 uiout->text ("\nOld value = ");
10722 watchpoint_value_print (bs->old_val, &stb);
10723 uiout->field_stream ("old", stb);
10724 uiout->text ("\nNew value = ");
10725 }
10726 else
10727 {
10728 mention (b);
10729 if (uiout->is_mi_like_p ())
10730 uiout->field_string
10731 ("reason",
10732 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10733 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10734 uiout->text ("\nValue = ");
10735 }
10736 watchpoint_value_print (w->val, &stb);
10737 uiout->field_stream ("new", stb);
10738 uiout->text ("\n");
10739 result = PRINT_UNKNOWN;
10740 break;
10741 default:
10742 result = PRINT_UNKNOWN;
10743 }
10744
10745 do_cleanups (old_chain);
10746 return result;
10747 }
10748
10749 /* Implement the "print_mention" breakpoint_ops method for hardware
10750 watchpoints. */
10751
10752 static void
10753 print_mention_watchpoint (struct breakpoint *b)
10754 {
10755 struct watchpoint *w = (struct watchpoint *) b;
10756 struct ui_out *uiout = current_uiout;
10757 const char *tuple_name;
10758
10759 switch (b->type)
10760 {
10761 case bp_watchpoint:
10762 uiout->text ("Watchpoint ");
10763 tuple_name = "wpt";
10764 break;
10765 case bp_hardware_watchpoint:
10766 uiout->text ("Hardware watchpoint ");
10767 tuple_name = "wpt";
10768 break;
10769 case bp_read_watchpoint:
10770 uiout->text ("Hardware read watchpoint ");
10771 tuple_name = "hw-rwpt";
10772 break;
10773 case bp_access_watchpoint:
10774 uiout->text ("Hardware access (read/write) watchpoint ");
10775 tuple_name = "hw-awpt";
10776 break;
10777 default:
10778 internal_error (__FILE__, __LINE__,
10779 _("Invalid hardware watchpoint type."));
10780 }
10781
10782 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10783 uiout->field_int ("number", b->number);
10784 uiout->text (": ");
10785 uiout->field_string ("exp", w->exp_string);
10786 }
10787
10788 /* Implement the "print_recreate" breakpoint_ops method for
10789 watchpoints. */
10790
10791 static void
10792 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10793 {
10794 struct watchpoint *w = (struct watchpoint *) b;
10795
10796 switch (b->type)
10797 {
10798 case bp_watchpoint:
10799 case bp_hardware_watchpoint:
10800 fprintf_unfiltered (fp, "watch");
10801 break;
10802 case bp_read_watchpoint:
10803 fprintf_unfiltered (fp, "rwatch");
10804 break;
10805 case bp_access_watchpoint:
10806 fprintf_unfiltered (fp, "awatch");
10807 break;
10808 default:
10809 internal_error (__FILE__, __LINE__,
10810 _("Invalid watchpoint type."));
10811 }
10812
10813 fprintf_unfiltered (fp, " %s", w->exp_string);
10814 print_recreate_thread (b, fp);
10815 }
10816
10817 /* Implement the "explains_signal" breakpoint_ops method for
10818 watchpoints. */
10819
10820 static int
10821 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10822 {
10823 /* A software watchpoint cannot cause a signal other than
10824 GDB_SIGNAL_TRAP. */
10825 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10826 return 0;
10827
10828 return 1;
10829 }
10830
10831 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10832
10833 static struct breakpoint_ops watchpoint_breakpoint_ops;
10834
10835 /* Implement the "insert" breakpoint_ops method for
10836 masked hardware watchpoints. */
10837
10838 static int
10839 insert_masked_watchpoint (struct bp_location *bl)
10840 {
10841 struct watchpoint *w = (struct watchpoint *) bl->owner;
10842
10843 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10844 bl->watchpoint_type);
10845 }
10846
10847 /* Implement the "remove" breakpoint_ops method for
10848 masked hardware watchpoints. */
10849
10850 static int
10851 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10852 {
10853 struct watchpoint *w = (struct watchpoint *) bl->owner;
10854
10855 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10856 bl->watchpoint_type);
10857 }
10858
10859 /* Implement the "resources_needed" breakpoint_ops method for
10860 masked hardware watchpoints. */
10861
10862 static int
10863 resources_needed_masked_watchpoint (const struct bp_location *bl)
10864 {
10865 struct watchpoint *w = (struct watchpoint *) bl->owner;
10866
10867 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10868 }
10869
10870 /* Implement the "works_in_software_mode" breakpoint_ops method for
10871 masked hardware watchpoints. */
10872
10873 static int
10874 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10875 {
10876 return 0;
10877 }
10878
10879 /* Implement the "print_it" breakpoint_ops method for
10880 masked hardware watchpoints. */
10881
10882 static enum print_stop_action
10883 print_it_masked_watchpoint (bpstat bs)
10884 {
10885 struct breakpoint *b = bs->breakpoint_at;
10886 struct ui_out *uiout = current_uiout;
10887
10888 /* Masked watchpoints have only one location. */
10889 gdb_assert (b->loc && b->loc->next == NULL);
10890
10891 annotate_watchpoint (b->number);
10892 maybe_print_thread_hit_breakpoint (uiout);
10893
10894 switch (b->type)
10895 {
10896 case bp_hardware_watchpoint:
10897 if (uiout->is_mi_like_p ())
10898 uiout->field_string
10899 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10900 break;
10901
10902 case bp_read_watchpoint:
10903 if (uiout->is_mi_like_p ())
10904 uiout->field_string
10905 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10906 break;
10907
10908 case bp_access_watchpoint:
10909 if (uiout->is_mi_like_p ())
10910 uiout->field_string
10911 ("reason",
10912 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10913 break;
10914 default:
10915 internal_error (__FILE__, __LINE__,
10916 _("Invalid hardware watchpoint type."));
10917 }
10918
10919 mention (b);
10920 uiout->text (_("\n\
10921 Check the underlying instruction at PC for the memory\n\
10922 address and value which triggered this watchpoint.\n"));
10923 uiout->text ("\n");
10924
10925 /* More than one watchpoint may have been triggered. */
10926 return PRINT_UNKNOWN;
10927 }
10928
10929 /* Implement the "print_one_detail" breakpoint_ops method for
10930 masked hardware watchpoints. */
10931
10932 static void
10933 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10934 struct ui_out *uiout)
10935 {
10936 struct watchpoint *w = (struct watchpoint *) b;
10937
10938 /* Masked watchpoints have only one location. */
10939 gdb_assert (b->loc && b->loc->next == NULL);
10940
10941 uiout->text ("\tmask ");
10942 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10943 uiout->text ("\n");
10944 }
10945
10946 /* Implement the "print_mention" breakpoint_ops method for
10947 masked hardware watchpoints. */
10948
10949 static void
10950 print_mention_masked_watchpoint (struct breakpoint *b)
10951 {
10952 struct watchpoint *w = (struct watchpoint *) b;
10953 struct ui_out *uiout = current_uiout;
10954 const char *tuple_name;
10955
10956 switch (b->type)
10957 {
10958 case bp_hardware_watchpoint:
10959 uiout->text ("Masked hardware watchpoint ");
10960 tuple_name = "wpt";
10961 break;
10962 case bp_read_watchpoint:
10963 uiout->text ("Masked hardware read watchpoint ");
10964 tuple_name = "hw-rwpt";
10965 break;
10966 case bp_access_watchpoint:
10967 uiout->text ("Masked hardware access (read/write) watchpoint ");
10968 tuple_name = "hw-awpt";
10969 break;
10970 default:
10971 internal_error (__FILE__, __LINE__,
10972 _("Invalid hardware watchpoint type."));
10973 }
10974
10975 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10976 uiout->field_int ("number", b->number);
10977 uiout->text (": ");
10978 uiout->field_string ("exp", w->exp_string);
10979 }
10980
10981 /* Implement the "print_recreate" breakpoint_ops method for
10982 masked hardware watchpoints. */
10983
10984 static void
10985 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10986 {
10987 struct watchpoint *w = (struct watchpoint *) b;
10988 char tmp[40];
10989
10990 switch (b->type)
10991 {
10992 case bp_hardware_watchpoint:
10993 fprintf_unfiltered (fp, "watch");
10994 break;
10995 case bp_read_watchpoint:
10996 fprintf_unfiltered (fp, "rwatch");
10997 break;
10998 case bp_access_watchpoint:
10999 fprintf_unfiltered (fp, "awatch");
11000 break;
11001 default:
11002 internal_error (__FILE__, __LINE__,
11003 _("Invalid hardware watchpoint type."));
11004 }
11005
11006 sprintf_vma (tmp, w->hw_wp_mask);
11007 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11008 print_recreate_thread (b, fp);
11009 }
11010
11011 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11012
11013 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11014
11015 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11016
11017 static int
11018 is_masked_watchpoint (const struct breakpoint *b)
11019 {
11020 return b->ops == &masked_watchpoint_breakpoint_ops;
11021 }
11022
11023 /* accessflag: hw_write: watch write,
11024 hw_read: watch read,
11025 hw_access: watch access (read or write) */
11026 static void
11027 watch_command_1 (const char *arg, int accessflag, int from_tty,
11028 int just_location, int internal)
11029 {
11030 struct breakpoint *b, *scope_breakpoint = NULL;
11031 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11032 struct value *val, *mark, *result;
11033 int saved_bitpos = 0, saved_bitsize = 0;
11034 const char *exp_start = NULL;
11035 const char *exp_end = NULL;
11036 const char *tok, *end_tok;
11037 int toklen = -1;
11038 const char *cond_start = NULL;
11039 const char *cond_end = NULL;
11040 enum bptype bp_type;
11041 int thread = -1;
11042 int pc = 0;
11043 /* Flag to indicate whether we are going to use masks for
11044 the hardware watchpoint. */
11045 int use_mask = 0;
11046 CORE_ADDR mask = 0;
11047 struct watchpoint *w;
11048 char *expression;
11049 struct cleanup *back_to;
11050
11051 /* Make sure that we actually have parameters to parse. */
11052 if (arg != NULL && arg[0] != '\0')
11053 {
11054 const char *value_start;
11055
11056 exp_end = arg + strlen (arg);
11057
11058 /* Look for "parameter value" pairs at the end
11059 of the arguments string. */
11060 for (tok = exp_end - 1; tok > arg; tok--)
11061 {
11062 /* Skip whitespace at the end of the argument list. */
11063 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11064 tok--;
11065
11066 /* Find the beginning of the last token.
11067 This is the value of the parameter. */
11068 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11069 tok--;
11070 value_start = tok + 1;
11071
11072 /* Skip whitespace. */
11073 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11074 tok--;
11075
11076 end_tok = tok;
11077
11078 /* Find the beginning of the second to last token.
11079 This is the parameter itself. */
11080 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11081 tok--;
11082 tok++;
11083 toklen = end_tok - tok + 1;
11084
11085 if (toklen == 6 && startswith (tok, "thread"))
11086 {
11087 struct thread_info *thr;
11088 /* At this point we've found a "thread" token, which means
11089 the user is trying to set a watchpoint that triggers
11090 only in a specific thread. */
11091 const char *endp;
11092
11093 if (thread != -1)
11094 error(_("You can specify only one thread."));
11095
11096 /* Extract the thread ID from the next token. */
11097 thr = parse_thread_id (value_start, &endp);
11098
11099 /* Check if the user provided a valid thread ID. */
11100 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11101 invalid_thread_id_error (value_start);
11102
11103 thread = thr->global_num;
11104 }
11105 else if (toklen == 4 && startswith (tok, "mask"))
11106 {
11107 /* We've found a "mask" token, which means the user wants to
11108 create a hardware watchpoint that is going to have the mask
11109 facility. */
11110 struct value *mask_value, *mark;
11111
11112 if (use_mask)
11113 error(_("You can specify only one mask."));
11114
11115 use_mask = just_location = 1;
11116
11117 mark = value_mark ();
11118 mask_value = parse_to_comma_and_eval (&value_start);
11119 mask = value_as_address (mask_value);
11120 value_free_to_mark (mark);
11121 }
11122 else
11123 /* We didn't recognize what we found. We should stop here. */
11124 break;
11125
11126 /* Truncate the string and get rid of the "parameter value" pair before
11127 the arguments string is parsed by the parse_exp_1 function. */
11128 exp_end = tok;
11129 }
11130 }
11131 else
11132 exp_end = arg;
11133
11134 /* Parse the rest of the arguments. From here on out, everything
11135 is in terms of a newly allocated string instead of the original
11136 ARG. */
11137 innermost_block = NULL;
11138 expression = savestring (arg, exp_end - arg);
11139 back_to = make_cleanup (xfree, expression);
11140 exp_start = arg = expression;
11141 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11142 exp_end = arg;
11143 /* Remove trailing whitespace from the expression before saving it.
11144 This makes the eventual display of the expression string a bit
11145 prettier. */
11146 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11147 --exp_end;
11148
11149 /* Checking if the expression is not constant. */
11150 if (watchpoint_exp_is_const (exp.get ()))
11151 {
11152 int len;
11153
11154 len = exp_end - exp_start;
11155 while (len > 0 && isspace (exp_start[len - 1]))
11156 len--;
11157 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11158 }
11159
11160 exp_valid_block = innermost_block;
11161 mark = value_mark ();
11162 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11163
11164 if (val != NULL && just_location)
11165 {
11166 saved_bitpos = value_bitpos (val);
11167 saved_bitsize = value_bitsize (val);
11168 }
11169
11170 if (just_location)
11171 {
11172 int ret;
11173
11174 exp_valid_block = NULL;
11175 val = value_addr (result);
11176 release_value (val);
11177 value_free_to_mark (mark);
11178
11179 if (use_mask)
11180 {
11181 ret = target_masked_watch_num_registers (value_as_address (val),
11182 mask);
11183 if (ret == -1)
11184 error (_("This target does not support masked watchpoints."));
11185 else if (ret == -2)
11186 error (_("Invalid mask or memory region."));
11187 }
11188 }
11189 else if (val != NULL)
11190 release_value (val);
11191
11192 tok = skip_spaces_const (arg);
11193 end_tok = skip_to_space_const (tok);
11194
11195 toklen = end_tok - tok;
11196 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11197 {
11198 innermost_block = NULL;
11199 tok = cond_start = end_tok + 1;
11200 parse_exp_1 (&tok, 0, 0, 0);
11201
11202 /* The watchpoint expression may not be local, but the condition
11203 may still be. E.g.: `watch global if local > 0'. */
11204 cond_exp_valid_block = innermost_block;
11205
11206 cond_end = tok;
11207 }
11208 if (*tok)
11209 error (_("Junk at end of command."));
11210
11211 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11212
11213 /* Save this because create_internal_breakpoint below invalidates
11214 'wp_frame'. */
11215 frame_id watchpoint_frame = get_frame_id (wp_frame);
11216
11217 /* If the expression is "local", then set up a "watchpoint scope"
11218 breakpoint at the point where we've left the scope of the watchpoint
11219 expression. Create the scope breakpoint before the watchpoint, so
11220 that we will encounter it first in bpstat_stop_status. */
11221 if (exp_valid_block != NULL && wp_frame != NULL)
11222 {
11223 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11224
11225 if (frame_id_p (caller_frame_id))
11226 {
11227 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11228 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11229
11230 scope_breakpoint
11231 = create_internal_breakpoint (caller_arch, caller_pc,
11232 bp_watchpoint_scope,
11233 &momentary_breakpoint_ops);
11234
11235 /* create_internal_breakpoint could invalidate WP_FRAME. */
11236 wp_frame = NULL;
11237
11238 scope_breakpoint->enable_state = bp_enabled;
11239
11240 /* Automatically delete the breakpoint when it hits. */
11241 scope_breakpoint->disposition = disp_del;
11242
11243 /* Only break in the proper frame (help with recursion). */
11244 scope_breakpoint->frame_id = caller_frame_id;
11245
11246 /* Set the address at which we will stop. */
11247 scope_breakpoint->loc->gdbarch = caller_arch;
11248 scope_breakpoint->loc->requested_address = caller_pc;
11249 scope_breakpoint->loc->address
11250 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11251 scope_breakpoint->loc->requested_address,
11252 scope_breakpoint->type);
11253 }
11254 }
11255
11256 /* Now set up the breakpoint. We create all watchpoints as hardware
11257 watchpoints here even if hardware watchpoints are turned off, a call
11258 to update_watchpoint later in this function will cause the type to
11259 drop back to bp_watchpoint (software watchpoint) if required. */
11260
11261 if (accessflag == hw_read)
11262 bp_type = bp_read_watchpoint;
11263 else if (accessflag == hw_access)
11264 bp_type = bp_access_watchpoint;
11265 else
11266 bp_type = bp_hardware_watchpoint;
11267
11268 w = new watchpoint ();
11269 b = &w->base;
11270 if (use_mask)
11271 init_raw_breakpoint_without_location (b, NULL, bp_type,
11272 &masked_watchpoint_breakpoint_ops);
11273 else
11274 init_raw_breakpoint_without_location (b, NULL, bp_type,
11275 &watchpoint_breakpoint_ops);
11276 b->thread = thread;
11277 b->disposition = disp_donttouch;
11278 b->pspace = current_program_space;
11279 w->exp = std::move (exp);
11280 w->exp_valid_block = exp_valid_block;
11281 w->cond_exp_valid_block = cond_exp_valid_block;
11282 if (just_location)
11283 {
11284 struct type *t = value_type (val);
11285 CORE_ADDR addr = value_as_address (val);
11286
11287 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11288
11289 std::string name = type_to_string (t);
11290
11291 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11292 core_addr_to_string (addr));
11293
11294 w->exp_string = xstrprintf ("-location %.*s",
11295 (int) (exp_end - exp_start), exp_start);
11296
11297 /* The above expression is in C. */
11298 b->language = language_c;
11299 }
11300 else
11301 w->exp_string = savestring (exp_start, exp_end - exp_start);
11302
11303 if (use_mask)
11304 {
11305 w->hw_wp_mask = mask;
11306 }
11307 else
11308 {
11309 w->val = val;
11310 w->val_bitpos = saved_bitpos;
11311 w->val_bitsize = saved_bitsize;
11312 w->val_valid = 1;
11313 }
11314
11315 if (cond_start)
11316 b->cond_string = savestring (cond_start, cond_end - cond_start);
11317 else
11318 b->cond_string = 0;
11319
11320 if (frame_id_p (watchpoint_frame))
11321 {
11322 w->watchpoint_frame = watchpoint_frame;
11323 w->watchpoint_thread = inferior_ptid;
11324 }
11325 else
11326 {
11327 w->watchpoint_frame = null_frame_id;
11328 w->watchpoint_thread = null_ptid;
11329 }
11330
11331 if (scope_breakpoint != NULL)
11332 {
11333 /* The scope breakpoint is related to the watchpoint. We will
11334 need to act on them together. */
11335 b->related_breakpoint = scope_breakpoint;
11336 scope_breakpoint->related_breakpoint = b;
11337 }
11338
11339 if (!just_location)
11340 value_free_to_mark (mark);
11341
11342 TRY
11343 {
11344 /* Finally update the new watchpoint. This creates the locations
11345 that should be inserted. */
11346 update_watchpoint (w, 1);
11347 }
11348 CATCH (e, RETURN_MASK_ALL)
11349 {
11350 delete_breakpoint (b);
11351 throw_exception (e);
11352 }
11353 END_CATCH
11354
11355 install_breakpoint (internal, b, 1);
11356 do_cleanups (back_to);
11357 }
11358
11359 /* Return count of debug registers needed to watch the given expression.
11360 If the watchpoint cannot be handled in hardware return zero. */
11361
11362 static int
11363 can_use_hardware_watchpoint (struct value *v)
11364 {
11365 int found_memory_cnt = 0;
11366 struct value *head = v;
11367
11368 /* Did the user specifically forbid us to use hardware watchpoints? */
11369 if (!can_use_hw_watchpoints)
11370 return 0;
11371
11372 /* Make sure that the value of the expression depends only upon
11373 memory contents, and values computed from them within GDB. If we
11374 find any register references or function calls, we can't use a
11375 hardware watchpoint.
11376
11377 The idea here is that evaluating an expression generates a series
11378 of values, one holding the value of every subexpression. (The
11379 expression a*b+c has five subexpressions: a, b, a*b, c, and
11380 a*b+c.) GDB's values hold almost enough information to establish
11381 the criteria given above --- they identify memory lvalues,
11382 register lvalues, computed values, etcetera. So we can evaluate
11383 the expression, and then scan the chain of values that leaves
11384 behind to decide whether we can detect any possible change to the
11385 expression's final value using only hardware watchpoints.
11386
11387 However, I don't think that the values returned by inferior
11388 function calls are special in any way. So this function may not
11389 notice that an expression involving an inferior function call
11390 can't be watched with hardware watchpoints. FIXME. */
11391 for (; v; v = value_next (v))
11392 {
11393 if (VALUE_LVAL (v) == lval_memory)
11394 {
11395 if (v != head && value_lazy (v))
11396 /* A lazy memory lvalue in the chain is one that GDB never
11397 needed to fetch; we either just used its address (e.g.,
11398 `a' in `a.b') or we never needed it at all (e.g., `a'
11399 in `a,b'). This doesn't apply to HEAD; if that is
11400 lazy then it was not readable, but watch it anyway. */
11401 ;
11402 else
11403 {
11404 /* Ahh, memory we actually used! Check if we can cover
11405 it with hardware watchpoints. */
11406 struct type *vtype = check_typedef (value_type (v));
11407
11408 /* We only watch structs and arrays if user asked for it
11409 explicitly, never if they just happen to appear in a
11410 middle of some value chain. */
11411 if (v == head
11412 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11413 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11414 {
11415 CORE_ADDR vaddr = value_address (v);
11416 int len;
11417 int num_regs;
11418
11419 len = (target_exact_watchpoints
11420 && is_scalar_type_recursive (vtype))?
11421 1 : TYPE_LENGTH (value_type (v));
11422
11423 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11424 if (!num_regs)
11425 return 0;
11426 else
11427 found_memory_cnt += num_regs;
11428 }
11429 }
11430 }
11431 else if (VALUE_LVAL (v) != not_lval
11432 && deprecated_value_modifiable (v) == 0)
11433 return 0; /* These are values from the history (e.g., $1). */
11434 else if (VALUE_LVAL (v) == lval_register)
11435 return 0; /* Cannot watch a register with a HW watchpoint. */
11436 }
11437
11438 /* The expression itself looks suitable for using a hardware
11439 watchpoint, but give the target machine a chance to reject it. */
11440 return found_memory_cnt;
11441 }
11442
11443 void
11444 watch_command_wrapper (char *arg, int from_tty, int internal)
11445 {
11446 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11447 }
11448
11449 /* A helper function that looks for the "-location" argument and then
11450 calls watch_command_1. */
11451
11452 static void
11453 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11454 {
11455 int just_location = 0;
11456
11457 if (arg
11458 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11459 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11460 {
11461 arg = skip_spaces (arg);
11462 just_location = 1;
11463 }
11464
11465 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11466 }
11467
11468 static void
11469 watch_command (char *arg, int from_tty)
11470 {
11471 watch_maybe_just_location (arg, hw_write, from_tty);
11472 }
11473
11474 void
11475 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11476 {
11477 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11478 }
11479
11480 static void
11481 rwatch_command (char *arg, int from_tty)
11482 {
11483 watch_maybe_just_location (arg, hw_read, from_tty);
11484 }
11485
11486 void
11487 awatch_command_wrapper (char *arg, int from_tty, int internal)
11488 {
11489 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11490 }
11491
11492 static void
11493 awatch_command (char *arg, int from_tty)
11494 {
11495 watch_maybe_just_location (arg, hw_access, from_tty);
11496 }
11497 \f
11498
11499 /* Data for the FSM that manages the until(location)/advance commands
11500 in infcmd.c. Here because it uses the mechanisms of
11501 breakpoints. */
11502
11503 struct until_break_fsm
11504 {
11505 /* The base class. */
11506 struct thread_fsm thread_fsm;
11507
11508 /* The thread that as current when the command was executed. */
11509 int thread;
11510
11511 /* The breakpoint set at the destination location. */
11512 struct breakpoint *location_breakpoint;
11513
11514 /* Breakpoint set at the return address in the caller frame. May be
11515 NULL. */
11516 struct breakpoint *caller_breakpoint;
11517 };
11518
11519 static void until_break_fsm_clean_up (struct thread_fsm *self,
11520 struct thread_info *thread);
11521 static int until_break_fsm_should_stop (struct thread_fsm *self,
11522 struct thread_info *thread);
11523 static enum async_reply_reason
11524 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11525
11526 /* until_break_fsm's vtable. */
11527
11528 static struct thread_fsm_ops until_break_fsm_ops =
11529 {
11530 NULL, /* dtor */
11531 until_break_fsm_clean_up,
11532 until_break_fsm_should_stop,
11533 NULL, /* return_value */
11534 until_break_fsm_async_reply_reason,
11535 };
11536
11537 /* Allocate a new until_break_command_fsm. */
11538
11539 static struct until_break_fsm *
11540 new_until_break_fsm (struct interp *cmd_interp, int thread,
11541 struct breakpoint *location_breakpoint,
11542 struct breakpoint *caller_breakpoint)
11543 {
11544 struct until_break_fsm *sm;
11545
11546 sm = XCNEW (struct until_break_fsm);
11547 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11548
11549 sm->thread = thread;
11550 sm->location_breakpoint = location_breakpoint;
11551 sm->caller_breakpoint = caller_breakpoint;
11552
11553 return sm;
11554 }
11555
11556 /* Implementation of the 'should_stop' FSM method for the
11557 until(location)/advance commands. */
11558
11559 static int
11560 until_break_fsm_should_stop (struct thread_fsm *self,
11561 struct thread_info *tp)
11562 {
11563 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11564
11565 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11566 sm->location_breakpoint) != NULL
11567 || (sm->caller_breakpoint != NULL
11568 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11569 sm->caller_breakpoint) != NULL))
11570 thread_fsm_set_finished (self);
11571
11572 return 1;
11573 }
11574
11575 /* Implementation of the 'clean_up' FSM method for the
11576 until(location)/advance commands. */
11577
11578 static void
11579 until_break_fsm_clean_up (struct thread_fsm *self,
11580 struct thread_info *thread)
11581 {
11582 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11583
11584 /* Clean up our temporary breakpoints. */
11585 if (sm->location_breakpoint != NULL)
11586 {
11587 delete_breakpoint (sm->location_breakpoint);
11588 sm->location_breakpoint = NULL;
11589 }
11590 if (sm->caller_breakpoint != NULL)
11591 {
11592 delete_breakpoint (sm->caller_breakpoint);
11593 sm->caller_breakpoint = NULL;
11594 }
11595 delete_longjmp_breakpoint (sm->thread);
11596 }
11597
11598 /* Implementation of the 'async_reply_reason' FSM method for the
11599 until(location)/advance commands. */
11600
11601 static enum async_reply_reason
11602 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11603 {
11604 return EXEC_ASYNC_LOCATION_REACHED;
11605 }
11606
11607 void
11608 until_break_command (char *arg, int from_tty, int anywhere)
11609 {
11610 struct symtabs_and_lines sals;
11611 struct symtab_and_line sal;
11612 struct frame_info *frame;
11613 struct gdbarch *frame_gdbarch;
11614 struct frame_id stack_frame_id;
11615 struct frame_id caller_frame_id;
11616 struct breakpoint *location_breakpoint;
11617 struct breakpoint *caller_breakpoint = NULL;
11618 struct cleanup *old_chain;
11619 int thread;
11620 struct thread_info *tp;
11621 struct until_break_fsm *sm;
11622
11623 clear_proceed_status (0);
11624
11625 /* Set a breakpoint where the user wants it and at return from
11626 this function. */
11627
11628 event_location_up location = string_to_event_location (&arg, current_language);
11629
11630 if (last_displayed_sal_is_valid ())
11631 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11632 get_last_displayed_symtab (),
11633 get_last_displayed_line ());
11634 else
11635 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11636 NULL, (struct symtab *) NULL, 0);
11637
11638 if (sals.nelts != 1)
11639 error (_("Couldn't get information on specified line."));
11640
11641 sal = sals.sals[0];
11642 xfree (sals.sals); /* malloc'd, so freed. */
11643
11644 if (*arg)
11645 error (_("Junk at end of arguments."));
11646
11647 resolve_sal_pc (&sal);
11648
11649 tp = inferior_thread ();
11650 thread = tp->global_num;
11651
11652 old_chain = make_cleanup (null_cleanup, NULL);
11653
11654 /* Note linespec handling above invalidates the frame chain.
11655 Installing a breakpoint also invalidates the frame chain (as it
11656 may need to switch threads), so do any frame handling before
11657 that. */
11658
11659 frame = get_selected_frame (NULL);
11660 frame_gdbarch = get_frame_arch (frame);
11661 stack_frame_id = get_stack_frame_id (frame);
11662 caller_frame_id = frame_unwind_caller_id (frame);
11663
11664 /* Keep within the current frame, or in frames called by the current
11665 one. */
11666
11667 if (frame_id_p (caller_frame_id))
11668 {
11669 struct symtab_and_line sal2;
11670 struct gdbarch *caller_gdbarch;
11671
11672 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11673 sal2.pc = frame_unwind_caller_pc (frame);
11674 caller_gdbarch = frame_unwind_caller_arch (frame);
11675 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11676 sal2,
11677 caller_frame_id,
11678 bp_until);
11679 make_cleanup_delete_breakpoint (caller_breakpoint);
11680
11681 set_longjmp_breakpoint (tp, caller_frame_id);
11682 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11683 }
11684
11685 /* set_momentary_breakpoint could invalidate FRAME. */
11686 frame = NULL;
11687
11688 if (anywhere)
11689 /* If the user told us to continue until a specified location,
11690 we don't specify a frame at which we need to stop. */
11691 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11692 null_frame_id, bp_until);
11693 else
11694 /* Otherwise, specify the selected frame, because we want to stop
11695 only at the very same frame. */
11696 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11697 stack_frame_id, bp_until);
11698 make_cleanup_delete_breakpoint (location_breakpoint);
11699
11700 sm = new_until_break_fsm (command_interp (), tp->global_num,
11701 location_breakpoint, caller_breakpoint);
11702 tp->thread_fsm = &sm->thread_fsm;
11703
11704 discard_cleanups (old_chain);
11705
11706 proceed (-1, GDB_SIGNAL_DEFAULT);
11707 }
11708
11709 /* This function attempts to parse an optional "if <cond>" clause
11710 from the arg string. If one is not found, it returns NULL.
11711
11712 Else, it returns a pointer to the condition string. (It does not
11713 attempt to evaluate the string against a particular block.) And,
11714 it updates arg to point to the first character following the parsed
11715 if clause in the arg string. */
11716
11717 const char *
11718 ep_parse_optional_if_clause (const char **arg)
11719 {
11720 const char *cond_string;
11721
11722 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11723 return NULL;
11724
11725 /* Skip the "if" keyword. */
11726 (*arg) += 2;
11727
11728 /* Skip any extra leading whitespace, and record the start of the
11729 condition string. */
11730 *arg = skip_spaces_const (*arg);
11731 cond_string = *arg;
11732
11733 /* Assume that the condition occupies the remainder of the arg
11734 string. */
11735 (*arg) += strlen (cond_string);
11736
11737 return cond_string;
11738 }
11739
11740 /* Commands to deal with catching events, such as signals, exceptions,
11741 process start/exit, etc. */
11742
11743 typedef enum
11744 {
11745 catch_fork_temporary, catch_vfork_temporary,
11746 catch_fork_permanent, catch_vfork_permanent
11747 }
11748 catch_fork_kind;
11749
11750 static void
11751 catch_fork_command_1 (char *arg_entry, int from_tty,
11752 struct cmd_list_element *command)
11753 {
11754 const char *arg = arg_entry;
11755 struct gdbarch *gdbarch = get_current_arch ();
11756 const char *cond_string = NULL;
11757 catch_fork_kind fork_kind;
11758 int tempflag;
11759
11760 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11761 tempflag = (fork_kind == catch_fork_temporary
11762 || fork_kind == catch_vfork_temporary);
11763
11764 if (!arg)
11765 arg = "";
11766 arg = skip_spaces_const (arg);
11767
11768 /* The allowed syntax is:
11769 catch [v]fork
11770 catch [v]fork if <cond>
11771
11772 First, check if there's an if clause. */
11773 cond_string = ep_parse_optional_if_clause (&arg);
11774
11775 if ((*arg != '\0') && !isspace (*arg))
11776 error (_("Junk at end of arguments."));
11777
11778 /* If this target supports it, create a fork or vfork catchpoint
11779 and enable reporting of such events. */
11780 switch (fork_kind)
11781 {
11782 case catch_fork_temporary:
11783 case catch_fork_permanent:
11784 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11785 &catch_fork_breakpoint_ops);
11786 break;
11787 case catch_vfork_temporary:
11788 case catch_vfork_permanent:
11789 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11790 &catch_vfork_breakpoint_ops);
11791 break;
11792 default:
11793 error (_("unsupported or unknown fork kind; cannot catch it"));
11794 break;
11795 }
11796 }
11797
11798 static void
11799 catch_exec_command_1 (char *arg_entry, int from_tty,
11800 struct cmd_list_element *command)
11801 {
11802 const char *arg = arg_entry;
11803 struct exec_catchpoint *c;
11804 struct gdbarch *gdbarch = get_current_arch ();
11805 int tempflag;
11806 const char *cond_string = NULL;
11807
11808 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11809
11810 if (!arg)
11811 arg = "";
11812 arg = skip_spaces_const (arg);
11813
11814 /* The allowed syntax is:
11815 catch exec
11816 catch exec if <cond>
11817
11818 First, check if there's an if clause. */
11819 cond_string = ep_parse_optional_if_clause (&arg);
11820
11821 if ((*arg != '\0') && !isspace (*arg))
11822 error (_("Junk at end of arguments."));
11823
11824 c = new exec_catchpoint ();
11825 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11826 &catch_exec_breakpoint_ops);
11827 c->exec_pathname = NULL;
11828
11829 install_breakpoint (0, &c->base, 1);
11830 }
11831
11832 void
11833 init_ada_exception_breakpoint (struct breakpoint *b,
11834 struct gdbarch *gdbarch,
11835 struct symtab_and_line sal,
11836 char *addr_string,
11837 const struct breakpoint_ops *ops,
11838 int tempflag,
11839 int enabled,
11840 int from_tty)
11841 {
11842 if (from_tty)
11843 {
11844 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11845 if (!loc_gdbarch)
11846 loc_gdbarch = gdbarch;
11847
11848 describe_other_breakpoints (loc_gdbarch,
11849 sal.pspace, sal.pc, sal.section, -1);
11850 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11851 version for exception catchpoints, because two catchpoints
11852 used for different exception names will use the same address.
11853 In this case, a "breakpoint ... also set at..." warning is
11854 unproductive. Besides, the warning phrasing is also a bit
11855 inappropriate, we should use the word catchpoint, and tell
11856 the user what type of catchpoint it is. The above is good
11857 enough for now, though. */
11858 }
11859
11860 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11861
11862 b->enable_state = enabled ? bp_enabled : bp_disabled;
11863 b->disposition = tempflag ? disp_del : disp_donttouch;
11864 b->location = string_to_event_location (&addr_string,
11865 language_def (language_ada));
11866 b->language = language_ada;
11867 }
11868
11869 static void
11870 catch_command (char *arg, int from_tty)
11871 {
11872 error (_("Catch requires an event name."));
11873 }
11874 \f
11875
11876 static void
11877 tcatch_command (char *arg, int from_tty)
11878 {
11879 error (_("Catch requires an event name."));
11880 }
11881
11882 /* A qsort comparison function that sorts breakpoints in order. */
11883
11884 static int
11885 compare_breakpoints (const void *a, const void *b)
11886 {
11887 const breakpoint_p *ba = (const breakpoint_p *) a;
11888 uintptr_t ua = (uintptr_t) *ba;
11889 const breakpoint_p *bb = (const breakpoint_p *) b;
11890 uintptr_t ub = (uintptr_t) *bb;
11891
11892 if ((*ba)->number < (*bb)->number)
11893 return -1;
11894 else if ((*ba)->number > (*bb)->number)
11895 return 1;
11896
11897 /* Now sort by address, in case we see, e..g, two breakpoints with
11898 the number 0. */
11899 if (ua < ub)
11900 return -1;
11901 return ua > ub ? 1 : 0;
11902 }
11903
11904 /* Delete breakpoints by address or line. */
11905
11906 static void
11907 clear_command (char *arg, int from_tty)
11908 {
11909 struct breakpoint *b, *prev;
11910 VEC(breakpoint_p) *found = 0;
11911 int ix;
11912 int default_match;
11913 struct symtabs_and_lines sals;
11914 struct symtab_and_line sal;
11915 int i;
11916 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11917
11918 if (arg)
11919 {
11920 sals = decode_line_with_current_source (arg,
11921 (DECODE_LINE_FUNFIRSTLINE
11922 | DECODE_LINE_LIST_MODE));
11923 make_cleanup (xfree, sals.sals);
11924 default_match = 0;
11925 }
11926 else
11927 {
11928 sals.sals = XNEW (struct symtab_and_line);
11929 make_cleanup (xfree, sals.sals);
11930 init_sal (&sal); /* Initialize to zeroes. */
11931
11932 /* Set sal's line, symtab, pc, and pspace to the values
11933 corresponding to the last call to print_frame_info. If the
11934 codepoint is not valid, this will set all the fields to 0. */
11935 get_last_displayed_sal (&sal);
11936 if (sal.symtab == 0)
11937 error (_("No source file specified."));
11938
11939 sals.sals[0] = sal;
11940 sals.nelts = 1;
11941
11942 default_match = 1;
11943 }
11944
11945 /* We don't call resolve_sal_pc here. That's not as bad as it
11946 seems, because all existing breakpoints typically have both
11947 file/line and pc set. So, if clear is given file/line, we can
11948 match this to existing breakpoint without obtaining pc at all.
11949
11950 We only support clearing given the address explicitly
11951 present in breakpoint table. Say, we've set breakpoint
11952 at file:line. There were several PC values for that file:line,
11953 due to optimization, all in one block.
11954
11955 We've picked one PC value. If "clear" is issued with another
11956 PC corresponding to the same file:line, the breakpoint won't
11957 be cleared. We probably can still clear the breakpoint, but
11958 since the other PC value is never presented to user, user
11959 can only find it by guessing, and it does not seem important
11960 to support that. */
11961
11962 /* For each line spec given, delete bps which correspond to it. Do
11963 it in two passes, solely to preserve the current behavior that
11964 from_tty is forced true if we delete more than one
11965 breakpoint. */
11966
11967 found = NULL;
11968 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11969 for (i = 0; i < sals.nelts; i++)
11970 {
11971 const char *sal_fullname;
11972
11973 /* If exact pc given, clear bpts at that pc.
11974 If line given (pc == 0), clear all bpts on specified line.
11975 If defaulting, clear all bpts on default line
11976 or at default pc.
11977
11978 defaulting sal.pc != 0 tests to do
11979
11980 0 1 pc
11981 1 1 pc _and_ line
11982 0 0 line
11983 1 0 <can't happen> */
11984
11985 sal = sals.sals[i];
11986 sal_fullname = (sal.symtab == NULL
11987 ? NULL : symtab_to_fullname (sal.symtab));
11988
11989 /* Find all matching breakpoints and add them to 'found'. */
11990 ALL_BREAKPOINTS (b)
11991 {
11992 int match = 0;
11993 /* Are we going to delete b? */
11994 if (b->type != bp_none && !is_watchpoint (b))
11995 {
11996 struct bp_location *loc = b->loc;
11997 for (; loc; loc = loc->next)
11998 {
11999 /* If the user specified file:line, don't allow a PC
12000 match. This matches historical gdb behavior. */
12001 int pc_match = (!sal.explicit_line
12002 && sal.pc
12003 && (loc->pspace == sal.pspace)
12004 && (loc->address == sal.pc)
12005 && (!section_is_overlay (loc->section)
12006 || loc->section == sal.section));
12007 int line_match = 0;
12008
12009 if ((default_match || sal.explicit_line)
12010 && loc->symtab != NULL
12011 && sal_fullname != NULL
12012 && sal.pspace == loc->pspace
12013 && loc->line_number == sal.line
12014 && filename_cmp (symtab_to_fullname (loc->symtab),
12015 sal_fullname) == 0)
12016 line_match = 1;
12017
12018 if (pc_match || line_match)
12019 {
12020 match = 1;
12021 break;
12022 }
12023 }
12024 }
12025
12026 if (match)
12027 VEC_safe_push(breakpoint_p, found, b);
12028 }
12029 }
12030
12031 /* Now go thru the 'found' chain and delete them. */
12032 if (VEC_empty(breakpoint_p, found))
12033 {
12034 if (arg)
12035 error (_("No breakpoint at %s."), arg);
12036 else
12037 error (_("No breakpoint at this line."));
12038 }
12039
12040 /* Remove duplicates from the vec. */
12041 qsort (VEC_address (breakpoint_p, found),
12042 VEC_length (breakpoint_p, found),
12043 sizeof (breakpoint_p),
12044 compare_breakpoints);
12045 prev = VEC_index (breakpoint_p, found, 0);
12046 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12047 {
12048 if (b == prev)
12049 {
12050 VEC_ordered_remove (breakpoint_p, found, ix);
12051 --ix;
12052 }
12053 }
12054
12055 if (VEC_length(breakpoint_p, found) > 1)
12056 from_tty = 1; /* Always report if deleted more than one. */
12057 if (from_tty)
12058 {
12059 if (VEC_length(breakpoint_p, found) == 1)
12060 printf_unfiltered (_("Deleted breakpoint "));
12061 else
12062 printf_unfiltered (_("Deleted breakpoints "));
12063 }
12064
12065 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12066 {
12067 if (from_tty)
12068 printf_unfiltered ("%d ", b->number);
12069 delete_breakpoint (b);
12070 }
12071 if (from_tty)
12072 putchar_unfiltered ('\n');
12073
12074 do_cleanups (cleanups);
12075 }
12076 \f
12077 /* Delete breakpoint in BS if they are `delete' breakpoints and
12078 all breakpoints that are marked for deletion, whether hit or not.
12079 This is called after any breakpoint is hit, or after errors. */
12080
12081 void
12082 breakpoint_auto_delete (bpstat bs)
12083 {
12084 struct breakpoint *b, *b_tmp;
12085
12086 for (; bs; bs = bs->next)
12087 if (bs->breakpoint_at
12088 && bs->breakpoint_at->disposition == disp_del
12089 && bs->stop)
12090 delete_breakpoint (bs->breakpoint_at);
12091
12092 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12093 {
12094 if (b->disposition == disp_del_at_next_stop)
12095 delete_breakpoint (b);
12096 }
12097 }
12098
12099 /* A comparison function for bp_location AP and BP being interfaced to
12100 qsort. Sort elements primarily by their ADDRESS (no matter what
12101 does breakpoint_address_is_meaningful say for its OWNER),
12102 secondarily by ordering first permanent elements and
12103 terciarily just ensuring the array is sorted stable way despite
12104 qsort being an unstable algorithm. */
12105
12106 static int
12107 bp_locations_compare (const void *ap, const void *bp)
12108 {
12109 const struct bp_location *a = *(const struct bp_location **) ap;
12110 const struct bp_location *b = *(const struct bp_location **) bp;
12111
12112 if (a->address != b->address)
12113 return (a->address > b->address) - (a->address < b->address);
12114
12115 /* Sort locations at the same address by their pspace number, keeping
12116 locations of the same inferior (in a multi-inferior environment)
12117 grouped. */
12118
12119 if (a->pspace->num != b->pspace->num)
12120 return ((a->pspace->num > b->pspace->num)
12121 - (a->pspace->num < b->pspace->num));
12122
12123 /* Sort permanent breakpoints first. */
12124 if (a->permanent != b->permanent)
12125 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12126
12127 /* Make the internal GDB representation stable across GDB runs
12128 where A and B memory inside GDB can differ. Breakpoint locations of
12129 the same type at the same address can be sorted in arbitrary order. */
12130
12131 if (a->owner->number != b->owner->number)
12132 return ((a->owner->number > b->owner->number)
12133 - (a->owner->number < b->owner->number));
12134
12135 return (a > b) - (a < b);
12136 }
12137
12138 /* Set bp_locations_placed_address_before_address_max and
12139 bp_locations_shadow_len_after_address_max according to the current
12140 content of the bp_locations array. */
12141
12142 static void
12143 bp_locations_target_extensions_update (void)
12144 {
12145 struct bp_location *bl, **blp_tmp;
12146
12147 bp_locations_placed_address_before_address_max = 0;
12148 bp_locations_shadow_len_after_address_max = 0;
12149
12150 ALL_BP_LOCATIONS (bl, blp_tmp)
12151 {
12152 CORE_ADDR start, end, addr;
12153
12154 if (!bp_location_has_shadow (bl))
12155 continue;
12156
12157 start = bl->target_info.placed_address;
12158 end = start + bl->target_info.shadow_len;
12159
12160 gdb_assert (bl->address >= start);
12161 addr = bl->address - start;
12162 if (addr > bp_locations_placed_address_before_address_max)
12163 bp_locations_placed_address_before_address_max = addr;
12164
12165 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12166
12167 gdb_assert (bl->address < end);
12168 addr = end - bl->address;
12169 if (addr > bp_locations_shadow_len_after_address_max)
12170 bp_locations_shadow_len_after_address_max = addr;
12171 }
12172 }
12173
12174 /* Download tracepoint locations if they haven't been. */
12175
12176 static void
12177 download_tracepoint_locations (void)
12178 {
12179 struct breakpoint *b;
12180 struct cleanup *old_chain;
12181 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12182
12183 old_chain = save_current_space_and_thread ();
12184
12185 ALL_TRACEPOINTS (b)
12186 {
12187 struct bp_location *bl;
12188 struct tracepoint *t;
12189 int bp_location_downloaded = 0;
12190
12191 if ((b->type == bp_fast_tracepoint
12192 ? !may_insert_fast_tracepoints
12193 : !may_insert_tracepoints))
12194 continue;
12195
12196 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12197 {
12198 if (target_can_download_tracepoint ())
12199 can_download_tracepoint = TRIBOOL_TRUE;
12200 else
12201 can_download_tracepoint = TRIBOOL_FALSE;
12202 }
12203
12204 if (can_download_tracepoint == TRIBOOL_FALSE)
12205 break;
12206
12207 for (bl = b->loc; bl; bl = bl->next)
12208 {
12209 /* In tracepoint, locations are _never_ duplicated, so
12210 should_be_inserted is equivalent to
12211 unduplicated_should_be_inserted. */
12212 if (!should_be_inserted (bl) || bl->inserted)
12213 continue;
12214
12215 switch_to_program_space_and_thread (bl->pspace);
12216
12217 target_download_tracepoint (bl);
12218
12219 bl->inserted = 1;
12220 bp_location_downloaded = 1;
12221 }
12222 t = (struct tracepoint *) b;
12223 t->number_on_target = b->number;
12224 if (bp_location_downloaded)
12225 observer_notify_breakpoint_modified (b);
12226 }
12227
12228 do_cleanups (old_chain);
12229 }
12230
12231 /* Swap the insertion/duplication state between two locations. */
12232
12233 static void
12234 swap_insertion (struct bp_location *left, struct bp_location *right)
12235 {
12236 const int left_inserted = left->inserted;
12237 const int left_duplicate = left->duplicate;
12238 const int left_needs_update = left->needs_update;
12239 const struct bp_target_info left_target_info = left->target_info;
12240
12241 /* Locations of tracepoints can never be duplicated. */
12242 if (is_tracepoint (left->owner))
12243 gdb_assert (!left->duplicate);
12244 if (is_tracepoint (right->owner))
12245 gdb_assert (!right->duplicate);
12246
12247 left->inserted = right->inserted;
12248 left->duplicate = right->duplicate;
12249 left->needs_update = right->needs_update;
12250 left->target_info = right->target_info;
12251 right->inserted = left_inserted;
12252 right->duplicate = left_duplicate;
12253 right->needs_update = left_needs_update;
12254 right->target_info = left_target_info;
12255 }
12256
12257 /* Force the re-insertion of the locations at ADDRESS. This is called
12258 once a new/deleted/modified duplicate location is found and we are evaluating
12259 conditions on the target's side. Such conditions need to be updated on
12260 the target. */
12261
12262 static void
12263 force_breakpoint_reinsertion (struct bp_location *bl)
12264 {
12265 struct bp_location **locp = NULL, **loc2p;
12266 struct bp_location *loc;
12267 CORE_ADDR address = 0;
12268 int pspace_num;
12269
12270 address = bl->address;
12271 pspace_num = bl->pspace->num;
12272
12273 /* This is only meaningful if the target is
12274 evaluating conditions and if the user has
12275 opted for condition evaluation on the target's
12276 side. */
12277 if (gdb_evaluates_breakpoint_condition_p ()
12278 || !target_supports_evaluation_of_breakpoint_conditions ())
12279 return;
12280
12281 /* Flag all breakpoint locations with this address and
12282 the same program space as the location
12283 as "its condition has changed". We need to
12284 update the conditions on the target's side. */
12285 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12286 {
12287 loc = *loc2p;
12288
12289 if (!is_breakpoint (loc->owner)
12290 || pspace_num != loc->pspace->num)
12291 continue;
12292
12293 /* Flag the location appropriately. We use a different state to
12294 let everyone know that we already updated the set of locations
12295 with addr bl->address and program space bl->pspace. This is so
12296 we don't have to keep calling these functions just to mark locations
12297 that have already been marked. */
12298 loc->condition_changed = condition_updated;
12299
12300 /* Free the agent expression bytecode as well. We will compute
12301 it later on. */
12302 loc->cond_bytecode.reset ();
12303 }
12304 }
12305 /* Called whether new breakpoints are created, or existing breakpoints
12306 deleted, to update the global location list and recompute which
12307 locations are duplicate of which.
12308
12309 The INSERT_MODE flag determines whether locations may not, may, or
12310 shall be inserted now. See 'enum ugll_insert_mode' for more
12311 info. */
12312
12313 static void
12314 update_global_location_list (enum ugll_insert_mode insert_mode)
12315 {
12316 struct breakpoint *b;
12317 struct bp_location **locp, *loc;
12318 struct cleanup *cleanups;
12319 /* Last breakpoint location address that was marked for update. */
12320 CORE_ADDR last_addr = 0;
12321 /* Last breakpoint location program space that was marked for update. */
12322 int last_pspace_num = -1;
12323
12324 /* Used in the duplicates detection below. When iterating over all
12325 bp_locations, points to the first bp_location of a given address.
12326 Breakpoints and watchpoints of different types are never
12327 duplicates of each other. Keep one pointer for each type of
12328 breakpoint/watchpoint, so we only need to loop over all locations
12329 once. */
12330 struct bp_location *bp_loc_first; /* breakpoint */
12331 struct bp_location *wp_loc_first; /* hardware watchpoint */
12332 struct bp_location *awp_loc_first; /* access watchpoint */
12333 struct bp_location *rwp_loc_first; /* read watchpoint */
12334
12335 /* Saved former bp_locations array which we compare against the newly
12336 built bp_locations from the current state of ALL_BREAKPOINTS. */
12337 struct bp_location **old_locations, **old_locp;
12338 unsigned old_locations_count;
12339
12340 old_locations = bp_locations;
12341 old_locations_count = bp_locations_count;
12342 bp_locations = NULL;
12343 bp_locations_count = 0;
12344 cleanups = make_cleanup (xfree, old_locations);
12345
12346 ALL_BREAKPOINTS (b)
12347 for (loc = b->loc; loc; loc = loc->next)
12348 bp_locations_count++;
12349
12350 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12351 locp = bp_locations;
12352 ALL_BREAKPOINTS (b)
12353 for (loc = b->loc; loc; loc = loc->next)
12354 *locp++ = loc;
12355 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12356 bp_locations_compare);
12357
12358 bp_locations_target_extensions_update ();
12359
12360 /* Identify bp_location instances that are no longer present in the
12361 new list, and therefore should be freed. Note that it's not
12362 necessary that those locations should be removed from inferior --
12363 if there's another location at the same address (previously
12364 marked as duplicate), we don't need to remove/insert the
12365 location.
12366
12367 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12368 and former bp_location array state respectively. */
12369
12370 locp = bp_locations;
12371 for (old_locp = old_locations;
12372 old_locp < old_locations + old_locations_count;
12373 old_locp++)
12374 {
12375 struct bp_location *old_loc = *old_locp;
12376 struct bp_location **loc2p;
12377
12378 /* Tells if 'old_loc' is found among the new locations. If
12379 not, we have to free it. */
12380 int found_object = 0;
12381 /* Tells if the location should remain inserted in the target. */
12382 int keep_in_target = 0;
12383 int removed = 0;
12384
12385 /* Skip LOCP entries which will definitely never be needed.
12386 Stop either at or being the one matching OLD_LOC. */
12387 while (locp < bp_locations + bp_locations_count
12388 && (*locp)->address < old_loc->address)
12389 locp++;
12390
12391 for (loc2p = locp;
12392 (loc2p < bp_locations + bp_locations_count
12393 && (*loc2p)->address == old_loc->address);
12394 loc2p++)
12395 {
12396 /* Check if this is a new/duplicated location or a duplicated
12397 location that had its condition modified. If so, we want to send
12398 its condition to the target if evaluation of conditions is taking
12399 place there. */
12400 if ((*loc2p)->condition_changed == condition_modified
12401 && (last_addr != old_loc->address
12402 || last_pspace_num != old_loc->pspace->num))
12403 {
12404 force_breakpoint_reinsertion (*loc2p);
12405 last_pspace_num = old_loc->pspace->num;
12406 }
12407
12408 if (*loc2p == old_loc)
12409 found_object = 1;
12410 }
12411
12412 /* We have already handled this address, update it so that we don't
12413 have to go through updates again. */
12414 last_addr = old_loc->address;
12415
12416 /* Target-side condition evaluation: Handle deleted locations. */
12417 if (!found_object)
12418 force_breakpoint_reinsertion (old_loc);
12419
12420 /* If this location is no longer present, and inserted, look if
12421 there's maybe a new location at the same address. If so,
12422 mark that one inserted, and don't remove this one. This is
12423 needed so that we don't have a time window where a breakpoint
12424 at certain location is not inserted. */
12425
12426 if (old_loc->inserted)
12427 {
12428 /* If the location is inserted now, we might have to remove
12429 it. */
12430
12431 if (found_object && should_be_inserted (old_loc))
12432 {
12433 /* The location is still present in the location list,
12434 and still should be inserted. Don't do anything. */
12435 keep_in_target = 1;
12436 }
12437 else
12438 {
12439 /* This location still exists, but it won't be kept in the
12440 target since it may have been disabled. We proceed to
12441 remove its target-side condition. */
12442
12443 /* The location is either no longer present, or got
12444 disabled. See if there's another location at the
12445 same address, in which case we don't need to remove
12446 this one from the target. */
12447
12448 /* OLD_LOC comes from existing struct breakpoint. */
12449 if (breakpoint_address_is_meaningful (old_loc->owner))
12450 {
12451 for (loc2p = locp;
12452 (loc2p < bp_locations + bp_locations_count
12453 && (*loc2p)->address == old_loc->address);
12454 loc2p++)
12455 {
12456 struct bp_location *loc2 = *loc2p;
12457
12458 if (breakpoint_locations_match (loc2, old_loc))
12459 {
12460 /* Read watchpoint locations are switched to
12461 access watchpoints, if the former are not
12462 supported, but the latter are. */
12463 if (is_hardware_watchpoint (old_loc->owner))
12464 {
12465 gdb_assert (is_hardware_watchpoint (loc2->owner));
12466 loc2->watchpoint_type = old_loc->watchpoint_type;
12467 }
12468
12469 /* loc2 is a duplicated location. We need to check
12470 if it should be inserted in case it will be
12471 unduplicated. */
12472 if (loc2 != old_loc
12473 && unduplicated_should_be_inserted (loc2))
12474 {
12475 swap_insertion (old_loc, loc2);
12476 keep_in_target = 1;
12477 break;
12478 }
12479 }
12480 }
12481 }
12482 }
12483
12484 if (!keep_in_target)
12485 {
12486 if (remove_breakpoint (old_loc))
12487 {
12488 /* This is just about all we can do. We could keep
12489 this location on the global list, and try to
12490 remove it next time, but there's no particular
12491 reason why we will succeed next time.
12492
12493 Note that at this point, old_loc->owner is still
12494 valid, as delete_breakpoint frees the breakpoint
12495 only after calling us. */
12496 printf_filtered (_("warning: Error removing "
12497 "breakpoint %d\n"),
12498 old_loc->owner->number);
12499 }
12500 removed = 1;
12501 }
12502 }
12503
12504 if (!found_object)
12505 {
12506 if (removed && target_is_non_stop_p ()
12507 && need_moribund_for_location_type (old_loc))
12508 {
12509 /* This location was removed from the target. In
12510 non-stop mode, a race condition is possible where
12511 we've removed a breakpoint, but stop events for that
12512 breakpoint are already queued and will arrive later.
12513 We apply an heuristic to be able to distinguish such
12514 SIGTRAPs from other random SIGTRAPs: we keep this
12515 breakpoint location for a bit, and will retire it
12516 after we see some number of events. The theory here
12517 is that reporting of events should, "on the average",
12518 be fair, so after a while we'll see events from all
12519 threads that have anything of interest, and no longer
12520 need to keep this breakpoint location around. We
12521 don't hold locations forever so to reduce chances of
12522 mistaking a non-breakpoint SIGTRAP for a breakpoint
12523 SIGTRAP.
12524
12525 The heuristic failing can be disastrous on
12526 decr_pc_after_break targets.
12527
12528 On decr_pc_after_break targets, like e.g., x86-linux,
12529 if we fail to recognize a late breakpoint SIGTRAP,
12530 because events_till_retirement has reached 0 too
12531 soon, we'll fail to do the PC adjustment, and report
12532 a random SIGTRAP to the user. When the user resumes
12533 the inferior, it will most likely immediately crash
12534 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12535 corrupted, because of being resumed e.g., in the
12536 middle of a multi-byte instruction, or skipped a
12537 one-byte instruction. This was actually seen happen
12538 on native x86-linux, and should be less rare on
12539 targets that do not support new thread events, like
12540 remote, due to the heuristic depending on
12541 thread_count.
12542
12543 Mistaking a random SIGTRAP for a breakpoint trap
12544 causes similar symptoms (PC adjustment applied when
12545 it shouldn't), but then again, playing with SIGTRAPs
12546 behind the debugger's back is asking for trouble.
12547
12548 Since hardware watchpoint traps are always
12549 distinguishable from other traps, so we don't need to
12550 apply keep hardware watchpoint moribund locations
12551 around. We simply always ignore hardware watchpoint
12552 traps we can no longer explain. */
12553
12554 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12555 old_loc->owner = NULL;
12556
12557 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12558 }
12559 else
12560 {
12561 old_loc->owner = NULL;
12562 decref_bp_location (&old_loc);
12563 }
12564 }
12565 }
12566
12567 /* Rescan breakpoints at the same address and section, marking the
12568 first one as "first" and any others as "duplicates". This is so
12569 that the bpt instruction is only inserted once. If we have a
12570 permanent breakpoint at the same place as BPT, make that one the
12571 official one, and the rest as duplicates. Permanent breakpoints
12572 are sorted first for the same address.
12573
12574 Do the same for hardware watchpoints, but also considering the
12575 watchpoint's type (regular/access/read) and length. */
12576
12577 bp_loc_first = NULL;
12578 wp_loc_first = NULL;
12579 awp_loc_first = NULL;
12580 rwp_loc_first = NULL;
12581 ALL_BP_LOCATIONS (loc, locp)
12582 {
12583 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12584 non-NULL. */
12585 struct bp_location **loc_first_p;
12586 b = loc->owner;
12587
12588 if (!unduplicated_should_be_inserted (loc)
12589 || !breakpoint_address_is_meaningful (b)
12590 /* Don't detect duplicate for tracepoint locations because they are
12591 never duplicated. See the comments in field `duplicate' of
12592 `struct bp_location'. */
12593 || is_tracepoint (b))
12594 {
12595 /* Clear the condition modification flag. */
12596 loc->condition_changed = condition_unchanged;
12597 continue;
12598 }
12599
12600 if (b->type == bp_hardware_watchpoint)
12601 loc_first_p = &wp_loc_first;
12602 else if (b->type == bp_read_watchpoint)
12603 loc_first_p = &rwp_loc_first;
12604 else if (b->type == bp_access_watchpoint)
12605 loc_first_p = &awp_loc_first;
12606 else
12607 loc_first_p = &bp_loc_first;
12608
12609 if (*loc_first_p == NULL
12610 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12611 || !breakpoint_locations_match (loc, *loc_first_p))
12612 {
12613 *loc_first_p = loc;
12614 loc->duplicate = 0;
12615
12616 if (is_breakpoint (loc->owner) && loc->condition_changed)
12617 {
12618 loc->needs_update = 1;
12619 /* Clear the condition modification flag. */
12620 loc->condition_changed = condition_unchanged;
12621 }
12622 continue;
12623 }
12624
12625
12626 /* This and the above ensure the invariant that the first location
12627 is not duplicated, and is the inserted one.
12628 All following are marked as duplicated, and are not inserted. */
12629 if (loc->inserted)
12630 swap_insertion (loc, *loc_first_p);
12631 loc->duplicate = 1;
12632
12633 /* Clear the condition modification flag. */
12634 loc->condition_changed = condition_unchanged;
12635 }
12636
12637 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12638 {
12639 if (insert_mode != UGLL_DONT_INSERT)
12640 insert_breakpoint_locations ();
12641 else
12642 {
12643 /* Even though the caller told us to not insert new
12644 locations, we may still need to update conditions on the
12645 target's side of breakpoints that were already inserted
12646 if the target is evaluating breakpoint conditions. We
12647 only update conditions for locations that are marked
12648 "needs_update". */
12649 update_inserted_breakpoint_locations ();
12650 }
12651 }
12652
12653 if (insert_mode != UGLL_DONT_INSERT)
12654 download_tracepoint_locations ();
12655
12656 do_cleanups (cleanups);
12657 }
12658
12659 void
12660 breakpoint_retire_moribund (void)
12661 {
12662 struct bp_location *loc;
12663 int ix;
12664
12665 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12666 if (--(loc->events_till_retirement) == 0)
12667 {
12668 decref_bp_location (&loc);
12669 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12670 --ix;
12671 }
12672 }
12673
12674 static void
12675 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12676 {
12677
12678 TRY
12679 {
12680 update_global_location_list (insert_mode);
12681 }
12682 CATCH (e, RETURN_MASK_ERROR)
12683 {
12684 }
12685 END_CATCH
12686 }
12687
12688 /* Clear BKP from a BPS. */
12689
12690 static void
12691 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12692 {
12693 bpstat bs;
12694
12695 for (bs = bps; bs; bs = bs->next)
12696 if (bs->breakpoint_at == bpt)
12697 {
12698 bs->breakpoint_at = NULL;
12699 bs->old_val = NULL;
12700 /* bs->commands will be freed later. */
12701 }
12702 }
12703
12704 /* Callback for iterate_over_threads. */
12705 static int
12706 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12707 {
12708 struct breakpoint *bpt = (struct breakpoint *) data;
12709
12710 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12711 return 0;
12712 }
12713
12714 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12715 callbacks. */
12716
12717 static void
12718 say_where (struct breakpoint *b)
12719 {
12720 struct value_print_options opts;
12721
12722 get_user_print_options (&opts);
12723
12724 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12725 single string. */
12726 if (b->loc == NULL)
12727 {
12728 /* For pending locations, the output differs slightly based
12729 on b->extra_string. If this is non-NULL, it contains either
12730 a condition or dprintf arguments. */
12731 if (b->extra_string == NULL)
12732 {
12733 printf_filtered (_(" (%s) pending."),
12734 event_location_to_string (b->location.get ()));
12735 }
12736 else if (b->type == bp_dprintf)
12737 {
12738 printf_filtered (_(" (%s,%s) pending."),
12739 event_location_to_string (b->location.get ()),
12740 b->extra_string);
12741 }
12742 else
12743 {
12744 printf_filtered (_(" (%s %s) pending."),
12745 event_location_to_string (b->location.get ()),
12746 b->extra_string);
12747 }
12748 }
12749 else
12750 {
12751 if (opts.addressprint || b->loc->symtab == NULL)
12752 {
12753 printf_filtered (" at ");
12754 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12755 gdb_stdout);
12756 }
12757 if (b->loc->symtab != NULL)
12758 {
12759 /* If there is a single location, we can print the location
12760 more nicely. */
12761 if (b->loc->next == NULL)
12762 printf_filtered (": file %s, line %d.",
12763 symtab_to_filename_for_display (b->loc->symtab),
12764 b->loc->line_number);
12765 else
12766 /* This is not ideal, but each location may have a
12767 different file name, and this at least reflects the
12768 real situation somewhat. */
12769 printf_filtered (": %s.",
12770 event_location_to_string (b->location.get ()));
12771 }
12772
12773 if (b->loc->next)
12774 {
12775 struct bp_location *loc = b->loc;
12776 int n = 0;
12777 for (; loc; loc = loc->next)
12778 ++n;
12779 printf_filtered (" (%d locations)", n);
12780 }
12781 }
12782 }
12783
12784 /* Default bp_location_ops methods. */
12785
12786 static void
12787 bp_location_dtor (struct bp_location *self)
12788 {
12789 xfree (self->function_name);
12790 }
12791
12792 static const struct bp_location_ops bp_location_ops =
12793 {
12794 bp_location_dtor
12795 };
12796
12797 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12798 inherit from. */
12799
12800 static void
12801 base_breakpoint_dtor (struct breakpoint *self)
12802 {
12803 decref_counted_command_line (&self->commands);
12804 xfree (self->cond_string);
12805 xfree (self->extra_string);
12806 xfree (self->filter);
12807 }
12808
12809 static struct bp_location *
12810 base_breakpoint_allocate_location (struct breakpoint *self)
12811 {
12812 return new bp_location (&bp_location_ops, self);
12813 }
12814
12815 static void
12816 base_breakpoint_re_set (struct breakpoint *b)
12817 {
12818 /* Nothing to re-set. */
12819 }
12820
12821 #define internal_error_pure_virtual_called() \
12822 gdb_assert_not_reached ("pure virtual function called")
12823
12824 static int
12825 base_breakpoint_insert_location (struct bp_location *bl)
12826 {
12827 internal_error_pure_virtual_called ();
12828 }
12829
12830 static int
12831 base_breakpoint_remove_location (struct bp_location *bl,
12832 enum remove_bp_reason reason)
12833 {
12834 internal_error_pure_virtual_called ();
12835 }
12836
12837 static int
12838 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12839 struct address_space *aspace,
12840 CORE_ADDR bp_addr,
12841 const struct target_waitstatus *ws)
12842 {
12843 internal_error_pure_virtual_called ();
12844 }
12845
12846 static void
12847 base_breakpoint_check_status (bpstat bs)
12848 {
12849 /* Always stop. */
12850 }
12851
12852 /* A "works_in_software_mode" breakpoint_ops method that just internal
12853 errors. */
12854
12855 static int
12856 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12857 {
12858 internal_error_pure_virtual_called ();
12859 }
12860
12861 /* A "resources_needed" breakpoint_ops method that just internal
12862 errors. */
12863
12864 static int
12865 base_breakpoint_resources_needed (const struct bp_location *bl)
12866 {
12867 internal_error_pure_virtual_called ();
12868 }
12869
12870 static enum print_stop_action
12871 base_breakpoint_print_it (bpstat bs)
12872 {
12873 internal_error_pure_virtual_called ();
12874 }
12875
12876 static void
12877 base_breakpoint_print_one_detail (const struct breakpoint *self,
12878 struct ui_out *uiout)
12879 {
12880 /* nothing */
12881 }
12882
12883 static void
12884 base_breakpoint_print_mention (struct breakpoint *b)
12885 {
12886 internal_error_pure_virtual_called ();
12887 }
12888
12889 static void
12890 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12891 {
12892 internal_error_pure_virtual_called ();
12893 }
12894
12895 static void
12896 base_breakpoint_create_sals_from_location
12897 (const struct event_location *location,
12898 struct linespec_result *canonical,
12899 enum bptype type_wanted)
12900 {
12901 internal_error_pure_virtual_called ();
12902 }
12903
12904 static void
12905 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12906 struct linespec_result *c,
12907 char *cond_string,
12908 char *extra_string,
12909 enum bptype type_wanted,
12910 enum bpdisp disposition,
12911 int thread,
12912 int task, int ignore_count,
12913 const struct breakpoint_ops *o,
12914 int from_tty, int enabled,
12915 int internal, unsigned flags)
12916 {
12917 internal_error_pure_virtual_called ();
12918 }
12919
12920 static void
12921 base_breakpoint_decode_location (struct breakpoint *b,
12922 const struct event_location *location,
12923 struct program_space *search_pspace,
12924 struct symtabs_and_lines *sals)
12925 {
12926 internal_error_pure_virtual_called ();
12927 }
12928
12929 /* The default 'explains_signal' method. */
12930
12931 static int
12932 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12933 {
12934 return 1;
12935 }
12936
12937 /* The default "after_condition_true" method. */
12938
12939 static void
12940 base_breakpoint_after_condition_true (struct bpstats *bs)
12941 {
12942 /* Nothing to do. */
12943 }
12944
12945 struct breakpoint_ops base_breakpoint_ops =
12946 {
12947 base_breakpoint_dtor,
12948 base_breakpoint_allocate_location,
12949 base_breakpoint_re_set,
12950 base_breakpoint_insert_location,
12951 base_breakpoint_remove_location,
12952 base_breakpoint_breakpoint_hit,
12953 base_breakpoint_check_status,
12954 base_breakpoint_resources_needed,
12955 base_breakpoint_works_in_software_mode,
12956 base_breakpoint_print_it,
12957 NULL,
12958 base_breakpoint_print_one_detail,
12959 base_breakpoint_print_mention,
12960 base_breakpoint_print_recreate,
12961 base_breakpoint_create_sals_from_location,
12962 base_breakpoint_create_breakpoints_sal,
12963 base_breakpoint_decode_location,
12964 base_breakpoint_explains_signal,
12965 base_breakpoint_after_condition_true,
12966 };
12967
12968 /* Default breakpoint_ops methods. */
12969
12970 static void
12971 bkpt_re_set (struct breakpoint *b)
12972 {
12973 /* FIXME: is this still reachable? */
12974 if (breakpoint_event_location_empty_p (b))
12975 {
12976 /* Anything without a location can't be re-set. */
12977 delete_breakpoint (b);
12978 return;
12979 }
12980
12981 breakpoint_re_set_default (b);
12982 }
12983
12984 static int
12985 bkpt_insert_location (struct bp_location *bl)
12986 {
12987 CORE_ADDR addr = bl->target_info.reqstd_address;
12988
12989 bl->target_info.kind = breakpoint_kind (bl, &addr);
12990 bl->target_info.placed_address = addr;
12991
12992 if (bl->loc_type == bp_loc_hardware_breakpoint)
12993 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12994 else
12995 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12996 }
12997
12998 static int
12999 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13000 {
13001 if (bl->loc_type == bp_loc_hardware_breakpoint)
13002 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13003 else
13004 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13005 }
13006
13007 static int
13008 bkpt_breakpoint_hit (const struct bp_location *bl,
13009 struct address_space *aspace, CORE_ADDR bp_addr,
13010 const struct target_waitstatus *ws)
13011 {
13012 if (ws->kind != TARGET_WAITKIND_STOPPED
13013 || ws->value.sig != GDB_SIGNAL_TRAP)
13014 return 0;
13015
13016 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13017 aspace, bp_addr))
13018 return 0;
13019
13020 if (overlay_debugging /* unmapped overlay section */
13021 && section_is_overlay (bl->section)
13022 && !section_is_mapped (bl->section))
13023 return 0;
13024
13025 return 1;
13026 }
13027
13028 static int
13029 dprintf_breakpoint_hit (const struct bp_location *bl,
13030 struct address_space *aspace, CORE_ADDR bp_addr,
13031 const struct target_waitstatus *ws)
13032 {
13033 if (dprintf_style == dprintf_style_agent
13034 && target_can_run_breakpoint_commands ())
13035 {
13036 /* An agent-style dprintf never causes a stop. If we see a trap
13037 for this address it must be for a breakpoint that happens to
13038 be set at the same address. */
13039 return 0;
13040 }
13041
13042 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13043 }
13044
13045 static int
13046 bkpt_resources_needed (const struct bp_location *bl)
13047 {
13048 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13049
13050 return 1;
13051 }
13052
13053 static enum print_stop_action
13054 bkpt_print_it (bpstat bs)
13055 {
13056 struct breakpoint *b;
13057 const struct bp_location *bl;
13058 int bp_temp;
13059 struct ui_out *uiout = current_uiout;
13060
13061 gdb_assert (bs->bp_location_at != NULL);
13062
13063 bl = bs->bp_location_at;
13064 b = bs->breakpoint_at;
13065
13066 bp_temp = b->disposition == disp_del;
13067 if (bl->address != bl->requested_address)
13068 breakpoint_adjustment_warning (bl->requested_address,
13069 bl->address,
13070 b->number, 1);
13071 annotate_breakpoint (b->number);
13072 maybe_print_thread_hit_breakpoint (uiout);
13073
13074 if (bp_temp)
13075 uiout->text ("Temporary breakpoint ");
13076 else
13077 uiout->text ("Breakpoint ");
13078 if (uiout->is_mi_like_p ())
13079 {
13080 uiout->field_string ("reason",
13081 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13082 uiout->field_string ("disp", bpdisp_text (b->disposition));
13083 }
13084 uiout->field_int ("bkptno", b->number);
13085 uiout->text (", ");
13086
13087 return PRINT_SRC_AND_LOC;
13088 }
13089
13090 static void
13091 bkpt_print_mention (struct breakpoint *b)
13092 {
13093 if (current_uiout->is_mi_like_p ())
13094 return;
13095
13096 switch (b->type)
13097 {
13098 case bp_breakpoint:
13099 case bp_gnu_ifunc_resolver:
13100 if (b->disposition == disp_del)
13101 printf_filtered (_("Temporary breakpoint"));
13102 else
13103 printf_filtered (_("Breakpoint"));
13104 printf_filtered (_(" %d"), b->number);
13105 if (b->type == bp_gnu_ifunc_resolver)
13106 printf_filtered (_(" at gnu-indirect-function resolver"));
13107 break;
13108 case bp_hardware_breakpoint:
13109 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13110 break;
13111 case bp_dprintf:
13112 printf_filtered (_("Dprintf %d"), b->number);
13113 break;
13114 }
13115
13116 say_where (b);
13117 }
13118
13119 static void
13120 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13121 {
13122 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13123 fprintf_unfiltered (fp, "tbreak");
13124 else if (tp->type == bp_breakpoint)
13125 fprintf_unfiltered (fp, "break");
13126 else if (tp->type == bp_hardware_breakpoint
13127 && tp->disposition == disp_del)
13128 fprintf_unfiltered (fp, "thbreak");
13129 else if (tp->type == bp_hardware_breakpoint)
13130 fprintf_unfiltered (fp, "hbreak");
13131 else
13132 internal_error (__FILE__, __LINE__,
13133 _("unhandled breakpoint type %d"), (int) tp->type);
13134
13135 fprintf_unfiltered (fp, " %s",
13136 event_location_to_string (tp->location.get ()));
13137
13138 /* Print out extra_string if this breakpoint is pending. It might
13139 contain, for example, conditions that were set by the user. */
13140 if (tp->loc == NULL && tp->extra_string != NULL)
13141 fprintf_unfiltered (fp, " %s", tp->extra_string);
13142
13143 print_recreate_thread (tp, fp);
13144 }
13145
13146 static void
13147 bkpt_create_sals_from_location (const struct event_location *location,
13148 struct linespec_result *canonical,
13149 enum bptype type_wanted)
13150 {
13151 create_sals_from_location_default (location, canonical, type_wanted);
13152 }
13153
13154 static void
13155 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13156 struct linespec_result *canonical,
13157 char *cond_string,
13158 char *extra_string,
13159 enum bptype type_wanted,
13160 enum bpdisp disposition,
13161 int thread,
13162 int task, int ignore_count,
13163 const struct breakpoint_ops *ops,
13164 int from_tty, int enabled,
13165 int internal, unsigned flags)
13166 {
13167 create_breakpoints_sal_default (gdbarch, canonical,
13168 cond_string, extra_string,
13169 type_wanted,
13170 disposition, thread, task,
13171 ignore_count, ops, from_tty,
13172 enabled, internal, flags);
13173 }
13174
13175 static void
13176 bkpt_decode_location (struct breakpoint *b,
13177 const struct event_location *location,
13178 struct program_space *search_pspace,
13179 struct symtabs_and_lines *sals)
13180 {
13181 decode_location_default (b, location, search_pspace, sals);
13182 }
13183
13184 /* Virtual table for internal breakpoints. */
13185
13186 static void
13187 internal_bkpt_re_set (struct breakpoint *b)
13188 {
13189 switch (b->type)
13190 {
13191 /* Delete overlay event and longjmp master breakpoints; they
13192 will be reset later by breakpoint_re_set. */
13193 case bp_overlay_event:
13194 case bp_longjmp_master:
13195 case bp_std_terminate_master:
13196 case bp_exception_master:
13197 delete_breakpoint (b);
13198 break;
13199
13200 /* This breakpoint is special, it's set up when the inferior
13201 starts and we really don't want to touch it. */
13202 case bp_shlib_event:
13203
13204 /* Like bp_shlib_event, this breakpoint type is special. Once
13205 it is set up, we do not want to touch it. */
13206 case bp_thread_event:
13207 break;
13208 }
13209 }
13210
13211 static void
13212 internal_bkpt_check_status (bpstat bs)
13213 {
13214 if (bs->breakpoint_at->type == bp_shlib_event)
13215 {
13216 /* If requested, stop when the dynamic linker notifies GDB of
13217 events. This allows the user to get control and place
13218 breakpoints in initializer routines for dynamically loaded
13219 objects (among other things). */
13220 bs->stop = stop_on_solib_events;
13221 bs->print = stop_on_solib_events;
13222 }
13223 else
13224 bs->stop = 0;
13225 }
13226
13227 static enum print_stop_action
13228 internal_bkpt_print_it (bpstat bs)
13229 {
13230 struct breakpoint *b;
13231
13232 b = bs->breakpoint_at;
13233
13234 switch (b->type)
13235 {
13236 case bp_shlib_event:
13237 /* Did we stop because the user set the stop_on_solib_events
13238 variable? (If so, we report this as a generic, "Stopped due
13239 to shlib event" message.) */
13240 print_solib_event (0);
13241 break;
13242
13243 case bp_thread_event:
13244 /* Not sure how we will get here.
13245 GDB should not stop for these breakpoints. */
13246 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13247 break;
13248
13249 case bp_overlay_event:
13250 /* By analogy with the thread event, GDB should not stop for these. */
13251 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13252 break;
13253
13254 case bp_longjmp_master:
13255 /* These should never be enabled. */
13256 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13257 break;
13258
13259 case bp_std_terminate_master:
13260 /* These should never be enabled. */
13261 printf_filtered (_("std::terminate Master Breakpoint: "
13262 "gdb should not stop!\n"));
13263 break;
13264
13265 case bp_exception_master:
13266 /* These should never be enabled. */
13267 printf_filtered (_("Exception Master Breakpoint: "
13268 "gdb should not stop!\n"));
13269 break;
13270 }
13271
13272 return PRINT_NOTHING;
13273 }
13274
13275 static void
13276 internal_bkpt_print_mention (struct breakpoint *b)
13277 {
13278 /* Nothing to mention. These breakpoints are internal. */
13279 }
13280
13281 /* Virtual table for momentary breakpoints */
13282
13283 static void
13284 momentary_bkpt_re_set (struct breakpoint *b)
13285 {
13286 /* Keep temporary breakpoints, which can be encountered when we step
13287 over a dlopen call and solib_add is resetting the breakpoints.
13288 Otherwise these should have been blown away via the cleanup chain
13289 or by breakpoint_init_inferior when we rerun the executable. */
13290 }
13291
13292 static void
13293 momentary_bkpt_check_status (bpstat bs)
13294 {
13295 /* Nothing. The point of these breakpoints is causing a stop. */
13296 }
13297
13298 static enum print_stop_action
13299 momentary_bkpt_print_it (bpstat bs)
13300 {
13301 return PRINT_UNKNOWN;
13302 }
13303
13304 static void
13305 momentary_bkpt_print_mention (struct breakpoint *b)
13306 {
13307 /* Nothing to mention. These breakpoints are internal. */
13308 }
13309
13310 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13311
13312 It gets cleared already on the removal of the first one of such placed
13313 breakpoints. This is OK as they get all removed altogether. */
13314
13315 static void
13316 longjmp_bkpt_dtor (struct breakpoint *self)
13317 {
13318 struct thread_info *tp = find_thread_global_id (self->thread);
13319
13320 if (tp)
13321 tp->initiating_frame = null_frame_id;
13322
13323 momentary_breakpoint_ops.dtor (self);
13324 }
13325
13326 /* Specific methods for probe breakpoints. */
13327
13328 static int
13329 bkpt_probe_insert_location (struct bp_location *bl)
13330 {
13331 int v = bkpt_insert_location (bl);
13332
13333 if (v == 0)
13334 {
13335 /* The insertion was successful, now let's set the probe's semaphore
13336 if needed. */
13337 if (bl->probe.probe->pops->set_semaphore != NULL)
13338 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13339 bl->probe.objfile,
13340 bl->gdbarch);
13341 }
13342
13343 return v;
13344 }
13345
13346 static int
13347 bkpt_probe_remove_location (struct bp_location *bl,
13348 enum remove_bp_reason reason)
13349 {
13350 /* Let's clear the semaphore before removing the location. */
13351 if (bl->probe.probe->pops->clear_semaphore != NULL)
13352 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13353 bl->probe.objfile,
13354 bl->gdbarch);
13355
13356 return bkpt_remove_location (bl, reason);
13357 }
13358
13359 static void
13360 bkpt_probe_create_sals_from_location (const struct event_location *location,
13361 struct linespec_result *canonical,
13362 enum bptype type_wanted)
13363 {
13364 struct linespec_sals lsal;
13365
13366 lsal.sals = parse_probes (location, NULL, canonical);
13367 lsal.canonical
13368 = xstrdup (event_location_to_string (canonical->location.get ()));
13369 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13370 }
13371
13372 static void
13373 bkpt_probe_decode_location (struct breakpoint *b,
13374 const struct event_location *location,
13375 struct program_space *search_pspace,
13376 struct symtabs_and_lines *sals)
13377 {
13378 *sals = parse_probes (location, search_pspace, NULL);
13379 if (!sals->sals)
13380 error (_("probe not found"));
13381 }
13382
13383 /* The breakpoint_ops structure to be used in tracepoints. */
13384
13385 static void
13386 tracepoint_re_set (struct breakpoint *b)
13387 {
13388 breakpoint_re_set_default (b);
13389 }
13390
13391 static int
13392 tracepoint_breakpoint_hit (const struct bp_location *bl,
13393 struct address_space *aspace, CORE_ADDR bp_addr,
13394 const struct target_waitstatus *ws)
13395 {
13396 /* By definition, the inferior does not report stops at
13397 tracepoints. */
13398 return 0;
13399 }
13400
13401 static void
13402 tracepoint_print_one_detail (const struct breakpoint *self,
13403 struct ui_out *uiout)
13404 {
13405 struct tracepoint *tp = (struct tracepoint *) self;
13406 if (tp->static_trace_marker_id)
13407 {
13408 gdb_assert (self->type == bp_static_tracepoint);
13409
13410 uiout->text ("\tmarker id is ");
13411 uiout->field_string ("static-tracepoint-marker-string-id",
13412 tp->static_trace_marker_id);
13413 uiout->text ("\n");
13414 }
13415 }
13416
13417 static void
13418 tracepoint_print_mention (struct breakpoint *b)
13419 {
13420 if (current_uiout->is_mi_like_p ())
13421 return;
13422
13423 switch (b->type)
13424 {
13425 case bp_tracepoint:
13426 printf_filtered (_("Tracepoint"));
13427 printf_filtered (_(" %d"), b->number);
13428 break;
13429 case bp_fast_tracepoint:
13430 printf_filtered (_("Fast tracepoint"));
13431 printf_filtered (_(" %d"), b->number);
13432 break;
13433 case bp_static_tracepoint:
13434 printf_filtered (_("Static tracepoint"));
13435 printf_filtered (_(" %d"), b->number);
13436 break;
13437 default:
13438 internal_error (__FILE__, __LINE__,
13439 _("unhandled tracepoint type %d"), (int) b->type);
13440 }
13441
13442 say_where (b);
13443 }
13444
13445 static void
13446 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13447 {
13448 struct tracepoint *tp = (struct tracepoint *) self;
13449
13450 if (self->type == bp_fast_tracepoint)
13451 fprintf_unfiltered (fp, "ftrace");
13452 else if (self->type == bp_static_tracepoint)
13453 fprintf_unfiltered (fp, "strace");
13454 else if (self->type == bp_tracepoint)
13455 fprintf_unfiltered (fp, "trace");
13456 else
13457 internal_error (__FILE__, __LINE__,
13458 _("unhandled tracepoint type %d"), (int) self->type);
13459
13460 fprintf_unfiltered (fp, " %s",
13461 event_location_to_string (self->location.get ()));
13462 print_recreate_thread (self, fp);
13463
13464 if (tp->pass_count)
13465 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13466 }
13467
13468 static void
13469 tracepoint_create_sals_from_location (const struct event_location *location,
13470 struct linespec_result *canonical,
13471 enum bptype type_wanted)
13472 {
13473 create_sals_from_location_default (location, canonical, type_wanted);
13474 }
13475
13476 static void
13477 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13478 struct linespec_result *canonical,
13479 char *cond_string,
13480 char *extra_string,
13481 enum bptype type_wanted,
13482 enum bpdisp disposition,
13483 int thread,
13484 int task, int ignore_count,
13485 const struct breakpoint_ops *ops,
13486 int from_tty, int enabled,
13487 int internal, unsigned flags)
13488 {
13489 create_breakpoints_sal_default (gdbarch, canonical,
13490 cond_string, extra_string,
13491 type_wanted,
13492 disposition, thread, task,
13493 ignore_count, ops, from_tty,
13494 enabled, internal, flags);
13495 }
13496
13497 static void
13498 tracepoint_decode_location (struct breakpoint *b,
13499 const struct event_location *location,
13500 struct program_space *search_pspace,
13501 struct symtabs_and_lines *sals)
13502 {
13503 decode_location_default (b, location, search_pspace, sals);
13504 }
13505
13506 struct breakpoint_ops tracepoint_breakpoint_ops;
13507
13508 /* The breakpoint_ops structure to be use on tracepoints placed in a
13509 static probe. */
13510
13511 static void
13512 tracepoint_probe_create_sals_from_location
13513 (const struct event_location *location,
13514 struct linespec_result *canonical,
13515 enum bptype type_wanted)
13516 {
13517 /* We use the same method for breakpoint on probes. */
13518 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13519 }
13520
13521 static void
13522 tracepoint_probe_decode_location (struct breakpoint *b,
13523 const struct event_location *location,
13524 struct program_space *search_pspace,
13525 struct symtabs_and_lines *sals)
13526 {
13527 /* We use the same method for breakpoint on probes. */
13528 bkpt_probe_decode_location (b, location, search_pspace, sals);
13529 }
13530
13531 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13532
13533 /* Dprintf breakpoint_ops methods. */
13534
13535 static void
13536 dprintf_re_set (struct breakpoint *b)
13537 {
13538 breakpoint_re_set_default (b);
13539
13540 /* extra_string should never be non-NULL for dprintf. */
13541 gdb_assert (b->extra_string != NULL);
13542
13543 /* 1 - connect to target 1, that can run breakpoint commands.
13544 2 - create a dprintf, which resolves fine.
13545 3 - disconnect from target 1
13546 4 - connect to target 2, that can NOT run breakpoint commands.
13547
13548 After steps #3/#4, you'll want the dprintf command list to
13549 be updated, because target 1 and 2 may well return different
13550 answers for target_can_run_breakpoint_commands().
13551 Given absence of finer grained resetting, we get to do
13552 it all the time. */
13553 if (b->extra_string != NULL)
13554 update_dprintf_command_list (b);
13555 }
13556
13557 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13558
13559 static void
13560 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13561 {
13562 fprintf_unfiltered (fp, "dprintf %s,%s",
13563 event_location_to_string (tp->location.get ()),
13564 tp->extra_string);
13565 print_recreate_thread (tp, fp);
13566 }
13567
13568 /* Implement the "after_condition_true" breakpoint_ops method for
13569 dprintf.
13570
13571 dprintf's are implemented with regular commands in their command
13572 list, but we run the commands here instead of before presenting the
13573 stop to the user, as dprintf's don't actually cause a stop. This
13574 also makes it so that the commands of multiple dprintfs at the same
13575 address are all handled. */
13576
13577 static void
13578 dprintf_after_condition_true (struct bpstats *bs)
13579 {
13580 struct cleanup *old_chain;
13581 struct bpstats tmp_bs = { NULL };
13582 struct bpstats *tmp_bs_p = &tmp_bs;
13583
13584 /* dprintf's never cause a stop. This wasn't set in the
13585 check_status hook instead because that would make the dprintf's
13586 condition not be evaluated. */
13587 bs->stop = 0;
13588
13589 /* Run the command list here. Take ownership of it instead of
13590 copying. We never want these commands to run later in
13591 bpstat_do_actions, if a breakpoint that causes a stop happens to
13592 be set at same address as this dprintf, or even if running the
13593 commands here throws. */
13594 tmp_bs.commands = bs->commands;
13595 bs->commands = NULL;
13596 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13597
13598 bpstat_do_actions_1 (&tmp_bs_p);
13599
13600 /* 'tmp_bs.commands' will usually be NULL by now, but
13601 bpstat_do_actions_1 may return early without processing the whole
13602 list. */
13603 do_cleanups (old_chain);
13604 }
13605
13606 /* The breakpoint_ops structure to be used on static tracepoints with
13607 markers (`-m'). */
13608
13609 static void
13610 strace_marker_create_sals_from_location (const struct event_location *location,
13611 struct linespec_result *canonical,
13612 enum bptype type_wanted)
13613 {
13614 struct linespec_sals lsal;
13615 const char *arg_start, *arg;
13616 char *str;
13617 struct cleanup *cleanup;
13618
13619 arg = arg_start = get_linespec_location (location);
13620 lsal.sals = decode_static_tracepoint_spec (&arg);
13621
13622 str = savestring (arg_start, arg - arg_start);
13623 cleanup = make_cleanup (xfree, str);
13624 canonical->location = new_linespec_location (&str);
13625 do_cleanups (cleanup);
13626
13627 lsal.canonical
13628 = xstrdup (event_location_to_string (canonical->location.get ()));
13629 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13630 }
13631
13632 static void
13633 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13634 struct linespec_result *canonical,
13635 char *cond_string,
13636 char *extra_string,
13637 enum bptype type_wanted,
13638 enum bpdisp disposition,
13639 int thread,
13640 int task, int ignore_count,
13641 const struct breakpoint_ops *ops,
13642 int from_tty, int enabled,
13643 int internal, unsigned flags)
13644 {
13645 int i;
13646 struct linespec_sals *lsal = VEC_index (linespec_sals,
13647 canonical->sals, 0);
13648
13649 /* If the user is creating a static tracepoint by marker id
13650 (strace -m MARKER_ID), then store the sals index, so that
13651 breakpoint_re_set can try to match up which of the newly
13652 found markers corresponds to this one, and, don't try to
13653 expand multiple locations for each sal, given than SALS
13654 already should contain all sals for MARKER_ID. */
13655
13656 for (i = 0; i < lsal->sals.nelts; ++i)
13657 {
13658 struct symtabs_and_lines expanded;
13659 struct tracepoint *tp;
13660 event_location_up location;
13661
13662 expanded.nelts = 1;
13663 expanded.sals = &lsal->sals.sals[i];
13664
13665 location = copy_event_location (canonical->location.get ());
13666
13667 tp = new tracepoint ();
13668 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13669 std::move (location), NULL,
13670 cond_string, extra_string,
13671 type_wanted, disposition,
13672 thread, task, ignore_count, ops,
13673 from_tty, enabled, internal, flags,
13674 canonical->special_display);
13675 /* Given that its possible to have multiple markers with
13676 the same string id, if the user is creating a static
13677 tracepoint by marker id ("strace -m MARKER_ID"), then
13678 store the sals index, so that breakpoint_re_set can
13679 try to match up which of the newly found markers
13680 corresponds to this one */
13681 tp->static_trace_marker_id_idx = i;
13682
13683 install_breakpoint (internal, &tp->base, 0);
13684 }
13685 }
13686
13687 static void
13688 strace_marker_decode_location (struct breakpoint *b,
13689 const struct event_location *location,
13690 struct program_space *search_pspace,
13691 struct symtabs_and_lines *sals)
13692 {
13693 struct tracepoint *tp = (struct tracepoint *) b;
13694 const char *s = get_linespec_location (location);
13695
13696 *sals = decode_static_tracepoint_spec (&s);
13697 if (sals->nelts > tp->static_trace_marker_id_idx)
13698 {
13699 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13700 sals->nelts = 1;
13701 }
13702 else
13703 error (_("marker %s not found"), tp->static_trace_marker_id);
13704 }
13705
13706 static struct breakpoint_ops strace_marker_breakpoint_ops;
13707
13708 static int
13709 strace_marker_p (struct breakpoint *b)
13710 {
13711 return b->ops == &strace_marker_breakpoint_ops;
13712 }
13713
13714 /* Delete a breakpoint and clean up all traces of it in the data
13715 structures. */
13716
13717 void
13718 delete_breakpoint (struct breakpoint *bpt)
13719 {
13720 struct breakpoint *b;
13721
13722 gdb_assert (bpt != NULL);
13723
13724 /* Has this bp already been deleted? This can happen because
13725 multiple lists can hold pointers to bp's. bpstat lists are
13726 especial culprits.
13727
13728 One example of this happening is a watchpoint's scope bp. When
13729 the scope bp triggers, we notice that the watchpoint is out of
13730 scope, and delete it. We also delete its scope bp. But the
13731 scope bp is marked "auto-deleting", and is already on a bpstat.
13732 That bpstat is then checked for auto-deleting bp's, which are
13733 deleted.
13734
13735 A real solution to this problem might involve reference counts in
13736 bp's, and/or giving them pointers back to their referencing
13737 bpstat's, and teaching delete_breakpoint to only free a bp's
13738 storage when no more references were extent. A cheaper bandaid
13739 was chosen. */
13740 if (bpt->type == bp_none)
13741 return;
13742
13743 /* At least avoid this stale reference until the reference counting
13744 of breakpoints gets resolved. */
13745 if (bpt->related_breakpoint != bpt)
13746 {
13747 struct breakpoint *related;
13748 struct watchpoint *w;
13749
13750 if (bpt->type == bp_watchpoint_scope)
13751 w = (struct watchpoint *) bpt->related_breakpoint;
13752 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13753 w = (struct watchpoint *) bpt;
13754 else
13755 w = NULL;
13756 if (w != NULL)
13757 watchpoint_del_at_next_stop (w);
13758
13759 /* Unlink bpt from the bpt->related_breakpoint ring. */
13760 for (related = bpt; related->related_breakpoint != bpt;
13761 related = related->related_breakpoint);
13762 related->related_breakpoint = bpt->related_breakpoint;
13763 bpt->related_breakpoint = bpt;
13764 }
13765
13766 /* watch_command_1 creates a watchpoint but only sets its number if
13767 update_watchpoint succeeds in creating its bp_locations. If there's
13768 a problem in that process, we'll be asked to delete the half-created
13769 watchpoint. In that case, don't announce the deletion. */
13770 if (bpt->number)
13771 observer_notify_breakpoint_deleted (bpt);
13772
13773 if (breakpoint_chain == bpt)
13774 breakpoint_chain = bpt->next;
13775
13776 ALL_BREAKPOINTS (b)
13777 if (b->next == bpt)
13778 {
13779 b->next = bpt->next;
13780 break;
13781 }
13782
13783 /* Be sure no bpstat's are pointing at the breakpoint after it's
13784 been freed. */
13785 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13786 in all threads for now. Note that we cannot just remove bpstats
13787 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13788 commands are associated with the bpstat; if we remove it here,
13789 then the later call to bpstat_do_actions (&stop_bpstat); in
13790 event-top.c won't do anything, and temporary breakpoints with
13791 commands won't work. */
13792
13793 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13794
13795 /* Now that breakpoint is removed from breakpoint list, update the
13796 global location list. This will remove locations that used to
13797 belong to this breakpoint. Do this before freeing the breakpoint
13798 itself, since remove_breakpoint looks at location's owner. It
13799 might be better design to have location completely
13800 self-contained, but it's not the case now. */
13801 update_global_location_list (UGLL_DONT_INSERT);
13802
13803 bpt->ops->dtor (bpt);
13804 /* On the chance that someone will soon try again to delete this
13805 same bp, we mark it as deleted before freeing its storage. */
13806 bpt->type = bp_none;
13807 delete bpt;
13808 }
13809
13810 static void
13811 do_delete_breakpoint_cleanup (void *b)
13812 {
13813 delete_breakpoint ((struct breakpoint *) b);
13814 }
13815
13816 struct cleanup *
13817 make_cleanup_delete_breakpoint (struct breakpoint *b)
13818 {
13819 return make_cleanup (do_delete_breakpoint_cleanup, b);
13820 }
13821
13822 /* Iterator function to call a user-provided callback function once
13823 for each of B and its related breakpoints. */
13824
13825 static void
13826 iterate_over_related_breakpoints (struct breakpoint *b,
13827 void (*function) (struct breakpoint *,
13828 void *),
13829 void *data)
13830 {
13831 struct breakpoint *related;
13832
13833 related = b;
13834 do
13835 {
13836 struct breakpoint *next;
13837
13838 /* FUNCTION may delete RELATED. */
13839 next = related->related_breakpoint;
13840
13841 if (next == related)
13842 {
13843 /* RELATED is the last ring entry. */
13844 function (related, data);
13845
13846 /* FUNCTION may have deleted it, so we'd never reach back to
13847 B. There's nothing left to do anyway, so just break
13848 out. */
13849 break;
13850 }
13851 else
13852 function (related, data);
13853
13854 related = next;
13855 }
13856 while (related != b);
13857 }
13858
13859 static void
13860 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13861 {
13862 delete_breakpoint (b);
13863 }
13864
13865 /* A callback for map_breakpoint_numbers that calls
13866 delete_breakpoint. */
13867
13868 static void
13869 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13870 {
13871 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13872 }
13873
13874 void
13875 delete_command (char *arg, int from_tty)
13876 {
13877 struct breakpoint *b, *b_tmp;
13878
13879 dont_repeat ();
13880
13881 if (arg == 0)
13882 {
13883 int breaks_to_delete = 0;
13884
13885 /* Delete all breakpoints if no argument. Do not delete
13886 internal breakpoints, these have to be deleted with an
13887 explicit breakpoint number argument. */
13888 ALL_BREAKPOINTS (b)
13889 if (user_breakpoint_p (b))
13890 {
13891 breaks_to_delete = 1;
13892 break;
13893 }
13894
13895 /* Ask user only if there are some breakpoints to delete. */
13896 if (!from_tty
13897 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13898 {
13899 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13900 if (user_breakpoint_p (b))
13901 delete_breakpoint (b);
13902 }
13903 }
13904 else
13905 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13906 }
13907
13908 /* Return true if all locations of B bound to PSPACE are pending. If
13909 PSPACE is NULL, all locations of all program spaces are
13910 considered. */
13911
13912 static int
13913 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13914 {
13915 struct bp_location *loc;
13916
13917 for (loc = b->loc; loc != NULL; loc = loc->next)
13918 if ((pspace == NULL
13919 || loc->pspace == pspace)
13920 && !loc->shlib_disabled
13921 && !loc->pspace->executing_startup)
13922 return 0;
13923 return 1;
13924 }
13925
13926 /* Subroutine of update_breakpoint_locations to simplify it.
13927 Return non-zero if multiple fns in list LOC have the same name.
13928 Null names are ignored. */
13929
13930 static int
13931 ambiguous_names_p (struct bp_location *loc)
13932 {
13933 struct bp_location *l;
13934 htab_t htab = htab_create_alloc (13, htab_hash_string,
13935 (int (*) (const void *,
13936 const void *)) streq,
13937 NULL, xcalloc, xfree);
13938
13939 for (l = loc; l != NULL; l = l->next)
13940 {
13941 const char **slot;
13942 const char *name = l->function_name;
13943
13944 /* Allow for some names to be NULL, ignore them. */
13945 if (name == NULL)
13946 continue;
13947
13948 slot = (const char **) htab_find_slot (htab, (const void *) name,
13949 INSERT);
13950 /* NOTE: We can assume slot != NULL here because xcalloc never
13951 returns NULL. */
13952 if (*slot != NULL)
13953 {
13954 htab_delete (htab);
13955 return 1;
13956 }
13957 *slot = name;
13958 }
13959
13960 htab_delete (htab);
13961 return 0;
13962 }
13963
13964 /* When symbols change, it probably means the sources changed as well,
13965 and it might mean the static tracepoint markers are no longer at
13966 the same address or line numbers they used to be at last we
13967 checked. Losing your static tracepoints whenever you rebuild is
13968 undesirable. This function tries to resync/rematch gdb static
13969 tracepoints with the markers on the target, for static tracepoints
13970 that have not been set by marker id. Static tracepoint that have
13971 been set by marker id are reset by marker id in breakpoint_re_set.
13972 The heuristic is:
13973
13974 1) For a tracepoint set at a specific address, look for a marker at
13975 the old PC. If one is found there, assume to be the same marker.
13976 If the name / string id of the marker found is different from the
13977 previous known name, assume that means the user renamed the marker
13978 in the sources, and output a warning.
13979
13980 2) For a tracepoint set at a given line number, look for a marker
13981 at the new address of the old line number. If one is found there,
13982 assume to be the same marker. If the name / string id of the
13983 marker found is different from the previous known name, assume that
13984 means the user renamed the marker in the sources, and output a
13985 warning.
13986
13987 3) If a marker is no longer found at the same address or line, it
13988 may mean the marker no longer exists. But it may also just mean
13989 the code changed a bit. Maybe the user added a few lines of code
13990 that made the marker move up or down (in line number terms). Ask
13991 the target for info about the marker with the string id as we knew
13992 it. If found, update line number and address in the matching
13993 static tracepoint. This will get confused if there's more than one
13994 marker with the same ID (possible in UST, although unadvised
13995 precisely because it confuses tools). */
13996
13997 static struct symtab_and_line
13998 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13999 {
14000 struct tracepoint *tp = (struct tracepoint *) b;
14001 struct static_tracepoint_marker marker;
14002 CORE_ADDR pc;
14003
14004 pc = sal.pc;
14005 if (sal.line)
14006 find_line_pc (sal.symtab, sal.line, &pc);
14007
14008 if (target_static_tracepoint_marker_at (pc, &marker))
14009 {
14010 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14011 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14012 b->number,
14013 tp->static_trace_marker_id, marker.str_id);
14014
14015 xfree (tp->static_trace_marker_id);
14016 tp->static_trace_marker_id = xstrdup (marker.str_id);
14017 release_static_tracepoint_marker (&marker);
14018
14019 return sal;
14020 }
14021
14022 /* Old marker wasn't found on target at lineno. Try looking it up
14023 by string ID. */
14024 if (!sal.explicit_pc
14025 && sal.line != 0
14026 && sal.symtab != NULL
14027 && tp->static_trace_marker_id != NULL)
14028 {
14029 VEC(static_tracepoint_marker_p) *markers;
14030
14031 markers
14032 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14033
14034 if (!VEC_empty(static_tracepoint_marker_p, markers))
14035 {
14036 struct symtab_and_line sal2;
14037 struct symbol *sym;
14038 struct static_tracepoint_marker *tpmarker;
14039 struct ui_out *uiout = current_uiout;
14040 struct explicit_location explicit_loc;
14041
14042 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14043
14044 xfree (tp->static_trace_marker_id);
14045 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14046
14047 warning (_("marker for static tracepoint %d (%s) not "
14048 "found at previous line number"),
14049 b->number, tp->static_trace_marker_id);
14050
14051 init_sal (&sal2);
14052
14053 sal2.pc = tpmarker->address;
14054
14055 sal2 = find_pc_line (tpmarker->address, 0);
14056 sym = find_pc_sect_function (tpmarker->address, NULL);
14057 uiout->text ("Now in ");
14058 if (sym)
14059 {
14060 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14061 uiout->text (" at ");
14062 }
14063 uiout->field_string ("file",
14064 symtab_to_filename_for_display (sal2.symtab));
14065 uiout->text (":");
14066
14067 if (uiout->is_mi_like_p ())
14068 {
14069 const char *fullname = symtab_to_fullname (sal2.symtab);
14070
14071 uiout->field_string ("fullname", fullname);
14072 }
14073
14074 uiout->field_int ("line", sal2.line);
14075 uiout->text ("\n");
14076
14077 b->loc->line_number = sal2.line;
14078 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14079
14080 b->location.reset (NULL);
14081 initialize_explicit_location (&explicit_loc);
14082 explicit_loc.source_filename
14083 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14084 explicit_loc.line_offset.offset = b->loc->line_number;
14085 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14086 b->location = new_explicit_location (&explicit_loc);
14087
14088 /* Might be nice to check if function changed, and warn if
14089 so. */
14090
14091 release_static_tracepoint_marker (tpmarker);
14092 }
14093 }
14094 return sal;
14095 }
14096
14097 /* Returns 1 iff locations A and B are sufficiently same that
14098 we don't need to report breakpoint as changed. */
14099
14100 static int
14101 locations_are_equal (struct bp_location *a, struct bp_location *b)
14102 {
14103 while (a && b)
14104 {
14105 if (a->address != b->address)
14106 return 0;
14107
14108 if (a->shlib_disabled != b->shlib_disabled)
14109 return 0;
14110
14111 if (a->enabled != b->enabled)
14112 return 0;
14113
14114 a = a->next;
14115 b = b->next;
14116 }
14117
14118 if ((a == NULL) != (b == NULL))
14119 return 0;
14120
14121 return 1;
14122 }
14123
14124 /* Split all locations of B that are bound to PSPACE out of B's
14125 location list to a separate list and return that list's head. If
14126 PSPACE is NULL, hoist out all locations of B. */
14127
14128 static struct bp_location *
14129 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14130 {
14131 struct bp_location head;
14132 struct bp_location *i = b->loc;
14133 struct bp_location **i_link = &b->loc;
14134 struct bp_location *hoisted = &head;
14135
14136 if (pspace == NULL)
14137 {
14138 i = b->loc;
14139 b->loc = NULL;
14140 return i;
14141 }
14142
14143 head.next = NULL;
14144
14145 while (i != NULL)
14146 {
14147 if (i->pspace == pspace)
14148 {
14149 *i_link = i->next;
14150 i->next = NULL;
14151 hoisted->next = i;
14152 hoisted = i;
14153 }
14154 else
14155 i_link = &i->next;
14156 i = *i_link;
14157 }
14158
14159 return head.next;
14160 }
14161
14162 /* Create new breakpoint locations for B (a hardware or software
14163 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14164 zero, then B is a ranged breakpoint. Only recreates locations for
14165 FILTER_PSPACE. Locations of other program spaces are left
14166 untouched. */
14167
14168 void
14169 update_breakpoint_locations (struct breakpoint *b,
14170 struct program_space *filter_pspace,
14171 struct symtabs_and_lines sals,
14172 struct symtabs_and_lines sals_end)
14173 {
14174 int i;
14175 struct bp_location *existing_locations;
14176
14177 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14178 {
14179 /* Ranged breakpoints have only one start location and one end
14180 location. */
14181 b->enable_state = bp_disabled;
14182 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14183 "multiple locations found\n"),
14184 b->number);
14185 return;
14186 }
14187
14188 /* If there's no new locations, and all existing locations are
14189 pending, don't do anything. This optimizes the common case where
14190 all locations are in the same shared library, that was unloaded.
14191 We'd like to retain the location, so that when the library is
14192 loaded again, we don't loose the enabled/disabled status of the
14193 individual locations. */
14194 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14195 return;
14196
14197 existing_locations = hoist_existing_locations (b, filter_pspace);
14198
14199 for (i = 0; i < sals.nelts; ++i)
14200 {
14201 struct bp_location *new_loc;
14202
14203 switch_to_program_space_and_thread (sals.sals[i].pspace);
14204
14205 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14206
14207 /* Reparse conditions, they might contain references to the
14208 old symtab. */
14209 if (b->cond_string != NULL)
14210 {
14211 const char *s;
14212
14213 s = b->cond_string;
14214 TRY
14215 {
14216 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14217 block_for_pc (sals.sals[i].pc),
14218 0);
14219 }
14220 CATCH (e, RETURN_MASK_ERROR)
14221 {
14222 warning (_("failed to reevaluate condition "
14223 "for breakpoint %d: %s"),
14224 b->number, e.message);
14225 new_loc->enabled = 0;
14226 }
14227 END_CATCH
14228 }
14229
14230 if (sals_end.nelts)
14231 {
14232 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14233
14234 new_loc->length = end - sals.sals[0].pc + 1;
14235 }
14236 }
14237
14238 /* If possible, carry over 'disable' status from existing
14239 breakpoints. */
14240 {
14241 struct bp_location *e = existing_locations;
14242 /* If there are multiple breakpoints with the same function name,
14243 e.g. for inline functions, comparing function names won't work.
14244 Instead compare pc addresses; this is just a heuristic as things
14245 may have moved, but in practice it gives the correct answer
14246 often enough until a better solution is found. */
14247 int have_ambiguous_names = ambiguous_names_p (b->loc);
14248
14249 for (; e; e = e->next)
14250 {
14251 if (!e->enabled && e->function_name)
14252 {
14253 struct bp_location *l = b->loc;
14254 if (have_ambiguous_names)
14255 {
14256 for (; l; l = l->next)
14257 if (breakpoint_locations_match (e, l))
14258 {
14259 l->enabled = 0;
14260 break;
14261 }
14262 }
14263 else
14264 {
14265 for (; l; l = l->next)
14266 if (l->function_name
14267 && strcmp (e->function_name, l->function_name) == 0)
14268 {
14269 l->enabled = 0;
14270 break;
14271 }
14272 }
14273 }
14274 }
14275 }
14276
14277 if (!locations_are_equal (existing_locations, b->loc))
14278 observer_notify_breakpoint_modified (b);
14279 }
14280
14281 /* Find the SaL locations corresponding to the given LOCATION.
14282 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14283
14284 static struct symtabs_and_lines
14285 location_to_sals (struct breakpoint *b, struct event_location *location,
14286 struct program_space *search_pspace, int *found)
14287 {
14288 struct symtabs_and_lines sals = {0};
14289 struct gdb_exception exception = exception_none;
14290
14291 gdb_assert (b->ops != NULL);
14292
14293 TRY
14294 {
14295 b->ops->decode_location (b, location, search_pspace, &sals);
14296 }
14297 CATCH (e, RETURN_MASK_ERROR)
14298 {
14299 int not_found_and_ok = 0;
14300
14301 exception = e;
14302
14303 /* For pending breakpoints, it's expected that parsing will
14304 fail until the right shared library is loaded. User has
14305 already told to create pending breakpoints and don't need
14306 extra messages. If breakpoint is in bp_shlib_disabled
14307 state, then user already saw the message about that
14308 breakpoint being disabled, and don't want to see more
14309 errors. */
14310 if (e.error == NOT_FOUND_ERROR
14311 && (b->condition_not_parsed
14312 || (b->loc != NULL
14313 && search_pspace != NULL
14314 && b->loc->pspace != search_pspace)
14315 || (b->loc && b->loc->shlib_disabled)
14316 || (b->loc && b->loc->pspace->executing_startup)
14317 || b->enable_state == bp_disabled))
14318 not_found_and_ok = 1;
14319
14320 if (!not_found_and_ok)
14321 {
14322 /* We surely don't want to warn about the same breakpoint
14323 10 times. One solution, implemented here, is disable
14324 the breakpoint on error. Another solution would be to
14325 have separate 'warning emitted' flag. Since this
14326 happens only when a binary has changed, I don't know
14327 which approach is better. */
14328 b->enable_state = bp_disabled;
14329 throw_exception (e);
14330 }
14331 }
14332 END_CATCH
14333
14334 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14335 {
14336 int i;
14337
14338 for (i = 0; i < sals.nelts; ++i)
14339 resolve_sal_pc (&sals.sals[i]);
14340 if (b->condition_not_parsed && b->extra_string != NULL)
14341 {
14342 char *cond_string, *extra_string;
14343 int thread, task;
14344
14345 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14346 &cond_string, &thread, &task,
14347 &extra_string);
14348 gdb_assert (b->cond_string == NULL);
14349 if (cond_string)
14350 b->cond_string = cond_string;
14351 b->thread = thread;
14352 b->task = task;
14353 if (extra_string)
14354 {
14355 xfree (b->extra_string);
14356 b->extra_string = extra_string;
14357 }
14358 b->condition_not_parsed = 0;
14359 }
14360
14361 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14362 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14363
14364 *found = 1;
14365 }
14366 else
14367 *found = 0;
14368
14369 return sals;
14370 }
14371
14372 /* The default re_set method, for typical hardware or software
14373 breakpoints. Reevaluate the breakpoint and recreate its
14374 locations. */
14375
14376 static void
14377 breakpoint_re_set_default (struct breakpoint *b)
14378 {
14379 int found;
14380 struct symtabs_and_lines sals, sals_end;
14381 struct symtabs_and_lines expanded = {0};
14382 struct symtabs_and_lines expanded_end = {0};
14383 struct program_space *filter_pspace = current_program_space;
14384
14385 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14386 if (found)
14387 {
14388 make_cleanup (xfree, sals.sals);
14389 expanded = sals;
14390 }
14391
14392 if (b->location_range_end != NULL)
14393 {
14394 sals_end = location_to_sals (b, b->location_range_end.get (),
14395 filter_pspace, &found);
14396 if (found)
14397 {
14398 make_cleanup (xfree, sals_end.sals);
14399 expanded_end = sals_end;
14400 }
14401 }
14402
14403 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14404 }
14405
14406 /* Default method for creating SALs from an address string. It basically
14407 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14408
14409 static void
14410 create_sals_from_location_default (const struct event_location *location,
14411 struct linespec_result *canonical,
14412 enum bptype type_wanted)
14413 {
14414 parse_breakpoint_sals (location, canonical);
14415 }
14416
14417 /* Call create_breakpoints_sal for the given arguments. This is the default
14418 function for the `create_breakpoints_sal' method of
14419 breakpoint_ops. */
14420
14421 static void
14422 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14423 struct linespec_result *canonical,
14424 char *cond_string,
14425 char *extra_string,
14426 enum bptype type_wanted,
14427 enum bpdisp disposition,
14428 int thread,
14429 int task, int ignore_count,
14430 const struct breakpoint_ops *ops,
14431 int from_tty, int enabled,
14432 int internal, unsigned flags)
14433 {
14434 create_breakpoints_sal (gdbarch, canonical, cond_string,
14435 extra_string,
14436 type_wanted, disposition,
14437 thread, task, ignore_count, ops, from_tty,
14438 enabled, internal, flags);
14439 }
14440
14441 /* Decode the line represented by S by calling decode_line_full. This is the
14442 default function for the `decode_location' method of breakpoint_ops. */
14443
14444 static void
14445 decode_location_default (struct breakpoint *b,
14446 const struct event_location *location,
14447 struct program_space *search_pspace,
14448 struct symtabs_and_lines *sals)
14449 {
14450 struct linespec_result canonical;
14451
14452 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14453 (struct symtab *) NULL, 0,
14454 &canonical, multiple_symbols_all,
14455 b->filter);
14456
14457 /* We should get 0 or 1 resulting SALs. */
14458 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14459
14460 if (VEC_length (linespec_sals, canonical.sals) > 0)
14461 {
14462 struct linespec_sals *lsal;
14463
14464 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14465 *sals = lsal->sals;
14466 /* Arrange it so the destructor does not free the
14467 contents. */
14468 lsal->sals.sals = NULL;
14469 }
14470 }
14471
14472 /* Prepare the global context for a re-set of breakpoint B. */
14473
14474 static struct cleanup *
14475 prepare_re_set_context (struct breakpoint *b)
14476 {
14477 input_radix = b->input_radix;
14478 set_language (b->language);
14479
14480 return make_cleanup (null_cleanup, NULL);
14481 }
14482
14483 /* Reset a breakpoint given it's struct breakpoint * BINT.
14484 The value we return ends up being the return value from catch_errors.
14485 Unused in this case. */
14486
14487 static int
14488 breakpoint_re_set_one (void *bint)
14489 {
14490 /* Get past catch_errs. */
14491 struct breakpoint *b = (struct breakpoint *) bint;
14492 struct cleanup *cleanups;
14493
14494 cleanups = prepare_re_set_context (b);
14495 b->ops->re_set (b);
14496 do_cleanups (cleanups);
14497 return 0;
14498 }
14499
14500 /* Re-set breakpoint locations for the current program space.
14501 Locations bound to other program spaces are left untouched. */
14502
14503 void
14504 breakpoint_re_set (void)
14505 {
14506 struct breakpoint *b, *b_tmp;
14507 enum language save_language;
14508 int save_input_radix;
14509 struct cleanup *old_chain;
14510
14511 save_language = current_language->la_language;
14512 save_input_radix = input_radix;
14513 old_chain = save_current_space_and_thread ();
14514
14515 /* Note: we must not try to insert locations until after all
14516 breakpoints have been re-set. Otherwise, e.g., when re-setting
14517 breakpoint 1, we'd insert the locations of breakpoint 2, which
14518 hadn't been re-set yet, and thus may have stale locations. */
14519
14520 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14521 {
14522 /* Format possible error msg. */
14523 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14524 b->number);
14525 struct cleanup *cleanups = make_cleanup (xfree, message);
14526 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14527 do_cleanups (cleanups);
14528 }
14529 set_language (save_language);
14530 input_radix = save_input_radix;
14531
14532 jit_breakpoint_re_set ();
14533
14534 do_cleanups (old_chain);
14535
14536 create_overlay_event_breakpoint ();
14537 create_longjmp_master_breakpoint ();
14538 create_std_terminate_master_breakpoint ();
14539 create_exception_master_breakpoint ();
14540
14541 /* Now we can insert. */
14542 update_global_location_list (UGLL_MAY_INSERT);
14543 }
14544 \f
14545 /* Reset the thread number of this breakpoint:
14546
14547 - If the breakpoint is for all threads, leave it as-is.
14548 - Else, reset it to the current thread for inferior_ptid. */
14549 void
14550 breakpoint_re_set_thread (struct breakpoint *b)
14551 {
14552 if (b->thread != -1)
14553 {
14554 if (in_thread_list (inferior_ptid))
14555 b->thread = ptid_to_global_thread_id (inferior_ptid);
14556
14557 /* We're being called after following a fork. The new fork is
14558 selected as current, and unless this was a vfork will have a
14559 different program space from the original thread. Reset that
14560 as well. */
14561 b->loc->pspace = current_program_space;
14562 }
14563 }
14564
14565 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14566 If from_tty is nonzero, it prints a message to that effect,
14567 which ends with a period (no newline). */
14568
14569 void
14570 set_ignore_count (int bptnum, int count, int from_tty)
14571 {
14572 struct breakpoint *b;
14573
14574 if (count < 0)
14575 count = 0;
14576
14577 ALL_BREAKPOINTS (b)
14578 if (b->number == bptnum)
14579 {
14580 if (is_tracepoint (b))
14581 {
14582 if (from_tty && count != 0)
14583 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14584 bptnum);
14585 return;
14586 }
14587
14588 b->ignore_count = count;
14589 if (from_tty)
14590 {
14591 if (count == 0)
14592 printf_filtered (_("Will stop next time "
14593 "breakpoint %d is reached."),
14594 bptnum);
14595 else if (count == 1)
14596 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14597 bptnum);
14598 else
14599 printf_filtered (_("Will ignore next %d "
14600 "crossings of breakpoint %d."),
14601 count, bptnum);
14602 }
14603 observer_notify_breakpoint_modified (b);
14604 return;
14605 }
14606
14607 error (_("No breakpoint number %d."), bptnum);
14608 }
14609
14610 /* Command to set ignore-count of breakpoint N to COUNT. */
14611
14612 static void
14613 ignore_command (char *args, int from_tty)
14614 {
14615 char *p = args;
14616 int num;
14617
14618 if (p == 0)
14619 error_no_arg (_("a breakpoint number"));
14620
14621 num = get_number (&p);
14622 if (num == 0)
14623 error (_("bad breakpoint number: '%s'"), args);
14624 if (*p == 0)
14625 error (_("Second argument (specified ignore-count) is missing."));
14626
14627 set_ignore_count (num,
14628 longest_to_int (value_as_long (parse_and_eval (p))),
14629 from_tty);
14630 if (from_tty)
14631 printf_filtered ("\n");
14632 }
14633 \f
14634 /* Call FUNCTION on each of the breakpoints
14635 whose numbers are given in ARGS. */
14636
14637 static void
14638 map_breakpoint_numbers (const char *args,
14639 void (*function) (struct breakpoint *,
14640 void *),
14641 void *data)
14642 {
14643 int num;
14644 struct breakpoint *b, *tmp;
14645
14646 if (args == 0 || *args == '\0')
14647 error_no_arg (_("one or more breakpoint numbers"));
14648
14649 number_or_range_parser parser (args);
14650
14651 while (!parser.finished ())
14652 {
14653 const char *p = parser.cur_tok ();
14654 bool match = false;
14655
14656 num = parser.get_number ();
14657 if (num == 0)
14658 {
14659 warning (_("bad breakpoint number at or near '%s'"), p);
14660 }
14661 else
14662 {
14663 ALL_BREAKPOINTS_SAFE (b, tmp)
14664 if (b->number == num)
14665 {
14666 match = true;
14667 function (b, data);
14668 break;
14669 }
14670 if (!match)
14671 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14672 }
14673 }
14674 }
14675
14676 static struct bp_location *
14677 find_location_by_number (char *number)
14678 {
14679 char *dot = strchr (number, '.');
14680 char *p1;
14681 int bp_num;
14682 int loc_num;
14683 struct breakpoint *b;
14684 struct bp_location *loc;
14685
14686 *dot = '\0';
14687
14688 p1 = number;
14689 bp_num = get_number (&p1);
14690 if (bp_num == 0)
14691 error (_("Bad breakpoint number '%s'"), number);
14692
14693 ALL_BREAKPOINTS (b)
14694 if (b->number == bp_num)
14695 {
14696 break;
14697 }
14698
14699 if (!b || b->number != bp_num)
14700 error (_("Bad breakpoint number '%s'"), number);
14701
14702 p1 = dot+1;
14703 loc_num = get_number (&p1);
14704 if (loc_num == 0)
14705 error (_("Bad breakpoint location number '%s'"), number);
14706
14707 --loc_num;
14708 loc = b->loc;
14709 for (;loc_num && loc; --loc_num, loc = loc->next)
14710 ;
14711 if (!loc)
14712 error (_("Bad breakpoint location number '%s'"), dot+1);
14713
14714 return loc;
14715 }
14716
14717
14718 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14719 If from_tty is nonzero, it prints a message to that effect,
14720 which ends with a period (no newline). */
14721
14722 void
14723 disable_breakpoint (struct breakpoint *bpt)
14724 {
14725 /* Never disable a watchpoint scope breakpoint; we want to
14726 hit them when we leave scope so we can delete both the
14727 watchpoint and its scope breakpoint at that time. */
14728 if (bpt->type == bp_watchpoint_scope)
14729 return;
14730
14731 bpt->enable_state = bp_disabled;
14732
14733 /* Mark breakpoint locations modified. */
14734 mark_breakpoint_modified (bpt);
14735
14736 if (target_supports_enable_disable_tracepoint ()
14737 && current_trace_status ()->running && is_tracepoint (bpt))
14738 {
14739 struct bp_location *location;
14740
14741 for (location = bpt->loc; location; location = location->next)
14742 target_disable_tracepoint (location);
14743 }
14744
14745 update_global_location_list (UGLL_DONT_INSERT);
14746
14747 observer_notify_breakpoint_modified (bpt);
14748 }
14749
14750 /* A callback for iterate_over_related_breakpoints. */
14751
14752 static void
14753 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14754 {
14755 disable_breakpoint (b);
14756 }
14757
14758 /* A callback for map_breakpoint_numbers that calls
14759 disable_breakpoint. */
14760
14761 static void
14762 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14763 {
14764 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14765 }
14766
14767 static void
14768 disable_command (char *args, int from_tty)
14769 {
14770 if (args == 0)
14771 {
14772 struct breakpoint *bpt;
14773
14774 ALL_BREAKPOINTS (bpt)
14775 if (user_breakpoint_p (bpt))
14776 disable_breakpoint (bpt);
14777 }
14778 else
14779 {
14780 char *num = extract_arg (&args);
14781
14782 while (num)
14783 {
14784 if (strchr (num, '.'))
14785 {
14786 struct bp_location *loc = find_location_by_number (num);
14787
14788 if (loc)
14789 {
14790 if (loc->enabled)
14791 {
14792 loc->enabled = 0;
14793 mark_breakpoint_location_modified (loc);
14794 }
14795 if (target_supports_enable_disable_tracepoint ()
14796 && current_trace_status ()->running && loc->owner
14797 && is_tracepoint (loc->owner))
14798 target_disable_tracepoint (loc);
14799 }
14800 update_global_location_list (UGLL_DONT_INSERT);
14801 }
14802 else
14803 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14804 num = extract_arg (&args);
14805 }
14806 }
14807 }
14808
14809 static void
14810 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14811 int count)
14812 {
14813 int target_resources_ok;
14814
14815 if (bpt->type == bp_hardware_breakpoint)
14816 {
14817 int i;
14818 i = hw_breakpoint_used_count ();
14819 target_resources_ok =
14820 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14821 i + 1, 0);
14822 if (target_resources_ok == 0)
14823 error (_("No hardware breakpoint support in the target."));
14824 else if (target_resources_ok < 0)
14825 error (_("Hardware breakpoints used exceeds limit."));
14826 }
14827
14828 if (is_watchpoint (bpt))
14829 {
14830 /* Initialize it just to avoid a GCC false warning. */
14831 enum enable_state orig_enable_state = bp_disabled;
14832
14833 TRY
14834 {
14835 struct watchpoint *w = (struct watchpoint *) bpt;
14836
14837 orig_enable_state = bpt->enable_state;
14838 bpt->enable_state = bp_enabled;
14839 update_watchpoint (w, 1 /* reparse */);
14840 }
14841 CATCH (e, RETURN_MASK_ALL)
14842 {
14843 bpt->enable_state = orig_enable_state;
14844 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14845 bpt->number);
14846 return;
14847 }
14848 END_CATCH
14849 }
14850
14851 bpt->enable_state = bp_enabled;
14852
14853 /* Mark breakpoint locations modified. */
14854 mark_breakpoint_modified (bpt);
14855
14856 if (target_supports_enable_disable_tracepoint ()
14857 && current_trace_status ()->running && is_tracepoint (bpt))
14858 {
14859 struct bp_location *location;
14860
14861 for (location = bpt->loc; location; location = location->next)
14862 target_enable_tracepoint (location);
14863 }
14864
14865 bpt->disposition = disposition;
14866 bpt->enable_count = count;
14867 update_global_location_list (UGLL_MAY_INSERT);
14868
14869 observer_notify_breakpoint_modified (bpt);
14870 }
14871
14872
14873 void
14874 enable_breakpoint (struct breakpoint *bpt)
14875 {
14876 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14877 }
14878
14879 static void
14880 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14881 {
14882 enable_breakpoint (bpt);
14883 }
14884
14885 /* A callback for map_breakpoint_numbers that calls
14886 enable_breakpoint. */
14887
14888 static void
14889 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14890 {
14891 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14892 }
14893
14894 /* The enable command enables the specified breakpoints (or all defined
14895 breakpoints) so they once again become (or continue to be) effective
14896 in stopping the inferior. */
14897
14898 static void
14899 enable_command (char *args, int from_tty)
14900 {
14901 if (args == 0)
14902 {
14903 struct breakpoint *bpt;
14904
14905 ALL_BREAKPOINTS (bpt)
14906 if (user_breakpoint_p (bpt))
14907 enable_breakpoint (bpt);
14908 }
14909 else
14910 {
14911 char *num = extract_arg (&args);
14912
14913 while (num)
14914 {
14915 if (strchr (num, '.'))
14916 {
14917 struct bp_location *loc = find_location_by_number (num);
14918
14919 if (loc)
14920 {
14921 if (!loc->enabled)
14922 {
14923 loc->enabled = 1;
14924 mark_breakpoint_location_modified (loc);
14925 }
14926 if (target_supports_enable_disable_tracepoint ()
14927 && current_trace_status ()->running && loc->owner
14928 && is_tracepoint (loc->owner))
14929 target_enable_tracepoint (loc);
14930 }
14931 update_global_location_list (UGLL_MAY_INSERT);
14932 }
14933 else
14934 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14935 num = extract_arg (&args);
14936 }
14937 }
14938 }
14939
14940 /* This struct packages up disposition data for application to multiple
14941 breakpoints. */
14942
14943 struct disp_data
14944 {
14945 enum bpdisp disp;
14946 int count;
14947 };
14948
14949 static void
14950 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14951 {
14952 struct disp_data disp_data = *(struct disp_data *) arg;
14953
14954 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14955 }
14956
14957 static void
14958 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14959 {
14960 struct disp_data disp = { disp_disable, 1 };
14961
14962 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14963 }
14964
14965 static void
14966 enable_once_command (char *args, int from_tty)
14967 {
14968 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14969 }
14970
14971 static void
14972 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14973 {
14974 struct disp_data disp = { disp_disable, *(int *) countptr };
14975
14976 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14977 }
14978
14979 static void
14980 enable_count_command (char *args, int from_tty)
14981 {
14982 int count;
14983
14984 if (args == NULL)
14985 error_no_arg (_("hit count"));
14986
14987 count = get_number (&args);
14988
14989 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14990 }
14991
14992 static void
14993 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14994 {
14995 struct disp_data disp = { disp_del, 1 };
14996
14997 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14998 }
14999
15000 static void
15001 enable_delete_command (char *args, int from_tty)
15002 {
15003 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15004 }
15005 \f
15006 static void
15007 set_breakpoint_cmd (char *args, int from_tty)
15008 {
15009 }
15010
15011 static void
15012 show_breakpoint_cmd (char *args, int from_tty)
15013 {
15014 }
15015
15016 /* Invalidate last known value of any hardware watchpoint if
15017 the memory which that value represents has been written to by
15018 GDB itself. */
15019
15020 static void
15021 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15022 CORE_ADDR addr, ssize_t len,
15023 const bfd_byte *data)
15024 {
15025 struct breakpoint *bp;
15026
15027 ALL_BREAKPOINTS (bp)
15028 if (bp->enable_state == bp_enabled
15029 && bp->type == bp_hardware_watchpoint)
15030 {
15031 struct watchpoint *wp = (struct watchpoint *) bp;
15032
15033 if (wp->val_valid && wp->val)
15034 {
15035 struct bp_location *loc;
15036
15037 for (loc = bp->loc; loc != NULL; loc = loc->next)
15038 if (loc->loc_type == bp_loc_hardware_watchpoint
15039 && loc->address + loc->length > addr
15040 && addr + len > loc->address)
15041 {
15042 value_free (wp->val);
15043 wp->val = NULL;
15044 wp->val_valid = 0;
15045 }
15046 }
15047 }
15048 }
15049
15050 /* Create and insert a breakpoint for software single step. */
15051
15052 void
15053 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15054 struct address_space *aspace,
15055 CORE_ADDR next_pc)
15056 {
15057 struct thread_info *tp = inferior_thread ();
15058 struct symtab_and_line sal;
15059 CORE_ADDR pc = next_pc;
15060
15061 if (tp->control.single_step_breakpoints == NULL)
15062 {
15063 tp->control.single_step_breakpoints
15064 = new_single_step_breakpoint (tp->global_num, gdbarch);
15065 }
15066
15067 sal = find_pc_line (pc, 0);
15068 sal.pc = pc;
15069 sal.section = find_pc_overlay (pc);
15070 sal.explicit_pc = 1;
15071 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15072
15073 update_global_location_list (UGLL_INSERT);
15074 }
15075
15076 /* Insert single step breakpoints according to the current state. */
15077
15078 int
15079 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15080 {
15081 struct regcache *regcache = get_current_regcache ();
15082 std::vector<CORE_ADDR> next_pcs;
15083
15084 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15085
15086 if (!next_pcs.empty ())
15087 {
15088 struct frame_info *frame = get_current_frame ();
15089 struct address_space *aspace = get_frame_address_space (frame);
15090
15091 for (CORE_ADDR pc : next_pcs)
15092 insert_single_step_breakpoint (gdbarch, aspace, pc);
15093
15094 return 1;
15095 }
15096 else
15097 return 0;
15098 }
15099
15100 /* See breakpoint.h. */
15101
15102 int
15103 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15104 struct address_space *aspace,
15105 CORE_ADDR pc)
15106 {
15107 struct bp_location *loc;
15108
15109 for (loc = bp->loc; loc != NULL; loc = loc->next)
15110 if (loc->inserted
15111 && breakpoint_location_address_match (loc, aspace, pc))
15112 return 1;
15113
15114 return 0;
15115 }
15116
15117 /* Check whether a software single-step breakpoint is inserted at
15118 PC. */
15119
15120 int
15121 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15122 CORE_ADDR pc)
15123 {
15124 struct breakpoint *bpt;
15125
15126 ALL_BREAKPOINTS (bpt)
15127 {
15128 if (bpt->type == bp_single_step
15129 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15130 return 1;
15131 }
15132 return 0;
15133 }
15134
15135 /* Tracepoint-specific operations. */
15136
15137 /* Set tracepoint count to NUM. */
15138 static void
15139 set_tracepoint_count (int num)
15140 {
15141 tracepoint_count = num;
15142 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15143 }
15144
15145 static void
15146 trace_command (char *arg, int from_tty)
15147 {
15148 struct breakpoint_ops *ops;
15149
15150 event_location_up location = string_to_event_location (&arg,
15151 current_language);
15152 if (location != NULL
15153 && event_location_type (location.get ()) == PROBE_LOCATION)
15154 ops = &tracepoint_probe_breakpoint_ops;
15155 else
15156 ops = &tracepoint_breakpoint_ops;
15157
15158 create_breakpoint (get_current_arch (),
15159 location.get (),
15160 NULL, 0, arg, 1 /* parse arg */,
15161 0 /* tempflag */,
15162 bp_tracepoint /* type_wanted */,
15163 0 /* Ignore count */,
15164 pending_break_support,
15165 ops,
15166 from_tty,
15167 1 /* enabled */,
15168 0 /* internal */, 0);
15169 }
15170
15171 static void
15172 ftrace_command (char *arg, int from_tty)
15173 {
15174 event_location_up location = string_to_event_location (&arg,
15175 current_language);
15176 create_breakpoint (get_current_arch (),
15177 location.get (),
15178 NULL, 0, arg, 1 /* parse arg */,
15179 0 /* tempflag */,
15180 bp_fast_tracepoint /* type_wanted */,
15181 0 /* Ignore count */,
15182 pending_break_support,
15183 &tracepoint_breakpoint_ops,
15184 from_tty,
15185 1 /* enabled */,
15186 0 /* internal */, 0);
15187 }
15188
15189 /* strace command implementation. Creates a static tracepoint. */
15190
15191 static void
15192 strace_command (char *arg, int from_tty)
15193 {
15194 struct breakpoint_ops *ops;
15195 event_location_up location;
15196 struct cleanup *back_to;
15197
15198 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15199 or with a normal static tracepoint. */
15200 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15201 {
15202 ops = &strace_marker_breakpoint_ops;
15203 location = new_linespec_location (&arg);
15204 }
15205 else
15206 {
15207 ops = &tracepoint_breakpoint_ops;
15208 location = string_to_event_location (&arg, current_language);
15209 }
15210
15211 create_breakpoint (get_current_arch (),
15212 location.get (),
15213 NULL, 0, arg, 1 /* parse arg */,
15214 0 /* tempflag */,
15215 bp_static_tracepoint /* type_wanted */,
15216 0 /* Ignore count */,
15217 pending_break_support,
15218 ops,
15219 from_tty,
15220 1 /* enabled */,
15221 0 /* internal */, 0);
15222 }
15223
15224 /* Set up a fake reader function that gets command lines from a linked
15225 list that was acquired during tracepoint uploading. */
15226
15227 static struct uploaded_tp *this_utp;
15228 static int next_cmd;
15229
15230 static char *
15231 read_uploaded_action (void)
15232 {
15233 char *rslt;
15234
15235 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15236
15237 next_cmd++;
15238
15239 return rslt;
15240 }
15241
15242 /* Given information about a tracepoint as recorded on a target (which
15243 can be either a live system or a trace file), attempt to create an
15244 equivalent GDB tracepoint. This is not a reliable process, since
15245 the target does not necessarily have all the information used when
15246 the tracepoint was originally defined. */
15247
15248 struct tracepoint *
15249 create_tracepoint_from_upload (struct uploaded_tp *utp)
15250 {
15251 char *addr_str, small_buf[100];
15252 struct tracepoint *tp;
15253
15254 if (utp->at_string)
15255 addr_str = utp->at_string;
15256 else
15257 {
15258 /* In the absence of a source location, fall back to raw
15259 address. Since there is no way to confirm that the address
15260 means the same thing as when the trace was started, warn the
15261 user. */
15262 warning (_("Uploaded tracepoint %d has no "
15263 "source location, using raw address"),
15264 utp->number);
15265 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15266 addr_str = small_buf;
15267 }
15268
15269 /* There's not much we can do with a sequence of bytecodes. */
15270 if (utp->cond && !utp->cond_string)
15271 warning (_("Uploaded tracepoint %d condition "
15272 "has no source form, ignoring it"),
15273 utp->number);
15274
15275 event_location_up location = string_to_event_location (&addr_str,
15276 current_language);
15277 if (!create_breakpoint (get_current_arch (),
15278 location.get (),
15279 utp->cond_string, -1, addr_str,
15280 0 /* parse cond/thread */,
15281 0 /* tempflag */,
15282 utp->type /* type_wanted */,
15283 0 /* Ignore count */,
15284 pending_break_support,
15285 &tracepoint_breakpoint_ops,
15286 0 /* from_tty */,
15287 utp->enabled /* enabled */,
15288 0 /* internal */,
15289 CREATE_BREAKPOINT_FLAGS_INSERTED))
15290 return NULL;
15291
15292 /* Get the tracepoint we just created. */
15293 tp = get_tracepoint (tracepoint_count);
15294 gdb_assert (tp != NULL);
15295
15296 if (utp->pass > 0)
15297 {
15298 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15299 tp->base.number);
15300
15301 trace_pass_command (small_buf, 0);
15302 }
15303
15304 /* If we have uploaded versions of the original commands, set up a
15305 special-purpose "reader" function and call the usual command line
15306 reader, then pass the result to the breakpoint command-setting
15307 function. */
15308 if (!VEC_empty (char_ptr, utp->cmd_strings))
15309 {
15310 command_line_up cmd_list;
15311
15312 this_utp = utp;
15313 next_cmd = 0;
15314
15315 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15316
15317 breakpoint_set_commands (&tp->base, std::move (cmd_list));
15318 }
15319 else if (!VEC_empty (char_ptr, utp->actions)
15320 || !VEC_empty (char_ptr, utp->step_actions))
15321 warning (_("Uploaded tracepoint %d actions "
15322 "have no source form, ignoring them"),
15323 utp->number);
15324
15325 /* Copy any status information that might be available. */
15326 tp->base.hit_count = utp->hit_count;
15327 tp->traceframe_usage = utp->traceframe_usage;
15328
15329 return tp;
15330 }
15331
15332 /* Print information on tracepoint number TPNUM_EXP, or all if
15333 omitted. */
15334
15335 static void
15336 tracepoints_info (char *args, int from_tty)
15337 {
15338 struct ui_out *uiout = current_uiout;
15339 int num_printed;
15340
15341 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15342
15343 if (num_printed == 0)
15344 {
15345 if (args == NULL || *args == '\0')
15346 uiout->message ("No tracepoints.\n");
15347 else
15348 uiout->message ("No tracepoint matching '%s'.\n", args);
15349 }
15350
15351 default_collect_info ();
15352 }
15353
15354 /* The 'enable trace' command enables tracepoints.
15355 Not supported by all targets. */
15356 static void
15357 enable_trace_command (char *args, int from_tty)
15358 {
15359 enable_command (args, from_tty);
15360 }
15361
15362 /* The 'disable trace' command disables tracepoints.
15363 Not supported by all targets. */
15364 static void
15365 disable_trace_command (char *args, int from_tty)
15366 {
15367 disable_command (args, from_tty);
15368 }
15369
15370 /* Remove a tracepoint (or all if no argument). */
15371 static void
15372 delete_trace_command (char *arg, int from_tty)
15373 {
15374 struct breakpoint *b, *b_tmp;
15375
15376 dont_repeat ();
15377
15378 if (arg == 0)
15379 {
15380 int breaks_to_delete = 0;
15381
15382 /* Delete all breakpoints if no argument.
15383 Do not delete internal or call-dummy breakpoints, these
15384 have to be deleted with an explicit breakpoint number
15385 argument. */
15386 ALL_TRACEPOINTS (b)
15387 if (is_tracepoint (b) && user_breakpoint_p (b))
15388 {
15389 breaks_to_delete = 1;
15390 break;
15391 }
15392
15393 /* Ask user only if there are some breakpoints to delete. */
15394 if (!from_tty
15395 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15396 {
15397 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15398 if (is_tracepoint (b) && user_breakpoint_p (b))
15399 delete_breakpoint (b);
15400 }
15401 }
15402 else
15403 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15404 }
15405
15406 /* Helper function for trace_pass_command. */
15407
15408 static void
15409 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15410 {
15411 tp->pass_count = count;
15412 observer_notify_breakpoint_modified (&tp->base);
15413 if (from_tty)
15414 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15415 tp->base.number, count);
15416 }
15417
15418 /* Set passcount for tracepoint.
15419
15420 First command argument is passcount, second is tracepoint number.
15421 If tracepoint number omitted, apply to most recently defined.
15422 Also accepts special argument "all". */
15423
15424 static void
15425 trace_pass_command (char *args, int from_tty)
15426 {
15427 struct tracepoint *t1;
15428 unsigned int count;
15429
15430 if (args == 0 || *args == 0)
15431 error (_("passcount command requires an "
15432 "argument (count + optional TP num)"));
15433
15434 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15435
15436 args = skip_spaces (args);
15437 if (*args && strncasecmp (args, "all", 3) == 0)
15438 {
15439 struct breakpoint *b;
15440
15441 args += 3; /* Skip special argument "all". */
15442 if (*args)
15443 error (_("Junk at end of arguments."));
15444
15445 ALL_TRACEPOINTS (b)
15446 {
15447 t1 = (struct tracepoint *) b;
15448 trace_pass_set_count (t1, count, from_tty);
15449 }
15450 }
15451 else if (*args == '\0')
15452 {
15453 t1 = get_tracepoint_by_number (&args, NULL);
15454 if (t1)
15455 trace_pass_set_count (t1, count, from_tty);
15456 }
15457 else
15458 {
15459 number_or_range_parser parser (args);
15460 while (!parser.finished ())
15461 {
15462 t1 = get_tracepoint_by_number (&args, &parser);
15463 if (t1)
15464 trace_pass_set_count (t1, count, from_tty);
15465 }
15466 }
15467 }
15468
15469 struct tracepoint *
15470 get_tracepoint (int num)
15471 {
15472 struct breakpoint *t;
15473
15474 ALL_TRACEPOINTS (t)
15475 if (t->number == num)
15476 return (struct tracepoint *) t;
15477
15478 return NULL;
15479 }
15480
15481 /* Find the tracepoint with the given target-side number (which may be
15482 different from the tracepoint number after disconnecting and
15483 reconnecting). */
15484
15485 struct tracepoint *
15486 get_tracepoint_by_number_on_target (int num)
15487 {
15488 struct breakpoint *b;
15489
15490 ALL_TRACEPOINTS (b)
15491 {
15492 struct tracepoint *t = (struct tracepoint *) b;
15493
15494 if (t->number_on_target == num)
15495 return t;
15496 }
15497
15498 return NULL;
15499 }
15500
15501 /* Utility: parse a tracepoint number and look it up in the list.
15502 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15503 If the argument is missing, the most recent tracepoint
15504 (tracepoint_count) is returned. */
15505
15506 struct tracepoint *
15507 get_tracepoint_by_number (char **arg,
15508 number_or_range_parser *parser)
15509 {
15510 struct breakpoint *t;
15511 int tpnum;
15512 char *instring = arg == NULL ? NULL : *arg;
15513
15514 if (parser != NULL)
15515 {
15516 gdb_assert (!parser->finished ());
15517 tpnum = parser->get_number ();
15518 }
15519 else if (arg == NULL || *arg == NULL || ! **arg)
15520 tpnum = tracepoint_count;
15521 else
15522 tpnum = get_number (arg);
15523
15524 if (tpnum <= 0)
15525 {
15526 if (instring && *instring)
15527 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15528 instring);
15529 else
15530 printf_filtered (_("No previous tracepoint\n"));
15531 return NULL;
15532 }
15533
15534 ALL_TRACEPOINTS (t)
15535 if (t->number == tpnum)
15536 {
15537 return (struct tracepoint *) t;
15538 }
15539
15540 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15541 return NULL;
15542 }
15543
15544 void
15545 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15546 {
15547 if (b->thread != -1)
15548 fprintf_unfiltered (fp, " thread %d", b->thread);
15549
15550 if (b->task != 0)
15551 fprintf_unfiltered (fp, " task %d", b->task);
15552
15553 fprintf_unfiltered (fp, "\n");
15554 }
15555
15556 /* Save information on user settable breakpoints (watchpoints, etc) to
15557 a new script file named FILENAME. If FILTER is non-NULL, call it
15558 on each breakpoint and only include the ones for which it returns
15559 non-zero. */
15560
15561 static void
15562 save_breakpoints (char *filename, int from_tty,
15563 int (*filter) (const struct breakpoint *))
15564 {
15565 struct breakpoint *tp;
15566 int any = 0;
15567 struct cleanup *cleanup;
15568 int extra_trace_bits = 0;
15569
15570 if (filename == 0 || *filename == 0)
15571 error (_("Argument required (file name in which to save)"));
15572
15573 /* See if we have anything to save. */
15574 ALL_BREAKPOINTS (tp)
15575 {
15576 /* Skip internal and momentary breakpoints. */
15577 if (!user_breakpoint_p (tp))
15578 continue;
15579
15580 /* If we have a filter, only save the breakpoints it accepts. */
15581 if (filter && !filter (tp))
15582 continue;
15583
15584 any = 1;
15585
15586 if (is_tracepoint (tp))
15587 {
15588 extra_trace_bits = 1;
15589
15590 /* We can stop searching. */
15591 break;
15592 }
15593 }
15594
15595 if (!any)
15596 {
15597 warning (_("Nothing to save."));
15598 return;
15599 }
15600
15601 filename = tilde_expand (filename);
15602 cleanup = make_cleanup (xfree, filename);
15603
15604 stdio_file fp;
15605
15606 if (!fp.open (filename, "w"))
15607 error (_("Unable to open file '%s' for saving (%s)"),
15608 filename, safe_strerror (errno));
15609
15610 if (extra_trace_bits)
15611 save_trace_state_variables (&fp);
15612
15613 ALL_BREAKPOINTS (tp)
15614 {
15615 /* Skip internal and momentary breakpoints. */
15616 if (!user_breakpoint_p (tp))
15617 continue;
15618
15619 /* If we have a filter, only save the breakpoints it accepts. */
15620 if (filter && !filter (tp))
15621 continue;
15622
15623 tp->ops->print_recreate (tp, &fp);
15624
15625 /* Note, we can't rely on tp->number for anything, as we can't
15626 assume the recreated breakpoint numbers will match. Use $bpnum
15627 instead. */
15628
15629 if (tp->cond_string)
15630 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15631
15632 if (tp->ignore_count)
15633 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15634
15635 if (tp->type != bp_dprintf && tp->commands)
15636 {
15637 fp.puts (" commands\n");
15638
15639 current_uiout->redirect (&fp);
15640 TRY
15641 {
15642 print_command_lines (current_uiout, tp->commands->commands, 2);
15643 }
15644 CATCH (ex, RETURN_MASK_ALL)
15645 {
15646 current_uiout->redirect (NULL);
15647 throw_exception (ex);
15648 }
15649 END_CATCH
15650
15651 current_uiout->redirect (NULL);
15652 fp.puts (" end\n");
15653 }
15654
15655 if (tp->enable_state == bp_disabled)
15656 fp.puts ("disable $bpnum\n");
15657
15658 /* If this is a multi-location breakpoint, check if the locations
15659 should be individually disabled. Watchpoint locations are
15660 special, and not user visible. */
15661 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15662 {
15663 struct bp_location *loc;
15664 int n = 1;
15665
15666 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15667 if (!loc->enabled)
15668 fp.printf ("disable $bpnum.%d\n", n);
15669 }
15670 }
15671
15672 if (extra_trace_bits && *default_collect)
15673 fp.printf ("set default-collect %s\n", default_collect);
15674
15675 if (from_tty)
15676 printf_filtered (_("Saved to file '%s'.\n"), filename);
15677 do_cleanups (cleanup);
15678 }
15679
15680 /* The `save breakpoints' command. */
15681
15682 static void
15683 save_breakpoints_command (char *args, int from_tty)
15684 {
15685 save_breakpoints (args, from_tty, NULL);
15686 }
15687
15688 /* The `save tracepoints' command. */
15689
15690 static void
15691 save_tracepoints_command (char *args, int from_tty)
15692 {
15693 save_breakpoints (args, from_tty, is_tracepoint);
15694 }
15695
15696 /* Create a vector of all tracepoints. */
15697
15698 VEC(breakpoint_p) *
15699 all_tracepoints (void)
15700 {
15701 VEC(breakpoint_p) *tp_vec = 0;
15702 struct breakpoint *tp;
15703
15704 ALL_TRACEPOINTS (tp)
15705 {
15706 VEC_safe_push (breakpoint_p, tp_vec, tp);
15707 }
15708
15709 return tp_vec;
15710 }
15711
15712 \f
15713 /* This help string is used to consolidate all the help string for specifying
15714 locations used by several commands. */
15715
15716 #define LOCATION_HELP_STRING \
15717 "Linespecs are colon-separated lists of location parameters, such as\n\
15718 source filename, function name, label name, and line number.\n\
15719 Example: To specify the start of a label named \"the_top\" in the\n\
15720 function \"fact\" in the file \"factorial.c\", use\n\
15721 \"factorial.c:fact:the_top\".\n\
15722 \n\
15723 Address locations begin with \"*\" and specify an exact address in the\n\
15724 program. Example: To specify the fourth byte past the start function\n\
15725 \"main\", use \"*main + 4\".\n\
15726 \n\
15727 Explicit locations are similar to linespecs but use an option/argument\n\
15728 syntax to specify location parameters.\n\
15729 Example: To specify the start of the label named \"the_top\" in the\n\
15730 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15731 -function fact -label the_top\".\n"
15732
15733 /* This help string is used for the break, hbreak, tbreak and thbreak
15734 commands. It is defined as a macro to prevent duplication.
15735 COMMAND should be a string constant containing the name of the
15736 command. */
15737
15738 #define BREAK_ARGS_HELP(command) \
15739 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15740 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15741 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15742 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15743 `-probe-dtrace' (for a DTrace probe).\n\
15744 LOCATION may be a linespec, address, or explicit location as described\n\
15745 below.\n\
15746 \n\
15747 With no LOCATION, uses current execution address of the selected\n\
15748 stack frame. This is useful for breaking on return to a stack frame.\n\
15749 \n\
15750 THREADNUM is the number from \"info threads\".\n\
15751 CONDITION is a boolean expression.\n\
15752 \n" LOCATION_HELP_STRING "\n\
15753 Multiple breakpoints at one place are permitted, and useful if their\n\
15754 conditions are different.\n\
15755 \n\
15756 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15757
15758 /* List of subcommands for "catch". */
15759 static struct cmd_list_element *catch_cmdlist;
15760
15761 /* List of subcommands for "tcatch". */
15762 static struct cmd_list_element *tcatch_cmdlist;
15763
15764 void
15765 add_catch_command (const char *name, const char *docstring,
15766 cmd_sfunc_ftype *sfunc,
15767 completer_ftype *completer,
15768 void *user_data_catch,
15769 void *user_data_tcatch)
15770 {
15771 struct cmd_list_element *command;
15772
15773 command = add_cmd (name, class_breakpoint, NULL, docstring,
15774 &catch_cmdlist);
15775 set_cmd_sfunc (command, sfunc);
15776 set_cmd_context (command, user_data_catch);
15777 set_cmd_completer (command, completer);
15778
15779 command = add_cmd (name, class_breakpoint, NULL, docstring,
15780 &tcatch_cmdlist);
15781 set_cmd_sfunc (command, sfunc);
15782 set_cmd_context (command, user_data_tcatch);
15783 set_cmd_completer (command, completer);
15784 }
15785
15786 static void
15787 save_command (char *arg, int from_tty)
15788 {
15789 printf_unfiltered (_("\"save\" must be followed by "
15790 "the name of a save subcommand.\n"));
15791 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15792 }
15793
15794 struct breakpoint *
15795 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15796 void *data)
15797 {
15798 struct breakpoint *b, *b_tmp;
15799
15800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15801 {
15802 if ((*callback) (b, data))
15803 return b;
15804 }
15805
15806 return NULL;
15807 }
15808
15809 /* Zero if any of the breakpoint's locations could be a location where
15810 functions have been inlined, nonzero otherwise. */
15811
15812 static int
15813 is_non_inline_function (struct breakpoint *b)
15814 {
15815 /* The shared library event breakpoint is set on the address of a
15816 non-inline function. */
15817 if (b->type == bp_shlib_event)
15818 return 1;
15819
15820 return 0;
15821 }
15822
15823 /* Nonzero if the specified PC cannot be a location where functions
15824 have been inlined. */
15825
15826 int
15827 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15828 const struct target_waitstatus *ws)
15829 {
15830 struct breakpoint *b;
15831 struct bp_location *bl;
15832
15833 ALL_BREAKPOINTS (b)
15834 {
15835 if (!is_non_inline_function (b))
15836 continue;
15837
15838 for (bl = b->loc; bl != NULL; bl = bl->next)
15839 {
15840 if (!bl->shlib_disabled
15841 && bpstat_check_location (bl, aspace, pc, ws))
15842 return 1;
15843 }
15844 }
15845
15846 return 0;
15847 }
15848
15849 /* Remove any references to OBJFILE which is going to be freed. */
15850
15851 void
15852 breakpoint_free_objfile (struct objfile *objfile)
15853 {
15854 struct bp_location **locp, *loc;
15855
15856 ALL_BP_LOCATIONS (loc, locp)
15857 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15858 loc->symtab = NULL;
15859 }
15860
15861 void
15862 initialize_breakpoint_ops (void)
15863 {
15864 static int initialized = 0;
15865
15866 struct breakpoint_ops *ops;
15867
15868 if (initialized)
15869 return;
15870 initialized = 1;
15871
15872 /* The breakpoint_ops structure to be inherit by all kinds of
15873 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15874 internal and momentary breakpoints, etc.). */
15875 ops = &bkpt_base_breakpoint_ops;
15876 *ops = base_breakpoint_ops;
15877 ops->re_set = bkpt_re_set;
15878 ops->insert_location = bkpt_insert_location;
15879 ops->remove_location = bkpt_remove_location;
15880 ops->breakpoint_hit = bkpt_breakpoint_hit;
15881 ops->create_sals_from_location = bkpt_create_sals_from_location;
15882 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15883 ops->decode_location = bkpt_decode_location;
15884
15885 /* The breakpoint_ops structure to be used in regular breakpoints. */
15886 ops = &bkpt_breakpoint_ops;
15887 *ops = bkpt_base_breakpoint_ops;
15888 ops->re_set = bkpt_re_set;
15889 ops->resources_needed = bkpt_resources_needed;
15890 ops->print_it = bkpt_print_it;
15891 ops->print_mention = bkpt_print_mention;
15892 ops->print_recreate = bkpt_print_recreate;
15893
15894 /* Ranged breakpoints. */
15895 ops = &ranged_breakpoint_ops;
15896 *ops = bkpt_breakpoint_ops;
15897 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15898 ops->resources_needed = resources_needed_ranged_breakpoint;
15899 ops->print_it = print_it_ranged_breakpoint;
15900 ops->print_one = print_one_ranged_breakpoint;
15901 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15902 ops->print_mention = print_mention_ranged_breakpoint;
15903 ops->print_recreate = print_recreate_ranged_breakpoint;
15904
15905 /* Internal breakpoints. */
15906 ops = &internal_breakpoint_ops;
15907 *ops = bkpt_base_breakpoint_ops;
15908 ops->re_set = internal_bkpt_re_set;
15909 ops->check_status = internal_bkpt_check_status;
15910 ops->print_it = internal_bkpt_print_it;
15911 ops->print_mention = internal_bkpt_print_mention;
15912
15913 /* Momentary breakpoints. */
15914 ops = &momentary_breakpoint_ops;
15915 *ops = bkpt_base_breakpoint_ops;
15916 ops->re_set = momentary_bkpt_re_set;
15917 ops->check_status = momentary_bkpt_check_status;
15918 ops->print_it = momentary_bkpt_print_it;
15919 ops->print_mention = momentary_bkpt_print_mention;
15920
15921 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15922 ops = &longjmp_breakpoint_ops;
15923 *ops = momentary_breakpoint_ops;
15924 ops->dtor = longjmp_bkpt_dtor;
15925
15926 /* Probe breakpoints. */
15927 ops = &bkpt_probe_breakpoint_ops;
15928 *ops = bkpt_breakpoint_ops;
15929 ops->insert_location = bkpt_probe_insert_location;
15930 ops->remove_location = bkpt_probe_remove_location;
15931 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15932 ops->decode_location = bkpt_probe_decode_location;
15933
15934 /* Watchpoints. */
15935 ops = &watchpoint_breakpoint_ops;
15936 *ops = base_breakpoint_ops;
15937 ops->dtor = dtor_watchpoint;
15938 ops->re_set = re_set_watchpoint;
15939 ops->insert_location = insert_watchpoint;
15940 ops->remove_location = remove_watchpoint;
15941 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15942 ops->check_status = check_status_watchpoint;
15943 ops->resources_needed = resources_needed_watchpoint;
15944 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15945 ops->print_it = print_it_watchpoint;
15946 ops->print_mention = print_mention_watchpoint;
15947 ops->print_recreate = print_recreate_watchpoint;
15948 ops->explains_signal = explains_signal_watchpoint;
15949
15950 /* Masked watchpoints. */
15951 ops = &masked_watchpoint_breakpoint_ops;
15952 *ops = watchpoint_breakpoint_ops;
15953 ops->insert_location = insert_masked_watchpoint;
15954 ops->remove_location = remove_masked_watchpoint;
15955 ops->resources_needed = resources_needed_masked_watchpoint;
15956 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15957 ops->print_it = print_it_masked_watchpoint;
15958 ops->print_one_detail = print_one_detail_masked_watchpoint;
15959 ops->print_mention = print_mention_masked_watchpoint;
15960 ops->print_recreate = print_recreate_masked_watchpoint;
15961
15962 /* Tracepoints. */
15963 ops = &tracepoint_breakpoint_ops;
15964 *ops = base_breakpoint_ops;
15965 ops->re_set = tracepoint_re_set;
15966 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15967 ops->print_one_detail = tracepoint_print_one_detail;
15968 ops->print_mention = tracepoint_print_mention;
15969 ops->print_recreate = tracepoint_print_recreate;
15970 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15971 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15972 ops->decode_location = tracepoint_decode_location;
15973
15974 /* Probe tracepoints. */
15975 ops = &tracepoint_probe_breakpoint_ops;
15976 *ops = tracepoint_breakpoint_ops;
15977 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15978 ops->decode_location = tracepoint_probe_decode_location;
15979
15980 /* Static tracepoints with marker (`-m'). */
15981 ops = &strace_marker_breakpoint_ops;
15982 *ops = tracepoint_breakpoint_ops;
15983 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15984 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15985 ops->decode_location = strace_marker_decode_location;
15986
15987 /* Fork catchpoints. */
15988 ops = &catch_fork_breakpoint_ops;
15989 *ops = base_breakpoint_ops;
15990 ops->insert_location = insert_catch_fork;
15991 ops->remove_location = remove_catch_fork;
15992 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15993 ops->print_it = print_it_catch_fork;
15994 ops->print_one = print_one_catch_fork;
15995 ops->print_mention = print_mention_catch_fork;
15996 ops->print_recreate = print_recreate_catch_fork;
15997
15998 /* Vfork catchpoints. */
15999 ops = &catch_vfork_breakpoint_ops;
16000 *ops = base_breakpoint_ops;
16001 ops->insert_location = insert_catch_vfork;
16002 ops->remove_location = remove_catch_vfork;
16003 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16004 ops->print_it = print_it_catch_vfork;
16005 ops->print_one = print_one_catch_vfork;
16006 ops->print_mention = print_mention_catch_vfork;
16007 ops->print_recreate = print_recreate_catch_vfork;
16008
16009 /* Exec catchpoints. */
16010 ops = &catch_exec_breakpoint_ops;
16011 *ops = base_breakpoint_ops;
16012 ops->dtor = dtor_catch_exec;
16013 ops->insert_location = insert_catch_exec;
16014 ops->remove_location = remove_catch_exec;
16015 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16016 ops->print_it = print_it_catch_exec;
16017 ops->print_one = print_one_catch_exec;
16018 ops->print_mention = print_mention_catch_exec;
16019 ops->print_recreate = print_recreate_catch_exec;
16020
16021 /* Solib-related catchpoints. */
16022 ops = &catch_solib_breakpoint_ops;
16023 *ops = base_breakpoint_ops;
16024 ops->dtor = dtor_catch_solib;
16025 ops->insert_location = insert_catch_solib;
16026 ops->remove_location = remove_catch_solib;
16027 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16028 ops->check_status = check_status_catch_solib;
16029 ops->print_it = print_it_catch_solib;
16030 ops->print_one = print_one_catch_solib;
16031 ops->print_mention = print_mention_catch_solib;
16032 ops->print_recreate = print_recreate_catch_solib;
16033
16034 ops = &dprintf_breakpoint_ops;
16035 *ops = bkpt_base_breakpoint_ops;
16036 ops->re_set = dprintf_re_set;
16037 ops->resources_needed = bkpt_resources_needed;
16038 ops->print_it = bkpt_print_it;
16039 ops->print_mention = bkpt_print_mention;
16040 ops->print_recreate = dprintf_print_recreate;
16041 ops->after_condition_true = dprintf_after_condition_true;
16042 ops->breakpoint_hit = dprintf_breakpoint_hit;
16043 }
16044
16045 /* Chain containing all defined "enable breakpoint" subcommands. */
16046
16047 static struct cmd_list_element *enablebreaklist = NULL;
16048
16049 void
16050 _initialize_breakpoint (void)
16051 {
16052 struct cmd_list_element *c;
16053
16054 initialize_breakpoint_ops ();
16055
16056 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16057 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16058 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16059
16060 breakpoint_objfile_key
16061 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16062
16063 breakpoint_chain = 0;
16064 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16065 before a breakpoint is set. */
16066 breakpoint_count = 0;
16067
16068 tracepoint_count = 0;
16069
16070 add_com ("ignore", class_breakpoint, ignore_command, _("\
16071 Set ignore-count of breakpoint number N to COUNT.\n\
16072 Usage is `ignore N COUNT'."));
16073
16074 add_com ("commands", class_breakpoint, commands_command, _("\
16075 Set commands to be executed when the given breakpoints are hit.\n\
16076 Give a space-separated breakpoint list as argument after \"commands\".\n\
16077 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16078 (e.g. `5-7').\n\
16079 With no argument, the targeted breakpoint is the last one set.\n\
16080 The commands themselves follow starting on the next line.\n\
16081 Type a line containing \"end\" to indicate the end of them.\n\
16082 Give \"silent\" as the first line to make the breakpoint silent;\n\
16083 then no output is printed when it is hit, except what the commands print."));
16084
16085 c = add_com ("condition", class_breakpoint, condition_command, _("\
16086 Specify breakpoint number N to break only if COND is true.\n\
16087 Usage is `condition N COND', where N is an integer and COND is an\n\
16088 expression to be evaluated whenever breakpoint N is reached."));
16089 set_cmd_completer (c, condition_completer);
16090
16091 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16092 Set a temporary breakpoint.\n\
16093 Like \"break\" except the breakpoint is only temporary,\n\
16094 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16095 by using \"enable delete\" on the breakpoint number.\n\
16096 \n"
16097 BREAK_ARGS_HELP ("tbreak")));
16098 set_cmd_completer (c, location_completer);
16099
16100 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16101 Set a hardware assisted breakpoint.\n\
16102 Like \"break\" except the breakpoint requires hardware support,\n\
16103 some target hardware may not have this support.\n\
16104 \n"
16105 BREAK_ARGS_HELP ("hbreak")));
16106 set_cmd_completer (c, location_completer);
16107
16108 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16109 Set a temporary hardware assisted breakpoint.\n\
16110 Like \"hbreak\" except the breakpoint is only temporary,\n\
16111 so it will be deleted when hit.\n\
16112 \n"
16113 BREAK_ARGS_HELP ("thbreak")));
16114 set_cmd_completer (c, location_completer);
16115
16116 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16117 Enable some breakpoints.\n\
16118 Give breakpoint numbers (separated by spaces) as arguments.\n\
16119 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16120 This is used to cancel the effect of the \"disable\" command.\n\
16121 With a subcommand you can enable temporarily."),
16122 &enablelist, "enable ", 1, &cmdlist);
16123
16124 add_com_alias ("en", "enable", class_breakpoint, 1);
16125
16126 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16127 Enable some breakpoints.\n\
16128 Give breakpoint numbers (separated by spaces) as arguments.\n\
16129 This is used to cancel the effect of the \"disable\" command.\n\
16130 May be abbreviated to simply \"enable\".\n"),
16131 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16132
16133 add_cmd ("once", no_class, enable_once_command, _("\
16134 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16135 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16136 &enablebreaklist);
16137
16138 add_cmd ("delete", no_class, enable_delete_command, _("\
16139 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16140 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16141 &enablebreaklist);
16142
16143 add_cmd ("count", no_class, enable_count_command, _("\
16144 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16145 If a breakpoint is hit while enabled in this fashion,\n\
16146 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16147 &enablebreaklist);
16148
16149 add_cmd ("delete", no_class, enable_delete_command, _("\
16150 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16151 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16152 &enablelist);
16153
16154 add_cmd ("once", no_class, enable_once_command, _("\
16155 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16156 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16157 &enablelist);
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 &enablelist);
16164
16165 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16166 Disable some breakpoints.\n\
16167 Arguments are breakpoint numbers with spaces in between.\n\
16168 To disable all breakpoints, give no argument.\n\
16169 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16170 &disablelist, "disable ", 1, &cmdlist);
16171 add_com_alias ("dis", "disable", class_breakpoint, 1);
16172 add_com_alias ("disa", "disable", class_breakpoint, 1);
16173
16174 add_cmd ("breakpoints", class_alias, disable_command, _("\
16175 Disable some breakpoints.\n\
16176 Arguments are breakpoint numbers with spaces in between.\n\
16177 To disable all breakpoints, give no argument.\n\
16178 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16179 This command may be abbreviated \"disable\"."),
16180 &disablelist);
16181
16182 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16183 Delete some breakpoints or auto-display expressions.\n\
16184 Arguments are breakpoint numbers with spaces in between.\n\
16185 To delete all breakpoints, give no argument.\n\
16186 \n\
16187 Also a prefix command for deletion of other GDB objects.\n\
16188 The \"unset\" command is also an alias for \"delete\"."),
16189 &deletelist, "delete ", 1, &cmdlist);
16190 add_com_alias ("d", "delete", class_breakpoint, 1);
16191 add_com_alias ("del", "delete", class_breakpoint, 1);
16192
16193 add_cmd ("breakpoints", class_alias, delete_command, _("\
16194 Delete some breakpoints or auto-display expressions.\n\
16195 Arguments are breakpoint numbers with spaces in between.\n\
16196 To delete all breakpoints, give no argument.\n\
16197 This command may be abbreviated \"delete\"."),
16198 &deletelist);
16199
16200 add_com ("clear", class_breakpoint, clear_command, _("\
16201 Clear breakpoint at specified location.\n\
16202 Argument may be a linespec, explicit, or address location as described below.\n\
16203 \n\
16204 With no argument, clears all breakpoints in the line that the selected frame\n\
16205 is executing in.\n"
16206 "\n" LOCATION_HELP_STRING "\n\
16207 See also the \"delete\" command which clears breakpoints by number."));
16208 add_com_alias ("cl", "clear", class_breakpoint, 1);
16209
16210 c = add_com ("break", class_breakpoint, break_command, _("\
16211 Set breakpoint at specified location.\n"
16212 BREAK_ARGS_HELP ("break")));
16213 set_cmd_completer (c, location_completer);
16214
16215 add_com_alias ("b", "break", class_run, 1);
16216 add_com_alias ("br", "break", class_run, 1);
16217 add_com_alias ("bre", "break", class_run, 1);
16218 add_com_alias ("brea", "break", class_run, 1);
16219
16220 if (dbx_commands)
16221 {
16222 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16223 Break in function/address or break at a line in the current file."),
16224 &stoplist, "stop ", 1, &cmdlist);
16225 add_cmd ("in", class_breakpoint, stopin_command,
16226 _("Break in function or address."), &stoplist);
16227 add_cmd ("at", class_breakpoint, stopat_command,
16228 _("Break at a line in the current file."), &stoplist);
16229 add_com ("status", class_info, breakpoints_info, _("\
16230 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16231 The \"Type\" column indicates one of:\n\
16232 \tbreakpoint - normal breakpoint\n\
16233 \twatchpoint - watchpoint\n\
16234 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16235 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16236 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16237 address and file/line number respectively.\n\
16238 \n\
16239 Convenience variable \"$_\" and default examine address for \"x\"\n\
16240 are set to the address of the last breakpoint listed unless the command\n\
16241 is prefixed with \"server \".\n\n\
16242 Convenience variable \"$bpnum\" contains the number of the last\n\
16243 breakpoint set."));
16244 }
16245
16246 add_info ("breakpoints", breakpoints_info, _("\
16247 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16248 The \"Type\" column indicates one of:\n\
16249 \tbreakpoint - normal breakpoint\n\
16250 \twatchpoint - watchpoint\n\
16251 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16252 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16253 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16254 address and file/line number respectively.\n\
16255 \n\
16256 Convenience variable \"$_\" and default examine address for \"x\"\n\
16257 are set to the address of the last breakpoint listed unless the command\n\
16258 is prefixed with \"server \".\n\n\
16259 Convenience variable \"$bpnum\" contains the number of the last\n\
16260 breakpoint set."));
16261
16262 add_info_alias ("b", "breakpoints", 1);
16263
16264 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16265 Status of all breakpoints, or breakpoint number NUMBER.\n\
16266 The \"Type\" column indicates one of:\n\
16267 \tbreakpoint - normal breakpoint\n\
16268 \twatchpoint - watchpoint\n\
16269 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16270 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16271 \tuntil - internal breakpoint used by the \"until\" command\n\
16272 \tfinish - internal breakpoint used by the \"finish\" command\n\
16273 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16274 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16275 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16276 address and file/line number respectively.\n\
16277 \n\
16278 Convenience variable \"$_\" and default examine address for \"x\"\n\
16279 are set to the address of the last breakpoint listed unless the command\n\
16280 is prefixed with \"server \".\n\n\
16281 Convenience variable \"$bpnum\" contains the number of the last\n\
16282 breakpoint set."),
16283 &maintenanceinfolist);
16284
16285 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16286 Set catchpoints to catch events."),
16287 &catch_cmdlist, "catch ",
16288 0/*allow-unknown*/, &cmdlist);
16289
16290 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16291 Set temporary catchpoints to catch events."),
16292 &tcatch_cmdlist, "tcatch ",
16293 0/*allow-unknown*/, &cmdlist);
16294
16295 add_catch_command ("fork", _("Catch calls to fork."),
16296 catch_fork_command_1,
16297 NULL,
16298 (void *) (uintptr_t) catch_fork_permanent,
16299 (void *) (uintptr_t) catch_fork_temporary);
16300 add_catch_command ("vfork", _("Catch calls to vfork."),
16301 catch_fork_command_1,
16302 NULL,
16303 (void *) (uintptr_t) catch_vfork_permanent,
16304 (void *) (uintptr_t) catch_vfork_temporary);
16305 add_catch_command ("exec", _("Catch calls to exec."),
16306 catch_exec_command_1,
16307 NULL,
16308 CATCH_PERMANENT,
16309 CATCH_TEMPORARY);
16310 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16311 Usage: catch load [REGEX]\n\
16312 If REGEX is given, only stop for libraries matching the regular expression."),
16313 catch_load_command_1,
16314 NULL,
16315 CATCH_PERMANENT,
16316 CATCH_TEMPORARY);
16317 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16318 Usage: catch unload [REGEX]\n\
16319 If REGEX is given, only stop for libraries matching the regular expression."),
16320 catch_unload_command_1,
16321 NULL,
16322 CATCH_PERMANENT,
16323 CATCH_TEMPORARY);
16324
16325 c = add_com ("watch", class_breakpoint, watch_command, _("\
16326 Set a watchpoint for an expression.\n\
16327 Usage: watch [-l|-location] EXPRESSION\n\
16328 A watchpoint stops execution of your program whenever the value of\n\
16329 an expression changes.\n\
16330 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16331 the memory to which it refers."));
16332 set_cmd_completer (c, expression_completer);
16333
16334 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16335 Set a read watchpoint for an expression.\n\
16336 Usage: rwatch [-l|-location] EXPRESSION\n\
16337 A watchpoint stops execution of your program whenever the value of\n\
16338 an expression is read.\n\
16339 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16340 the memory to which it refers."));
16341 set_cmd_completer (c, expression_completer);
16342
16343 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16344 Set a watchpoint for an expression.\n\
16345 Usage: awatch [-l|-location] EXPRESSION\n\
16346 A watchpoint stops execution of your program whenever the value of\n\
16347 an expression is either read or written.\n\
16348 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16349 the memory to which it refers."));
16350 set_cmd_completer (c, expression_completer);
16351
16352 add_info ("watchpoints", watchpoints_info, _("\
16353 Status of specified watchpoints (all watchpoints if no argument)."));
16354
16355 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16356 respond to changes - contrary to the description. */
16357 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16358 &can_use_hw_watchpoints, _("\
16359 Set debugger's willingness to use watchpoint hardware."), _("\
16360 Show debugger's willingness to use watchpoint hardware."), _("\
16361 If zero, gdb will not use hardware for new watchpoints, even if\n\
16362 such is available. (However, any hardware watchpoints that were\n\
16363 created before setting this to nonzero, will continue to use watchpoint\n\
16364 hardware.)"),
16365 NULL,
16366 show_can_use_hw_watchpoints,
16367 &setlist, &showlist);
16368
16369 can_use_hw_watchpoints = 1;
16370
16371 /* Tracepoint manipulation commands. */
16372
16373 c = add_com ("trace", class_breakpoint, trace_command, _("\
16374 Set a tracepoint at specified location.\n\
16375 \n"
16376 BREAK_ARGS_HELP ("trace") "\n\
16377 Do \"help tracepoints\" for info on other tracepoint commands."));
16378 set_cmd_completer (c, location_completer);
16379
16380 add_com_alias ("tp", "trace", class_alias, 0);
16381 add_com_alias ("tr", "trace", class_alias, 1);
16382 add_com_alias ("tra", "trace", class_alias, 1);
16383 add_com_alias ("trac", "trace", class_alias, 1);
16384
16385 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16386 Set a fast tracepoint at specified location.\n\
16387 \n"
16388 BREAK_ARGS_HELP ("ftrace") "\n\
16389 Do \"help tracepoints\" for info on other tracepoint commands."));
16390 set_cmd_completer (c, location_completer);
16391
16392 c = add_com ("strace", class_breakpoint, strace_command, _("\
16393 Set a static tracepoint at location or marker.\n\
16394 \n\
16395 strace [LOCATION] [if CONDITION]\n\
16396 LOCATION may be a linespec, explicit, or address location (described below) \n\
16397 or -m MARKER_ID.\n\n\
16398 If a marker id is specified, probe the marker with that name. With\n\
16399 no LOCATION, uses current execution address of the selected stack frame.\n\
16400 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16401 This collects arbitrary user data passed in the probe point call to the\n\
16402 tracing library. You can inspect it when analyzing the trace buffer,\n\
16403 by printing the $_sdata variable like any other convenience variable.\n\
16404 \n\
16405 CONDITION is a boolean expression.\n\
16406 \n" LOCATION_HELP_STRING "\n\
16407 Multiple tracepoints at one place are permitted, and useful if their\n\
16408 conditions are different.\n\
16409 \n\
16410 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16411 Do \"help tracepoints\" for info on other tracepoint commands."));
16412 set_cmd_completer (c, location_completer);
16413
16414 add_info ("tracepoints", tracepoints_info, _("\
16415 Status of specified tracepoints (all tracepoints if no argument).\n\
16416 Convenience variable \"$tpnum\" contains the number of the\n\
16417 last tracepoint set."));
16418
16419 add_info_alias ("tp", "tracepoints", 1);
16420
16421 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16422 Delete specified tracepoints.\n\
16423 Arguments are tracepoint numbers, separated by spaces.\n\
16424 No argument means delete all tracepoints."),
16425 &deletelist);
16426 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16427
16428 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16429 Disable specified tracepoints.\n\
16430 Arguments are tracepoint numbers, separated by spaces.\n\
16431 No argument means disable all tracepoints."),
16432 &disablelist);
16433 deprecate_cmd (c, "disable");
16434
16435 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16436 Enable specified tracepoints.\n\
16437 Arguments are tracepoint numbers, separated by spaces.\n\
16438 No argument means enable all tracepoints."),
16439 &enablelist);
16440 deprecate_cmd (c, "enable");
16441
16442 add_com ("passcount", class_trace, trace_pass_command, _("\
16443 Set the passcount for a tracepoint.\n\
16444 The trace will end when the tracepoint has been passed 'count' times.\n\
16445 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16446 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16447
16448 add_prefix_cmd ("save", class_breakpoint, save_command,
16449 _("Save breakpoint definitions as a script."),
16450 &save_cmdlist, "save ",
16451 0/*allow-unknown*/, &cmdlist);
16452
16453 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16454 Save current breakpoint definitions as a script.\n\
16455 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16456 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16457 session to restore them."),
16458 &save_cmdlist);
16459 set_cmd_completer (c, filename_completer);
16460
16461 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16462 Save current tracepoint definitions as a script.\n\
16463 Use the 'source' command in another debug session to restore them."),
16464 &save_cmdlist);
16465 set_cmd_completer (c, filename_completer);
16466
16467 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16468 deprecate_cmd (c, "save tracepoints");
16469
16470 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16471 Breakpoint specific settings\n\
16472 Configure various breakpoint-specific variables such as\n\
16473 pending breakpoint behavior"),
16474 &breakpoint_set_cmdlist, "set breakpoint ",
16475 0/*allow-unknown*/, &setlist);
16476 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16477 Breakpoint specific settings\n\
16478 Configure various breakpoint-specific variables such as\n\
16479 pending breakpoint behavior"),
16480 &breakpoint_show_cmdlist, "show breakpoint ",
16481 0/*allow-unknown*/, &showlist);
16482
16483 add_setshow_auto_boolean_cmd ("pending", no_class,
16484 &pending_break_support, _("\
16485 Set debugger's behavior regarding pending breakpoints."), _("\
16486 Show debugger's behavior regarding pending breakpoints."), _("\
16487 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16488 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16489 an error. If auto, an unrecognized breakpoint location results in a\n\
16490 user-query to see if a pending breakpoint should be created."),
16491 NULL,
16492 show_pending_break_support,
16493 &breakpoint_set_cmdlist,
16494 &breakpoint_show_cmdlist);
16495
16496 pending_break_support = AUTO_BOOLEAN_AUTO;
16497
16498 add_setshow_boolean_cmd ("auto-hw", no_class,
16499 &automatic_hardware_breakpoints, _("\
16500 Set automatic usage of hardware breakpoints."), _("\
16501 Show automatic usage of hardware breakpoints."), _("\
16502 If set, the debugger will automatically use hardware breakpoints for\n\
16503 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16504 a warning will be emitted for such breakpoints."),
16505 NULL,
16506 show_automatic_hardware_breakpoints,
16507 &breakpoint_set_cmdlist,
16508 &breakpoint_show_cmdlist);
16509
16510 add_setshow_boolean_cmd ("always-inserted", class_support,
16511 &always_inserted_mode, _("\
16512 Set mode for inserting breakpoints."), _("\
16513 Show mode for inserting breakpoints."), _("\
16514 When this mode is on, breakpoints are inserted immediately as soon as\n\
16515 they're created, kept inserted even when execution stops, and removed\n\
16516 only when the user deletes them. When this mode is off (the default),\n\
16517 breakpoints are inserted only when execution continues, and removed\n\
16518 when execution stops."),
16519 NULL,
16520 &show_always_inserted_mode,
16521 &breakpoint_set_cmdlist,
16522 &breakpoint_show_cmdlist);
16523
16524 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16525 condition_evaluation_enums,
16526 &condition_evaluation_mode_1, _("\
16527 Set mode of breakpoint condition evaluation."), _("\
16528 Show mode of breakpoint condition evaluation."), _("\
16529 When this is set to \"host\", breakpoint conditions will be\n\
16530 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16531 breakpoint conditions will be downloaded to the target (if the target\n\
16532 supports such feature) and conditions will be evaluated on the target's side.\n\
16533 If this is set to \"auto\" (default), this will be automatically set to\n\
16534 \"target\" if it supports condition evaluation, otherwise it will\n\
16535 be set to \"gdb\""),
16536 &set_condition_evaluation_mode,
16537 &show_condition_evaluation_mode,
16538 &breakpoint_set_cmdlist,
16539 &breakpoint_show_cmdlist);
16540
16541 add_com ("break-range", class_breakpoint, break_range_command, _("\
16542 Set a breakpoint for an address range.\n\
16543 break-range START-LOCATION, END-LOCATION\n\
16544 where START-LOCATION and END-LOCATION can be one of the following:\n\
16545 LINENUM, for that line in the current file,\n\
16546 FILE:LINENUM, for that line in that file,\n\
16547 +OFFSET, for that number of lines after the current line\n\
16548 or the start of the range\n\
16549 FUNCTION, for the first line in that function,\n\
16550 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16551 *ADDRESS, for the instruction at that address.\n\
16552 \n\
16553 The breakpoint will stop execution of the inferior whenever it executes\n\
16554 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16555 range (including START-LOCATION and END-LOCATION)."));
16556
16557 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16558 Set a dynamic printf at specified location.\n\
16559 dprintf location,format string,arg1,arg2,...\n\
16560 location may be a linespec, explicit, or address location.\n"
16561 "\n" LOCATION_HELP_STRING));
16562 set_cmd_completer (c, location_completer);
16563
16564 add_setshow_enum_cmd ("dprintf-style", class_support,
16565 dprintf_style_enums, &dprintf_style, _("\
16566 Set the style of usage for dynamic printf."), _("\
16567 Show the style of usage for dynamic printf."), _("\
16568 This setting chooses how GDB will do a dynamic printf.\n\
16569 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16570 console, as with the \"printf\" command.\n\
16571 If the value is \"call\", the print is done by calling a function in your\n\
16572 program; by default printf(), but you can choose a different function or\n\
16573 output stream by setting dprintf-function and dprintf-channel."),
16574 update_dprintf_commands, NULL,
16575 &setlist, &showlist);
16576
16577 dprintf_function = xstrdup ("printf");
16578 add_setshow_string_cmd ("dprintf-function", class_support,
16579 &dprintf_function, _("\
16580 Set the function to use for dynamic printf"), _("\
16581 Show the function to use for dynamic printf"), NULL,
16582 update_dprintf_commands, NULL,
16583 &setlist, &showlist);
16584
16585 dprintf_channel = xstrdup ("");
16586 add_setshow_string_cmd ("dprintf-channel", class_support,
16587 &dprintf_channel, _("\
16588 Set the channel to use for dynamic printf"), _("\
16589 Show the channel to use for dynamic printf"), NULL,
16590 update_dprintf_commands, NULL,
16591 &setlist, &showlist);
16592
16593 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16594 &disconnected_dprintf, _("\
16595 Set whether dprintf continues after GDB disconnects."), _("\
16596 Show whether dprintf continues after GDB disconnects."), _("\
16597 Use this to let dprintf commands continue to hit and produce output\n\
16598 even if GDB disconnects or detaches from the target."),
16599 NULL,
16600 NULL,
16601 &setlist, &showlist);
16602
16603 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16604 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16605 (target agent only) This is useful for formatted output in user-defined commands."));
16606
16607 automatic_hardware_breakpoints = 1;
16608
16609 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16610 observer_attach_thread_exit (remove_threaded_breakpoints);
16611 }
This page took 0.517285 seconds and 5 git commands to generate.