Replace some uses of xstrprintf with string_printf
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2018 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 "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "continuations.h"
63 #include "stack.h"
64 #include "skip.h"
65 #include "ax-gdb.h"
66 #include "dummy-frame.h"
67 #include "interps.h"
68 #include "format.h"
69 #include "thread-fsm.h"
70 #include "tid-parse.h"
71
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this. */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "extension.h"
81 #include <algorithm>
82 #include "progspace-and-thread.h"
83 #include "common/array-view.h"
84 #include "common/gdb_optional.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 map_breakpoint_numbers (const char *,
97 gdb::function_view<void (breakpoint *)>);
98
99 static void breakpoint_re_set_default (struct breakpoint *);
100
101 static void
102 create_sals_from_location_default (const struct event_location *location,
103 struct linespec_result *canonical,
104 enum bptype type_wanted);
105
106 static void create_breakpoints_sal_default (struct gdbarch *,
107 struct linespec_result *,
108 gdb::unique_xmalloc_ptr<char>,
109 gdb::unique_xmalloc_ptr<char>,
110 enum bptype,
111 enum bpdisp, int, int,
112 int,
113 const struct breakpoint_ops *,
114 int, int, int, unsigned);
115
116 static std::vector<symtab_and_line> decode_location_default
117 (struct breakpoint *b, const struct event_location *location,
118 struct program_space *search_pspace);
119
120 static int can_use_hardware_watchpoint
121 (const std::vector<value_ref_ptr> &vals);
122
123 static void mention (struct breakpoint *);
124
125 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
126 enum bptype,
127 const struct breakpoint_ops *);
128 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
129 const struct symtab_and_line *);
130
131 /* This function is used in gdbtk sources and thus can not be made
132 static. */
133 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
134 struct symtab_and_line,
135 enum bptype,
136 const struct breakpoint_ops *);
137
138 static struct breakpoint *
139 momentary_breakpoint_from_master (struct breakpoint *orig,
140 enum bptype type,
141 const struct breakpoint_ops *ops,
142 int loc_enabled);
143
144 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
145
146 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
147 CORE_ADDR bpaddr,
148 enum bptype bptype);
149
150 static void describe_other_breakpoints (struct gdbarch *,
151 struct program_space *, CORE_ADDR,
152 struct obj_section *, int);
153
154 static int watchpoint_locations_match (struct bp_location *loc1,
155 struct bp_location *loc2);
156
157 static int breakpoint_location_address_match (struct bp_location *bl,
158 const struct address_space *aspace,
159 CORE_ADDR addr);
160
161 static int breakpoint_location_address_range_overlap (struct bp_location *,
162 const address_space *,
163 CORE_ADDR, int);
164
165 static int remove_breakpoint (struct bp_location *);
166 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
167
168 static enum print_stop_action print_bp_stop_message (bpstat bs);
169
170 static int hw_breakpoint_used_count (void);
171
172 static int hw_watchpoint_use_count (struct breakpoint *);
173
174 static int hw_watchpoint_used_count_others (struct breakpoint *except,
175 enum bptype type,
176 int *other_type_used);
177
178 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
179 int count);
180
181 static void free_bp_location (struct bp_location *loc);
182 static void incref_bp_location (struct bp_location *loc);
183 static void decref_bp_location (struct bp_location **loc);
184
185 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
186
187 /* update_global_location_list's modes of operation wrt to whether to
188 insert locations now. */
189 enum ugll_insert_mode
190 {
191 /* Don't insert any breakpoint locations into the inferior, only
192 remove already-inserted locations that no longer should be
193 inserted. Functions that delete a breakpoint or breakpoints
194 should specify this mode, so that deleting a breakpoint doesn't
195 have the side effect of inserting the locations of other
196 breakpoints that are marked not-inserted, but should_be_inserted
197 returns true on them.
198
199 This behavior is useful is situations close to tear-down -- e.g.,
200 after an exec, while the target still has execution, but
201 breakpoint shadows of the previous executable image should *NOT*
202 be restored to the new image; or before detaching, where the
203 target still has execution and wants to delete breakpoints from
204 GDB's lists, and all breakpoints had already been removed from
205 the inferior. */
206 UGLL_DONT_INSERT,
207
208 /* May insert breakpoints iff breakpoints_should_be_inserted_now
209 claims breakpoints should be inserted now. */
210 UGLL_MAY_INSERT,
211
212 /* Insert locations now, irrespective of
213 breakpoints_should_be_inserted_now. E.g., say all threads are
214 stopped right now, and the user did "continue". We need to
215 insert breakpoints _before_ resuming the target, but
216 UGLL_MAY_INSERT wouldn't insert them, because
217 breakpoints_should_be_inserted_now returns false at that point,
218 as no thread is running yet. */
219 UGLL_INSERT
220 };
221
222 static void update_global_location_list (enum ugll_insert_mode);
223
224 static void update_global_location_list_nothrow (enum ugll_insert_mode);
225
226 static int is_hardware_watchpoint (const struct breakpoint *bpt);
227
228 static void insert_breakpoint_locations (void);
229
230 static void trace_pass_command (const char *, int);
231
232 static void set_tracepoint_count (int num);
233
234 static int is_masked_watchpoint (const struct breakpoint *b);
235
236 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
237
238 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
239 otherwise. */
240
241 static int strace_marker_p (struct breakpoint *b);
242
243 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
244 that are implemented on top of software or hardware breakpoints
245 (user breakpoints, internal and momentary breakpoints, etc.). */
246 static struct breakpoint_ops bkpt_base_breakpoint_ops;
247
248 /* Internal breakpoints class type. */
249 static struct breakpoint_ops internal_breakpoint_ops;
250
251 /* Momentary breakpoints class type. */
252 static struct breakpoint_ops momentary_breakpoint_ops;
253
254 /* The breakpoint_ops structure to be used in regular user created
255 breakpoints. */
256 struct breakpoint_ops bkpt_breakpoint_ops;
257
258 /* Breakpoints set on probes. */
259 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
260
261 /* Dynamic printf class type. */
262 struct breakpoint_ops dprintf_breakpoint_ops;
263
264 /* The style in which to perform a dynamic printf. This is a user
265 option because different output options have different tradeoffs;
266 if GDB does the printing, there is better error handling if there
267 is a problem with any of the arguments, but using an inferior
268 function lets you have special-purpose printers and sending of
269 output to the same place as compiled-in print functions. */
270
271 static const char dprintf_style_gdb[] = "gdb";
272 static const char dprintf_style_call[] = "call";
273 static const char dprintf_style_agent[] = "agent";
274 static const char *const dprintf_style_enums[] = {
275 dprintf_style_gdb,
276 dprintf_style_call,
277 dprintf_style_agent,
278 NULL
279 };
280 static const char *dprintf_style = dprintf_style_gdb;
281
282 /* The function to use for dynamic printf if the preferred style is to
283 call into the inferior. The value is simply a string that is
284 copied into the command, so it can be anything that GDB can
285 evaluate to a callable address, not necessarily a function name. */
286
287 static char *dprintf_function;
288
289 /* The channel to use for dynamic printf if the preferred style is to
290 call into the inferior; if a nonempty string, it will be passed to
291 the call as the first argument, with the format string as the
292 second. As with the dprintf function, this can be anything that
293 GDB knows how to evaluate, so in addition to common choices like
294 "stderr", this could be an app-specific expression like
295 "mystreams[curlogger]". */
296
297 static char *dprintf_channel;
298
299 /* True if dprintf commands should continue to operate even if GDB
300 has disconnected. */
301 static int disconnected_dprintf = 1;
302
303 struct command_line *
304 breakpoint_commands (struct breakpoint *b)
305 {
306 return b->commands ? b->commands.get () : NULL;
307 }
308
309 /* Flag indicating that a command has proceeded the inferior past the
310 current breakpoint. */
311
312 static int breakpoint_proceeded;
313
314 const char *
315 bpdisp_text (enum bpdisp disp)
316 {
317 /* NOTE: the following values are a part of MI protocol and
318 represent values of 'disp' field returned when inferior stops at
319 a breakpoint. */
320 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
321
322 return bpdisps[(int) disp];
323 }
324
325 /* Prototypes for exported functions. */
326 /* If FALSE, gdb will not use hardware support for watchpoints, even
327 if such is available. */
328 static int can_use_hw_watchpoints;
329
330 static void
331 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
332 struct cmd_list_element *c,
333 const char *value)
334 {
335 fprintf_filtered (file,
336 _("Debugger's willingness to use "
337 "watchpoint hardware is %s.\n"),
338 value);
339 }
340
341 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
342 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
343 for unrecognized breakpoint locations.
344 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
345 static enum auto_boolean pending_break_support;
346 static void
347 show_pending_break_support (struct ui_file *file, int from_tty,
348 struct cmd_list_element *c,
349 const char *value)
350 {
351 fprintf_filtered (file,
352 _("Debugger's behavior regarding "
353 "pending breakpoints is %s.\n"),
354 value);
355 }
356
357 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
358 set with "break" but falling in read-only memory.
359 If 0, gdb will warn about such breakpoints, but won't automatically
360 use hardware breakpoints. */
361 static int automatic_hardware_breakpoints;
362 static void
363 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
364 struct cmd_list_element *c,
365 const char *value)
366 {
367 fprintf_filtered (file,
368 _("Automatic usage of hardware breakpoints is %s.\n"),
369 value);
370 }
371
372 /* If on, GDB keeps breakpoints inserted even if the inferior is
373 stopped, and immediately inserts any new breakpoints as soon as
374 they're created. If off (default), GDB keeps breakpoints off of
375 the target as long as possible. That is, it delays inserting
376 breakpoints until the next resume, and removes them again when the
377 target fully stops. This is a bit safer in case GDB crashes while
378 processing user input. */
379 static int always_inserted_mode = 0;
380
381 static void
382 show_always_inserted_mode (struct ui_file *file, int from_tty,
383 struct cmd_list_element *c, const char *value)
384 {
385 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
386 value);
387 }
388
389 /* See breakpoint.h. */
390
391 int
392 breakpoints_should_be_inserted_now (void)
393 {
394 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
395 {
396 /* If breakpoints are global, they should be inserted even if no
397 thread under gdb's control is running, or even if there are
398 no threads under GDB's control yet. */
399 return 1;
400 }
401 else if (target_has_execution)
402 {
403 struct thread_info *tp;
404
405 if (always_inserted_mode)
406 {
407 /* The user wants breakpoints inserted even if all threads
408 are stopped. */
409 return 1;
410 }
411
412 if (threads_are_executing ())
413 return 1;
414
415 /* Don't remove breakpoints yet if, even though all threads are
416 stopped, we still have events to process. */
417 ALL_NON_EXITED_THREADS (tp)
418 if (tp->resumed
419 && tp->suspend.waitstatus_pending_p)
420 return 1;
421 }
422 return 0;
423 }
424
425 static const char condition_evaluation_both[] = "host or target";
426
427 /* Modes for breakpoint condition evaluation. */
428 static const char condition_evaluation_auto[] = "auto";
429 static const char condition_evaluation_host[] = "host";
430 static const char condition_evaluation_target[] = "target";
431 static const char *const condition_evaluation_enums[] = {
432 condition_evaluation_auto,
433 condition_evaluation_host,
434 condition_evaluation_target,
435 NULL
436 };
437
438 /* Global that holds the current mode for breakpoint condition evaluation. */
439 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
440
441 /* Global that we use to display information to the user (gets its value from
442 condition_evaluation_mode_1. */
443 static const char *condition_evaluation_mode = condition_evaluation_auto;
444
445 /* Translate a condition evaluation mode MODE into either "host"
446 or "target". This is used mostly to translate from "auto" to the
447 real setting that is being used. It returns the translated
448 evaluation mode. */
449
450 static const char *
451 translate_condition_evaluation_mode (const char *mode)
452 {
453 if (mode == condition_evaluation_auto)
454 {
455 if (target_supports_evaluation_of_breakpoint_conditions ())
456 return condition_evaluation_target;
457 else
458 return condition_evaluation_host;
459 }
460 else
461 return mode;
462 }
463
464 /* Discovers what condition_evaluation_auto translates to. */
465
466 static const char *
467 breakpoint_condition_evaluation_mode (void)
468 {
469 return translate_condition_evaluation_mode (condition_evaluation_mode);
470 }
471
472 /* Return true if GDB should evaluate breakpoint conditions or false
473 otherwise. */
474
475 static int
476 gdb_evaluates_breakpoint_condition_p (void)
477 {
478 const char *mode = breakpoint_condition_evaluation_mode ();
479
480 return (mode == condition_evaluation_host);
481 }
482
483 /* Are we executing breakpoint commands? */
484 static int executing_breakpoint_commands;
485
486 /* Are overlay event breakpoints enabled? */
487 static int overlay_events_enabled;
488
489 /* See description in breakpoint.h. */
490 int target_exact_watchpoints = 0;
491
492 /* Walk the following statement or block through all breakpoints.
493 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
494 current breakpoint. */
495
496 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
497
498 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
499 for (B = breakpoint_chain; \
500 B ? (TMP=B->next, 1): 0; \
501 B = TMP)
502
503 /* Similar iterator for the low-level breakpoints. SAFE variant is
504 not provided so update_global_location_list must not be called
505 while executing the block of ALL_BP_LOCATIONS. */
506
507 #define ALL_BP_LOCATIONS(B,BP_TMP) \
508 for (BP_TMP = bp_locations; \
509 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
510 BP_TMP++)
511
512 /* Iterates through locations with address ADDRESS for the currently selected
513 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
514 to where the loop should start from.
515 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
516 appropriate location to start with. */
517
518 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
519 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
520 BP_LOCP_TMP = BP_LOCP_START; \
521 BP_LOCP_START \
522 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
523 && (*BP_LOCP_TMP)->address == ADDRESS); \
524 BP_LOCP_TMP++)
525
526 /* Iterator for tracepoints only. */
527
528 #define ALL_TRACEPOINTS(B) \
529 for (B = breakpoint_chain; B; B = B->next) \
530 if (is_tracepoint (B))
531
532 /* Chains of all breakpoints defined. */
533
534 struct breakpoint *breakpoint_chain;
535
536 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
537
538 static struct bp_location **bp_locations;
539
540 /* Number of elements of BP_LOCATIONS. */
541
542 static unsigned bp_locations_count;
543
544 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
545 ADDRESS for the current elements of BP_LOCATIONS which get a valid
546 result from bp_location_has_shadow. You can use it for roughly
547 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
548 an address you need to read. */
549
550 static CORE_ADDR bp_locations_placed_address_before_address_max;
551
552 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
553 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
554 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
555 You can use it for roughly limiting the subrange of BP_LOCATIONS to
556 scan for shadow bytes for an address you need to read. */
557
558 static CORE_ADDR bp_locations_shadow_len_after_address_max;
559
560 /* The locations that no longer correspond to any breakpoint, unlinked
561 from the bp_locations array, but for which a hit may still be
562 reported by a target. */
563 static std::vector<bp_location *> moribund_locations;
564
565 /* Number of last breakpoint made. */
566
567 static int breakpoint_count;
568
569 /* The value of `breakpoint_count' before the last command that
570 created breakpoints. If the last (break-like) command created more
571 than one breakpoint, then the difference between BREAKPOINT_COUNT
572 and PREV_BREAKPOINT_COUNT is more than one. */
573 static int prev_breakpoint_count;
574
575 /* Number of last tracepoint made. */
576
577 static int tracepoint_count;
578
579 static struct cmd_list_element *breakpoint_set_cmdlist;
580 static struct cmd_list_element *breakpoint_show_cmdlist;
581 struct cmd_list_element *save_cmdlist;
582
583 /* See declaration at breakpoint.h. */
584
585 struct breakpoint *
586 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
587 void *user_data)
588 {
589 struct breakpoint *b = NULL;
590
591 ALL_BREAKPOINTS (b)
592 {
593 if (func (b, user_data) != 0)
594 break;
595 }
596
597 return b;
598 }
599
600 /* Return whether a breakpoint is an active enabled breakpoint. */
601 static int
602 breakpoint_enabled (struct breakpoint *b)
603 {
604 return (b->enable_state == bp_enabled);
605 }
606
607 /* Set breakpoint count to NUM. */
608
609 static void
610 set_breakpoint_count (int num)
611 {
612 prev_breakpoint_count = breakpoint_count;
613 breakpoint_count = num;
614 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
615 }
616
617 /* Used by `start_rbreak_breakpoints' below, to record the current
618 breakpoint count before "rbreak" creates any breakpoint. */
619 static int rbreak_start_breakpoint_count;
620
621 /* Called at the start an "rbreak" command to record the first
622 breakpoint made. */
623
624 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
625 {
626 rbreak_start_breakpoint_count = breakpoint_count;
627 }
628
629 /* Called at the end of an "rbreak" command to record the last
630 breakpoint made. */
631
632 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
633 {
634 prev_breakpoint_count = rbreak_start_breakpoint_count;
635 }
636
637 /* Used in run_command to zero the hit count when a new run starts. */
638
639 void
640 clear_breakpoint_hit_counts (void)
641 {
642 struct breakpoint *b;
643
644 ALL_BREAKPOINTS (b)
645 b->hit_count = 0;
646 }
647
648 \f
649 /* Return the breakpoint with the specified number, or NULL
650 if the number does not refer to an existing breakpoint. */
651
652 struct breakpoint *
653 get_breakpoint (int num)
654 {
655 struct breakpoint *b;
656
657 ALL_BREAKPOINTS (b)
658 if (b->number == num)
659 return b;
660
661 return NULL;
662 }
663
664 \f
665
666 /* Mark locations as "conditions have changed" in case the target supports
667 evaluating conditions on its side. */
668
669 static void
670 mark_breakpoint_modified (struct breakpoint *b)
671 {
672 struct bp_location *loc;
673
674 /* This is only meaningful if the target is
675 evaluating conditions and if the user has
676 opted for condition evaluation on the target's
677 side. */
678 if (gdb_evaluates_breakpoint_condition_p ()
679 || !target_supports_evaluation_of_breakpoint_conditions ())
680 return;
681
682 if (!is_breakpoint (b))
683 return;
684
685 for (loc = b->loc; loc; loc = loc->next)
686 loc->condition_changed = condition_modified;
687 }
688
689 /* Mark location as "conditions have changed" in case the target supports
690 evaluating conditions on its side. */
691
692 static void
693 mark_breakpoint_location_modified (struct bp_location *loc)
694 {
695 /* This is only meaningful if the target is
696 evaluating conditions and if the user has
697 opted for condition evaluation on the target's
698 side. */
699 if (gdb_evaluates_breakpoint_condition_p ()
700 || !target_supports_evaluation_of_breakpoint_conditions ())
701
702 return;
703
704 if (!is_breakpoint (loc->owner))
705 return;
706
707 loc->condition_changed = condition_modified;
708 }
709
710 /* Sets the condition-evaluation mode using the static global
711 condition_evaluation_mode. */
712
713 static void
714 set_condition_evaluation_mode (const char *args, int from_tty,
715 struct cmd_list_element *c)
716 {
717 const char *old_mode, *new_mode;
718
719 if ((condition_evaluation_mode_1 == condition_evaluation_target)
720 && !target_supports_evaluation_of_breakpoint_conditions ())
721 {
722 condition_evaluation_mode_1 = condition_evaluation_mode;
723 warning (_("Target does not support breakpoint condition evaluation.\n"
724 "Using host evaluation mode instead."));
725 return;
726 }
727
728 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
729 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
730
731 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
732 settings was "auto". */
733 condition_evaluation_mode = condition_evaluation_mode_1;
734
735 /* Only update the mode if the user picked a different one. */
736 if (new_mode != old_mode)
737 {
738 struct bp_location *loc, **loc_tmp;
739 /* If the user switched to a different evaluation mode, we
740 need to synch the changes with the target as follows:
741
742 "host" -> "target": Send all (valid) conditions to the target.
743 "target" -> "host": Remove all the conditions from the target.
744 */
745
746 if (new_mode == condition_evaluation_target)
747 {
748 /* Mark everything modified and synch conditions with the
749 target. */
750 ALL_BP_LOCATIONS (loc, loc_tmp)
751 mark_breakpoint_location_modified (loc);
752 }
753 else
754 {
755 /* Manually mark non-duplicate locations to synch conditions
756 with the target. We do this to remove all the conditions the
757 target knows about. */
758 ALL_BP_LOCATIONS (loc, loc_tmp)
759 if (is_breakpoint (loc->owner) && loc->inserted)
760 loc->needs_update = 1;
761 }
762
763 /* Do the update. */
764 update_global_location_list (UGLL_MAY_INSERT);
765 }
766
767 return;
768 }
769
770 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
771 what "auto" is translating to. */
772
773 static void
774 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
775 struct cmd_list_element *c, const char *value)
776 {
777 if (condition_evaluation_mode == condition_evaluation_auto)
778 fprintf_filtered (file,
779 _("Breakpoint condition evaluation "
780 "mode is %s (currently %s).\n"),
781 value,
782 breakpoint_condition_evaluation_mode ());
783 else
784 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
785 value);
786 }
787
788 /* A comparison function for bp_location AP and BP that is used by
789 bsearch. This comparison function only cares about addresses, unlike
790 the more general bp_locations_compare function. */
791
792 static int
793 bp_locations_compare_addrs (const void *ap, const void *bp)
794 {
795 const struct bp_location *a = *(const struct bp_location **) ap;
796 const struct bp_location *b = *(const struct bp_location **) bp;
797
798 if (a->address == b->address)
799 return 0;
800 else
801 return ((a->address > b->address) - (a->address < b->address));
802 }
803
804 /* Helper function to skip all bp_locations with addresses
805 less than ADDRESS. It returns the first bp_location that
806 is greater than or equal to ADDRESS. If none is found, just
807 return NULL. */
808
809 static struct bp_location **
810 get_first_locp_gte_addr (CORE_ADDR address)
811 {
812 struct bp_location dummy_loc;
813 struct bp_location *dummy_locp = &dummy_loc;
814 struct bp_location **locp_found = NULL;
815
816 /* Initialize the dummy location's address field. */
817 dummy_loc.address = address;
818
819 /* Find a close match to the first location at ADDRESS. */
820 locp_found = ((struct bp_location **)
821 bsearch (&dummy_locp, bp_locations, bp_locations_count,
822 sizeof (struct bp_location **),
823 bp_locations_compare_addrs));
824
825 /* Nothing was found, nothing left to do. */
826 if (locp_found == NULL)
827 return NULL;
828
829 /* We may have found a location that is at ADDRESS but is not the first in the
830 location's list. Go backwards (if possible) and locate the first one. */
831 while ((locp_found - 1) >= bp_locations
832 && (*(locp_found - 1))->address == address)
833 locp_found--;
834
835 return locp_found;
836 }
837
838 void
839 set_breakpoint_condition (struct breakpoint *b, const char *exp,
840 int from_tty)
841 {
842 xfree (b->cond_string);
843 b->cond_string = NULL;
844
845 if (is_watchpoint (b))
846 {
847 struct watchpoint *w = (struct watchpoint *) b;
848
849 w->cond_exp.reset ();
850 }
851 else
852 {
853 struct bp_location *loc;
854
855 for (loc = b->loc; loc; loc = loc->next)
856 {
857 loc->cond.reset ();
858
859 /* No need to free the condition agent expression
860 bytecode (if we have one). We will handle this
861 when we go through update_global_location_list. */
862 }
863 }
864
865 if (*exp == 0)
866 {
867 if (from_tty)
868 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
869 }
870 else
871 {
872 const char *arg = exp;
873
874 /* I don't know if it matters whether this is the string the user
875 typed in or the decompiled expression. */
876 b->cond_string = xstrdup (arg);
877 b->condition_not_parsed = 0;
878
879 if (is_watchpoint (b))
880 {
881 struct watchpoint *w = (struct watchpoint *) b;
882
883 innermost_block.reset ();
884 arg = exp;
885 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
886 if (*arg)
887 error (_("Junk at end of expression"));
888 w->cond_exp_valid_block = innermost_block.block ();
889 }
890 else
891 {
892 struct bp_location *loc;
893
894 for (loc = b->loc; loc; loc = loc->next)
895 {
896 arg = exp;
897 loc->cond =
898 parse_exp_1 (&arg, loc->address,
899 block_for_pc (loc->address), 0);
900 if (*arg)
901 error (_("Junk at end of expression"));
902 }
903 }
904 }
905 mark_breakpoint_modified (b);
906
907 gdb::observers::breakpoint_modified.notify (b);
908 }
909
910 /* Completion for the "condition" command. */
911
912 static void
913 condition_completer (struct cmd_list_element *cmd,
914 completion_tracker &tracker,
915 const char *text, const char *word)
916 {
917 const char *space;
918
919 text = skip_spaces (text);
920 space = skip_to_space (text);
921 if (*space == '\0')
922 {
923 int len;
924 struct breakpoint *b;
925
926 if (text[0] == '$')
927 {
928 /* We don't support completion of history indices. */
929 if (!isdigit (text[1]))
930 complete_internalvar (tracker, &text[1]);
931 return;
932 }
933
934 /* We're completing the breakpoint number. */
935 len = strlen (text);
936
937 ALL_BREAKPOINTS (b)
938 {
939 char number[50];
940
941 xsnprintf (number, sizeof (number), "%d", b->number);
942
943 if (strncmp (number, text, len) == 0)
944 {
945 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
946 tracker.add_completion (std::move (copy));
947 }
948 }
949
950 return;
951 }
952
953 /* We're completing the expression part. */
954 text = skip_spaces (space);
955 expression_completer (cmd, tracker, text, word);
956 }
957
958 /* condition N EXP -- set break condition of breakpoint N to EXP. */
959
960 static void
961 condition_command (const char *arg, int from_tty)
962 {
963 struct breakpoint *b;
964 const char *p;
965 int bnum;
966
967 if (arg == 0)
968 error_no_arg (_("breakpoint number"));
969
970 p = arg;
971 bnum = get_number (&p);
972 if (bnum == 0)
973 error (_("Bad breakpoint argument: '%s'"), arg);
974
975 ALL_BREAKPOINTS (b)
976 if (b->number == bnum)
977 {
978 /* Check if this breakpoint has a "stop" method implemented in an
979 extension language. This method and conditions entered into GDB
980 from the CLI are mutually exclusive. */
981 const struct extension_language_defn *extlang
982 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
983
984 if (extlang != NULL)
985 {
986 error (_("Only one stop condition allowed. There is currently"
987 " a %s stop condition defined for this breakpoint."),
988 ext_lang_capitalized_name (extlang));
989 }
990 set_breakpoint_condition (b, p, from_tty);
991
992 if (is_breakpoint (b))
993 update_global_location_list (UGLL_MAY_INSERT);
994
995 return;
996 }
997
998 error (_("No breakpoint number %d."), bnum);
999 }
1000
1001 /* Check that COMMAND do not contain commands that are suitable
1002 only for tracepoints and not suitable for ordinary breakpoints.
1003 Throw if any such commands is found. */
1004
1005 static void
1006 check_no_tracepoint_commands (struct command_line *commands)
1007 {
1008 struct command_line *c;
1009
1010 for (c = commands; c; c = c->next)
1011 {
1012 if (c->control_type == while_stepping_control)
1013 error (_("The 'while-stepping' command can "
1014 "only be used for tracepoints"));
1015
1016 check_no_tracepoint_commands (c->body_list_0.get ());
1017 check_no_tracepoint_commands (c->body_list_1.get ());
1018
1019 /* Not that command parsing removes leading whitespace and comment
1020 lines and also empty lines. So, we only need to check for
1021 command directly. */
1022 if (strstr (c->line, "collect ") == c->line)
1023 error (_("The 'collect' command can only be used for tracepoints"));
1024
1025 if (strstr (c->line, "teval ") == c->line)
1026 error (_("The 'teval' command can only be used for tracepoints"));
1027 }
1028 }
1029
1030 struct longjmp_breakpoint : public breakpoint
1031 {
1032 ~longjmp_breakpoint () override;
1033 };
1034
1035 /* Encapsulate tests for different types of tracepoints. */
1036
1037 static bool
1038 is_tracepoint_type (bptype type)
1039 {
1040 return (type == bp_tracepoint
1041 || type == bp_fast_tracepoint
1042 || type == bp_static_tracepoint);
1043 }
1044
1045 static bool
1046 is_longjmp_type (bptype type)
1047 {
1048 return type == bp_longjmp || type == bp_exception;
1049 }
1050
1051 int
1052 is_tracepoint (const struct breakpoint *b)
1053 {
1054 return is_tracepoint_type (b->type);
1055 }
1056
1057 /* Factory function to create an appropriate instance of breakpoint given
1058 TYPE. */
1059
1060 static std::unique_ptr<breakpoint>
1061 new_breakpoint_from_type (bptype type)
1062 {
1063 breakpoint *b;
1064
1065 if (is_tracepoint_type (type))
1066 b = new tracepoint ();
1067 else if (is_longjmp_type (type))
1068 b = new longjmp_breakpoint ();
1069 else
1070 b = new breakpoint ();
1071
1072 return std::unique_ptr<breakpoint> (b);
1073 }
1074
1075 /* A helper function that validates that COMMANDS are valid for a
1076 breakpoint. This function will throw an exception if a problem is
1077 found. */
1078
1079 static void
1080 validate_commands_for_breakpoint (struct breakpoint *b,
1081 struct command_line *commands)
1082 {
1083 if (is_tracepoint (b))
1084 {
1085 struct tracepoint *t = (struct tracepoint *) b;
1086 struct command_line *c;
1087 struct command_line *while_stepping = 0;
1088
1089 /* Reset the while-stepping step count. The previous commands
1090 might have included a while-stepping action, while the new
1091 ones might not. */
1092 t->step_count = 0;
1093
1094 /* We need to verify that each top-level element of commands is
1095 valid for tracepoints, that there's at most one
1096 while-stepping element, and that the while-stepping's body
1097 has valid tracing commands excluding nested while-stepping.
1098 We also need to validate the tracepoint action line in the
1099 context of the tracepoint --- validate_actionline actually
1100 has side effects, like setting the tracepoint's
1101 while-stepping STEP_COUNT, in addition to checking if the
1102 collect/teval actions parse and make sense in the
1103 tracepoint's context. */
1104 for (c = commands; c; c = c->next)
1105 {
1106 if (c->control_type == while_stepping_control)
1107 {
1108 if (b->type == bp_fast_tracepoint)
1109 error (_("The 'while-stepping' command "
1110 "cannot be used for fast tracepoint"));
1111 else if (b->type == bp_static_tracepoint)
1112 error (_("The 'while-stepping' command "
1113 "cannot be used for static tracepoint"));
1114
1115 if (while_stepping)
1116 error (_("The 'while-stepping' command "
1117 "can be used only once"));
1118 else
1119 while_stepping = c;
1120 }
1121
1122 validate_actionline (c->line, b);
1123 }
1124 if (while_stepping)
1125 {
1126 struct command_line *c2;
1127
1128 gdb_assert (while_stepping->body_list_1 == nullptr);
1129 c2 = while_stepping->body_list_0.get ();
1130 for (; c2; c2 = c2->next)
1131 {
1132 if (c2->control_type == while_stepping_control)
1133 error (_("The 'while-stepping' command cannot be nested"));
1134 }
1135 }
1136 }
1137 else
1138 {
1139 check_no_tracepoint_commands (commands);
1140 }
1141 }
1142
1143 /* Return a vector of all the static tracepoints set at ADDR. The
1144 caller is responsible for releasing the vector. */
1145
1146 std::vector<breakpoint *>
1147 static_tracepoints_here (CORE_ADDR addr)
1148 {
1149 struct breakpoint *b;
1150 std::vector<breakpoint *> found;
1151 struct bp_location *loc;
1152
1153 ALL_BREAKPOINTS (b)
1154 if (b->type == bp_static_tracepoint)
1155 {
1156 for (loc = b->loc; loc; loc = loc->next)
1157 if (loc->address == addr)
1158 found.push_back (b);
1159 }
1160
1161 return found;
1162 }
1163
1164 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1165 validate that only allowed commands are included. */
1166
1167 void
1168 breakpoint_set_commands (struct breakpoint *b,
1169 counted_command_line &&commands)
1170 {
1171 validate_commands_for_breakpoint (b, commands.get ());
1172
1173 b->commands = std::move (commands);
1174 gdb::observers::breakpoint_modified.notify (b);
1175 }
1176
1177 /* Set the internal `silent' flag on the breakpoint. Note that this
1178 is not the same as the "silent" that may appear in the breakpoint's
1179 commands. */
1180
1181 void
1182 breakpoint_set_silent (struct breakpoint *b, int silent)
1183 {
1184 int old_silent = b->silent;
1185
1186 b->silent = silent;
1187 if (old_silent != silent)
1188 gdb::observers::breakpoint_modified.notify (b);
1189 }
1190
1191 /* Set the thread for this breakpoint. If THREAD is -1, make the
1192 breakpoint work for any thread. */
1193
1194 void
1195 breakpoint_set_thread (struct breakpoint *b, int thread)
1196 {
1197 int old_thread = b->thread;
1198
1199 b->thread = thread;
1200 if (old_thread != thread)
1201 gdb::observers::breakpoint_modified.notify (b);
1202 }
1203
1204 /* Set the task for this breakpoint. If TASK is 0, make the
1205 breakpoint work for any task. */
1206
1207 void
1208 breakpoint_set_task (struct breakpoint *b, int task)
1209 {
1210 int old_task = b->task;
1211
1212 b->task = task;
1213 if (old_task != task)
1214 gdb::observers::breakpoint_modified.notify (b);
1215 }
1216
1217 static void
1218 commands_command_1 (const char *arg, int from_tty,
1219 struct command_line *control)
1220 {
1221 counted_command_line cmd;
1222
1223 std::string new_arg;
1224
1225 if (arg == NULL || !*arg)
1226 {
1227 if (breakpoint_count - prev_breakpoint_count > 1)
1228 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1229 breakpoint_count);
1230 else if (breakpoint_count > 0)
1231 new_arg = string_printf ("%d", breakpoint_count);
1232 arg = new_arg.c_str ();
1233 }
1234
1235 map_breakpoint_numbers
1236 (arg, [&] (breakpoint *b)
1237 {
1238 if (cmd == NULL)
1239 {
1240 if (control != NULL)
1241 cmd = control->body_list_0;
1242 else
1243 {
1244 std::string str
1245 = string_printf (_("Type commands for breakpoint(s) "
1246 "%s, one per line."),
1247 arg);
1248
1249 auto do_validate = [=] (const char *line)
1250 {
1251 validate_actionline (line, b);
1252 };
1253 gdb::function_view<void (const char *)> validator;
1254 if (is_tracepoint (b))
1255 validator = do_validate;
1256
1257 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1258 }
1259 }
1260
1261 /* If a breakpoint was on the list more than once, we don't need to
1262 do anything. */
1263 if (b->commands != cmd)
1264 {
1265 validate_commands_for_breakpoint (b, cmd.get ());
1266 b->commands = cmd;
1267 gdb::observers::breakpoint_modified.notify (b);
1268 }
1269 });
1270 }
1271
1272 static void
1273 commands_command (const char *arg, int from_tty)
1274 {
1275 commands_command_1 (arg, from_tty, NULL);
1276 }
1277
1278 /* Like commands_command, but instead of reading the commands from
1279 input stream, takes them from an already parsed command structure.
1280
1281 This is used by cli-script.c to DTRT with breakpoint commands
1282 that are part of if and while bodies. */
1283 enum command_control_type
1284 commands_from_control_command (const char *arg, struct command_line *cmd)
1285 {
1286 commands_command_1 (arg, 0, cmd);
1287 return simple_control;
1288 }
1289
1290 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1291
1292 static int
1293 bp_location_has_shadow (struct bp_location *bl)
1294 {
1295 if (bl->loc_type != bp_loc_software_breakpoint)
1296 return 0;
1297 if (!bl->inserted)
1298 return 0;
1299 if (bl->target_info.shadow_len == 0)
1300 /* BL isn't valid, or doesn't shadow memory. */
1301 return 0;
1302 return 1;
1303 }
1304
1305 /* Update BUF, which is LEN bytes read from the target address
1306 MEMADDR, by replacing a memory breakpoint with its shadowed
1307 contents.
1308
1309 If READBUF is not NULL, this buffer must not overlap with the of
1310 the breakpoint location's shadow_contents buffer. Otherwise, a
1311 failed assertion internal error will be raised. */
1312
1313 static void
1314 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1315 const gdb_byte *writebuf_org,
1316 ULONGEST memaddr, LONGEST len,
1317 struct bp_target_info *target_info,
1318 struct gdbarch *gdbarch)
1319 {
1320 /* Now do full processing of the found relevant range of elements. */
1321 CORE_ADDR bp_addr = 0;
1322 int bp_size = 0;
1323 int bptoffset = 0;
1324
1325 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1326 current_program_space->aspace, 0))
1327 {
1328 /* The breakpoint is inserted in a different address space. */
1329 return;
1330 }
1331
1332 /* Addresses and length of the part of the breakpoint that
1333 we need to copy. */
1334 bp_addr = target_info->placed_address;
1335 bp_size = target_info->shadow_len;
1336
1337 if (bp_addr + bp_size <= memaddr)
1338 {
1339 /* The breakpoint is entirely before the chunk of memory we are
1340 reading. */
1341 return;
1342 }
1343
1344 if (bp_addr >= memaddr + len)
1345 {
1346 /* The breakpoint is entirely after the chunk of memory we are
1347 reading. */
1348 return;
1349 }
1350
1351 /* Offset within shadow_contents. */
1352 if (bp_addr < memaddr)
1353 {
1354 /* Only copy the second part of the breakpoint. */
1355 bp_size -= memaddr - bp_addr;
1356 bptoffset = memaddr - bp_addr;
1357 bp_addr = memaddr;
1358 }
1359
1360 if (bp_addr + bp_size > memaddr + len)
1361 {
1362 /* Only copy the first part of the breakpoint. */
1363 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1364 }
1365
1366 if (readbuf != NULL)
1367 {
1368 /* Verify that the readbuf buffer does not overlap with the
1369 shadow_contents buffer. */
1370 gdb_assert (target_info->shadow_contents >= readbuf + len
1371 || readbuf >= (target_info->shadow_contents
1372 + target_info->shadow_len));
1373
1374 /* Update the read buffer with this inserted breakpoint's
1375 shadow. */
1376 memcpy (readbuf + bp_addr - memaddr,
1377 target_info->shadow_contents + bptoffset, bp_size);
1378 }
1379 else
1380 {
1381 const unsigned char *bp;
1382 CORE_ADDR addr = target_info->reqstd_address;
1383 int placed_size;
1384
1385 /* Update the shadow with what we want to write to memory. */
1386 memcpy (target_info->shadow_contents + bptoffset,
1387 writebuf_org + bp_addr - memaddr, bp_size);
1388
1389 /* Determine appropriate breakpoint contents and size for this
1390 address. */
1391 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1392
1393 /* Update the final write buffer with this inserted
1394 breakpoint's INSN. */
1395 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1396 }
1397 }
1398
1399 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1400 by replacing any memory breakpoints with their shadowed contents.
1401
1402 If READBUF is not NULL, this buffer must not overlap with any of
1403 the breakpoint location's shadow_contents buffers. Otherwise,
1404 a failed assertion internal error will be raised.
1405
1406 The range of shadowed area by each bp_location is:
1407 bl->address - bp_locations_placed_address_before_address_max
1408 up to bl->address + bp_locations_shadow_len_after_address_max
1409 The range we were requested to resolve shadows for is:
1410 memaddr ... memaddr + len
1411 Thus the safe cutoff boundaries for performance optimization are
1412 memaddr + len <= (bl->address
1413 - bp_locations_placed_address_before_address_max)
1414 and:
1415 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1416
1417 void
1418 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1419 const gdb_byte *writebuf_org,
1420 ULONGEST memaddr, LONGEST len)
1421 {
1422 /* Left boundary, right boundary and median element of our binary
1423 search. */
1424 unsigned bc_l, bc_r, bc;
1425
1426 /* Find BC_L which is a leftmost element which may affect BUF
1427 content. It is safe to report lower value but a failure to
1428 report higher one. */
1429
1430 bc_l = 0;
1431 bc_r = bp_locations_count;
1432 while (bc_l + 1 < bc_r)
1433 {
1434 struct bp_location *bl;
1435
1436 bc = (bc_l + bc_r) / 2;
1437 bl = bp_locations[bc];
1438
1439 /* Check first BL->ADDRESS will not overflow due to the added
1440 constant. Then advance the left boundary only if we are sure
1441 the BC element can in no way affect the BUF content (MEMADDR
1442 to MEMADDR + LEN range).
1443
1444 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1445 offset so that we cannot miss a breakpoint with its shadow
1446 range tail still reaching MEMADDR. */
1447
1448 if ((bl->address + bp_locations_shadow_len_after_address_max
1449 >= bl->address)
1450 && (bl->address + bp_locations_shadow_len_after_address_max
1451 <= memaddr))
1452 bc_l = bc;
1453 else
1454 bc_r = bc;
1455 }
1456
1457 /* Due to the binary search above, we need to make sure we pick the
1458 first location that's at BC_L's address. E.g., if there are
1459 multiple locations at the same address, BC_L may end up pointing
1460 at a duplicate location, and miss the "master"/"inserted"
1461 location. Say, given locations L1, L2 and L3 at addresses A and
1462 B:
1463
1464 L1@A, L2@A, L3@B, ...
1465
1466 BC_L could end up pointing at location L2, while the "master"
1467 location could be L1. Since the `loc->inserted' flag is only set
1468 on "master" locations, we'd forget to restore the shadow of L1
1469 and L2. */
1470 while (bc_l > 0
1471 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1472 bc_l--;
1473
1474 /* Now do full processing of the found relevant range of elements. */
1475
1476 for (bc = bc_l; bc < bp_locations_count; bc++)
1477 {
1478 struct bp_location *bl = bp_locations[bc];
1479
1480 /* bp_location array has BL->OWNER always non-NULL. */
1481 if (bl->owner->type == bp_none)
1482 warning (_("reading through apparently deleted breakpoint #%d?"),
1483 bl->owner->number);
1484
1485 /* Performance optimization: any further element can no longer affect BUF
1486 content. */
1487
1488 if (bl->address >= bp_locations_placed_address_before_address_max
1489 && memaddr + len <= (bl->address
1490 - bp_locations_placed_address_before_address_max))
1491 break;
1492
1493 if (!bp_location_has_shadow (bl))
1494 continue;
1495
1496 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1497 memaddr, len, &bl->target_info, bl->gdbarch);
1498 }
1499 }
1500
1501 \f
1502
1503 /* Return true if BPT is either a software breakpoint or a hardware
1504 breakpoint. */
1505
1506 int
1507 is_breakpoint (const struct breakpoint *bpt)
1508 {
1509 return (bpt->type == bp_breakpoint
1510 || bpt->type == bp_hardware_breakpoint
1511 || bpt->type == bp_dprintf);
1512 }
1513
1514 /* Return true if BPT is of any hardware watchpoint kind. */
1515
1516 static int
1517 is_hardware_watchpoint (const struct breakpoint *bpt)
1518 {
1519 return (bpt->type == bp_hardware_watchpoint
1520 || bpt->type == bp_read_watchpoint
1521 || bpt->type == bp_access_watchpoint);
1522 }
1523
1524 /* Return true if BPT is of any watchpoint kind, hardware or
1525 software. */
1526
1527 int
1528 is_watchpoint (const struct breakpoint *bpt)
1529 {
1530 return (is_hardware_watchpoint (bpt)
1531 || bpt->type == bp_watchpoint);
1532 }
1533
1534 /* Returns true if the current thread and its running state are safe
1535 to evaluate or update watchpoint B. Watchpoints on local
1536 expressions need to be evaluated in the context of the thread that
1537 was current when the watchpoint was created, and, that thread needs
1538 to be stopped to be able to select the correct frame context.
1539 Watchpoints on global expressions can be evaluated on any thread,
1540 and in any state. It is presently left to the target allowing
1541 memory accesses when threads are running. */
1542
1543 static int
1544 watchpoint_in_thread_scope (struct watchpoint *b)
1545 {
1546 return (b->pspace == current_program_space
1547 && (b->watchpoint_thread == null_ptid
1548 || (inferior_ptid == b->watchpoint_thread
1549 && !inferior_thread ()->executing)));
1550 }
1551
1552 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1553 associated bp_watchpoint_scope breakpoint. */
1554
1555 static void
1556 watchpoint_del_at_next_stop (struct watchpoint *w)
1557 {
1558 if (w->related_breakpoint != w)
1559 {
1560 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1561 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1562 w->related_breakpoint->disposition = disp_del_at_next_stop;
1563 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1564 w->related_breakpoint = w;
1565 }
1566 w->disposition = disp_del_at_next_stop;
1567 }
1568
1569 /* Extract a bitfield value from value VAL using the bit parameters contained in
1570 watchpoint W. */
1571
1572 static struct value *
1573 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1574 {
1575 struct value *bit_val;
1576
1577 if (val == NULL)
1578 return NULL;
1579
1580 bit_val = allocate_value (value_type (val));
1581
1582 unpack_value_bitfield (bit_val,
1583 w->val_bitpos,
1584 w->val_bitsize,
1585 value_contents_for_printing (val),
1586 value_offset (val),
1587 val);
1588
1589 return bit_val;
1590 }
1591
1592 /* Allocate a dummy location and add it to B, which must be a software
1593 watchpoint. This is required because even if a software watchpoint
1594 is not watching any memory, bpstat_stop_status requires a location
1595 to be able to report stops. */
1596
1597 static void
1598 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1599 struct program_space *pspace)
1600 {
1601 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1602
1603 b->loc = allocate_bp_location (b);
1604 b->loc->pspace = pspace;
1605 b->loc->address = -1;
1606 b->loc->length = -1;
1607 }
1608
1609 /* Returns true if B is a software watchpoint that is not watching any
1610 memory (e.g., "watch $pc"). */
1611
1612 static int
1613 is_no_memory_software_watchpoint (struct breakpoint *b)
1614 {
1615 return (b->type == bp_watchpoint
1616 && b->loc != NULL
1617 && b->loc->next == NULL
1618 && b->loc->address == -1
1619 && b->loc->length == -1);
1620 }
1621
1622 /* Assuming that B is a watchpoint:
1623 - Reparse watchpoint expression, if REPARSE is non-zero
1624 - Evaluate expression and store the result in B->val
1625 - Evaluate the condition if there is one, and store the result
1626 in b->loc->cond.
1627 - Update the list of values that must be watched in B->loc.
1628
1629 If the watchpoint disposition is disp_del_at_next_stop, then do
1630 nothing. If this is local watchpoint that is out of scope, delete
1631 it.
1632
1633 Even with `set breakpoint always-inserted on' the watchpoints are
1634 removed + inserted on each stop here. Normal breakpoints must
1635 never be removed because they might be missed by a running thread
1636 when debugging in non-stop mode. On the other hand, hardware
1637 watchpoints (is_hardware_watchpoint; processed here) are specific
1638 to each LWP since they are stored in each LWP's hardware debug
1639 registers. Therefore, such LWP must be stopped first in order to
1640 be able to modify its hardware watchpoints.
1641
1642 Hardware watchpoints must be reset exactly once after being
1643 presented to the user. It cannot be done sooner, because it would
1644 reset the data used to present the watchpoint hit to the user. And
1645 it must not be done later because it could display the same single
1646 watchpoint hit during multiple GDB stops. Note that the latter is
1647 relevant only to the hardware watchpoint types bp_read_watchpoint
1648 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1649 not user-visible - its hit is suppressed if the memory content has
1650 not changed.
1651
1652 The following constraints influence the location where we can reset
1653 hardware watchpoints:
1654
1655 * target_stopped_by_watchpoint and target_stopped_data_address are
1656 called several times when GDB stops.
1657
1658 [linux]
1659 * Multiple hardware watchpoints can be hit at the same time,
1660 causing GDB to stop. GDB only presents one hardware watchpoint
1661 hit at a time as the reason for stopping, and all the other hits
1662 are presented later, one after the other, each time the user
1663 requests the execution to be resumed. Execution is not resumed
1664 for the threads still having pending hit event stored in
1665 LWP_INFO->STATUS. While the watchpoint is already removed from
1666 the inferior on the first stop the thread hit event is kept being
1667 reported from its cached value by linux_nat_stopped_data_address
1668 until the real thread resume happens after the watchpoint gets
1669 presented and thus its LWP_INFO->STATUS gets reset.
1670
1671 Therefore the hardware watchpoint hit can get safely reset on the
1672 watchpoint removal from inferior. */
1673
1674 static void
1675 update_watchpoint (struct watchpoint *b, int reparse)
1676 {
1677 int within_current_scope;
1678 struct frame_id saved_frame_id;
1679 int frame_saved;
1680
1681 /* If this is a local watchpoint, we only want to check if the
1682 watchpoint frame is in scope if the current thread is the thread
1683 that was used to create the watchpoint. */
1684 if (!watchpoint_in_thread_scope (b))
1685 return;
1686
1687 if (b->disposition == disp_del_at_next_stop)
1688 return;
1689
1690 frame_saved = 0;
1691
1692 /* Determine if the watchpoint is within scope. */
1693 if (b->exp_valid_block == NULL)
1694 within_current_scope = 1;
1695 else
1696 {
1697 struct frame_info *fi = get_current_frame ();
1698 struct gdbarch *frame_arch = get_frame_arch (fi);
1699 CORE_ADDR frame_pc = get_frame_pc (fi);
1700
1701 /* If we're at a point where the stack has been destroyed
1702 (e.g. in a function epilogue), unwinding may not work
1703 properly. Do not attempt to recreate locations at this
1704 point. See similar comments in watchpoint_check. */
1705 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1706 return;
1707
1708 /* Save the current frame's ID so we can restore it after
1709 evaluating the watchpoint expression on its own frame. */
1710 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1711 took a frame parameter, so that we didn't have to change the
1712 selected frame. */
1713 frame_saved = 1;
1714 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1715
1716 fi = frame_find_by_id (b->watchpoint_frame);
1717 within_current_scope = (fi != NULL);
1718 if (within_current_scope)
1719 select_frame (fi);
1720 }
1721
1722 /* We don't free locations. They are stored in the bp_location array
1723 and update_global_location_list will eventually delete them and
1724 remove breakpoints if needed. */
1725 b->loc = NULL;
1726
1727 if (within_current_scope && reparse)
1728 {
1729 const char *s;
1730
1731 b->exp.reset ();
1732 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1733 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1734 /* If the meaning of expression itself changed, the old value is
1735 no longer relevant. We don't want to report a watchpoint hit
1736 to the user when the old value and the new value may actually
1737 be completely different objects. */
1738 b->val = NULL;
1739 b->val_valid = 0;
1740
1741 /* Note that unlike with breakpoints, the watchpoint's condition
1742 expression is stored in the breakpoint object, not in the
1743 locations (re)created below. */
1744 if (b->cond_string != NULL)
1745 {
1746 b->cond_exp.reset ();
1747
1748 s = b->cond_string;
1749 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1750 }
1751 }
1752
1753 /* If we failed to parse the expression, for example because
1754 it refers to a global variable in a not-yet-loaded shared library,
1755 don't try to insert watchpoint. We don't automatically delete
1756 such watchpoint, though, since failure to parse expression
1757 is different from out-of-scope watchpoint. */
1758 if (!target_has_execution)
1759 {
1760 /* Without execution, memory can't change. No use to try and
1761 set watchpoint locations. The watchpoint will be reset when
1762 the target gains execution, through breakpoint_re_set. */
1763 if (!can_use_hw_watchpoints)
1764 {
1765 if (b->ops->works_in_software_mode (b))
1766 b->type = bp_watchpoint;
1767 else
1768 error (_("Can't set read/access watchpoint when "
1769 "hardware watchpoints are disabled."));
1770 }
1771 }
1772 else if (within_current_scope && b->exp)
1773 {
1774 int pc = 0;
1775 std::vector<value_ref_ptr> val_chain;
1776 struct value *v, *result;
1777 struct program_space *frame_pspace;
1778
1779 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1780
1781 /* Avoid setting b->val if it's already set. The meaning of
1782 b->val is 'the last value' user saw, and we should update
1783 it only if we reported that last value to user. As it
1784 happens, the code that reports it updates b->val directly.
1785 We don't keep track of the memory value for masked
1786 watchpoints. */
1787 if (!b->val_valid && !is_masked_watchpoint (b))
1788 {
1789 if (b->val_bitsize != 0)
1790 v = extract_bitfield_from_watchpoint_value (b, v);
1791 b->val = release_value (v);
1792 b->val_valid = 1;
1793 }
1794
1795 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1796
1797 /* Look at each value on the value chain. */
1798 gdb_assert (!val_chain.empty ());
1799 for (const value_ref_ptr &iter : val_chain)
1800 {
1801 v = iter.get ();
1802
1803 /* If it's a memory location, and GDB actually needed
1804 its contents to evaluate the expression, then we
1805 must watch it. If the first value returned is
1806 still lazy, that means an error occurred reading it;
1807 watch it anyway in case it becomes readable. */
1808 if (VALUE_LVAL (v) == lval_memory
1809 && (v == val_chain[0] || ! value_lazy (v)))
1810 {
1811 struct type *vtype = check_typedef (value_type (v));
1812
1813 /* We only watch structs and arrays if user asked
1814 for it explicitly, never if they just happen to
1815 appear in the middle of some value chain. */
1816 if (v == result
1817 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1818 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1819 {
1820 CORE_ADDR addr;
1821 enum target_hw_bp_type type;
1822 struct bp_location *loc, **tmp;
1823 int bitpos = 0, bitsize = 0;
1824
1825 if (value_bitsize (v) != 0)
1826 {
1827 /* Extract the bit parameters out from the bitfield
1828 sub-expression. */
1829 bitpos = value_bitpos (v);
1830 bitsize = value_bitsize (v);
1831 }
1832 else if (v == result && b->val_bitsize != 0)
1833 {
1834 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1835 lvalue whose bit parameters are saved in the fields
1836 VAL_BITPOS and VAL_BITSIZE. */
1837 bitpos = b->val_bitpos;
1838 bitsize = b->val_bitsize;
1839 }
1840
1841 addr = value_address (v);
1842 if (bitsize != 0)
1843 {
1844 /* Skip the bytes that don't contain the bitfield. */
1845 addr += bitpos / 8;
1846 }
1847
1848 type = hw_write;
1849 if (b->type == bp_read_watchpoint)
1850 type = hw_read;
1851 else if (b->type == bp_access_watchpoint)
1852 type = hw_access;
1853
1854 loc = allocate_bp_location (b);
1855 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
1856 ;
1857 *tmp = loc;
1858 loc->gdbarch = get_type_arch (value_type (v));
1859
1860 loc->pspace = frame_pspace;
1861 loc->address = address_significant (loc->gdbarch, addr);
1862
1863 if (bitsize != 0)
1864 {
1865 /* Just cover the bytes that make up the bitfield. */
1866 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1867 }
1868 else
1869 loc->length = TYPE_LENGTH (value_type (v));
1870
1871 loc->watchpoint_type = type;
1872 }
1873 }
1874 }
1875
1876 /* Change the type of breakpoint between hardware assisted or
1877 an ordinary watchpoint depending on the hardware support
1878 and free hardware slots. REPARSE is set when the inferior
1879 is started. */
1880 if (reparse)
1881 {
1882 int reg_cnt;
1883 enum bp_loc_type loc_type;
1884 struct bp_location *bl;
1885
1886 reg_cnt = can_use_hardware_watchpoint (val_chain);
1887
1888 if (reg_cnt)
1889 {
1890 int i, target_resources_ok, other_type_used;
1891 enum bptype type;
1892
1893 /* Use an exact watchpoint when there's only one memory region to be
1894 watched, and only one debug register is needed to watch it. */
1895 b->exact = target_exact_watchpoints && reg_cnt == 1;
1896
1897 /* We need to determine how many resources are already
1898 used for all other hardware watchpoints plus this one
1899 to see if we still have enough resources to also fit
1900 this watchpoint in as well. */
1901
1902 /* If this is a software watchpoint, we try to turn it
1903 to a hardware one -- count resources as if B was of
1904 hardware watchpoint type. */
1905 type = b->type;
1906 if (type == bp_watchpoint)
1907 type = bp_hardware_watchpoint;
1908
1909 /* This watchpoint may or may not have been placed on
1910 the list yet at this point (it won't be in the list
1911 if we're trying to create it for the first time,
1912 through watch_command), so always account for it
1913 manually. */
1914
1915 /* Count resources used by all watchpoints except B. */
1916 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
1917
1918 /* Add in the resources needed for B. */
1919 i += hw_watchpoint_use_count (b);
1920
1921 target_resources_ok
1922 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1923 if (target_resources_ok <= 0)
1924 {
1925 int sw_mode = b->ops->works_in_software_mode (b);
1926
1927 if (target_resources_ok == 0 && !sw_mode)
1928 error (_("Target does not support this type of "
1929 "hardware watchpoint."));
1930 else if (target_resources_ok < 0 && !sw_mode)
1931 error (_("There are not enough available hardware "
1932 "resources for this watchpoint."));
1933
1934 /* Downgrade to software watchpoint. */
1935 b->type = bp_watchpoint;
1936 }
1937 else
1938 {
1939 /* If this was a software watchpoint, we've just
1940 found we have enough resources to turn it to a
1941 hardware watchpoint. Otherwise, this is a
1942 nop. */
1943 b->type = type;
1944 }
1945 }
1946 else if (!b->ops->works_in_software_mode (b))
1947 {
1948 if (!can_use_hw_watchpoints)
1949 error (_("Can't set read/access watchpoint when "
1950 "hardware watchpoints are disabled."));
1951 else
1952 error (_("Expression cannot be implemented with "
1953 "read/access watchpoint."));
1954 }
1955 else
1956 b->type = bp_watchpoint;
1957
1958 loc_type = (b->type == bp_watchpoint? bp_loc_other
1959 : bp_loc_hardware_watchpoint);
1960 for (bl = b->loc; bl; bl = bl->next)
1961 bl->loc_type = loc_type;
1962 }
1963
1964 /* If a software watchpoint is not watching any memory, then the
1965 above left it without any location set up. But,
1966 bpstat_stop_status requires a location to be able to report
1967 stops, so make sure there's at least a dummy one. */
1968 if (b->type == bp_watchpoint && b->loc == NULL)
1969 software_watchpoint_add_no_memory_location (b, frame_pspace);
1970 }
1971 else if (!within_current_scope)
1972 {
1973 printf_filtered (_("\
1974 Watchpoint %d deleted because the program has left the block\n\
1975 in which its expression is valid.\n"),
1976 b->number);
1977 watchpoint_del_at_next_stop (b);
1978 }
1979
1980 /* Restore the selected frame. */
1981 if (frame_saved)
1982 select_frame (frame_find_by_id (saved_frame_id));
1983 }
1984
1985
1986 /* Returns 1 iff breakpoint location should be
1987 inserted in the inferior. We don't differentiate the type of BL's owner
1988 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1989 breakpoint_ops is not defined, because in insert_bp_location,
1990 tracepoint's insert_location will not be called. */
1991 static int
1992 should_be_inserted (struct bp_location *bl)
1993 {
1994 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1995 return 0;
1996
1997 if (bl->owner->disposition == disp_del_at_next_stop)
1998 return 0;
1999
2000 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2001 return 0;
2002
2003 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2004 return 0;
2005
2006 /* This is set for example, when we're attached to the parent of a
2007 vfork, and have detached from the child. The child is running
2008 free, and we expect it to do an exec or exit, at which point the
2009 OS makes the parent schedulable again (and the target reports
2010 that the vfork is done). Until the child is done with the shared
2011 memory region, do not insert breakpoints in the parent, otherwise
2012 the child could still trip on the parent's breakpoints. Since
2013 the parent is blocked anyway, it won't miss any breakpoint. */
2014 if (bl->pspace->breakpoints_not_allowed)
2015 return 0;
2016
2017 /* Don't insert a breakpoint if we're trying to step past its
2018 location, except if the breakpoint is a single-step breakpoint,
2019 and the breakpoint's thread is the thread which is stepping past
2020 a breakpoint. */
2021 if ((bl->loc_type == bp_loc_software_breakpoint
2022 || bl->loc_type == bp_loc_hardware_breakpoint)
2023 && stepping_past_instruction_at (bl->pspace->aspace,
2024 bl->address)
2025 /* The single-step breakpoint may be inserted at the location
2026 we're trying to step if the instruction branches to itself.
2027 However, the instruction won't be executed at all and it may
2028 break the semantics of the instruction, for example, the
2029 instruction is a conditional branch or updates some flags.
2030 We can't fix it unless GDB is able to emulate the instruction
2031 or switch to displaced stepping. */
2032 && !(bl->owner->type == bp_single_step
2033 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2034 {
2035 if (debug_infrun)
2036 {
2037 fprintf_unfiltered (gdb_stdlog,
2038 "infrun: skipping breakpoint: "
2039 "stepping past insn at: %s\n",
2040 paddress (bl->gdbarch, bl->address));
2041 }
2042 return 0;
2043 }
2044
2045 /* Don't insert watchpoints if we're trying to step past the
2046 instruction that triggered one. */
2047 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2048 && stepping_past_nonsteppable_watchpoint ())
2049 {
2050 if (debug_infrun)
2051 {
2052 fprintf_unfiltered (gdb_stdlog,
2053 "infrun: stepping past non-steppable watchpoint. "
2054 "skipping watchpoint at %s:%d\n",
2055 paddress (bl->gdbarch, bl->address),
2056 bl->length);
2057 }
2058 return 0;
2059 }
2060
2061 return 1;
2062 }
2063
2064 /* Same as should_be_inserted but does the check assuming
2065 that the location is not duplicated. */
2066
2067 static int
2068 unduplicated_should_be_inserted (struct bp_location *bl)
2069 {
2070 int result;
2071 const int save_duplicate = bl->duplicate;
2072
2073 bl->duplicate = 0;
2074 result = should_be_inserted (bl);
2075 bl->duplicate = save_duplicate;
2076 return result;
2077 }
2078
2079 /* Parses a conditional described by an expression COND into an
2080 agent expression bytecode suitable for evaluation
2081 by the bytecode interpreter. Return NULL if there was
2082 any error during parsing. */
2083
2084 static agent_expr_up
2085 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2086 {
2087 if (cond == NULL)
2088 return NULL;
2089
2090 agent_expr_up aexpr;
2091
2092 /* We don't want to stop processing, so catch any errors
2093 that may show up. */
2094 TRY
2095 {
2096 aexpr = gen_eval_for_expr (scope, cond);
2097 }
2098
2099 CATCH (ex, RETURN_MASK_ERROR)
2100 {
2101 /* If we got here, it means the condition could not be parsed to a valid
2102 bytecode expression and thus can't be evaluated on the target's side.
2103 It's no use iterating through the conditions. */
2104 }
2105 END_CATCH
2106
2107 /* We have a valid agent expression. */
2108 return aexpr;
2109 }
2110
2111 /* Based on location BL, create a list of breakpoint conditions to be
2112 passed on to the target. If we have duplicated locations with different
2113 conditions, we will add such conditions to the list. The idea is that the
2114 target will evaluate the list of conditions and will only notify GDB when
2115 one of them is true. */
2116
2117 static void
2118 build_target_condition_list (struct bp_location *bl)
2119 {
2120 struct bp_location **locp = NULL, **loc2p;
2121 int null_condition_or_parse_error = 0;
2122 int modified = bl->needs_update;
2123 struct bp_location *loc;
2124
2125 /* Release conditions left over from a previous insert. */
2126 bl->target_info.conditions.clear ();
2127
2128 /* This is only meaningful if the target is
2129 evaluating conditions and if the user has
2130 opted for condition evaluation on the target's
2131 side. */
2132 if (gdb_evaluates_breakpoint_condition_p ()
2133 || !target_supports_evaluation_of_breakpoint_conditions ())
2134 return;
2135
2136 /* Do a first pass to check for locations with no assigned
2137 conditions or conditions that fail to parse to a valid agent expression
2138 bytecode. If any of these happen, then it's no use to send conditions
2139 to the target since this location will always trigger and generate a
2140 response back to GDB. */
2141 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2142 {
2143 loc = (*loc2p);
2144 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2145 {
2146 if (modified)
2147 {
2148 /* Re-parse the conditions since something changed. In that
2149 case we already freed the condition bytecodes (see
2150 force_breakpoint_reinsertion). We just
2151 need to parse the condition to bytecodes again. */
2152 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2153 loc->cond.get ());
2154 }
2155
2156 /* If we have a NULL bytecode expression, it means something
2157 went wrong or we have a null condition expression. */
2158 if (!loc->cond_bytecode)
2159 {
2160 null_condition_or_parse_error = 1;
2161 break;
2162 }
2163 }
2164 }
2165
2166 /* If any of these happened, it means we will have to evaluate the conditions
2167 for the location's address on gdb's side. It is no use keeping bytecodes
2168 for all the other duplicate locations, thus we free all of them here.
2169
2170 This is so we have a finer control over which locations' conditions are
2171 being evaluated by GDB or the remote stub. */
2172 if (null_condition_or_parse_error)
2173 {
2174 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2175 {
2176 loc = (*loc2p);
2177 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2178 {
2179 /* Only go as far as the first NULL bytecode is
2180 located. */
2181 if (!loc->cond_bytecode)
2182 return;
2183
2184 loc->cond_bytecode.reset ();
2185 }
2186 }
2187 }
2188
2189 /* No NULL conditions or failed bytecode generation. Build a condition list
2190 for this location's address. */
2191 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2192 {
2193 loc = (*loc2p);
2194 if (loc->cond
2195 && is_breakpoint (loc->owner)
2196 && loc->pspace->num == bl->pspace->num
2197 && loc->owner->enable_state == bp_enabled
2198 && loc->enabled)
2199 {
2200 /* Add the condition to the vector. This will be used later
2201 to send the conditions to the target. */
2202 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2203 }
2204 }
2205
2206 return;
2207 }
2208
2209 /* Parses a command described by string CMD into an agent expression
2210 bytecode suitable for evaluation by the bytecode interpreter.
2211 Return NULL if there was any error during parsing. */
2212
2213 static agent_expr_up
2214 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2215 {
2216 const char *cmdrest;
2217 const char *format_start, *format_end;
2218 struct gdbarch *gdbarch = get_current_arch ();
2219
2220 if (cmd == NULL)
2221 return NULL;
2222
2223 cmdrest = cmd;
2224
2225 if (*cmdrest == ',')
2226 ++cmdrest;
2227 cmdrest = skip_spaces (cmdrest);
2228
2229 if (*cmdrest++ != '"')
2230 error (_("No format string following the location"));
2231
2232 format_start = cmdrest;
2233
2234 format_pieces fpieces (&cmdrest);
2235
2236 format_end = cmdrest;
2237
2238 if (*cmdrest++ != '"')
2239 error (_("Bad format string, non-terminated '\"'."));
2240
2241 cmdrest = skip_spaces (cmdrest);
2242
2243 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2244 error (_("Invalid argument syntax"));
2245
2246 if (*cmdrest == ',')
2247 cmdrest++;
2248 cmdrest = skip_spaces (cmdrest);
2249
2250 /* For each argument, make an expression. */
2251
2252 std::vector<struct expression *> argvec;
2253 while (*cmdrest != '\0')
2254 {
2255 const char *cmd1;
2256
2257 cmd1 = cmdrest;
2258 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2259 argvec.push_back (expr.release ());
2260 cmdrest = cmd1;
2261 if (*cmdrest == ',')
2262 ++cmdrest;
2263 }
2264
2265 agent_expr_up aexpr;
2266
2267 /* We don't want to stop processing, so catch any errors
2268 that may show up. */
2269 TRY
2270 {
2271 aexpr = gen_printf (scope, gdbarch, 0, 0,
2272 format_start, format_end - format_start,
2273 argvec.size (), argvec.data ());
2274 }
2275 CATCH (ex, RETURN_MASK_ERROR)
2276 {
2277 /* If we got here, it means the command could not be parsed to a valid
2278 bytecode expression and thus can't be evaluated on the target's side.
2279 It's no use iterating through the other commands. */
2280 }
2281 END_CATCH
2282
2283 /* We have a valid agent expression, return it. */
2284 return aexpr;
2285 }
2286
2287 /* Based on location BL, create a list of breakpoint commands to be
2288 passed on to the target. If we have duplicated locations with
2289 different commands, we will add any such to the list. */
2290
2291 static void
2292 build_target_command_list (struct bp_location *bl)
2293 {
2294 struct bp_location **locp = NULL, **loc2p;
2295 int null_command_or_parse_error = 0;
2296 int modified = bl->needs_update;
2297 struct bp_location *loc;
2298
2299 /* Clear commands left over from a previous insert. */
2300 bl->target_info.tcommands.clear ();
2301
2302 if (!target_can_run_breakpoint_commands ())
2303 return;
2304
2305 /* For now, limit to agent-style dprintf breakpoints. */
2306 if (dprintf_style != dprintf_style_agent)
2307 return;
2308
2309 /* For now, if we have any duplicate location that isn't a dprintf,
2310 don't install the target-side commands, as that would make the
2311 breakpoint not be reported to the core, and we'd lose
2312 control. */
2313 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2314 {
2315 loc = (*loc2p);
2316 if (is_breakpoint (loc->owner)
2317 && loc->pspace->num == bl->pspace->num
2318 && loc->owner->type != bp_dprintf)
2319 return;
2320 }
2321
2322 /* Do a first pass to check for locations with no assigned
2323 conditions or conditions that fail to parse to a valid agent expression
2324 bytecode. If any of these happen, then it's no use to send conditions
2325 to the target since this location will always trigger and generate a
2326 response back to GDB. */
2327 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2328 {
2329 loc = (*loc2p);
2330 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2331 {
2332 if (modified)
2333 {
2334 /* Re-parse the commands since something changed. In that
2335 case we already freed the command bytecodes (see
2336 force_breakpoint_reinsertion). We just
2337 need to parse the command to bytecodes again. */
2338 loc->cmd_bytecode
2339 = parse_cmd_to_aexpr (bl->address,
2340 loc->owner->extra_string);
2341 }
2342
2343 /* If we have a NULL bytecode expression, it means something
2344 went wrong or we have a null command expression. */
2345 if (!loc->cmd_bytecode)
2346 {
2347 null_command_or_parse_error = 1;
2348 break;
2349 }
2350 }
2351 }
2352
2353 /* If anything failed, then we're not doing target-side commands,
2354 and so clean up. */
2355 if (null_command_or_parse_error)
2356 {
2357 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2358 {
2359 loc = (*loc2p);
2360 if (is_breakpoint (loc->owner)
2361 && loc->pspace->num == bl->pspace->num)
2362 {
2363 /* Only go as far as the first NULL bytecode is
2364 located. */
2365 if (loc->cmd_bytecode == NULL)
2366 return;
2367
2368 loc->cmd_bytecode.reset ();
2369 }
2370 }
2371 }
2372
2373 /* No NULL commands or failed bytecode generation. Build a command list
2374 for this location's address. */
2375 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2376 {
2377 loc = (*loc2p);
2378 if (loc->owner->extra_string
2379 && is_breakpoint (loc->owner)
2380 && loc->pspace->num == bl->pspace->num
2381 && loc->owner->enable_state == bp_enabled
2382 && loc->enabled)
2383 {
2384 /* Add the command to the vector. This will be used later
2385 to send the commands to the target. */
2386 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2387 }
2388 }
2389
2390 bl->target_info.persist = 0;
2391 /* Maybe flag this location as persistent. */
2392 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2393 bl->target_info.persist = 1;
2394 }
2395
2396 /* Return the kind of breakpoint on address *ADDR. Get the kind
2397 of breakpoint according to ADDR except single-step breakpoint.
2398 Get the kind of single-step breakpoint according to the current
2399 registers state. */
2400
2401 static int
2402 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2403 {
2404 if (bl->owner->type == bp_single_step)
2405 {
2406 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2407 struct regcache *regcache;
2408
2409 regcache = get_thread_regcache (thr);
2410
2411 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2412 regcache, addr);
2413 }
2414 else
2415 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2416 }
2417
2418 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2419 location. Any error messages are printed to TMP_ERROR_STREAM; and
2420 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2421 Returns 0 for success, 1 if the bp_location type is not supported or
2422 -1 for failure.
2423
2424 NOTE drow/2003-09-09: This routine could be broken down to an
2425 object-style method for each breakpoint or catchpoint type. */
2426 static int
2427 insert_bp_location (struct bp_location *bl,
2428 struct ui_file *tmp_error_stream,
2429 int *disabled_breaks,
2430 int *hw_breakpoint_error,
2431 int *hw_bp_error_explained_already)
2432 {
2433 gdb_exception bp_excpt = exception_none;
2434
2435 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2436 return 0;
2437
2438 /* Note we don't initialize bl->target_info, as that wipes out
2439 the breakpoint location's shadow_contents if the breakpoint
2440 is still inserted at that location. This in turn breaks
2441 target_read_memory which depends on these buffers when
2442 a memory read is requested at the breakpoint location:
2443 Once the target_info has been wiped, we fail to see that
2444 we have a breakpoint inserted at that address and thus
2445 read the breakpoint instead of returning the data saved in
2446 the breakpoint location's shadow contents. */
2447 bl->target_info.reqstd_address = bl->address;
2448 bl->target_info.placed_address_space = bl->pspace->aspace;
2449 bl->target_info.length = bl->length;
2450
2451 /* When working with target-side conditions, we must pass all the conditions
2452 for the same breakpoint address down to the target since GDB will not
2453 insert those locations. With a list of breakpoint conditions, the target
2454 can decide when to stop and notify GDB. */
2455
2456 if (is_breakpoint (bl->owner))
2457 {
2458 build_target_condition_list (bl);
2459 build_target_command_list (bl);
2460 /* Reset the modification marker. */
2461 bl->needs_update = 0;
2462 }
2463
2464 if (bl->loc_type == bp_loc_software_breakpoint
2465 || bl->loc_type == bp_loc_hardware_breakpoint)
2466 {
2467 if (bl->owner->type != bp_hardware_breakpoint)
2468 {
2469 /* If the explicitly specified breakpoint type
2470 is not hardware breakpoint, check the memory map to see
2471 if the breakpoint address is in read only memory or not.
2472
2473 Two important cases are:
2474 - location type is not hardware breakpoint, memory
2475 is readonly. We change the type of the location to
2476 hardware breakpoint.
2477 - location type is hardware breakpoint, memory is
2478 read-write. This means we've previously made the
2479 location hardware one, but then the memory map changed,
2480 so we undo.
2481
2482 When breakpoints are removed, remove_breakpoints will use
2483 location types we've just set here, the only possible
2484 problem is that memory map has changed during running
2485 program, but it's not going to work anyway with current
2486 gdb. */
2487 struct mem_region *mr
2488 = lookup_mem_region (bl->target_info.reqstd_address);
2489
2490 if (mr)
2491 {
2492 if (automatic_hardware_breakpoints)
2493 {
2494 enum bp_loc_type new_type;
2495
2496 if (mr->attrib.mode != MEM_RW)
2497 new_type = bp_loc_hardware_breakpoint;
2498 else
2499 new_type = bp_loc_software_breakpoint;
2500
2501 if (new_type != bl->loc_type)
2502 {
2503 static int said = 0;
2504
2505 bl->loc_type = new_type;
2506 if (!said)
2507 {
2508 fprintf_filtered (gdb_stdout,
2509 _("Note: automatically using "
2510 "hardware breakpoints for "
2511 "read-only addresses.\n"));
2512 said = 1;
2513 }
2514 }
2515 }
2516 else if (bl->loc_type == bp_loc_software_breakpoint
2517 && mr->attrib.mode != MEM_RW)
2518 {
2519 fprintf_unfiltered (tmp_error_stream,
2520 _("Cannot insert breakpoint %d.\n"
2521 "Cannot set software breakpoint "
2522 "at read-only address %s\n"),
2523 bl->owner->number,
2524 paddress (bl->gdbarch, bl->address));
2525 return 1;
2526 }
2527 }
2528 }
2529
2530 /* First check to see if we have to handle an overlay. */
2531 if (overlay_debugging == ovly_off
2532 || bl->section == NULL
2533 || !(section_is_overlay (bl->section)))
2534 {
2535 /* No overlay handling: just set the breakpoint. */
2536 TRY
2537 {
2538 int val;
2539
2540 val = bl->owner->ops->insert_location (bl);
2541 if (val)
2542 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2543 }
2544 CATCH (e, RETURN_MASK_ALL)
2545 {
2546 bp_excpt = e;
2547 }
2548 END_CATCH
2549 }
2550 else
2551 {
2552 /* This breakpoint is in an overlay section.
2553 Shall we set a breakpoint at the LMA? */
2554 if (!overlay_events_enabled)
2555 {
2556 /* Yes -- overlay event support is not active,
2557 so we must try to set a breakpoint at the LMA.
2558 This will not work for a hardware breakpoint. */
2559 if (bl->loc_type == bp_loc_hardware_breakpoint)
2560 warning (_("hardware breakpoint %d not supported in overlay!"),
2561 bl->owner->number);
2562 else
2563 {
2564 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2565 bl->section);
2566 /* Set a software (trap) breakpoint at the LMA. */
2567 bl->overlay_target_info = bl->target_info;
2568 bl->overlay_target_info.reqstd_address = addr;
2569
2570 /* No overlay handling: just set the breakpoint. */
2571 TRY
2572 {
2573 int val;
2574
2575 bl->overlay_target_info.kind
2576 = breakpoint_kind (bl, &addr);
2577 bl->overlay_target_info.placed_address = addr;
2578 val = target_insert_breakpoint (bl->gdbarch,
2579 &bl->overlay_target_info);
2580 if (val)
2581 bp_excpt
2582 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2583 }
2584 CATCH (e, RETURN_MASK_ALL)
2585 {
2586 bp_excpt = e;
2587 }
2588 END_CATCH
2589
2590 if (bp_excpt.reason != 0)
2591 fprintf_unfiltered (tmp_error_stream,
2592 "Overlay breakpoint %d "
2593 "failed: in ROM?\n",
2594 bl->owner->number);
2595 }
2596 }
2597 /* Shall we set a breakpoint at the VMA? */
2598 if (section_is_mapped (bl->section))
2599 {
2600 /* Yes. This overlay section is mapped into memory. */
2601 TRY
2602 {
2603 int val;
2604
2605 val = bl->owner->ops->insert_location (bl);
2606 if (val)
2607 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2608 }
2609 CATCH (e, RETURN_MASK_ALL)
2610 {
2611 bp_excpt = e;
2612 }
2613 END_CATCH
2614 }
2615 else
2616 {
2617 /* No. This breakpoint will not be inserted.
2618 No error, but do not mark the bp as 'inserted'. */
2619 return 0;
2620 }
2621 }
2622
2623 if (bp_excpt.reason != 0)
2624 {
2625 /* Can't set the breakpoint. */
2626
2627 /* In some cases, we might not be able to insert a
2628 breakpoint in a shared library that has already been
2629 removed, but we have not yet processed the shlib unload
2630 event. Unfortunately, some targets that implement
2631 breakpoint insertion themselves can't tell why the
2632 breakpoint insertion failed (e.g., the remote target
2633 doesn't define error codes), so we must treat generic
2634 errors as memory errors. */
2635 if (bp_excpt.reason == RETURN_ERROR
2636 && (bp_excpt.error == GENERIC_ERROR
2637 || bp_excpt.error == MEMORY_ERROR)
2638 && bl->loc_type == bp_loc_software_breakpoint
2639 && (solib_name_from_address (bl->pspace, bl->address)
2640 || shared_objfile_contains_address_p (bl->pspace,
2641 bl->address)))
2642 {
2643 /* See also: disable_breakpoints_in_shlibs. */
2644 bl->shlib_disabled = 1;
2645 gdb::observers::breakpoint_modified.notify (bl->owner);
2646 if (!*disabled_breaks)
2647 {
2648 fprintf_unfiltered (tmp_error_stream,
2649 "Cannot insert breakpoint %d.\n",
2650 bl->owner->number);
2651 fprintf_unfiltered (tmp_error_stream,
2652 "Temporarily disabling shared "
2653 "library breakpoints:\n");
2654 }
2655 *disabled_breaks = 1;
2656 fprintf_unfiltered (tmp_error_stream,
2657 "breakpoint #%d\n", bl->owner->number);
2658 return 0;
2659 }
2660 else
2661 {
2662 if (bl->loc_type == bp_loc_hardware_breakpoint)
2663 {
2664 *hw_breakpoint_error = 1;
2665 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2666 fprintf_unfiltered (tmp_error_stream,
2667 "Cannot insert hardware breakpoint %d%s",
2668 bl->owner->number,
2669 bp_excpt.message ? ":" : ".\n");
2670 if (bp_excpt.message != NULL)
2671 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2672 bp_excpt.message);
2673 }
2674 else
2675 {
2676 if (bp_excpt.message == NULL)
2677 {
2678 std::string message
2679 = memory_error_message (TARGET_XFER_E_IO,
2680 bl->gdbarch, bl->address);
2681
2682 fprintf_unfiltered (tmp_error_stream,
2683 "Cannot insert breakpoint %d.\n"
2684 "%s\n",
2685 bl->owner->number, message.c_str ());
2686 }
2687 else
2688 {
2689 fprintf_unfiltered (tmp_error_stream,
2690 "Cannot insert breakpoint %d: %s\n",
2691 bl->owner->number,
2692 bp_excpt.message);
2693 }
2694 }
2695 return 1;
2696
2697 }
2698 }
2699 else
2700 bl->inserted = 1;
2701
2702 return 0;
2703 }
2704
2705 else if (bl->loc_type == bp_loc_hardware_watchpoint
2706 /* NOTE drow/2003-09-08: This state only exists for removing
2707 watchpoints. It's not clear that it's necessary... */
2708 && bl->owner->disposition != disp_del_at_next_stop)
2709 {
2710 int val;
2711
2712 gdb_assert (bl->owner->ops != NULL
2713 && bl->owner->ops->insert_location != NULL);
2714
2715 val = bl->owner->ops->insert_location (bl);
2716
2717 /* If trying to set a read-watchpoint, and it turns out it's not
2718 supported, try emulating one with an access watchpoint. */
2719 if (val == 1 && bl->watchpoint_type == hw_read)
2720 {
2721 struct bp_location *loc, **loc_temp;
2722
2723 /* But don't try to insert it, if there's already another
2724 hw_access location that would be considered a duplicate
2725 of this one. */
2726 ALL_BP_LOCATIONS (loc, loc_temp)
2727 if (loc != bl
2728 && loc->watchpoint_type == hw_access
2729 && watchpoint_locations_match (bl, loc))
2730 {
2731 bl->duplicate = 1;
2732 bl->inserted = 1;
2733 bl->target_info = loc->target_info;
2734 bl->watchpoint_type = hw_access;
2735 val = 0;
2736 break;
2737 }
2738
2739 if (val == 1)
2740 {
2741 bl->watchpoint_type = hw_access;
2742 val = bl->owner->ops->insert_location (bl);
2743
2744 if (val)
2745 /* Back to the original value. */
2746 bl->watchpoint_type = hw_read;
2747 }
2748 }
2749
2750 bl->inserted = (val == 0);
2751 }
2752
2753 else if (bl->owner->type == bp_catchpoint)
2754 {
2755 int val;
2756
2757 gdb_assert (bl->owner->ops != NULL
2758 && bl->owner->ops->insert_location != NULL);
2759
2760 val = bl->owner->ops->insert_location (bl);
2761 if (val)
2762 {
2763 bl->owner->enable_state = bp_disabled;
2764
2765 if (val == 1)
2766 warning (_("\
2767 Error inserting catchpoint %d: Your system does not support this type\n\
2768 of catchpoint."), bl->owner->number);
2769 else
2770 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2771 }
2772
2773 bl->inserted = (val == 0);
2774
2775 /* We've already printed an error message if there was a problem
2776 inserting this catchpoint, and we've disabled the catchpoint,
2777 so just return success. */
2778 return 0;
2779 }
2780
2781 return 0;
2782 }
2783
2784 /* This function is called when program space PSPACE is about to be
2785 deleted. It takes care of updating breakpoints to not reference
2786 PSPACE anymore. */
2787
2788 void
2789 breakpoint_program_space_exit (struct program_space *pspace)
2790 {
2791 struct breakpoint *b, *b_temp;
2792 struct bp_location *loc, **loc_temp;
2793
2794 /* Remove any breakpoint that was set through this program space. */
2795 ALL_BREAKPOINTS_SAFE (b, b_temp)
2796 {
2797 if (b->pspace == pspace)
2798 delete_breakpoint (b);
2799 }
2800
2801 /* Breakpoints set through other program spaces could have locations
2802 bound to PSPACE as well. Remove those. */
2803 ALL_BP_LOCATIONS (loc, loc_temp)
2804 {
2805 struct bp_location *tmp;
2806
2807 if (loc->pspace == pspace)
2808 {
2809 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2810 if (loc->owner->loc == loc)
2811 loc->owner->loc = loc->next;
2812 else
2813 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2814 if (tmp->next == loc)
2815 {
2816 tmp->next = loc->next;
2817 break;
2818 }
2819 }
2820 }
2821
2822 /* Now update the global location list to permanently delete the
2823 removed locations above. */
2824 update_global_location_list (UGLL_DONT_INSERT);
2825 }
2826
2827 /* Make sure all breakpoints are inserted in inferior.
2828 Throws exception on any error.
2829 A breakpoint that is already inserted won't be inserted
2830 again, so calling this function twice is safe. */
2831 void
2832 insert_breakpoints (void)
2833 {
2834 struct breakpoint *bpt;
2835
2836 ALL_BREAKPOINTS (bpt)
2837 if (is_hardware_watchpoint (bpt))
2838 {
2839 struct watchpoint *w = (struct watchpoint *) bpt;
2840
2841 update_watchpoint (w, 0 /* don't reparse. */);
2842 }
2843
2844 /* Updating watchpoints creates new locations, so update the global
2845 location list. Explicitly tell ugll to insert locations and
2846 ignore breakpoints_always_inserted_mode. */
2847 update_global_location_list (UGLL_INSERT);
2848 }
2849
2850 /* Invoke CALLBACK for each of bp_location. */
2851
2852 void
2853 iterate_over_bp_locations (walk_bp_location_callback callback)
2854 {
2855 struct bp_location *loc, **loc_tmp;
2856
2857 ALL_BP_LOCATIONS (loc, loc_tmp)
2858 {
2859 callback (loc, NULL);
2860 }
2861 }
2862
2863 /* This is used when we need to synch breakpoint conditions between GDB and the
2864 target. It is the case with deleting and disabling of breakpoints when using
2865 always-inserted mode. */
2866
2867 static void
2868 update_inserted_breakpoint_locations (void)
2869 {
2870 struct bp_location *bl, **blp_tmp;
2871 int error_flag = 0;
2872 int val = 0;
2873 int disabled_breaks = 0;
2874 int hw_breakpoint_error = 0;
2875 int hw_bp_details_reported = 0;
2876
2877 string_file tmp_error_stream;
2878
2879 /* Explicitly mark the warning -- this will only be printed if
2880 there was an error. */
2881 tmp_error_stream.puts ("Warning:\n");
2882
2883 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2884
2885 ALL_BP_LOCATIONS (bl, blp_tmp)
2886 {
2887 /* We only want to update software breakpoints and hardware
2888 breakpoints. */
2889 if (!is_breakpoint (bl->owner))
2890 continue;
2891
2892 /* We only want to update locations that are already inserted
2893 and need updating. This is to avoid unwanted insertion during
2894 deletion of breakpoints. */
2895 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2896 continue;
2897
2898 switch_to_program_space_and_thread (bl->pspace);
2899
2900 /* For targets that support global breakpoints, there's no need
2901 to select an inferior to insert breakpoint to. In fact, even
2902 if we aren't attached to any process yet, we should still
2903 insert breakpoints. */
2904 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2905 && inferior_ptid == null_ptid)
2906 continue;
2907
2908 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2909 &hw_breakpoint_error, &hw_bp_details_reported);
2910 if (val)
2911 error_flag = val;
2912 }
2913
2914 if (error_flag)
2915 {
2916 target_terminal::ours_for_output ();
2917 error_stream (tmp_error_stream);
2918 }
2919 }
2920
2921 /* Used when starting or continuing the program. */
2922
2923 static void
2924 insert_breakpoint_locations (void)
2925 {
2926 struct breakpoint *bpt;
2927 struct bp_location *bl, **blp_tmp;
2928 int error_flag = 0;
2929 int val = 0;
2930 int disabled_breaks = 0;
2931 int hw_breakpoint_error = 0;
2932 int hw_bp_error_explained_already = 0;
2933
2934 string_file tmp_error_stream;
2935
2936 /* Explicitly mark the warning -- this will only be printed if
2937 there was an error. */
2938 tmp_error_stream.puts ("Warning:\n");
2939
2940 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2941
2942 ALL_BP_LOCATIONS (bl, blp_tmp)
2943 {
2944 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2945 continue;
2946
2947 /* There is no point inserting thread-specific breakpoints if
2948 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2949 has BL->OWNER always non-NULL. */
2950 if (bl->owner->thread != -1
2951 && !valid_global_thread_id (bl->owner->thread))
2952 continue;
2953
2954 switch_to_program_space_and_thread (bl->pspace);
2955
2956 /* For targets that support global breakpoints, there's no need
2957 to select an inferior to insert breakpoint to. In fact, even
2958 if we aren't attached to any process yet, we should still
2959 insert breakpoints. */
2960 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2961 && inferior_ptid == null_ptid)
2962 continue;
2963
2964 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
2965 &hw_breakpoint_error, &hw_bp_error_explained_already);
2966 if (val)
2967 error_flag = val;
2968 }
2969
2970 /* If we failed to insert all locations of a watchpoint, remove
2971 them, as half-inserted watchpoint is of limited use. */
2972 ALL_BREAKPOINTS (bpt)
2973 {
2974 int some_failed = 0;
2975 struct bp_location *loc;
2976
2977 if (!is_hardware_watchpoint (bpt))
2978 continue;
2979
2980 if (!breakpoint_enabled (bpt))
2981 continue;
2982
2983 if (bpt->disposition == disp_del_at_next_stop)
2984 continue;
2985
2986 for (loc = bpt->loc; loc; loc = loc->next)
2987 if (!loc->inserted && should_be_inserted (loc))
2988 {
2989 some_failed = 1;
2990 break;
2991 }
2992 if (some_failed)
2993 {
2994 for (loc = bpt->loc; loc; loc = loc->next)
2995 if (loc->inserted)
2996 remove_breakpoint (loc);
2997
2998 hw_breakpoint_error = 1;
2999 tmp_error_stream.printf ("Could not insert "
3000 "hardware watchpoint %d.\n",
3001 bpt->number);
3002 error_flag = -1;
3003 }
3004 }
3005
3006 if (error_flag)
3007 {
3008 /* If a hardware breakpoint or watchpoint was inserted, add a
3009 message about possibly exhausted resources. */
3010 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3011 {
3012 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3013 You may have requested too many hardware breakpoints/watchpoints.\n");
3014 }
3015 target_terminal::ours_for_output ();
3016 error_stream (tmp_error_stream);
3017 }
3018 }
3019
3020 /* Used when the program stops.
3021 Returns zero if successful, or non-zero if there was a problem
3022 removing a breakpoint location. */
3023
3024 int
3025 remove_breakpoints (void)
3026 {
3027 struct bp_location *bl, **blp_tmp;
3028 int val = 0;
3029
3030 ALL_BP_LOCATIONS (bl, blp_tmp)
3031 {
3032 if (bl->inserted && !is_tracepoint (bl->owner))
3033 val |= remove_breakpoint (bl);
3034 }
3035 return val;
3036 }
3037
3038 /* When a thread exits, remove breakpoints that are related to
3039 that thread. */
3040
3041 static void
3042 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3043 {
3044 struct breakpoint *b, *b_tmp;
3045
3046 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3047 {
3048 if (b->thread == tp->global_num && user_breakpoint_p (b))
3049 {
3050 b->disposition = disp_del_at_next_stop;
3051
3052 printf_filtered (_("\
3053 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3054 b->number, print_thread_id (tp));
3055
3056 /* Hide it from the user. */
3057 b->number = 0;
3058 }
3059 }
3060 }
3061
3062 /* Remove breakpoints of inferior INF. */
3063
3064 int
3065 remove_breakpoints_inf (inferior *inf)
3066 {
3067 struct bp_location *bl, **blp_tmp;
3068 int val;
3069
3070 ALL_BP_LOCATIONS (bl, blp_tmp)
3071 {
3072 if (bl->pspace != inf->pspace)
3073 continue;
3074
3075 if (bl->inserted && !bl->target_info.persist)
3076 {
3077 val = remove_breakpoint (bl);
3078 if (val != 0)
3079 return val;
3080 }
3081 }
3082 return 0;
3083 }
3084
3085 static int internal_breakpoint_number = -1;
3086
3087 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3088 If INTERNAL is non-zero, the breakpoint number will be populated
3089 from internal_breakpoint_number and that variable decremented.
3090 Otherwise the breakpoint number will be populated from
3091 breakpoint_count and that value incremented. Internal breakpoints
3092 do not set the internal var bpnum. */
3093 static void
3094 set_breakpoint_number (int internal, struct breakpoint *b)
3095 {
3096 if (internal)
3097 b->number = internal_breakpoint_number--;
3098 else
3099 {
3100 set_breakpoint_count (breakpoint_count + 1);
3101 b->number = breakpoint_count;
3102 }
3103 }
3104
3105 static struct breakpoint *
3106 create_internal_breakpoint (struct gdbarch *gdbarch,
3107 CORE_ADDR address, enum bptype type,
3108 const struct breakpoint_ops *ops)
3109 {
3110 symtab_and_line sal;
3111 sal.pc = address;
3112 sal.section = find_pc_overlay (sal.pc);
3113 sal.pspace = current_program_space;
3114
3115 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3116 b->number = internal_breakpoint_number--;
3117 b->disposition = disp_donttouch;
3118
3119 return b;
3120 }
3121
3122 static const char *const longjmp_names[] =
3123 {
3124 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3125 };
3126 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3127
3128 /* Per-objfile data private to breakpoint.c. */
3129 struct breakpoint_objfile_data
3130 {
3131 /* Minimal symbol for "_ovly_debug_event" (if any). */
3132 struct bound_minimal_symbol overlay_msym {};
3133
3134 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3135 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
3136
3137 /* True if we have looked for longjmp probes. */
3138 int longjmp_searched = 0;
3139
3140 /* SystemTap probe points for longjmp (if any). These are non-owning
3141 references. */
3142 std::vector<probe *> longjmp_probes;
3143
3144 /* Minimal symbol for "std::terminate()" (if any). */
3145 struct bound_minimal_symbol terminate_msym {};
3146
3147 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3148 struct bound_minimal_symbol exception_msym {};
3149
3150 /* True if we have looked for exception probes. */
3151 int exception_searched = 0;
3152
3153 /* SystemTap probe points for unwinding (if any). These are non-owning
3154 references. */
3155 std::vector<probe *> exception_probes;
3156 };
3157
3158 static const struct objfile_data *breakpoint_objfile_key;
3159
3160 /* Minimal symbol not found sentinel. */
3161 static struct minimal_symbol msym_not_found;
3162
3163 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3164
3165 static int
3166 msym_not_found_p (const struct minimal_symbol *msym)
3167 {
3168 return msym == &msym_not_found;
3169 }
3170
3171 /* Return per-objfile data needed by breakpoint.c.
3172 Allocate the data if necessary. */
3173
3174 static struct breakpoint_objfile_data *
3175 get_breakpoint_objfile_data (struct objfile *objfile)
3176 {
3177 struct breakpoint_objfile_data *bp_objfile_data;
3178
3179 bp_objfile_data = ((struct breakpoint_objfile_data *)
3180 objfile_data (objfile, breakpoint_objfile_key));
3181 if (bp_objfile_data == NULL)
3182 {
3183 bp_objfile_data = new breakpoint_objfile_data ();
3184 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3185 }
3186 return bp_objfile_data;
3187 }
3188
3189 static void
3190 free_breakpoint_objfile_data (struct objfile *obj, void *data)
3191 {
3192 struct breakpoint_objfile_data *bp_objfile_data
3193 = (struct breakpoint_objfile_data *) data;
3194
3195 delete bp_objfile_data;
3196 }
3197
3198 static void
3199 create_overlay_event_breakpoint (void)
3200 {
3201 struct objfile *objfile;
3202 const char *const func_name = "_ovly_debug_event";
3203
3204 ALL_OBJFILES (objfile)
3205 {
3206 struct breakpoint *b;
3207 struct breakpoint_objfile_data *bp_objfile_data;
3208 CORE_ADDR addr;
3209 struct explicit_location explicit_loc;
3210
3211 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3212
3213 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3214 continue;
3215
3216 if (bp_objfile_data->overlay_msym.minsym == NULL)
3217 {
3218 struct bound_minimal_symbol m;
3219
3220 m = lookup_minimal_symbol_text (func_name, objfile);
3221 if (m.minsym == NULL)
3222 {
3223 /* Avoid future lookups in this objfile. */
3224 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3225 continue;
3226 }
3227 bp_objfile_data->overlay_msym = m;
3228 }
3229
3230 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3231 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3232 bp_overlay_event,
3233 &internal_breakpoint_ops);
3234 initialize_explicit_location (&explicit_loc);
3235 explicit_loc.function_name = ASTRDUP (func_name);
3236 b->location = new_explicit_location (&explicit_loc);
3237
3238 if (overlay_debugging == ovly_auto)
3239 {
3240 b->enable_state = bp_enabled;
3241 overlay_events_enabled = 1;
3242 }
3243 else
3244 {
3245 b->enable_state = bp_disabled;
3246 overlay_events_enabled = 0;
3247 }
3248 }
3249 }
3250
3251 static void
3252 create_longjmp_master_breakpoint (void)
3253 {
3254 struct program_space *pspace;
3255
3256 scoped_restore_current_program_space restore_pspace;
3257
3258 ALL_PSPACES (pspace)
3259 {
3260 struct objfile *objfile;
3261
3262 set_current_program_space (pspace);
3263
3264 ALL_OBJFILES (objfile)
3265 {
3266 int i;
3267 struct gdbarch *gdbarch;
3268 struct breakpoint_objfile_data *bp_objfile_data;
3269
3270 gdbarch = get_objfile_arch (objfile);
3271
3272 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3273
3274 if (!bp_objfile_data->longjmp_searched)
3275 {
3276 std::vector<probe *> ret
3277 = find_probes_in_objfile (objfile, "libc", "longjmp");
3278
3279 if (!ret.empty ())
3280 {
3281 /* We are only interested in checking one element. */
3282 probe *p = ret[0];
3283
3284 if (!p->can_evaluate_arguments ())
3285 {
3286 /* We cannot use the probe interface here, because it does
3287 not know how to evaluate arguments. */
3288 ret.clear ();
3289 }
3290 }
3291 bp_objfile_data->longjmp_probes = ret;
3292 bp_objfile_data->longjmp_searched = 1;
3293 }
3294
3295 if (!bp_objfile_data->longjmp_probes.empty ())
3296 {
3297 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3298
3299 for (probe *p : bp_objfile_data->longjmp_probes)
3300 {
3301 struct breakpoint *b;
3302
3303 b = create_internal_breakpoint (gdbarch,
3304 p->get_relocated_address (objfile),
3305 bp_longjmp_master,
3306 &internal_breakpoint_ops);
3307 b->location = new_probe_location ("-probe-stap libc:longjmp");
3308 b->enable_state = bp_disabled;
3309 }
3310
3311 continue;
3312 }
3313
3314 if (!gdbarch_get_longjmp_target_p (gdbarch))
3315 continue;
3316
3317 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3318 {
3319 struct breakpoint *b;
3320 const char *func_name;
3321 CORE_ADDR addr;
3322 struct explicit_location explicit_loc;
3323
3324 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3325 continue;
3326
3327 func_name = longjmp_names[i];
3328 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3329 {
3330 struct bound_minimal_symbol m;
3331
3332 m = lookup_minimal_symbol_text (func_name, objfile);
3333 if (m.minsym == NULL)
3334 {
3335 /* Prevent future lookups in this objfile. */
3336 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3337 continue;
3338 }
3339 bp_objfile_data->longjmp_msym[i] = m;
3340 }
3341
3342 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3343 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3344 &internal_breakpoint_ops);
3345 initialize_explicit_location (&explicit_loc);
3346 explicit_loc.function_name = ASTRDUP (func_name);
3347 b->location = new_explicit_location (&explicit_loc);
3348 b->enable_state = bp_disabled;
3349 }
3350 }
3351 }
3352 }
3353
3354 /* Create a master std::terminate breakpoint. */
3355 static void
3356 create_std_terminate_master_breakpoint (void)
3357 {
3358 struct program_space *pspace;
3359 const char *const func_name = "std::terminate()";
3360
3361 scoped_restore_current_program_space restore_pspace;
3362
3363 ALL_PSPACES (pspace)
3364 {
3365 struct objfile *objfile;
3366 CORE_ADDR addr;
3367
3368 set_current_program_space (pspace);
3369
3370 ALL_OBJFILES (objfile)
3371 {
3372 struct breakpoint *b;
3373 struct breakpoint_objfile_data *bp_objfile_data;
3374 struct explicit_location explicit_loc;
3375
3376 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3377
3378 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3379 continue;
3380
3381 if (bp_objfile_data->terminate_msym.minsym == NULL)
3382 {
3383 struct bound_minimal_symbol m;
3384
3385 m = lookup_minimal_symbol (func_name, NULL, objfile);
3386 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3387 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3388 {
3389 /* Prevent future lookups in this objfile. */
3390 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3391 continue;
3392 }
3393 bp_objfile_data->terminate_msym = m;
3394 }
3395
3396 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3397 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3398 bp_std_terminate_master,
3399 &internal_breakpoint_ops);
3400 initialize_explicit_location (&explicit_loc);
3401 explicit_loc.function_name = ASTRDUP (func_name);
3402 b->location = new_explicit_location (&explicit_loc);
3403 b->enable_state = bp_disabled;
3404 }
3405 }
3406 }
3407
3408 /* Install a master breakpoint on the unwinder's debug hook. */
3409
3410 static void
3411 create_exception_master_breakpoint (void)
3412 {
3413 struct objfile *objfile;
3414 const char *const func_name = "_Unwind_DebugHook";
3415
3416 ALL_OBJFILES (objfile)
3417 {
3418 struct breakpoint *b;
3419 struct gdbarch *gdbarch;
3420 struct breakpoint_objfile_data *bp_objfile_data;
3421 CORE_ADDR addr;
3422 struct explicit_location explicit_loc;
3423
3424 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3425
3426 /* We prefer the SystemTap probe point if it exists. */
3427 if (!bp_objfile_data->exception_searched)
3428 {
3429 std::vector<probe *> ret
3430 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3431
3432 if (!ret.empty ())
3433 {
3434 /* We are only interested in checking one element. */
3435 probe *p = ret[0];
3436
3437 if (!p->can_evaluate_arguments ())
3438 {
3439 /* We cannot use the probe interface here, because it does
3440 not know how to evaluate arguments. */
3441 ret.clear ();
3442 }
3443 }
3444 bp_objfile_data->exception_probes = ret;
3445 bp_objfile_data->exception_searched = 1;
3446 }
3447
3448 if (!bp_objfile_data->exception_probes.empty ())
3449 {
3450 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3451
3452 for (probe *p : bp_objfile_data->exception_probes)
3453 {
3454 struct breakpoint *b;
3455
3456 b = create_internal_breakpoint (gdbarch,
3457 p->get_relocated_address (objfile),
3458 bp_exception_master,
3459 &internal_breakpoint_ops);
3460 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3461 b->enable_state = bp_disabled;
3462 }
3463
3464 continue;
3465 }
3466
3467 /* Otherwise, try the hook function. */
3468
3469 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3470 continue;
3471
3472 gdbarch = get_objfile_arch (objfile);
3473
3474 if (bp_objfile_data->exception_msym.minsym == NULL)
3475 {
3476 struct bound_minimal_symbol debug_hook;
3477
3478 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3479 if (debug_hook.minsym == NULL)
3480 {
3481 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3482 continue;
3483 }
3484
3485 bp_objfile_data->exception_msym = debug_hook;
3486 }
3487
3488 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3489 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3490 current_top_target ());
3491 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3492 &internal_breakpoint_ops);
3493 initialize_explicit_location (&explicit_loc);
3494 explicit_loc.function_name = ASTRDUP (func_name);
3495 b->location = new_explicit_location (&explicit_loc);
3496 b->enable_state = bp_disabled;
3497 }
3498 }
3499
3500 /* Does B have a location spec? */
3501
3502 static int
3503 breakpoint_event_location_empty_p (const struct breakpoint *b)
3504 {
3505 return b->location != NULL && event_location_empty_p (b->location.get ());
3506 }
3507
3508 void
3509 update_breakpoints_after_exec (void)
3510 {
3511 struct breakpoint *b, *b_tmp;
3512 struct bp_location *bploc, **bplocp_tmp;
3513
3514 /* We're about to delete breakpoints from GDB's lists. If the
3515 INSERTED flag is true, GDB will try to lift the breakpoints by
3516 writing the breakpoints' "shadow contents" back into memory. The
3517 "shadow contents" are NOT valid after an exec, so GDB should not
3518 do that. Instead, the target is responsible from marking
3519 breakpoints out as soon as it detects an exec. We don't do that
3520 here instead, because there may be other attempts to delete
3521 breakpoints after detecting an exec and before reaching here. */
3522 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3523 if (bploc->pspace == current_program_space)
3524 gdb_assert (!bploc->inserted);
3525
3526 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3527 {
3528 if (b->pspace != current_program_space)
3529 continue;
3530
3531 /* Solib breakpoints must be explicitly reset after an exec(). */
3532 if (b->type == bp_shlib_event)
3533 {
3534 delete_breakpoint (b);
3535 continue;
3536 }
3537
3538 /* JIT breakpoints must be explicitly reset after an exec(). */
3539 if (b->type == bp_jit_event)
3540 {
3541 delete_breakpoint (b);
3542 continue;
3543 }
3544
3545 /* Thread event breakpoints must be set anew after an exec(),
3546 as must overlay event and longjmp master breakpoints. */
3547 if (b->type == bp_thread_event || b->type == bp_overlay_event
3548 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3549 || b->type == bp_exception_master)
3550 {
3551 delete_breakpoint (b);
3552 continue;
3553 }
3554
3555 /* Step-resume breakpoints are meaningless after an exec(). */
3556 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3557 {
3558 delete_breakpoint (b);
3559 continue;
3560 }
3561
3562 /* Just like single-step breakpoints. */
3563 if (b->type == bp_single_step)
3564 {
3565 delete_breakpoint (b);
3566 continue;
3567 }
3568
3569 /* Longjmp and longjmp-resume breakpoints are also meaningless
3570 after an exec. */
3571 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3572 || b->type == bp_longjmp_call_dummy
3573 || b->type == bp_exception || b->type == bp_exception_resume)
3574 {
3575 delete_breakpoint (b);
3576 continue;
3577 }
3578
3579 if (b->type == bp_catchpoint)
3580 {
3581 /* For now, none of the bp_catchpoint breakpoints need to
3582 do anything at this point. In the future, if some of
3583 the catchpoints need to something, we will need to add
3584 a new method, and call this method from here. */
3585 continue;
3586 }
3587
3588 /* bp_finish is a special case. The only way we ought to be able
3589 to see one of these when an exec() has happened, is if the user
3590 caught a vfork, and then said "finish". Ordinarily a finish just
3591 carries them to the call-site of the current callee, by setting
3592 a temporary bp there and resuming. But in this case, the finish
3593 will carry them entirely through the vfork & exec.
3594
3595 We don't want to allow a bp_finish to remain inserted now. But
3596 we can't safely delete it, 'cause finish_command has a handle to
3597 the bp on a bpstat, and will later want to delete it. There's a
3598 chance (and I've seen it happen) that if we delete the bp_finish
3599 here, that its storage will get reused by the time finish_command
3600 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3601 We really must allow finish_command to delete a bp_finish.
3602
3603 In the absence of a general solution for the "how do we know
3604 it's safe to delete something others may have handles to?"
3605 problem, what we'll do here is just uninsert the bp_finish, and
3606 let finish_command delete it.
3607
3608 (We know the bp_finish is "doomed" in the sense that it's
3609 momentary, and will be deleted as soon as finish_command sees
3610 the inferior stopped. So it doesn't matter that the bp's
3611 address is probably bogus in the new a.out, unlike e.g., the
3612 solib breakpoints.) */
3613
3614 if (b->type == bp_finish)
3615 {
3616 continue;
3617 }
3618
3619 /* Without a symbolic address, we have little hope of the
3620 pre-exec() address meaning the same thing in the post-exec()
3621 a.out. */
3622 if (breakpoint_event_location_empty_p (b))
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
3627 }
3628 }
3629
3630 int
3631 detach_breakpoints (ptid_t ptid)
3632 {
3633 struct bp_location *bl, **blp_tmp;
3634 int val = 0;
3635 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3636 struct inferior *inf = current_inferior ();
3637
3638 if (ptid.pid () == inferior_ptid.pid ())
3639 error (_("Cannot detach breakpoints of inferior_ptid"));
3640
3641 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3642 inferior_ptid = ptid;
3643 ALL_BP_LOCATIONS (bl, blp_tmp)
3644 {
3645 if (bl->pspace != inf->pspace)
3646 continue;
3647
3648 /* This function must physically remove breakpoints locations
3649 from the specified ptid, without modifying the breakpoint
3650 package's state. Locations of type bp_loc_other are only
3651 maintained at GDB side. So, there is no need to remove
3652 these bp_loc_other locations. Moreover, removing these
3653 would modify the breakpoint package's state. */
3654 if (bl->loc_type == bp_loc_other)
3655 continue;
3656
3657 if (bl->inserted)
3658 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3659 }
3660
3661 return val;
3662 }
3663
3664 /* Remove the breakpoint location BL from the current address space.
3665 Note that this is used to detach breakpoints from a child fork.
3666 When we get here, the child isn't in the inferior list, and neither
3667 do we have objects to represent its address space --- we should
3668 *not* look at bl->pspace->aspace here. */
3669
3670 static int
3671 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3672 {
3673 int val;
3674
3675 /* BL is never in moribund_locations by our callers. */
3676 gdb_assert (bl->owner != NULL);
3677
3678 /* The type of none suggests that owner is actually deleted.
3679 This should not ever happen. */
3680 gdb_assert (bl->owner->type != bp_none);
3681
3682 if (bl->loc_type == bp_loc_software_breakpoint
3683 || bl->loc_type == bp_loc_hardware_breakpoint)
3684 {
3685 /* "Normal" instruction breakpoint: either the standard
3686 trap-instruction bp (bp_breakpoint), or a
3687 bp_hardware_breakpoint. */
3688
3689 /* First check to see if we have to handle an overlay. */
3690 if (overlay_debugging == ovly_off
3691 || bl->section == NULL
3692 || !(section_is_overlay (bl->section)))
3693 {
3694 /* No overlay handling: just remove the breakpoint. */
3695
3696 /* If we're trying to uninsert a memory breakpoint that we
3697 know is set in a dynamic object that is marked
3698 shlib_disabled, then either the dynamic object was
3699 removed with "remove-symbol-file" or with
3700 "nosharedlibrary". In the former case, we don't know
3701 whether another dynamic object might have loaded over the
3702 breakpoint's address -- the user might well let us know
3703 about it next with add-symbol-file (the whole point of
3704 add-symbol-file is letting the user manually maintain a
3705 list of dynamically loaded objects). If we have the
3706 breakpoint's shadow memory, that is, this is a software
3707 breakpoint managed by GDB, check whether the breakpoint
3708 is still inserted in memory, to avoid overwriting wrong
3709 code with stale saved shadow contents. Note that HW
3710 breakpoints don't have shadow memory, as they're
3711 implemented using a mechanism that is not dependent on
3712 being able to modify the target's memory, and as such
3713 they should always be removed. */
3714 if (bl->shlib_disabled
3715 && bl->target_info.shadow_len != 0
3716 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3717 val = 0;
3718 else
3719 val = bl->owner->ops->remove_location (bl, reason);
3720 }
3721 else
3722 {
3723 /* This breakpoint is in an overlay section.
3724 Did we set a breakpoint at the LMA? */
3725 if (!overlay_events_enabled)
3726 {
3727 /* Yes -- overlay event support is not active, so we
3728 should have set a breakpoint at the LMA. Remove it.
3729 */
3730 /* Ignore any failures: if the LMA is in ROM, we will
3731 have already warned when we failed to insert it. */
3732 if (bl->loc_type == bp_loc_hardware_breakpoint)
3733 target_remove_hw_breakpoint (bl->gdbarch,
3734 &bl->overlay_target_info);
3735 else
3736 target_remove_breakpoint (bl->gdbarch,
3737 &bl->overlay_target_info,
3738 reason);
3739 }
3740 /* Did we set a breakpoint at the VMA?
3741 If so, we will have marked the breakpoint 'inserted'. */
3742 if (bl->inserted)
3743 {
3744 /* Yes -- remove it. Previously we did not bother to
3745 remove the breakpoint if the section had been
3746 unmapped, but let's not rely on that being safe. We
3747 don't know what the overlay manager might do. */
3748
3749 /* However, we should remove *software* breakpoints only
3750 if the section is still mapped, or else we overwrite
3751 wrong code with the saved shadow contents. */
3752 if (bl->loc_type == bp_loc_hardware_breakpoint
3753 || section_is_mapped (bl->section))
3754 val = bl->owner->ops->remove_location (bl, reason);
3755 else
3756 val = 0;
3757 }
3758 else
3759 {
3760 /* No -- not inserted, so no need to remove. No error. */
3761 val = 0;
3762 }
3763 }
3764
3765 /* In some cases, we might not be able to remove a breakpoint in
3766 a shared library that has already been removed, but we have
3767 not yet processed the shlib unload event. Similarly for an
3768 unloaded add-symbol-file object - the user might not yet have
3769 had the chance to remove-symbol-file it. shlib_disabled will
3770 be set if the library/object has already been removed, but
3771 the breakpoint hasn't been uninserted yet, e.g., after
3772 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3773 always-inserted mode. */
3774 if (val
3775 && (bl->loc_type == bp_loc_software_breakpoint
3776 && (bl->shlib_disabled
3777 || solib_name_from_address (bl->pspace, bl->address)
3778 || shared_objfile_contains_address_p (bl->pspace,
3779 bl->address))))
3780 val = 0;
3781
3782 if (val)
3783 return val;
3784 bl->inserted = (reason == DETACH_BREAKPOINT);
3785 }
3786 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3787 {
3788 gdb_assert (bl->owner->ops != NULL
3789 && bl->owner->ops->remove_location != NULL);
3790
3791 bl->inserted = (reason == DETACH_BREAKPOINT);
3792 bl->owner->ops->remove_location (bl, reason);
3793
3794 /* Failure to remove any of the hardware watchpoints comes here. */
3795 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3796 warning (_("Could not remove hardware watchpoint %d."),
3797 bl->owner->number);
3798 }
3799 else if (bl->owner->type == bp_catchpoint
3800 && breakpoint_enabled (bl->owner)
3801 && !bl->duplicate)
3802 {
3803 gdb_assert (bl->owner->ops != NULL
3804 && bl->owner->ops->remove_location != NULL);
3805
3806 val = bl->owner->ops->remove_location (bl, reason);
3807 if (val)
3808 return val;
3809
3810 bl->inserted = (reason == DETACH_BREAKPOINT);
3811 }
3812
3813 return 0;
3814 }
3815
3816 static int
3817 remove_breakpoint (struct bp_location *bl)
3818 {
3819 /* BL is never in moribund_locations by our callers. */
3820 gdb_assert (bl->owner != NULL);
3821
3822 /* The type of none suggests that owner is actually deleted.
3823 This should not ever happen. */
3824 gdb_assert (bl->owner->type != bp_none);
3825
3826 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3827
3828 switch_to_program_space_and_thread (bl->pspace);
3829
3830 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3831 }
3832
3833 /* Clear the "inserted" flag in all breakpoints. */
3834
3835 void
3836 mark_breakpoints_out (void)
3837 {
3838 struct bp_location *bl, **blp_tmp;
3839
3840 ALL_BP_LOCATIONS (bl, blp_tmp)
3841 if (bl->pspace == current_program_space)
3842 bl->inserted = 0;
3843 }
3844
3845 /* Clear the "inserted" flag in all breakpoints and delete any
3846 breakpoints which should go away between runs of the program.
3847
3848 Plus other such housekeeping that has to be done for breakpoints
3849 between runs.
3850
3851 Note: this function gets called at the end of a run (by
3852 generic_mourn_inferior) and when a run begins (by
3853 init_wait_for_inferior). */
3854
3855
3856
3857 void
3858 breakpoint_init_inferior (enum inf_context context)
3859 {
3860 struct breakpoint *b, *b_tmp;
3861 struct program_space *pspace = current_program_space;
3862
3863 /* If breakpoint locations are shared across processes, then there's
3864 nothing to do. */
3865 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3866 return;
3867
3868 mark_breakpoints_out ();
3869
3870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3871 {
3872 if (b->loc && b->loc->pspace != pspace)
3873 continue;
3874
3875 switch (b->type)
3876 {
3877 case bp_call_dummy:
3878 case bp_longjmp_call_dummy:
3879
3880 /* If the call dummy breakpoint is at the entry point it will
3881 cause problems when the inferior is rerun, so we better get
3882 rid of it. */
3883
3884 case bp_watchpoint_scope:
3885
3886 /* Also get rid of scope breakpoints. */
3887
3888 case bp_shlib_event:
3889
3890 /* Also remove solib event breakpoints. Their addresses may
3891 have changed since the last time we ran the program.
3892 Actually we may now be debugging against different target;
3893 and so the solib backend that installed this breakpoint may
3894 not be used in by the target. E.g.,
3895
3896 (gdb) file prog-linux
3897 (gdb) run # native linux target
3898 ...
3899 (gdb) kill
3900 (gdb) file prog-win.exe
3901 (gdb) tar rem :9999 # remote Windows gdbserver.
3902 */
3903
3904 case bp_step_resume:
3905
3906 /* Also remove step-resume breakpoints. */
3907
3908 case bp_single_step:
3909
3910 /* Also remove single-step breakpoints. */
3911
3912 delete_breakpoint (b);
3913 break;
3914
3915 case bp_watchpoint:
3916 case bp_hardware_watchpoint:
3917 case bp_read_watchpoint:
3918 case bp_access_watchpoint:
3919 {
3920 struct watchpoint *w = (struct watchpoint *) b;
3921
3922 /* Likewise for watchpoints on local expressions. */
3923 if (w->exp_valid_block != NULL)
3924 delete_breakpoint (b);
3925 else
3926 {
3927 /* Get rid of existing locations, which are no longer
3928 valid. New ones will be created in
3929 update_watchpoint, when the inferior is restarted.
3930 The next update_global_location_list call will
3931 garbage collect them. */
3932 b->loc = NULL;
3933
3934 if (context == inf_starting)
3935 {
3936 /* Reset val field to force reread of starting value in
3937 insert_breakpoints. */
3938 w->val.reset (nullptr);
3939 w->val_valid = 0;
3940 }
3941 }
3942 }
3943 break;
3944 default:
3945 break;
3946 }
3947 }
3948
3949 /* Get rid of the moribund locations. */
3950 for (bp_location *bl : moribund_locations)
3951 decref_bp_location (&bl);
3952 moribund_locations.clear ();
3953 }
3954
3955 /* These functions concern about actual breakpoints inserted in the
3956 target --- to e.g. check if we need to do decr_pc adjustment or if
3957 we need to hop over the bkpt --- so we check for address space
3958 match, not program space. */
3959
3960 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3961 exists at PC. It returns ordinary_breakpoint_here if it's an
3962 ordinary breakpoint, or permanent_breakpoint_here if it's a
3963 permanent breakpoint.
3964 - When continuing from a location with an ordinary breakpoint, we
3965 actually single step once before calling insert_breakpoints.
3966 - When continuing from a location with a permanent breakpoint, we
3967 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3968 the target, to advance the PC past the breakpoint. */
3969
3970 enum breakpoint_here
3971 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
3972 {
3973 struct bp_location *bl, **blp_tmp;
3974 int any_breakpoint_here = 0;
3975
3976 ALL_BP_LOCATIONS (bl, blp_tmp)
3977 {
3978 if (bl->loc_type != bp_loc_software_breakpoint
3979 && bl->loc_type != bp_loc_hardware_breakpoint)
3980 continue;
3981
3982 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3983 if ((breakpoint_enabled (bl->owner)
3984 || bl->permanent)
3985 && breakpoint_location_address_match (bl, aspace, pc))
3986 {
3987 if (overlay_debugging
3988 && section_is_overlay (bl->section)
3989 && !section_is_mapped (bl->section))
3990 continue; /* unmapped overlay -- can't be a match */
3991 else if (bl->permanent)
3992 return permanent_breakpoint_here;
3993 else
3994 any_breakpoint_here = 1;
3995 }
3996 }
3997
3998 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
3999 }
4000
4001 /* See breakpoint.h. */
4002
4003 int
4004 breakpoint_in_range_p (const address_space *aspace,
4005 CORE_ADDR addr, ULONGEST len)
4006 {
4007 struct bp_location *bl, **blp_tmp;
4008
4009 ALL_BP_LOCATIONS (bl, blp_tmp)
4010 {
4011 if (bl->loc_type != bp_loc_software_breakpoint
4012 && bl->loc_type != bp_loc_hardware_breakpoint)
4013 continue;
4014
4015 if ((breakpoint_enabled (bl->owner)
4016 || bl->permanent)
4017 && breakpoint_location_address_range_overlap (bl, aspace,
4018 addr, len))
4019 {
4020 if (overlay_debugging
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
4023 {
4024 /* Unmapped overlay -- can't be a match. */
4025 continue;
4026 }
4027
4028 return 1;
4029 }
4030 }
4031
4032 return 0;
4033 }
4034
4035 /* Return true if there's a moribund breakpoint at PC. */
4036
4037 int
4038 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4039 {
4040 for (bp_location *loc : moribund_locations)
4041 if (breakpoint_location_address_match (loc, aspace, pc))
4042 return 1;
4043
4044 return 0;
4045 }
4046
4047 /* Returns non-zero iff BL is inserted at PC, in address space
4048 ASPACE. */
4049
4050 static int
4051 bp_location_inserted_here_p (struct bp_location *bl,
4052 const address_space *aspace, CORE_ADDR pc)
4053 {
4054 if (bl->inserted
4055 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4056 aspace, pc))
4057 {
4058 if (overlay_debugging
4059 && section_is_overlay (bl->section)
4060 && !section_is_mapped (bl->section))
4061 return 0; /* unmapped overlay -- can't be a match */
4062 else
4063 return 1;
4064 }
4065 return 0;
4066 }
4067
4068 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4069
4070 int
4071 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4072 {
4073 struct bp_location **blp, **blp_tmp = NULL;
4074
4075 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4076 {
4077 struct bp_location *bl = *blp;
4078
4079 if (bl->loc_type != bp_loc_software_breakpoint
4080 && bl->loc_type != bp_loc_hardware_breakpoint)
4081 continue;
4082
4083 if (bp_location_inserted_here_p (bl, aspace, pc))
4084 return 1;
4085 }
4086 return 0;
4087 }
4088
4089 /* This function returns non-zero iff there is a software breakpoint
4090 inserted at PC. */
4091
4092 int
4093 software_breakpoint_inserted_here_p (const address_space *aspace,
4094 CORE_ADDR pc)
4095 {
4096 struct bp_location **blp, **blp_tmp = NULL;
4097
4098 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4099 {
4100 struct bp_location *bl = *blp;
4101
4102 if (bl->loc_type != bp_loc_software_breakpoint)
4103 continue;
4104
4105 if (bp_location_inserted_here_p (bl, aspace, pc))
4106 return 1;
4107 }
4108
4109 return 0;
4110 }
4111
4112 /* See breakpoint.h. */
4113
4114 int
4115 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4116 CORE_ADDR pc)
4117 {
4118 struct bp_location **blp, **blp_tmp = NULL;
4119
4120 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4121 {
4122 struct bp_location *bl = *blp;
4123
4124 if (bl->loc_type != bp_loc_hardware_breakpoint)
4125 continue;
4126
4127 if (bp_location_inserted_here_p (bl, aspace, pc))
4128 return 1;
4129 }
4130
4131 return 0;
4132 }
4133
4134 int
4135 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4136 CORE_ADDR addr, ULONGEST len)
4137 {
4138 struct breakpoint *bpt;
4139
4140 ALL_BREAKPOINTS (bpt)
4141 {
4142 struct bp_location *loc;
4143
4144 if (bpt->type != bp_hardware_watchpoint
4145 && bpt->type != bp_access_watchpoint)
4146 continue;
4147
4148 if (!breakpoint_enabled (bpt))
4149 continue;
4150
4151 for (loc = bpt->loc; loc; loc = loc->next)
4152 if (loc->pspace->aspace == aspace && loc->inserted)
4153 {
4154 CORE_ADDR l, h;
4155
4156 /* Check for intersection. */
4157 l = std::max<CORE_ADDR> (loc->address, addr);
4158 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4159 if (l < h)
4160 return 1;
4161 }
4162 }
4163 return 0;
4164 }
4165 \f
4166
4167 /* bpstat stuff. External routines' interfaces are documented
4168 in breakpoint.h. */
4169
4170 int
4171 is_catchpoint (struct breakpoint *ep)
4172 {
4173 return (ep->type == bp_catchpoint);
4174 }
4175
4176 /* Frees any storage that is part of a bpstat. Does not walk the
4177 'next' chain. */
4178
4179 bpstats::~bpstats ()
4180 {
4181 if (bp_location_at != NULL)
4182 decref_bp_location (&bp_location_at);
4183 }
4184
4185 /* Clear a bpstat so that it says we are not at any breakpoint.
4186 Also free any storage that is part of a bpstat. */
4187
4188 void
4189 bpstat_clear (bpstat *bsp)
4190 {
4191 bpstat p;
4192 bpstat q;
4193
4194 if (bsp == 0)
4195 return;
4196 p = *bsp;
4197 while (p != NULL)
4198 {
4199 q = p->next;
4200 delete p;
4201 p = q;
4202 }
4203 *bsp = NULL;
4204 }
4205
4206 bpstats::bpstats (const bpstats &other)
4207 : next (NULL),
4208 bp_location_at (other.bp_location_at),
4209 breakpoint_at (other.breakpoint_at),
4210 commands (other.commands),
4211 print (other.print),
4212 stop (other.stop),
4213 print_it (other.print_it)
4214 {
4215 if (other.old_val != NULL)
4216 old_val = release_value (value_copy (other.old_val.get ()));
4217 incref_bp_location (bp_location_at);
4218 }
4219
4220 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4221 is part of the bpstat is copied as well. */
4222
4223 bpstat
4224 bpstat_copy (bpstat bs)
4225 {
4226 bpstat p = NULL;
4227 bpstat tmp;
4228 bpstat retval = NULL;
4229
4230 if (bs == NULL)
4231 return bs;
4232
4233 for (; bs != NULL; bs = bs->next)
4234 {
4235 tmp = new bpstats (*bs);
4236
4237 if (p == NULL)
4238 /* This is the first thing in the chain. */
4239 retval = tmp;
4240 else
4241 p->next = tmp;
4242 p = tmp;
4243 }
4244 p->next = NULL;
4245 return retval;
4246 }
4247
4248 /* Find the bpstat associated with this breakpoint. */
4249
4250 bpstat
4251 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4252 {
4253 if (bsp == NULL)
4254 return NULL;
4255
4256 for (; bsp != NULL; bsp = bsp->next)
4257 {
4258 if (bsp->breakpoint_at == breakpoint)
4259 return bsp;
4260 }
4261 return NULL;
4262 }
4263
4264 /* See breakpoint.h. */
4265
4266 int
4267 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4268 {
4269 for (; bsp != NULL; bsp = bsp->next)
4270 {
4271 if (bsp->breakpoint_at == NULL)
4272 {
4273 /* A moribund location can never explain a signal other than
4274 GDB_SIGNAL_TRAP. */
4275 if (sig == GDB_SIGNAL_TRAP)
4276 return 1;
4277 }
4278 else
4279 {
4280 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4281 sig))
4282 return 1;
4283 }
4284 }
4285
4286 return 0;
4287 }
4288
4289 /* Put in *NUM the breakpoint number of the first breakpoint we are
4290 stopped at. *BSP upon return is a bpstat which points to the
4291 remaining breakpoints stopped at (but which is not guaranteed to be
4292 good for anything but further calls to bpstat_num).
4293
4294 Return 0 if passed a bpstat which does not indicate any breakpoints.
4295 Return -1 if stopped at a breakpoint that has been deleted since
4296 we set it.
4297 Return 1 otherwise. */
4298
4299 int
4300 bpstat_num (bpstat *bsp, int *num)
4301 {
4302 struct breakpoint *b;
4303
4304 if ((*bsp) == NULL)
4305 return 0; /* No more breakpoint values */
4306
4307 /* We assume we'll never have several bpstats that correspond to a
4308 single breakpoint -- otherwise, this function might return the
4309 same number more than once and this will look ugly. */
4310 b = (*bsp)->breakpoint_at;
4311 *bsp = (*bsp)->next;
4312 if (b == NULL)
4313 return -1; /* breakpoint that's been deleted since */
4314
4315 *num = b->number; /* We have its number */
4316 return 1;
4317 }
4318
4319 /* See breakpoint.h. */
4320
4321 void
4322 bpstat_clear_actions (void)
4323 {
4324 bpstat bs;
4325
4326 if (inferior_ptid == null_ptid)
4327 return;
4328
4329 thread_info *tp = inferior_thread ();
4330 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4331 {
4332 bs->commands = NULL;
4333 bs->old_val.reset (nullptr);
4334 }
4335 }
4336
4337 /* Called when a command is about to proceed the inferior. */
4338
4339 static void
4340 breakpoint_about_to_proceed (void)
4341 {
4342 if (inferior_ptid != null_ptid)
4343 {
4344 struct thread_info *tp = inferior_thread ();
4345
4346 /* Allow inferior function calls in breakpoint commands to not
4347 interrupt the command list. When the call finishes
4348 successfully, the inferior will be standing at the same
4349 breakpoint as if nothing happened. */
4350 if (tp->control.in_infcall)
4351 return;
4352 }
4353
4354 breakpoint_proceeded = 1;
4355 }
4356
4357 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4358 or its equivalent. */
4359
4360 static int
4361 command_line_is_silent (struct command_line *cmd)
4362 {
4363 return cmd && (strcmp ("silent", cmd->line) == 0);
4364 }
4365
4366 /* Execute all the commands associated with all the breakpoints at
4367 this location. Any of these commands could cause the process to
4368 proceed beyond this point, etc. We look out for such changes by
4369 checking the global "breakpoint_proceeded" after each command.
4370
4371 Returns true if a breakpoint command resumed the inferior. In that
4372 case, it is the caller's responsibility to recall it again with the
4373 bpstat of the current thread. */
4374
4375 static int
4376 bpstat_do_actions_1 (bpstat *bsp)
4377 {
4378 bpstat bs;
4379 int again = 0;
4380
4381 /* Avoid endless recursion if a `source' command is contained
4382 in bs->commands. */
4383 if (executing_breakpoint_commands)
4384 return 0;
4385
4386 scoped_restore save_executing
4387 = make_scoped_restore (&executing_breakpoint_commands, 1);
4388
4389 scoped_restore preventer = prevent_dont_repeat ();
4390
4391 /* This pointer will iterate over the list of bpstat's. */
4392 bs = *bsp;
4393
4394 breakpoint_proceeded = 0;
4395 for (; bs != NULL; bs = bs->next)
4396 {
4397 struct command_line *cmd = NULL;
4398
4399 /* Take ownership of the BSP's command tree, if it has one.
4400
4401 The command tree could legitimately contain commands like
4402 'step' and 'next', which call clear_proceed_status, which
4403 frees stop_bpstat's command tree. To make sure this doesn't
4404 free the tree we're executing out from under us, we need to
4405 take ownership of the tree ourselves. Since a given bpstat's
4406 commands are only executed once, we don't need to copy it; we
4407 can clear the pointer in the bpstat, and make sure we free
4408 the tree when we're done. */
4409 counted_command_line ccmd = bs->commands;
4410 bs->commands = NULL;
4411 if (ccmd != NULL)
4412 cmd = ccmd.get ();
4413 if (command_line_is_silent (cmd))
4414 {
4415 /* The action has been already done by bpstat_stop_status. */
4416 cmd = cmd->next;
4417 }
4418
4419 while (cmd != NULL)
4420 {
4421 execute_control_command (cmd);
4422
4423 if (breakpoint_proceeded)
4424 break;
4425 else
4426 cmd = cmd->next;
4427 }
4428
4429 if (breakpoint_proceeded)
4430 {
4431 if (current_ui->async)
4432 /* If we are in async mode, then the target might be still
4433 running, not stopped at any breakpoint, so nothing for
4434 us to do here -- just return to the event loop. */
4435 ;
4436 else
4437 /* In sync mode, when execute_control_command returns
4438 we're already standing on the next breakpoint.
4439 Breakpoint commands for that stop were not run, since
4440 execute_command does not run breakpoint commands --
4441 only command_line_handler does, but that one is not
4442 involved in execution of breakpoint commands. So, we
4443 can now execute breakpoint commands. It should be
4444 noted that making execute_command do bpstat actions is
4445 not an option -- in this case we'll have recursive
4446 invocation of bpstat for each breakpoint with a
4447 command, and can easily blow up GDB stack. Instead, we
4448 return true, which will trigger the caller to recall us
4449 with the new stop_bpstat. */
4450 again = 1;
4451 break;
4452 }
4453 }
4454 return again;
4455 }
4456
4457 /* Helper for bpstat_do_actions. Get the current thread, if there's
4458 one, is alive and has execution. Return NULL otherwise. */
4459
4460 static thread_info *
4461 get_bpstat_thread ()
4462 {
4463 if (inferior_ptid == null_ptid || !target_has_execution)
4464 return NULL;
4465
4466 thread_info *tp = inferior_thread ();
4467 if (tp->state == THREAD_EXITED || tp->executing)
4468 return NULL;
4469 return tp;
4470 }
4471
4472 void
4473 bpstat_do_actions (void)
4474 {
4475 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4476 thread_info *tp;
4477
4478 /* Do any commands attached to breakpoint we are stopped at. */
4479 while ((tp = get_bpstat_thread ()) != NULL)
4480 {
4481 /* Since in sync mode, bpstat_do_actions may resume the
4482 inferior, and only return when it is stopped at the next
4483 breakpoint, we keep doing breakpoint actions until it returns
4484 false to indicate the inferior was not resumed. */
4485 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4486 break;
4487 }
4488
4489 discard_cleanups (cleanup_if_error);
4490 }
4491
4492 /* Print out the (old or new) value associated with a watchpoint. */
4493
4494 static void
4495 watchpoint_value_print (struct value *val, struct ui_file *stream)
4496 {
4497 if (val == NULL)
4498 fprintf_unfiltered (stream, _("<unreadable>"));
4499 else
4500 {
4501 struct value_print_options opts;
4502 get_user_print_options (&opts);
4503 value_print (val, stream, &opts);
4504 }
4505 }
4506
4507 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4508 debugging multiple threads. */
4509
4510 void
4511 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4512 {
4513 if (uiout->is_mi_like_p ())
4514 return;
4515
4516 uiout->text ("\n");
4517
4518 if (show_thread_that_caused_stop ())
4519 {
4520 const char *name;
4521 struct thread_info *thr = inferior_thread ();
4522
4523 uiout->text ("Thread ");
4524 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4525
4526 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4527 if (name != NULL)
4528 {
4529 uiout->text (" \"");
4530 uiout->field_fmt ("name", "%s", name);
4531 uiout->text ("\"");
4532 }
4533
4534 uiout->text (" hit ");
4535 }
4536 }
4537
4538 /* Generic routine for printing messages indicating why we
4539 stopped. The behavior of this function depends on the value
4540 'print_it' in the bpstat structure. Under some circumstances we
4541 may decide not to print anything here and delegate the task to
4542 normal_stop(). */
4543
4544 static enum print_stop_action
4545 print_bp_stop_message (bpstat bs)
4546 {
4547 switch (bs->print_it)
4548 {
4549 case print_it_noop:
4550 /* Nothing should be printed for this bpstat entry. */
4551 return PRINT_UNKNOWN;
4552 break;
4553
4554 case print_it_done:
4555 /* We still want to print the frame, but we already printed the
4556 relevant messages. */
4557 return PRINT_SRC_AND_LOC;
4558 break;
4559
4560 case print_it_normal:
4561 {
4562 struct breakpoint *b = bs->breakpoint_at;
4563
4564 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4565 which has since been deleted. */
4566 if (b == NULL)
4567 return PRINT_UNKNOWN;
4568
4569 /* Normal case. Call the breakpoint's print_it method. */
4570 return b->ops->print_it (bs);
4571 }
4572 break;
4573
4574 default:
4575 internal_error (__FILE__, __LINE__,
4576 _("print_bp_stop_message: unrecognized enum value"));
4577 break;
4578 }
4579 }
4580
4581 /* A helper function that prints a shared library stopped event. */
4582
4583 static void
4584 print_solib_event (int is_catchpoint)
4585 {
4586 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4587 bool any_added = !current_program_space->added_solibs.empty ();
4588
4589 if (!is_catchpoint)
4590 {
4591 if (any_added || any_deleted)
4592 current_uiout->text (_("Stopped due to shared library event:\n"));
4593 else
4594 current_uiout->text (_("Stopped due to shared library event (no "
4595 "libraries added or removed)\n"));
4596 }
4597
4598 if (current_uiout->is_mi_like_p ())
4599 current_uiout->field_string ("reason",
4600 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4601
4602 if (any_deleted)
4603 {
4604 current_uiout->text (_(" Inferior unloaded "));
4605 ui_out_emit_list list_emitter (current_uiout, "removed");
4606 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4607 {
4608 const std::string &name = current_program_space->deleted_solibs[ix];
4609
4610 if (ix > 0)
4611 current_uiout->text (" ");
4612 current_uiout->field_string ("library", name);
4613 current_uiout->text ("\n");
4614 }
4615 }
4616
4617 if (any_added)
4618 {
4619 current_uiout->text (_(" Inferior loaded "));
4620 ui_out_emit_list list_emitter (current_uiout, "added");
4621 bool first = true;
4622 for (so_list *iter : current_program_space->added_solibs)
4623 {
4624 if (!first)
4625 current_uiout->text (" ");
4626 first = false;
4627 current_uiout->field_string ("library", iter->so_name);
4628 current_uiout->text ("\n");
4629 }
4630 }
4631 }
4632
4633 /* Print a message indicating what happened. This is called from
4634 normal_stop(). The input to this routine is the head of the bpstat
4635 list - a list of the eventpoints that caused this stop. KIND is
4636 the target_waitkind for the stopping event. This
4637 routine calls the generic print routine for printing a message
4638 about reasons for stopping. This will print (for example) the
4639 "Breakpoint n," part of the output. The return value of this
4640 routine is one of:
4641
4642 PRINT_UNKNOWN: Means we printed nothing.
4643 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4644 code to print the location. An example is
4645 "Breakpoint 1, " which should be followed by
4646 the location.
4647 PRINT_SRC_ONLY: Means we printed something, but there is no need
4648 to also print the location part of the message.
4649 An example is the catch/throw messages, which
4650 don't require a location appended to the end.
4651 PRINT_NOTHING: We have done some printing and we don't need any
4652 further info to be printed. */
4653
4654 enum print_stop_action
4655 bpstat_print (bpstat bs, int kind)
4656 {
4657 enum print_stop_action val;
4658
4659 /* Maybe another breakpoint in the chain caused us to stop.
4660 (Currently all watchpoints go on the bpstat whether hit or not.
4661 That probably could (should) be changed, provided care is taken
4662 with respect to bpstat_explains_signal). */
4663 for (; bs; bs = bs->next)
4664 {
4665 val = print_bp_stop_message (bs);
4666 if (val == PRINT_SRC_ONLY
4667 || val == PRINT_SRC_AND_LOC
4668 || val == PRINT_NOTHING)
4669 return val;
4670 }
4671
4672 /* If we had hit a shared library event breakpoint,
4673 print_bp_stop_message would print out this message. If we hit an
4674 OS-level shared library event, do the same thing. */
4675 if (kind == TARGET_WAITKIND_LOADED)
4676 {
4677 print_solib_event (0);
4678 return PRINT_NOTHING;
4679 }
4680
4681 /* We reached the end of the chain, or we got a null BS to start
4682 with and nothing was printed. */
4683 return PRINT_UNKNOWN;
4684 }
4685
4686 /* Evaluate the boolean expression EXP and return the result. */
4687
4688 static bool
4689 breakpoint_cond_eval (expression *exp)
4690 {
4691 struct value *mark = value_mark ();
4692 bool res = value_true (evaluate_expression (exp));
4693
4694 value_free_to_mark (mark);
4695 return res;
4696 }
4697
4698 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4699
4700 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4701 : next (NULL),
4702 bp_location_at (bl),
4703 breakpoint_at (bl->owner),
4704 commands (NULL),
4705 print (0),
4706 stop (0),
4707 print_it (print_it_normal)
4708 {
4709 incref_bp_location (bl);
4710 **bs_link_pointer = this;
4711 *bs_link_pointer = &next;
4712 }
4713
4714 bpstats::bpstats ()
4715 : next (NULL),
4716 bp_location_at (NULL),
4717 breakpoint_at (NULL),
4718 commands (NULL),
4719 print (0),
4720 stop (0),
4721 print_it (print_it_normal)
4722 {
4723 }
4724 \f
4725 /* The target has stopped with waitstatus WS. Check if any hardware
4726 watchpoints have triggered, according to the target. */
4727
4728 int
4729 watchpoints_triggered (struct target_waitstatus *ws)
4730 {
4731 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4732 CORE_ADDR addr;
4733 struct breakpoint *b;
4734
4735 if (!stopped_by_watchpoint)
4736 {
4737 /* We were not stopped by a watchpoint. Mark all watchpoints
4738 as not triggered. */
4739 ALL_BREAKPOINTS (b)
4740 if (is_hardware_watchpoint (b))
4741 {
4742 struct watchpoint *w = (struct watchpoint *) b;
4743
4744 w->watchpoint_triggered = watch_triggered_no;
4745 }
4746
4747 return 0;
4748 }
4749
4750 if (!target_stopped_data_address (current_top_target (), &addr))
4751 {
4752 /* We were stopped by a watchpoint, but we don't know where.
4753 Mark all watchpoints as unknown. */
4754 ALL_BREAKPOINTS (b)
4755 if (is_hardware_watchpoint (b))
4756 {
4757 struct watchpoint *w = (struct watchpoint *) b;
4758
4759 w->watchpoint_triggered = watch_triggered_unknown;
4760 }
4761
4762 return 1;
4763 }
4764
4765 /* The target could report the data address. Mark watchpoints
4766 affected by this data address as triggered, and all others as not
4767 triggered. */
4768
4769 ALL_BREAKPOINTS (b)
4770 if (is_hardware_watchpoint (b))
4771 {
4772 struct watchpoint *w = (struct watchpoint *) b;
4773 struct bp_location *loc;
4774
4775 w->watchpoint_triggered = watch_triggered_no;
4776 for (loc = b->loc; loc; loc = loc->next)
4777 {
4778 if (is_masked_watchpoint (b))
4779 {
4780 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4781 CORE_ADDR start = loc->address & w->hw_wp_mask;
4782
4783 if (newaddr == start)
4784 {
4785 w->watchpoint_triggered = watch_triggered_yes;
4786 break;
4787 }
4788 }
4789 /* Exact match not required. Within range is sufficient. */
4790 else if (target_watchpoint_addr_within_range (current_top_target (),
4791 addr, loc->address,
4792 loc->length))
4793 {
4794 w->watchpoint_triggered = watch_triggered_yes;
4795 break;
4796 }
4797 }
4798 }
4799
4800 return 1;
4801 }
4802
4803 /* Possible return values for watchpoint_check. */
4804 enum wp_check_result
4805 {
4806 /* The watchpoint has been deleted. */
4807 WP_DELETED = 1,
4808
4809 /* The value has changed. */
4810 WP_VALUE_CHANGED = 2,
4811
4812 /* The value has not changed. */
4813 WP_VALUE_NOT_CHANGED = 3,
4814
4815 /* Ignore this watchpoint, no matter if the value changed or not. */
4816 WP_IGNORE = 4,
4817 };
4818
4819 #define BP_TEMPFLAG 1
4820 #define BP_HARDWAREFLAG 2
4821
4822 /* Evaluate watchpoint condition expression and check if its value
4823 changed. */
4824
4825 static wp_check_result
4826 watchpoint_check (bpstat bs)
4827 {
4828 struct watchpoint *b;
4829 struct frame_info *fr;
4830 int within_current_scope;
4831
4832 /* BS is built from an existing struct breakpoint. */
4833 gdb_assert (bs->breakpoint_at != NULL);
4834 b = (struct watchpoint *) bs->breakpoint_at;
4835
4836 /* If this is a local watchpoint, we only want to check if the
4837 watchpoint frame is in scope if the current thread is the thread
4838 that was used to create the watchpoint. */
4839 if (!watchpoint_in_thread_scope (b))
4840 return WP_IGNORE;
4841
4842 if (b->exp_valid_block == NULL)
4843 within_current_scope = 1;
4844 else
4845 {
4846 struct frame_info *frame = get_current_frame ();
4847 struct gdbarch *frame_arch = get_frame_arch (frame);
4848 CORE_ADDR frame_pc = get_frame_pc (frame);
4849
4850 /* stack_frame_destroyed_p() returns a non-zero value if we're
4851 still in the function but the stack frame has already been
4852 invalidated. Since we can't rely on the values of local
4853 variables after the stack has been destroyed, we are treating
4854 the watchpoint in that state as `not changed' without further
4855 checking. Don't mark watchpoints as changed if the current
4856 frame is in an epilogue - even if they are in some other
4857 frame, our view of the stack is likely to be wrong and
4858 frame_find_by_id could error out. */
4859 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4860 return WP_IGNORE;
4861
4862 fr = frame_find_by_id (b->watchpoint_frame);
4863 within_current_scope = (fr != NULL);
4864
4865 /* If we've gotten confused in the unwinder, we might have
4866 returned a frame that can't describe this variable. */
4867 if (within_current_scope)
4868 {
4869 struct symbol *function;
4870
4871 function = get_frame_function (fr);
4872 if (function == NULL
4873 || !contained_in (b->exp_valid_block,
4874 SYMBOL_BLOCK_VALUE (function)))
4875 within_current_scope = 0;
4876 }
4877
4878 if (within_current_scope)
4879 /* If we end up stopping, the current frame will get selected
4880 in normal_stop. So this call to select_frame won't affect
4881 the user. */
4882 select_frame (fr);
4883 }
4884
4885 if (within_current_scope)
4886 {
4887 /* We use value_{,free_to_}mark because it could be a *long*
4888 time before we return to the command level and call
4889 free_all_values. We can't call free_all_values because we
4890 might be in the middle of evaluating a function call. */
4891
4892 int pc = 0;
4893 struct value *mark;
4894 struct value *new_val;
4895
4896 if (is_masked_watchpoint (b))
4897 /* Since we don't know the exact trigger address (from
4898 stopped_data_address), just tell the user we've triggered
4899 a mask watchpoint. */
4900 return WP_VALUE_CHANGED;
4901
4902 mark = value_mark ();
4903 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
4904
4905 if (b->val_bitsize != 0)
4906 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4907
4908 /* We use value_equal_contents instead of value_equal because
4909 the latter coerces an array to a pointer, thus comparing just
4910 the address of the array instead of its contents. This is
4911 not what we want. */
4912 if ((b->val != NULL) != (new_val != NULL)
4913 || (b->val != NULL && !value_equal_contents (b->val.get (),
4914 new_val)))
4915 {
4916 bs->old_val = b->val;
4917 b->val = release_value (new_val);
4918 b->val_valid = 1;
4919 if (new_val != NULL)
4920 value_free_to_mark (mark);
4921 return WP_VALUE_CHANGED;
4922 }
4923 else
4924 {
4925 /* Nothing changed. */
4926 value_free_to_mark (mark);
4927 return WP_VALUE_NOT_CHANGED;
4928 }
4929 }
4930 else
4931 {
4932 /* This seems like the only logical thing to do because
4933 if we temporarily ignored the watchpoint, then when
4934 we reenter the block in which it is valid it contains
4935 garbage (in the case of a function, it may have two
4936 garbage values, one before and one after the prologue).
4937 So we can't even detect the first assignment to it and
4938 watch after that (since the garbage may or may not equal
4939 the first value assigned). */
4940 /* We print all the stop information in
4941 breakpoint_ops->print_it, but in this case, by the time we
4942 call breakpoint_ops->print_it this bp will be deleted
4943 already. So we have no choice but print the information
4944 here. */
4945
4946 SWITCH_THRU_ALL_UIS ()
4947 {
4948 struct ui_out *uiout = current_uiout;
4949
4950 if (uiout->is_mi_like_p ())
4951 uiout->field_string
4952 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4953 uiout->text ("\nWatchpoint ");
4954 uiout->field_int ("wpnum", b->number);
4955 uiout->text (" deleted because the program has left the block in\n"
4956 "which its expression is valid.\n");
4957 }
4958
4959 /* Make sure the watchpoint's commands aren't executed. */
4960 b->commands = NULL;
4961 watchpoint_del_at_next_stop (b);
4962
4963 return WP_DELETED;
4964 }
4965 }
4966
4967 /* Return true if it looks like target has stopped due to hitting
4968 breakpoint location BL. This function does not check if we should
4969 stop, only if BL explains the stop. */
4970
4971 static int
4972 bpstat_check_location (const struct bp_location *bl,
4973 const address_space *aspace, CORE_ADDR bp_addr,
4974 const struct target_waitstatus *ws)
4975 {
4976 struct breakpoint *b = bl->owner;
4977
4978 /* BL is from an existing breakpoint. */
4979 gdb_assert (b != NULL);
4980
4981 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4982 }
4983
4984 /* Determine if the watched values have actually changed, and we
4985 should stop. If not, set BS->stop to 0. */
4986
4987 static void
4988 bpstat_check_watchpoint (bpstat bs)
4989 {
4990 const struct bp_location *bl;
4991 struct watchpoint *b;
4992
4993 /* BS is built for existing struct breakpoint. */
4994 bl = bs->bp_location_at;
4995 gdb_assert (bl != NULL);
4996 b = (struct watchpoint *) bs->breakpoint_at;
4997 gdb_assert (b != NULL);
4998
4999 {
5000 int must_check_value = 0;
5001
5002 if (b->type == bp_watchpoint)
5003 /* For a software watchpoint, we must always check the
5004 watched value. */
5005 must_check_value = 1;
5006 else if (b->watchpoint_triggered == watch_triggered_yes)
5007 /* We have a hardware watchpoint (read, write, or access)
5008 and the target earlier reported an address watched by
5009 this watchpoint. */
5010 must_check_value = 1;
5011 else if (b->watchpoint_triggered == watch_triggered_unknown
5012 && b->type == bp_hardware_watchpoint)
5013 /* We were stopped by a hardware watchpoint, but the target could
5014 not report the data address. We must check the watchpoint's
5015 value. Access and read watchpoints are out of luck; without
5016 a data address, we can't figure it out. */
5017 must_check_value = 1;
5018
5019 if (must_check_value)
5020 {
5021 wp_check_result e;
5022
5023 TRY
5024 {
5025 e = watchpoint_check (bs);
5026 }
5027 CATCH (ex, RETURN_MASK_ALL)
5028 {
5029 exception_fprintf (gdb_stderr, ex,
5030 "Error evaluating expression "
5031 "for watchpoint %d\n",
5032 b->number);
5033
5034 SWITCH_THRU_ALL_UIS ()
5035 {
5036 printf_filtered (_("Watchpoint %d deleted.\n"),
5037 b->number);
5038 }
5039 watchpoint_del_at_next_stop (b);
5040 e = WP_DELETED;
5041 }
5042 END_CATCH
5043
5044 switch (e)
5045 {
5046 case WP_DELETED:
5047 /* We've already printed what needs to be printed. */
5048 bs->print_it = print_it_done;
5049 /* Stop. */
5050 break;
5051 case WP_IGNORE:
5052 bs->print_it = print_it_noop;
5053 bs->stop = 0;
5054 break;
5055 case WP_VALUE_CHANGED:
5056 if (b->type == bp_read_watchpoint)
5057 {
5058 /* There are two cases to consider here:
5059
5060 1. We're watching the triggered memory for reads.
5061 In that case, trust the target, and always report
5062 the watchpoint hit to the user. Even though
5063 reads don't cause value changes, the value may
5064 have changed since the last time it was read, and
5065 since we're not trapping writes, we will not see
5066 those, and as such we should ignore our notion of
5067 old value.
5068
5069 2. We're watching the triggered memory for both
5070 reads and writes. There are two ways this may
5071 happen:
5072
5073 2.1. This is a target that can't break on data
5074 reads only, but can break on accesses (reads or
5075 writes), such as e.g., x86. We detect this case
5076 at the time we try to insert read watchpoints.
5077
5078 2.2. Otherwise, the target supports read
5079 watchpoints, but, the user set an access or write
5080 watchpoint watching the same memory as this read
5081 watchpoint.
5082
5083 If we're watching memory writes as well as reads,
5084 ignore watchpoint hits when we find that the
5085 value hasn't changed, as reads don't cause
5086 changes. This still gives false positives when
5087 the program writes the same value to memory as
5088 what there was already in memory (we will confuse
5089 it for a read), but it's much better than
5090 nothing. */
5091
5092 int other_write_watchpoint = 0;
5093
5094 if (bl->watchpoint_type == hw_read)
5095 {
5096 struct breakpoint *other_b;
5097
5098 ALL_BREAKPOINTS (other_b)
5099 if (other_b->type == bp_hardware_watchpoint
5100 || other_b->type == bp_access_watchpoint)
5101 {
5102 struct watchpoint *other_w =
5103 (struct watchpoint *) other_b;
5104
5105 if (other_w->watchpoint_triggered
5106 == watch_triggered_yes)
5107 {
5108 other_write_watchpoint = 1;
5109 break;
5110 }
5111 }
5112 }
5113
5114 if (other_write_watchpoint
5115 || bl->watchpoint_type == hw_access)
5116 {
5117 /* We're watching the same memory for writes,
5118 and the value changed since the last time we
5119 updated it, so this trap must be for a write.
5120 Ignore it. */
5121 bs->print_it = print_it_noop;
5122 bs->stop = 0;
5123 }
5124 }
5125 break;
5126 case WP_VALUE_NOT_CHANGED:
5127 if (b->type == bp_hardware_watchpoint
5128 || b->type == bp_watchpoint)
5129 {
5130 /* Don't stop: write watchpoints shouldn't fire if
5131 the value hasn't changed. */
5132 bs->print_it = print_it_noop;
5133 bs->stop = 0;
5134 }
5135 /* Stop. */
5136 break;
5137 default:
5138 /* Can't happen. */
5139 break;
5140 }
5141 }
5142 else /* must_check_value == 0 */
5143 {
5144 /* This is a case where some watchpoint(s) triggered, but
5145 not at the address of this watchpoint, or else no
5146 watchpoint triggered after all. So don't print
5147 anything for this watchpoint. */
5148 bs->print_it = print_it_noop;
5149 bs->stop = 0;
5150 }
5151 }
5152 }
5153
5154 /* For breakpoints that are currently marked as telling gdb to stop,
5155 check conditions (condition proper, frame, thread and ignore count)
5156 of breakpoint referred to by BS. If we should not stop for this
5157 breakpoint, set BS->stop to 0. */
5158
5159 static void
5160 bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
5161 {
5162 const struct bp_location *bl;
5163 struct breakpoint *b;
5164 /* Assume stop. */
5165 bool condition_result = true;
5166 struct expression *cond;
5167
5168 gdb_assert (bs->stop);
5169
5170 /* BS is built for existing struct breakpoint. */
5171 bl = bs->bp_location_at;
5172 gdb_assert (bl != NULL);
5173 b = bs->breakpoint_at;
5174 gdb_assert (b != NULL);
5175
5176 /* Even if the target evaluated the condition on its end and notified GDB, we
5177 need to do so again since GDB does not know if we stopped due to a
5178 breakpoint or a single step breakpoint. */
5179
5180 if (frame_id_p (b->frame_id)
5181 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5182 {
5183 bs->stop = 0;
5184 return;
5185 }
5186
5187 /* If this is a thread/task-specific breakpoint, don't waste cpu
5188 evaluating the condition if this isn't the specified
5189 thread/task. */
5190 if ((b->thread != -1 && b->thread != thread->global_num)
5191 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5192 {
5193 bs->stop = 0;
5194 return;
5195 }
5196
5197 /* Evaluate extension language breakpoints that have a "stop" method
5198 implemented. */
5199 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5200
5201 if (is_watchpoint (b))
5202 {
5203 struct watchpoint *w = (struct watchpoint *) b;
5204
5205 cond = w->cond_exp.get ();
5206 }
5207 else
5208 cond = bl->cond.get ();
5209
5210 if (cond && b->disposition != disp_del_at_next_stop)
5211 {
5212 int within_current_scope = 1;
5213 struct watchpoint * w;
5214
5215 /* We use value_mark and value_free_to_mark because it could
5216 be a long time before we return to the command level and
5217 call free_all_values. We can't call free_all_values
5218 because we might be in the middle of evaluating a
5219 function call. */
5220 struct value *mark = value_mark ();
5221
5222 if (is_watchpoint (b))
5223 w = (struct watchpoint *) b;
5224 else
5225 w = NULL;
5226
5227 /* Need to select the frame, with all that implies so that
5228 the conditions will have the right context. Because we
5229 use the frame, we will not see an inlined function's
5230 variables when we arrive at a breakpoint at the start
5231 of the inlined function; the current frame will be the
5232 call site. */
5233 if (w == NULL || w->cond_exp_valid_block == NULL)
5234 select_frame (get_current_frame ());
5235 else
5236 {
5237 struct frame_info *frame;
5238
5239 /* For local watchpoint expressions, which particular
5240 instance of a local is being watched matters, so we
5241 keep track of the frame to evaluate the expression
5242 in. To evaluate the condition however, it doesn't
5243 really matter which instantiation of the function
5244 where the condition makes sense triggers the
5245 watchpoint. This allows an expression like "watch
5246 global if q > 10" set in `func', catch writes to
5247 global on all threads that call `func', or catch
5248 writes on all recursive calls of `func' by a single
5249 thread. We simply always evaluate the condition in
5250 the innermost frame that's executing where it makes
5251 sense to evaluate the condition. It seems
5252 intuitive. */
5253 frame = block_innermost_frame (w->cond_exp_valid_block);
5254 if (frame != NULL)
5255 select_frame (frame);
5256 else
5257 within_current_scope = 0;
5258 }
5259 if (within_current_scope)
5260 {
5261 TRY
5262 {
5263 condition_result = breakpoint_cond_eval (cond);
5264 }
5265 CATCH (ex, RETURN_MASK_ALL)
5266 {
5267 exception_fprintf (gdb_stderr, ex,
5268 "Error in testing breakpoint condition:\n");
5269 }
5270 END_CATCH
5271 }
5272 else
5273 {
5274 warning (_("Watchpoint condition cannot be tested "
5275 "in the current scope"));
5276 /* If we failed to set the right context for this
5277 watchpoint, unconditionally report it. */
5278 }
5279 /* FIXME-someday, should give breakpoint #. */
5280 value_free_to_mark (mark);
5281 }
5282
5283 if (cond && !condition_result)
5284 {
5285 bs->stop = 0;
5286 }
5287 else if (b->ignore_count > 0)
5288 {
5289 b->ignore_count--;
5290 bs->stop = 0;
5291 /* Increase the hit count even though we don't stop. */
5292 ++(b->hit_count);
5293 gdb::observers::breakpoint_modified.notify (b);
5294 }
5295 }
5296
5297 /* Returns true if we need to track moribund locations of LOC's type
5298 on the current target. */
5299
5300 static int
5301 need_moribund_for_location_type (struct bp_location *loc)
5302 {
5303 return ((loc->loc_type == bp_loc_software_breakpoint
5304 && !target_supports_stopped_by_sw_breakpoint ())
5305 || (loc->loc_type == bp_loc_hardware_breakpoint
5306 && !target_supports_stopped_by_hw_breakpoint ()));
5307 }
5308
5309 /* See breakpoint.h. */
5310
5311 bpstat
5312 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5313 const struct target_waitstatus *ws)
5314 {
5315 struct breakpoint *b;
5316 bpstat bs_head = NULL, *bs_link = &bs_head;
5317
5318 ALL_BREAKPOINTS (b)
5319 {
5320 if (!breakpoint_enabled (b))
5321 continue;
5322
5323 for (bp_location *bl = b->loc; bl != NULL; bl = bl->next)
5324 {
5325 /* For hardware watchpoints, we look only at the first
5326 location. The watchpoint_check function will work on the
5327 entire expression, not the individual locations. For
5328 read watchpoints, the watchpoints_triggered function has
5329 checked all locations already. */
5330 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5331 break;
5332
5333 if (!bl->enabled || bl->shlib_disabled)
5334 continue;
5335
5336 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5337 continue;
5338
5339 /* Come here if it's a watchpoint, or if the break address
5340 matches. */
5341
5342 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5343 explain stop. */
5344
5345 /* Assume we stop. Should we find a watchpoint that is not
5346 actually triggered, or if the condition of the breakpoint
5347 evaluates as false, we'll reset 'stop' to 0. */
5348 bs->stop = 1;
5349 bs->print = 1;
5350
5351 /* If this is a scope breakpoint, mark the associated
5352 watchpoint as triggered so that we will handle the
5353 out-of-scope event. We'll get to the watchpoint next
5354 iteration. */
5355 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5356 {
5357 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5358
5359 w->watchpoint_triggered = watch_triggered_yes;
5360 }
5361 }
5362 }
5363
5364 /* Check if a moribund breakpoint explains the stop. */
5365 if (!target_supports_stopped_by_sw_breakpoint ()
5366 || !target_supports_stopped_by_hw_breakpoint ())
5367 {
5368 for (bp_location *loc : moribund_locations)
5369 {
5370 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5371 && need_moribund_for_location_type (loc))
5372 {
5373 bpstat bs = new bpstats (loc, &bs_link);
5374 /* For hits of moribund locations, we should just proceed. */
5375 bs->stop = 0;
5376 bs->print = 0;
5377 bs->print_it = print_it_noop;
5378 }
5379 }
5380 }
5381
5382 return bs_head;
5383 }
5384
5385 /* See breakpoint.h. */
5386
5387 bpstat
5388 bpstat_stop_status (const address_space *aspace,
5389 CORE_ADDR bp_addr, thread_info *thread,
5390 const struct target_waitstatus *ws,
5391 bpstat stop_chain)
5392 {
5393 struct breakpoint *b = NULL;
5394 /* First item of allocated bpstat's. */
5395 bpstat bs_head = stop_chain;
5396 bpstat bs;
5397 int need_remove_insert;
5398 int removed_any;
5399
5400 /* First, build the bpstat chain with locations that explain a
5401 target stop, while being careful to not set the target running,
5402 as that may invalidate locations (in particular watchpoint
5403 locations are recreated). Resuming will happen here with
5404 breakpoint conditions or watchpoint expressions that include
5405 inferior function calls. */
5406 if (bs_head == NULL)
5407 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5408
5409 /* A bit of special processing for shlib breakpoints. We need to
5410 process solib loading here, so that the lists of loaded and
5411 unloaded libraries are correct before we handle "catch load" and
5412 "catch unload". */
5413 for (bs = bs_head; bs != NULL; bs = bs->next)
5414 {
5415 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5416 {
5417 handle_solib_event ();
5418 break;
5419 }
5420 }
5421
5422 /* Now go through the locations that caused the target to stop, and
5423 check whether we're interested in reporting this stop to higher
5424 layers, or whether we should resume the target transparently. */
5425
5426 removed_any = 0;
5427
5428 for (bs = bs_head; bs != NULL; bs = bs->next)
5429 {
5430 if (!bs->stop)
5431 continue;
5432
5433 b = bs->breakpoint_at;
5434 b->ops->check_status (bs);
5435 if (bs->stop)
5436 {
5437 bpstat_check_breakpoint_conditions (bs, thread);
5438
5439 if (bs->stop)
5440 {
5441 ++(b->hit_count);
5442 gdb::observers::breakpoint_modified.notify (b);
5443
5444 /* We will stop here. */
5445 if (b->disposition == disp_disable)
5446 {
5447 --(b->enable_count);
5448 if (b->enable_count <= 0)
5449 b->enable_state = bp_disabled;
5450 removed_any = 1;
5451 }
5452 if (b->silent)
5453 bs->print = 0;
5454 bs->commands = b->commands;
5455 if (command_line_is_silent (bs->commands
5456 ? bs->commands.get () : NULL))
5457 bs->print = 0;
5458
5459 b->ops->after_condition_true (bs);
5460 }
5461
5462 }
5463
5464 /* Print nothing for this entry if we don't stop or don't
5465 print. */
5466 if (!bs->stop || !bs->print)
5467 bs->print_it = print_it_noop;
5468 }
5469
5470 /* If we aren't stopping, the value of some hardware watchpoint may
5471 not have changed, but the intermediate memory locations we are
5472 watching may have. Don't bother if we're stopping; this will get
5473 done later. */
5474 need_remove_insert = 0;
5475 if (! bpstat_causes_stop (bs_head))
5476 for (bs = bs_head; bs != NULL; bs = bs->next)
5477 if (!bs->stop
5478 && bs->breakpoint_at
5479 && is_hardware_watchpoint (bs->breakpoint_at))
5480 {
5481 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5482
5483 update_watchpoint (w, 0 /* don't reparse. */);
5484 need_remove_insert = 1;
5485 }
5486
5487 if (need_remove_insert)
5488 update_global_location_list (UGLL_MAY_INSERT);
5489 else if (removed_any)
5490 update_global_location_list (UGLL_DONT_INSERT);
5491
5492 return bs_head;
5493 }
5494
5495 static void
5496 handle_jit_event (void)
5497 {
5498 struct frame_info *frame;
5499 struct gdbarch *gdbarch;
5500
5501 if (debug_infrun)
5502 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5503
5504 /* Switch terminal for any messages produced by
5505 breakpoint_re_set. */
5506 target_terminal::ours_for_output ();
5507
5508 frame = get_current_frame ();
5509 gdbarch = get_frame_arch (frame);
5510
5511 jit_event_handler (gdbarch);
5512
5513 target_terminal::inferior ();
5514 }
5515
5516 /* Prepare WHAT final decision for infrun. */
5517
5518 /* Decide what infrun needs to do with this bpstat. */
5519
5520 struct bpstat_what
5521 bpstat_what (bpstat bs_head)
5522 {
5523 struct bpstat_what retval;
5524 bpstat bs;
5525
5526 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5527 retval.call_dummy = STOP_NONE;
5528 retval.is_longjmp = 0;
5529
5530 for (bs = bs_head; bs != NULL; bs = bs->next)
5531 {
5532 /* Extract this BS's action. After processing each BS, we check
5533 if its action overrides all we've seem so far. */
5534 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5535 enum bptype bptype;
5536
5537 if (bs->breakpoint_at == NULL)
5538 {
5539 /* I suspect this can happen if it was a momentary
5540 breakpoint which has since been deleted. */
5541 bptype = bp_none;
5542 }
5543 else
5544 bptype = bs->breakpoint_at->type;
5545
5546 switch (bptype)
5547 {
5548 case bp_none:
5549 break;
5550 case bp_breakpoint:
5551 case bp_hardware_breakpoint:
5552 case bp_single_step:
5553 case bp_until:
5554 case bp_finish:
5555 case bp_shlib_event:
5556 if (bs->stop)
5557 {
5558 if (bs->print)
5559 this_action = BPSTAT_WHAT_STOP_NOISY;
5560 else
5561 this_action = BPSTAT_WHAT_STOP_SILENT;
5562 }
5563 else
5564 this_action = BPSTAT_WHAT_SINGLE;
5565 break;
5566 case bp_watchpoint:
5567 case bp_hardware_watchpoint:
5568 case bp_read_watchpoint:
5569 case bp_access_watchpoint:
5570 if (bs->stop)
5571 {
5572 if (bs->print)
5573 this_action = BPSTAT_WHAT_STOP_NOISY;
5574 else
5575 this_action = BPSTAT_WHAT_STOP_SILENT;
5576 }
5577 else
5578 {
5579 /* There was a watchpoint, but we're not stopping.
5580 This requires no further action. */
5581 }
5582 break;
5583 case bp_longjmp:
5584 case bp_longjmp_call_dummy:
5585 case bp_exception:
5586 if (bs->stop)
5587 {
5588 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5589 retval.is_longjmp = bptype != bp_exception;
5590 }
5591 else
5592 this_action = BPSTAT_WHAT_SINGLE;
5593 break;
5594 case bp_longjmp_resume:
5595 case bp_exception_resume:
5596 if (bs->stop)
5597 {
5598 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5599 retval.is_longjmp = bptype == bp_longjmp_resume;
5600 }
5601 else
5602 this_action = BPSTAT_WHAT_SINGLE;
5603 break;
5604 case bp_step_resume:
5605 if (bs->stop)
5606 this_action = BPSTAT_WHAT_STEP_RESUME;
5607 else
5608 {
5609 /* It is for the wrong frame. */
5610 this_action = BPSTAT_WHAT_SINGLE;
5611 }
5612 break;
5613 case bp_hp_step_resume:
5614 if (bs->stop)
5615 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5616 else
5617 {
5618 /* It is for the wrong frame. */
5619 this_action = BPSTAT_WHAT_SINGLE;
5620 }
5621 break;
5622 case bp_watchpoint_scope:
5623 case bp_thread_event:
5624 case bp_overlay_event:
5625 case bp_longjmp_master:
5626 case bp_std_terminate_master:
5627 case bp_exception_master:
5628 this_action = BPSTAT_WHAT_SINGLE;
5629 break;
5630 case bp_catchpoint:
5631 if (bs->stop)
5632 {
5633 if (bs->print)
5634 this_action = BPSTAT_WHAT_STOP_NOISY;
5635 else
5636 this_action = BPSTAT_WHAT_STOP_SILENT;
5637 }
5638 else
5639 {
5640 /* There was a catchpoint, but we're not stopping.
5641 This requires no further action. */
5642 }
5643 break;
5644 case bp_jit_event:
5645 this_action = BPSTAT_WHAT_SINGLE;
5646 break;
5647 case bp_call_dummy:
5648 /* Make sure the action is stop (silent or noisy),
5649 so infrun.c pops the dummy frame. */
5650 retval.call_dummy = STOP_STACK_DUMMY;
5651 this_action = BPSTAT_WHAT_STOP_SILENT;
5652 break;
5653 case bp_std_terminate:
5654 /* Make sure the action is stop (silent or noisy),
5655 so infrun.c pops the dummy frame. */
5656 retval.call_dummy = STOP_STD_TERMINATE;
5657 this_action = BPSTAT_WHAT_STOP_SILENT;
5658 break;
5659 case bp_tracepoint:
5660 case bp_fast_tracepoint:
5661 case bp_static_tracepoint:
5662 /* Tracepoint hits should not be reported back to GDB, and
5663 if one got through somehow, it should have been filtered
5664 out already. */
5665 internal_error (__FILE__, __LINE__,
5666 _("bpstat_what: tracepoint encountered"));
5667 break;
5668 case bp_gnu_ifunc_resolver:
5669 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5670 this_action = BPSTAT_WHAT_SINGLE;
5671 break;
5672 case bp_gnu_ifunc_resolver_return:
5673 /* The breakpoint will be removed, execution will restart from the
5674 PC of the former breakpoint. */
5675 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5676 break;
5677
5678 case bp_dprintf:
5679 if (bs->stop)
5680 this_action = BPSTAT_WHAT_STOP_SILENT;
5681 else
5682 this_action = BPSTAT_WHAT_SINGLE;
5683 break;
5684
5685 default:
5686 internal_error (__FILE__, __LINE__,
5687 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5688 }
5689
5690 retval.main_action = std::max (retval.main_action, this_action);
5691 }
5692
5693 return retval;
5694 }
5695
5696 void
5697 bpstat_run_callbacks (bpstat bs_head)
5698 {
5699 bpstat bs;
5700
5701 for (bs = bs_head; bs != NULL; bs = bs->next)
5702 {
5703 struct breakpoint *b = bs->breakpoint_at;
5704
5705 if (b == NULL)
5706 continue;
5707 switch (b->type)
5708 {
5709 case bp_jit_event:
5710 handle_jit_event ();
5711 break;
5712 case bp_gnu_ifunc_resolver:
5713 gnu_ifunc_resolver_stop (b);
5714 break;
5715 case bp_gnu_ifunc_resolver_return:
5716 gnu_ifunc_resolver_return_stop (b);
5717 break;
5718 }
5719 }
5720 }
5721
5722 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5723 without hardware support). This isn't related to a specific bpstat,
5724 just to things like whether watchpoints are set. */
5725
5726 int
5727 bpstat_should_step (void)
5728 {
5729 struct breakpoint *b;
5730
5731 ALL_BREAKPOINTS (b)
5732 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5733 return 1;
5734 return 0;
5735 }
5736
5737 int
5738 bpstat_causes_stop (bpstat bs)
5739 {
5740 for (; bs != NULL; bs = bs->next)
5741 if (bs->stop)
5742 return 1;
5743
5744 return 0;
5745 }
5746
5747 \f
5748
5749 /* Compute a string of spaces suitable to indent the next line
5750 so it starts at the position corresponding to the table column
5751 named COL_NAME in the currently active table of UIOUT. */
5752
5753 static char *
5754 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5755 {
5756 static char wrap_indent[80];
5757 int i, total_width, width, align;
5758 const char *text;
5759
5760 total_width = 0;
5761 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5762 {
5763 if (strcmp (text, col_name) == 0)
5764 {
5765 gdb_assert (total_width < sizeof wrap_indent);
5766 memset (wrap_indent, ' ', total_width);
5767 wrap_indent[total_width] = 0;
5768
5769 return wrap_indent;
5770 }
5771
5772 total_width += width + 1;
5773 }
5774
5775 return NULL;
5776 }
5777
5778 /* Determine if the locations of this breakpoint will have their conditions
5779 evaluated by the target, host or a mix of both. Returns the following:
5780
5781 "host": Host evals condition.
5782 "host or target": Host or Target evals condition.
5783 "target": Target evals condition.
5784 */
5785
5786 static const char *
5787 bp_condition_evaluator (struct breakpoint *b)
5788 {
5789 struct bp_location *bl;
5790 char host_evals = 0;
5791 char target_evals = 0;
5792
5793 if (!b)
5794 return NULL;
5795
5796 if (!is_breakpoint (b))
5797 return NULL;
5798
5799 if (gdb_evaluates_breakpoint_condition_p ()
5800 || !target_supports_evaluation_of_breakpoint_conditions ())
5801 return condition_evaluation_host;
5802
5803 for (bl = b->loc; bl; bl = bl->next)
5804 {
5805 if (bl->cond_bytecode)
5806 target_evals++;
5807 else
5808 host_evals++;
5809 }
5810
5811 if (host_evals && target_evals)
5812 return condition_evaluation_both;
5813 else if (target_evals)
5814 return condition_evaluation_target;
5815 else
5816 return condition_evaluation_host;
5817 }
5818
5819 /* Determine the breakpoint location's condition evaluator. This is
5820 similar to bp_condition_evaluator, but for locations. */
5821
5822 static const char *
5823 bp_location_condition_evaluator (struct bp_location *bl)
5824 {
5825 if (bl && !is_breakpoint (bl->owner))
5826 return NULL;
5827
5828 if (gdb_evaluates_breakpoint_condition_p ()
5829 || !target_supports_evaluation_of_breakpoint_conditions ())
5830 return condition_evaluation_host;
5831
5832 if (bl && bl->cond_bytecode)
5833 return condition_evaluation_target;
5834 else
5835 return condition_evaluation_host;
5836 }
5837
5838 /* Print the LOC location out of the list of B->LOC locations. */
5839
5840 static void
5841 print_breakpoint_location (struct breakpoint *b,
5842 struct bp_location *loc)
5843 {
5844 struct ui_out *uiout = current_uiout;
5845
5846 scoped_restore_current_program_space restore_pspace;
5847
5848 if (loc != NULL && loc->shlib_disabled)
5849 loc = NULL;
5850
5851 if (loc != NULL)
5852 set_current_program_space (loc->pspace);
5853
5854 if (b->display_canonical)
5855 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5856 else if (loc && loc->symtab)
5857 {
5858 const struct symbol *sym = loc->symbol;
5859
5860 if (sym)
5861 {
5862 uiout->text ("in ");
5863 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5864 uiout->text (" ");
5865 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5866 uiout->text ("at ");
5867 }
5868 uiout->field_string ("file",
5869 symtab_to_filename_for_display (loc->symtab));
5870 uiout->text (":");
5871
5872 if (uiout->is_mi_like_p ())
5873 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5874
5875 uiout->field_int ("line", loc->line_number);
5876 }
5877 else if (loc)
5878 {
5879 string_file stb;
5880
5881 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5882 demangle, "");
5883 uiout->field_stream ("at", stb);
5884 }
5885 else
5886 {
5887 uiout->field_string ("pending",
5888 event_location_to_string (b->location.get ()));
5889 /* If extra_string is available, it could be holding a condition
5890 or dprintf arguments. In either case, make sure it is printed,
5891 too, but only for non-MI streams. */
5892 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
5893 {
5894 if (b->type == bp_dprintf)
5895 uiout->text (",");
5896 else
5897 uiout->text (" ");
5898 uiout->text (b->extra_string);
5899 }
5900 }
5901
5902 if (loc && is_breakpoint (b)
5903 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5904 && bp_condition_evaluator (b) == condition_evaluation_both)
5905 {
5906 uiout->text (" (");
5907 uiout->field_string ("evaluated-by",
5908 bp_location_condition_evaluator (loc));
5909 uiout->text (")");
5910 }
5911 }
5912
5913 static const char *
5914 bptype_string (enum bptype type)
5915 {
5916 struct ep_type_description
5917 {
5918 enum bptype type;
5919 const char *description;
5920 };
5921 static struct ep_type_description bptypes[] =
5922 {
5923 {bp_none, "?deleted?"},
5924 {bp_breakpoint, "breakpoint"},
5925 {bp_hardware_breakpoint, "hw breakpoint"},
5926 {bp_single_step, "sw single-step"},
5927 {bp_until, "until"},
5928 {bp_finish, "finish"},
5929 {bp_watchpoint, "watchpoint"},
5930 {bp_hardware_watchpoint, "hw watchpoint"},
5931 {bp_read_watchpoint, "read watchpoint"},
5932 {bp_access_watchpoint, "acc watchpoint"},
5933 {bp_longjmp, "longjmp"},
5934 {bp_longjmp_resume, "longjmp resume"},
5935 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5936 {bp_exception, "exception"},
5937 {bp_exception_resume, "exception resume"},
5938 {bp_step_resume, "step resume"},
5939 {bp_hp_step_resume, "high-priority step resume"},
5940 {bp_watchpoint_scope, "watchpoint scope"},
5941 {bp_call_dummy, "call dummy"},
5942 {bp_std_terminate, "std::terminate"},
5943 {bp_shlib_event, "shlib events"},
5944 {bp_thread_event, "thread events"},
5945 {bp_overlay_event, "overlay events"},
5946 {bp_longjmp_master, "longjmp master"},
5947 {bp_std_terminate_master, "std::terminate master"},
5948 {bp_exception_master, "exception master"},
5949 {bp_catchpoint, "catchpoint"},
5950 {bp_tracepoint, "tracepoint"},
5951 {bp_fast_tracepoint, "fast tracepoint"},
5952 {bp_static_tracepoint, "static tracepoint"},
5953 {bp_dprintf, "dprintf"},
5954 {bp_jit_event, "jit events"},
5955 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5956 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5957 };
5958
5959 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5960 || ((int) type != bptypes[(int) type].type))
5961 internal_error (__FILE__, __LINE__,
5962 _("bptypes table does not describe type #%d."),
5963 (int) type);
5964
5965 return bptypes[(int) type].description;
5966 }
5967
5968 /* For MI, output a field named 'thread-groups' with a list as the value.
5969 For CLI, prefix the list with the string 'inf'. */
5970
5971 static void
5972 output_thread_groups (struct ui_out *uiout,
5973 const char *field_name,
5974 const std::vector<int> &inf_nums,
5975 int mi_only)
5976 {
5977 int is_mi = uiout->is_mi_like_p ();
5978
5979 /* For backward compatibility, don't display inferiors in CLI unless
5980 there are several. Always display them for MI. */
5981 if (!is_mi && mi_only)
5982 return;
5983
5984 ui_out_emit_list list_emitter (uiout, field_name);
5985
5986 for (size_t i = 0; i < inf_nums.size (); i++)
5987 {
5988 if (is_mi)
5989 {
5990 char mi_group[10];
5991
5992 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
5993 uiout->field_string (NULL, mi_group);
5994 }
5995 else
5996 {
5997 if (i == 0)
5998 uiout->text (" inf ");
5999 else
6000 uiout->text (", ");
6001
6002 uiout->text (plongest (inf_nums[i]));
6003 }
6004 }
6005 }
6006
6007 /* Print B to gdb_stdout. */
6008
6009 static void
6010 print_one_breakpoint_location (struct breakpoint *b,
6011 struct bp_location *loc,
6012 int loc_number,
6013 struct bp_location **last_loc,
6014 int allflag)
6015 {
6016 struct command_line *l;
6017 static char bpenables[] = "nynny";
6018
6019 struct ui_out *uiout = current_uiout;
6020 int header_of_multiple = 0;
6021 int part_of_multiple = (loc != NULL);
6022 struct value_print_options opts;
6023
6024 get_user_print_options (&opts);
6025
6026 gdb_assert (!loc || loc_number != 0);
6027 /* See comment in print_one_breakpoint concerning treatment of
6028 breakpoints with single disabled location. */
6029 if (loc == NULL
6030 && (b->loc != NULL
6031 && (b->loc->next != NULL || !b->loc->enabled)))
6032 header_of_multiple = 1;
6033 if (loc == NULL)
6034 loc = b->loc;
6035
6036 annotate_record ();
6037
6038 /* 1 */
6039 annotate_field (0);
6040 if (part_of_multiple)
6041 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6042 else
6043 uiout->field_int ("number", b->number);
6044
6045 /* 2 */
6046 annotate_field (1);
6047 if (part_of_multiple)
6048 uiout->field_skip ("type");
6049 else
6050 uiout->field_string ("type", bptype_string (b->type));
6051
6052 /* 3 */
6053 annotate_field (2);
6054 if (part_of_multiple)
6055 uiout->field_skip ("disp");
6056 else
6057 uiout->field_string ("disp", bpdisp_text (b->disposition));
6058
6059
6060 /* 4 */
6061 annotate_field (3);
6062 if (part_of_multiple)
6063 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6064 else
6065 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6066 uiout->spaces (2);
6067
6068
6069 /* 5 and 6 */
6070 if (b->ops != NULL && b->ops->print_one != NULL)
6071 {
6072 /* Although the print_one can possibly print all locations,
6073 calling it here is not likely to get any nice result. So,
6074 make sure there's just one location. */
6075 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6076 b->ops->print_one (b, last_loc);
6077 }
6078 else
6079 switch (b->type)
6080 {
6081 case bp_none:
6082 internal_error (__FILE__, __LINE__,
6083 _("print_one_breakpoint: bp_none encountered\n"));
6084 break;
6085
6086 case bp_watchpoint:
6087 case bp_hardware_watchpoint:
6088 case bp_read_watchpoint:
6089 case bp_access_watchpoint:
6090 {
6091 struct watchpoint *w = (struct watchpoint *) b;
6092
6093 /* Field 4, the address, is omitted (which makes the columns
6094 not line up too nicely with the headers, but the effect
6095 is relatively readable). */
6096 if (opts.addressprint)
6097 uiout->field_skip ("addr");
6098 annotate_field (5);
6099 uiout->field_string ("what", w->exp_string);
6100 }
6101 break;
6102
6103 case bp_breakpoint:
6104 case bp_hardware_breakpoint:
6105 case bp_single_step:
6106 case bp_until:
6107 case bp_finish:
6108 case bp_longjmp:
6109 case bp_longjmp_resume:
6110 case bp_longjmp_call_dummy:
6111 case bp_exception:
6112 case bp_exception_resume:
6113 case bp_step_resume:
6114 case bp_hp_step_resume:
6115 case bp_watchpoint_scope:
6116 case bp_call_dummy:
6117 case bp_std_terminate:
6118 case bp_shlib_event:
6119 case bp_thread_event:
6120 case bp_overlay_event:
6121 case bp_longjmp_master:
6122 case bp_std_terminate_master:
6123 case bp_exception_master:
6124 case bp_tracepoint:
6125 case bp_fast_tracepoint:
6126 case bp_static_tracepoint:
6127 case bp_dprintf:
6128 case bp_jit_event:
6129 case bp_gnu_ifunc_resolver:
6130 case bp_gnu_ifunc_resolver_return:
6131 if (opts.addressprint)
6132 {
6133 annotate_field (4);
6134 if (header_of_multiple)
6135 uiout->field_string ("addr", "<MULTIPLE>");
6136 else if (b->loc == NULL || loc->shlib_disabled)
6137 uiout->field_string ("addr", "<PENDING>");
6138 else
6139 uiout->field_core_addr ("addr",
6140 loc->gdbarch, loc->address);
6141 }
6142 annotate_field (5);
6143 if (!header_of_multiple)
6144 print_breakpoint_location (b, loc);
6145 if (b->loc)
6146 *last_loc = b->loc;
6147 break;
6148 }
6149
6150
6151 if (loc != NULL && !header_of_multiple)
6152 {
6153 struct inferior *inf;
6154 std::vector<int> inf_nums;
6155 int mi_only = 1;
6156
6157 ALL_INFERIORS (inf)
6158 {
6159 if (inf->pspace == loc->pspace)
6160 inf_nums.push_back (inf->num);
6161 }
6162
6163 /* For backward compatibility, don't display inferiors in CLI unless
6164 there are several. Always display for MI. */
6165 if (allflag
6166 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6167 && (number_of_program_spaces () > 1
6168 || number_of_inferiors () > 1)
6169 /* LOC is for existing B, it cannot be in
6170 moribund_locations and thus having NULL OWNER. */
6171 && loc->owner->type != bp_catchpoint))
6172 mi_only = 0;
6173 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6174 }
6175
6176 if (!part_of_multiple)
6177 {
6178 if (b->thread != -1)
6179 {
6180 /* FIXME: This seems to be redundant and lost here; see the
6181 "stop only in" line a little further down. */
6182 uiout->text (" thread ");
6183 uiout->field_int ("thread", b->thread);
6184 }
6185 else if (b->task != 0)
6186 {
6187 uiout->text (" task ");
6188 uiout->field_int ("task", b->task);
6189 }
6190 }
6191
6192 uiout->text ("\n");
6193
6194 if (!part_of_multiple)
6195 b->ops->print_one_detail (b, uiout);
6196
6197 if (part_of_multiple && frame_id_p (b->frame_id))
6198 {
6199 annotate_field (6);
6200 uiout->text ("\tstop only in stack frame at ");
6201 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6202 the frame ID. */
6203 uiout->field_core_addr ("frame",
6204 b->gdbarch, b->frame_id.stack_addr);
6205 uiout->text ("\n");
6206 }
6207
6208 if (!part_of_multiple && b->cond_string)
6209 {
6210 annotate_field (7);
6211 if (is_tracepoint (b))
6212 uiout->text ("\ttrace only if ");
6213 else
6214 uiout->text ("\tstop only if ");
6215 uiout->field_string ("cond", b->cond_string);
6216
6217 /* Print whether the target is doing the breakpoint's condition
6218 evaluation. If GDB is doing the evaluation, don't print anything. */
6219 if (is_breakpoint (b)
6220 && breakpoint_condition_evaluation_mode ()
6221 == condition_evaluation_target)
6222 {
6223 uiout->text (" (");
6224 uiout->field_string ("evaluated-by",
6225 bp_condition_evaluator (b));
6226 uiout->text (" evals)");
6227 }
6228 uiout->text ("\n");
6229 }
6230
6231 if (!part_of_multiple && b->thread != -1)
6232 {
6233 /* FIXME should make an annotation for this. */
6234 uiout->text ("\tstop only in thread ");
6235 if (uiout->is_mi_like_p ())
6236 uiout->field_int ("thread", b->thread);
6237 else
6238 {
6239 struct thread_info *thr = find_thread_global_id (b->thread);
6240
6241 uiout->field_string ("thread", print_thread_id (thr));
6242 }
6243 uiout->text ("\n");
6244 }
6245
6246 if (!part_of_multiple)
6247 {
6248 if (b->hit_count)
6249 {
6250 /* FIXME should make an annotation for this. */
6251 if (is_catchpoint (b))
6252 uiout->text ("\tcatchpoint");
6253 else if (is_tracepoint (b))
6254 uiout->text ("\ttracepoint");
6255 else
6256 uiout->text ("\tbreakpoint");
6257 uiout->text (" already hit ");
6258 uiout->field_int ("times", b->hit_count);
6259 if (b->hit_count == 1)
6260 uiout->text (" time\n");
6261 else
6262 uiout->text (" times\n");
6263 }
6264 else
6265 {
6266 /* Output the count also if it is zero, but only if this is mi. */
6267 if (uiout->is_mi_like_p ())
6268 uiout->field_int ("times", b->hit_count);
6269 }
6270 }
6271
6272 if (!part_of_multiple && b->ignore_count)
6273 {
6274 annotate_field (8);
6275 uiout->text ("\tignore next ");
6276 uiout->field_int ("ignore", b->ignore_count);
6277 uiout->text (" hits\n");
6278 }
6279
6280 /* Note that an enable count of 1 corresponds to "enable once"
6281 behavior, which is reported by the combination of enablement and
6282 disposition, so we don't need to mention it here. */
6283 if (!part_of_multiple && b->enable_count > 1)
6284 {
6285 annotate_field (8);
6286 uiout->text ("\tdisable after ");
6287 /* Tweak the wording to clarify that ignore and enable counts
6288 are distinct, and have additive effect. */
6289 if (b->ignore_count)
6290 uiout->text ("additional ");
6291 else
6292 uiout->text ("next ");
6293 uiout->field_int ("enable", b->enable_count);
6294 uiout->text (" hits\n");
6295 }
6296
6297 if (!part_of_multiple && is_tracepoint (b))
6298 {
6299 struct tracepoint *tp = (struct tracepoint *) b;
6300
6301 if (tp->traceframe_usage)
6302 {
6303 uiout->text ("\ttrace buffer usage ");
6304 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6305 uiout->text (" bytes\n");
6306 }
6307 }
6308
6309 l = b->commands ? b->commands.get () : NULL;
6310 if (!part_of_multiple && l)
6311 {
6312 annotate_field (9);
6313 ui_out_emit_tuple tuple_emitter (uiout, "script");
6314 print_command_lines (uiout, l, 4);
6315 }
6316
6317 if (is_tracepoint (b))
6318 {
6319 struct tracepoint *t = (struct tracepoint *) b;
6320
6321 if (!part_of_multiple && t->pass_count)
6322 {
6323 annotate_field (10);
6324 uiout->text ("\tpass count ");
6325 uiout->field_int ("pass", t->pass_count);
6326 uiout->text (" \n");
6327 }
6328
6329 /* Don't display it when tracepoint or tracepoint location is
6330 pending. */
6331 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6332 {
6333 annotate_field (11);
6334
6335 if (uiout->is_mi_like_p ())
6336 uiout->field_string ("installed",
6337 loc->inserted ? "y" : "n");
6338 else
6339 {
6340 if (loc->inserted)
6341 uiout->text ("\t");
6342 else
6343 uiout->text ("\tnot ");
6344 uiout->text ("installed on target\n");
6345 }
6346 }
6347 }
6348
6349 if (uiout->is_mi_like_p () && !part_of_multiple)
6350 {
6351 if (is_watchpoint (b))
6352 {
6353 struct watchpoint *w = (struct watchpoint *) b;
6354
6355 uiout->field_string ("original-location", w->exp_string);
6356 }
6357 else if (b->location != NULL
6358 && event_location_to_string (b->location.get ()) != NULL)
6359 uiout->field_string ("original-location",
6360 event_location_to_string (b->location.get ()));
6361 }
6362 }
6363
6364 static void
6365 print_one_breakpoint (struct breakpoint *b,
6366 struct bp_location **last_loc,
6367 int allflag)
6368 {
6369 struct ui_out *uiout = current_uiout;
6370
6371 {
6372 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6373
6374 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6375 }
6376
6377 /* If this breakpoint has custom print function,
6378 it's already printed. Otherwise, print individual
6379 locations, if any. */
6380 if (b->ops == NULL || b->ops->print_one == NULL)
6381 {
6382 /* If breakpoint has a single location that is disabled, we
6383 print it as if it had several locations, since otherwise it's
6384 hard to represent "breakpoint enabled, location disabled"
6385 situation.
6386
6387 Note that while hardware watchpoints have several locations
6388 internally, that's not a property exposed to user. */
6389 if (b->loc
6390 && !is_hardware_watchpoint (b)
6391 && (b->loc->next || !b->loc->enabled))
6392 {
6393 struct bp_location *loc;
6394 int n = 1;
6395
6396 for (loc = b->loc; loc; loc = loc->next, ++n)
6397 {
6398 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6399 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6400 }
6401 }
6402 }
6403 }
6404
6405 static int
6406 breakpoint_address_bits (struct breakpoint *b)
6407 {
6408 int print_address_bits = 0;
6409 struct bp_location *loc;
6410
6411 /* Software watchpoints that aren't watching memory don't have an
6412 address to print. */
6413 if (is_no_memory_software_watchpoint (b))
6414 return 0;
6415
6416 for (loc = b->loc; loc; loc = loc->next)
6417 {
6418 int addr_bit;
6419
6420 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6421 if (addr_bit > print_address_bits)
6422 print_address_bits = addr_bit;
6423 }
6424
6425 return print_address_bits;
6426 }
6427
6428 /* See breakpoint.h. */
6429
6430 void
6431 print_breakpoint (breakpoint *b)
6432 {
6433 struct bp_location *dummy_loc = NULL;
6434 print_one_breakpoint (b, &dummy_loc, 0);
6435 }
6436
6437 /* Return true if this breakpoint was set by the user, false if it is
6438 internal or momentary. */
6439
6440 int
6441 user_breakpoint_p (struct breakpoint *b)
6442 {
6443 return b->number > 0;
6444 }
6445
6446 /* See breakpoint.h. */
6447
6448 int
6449 pending_breakpoint_p (struct breakpoint *b)
6450 {
6451 return b->loc == NULL;
6452 }
6453
6454 /* Print information on user settable breakpoint (watchpoint, etc)
6455 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6456 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6457 FILTER is non-NULL, call it on each breakpoint and only include the
6458 ones for which it returns non-zero. Return the total number of
6459 breakpoints listed. */
6460
6461 static int
6462 breakpoint_1 (const char *args, int allflag,
6463 int (*filter) (const struct breakpoint *))
6464 {
6465 struct breakpoint *b;
6466 struct bp_location *last_loc = NULL;
6467 int nr_printable_breakpoints;
6468 struct value_print_options opts;
6469 int print_address_bits = 0;
6470 int print_type_col_width = 14;
6471 struct ui_out *uiout = current_uiout;
6472
6473 get_user_print_options (&opts);
6474
6475 /* Compute the number of rows in the table, as well as the size
6476 required for address fields. */
6477 nr_printable_breakpoints = 0;
6478 ALL_BREAKPOINTS (b)
6479 {
6480 /* If we have a filter, only list the breakpoints it accepts. */
6481 if (filter && !filter (b))
6482 continue;
6483
6484 /* If we have an "args" string, it is a list of breakpoints to
6485 accept. Skip the others. */
6486 if (args != NULL && *args != '\0')
6487 {
6488 if (allflag && parse_and_eval_long (args) != b->number)
6489 continue;
6490 if (!allflag && !number_is_in_list (args, b->number))
6491 continue;
6492 }
6493
6494 if (allflag || user_breakpoint_p (b))
6495 {
6496 int addr_bit, type_len;
6497
6498 addr_bit = breakpoint_address_bits (b);
6499 if (addr_bit > print_address_bits)
6500 print_address_bits = addr_bit;
6501
6502 type_len = strlen (bptype_string (b->type));
6503 if (type_len > print_type_col_width)
6504 print_type_col_width = type_len;
6505
6506 nr_printable_breakpoints++;
6507 }
6508 }
6509
6510 {
6511 ui_out_emit_table table_emitter (uiout,
6512 opts.addressprint ? 6 : 5,
6513 nr_printable_breakpoints,
6514 "BreakpointTable");
6515
6516 if (nr_printable_breakpoints > 0)
6517 annotate_breakpoints_headers ();
6518 if (nr_printable_breakpoints > 0)
6519 annotate_field (0);
6520 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6521 if (nr_printable_breakpoints > 0)
6522 annotate_field (1);
6523 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6524 if (nr_printable_breakpoints > 0)
6525 annotate_field (2);
6526 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6527 if (nr_printable_breakpoints > 0)
6528 annotate_field (3);
6529 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6530 if (opts.addressprint)
6531 {
6532 if (nr_printable_breakpoints > 0)
6533 annotate_field (4);
6534 if (print_address_bits <= 32)
6535 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6536 else
6537 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6538 }
6539 if (nr_printable_breakpoints > 0)
6540 annotate_field (5);
6541 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6542 uiout->table_body ();
6543 if (nr_printable_breakpoints > 0)
6544 annotate_breakpoints_table ();
6545
6546 ALL_BREAKPOINTS (b)
6547 {
6548 QUIT;
6549 /* If we have a filter, only list the breakpoints it accepts. */
6550 if (filter && !filter (b))
6551 continue;
6552
6553 /* If we have an "args" string, it is a list of breakpoints to
6554 accept. Skip the others. */
6555
6556 if (args != NULL && *args != '\0')
6557 {
6558 if (allflag) /* maintenance info breakpoint */
6559 {
6560 if (parse_and_eval_long (args) != b->number)
6561 continue;
6562 }
6563 else /* all others */
6564 {
6565 if (!number_is_in_list (args, b->number))
6566 continue;
6567 }
6568 }
6569 /* We only print out user settable breakpoints unless the
6570 allflag is set. */
6571 if (allflag || user_breakpoint_p (b))
6572 print_one_breakpoint (b, &last_loc, allflag);
6573 }
6574 }
6575
6576 if (nr_printable_breakpoints == 0)
6577 {
6578 /* If there's a filter, let the caller decide how to report
6579 empty list. */
6580 if (!filter)
6581 {
6582 if (args == NULL || *args == '\0')
6583 uiout->message ("No breakpoints or watchpoints.\n");
6584 else
6585 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6586 args);
6587 }
6588 }
6589 else
6590 {
6591 if (last_loc && !server_command)
6592 set_next_address (last_loc->gdbarch, last_loc->address);
6593 }
6594
6595 /* FIXME? Should this be moved up so that it is only called when
6596 there have been breakpoints? */
6597 annotate_breakpoints_table_end ();
6598
6599 return nr_printable_breakpoints;
6600 }
6601
6602 /* Display the value of default-collect in a way that is generally
6603 compatible with the breakpoint list. */
6604
6605 static void
6606 default_collect_info (void)
6607 {
6608 struct ui_out *uiout = current_uiout;
6609
6610 /* If it has no value (which is frequently the case), say nothing; a
6611 message like "No default-collect." gets in user's face when it's
6612 not wanted. */
6613 if (!*default_collect)
6614 return;
6615
6616 /* The following phrase lines up nicely with per-tracepoint collect
6617 actions. */
6618 uiout->text ("default collect ");
6619 uiout->field_string ("default-collect", default_collect);
6620 uiout->text (" \n");
6621 }
6622
6623 static void
6624 info_breakpoints_command (const char *args, int from_tty)
6625 {
6626 breakpoint_1 (args, 0, NULL);
6627
6628 default_collect_info ();
6629 }
6630
6631 static void
6632 info_watchpoints_command (const char *args, int from_tty)
6633 {
6634 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6635 struct ui_out *uiout = current_uiout;
6636
6637 if (num_printed == 0)
6638 {
6639 if (args == NULL || *args == '\0')
6640 uiout->message ("No watchpoints.\n");
6641 else
6642 uiout->message ("No watchpoint matching '%s'.\n", args);
6643 }
6644 }
6645
6646 static void
6647 maintenance_info_breakpoints (const char *args, int from_tty)
6648 {
6649 breakpoint_1 (args, 1, NULL);
6650
6651 default_collect_info ();
6652 }
6653
6654 static int
6655 breakpoint_has_pc (struct breakpoint *b,
6656 struct program_space *pspace,
6657 CORE_ADDR pc, struct obj_section *section)
6658 {
6659 struct bp_location *bl = b->loc;
6660
6661 for (; bl; bl = bl->next)
6662 {
6663 if (bl->pspace == pspace
6664 && bl->address == pc
6665 && (!overlay_debugging || bl->section == section))
6666 return 1;
6667 }
6668 return 0;
6669 }
6670
6671 /* Print a message describing any user-breakpoints set at PC. This
6672 concerns with logical breakpoints, so we match program spaces, not
6673 address spaces. */
6674
6675 static void
6676 describe_other_breakpoints (struct gdbarch *gdbarch,
6677 struct program_space *pspace, CORE_ADDR pc,
6678 struct obj_section *section, int thread)
6679 {
6680 int others = 0;
6681 struct breakpoint *b;
6682
6683 ALL_BREAKPOINTS (b)
6684 others += (user_breakpoint_p (b)
6685 && breakpoint_has_pc (b, pspace, pc, section));
6686 if (others > 0)
6687 {
6688 if (others == 1)
6689 printf_filtered (_("Note: breakpoint "));
6690 else /* if (others == ???) */
6691 printf_filtered (_("Note: breakpoints "));
6692 ALL_BREAKPOINTS (b)
6693 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6694 {
6695 others--;
6696 printf_filtered ("%d", b->number);
6697 if (b->thread == -1 && thread != -1)
6698 printf_filtered (" (all threads)");
6699 else if (b->thread != -1)
6700 printf_filtered (" (thread %d)", b->thread);
6701 printf_filtered ("%s%s ",
6702 ((b->enable_state == bp_disabled
6703 || b->enable_state == bp_call_disabled)
6704 ? " (disabled)"
6705 : ""),
6706 (others > 1) ? ","
6707 : ((others == 1) ? " and" : ""));
6708 }
6709 printf_filtered (_("also set at pc "));
6710 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6711 printf_filtered (".\n");
6712 }
6713 }
6714 \f
6715
6716 /* Return true iff it is meaningful to use the address member of
6717 BPT locations. For some breakpoint types, the locations' address members
6718 are irrelevant and it makes no sense to attempt to compare them to other
6719 addresses (or use them for any other purpose either).
6720
6721 More specifically, each of the following breakpoint types will
6722 always have a zero valued location address and we don't want to mark
6723 breakpoints of any of these types to be a duplicate of an actual
6724 breakpoint location at address zero:
6725
6726 bp_watchpoint
6727 bp_catchpoint
6728
6729 */
6730
6731 static int
6732 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6733 {
6734 enum bptype type = bpt->type;
6735
6736 return (type != bp_watchpoint && type != bp_catchpoint);
6737 }
6738
6739 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6740 true if LOC1 and LOC2 represent the same watchpoint location. */
6741
6742 static int
6743 watchpoint_locations_match (struct bp_location *loc1,
6744 struct bp_location *loc2)
6745 {
6746 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6747 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6748
6749 /* Both of them must exist. */
6750 gdb_assert (w1 != NULL);
6751 gdb_assert (w2 != NULL);
6752
6753 /* If the target can evaluate the condition expression in hardware,
6754 then we we need to insert both watchpoints even if they are at
6755 the same place. Otherwise the watchpoint will only trigger when
6756 the condition of whichever watchpoint was inserted evaluates to
6757 true, not giving a chance for GDB to check the condition of the
6758 other watchpoint. */
6759 if ((w1->cond_exp
6760 && target_can_accel_watchpoint_condition (loc1->address,
6761 loc1->length,
6762 loc1->watchpoint_type,
6763 w1->cond_exp.get ()))
6764 || (w2->cond_exp
6765 && target_can_accel_watchpoint_condition (loc2->address,
6766 loc2->length,
6767 loc2->watchpoint_type,
6768 w2->cond_exp.get ())))
6769 return 0;
6770
6771 /* Note that this checks the owner's type, not the location's. In
6772 case the target does not support read watchpoints, but does
6773 support access watchpoints, we'll have bp_read_watchpoint
6774 watchpoints with hw_access locations. Those should be considered
6775 duplicates of hw_read locations. The hw_read locations will
6776 become hw_access locations later. */
6777 return (loc1->owner->type == loc2->owner->type
6778 && loc1->pspace->aspace == loc2->pspace->aspace
6779 && loc1->address == loc2->address
6780 && loc1->length == loc2->length);
6781 }
6782
6783 /* See breakpoint.h. */
6784
6785 int
6786 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6787 const address_space *aspace2, CORE_ADDR addr2)
6788 {
6789 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6790 || aspace1 == aspace2)
6791 && addr1 == addr2);
6792 }
6793
6794 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6795 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6796 matches ASPACE2. On targets that have global breakpoints, the address
6797 space doesn't really matter. */
6798
6799 static int
6800 breakpoint_address_match_range (const address_space *aspace1,
6801 CORE_ADDR addr1,
6802 int len1, const address_space *aspace2,
6803 CORE_ADDR addr2)
6804 {
6805 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6806 || aspace1 == aspace2)
6807 && addr2 >= addr1 && addr2 < addr1 + len1);
6808 }
6809
6810 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6811 a ranged breakpoint. In most targets, a match happens only if ASPACE
6812 matches the breakpoint's address space. On targets that have global
6813 breakpoints, the address space doesn't really matter. */
6814
6815 static int
6816 breakpoint_location_address_match (struct bp_location *bl,
6817 const address_space *aspace,
6818 CORE_ADDR addr)
6819 {
6820 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6821 aspace, addr)
6822 || (bl->length
6823 && breakpoint_address_match_range (bl->pspace->aspace,
6824 bl->address, bl->length,
6825 aspace, addr)));
6826 }
6827
6828 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6829 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6830 match happens only if ASPACE matches the breakpoint's address
6831 space. On targets that have global breakpoints, the address space
6832 doesn't really matter. */
6833
6834 static int
6835 breakpoint_location_address_range_overlap (struct bp_location *bl,
6836 const address_space *aspace,
6837 CORE_ADDR addr, int len)
6838 {
6839 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6840 || bl->pspace->aspace == aspace)
6841 {
6842 int bl_len = bl->length != 0 ? bl->length : 1;
6843
6844 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6845 return 1;
6846 }
6847 return 0;
6848 }
6849
6850 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6851 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6852 true, otherwise returns false. */
6853
6854 static int
6855 tracepoint_locations_match (struct bp_location *loc1,
6856 struct bp_location *loc2)
6857 {
6858 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6859 /* Since tracepoint locations are never duplicated with others', tracepoint
6860 locations at the same address of different tracepoints are regarded as
6861 different locations. */
6862 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6863 else
6864 return 0;
6865 }
6866
6867 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6868 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6869 represent the same location. */
6870
6871 static int
6872 breakpoint_locations_match (struct bp_location *loc1,
6873 struct bp_location *loc2)
6874 {
6875 int hw_point1, hw_point2;
6876
6877 /* Both of them must not be in moribund_locations. */
6878 gdb_assert (loc1->owner != NULL);
6879 gdb_assert (loc2->owner != NULL);
6880
6881 hw_point1 = is_hardware_watchpoint (loc1->owner);
6882 hw_point2 = is_hardware_watchpoint (loc2->owner);
6883
6884 if (hw_point1 != hw_point2)
6885 return 0;
6886 else if (hw_point1)
6887 return watchpoint_locations_match (loc1, loc2);
6888 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6889 return tracepoint_locations_match (loc1, loc2);
6890 else
6891 /* We compare bp_location.length in order to cover ranged breakpoints. */
6892 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6893 loc2->pspace->aspace, loc2->address)
6894 && loc1->length == loc2->length);
6895 }
6896
6897 static void
6898 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6899 int bnum, int have_bnum)
6900 {
6901 /* The longest string possibly returned by hex_string_custom
6902 is 50 chars. These must be at least that big for safety. */
6903 char astr1[64];
6904 char astr2[64];
6905
6906 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6907 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6908 if (have_bnum)
6909 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6910 bnum, astr1, astr2);
6911 else
6912 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6913 }
6914
6915 /* Adjust a breakpoint's address to account for architectural
6916 constraints on breakpoint placement. Return the adjusted address.
6917 Note: Very few targets require this kind of adjustment. For most
6918 targets, this function is simply the identity function. */
6919
6920 static CORE_ADDR
6921 adjust_breakpoint_address (struct gdbarch *gdbarch,
6922 CORE_ADDR bpaddr, enum bptype bptype)
6923 {
6924 if (bptype == bp_watchpoint
6925 || bptype == bp_hardware_watchpoint
6926 || bptype == bp_read_watchpoint
6927 || bptype == bp_access_watchpoint
6928 || bptype == bp_catchpoint)
6929 {
6930 /* Watchpoints and the various bp_catch_* eventpoints should not
6931 have their addresses modified. */
6932 return bpaddr;
6933 }
6934 else if (bptype == bp_single_step)
6935 {
6936 /* Single-step breakpoints should not have their addresses
6937 modified. If there's any architectural constrain that
6938 applies to this address, then it should have already been
6939 taken into account when the breakpoint was created in the
6940 first place. If we didn't do this, stepping through e.g.,
6941 Thumb-2 IT blocks would break. */
6942 return bpaddr;
6943 }
6944 else
6945 {
6946 CORE_ADDR adjusted_bpaddr = bpaddr;
6947
6948 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
6949 {
6950 /* Some targets have architectural constraints on the placement
6951 of breakpoint instructions. Obtain the adjusted address. */
6952 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6953 }
6954
6955 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
6956
6957 /* An adjusted breakpoint address can significantly alter
6958 a user's expectations. Print a warning if an adjustment
6959 is required. */
6960 if (adjusted_bpaddr != bpaddr)
6961 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6962
6963 return adjusted_bpaddr;
6964 }
6965 }
6966
6967 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
6968 {
6969 bp_location *loc = this;
6970
6971 gdb_assert (ops != NULL);
6972
6973 loc->ops = ops;
6974 loc->owner = owner;
6975 loc->cond_bytecode = NULL;
6976 loc->shlib_disabled = 0;
6977 loc->enabled = 1;
6978
6979 switch (owner->type)
6980 {
6981 case bp_breakpoint:
6982 case bp_single_step:
6983 case bp_until:
6984 case bp_finish:
6985 case bp_longjmp:
6986 case bp_longjmp_resume:
6987 case bp_longjmp_call_dummy:
6988 case bp_exception:
6989 case bp_exception_resume:
6990 case bp_step_resume:
6991 case bp_hp_step_resume:
6992 case bp_watchpoint_scope:
6993 case bp_call_dummy:
6994 case bp_std_terminate:
6995 case bp_shlib_event:
6996 case bp_thread_event:
6997 case bp_overlay_event:
6998 case bp_jit_event:
6999 case bp_longjmp_master:
7000 case bp_std_terminate_master:
7001 case bp_exception_master:
7002 case bp_gnu_ifunc_resolver:
7003 case bp_gnu_ifunc_resolver_return:
7004 case bp_dprintf:
7005 loc->loc_type = bp_loc_software_breakpoint;
7006 mark_breakpoint_location_modified (loc);
7007 break;
7008 case bp_hardware_breakpoint:
7009 loc->loc_type = bp_loc_hardware_breakpoint;
7010 mark_breakpoint_location_modified (loc);
7011 break;
7012 case bp_hardware_watchpoint:
7013 case bp_read_watchpoint:
7014 case bp_access_watchpoint:
7015 loc->loc_type = bp_loc_hardware_watchpoint;
7016 break;
7017 case bp_watchpoint:
7018 case bp_catchpoint:
7019 case bp_tracepoint:
7020 case bp_fast_tracepoint:
7021 case bp_static_tracepoint:
7022 loc->loc_type = bp_loc_other;
7023 break;
7024 default:
7025 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7026 }
7027
7028 loc->refc = 1;
7029 }
7030
7031 /* Allocate a struct bp_location. */
7032
7033 static struct bp_location *
7034 allocate_bp_location (struct breakpoint *bpt)
7035 {
7036 return bpt->ops->allocate_location (bpt);
7037 }
7038
7039 static void
7040 free_bp_location (struct bp_location *loc)
7041 {
7042 loc->ops->dtor (loc);
7043 delete loc;
7044 }
7045
7046 /* Increment reference count. */
7047
7048 static void
7049 incref_bp_location (struct bp_location *bl)
7050 {
7051 ++bl->refc;
7052 }
7053
7054 /* Decrement reference count. If the reference count reaches 0,
7055 destroy the bp_location. Sets *BLP to NULL. */
7056
7057 static void
7058 decref_bp_location (struct bp_location **blp)
7059 {
7060 gdb_assert ((*blp)->refc > 0);
7061
7062 if (--(*blp)->refc == 0)
7063 free_bp_location (*blp);
7064 *blp = NULL;
7065 }
7066
7067 /* Add breakpoint B at the end of the global breakpoint chain. */
7068
7069 static breakpoint *
7070 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7071 {
7072 struct breakpoint *b1;
7073 struct breakpoint *result = b.get ();
7074
7075 /* Add this breakpoint to the end of the chain so that a list of
7076 breakpoints will come out in order of increasing numbers. */
7077
7078 b1 = breakpoint_chain;
7079 if (b1 == 0)
7080 breakpoint_chain = b.release ();
7081 else
7082 {
7083 while (b1->next)
7084 b1 = b1->next;
7085 b1->next = b.release ();
7086 }
7087
7088 return result;
7089 }
7090
7091 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7092
7093 static void
7094 init_raw_breakpoint_without_location (struct breakpoint *b,
7095 struct gdbarch *gdbarch,
7096 enum bptype bptype,
7097 const struct breakpoint_ops *ops)
7098 {
7099 gdb_assert (ops != NULL);
7100
7101 b->ops = ops;
7102 b->type = bptype;
7103 b->gdbarch = gdbarch;
7104 b->language = current_language->la_language;
7105 b->input_radix = input_radix;
7106 b->related_breakpoint = b;
7107 }
7108
7109 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7110 that has type BPTYPE and has no locations as yet. */
7111
7112 static struct breakpoint *
7113 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7114 enum bptype bptype,
7115 const struct breakpoint_ops *ops)
7116 {
7117 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7118
7119 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7120 return add_to_breakpoint_chain (std::move (b));
7121 }
7122
7123 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7124 resolutions should be made as the user specified the location explicitly
7125 enough. */
7126
7127 static void
7128 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7129 {
7130 gdb_assert (loc->owner != NULL);
7131
7132 if (loc->owner->type == bp_breakpoint
7133 || loc->owner->type == bp_hardware_breakpoint
7134 || is_tracepoint (loc->owner))
7135 {
7136 const char *function_name;
7137
7138 if (loc->msymbol != NULL
7139 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
7140 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc)
7141 && !explicit_loc)
7142 {
7143 struct breakpoint *b = loc->owner;
7144
7145 function_name = MSYMBOL_LINKAGE_NAME (loc->msymbol);
7146
7147 if (b->type == bp_breakpoint && b->loc == loc
7148 && loc->next == NULL && b->related_breakpoint == b)
7149 {
7150 /* Create only the whole new breakpoint of this type but do not
7151 mess more complicated breakpoints with multiple locations. */
7152 b->type = bp_gnu_ifunc_resolver;
7153 /* Remember the resolver's address for use by the return
7154 breakpoint. */
7155 loc->related_address = loc->address;
7156 }
7157 }
7158 else
7159 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7160
7161 if (function_name)
7162 loc->function_name = xstrdup (function_name);
7163 }
7164 }
7165
7166 /* Attempt to determine architecture of location identified by SAL. */
7167 struct gdbarch *
7168 get_sal_arch (struct symtab_and_line sal)
7169 {
7170 if (sal.section)
7171 return get_objfile_arch (sal.section->objfile);
7172 if (sal.symtab)
7173 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7174
7175 return NULL;
7176 }
7177
7178 /* Low level routine for partially initializing a breakpoint of type
7179 BPTYPE. The newly created breakpoint's address, section, source
7180 file name, and line number are provided by SAL.
7181
7182 It is expected that the caller will complete the initialization of
7183 the newly created breakpoint struct as well as output any status
7184 information regarding the creation of a new breakpoint. */
7185
7186 static void
7187 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7188 struct symtab_and_line sal, enum bptype bptype,
7189 const struct breakpoint_ops *ops)
7190 {
7191 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7192
7193 add_location_to_breakpoint (b, &sal);
7194
7195 if (bptype != bp_catchpoint)
7196 gdb_assert (sal.pspace != NULL);
7197
7198 /* Store the program space that was used to set the breakpoint,
7199 except for ordinary breakpoints, which are independent of the
7200 program space. */
7201 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7202 b->pspace = sal.pspace;
7203 }
7204
7205 /* set_raw_breakpoint is a low level routine for allocating and
7206 partially initializing a breakpoint of type BPTYPE. The newly
7207 created breakpoint's address, section, source file name, and line
7208 number are provided by SAL. The newly created and partially
7209 initialized breakpoint is added to the breakpoint chain and
7210 is also returned as the value of this function.
7211
7212 It is expected that the caller will complete the initialization of
7213 the newly created breakpoint struct as well as output any status
7214 information regarding the creation of a new breakpoint. In
7215 particular, set_raw_breakpoint does NOT set the breakpoint
7216 number! Care should be taken to not allow an error to occur
7217 prior to completing the initialization of the breakpoint. If this
7218 should happen, a bogus breakpoint will be left on the chain. */
7219
7220 struct breakpoint *
7221 set_raw_breakpoint (struct gdbarch *gdbarch,
7222 struct symtab_and_line sal, enum bptype bptype,
7223 const struct breakpoint_ops *ops)
7224 {
7225 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7226
7227 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7228 return add_to_breakpoint_chain (std::move (b));
7229 }
7230
7231 /* Call this routine when stepping and nexting to enable a breakpoint
7232 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7233 initiated the operation. */
7234
7235 void
7236 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7237 {
7238 struct breakpoint *b, *b_tmp;
7239 int thread = tp->global_num;
7240
7241 /* To avoid having to rescan all objfile symbols at every step,
7242 we maintain a list of continually-inserted but always disabled
7243 longjmp "master" breakpoints. Here, we simply create momentary
7244 clones of those and enable them for the requested thread. */
7245 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7246 if (b->pspace == current_program_space
7247 && (b->type == bp_longjmp_master
7248 || b->type == bp_exception_master))
7249 {
7250 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7251 struct breakpoint *clone;
7252
7253 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7254 after their removal. */
7255 clone = momentary_breakpoint_from_master (b, type,
7256 &momentary_breakpoint_ops, 1);
7257 clone->thread = thread;
7258 }
7259
7260 tp->initiating_frame = frame;
7261 }
7262
7263 /* Delete all longjmp breakpoints from THREAD. */
7264 void
7265 delete_longjmp_breakpoint (int thread)
7266 {
7267 struct breakpoint *b, *b_tmp;
7268
7269 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7270 if (b->type == bp_longjmp || b->type == bp_exception)
7271 {
7272 if (b->thread == thread)
7273 delete_breakpoint (b);
7274 }
7275 }
7276
7277 void
7278 delete_longjmp_breakpoint_at_next_stop (int thread)
7279 {
7280 struct breakpoint *b, *b_tmp;
7281
7282 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7283 if (b->type == bp_longjmp || b->type == bp_exception)
7284 {
7285 if (b->thread == thread)
7286 b->disposition = disp_del_at_next_stop;
7287 }
7288 }
7289
7290 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7291 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7292 pointer to any of them. Return NULL if this system cannot place longjmp
7293 breakpoints. */
7294
7295 struct breakpoint *
7296 set_longjmp_breakpoint_for_call_dummy (void)
7297 {
7298 struct breakpoint *b, *retval = NULL;
7299
7300 ALL_BREAKPOINTS (b)
7301 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7302 {
7303 struct breakpoint *new_b;
7304
7305 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7306 &momentary_breakpoint_ops,
7307 1);
7308 new_b->thread = inferior_thread ()->global_num;
7309
7310 /* Link NEW_B into the chain of RETVAL breakpoints. */
7311
7312 gdb_assert (new_b->related_breakpoint == new_b);
7313 if (retval == NULL)
7314 retval = new_b;
7315 new_b->related_breakpoint = retval;
7316 while (retval->related_breakpoint != new_b->related_breakpoint)
7317 retval = retval->related_breakpoint;
7318 retval->related_breakpoint = new_b;
7319 }
7320
7321 return retval;
7322 }
7323
7324 /* Verify all existing dummy frames and their associated breakpoints for
7325 TP. Remove those which can no longer be found in the current frame
7326 stack.
7327
7328 You should call this function only at places where it is safe to currently
7329 unwind the whole stack. Failed stack unwind would discard live dummy
7330 frames. */
7331
7332 void
7333 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7334 {
7335 struct breakpoint *b, *b_tmp;
7336
7337 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7338 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7339 {
7340 struct breakpoint *dummy_b = b->related_breakpoint;
7341
7342 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7343 dummy_b = dummy_b->related_breakpoint;
7344 if (dummy_b->type != bp_call_dummy
7345 || frame_find_by_id (dummy_b->frame_id) != NULL)
7346 continue;
7347
7348 dummy_frame_discard (dummy_b->frame_id, tp);
7349
7350 while (b->related_breakpoint != b)
7351 {
7352 if (b_tmp == b->related_breakpoint)
7353 b_tmp = b->related_breakpoint->next;
7354 delete_breakpoint (b->related_breakpoint);
7355 }
7356 delete_breakpoint (b);
7357 }
7358 }
7359
7360 void
7361 enable_overlay_breakpoints (void)
7362 {
7363 struct breakpoint *b;
7364
7365 ALL_BREAKPOINTS (b)
7366 if (b->type == bp_overlay_event)
7367 {
7368 b->enable_state = bp_enabled;
7369 update_global_location_list (UGLL_MAY_INSERT);
7370 overlay_events_enabled = 1;
7371 }
7372 }
7373
7374 void
7375 disable_overlay_breakpoints (void)
7376 {
7377 struct breakpoint *b;
7378
7379 ALL_BREAKPOINTS (b)
7380 if (b->type == bp_overlay_event)
7381 {
7382 b->enable_state = bp_disabled;
7383 update_global_location_list (UGLL_DONT_INSERT);
7384 overlay_events_enabled = 0;
7385 }
7386 }
7387
7388 /* Set an active std::terminate breakpoint for each std::terminate
7389 master breakpoint. */
7390 void
7391 set_std_terminate_breakpoint (void)
7392 {
7393 struct breakpoint *b, *b_tmp;
7394
7395 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7396 if (b->pspace == current_program_space
7397 && b->type == bp_std_terminate_master)
7398 {
7399 momentary_breakpoint_from_master (b, bp_std_terminate,
7400 &momentary_breakpoint_ops, 1);
7401 }
7402 }
7403
7404 /* Delete all the std::terminate breakpoints. */
7405 void
7406 delete_std_terminate_breakpoint (void)
7407 {
7408 struct breakpoint *b, *b_tmp;
7409
7410 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7411 if (b->type == bp_std_terminate)
7412 delete_breakpoint (b);
7413 }
7414
7415 struct breakpoint *
7416 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7417 {
7418 struct breakpoint *b;
7419
7420 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7421 &internal_breakpoint_ops);
7422
7423 b->enable_state = bp_enabled;
7424 /* location has to be used or breakpoint_re_set will delete me. */
7425 b->location = new_address_location (b->loc->address, NULL, 0);
7426
7427 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7428
7429 return b;
7430 }
7431
7432 struct lang_and_radix
7433 {
7434 enum language lang;
7435 int radix;
7436 };
7437
7438 /* Create a breakpoint for JIT code registration and unregistration. */
7439
7440 struct breakpoint *
7441 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7442 {
7443 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7444 &internal_breakpoint_ops);
7445 }
7446
7447 /* Remove JIT code registration and unregistration breakpoint(s). */
7448
7449 void
7450 remove_jit_event_breakpoints (void)
7451 {
7452 struct breakpoint *b, *b_tmp;
7453
7454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7455 if (b->type == bp_jit_event
7456 && b->loc->pspace == current_program_space)
7457 delete_breakpoint (b);
7458 }
7459
7460 void
7461 remove_solib_event_breakpoints (void)
7462 {
7463 struct breakpoint *b, *b_tmp;
7464
7465 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7466 if (b->type == bp_shlib_event
7467 && b->loc->pspace == current_program_space)
7468 delete_breakpoint (b);
7469 }
7470
7471 /* See breakpoint.h. */
7472
7473 void
7474 remove_solib_event_breakpoints_at_next_stop (void)
7475 {
7476 struct breakpoint *b, *b_tmp;
7477
7478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7479 if (b->type == bp_shlib_event
7480 && b->loc->pspace == current_program_space)
7481 b->disposition = disp_del_at_next_stop;
7482 }
7483
7484 /* Helper for create_solib_event_breakpoint /
7485 create_and_insert_solib_event_breakpoint. Allows specifying which
7486 INSERT_MODE to pass through to update_global_location_list. */
7487
7488 static struct breakpoint *
7489 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7490 enum ugll_insert_mode insert_mode)
7491 {
7492 struct breakpoint *b;
7493
7494 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7495 &internal_breakpoint_ops);
7496 update_global_location_list_nothrow (insert_mode);
7497 return b;
7498 }
7499
7500 struct breakpoint *
7501 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7502 {
7503 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7504 }
7505
7506 /* See breakpoint.h. */
7507
7508 struct breakpoint *
7509 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7510 {
7511 struct breakpoint *b;
7512
7513 /* Explicitly tell update_global_location_list to insert
7514 locations. */
7515 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7516 if (!b->loc->inserted)
7517 {
7518 delete_breakpoint (b);
7519 return NULL;
7520 }
7521 return b;
7522 }
7523
7524 /* Disable any breakpoints that are on code in shared libraries. Only
7525 apply to enabled breakpoints, disabled ones can just stay disabled. */
7526
7527 void
7528 disable_breakpoints_in_shlibs (void)
7529 {
7530 struct bp_location *loc, **locp_tmp;
7531
7532 ALL_BP_LOCATIONS (loc, locp_tmp)
7533 {
7534 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7535 struct breakpoint *b = loc->owner;
7536
7537 /* We apply the check to all breakpoints, including disabled for
7538 those with loc->duplicate set. This is so that when breakpoint
7539 becomes enabled, or the duplicate is removed, gdb will try to
7540 insert all breakpoints. If we don't set shlib_disabled here,
7541 we'll try to insert those breakpoints and fail. */
7542 if (((b->type == bp_breakpoint)
7543 || (b->type == bp_jit_event)
7544 || (b->type == bp_hardware_breakpoint)
7545 || (is_tracepoint (b)))
7546 && loc->pspace == current_program_space
7547 && !loc->shlib_disabled
7548 && solib_name_from_address (loc->pspace, loc->address)
7549 )
7550 {
7551 loc->shlib_disabled = 1;
7552 }
7553 }
7554 }
7555
7556 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7557 notification of unloaded_shlib. Only apply to enabled breakpoints,
7558 disabled ones can just stay disabled. */
7559
7560 static void
7561 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7562 {
7563 struct bp_location *loc, **locp_tmp;
7564 int disabled_shlib_breaks = 0;
7565
7566 ALL_BP_LOCATIONS (loc, locp_tmp)
7567 {
7568 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7569 struct breakpoint *b = loc->owner;
7570
7571 if (solib->pspace == loc->pspace
7572 && !loc->shlib_disabled
7573 && (((b->type == bp_breakpoint
7574 || b->type == bp_jit_event
7575 || b->type == bp_hardware_breakpoint)
7576 && (loc->loc_type == bp_loc_hardware_breakpoint
7577 || loc->loc_type == bp_loc_software_breakpoint))
7578 || is_tracepoint (b))
7579 && solib_contains_address_p (solib, loc->address))
7580 {
7581 loc->shlib_disabled = 1;
7582 /* At this point, we cannot rely on remove_breakpoint
7583 succeeding so we must mark the breakpoint as not inserted
7584 to prevent future errors occurring in remove_breakpoints. */
7585 loc->inserted = 0;
7586
7587 /* This may cause duplicate notifications for the same breakpoint. */
7588 gdb::observers::breakpoint_modified.notify (b);
7589
7590 if (!disabled_shlib_breaks)
7591 {
7592 target_terminal::ours_for_output ();
7593 warning (_("Temporarily disabling breakpoints "
7594 "for unloaded shared library \"%s\""),
7595 solib->so_name);
7596 }
7597 disabled_shlib_breaks = 1;
7598 }
7599 }
7600 }
7601
7602 /* Disable any breakpoints and tracepoints in OBJFILE upon
7603 notification of free_objfile. Only apply to enabled breakpoints,
7604 disabled ones can just stay disabled. */
7605
7606 static void
7607 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7608 {
7609 struct breakpoint *b;
7610
7611 if (objfile == NULL)
7612 return;
7613
7614 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7615 managed by the user with add-symbol-file/remove-symbol-file.
7616 Similarly to how breakpoints in shared libraries are handled in
7617 response to "nosharedlibrary", mark breakpoints in such modules
7618 shlib_disabled so they end up uninserted on the next global
7619 location list update. Shared libraries not loaded by the user
7620 aren't handled here -- they're already handled in
7621 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7622 solib_unloaded observer. We skip objfiles that are not
7623 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7624 main objfile). */
7625 if ((objfile->flags & OBJF_SHARED) == 0
7626 || (objfile->flags & OBJF_USERLOADED) == 0)
7627 return;
7628
7629 ALL_BREAKPOINTS (b)
7630 {
7631 struct bp_location *loc;
7632 int bp_modified = 0;
7633
7634 if (!is_breakpoint (b) && !is_tracepoint (b))
7635 continue;
7636
7637 for (loc = b->loc; loc != NULL; loc = loc->next)
7638 {
7639 CORE_ADDR loc_addr = loc->address;
7640
7641 if (loc->loc_type != bp_loc_hardware_breakpoint
7642 && loc->loc_type != bp_loc_software_breakpoint)
7643 continue;
7644
7645 if (loc->shlib_disabled != 0)
7646 continue;
7647
7648 if (objfile->pspace != loc->pspace)
7649 continue;
7650
7651 if (loc->loc_type != bp_loc_hardware_breakpoint
7652 && loc->loc_type != bp_loc_software_breakpoint)
7653 continue;
7654
7655 if (is_addr_in_objfile (loc_addr, objfile))
7656 {
7657 loc->shlib_disabled = 1;
7658 /* At this point, we don't know whether the object was
7659 unmapped from the inferior or not, so leave the
7660 inserted flag alone. We'll handle failure to
7661 uninsert quietly, in case the object was indeed
7662 unmapped. */
7663
7664 mark_breakpoint_location_modified (loc);
7665
7666 bp_modified = 1;
7667 }
7668 }
7669
7670 if (bp_modified)
7671 gdb::observers::breakpoint_modified.notify (b);
7672 }
7673 }
7674
7675 /* FORK & VFORK catchpoints. */
7676
7677 /* An instance of this type is used to represent a fork or vfork
7678 catchpoint. A breakpoint is really of this type iff its ops pointer points
7679 to CATCH_FORK_BREAKPOINT_OPS. */
7680
7681 struct fork_catchpoint : public breakpoint
7682 {
7683 /* Process id of a child process whose forking triggered this
7684 catchpoint. This field is only valid immediately after this
7685 catchpoint has triggered. */
7686 ptid_t forked_inferior_pid;
7687 };
7688
7689 /* Implement the "insert" breakpoint_ops method for fork
7690 catchpoints. */
7691
7692 static int
7693 insert_catch_fork (struct bp_location *bl)
7694 {
7695 return target_insert_fork_catchpoint (inferior_ptid.pid ());
7696 }
7697
7698 /* Implement the "remove" breakpoint_ops method for fork
7699 catchpoints. */
7700
7701 static int
7702 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
7703 {
7704 return target_remove_fork_catchpoint (inferior_ptid.pid ());
7705 }
7706
7707 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7708 catchpoints. */
7709
7710 static int
7711 breakpoint_hit_catch_fork (const struct bp_location *bl,
7712 const address_space *aspace, CORE_ADDR bp_addr,
7713 const struct target_waitstatus *ws)
7714 {
7715 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7716
7717 if (ws->kind != TARGET_WAITKIND_FORKED)
7718 return 0;
7719
7720 c->forked_inferior_pid = ws->value.related_pid;
7721 return 1;
7722 }
7723
7724 /* Implement the "print_it" breakpoint_ops method for fork
7725 catchpoints. */
7726
7727 static enum print_stop_action
7728 print_it_catch_fork (bpstat bs)
7729 {
7730 struct ui_out *uiout = current_uiout;
7731 struct breakpoint *b = bs->breakpoint_at;
7732 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7733
7734 annotate_catchpoint (b->number);
7735 maybe_print_thread_hit_breakpoint (uiout);
7736 if (b->disposition == disp_del)
7737 uiout->text ("Temporary catchpoint ");
7738 else
7739 uiout->text ("Catchpoint ");
7740 if (uiout->is_mi_like_p ())
7741 {
7742 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7743 uiout->field_string ("disp", bpdisp_text (b->disposition));
7744 }
7745 uiout->field_int ("bkptno", b->number);
7746 uiout->text (" (forked process ");
7747 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
7748 uiout->text ("), ");
7749 return PRINT_SRC_AND_LOC;
7750 }
7751
7752 /* Implement the "print_one" breakpoint_ops method for fork
7753 catchpoints. */
7754
7755 static void
7756 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7757 {
7758 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7759 struct value_print_options opts;
7760 struct ui_out *uiout = current_uiout;
7761
7762 get_user_print_options (&opts);
7763
7764 /* Field 4, the address, is omitted (which makes the columns not
7765 line up too nicely with the headers, but the effect is relatively
7766 readable). */
7767 if (opts.addressprint)
7768 uiout->field_skip ("addr");
7769 annotate_field (5);
7770 uiout->text ("fork");
7771 if (c->forked_inferior_pid != null_ptid)
7772 {
7773 uiout->text (", process ");
7774 uiout->field_int ("what", c->forked_inferior_pid.pid ());
7775 uiout->spaces (1);
7776 }
7777
7778 if (uiout->is_mi_like_p ())
7779 uiout->field_string ("catch-type", "fork");
7780 }
7781
7782 /* Implement the "print_mention" breakpoint_ops method for fork
7783 catchpoints. */
7784
7785 static void
7786 print_mention_catch_fork (struct breakpoint *b)
7787 {
7788 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7789 }
7790
7791 /* Implement the "print_recreate" breakpoint_ops method for fork
7792 catchpoints. */
7793
7794 static void
7795 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7796 {
7797 fprintf_unfiltered (fp, "catch fork");
7798 print_recreate_thread (b, fp);
7799 }
7800
7801 /* The breakpoint_ops structure to be used in fork catchpoints. */
7802
7803 static struct breakpoint_ops catch_fork_breakpoint_ops;
7804
7805 /* Implement the "insert" breakpoint_ops method for vfork
7806 catchpoints. */
7807
7808 static int
7809 insert_catch_vfork (struct bp_location *bl)
7810 {
7811 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
7812 }
7813
7814 /* Implement the "remove" breakpoint_ops method for vfork
7815 catchpoints. */
7816
7817 static int
7818 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
7819 {
7820 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
7821 }
7822
7823 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7824 catchpoints. */
7825
7826 static int
7827 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7828 const address_space *aspace, CORE_ADDR bp_addr,
7829 const struct target_waitstatus *ws)
7830 {
7831 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7832
7833 if (ws->kind != TARGET_WAITKIND_VFORKED)
7834 return 0;
7835
7836 c->forked_inferior_pid = ws->value.related_pid;
7837 return 1;
7838 }
7839
7840 /* Implement the "print_it" breakpoint_ops method for vfork
7841 catchpoints. */
7842
7843 static enum print_stop_action
7844 print_it_catch_vfork (bpstat bs)
7845 {
7846 struct ui_out *uiout = current_uiout;
7847 struct breakpoint *b = bs->breakpoint_at;
7848 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7849
7850 annotate_catchpoint (b->number);
7851 maybe_print_thread_hit_breakpoint (uiout);
7852 if (b->disposition == disp_del)
7853 uiout->text ("Temporary catchpoint ");
7854 else
7855 uiout->text ("Catchpoint ");
7856 if (uiout->is_mi_like_p ())
7857 {
7858 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7859 uiout->field_string ("disp", bpdisp_text (b->disposition));
7860 }
7861 uiout->field_int ("bkptno", b->number);
7862 uiout->text (" (vforked process ");
7863 uiout->field_int ("newpid", c->forked_inferior_pid.pid ());
7864 uiout->text ("), ");
7865 return PRINT_SRC_AND_LOC;
7866 }
7867
7868 /* Implement the "print_one" breakpoint_ops method for vfork
7869 catchpoints. */
7870
7871 static void
7872 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7873 {
7874 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7875 struct value_print_options opts;
7876 struct ui_out *uiout = current_uiout;
7877
7878 get_user_print_options (&opts);
7879 /* Field 4, the address, is omitted (which makes the columns not
7880 line up too nicely with the headers, but the effect is relatively
7881 readable). */
7882 if (opts.addressprint)
7883 uiout->field_skip ("addr");
7884 annotate_field (5);
7885 uiout->text ("vfork");
7886 if (c->forked_inferior_pid != null_ptid)
7887 {
7888 uiout->text (", process ");
7889 uiout->field_int ("what", c->forked_inferior_pid.pid ());
7890 uiout->spaces (1);
7891 }
7892
7893 if (uiout->is_mi_like_p ())
7894 uiout->field_string ("catch-type", "vfork");
7895 }
7896
7897 /* Implement the "print_mention" breakpoint_ops method for vfork
7898 catchpoints. */
7899
7900 static void
7901 print_mention_catch_vfork (struct breakpoint *b)
7902 {
7903 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7904 }
7905
7906 /* Implement the "print_recreate" breakpoint_ops method for vfork
7907 catchpoints. */
7908
7909 static void
7910 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7911 {
7912 fprintf_unfiltered (fp, "catch vfork");
7913 print_recreate_thread (b, fp);
7914 }
7915
7916 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7917
7918 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7919
7920 /* An instance of this type is used to represent an solib catchpoint.
7921 A breakpoint is really of this type iff its ops pointer points to
7922 CATCH_SOLIB_BREAKPOINT_OPS. */
7923
7924 struct solib_catchpoint : public breakpoint
7925 {
7926 ~solib_catchpoint () override;
7927
7928 /* True for "catch load", false for "catch unload". */
7929 unsigned char is_load;
7930
7931 /* Regular expression to match, if any. COMPILED is only valid when
7932 REGEX is non-NULL. */
7933 char *regex;
7934 std::unique_ptr<compiled_regex> compiled;
7935 };
7936
7937 solib_catchpoint::~solib_catchpoint ()
7938 {
7939 xfree (this->regex);
7940 }
7941
7942 static int
7943 insert_catch_solib (struct bp_location *ignore)
7944 {
7945 return 0;
7946 }
7947
7948 static int
7949 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
7950 {
7951 return 0;
7952 }
7953
7954 static int
7955 breakpoint_hit_catch_solib (const struct bp_location *bl,
7956 const address_space *aspace,
7957 CORE_ADDR bp_addr,
7958 const struct target_waitstatus *ws)
7959 {
7960 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7961 struct breakpoint *other;
7962
7963 if (ws->kind == TARGET_WAITKIND_LOADED)
7964 return 1;
7965
7966 ALL_BREAKPOINTS (other)
7967 {
7968 struct bp_location *other_bl;
7969
7970 if (other == bl->owner)
7971 continue;
7972
7973 if (other->type != bp_shlib_event)
7974 continue;
7975
7976 if (self->pspace != NULL && other->pspace != self->pspace)
7977 continue;
7978
7979 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7980 {
7981 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7982 return 1;
7983 }
7984 }
7985
7986 return 0;
7987 }
7988
7989 static void
7990 check_status_catch_solib (struct bpstats *bs)
7991 {
7992 struct solib_catchpoint *self
7993 = (struct solib_catchpoint *) bs->breakpoint_at;
7994
7995 if (self->is_load)
7996 {
7997 for (so_list *iter : current_program_space->added_solibs)
7998 {
7999 if (!self->regex
8000 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8001 return;
8002 }
8003 }
8004 else
8005 {
8006 for (const std::string &iter : current_program_space->deleted_solibs)
8007 {
8008 if (!self->regex
8009 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
8010 return;
8011 }
8012 }
8013
8014 bs->stop = 0;
8015 bs->print_it = print_it_noop;
8016 }
8017
8018 static enum print_stop_action
8019 print_it_catch_solib (bpstat bs)
8020 {
8021 struct breakpoint *b = bs->breakpoint_at;
8022 struct ui_out *uiout = current_uiout;
8023
8024 annotate_catchpoint (b->number);
8025 maybe_print_thread_hit_breakpoint (uiout);
8026 if (b->disposition == disp_del)
8027 uiout->text ("Temporary catchpoint ");
8028 else
8029 uiout->text ("Catchpoint ");
8030 uiout->field_int ("bkptno", b->number);
8031 uiout->text ("\n");
8032 if (uiout->is_mi_like_p ())
8033 uiout->field_string ("disp", bpdisp_text (b->disposition));
8034 print_solib_event (1);
8035 return PRINT_SRC_AND_LOC;
8036 }
8037
8038 static void
8039 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8040 {
8041 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8042 struct value_print_options opts;
8043 struct ui_out *uiout = current_uiout;
8044
8045 get_user_print_options (&opts);
8046 /* Field 4, the address, is omitted (which makes the columns not
8047 line up too nicely with the headers, but the effect is relatively
8048 readable). */
8049 if (opts.addressprint)
8050 {
8051 annotate_field (4);
8052 uiout->field_skip ("addr");
8053 }
8054
8055 std::string msg;
8056 annotate_field (5);
8057 if (self->is_load)
8058 {
8059 if (self->regex)
8060 msg = string_printf (_("load of library matching %s"), self->regex);
8061 else
8062 msg = _("load of library");
8063 }
8064 else
8065 {
8066 if (self->regex)
8067 msg = string_printf (_("unload of library matching %s"), self->regex);
8068 else
8069 msg = _("unload of library");
8070 }
8071 uiout->field_string ("what", msg);
8072
8073 if (uiout->is_mi_like_p ())
8074 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8075 }
8076
8077 static void
8078 print_mention_catch_solib (struct breakpoint *b)
8079 {
8080 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8081
8082 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8083 self->is_load ? "load" : "unload");
8084 }
8085
8086 static void
8087 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8088 {
8089 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8090
8091 fprintf_unfiltered (fp, "%s %s",
8092 b->disposition == disp_del ? "tcatch" : "catch",
8093 self->is_load ? "load" : "unload");
8094 if (self->regex)
8095 fprintf_unfiltered (fp, " %s", self->regex);
8096 fprintf_unfiltered (fp, "\n");
8097 }
8098
8099 static struct breakpoint_ops catch_solib_breakpoint_ops;
8100
8101 /* Shared helper function (MI and CLI) for creating and installing
8102 a shared object event catchpoint. If IS_LOAD is non-zero then
8103 the events to be caught are load events, otherwise they are
8104 unload events. If IS_TEMP is non-zero the catchpoint is a
8105 temporary one. If ENABLED is non-zero the catchpoint is
8106 created in an enabled state. */
8107
8108 void
8109 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8110 {
8111 struct gdbarch *gdbarch = get_current_arch ();
8112
8113 if (!arg)
8114 arg = "";
8115 arg = skip_spaces (arg);
8116
8117 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8118
8119 if (*arg != '\0')
8120 {
8121 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8122 _("Invalid regexp")));
8123 c->regex = xstrdup (arg);
8124 }
8125
8126 c->is_load = is_load;
8127 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8128 &catch_solib_breakpoint_ops);
8129
8130 c->enable_state = enabled ? bp_enabled : bp_disabled;
8131
8132 install_breakpoint (0, std::move (c), 1);
8133 }
8134
8135 /* A helper function that does all the work for "catch load" and
8136 "catch unload". */
8137
8138 static void
8139 catch_load_or_unload (const char *arg, int from_tty, int is_load,
8140 struct cmd_list_element *command)
8141 {
8142 int tempflag;
8143 const int enabled = 1;
8144
8145 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8146
8147 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8148 }
8149
8150 static void
8151 catch_load_command_1 (const char *arg, int from_tty,
8152 struct cmd_list_element *command)
8153 {
8154 catch_load_or_unload (arg, from_tty, 1, command);
8155 }
8156
8157 static void
8158 catch_unload_command_1 (const char *arg, int from_tty,
8159 struct cmd_list_element *command)
8160 {
8161 catch_load_or_unload (arg, from_tty, 0, command);
8162 }
8163
8164 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8165 is non-zero, then make the breakpoint temporary. If COND_STRING is
8166 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8167 the breakpoint_ops structure associated to the catchpoint. */
8168
8169 void
8170 init_catchpoint (struct breakpoint *b,
8171 struct gdbarch *gdbarch, int tempflag,
8172 const char *cond_string,
8173 const struct breakpoint_ops *ops)
8174 {
8175 symtab_and_line sal;
8176 sal.pspace = current_program_space;
8177
8178 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8179
8180 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8181 b->disposition = tempflag ? disp_del : disp_donttouch;
8182 }
8183
8184 void
8185 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8186 {
8187 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8188 set_breakpoint_number (internal, b);
8189 if (is_tracepoint (b))
8190 set_tracepoint_count (breakpoint_count);
8191 if (!internal)
8192 mention (b);
8193 gdb::observers::breakpoint_created.notify (b);
8194
8195 if (update_gll)
8196 update_global_location_list (UGLL_MAY_INSERT);
8197 }
8198
8199 static void
8200 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8201 int tempflag, const char *cond_string,
8202 const struct breakpoint_ops *ops)
8203 {
8204 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8205
8206 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8207
8208 c->forked_inferior_pid = null_ptid;
8209
8210 install_breakpoint (0, std::move (c), 1);
8211 }
8212
8213 /* Exec catchpoints. */
8214
8215 /* An instance of this type is used to represent an exec catchpoint.
8216 A breakpoint is really of this type iff its ops pointer points to
8217 CATCH_EXEC_BREAKPOINT_OPS. */
8218
8219 struct exec_catchpoint : public breakpoint
8220 {
8221 ~exec_catchpoint () override;
8222
8223 /* Filename of a program whose exec triggered this catchpoint.
8224 This field is only valid immediately after this catchpoint has
8225 triggered. */
8226 char *exec_pathname;
8227 };
8228
8229 /* Exec catchpoint destructor. */
8230
8231 exec_catchpoint::~exec_catchpoint ()
8232 {
8233 xfree (this->exec_pathname);
8234 }
8235
8236 static int
8237 insert_catch_exec (struct bp_location *bl)
8238 {
8239 return target_insert_exec_catchpoint (inferior_ptid.pid ());
8240 }
8241
8242 static int
8243 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8244 {
8245 return target_remove_exec_catchpoint (inferior_ptid.pid ());
8246 }
8247
8248 static int
8249 breakpoint_hit_catch_exec (const struct bp_location *bl,
8250 const address_space *aspace, CORE_ADDR bp_addr,
8251 const struct target_waitstatus *ws)
8252 {
8253 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8254
8255 if (ws->kind != TARGET_WAITKIND_EXECD)
8256 return 0;
8257
8258 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8259 return 1;
8260 }
8261
8262 static enum print_stop_action
8263 print_it_catch_exec (bpstat bs)
8264 {
8265 struct ui_out *uiout = current_uiout;
8266 struct breakpoint *b = bs->breakpoint_at;
8267 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8268
8269 annotate_catchpoint (b->number);
8270 maybe_print_thread_hit_breakpoint (uiout);
8271 if (b->disposition == disp_del)
8272 uiout->text ("Temporary catchpoint ");
8273 else
8274 uiout->text ("Catchpoint ");
8275 if (uiout->is_mi_like_p ())
8276 {
8277 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8278 uiout->field_string ("disp", bpdisp_text (b->disposition));
8279 }
8280 uiout->field_int ("bkptno", b->number);
8281 uiout->text (" (exec'd ");
8282 uiout->field_string ("new-exec", c->exec_pathname);
8283 uiout->text ("), ");
8284
8285 return PRINT_SRC_AND_LOC;
8286 }
8287
8288 static void
8289 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8290 {
8291 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8292 struct value_print_options opts;
8293 struct ui_out *uiout = current_uiout;
8294
8295 get_user_print_options (&opts);
8296
8297 /* Field 4, the address, is omitted (which makes the columns
8298 not line up too nicely with the headers, but the effect
8299 is relatively readable). */
8300 if (opts.addressprint)
8301 uiout->field_skip ("addr");
8302 annotate_field (5);
8303 uiout->text ("exec");
8304 if (c->exec_pathname != NULL)
8305 {
8306 uiout->text (", program \"");
8307 uiout->field_string ("what", c->exec_pathname);
8308 uiout->text ("\" ");
8309 }
8310
8311 if (uiout->is_mi_like_p ())
8312 uiout->field_string ("catch-type", "exec");
8313 }
8314
8315 static void
8316 print_mention_catch_exec (struct breakpoint *b)
8317 {
8318 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8319 }
8320
8321 /* Implement the "print_recreate" breakpoint_ops method for exec
8322 catchpoints. */
8323
8324 static void
8325 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8326 {
8327 fprintf_unfiltered (fp, "catch exec");
8328 print_recreate_thread (b, fp);
8329 }
8330
8331 static struct breakpoint_ops catch_exec_breakpoint_ops;
8332
8333 static int
8334 hw_breakpoint_used_count (void)
8335 {
8336 int i = 0;
8337 struct breakpoint *b;
8338 struct bp_location *bl;
8339
8340 ALL_BREAKPOINTS (b)
8341 {
8342 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8343 for (bl = b->loc; bl; bl = bl->next)
8344 {
8345 /* Special types of hardware breakpoints may use more than
8346 one register. */
8347 i += b->ops->resources_needed (bl);
8348 }
8349 }
8350
8351 return i;
8352 }
8353
8354 /* Returns the resources B would use if it were a hardware
8355 watchpoint. */
8356
8357 static int
8358 hw_watchpoint_use_count (struct breakpoint *b)
8359 {
8360 int i = 0;
8361 struct bp_location *bl;
8362
8363 if (!breakpoint_enabled (b))
8364 return 0;
8365
8366 for (bl = b->loc; bl; bl = bl->next)
8367 {
8368 /* Special types of hardware watchpoints may use more than
8369 one register. */
8370 i += b->ops->resources_needed (bl);
8371 }
8372
8373 return i;
8374 }
8375
8376 /* Returns the sum the used resources of all hardware watchpoints of
8377 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8378 the sum of the used resources of all hardware watchpoints of other
8379 types _not_ TYPE. */
8380
8381 static int
8382 hw_watchpoint_used_count_others (struct breakpoint *except,
8383 enum bptype type, int *other_type_used)
8384 {
8385 int i = 0;
8386 struct breakpoint *b;
8387
8388 *other_type_used = 0;
8389 ALL_BREAKPOINTS (b)
8390 {
8391 if (b == except)
8392 continue;
8393 if (!breakpoint_enabled (b))
8394 continue;
8395
8396 if (b->type == type)
8397 i += hw_watchpoint_use_count (b);
8398 else if (is_hardware_watchpoint (b))
8399 *other_type_used = 1;
8400 }
8401
8402 return i;
8403 }
8404
8405 void
8406 disable_watchpoints_before_interactive_call_start (void)
8407 {
8408 struct breakpoint *b;
8409
8410 ALL_BREAKPOINTS (b)
8411 {
8412 if (is_watchpoint (b) && breakpoint_enabled (b))
8413 {
8414 b->enable_state = bp_call_disabled;
8415 update_global_location_list (UGLL_DONT_INSERT);
8416 }
8417 }
8418 }
8419
8420 void
8421 enable_watchpoints_after_interactive_call_stop (void)
8422 {
8423 struct breakpoint *b;
8424
8425 ALL_BREAKPOINTS (b)
8426 {
8427 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8428 {
8429 b->enable_state = bp_enabled;
8430 update_global_location_list (UGLL_MAY_INSERT);
8431 }
8432 }
8433 }
8434
8435 void
8436 disable_breakpoints_before_startup (void)
8437 {
8438 current_program_space->executing_startup = 1;
8439 update_global_location_list (UGLL_DONT_INSERT);
8440 }
8441
8442 void
8443 enable_breakpoints_after_startup (void)
8444 {
8445 current_program_space->executing_startup = 0;
8446 breakpoint_re_set ();
8447 }
8448
8449 /* Create a new single-step breakpoint for thread THREAD, with no
8450 locations. */
8451
8452 static struct breakpoint *
8453 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8454 {
8455 std::unique_ptr<breakpoint> b (new breakpoint ());
8456
8457 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8458 &momentary_breakpoint_ops);
8459
8460 b->disposition = disp_donttouch;
8461 b->frame_id = null_frame_id;
8462
8463 b->thread = thread;
8464 gdb_assert (b->thread != 0);
8465
8466 return add_to_breakpoint_chain (std::move (b));
8467 }
8468
8469 /* Set a momentary breakpoint of type TYPE at address specified by
8470 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8471 frame. */
8472
8473 breakpoint_up
8474 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8475 struct frame_id frame_id, enum bptype type)
8476 {
8477 struct breakpoint *b;
8478
8479 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8480 tail-called one. */
8481 gdb_assert (!frame_id_artificial_p (frame_id));
8482
8483 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8484 b->enable_state = bp_enabled;
8485 b->disposition = disp_donttouch;
8486 b->frame_id = frame_id;
8487
8488 b->thread = inferior_thread ()->global_num;
8489
8490 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8491
8492 return breakpoint_up (b);
8493 }
8494
8495 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8496 The new breakpoint will have type TYPE, use OPS as its
8497 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8498
8499 static struct breakpoint *
8500 momentary_breakpoint_from_master (struct breakpoint *orig,
8501 enum bptype type,
8502 const struct breakpoint_ops *ops,
8503 int loc_enabled)
8504 {
8505 struct breakpoint *copy;
8506
8507 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8508 copy->loc = allocate_bp_location (copy);
8509 set_breakpoint_location_function (copy->loc, 1);
8510
8511 copy->loc->gdbarch = orig->loc->gdbarch;
8512 copy->loc->requested_address = orig->loc->requested_address;
8513 copy->loc->address = orig->loc->address;
8514 copy->loc->section = orig->loc->section;
8515 copy->loc->pspace = orig->loc->pspace;
8516 copy->loc->probe = orig->loc->probe;
8517 copy->loc->line_number = orig->loc->line_number;
8518 copy->loc->symtab = orig->loc->symtab;
8519 copy->loc->enabled = loc_enabled;
8520 copy->frame_id = orig->frame_id;
8521 copy->thread = orig->thread;
8522 copy->pspace = orig->pspace;
8523
8524 copy->enable_state = bp_enabled;
8525 copy->disposition = disp_donttouch;
8526 copy->number = internal_breakpoint_number--;
8527
8528 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8529 return copy;
8530 }
8531
8532 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8533 ORIG is NULL. */
8534
8535 struct breakpoint *
8536 clone_momentary_breakpoint (struct breakpoint *orig)
8537 {
8538 /* If there's nothing to clone, then return nothing. */
8539 if (orig == NULL)
8540 return NULL;
8541
8542 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8543 }
8544
8545 breakpoint_up
8546 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8547 enum bptype type)
8548 {
8549 struct symtab_and_line sal;
8550
8551 sal = find_pc_line (pc, 0);
8552 sal.pc = pc;
8553 sal.section = find_pc_overlay (pc);
8554 sal.explicit_pc = 1;
8555
8556 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8557 }
8558 \f
8559
8560 /* Tell the user we have just set a breakpoint B. */
8561
8562 static void
8563 mention (struct breakpoint *b)
8564 {
8565 b->ops->print_mention (b);
8566 current_uiout->text ("\n");
8567 }
8568 \f
8569
8570 static int bp_loc_is_permanent (struct bp_location *loc);
8571
8572 static struct bp_location *
8573 add_location_to_breakpoint (struct breakpoint *b,
8574 const struct symtab_and_line *sal)
8575 {
8576 struct bp_location *loc, **tmp;
8577 CORE_ADDR adjusted_address;
8578 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8579
8580 if (loc_gdbarch == NULL)
8581 loc_gdbarch = b->gdbarch;
8582
8583 /* Adjust the breakpoint's address prior to allocating a location.
8584 Once we call allocate_bp_location(), that mostly uninitialized
8585 location will be placed on the location chain. Adjustment of the
8586 breakpoint may cause target_read_memory() to be called and we do
8587 not want its scan of the location chain to find a breakpoint and
8588 location that's only been partially initialized. */
8589 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8590 sal->pc, b->type);
8591
8592 /* Sort the locations by their ADDRESS. */
8593 loc = allocate_bp_location (b);
8594 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8595 tmp = &((*tmp)->next))
8596 ;
8597 loc->next = *tmp;
8598 *tmp = loc;
8599
8600 loc->requested_address = sal->pc;
8601 loc->address = adjusted_address;
8602 loc->pspace = sal->pspace;
8603 loc->probe.prob = sal->prob;
8604 loc->probe.objfile = sal->objfile;
8605 gdb_assert (loc->pspace != NULL);
8606 loc->section = sal->section;
8607 loc->gdbarch = loc_gdbarch;
8608 loc->line_number = sal->line;
8609 loc->symtab = sal->symtab;
8610 loc->symbol = sal->symbol;
8611 loc->msymbol = sal->msymbol;
8612 loc->objfile = sal->objfile;
8613
8614 set_breakpoint_location_function (loc,
8615 sal->explicit_pc || sal->explicit_line);
8616
8617 /* While by definition, permanent breakpoints are already present in the
8618 code, we don't mark the location as inserted. Normally one would expect
8619 that GDB could rely on that breakpoint instruction to stop the program,
8620 thus removing the need to insert its own breakpoint, except that executing
8621 the breakpoint instruction can kill the target instead of reporting a
8622 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8623 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8624 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8625 breakpoint be inserted normally results in QEMU knowing about the GDB
8626 breakpoint, and thus trap before the breakpoint instruction is executed.
8627 (If GDB later needs to continue execution past the permanent breakpoint,
8628 it manually increments the PC, thus avoiding executing the breakpoint
8629 instruction.) */
8630 if (bp_loc_is_permanent (loc))
8631 loc->permanent = 1;
8632
8633 return loc;
8634 }
8635 \f
8636
8637 /* See breakpoint.h. */
8638
8639 int
8640 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8641 {
8642 int len;
8643 CORE_ADDR addr;
8644 const gdb_byte *bpoint;
8645 gdb_byte *target_mem;
8646
8647 addr = address;
8648 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8649
8650 /* Software breakpoints unsupported? */
8651 if (bpoint == NULL)
8652 return 0;
8653
8654 target_mem = (gdb_byte *) alloca (len);
8655
8656 /* Enable the automatic memory restoration from breakpoints while
8657 we read the memory. Otherwise we could say about our temporary
8658 breakpoints they are permanent. */
8659 scoped_restore restore_memory
8660 = make_scoped_restore_show_memory_breakpoints (0);
8661
8662 if (target_read_memory (address, target_mem, len) == 0
8663 && memcmp (target_mem, bpoint, len) == 0)
8664 return 1;
8665
8666 return 0;
8667 }
8668
8669 /* Return 1 if LOC is pointing to a permanent breakpoint,
8670 return 0 otherwise. */
8671
8672 static int
8673 bp_loc_is_permanent (struct bp_location *loc)
8674 {
8675 gdb_assert (loc != NULL);
8676
8677 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8678 attempt to read from the addresses the locations of these breakpoint types
8679 point to. program_breakpoint_here_p, below, will attempt to read
8680 memory. */
8681 if (!breakpoint_address_is_meaningful (loc->owner))
8682 return 0;
8683
8684 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8685 switch_to_program_space_and_thread (loc->pspace);
8686 return program_breakpoint_here_p (loc->gdbarch, loc->address);
8687 }
8688
8689 /* Build a command list for the dprintf corresponding to the current
8690 settings of the dprintf style options. */
8691
8692 static void
8693 update_dprintf_command_list (struct breakpoint *b)
8694 {
8695 char *dprintf_args = b->extra_string;
8696 char *printf_line = NULL;
8697
8698 if (!dprintf_args)
8699 return;
8700
8701 dprintf_args = skip_spaces (dprintf_args);
8702
8703 /* Allow a comma, as it may have terminated a location, but don't
8704 insist on it. */
8705 if (*dprintf_args == ',')
8706 ++dprintf_args;
8707 dprintf_args = skip_spaces (dprintf_args);
8708
8709 if (*dprintf_args != '"')
8710 error (_("Bad format string, missing '\"'."));
8711
8712 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8713 printf_line = xstrprintf ("printf %s", dprintf_args);
8714 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8715 {
8716 if (!dprintf_function)
8717 error (_("No function supplied for dprintf call"));
8718
8719 if (dprintf_channel && strlen (dprintf_channel) > 0)
8720 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8721 dprintf_function,
8722 dprintf_channel,
8723 dprintf_args);
8724 else
8725 printf_line = xstrprintf ("call (void) %s (%s)",
8726 dprintf_function,
8727 dprintf_args);
8728 }
8729 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8730 {
8731 if (target_can_run_breakpoint_commands ())
8732 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8733 else
8734 {
8735 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8736 printf_line = xstrprintf ("printf %s", dprintf_args);
8737 }
8738 }
8739 else
8740 internal_error (__FILE__, __LINE__,
8741 _("Invalid dprintf style."));
8742
8743 gdb_assert (printf_line != NULL);
8744
8745 /* Manufacture a printf sequence. */
8746 struct command_line *printf_cmd_line
8747 = new struct command_line (simple_control, printf_line);
8748 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8749 command_lines_deleter ()));
8750 }
8751
8752 /* Update all dprintf commands, making their command lists reflect
8753 current style settings. */
8754
8755 static void
8756 update_dprintf_commands (const char *args, int from_tty,
8757 struct cmd_list_element *c)
8758 {
8759 struct breakpoint *b;
8760
8761 ALL_BREAKPOINTS (b)
8762 {
8763 if (b->type == bp_dprintf)
8764 update_dprintf_command_list (b);
8765 }
8766 }
8767
8768 /* Create a breakpoint with SAL as location. Use LOCATION
8769 as a description of the location, and COND_STRING
8770 as condition expression. If LOCATION is NULL then create an
8771 "address location" from the address in the SAL. */
8772
8773 static void
8774 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8775 gdb::array_view<const symtab_and_line> sals,
8776 event_location_up &&location,
8777 gdb::unique_xmalloc_ptr<char> filter,
8778 gdb::unique_xmalloc_ptr<char> cond_string,
8779 gdb::unique_xmalloc_ptr<char> extra_string,
8780 enum bptype type, enum bpdisp disposition,
8781 int thread, int task, int ignore_count,
8782 const struct breakpoint_ops *ops, int from_tty,
8783 int enabled, int internal, unsigned flags,
8784 int display_canonical)
8785 {
8786 int i;
8787
8788 if (type == bp_hardware_breakpoint)
8789 {
8790 int target_resources_ok;
8791
8792 i = hw_breakpoint_used_count ();
8793 target_resources_ok =
8794 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8795 i + 1, 0);
8796 if (target_resources_ok == 0)
8797 error (_("No hardware breakpoint support in the target."));
8798 else if (target_resources_ok < 0)
8799 error (_("Hardware breakpoints used exceeds limit."));
8800 }
8801
8802 gdb_assert (!sals.empty ());
8803
8804 for (const auto &sal : sals)
8805 {
8806 struct bp_location *loc;
8807
8808 if (from_tty)
8809 {
8810 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8811 if (!loc_gdbarch)
8812 loc_gdbarch = gdbarch;
8813
8814 describe_other_breakpoints (loc_gdbarch,
8815 sal.pspace, sal.pc, sal.section, thread);
8816 }
8817
8818 if (&sal == &sals[0])
8819 {
8820 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8821 b->thread = thread;
8822 b->task = task;
8823
8824 b->cond_string = cond_string.release ();
8825 b->extra_string = extra_string.release ();
8826 b->ignore_count = ignore_count;
8827 b->enable_state = enabled ? bp_enabled : bp_disabled;
8828 b->disposition = disposition;
8829
8830 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8831 b->loc->inserted = 1;
8832
8833 if (type == bp_static_tracepoint)
8834 {
8835 struct tracepoint *t = (struct tracepoint *) b;
8836 struct static_tracepoint_marker marker;
8837
8838 if (strace_marker_p (b))
8839 {
8840 /* We already know the marker exists, otherwise, we
8841 wouldn't see a sal for it. */
8842 const char *p
8843 = &event_location_to_string (b->location.get ())[3];
8844 const char *endp;
8845
8846 p = skip_spaces (p);
8847
8848 endp = skip_to_space (p);
8849
8850 t->static_trace_marker_id.assign (p, endp - p);
8851
8852 printf_filtered (_("Probed static tracepoint "
8853 "marker \"%s\"\n"),
8854 t->static_trace_marker_id.c_str ());
8855 }
8856 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8857 {
8858 t->static_trace_marker_id = std::move (marker.str_id);
8859
8860 printf_filtered (_("Probed static tracepoint "
8861 "marker \"%s\"\n"),
8862 t->static_trace_marker_id.c_str ());
8863 }
8864 else
8865 warning (_("Couldn't determine the static "
8866 "tracepoint marker to probe"));
8867 }
8868
8869 loc = b->loc;
8870 }
8871 else
8872 {
8873 loc = add_location_to_breakpoint (b, &sal);
8874 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8875 loc->inserted = 1;
8876 }
8877
8878 if (b->cond_string)
8879 {
8880 const char *arg = b->cond_string;
8881
8882 loc->cond = parse_exp_1 (&arg, loc->address,
8883 block_for_pc (loc->address), 0);
8884 if (*arg)
8885 error (_("Garbage '%s' follows condition"), arg);
8886 }
8887
8888 /* Dynamic printf requires and uses additional arguments on the
8889 command line, otherwise it's an error. */
8890 if (type == bp_dprintf)
8891 {
8892 if (b->extra_string)
8893 update_dprintf_command_list (b);
8894 else
8895 error (_("Format string required"));
8896 }
8897 else if (b->extra_string)
8898 error (_("Garbage '%s' at end of command"), b->extra_string);
8899 }
8900
8901 b->display_canonical = display_canonical;
8902 if (location != NULL)
8903 b->location = std::move (location);
8904 else
8905 b->location = new_address_location (b->loc->address, NULL, 0);
8906 b->filter = filter.release ();
8907 }
8908
8909 static void
8910 create_breakpoint_sal (struct gdbarch *gdbarch,
8911 gdb::array_view<const symtab_and_line> sals,
8912 event_location_up &&location,
8913 gdb::unique_xmalloc_ptr<char> filter,
8914 gdb::unique_xmalloc_ptr<char> cond_string,
8915 gdb::unique_xmalloc_ptr<char> extra_string,
8916 enum bptype type, enum bpdisp disposition,
8917 int thread, int task, int ignore_count,
8918 const struct breakpoint_ops *ops, int from_tty,
8919 int enabled, int internal, unsigned flags,
8920 int display_canonical)
8921 {
8922 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8923
8924 init_breakpoint_sal (b.get (), gdbarch,
8925 sals, std::move (location),
8926 std::move (filter),
8927 std::move (cond_string),
8928 std::move (extra_string),
8929 type, disposition,
8930 thread, task, ignore_count,
8931 ops, from_tty,
8932 enabled, internal, flags,
8933 display_canonical);
8934
8935 install_breakpoint (internal, std::move (b), 0);
8936 }
8937
8938 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8939 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8940 value. COND_STRING, if not NULL, specified the condition to be
8941 used for all breakpoints. Essentially the only case where
8942 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8943 function. In that case, it's still not possible to specify
8944 separate conditions for different overloaded functions, so
8945 we take just a single condition string.
8946
8947 NOTE: If the function succeeds, the caller is expected to cleanup
8948 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8949 array contents). If the function fails (error() is called), the
8950 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8951 COND and SALS arrays and each of those arrays contents. */
8952
8953 static void
8954 create_breakpoints_sal (struct gdbarch *gdbarch,
8955 struct linespec_result *canonical,
8956 gdb::unique_xmalloc_ptr<char> cond_string,
8957 gdb::unique_xmalloc_ptr<char> extra_string,
8958 enum bptype type, enum bpdisp disposition,
8959 int thread, int task, int ignore_count,
8960 const struct breakpoint_ops *ops, int from_tty,
8961 int enabled, int internal, unsigned flags)
8962 {
8963 if (canonical->pre_expanded)
8964 gdb_assert (canonical->lsals.size () == 1);
8965
8966 for (const auto &lsal : canonical->lsals)
8967 {
8968 /* Note that 'location' can be NULL in the case of a plain
8969 'break', without arguments. */
8970 event_location_up location
8971 = (canonical->location != NULL
8972 ? copy_event_location (canonical->location.get ()) : NULL);
8973 gdb::unique_xmalloc_ptr<char> filter_string
8974 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8975
8976 create_breakpoint_sal (gdbarch, lsal.sals,
8977 std::move (location),
8978 std::move (filter_string),
8979 std::move (cond_string),
8980 std::move (extra_string),
8981 type, disposition,
8982 thread, task, ignore_count, ops,
8983 from_tty, enabled, internal, flags,
8984 canonical->special_display);
8985 }
8986 }
8987
8988 /* Parse LOCATION which is assumed to be a SAL specification possibly
8989 followed by conditionals. On return, SALS contains an array of SAL
8990 addresses found. LOCATION points to the end of the SAL (for
8991 linespec locations).
8992
8993 The array and the line spec strings are allocated on the heap, it is
8994 the caller's responsibility to free them. */
8995
8996 static void
8997 parse_breakpoint_sals (const struct event_location *location,
8998 struct linespec_result *canonical)
8999 {
9000 struct symtab_and_line cursal;
9001
9002 if (event_location_type (location) == LINESPEC_LOCATION)
9003 {
9004 const char *spec = get_linespec_location (location)->spec_string;
9005
9006 if (spec == NULL)
9007 {
9008 /* The last displayed codepoint, if it's valid, is our default
9009 breakpoint address. */
9010 if (last_displayed_sal_is_valid ())
9011 {
9012 /* Set sal's pspace, pc, symtab, and line to the values
9013 corresponding to the last call to print_frame_info.
9014 Be sure to reinitialize LINE with NOTCURRENT == 0
9015 as the breakpoint line number is inappropriate otherwise.
9016 find_pc_line would adjust PC, re-set it back. */
9017 symtab_and_line sal = get_last_displayed_sal ();
9018 CORE_ADDR pc = sal.pc;
9019
9020 sal = find_pc_line (pc, 0);
9021
9022 /* "break" without arguments is equivalent to "break *PC"
9023 where PC is the last displayed codepoint's address. So
9024 make sure to set sal.explicit_pc to prevent GDB from
9025 trying to expand the list of sals to include all other
9026 instances with the same symtab and line. */
9027 sal.pc = pc;
9028 sal.explicit_pc = 1;
9029
9030 struct linespec_sals lsal;
9031 lsal.sals = {sal};
9032 lsal.canonical = NULL;
9033
9034 canonical->lsals.push_back (std::move (lsal));
9035 return;
9036 }
9037 else
9038 error (_("No default breakpoint address now."));
9039 }
9040 }
9041
9042 /* Force almost all breakpoints to be in terms of the
9043 current_source_symtab (which is decode_line_1's default).
9044 This should produce the results we want almost all of the
9045 time while leaving default_breakpoint_* alone.
9046
9047 ObjC: However, don't match an Objective-C method name which
9048 may have a '+' or '-' succeeded by a '['. */
9049 cursal = get_current_source_symtab_and_line ();
9050 if (last_displayed_sal_is_valid ())
9051 {
9052 const char *spec = NULL;
9053
9054 if (event_location_type (location) == LINESPEC_LOCATION)
9055 spec = get_linespec_location (location)->spec_string;
9056
9057 if (!cursal.symtab
9058 || (spec != NULL
9059 && strchr ("+-", spec[0]) != NULL
9060 && spec[1] != '['))
9061 {
9062 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9063 get_last_displayed_symtab (),
9064 get_last_displayed_line (),
9065 canonical, NULL, NULL);
9066 return;
9067 }
9068 }
9069
9070 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9071 cursal.symtab, cursal.line, canonical, NULL, NULL);
9072 }
9073
9074
9075 /* Convert each SAL into a real PC. Verify that the PC can be
9076 inserted as a breakpoint. If it can't throw an error. */
9077
9078 static void
9079 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9080 {
9081 for (auto &sal : sals)
9082 resolve_sal_pc (&sal);
9083 }
9084
9085 /* Fast tracepoints may have restrictions on valid locations. For
9086 instance, a fast tracepoint using a jump instead of a trap will
9087 likely have to overwrite more bytes than a trap would, and so can
9088 only be placed where the instruction is longer than the jump, or a
9089 multi-instruction sequence does not have a jump into the middle of
9090 it, etc. */
9091
9092 static void
9093 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9094 gdb::array_view<const symtab_and_line> sals)
9095 {
9096 for (const auto &sal : sals)
9097 {
9098 struct gdbarch *sarch;
9099
9100 sarch = get_sal_arch (sal);
9101 /* We fall back to GDBARCH if there is no architecture
9102 associated with SAL. */
9103 if (sarch == NULL)
9104 sarch = gdbarch;
9105 std::string msg;
9106 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9107 error (_("May not have a fast tracepoint at %s%s"),
9108 paddress (sarch, sal.pc), msg.c_str ());
9109 }
9110 }
9111
9112 /* Given TOK, a string specification of condition and thread, as
9113 accepted by the 'break' command, extract the condition
9114 string and thread number and set *COND_STRING and *THREAD.
9115 PC identifies the context at which the condition should be parsed.
9116 If no condition is found, *COND_STRING is set to NULL.
9117 If no thread is found, *THREAD is set to -1. */
9118
9119 static void
9120 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9121 char **cond_string, int *thread, int *task,
9122 char **rest)
9123 {
9124 *cond_string = NULL;
9125 *thread = -1;
9126 *task = 0;
9127 *rest = NULL;
9128
9129 while (tok && *tok)
9130 {
9131 const char *end_tok;
9132 int toklen;
9133 const char *cond_start = NULL;
9134 const char *cond_end = NULL;
9135
9136 tok = skip_spaces (tok);
9137
9138 if ((*tok == '"' || *tok == ',') && rest)
9139 {
9140 *rest = savestring (tok, strlen (tok));
9141 return;
9142 }
9143
9144 end_tok = skip_to_space (tok);
9145
9146 toklen = end_tok - tok;
9147
9148 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9149 {
9150 tok = cond_start = end_tok + 1;
9151 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9152 cond_end = tok;
9153 *cond_string = savestring (cond_start, cond_end - cond_start);
9154 }
9155 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9156 {
9157 const char *tmptok;
9158 struct thread_info *thr;
9159
9160 tok = end_tok + 1;
9161 thr = parse_thread_id (tok, &tmptok);
9162 if (tok == tmptok)
9163 error (_("Junk after thread keyword."));
9164 *thread = thr->global_num;
9165 tok = tmptok;
9166 }
9167 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9168 {
9169 char *tmptok;
9170
9171 tok = end_tok + 1;
9172 *task = strtol (tok, &tmptok, 0);
9173 if (tok == tmptok)
9174 error (_("Junk after task keyword."));
9175 if (!valid_task_id (*task))
9176 error (_("Unknown task %d."), *task);
9177 tok = tmptok;
9178 }
9179 else if (rest)
9180 {
9181 *rest = savestring (tok, strlen (tok));
9182 return;
9183 }
9184 else
9185 error (_("Junk at end of arguments."));
9186 }
9187 }
9188
9189 /* Decode a static tracepoint marker spec. */
9190
9191 static std::vector<symtab_and_line>
9192 decode_static_tracepoint_spec (const char **arg_p)
9193 {
9194 const char *p = &(*arg_p)[3];
9195 const char *endp;
9196
9197 p = skip_spaces (p);
9198
9199 endp = skip_to_space (p);
9200
9201 std::string marker_str (p, endp - p);
9202
9203 std::vector<static_tracepoint_marker> markers
9204 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9205 if (markers.empty ())
9206 error (_("No known static tracepoint marker named %s"),
9207 marker_str.c_str ());
9208
9209 std::vector<symtab_and_line> sals;
9210 sals.reserve (markers.size ());
9211
9212 for (const static_tracepoint_marker &marker : markers)
9213 {
9214 symtab_and_line sal = find_pc_line (marker.address, 0);
9215 sal.pc = marker.address;
9216 sals.push_back (sal);
9217 }
9218
9219 *arg_p = endp;
9220 return sals;
9221 }
9222
9223 /* See breakpoint.h. */
9224
9225 int
9226 create_breakpoint (struct gdbarch *gdbarch,
9227 const struct event_location *location,
9228 const char *cond_string,
9229 int thread, const char *extra_string,
9230 int parse_extra,
9231 int tempflag, enum bptype type_wanted,
9232 int ignore_count,
9233 enum auto_boolean pending_break_support,
9234 const struct breakpoint_ops *ops,
9235 int from_tty, int enabled, int internal,
9236 unsigned flags)
9237 {
9238 struct linespec_result canonical;
9239 struct cleanup *bkpt_chain = NULL;
9240 int pending = 0;
9241 int task = 0;
9242 int prev_bkpt_count = breakpoint_count;
9243
9244 gdb_assert (ops != NULL);
9245
9246 /* If extra_string isn't useful, set it to NULL. */
9247 if (extra_string != NULL && *extra_string == '\0')
9248 extra_string = NULL;
9249
9250 TRY
9251 {
9252 ops->create_sals_from_location (location, &canonical, type_wanted);
9253 }
9254 CATCH (e, RETURN_MASK_ERROR)
9255 {
9256 /* If caller is interested in rc value from parse, set
9257 value. */
9258 if (e.error == NOT_FOUND_ERROR)
9259 {
9260 /* If pending breakpoint support is turned off, throw
9261 error. */
9262
9263 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9264 throw_exception (e);
9265
9266 exception_print (gdb_stderr, e);
9267
9268 /* If pending breakpoint support is auto query and the user
9269 selects no, then simply return the error code. */
9270 if (pending_break_support == AUTO_BOOLEAN_AUTO
9271 && !nquery (_("Make %s pending on future shared library load? "),
9272 bptype_string (type_wanted)))
9273 return 0;
9274
9275 /* At this point, either the user was queried about setting
9276 a pending breakpoint and selected yes, or pending
9277 breakpoint behavior is on and thus a pending breakpoint
9278 is defaulted on behalf of the user. */
9279 pending = 1;
9280 }
9281 else
9282 throw_exception (e);
9283 }
9284 END_CATCH
9285
9286 if (!pending && canonical.lsals.empty ())
9287 return 0;
9288
9289 /* ----------------------------- SNIP -----------------------------
9290 Anything added to the cleanup chain beyond this point is assumed
9291 to be part of a breakpoint. If the breakpoint create succeeds
9292 then the memory is not reclaimed. */
9293 bkpt_chain = make_cleanup (null_cleanup, 0);
9294
9295 /* Resolve all line numbers to PC's and verify that the addresses
9296 are ok for the target. */
9297 if (!pending)
9298 {
9299 for (auto &lsal : canonical.lsals)
9300 breakpoint_sals_to_pc (lsal.sals);
9301 }
9302
9303 /* Fast tracepoints may have additional restrictions on location. */
9304 if (!pending && type_wanted == bp_fast_tracepoint)
9305 {
9306 for (const auto &lsal : canonical.lsals)
9307 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9308 }
9309
9310 /* Verify that condition can be parsed, before setting any
9311 breakpoints. Allocate a separate condition expression for each
9312 breakpoint. */
9313 if (!pending)
9314 {
9315 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9316 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9317
9318 if (parse_extra)
9319 {
9320 char *rest;
9321 char *cond;
9322
9323 const linespec_sals &lsal = canonical.lsals[0];
9324
9325 /* Here we only parse 'arg' to separate condition
9326 from thread number, so parsing in context of first
9327 sal is OK. When setting the breakpoint we'll
9328 re-parse it in context of each sal. */
9329
9330 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9331 &cond, &thread, &task, &rest);
9332 cond_string_copy.reset (cond);
9333 extra_string_copy.reset (rest);
9334 }
9335 else
9336 {
9337 if (type_wanted != bp_dprintf
9338 && extra_string != NULL && *extra_string != '\0')
9339 error (_("Garbage '%s' at end of location"), extra_string);
9340
9341 /* Create a private copy of condition string. */
9342 if (cond_string)
9343 cond_string_copy.reset (xstrdup (cond_string));
9344 /* Create a private copy of any extra string. */
9345 if (extra_string)
9346 extra_string_copy.reset (xstrdup (extra_string));
9347 }
9348
9349 ops->create_breakpoints_sal (gdbarch, &canonical,
9350 std::move (cond_string_copy),
9351 std::move (extra_string_copy),
9352 type_wanted,
9353 tempflag ? disp_del : disp_donttouch,
9354 thread, task, ignore_count, ops,
9355 from_tty, enabled, internal, flags);
9356 }
9357 else
9358 {
9359 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9360
9361 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9362 b->location = copy_event_location (location);
9363
9364 if (parse_extra)
9365 b->cond_string = NULL;
9366 else
9367 {
9368 /* Create a private copy of condition string. */
9369 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9370 b->thread = thread;
9371 }
9372
9373 /* Create a private copy of any extra string. */
9374 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9375 b->ignore_count = ignore_count;
9376 b->disposition = tempflag ? disp_del : disp_donttouch;
9377 b->condition_not_parsed = 1;
9378 b->enable_state = enabled ? bp_enabled : bp_disabled;
9379 if ((type_wanted != bp_breakpoint
9380 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9381 b->pspace = current_program_space;
9382
9383 install_breakpoint (internal, std::move (b), 0);
9384 }
9385
9386 if (canonical.lsals.size () > 1)
9387 {
9388 warning (_("Multiple breakpoints were set.\nUse the "
9389 "\"delete\" command to delete unwanted breakpoints."));
9390 prev_breakpoint_count = prev_bkpt_count;
9391 }
9392
9393 /* That's it. Discard the cleanups for data inserted into the
9394 breakpoint. */
9395 discard_cleanups (bkpt_chain);
9396
9397 /* error call may happen here - have BKPT_CHAIN already discarded. */
9398 update_global_location_list (UGLL_MAY_INSERT);
9399
9400 return 1;
9401 }
9402
9403 /* Set a breakpoint.
9404 ARG is a string describing breakpoint address,
9405 condition, and thread.
9406 FLAG specifies if a breakpoint is hardware on,
9407 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9408 and BP_TEMPFLAG. */
9409
9410 static void
9411 break_command_1 (const char *arg, int flag, int from_tty)
9412 {
9413 int tempflag = flag & BP_TEMPFLAG;
9414 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9415 ? bp_hardware_breakpoint
9416 : bp_breakpoint);
9417 struct breakpoint_ops *ops;
9418
9419 event_location_up location = string_to_event_location (&arg, current_language);
9420
9421 /* Matching breakpoints on probes. */
9422 if (location != NULL
9423 && event_location_type (location.get ()) == PROBE_LOCATION)
9424 ops = &bkpt_probe_breakpoint_ops;
9425 else
9426 ops = &bkpt_breakpoint_ops;
9427
9428 create_breakpoint (get_current_arch (),
9429 location.get (),
9430 NULL, 0, arg, 1 /* parse arg */,
9431 tempflag, type_wanted,
9432 0 /* Ignore count */,
9433 pending_break_support,
9434 ops,
9435 from_tty,
9436 1 /* enabled */,
9437 0 /* internal */,
9438 0);
9439 }
9440
9441 /* Helper function for break_command_1 and disassemble_command. */
9442
9443 void
9444 resolve_sal_pc (struct symtab_and_line *sal)
9445 {
9446 CORE_ADDR pc;
9447
9448 if (sal->pc == 0 && sal->symtab != NULL)
9449 {
9450 if (!find_line_pc (sal->symtab, sal->line, &pc))
9451 error (_("No line %d in file \"%s\"."),
9452 sal->line, symtab_to_filename_for_display (sal->symtab));
9453 sal->pc = pc;
9454
9455 /* If this SAL corresponds to a breakpoint inserted using a line
9456 number, then skip the function prologue if necessary. */
9457 if (sal->explicit_line)
9458 skip_prologue_sal (sal);
9459 }
9460
9461 if (sal->section == 0 && sal->symtab != NULL)
9462 {
9463 const struct blockvector *bv;
9464 const struct block *b;
9465 struct symbol *sym;
9466
9467 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9468 SYMTAB_COMPUNIT (sal->symtab));
9469 if (bv != NULL)
9470 {
9471 sym = block_linkage_function (b);
9472 if (sym != NULL)
9473 {
9474 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9475 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9476 sym);
9477 }
9478 else
9479 {
9480 /* It really is worthwhile to have the section, so we'll
9481 just have to look harder. This case can be executed
9482 if we have line numbers but no functions (as can
9483 happen in assembly source). */
9484
9485 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9486 switch_to_program_space_and_thread (sal->pspace);
9487
9488 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9489 if (msym.minsym)
9490 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9491 }
9492 }
9493 }
9494 }
9495
9496 void
9497 break_command (const char *arg, int from_tty)
9498 {
9499 break_command_1 (arg, 0, from_tty);
9500 }
9501
9502 void
9503 tbreak_command (const char *arg, int from_tty)
9504 {
9505 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9506 }
9507
9508 static void
9509 hbreak_command (const char *arg, int from_tty)
9510 {
9511 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9512 }
9513
9514 static void
9515 thbreak_command (const char *arg, int from_tty)
9516 {
9517 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9518 }
9519
9520 static void
9521 stop_command (const char *arg, int from_tty)
9522 {
9523 printf_filtered (_("Specify the type of breakpoint to set.\n\
9524 Usage: stop in <function | address>\n\
9525 stop at <line>\n"));
9526 }
9527
9528 static void
9529 stopin_command (const char *arg, int from_tty)
9530 {
9531 int badInput = 0;
9532
9533 if (arg == (char *) NULL)
9534 badInput = 1;
9535 else if (*arg != '*')
9536 {
9537 const char *argptr = arg;
9538 int hasColon = 0;
9539
9540 /* Look for a ':'. If this is a line number specification, then
9541 say it is bad, otherwise, it should be an address or
9542 function/method name. */
9543 while (*argptr && !hasColon)
9544 {
9545 hasColon = (*argptr == ':');
9546 argptr++;
9547 }
9548
9549 if (hasColon)
9550 badInput = (*argptr != ':'); /* Not a class::method */
9551 else
9552 badInput = isdigit (*arg); /* a simple line number */
9553 }
9554
9555 if (badInput)
9556 printf_filtered (_("Usage: stop in <function | address>\n"));
9557 else
9558 break_command_1 (arg, 0, from_tty);
9559 }
9560
9561 static void
9562 stopat_command (const char *arg, int from_tty)
9563 {
9564 int badInput = 0;
9565
9566 if (arg == (char *) NULL || *arg == '*') /* no line number */
9567 badInput = 1;
9568 else
9569 {
9570 const char *argptr = arg;
9571 int hasColon = 0;
9572
9573 /* Look for a ':'. If there is a '::' then get out, otherwise
9574 it is probably a line number. */
9575 while (*argptr && !hasColon)
9576 {
9577 hasColon = (*argptr == ':');
9578 argptr++;
9579 }
9580
9581 if (hasColon)
9582 badInput = (*argptr == ':'); /* we have class::method */
9583 else
9584 badInput = !isdigit (*arg); /* not a line number */
9585 }
9586
9587 if (badInput)
9588 printf_filtered (_("Usage: stop at <line>\n"));
9589 else
9590 break_command_1 (arg, 0, from_tty);
9591 }
9592
9593 /* The dynamic printf command is mostly like a regular breakpoint, but
9594 with a prewired command list consisting of a single output command,
9595 built from extra arguments supplied on the dprintf command
9596 line. */
9597
9598 static void
9599 dprintf_command (const char *arg, int from_tty)
9600 {
9601 event_location_up location = string_to_event_location (&arg, current_language);
9602
9603 /* If non-NULL, ARG should have been advanced past the location;
9604 the next character must be ','. */
9605 if (arg != NULL)
9606 {
9607 if (arg[0] != ',' || arg[1] == '\0')
9608 error (_("Format string required"));
9609 else
9610 {
9611 /* Skip the comma. */
9612 ++arg;
9613 }
9614 }
9615
9616 create_breakpoint (get_current_arch (),
9617 location.get (),
9618 NULL, 0, arg, 1 /* parse arg */,
9619 0, bp_dprintf,
9620 0 /* Ignore count */,
9621 pending_break_support,
9622 &dprintf_breakpoint_ops,
9623 from_tty,
9624 1 /* enabled */,
9625 0 /* internal */,
9626 0);
9627 }
9628
9629 static void
9630 agent_printf_command (const char *arg, int from_tty)
9631 {
9632 error (_("May only run agent-printf on the target"));
9633 }
9634
9635 /* Implement the "breakpoint_hit" breakpoint_ops method for
9636 ranged breakpoints. */
9637
9638 static int
9639 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9640 const address_space *aspace,
9641 CORE_ADDR bp_addr,
9642 const struct target_waitstatus *ws)
9643 {
9644 if (ws->kind != TARGET_WAITKIND_STOPPED
9645 || ws->value.sig != GDB_SIGNAL_TRAP)
9646 return 0;
9647
9648 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9649 bl->length, aspace, bp_addr);
9650 }
9651
9652 /* Implement the "resources_needed" breakpoint_ops method for
9653 ranged breakpoints. */
9654
9655 static int
9656 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9657 {
9658 return target_ranged_break_num_registers ();
9659 }
9660
9661 /* Implement the "print_it" breakpoint_ops method for
9662 ranged breakpoints. */
9663
9664 static enum print_stop_action
9665 print_it_ranged_breakpoint (bpstat bs)
9666 {
9667 struct breakpoint *b = bs->breakpoint_at;
9668 struct bp_location *bl = b->loc;
9669 struct ui_out *uiout = current_uiout;
9670
9671 gdb_assert (b->type == bp_hardware_breakpoint);
9672
9673 /* Ranged breakpoints have only one location. */
9674 gdb_assert (bl && bl->next == NULL);
9675
9676 annotate_breakpoint (b->number);
9677
9678 maybe_print_thread_hit_breakpoint (uiout);
9679
9680 if (b->disposition == disp_del)
9681 uiout->text ("Temporary ranged breakpoint ");
9682 else
9683 uiout->text ("Ranged breakpoint ");
9684 if (uiout->is_mi_like_p ())
9685 {
9686 uiout->field_string ("reason",
9687 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9688 uiout->field_string ("disp", bpdisp_text (b->disposition));
9689 }
9690 uiout->field_int ("bkptno", b->number);
9691 uiout->text (", ");
9692
9693 return PRINT_SRC_AND_LOC;
9694 }
9695
9696 /* Implement the "print_one" breakpoint_ops method for
9697 ranged breakpoints. */
9698
9699 static void
9700 print_one_ranged_breakpoint (struct breakpoint *b,
9701 struct bp_location **last_loc)
9702 {
9703 struct bp_location *bl = b->loc;
9704 struct value_print_options opts;
9705 struct ui_out *uiout = current_uiout;
9706
9707 /* Ranged breakpoints have only one location. */
9708 gdb_assert (bl && bl->next == NULL);
9709
9710 get_user_print_options (&opts);
9711
9712 if (opts.addressprint)
9713 /* We don't print the address range here, it will be printed later
9714 by print_one_detail_ranged_breakpoint. */
9715 uiout->field_skip ("addr");
9716 annotate_field (5);
9717 print_breakpoint_location (b, bl);
9718 *last_loc = bl;
9719 }
9720
9721 /* Implement the "print_one_detail" breakpoint_ops method for
9722 ranged breakpoints. */
9723
9724 static void
9725 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9726 struct ui_out *uiout)
9727 {
9728 CORE_ADDR address_start, address_end;
9729 struct bp_location *bl = b->loc;
9730 string_file stb;
9731
9732 gdb_assert (bl);
9733
9734 address_start = bl->address;
9735 address_end = address_start + bl->length - 1;
9736
9737 uiout->text ("\taddress range: ");
9738 stb.printf ("[%s, %s]",
9739 print_core_address (bl->gdbarch, address_start),
9740 print_core_address (bl->gdbarch, address_end));
9741 uiout->field_stream ("addr", stb);
9742 uiout->text ("\n");
9743 }
9744
9745 /* Implement the "print_mention" breakpoint_ops method for
9746 ranged breakpoints. */
9747
9748 static void
9749 print_mention_ranged_breakpoint (struct breakpoint *b)
9750 {
9751 struct bp_location *bl = b->loc;
9752 struct ui_out *uiout = current_uiout;
9753
9754 gdb_assert (bl);
9755 gdb_assert (b->type == bp_hardware_breakpoint);
9756
9757 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9758 b->number, paddress (bl->gdbarch, bl->address),
9759 paddress (bl->gdbarch, bl->address + bl->length - 1));
9760 }
9761
9762 /* Implement the "print_recreate" breakpoint_ops method for
9763 ranged breakpoints. */
9764
9765 static void
9766 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9767 {
9768 fprintf_unfiltered (fp, "break-range %s, %s",
9769 event_location_to_string (b->location.get ()),
9770 event_location_to_string (b->location_range_end.get ()));
9771 print_recreate_thread (b, fp);
9772 }
9773
9774 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9775
9776 static struct breakpoint_ops ranged_breakpoint_ops;
9777
9778 /* Find the address where the end of the breakpoint range should be
9779 placed, given the SAL of the end of the range. This is so that if
9780 the user provides a line number, the end of the range is set to the
9781 last instruction of the given line. */
9782
9783 static CORE_ADDR
9784 find_breakpoint_range_end (struct symtab_and_line sal)
9785 {
9786 CORE_ADDR end;
9787
9788 /* If the user provided a PC value, use it. Otherwise,
9789 find the address of the end of the given location. */
9790 if (sal.explicit_pc)
9791 end = sal.pc;
9792 else
9793 {
9794 int ret;
9795 CORE_ADDR start;
9796
9797 ret = find_line_pc_range (sal, &start, &end);
9798 if (!ret)
9799 error (_("Could not find location of the end of the range."));
9800
9801 /* find_line_pc_range returns the start of the next line. */
9802 end--;
9803 }
9804
9805 return end;
9806 }
9807
9808 /* Implement the "break-range" CLI command. */
9809
9810 static void
9811 break_range_command (const char *arg, int from_tty)
9812 {
9813 const char *arg_start;
9814 struct linespec_result canonical_start, canonical_end;
9815 int bp_count, can_use_bp, length;
9816 CORE_ADDR end;
9817 struct breakpoint *b;
9818
9819 /* We don't support software ranged breakpoints. */
9820 if (target_ranged_break_num_registers () < 0)
9821 error (_("This target does not support hardware ranged breakpoints."));
9822
9823 bp_count = hw_breakpoint_used_count ();
9824 bp_count += target_ranged_break_num_registers ();
9825 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9826 bp_count, 0);
9827 if (can_use_bp < 0)
9828 error (_("Hardware breakpoints used exceeds limit."));
9829
9830 arg = skip_spaces (arg);
9831 if (arg == NULL || arg[0] == '\0')
9832 error(_("No address range specified."));
9833
9834 arg_start = arg;
9835 event_location_up start_location = string_to_event_location (&arg,
9836 current_language);
9837 parse_breakpoint_sals (start_location.get (), &canonical_start);
9838
9839 if (arg[0] != ',')
9840 error (_("Too few arguments."));
9841 else if (canonical_start.lsals.empty ())
9842 error (_("Could not find location of the beginning of the range."));
9843
9844 const linespec_sals &lsal_start = canonical_start.lsals[0];
9845
9846 if (canonical_start.lsals.size () > 1
9847 || lsal_start.sals.size () != 1)
9848 error (_("Cannot create a ranged breakpoint with multiple locations."));
9849
9850 const symtab_and_line &sal_start = lsal_start.sals[0];
9851 std::string addr_string_start (arg_start, arg - arg_start);
9852
9853 arg++; /* Skip the comma. */
9854 arg = skip_spaces (arg);
9855
9856 /* Parse the end location. */
9857
9858 arg_start = arg;
9859
9860 /* We call decode_line_full directly here instead of using
9861 parse_breakpoint_sals because we need to specify the start location's
9862 symtab and line as the default symtab and line for the end of the
9863 range. This makes it possible to have ranges like "foo.c:27, +14",
9864 where +14 means 14 lines from the start location. */
9865 event_location_up end_location = string_to_event_location (&arg,
9866 current_language);
9867 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9868 sal_start.symtab, sal_start.line,
9869 &canonical_end, NULL, NULL);
9870
9871 if (canonical_end.lsals.empty ())
9872 error (_("Could not find location of the end of the range."));
9873
9874 const linespec_sals &lsal_end = canonical_end.lsals[0];
9875 if (canonical_end.lsals.size () > 1
9876 || lsal_end.sals.size () != 1)
9877 error (_("Cannot create a ranged breakpoint with multiple locations."));
9878
9879 const symtab_and_line &sal_end = lsal_end.sals[0];
9880
9881 end = find_breakpoint_range_end (sal_end);
9882 if (sal_start.pc > end)
9883 error (_("Invalid address range, end precedes start."));
9884
9885 length = end - sal_start.pc + 1;
9886 if (length < 0)
9887 /* Length overflowed. */
9888 error (_("Address range too large."));
9889 else if (length == 1)
9890 {
9891 /* This range is simple enough to be handled by
9892 the `hbreak' command. */
9893 hbreak_command (&addr_string_start[0], 1);
9894
9895 return;
9896 }
9897
9898 /* Now set up the breakpoint. */
9899 b = set_raw_breakpoint (get_current_arch (), sal_start,
9900 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9901 set_breakpoint_count (breakpoint_count + 1);
9902 b->number = breakpoint_count;
9903 b->disposition = disp_donttouch;
9904 b->location = std::move (start_location);
9905 b->location_range_end = std::move (end_location);
9906 b->loc->length = length;
9907
9908 mention (b);
9909 gdb::observers::breakpoint_created.notify (b);
9910 update_global_location_list (UGLL_MAY_INSERT);
9911 }
9912
9913 /* Return non-zero if EXP is verified as constant. Returned zero
9914 means EXP is variable. Also the constant detection may fail for
9915 some constant expressions and in such case still falsely return
9916 zero. */
9917
9918 static int
9919 watchpoint_exp_is_const (const struct expression *exp)
9920 {
9921 int i = exp->nelts;
9922
9923 while (i > 0)
9924 {
9925 int oplenp, argsp;
9926
9927 /* We are only interested in the descriptor of each element. */
9928 operator_length (exp, i, &oplenp, &argsp);
9929 i -= oplenp;
9930
9931 switch (exp->elts[i].opcode)
9932 {
9933 case BINOP_ADD:
9934 case BINOP_SUB:
9935 case BINOP_MUL:
9936 case BINOP_DIV:
9937 case BINOP_REM:
9938 case BINOP_MOD:
9939 case BINOP_LSH:
9940 case BINOP_RSH:
9941 case BINOP_LOGICAL_AND:
9942 case BINOP_LOGICAL_OR:
9943 case BINOP_BITWISE_AND:
9944 case BINOP_BITWISE_IOR:
9945 case BINOP_BITWISE_XOR:
9946 case BINOP_EQUAL:
9947 case BINOP_NOTEQUAL:
9948 case BINOP_LESS:
9949 case BINOP_GTR:
9950 case BINOP_LEQ:
9951 case BINOP_GEQ:
9952 case BINOP_REPEAT:
9953 case BINOP_COMMA:
9954 case BINOP_EXP:
9955 case BINOP_MIN:
9956 case BINOP_MAX:
9957 case BINOP_INTDIV:
9958 case BINOP_CONCAT:
9959 case TERNOP_COND:
9960 case TERNOP_SLICE:
9961
9962 case OP_LONG:
9963 case OP_FLOAT:
9964 case OP_LAST:
9965 case OP_COMPLEX:
9966 case OP_STRING:
9967 case OP_ARRAY:
9968 case OP_TYPE:
9969 case OP_TYPEOF:
9970 case OP_DECLTYPE:
9971 case OP_TYPEID:
9972 case OP_NAME:
9973 case OP_OBJC_NSSTRING:
9974
9975 case UNOP_NEG:
9976 case UNOP_LOGICAL_NOT:
9977 case UNOP_COMPLEMENT:
9978 case UNOP_ADDR:
9979 case UNOP_HIGH:
9980 case UNOP_CAST:
9981
9982 case UNOP_CAST_TYPE:
9983 case UNOP_REINTERPRET_CAST:
9984 case UNOP_DYNAMIC_CAST:
9985 /* Unary, binary and ternary operators: We have to check
9986 their operands. If they are constant, then so is the
9987 result of that operation. For instance, if A and B are
9988 determined to be constants, then so is "A + B".
9989
9990 UNOP_IND is one exception to the rule above, because the
9991 value of *ADDR is not necessarily a constant, even when
9992 ADDR is. */
9993 break;
9994
9995 case OP_VAR_VALUE:
9996 /* Check whether the associated symbol is a constant.
9997
9998 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9999 possible that a buggy compiler could mark a variable as
10000 constant even when it is not, and TYPE_CONST would return
10001 true in this case, while SYMBOL_CLASS wouldn't.
10002
10003 We also have to check for function symbols because they
10004 are always constant. */
10005 {
10006 struct symbol *s = exp->elts[i + 2].symbol;
10007
10008 if (SYMBOL_CLASS (s) != LOC_BLOCK
10009 && SYMBOL_CLASS (s) != LOC_CONST
10010 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10011 return 0;
10012 break;
10013 }
10014
10015 /* The default action is to return 0 because we are using
10016 the optimistic approach here: If we don't know something,
10017 then it is not a constant. */
10018 default:
10019 return 0;
10020 }
10021 }
10022
10023 return 1;
10024 }
10025
10026 /* Watchpoint destructor. */
10027
10028 watchpoint::~watchpoint ()
10029 {
10030 xfree (this->exp_string);
10031 xfree (this->exp_string_reparse);
10032 }
10033
10034 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10035
10036 static void
10037 re_set_watchpoint (struct breakpoint *b)
10038 {
10039 struct watchpoint *w = (struct watchpoint *) b;
10040
10041 /* Watchpoint can be either on expression using entirely global
10042 variables, or it can be on local variables.
10043
10044 Watchpoints of the first kind are never auto-deleted, and even
10045 persist across program restarts. Since they can use variables
10046 from shared libraries, we need to reparse expression as libraries
10047 are loaded and unloaded.
10048
10049 Watchpoints on local variables can also change meaning as result
10050 of solib event. For example, if a watchpoint uses both a local
10051 and a global variables in expression, it's a local watchpoint,
10052 but unloading of a shared library will make the expression
10053 invalid. This is not a very common use case, but we still
10054 re-evaluate expression, to avoid surprises to the user.
10055
10056 Note that for local watchpoints, we re-evaluate it only if
10057 watchpoints frame id is still valid. If it's not, it means the
10058 watchpoint is out of scope and will be deleted soon. In fact,
10059 I'm not sure we'll ever be called in this case.
10060
10061 If a local watchpoint's frame id is still valid, then
10062 w->exp_valid_block is likewise valid, and we can safely use it.
10063
10064 Don't do anything about disabled watchpoints, since they will be
10065 reevaluated again when enabled. */
10066 update_watchpoint (w, 1 /* reparse */);
10067 }
10068
10069 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10070
10071 static int
10072 insert_watchpoint (struct bp_location *bl)
10073 {
10074 struct watchpoint *w = (struct watchpoint *) bl->owner;
10075 int length = w->exact ? 1 : bl->length;
10076
10077 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10078 w->cond_exp.get ());
10079 }
10080
10081 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10082
10083 static int
10084 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10085 {
10086 struct watchpoint *w = (struct watchpoint *) bl->owner;
10087 int length = w->exact ? 1 : bl->length;
10088
10089 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10090 w->cond_exp.get ());
10091 }
10092
10093 static int
10094 breakpoint_hit_watchpoint (const struct bp_location *bl,
10095 const address_space *aspace, CORE_ADDR bp_addr,
10096 const struct target_waitstatus *ws)
10097 {
10098 struct breakpoint *b = bl->owner;
10099 struct watchpoint *w = (struct watchpoint *) b;
10100
10101 /* Continuable hardware watchpoints are treated as non-existent if the
10102 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10103 some data address). Otherwise gdb won't stop on a break instruction
10104 in the code (not from a breakpoint) when a hardware watchpoint has
10105 been defined. Also skip watchpoints which we know did not trigger
10106 (did not match the data address). */
10107 if (is_hardware_watchpoint (b)
10108 && w->watchpoint_triggered == watch_triggered_no)
10109 return 0;
10110
10111 return 1;
10112 }
10113
10114 static void
10115 check_status_watchpoint (bpstat bs)
10116 {
10117 gdb_assert (is_watchpoint (bs->breakpoint_at));
10118
10119 bpstat_check_watchpoint (bs);
10120 }
10121
10122 /* Implement the "resources_needed" breakpoint_ops method for
10123 hardware watchpoints. */
10124
10125 static int
10126 resources_needed_watchpoint (const struct bp_location *bl)
10127 {
10128 struct watchpoint *w = (struct watchpoint *) bl->owner;
10129 int length = w->exact? 1 : bl->length;
10130
10131 return target_region_ok_for_hw_watchpoint (bl->address, length);
10132 }
10133
10134 /* Implement the "works_in_software_mode" breakpoint_ops method for
10135 hardware watchpoints. */
10136
10137 static int
10138 works_in_software_mode_watchpoint (const struct breakpoint *b)
10139 {
10140 /* Read and access watchpoints only work with hardware support. */
10141 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10142 }
10143
10144 static enum print_stop_action
10145 print_it_watchpoint (bpstat bs)
10146 {
10147 struct breakpoint *b;
10148 enum print_stop_action result;
10149 struct watchpoint *w;
10150 struct ui_out *uiout = current_uiout;
10151
10152 gdb_assert (bs->bp_location_at != NULL);
10153
10154 b = bs->breakpoint_at;
10155 w = (struct watchpoint *) b;
10156
10157 annotate_watchpoint (b->number);
10158 maybe_print_thread_hit_breakpoint (uiout);
10159
10160 string_file stb;
10161
10162 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10163 switch (b->type)
10164 {
10165 case bp_watchpoint:
10166 case bp_hardware_watchpoint:
10167 if (uiout->is_mi_like_p ())
10168 uiout->field_string
10169 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10170 mention (b);
10171 tuple_emitter.emplace (uiout, "value");
10172 uiout->text ("\nOld value = ");
10173 watchpoint_value_print (bs->old_val.get (), &stb);
10174 uiout->field_stream ("old", stb);
10175 uiout->text ("\nNew value = ");
10176 watchpoint_value_print (w->val.get (), &stb);
10177 uiout->field_stream ("new", stb);
10178 uiout->text ("\n");
10179 /* More than one watchpoint may have been triggered. */
10180 result = PRINT_UNKNOWN;
10181 break;
10182
10183 case bp_read_watchpoint:
10184 if (uiout->is_mi_like_p ())
10185 uiout->field_string
10186 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10187 mention (b);
10188 tuple_emitter.emplace (uiout, "value");
10189 uiout->text ("\nValue = ");
10190 watchpoint_value_print (w->val.get (), &stb);
10191 uiout->field_stream ("value", stb);
10192 uiout->text ("\n");
10193 result = PRINT_UNKNOWN;
10194 break;
10195
10196 case bp_access_watchpoint:
10197 if (bs->old_val != NULL)
10198 {
10199 if (uiout->is_mi_like_p ())
10200 uiout->field_string
10201 ("reason",
10202 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10203 mention (b);
10204 tuple_emitter.emplace (uiout, "value");
10205 uiout->text ("\nOld value = ");
10206 watchpoint_value_print (bs->old_val.get (), &stb);
10207 uiout->field_stream ("old", stb);
10208 uiout->text ("\nNew value = ");
10209 }
10210 else
10211 {
10212 mention (b);
10213 if (uiout->is_mi_like_p ())
10214 uiout->field_string
10215 ("reason",
10216 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10217 tuple_emitter.emplace (uiout, "value");
10218 uiout->text ("\nValue = ");
10219 }
10220 watchpoint_value_print (w->val.get (), &stb);
10221 uiout->field_stream ("new", stb);
10222 uiout->text ("\n");
10223 result = PRINT_UNKNOWN;
10224 break;
10225 default:
10226 result = PRINT_UNKNOWN;
10227 }
10228
10229 return result;
10230 }
10231
10232 /* Implement the "print_mention" breakpoint_ops method for hardware
10233 watchpoints. */
10234
10235 static void
10236 print_mention_watchpoint (struct breakpoint *b)
10237 {
10238 struct watchpoint *w = (struct watchpoint *) b;
10239 struct ui_out *uiout = current_uiout;
10240 const char *tuple_name;
10241
10242 switch (b->type)
10243 {
10244 case bp_watchpoint:
10245 uiout->text ("Watchpoint ");
10246 tuple_name = "wpt";
10247 break;
10248 case bp_hardware_watchpoint:
10249 uiout->text ("Hardware watchpoint ");
10250 tuple_name = "wpt";
10251 break;
10252 case bp_read_watchpoint:
10253 uiout->text ("Hardware read watchpoint ");
10254 tuple_name = "hw-rwpt";
10255 break;
10256 case bp_access_watchpoint:
10257 uiout->text ("Hardware access (read/write) watchpoint ");
10258 tuple_name = "hw-awpt";
10259 break;
10260 default:
10261 internal_error (__FILE__, __LINE__,
10262 _("Invalid hardware watchpoint type."));
10263 }
10264
10265 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10266 uiout->field_int ("number", b->number);
10267 uiout->text (": ");
10268 uiout->field_string ("exp", w->exp_string);
10269 }
10270
10271 /* Implement the "print_recreate" breakpoint_ops method for
10272 watchpoints. */
10273
10274 static void
10275 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10276 {
10277 struct watchpoint *w = (struct watchpoint *) b;
10278
10279 switch (b->type)
10280 {
10281 case bp_watchpoint:
10282 case bp_hardware_watchpoint:
10283 fprintf_unfiltered (fp, "watch");
10284 break;
10285 case bp_read_watchpoint:
10286 fprintf_unfiltered (fp, "rwatch");
10287 break;
10288 case bp_access_watchpoint:
10289 fprintf_unfiltered (fp, "awatch");
10290 break;
10291 default:
10292 internal_error (__FILE__, __LINE__,
10293 _("Invalid watchpoint type."));
10294 }
10295
10296 fprintf_unfiltered (fp, " %s", w->exp_string);
10297 print_recreate_thread (b, fp);
10298 }
10299
10300 /* Implement the "explains_signal" breakpoint_ops method for
10301 watchpoints. */
10302
10303 static int
10304 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10305 {
10306 /* A software watchpoint cannot cause a signal other than
10307 GDB_SIGNAL_TRAP. */
10308 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10309 return 0;
10310
10311 return 1;
10312 }
10313
10314 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10315
10316 static struct breakpoint_ops watchpoint_breakpoint_ops;
10317
10318 /* Implement the "insert" breakpoint_ops method for
10319 masked hardware watchpoints. */
10320
10321 static int
10322 insert_masked_watchpoint (struct bp_location *bl)
10323 {
10324 struct watchpoint *w = (struct watchpoint *) bl->owner;
10325
10326 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10327 bl->watchpoint_type);
10328 }
10329
10330 /* Implement the "remove" breakpoint_ops method for
10331 masked hardware watchpoints. */
10332
10333 static int
10334 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10335 {
10336 struct watchpoint *w = (struct watchpoint *) bl->owner;
10337
10338 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10339 bl->watchpoint_type);
10340 }
10341
10342 /* Implement the "resources_needed" breakpoint_ops method for
10343 masked hardware watchpoints. */
10344
10345 static int
10346 resources_needed_masked_watchpoint (const struct bp_location *bl)
10347 {
10348 struct watchpoint *w = (struct watchpoint *) bl->owner;
10349
10350 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10351 }
10352
10353 /* Implement the "works_in_software_mode" breakpoint_ops method for
10354 masked hardware watchpoints. */
10355
10356 static int
10357 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10358 {
10359 return 0;
10360 }
10361
10362 /* Implement the "print_it" breakpoint_ops method for
10363 masked hardware watchpoints. */
10364
10365 static enum print_stop_action
10366 print_it_masked_watchpoint (bpstat bs)
10367 {
10368 struct breakpoint *b = bs->breakpoint_at;
10369 struct ui_out *uiout = current_uiout;
10370
10371 /* Masked watchpoints have only one location. */
10372 gdb_assert (b->loc && b->loc->next == NULL);
10373
10374 annotate_watchpoint (b->number);
10375 maybe_print_thread_hit_breakpoint (uiout);
10376
10377 switch (b->type)
10378 {
10379 case bp_hardware_watchpoint:
10380 if (uiout->is_mi_like_p ())
10381 uiout->field_string
10382 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10383 break;
10384
10385 case bp_read_watchpoint:
10386 if (uiout->is_mi_like_p ())
10387 uiout->field_string
10388 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10389 break;
10390
10391 case bp_access_watchpoint:
10392 if (uiout->is_mi_like_p ())
10393 uiout->field_string
10394 ("reason",
10395 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10396 break;
10397 default:
10398 internal_error (__FILE__, __LINE__,
10399 _("Invalid hardware watchpoint type."));
10400 }
10401
10402 mention (b);
10403 uiout->text (_("\n\
10404 Check the underlying instruction at PC for the memory\n\
10405 address and value which triggered this watchpoint.\n"));
10406 uiout->text ("\n");
10407
10408 /* More than one watchpoint may have been triggered. */
10409 return PRINT_UNKNOWN;
10410 }
10411
10412 /* Implement the "print_one_detail" breakpoint_ops method for
10413 masked hardware watchpoints. */
10414
10415 static void
10416 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10417 struct ui_out *uiout)
10418 {
10419 struct watchpoint *w = (struct watchpoint *) b;
10420
10421 /* Masked watchpoints have only one location. */
10422 gdb_assert (b->loc && b->loc->next == NULL);
10423
10424 uiout->text ("\tmask ");
10425 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10426 uiout->text ("\n");
10427 }
10428
10429 /* Implement the "print_mention" breakpoint_ops method for
10430 masked hardware watchpoints. */
10431
10432 static void
10433 print_mention_masked_watchpoint (struct breakpoint *b)
10434 {
10435 struct watchpoint *w = (struct watchpoint *) b;
10436 struct ui_out *uiout = current_uiout;
10437 const char *tuple_name;
10438
10439 switch (b->type)
10440 {
10441 case bp_hardware_watchpoint:
10442 uiout->text ("Masked hardware watchpoint ");
10443 tuple_name = "wpt";
10444 break;
10445 case bp_read_watchpoint:
10446 uiout->text ("Masked hardware read watchpoint ");
10447 tuple_name = "hw-rwpt";
10448 break;
10449 case bp_access_watchpoint:
10450 uiout->text ("Masked hardware access (read/write) watchpoint ");
10451 tuple_name = "hw-awpt";
10452 break;
10453 default:
10454 internal_error (__FILE__, __LINE__,
10455 _("Invalid hardware watchpoint type."));
10456 }
10457
10458 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10459 uiout->field_int ("number", b->number);
10460 uiout->text (": ");
10461 uiout->field_string ("exp", w->exp_string);
10462 }
10463
10464 /* Implement the "print_recreate" breakpoint_ops method for
10465 masked hardware watchpoints. */
10466
10467 static void
10468 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10469 {
10470 struct watchpoint *w = (struct watchpoint *) b;
10471 char tmp[40];
10472
10473 switch (b->type)
10474 {
10475 case bp_hardware_watchpoint:
10476 fprintf_unfiltered (fp, "watch");
10477 break;
10478 case bp_read_watchpoint:
10479 fprintf_unfiltered (fp, "rwatch");
10480 break;
10481 case bp_access_watchpoint:
10482 fprintf_unfiltered (fp, "awatch");
10483 break;
10484 default:
10485 internal_error (__FILE__, __LINE__,
10486 _("Invalid hardware watchpoint type."));
10487 }
10488
10489 sprintf_vma (tmp, w->hw_wp_mask);
10490 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10491 print_recreate_thread (b, fp);
10492 }
10493
10494 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10495
10496 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10497
10498 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10499
10500 static int
10501 is_masked_watchpoint (const struct breakpoint *b)
10502 {
10503 return b->ops == &masked_watchpoint_breakpoint_ops;
10504 }
10505
10506 /* accessflag: hw_write: watch write,
10507 hw_read: watch read,
10508 hw_access: watch access (read or write) */
10509 static void
10510 watch_command_1 (const char *arg, int accessflag, int from_tty,
10511 int just_location, int internal)
10512 {
10513 struct breakpoint *scope_breakpoint = NULL;
10514 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10515 struct value *mark, *result;
10516 int saved_bitpos = 0, saved_bitsize = 0;
10517 const char *exp_start = NULL;
10518 const char *exp_end = NULL;
10519 const char *tok, *end_tok;
10520 int toklen = -1;
10521 const char *cond_start = NULL;
10522 const char *cond_end = NULL;
10523 enum bptype bp_type;
10524 int thread = -1;
10525 int pc = 0;
10526 /* Flag to indicate whether we are going to use masks for
10527 the hardware watchpoint. */
10528 int use_mask = 0;
10529 CORE_ADDR mask = 0;
10530
10531 /* Make sure that we actually have parameters to parse. */
10532 if (arg != NULL && arg[0] != '\0')
10533 {
10534 const char *value_start;
10535
10536 exp_end = arg + strlen (arg);
10537
10538 /* Look for "parameter value" pairs at the end
10539 of the arguments string. */
10540 for (tok = exp_end - 1; tok > arg; tok--)
10541 {
10542 /* Skip whitespace at the end of the argument list. */
10543 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10544 tok--;
10545
10546 /* Find the beginning of the last token.
10547 This is the value of the parameter. */
10548 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10549 tok--;
10550 value_start = tok + 1;
10551
10552 /* Skip whitespace. */
10553 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10554 tok--;
10555
10556 end_tok = tok;
10557
10558 /* Find the beginning of the second to last token.
10559 This is the parameter itself. */
10560 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10561 tok--;
10562 tok++;
10563 toklen = end_tok - tok + 1;
10564
10565 if (toklen == 6 && startswith (tok, "thread"))
10566 {
10567 struct thread_info *thr;
10568 /* At this point we've found a "thread" token, which means
10569 the user is trying to set a watchpoint that triggers
10570 only in a specific thread. */
10571 const char *endp;
10572
10573 if (thread != -1)
10574 error(_("You can specify only one thread."));
10575
10576 /* Extract the thread ID from the next token. */
10577 thr = parse_thread_id (value_start, &endp);
10578
10579 /* Check if the user provided a valid thread ID. */
10580 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10581 invalid_thread_id_error (value_start);
10582
10583 thread = thr->global_num;
10584 }
10585 else if (toklen == 4 && startswith (tok, "mask"))
10586 {
10587 /* We've found a "mask" token, which means the user wants to
10588 create a hardware watchpoint that is going to have the mask
10589 facility. */
10590 struct value *mask_value, *mark;
10591
10592 if (use_mask)
10593 error(_("You can specify only one mask."));
10594
10595 use_mask = just_location = 1;
10596
10597 mark = value_mark ();
10598 mask_value = parse_to_comma_and_eval (&value_start);
10599 mask = value_as_address (mask_value);
10600 value_free_to_mark (mark);
10601 }
10602 else
10603 /* We didn't recognize what we found. We should stop here. */
10604 break;
10605
10606 /* Truncate the string and get rid of the "parameter value" pair before
10607 the arguments string is parsed by the parse_exp_1 function. */
10608 exp_end = tok;
10609 }
10610 }
10611 else
10612 exp_end = arg;
10613
10614 /* Parse the rest of the arguments. From here on out, everything
10615 is in terms of a newly allocated string instead of the original
10616 ARG. */
10617 innermost_block.reset ();
10618 std::string expression (arg, exp_end - arg);
10619 exp_start = arg = expression.c_str ();
10620 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
10621 exp_end = arg;
10622 /* Remove trailing whitespace from the expression before saving it.
10623 This makes the eventual display of the expression string a bit
10624 prettier. */
10625 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10626 --exp_end;
10627
10628 /* Checking if the expression is not constant. */
10629 if (watchpoint_exp_is_const (exp.get ()))
10630 {
10631 int len;
10632
10633 len = exp_end - exp_start;
10634 while (len > 0 && isspace (exp_start[len - 1]))
10635 len--;
10636 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10637 }
10638
10639 exp_valid_block = innermost_block.block ();
10640 mark = value_mark ();
10641 struct value *val_as_value = nullptr;
10642 fetch_subexp_value (exp.get (), &pc, &val_as_value, &result, NULL,
10643 just_location);
10644
10645 if (val_as_value != NULL && just_location)
10646 {
10647 saved_bitpos = value_bitpos (val_as_value);
10648 saved_bitsize = value_bitsize (val_as_value);
10649 }
10650
10651 value_ref_ptr val;
10652 if (just_location)
10653 {
10654 int ret;
10655
10656 exp_valid_block = NULL;
10657 val = release_value (value_addr (result));
10658 value_free_to_mark (mark);
10659
10660 if (use_mask)
10661 {
10662 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10663 mask);
10664 if (ret == -1)
10665 error (_("This target does not support masked watchpoints."));
10666 else if (ret == -2)
10667 error (_("Invalid mask or memory region."));
10668 }
10669 }
10670 else if (val_as_value != NULL)
10671 val = release_value (val_as_value);
10672
10673 tok = skip_spaces (arg);
10674 end_tok = skip_to_space (tok);
10675
10676 toklen = end_tok - tok;
10677 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10678 {
10679 innermost_block.reset ();
10680 tok = cond_start = end_tok + 1;
10681 parse_exp_1 (&tok, 0, 0, 0);
10682
10683 /* The watchpoint expression may not be local, but the condition
10684 may still be. E.g.: `watch global if local > 0'. */
10685 cond_exp_valid_block = innermost_block.block ();
10686
10687 cond_end = tok;
10688 }
10689 if (*tok)
10690 error (_("Junk at end of command."));
10691
10692 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10693
10694 /* Save this because create_internal_breakpoint below invalidates
10695 'wp_frame'. */
10696 frame_id watchpoint_frame = get_frame_id (wp_frame);
10697
10698 /* If the expression is "local", then set up a "watchpoint scope"
10699 breakpoint at the point where we've left the scope of the watchpoint
10700 expression. Create the scope breakpoint before the watchpoint, so
10701 that we will encounter it first in bpstat_stop_status. */
10702 if (exp_valid_block != NULL && wp_frame != NULL)
10703 {
10704 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10705
10706 if (frame_id_p (caller_frame_id))
10707 {
10708 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10709 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10710
10711 scope_breakpoint
10712 = create_internal_breakpoint (caller_arch, caller_pc,
10713 bp_watchpoint_scope,
10714 &momentary_breakpoint_ops);
10715
10716 /* create_internal_breakpoint could invalidate WP_FRAME. */
10717 wp_frame = NULL;
10718
10719 scope_breakpoint->enable_state = bp_enabled;
10720
10721 /* Automatically delete the breakpoint when it hits. */
10722 scope_breakpoint->disposition = disp_del;
10723
10724 /* Only break in the proper frame (help with recursion). */
10725 scope_breakpoint->frame_id = caller_frame_id;
10726
10727 /* Set the address at which we will stop. */
10728 scope_breakpoint->loc->gdbarch = caller_arch;
10729 scope_breakpoint->loc->requested_address = caller_pc;
10730 scope_breakpoint->loc->address
10731 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10732 scope_breakpoint->loc->requested_address,
10733 scope_breakpoint->type);
10734 }
10735 }
10736
10737 /* Now set up the breakpoint. We create all watchpoints as hardware
10738 watchpoints here even if hardware watchpoints are turned off, a call
10739 to update_watchpoint later in this function will cause the type to
10740 drop back to bp_watchpoint (software watchpoint) if required. */
10741
10742 if (accessflag == hw_read)
10743 bp_type = bp_read_watchpoint;
10744 else if (accessflag == hw_access)
10745 bp_type = bp_access_watchpoint;
10746 else
10747 bp_type = bp_hardware_watchpoint;
10748
10749 std::unique_ptr<watchpoint> w (new watchpoint ());
10750
10751 if (use_mask)
10752 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10753 &masked_watchpoint_breakpoint_ops);
10754 else
10755 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
10756 &watchpoint_breakpoint_ops);
10757 w->thread = thread;
10758 w->disposition = disp_donttouch;
10759 w->pspace = current_program_space;
10760 w->exp = std::move (exp);
10761 w->exp_valid_block = exp_valid_block;
10762 w->cond_exp_valid_block = cond_exp_valid_block;
10763 if (just_location)
10764 {
10765 struct type *t = value_type (val.get ());
10766 CORE_ADDR addr = value_as_address (val.get ());
10767
10768 w->exp_string_reparse
10769 = current_language->la_watch_location_expression (t, addr).release ();
10770
10771 w->exp_string = xstrprintf ("-location %.*s",
10772 (int) (exp_end - exp_start), exp_start);
10773 }
10774 else
10775 w->exp_string = savestring (exp_start, exp_end - exp_start);
10776
10777 if (use_mask)
10778 {
10779 w->hw_wp_mask = mask;
10780 }
10781 else
10782 {
10783 w->val = val;
10784 w->val_bitpos = saved_bitpos;
10785 w->val_bitsize = saved_bitsize;
10786 w->val_valid = 1;
10787 }
10788
10789 if (cond_start)
10790 w->cond_string = savestring (cond_start, cond_end - cond_start);
10791 else
10792 w->cond_string = 0;
10793
10794 if (frame_id_p (watchpoint_frame))
10795 {
10796 w->watchpoint_frame = watchpoint_frame;
10797 w->watchpoint_thread = inferior_ptid;
10798 }
10799 else
10800 {
10801 w->watchpoint_frame = null_frame_id;
10802 w->watchpoint_thread = null_ptid;
10803 }
10804
10805 if (scope_breakpoint != NULL)
10806 {
10807 /* The scope breakpoint is related to the watchpoint. We will
10808 need to act on them together. */
10809 w->related_breakpoint = scope_breakpoint;
10810 scope_breakpoint->related_breakpoint = w.get ();
10811 }
10812
10813 if (!just_location)
10814 value_free_to_mark (mark);
10815
10816 /* Finally update the new watchpoint. This creates the locations
10817 that should be inserted. */
10818 update_watchpoint (w.get (), 1);
10819
10820 install_breakpoint (internal, std::move (w), 1);
10821 }
10822
10823 /* Return count of debug registers needed to watch the given expression.
10824 If the watchpoint cannot be handled in hardware return zero. */
10825
10826 static int
10827 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10828 {
10829 int found_memory_cnt = 0;
10830
10831 /* Did the user specifically forbid us to use hardware watchpoints? */
10832 if (!can_use_hw_watchpoints)
10833 return 0;
10834
10835 gdb_assert (!vals.empty ());
10836 struct value *head = vals[0].get ();
10837
10838 /* Make sure that the value of the expression depends only upon
10839 memory contents, and values computed from them within GDB. If we
10840 find any register references or function calls, we can't use a
10841 hardware watchpoint.
10842
10843 The idea here is that evaluating an expression generates a series
10844 of values, one holding the value of every subexpression. (The
10845 expression a*b+c has five subexpressions: a, b, a*b, c, and
10846 a*b+c.) GDB's values hold almost enough information to establish
10847 the criteria given above --- they identify memory lvalues,
10848 register lvalues, computed values, etcetera. So we can evaluate
10849 the expression, and then scan the chain of values that leaves
10850 behind to decide whether we can detect any possible change to the
10851 expression's final value using only hardware watchpoints.
10852
10853 However, I don't think that the values returned by inferior
10854 function calls are special in any way. So this function may not
10855 notice that an expression involving an inferior function call
10856 can't be watched with hardware watchpoints. FIXME. */
10857 for (const value_ref_ptr &iter : vals)
10858 {
10859 struct value *v = iter.get ();
10860
10861 if (VALUE_LVAL (v) == lval_memory)
10862 {
10863 if (v != head && value_lazy (v))
10864 /* A lazy memory lvalue in the chain is one that GDB never
10865 needed to fetch; we either just used its address (e.g.,
10866 `a' in `a.b') or we never needed it at all (e.g., `a'
10867 in `a,b'). This doesn't apply to HEAD; if that is
10868 lazy then it was not readable, but watch it anyway. */
10869 ;
10870 else
10871 {
10872 /* Ahh, memory we actually used! Check if we can cover
10873 it with hardware watchpoints. */
10874 struct type *vtype = check_typedef (value_type (v));
10875
10876 /* We only watch structs and arrays if user asked for it
10877 explicitly, never if they just happen to appear in a
10878 middle of some value chain. */
10879 if (v == head
10880 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10881 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10882 {
10883 CORE_ADDR vaddr = value_address (v);
10884 int len;
10885 int num_regs;
10886
10887 len = (target_exact_watchpoints
10888 && is_scalar_type_recursive (vtype))?
10889 1 : TYPE_LENGTH (value_type (v));
10890
10891 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10892 if (!num_regs)
10893 return 0;
10894 else
10895 found_memory_cnt += num_regs;
10896 }
10897 }
10898 }
10899 else if (VALUE_LVAL (v) != not_lval
10900 && deprecated_value_modifiable (v) == 0)
10901 return 0; /* These are values from the history (e.g., $1). */
10902 else if (VALUE_LVAL (v) == lval_register)
10903 return 0; /* Cannot watch a register with a HW watchpoint. */
10904 }
10905
10906 /* The expression itself looks suitable for using a hardware
10907 watchpoint, but give the target machine a chance to reject it. */
10908 return found_memory_cnt;
10909 }
10910
10911 void
10912 watch_command_wrapper (const char *arg, int from_tty, int internal)
10913 {
10914 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10915 }
10916
10917 /* A helper function that looks for the "-location" argument and then
10918 calls watch_command_1. */
10919
10920 static void
10921 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10922 {
10923 int just_location = 0;
10924
10925 if (arg
10926 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10927 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10928 {
10929 arg = skip_spaces (arg);
10930 just_location = 1;
10931 }
10932
10933 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10934 }
10935
10936 static void
10937 watch_command (const char *arg, int from_tty)
10938 {
10939 watch_maybe_just_location (arg, hw_write, from_tty);
10940 }
10941
10942 void
10943 rwatch_command_wrapper (const char *arg, int from_tty, int internal)
10944 {
10945 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10946 }
10947
10948 static void
10949 rwatch_command (const char *arg, int from_tty)
10950 {
10951 watch_maybe_just_location (arg, hw_read, from_tty);
10952 }
10953
10954 void
10955 awatch_command_wrapper (const char *arg, int from_tty, int internal)
10956 {
10957 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10958 }
10959
10960 static void
10961 awatch_command (const char *arg, int from_tty)
10962 {
10963 watch_maybe_just_location (arg, hw_access, from_tty);
10964 }
10965 \f
10966
10967 /* Data for the FSM that manages the until(location)/advance commands
10968 in infcmd.c. Here because it uses the mechanisms of
10969 breakpoints. */
10970
10971 struct until_break_fsm
10972 {
10973 /* The base class. */
10974 struct thread_fsm thread_fsm;
10975
10976 /* The thread that as current when the command was executed. */
10977 int thread;
10978
10979 /* The breakpoint set at the destination location. */
10980 struct breakpoint *location_breakpoint;
10981
10982 /* Breakpoint set at the return address in the caller frame. May be
10983 NULL. */
10984 struct breakpoint *caller_breakpoint;
10985 };
10986
10987 static void until_break_fsm_clean_up (struct thread_fsm *self,
10988 struct thread_info *thread);
10989 static int until_break_fsm_should_stop (struct thread_fsm *self,
10990 struct thread_info *thread);
10991 static enum async_reply_reason
10992 until_break_fsm_async_reply_reason (struct thread_fsm *self);
10993
10994 /* until_break_fsm's vtable. */
10995
10996 static struct thread_fsm_ops until_break_fsm_ops =
10997 {
10998 NULL, /* dtor */
10999 until_break_fsm_clean_up,
11000 until_break_fsm_should_stop,
11001 NULL, /* return_value */
11002 until_break_fsm_async_reply_reason,
11003 };
11004
11005 /* Allocate a new until_break_command_fsm. */
11006
11007 static struct until_break_fsm *
11008 new_until_break_fsm (struct interp *cmd_interp, int thread,
11009 breakpoint_up &&location_breakpoint,
11010 breakpoint_up &&caller_breakpoint)
11011 {
11012 struct until_break_fsm *sm;
11013
11014 sm = XCNEW (struct until_break_fsm);
11015 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11016
11017 sm->thread = thread;
11018 sm->location_breakpoint = location_breakpoint.release ();
11019 sm->caller_breakpoint = caller_breakpoint.release ();
11020
11021 return sm;
11022 }
11023
11024 /* Implementation of the 'should_stop' FSM method for the
11025 until(location)/advance commands. */
11026
11027 static int
11028 until_break_fsm_should_stop (struct thread_fsm *self,
11029 struct thread_info *tp)
11030 {
11031 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11032
11033 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11034 sm->location_breakpoint) != NULL
11035 || (sm->caller_breakpoint != NULL
11036 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11037 sm->caller_breakpoint) != NULL))
11038 thread_fsm_set_finished (self);
11039
11040 return 1;
11041 }
11042
11043 /* Implementation of the 'clean_up' FSM method for the
11044 until(location)/advance commands. */
11045
11046 static void
11047 until_break_fsm_clean_up (struct thread_fsm *self,
11048 struct thread_info *thread)
11049 {
11050 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11051
11052 /* Clean up our temporary breakpoints. */
11053 if (sm->location_breakpoint != NULL)
11054 {
11055 delete_breakpoint (sm->location_breakpoint);
11056 sm->location_breakpoint = NULL;
11057 }
11058 if (sm->caller_breakpoint != NULL)
11059 {
11060 delete_breakpoint (sm->caller_breakpoint);
11061 sm->caller_breakpoint = NULL;
11062 }
11063 delete_longjmp_breakpoint (sm->thread);
11064 }
11065
11066 /* Implementation of the 'async_reply_reason' FSM method for the
11067 until(location)/advance commands. */
11068
11069 static enum async_reply_reason
11070 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11071 {
11072 return EXEC_ASYNC_LOCATION_REACHED;
11073 }
11074
11075 void
11076 until_break_command (const char *arg, int from_tty, int anywhere)
11077 {
11078 struct frame_info *frame;
11079 struct gdbarch *frame_gdbarch;
11080 struct frame_id stack_frame_id;
11081 struct frame_id caller_frame_id;
11082 struct cleanup *old_chain;
11083 int thread;
11084 struct thread_info *tp;
11085 struct until_break_fsm *sm;
11086
11087 clear_proceed_status (0);
11088
11089 /* Set a breakpoint where the user wants it and at return from
11090 this function. */
11091
11092 event_location_up location = string_to_event_location (&arg, current_language);
11093
11094 std::vector<symtab_and_line> sals
11095 = (last_displayed_sal_is_valid ()
11096 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11097 get_last_displayed_symtab (),
11098 get_last_displayed_line ())
11099 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11100 NULL, (struct symtab *) NULL, 0));
11101
11102 if (sals.size () != 1)
11103 error (_("Couldn't get information on specified line."));
11104
11105 symtab_and_line &sal = sals[0];
11106
11107 if (*arg)
11108 error (_("Junk at end of arguments."));
11109
11110 resolve_sal_pc (&sal);
11111
11112 tp = inferior_thread ();
11113 thread = tp->global_num;
11114
11115 old_chain = make_cleanup (null_cleanup, NULL);
11116
11117 /* Note linespec handling above invalidates the frame chain.
11118 Installing a breakpoint also invalidates the frame chain (as it
11119 may need to switch threads), so do any frame handling before
11120 that. */
11121
11122 frame = get_selected_frame (NULL);
11123 frame_gdbarch = get_frame_arch (frame);
11124 stack_frame_id = get_stack_frame_id (frame);
11125 caller_frame_id = frame_unwind_caller_id (frame);
11126
11127 /* Keep within the current frame, or in frames called by the current
11128 one. */
11129
11130 breakpoint_up caller_breakpoint;
11131 if (frame_id_p (caller_frame_id))
11132 {
11133 struct symtab_and_line sal2;
11134 struct gdbarch *caller_gdbarch;
11135
11136 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11137 sal2.pc = frame_unwind_caller_pc (frame);
11138 caller_gdbarch = frame_unwind_caller_arch (frame);
11139 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11140 sal2,
11141 caller_frame_id,
11142 bp_until);
11143
11144 set_longjmp_breakpoint (tp, caller_frame_id);
11145 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11146 }
11147
11148 /* set_momentary_breakpoint could invalidate FRAME. */
11149 frame = NULL;
11150
11151 breakpoint_up location_breakpoint;
11152 if (anywhere)
11153 /* If the user told us to continue until a specified location,
11154 we don't specify a frame at which we need to stop. */
11155 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11156 null_frame_id, bp_until);
11157 else
11158 /* Otherwise, specify the selected frame, because we want to stop
11159 only at the very same frame. */
11160 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11161 stack_frame_id, bp_until);
11162
11163 sm = new_until_break_fsm (command_interp (), tp->global_num,
11164 std::move (location_breakpoint),
11165 std::move (caller_breakpoint));
11166 tp->thread_fsm = &sm->thread_fsm;
11167
11168 discard_cleanups (old_chain);
11169
11170 proceed (-1, GDB_SIGNAL_DEFAULT);
11171 }
11172
11173 /* This function attempts to parse an optional "if <cond>" clause
11174 from the arg string. If one is not found, it returns NULL.
11175
11176 Else, it returns a pointer to the condition string. (It does not
11177 attempt to evaluate the string against a particular block.) And,
11178 it updates arg to point to the first character following the parsed
11179 if clause in the arg string. */
11180
11181 const char *
11182 ep_parse_optional_if_clause (const char **arg)
11183 {
11184 const char *cond_string;
11185
11186 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11187 return NULL;
11188
11189 /* Skip the "if" keyword. */
11190 (*arg) += 2;
11191
11192 /* Skip any extra leading whitespace, and record the start of the
11193 condition string. */
11194 *arg = skip_spaces (*arg);
11195 cond_string = *arg;
11196
11197 /* Assume that the condition occupies the remainder of the arg
11198 string. */
11199 (*arg) += strlen (cond_string);
11200
11201 return cond_string;
11202 }
11203
11204 /* Commands to deal with catching events, such as signals, exceptions,
11205 process start/exit, etc. */
11206
11207 typedef enum
11208 {
11209 catch_fork_temporary, catch_vfork_temporary,
11210 catch_fork_permanent, catch_vfork_permanent
11211 }
11212 catch_fork_kind;
11213
11214 static void
11215 catch_fork_command_1 (const char *arg, int from_tty,
11216 struct cmd_list_element *command)
11217 {
11218 struct gdbarch *gdbarch = get_current_arch ();
11219 const char *cond_string = NULL;
11220 catch_fork_kind fork_kind;
11221 int tempflag;
11222
11223 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11224 tempflag = (fork_kind == catch_fork_temporary
11225 || fork_kind == catch_vfork_temporary);
11226
11227 if (!arg)
11228 arg = "";
11229 arg = skip_spaces (arg);
11230
11231 /* The allowed syntax is:
11232 catch [v]fork
11233 catch [v]fork if <cond>
11234
11235 First, check if there's an if clause. */
11236 cond_string = ep_parse_optional_if_clause (&arg);
11237
11238 if ((*arg != '\0') && !isspace (*arg))
11239 error (_("Junk at end of arguments."));
11240
11241 /* If this target supports it, create a fork or vfork catchpoint
11242 and enable reporting of such events. */
11243 switch (fork_kind)
11244 {
11245 case catch_fork_temporary:
11246 case catch_fork_permanent:
11247 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11248 &catch_fork_breakpoint_ops);
11249 break;
11250 case catch_vfork_temporary:
11251 case catch_vfork_permanent:
11252 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11253 &catch_vfork_breakpoint_ops);
11254 break;
11255 default:
11256 error (_("unsupported or unknown fork kind; cannot catch it"));
11257 break;
11258 }
11259 }
11260
11261 static void
11262 catch_exec_command_1 (const char *arg, int from_tty,
11263 struct cmd_list_element *command)
11264 {
11265 struct gdbarch *gdbarch = get_current_arch ();
11266 int tempflag;
11267 const char *cond_string = NULL;
11268
11269 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11270
11271 if (!arg)
11272 arg = "";
11273 arg = skip_spaces (arg);
11274
11275 /* The allowed syntax is:
11276 catch exec
11277 catch exec if <cond>
11278
11279 First, check if there's an if clause. */
11280 cond_string = ep_parse_optional_if_clause (&arg);
11281
11282 if ((*arg != '\0') && !isspace (*arg))
11283 error (_("Junk at end of arguments."));
11284
11285 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11286 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11287 &catch_exec_breakpoint_ops);
11288 c->exec_pathname = NULL;
11289
11290 install_breakpoint (0, std::move (c), 1);
11291 }
11292
11293 void
11294 init_ada_exception_breakpoint (struct breakpoint *b,
11295 struct gdbarch *gdbarch,
11296 struct symtab_and_line sal,
11297 const char *addr_string,
11298 const struct breakpoint_ops *ops,
11299 int tempflag,
11300 int enabled,
11301 int from_tty)
11302 {
11303 if (from_tty)
11304 {
11305 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11306 if (!loc_gdbarch)
11307 loc_gdbarch = gdbarch;
11308
11309 describe_other_breakpoints (loc_gdbarch,
11310 sal.pspace, sal.pc, sal.section, -1);
11311 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11312 version for exception catchpoints, because two catchpoints
11313 used for different exception names will use the same address.
11314 In this case, a "breakpoint ... also set at..." warning is
11315 unproductive. Besides, the warning phrasing is also a bit
11316 inappropriate, we should use the word catchpoint, and tell
11317 the user what type of catchpoint it is. The above is good
11318 enough for now, though. */
11319 }
11320
11321 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11322
11323 b->enable_state = enabled ? bp_enabled : bp_disabled;
11324 b->disposition = tempflag ? disp_del : disp_donttouch;
11325 b->location = string_to_event_location (&addr_string,
11326 language_def (language_ada));
11327 b->language = language_ada;
11328 }
11329
11330 static void
11331 catch_command (const char *arg, int from_tty)
11332 {
11333 error (_("Catch requires an event name."));
11334 }
11335 \f
11336
11337 static void
11338 tcatch_command (const char *arg, int from_tty)
11339 {
11340 error (_("Catch requires an event name."));
11341 }
11342
11343 /* Compare two breakpoints and return a strcmp-like result. */
11344
11345 static int
11346 compare_breakpoints (const breakpoint *a, const breakpoint *b)
11347 {
11348 uintptr_t ua = (uintptr_t) a;
11349 uintptr_t ub = (uintptr_t) b;
11350
11351 if (a->number < b->number)
11352 return -1;
11353 else if (a->number > b->number)
11354 return 1;
11355
11356 /* Now sort by address, in case we see, e..g, two breakpoints with
11357 the number 0. */
11358 if (ua < ub)
11359 return -1;
11360 return ua > ub ? 1 : 0;
11361 }
11362
11363 /* Delete breakpoints by address or line. */
11364
11365 static void
11366 clear_command (const char *arg, int from_tty)
11367 {
11368 struct breakpoint *b;
11369 int default_match;
11370
11371 std::vector<symtab_and_line> decoded_sals;
11372 symtab_and_line last_sal;
11373 gdb::array_view<symtab_and_line> sals;
11374 if (arg)
11375 {
11376 decoded_sals
11377 = decode_line_with_current_source (arg,
11378 (DECODE_LINE_FUNFIRSTLINE
11379 | DECODE_LINE_LIST_MODE));
11380 default_match = 0;
11381 sals = decoded_sals;
11382 }
11383 else
11384 {
11385 /* Set sal's line, symtab, pc, and pspace to the values
11386 corresponding to the last call to print_frame_info. If the
11387 codepoint is not valid, this will set all the fields to 0. */
11388 last_sal = get_last_displayed_sal ();
11389 if (last_sal.symtab == 0)
11390 error (_("No source file specified."));
11391
11392 default_match = 1;
11393 sals = last_sal;
11394 }
11395
11396 /* We don't call resolve_sal_pc here. That's not as bad as it
11397 seems, because all existing breakpoints typically have both
11398 file/line and pc set. So, if clear is given file/line, we can
11399 match this to existing breakpoint without obtaining pc at all.
11400
11401 We only support clearing given the address explicitly
11402 present in breakpoint table. Say, we've set breakpoint
11403 at file:line. There were several PC values for that file:line,
11404 due to optimization, all in one block.
11405
11406 We've picked one PC value. If "clear" is issued with another
11407 PC corresponding to the same file:line, the breakpoint won't
11408 be cleared. We probably can still clear the breakpoint, but
11409 since the other PC value is never presented to user, user
11410 can only find it by guessing, and it does not seem important
11411 to support that. */
11412
11413 /* For each line spec given, delete bps which correspond to it. Do
11414 it in two passes, solely to preserve the current behavior that
11415 from_tty is forced true if we delete more than one
11416 breakpoint. */
11417
11418 std::vector<struct breakpoint *> found;
11419 for (const auto &sal : sals)
11420 {
11421 const char *sal_fullname;
11422
11423 /* If exact pc given, clear bpts at that pc.
11424 If line given (pc == 0), clear all bpts on specified line.
11425 If defaulting, clear all bpts on default line
11426 or at default pc.
11427
11428 defaulting sal.pc != 0 tests to do
11429
11430 0 1 pc
11431 1 1 pc _and_ line
11432 0 0 line
11433 1 0 <can't happen> */
11434
11435 sal_fullname = (sal.symtab == NULL
11436 ? NULL : symtab_to_fullname (sal.symtab));
11437
11438 /* Find all matching breakpoints and add them to 'found'. */
11439 ALL_BREAKPOINTS (b)
11440 {
11441 int match = 0;
11442 /* Are we going to delete b? */
11443 if (b->type != bp_none && !is_watchpoint (b))
11444 {
11445 struct bp_location *loc = b->loc;
11446 for (; loc; loc = loc->next)
11447 {
11448 /* If the user specified file:line, don't allow a PC
11449 match. This matches historical gdb behavior. */
11450 int pc_match = (!sal.explicit_line
11451 && sal.pc
11452 && (loc->pspace == sal.pspace)
11453 && (loc->address == sal.pc)
11454 && (!section_is_overlay (loc->section)
11455 || loc->section == sal.section));
11456 int line_match = 0;
11457
11458 if ((default_match || sal.explicit_line)
11459 && loc->symtab != NULL
11460 && sal_fullname != NULL
11461 && sal.pspace == loc->pspace
11462 && loc->line_number == sal.line
11463 && filename_cmp (symtab_to_fullname (loc->symtab),
11464 sal_fullname) == 0)
11465 line_match = 1;
11466
11467 if (pc_match || line_match)
11468 {
11469 match = 1;
11470 break;
11471 }
11472 }
11473 }
11474
11475 if (match)
11476 found.push_back (b);
11477 }
11478 }
11479
11480 /* Now go thru the 'found' chain and delete them. */
11481 if (found.empty ())
11482 {
11483 if (arg)
11484 error (_("No breakpoint at %s."), arg);
11485 else
11486 error (_("No breakpoint at this line."));
11487 }
11488
11489 /* Remove duplicates from the vec. */
11490 std::sort (found.begin (), found.end (),
11491 [] (const breakpoint *a, const breakpoint *b)
11492 {
11493 return compare_breakpoints (a, b) < 0;
11494 });
11495 found.erase (std::unique (found.begin (), found.end (),
11496 [] (const breakpoint *a, const breakpoint *b)
11497 {
11498 return compare_breakpoints (a, b) == 0;
11499 }),
11500 found.end ());
11501
11502 if (found.size () > 1)
11503 from_tty = 1; /* Always report if deleted more than one. */
11504 if (from_tty)
11505 {
11506 if (found.size () == 1)
11507 printf_unfiltered (_("Deleted breakpoint "));
11508 else
11509 printf_unfiltered (_("Deleted breakpoints "));
11510 }
11511
11512 for (breakpoint *iter : found)
11513 {
11514 if (from_tty)
11515 printf_unfiltered ("%d ", iter->number);
11516 delete_breakpoint (iter);
11517 }
11518 if (from_tty)
11519 putchar_unfiltered ('\n');
11520 }
11521 \f
11522 /* Delete breakpoint in BS if they are `delete' breakpoints and
11523 all breakpoints that are marked for deletion, whether hit or not.
11524 This is called after any breakpoint is hit, or after errors. */
11525
11526 void
11527 breakpoint_auto_delete (bpstat bs)
11528 {
11529 struct breakpoint *b, *b_tmp;
11530
11531 for (; bs; bs = bs->next)
11532 if (bs->breakpoint_at
11533 && bs->breakpoint_at->disposition == disp_del
11534 && bs->stop)
11535 delete_breakpoint (bs->breakpoint_at);
11536
11537 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11538 {
11539 if (b->disposition == disp_del_at_next_stop)
11540 delete_breakpoint (b);
11541 }
11542 }
11543
11544 /* A comparison function for bp_location AP and BP being interfaced to
11545 qsort. Sort elements primarily by their ADDRESS (no matter what
11546 does breakpoint_address_is_meaningful say for its OWNER),
11547 secondarily by ordering first permanent elements and
11548 terciarily just ensuring the array is sorted stable way despite
11549 qsort being an unstable algorithm. */
11550
11551 static int
11552 bp_locations_compare (const void *ap, const void *bp)
11553 {
11554 const struct bp_location *a = *(const struct bp_location **) ap;
11555 const struct bp_location *b = *(const struct bp_location **) bp;
11556
11557 if (a->address != b->address)
11558 return (a->address > b->address) - (a->address < b->address);
11559
11560 /* Sort locations at the same address by their pspace number, keeping
11561 locations of the same inferior (in a multi-inferior environment)
11562 grouped. */
11563
11564 if (a->pspace->num != b->pspace->num)
11565 return ((a->pspace->num > b->pspace->num)
11566 - (a->pspace->num < b->pspace->num));
11567
11568 /* Sort permanent breakpoints first. */
11569 if (a->permanent != b->permanent)
11570 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11571
11572 /* Make the internal GDB representation stable across GDB runs
11573 where A and B memory inside GDB can differ. Breakpoint locations of
11574 the same type at the same address can be sorted in arbitrary order. */
11575
11576 if (a->owner->number != b->owner->number)
11577 return ((a->owner->number > b->owner->number)
11578 - (a->owner->number < b->owner->number));
11579
11580 return (a > b) - (a < b);
11581 }
11582
11583 /* Set bp_locations_placed_address_before_address_max and
11584 bp_locations_shadow_len_after_address_max according to the current
11585 content of the bp_locations array. */
11586
11587 static void
11588 bp_locations_target_extensions_update (void)
11589 {
11590 struct bp_location *bl, **blp_tmp;
11591
11592 bp_locations_placed_address_before_address_max = 0;
11593 bp_locations_shadow_len_after_address_max = 0;
11594
11595 ALL_BP_LOCATIONS (bl, blp_tmp)
11596 {
11597 CORE_ADDR start, end, addr;
11598
11599 if (!bp_location_has_shadow (bl))
11600 continue;
11601
11602 start = bl->target_info.placed_address;
11603 end = start + bl->target_info.shadow_len;
11604
11605 gdb_assert (bl->address >= start);
11606 addr = bl->address - start;
11607 if (addr > bp_locations_placed_address_before_address_max)
11608 bp_locations_placed_address_before_address_max = addr;
11609
11610 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11611
11612 gdb_assert (bl->address < end);
11613 addr = end - bl->address;
11614 if (addr > bp_locations_shadow_len_after_address_max)
11615 bp_locations_shadow_len_after_address_max = addr;
11616 }
11617 }
11618
11619 /* Download tracepoint locations if they haven't been. */
11620
11621 static void
11622 download_tracepoint_locations (void)
11623 {
11624 struct breakpoint *b;
11625 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11626
11627 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11628
11629 ALL_TRACEPOINTS (b)
11630 {
11631 struct bp_location *bl;
11632 struct tracepoint *t;
11633 int bp_location_downloaded = 0;
11634
11635 if ((b->type == bp_fast_tracepoint
11636 ? !may_insert_fast_tracepoints
11637 : !may_insert_tracepoints))
11638 continue;
11639
11640 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11641 {
11642 if (target_can_download_tracepoint ())
11643 can_download_tracepoint = TRIBOOL_TRUE;
11644 else
11645 can_download_tracepoint = TRIBOOL_FALSE;
11646 }
11647
11648 if (can_download_tracepoint == TRIBOOL_FALSE)
11649 break;
11650
11651 for (bl = b->loc; bl; bl = bl->next)
11652 {
11653 /* In tracepoint, locations are _never_ duplicated, so
11654 should_be_inserted is equivalent to
11655 unduplicated_should_be_inserted. */
11656 if (!should_be_inserted (bl) || bl->inserted)
11657 continue;
11658
11659 switch_to_program_space_and_thread (bl->pspace);
11660
11661 target_download_tracepoint (bl);
11662
11663 bl->inserted = 1;
11664 bp_location_downloaded = 1;
11665 }
11666 t = (struct tracepoint *) b;
11667 t->number_on_target = b->number;
11668 if (bp_location_downloaded)
11669 gdb::observers::breakpoint_modified.notify (b);
11670 }
11671 }
11672
11673 /* Swap the insertion/duplication state between two locations. */
11674
11675 static void
11676 swap_insertion (struct bp_location *left, struct bp_location *right)
11677 {
11678 const int left_inserted = left->inserted;
11679 const int left_duplicate = left->duplicate;
11680 const int left_needs_update = left->needs_update;
11681 const struct bp_target_info left_target_info = left->target_info;
11682
11683 /* Locations of tracepoints can never be duplicated. */
11684 if (is_tracepoint (left->owner))
11685 gdb_assert (!left->duplicate);
11686 if (is_tracepoint (right->owner))
11687 gdb_assert (!right->duplicate);
11688
11689 left->inserted = right->inserted;
11690 left->duplicate = right->duplicate;
11691 left->needs_update = right->needs_update;
11692 left->target_info = right->target_info;
11693 right->inserted = left_inserted;
11694 right->duplicate = left_duplicate;
11695 right->needs_update = left_needs_update;
11696 right->target_info = left_target_info;
11697 }
11698
11699 /* Force the re-insertion of the locations at ADDRESS. This is called
11700 once a new/deleted/modified duplicate location is found and we are evaluating
11701 conditions on the target's side. Such conditions need to be updated on
11702 the target. */
11703
11704 static void
11705 force_breakpoint_reinsertion (struct bp_location *bl)
11706 {
11707 struct bp_location **locp = NULL, **loc2p;
11708 struct bp_location *loc;
11709 CORE_ADDR address = 0;
11710 int pspace_num;
11711
11712 address = bl->address;
11713 pspace_num = bl->pspace->num;
11714
11715 /* This is only meaningful if the target is
11716 evaluating conditions and if the user has
11717 opted for condition evaluation on the target's
11718 side. */
11719 if (gdb_evaluates_breakpoint_condition_p ()
11720 || !target_supports_evaluation_of_breakpoint_conditions ())
11721 return;
11722
11723 /* Flag all breakpoint locations with this address and
11724 the same program space as the location
11725 as "its condition has changed". We need to
11726 update the conditions on the target's side. */
11727 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11728 {
11729 loc = *loc2p;
11730
11731 if (!is_breakpoint (loc->owner)
11732 || pspace_num != loc->pspace->num)
11733 continue;
11734
11735 /* Flag the location appropriately. We use a different state to
11736 let everyone know that we already updated the set of locations
11737 with addr bl->address and program space bl->pspace. This is so
11738 we don't have to keep calling these functions just to mark locations
11739 that have already been marked. */
11740 loc->condition_changed = condition_updated;
11741
11742 /* Free the agent expression bytecode as well. We will compute
11743 it later on. */
11744 loc->cond_bytecode.reset ();
11745 }
11746 }
11747 /* Called whether new breakpoints are created, or existing breakpoints
11748 deleted, to update the global location list and recompute which
11749 locations are duplicate of which.
11750
11751 The INSERT_MODE flag determines whether locations may not, may, or
11752 shall be inserted now. See 'enum ugll_insert_mode' for more
11753 info. */
11754
11755 static void
11756 update_global_location_list (enum ugll_insert_mode insert_mode)
11757 {
11758 struct breakpoint *b;
11759 struct bp_location **locp, *loc;
11760 /* Last breakpoint location address that was marked for update. */
11761 CORE_ADDR last_addr = 0;
11762 /* Last breakpoint location program space that was marked for update. */
11763 int last_pspace_num = -1;
11764
11765 /* Used in the duplicates detection below. When iterating over all
11766 bp_locations, points to the first bp_location of a given address.
11767 Breakpoints and watchpoints of different types are never
11768 duplicates of each other. Keep one pointer for each type of
11769 breakpoint/watchpoint, so we only need to loop over all locations
11770 once. */
11771 struct bp_location *bp_loc_first; /* breakpoint */
11772 struct bp_location *wp_loc_first; /* hardware watchpoint */
11773 struct bp_location *awp_loc_first; /* access watchpoint */
11774 struct bp_location *rwp_loc_first; /* read watchpoint */
11775
11776 /* Saved former bp_locations array which we compare against the newly
11777 built bp_locations from the current state of ALL_BREAKPOINTS. */
11778 struct bp_location **old_locp;
11779 unsigned old_locations_count;
11780 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
11781
11782 old_locations_count = bp_locations_count;
11783 bp_locations = NULL;
11784 bp_locations_count = 0;
11785
11786 ALL_BREAKPOINTS (b)
11787 for (loc = b->loc; loc; loc = loc->next)
11788 bp_locations_count++;
11789
11790 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11791 locp = bp_locations;
11792 ALL_BREAKPOINTS (b)
11793 for (loc = b->loc; loc; loc = loc->next)
11794 *locp++ = loc;
11795 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11796 bp_locations_compare);
11797
11798 bp_locations_target_extensions_update ();
11799
11800 /* Identify bp_location instances that are no longer present in the
11801 new list, and therefore should be freed. Note that it's not
11802 necessary that those locations should be removed from inferior --
11803 if there's another location at the same address (previously
11804 marked as duplicate), we don't need to remove/insert the
11805 location.
11806
11807 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11808 and former bp_location array state respectively. */
11809
11810 locp = bp_locations;
11811 for (old_locp = old_locations.get ();
11812 old_locp < old_locations.get () + old_locations_count;
11813 old_locp++)
11814 {
11815 struct bp_location *old_loc = *old_locp;
11816 struct bp_location **loc2p;
11817
11818 /* Tells if 'old_loc' is found among the new locations. If
11819 not, we have to free it. */
11820 int found_object = 0;
11821 /* Tells if the location should remain inserted in the target. */
11822 int keep_in_target = 0;
11823 int removed = 0;
11824
11825 /* Skip LOCP entries which will definitely never be needed.
11826 Stop either at or being the one matching OLD_LOC. */
11827 while (locp < bp_locations + bp_locations_count
11828 && (*locp)->address < old_loc->address)
11829 locp++;
11830
11831 for (loc2p = locp;
11832 (loc2p < bp_locations + bp_locations_count
11833 && (*loc2p)->address == old_loc->address);
11834 loc2p++)
11835 {
11836 /* Check if this is a new/duplicated location or a duplicated
11837 location that had its condition modified. If so, we want to send
11838 its condition to the target if evaluation of conditions is taking
11839 place there. */
11840 if ((*loc2p)->condition_changed == condition_modified
11841 && (last_addr != old_loc->address
11842 || last_pspace_num != old_loc->pspace->num))
11843 {
11844 force_breakpoint_reinsertion (*loc2p);
11845 last_pspace_num = old_loc->pspace->num;
11846 }
11847
11848 if (*loc2p == old_loc)
11849 found_object = 1;
11850 }
11851
11852 /* We have already handled this address, update it so that we don't
11853 have to go through updates again. */
11854 last_addr = old_loc->address;
11855
11856 /* Target-side condition evaluation: Handle deleted locations. */
11857 if (!found_object)
11858 force_breakpoint_reinsertion (old_loc);
11859
11860 /* If this location is no longer present, and inserted, look if
11861 there's maybe a new location at the same address. If so,
11862 mark that one inserted, and don't remove this one. This is
11863 needed so that we don't have a time window where a breakpoint
11864 at certain location is not inserted. */
11865
11866 if (old_loc->inserted)
11867 {
11868 /* If the location is inserted now, we might have to remove
11869 it. */
11870
11871 if (found_object && should_be_inserted (old_loc))
11872 {
11873 /* The location is still present in the location list,
11874 and still should be inserted. Don't do anything. */
11875 keep_in_target = 1;
11876 }
11877 else
11878 {
11879 /* This location still exists, but it won't be kept in the
11880 target since it may have been disabled. We proceed to
11881 remove its target-side condition. */
11882
11883 /* The location is either no longer present, or got
11884 disabled. See if there's another location at the
11885 same address, in which case we don't need to remove
11886 this one from the target. */
11887
11888 /* OLD_LOC comes from existing struct breakpoint. */
11889 if (breakpoint_address_is_meaningful (old_loc->owner))
11890 {
11891 for (loc2p = locp;
11892 (loc2p < bp_locations + bp_locations_count
11893 && (*loc2p)->address == old_loc->address);
11894 loc2p++)
11895 {
11896 struct bp_location *loc2 = *loc2p;
11897
11898 if (breakpoint_locations_match (loc2, old_loc))
11899 {
11900 /* Read watchpoint locations are switched to
11901 access watchpoints, if the former are not
11902 supported, but the latter are. */
11903 if (is_hardware_watchpoint (old_loc->owner))
11904 {
11905 gdb_assert (is_hardware_watchpoint (loc2->owner));
11906 loc2->watchpoint_type = old_loc->watchpoint_type;
11907 }
11908
11909 /* loc2 is a duplicated location. We need to check
11910 if it should be inserted in case it will be
11911 unduplicated. */
11912 if (loc2 != old_loc
11913 && unduplicated_should_be_inserted (loc2))
11914 {
11915 swap_insertion (old_loc, loc2);
11916 keep_in_target = 1;
11917 break;
11918 }
11919 }
11920 }
11921 }
11922 }
11923
11924 if (!keep_in_target)
11925 {
11926 if (remove_breakpoint (old_loc))
11927 {
11928 /* This is just about all we can do. We could keep
11929 this location on the global list, and try to
11930 remove it next time, but there's no particular
11931 reason why we will succeed next time.
11932
11933 Note that at this point, old_loc->owner is still
11934 valid, as delete_breakpoint frees the breakpoint
11935 only after calling us. */
11936 printf_filtered (_("warning: Error removing "
11937 "breakpoint %d\n"),
11938 old_loc->owner->number);
11939 }
11940 removed = 1;
11941 }
11942 }
11943
11944 if (!found_object)
11945 {
11946 if (removed && target_is_non_stop_p ()
11947 && need_moribund_for_location_type (old_loc))
11948 {
11949 /* This location was removed from the target. In
11950 non-stop mode, a race condition is possible where
11951 we've removed a breakpoint, but stop events for that
11952 breakpoint are already queued and will arrive later.
11953 We apply an heuristic to be able to distinguish such
11954 SIGTRAPs from other random SIGTRAPs: we keep this
11955 breakpoint location for a bit, and will retire it
11956 after we see some number of events. The theory here
11957 is that reporting of events should, "on the average",
11958 be fair, so after a while we'll see events from all
11959 threads that have anything of interest, and no longer
11960 need to keep this breakpoint location around. We
11961 don't hold locations forever so to reduce chances of
11962 mistaking a non-breakpoint SIGTRAP for a breakpoint
11963 SIGTRAP.
11964
11965 The heuristic failing can be disastrous on
11966 decr_pc_after_break targets.
11967
11968 On decr_pc_after_break targets, like e.g., x86-linux,
11969 if we fail to recognize a late breakpoint SIGTRAP,
11970 because events_till_retirement has reached 0 too
11971 soon, we'll fail to do the PC adjustment, and report
11972 a random SIGTRAP to the user. When the user resumes
11973 the inferior, it will most likely immediately crash
11974 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11975 corrupted, because of being resumed e.g., in the
11976 middle of a multi-byte instruction, or skipped a
11977 one-byte instruction. This was actually seen happen
11978 on native x86-linux, and should be less rare on
11979 targets that do not support new thread events, like
11980 remote, due to the heuristic depending on
11981 thread_count.
11982
11983 Mistaking a random SIGTRAP for a breakpoint trap
11984 causes similar symptoms (PC adjustment applied when
11985 it shouldn't), but then again, playing with SIGTRAPs
11986 behind the debugger's back is asking for trouble.
11987
11988 Since hardware watchpoint traps are always
11989 distinguishable from other traps, so we don't need to
11990 apply keep hardware watchpoint moribund locations
11991 around. We simply always ignore hardware watchpoint
11992 traps we can no longer explain. */
11993
11994 old_loc->events_till_retirement = 3 * (thread_count () + 1);
11995 old_loc->owner = NULL;
11996
11997 moribund_locations.push_back (old_loc);
11998 }
11999 else
12000 {
12001 old_loc->owner = NULL;
12002 decref_bp_location (&old_loc);
12003 }
12004 }
12005 }
12006
12007 /* Rescan breakpoints at the same address and section, marking the
12008 first one as "first" and any others as "duplicates". This is so
12009 that the bpt instruction is only inserted once. If we have a
12010 permanent breakpoint at the same place as BPT, make that one the
12011 official one, and the rest as duplicates. Permanent breakpoints
12012 are sorted first for the same address.
12013
12014 Do the same for hardware watchpoints, but also considering the
12015 watchpoint's type (regular/access/read) and length. */
12016
12017 bp_loc_first = NULL;
12018 wp_loc_first = NULL;
12019 awp_loc_first = NULL;
12020 rwp_loc_first = NULL;
12021 ALL_BP_LOCATIONS (loc, locp)
12022 {
12023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12024 non-NULL. */
12025 struct bp_location **loc_first_p;
12026 b = loc->owner;
12027
12028 if (!unduplicated_should_be_inserted (loc)
12029 || !breakpoint_address_is_meaningful (b)
12030 /* Don't detect duplicate for tracepoint locations because they are
12031 never duplicated. See the comments in field `duplicate' of
12032 `struct bp_location'. */
12033 || is_tracepoint (b))
12034 {
12035 /* Clear the condition modification flag. */
12036 loc->condition_changed = condition_unchanged;
12037 continue;
12038 }
12039
12040 if (b->type == bp_hardware_watchpoint)
12041 loc_first_p = &wp_loc_first;
12042 else if (b->type == bp_read_watchpoint)
12043 loc_first_p = &rwp_loc_first;
12044 else if (b->type == bp_access_watchpoint)
12045 loc_first_p = &awp_loc_first;
12046 else
12047 loc_first_p = &bp_loc_first;
12048
12049 if (*loc_first_p == NULL
12050 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12051 || !breakpoint_locations_match (loc, *loc_first_p))
12052 {
12053 *loc_first_p = loc;
12054 loc->duplicate = 0;
12055
12056 if (is_breakpoint (loc->owner) && loc->condition_changed)
12057 {
12058 loc->needs_update = 1;
12059 /* Clear the condition modification flag. */
12060 loc->condition_changed = condition_unchanged;
12061 }
12062 continue;
12063 }
12064
12065
12066 /* This and the above ensure the invariant that the first location
12067 is not duplicated, and is the inserted one.
12068 All following are marked as duplicated, and are not inserted. */
12069 if (loc->inserted)
12070 swap_insertion (loc, *loc_first_p);
12071 loc->duplicate = 1;
12072
12073 /* Clear the condition modification flag. */
12074 loc->condition_changed = condition_unchanged;
12075 }
12076
12077 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12078 {
12079 if (insert_mode != UGLL_DONT_INSERT)
12080 insert_breakpoint_locations ();
12081 else
12082 {
12083 /* Even though the caller told us to not insert new
12084 locations, we may still need to update conditions on the
12085 target's side of breakpoints that were already inserted
12086 if the target is evaluating breakpoint conditions. We
12087 only update conditions for locations that are marked
12088 "needs_update". */
12089 update_inserted_breakpoint_locations ();
12090 }
12091 }
12092
12093 if (insert_mode != UGLL_DONT_INSERT)
12094 download_tracepoint_locations ();
12095 }
12096
12097 void
12098 breakpoint_retire_moribund (void)
12099 {
12100 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12101 {
12102 struct bp_location *loc = moribund_locations[ix];
12103 if (--(loc->events_till_retirement) == 0)
12104 {
12105 decref_bp_location (&loc);
12106 unordered_remove (moribund_locations, ix);
12107 --ix;
12108 }
12109 }
12110 }
12111
12112 static void
12113 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12114 {
12115
12116 TRY
12117 {
12118 update_global_location_list (insert_mode);
12119 }
12120 CATCH (e, RETURN_MASK_ERROR)
12121 {
12122 }
12123 END_CATCH
12124 }
12125
12126 /* Clear BKP from a BPS. */
12127
12128 static void
12129 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12130 {
12131 bpstat bs;
12132
12133 for (bs = bps; bs; bs = bs->next)
12134 if (bs->breakpoint_at == bpt)
12135 {
12136 bs->breakpoint_at = NULL;
12137 bs->old_val = NULL;
12138 /* bs->commands will be freed later. */
12139 }
12140 }
12141
12142 /* Callback for iterate_over_threads. */
12143 static int
12144 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12145 {
12146 struct breakpoint *bpt = (struct breakpoint *) data;
12147
12148 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12149 return 0;
12150 }
12151
12152 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12153 callbacks. */
12154
12155 static void
12156 say_where (struct breakpoint *b)
12157 {
12158 struct value_print_options opts;
12159
12160 get_user_print_options (&opts);
12161
12162 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12163 single string. */
12164 if (b->loc == NULL)
12165 {
12166 /* For pending locations, the output differs slightly based
12167 on b->extra_string. If this is non-NULL, it contains either
12168 a condition or dprintf arguments. */
12169 if (b->extra_string == NULL)
12170 {
12171 printf_filtered (_(" (%s) pending."),
12172 event_location_to_string (b->location.get ()));
12173 }
12174 else if (b->type == bp_dprintf)
12175 {
12176 printf_filtered (_(" (%s,%s) pending."),
12177 event_location_to_string (b->location.get ()),
12178 b->extra_string);
12179 }
12180 else
12181 {
12182 printf_filtered (_(" (%s %s) pending."),
12183 event_location_to_string (b->location.get ()),
12184 b->extra_string);
12185 }
12186 }
12187 else
12188 {
12189 if (opts.addressprint || b->loc->symtab == NULL)
12190 {
12191 printf_filtered (" at ");
12192 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12193 gdb_stdout);
12194 }
12195 if (b->loc->symtab != NULL)
12196 {
12197 /* If there is a single location, we can print the location
12198 more nicely. */
12199 if (b->loc->next == NULL)
12200 printf_filtered (": file %s, line %d.",
12201 symtab_to_filename_for_display (b->loc->symtab),
12202 b->loc->line_number);
12203 else
12204 /* This is not ideal, but each location may have a
12205 different file name, and this at least reflects the
12206 real situation somewhat. */
12207 printf_filtered (": %s.",
12208 event_location_to_string (b->location.get ()));
12209 }
12210
12211 if (b->loc->next)
12212 {
12213 struct bp_location *loc = b->loc;
12214 int n = 0;
12215 for (; loc; loc = loc->next)
12216 ++n;
12217 printf_filtered (" (%d locations)", n);
12218 }
12219 }
12220 }
12221
12222 /* Default bp_location_ops methods. */
12223
12224 static void
12225 bp_location_dtor (struct bp_location *self)
12226 {
12227 xfree (self->function_name);
12228 }
12229
12230 static const struct bp_location_ops bp_location_ops =
12231 {
12232 bp_location_dtor
12233 };
12234
12235 /* Destructor for the breakpoint base class. */
12236
12237 breakpoint::~breakpoint ()
12238 {
12239 xfree (this->cond_string);
12240 xfree (this->extra_string);
12241 xfree (this->filter);
12242 }
12243
12244 static struct bp_location *
12245 base_breakpoint_allocate_location (struct breakpoint *self)
12246 {
12247 return new bp_location (&bp_location_ops, self);
12248 }
12249
12250 static void
12251 base_breakpoint_re_set (struct breakpoint *b)
12252 {
12253 /* Nothing to re-set. */
12254 }
12255
12256 #define internal_error_pure_virtual_called() \
12257 gdb_assert_not_reached ("pure virtual function called")
12258
12259 static int
12260 base_breakpoint_insert_location (struct bp_location *bl)
12261 {
12262 internal_error_pure_virtual_called ();
12263 }
12264
12265 static int
12266 base_breakpoint_remove_location (struct bp_location *bl,
12267 enum remove_bp_reason reason)
12268 {
12269 internal_error_pure_virtual_called ();
12270 }
12271
12272 static int
12273 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12274 const address_space *aspace,
12275 CORE_ADDR bp_addr,
12276 const struct target_waitstatus *ws)
12277 {
12278 internal_error_pure_virtual_called ();
12279 }
12280
12281 static void
12282 base_breakpoint_check_status (bpstat bs)
12283 {
12284 /* Always stop. */
12285 }
12286
12287 /* A "works_in_software_mode" breakpoint_ops method that just internal
12288 errors. */
12289
12290 static int
12291 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12292 {
12293 internal_error_pure_virtual_called ();
12294 }
12295
12296 /* A "resources_needed" breakpoint_ops method that just internal
12297 errors. */
12298
12299 static int
12300 base_breakpoint_resources_needed (const struct bp_location *bl)
12301 {
12302 internal_error_pure_virtual_called ();
12303 }
12304
12305 static enum print_stop_action
12306 base_breakpoint_print_it (bpstat bs)
12307 {
12308 internal_error_pure_virtual_called ();
12309 }
12310
12311 static void
12312 base_breakpoint_print_one_detail (const struct breakpoint *self,
12313 struct ui_out *uiout)
12314 {
12315 /* nothing */
12316 }
12317
12318 static void
12319 base_breakpoint_print_mention (struct breakpoint *b)
12320 {
12321 internal_error_pure_virtual_called ();
12322 }
12323
12324 static void
12325 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12326 {
12327 internal_error_pure_virtual_called ();
12328 }
12329
12330 static void
12331 base_breakpoint_create_sals_from_location
12332 (const struct event_location *location,
12333 struct linespec_result *canonical,
12334 enum bptype type_wanted)
12335 {
12336 internal_error_pure_virtual_called ();
12337 }
12338
12339 static void
12340 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12341 struct linespec_result *c,
12342 gdb::unique_xmalloc_ptr<char> cond_string,
12343 gdb::unique_xmalloc_ptr<char> extra_string,
12344 enum bptype type_wanted,
12345 enum bpdisp disposition,
12346 int thread,
12347 int task, int ignore_count,
12348 const struct breakpoint_ops *o,
12349 int from_tty, int enabled,
12350 int internal, unsigned flags)
12351 {
12352 internal_error_pure_virtual_called ();
12353 }
12354
12355 static std::vector<symtab_and_line>
12356 base_breakpoint_decode_location (struct breakpoint *b,
12357 const struct event_location *location,
12358 struct program_space *search_pspace)
12359 {
12360 internal_error_pure_virtual_called ();
12361 }
12362
12363 /* The default 'explains_signal' method. */
12364
12365 static int
12366 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12367 {
12368 return 1;
12369 }
12370
12371 /* The default "after_condition_true" method. */
12372
12373 static void
12374 base_breakpoint_after_condition_true (struct bpstats *bs)
12375 {
12376 /* Nothing to do. */
12377 }
12378
12379 struct breakpoint_ops base_breakpoint_ops =
12380 {
12381 base_breakpoint_allocate_location,
12382 base_breakpoint_re_set,
12383 base_breakpoint_insert_location,
12384 base_breakpoint_remove_location,
12385 base_breakpoint_breakpoint_hit,
12386 base_breakpoint_check_status,
12387 base_breakpoint_resources_needed,
12388 base_breakpoint_works_in_software_mode,
12389 base_breakpoint_print_it,
12390 NULL,
12391 base_breakpoint_print_one_detail,
12392 base_breakpoint_print_mention,
12393 base_breakpoint_print_recreate,
12394 base_breakpoint_create_sals_from_location,
12395 base_breakpoint_create_breakpoints_sal,
12396 base_breakpoint_decode_location,
12397 base_breakpoint_explains_signal,
12398 base_breakpoint_after_condition_true,
12399 };
12400
12401 /* Default breakpoint_ops methods. */
12402
12403 static void
12404 bkpt_re_set (struct breakpoint *b)
12405 {
12406 /* FIXME: is this still reachable? */
12407 if (breakpoint_event_location_empty_p (b))
12408 {
12409 /* Anything without a location can't be re-set. */
12410 delete_breakpoint (b);
12411 return;
12412 }
12413
12414 breakpoint_re_set_default (b);
12415 }
12416
12417 static int
12418 bkpt_insert_location (struct bp_location *bl)
12419 {
12420 CORE_ADDR addr = bl->target_info.reqstd_address;
12421
12422 bl->target_info.kind = breakpoint_kind (bl, &addr);
12423 bl->target_info.placed_address = addr;
12424
12425 if (bl->loc_type == bp_loc_hardware_breakpoint)
12426 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12427 else
12428 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12429 }
12430
12431 static int
12432 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12433 {
12434 if (bl->loc_type == bp_loc_hardware_breakpoint)
12435 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12436 else
12437 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12438 }
12439
12440 static int
12441 bkpt_breakpoint_hit (const struct bp_location *bl,
12442 const address_space *aspace, CORE_ADDR bp_addr,
12443 const struct target_waitstatus *ws)
12444 {
12445 if (ws->kind != TARGET_WAITKIND_STOPPED
12446 || ws->value.sig != GDB_SIGNAL_TRAP)
12447 return 0;
12448
12449 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12450 aspace, bp_addr))
12451 return 0;
12452
12453 if (overlay_debugging /* unmapped overlay section */
12454 && section_is_overlay (bl->section)
12455 && !section_is_mapped (bl->section))
12456 return 0;
12457
12458 return 1;
12459 }
12460
12461 static int
12462 dprintf_breakpoint_hit (const struct bp_location *bl,
12463 const address_space *aspace, CORE_ADDR bp_addr,
12464 const struct target_waitstatus *ws)
12465 {
12466 if (dprintf_style == dprintf_style_agent
12467 && target_can_run_breakpoint_commands ())
12468 {
12469 /* An agent-style dprintf never causes a stop. If we see a trap
12470 for this address it must be for a breakpoint that happens to
12471 be set at the same address. */
12472 return 0;
12473 }
12474
12475 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12476 }
12477
12478 static int
12479 bkpt_resources_needed (const struct bp_location *bl)
12480 {
12481 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12482
12483 return 1;
12484 }
12485
12486 static enum print_stop_action
12487 bkpt_print_it (bpstat bs)
12488 {
12489 struct breakpoint *b;
12490 const struct bp_location *bl;
12491 int bp_temp;
12492 struct ui_out *uiout = current_uiout;
12493
12494 gdb_assert (bs->bp_location_at != NULL);
12495
12496 bl = bs->bp_location_at;
12497 b = bs->breakpoint_at;
12498
12499 bp_temp = b->disposition == disp_del;
12500 if (bl->address != bl->requested_address)
12501 breakpoint_adjustment_warning (bl->requested_address,
12502 bl->address,
12503 b->number, 1);
12504 annotate_breakpoint (b->number);
12505 maybe_print_thread_hit_breakpoint (uiout);
12506
12507 if (bp_temp)
12508 uiout->text ("Temporary breakpoint ");
12509 else
12510 uiout->text ("Breakpoint ");
12511 if (uiout->is_mi_like_p ())
12512 {
12513 uiout->field_string ("reason",
12514 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12515 uiout->field_string ("disp", bpdisp_text (b->disposition));
12516 }
12517 uiout->field_int ("bkptno", b->number);
12518 uiout->text (", ");
12519
12520 return PRINT_SRC_AND_LOC;
12521 }
12522
12523 static void
12524 bkpt_print_mention (struct breakpoint *b)
12525 {
12526 if (current_uiout->is_mi_like_p ())
12527 return;
12528
12529 switch (b->type)
12530 {
12531 case bp_breakpoint:
12532 case bp_gnu_ifunc_resolver:
12533 if (b->disposition == disp_del)
12534 printf_filtered (_("Temporary breakpoint"));
12535 else
12536 printf_filtered (_("Breakpoint"));
12537 printf_filtered (_(" %d"), b->number);
12538 if (b->type == bp_gnu_ifunc_resolver)
12539 printf_filtered (_(" at gnu-indirect-function resolver"));
12540 break;
12541 case bp_hardware_breakpoint:
12542 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12543 break;
12544 case bp_dprintf:
12545 printf_filtered (_("Dprintf %d"), b->number);
12546 break;
12547 }
12548
12549 say_where (b);
12550 }
12551
12552 static void
12553 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12554 {
12555 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12556 fprintf_unfiltered (fp, "tbreak");
12557 else if (tp->type == bp_breakpoint)
12558 fprintf_unfiltered (fp, "break");
12559 else if (tp->type == bp_hardware_breakpoint
12560 && tp->disposition == disp_del)
12561 fprintf_unfiltered (fp, "thbreak");
12562 else if (tp->type == bp_hardware_breakpoint)
12563 fprintf_unfiltered (fp, "hbreak");
12564 else
12565 internal_error (__FILE__, __LINE__,
12566 _("unhandled breakpoint type %d"), (int) tp->type);
12567
12568 fprintf_unfiltered (fp, " %s",
12569 event_location_to_string (tp->location.get ()));
12570
12571 /* Print out extra_string if this breakpoint is pending. It might
12572 contain, for example, conditions that were set by the user. */
12573 if (tp->loc == NULL && tp->extra_string != NULL)
12574 fprintf_unfiltered (fp, " %s", tp->extra_string);
12575
12576 print_recreate_thread (tp, fp);
12577 }
12578
12579 static void
12580 bkpt_create_sals_from_location (const struct event_location *location,
12581 struct linespec_result *canonical,
12582 enum bptype type_wanted)
12583 {
12584 create_sals_from_location_default (location, canonical, type_wanted);
12585 }
12586
12587 static void
12588 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12589 struct linespec_result *canonical,
12590 gdb::unique_xmalloc_ptr<char> cond_string,
12591 gdb::unique_xmalloc_ptr<char> extra_string,
12592 enum bptype type_wanted,
12593 enum bpdisp disposition,
12594 int thread,
12595 int task, int ignore_count,
12596 const struct breakpoint_ops *ops,
12597 int from_tty, int enabled,
12598 int internal, unsigned flags)
12599 {
12600 create_breakpoints_sal_default (gdbarch, canonical,
12601 std::move (cond_string),
12602 std::move (extra_string),
12603 type_wanted,
12604 disposition, thread, task,
12605 ignore_count, ops, from_tty,
12606 enabled, internal, flags);
12607 }
12608
12609 static std::vector<symtab_and_line>
12610 bkpt_decode_location (struct breakpoint *b,
12611 const struct event_location *location,
12612 struct program_space *search_pspace)
12613 {
12614 return decode_location_default (b, location, search_pspace);
12615 }
12616
12617 /* Virtual table for internal breakpoints. */
12618
12619 static void
12620 internal_bkpt_re_set (struct breakpoint *b)
12621 {
12622 switch (b->type)
12623 {
12624 /* Delete overlay event and longjmp master breakpoints; they
12625 will be reset later by breakpoint_re_set. */
12626 case bp_overlay_event:
12627 case bp_longjmp_master:
12628 case bp_std_terminate_master:
12629 case bp_exception_master:
12630 delete_breakpoint (b);
12631 break;
12632
12633 /* This breakpoint is special, it's set up when the inferior
12634 starts and we really don't want to touch it. */
12635 case bp_shlib_event:
12636
12637 /* Like bp_shlib_event, this breakpoint type is special. Once
12638 it is set up, we do not want to touch it. */
12639 case bp_thread_event:
12640 break;
12641 }
12642 }
12643
12644 static void
12645 internal_bkpt_check_status (bpstat bs)
12646 {
12647 if (bs->breakpoint_at->type == bp_shlib_event)
12648 {
12649 /* If requested, stop when the dynamic linker notifies GDB of
12650 events. This allows the user to get control and place
12651 breakpoints in initializer routines for dynamically loaded
12652 objects (among other things). */
12653 bs->stop = stop_on_solib_events;
12654 bs->print = stop_on_solib_events;
12655 }
12656 else
12657 bs->stop = 0;
12658 }
12659
12660 static enum print_stop_action
12661 internal_bkpt_print_it (bpstat bs)
12662 {
12663 struct breakpoint *b;
12664
12665 b = bs->breakpoint_at;
12666
12667 switch (b->type)
12668 {
12669 case bp_shlib_event:
12670 /* Did we stop because the user set the stop_on_solib_events
12671 variable? (If so, we report this as a generic, "Stopped due
12672 to shlib event" message.) */
12673 print_solib_event (0);
12674 break;
12675
12676 case bp_thread_event:
12677 /* Not sure how we will get here.
12678 GDB should not stop for these breakpoints. */
12679 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12680 break;
12681
12682 case bp_overlay_event:
12683 /* By analogy with the thread event, GDB should not stop for these. */
12684 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12685 break;
12686
12687 case bp_longjmp_master:
12688 /* These should never be enabled. */
12689 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12690 break;
12691
12692 case bp_std_terminate_master:
12693 /* These should never be enabled. */
12694 printf_filtered (_("std::terminate Master Breakpoint: "
12695 "gdb should not stop!\n"));
12696 break;
12697
12698 case bp_exception_master:
12699 /* These should never be enabled. */
12700 printf_filtered (_("Exception Master Breakpoint: "
12701 "gdb should not stop!\n"));
12702 break;
12703 }
12704
12705 return PRINT_NOTHING;
12706 }
12707
12708 static void
12709 internal_bkpt_print_mention (struct breakpoint *b)
12710 {
12711 /* Nothing to mention. These breakpoints are internal. */
12712 }
12713
12714 /* Virtual table for momentary breakpoints */
12715
12716 static void
12717 momentary_bkpt_re_set (struct breakpoint *b)
12718 {
12719 /* Keep temporary breakpoints, which can be encountered when we step
12720 over a dlopen call and solib_add is resetting the breakpoints.
12721 Otherwise these should have been blown away via the cleanup chain
12722 or by breakpoint_init_inferior when we rerun the executable. */
12723 }
12724
12725 static void
12726 momentary_bkpt_check_status (bpstat bs)
12727 {
12728 /* Nothing. The point of these breakpoints is causing a stop. */
12729 }
12730
12731 static enum print_stop_action
12732 momentary_bkpt_print_it (bpstat bs)
12733 {
12734 return PRINT_UNKNOWN;
12735 }
12736
12737 static void
12738 momentary_bkpt_print_mention (struct breakpoint *b)
12739 {
12740 /* Nothing to mention. These breakpoints are internal. */
12741 }
12742
12743 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12744
12745 It gets cleared already on the removal of the first one of such placed
12746 breakpoints. This is OK as they get all removed altogether. */
12747
12748 longjmp_breakpoint::~longjmp_breakpoint ()
12749 {
12750 thread_info *tp = find_thread_global_id (this->thread);
12751
12752 if (tp != NULL)
12753 tp->initiating_frame = null_frame_id;
12754 }
12755
12756 /* Specific methods for probe breakpoints. */
12757
12758 static int
12759 bkpt_probe_insert_location (struct bp_location *bl)
12760 {
12761 int v = bkpt_insert_location (bl);
12762
12763 if (v == 0)
12764 {
12765 /* The insertion was successful, now let's set the probe's semaphore
12766 if needed. */
12767 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12768 }
12769
12770 return v;
12771 }
12772
12773 static int
12774 bkpt_probe_remove_location (struct bp_location *bl,
12775 enum remove_bp_reason reason)
12776 {
12777 /* Let's clear the semaphore before removing the location. */
12778 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12779
12780 return bkpt_remove_location (bl, reason);
12781 }
12782
12783 static void
12784 bkpt_probe_create_sals_from_location (const struct event_location *location,
12785 struct linespec_result *canonical,
12786 enum bptype type_wanted)
12787 {
12788 struct linespec_sals lsal;
12789
12790 lsal.sals = parse_probes (location, NULL, canonical);
12791 lsal.canonical
12792 = xstrdup (event_location_to_string (canonical->location.get ()));
12793 canonical->lsals.push_back (std::move (lsal));
12794 }
12795
12796 static std::vector<symtab_and_line>
12797 bkpt_probe_decode_location (struct breakpoint *b,
12798 const struct event_location *location,
12799 struct program_space *search_pspace)
12800 {
12801 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12802 if (sals.empty ())
12803 error (_("probe not found"));
12804 return sals;
12805 }
12806
12807 /* The breakpoint_ops structure to be used in tracepoints. */
12808
12809 static void
12810 tracepoint_re_set (struct breakpoint *b)
12811 {
12812 breakpoint_re_set_default (b);
12813 }
12814
12815 static int
12816 tracepoint_breakpoint_hit (const struct bp_location *bl,
12817 const address_space *aspace, CORE_ADDR bp_addr,
12818 const struct target_waitstatus *ws)
12819 {
12820 /* By definition, the inferior does not report stops at
12821 tracepoints. */
12822 return 0;
12823 }
12824
12825 static void
12826 tracepoint_print_one_detail (const struct breakpoint *self,
12827 struct ui_out *uiout)
12828 {
12829 struct tracepoint *tp = (struct tracepoint *) self;
12830 if (!tp->static_trace_marker_id.empty ())
12831 {
12832 gdb_assert (self->type == bp_static_tracepoint);
12833
12834 uiout->text ("\tmarker id is ");
12835 uiout->field_string ("static-tracepoint-marker-string-id",
12836 tp->static_trace_marker_id);
12837 uiout->text ("\n");
12838 }
12839 }
12840
12841 static void
12842 tracepoint_print_mention (struct breakpoint *b)
12843 {
12844 if (current_uiout->is_mi_like_p ())
12845 return;
12846
12847 switch (b->type)
12848 {
12849 case bp_tracepoint:
12850 printf_filtered (_("Tracepoint"));
12851 printf_filtered (_(" %d"), b->number);
12852 break;
12853 case bp_fast_tracepoint:
12854 printf_filtered (_("Fast tracepoint"));
12855 printf_filtered (_(" %d"), b->number);
12856 break;
12857 case bp_static_tracepoint:
12858 printf_filtered (_("Static tracepoint"));
12859 printf_filtered (_(" %d"), b->number);
12860 break;
12861 default:
12862 internal_error (__FILE__, __LINE__,
12863 _("unhandled tracepoint type %d"), (int) b->type);
12864 }
12865
12866 say_where (b);
12867 }
12868
12869 static void
12870 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12871 {
12872 struct tracepoint *tp = (struct tracepoint *) self;
12873
12874 if (self->type == bp_fast_tracepoint)
12875 fprintf_unfiltered (fp, "ftrace");
12876 else if (self->type == bp_static_tracepoint)
12877 fprintf_unfiltered (fp, "strace");
12878 else if (self->type == bp_tracepoint)
12879 fprintf_unfiltered (fp, "trace");
12880 else
12881 internal_error (__FILE__, __LINE__,
12882 _("unhandled tracepoint type %d"), (int) self->type);
12883
12884 fprintf_unfiltered (fp, " %s",
12885 event_location_to_string (self->location.get ()));
12886 print_recreate_thread (self, fp);
12887
12888 if (tp->pass_count)
12889 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12890 }
12891
12892 static void
12893 tracepoint_create_sals_from_location (const struct event_location *location,
12894 struct linespec_result *canonical,
12895 enum bptype type_wanted)
12896 {
12897 create_sals_from_location_default (location, canonical, type_wanted);
12898 }
12899
12900 static void
12901 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12902 struct linespec_result *canonical,
12903 gdb::unique_xmalloc_ptr<char> cond_string,
12904 gdb::unique_xmalloc_ptr<char> extra_string,
12905 enum bptype type_wanted,
12906 enum bpdisp disposition,
12907 int thread,
12908 int task, int ignore_count,
12909 const struct breakpoint_ops *ops,
12910 int from_tty, int enabled,
12911 int internal, unsigned flags)
12912 {
12913 create_breakpoints_sal_default (gdbarch, canonical,
12914 std::move (cond_string),
12915 std::move (extra_string),
12916 type_wanted,
12917 disposition, thread, task,
12918 ignore_count, ops, from_tty,
12919 enabled, internal, flags);
12920 }
12921
12922 static std::vector<symtab_and_line>
12923 tracepoint_decode_location (struct breakpoint *b,
12924 const struct event_location *location,
12925 struct program_space *search_pspace)
12926 {
12927 return decode_location_default (b, location, search_pspace);
12928 }
12929
12930 struct breakpoint_ops tracepoint_breakpoint_ops;
12931
12932 /* The breakpoint_ops structure to be use on tracepoints placed in a
12933 static probe. */
12934
12935 static void
12936 tracepoint_probe_create_sals_from_location
12937 (const struct event_location *location,
12938 struct linespec_result *canonical,
12939 enum bptype type_wanted)
12940 {
12941 /* We use the same method for breakpoint on probes. */
12942 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12943 }
12944
12945 static std::vector<symtab_and_line>
12946 tracepoint_probe_decode_location (struct breakpoint *b,
12947 const struct event_location *location,
12948 struct program_space *search_pspace)
12949 {
12950 /* We use the same method for breakpoint on probes. */
12951 return bkpt_probe_decode_location (b, location, search_pspace);
12952 }
12953
12954 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12955
12956 /* Dprintf breakpoint_ops methods. */
12957
12958 static void
12959 dprintf_re_set (struct breakpoint *b)
12960 {
12961 breakpoint_re_set_default (b);
12962
12963 /* extra_string should never be non-NULL for dprintf. */
12964 gdb_assert (b->extra_string != NULL);
12965
12966 /* 1 - connect to target 1, that can run breakpoint commands.
12967 2 - create a dprintf, which resolves fine.
12968 3 - disconnect from target 1
12969 4 - connect to target 2, that can NOT run breakpoint commands.
12970
12971 After steps #3/#4, you'll want the dprintf command list to
12972 be updated, because target 1 and 2 may well return different
12973 answers for target_can_run_breakpoint_commands().
12974 Given absence of finer grained resetting, we get to do
12975 it all the time. */
12976 if (b->extra_string != NULL)
12977 update_dprintf_command_list (b);
12978 }
12979
12980 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12981
12982 static void
12983 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12984 {
12985 fprintf_unfiltered (fp, "dprintf %s,%s",
12986 event_location_to_string (tp->location.get ()),
12987 tp->extra_string);
12988 print_recreate_thread (tp, fp);
12989 }
12990
12991 /* Implement the "after_condition_true" breakpoint_ops method for
12992 dprintf.
12993
12994 dprintf's are implemented with regular commands in their command
12995 list, but we run the commands here instead of before presenting the
12996 stop to the user, as dprintf's don't actually cause a stop. This
12997 also makes it so that the commands of multiple dprintfs at the same
12998 address are all handled. */
12999
13000 static void
13001 dprintf_after_condition_true (struct bpstats *bs)
13002 {
13003 struct bpstats tmp_bs;
13004 struct bpstats *tmp_bs_p = &tmp_bs;
13005
13006 /* dprintf's never cause a stop. This wasn't set in the
13007 check_status hook instead because that would make the dprintf's
13008 condition not be evaluated. */
13009 bs->stop = 0;
13010
13011 /* Run the command list here. Take ownership of it instead of
13012 copying. We never want these commands to run later in
13013 bpstat_do_actions, if a breakpoint that causes a stop happens to
13014 be set at same address as this dprintf, or even if running the
13015 commands here throws. */
13016 tmp_bs.commands = bs->commands;
13017 bs->commands = NULL;
13018
13019 bpstat_do_actions_1 (&tmp_bs_p);
13020
13021 /* 'tmp_bs.commands' will usually be NULL by now, but
13022 bpstat_do_actions_1 may return early without processing the whole
13023 list. */
13024 }
13025
13026 /* The breakpoint_ops structure to be used on static tracepoints with
13027 markers (`-m'). */
13028
13029 static void
13030 strace_marker_create_sals_from_location (const struct event_location *location,
13031 struct linespec_result *canonical,
13032 enum bptype type_wanted)
13033 {
13034 struct linespec_sals lsal;
13035 const char *arg_start, *arg;
13036
13037 arg = arg_start = get_linespec_location (location)->spec_string;
13038 lsal.sals = decode_static_tracepoint_spec (&arg);
13039
13040 std::string str (arg_start, arg - arg_start);
13041 const char *ptr = str.c_str ();
13042 canonical->location
13043 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
13044
13045 lsal.canonical
13046 = xstrdup (event_location_to_string (canonical->location.get ()));
13047 canonical->lsals.push_back (std::move (lsal));
13048 }
13049
13050 static void
13051 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13052 struct linespec_result *canonical,
13053 gdb::unique_xmalloc_ptr<char> cond_string,
13054 gdb::unique_xmalloc_ptr<char> extra_string,
13055 enum bptype type_wanted,
13056 enum bpdisp disposition,
13057 int thread,
13058 int task, int ignore_count,
13059 const struct breakpoint_ops *ops,
13060 int from_tty, int enabled,
13061 int internal, unsigned flags)
13062 {
13063 const linespec_sals &lsal = canonical->lsals[0];
13064
13065 /* If the user is creating a static tracepoint by marker id
13066 (strace -m MARKER_ID), then store the sals index, so that
13067 breakpoint_re_set can try to match up which of the newly
13068 found markers corresponds to this one, and, don't try to
13069 expand multiple locations for each sal, given than SALS
13070 already should contain all sals for MARKER_ID. */
13071
13072 for (size_t i = 0; i < lsal.sals.size (); i++)
13073 {
13074 event_location_up location
13075 = copy_event_location (canonical->location.get ());
13076
13077 std::unique_ptr<tracepoint> tp (new tracepoint ());
13078 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13079 std::move (location), NULL,
13080 std::move (cond_string),
13081 std::move (extra_string),
13082 type_wanted, disposition,
13083 thread, task, ignore_count, ops,
13084 from_tty, enabled, internal, flags,
13085 canonical->special_display);
13086 /* Given that its possible to have multiple markers with
13087 the same string id, if the user is creating a static
13088 tracepoint by marker id ("strace -m MARKER_ID"), then
13089 store the sals index, so that breakpoint_re_set can
13090 try to match up which of the newly found markers
13091 corresponds to this one */
13092 tp->static_trace_marker_id_idx = i;
13093
13094 install_breakpoint (internal, std::move (tp), 0);
13095 }
13096 }
13097
13098 static std::vector<symtab_and_line>
13099 strace_marker_decode_location (struct breakpoint *b,
13100 const struct event_location *location,
13101 struct program_space *search_pspace)
13102 {
13103 struct tracepoint *tp = (struct tracepoint *) b;
13104 const char *s = get_linespec_location (location)->spec_string;
13105
13106 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13107 if (sals.size () > tp->static_trace_marker_id_idx)
13108 {
13109 sals[0] = sals[tp->static_trace_marker_id_idx];
13110 sals.resize (1);
13111 return sals;
13112 }
13113 else
13114 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
13115 }
13116
13117 static struct breakpoint_ops strace_marker_breakpoint_ops;
13118
13119 static int
13120 strace_marker_p (struct breakpoint *b)
13121 {
13122 return b->ops == &strace_marker_breakpoint_ops;
13123 }
13124
13125 /* Delete a breakpoint and clean up all traces of it in the data
13126 structures. */
13127
13128 void
13129 delete_breakpoint (struct breakpoint *bpt)
13130 {
13131 struct breakpoint *b;
13132
13133 gdb_assert (bpt != NULL);
13134
13135 /* Has this bp already been deleted? This can happen because
13136 multiple lists can hold pointers to bp's. bpstat lists are
13137 especial culprits.
13138
13139 One example of this happening is a watchpoint's scope bp. When
13140 the scope bp triggers, we notice that the watchpoint is out of
13141 scope, and delete it. We also delete its scope bp. But the
13142 scope bp is marked "auto-deleting", and is already on a bpstat.
13143 That bpstat is then checked for auto-deleting bp's, which are
13144 deleted.
13145
13146 A real solution to this problem might involve reference counts in
13147 bp's, and/or giving them pointers back to their referencing
13148 bpstat's, and teaching delete_breakpoint to only free a bp's
13149 storage when no more references were extent. A cheaper bandaid
13150 was chosen. */
13151 if (bpt->type == bp_none)
13152 return;
13153
13154 /* At least avoid this stale reference until the reference counting
13155 of breakpoints gets resolved. */
13156 if (bpt->related_breakpoint != bpt)
13157 {
13158 struct breakpoint *related;
13159 struct watchpoint *w;
13160
13161 if (bpt->type == bp_watchpoint_scope)
13162 w = (struct watchpoint *) bpt->related_breakpoint;
13163 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13164 w = (struct watchpoint *) bpt;
13165 else
13166 w = NULL;
13167 if (w != NULL)
13168 watchpoint_del_at_next_stop (w);
13169
13170 /* Unlink bpt from the bpt->related_breakpoint ring. */
13171 for (related = bpt; related->related_breakpoint != bpt;
13172 related = related->related_breakpoint);
13173 related->related_breakpoint = bpt->related_breakpoint;
13174 bpt->related_breakpoint = bpt;
13175 }
13176
13177 /* watch_command_1 creates a watchpoint but only sets its number if
13178 update_watchpoint succeeds in creating its bp_locations. If there's
13179 a problem in that process, we'll be asked to delete the half-created
13180 watchpoint. In that case, don't announce the deletion. */
13181 if (bpt->number)
13182 gdb::observers::breakpoint_deleted.notify (bpt);
13183
13184 if (breakpoint_chain == bpt)
13185 breakpoint_chain = bpt->next;
13186
13187 ALL_BREAKPOINTS (b)
13188 if (b->next == bpt)
13189 {
13190 b->next = bpt->next;
13191 break;
13192 }
13193
13194 /* Be sure no bpstat's are pointing at the breakpoint after it's
13195 been freed. */
13196 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13197 in all threads for now. Note that we cannot just remove bpstats
13198 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13199 commands are associated with the bpstat; if we remove it here,
13200 then the later call to bpstat_do_actions (&stop_bpstat); in
13201 event-top.c won't do anything, and temporary breakpoints with
13202 commands won't work. */
13203
13204 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13205
13206 /* Now that breakpoint is removed from breakpoint list, update the
13207 global location list. This will remove locations that used to
13208 belong to this breakpoint. Do this before freeing the breakpoint
13209 itself, since remove_breakpoint looks at location's owner. It
13210 might be better design to have location completely
13211 self-contained, but it's not the case now. */
13212 update_global_location_list (UGLL_DONT_INSERT);
13213
13214 /* On the chance that someone will soon try again to delete this
13215 same bp, we mark it as deleted before freeing its storage. */
13216 bpt->type = bp_none;
13217 delete bpt;
13218 }
13219
13220 /* Iterator function to call a user-provided callback function once
13221 for each of B and its related breakpoints. */
13222
13223 static void
13224 iterate_over_related_breakpoints (struct breakpoint *b,
13225 gdb::function_view<void (breakpoint *)> function)
13226 {
13227 struct breakpoint *related;
13228
13229 related = b;
13230 do
13231 {
13232 struct breakpoint *next;
13233
13234 /* FUNCTION may delete RELATED. */
13235 next = related->related_breakpoint;
13236
13237 if (next == related)
13238 {
13239 /* RELATED is the last ring entry. */
13240 function (related);
13241
13242 /* FUNCTION may have deleted it, so we'd never reach back to
13243 B. There's nothing left to do anyway, so just break
13244 out. */
13245 break;
13246 }
13247 else
13248 function (related);
13249
13250 related = next;
13251 }
13252 while (related != b);
13253 }
13254
13255 static void
13256 delete_command (const char *arg, int from_tty)
13257 {
13258 struct breakpoint *b, *b_tmp;
13259
13260 dont_repeat ();
13261
13262 if (arg == 0)
13263 {
13264 int breaks_to_delete = 0;
13265
13266 /* Delete all breakpoints if no argument. Do not delete
13267 internal breakpoints, these have to be deleted with an
13268 explicit breakpoint number argument. */
13269 ALL_BREAKPOINTS (b)
13270 if (user_breakpoint_p (b))
13271 {
13272 breaks_to_delete = 1;
13273 break;
13274 }
13275
13276 /* Ask user only if there are some breakpoints to delete. */
13277 if (!from_tty
13278 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13279 {
13280 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13281 if (user_breakpoint_p (b))
13282 delete_breakpoint (b);
13283 }
13284 }
13285 else
13286 map_breakpoint_numbers
13287 (arg, [&] (breakpoint *b)
13288 {
13289 iterate_over_related_breakpoints (b, delete_breakpoint);
13290 });
13291 }
13292
13293 /* Return true if all locations of B bound to PSPACE are pending. If
13294 PSPACE is NULL, all locations of all program spaces are
13295 considered. */
13296
13297 static int
13298 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13299 {
13300 struct bp_location *loc;
13301
13302 for (loc = b->loc; loc != NULL; loc = loc->next)
13303 if ((pspace == NULL
13304 || loc->pspace == pspace)
13305 && !loc->shlib_disabled
13306 && !loc->pspace->executing_startup)
13307 return 0;
13308 return 1;
13309 }
13310
13311 /* Subroutine of update_breakpoint_locations to simplify it.
13312 Return non-zero if multiple fns in list LOC have the same name.
13313 Null names are ignored. */
13314
13315 static int
13316 ambiguous_names_p (struct bp_location *loc)
13317 {
13318 struct bp_location *l;
13319 htab_t htab = htab_create_alloc (13, htab_hash_string, streq_hash, NULL,
13320 xcalloc, xfree);
13321
13322 for (l = loc; l != NULL; l = l->next)
13323 {
13324 const char **slot;
13325 const char *name = l->function_name;
13326
13327 /* Allow for some names to be NULL, ignore them. */
13328 if (name == NULL)
13329 continue;
13330
13331 slot = (const char **) htab_find_slot (htab, (const void *) name,
13332 INSERT);
13333 /* NOTE: We can assume slot != NULL here because xcalloc never
13334 returns NULL. */
13335 if (*slot != NULL)
13336 {
13337 htab_delete (htab);
13338 return 1;
13339 }
13340 *slot = name;
13341 }
13342
13343 htab_delete (htab);
13344 return 0;
13345 }
13346
13347 /* When symbols change, it probably means the sources changed as well,
13348 and it might mean the static tracepoint markers are no longer at
13349 the same address or line numbers they used to be at last we
13350 checked. Losing your static tracepoints whenever you rebuild is
13351 undesirable. This function tries to resync/rematch gdb static
13352 tracepoints with the markers on the target, for static tracepoints
13353 that have not been set by marker id. Static tracepoint that have
13354 been set by marker id are reset by marker id in breakpoint_re_set.
13355 The heuristic is:
13356
13357 1) For a tracepoint set at a specific address, look for a marker at
13358 the old PC. If one is found there, assume to be the same marker.
13359 If the name / string id of the marker found is different from the
13360 previous known name, assume that means the user renamed the marker
13361 in the sources, and output a warning.
13362
13363 2) For a tracepoint set at a given line number, look for a marker
13364 at the new address of the old line number. If one is found there,
13365 assume to be the same marker. If the name / string id of the
13366 marker found is different from the previous known name, assume that
13367 means the user renamed the marker in the sources, and output a
13368 warning.
13369
13370 3) If a marker is no longer found at the same address or line, it
13371 may mean the marker no longer exists. But it may also just mean
13372 the code changed a bit. Maybe the user added a few lines of code
13373 that made the marker move up or down (in line number terms). Ask
13374 the target for info about the marker with the string id as we knew
13375 it. If found, update line number and address in the matching
13376 static tracepoint. This will get confused if there's more than one
13377 marker with the same ID (possible in UST, although unadvised
13378 precisely because it confuses tools). */
13379
13380 static struct symtab_and_line
13381 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13382 {
13383 struct tracepoint *tp = (struct tracepoint *) b;
13384 struct static_tracepoint_marker marker;
13385 CORE_ADDR pc;
13386
13387 pc = sal.pc;
13388 if (sal.line)
13389 find_line_pc (sal.symtab, sal.line, &pc);
13390
13391 if (target_static_tracepoint_marker_at (pc, &marker))
13392 {
13393 if (tp->static_trace_marker_id != marker.str_id)
13394 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13395 b->number, tp->static_trace_marker_id.c_str (),
13396 marker.str_id.c_str ());
13397
13398 tp->static_trace_marker_id = std::move (marker.str_id);
13399
13400 return sal;
13401 }
13402
13403 /* Old marker wasn't found on target at lineno. Try looking it up
13404 by string ID. */
13405 if (!sal.explicit_pc
13406 && sal.line != 0
13407 && sal.symtab != NULL
13408 && !tp->static_trace_marker_id.empty ())
13409 {
13410 std::vector<static_tracepoint_marker> markers
13411 = target_static_tracepoint_markers_by_strid
13412 (tp->static_trace_marker_id.c_str ());
13413
13414 if (!markers.empty ())
13415 {
13416 struct symbol *sym;
13417 struct static_tracepoint_marker *tpmarker;
13418 struct ui_out *uiout = current_uiout;
13419 struct explicit_location explicit_loc;
13420
13421 tpmarker = &markers[0];
13422
13423 tp->static_trace_marker_id = std::move (tpmarker->str_id);
13424
13425 warning (_("marker for static tracepoint %d (%s) not "
13426 "found at previous line number"),
13427 b->number, tp->static_trace_marker_id.c_str ());
13428
13429 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13430 sym = find_pc_sect_function (tpmarker->address, NULL);
13431 uiout->text ("Now in ");
13432 if (sym)
13433 {
13434 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13435 uiout->text (" at ");
13436 }
13437 uiout->field_string ("file",
13438 symtab_to_filename_for_display (sal2.symtab));
13439 uiout->text (":");
13440
13441 if (uiout->is_mi_like_p ())
13442 {
13443 const char *fullname = symtab_to_fullname (sal2.symtab);
13444
13445 uiout->field_string ("fullname", fullname);
13446 }
13447
13448 uiout->field_int ("line", sal2.line);
13449 uiout->text ("\n");
13450
13451 b->loc->line_number = sal2.line;
13452 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13453
13454 b->location.reset (NULL);
13455 initialize_explicit_location (&explicit_loc);
13456 explicit_loc.source_filename
13457 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13458 explicit_loc.line_offset.offset = b->loc->line_number;
13459 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13460 b->location = new_explicit_location (&explicit_loc);
13461
13462 /* Might be nice to check if function changed, and warn if
13463 so. */
13464 }
13465 }
13466 return sal;
13467 }
13468
13469 /* Returns 1 iff locations A and B are sufficiently same that
13470 we don't need to report breakpoint as changed. */
13471
13472 static int
13473 locations_are_equal (struct bp_location *a, struct bp_location *b)
13474 {
13475 while (a && b)
13476 {
13477 if (a->address != b->address)
13478 return 0;
13479
13480 if (a->shlib_disabled != b->shlib_disabled)
13481 return 0;
13482
13483 if (a->enabled != b->enabled)
13484 return 0;
13485
13486 a = a->next;
13487 b = b->next;
13488 }
13489
13490 if ((a == NULL) != (b == NULL))
13491 return 0;
13492
13493 return 1;
13494 }
13495
13496 /* Split all locations of B that are bound to PSPACE out of B's
13497 location list to a separate list and return that list's head. If
13498 PSPACE is NULL, hoist out all locations of B. */
13499
13500 static struct bp_location *
13501 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13502 {
13503 struct bp_location head;
13504 struct bp_location *i = b->loc;
13505 struct bp_location **i_link = &b->loc;
13506 struct bp_location *hoisted = &head;
13507
13508 if (pspace == NULL)
13509 {
13510 i = b->loc;
13511 b->loc = NULL;
13512 return i;
13513 }
13514
13515 head.next = NULL;
13516
13517 while (i != NULL)
13518 {
13519 if (i->pspace == pspace)
13520 {
13521 *i_link = i->next;
13522 i->next = NULL;
13523 hoisted->next = i;
13524 hoisted = i;
13525 }
13526 else
13527 i_link = &i->next;
13528 i = *i_link;
13529 }
13530
13531 return head.next;
13532 }
13533
13534 /* Create new breakpoint locations for B (a hardware or software
13535 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13536 zero, then B is a ranged breakpoint. Only recreates locations for
13537 FILTER_PSPACE. Locations of other program spaces are left
13538 untouched. */
13539
13540 void
13541 update_breakpoint_locations (struct breakpoint *b,
13542 struct program_space *filter_pspace,
13543 gdb::array_view<const symtab_and_line> sals,
13544 gdb::array_view<const symtab_and_line> sals_end)
13545 {
13546 struct bp_location *existing_locations;
13547
13548 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
13549 {
13550 /* Ranged breakpoints have only one start location and one end
13551 location. */
13552 b->enable_state = bp_disabled;
13553 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13554 "multiple locations found\n"),
13555 b->number);
13556 return;
13557 }
13558
13559 /* If there's no new locations, and all existing locations are
13560 pending, don't do anything. This optimizes the common case where
13561 all locations are in the same shared library, that was unloaded.
13562 We'd like to retain the location, so that when the library is
13563 loaded again, we don't loose the enabled/disabled status of the
13564 individual locations. */
13565 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
13566 return;
13567
13568 existing_locations = hoist_existing_locations (b, filter_pspace);
13569
13570 for (const auto &sal : sals)
13571 {
13572 struct bp_location *new_loc;
13573
13574 switch_to_program_space_and_thread (sal.pspace);
13575
13576 new_loc = add_location_to_breakpoint (b, &sal);
13577
13578 /* Reparse conditions, they might contain references to the
13579 old symtab. */
13580 if (b->cond_string != NULL)
13581 {
13582 const char *s;
13583
13584 s = b->cond_string;
13585 TRY
13586 {
13587 new_loc->cond = parse_exp_1 (&s, sal.pc,
13588 block_for_pc (sal.pc),
13589 0);
13590 }
13591 CATCH (e, RETURN_MASK_ERROR)
13592 {
13593 warning (_("failed to reevaluate condition "
13594 "for breakpoint %d: %s"),
13595 b->number, e.message);
13596 new_loc->enabled = 0;
13597 }
13598 END_CATCH
13599 }
13600
13601 if (!sals_end.empty ())
13602 {
13603 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13604
13605 new_loc->length = end - sals[0].pc + 1;
13606 }
13607 }
13608
13609 /* If possible, carry over 'disable' status from existing
13610 breakpoints. */
13611 {
13612 struct bp_location *e = existing_locations;
13613 /* If there are multiple breakpoints with the same function name,
13614 e.g. for inline functions, comparing function names won't work.
13615 Instead compare pc addresses; this is just a heuristic as things
13616 may have moved, but in practice it gives the correct answer
13617 often enough until a better solution is found. */
13618 int have_ambiguous_names = ambiguous_names_p (b->loc);
13619
13620 for (; e; e = e->next)
13621 {
13622 if (!e->enabled && e->function_name)
13623 {
13624 struct bp_location *l = b->loc;
13625 if (have_ambiguous_names)
13626 {
13627 for (; l; l = l->next)
13628 if (breakpoint_locations_match (e, l))
13629 {
13630 l->enabled = 0;
13631 break;
13632 }
13633 }
13634 else
13635 {
13636 for (; l; l = l->next)
13637 if (l->function_name
13638 && strcmp (e->function_name, l->function_name) == 0)
13639 {
13640 l->enabled = 0;
13641 break;
13642 }
13643 }
13644 }
13645 }
13646 }
13647
13648 if (!locations_are_equal (existing_locations, b->loc))
13649 gdb::observers::breakpoint_modified.notify (b);
13650 }
13651
13652 /* Find the SaL locations corresponding to the given LOCATION.
13653 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13654
13655 static std::vector<symtab_and_line>
13656 location_to_sals (struct breakpoint *b, struct event_location *location,
13657 struct program_space *search_pspace, int *found)
13658 {
13659 struct gdb_exception exception = exception_none;
13660
13661 gdb_assert (b->ops != NULL);
13662
13663 std::vector<symtab_and_line> sals;
13664
13665 TRY
13666 {
13667 sals = b->ops->decode_location (b, location, search_pspace);
13668 }
13669 CATCH (e, RETURN_MASK_ERROR)
13670 {
13671 int not_found_and_ok = 0;
13672
13673 exception = e;
13674
13675 /* For pending breakpoints, it's expected that parsing will
13676 fail until the right shared library is loaded. User has
13677 already told to create pending breakpoints and don't need
13678 extra messages. If breakpoint is in bp_shlib_disabled
13679 state, then user already saw the message about that
13680 breakpoint being disabled, and don't want to see more
13681 errors. */
13682 if (e.error == NOT_FOUND_ERROR
13683 && (b->condition_not_parsed
13684 || (b->loc != NULL
13685 && search_pspace != NULL
13686 && b->loc->pspace != search_pspace)
13687 || (b->loc && b->loc->shlib_disabled)
13688 || (b->loc && b->loc->pspace->executing_startup)
13689 || b->enable_state == bp_disabled))
13690 not_found_and_ok = 1;
13691
13692 if (!not_found_and_ok)
13693 {
13694 /* We surely don't want to warn about the same breakpoint
13695 10 times. One solution, implemented here, is disable
13696 the breakpoint on error. Another solution would be to
13697 have separate 'warning emitted' flag. Since this
13698 happens only when a binary has changed, I don't know
13699 which approach is better. */
13700 b->enable_state = bp_disabled;
13701 throw_exception (e);
13702 }
13703 }
13704 END_CATCH
13705
13706 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13707 {
13708 for (auto &sal : sals)
13709 resolve_sal_pc (&sal);
13710 if (b->condition_not_parsed && b->extra_string != NULL)
13711 {
13712 char *cond_string, *extra_string;
13713 int thread, task;
13714
13715 find_condition_and_thread (b->extra_string, sals[0].pc,
13716 &cond_string, &thread, &task,
13717 &extra_string);
13718 gdb_assert (b->cond_string == NULL);
13719 if (cond_string)
13720 b->cond_string = cond_string;
13721 b->thread = thread;
13722 b->task = task;
13723 if (extra_string)
13724 {
13725 xfree (b->extra_string);
13726 b->extra_string = extra_string;
13727 }
13728 b->condition_not_parsed = 0;
13729 }
13730
13731 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13732 sals[0] = update_static_tracepoint (b, sals[0]);
13733
13734 *found = 1;
13735 }
13736 else
13737 *found = 0;
13738
13739 return sals;
13740 }
13741
13742 /* The default re_set method, for typical hardware or software
13743 breakpoints. Reevaluate the breakpoint and recreate its
13744 locations. */
13745
13746 static void
13747 breakpoint_re_set_default (struct breakpoint *b)
13748 {
13749 struct program_space *filter_pspace = current_program_space;
13750 std::vector<symtab_and_line> expanded, expanded_end;
13751
13752 int found;
13753 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13754 filter_pspace, &found);
13755 if (found)
13756 expanded = std::move (sals);
13757
13758 if (b->location_range_end != NULL)
13759 {
13760 std::vector<symtab_and_line> sals_end
13761 = location_to_sals (b, b->location_range_end.get (),
13762 filter_pspace, &found);
13763 if (found)
13764 expanded_end = std::move (sals_end);
13765 }
13766
13767 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13768 }
13769
13770 /* Default method for creating SALs from an address string. It basically
13771 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13772
13773 static void
13774 create_sals_from_location_default (const struct event_location *location,
13775 struct linespec_result *canonical,
13776 enum bptype type_wanted)
13777 {
13778 parse_breakpoint_sals (location, canonical);
13779 }
13780
13781 /* Call create_breakpoints_sal for the given arguments. This is the default
13782 function for the `create_breakpoints_sal' method of
13783 breakpoint_ops. */
13784
13785 static void
13786 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13787 struct linespec_result *canonical,
13788 gdb::unique_xmalloc_ptr<char> cond_string,
13789 gdb::unique_xmalloc_ptr<char> extra_string,
13790 enum bptype type_wanted,
13791 enum bpdisp disposition,
13792 int thread,
13793 int task, int ignore_count,
13794 const struct breakpoint_ops *ops,
13795 int from_tty, int enabled,
13796 int internal, unsigned flags)
13797 {
13798 create_breakpoints_sal (gdbarch, canonical,
13799 std::move (cond_string),
13800 std::move (extra_string),
13801 type_wanted, disposition,
13802 thread, task, ignore_count, ops, from_tty,
13803 enabled, internal, flags);
13804 }
13805
13806 /* Decode the line represented by S by calling decode_line_full. This is the
13807 default function for the `decode_location' method of breakpoint_ops. */
13808
13809 static std::vector<symtab_and_line>
13810 decode_location_default (struct breakpoint *b,
13811 const struct event_location *location,
13812 struct program_space *search_pspace)
13813 {
13814 struct linespec_result canonical;
13815
13816 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13817 (struct symtab *) NULL, 0,
13818 &canonical, multiple_symbols_all,
13819 b->filter);
13820
13821 /* We should get 0 or 1 resulting SALs. */
13822 gdb_assert (canonical.lsals.size () < 2);
13823
13824 if (!canonical.lsals.empty ())
13825 {
13826 const linespec_sals &lsal = canonical.lsals[0];
13827 return std::move (lsal.sals);
13828 }
13829 return {};
13830 }
13831
13832 /* Reset a breakpoint. */
13833
13834 static void
13835 breakpoint_re_set_one (breakpoint *b)
13836 {
13837 input_radix = b->input_radix;
13838 set_language (b->language);
13839
13840 b->ops->re_set (b);
13841 }
13842
13843 /* Re-set breakpoint locations for the current program space.
13844 Locations bound to other program spaces are left untouched. */
13845
13846 void
13847 breakpoint_re_set (void)
13848 {
13849 struct breakpoint *b, *b_tmp;
13850
13851 {
13852 scoped_restore_current_language save_language;
13853 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13854 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13855
13856 /* breakpoint_re_set_one sets the current_language to the language
13857 of the breakpoint it is resetting (see prepare_re_set_context)
13858 before re-evaluating the breakpoint's location. This change can
13859 unfortunately get undone by accident if the language_mode is set
13860 to auto, and we either switch frames, or more likely in this context,
13861 we select the current frame.
13862
13863 We prevent this by temporarily turning the language_mode to
13864 language_mode_manual. We restore it once all breakpoints
13865 have been reset. */
13866 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13867 language_mode = language_mode_manual;
13868
13869 /* Note: we must not try to insert locations until after all
13870 breakpoints have been re-set. Otherwise, e.g., when re-setting
13871 breakpoint 1, we'd insert the locations of breakpoint 2, which
13872 hadn't been re-set yet, and thus may have stale locations. */
13873
13874 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13875 {
13876 TRY
13877 {
13878 breakpoint_re_set_one (b);
13879 }
13880 CATCH (ex, RETURN_MASK_ALL)
13881 {
13882 exception_fprintf (gdb_stderr, ex,
13883 "Error in re-setting breakpoint %d: ",
13884 b->number);
13885 }
13886 END_CATCH
13887 }
13888
13889 jit_breakpoint_re_set ();
13890 }
13891
13892 create_overlay_event_breakpoint ();
13893 create_longjmp_master_breakpoint ();
13894 create_std_terminate_master_breakpoint ();
13895 create_exception_master_breakpoint ();
13896
13897 /* Now we can insert. */
13898 update_global_location_list (UGLL_MAY_INSERT);
13899 }
13900 \f
13901 /* Reset the thread number of this breakpoint:
13902
13903 - If the breakpoint is for all threads, leave it as-is.
13904 - Else, reset it to the current thread for inferior_ptid. */
13905 void
13906 breakpoint_re_set_thread (struct breakpoint *b)
13907 {
13908 if (b->thread != -1)
13909 {
13910 b->thread = inferior_thread ()->global_num;
13911
13912 /* We're being called after following a fork. The new fork is
13913 selected as current, and unless this was a vfork will have a
13914 different program space from the original thread. Reset that
13915 as well. */
13916 b->loc->pspace = current_program_space;
13917 }
13918 }
13919
13920 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13921 If from_tty is nonzero, it prints a message to that effect,
13922 which ends with a period (no newline). */
13923
13924 void
13925 set_ignore_count (int bptnum, int count, int from_tty)
13926 {
13927 struct breakpoint *b;
13928
13929 if (count < 0)
13930 count = 0;
13931
13932 ALL_BREAKPOINTS (b)
13933 if (b->number == bptnum)
13934 {
13935 if (is_tracepoint (b))
13936 {
13937 if (from_tty && count != 0)
13938 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13939 bptnum);
13940 return;
13941 }
13942
13943 b->ignore_count = count;
13944 if (from_tty)
13945 {
13946 if (count == 0)
13947 printf_filtered (_("Will stop next time "
13948 "breakpoint %d is reached."),
13949 bptnum);
13950 else if (count == 1)
13951 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13952 bptnum);
13953 else
13954 printf_filtered (_("Will ignore next %d "
13955 "crossings of breakpoint %d."),
13956 count, bptnum);
13957 }
13958 gdb::observers::breakpoint_modified.notify (b);
13959 return;
13960 }
13961
13962 error (_("No breakpoint number %d."), bptnum);
13963 }
13964
13965 /* Command to set ignore-count of breakpoint N to COUNT. */
13966
13967 static void
13968 ignore_command (const char *args, int from_tty)
13969 {
13970 const char *p = args;
13971 int num;
13972
13973 if (p == 0)
13974 error_no_arg (_("a breakpoint number"));
13975
13976 num = get_number (&p);
13977 if (num == 0)
13978 error (_("bad breakpoint number: '%s'"), args);
13979 if (*p == 0)
13980 error (_("Second argument (specified ignore-count) is missing."));
13981
13982 set_ignore_count (num,
13983 longest_to_int (value_as_long (parse_and_eval (p))),
13984 from_tty);
13985 if (from_tty)
13986 printf_filtered ("\n");
13987 }
13988 \f
13989
13990 /* Call FUNCTION on each of the breakpoints with numbers in the range
13991 defined by BP_NUM_RANGE (an inclusive range). */
13992
13993 static void
13994 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13995 gdb::function_view<void (breakpoint *)> function)
13996 {
13997 if (bp_num_range.first == 0)
13998 {
13999 warning (_("bad breakpoint number at or near '%d'"),
14000 bp_num_range.first);
14001 }
14002 else
14003 {
14004 struct breakpoint *b, *tmp;
14005
14006 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
14007 {
14008 bool match = false;
14009
14010 ALL_BREAKPOINTS_SAFE (b, tmp)
14011 if (b->number == i)
14012 {
14013 match = true;
14014 function (b);
14015 break;
14016 }
14017 if (!match)
14018 printf_unfiltered (_("No breakpoint number %d.\n"), i);
14019 }
14020 }
14021 }
14022
14023 /* Call FUNCTION on each of the breakpoints whose numbers are given in
14024 ARGS. */
14025
14026 static void
14027 map_breakpoint_numbers (const char *args,
14028 gdb::function_view<void (breakpoint *)> function)
14029 {
14030 if (args == NULL || *args == '\0')
14031 error_no_arg (_("one or more breakpoint numbers"));
14032
14033 number_or_range_parser parser (args);
14034
14035 while (!parser.finished ())
14036 {
14037 int num = parser.get_number ();
14038 map_breakpoint_number_range (std::make_pair (num, num), function);
14039 }
14040 }
14041
14042 /* Return the breakpoint location structure corresponding to the
14043 BP_NUM and LOC_NUM values. */
14044
14045 static struct bp_location *
14046 find_location_by_number (int bp_num, int loc_num)
14047 {
14048 struct breakpoint *b;
14049
14050 ALL_BREAKPOINTS (b)
14051 if (b->number == bp_num)
14052 {
14053 break;
14054 }
14055
14056 if (!b || b->number != bp_num)
14057 error (_("Bad breakpoint number '%d'"), bp_num);
14058
14059 if (loc_num == 0)
14060 error (_("Bad breakpoint location number '%d'"), loc_num);
14061
14062 int n = 0;
14063 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14064 if (++n == loc_num)
14065 return loc;
14066
14067 error (_("Bad breakpoint location number '%d'"), loc_num);
14068 }
14069
14070 /* Modes of operation for extract_bp_num. */
14071 enum class extract_bp_kind
14072 {
14073 /* Extracting a breakpoint number. */
14074 bp,
14075
14076 /* Extracting a location number. */
14077 loc,
14078 };
14079
14080 /* Extract a breakpoint or location number (as determined by KIND)
14081 from the string starting at START. TRAILER is a character which
14082 can be found after the number. If you don't want a trailer, use
14083 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14084 string. This always returns a positive integer. */
14085
14086 static int
14087 extract_bp_num (extract_bp_kind kind, const char *start,
14088 int trailer, const char **end_out = NULL)
14089 {
14090 const char *end = start;
14091 int num = get_number_trailer (&end, trailer);
14092 if (num < 0)
14093 error (kind == extract_bp_kind::bp
14094 ? _("Negative breakpoint number '%.*s'")
14095 : _("Negative breakpoint location number '%.*s'"),
14096 int (end - start), start);
14097 if (num == 0)
14098 error (kind == extract_bp_kind::bp
14099 ? _("Bad breakpoint number '%.*s'")
14100 : _("Bad breakpoint location number '%.*s'"),
14101 int (end - start), start);
14102
14103 if (end_out != NULL)
14104 *end_out = end;
14105 return num;
14106 }
14107
14108 /* Extract a breakpoint or location range (as determined by KIND) in
14109 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14110 representing the (inclusive) range. The returned pair's elements
14111 are always positive integers. */
14112
14113 static std::pair<int, int>
14114 extract_bp_or_bp_range (extract_bp_kind kind,
14115 const std::string &arg,
14116 std::string::size_type arg_offset)
14117 {
14118 std::pair<int, int> range;
14119 const char *bp_loc = &arg[arg_offset];
14120 std::string::size_type dash = arg.find ('-', arg_offset);
14121 if (dash != std::string::npos)
14122 {
14123 /* bp_loc is a range (x-z). */
14124 if (arg.length () == dash + 1)
14125 error (kind == extract_bp_kind::bp
14126 ? _("Bad breakpoint number at or near: '%s'")
14127 : _("Bad breakpoint location number at or near: '%s'"),
14128 bp_loc);
14129
14130 const char *end;
14131 const char *start_first = bp_loc;
14132 const char *start_second = &arg[dash + 1];
14133 range.first = extract_bp_num (kind, start_first, '-');
14134 range.second = extract_bp_num (kind, start_second, '\0', &end);
14135
14136 if (range.first > range.second)
14137 error (kind == extract_bp_kind::bp
14138 ? _("Inverted breakpoint range at '%.*s'")
14139 : _("Inverted breakpoint location range at '%.*s'"),
14140 int (end - start_first), start_first);
14141 }
14142 else
14143 {
14144 /* bp_loc is a single value. */
14145 range.first = extract_bp_num (kind, bp_loc, '\0');
14146 range.second = range.first;
14147 }
14148 return range;
14149 }
14150
14151 /* Extract the breakpoint/location range specified by ARG. Returns
14152 the breakpoint range in BP_NUM_RANGE, and the location range in
14153 BP_LOC_RANGE.
14154
14155 ARG may be in any of the following forms:
14156
14157 x where 'x' is a breakpoint number.
14158 x-y where 'x' and 'y' specify a breakpoint numbers range.
14159 x.y where 'x' is a breakpoint number and 'y' a location number.
14160 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14161 location number range.
14162 */
14163
14164 static void
14165 extract_bp_number_and_location (const std::string &arg,
14166 std::pair<int, int> &bp_num_range,
14167 std::pair<int, int> &bp_loc_range)
14168 {
14169 std::string::size_type dot = arg.find ('.');
14170
14171 if (dot != std::string::npos)
14172 {
14173 /* Handle 'x.y' and 'x.y-z' cases. */
14174
14175 if (arg.length () == dot + 1 || dot == 0)
14176 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
14177
14178 bp_num_range.first
14179 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14180 bp_num_range.second = bp_num_range.first;
14181
14182 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14183 arg, dot + 1);
14184 }
14185 else
14186 {
14187 /* Handle x and x-y cases. */
14188
14189 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
14190 bp_loc_range.first = 0;
14191 bp_loc_range.second = 0;
14192 }
14193 }
14194
14195 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14196 specifies whether to enable or disable. */
14197
14198 static void
14199 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14200 {
14201 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14202 if (loc != NULL)
14203 {
14204 if (loc->enabled != enable)
14205 {
14206 loc->enabled = enable;
14207 mark_breakpoint_location_modified (loc);
14208 }
14209 if (target_supports_enable_disable_tracepoint ()
14210 && current_trace_status ()->running && loc->owner
14211 && is_tracepoint (loc->owner))
14212 target_disable_tracepoint (loc);
14213 }
14214 update_global_location_list (UGLL_DONT_INSERT);
14215
14216 gdb::observers::breakpoint_modified.notify (loc->owner);
14217 }
14218
14219 /* Enable or disable a range of breakpoint locations. BP_NUM is the
14220 number of the breakpoint, and BP_LOC_RANGE specifies the
14221 (inclusive) range of location numbers of that breakpoint to
14222 enable/disable. ENABLE specifies whether to enable or disable the
14223 location. */
14224
14225 static void
14226 enable_disable_breakpoint_location_range (int bp_num,
14227 std::pair<int, int> &bp_loc_range,
14228 bool enable)
14229 {
14230 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14231 enable_disable_bp_num_loc (bp_num, i, enable);
14232 }
14233
14234 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14235 If from_tty is nonzero, it prints a message to that effect,
14236 which ends with a period (no newline). */
14237
14238 void
14239 disable_breakpoint (struct breakpoint *bpt)
14240 {
14241 /* Never disable a watchpoint scope breakpoint; we want to
14242 hit them when we leave scope so we can delete both the
14243 watchpoint and its scope breakpoint at that time. */
14244 if (bpt->type == bp_watchpoint_scope)
14245 return;
14246
14247 bpt->enable_state = bp_disabled;
14248
14249 /* Mark breakpoint locations modified. */
14250 mark_breakpoint_modified (bpt);
14251
14252 if (target_supports_enable_disable_tracepoint ()
14253 && current_trace_status ()->running && is_tracepoint (bpt))
14254 {
14255 struct bp_location *location;
14256
14257 for (location = bpt->loc; location; location = location->next)
14258 target_disable_tracepoint (location);
14259 }
14260
14261 update_global_location_list (UGLL_DONT_INSERT);
14262
14263 gdb::observers::breakpoint_modified.notify (bpt);
14264 }
14265
14266 /* Enable or disable the breakpoint(s) or breakpoint location(s)
14267 specified in ARGS. ARGS may be in any of the formats handled by
14268 extract_bp_number_and_location. ENABLE specifies whether to enable
14269 or disable the breakpoints/locations. */
14270
14271 static void
14272 enable_disable_command (const char *args, int from_tty, bool enable)
14273 {
14274 if (args == 0)
14275 {
14276 struct breakpoint *bpt;
14277
14278 ALL_BREAKPOINTS (bpt)
14279 if (user_breakpoint_p (bpt))
14280 {
14281 if (enable)
14282 enable_breakpoint (bpt);
14283 else
14284 disable_breakpoint (bpt);
14285 }
14286 }
14287 else
14288 {
14289 std::string num = extract_arg (&args);
14290
14291 while (!num.empty ())
14292 {
14293 std::pair<int, int> bp_num_range, bp_loc_range;
14294
14295 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14296
14297 if (bp_loc_range.first == bp_loc_range.second
14298 && bp_loc_range.first == 0)
14299 {
14300 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14301 map_breakpoint_number_range (bp_num_range,
14302 enable
14303 ? enable_breakpoint
14304 : disable_breakpoint);
14305 }
14306 else
14307 {
14308 /* Handle breakpoint ids with formats 'x.y' or
14309 'x.y-z'. */
14310 enable_disable_breakpoint_location_range
14311 (bp_num_range.first, bp_loc_range, enable);
14312 }
14313 num = extract_arg (&args);
14314 }
14315 }
14316 }
14317
14318 /* The disable command disables the specified breakpoints/locations
14319 (or all defined breakpoints) so they're no longer effective in
14320 stopping the inferior. ARGS may be in any of the forms defined in
14321 extract_bp_number_and_location. */
14322
14323 static void
14324 disable_command (const char *args, int from_tty)
14325 {
14326 enable_disable_command (args, from_tty, false);
14327 }
14328
14329 static void
14330 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14331 int count)
14332 {
14333 int target_resources_ok;
14334
14335 if (bpt->type == bp_hardware_breakpoint)
14336 {
14337 int i;
14338 i = hw_breakpoint_used_count ();
14339 target_resources_ok =
14340 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14341 i + 1, 0);
14342 if (target_resources_ok == 0)
14343 error (_("No hardware breakpoint support in the target."));
14344 else if (target_resources_ok < 0)
14345 error (_("Hardware breakpoints used exceeds limit."));
14346 }
14347
14348 if (is_watchpoint (bpt))
14349 {
14350 /* Initialize it just to avoid a GCC false warning. */
14351 enum enable_state orig_enable_state = bp_disabled;
14352
14353 TRY
14354 {
14355 struct watchpoint *w = (struct watchpoint *) bpt;
14356
14357 orig_enable_state = bpt->enable_state;
14358 bpt->enable_state = bp_enabled;
14359 update_watchpoint (w, 1 /* reparse */);
14360 }
14361 CATCH (e, RETURN_MASK_ALL)
14362 {
14363 bpt->enable_state = orig_enable_state;
14364 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14365 bpt->number);
14366 return;
14367 }
14368 END_CATCH
14369 }
14370
14371 bpt->enable_state = bp_enabled;
14372
14373 /* Mark breakpoint locations modified. */
14374 mark_breakpoint_modified (bpt);
14375
14376 if (target_supports_enable_disable_tracepoint ()
14377 && current_trace_status ()->running && is_tracepoint (bpt))
14378 {
14379 struct bp_location *location;
14380
14381 for (location = bpt->loc; location; location = location->next)
14382 target_enable_tracepoint (location);
14383 }
14384
14385 bpt->disposition = disposition;
14386 bpt->enable_count = count;
14387 update_global_location_list (UGLL_MAY_INSERT);
14388
14389 gdb::observers::breakpoint_modified.notify (bpt);
14390 }
14391
14392
14393 void
14394 enable_breakpoint (struct breakpoint *bpt)
14395 {
14396 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14397 }
14398
14399 /* The enable command enables the specified breakpoints/locations (or
14400 all defined breakpoints) so they once again become (or continue to
14401 be) effective in stopping the inferior. ARGS may be in any of the
14402 forms defined in extract_bp_number_and_location. */
14403
14404 static void
14405 enable_command (const char *args, int from_tty)
14406 {
14407 enable_disable_command (args, from_tty, true);
14408 }
14409
14410 static void
14411 enable_once_command (const char *args, int from_tty)
14412 {
14413 map_breakpoint_numbers
14414 (args, [&] (breakpoint *b)
14415 {
14416 iterate_over_related_breakpoints
14417 (b, [&] (breakpoint *bpt)
14418 {
14419 enable_breakpoint_disp (bpt, disp_disable, 1);
14420 });
14421 });
14422 }
14423
14424 static void
14425 enable_count_command (const char *args, int from_tty)
14426 {
14427 int count;
14428
14429 if (args == NULL)
14430 error_no_arg (_("hit count"));
14431
14432 count = get_number (&args);
14433
14434 map_breakpoint_numbers
14435 (args, [&] (breakpoint *b)
14436 {
14437 iterate_over_related_breakpoints
14438 (b, [&] (breakpoint *bpt)
14439 {
14440 enable_breakpoint_disp (bpt, disp_disable, count);
14441 });
14442 });
14443 }
14444
14445 static void
14446 enable_delete_command (const char *args, int from_tty)
14447 {
14448 map_breakpoint_numbers
14449 (args, [&] (breakpoint *b)
14450 {
14451 iterate_over_related_breakpoints
14452 (b, [&] (breakpoint *bpt)
14453 {
14454 enable_breakpoint_disp (bpt, disp_del, 1);
14455 });
14456 });
14457 }
14458 \f
14459 static void
14460 set_breakpoint_cmd (const char *args, int from_tty)
14461 {
14462 }
14463
14464 static void
14465 show_breakpoint_cmd (const char *args, int from_tty)
14466 {
14467 }
14468
14469 /* Invalidate last known value of any hardware watchpoint if
14470 the memory which that value represents has been written to by
14471 GDB itself. */
14472
14473 static void
14474 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14475 CORE_ADDR addr, ssize_t len,
14476 const bfd_byte *data)
14477 {
14478 struct breakpoint *bp;
14479
14480 ALL_BREAKPOINTS (bp)
14481 if (bp->enable_state == bp_enabled
14482 && bp->type == bp_hardware_watchpoint)
14483 {
14484 struct watchpoint *wp = (struct watchpoint *) bp;
14485
14486 if (wp->val_valid && wp->val != nullptr)
14487 {
14488 struct bp_location *loc;
14489
14490 for (loc = bp->loc; loc != NULL; loc = loc->next)
14491 if (loc->loc_type == bp_loc_hardware_watchpoint
14492 && loc->address + loc->length > addr
14493 && addr + len > loc->address)
14494 {
14495 wp->val = NULL;
14496 wp->val_valid = 0;
14497 }
14498 }
14499 }
14500 }
14501
14502 /* Create and insert a breakpoint for software single step. */
14503
14504 void
14505 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14506 const address_space *aspace,
14507 CORE_ADDR next_pc)
14508 {
14509 struct thread_info *tp = inferior_thread ();
14510 struct symtab_and_line sal;
14511 CORE_ADDR pc = next_pc;
14512
14513 if (tp->control.single_step_breakpoints == NULL)
14514 {
14515 tp->control.single_step_breakpoints
14516 = new_single_step_breakpoint (tp->global_num, gdbarch);
14517 }
14518
14519 sal = find_pc_line (pc, 0);
14520 sal.pc = pc;
14521 sal.section = find_pc_overlay (pc);
14522 sal.explicit_pc = 1;
14523 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14524
14525 update_global_location_list (UGLL_INSERT);
14526 }
14527
14528 /* Insert single step breakpoints according to the current state. */
14529
14530 int
14531 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14532 {
14533 struct regcache *regcache = get_current_regcache ();
14534 std::vector<CORE_ADDR> next_pcs;
14535
14536 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14537
14538 if (!next_pcs.empty ())
14539 {
14540 struct frame_info *frame = get_current_frame ();
14541 const address_space *aspace = get_frame_address_space (frame);
14542
14543 for (CORE_ADDR pc : next_pcs)
14544 insert_single_step_breakpoint (gdbarch, aspace, pc);
14545
14546 return 1;
14547 }
14548 else
14549 return 0;
14550 }
14551
14552 /* See breakpoint.h. */
14553
14554 int
14555 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14556 const address_space *aspace,
14557 CORE_ADDR pc)
14558 {
14559 struct bp_location *loc;
14560
14561 for (loc = bp->loc; loc != NULL; loc = loc->next)
14562 if (loc->inserted
14563 && breakpoint_location_address_match (loc, aspace, pc))
14564 return 1;
14565
14566 return 0;
14567 }
14568
14569 /* Check whether a software single-step breakpoint is inserted at
14570 PC. */
14571
14572 int
14573 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14574 CORE_ADDR pc)
14575 {
14576 struct breakpoint *bpt;
14577
14578 ALL_BREAKPOINTS (bpt)
14579 {
14580 if (bpt->type == bp_single_step
14581 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14582 return 1;
14583 }
14584 return 0;
14585 }
14586
14587 /* Tracepoint-specific operations. */
14588
14589 /* Set tracepoint count to NUM. */
14590 static void
14591 set_tracepoint_count (int num)
14592 {
14593 tracepoint_count = num;
14594 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14595 }
14596
14597 static void
14598 trace_command (const char *arg, int from_tty)
14599 {
14600 struct breakpoint_ops *ops;
14601
14602 event_location_up location = string_to_event_location (&arg,
14603 current_language);
14604 if (location != NULL
14605 && event_location_type (location.get ()) == PROBE_LOCATION)
14606 ops = &tracepoint_probe_breakpoint_ops;
14607 else
14608 ops = &tracepoint_breakpoint_ops;
14609
14610 create_breakpoint (get_current_arch (),
14611 location.get (),
14612 NULL, 0, arg, 1 /* parse arg */,
14613 0 /* tempflag */,
14614 bp_tracepoint /* type_wanted */,
14615 0 /* Ignore count */,
14616 pending_break_support,
14617 ops,
14618 from_tty,
14619 1 /* enabled */,
14620 0 /* internal */, 0);
14621 }
14622
14623 static void
14624 ftrace_command (const char *arg, int from_tty)
14625 {
14626 event_location_up location = string_to_event_location (&arg,
14627 current_language);
14628 create_breakpoint (get_current_arch (),
14629 location.get (),
14630 NULL, 0, arg, 1 /* parse arg */,
14631 0 /* tempflag */,
14632 bp_fast_tracepoint /* type_wanted */,
14633 0 /* Ignore count */,
14634 pending_break_support,
14635 &tracepoint_breakpoint_ops,
14636 from_tty,
14637 1 /* enabled */,
14638 0 /* internal */, 0);
14639 }
14640
14641 /* strace command implementation. Creates a static tracepoint. */
14642
14643 static void
14644 strace_command (const char *arg, int from_tty)
14645 {
14646 struct breakpoint_ops *ops;
14647 event_location_up location;
14648
14649 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14650 or with a normal static tracepoint. */
14651 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14652 {
14653 ops = &strace_marker_breakpoint_ops;
14654 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
14655 }
14656 else
14657 {
14658 ops = &tracepoint_breakpoint_ops;
14659 location = string_to_event_location (&arg, current_language);
14660 }
14661
14662 create_breakpoint (get_current_arch (),
14663 location.get (),
14664 NULL, 0, arg, 1 /* parse arg */,
14665 0 /* tempflag */,
14666 bp_static_tracepoint /* type_wanted */,
14667 0 /* Ignore count */,
14668 pending_break_support,
14669 ops,
14670 from_tty,
14671 1 /* enabled */,
14672 0 /* internal */, 0);
14673 }
14674
14675 /* Set up a fake reader function that gets command lines from a linked
14676 list that was acquired during tracepoint uploading. */
14677
14678 static struct uploaded_tp *this_utp;
14679 static int next_cmd;
14680
14681 static char *
14682 read_uploaded_action (void)
14683 {
14684 char *rslt = nullptr;
14685
14686 if (next_cmd < this_utp->cmd_strings.size ())
14687 {
14688 rslt = this_utp->cmd_strings[next_cmd];
14689 next_cmd++;
14690 }
14691
14692 return rslt;
14693 }
14694
14695 /* Given information about a tracepoint as recorded on a target (which
14696 can be either a live system or a trace file), attempt to create an
14697 equivalent GDB tracepoint. This is not a reliable process, since
14698 the target does not necessarily have all the information used when
14699 the tracepoint was originally defined. */
14700
14701 struct tracepoint *
14702 create_tracepoint_from_upload (struct uploaded_tp *utp)
14703 {
14704 const char *addr_str;
14705 char small_buf[100];
14706 struct tracepoint *tp;
14707
14708 if (utp->at_string)
14709 addr_str = utp->at_string;
14710 else
14711 {
14712 /* In the absence of a source location, fall back to raw
14713 address. Since there is no way to confirm that the address
14714 means the same thing as when the trace was started, warn the
14715 user. */
14716 warning (_("Uploaded tracepoint %d has no "
14717 "source location, using raw address"),
14718 utp->number);
14719 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14720 addr_str = small_buf;
14721 }
14722
14723 /* There's not much we can do with a sequence of bytecodes. */
14724 if (utp->cond && !utp->cond_string)
14725 warning (_("Uploaded tracepoint %d condition "
14726 "has no source form, ignoring it"),
14727 utp->number);
14728
14729 event_location_up location = string_to_event_location (&addr_str,
14730 current_language);
14731 if (!create_breakpoint (get_current_arch (),
14732 location.get (),
14733 utp->cond_string, -1, addr_str,
14734 0 /* parse cond/thread */,
14735 0 /* tempflag */,
14736 utp->type /* type_wanted */,
14737 0 /* Ignore count */,
14738 pending_break_support,
14739 &tracepoint_breakpoint_ops,
14740 0 /* from_tty */,
14741 utp->enabled /* enabled */,
14742 0 /* internal */,
14743 CREATE_BREAKPOINT_FLAGS_INSERTED))
14744 return NULL;
14745
14746 /* Get the tracepoint we just created. */
14747 tp = get_tracepoint (tracepoint_count);
14748 gdb_assert (tp != NULL);
14749
14750 if (utp->pass > 0)
14751 {
14752 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14753 tp->number);
14754
14755 trace_pass_command (small_buf, 0);
14756 }
14757
14758 /* If we have uploaded versions of the original commands, set up a
14759 special-purpose "reader" function and call the usual command line
14760 reader, then pass the result to the breakpoint command-setting
14761 function. */
14762 if (!utp->cmd_strings.empty ())
14763 {
14764 counted_command_line cmd_list;
14765
14766 this_utp = utp;
14767 next_cmd = 0;
14768
14769 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14770
14771 breakpoint_set_commands (tp, std::move (cmd_list));
14772 }
14773 else if (!utp->actions.empty ()
14774 || !utp->step_actions.empty ())
14775 warning (_("Uploaded tracepoint %d actions "
14776 "have no source form, ignoring them"),
14777 utp->number);
14778
14779 /* Copy any status information that might be available. */
14780 tp->hit_count = utp->hit_count;
14781 tp->traceframe_usage = utp->traceframe_usage;
14782
14783 return tp;
14784 }
14785
14786 /* Print information on tracepoint number TPNUM_EXP, or all if
14787 omitted. */
14788
14789 static void
14790 info_tracepoints_command (const char *args, int from_tty)
14791 {
14792 struct ui_out *uiout = current_uiout;
14793 int num_printed;
14794
14795 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14796
14797 if (num_printed == 0)
14798 {
14799 if (args == NULL || *args == '\0')
14800 uiout->message ("No tracepoints.\n");
14801 else
14802 uiout->message ("No tracepoint matching '%s'.\n", args);
14803 }
14804
14805 default_collect_info ();
14806 }
14807
14808 /* The 'enable trace' command enables tracepoints.
14809 Not supported by all targets. */
14810 static void
14811 enable_trace_command (const char *args, int from_tty)
14812 {
14813 enable_command (args, from_tty);
14814 }
14815
14816 /* The 'disable trace' command disables tracepoints.
14817 Not supported by all targets. */
14818 static void
14819 disable_trace_command (const char *args, int from_tty)
14820 {
14821 disable_command (args, from_tty);
14822 }
14823
14824 /* Remove a tracepoint (or all if no argument). */
14825 static void
14826 delete_trace_command (const char *arg, int from_tty)
14827 {
14828 struct breakpoint *b, *b_tmp;
14829
14830 dont_repeat ();
14831
14832 if (arg == 0)
14833 {
14834 int breaks_to_delete = 0;
14835
14836 /* Delete all breakpoints if no argument.
14837 Do not delete internal or call-dummy breakpoints, these
14838 have to be deleted with an explicit breakpoint number
14839 argument. */
14840 ALL_TRACEPOINTS (b)
14841 if (is_tracepoint (b) && user_breakpoint_p (b))
14842 {
14843 breaks_to_delete = 1;
14844 break;
14845 }
14846
14847 /* Ask user only if there are some breakpoints to delete. */
14848 if (!from_tty
14849 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14850 {
14851 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14852 if (is_tracepoint (b) && user_breakpoint_p (b))
14853 delete_breakpoint (b);
14854 }
14855 }
14856 else
14857 map_breakpoint_numbers
14858 (arg, [&] (breakpoint *b)
14859 {
14860 iterate_over_related_breakpoints (b, delete_breakpoint);
14861 });
14862 }
14863
14864 /* Helper function for trace_pass_command. */
14865
14866 static void
14867 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14868 {
14869 tp->pass_count = count;
14870 gdb::observers::breakpoint_modified.notify (tp);
14871 if (from_tty)
14872 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14873 tp->number, count);
14874 }
14875
14876 /* Set passcount for tracepoint.
14877
14878 First command argument is passcount, second is tracepoint number.
14879 If tracepoint number omitted, apply to most recently defined.
14880 Also accepts special argument "all". */
14881
14882 static void
14883 trace_pass_command (const char *args, int from_tty)
14884 {
14885 struct tracepoint *t1;
14886 ULONGEST count;
14887
14888 if (args == 0 || *args == 0)
14889 error (_("passcount command requires an "
14890 "argument (count + optional TP num)"));
14891
14892 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14893
14894 args = skip_spaces (args);
14895 if (*args && strncasecmp (args, "all", 3) == 0)
14896 {
14897 struct breakpoint *b;
14898
14899 args += 3; /* Skip special argument "all". */
14900 if (*args)
14901 error (_("Junk at end of arguments."));
14902
14903 ALL_TRACEPOINTS (b)
14904 {
14905 t1 = (struct tracepoint *) b;
14906 trace_pass_set_count (t1, count, from_tty);
14907 }
14908 }
14909 else if (*args == '\0')
14910 {
14911 t1 = get_tracepoint_by_number (&args, NULL);
14912 if (t1)
14913 trace_pass_set_count (t1, count, from_tty);
14914 }
14915 else
14916 {
14917 number_or_range_parser parser (args);
14918 while (!parser.finished ())
14919 {
14920 t1 = get_tracepoint_by_number (&args, &parser);
14921 if (t1)
14922 trace_pass_set_count (t1, count, from_tty);
14923 }
14924 }
14925 }
14926
14927 struct tracepoint *
14928 get_tracepoint (int num)
14929 {
14930 struct breakpoint *t;
14931
14932 ALL_TRACEPOINTS (t)
14933 if (t->number == num)
14934 return (struct tracepoint *) t;
14935
14936 return NULL;
14937 }
14938
14939 /* Find the tracepoint with the given target-side number (which may be
14940 different from the tracepoint number after disconnecting and
14941 reconnecting). */
14942
14943 struct tracepoint *
14944 get_tracepoint_by_number_on_target (int num)
14945 {
14946 struct breakpoint *b;
14947
14948 ALL_TRACEPOINTS (b)
14949 {
14950 struct tracepoint *t = (struct tracepoint *) b;
14951
14952 if (t->number_on_target == num)
14953 return t;
14954 }
14955
14956 return NULL;
14957 }
14958
14959 /* Utility: parse a tracepoint number and look it up in the list.
14960 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14961 If the argument is missing, the most recent tracepoint
14962 (tracepoint_count) is returned. */
14963
14964 struct tracepoint *
14965 get_tracepoint_by_number (const char **arg,
14966 number_or_range_parser *parser)
14967 {
14968 struct breakpoint *t;
14969 int tpnum;
14970 const char *instring = arg == NULL ? NULL : *arg;
14971
14972 if (parser != NULL)
14973 {
14974 gdb_assert (!parser->finished ());
14975 tpnum = parser->get_number ();
14976 }
14977 else if (arg == NULL || *arg == NULL || ! **arg)
14978 tpnum = tracepoint_count;
14979 else
14980 tpnum = get_number (arg);
14981
14982 if (tpnum <= 0)
14983 {
14984 if (instring && *instring)
14985 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14986 instring);
14987 else
14988 printf_filtered (_("No previous tracepoint\n"));
14989 return NULL;
14990 }
14991
14992 ALL_TRACEPOINTS (t)
14993 if (t->number == tpnum)
14994 {
14995 return (struct tracepoint *) t;
14996 }
14997
14998 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14999 return NULL;
15000 }
15001
15002 void
15003 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15004 {
15005 if (b->thread != -1)
15006 fprintf_unfiltered (fp, " thread %d", b->thread);
15007
15008 if (b->task != 0)
15009 fprintf_unfiltered (fp, " task %d", b->task);
15010
15011 fprintf_unfiltered (fp, "\n");
15012 }
15013
15014 /* Save information on user settable breakpoints (watchpoints, etc) to
15015 a new script file named FILENAME. If FILTER is non-NULL, call it
15016 on each breakpoint and only include the ones for which it returns
15017 non-zero. */
15018
15019 static void
15020 save_breakpoints (const char *filename, int from_tty,
15021 int (*filter) (const struct breakpoint *))
15022 {
15023 struct breakpoint *tp;
15024 int any = 0;
15025 int extra_trace_bits = 0;
15026
15027 if (filename == 0 || *filename == 0)
15028 error (_("Argument required (file name in which to save)"));
15029
15030 /* See if we have anything to save. */
15031 ALL_BREAKPOINTS (tp)
15032 {
15033 /* Skip internal and momentary breakpoints. */
15034 if (!user_breakpoint_p (tp))
15035 continue;
15036
15037 /* If we have a filter, only save the breakpoints it accepts. */
15038 if (filter && !filter (tp))
15039 continue;
15040
15041 any = 1;
15042
15043 if (is_tracepoint (tp))
15044 {
15045 extra_trace_bits = 1;
15046
15047 /* We can stop searching. */
15048 break;
15049 }
15050 }
15051
15052 if (!any)
15053 {
15054 warning (_("Nothing to save."));
15055 return;
15056 }
15057
15058 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15059
15060 stdio_file fp;
15061
15062 if (!fp.open (expanded_filename.get (), "w"))
15063 error (_("Unable to open file '%s' for saving (%s)"),
15064 expanded_filename.get (), safe_strerror (errno));
15065
15066 if (extra_trace_bits)
15067 save_trace_state_variables (&fp);
15068
15069 ALL_BREAKPOINTS (tp)
15070 {
15071 /* Skip internal and momentary breakpoints. */
15072 if (!user_breakpoint_p (tp))
15073 continue;
15074
15075 /* If we have a filter, only save the breakpoints it accepts. */
15076 if (filter && !filter (tp))
15077 continue;
15078
15079 tp->ops->print_recreate (tp, &fp);
15080
15081 /* Note, we can't rely on tp->number for anything, as we can't
15082 assume the recreated breakpoint numbers will match. Use $bpnum
15083 instead. */
15084
15085 if (tp->cond_string)
15086 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15087
15088 if (tp->ignore_count)
15089 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15090
15091 if (tp->type != bp_dprintf && tp->commands)
15092 {
15093 fp.puts (" commands\n");
15094
15095 current_uiout->redirect (&fp);
15096 TRY
15097 {
15098 print_command_lines (current_uiout, tp->commands.get (), 2);
15099 }
15100 CATCH (ex, RETURN_MASK_ALL)
15101 {
15102 current_uiout->redirect (NULL);
15103 throw_exception (ex);
15104 }
15105 END_CATCH
15106
15107 current_uiout->redirect (NULL);
15108 fp.puts (" end\n");
15109 }
15110
15111 if (tp->enable_state == bp_disabled)
15112 fp.puts ("disable $bpnum\n");
15113
15114 /* If this is a multi-location breakpoint, check if the locations
15115 should be individually disabled. Watchpoint locations are
15116 special, and not user visible. */
15117 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15118 {
15119 struct bp_location *loc;
15120 int n = 1;
15121
15122 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15123 if (!loc->enabled)
15124 fp.printf ("disable $bpnum.%d\n", n);
15125 }
15126 }
15127
15128 if (extra_trace_bits && *default_collect)
15129 fp.printf ("set default-collect %s\n", default_collect);
15130
15131 if (from_tty)
15132 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15133 }
15134
15135 /* The `save breakpoints' command. */
15136
15137 static void
15138 save_breakpoints_command (const char *args, int from_tty)
15139 {
15140 save_breakpoints (args, from_tty, NULL);
15141 }
15142
15143 /* The `save tracepoints' command. */
15144
15145 static void
15146 save_tracepoints_command (const char *args, int from_tty)
15147 {
15148 save_breakpoints (args, from_tty, is_tracepoint);
15149 }
15150
15151 /* Create a vector of all tracepoints. */
15152
15153 std::vector<breakpoint *>
15154 all_tracepoints (void)
15155 {
15156 std::vector<breakpoint *> tp_vec;
15157 struct breakpoint *tp;
15158
15159 ALL_TRACEPOINTS (tp)
15160 {
15161 tp_vec.push_back (tp);
15162 }
15163
15164 return tp_vec;
15165 }
15166
15167 \f
15168 /* This help string is used to consolidate all the help string for specifying
15169 locations used by several commands. */
15170
15171 #define LOCATION_HELP_STRING \
15172 "Linespecs are colon-separated lists of location parameters, such as\n\
15173 source filename, function name, label name, and line number.\n\
15174 Example: To specify the start of a label named \"the_top\" in the\n\
15175 function \"fact\" in the file \"factorial.c\", use\n\
15176 \"factorial.c:fact:the_top\".\n\
15177 \n\
15178 Address locations begin with \"*\" and specify an exact address in the\n\
15179 program. Example: To specify the fourth byte past the start function\n\
15180 \"main\", use \"*main + 4\".\n\
15181 \n\
15182 Explicit locations are similar to linespecs but use an option/argument\n\
15183 syntax to specify location parameters.\n\
15184 Example: To specify the start of the label named \"the_top\" in the\n\
15185 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15186 -function fact -label the_top\".\n\
15187 \n\
15188 By default, a specified function is matched against the program's\n\
15189 functions in all scopes. For C++, this means in all namespaces and\n\
15190 classes. For Ada, this means in all packages. E.g., in C++,\n\
15191 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15192 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
15193 specified name as a complete fully-qualified name instead.\n"
15194
15195 /* This help string is used for the break, hbreak, tbreak and thbreak
15196 commands. It is defined as a macro to prevent duplication.
15197 COMMAND should be a string constant containing the name of the
15198 command. */
15199
15200 #define BREAK_ARGS_HELP(command) \
15201 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15202 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15203 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15204 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15205 `-probe-dtrace' (for a DTrace probe).\n\
15206 LOCATION may be a linespec, address, or explicit location as described\n\
15207 below.\n\
15208 \n\
15209 With no LOCATION, uses current execution address of the selected\n\
15210 stack frame. This is useful for breaking on return to a stack frame.\n\
15211 \n\
15212 THREADNUM is the number from \"info threads\".\n\
15213 CONDITION is a boolean expression.\n\
15214 \n" LOCATION_HELP_STRING "\n\
15215 Multiple breakpoints at one place are permitted, and useful if their\n\
15216 conditions are different.\n\
15217 \n\
15218 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15219
15220 /* List of subcommands for "catch". */
15221 static struct cmd_list_element *catch_cmdlist;
15222
15223 /* List of subcommands for "tcatch". */
15224 static struct cmd_list_element *tcatch_cmdlist;
15225
15226 void
15227 add_catch_command (const char *name, const char *docstring,
15228 cmd_const_sfunc_ftype *sfunc,
15229 completer_ftype *completer,
15230 void *user_data_catch,
15231 void *user_data_tcatch)
15232 {
15233 struct cmd_list_element *command;
15234
15235 command = add_cmd (name, class_breakpoint, docstring,
15236 &catch_cmdlist);
15237 set_cmd_sfunc (command, sfunc);
15238 set_cmd_context (command, user_data_catch);
15239 set_cmd_completer (command, completer);
15240
15241 command = add_cmd (name, class_breakpoint, docstring,
15242 &tcatch_cmdlist);
15243 set_cmd_sfunc (command, sfunc);
15244 set_cmd_context (command, user_data_tcatch);
15245 set_cmd_completer (command, completer);
15246 }
15247
15248 static void
15249 save_command (const char *arg, int from_tty)
15250 {
15251 printf_unfiltered (_("\"save\" must be followed by "
15252 "the name of a save subcommand.\n"));
15253 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15254 }
15255
15256 struct breakpoint *
15257 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15258 void *data)
15259 {
15260 struct breakpoint *b, *b_tmp;
15261
15262 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15263 {
15264 if ((*callback) (b, data))
15265 return b;
15266 }
15267
15268 return NULL;
15269 }
15270
15271 /* Zero if any of the breakpoint's locations could be a location where
15272 functions have been inlined, nonzero otherwise. */
15273
15274 static int
15275 is_non_inline_function (struct breakpoint *b)
15276 {
15277 /* The shared library event breakpoint is set on the address of a
15278 non-inline function. */
15279 if (b->type == bp_shlib_event)
15280 return 1;
15281
15282 return 0;
15283 }
15284
15285 /* Nonzero if the specified PC cannot be a location where functions
15286 have been inlined. */
15287
15288 int
15289 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
15290 const struct target_waitstatus *ws)
15291 {
15292 struct breakpoint *b;
15293 struct bp_location *bl;
15294
15295 ALL_BREAKPOINTS (b)
15296 {
15297 if (!is_non_inline_function (b))
15298 continue;
15299
15300 for (bl = b->loc; bl != NULL; bl = bl->next)
15301 {
15302 if (!bl->shlib_disabled
15303 && bpstat_check_location (bl, aspace, pc, ws))
15304 return 1;
15305 }
15306 }
15307
15308 return 0;
15309 }
15310
15311 /* Remove any references to OBJFILE which is going to be freed. */
15312
15313 void
15314 breakpoint_free_objfile (struct objfile *objfile)
15315 {
15316 struct bp_location **locp, *loc;
15317
15318 ALL_BP_LOCATIONS (loc, locp)
15319 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15320 loc->symtab = NULL;
15321 }
15322
15323 void
15324 initialize_breakpoint_ops (void)
15325 {
15326 static int initialized = 0;
15327
15328 struct breakpoint_ops *ops;
15329
15330 if (initialized)
15331 return;
15332 initialized = 1;
15333
15334 /* The breakpoint_ops structure to be inherit by all kinds of
15335 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15336 internal and momentary breakpoints, etc.). */
15337 ops = &bkpt_base_breakpoint_ops;
15338 *ops = base_breakpoint_ops;
15339 ops->re_set = bkpt_re_set;
15340 ops->insert_location = bkpt_insert_location;
15341 ops->remove_location = bkpt_remove_location;
15342 ops->breakpoint_hit = bkpt_breakpoint_hit;
15343 ops->create_sals_from_location = bkpt_create_sals_from_location;
15344 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15345 ops->decode_location = bkpt_decode_location;
15346
15347 /* The breakpoint_ops structure to be used in regular breakpoints. */
15348 ops = &bkpt_breakpoint_ops;
15349 *ops = bkpt_base_breakpoint_ops;
15350 ops->re_set = bkpt_re_set;
15351 ops->resources_needed = bkpt_resources_needed;
15352 ops->print_it = bkpt_print_it;
15353 ops->print_mention = bkpt_print_mention;
15354 ops->print_recreate = bkpt_print_recreate;
15355
15356 /* Ranged breakpoints. */
15357 ops = &ranged_breakpoint_ops;
15358 *ops = bkpt_breakpoint_ops;
15359 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15360 ops->resources_needed = resources_needed_ranged_breakpoint;
15361 ops->print_it = print_it_ranged_breakpoint;
15362 ops->print_one = print_one_ranged_breakpoint;
15363 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15364 ops->print_mention = print_mention_ranged_breakpoint;
15365 ops->print_recreate = print_recreate_ranged_breakpoint;
15366
15367 /* Internal breakpoints. */
15368 ops = &internal_breakpoint_ops;
15369 *ops = bkpt_base_breakpoint_ops;
15370 ops->re_set = internal_bkpt_re_set;
15371 ops->check_status = internal_bkpt_check_status;
15372 ops->print_it = internal_bkpt_print_it;
15373 ops->print_mention = internal_bkpt_print_mention;
15374
15375 /* Momentary breakpoints. */
15376 ops = &momentary_breakpoint_ops;
15377 *ops = bkpt_base_breakpoint_ops;
15378 ops->re_set = momentary_bkpt_re_set;
15379 ops->check_status = momentary_bkpt_check_status;
15380 ops->print_it = momentary_bkpt_print_it;
15381 ops->print_mention = momentary_bkpt_print_mention;
15382
15383 /* Probe breakpoints. */
15384 ops = &bkpt_probe_breakpoint_ops;
15385 *ops = bkpt_breakpoint_ops;
15386 ops->insert_location = bkpt_probe_insert_location;
15387 ops->remove_location = bkpt_probe_remove_location;
15388 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15389 ops->decode_location = bkpt_probe_decode_location;
15390
15391 /* Watchpoints. */
15392 ops = &watchpoint_breakpoint_ops;
15393 *ops = base_breakpoint_ops;
15394 ops->re_set = re_set_watchpoint;
15395 ops->insert_location = insert_watchpoint;
15396 ops->remove_location = remove_watchpoint;
15397 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15398 ops->check_status = check_status_watchpoint;
15399 ops->resources_needed = resources_needed_watchpoint;
15400 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15401 ops->print_it = print_it_watchpoint;
15402 ops->print_mention = print_mention_watchpoint;
15403 ops->print_recreate = print_recreate_watchpoint;
15404 ops->explains_signal = explains_signal_watchpoint;
15405
15406 /* Masked watchpoints. */
15407 ops = &masked_watchpoint_breakpoint_ops;
15408 *ops = watchpoint_breakpoint_ops;
15409 ops->insert_location = insert_masked_watchpoint;
15410 ops->remove_location = remove_masked_watchpoint;
15411 ops->resources_needed = resources_needed_masked_watchpoint;
15412 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15413 ops->print_it = print_it_masked_watchpoint;
15414 ops->print_one_detail = print_one_detail_masked_watchpoint;
15415 ops->print_mention = print_mention_masked_watchpoint;
15416 ops->print_recreate = print_recreate_masked_watchpoint;
15417
15418 /* Tracepoints. */
15419 ops = &tracepoint_breakpoint_ops;
15420 *ops = base_breakpoint_ops;
15421 ops->re_set = tracepoint_re_set;
15422 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15423 ops->print_one_detail = tracepoint_print_one_detail;
15424 ops->print_mention = tracepoint_print_mention;
15425 ops->print_recreate = tracepoint_print_recreate;
15426 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15427 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15428 ops->decode_location = tracepoint_decode_location;
15429
15430 /* Probe tracepoints. */
15431 ops = &tracepoint_probe_breakpoint_ops;
15432 *ops = tracepoint_breakpoint_ops;
15433 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15434 ops->decode_location = tracepoint_probe_decode_location;
15435
15436 /* Static tracepoints with marker (`-m'). */
15437 ops = &strace_marker_breakpoint_ops;
15438 *ops = tracepoint_breakpoint_ops;
15439 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15440 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15441 ops->decode_location = strace_marker_decode_location;
15442
15443 /* Fork catchpoints. */
15444 ops = &catch_fork_breakpoint_ops;
15445 *ops = base_breakpoint_ops;
15446 ops->insert_location = insert_catch_fork;
15447 ops->remove_location = remove_catch_fork;
15448 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15449 ops->print_it = print_it_catch_fork;
15450 ops->print_one = print_one_catch_fork;
15451 ops->print_mention = print_mention_catch_fork;
15452 ops->print_recreate = print_recreate_catch_fork;
15453
15454 /* Vfork catchpoints. */
15455 ops = &catch_vfork_breakpoint_ops;
15456 *ops = base_breakpoint_ops;
15457 ops->insert_location = insert_catch_vfork;
15458 ops->remove_location = remove_catch_vfork;
15459 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15460 ops->print_it = print_it_catch_vfork;
15461 ops->print_one = print_one_catch_vfork;
15462 ops->print_mention = print_mention_catch_vfork;
15463 ops->print_recreate = print_recreate_catch_vfork;
15464
15465 /* Exec catchpoints. */
15466 ops = &catch_exec_breakpoint_ops;
15467 *ops = base_breakpoint_ops;
15468 ops->insert_location = insert_catch_exec;
15469 ops->remove_location = remove_catch_exec;
15470 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15471 ops->print_it = print_it_catch_exec;
15472 ops->print_one = print_one_catch_exec;
15473 ops->print_mention = print_mention_catch_exec;
15474 ops->print_recreate = print_recreate_catch_exec;
15475
15476 /* Solib-related catchpoints. */
15477 ops = &catch_solib_breakpoint_ops;
15478 *ops = base_breakpoint_ops;
15479 ops->insert_location = insert_catch_solib;
15480 ops->remove_location = remove_catch_solib;
15481 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15482 ops->check_status = check_status_catch_solib;
15483 ops->print_it = print_it_catch_solib;
15484 ops->print_one = print_one_catch_solib;
15485 ops->print_mention = print_mention_catch_solib;
15486 ops->print_recreate = print_recreate_catch_solib;
15487
15488 ops = &dprintf_breakpoint_ops;
15489 *ops = bkpt_base_breakpoint_ops;
15490 ops->re_set = dprintf_re_set;
15491 ops->resources_needed = bkpt_resources_needed;
15492 ops->print_it = bkpt_print_it;
15493 ops->print_mention = bkpt_print_mention;
15494 ops->print_recreate = dprintf_print_recreate;
15495 ops->after_condition_true = dprintf_after_condition_true;
15496 ops->breakpoint_hit = dprintf_breakpoint_hit;
15497 }
15498
15499 /* Chain containing all defined "enable breakpoint" subcommands. */
15500
15501 static struct cmd_list_element *enablebreaklist = NULL;
15502
15503 void
15504 _initialize_breakpoint (void)
15505 {
15506 struct cmd_list_element *c;
15507
15508 initialize_breakpoint_ops ();
15509
15510 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib);
15511 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile);
15512 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change);
15513
15514 breakpoint_objfile_key
15515 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
15516
15517 breakpoint_chain = 0;
15518 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15519 before a breakpoint is set. */
15520 breakpoint_count = 0;
15521
15522 tracepoint_count = 0;
15523
15524 add_com ("ignore", class_breakpoint, ignore_command, _("\
15525 Set ignore-count of breakpoint number N to COUNT.\n\
15526 Usage is `ignore N COUNT'."));
15527
15528 add_com ("commands", class_breakpoint, commands_command, _("\
15529 Set commands to be executed when the given breakpoints are hit.\n\
15530 Give a space-separated breakpoint list as argument after \"commands\".\n\
15531 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15532 (e.g. `5-7').\n\
15533 With no argument, the targeted breakpoint is the last one set.\n\
15534 The commands themselves follow starting on the next line.\n\
15535 Type a line containing \"end\" to indicate the end of them.\n\
15536 Give \"silent\" as the first line to make the breakpoint silent;\n\
15537 then no output is printed when it is hit, except what the commands print."));
15538
15539 c = add_com ("condition", class_breakpoint, condition_command, _("\
15540 Specify breakpoint number N to break only if COND is true.\n\
15541 Usage is `condition N COND', where N is an integer and COND is an\n\
15542 expression to be evaluated whenever breakpoint N is reached."));
15543 set_cmd_completer (c, condition_completer);
15544
15545 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15546 Set a temporary breakpoint.\n\
15547 Like \"break\" except the breakpoint is only temporary,\n\
15548 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15549 by using \"enable delete\" on the breakpoint number.\n\
15550 \n"
15551 BREAK_ARGS_HELP ("tbreak")));
15552 set_cmd_completer (c, location_completer);
15553
15554 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15555 Set a hardware assisted breakpoint.\n\
15556 Like \"break\" except the breakpoint requires hardware support,\n\
15557 some target hardware may not have this support.\n\
15558 \n"
15559 BREAK_ARGS_HELP ("hbreak")));
15560 set_cmd_completer (c, location_completer);
15561
15562 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15563 Set a temporary hardware assisted breakpoint.\n\
15564 Like \"hbreak\" except the breakpoint is only temporary,\n\
15565 so it will be deleted when hit.\n\
15566 \n"
15567 BREAK_ARGS_HELP ("thbreak")));
15568 set_cmd_completer (c, location_completer);
15569
15570 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15571 Enable some breakpoints.\n\
15572 Give breakpoint numbers (separated by spaces) as arguments.\n\
15573 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15574 This is used to cancel the effect of the \"disable\" command.\n\
15575 With a subcommand you can enable temporarily."),
15576 &enablelist, "enable ", 1, &cmdlist);
15577
15578 add_com_alias ("en", "enable", class_breakpoint, 1);
15579
15580 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15581 Enable some breakpoints.\n\
15582 Give breakpoint numbers (separated by spaces) as arguments.\n\
15583 This is used to cancel the effect of the \"disable\" command.\n\
15584 May be abbreviated to simply \"enable\".\n"),
15585 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15586
15587 add_cmd ("once", no_class, enable_once_command, _("\
15588 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15589 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15590 &enablebreaklist);
15591
15592 add_cmd ("delete", no_class, enable_delete_command, _("\
15593 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15594 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15595 &enablebreaklist);
15596
15597 add_cmd ("count", no_class, enable_count_command, _("\
15598 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15599 If a breakpoint is hit while enabled in this fashion,\n\
15600 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15601 &enablebreaklist);
15602
15603 add_cmd ("delete", no_class, enable_delete_command, _("\
15604 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15605 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15606 &enablelist);
15607
15608 add_cmd ("once", no_class, enable_once_command, _("\
15609 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15610 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15611 &enablelist);
15612
15613 add_cmd ("count", no_class, enable_count_command, _("\
15614 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15615 If a breakpoint is hit while enabled in this fashion,\n\
15616 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15617 &enablelist);
15618
15619 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15620 Disable some breakpoints.\n\
15621 Arguments are breakpoint numbers with spaces in between.\n\
15622 To disable all breakpoints, give no argument.\n\
15623 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15624 &disablelist, "disable ", 1, &cmdlist);
15625 add_com_alias ("dis", "disable", class_breakpoint, 1);
15626 add_com_alias ("disa", "disable", class_breakpoint, 1);
15627
15628 add_cmd ("breakpoints", class_alias, disable_command, _("\
15629 Disable some breakpoints.\n\
15630 Arguments are breakpoint numbers with spaces in between.\n\
15631 To disable all breakpoints, give no argument.\n\
15632 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15633 This command may be abbreviated \"disable\"."),
15634 &disablelist);
15635
15636 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15637 Delete some breakpoints or auto-display expressions.\n\
15638 Arguments are breakpoint numbers with spaces in between.\n\
15639 To delete all breakpoints, give no argument.\n\
15640 \n\
15641 Also a prefix command for deletion of other GDB objects.\n\
15642 The \"unset\" command is also an alias for \"delete\"."),
15643 &deletelist, "delete ", 1, &cmdlist);
15644 add_com_alias ("d", "delete", class_breakpoint, 1);
15645 add_com_alias ("del", "delete", class_breakpoint, 1);
15646
15647 add_cmd ("breakpoints", class_alias, delete_command, _("\
15648 Delete some breakpoints or auto-display expressions.\n\
15649 Arguments are breakpoint numbers with spaces in between.\n\
15650 To delete all breakpoints, give no argument.\n\
15651 This command may be abbreviated \"delete\"."),
15652 &deletelist);
15653
15654 add_com ("clear", class_breakpoint, clear_command, _("\
15655 Clear breakpoint at specified location.\n\
15656 Argument may be a linespec, explicit, or address location as described below.\n\
15657 \n\
15658 With no argument, clears all breakpoints in the line that the selected frame\n\
15659 is executing in.\n"
15660 "\n" LOCATION_HELP_STRING "\n\
15661 See also the \"delete\" command which clears breakpoints by number."));
15662 add_com_alias ("cl", "clear", class_breakpoint, 1);
15663
15664 c = add_com ("break", class_breakpoint, break_command, _("\
15665 Set breakpoint at specified location.\n"
15666 BREAK_ARGS_HELP ("break")));
15667 set_cmd_completer (c, location_completer);
15668
15669 add_com_alias ("b", "break", class_run, 1);
15670 add_com_alias ("br", "break", class_run, 1);
15671 add_com_alias ("bre", "break", class_run, 1);
15672 add_com_alias ("brea", "break", class_run, 1);
15673
15674 if (dbx_commands)
15675 {
15676 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15677 Break in function/address or break at a line in the current file."),
15678 &stoplist, "stop ", 1, &cmdlist);
15679 add_cmd ("in", class_breakpoint, stopin_command,
15680 _("Break in function or address."), &stoplist);
15681 add_cmd ("at", class_breakpoint, stopat_command,
15682 _("Break at a line in the current file."), &stoplist);
15683 add_com ("status", class_info, info_breakpoints_command, _("\
15684 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15685 The \"Type\" column indicates one of:\n\
15686 \tbreakpoint - normal breakpoint\n\
15687 \twatchpoint - watchpoint\n\
15688 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15689 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15690 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15691 address and file/line number respectively.\n\
15692 \n\
15693 Convenience variable \"$_\" and default examine address for \"x\"\n\
15694 are set to the address of the last breakpoint listed unless the command\n\
15695 is prefixed with \"server \".\n\n\
15696 Convenience variable \"$bpnum\" contains the number of the last\n\
15697 breakpoint set."));
15698 }
15699
15700 add_info ("breakpoints", info_breakpoints_command, _("\
15701 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15702 The \"Type\" column indicates one of:\n\
15703 \tbreakpoint - normal breakpoint\n\
15704 \twatchpoint - watchpoint\n\
15705 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15706 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15707 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15708 address and file/line number respectively.\n\
15709 \n\
15710 Convenience variable \"$_\" and default examine address for \"x\"\n\
15711 are set to the address of the last breakpoint listed unless the command\n\
15712 is prefixed with \"server \".\n\n\
15713 Convenience variable \"$bpnum\" contains the number of the last\n\
15714 breakpoint set."));
15715
15716 add_info_alias ("b", "breakpoints", 1);
15717
15718 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15719 Status of all breakpoints, or breakpoint number NUMBER.\n\
15720 The \"Type\" column indicates one of:\n\
15721 \tbreakpoint - normal breakpoint\n\
15722 \twatchpoint - watchpoint\n\
15723 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15724 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15725 \tuntil - internal breakpoint used by the \"until\" command\n\
15726 \tfinish - internal breakpoint used by the \"finish\" command\n\
15727 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15728 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15729 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15730 address and file/line number respectively.\n\
15731 \n\
15732 Convenience variable \"$_\" and default examine address for \"x\"\n\
15733 are set to the address of the last breakpoint listed unless the command\n\
15734 is prefixed with \"server \".\n\n\
15735 Convenience variable \"$bpnum\" contains the number of the last\n\
15736 breakpoint set."),
15737 &maintenanceinfolist);
15738
15739 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15740 Set catchpoints to catch events."),
15741 &catch_cmdlist, "catch ",
15742 0/*allow-unknown*/, &cmdlist);
15743
15744 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15745 Set temporary catchpoints to catch events."),
15746 &tcatch_cmdlist, "tcatch ",
15747 0/*allow-unknown*/, &cmdlist);
15748
15749 add_catch_command ("fork", _("Catch calls to fork."),
15750 catch_fork_command_1,
15751 NULL,
15752 (void *) (uintptr_t) catch_fork_permanent,
15753 (void *) (uintptr_t) catch_fork_temporary);
15754 add_catch_command ("vfork", _("Catch calls to vfork."),
15755 catch_fork_command_1,
15756 NULL,
15757 (void *) (uintptr_t) catch_vfork_permanent,
15758 (void *) (uintptr_t) catch_vfork_temporary);
15759 add_catch_command ("exec", _("Catch calls to exec."),
15760 catch_exec_command_1,
15761 NULL,
15762 CATCH_PERMANENT,
15763 CATCH_TEMPORARY);
15764 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15765 Usage: catch load [REGEX]\n\
15766 If REGEX is given, only stop for libraries matching the regular expression."),
15767 catch_load_command_1,
15768 NULL,
15769 CATCH_PERMANENT,
15770 CATCH_TEMPORARY);
15771 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15772 Usage: catch unload [REGEX]\n\
15773 If REGEX is given, only stop for libraries matching the regular expression."),
15774 catch_unload_command_1,
15775 NULL,
15776 CATCH_PERMANENT,
15777 CATCH_TEMPORARY);
15778
15779 c = add_com ("watch", class_breakpoint, watch_command, _("\
15780 Set a watchpoint for an expression.\n\
15781 Usage: watch [-l|-location] EXPRESSION\n\
15782 A watchpoint stops execution of your program whenever the value of\n\
15783 an expression changes.\n\
15784 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15785 the memory to which it refers."));
15786 set_cmd_completer (c, expression_completer);
15787
15788 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15789 Set a read watchpoint for an expression.\n\
15790 Usage: rwatch [-l|-location] EXPRESSION\n\
15791 A watchpoint stops execution of your program whenever the value of\n\
15792 an expression is read.\n\
15793 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15794 the memory to which it refers."));
15795 set_cmd_completer (c, expression_completer);
15796
15797 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15798 Set a watchpoint for an expression.\n\
15799 Usage: awatch [-l|-location] EXPRESSION\n\
15800 A watchpoint stops execution of your program whenever the value of\n\
15801 an expression is either read or written.\n\
15802 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15803 the memory to which it refers."));
15804 set_cmd_completer (c, expression_completer);
15805
15806 add_info ("watchpoints", info_watchpoints_command, _("\
15807 Status of specified watchpoints (all watchpoints if no argument)."));
15808
15809 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15810 respond to changes - contrary to the description. */
15811 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15812 &can_use_hw_watchpoints, _("\
15813 Set debugger's willingness to use watchpoint hardware."), _("\
15814 Show debugger's willingness to use watchpoint hardware."), _("\
15815 If zero, gdb will not use hardware for new watchpoints, even if\n\
15816 such is available. (However, any hardware watchpoints that were\n\
15817 created before setting this to nonzero, will continue to use watchpoint\n\
15818 hardware.)"),
15819 NULL,
15820 show_can_use_hw_watchpoints,
15821 &setlist, &showlist);
15822
15823 can_use_hw_watchpoints = 1;
15824
15825 /* Tracepoint manipulation commands. */
15826
15827 c = add_com ("trace", class_breakpoint, trace_command, _("\
15828 Set a tracepoint at specified location.\n\
15829 \n"
15830 BREAK_ARGS_HELP ("trace") "\n\
15831 Do \"help tracepoints\" for info on other tracepoint commands."));
15832 set_cmd_completer (c, location_completer);
15833
15834 add_com_alias ("tp", "trace", class_alias, 0);
15835 add_com_alias ("tr", "trace", class_alias, 1);
15836 add_com_alias ("tra", "trace", class_alias, 1);
15837 add_com_alias ("trac", "trace", class_alias, 1);
15838
15839 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15840 Set a fast tracepoint at specified location.\n\
15841 \n"
15842 BREAK_ARGS_HELP ("ftrace") "\n\
15843 Do \"help tracepoints\" for info on other tracepoint commands."));
15844 set_cmd_completer (c, location_completer);
15845
15846 c = add_com ("strace", class_breakpoint, strace_command, _("\
15847 Set a static tracepoint at location or marker.\n\
15848 \n\
15849 strace [LOCATION] [if CONDITION]\n\
15850 LOCATION may be a linespec, explicit, or address location (described below) \n\
15851 or -m MARKER_ID.\n\n\
15852 If a marker id is specified, probe the marker with that name. With\n\
15853 no LOCATION, uses current execution address of the selected stack frame.\n\
15854 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15855 This collects arbitrary user data passed in the probe point call to the\n\
15856 tracing library. You can inspect it when analyzing the trace buffer,\n\
15857 by printing the $_sdata variable like any other convenience variable.\n\
15858 \n\
15859 CONDITION is a boolean expression.\n\
15860 \n" LOCATION_HELP_STRING "\n\
15861 Multiple tracepoints at one place are permitted, and useful if their\n\
15862 conditions are different.\n\
15863 \n\
15864 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15865 Do \"help tracepoints\" for info on other tracepoint commands."));
15866 set_cmd_completer (c, location_completer);
15867
15868 add_info ("tracepoints", info_tracepoints_command, _("\
15869 Status of specified tracepoints (all tracepoints if no argument).\n\
15870 Convenience variable \"$tpnum\" contains the number of the\n\
15871 last tracepoint set."));
15872
15873 add_info_alias ("tp", "tracepoints", 1);
15874
15875 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15876 Delete specified tracepoints.\n\
15877 Arguments are tracepoint numbers, separated by spaces.\n\
15878 No argument means delete all tracepoints."),
15879 &deletelist);
15880 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
15881
15882 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15883 Disable specified tracepoints.\n\
15884 Arguments are tracepoint numbers, separated by spaces.\n\
15885 No argument means disable all tracepoints."),
15886 &disablelist);
15887 deprecate_cmd (c, "disable");
15888
15889 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15890 Enable specified tracepoints.\n\
15891 Arguments are tracepoint numbers, separated by spaces.\n\
15892 No argument means enable all tracepoints."),
15893 &enablelist);
15894 deprecate_cmd (c, "enable");
15895
15896 add_com ("passcount", class_trace, trace_pass_command, _("\
15897 Set the passcount for a tracepoint.\n\
15898 The trace will end when the tracepoint has been passed 'count' times.\n\
15899 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15900 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15901
15902 add_prefix_cmd ("save", class_breakpoint, save_command,
15903 _("Save breakpoint definitions as a script."),
15904 &save_cmdlist, "save ",
15905 0/*allow-unknown*/, &cmdlist);
15906
15907 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15908 Save current breakpoint definitions as a script.\n\
15909 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15910 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15911 session to restore them."),
15912 &save_cmdlist);
15913 set_cmd_completer (c, filename_completer);
15914
15915 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15916 Save current tracepoint definitions as a script.\n\
15917 Use the 'source' command in another debug session to restore them."),
15918 &save_cmdlist);
15919 set_cmd_completer (c, filename_completer);
15920
15921 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15922 deprecate_cmd (c, "save tracepoints");
15923
15924 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
15925 Breakpoint specific settings\n\
15926 Configure various breakpoint-specific variables such as\n\
15927 pending breakpoint behavior"),
15928 &breakpoint_set_cmdlist, "set breakpoint ",
15929 0/*allow-unknown*/, &setlist);
15930 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
15931 Breakpoint specific settings\n\
15932 Configure various breakpoint-specific variables such as\n\
15933 pending breakpoint behavior"),
15934 &breakpoint_show_cmdlist, "show breakpoint ",
15935 0/*allow-unknown*/, &showlist);
15936
15937 add_setshow_auto_boolean_cmd ("pending", no_class,
15938 &pending_break_support, _("\
15939 Set debugger's behavior regarding pending breakpoints."), _("\
15940 Show debugger's behavior regarding pending breakpoints."), _("\
15941 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15942 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15943 an error. If auto, an unrecognized breakpoint location results in a\n\
15944 user-query to see if a pending breakpoint should be created."),
15945 NULL,
15946 show_pending_break_support,
15947 &breakpoint_set_cmdlist,
15948 &breakpoint_show_cmdlist);
15949
15950 pending_break_support = AUTO_BOOLEAN_AUTO;
15951
15952 add_setshow_boolean_cmd ("auto-hw", no_class,
15953 &automatic_hardware_breakpoints, _("\
15954 Set automatic usage of hardware breakpoints."), _("\
15955 Show automatic usage of hardware breakpoints."), _("\
15956 If set, the debugger will automatically use hardware breakpoints for\n\
15957 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15958 a warning will be emitted for such breakpoints."),
15959 NULL,
15960 show_automatic_hardware_breakpoints,
15961 &breakpoint_set_cmdlist,
15962 &breakpoint_show_cmdlist);
15963
15964 add_setshow_boolean_cmd ("always-inserted", class_support,
15965 &always_inserted_mode, _("\
15966 Set mode for inserting breakpoints."), _("\
15967 Show mode for inserting breakpoints."), _("\
15968 When this mode is on, breakpoints are inserted immediately as soon as\n\
15969 they're created, kept inserted even when execution stops, and removed\n\
15970 only when the user deletes them. When this mode is off (the default),\n\
15971 breakpoints are inserted only when execution continues, and removed\n\
15972 when execution stops."),
15973 NULL,
15974 &show_always_inserted_mode,
15975 &breakpoint_set_cmdlist,
15976 &breakpoint_show_cmdlist);
15977
15978 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15979 condition_evaluation_enums,
15980 &condition_evaluation_mode_1, _("\
15981 Set mode of breakpoint condition evaluation."), _("\
15982 Show mode of breakpoint condition evaluation."), _("\
15983 When this is set to \"host\", breakpoint conditions will be\n\
15984 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15985 breakpoint conditions will be downloaded to the target (if the target\n\
15986 supports such feature) and conditions will be evaluated on the target's side.\n\
15987 If this is set to \"auto\" (default), this will be automatically set to\n\
15988 \"target\" if it supports condition evaluation, otherwise it will\n\
15989 be set to \"gdb\""),
15990 &set_condition_evaluation_mode,
15991 &show_condition_evaluation_mode,
15992 &breakpoint_set_cmdlist,
15993 &breakpoint_show_cmdlist);
15994
15995 add_com ("break-range", class_breakpoint, break_range_command, _("\
15996 Set a breakpoint for an address range.\n\
15997 break-range START-LOCATION, END-LOCATION\n\
15998 where START-LOCATION and END-LOCATION can be one of the following:\n\
15999 LINENUM, for that line in the current file,\n\
16000 FILE:LINENUM, for that line in that file,\n\
16001 +OFFSET, for that number of lines after the current line\n\
16002 or the start of the range\n\
16003 FUNCTION, for the first line in that function,\n\
16004 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16005 *ADDRESS, for the instruction at that address.\n\
16006 \n\
16007 The breakpoint will stop execution of the inferior whenever it executes\n\
16008 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16009 range (including START-LOCATION and END-LOCATION)."));
16010
16011 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16012 Set a dynamic printf at specified location.\n\
16013 dprintf location,format string,arg1,arg2,...\n\
16014 location may be a linespec, explicit, or address location.\n"
16015 "\n" LOCATION_HELP_STRING));
16016 set_cmd_completer (c, location_completer);
16017
16018 add_setshow_enum_cmd ("dprintf-style", class_support,
16019 dprintf_style_enums, &dprintf_style, _("\
16020 Set the style of usage for dynamic printf."), _("\
16021 Show the style of usage for dynamic printf."), _("\
16022 This setting chooses how GDB will do a dynamic printf.\n\
16023 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16024 console, as with the \"printf\" command.\n\
16025 If the value is \"call\", the print is done by calling a function in your\n\
16026 program; by default printf(), but you can choose a different function or\n\
16027 output stream by setting dprintf-function and dprintf-channel."),
16028 update_dprintf_commands, NULL,
16029 &setlist, &showlist);
16030
16031 dprintf_function = xstrdup ("printf");
16032 add_setshow_string_cmd ("dprintf-function", class_support,
16033 &dprintf_function, _("\
16034 Set the function to use for dynamic printf"), _("\
16035 Show the function to use for dynamic printf"), NULL,
16036 update_dprintf_commands, NULL,
16037 &setlist, &showlist);
16038
16039 dprintf_channel = xstrdup ("");
16040 add_setshow_string_cmd ("dprintf-channel", class_support,
16041 &dprintf_channel, _("\
16042 Set the channel to use for dynamic printf"), _("\
16043 Show the channel to use for dynamic printf"), NULL,
16044 update_dprintf_commands, NULL,
16045 &setlist, &showlist);
16046
16047 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16048 &disconnected_dprintf, _("\
16049 Set whether dprintf continues after GDB disconnects."), _("\
16050 Show whether dprintf continues after GDB disconnects."), _("\
16051 Use this to let dprintf commands continue to hit and produce output\n\
16052 even if GDB disconnects or detaches from the target."),
16053 NULL,
16054 NULL,
16055 &setlist, &showlist);
16056
16057 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16058 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16059 (target agent only) This is useful for formatted output in user-defined commands."));
16060
16061 automatic_hardware_breakpoints = 1;
16062
16063 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed);
16064 gdb::observers::thread_exit.attach (remove_threaded_breakpoints);
16065 }