Kill init_sal
[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 #include "progspace-and-thread.h"
84 #include "common/array-view.h"
85
86 /* Enums for exception-handling support. */
87 enum exception_event_kind
88 {
89 EX_EVENT_THROW,
90 EX_EVENT_RETHROW,
91 EX_EVENT_CATCH
92 };
93
94 /* Prototypes for local functions. */
95
96 static void enable_delete_command (char *, int);
97
98 static void enable_once_command (char *, int);
99
100 static void enable_count_command (char *, int);
101
102 static void disable_command (char *, int);
103
104 static void enable_command (char *, int);
105
106 static void map_breakpoint_numbers (const char *,
107 void (*) (struct breakpoint *,
108 void *),
109 void *);
110
111 static void ignore_command (char *, int);
112
113 static int breakpoint_re_set_one (void *);
114
115 static void breakpoint_re_set_default (struct breakpoint *);
116
117 static void
118 create_sals_from_location_default (const struct event_location *location,
119 struct linespec_result *canonical,
120 enum bptype type_wanted);
121
122 static void create_breakpoints_sal_default (struct gdbarch *,
123 struct linespec_result *,
124 gdb::unique_xmalloc_ptr<char>,
125 gdb::unique_xmalloc_ptr<char>,
126 enum bptype,
127 enum bpdisp, int, int,
128 int,
129 const struct breakpoint_ops *,
130 int, int, int, unsigned);
131
132 static std::vector<symtab_and_line> decode_location_default
133 (struct breakpoint *b, const struct event_location *location,
134 struct program_space *search_pspace);
135
136 static void clear_command (char *, int);
137
138 static void catch_command (char *, int);
139
140 static int can_use_hardware_watchpoint (struct value *);
141
142 static void break_command_1 (char *, int, int);
143
144 static void mention (struct breakpoint *);
145
146 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
147 enum bptype,
148 const struct breakpoint_ops *);
149 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
150 const struct symtab_and_line *);
151
152 /* This function is used in gdbtk sources and thus can not be made
153 static. */
154 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
155 struct symtab_and_line,
156 enum bptype,
157 const struct breakpoint_ops *);
158
159 static struct breakpoint *
160 momentary_breakpoint_from_master (struct breakpoint *orig,
161 enum bptype type,
162 const struct breakpoint_ops *ops,
163 int loc_enabled);
164
165 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
166
167 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
168 CORE_ADDR bpaddr,
169 enum bptype bptype);
170
171 static void describe_other_breakpoints (struct gdbarch *,
172 struct program_space *, CORE_ADDR,
173 struct obj_section *, int);
174
175 static int watchpoint_locations_match (struct bp_location *loc1,
176 struct bp_location *loc2);
177
178 static int breakpoint_location_address_match (struct bp_location *bl,
179 struct address_space *aspace,
180 CORE_ADDR addr);
181
182 static int breakpoint_location_address_range_overlap (struct bp_location *,
183 struct address_space *,
184 CORE_ADDR, int);
185
186 static void info_breakpoints_command (char *, int);
187
188 static void info_watchpoints_command (char *, int);
189
190 static int breakpoint_1 (char *, int,
191 int (*) (const struct breakpoint *));
192
193 static int breakpoint_cond_eval (void *);
194
195 static void cleanup_executing_breakpoints (void *);
196
197 static void commands_command (char *, int);
198
199 static void condition_command (char *, int);
200
201 static int remove_breakpoint (struct bp_location *);
202 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
203
204 static enum print_stop_action print_bp_stop_message (bpstat bs);
205
206 static int watchpoint_check (void *);
207
208 static void maintenance_info_breakpoints (char *, int);
209
210 static int hw_breakpoint_used_count (void);
211
212 static int hw_watchpoint_use_count (struct breakpoint *);
213
214 static int hw_watchpoint_used_count_others (struct breakpoint *except,
215 enum bptype type,
216 int *other_type_used);
217
218 static void hbreak_command (char *, int);
219
220 static void thbreak_command (char *, int);
221
222 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
223 int count);
224
225 static void stop_command (char *arg, int from_tty);
226
227 static void stopin_command (char *arg, int from_tty);
228
229 static void stopat_command (char *arg, int from_tty);
230
231 static void tcatch_command (char *arg, int from_tty);
232
233 static void free_bp_location (struct bp_location *loc);
234 static void incref_bp_location (struct bp_location *loc);
235 static void decref_bp_location (struct bp_location **loc);
236
237 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
238
239 /* update_global_location_list's modes of operation wrt to whether to
240 insert locations now. */
241 enum ugll_insert_mode
242 {
243 /* Don't insert any breakpoint locations into the inferior, only
244 remove already-inserted locations that no longer should be
245 inserted. Functions that delete a breakpoint or breakpoints
246 should specify this mode, so that deleting a breakpoint doesn't
247 have the side effect of inserting the locations of other
248 breakpoints that are marked not-inserted, but should_be_inserted
249 returns true on them.
250
251 This behavior is useful is situations close to tear-down -- e.g.,
252 after an exec, while the target still has execution, but
253 breakpoint shadows of the previous executable image should *NOT*
254 be restored to the new image; or before detaching, where the
255 target still has execution and wants to delete breakpoints from
256 GDB's lists, and all breakpoints had already been removed from
257 the inferior. */
258 UGLL_DONT_INSERT,
259
260 /* May insert breakpoints iff breakpoints_should_be_inserted_now
261 claims breakpoints should be inserted now. */
262 UGLL_MAY_INSERT,
263
264 /* Insert locations now, irrespective of
265 breakpoints_should_be_inserted_now. E.g., say all threads are
266 stopped right now, and the user did "continue". We need to
267 insert breakpoints _before_ resuming the target, but
268 UGLL_MAY_INSERT wouldn't insert them, because
269 breakpoints_should_be_inserted_now returns false at that point,
270 as no thread is running yet. */
271 UGLL_INSERT
272 };
273
274 static void update_global_location_list (enum ugll_insert_mode);
275
276 static void update_global_location_list_nothrow (enum ugll_insert_mode);
277
278 static int is_hardware_watchpoint (const struct breakpoint *bpt);
279
280 static void insert_breakpoint_locations (void);
281
282 static void info_tracepoints_command (char *, int);
283
284 static void delete_trace_command (char *, int);
285
286 static void enable_trace_command (char *, int);
287
288 static void disable_trace_command (char *, int);
289
290 static void trace_pass_command (char *, int);
291
292 static void set_tracepoint_count (int num);
293
294 static int is_masked_watchpoint (const struct breakpoint *b);
295
296 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
297
298 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
299 otherwise. */
300
301 static int strace_marker_p (struct breakpoint *b);
302
303 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
304 that are implemented on top of software or hardware breakpoints
305 (user breakpoints, internal and momentary breakpoints, etc.). */
306 static struct breakpoint_ops bkpt_base_breakpoint_ops;
307
308 /* Internal breakpoints class type. */
309 static struct breakpoint_ops internal_breakpoint_ops;
310
311 /* Momentary breakpoints class type. */
312 static struct breakpoint_ops momentary_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 void
1046 condition_completer (struct cmd_list_element *cmd,
1047 completion_tracker &tracker,
1048 const char *text, const char *word)
1049 {
1050 const char *space;
1051
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (!isdigit (text[1]))
1064 complete_internalvar (tracker, &text[1]);
1065 return;
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 {
1079 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1080 tracker.add_completion (std::move (copy));
1081 }
1082 }
1083
1084 return;
1085 }
1086
1087 /* We're completing the expression part. */
1088 text = skip_spaces_const (space);
1089 expression_completer (cmd, tracker, text, word);
1090 }
1091
1092 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1093
1094 static void
1095 condition_command (char *arg, int from_tty)
1096 {
1097 struct breakpoint *b;
1098 char *p;
1099 int bnum;
1100
1101 if (arg == 0)
1102 error_no_arg (_("breakpoint number"));
1103
1104 p = arg;
1105 bnum = get_number (&p);
1106 if (bnum == 0)
1107 error (_("Bad breakpoint argument: '%s'"), arg);
1108
1109 ALL_BREAKPOINTS (b)
1110 if (b->number == bnum)
1111 {
1112 /* Check if this breakpoint has a "stop" method implemented in an
1113 extension language. This method and conditions entered into GDB
1114 from the CLI are mutually exclusive. */
1115 const struct extension_language_defn *extlang
1116 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1117
1118 if (extlang != NULL)
1119 {
1120 error (_("Only one stop condition allowed. There is currently"
1121 " a %s stop condition defined for this breakpoint."),
1122 ext_lang_capitalized_name (extlang));
1123 }
1124 set_breakpoint_condition (b, p, from_tty);
1125
1126 if (is_breakpoint (b))
1127 update_global_location_list (UGLL_MAY_INSERT);
1128
1129 return;
1130 }
1131
1132 error (_("No breakpoint number %d."), bnum);
1133 }
1134
1135 /* Check that COMMAND do not contain commands that are suitable
1136 only for tracepoints and not suitable for ordinary breakpoints.
1137 Throw if any such commands is found. */
1138
1139 static void
1140 check_no_tracepoint_commands (struct command_line *commands)
1141 {
1142 struct command_line *c;
1143
1144 for (c = commands; c; c = c->next)
1145 {
1146 int i;
1147
1148 if (c->control_type == while_stepping_control)
1149 error (_("The 'while-stepping' command can "
1150 "only be used for tracepoints"));
1151
1152 for (i = 0; i < c->body_count; ++i)
1153 check_no_tracepoint_commands ((c->body_list)[i]);
1154
1155 /* Not that command parsing removes leading whitespace and comment
1156 lines and also empty lines. So, we only need to check for
1157 command directly. */
1158 if (strstr (c->line, "collect ") == c->line)
1159 error (_("The 'collect' command can only be used for tracepoints"));
1160
1161 if (strstr (c->line, "teval ") == c->line)
1162 error (_("The 'teval' command can only be used for tracepoints"));
1163 }
1164 }
1165
1166 struct longjmp_breakpoint : public breakpoint
1167 {
1168 ~longjmp_breakpoint () override;
1169 };
1170
1171 /* Encapsulate tests for different types of tracepoints. */
1172
1173 static bool
1174 is_tracepoint_type (bptype type)
1175 {
1176 return (type == bp_tracepoint
1177 || type == bp_fast_tracepoint
1178 || type == bp_static_tracepoint);
1179 }
1180
1181 static bool
1182 is_longjmp_type (bptype type)
1183 {
1184 return type == bp_longjmp || type == bp_exception;
1185 }
1186
1187 int
1188 is_tracepoint (const struct breakpoint *b)
1189 {
1190 return is_tracepoint_type (b->type);
1191 }
1192
1193 /* Factory function to create an appropriate instance of breakpoint given
1194 TYPE. */
1195
1196 static std::unique_ptr<breakpoint>
1197 new_breakpoint_from_type (bptype type)
1198 {
1199 breakpoint *b;
1200
1201 if (is_tracepoint_type (type))
1202 b = new tracepoint ();
1203 else if (is_longjmp_type (type))
1204 b = new longjmp_breakpoint ();
1205 else
1206 b = new breakpoint ();
1207
1208 return std::unique_ptr<breakpoint> (b);
1209 }
1210
1211 /* A helper function that validates that COMMANDS are valid for a
1212 breakpoint. This function will throw an exception if a problem is
1213 found. */
1214
1215 static void
1216 validate_commands_for_breakpoint (struct breakpoint *b,
1217 struct command_line *commands)
1218 {
1219 if (is_tracepoint (b))
1220 {
1221 struct tracepoint *t = (struct tracepoint *) b;
1222 struct command_line *c;
1223 struct command_line *while_stepping = 0;
1224
1225 /* Reset the while-stepping step count. The previous commands
1226 might have included a while-stepping action, while the new
1227 ones might not. */
1228 t->step_count = 0;
1229
1230 /* We need to verify that each top-level element of commands is
1231 valid for tracepoints, that there's at most one
1232 while-stepping element, and that the while-stepping's body
1233 has valid tracing commands excluding nested while-stepping.
1234 We also need to validate the tracepoint action line in the
1235 context of the tracepoint --- validate_actionline actually
1236 has side effects, like setting the tracepoint's
1237 while-stepping STEP_COUNT, in addition to checking if the
1238 collect/teval actions parse and make sense in the
1239 tracepoint's context. */
1240 for (c = commands; c; c = c->next)
1241 {
1242 if (c->control_type == while_stepping_control)
1243 {
1244 if (b->type == bp_fast_tracepoint)
1245 error (_("The 'while-stepping' command "
1246 "cannot be used for fast tracepoint"));
1247 else if (b->type == bp_static_tracepoint)
1248 error (_("The 'while-stepping' command "
1249 "cannot be used for static tracepoint"));
1250
1251 if (while_stepping)
1252 error (_("The 'while-stepping' command "
1253 "can be used only once"));
1254 else
1255 while_stepping = c;
1256 }
1257
1258 validate_actionline (c->line, b);
1259 }
1260 if (while_stepping)
1261 {
1262 struct command_line *c2;
1263
1264 gdb_assert (while_stepping->body_count == 1);
1265 c2 = while_stepping->body_list[0];
1266 for (; c2; c2 = c2->next)
1267 {
1268 if (c2->control_type == while_stepping_control)
1269 error (_("The 'while-stepping' command cannot be nested"));
1270 }
1271 }
1272 }
1273 else
1274 {
1275 check_no_tracepoint_commands (commands);
1276 }
1277 }
1278
1279 /* Return a vector of all the static tracepoints set at ADDR. The
1280 caller is responsible for releasing the vector. */
1281
1282 VEC(breakpoint_p) *
1283 static_tracepoints_here (CORE_ADDR addr)
1284 {
1285 struct breakpoint *b;
1286 VEC(breakpoint_p) *found = 0;
1287 struct bp_location *loc;
1288
1289 ALL_BREAKPOINTS (b)
1290 if (b->type == bp_static_tracepoint)
1291 {
1292 for (loc = b->loc; loc; loc = loc->next)
1293 if (loc->address == addr)
1294 VEC_safe_push(breakpoint_p, found, b);
1295 }
1296
1297 return found;
1298 }
1299
1300 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1301 validate that only allowed commands are included. */
1302
1303 void
1304 breakpoint_set_commands (struct breakpoint *b,
1305 command_line_up &&commands)
1306 {
1307 validate_commands_for_breakpoint (b, commands.get ());
1308
1309 decref_counted_command_line (&b->commands);
1310 b->commands = alloc_counted_command_line (commands.release ());
1311 observer_notify_breakpoint_modified (b);
1312 }
1313
1314 /* Set the internal `silent' flag on the breakpoint. Note that this
1315 is not the same as the "silent" that may appear in the breakpoint's
1316 commands. */
1317
1318 void
1319 breakpoint_set_silent (struct breakpoint *b, int silent)
1320 {
1321 int old_silent = b->silent;
1322
1323 b->silent = silent;
1324 if (old_silent != silent)
1325 observer_notify_breakpoint_modified (b);
1326 }
1327
1328 /* Set the thread for this breakpoint. If THREAD is -1, make the
1329 breakpoint work for any thread. */
1330
1331 void
1332 breakpoint_set_thread (struct breakpoint *b, int thread)
1333 {
1334 int old_thread = b->thread;
1335
1336 b->thread = thread;
1337 if (old_thread != thread)
1338 observer_notify_breakpoint_modified (b);
1339 }
1340
1341 /* Set the task for this breakpoint. If TASK is 0, make the
1342 breakpoint work for any task. */
1343
1344 void
1345 breakpoint_set_task (struct breakpoint *b, int task)
1346 {
1347 int old_task = b->task;
1348
1349 b->task = task;
1350 if (old_task != task)
1351 observer_notify_breakpoint_modified (b);
1352 }
1353
1354 void
1355 check_tracepoint_command (char *line, void *closure)
1356 {
1357 struct breakpoint *b = (struct breakpoint *) closure;
1358
1359 validate_actionline (line, b);
1360 }
1361
1362 /* A structure used to pass information through
1363 map_breakpoint_numbers. */
1364
1365 struct commands_info
1366 {
1367 /* True if the command was typed at a tty. */
1368 int from_tty;
1369
1370 /* The breakpoint range spec. */
1371 const char *arg;
1372
1373 /* Non-NULL if the body of the commands are being read from this
1374 already-parsed command. */
1375 struct command_line *control;
1376
1377 /* The command lines read from the user, or NULL if they have not
1378 yet been read. */
1379 struct counted_command_line *cmd;
1380 };
1381
1382 /* A callback for map_breakpoint_numbers that sets the commands for
1383 commands_command. */
1384
1385 static void
1386 do_map_commands_command (struct breakpoint *b, void *data)
1387 {
1388 struct commands_info *info = (struct commands_info *) data;
1389
1390 if (info->cmd == NULL)
1391 {
1392 command_line_up l;
1393
1394 if (info->control != NULL)
1395 l = copy_command_lines (info->control->body_list[0]);
1396 else
1397 {
1398 struct cleanup *old_chain;
1399 char *str;
1400
1401 str = xstrprintf (_("Type commands for breakpoint(s) "
1402 "%s, one per line."),
1403 info->arg);
1404
1405 old_chain = make_cleanup (xfree, str);
1406
1407 l = read_command_lines (str,
1408 info->from_tty, 1,
1409 (is_tracepoint (b)
1410 ? check_tracepoint_command : 0),
1411 b);
1412
1413 do_cleanups (old_chain);
1414 }
1415
1416 info->cmd = alloc_counted_command_line (l.release ());
1417 }
1418
1419 /* If a breakpoint was on the list more than once, we don't need to
1420 do anything. */
1421 if (b->commands != info->cmd)
1422 {
1423 validate_commands_for_breakpoint (b, info->cmd->commands);
1424 incref_counted_command_line (info->cmd);
1425 decref_counted_command_line (&b->commands);
1426 b->commands = info->cmd;
1427 observer_notify_breakpoint_modified (b);
1428 }
1429 }
1430
1431 static void
1432 commands_command_1 (const char *arg, int from_tty,
1433 struct command_line *control)
1434 {
1435 struct cleanup *cleanups;
1436 struct commands_info info;
1437
1438 info.from_tty = from_tty;
1439 info.control = control;
1440 info.cmd = NULL;
1441 /* If we read command lines from the user, then `info' will hold an
1442 extra reference to the commands that we must clean up. */
1443 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1444
1445 std::string new_arg;
1446
1447 if (arg == NULL || !*arg)
1448 {
1449 if (breakpoint_count - prev_breakpoint_count > 1)
1450 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1451 breakpoint_count);
1452 else if (breakpoint_count > 0)
1453 new_arg = string_printf ("%d", breakpoint_count);
1454 }
1455 else
1456 new_arg = arg;
1457
1458 info.arg = new_arg.c_str ();
1459
1460 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1461
1462 if (info.cmd == NULL)
1463 error (_("No breakpoints specified."));
1464
1465 do_cleanups (cleanups);
1466 }
1467
1468 static void
1469 commands_command (char *arg, int from_tty)
1470 {
1471 commands_command_1 (arg, from_tty, NULL);
1472 }
1473
1474 /* Like commands_command, but instead of reading the commands from
1475 input stream, takes them from an already parsed command structure.
1476
1477 This is used by cli-script.c to DTRT with breakpoint commands
1478 that are part of if and while bodies. */
1479 enum command_control_type
1480 commands_from_control_command (const char *arg, struct command_line *cmd)
1481 {
1482 commands_command_1 (arg, 0, cmd);
1483 return simple_control;
1484 }
1485
1486 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1487
1488 static int
1489 bp_location_has_shadow (struct bp_location *bl)
1490 {
1491 if (bl->loc_type != bp_loc_software_breakpoint)
1492 return 0;
1493 if (!bl->inserted)
1494 return 0;
1495 if (bl->target_info.shadow_len == 0)
1496 /* BL isn't valid, or doesn't shadow memory. */
1497 return 0;
1498 return 1;
1499 }
1500
1501 /* Update BUF, which is LEN bytes read from the target address
1502 MEMADDR, by replacing a memory breakpoint with its shadowed
1503 contents.
1504
1505 If READBUF is not NULL, this buffer must not overlap with the of
1506 the breakpoint location's shadow_contents buffer. Otherwise, a
1507 failed assertion internal error will be raised. */
1508
1509 static void
1510 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1511 const gdb_byte *writebuf_org,
1512 ULONGEST memaddr, LONGEST len,
1513 struct bp_target_info *target_info,
1514 struct gdbarch *gdbarch)
1515 {
1516 /* Now do full processing of the found relevant range of elements. */
1517 CORE_ADDR bp_addr = 0;
1518 int bp_size = 0;
1519 int bptoffset = 0;
1520
1521 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1522 current_program_space->aspace, 0))
1523 {
1524 /* The breakpoint is inserted in a different address space. */
1525 return;
1526 }
1527
1528 /* Addresses and length of the part of the breakpoint that
1529 we need to copy. */
1530 bp_addr = target_info->placed_address;
1531 bp_size = target_info->shadow_len;
1532
1533 if (bp_addr + bp_size <= memaddr)
1534 {
1535 /* The breakpoint is entirely before the chunk of memory we are
1536 reading. */
1537 return;
1538 }
1539
1540 if (bp_addr >= memaddr + len)
1541 {
1542 /* The breakpoint is entirely after the chunk of memory we are
1543 reading. */
1544 return;
1545 }
1546
1547 /* Offset within shadow_contents. */
1548 if (bp_addr < memaddr)
1549 {
1550 /* Only copy the second part of the breakpoint. */
1551 bp_size -= memaddr - bp_addr;
1552 bptoffset = memaddr - bp_addr;
1553 bp_addr = memaddr;
1554 }
1555
1556 if (bp_addr + bp_size > memaddr + len)
1557 {
1558 /* Only copy the first part of the breakpoint. */
1559 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1560 }
1561
1562 if (readbuf != NULL)
1563 {
1564 /* Verify that the readbuf buffer does not overlap with the
1565 shadow_contents buffer. */
1566 gdb_assert (target_info->shadow_contents >= readbuf + len
1567 || readbuf >= (target_info->shadow_contents
1568 + target_info->shadow_len));
1569
1570 /* Update the read buffer with this inserted breakpoint's
1571 shadow. */
1572 memcpy (readbuf + bp_addr - memaddr,
1573 target_info->shadow_contents + bptoffset, bp_size);
1574 }
1575 else
1576 {
1577 const unsigned char *bp;
1578 CORE_ADDR addr = target_info->reqstd_address;
1579 int placed_size;
1580
1581 /* Update the shadow with what we want to write to memory. */
1582 memcpy (target_info->shadow_contents + bptoffset,
1583 writebuf_org + bp_addr - memaddr, bp_size);
1584
1585 /* Determine appropriate breakpoint contents and size for this
1586 address. */
1587 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1588
1589 /* Update the final write buffer with this inserted
1590 breakpoint's INSN. */
1591 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1592 }
1593 }
1594
1595 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1596 by replacing any memory breakpoints with their shadowed contents.
1597
1598 If READBUF is not NULL, this buffer must not overlap with any of
1599 the breakpoint location's shadow_contents buffers. Otherwise,
1600 a failed assertion internal error will be raised.
1601
1602 The range of shadowed area by each bp_location is:
1603 bl->address - bp_locations_placed_address_before_address_max
1604 up to bl->address + bp_locations_shadow_len_after_address_max
1605 The range we were requested to resolve shadows for is:
1606 memaddr ... memaddr + len
1607 Thus the safe cutoff boundaries for performance optimization are
1608 memaddr + len <= (bl->address
1609 - bp_locations_placed_address_before_address_max)
1610 and:
1611 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1612
1613 void
1614 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1615 const gdb_byte *writebuf_org,
1616 ULONGEST memaddr, LONGEST len)
1617 {
1618 /* Left boundary, right boundary and median element of our binary
1619 search. */
1620 unsigned bc_l, bc_r, bc;
1621
1622 /* Find BC_L which is a leftmost element which may affect BUF
1623 content. It is safe to report lower value but a failure to
1624 report higher one. */
1625
1626 bc_l = 0;
1627 bc_r = bp_locations_count;
1628 while (bc_l + 1 < bc_r)
1629 {
1630 struct bp_location *bl;
1631
1632 bc = (bc_l + bc_r) / 2;
1633 bl = bp_locations[bc];
1634
1635 /* Check first BL->ADDRESS will not overflow due to the added
1636 constant. Then advance the left boundary only if we are sure
1637 the BC element can in no way affect the BUF content (MEMADDR
1638 to MEMADDR + LEN range).
1639
1640 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1641 offset so that we cannot miss a breakpoint with its shadow
1642 range tail still reaching MEMADDR. */
1643
1644 if ((bl->address + bp_locations_shadow_len_after_address_max
1645 >= bl->address)
1646 && (bl->address + bp_locations_shadow_len_after_address_max
1647 <= memaddr))
1648 bc_l = bc;
1649 else
1650 bc_r = bc;
1651 }
1652
1653 /* Due to the binary search above, we need to make sure we pick the
1654 first location that's at BC_L's address. E.g., if there are
1655 multiple locations at the same address, BC_L may end up pointing
1656 at a duplicate location, and miss the "master"/"inserted"
1657 location. Say, given locations L1, L2 and L3 at addresses A and
1658 B:
1659
1660 L1@A, L2@A, L3@B, ...
1661
1662 BC_L could end up pointing at location L2, while the "master"
1663 location could be L1. Since the `loc->inserted' flag is only set
1664 on "master" locations, we'd forget to restore the shadow of L1
1665 and L2. */
1666 while (bc_l > 0
1667 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1668 bc_l--;
1669
1670 /* Now do full processing of the found relevant range of elements. */
1671
1672 for (bc = bc_l; bc < bp_locations_count; bc++)
1673 {
1674 struct bp_location *bl = bp_locations[bc];
1675
1676 /* bp_location array has BL->OWNER always non-NULL. */
1677 if (bl->owner->type == bp_none)
1678 warning (_("reading through apparently deleted breakpoint #%d?"),
1679 bl->owner->number);
1680
1681 /* Performance optimization: any further element can no longer affect BUF
1682 content. */
1683
1684 if (bl->address >= bp_locations_placed_address_before_address_max
1685 && memaddr + len <= (bl->address
1686 - bp_locations_placed_address_before_address_max))
1687 break;
1688
1689 if (!bp_location_has_shadow (bl))
1690 continue;
1691
1692 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1693 memaddr, len, &bl->target_info, bl->gdbarch);
1694 }
1695 }
1696
1697 \f
1698
1699 /* Return true if BPT is either a software breakpoint or a hardware
1700 breakpoint. */
1701
1702 int
1703 is_breakpoint (const struct breakpoint *bpt)
1704 {
1705 return (bpt->type == bp_breakpoint
1706 || bpt->type == bp_hardware_breakpoint
1707 || bpt->type == bp_dprintf);
1708 }
1709
1710 /* Return true if BPT is of any hardware watchpoint kind. */
1711
1712 static int
1713 is_hardware_watchpoint (const struct breakpoint *bpt)
1714 {
1715 return (bpt->type == bp_hardware_watchpoint
1716 || bpt->type == bp_read_watchpoint
1717 || bpt->type == bp_access_watchpoint);
1718 }
1719
1720 /* Return true if BPT is of any watchpoint kind, hardware or
1721 software. */
1722
1723 int
1724 is_watchpoint (const struct breakpoint *bpt)
1725 {
1726 return (is_hardware_watchpoint (bpt)
1727 || bpt->type == bp_watchpoint);
1728 }
1729
1730 /* Returns true if the current thread and its running state are safe
1731 to evaluate or update watchpoint B. Watchpoints on local
1732 expressions need to be evaluated in the context of the thread that
1733 was current when the watchpoint was created, and, that thread needs
1734 to be stopped to be able to select the correct frame context.
1735 Watchpoints on global expressions can be evaluated on any thread,
1736 and in any state. It is presently left to the target allowing
1737 memory accesses when threads are running. */
1738
1739 static int
1740 watchpoint_in_thread_scope (struct watchpoint *b)
1741 {
1742 return (b->pspace == current_program_space
1743 && (ptid_equal (b->watchpoint_thread, null_ptid)
1744 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1745 && !is_executing (inferior_ptid))));
1746 }
1747
1748 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1749 associated bp_watchpoint_scope breakpoint. */
1750
1751 static void
1752 watchpoint_del_at_next_stop (struct watchpoint *w)
1753 {
1754 if (w->related_breakpoint != w)
1755 {
1756 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1757 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1758 w->related_breakpoint->disposition = disp_del_at_next_stop;
1759 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1760 w->related_breakpoint = w;
1761 }
1762 w->disposition = disp_del_at_next_stop;
1763 }
1764
1765 /* Extract a bitfield value from value VAL using the bit parameters contained in
1766 watchpoint W. */
1767
1768 static struct value *
1769 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1770 {
1771 struct value *bit_val;
1772
1773 if (val == NULL)
1774 return NULL;
1775
1776 bit_val = allocate_value (value_type (val));
1777
1778 unpack_value_bitfield (bit_val,
1779 w->val_bitpos,
1780 w->val_bitsize,
1781 value_contents_for_printing (val),
1782 value_offset (val),
1783 val);
1784
1785 return bit_val;
1786 }
1787
1788 /* Allocate a dummy location and add it to B, which must be a software
1789 watchpoint. This is required because even if a software watchpoint
1790 is not watching any memory, bpstat_stop_status requires a location
1791 to be able to report stops. */
1792
1793 static void
1794 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1795 struct program_space *pspace)
1796 {
1797 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1798
1799 b->loc = allocate_bp_location (b);
1800 b->loc->pspace = pspace;
1801 b->loc->address = -1;
1802 b->loc->length = -1;
1803 }
1804
1805 /* Returns true if B is a software watchpoint that is not watching any
1806 memory (e.g., "watch $pc"). */
1807
1808 static int
1809 is_no_memory_software_watchpoint (struct breakpoint *b)
1810 {
1811 return (b->type == bp_watchpoint
1812 && b->loc != NULL
1813 && b->loc->next == NULL
1814 && b->loc->address == -1
1815 && b->loc->length == -1);
1816 }
1817
1818 /* Assuming that B is a watchpoint:
1819 - Reparse watchpoint expression, if REPARSE is non-zero
1820 - Evaluate expression and store the result in B->val
1821 - Evaluate the condition if there is one, and store the result
1822 in b->loc->cond.
1823 - Update the list of values that must be watched in B->loc.
1824
1825 If the watchpoint disposition is disp_del_at_next_stop, then do
1826 nothing. If this is local watchpoint that is out of scope, delete
1827 it.
1828
1829 Even with `set breakpoint always-inserted on' the watchpoints are
1830 removed + inserted on each stop here. Normal breakpoints must
1831 never be removed because they might be missed by a running thread
1832 when debugging in non-stop mode. On the other hand, hardware
1833 watchpoints (is_hardware_watchpoint; processed here) are specific
1834 to each LWP since they are stored in each LWP's hardware debug
1835 registers. Therefore, such LWP must be stopped first in order to
1836 be able to modify its hardware watchpoints.
1837
1838 Hardware watchpoints must be reset exactly once after being
1839 presented to the user. It cannot be done sooner, because it would
1840 reset the data used to present the watchpoint hit to the user. And
1841 it must not be done later because it could display the same single
1842 watchpoint hit during multiple GDB stops. Note that the latter is
1843 relevant only to the hardware watchpoint types bp_read_watchpoint
1844 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1845 not user-visible - its hit is suppressed if the memory content has
1846 not changed.
1847
1848 The following constraints influence the location where we can reset
1849 hardware watchpoints:
1850
1851 * target_stopped_by_watchpoint and target_stopped_data_address are
1852 called several times when GDB stops.
1853
1854 [linux]
1855 * Multiple hardware watchpoints can be hit at the same time,
1856 causing GDB to stop. GDB only presents one hardware watchpoint
1857 hit at a time as the reason for stopping, and all the other hits
1858 are presented later, one after the other, each time the user
1859 requests the execution to be resumed. Execution is not resumed
1860 for the threads still having pending hit event stored in
1861 LWP_INFO->STATUS. While the watchpoint is already removed from
1862 the inferior on the first stop the thread hit event is kept being
1863 reported from its cached value by linux_nat_stopped_data_address
1864 until the real thread resume happens after the watchpoint gets
1865 presented and thus its LWP_INFO->STATUS gets reset.
1866
1867 Therefore the hardware watchpoint hit can get safely reset on the
1868 watchpoint removal from inferior. */
1869
1870 static void
1871 update_watchpoint (struct watchpoint *b, int reparse)
1872 {
1873 int within_current_scope;
1874 struct frame_id saved_frame_id;
1875 int frame_saved;
1876
1877 /* If this is a local watchpoint, we only want to check if the
1878 watchpoint frame is in scope if the current thread is the thread
1879 that was used to create the watchpoint. */
1880 if (!watchpoint_in_thread_scope (b))
1881 return;
1882
1883 if (b->disposition == disp_del_at_next_stop)
1884 return;
1885
1886 frame_saved = 0;
1887
1888 /* Determine if the watchpoint is within scope. */
1889 if (b->exp_valid_block == NULL)
1890 within_current_scope = 1;
1891 else
1892 {
1893 struct frame_info *fi = get_current_frame ();
1894 struct gdbarch *frame_arch = get_frame_arch (fi);
1895 CORE_ADDR frame_pc = get_frame_pc (fi);
1896
1897 /* If we're at a point where the stack has been destroyed
1898 (e.g. in a function epilogue), unwinding may not work
1899 properly. Do not attempt to recreate locations at this
1900 point. See similar comments in watchpoint_check. */
1901 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1902 return;
1903
1904 /* Save the current frame's ID so we can restore it after
1905 evaluating the watchpoint expression on its own frame. */
1906 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1907 took a frame parameter, so that we didn't have to change the
1908 selected frame. */
1909 frame_saved = 1;
1910 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1911
1912 fi = frame_find_by_id (b->watchpoint_frame);
1913 within_current_scope = (fi != NULL);
1914 if (within_current_scope)
1915 select_frame (fi);
1916 }
1917
1918 /* We don't free locations. They are stored in the bp_location array
1919 and update_global_location_list will eventually delete them and
1920 remove breakpoints if needed. */
1921 b->loc = NULL;
1922
1923 if (within_current_scope && reparse)
1924 {
1925 const char *s;
1926
1927 b->exp.reset ();
1928 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1929 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1930 /* If the meaning of expression itself changed, the old value is
1931 no longer relevant. We don't want to report a watchpoint hit
1932 to the user when the old value and the new value may actually
1933 be completely different objects. */
1934 value_free (b->val);
1935 b->val = NULL;
1936 b->val_valid = 0;
1937
1938 /* Note that unlike with breakpoints, the watchpoint's condition
1939 expression is stored in the breakpoint object, not in the
1940 locations (re)created below. */
1941 if (b->cond_string != NULL)
1942 {
1943 b->cond_exp.reset ();
1944
1945 s = b->cond_string;
1946 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1947 }
1948 }
1949
1950 /* If we failed to parse the expression, for example because
1951 it refers to a global variable in a not-yet-loaded shared library,
1952 don't try to insert watchpoint. We don't automatically delete
1953 such watchpoint, though, since failure to parse expression
1954 is different from out-of-scope watchpoint. */
1955 if (!target_has_execution)
1956 {
1957 /* Without execution, memory can't change. No use to try and
1958 set watchpoint locations. The watchpoint will be reset when
1959 the target gains execution, through breakpoint_re_set. */
1960 if (!can_use_hw_watchpoints)
1961 {
1962 if (b->ops->works_in_software_mode (b))
1963 b->type = bp_watchpoint;
1964 else
1965 error (_("Can't set read/access watchpoint when "
1966 "hardware watchpoints are disabled."));
1967 }
1968 }
1969 else if (within_current_scope && b->exp)
1970 {
1971 int pc = 0;
1972 struct value *val_chain, *v, *result, *next;
1973 struct program_space *frame_pspace;
1974
1975 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1976
1977 /* Avoid setting b->val if it's already set. The meaning of
1978 b->val is 'the last value' user saw, and we should update
1979 it only if we reported that last value to user. As it
1980 happens, the code that reports it updates b->val directly.
1981 We don't keep track of the memory value for masked
1982 watchpoints. */
1983 if (!b->val_valid && !is_masked_watchpoint (b))
1984 {
1985 if (b->val_bitsize != 0)
1986 {
1987 v = extract_bitfield_from_watchpoint_value (b, v);
1988 if (v != NULL)
1989 release_value (v);
1990 }
1991 b->val = v;
1992 b->val_valid = 1;
1993 }
1994
1995 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1996
1997 /* Look at each value on the value chain. */
1998 for (v = val_chain; v; v = value_next (v))
1999 {
2000 /* If it's a memory location, and GDB actually needed
2001 its contents to evaluate the expression, then we
2002 must watch it. If the first value returned is
2003 still lazy, that means an error occurred reading it;
2004 watch it anyway in case it becomes readable. */
2005 if (VALUE_LVAL (v) == lval_memory
2006 && (v == val_chain || ! value_lazy (v)))
2007 {
2008 struct type *vtype = check_typedef (value_type (v));
2009
2010 /* We only watch structs and arrays if user asked
2011 for it explicitly, never if they just happen to
2012 appear in the middle of some value chain. */
2013 if (v == result
2014 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2015 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2016 {
2017 CORE_ADDR addr;
2018 enum target_hw_bp_type type;
2019 struct bp_location *loc, **tmp;
2020 int bitpos = 0, bitsize = 0;
2021
2022 if (value_bitsize (v) != 0)
2023 {
2024 /* Extract the bit parameters out from the bitfield
2025 sub-expression. */
2026 bitpos = value_bitpos (v);
2027 bitsize = value_bitsize (v);
2028 }
2029 else if (v == result && b->val_bitsize != 0)
2030 {
2031 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2032 lvalue whose bit parameters are saved in the fields
2033 VAL_BITPOS and VAL_BITSIZE. */
2034 bitpos = b->val_bitpos;
2035 bitsize = b->val_bitsize;
2036 }
2037
2038 addr = value_address (v);
2039 if (bitsize != 0)
2040 {
2041 /* Skip the bytes that don't contain the bitfield. */
2042 addr += bitpos / 8;
2043 }
2044
2045 type = hw_write;
2046 if (b->type == bp_read_watchpoint)
2047 type = hw_read;
2048 else if (b->type == bp_access_watchpoint)
2049 type = hw_access;
2050
2051 loc = allocate_bp_location (b);
2052 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2053 ;
2054 *tmp = loc;
2055 loc->gdbarch = get_type_arch (value_type (v));
2056
2057 loc->pspace = frame_pspace;
2058 loc->address = addr;
2059
2060 if (bitsize != 0)
2061 {
2062 /* Just cover the bytes that make up the bitfield. */
2063 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2064 }
2065 else
2066 loc->length = TYPE_LENGTH (value_type (v));
2067
2068 loc->watchpoint_type = type;
2069 }
2070 }
2071 }
2072
2073 /* Change the type of breakpoint between hardware assisted or
2074 an ordinary watchpoint depending on the hardware support
2075 and free hardware slots. REPARSE is set when the inferior
2076 is started. */
2077 if (reparse)
2078 {
2079 int reg_cnt;
2080 enum bp_loc_type loc_type;
2081 struct bp_location *bl;
2082
2083 reg_cnt = can_use_hardware_watchpoint (val_chain);
2084
2085 if (reg_cnt)
2086 {
2087 int i, target_resources_ok, other_type_used;
2088 enum bptype type;
2089
2090 /* Use an exact watchpoint when there's only one memory region to be
2091 watched, and only one debug register is needed to watch it. */
2092 b->exact = target_exact_watchpoints && reg_cnt == 1;
2093
2094 /* We need to determine how many resources are already
2095 used for all other hardware watchpoints plus this one
2096 to see if we still have enough resources to also fit
2097 this watchpoint in as well. */
2098
2099 /* If this is a software watchpoint, we try to turn it
2100 to a hardware one -- count resources as if B was of
2101 hardware watchpoint type. */
2102 type = b->type;
2103 if (type == bp_watchpoint)
2104 type = bp_hardware_watchpoint;
2105
2106 /* This watchpoint may or may not have been placed on
2107 the list yet at this point (it won't be in the list
2108 if we're trying to create it for the first time,
2109 through watch_command), so always account for it
2110 manually. */
2111
2112 /* Count resources used by all watchpoints except B. */
2113 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2114
2115 /* Add in the resources needed for B. */
2116 i += hw_watchpoint_use_count (b);
2117
2118 target_resources_ok
2119 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2120 if (target_resources_ok <= 0)
2121 {
2122 int sw_mode = b->ops->works_in_software_mode (b);
2123
2124 if (target_resources_ok == 0 && !sw_mode)
2125 error (_("Target does not support this type of "
2126 "hardware watchpoint."));
2127 else if (target_resources_ok < 0 && !sw_mode)
2128 error (_("There are not enough available hardware "
2129 "resources for this watchpoint."));
2130
2131 /* Downgrade to software watchpoint. */
2132 b->type = bp_watchpoint;
2133 }
2134 else
2135 {
2136 /* If this was a software watchpoint, we've just
2137 found we have enough resources to turn it to a
2138 hardware watchpoint. Otherwise, this is a
2139 nop. */
2140 b->type = type;
2141 }
2142 }
2143 else if (!b->ops->works_in_software_mode (b))
2144 {
2145 if (!can_use_hw_watchpoints)
2146 error (_("Can't set read/access watchpoint when "
2147 "hardware watchpoints are disabled."));
2148 else
2149 error (_("Expression cannot be implemented with "
2150 "read/access watchpoint."));
2151 }
2152 else
2153 b->type = bp_watchpoint;
2154
2155 loc_type = (b->type == bp_watchpoint? bp_loc_other
2156 : bp_loc_hardware_watchpoint);
2157 for (bl = b->loc; bl; bl = bl->next)
2158 bl->loc_type = loc_type;
2159 }
2160
2161 for (v = val_chain; v; v = next)
2162 {
2163 next = value_next (v);
2164 if (v != b->val)
2165 value_free (v);
2166 }
2167
2168 /* If a software watchpoint is not watching any memory, then the
2169 above left it without any location set up. But,
2170 bpstat_stop_status requires a location to be able to report
2171 stops, so make sure there's at least a dummy one. */
2172 if (b->type == bp_watchpoint && b->loc == NULL)
2173 software_watchpoint_add_no_memory_location (b, frame_pspace);
2174 }
2175 else if (!within_current_scope)
2176 {
2177 printf_filtered (_("\
2178 Watchpoint %d deleted because the program has left the block\n\
2179 in which its expression is valid.\n"),
2180 b->number);
2181 watchpoint_del_at_next_stop (b);
2182 }
2183
2184 /* Restore the selected frame. */
2185 if (frame_saved)
2186 select_frame (frame_find_by_id (saved_frame_id));
2187 }
2188
2189
2190 /* Returns 1 iff breakpoint location should be
2191 inserted in the inferior. We don't differentiate the type of BL's owner
2192 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2193 breakpoint_ops is not defined, because in insert_bp_location,
2194 tracepoint's insert_location will not be called. */
2195 static int
2196 should_be_inserted (struct bp_location *bl)
2197 {
2198 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2199 return 0;
2200
2201 if (bl->owner->disposition == disp_del_at_next_stop)
2202 return 0;
2203
2204 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2205 return 0;
2206
2207 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2208 return 0;
2209
2210 /* This is set for example, when we're attached to the parent of a
2211 vfork, and have detached from the child. The child is running
2212 free, and we expect it to do an exec or exit, at which point the
2213 OS makes the parent schedulable again (and the target reports
2214 that the vfork is done). Until the child is done with the shared
2215 memory region, do not insert breakpoints in the parent, otherwise
2216 the child could still trip on the parent's breakpoints. Since
2217 the parent is blocked anyway, it won't miss any breakpoint. */
2218 if (bl->pspace->breakpoints_not_allowed)
2219 return 0;
2220
2221 /* Don't insert a breakpoint if we're trying to step past its
2222 location, except if the breakpoint is a single-step breakpoint,
2223 and the breakpoint's thread is the thread which is stepping past
2224 a breakpoint. */
2225 if ((bl->loc_type == bp_loc_software_breakpoint
2226 || bl->loc_type == bp_loc_hardware_breakpoint)
2227 && stepping_past_instruction_at (bl->pspace->aspace,
2228 bl->address)
2229 /* The single-step breakpoint may be inserted at the location
2230 we're trying to step if the instruction branches to itself.
2231 However, the instruction won't be executed at all and it may
2232 break the semantics of the instruction, for example, the
2233 instruction is a conditional branch or updates some flags.
2234 We can't fix it unless GDB is able to emulate the instruction
2235 or switch to displaced stepping. */
2236 && !(bl->owner->type == bp_single_step
2237 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2238 {
2239 if (debug_infrun)
2240 {
2241 fprintf_unfiltered (gdb_stdlog,
2242 "infrun: skipping breakpoint: "
2243 "stepping past insn at: %s\n",
2244 paddress (bl->gdbarch, bl->address));
2245 }
2246 return 0;
2247 }
2248
2249 /* Don't insert watchpoints if we're trying to step past the
2250 instruction that triggered one. */
2251 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2252 && stepping_past_nonsteppable_watchpoint ())
2253 {
2254 if (debug_infrun)
2255 {
2256 fprintf_unfiltered (gdb_stdlog,
2257 "infrun: stepping past non-steppable watchpoint. "
2258 "skipping watchpoint at %s:%d\n",
2259 paddress (bl->gdbarch, bl->address),
2260 bl->length);
2261 }
2262 return 0;
2263 }
2264
2265 return 1;
2266 }
2267
2268 /* Same as should_be_inserted but does the check assuming
2269 that the location is not duplicated. */
2270
2271 static int
2272 unduplicated_should_be_inserted (struct bp_location *bl)
2273 {
2274 int result;
2275 const int save_duplicate = bl->duplicate;
2276
2277 bl->duplicate = 0;
2278 result = should_be_inserted (bl);
2279 bl->duplicate = save_duplicate;
2280 return result;
2281 }
2282
2283 /* Parses a conditional described by an expression COND into an
2284 agent expression bytecode suitable for evaluation
2285 by the bytecode interpreter. Return NULL if there was
2286 any error during parsing. */
2287
2288 static agent_expr_up
2289 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2290 {
2291 if (cond == NULL)
2292 return NULL;
2293
2294 agent_expr_up aexpr;
2295
2296 /* We don't want to stop processing, so catch any errors
2297 that may show up. */
2298 TRY
2299 {
2300 aexpr = gen_eval_for_expr (scope, cond);
2301 }
2302
2303 CATCH (ex, RETURN_MASK_ERROR)
2304 {
2305 /* If we got here, it means the condition could not be parsed to a valid
2306 bytecode expression and thus can't be evaluated on the target's side.
2307 It's no use iterating through the conditions. */
2308 }
2309 END_CATCH
2310
2311 /* We have a valid agent expression. */
2312 return aexpr;
2313 }
2314
2315 /* Based on location BL, create a list of breakpoint conditions to be
2316 passed on to the target. If we have duplicated locations with different
2317 conditions, we will add such conditions to the list. The idea is that the
2318 target will evaluate the list of conditions and will only notify GDB when
2319 one of them is true. */
2320
2321 static void
2322 build_target_condition_list (struct bp_location *bl)
2323 {
2324 struct bp_location **locp = NULL, **loc2p;
2325 int null_condition_or_parse_error = 0;
2326 int modified = bl->needs_update;
2327 struct bp_location *loc;
2328
2329 /* Release conditions left over from a previous insert. */
2330 bl->target_info.conditions.clear ();
2331
2332 /* This is only meaningful if the target is
2333 evaluating conditions and if the user has
2334 opted for condition evaluation on the target's
2335 side. */
2336 if (gdb_evaluates_breakpoint_condition_p ()
2337 || !target_supports_evaluation_of_breakpoint_conditions ())
2338 return;
2339
2340 /* Do a first pass to check for locations with no assigned
2341 conditions or conditions that fail to parse to a valid agent expression
2342 bytecode. If any of these happen, then it's no use to send conditions
2343 to the target since this location will always trigger and generate a
2344 response back to GDB. */
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 if (modified)
2351 {
2352 /* Re-parse the conditions since something changed. In that
2353 case we already freed the condition bytecodes (see
2354 force_breakpoint_reinsertion). We just
2355 need to parse the condition to bytecodes again. */
2356 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2357 loc->cond.get ());
2358 }
2359
2360 /* If we have a NULL bytecode expression, it means something
2361 went wrong or we have a null condition expression. */
2362 if (!loc->cond_bytecode)
2363 {
2364 null_condition_or_parse_error = 1;
2365 break;
2366 }
2367 }
2368 }
2369
2370 /* If any of these happened, it means we will have to evaluate the conditions
2371 for the location's address on gdb's side. It is no use keeping bytecodes
2372 for all the other duplicate locations, thus we free all of them here.
2373
2374 This is so we have a finer control over which locations' conditions are
2375 being evaluated by GDB or the remote stub. */
2376 if (null_condition_or_parse_error)
2377 {
2378 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2379 {
2380 loc = (*loc2p);
2381 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2382 {
2383 /* Only go as far as the first NULL bytecode is
2384 located. */
2385 if (!loc->cond_bytecode)
2386 return;
2387
2388 loc->cond_bytecode.reset ();
2389 }
2390 }
2391 }
2392
2393 /* No NULL conditions or failed bytecode generation. Build a condition list
2394 for this location's address. */
2395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2396 {
2397 loc = (*loc2p);
2398 if (loc->cond
2399 && is_breakpoint (loc->owner)
2400 && loc->pspace->num == bl->pspace->num
2401 && loc->owner->enable_state == bp_enabled
2402 && loc->enabled)
2403 {
2404 /* Add the condition to the vector. This will be used later
2405 to send the conditions to the target. */
2406 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2407 }
2408 }
2409
2410 return;
2411 }
2412
2413 /* Parses a command described by string CMD into an agent expression
2414 bytecode suitable for evaluation by the bytecode interpreter.
2415 Return NULL if there was any error during parsing. */
2416
2417 static agent_expr_up
2418 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2419 {
2420 struct cleanup *old_cleanups = 0;
2421 struct expression **argvec;
2422 const char *cmdrest;
2423 const char *format_start, *format_end;
2424 struct format_piece *fpieces;
2425 int nargs;
2426 struct gdbarch *gdbarch = get_current_arch ();
2427
2428 if (cmd == NULL)
2429 return NULL;
2430
2431 cmdrest = cmd;
2432
2433 if (*cmdrest == ',')
2434 ++cmdrest;
2435 cmdrest = skip_spaces_const (cmdrest);
2436
2437 if (*cmdrest++ != '"')
2438 error (_("No format string following the location"));
2439
2440 format_start = cmdrest;
2441
2442 fpieces = parse_format_string (&cmdrest);
2443
2444 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2445
2446 format_end = cmdrest;
2447
2448 if (*cmdrest++ != '"')
2449 error (_("Bad format string, non-terminated '\"'."));
2450
2451 cmdrest = skip_spaces_const (cmdrest);
2452
2453 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2454 error (_("Invalid argument syntax"));
2455
2456 if (*cmdrest == ',')
2457 cmdrest++;
2458 cmdrest = skip_spaces_const (cmdrest);
2459
2460 /* For each argument, make an expression. */
2461
2462 argvec = (struct expression **) alloca (strlen (cmd)
2463 * sizeof (struct expression *));
2464
2465 nargs = 0;
2466 while (*cmdrest != '\0')
2467 {
2468 const char *cmd1;
2469
2470 cmd1 = cmdrest;
2471 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2472 argvec[nargs++] = expr.release ();
2473 cmdrest = cmd1;
2474 if (*cmdrest == ',')
2475 ++cmdrest;
2476 }
2477
2478 agent_expr_up aexpr;
2479
2480 /* We don't want to stop processing, so catch any errors
2481 that may show up. */
2482 TRY
2483 {
2484 aexpr = gen_printf (scope, gdbarch, 0, 0,
2485 format_start, format_end - format_start,
2486 fpieces, nargs, argvec);
2487 }
2488 CATCH (ex, RETURN_MASK_ERROR)
2489 {
2490 /* If we got here, it means the command could not be parsed to a valid
2491 bytecode expression and thus can't be evaluated on the target's side.
2492 It's no use iterating through the other commands. */
2493 }
2494 END_CATCH
2495
2496 do_cleanups (old_cleanups);
2497
2498 /* We have a valid agent expression, return it. */
2499 return aexpr;
2500 }
2501
2502 /* Based on location BL, create a list of breakpoint commands to be
2503 passed on to the target. If we have duplicated locations with
2504 different commands, we will add any such to the list. */
2505
2506 static void
2507 build_target_command_list (struct bp_location *bl)
2508 {
2509 struct bp_location **locp = NULL, **loc2p;
2510 int null_command_or_parse_error = 0;
2511 int modified = bl->needs_update;
2512 struct bp_location *loc;
2513
2514 /* Clear commands left over from a previous insert. */
2515 bl->target_info.tcommands.clear ();
2516
2517 if (!target_can_run_breakpoint_commands ())
2518 return;
2519
2520 /* For now, limit to agent-style dprintf breakpoints. */
2521 if (dprintf_style != dprintf_style_agent)
2522 return;
2523
2524 /* For now, if we have any duplicate location that isn't a dprintf,
2525 don't install the target-side commands, as that would make the
2526 breakpoint not be reported to the core, and we'd lose
2527 control. */
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner)
2532 && loc->pspace->num == bl->pspace->num
2533 && loc->owner->type != bp_dprintf)
2534 return;
2535 }
2536
2537 /* Do a first pass to check for locations with no assigned
2538 conditions or conditions that fail to parse to a valid agent expression
2539 bytecode. If any of these happen, then it's no use to send conditions
2540 to the target since this location will always trigger and generate a
2541 response back to GDB. */
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2546 {
2547 if (modified)
2548 {
2549 /* Re-parse the commands since something changed. In that
2550 case we already freed the command bytecodes (see
2551 force_breakpoint_reinsertion). We just
2552 need to parse the command to bytecodes again. */
2553 loc->cmd_bytecode
2554 = parse_cmd_to_aexpr (bl->address,
2555 loc->owner->extra_string);
2556 }
2557
2558 /* If we have a NULL bytecode expression, it means something
2559 went wrong or we have a null command expression. */
2560 if (!loc->cmd_bytecode)
2561 {
2562 null_command_or_parse_error = 1;
2563 break;
2564 }
2565 }
2566 }
2567
2568 /* If anything failed, then we're not doing target-side commands,
2569 and so clean up. */
2570 if (null_command_or_parse_error)
2571 {
2572 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2573 {
2574 loc = (*loc2p);
2575 if (is_breakpoint (loc->owner)
2576 && loc->pspace->num == bl->pspace->num)
2577 {
2578 /* Only go as far as the first NULL bytecode is
2579 located. */
2580 if (loc->cmd_bytecode == NULL)
2581 return;
2582
2583 loc->cmd_bytecode.reset ();
2584 }
2585 }
2586 }
2587
2588 /* No NULL commands or failed bytecode generation. Build a command list
2589 for this location's address. */
2590 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2591 {
2592 loc = (*loc2p);
2593 if (loc->owner->extra_string
2594 && is_breakpoint (loc->owner)
2595 && loc->pspace->num == bl->pspace->num
2596 && loc->owner->enable_state == bp_enabled
2597 && loc->enabled)
2598 {
2599 /* Add the command to the vector. This will be used later
2600 to send the commands to the target. */
2601 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2602 }
2603 }
2604
2605 bl->target_info.persist = 0;
2606 /* Maybe flag this location as persistent. */
2607 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2608 bl->target_info.persist = 1;
2609 }
2610
2611 /* Return the kind of breakpoint on address *ADDR. Get the kind
2612 of breakpoint according to ADDR except single-step breakpoint.
2613 Get the kind of single-step breakpoint according to the current
2614 registers state. */
2615
2616 static int
2617 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2618 {
2619 if (bl->owner->type == bp_single_step)
2620 {
2621 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2622 struct regcache *regcache;
2623
2624 regcache = get_thread_regcache (thr->ptid);
2625
2626 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2627 regcache, addr);
2628 }
2629 else
2630 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2631 }
2632
2633 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2634 location. Any error messages are printed to TMP_ERROR_STREAM; and
2635 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2636 Returns 0 for success, 1 if the bp_location type is not supported or
2637 -1 for failure.
2638
2639 NOTE drow/2003-09-09: This routine could be broken down to an
2640 object-style method for each breakpoint or catchpoint type. */
2641 static int
2642 insert_bp_location (struct bp_location *bl,
2643 struct ui_file *tmp_error_stream,
2644 int *disabled_breaks,
2645 int *hw_breakpoint_error,
2646 int *hw_bp_error_explained_already)
2647 {
2648 enum errors bp_err = GDB_NO_ERROR;
2649 const char *bp_err_message = NULL;
2650
2651 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2652 return 0;
2653
2654 /* Note we don't initialize bl->target_info, as that wipes out
2655 the breakpoint location's shadow_contents if the breakpoint
2656 is still inserted at that location. This in turn breaks
2657 target_read_memory which depends on these buffers when
2658 a memory read is requested at the breakpoint location:
2659 Once the target_info has been wiped, we fail to see that
2660 we have a breakpoint inserted at that address and thus
2661 read the breakpoint instead of returning the data saved in
2662 the breakpoint location's shadow contents. */
2663 bl->target_info.reqstd_address = bl->address;
2664 bl->target_info.placed_address_space = bl->pspace->aspace;
2665 bl->target_info.length = bl->length;
2666
2667 /* When working with target-side conditions, we must pass all the conditions
2668 for the same breakpoint address down to the target since GDB will not
2669 insert those locations. With a list of breakpoint conditions, the target
2670 can decide when to stop and notify GDB. */
2671
2672 if (is_breakpoint (bl->owner))
2673 {
2674 build_target_condition_list (bl);
2675 build_target_command_list (bl);
2676 /* Reset the modification marker. */
2677 bl->needs_update = 0;
2678 }
2679
2680 if (bl->loc_type == bp_loc_software_breakpoint
2681 || bl->loc_type == bp_loc_hardware_breakpoint)
2682 {
2683 if (bl->owner->type != bp_hardware_breakpoint)
2684 {
2685 /* If the explicitly specified breakpoint type
2686 is not hardware breakpoint, check the memory map to see
2687 if the breakpoint address is in read only memory or not.
2688
2689 Two important cases are:
2690 - location type is not hardware breakpoint, memory
2691 is readonly. We change the type of the location to
2692 hardware breakpoint.
2693 - location type is hardware breakpoint, memory is
2694 read-write. This means we've previously made the
2695 location hardware one, but then the memory map changed,
2696 so we undo.
2697
2698 When breakpoints are removed, remove_breakpoints will use
2699 location types we've just set here, the only possible
2700 problem is that memory map has changed during running
2701 program, but it's not going to work anyway with current
2702 gdb. */
2703 struct mem_region *mr
2704 = lookup_mem_region (bl->target_info.reqstd_address);
2705
2706 if (mr)
2707 {
2708 if (automatic_hardware_breakpoints)
2709 {
2710 enum bp_loc_type new_type;
2711
2712 if (mr->attrib.mode != MEM_RW)
2713 new_type = bp_loc_hardware_breakpoint;
2714 else
2715 new_type = bp_loc_software_breakpoint;
2716
2717 if (new_type != bl->loc_type)
2718 {
2719 static int said = 0;
2720
2721 bl->loc_type = new_type;
2722 if (!said)
2723 {
2724 fprintf_filtered (gdb_stdout,
2725 _("Note: automatically using "
2726 "hardware breakpoints for "
2727 "read-only addresses.\n"));
2728 said = 1;
2729 }
2730 }
2731 }
2732 else if (bl->loc_type == bp_loc_software_breakpoint
2733 && mr->attrib.mode != MEM_RW)
2734 {
2735 fprintf_unfiltered (tmp_error_stream,
2736 _("Cannot insert breakpoint %d.\n"
2737 "Cannot set software breakpoint "
2738 "at read-only address %s\n"),
2739 bl->owner->number,
2740 paddress (bl->gdbarch, bl->address));
2741 return 1;
2742 }
2743 }
2744 }
2745
2746 /* First check to see if we have to handle an overlay. */
2747 if (overlay_debugging == ovly_off
2748 || bl->section == NULL
2749 || !(section_is_overlay (bl->section)))
2750 {
2751 /* No overlay handling: just set the breakpoint. */
2752 TRY
2753 {
2754 int val;
2755
2756 val = bl->owner->ops->insert_location (bl);
2757 if (val)
2758 bp_err = GENERIC_ERROR;
2759 }
2760 CATCH (e, RETURN_MASK_ALL)
2761 {
2762 bp_err = e.error;
2763 bp_err_message = e.message;
2764 }
2765 END_CATCH
2766 }
2767 else
2768 {
2769 /* This breakpoint is in an overlay section.
2770 Shall we set a breakpoint at the LMA? */
2771 if (!overlay_events_enabled)
2772 {
2773 /* Yes -- overlay event support is not active,
2774 so we must try to set a breakpoint at the LMA.
2775 This will not work for a hardware breakpoint. */
2776 if (bl->loc_type == bp_loc_hardware_breakpoint)
2777 warning (_("hardware breakpoint %d not supported in overlay!"),
2778 bl->owner->number);
2779 else
2780 {
2781 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2782 bl->section);
2783 /* Set a software (trap) breakpoint at the LMA. */
2784 bl->overlay_target_info = bl->target_info;
2785 bl->overlay_target_info.reqstd_address = addr;
2786
2787 /* No overlay handling: just set the breakpoint. */
2788 TRY
2789 {
2790 int val;
2791
2792 bl->overlay_target_info.kind
2793 = breakpoint_kind (bl, &addr);
2794 bl->overlay_target_info.placed_address = addr;
2795 val = target_insert_breakpoint (bl->gdbarch,
2796 &bl->overlay_target_info);
2797 if (val)
2798 bp_err = GENERIC_ERROR;
2799 }
2800 CATCH (e, RETURN_MASK_ALL)
2801 {
2802 bp_err = e.error;
2803 bp_err_message = e.message;
2804 }
2805 END_CATCH
2806
2807 if (bp_err != GDB_NO_ERROR)
2808 fprintf_unfiltered (tmp_error_stream,
2809 "Overlay breakpoint %d "
2810 "failed: in ROM?\n",
2811 bl->owner->number);
2812 }
2813 }
2814 /* Shall we set a breakpoint at the VMA? */
2815 if (section_is_mapped (bl->section))
2816 {
2817 /* Yes. This overlay section is mapped into memory. */
2818 TRY
2819 {
2820 int val;
2821
2822 val = bl->owner->ops->insert_location (bl);
2823 if (val)
2824 bp_err = GENERIC_ERROR;
2825 }
2826 CATCH (e, RETURN_MASK_ALL)
2827 {
2828 bp_err = e.error;
2829 bp_err_message = e.message;
2830 }
2831 END_CATCH
2832 }
2833 else
2834 {
2835 /* No. This breakpoint will not be inserted.
2836 No error, but do not mark the bp as 'inserted'. */
2837 return 0;
2838 }
2839 }
2840
2841 if (bp_err != GDB_NO_ERROR)
2842 {
2843 /* Can't set the breakpoint. */
2844
2845 /* In some cases, we might not be able to insert a
2846 breakpoint in a shared library that has already been
2847 removed, but we have not yet processed the shlib unload
2848 event. Unfortunately, some targets that implement
2849 breakpoint insertion themselves can't tell why the
2850 breakpoint insertion failed (e.g., the remote target
2851 doesn't define error codes), so we must treat generic
2852 errors as memory errors. */
2853 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2854 && bl->loc_type == bp_loc_software_breakpoint
2855 && (solib_name_from_address (bl->pspace, bl->address)
2856 || shared_objfile_contains_address_p (bl->pspace,
2857 bl->address)))
2858 {
2859 /* See also: disable_breakpoints_in_shlibs. */
2860 bl->shlib_disabled = 1;
2861 observer_notify_breakpoint_modified (bl->owner);
2862 if (!*disabled_breaks)
2863 {
2864 fprintf_unfiltered (tmp_error_stream,
2865 "Cannot insert breakpoint %d.\n",
2866 bl->owner->number);
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Temporarily disabling shared "
2869 "library breakpoints:\n");
2870 }
2871 *disabled_breaks = 1;
2872 fprintf_unfiltered (tmp_error_stream,
2873 "breakpoint #%d\n", bl->owner->number);
2874 return 0;
2875 }
2876 else
2877 {
2878 if (bl->loc_type == bp_loc_hardware_breakpoint)
2879 {
2880 *hw_breakpoint_error = 1;
2881 *hw_bp_error_explained_already = bp_err_message != NULL;
2882 fprintf_unfiltered (tmp_error_stream,
2883 "Cannot insert hardware breakpoint %d%s",
2884 bl->owner->number, bp_err_message ? ":" : ".\n");
2885 if (bp_err_message != NULL)
2886 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2887 }
2888 else
2889 {
2890 if (bp_err_message == NULL)
2891 {
2892 std::string message
2893 = memory_error_message (TARGET_XFER_E_IO,
2894 bl->gdbarch, bl->address);
2895
2896 fprintf_unfiltered (tmp_error_stream,
2897 "Cannot insert breakpoint %d.\n"
2898 "%s\n",
2899 bl->owner->number, message.c_str ());
2900 }
2901 else
2902 {
2903 fprintf_unfiltered (tmp_error_stream,
2904 "Cannot insert breakpoint %d: %s\n",
2905 bl->owner->number,
2906 bp_err_message);
2907 }
2908 }
2909 return 1;
2910
2911 }
2912 }
2913 else
2914 bl->inserted = 1;
2915
2916 return 0;
2917 }
2918
2919 else if (bl->loc_type == bp_loc_hardware_watchpoint
2920 /* NOTE drow/2003-09-08: This state only exists for removing
2921 watchpoints. It's not clear that it's necessary... */
2922 && bl->owner->disposition != disp_del_at_next_stop)
2923 {
2924 int val;
2925
2926 gdb_assert (bl->owner->ops != NULL
2927 && bl->owner->ops->insert_location != NULL);
2928
2929 val = bl->owner->ops->insert_location (bl);
2930
2931 /* If trying to set a read-watchpoint, and it turns out it's not
2932 supported, try emulating one with an access watchpoint. */
2933 if (val == 1 && bl->watchpoint_type == hw_read)
2934 {
2935 struct bp_location *loc, **loc_temp;
2936
2937 /* But don't try to insert it, if there's already another
2938 hw_access location that would be considered a duplicate
2939 of this one. */
2940 ALL_BP_LOCATIONS (loc, loc_temp)
2941 if (loc != bl
2942 && loc->watchpoint_type == hw_access
2943 && watchpoint_locations_match (bl, loc))
2944 {
2945 bl->duplicate = 1;
2946 bl->inserted = 1;
2947 bl->target_info = loc->target_info;
2948 bl->watchpoint_type = hw_access;
2949 val = 0;
2950 break;
2951 }
2952
2953 if (val == 1)
2954 {
2955 bl->watchpoint_type = hw_access;
2956 val = bl->owner->ops->insert_location (bl);
2957
2958 if (val)
2959 /* Back to the original value. */
2960 bl->watchpoint_type = hw_read;
2961 }
2962 }
2963
2964 bl->inserted = (val == 0);
2965 }
2966
2967 else if (bl->owner->type == bp_catchpoint)
2968 {
2969 int val;
2970
2971 gdb_assert (bl->owner->ops != NULL
2972 && bl->owner->ops->insert_location != NULL);
2973
2974 val = bl->owner->ops->insert_location (bl);
2975 if (val)
2976 {
2977 bl->owner->enable_state = bp_disabled;
2978
2979 if (val == 1)
2980 warning (_("\
2981 Error inserting catchpoint %d: Your system does not support this type\n\
2982 of catchpoint."), bl->owner->number);
2983 else
2984 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2985 }
2986
2987 bl->inserted = (val == 0);
2988
2989 /* We've already printed an error message if there was a problem
2990 inserting this catchpoint, and we've disabled the catchpoint,
2991 so just return success. */
2992 return 0;
2993 }
2994
2995 return 0;
2996 }
2997
2998 /* This function is called when program space PSPACE is about to be
2999 deleted. It takes care of updating breakpoints to not reference
3000 PSPACE anymore. */
3001
3002 void
3003 breakpoint_program_space_exit (struct program_space *pspace)
3004 {
3005 struct breakpoint *b, *b_temp;
3006 struct bp_location *loc, **loc_temp;
3007
3008 /* Remove any breakpoint that was set through this program space. */
3009 ALL_BREAKPOINTS_SAFE (b, b_temp)
3010 {
3011 if (b->pspace == pspace)
3012 delete_breakpoint (b);
3013 }
3014
3015 /* Breakpoints set through other program spaces could have locations
3016 bound to PSPACE as well. Remove those. */
3017 ALL_BP_LOCATIONS (loc, loc_temp)
3018 {
3019 struct bp_location *tmp;
3020
3021 if (loc->pspace == pspace)
3022 {
3023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3024 if (loc->owner->loc == loc)
3025 loc->owner->loc = loc->next;
3026 else
3027 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3028 if (tmp->next == loc)
3029 {
3030 tmp->next = loc->next;
3031 break;
3032 }
3033 }
3034 }
3035
3036 /* Now update the global location list to permanently delete the
3037 removed locations above. */
3038 update_global_location_list (UGLL_DONT_INSERT);
3039 }
3040
3041 /* Make sure all breakpoints are inserted in inferior.
3042 Throws exception on any error.
3043 A breakpoint that is already inserted won't be inserted
3044 again, so calling this function twice is safe. */
3045 void
3046 insert_breakpoints (void)
3047 {
3048 struct breakpoint *bpt;
3049
3050 ALL_BREAKPOINTS (bpt)
3051 if (is_hardware_watchpoint (bpt))
3052 {
3053 struct watchpoint *w = (struct watchpoint *) bpt;
3054
3055 update_watchpoint (w, 0 /* don't reparse. */);
3056 }
3057
3058 /* Updating watchpoints creates new locations, so update the global
3059 location list. Explicitly tell ugll to insert locations and
3060 ignore breakpoints_always_inserted_mode. */
3061 update_global_location_list (UGLL_INSERT);
3062 }
3063
3064 /* Invoke CALLBACK for each of bp_location. */
3065
3066 void
3067 iterate_over_bp_locations (walk_bp_location_callback callback)
3068 {
3069 struct bp_location *loc, **loc_tmp;
3070
3071 ALL_BP_LOCATIONS (loc, loc_tmp)
3072 {
3073 callback (loc, NULL);
3074 }
3075 }
3076
3077 /* This is used when we need to synch breakpoint conditions between GDB and the
3078 target. It is the case with deleting and disabling of breakpoints when using
3079 always-inserted mode. */
3080
3081 static void
3082 update_inserted_breakpoint_locations (void)
3083 {
3084 struct bp_location *bl, **blp_tmp;
3085 int error_flag = 0;
3086 int val = 0;
3087 int disabled_breaks = 0;
3088 int hw_breakpoint_error = 0;
3089 int hw_bp_details_reported = 0;
3090
3091 string_file tmp_error_stream;
3092
3093 /* Explicitly mark the warning -- this will only be printed if
3094 there was an error. */
3095 tmp_error_stream.puts ("Warning:\n");
3096
3097 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3098
3099 ALL_BP_LOCATIONS (bl, blp_tmp)
3100 {
3101 /* We only want to update software breakpoints and hardware
3102 breakpoints. */
3103 if (!is_breakpoint (bl->owner))
3104 continue;
3105
3106 /* We only want to update locations that are already inserted
3107 and need updating. This is to avoid unwanted insertion during
3108 deletion of breakpoints. */
3109 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3110 continue;
3111
3112 switch_to_program_space_and_thread (bl->pspace);
3113
3114 /* For targets that support global breakpoints, there's no need
3115 to select an inferior to insert breakpoint to. In fact, even
3116 if we aren't attached to any process yet, we should still
3117 insert breakpoints. */
3118 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3119 && ptid_equal (inferior_ptid, null_ptid))
3120 continue;
3121
3122 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3123 &hw_breakpoint_error, &hw_bp_details_reported);
3124 if (val)
3125 error_flag = val;
3126 }
3127
3128 if (error_flag)
3129 {
3130 target_terminal_ours_for_output ();
3131 error_stream (tmp_error_stream);
3132 }
3133 }
3134
3135 /* Used when starting or continuing the program. */
3136
3137 static void
3138 insert_breakpoint_locations (void)
3139 {
3140 struct breakpoint *bpt;
3141 struct bp_location *bl, **blp_tmp;
3142 int error_flag = 0;
3143 int val = 0;
3144 int disabled_breaks = 0;
3145 int hw_breakpoint_error = 0;
3146 int hw_bp_error_explained_already = 0;
3147
3148 string_file tmp_error_stream;
3149
3150 /* Explicitly mark the warning -- this will only be printed if
3151 there was an error. */
3152 tmp_error_stream.puts ("Warning:\n");
3153
3154 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3155
3156 ALL_BP_LOCATIONS (bl, blp_tmp)
3157 {
3158 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3159 continue;
3160
3161 /* There is no point inserting thread-specific breakpoints if
3162 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3163 has BL->OWNER always non-NULL. */
3164 if (bl->owner->thread != -1
3165 && !valid_global_thread_id (bl->owner->thread))
3166 continue;
3167
3168 switch_to_program_space_and_thread (bl->pspace);
3169
3170 /* For targets that support global breakpoints, there's no need
3171 to select an inferior to insert breakpoint to. In fact, even
3172 if we aren't attached to any process yet, we should still
3173 insert breakpoints. */
3174 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3175 && ptid_equal (inferior_ptid, null_ptid))
3176 continue;
3177
3178 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3179 &hw_breakpoint_error, &hw_bp_error_explained_already);
3180 if (val)
3181 error_flag = val;
3182 }
3183
3184 /* If we failed to insert all locations of a watchpoint, remove
3185 them, as half-inserted watchpoint is of limited use. */
3186 ALL_BREAKPOINTS (bpt)
3187 {
3188 int some_failed = 0;
3189 struct bp_location *loc;
3190
3191 if (!is_hardware_watchpoint (bpt))
3192 continue;
3193
3194 if (!breakpoint_enabled (bpt))
3195 continue;
3196
3197 if (bpt->disposition == disp_del_at_next_stop)
3198 continue;
3199
3200 for (loc = bpt->loc; loc; loc = loc->next)
3201 if (!loc->inserted && should_be_inserted (loc))
3202 {
3203 some_failed = 1;
3204 break;
3205 }
3206 if (some_failed)
3207 {
3208 for (loc = bpt->loc; loc; loc = loc->next)
3209 if (loc->inserted)
3210 remove_breakpoint (loc);
3211
3212 hw_breakpoint_error = 1;
3213 tmp_error_stream.printf ("Could not insert "
3214 "hardware watchpoint %d.\n",
3215 bpt->number);
3216 error_flag = -1;
3217 }
3218 }
3219
3220 if (error_flag)
3221 {
3222 /* If a hardware breakpoint or watchpoint was inserted, add a
3223 message about possibly exhausted resources. */
3224 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3225 {
3226 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3227 You may have requested too many hardware breakpoints/watchpoints.\n");
3228 }
3229 target_terminal_ours_for_output ();
3230 error_stream (tmp_error_stream);
3231 }
3232 }
3233
3234 /* Used when the program stops.
3235 Returns zero if successful, or non-zero if there was a problem
3236 removing a breakpoint location. */
3237
3238 int
3239 remove_breakpoints (void)
3240 {
3241 struct bp_location *bl, **blp_tmp;
3242 int val = 0;
3243
3244 ALL_BP_LOCATIONS (bl, blp_tmp)
3245 {
3246 if (bl->inserted && !is_tracepoint (bl->owner))
3247 val |= remove_breakpoint (bl);
3248 }
3249 return val;
3250 }
3251
3252 /* When a thread exits, remove breakpoints that are related to
3253 that thread. */
3254
3255 static void
3256 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3257 {
3258 struct breakpoint *b, *b_tmp;
3259
3260 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3261 {
3262 if (b->thread == tp->global_num && user_breakpoint_p (b))
3263 {
3264 b->disposition = disp_del_at_next_stop;
3265
3266 printf_filtered (_("\
3267 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3268 b->number, print_thread_id (tp));
3269
3270 /* Hide it from the user. */
3271 b->number = 0;
3272 }
3273 }
3274 }
3275
3276 /* Remove breakpoints of process PID. */
3277
3278 int
3279 remove_breakpoints_pid (int pid)
3280 {
3281 struct bp_location *bl, **blp_tmp;
3282 int val;
3283 struct inferior *inf = find_inferior_pid (pid);
3284
3285 ALL_BP_LOCATIONS (bl, blp_tmp)
3286 {
3287 if (bl->pspace != inf->pspace)
3288 continue;
3289
3290 if (bl->inserted && !bl->target_info.persist)
3291 {
3292 val = remove_breakpoint (bl);
3293 if (val != 0)
3294 return val;
3295 }
3296 }
3297 return 0;
3298 }
3299
3300 int
3301 reattach_breakpoints (int pid)
3302 {
3303 struct bp_location *bl, **blp_tmp;
3304 int val;
3305 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3306 struct inferior *inf;
3307 struct thread_info *tp;
3308
3309 tp = any_live_thread_of_process (pid);
3310 if (tp == NULL)
3311 return 1;
3312
3313 inf = find_inferior_pid (pid);
3314
3315 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3316 inferior_ptid = tp->ptid;
3317
3318 string_file tmp_error_stream;
3319
3320 ALL_BP_LOCATIONS (bl, blp_tmp)
3321 {
3322 if (bl->pspace != inf->pspace)
3323 continue;
3324
3325 if (bl->inserted)
3326 {
3327 bl->inserted = 0;
3328 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3329 if (val != 0)
3330 return val;
3331 }
3332 }
3333 return 0;
3334 }
3335
3336 static int internal_breakpoint_number = -1;
3337
3338 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3339 If INTERNAL is non-zero, the breakpoint number will be populated
3340 from internal_breakpoint_number and that variable decremented.
3341 Otherwise the breakpoint number will be populated from
3342 breakpoint_count and that value incremented. Internal breakpoints
3343 do not set the internal var bpnum. */
3344 static void
3345 set_breakpoint_number (int internal, struct breakpoint *b)
3346 {
3347 if (internal)
3348 b->number = internal_breakpoint_number--;
3349 else
3350 {
3351 set_breakpoint_count (breakpoint_count + 1);
3352 b->number = breakpoint_count;
3353 }
3354 }
3355
3356 static struct breakpoint *
3357 create_internal_breakpoint (struct gdbarch *gdbarch,
3358 CORE_ADDR address, enum bptype type,
3359 const struct breakpoint_ops *ops)
3360 {
3361 symtab_and_line sal;
3362 sal.pc = address;
3363 sal.section = find_pc_overlay (sal.pc);
3364 sal.pspace = current_program_space;
3365
3366 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3367 b->number = internal_breakpoint_number--;
3368 b->disposition = disp_donttouch;
3369
3370 return b;
3371 }
3372
3373 static const char *const longjmp_names[] =
3374 {
3375 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3376 };
3377 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3378
3379 /* Per-objfile data private to breakpoint.c. */
3380 struct breakpoint_objfile_data
3381 {
3382 /* Minimal symbol for "_ovly_debug_event" (if any). */
3383 struct bound_minimal_symbol overlay_msym;
3384
3385 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3386 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3387
3388 /* True if we have looked for longjmp probes. */
3389 int longjmp_searched;
3390
3391 /* SystemTap probe points for longjmp (if any). */
3392 VEC (probe_p) *longjmp_probes;
3393
3394 /* Minimal symbol for "std::terminate()" (if any). */
3395 struct bound_minimal_symbol terminate_msym;
3396
3397 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3398 struct bound_minimal_symbol exception_msym;
3399
3400 /* True if we have looked for exception probes. */
3401 int exception_searched;
3402
3403 /* SystemTap probe points for unwinding (if any). */
3404 VEC (probe_p) *exception_probes;
3405 };
3406
3407 static const struct objfile_data *breakpoint_objfile_key;
3408
3409 /* Minimal symbol not found sentinel. */
3410 static struct minimal_symbol msym_not_found;
3411
3412 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3413
3414 static int
3415 msym_not_found_p (const struct minimal_symbol *msym)
3416 {
3417 return msym == &msym_not_found;
3418 }
3419
3420 /* Return per-objfile data needed by breakpoint.c.
3421 Allocate the data if necessary. */
3422
3423 static struct breakpoint_objfile_data *
3424 get_breakpoint_objfile_data (struct objfile *objfile)
3425 {
3426 struct breakpoint_objfile_data *bp_objfile_data;
3427
3428 bp_objfile_data = ((struct breakpoint_objfile_data *)
3429 objfile_data (objfile, breakpoint_objfile_key));
3430 if (bp_objfile_data == NULL)
3431 {
3432 bp_objfile_data =
3433 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3434
3435 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3436 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3437 }
3438 return bp_objfile_data;
3439 }
3440
3441 static void
3442 free_breakpoint_probes (struct objfile *obj, void *data)
3443 {
3444 struct breakpoint_objfile_data *bp_objfile_data
3445 = (struct breakpoint_objfile_data *) data;
3446
3447 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3448 VEC_free (probe_p, bp_objfile_data->exception_probes);
3449 }
3450
3451 static void
3452 create_overlay_event_breakpoint (void)
3453 {
3454 struct objfile *objfile;
3455 const char *const func_name = "_ovly_debug_event";
3456
3457 ALL_OBJFILES (objfile)
3458 {
3459 struct breakpoint *b;
3460 struct breakpoint_objfile_data *bp_objfile_data;
3461 CORE_ADDR addr;
3462 struct explicit_location explicit_loc;
3463
3464 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3465
3466 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3467 continue;
3468
3469 if (bp_objfile_data->overlay_msym.minsym == NULL)
3470 {
3471 struct bound_minimal_symbol m;
3472
3473 m = lookup_minimal_symbol_text (func_name, objfile);
3474 if (m.minsym == NULL)
3475 {
3476 /* Avoid future lookups in this objfile. */
3477 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3478 continue;
3479 }
3480 bp_objfile_data->overlay_msym = m;
3481 }
3482
3483 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3484 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3485 bp_overlay_event,
3486 &internal_breakpoint_ops);
3487 initialize_explicit_location (&explicit_loc);
3488 explicit_loc.function_name = ASTRDUP (func_name);
3489 b->location = new_explicit_location (&explicit_loc);
3490
3491 if (overlay_debugging == ovly_auto)
3492 {
3493 b->enable_state = bp_enabled;
3494 overlay_events_enabled = 1;
3495 }
3496 else
3497 {
3498 b->enable_state = bp_disabled;
3499 overlay_events_enabled = 0;
3500 }
3501 }
3502 }
3503
3504 static void
3505 create_longjmp_master_breakpoint (void)
3506 {
3507 struct program_space *pspace;
3508
3509 scoped_restore_current_program_space restore_pspace;
3510
3511 ALL_PSPACES (pspace)
3512 {
3513 struct objfile *objfile;
3514
3515 set_current_program_space (pspace);
3516
3517 ALL_OBJFILES (objfile)
3518 {
3519 int i;
3520 struct gdbarch *gdbarch;
3521 struct breakpoint_objfile_data *bp_objfile_data;
3522
3523 gdbarch = get_objfile_arch (objfile);
3524
3525 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3526
3527 if (!bp_objfile_data->longjmp_searched)
3528 {
3529 VEC (probe_p) *ret;
3530
3531 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3532 if (ret != NULL)
3533 {
3534 /* We are only interested in checking one element. */
3535 struct probe *p = VEC_index (probe_p, ret, 0);
3536
3537 if (!can_evaluate_probe_arguments (p))
3538 {
3539 /* We cannot use the probe interface here, because it does
3540 not know how to evaluate arguments. */
3541 VEC_free (probe_p, ret);
3542 ret = NULL;
3543 }
3544 }
3545 bp_objfile_data->longjmp_probes = ret;
3546 bp_objfile_data->longjmp_searched = 1;
3547 }
3548
3549 if (bp_objfile_data->longjmp_probes != NULL)
3550 {
3551 int i;
3552 struct probe *probe;
3553 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3554
3555 for (i = 0;
3556 VEC_iterate (probe_p,
3557 bp_objfile_data->longjmp_probes,
3558 i, probe);
3559 ++i)
3560 {
3561 struct breakpoint *b;
3562
3563 b = create_internal_breakpoint (gdbarch,
3564 get_probe_address (probe,
3565 objfile),
3566 bp_longjmp_master,
3567 &internal_breakpoint_ops);
3568 b->location = new_probe_location ("-probe-stap libc:longjmp");
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 continue;
3573 }
3574
3575 if (!gdbarch_get_longjmp_target_p (gdbarch))
3576 continue;
3577
3578 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3579 {
3580 struct breakpoint *b;
3581 const char *func_name;
3582 CORE_ADDR addr;
3583 struct explicit_location explicit_loc;
3584
3585 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3586 continue;
3587
3588 func_name = longjmp_names[i];
3589 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3590 {
3591 struct bound_minimal_symbol m;
3592
3593 m = lookup_minimal_symbol_text (func_name, objfile);
3594 if (m.minsym == NULL)
3595 {
3596 /* Prevent future lookups in this objfile. */
3597 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3598 continue;
3599 }
3600 bp_objfile_data->longjmp_msym[i] = m;
3601 }
3602
3603 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3604 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3605 &internal_breakpoint_ops);
3606 initialize_explicit_location (&explicit_loc);
3607 explicit_loc.function_name = ASTRDUP (func_name);
3608 b->location = new_explicit_location (&explicit_loc);
3609 b->enable_state = bp_disabled;
3610 }
3611 }
3612 }
3613 }
3614
3615 /* Create a master std::terminate breakpoint. */
3616 static void
3617 create_std_terminate_master_breakpoint (void)
3618 {
3619 struct program_space *pspace;
3620 const char *const func_name = "std::terminate()";
3621
3622 scoped_restore_current_program_space restore_pspace;
3623
3624 ALL_PSPACES (pspace)
3625 {
3626 struct objfile *objfile;
3627 CORE_ADDR addr;
3628
3629 set_current_program_space (pspace);
3630
3631 ALL_OBJFILES (objfile)
3632 {
3633 struct breakpoint *b;
3634 struct breakpoint_objfile_data *bp_objfile_data;
3635 struct explicit_location explicit_loc;
3636
3637 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3638
3639 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3640 continue;
3641
3642 if (bp_objfile_data->terminate_msym.minsym == NULL)
3643 {
3644 struct bound_minimal_symbol m;
3645
3646 m = lookup_minimal_symbol (func_name, NULL, objfile);
3647 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3648 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3649 {
3650 /* Prevent future lookups in this objfile. */
3651 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3652 continue;
3653 }
3654 bp_objfile_data->terminate_msym = m;
3655 }
3656
3657 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3658 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3659 bp_std_terminate_master,
3660 &internal_breakpoint_ops);
3661 initialize_explicit_location (&explicit_loc);
3662 explicit_loc.function_name = ASTRDUP (func_name);
3663 b->location = new_explicit_location (&explicit_loc);
3664 b->enable_state = bp_disabled;
3665 }
3666 }
3667 }
3668
3669 /* Install a master breakpoint on the unwinder's debug hook. */
3670
3671 static void
3672 create_exception_master_breakpoint (void)
3673 {
3674 struct objfile *objfile;
3675 const char *const func_name = "_Unwind_DebugHook";
3676
3677 ALL_OBJFILES (objfile)
3678 {
3679 struct breakpoint *b;
3680 struct gdbarch *gdbarch;
3681 struct breakpoint_objfile_data *bp_objfile_data;
3682 CORE_ADDR addr;
3683 struct explicit_location explicit_loc;
3684
3685 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3686
3687 /* We prefer the SystemTap probe point if it exists. */
3688 if (!bp_objfile_data->exception_searched)
3689 {
3690 VEC (probe_p) *ret;
3691
3692 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3693
3694 if (ret != NULL)
3695 {
3696 /* We are only interested in checking one element. */
3697 struct probe *p = VEC_index (probe_p, ret, 0);
3698
3699 if (!can_evaluate_probe_arguments (p))
3700 {
3701 /* We cannot use the probe interface here, because it does
3702 not know how to evaluate arguments. */
3703 VEC_free (probe_p, ret);
3704 ret = NULL;
3705 }
3706 }
3707 bp_objfile_data->exception_probes = ret;
3708 bp_objfile_data->exception_searched = 1;
3709 }
3710
3711 if (bp_objfile_data->exception_probes != NULL)
3712 {
3713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3714 int i;
3715 struct probe *probe;
3716
3717 for (i = 0;
3718 VEC_iterate (probe_p,
3719 bp_objfile_data->exception_probes,
3720 i, probe);
3721 ++i)
3722 {
3723 struct breakpoint *b;
3724
3725 b = create_internal_breakpoint (gdbarch,
3726 get_probe_address (probe,
3727 objfile),
3728 bp_exception_master,
3729 &internal_breakpoint_ops);
3730 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3731 b->enable_state = bp_disabled;
3732 }
3733
3734 continue;
3735 }
3736
3737 /* Otherwise, try the hook function. */
3738
3739 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3740 continue;
3741
3742 gdbarch = get_objfile_arch (objfile);
3743
3744 if (bp_objfile_data->exception_msym.minsym == NULL)
3745 {
3746 struct bound_minimal_symbol debug_hook;
3747
3748 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3749 if (debug_hook.minsym == NULL)
3750 {
3751 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3752 continue;
3753 }
3754
3755 bp_objfile_data->exception_msym = debug_hook;
3756 }
3757
3758 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3759 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3760 &current_target);
3761 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3762 &internal_breakpoint_ops);
3763 initialize_explicit_location (&explicit_loc);
3764 explicit_loc.function_name = ASTRDUP (func_name);
3765 b->location = new_explicit_location (&explicit_loc);
3766 b->enable_state = bp_disabled;
3767 }
3768 }
3769
3770 /* Does B have a location spec? */
3771
3772 static int
3773 breakpoint_event_location_empty_p (const struct breakpoint *b)
3774 {
3775 return b->location != NULL && event_location_empty_p (b->location.get ());
3776 }
3777
3778 void
3779 update_breakpoints_after_exec (void)
3780 {
3781 struct breakpoint *b, *b_tmp;
3782 struct bp_location *bploc, **bplocp_tmp;
3783
3784 /* We're about to delete breakpoints from GDB's lists. If the
3785 INSERTED flag is true, GDB will try to lift the breakpoints by
3786 writing the breakpoints' "shadow contents" back into memory. The
3787 "shadow contents" are NOT valid after an exec, so GDB should not
3788 do that. Instead, the target is responsible from marking
3789 breakpoints out as soon as it detects an exec. We don't do that
3790 here instead, because there may be other attempts to delete
3791 breakpoints after detecting an exec and before reaching here. */
3792 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3793 if (bploc->pspace == current_program_space)
3794 gdb_assert (!bploc->inserted);
3795
3796 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3797 {
3798 if (b->pspace != current_program_space)
3799 continue;
3800
3801 /* Solib breakpoints must be explicitly reset after an exec(). */
3802 if (b->type == bp_shlib_event)
3803 {
3804 delete_breakpoint (b);
3805 continue;
3806 }
3807
3808 /* JIT breakpoints must be explicitly reset after an exec(). */
3809 if (b->type == bp_jit_event)
3810 {
3811 delete_breakpoint (b);
3812 continue;
3813 }
3814
3815 /* Thread event breakpoints must be set anew after an exec(),
3816 as must overlay event and longjmp master breakpoints. */
3817 if (b->type == bp_thread_event || b->type == bp_overlay_event
3818 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3819 || b->type == bp_exception_master)
3820 {
3821 delete_breakpoint (b);
3822 continue;
3823 }
3824
3825 /* Step-resume breakpoints are meaningless after an exec(). */
3826 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3827 {
3828 delete_breakpoint (b);
3829 continue;
3830 }
3831
3832 /* Just like single-step breakpoints. */
3833 if (b->type == bp_single_step)
3834 {
3835 delete_breakpoint (b);
3836 continue;
3837 }
3838
3839 /* Longjmp and longjmp-resume breakpoints are also meaningless
3840 after an exec. */
3841 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3842 || b->type == bp_longjmp_call_dummy
3843 || b->type == bp_exception || b->type == bp_exception_resume)
3844 {
3845 delete_breakpoint (b);
3846 continue;
3847 }
3848
3849 if (b->type == bp_catchpoint)
3850 {
3851 /* For now, none of the bp_catchpoint breakpoints need to
3852 do anything at this point. In the future, if some of
3853 the catchpoints need to something, we will need to add
3854 a new method, and call this method from here. */
3855 continue;
3856 }
3857
3858 /* bp_finish is a special case. The only way we ought to be able
3859 to see one of these when an exec() has happened, is if the user
3860 caught a vfork, and then said "finish". Ordinarily a finish just
3861 carries them to the call-site of the current callee, by setting
3862 a temporary bp there and resuming. But in this case, the finish
3863 will carry them entirely through the vfork & exec.
3864
3865 We don't want to allow a bp_finish to remain inserted now. But
3866 we can't safely delete it, 'cause finish_command has a handle to
3867 the bp on a bpstat, and will later want to delete it. There's a
3868 chance (and I've seen it happen) that if we delete the bp_finish
3869 here, that its storage will get reused by the time finish_command
3870 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3871 We really must allow finish_command to delete a bp_finish.
3872
3873 In the absence of a general solution for the "how do we know
3874 it's safe to delete something others may have handles to?"
3875 problem, what we'll do here is just uninsert the bp_finish, and
3876 let finish_command delete it.
3877
3878 (We know the bp_finish is "doomed" in the sense that it's
3879 momentary, and will be deleted as soon as finish_command sees
3880 the inferior stopped. So it doesn't matter that the bp's
3881 address is probably bogus in the new a.out, unlike e.g., the
3882 solib breakpoints.) */
3883
3884 if (b->type == bp_finish)
3885 {
3886 continue;
3887 }
3888
3889 /* Without a symbolic address, we have little hope of the
3890 pre-exec() address meaning the same thing in the post-exec()
3891 a.out. */
3892 if (breakpoint_event_location_empty_p (b))
3893 {
3894 delete_breakpoint (b);
3895 continue;
3896 }
3897 }
3898 }
3899
3900 int
3901 detach_breakpoints (ptid_t ptid)
3902 {
3903 struct bp_location *bl, **blp_tmp;
3904 int val = 0;
3905 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3906 struct inferior *inf = current_inferior ();
3907
3908 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3909 error (_("Cannot detach breakpoints of inferior_ptid"));
3910
3911 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3912 inferior_ptid = ptid;
3913 ALL_BP_LOCATIONS (bl, blp_tmp)
3914 {
3915 if (bl->pspace != inf->pspace)
3916 continue;
3917
3918 /* This function must physically remove breakpoints locations
3919 from the specified ptid, without modifying the breakpoint
3920 package's state. Locations of type bp_loc_other are only
3921 maintained at GDB side. So, there is no need to remove
3922 these bp_loc_other locations. Moreover, removing these
3923 would modify the breakpoint package's state. */
3924 if (bl->loc_type == bp_loc_other)
3925 continue;
3926
3927 if (bl->inserted)
3928 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3929 }
3930
3931 return val;
3932 }
3933
3934 /* Remove the breakpoint location BL from the current address space.
3935 Note that this is used to detach breakpoints from a child fork.
3936 When we get here, the child isn't in the inferior list, and neither
3937 do we have objects to represent its address space --- we should
3938 *not* look at bl->pspace->aspace here. */
3939
3940 static int
3941 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3942 {
3943 int val;
3944
3945 /* BL is never in moribund_locations by our callers. */
3946 gdb_assert (bl->owner != NULL);
3947
3948 /* The type of none suggests that owner is actually deleted.
3949 This should not ever happen. */
3950 gdb_assert (bl->owner->type != bp_none);
3951
3952 if (bl->loc_type == bp_loc_software_breakpoint
3953 || bl->loc_type == bp_loc_hardware_breakpoint)
3954 {
3955 /* "Normal" instruction breakpoint: either the standard
3956 trap-instruction bp (bp_breakpoint), or a
3957 bp_hardware_breakpoint. */
3958
3959 /* First check to see if we have to handle an overlay. */
3960 if (overlay_debugging == ovly_off
3961 || bl->section == NULL
3962 || !(section_is_overlay (bl->section)))
3963 {
3964 /* No overlay handling: just remove the breakpoint. */
3965
3966 /* If we're trying to uninsert a memory breakpoint that we
3967 know is set in a dynamic object that is marked
3968 shlib_disabled, then either the dynamic object was
3969 removed with "remove-symbol-file" or with
3970 "nosharedlibrary". In the former case, we don't know
3971 whether another dynamic object might have loaded over the
3972 breakpoint's address -- the user might well let us know
3973 about it next with add-symbol-file (the whole point of
3974 add-symbol-file is letting the user manually maintain a
3975 list of dynamically loaded objects). If we have the
3976 breakpoint's shadow memory, that is, this is a software
3977 breakpoint managed by GDB, check whether the breakpoint
3978 is still inserted in memory, to avoid overwriting wrong
3979 code with stale saved shadow contents. Note that HW
3980 breakpoints don't have shadow memory, as they're
3981 implemented using a mechanism that is not dependent on
3982 being able to modify the target's memory, and as such
3983 they should always be removed. */
3984 if (bl->shlib_disabled
3985 && bl->target_info.shadow_len != 0
3986 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3987 val = 0;
3988 else
3989 val = bl->owner->ops->remove_location (bl, reason);
3990 }
3991 else
3992 {
3993 /* This breakpoint is in an overlay section.
3994 Did we set a breakpoint at the LMA? */
3995 if (!overlay_events_enabled)
3996 {
3997 /* Yes -- overlay event support is not active, so we
3998 should have set a breakpoint at the LMA. Remove it.
3999 */
4000 /* Ignore any failures: if the LMA is in ROM, we will
4001 have already warned when we failed to insert it. */
4002 if (bl->loc_type == bp_loc_hardware_breakpoint)
4003 target_remove_hw_breakpoint (bl->gdbarch,
4004 &bl->overlay_target_info);
4005 else
4006 target_remove_breakpoint (bl->gdbarch,
4007 &bl->overlay_target_info,
4008 reason);
4009 }
4010 /* Did we set a breakpoint at the VMA?
4011 If so, we will have marked the breakpoint 'inserted'. */
4012 if (bl->inserted)
4013 {
4014 /* Yes -- remove it. Previously we did not bother to
4015 remove the breakpoint if the section had been
4016 unmapped, but let's not rely on that being safe. We
4017 don't know what the overlay manager might do. */
4018
4019 /* However, we should remove *software* breakpoints only
4020 if the section is still mapped, or else we overwrite
4021 wrong code with the saved shadow contents. */
4022 if (bl->loc_type == bp_loc_hardware_breakpoint
4023 || section_is_mapped (bl->section))
4024 val = bl->owner->ops->remove_location (bl, reason);
4025 else
4026 val = 0;
4027 }
4028 else
4029 {
4030 /* No -- not inserted, so no need to remove. No error. */
4031 val = 0;
4032 }
4033 }
4034
4035 /* In some cases, we might not be able to remove a breakpoint in
4036 a shared library that has already been removed, but we have
4037 not yet processed the shlib unload event. Similarly for an
4038 unloaded add-symbol-file object - the user might not yet have
4039 had the chance to remove-symbol-file it. shlib_disabled will
4040 be set if the library/object has already been removed, but
4041 the breakpoint hasn't been uninserted yet, e.g., after
4042 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4043 always-inserted mode. */
4044 if (val
4045 && (bl->loc_type == bp_loc_software_breakpoint
4046 && (bl->shlib_disabled
4047 || solib_name_from_address (bl->pspace, bl->address)
4048 || shared_objfile_contains_address_p (bl->pspace,
4049 bl->address))))
4050 val = 0;
4051
4052 if (val)
4053 return val;
4054 bl->inserted = (reason == DETACH_BREAKPOINT);
4055 }
4056 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4057 {
4058 gdb_assert (bl->owner->ops != NULL
4059 && bl->owner->ops->remove_location != NULL);
4060
4061 bl->inserted = (reason == DETACH_BREAKPOINT);
4062 bl->owner->ops->remove_location (bl, reason);
4063
4064 /* Failure to remove any of the hardware watchpoints comes here. */
4065 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4066 warning (_("Could not remove hardware watchpoint %d."),
4067 bl->owner->number);
4068 }
4069 else if (bl->owner->type == bp_catchpoint
4070 && breakpoint_enabled (bl->owner)
4071 && !bl->duplicate)
4072 {
4073 gdb_assert (bl->owner->ops != NULL
4074 && bl->owner->ops->remove_location != NULL);
4075
4076 val = bl->owner->ops->remove_location (bl, reason);
4077 if (val)
4078 return val;
4079
4080 bl->inserted = (reason == DETACH_BREAKPOINT);
4081 }
4082
4083 return 0;
4084 }
4085
4086 static int
4087 remove_breakpoint (struct bp_location *bl)
4088 {
4089 /* BL is never in moribund_locations by our callers. */
4090 gdb_assert (bl->owner != NULL);
4091
4092 /* The type of none suggests that owner is actually deleted.
4093 This should not ever happen. */
4094 gdb_assert (bl->owner->type != bp_none);
4095
4096 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4097
4098 switch_to_program_space_and_thread (bl->pspace);
4099
4100 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4101 }
4102
4103 /* Clear the "inserted" flag in all breakpoints. */
4104
4105 void
4106 mark_breakpoints_out (void)
4107 {
4108 struct bp_location *bl, **blp_tmp;
4109
4110 ALL_BP_LOCATIONS (bl, blp_tmp)
4111 if (bl->pspace == current_program_space)
4112 bl->inserted = 0;
4113 }
4114
4115 /* Clear the "inserted" flag in all breakpoints and delete any
4116 breakpoints which should go away between runs of the program.
4117
4118 Plus other such housekeeping that has to be done for breakpoints
4119 between runs.
4120
4121 Note: this function gets called at the end of a run (by
4122 generic_mourn_inferior) and when a run begins (by
4123 init_wait_for_inferior). */
4124
4125
4126
4127 void
4128 breakpoint_init_inferior (enum inf_context context)
4129 {
4130 struct breakpoint *b, *b_tmp;
4131 struct bp_location *bl;
4132 int ix;
4133 struct program_space *pspace = current_program_space;
4134
4135 /* If breakpoint locations are shared across processes, then there's
4136 nothing to do. */
4137 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4138 return;
4139
4140 mark_breakpoints_out ();
4141
4142 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4143 {
4144 if (b->loc && b->loc->pspace != pspace)
4145 continue;
4146
4147 switch (b->type)
4148 {
4149 case bp_call_dummy:
4150 case bp_longjmp_call_dummy:
4151
4152 /* If the call dummy breakpoint is at the entry point it will
4153 cause problems when the inferior is rerun, so we better get
4154 rid of it. */
4155
4156 case bp_watchpoint_scope:
4157
4158 /* Also get rid of scope breakpoints. */
4159
4160 case bp_shlib_event:
4161
4162 /* Also remove solib event breakpoints. Their addresses may
4163 have changed since the last time we ran the program.
4164 Actually we may now be debugging against different target;
4165 and so the solib backend that installed this breakpoint may
4166 not be used in by the target. E.g.,
4167
4168 (gdb) file prog-linux
4169 (gdb) run # native linux target
4170 ...
4171 (gdb) kill
4172 (gdb) file prog-win.exe
4173 (gdb) tar rem :9999 # remote Windows gdbserver.
4174 */
4175
4176 case bp_step_resume:
4177
4178 /* Also remove step-resume breakpoints. */
4179
4180 case bp_single_step:
4181
4182 /* Also remove single-step breakpoints. */
4183
4184 delete_breakpoint (b);
4185 break;
4186
4187 case bp_watchpoint:
4188 case bp_hardware_watchpoint:
4189 case bp_read_watchpoint:
4190 case bp_access_watchpoint:
4191 {
4192 struct watchpoint *w = (struct watchpoint *) b;
4193
4194 /* Likewise for watchpoints on local expressions. */
4195 if (w->exp_valid_block != NULL)
4196 delete_breakpoint (b);
4197 else
4198 {
4199 /* Get rid of existing locations, which are no longer
4200 valid. New ones will be created in
4201 update_watchpoint, when the inferior is restarted.
4202 The next update_global_location_list call will
4203 garbage collect them. */
4204 b->loc = NULL;
4205
4206 if (context == inf_starting)
4207 {
4208 /* Reset val field to force reread of starting value in
4209 insert_breakpoints. */
4210 if (w->val)
4211 value_free (w->val);
4212 w->val = NULL;
4213 w->val_valid = 0;
4214 }
4215 }
4216 }
4217 break;
4218 default:
4219 break;
4220 }
4221 }
4222
4223 /* Get rid of the moribund locations. */
4224 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4225 decref_bp_location (&bl);
4226 VEC_free (bp_location_p, moribund_locations);
4227 }
4228
4229 /* These functions concern about actual breakpoints inserted in the
4230 target --- to e.g. check if we need to do decr_pc adjustment or if
4231 we need to hop over the bkpt --- so we check for address space
4232 match, not program space. */
4233
4234 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4235 exists at PC. It returns ordinary_breakpoint_here if it's an
4236 ordinary breakpoint, or permanent_breakpoint_here if it's a
4237 permanent breakpoint.
4238 - When continuing from a location with an ordinary breakpoint, we
4239 actually single step once before calling insert_breakpoints.
4240 - When continuing from a location with a permanent breakpoint, we
4241 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4242 the target, to advance the PC past the breakpoint. */
4243
4244 enum breakpoint_here
4245 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4246 {
4247 struct bp_location *bl, **blp_tmp;
4248 int any_breakpoint_here = 0;
4249
4250 ALL_BP_LOCATIONS (bl, blp_tmp)
4251 {
4252 if (bl->loc_type != bp_loc_software_breakpoint
4253 && bl->loc_type != bp_loc_hardware_breakpoint)
4254 continue;
4255
4256 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4257 if ((breakpoint_enabled (bl->owner)
4258 || bl->permanent)
4259 && breakpoint_location_address_match (bl, aspace, pc))
4260 {
4261 if (overlay_debugging
4262 && section_is_overlay (bl->section)
4263 && !section_is_mapped (bl->section))
4264 continue; /* unmapped overlay -- can't be a match */
4265 else if (bl->permanent)
4266 return permanent_breakpoint_here;
4267 else
4268 any_breakpoint_here = 1;
4269 }
4270 }
4271
4272 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4273 }
4274
4275 /* See breakpoint.h. */
4276
4277 int
4278 breakpoint_in_range_p (struct address_space *aspace,
4279 CORE_ADDR addr, ULONGEST len)
4280 {
4281 struct bp_location *bl, **blp_tmp;
4282
4283 ALL_BP_LOCATIONS (bl, blp_tmp)
4284 {
4285 if (bl->loc_type != bp_loc_software_breakpoint
4286 && bl->loc_type != bp_loc_hardware_breakpoint)
4287 continue;
4288
4289 if ((breakpoint_enabled (bl->owner)
4290 || bl->permanent)
4291 && breakpoint_location_address_range_overlap (bl, aspace,
4292 addr, len))
4293 {
4294 if (overlay_debugging
4295 && section_is_overlay (bl->section)
4296 && !section_is_mapped (bl->section))
4297 {
4298 /* Unmapped overlay -- can't be a match. */
4299 continue;
4300 }
4301
4302 return 1;
4303 }
4304 }
4305
4306 return 0;
4307 }
4308
4309 /* Return true if there's a moribund breakpoint at PC. */
4310
4311 int
4312 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4313 {
4314 struct bp_location *loc;
4315 int ix;
4316
4317 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4318 if (breakpoint_location_address_match (loc, aspace, pc))
4319 return 1;
4320
4321 return 0;
4322 }
4323
4324 /* Returns non-zero iff BL is inserted at PC, in address space
4325 ASPACE. */
4326
4327 static int
4328 bp_location_inserted_here_p (struct bp_location *bl,
4329 struct address_space *aspace, CORE_ADDR pc)
4330 {
4331 if (bl->inserted
4332 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4333 aspace, pc))
4334 {
4335 if (overlay_debugging
4336 && section_is_overlay (bl->section)
4337 && !section_is_mapped (bl->section))
4338 return 0; /* unmapped overlay -- can't be a match */
4339 else
4340 return 1;
4341 }
4342 return 0;
4343 }
4344
4345 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4346
4347 int
4348 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4349 {
4350 struct bp_location **blp, **blp_tmp = NULL;
4351
4352 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4353 {
4354 struct bp_location *bl = *blp;
4355
4356 if (bl->loc_type != bp_loc_software_breakpoint
4357 && bl->loc_type != bp_loc_hardware_breakpoint)
4358 continue;
4359
4360 if (bp_location_inserted_here_p (bl, aspace, pc))
4361 return 1;
4362 }
4363 return 0;
4364 }
4365
4366 /* This function returns non-zero iff there is a software breakpoint
4367 inserted at PC. */
4368
4369 int
4370 software_breakpoint_inserted_here_p (struct address_space *aspace,
4371 CORE_ADDR pc)
4372 {
4373 struct bp_location **blp, **blp_tmp = NULL;
4374
4375 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4376 {
4377 struct bp_location *bl = *blp;
4378
4379 if (bl->loc_type != bp_loc_software_breakpoint)
4380 continue;
4381
4382 if (bp_location_inserted_here_p (bl, aspace, pc))
4383 return 1;
4384 }
4385
4386 return 0;
4387 }
4388
4389 /* See breakpoint.h. */
4390
4391 int
4392 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4393 CORE_ADDR pc)
4394 {
4395 struct bp_location **blp, **blp_tmp = NULL;
4396
4397 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4398 {
4399 struct bp_location *bl = *blp;
4400
4401 if (bl->loc_type != bp_loc_hardware_breakpoint)
4402 continue;
4403
4404 if (bp_location_inserted_here_p (bl, aspace, pc))
4405 return 1;
4406 }
4407
4408 return 0;
4409 }
4410
4411 int
4412 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4413 CORE_ADDR addr, ULONGEST len)
4414 {
4415 struct breakpoint *bpt;
4416
4417 ALL_BREAKPOINTS (bpt)
4418 {
4419 struct bp_location *loc;
4420
4421 if (bpt->type != bp_hardware_watchpoint
4422 && bpt->type != bp_access_watchpoint)
4423 continue;
4424
4425 if (!breakpoint_enabled (bpt))
4426 continue;
4427
4428 for (loc = bpt->loc; loc; loc = loc->next)
4429 if (loc->pspace->aspace == aspace && loc->inserted)
4430 {
4431 CORE_ADDR l, h;
4432
4433 /* Check for intersection. */
4434 l = std::max<CORE_ADDR> (loc->address, addr);
4435 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4436 if (l < h)
4437 return 1;
4438 }
4439 }
4440 return 0;
4441 }
4442 \f
4443
4444 /* bpstat stuff. External routines' interfaces are documented
4445 in breakpoint.h. */
4446
4447 int
4448 is_catchpoint (struct breakpoint *ep)
4449 {
4450 return (ep->type == bp_catchpoint);
4451 }
4452
4453 /* Frees any storage that is part of a bpstat. Does not walk the
4454 'next' chain. */
4455
4456 static void
4457 bpstat_free (bpstat bs)
4458 {
4459 if (bs->old_val != NULL)
4460 value_free (bs->old_val);
4461 decref_counted_command_line (&bs->commands);
4462 decref_bp_location (&bs->bp_location_at);
4463 xfree (bs);
4464 }
4465
4466 /* Clear a bpstat so that it says we are not at any breakpoint.
4467 Also free any storage that is part of a bpstat. */
4468
4469 void
4470 bpstat_clear (bpstat *bsp)
4471 {
4472 bpstat p;
4473 bpstat q;
4474
4475 if (bsp == 0)
4476 return;
4477 p = *bsp;
4478 while (p != NULL)
4479 {
4480 q = p->next;
4481 bpstat_free (p);
4482 p = q;
4483 }
4484 *bsp = NULL;
4485 }
4486
4487 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4488 is part of the bpstat is copied as well. */
4489
4490 bpstat
4491 bpstat_copy (bpstat bs)
4492 {
4493 bpstat p = NULL;
4494 bpstat tmp;
4495 bpstat retval = NULL;
4496
4497 if (bs == NULL)
4498 return bs;
4499
4500 for (; bs != NULL; bs = bs->next)
4501 {
4502 tmp = (bpstat) xmalloc (sizeof (*tmp));
4503 memcpy (tmp, bs, sizeof (*tmp));
4504 incref_counted_command_line (tmp->commands);
4505 incref_bp_location (tmp->bp_location_at);
4506 if (bs->old_val != NULL)
4507 {
4508 tmp->old_val = value_copy (bs->old_val);
4509 release_value (tmp->old_val);
4510 }
4511
4512 if (p == NULL)
4513 /* This is the first thing in the chain. */
4514 retval = tmp;
4515 else
4516 p->next = tmp;
4517 p = tmp;
4518 }
4519 p->next = NULL;
4520 return retval;
4521 }
4522
4523 /* Find the bpstat associated with this breakpoint. */
4524
4525 bpstat
4526 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4527 {
4528 if (bsp == NULL)
4529 return NULL;
4530
4531 for (; bsp != NULL; bsp = bsp->next)
4532 {
4533 if (bsp->breakpoint_at == breakpoint)
4534 return bsp;
4535 }
4536 return NULL;
4537 }
4538
4539 /* See breakpoint.h. */
4540
4541 int
4542 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4543 {
4544 for (; bsp != NULL; bsp = bsp->next)
4545 {
4546 if (bsp->breakpoint_at == NULL)
4547 {
4548 /* A moribund location can never explain a signal other than
4549 GDB_SIGNAL_TRAP. */
4550 if (sig == GDB_SIGNAL_TRAP)
4551 return 1;
4552 }
4553 else
4554 {
4555 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4556 sig))
4557 return 1;
4558 }
4559 }
4560
4561 return 0;
4562 }
4563
4564 /* Put in *NUM the breakpoint number of the first breakpoint we are
4565 stopped at. *BSP upon return is a bpstat which points to the
4566 remaining breakpoints stopped at (but which is not guaranteed to be
4567 good for anything but further calls to bpstat_num).
4568
4569 Return 0 if passed a bpstat which does not indicate any breakpoints.
4570 Return -1 if stopped at a breakpoint that has been deleted since
4571 we set it.
4572 Return 1 otherwise. */
4573
4574 int
4575 bpstat_num (bpstat *bsp, int *num)
4576 {
4577 struct breakpoint *b;
4578
4579 if ((*bsp) == NULL)
4580 return 0; /* No more breakpoint values */
4581
4582 /* We assume we'll never have several bpstats that correspond to a
4583 single breakpoint -- otherwise, this function might return the
4584 same number more than once and this will look ugly. */
4585 b = (*bsp)->breakpoint_at;
4586 *bsp = (*bsp)->next;
4587 if (b == NULL)
4588 return -1; /* breakpoint that's been deleted since */
4589
4590 *num = b->number; /* We have its number */
4591 return 1;
4592 }
4593
4594 /* See breakpoint.h. */
4595
4596 void
4597 bpstat_clear_actions (void)
4598 {
4599 struct thread_info *tp;
4600 bpstat bs;
4601
4602 if (ptid_equal (inferior_ptid, null_ptid))
4603 return;
4604
4605 tp = find_thread_ptid (inferior_ptid);
4606 if (tp == NULL)
4607 return;
4608
4609 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4610 {
4611 decref_counted_command_line (&bs->commands);
4612
4613 if (bs->old_val != NULL)
4614 {
4615 value_free (bs->old_val);
4616 bs->old_val = NULL;
4617 }
4618 }
4619 }
4620
4621 /* Called when a command is about to proceed the inferior. */
4622
4623 static void
4624 breakpoint_about_to_proceed (void)
4625 {
4626 if (!ptid_equal (inferior_ptid, null_ptid))
4627 {
4628 struct thread_info *tp = inferior_thread ();
4629
4630 /* Allow inferior function calls in breakpoint commands to not
4631 interrupt the command list. When the call finishes
4632 successfully, the inferior will be standing at the same
4633 breakpoint as if nothing happened. */
4634 if (tp->control.in_infcall)
4635 return;
4636 }
4637
4638 breakpoint_proceeded = 1;
4639 }
4640
4641 /* Stub for cleaning up our state if we error-out of a breakpoint
4642 command. */
4643 static void
4644 cleanup_executing_breakpoints (void *ignore)
4645 {
4646 executing_breakpoint_commands = 0;
4647 }
4648
4649 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4650 or its equivalent. */
4651
4652 static int
4653 command_line_is_silent (struct command_line *cmd)
4654 {
4655 return cmd && (strcmp ("silent", cmd->line) == 0);
4656 }
4657
4658 /* Execute all the commands associated with all the breakpoints at
4659 this location. Any of these commands could cause the process to
4660 proceed beyond this point, etc. We look out for such changes by
4661 checking the global "breakpoint_proceeded" after each command.
4662
4663 Returns true if a breakpoint command resumed the inferior. In that
4664 case, it is the caller's responsibility to recall it again with the
4665 bpstat of the current thread. */
4666
4667 static int
4668 bpstat_do_actions_1 (bpstat *bsp)
4669 {
4670 bpstat bs;
4671 struct cleanup *old_chain;
4672 int again = 0;
4673
4674 /* Avoid endless recursion if a `source' command is contained
4675 in bs->commands. */
4676 if (executing_breakpoint_commands)
4677 return 0;
4678
4679 executing_breakpoint_commands = 1;
4680 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4681
4682 scoped_restore preventer = prevent_dont_repeat ();
4683
4684 /* This pointer will iterate over the list of bpstat's. */
4685 bs = *bsp;
4686
4687 breakpoint_proceeded = 0;
4688 for (; bs != NULL; bs = bs->next)
4689 {
4690 struct counted_command_line *ccmd;
4691 struct command_line *cmd;
4692 struct cleanup *this_cmd_tree_chain;
4693
4694 /* Take ownership of the BSP's command tree, if it has one.
4695
4696 The command tree could legitimately contain commands like
4697 'step' and 'next', which call clear_proceed_status, which
4698 frees stop_bpstat's command tree. To make sure this doesn't
4699 free the tree we're executing out from under us, we need to
4700 take ownership of the tree ourselves. Since a given bpstat's
4701 commands are only executed once, we don't need to copy it; we
4702 can clear the pointer in the bpstat, and make sure we free
4703 the tree when we're done. */
4704 ccmd = bs->commands;
4705 bs->commands = NULL;
4706 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4707 cmd = ccmd ? ccmd->commands : NULL;
4708 if (command_line_is_silent (cmd))
4709 {
4710 /* The action has been already done by bpstat_stop_status. */
4711 cmd = cmd->next;
4712 }
4713
4714 while (cmd != NULL)
4715 {
4716 execute_control_command (cmd);
4717
4718 if (breakpoint_proceeded)
4719 break;
4720 else
4721 cmd = cmd->next;
4722 }
4723
4724 /* We can free this command tree now. */
4725 do_cleanups (this_cmd_tree_chain);
4726
4727 if (breakpoint_proceeded)
4728 {
4729 if (current_ui->async)
4730 /* If we are in async mode, then the target might be still
4731 running, not stopped at any breakpoint, so nothing for
4732 us to do here -- just return to the event loop. */
4733 ;
4734 else
4735 /* In sync mode, when execute_control_command returns
4736 we're already standing on the next breakpoint.
4737 Breakpoint commands for that stop were not run, since
4738 execute_command does not run breakpoint commands --
4739 only command_line_handler does, but that one is not
4740 involved in execution of breakpoint commands. So, we
4741 can now execute breakpoint commands. It should be
4742 noted that making execute_command do bpstat actions is
4743 not an option -- in this case we'll have recursive
4744 invocation of bpstat for each breakpoint with a
4745 command, and can easily blow up GDB stack. Instead, we
4746 return true, which will trigger the caller to recall us
4747 with the new stop_bpstat. */
4748 again = 1;
4749 break;
4750 }
4751 }
4752 do_cleanups (old_chain);
4753 return again;
4754 }
4755
4756 void
4757 bpstat_do_actions (void)
4758 {
4759 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4760
4761 /* Do any commands attached to breakpoint we are stopped at. */
4762 while (!ptid_equal (inferior_ptid, null_ptid)
4763 && target_has_execution
4764 && !is_exited (inferior_ptid)
4765 && !is_executing (inferior_ptid))
4766 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4767 and only return when it is stopped at the next breakpoint, we
4768 keep doing breakpoint actions until it returns false to
4769 indicate the inferior was not resumed. */
4770 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4771 break;
4772
4773 discard_cleanups (cleanup_if_error);
4774 }
4775
4776 /* Print out the (old or new) value associated with a watchpoint. */
4777
4778 static void
4779 watchpoint_value_print (struct value *val, struct ui_file *stream)
4780 {
4781 if (val == NULL)
4782 fprintf_unfiltered (stream, _("<unreadable>"));
4783 else
4784 {
4785 struct value_print_options opts;
4786 get_user_print_options (&opts);
4787 value_print (val, stream, &opts);
4788 }
4789 }
4790
4791 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4792 debugging multiple threads. */
4793
4794 void
4795 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4796 {
4797 if (uiout->is_mi_like_p ())
4798 return;
4799
4800 uiout->text ("\n");
4801
4802 if (show_thread_that_caused_stop ())
4803 {
4804 const char *name;
4805 struct thread_info *thr = inferior_thread ();
4806
4807 uiout->text ("Thread ");
4808 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4809
4810 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4811 if (name != NULL)
4812 {
4813 uiout->text (" \"");
4814 uiout->field_fmt ("name", "%s", name);
4815 uiout->text ("\"");
4816 }
4817
4818 uiout->text (" hit ");
4819 }
4820 }
4821
4822 /* Generic routine for printing messages indicating why we
4823 stopped. The behavior of this function depends on the value
4824 'print_it' in the bpstat structure. Under some circumstances we
4825 may decide not to print anything here and delegate the task to
4826 normal_stop(). */
4827
4828 static enum print_stop_action
4829 print_bp_stop_message (bpstat bs)
4830 {
4831 switch (bs->print_it)
4832 {
4833 case print_it_noop:
4834 /* Nothing should be printed for this bpstat entry. */
4835 return PRINT_UNKNOWN;
4836 break;
4837
4838 case print_it_done:
4839 /* We still want to print the frame, but we already printed the
4840 relevant messages. */
4841 return PRINT_SRC_AND_LOC;
4842 break;
4843
4844 case print_it_normal:
4845 {
4846 struct breakpoint *b = bs->breakpoint_at;
4847
4848 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4849 which has since been deleted. */
4850 if (b == NULL)
4851 return PRINT_UNKNOWN;
4852
4853 /* Normal case. Call the breakpoint's print_it method. */
4854 return b->ops->print_it (bs);
4855 }
4856 break;
4857
4858 default:
4859 internal_error (__FILE__, __LINE__,
4860 _("print_bp_stop_message: unrecognized enum value"));
4861 break;
4862 }
4863 }
4864
4865 /* A helper function that prints a shared library stopped event. */
4866
4867 static void
4868 print_solib_event (int is_catchpoint)
4869 {
4870 int any_deleted
4871 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4872 int any_added
4873 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4874
4875 if (!is_catchpoint)
4876 {
4877 if (any_added || any_deleted)
4878 current_uiout->text (_("Stopped due to shared library event:\n"));
4879 else
4880 current_uiout->text (_("Stopped due to shared library event (no "
4881 "libraries added or removed)\n"));
4882 }
4883
4884 if (current_uiout->is_mi_like_p ())
4885 current_uiout->field_string ("reason",
4886 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4887
4888 if (any_deleted)
4889 {
4890 char *name;
4891 int ix;
4892
4893 current_uiout->text (_(" Inferior unloaded "));
4894 ui_out_emit_list list_emitter (current_uiout, "removed");
4895 for (ix = 0;
4896 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4897 ix, name);
4898 ++ix)
4899 {
4900 if (ix > 0)
4901 current_uiout->text (" ");
4902 current_uiout->field_string ("library", name);
4903 current_uiout->text ("\n");
4904 }
4905 }
4906
4907 if (any_added)
4908 {
4909 struct so_list *iter;
4910 int ix;
4911
4912 current_uiout->text (_(" Inferior loaded "));
4913 ui_out_emit_list list_emitter (current_uiout, "added");
4914 for (ix = 0;
4915 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4916 ix, iter);
4917 ++ix)
4918 {
4919 if (ix > 0)
4920 current_uiout->text (" ");
4921 current_uiout->field_string ("library", iter->so_name);
4922 current_uiout->text ("\n");
4923 }
4924 }
4925 }
4926
4927 /* Print a message indicating what happened. This is called from
4928 normal_stop(). The input to this routine is the head of the bpstat
4929 list - a list of the eventpoints that caused this stop. KIND is
4930 the target_waitkind for the stopping event. This
4931 routine calls the generic print routine for printing a message
4932 about reasons for stopping. This will print (for example) the
4933 "Breakpoint n," part of the output. The return value of this
4934 routine is one of:
4935
4936 PRINT_UNKNOWN: Means we printed nothing.
4937 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4938 code to print the location. An example is
4939 "Breakpoint 1, " which should be followed by
4940 the location.
4941 PRINT_SRC_ONLY: Means we printed something, but there is no need
4942 to also print the location part of the message.
4943 An example is the catch/throw messages, which
4944 don't require a location appended to the end.
4945 PRINT_NOTHING: We have done some printing and we don't need any
4946 further info to be printed. */
4947
4948 enum print_stop_action
4949 bpstat_print (bpstat bs, int kind)
4950 {
4951 enum print_stop_action val;
4952
4953 /* Maybe another breakpoint in the chain caused us to stop.
4954 (Currently all watchpoints go on the bpstat whether hit or not.
4955 That probably could (should) be changed, provided care is taken
4956 with respect to bpstat_explains_signal). */
4957 for (; bs; bs = bs->next)
4958 {
4959 val = print_bp_stop_message (bs);
4960 if (val == PRINT_SRC_ONLY
4961 || val == PRINT_SRC_AND_LOC
4962 || val == PRINT_NOTHING)
4963 return val;
4964 }
4965
4966 /* If we had hit a shared library event breakpoint,
4967 print_bp_stop_message would print out this message. If we hit an
4968 OS-level shared library event, do the same thing. */
4969 if (kind == TARGET_WAITKIND_LOADED)
4970 {
4971 print_solib_event (0);
4972 return PRINT_NOTHING;
4973 }
4974
4975 /* We reached the end of the chain, or we got a null BS to start
4976 with and nothing was printed. */
4977 return PRINT_UNKNOWN;
4978 }
4979
4980 /* Evaluate the expression EXP and return 1 if value is zero.
4981 This returns the inverse of the condition because it is called
4982 from catch_errors which returns 0 if an exception happened, and if an
4983 exception happens we want execution to stop.
4984 The argument is a "struct expression *" that has been cast to a
4985 "void *" to make it pass through catch_errors. */
4986
4987 static int
4988 breakpoint_cond_eval (void *exp)
4989 {
4990 struct value *mark = value_mark ();
4991 int i = !value_true (evaluate_expression ((struct expression *) exp));
4992
4993 value_free_to_mark (mark);
4994 return i;
4995 }
4996
4997 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4998
4999 static bpstat
5000 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5001 {
5002 bpstat bs;
5003
5004 bs = (bpstat) xmalloc (sizeof (*bs));
5005 bs->next = NULL;
5006 **bs_link_pointer = bs;
5007 *bs_link_pointer = &bs->next;
5008 bs->breakpoint_at = bl->owner;
5009 bs->bp_location_at = bl;
5010 incref_bp_location (bl);
5011 /* If the condition is false, etc., don't do the commands. */
5012 bs->commands = NULL;
5013 bs->old_val = NULL;
5014 bs->print_it = print_it_normal;
5015 return bs;
5016 }
5017 \f
5018 /* The target has stopped with waitstatus WS. Check if any hardware
5019 watchpoints have triggered, according to the target. */
5020
5021 int
5022 watchpoints_triggered (struct target_waitstatus *ws)
5023 {
5024 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5025 CORE_ADDR addr;
5026 struct breakpoint *b;
5027
5028 if (!stopped_by_watchpoint)
5029 {
5030 /* We were not stopped by a watchpoint. Mark all watchpoints
5031 as not triggered. */
5032 ALL_BREAKPOINTS (b)
5033 if (is_hardware_watchpoint (b))
5034 {
5035 struct watchpoint *w = (struct watchpoint *) b;
5036
5037 w->watchpoint_triggered = watch_triggered_no;
5038 }
5039
5040 return 0;
5041 }
5042
5043 if (!target_stopped_data_address (&current_target, &addr))
5044 {
5045 /* We were stopped by a watchpoint, but we don't know where.
5046 Mark all watchpoints as unknown. */
5047 ALL_BREAKPOINTS (b)
5048 if (is_hardware_watchpoint (b))
5049 {
5050 struct watchpoint *w = (struct watchpoint *) b;
5051
5052 w->watchpoint_triggered = watch_triggered_unknown;
5053 }
5054
5055 return 1;
5056 }
5057
5058 /* The target could report the data address. Mark watchpoints
5059 affected by this data address as triggered, and all others as not
5060 triggered. */
5061
5062 ALL_BREAKPOINTS (b)
5063 if (is_hardware_watchpoint (b))
5064 {
5065 struct watchpoint *w = (struct watchpoint *) b;
5066 struct bp_location *loc;
5067
5068 w->watchpoint_triggered = watch_triggered_no;
5069 for (loc = b->loc; loc; loc = loc->next)
5070 {
5071 if (is_masked_watchpoint (b))
5072 {
5073 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5074 CORE_ADDR start = loc->address & w->hw_wp_mask;
5075
5076 if (newaddr == start)
5077 {
5078 w->watchpoint_triggered = watch_triggered_yes;
5079 break;
5080 }
5081 }
5082 /* Exact match not required. Within range is sufficient. */
5083 else if (target_watchpoint_addr_within_range (&current_target,
5084 addr, loc->address,
5085 loc->length))
5086 {
5087 w->watchpoint_triggered = watch_triggered_yes;
5088 break;
5089 }
5090 }
5091 }
5092
5093 return 1;
5094 }
5095
5096 /* Possible return values for watchpoint_check (this can't be an enum
5097 because of check_errors). */
5098 /* The watchpoint has been deleted. */
5099 #define WP_DELETED 1
5100 /* The value has changed. */
5101 #define WP_VALUE_CHANGED 2
5102 /* The value has not changed. */
5103 #define WP_VALUE_NOT_CHANGED 3
5104 /* Ignore this watchpoint, no matter if the value changed or not. */
5105 #define WP_IGNORE 4
5106
5107 #define BP_TEMPFLAG 1
5108 #define BP_HARDWAREFLAG 2
5109
5110 /* Evaluate watchpoint condition expression and check if its value
5111 changed.
5112
5113 P should be a pointer to struct bpstat, but is defined as a void *
5114 in order for this function to be usable with catch_errors. */
5115
5116 static int
5117 watchpoint_check (void *p)
5118 {
5119 bpstat bs = (bpstat) p;
5120 struct watchpoint *b;
5121 struct frame_info *fr;
5122 int within_current_scope;
5123
5124 /* BS is built from an existing struct breakpoint. */
5125 gdb_assert (bs->breakpoint_at != NULL);
5126 b = (struct watchpoint *) bs->breakpoint_at;
5127
5128 /* If this is a local watchpoint, we only want to check if the
5129 watchpoint frame is in scope if the current thread is the thread
5130 that was used to create the watchpoint. */
5131 if (!watchpoint_in_thread_scope (b))
5132 return WP_IGNORE;
5133
5134 if (b->exp_valid_block == NULL)
5135 within_current_scope = 1;
5136 else
5137 {
5138 struct frame_info *frame = get_current_frame ();
5139 struct gdbarch *frame_arch = get_frame_arch (frame);
5140 CORE_ADDR frame_pc = get_frame_pc (frame);
5141
5142 /* stack_frame_destroyed_p() returns a non-zero value if we're
5143 still in the function but the stack frame has already been
5144 invalidated. Since we can't rely on the values of local
5145 variables after the stack has been destroyed, we are treating
5146 the watchpoint in that state as `not changed' without further
5147 checking. Don't mark watchpoints as changed if the current
5148 frame is in an epilogue - even if they are in some other
5149 frame, our view of the stack is likely to be wrong and
5150 frame_find_by_id could error out. */
5151 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5152 return WP_IGNORE;
5153
5154 fr = frame_find_by_id (b->watchpoint_frame);
5155 within_current_scope = (fr != NULL);
5156
5157 /* If we've gotten confused in the unwinder, we might have
5158 returned a frame that can't describe this variable. */
5159 if (within_current_scope)
5160 {
5161 struct symbol *function;
5162
5163 function = get_frame_function (fr);
5164 if (function == NULL
5165 || !contained_in (b->exp_valid_block,
5166 SYMBOL_BLOCK_VALUE (function)))
5167 within_current_scope = 0;
5168 }
5169
5170 if (within_current_scope)
5171 /* If we end up stopping, the current frame will get selected
5172 in normal_stop. So this call to select_frame won't affect
5173 the user. */
5174 select_frame (fr);
5175 }
5176
5177 if (within_current_scope)
5178 {
5179 /* We use value_{,free_to_}mark because it could be a *long*
5180 time before we return to the command level and call
5181 free_all_values. We can't call free_all_values because we
5182 might be in the middle of evaluating a function call. */
5183
5184 int pc = 0;
5185 struct value *mark;
5186 struct value *new_val;
5187
5188 if (is_masked_watchpoint (b))
5189 /* Since we don't know the exact trigger address (from
5190 stopped_data_address), just tell the user we've triggered
5191 a mask watchpoint. */
5192 return WP_VALUE_CHANGED;
5193
5194 mark = value_mark ();
5195 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5196
5197 if (b->val_bitsize != 0)
5198 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5199
5200 /* We use value_equal_contents instead of value_equal because
5201 the latter coerces an array to a pointer, thus comparing just
5202 the address of the array instead of its contents. This is
5203 not what we want. */
5204 if ((b->val != NULL) != (new_val != NULL)
5205 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5206 {
5207 if (new_val != NULL)
5208 {
5209 release_value (new_val);
5210 value_free_to_mark (mark);
5211 }
5212 bs->old_val = b->val;
5213 b->val = new_val;
5214 b->val_valid = 1;
5215 return WP_VALUE_CHANGED;
5216 }
5217 else
5218 {
5219 /* Nothing changed. */
5220 value_free_to_mark (mark);
5221 return WP_VALUE_NOT_CHANGED;
5222 }
5223 }
5224 else
5225 {
5226 /* This seems like the only logical thing to do because
5227 if we temporarily ignored the watchpoint, then when
5228 we reenter the block in which it is valid it contains
5229 garbage (in the case of a function, it may have two
5230 garbage values, one before and one after the prologue).
5231 So we can't even detect the first assignment to it and
5232 watch after that (since the garbage may or may not equal
5233 the first value assigned). */
5234 /* We print all the stop information in
5235 breakpoint_ops->print_it, but in this case, by the time we
5236 call breakpoint_ops->print_it this bp will be deleted
5237 already. So we have no choice but print the information
5238 here. */
5239
5240 SWITCH_THRU_ALL_UIS ()
5241 {
5242 struct ui_out *uiout = current_uiout;
5243
5244 if (uiout->is_mi_like_p ())
5245 uiout->field_string
5246 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5247 uiout->text ("\nWatchpoint ");
5248 uiout->field_int ("wpnum", b->number);
5249 uiout->text (" deleted because the program has left the block in\n"
5250 "which its expression is valid.\n");
5251 }
5252
5253 /* Make sure the watchpoint's commands aren't executed. */
5254 decref_counted_command_line (&b->commands);
5255 watchpoint_del_at_next_stop (b);
5256
5257 return WP_DELETED;
5258 }
5259 }
5260
5261 /* Return true if it looks like target has stopped due to hitting
5262 breakpoint location BL. This function does not check if we should
5263 stop, only if BL explains the stop. */
5264
5265 static int
5266 bpstat_check_location (const struct bp_location *bl,
5267 struct address_space *aspace, CORE_ADDR bp_addr,
5268 const struct target_waitstatus *ws)
5269 {
5270 struct breakpoint *b = bl->owner;
5271
5272 /* BL is from an existing breakpoint. */
5273 gdb_assert (b != NULL);
5274
5275 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5276 }
5277
5278 /* Determine if the watched values have actually changed, and we
5279 should stop. If not, set BS->stop to 0. */
5280
5281 static void
5282 bpstat_check_watchpoint (bpstat bs)
5283 {
5284 const struct bp_location *bl;
5285 struct watchpoint *b;
5286
5287 /* BS is built for existing struct breakpoint. */
5288 bl = bs->bp_location_at;
5289 gdb_assert (bl != NULL);
5290 b = (struct watchpoint *) bs->breakpoint_at;
5291 gdb_assert (b != NULL);
5292
5293 {
5294 int must_check_value = 0;
5295
5296 if (b->type == bp_watchpoint)
5297 /* For a software watchpoint, we must always check the
5298 watched value. */
5299 must_check_value = 1;
5300 else if (b->watchpoint_triggered == watch_triggered_yes)
5301 /* We have a hardware watchpoint (read, write, or access)
5302 and the target earlier reported an address watched by
5303 this watchpoint. */
5304 must_check_value = 1;
5305 else if (b->watchpoint_triggered == watch_triggered_unknown
5306 && b->type == bp_hardware_watchpoint)
5307 /* We were stopped by a hardware watchpoint, but the target could
5308 not report the data address. We must check the watchpoint's
5309 value. Access and read watchpoints are out of luck; without
5310 a data address, we can't figure it out. */
5311 must_check_value = 1;
5312
5313 if (must_check_value)
5314 {
5315 char *message
5316 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5317 b->number);
5318 struct cleanup *cleanups = make_cleanup (xfree, message);
5319 int e = catch_errors (watchpoint_check, bs, message,
5320 RETURN_MASK_ALL);
5321 do_cleanups (cleanups);
5322 switch (e)
5323 {
5324 case WP_DELETED:
5325 /* We've already printed what needs to be printed. */
5326 bs->print_it = print_it_done;
5327 /* Stop. */
5328 break;
5329 case WP_IGNORE:
5330 bs->print_it = print_it_noop;
5331 bs->stop = 0;
5332 break;
5333 case WP_VALUE_CHANGED:
5334 if (b->type == bp_read_watchpoint)
5335 {
5336 /* There are two cases to consider here:
5337
5338 1. We're watching the triggered memory for reads.
5339 In that case, trust the target, and always report
5340 the watchpoint hit to the user. Even though
5341 reads don't cause value changes, the value may
5342 have changed since the last time it was read, and
5343 since we're not trapping writes, we will not see
5344 those, and as such we should ignore our notion of
5345 old value.
5346
5347 2. We're watching the triggered memory for both
5348 reads and writes. There are two ways this may
5349 happen:
5350
5351 2.1. This is a target that can't break on data
5352 reads only, but can break on accesses (reads or
5353 writes), such as e.g., x86. We detect this case
5354 at the time we try to insert read watchpoints.
5355
5356 2.2. Otherwise, the target supports read
5357 watchpoints, but, the user set an access or write
5358 watchpoint watching the same memory as this read
5359 watchpoint.
5360
5361 If we're watching memory writes as well as reads,
5362 ignore watchpoint hits when we find that the
5363 value hasn't changed, as reads don't cause
5364 changes. This still gives false positives when
5365 the program writes the same value to memory as
5366 what there was already in memory (we will confuse
5367 it for a read), but it's much better than
5368 nothing. */
5369
5370 int other_write_watchpoint = 0;
5371
5372 if (bl->watchpoint_type == hw_read)
5373 {
5374 struct breakpoint *other_b;
5375
5376 ALL_BREAKPOINTS (other_b)
5377 if (other_b->type == bp_hardware_watchpoint
5378 || other_b->type == bp_access_watchpoint)
5379 {
5380 struct watchpoint *other_w =
5381 (struct watchpoint *) other_b;
5382
5383 if (other_w->watchpoint_triggered
5384 == watch_triggered_yes)
5385 {
5386 other_write_watchpoint = 1;
5387 break;
5388 }
5389 }
5390 }
5391
5392 if (other_write_watchpoint
5393 || bl->watchpoint_type == hw_access)
5394 {
5395 /* We're watching the same memory for writes,
5396 and the value changed since the last time we
5397 updated it, so this trap must be for a write.
5398 Ignore it. */
5399 bs->print_it = print_it_noop;
5400 bs->stop = 0;
5401 }
5402 }
5403 break;
5404 case WP_VALUE_NOT_CHANGED:
5405 if (b->type == bp_hardware_watchpoint
5406 || b->type == bp_watchpoint)
5407 {
5408 /* Don't stop: write watchpoints shouldn't fire if
5409 the value hasn't changed. */
5410 bs->print_it = print_it_noop;
5411 bs->stop = 0;
5412 }
5413 /* Stop. */
5414 break;
5415 default:
5416 /* Can't happen. */
5417 case 0:
5418 /* Error from catch_errors. */
5419 {
5420 SWITCH_THRU_ALL_UIS ()
5421 {
5422 printf_filtered (_("Watchpoint %d deleted.\n"),
5423 b->number);
5424 }
5425 watchpoint_del_at_next_stop (b);
5426 /* We've already printed what needs to be printed. */
5427 bs->print_it = print_it_done;
5428 }
5429 break;
5430 }
5431 }
5432 else /* must_check_value == 0 */
5433 {
5434 /* This is a case where some watchpoint(s) triggered, but
5435 not at the address of this watchpoint, or else no
5436 watchpoint triggered after all. So don't print
5437 anything for this watchpoint. */
5438 bs->print_it = print_it_noop;
5439 bs->stop = 0;
5440 }
5441 }
5442 }
5443
5444 /* For breakpoints that are currently marked as telling gdb to stop,
5445 check conditions (condition proper, frame, thread and ignore count)
5446 of breakpoint referred to by BS. If we should not stop for this
5447 breakpoint, set BS->stop to 0. */
5448
5449 static void
5450 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5451 {
5452 const struct bp_location *bl;
5453 struct breakpoint *b;
5454 int value_is_zero = 0;
5455 struct expression *cond;
5456
5457 gdb_assert (bs->stop);
5458
5459 /* BS is built for existing struct breakpoint. */
5460 bl = bs->bp_location_at;
5461 gdb_assert (bl != NULL);
5462 b = bs->breakpoint_at;
5463 gdb_assert (b != NULL);
5464
5465 /* Even if the target evaluated the condition on its end and notified GDB, we
5466 need to do so again since GDB does not know if we stopped due to a
5467 breakpoint or a single step breakpoint. */
5468
5469 if (frame_id_p (b->frame_id)
5470 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5471 {
5472 bs->stop = 0;
5473 return;
5474 }
5475
5476 /* If this is a thread/task-specific breakpoint, don't waste cpu
5477 evaluating the condition if this isn't the specified
5478 thread/task. */
5479 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5480 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5481
5482 {
5483 bs->stop = 0;
5484 return;
5485 }
5486
5487 /* Evaluate extension language breakpoints that have a "stop" method
5488 implemented. */
5489 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5490
5491 if (is_watchpoint (b))
5492 {
5493 struct watchpoint *w = (struct watchpoint *) b;
5494
5495 cond = w->cond_exp.get ();
5496 }
5497 else
5498 cond = bl->cond.get ();
5499
5500 if (cond && b->disposition != disp_del_at_next_stop)
5501 {
5502 int within_current_scope = 1;
5503 struct watchpoint * w;
5504
5505 /* We use value_mark and value_free_to_mark because it could
5506 be a long time before we return to the command level and
5507 call free_all_values. We can't call free_all_values
5508 because we might be in the middle of evaluating a
5509 function call. */
5510 struct value *mark = value_mark ();
5511
5512 if (is_watchpoint (b))
5513 w = (struct watchpoint *) b;
5514 else
5515 w = NULL;
5516
5517 /* Need to select the frame, with all that implies so that
5518 the conditions will have the right context. Because we
5519 use the frame, we will not see an inlined function's
5520 variables when we arrive at a breakpoint at the start
5521 of the inlined function; the current frame will be the
5522 call site. */
5523 if (w == NULL || w->cond_exp_valid_block == NULL)
5524 select_frame (get_current_frame ());
5525 else
5526 {
5527 struct frame_info *frame;
5528
5529 /* For local watchpoint expressions, which particular
5530 instance of a local is being watched matters, so we
5531 keep track of the frame to evaluate the expression
5532 in. To evaluate the condition however, it doesn't
5533 really matter which instantiation of the function
5534 where the condition makes sense triggers the
5535 watchpoint. This allows an expression like "watch
5536 global if q > 10" set in `func', catch writes to
5537 global on all threads that call `func', or catch
5538 writes on all recursive calls of `func' by a single
5539 thread. We simply always evaluate the condition in
5540 the innermost frame that's executing where it makes
5541 sense to evaluate the condition. It seems
5542 intuitive. */
5543 frame = block_innermost_frame (w->cond_exp_valid_block);
5544 if (frame != NULL)
5545 select_frame (frame);
5546 else
5547 within_current_scope = 0;
5548 }
5549 if (within_current_scope)
5550 value_is_zero
5551 = catch_errors (breakpoint_cond_eval, cond,
5552 "Error in testing breakpoint condition:\n",
5553 RETURN_MASK_ALL);
5554 else
5555 {
5556 warning (_("Watchpoint condition cannot be tested "
5557 "in the current scope"));
5558 /* If we failed to set the right context for this
5559 watchpoint, unconditionally report it. */
5560 value_is_zero = 0;
5561 }
5562 /* FIXME-someday, should give breakpoint #. */
5563 value_free_to_mark (mark);
5564 }
5565
5566 if (cond && value_is_zero)
5567 {
5568 bs->stop = 0;
5569 }
5570 else if (b->ignore_count > 0)
5571 {
5572 b->ignore_count--;
5573 bs->stop = 0;
5574 /* Increase the hit count even though we don't stop. */
5575 ++(b->hit_count);
5576 observer_notify_breakpoint_modified (b);
5577 }
5578 }
5579
5580 /* Returns true if we need to track moribund locations of LOC's type
5581 on the current target. */
5582
5583 static int
5584 need_moribund_for_location_type (struct bp_location *loc)
5585 {
5586 return ((loc->loc_type == bp_loc_software_breakpoint
5587 && !target_supports_stopped_by_sw_breakpoint ())
5588 || (loc->loc_type == bp_loc_hardware_breakpoint
5589 && !target_supports_stopped_by_hw_breakpoint ()));
5590 }
5591
5592
5593 /* Get a bpstat associated with having just stopped at address
5594 BP_ADDR in thread PTID.
5595
5596 Determine whether we stopped at a breakpoint, etc, or whether we
5597 don't understand this stop. Result is a chain of bpstat's such
5598 that:
5599
5600 if we don't understand the stop, the result is a null pointer.
5601
5602 if we understand why we stopped, the result is not null.
5603
5604 Each element of the chain refers to a particular breakpoint or
5605 watchpoint at which we have stopped. (We may have stopped for
5606 several reasons concurrently.)
5607
5608 Each element of the chain has valid next, breakpoint_at,
5609 commands, FIXME??? fields. */
5610
5611 bpstat
5612 bpstat_stop_status (struct address_space *aspace,
5613 CORE_ADDR bp_addr, ptid_t ptid,
5614 const struct target_waitstatus *ws)
5615 {
5616 struct breakpoint *b = NULL;
5617 struct bp_location *bl;
5618 struct bp_location *loc;
5619 /* First item of allocated bpstat's. */
5620 bpstat bs_head = NULL, *bs_link = &bs_head;
5621 /* Pointer to the last thing in the chain currently. */
5622 bpstat bs;
5623 int ix;
5624 int need_remove_insert;
5625 int removed_any;
5626
5627 /* First, build the bpstat chain with locations that explain a
5628 target stop, while being careful to not set the target running,
5629 as that may invalidate locations (in particular watchpoint
5630 locations are recreated). Resuming will happen here with
5631 breakpoint conditions or watchpoint expressions that include
5632 inferior function calls. */
5633
5634 ALL_BREAKPOINTS (b)
5635 {
5636 if (!breakpoint_enabled (b))
5637 continue;
5638
5639 for (bl = b->loc; bl != NULL; bl = bl->next)
5640 {
5641 /* For hardware watchpoints, we look only at the first
5642 location. The watchpoint_check function will work on the
5643 entire expression, not the individual locations. For
5644 read watchpoints, the watchpoints_triggered function has
5645 checked all locations already. */
5646 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5647 break;
5648
5649 if (!bl->enabled || bl->shlib_disabled)
5650 continue;
5651
5652 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5653 continue;
5654
5655 /* Come here if it's a watchpoint, or if the break address
5656 matches. */
5657
5658 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5659 explain stop. */
5660
5661 /* Assume we stop. Should we find a watchpoint that is not
5662 actually triggered, or if the condition of the breakpoint
5663 evaluates as false, we'll reset 'stop' to 0. */
5664 bs->stop = 1;
5665 bs->print = 1;
5666
5667 /* If this is a scope breakpoint, mark the associated
5668 watchpoint as triggered so that we will handle the
5669 out-of-scope event. We'll get to the watchpoint next
5670 iteration. */
5671 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5672 {
5673 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5674
5675 w->watchpoint_triggered = watch_triggered_yes;
5676 }
5677 }
5678 }
5679
5680 /* Check if a moribund breakpoint explains the stop. */
5681 if (!target_supports_stopped_by_sw_breakpoint ()
5682 || !target_supports_stopped_by_hw_breakpoint ())
5683 {
5684 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5685 {
5686 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5687 && need_moribund_for_location_type (loc))
5688 {
5689 bs = bpstat_alloc (loc, &bs_link);
5690 /* For hits of moribund locations, we should just proceed. */
5691 bs->stop = 0;
5692 bs->print = 0;
5693 bs->print_it = print_it_noop;
5694 }
5695 }
5696 }
5697
5698 /* A bit of special processing for shlib breakpoints. We need to
5699 process solib loading here, so that the lists of loaded and
5700 unloaded libraries are correct before we handle "catch load" and
5701 "catch unload". */
5702 for (bs = bs_head; bs != NULL; bs = bs->next)
5703 {
5704 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5705 {
5706 handle_solib_event ();
5707 break;
5708 }
5709 }
5710
5711 /* Now go through the locations that caused the target to stop, and
5712 check whether we're interested in reporting this stop to higher
5713 layers, or whether we should resume the target transparently. */
5714
5715 removed_any = 0;
5716
5717 for (bs = bs_head; bs != NULL; bs = bs->next)
5718 {
5719 if (!bs->stop)
5720 continue;
5721
5722 b = bs->breakpoint_at;
5723 b->ops->check_status (bs);
5724 if (bs->stop)
5725 {
5726 bpstat_check_breakpoint_conditions (bs, ptid);
5727
5728 if (bs->stop)
5729 {
5730 ++(b->hit_count);
5731 observer_notify_breakpoint_modified (b);
5732
5733 /* We will stop here. */
5734 if (b->disposition == disp_disable)
5735 {
5736 --(b->enable_count);
5737 if (b->enable_count <= 0)
5738 b->enable_state = bp_disabled;
5739 removed_any = 1;
5740 }
5741 if (b->silent)
5742 bs->print = 0;
5743 bs->commands = b->commands;
5744 incref_counted_command_line (bs->commands);
5745 if (command_line_is_silent (bs->commands
5746 ? bs->commands->commands : NULL))
5747 bs->print = 0;
5748
5749 b->ops->after_condition_true (bs);
5750 }
5751
5752 }
5753
5754 /* Print nothing for this entry if we don't stop or don't
5755 print. */
5756 if (!bs->stop || !bs->print)
5757 bs->print_it = print_it_noop;
5758 }
5759
5760 /* If we aren't stopping, the value of some hardware watchpoint may
5761 not have changed, but the intermediate memory locations we are
5762 watching may have. Don't bother if we're stopping; this will get
5763 done later. */
5764 need_remove_insert = 0;
5765 if (! bpstat_causes_stop (bs_head))
5766 for (bs = bs_head; bs != NULL; bs = bs->next)
5767 if (!bs->stop
5768 && bs->breakpoint_at
5769 && is_hardware_watchpoint (bs->breakpoint_at))
5770 {
5771 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5772
5773 update_watchpoint (w, 0 /* don't reparse. */);
5774 need_remove_insert = 1;
5775 }
5776
5777 if (need_remove_insert)
5778 update_global_location_list (UGLL_MAY_INSERT);
5779 else if (removed_any)
5780 update_global_location_list (UGLL_DONT_INSERT);
5781
5782 return bs_head;
5783 }
5784
5785 static void
5786 handle_jit_event (void)
5787 {
5788 struct frame_info *frame;
5789 struct gdbarch *gdbarch;
5790
5791 if (debug_infrun)
5792 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5793
5794 /* Switch terminal for any messages produced by
5795 breakpoint_re_set. */
5796 target_terminal_ours_for_output ();
5797
5798 frame = get_current_frame ();
5799 gdbarch = get_frame_arch (frame);
5800
5801 jit_event_handler (gdbarch);
5802
5803 target_terminal_inferior ();
5804 }
5805
5806 /* Prepare WHAT final decision for infrun. */
5807
5808 /* Decide what infrun needs to do with this bpstat. */
5809
5810 struct bpstat_what
5811 bpstat_what (bpstat bs_head)
5812 {
5813 struct bpstat_what retval;
5814 bpstat bs;
5815
5816 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5817 retval.call_dummy = STOP_NONE;
5818 retval.is_longjmp = 0;
5819
5820 for (bs = bs_head; bs != NULL; bs = bs->next)
5821 {
5822 /* Extract this BS's action. After processing each BS, we check
5823 if its action overrides all we've seem so far. */
5824 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5825 enum bptype bptype;
5826
5827 if (bs->breakpoint_at == NULL)
5828 {
5829 /* I suspect this can happen if it was a momentary
5830 breakpoint which has since been deleted. */
5831 bptype = bp_none;
5832 }
5833 else
5834 bptype = bs->breakpoint_at->type;
5835
5836 switch (bptype)
5837 {
5838 case bp_none:
5839 break;
5840 case bp_breakpoint:
5841 case bp_hardware_breakpoint:
5842 case bp_single_step:
5843 case bp_until:
5844 case bp_finish:
5845 case bp_shlib_event:
5846 if (bs->stop)
5847 {
5848 if (bs->print)
5849 this_action = BPSTAT_WHAT_STOP_NOISY;
5850 else
5851 this_action = BPSTAT_WHAT_STOP_SILENT;
5852 }
5853 else
5854 this_action = BPSTAT_WHAT_SINGLE;
5855 break;
5856 case bp_watchpoint:
5857 case bp_hardware_watchpoint:
5858 case bp_read_watchpoint:
5859 case bp_access_watchpoint:
5860 if (bs->stop)
5861 {
5862 if (bs->print)
5863 this_action = BPSTAT_WHAT_STOP_NOISY;
5864 else
5865 this_action = BPSTAT_WHAT_STOP_SILENT;
5866 }
5867 else
5868 {
5869 /* There was a watchpoint, but we're not stopping.
5870 This requires no further action. */
5871 }
5872 break;
5873 case bp_longjmp:
5874 case bp_longjmp_call_dummy:
5875 case bp_exception:
5876 if (bs->stop)
5877 {
5878 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5879 retval.is_longjmp = bptype != bp_exception;
5880 }
5881 else
5882 this_action = BPSTAT_WHAT_SINGLE;
5883 break;
5884 case bp_longjmp_resume:
5885 case bp_exception_resume:
5886 if (bs->stop)
5887 {
5888 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5889 retval.is_longjmp = bptype == bp_longjmp_resume;
5890 }
5891 else
5892 this_action = BPSTAT_WHAT_SINGLE;
5893 break;
5894 case bp_step_resume:
5895 if (bs->stop)
5896 this_action = BPSTAT_WHAT_STEP_RESUME;
5897 else
5898 {
5899 /* It is for the wrong frame. */
5900 this_action = BPSTAT_WHAT_SINGLE;
5901 }
5902 break;
5903 case bp_hp_step_resume:
5904 if (bs->stop)
5905 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5906 else
5907 {
5908 /* It is for the wrong frame. */
5909 this_action = BPSTAT_WHAT_SINGLE;
5910 }
5911 break;
5912 case bp_watchpoint_scope:
5913 case bp_thread_event:
5914 case bp_overlay_event:
5915 case bp_longjmp_master:
5916 case bp_std_terminate_master:
5917 case bp_exception_master:
5918 this_action = BPSTAT_WHAT_SINGLE;
5919 break;
5920 case bp_catchpoint:
5921 if (bs->stop)
5922 {
5923 if (bs->print)
5924 this_action = BPSTAT_WHAT_STOP_NOISY;
5925 else
5926 this_action = BPSTAT_WHAT_STOP_SILENT;
5927 }
5928 else
5929 {
5930 /* There was a catchpoint, but we're not stopping.
5931 This requires no further action. */
5932 }
5933 break;
5934 case bp_jit_event:
5935 this_action = BPSTAT_WHAT_SINGLE;
5936 break;
5937 case bp_call_dummy:
5938 /* Make sure the action is stop (silent or noisy),
5939 so infrun.c pops the dummy frame. */
5940 retval.call_dummy = STOP_STACK_DUMMY;
5941 this_action = BPSTAT_WHAT_STOP_SILENT;
5942 break;
5943 case bp_std_terminate:
5944 /* Make sure the action is stop (silent or noisy),
5945 so infrun.c pops the dummy frame. */
5946 retval.call_dummy = STOP_STD_TERMINATE;
5947 this_action = BPSTAT_WHAT_STOP_SILENT;
5948 break;
5949 case bp_tracepoint:
5950 case bp_fast_tracepoint:
5951 case bp_static_tracepoint:
5952 /* Tracepoint hits should not be reported back to GDB, and
5953 if one got through somehow, it should have been filtered
5954 out already. */
5955 internal_error (__FILE__, __LINE__,
5956 _("bpstat_what: tracepoint encountered"));
5957 break;
5958 case bp_gnu_ifunc_resolver:
5959 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5960 this_action = BPSTAT_WHAT_SINGLE;
5961 break;
5962 case bp_gnu_ifunc_resolver_return:
5963 /* The breakpoint will be removed, execution will restart from the
5964 PC of the former breakpoint. */
5965 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5966 break;
5967
5968 case bp_dprintf:
5969 if (bs->stop)
5970 this_action = BPSTAT_WHAT_STOP_SILENT;
5971 else
5972 this_action = BPSTAT_WHAT_SINGLE;
5973 break;
5974
5975 default:
5976 internal_error (__FILE__, __LINE__,
5977 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5978 }
5979
5980 retval.main_action = std::max (retval.main_action, this_action);
5981 }
5982
5983 return retval;
5984 }
5985
5986 void
5987 bpstat_run_callbacks (bpstat bs_head)
5988 {
5989 bpstat bs;
5990
5991 for (bs = bs_head; bs != NULL; bs = bs->next)
5992 {
5993 struct breakpoint *b = bs->breakpoint_at;
5994
5995 if (b == NULL)
5996 continue;
5997 switch (b->type)
5998 {
5999 case bp_jit_event:
6000 handle_jit_event ();
6001 break;
6002 case bp_gnu_ifunc_resolver:
6003 gnu_ifunc_resolver_stop (b);
6004 break;
6005 case bp_gnu_ifunc_resolver_return:
6006 gnu_ifunc_resolver_return_stop (b);
6007 break;
6008 }
6009 }
6010 }
6011
6012 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6013 without hardware support). This isn't related to a specific bpstat,
6014 just to things like whether watchpoints are set. */
6015
6016 int
6017 bpstat_should_step (void)
6018 {
6019 struct breakpoint *b;
6020
6021 ALL_BREAKPOINTS (b)
6022 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6023 return 1;
6024 return 0;
6025 }
6026
6027 int
6028 bpstat_causes_stop (bpstat bs)
6029 {
6030 for (; bs != NULL; bs = bs->next)
6031 if (bs->stop)
6032 return 1;
6033
6034 return 0;
6035 }
6036
6037 \f
6038
6039 /* Compute a string of spaces suitable to indent the next line
6040 so it starts at the position corresponding to the table column
6041 named COL_NAME in the currently active table of UIOUT. */
6042
6043 static char *
6044 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6045 {
6046 static char wrap_indent[80];
6047 int i, total_width, width, align;
6048 const char *text;
6049
6050 total_width = 0;
6051 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6052 {
6053 if (strcmp (text, col_name) == 0)
6054 {
6055 gdb_assert (total_width < sizeof wrap_indent);
6056 memset (wrap_indent, ' ', total_width);
6057 wrap_indent[total_width] = 0;
6058
6059 return wrap_indent;
6060 }
6061
6062 total_width += width + 1;
6063 }
6064
6065 return NULL;
6066 }
6067
6068 /* Determine if the locations of this breakpoint will have their conditions
6069 evaluated by the target, host or a mix of both. Returns the following:
6070
6071 "host": Host evals condition.
6072 "host or target": Host or Target evals condition.
6073 "target": Target evals condition.
6074 */
6075
6076 static const char *
6077 bp_condition_evaluator (struct breakpoint *b)
6078 {
6079 struct bp_location *bl;
6080 char host_evals = 0;
6081 char target_evals = 0;
6082
6083 if (!b)
6084 return NULL;
6085
6086 if (!is_breakpoint (b))
6087 return NULL;
6088
6089 if (gdb_evaluates_breakpoint_condition_p ()
6090 || !target_supports_evaluation_of_breakpoint_conditions ())
6091 return condition_evaluation_host;
6092
6093 for (bl = b->loc; bl; bl = bl->next)
6094 {
6095 if (bl->cond_bytecode)
6096 target_evals++;
6097 else
6098 host_evals++;
6099 }
6100
6101 if (host_evals && target_evals)
6102 return condition_evaluation_both;
6103 else if (target_evals)
6104 return condition_evaluation_target;
6105 else
6106 return condition_evaluation_host;
6107 }
6108
6109 /* Determine the breakpoint location's condition evaluator. This is
6110 similar to bp_condition_evaluator, but for locations. */
6111
6112 static const char *
6113 bp_location_condition_evaluator (struct bp_location *bl)
6114 {
6115 if (bl && !is_breakpoint (bl->owner))
6116 return NULL;
6117
6118 if (gdb_evaluates_breakpoint_condition_p ()
6119 || !target_supports_evaluation_of_breakpoint_conditions ())
6120 return condition_evaluation_host;
6121
6122 if (bl && bl->cond_bytecode)
6123 return condition_evaluation_target;
6124 else
6125 return condition_evaluation_host;
6126 }
6127
6128 /* Print the LOC location out of the list of B->LOC locations. */
6129
6130 static void
6131 print_breakpoint_location (struct breakpoint *b,
6132 struct bp_location *loc)
6133 {
6134 struct ui_out *uiout = current_uiout;
6135
6136 scoped_restore_current_program_space restore_pspace;
6137
6138 if (loc != NULL && loc->shlib_disabled)
6139 loc = NULL;
6140
6141 if (loc != NULL)
6142 set_current_program_space (loc->pspace);
6143
6144 if (b->display_canonical)
6145 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6146 else if (loc && loc->symtab)
6147 {
6148 struct symbol *sym
6149 = find_pc_sect_function (loc->address, loc->section);
6150 if (sym)
6151 {
6152 uiout->text ("in ");
6153 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6154 uiout->text (" ");
6155 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6156 uiout->text ("at ");
6157 }
6158 uiout->field_string ("file",
6159 symtab_to_filename_for_display (loc->symtab));
6160 uiout->text (":");
6161
6162 if (uiout->is_mi_like_p ())
6163 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6164
6165 uiout->field_int ("line", loc->line_number);
6166 }
6167 else if (loc)
6168 {
6169 string_file stb;
6170
6171 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6172 demangle, "");
6173 uiout->field_stream ("at", stb);
6174 }
6175 else
6176 {
6177 uiout->field_string ("pending",
6178 event_location_to_string (b->location.get ()));
6179 /* If extra_string is available, it could be holding a condition
6180 or dprintf arguments. In either case, make sure it is printed,
6181 too, but only for non-MI streams. */
6182 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6183 {
6184 if (b->type == bp_dprintf)
6185 uiout->text (",");
6186 else
6187 uiout->text (" ");
6188 uiout->text (b->extra_string);
6189 }
6190 }
6191
6192 if (loc && is_breakpoint (b)
6193 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6194 && bp_condition_evaluator (b) == condition_evaluation_both)
6195 {
6196 uiout->text (" (");
6197 uiout->field_string ("evaluated-by",
6198 bp_location_condition_evaluator (loc));
6199 uiout->text (")");
6200 }
6201 }
6202
6203 static const char *
6204 bptype_string (enum bptype type)
6205 {
6206 struct ep_type_description
6207 {
6208 enum bptype type;
6209 const char *description;
6210 };
6211 static struct ep_type_description bptypes[] =
6212 {
6213 {bp_none, "?deleted?"},
6214 {bp_breakpoint, "breakpoint"},
6215 {bp_hardware_breakpoint, "hw breakpoint"},
6216 {bp_single_step, "sw single-step"},
6217 {bp_until, "until"},
6218 {bp_finish, "finish"},
6219 {bp_watchpoint, "watchpoint"},
6220 {bp_hardware_watchpoint, "hw watchpoint"},
6221 {bp_read_watchpoint, "read watchpoint"},
6222 {bp_access_watchpoint, "acc watchpoint"},
6223 {bp_longjmp, "longjmp"},
6224 {bp_longjmp_resume, "longjmp resume"},
6225 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6226 {bp_exception, "exception"},
6227 {bp_exception_resume, "exception resume"},
6228 {bp_step_resume, "step resume"},
6229 {bp_hp_step_resume, "high-priority step resume"},
6230 {bp_watchpoint_scope, "watchpoint scope"},
6231 {bp_call_dummy, "call dummy"},
6232 {bp_std_terminate, "std::terminate"},
6233 {bp_shlib_event, "shlib events"},
6234 {bp_thread_event, "thread events"},
6235 {bp_overlay_event, "overlay events"},
6236 {bp_longjmp_master, "longjmp master"},
6237 {bp_std_terminate_master, "std::terminate master"},
6238 {bp_exception_master, "exception master"},
6239 {bp_catchpoint, "catchpoint"},
6240 {bp_tracepoint, "tracepoint"},
6241 {bp_fast_tracepoint, "fast tracepoint"},
6242 {bp_static_tracepoint, "static tracepoint"},
6243 {bp_dprintf, "dprintf"},
6244 {bp_jit_event, "jit events"},
6245 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6246 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6247 };
6248
6249 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6250 || ((int) type != bptypes[(int) type].type))
6251 internal_error (__FILE__, __LINE__,
6252 _("bptypes table does not describe type #%d."),
6253 (int) type);
6254
6255 return bptypes[(int) type].description;
6256 }
6257
6258 /* For MI, output a field named 'thread-groups' with a list as the value.
6259 For CLI, prefix the list with the string 'inf'. */
6260
6261 static void
6262 output_thread_groups (struct ui_out *uiout,
6263 const char *field_name,
6264 VEC(int) *inf_num,
6265 int mi_only)
6266 {
6267 int is_mi = uiout->is_mi_like_p ();
6268 int inf;
6269 int i;
6270
6271 /* For backward compatibility, don't display inferiors in CLI unless
6272 there are several. Always display them for MI. */
6273 if (!is_mi && mi_only)
6274 return;
6275
6276 ui_out_emit_list list_emitter (uiout, field_name);
6277
6278 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6279 {
6280 if (is_mi)
6281 {
6282 char mi_group[10];
6283
6284 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6285 uiout->field_string (NULL, mi_group);
6286 }
6287 else
6288 {
6289 if (i == 0)
6290 uiout->text (" inf ");
6291 else
6292 uiout->text (", ");
6293
6294 uiout->text (plongest (inf));
6295 }
6296 }
6297 }
6298
6299 /* Print B to gdb_stdout. */
6300
6301 static void
6302 print_one_breakpoint_location (struct breakpoint *b,
6303 struct bp_location *loc,
6304 int loc_number,
6305 struct bp_location **last_loc,
6306 int allflag)
6307 {
6308 struct command_line *l;
6309 static char bpenables[] = "nynny";
6310
6311 struct ui_out *uiout = current_uiout;
6312 int header_of_multiple = 0;
6313 int part_of_multiple = (loc != NULL);
6314 struct value_print_options opts;
6315
6316 get_user_print_options (&opts);
6317
6318 gdb_assert (!loc || loc_number != 0);
6319 /* See comment in print_one_breakpoint concerning treatment of
6320 breakpoints with single disabled location. */
6321 if (loc == NULL
6322 && (b->loc != NULL
6323 && (b->loc->next != NULL || !b->loc->enabled)))
6324 header_of_multiple = 1;
6325 if (loc == NULL)
6326 loc = b->loc;
6327
6328 annotate_record ();
6329
6330 /* 1 */
6331 annotate_field (0);
6332 if (part_of_multiple)
6333 {
6334 char *formatted;
6335 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6336 uiout->field_string ("number", formatted);
6337 xfree (formatted);
6338 }
6339 else
6340 {
6341 uiout->field_int ("number", b->number);
6342 }
6343
6344 /* 2 */
6345 annotate_field (1);
6346 if (part_of_multiple)
6347 uiout->field_skip ("type");
6348 else
6349 uiout->field_string ("type", bptype_string (b->type));
6350
6351 /* 3 */
6352 annotate_field (2);
6353 if (part_of_multiple)
6354 uiout->field_skip ("disp");
6355 else
6356 uiout->field_string ("disp", bpdisp_text (b->disposition));
6357
6358
6359 /* 4 */
6360 annotate_field (3);
6361 if (part_of_multiple)
6362 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6363 else
6364 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6365 uiout->spaces (2);
6366
6367
6368 /* 5 and 6 */
6369 if (b->ops != NULL && b->ops->print_one != NULL)
6370 {
6371 /* Although the print_one can possibly print all locations,
6372 calling it here is not likely to get any nice result. So,
6373 make sure there's just one location. */
6374 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6375 b->ops->print_one (b, last_loc);
6376 }
6377 else
6378 switch (b->type)
6379 {
6380 case bp_none:
6381 internal_error (__FILE__, __LINE__,
6382 _("print_one_breakpoint: bp_none encountered\n"));
6383 break;
6384
6385 case bp_watchpoint:
6386 case bp_hardware_watchpoint:
6387 case bp_read_watchpoint:
6388 case bp_access_watchpoint:
6389 {
6390 struct watchpoint *w = (struct watchpoint *) b;
6391
6392 /* Field 4, the address, is omitted (which makes the columns
6393 not line up too nicely with the headers, but the effect
6394 is relatively readable). */
6395 if (opts.addressprint)
6396 uiout->field_skip ("addr");
6397 annotate_field (5);
6398 uiout->field_string ("what", w->exp_string);
6399 }
6400 break;
6401
6402 case bp_breakpoint:
6403 case bp_hardware_breakpoint:
6404 case bp_single_step:
6405 case bp_until:
6406 case bp_finish:
6407 case bp_longjmp:
6408 case bp_longjmp_resume:
6409 case bp_longjmp_call_dummy:
6410 case bp_exception:
6411 case bp_exception_resume:
6412 case bp_step_resume:
6413 case bp_hp_step_resume:
6414 case bp_watchpoint_scope:
6415 case bp_call_dummy:
6416 case bp_std_terminate:
6417 case bp_shlib_event:
6418 case bp_thread_event:
6419 case bp_overlay_event:
6420 case bp_longjmp_master:
6421 case bp_std_terminate_master:
6422 case bp_exception_master:
6423 case bp_tracepoint:
6424 case bp_fast_tracepoint:
6425 case bp_static_tracepoint:
6426 case bp_dprintf:
6427 case bp_jit_event:
6428 case bp_gnu_ifunc_resolver:
6429 case bp_gnu_ifunc_resolver_return:
6430 if (opts.addressprint)
6431 {
6432 annotate_field (4);
6433 if (header_of_multiple)
6434 uiout->field_string ("addr", "<MULTIPLE>");
6435 else if (b->loc == NULL || loc->shlib_disabled)
6436 uiout->field_string ("addr", "<PENDING>");
6437 else
6438 uiout->field_core_addr ("addr",
6439 loc->gdbarch, loc->address);
6440 }
6441 annotate_field (5);
6442 if (!header_of_multiple)
6443 print_breakpoint_location (b, loc);
6444 if (b->loc)
6445 *last_loc = b->loc;
6446 break;
6447 }
6448
6449
6450 if (loc != NULL && !header_of_multiple)
6451 {
6452 struct inferior *inf;
6453 VEC(int) *inf_num = NULL;
6454 int mi_only = 1;
6455
6456 ALL_INFERIORS (inf)
6457 {
6458 if (inf->pspace == loc->pspace)
6459 VEC_safe_push (int, inf_num, inf->num);
6460 }
6461
6462 /* For backward compatibility, don't display inferiors in CLI unless
6463 there are several. Always display for MI. */
6464 if (allflag
6465 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6466 && (number_of_program_spaces () > 1
6467 || number_of_inferiors () > 1)
6468 /* LOC is for existing B, it cannot be in
6469 moribund_locations and thus having NULL OWNER. */
6470 && loc->owner->type != bp_catchpoint))
6471 mi_only = 0;
6472 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6473 VEC_free (int, inf_num);
6474 }
6475
6476 if (!part_of_multiple)
6477 {
6478 if (b->thread != -1)
6479 {
6480 /* FIXME: This seems to be redundant and lost here; see the
6481 "stop only in" line a little further down. */
6482 uiout->text (" thread ");
6483 uiout->field_int ("thread", b->thread);
6484 }
6485 else if (b->task != 0)
6486 {
6487 uiout->text (" task ");
6488 uiout->field_int ("task", b->task);
6489 }
6490 }
6491
6492 uiout->text ("\n");
6493
6494 if (!part_of_multiple)
6495 b->ops->print_one_detail (b, uiout);
6496
6497 if (part_of_multiple && frame_id_p (b->frame_id))
6498 {
6499 annotate_field (6);
6500 uiout->text ("\tstop only in stack frame at ");
6501 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6502 the frame ID. */
6503 uiout->field_core_addr ("frame",
6504 b->gdbarch, b->frame_id.stack_addr);
6505 uiout->text ("\n");
6506 }
6507
6508 if (!part_of_multiple && b->cond_string)
6509 {
6510 annotate_field (7);
6511 if (is_tracepoint (b))
6512 uiout->text ("\ttrace only if ");
6513 else
6514 uiout->text ("\tstop only if ");
6515 uiout->field_string ("cond", b->cond_string);
6516
6517 /* Print whether the target is doing the breakpoint's condition
6518 evaluation. If GDB is doing the evaluation, don't print anything. */
6519 if (is_breakpoint (b)
6520 && breakpoint_condition_evaluation_mode ()
6521 == condition_evaluation_target)
6522 {
6523 uiout->text (" (");
6524 uiout->field_string ("evaluated-by",
6525 bp_condition_evaluator (b));
6526 uiout->text (" evals)");
6527 }
6528 uiout->text ("\n");
6529 }
6530
6531 if (!part_of_multiple && b->thread != -1)
6532 {
6533 /* FIXME should make an annotation for this. */
6534 uiout->text ("\tstop only in thread ");
6535 if (uiout->is_mi_like_p ())
6536 uiout->field_int ("thread", b->thread);
6537 else
6538 {
6539 struct thread_info *thr = find_thread_global_id (b->thread);
6540
6541 uiout->field_string ("thread", print_thread_id (thr));
6542 }
6543 uiout->text ("\n");
6544 }
6545
6546 if (!part_of_multiple)
6547 {
6548 if (b->hit_count)
6549 {
6550 /* FIXME should make an annotation for this. */
6551 if (is_catchpoint (b))
6552 uiout->text ("\tcatchpoint");
6553 else if (is_tracepoint (b))
6554 uiout->text ("\ttracepoint");
6555 else
6556 uiout->text ("\tbreakpoint");
6557 uiout->text (" already hit ");
6558 uiout->field_int ("times", b->hit_count);
6559 if (b->hit_count == 1)
6560 uiout->text (" time\n");
6561 else
6562 uiout->text (" times\n");
6563 }
6564 else
6565 {
6566 /* Output the count also if it is zero, but only if this is mi. */
6567 if (uiout->is_mi_like_p ())
6568 uiout->field_int ("times", b->hit_count);
6569 }
6570 }
6571
6572 if (!part_of_multiple && b->ignore_count)
6573 {
6574 annotate_field (8);
6575 uiout->text ("\tignore next ");
6576 uiout->field_int ("ignore", b->ignore_count);
6577 uiout->text (" hits\n");
6578 }
6579
6580 /* Note that an enable count of 1 corresponds to "enable once"
6581 behavior, which is reported by the combination of enablement and
6582 disposition, so we don't need to mention it here. */
6583 if (!part_of_multiple && b->enable_count > 1)
6584 {
6585 annotate_field (8);
6586 uiout->text ("\tdisable after ");
6587 /* Tweak the wording to clarify that ignore and enable counts
6588 are distinct, and have additive effect. */
6589 if (b->ignore_count)
6590 uiout->text ("additional ");
6591 else
6592 uiout->text ("next ");
6593 uiout->field_int ("enable", b->enable_count);
6594 uiout->text (" hits\n");
6595 }
6596
6597 if (!part_of_multiple && is_tracepoint (b))
6598 {
6599 struct tracepoint *tp = (struct tracepoint *) b;
6600
6601 if (tp->traceframe_usage)
6602 {
6603 uiout->text ("\ttrace buffer usage ");
6604 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6605 uiout->text (" bytes\n");
6606 }
6607 }
6608
6609 l = b->commands ? b->commands->commands : NULL;
6610 if (!part_of_multiple && l)
6611 {
6612 annotate_field (9);
6613 ui_out_emit_tuple tuple_emitter (uiout, "script");
6614 print_command_lines (uiout, l, 4);
6615 }
6616
6617 if (is_tracepoint (b))
6618 {
6619 struct tracepoint *t = (struct tracepoint *) b;
6620
6621 if (!part_of_multiple && t->pass_count)
6622 {
6623 annotate_field (10);
6624 uiout->text ("\tpass count ");
6625 uiout->field_int ("pass", t->pass_count);
6626 uiout->text (" \n");
6627 }
6628
6629 /* Don't display it when tracepoint or tracepoint location is
6630 pending. */
6631 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6632 {
6633 annotate_field (11);
6634
6635 if (uiout->is_mi_like_p ())
6636 uiout->field_string ("installed",
6637 loc->inserted ? "y" : "n");
6638 else
6639 {
6640 if (loc->inserted)
6641 uiout->text ("\t");
6642 else
6643 uiout->text ("\tnot ");
6644 uiout->text ("installed on target\n");
6645 }
6646 }
6647 }
6648
6649 if (uiout->is_mi_like_p () && !part_of_multiple)
6650 {
6651 if (is_watchpoint (b))
6652 {
6653 struct watchpoint *w = (struct watchpoint *) b;
6654
6655 uiout->field_string ("original-location", w->exp_string);
6656 }
6657 else if (b->location != NULL
6658 && event_location_to_string (b->location.get ()) != NULL)
6659 uiout->field_string ("original-location",
6660 event_location_to_string (b->location.get ()));
6661 }
6662 }
6663
6664 static void
6665 print_one_breakpoint (struct breakpoint *b,
6666 struct bp_location **last_loc,
6667 int allflag)
6668 {
6669 struct ui_out *uiout = current_uiout;
6670
6671 {
6672 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6673
6674 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6675 }
6676
6677 /* If this breakpoint has custom print function,
6678 it's already printed. Otherwise, print individual
6679 locations, if any. */
6680 if (b->ops == NULL || b->ops->print_one == NULL)
6681 {
6682 /* If breakpoint has a single location that is disabled, we
6683 print it as if it had several locations, since otherwise it's
6684 hard to represent "breakpoint enabled, location disabled"
6685 situation.
6686
6687 Note that while hardware watchpoints have several locations
6688 internally, that's not a property exposed to user. */
6689 if (b->loc
6690 && !is_hardware_watchpoint (b)
6691 && (b->loc->next || !b->loc->enabled))
6692 {
6693 struct bp_location *loc;
6694 int n = 1;
6695
6696 for (loc = b->loc; loc; loc = loc->next, ++n)
6697 {
6698 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6699 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6700 }
6701 }
6702 }
6703 }
6704
6705 static int
6706 breakpoint_address_bits (struct breakpoint *b)
6707 {
6708 int print_address_bits = 0;
6709 struct bp_location *loc;
6710
6711 /* Software watchpoints that aren't watching memory don't have an
6712 address to print. */
6713 if (is_no_memory_software_watchpoint (b))
6714 return 0;
6715
6716 for (loc = b->loc; loc; loc = loc->next)
6717 {
6718 int addr_bit;
6719
6720 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6721 if (addr_bit > print_address_bits)
6722 print_address_bits = addr_bit;
6723 }
6724
6725 return print_address_bits;
6726 }
6727
6728 struct captured_breakpoint_query_args
6729 {
6730 int bnum;
6731 };
6732
6733 static int
6734 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6735 {
6736 struct captured_breakpoint_query_args *args
6737 = (struct captured_breakpoint_query_args *) data;
6738 struct breakpoint *b;
6739 struct bp_location *dummy_loc = NULL;
6740
6741 ALL_BREAKPOINTS (b)
6742 {
6743 if (args->bnum == b->number)
6744 {
6745 print_one_breakpoint (b, &dummy_loc, 0);
6746 return GDB_RC_OK;
6747 }
6748 }
6749 return GDB_RC_NONE;
6750 }
6751
6752 enum gdb_rc
6753 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6754 char **error_message)
6755 {
6756 struct captured_breakpoint_query_args args;
6757
6758 args.bnum = bnum;
6759 /* For the moment we don't trust print_one_breakpoint() to not throw
6760 an error. */
6761 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6762 error_message, RETURN_MASK_ALL) < 0)
6763 return GDB_RC_FAIL;
6764 else
6765 return GDB_RC_OK;
6766 }
6767
6768 /* Return true if this breakpoint was set by the user, false if it is
6769 internal or momentary. */
6770
6771 int
6772 user_breakpoint_p (struct breakpoint *b)
6773 {
6774 return b->number > 0;
6775 }
6776
6777 /* See breakpoint.h. */
6778
6779 int
6780 pending_breakpoint_p (struct breakpoint *b)
6781 {
6782 return b->loc == NULL;
6783 }
6784
6785 /* Print information on user settable breakpoint (watchpoint, etc)
6786 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6787 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6788 FILTER is non-NULL, call it on each breakpoint and only include the
6789 ones for which it returns non-zero. Return the total number of
6790 breakpoints listed. */
6791
6792 static int
6793 breakpoint_1 (char *args, int allflag,
6794 int (*filter) (const struct breakpoint *))
6795 {
6796 struct breakpoint *b;
6797 struct bp_location *last_loc = NULL;
6798 int nr_printable_breakpoints;
6799 struct value_print_options opts;
6800 int print_address_bits = 0;
6801 int print_type_col_width = 14;
6802 struct ui_out *uiout = current_uiout;
6803
6804 get_user_print_options (&opts);
6805
6806 /* Compute the number of rows in the table, as well as the size
6807 required for address fields. */
6808 nr_printable_breakpoints = 0;
6809 ALL_BREAKPOINTS (b)
6810 {
6811 /* If we have a filter, only list the breakpoints it accepts. */
6812 if (filter && !filter (b))
6813 continue;
6814
6815 /* If we have an "args" string, it is a list of breakpoints to
6816 accept. Skip the others. */
6817 if (args != NULL && *args != '\0')
6818 {
6819 if (allflag && parse_and_eval_long (args) != b->number)
6820 continue;
6821 if (!allflag && !number_is_in_list (args, b->number))
6822 continue;
6823 }
6824
6825 if (allflag || user_breakpoint_p (b))
6826 {
6827 int addr_bit, type_len;
6828
6829 addr_bit = breakpoint_address_bits (b);
6830 if (addr_bit > print_address_bits)
6831 print_address_bits = addr_bit;
6832
6833 type_len = strlen (bptype_string (b->type));
6834 if (type_len > print_type_col_width)
6835 print_type_col_width = type_len;
6836
6837 nr_printable_breakpoints++;
6838 }
6839 }
6840
6841 {
6842 ui_out_emit_table table_emitter (uiout,
6843 opts.addressprint ? 6 : 5,
6844 nr_printable_breakpoints,
6845 "BreakpointTable");
6846
6847 if (nr_printable_breakpoints > 0)
6848 annotate_breakpoints_headers ();
6849 if (nr_printable_breakpoints > 0)
6850 annotate_field (0);
6851 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6852 if (nr_printable_breakpoints > 0)
6853 annotate_field (1);
6854 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6855 if (nr_printable_breakpoints > 0)
6856 annotate_field (2);
6857 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6858 if (nr_printable_breakpoints > 0)
6859 annotate_field (3);
6860 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6861 if (opts.addressprint)
6862 {
6863 if (nr_printable_breakpoints > 0)
6864 annotate_field (4);
6865 if (print_address_bits <= 32)
6866 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6867 else
6868 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6869 }
6870 if (nr_printable_breakpoints > 0)
6871 annotate_field (5);
6872 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6873 uiout->table_body ();
6874 if (nr_printable_breakpoints > 0)
6875 annotate_breakpoints_table ();
6876
6877 ALL_BREAKPOINTS (b)
6878 {
6879 QUIT;
6880 /* If we have a filter, only list the breakpoints it accepts. */
6881 if (filter && !filter (b))
6882 continue;
6883
6884 /* If we have an "args" string, it is a list of breakpoints to
6885 accept. Skip the others. */
6886
6887 if (args != NULL && *args != '\0')
6888 {
6889 if (allflag) /* maintenance info breakpoint */
6890 {
6891 if (parse_and_eval_long (args) != b->number)
6892 continue;
6893 }
6894 else /* all others */
6895 {
6896 if (!number_is_in_list (args, b->number))
6897 continue;
6898 }
6899 }
6900 /* We only print out user settable breakpoints unless the
6901 allflag is set. */
6902 if (allflag || user_breakpoint_p (b))
6903 print_one_breakpoint (b, &last_loc, allflag);
6904 }
6905 }
6906
6907 if (nr_printable_breakpoints == 0)
6908 {
6909 /* If there's a filter, let the caller decide how to report
6910 empty list. */
6911 if (!filter)
6912 {
6913 if (args == NULL || *args == '\0')
6914 uiout->message ("No breakpoints or watchpoints.\n");
6915 else
6916 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6917 args);
6918 }
6919 }
6920 else
6921 {
6922 if (last_loc && !server_command)
6923 set_next_address (last_loc->gdbarch, last_loc->address);
6924 }
6925
6926 /* FIXME? Should this be moved up so that it is only called when
6927 there have been breakpoints? */
6928 annotate_breakpoints_table_end ();
6929
6930 return nr_printable_breakpoints;
6931 }
6932
6933 /* Display the value of default-collect in a way that is generally
6934 compatible with the breakpoint list. */
6935
6936 static void
6937 default_collect_info (void)
6938 {
6939 struct ui_out *uiout = current_uiout;
6940
6941 /* If it has no value (which is frequently the case), say nothing; a
6942 message like "No default-collect." gets in user's face when it's
6943 not wanted. */
6944 if (!*default_collect)
6945 return;
6946
6947 /* The following phrase lines up nicely with per-tracepoint collect
6948 actions. */
6949 uiout->text ("default collect ");
6950 uiout->field_string ("default-collect", default_collect);
6951 uiout->text (" \n");
6952 }
6953
6954 static void
6955 info_breakpoints_command (char *args, int from_tty)
6956 {
6957 breakpoint_1 (args, 0, NULL);
6958
6959 default_collect_info ();
6960 }
6961
6962 static void
6963 info_watchpoints_command (char *args, int from_tty)
6964 {
6965 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6966 struct ui_out *uiout = current_uiout;
6967
6968 if (num_printed == 0)
6969 {
6970 if (args == NULL || *args == '\0')
6971 uiout->message ("No watchpoints.\n");
6972 else
6973 uiout->message ("No watchpoint matching '%s'.\n", args);
6974 }
6975 }
6976
6977 static void
6978 maintenance_info_breakpoints (char *args, int from_tty)
6979 {
6980 breakpoint_1 (args, 1, NULL);
6981
6982 default_collect_info ();
6983 }
6984
6985 static int
6986 breakpoint_has_pc (struct breakpoint *b,
6987 struct program_space *pspace,
6988 CORE_ADDR pc, struct obj_section *section)
6989 {
6990 struct bp_location *bl = b->loc;
6991
6992 for (; bl; bl = bl->next)
6993 {
6994 if (bl->pspace == pspace
6995 && bl->address == pc
6996 && (!overlay_debugging || bl->section == section))
6997 return 1;
6998 }
6999 return 0;
7000 }
7001
7002 /* Print a message describing any user-breakpoints set at PC. This
7003 concerns with logical breakpoints, so we match program spaces, not
7004 address spaces. */
7005
7006 static void
7007 describe_other_breakpoints (struct gdbarch *gdbarch,
7008 struct program_space *pspace, CORE_ADDR pc,
7009 struct obj_section *section, int thread)
7010 {
7011 int others = 0;
7012 struct breakpoint *b;
7013
7014 ALL_BREAKPOINTS (b)
7015 others += (user_breakpoint_p (b)
7016 && breakpoint_has_pc (b, pspace, pc, section));
7017 if (others > 0)
7018 {
7019 if (others == 1)
7020 printf_filtered (_("Note: breakpoint "));
7021 else /* if (others == ???) */
7022 printf_filtered (_("Note: breakpoints "));
7023 ALL_BREAKPOINTS (b)
7024 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7025 {
7026 others--;
7027 printf_filtered ("%d", b->number);
7028 if (b->thread == -1 && thread != -1)
7029 printf_filtered (" (all threads)");
7030 else if (b->thread != -1)
7031 printf_filtered (" (thread %d)", b->thread);
7032 printf_filtered ("%s%s ",
7033 ((b->enable_state == bp_disabled
7034 || b->enable_state == bp_call_disabled)
7035 ? " (disabled)"
7036 : ""),
7037 (others > 1) ? ","
7038 : ((others == 1) ? " and" : ""));
7039 }
7040 printf_filtered (_("also set at pc "));
7041 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7042 printf_filtered (".\n");
7043 }
7044 }
7045 \f
7046
7047 /* Return true iff it is meaningful to use the address member of
7048 BPT locations. For some breakpoint types, the locations' address members
7049 are irrelevant and it makes no sense to attempt to compare them to other
7050 addresses (or use them for any other purpose either).
7051
7052 More specifically, each of the following breakpoint types will
7053 always have a zero valued location address and we don't want to mark
7054 breakpoints of any of these types to be a duplicate of an actual
7055 breakpoint location at address zero:
7056
7057 bp_watchpoint
7058 bp_catchpoint
7059
7060 */
7061
7062 static int
7063 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7064 {
7065 enum bptype type = bpt->type;
7066
7067 return (type != bp_watchpoint && type != bp_catchpoint);
7068 }
7069
7070 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7071 true if LOC1 and LOC2 represent the same watchpoint location. */
7072
7073 static int
7074 watchpoint_locations_match (struct bp_location *loc1,
7075 struct bp_location *loc2)
7076 {
7077 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7078 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7079
7080 /* Both of them must exist. */
7081 gdb_assert (w1 != NULL);
7082 gdb_assert (w2 != NULL);
7083
7084 /* If the target can evaluate the condition expression in hardware,
7085 then we we need to insert both watchpoints even if they are at
7086 the same place. Otherwise the watchpoint will only trigger when
7087 the condition of whichever watchpoint was inserted evaluates to
7088 true, not giving a chance for GDB to check the condition of the
7089 other watchpoint. */
7090 if ((w1->cond_exp
7091 && target_can_accel_watchpoint_condition (loc1->address,
7092 loc1->length,
7093 loc1->watchpoint_type,
7094 w1->cond_exp.get ()))
7095 || (w2->cond_exp
7096 && target_can_accel_watchpoint_condition (loc2->address,
7097 loc2->length,
7098 loc2->watchpoint_type,
7099 w2->cond_exp.get ())))
7100 return 0;
7101
7102 /* Note that this checks the owner's type, not the location's. In
7103 case the target does not support read watchpoints, but does
7104 support access watchpoints, we'll have bp_read_watchpoint
7105 watchpoints with hw_access locations. Those should be considered
7106 duplicates of hw_read locations. The hw_read locations will
7107 become hw_access locations later. */
7108 return (loc1->owner->type == loc2->owner->type
7109 && loc1->pspace->aspace == loc2->pspace->aspace
7110 && loc1->address == loc2->address
7111 && loc1->length == loc2->length);
7112 }
7113
7114 /* See breakpoint.h. */
7115
7116 int
7117 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7118 struct address_space *aspace2, CORE_ADDR addr2)
7119 {
7120 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7121 || aspace1 == aspace2)
7122 && addr1 == addr2);
7123 }
7124
7125 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7126 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7127 matches ASPACE2. On targets that have global breakpoints, the address
7128 space doesn't really matter. */
7129
7130 static int
7131 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7132 int len1, struct address_space *aspace2,
7133 CORE_ADDR addr2)
7134 {
7135 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7136 || aspace1 == aspace2)
7137 && addr2 >= addr1 && addr2 < addr1 + len1);
7138 }
7139
7140 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7141 a ranged breakpoint. In most targets, a match happens only if ASPACE
7142 matches the breakpoint's address space. On targets that have global
7143 breakpoints, the address space doesn't really matter. */
7144
7145 static int
7146 breakpoint_location_address_match (struct bp_location *bl,
7147 struct address_space *aspace,
7148 CORE_ADDR addr)
7149 {
7150 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7151 aspace, addr)
7152 || (bl->length
7153 && breakpoint_address_match_range (bl->pspace->aspace,
7154 bl->address, bl->length,
7155 aspace, addr)));
7156 }
7157
7158 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7159 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7160 match happens only if ASPACE matches the breakpoint's address
7161 space. On targets that have global breakpoints, the address space
7162 doesn't really matter. */
7163
7164 static int
7165 breakpoint_location_address_range_overlap (struct bp_location *bl,
7166 struct address_space *aspace,
7167 CORE_ADDR addr, int len)
7168 {
7169 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7170 || bl->pspace->aspace == aspace)
7171 {
7172 int bl_len = bl->length != 0 ? bl->length : 1;
7173
7174 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7175 return 1;
7176 }
7177 return 0;
7178 }
7179
7180 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7181 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7182 true, otherwise returns false. */
7183
7184 static int
7185 tracepoint_locations_match (struct bp_location *loc1,
7186 struct bp_location *loc2)
7187 {
7188 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7189 /* Since tracepoint locations are never duplicated with others', tracepoint
7190 locations at the same address of different tracepoints are regarded as
7191 different locations. */
7192 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7193 else
7194 return 0;
7195 }
7196
7197 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7198 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7199 represent the same location. */
7200
7201 static int
7202 breakpoint_locations_match (struct bp_location *loc1,
7203 struct bp_location *loc2)
7204 {
7205 int hw_point1, hw_point2;
7206
7207 /* Both of them must not be in moribund_locations. */
7208 gdb_assert (loc1->owner != NULL);
7209 gdb_assert (loc2->owner != NULL);
7210
7211 hw_point1 = is_hardware_watchpoint (loc1->owner);
7212 hw_point2 = is_hardware_watchpoint (loc2->owner);
7213
7214 if (hw_point1 != hw_point2)
7215 return 0;
7216 else if (hw_point1)
7217 return watchpoint_locations_match (loc1, loc2);
7218 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7219 return tracepoint_locations_match (loc1, loc2);
7220 else
7221 /* We compare bp_location.length in order to cover ranged breakpoints. */
7222 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7223 loc2->pspace->aspace, loc2->address)
7224 && loc1->length == loc2->length);
7225 }
7226
7227 static void
7228 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7229 int bnum, int have_bnum)
7230 {
7231 /* The longest string possibly returned by hex_string_custom
7232 is 50 chars. These must be at least that big for safety. */
7233 char astr1[64];
7234 char astr2[64];
7235
7236 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7237 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7238 if (have_bnum)
7239 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7240 bnum, astr1, astr2);
7241 else
7242 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7243 }
7244
7245 /* Adjust a breakpoint's address to account for architectural
7246 constraints on breakpoint placement. Return the adjusted address.
7247 Note: Very few targets require this kind of adjustment. For most
7248 targets, this function is simply the identity function. */
7249
7250 static CORE_ADDR
7251 adjust_breakpoint_address (struct gdbarch *gdbarch,
7252 CORE_ADDR bpaddr, enum bptype bptype)
7253 {
7254 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7255 {
7256 /* Very few targets need any kind of breakpoint adjustment. */
7257 return bpaddr;
7258 }
7259 else if (bptype == bp_watchpoint
7260 || bptype == bp_hardware_watchpoint
7261 || bptype == bp_read_watchpoint
7262 || bptype == bp_access_watchpoint
7263 || bptype == bp_catchpoint)
7264 {
7265 /* Watchpoints and the various bp_catch_* eventpoints should not
7266 have their addresses modified. */
7267 return bpaddr;
7268 }
7269 else if (bptype == bp_single_step)
7270 {
7271 /* Single-step breakpoints should not have their addresses
7272 modified. If there's any architectural constrain that
7273 applies to this address, then it should have already been
7274 taken into account when the breakpoint was created in the
7275 first place. If we didn't do this, stepping through e.g.,
7276 Thumb-2 IT blocks would break. */
7277 return bpaddr;
7278 }
7279 else
7280 {
7281 CORE_ADDR adjusted_bpaddr;
7282
7283 /* Some targets have architectural constraints on the placement
7284 of breakpoint instructions. Obtain the adjusted address. */
7285 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7286
7287 /* An adjusted breakpoint address can significantly alter
7288 a user's expectations. Print a warning if an adjustment
7289 is required. */
7290 if (adjusted_bpaddr != bpaddr)
7291 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7292
7293 return adjusted_bpaddr;
7294 }
7295 }
7296
7297 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7298 {
7299 bp_location *loc = this;
7300
7301 gdb_assert (ops != NULL);
7302
7303 loc->ops = ops;
7304 loc->owner = owner;
7305 loc->cond_bytecode = NULL;
7306 loc->shlib_disabled = 0;
7307 loc->enabled = 1;
7308
7309 switch (owner->type)
7310 {
7311 case bp_breakpoint:
7312 case bp_single_step:
7313 case bp_until:
7314 case bp_finish:
7315 case bp_longjmp:
7316 case bp_longjmp_resume:
7317 case bp_longjmp_call_dummy:
7318 case bp_exception:
7319 case bp_exception_resume:
7320 case bp_step_resume:
7321 case bp_hp_step_resume:
7322 case bp_watchpoint_scope:
7323 case bp_call_dummy:
7324 case bp_std_terminate:
7325 case bp_shlib_event:
7326 case bp_thread_event:
7327 case bp_overlay_event:
7328 case bp_jit_event:
7329 case bp_longjmp_master:
7330 case bp_std_terminate_master:
7331 case bp_exception_master:
7332 case bp_gnu_ifunc_resolver:
7333 case bp_gnu_ifunc_resolver_return:
7334 case bp_dprintf:
7335 loc->loc_type = bp_loc_software_breakpoint;
7336 mark_breakpoint_location_modified (loc);
7337 break;
7338 case bp_hardware_breakpoint:
7339 loc->loc_type = bp_loc_hardware_breakpoint;
7340 mark_breakpoint_location_modified (loc);
7341 break;
7342 case bp_hardware_watchpoint:
7343 case bp_read_watchpoint:
7344 case bp_access_watchpoint:
7345 loc->loc_type = bp_loc_hardware_watchpoint;
7346 break;
7347 case bp_watchpoint:
7348 case bp_catchpoint:
7349 case bp_tracepoint:
7350 case bp_fast_tracepoint:
7351 case bp_static_tracepoint:
7352 loc->loc_type = bp_loc_other;
7353 break;
7354 default:
7355 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7356 }
7357
7358 loc->refc = 1;
7359 }
7360
7361 /* Allocate a struct bp_location. */
7362
7363 static struct bp_location *
7364 allocate_bp_location (struct breakpoint *bpt)
7365 {
7366 return bpt->ops->allocate_location (bpt);
7367 }
7368
7369 static void
7370 free_bp_location (struct bp_location *loc)
7371 {
7372 loc->ops->dtor (loc);
7373 delete loc;
7374 }
7375
7376 /* Increment reference count. */
7377
7378 static void
7379 incref_bp_location (struct bp_location *bl)
7380 {
7381 ++bl->refc;
7382 }
7383
7384 /* Decrement reference count. If the reference count reaches 0,
7385 destroy the bp_location. Sets *BLP to NULL. */
7386
7387 static void
7388 decref_bp_location (struct bp_location **blp)
7389 {
7390 gdb_assert ((*blp)->refc > 0);
7391
7392 if (--(*blp)->refc == 0)
7393 free_bp_location (*blp);
7394 *blp = NULL;
7395 }
7396
7397 /* Add breakpoint B at the end of the global breakpoint chain. */
7398
7399 static breakpoint *
7400 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7401 {
7402 struct breakpoint *b1;
7403 struct breakpoint *result = b.get ();
7404
7405 /* Add this breakpoint to the end of the chain so that a list of
7406 breakpoints will come out in order of increasing numbers. */
7407
7408 b1 = breakpoint_chain;
7409 if (b1 == 0)
7410 breakpoint_chain = b.release ();
7411 else
7412 {
7413 while (b1->next)
7414 b1 = b1->next;
7415 b1->next = b.release ();
7416 }
7417
7418 return result;
7419 }
7420
7421 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7422
7423 static void
7424 init_raw_breakpoint_without_location (struct breakpoint *b,
7425 struct gdbarch *gdbarch,
7426 enum bptype bptype,
7427 const struct breakpoint_ops *ops)
7428 {
7429 gdb_assert (ops != NULL);
7430
7431 b->ops = ops;
7432 b->type = bptype;
7433 b->gdbarch = gdbarch;
7434 b->language = current_language->la_language;
7435 b->input_radix = input_radix;
7436 b->related_breakpoint = b;
7437 }
7438
7439 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7440 that has type BPTYPE and has no locations as yet. */
7441
7442 static struct breakpoint *
7443 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7444 enum bptype bptype,
7445 const struct breakpoint_ops *ops)
7446 {
7447 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7448
7449 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7450 return add_to_breakpoint_chain (std::move (b));
7451 }
7452
7453 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7454 resolutions should be made as the user specified the location explicitly
7455 enough. */
7456
7457 static void
7458 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7459 {
7460 gdb_assert (loc->owner != NULL);
7461
7462 if (loc->owner->type == bp_breakpoint
7463 || loc->owner->type == bp_hardware_breakpoint
7464 || is_tracepoint (loc->owner))
7465 {
7466 int is_gnu_ifunc;
7467 const char *function_name;
7468 CORE_ADDR func_addr;
7469
7470 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7471 &func_addr, NULL, &is_gnu_ifunc);
7472
7473 if (is_gnu_ifunc && !explicit_loc)
7474 {
7475 struct breakpoint *b = loc->owner;
7476
7477 gdb_assert (loc->pspace == current_program_space);
7478 if (gnu_ifunc_resolve_name (function_name,
7479 &loc->requested_address))
7480 {
7481 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7482 loc->address = adjust_breakpoint_address (loc->gdbarch,
7483 loc->requested_address,
7484 b->type);
7485 }
7486 else if (b->type == bp_breakpoint && b->loc == loc
7487 && loc->next == NULL && b->related_breakpoint == b)
7488 {
7489 /* Create only the whole new breakpoint of this type but do not
7490 mess more complicated breakpoints with multiple locations. */
7491 b->type = bp_gnu_ifunc_resolver;
7492 /* Remember the resolver's address for use by the return
7493 breakpoint. */
7494 loc->related_address = func_addr;
7495 }
7496 }
7497
7498 if (function_name)
7499 loc->function_name = xstrdup (function_name);
7500 }
7501 }
7502
7503 /* Attempt to determine architecture of location identified by SAL. */
7504 struct gdbarch *
7505 get_sal_arch (struct symtab_and_line sal)
7506 {
7507 if (sal.section)
7508 return get_objfile_arch (sal.section->objfile);
7509 if (sal.symtab)
7510 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7511
7512 return NULL;
7513 }
7514
7515 /* Low level routine for partially initializing a breakpoint of type
7516 BPTYPE. The newly created breakpoint's address, section, source
7517 file name, and line number are provided by SAL.
7518
7519 It is expected that the caller will complete the initialization of
7520 the newly created breakpoint struct as well as output any status
7521 information regarding the creation of a new breakpoint. */
7522
7523 static void
7524 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7525 struct symtab_and_line sal, enum bptype bptype,
7526 const struct breakpoint_ops *ops)
7527 {
7528 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7529
7530 add_location_to_breakpoint (b, &sal);
7531
7532 if (bptype != bp_catchpoint)
7533 gdb_assert (sal.pspace != NULL);
7534
7535 /* Store the program space that was used to set the breakpoint,
7536 except for ordinary breakpoints, which are independent of the
7537 program space. */
7538 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7539 b->pspace = sal.pspace;
7540 }
7541
7542 /* set_raw_breakpoint is a low level routine for allocating and
7543 partially initializing a breakpoint of type BPTYPE. The newly
7544 created breakpoint's address, section, source file name, and line
7545 number are provided by SAL. The newly created and partially
7546 initialized breakpoint is added to the breakpoint chain and
7547 is also returned as the value of this function.
7548
7549 It is expected that the caller will complete the initialization of
7550 the newly created breakpoint struct as well as output any status
7551 information regarding the creation of a new breakpoint. In
7552 particular, set_raw_breakpoint does NOT set the breakpoint
7553 number! Care should be taken to not allow an error to occur
7554 prior to completing the initialization of the breakpoint. If this
7555 should happen, a bogus breakpoint will be left on the chain. */
7556
7557 struct breakpoint *
7558 set_raw_breakpoint (struct gdbarch *gdbarch,
7559 struct symtab_and_line sal, enum bptype bptype,
7560 const struct breakpoint_ops *ops)
7561 {
7562 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7563
7564 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7565 return add_to_breakpoint_chain (std::move (b));
7566 }
7567
7568 /* Call this routine when stepping and nexting to enable a breakpoint
7569 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7570 initiated the operation. */
7571
7572 void
7573 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7574 {
7575 struct breakpoint *b, *b_tmp;
7576 int thread = tp->global_num;
7577
7578 /* To avoid having to rescan all objfile symbols at every step,
7579 we maintain a list of continually-inserted but always disabled
7580 longjmp "master" breakpoints. Here, we simply create momentary
7581 clones of those and enable them for the requested thread. */
7582 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7583 if (b->pspace == current_program_space
7584 && (b->type == bp_longjmp_master
7585 || b->type == bp_exception_master))
7586 {
7587 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7588 struct breakpoint *clone;
7589
7590 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7591 after their removal. */
7592 clone = momentary_breakpoint_from_master (b, type,
7593 &momentary_breakpoint_ops, 1);
7594 clone->thread = thread;
7595 }
7596
7597 tp->initiating_frame = frame;
7598 }
7599
7600 /* Delete all longjmp breakpoints from THREAD. */
7601 void
7602 delete_longjmp_breakpoint (int thread)
7603 {
7604 struct breakpoint *b, *b_tmp;
7605
7606 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7607 if (b->type == bp_longjmp || b->type == bp_exception)
7608 {
7609 if (b->thread == thread)
7610 delete_breakpoint (b);
7611 }
7612 }
7613
7614 void
7615 delete_longjmp_breakpoint_at_next_stop (int thread)
7616 {
7617 struct breakpoint *b, *b_tmp;
7618
7619 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7620 if (b->type == bp_longjmp || b->type == bp_exception)
7621 {
7622 if (b->thread == thread)
7623 b->disposition = disp_del_at_next_stop;
7624 }
7625 }
7626
7627 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7628 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7629 pointer to any of them. Return NULL if this system cannot place longjmp
7630 breakpoints. */
7631
7632 struct breakpoint *
7633 set_longjmp_breakpoint_for_call_dummy (void)
7634 {
7635 struct breakpoint *b, *retval = NULL;
7636
7637 ALL_BREAKPOINTS (b)
7638 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7639 {
7640 struct breakpoint *new_b;
7641
7642 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7643 &momentary_breakpoint_ops,
7644 1);
7645 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7646
7647 /* Link NEW_B into the chain of RETVAL breakpoints. */
7648
7649 gdb_assert (new_b->related_breakpoint == new_b);
7650 if (retval == NULL)
7651 retval = new_b;
7652 new_b->related_breakpoint = retval;
7653 while (retval->related_breakpoint != new_b->related_breakpoint)
7654 retval = retval->related_breakpoint;
7655 retval->related_breakpoint = new_b;
7656 }
7657
7658 return retval;
7659 }
7660
7661 /* Verify all existing dummy frames and their associated breakpoints for
7662 TP. Remove those which can no longer be found in the current frame
7663 stack.
7664
7665 You should call this function only at places where it is safe to currently
7666 unwind the whole stack. Failed stack unwind would discard live dummy
7667 frames. */
7668
7669 void
7670 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7671 {
7672 struct breakpoint *b, *b_tmp;
7673
7674 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7675 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7676 {
7677 struct breakpoint *dummy_b = b->related_breakpoint;
7678
7679 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7680 dummy_b = dummy_b->related_breakpoint;
7681 if (dummy_b->type != bp_call_dummy
7682 || frame_find_by_id (dummy_b->frame_id) != NULL)
7683 continue;
7684
7685 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7686
7687 while (b->related_breakpoint != b)
7688 {
7689 if (b_tmp == b->related_breakpoint)
7690 b_tmp = b->related_breakpoint->next;
7691 delete_breakpoint (b->related_breakpoint);
7692 }
7693 delete_breakpoint (b);
7694 }
7695 }
7696
7697 void
7698 enable_overlay_breakpoints (void)
7699 {
7700 struct breakpoint *b;
7701
7702 ALL_BREAKPOINTS (b)
7703 if (b->type == bp_overlay_event)
7704 {
7705 b->enable_state = bp_enabled;
7706 update_global_location_list (UGLL_MAY_INSERT);
7707 overlay_events_enabled = 1;
7708 }
7709 }
7710
7711 void
7712 disable_overlay_breakpoints (void)
7713 {
7714 struct breakpoint *b;
7715
7716 ALL_BREAKPOINTS (b)
7717 if (b->type == bp_overlay_event)
7718 {
7719 b->enable_state = bp_disabled;
7720 update_global_location_list (UGLL_DONT_INSERT);
7721 overlay_events_enabled = 0;
7722 }
7723 }
7724
7725 /* Set an active std::terminate breakpoint for each std::terminate
7726 master breakpoint. */
7727 void
7728 set_std_terminate_breakpoint (void)
7729 {
7730 struct breakpoint *b, *b_tmp;
7731
7732 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7733 if (b->pspace == current_program_space
7734 && b->type == bp_std_terminate_master)
7735 {
7736 momentary_breakpoint_from_master (b, bp_std_terminate,
7737 &momentary_breakpoint_ops, 1);
7738 }
7739 }
7740
7741 /* Delete all the std::terminate breakpoints. */
7742 void
7743 delete_std_terminate_breakpoint (void)
7744 {
7745 struct breakpoint *b, *b_tmp;
7746
7747 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7748 if (b->type == bp_std_terminate)
7749 delete_breakpoint (b);
7750 }
7751
7752 struct breakpoint *
7753 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7754 {
7755 struct breakpoint *b;
7756
7757 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7758 &internal_breakpoint_ops);
7759
7760 b->enable_state = bp_enabled;
7761 /* location has to be used or breakpoint_re_set will delete me. */
7762 b->location = new_address_location (b->loc->address, NULL, 0);
7763
7764 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7765
7766 return b;
7767 }
7768
7769 struct lang_and_radix
7770 {
7771 enum language lang;
7772 int radix;
7773 };
7774
7775 /* Create a breakpoint for JIT code registration and unregistration. */
7776
7777 struct breakpoint *
7778 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7779 {
7780 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7781 &internal_breakpoint_ops);
7782 }
7783
7784 /* Remove JIT code registration and unregistration breakpoint(s). */
7785
7786 void
7787 remove_jit_event_breakpoints (void)
7788 {
7789 struct breakpoint *b, *b_tmp;
7790
7791 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7792 if (b->type == bp_jit_event
7793 && b->loc->pspace == current_program_space)
7794 delete_breakpoint (b);
7795 }
7796
7797 void
7798 remove_solib_event_breakpoints (void)
7799 {
7800 struct breakpoint *b, *b_tmp;
7801
7802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7803 if (b->type == bp_shlib_event
7804 && b->loc->pspace == current_program_space)
7805 delete_breakpoint (b);
7806 }
7807
7808 /* See breakpoint.h. */
7809
7810 void
7811 remove_solib_event_breakpoints_at_next_stop (void)
7812 {
7813 struct breakpoint *b, *b_tmp;
7814
7815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7816 if (b->type == bp_shlib_event
7817 && b->loc->pspace == current_program_space)
7818 b->disposition = disp_del_at_next_stop;
7819 }
7820
7821 /* Helper for create_solib_event_breakpoint /
7822 create_and_insert_solib_event_breakpoint. Allows specifying which
7823 INSERT_MODE to pass through to update_global_location_list. */
7824
7825 static struct breakpoint *
7826 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7827 enum ugll_insert_mode insert_mode)
7828 {
7829 struct breakpoint *b;
7830
7831 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7832 &internal_breakpoint_ops);
7833 update_global_location_list_nothrow (insert_mode);
7834 return b;
7835 }
7836
7837 struct breakpoint *
7838 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7839 {
7840 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7841 }
7842
7843 /* See breakpoint.h. */
7844
7845 struct breakpoint *
7846 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7847 {
7848 struct breakpoint *b;
7849
7850 /* Explicitly tell update_global_location_list to insert
7851 locations. */
7852 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7853 if (!b->loc->inserted)
7854 {
7855 delete_breakpoint (b);
7856 return NULL;
7857 }
7858 return b;
7859 }
7860
7861 /* Disable any breakpoints that are on code in shared libraries. Only
7862 apply to enabled breakpoints, disabled ones can just stay disabled. */
7863
7864 void
7865 disable_breakpoints_in_shlibs (void)
7866 {
7867 struct bp_location *loc, **locp_tmp;
7868
7869 ALL_BP_LOCATIONS (loc, locp_tmp)
7870 {
7871 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7872 struct breakpoint *b = loc->owner;
7873
7874 /* We apply the check to all breakpoints, including disabled for
7875 those with loc->duplicate set. This is so that when breakpoint
7876 becomes enabled, or the duplicate is removed, gdb will try to
7877 insert all breakpoints. If we don't set shlib_disabled here,
7878 we'll try to insert those breakpoints and fail. */
7879 if (((b->type == bp_breakpoint)
7880 || (b->type == bp_jit_event)
7881 || (b->type == bp_hardware_breakpoint)
7882 || (is_tracepoint (b)))
7883 && loc->pspace == current_program_space
7884 && !loc->shlib_disabled
7885 && solib_name_from_address (loc->pspace, loc->address)
7886 )
7887 {
7888 loc->shlib_disabled = 1;
7889 }
7890 }
7891 }
7892
7893 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7894 notification of unloaded_shlib. Only apply to enabled breakpoints,
7895 disabled ones can just stay disabled. */
7896
7897 static void
7898 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7899 {
7900 struct bp_location *loc, **locp_tmp;
7901 int disabled_shlib_breaks = 0;
7902
7903 ALL_BP_LOCATIONS (loc, locp_tmp)
7904 {
7905 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7906 struct breakpoint *b = loc->owner;
7907
7908 if (solib->pspace == loc->pspace
7909 && !loc->shlib_disabled
7910 && (((b->type == bp_breakpoint
7911 || b->type == bp_jit_event
7912 || b->type == bp_hardware_breakpoint)
7913 && (loc->loc_type == bp_loc_hardware_breakpoint
7914 || loc->loc_type == bp_loc_software_breakpoint))
7915 || is_tracepoint (b))
7916 && solib_contains_address_p (solib, loc->address))
7917 {
7918 loc->shlib_disabled = 1;
7919 /* At this point, we cannot rely on remove_breakpoint
7920 succeeding so we must mark the breakpoint as not inserted
7921 to prevent future errors occurring in remove_breakpoints. */
7922 loc->inserted = 0;
7923
7924 /* This may cause duplicate notifications for the same breakpoint. */
7925 observer_notify_breakpoint_modified (b);
7926
7927 if (!disabled_shlib_breaks)
7928 {
7929 target_terminal_ours_for_output ();
7930 warning (_("Temporarily disabling breakpoints "
7931 "for unloaded shared library \"%s\""),
7932 solib->so_name);
7933 }
7934 disabled_shlib_breaks = 1;
7935 }
7936 }
7937 }
7938
7939 /* Disable any breakpoints and tracepoints in OBJFILE upon
7940 notification of free_objfile. Only apply to enabled breakpoints,
7941 disabled ones can just stay disabled. */
7942
7943 static void
7944 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7945 {
7946 struct breakpoint *b;
7947
7948 if (objfile == NULL)
7949 return;
7950
7951 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7952 managed by the user with add-symbol-file/remove-symbol-file.
7953 Similarly to how breakpoints in shared libraries are handled in
7954 response to "nosharedlibrary", mark breakpoints in such modules
7955 shlib_disabled so they end up uninserted on the next global
7956 location list update. Shared libraries not loaded by the user
7957 aren't handled here -- they're already handled in
7958 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7959 solib_unloaded observer. We skip objfiles that are not
7960 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7961 main objfile). */
7962 if ((objfile->flags & OBJF_SHARED) == 0
7963 || (objfile->flags & OBJF_USERLOADED) == 0)
7964 return;
7965
7966 ALL_BREAKPOINTS (b)
7967 {
7968 struct bp_location *loc;
7969 int bp_modified = 0;
7970
7971 if (!is_breakpoint (b) && !is_tracepoint (b))
7972 continue;
7973
7974 for (loc = b->loc; loc != NULL; loc = loc->next)
7975 {
7976 CORE_ADDR loc_addr = loc->address;
7977
7978 if (loc->loc_type != bp_loc_hardware_breakpoint
7979 && loc->loc_type != bp_loc_software_breakpoint)
7980 continue;
7981
7982 if (loc->shlib_disabled != 0)
7983 continue;
7984
7985 if (objfile->pspace != loc->pspace)
7986 continue;
7987
7988 if (loc->loc_type != bp_loc_hardware_breakpoint
7989 && loc->loc_type != bp_loc_software_breakpoint)
7990 continue;
7991
7992 if (is_addr_in_objfile (loc_addr, objfile))
7993 {
7994 loc->shlib_disabled = 1;
7995 /* At this point, we don't know whether the object was
7996 unmapped from the inferior or not, so leave the
7997 inserted flag alone. We'll handle failure to
7998 uninsert quietly, in case the object was indeed
7999 unmapped. */
8000
8001 mark_breakpoint_location_modified (loc);
8002
8003 bp_modified = 1;
8004 }
8005 }
8006
8007 if (bp_modified)
8008 observer_notify_breakpoint_modified (b);
8009 }
8010 }
8011
8012 /* FORK & VFORK catchpoints. */
8013
8014 /* An instance of this type is used to represent a fork or vfork
8015 catchpoint. A breakpoint is really of this type iff its ops pointer points
8016 to CATCH_FORK_BREAKPOINT_OPS. */
8017
8018 struct fork_catchpoint : public breakpoint
8019 {
8020 /* Process id of a child process whose forking triggered this
8021 catchpoint. This field is only valid immediately after this
8022 catchpoint has triggered. */
8023 ptid_t forked_inferior_pid;
8024 };
8025
8026 /* Implement the "insert" breakpoint_ops method for fork
8027 catchpoints. */
8028
8029 static int
8030 insert_catch_fork (struct bp_location *bl)
8031 {
8032 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8033 }
8034
8035 /* Implement the "remove" breakpoint_ops method for fork
8036 catchpoints. */
8037
8038 static int
8039 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8040 {
8041 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8042 }
8043
8044 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8045 catchpoints. */
8046
8047 static int
8048 breakpoint_hit_catch_fork (const struct bp_location *bl,
8049 struct address_space *aspace, CORE_ADDR bp_addr,
8050 const struct target_waitstatus *ws)
8051 {
8052 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8053
8054 if (ws->kind != TARGET_WAITKIND_FORKED)
8055 return 0;
8056
8057 c->forked_inferior_pid = ws->value.related_pid;
8058 return 1;
8059 }
8060
8061 /* Implement the "print_it" breakpoint_ops method for fork
8062 catchpoints. */
8063
8064 static enum print_stop_action
8065 print_it_catch_fork (bpstat bs)
8066 {
8067 struct ui_out *uiout = current_uiout;
8068 struct breakpoint *b = bs->breakpoint_at;
8069 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8070
8071 annotate_catchpoint (b->number);
8072 maybe_print_thread_hit_breakpoint (uiout);
8073 if (b->disposition == disp_del)
8074 uiout->text ("Temporary catchpoint ");
8075 else
8076 uiout->text ("Catchpoint ");
8077 if (uiout->is_mi_like_p ())
8078 {
8079 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8080 uiout->field_string ("disp", bpdisp_text (b->disposition));
8081 }
8082 uiout->field_int ("bkptno", b->number);
8083 uiout->text (" (forked process ");
8084 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8085 uiout->text ("), ");
8086 return PRINT_SRC_AND_LOC;
8087 }
8088
8089 /* Implement the "print_one" breakpoint_ops method for fork
8090 catchpoints. */
8091
8092 static void
8093 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8094 {
8095 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8096 struct value_print_options opts;
8097 struct ui_out *uiout = current_uiout;
8098
8099 get_user_print_options (&opts);
8100
8101 /* Field 4, the address, is omitted (which makes the columns not
8102 line up too nicely with the headers, but the effect is relatively
8103 readable). */
8104 if (opts.addressprint)
8105 uiout->field_skip ("addr");
8106 annotate_field (5);
8107 uiout->text ("fork");
8108 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8109 {
8110 uiout->text (", process ");
8111 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8112 uiout->spaces (1);
8113 }
8114
8115 if (uiout->is_mi_like_p ())
8116 uiout->field_string ("catch-type", "fork");
8117 }
8118
8119 /* Implement the "print_mention" breakpoint_ops method for fork
8120 catchpoints. */
8121
8122 static void
8123 print_mention_catch_fork (struct breakpoint *b)
8124 {
8125 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8126 }
8127
8128 /* Implement the "print_recreate" breakpoint_ops method for fork
8129 catchpoints. */
8130
8131 static void
8132 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8133 {
8134 fprintf_unfiltered (fp, "catch fork");
8135 print_recreate_thread (b, fp);
8136 }
8137
8138 /* The breakpoint_ops structure to be used in fork catchpoints. */
8139
8140 static struct breakpoint_ops catch_fork_breakpoint_ops;
8141
8142 /* Implement the "insert" breakpoint_ops method for vfork
8143 catchpoints. */
8144
8145 static int
8146 insert_catch_vfork (struct bp_location *bl)
8147 {
8148 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8149 }
8150
8151 /* Implement the "remove" breakpoint_ops method for vfork
8152 catchpoints. */
8153
8154 static int
8155 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8156 {
8157 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8158 }
8159
8160 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8161 catchpoints. */
8162
8163 static int
8164 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8165 struct address_space *aspace, CORE_ADDR bp_addr,
8166 const struct target_waitstatus *ws)
8167 {
8168 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8169
8170 if (ws->kind != TARGET_WAITKIND_VFORKED)
8171 return 0;
8172
8173 c->forked_inferior_pid = ws->value.related_pid;
8174 return 1;
8175 }
8176
8177 /* Implement the "print_it" breakpoint_ops method for vfork
8178 catchpoints. */
8179
8180 static enum print_stop_action
8181 print_it_catch_vfork (bpstat bs)
8182 {
8183 struct ui_out *uiout = current_uiout;
8184 struct breakpoint *b = bs->breakpoint_at;
8185 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8186
8187 annotate_catchpoint (b->number);
8188 maybe_print_thread_hit_breakpoint (uiout);
8189 if (b->disposition == disp_del)
8190 uiout->text ("Temporary catchpoint ");
8191 else
8192 uiout->text ("Catchpoint ");
8193 if (uiout->is_mi_like_p ())
8194 {
8195 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8196 uiout->field_string ("disp", bpdisp_text (b->disposition));
8197 }
8198 uiout->field_int ("bkptno", b->number);
8199 uiout->text (" (vforked process ");
8200 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8201 uiout->text ("), ");
8202 return PRINT_SRC_AND_LOC;
8203 }
8204
8205 /* Implement the "print_one" breakpoint_ops method for vfork
8206 catchpoints. */
8207
8208 static void
8209 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8210 {
8211 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8212 struct value_print_options opts;
8213 struct ui_out *uiout = current_uiout;
8214
8215 get_user_print_options (&opts);
8216 /* Field 4, the address, is omitted (which makes the columns not
8217 line up too nicely with the headers, but the effect is relatively
8218 readable). */
8219 if (opts.addressprint)
8220 uiout->field_skip ("addr");
8221 annotate_field (5);
8222 uiout->text ("vfork");
8223 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8224 {
8225 uiout->text (", process ");
8226 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8227 uiout->spaces (1);
8228 }
8229
8230 if (uiout->is_mi_like_p ())
8231 uiout->field_string ("catch-type", "vfork");
8232 }
8233
8234 /* Implement the "print_mention" breakpoint_ops method for vfork
8235 catchpoints. */
8236
8237 static void
8238 print_mention_catch_vfork (struct breakpoint *b)
8239 {
8240 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8241 }
8242
8243 /* Implement the "print_recreate" breakpoint_ops method for vfork
8244 catchpoints. */
8245
8246 static void
8247 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8248 {
8249 fprintf_unfiltered (fp, "catch vfork");
8250 print_recreate_thread (b, fp);
8251 }
8252
8253 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8254
8255 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8256
8257 /* An instance of this type is used to represent an solib catchpoint.
8258 A breakpoint is really of this type iff its ops pointer points to
8259 CATCH_SOLIB_BREAKPOINT_OPS. */
8260
8261 struct solib_catchpoint : public breakpoint
8262 {
8263 ~solib_catchpoint () override;
8264
8265 /* True for "catch load", false for "catch unload". */
8266 unsigned char is_load;
8267
8268 /* Regular expression to match, if any. COMPILED is only valid when
8269 REGEX is non-NULL. */
8270 char *regex;
8271 std::unique_ptr<compiled_regex> compiled;
8272 };
8273
8274 solib_catchpoint::~solib_catchpoint ()
8275 {
8276 xfree (this->regex);
8277 }
8278
8279 static int
8280 insert_catch_solib (struct bp_location *ignore)
8281 {
8282 return 0;
8283 }
8284
8285 static int
8286 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8287 {
8288 return 0;
8289 }
8290
8291 static int
8292 breakpoint_hit_catch_solib (const struct bp_location *bl,
8293 struct address_space *aspace,
8294 CORE_ADDR bp_addr,
8295 const struct target_waitstatus *ws)
8296 {
8297 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8298 struct breakpoint *other;
8299
8300 if (ws->kind == TARGET_WAITKIND_LOADED)
8301 return 1;
8302
8303 ALL_BREAKPOINTS (other)
8304 {
8305 struct bp_location *other_bl;
8306
8307 if (other == bl->owner)
8308 continue;
8309
8310 if (other->type != bp_shlib_event)
8311 continue;
8312
8313 if (self->pspace != NULL && other->pspace != self->pspace)
8314 continue;
8315
8316 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8317 {
8318 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8319 return 1;
8320 }
8321 }
8322
8323 return 0;
8324 }
8325
8326 static void
8327 check_status_catch_solib (struct bpstats *bs)
8328 {
8329 struct solib_catchpoint *self
8330 = (struct solib_catchpoint *) bs->breakpoint_at;
8331 int ix;
8332
8333 if (self->is_load)
8334 {
8335 struct so_list *iter;
8336
8337 for (ix = 0;
8338 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8339 ix, iter);
8340 ++ix)
8341 {
8342 if (!self->regex
8343 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8344 return;
8345 }
8346 }
8347 else
8348 {
8349 char *iter;
8350
8351 for (ix = 0;
8352 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8353 ix, iter);
8354 ++ix)
8355 {
8356 if (!self->regex
8357 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8358 return;
8359 }
8360 }
8361
8362 bs->stop = 0;
8363 bs->print_it = print_it_noop;
8364 }
8365
8366 static enum print_stop_action
8367 print_it_catch_solib (bpstat bs)
8368 {
8369 struct breakpoint *b = bs->breakpoint_at;
8370 struct ui_out *uiout = current_uiout;
8371
8372 annotate_catchpoint (b->number);
8373 maybe_print_thread_hit_breakpoint (uiout);
8374 if (b->disposition == disp_del)
8375 uiout->text ("Temporary catchpoint ");
8376 else
8377 uiout->text ("Catchpoint ");
8378 uiout->field_int ("bkptno", b->number);
8379 uiout->text ("\n");
8380 if (uiout->is_mi_like_p ())
8381 uiout->field_string ("disp", bpdisp_text (b->disposition));
8382 print_solib_event (1);
8383 return PRINT_SRC_AND_LOC;
8384 }
8385
8386 static void
8387 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8388 {
8389 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8390 struct value_print_options opts;
8391 struct ui_out *uiout = current_uiout;
8392 char *msg;
8393
8394 get_user_print_options (&opts);
8395 /* Field 4, the address, is omitted (which makes the columns not
8396 line up too nicely with the headers, but the effect is relatively
8397 readable). */
8398 if (opts.addressprint)
8399 {
8400 annotate_field (4);
8401 uiout->field_skip ("addr");
8402 }
8403
8404 annotate_field (5);
8405 if (self->is_load)
8406 {
8407 if (self->regex)
8408 msg = xstrprintf (_("load of library matching %s"), self->regex);
8409 else
8410 msg = xstrdup (_("load of library"));
8411 }
8412 else
8413 {
8414 if (self->regex)
8415 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8416 else
8417 msg = xstrdup (_("unload of library"));
8418 }
8419 uiout->field_string ("what", msg);
8420 xfree (msg);
8421
8422 if (uiout->is_mi_like_p ())
8423 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8424 }
8425
8426 static void
8427 print_mention_catch_solib (struct breakpoint *b)
8428 {
8429 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8430
8431 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8432 self->is_load ? "load" : "unload");
8433 }
8434
8435 static void
8436 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8437 {
8438 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8439
8440 fprintf_unfiltered (fp, "%s %s",
8441 b->disposition == disp_del ? "tcatch" : "catch",
8442 self->is_load ? "load" : "unload");
8443 if (self->regex)
8444 fprintf_unfiltered (fp, " %s", self->regex);
8445 fprintf_unfiltered (fp, "\n");
8446 }
8447
8448 static struct breakpoint_ops catch_solib_breakpoint_ops;
8449
8450 /* Shared helper function (MI and CLI) for creating and installing
8451 a shared object event catchpoint. If IS_LOAD is non-zero then
8452 the events to be caught are load events, otherwise they are
8453 unload events. If IS_TEMP is non-zero the catchpoint is a
8454 temporary one. If ENABLED is non-zero the catchpoint is
8455 created in an enabled state. */
8456
8457 void
8458 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8459 {
8460 struct gdbarch *gdbarch = get_current_arch ();
8461
8462 if (!arg)
8463 arg = "";
8464 arg = skip_spaces_const (arg);
8465
8466 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8467
8468 if (*arg != '\0')
8469 {
8470 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8471 _("Invalid regexp")));
8472 c->regex = xstrdup (arg);
8473 }
8474
8475 c->is_load = is_load;
8476 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8477 &catch_solib_breakpoint_ops);
8478
8479 c->enable_state = enabled ? bp_enabled : bp_disabled;
8480
8481 install_breakpoint (0, std::move (c), 1);
8482 }
8483
8484 /* A helper function that does all the work for "catch load" and
8485 "catch unload". */
8486
8487 static void
8488 catch_load_or_unload (char *arg, int from_tty, int is_load,
8489 struct cmd_list_element *command)
8490 {
8491 int tempflag;
8492 const int enabled = 1;
8493
8494 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8495
8496 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8497 }
8498
8499 static void
8500 catch_load_command_1 (char *arg, int from_tty,
8501 struct cmd_list_element *command)
8502 {
8503 catch_load_or_unload (arg, from_tty, 1, command);
8504 }
8505
8506 static void
8507 catch_unload_command_1 (char *arg, int from_tty,
8508 struct cmd_list_element *command)
8509 {
8510 catch_load_or_unload (arg, from_tty, 0, command);
8511 }
8512
8513 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8514 is non-zero, then make the breakpoint temporary. If COND_STRING is
8515 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8516 the breakpoint_ops structure associated to the catchpoint. */
8517
8518 void
8519 init_catchpoint (struct breakpoint *b,
8520 struct gdbarch *gdbarch, int tempflag,
8521 const char *cond_string,
8522 const struct breakpoint_ops *ops)
8523 {
8524 symtab_and_line sal;
8525 sal.pspace = current_program_space;
8526
8527 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8528
8529 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8530 b->disposition = tempflag ? disp_del : disp_donttouch;
8531 }
8532
8533 void
8534 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8535 {
8536 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8537 set_breakpoint_number (internal, b);
8538 if (is_tracepoint (b))
8539 set_tracepoint_count (breakpoint_count);
8540 if (!internal)
8541 mention (b);
8542 observer_notify_breakpoint_created (b);
8543
8544 if (update_gll)
8545 update_global_location_list (UGLL_MAY_INSERT);
8546 }
8547
8548 static void
8549 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8550 int tempflag, const char *cond_string,
8551 const struct breakpoint_ops *ops)
8552 {
8553 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8554
8555 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8556
8557 c->forked_inferior_pid = null_ptid;
8558
8559 install_breakpoint (0, std::move (c), 1);
8560 }
8561
8562 /* Exec catchpoints. */
8563
8564 /* An instance of this type is used to represent an exec catchpoint.
8565 A breakpoint is really of this type iff its ops pointer points to
8566 CATCH_EXEC_BREAKPOINT_OPS. */
8567
8568 struct exec_catchpoint : public breakpoint
8569 {
8570 ~exec_catchpoint () override;
8571
8572 /* Filename of a program whose exec triggered this catchpoint.
8573 This field is only valid immediately after this catchpoint has
8574 triggered. */
8575 char *exec_pathname;
8576 };
8577
8578 /* Exec catchpoint destructor. */
8579
8580 exec_catchpoint::~exec_catchpoint ()
8581 {
8582 xfree (this->exec_pathname);
8583 }
8584
8585 static int
8586 insert_catch_exec (struct bp_location *bl)
8587 {
8588 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8589 }
8590
8591 static int
8592 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8593 {
8594 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8595 }
8596
8597 static int
8598 breakpoint_hit_catch_exec (const struct bp_location *bl,
8599 struct address_space *aspace, CORE_ADDR bp_addr,
8600 const struct target_waitstatus *ws)
8601 {
8602 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8603
8604 if (ws->kind != TARGET_WAITKIND_EXECD)
8605 return 0;
8606
8607 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8608 return 1;
8609 }
8610
8611 static enum print_stop_action
8612 print_it_catch_exec (bpstat bs)
8613 {
8614 struct ui_out *uiout = current_uiout;
8615 struct breakpoint *b = bs->breakpoint_at;
8616 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8617
8618 annotate_catchpoint (b->number);
8619 maybe_print_thread_hit_breakpoint (uiout);
8620 if (b->disposition == disp_del)
8621 uiout->text ("Temporary catchpoint ");
8622 else
8623 uiout->text ("Catchpoint ");
8624 if (uiout->is_mi_like_p ())
8625 {
8626 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8627 uiout->field_string ("disp", bpdisp_text (b->disposition));
8628 }
8629 uiout->field_int ("bkptno", b->number);
8630 uiout->text (" (exec'd ");
8631 uiout->field_string ("new-exec", c->exec_pathname);
8632 uiout->text ("), ");
8633
8634 return PRINT_SRC_AND_LOC;
8635 }
8636
8637 static void
8638 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8639 {
8640 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8641 struct value_print_options opts;
8642 struct ui_out *uiout = current_uiout;
8643
8644 get_user_print_options (&opts);
8645
8646 /* Field 4, the address, is omitted (which makes the columns
8647 not line up too nicely with the headers, but the effect
8648 is relatively readable). */
8649 if (opts.addressprint)
8650 uiout->field_skip ("addr");
8651 annotate_field (5);
8652 uiout->text ("exec");
8653 if (c->exec_pathname != NULL)
8654 {
8655 uiout->text (", program \"");
8656 uiout->field_string ("what", c->exec_pathname);
8657 uiout->text ("\" ");
8658 }
8659
8660 if (uiout->is_mi_like_p ())
8661 uiout->field_string ("catch-type", "exec");
8662 }
8663
8664 static void
8665 print_mention_catch_exec (struct breakpoint *b)
8666 {
8667 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8668 }
8669
8670 /* Implement the "print_recreate" breakpoint_ops method for exec
8671 catchpoints. */
8672
8673 static void
8674 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8675 {
8676 fprintf_unfiltered (fp, "catch exec");
8677 print_recreate_thread (b, fp);
8678 }
8679
8680 static struct breakpoint_ops catch_exec_breakpoint_ops;
8681
8682 static int
8683 hw_breakpoint_used_count (void)
8684 {
8685 int i = 0;
8686 struct breakpoint *b;
8687 struct bp_location *bl;
8688
8689 ALL_BREAKPOINTS (b)
8690 {
8691 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8692 for (bl = b->loc; bl; bl = bl->next)
8693 {
8694 /* Special types of hardware breakpoints may use more than
8695 one register. */
8696 i += b->ops->resources_needed (bl);
8697 }
8698 }
8699
8700 return i;
8701 }
8702
8703 /* Returns the resources B would use if it were a hardware
8704 watchpoint. */
8705
8706 static int
8707 hw_watchpoint_use_count (struct breakpoint *b)
8708 {
8709 int i = 0;
8710 struct bp_location *bl;
8711
8712 if (!breakpoint_enabled (b))
8713 return 0;
8714
8715 for (bl = b->loc; bl; bl = bl->next)
8716 {
8717 /* Special types of hardware watchpoints may use more than
8718 one register. */
8719 i += b->ops->resources_needed (bl);
8720 }
8721
8722 return i;
8723 }
8724
8725 /* Returns the sum the used resources of all hardware watchpoints of
8726 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8727 the sum of the used resources of all hardware watchpoints of other
8728 types _not_ TYPE. */
8729
8730 static int
8731 hw_watchpoint_used_count_others (struct breakpoint *except,
8732 enum bptype type, int *other_type_used)
8733 {
8734 int i = 0;
8735 struct breakpoint *b;
8736
8737 *other_type_used = 0;
8738 ALL_BREAKPOINTS (b)
8739 {
8740 if (b == except)
8741 continue;
8742 if (!breakpoint_enabled (b))
8743 continue;
8744
8745 if (b->type == type)
8746 i += hw_watchpoint_use_count (b);
8747 else if (is_hardware_watchpoint (b))
8748 *other_type_used = 1;
8749 }
8750
8751 return i;
8752 }
8753
8754 void
8755 disable_watchpoints_before_interactive_call_start (void)
8756 {
8757 struct breakpoint *b;
8758
8759 ALL_BREAKPOINTS (b)
8760 {
8761 if (is_watchpoint (b) && breakpoint_enabled (b))
8762 {
8763 b->enable_state = bp_call_disabled;
8764 update_global_location_list (UGLL_DONT_INSERT);
8765 }
8766 }
8767 }
8768
8769 void
8770 enable_watchpoints_after_interactive_call_stop (void)
8771 {
8772 struct breakpoint *b;
8773
8774 ALL_BREAKPOINTS (b)
8775 {
8776 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8777 {
8778 b->enable_state = bp_enabled;
8779 update_global_location_list (UGLL_MAY_INSERT);
8780 }
8781 }
8782 }
8783
8784 void
8785 disable_breakpoints_before_startup (void)
8786 {
8787 current_program_space->executing_startup = 1;
8788 update_global_location_list (UGLL_DONT_INSERT);
8789 }
8790
8791 void
8792 enable_breakpoints_after_startup (void)
8793 {
8794 current_program_space->executing_startup = 0;
8795 breakpoint_re_set ();
8796 }
8797
8798 /* Create a new single-step breakpoint for thread THREAD, with no
8799 locations. */
8800
8801 static struct breakpoint *
8802 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8803 {
8804 std::unique_ptr<breakpoint> b (new breakpoint ());
8805
8806 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8807 &momentary_breakpoint_ops);
8808
8809 b->disposition = disp_donttouch;
8810 b->frame_id = null_frame_id;
8811
8812 b->thread = thread;
8813 gdb_assert (b->thread != 0);
8814
8815 return add_to_breakpoint_chain (std::move (b));
8816 }
8817
8818 /* Set a momentary breakpoint of type TYPE at address specified by
8819 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8820 frame. */
8821
8822 struct breakpoint *
8823 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8824 struct frame_id frame_id, enum bptype type)
8825 {
8826 struct breakpoint *b;
8827
8828 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8829 tail-called one. */
8830 gdb_assert (!frame_id_artificial_p (frame_id));
8831
8832 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8833 b->enable_state = bp_enabled;
8834 b->disposition = disp_donttouch;
8835 b->frame_id = frame_id;
8836
8837 /* If we're debugging a multi-threaded program, then we want
8838 momentary breakpoints to be active in only a single thread of
8839 control. */
8840 if (in_thread_list (inferior_ptid))
8841 b->thread = ptid_to_global_thread_id (inferior_ptid);
8842
8843 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8844
8845 return b;
8846 }
8847
8848 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8849 The new breakpoint will have type TYPE, use OPS as its
8850 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8851
8852 static struct breakpoint *
8853 momentary_breakpoint_from_master (struct breakpoint *orig,
8854 enum bptype type,
8855 const struct breakpoint_ops *ops,
8856 int loc_enabled)
8857 {
8858 struct breakpoint *copy;
8859
8860 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8861 copy->loc = allocate_bp_location (copy);
8862 set_breakpoint_location_function (copy->loc, 1);
8863
8864 copy->loc->gdbarch = orig->loc->gdbarch;
8865 copy->loc->requested_address = orig->loc->requested_address;
8866 copy->loc->address = orig->loc->address;
8867 copy->loc->section = orig->loc->section;
8868 copy->loc->pspace = orig->loc->pspace;
8869 copy->loc->probe = orig->loc->probe;
8870 copy->loc->line_number = orig->loc->line_number;
8871 copy->loc->symtab = orig->loc->symtab;
8872 copy->loc->enabled = loc_enabled;
8873 copy->frame_id = orig->frame_id;
8874 copy->thread = orig->thread;
8875 copy->pspace = orig->pspace;
8876
8877 copy->enable_state = bp_enabled;
8878 copy->disposition = disp_donttouch;
8879 copy->number = internal_breakpoint_number--;
8880
8881 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8882 return copy;
8883 }
8884
8885 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8886 ORIG is NULL. */
8887
8888 struct breakpoint *
8889 clone_momentary_breakpoint (struct breakpoint *orig)
8890 {
8891 /* If there's nothing to clone, then return nothing. */
8892 if (orig == NULL)
8893 return NULL;
8894
8895 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8896 }
8897
8898 struct breakpoint *
8899 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8900 enum bptype type)
8901 {
8902 struct symtab_and_line sal;
8903
8904 sal = find_pc_line (pc, 0);
8905 sal.pc = pc;
8906 sal.section = find_pc_overlay (pc);
8907 sal.explicit_pc = 1;
8908
8909 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8910 }
8911 \f
8912
8913 /* Tell the user we have just set a breakpoint B. */
8914
8915 static void
8916 mention (struct breakpoint *b)
8917 {
8918 b->ops->print_mention (b);
8919 if (current_uiout->is_mi_like_p ())
8920 return;
8921 printf_filtered ("\n");
8922 }
8923 \f
8924
8925 static int bp_loc_is_permanent (struct bp_location *loc);
8926
8927 static struct bp_location *
8928 add_location_to_breakpoint (struct breakpoint *b,
8929 const struct symtab_and_line *sal)
8930 {
8931 struct bp_location *loc, **tmp;
8932 CORE_ADDR adjusted_address;
8933 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8934
8935 if (loc_gdbarch == NULL)
8936 loc_gdbarch = b->gdbarch;
8937
8938 /* Adjust the breakpoint's address prior to allocating a location.
8939 Once we call allocate_bp_location(), that mostly uninitialized
8940 location will be placed on the location chain. Adjustment of the
8941 breakpoint may cause target_read_memory() to be called and we do
8942 not want its scan of the location chain to find a breakpoint and
8943 location that's only been partially initialized. */
8944 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8945 sal->pc, b->type);
8946
8947 /* Sort the locations by their ADDRESS. */
8948 loc = allocate_bp_location (b);
8949 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8950 tmp = &((*tmp)->next))
8951 ;
8952 loc->next = *tmp;
8953 *tmp = loc;
8954
8955 loc->requested_address = sal->pc;
8956 loc->address = adjusted_address;
8957 loc->pspace = sal->pspace;
8958 loc->probe.probe = sal->probe;
8959 loc->probe.objfile = sal->objfile;
8960 gdb_assert (loc->pspace != NULL);
8961 loc->section = sal->section;
8962 loc->gdbarch = loc_gdbarch;
8963 loc->line_number = sal->line;
8964 loc->symtab = sal->symtab;
8965
8966 set_breakpoint_location_function (loc,
8967 sal->explicit_pc || sal->explicit_line);
8968
8969 /* While by definition, permanent breakpoints are already present in the
8970 code, we don't mark the location as inserted. Normally one would expect
8971 that GDB could rely on that breakpoint instruction to stop the program,
8972 thus removing the need to insert its own breakpoint, except that executing
8973 the breakpoint instruction can kill the target instead of reporting a
8974 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8975 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8976 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8977 breakpoint be inserted normally results in QEMU knowing about the GDB
8978 breakpoint, and thus trap before the breakpoint instruction is executed.
8979 (If GDB later needs to continue execution past the permanent breakpoint,
8980 it manually increments the PC, thus avoiding executing the breakpoint
8981 instruction.) */
8982 if (bp_loc_is_permanent (loc))
8983 loc->permanent = 1;
8984
8985 return loc;
8986 }
8987 \f
8988
8989 /* See breakpoint.h. */
8990
8991 int
8992 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8993 {
8994 int len;
8995 CORE_ADDR addr;
8996 const gdb_byte *bpoint;
8997 gdb_byte *target_mem;
8998 struct cleanup *cleanup;
8999 int retval = 0;
9000
9001 addr = address;
9002 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9003
9004 /* Software breakpoints unsupported? */
9005 if (bpoint == NULL)
9006 return 0;
9007
9008 target_mem = (gdb_byte *) alloca (len);
9009
9010 /* Enable the automatic memory restoration from breakpoints while
9011 we read the memory. Otherwise we could say about our temporary
9012 breakpoints they are permanent. */
9013 cleanup = make_show_memory_breakpoints_cleanup (0);
9014
9015 if (target_read_memory (address, target_mem, len) == 0
9016 && memcmp (target_mem, bpoint, len) == 0)
9017 retval = 1;
9018
9019 do_cleanups (cleanup);
9020
9021 return retval;
9022 }
9023
9024 /* Return 1 if LOC is pointing to a permanent breakpoint,
9025 return 0 otherwise. */
9026
9027 static int
9028 bp_loc_is_permanent (struct bp_location *loc)
9029 {
9030 gdb_assert (loc != NULL);
9031
9032 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9033 attempt to read from the addresses the locations of these breakpoint types
9034 point to. program_breakpoint_here_p, below, will attempt to read
9035 memory. */
9036 if (!breakpoint_address_is_meaningful (loc->owner))
9037 return 0;
9038
9039 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9040 switch_to_program_space_and_thread (loc->pspace);
9041 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9042 }
9043
9044 /* Build a command list for the dprintf corresponding to the current
9045 settings of the dprintf style options. */
9046
9047 static void
9048 update_dprintf_command_list (struct breakpoint *b)
9049 {
9050 char *dprintf_args = b->extra_string;
9051 char *printf_line = NULL;
9052
9053 if (!dprintf_args)
9054 return;
9055
9056 dprintf_args = skip_spaces (dprintf_args);
9057
9058 /* Allow a comma, as it may have terminated a location, but don't
9059 insist on it. */
9060 if (*dprintf_args == ',')
9061 ++dprintf_args;
9062 dprintf_args = skip_spaces (dprintf_args);
9063
9064 if (*dprintf_args != '"')
9065 error (_("Bad format string, missing '\"'."));
9066
9067 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9068 printf_line = xstrprintf ("printf %s", dprintf_args);
9069 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9070 {
9071 if (!dprintf_function)
9072 error (_("No function supplied for dprintf call"));
9073
9074 if (dprintf_channel && strlen (dprintf_channel) > 0)
9075 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9076 dprintf_function,
9077 dprintf_channel,
9078 dprintf_args);
9079 else
9080 printf_line = xstrprintf ("call (void) %s (%s)",
9081 dprintf_function,
9082 dprintf_args);
9083 }
9084 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9085 {
9086 if (target_can_run_breakpoint_commands ())
9087 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9088 else
9089 {
9090 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9091 printf_line = xstrprintf ("printf %s", dprintf_args);
9092 }
9093 }
9094 else
9095 internal_error (__FILE__, __LINE__,
9096 _("Invalid dprintf style."));
9097
9098 gdb_assert (printf_line != NULL);
9099 /* Manufacture a printf sequence. */
9100 {
9101 struct command_line *printf_cmd_line = XNEW (struct command_line);
9102
9103 printf_cmd_line->control_type = simple_control;
9104 printf_cmd_line->body_count = 0;
9105 printf_cmd_line->body_list = NULL;
9106 printf_cmd_line->next = NULL;
9107 printf_cmd_line->line = printf_line;
9108
9109 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9110 }
9111 }
9112
9113 /* Update all dprintf commands, making their command lists reflect
9114 current style settings. */
9115
9116 static void
9117 update_dprintf_commands (char *args, int from_tty,
9118 struct cmd_list_element *c)
9119 {
9120 struct breakpoint *b;
9121
9122 ALL_BREAKPOINTS (b)
9123 {
9124 if (b->type == bp_dprintf)
9125 update_dprintf_command_list (b);
9126 }
9127 }
9128
9129 /* Create a breakpoint with SAL as location. Use LOCATION
9130 as a description of the location, and COND_STRING
9131 as condition expression. If LOCATION is NULL then create an
9132 "address location" from the address in the SAL. */
9133
9134 static void
9135 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9136 gdb::array_view<const symtab_and_line> sals,
9137 event_location_up &&location,
9138 gdb::unique_xmalloc_ptr<char> filter,
9139 gdb::unique_xmalloc_ptr<char> cond_string,
9140 gdb::unique_xmalloc_ptr<char> extra_string,
9141 enum bptype type, enum bpdisp disposition,
9142 int thread, int task, int ignore_count,
9143 const struct breakpoint_ops *ops, int from_tty,
9144 int enabled, int internal, unsigned flags,
9145 int display_canonical)
9146 {
9147 int i;
9148
9149 if (type == bp_hardware_breakpoint)
9150 {
9151 int target_resources_ok;
9152
9153 i = hw_breakpoint_used_count ();
9154 target_resources_ok =
9155 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9156 i + 1, 0);
9157 if (target_resources_ok == 0)
9158 error (_("No hardware breakpoint support in the target."));
9159 else if (target_resources_ok < 0)
9160 error (_("Hardware breakpoints used exceeds limit."));
9161 }
9162
9163 gdb_assert (!sals.empty ());
9164
9165 for (const auto &sal : sals)
9166 {
9167 struct bp_location *loc;
9168
9169 if (from_tty)
9170 {
9171 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9172 if (!loc_gdbarch)
9173 loc_gdbarch = gdbarch;
9174
9175 describe_other_breakpoints (loc_gdbarch,
9176 sal.pspace, sal.pc, sal.section, thread);
9177 }
9178
9179 if (&sal == &sals[0])
9180 {
9181 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9182 b->thread = thread;
9183 b->task = task;
9184
9185 b->cond_string = cond_string.release ();
9186 b->extra_string = extra_string.release ();
9187 b->ignore_count = ignore_count;
9188 b->enable_state = enabled ? bp_enabled : bp_disabled;
9189 b->disposition = disposition;
9190
9191 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9192 b->loc->inserted = 1;
9193
9194 if (type == bp_static_tracepoint)
9195 {
9196 struct tracepoint *t = (struct tracepoint *) b;
9197 struct static_tracepoint_marker marker;
9198
9199 if (strace_marker_p (b))
9200 {
9201 /* We already know the marker exists, otherwise, we
9202 wouldn't see a sal for it. */
9203 const char *p
9204 = &event_location_to_string (b->location.get ())[3];
9205 const char *endp;
9206 char *marker_str;
9207
9208 p = skip_spaces_const (p);
9209
9210 endp = skip_to_space_const (p);
9211
9212 marker_str = savestring (p, endp - p);
9213 t->static_trace_marker_id = marker_str;
9214
9215 printf_filtered (_("Probed static tracepoint "
9216 "marker \"%s\"\n"),
9217 t->static_trace_marker_id);
9218 }
9219 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9220 {
9221 t->static_trace_marker_id = xstrdup (marker.str_id);
9222 release_static_tracepoint_marker (&marker);
9223
9224 printf_filtered (_("Probed static tracepoint "
9225 "marker \"%s\"\n"),
9226 t->static_trace_marker_id);
9227 }
9228 else
9229 warning (_("Couldn't determine the static "
9230 "tracepoint marker to probe"));
9231 }
9232
9233 loc = b->loc;
9234 }
9235 else
9236 {
9237 loc = add_location_to_breakpoint (b, &sal);
9238 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9239 loc->inserted = 1;
9240 }
9241
9242 if (b->cond_string)
9243 {
9244 const char *arg = b->cond_string;
9245
9246 loc->cond = parse_exp_1 (&arg, loc->address,
9247 block_for_pc (loc->address), 0);
9248 if (*arg)
9249 error (_("Garbage '%s' follows condition"), arg);
9250 }
9251
9252 /* Dynamic printf requires and uses additional arguments on the
9253 command line, otherwise it's an error. */
9254 if (type == bp_dprintf)
9255 {
9256 if (b->extra_string)
9257 update_dprintf_command_list (b);
9258 else
9259 error (_("Format string required"));
9260 }
9261 else if (b->extra_string)
9262 error (_("Garbage '%s' at end of command"), b->extra_string);
9263 }
9264
9265 b->display_canonical = display_canonical;
9266 if (location != NULL)
9267 b->location = std::move (location);
9268 else
9269 b->location = new_address_location (b->loc->address, NULL, 0);
9270 b->filter = filter.release ();
9271 }
9272
9273 static void
9274 create_breakpoint_sal (struct gdbarch *gdbarch,
9275 gdb::array_view<const symtab_and_line> sals,
9276 event_location_up &&location,
9277 gdb::unique_xmalloc_ptr<char> filter,
9278 gdb::unique_xmalloc_ptr<char> cond_string,
9279 gdb::unique_xmalloc_ptr<char> extra_string,
9280 enum bptype type, enum bpdisp disposition,
9281 int thread, int task, int ignore_count,
9282 const struct breakpoint_ops *ops, int from_tty,
9283 int enabled, int internal, unsigned flags,
9284 int display_canonical)
9285 {
9286 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9287
9288 init_breakpoint_sal (b.get (), gdbarch,
9289 sals, std::move (location),
9290 std::move (filter),
9291 std::move (cond_string),
9292 std::move (extra_string),
9293 type, disposition,
9294 thread, task, ignore_count,
9295 ops, from_tty,
9296 enabled, internal, flags,
9297 display_canonical);
9298
9299 install_breakpoint (internal, std::move (b), 0);
9300 }
9301
9302 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9303 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9304 value. COND_STRING, if not NULL, specified the condition to be
9305 used for all breakpoints. Essentially the only case where
9306 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9307 function. In that case, it's still not possible to specify
9308 separate conditions for different overloaded functions, so
9309 we take just a single condition string.
9310
9311 NOTE: If the function succeeds, the caller is expected to cleanup
9312 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9313 array contents). If the function fails (error() is called), the
9314 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9315 COND and SALS arrays and each of those arrays contents. */
9316
9317 static void
9318 create_breakpoints_sal (struct gdbarch *gdbarch,
9319 struct linespec_result *canonical,
9320 gdb::unique_xmalloc_ptr<char> cond_string,
9321 gdb::unique_xmalloc_ptr<char> extra_string,
9322 enum bptype type, enum bpdisp disposition,
9323 int thread, int task, int ignore_count,
9324 const struct breakpoint_ops *ops, int from_tty,
9325 int enabled, int internal, unsigned flags)
9326 {
9327 if (canonical->pre_expanded)
9328 gdb_assert (canonical->lsals.size () == 1);
9329
9330 for (const auto &lsal : canonical->lsals)
9331 {
9332 /* Note that 'location' can be NULL in the case of a plain
9333 'break', without arguments. */
9334 event_location_up location
9335 = (canonical->location != NULL
9336 ? copy_event_location (canonical->location.get ()) : NULL);
9337 gdb::unique_xmalloc_ptr<char> filter_string
9338 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9339
9340 create_breakpoint_sal (gdbarch, lsal.sals,
9341 std::move (location),
9342 std::move (filter_string),
9343 std::move (cond_string),
9344 std::move (extra_string),
9345 type, disposition,
9346 thread, task, ignore_count, ops,
9347 from_tty, enabled, internal, flags,
9348 canonical->special_display);
9349 }
9350 }
9351
9352 /* Parse LOCATION which is assumed to be a SAL specification possibly
9353 followed by conditionals. On return, SALS contains an array of SAL
9354 addresses found. LOCATION points to the end of the SAL (for
9355 linespec locations).
9356
9357 The array and the line spec strings are allocated on the heap, it is
9358 the caller's responsibility to free them. */
9359
9360 static void
9361 parse_breakpoint_sals (const struct event_location *location,
9362 struct linespec_result *canonical)
9363 {
9364 struct symtab_and_line cursal;
9365
9366 if (event_location_type (location) == LINESPEC_LOCATION)
9367 {
9368 const char *address = get_linespec_location (location);
9369
9370 if (address == NULL)
9371 {
9372 /* The last displayed codepoint, if it's valid, is our default
9373 breakpoint address. */
9374 if (last_displayed_sal_is_valid ())
9375 {
9376 /* Set sal's pspace, pc, symtab, and line to the values
9377 corresponding to the last call to print_frame_info.
9378 Be sure to reinitialize LINE with NOTCURRENT == 0
9379 as the breakpoint line number is inappropriate otherwise.
9380 find_pc_line would adjust PC, re-set it back. */
9381 symtab_and_line sal = get_last_displayed_sal ();
9382 CORE_ADDR pc = sal.pc;
9383
9384 sal = find_pc_line (pc, 0);
9385
9386 /* "break" without arguments is equivalent to "break *PC"
9387 where PC is the last displayed codepoint's address. So
9388 make sure to set sal.explicit_pc to prevent GDB from
9389 trying to expand the list of sals to include all other
9390 instances with the same symtab and line. */
9391 sal.pc = pc;
9392 sal.explicit_pc = 1;
9393
9394 struct linespec_sals lsal;
9395 lsal.sals = {sal};
9396 lsal.canonical = NULL;
9397
9398 canonical->lsals.push_back (std::move (lsal));
9399 return;
9400 }
9401 else
9402 error (_("No default breakpoint address now."));
9403 }
9404 }
9405
9406 /* Force almost all breakpoints to be in terms of the
9407 current_source_symtab (which is decode_line_1's default).
9408 This should produce the results we want almost all of the
9409 time while leaving default_breakpoint_* alone.
9410
9411 ObjC: However, don't match an Objective-C method name which
9412 may have a '+' or '-' succeeded by a '['. */
9413 cursal = get_current_source_symtab_and_line ();
9414 if (last_displayed_sal_is_valid ())
9415 {
9416 const char *address = NULL;
9417
9418 if (event_location_type (location) == LINESPEC_LOCATION)
9419 address = get_linespec_location (location);
9420
9421 if (!cursal.symtab
9422 || (address != NULL
9423 && strchr ("+-", address[0]) != NULL
9424 && address[1] != '['))
9425 {
9426 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9427 get_last_displayed_symtab (),
9428 get_last_displayed_line (),
9429 canonical, NULL, NULL);
9430 return;
9431 }
9432 }
9433
9434 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9435 cursal.symtab, cursal.line, canonical, NULL, NULL);
9436 }
9437
9438
9439 /* Convert each SAL into a real PC. Verify that the PC can be
9440 inserted as a breakpoint. If it can't throw an error. */
9441
9442 static void
9443 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9444 {
9445 for (auto &sal : sals)
9446 resolve_sal_pc (&sal);
9447 }
9448
9449 /* Fast tracepoints may have restrictions on valid locations. For
9450 instance, a fast tracepoint using a jump instead of a trap will
9451 likely have to overwrite more bytes than a trap would, and so can
9452 only be placed where the instruction is longer than the jump, or a
9453 multi-instruction sequence does not have a jump into the middle of
9454 it, etc. */
9455
9456 static void
9457 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9458 gdb::array_view<const symtab_and_line> sals)
9459 {
9460 int rslt;
9461 char *msg;
9462 struct cleanup *old_chain;
9463
9464 for (const auto &sal : sals)
9465 {
9466 struct gdbarch *sarch;
9467
9468 sarch = get_sal_arch (sal);
9469 /* We fall back to GDBARCH if there is no architecture
9470 associated with SAL. */
9471 if (sarch == NULL)
9472 sarch = gdbarch;
9473 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9474 old_chain = make_cleanup (xfree, msg);
9475
9476 if (!rslt)
9477 error (_("May not have a fast tracepoint at %s%s"),
9478 paddress (sarch, sal.pc), (msg ? msg : ""));
9479
9480 do_cleanups (old_chain);
9481 }
9482 }
9483
9484 /* Given TOK, a string specification of condition and thread, as
9485 accepted by the 'break' command, extract the condition
9486 string and thread number and set *COND_STRING and *THREAD.
9487 PC identifies the context at which the condition should be parsed.
9488 If no condition is found, *COND_STRING is set to NULL.
9489 If no thread is found, *THREAD is set to -1. */
9490
9491 static void
9492 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9493 char **cond_string, int *thread, int *task,
9494 char **rest)
9495 {
9496 *cond_string = NULL;
9497 *thread = -1;
9498 *task = 0;
9499 *rest = NULL;
9500
9501 while (tok && *tok)
9502 {
9503 const char *end_tok;
9504 int toklen;
9505 const char *cond_start = NULL;
9506 const char *cond_end = NULL;
9507
9508 tok = skip_spaces_const (tok);
9509
9510 if ((*tok == '"' || *tok == ',') && rest)
9511 {
9512 *rest = savestring (tok, strlen (tok));
9513 return;
9514 }
9515
9516 end_tok = skip_to_space_const (tok);
9517
9518 toklen = end_tok - tok;
9519
9520 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9521 {
9522 tok = cond_start = end_tok + 1;
9523 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9524 cond_end = tok;
9525 *cond_string = savestring (cond_start, cond_end - cond_start);
9526 }
9527 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9528 {
9529 const char *tmptok;
9530 struct thread_info *thr;
9531
9532 tok = end_tok + 1;
9533 thr = parse_thread_id (tok, &tmptok);
9534 if (tok == tmptok)
9535 error (_("Junk after thread keyword."));
9536 *thread = thr->global_num;
9537 tok = tmptok;
9538 }
9539 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9540 {
9541 char *tmptok;
9542
9543 tok = end_tok + 1;
9544 *task = strtol (tok, &tmptok, 0);
9545 if (tok == tmptok)
9546 error (_("Junk after task keyword."));
9547 if (!valid_task_id (*task))
9548 error (_("Unknown task %d."), *task);
9549 tok = tmptok;
9550 }
9551 else if (rest)
9552 {
9553 *rest = savestring (tok, strlen (tok));
9554 return;
9555 }
9556 else
9557 error (_("Junk at end of arguments."));
9558 }
9559 }
9560
9561 /* Decode a static tracepoint marker spec. */
9562
9563 static std::vector<symtab_and_line>
9564 decode_static_tracepoint_spec (const char **arg_p)
9565 {
9566 VEC(static_tracepoint_marker_p) *markers = NULL;
9567 struct cleanup *old_chain;
9568 const char *p = &(*arg_p)[3];
9569 const char *endp;
9570 char *marker_str;
9571 int i;
9572
9573 p = skip_spaces_const (p);
9574
9575 endp = skip_to_space_const (p);
9576
9577 marker_str = savestring (p, endp - p);
9578 old_chain = make_cleanup (xfree, marker_str);
9579
9580 markers = target_static_tracepoint_markers_by_strid (marker_str);
9581 if (VEC_empty(static_tracepoint_marker_p, markers))
9582 error (_("No known static tracepoint marker named %s"), marker_str);
9583
9584 std::vector<symtab_and_line> sals;
9585 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9586
9587 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9588 {
9589 struct static_tracepoint_marker *marker;
9590
9591 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9592
9593 symtab_and_line sal = find_pc_line (marker->address, 0);
9594 sal.pc = marker->address;
9595 sals.push_back (sal);
9596
9597 release_static_tracepoint_marker (marker);
9598 }
9599
9600 do_cleanups (old_chain);
9601
9602 *arg_p = endp;
9603 return sals;
9604 }
9605
9606 /* See breakpoint.h. */
9607
9608 int
9609 create_breakpoint (struct gdbarch *gdbarch,
9610 const struct event_location *location,
9611 const char *cond_string,
9612 int thread, const char *extra_string,
9613 int parse_extra,
9614 int tempflag, enum bptype type_wanted,
9615 int ignore_count,
9616 enum auto_boolean pending_break_support,
9617 const struct breakpoint_ops *ops,
9618 int from_tty, int enabled, int internal,
9619 unsigned flags)
9620 {
9621 struct linespec_result canonical;
9622 struct cleanup *bkpt_chain = NULL;
9623 int pending = 0;
9624 int task = 0;
9625 int prev_bkpt_count = breakpoint_count;
9626
9627 gdb_assert (ops != NULL);
9628
9629 /* If extra_string isn't useful, set it to NULL. */
9630 if (extra_string != NULL && *extra_string == '\0')
9631 extra_string = NULL;
9632
9633 TRY
9634 {
9635 ops->create_sals_from_location (location, &canonical, type_wanted);
9636 }
9637 CATCH (e, RETURN_MASK_ERROR)
9638 {
9639 /* If caller is interested in rc value from parse, set
9640 value. */
9641 if (e.error == NOT_FOUND_ERROR)
9642 {
9643 /* If pending breakpoint support is turned off, throw
9644 error. */
9645
9646 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9647 throw_exception (e);
9648
9649 exception_print (gdb_stderr, e);
9650
9651 /* If pending breakpoint support is auto query and the user
9652 selects no, then simply return the error code. */
9653 if (pending_break_support == AUTO_BOOLEAN_AUTO
9654 && !nquery (_("Make %s pending on future shared library load? "),
9655 bptype_string (type_wanted)))
9656 return 0;
9657
9658 /* At this point, either the user was queried about setting
9659 a pending breakpoint and selected yes, or pending
9660 breakpoint behavior is on and thus a pending breakpoint
9661 is defaulted on behalf of the user. */
9662 pending = 1;
9663 }
9664 else
9665 throw_exception (e);
9666 }
9667 END_CATCH
9668
9669 if (!pending && canonical.lsals.empty ())
9670 return 0;
9671
9672 /* ----------------------------- SNIP -----------------------------
9673 Anything added to the cleanup chain beyond this point is assumed
9674 to be part of a breakpoint. If the breakpoint create succeeds
9675 then the memory is not reclaimed. */
9676 bkpt_chain = make_cleanup (null_cleanup, 0);
9677
9678 /* Resolve all line numbers to PC's and verify that the addresses
9679 are ok for the target. */
9680 if (!pending)
9681 {
9682 for (auto &lsal : canonical.lsals)
9683 breakpoint_sals_to_pc (lsal.sals);
9684 }
9685
9686 /* Fast tracepoints may have additional restrictions on location. */
9687 if (!pending && type_wanted == bp_fast_tracepoint)
9688 {
9689 for (const auto &lsal : canonical.lsals)
9690 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9691 }
9692
9693 /* Verify that condition can be parsed, before setting any
9694 breakpoints. Allocate a separate condition expression for each
9695 breakpoint. */
9696 if (!pending)
9697 {
9698 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9699 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9700
9701 if (parse_extra)
9702 {
9703 char *rest;
9704 char *cond;
9705
9706 const linespec_sals &lsal = canonical.lsals[0];
9707
9708 /* Here we only parse 'arg' to separate condition
9709 from thread number, so parsing in context of first
9710 sal is OK. When setting the breakpoint we'll
9711 re-parse it in context of each sal. */
9712
9713 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9714 &cond, &thread, &task, &rest);
9715 cond_string_copy.reset (cond);
9716 extra_string_copy.reset (rest);
9717 }
9718 else
9719 {
9720 if (type_wanted != bp_dprintf
9721 && extra_string != NULL && *extra_string != '\0')
9722 error (_("Garbage '%s' at end of location"), extra_string);
9723
9724 /* Create a private copy of condition string. */
9725 if (cond_string)
9726 cond_string_copy.reset (xstrdup (cond_string));
9727 /* Create a private copy of any extra string. */
9728 if (extra_string)
9729 extra_string_copy.reset (xstrdup (extra_string));
9730 }
9731
9732 ops->create_breakpoints_sal (gdbarch, &canonical,
9733 std::move (cond_string_copy),
9734 std::move (extra_string_copy),
9735 type_wanted,
9736 tempflag ? disp_del : disp_donttouch,
9737 thread, task, ignore_count, ops,
9738 from_tty, enabled, internal, flags);
9739 }
9740 else
9741 {
9742 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9743
9744 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9745 b->location = copy_event_location (location);
9746
9747 if (parse_extra)
9748 b->cond_string = NULL;
9749 else
9750 {
9751 /* Create a private copy of condition string. */
9752 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9753 b->thread = thread;
9754 }
9755
9756 /* Create a private copy of any extra string. */
9757 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9758 b->ignore_count = ignore_count;
9759 b->disposition = tempflag ? disp_del : disp_donttouch;
9760 b->condition_not_parsed = 1;
9761 b->enable_state = enabled ? bp_enabled : bp_disabled;
9762 if ((type_wanted != bp_breakpoint
9763 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9764 b->pspace = current_program_space;
9765
9766 install_breakpoint (internal, std::move (b), 0);
9767 }
9768
9769 if (canonical.lsals.size () > 1)
9770 {
9771 warning (_("Multiple breakpoints were set.\nUse the "
9772 "\"delete\" command to delete unwanted breakpoints."));
9773 prev_breakpoint_count = prev_bkpt_count;
9774 }
9775
9776 /* That's it. Discard the cleanups for data inserted into the
9777 breakpoint. */
9778 discard_cleanups (bkpt_chain);
9779
9780 /* error call may happen here - have BKPT_CHAIN already discarded. */
9781 update_global_location_list (UGLL_MAY_INSERT);
9782
9783 return 1;
9784 }
9785
9786 /* Set a breakpoint.
9787 ARG is a string describing breakpoint address,
9788 condition, and thread.
9789 FLAG specifies if a breakpoint is hardware on,
9790 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9791 and BP_TEMPFLAG. */
9792
9793 static void
9794 break_command_1 (char *arg, int flag, int from_tty)
9795 {
9796 int tempflag = flag & BP_TEMPFLAG;
9797 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9798 ? bp_hardware_breakpoint
9799 : bp_breakpoint);
9800 struct breakpoint_ops *ops;
9801
9802 event_location_up location = string_to_event_location (&arg, current_language);
9803
9804 /* Matching breakpoints on probes. */
9805 if (location != NULL
9806 && event_location_type (location.get ()) == PROBE_LOCATION)
9807 ops = &bkpt_probe_breakpoint_ops;
9808 else
9809 ops = &bkpt_breakpoint_ops;
9810
9811 create_breakpoint (get_current_arch (),
9812 location.get (),
9813 NULL, 0, arg, 1 /* parse arg */,
9814 tempflag, type_wanted,
9815 0 /* Ignore count */,
9816 pending_break_support,
9817 ops,
9818 from_tty,
9819 1 /* enabled */,
9820 0 /* internal */,
9821 0);
9822 }
9823
9824 /* Helper function for break_command_1 and disassemble_command. */
9825
9826 void
9827 resolve_sal_pc (struct symtab_and_line *sal)
9828 {
9829 CORE_ADDR pc;
9830
9831 if (sal->pc == 0 && sal->symtab != NULL)
9832 {
9833 if (!find_line_pc (sal->symtab, sal->line, &pc))
9834 error (_("No line %d in file \"%s\"."),
9835 sal->line, symtab_to_filename_for_display (sal->symtab));
9836 sal->pc = pc;
9837
9838 /* If this SAL corresponds to a breakpoint inserted using a line
9839 number, then skip the function prologue if necessary. */
9840 if (sal->explicit_line)
9841 skip_prologue_sal (sal);
9842 }
9843
9844 if (sal->section == 0 && sal->symtab != NULL)
9845 {
9846 const struct blockvector *bv;
9847 const struct block *b;
9848 struct symbol *sym;
9849
9850 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9851 SYMTAB_COMPUNIT (sal->symtab));
9852 if (bv != NULL)
9853 {
9854 sym = block_linkage_function (b);
9855 if (sym != NULL)
9856 {
9857 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9858 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9859 sym);
9860 }
9861 else
9862 {
9863 /* It really is worthwhile to have the section, so we'll
9864 just have to look harder. This case can be executed
9865 if we have line numbers but no functions (as can
9866 happen in assembly source). */
9867
9868 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9869 switch_to_program_space_and_thread (sal->pspace);
9870
9871 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9872 if (msym.minsym)
9873 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9874 }
9875 }
9876 }
9877 }
9878
9879 void
9880 break_command (char *arg, int from_tty)
9881 {
9882 break_command_1 (arg, 0, from_tty);
9883 }
9884
9885 void
9886 tbreak_command (char *arg, int from_tty)
9887 {
9888 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9889 }
9890
9891 static void
9892 hbreak_command (char *arg, int from_tty)
9893 {
9894 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9895 }
9896
9897 static void
9898 thbreak_command (char *arg, int from_tty)
9899 {
9900 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9901 }
9902
9903 static void
9904 stop_command (char *arg, int from_tty)
9905 {
9906 printf_filtered (_("Specify the type of breakpoint to set.\n\
9907 Usage: stop in <function | address>\n\
9908 stop at <line>\n"));
9909 }
9910
9911 static void
9912 stopin_command (char *arg, int from_tty)
9913 {
9914 int badInput = 0;
9915
9916 if (arg == (char *) NULL)
9917 badInput = 1;
9918 else if (*arg != '*')
9919 {
9920 char *argptr = arg;
9921 int hasColon = 0;
9922
9923 /* Look for a ':'. If this is a line number specification, then
9924 say it is bad, otherwise, it should be an address or
9925 function/method name. */
9926 while (*argptr && !hasColon)
9927 {
9928 hasColon = (*argptr == ':');
9929 argptr++;
9930 }
9931
9932 if (hasColon)
9933 badInput = (*argptr != ':'); /* Not a class::method */
9934 else
9935 badInput = isdigit (*arg); /* a simple line number */
9936 }
9937
9938 if (badInput)
9939 printf_filtered (_("Usage: stop in <function | address>\n"));
9940 else
9941 break_command_1 (arg, 0, from_tty);
9942 }
9943
9944 static void
9945 stopat_command (char *arg, int from_tty)
9946 {
9947 int badInput = 0;
9948
9949 if (arg == (char *) NULL || *arg == '*') /* no line number */
9950 badInput = 1;
9951 else
9952 {
9953 char *argptr = arg;
9954 int hasColon = 0;
9955
9956 /* Look for a ':'. If there is a '::' then get out, otherwise
9957 it is probably a line number. */
9958 while (*argptr && !hasColon)
9959 {
9960 hasColon = (*argptr == ':');
9961 argptr++;
9962 }
9963
9964 if (hasColon)
9965 badInput = (*argptr == ':'); /* we have class::method */
9966 else
9967 badInput = !isdigit (*arg); /* not a line number */
9968 }
9969
9970 if (badInput)
9971 printf_filtered (_("Usage: stop at <line>\n"));
9972 else
9973 break_command_1 (arg, 0, from_tty);
9974 }
9975
9976 /* The dynamic printf command is mostly like a regular breakpoint, but
9977 with a prewired command list consisting of a single output command,
9978 built from extra arguments supplied on the dprintf command
9979 line. */
9980
9981 static void
9982 dprintf_command (char *arg, int from_tty)
9983 {
9984 event_location_up location = string_to_event_location (&arg, current_language);
9985
9986 /* If non-NULL, ARG should have been advanced past the location;
9987 the next character must be ','. */
9988 if (arg != NULL)
9989 {
9990 if (arg[0] != ',' || arg[1] == '\0')
9991 error (_("Format string required"));
9992 else
9993 {
9994 /* Skip the comma. */
9995 ++arg;
9996 }
9997 }
9998
9999 create_breakpoint (get_current_arch (),
10000 location.get (),
10001 NULL, 0, arg, 1 /* parse arg */,
10002 0, bp_dprintf,
10003 0 /* Ignore count */,
10004 pending_break_support,
10005 &dprintf_breakpoint_ops,
10006 from_tty,
10007 1 /* enabled */,
10008 0 /* internal */,
10009 0);
10010 }
10011
10012 static void
10013 agent_printf_command (char *arg, int from_tty)
10014 {
10015 error (_("May only run agent-printf on the target"));
10016 }
10017
10018 /* Implement the "breakpoint_hit" breakpoint_ops method for
10019 ranged breakpoints. */
10020
10021 static int
10022 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10023 struct address_space *aspace,
10024 CORE_ADDR bp_addr,
10025 const struct target_waitstatus *ws)
10026 {
10027 if (ws->kind != TARGET_WAITKIND_STOPPED
10028 || ws->value.sig != GDB_SIGNAL_TRAP)
10029 return 0;
10030
10031 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10032 bl->length, aspace, bp_addr);
10033 }
10034
10035 /* Implement the "resources_needed" breakpoint_ops method for
10036 ranged breakpoints. */
10037
10038 static int
10039 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10040 {
10041 return target_ranged_break_num_registers ();
10042 }
10043
10044 /* Implement the "print_it" breakpoint_ops method for
10045 ranged breakpoints. */
10046
10047 static enum print_stop_action
10048 print_it_ranged_breakpoint (bpstat bs)
10049 {
10050 struct breakpoint *b = bs->breakpoint_at;
10051 struct bp_location *bl = b->loc;
10052 struct ui_out *uiout = current_uiout;
10053
10054 gdb_assert (b->type == bp_hardware_breakpoint);
10055
10056 /* Ranged breakpoints have only one location. */
10057 gdb_assert (bl && bl->next == NULL);
10058
10059 annotate_breakpoint (b->number);
10060
10061 maybe_print_thread_hit_breakpoint (uiout);
10062
10063 if (b->disposition == disp_del)
10064 uiout->text ("Temporary ranged breakpoint ");
10065 else
10066 uiout->text ("Ranged breakpoint ");
10067 if (uiout->is_mi_like_p ())
10068 {
10069 uiout->field_string ("reason",
10070 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10071 uiout->field_string ("disp", bpdisp_text (b->disposition));
10072 }
10073 uiout->field_int ("bkptno", b->number);
10074 uiout->text (", ");
10075
10076 return PRINT_SRC_AND_LOC;
10077 }
10078
10079 /* Implement the "print_one" breakpoint_ops method for
10080 ranged breakpoints. */
10081
10082 static void
10083 print_one_ranged_breakpoint (struct breakpoint *b,
10084 struct bp_location **last_loc)
10085 {
10086 struct bp_location *bl = b->loc;
10087 struct value_print_options opts;
10088 struct ui_out *uiout = current_uiout;
10089
10090 /* Ranged breakpoints have only one location. */
10091 gdb_assert (bl && bl->next == NULL);
10092
10093 get_user_print_options (&opts);
10094
10095 if (opts.addressprint)
10096 /* We don't print the address range here, it will be printed later
10097 by print_one_detail_ranged_breakpoint. */
10098 uiout->field_skip ("addr");
10099 annotate_field (5);
10100 print_breakpoint_location (b, bl);
10101 *last_loc = bl;
10102 }
10103
10104 /* Implement the "print_one_detail" breakpoint_ops method for
10105 ranged breakpoints. */
10106
10107 static void
10108 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10109 struct ui_out *uiout)
10110 {
10111 CORE_ADDR address_start, address_end;
10112 struct bp_location *bl = b->loc;
10113 string_file stb;
10114
10115 gdb_assert (bl);
10116
10117 address_start = bl->address;
10118 address_end = address_start + bl->length - 1;
10119
10120 uiout->text ("\taddress range: ");
10121 stb.printf ("[%s, %s]",
10122 print_core_address (bl->gdbarch, address_start),
10123 print_core_address (bl->gdbarch, address_end));
10124 uiout->field_stream ("addr", stb);
10125 uiout->text ("\n");
10126 }
10127
10128 /* Implement the "print_mention" breakpoint_ops method for
10129 ranged breakpoints. */
10130
10131 static void
10132 print_mention_ranged_breakpoint (struct breakpoint *b)
10133 {
10134 struct bp_location *bl = b->loc;
10135 struct ui_out *uiout = current_uiout;
10136
10137 gdb_assert (bl);
10138 gdb_assert (b->type == bp_hardware_breakpoint);
10139
10140 if (uiout->is_mi_like_p ())
10141 return;
10142
10143 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10144 b->number, paddress (bl->gdbarch, bl->address),
10145 paddress (bl->gdbarch, bl->address + bl->length - 1));
10146 }
10147
10148 /* Implement the "print_recreate" breakpoint_ops method for
10149 ranged breakpoints. */
10150
10151 static void
10152 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10153 {
10154 fprintf_unfiltered (fp, "break-range %s, %s",
10155 event_location_to_string (b->location.get ()),
10156 event_location_to_string (b->location_range_end.get ()));
10157 print_recreate_thread (b, fp);
10158 }
10159
10160 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10161
10162 static struct breakpoint_ops ranged_breakpoint_ops;
10163
10164 /* Find the address where the end of the breakpoint range should be
10165 placed, given the SAL of the end of the range. This is so that if
10166 the user provides a line number, the end of the range is set to the
10167 last instruction of the given line. */
10168
10169 static CORE_ADDR
10170 find_breakpoint_range_end (struct symtab_and_line sal)
10171 {
10172 CORE_ADDR end;
10173
10174 /* If the user provided a PC value, use it. Otherwise,
10175 find the address of the end of the given location. */
10176 if (sal.explicit_pc)
10177 end = sal.pc;
10178 else
10179 {
10180 int ret;
10181 CORE_ADDR start;
10182
10183 ret = find_line_pc_range (sal, &start, &end);
10184 if (!ret)
10185 error (_("Could not find location of the end of the range."));
10186
10187 /* find_line_pc_range returns the start of the next line. */
10188 end--;
10189 }
10190
10191 return end;
10192 }
10193
10194 /* Implement the "break-range" CLI command. */
10195
10196 static void
10197 break_range_command (char *arg, int from_tty)
10198 {
10199 char *arg_start, *addr_string_start;
10200 struct linespec_result canonical_start, canonical_end;
10201 int bp_count, can_use_bp, length;
10202 CORE_ADDR end;
10203 struct breakpoint *b;
10204 struct cleanup *cleanup_bkpt;
10205
10206 /* We don't support software ranged breakpoints. */
10207 if (target_ranged_break_num_registers () < 0)
10208 error (_("This target does not support hardware ranged breakpoints."));
10209
10210 bp_count = hw_breakpoint_used_count ();
10211 bp_count += target_ranged_break_num_registers ();
10212 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10213 bp_count, 0);
10214 if (can_use_bp < 0)
10215 error (_("Hardware breakpoints used exceeds limit."));
10216
10217 arg = skip_spaces (arg);
10218 if (arg == NULL || arg[0] == '\0')
10219 error(_("No address range specified."));
10220
10221 arg_start = arg;
10222 event_location_up start_location = string_to_event_location (&arg,
10223 current_language);
10224 parse_breakpoint_sals (start_location.get (), &canonical_start);
10225
10226 if (arg[0] != ',')
10227 error (_("Too few arguments."));
10228 else if (canonical_start.lsals.empty ())
10229 error (_("Could not find location of the beginning of the range."));
10230
10231 const linespec_sals &lsal_start = canonical_start.lsals[0];
10232
10233 if (canonical_start.lsals.size () > 1
10234 || lsal_start.sals.size () != 1)
10235 error (_("Cannot create a ranged breakpoint with multiple locations."));
10236
10237 const symtab_and_line &sal_start = lsal_start.sals[0];
10238 addr_string_start = savestring (arg_start, arg - arg_start);
10239 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10240
10241 arg++; /* Skip the comma. */
10242 arg = skip_spaces (arg);
10243
10244 /* Parse the end location. */
10245
10246 arg_start = arg;
10247
10248 /* We call decode_line_full directly here instead of using
10249 parse_breakpoint_sals because we need to specify the start location's
10250 symtab and line as the default symtab and line for the end of the
10251 range. This makes it possible to have ranges like "foo.c:27, +14",
10252 where +14 means 14 lines from the start location. */
10253 event_location_up end_location = string_to_event_location (&arg,
10254 current_language);
10255 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10256 sal_start.symtab, sal_start.line,
10257 &canonical_end, NULL, NULL);
10258
10259 if (canonical_end.lsals.empty ())
10260 error (_("Could not find location of the end of the range."));
10261
10262 const linespec_sals &lsal_end = canonical_end.lsals[0];
10263 if (canonical_end.lsals.size () > 1
10264 || lsal_end.sals.size () != 1)
10265 error (_("Cannot create a ranged breakpoint with multiple locations."));
10266
10267 const symtab_and_line &sal_end = lsal_end.sals[0];
10268
10269 end = find_breakpoint_range_end (sal_end);
10270 if (sal_start.pc > end)
10271 error (_("Invalid address range, end precedes start."));
10272
10273 length = end - sal_start.pc + 1;
10274 if (length < 0)
10275 /* Length overflowed. */
10276 error (_("Address range too large."));
10277 else if (length == 1)
10278 {
10279 /* This range is simple enough to be handled by
10280 the `hbreak' command. */
10281 hbreak_command (addr_string_start, 1);
10282
10283 do_cleanups (cleanup_bkpt);
10284
10285 return;
10286 }
10287
10288 /* Now set up the breakpoint. */
10289 b = set_raw_breakpoint (get_current_arch (), sal_start,
10290 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10291 set_breakpoint_count (breakpoint_count + 1);
10292 b->number = breakpoint_count;
10293 b->disposition = disp_donttouch;
10294 b->location = std::move (start_location);
10295 b->location_range_end = std::move (end_location);
10296 b->loc->length = length;
10297
10298 do_cleanups (cleanup_bkpt);
10299
10300 mention (b);
10301 observer_notify_breakpoint_created (b);
10302 update_global_location_list (UGLL_MAY_INSERT);
10303 }
10304
10305 /* Return non-zero if EXP is verified as constant. Returned zero
10306 means EXP is variable. Also the constant detection may fail for
10307 some constant expressions and in such case still falsely return
10308 zero. */
10309
10310 static int
10311 watchpoint_exp_is_const (const struct expression *exp)
10312 {
10313 int i = exp->nelts;
10314
10315 while (i > 0)
10316 {
10317 int oplenp, argsp;
10318
10319 /* We are only interested in the descriptor of each element. */
10320 operator_length (exp, i, &oplenp, &argsp);
10321 i -= oplenp;
10322
10323 switch (exp->elts[i].opcode)
10324 {
10325 case BINOP_ADD:
10326 case BINOP_SUB:
10327 case BINOP_MUL:
10328 case BINOP_DIV:
10329 case BINOP_REM:
10330 case BINOP_MOD:
10331 case BINOP_LSH:
10332 case BINOP_RSH:
10333 case BINOP_LOGICAL_AND:
10334 case BINOP_LOGICAL_OR:
10335 case BINOP_BITWISE_AND:
10336 case BINOP_BITWISE_IOR:
10337 case BINOP_BITWISE_XOR:
10338 case BINOP_EQUAL:
10339 case BINOP_NOTEQUAL:
10340 case BINOP_LESS:
10341 case BINOP_GTR:
10342 case BINOP_LEQ:
10343 case BINOP_GEQ:
10344 case BINOP_REPEAT:
10345 case BINOP_COMMA:
10346 case BINOP_EXP:
10347 case BINOP_MIN:
10348 case BINOP_MAX:
10349 case BINOP_INTDIV:
10350 case BINOP_CONCAT:
10351 case TERNOP_COND:
10352 case TERNOP_SLICE:
10353
10354 case OP_LONG:
10355 case OP_DOUBLE:
10356 case OP_DECFLOAT:
10357 case OP_LAST:
10358 case OP_COMPLEX:
10359 case OP_STRING:
10360 case OP_ARRAY:
10361 case OP_TYPE:
10362 case OP_TYPEOF:
10363 case OP_DECLTYPE:
10364 case OP_TYPEID:
10365 case OP_NAME:
10366 case OP_OBJC_NSSTRING:
10367
10368 case UNOP_NEG:
10369 case UNOP_LOGICAL_NOT:
10370 case UNOP_COMPLEMENT:
10371 case UNOP_ADDR:
10372 case UNOP_HIGH:
10373 case UNOP_CAST:
10374
10375 case UNOP_CAST_TYPE:
10376 case UNOP_REINTERPRET_CAST:
10377 case UNOP_DYNAMIC_CAST:
10378 /* Unary, binary and ternary operators: We have to check
10379 their operands. If they are constant, then so is the
10380 result of that operation. For instance, if A and B are
10381 determined to be constants, then so is "A + B".
10382
10383 UNOP_IND is one exception to the rule above, because the
10384 value of *ADDR is not necessarily a constant, even when
10385 ADDR is. */
10386 break;
10387
10388 case OP_VAR_VALUE:
10389 /* Check whether the associated symbol is a constant.
10390
10391 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10392 possible that a buggy compiler could mark a variable as
10393 constant even when it is not, and TYPE_CONST would return
10394 true in this case, while SYMBOL_CLASS wouldn't.
10395
10396 We also have to check for function symbols because they
10397 are always constant. */
10398 {
10399 struct symbol *s = exp->elts[i + 2].symbol;
10400
10401 if (SYMBOL_CLASS (s) != LOC_BLOCK
10402 && SYMBOL_CLASS (s) != LOC_CONST
10403 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10404 return 0;
10405 break;
10406 }
10407
10408 /* The default action is to return 0 because we are using
10409 the optimistic approach here: If we don't know something,
10410 then it is not a constant. */
10411 default:
10412 return 0;
10413 }
10414 }
10415
10416 return 1;
10417 }
10418
10419 /* Watchpoint destructor. */
10420
10421 watchpoint::~watchpoint ()
10422 {
10423 xfree (this->exp_string);
10424 xfree (this->exp_string_reparse);
10425 value_free (this->val);
10426 }
10427
10428 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10429
10430 static void
10431 re_set_watchpoint (struct breakpoint *b)
10432 {
10433 struct watchpoint *w = (struct watchpoint *) b;
10434
10435 /* Watchpoint can be either on expression using entirely global
10436 variables, or it can be on local variables.
10437
10438 Watchpoints of the first kind are never auto-deleted, and even
10439 persist across program restarts. Since they can use variables
10440 from shared libraries, we need to reparse expression as libraries
10441 are loaded and unloaded.
10442
10443 Watchpoints on local variables can also change meaning as result
10444 of solib event. For example, if a watchpoint uses both a local
10445 and a global variables in expression, it's a local watchpoint,
10446 but unloading of a shared library will make the expression
10447 invalid. This is not a very common use case, but we still
10448 re-evaluate expression, to avoid surprises to the user.
10449
10450 Note that for local watchpoints, we re-evaluate it only if
10451 watchpoints frame id is still valid. If it's not, it means the
10452 watchpoint is out of scope and will be deleted soon. In fact,
10453 I'm not sure we'll ever be called in this case.
10454
10455 If a local watchpoint's frame id is still valid, then
10456 w->exp_valid_block is likewise valid, and we can safely use it.
10457
10458 Don't do anything about disabled watchpoints, since they will be
10459 reevaluated again when enabled. */
10460 update_watchpoint (w, 1 /* reparse */);
10461 }
10462
10463 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10464
10465 static int
10466 insert_watchpoint (struct bp_location *bl)
10467 {
10468 struct watchpoint *w = (struct watchpoint *) bl->owner;
10469 int length = w->exact ? 1 : bl->length;
10470
10471 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10472 w->cond_exp.get ());
10473 }
10474
10475 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10476
10477 static int
10478 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10479 {
10480 struct watchpoint *w = (struct watchpoint *) bl->owner;
10481 int length = w->exact ? 1 : bl->length;
10482
10483 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10484 w->cond_exp.get ());
10485 }
10486
10487 static int
10488 breakpoint_hit_watchpoint (const struct bp_location *bl,
10489 struct address_space *aspace, CORE_ADDR bp_addr,
10490 const struct target_waitstatus *ws)
10491 {
10492 struct breakpoint *b = bl->owner;
10493 struct watchpoint *w = (struct watchpoint *) b;
10494
10495 /* Continuable hardware watchpoints are treated as non-existent if the
10496 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10497 some data address). Otherwise gdb won't stop on a break instruction
10498 in the code (not from a breakpoint) when a hardware watchpoint has
10499 been defined. Also skip watchpoints which we know did not trigger
10500 (did not match the data address). */
10501 if (is_hardware_watchpoint (b)
10502 && w->watchpoint_triggered == watch_triggered_no)
10503 return 0;
10504
10505 return 1;
10506 }
10507
10508 static void
10509 check_status_watchpoint (bpstat bs)
10510 {
10511 gdb_assert (is_watchpoint (bs->breakpoint_at));
10512
10513 bpstat_check_watchpoint (bs);
10514 }
10515
10516 /* Implement the "resources_needed" breakpoint_ops method for
10517 hardware watchpoints. */
10518
10519 static int
10520 resources_needed_watchpoint (const struct bp_location *bl)
10521 {
10522 struct watchpoint *w = (struct watchpoint *) bl->owner;
10523 int length = w->exact? 1 : bl->length;
10524
10525 return target_region_ok_for_hw_watchpoint (bl->address, length);
10526 }
10527
10528 /* Implement the "works_in_software_mode" breakpoint_ops method for
10529 hardware watchpoints. */
10530
10531 static int
10532 works_in_software_mode_watchpoint (const struct breakpoint *b)
10533 {
10534 /* Read and access watchpoints only work with hardware support. */
10535 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10536 }
10537
10538 static enum print_stop_action
10539 print_it_watchpoint (bpstat bs)
10540 {
10541 struct cleanup *old_chain;
10542 struct breakpoint *b;
10543 enum print_stop_action result;
10544 struct watchpoint *w;
10545 struct ui_out *uiout = current_uiout;
10546
10547 gdb_assert (bs->bp_location_at != NULL);
10548
10549 b = bs->breakpoint_at;
10550 w = (struct watchpoint *) b;
10551
10552 old_chain = make_cleanup (null_cleanup, NULL);
10553
10554 annotate_watchpoint (b->number);
10555 maybe_print_thread_hit_breakpoint (uiout);
10556
10557 string_file stb;
10558
10559 switch (b->type)
10560 {
10561 case bp_watchpoint:
10562 case bp_hardware_watchpoint:
10563 if (uiout->is_mi_like_p ())
10564 uiout->field_string
10565 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10566 mention (b);
10567 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10568 uiout->text ("\nOld value = ");
10569 watchpoint_value_print (bs->old_val, &stb);
10570 uiout->field_stream ("old", stb);
10571 uiout->text ("\nNew value = ");
10572 watchpoint_value_print (w->val, &stb);
10573 uiout->field_stream ("new", stb);
10574 uiout->text ("\n");
10575 /* More than one watchpoint may have been triggered. */
10576 result = PRINT_UNKNOWN;
10577 break;
10578
10579 case bp_read_watchpoint:
10580 if (uiout->is_mi_like_p ())
10581 uiout->field_string
10582 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10583 mention (b);
10584 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10585 uiout->text ("\nValue = ");
10586 watchpoint_value_print (w->val, &stb);
10587 uiout->field_stream ("value", stb);
10588 uiout->text ("\n");
10589 result = PRINT_UNKNOWN;
10590 break;
10591
10592 case bp_access_watchpoint:
10593 if (bs->old_val != NULL)
10594 {
10595 if (uiout->is_mi_like_p ())
10596 uiout->field_string
10597 ("reason",
10598 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10599 mention (b);
10600 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10601 uiout->text ("\nOld value = ");
10602 watchpoint_value_print (bs->old_val, &stb);
10603 uiout->field_stream ("old", stb);
10604 uiout->text ("\nNew value = ");
10605 }
10606 else
10607 {
10608 mention (b);
10609 if (uiout->is_mi_like_p ())
10610 uiout->field_string
10611 ("reason",
10612 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10613 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10614 uiout->text ("\nValue = ");
10615 }
10616 watchpoint_value_print (w->val, &stb);
10617 uiout->field_stream ("new", stb);
10618 uiout->text ("\n");
10619 result = PRINT_UNKNOWN;
10620 break;
10621 default:
10622 result = PRINT_UNKNOWN;
10623 }
10624
10625 do_cleanups (old_chain);
10626 return result;
10627 }
10628
10629 /* Implement the "print_mention" breakpoint_ops method for hardware
10630 watchpoints. */
10631
10632 static void
10633 print_mention_watchpoint (struct breakpoint *b)
10634 {
10635 struct watchpoint *w = (struct watchpoint *) b;
10636 struct ui_out *uiout = current_uiout;
10637 const char *tuple_name;
10638
10639 switch (b->type)
10640 {
10641 case bp_watchpoint:
10642 uiout->text ("Watchpoint ");
10643 tuple_name = "wpt";
10644 break;
10645 case bp_hardware_watchpoint:
10646 uiout->text ("Hardware watchpoint ");
10647 tuple_name = "wpt";
10648 break;
10649 case bp_read_watchpoint:
10650 uiout->text ("Hardware read watchpoint ");
10651 tuple_name = "hw-rwpt";
10652 break;
10653 case bp_access_watchpoint:
10654 uiout->text ("Hardware access (read/write) watchpoint ");
10655 tuple_name = "hw-awpt";
10656 break;
10657 default:
10658 internal_error (__FILE__, __LINE__,
10659 _("Invalid hardware watchpoint type."));
10660 }
10661
10662 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10663 uiout->field_int ("number", b->number);
10664 uiout->text (": ");
10665 uiout->field_string ("exp", w->exp_string);
10666 }
10667
10668 /* Implement the "print_recreate" breakpoint_ops method for
10669 watchpoints. */
10670
10671 static void
10672 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10673 {
10674 struct watchpoint *w = (struct watchpoint *) b;
10675
10676 switch (b->type)
10677 {
10678 case bp_watchpoint:
10679 case bp_hardware_watchpoint:
10680 fprintf_unfiltered (fp, "watch");
10681 break;
10682 case bp_read_watchpoint:
10683 fprintf_unfiltered (fp, "rwatch");
10684 break;
10685 case bp_access_watchpoint:
10686 fprintf_unfiltered (fp, "awatch");
10687 break;
10688 default:
10689 internal_error (__FILE__, __LINE__,
10690 _("Invalid watchpoint type."));
10691 }
10692
10693 fprintf_unfiltered (fp, " %s", w->exp_string);
10694 print_recreate_thread (b, fp);
10695 }
10696
10697 /* Implement the "explains_signal" breakpoint_ops method for
10698 watchpoints. */
10699
10700 static int
10701 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10702 {
10703 /* A software watchpoint cannot cause a signal other than
10704 GDB_SIGNAL_TRAP. */
10705 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10706 return 0;
10707
10708 return 1;
10709 }
10710
10711 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10712
10713 static struct breakpoint_ops watchpoint_breakpoint_ops;
10714
10715 /* Implement the "insert" breakpoint_ops method for
10716 masked hardware watchpoints. */
10717
10718 static int
10719 insert_masked_watchpoint (struct bp_location *bl)
10720 {
10721 struct watchpoint *w = (struct watchpoint *) bl->owner;
10722
10723 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10724 bl->watchpoint_type);
10725 }
10726
10727 /* Implement the "remove" breakpoint_ops method for
10728 masked hardware watchpoints. */
10729
10730 static int
10731 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10732 {
10733 struct watchpoint *w = (struct watchpoint *) bl->owner;
10734
10735 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10736 bl->watchpoint_type);
10737 }
10738
10739 /* Implement the "resources_needed" breakpoint_ops method for
10740 masked hardware watchpoints. */
10741
10742 static int
10743 resources_needed_masked_watchpoint (const struct bp_location *bl)
10744 {
10745 struct watchpoint *w = (struct watchpoint *) bl->owner;
10746
10747 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10748 }
10749
10750 /* Implement the "works_in_software_mode" breakpoint_ops method for
10751 masked hardware watchpoints. */
10752
10753 static int
10754 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10755 {
10756 return 0;
10757 }
10758
10759 /* Implement the "print_it" breakpoint_ops method for
10760 masked hardware watchpoints. */
10761
10762 static enum print_stop_action
10763 print_it_masked_watchpoint (bpstat bs)
10764 {
10765 struct breakpoint *b = bs->breakpoint_at;
10766 struct ui_out *uiout = current_uiout;
10767
10768 /* Masked watchpoints have only one location. */
10769 gdb_assert (b->loc && b->loc->next == NULL);
10770
10771 annotate_watchpoint (b->number);
10772 maybe_print_thread_hit_breakpoint (uiout);
10773
10774 switch (b->type)
10775 {
10776 case bp_hardware_watchpoint:
10777 if (uiout->is_mi_like_p ())
10778 uiout->field_string
10779 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10780 break;
10781
10782 case bp_read_watchpoint:
10783 if (uiout->is_mi_like_p ())
10784 uiout->field_string
10785 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10786 break;
10787
10788 case bp_access_watchpoint:
10789 if (uiout->is_mi_like_p ())
10790 uiout->field_string
10791 ("reason",
10792 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10793 break;
10794 default:
10795 internal_error (__FILE__, __LINE__,
10796 _("Invalid hardware watchpoint type."));
10797 }
10798
10799 mention (b);
10800 uiout->text (_("\n\
10801 Check the underlying instruction at PC for the memory\n\
10802 address and value which triggered this watchpoint.\n"));
10803 uiout->text ("\n");
10804
10805 /* More than one watchpoint may have been triggered. */
10806 return PRINT_UNKNOWN;
10807 }
10808
10809 /* Implement the "print_one_detail" breakpoint_ops method for
10810 masked hardware watchpoints. */
10811
10812 static void
10813 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10814 struct ui_out *uiout)
10815 {
10816 struct watchpoint *w = (struct watchpoint *) b;
10817
10818 /* Masked watchpoints have only one location. */
10819 gdb_assert (b->loc && b->loc->next == NULL);
10820
10821 uiout->text ("\tmask ");
10822 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10823 uiout->text ("\n");
10824 }
10825
10826 /* Implement the "print_mention" breakpoint_ops method for
10827 masked hardware watchpoints. */
10828
10829 static void
10830 print_mention_masked_watchpoint (struct breakpoint *b)
10831 {
10832 struct watchpoint *w = (struct watchpoint *) b;
10833 struct ui_out *uiout = current_uiout;
10834 const char *tuple_name;
10835
10836 switch (b->type)
10837 {
10838 case bp_hardware_watchpoint:
10839 uiout->text ("Masked hardware watchpoint ");
10840 tuple_name = "wpt";
10841 break;
10842 case bp_read_watchpoint:
10843 uiout->text ("Masked hardware read watchpoint ");
10844 tuple_name = "hw-rwpt";
10845 break;
10846 case bp_access_watchpoint:
10847 uiout->text ("Masked hardware access (read/write) watchpoint ");
10848 tuple_name = "hw-awpt";
10849 break;
10850 default:
10851 internal_error (__FILE__, __LINE__,
10852 _("Invalid hardware watchpoint type."));
10853 }
10854
10855 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10856 uiout->field_int ("number", b->number);
10857 uiout->text (": ");
10858 uiout->field_string ("exp", w->exp_string);
10859 }
10860
10861 /* Implement the "print_recreate" breakpoint_ops method for
10862 masked hardware watchpoints. */
10863
10864 static void
10865 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10866 {
10867 struct watchpoint *w = (struct watchpoint *) b;
10868 char tmp[40];
10869
10870 switch (b->type)
10871 {
10872 case bp_hardware_watchpoint:
10873 fprintf_unfiltered (fp, "watch");
10874 break;
10875 case bp_read_watchpoint:
10876 fprintf_unfiltered (fp, "rwatch");
10877 break;
10878 case bp_access_watchpoint:
10879 fprintf_unfiltered (fp, "awatch");
10880 break;
10881 default:
10882 internal_error (__FILE__, __LINE__,
10883 _("Invalid hardware watchpoint type."));
10884 }
10885
10886 sprintf_vma (tmp, w->hw_wp_mask);
10887 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10888 print_recreate_thread (b, fp);
10889 }
10890
10891 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10892
10893 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10894
10895 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10896
10897 static int
10898 is_masked_watchpoint (const struct breakpoint *b)
10899 {
10900 return b->ops == &masked_watchpoint_breakpoint_ops;
10901 }
10902
10903 /* accessflag: hw_write: watch write,
10904 hw_read: watch read,
10905 hw_access: watch access (read or write) */
10906 static void
10907 watch_command_1 (const char *arg, int accessflag, int from_tty,
10908 int just_location, int internal)
10909 {
10910 struct breakpoint *scope_breakpoint = NULL;
10911 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10912 struct value *val, *mark, *result;
10913 int saved_bitpos = 0, saved_bitsize = 0;
10914 const char *exp_start = NULL;
10915 const char *exp_end = NULL;
10916 const char *tok, *end_tok;
10917 int toklen = -1;
10918 const char *cond_start = NULL;
10919 const char *cond_end = NULL;
10920 enum bptype bp_type;
10921 int thread = -1;
10922 int pc = 0;
10923 /* Flag to indicate whether we are going to use masks for
10924 the hardware watchpoint. */
10925 int use_mask = 0;
10926 CORE_ADDR mask = 0;
10927 char *expression;
10928 struct cleanup *back_to;
10929
10930 /* Make sure that we actually have parameters to parse. */
10931 if (arg != NULL && arg[0] != '\0')
10932 {
10933 const char *value_start;
10934
10935 exp_end = arg + strlen (arg);
10936
10937 /* Look for "parameter value" pairs at the end
10938 of the arguments string. */
10939 for (tok = exp_end - 1; tok > arg; tok--)
10940 {
10941 /* Skip whitespace at the end of the argument list. */
10942 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10943 tok--;
10944
10945 /* Find the beginning of the last token.
10946 This is the value of the parameter. */
10947 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10948 tok--;
10949 value_start = tok + 1;
10950
10951 /* Skip whitespace. */
10952 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10953 tok--;
10954
10955 end_tok = tok;
10956
10957 /* Find the beginning of the second to last token.
10958 This is the parameter itself. */
10959 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10960 tok--;
10961 tok++;
10962 toklen = end_tok - tok + 1;
10963
10964 if (toklen == 6 && startswith (tok, "thread"))
10965 {
10966 struct thread_info *thr;
10967 /* At this point we've found a "thread" token, which means
10968 the user is trying to set a watchpoint that triggers
10969 only in a specific thread. */
10970 const char *endp;
10971
10972 if (thread != -1)
10973 error(_("You can specify only one thread."));
10974
10975 /* Extract the thread ID from the next token. */
10976 thr = parse_thread_id (value_start, &endp);
10977
10978 /* Check if the user provided a valid thread ID. */
10979 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10980 invalid_thread_id_error (value_start);
10981
10982 thread = thr->global_num;
10983 }
10984 else if (toklen == 4 && startswith (tok, "mask"))
10985 {
10986 /* We've found a "mask" token, which means the user wants to
10987 create a hardware watchpoint that is going to have the mask
10988 facility. */
10989 struct value *mask_value, *mark;
10990
10991 if (use_mask)
10992 error(_("You can specify only one mask."));
10993
10994 use_mask = just_location = 1;
10995
10996 mark = value_mark ();
10997 mask_value = parse_to_comma_and_eval (&value_start);
10998 mask = value_as_address (mask_value);
10999 value_free_to_mark (mark);
11000 }
11001 else
11002 /* We didn't recognize what we found. We should stop here. */
11003 break;
11004
11005 /* Truncate the string and get rid of the "parameter value" pair before
11006 the arguments string is parsed by the parse_exp_1 function. */
11007 exp_end = tok;
11008 }
11009 }
11010 else
11011 exp_end = arg;
11012
11013 /* Parse the rest of the arguments. From here on out, everything
11014 is in terms of a newly allocated string instead of the original
11015 ARG. */
11016 innermost_block = NULL;
11017 expression = savestring (arg, exp_end - arg);
11018 back_to = make_cleanup (xfree, expression);
11019 exp_start = arg = expression;
11020 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11021 exp_end = arg;
11022 /* Remove trailing whitespace from the expression before saving it.
11023 This makes the eventual display of the expression string a bit
11024 prettier. */
11025 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11026 --exp_end;
11027
11028 /* Checking if the expression is not constant. */
11029 if (watchpoint_exp_is_const (exp.get ()))
11030 {
11031 int len;
11032
11033 len = exp_end - exp_start;
11034 while (len > 0 && isspace (exp_start[len - 1]))
11035 len--;
11036 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11037 }
11038
11039 exp_valid_block = innermost_block;
11040 mark = value_mark ();
11041 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11042
11043 if (val != NULL && just_location)
11044 {
11045 saved_bitpos = value_bitpos (val);
11046 saved_bitsize = value_bitsize (val);
11047 }
11048
11049 if (just_location)
11050 {
11051 int ret;
11052
11053 exp_valid_block = NULL;
11054 val = value_addr (result);
11055 release_value (val);
11056 value_free_to_mark (mark);
11057
11058 if (use_mask)
11059 {
11060 ret = target_masked_watch_num_registers (value_as_address (val),
11061 mask);
11062 if (ret == -1)
11063 error (_("This target does not support masked watchpoints."));
11064 else if (ret == -2)
11065 error (_("Invalid mask or memory region."));
11066 }
11067 }
11068 else if (val != NULL)
11069 release_value (val);
11070
11071 tok = skip_spaces_const (arg);
11072 end_tok = skip_to_space_const (tok);
11073
11074 toklen = end_tok - tok;
11075 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11076 {
11077 innermost_block = NULL;
11078 tok = cond_start = end_tok + 1;
11079 parse_exp_1 (&tok, 0, 0, 0);
11080
11081 /* The watchpoint expression may not be local, but the condition
11082 may still be. E.g.: `watch global if local > 0'. */
11083 cond_exp_valid_block = innermost_block;
11084
11085 cond_end = tok;
11086 }
11087 if (*tok)
11088 error (_("Junk at end of command."));
11089
11090 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11091
11092 /* Save this because create_internal_breakpoint below invalidates
11093 'wp_frame'. */
11094 frame_id watchpoint_frame = get_frame_id (wp_frame);
11095
11096 /* If the expression is "local", then set up a "watchpoint scope"
11097 breakpoint at the point where we've left the scope of the watchpoint
11098 expression. Create the scope breakpoint before the watchpoint, so
11099 that we will encounter it first in bpstat_stop_status. */
11100 if (exp_valid_block != NULL && wp_frame != NULL)
11101 {
11102 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11103
11104 if (frame_id_p (caller_frame_id))
11105 {
11106 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11107 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11108
11109 scope_breakpoint
11110 = create_internal_breakpoint (caller_arch, caller_pc,
11111 bp_watchpoint_scope,
11112 &momentary_breakpoint_ops);
11113
11114 /* create_internal_breakpoint could invalidate WP_FRAME. */
11115 wp_frame = NULL;
11116
11117 scope_breakpoint->enable_state = bp_enabled;
11118
11119 /* Automatically delete the breakpoint when it hits. */
11120 scope_breakpoint->disposition = disp_del;
11121
11122 /* Only break in the proper frame (help with recursion). */
11123 scope_breakpoint->frame_id = caller_frame_id;
11124
11125 /* Set the address at which we will stop. */
11126 scope_breakpoint->loc->gdbarch = caller_arch;
11127 scope_breakpoint->loc->requested_address = caller_pc;
11128 scope_breakpoint->loc->address
11129 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11130 scope_breakpoint->loc->requested_address,
11131 scope_breakpoint->type);
11132 }
11133 }
11134
11135 /* Now set up the breakpoint. We create all watchpoints as hardware
11136 watchpoints here even if hardware watchpoints are turned off, a call
11137 to update_watchpoint later in this function will cause the type to
11138 drop back to bp_watchpoint (software watchpoint) if required. */
11139
11140 if (accessflag == hw_read)
11141 bp_type = bp_read_watchpoint;
11142 else if (accessflag == hw_access)
11143 bp_type = bp_access_watchpoint;
11144 else
11145 bp_type = bp_hardware_watchpoint;
11146
11147 std::unique_ptr<watchpoint> w (new watchpoint ());
11148
11149 if (use_mask)
11150 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11151 &masked_watchpoint_breakpoint_ops);
11152 else
11153 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11154 &watchpoint_breakpoint_ops);
11155 w->thread = thread;
11156 w->disposition = disp_donttouch;
11157 w->pspace = current_program_space;
11158 w->exp = std::move (exp);
11159 w->exp_valid_block = exp_valid_block;
11160 w->cond_exp_valid_block = cond_exp_valid_block;
11161 if (just_location)
11162 {
11163 struct type *t = value_type (val);
11164 CORE_ADDR addr = value_as_address (val);
11165
11166 w->exp_string_reparse
11167 = current_language->la_watch_location_expression (t, addr).release ();
11168
11169 w->exp_string = xstrprintf ("-location %.*s",
11170 (int) (exp_end - exp_start), exp_start);
11171 }
11172 else
11173 w->exp_string = savestring (exp_start, exp_end - exp_start);
11174
11175 if (use_mask)
11176 {
11177 w->hw_wp_mask = mask;
11178 }
11179 else
11180 {
11181 w->val = val;
11182 w->val_bitpos = saved_bitpos;
11183 w->val_bitsize = saved_bitsize;
11184 w->val_valid = 1;
11185 }
11186
11187 if (cond_start)
11188 w->cond_string = savestring (cond_start, cond_end - cond_start);
11189 else
11190 w->cond_string = 0;
11191
11192 if (frame_id_p (watchpoint_frame))
11193 {
11194 w->watchpoint_frame = watchpoint_frame;
11195 w->watchpoint_thread = inferior_ptid;
11196 }
11197 else
11198 {
11199 w->watchpoint_frame = null_frame_id;
11200 w->watchpoint_thread = null_ptid;
11201 }
11202
11203 if (scope_breakpoint != NULL)
11204 {
11205 /* The scope breakpoint is related to the watchpoint. We will
11206 need to act on them together. */
11207 w->related_breakpoint = scope_breakpoint;
11208 scope_breakpoint->related_breakpoint = w.get ();
11209 }
11210
11211 if (!just_location)
11212 value_free_to_mark (mark);
11213
11214 /* Finally update the new watchpoint. This creates the locations
11215 that should be inserted. */
11216 update_watchpoint (w.get (), 1);
11217
11218 install_breakpoint (internal, std::move (w), 1);
11219 do_cleanups (back_to);
11220 }
11221
11222 /* Return count of debug registers needed to watch the given expression.
11223 If the watchpoint cannot be handled in hardware return zero. */
11224
11225 static int
11226 can_use_hardware_watchpoint (struct value *v)
11227 {
11228 int found_memory_cnt = 0;
11229 struct value *head = v;
11230
11231 /* Did the user specifically forbid us to use hardware watchpoints? */
11232 if (!can_use_hw_watchpoints)
11233 return 0;
11234
11235 /* Make sure that the value of the expression depends only upon
11236 memory contents, and values computed from them within GDB. If we
11237 find any register references or function calls, we can't use a
11238 hardware watchpoint.
11239
11240 The idea here is that evaluating an expression generates a series
11241 of values, one holding the value of every subexpression. (The
11242 expression a*b+c has five subexpressions: a, b, a*b, c, and
11243 a*b+c.) GDB's values hold almost enough information to establish
11244 the criteria given above --- they identify memory lvalues,
11245 register lvalues, computed values, etcetera. So we can evaluate
11246 the expression, and then scan the chain of values that leaves
11247 behind to decide whether we can detect any possible change to the
11248 expression's final value using only hardware watchpoints.
11249
11250 However, I don't think that the values returned by inferior
11251 function calls are special in any way. So this function may not
11252 notice that an expression involving an inferior function call
11253 can't be watched with hardware watchpoints. FIXME. */
11254 for (; v; v = value_next (v))
11255 {
11256 if (VALUE_LVAL (v) == lval_memory)
11257 {
11258 if (v != head && value_lazy (v))
11259 /* A lazy memory lvalue in the chain is one that GDB never
11260 needed to fetch; we either just used its address (e.g.,
11261 `a' in `a.b') or we never needed it at all (e.g., `a'
11262 in `a,b'). This doesn't apply to HEAD; if that is
11263 lazy then it was not readable, but watch it anyway. */
11264 ;
11265 else
11266 {
11267 /* Ahh, memory we actually used! Check if we can cover
11268 it with hardware watchpoints. */
11269 struct type *vtype = check_typedef (value_type (v));
11270
11271 /* We only watch structs and arrays if user asked for it
11272 explicitly, never if they just happen to appear in a
11273 middle of some value chain. */
11274 if (v == head
11275 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11276 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11277 {
11278 CORE_ADDR vaddr = value_address (v);
11279 int len;
11280 int num_regs;
11281
11282 len = (target_exact_watchpoints
11283 && is_scalar_type_recursive (vtype))?
11284 1 : TYPE_LENGTH (value_type (v));
11285
11286 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11287 if (!num_regs)
11288 return 0;
11289 else
11290 found_memory_cnt += num_regs;
11291 }
11292 }
11293 }
11294 else if (VALUE_LVAL (v) != not_lval
11295 && deprecated_value_modifiable (v) == 0)
11296 return 0; /* These are values from the history (e.g., $1). */
11297 else if (VALUE_LVAL (v) == lval_register)
11298 return 0; /* Cannot watch a register with a HW watchpoint. */
11299 }
11300
11301 /* The expression itself looks suitable for using a hardware
11302 watchpoint, but give the target machine a chance to reject it. */
11303 return found_memory_cnt;
11304 }
11305
11306 void
11307 watch_command_wrapper (char *arg, int from_tty, int internal)
11308 {
11309 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11310 }
11311
11312 /* A helper function that looks for the "-location" argument and then
11313 calls watch_command_1. */
11314
11315 static void
11316 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11317 {
11318 int just_location = 0;
11319
11320 if (arg
11321 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11322 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11323 {
11324 arg = skip_spaces (arg);
11325 just_location = 1;
11326 }
11327
11328 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11329 }
11330
11331 static void
11332 watch_command (char *arg, int from_tty)
11333 {
11334 watch_maybe_just_location (arg, hw_write, from_tty);
11335 }
11336
11337 void
11338 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11339 {
11340 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11341 }
11342
11343 static void
11344 rwatch_command (char *arg, int from_tty)
11345 {
11346 watch_maybe_just_location (arg, hw_read, from_tty);
11347 }
11348
11349 void
11350 awatch_command_wrapper (char *arg, int from_tty, int internal)
11351 {
11352 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11353 }
11354
11355 static void
11356 awatch_command (char *arg, int from_tty)
11357 {
11358 watch_maybe_just_location (arg, hw_access, from_tty);
11359 }
11360 \f
11361
11362 /* Data for the FSM that manages the until(location)/advance commands
11363 in infcmd.c. Here because it uses the mechanisms of
11364 breakpoints. */
11365
11366 struct until_break_fsm
11367 {
11368 /* The base class. */
11369 struct thread_fsm thread_fsm;
11370
11371 /* The thread that as current when the command was executed. */
11372 int thread;
11373
11374 /* The breakpoint set at the destination location. */
11375 struct breakpoint *location_breakpoint;
11376
11377 /* Breakpoint set at the return address in the caller frame. May be
11378 NULL. */
11379 struct breakpoint *caller_breakpoint;
11380 };
11381
11382 static void until_break_fsm_clean_up (struct thread_fsm *self,
11383 struct thread_info *thread);
11384 static int until_break_fsm_should_stop (struct thread_fsm *self,
11385 struct thread_info *thread);
11386 static enum async_reply_reason
11387 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11388
11389 /* until_break_fsm's vtable. */
11390
11391 static struct thread_fsm_ops until_break_fsm_ops =
11392 {
11393 NULL, /* dtor */
11394 until_break_fsm_clean_up,
11395 until_break_fsm_should_stop,
11396 NULL, /* return_value */
11397 until_break_fsm_async_reply_reason,
11398 };
11399
11400 /* Allocate a new until_break_command_fsm. */
11401
11402 static struct until_break_fsm *
11403 new_until_break_fsm (struct interp *cmd_interp, int thread,
11404 struct breakpoint *location_breakpoint,
11405 struct breakpoint *caller_breakpoint)
11406 {
11407 struct until_break_fsm *sm;
11408
11409 sm = XCNEW (struct until_break_fsm);
11410 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11411
11412 sm->thread = thread;
11413 sm->location_breakpoint = location_breakpoint;
11414 sm->caller_breakpoint = caller_breakpoint;
11415
11416 return sm;
11417 }
11418
11419 /* Implementation of the 'should_stop' FSM method for the
11420 until(location)/advance commands. */
11421
11422 static int
11423 until_break_fsm_should_stop (struct thread_fsm *self,
11424 struct thread_info *tp)
11425 {
11426 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11427
11428 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11429 sm->location_breakpoint) != NULL
11430 || (sm->caller_breakpoint != NULL
11431 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11432 sm->caller_breakpoint) != NULL))
11433 thread_fsm_set_finished (self);
11434
11435 return 1;
11436 }
11437
11438 /* Implementation of the 'clean_up' FSM method for the
11439 until(location)/advance commands. */
11440
11441 static void
11442 until_break_fsm_clean_up (struct thread_fsm *self,
11443 struct thread_info *thread)
11444 {
11445 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11446
11447 /* Clean up our temporary breakpoints. */
11448 if (sm->location_breakpoint != NULL)
11449 {
11450 delete_breakpoint (sm->location_breakpoint);
11451 sm->location_breakpoint = NULL;
11452 }
11453 if (sm->caller_breakpoint != NULL)
11454 {
11455 delete_breakpoint (sm->caller_breakpoint);
11456 sm->caller_breakpoint = NULL;
11457 }
11458 delete_longjmp_breakpoint (sm->thread);
11459 }
11460
11461 /* Implementation of the 'async_reply_reason' FSM method for the
11462 until(location)/advance commands. */
11463
11464 static enum async_reply_reason
11465 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11466 {
11467 return EXEC_ASYNC_LOCATION_REACHED;
11468 }
11469
11470 void
11471 until_break_command (char *arg, int from_tty, int anywhere)
11472 {
11473 struct frame_info *frame;
11474 struct gdbarch *frame_gdbarch;
11475 struct frame_id stack_frame_id;
11476 struct frame_id caller_frame_id;
11477 struct breakpoint *location_breakpoint;
11478 struct breakpoint *caller_breakpoint = NULL;
11479 struct cleanup *old_chain;
11480 int thread;
11481 struct thread_info *tp;
11482 struct until_break_fsm *sm;
11483
11484 clear_proceed_status (0);
11485
11486 /* Set a breakpoint where the user wants it and at return from
11487 this function. */
11488
11489 event_location_up location = string_to_event_location (&arg, current_language);
11490
11491 std::vector<symtab_and_line> sals
11492 = (last_displayed_sal_is_valid ()
11493 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11494 get_last_displayed_symtab (),
11495 get_last_displayed_line ())
11496 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11497 NULL, (struct symtab *) NULL, 0));
11498
11499 if (sals.size () != 1)
11500 error (_("Couldn't get information on specified line."));
11501
11502 symtab_and_line &sal = sals[0];
11503
11504 if (*arg)
11505 error (_("Junk at end of arguments."));
11506
11507 resolve_sal_pc (&sal);
11508
11509 tp = inferior_thread ();
11510 thread = tp->global_num;
11511
11512 old_chain = make_cleanup (null_cleanup, NULL);
11513
11514 /* Note linespec handling above invalidates the frame chain.
11515 Installing a breakpoint also invalidates the frame chain (as it
11516 may need to switch threads), so do any frame handling before
11517 that. */
11518
11519 frame = get_selected_frame (NULL);
11520 frame_gdbarch = get_frame_arch (frame);
11521 stack_frame_id = get_stack_frame_id (frame);
11522 caller_frame_id = frame_unwind_caller_id (frame);
11523
11524 /* Keep within the current frame, or in frames called by the current
11525 one. */
11526
11527 if (frame_id_p (caller_frame_id))
11528 {
11529 struct symtab_and_line sal2;
11530 struct gdbarch *caller_gdbarch;
11531
11532 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11533 sal2.pc = frame_unwind_caller_pc (frame);
11534 caller_gdbarch = frame_unwind_caller_arch (frame);
11535 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11536 sal2,
11537 caller_frame_id,
11538 bp_until);
11539 make_cleanup_delete_breakpoint (caller_breakpoint);
11540
11541 set_longjmp_breakpoint (tp, caller_frame_id);
11542 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11543 }
11544
11545 /* set_momentary_breakpoint could invalidate FRAME. */
11546 frame = NULL;
11547
11548 if (anywhere)
11549 /* If the user told us to continue until a specified location,
11550 we don't specify a frame at which we need to stop. */
11551 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11552 null_frame_id, bp_until);
11553 else
11554 /* Otherwise, specify the selected frame, because we want to stop
11555 only at the very same frame. */
11556 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11557 stack_frame_id, bp_until);
11558 make_cleanup_delete_breakpoint (location_breakpoint);
11559
11560 sm = new_until_break_fsm (command_interp (), tp->global_num,
11561 location_breakpoint, caller_breakpoint);
11562 tp->thread_fsm = &sm->thread_fsm;
11563
11564 discard_cleanups (old_chain);
11565
11566 proceed (-1, GDB_SIGNAL_DEFAULT);
11567 }
11568
11569 /* This function attempts to parse an optional "if <cond>" clause
11570 from the arg string. If one is not found, it returns NULL.
11571
11572 Else, it returns a pointer to the condition string. (It does not
11573 attempt to evaluate the string against a particular block.) And,
11574 it updates arg to point to the first character following the parsed
11575 if clause in the arg string. */
11576
11577 const char *
11578 ep_parse_optional_if_clause (const char **arg)
11579 {
11580 const char *cond_string;
11581
11582 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11583 return NULL;
11584
11585 /* Skip the "if" keyword. */
11586 (*arg) += 2;
11587
11588 /* Skip any extra leading whitespace, and record the start of the
11589 condition string. */
11590 *arg = skip_spaces_const (*arg);
11591 cond_string = *arg;
11592
11593 /* Assume that the condition occupies the remainder of the arg
11594 string. */
11595 (*arg) += strlen (cond_string);
11596
11597 return cond_string;
11598 }
11599
11600 /* Commands to deal with catching events, such as signals, exceptions,
11601 process start/exit, etc. */
11602
11603 typedef enum
11604 {
11605 catch_fork_temporary, catch_vfork_temporary,
11606 catch_fork_permanent, catch_vfork_permanent
11607 }
11608 catch_fork_kind;
11609
11610 static void
11611 catch_fork_command_1 (char *arg_entry, int from_tty,
11612 struct cmd_list_element *command)
11613 {
11614 const char *arg = arg_entry;
11615 struct gdbarch *gdbarch = get_current_arch ();
11616 const char *cond_string = NULL;
11617 catch_fork_kind fork_kind;
11618 int tempflag;
11619
11620 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11621 tempflag = (fork_kind == catch_fork_temporary
11622 || fork_kind == catch_vfork_temporary);
11623
11624 if (!arg)
11625 arg = "";
11626 arg = skip_spaces_const (arg);
11627
11628 /* The allowed syntax is:
11629 catch [v]fork
11630 catch [v]fork if <cond>
11631
11632 First, check if there's an if clause. */
11633 cond_string = ep_parse_optional_if_clause (&arg);
11634
11635 if ((*arg != '\0') && !isspace (*arg))
11636 error (_("Junk at end of arguments."));
11637
11638 /* If this target supports it, create a fork or vfork catchpoint
11639 and enable reporting of such events. */
11640 switch (fork_kind)
11641 {
11642 case catch_fork_temporary:
11643 case catch_fork_permanent:
11644 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11645 &catch_fork_breakpoint_ops);
11646 break;
11647 case catch_vfork_temporary:
11648 case catch_vfork_permanent:
11649 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11650 &catch_vfork_breakpoint_ops);
11651 break;
11652 default:
11653 error (_("unsupported or unknown fork kind; cannot catch it"));
11654 break;
11655 }
11656 }
11657
11658 static void
11659 catch_exec_command_1 (char *arg_entry, int from_tty,
11660 struct cmd_list_element *command)
11661 {
11662 const char *arg = arg_entry;
11663 struct gdbarch *gdbarch = get_current_arch ();
11664 int tempflag;
11665 const char *cond_string = NULL;
11666
11667 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11668
11669 if (!arg)
11670 arg = "";
11671 arg = skip_spaces_const (arg);
11672
11673 /* The allowed syntax is:
11674 catch exec
11675 catch exec if <cond>
11676
11677 First, check if there's an if clause. */
11678 cond_string = ep_parse_optional_if_clause (&arg);
11679
11680 if ((*arg != '\0') && !isspace (*arg))
11681 error (_("Junk at end of arguments."));
11682
11683 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11684 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11685 &catch_exec_breakpoint_ops);
11686 c->exec_pathname = NULL;
11687
11688 install_breakpoint (0, std::move (c), 1);
11689 }
11690
11691 void
11692 init_ada_exception_breakpoint (struct breakpoint *b,
11693 struct gdbarch *gdbarch,
11694 struct symtab_and_line sal,
11695 char *addr_string,
11696 const struct breakpoint_ops *ops,
11697 int tempflag,
11698 int enabled,
11699 int from_tty)
11700 {
11701 if (from_tty)
11702 {
11703 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11704 if (!loc_gdbarch)
11705 loc_gdbarch = gdbarch;
11706
11707 describe_other_breakpoints (loc_gdbarch,
11708 sal.pspace, sal.pc, sal.section, -1);
11709 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11710 version for exception catchpoints, because two catchpoints
11711 used for different exception names will use the same address.
11712 In this case, a "breakpoint ... also set at..." warning is
11713 unproductive. Besides, the warning phrasing is also a bit
11714 inappropriate, we should use the word catchpoint, and tell
11715 the user what type of catchpoint it is. The above is good
11716 enough for now, though. */
11717 }
11718
11719 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11720
11721 b->enable_state = enabled ? bp_enabled : bp_disabled;
11722 b->disposition = tempflag ? disp_del : disp_donttouch;
11723 b->location = string_to_event_location (&addr_string,
11724 language_def (language_ada));
11725 b->language = language_ada;
11726 }
11727
11728 static void
11729 catch_command (char *arg, int from_tty)
11730 {
11731 error (_("Catch requires an event name."));
11732 }
11733 \f
11734
11735 static void
11736 tcatch_command (char *arg, int from_tty)
11737 {
11738 error (_("Catch requires an event name."));
11739 }
11740
11741 /* A qsort comparison function that sorts breakpoints in order. */
11742
11743 static int
11744 compare_breakpoints (const void *a, const void *b)
11745 {
11746 const breakpoint_p *ba = (const breakpoint_p *) a;
11747 uintptr_t ua = (uintptr_t) *ba;
11748 const breakpoint_p *bb = (const breakpoint_p *) b;
11749 uintptr_t ub = (uintptr_t) *bb;
11750
11751 if ((*ba)->number < (*bb)->number)
11752 return -1;
11753 else if ((*ba)->number > (*bb)->number)
11754 return 1;
11755
11756 /* Now sort by address, in case we see, e..g, two breakpoints with
11757 the number 0. */
11758 if (ua < ub)
11759 return -1;
11760 return ua > ub ? 1 : 0;
11761 }
11762
11763 /* Delete breakpoints by address or line. */
11764
11765 static void
11766 clear_command (char *arg, int from_tty)
11767 {
11768 struct breakpoint *b, *prev;
11769 VEC(breakpoint_p) *found = 0;
11770 int ix;
11771 int default_match;
11772 int i;
11773 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11774
11775 std::vector<symtab_and_line> decoded_sals;
11776 symtab_and_line last_sal;
11777 gdb::array_view<symtab_and_line> sals;
11778 if (arg)
11779 {
11780 decoded_sals
11781 = decode_line_with_current_source (arg,
11782 (DECODE_LINE_FUNFIRSTLINE
11783 | DECODE_LINE_LIST_MODE));
11784 default_match = 0;
11785 sals = decoded_sals;
11786 }
11787 else
11788 {
11789 /* Set sal's line, symtab, pc, and pspace to the values
11790 corresponding to the last call to print_frame_info. If the
11791 codepoint is not valid, this will set all the fields to 0. */
11792 last_sal = get_last_displayed_sal ();
11793 if (last_sal.symtab == 0)
11794 error (_("No source file specified."));
11795
11796 default_match = 1;
11797 sals = last_sal;
11798 }
11799
11800 /* We don't call resolve_sal_pc here. That's not as bad as it
11801 seems, because all existing breakpoints typically have both
11802 file/line and pc set. So, if clear is given file/line, we can
11803 match this to existing breakpoint without obtaining pc at all.
11804
11805 We only support clearing given the address explicitly
11806 present in breakpoint table. Say, we've set breakpoint
11807 at file:line. There were several PC values for that file:line,
11808 due to optimization, all in one block.
11809
11810 We've picked one PC value. If "clear" is issued with another
11811 PC corresponding to the same file:line, the breakpoint won't
11812 be cleared. We probably can still clear the breakpoint, but
11813 since the other PC value is never presented to user, user
11814 can only find it by guessing, and it does not seem important
11815 to support that. */
11816
11817 /* For each line spec given, delete bps which correspond to it. Do
11818 it in two passes, solely to preserve the current behavior that
11819 from_tty is forced true if we delete more than one
11820 breakpoint. */
11821
11822 found = NULL;
11823 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11824 for (const auto &sal : sals)
11825 {
11826 const char *sal_fullname;
11827
11828 /* If exact pc given, clear bpts at that pc.
11829 If line given (pc == 0), clear all bpts on specified line.
11830 If defaulting, clear all bpts on default line
11831 or at default pc.
11832
11833 defaulting sal.pc != 0 tests to do
11834
11835 0 1 pc
11836 1 1 pc _and_ line
11837 0 0 line
11838 1 0 <can't happen> */
11839
11840 sal_fullname = (sal.symtab == NULL
11841 ? NULL : symtab_to_fullname (sal.symtab));
11842
11843 /* Find all matching breakpoints and add them to 'found'. */
11844 ALL_BREAKPOINTS (b)
11845 {
11846 int match = 0;
11847 /* Are we going to delete b? */
11848 if (b->type != bp_none && !is_watchpoint (b))
11849 {
11850 struct bp_location *loc = b->loc;
11851 for (; loc; loc = loc->next)
11852 {
11853 /* If the user specified file:line, don't allow a PC
11854 match. This matches historical gdb behavior. */
11855 int pc_match = (!sal.explicit_line
11856 && sal.pc
11857 && (loc->pspace == sal.pspace)
11858 && (loc->address == sal.pc)
11859 && (!section_is_overlay (loc->section)
11860 || loc->section == sal.section));
11861 int line_match = 0;
11862
11863 if ((default_match || sal.explicit_line)
11864 && loc->symtab != NULL
11865 && sal_fullname != NULL
11866 && sal.pspace == loc->pspace
11867 && loc->line_number == sal.line
11868 && filename_cmp (symtab_to_fullname (loc->symtab),
11869 sal_fullname) == 0)
11870 line_match = 1;
11871
11872 if (pc_match || line_match)
11873 {
11874 match = 1;
11875 break;
11876 }
11877 }
11878 }
11879
11880 if (match)
11881 VEC_safe_push(breakpoint_p, found, b);
11882 }
11883 }
11884
11885 /* Now go thru the 'found' chain and delete them. */
11886 if (VEC_empty(breakpoint_p, found))
11887 {
11888 if (arg)
11889 error (_("No breakpoint at %s."), arg);
11890 else
11891 error (_("No breakpoint at this line."));
11892 }
11893
11894 /* Remove duplicates from the vec. */
11895 qsort (VEC_address (breakpoint_p, found),
11896 VEC_length (breakpoint_p, found),
11897 sizeof (breakpoint_p),
11898 compare_breakpoints);
11899 prev = VEC_index (breakpoint_p, found, 0);
11900 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11901 {
11902 if (b == prev)
11903 {
11904 VEC_ordered_remove (breakpoint_p, found, ix);
11905 --ix;
11906 }
11907 }
11908
11909 if (VEC_length(breakpoint_p, found) > 1)
11910 from_tty = 1; /* Always report if deleted more than one. */
11911 if (from_tty)
11912 {
11913 if (VEC_length(breakpoint_p, found) == 1)
11914 printf_unfiltered (_("Deleted breakpoint "));
11915 else
11916 printf_unfiltered (_("Deleted breakpoints "));
11917 }
11918
11919 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11920 {
11921 if (from_tty)
11922 printf_unfiltered ("%d ", b->number);
11923 delete_breakpoint (b);
11924 }
11925 if (from_tty)
11926 putchar_unfiltered ('\n');
11927
11928 do_cleanups (cleanups);
11929 }
11930 \f
11931 /* Delete breakpoint in BS if they are `delete' breakpoints and
11932 all breakpoints that are marked for deletion, whether hit or not.
11933 This is called after any breakpoint is hit, or after errors. */
11934
11935 void
11936 breakpoint_auto_delete (bpstat bs)
11937 {
11938 struct breakpoint *b, *b_tmp;
11939
11940 for (; bs; bs = bs->next)
11941 if (bs->breakpoint_at
11942 && bs->breakpoint_at->disposition == disp_del
11943 && bs->stop)
11944 delete_breakpoint (bs->breakpoint_at);
11945
11946 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11947 {
11948 if (b->disposition == disp_del_at_next_stop)
11949 delete_breakpoint (b);
11950 }
11951 }
11952
11953 /* A comparison function for bp_location AP and BP being interfaced to
11954 qsort. Sort elements primarily by their ADDRESS (no matter what
11955 does breakpoint_address_is_meaningful say for its OWNER),
11956 secondarily by ordering first permanent elements and
11957 terciarily just ensuring the array is sorted stable way despite
11958 qsort being an unstable algorithm. */
11959
11960 static int
11961 bp_locations_compare (const void *ap, const void *bp)
11962 {
11963 const struct bp_location *a = *(const struct bp_location **) ap;
11964 const struct bp_location *b = *(const struct bp_location **) bp;
11965
11966 if (a->address != b->address)
11967 return (a->address > b->address) - (a->address < b->address);
11968
11969 /* Sort locations at the same address by their pspace number, keeping
11970 locations of the same inferior (in a multi-inferior environment)
11971 grouped. */
11972
11973 if (a->pspace->num != b->pspace->num)
11974 return ((a->pspace->num > b->pspace->num)
11975 - (a->pspace->num < b->pspace->num));
11976
11977 /* Sort permanent breakpoints first. */
11978 if (a->permanent != b->permanent)
11979 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11980
11981 /* Make the internal GDB representation stable across GDB runs
11982 where A and B memory inside GDB can differ. Breakpoint locations of
11983 the same type at the same address can be sorted in arbitrary order. */
11984
11985 if (a->owner->number != b->owner->number)
11986 return ((a->owner->number > b->owner->number)
11987 - (a->owner->number < b->owner->number));
11988
11989 return (a > b) - (a < b);
11990 }
11991
11992 /* Set bp_locations_placed_address_before_address_max and
11993 bp_locations_shadow_len_after_address_max according to the current
11994 content of the bp_locations array. */
11995
11996 static void
11997 bp_locations_target_extensions_update (void)
11998 {
11999 struct bp_location *bl, **blp_tmp;
12000
12001 bp_locations_placed_address_before_address_max = 0;
12002 bp_locations_shadow_len_after_address_max = 0;
12003
12004 ALL_BP_LOCATIONS (bl, blp_tmp)
12005 {
12006 CORE_ADDR start, end, addr;
12007
12008 if (!bp_location_has_shadow (bl))
12009 continue;
12010
12011 start = bl->target_info.placed_address;
12012 end = start + bl->target_info.shadow_len;
12013
12014 gdb_assert (bl->address >= start);
12015 addr = bl->address - start;
12016 if (addr > bp_locations_placed_address_before_address_max)
12017 bp_locations_placed_address_before_address_max = addr;
12018
12019 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12020
12021 gdb_assert (bl->address < end);
12022 addr = end - bl->address;
12023 if (addr > bp_locations_shadow_len_after_address_max)
12024 bp_locations_shadow_len_after_address_max = addr;
12025 }
12026 }
12027
12028 /* Download tracepoint locations if they haven't been. */
12029
12030 static void
12031 download_tracepoint_locations (void)
12032 {
12033 struct breakpoint *b;
12034 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12035
12036 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12037
12038 ALL_TRACEPOINTS (b)
12039 {
12040 struct bp_location *bl;
12041 struct tracepoint *t;
12042 int bp_location_downloaded = 0;
12043
12044 if ((b->type == bp_fast_tracepoint
12045 ? !may_insert_fast_tracepoints
12046 : !may_insert_tracepoints))
12047 continue;
12048
12049 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12050 {
12051 if (target_can_download_tracepoint ())
12052 can_download_tracepoint = TRIBOOL_TRUE;
12053 else
12054 can_download_tracepoint = TRIBOOL_FALSE;
12055 }
12056
12057 if (can_download_tracepoint == TRIBOOL_FALSE)
12058 break;
12059
12060 for (bl = b->loc; bl; bl = bl->next)
12061 {
12062 /* In tracepoint, locations are _never_ duplicated, so
12063 should_be_inserted is equivalent to
12064 unduplicated_should_be_inserted. */
12065 if (!should_be_inserted (bl) || bl->inserted)
12066 continue;
12067
12068 switch_to_program_space_and_thread (bl->pspace);
12069
12070 target_download_tracepoint (bl);
12071
12072 bl->inserted = 1;
12073 bp_location_downloaded = 1;
12074 }
12075 t = (struct tracepoint *) b;
12076 t->number_on_target = b->number;
12077 if (bp_location_downloaded)
12078 observer_notify_breakpoint_modified (b);
12079 }
12080 }
12081
12082 /* Swap the insertion/duplication state between two locations. */
12083
12084 static void
12085 swap_insertion (struct bp_location *left, struct bp_location *right)
12086 {
12087 const int left_inserted = left->inserted;
12088 const int left_duplicate = left->duplicate;
12089 const int left_needs_update = left->needs_update;
12090 const struct bp_target_info left_target_info = left->target_info;
12091
12092 /* Locations of tracepoints can never be duplicated. */
12093 if (is_tracepoint (left->owner))
12094 gdb_assert (!left->duplicate);
12095 if (is_tracepoint (right->owner))
12096 gdb_assert (!right->duplicate);
12097
12098 left->inserted = right->inserted;
12099 left->duplicate = right->duplicate;
12100 left->needs_update = right->needs_update;
12101 left->target_info = right->target_info;
12102 right->inserted = left_inserted;
12103 right->duplicate = left_duplicate;
12104 right->needs_update = left_needs_update;
12105 right->target_info = left_target_info;
12106 }
12107
12108 /* Force the re-insertion of the locations at ADDRESS. This is called
12109 once a new/deleted/modified duplicate location is found and we are evaluating
12110 conditions on the target's side. Such conditions need to be updated on
12111 the target. */
12112
12113 static void
12114 force_breakpoint_reinsertion (struct bp_location *bl)
12115 {
12116 struct bp_location **locp = NULL, **loc2p;
12117 struct bp_location *loc;
12118 CORE_ADDR address = 0;
12119 int pspace_num;
12120
12121 address = bl->address;
12122 pspace_num = bl->pspace->num;
12123
12124 /* This is only meaningful if the target is
12125 evaluating conditions and if the user has
12126 opted for condition evaluation on the target's
12127 side. */
12128 if (gdb_evaluates_breakpoint_condition_p ()
12129 || !target_supports_evaluation_of_breakpoint_conditions ())
12130 return;
12131
12132 /* Flag all breakpoint locations with this address and
12133 the same program space as the location
12134 as "its condition has changed". We need to
12135 update the conditions on the target's side. */
12136 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12137 {
12138 loc = *loc2p;
12139
12140 if (!is_breakpoint (loc->owner)
12141 || pspace_num != loc->pspace->num)
12142 continue;
12143
12144 /* Flag the location appropriately. We use a different state to
12145 let everyone know that we already updated the set of locations
12146 with addr bl->address and program space bl->pspace. This is so
12147 we don't have to keep calling these functions just to mark locations
12148 that have already been marked. */
12149 loc->condition_changed = condition_updated;
12150
12151 /* Free the agent expression bytecode as well. We will compute
12152 it later on. */
12153 loc->cond_bytecode.reset ();
12154 }
12155 }
12156 /* Called whether new breakpoints are created, or existing breakpoints
12157 deleted, to update the global location list and recompute which
12158 locations are duplicate of which.
12159
12160 The INSERT_MODE flag determines whether locations may not, may, or
12161 shall be inserted now. See 'enum ugll_insert_mode' for more
12162 info. */
12163
12164 static void
12165 update_global_location_list (enum ugll_insert_mode insert_mode)
12166 {
12167 struct breakpoint *b;
12168 struct bp_location **locp, *loc;
12169 struct cleanup *cleanups;
12170 /* Last breakpoint location address that was marked for update. */
12171 CORE_ADDR last_addr = 0;
12172 /* Last breakpoint location program space that was marked for update. */
12173 int last_pspace_num = -1;
12174
12175 /* Used in the duplicates detection below. When iterating over all
12176 bp_locations, points to the first bp_location of a given address.
12177 Breakpoints and watchpoints of different types are never
12178 duplicates of each other. Keep one pointer for each type of
12179 breakpoint/watchpoint, so we only need to loop over all locations
12180 once. */
12181 struct bp_location *bp_loc_first; /* breakpoint */
12182 struct bp_location *wp_loc_first; /* hardware watchpoint */
12183 struct bp_location *awp_loc_first; /* access watchpoint */
12184 struct bp_location *rwp_loc_first; /* read watchpoint */
12185
12186 /* Saved former bp_locations array which we compare against the newly
12187 built bp_locations from the current state of ALL_BREAKPOINTS. */
12188 struct bp_location **old_locations, **old_locp;
12189 unsigned old_locations_count;
12190
12191 old_locations = bp_locations;
12192 old_locations_count = bp_locations_count;
12193 bp_locations = NULL;
12194 bp_locations_count = 0;
12195 cleanups = make_cleanup (xfree, old_locations);
12196
12197 ALL_BREAKPOINTS (b)
12198 for (loc = b->loc; loc; loc = loc->next)
12199 bp_locations_count++;
12200
12201 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12202 locp = bp_locations;
12203 ALL_BREAKPOINTS (b)
12204 for (loc = b->loc; loc; loc = loc->next)
12205 *locp++ = loc;
12206 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12207 bp_locations_compare);
12208
12209 bp_locations_target_extensions_update ();
12210
12211 /* Identify bp_location instances that are no longer present in the
12212 new list, and therefore should be freed. Note that it's not
12213 necessary that those locations should be removed from inferior --
12214 if there's another location at the same address (previously
12215 marked as duplicate), we don't need to remove/insert the
12216 location.
12217
12218 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12219 and former bp_location array state respectively. */
12220
12221 locp = bp_locations;
12222 for (old_locp = old_locations;
12223 old_locp < old_locations + old_locations_count;
12224 old_locp++)
12225 {
12226 struct bp_location *old_loc = *old_locp;
12227 struct bp_location **loc2p;
12228
12229 /* Tells if 'old_loc' is found among the new locations. If
12230 not, we have to free it. */
12231 int found_object = 0;
12232 /* Tells if the location should remain inserted in the target. */
12233 int keep_in_target = 0;
12234 int removed = 0;
12235
12236 /* Skip LOCP entries which will definitely never be needed.
12237 Stop either at or being the one matching OLD_LOC. */
12238 while (locp < bp_locations + bp_locations_count
12239 && (*locp)->address < old_loc->address)
12240 locp++;
12241
12242 for (loc2p = locp;
12243 (loc2p < bp_locations + bp_locations_count
12244 && (*loc2p)->address == old_loc->address);
12245 loc2p++)
12246 {
12247 /* Check if this is a new/duplicated location or a duplicated
12248 location that had its condition modified. If so, we want to send
12249 its condition to the target if evaluation of conditions is taking
12250 place there. */
12251 if ((*loc2p)->condition_changed == condition_modified
12252 && (last_addr != old_loc->address
12253 || last_pspace_num != old_loc->pspace->num))
12254 {
12255 force_breakpoint_reinsertion (*loc2p);
12256 last_pspace_num = old_loc->pspace->num;
12257 }
12258
12259 if (*loc2p == old_loc)
12260 found_object = 1;
12261 }
12262
12263 /* We have already handled this address, update it so that we don't
12264 have to go through updates again. */
12265 last_addr = old_loc->address;
12266
12267 /* Target-side condition evaluation: Handle deleted locations. */
12268 if (!found_object)
12269 force_breakpoint_reinsertion (old_loc);
12270
12271 /* If this location is no longer present, and inserted, look if
12272 there's maybe a new location at the same address. If so,
12273 mark that one inserted, and don't remove this one. This is
12274 needed so that we don't have a time window where a breakpoint
12275 at certain location is not inserted. */
12276
12277 if (old_loc->inserted)
12278 {
12279 /* If the location is inserted now, we might have to remove
12280 it. */
12281
12282 if (found_object && should_be_inserted (old_loc))
12283 {
12284 /* The location is still present in the location list,
12285 and still should be inserted. Don't do anything. */
12286 keep_in_target = 1;
12287 }
12288 else
12289 {
12290 /* This location still exists, but it won't be kept in the
12291 target since it may have been disabled. We proceed to
12292 remove its target-side condition. */
12293
12294 /* The location is either no longer present, or got
12295 disabled. See if there's another location at the
12296 same address, in which case we don't need to remove
12297 this one from the target. */
12298
12299 /* OLD_LOC comes from existing struct breakpoint. */
12300 if (breakpoint_address_is_meaningful (old_loc->owner))
12301 {
12302 for (loc2p = locp;
12303 (loc2p < bp_locations + bp_locations_count
12304 && (*loc2p)->address == old_loc->address);
12305 loc2p++)
12306 {
12307 struct bp_location *loc2 = *loc2p;
12308
12309 if (breakpoint_locations_match (loc2, old_loc))
12310 {
12311 /* Read watchpoint locations are switched to
12312 access watchpoints, if the former are not
12313 supported, but the latter are. */
12314 if (is_hardware_watchpoint (old_loc->owner))
12315 {
12316 gdb_assert (is_hardware_watchpoint (loc2->owner));
12317 loc2->watchpoint_type = old_loc->watchpoint_type;
12318 }
12319
12320 /* loc2 is a duplicated location. We need to check
12321 if it should be inserted in case it will be
12322 unduplicated. */
12323 if (loc2 != old_loc
12324 && unduplicated_should_be_inserted (loc2))
12325 {
12326 swap_insertion (old_loc, loc2);
12327 keep_in_target = 1;
12328 break;
12329 }
12330 }
12331 }
12332 }
12333 }
12334
12335 if (!keep_in_target)
12336 {
12337 if (remove_breakpoint (old_loc))
12338 {
12339 /* This is just about all we can do. We could keep
12340 this location on the global list, and try to
12341 remove it next time, but there's no particular
12342 reason why we will succeed next time.
12343
12344 Note that at this point, old_loc->owner is still
12345 valid, as delete_breakpoint frees the breakpoint
12346 only after calling us. */
12347 printf_filtered (_("warning: Error removing "
12348 "breakpoint %d\n"),
12349 old_loc->owner->number);
12350 }
12351 removed = 1;
12352 }
12353 }
12354
12355 if (!found_object)
12356 {
12357 if (removed && target_is_non_stop_p ()
12358 && need_moribund_for_location_type (old_loc))
12359 {
12360 /* This location was removed from the target. In
12361 non-stop mode, a race condition is possible where
12362 we've removed a breakpoint, but stop events for that
12363 breakpoint are already queued and will arrive later.
12364 We apply an heuristic to be able to distinguish such
12365 SIGTRAPs from other random SIGTRAPs: we keep this
12366 breakpoint location for a bit, and will retire it
12367 after we see some number of events. The theory here
12368 is that reporting of events should, "on the average",
12369 be fair, so after a while we'll see events from all
12370 threads that have anything of interest, and no longer
12371 need to keep this breakpoint location around. We
12372 don't hold locations forever so to reduce chances of
12373 mistaking a non-breakpoint SIGTRAP for a breakpoint
12374 SIGTRAP.
12375
12376 The heuristic failing can be disastrous on
12377 decr_pc_after_break targets.
12378
12379 On decr_pc_after_break targets, like e.g., x86-linux,
12380 if we fail to recognize a late breakpoint SIGTRAP,
12381 because events_till_retirement has reached 0 too
12382 soon, we'll fail to do the PC adjustment, and report
12383 a random SIGTRAP to the user. When the user resumes
12384 the inferior, it will most likely immediately crash
12385 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12386 corrupted, because of being resumed e.g., in the
12387 middle of a multi-byte instruction, or skipped a
12388 one-byte instruction. This was actually seen happen
12389 on native x86-linux, and should be less rare on
12390 targets that do not support new thread events, like
12391 remote, due to the heuristic depending on
12392 thread_count.
12393
12394 Mistaking a random SIGTRAP for a breakpoint trap
12395 causes similar symptoms (PC adjustment applied when
12396 it shouldn't), but then again, playing with SIGTRAPs
12397 behind the debugger's back is asking for trouble.
12398
12399 Since hardware watchpoint traps are always
12400 distinguishable from other traps, so we don't need to
12401 apply keep hardware watchpoint moribund locations
12402 around. We simply always ignore hardware watchpoint
12403 traps we can no longer explain. */
12404
12405 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12406 old_loc->owner = NULL;
12407
12408 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12409 }
12410 else
12411 {
12412 old_loc->owner = NULL;
12413 decref_bp_location (&old_loc);
12414 }
12415 }
12416 }
12417
12418 /* Rescan breakpoints at the same address and section, marking the
12419 first one as "first" and any others as "duplicates". This is so
12420 that the bpt instruction is only inserted once. If we have a
12421 permanent breakpoint at the same place as BPT, make that one the
12422 official one, and the rest as duplicates. Permanent breakpoints
12423 are sorted first for the same address.
12424
12425 Do the same for hardware watchpoints, but also considering the
12426 watchpoint's type (regular/access/read) and length. */
12427
12428 bp_loc_first = NULL;
12429 wp_loc_first = NULL;
12430 awp_loc_first = NULL;
12431 rwp_loc_first = NULL;
12432 ALL_BP_LOCATIONS (loc, locp)
12433 {
12434 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12435 non-NULL. */
12436 struct bp_location **loc_first_p;
12437 b = loc->owner;
12438
12439 if (!unduplicated_should_be_inserted (loc)
12440 || !breakpoint_address_is_meaningful (b)
12441 /* Don't detect duplicate for tracepoint locations because they are
12442 never duplicated. See the comments in field `duplicate' of
12443 `struct bp_location'. */
12444 || is_tracepoint (b))
12445 {
12446 /* Clear the condition modification flag. */
12447 loc->condition_changed = condition_unchanged;
12448 continue;
12449 }
12450
12451 if (b->type == bp_hardware_watchpoint)
12452 loc_first_p = &wp_loc_first;
12453 else if (b->type == bp_read_watchpoint)
12454 loc_first_p = &rwp_loc_first;
12455 else if (b->type == bp_access_watchpoint)
12456 loc_first_p = &awp_loc_first;
12457 else
12458 loc_first_p = &bp_loc_first;
12459
12460 if (*loc_first_p == NULL
12461 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12462 || !breakpoint_locations_match (loc, *loc_first_p))
12463 {
12464 *loc_first_p = loc;
12465 loc->duplicate = 0;
12466
12467 if (is_breakpoint (loc->owner) && loc->condition_changed)
12468 {
12469 loc->needs_update = 1;
12470 /* Clear the condition modification flag. */
12471 loc->condition_changed = condition_unchanged;
12472 }
12473 continue;
12474 }
12475
12476
12477 /* This and the above ensure the invariant that the first location
12478 is not duplicated, and is the inserted one.
12479 All following are marked as duplicated, and are not inserted. */
12480 if (loc->inserted)
12481 swap_insertion (loc, *loc_first_p);
12482 loc->duplicate = 1;
12483
12484 /* Clear the condition modification flag. */
12485 loc->condition_changed = condition_unchanged;
12486 }
12487
12488 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12489 {
12490 if (insert_mode != UGLL_DONT_INSERT)
12491 insert_breakpoint_locations ();
12492 else
12493 {
12494 /* Even though the caller told us to not insert new
12495 locations, we may still need to update conditions on the
12496 target's side of breakpoints that were already inserted
12497 if the target is evaluating breakpoint conditions. We
12498 only update conditions for locations that are marked
12499 "needs_update". */
12500 update_inserted_breakpoint_locations ();
12501 }
12502 }
12503
12504 if (insert_mode != UGLL_DONT_INSERT)
12505 download_tracepoint_locations ();
12506
12507 do_cleanups (cleanups);
12508 }
12509
12510 void
12511 breakpoint_retire_moribund (void)
12512 {
12513 struct bp_location *loc;
12514 int ix;
12515
12516 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12517 if (--(loc->events_till_retirement) == 0)
12518 {
12519 decref_bp_location (&loc);
12520 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12521 --ix;
12522 }
12523 }
12524
12525 static void
12526 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12527 {
12528
12529 TRY
12530 {
12531 update_global_location_list (insert_mode);
12532 }
12533 CATCH (e, RETURN_MASK_ERROR)
12534 {
12535 }
12536 END_CATCH
12537 }
12538
12539 /* Clear BKP from a BPS. */
12540
12541 static void
12542 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12543 {
12544 bpstat bs;
12545
12546 for (bs = bps; bs; bs = bs->next)
12547 if (bs->breakpoint_at == bpt)
12548 {
12549 bs->breakpoint_at = NULL;
12550 bs->old_val = NULL;
12551 /* bs->commands will be freed later. */
12552 }
12553 }
12554
12555 /* Callback for iterate_over_threads. */
12556 static int
12557 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12558 {
12559 struct breakpoint *bpt = (struct breakpoint *) data;
12560
12561 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12562 return 0;
12563 }
12564
12565 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12566 callbacks. */
12567
12568 static void
12569 say_where (struct breakpoint *b)
12570 {
12571 struct value_print_options opts;
12572
12573 get_user_print_options (&opts);
12574
12575 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12576 single string. */
12577 if (b->loc == NULL)
12578 {
12579 /* For pending locations, the output differs slightly based
12580 on b->extra_string. If this is non-NULL, it contains either
12581 a condition or dprintf arguments. */
12582 if (b->extra_string == NULL)
12583 {
12584 printf_filtered (_(" (%s) pending."),
12585 event_location_to_string (b->location.get ()));
12586 }
12587 else if (b->type == bp_dprintf)
12588 {
12589 printf_filtered (_(" (%s,%s) pending."),
12590 event_location_to_string (b->location.get ()),
12591 b->extra_string);
12592 }
12593 else
12594 {
12595 printf_filtered (_(" (%s %s) pending."),
12596 event_location_to_string (b->location.get ()),
12597 b->extra_string);
12598 }
12599 }
12600 else
12601 {
12602 if (opts.addressprint || b->loc->symtab == NULL)
12603 {
12604 printf_filtered (" at ");
12605 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12606 gdb_stdout);
12607 }
12608 if (b->loc->symtab != NULL)
12609 {
12610 /* If there is a single location, we can print the location
12611 more nicely. */
12612 if (b->loc->next == NULL)
12613 printf_filtered (": file %s, line %d.",
12614 symtab_to_filename_for_display (b->loc->symtab),
12615 b->loc->line_number);
12616 else
12617 /* This is not ideal, but each location may have a
12618 different file name, and this at least reflects the
12619 real situation somewhat. */
12620 printf_filtered (": %s.",
12621 event_location_to_string (b->location.get ()));
12622 }
12623
12624 if (b->loc->next)
12625 {
12626 struct bp_location *loc = b->loc;
12627 int n = 0;
12628 for (; loc; loc = loc->next)
12629 ++n;
12630 printf_filtered (" (%d locations)", n);
12631 }
12632 }
12633 }
12634
12635 /* Default bp_location_ops methods. */
12636
12637 static void
12638 bp_location_dtor (struct bp_location *self)
12639 {
12640 xfree (self->function_name);
12641 }
12642
12643 static const struct bp_location_ops bp_location_ops =
12644 {
12645 bp_location_dtor
12646 };
12647
12648 /* Destructor for the breakpoint base class. */
12649
12650 breakpoint::~breakpoint ()
12651 {
12652 decref_counted_command_line (&this->commands);
12653 xfree (this->cond_string);
12654 xfree (this->extra_string);
12655 xfree (this->filter);
12656 }
12657
12658 static struct bp_location *
12659 base_breakpoint_allocate_location (struct breakpoint *self)
12660 {
12661 return new bp_location (&bp_location_ops, self);
12662 }
12663
12664 static void
12665 base_breakpoint_re_set (struct breakpoint *b)
12666 {
12667 /* Nothing to re-set. */
12668 }
12669
12670 #define internal_error_pure_virtual_called() \
12671 gdb_assert_not_reached ("pure virtual function called")
12672
12673 static int
12674 base_breakpoint_insert_location (struct bp_location *bl)
12675 {
12676 internal_error_pure_virtual_called ();
12677 }
12678
12679 static int
12680 base_breakpoint_remove_location (struct bp_location *bl,
12681 enum remove_bp_reason reason)
12682 {
12683 internal_error_pure_virtual_called ();
12684 }
12685
12686 static int
12687 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12688 struct address_space *aspace,
12689 CORE_ADDR bp_addr,
12690 const struct target_waitstatus *ws)
12691 {
12692 internal_error_pure_virtual_called ();
12693 }
12694
12695 static void
12696 base_breakpoint_check_status (bpstat bs)
12697 {
12698 /* Always stop. */
12699 }
12700
12701 /* A "works_in_software_mode" breakpoint_ops method that just internal
12702 errors. */
12703
12704 static int
12705 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12706 {
12707 internal_error_pure_virtual_called ();
12708 }
12709
12710 /* A "resources_needed" breakpoint_ops method that just internal
12711 errors. */
12712
12713 static int
12714 base_breakpoint_resources_needed (const struct bp_location *bl)
12715 {
12716 internal_error_pure_virtual_called ();
12717 }
12718
12719 static enum print_stop_action
12720 base_breakpoint_print_it (bpstat bs)
12721 {
12722 internal_error_pure_virtual_called ();
12723 }
12724
12725 static void
12726 base_breakpoint_print_one_detail (const struct breakpoint *self,
12727 struct ui_out *uiout)
12728 {
12729 /* nothing */
12730 }
12731
12732 static void
12733 base_breakpoint_print_mention (struct breakpoint *b)
12734 {
12735 internal_error_pure_virtual_called ();
12736 }
12737
12738 static void
12739 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12740 {
12741 internal_error_pure_virtual_called ();
12742 }
12743
12744 static void
12745 base_breakpoint_create_sals_from_location
12746 (const struct event_location *location,
12747 struct linespec_result *canonical,
12748 enum bptype type_wanted)
12749 {
12750 internal_error_pure_virtual_called ();
12751 }
12752
12753 static void
12754 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12755 struct linespec_result *c,
12756 gdb::unique_xmalloc_ptr<char> cond_string,
12757 gdb::unique_xmalloc_ptr<char> extra_string,
12758 enum bptype type_wanted,
12759 enum bpdisp disposition,
12760 int thread,
12761 int task, int ignore_count,
12762 const struct breakpoint_ops *o,
12763 int from_tty, int enabled,
12764 int internal, unsigned flags)
12765 {
12766 internal_error_pure_virtual_called ();
12767 }
12768
12769 static std::vector<symtab_and_line>
12770 base_breakpoint_decode_location (struct breakpoint *b,
12771 const struct event_location *location,
12772 struct program_space *search_pspace)
12773 {
12774 internal_error_pure_virtual_called ();
12775 }
12776
12777 /* The default 'explains_signal' method. */
12778
12779 static int
12780 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12781 {
12782 return 1;
12783 }
12784
12785 /* The default "after_condition_true" method. */
12786
12787 static void
12788 base_breakpoint_after_condition_true (struct bpstats *bs)
12789 {
12790 /* Nothing to do. */
12791 }
12792
12793 struct breakpoint_ops base_breakpoint_ops =
12794 {
12795 base_breakpoint_allocate_location,
12796 base_breakpoint_re_set,
12797 base_breakpoint_insert_location,
12798 base_breakpoint_remove_location,
12799 base_breakpoint_breakpoint_hit,
12800 base_breakpoint_check_status,
12801 base_breakpoint_resources_needed,
12802 base_breakpoint_works_in_software_mode,
12803 base_breakpoint_print_it,
12804 NULL,
12805 base_breakpoint_print_one_detail,
12806 base_breakpoint_print_mention,
12807 base_breakpoint_print_recreate,
12808 base_breakpoint_create_sals_from_location,
12809 base_breakpoint_create_breakpoints_sal,
12810 base_breakpoint_decode_location,
12811 base_breakpoint_explains_signal,
12812 base_breakpoint_after_condition_true,
12813 };
12814
12815 /* Default breakpoint_ops methods. */
12816
12817 static void
12818 bkpt_re_set (struct breakpoint *b)
12819 {
12820 /* FIXME: is this still reachable? */
12821 if (breakpoint_event_location_empty_p (b))
12822 {
12823 /* Anything without a location can't be re-set. */
12824 delete_breakpoint (b);
12825 return;
12826 }
12827
12828 breakpoint_re_set_default (b);
12829 }
12830
12831 static int
12832 bkpt_insert_location (struct bp_location *bl)
12833 {
12834 CORE_ADDR addr = bl->target_info.reqstd_address;
12835
12836 bl->target_info.kind = breakpoint_kind (bl, &addr);
12837 bl->target_info.placed_address = addr;
12838
12839 if (bl->loc_type == bp_loc_hardware_breakpoint)
12840 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12841 else
12842 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12843 }
12844
12845 static int
12846 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12847 {
12848 if (bl->loc_type == bp_loc_hardware_breakpoint)
12849 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12850 else
12851 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12852 }
12853
12854 static int
12855 bkpt_breakpoint_hit (const struct bp_location *bl,
12856 struct address_space *aspace, CORE_ADDR bp_addr,
12857 const struct target_waitstatus *ws)
12858 {
12859 if (ws->kind != TARGET_WAITKIND_STOPPED
12860 || ws->value.sig != GDB_SIGNAL_TRAP)
12861 return 0;
12862
12863 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12864 aspace, bp_addr))
12865 return 0;
12866
12867 if (overlay_debugging /* unmapped overlay section */
12868 && section_is_overlay (bl->section)
12869 && !section_is_mapped (bl->section))
12870 return 0;
12871
12872 return 1;
12873 }
12874
12875 static int
12876 dprintf_breakpoint_hit (const struct bp_location *bl,
12877 struct address_space *aspace, CORE_ADDR bp_addr,
12878 const struct target_waitstatus *ws)
12879 {
12880 if (dprintf_style == dprintf_style_agent
12881 && target_can_run_breakpoint_commands ())
12882 {
12883 /* An agent-style dprintf never causes a stop. If we see a trap
12884 for this address it must be for a breakpoint that happens to
12885 be set at the same address. */
12886 return 0;
12887 }
12888
12889 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12890 }
12891
12892 static int
12893 bkpt_resources_needed (const struct bp_location *bl)
12894 {
12895 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12896
12897 return 1;
12898 }
12899
12900 static enum print_stop_action
12901 bkpt_print_it (bpstat bs)
12902 {
12903 struct breakpoint *b;
12904 const struct bp_location *bl;
12905 int bp_temp;
12906 struct ui_out *uiout = current_uiout;
12907
12908 gdb_assert (bs->bp_location_at != NULL);
12909
12910 bl = bs->bp_location_at;
12911 b = bs->breakpoint_at;
12912
12913 bp_temp = b->disposition == disp_del;
12914 if (bl->address != bl->requested_address)
12915 breakpoint_adjustment_warning (bl->requested_address,
12916 bl->address,
12917 b->number, 1);
12918 annotate_breakpoint (b->number);
12919 maybe_print_thread_hit_breakpoint (uiout);
12920
12921 if (bp_temp)
12922 uiout->text ("Temporary breakpoint ");
12923 else
12924 uiout->text ("Breakpoint ");
12925 if (uiout->is_mi_like_p ())
12926 {
12927 uiout->field_string ("reason",
12928 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12929 uiout->field_string ("disp", bpdisp_text (b->disposition));
12930 }
12931 uiout->field_int ("bkptno", b->number);
12932 uiout->text (", ");
12933
12934 return PRINT_SRC_AND_LOC;
12935 }
12936
12937 static void
12938 bkpt_print_mention (struct breakpoint *b)
12939 {
12940 if (current_uiout->is_mi_like_p ())
12941 return;
12942
12943 switch (b->type)
12944 {
12945 case bp_breakpoint:
12946 case bp_gnu_ifunc_resolver:
12947 if (b->disposition == disp_del)
12948 printf_filtered (_("Temporary breakpoint"));
12949 else
12950 printf_filtered (_("Breakpoint"));
12951 printf_filtered (_(" %d"), b->number);
12952 if (b->type == bp_gnu_ifunc_resolver)
12953 printf_filtered (_(" at gnu-indirect-function resolver"));
12954 break;
12955 case bp_hardware_breakpoint:
12956 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12957 break;
12958 case bp_dprintf:
12959 printf_filtered (_("Dprintf %d"), b->number);
12960 break;
12961 }
12962
12963 say_where (b);
12964 }
12965
12966 static void
12967 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12968 {
12969 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12970 fprintf_unfiltered (fp, "tbreak");
12971 else if (tp->type == bp_breakpoint)
12972 fprintf_unfiltered (fp, "break");
12973 else if (tp->type == bp_hardware_breakpoint
12974 && tp->disposition == disp_del)
12975 fprintf_unfiltered (fp, "thbreak");
12976 else if (tp->type == bp_hardware_breakpoint)
12977 fprintf_unfiltered (fp, "hbreak");
12978 else
12979 internal_error (__FILE__, __LINE__,
12980 _("unhandled breakpoint type %d"), (int) tp->type);
12981
12982 fprintf_unfiltered (fp, " %s",
12983 event_location_to_string (tp->location.get ()));
12984
12985 /* Print out extra_string if this breakpoint is pending. It might
12986 contain, for example, conditions that were set by the user. */
12987 if (tp->loc == NULL && tp->extra_string != NULL)
12988 fprintf_unfiltered (fp, " %s", tp->extra_string);
12989
12990 print_recreate_thread (tp, fp);
12991 }
12992
12993 static void
12994 bkpt_create_sals_from_location (const struct event_location *location,
12995 struct linespec_result *canonical,
12996 enum bptype type_wanted)
12997 {
12998 create_sals_from_location_default (location, canonical, type_wanted);
12999 }
13000
13001 static void
13002 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13003 struct linespec_result *canonical,
13004 gdb::unique_xmalloc_ptr<char> cond_string,
13005 gdb::unique_xmalloc_ptr<char> extra_string,
13006 enum bptype type_wanted,
13007 enum bpdisp disposition,
13008 int thread,
13009 int task, int ignore_count,
13010 const struct breakpoint_ops *ops,
13011 int from_tty, int enabled,
13012 int internal, unsigned flags)
13013 {
13014 create_breakpoints_sal_default (gdbarch, canonical,
13015 std::move (cond_string),
13016 std::move (extra_string),
13017 type_wanted,
13018 disposition, thread, task,
13019 ignore_count, ops, from_tty,
13020 enabled, internal, flags);
13021 }
13022
13023 static std::vector<symtab_and_line>
13024 bkpt_decode_location (struct breakpoint *b,
13025 const struct event_location *location,
13026 struct program_space *search_pspace)
13027 {
13028 return decode_location_default (b, location, search_pspace);
13029 }
13030
13031 /* Virtual table for internal breakpoints. */
13032
13033 static void
13034 internal_bkpt_re_set (struct breakpoint *b)
13035 {
13036 switch (b->type)
13037 {
13038 /* Delete overlay event and longjmp master breakpoints; they
13039 will be reset later by breakpoint_re_set. */
13040 case bp_overlay_event:
13041 case bp_longjmp_master:
13042 case bp_std_terminate_master:
13043 case bp_exception_master:
13044 delete_breakpoint (b);
13045 break;
13046
13047 /* This breakpoint is special, it's set up when the inferior
13048 starts and we really don't want to touch it. */
13049 case bp_shlib_event:
13050
13051 /* Like bp_shlib_event, this breakpoint type is special. Once
13052 it is set up, we do not want to touch it. */
13053 case bp_thread_event:
13054 break;
13055 }
13056 }
13057
13058 static void
13059 internal_bkpt_check_status (bpstat bs)
13060 {
13061 if (bs->breakpoint_at->type == bp_shlib_event)
13062 {
13063 /* If requested, stop when the dynamic linker notifies GDB of
13064 events. This allows the user to get control and place
13065 breakpoints in initializer routines for dynamically loaded
13066 objects (among other things). */
13067 bs->stop = stop_on_solib_events;
13068 bs->print = stop_on_solib_events;
13069 }
13070 else
13071 bs->stop = 0;
13072 }
13073
13074 static enum print_stop_action
13075 internal_bkpt_print_it (bpstat bs)
13076 {
13077 struct breakpoint *b;
13078
13079 b = bs->breakpoint_at;
13080
13081 switch (b->type)
13082 {
13083 case bp_shlib_event:
13084 /* Did we stop because the user set the stop_on_solib_events
13085 variable? (If so, we report this as a generic, "Stopped due
13086 to shlib event" message.) */
13087 print_solib_event (0);
13088 break;
13089
13090 case bp_thread_event:
13091 /* Not sure how we will get here.
13092 GDB should not stop for these breakpoints. */
13093 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13094 break;
13095
13096 case bp_overlay_event:
13097 /* By analogy with the thread event, GDB should not stop for these. */
13098 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13099 break;
13100
13101 case bp_longjmp_master:
13102 /* These should never be enabled. */
13103 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13104 break;
13105
13106 case bp_std_terminate_master:
13107 /* These should never be enabled. */
13108 printf_filtered (_("std::terminate Master Breakpoint: "
13109 "gdb should not stop!\n"));
13110 break;
13111
13112 case bp_exception_master:
13113 /* These should never be enabled. */
13114 printf_filtered (_("Exception Master Breakpoint: "
13115 "gdb should not stop!\n"));
13116 break;
13117 }
13118
13119 return PRINT_NOTHING;
13120 }
13121
13122 static void
13123 internal_bkpt_print_mention (struct breakpoint *b)
13124 {
13125 /* Nothing to mention. These breakpoints are internal. */
13126 }
13127
13128 /* Virtual table for momentary breakpoints */
13129
13130 static void
13131 momentary_bkpt_re_set (struct breakpoint *b)
13132 {
13133 /* Keep temporary breakpoints, which can be encountered when we step
13134 over a dlopen call and solib_add is resetting the breakpoints.
13135 Otherwise these should have been blown away via the cleanup chain
13136 or by breakpoint_init_inferior when we rerun the executable. */
13137 }
13138
13139 static void
13140 momentary_bkpt_check_status (bpstat bs)
13141 {
13142 /* Nothing. The point of these breakpoints is causing a stop. */
13143 }
13144
13145 static enum print_stop_action
13146 momentary_bkpt_print_it (bpstat bs)
13147 {
13148 return PRINT_UNKNOWN;
13149 }
13150
13151 static void
13152 momentary_bkpt_print_mention (struct breakpoint *b)
13153 {
13154 /* Nothing to mention. These breakpoints are internal. */
13155 }
13156
13157 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13158
13159 It gets cleared already on the removal of the first one of such placed
13160 breakpoints. This is OK as they get all removed altogether. */
13161
13162 longjmp_breakpoint::~longjmp_breakpoint ()
13163 {
13164 thread_info *tp = find_thread_global_id (this->thread);
13165
13166 if (tp != NULL)
13167 tp->initiating_frame = null_frame_id;
13168 }
13169
13170 /* Specific methods for probe breakpoints. */
13171
13172 static int
13173 bkpt_probe_insert_location (struct bp_location *bl)
13174 {
13175 int v = bkpt_insert_location (bl);
13176
13177 if (v == 0)
13178 {
13179 /* The insertion was successful, now let's set the probe's semaphore
13180 if needed. */
13181 if (bl->probe.probe->pops->set_semaphore != NULL)
13182 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13183 bl->probe.objfile,
13184 bl->gdbarch);
13185 }
13186
13187 return v;
13188 }
13189
13190 static int
13191 bkpt_probe_remove_location (struct bp_location *bl,
13192 enum remove_bp_reason reason)
13193 {
13194 /* Let's clear the semaphore before removing the location. */
13195 if (bl->probe.probe->pops->clear_semaphore != NULL)
13196 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13197 bl->probe.objfile,
13198 bl->gdbarch);
13199
13200 return bkpt_remove_location (bl, reason);
13201 }
13202
13203 static void
13204 bkpt_probe_create_sals_from_location (const struct event_location *location,
13205 struct linespec_result *canonical,
13206 enum bptype type_wanted)
13207 {
13208 struct linespec_sals lsal;
13209
13210 lsal.sals = parse_probes (location, NULL, canonical);
13211 lsal.canonical
13212 = xstrdup (event_location_to_string (canonical->location.get ()));
13213 canonical->lsals.push_back (std::move (lsal));
13214 }
13215
13216 static std::vector<symtab_and_line>
13217 bkpt_probe_decode_location (struct breakpoint *b,
13218 const struct event_location *location,
13219 struct program_space *search_pspace)
13220 {
13221 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13222 if (sals.empty ())
13223 error (_("probe not found"));
13224 return sals;
13225 }
13226
13227 /* The breakpoint_ops structure to be used in tracepoints. */
13228
13229 static void
13230 tracepoint_re_set (struct breakpoint *b)
13231 {
13232 breakpoint_re_set_default (b);
13233 }
13234
13235 static int
13236 tracepoint_breakpoint_hit (const struct bp_location *bl,
13237 struct address_space *aspace, CORE_ADDR bp_addr,
13238 const struct target_waitstatus *ws)
13239 {
13240 /* By definition, the inferior does not report stops at
13241 tracepoints. */
13242 return 0;
13243 }
13244
13245 static void
13246 tracepoint_print_one_detail (const struct breakpoint *self,
13247 struct ui_out *uiout)
13248 {
13249 struct tracepoint *tp = (struct tracepoint *) self;
13250 if (tp->static_trace_marker_id)
13251 {
13252 gdb_assert (self->type == bp_static_tracepoint);
13253
13254 uiout->text ("\tmarker id is ");
13255 uiout->field_string ("static-tracepoint-marker-string-id",
13256 tp->static_trace_marker_id);
13257 uiout->text ("\n");
13258 }
13259 }
13260
13261 static void
13262 tracepoint_print_mention (struct breakpoint *b)
13263 {
13264 if (current_uiout->is_mi_like_p ())
13265 return;
13266
13267 switch (b->type)
13268 {
13269 case bp_tracepoint:
13270 printf_filtered (_("Tracepoint"));
13271 printf_filtered (_(" %d"), b->number);
13272 break;
13273 case bp_fast_tracepoint:
13274 printf_filtered (_("Fast tracepoint"));
13275 printf_filtered (_(" %d"), b->number);
13276 break;
13277 case bp_static_tracepoint:
13278 printf_filtered (_("Static tracepoint"));
13279 printf_filtered (_(" %d"), b->number);
13280 break;
13281 default:
13282 internal_error (__FILE__, __LINE__,
13283 _("unhandled tracepoint type %d"), (int) b->type);
13284 }
13285
13286 say_where (b);
13287 }
13288
13289 static void
13290 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13291 {
13292 struct tracepoint *tp = (struct tracepoint *) self;
13293
13294 if (self->type == bp_fast_tracepoint)
13295 fprintf_unfiltered (fp, "ftrace");
13296 else if (self->type == bp_static_tracepoint)
13297 fprintf_unfiltered (fp, "strace");
13298 else if (self->type == bp_tracepoint)
13299 fprintf_unfiltered (fp, "trace");
13300 else
13301 internal_error (__FILE__, __LINE__,
13302 _("unhandled tracepoint type %d"), (int) self->type);
13303
13304 fprintf_unfiltered (fp, " %s",
13305 event_location_to_string (self->location.get ()));
13306 print_recreate_thread (self, fp);
13307
13308 if (tp->pass_count)
13309 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13310 }
13311
13312 static void
13313 tracepoint_create_sals_from_location (const struct event_location *location,
13314 struct linespec_result *canonical,
13315 enum bptype type_wanted)
13316 {
13317 create_sals_from_location_default (location, canonical, type_wanted);
13318 }
13319
13320 static void
13321 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13322 struct linespec_result *canonical,
13323 gdb::unique_xmalloc_ptr<char> cond_string,
13324 gdb::unique_xmalloc_ptr<char> extra_string,
13325 enum bptype type_wanted,
13326 enum bpdisp disposition,
13327 int thread,
13328 int task, int ignore_count,
13329 const struct breakpoint_ops *ops,
13330 int from_tty, int enabled,
13331 int internal, unsigned flags)
13332 {
13333 create_breakpoints_sal_default (gdbarch, canonical,
13334 std::move (cond_string),
13335 std::move (extra_string),
13336 type_wanted,
13337 disposition, thread, task,
13338 ignore_count, ops, from_tty,
13339 enabled, internal, flags);
13340 }
13341
13342 static std::vector<symtab_and_line>
13343 tracepoint_decode_location (struct breakpoint *b,
13344 const struct event_location *location,
13345 struct program_space *search_pspace)
13346 {
13347 return decode_location_default (b, location, search_pspace);
13348 }
13349
13350 struct breakpoint_ops tracepoint_breakpoint_ops;
13351
13352 /* The breakpoint_ops structure to be use on tracepoints placed in a
13353 static probe. */
13354
13355 static void
13356 tracepoint_probe_create_sals_from_location
13357 (const struct event_location *location,
13358 struct linespec_result *canonical,
13359 enum bptype type_wanted)
13360 {
13361 /* We use the same method for breakpoint on probes. */
13362 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13363 }
13364
13365 static std::vector<symtab_and_line>
13366 tracepoint_probe_decode_location (struct breakpoint *b,
13367 const struct event_location *location,
13368 struct program_space *search_pspace)
13369 {
13370 /* We use the same method for breakpoint on probes. */
13371 return bkpt_probe_decode_location (b, location, search_pspace);
13372 }
13373
13374 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13375
13376 /* Dprintf breakpoint_ops methods. */
13377
13378 static void
13379 dprintf_re_set (struct breakpoint *b)
13380 {
13381 breakpoint_re_set_default (b);
13382
13383 /* extra_string should never be non-NULL for dprintf. */
13384 gdb_assert (b->extra_string != NULL);
13385
13386 /* 1 - connect to target 1, that can run breakpoint commands.
13387 2 - create a dprintf, which resolves fine.
13388 3 - disconnect from target 1
13389 4 - connect to target 2, that can NOT run breakpoint commands.
13390
13391 After steps #3/#4, you'll want the dprintf command list to
13392 be updated, because target 1 and 2 may well return different
13393 answers for target_can_run_breakpoint_commands().
13394 Given absence of finer grained resetting, we get to do
13395 it all the time. */
13396 if (b->extra_string != NULL)
13397 update_dprintf_command_list (b);
13398 }
13399
13400 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13401
13402 static void
13403 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13404 {
13405 fprintf_unfiltered (fp, "dprintf %s,%s",
13406 event_location_to_string (tp->location.get ()),
13407 tp->extra_string);
13408 print_recreate_thread (tp, fp);
13409 }
13410
13411 /* Implement the "after_condition_true" breakpoint_ops method for
13412 dprintf.
13413
13414 dprintf's are implemented with regular commands in their command
13415 list, but we run the commands here instead of before presenting the
13416 stop to the user, as dprintf's don't actually cause a stop. This
13417 also makes it so that the commands of multiple dprintfs at the same
13418 address are all handled. */
13419
13420 static void
13421 dprintf_after_condition_true (struct bpstats *bs)
13422 {
13423 struct cleanup *old_chain;
13424 struct bpstats tmp_bs = { NULL };
13425 struct bpstats *tmp_bs_p = &tmp_bs;
13426
13427 /* dprintf's never cause a stop. This wasn't set in the
13428 check_status hook instead because that would make the dprintf's
13429 condition not be evaluated. */
13430 bs->stop = 0;
13431
13432 /* Run the command list here. Take ownership of it instead of
13433 copying. We never want these commands to run later in
13434 bpstat_do_actions, if a breakpoint that causes a stop happens to
13435 be set at same address as this dprintf, or even if running the
13436 commands here throws. */
13437 tmp_bs.commands = bs->commands;
13438 bs->commands = NULL;
13439 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13440
13441 bpstat_do_actions_1 (&tmp_bs_p);
13442
13443 /* 'tmp_bs.commands' will usually be NULL by now, but
13444 bpstat_do_actions_1 may return early without processing the whole
13445 list. */
13446 do_cleanups (old_chain);
13447 }
13448
13449 /* The breakpoint_ops structure to be used on static tracepoints with
13450 markers (`-m'). */
13451
13452 static void
13453 strace_marker_create_sals_from_location (const struct event_location *location,
13454 struct linespec_result *canonical,
13455 enum bptype type_wanted)
13456 {
13457 struct linespec_sals lsal;
13458 const char *arg_start, *arg;
13459 char *str;
13460 struct cleanup *cleanup;
13461
13462 arg = arg_start = get_linespec_location (location);
13463 lsal.sals = decode_static_tracepoint_spec (&arg);
13464
13465 str = savestring (arg_start, arg - arg_start);
13466 cleanup = make_cleanup (xfree, str);
13467 canonical->location = new_linespec_location (&str);
13468 do_cleanups (cleanup);
13469
13470 lsal.canonical
13471 = xstrdup (event_location_to_string (canonical->location.get ()));
13472 canonical->lsals.push_back (std::move (lsal));
13473 }
13474
13475 static void
13476 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13477 struct linespec_result *canonical,
13478 gdb::unique_xmalloc_ptr<char> cond_string,
13479 gdb::unique_xmalloc_ptr<char> extra_string,
13480 enum bptype type_wanted,
13481 enum bpdisp disposition,
13482 int thread,
13483 int task, int ignore_count,
13484 const struct breakpoint_ops *ops,
13485 int from_tty, int enabled,
13486 int internal, unsigned flags)
13487 {
13488 const linespec_sals &lsal = canonical->lsals[0];
13489
13490 /* If the user is creating a static tracepoint by marker id
13491 (strace -m MARKER_ID), then store the sals index, so that
13492 breakpoint_re_set can try to match up which of the newly
13493 found markers corresponds to this one, and, don't try to
13494 expand multiple locations for each sal, given than SALS
13495 already should contain all sals for MARKER_ID. */
13496
13497 for (size_t i = 0; i < lsal.sals.size (); i++)
13498 {
13499 event_location_up location
13500 = copy_event_location (canonical->location.get ());
13501
13502 std::unique_ptr<tracepoint> tp (new tracepoint ());
13503 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13504 std::move (location), NULL,
13505 std::move (cond_string),
13506 std::move (extra_string),
13507 type_wanted, disposition,
13508 thread, task, ignore_count, ops,
13509 from_tty, enabled, internal, flags,
13510 canonical->special_display);
13511 /* Given that its possible to have multiple markers with
13512 the same string id, if the user is creating a static
13513 tracepoint by marker id ("strace -m MARKER_ID"), then
13514 store the sals index, so that breakpoint_re_set can
13515 try to match up which of the newly found markers
13516 corresponds to this one */
13517 tp->static_trace_marker_id_idx = i;
13518
13519 install_breakpoint (internal, std::move (tp), 0);
13520 }
13521 }
13522
13523 static std::vector<symtab_and_line>
13524 strace_marker_decode_location (struct breakpoint *b,
13525 const struct event_location *location,
13526 struct program_space *search_pspace)
13527 {
13528 struct tracepoint *tp = (struct tracepoint *) b;
13529 const char *s = get_linespec_location (location);
13530
13531 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13532 if (sals.size () > tp->static_trace_marker_id_idx)
13533 {
13534 sals[0] = sals[tp->static_trace_marker_id_idx];
13535 sals.resize (1);
13536 return sals;
13537 }
13538 else
13539 error (_("marker %s not found"), tp->static_trace_marker_id);
13540 }
13541
13542 static struct breakpoint_ops strace_marker_breakpoint_ops;
13543
13544 static int
13545 strace_marker_p (struct breakpoint *b)
13546 {
13547 return b->ops == &strace_marker_breakpoint_ops;
13548 }
13549
13550 /* Delete a breakpoint and clean up all traces of it in the data
13551 structures. */
13552
13553 void
13554 delete_breakpoint (struct breakpoint *bpt)
13555 {
13556 struct breakpoint *b;
13557
13558 gdb_assert (bpt != NULL);
13559
13560 /* Has this bp already been deleted? This can happen because
13561 multiple lists can hold pointers to bp's. bpstat lists are
13562 especial culprits.
13563
13564 One example of this happening is a watchpoint's scope bp. When
13565 the scope bp triggers, we notice that the watchpoint is out of
13566 scope, and delete it. We also delete its scope bp. But the
13567 scope bp is marked "auto-deleting", and is already on a bpstat.
13568 That bpstat is then checked for auto-deleting bp's, which are
13569 deleted.
13570
13571 A real solution to this problem might involve reference counts in
13572 bp's, and/or giving them pointers back to their referencing
13573 bpstat's, and teaching delete_breakpoint to only free a bp's
13574 storage when no more references were extent. A cheaper bandaid
13575 was chosen. */
13576 if (bpt->type == bp_none)
13577 return;
13578
13579 /* At least avoid this stale reference until the reference counting
13580 of breakpoints gets resolved. */
13581 if (bpt->related_breakpoint != bpt)
13582 {
13583 struct breakpoint *related;
13584 struct watchpoint *w;
13585
13586 if (bpt->type == bp_watchpoint_scope)
13587 w = (struct watchpoint *) bpt->related_breakpoint;
13588 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13589 w = (struct watchpoint *) bpt;
13590 else
13591 w = NULL;
13592 if (w != NULL)
13593 watchpoint_del_at_next_stop (w);
13594
13595 /* Unlink bpt from the bpt->related_breakpoint ring. */
13596 for (related = bpt; related->related_breakpoint != bpt;
13597 related = related->related_breakpoint);
13598 related->related_breakpoint = bpt->related_breakpoint;
13599 bpt->related_breakpoint = bpt;
13600 }
13601
13602 /* watch_command_1 creates a watchpoint but only sets its number if
13603 update_watchpoint succeeds in creating its bp_locations. If there's
13604 a problem in that process, we'll be asked to delete the half-created
13605 watchpoint. In that case, don't announce the deletion. */
13606 if (bpt->number)
13607 observer_notify_breakpoint_deleted (bpt);
13608
13609 if (breakpoint_chain == bpt)
13610 breakpoint_chain = bpt->next;
13611
13612 ALL_BREAKPOINTS (b)
13613 if (b->next == bpt)
13614 {
13615 b->next = bpt->next;
13616 break;
13617 }
13618
13619 /* Be sure no bpstat's are pointing at the breakpoint after it's
13620 been freed. */
13621 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13622 in all threads for now. Note that we cannot just remove bpstats
13623 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13624 commands are associated with the bpstat; if we remove it here,
13625 then the later call to bpstat_do_actions (&stop_bpstat); in
13626 event-top.c won't do anything, and temporary breakpoints with
13627 commands won't work. */
13628
13629 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13630
13631 /* Now that breakpoint is removed from breakpoint list, update the
13632 global location list. This will remove locations that used to
13633 belong to this breakpoint. Do this before freeing the breakpoint
13634 itself, since remove_breakpoint looks at location's owner. It
13635 might be better design to have location completely
13636 self-contained, but it's not the case now. */
13637 update_global_location_list (UGLL_DONT_INSERT);
13638
13639 /* On the chance that someone will soon try again to delete this
13640 same bp, we mark it as deleted before freeing its storage. */
13641 bpt->type = bp_none;
13642 delete bpt;
13643 }
13644
13645 static void
13646 do_delete_breakpoint_cleanup (void *b)
13647 {
13648 delete_breakpoint ((struct breakpoint *) b);
13649 }
13650
13651 struct cleanup *
13652 make_cleanup_delete_breakpoint (struct breakpoint *b)
13653 {
13654 return make_cleanup (do_delete_breakpoint_cleanup, b);
13655 }
13656
13657 /* Iterator function to call a user-provided callback function once
13658 for each of B and its related breakpoints. */
13659
13660 static void
13661 iterate_over_related_breakpoints (struct breakpoint *b,
13662 void (*function) (struct breakpoint *,
13663 void *),
13664 void *data)
13665 {
13666 struct breakpoint *related;
13667
13668 related = b;
13669 do
13670 {
13671 struct breakpoint *next;
13672
13673 /* FUNCTION may delete RELATED. */
13674 next = related->related_breakpoint;
13675
13676 if (next == related)
13677 {
13678 /* RELATED is the last ring entry. */
13679 function (related, data);
13680
13681 /* FUNCTION may have deleted it, so we'd never reach back to
13682 B. There's nothing left to do anyway, so just break
13683 out. */
13684 break;
13685 }
13686 else
13687 function (related, data);
13688
13689 related = next;
13690 }
13691 while (related != b);
13692 }
13693
13694 static void
13695 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13696 {
13697 delete_breakpoint (b);
13698 }
13699
13700 /* A callback for map_breakpoint_numbers that calls
13701 delete_breakpoint. */
13702
13703 static void
13704 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13705 {
13706 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13707 }
13708
13709 void
13710 delete_command (char *arg, int from_tty)
13711 {
13712 struct breakpoint *b, *b_tmp;
13713
13714 dont_repeat ();
13715
13716 if (arg == 0)
13717 {
13718 int breaks_to_delete = 0;
13719
13720 /* Delete all breakpoints if no argument. Do not delete
13721 internal breakpoints, these have to be deleted with an
13722 explicit breakpoint number argument. */
13723 ALL_BREAKPOINTS (b)
13724 if (user_breakpoint_p (b))
13725 {
13726 breaks_to_delete = 1;
13727 break;
13728 }
13729
13730 /* Ask user only if there are some breakpoints to delete. */
13731 if (!from_tty
13732 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13733 {
13734 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13735 if (user_breakpoint_p (b))
13736 delete_breakpoint (b);
13737 }
13738 }
13739 else
13740 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13741 }
13742
13743 /* Return true if all locations of B bound to PSPACE are pending. If
13744 PSPACE is NULL, all locations of all program spaces are
13745 considered. */
13746
13747 static int
13748 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13749 {
13750 struct bp_location *loc;
13751
13752 for (loc = b->loc; loc != NULL; loc = loc->next)
13753 if ((pspace == NULL
13754 || loc->pspace == pspace)
13755 && !loc->shlib_disabled
13756 && !loc->pspace->executing_startup)
13757 return 0;
13758 return 1;
13759 }
13760
13761 /* Subroutine of update_breakpoint_locations to simplify it.
13762 Return non-zero if multiple fns in list LOC have the same name.
13763 Null names are ignored. */
13764
13765 static int
13766 ambiguous_names_p (struct bp_location *loc)
13767 {
13768 struct bp_location *l;
13769 htab_t htab = htab_create_alloc (13, htab_hash_string,
13770 (int (*) (const void *,
13771 const void *)) streq,
13772 NULL, xcalloc, xfree);
13773
13774 for (l = loc; l != NULL; l = l->next)
13775 {
13776 const char **slot;
13777 const char *name = l->function_name;
13778
13779 /* Allow for some names to be NULL, ignore them. */
13780 if (name == NULL)
13781 continue;
13782
13783 slot = (const char **) htab_find_slot (htab, (const void *) name,
13784 INSERT);
13785 /* NOTE: We can assume slot != NULL here because xcalloc never
13786 returns NULL. */
13787 if (*slot != NULL)
13788 {
13789 htab_delete (htab);
13790 return 1;
13791 }
13792 *slot = name;
13793 }
13794
13795 htab_delete (htab);
13796 return 0;
13797 }
13798
13799 /* When symbols change, it probably means the sources changed as well,
13800 and it might mean the static tracepoint markers are no longer at
13801 the same address or line numbers they used to be at last we
13802 checked. Losing your static tracepoints whenever you rebuild is
13803 undesirable. This function tries to resync/rematch gdb static
13804 tracepoints with the markers on the target, for static tracepoints
13805 that have not been set by marker id. Static tracepoint that have
13806 been set by marker id are reset by marker id in breakpoint_re_set.
13807 The heuristic is:
13808
13809 1) For a tracepoint set at a specific address, look for a marker at
13810 the old PC. If one is found there, assume to be the same marker.
13811 If the name / string id of the marker found is different from the
13812 previous known name, assume that means the user renamed the marker
13813 in the sources, and output a warning.
13814
13815 2) For a tracepoint set at a given line number, look for a marker
13816 at the new address of the old line number. If one is found there,
13817 assume to be the same marker. If the name / string id of the
13818 marker found is different from the previous known name, assume that
13819 means the user renamed the marker in the sources, and output a
13820 warning.
13821
13822 3) If a marker is no longer found at the same address or line, it
13823 may mean the marker no longer exists. But it may also just mean
13824 the code changed a bit. Maybe the user added a few lines of code
13825 that made the marker move up or down (in line number terms). Ask
13826 the target for info about the marker with the string id as we knew
13827 it. If found, update line number and address in the matching
13828 static tracepoint. This will get confused if there's more than one
13829 marker with the same ID (possible in UST, although unadvised
13830 precisely because it confuses tools). */
13831
13832 static struct symtab_and_line
13833 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13834 {
13835 struct tracepoint *tp = (struct tracepoint *) b;
13836 struct static_tracepoint_marker marker;
13837 CORE_ADDR pc;
13838
13839 pc = sal.pc;
13840 if (sal.line)
13841 find_line_pc (sal.symtab, sal.line, &pc);
13842
13843 if (target_static_tracepoint_marker_at (pc, &marker))
13844 {
13845 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13846 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13847 b->number,
13848 tp->static_trace_marker_id, marker.str_id);
13849
13850 xfree (tp->static_trace_marker_id);
13851 tp->static_trace_marker_id = xstrdup (marker.str_id);
13852 release_static_tracepoint_marker (&marker);
13853
13854 return sal;
13855 }
13856
13857 /* Old marker wasn't found on target at lineno. Try looking it up
13858 by string ID. */
13859 if (!sal.explicit_pc
13860 && sal.line != 0
13861 && sal.symtab != NULL
13862 && tp->static_trace_marker_id != NULL)
13863 {
13864 VEC(static_tracepoint_marker_p) *markers;
13865
13866 markers
13867 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13868
13869 if (!VEC_empty(static_tracepoint_marker_p, markers))
13870 {
13871 struct symbol *sym;
13872 struct static_tracepoint_marker *tpmarker;
13873 struct ui_out *uiout = current_uiout;
13874 struct explicit_location explicit_loc;
13875
13876 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13877
13878 xfree (tp->static_trace_marker_id);
13879 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13880
13881 warning (_("marker for static tracepoint %d (%s) not "
13882 "found at previous line number"),
13883 b->number, tp->static_trace_marker_id);
13884
13885 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13886 sym = find_pc_sect_function (tpmarker->address, NULL);
13887 uiout->text ("Now in ");
13888 if (sym)
13889 {
13890 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13891 uiout->text (" at ");
13892 }
13893 uiout->field_string ("file",
13894 symtab_to_filename_for_display (sal2.symtab));
13895 uiout->text (":");
13896
13897 if (uiout->is_mi_like_p ())
13898 {
13899 const char *fullname = symtab_to_fullname (sal2.symtab);
13900
13901 uiout->field_string ("fullname", fullname);
13902 }
13903
13904 uiout->field_int ("line", sal2.line);
13905 uiout->text ("\n");
13906
13907 b->loc->line_number = sal2.line;
13908 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13909
13910 b->location.reset (NULL);
13911 initialize_explicit_location (&explicit_loc);
13912 explicit_loc.source_filename
13913 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13914 explicit_loc.line_offset.offset = b->loc->line_number;
13915 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13916 b->location = new_explicit_location (&explicit_loc);
13917
13918 /* Might be nice to check if function changed, and warn if
13919 so. */
13920
13921 release_static_tracepoint_marker (tpmarker);
13922 }
13923 }
13924 return sal;
13925 }
13926
13927 /* Returns 1 iff locations A and B are sufficiently same that
13928 we don't need to report breakpoint as changed. */
13929
13930 static int
13931 locations_are_equal (struct bp_location *a, struct bp_location *b)
13932 {
13933 while (a && b)
13934 {
13935 if (a->address != b->address)
13936 return 0;
13937
13938 if (a->shlib_disabled != b->shlib_disabled)
13939 return 0;
13940
13941 if (a->enabled != b->enabled)
13942 return 0;
13943
13944 a = a->next;
13945 b = b->next;
13946 }
13947
13948 if ((a == NULL) != (b == NULL))
13949 return 0;
13950
13951 return 1;
13952 }
13953
13954 /* Split all locations of B that are bound to PSPACE out of B's
13955 location list to a separate list and return that list's head. If
13956 PSPACE is NULL, hoist out all locations of B. */
13957
13958 static struct bp_location *
13959 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13960 {
13961 struct bp_location head;
13962 struct bp_location *i = b->loc;
13963 struct bp_location **i_link = &b->loc;
13964 struct bp_location *hoisted = &head;
13965
13966 if (pspace == NULL)
13967 {
13968 i = b->loc;
13969 b->loc = NULL;
13970 return i;
13971 }
13972
13973 head.next = NULL;
13974
13975 while (i != NULL)
13976 {
13977 if (i->pspace == pspace)
13978 {
13979 *i_link = i->next;
13980 i->next = NULL;
13981 hoisted->next = i;
13982 hoisted = i;
13983 }
13984 else
13985 i_link = &i->next;
13986 i = *i_link;
13987 }
13988
13989 return head.next;
13990 }
13991
13992 /* Create new breakpoint locations for B (a hardware or software
13993 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13994 zero, then B is a ranged breakpoint. Only recreates locations for
13995 FILTER_PSPACE. Locations of other program spaces are left
13996 untouched. */
13997
13998 void
13999 update_breakpoint_locations (struct breakpoint *b,
14000 struct program_space *filter_pspace,
14001 gdb::array_view<const symtab_and_line> sals,
14002 gdb::array_view<const symtab_and_line> sals_end)
14003 {
14004 int i;
14005 struct bp_location *existing_locations;
14006
14007 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
14008 {
14009 /* Ranged breakpoints have only one start location and one end
14010 location. */
14011 b->enable_state = bp_disabled;
14012 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14013 "multiple locations found\n"),
14014 b->number);
14015 return;
14016 }
14017
14018 /* If there's no new locations, and all existing locations are
14019 pending, don't do anything. This optimizes the common case where
14020 all locations are in the same shared library, that was unloaded.
14021 We'd like to retain the location, so that when the library is
14022 loaded again, we don't loose the enabled/disabled status of the
14023 individual locations. */
14024 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
14025 return;
14026
14027 existing_locations = hoist_existing_locations (b, filter_pspace);
14028
14029 for (const auto &sal : sals)
14030 {
14031 struct bp_location *new_loc;
14032
14033 switch_to_program_space_and_thread (sal.pspace);
14034
14035 new_loc = add_location_to_breakpoint (b, &sal);
14036
14037 /* Reparse conditions, they might contain references to the
14038 old symtab. */
14039 if (b->cond_string != NULL)
14040 {
14041 const char *s;
14042
14043 s = b->cond_string;
14044 TRY
14045 {
14046 new_loc->cond = parse_exp_1 (&s, sal.pc,
14047 block_for_pc (sal.pc),
14048 0);
14049 }
14050 CATCH (e, RETURN_MASK_ERROR)
14051 {
14052 warning (_("failed to reevaluate condition "
14053 "for breakpoint %d: %s"),
14054 b->number, e.message);
14055 new_loc->enabled = 0;
14056 }
14057 END_CATCH
14058 }
14059
14060 if (!sals_end.empty ())
14061 {
14062 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
14063
14064 new_loc->length = end - sals[0].pc + 1;
14065 }
14066 }
14067
14068 /* If possible, carry over 'disable' status from existing
14069 breakpoints. */
14070 {
14071 struct bp_location *e = existing_locations;
14072 /* If there are multiple breakpoints with the same function name,
14073 e.g. for inline functions, comparing function names won't work.
14074 Instead compare pc addresses; this is just a heuristic as things
14075 may have moved, but in practice it gives the correct answer
14076 often enough until a better solution is found. */
14077 int have_ambiguous_names = ambiguous_names_p (b->loc);
14078
14079 for (; e; e = e->next)
14080 {
14081 if (!e->enabled && e->function_name)
14082 {
14083 struct bp_location *l = b->loc;
14084 if (have_ambiguous_names)
14085 {
14086 for (; l; l = l->next)
14087 if (breakpoint_locations_match (e, l))
14088 {
14089 l->enabled = 0;
14090 break;
14091 }
14092 }
14093 else
14094 {
14095 for (; l; l = l->next)
14096 if (l->function_name
14097 && strcmp (e->function_name, l->function_name) == 0)
14098 {
14099 l->enabled = 0;
14100 break;
14101 }
14102 }
14103 }
14104 }
14105 }
14106
14107 if (!locations_are_equal (existing_locations, b->loc))
14108 observer_notify_breakpoint_modified (b);
14109 }
14110
14111 /* Find the SaL locations corresponding to the given LOCATION.
14112 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14113
14114 static std::vector<symtab_and_line>
14115 location_to_sals (struct breakpoint *b, struct event_location *location,
14116 struct program_space *search_pspace, int *found)
14117 {
14118 struct gdb_exception exception = exception_none;
14119
14120 gdb_assert (b->ops != NULL);
14121
14122 std::vector<symtab_and_line> sals;
14123
14124 TRY
14125 {
14126 sals = b->ops->decode_location (b, location, search_pspace);
14127 }
14128 CATCH (e, RETURN_MASK_ERROR)
14129 {
14130 int not_found_and_ok = 0;
14131
14132 exception = e;
14133
14134 /* For pending breakpoints, it's expected that parsing will
14135 fail until the right shared library is loaded. User has
14136 already told to create pending breakpoints and don't need
14137 extra messages. If breakpoint is in bp_shlib_disabled
14138 state, then user already saw the message about that
14139 breakpoint being disabled, and don't want to see more
14140 errors. */
14141 if (e.error == NOT_FOUND_ERROR
14142 && (b->condition_not_parsed
14143 || (b->loc != NULL
14144 && search_pspace != NULL
14145 && b->loc->pspace != search_pspace)
14146 || (b->loc && b->loc->shlib_disabled)
14147 || (b->loc && b->loc->pspace->executing_startup)
14148 || b->enable_state == bp_disabled))
14149 not_found_and_ok = 1;
14150
14151 if (!not_found_and_ok)
14152 {
14153 /* We surely don't want to warn about the same breakpoint
14154 10 times. One solution, implemented here, is disable
14155 the breakpoint on error. Another solution would be to
14156 have separate 'warning emitted' flag. Since this
14157 happens only when a binary has changed, I don't know
14158 which approach is better. */
14159 b->enable_state = bp_disabled;
14160 throw_exception (e);
14161 }
14162 }
14163 END_CATCH
14164
14165 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14166 {
14167 for (auto &sal : sals)
14168 resolve_sal_pc (&sal);
14169 if (b->condition_not_parsed && b->extra_string != NULL)
14170 {
14171 char *cond_string, *extra_string;
14172 int thread, task;
14173
14174 find_condition_and_thread (b->extra_string, sals[0].pc,
14175 &cond_string, &thread, &task,
14176 &extra_string);
14177 gdb_assert (b->cond_string == NULL);
14178 if (cond_string)
14179 b->cond_string = cond_string;
14180 b->thread = thread;
14181 b->task = task;
14182 if (extra_string)
14183 {
14184 xfree (b->extra_string);
14185 b->extra_string = extra_string;
14186 }
14187 b->condition_not_parsed = 0;
14188 }
14189
14190 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14191 sals[0] = update_static_tracepoint (b, sals[0]);
14192
14193 *found = 1;
14194 }
14195 else
14196 *found = 0;
14197
14198 return sals;
14199 }
14200
14201 /* The default re_set method, for typical hardware or software
14202 breakpoints. Reevaluate the breakpoint and recreate its
14203 locations. */
14204
14205 static void
14206 breakpoint_re_set_default (struct breakpoint *b)
14207 {
14208 struct program_space *filter_pspace = current_program_space;
14209 std::vector<symtab_and_line> expanded, expanded_end;
14210
14211 int found;
14212 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14213 filter_pspace, &found);
14214 if (found)
14215 expanded = std::move (sals);
14216
14217 if (b->location_range_end != NULL)
14218 {
14219 std::vector<symtab_and_line> sals_end
14220 = location_to_sals (b, b->location_range_end.get (),
14221 filter_pspace, &found);
14222 if (found)
14223 expanded_end = std::move (sals_end);
14224 }
14225
14226 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14227 }
14228
14229 /* Default method for creating SALs from an address string. It basically
14230 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14231
14232 static void
14233 create_sals_from_location_default (const struct event_location *location,
14234 struct linespec_result *canonical,
14235 enum bptype type_wanted)
14236 {
14237 parse_breakpoint_sals (location, canonical);
14238 }
14239
14240 /* Call create_breakpoints_sal for the given arguments. This is the default
14241 function for the `create_breakpoints_sal' method of
14242 breakpoint_ops. */
14243
14244 static void
14245 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14246 struct linespec_result *canonical,
14247 gdb::unique_xmalloc_ptr<char> cond_string,
14248 gdb::unique_xmalloc_ptr<char> extra_string,
14249 enum bptype type_wanted,
14250 enum bpdisp disposition,
14251 int thread,
14252 int task, int ignore_count,
14253 const struct breakpoint_ops *ops,
14254 int from_tty, int enabled,
14255 int internal, unsigned flags)
14256 {
14257 create_breakpoints_sal (gdbarch, canonical,
14258 std::move (cond_string),
14259 std::move (extra_string),
14260 type_wanted, disposition,
14261 thread, task, ignore_count, ops, from_tty,
14262 enabled, internal, flags);
14263 }
14264
14265 /* Decode the line represented by S by calling decode_line_full. This is the
14266 default function for the `decode_location' method of breakpoint_ops. */
14267
14268 static std::vector<symtab_and_line>
14269 decode_location_default (struct breakpoint *b,
14270 const struct event_location *location,
14271 struct program_space *search_pspace)
14272 {
14273 struct linespec_result canonical;
14274
14275 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14276 (struct symtab *) NULL, 0,
14277 &canonical, multiple_symbols_all,
14278 b->filter);
14279
14280 /* We should get 0 or 1 resulting SALs. */
14281 gdb_assert (canonical.lsals.size () < 2);
14282
14283 if (!canonical.lsals.empty ())
14284 {
14285 const linespec_sals &lsal = canonical.lsals[0];
14286 return std::move (lsal.sals);
14287 }
14288 return {};
14289 }
14290
14291 /* Prepare the global context for a re-set of breakpoint B. */
14292
14293 static struct cleanup *
14294 prepare_re_set_context (struct breakpoint *b)
14295 {
14296 input_radix = b->input_radix;
14297 set_language (b->language);
14298
14299 return make_cleanup (null_cleanup, NULL);
14300 }
14301
14302 /* Reset a breakpoint given it's struct breakpoint * BINT.
14303 The value we return ends up being the return value from catch_errors.
14304 Unused in this case. */
14305
14306 static int
14307 breakpoint_re_set_one (void *bint)
14308 {
14309 /* Get past catch_errs. */
14310 struct breakpoint *b = (struct breakpoint *) bint;
14311 struct cleanup *cleanups;
14312
14313 cleanups = prepare_re_set_context (b);
14314 b->ops->re_set (b);
14315 do_cleanups (cleanups);
14316 return 0;
14317 }
14318
14319 /* Re-set breakpoint locations for the current program space.
14320 Locations bound to other program spaces are left untouched. */
14321
14322 void
14323 breakpoint_re_set (void)
14324 {
14325 struct breakpoint *b, *b_tmp;
14326 enum language save_language;
14327 int save_input_radix;
14328
14329 save_language = current_language->la_language;
14330 save_input_radix = input_radix;
14331
14332 {
14333 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14334
14335 /* Note: we must not try to insert locations until after all
14336 breakpoints have been re-set. Otherwise, e.g., when re-setting
14337 breakpoint 1, we'd insert the locations of breakpoint 2, which
14338 hadn't been re-set yet, and thus may have stale locations. */
14339
14340 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14341 {
14342 /* Format possible error msg. */
14343 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14344 b->number);
14345 struct cleanup *cleanups = make_cleanup (xfree, message);
14346 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14347 do_cleanups (cleanups);
14348 }
14349 set_language (save_language);
14350 input_radix = save_input_radix;
14351
14352 jit_breakpoint_re_set ();
14353 }
14354
14355 create_overlay_event_breakpoint ();
14356 create_longjmp_master_breakpoint ();
14357 create_std_terminate_master_breakpoint ();
14358 create_exception_master_breakpoint ();
14359
14360 /* Now we can insert. */
14361 update_global_location_list (UGLL_MAY_INSERT);
14362 }
14363 \f
14364 /* Reset the thread number of this breakpoint:
14365
14366 - If the breakpoint is for all threads, leave it as-is.
14367 - Else, reset it to the current thread for inferior_ptid. */
14368 void
14369 breakpoint_re_set_thread (struct breakpoint *b)
14370 {
14371 if (b->thread != -1)
14372 {
14373 if (in_thread_list (inferior_ptid))
14374 b->thread = ptid_to_global_thread_id (inferior_ptid);
14375
14376 /* We're being called after following a fork. The new fork is
14377 selected as current, and unless this was a vfork will have a
14378 different program space from the original thread. Reset that
14379 as well. */
14380 b->loc->pspace = current_program_space;
14381 }
14382 }
14383
14384 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14385 If from_tty is nonzero, it prints a message to that effect,
14386 which ends with a period (no newline). */
14387
14388 void
14389 set_ignore_count (int bptnum, int count, int from_tty)
14390 {
14391 struct breakpoint *b;
14392
14393 if (count < 0)
14394 count = 0;
14395
14396 ALL_BREAKPOINTS (b)
14397 if (b->number == bptnum)
14398 {
14399 if (is_tracepoint (b))
14400 {
14401 if (from_tty && count != 0)
14402 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14403 bptnum);
14404 return;
14405 }
14406
14407 b->ignore_count = count;
14408 if (from_tty)
14409 {
14410 if (count == 0)
14411 printf_filtered (_("Will stop next time "
14412 "breakpoint %d is reached."),
14413 bptnum);
14414 else if (count == 1)
14415 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14416 bptnum);
14417 else
14418 printf_filtered (_("Will ignore next %d "
14419 "crossings of breakpoint %d."),
14420 count, bptnum);
14421 }
14422 observer_notify_breakpoint_modified (b);
14423 return;
14424 }
14425
14426 error (_("No breakpoint number %d."), bptnum);
14427 }
14428
14429 /* Command to set ignore-count of breakpoint N to COUNT. */
14430
14431 static void
14432 ignore_command (char *args, int from_tty)
14433 {
14434 char *p = args;
14435 int num;
14436
14437 if (p == 0)
14438 error_no_arg (_("a breakpoint number"));
14439
14440 num = get_number (&p);
14441 if (num == 0)
14442 error (_("bad breakpoint number: '%s'"), args);
14443 if (*p == 0)
14444 error (_("Second argument (specified ignore-count) is missing."));
14445
14446 set_ignore_count (num,
14447 longest_to_int (value_as_long (parse_and_eval (p))),
14448 from_tty);
14449 if (from_tty)
14450 printf_filtered ("\n");
14451 }
14452 \f
14453 /* Call FUNCTION on each of the breakpoints
14454 whose numbers are given in ARGS. */
14455
14456 static void
14457 map_breakpoint_numbers (const char *args,
14458 void (*function) (struct breakpoint *,
14459 void *),
14460 void *data)
14461 {
14462 int num;
14463 struct breakpoint *b, *tmp;
14464
14465 if (args == 0 || *args == '\0')
14466 error_no_arg (_("one or more breakpoint numbers"));
14467
14468 number_or_range_parser parser (args);
14469
14470 while (!parser.finished ())
14471 {
14472 const char *p = parser.cur_tok ();
14473 bool match = false;
14474
14475 num = parser.get_number ();
14476 if (num == 0)
14477 {
14478 warning (_("bad breakpoint number at or near '%s'"), p);
14479 }
14480 else
14481 {
14482 ALL_BREAKPOINTS_SAFE (b, tmp)
14483 if (b->number == num)
14484 {
14485 match = true;
14486 function (b, data);
14487 break;
14488 }
14489 if (!match)
14490 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14491 }
14492 }
14493 }
14494
14495 static struct bp_location *
14496 find_location_by_number (char *number)
14497 {
14498 char *dot = strchr (number, '.');
14499 char *p1;
14500 int bp_num;
14501 int loc_num;
14502 struct breakpoint *b;
14503 struct bp_location *loc;
14504
14505 *dot = '\0';
14506
14507 p1 = number;
14508 bp_num = get_number (&p1);
14509 if (bp_num == 0)
14510 error (_("Bad breakpoint number '%s'"), number);
14511
14512 ALL_BREAKPOINTS (b)
14513 if (b->number == bp_num)
14514 {
14515 break;
14516 }
14517
14518 if (!b || b->number != bp_num)
14519 error (_("Bad breakpoint number '%s'"), number);
14520
14521 p1 = dot+1;
14522 loc_num = get_number (&p1);
14523 if (loc_num == 0)
14524 error (_("Bad breakpoint location number '%s'"), number);
14525
14526 --loc_num;
14527 loc = b->loc;
14528 for (;loc_num && loc; --loc_num, loc = loc->next)
14529 ;
14530 if (!loc)
14531 error (_("Bad breakpoint location number '%s'"), dot+1);
14532
14533 return loc;
14534 }
14535
14536
14537 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14538 If from_tty is nonzero, it prints a message to that effect,
14539 which ends with a period (no newline). */
14540
14541 void
14542 disable_breakpoint (struct breakpoint *bpt)
14543 {
14544 /* Never disable a watchpoint scope breakpoint; we want to
14545 hit them when we leave scope so we can delete both the
14546 watchpoint and its scope breakpoint at that time. */
14547 if (bpt->type == bp_watchpoint_scope)
14548 return;
14549
14550 bpt->enable_state = bp_disabled;
14551
14552 /* Mark breakpoint locations modified. */
14553 mark_breakpoint_modified (bpt);
14554
14555 if (target_supports_enable_disable_tracepoint ()
14556 && current_trace_status ()->running && is_tracepoint (bpt))
14557 {
14558 struct bp_location *location;
14559
14560 for (location = bpt->loc; location; location = location->next)
14561 target_disable_tracepoint (location);
14562 }
14563
14564 update_global_location_list (UGLL_DONT_INSERT);
14565
14566 observer_notify_breakpoint_modified (bpt);
14567 }
14568
14569 /* A callback for iterate_over_related_breakpoints. */
14570
14571 static void
14572 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14573 {
14574 disable_breakpoint (b);
14575 }
14576
14577 /* A callback for map_breakpoint_numbers that calls
14578 disable_breakpoint. */
14579
14580 static void
14581 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14582 {
14583 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14584 }
14585
14586 static void
14587 disable_command (char *args, int from_tty)
14588 {
14589 if (args == 0)
14590 {
14591 struct breakpoint *bpt;
14592
14593 ALL_BREAKPOINTS (bpt)
14594 if (user_breakpoint_p (bpt))
14595 disable_breakpoint (bpt);
14596 }
14597 else
14598 {
14599 char *num = extract_arg (&args);
14600
14601 while (num)
14602 {
14603 if (strchr (num, '.'))
14604 {
14605 struct bp_location *loc = find_location_by_number (num);
14606
14607 if (loc)
14608 {
14609 if (loc->enabled)
14610 {
14611 loc->enabled = 0;
14612 mark_breakpoint_location_modified (loc);
14613 }
14614 if (target_supports_enable_disable_tracepoint ()
14615 && current_trace_status ()->running && loc->owner
14616 && is_tracepoint (loc->owner))
14617 target_disable_tracepoint (loc);
14618 }
14619 update_global_location_list (UGLL_DONT_INSERT);
14620 }
14621 else
14622 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14623 num = extract_arg (&args);
14624 }
14625 }
14626 }
14627
14628 static void
14629 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14630 int count)
14631 {
14632 int target_resources_ok;
14633
14634 if (bpt->type == bp_hardware_breakpoint)
14635 {
14636 int i;
14637 i = hw_breakpoint_used_count ();
14638 target_resources_ok =
14639 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14640 i + 1, 0);
14641 if (target_resources_ok == 0)
14642 error (_("No hardware breakpoint support in the target."));
14643 else if (target_resources_ok < 0)
14644 error (_("Hardware breakpoints used exceeds limit."));
14645 }
14646
14647 if (is_watchpoint (bpt))
14648 {
14649 /* Initialize it just to avoid a GCC false warning. */
14650 enum enable_state orig_enable_state = bp_disabled;
14651
14652 TRY
14653 {
14654 struct watchpoint *w = (struct watchpoint *) bpt;
14655
14656 orig_enable_state = bpt->enable_state;
14657 bpt->enable_state = bp_enabled;
14658 update_watchpoint (w, 1 /* reparse */);
14659 }
14660 CATCH (e, RETURN_MASK_ALL)
14661 {
14662 bpt->enable_state = orig_enable_state;
14663 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14664 bpt->number);
14665 return;
14666 }
14667 END_CATCH
14668 }
14669
14670 bpt->enable_state = bp_enabled;
14671
14672 /* Mark breakpoint locations modified. */
14673 mark_breakpoint_modified (bpt);
14674
14675 if (target_supports_enable_disable_tracepoint ()
14676 && current_trace_status ()->running && is_tracepoint (bpt))
14677 {
14678 struct bp_location *location;
14679
14680 for (location = bpt->loc; location; location = location->next)
14681 target_enable_tracepoint (location);
14682 }
14683
14684 bpt->disposition = disposition;
14685 bpt->enable_count = count;
14686 update_global_location_list (UGLL_MAY_INSERT);
14687
14688 observer_notify_breakpoint_modified (bpt);
14689 }
14690
14691
14692 void
14693 enable_breakpoint (struct breakpoint *bpt)
14694 {
14695 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14696 }
14697
14698 static void
14699 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14700 {
14701 enable_breakpoint (bpt);
14702 }
14703
14704 /* A callback for map_breakpoint_numbers that calls
14705 enable_breakpoint. */
14706
14707 static void
14708 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14709 {
14710 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14711 }
14712
14713 /* The enable command enables the specified breakpoints (or all defined
14714 breakpoints) so they once again become (or continue to be) effective
14715 in stopping the inferior. */
14716
14717 static void
14718 enable_command (char *args, int from_tty)
14719 {
14720 if (args == 0)
14721 {
14722 struct breakpoint *bpt;
14723
14724 ALL_BREAKPOINTS (bpt)
14725 if (user_breakpoint_p (bpt))
14726 enable_breakpoint (bpt);
14727 }
14728 else
14729 {
14730 char *num = extract_arg (&args);
14731
14732 while (num)
14733 {
14734 if (strchr (num, '.'))
14735 {
14736 struct bp_location *loc = find_location_by_number (num);
14737
14738 if (loc)
14739 {
14740 if (!loc->enabled)
14741 {
14742 loc->enabled = 1;
14743 mark_breakpoint_location_modified (loc);
14744 }
14745 if (target_supports_enable_disable_tracepoint ()
14746 && current_trace_status ()->running && loc->owner
14747 && is_tracepoint (loc->owner))
14748 target_enable_tracepoint (loc);
14749 }
14750 update_global_location_list (UGLL_MAY_INSERT);
14751 }
14752 else
14753 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14754 num = extract_arg (&args);
14755 }
14756 }
14757 }
14758
14759 /* This struct packages up disposition data for application to multiple
14760 breakpoints. */
14761
14762 struct disp_data
14763 {
14764 enum bpdisp disp;
14765 int count;
14766 };
14767
14768 static void
14769 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14770 {
14771 struct disp_data disp_data = *(struct disp_data *) arg;
14772
14773 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14774 }
14775
14776 static void
14777 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14778 {
14779 struct disp_data disp = { disp_disable, 1 };
14780
14781 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14782 }
14783
14784 static void
14785 enable_once_command (char *args, int from_tty)
14786 {
14787 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14788 }
14789
14790 static void
14791 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14792 {
14793 struct disp_data disp = { disp_disable, *(int *) countptr };
14794
14795 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14796 }
14797
14798 static void
14799 enable_count_command (char *args, int from_tty)
14800 {
14801 int count;
14802
14803 if (args == NULL)
14804 error_no_arg (_("hit count"));
14805
14806 count = get_number (&args);
14807
14808 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14809 }
14810
14811 static void
14812 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14813 {
14814 struct disp_data disp = { disp_del, 1 };
14815
14816 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14817 }
14818
14819 static void
14820 enable_delete_command (char *args, int from_tty)
14821 {
14822 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14823 }
14824 \f
14825 static void
14826 set_breakpoint_cmd (char *args, int from_tty)
14827 {
14828 }
14829
14830 static void
14831 show_breakpoint_cmd (char *args, int from_tty)
14832 {
14833 }
14834
14835 /* Invalidate last known value of any hardware watchpoint if
14836 the memory which that value represents has been written to by
14837 GDB itself. */
14838
14839 static void
14840 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14841 CORE_ADDR addr, ssize_t len,
14842 const bfd_byte *data)
14843 {
14844 struct breakpoint *bp;
14845
14846 ALL_BREAKPOINTS (bp)
14847 if (bp->enable_state == bp_enabled
14848 && bp->type == bp_hardware_watchpoint)
14849 {
14850 struct watchpoint *wp = (struct watchpoint *) bp;
14851
14852 if (wp->val_valid && wp->val)
14853 {
14854 struct bp_location *loc;
14855
14856 for (loc = bp->loc; loc != NULL; loc = loc->next)
14857 if (loc->loc_type == bp_loc_hardware_watchpoint
14858 && loc->address + loc->length > addr
14859 && addr + len > loc->address)
14860 {
14861 value_free (wp->val);
14862 wp->val = NULL;
14863 wp->val_valid = 0;
14864 }
14865 }
14866 }
14867 }
14868
14869 /* Create and insert a breakpoint for software single step. */
14870
14871 void
14872 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14873 struct address_space *aspace,
14874 CORE_ADDR next_pc)
14875 {
14876 struct thread_info *tp = inferior_thread ();
14877 struct symtab_and_line sal;
14878 CORE_ADDR pc = next_pc;
14879
14880 if (tp->control.single_step_breakpoints == NULL)
14881 {
14882 tp->control.single_step_breakpoints
14883 = new_single_step_breakpoint (tp->global_num, gdbarch);
14884 }
14885
14886 sal = find_pc_line (pc, 0);
14887 sal.pc = pc;
14888 sal.section = find_pc_overlay (pc);
14889 sal.explicit_pc = 1;
14890 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14891
14892 update_global_location_list (UGLL_INSERT);
14893 }
14894
14895 /* Insert single step breakpoints according to the current state. */
14896
14897 int
14898 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14899 {
14900 struct regcache *regcache = get_current_regcache ();
14901 std::vector<CORE_ADDR> next_pcs;
14902
14903 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14904
14905 if (!next_pcs.empty ())
14906 {
14907 struct frame_info *frame = get_current_frame ();
14908 struct address_space *aspace = get_frame_address_space (frame);
14909
14910 for (CORE_ADDR pc : next_pcs)
14911 insert_single_step_breakpoint (gdbarch, aspace, pc);
14912
14913 return 1;
14914 }
14915 else
14916 return 0;
14917 }
14918
14919 /* See breakpoint.h. */
14920
14921 int
14922 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14923 struct address_space *aspace,
14924 CORE_ADDR pc)
14925 {
14926 struct bp_location *loc;
14927
14928 for (loc = bp->loc; loc != NULL; loc = loc->next)
14929 if (loc->inserted
14930 && breakpoint_location_address_match (loc, aspace, pc))
14931 return 1;
14932
14933 return 0;
14934 }
14935
14936 /* Check whether a software single-step breakpoint is inserted at
14937 PC. */
14938
14939 int
14940 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14941 CORE_ADDR pc)
14942 {
14943 struct breakpoint *bpt;
14944
14945 ALL_BREAKPOINTS (bpt)
14946 {
14947 if (bpt->type == bp_single_step
14948 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14949 return 1;
14950 }
14951 return 0;
14952 }
14953
14954 /* Tracepoint-specific operations. */
14955
14956 /* Set tracepoint count to NUM. */
14957 static void
14958 set_tracepoint_count (int num)
14959 {
14960 tracepoint_count = num;
14961 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14962 }
14963
14964 static void
14965 trace_command (char *arg, int from_tty)
14966 {
14967 struct breakpoint_ops *ops;
14968
14969 event_location_up location = string_to_event_location (&arg,
14970 current_language);
14971 if (location != NULL
14972 && event_location_type (location.get ()) == PROBE_LOCATION)
14973 ops = &tracepoint_probe_breakpoint_ops;
14974 else
14975 ops = &tracepoint_breakpoint_ops;
14976
14977 create_breakpoint (get_current_arch (),
14978 location.get (),
14979 NULL, 0, arg, 1 /* parse arg */,
14980 0 /* tempflag */,
14981 bp_tracepoint /* type_wanted */,
14982 0 /* Ignore count */,
14983 pending_break_support,
14984 ops,
14985 from_tty,
14986 1 /* enabled */,
14987 0 /* internal */, 0);
14988 }
14989
14990 static void
14991 ftrace_command (char *arg, int from_tty)
14992 {
14993 event_location_up location = string_to_event_location (&arg,
14994 current_language);
14995 create_breakpoint (get_current_arch (),
14996 location.get (),
14997 NULL, 0, arg, 1 /* parse arg */,
14998 0 /* tempflag */,
14999 bp_fast_tracepoint /* type_wanted */,
15000 0 /* Ignore count */,
15001 pending_break_support,
15002 &tracepoint_breakpoint_ops,
15003 from_tty,
15004 1 /* enabled */,
15005 0 /* internal */, 0);
15006 }
15007
15008 /* strace command implementation. Creates a static tracepoint. */
15009
15010 static void
15011 strace_command (char *arg, int from_tty)
15012 {
15013 struct breakpoint_ops *ops;
15014 event_location_up location;
15015 struct cleanup *back_to;
15016
15017 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15018 or with a normal static tracepoint. */
15019 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15020 {
15021 ops = &strace_marker_breakpoint_ops;
15022 location = new_linespec_location (&arg);
15023 }
15024 else
15025 {
15026 ops = &tracepoint_breakpoint_ops;
15027 location = string_to_event_location (&arg, current_language);
15028 }
15029
15030 create_breakpoint (get_current_arch (),
15031 location.get (),
15032 NULL, 0, arg, 1 /* parse arg */,
15033 0 /* tempflag */,
15034 bp_static_tracepoint /* type_wanted */,
15035 0 /* Ignore count */,
15036 pending_break_support,
15037 ops,
15038 from_tty,
15039 1 /* enabled */,
15040 0 /* internal */, 0);
15041 }
15042
15043 /* Set up a fake reader function that gets command lines from a linked
15044 list that was acquired during tracepoint uploading. */
15045
15046 static struct uploaded_tp *this_utp;
15047 static int next_cmd;
15048
15049 static char *
15050 read_uploaded_action (void)
15051 {
15052 char *rslt;
15053
15054 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15055
15056 next_cmd++;
15057
15058 return rslt;
15059 }
15060
15061 /* Given information about a tracepoint as recorded on a target (which
15062 can be either a live system or a trace file), attempt to create an
15063 equivalent GDB tracepoint. This is not a reliable process, since
15064 the target does not necessarily have all the information used when
15065 the tracepoint was originally defined. */
15066
15067 struct tracepoint *
15068 create_tracepoint_from_upload (struct uploaded_tp *utp)
15069 {
15070 char *addr_str, small_buf[100];
15071 struct tracepoint *tp;
15072
15073 if (utp->at_string)
15074 addr_str = utp->at_string;
15075 else
15076 {
15077 /* In the absence of a source location, fall back to raw
15078 address. Since there is no way to confirm that the address
15079 means the same thing as when the trace was started, warn the
15080 user. */
15081 warning (_("Uploaded tracepoint %d has no "
15082 "source location, using raw address"),
15083 utp->number);
15084 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15085 addr_str = small_buf;
15086 }
15087
15088 /* There's not much we can do with a sequence of bytecodes. */
15089 if (utp->cond && !utp->cond_string)
15090 warning (_("Uploaded tracepoint %d condition "
15091 "has no source form, ignoring it"),
15092 utp->number);
15093
15094 event_location_up location = string_to_event_location (&addr_str,
15095 current_language);
15096 if (!create_breakpoint (get_current_arch (),
15097 location.get (),
15098 utp->cond_string, -1, addr_str,
15099 0 /* parse cond/thread */,
15100 0 /* tempflag */,
15101 utp->type /* type_wanted */,
15102 0 /* Ignore count */,
15103 pending_break_support,
15104 &tracepoint_breakpoint_ops,
15105 0 /* from_tty */,
15106 utp->enabled /* enabled */,
15107 0 /* internal */,
15108 CREATE_BREAKPOINT_FLAGS_INSERTED))
15109 return NULL;
15110
15111 /* Get the tracepoint we just created. */
15112 tp = get_tracepoint (tracepoint_count);
15113 gdb_assert (tp != NULL);
15114
15115 if (utp->pass > 0)
15116 {
15117 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15118 tp->number);
15119
15120 trace_pass_command (small_buf, 0);
15121 }
15122
15123 /* If we have uploaded versions of the original commands, set up a
15124 special-purpose "reader" function and call the usual command line
15125 reader, then pass the result to the breakpoint command-setting
15126 function. */
15127 if (!VEC_empty (char_ptr, utp->cmd_strings))
15128 {
15129 command_line_up cmd_list;
15130
15131 this_utp = utp;
15132 next_cmd = 0;
15133
15134 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15135
15136 breakpoint_set_commands (tp, std::move (cmd_list));
15137 }
15138 else if (!VEC_empty (char_ptr, utp->actions)
15139 || !VEC_empty (char_ptr, utp->step_actions))
15140 warning (_("Uploaded tracepoint %d actions "
15141 "have no source form, ignoring them"),
15142 utp->number);
15143
15144 /* Copy any status information that might be available. */
15145 tp->hit_count = utp->hit_count;
15146 tp->traceframe_usage = utp->traceframe_usage;
15147
15148 return tp;
15149 }
15150
15151 /* Print information on tracepoint number TPNUM_EXP, or all if
15152 omitted. */
15153
15154 static void
15155 info_tracepoints_command (char *args, int from_tty)
15156 {
15157 struct ui_out *uiout = current_uiout;
15158 int num_printed;
15159
15160 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15161
15162 if (num_printed == 0)
15163 {
15164 if (args == NULL || *args == '\0')
15165 uiout->message ("No tracepoints.\n");
15166 else
15167 uiout->message ("No tracepoint matching '%s'.\n", args);
15168 }
15169
15170 default_collect_info ();
15171 }
15172
15173 /* The 'enable trace' command enables tracepoints.
15174 Not supported by all targets. */
15175 static void
15176 enable_trace_command (char *args, int from_tty)
15177 {
15178 enable_command (args, from_tty);
15179 }
15180
15181 /* The 'disable trace' command disables tracepoints.
15182 Not supported by all targets. */
15183 static void
15184 disable_trace_command (char *args, int from_tty)
15185 {
15186 disable_command (args, from_tty);
15187 }
15188
15189 /* Remove a tracepoint (or all if no argument). */
15190 static void
15191 delete_trace_command (char *arg, int from_tty)
15192 {
15193 struct breakpoint *b, *b_tmp;
15194
15195 dont_repeat ();
15196
15197 if (arg == 0)
15198 {
15199 int breaks_to_delete = 0;
15200
15201 /* Delete all breakpoints if no argument.
15202 Do not delete internal or call-dummy breakpoints, these
15203 have to be deleted with an explicit breakpoint number
15204 argument. */
15205 ALL_TRACEPOINTS (b)
15206 if (is_tracepoint (b) && user_breakpoint_p (b))
15207 {
15208 breaks_to_delete = 1;
15209 break;
15210 }
15211
15212 /* Ask user only if there are some breakpoints to delete. */
15213 if (!from_tty
15214 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15215 {
15216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15217 if (is_tracepoint (b) && user_breakpoint_p (b))
15218 delete_breakpoint (b);
15219 }
15220 }
15221 else
15222 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15223 }
15224
15225 /* Helper function for trace_pass_command. */
15226
15227 static void
15228 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15229 {
15230 tp->pass_count = count;
15231 observer_notify_breakpoint_modified (tp);
15232 if (from_tty)
15233 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15234 tp->number, count);
15235 }
15236
15237 /* Set passcount for tracepoint.
15238
15239 First command argument is passcount, second is tracepoint number.
15240 If tracepoint number omitted, apply to most recently defined.
15241 Also accepts special argument "all". */
15242
15243 static void
15244 trace_pass_command (char *args, int from_tty)
15245 {
15246 struct tracepoint *t1;
15247 unsigned int count;
15248
15249 if (args == 0 || *args == 0)
15250 error (_("passcount command requires an "
15251 "argument (count + optional TP num)"));
15252
15253 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15254
15255 args = skip_spaces (args);
15256 if (*args && strncasecmp (args, "all", 3) == 0)
15257 {
15258 struct breakpoint *b;
15259
15260 args += 3; /* Skip special argument "all". */
15261 if (*args)
15262 error (_("Junk at end of arguments."));
15263
15264 ALL_TRACEPOINTS (b)
15265 {
15266 t1 = (struct tracepoint *) b;
15267 trace_pass_set_count (t1, count, from_tty);
15268 }
15269 }
15270 else if (*args == '\0')
15271 {
15272 t1 = get_tracepoint_by_number (&args, NULL);
15273 if (t1)
15274 trace_pass_set_count (t1, count, from_tty);
15275 }
15276 else
15277 {
15278 number_or_range_parser parser (args);
15279 while (!parser.finished ())
15280 {
15281 t1 = get_tracepoint_by_number (&args, &parser);
15282 if (t1)
15283 trace_pass_set_count (t1, count, from_tty);
15284 }
15285 }
15286 }
15287
15288 struct tracepoint *
15289 get_tracepoint (int num)
15290 {
15291 struct breakpoint *t;
15292
15293 ALL_TRACEPOINTS (t)
15294 if (t->number == num)
15295 return (struct tracepoint *) t;
15296
15297 return NULL;
15298 }
15299
15300 /* Find the tracepoint with the given target-side number (which may be
15301 different from the tracepoint number after disconnecting and
15302 reconnecting). */
15303
15304 struct tracepoint *
15305 get_tracepoint_by_number_on_target (int num)
15306 {
15307 struct breakpoint *b;
15308
15309 ALL_TRACEPOINTS (b)
15310 {
15311 struct tracepoint *t = (struct tracepoint *) b;
15312
15313 if (t->number_on_target == num)
15314 return t;
15315 }
15316
15317 return NULL;
15318 }
15319
15320 /* Utility: parse a tracepoint number and look it up in the list.
15321 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15322 If the argument is missing, the most recent tracepoint
15323 (tracepoint_count) is returned. */
15324
15325 struct tracepoint *
15326 get_tracepoint_by_number (char **arg,
15327 number_or_range_parser *parser)
15328 {
15329 struct breakpoint *t;
15330 int tpnum;
15331 char *instring = arg == NULL ? NULL : *arg;
15332
15333 if (parser != NULL)
15334 {
15335 gdb_assert (!parser->finished ());
15336 tpnum = parser->get_number ();
15337 }
15338 else if (arg == NULL || *arg == NULL || ! **arg)
15339 tpnum = tracepoint_count;
15340 else
15341 tpnum = get_number (arg);
15342
15343 if (tpnum <= 0)
15344 {
15345 if (instring && *instring)
15346 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15347 instring);
15348 else
15349 printf_filtered (_("No previous tracepoint\n"));
15350 return NULL;
15351 }
15352
15353 ALL_TRACEPOINTS (t)
15354 if (t->number == tpnum)
15355 {
15356 return (struct tracepoint *) t;
15357 }
15358
15359 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15360 return NULL;
15361 }
15362
15363 void
15364 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15365 {
15366 if (b->thread != -1)
15367 fprintf_unfiltered (fp, " thread %d", b->thread);
15368
15369 if (b->task != 0)
15370 fprintf_unfiltered (fp, " task %d", b->task);
15371
15372 fprintf_unfiltered (fp, "\n");
15373 }
15374
15375 /* Save information on user settable breakpoints (watchpoints, etc) to
15376 a new script file named FILENAME. If FILTER is non-NULL, call it
15377 on each breakpoint and only include the ones for which it returns
15378 non-zero. */
15379
15380 static void
15381 save_breakpoints (char *filename, int from_tty,
15382 int (*filter) (const struct breakpoint *))
15383 {
15384 struct breakpoint *tp;
15385 int any = 0;
15386 int extra_trace_bits = 0;
15387
15388 if (filename == 0 || *filename == 0)
15389 error (_("Argument required (file name in which to save)"));
15390
15391 /* See if we have anything to save. */
15392 ALL_BREAKPOINTS (tp)
15393 {
15394 /* Skip internal and momentary breakpoints. */
15395 if (!user_breakpoint_p (tp))
15396 continue;
15397
15398 /* If we have a filter, only save the breakpoints it accepts. */
15399 if (filter && !filter (tp))
15400 continue;
15401
15402 any = 1;
15403
15404 if (is_tracepoint (tp))
15405 {
15406 extra_trace_bits = 1;
15407
15408 /* We can stop searching. */
15409 break;
15410 }
15411 }
15412
15413 if (!any)
15414 {
15415 warning (_("Nothing to save."));
15416 return;
15417 }
15418
15419 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15420
15421 stdio_file fp;
15422
15423 if (!fp.open (expanded_filename.get (), "w"))
15424 error (_("Unable to open file '%s' for saving (%s)"),
15425 expanded_filename.get (), safe_strerror (errno));
15426
15427 if (extra_trace_bits)
15428 save_trace_state_variables (&fp);
15429
15430 ALL_BREAKPOINTS (tp)
15431 {
15432 /* Skip internal and momentary breakpoints. */
15433 if (!user_breakpoint_p (tp))
15434 continue;
15435
15436 /* If we have a filter, only save the breakpoints it accepts. */
15437 if (filter && !filter (tp))
15438 continue;
15439
15440 tp->ops->print_recreate (tp, &fp);
15441
15442 /* Note, we can't rely on tp->number for anything, as we can't
15443 assume the recreated breakpoint numbers will match. Use $bpnum
15444 instead. */
15445
15446 if (tp->cond_string)
15447 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15448
15449 if (tp->ignore_count)
15450 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15451
15452 if (tp->type != bp_dprintf && tp->commands)
15453 {
15454 fp.puts (" commands\n");
15455
15456 current_uiout->redirect (&fp);
15457 TRY
15458 {
15459 print_command_lines (current_uiout, tp->commands->commands, 2);
15460 }
15461 CATCH (ex, RETURN_MASK_ALL)
15462 {
15463 current_uiout->redirect (NULL);
15464 throw_exception (ex);
15465 }
15466 END_CATCH
15467
15468 current_uiout->redirect (NULL);
15469 fp.puts (" end\n");
15470 }
15471
15472 if (tp->enable_state == bp_disabled)
15473 fp.puts ("disable $bpnum\n");
15474
15475 /* If this is a multi-location breakpoint, check if the locations
15476 should be individually disabled. Watchpoint locations are
15477 special, and not user visible. */
15478 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15479 {
15480 struct bp_location *loc;
15481 int n = 1;
15482
15483 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15484 if (!loc->enabled)
15485 fp.printf ("disable $bpnum.%d\n", n);
15486 }
15487 }
15488
15489 if (extra_trace_bits && *default_collect)
15490 fp.printf ("set default-collect %s\n", default_collect);
15491
15492 if (from_tty)
15493 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15494 }
15495
15496 /* The `save breakpoints' command. */
15497
15498 static void
15499 save_breakpoints_command (char *args, int from_tty)
15500 {
15501 save_breakpoints (args, from_tty, NULL);
15502 }
15503
15504 /* The `save tracepoints' command. */
15505
15506 static void
15507 save_tracepoints_command (char *args, int from_tty)
15508 {
15509 save_breakpoints (args, from_tty, is_tracepoint);
15510 }
15511
15512 /* Create a vector of all tracepoints. */
15513
15514 VEC(breakpoint_p) *
15515 all_tracepoints (void)
15516 {
15517 VEC(breakpoint_p) *tp_vec = 0;
15518 struct breakpoint *tp;
15519
15520 ALL_TRACEPOINTS (tp)
15521 {
15522 VEC_safe_push (breakpoint_p, tp_vec, tp);
15523 }
15524
15525 return tp_vec;
15526 }
15527
15528 \f
15529 /* This help string is used to consolidate all the help string for specifying
15530 locations used by several commands. */
15531
15532 #define LOCATION_HELP_STRING \
15533 "Linespecs are colon-separated lists of location parameters, such as\n\
15534 source filename, function name, label name, and line number.\n\
15535 Example: To specify the start of a label named \"the_top\" in the\n\
15536 function \"fact\" in the file \"factorial.c\", use\n\
15537 \"factorial.c:fact:the_top\".\n\
15538 \n\
15539 Address locations begin with \"*\" and specify an exact address in the\n\
15540 program. Example: To specify the fourth byte past the start function\n\
15541 \"main\", use \"*main + 4\".\n\
15542 \n\
15543 Explicit locations are similar to linespecs but use an option/argument\n\
15544 syntax to specify location parameters.\n\
15545 Example: To specify the start of the label named \"the_top\" in the\n\
15546 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15547 -function fact -label the_top\".\n"
15548
15549 /* This help string is used for the break, hbreak, tbreak and thbreak
15550 commands. It is defined as a macro to prevent duplication.
15551 COMMAND should be a string constant containing the name of the
15552 command. */
15553
15554 #define BREAK_ARGS_HELP(command) \
15555 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15556 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15557 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15558 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15559 `-probe-dtrace' (for a DTrace probe).\n\
15560 LOCATION may be a linespec, address, or explicit location as described\n\
15561 below.\n\
15562 \n\
15563 With no LOCATION, uses current execution address of the selected\n\
15564 stack frame. This is useful for breaking on return to a stack frame.\n\
15565 \n\
15566 THREADNUM is the number from \"info threads\".\n\
15567 CONDITION is a boolean expression.\n\
15568 \n" LOCATION_HELP_STRING "\n\
15569 Multiple breakpoints at one place are permitted, and useful if their\n\
15570 conditions are different.\n\
15571 \n\
15572 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15573
15574 /* List of subcommands for "catch". */
15575 static struct cmd_list_element *catch_cmdlist;
15576
15577 /* List of subcommands for "tcatch". */
15578 static struct cmd_list_element *tcatch_cmdlist;
15579
15580 void
15581 add_catch_command (const char *name, const char *docstring,
15582 cmd_sfunc_ftype *sfunc,
15583 completer_ftype *completer,
15584 void *user_data_catch,
15585 void *user_data_tcatch)
15586 {
15587 struct cmd_list_element *command;
15588
15589 command = add_cmd (name, class_breakpoint, NULL, docstring,
15590 &catch_cmdlist);
15591 set_cmd_sfunc (command, sfunc);
15592 set_cmd_context (command, user_data_catch);
15593 set_cmd_completer (command, completer);
15594
15595 command = add_cmd (name, class_breakpoint, NULL, docstring,
15596 &tcatch_cmdlist);
15597 set_cmd_sfunc (command, sfunc);
15598 set_cmd_context (command, user_data_tcatch);
15599 set_cmd_completer (command, completer);
15600 }
15601
15602 static void
15603 save_command (char *arg, int from_tty)
15604 {
15605 printf_unfiltered (_("\"save\" must be followed by "
15606 "the name of a save subcommand.\n"));
15607 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15608 }
15609
15610 struct breakpoint *
15611 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15612 void *data)
15613 {
15614 struct breakpoint *b, *b_tmp;
15615
15616 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15617 {
15618 if ((*callback) (b, data))
15619 return b;
15620 }
15621
15622 return NULL;
15623 }
15624
15625 /* Zero if any of the breakpoint's locations could be a location where
15626 functions have been inlined, nonzero otherwise. */
15627
15628 static int
15629 is_non_inline_function (struct breakpoint *b)
15630 {
15631 /* The shared library event breakpoint is set on the address of a
15632 non-inline function. */
15633 if (b->type == bp_shlib_event)
15634 return 1;
15635
15636 return 0;
15637 }
15638
15639 /* Nonzero if the specified PC cannot be a location where functions
15640 have been inlined. */
15641
15642 int
15643 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15644 const struct target_waitstatus *ws)
15645 {
15646 struct breakpoint *b;
15647 struct bp_location *bl;
15648
15649 ALL_BREAKPOINTS (b)
15650 {
15651 if (!is_non_inline_function (b))
15652 continue;
15653
15654 for (bl = b->loc; bl != NULL; bl = bl->next)
15655 {
15656 if (!bl->shlib_disabled
15657 && bpstat_check_location (bl, aspace, pc, ws))
15658 return 1;
15659 }
15660 }
15661
15662 return 0;
15663 }
15664
15665 /* Remove any references to OBJFILE which is going to be freed. */
15666
15667 void
15668 breakpoint_free_objfile (struct objfile *objfile)
15669 {
15670 struct bp_location **locp, *loc;
15671
15672 ALL_BP_LOCATIONS (loc, locp)
15673 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15674 loc->symtab = NULL;
15675 }
15676
15677 void
15678 initialize_breakpoint_ops (void)
15679 {
15680 static int initialized = 0;
15681
15682 struct breakpoint_ops *ops;
15683
15684 if (initialized)
15685 return;
15686 initialized = 1;
15687
15688 /* The breakpoint_ops structure to be inherit by all kinds of
15689 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15690 internal and momentary breakpoints, etc.). */
15691 ops = &bkpt_base_breakpoint_ops;
15692 *ops = base_breakpoint_ops;
15693 ops->re_set = bkpt_re_set;
15694 ops->insert_location = bkpt_insert_location;
15695 ops->remove_location = bkpt_remove_location;
15696 ops->breakpoint_hit = bkpt_breakpoint_hit;
15697 ops->create_sals_from_location = bkpt_create_sals_from_location;
15698 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15699 ops->decode_location = bkpt_decode_location;
15700
15701 /* The breakpoint_ops structure to be used in regular breakpoints. */
15702 ops = &bkpt_breakpoint_ops;
15703 *ops = bkpt_base_breakpoint_ops;
15704 ops->re_set = bkpt_re_set;
15705 ops->resources_needed = bkpt_resources_needed;
15706 ops->print_it = bkpt_print_it;
15707 ops->print_mention = bkpt_print_mention;
15708 ops->print_recreate = bkpt_print_recreate;
15709
15710 /* Ranged breakpoints. */
15711 ops = &ranged_breakpoint_ops;
15712 *ops = bkpt_breakpoint_ops;
15713 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15714 ops->resources_needed = resources_needed_ranged_breakpoint;
15715 ops->print_it = print_it_ranged_breakpoint;
15716 ops->print_one = print_one_ranged_breakpoint;
15717 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15718 ops->print_mention = print_mention_ranged_breakpoint;
15719 ops->print_recreate = print_recreate_ranged_breakpoint;
15720
15721 /* Internal breakpoints. */
15722 ops = &internal_breakpoint_ops;
15723 *ops = bkpt_base_breakpoint_ops;
15724 ops->re_set = internal_bkpt_re_set;
15725 ops->check_status = internal_bkpt_check_status;
15726 ops->print_it = internal_bkpt_print_it;
15727 ops->print_mention = internal_bkpt_print_mention;
15728
15729 /* Momentary breakpoints. */
15730 ops = &momentary_breakpoint_ops;
15731 *ops = bkpt_base_breakpoint_ops;
15732 ops->re_set = momentary_bkpt_re_set;
15733 ops->check_status = momentary_bkpt_check_status;
15734 ops->print_it = momentary_bkpt_print_it;
15735 ops->print_mention = momentary_bkpt_print_mention;
15736
15737 /* Probe breakpoints. */
15738 ops = &bkpt_probe_breakpoint_ops;
15739 *ops = bkpt_breakpoint_ops;
15740 ops->insert_location = bkpt_probe_insert_location;
15741 ops->remove_location = bkpt_probe_remove_location;
15742 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15743 ops->decode_location = bkpt_probe_decode_location;
15744
15745 /* Watchpoints. */
15746 ops = &watchpoint_breakpoint_ops;
15747 *ops = base_breakpoint_ops;
15748 ops->re_set = re_set_watchpoint;
15749 ops->insert_location = insert_watchpoint;
15750 ops->remove_location = remove_watchpoint;
15751 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15752 ops->check_status = check_status_watchpoint;
15753 ops->resources_needed = resources_needed_watchpoint;
15754 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15755 ops->print_it = print_it_watchpoint;
15756 ops->print_mention = print_mention_watchpoint;
15757 ops->print_recreate = print_recreate_watchpoint;
15758 ops->explains_signal = explains_signal_watchpoint;
15759
15760 /* Masked watchpoints. */
15761 ops = &masked_watchpoint_breakpoint_ops;
15762 *ops = watchpoint_breakpoint_ops;
15763 ops->insert_location = insert_masked_watchpoint;
15764 ops->remove_location = remove_masked_watchpoint;
15765 ops->resources_needed = resources_needed_masked_watchpoint;
15766 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15767 ops->print_it = print_it_masked_watchpoint;
15768 ops->print_one_detail = print_one_detail_masked_watchpoint;
15769 ops->print_mention = print_mention_masked_watchpoint;
15770 ops->print_recreate = print_recreate_masked_watchpoint;
15771
15772 /* Tracepoints. */
15773 ops = &tracepoint_breakpoint_ops;
15774 *ops = base_breakpoint_ops;
15775 ops->re_set = tracepoint_re_set;
15776 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15777 ops->print_one_detail = tracepoint_print_one_detail;
15778 ops->print_mention = tracepoint_print_mention;
15779 ops->print_recreate = tracepoint_print_recreate;
15780 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15781 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15782 ops->decode_location = tracepoint_decode_location;
15783
15784 /* Probe tracepoints. */
15785 ops = &tracepoint_probe_breakpoint_ops;
15786 *ops = tracepoint_breakpoint_ops;
15787 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15788 ops->decode_location = tracepoint_probe_decode_location;
15789
15790 /* Static tracepoints with marker (`-m'). */
15791 ops = &strace_marker_breakpoint_ops;
15792 *ops = tracepoint_breakpoint_ops;
15793 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15794 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15795 ops->decode_location = strace_marker_decode_location;
15796
15797 /* Fork catchpoints. */
15798 ops = &catch_fork_breakpoint_ops;
15799 *ops = base_breakpoint_ops;
15800 ops->insert_location = insert_catch_fork;
15801 ops->remove_location = remove_catch_fork;
15802 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15803 ops->print_it = print_it_catch_fork;
15804 ops->print_one = print_one_catch_fork;
15805 ops->print_mention = print_mention_catch_fork;
15806 ops->print_recreate = print_recreate_catch_fork;
15807
15808 /* Vfork catchpoints. */
15809 ops = &catch_vfork_breakpoint_ops;
15810 *ops = base_breakpoint_ops;
15811 ops->insert_location = insert_catch_vfork;
15812 ops->remove_location = remove_catch_vfork;
15813 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15814 ops->print_it = print_it_catch_vfork;
15815 ops->print_one = print_one_catch_vfork;
15816 ops->print_mention = print_mention_catch_vfork;
15817 ops->print_recreate = print_recreate_catch_vfork;
15818
15819 /* Exec catchpoints. */
15820 ops = &catch_exec_breakpoint_ops;
15821 *ops = base_breakpoint_ops;
15822 ops->insert_location = insert_catch_exec;
15823 ops->remove_location = remove_catch_exec;
15824 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15825 ops->print_it = print_it_catch_exec;
15826 ops->print_one = print_one_catch_exec;
15827 ops->print_mention = print_mention_catch_exec;
15828 ops->print_recreate = print_recreate_catch_exec;
15829
15830 /* Solib-related catchpoints. */
15831 ops = &catch_solib_breakpoint_ops;
15832 *ops = base_breakpoint_ops;
15833 ops->insert_location = insert_catch_solib;
15834 ops->remove_location = remove_catch_solib;
15835 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15836 ops->check_status = check_status_catch_solib;
15837 ops->print_it = print_it_catch_solib;
15838 ops->print_one = print_one_catch_solib;
15839 ops->print_mention = print_mention_catch_solib;
15840 ops->print_recreate = print_recreate_catch_solib;
15841
15842 ops = &dprintf_breakpoint_ops;
15843 *ops = bkpt_base_breakpoint_ops;
15844 ops->re_set = dprintf_re_set;
15845 ops->resources_needed = bkpt_resources_needed;
15846 ops->print_it = bkpt_print_it;
15847 ops->print_mention = bkpt_print_mention;
15848 ops->print_recreate = dprintf_print_recreate;
15849 ops->after_condition_true = dprintf_after_condition_true;
15850 ops->breakpoint_hit = dprintf_breakpoint_hit;
15851 }
15852
15853 /* Chain containing all defined "enable breakpoint" subcommands. */
15854
15855 static struct cmd_list_element *enablebreaklist = NULL;
15856
15857 void
15858 _initialize_breakpoint (void)
15859 {
15860 struct cmd_list_element *c;
15861
15862 initialize_breakpoint_ops ();
15863
15864 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15865 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15866 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15867
15868 breakpoint_objfile_key
15869 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15870
15871 breakpoint_chain = 0;
15872 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15873 before a breakpoint is set. */
15874 breakpoint_count = 0;
15875
15876 tracepoint_count = 0;
15877
15878 add_com ("ignore", class_breakpoint, ignore_command, _("\
15879 Set ignore-count of breakpoint number N to COUNT.\n\
15880 Usage is `ignore N COUNT'."));
15881
15882 add_com ("commands", class_breakpoint, commands_command, _("\
15883 Set commands to be executed when the given breakpoints are hit.\n\
15884 Give a space-separated breakpoint list as argument after \"commands\".\n\
15885 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15886 (e.g. `5-7').\n\
15887 With no argument, the targeted breakpoint is the last one set.\n\
15888 The commands themselves follow starting on the next line.\n\
15889 Type a line containing \"end\" to indicate the end of them.\n\
15890 Give \"silent\" as the first line to make the breakpoint silent;\n\
15891 then no output is printed when it is hit, except what the commands print."));
15892
15893 c = add_com ("condition", class_breakpoint, condition_command, _("\
15894 Specify breakpoint number N to break only if COND is true.\n\
15895 Usage is `condition N COND', where N is an integer and COND is an\n\
15896 expression to be evaluated whenever breakpoint N is reached."));
15897 set_cmd_completer (c, condition_completer);
15898
15899 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15900 Set a temporary breakpoint.\n\
15901 Like \"break\" except the breakpoint is only temporary,\n\
15902 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15903 by using \"enable delete\" on the breakpoint number.\n\
15904 \n"
15905 BREAK_ARGS_HELP ("tbreak")));
15906 set_cmd_completer (c, location_completer);
15907
15908 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15909 Set a hardware assisted breakpoint.\n\
15910 Like \"break\" except the breakpoint requires hardware support,\n\
15911 some target hardware may not have this support.\n\
15912 \n"
15913 BREAK_ARGS_HELP ("hbreak")));
15914 set_cmd_completer (c, location_completer);
15915
15916 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15917 Set a temporary hardware assisted breakpoint.\n\
15918 Like \"hbreak\" except the breakpoint is only temporary,\n\
15919 so it will be deleted when hit.\n\
15920 \n"
15921 BREAK_ARGS_HELP ("thbreak")));
15922 set_cmd_completer (c, location_completer);
15923
15924 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15925 Enable some breakpoints.\n\
15926 Give breakpoint numbers (separated by spaces) as arguments.\n\
15927 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15928 This is used to cancel the effect of the \"disable\" command.\n\
15929 With a subcommand you can enable temporarily."),
15930 &enablelist, "enable ", 1, &cmdlist);
15931
15932 add_com_alias ("en", "enable", class_breakpoint, 1);
15933
15934 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15935 Enable some breakpoints.\n\
15936 Give breakpoint numbers (separated by spaces) as arguments.\n\
15937 This is used to cancel the effect of the \"disable\" command.\n\
15938 May be abbreviated to simply \"enable\".\n"),
15939 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15940
15941 add_cmd ("once", no_class, enable_once_command, _("\
15942 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15943 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15944 &enablebreaklist);
15945
15946 add_cmd ("delete", no_class, enable_delete_command, _("\
15947 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15948 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15949 &enablebreaklist);
15950
15951 add_cmd ("count", no_class, enable_count_command, _("\
15952 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15953 If a breakpoint is hit while enabled in this fashion,\n\
15954 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15955 &enablebreaklist);
15956
15957 add_cmd ("delete", no_class, enable_delete_command, _("\
15958 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15959 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15960 &enablelist);
15961
15962 add_cmd ("once", no_class, enable_once_command, _("\
15963 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15964 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15965 &enablelist);
15966
15967 add_cmd ("count", no_class, enable_count_command, _("\
15968 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15969 If a breakpoint is hit while enabled in this fashion,\n\
15970 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15971 &enablelist);
15972
15973 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15974 Disable some breakpoints.\n\
15975 Arguments are breakpoint numbers with spaces in between.\n\
15976 To disable all breakpoints, give no argument.\n\
15977 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15978 &disablelist, "disable ", 1, &cmdlist);
15979 add_com_alias ("dis", "disable", class_breakpoint, 1);
15980 add_com_alias ("disa", "disable", class_breakpoint, 1);
15981
15982 add_cmd ("breakpoints", class_alias, disable_command, _("\
15983 Disable some breakpoints.\n\
15984 Arguments are breakpoint numbers with spaces in between.\n\
15985 To disable all breakpoints, give no argument.\n\
15986 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15987 This command may be abbreviated \"disable\"."),
15988 &disablelist);
15989
15990 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15991 Delete some breakpoints or auto-display expressions.\n\
15992 Arguments are breakpoint numbers with spaces in between.\n\
15993 To delete all breakpoints, give no argument.\n\
15994 \n\
15995 Also a prefix command for deletion of other GDB objects.\n\
15996 The \"unset\" command is also an alias for \"delete\"."),
15997 &deletelist, "delete ", 1, &cmdlist);
15998 add_com_alias ("d", "delete", class_breakpoint, 1);
15999 add_com_alias ("del", "delete", class_breakpoint, 1);
16000
16001 add_cmd ("breakpoints", class_alias, delete_command, _("\
16002 Delete some breakpoints or auto-display expressions.\n\
16003 Arguments are breakpoint numbers with spaces in between.\n\
16004 To delete all breakpoints, give no argument.\n\
16005 This command may be abbreviated \"delete\"."),
16006 &deletelist);
16007
16008 add_com ("clear", class_breakpoint, clear_command, _("\
16009 Clear breakpoint at specified location.\n\
16010 Argument may be a linespec, explicit, or address location as described below.\n\
16011 \n\
16012 With no argument, clears all breakpoints in the line that the selected frame\n\
16013 is executing in.\n"
16014 "\n" LOCATION_HELP_STRING "\n\
16015 See also the \"delete\" command which clears breakpoints by number."));
16016 add_com_alias ("cl", "clear", class_breakpoint, 1);
16017
16018 c = add_com ("break", class_breakpoint, break_command, _("\
16019 Set breakpoint at specified location.\n"
16020 BREAK_ARGS_HELP ("break")));
16021 set_cmd_completer (c, location_completer);
16022
16023 add_com_alias ("b", "break", class_run, 1);
16024 add_com_alias ("br", "break", class_run, 1);
16025 add_com_alias ("bre", "break", class_run, 1);
16026 add_com_alias ("brea", "break", class_run, 1);
16027
16028 if (dbx_commands)
16029 {
16030 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16031 Break in function/address or break at a line in the current file."),
16032 &stoplist, "stop ", 1, &cmdlist);
16033 add_cmd ("in", class_breakpoint, stopin_command,
16034 _("Break in function or address."), &stoplist);
16035 add_cmd ("at", class_breakpoint, stopat_command,
16036 _("Break at a line in the current file."), &stoplist);
16037 add_com ("status", class_info, info_breakpoints_command, _("\
16038 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16039 The \"Type\" column indicates one of:\n\
16040 \tbreakpoint - normal breakpoint\n\
16041 \twatchpoint - watchpoint\n\
16042 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16043 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16044 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16045 address and file/line number respectively.\n\
16046 \n\
16047 Convenience variable \"$_\" and default examine address for \"x\"\n\
16048 are set to the address of the last breakpoint listed unless the command\n\
16049 is prefixed with \"server \".\n\n\
16050 Convenience variable \"$bpnum\" contains the number of the last\n\
16051 breakpoint set."));
16052 }
16053
16054 add_info ("breakpoints", info_breakpoints_command, _("\
16055 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16056 The \"Type\" column indicates one of:\n\
16057 \tbreakpoint - normal breakpoint\n\
16058 \twatchpoint - watchpoint\n\
16059 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16060 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16061 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16062 address and file/line number respectively.\n\
16063 \n\
16064 Convenience variable \"$_\" and default examine address for \"x\"\n\
16065 are set to the address of the last breakpoint listed unless the command\n\
16066 is prefixed with \"server \".\n\n\
16067 Convenience variable \"$bpnum\" contains the number of the last\n\
16068 breakpoint set."));
16069
16070 add_info_alias ("b", "breakpoints", 1);
16071
16072 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16073 Status of all breakpoints, or breakpoint number NUMBER.\n\
16074 The \"Type\" column indicates one of:\n\
16075 \tbreakpoint - normal breakpoint\n\
16076 \twatchpoint - watchpoint\n\
16077 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16078 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16079 \tuntil - internal breakpoint used by the \"until\" command\n\
16080 \tfinish - internal breakpoint used by the \"finish\" command\n\
16081 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16082 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16083 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16084 address and file/line number respectively.\n\
16085 \n\
16086 Convenience variable \"$_\" and default examine address for \"x\"\n\
16087 are set to the address of the last breakpoint listed unless the command\n\
16088 is prefixed with \"server \".\n\n\
16089 Convenience variable \"$bpnum\" contains the number of the last\n\
16090 breakpoint set."),
16091 &maintenanceinfolist);
16092
16093 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16094 Set catchpoints to catch events."),
16095 &catch_cmdlist, "catch ",
16096 0/*allow-unknown*/, &cmdlist);
16097
16098 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16099 Set temporary catchpoints to catch events."),
16100 &tcatch_cmdlist, "tcatch ",
16101 0/*allow-unknown*/, &cmdlist);
16102
16103 add_catch_command ("fork", _("Catch calls to fork."),
16104 catch_fork_command_1,
16105 NULL,
16106 (void *) (uintptr_t) catch_fork_permanent,
16107 (void *) (uintptr_t) catch_fork_temporary);
16108 add_catch_command ("vfork", _("Catch calls to vfork."),
16109 catch_fork_command_1,
16110 NULL,
16111 (void *) (uintptr_t) catch_vfork_permanent,
16112 (void *) (uintptr_t) catch_vfork_temporary);
16113 add_catch_command ("exec", _("Catch calls to exec."),
16114 catch_exec_command_1,
16115 NULL,
16116 CATCH_PERMANENT,
16117 CATCH_TEMPORARY);
16118 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16119 Usage: catch load [REGEX]\n\
16120 If REGEX is given, only stop for libraries matching the regular expression."),
16121 catch_load_command_1,
16122 NULL,
16123 CATCH_PERMANENT,
16124 CATCH_TEMPORARY);
16125 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16126 Usage: catch unload [REGEX]\n\
16127 If REGEX is given, only stop for libraries matching the regular expression."),
16128 catch_unload_command_1,
16129 NULL,
16130 CATCH_PERMANENT,
16131 CATCH_TEMPORARY);
16132
16133 c = add_com ("watch", class_breakpoint, watch_command, _("\
16134 Set a watchpoint for an expression.\n\
16135 Usage: watch [-l|-location] EXPRESSION\n\
16136 A watchpoint stops execution of your program whenever the value of\n\
16137 an expression changes.\n\
16138 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16139 the memory to which it refers."));
16140 set_cmd_completer (c, expression_completer);
16141
16142 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16143 Set a read watchpoint for an expression.\n\
16144 Usage: rwatch [-l|-location] EXPRESSION\n\
16145 A watchpoint stops execution of your program whenever the value of\n\
16146 an expression is read.\n\
16147 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16148 the memory to which it refers."));
16149 set_cmd_completer (c, expression_completer);
16150
16151 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16152 Set a watchpoint for an expression.\n\
16153 Usage: awatch [-l|-location] EXPRESSION\n\
16154 A watchpoint stops execution of your program whenever the value of\n\
16155 an expression is either read or written.\n\
16156 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16157 the memory to which it refers."));
16158 set_cmd_completer (c, expression_completer);
16159
16160 add_info ("watchpoints", info_watchpoints_command, _("\
16161 Status of specified watchpoints (all watchpoints if no argument)."));
16162
16163 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16164 respond to changes - contrary to the description. */
16165 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16166 &can_use_hw_watchpoints, _("\
16167 Set debugger's willingness to use watchpoint hardware."), _("\
16168 Show debugger's willingness to use watchpoint hardware."), _("\
16169 If zero, gdb will not use hardware for new watchpoints, even if\n\
16170 such is available. (However, any hardware watchpoints that were\n\
16171 created before setting this to nonzero, will continue to use watchpoint\n\
16172 hardware.)"),
16173 NULL,
16174 show_can_use_hw_watchpoints,
16175 &setlist, &showlist);
16176
16177 can_use_hw_watchpoints = 1;
16178
16179 /* Tracepoint manipulation commands. */
16180
16181 c = add_com ("trace", class_breakpoint, trace_command, _("\
16182 Set a tracepoint at specified location.\n\
16183 \n"
16184 BREAK_ARGS_HELP ("trace") "\n\
16185 Do \"help tracepoints\" for info on other tracepoint commands."));
16186 set_cmd_completer (c, location_completer);
16187
16188 add_com_alias ("tp", "trace", class_alias, 0);
16189 add_com_alias ("tr", "trace", class_alias, 1);
16190 add_com_alias ("tra", "trace", class_alias, 1);
16191 add_com_alias ("trac", "trace", class_alias, 1);
16192
16193 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16194 Set a fast tracepoint at specified location.\n\
16195 \n"
16196 BREAK_ARGS_HELP ("ftrace") "\n\
16197 Do \"help tracepoints\" for info on other tracepoint commands."));
16198 set_cmd_completer (c, location_completer);
16199
16200 c = add_com ("strace", class_breakpoint, strace_command, _("\
16201 Set a static tracepoint at location or marker.\n\
16202 \n\
16203 strace [LOCATION] [if CONDITION]\n\
16204 LOCATION may be a linespec, explicit, or address location (described below) \n\
16205 or -m MARKER_ID.\n\n\
16206 If a marker id is specified, probe the marker with that name. With\n\
16207 no LOCATION, uses current execution address of the selected stack frame.\n\
16208 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16209 This collects arbitrary user data passed in the probe point call to the\n\
16210 tracing library. You can inspect it when analyzing the trace buffer,\n\
16211 by printing the $_sdata variable like any other convenience variable.\n\
16212 \n\
16213 CONDITION is a boolean expression.\n\
16214 \n" LOCATION_HELP_STRING "\n\
16215 Multiple tracepoints at one place are permitted, and useful if their\n\
16216 conditions are different.\n\
16217 \n\
16218 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16219 Do \"help tracepoints\" for info on other tracepoint commands."));
16220 set_cmd_completer (c, location_completer);
16221
16222 add_info ("tracepoints", info_tracepoints_command, _("\
16223 Status of specified tracepoints (all tracepoints if no argument).\n\
16224 Convenience variable \"$tpnum\" contains the number of the\n\
16225 last tracepoint set."));
16226
16227 add_info_alias ("tp", "tracepoints", 1);
16228
16229 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16230 Delete specified tracepoints.\n\
16231 Arguments are tracepoint numbers, separated by spaces.\n\
16232 No argument means delete all tracepoints."),
16233 &deletelist);
16234 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16235
16236 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16237 Disable specified tracepoints.\n\
16238 Arguments are tracepoint numbers, separated by spaces.\n\
16239 No argument means disable all tracepoints."),
16240 &disablelist);
16241 deprecate_cmd (c, "disable");
16242
16243 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16244 Enable specified tracepoints.\n\
16245 Arguments are tracepoint numbers, separated by spaces.\n\
16246 No argument means enable all tracepoints."),
16247 &enablelist);
16248 deprecate_cmd (c, "enable");
16249
16250 add_com ("passcount", class_trace, trace_pass_command, _("\
16251 Set the passcount for a tracepoint.\n\
16252 The trace will end when the tracepoint has been passed 'count' times.\n\
16253 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16254 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16255
16256 add_prefix_cmd ("save", class_breakpoint, save_command,
16257 _("Save breakpoint definitions as a script."),
16258 &save_cmdlist, "save ",
16259 0/*allow-unknown*/, &cmdlist);
16260
16261 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16262 Save current breakpoint definitions as a script.\n\
16263 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16264 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16265 session to restore them."),
16266 &save_cmdlist);
16267 set_cmd_completer (c, filename_completer);
16268
16269 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16270 Save current tracepoint definitions as a script.\n\
16271 Use the 'source' command in another debug session to restore them."),
16272 &save_cmdlist);
16273 set_cmd_completer (c, filename_completer);
16274
16275 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16276 deprecate_cmd (c, "save tracepoints");
16277
16278 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16279 Breakpoint specific settings\n\
16280 Configure various breakpoint-specific variables such as\n\
16281 pending breakpoint behavior"),
16282 &breakpoint_set_cmdlist, "set breakpoint ",
16283 0/*allow-unknown*/, &setlist);
16284 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16285 Breakpoint specific settings\n\
16286 Configure various breakpoint-specific variables such as\n\
16287 pending breakpoint behavior"),
16288 &breakpoint_show_cmdlist, "show breakpoint ",
16289 0/*allow-unknown*/, &showlist);
16290
16291 add_setshow_auto_boolean_cmd ("pending", no_class,
16292 &pending_break_support, _("\
16293 Set debugger's behavior regarding pending breakpoints."), _("\
16294 Show debugger's behavior regarding pending breakpoints."), _("\
16295 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16296 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16297 an error. If auto, an unrecognized breakpoint location results in a\n\
16298 user-query to see if a pending breakpoint should be created."),
16299 NULL,
16300 show_pending_break_support,
16301 &breakpoint_set_cmdlist,
16302 &breakpoint_show_cmdlist);
16303
16304 pending_break_support = AUTO_BOOLEAN_AUTO;
16305
16306 add_setshow_boolean_cmd ("auto-hw", no_class,
16307 &automatic_hardware_breakpoints, _("\
16308 Set automatic usage of hardware breakpoints."), _("\
16309 Show automatic usage of hardware breakpoints."), _("\
16310 If set, the debugger will automatically use hardware breakpoints for\n\
16311 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16312 a warning will be emitted for such breakpoints."),
16313 NULL,
16314 show_automatic_hardware_breakpoints,
16315 &breakpoint_set_cmdlist,
16316 &breakpoint_show_cmdlist);
16317
16318 add_setshow_boolean_cmd ("always-inserted", class_support,
16319 &always_inserted_mode, _("\
16320 Set mode for inserting breakpoints."), _("\
16321 Show mode for inserting breakpoints."), _("\
16322 When this mode is on, breakpoints are inserted immediately as soon as\n\
16323 they're created, kept inserted even when execution stops, and removed\n\
16324 only when the user deletes them. When this mode is off (the default),\n\
16325 breakpoints are inserted only when execution continues, and removed\n\
16326 when execution stops."),
16327 NULL,
16328 &show_always_inserted_mode,
16329 &breakpoint_set_cmdlist,
16330 &breakpoint_show_cmdlist);
16331
16332 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16333 condition_evaluation_enums,
16334 &condition_evaluation_mode_1, _("\
16335 Set mode of breakpoint condition evaluation."), _("\
16336 Show mode of breakpoint condition evaluation."), _("\
16337 When this is set to \"host\", breakpoint conditions will be\n\
16338 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16339 breakpoint conditions will be downloaded to the target (if the target\n\
16340 supports such feature) and conditions will be evaluated on the target's side.\n\
16341 If this is set to \"auto\" (default), this will be automatically set to\n\
16342 \"target\" if it supports condition evaluation, otherwise it will\n\
16343 be set to \"gdb\""),
16344 &set_condition_evaluation_mode,
16345 &show_condition_evaluation_mode,
16346 &breakpoint_set_cmdlist,
16347 &breakpoint_show_cmdlist);
16348
16349 add_com ("break-range", class_breakpoint, break_range_command, _("\
16350 Set a breakpoint for an address range.\n\
16351 break-range START-LOCATION, END-LOCATION\n\
16352 where START-LOCATION and END-LOCATION can be one of the following:\n\
16353 LINENUM, for that line in the current file,\n\
16354 FILE:LINENUM, for that line in that file,\n\
16355 +OFFSET, for that number of lines after the current line\n\
16356 or the start of the range\n\
16357 FUNCTION, for the first line in that function,\n\
16358 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16359 *ADDRESS, for the instruction at that address.\n\
16360 \n\
16361 The breakpoint will stop execution of the inferior whenever it executes\n\
16362 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16363 range (including START-LOCATION and END-LOCATION)."));
16364
16365 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16366 Set a dynamic printf at specified location.\n\
16367 dprintf location,format string,arg1,arg2,...\n\
16368 location may be a linespec, explicit, or address location.\n"
16369 "\n" LOCATION_HELP_STRING));
16370 set_cmd_completer (c, location_completer);
16371
16372 add_setshow_enum_cmd ("dprintf-style", class_support,
16373 dprintf_style_enums, &dprintf_style, _("\
16374 Set the style of usage for dynamic printf."), _("\
16375 Show the style of usage for dynamic printf."), _("\
16376 This setting chooses how GDB will do a dynamic printf.\n\
16377 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16378 console, as with the \"printf\" command.\n\
16379 If the value is \"call\", the print is done by calling a function in your\n\
16380 program; by default printf(), but you can choose a different function or\n\
16381 output stream by setting dprintf-function and dprintf-channel."),
16382 update_dprintf_commands, NULL,
16383 &setlist, &showlist);
16384
16385 dprintf_function = xstrdup ("printf");
16386 add_setshow_string_cmd ("dprintf-function", class_support,
16387 &dprintf_function, _("\
16388 Set the function to use for dynamic printf"), _("\
16389 Show the function to use for dynamic printf"), NULL,
16390 update_dprintf_commands, NULL,
16391 &setlist, &showlist);
16392
16393 dprintf_channel = xstrdup ("");
16394 add_setshow_string_cmd ("dprintf-channel", class_support,
16395 &dprintf_channel, _("\
16396 Set the channel to use for dynamic printf"), _("\
16397 Show the channel to use for dynamic printf"), NULL,
16398 update_dprintf_commands, NULL,
16399 &setlist, &showlist);
16400
16401 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16402 &disconnected_dprintf, _("\
16403 Set whether dprintf continues after GDB disconnects."), _("\
16404 Show whether dprintf continues after GDB disconnects."), _("\
16405 Use this to let dprintf commands continue to hit and produce output\n\
16406 even if GDB disconnects or detaches from the target."),
16407 NULL,
16408 NULL,
16409 &setlist, &showlist);
16410
16411 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16412 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16413 (target agent only) This is useful for formatted output in user-defined commands."));
16414
16415 automatic_hardware_breakpoints = 1;
16416
16417 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16418 observer_attach_thread_exit (remove_threaded_breakpoints);
16419 }
This page took 0.401238 seconds and 4 git commands to generate.