Allocate bpstats with new
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "thread-fsm.h"
71 #include "tid-parse.h"
72
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "common/array-view.h"
85 #include "common/gdb_optional.h"
86
87 /* Enums for exception-handling support. */
88 enum exception_event_kind
89 {
90 EX_EVENT_THROW,
91 EX_EVENT_RETHROW,
92 EX_EVENT_CATCH
93 };
94
95 /* Prototypes for local functions. */
96
97 static void enable_delete_command (char *, int);
98
99 static void enable_once_command (char *, int);
100
101 static void enable_count_command (char *, int);
102
103 static void disable_command (char *, int);
104
105 static void enable_command (char *, int);
106
107 static void map_breakpoint_numbers (const char *,
108 void (*) (struct breakpoint *,
109 void *),
110 void *);
111
112 static void ignore_command (char *, int);
113
114 static int breakpoint_re_set_one (void *);
115
116 static void breakpoint_re_set_default (struct breakpoint *);
117
118 static void
119 create_sals_from_location_default (const struct event_location *location,
120 struct linespec_result *canonical,
121 enum bptype type_wanted);
122
123 static void create_breakpoints_sal_default (struct gdbarch *,
124 struct linespec_result *,
125 gdb::unique_xmalloc_ptr<char>,
126 gdb::unique_xmalloc_ptr<char>,
127 enum bptype,
128 enum bpdisp, int, int,
129 int,
130 const struct breakpoint_ops *,
131 int, int, int, unsigned);
132
133 static std::vector<symtab_and_line> decode_location_default
134 (struct breakpoint *b, const struct event_location *location,
135 struct program_space *search_pspace);
136
137 static void clear_command (char *, int);
138
139 static void catch_command (char *, int);
140
141 static int can_use_hardware_watchpoint (struct value *);
142
143 static void break_command_1 (char *, int, int);
144
145 static void mention (struct breakpoint *);
146
147 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
148 enum bptype,
149 const struct breakpoint_ops *);
150 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
151 const struct symtab_and_line *);
152
153 /* This function is used in gdbtk sources and thus can not be made
154 static. */
155 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
156 struct symtab_and_line,
157 enum bptype,
158 const struct breakpoint_ops *);
159
160 static struct breakpoint *
161 momentary_breakpoint_from_master (struct breakpoint *orig,
162 enum bptype type,
163 const struct breakpoint_ops *ops,
164 int loc_enabled);
165
166 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
167
168 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
169 CORE_ADDR bpaddr,
170 enum bptype bptype);
171
172 static void describe_other_breakpoints (struct gdbarch *,
173 struct program_space *, CORE_ADDR,
174 struct obj_section *, int);
175
176 static int watchpoint_locations_match (struct bp_location *loc1,
177 struct bp_location *loc2);
178
179 static int breakpoint_location_address_match (struct bp_location *bl,
180 struct address_space *aspace,
181 CORE_ADDR addr);
182
183 static int breakpoint_location_address_range_overlap (struct bp_location *,
184 struct address_space *,
185 CORE_ADDR, int);
186
187 static void info_breakpoints_command (char *, int);
188
189 static void info_watchpoints_command (char *, int);
190
191 static int breakpoint_1 (char *, int,
192 int (*) (const struct breakpoint *));
193
194 static int breakpoint_cond_eval (void *);
195
196 static void cleanup_executing_breakpoints (void *);
197
198 static void commands_command (char *, int);
199
200 static void condition_command (char *, int);
201
202 static int remove_breakpoint (struct bp_location *);
203 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
204
205 static enum print_stop_action print_bp_stop_message (bpstat bs);
206
207 static int watchpoint_check (void *);
208
209 static void maintenance_info_breakpoints (char *, int);
210
211 static int hw_breakpoint_used_count (void);
212
213 static int hw_watchpoint_use_count (struct breakpoint *);
214
215 static int hw_watchpoint_used_count_others (struct breakpoint *except,
216 enum bptype type,
217 int *other_type_used);
218
219 static void hbreak_command (char *, int);
220
221 static void thbreak_command (char *, int);
222
223 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
224 int count);
225
226 static void stop_command (char *arg, int from_tty);
227
228 static void stopin_command (char *arg, int from_tty);
229
230 static void stopat_command (char *arg, int from_tty);
231
232 static void tcatch_command (char *arg, int from_tty);
233
234 static void free_bp_location (struct bp_location *loc);
235 static void incref_bp_location (struct bp_location *loc);
236 static void decref_bp_location (struct bp_location **loc);
237
238 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
239
240 /* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242 enum ugll_insert_mode
243 {
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
261 /* May insert breakpoints iff breakpoints_should_be_inserted_now
262 claims breakpoints should be inserted now. */
263 UGLL_MAY_INSERT,
264
265 /* Insert locations now, irrespective of
266 breakpoints_should_be_inserted_now. E.g., say all threads are
267 stopped right now, and the user did "continue". We need to
268 insert breakpoints _before_ resuming the target, but
269 UGLL_MAY_INSERT wouldn't insert them, because
270 breakpoints_should_be_inserted_now returns false at that point,
271 as no thread is running yet. */
272 UGLL_INSERT
273 };
274
275 static void update_global_location_list (enum ugll_insert_mode);
276
277 static void update_global_location_list_nothrow (enum ugll_insert_mode);
278
279 static int is_hardware_watchpoint (const struct breakpoint *bpt);
280
281 static void insert_breakpoint_locations (void);
282
283 static void info_tracepoints_command (char *, int);
284
285 static void delete_trace_command (char *, int);
286
287 static void enable_trace_command (char *, int);
288
289 static void disable_trace_command (char *, int);
290
291 static void trace_pass_command (char *, int);
292
293 static void set_tracepoint_count (int num);
294
295 static int is_masked_watchpoint (const struct breakpoint *b);
296
297 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
298
299 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
300 otherwise. */
301
302 static int strace_marker_p (struct breakpoint *b);
303
304 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307 static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309 /* Internal breakpoints class type. */
310 static struct breakpoint_ops internal_breakpoint_ops;
311
312 /* Momentary breakpoints class type. */
313 static struct breakpoint_ops momentary_breakpoint_ops;
314
315 /* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317 struct breakpoint_ops bkpt_breakpoint_ops;
318
319 /* Breakpoints set on probes. */
320 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
322 /* Dynamic printf class type. */
323 struct breakpoint_ops dprintf_breakpoint_ops;
324
325 /* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332 static const char dprintf_style_gdb[] = "gdb";
333 static const char dprintf_style_call[] = "call";
334 static const char dprintf_style_agent[] = "agent";
335 static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340 };
341 static const char *dprintf_style = dprintf_style_gdb;
342
343 /* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348 static char *dprintf_function;
349
350 /* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358 static char *dprintf_channel;
359
360 /* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362 static int disconnected_dprintf = 1;
363
364 /* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366 struct counted_command_line
367 {
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373 };
374
375 struct command_line *
376 breakpoint_commands (struct breakpoint *b)
377 {
378 return b->commands ? b->commands->commands : NULL;
379 }
380
381 /* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384 static int breakpoint_proceeded;
385
386 const char *
387 bpdisp_text (enum bpdisp disp)
388 {
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
393
394 return bpdisps[(int) disp];
395 }
396
397 /* Prototypes for exported functions. */
398 /* If FALSE, gdb will not use hardware support for watchpoints, even
399 if such is available. */
400 static int can_use_hw_watchpoints;
401
402 static void
403 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406 {
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
410 value);
411 }
412
413 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
415 for unrecognized breakpoint locations.
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417 static enum auto_boolean pending_break_support;
418 static void
419 show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422 {
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
426 value);
427 }
428
429 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
430 set with "break" but falling in read-only memory.
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433 static int automatic_hardware_breakpoints;
434 static void
435 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438 {
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
441 value);
442 }
443
444 /* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451 static int always_inserted_mode = 0;
452
453 static void
454 show_always_inserted_mode (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
456 {
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
459 }
460
461 /* See breakpoint.h. */
462
463 int
464 breakpoints_should_be_inserted_now (void)
465 {
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
475 struct thread_info *tp;
476
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
484 if (threads_are_executing ())
485 return 1;
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
493 }
494 return 0;
495 }
496
497 static const char condition_evaluation_both[] = "host or target";
498
499 /* Modes for breakpoint condition evaluation. */
500 static const char condition_evaluation_auto[] = "auto";
501 static const char condition_evaluation_host[] = "host";
502 static const char condition_evaluation_target[] = "target";
503 static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508 };
509
510 /* Global that holds the current mode for breakpoint condition evaluation. */
511 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513 /* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515 static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517 /* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522 static const char *
523 translate_condition_evaluation_mode (const char *mode)
524 {
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534 }
535
536 /* Discovers what condition_evaluation_auto translates to. */
537
538 static const char *
539 breakpoint_condition_evaluation_mode (void)
540 {
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542 }
543
544 /* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547 static int
548 gdb_evaluates_breakpoint_condition_p (void)
549 {
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553 }
554
555 /* Are we executing breakpoint commands? */
556 static int executing_breakpoint_commands;
557
558 /* Are overlay event breakpoints enabled? */
559 static int overlay_events_enabled;
560
561 /* See description in breakpoint.h. */
562 int target_exact_watchpoints = 0;
563
564 /* Walk the following statement or block through all breakpoints.
565 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
566 current breakpoint. */
567
568 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
569
570 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
571 for (B = breakpoint_chain; \
572 B ? (TMP=B->next, 1): 0; \
573 B = TMP)
574
575 /* Similar iterator for the low-level breakpoints. SAFE variant is
576 not provided so update_global_location_list must not be called
577 while executing the block of ALL_BP_LOCATIONS. */
578
579 #define ALL_BP_LOCATIONS(B,BP_TMP) \
580 for (BP_TMP = bp_locations; \
581 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
582 BP_TMP++)
583
584 /* Iterates through locations with address ADDRESS for the currently selected
585 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
586 to where the loop should start from.
587 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
588 appropriate location to start with. */
589
590 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
591 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
592 BP_LOCP_TMP = BP_LOCP_START; \
593 BP_LOCP_START \
594 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
595 && (*BP_LOCP_TMP)->address == ADDRESS); \
596 BP_LOCP_TMP++)
597
598 /* Iterator for tracepoints only. */
599
600 #define ALL_TRACEPOINTS(B) \
601 for (B = breakpoint_chain; B; B = B->next) \
602 if (is_tracepoint (B))
603
604 /* Chains of all breakpoints defined. */
605
606 struct breakpoint *breakpoint_chain;
607
608 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
609
610 static struct bp_location **bp_locations;
611
612 /* Number of elements of BP_LOCATIONS. */
613
614 static unsigned bp_locations_count;
615
616 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
617 ADDRESS for the current elements of BP_LOCATIONS which get a valid
618 result from bp_location_has_shadow. You can use it for roughly
619 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
620 an address you need to read. */
621
622 static CORE_ADDR bp_locations_placed_address_before_address_max;
623
624 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
625 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
626 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
627 You can use it for roughly limiting the subrange of BP_LOCATIONS to
628 scan for shadow bytes for an address you need to read. */
629
630 static CORE_ADDR bp_locations_shadow_len_after_address_max;
631
632 /* The locations that no longer correspond to any breakpoint, unlinked
633 from the bp_locations array, but for which a hit may still be
634 reported by a target. */
635 VEC(bp_location_p) *moribund_locations = NULL;
636
637 /* Number of last breakpoint made. */
638
639 static int breakpoint_count;
640
641 /* The value of `breakpoint_count' before the last command that
642 created breakpoints. If the last (break-like) command created more
643 than one breakpoint, then the difference between BREAKPOINT_COUNT
644 and PREV_BREAKPOINT_COUNT is more than one. */
645 static int prev_breakpoint_count;
646
647 /* Number of last tracepoint made. */
648
649 static int tracepoint_count;
650
651 static struct cmd_list_element *breakpoint_set_cmdlist;
652 static struct cmd_list_element *breakpoint_show_cmdlist;
653 struct cmd_list_element *save_cmdlist;
654
655 /* See declaration at breakpoint.h. */
656
657 struct breakpoint *
658 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
659 void *user_data)
660 {
661 struct breakpoint *b = NULL;
662
663 ALL_BREAKPOINTS (b)
664 {
665 if (func (b, user_data) != 0)
666 break;
667 }
668
669 return b;
670 }
671
672 /* Return whether a breakpoint is an active enabled breakpoint. */
673 static int
674 breakpoint_enabled (struct breakpoint *b)
675 {
676 return (b->enable_state == bp_enabled);
677 }
678
679 /* Set breakpoint count to NUM. */
680
681 static void
682 set_breakpoint_count (int num)
683 {
684 prev_breakpoint_count = breakpoint_count;
685 breakpoint_count = num;
686 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
687 }
688
689 /* Used by `start_rbreak_breakpoints' below, to record the current
690 breakpoint count before "rbreak" creates any breakpoint. */
691 static int rbreak_start_breakpoint_count;
692
693 /* Called at the start an "rbreak" command to record the first
694 breakpoint made. */
695
696 void
697 start_rbreak_breakpoints (void)
698 {
699 rbreak_start_breakpoint_count = breakpoint_count;
700 }
701
702 /* Called at the end of an "rbreak" command to record the last
703 breakpoint made. */
704
705 void
706 end_rbreak_breakpoints (void)
707 {
708 prev_breakpoint_count = rbreak_start_breakpoint_count;
709 }
710
711 /* Used in run_command to zero the hit count when a new run starts. */
712
713 void
714 clear_breakpoint_hit_counts (void)
715 {
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 b->hit_count = 0;
720 }
721
722 /* Allocate a new counted_command_line with reference count of 1.
723 The new structure owns COMMANDS. */
724
725 static struct counted_command_line *
726 alloc_counted_command_line (struct command_line *commands)
727 {
728 struct counted_command_line *result = XNEW (struct counted_command_line);
729
730 result->refc = 1;
731 result->commands = commands;
732
733 return result;
734 }
735
736 /* Increment reference count. This does nothing if CMD is NULL. */
737
738 static void
739 incref_counted_command_line (struct counted_command_line *cmd)
740 {
741 if (cmd)
742 ++cmd->refc;
743 }
744
745 /* Decrement reference count. If the reference count reaches 0,
746 destroy the counted_command_line. Sets *CMDP to NULL. This does
747 nothing if *CMDP is NULL. */
748
749 static void
750 decref_counted_command_line (struct counted_command_line **cmdp)
751 {
752 if (*cmdp)
753 {
754 if (--(*cmdp)->refc == 0)
755 {
756 free_command_lines (&(*cmdp)->commands);
757 xfree (*cmdp);
758 }
759 *cmdp = NULL;
760 }
761 }
762
763 /* A cleanup function that calls decref_counted_command_line. */
764
765 static void
766 do_cleanup_counted_command_line (void *arg)
767 {
768 decref_counted_command_line ((struct counted_command_line **) arg);
769 }
770
771 /* Create a cleanup that calls decref_counted_command_line on the
772 argument. */
773
774 static struct cleanup *
775 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
776 {
777 return make_cleanup (do_cleanup_counted_command_line, cmdp);
778 }
779
780 \f
781 /* Return the breakpoint with the specified number, or NULL
782 if the number does not refer to an existing breakpoint. */
783
784 struct breakpoint *
785 get_breakpoint (int num)
786 {
787 struct breakpoint *b;
788
789 ALL_BREAKPOINTS (b)
790 if (b->number == num)
791 return b;
792
793 return NULL;
794 }
795
796 \f
797
798 /* Mark locations as "conditions have changed" in case the target supports
799 evaluating conditions on its side. */
800
801 static void
802 mark_breakpoint_modified (struct breakpoint *b)
803 {
804 struct bp_location *loc;
805
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812 return;
813
814 if (!is_breakpoint (b))
815 return;
816
817 for (loc = b->loc; loc; loc = loc->next)
818 loc->condition_changed = condition_modified;
819 }
820
821 /* Mark location as "conditions have changed" in case the target supports
822 evaluating conditions on its side. */
823
824 static void
825 mark_breakpoint_location_modified (struct bp_location *loc)
826 {
827 /* This is only meaningful if the target is
828 evaluating conditions and if the user has
829 opted for condition evaluation on the target's
830 side. */
831 if (gdb_evaluates_breakpoint_condition_p ()
832 || !target_supports_evaluation_of_breakpoint_conditions ())
833
834 return;
835
836 if (!is_breakpoint (loc->owner))
837 return;
838
839 loc->condition_changed = condition_modified;
840 }
841
842 /* Sets the condition-evaluation mode using the static global
843 condition_evaluation_mode. */
844
845 static void
846 set_condition_evaluation_mode (char *args, int from_tty,
847 struct cmd_list_element *c)
848 {
849 const char *old_mode, *new_mode;
850
851 if ((condition_evaluation_mode_1 == condition_evaluation_target)
852 && !target_supports_evaluation_of_breakpoint_conditions ())
853 {
854 condition_evaluation_mode_1 = condition_evaluation_mode;
855 warning (_("Target does not support breakpoint condition evaluation.\n"
856 "Using host evaluation mode instead."));
857 return;
858 }
859
860 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
861 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
862
863 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
864 settings was "auto". */
865 condition_evaluation_mode = condition_evaluation_mode_1;
866
867 /* Only update the mode if the user picked a different one. */
868 if (new_mode != old_mode)
869 {
870 struct bp_location *loc, **loc_tmp;
871 /* If the user switched to a different evaluation mode, we
872 need to synch the changes with the target as follows:
873
874 "host" -> "target": Send all (valid) conditions to the target.
875 "target" -> "host": Remove all the conditions from the target.
876 */
877
878 if (new_mode == condition_evaluation_target)
879 {
880 /* Mark everything modified and synch conditions with the
881 target. */
882 ALL_BP_LOCATIONS (loc, loc_tmp)
883 mark_breakpoint_location_modified (loc);
884 }
885 else
886 {
887 /* Manually mark non-duplicate locations to synch conditions
888 with the target. We do this to remove all the conditions the
889 target knows about. */
890 ALL_BP_LOCATIONS (loc, loc_tmp)
891 if (is_breakpoint (loc->owner) && loc->inserted)
892 loc->needs_update = 1;
893 }
894
895 /* Do the update. */
896 update_global_location_list (UGLL_MAY_INSERT);
897 }
898
899 return;
900 }
901
902 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
903 what "auto" is translating to. */
904
905 static void
906 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
907 struct cmd_list_element *c, const char *value)
908 {
909 if (condition_evaluation_mode == condition_evaluation_auto)
910 fprintf_filtered (file,
911 _("Breakpoint condition evaluation "
912 "mode is %s (currently %s).\n"),
913 value,
914 breakpoint_condition_evaluation_mode ());
915 else
916 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
917 value);
918 }
919
920 /* A comparison function for bp_location AP and BP that is used by
921 bsearch. This comparison function only cares about addresses, unlike
922 the more general bp_locations_compare function. */
923
924 static int
925 bp_locations_compare_addrs (const void *ap, const void *bp)
926 {
927 const struct bp_location *a = *(const struct bp_location **) ap;
928 const struct bp_location *b = *(const struct bp_location **) bp;
929
930 if (a->address == b->address)
931 return 0;
932 else
933 return ((a->address > b->address) - (a->address < b->address));
934 }
935
936 /* Helper function to skip all bp_locations with addresses
937 less than ADDRESS. It returns the first bp_location that
938 is greater than or equal to ADDRESS. If none is found, just
939 return NULL. */
940
941 static struct bp_location **
942 get_first_locp_gte_addr (CORE_ADDR address)
943 {
944 struct bp_location dummy_loc;
945 struct bp_location *dummy_locp = &dummy_loc;
946 struct bp_location **locp_found = NULL;
947
948 /* Initialize the dummy location's address field. */
949 dummy_loc.address = address;
950
951 /* Find a close match to the first location at ADDRESS. */
952 locp_found = ((struct bp_location **)
953 bsearch (&dummy_locp, bp_locations, bp_locations_count,
954 sizeof (struct bp_location **),
955 bp_locations_compare_addrs));
956
957 /* Nothing was found, nothing left to do. */
958 if (locp_found == NULL)
959 return NULL;
960
961 /* We may have found a location that is at ADDRESS but is not the first in the
962 location's list. Go backwards (if possible) and locate the first one. */
963 while ((locp_found - 1) >= bp_locations
964 && (*(locp_found - 1))->address == address)
965 locp_found--;
966
967 return locp_found;
968 }
969
970 void
971 set_breakpoint_condition (struct breakpoint *b, const char *exp,
972 int from_tty)
973 {
974 xfree (b->cond_string);
975 b->cond_string = NULL;
976
977 if (is_watchpoint (b))
978 {
979 struct watchpoint *w = (struct watchpoint *) b;
980
981 w->cond_exp.reset ();
982 }
983 else
984 {
985 struct bp_location *loc;
986
987 for (loc = b->loc; loc; loc = loc->next)
988 {
989 loc->cond.reset ();
990
991 /* No need to free the condition agent expression
992 bytecode (if we have one). We will handle this
993 when we go through update_global_location_list. */
994 }
995 }
996
997 if (*exp == 0)
998 {
999 if (from_tty)
1000 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1001 }
1002 else
1003 {
1004 const char *arg = exp;
1005
1006 /* I don't know if it matters whether this is the string the user
1007 typed in or the decompiled expression. */
1008 b->cond_string = xstrdup (arg);
1009 b->condition_not_parsed = 0;
1010
1011 if (is_watchpoint (b))
1012 {
1013 struct watchpoint *w = (struct watchpoint *) b;
1014
1015 innermost_block = NULL;
1016 arg = exp;
1017 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1018 if (*arg)
1019 error (_("Junk at end of expression"));
1020 w->cond_exp_valid_block = innermost_block;
1021 }
1022 else
1023 {
1024 struct bp_location *loc;
1025
1026 for (loc = b->loc; loc; loc = loc->next)
1027 {
1028 arg = exp;
1029 loc->cond =
1030 parse_exp_1 (&arg, loc->address,
1031 block_for_pc (loc->address), 0);
1032 if (*arg)
1033 error (_("Junk at end of expression"));
1034 }
1035 }
1036 }
1037 mark_breakpoint_modified (b);
1038
1039 observer_notify_breakpoint_modified (b);
1040 }
1041
1042 /* Completion for the "condition" command. */
1043
1044 static void
1045 condition_completer (struct cmd_list_element *cmd,
1046 completion_tracker &tracker,
1047 const char *text, const char *word)
1048 {
1049 const char *space;
1050
1051 text = skip_spaces (text);
1052 space = skip_to_space (text);
1053 if (*space == '\0')
1054 {
1055 int len;
1056 struct breakpoint *b;
1057 VEC (char_ptr) *result = NULL;
1058
1059 if (text[0] == '$')
1060 {
1061 /* We don't support completion of history indices. */
1062 if (!isdigit (text[1]))
1063 complete_internalvar (tracker, &text[1]);
1064 return;
1065 }
1066
1067 /* We're completing the breakpoint number. */
1068 len = strlen (text);
1069
1070 ALL_BREAKPOINTS (b)
1071 {
1072 char number[50];
1073
1074 xsnprintf (number, sizeof (number), "%d", b->number);
1075
1076 if (strncmp (number, text, len) == 0)
1077 {
1078 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1079 tracker.add_completion (std::move (copy));
1080 }
1081 }
1082
1083 return;
1084 }
1085
1086 /* We're completing the expression part. */
1087 text = skip_spaces (space);
1088 expression_completer (cmd, tracker, text, word);
1089 }
1090
1091 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1092
1093 static void
1094 condition_command (char *arg, int from_tty)
1095 {
1096 struct breakpoint *b;
1097 char *p;
1098 int bnum;
1099
1100 if (arg == 0)
1101 error_no_arg (_("breakpoint number"));
1102
1103 p = arg;
1104 bnum = get_number (&p);
1105 if (bnum == 0)
1106 error (_("Bad breakpoint argument: '%s'"), arg);
1107
1108 ALL_BREAKPOINTS (b)
1109 if (b->number == bnum)
1110 {
1111 /* Check if this breakpoint has a "stop" method implemented in an
1112 extension language. This method and conditions entered into GDB
1113 from the CLI are mutually exclusive. */
1114 const struct extension_language_defn *extlang
1115 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1116
1117 if (extlang != NULL)
1118 {
1119 error (_("Only one stop condition allowed. There is currently"
1120 " a %s stop condition defined for this breakpoint."),
1121 ext_lang_capitalized_name (extlang));
1122 }
1123 set_breakpoint_condition (b, p, from_tty);
1124
1125 if (is_breakpoint (b))
1126 update_global_location_list (UGLL_MAY_INSERT);
1127
1128 return;
1129 }
1130
1131 error (_("No breakpoint number %d."), bnum);
1132 }
1133
1134 /* Check that COMMAND do not contain commands that are suitable
1135 only for tracepoints and not suitable for ordinary breakpoints.
1136 Throw if any such commands is found. */
1137
1138 static void
1139 check_no_tracepoint_commands (struct command_line *commands)
1140 {
1141 struct command_line *c;
1142
1143 for (c = commands; c; c = c->next)
1144 {
1145 int i;
1146
1147 if (c->control_type == while_stepping_control)
1148 error (_("The 'while-stepping' command can "
1149 "only be used for tracepoints"));
1150
1151 for (i = 0; i < c->body_count; ++i)
1152 check_no_tracepoint_commands ((c->body_list)[i]);
1153
1154 /* Not that command parsing removes leading whitespace and comment
1155 lines and also empty lines. So, we only need to check for
1156 command directly. */
1157 if (strstr (c->line, "collect ") == c->line)
1158 error (_("The 'collect' command can only be used for tracepoints"));
1159
1160 if (strstr (c->line, "teval ") == c->line)
1161 error (_("The 'teval' command can only be used for tracepoints"));
1162 }
1163 }
1164
1165 struct longjmp_breakpoint : public breakpoint
1166 {
1167 ~longjmp_breakpoint () override;
1168 };
1169
1170 /* Encapsulate tests for different types of tracepoints. */
1171
1172 static bool
1173 is_tracepoint_type (bptype type)
1174 {
1175 return (type == bp_tracepoint
1176 || type == bp_fast_tracepoint
1177 || type == bp_static_tracepoint);
1178 }
1179
1180 static bool
1181 is_longjmp_type (bptype type)
1182 {
1183 return type == bp_longjmp || type == bp_exception;
1184 }
1185
1186 int
1187 is_tracepoint (const struct breakpoint *b)
1188 {
1189 return is_tracepoint_type (b->type);
1190 }
1191
1192 /* Factory function to create an appropriate instance of breakpoint given
1193 TYPE. */
1194
1195 static std::unique_ptr<breakpoint>
1196 new_breakpoint_from_type (bptype type)
1197 {
1198 breakpoint *b;
1199
1200 if (is_tracepoint_type (type))
1201 b = new tracepoint ();
1202 else if (is_longjmp_type (type))
1203 b = new longjmp_breakpoint ();
1204 else
1205 b = new breakpoint ();
1206
1207 return std::unique_ptr<breakpoint> (b);
1208 }
1209
1210 /* A helper function that validates that COMMANDS are valid for a
1211 breakpoint. This function will throw an exception if a problem is
1212 found. */
1213
1214 static void
1215 validate_commands_for_breakpoint (struct breakpoint *b,
1216 struct command_line *commands)
1217 {
1218 if (is_tracepoint (b))
1219 {
1220 struct tracepoint *t = (struct tracepoint *) b;
1221 struct command_line *c;
1222 struct command_line *while_stepping = 0;
1223
1224 /* Reset the while-stepping step count. The previous commands
1225 might have included a while-stepping action, while the new
1226 ones might not. */
1227 t->step_count = 0;
1228
1229 /* We need to verify that each top-level element of commands is
1230 valid for tracepoints, that there's at most one
1231 while-stepping element, and that the while-stepping's body
1232 has valid tracing commands excluding nested while-stepping.
1233 We also need to validate the tracepoint action line in the
1234 context of the tracepoint --- validate_actionline actually
1235 has side effects, like setting the tracepoint's
1236 while-stepping STEP_COUNT, in addition to checking if the
1237 collect/teval actions parse and make sense in the
1238 tracepoint's context. */
1239 for (c = commands; c; c = c->next)
1240 {
1241 if (c->control_type == while_stepping_control)
1242 {
1243 if (b->type == bp_fast_tracepoint)
1244 error (_("The 'while-stepping' command "
1245 "cannot be used for fast tracepoint"));
1246 else if (b->type == bp_static_tracepoint)
1247 error (_("The 'while-stepping' command "
1248 "cannot be used for static tracepoint"));
1249
1250 if (while_stepping)
1251 error (_("The 'while-stepping' command "
1252 "can be used only once"));
1253 else
1254 while_stepping = c;
1255 }
1256
1257 validate_actionline (c->line, b);
1258 }
1259 if (while_stepping)
1260 {
1261 struct command_line *c2;
1262
1263 gdb_assert (while_stepping->body_count == 1);
1264 c2 = while_stepping->body_list[0];
1265 for (; c2; c2 = c2->next)
1266 {
1267 if (c2->control_type == while_stepping_control)
1268 error (_("The 'while-stepping' command cannot be nested"));
1269 }
1270 }
1271 }
1272 else
1273 {
1274 check_no_tracepoint_commands (commands);
1275 }
1276 }
1277
1278 /* Return a vector of all the static tracepoints set at ADDR. The
1279 caller is responsible for releasing the vector. */
1280
1281 VEC(breakpoint_p) *
1282 static_tracepoints_here (CORE_ADDR addr)
1283 {
1284 struct breakpoint *b;
1285 VEC(breakpoint_p) *found = 0;
1286 struct bp_location *loc;
1287
1288 ALL_BREAKPOINTS (b)
1289 if (b->type == bp_static_tracepoint)
1290 {
1291 for (loc = b->loc; loc; loc = loc->next)
1292 if (loc->address == addr)
1293 VEC_safe_push(breakpoint_p, found, b);
1294 }
1295
1296 return found;
1297 }
1298
1299 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1300 validate that only allowed commands are included. */
1301
1302 void
1303 breakpoint_set_commands (struct breakpoint *b,
1304 command_line_up &&commands)
1305 {
1306 validate_commands_for_breakpoint (b, commands.get ());
1307
1308 decref_counted_command_line (&b->commands);
1309 b->commands = alloc_counted_command_line (commands.release ());
1310 observer_notify_breakpoint_modified (b);
1311 }
1312
1313 /* Set the internal `silent' flag on the breakpoint. Note that this
1314 is not the same as the "silent" that may appear in the breakpoint's
1315 commands. */
1316
1317 void
1318 breakpoint_set_silent (struct breakpoint *b, int silent)
1319 {
1320 int old_silent = b->silent;
1321
1322 b->silent = silent;
1323 if (old_silent != silent)
1324 observer_notify_breakpoint_modified (b);
1325 }
1326
1327 /* Set the thread for this breakpoint. If THREAD is -1, make the
1328 breakpoint work for any thread. */
1329
1330 void
1331 breakpoint_set_thread (struct breakpoint *b, int thread)
1332 {
1333 int old_thread = b->thread;
1334
1335 b->thread = thread;
1336 if (old_thread != thread)
1337 observer_notify_breakpoint_modified (b);
1338 }
1339
1340 /* Set the task for this breakpoint. If TASK is 0, make the
1341 breakpoint work for any task. */
1342
1343 void
1344 breakpoint_set_task (struct breakpoint *b, int task)
1345 {
1346 int old_task = b->task;
1347
1348 b->task = task;
1349 if (old_task != task)
1350 observer_notify_breakpoint_modified (b);
1351 }
1352
1353 void
1354 check_tracepoint_command (char *line, void *closure)
1355 {
1356 struct breakpoint *b = (struct breakpoint *) closure;
1357
1358 validate_actionline (line, b);
1359 }
1360
1361 /* A structure used to pass information through
1362 map_breakpoint_numbers. */
1363
1364 struct commands_info
1365 {
1366 /* True if the command was typed at a tty. */
1367 int from_tty;
1368
1369 /* The breakpoint range spec. */
1370 const char *arg;
1371
1372 /* Non-NULL if the body of the commands are being read from this
1373 already-parsed command. */
1374 struct command_line *control;
1375
1376 /* The command lines read from the user, or NULL if they have not
1377 yet been read. */
1378 struct counted_command_line *cmd;
1379 };
1380
1381 /* A callback for map_breakpoint_numbers that sets the commands for
1382 commands_command. */
1383
1384 static void
1385 do_map_commands_command (struct breakpoint *b, void *data)
1386 {
1387 struct commands_info *info = (struct commands_info *) data;
1388
1389 if (info->cmd == NULL)
1390 {
1391 command_line_up l;
1392
1393 if (info->control != NULL)
1394 l = copy_command_lines (info->control->body_list[0]);
1395 else
1396 {
1397 struct cleanup *old_chain;
1398 char *str;
1399
1400 str = xstrprintf (_("Type commands for breakpoint(s) "
1401 "%s, one per line."),
1402 info->arg);
1403
1404 old_chain = make_cleanup (xfree, str);
1405
1406 l = read_command_lines (str,
1407 info->from_tty, 1,
1408 (is_tracepoint (b)
1409 ? check_tracepoint_command : 0),
1410 b);
1411
1412 do_cleanups (old_chain);
1413 }
1414
1415 info->cmd = alloc_counted_command_line (l.release ());
1416 }
1417
1418 /* If a breakpoint was on the list more than once, we don't need to
1419 do anything. */
1420 if (b->commands != info->cmd)
1421 {
1422 validate_commands_for_breakpoint (b, info->cmd->commands);
1423 incref_counted_command_line (info->cmd);
1424 decref_counted_command_line (&b->commands);
1425 b->commands = info->cmd;
1426 observer_notify_breakpoint_modified (b);
1427 }
1428 }
1429
1430 static void
1431 commands_command_1 (const char *arg, int from_tty,
1432 struct command_line *control)
1433 {
1434 struct cleanup *cleanups;
1435 struct commands_info info;
1436
1437 info.from_tty = from_tty;
1438 info.control = control;
1439 info.cmd = NULL;
1440 /* If we read command lines from the user, then `info' will hold an
1441 extra reference to the commands that we must clean up. */
1442 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1443
1444 std::string new_arg;
1445
1446 if (arg == NULL || !*arg)
1447 {
1448 if (breakpoint_count - prev_breakpoint_count > 1)
1449 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1450 breakpoint_count);
1451 else if (breakpoint_count > 0)
1452 new_arg = string_printf ("%d", breakpoint_count);
1453 }
1454 else
1455 new_arg = arg;
1456
1457 info.arg = new_arg.c_str ();
1458
1459 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1460
1461 if (info.cmd == NULL)
1462 error (_("No breakpoints specified."));
1463
1464 do_cleanups (cleanups);
1465 }
1466
1467 static void
1468 commands_command (char *arg, int from_tty)
1469 {
1470 commands_command_1 (arg, from_tty, NULL);
1471 }
1472
1473 /* Like commands_command, but instead of reading the commands from
1474 input stream, takes them from an already parsed command structure.
1475
1476 This is used by cli-script.c to DTRT with breakpoint commands
1477 that are part of if and while bodies. */
1478 enum command_control_type
1479 commands_from_control_command (const char *arg, struct command_line *cmd)
1480 {
1481 commands_command_1 (arg, 0, cmd);
1482 return simple_control;
1483 }
1484
1485 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1486
1487 static int
1488 bp_location_has_shadow (struct bp_location *bl)
1489 {
1490 if (bl->loc_type != bp_loc_software_breakpoint)
1491 return 0;
1492 if (!bl->inserted)
1493 return 0;
1494 if (bl->target_info.shadow_len == 0)
1495 /* BL isn't valid, or doesn't shadow memory. */
1496 return 0;
1497 return 1;
1498 }
1499
1500 /* Update BUF, which is LEN bytes read from the target address
1501 MEMADDR, by replacing a memory breakpoint with its shadowed
1502 contents.
1503
1504 If READBUF is not NULL, this buffer must not overlap with the of
1505 the breakpoint location's shadow_contents buffer. Otherwise, a
1506 failed assertion internal error will be raised. */
1507
1508 static void
1509 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1510 const gdb_byte *writebuf_org,
1511 ULONGEST memaddr, LONGEST len,
1512 struct bp_target_info *target_info,
1513 struct gdbarch *gdbarch)
1514 {
1515 /* Now do full processing of the found relevant range of elements. */
1516 CORE_ADDR bp_addr = 0;
1517 int bp_size = 0;
1518 int bptoffset = 0;
1519
1520 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1521 current_program_space->aspace, 0))
1522 {
1523 /* The breakpoint is inserted in a different address space. */
1524 return;
1525 }
1526
1527 /* Addresses and length of the part of the breakpoint that
1528 we need to copy. */
1529 bp_addr = target_info->placed_address;
1530 bp_size = target_info->shadow_len;
1531
1532 if (bp_addr + bp_size <= memaddr)
1533 {
1534 /* The breakpoint is entirely before the chunk of memory we are
1535 reading. */
1536 return;
1537 }
1538
1539 if (bp_addr >= memaddr + len)
1540 {
1541 /* The breakpoint is entirely after the chunk of memory we are
1542 reading. */
1543 return;
1544 }
1545
1546 /* Offset within shadow_contents. */
1547 if (bp_addr < memaddr)
1548 {
1549 /* Only copy the second part of the breakpoint. */
1550 bp_size -= memaddr - bp_addr;
1551 bptoffset = memaddr - bp_addr;
1552 bp_addr = memaddr;
1553 }
1554
1555 if (bp_addr + bp_size > memaddr + len)
1556 {
1557 /* Only copy the first part of the breakpoint. */
1558 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1559 }
1560
1561 if (readbuf != NULL)
1562 {
1563 /* Verify that the readbuf buffer does not overlap with the
1564 shadow_contents buffer. */
1565 gdb_assert (target_info->shadow_contents >= readbuf + len
1566 || readbuf >= (target_info->shadow_contents
1567 + target_info->shadow_len));
1568
1569 /* Update the read buffer with this inserted breakpoint's
1570 shadow. */
1571 memcpy (readbuf + bp_addr - memaddr,
1572 target_info->shadow_contents + bptoffset, bp_size);
1573 }
1574 else
1575 {
1576 const unsigned char *bp;
1577 CORE_ADDR addr = target_info->reqstd_address;
1578 int placed_size;
1579
1580 /* Update the shadow with what we want to write to memory. */
1581 memcpy (target_info->shadow_contents + bptoffset,
1582 writebuf_org + bp_addr - memaddr, bp_size);
1583
1584 /* Determine appropriate breakpoint contents and size for this
1585 address. */
1586 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1587
1588 /* Update the final write buffer with this inserted
1589 breakpoint's INSN. */
1590 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1591 }
1592 }
1593
1594 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1595 by replacing any memory breakpoints with their shadowed contents.
1596
1597 If READBUF is not NULL, this buffer must not overlap with any of
1598 the breakpoint location's shadow_contents buffers. Otherwise,
1599 a failed assertion internal error will be raised.
1600
1601 The range of shadowed area by each bp_location is:
1602 bl->address - bp_locations_placed_address_before_address_max
1603 up to bl->address + bp_locations_shadow_len_after_address_max
1604 The range we were requested to resolve shadows for is:
1605 memaddr ... memaddr + len
1606 Thus the safe cutoff boundaries for performance optimization are
1607 memaddr + len <= (bl->address
1608 - bp_locations_placed_address_before_address_max)
1609 and:
1610 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1611
1612 void
1613 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1614 const gdb_byte *writebuf_org,
1615 ULONGEST memaddr, LONGEST len)
1616 {
1617 /* Left boundary, right boundary and median element of our binary
1618 search. */
1619 unsigned bc_l, bc_r, bc;
1620
1621 /* Find BC_L which is a leftmost element which may affect BUF
1622 content. It is safe to report lower value but a failure to
1623 report higher one. */
1624
1625 bc_l = 0;
1626 bc_r = bp_locations_count;
1627 while (bc_l + 1 < bc_r)
1628 {
1629 struct bp_location *bl;
1630
1631 bc = (bc_l + bc_r) / 2;
1632 bl = bp_locations[bc];
1633
1634 /* Check first BL->ADDRESS will not overflow due to the added
1635 constant. Then advance the left boundary only if we are sure
1636 the BC element can in no way affect the BUF content (MEMADDR
1637 to MEMADDR + LEN range).
1638
1639 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1640 offset so that we cannot miss a breakpoint with its shadow
1641 range tail still reaching MEMADDR. */
1642
1643 if ((bl->address + bp_locations_shadow_len_after_address_max
1644 >= bl->address)
1645 && (bl->address + bp_locations_shadow_len_after_address_max
1646 <= memaddr))
1647 bc_l = bc;
1648 else
1649 bc_r = bc;
1650 }
1651
1652 /* Due to the binary search above, we need to make sure we pick the
1653 first location that's at BC_L's address. E.g., if there are
1654 multiple locations at the same address, BC_L may end up pointing
1655 at a duplicate location, and miss the "master"/"inserted"
1656 location. Say, given locations L1, L2 and L3 at addresses A and
1657 B:
1658
1659 L1@A, L2@A, L3@B, ...
1660
1661 BC_L could end up pointing at location L2, while the "master"
1662 location could be L1. Since the `loc->inserted' flag is only set
1663 on "master" locations, we'd forget to restore the shadow of L1
1664 and L2. */
1665 while (bc_l > 0
1666 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1667 bc_l--;
1668
1669 /* Now do full processing of the found relevant range of elements. */
1670
1671 for (bc = bc_l; bc < bp_locations_count; bc++)
1672 {
1673 struct bp_location *bl = bp_locations[bc];
1674
1675 /* bp_location array has BL->OWNER always non-NULL. */
1676 if (bl->owner->type == bp_none)
1677 warning (_("reading through apparently deleted breakpoint #%d?"),
1678 bl->owner->number);
1679
1680 /* Performance optimization: any further element can no longer affect BUF
1681 content. */
1682
1683 if (bl->address >= bp_locations_placed_address_before_address_max
1684 && memaddr + len <= (bl->address
1685 - bp_locations_placed_address_before_address_max))
1686 break;
1687
1688 if (!bp_location_has_shadow (bl))
1689 continue;
1690
1691 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1692 memaddr, len, &bl->target_info, bl->gdbarch);
1693 }
1694 }
1695
1696 \f
1697
1698 /* Return true if BPT is either a software breakpoint or a hardware
1699 breakpoint. */
1700
1701 int
1702 is_breakpoint (const struct breakpoint *bpt)
1703 {
1704 return (bpt->type == bp_breakpoint
1705 || bpt->type == bp_hardware_breakpoint
1706 || bpt->type == bp_dprintf);
1707 }
1708
1709 /* Return true if BPT is of any hardware watchpoint kind. */
1710
1711 static int
1712 is_hardware_watchpoint (const struct breakpoint *bpt)
1713 {
1714 return (bpt->type == bp_hardware_watchpoint
1715 || bpt->type == bp_read_watchpoint
1716 || bpt->type == bp_access_watchpoint);
1717 }
1718
1719 /* Return true if BPT is of any watchpoint kind, hardware or
1720 software. */
1721
1722 int
1723 is_watchpoint (const struct breakpoint *bpt)
1724 {
1725 return (is_hardware_watchpoint (bpt)
1726 || bpt->type == bp_watchpoint);
1727 }
1728
1729 /* Returns true if the current thread and its running state are safe
1730 to evaluate or update watchpoint B. Watchpoints on local
1731 expressions need to be evaluated in the context of the thread that
1732 was current when the watchpoint was created, and, that thread needs
1733 to be stopped to be able to select the correct frame context.
1734 Watchpoints on global expressions can be evaluated on any thread,
1735 and in any state. It is presently left to the target allowing
1736 memory accesses when threads are running. */
1737
1738 static int
1739 watchpoint_in_thread_scope (struct watchpoint *b)
1740 {
1741 return (b->pspace == current_program_space
1742 && (ptid_equal (b->watchpoint_thread, null_ptid)
1743 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1744 && !is_executing (inferior_ptid))));
1745 }
1746
1747 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1748 associated bp_watchpoint_scope breakpoint. */
1749
1750 static void
1751 watchpoint_del_at_next_stop (struct watchpoint *w)
1752 {
1753 if (w->related_breakpoint != w)
1754 {
1755 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1756 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1757 w->related_breakpoint->disposition = disp_del_at_next_stop;
1758 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1759 w->related_breakpoint = w;
1760 }
1761 w->disposition = disp_del_at_next_stop;
1762 }
1763
1764 /* Extract a bitfield value from value VAL using the bit parameters contained in
1765 watchpoint W. */
1766
1767 static struct value *
1768 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1769 {
1770 struct value *bit_val;
1771
1772 if (val == NULL)
1773 return NULL;
1774
1775 bit_val = allocate_value (value_type (val));
1776
1777 unpack_value_bitfield (bit_val,
1778 w->val_bitpos,
1779 w->val_bitsize,
1780 value_contents_for_printing (val),
1781 value_offset (val),
1782 val);
1783
1784 return bit_val;
1785 }
1786
1787 /* Allocate a dummy location and add it to B, which must be a software
1788 watchpoint. This is required because even if a software watchpoint
1789 is not watching any memory, bpstat_stop_status requires a location
1790 to be able to report stops. */
1791
1792 static void
1793 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1794 struct program_space *pspace)
1795 {
1796 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1797
1798 b->loc = allocate_bp_location (b);
1799 b->loc->pspace = pspace;
1800 b->loc->address = -1;
1801 b->loc->length = -1;
1802 }
1803
1804 /* Returns true if B is a software watchpoint that is not watching any
1805 memory (e.g., "watch $pc"). */
1806
1807 static int
1808 is_no_memory_software_watchpoint (struct breakpoint *b)
1809 {
1810 return (b->type == bp_watchpoint
1811 && b->loc != NULL
1812 && b->loc->next == NULL
1813 && b->loc->address == -1
1814 && b->loc->length == -1);
1815 }
1816
1817 /* Assuming that B is a watchpoint:
1818 - Reparse watchpoint expression, if REPARSE is non-zero
1819 - Evaluate expression and store the result in B->val
1820 - Evaluate the condition if there is one, and store the result
1821 in b->loc->cond.
1822 - Update the list of values that must be watched in B->loc.
1823
1824 If the watchpoint disposition is disp_del_at_next_stop, then do
1825 nothing. If this is local watchpoint that is out of scope, delete
1826 it.
1827
1828 Even with `set breakpoint always-inserted on' the watchpoints are
1829 removed + inserted on each stop here. Normal breakpoints must
1830 never be removed because they might be missed by a running thread
1831 when debugging in non-stop mode. On the other hand, hardware
1832 watchpoints (is_hardware_watchpoint; processed here) are specific
1833 to each LWP since they are stored in each LWP's hardware debug
1834 registers. Therefore, such LWP must be stopped first in order to
1835 be able to modify its hardware watchpoints.
1836
1837 Hardware watchpoints must be reset exactly once after being
1838 presented to the user. It cannot be done sooner, because it would
1839 reset the data used to present the watchpoint hit to the user. And
1840 it must not be done later because it could display the same single
1841 watchpoint hit during multiple GDB stops. Note that the latter is
1842 relevant only to the hardware watchpoint types bp_read_watchpoint
1843 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1844 not user-visible - its hit is suppressed if the memory content has
1845 not changed.
1846
1847 The following constraints influence the location where we can reset
1848 hardware watchpoints:
1849
1850 * target_stopped_by_watchpoint and target_stopped_data_address are
1851 called several times when GDB stops.
1852
1853 [linux]
1854 * Multiple hardware watchpoints can be hit at the same time,
1855 causing GDB to stop. GDB only presents one hardware watchpoint
1856 hit at a time as the reason for stopping, and all the other hits
1857 are presented later, one after the other, each time the user
1858 requests the execution to be resumed. Execution is not resumed
1859 for the threads still having pending hit event stored in
1860 LWP_INFO->STATUS. While the watchpoint is already removed from
1861 the inferior on the first stop the thread hit event is kept being
1862 reported from its cached value by linux_nat_stopped_data_address
1863 until the real thread resume happens after the watchpoint gets
1864 presented and thus its LWP_INFO->STATUS gets reset.
1865
1866 Therefore the hardware watchpoint hit can get safely reset on the
1867 watchpoint removal from inferior. */
1868
1869 static void
1870 update_watchpoint (struct watchpoint *b, int reparse)
1871 {
1872 int within_current_scope;
1873 struct frame_id saved_frame_id;
1874 int frame_saved;
1875
1876 /* If this is a local watchpoint, we only want to check if the
1877 watchpoint frame is in scope if the current thread is the thread
1878 that was used to create the watchpoint. */
1879 if (!watchpoint_in_thread_scope (b))
1880 return;
1881
1882 if (b->disposition == disp_del_at_next_stop)
1883 return;
1884
1885 frame_saved = 0;
1886
1887 /* Determine if the watchpoint is within scope. */
1888 if (b->exp_valid_block == NULL)
1889 within_current_scope = 1;
1890 else
1891 {
1892 struct frame_info *fi = get_current_frame ();
1893 struct gdbarch *frame_arch = get_frame_arch (fi);
1894 CORE_ADDR frame_pc = get_frame_pc (fi);
1895
1896 /* If we're at a point where the stack has been destroyed
1897 (e.g. in a function epilogue), unwinding may not work
1898 properly. Do not attempt to recreate locations at this
1899 point. See similar comments in watchpoint_check. */
1900 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1901 return;
1902
1903 /* Save the current frame's ID so we can restore it after
1904 evaluating the watchpoint expression on its own frame. */
1905 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1906 took a frame parameter, so that we didn't have to change the
1907 selected frame. */
1908 frame_saved = 1;
1909 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1910
1911 fi = frame_find_by_id (b->watchpoint_frame);
1912 within_current_scope = (fi != NULL);
1913 if (within_current_scope)
1914 select_frame (fi);
1915 }
1916
1917 /* We don't free locations. They are stored in the bp_location array
1918 and update_global_location_list will eventually delete them and
1919 remove breakpoints if needed. */
1920 b->loc = NULL;
1921
1922 if (within_current_scope && reparse)
1923 {
1924 const char *s;
1925
1926 b->exp.reset ();
1927 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1928 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1929 /* If the meaning of expression itself changed, the old value is
1930 no longer relevant. We don't want to report a watchpoint hit
1931 to the user when the old value and the new value may actually
1932 be completely different objects. */
1933 value_free (b->val);
1934 b->val = NULL;
1935 b->val_valid = 0;
1936
1937 /* Note that unlike with breakpoints, the watchpoint's condition
1938 expression is stored in the breakpoint object, not in the
1939 locations (re)created below. */
1940 if (b->cond_string != NULL)
1941 {
1942 b->cond_exp.reset ();
1943
1944 s = b->cond_string;
1945 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1946 }
1947 }
1948
1949 /* If we failed to parse the expression, for example because
1950 it refers to a global variable in a not-yet-loaded shared library,
1951 don't try to insert watchpoint. We don't automatically delete
1952 such watchpoint, though, since failure to parse expression
1953 is different from out-of-scope watchpoint. */
1954 if (!target_has_execution)
1955 {
1956 /* Without execution, memory can't change. No use to try and
1957 set watchpoint locations. The watchpoint will be reset when
1958 the target gains execution, through breakpoint_re_set. */
1959 if (!can_use_hw_watchpoints)
1960 {
1961 if (b->ops->works_in_software_mode (b))
1962 b->type = bp_watchpoint;
1963 else
1964 error (_("Can't set read/access watchpoint when "
1965 "hardware watchpoints are disabled."));
1966 }
1967 }
1968 else if (within_current_scope && b->exp)
1969 {
1970 int pc = 0;
1971 struct value *val_chain, *v, *result, *next;
1972 struct program_space *frame_pspace;
1973
1974 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1975
1976 /* Avoid setting b->val if it's already set. The meaning of
1977 b->val is 'the last value' user saw, and we should update
1978 it only if we reported that last value to user. As it
1979 happens, the code that reports it updates b->val directly.
1980 We don't keep track of the memory value for masked
1981 watchpoints. */
1982 if (!b->val_valid && !is_masked_watchpoint (b))
1983 {
1984 if (b->val_bitsize != 0)
1985 {
1986 v = extract_bitfield_from_watchpoint_value (b, v);
1987 if (v != NULL)
1988 release_value (v);
1989 }
1990 b->val = v;
1991 b->val_valid = 1;
1992 }
1993
1994 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1995
1996 /* Look at each value on the value chain. */
1997 for (v = val_chain; v; v = value_next (v))
1998 {
1999 /* If it's a memory location, and GDB actually needed
2000 its contents to evaluate the expression, then we
2001 must watch it. If the first value returned is
2002 still lazy, that means an error occurred reading it;
2003 watch it anyway in case it becomes readable. */
2004 if (VALUE_LVAL (v) == lval_memory
2005 && (v == val_chain || ! value_lazy (v)))
2006 {
2007 struct type *vtype = check_typedef (value_type (v));
2008
2009 /* We only watch structs and arrays if user asked
2010 for it explicitly, never if they just happen to
2011 appear in the middle of some value chain. */
2012 if (v == result
2013 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2014 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2015 {
2016 CORE_ADDR addr;
2017 enum target_hw_bp_type type;
2018 struct bp_location *loc, **tmp;
2019 int bitpos = 0, bitsize = 0;
2020
2021 if (value_bitsize (v) != 0)
2022 {
2023 /* Extract the bit parameters out from the bitfield
2024 sub-expression. */
2025 bitpos = value_bitpos (v);
2026 bitsize = value_bitsize (v);
2027 }
2028 else if (v == result && b->val_bitsize != 0)
2029 {
2030 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2031 lvalue whose bit parameters are saved in the fields
2032 VAL_BITPOS and VAL_BITSIZE. */
2033 bitpos = b->val_bitpos;
2034 bitsize = b->val_bitsize;
2035 }
2036
2037 addr = value_address (v);
2038 if (bitsize != 0)
2039 {
2040 /* Skip the bytes that don't contain the bitfield. */
2041 addr += bitpos / 8;
2042 }
2043
2044 type = hw_write;
2045 if (b->type == bp_read_watchpoint)
2046 type = hw_read;
2047 else if (b->type == bp_access_watchpoint)
2048 type = hw_access;
2049
2050 loc = allocate_bp_location (b);
2051 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2052 ;
2053 *tmp = loc;
2054 loc->gdbarch = get_type_arch (value_type (v));
2055
2056 loc->pspace = frame_pspace;
2057 loc->address = addr;
2058
2059 if (bitsize != 0)
2060 {
2061 /* Just cover the bytes that make up the bitfield. */
2062 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2063 }
2064 else
2065 loc->length = TYPE_LENGTH (value_type (v));
2066
2067 loc->watchpoint_type = type;
2068 }
2069 }
2070 }
2071
2072 /* Change the type of breakpoint between hardware assisted or
2073 an ordinary watchpoint depending on the hardware support
2074 and free hardware slots. REPARSE is set when the inferior
2075 is started. */
2076 if (reparse)
2077 {
2078 int reg_cnt;
2079 enum bp_loc_type loc_type;
2080 struct bp_location *bl;
2081
2082 reg_cnt = can_use_hardware_watchpoint (val_chain);
2083
2084 if (reg_cnt)
2085 {
2086 int i, target_resources_ok, other_type_used;
2087 enum bptype type;
2088
2089 /* Use an exact watchpoint when there's only one memory region to be
2090 watched, and only one debug register is needed to watch it. */
2091 b->exact = target_exact_watchpoints && reg_cnt == 1;
2092
2093 /* We need to determine how many resources are already
2094 used for all other hardware watchpoints plus this one
2095 to see if we still have enough resources to also fit
2096 this watchpoint in as well. */
2097
2098 /* If this is a software watchpoint, we try to turn it
2099 to a hardware one -- count resources as if B was of
2100 hardware watchpoint type. */
2101 type = b->type;
2102 if (type == bp_watchpoint)
2103 type = bp_hardware_watchpoint;
2104
2105 /* This watchpoint may or may not have been placed on
2106 the list yet at this point (it won't be in the list
2107 if we're trying to create it for the first time,
2108 through watch_command), so always account for it
2109 manually. */
2110
2111 /* Count resources used by all watchpoints except B. */
2112 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2113
2114 /* Add in the resources needed for B. */
2115 i += hw_watchpoint_use_count (b);
2116
2117 target_resources_ok
2118 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2119 if (target_resources_ok <= 0)
2120 {
2121 int sw_mode = b->ops->works_in_software_mode (b);
2122
2123 if (target_resources_ok == 0 && !sw_mode)
2124 error (_("Target does not support this type of "
2125 "hardware watchpoint."));
2126 else if (target_resources_ok < 0 && !sw_mode)
2127 error (_("There are not enough available hardware "
2128 "resources for this watchpoint."));
2129
2130 /* Downgrade to software watchpoint. */
2131 b->type = bp_watchpoint;
2132 }
2133 else
2134 {
2135 /* If this was a software watchpoint, we've just
2136 found we have enough resources to turn it to a
2137 hardware watchpoint. Otherwise, this is a
2138 nop. */
2139 b->type = type;
2140 }
2141 }
2142 else if (!b->ops->works_in_software_mode (b))
2143 {
2144 if (!can_use_hw_watchpoints)
2145 error (_("Can't set read/access watchpoint when "
2146 "hardware watchpoints are disabled."));
2147 else
2148 error (_("Expression cannot be implemented with "
2149 "read/access watchpoint."));
2150 }
2151 else
2152 b->type = bp_watchpoint;
2153
2154 loc_type = (b->type == bp_watchpoint? bp_loc_other
2155 : bp_loc_hardware_watchpoint);
2156 for (bl = b->loc; bl; bl = bl->next)
2157 bl->loc_type = loc_type;
2158 }
2159
2160 for (v = val_chain; v; v = next)
2161 {
2162 next = value_next (v);
2163 if (v != b->val)
2164 value_free (v);
2165 }
2166
2167 /* If a software watchpoint is not watching any memory, then the
2168 above left it without any location set up. But,
2169 bpstat_stop_status requires a location to be able to report
2170 stops, so make sure there's at least a dummy one. */
2171 if (b->type == bp_watchpoint && b->loc == NULL)
2172 software_watchpoint_add_no_memory_location (b, frame_pspace);
2173 }
2174 else if (!within_current_scope)
2175 {
2176 printf_filtered (_("\
2177 Watchpoint %d deleted because the program has left the block\n\
2178 in which its expression is valid.\n"),
2179 b->number);
2180 watchpoint_del_at_next_stop (b);
2181 }
2182
2183 /* Restore the selected frame. */
2184 if (frame_saved)
2185 select_frame (frame_find_by_id (saved_frame_id));
2186 }
2187
2188
2189 /* Returns 1 iff breakpoint location should be
2190 inserted in the inferior. We don't differentiate the type of BL's owner
2191 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2192 breakpoint_ops is not defined, because in insert_bp_location,
2193 tracepoint's insert_location will not be called. */
2194 static int
2195 should_be_inserted (struct bp_location *bl)
2196 {
2197 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2198 return 0;
2199
2200 if (bl->owner->disposition == disp_del_at_next_stop)
2201 return 0;
2202
2203 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2204 return 0;
2205
2206 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2207 return 0;
2208
2209 /* This is set for example, when we're attached to the parent of a
2210 vfork, and have detached from the child. The child is running
2211 free, and we expect it to do an exec or exit, at which point the
2212 OS makes the parent schedulable again (and the target reports
2213 that the vfork is done). Until the child is done with the shared
2214 memory region, do not insert breakpoints in the parent, otherwise
2215 the child could still trip on the parent's breakpoints. Since
2216 the parent is blocked anyway, it won't miss any breakpoint. */
2217 if (bl->pspace->breakpoints_not_allowed)
2218 return 0;
2219
2220 /* Don't insert a breakpoint if we're trying to step past its
2221 location, except if the breakpoint is a single-step breakpoint,
2222 and the breakpoint's thread is the thread which is stepping past
2223 a breakpoint. */
2224 if ((bl->loc_type == bp_loc_software_breakpoint
2225 || bl->loc_type == bp_loc_hardware_breakpoint)
2226 && stepping_past_instruction_at (bl->pspace->aspace,
2227 bl->address)
2228 /* The single-step breakpoint may be inserted at the location
2229 we're trying to step if the instruction branches to itself.
2230 However, the instruction won't be executed at all and it may
2231 break the semantics of the instruction, for example, the
2232 instruction is a conditional branch or updates some flags.
2233 We can't fix it unless GDB is able to emulate the instruction
2234 or switch to displaced stepping. */
2235 && !(bl->owner->type == bp_single_step
2236 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2237 {
2238 if (debug_infrun)
2239 {
2240 fprintf_unfiltered (gdb_stdlog,
2241 "infrun: skipping breakpoint: "
2242 "stepping past insn at: %s\n",
2243 paddress (bl->gdbarch, bl->address));
2244 }
2245 return 0;
2246 }
2247
2248 /* Don't insert watchpoints if we're trying to step past the
2249 instruction that triggered one. */
2250 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2251 && stepping_past_nonsteppable_watchpoint ())
2252 {
2253 if (debug_infrun)
2254 {
2255 fprintf_unfiltered (gdb_stdlog,
2256 "infrun: stepping past non-steppable watchpoint. "
2257 "skipping watchpoint at %s:%d\n",
2258 paddress (bl->gdbarch, bl->address),
2259 bl->length);
2260 }
2261 return 0;
2262 }
2263
2264 return 1;
2265 }
2266
2267 /* Same as should_be_inserted but does the check assuming
2268 that the location is not duplicated. */
2269
2270 static int
2271 unduplicated_should_be_inserted (struct bp_location *bl)
2272 {
2273 int result;
2274 const int save_duplicate = bl->duplicate;
2275
2276 bl->duplicate = 0;
2277 result = should_be_inserted (bl);
2278 bl->duplicate = save_duplicate;
2279 return result;
2280 }
2281
2282 /* Parses a conditional described by an expression COND into an
2283 agent expression bytecode suitable for evaluation
2284 by the bytecode interpreter. Return NULL if there was
2285 any error during parsing. */
2286
2287 static agent_expr_up
2288 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2289 {
2290 if (cond == NULL)
2291 return NULL;
2292
2293 agent_expr_up aexpr;
2294
2295 /* We don't want to stop processing, so catch any errors
2296 that may show up. */
2297 TRY
2298 {
2299 aexpr = gen_eval_for_expr (scope, cond);
2300 }
2301
2302 CATCH (ex, RETURN_MASK_ERROR)
2303 {
2304 /* If we got here, it means the condition could not be parsed to a valid
2305 bytecode expression and thus can't be evaluated on the target's side.
2306 It's no use iterating through the conditions. */
2307 }
2308 END_CATCH
2309
2310 /* We have a valid agent expression. */
2311 return aexpr;
2312 }
2313
2314 /* Based on location BL, create a list of breakpoint conditions to be
2315 passed on to the target. If we have duplicated locations with different
2316 conditions, we will add such conditions to the list. The idea is that the
2317 target will evaluate the list of conditions and will only notify GDB when
2318 one of them is true. */
2319
2320 static void
2321 build_target_condition_list (struct bp_location *bl)
2322 {
2323 struct bp_location **locp = NULL, **loc2p;
2324 int null_condition_or_parse_error = 0;
2325 int modified = bl->needs_update;
2326 struct bp_location *loc;
2327
2328 /* Release conditions left over from a previous insert. */
2329 bl->target_info.conditions.clear ();
2330
2331 /* This is only meaningful if the target is
2332 evaluating conditions and if the user has
2333 opted for condition evaluation on the target's
2334 side. */
2335 if (gdb_evaluates_breakpoint_condition_p ()
2336 || !target_supports_evaluation_of_breakpoint_conditions ())
2337 return;
2338
2339 /* Do a first pass to check for locations with no assigned
2340 conditions or conditions that fail to parse to a valid agent expression
2341 bytecode. If any of these happen, then it's no use to send conditions
2342 to the target since this location will always trigger and generate a
2343 response back to GDB. */
2344 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2345 {
2346 loc = (*loc2p);
2347 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2348 {
2349 if (modified)
2350 {
2351 /* Re-parse the conditions since something changed. In that
2352 case we already freed the condition bytecodes (see
2353 force_breakpoint_reinsertion). We just
2354 need to parse the condition to bytecodes again. */
2355 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2356 loc->cond.get ());
2357 }
2358
2359 /* If we have a NULL bytecode expression, it means something
2360 went wrong or we have a null condition expression. */
2361 if (!loc->cond_bytecode)
2362 {
2363 null_condition_or_parse_error = 1;
2364 break;
2365 }
2366 }
2367 }
2368
2369 /* If any of these happened, it means we will have to evaluate the conditions
2370 for the location's address on gdb's side. It is no use keeping bytecodes
2371 for all the other duplicate locations, thus we free all of them here.
2372
2373 This is so we have a finer control over which locations' conditions are
2374 being evaluated by GDB or the remote stub. */
2375 if (null_condition_or_parse_error)
2376 {
2377 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2378 {
2379 loc = (*loc2p);
2380 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2381 {
2382 /* Only go as far as the first NULL bytecode is
2383 located. */
2384 if (!loc->cond_bytecode)
2385 return;
2386
2387 loc->cond_bytecode.reset ();
2388 }
2389 }
2390 }
2391
2392 /* No NULL conditions or failed bytecode generation. Build a condition list
2393 for this location's address. */
2394 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2395 {
2396 loc = (*loc2p);
2397 if (loc->cond
2398 && is_breakpoint (loc->owner)
2399 && loc->pspace->num == bl->pspace->num
2400 && loc->owner->enable_state == bp_enabled
2401 && loc->enabled)
2402 {
2403 /* Add the condition to the vector. This will be used later
2404 to send the conditions to the target. */
2405 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2406 }
2407 }
2408
2409 return;
2410 }
2411
2412 /* Parses a command described by string CMD into an agent expression
2413 bytecode suitable for evaluation by the bytecode interpreter.
2414 Return NULL if there was any error during parsing. */
2415
2416 static agent_expr_up
2417 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2418 {
2419 struct cleanup *old_cleanups = 0;
2420 struct expression **argvec;
2421 const char *cmdrest;
2422 const char *format_start, *format_end;
2423 struct format_piece *fpieces;
2424 int nargs;
2425 struct gdbarch *gdbarch = get_current_arch ();
2426
2427 if (cmd == NULL)
2428 return NULL;
2429
2430 cmdrest = cmd;
2431
2432 if (*cmdrest == ',')
2433 ++cmdrest;
2434 cmdrest = skip_spaces (cmdrest);
2435
2436 if (*cmdrest++ != '"')
2437 error (_("No format string following the location"));
2438
2439 format_start = cmdrest;
2440
2441 fpieces = parse_format_string (&cmdrest);
2442
2443 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2444
2445 format_end = cmdrest;
2446
2447 if (*cmdrest++ != '"')
2448 error (_("Bad format string, non-terminated '\"'."));
2449
2450 cmdrest = skip_spaces (cmdrest);
2451
2452 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2453 error (_("Invalid argument syntax"));
2454
2455 if (*cmdrest == ',')
2456 cmdrest++;
2457 cmdrest = skip_spaces (cmdrest);
2458
2459 /* For each argument, make an expression. */
2460
2461 argvec = (struct expression **) alloca (strlen (cmd)
2462 * sizeof (struct expression *));
2463
2464 nargs = 0;
2465 while (*cmdrest != '\0')
2466 {
2467 const char *cmd1;
2468
2469 cmd1 = cmdrest;
2470 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2471 argvec[nargs++] = expr.release ();
2472 cmdrest = cmd1;
2473 if (*cmdrest == ',')
2474 ++cmdrest;
2475 }
2476
2477 agent_expr_up aexpr;
2478
2479 /* We don't want to stop processing, so catch any errors
2480 that may show up. */
2481 TRY
2482 {
2483 aexpr = gen_printf (scope, gdbarch, 0, 0,
2484 format_start, format_end - format_start,
2485 fpieces, nargs, argvec);
2486 }
2487 CATCH (ex, RETURN_MASK_ERROR)
2488 {
2489 /* If we got here, it means the command could not be parsed to a valid
2490 bytecode expression and thus can't be evaluated on the target's side.
2491 It's no use iterating through the other commands. */
2492 }
2493 END_CATCH
2494
2495 do_cleanups (old_cleanups);
2496
2497 /* We have a valid agent expression, return it. */
2498 return aexpr;
2499 }
2500
2501 /* Based on location BL, create a list of breakpoint commands to be
2502 passed on to the target. If we have duplicated locations with
2503 different commands, we will add any such to the list. */
2504
2505 static void
2506 build_target_command_list (struct bp_location *bl)
2507 {
2508 struct bp_location **locp = NULL, **loc2p;
2509 int null_command_or_parse_error = 0;
2510 int modified = bl->needs_update;
2511 struct bp_location *loc;
2512
2513 /* Clear commands left over from a previous insert. */
2514 bl->target_info.tcommands.clear ();
2515
2516 if (!target_can_run_breakpoint_commands ())
2517 return;
2518
2519 /* For now, limit to agent-style dprintf breakpoints. */
2520 if (dprintf_style != dprintf_style_agent)
2521 return;
2522
2523 /* For now, if we have any duplicate location that isn't a dprintf,
2524 don't install the target-side commands, as that would make the
2525 breakpoint not be reported to the core, and we'd lose
2526 control. */
2527 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2528 {
2529 loc = (*loc2p);
2530 if (is_breakpoint (loc->owner)
2531 && loc->pspace->num == bl->pspace->num
2532 && loc->owner->type != bp_dprintf)
2533 return;
2534 }
2535
2536 /* Do a first pass to check for locations with no assigned
2537 conditions or conditions that fail to parse to a valid agent expression
2538 bytecode. If any of these happen, then it's no use to send conditions
2539 to the target since this location will always trigger and generate a
2540 response back to GDB. */
2541 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2542 {
2543 loc = (*loc2p);
2544 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2545 {
2546 if (modified)
2547 {
2548 /* Re-parse the commands since something changed. In that
2549 case we already freed the command bytecodes (see
2550 force_breakpoint_reinsertion). We just
2551 need to parse the command to bytecodes again. */
2552 loc->cmd_bytecode
2553 = parse_cmd_to_aexpr (bl->address,
2554 loc->owner->extra_string);
2555 }
2556
2557 /* If we have a NULL bytecode expression, it means something
2558 went wrong or we have a null command expression. */
2559 if (!loc->cmd_bytecode)
2560 {
2561 null_command_or_parse_error = 1;
2562 break;
2563 }
2564 }
2565 }
2566
2567 /* If anything failed, then we're not doing target-side commands,
2568 and so clean up. */
2569 if (null_command_or_parse_error)
2570 {
2571 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2572 {
2573 loc = (*loc2p);
2574 if (is_breakpoint (loc->owner)
2575 && loc->pspace->num == bl->pspace->num)
2576 {
2577 /* Only go as far as the first NULL bytecode is
2578 located. */
2579 if (loc->cmd_bytecode == NULL)
2580 return;
2581
2582 loc->cmd_bytecode.reset ();
2583 }
2584 }
2585 }
2586
2587 /* No NULL commands or failed bytecode generation. Build a command list
2588 for this location's address. */
2589 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2590 {
2591 loc = (*loc2p);
2592 if (loc->owner->extra_string
2593 && is_breakpoint (loc->owner)
2594 && loc->pspace->num == bl->pspace->num
2595 && loc->owner->enable_state == bp_enabled
2596 && loc->enabled)
2597 {
2598 /* Add the command to the vector. This will be used later
2599 to send the commands to the target. */
2600 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2601 }
2602 }
2603
2604 bl->target_info.persist = 0;
2605 /* Maybe flag this location as persistent. */
2606 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2607 bl->target_info.persist = 1;
2608 }
2609
2610 /* Return the kind of breakpoint on address *ADDR. Get the kind
2611 of breakpoint according to ADDR except single-step breakpoint.
2612 Get the kind of single-step breakpoint according to the current
2613 registers state. */
2614
2615 static int
2616 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2617 {
2618 if (bl->owner->type == bp_single_step)
2619 {
2620 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2621 struct regcache *regcache;
2622
2623 regcache = get_thread_regcache (thr->ptid);
2624
2625 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2626 regcache, addr);
2627 }
2628 else
2629 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2630 }
2631
2632 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2633 location. Any error messages are printed to TMP_ERROR_STREAM; and
2634 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2635 Returns 0 for success, 1 if the bp_location type is not supported or
2636 -1 for failure.
2637
2638 NOTE drow/2003-09-09: This routine could be broken down to an
2639 object-style method for each breakpoint or catchpoint type. */
2640 static int
2641 insert_bp_location (struct bp_location *bl,
2642 struct ui_file *tmp_error_stream,
2643 int *disabled_breaks,
2644 int *hw_breakpoint_error,
2645 int *hw_bp_error_explained_already)
2646 {
2647 enum errors bp_err = GDB_NO_ERROR;
2648 const char *bp_err_message = NULL;
2649
2650 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2651 return 0;
2652
2653 /* Note we don't initialize bl->target_info, as that wipes out
2654 the breakpoint location's shadow_contents if the breakpoint
2655 is still inserted at that location. This in turn breaks
2656 target_read_memory which depends on these buffers when
2657 a memory read is requested at the breakpoint location:
2658 Once the target_info has been wiped, we fail to see that
2659 we have a breakpoint inserted at that address and thus
2660 read the breakpoint instead of returning the data saved in
2661 the breakpoint location's shadow contents. */
2662 bl->target_info.reqstd_address = bl->address;
2663 bl->target_info.placed_address_space = bl->pspace->aspace;
2664 bl->target_info.length = bl->length;
2665
2666 /* When working with target-side conditions, we must pass all the conditions
2667 for the same breakpoint address down to the target since GDB will not
2668 insert those locations. With a list of breakpoint conditions, the target
2669 can decide when to stop and notify GDB. */
2670
2671 if (is_breakpoint (bl->owner))
2672 {
2673 build_target_condition_list (bl);
2674 build_target_command_list (bl);
2675 /* Reset the modification marker. */
2676 bl->needs_update = 0;
2677 }
2678
2679 if (bl->loc_type == bp_loc_software_breakpoint
2680 || bl->loc_type == bp_loc_hardware_breakpoint)
2681 {
2682 if (bl->owner->type != bp_hardware_breakpoint)
2683 {
2684 /* If the explicitly specified breakpoint type
2685 is not hardware breakpoint, check the memory map to see
2686 if the breakpoint address is in read only memory or not.
2687
2688 Two important cases are:
2689 - location type is not hardware breakpoint, memory
2690 is readonly. We change the type of the location to
2691 hardware breakpoint.
2692 - location type is hardware breakpoint, memory is
2693 read-write. This means we've previously made the
2694 location hardware one, but then the memory map changed,
2695 so we undo.
2696
2697 When breakpoints are removed, remove_breakpoints will use
2698 location types we've just set here, the only possible
2699 problem is that memory map has changed during running
2700 program, but it's not going to work anyway with current
2701 gdb. */
2702 struct mem_region *mr
2703 = lookup_mem_region (bl->target_info.reqstd_address);
2704
2705 if (mr)
2706 {
2707 if (automatic_hardware_breakpoints)
2708 {
2709 enum bp_loc_type new_type;
2710
2711 if (mr->attrib.mode != MEM_RW)
2712 new_type = bp_loc_hardware_breakpoint;
2713 else
2714 new_type = bp_loc_software_breakpoint;
2715
2716 if (new_type != bl->loc_type)
2717 {
2718 static int said = 0;
2719
2720 bl->loc_type = new_type;
2721 if (!said)
2722 {
2723 fprintf_filtered (gdb_stdout,
2724 _("Note: automatically using "
2725 "hardware breakpoints for "
2726 "read-only addresses.\n"));
2727 said = 1;
2728 }
2729 }
2730 }
2731 else if (bl->loc_type == bp_loc_software_breakpoint
2732 && mr->attrib.mode != MEM_RW)
2733 {
2734 fprintf_unfiltered (tmp_error_stream,
2735 _("Cannot insert breakpoint %d.\n"
2736 "Cannot set software breakpoint "
2737 "at read-only address %s\n"),
2738 bl->owner->number,
2739 paddress (bl->gdbarch, bl->address));
2740 return 1;
2741 }
2742 }
2743 }
2744
2745 /* First check to see if we have to handle an overlay. */
2746 if (overlay_debugging == ovly_off
2747 || bl->section == NULL
2748 || !(section_is_overlay (bl->section)))
2749 {
2750 /* No overlay handling: just set the breakpoint. */
2751 TRY
2752 {
2753 int val;
2754
2755 val = bl->owner->ops->insert_location (bl);
2756 if (val)
2757 bp_err = GENERIC_ERROR;
2758 }
2759 CATCH (e, RETURN_MASK_ALL)
2760 {
2761 bp_err = e.error;
2762 bp_err_message = e.message;
2763 }
2764 END_CATCH
2765 }
2766 else
2767 {
2768 /* This breakpoint is in an overlay section.
2769 Shall we set a breakpoint at the LMA? */
2770 if (!overlay_events_enabled)
2771 {
2772 /* Yes -- overlay event support is not active,
2773 so we must try to set a breakpoint at the LMA.
2774 This will not work for a hardware breakpoint. */
2775 if (bl->loc_type == bp_loc_hardware_breakpoint)
2776 warning (_("hardware breakpoint %d not supported in overlay!"),
2777 bl->owner->number);
2778 else
2779 {
2780 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2781 bl->section);
2782 /* Set a software (trap) breakpoint at the LMA. */
2783 bl->overlay_target_info = bl->target_info;
2784 bl->overlay_target_info.reqstd_address = addr;
2785
2786 /* No overlay handling: just set the breakpoint. */
2787 TRY
2788 {
2789 int val;
2790
2791 bl->overlay_target_info.kind
2792 = breakpoint_kind (bl, &addr);
2793 bl->overlay_target_info.placed_address = addr;
2794 val = target_insert_breakpoint (bl->gdbarch,
2795 &bl->overlay_target_info);
2796 if (val)
2797 bp_err = GENERIC_ERROR;
2798 }
2799 CATCH (e, RETURN_MASK_ALL)
2800 {
2801 bp_err = e.error;
2802 bp_err_message = e.message;
2803 }
2804 END_CATCH
2805
2806 if (bp_err != GDB_NO_ERROR)
2807 fprintf_unfiltered (tmp_error_stream,
2808 "Overlay breakpoint %d "
2809 "failed: in ROM?\n",
2810 bl->owner->number);
2811 }
2812 }
2813 /* Shall we set a breakpoint at the VMA? */
2814 if (section_is_mapped (bl->section))
2815 {
2816 /* Yes. This overlay section is mapped into memory. */
2817 TRY
2818 {
2819 int val;
2820
2821 val = bl->owner->ops->insert_location (bl);
2822 if (val)
2823 bp_err = GENERIC_ERROR;
2824 }
2825 CATCH (e, RETURN_MASK_ALL)
2826 {
2827 bp_err = e.error;
2828 bp_err_message = e.message;
2829 }
2830 END_CATCH
2831 }
2832 else
2833 {
2834 /* No. This breakpoint will not be inserted.
2835 No error, but do not mark the bp as 'inserted'. */
2836 return 0;
2837 }
2838 }
2839
2840 if (bp_err != GDB_NO_ERROR)
2841 {
2842 /* Can't set the breakpoint. */
2843
2844 /* In some cases, we might not be able to insert a
2845 breakpoint in a shared library that has already been
2846 removed, but we have not yet processed the shlib unload
2847 event. Unfortunately, some targets that implement
2848 breakpoint insertion themselves can't tell why the
2849 breakpoint insertion failed (e.g., the remote target
2850 doesn't define error codes), so we must treat generic
2851 errors as memory errors. */
2852 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2853 && bl->loc_type == bp_loc_software_breakpoint
2854 && (solib_name_from_address (bl->pspace, bl->address)
2855 || shared_objfile_contains_address_p (bl->pspace,
2856 bl->address)))
2857 {
2858 /* See also: disable_breakpoints_in_shlibs. */
2859 bl->shlib_disabled = 1;
2860 observer_notify_breakpoint_modified (bl->owner);
2861 if (!*disabled_breaks)
2862 {
2863 fprintf_unfiltered (tmp_error_stream,
2864 "Cannot insert breakpoint %d.\n",
2865 bl->owner->number);
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Temporarily disabling shared "
2868 "library breakpoints:\n");
2869 }
2870 *disabled_breaks = 1;
2871 fprintf_unfiltered (tmp_error_stream,
2872 "breakpoint #%d\n", bl->owner->number);
2873 return 0;
2874 }
2875 else
2876 {
2877 if (bl->loc_type == bp_loc_hardware_breakpoint)
2878 {
2879 *hw_breakpoint_error = 1;
2880 *hw_bp_error_explained_already = bp_err_message != NULL;
2881 fprintf_unfiltered (tmp_error_stream,
2882 "Cannot insert hardware breakpoint %d%s",
2883 bl->owner->number, bp_err_message ? ":" : ".\n");
2884 if (bp_err_message != NULL)
2885 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2886 }
2887 else
2888 {
2889 if (bp_err_message == NULL)
2890 {
2891 std::string message
2892 = memory_error_message (TARGET_XFER_E_IO,
2893 bl->gdbarch, bl->address);
2894
2895 fprintf_unfiltered (tmp_error_stream,
2896 "Cannot insert breakpoint %d.\n"
2897 "%s\n",
2898 bl->owner->number, message.c_str ());
2899 }
2900 else
2901 {
2902 fprintf_unfiltered (tmp_error_stream,
2903 "Cannot insert breakpoint %d: %s\n",
2904 bl->owner->number,
2905 bp_err_message);
2906 }
2907 }
2908 return 1;
2909
2910 }
2911 }
2912 else
2913 bl->inserted = 1;
2914
2915 return 0;
2916 }
2917
2918 else if (bl->loc_type == bp_loc_hardware_watchpoint
2919 /* NOTE drow/2003-09-08: This state only exists for removing
2920 watchpoints. It's not clear that it's necessary... */
2921 && bl->owner->disposition != disp_del_at_next_stop)
2922 {
2923 int val;
2924
2925 gdb_assert (bl->owner->ops != NULL
2926 && bl->owner->ops->insert_location != NULL);
2927
2928 val = bl->owner->ops->insert_location (bl);
2929
2930 /* If trying to set a read-watchpoint, and it turns out it's not
2931 supported, try emulating one with an access watchpoint. */
2932 if (val == 1 && bl->watchpoint_type == hw_read)
2933 {
2934 struct bp_location *loc, **loc_temp;
2935
2936 /* But don't try to insert it, if there's already another
2937 hw_access location that would be considered a duplicate
2938 of this one. */
2939 ALL_BP_LOCATIONS (loc, loc_temp)
2940 if (loc != bl
2941 && loc->watchpoint_type == hw_access
2942 && watchpoint_locations_match (bl, loc))
2943 {
2944 bl->duplicate = 1;
2945 bl->inserted = 1;
2946 bl->target_info = loc->target_info;
2947 bl->watchpoint_type = hw_access;
2948 val = 0;
2949 break;
2950 }
2951
2952 if (val == 1)
2953 {
2954 bl->watchpoint_type = hw_access;
2955 val = bl->owner->ops->insert_location (bl);
2956
2957 if (val)
2958 /* Back to the original value. */
2959 bl->watchpoint_type = hw_read;
2960 }
2961 }
2962
2963 bl->inserted = (val == 0);
2964 }
2965
2966 else if (bl->owner->type == bp_catchpoint)
2967 {
2968 int val;
2969
2970 gdb_assert (bl->owner->ops != NULL
2971 && bl->owner->ops->insert_location != NULL);
2972
2973 val = bl->owner->ops->insert_location (bl);
2974 if (val)
2975 {
2976 bl->owner->enable_state = bp_disabled;
2977
2978 if (val == 1)
2979 warning (_("\
2980 Error inserting catchpoint %d: Your system does not support this type\n\
2981 of catchpoint."), bl->owner->number);
2982 else
2983 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2984 }
2985
2986 bl->inserted = (val == 0);
2987
2988 /* We've already printed an error message if there was a problem
2989 inserting this catchpoint, and we've disabled the catchpoint,
2990 so just return success. */
2991 return 0;
2992 }
2993
2994 return 0;
2995 }
2996
2997 /* This function is called when program space PSPACE is about to be
2998 deleted. It takes care of updating breakpoints to not reference
2999 PSPACE anymore. */
3000
3001 void
3002 breakpoint_program_space_exit (struct program_space *pspace)
3003 {
3004 struct breakpoint *b, *b_temp;
3005 struct bp_location *loc, **loc_temp;
3006
3007 /* Remove any breakpoint that was set through this program space. */
3008 ALL_BREAKPOINTS_SAFE (b, b_temp)
3009 {
3010 if (b->pspace == pspace)
3011 delete_breakpoint (b);
3012 }
3013
3014 /* Breakpoints set through other program spaces could have locations
3015 bound to PSPACE as well. Remove those. */
3016 ALL_BP_LOCATIONS (loc, loc_temp)
3017 {
3018 struct bp_location *tmp;
3019
3020 if (loc->pspace == pspace)
3021 {
3022 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3023 if (loc->owner->loc == loc)
3024 loc->owner->loc = loc->next;
3025 else
3026 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3027 if (tmp->next == loc)
3028 {
3029 tmp->next = loc->next;
3030 break;
3031 }
3032 }
3033 }
3034
3035 /* Now update the global location list to permanently delete the
3036 removed locations above. */
3037 update_global_location_list (UGLL_DONT_INSERT);
3038 }
3039
3040 /* Make sure all breakpoints are inserted in inferior.
3041 Throws exception on any error.
3042 A breakpoint that is already inserted won't be inserted
3043 again, so calling this function twice is safe. */
3044 void
3045 insert_breakpoints (void)
3046 {
3047 struct breakpoint *bpt;
3048
3049 ALL_BREAKPOINTS (bpt)
3050 if (is_hardware_watchpoint (bpt))
3051 {
3052 struct watchpoint *w = (struct watchpoint *) bpt;
3053
3054 update_watchpoint (w, 0 /* don't reparse. */);
3055 }
3056
3057 /* Updating watchpoints creates new locations, so update the global
3058 location list. Explicitly tell ugll to insert locations and
3059 ignore breakpoints_always_inserted_mode. */
3060 update_global_location_list (UGLL_INSERT);
3061 }
3062
3063 /* Invoke CALLBACK for each of bp_location. */
3064
3065 void
3066 iterate_over_bp_locations (walk_bp_location_callback callback)
3067 {
3068 struct bp_location *loc, **loc_tmp;
3069
3070 ALL_BP_LOCATIONS (loc, loc_tmp)
3071 {
3072 callback (loc, NULL);
3073 }
3074 }
3075
3076 /* This is used when we need to synch breakpoint conditions between GDB and the
3077 target. It is the case with deleting and disabling of breakpoints when using
3078 always-inserted mode. */
3079
3080 static void
3081 update_inserted_breakpoint_locations (void)
3082 {
3083 struct bp_location *bl, **blp_tmp;
3084 int error_flag = 0;
3085 int val = 0;
3086 int disabled_breaks = 0;
3087 int hw_breakpoint_error = 0;
3088 int hw_bp_details_reported = 0;
3089
3090 string_file tmp_error_stream;
3091
3092 /* Explicitly mark the warning -- this will only be printed if
3093 there was an error. */
3094 tmp_error_stream.puts ("Warning:\n");
3095
3096 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3097
3098 ALL_BP_LOCATIONS (bl, blp_tmp)
3099 {
3100 /* We only want to update software breakpoints and hardware
3101 breakpoints. */
3102 if (!is_breakpoint (bl->owner))
3103 continue;
3104
3105 /* We only want to update locations that are already inserted
3106 and need updating. This is to avoid unwanted insertion during
3107 deletion of breakpoints. */
3108 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3109 continue;
3110
3111 switch_to_program_space_and_thread (bl->pspace);
3112
3113 /* For targets that support global breakpoints, there's no need
3114 to select an inferior to insert breakpoint to. In fact, even
3115 if we aren't attached to any process yet, we should still
3116 insert breakpoints. */
3117 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3118 && ptid_equal (inferior_ptid, null_ptid))
3119 continue;
3120
3121 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3122 &hw_breakpoint_error, &hw_bp_details_reported);
3123 if (val)
3124 error_flag = val;
3125 }
3126
3127 if (error_flag)
3128 {
3129 target_terminal::ours_for_output ();
3130 error_stream (tmp_error_stream);
3131 }
3132 }
3133
3134 /* Used when starting or continuing the program. */
3135
3136 static void
3137 insert_breakpoint_locations (void)
3138 {
3139 struct breakpoint *bpt;
3140 struct bp_location *bl, **blp_tmp;
3141 int error_flag = 0;
3142 int val = 0;
3143 int disabled_breaks = 0;
3144 int hw_breakpoint_error = 0;
3145 int hw_bp_error_explained_already = 0;
3146
3147 string_file tmp_error_stream;
3148
3149 /* Explicitly mark the warning -- this will only be printed if
3150 there was an error. */
3151 tmp_error_stream.puts ("Warning:\n");
3152
3153 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3154
3155 ALL_BP_LOCATIONS (bl, blp_tmp)
3156 {
3157 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3158 continue;
3159
3160 /* There is no point inserting thread-specific breakpoints if
3161 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3162 has BL->OWNER always non-NULL. */
3163 if (bl->owner->thread != -1
3164 && !valid_global_thread_id (bl->owner->thread))
3165 continue;
3166
3167 switch_to_program_space_and_thread (bl->pspace);
3168
3169 /* For targets that support global breakpoints, there's no need
3170 to select an inferior to insert breakpoint to. In fact, even
3171 if we aren't attached to any process yet, we should still
3172 insert breakpoints. */
3173 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3174 && ptid_equal (inferior_ptid, null_ptid))
3175 continue;
3176
3177 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3178 &hw_breakpoint_error, &hw_bp_error_explained_already);
3179 if (val)
3180 error_flag = val;
3181 }
3182
3183 /* If we failed to insert all locations of a watchpoint, remove
3184 them, as half-inserted watchpoint is of limited use. */
3185 ALL_BREAKPOINTS (bpt)
3186 {
3187 int some_failed = 0;
3188 struct bp_location *loc;
3189
3190 if (!is_hardware_watchpoint (bpt))
3191 continue;
3192
3193 if (!breakpoint_enabled (bpt))
3194 continue;
3195
3196 if (bpt->disposition == disp_del_at_next_stop)
3197 continue;
3198
3199 for (loc = bpt->loc; loc; loc = loc->next)
3200 if (!loc->inserted && should_be_inserted (loc))
3201 {
3202 some_failed = 1;
3203 break;
3204 }
3205 if (some_failed)
3206 {
3207 for (loc = bpt->loc; loc; loc = loc->next)
3208 if (loc->inserted)
3209 remove_breakpoint (loc);
3210
3211 hw_breakpoint_error = 1;
3212 tmp_error_stream.printf ("Could not insert "
3213 "hardware watchpoint %d.\n",
3214 bpt->number);
3215 error_flag = -1;
3216 }
3217 }
3218
3219 if (error_flag)
3220 {
3221 /* If a hardware breakpoint or watchpoint was inserted, add a
3222 message about possibly exhausted resources. */
3223 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3224 {
3225 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3226 You may have requested too many hardware breakpoints/watchpoints.\n");
3227 }
3228 target_terminal::ours_for_output ();
3229 error_stream (tmp_error_stream);
3230 }
3231 }
3232
3233 /* Used when the program stops.
3234 Returns zero if successful, or non-zero if there was a problem
3235 removing a breakpoint location. */
3236
3237 int
3238 remove_breakpoints (void)
3239 {
3240 struct bp_location *bl, **blp_tmp;
3241 int val = 0;
3242
3243 ALL_BP_LOCATIONS (bl, blp_tmp)
3244 {
3245 if (bl->inserted && !is_tracepoint (bl->owner))
3246 val |= remove_breakpoint (bl);
3247 }
3248 return val;
3249 }
3250
3251 /* When a thread exits, remove breakpoints that are related to
3252 that thread. */
3253
3254 static void
3255 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3256 {
3257 struct breakpoint *b, *b_tmp;
3258
3259 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3260 {
3261 if (b->thread == tp->global_num && user_breakpoint_p (b))
3262 {
3263 b->disposition = disp_del_at_next_stop;
3264
3265 printf_filtered (_("\
3266 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3267 b->number, print_thread_id (tp));
3268
3269 /* Hide it from the user. */
3270 b->number = 0;
3271 }
3272 }
3273 }
3274
3275 /* Remove breakpoints of process PID. */
3276
3277 int
3278 remove_breakpoints_pid (int pid)
3279 {
3280 struct bp_location *bl, **blp_tmp;
3281 int val;
3282 struct inferior *inf = find_inferior_pid (pid);
3283
3284 ALL_BP_LOCATIONS (bl, blp_tmp)
3285 {
3286 if (bl->pspace != inf->pspace)
3287 continue;
3288
3289 if (bl->inserted && !bl->target_info.persist)
3290 {
3291 val = remove_breakpoint (bl);
3292 if (val != 0)
3293 return val;
3294 }
3295 }
3296 return 0;
3297 }
3298
3299 int
3300 reattach_breakpoints (int pid)
3301 {
3302 struct bp_location *bl, **blp_tmp;
3303 int val;
3304 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3305 struct inferior *inf;
3306 struct thread_info *tp;
3307
3308 tp = any_live_thread_of_process (pid);
3309 if (tp == NULL)
3310 return 1;
3311
3312 inf = find_inferior_pid (pid);
3313
3314 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3315 inferior_ptid = tp->ptid;
3316
3317 string_file tmp_error_stream;
3318
3319 ALL_BP_LOCATIONS (bl, blp_tmp)
3320 {
3321 if (bl->pspace != inf->pspace)
3322 continue;
3323
3324 if (bl->inserted)
3325 {
3326 bl->inserted = 0;
3327 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3328 if (val != 0)
3329 return val;
3330 }
3331 }
3332 return 0;
3333 }
3334
3335 static int internal_breakpoint_number = -1;
3336
3337 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3338 If INTERNAL is non-zero, the breakpoint number will be populated
3339 from internal_breakpoint_number and that variable decremented.
3340 Otherwise the breakpoint number will be populated from
3341 breakpoint_count and that value incremented. Internal breakpoints
3342 do not set the internal var bpnum. */
3343 static void
3344 set_breakpoint_number (int internal, struct breakpoint *b)
3345 {
3346 if (internal)
3347 b->number = internal_breakpoint_number--;
3348 else
3349 {
3350 set_breakpoint_count (breakpoint_count + 1);
3351 b->number = breakpoint_count;
3352 }
3353 }
3354
3355 static struct breakpoint *
3356 create_internal_breakpoint (struct gdbarch *gdbarch,
3357 CORE_ADDR address, enum bptype type,
3358 const struct breakpoint_ops *ops)
3359 {
3360 symtab_and_line sal;
3361 sal.pc = address;
3362 sal.section = find_pc_overlay (sal.pc);
3363 sal.pspace = current_program_space;
3364
3365 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3366 b->number = internal_breakpoint_number--;
3367 b->disposition = disp_donttouch;
3368
3369 return b;
3370 }
3371
3372 static const char *const longjmp_names[] =
3373 {
3374 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3375 };
3376 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3377
3378 /* Per-objfile data private to breakpoint.c. */
3379 struct breakpoint_objfile_data
3380 {
3381 /* Minimal symbol for "_ovly_debug_event" (if any). */
3382 struct bound_minimal_symbol overlay_msym;
3383
3384 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3385 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3386
3387 /* True if we have looked for longjmp probes. */
3388 int longjmp_searched;
3389
3390 /* SystemTap probe points for longjmp (if any). */
3391 VEC (probe_p) *longjmp_probes;
3392
3393 /* Minimal symbol for "std::terminate()" (if any). */
3394 struct bound_minimal_symbol terminate_msym;
3395
3396 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3397 struct bound_minimal_symbol exception_msym;
3398
3399 /* True if we have looked for exception probes. */
3400 int exception_searched;
3401
3402 /* SystemTap probe points for unwinding (if any). */
3403 VEC (probe_p) *exception_probes;
3404 };
3405
3406 static const struct objfile_data *breakpoint_objfile_key;
3407
3408 /* Minimal symbol not found sentinel. */
3409 static struct minimal_symbol msym_not_found;
3410
3411 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3412
3413 static int
3414 msym_not_found_p (const struct minimal_symbol *msym)
3415 {
3416 return msym == &msym_not_found;
3417 }
3418
3419 /* Return per-objfile data needed by breakpoint.c.
3420 Allocate the data if necessary. */
3421
3422 static struct breakpoint_objfile_data *
3423 get_breakpoint_objfile_data (struct objfile *objfile)
3424 {
3425 struct breakpoint_objfile_data *bp_objfile_data;
3426
3427 bp_objfile_data = ((struct breakpoint_objfile_data *)
3428 objfile_data (objfile, breakpoint_objfile_key));
3429 if (bp_objfile_data == NULL)
3430 {
3431 bp_objfile_data =
3432 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3433
3434 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3435 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3436 }
3437 return bp_objfile_data;
3438 }
3439
3440 static void
3441 free_breakpoint_probes (struct objfile *obj, void *data)
3442 {
3443 struct breakpoint_objfile_data *bp_objfile_data
3444 = (struct breakpoint_objfile_data *) data;
3445
3446 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3447 VEC_free (probe_p, bp_objfile_data->exception_probes);
3448 }
3449
3450 static void
3451 create_overlay_event_breakpoint (void)
3452 {
3453 struct objfile *objfile;
3454 const char *const func_name = "_ovly_debug_event";
3455
3456 ALL_OBJFILES (objfile)
3457 {
3458 struct breakpoint *b;
3459 struct breakpoint_objfile_data *bp_objfile_data;
3460 CORE_ADDR addr;
3461 struct explicit_location explicit_loc;
3462
3463 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3464
3465 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3466 continue;
3467
3468 if (bp_objfile_data->overlay_msym.minsym == NULL)
3469 {
3470 struct bound_minimal_symbol m;
3471
3472 m = lookup_minimal_symbol_text (func_name, objfile);
3473 if (m.minsym == NULL)
3474 {
3475 /* Avoid future lookups in this objfile. */
3476 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3477 continue;
3478 }
3479 bp_objfile_data->overlay_msym = m;
3480 }
3481
3482 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3483 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3484 bp_overlay_event,
3485 &internal_breakpoint_ops);
3486 initialize_explicit_location (&explicit_loc);
3487 explicit_loc.function_name = ASTRDUP (func_name);
3488 b->location = new_explicit_location (&explicit_loc);
3489
3490 if (overlay_debugging == ovly_auto)
3491 {
3492 b->enable_state = bp_enabled;
3493 overlay_events_enabled = 1;
3494 }
3495 else
3496 {
3497 b->enable_state = bp_disabled;
3498 overlay_events_enabled = 0;
3499 }
3500 }
3501 }
3502
3503 static void
3504 create_longjmp_master_breakpoint (void)
3505 {
3506 struct program_space *pspace;
3507
3508 scoped_restore_current_program_space restore_pspace;
3509
3510 ALL_PSPACES (pspace)
3511 {
3512 struct objfile *objfile;
3513
3514 set_current_program_space (pspace);
3515
3516 ALL_OBJFILES (objfile)
3517 {
3518 int i;
3519 struct gdbarch *gdbarch;
3520 struct breakpoint_objfile_data *bp_objfile_data;
3521
3522 gdbarch = get_objfile_arch (objfile);
3523
3524 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3525
3526 if (!bp_objfile_data->longjmp_searched)
3527 {
3528 VEC (probe_p) *ret;
3529
3530 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3531 if (ret != NULL)
3532 {
3533 /* We are only interested in checking one element. */
3534 struct probe *p = VEC_index (probe_p, ret, 0);
3535
3536 if (!can_evaluate_probe_arguments (p))
3537 {
3538 /* We cannot use the probe interface here, because it does
3539 not know how to evaluate arguments. */
3540 VEC_free (probe_p, ret);
3541 ret = NULL;
3542 }
3543 }
3544 bp_objfile_data->longjmp_probes = ret;
3545 bp_objfile_data->longjmp_searched = 1;
3546 }
3547
3548 if (bp_objfile_data->longjmp_probes != NULL)
3549 {
3550 int i;
3551 struct probe *probe;
3552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3553
3554 for (i = 0;
3555 VEC_iterate (probe_p,
3556 bp_objfile_data->longjmp_probes,
3557 i, probe);
3558 ++i)
3559 {
3560 struct breakpoint *b;
3561
3562 b = create_internal_breakpoint (gdbarch,
3563 get_probe_address (probe,
3564 objfile),
3565 bp_longjmp_master,
3566 &internal_breakpoint_ops);
3567 b->location = new_probe_location ("-probe-stap libc:longjmp");
3568 b->enable_state = bp_disabled;
3569 }
3570
3571 continue;
3572 }
3573
3574 if (!gdbarch_get_longjmp_target_p (gdbarch))
3575 continue;
3576
3577 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3578 {
3579 struct breakpoint *b;
3580 const char *func_name;
3581 CORE_ADDR addr;
3582 struct explicit_location explicit_loc;
3583
3584 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3585 continue;
3586
3587 func_name = longjmp_names[i];
3588 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3589 {
3590 struct bound_minimal_symbol m;
3591
3592 m = lookup_minimal_symbol_text (func_name, objfile);
3593 if (m.minsym == NULL)
3594 {
3595 /* Prevent future lookups in this objfile. */
3596 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3597 continue;
3598 }
3599 bp_objfile_data->longjmp_msym[i] = m;
3600 }
3601
3602 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3603 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3604 &internal_breakpoint_ops);
3605 initialize_explicit_location (&explicit_loc);
3606 explicit_loc.function_name = ASTRDUP (func_name);
3607 b->location = new_explicit_location (&explicit_loc);
3608 b->enable_state = bp_disabled;
3609 }
3610 }
3611 }
3612 }
3613
3614 /* Create a master std::terminate breakpoint. */
3615 static void
3616 create_std_terminate_master_breakpoint (void)
3617 {
3618 struct program_space *pspace;
3619 const char *const func_name = "std::terminate()";
3620
3621 scoped_restore_current_program_space restore_pspace;
3622
3623 ALL_PSPACES (pspace)
3624 {
3625 struct objfile *objfile;
3626 CORE_ADDR addr;
3627
3628 set_current_program_space (pspace);
3629
3630 ALL_OBJFILES (objfile)
3631 {
3632 struct breakpoint *b;
3633 struct breakpoint_objfile_data *bp_objfile_data;
3634 struct explicit_location explicit_loc;
3635
3636 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3637
3638 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3639 continue;
3640
3641 if (bp_objfile_data->terminate_msym.minsym == NULL)
3642 {
3643 struct bound_minimal_symbol m;
3644
3645 m = lookup_minimal_symbol (func_name, NULL, objfile);
3646 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3647 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3648 {
3649 /* Prevent future lookups in this objfile. */
3650 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3651 continue;
3652 }
3653 bp_objfile_data->terminate_msym = m;
3654 }
3655
3656 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3657 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3658 bp_std_terminate_master,
3659 &internal_breakpoint_ops);
3660 initialize_explicit_location (&explicit_loc);
3661 explicit_loc.function_name = ASTRDUP (func_name);
3662 b->location = new_explicit_location (&explicit_loc);
3663 b->enable_state = bp_disabled;
3664 }
3665 }
3666 }
3667
3668 /* Install a master breakpoint on the unwinder's debug hook. */
3669
3670 static void
3671 create_exception_master_breakpoint (void)
3672 {
3673 struct objfile *objfile;
3674 const char *const func_name = "_Unwind_DebugHook";
3675
3676 ALL_OBJFILES (objfile)
3677 {
3678 struct breakpoint *b;
3679 struct gdbarch *gdbarch;
3680 struct breakpoint_objfile_data *bp_objfile_data;
3681 CORE_ADDR addr;
3682 struct explicit_location explicit_loc;
3683
3684 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3685
3686 /* We prefer the SystemTap probe point if it exists. */
3687 if (!bp_objfile_data->exception_searched)
3688 {
3689 VEC (probe_p) *ret;
3690
3691 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3692
3693 if (ret != NULL)
3694 {
3695 /* We are only interested in checking one element. */
3696 struct probe *p = VEC_index (probe_p, ret, 0);
3697
3698 if (!can_evaluate_probe_arguments (p))
3699 {
3700 /* We cannot use the probe interface here, because it does
3701 not know how to evaluate arguments. */
3702 VEC_free (probe_p, ret);
3703 ret = NULL;
3704 }
3705 }
3706 bp_objfile_data->exception_probes = ret;
3707 bp_objfile_data->exception_searched = 1;
3708 }
3709
3710 if (bp_objfile_data->exception_probes != NULL)
3711 {
3712 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3713 int i;
3714 struct probe *probe;
3715
3716 for (i = 0;
3717 VEC_iterate (probe_p,
3718 bp_objfile_data->exception_probes,
3719 i, probe);
3720 ++i)
3721 {
3722 struct breakpoint *b;
3723
3724 b = create_internal_breakpoint (gdbarch,
3725 get_probe_address (probe,
3726 objfile),
3727 bp_exception_master,
3728 &internal_breakpoint_ops);
3729 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3730 b->enable_state = bp_disabled;
3731 }
3732
3733 continue;
3734 }
3735
3736 /* Otherwise, try the hook function. */
3737
3738 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3739 continue;
3740
3741 gdbarch = get_objfile_arch (objfile);
3742
3743 if (bp_objfile_data->exception_msym.minsym == NULL)
3744 {
3745 struct bound_minimal_symbol debug_hook;
3746
3747 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3748 if (debug_hook.minsym == NULL)
3749 {
3750 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3751 continue;
3752 }
3753
3754 bp_objfile_data->exception_msym = debug_hook;
3755 }
3756
3757 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3758 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3759 &current_target);
3760 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3761 &internal_breakpoint_ops);
3762 initialize_explicit_location (&explicit_loc);
3763 explicit_loc.function_name = ASTRDUP (func_name);
3764 b->location = new_explicit_location (&explicit_loc);
3765 b->enable_state = bp_disabled;
3766 }
3767 }
3768
3769 /* Does B have a location spec? */
3770
3771 static int
3772 breakpoint_event_location_empty_p (const struct breakpoint *b)
3773 {
3774 return b->location != NULL && event_location_empty_p (b->location.get ());
3775 }
3776
3777 void
3778 update_breakpoints_after_exec (void)
3779 {
3780 struct breakpoint *b, *b_tmp;
3781 struct bp_location *bploc, **bplocp_tmp;
3782
3783 /* We're about to delete breakpoints from GDB's lists. If the
3784 INSERTED flag is true, GDB will try to lift the breakpoints by
3785 writing the breakpoints' "shadow contents" back into memory. The
3786 "shadow contents" are NOT valid after an exec, so GDB should not
3787 do that. Instead, the target is responsible from marking
3788 breakpoints out as soon as it detects an exec. We don't do that
3789 here instead, because there may be other attempts to delete
3790 breakpoints after detecting an exec and before reaching here. */
3791 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3792 if (bploc->pspace == current_program_space)
3793 gdb_assert (!bploc->inserted);
3794
3795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3796 {
3797 if (b->pspace != current_program_space)
3798 continue;
3799
3800 /* Solib breakpoints must be explicitly reset after an exec(). */
3801 if (b->type == bp_shlib_event)
3802 {
3803 delete_breakpoint (b);
3804 continue;
3805 }
3806
3807 /* JIT breakpoints must be explicitly reset after an exec(). */
3808 if (b->type == bp_jit_event)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
3813
3814 /* Thread event breakpoints must be set anew after an exec(),
3815 as must overlay event and longjmp master breakpoints. */
3816 if (b->type == bp_thread_event || b->type == bp_overlay_event
3817 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3818 || b->type == bp_exception_master)
3819 {
3820 delete_breakpoint (b);
3821 continue;
3822 }
3823
3824 /* Step-resume breakpoints are meaningless after an exec(). */
3825 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3826 {
3827 delete_breakpoint (b);
3828 continue;
3829 }
3830
3831 /* Just like single-step breakpoints. */
3832 if (b->type == bp_single_step)
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
3838 /* Longjmp and longjmp-resume breakpoints are also meaningless
3839 after an exec. */
3840 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3841 || b->type == bp_longjmp_call_dummy
3842 || b->type == bp_exception || b->type == bp_exception_resume)
3843 {
3844 delete_breakpoint (b);
3845 continue;
3846 }
3847
3848 if (b->type == bp_catchpoint)
3849 {
3850 /* For now, none of the bp_catchpoint breakpoints need to
3851 do anything at this point. In the future, if some of
3852 the catchpoints need to something, we will need to add
3853 a new method, and call this method from here. */
3854 continue;
3855 }
3856
3857 /* bp_finish is a special case. The only way we ought to be able
3858 to see one of these when an exec() has happened, is if the user
3859 caught a vfork, and then said "finish". Ordinarily a finish just
3860 carries them to the call-site of the current callee, by setting
3861 a temporary bp there and resuming. But in this case, the finish
3862 will carry them entirely through the vfork & exec.
3863
3864 We don't want to allow a bp_finish to remain inserted now. But
3865 we can't safely delete it, 'cause finish_command has a handle to
3866 the bp on a bpstat, and will later want to delete it. There's a
3867 chance (and I've seen it happen) that if we delete the bp_finish
3868 here, that its storage will get reused by the time finish_command
3869 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3870 We really must allow finish_command to delete a bp_finish.
3871
3872 In the absence of a general solution for the "how do we know
3873 it's safe to delete something others may have handles to?"
3874 problem, what we'll do here is just uninsert the bp_finish, and
3875 let finish_command delete it.
3876
3877 (We know the bp_finish is "doomed" in the sense that it's
3878 momentary, and will be deleted as soon as finish_command sees
3879 the inferior stopped. So it doesn't matter that the bp's
3880 address is probably bogus in the new a.out, unlike e.g., the
3881 solib breakpoints.) */
3882
3883 if (b->type == bp_finish)
3884 {
3885 continue;
3886 }
3887
3888 /* Without a symbolic address, we have little hope of the
3889 pre-exec() address meaning the same thing in the post-exec()
3890 a.out. */
3891 if (breakpoint_event_location_empty_p (b))
3892 {
3893 delete_breakpoint (b);
3894 continue;
3895 }
3896 }
3897 }
3898
3899 int
3900 detach_breakpoints (ptid_t ptid)
3901 {
3902 struct bp_location *bl, **blp_tmp;
3903 int val = 0;
3904 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3905 struct inferior *inf = current_inferior ();
3906
3907 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3908 error (_("Cannot detach breakpoints of inferior_ptid"));
3909
3910 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3911 inferior_ptid = ptid;
3912 ALL_BP_LOCATIONS (bl, blp_tmp)
3913 {
3914 if (bl->pspace != inf->pspace)
3915 continue;
3916
3917 /* This function must physically remove breakpoints locations
3918 from the specified ptid, without modifying the breakpoint
3919 package's state. Locations of type bp_loc_other are only
3920 maintained at GDB side. So, there is no need to remove
3921 these bp_loc_other locations. Moreover, removing these
3922 would modify the breakpoint package's state. */
3923 if (bl->loc_type == bp_loc_other)
3924 continue;
3925
3926 if (bl->inserted)
3927 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3928 }
3929
3930 return val;
3931 }
3932
3933 /* Remove the breakpoint location BL from the current address space.
3934 Note that this is used to detach breakpoints from a child fork.
3935 When we get here, the child isn't in the inferior list, and neither
3936 do we have objects to represent its address space --- we should
3937 *not* look at bl->pspace->aspace here. */
3938
3939 static int
3940 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3941 {
3942 int val;
3943
3944 /* BL is never in moribund_locations by our callers. */
3945 gdb_assert (bl->owner != NULL);
3946
3947 /* The type of none suggests that owner is actually deleted.
3948 This should not ever happen. */
3949 gdb_assert (bl->owner->type != bp_none);
3950
3951 if (bl->loc_type == bp_loc_software_breakpoint
3952 || bl->loc_type == bp_loc_hardware_breakpoint)
3953 {
3954 /* "Normal" instruction breakpoint: either the standard
3955 trap-instruction bp (bp_breakpoint), or a
3956 bp_hardware_breakpoint. */
3957
3958 /* First check to see if we have to handle an overlay. */
3959 if (overlay_debugging == ovly_off
3960 || bl->section == NULL
3961 || !(section_is_overlay (bl->section)))
3962 {
3963 /* No overlay handling: just remove the breakpoint. */
3964
3965 /* If we're trying to uninsert a memory breakpoint that we
3966 know is set in a dynamic object that is marked
3967 shlib_disabled, then either the dynamic object was
3968 removed with "remove-symbol-file" or with
3969 "nosharedlibrary". In the former case, we don't know
3970 whether another dynamic object might have loaded over the
3971 breakpoint's address -- the user might well let us know
3972 about it next with add-symbol-file (the whole point of
3973 add-symbol-file is letting the user manually maintain a
3974 list of dynamically loaded objects). If we have the
3975 breakpoint's shadow memory, that is, this is a software
3976 breakpoint managed by GDB, check whether the breakpoint
3977 is still inserted in memory, to avoid overwriting wrong
3978 code with stale saved shadow contents. Note that HW
3979 breakpoints don't have shadow memory, as they're
3980 implemented using a mechanism that is not dependent on
3981 being able to modify the target's memory, and as such
3982 they should always be removed. */
3983 if (bl->shlib_disabled
3984 && bl->target_info.shadow_len != 0
3985 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3986 val = 0;
3987 else
3988 val = bl->owner->ops->remove_location (bl, reason);
3989 }
3990 else
3991 {
3992 /* This breakpoint is in an overlay section.
3993 Did we set a breakpoint at the LMA? */
3994 if (!overlay_events_enabled)
3995 {
3996 /* Yes -- overlay event support is not active, so we
3997 should have set a breakpoint at the LMA. Remove it.
3998 */
3999 /* Ignore any failures: if the LMA is in ROM, we will
4000 have already warned when we failed to insert it. */
4001 if (bl->loc_type == bp_loc_hardware_breakpoint)
4002 target_remove_hw_breakpoint (bl->gdbarch,
4003 &bl->overlay_target_info);
4004 else
4005 target_remove_breakpoint (bl->gdbarch,
4006 &bl->overlay_target_info,
4007 reason);
4008 }
4009 /* Did we set a breakpoint at the VMA?
4010 If so, we will have marked the breakpoint 'inserted'. */
4011 if (bl->inserted)
4012 {
4013 /* Yes -- remove it. Previously we did not bother to
4014 remove the breakpoint if the section had been
4015 unmapped, but let's not rely on that being safe. We
4016 don't know what the overlay manager might do. */
4017
4018 /* However, we should remove *software* breakpoints only
4019 if the section is still mapped, or else we overwrite
4020 wrong code with the saved shadow contents. */
4021 if (bl->loc_type == bp_loc_hardware_breakpoint
4022 || section_is_mapped (bl->section))
4023 val = bl->owner->ops->remove_location (bl, reason);
4024 else
4025 val = 0;
4026 }
4027 else
4028 {
4029 /* No -- not inserted, so no need to remove. No error. */
4030 val = 0;
4031 }
4032 }
4033
4034 /* In some cases, we might not be able to remove a breakpoint in
4035 a shared library that has already been removed, but we have
4036 not yet processed the shlib unload event. Similarly for an
4037 unloaded add-symbol-file object - the user might not yet have
4038 had the chance to remove-symbol-file it. shlib_disabled will
4039 be set if the library/object has already been removed, but
4040 the breakpoint hasn't been uninserted yet, e.g., after
4041 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4042 always-inserted mode. */
4043 if (val
4044 && (bl->loc_type == bp_loc_software_breakpoint
4045 && (bl->shlib_disabled
4046 || solib_name_from_address (bl->pspace, bl->address)
4047 || shared_objfile_contains_address_p (bl->pspace,
4048 bl->address))))
4049 val = 0;
4050
4051 if (val)
4052 return val;
4053 bl->inserted = (reason == DETACH_BREAKPOINT);
4054 }
4055 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4056 {
4057 gdb_assert (bl->owner->ops != NULL
4058 && bl->owner->ops->remove_location != NULL);
4059
4060 bl->inserted = (reason == DETACH_BREAKPOINT);
4061 bl->owner->ops->remove_location (bl, reason);
4062
4063 /* Failure to remove any of the hardware watchpoints comes here. */
4064 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4065 warning (_("Could not remove hardware watchpoint %d."),
4066 bl->owner->number);
4067 }
4068 else if (bl->owner->type == bp_catchpoint
4069 && breakpoint_enabled (bl->owner)
4070 && !bl->duplicate)
4071 {
4072 gdb_assert (bl->owner->ops != NULL
4073 && bl->owner->ops->remove_location != NULL);
4074
4075 val = bl->owner->ops->remove_location (bl, reason);
4076 if (val)
4077 return val;
4078
4079 bl->inserted = (reason == DETACH_BREAKPOINT);
4080 }
4081
4082 return 0;
4083 }
4084
4085 static int
4086 remove_breakpoint (struct bp_location *bl)
4087 {
4088 /* BL is never in moribund_locations by our callers. */
4089 gdb_assert (bl->owner != NULL);
4090
4091 /* The type of none suggests that owner is actually deleted.
4092 This should not ever happen. */
4093 gdb_assert (bl->owner->type != bp_none);
4094
4095 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4096
4097 switch_to_program_space_and_thread (bl->pspace);
4098
4099 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4100 }
4101
4102 /* Clear the "inserted" flag in all breakpoints. */
4103
4104 void
4105 mark_breakpoints_out (void)
4106 {
4107 struct bp_location *bl, **blp_tmp;
4108
4109 ALL_BP_LOCATIONS (bl, blp_tmp)
4110 if (bl->pspace == current_program_space)
4111 bl->inserted = 0;
4112 }
4113
4114 /* Clear the "inserted" flag in all breakpoints and delete any
4115 breakpoints which should go away between runs of the program.
4116
4117 Plus other such housekeeping that has to be done for breakpoints
4118 between runs.
4119
4120 Note: this function gets called at the end of a run (by
4121 generic_mourn_inferior) and when a run begins (by
4122 init_wait_for_inferior). */
4123
4124
4125
4126 void
4127 breakpoint_init_inferior (enum inf_context context)
4128 {
4129 struct breakpoint *b, *b_tmp;
4130 struct bp_location *bl;
4131 int ix;
4132 struct program_space *pspace = current_program_space;
4133
4134 /* If breakpoint locations are shared across processes, then there's
4135 nothing to do. */
4136 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4137 return;
4138
4139 mark_breakpoints_out ();
4140
4141 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4142 {
4143 if (b->loc && b->loc->pspace != pspace)
4144 continue;
4145
4146 switch (b->type)
4147 {
4148 case bp_call_dummy:
4149 case bp_longjmp_call_dummy:
4150
4151 /* If the call dummy breakpoint is at the entry point it will
4152 cause problems when the inferior is rerun, so we better get
4153 rid of it. */
4154
4155 case bp_watchpoint_scope:
4156
4157 /* Also get rid of scope breakpoints. */
4158
4159 case bp_shlib_event:
4160
4161 /* Also remove solib event breakpoints. Their addresses may
4162 have changed since the last time we ran the program.
4163 Actually we may now be debugging against different target;
4164 and so the solib backend that installed this breakpoint may
4165 not be used in by the target. E.g.,
4166
4167 (gdb) file prog-linux
4168 (gdb) run # native linux target
4169 ...
4170 (gdb) kill
4171 (gdb) file prog-win.exe
4172 (gdb) tar rem :9999 # remote Windows gdbserver.
4173 */
4174
4175 case bp_step_resume:
4176
4177 /* Also remove step-resume breakpoints. */
4178
4179 case bp_single_step:
4180
4181 /* Also remove single-step breakpoints. */
4182
4183 delete_breakpoint (b);
4184 break;
4185
4186 case bp_watchpoint:
4187 case bp_hardware_watchpoint:
4188 case bp_read_watchpoint:
4189 case bp_access_watchpoint:
4190 {
4191 struct watchpoint *w = (struct watchpoint *) b;
4192
4193 /* Likewise for watchpoints on local expressions. */
4194 if (w->exp_valid_block != NULL)
4195 delete_breakpoint (b);
4196 else
4197 {
4198 /* Get rid of existing locations, which are no longer
4199 valid. New ones will be created in
4200 update_watchpoint, when the inferior is restarted.
4201 The next update_global_location_list call will
4202 garbage collect them. */
4203 b->loc = NULL;
4204
4205 if (context == inf_starting)
4206 {
4207 /* Reset val field to force reread of starting value in
4208 insert_breakpoints. */
4209 if (w->val)
4210 value_free (w->val);
4211 w->val = NULL;
4212 w->val_valid = 0;
4213 }
4214 }
4215 }
4216 break;
4217 default:
4218 break;
4219 }
4220 }
4221
4222 /* Get rid of the moribund locations. */
4223 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4224 decref_bp_location (&bl);
4225 VEC_free (bp_location_p, moribund_locations);
4226 }
4227
4228 /* These functions concern about actual breakpoints inserted in the
4229 target --- to e.g. check if we need to do decr_pc adjustment or if
4230 we need to hop over the bkpt --- so we check for address space
4231 match, not program space. */
4232
4233 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4234 exists at PC. It returns ordinary_breakpoint_here if it's an
4235 ordinary breakpoint, or permanent_breakpoint_here if it's a
4236 permanent breakpoint.
4237 - When continuing from a location with an ordinary breakpoint, we
4238 actually single step once before calling insert_breakpoints.
4239 - When continuing from a location with a permanent breakpoint, we
4240 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4241 the target, to advance the PC past the breakpoint. */
4242
4243 enum breakpoint_here
4244 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4245 {
4246 struct bp_location *bl, **blp_tmp;
4247 int any_breakpoint_here = 0;
4248
4249 ALL_BP_LOCATIONS (bl, blp_tmp)
4250 {
4251 if (bl->loc_type != bp_loc_software_breakpoint
4252 && bl->loc_type != bp_loc_hardware_breakpoint)
4253 continue;
4254
4255 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4256 if ((breakpoint_enabled (bl->owner)
4257 || bl->permanent)
4258 && breakpoint_location_address_match (bl, aspace, pc))
4259 {
4260 if (overlay_debugging
4261 && section_is_overlay (bl->section)
4262 && !section_is_mapped (bl->section))
4263 continue; /* unmapped overlay -- can't be a match */
4264 else if (bl->permanent)
4265 return permanent_breakpoint_here;
4266 else
4267 any_breakpoint_here = 1;
4268 }
4269 }
4270
4271 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4272 }
4273
4274 /* See breakpoint.h. */
4275
4276 int
4277 breakpoint_in_range_p (struct address_space *aspace,
4278 CORE_ADDR addr, ULONGEST len)
4279 {
4280 struct bp_location *bl, **blp_tmp;
4281
4282 ALL_BP_LOCATIONS (bl, blp_tmp)
4283 {
4284 if (bl->loc_type != bp_loc_software_breakpoint
4285 && bl->loc_type != bp_loc_hardware_breakpoint)
4286 continue;
4287
4288 if ((breakpoint_enabled (bl->owner)
4289 || bl->permanent)
4290 && breakpoint_location_address_range_overlap (bl, aspace,
4291 addr, len))
4292 {
4293 if (overlay_debugging
4294 && section_is_overlay (bl->section)
4295 && !section_is_mapped (bl->section))
4296 {
4297 /* Unmapped overlay -- can't be a match. */
4298 continue;
4299 }
4300
4301 return 1;
4302 }
4303 }
4304
4305 return 0;
4306 }
4307
4308 /* Return true if there's a moribund breakpoint at PC. */
4309
4310 int
4311 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4312 {
4313 struct bp_location *loc;
4314 int ix;
4315
4316 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4317 if (breakpoint_location_address_match (loc, aspace, pc))
4318 return 1;
4319
4320 return 0;
4321 }
4322
4323 /* Returns non-zero iff BL is inserted at PC, in address space
4324 ASPACE. */
4325
4326 static int
4327 bp_location_inserted_here_p (struct bp_location *bl,
4328 struct address_space *aspace, CORE_ADDR pc)
4329 {
4330 if (bl->inserted
4331 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4332 aspace, pc))
4333 {
4334 if (overlay_debugging
4335 && section_is_overlay (bl->section)
4336 && !section_is_mapped (bl->section))
4337 return 0; /* unmapped overlay -- can't be a match */
4338 else
4339 return 1;
4340 }
4341 return 0;
4342 }
4343
4344 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4345
4346 int
4347 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4348 {
4349 struct bp_location **blp, **blp_tmp = NULL;
4350
4351 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4352 {
4353 struct bp_location *bl = *blp;
4354
4355 if (bl->loc_type != bp_loc_software_breakpoint
4356 && bl->loc_type != bp_loc_hardware_breakpoint)
4357 continue;
4358
4359 if (bp_location_inserted_here_p (bl, aspace, pc))
4360 return 1;
4361 }
4362 return 0;
4363 }
4364
4365 /* This function returns non-zero iff there is a software breakpoint
4366 inserted at PC. */
4367
4368 int
4369 software_breakpoint_inserted_here_p (struct address_space *aspace,
4370 CORE_ADDR pc)
4371 {
4372 struct bp_location **blp, **blp_tmp = NULL;
4373
4374 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4375 {
4376 struct bp_location *bl = *blp;
4377
4378 if (bl->loc_type != bp_loc_software_breakpoint)
4379 continue;
4380
4381 if (bp_location_inserted_here_p (bl, aspace, pc))
4382 return 1;
4383 }
4384
4385 return 0;
4386 }
4387
4388 /* See breakpoint.h. */
4389
4390 int
4391 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4392 CORE_ADDR pc)
4393 {
4394 struct bp_location **blp, **blp_tmp = NULL;
4395
4396 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4397 {
4398 struct bp_location *bl = *blp;
4399
4400 if (bl->loc_type != bp_loc_hardware_breakpoint)
4401 continue;
4402
4403 if (bp_location_inserted_here_p (bl, aspace, pc))
4404 return 1;
4405 }
4406
4407 return 0;
4408 }
4409
4410 int
4411 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4412 CORE_ADDR addr, ULONGEST len)
4413 {
4414 struct breakpoint *bpt;
4415
4416 ALL_BREAKPOINTS (bpt)
4417 {
4418 struct bp_location *loc;
4419
4420 if (bpt->type != bp_hardware_watchpoint
4421 && bpt->type != bp_access_watchpoint)
4422 continue;
4423
4424 if (!breakpoint_enabled (bpt))
4425 continue;
4426
4427 for (loc = bpt->loc; loc; loc = loc->next)
4428 if (loc->pspace->aspace == aspace && loc->inserted)
4429 {
4430 CORE_ADDR l, h;
4431
4432 /* Check for intersection. */
4433 l = std::max<CORE_ADDR> (loc->address, addr);
4434 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4435 if (l < h)
4436 return 1;
4437 }
4438 }
4439 return 0;
4440 }
4441 \f
4442
4443 /* bpstat stuff. External routines' interfaces are documented
4444 in breakpoint.h. */
4445
4446 int
4447 is_catchpoint (struct breakpoint *ep)
4448 {
4449 return (ep->type == bp_catchpoint);
4450 }
4451
4452 /* Frees any storage that is part of a bpstat. Does not walk the
4453 'next' chain. */
4454
4455 bpstats::~bpstats ()
4456 {
4457 if (old_val != NULL)
4458 value_free (old_val);
4459 decref_counted_command_line (&commands);
4460 if (bp_location_at != NULL)
4461 decref_bp_location (&bp_location_at);
4462 }
4463
4464 /* Clear a bpstat so that it says we are not at any breakpoint.
4465 Also free any storage that is part of a bpstat. */
4466
4467 void
4468 bpstat_clear (bpstat *bsp)
4469 {
4470 bpstat p;
4471 bpstat q;
4472
4473 if (bsp == 0)
4474 return;
4475 p = *bsp;
4476 while (p != NULL)
4477 {
4478 q = p->next;
4479 delete p;
4480 p = q;
4481 }
4482 *bsp = NULL;
4483 }
4484
4485 bpstats::bpstats (const bpstats &other)
4486 : next (NULL),
4487 bp_location_at (other.bp_location_at),
4488 breakpoint_at (other.breakpoint_at),
4489 commands (other.commands),
4490 old_val (other.old_val),
4491 print (other.print),
4492 stop (other.stop),
4493 print_it (other.print_it)
4494 {
4495 if (old_val != NULL)
4496 {
4497 old_val = value_copy (old_val);
4498 release_value (old_val);
4499 }
4500 incref_bp_location (bp_location_at);
4501 incref_counted_command_line (commands);
4502 }
4503
4504 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4505 is part of the bpstat is copied as well. */
4506
4507 bpstat
4508 bpstat_copy (bpstat bs)
4509 {
4510 bpstat p = NULL;
4511 bpstat tmp;
4512 bpstat retval = NULL;
4513
4514 if (bs == NULL)
4515 return bs;
4516
4517 for (; bs != NULL; bs = bs->next)
4518 {
4519 tmp = new bpstats (*bs);
4520
4521 if (p == NULL)
4522 /* This is the first thing in the chain. */
4523 retval = tmp;
4524 else
4525 p->next = tmp;
4526 p = tmp;
4527 }
4528 p->next = NULL;
4529 return retval;
4530 }
4531
4532 /* Find the bpstat associated with this breakpoint. */
4533
4534 bpstat
4535 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4536 {
4537 if (bsp == NULL)
4538 return NULL;
4539
4540 for (; bsp != NULL; bsp = bsp->next)
4541 {
4542 if (bsp->breakpoint_at == breakpoint)
4543 return bsp;
4544 }
4545 return NULL;
4546 }
4547
4548 /* See breakpoint.h. */
4549
4550 int
4551 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4552 {
4553 for (; bsp != NULL; bsp = bsp->next)
4554 {
4555 if (bsp->breakpoint_at == NULL)
4556 {
4557 /* A moribund location can never explain a signal other than
4558 GDB_SIGNAL_TRAP. */
4559 if (sig == GDB_SIGNAL_TRAP)
4560 return 1;
4561 }
4562 else
4563 {
4564 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4565 sig))
4566 return 1;
4567 }
4568 }
4569
4570 return 0;
4571 }
4572
4573 /* Put in *NUM the breakpoint number of the first breakpoint we are
4574 stopped at. *BSP upon return is a bpstat which points to the
4575 remaining breakpoints stopped at (but which is not guaranteed to be
4576 good for anything but further calls to bpstat_num).
4577
4578 Return 0 if passed a bpstat which does not indicate any breakpoints.
4579 Return -1 if stopped at a breakpoint that has been deleted since
4580 we set it.
4581 Return 1 otherwise. */
4582
4583 int
4584 bpstat_num (bpstat *bsp, int *num)
4585 {
4586 struct breakpoint *b;
4587
4588 if ((*bsp) == NULL)
4589 return 0; /* No more breakpoint values */
4590
4591 /* We assume we'll never have several bpstats that correspond to a
4592 single breakpoint -- otherwise, this function might return the
4593 same number more than once and this will look ugly. */
4594 b = (*bsp)->breakpoint_at;
4595 *bsp = (*bsp)->next;
4596 if (b == NULL)
4597 return -1; /* breakpoint that's been deleted since */
4598
4599 *num = b->number; /* We have its number */
4600 return 1;
4601 }
4602
4603 /* See breakpoint.h. */
4604
4605 void
4606 bpstat_clear_actions (void)
4607 {
4608 struct thread_info *tp;
4609 bpstat bs;
4610
4611 if (ptid_equal (inferior_ptid, null_ptid))
4612 return;
4613
4614 tp = find_thread_ptid (inferior_ptid);
4615 if (tp == NULL)
4616 return;
4617
4618 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4619 {
4620 decref_counted_command_line (&bs->commands);
4621
4622 if (bs->old_val != NULL)
4623 {
4624 value_free (bs->old_val);
4625 bs->old_val = NULL;
4626 }
4627 }
4628 }
4629
4630 /* Called when a command is about to proceed the inferior. */
4631
4632 static void
4633 breakpoint_about_to_proceed (void)
4634 {
4635 if (!ptid_equal (inferior_ptid, null_ptid))
4636 {
4637 struct thread_info *tp = inferior_thread ();
4638
4639 /* Allow inferior function calls in breakpoint commands to not
4640 interrupt the command list. When the call finishes
4641 successfully, the inferior will be standing at the same
4642 breakpoint as if nothing happened. */
4643 if (tp->control.in_infcall)
4644 return;
4645 }
4646
4647 breakpoint_proceeded = 1;
4648 }
4649
4650 /* Stub for cleaning up our state if we error-out of a breakpoint
4651 command. */
4652 static void
4653 cleanup_executing_breakpoints (void *ignore)
4654 {
4655 executing_breakpoint_commands = 0;
4656 }
4657
4658 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4659 or its equivalent. */
4660
4661 static int
4662 command_line_is_silent (struct command_line *cmd)
4663 {
4664 return cmd && (strcmp ("silent", cmd->line) == 0);
4665 }
4666
4667 /* Execute all the commands associated with all the breakpoints at
4668 this location. Any of these commands could cause the process to
4669 proceed beyond this point, etc. We look out for such changes by
4670 checking the global "breakpoint_proceeded" after each command.
4671
4672 Returns true if a breakpoint command resumed the inferior. In that
4673 case, it is the caller's responsibility to recall it again with the
4674 bpstat of the current thread. */
4675
4676 static int
4677 bpstat_do_actions_1 (bpstat *bsp)
4678 {
4679 bpstat bs;
4680 struct cleanup *old_chain;
4681 int again = 0;
4682
4683 /* Avoid endless recursion if a `source' command is contained
4684 in bs->commands. */
4685 if (executing_breakpoint_commands)
4686 return 0;
4687
4688 executing_breakpoint_commands = 1;
4689 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4690
4691 scoped_restore preventer = prevent_dont_repeat ();
4692
4693 /* This pointer will iterate over the list of bpstat's. */
4694 bs = *bsp;
4695
4696 breakpoint_proceeded = 0;
4697 for (; bs != NULL; bs = bs->next)
4698 {
4699 struct counted_command_line *ccmd;
4700 struct command_line *cmd;
4701 struct cleanup *this_cmd_tree_chain;
4702
4703 /* Take ownership of the BSP's command tree, if it has one.
4704
4705 The command tree could legitimately contain commands like
4706 'step' and 'next', which call clear_proceed_status, which
4707 frees stop_bpstat's command tree. To make sure this doesn't
4708 free the tree we're executing out from under us, we need to
4709 take ownership of the tree ourselves. Since a given bpstat's
4710 commands are only executed once, we don't need to copy it; we
4711 can clear the pointer in the bpstat, and make sure we free
4712 the tree when we're done. */
4713 ccmd = bs->commands;
4714 bs->commands = NULL;
4715 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4716 cmd = ccmd ? ccmd->commands : NULL;
4717 if (command_line_is_silent (cmd))
4718 {
4719 /* The action has been already done by bpstat_stop_status. */
4720 cmd = cmd->next;
4721 }
4722
4723 while (cmd != NULL)
4724 {
4725 execute_control_command (cmd);
4726
4727 if (breakpoint_proceeded)
4728 break;
4729 else
4730 cmd = cmd->next;
4731 }
4732
4733 /* We can free this command tree now. */
4734 do_cleanups (this_cmd_tree_chain);
4735
4736 if (breakpoint_proceeded)
4737 {
4738 if (current_ui->async)
4739 /* If we are in async mode, then the target might be still
4740 running, not stopped at any breakpoint, so nothing for
4741 us to do here -- just return to the event loop. */
4742 ;
4743 else
4744 /* In sync mode, when execute_control_command returns
4745 we're already standing on the next breakpoint.
4746 Breakpoint commands for that stop were not run, since
4747 execute_command does not run breakpoint commands --
4748 only command_line_handler does, but that one is not
4749 involved in execution of breakpoint commands. So, we
4750 can now execute breakpoint commands. It should be
4751 noted that making execute_command do bpstat actions is
4752 not an option -- in this case we'll have recursive
4753 invocation of bpstat for each breakpoint with a
4754 command, and can easily blow up GDB stack. Instead, we
4755 return true, which will trigger the caller to recall us
4756 with the new stop_bpstat. */
4757 again = 1;
4758 break;
4759 }
4760 }
4761 do_cleanups (old_chain);
4762 return again;
4763 }
4764
4765 void
4766 bpstat_do_actions (void)
4767 {
4768 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4769
4770 /* Do any commands attached to breakpoint we are stopped at. */
4771 while (!ptid_equal (inferior_ptid, null_ptid)
4772 && target_has_execution
4773 && !is_exited (inferior_ptid)
4774 && !is_executing (inferior_ptid))
4775 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4776 and only return when it is stopped at the next breakpoint, we
4777 keep doing breakpoint actions until it returns false to
4778 indicate the inferior was not resumed. */
4779 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4780 break;
4781
4782 discard_cleanups (cleanup_if_error);
4783 }
4784
4785 /* Print out the (old or new) value associated with a watchpoint. */
4786
4787 static void
4788 watchpoint_value_print (struct value *val, struct ui_file *stream)
4789 {
4790 if (val == NULL)
4791 fprintf_unfiltered (stream, _("<unreadable>"));
4792 else
4793 {
4794 struct value_print_options opts;
4795 get_user_print_options (&opts);
4796 value_print (val, stream, &opts);
4797 }
4798 }
4799
4800 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4801 debugging multiple threads. */
4802
4803 void
4804 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4805 {
4806 if (uiout->is_mi_like_p ())
4807 return;
4808
4809 uiout->text ("\n");
4810
4811 if (show_thread_that_caused_stop ())
4812 {
4813 const char *name;
4814 struct thread_info *thr = inferior_thread ();
4815
4816 uiout->text ("Thread ");
4817 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4818
4819 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4820 if (name != NULL)
4821 {
4822 uiout->text (" \"");
4823 uiout->field_fmt ("name", "%s", name);
4824 uiout->text ("\"");
4825 }
4826
4827 uiout->text (" hit ");
4828 }
4829 }
4830
4831 /* Generic routine for printing messages indicating why we
4832 stopped. The behavior of this function depends on the value
4833 'print_it' in the bpstat structure. Under some circumstances we
4834 may decide not to print anything here and delegate the task to
4835 normal_stop(). */
4836
4837 static enum print_stop_action
4838 print_bp_stop_message (bpstat bs)
4839 {
4840 switch (bs->print_it)
4841 {
4842 case print_it_noop:
4843 /* Nothing should be printed for this bpstat entry. */
4844 return PRINT_UNKNOWN;
4845 break;
4846
4847 case print_it_done:
4848 /* We still want to print the frame, but we already printed the
4849 relevant messages. */
4850 return PRINT_SRC_AND_LOC;
4851 break;
4852
4853 case print_it_normal:
4854 {
4855 struct breakpoint *b = bs->breakpoint_at;
4856
4857 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4858 which has since been deleted. */
4859 if (b == NULL)
4860 return PRINT_UNKNOWN;
4861
4862 /* Normal case. Call the breakpoint's print_it method. */
4863 return b->ops->print_it (bs);
4864 }
4865 break;
4866
4867 default:
4868 internal_error (__FILE__, __LINE__,
4869 _("print_bp_stop_message: unrecognized enum value"));
4870 break;
4871 }
4872 }
4873
4874 /* A helper function that prints a shared library stopped event. */
4875
4876 static void
4877 print_solib_event (int is_catchpoint)
4878 {
4879 int any_deleted
4880 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4881 int any_added
4882 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4883
4884 if (!is_catchpoint)
4885 {
4886 if (any_added || any_deleted)
4887 current_uiout->text (_("Stopped due to shared library event:\n"));
4888 else
4889 current_uiout->text (_("Stopped due to shared library event (no "
4890 "libraries added or removed)\n"));
4891 }
4892
4893 if (current_uiout->is_mi_like_p ())
4894 current_uiout->field_string ("reason",
4895 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4896
4897 if (any_deleted)
4898 {
4899 char *name;
4900 int ix;
4901
4902 current_uiout->text (_(" Inferior unloaded "));
4903 ui_out_emit_list list_emitter (current_uiout, "removed");
4904 for (ix = 0;
4905 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4906 ix, name);
4907 ++ix)
4908 {
4909 if (ix > 0)
4910 current_uiout->text (" ");
4911 current_uiout->field_string ("library", name);
4912 current_uiout->text ("\n");
4913 }
4914 }
4915
4916 if (any_added)
4917 {
4918 struct so_list *iter;
4919 int ix;
4920
4921 current_uiout->text (_(" Inferior loaded "));
4922 ui_out_emit_list list_emitter (current_uiout, "added");
4923 for (ix = 0;
4924 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4925 ix, iter);
4926 ++ix)
4927 {
4928 if (ix > 0)
4929 current_uiout->text (" ");
4930 current_uiout->field_string ("library", iter->so_name);
4931 current_uiout->text ("\n");
4932 }
4933 }
4934 }
4935
4936 /* Print a message indicating what happened. This is called from
4937 normal_stop(). The input to this routine is the head of the bpstat
4938 list - a list of the eventpoints that caused this stop. KIND is
4939 the target_waitkind for the stopping event. This
4940 routine calls the generic print routine for printing a message
4941 about reasons for stopping. This will print (for example) the
4942 "Breakpoint n," part of the output. The return value of this
4943 routine is one of:
4944
4945 PRINT_UNKNOWN: Means we printed nothing.
4946 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4947 code to print the location. An example is
4948 "Breakpoint 1, " which should be followed by
4949 the location.
4950 PRINT_SRC_ONLY: Means we printed something, but there is no need
4951 to also print the location part of the message.
4952 An example is the catch/throw messages, which
4953 don't require a location appended to the end.
4954 PRINT_NOTHING: We have done some printing and we don't need any
4955 further info to be printed. */
4956
4957 enum print_stop_action
4958 bpstat_print (bpstat bs, int kind)
4959 {
4960 enum print_stop_action val;
4961
4962 /* Maybe another breakpoint in the chain caused us to stop.
4963 (Currently all watchpoints go on the bpstat whether hit or not.
4964 That probably could (should) be changed, provided care is taken
4965 with respect to bpstat_explains_signal). */
4966 for (; bs; bs = bs->next)
4967 {
4968 val = print_bp_stop_message (bs);
4969 if (val == PRINT_SRC_ONLY
4970 || val == PRINT_SRC_AND_LOC
4971 || val == PRINT_NOTHING)
4972 return val;
4973 }
4974
4975 /* If we had hit a shared library event breakpoint,
4976 print_bp_stop_message would print out this message. If we hit an
4977 OS-level shared library event, do the same thing. */
4978 if (kind == TARGET_WAITKIND_LOADED)
4979 {
4980 print_solib_event (0);
4981 return PRINT_NOTHING;
4982 }
4983
4984 /* We reached the end of the chain, or we got a null BS to start
4985 with and nothing was printed. */
4986 return PRINT_UNKNOWN;
4987 }
4988
4989 /* Evaluate the expression EXP and return 1 if value is zero.
4990 This returns the inverse of the condition because it is called
4991 from catch_errors which returns 0 if an exception happened, and if an
4992 exception happens we want execution to stop.
4993 The argument is a "struct expression *" that has been cast to a
4994 "void *" to make it pass through catch_errors. */
4995
4996 static int
4997 breakpoint_cond_eval (void *exp)
4998 {
4999 struct value *mark = value_mark ();
5000 int i = !value_true (evaluate_expression ((struct expression *) exp));
5001
5002 value_free_to_mark (mark);
5003 return i;
5004 }
5005
5006 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5007
5008 bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
5009 : next (NULL),
5010 bp_location_at (bl),
5011 breakpoint_at (bl->owner),
5012 commands (NULL),
5013 old_val (NULL),
5014 print (0),
5015 stop (0),
5016 print_it (print_it_normal)
5017 {
5018 incref_bp_location (bl);
5019 **bs_link_pointer = this;
5020 *bs_link_pointer = &next;
5021 }
5022
5023 bpstats::bpstats ()
5024 : next (NULL),
5025 bp_location_at (NULL),
5026 breakpoint_at (NULL),
5027 commands (NULL),
5028 old_val (NULL),
5029 print (0),
5030 stop (0),
5031 print_it (print_it_normal)
5032 {
5033 }
5034 \f
5035 /* The target has stopped with waitstatus WS. Check if any hardware
5036 watchpoints have triggered, according to the target. */
5037
5038 int
5039 watchpoints_triggered (struct target_waitstatus *ws)
5040 {
5041 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5042 CORE_ADDR addr;
5043 struct breakpoint *b;
5044
5045 if (!stopped_by_watchpoint)
5046 {
5047 /* We were not stopped by a watchpoint. Mark all watchpoints
5048 as not triggered. */
5049 ALL_BREAKPOINTS (b)
5050 if (is_hardware_watchpoint (b))
5051 {
5052 struct watchpoint *w = (struct watchpoint *) b;
5053
5054 w->watchpoint_triggered = watch_triggered_no;
5055 }
5056
5057 return 0;
5058 }
5059
5060 if (!target_stopped_data_address (&current_target, &addr))
5061 {
5062 /* We were stopped by a watchpoint, but we don't know where.
5063 Mark all watchpoints as unknown. */
5064 ALL_BREAKPOINTS (b)
5065 if (is_hardware_watchpoint (b))
5066 {
5067 struct watchpoint *w = (struct watchpoint *) b;
5068
5069 w->watchpoint_triggered = watch_triggered_unknown;
5070 }
5071
5072 return 1;
5073 }
5074
5075 /* The target could report the data address. Mark watchpoints
5076 affected by this data address as triggered, and all others as not
5077 triggered. */
5078
5079 ALL_BREAKPOINTS (b)
5080 if (is_hardware_watchpoint (b))
5081 {
5082 struct watchpoint *w = (struct watchpoint *) b;
5083 struct bp_location *loc;
5084
5085 w->watchpoint_triggered = watch_triggered_no;
5086 for (loc = b->loc; loc; loc = loc->next)
5087 {
5088 if (is_masked_watchpoint (b))
5089 {
5090 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5091 CORE_ADDR start = loc->address & w->hw_wp_mask;
5092
5093 if (newaddr == start)
5094 {
5095 w->watchpoint_triggered = watch_triggered_yes;
5096 break;
5097 }
5098 }
5099 /* Exact match not required. Within range is sufficient. */
5100 else if (target_watchpoint_addr_within_range (&current_target,
5101 addr, loc->address,
5102 loc->length))
5103 {
5104 w->watchpoint_triggered = watch_triggered_yes;
5105 break;
5106 }
5107 }
5108 }
5109
5110 return 1;
5111 }
5112
5113 /* Possible return values for watchpoint_check (this can't be an enum
5114 because of check_errors). */
5115 /* The watchpoint has been deleted. */
5116 #define WP_DELETED 1
5117 /* The value has changed. */
5118 #define WP_VALUE_CHANGED 2
5119 /* The value has not changed. */
5120 #define WP_VALUE_NOT_CHANGED 3
5121 /* Ignore this watchpoint, no matter if the value changed or not. */
5122 #define WP_IGNORE 4
5123
5124 #define BP_TEMPFLAG 1
5125 #define BP_HARDWAREFLAG 2
5126
5127 /* Evaluate watchpoint condition expression and check if its value
5128 changed.
5129
5130 P should be a pointer to struct bpstat, but is defined as a void *
5131 in order for this function to be usable with catch_errors. */
5132
5133 static int
5134 watchpoint_check (void *p)
5135 {
5136 bpstat bs = (bpstat) p;
5137 struct watchpoint *b;
5138 struct frame_info *fr;
5139 int within_current_scope;
5140
5141 /* BS is built from an existing struct breakpoint. */
5142 gdb_assert (bs->breakpoint_at != NULL);
5143 b = (struct watchpoint *) bs->breakpoint_at;
5144
5145 /* If this is a local watchpoint, we only want to check if the
5146 watchpoint frame is in scope if the current thread is the thread
5147 that was used to create the watchpoint. */
5148 if (!watchpoint_in_thread_scope (b))
5149 return WP_IGNORE;
5150
5151 if (b->exp_valid_block == NULL)
5152 within_current_scope = 1;
5153 else
5154 {
5155 struct frame_info *frame = get_current_frame ();
5156 struct gdbarch *frame_arch = get_frame_arch (frame);
5157 CORE_ADDR frame_pc = get_frame_pc (frame);
5158
5159 /* stack_frame_destroyed_p() returns a non-zero value if we're
5160 still in the function but the stack frame has already been
5161 invalidated. Since we can't rely on the values of local
5162 variables after the stack has been destroyed, we are treating
5163 the watchpoint in that state as `not changed' without further
5164 checking. Don't mark watchpoints as changed if the current
5165 frame is in an epilogue - even if they are in some other
5166 frame, our view of the stack is likely to be wrong and
5167 frame_find_by_id could error out. */
5168 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5169 return WP_IGNORE;
5170
5171 fr = frame_find_by_id (b->watchpoint_frame);
5172 within_current_scope = (fr != NULL);
5173
5174 /* If we've gotten confused in the unwinder, we might have
5175 returned a frame that can't describe this variable. */
5176 if (within_current_scope)
5177 {
5178 struct symbol *function;
5179
5180 function = get_frame_function (fr);
5181 if (function == NULL
5182 || !contained_in (b->exp_valid_block,
5183 SYMBOL_BLOCK_VALUE (function)))
5184 within_current_scope = 0;
5185 }
5186
5187 if (within_current_scope)
5188 /* If we end up stopping, the current frame will get selected
5189 in normal_stop. So this call to select_frame won't affect
5190 the user. */
5191 select_frame (fr);
5192 }
5193
5194 if (within_current_scope)
5195 {
5196 /* We use value_{,free_to_}mark because it could be a *long*
5197 time before we return to the command level and call
5198 free_all_values. We can't call free_all_values because we
5199 might be in the middle of evaluating a function call. */
5200
5201 int pc = 0;
5202 struct value *mark;
5203 struct value *new_val;
5204
5205 if (is_masked_watchpoint (b))
5206 /* Since we don't know the exact trigger address (from
5207 stopped_data_address), just tell the user we've triggered
5208 a mask watchpoint. */
5209 return WP_VALUE_CHANGED;
5210
5211 mark = value_mark ();
5212 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5213
5214 if (b->val_bitsize != 0)
5215 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5216
5217 /* We use value_equal_contents instead of value_equal because
5218 the latter coerces an array to a pointer, thus comparing just
5219 the address of the array instead of its contents. This is
5220 not what we want. */
5221 if ((b->val != NULL) != (new_val != NULL)
5222 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5223 {
5224 if (new_val != NULL)
5225 {
5226 release_value (new_val);
5227 value_free_to_mark (mark);
5228 }
5229 bs->old_val = b->val;
5230 b->val = new_val;
5231 b->val_valid = 1;
5232 return WP_VALUE_CHANGED;
5233 }
5234 else
5235 {
5236 /* Nothing changed. */
5237 value_free_to_mark (mark);
5238 return WP_VALUE_NOT_CHANGED;
5239 }
5240 }
5241 else
5242 {
5243 /* This seems like the only logical thing to do because
5244 if we temporarily ignored the watchpoint, then when
5245 we reenter the block in which it is valid it contains
5246 garbage (in the case of a function, it may have two
5247 garbage values, one before and one after the prologue).
5248 So we can't even detect the first assignment to it and
5249 watch after that (since the garbage may or may not equal
5250 the first value assigned). */
5251 /* We print all the stop information in
5252 breakpoint_ops->print_it, but in this case, by the time we
5253 call breakpoint_ops->print_it this bp will be deleted
5254 already. So we have no choice but print the information
5255 here. */
5256
5257 SWITCH_THRU_ALL_UIS ()
5258 {
5259 struct ui_out *uiout = current_uiout;
5260
5261 if (uiout->is_mi_like_p ())
5262 uiout->field_string
5263 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5264 uiout->text ("\nWatchpoint ");
5265 uiout->field_int ("wpnum", b->number);
5266 uiout->text (" deleted because the program has left the block in\n"
5267 "which its expression is valid.\n");
5268 }
5269
5270 /* Make sure the watchpoint's commands aren't executed. */
5271 decref_counted_command_line (&b->commands);
5272 watchpoint_del_at_next_stop (b);
5273
5274 return WP_DELETED;
5275 }
5276 }
5277
5278 /* Return true if it looks like target has stopped due to hitting
5279 breakpoint location BL. This function does not check if we should
5280 stop, only if BL explains the stop. */
5281
5282 static int
5283 bpstat_check_location (const struct bp_location *bl,
5284 struct address_space *aspace, CORE_ADDR bp_addr,
5285 const struct target_waitstatus *ws)
5286 {
5287 struct breakpoint *b = bl->owner;
5288
5289 /* BL is from an existing breakpoint. */
5290 gdb_assert (b != NULL);
5291
5292 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5293 }
5294
5295 /* Determine if the watched values have actually changed, and we
5296 should stop. If not, set BS->stop to 0. */
5297
5298 static void
5299 bpstat_check_watchpoint (bpstat bs)
5300 {
5301 const struct bp_location *bl;
5302 struct watchpoint *b;
5303
5304 /* BS is built for existing struct breakpoint. */
5305 bl = bs->bp_location_at;
5306 gdb_assert (bl != NULL);
5307 b = (struct watchpoint *) bs->breakpoint_at;
5308 gdb_assert (b != NULL);
5309
5310 {
5311 int must_check_value = 0;
5312
5313 if (b->type == bp_watchpoint)
5314 /* For a software watchpoint, we must always check the
5315 watched value. */
5316 must_check_value = 1;
5317 else if (b->watchpoint_triggered == watch_triggered_yes)
5318 /* We have a hardware watchpoint (read, write, or access)
5319 and the target earlier reported an address watched by
5320 this watchpoint. */
5321 must_check_value = 1;
5322 else if (b->watchpoint_triggered == watch_triggered_unknown
5323 && b->type == bp_hardware_watchpoint)
5324 /* We were stopped by a hardware watchpoint, but the target could
5325 not report the data address. We must check the watchpoint's
5326 value. Access and read watchpoints are out of luck; without
5327 a data address, we can't figure it out. */
5328 must_check_value = 1;
5329
5330 if (must_check_value)
5331 {
5332 char *message
5333 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5334 b->number);
5335 struct cleanup *cleanups = make_cleanup (xfree, message);
5336 int e = catch_errors (watchpoint_check, bs, message,
5337 RETURN_MASK_ALL);
5338 do_cleanups (cleanups);
5339 switch (e)
5340 {
5341 case WP_DELETED:
5342 /* We've already printed what needs to be printed. */
5343 bs->print_it = print_it_done;
5344 /* Stop. */
5345 break;
5346 case WP_IGNORE:
5347 bs->print_it = print_it_noop;
5348 bs->stop = 0;
5349 break;
5350 case WP_VALUE_CHANGED:
5351 if (b->type == bp_read_watchpoint)
5352 {
5353 /* There are two cases to consider here:
5354
5355 1. We're watching the triggered memory for reads.
5356 In that case, trust the target, and always report
5357 the watchpoint hit to the user. Even though
5358 reads don't cause value changes, the value may
5359 have changed since the last time it was read, and
5360 since we're not trapping writes, we will not see
5361 those, and as such we should ignore our notion of
5362 old value.
5363
5364 2. We're watching the triggered memory for both
5365 reads and writes. There are two ways this may
5366 happen:
5367
5368 2.1. This is a target that can't break on data
5369 reads only, but can break on accesses (reads or
5370 writes), such as e.g., x86. We detect this case
5371 at the time we try to insert read watchpoints.
5372
5373 2.2. Otherwise, the target supports read
5374 watchpoints, but, the user set an access or write
5375 watchpoint watching the same memory as this read
5376 watchpoint.
5377
5378 If we're watching memory writes as well as reads,
5379 ignore watchpoint hits when we find that the
5380 value hasn't changed, as reads don't cause
5381 changes. This still gives false positives when
5382 the program writes the same value to memory as
5383 what there was already in memory (we will confuse
5384 it for a read), but it's much better than
5385 nothing. */
5386
5387 int other_write_watchpoint = 0;
5388
5389 if (bl->watchpoint_type == hw_read)
5390 {
5391 struct breakpoint *other_b;
5392
5393 ALL_BREAKPOINTS (other_b)
5394 if (other_b->type == bp_hardware_watchpoint
5395 || other_b->type == bp_access_watchpoint)
5396 {
5397 struct watchpoint *other_w =
5398 (struct watchpoint *) other_b;
5399
5400 if (other_w->watchpoint_triggered
5401 == watch_triggered_yes)
5402 {
5403 other_write_watchpoint = 1;
5404 break;
5405 }
5406 }
5407 }
5408
5409 if (other_write_watchpoint
5410 || bl->watchpoint_type == hw_access)
5411 {
5412 /* We're watching the same memory for writes,
5413 and the value changed since the last time we
5414 updated it, so this trap must be for a write.
5415 Ignore it. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
5419 }
5420 break;
5421 case WP_VALUE_NOT_CHANGED:
5422 if (b->type == bp_hardware_watchpoint
5423 || b->type == bp_watchpoint)
5424 {
5425 /* Don't stop: write watchpoints shouldn't fire if
5426 the value hasn't changed. */
5427 bs->print_it = print_it_noop;
5428 bs->stop = 0;
5429 }
5430 /* Stop. */
5431 break;
5432 default:
5433 /* Can't happen. */
5434 case 0:
5435 /* Error from catch_errors. */
5436 {
5437 SWITCH_THRU_ALL_UIS ()
5438 {
5439 printf_filtered (_("Watchpoint %d deleted.\n"),
5440 b->number);
5441 }
5442 watchpoint_del_at_next_stop (b);
5443 /* We've already printed what needs to be printed. */
5444 bs->print_it = print_it_done;
5445 }
5446 break;
5447 }
5448 }
5449 else /* must_check_value == 0 */
5450 {
5451 /* This is a case where some watchpoint(s) triggered, but
5452 not at the address of this watchpoint, or else no
5453 watchpoint triggered after all. So don't print
5454 anything for this watchpoint. */
5455 bs->print_it = print_it_noop;
5456 bs->stop = 0;
5457 }
5458 }
5459 }
5460
5461 /* For breakpoints that are currently marked as telling gdb to stop,
5462 check conditions (condition proper, frame, thread and ignore count)
5463 of breakpoint referred to by BS. If we should not stop for this
5464 breakpoint, set BS->stop to 0. */
5465
5466 static void
5467 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5468 {
5469 const struct bp_location *bl;
5470 struct breakpoint *b;
5471 int value_is_zero = 0;
5472 struct expression *cond;
5473
5474 gdb_assert (bs->stop);
5475
5476 /* BS is built for existing struct breakpoint. */
5477 bl = bs->bp_location_at;
5478 gdb_assert (bl != NULL);
5479 b = bs->breakpoint_at;
5480 gdb_assert (b != NULL);
5481
5482 /* Even if the target evaluated the condition on its end and notified GDB, we
5483 need to do so again since GDB does not know if we stopped due to a
5484 breakpoint or a single step breakpoint. */
5485
5486 if (frame_id_p (b->frame_id)
5487 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5488 {
5489 bs->stop = 0;
5490 return;
5491 }
5492
5493 /* If this is a thread/task-specific breakpoint, don't waste cpu
5494 evaluating the condition if this isn't the specified
5495 thread/task. */
5496 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5497 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5498
5499 {
5500 bs->stop = 0;
5501 return;
5502 }
5503
5504 /* Evaluate extension language breakpoints that have a "stop" method
5505 implemented. */
5506 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5507
5508 if (is_watchpoint (b))
5509 {
5510 struct watchpoint *w = (struct watchpoint *) b;
5511
5512 cond = w->cond_exp.get ();
5513 }
5514 else
5515 cond = bl->cond.get ();
5516
5517 if (cond && b->disposition != disp_del_at_next_stop)
5518 {
5519 int within_current_scope = 1;
5520 struct watchpoint * w;
5521
5522 /* We use value_mark and value_free_to_mark because it could
5523 be a long time before we return to the command level and
5524 call free_all_values. We can't call free_all_values
5525 because we might be in the middle of evaluating a
5526 function call. */
5527 struct value *mark = value_mark ();
5528
5529 if (is_watchpoint (b))
5530 w = (struct watchpoint *) b;
5531 else
5532 w = NULL;
5533
5534 /* Need to select the frame, with all that implies so that
5535 the conditions will have the right context. Because we
5536 use the frame, we will not see an inlined function's
5537 variables when we arrive at a breakpoint at the start
5538 of the inlined function; the current frame will be the
5539 call site. */
5540 if (w == NULL || w->cond_exp_valid_block == NULL)
5541 select_frame (get_current_frame ());
5542 else
5543 {
5544 struct frame_info *frame;
5545
5546 /* For local watchpoint expressions, which particular
5547 instance of a local is being watched matters, so we
5548 keep track of the frame to evaluate the expression
5549 in. To evaluate the condition however, it doesn't
5550 really matter which instantiation of the function
5551 where the condition makes sense triggers the
5552 watchpoint. This allows an expression like "watch
5553 global if q > 10" set in `func', catch writes to
5554 global on all threads that call `func', or catch
5555 writes on all recursive calls of `func' by a single
5556 thread. We simply always evaluate the condition in
5557 the innermost frame that's executing where it makes
5558 sense to evaluate the condition. It seems
5559 intuitive. */
5560 frame = block_innermost_frame (w->cond_exp_valid_block);
5561 if (frame != NULL)
5562 select_frame (frame);
5563 else
5564 within_current_scope = 0;
5565 }
5566 if (within_current_scope)
5567 value_is_zero
5568 = catch_errors (breakpoint_cond_eval, cond,
5569 "Error in testing breakpoint condition:\n",
5570 RETURN_MASK_ALL);
5571 else
5572 {
5573 warning (_("Watchpoint condition cannot be tested "
5574 "in the current scope"));
5575 /* If we failed to set the right context for this
5576 watchpoint, unconditionally report it. */
5577 value_is_zero = 0;
5578 }
5579 /* FIXME-someday, should give breakpoint #. */
5580 value_free_to_mark (mark);
5581 }
5582
5583 if (cond && value_is_zero)
5584 {
5585 bs->stop = 0;
5586 }
5587 else if (b->ignore_count > 0)
5588 {
5589 b->ignore_count--;
5590 bs->stop = 0;
5591 /* Increase the hit count even though we don't stop. */
5592 ++(b->hit_count);
5593 observer_notify_breakpoint_modified (b);
5594 }
5595 }
5596
5597 /* Returns true if we need to track moribund locations of LOC's type
5598 on the current target. */
5599
5600 static int
5601 need_moribund_for_location_type (struct bp_location *loc)
5602 {
5603 return ((loc->loc_type == bp_loc_software_breakpoint
5604 && !target_supports_stopped_by_sw_breakpoint ())
5605 || (loc->loc_type == bp_loc_hardware_breakpoint
5606 && !target_supports_stopped_by_hw_breakpoint ()));
5607 }
5608
5609
5610 /* Get a bpstat associated with having just stopped at address
5611 BP_ADDR in thread PTID.
5612
5613 Determine whether we stopped at a breakpoint, etc, or whether we
5614 don't understand this stop. Result is a chain of bpstat's such
5615 that:
5616
5617 if we don't understand the stop, the result is a null pointer.
5618
5619 if we understand why we stopped, the result is not null.
5620
5621 Each element of the chain refers to a particular breakpoint or
5622 watchpoint at which we have stopped. (We may have stopped for
5623 several reasons concurrently.)
5624
5625 Each element of the chain has valid next, breakpoint_at,
5626 commands, FIXME??? fields. */
5627
5628 bpstat
5629 bpstat_stop_status (struct address_space *aspace,
5630 CORE_ADDR bp_addr, ptid_t ptid,
5631 const struct target_waitstatus *ws)
5632 {
5633 struct breakpoint *b = NULL;
5634 struct bp_location *bl;
5635 struct bp_location *loc;
5636 /* First item of allocated bpstat's. */
5637 bpstat bs_head = NULL, *bs_link = &bs_head;
5638 /* Pointer to the last thing in the chain currently. */
5639 bpstat bs;
5640 int ix;
5641 int need_remove_insert;
5642 int removed_any;
5643
5644 /* First, build the bpstat chain with locations that explain a
5645 target stop, while being careful to not set the target running,
5646 as that may invalidate locations (in particular watchpoint
5647 locations are recreated). Resuming will happen here with
5648 breakpoint conditions or watchpoint expressions that include
5649 inferior function calls. */
5650
5651 ALL_BREAKPOINTS (b)
5652 {
5653 if (!breakpoint_enabled (b))
5654 continue;
5655
5656 for (bl = b->loc; bl != NULL; bl = bl->next)
5657 {
5658 /* For hardware watchpoints, we look only at the first
5659 location. The watchpoint_check function will work on the
5660 entire expression, not the individual locations. For
5661 read watchpoints, the watchpoints_triggered function has
5662 checked all locations already. */
5663 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5664 break;
5665
5666 if (!bl->enabled || bl->shlib_disabled)
5667 continue;
5668
5669 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5670 continue;
5671
5672 /* Come here if it's a watchpoint, or if the break address
5673 matches. */
5674
5675 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5676 explain stop. */
5677
5678 /* Assume we stop. Should we find a watchpoint that is not
5679 actually triggered, or if the condition of the breakpoint
5680 evaluates as false, we'll reset 'stop' to 0. */
5681 bs->stop = 1;
5682 bs->print = 1;
5683
5684 /* If this is a scope breakpoint, mark the associated
5685 watchpoint as triggered so that we will handle the
5686 out-of-scope event. We'll get to the watchpoint next
5687 iteration. */
5688 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5689 {
5690 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5691
5692 w->watchpoint_triggered = watch_triggered_yes;
5693 }
5694 }
5695 }
5696
5697 /* Check if a moribund breakpoint explains the stop. */
5698 if (!target_supports_stopped_by_sw_breakpoint ()
5699 || !target_supports_stopped_by_hw_breakpoint ())
5700 {
5701 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5702 {
5703 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5704 && need_moribund_for_location_type (loc))
5705 {
5706 bs = new bpstats (loc, &bs_link);
5707 /* For hits of moribund locations, we should just proceed. */
5708 bs->stop = 0;
5709 bs->print = 0;
5710 bs->print_it = print_it_noop;
5711 }
5712 }
5713 }
5714
5715 /* A bit of special processing for shlib breakpoints. We need to
5716 process solib loading here, so that the lists of loaded and
5717 unloaded libraries are correct before we handle "catch load" and
5718 "catch unload". */
5719 for (bs = bs_head; bs != NULL; bs = bs->next)
5720 {
5721 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5722 {
5723 handle_solib_event ();
5724 break;
5725 }
5726 }
5727
5728 /* Now go through the locations that caused the target to stop, and
5729 check whether we're interested in reporting this stop to higher
5730 layers, or whether we should resume the target transparently. */
5731
5732 removed_any = 0;
5733
5734 for (bs = bs_head; bs != NULL; bs = bs->next)
5735 {
5736 if (!bs->stop)
5737 continue;
5738
5739 b = bs->breakpoint_at;
5740 b->ops->check_status (bs);
5741 if (bs->stop)
5742 {
5743 bpstat_check_breakpoint_conditions (bs, ptid);
5744
5745 if (bs->stop)
5746 {
5747 ++(b->hit_count);
5748 observer_notify_breakpoint_modified (b);
5749
5750 /* We will stop here. */
5751 if (b->disposition == disp_disable)
5752 {
5753 --(b->enable_count);
5754 if (b->enable_count <= 0)
5755 b->enable_state = bp_disabled;
5756 removed_any = 1;
5757 }
5758 if (b->silent)
5759 bs->print = 0;
5760 bs->commands = b->commands;
5761 incref_counted_command_line (bs->commands);
5762 if (command_line_is_silent (bs->commands
5763 ? bs->commands->commands : NULL))
5764 bs->print = 0;
5765
5766 b->ops->after_condition_true (bs);
5767 }
5768
5769 }
5770
5771 /* Print nothing for this entry if we don't stop or don't
5772 print. */
5773 if (!bs->stop || !bs->print)
5774 bs->print_it = print_it_noop;
5775 }
5776
5777 /* If we aren't stopping, the value of some hardware watchpoint may
5778 not have changed, but the intermediate memory locations we are
5779 watching may have. Don't bother if we're stopping; this will get
5780 done later. */
5781 need_remove_insert = 0;
5782 if (! bpstat_causes_stop (bs_head))
5783 for (bs = bs_head; bs != NULL; bs = bs->next)
5784 if (!bs->stop
5785 && bs->breakpoint_at
5786 && is_hardware_watchpoint (bs->breakpoint_at))
5787 {
5788 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5789
5790 update_watchpoint (w, 0 /* don't reparse. */);
5791 need_remove_insert = 1;
5792 }
5793
5794 if (need_remove_insert)
5795 update_global_location_list (UGLL_MAY_INSERT);
5796 else if (removed_any)
5797 update_global_location_list (UGLL_DONT_INSERT);
5798
5799 return bs_head;
5800 }
5801
5802 static void
5803 handle_jit_event (void)
5804 {
5805 struct frame_info *frame;
5806 struct gdbarch *gdbarch;
5807
5808 if (debug_infrun)
5809 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5810
5811 /* Switch terminal for any messages produced by
5812 breakpoint_re_set. */
5813 target_terminal::ours_for_output ();
5814
5815 frame = get_current_frame ();
5816 gdbarch = get_frame_arch (frame);
5817
5818 jit_event_handler (gdbarch);
5819
5820 target_terminal::inferior ();
5821 }
5822
5823 /* Prepare WHAT final decision for infrun. */
5824
5825 /* Decide what infrun needs to do with this bpstat. */
5826
5827 struct bpstat_what
5828 bpstat_what (bpstat bs_head)
5829 {
5830 struct bpstat_what retval;
5831 bpstat bs;
5832
5833 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5834 retval.call_dummy = STOP_NONE;
5835 retval.is_longjmp = 0;
5836
5837 for (bs = bs_head; bs != NULL; bs = bs->next)
5838 {
5839 /* Extract this BS's action. After processing each BS, we check
5840 if its action overrides all we've seem so far. */
5841 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5842 enum bptype bptype;
5843
5844 if (bs->breakpoint_at == NULL)
5845 {
5846 /* I suspect this can happen if it was a momentary
5847 breakpoint which has since been deleted. */
5848 bptype = bp_none;
5849 }
5850 else
5851 bptype = bs->breakpoint_at->type;
5852
5853 switch (bptype)
5854 {
5855 case bp_none:
5856 break;
5857 case bp_breakpoint:
5858 case bp_hardware_breakpoint:
5859 case bp_single_step:
5860 case bp_until:
5861 case bp_finish:
5862 case bp_shlib_event:
5863 if (bs->stop)
5864 {
5865 if (bs->print)
5866 this_action = BPSTAT_WHAT_STOP_NOISY;
5867 else
5868 this_action = BPSTAT_WHAT_STOP_SILENT;
5869 }
5870 else
5871 this_action = BPSTAT_WHAT_SINGLE;
5872 break;
5873 case bp_watchpoint:
5874 case bp_hardware_watchpoint:
5875 case bp_read_watchpoint:
5876 case bp_access_watchpoint:
5877 if (bs->stop)
5878 {
5879 if (bs->print)
5880 this_action = BPSTAT_WHAT_STOP_NOISY;
5881 else
5882 this_action = BPSTAT_WHAT_STOP_SILENT;
5883 }
5884 else
5885 {
5886 /* There was a watchpoint, but we're not stopping.
5887 This requires no further action. */
5888 }
5889 break;
5890 case bp_longjmp:
5891 case bp_longjmp_call_dummy:
5892 case bp_exception:
5893 if (bs->stop)
5894 {
5895 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5896 retval.is_longjmp = bptype != bp_exception;
5897 }
5898 else
5899 this_action = BPSTAT_WHAT_SINGLE;
5900 break;
5901 case bp_longjmp_resume:
5902 case bp_exception_resume:
5903 if (bs->stop)
5904 {
5905 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5906 retval.is_longjmp = bptype == bp_longjmp_resume;
5907 }
5908 else
5909 this_action = BPSTAT_WHAT_SINGLE;
5910 break;
5911 case bp_step_resume:
5912 if (bs->stop)
5913 this_action = BPSTAT_WHAT_STEP_RESUME;
5914 else
5915 {
5916 /* It is for the wrong frame. */
5917 this_action = BPSTAT_WHAT_SINGLE;
5918 }
5919 break;
5920 case bp_hp_step_resume:
5921 if (bs->stop)
5922 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5923 else
5924 {
5925 /* It is for the wrong frame. */
5926 this_action = BPSTAT_WHAT_SINGLE;
5927 }
5928 break;
5929 case bp_watchpoint_scope:
5930 case bp_thread_event:
5931 case bp_overlay_event:
5932 case bp_longjmp_master:
5933 case bp_std_terminate_master:
5934 case bp_exception_master:
5935 this_action = BPSTAT_WHAT_SINGLE;
5936 break;
5937 case bp_catchpoint:
5938 if (bs->stop)
5939 {
5940 if (bs->print)
5941 this_action = BPSTAT_WHAT_STOP_NOISY;
5942 else
5943 this_action = BPSTAT_WHAT_STOP_SILENT;
5944 }
5945 else
5946 {
5947 /* There was a catchpoint, but we're not stopping.
5948 This requires no further action. */
5949 }
5950 break;
5951 case bp_jit_event:
5952 this_action = BPSTAT_WHAT_SINGLE;
5953 break;
5954 case bp_call_dummy:
5955 /* Make sure the action is stop (silent or noisy),
5956 so infrun.c pops the dummy frame. */
5957 retval.call_dummy = STOP_STACK_DUMMY;
5958 this_action = BPSTAT_WHAT_STOP_SILENT;
5959 break;
5960 case bp_std_terminate:
5961 /* Make sure the action is stop (silent or noisy),
5962 so infrun.c pops the dummy frame. */
5963 retval.call_dummy = STOP_STD_TERMINATE;
5964 this_action = BPSTAT_WHAT_STOP_SILENT;
5965 break;
5966 case bp_tracepoint:
5967 case bp_fast_tracepoint:
5968 case bp_static_tracepoint:
5969 /* Tracepoint hits should not be reported back to GDB, and
5970 if one got through somehow, it should have been filtered
5971 out already. */
5972 internal_error (__FILE__, __LINE__,
5973 _("bpstat_what: tracepoint encountered"));
5974 break;
5975 case bp_gnu_ifunc_resolver:
5976 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5977 this_action = BPSTAT_WHAT_SINGLE;
5978 break;
5979 case bp_gnu_ifunc_resolver_return:
5980 /* The breakpoint will be removed, execution will restart from the
5981 PC of the former breakpoint. */
5982 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5983 break;
5984
5985 case bp_dprintf:
5986 if (bs->stop)
5987 this_action = BPSTAT_WHAT_STOP_SILENT;
5988 else
5989 this_action = BPSTAT_WHAT_SINGLE;
5990 break;
5991
5992 default:
5993 internal_error (__FILE__, __LINE__,
5994 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5995 }
5996
5997 retval.main_action = std::max (retval.main_action, this_action);
5998 }
5999
6000 return retval;
6001 }
6002
6003 void
6004 bpstat_run_callbacks (bpstat bs_head)
6005 {
6006 bpstat bs;
6007
6008 for (bs = bs_head; bs != NULL; bs = bs->next)
6009 {
6010 struct breakpoint *b = bs->breakpoint_at;
6011
6012 if (b == NULL)
6013 continue;
6014 switch (b->type)
6015 {
6016 case bp_jit_event:
6017 handle_jit_event ();
6018 break;
6019 case bp_gnu_ifunc_resolver:
6020 gnu_ifunc_resolver_stop (b);
6021 break;
6022 case bp_gnu_ifunc_resolver_return:
6023 gnu_ifunc_resolver_return_stop (b);
6024 break;
6025 }
6026 }
6027 }
6028
6029 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6030 without hardware support). This isn't related to a specific bpstat,
6031 just to things like whether watchpoints are set. */
6032
6033 int
6034 bpstat_should_step (void)
6035 {
6036 struct breakpoint *b;
6037
6038 ALL_BREAKPOINTS (b)
6039 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6040 return 1;
6041 return 0;
6042 }
6043
6044 int
6045 bpstat_causes_stop (bpstat bs)
6046 {
6047 for (; bs != NULL; bs = bs->next)
6048 if (bs->stop)
6049 return 1;
6050
6051 return 0;
6052 }
6053
6054 \f
6055
6056 /* Compute a string of spaces suitable to indent the next line
6057 so it starts at the position corresponding to the table column
6058 named COL_NAME in the currently active table of UIOUT. */
6059
6060 static char *
6061 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6062 {
6063 static char wrap_indent[80];
6064 int i, total_width, width, align;
6065 const char *text;
6066
6067 total_width = 0;
6068 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6069 {
6070 if (strcmp (text, col_name) == 0)
6071 {
6072 gdb_assert (total_width < sizeof wrap_indent);
6073 memset (wrap_indent, ' ', total_width);
6074 wrap_indent[total_width] = 0;
6075
6076 return wrap_indent;
6077 }
6078
6079 total_width += width + 1;
6080 }
6081
6082 return NULL;
6083 }
6084
6085 /* Determine if the locations of this breakpoint will have their conditions
6086 evaluated by the target, host or a mix of both. Returns the following:
6087
6088 "host": Host evals condition.
6089 "host or target": Host or Target evals condition.
6090 "target": Target evals condition.
6091 */
6092
6093 static const char *
6094 bp_condition_evaluator (struct breakpoint *b)
6095 {
6096 struct bp_location *bl;
6097 char host_evals = 0;
6098 char target_evals = 0;
6099
6100 if (!b)
6101 return NULL;
6102
6103 if (!is_breakpoint (b))
6104 return NULL;
6105
6106 if (gdb_evaluates_breakpoint_condition_p ()
6107 || !target_supports_evaluation_of_breakpoint_conditions ())
6108 return condition_evaluation_host;
6109
6110 for (bl = b->loc; bl; bl = bl->next)
6111 {
6112 if (bl->cond_bytecode)
6113 target_evals++;
6114 else
6115 host_evals++;
6116 }
6117
6118 if (host_evals && target_evals)
6119 return condition_evaluation_both;
6120 else if (target_evals)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124 }
6125
6126 /* Determine the breakpoint location's condition evaluator. This is
6127 similar to bp_condition_evaluator, but for locations. */
6128
6129 static const char *
6130 bp_location_condition_evaluator (struct bp_location *bl)
6131 {
6132 if (bl && !is_breakpoint (bl->owner))
6133 return NULL;
6134
6135 if (gdb_evaluates_breakpoint_condition_p ()
6136 || !target_supports_evaluation_of_breakpoint_conditions ())
6137 return condition_evaluation_host;
6138
6139 if (bl && bl->cond_bytecode)
6140 return condition_evaluation_target;
6141 else
6142 return condition_evaluation_host;
6143 }
6144
6145 /* Print the LOC location out of the list of B->LOC locations. */
6146
6147 static void
6148 print_breakpoint_location (struct breakpoint *b,
6149 struct bp_location *loc)
6150 {
6151 struct ui_out *uiout = current_uiout;
6152
6153 scoped_restore_current_program_space restore_pspace;
6154
6155 if (loc != NULL && loc->shlib_disabled)
6156 loc = NULL;
6157
6158 if (loc != NULL)
6159 set_current_program_space (loc->pspace);
6160
6161 if (b->display_canonical)
6162 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6163 else if (loc && loc->symtab)
6164 {
6165 struct symbol *sym
6166 = find_pc_sect_function (loc->address, loc->section);
6167 if (sym)
6168 {
6169 uiout->text ("in ");
6170 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6171 uiout->text (" ");
6172 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6173 uiout->text ("at ");
6174 }
6175 uiout->field_string ("file",
6176 symtab_to_filename_for_display (loc->symtab));
6177 uiout->text (":");
6178
6179 if (uiout->is_mi_like_p ())
6180 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6181
6182 uiout->field_int ("line", loc->line_number);
6183 }
6184 else if (loc)
6185 {
6186 string_file stb;
6187
6188 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6189 demangle, "");
6190 uiout->field_stream ("at", stb);
6191 }
6192 else
6193 {
6194 uiout->field_string ("pending",
6195 event_location_to_string (b->location.get ()));
6196 /* If extra_string is available, it could be holding a condition
6197 or dprintf arguments. In either case, make sure it is printed,
6198 too, but only for non-MI streams. */
6199 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6200 {
6201 if (b->type == bp_dprintf)
6202 uiout->text (",");
6203 else
6204 uiout->text (" ");
6205 uiout->text (b->extra_string);
6206 }
6207 }
6208
6209 if (loc && is_breakpoint (b)
6210 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6211 && bp_condition_evaluator (b) == condition_evaluation_both)
6212 {
6213 uiout->text (" (");
6214 uiout->field_string ("evaluated-by",
6215 bp_location_condition_evaluator (loc));
6216 uiout->text (")");
6217 }
6218 }
6219
6220 static const char *
6221 bptype_string (enum bptype type)
6222 {
6223 struct ep_type_description
6224 {
6225 enum bptype type;
6226 const char *description;
6227 };
6228 static struct ep_type_description bptypes[] =
6229 {
6230 {bp_none, "?deleted?"},
6231 {bp_breakpoint, "breakpoint"},
6232 {bp_hardware_breakpoint, "hw breakpoint"},
6233 {bp_single_step, "sw single-step"},
6234 {bp_until, "until"},
6235 {bp_finish, "finish"},
6236 {bp_watchpoint, "watchpoint"},
6237 {bp_hardware_watchpoint, "hw watchpoint"},
6238 {bp_read_watchpoint, "read watchpoint"},
6239 {bp_access_watchpoint, "acc watchpoint"},
6240 {bp_longjmp, "longjmp"},
6241 {bp_longjmp_resume, "longjmp resume"},
6242 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6243 {bp_exception, "exception"},
6244 {bp_exception_resume, "exception resume"},
6245 {bp_step_resume, "step resume"},
6246 {bp_hp_step_resume, "high-priority step resume"},
6247 {bp_watchpoint_scope, "watchpoint scope"},
6248 {bp_call_dummy, "call dummy"},
6249 {bp_std_terminate, "std::terminate"},
6250 {bp_shlib_event, "shlib events"},
6251 {bp_thread_event, "thread events"},
6252 {bp_overlay_event, "overlay events"},
6253 {bp_longjmp_master, "longjmp master"},
6254 {bp_std_terminate_master, "std::terminate master"},
6255 {bp_exception_master, "exception master"},
6256 {bp_catchpoint, "catchpoint"},
6257 {bp_tracepoint, "tracepoint"},
6258 {bp_fast_tracepoint, "fast tracepoint"},
6259 {bp_static_tracepoint, "static tracepoint"},
6260 {bp_dprintf, "dprintf"},
6261 {bp_jit_event, "jit events"},
6262 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6263 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6264 };
6265
6266 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6267 || ((int) type != bptypes[(int) type].type))
6268 internal_error (__FILE__, __LINE__,
6269 _("bptypes table does not describe type #%d."),
6270 (int) type);
6271
6272 return bptypes[(int) type].description;
6273 }
6274
6275 /* For MI, output a field named 'thread-groups' with a list as the value.
6276 For CLI, prefix the list with the string 'inf'. */
6277
6278 static void
6279 output_thread_groups (struct ui_out *uiout,
6280 const char *field_name,
6281 VEC(int) *inf_num,
6282 int mi_only)
6283 {
6284 int is_mi = uiout->is_mi_like_p ();
6285 int inf;
6286 int i;
6287
6288 /* For backward compatibility, don't display inferiors in CLI unless
6289 there are several. Always display them for MI. */
6290 if (!is_mi && mi_only)
6291 return;
6292
6293 ui_out_emit_list list_emitter (uiout, field_name);
6294
6295 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6296 {
6297 if (is_mi)
6298 {
6299 char mi_group[10];
6300
6301 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6302 uiout->field_string (NULL, mi_group);
6303 }
6304 else
6305 {
6306 if (i == 0)
6307 uiout->text (" inf ");
6308 else
6309 uiout->text (", ");
6310
6311 uiout->text (plongest (inf));
6312 }
6313 }
6314 }
6315
6316 /* Print B to gdb_stdout. */
6317
6318 static void
6319 print_one_breakpoint_location (struct breakpoint *b,
6320 struct bp_location *loc,
6321 int loc_number,
6322 struct bp_location **last_loc,
6323 int allflag)
6324 {
6325 struct command_line *l;
6326 static char bpenables[] = "nynny";
6327
6328 struct ui_out *uiout = current_uiout;
6329 int header_of_multiple = 0;
6330 int part_of_multiple = (loc != NULL);
6331 struct value_print_options opts;
6332
6333 get_user_print_options (&opts);
6334
6335 gdb_assert (!loc || loc_number != 0);
6336 /* See comment in print_one_breakpoint concerning treatment of
6337 breakpoints with single disabled location. */
6338 if (loc == NULL
6339 && (b->loc != NULL
6340 && (b->loc->next != NULL || !b->loc->enabled)))
6341 header_of_multiple = 1;
6342 if (loc == NULL)
6343 loc = b->loc;
6344
6345 annotate_record ();
6346
6347 /* 1 */
6348 annotate_field (0);
6349 if (part_of_multiple)
6350 {
6351 char *formatted;
6352 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6353 uiout->field_string ("number", formatted);
6354 xfree (formatted);
6355 }
6356 else
6357 {
6358 uiout->field_int ("number", b->number);
6359 }
6360
6361 /* 2 */
6362 annotate_field (1);
6363 if (part_of_multiple)
6364 uiout->field_skip ("type");
6365 else
6366 uiout->field_string ("type", bptype_string (b->type));
6367
6368 /* 3 */
6369 annotate_field (2);
6370 if (part_of_multiple)
6371 uiout->field_skip ("disp");
6372 else
6373 uiout->field_string ("disp", bpdisp_text (b->disposition));
6374
6375
6376 /* 4 */
6377 annotate_field (3);
6378 if (part_of_multiple)
6379 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6380 else
6381 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6382 uiout->spaces (2);
6383
6384
6385 /* 5 and 6 */
6386 if (b->ops != NULL && b->ops->print_one != NULL)
6387 {
6388 /* Although the print_one can possibly print all locations,
6389 calling it here is not likely to get any nice result. So,
6390 make sure there's just one location. */
6391 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6392 b->ops->print_one (b, last_loc);
6393 }
6394 else
6395 switch (b->type)
6396 {
6397 case bp_none:
6398 internal_error (__FILE__, __LINE__,
6399 _("print_one_breakpoint: bp_none encountered\n"));
6400 break;
6401
6402 case bp_watchpoint:
6403 case bp_hardware_watchpoint:
6404 case bp_read_watchpoint:
6405 case bp_access_watchpoint:
6406 {
6407 struct watchpoint *w = (struct watchpoint *) b;
6408
6409 /* Field 4, the address, is omitted (which makes the columns
6410 not line up too nicely with the headers, but the effect
6411 is relatively readable). */
6412 if (opts.addressprint)
6413 uiout->field_skip ("addr");
6414 annotate_field (5);
6415 uiout->field_string ("what", w->exp_string);
6416 }
6417 break;
6418
6419 case bp_breakpoint:
6420 case bp_hardware_breakpoint:
6421 case bp_single_step:
6422 case bp_until:
6423 case bp_finish:
6424 case bp_longjmp:
6425 case bp_longjmp_resume:
6426 case bp_longjmp_call_dummy:
6427 case bp_exception:
6428 case bp_exception_resume:
6429 case bp_step_resume:
6430 case bp_hp_step_resume:
6431 case bp_watchpoint_scope:
6432 case bp_call_dummy:
6433 case bp_std_terminate:
6434 case bp_shlib_event:
6435 case bp_thread_event:
6436 case bp_overlay_event:
6437 case bp_longjmp_master:
6438 case bp_std_terminate_master:
6439 case bp_exception_master:
6440 case bp_tracepoint:
6441 case bp_fast_tracepoint:
6442 case bp_static_tracepoint:
6443 case bp_dprintf:
6444 case bp_jit_event:
6445 case bp_gnu_ifunc_resolver:
6446 case bp_gnu_ifunc_resolver_return:
6447 if (opts.addressprint)
6448 {
6449 annotate_field (4);
6450 if (header_of_multiple)
6451 uiout->field_string ("addr", "<MULTIPLE>");
6452 else if (b->loc == NULL || loc->shlib_disabled)
6453 uiout->field_string ("addr", "<PENDING>");
6454 else
6455 uiout->field_core_addr ("addr",
6456 loc->gdbarch, loc->address);
6457 }
6458 annotate_field (5);
6459 if (!header_of_multiple)
6460 print_breakpoint_location (b, loc);
6461 if (b->loc)
6462 *last_loc = b->loc;
6463 break;
6464 }
6465
6466
6467 if (loc != NULL && !header_of_multiple)
6468 {
6469 struct inferior *inf;
6470 VEC(int) *inf_num = NULL;
6471 int mi_only = 1;
6472
6473 ALL_INFERIORS (inf)
6474 {
6475 if (inf->pspace == loc->pspace)
6476 VEC_safe_push (int, inf_num, inf->num);
6477 }
6478
6479 /* For backward compatibility, don't display inferiors in CLI unless
6480 there are several. Always display for MI. */
6481 if (allflag
6482 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6483 && (number_of_program_spaces () > 1
6484 || number_of_inferiors () > 1)
6485 /* LOC is for existing B, it cannot be in
6486 moribund_locations and thus having NULL OWNER. */
6487 && loc->owner->type != bp_catchpoint))
6488 mi_only = 0;
6489 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6490 VEC_free (int, inf_num);
6491 }
6492
6493 if (!part_of_multiple)
6494 {
6495 if (b->thread != -1)
6496 {
6497 /* FIXME: This seems to be redundant and lost here; see the
6498 "stop only in" line a little further down. */
6499 uiout->text (" thread ");
6500 uiout->field_int ("thread", b->thread);
6501 }
6502 else if (b->task != 0)
6503 {
6504 uiout->text (" task ");
6505 uiout->field_int ("task", b->task);
6506 }
6507 }
6508
6509 uiout->text ("\n");
6510
6511 if (!part_of_multiple)
6512 b->ops->print_one_detail (b, uiout);
6513
6514 if (part_of_multiple && frame_id_p (b->frame_id))
6515 {
6516 annotate_field (6);
6517 uiout->text ("\tstop only in stack frame at ");
6518 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6519 the frame ID. */
6520 uiout->field_core_addr ("frame",
6521 b->gdbarch, b->frame_id.stack_addr);
6522 uiout->text ("\n");
6523 }
6524
6525 if (!part_of_multiple && b->cond_string)
6526 {
6527 annotate_field (7);
6528 if (is_tracepoint (b))
6529 uiout->text ("\ttrace only if ");
6530 else
6531 uiout->text ("\tstop only if ");
6532 uiout->field_string ("cond", b->cond_string);
6533
6534 /* Print whether the target is doing the breakpoint's condition
6535 evaluation. If GDB is doing the evaluation, don't print anything. */
6536 if (is_breakpoint (b)
6537 && breakpoint_condition_evaluation_mode ()
6538 == condition_evaluation_target)
6539 {
6540 uiout->text (" (");
6541 uiout->field_string ("evaluated-by",
6542 bp_condition_evaluator (b));
6543 uiout->text (" evals)");
6544 }
6545 uiout->text ("\n");
6546 }
6547
6548 if (!part_of_multiple && b->thread != -1)
6549 {
6550 /* FIXME should make an annotation for this. */
6551 uiout->text ("\tstop only in thread ");
6552 if (uiout->is_mi_like_p ())
6553 uiout->field_int ("thread", b->thread);
6554 else
6555 {
6556 struct thread_info *thr = find_thread_global_id (b->thread);
6557
6558 uiout->field_string ("thread", print_thread_id (thr));
6559 }
6560 uiout->text ("\n");
6561 }
6562
6563 if (!part_of_multiple)
6564 {
6565 if (b->hit_count)
6566 {
6567 /* FIXME should make an annotation for this. */
6568 if (is_catchpoint (b))
6569 uiout->text ("\tcatchpoint");
6570 else if (is_tracepoint (b))
6571 uiout->text ("\ttracepoint");
6572 else
6573 uiout->text ("\tbreakpoint");
6574 uiout->text (" already hit ");
6575 uiout->field_int ("times", b->hit_count);
6576 if (b->hit_count == 1)
6577 uiout->text (" time\n");
6578 else
6579 uiout->text (" times\n");
6580 }
6581 else
6582 {
6583 /* Output the count also if it is zero, but only if this is mi. */
6584 if (uiout->is_mi_like_p ())
6585 uiout->field_int ("times", b->hit_count);
6586 }
6587 }
6588
6589 if (!part_of_multiple && b->ignore_count)
6590 {
6591 annotate_field (8);
6592 uiout->text ("\tignore next ");
6593 uiout->field_int ("ignore", b->ignore_count);
6594 uiout->text (" hits\n");
6595 }
6596
6597 /* Note that an enable count of 1 corresponds to "enable once"
6598 behavior, which is reported by the combination of enablement and
6599 disposition, so we don't need to mention it here. */
6600 if (!part_of_multiple && b->enable_count > 1)
6601 {
6602 annotate_field (8);
6603 uiout->text ("\tdisable after ");
6604 /* Tweak the wording to clarify that ignore and enable counts
6605 are distinct, and have additive effect. */
6606 if (b->ignore_count)
6607 uiout->text ("additional ");
6608 else
6609 uiout->text ("next ");
6610 uiout->field_int ("enable", b->enable_count);
6611 uiout->text (" hits\n");
6612 }
6613
6614 if (!part_of_multiple && is_tracepoint (b))
6615 {
6616 struct tracepoint *tp = (struct tracepoint *) b;
6617
6618 if (tp->traceframe_usage)
6619 {
6620 uiout->text ("\ttrace buffer usage ");
6621 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6622 uiout->text (" bytes\n");
6623 }
6624 }
6625
6626 l = b->commands ? b->commands->commands : NULL;
6627 if (!part_of_multiple && l)
6628 {
6629 annotate_field (9);
6630 ui_out_emit_tuple tuple_emitter (uiout, "script");
6631 print_command_lines (uiout, l, 4);
6632 }
6633
6634 if (is_tracepoint (b))
6635 {
6636 struct tracepoint *t = (struct tracepoint *) b;
6637
6638 if (!part_of_multiple && t->pass_count)
6639 {
6640 annotate_field (10);
6641 uiout->text ("\tpass count ");
6642 uiout->field_int ("pass", t->pass_count);
6643 uiout->text (" \n");
6644 }
6645
6646 /* Don't display it when tracepoint or tracepoint location is
6647 pending. */
6648 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6649 {
6650 annotate_field (11);
6651
6652 if (uiout->is_mi_like_p ())
6653 uiout->field_string ("installed",
6654 loc->inserted ? "y" : "n");
6655 else
6656 {
6657 if (loc->inserted)
6658 uiout->text ("\t");
6659 else
6660 uiout->text ("\tnot ");
6661 uiout->text ("installed on target\n");
6662 }
6663 }
6664 }
6665
6666 if (uiout->is_mi_like_p () && !part_of_multiple)
6667 {
6668 if (is_watchpoint (b))
6669 {
6670 struct watchpoint *w = (struct watchpoint *) b;
6671
6672 uiout->field_string ("original-location", w->exp_string);
6673 }
6674 else if (b->location != NULL
6675 && event_location_to_string (b->location.get ()) != NULL)
6676 uiout->field_string ("original-location",
6677 event_location_to_string (b->location.get ()));
6678 }
6679 }
6680
6681 static void
6682 print_one_breakpoint (struct breakpoint *b,
6683 struct bp_location **last_loc,
6684 int allflag)
6685 {
6686 struct ui_out *uiout = current_uiout;
6687
6688 {
6689 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6690
6691 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6692 }
6693
6694 /* If this breakpoint has custom print function,
6695 it's already printed. Otherwise, print individual
6696 locations, if any. */
6697 if (b->ops == NULL || b->ops->print_one == NULL)
6698 {
6699 /* If breakpoint has a single location that is disabled, we
6700 print it as if it had several locations, since otherwise it's
6701 hard to represent "breakpoint enabled, location disabled"
6702 situation.
6703
6704 Note that while hardware watchpoints have several locations
6705 internally, that's not a property exposed to user. */
6706 if (b->loc
6707 && !is_hardware_watchpoint (b)
6708 && (b->loc->next || !b->loc->enabled))
6709 {
6710 struct bp_location *loc;
6711 int n = 1;
6712
6713 for (loc = b->loc; loc; loc = loc->next, ++n)
6714 {
6715 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6716 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6717 }
6718 }
6719 }
6720 }
6721
6722 static int
6723 breakpoint_address_bits (struct breakpoint *b)
6724 {
6725 int print_address_bits = 0;
6726 struct bp_location *loc;
6727
6728 /* Software watchpoints that aren't watching memory don't have an
6729 address to print. */
6730 if (is_no_memory_software_watchpoint (b))
6731 return 0;
6732
6733 for (loc = b->loc; loc; loc = loc->next)
6734 {
6735 int addr_bit;
6736
6737 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6738 if (addr_bit > print_address_bits)
6739 print_address_bits = addr_bit;
6740 }
6741
6742 return print_address_bits;
6743 }
6744
6745 struct captured_breakpoint_query_args
6746 {
6747 int bnum;
6748 };
6749
6750 static int
6751 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6752 {
6753 struct captured_breakpoint_query_args *args
6754 = (struct captured_breakpoint_query_args *) data;
6755 struct breakpoint *b;
6756 struct bp_location *dummy_loc = NULL;
6757
6758 ALL_BREAKPOINTS (b)
6759 {
6760 if (args->bnum == b->number)
6761 {
6762 print_one_breakpoint (b, &dummy_loc, 0);
6763 return GDB_RC_OK;
6764 }
6765 }
6766 return GDB_RC_NONE;
6767 }
6768
6769 enum gdb_rc
6770 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6771 char **error_message)
6772 {
6773 struct captured_breakpoint_query_args args;
6774
6775 args.bnum = bnum;
6776 /* For the moment we don't trust print_one_breakpoint() to not throw
6777 an error. */
6778 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6779 error_message, RETURN_MASK_ALL) < 0)
6780 return GDB_RC_FAIL;
6781 else
6782 return GDB_RC_OK;
6783 }
6784
6785 /* Return true if this breakpoint was set by the user, false if it is
6786 internal or momentary. */
6787
6788 int
6789 user_breakpoint_p (struct breakpoint *b)
6790 {
6791 return b->number > 0;
6792 }
6793
6794 /* See breakpoint.h. */
6795
6796 int
6797 pending_breakpoint_p (struct breakpoint *b)
6798 {
6799 return b->loc == NULL;
6800 }
6801
6802 /* Print information on user settable breakpoint (watchpoint, etc)
6803 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6804 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6805 FILTER is non-NULL, call it on each breakpoint and only include the
6806 ones for which it returns non-zero. Return the total number of
6807 breakpoints listed. */
6808
6809 static int
6810 breakpoint_1 (char *args, int allflag,
6811 int (*filter) (const struct breakpoint *))
6812 {
6813 struct breakpoint *b;
6814 struct bp_location *last_loc = NULL;
6815 int nr_printable_breakpoints;
6816 struct value_print_options opts;
6817 int print_address_bits = 0;
6818 int print_type_col_width = 14;
6819 struct ui_out *uiout = current_uiout;
6820
6821 get_user_print_options (&opts);
6822
6823 /* Compute the number of rows in the table, as well as the size
6824 required for address fields. */
6825 nr_printable_breakpoints = 0;
6826 ALL_BREAKPOINTS (b)
6827 {
6828 /* If we have a filter, only list the breakpoints it accepts. */
6829 if (filter && !filter (b))
6830 continue;
6831
6832 /* If we have an "args" string, it is a list of breakpoints to
6833 accept. Skip the others. */
6834 if (args != NULL && *args != '\0')
6835 {
6836 if (allflag && parse_and_eval_long (args) != b->number)
6837 continue;
6838 if (!allflag && !number_is_in_list (args, b->number))
6839 continue;
6840 }
6841
6842 if (allflag || user_breakpoint_p (b))
6843 {
6844 int addr_bit, type_len;
6845
6846 addr_bit = breakpoint_address_bits (b);
6847 if (addr_bit > print_address_bits)
6848 print_address_bits = addr_bit;
6849
6850 type_len = strlen (bptype_string (b->type));
6851 if (type_len > print_type_col_width)
6852 print_type_col_width = type_len;
6853
6854 nr_printable_breakpoints++;
6855 }
6856 }
6857
6858 {
6859 ui_out_emit_table table_emitter (uiout,
6860 opts.addressprint ? 6 : 5,
6861 nr_printable_breakpoints,
6862 "BreakpointTable");
6863
6864 if (nr_printable_breakpoints > 0)
6865 annotate_breakpoints_headers ();
6866 if (nr_printable_breakpoints > 0)
6867 annotate_field (0);
6868 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (1);
6871 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6872 if (nr_printable_breakpoints > 0)
6873 annotate_field (2);
6874 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (3);
6877 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6878 if (opts.addressprint)
6879 {
6880 if (nr_printable_breakpoints > 0)
6881 annotate_field (4);
6882 if (print_address_bits <= 32)
6883 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6884 else
6885 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6886 }
6887 if (nr_printable_breakpoints > 0)
6888 annotate_field (5);
6889 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6890 uiout->table_body ();
6891 if (nr_printable_breakpoints > 0)
6892 annotate_breakpoints_table ();
6893
6894 ALL_BREAKPOINTS (b)
6895 {
6896 QUIT;
6897 /* If we have a filter, only list the breakpoints it accepts. */
6898 if (filter && !filter (b))
6899 continue;
6900
6901 /* If we have an "args" string, it is a list of breakpoints to
6902 accept. Skip the others. */
6903
6904 if (args != NULL && *args != '\0')
6905 {
6906 if (allflag) /* maintenance info breakpoint */
6907 {
6908 if (parse_and_eval_long (args) != b->number)
6909 continue;
6910 }
6911 else /* all others */
6912 {
6913 if (!number_is_in_list (args, b->number))
6914 continue;
6915 }
6916 }
6917 /* We only print out user settable breakpoints unless the
6918 allflag is set. */
6919 if (allflag || user_breakpoint_p (b))
6920 print_one_breakpoint (b, &last_loc, allflag);
6921 }
6922 }
6923
6924 if (nr_printable_breakpoints == 0)
6925 {
6926 /* If there's a filter, let the caller decide how to report
6927 empty list. */
6928 if (!filter)
6929 {
6930 if (args == NULL || *args == '\0')
6931 uiout->message ("No breakpoints or watchpoints.\n");
6932 else
6933 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6934 args);
6935 }
6936 }
6937 else
6938 {
6939 if (last_loc && !server_command)
6940 set_next_address (last_loc->gdbarch, last_loc->address);
6941 }
6942
6943 /* FIXME? Should this be moved up so that it is only called when
6944 there have been breakpoints? */
6945 annotate_breakpoints_table_end ();
6946
6947 return nr_printable_breakpoints;
6948 }
6949
6950 /* Display the value of default-collect in a way that is generally
6951 compatible with the breakpoint list. */
6952
6953 static void
6954 default_collect_info (void)
6955 {
6956 struct ui_out *uiout = current_uiout;
6957
6958 /* If it has no value (which is frequently the case), say nothing; a
6959 message like "No default-collect." gets in user's face when it's
6960 not wanted. */
6961 if (!*default_collect)
6962 return;
6963
6964 /* The following phrase lines up nicely with per-tracepoint collect
6965 actions. */
6966 uiout->text ("default collect ");
6967 uiout->field_string ("default-collect", default_collect);
6968 uiout->text (" \n");
6969 }
6970
6971 static void
6972 info_breakpoints_command (char *args, int from_tty)
6973 {
6974 breakpoint_1 (args, 0, NULL);
6975
6976 default_collect_info ();
6977 }
6978
6979 static void
6980 info_watchpoints_command (char *args, int from_tty)
6981 {
6982 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6983 struct ui_out *uiout = current_uiout;
6984
6985 if (num_printed == 0)
6986 {
6987 if (args == NULL || *args == '\0')
6988 uiout->message ("No watchpoints.\n");
6989 else
6990 uiout->message ("No watchpoint matching '%s'.\n", args);
6991 }
6992 }
6993
6994 static void
6995 maintenance_info_breakpoints (char *args, int from_tty)
6996 {
6997 breakpoint_1 (args, 1, NULL);
6998
6999 default_collect_info ();
7000 }
7001
7002 static int
7003 breakpoint_has_pc (struct breakpoint *b,
7004 struct program_space *pspace,
7005 CORE_ADDR pc, struct obj_section *section)
7006 {
7007 struct bp_location *bl = b->loc;
7008
7009 for (; bl; bl = bl->next)
7010 {
7011 if (bl->pspace == pspace
7012 && bl->address == pc
7013 && (!overlay_debugging || bl->section == section))
7014 return 1;
7015 }
7016 return 0;
7017 }
7018
7019 /* Print a message describing any user-breakpoints set at PC. This
7020 concerns with logical breakpoints, so we match program spaces, not
7021 address spaces. */
7022
7023 static void
7024 describe_other_breakpoints (struct gdbarch *gdbarch,
7025 struct program_space *pspace, CORE_ADDR pc,
7026 struct obj_section *section, int thread)
7027 {
7028 int others = 0;
7029 struct breakpoint *b;
7030
7031 ALL_BREAKPOINTS (b)
7032 others += (user_breakpoint_p (b)
7033 && breakpoint_has_pc (b, pspace, pc, section));
7034 if (others > 0)
7035 {
7036 if (others == 1)
7037 printf_filtered (_("Note: breakpoint "));
7038 else /* if (others == ???) */
7039 printf_filtered (_("Note: breakpoints "));
7040 ALL_BREAKPOINTS (b)
7041 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7042 {
7043 others--;
7044 printf_filtered ("%d", b->number);
7045 if (b->thread == -1 && thread != -1)
7046 printf_filtered (" (all threads)");
7047 else if (b->thread != -1)
7048 printf_filtered (" (thread %d)", b->thread);
7049 printf_filtered ("%s%s ",
7050 ((b->enable_state == bp_disabled
7051 || b->enable_state == bp_call_disabled)
7052 ? " (disabled)"
7053 : ""),
7054 (others > 1) ? ","
7055 : ((others == 1) ? " and" : ""));
7056 }
7057 printf_filtered (_("also set at pc "));
7058 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7059 printf_filtered (".\n");
7060 }
7061 }
7062 \f
7063
7064 /* Return true iff it is meaningful to use the address member of
7065 BPT locations. For some breakpoint types, the locations' address members
7066 are irrelevant and it makes no sense to attempt to compare them to other
7067 addresses (or use them for any other purpose either).
7068
7069 More specifically, each of the following breakpoint types will
7070 always have a zero valued location address and we don't want to mark
7071 breakpoints of any of these types to be a duplicate of an actual
7072 breakpoint location at address zero:
7073
7074 bp_watchpoint
7075 bp_catchpoint
7076
7077 */
7078
7079 static int
7080 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7081 {
7082 enum bptype type = bpt->type;
7083
7084 return (type != bp_watchpoint && type != bp_catchpoint);
7085 }
7086
7087 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7088 true if LOC1 and LOC2 represent the same watchpoint location. */
7089
7090 static int
7091 watchpoint_locations_match (struct bp_location *loc1,
7092 struct bp_location *loc2)
7093 {
7094 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7095 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7096
7097 /* Both of them must exist. */
7098 gdb_assert (w1 != NULL);
7099 gdb_assert (w2 != NULL);
7100
7101 /* If the target can evaluate the condition expression in hardware,
7102 then we we need to insert both watchpoints even if they are at
7103 the same place. Otherwise the watchpoint will only trigger when
7104 the condition of whichever watchpoint was inserted evaluates to
7105 true, not giving a chance for GDB to check the condition of the
7106 other watchpoint. */
7107 if ((w1->cond_exp
7108 && target_can_accel_watchpoint_condition (loc1->address,
7109 loc1->length,
7110 loc1->watchpoint_type,
7111 w1->cond_exp.get ()))
7112 || (w2->cond_exp
7113 && target_can_accel_watchpoint_condition (loc2->address,
7114 loc2->length,
7115 loc2->watchpoint_type,
7116 w2->cond_exp.get ())))
7117 return 0;
7118
7119 /* Note that this checks the owner's type, not the location's. In
7120 case the target does not support read watchpoints, but does
7121 support access watchpoints, we'll have bp_read_watchpoint
7122 watchpoints with hw_access locations. Those should be considered
7123 duplicates of hw_read locations. The hw_read locations will
7124 become hw_access locations later. */
7125 return (loc1->owner->type == loc2->owner->type
7126 && loc1->pspace->aspace == loc2->pspace->aspace
7127 && loc1->address == loc2->address
7128 && loc1->length == loc2->length);
7129 }
7130
7131 /* See breakpoint.h. */
7132
7133 int
7134 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7135 struct address_space *aspace2, CORE_ADDR addr2)
7136 {
7137 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7138 || aspace1 == aspace2)
7139 && addr1 == addr2);
7140 }
7141
7142 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7143 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7144 matches ASPACE2. On targets that have global breakpoints, the address
7145 space doesn't really matter. */
7146
7147 static int
7148 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7149 int len1, struct address_space *aspace2,
7150 CORE_ADDR addr2)
7151 {
7152 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7153 || aspace1 == aspace2)
7154 && addr2 >= addr1 && addr2 < addr1 + len1);
7155 }
7156
7157 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7158 a ranged breakpoint. In most targets, a match happens only if ASPACE
7159 matches the breakpoint's address space. On targets that have global
7160 breakpoints, the address space doesn't really matter. */
7161
7162 static int
7163 breakpoint_location_address_match (struct bp_location *bl,
7164 struct address_space *aspace,
7165 CORE_ADDR addr)
7166 {
7167 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7168 aspace, addr)
7169 || (bl->length
7170 && breakpoint_address_match_range (bl->pspace->aspace,
7171 bl->address, bl->length,
7172 aspace, addr)));
7173 }
7174
7175 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7176 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7177 match happens only if ASPACE matches the breakpoint's address
7178 space. On targets that have global breakpoints, the address space
7179 doesn't really matter. */
7180
7181 static int
7182 breakpoint_location_address_range_overlap (struct bp_location *bl,
7183 struct address_space *aspace,
7184 CORE_ADDR addr, int len)
7185 {
7186 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7187 || bl->pspace->aspace == aspace)
7188 {
7189 int bl_len = bl->length != 0 ? bl->length : 1;
7190
7191 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7192 return 1;
7193 }
7194 return 0;
7195 }
7196
7197 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7198 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7199 true, otherwise returns false. */
7200
7201 static int
7202 tracepoint_locations_match (struct bp_location *loc1,
7203 struct bp_location *loc2)
7204 {
7205 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7206 /* Since tracepoint locations are never duplicated with others', tracepoint
7207 locations at the same address of different tracepoints are regarded as
7208 different locations. */
7209 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7210 else
7211 return 0;
7212 }
7213
7214 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7215 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7216 represent the same location. */
7217
7218 static int
7219 breakpoint_locations_match (struct bp_location *loc1,
7220 struct bp_location *loc2)
7221 {
7222 int hw_point1, hw_point2;
7223
7224 /* Both of them must not be in moribund_locations. */
7225 gdb_assert (loc1->owner != NULL);
7226 gdb_assert (loc2->owner != NULL);
7227
7228 hw_point1 = is_hardware_watchpoint (loc1->owner);
7229 hw_point2 = is_hardware_watchpoint (loc2->owner);
7230
7231 if (hw_point1 != hw_point2)
7232 return 0;
7233 else if (hw_point1)
7234 return watchpoint_locations_match (loc1, loc2);
7235 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7236 return tracepoint_locations_match (loc1, loc2);
7237 else
7238 /* We compare bp_location.length in order to cover ranged breakpoints. */
7239 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7240 loc2->pspace->aspace, loc2->address)
7241 && loc1->length == loc2->length);
7242 }
7243
7244 static void
7245 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7246 int bnum, int have_bnum)
7247 {
7248 /* The longest string possibly returned by hex_string_custom
7249 is 50 chars. These must be at least that big for safety. */
7250 char astr1[64];
7251 char astr2[64];
7252
7253 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7254 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7255 if (have_bnum)
7256 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7257 bnum, astr1, astr2);
7258 else
7259 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7260 }
7261
7262 /* Adjust a breakpoint's address to account for architectural
7263 constraints on breakpoint placement. Return the adjusted address.
7264 Note: Very few targets require this kind of adjustment. For most
7265 targets, this function is simply the identity function. */
7266
7267 static CORE_ADDR
7268 adjust_breakpoint_address (struct gdbarch *gdbarch,
7269 CORE_ADDR bpaddr, enum bptype bptype)
7270 {
7271 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7272 {
7273 /* Very few targets need any kind of breakpoint adjustment. */
7274 return bpaddr;
7275 }
7276 else if (bptype == bp_watchpoint
7277 || bptype == bp_hardware_watchpoint
7278 || bptype == bp_read_watchpoint
7279 || bptype == bp_access_watchpoint
7280 || bptype == bp_catchpoint)
7281 {
7282 /* Watchpoints and the various bp_catch_* eventpoints should not
7283 have their addresses modified. */
7284 return bpaddr;
7285 }
7286 else if (bptype == bp_single_step)
7287 {
7288 /* Single-step breakpoints should not have their addresses
7289 modified. If there's any architectural constrain that
7290 applies to this address, then it should have already been
7291 taken into account when the breakpoint was created in the
7292 first place. If we didn't do this, stepping through e.g.,
7293 Thumb-2 IT blocks would break. */
7294 return bpaddr;
7295 }
7296 else
7297 {
7298 CORE_ADDR adjusted_bpaddr;
7299
7300 /* Some targets have architectural constraints on the placement
7301 of breakpoint instructions. Obtain the adjusted address. */
7302 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7303
7304 /* An adjusted breakpoint address can significantly alter
7305 a user's expectations. Print a warning if an adjustment
7306 is required. */
7307 if (adjusted_bpaddr != bpaddr)
7308 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7309
7310 return adjusted_bpaddr;
7311 }
7312 }
7313
7314 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7315 {
7316 bp_location *loc = this;
7317
7318 gdb_assert (ops != NULL);
7319
7320 loc->ops = ops;
7321 loc->owner = owner;
7322 loc->cond_bytecode = NULL;
7323 loc->shlib_disabled = 0;
7324 loc->enabled = 1;
7325
7326 switch (owner->type)
7327 {
7328 case bp_breakpoint:
7329 case bp_single_step:
7330 case bp_until:
7331 case bp_finish:
7332 case bp_longjmp:
7333 case bp_longjmp_resume:
7334 case bp_longjmp_call_dummy:
7335 case bp_exception:
7336 case bp_exception_resume:
7337 case bp_step_resume:
7338 case bp_hp_step_resume:
7339 case bp_watchpoint_scope:
7340 case bp_call_dummy:
7341 case bp_std_terminate:
7342 case bp_shlib_event:
7343 case bp_thread_event:
7344 case bp_overlay_event:
7345 case bp_jit_event:
7346 case bp_longjmp_master:
7347 case bp_std_terminate_master:
7348 case bp_exception_master:
7349 case bp_gnu_ifunc_resolver:
7350 case bp_gnu_ifunc_resolver_return:
7351 case bp_dprintf:
7352 loc->loc_type = bp_loc_software_breakpoint;
7353 mark_breakpoint_location_modified (loc);
7354 break;
7355 case bp_hardware_breakpoint:
7356 loc->loc_type = bp_loc_hardware_breakpoint;
7357 mark_breakpoint_location_modified (loc);
7358 break;
7359 case bp_hardware_watchpoint:
7360 case bp_read_watchpoint:
7361 case bp_access_watchpoint:
7362 loc->loc_type = bp_loc_hardware_watchpoint;
7363 break;
7364 case bp_watchpoint:
7365 case bp_catchpoint:
7366 case bp_tracepoint:
7367 case bp_fast_tracepoint:
7368 case bp_static_tracepoint:
7369 loc->loc_type = bp_loc_other;
7370 break;
7371 default:
7372 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7373 }
7374
7375 loc->refc = 1;
7376 }
7377
7378 /* Allocate a struct bp_location. */
7379
7380 static struct bp_location *
7381 allocate_bp_location (struct breakpoint *bpt)
7382 {
7383 return bpt->ops->allocate_location (bpt);
7384 }
7385
7386 static void
7387 free_bp_location (struct bp_location *loc)
7388 {
7389 loc->ops->dtor (loc);
7390 delete loc;
7391 }
7392
7393 /* Increment reference count. */
7394
7395 static void
7396 incref_bp_location (struct bp_location *bl)
7397 {
7398 ++bl->refc;
7399 }
7400
7401 /* Decrement reference count. If the reference count reaches 0,
7402 destroy the bp_location. Sets *BLP to NULL. */
7403
7404 static void
7405 decref_bp_location (struct bp_location **blp)
7406 {
7407 gdb_assert ((*blp)->refc > 0);
7408
7409 if (--(*blp)->refc == 0)
7410 free_bp_location (*blp);
7411 *blp = NULL;
7412 }
7413
7414 /* Add breakpoint B at the end of the global breakpoint chain. */
7415
7416 static breakpoint *
7417 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7418 {
7419 struct breakpoint *b1;
7420 struct breakpoint *result = b.get ();
7421
7422 /* Add this breakpoint to the end of the chain so that a list of
7423 breakpoints will come out in order of increasing numbers. */
7424
7425 b1 = breakpoint_chain;
7426 if (b1 == 0)
7427 breakpoint_chain = b.release ();
7428 else
7429 {
7430 while (b1->next)
7431 b1 = b1->next;
7432 b1->next = b.release ();
7433 }
7434
7435 return result;
7436 }
7437
7438 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7439
7440 static void
7441 init_raw_breakpoint_without_location (struct breakpoint *b,
7442 struct gdbarch *gdbarch,
7443 enum bptype bptype,
7444 const struct breakpoint_ops *ops)
7445 {
7446 gdb_assert (ops != NULL);
7447
7448 b->ops = ops;
7449 b->type = bptype;
7450 b->gdbarch = gdbarch;
7451 b->language = current_language->la_language;
7452 b->input_radix = input_radix;
7453 b->related_breakpoint = b;
7454 }
7455
7456 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7457 that has type BPTYPE and has no locations as yet. */
7458
7459 static struct breakpoint *
7460 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7461 enum bptype bptype,
7462 const struct breakpoint_ops *ops)
7463 {
7464 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7465
7466 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7467 return add_to_breakpoint_chain (std::move (b));
7468 }
7469
7470 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7471 resolutions should be made as the user specified the location explicitly
7472 enough. */
7473
7474 static void
7475 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7476 {
7477 gdb_assert (loc->owner != NULL);
7478
7479 if (loc->owner->type == bp_breakpoint
7480 || loc->owner->type == bp_hardware_breakpoint
7481 || is_tracepoint (loc->owner))
7482 {
7483 int is_gnu_ifunc;
7484 const char *function_name;
7485 CORE_ADDR func_addr;
7486
7487 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7488 &func_addr, NULL, &is_gnu_ifunc);
7489
7490 if (is_gnu_ifunc && !explicit_loc)
7491 {
7492 struct breakpoint *b = loc->owner;
7493
7494 gdb_assert (loc->pspace == current_program_space);
7495 if (gnu_ifunc_resolve_name (function_name,
7496 &loc->requested_address))
7497 {
7498 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7499 loc->address = adjust_breakpoint_address (loc->gdbarch,
7500 loc->requested_address,
7501 b->type);
7502 }
7503 else if (b->type == bp_breakpoint && b->loc == loc
7504 && loc->next == NULL && b->related_breakpoint == b)
7505 {
7506 /* Create only the whole new breakpoint of this type but do not
7507 mess more complicated breakpoints with multiple locations. */
7508 b->type = bp_gnu_ifunc_resolver;
7509 /* Remember the resolver's address for use by the return
7510 breakpoint. */
7511 loc->related_address = func_addr;
7512 }
7513 }
7514
7515 if (function_name)
7516 loc->function_name = xstrdup (function_name);
7517 }
7518 }
7519
7520 /* Attempt to determine architecture of location identified by SAL. */
7521 struct gdbarch *
7522 get_sal_arch (struct symtab_and_line sal)
7523 {
7524 if (sal.section)
7525 return get_objfile_arch (sal.section->objfile);
7526 if (sal.symtab)
7527 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7528
7529 return NULL;
7530 }
7531
7532 /* Low level routine for partially initializing a breakpoint of type
7533 BPTYPE. The newly created breakpoint's address, section, source
7534 file name, and line number are provided by SAL.
7535
7536 It is expected that the caller will complete the initialization of
7537 the newly created breakpoint struct as well as output any status
7538 information regarding the creation of a new breakpoint. */
7539
7540 static void
7541 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7542 struct symtab_and_line sal, enum bptype bptype,
7543 const struct breakpoint_ops *ops)
7544 {
7545 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7546
7547 add_location_to_breakpoint (b, &sal);
7548
7549 if (bptype != bp_catchpoint)
7550 gdb_assert (sal.pspace != NULL);
7551
7552 /* Store the program space that was used to set the breakpoint,
7553 except for ordinary breakpoints, which are independent of the
7554 program space. */
7555 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7556 b->pspace = sal.pspace;
7557 }
7558
7559 /* set_raw_breakpoint is a low level routine for allocating and
7560 partially initializing a breakpoint of type BPTYPE. The newly
7561 created breakpoint's address, section, source file name, and line
7562 number are provided by SAL. The newly created and partially
7563 initialized breakpoint is added to the breakpoint chain and
7564 is also returned as the value of this function.
7565
7566 It is expected that the caller will complete the initialization of
7567 the newly created breakpoint struct as well as output any status
7568 information regarding the creation of a new breakpoint. In
7569 particular, set_raw_breakpoint does NOT set the breakpoint
7570 number! Care should be taken to not allow an error to occur
7571 prior to completing the initialization of the breakpoint. If this
7572 should happen, a bogus breakpoint will be left on the chain. */
7573
7574 struct breakpoint *
7575 set_raw_breakpoint (struct gdbarch *gdbarch,
7576 struct symtab_and_line sal, enum bptype bptype,
7577 const struct breakpoint_ops *ops)
7578 {
7579 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7580
7581 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7582 return add_to_breakpoint_chain (std::move (b));
7583 }
7584
7585 /* Call this routine when stepping and nexting to enable a breakpoint
7586 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7587 initiated the operation. */
7588
7589 void
7590 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7591 {
7592 struct breakpoint *b, *b_tmp;
7593 int thread = tp->global_num;
7594
7595 /* To avoid having to rescan all objfile symbols at every step,
7596 we maintain a list of continually-inserted but always disabled
7597 longjmp "master" breakpoints. Here, we simply create momentary
7598 clones of those and enable them for the requested thread. */
7599 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7600 if (b->pspace == current_program_space
7601 && (b->type == bp_longjmp_master
7602 || b->type == bp_exception_master))
7603 {
7604 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7605 struct breakpoint *clone;
7606
7607 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7608 after their removal. */
7609 clone = momentary_breakpoint_from_master (b, type,
7610 &momentary_breakpoint_ops, 1);
7611 clone->thread = thread;
7612 }
7613
7614 tp->initiating_frame = frame;
7615 }
7616
7617 /* Delete all longjmp breakpoints from THREAD. */
7618 void
7619 delete_longjmp_breakpoint (int thread)
7620 {
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_longjmp || b->type == bp_exception)
7625 {
7626 if (b->thread == thread)
7627 delete_breakpoint (b);
7628 }
7629 }
7630
7631 void
7632 delete_longjmp_breakpoint_at_next_stop (int thread)
7633 {
7634 struct breakpoint *b, *b_tmp;
7635
7636 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7637 if (b->type == bp_longjmp || b->type == bp_exception)
7638 {
7639 if (b->thread == thread)
7640 b->disposition = disp_del_at_next_stop;
7641 }
7642 }
7643
7644 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7645 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7646 pointer to any of them. Return NULL if this system cannot place longjmp
7647 breakpoints. */
7648
7649 struct breakpoint *
7650 set_longjmp_breakpoint_for_call_dummy (void)
7651 {
7652 struct breakpoint *b, *retval = NULL;
7653
7654 ALL_BREAKPOINTS (b)
7655 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7656 {
7657 struct breakpoint *new_b;
7658
7659 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7660 &momentary_breakpoint_ops,
7661 1);
7662 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7663
7664 /* Link NEW_B into the chain of RETVAL breakpoints. */
7665
7666 gdb_assert (new_b->related_breakpoint == new_b);
7667 if (retval == NULL)
7668 retval = new_b;
7669 new_b->related_breakpoint = retval;
7670 while (retval->related_breakpoint != new_b->related_breakpoint)
7671 retval = retval->related_breakpoint;
7672 retval->related_breakpoint = new_b;
7673 }
7674
7675 return retval;
7676 }
7677
7678 /* Verify all existing dummy frames and their associated breakpoints for
7679 TP. Remove those which can no longer be found in the current frame
7680 stack.
7681
7682 You should call this function only at places where it is safe to currently
7683 unwind the whole stack. Failed stack unwind would discard live dummy
7684 frames. */
7685
7686 void
7687 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7688 {
7689 struct breakpoint *b, *b_tmp;
7690
7691 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7692 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7693 {
7694 struct breakpoint *dummy_b = b->related_breakpoint;
7695
7696 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7697 dummy_b = dummy_b->related_breakpoint;
7698 if (dummy_b->type != bp_call_dummy
7699 || frame_find_by_id (dummy_b->frame_id) != NULL)
7700 continue;
7701
7702 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7703
7704 while (b->related_breakpoint != b)
7705 {
7706 if (b_tmp == b->related_breakpoint)
7707 b_tmp = b->related_breakpoint->next;
7708 delete_breakpoint (b->related_breakpoint);
7709 }
7710 delete_breakpoint (b);
7711 }
7712 }
7713
7714 void
7715 enable_overlay_breakpoints (void)
7716 {
7717 struct breakpoint *b;
7718
7719 ALL_BREAKPOINTS (b)
7720 if (b->type == bp_overlay_event)
7721 {
7722 b->enable_state = bp_enabled;
7723 update_global_location_list (UGLL_MAY_INSERT);
7724 overlay_events_enabled = 1;
7725 }
7726 }
7727
7728 void
7729 disable_overlay_breakpoints (void)
7730 {
7731 struct breakpoint *b;
7732
7733 ALL_BREAKPOINTS (b)
7734 if (b->type == bp_overlay_event)
7735 {
7736 b->enable_state = bp_disabled;
7737 update_global_location_list (UGLL_DONT_INSERT);
7738 overlay_events_enabled = 0;
7739 }
7740 }
7741
7742 /* Set an active std::terminate breakpoint for each std::terminate
7743 master breakpoint. */
7744 void
7745 set_std_terminate_breakpoint (void)
7746 {
7747 struct breakpoint *b, *b_tmp;
7748
7749 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7750 if (b->pspace == current_program_space
7751 && b->type == bp_std_terminate_master)
7752 {
7753 momentary_breakpoint_from_master (b, bp_std_terminate,
7754 &momentary_breakpoint_ops, 1);
7755 }
7756 }
7757
7758 /* Delete all the std::terminate breakpoints. */
7759 void
7760 delete_std_terminate_breakpoint (void)
7761 {
7762 struct breakpoint *b, *b_tmp;
7763
7764 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7765 if (b->type == bp_std_terminate)
7766 delete_breakpoint (b);
7767 }
7768
7769 struct breakpoint *
7770 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7771 {
7772 struct breakpoint *b;
7773
7774 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7775 &internal_breakpoint_ops);
7776
7777 b->enable_state = bp_enabled;
7778 /* location has to be used or breakpoint_re_set will delete me. */
7779 b->location = new_address_location (b->loc->address, NULL, 0);
7780
7781 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7782
7783 return b;
7784 }
7785
7786 struct lang_and_radix
7787 {
7788 enum language lang;
7789 int radix;
7790 };
7791
7792 /* Create a breakpoint for JIT code registration and unregistration. */
7793
7794 struct breakpoint *
7795 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7796 {
7797 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7798 &internal_breakpoint_ops);
7799 }
7800
7801 /* Remove JIT code registration and unregistration breakpoint(s). */
7802
7803 void
7804 remove_jit_event_breakpoints (void)
7805 {
7806 struct breakpoint *b, *b_tmp;
7807
7808 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7809 if (b->type == bp_jit_event
7810 && b->loc->pspace == current_program_space)
7811 delete_breakpoint (b);
7812 }
7813
7814 void
7815 remove_solib_event_breakpoints (void)
7816 {
7817 struct breakpoint *b, *b_tmp;
7818
7819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7820 if (b->type == bp_shlib_event
7821 && b->loc->pspace == current_program_space)
7822 delete_breakpoint (b);
7823 }
7824
7825 /* See breakpoint.h. */
7826
7827 void
7828 remove_solib_event_breakpoints_at_next_stop (void)
7829 {
7830 struct breakpoint *b, *b_tmp;
7831
7832 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7833 if (b->type == bp_shlib_event
7834 && b->loc->pspace == current_program_space)
7835 b->disposition = disp_del_at_next_stop;
7836 }
7837
7838 /* Helper for create_solib_event_breakpoint /
7839 create_and_insert_solib_event_breakpoint. Allows specifying which
7840 INSERT_MODE to pass through to update_global_location_list. */
7841
7842 static struct breakpoint *
7843 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7844 enum ugll_insert_mode insert_mode)
7845 {
7846 struct breakpoint *b;
7847
7848 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7849 &internal_breakpoint_ops);
7850 update_global_location_list_nothrow (insert_mode);
7851 return b;
7852 }
7853
7854 struct breakpoint *
7855 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7856 {
7857 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7858 }
7859
7860 /* See breakpoint.h. */
7861
7862 struct breakpoint *
7863 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7864 {
7865 struct breakpoint *b;
7866
7867 /* Explicitly tell update_global_location_list to insert
7868 locations. */
7869 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7870 if (!b->loc->inserted)
7871 {
7872 delete_breakpoint (b);
7873 return NULL;
7874 }
7875 return b;
7876 }
7877
7878 /* Disable any breakpoints that are on code in shared libraries. Only
7879 apply to enabled breakpoints, disabled ones can just stay disabled. */
7880
7881 void
7882 disable_breakpoints_in_shlibs (void)
7883 {
7884 struct bp_location *loc, **locp_tmp;
7885
7886 ALL_BP_LOCATIONS (loc, locp_tmp)
7887 {
7888 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7889 struct breakpoint *b = loc->owner;
7890
7891 /* We apply the check to all breakpoints, including disabled for
7892 those with loc->duplicate set. This is so that when breakpoint
7893 becomes enabled, or the duplicate is removed, gdb will try to
7894 insert all breakpoints. If we don't set shlib_disabled here,
7895 we'll try to insert those breakpoints and fail. */
7896 if (((b->type == bp_breakpoint)
7897 || (b->type == bp_jit_event)
7898 || (b->type == bp_hardware_breakpoint)
7899 || (is_tracepoint (b)))
7900 && loc->pspace == current_program_space
7901 && !loc->shlib_disabled
7902 && solib_name_from_address (loc->pspace, loc->address)
7903 )
7904 {
7905 loc->shlib_disabled = 1;
7906 }
7907 }
7908 }
7909
7910 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7911 notification of unloaded_shlib. Only apply to enabled breakpoints,
7912 disabled ones can just stay disabled. */
7913
7914 static void
7915 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7916 {
7917 struct bp_location *loc, **locp_tmp;
7918 int disabled_shlib_breaks = 0;
7919
7920 ALL_BP_LOCATIONS (loc, locp_tmp)
7921 {
7922 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7923 struct breakpoint *b = loc->owner;
7924
7925 if (solib->pspace == loc->pspace
7926 && !loc->shlib_disabled
7927 && (((b->type == bp_breakpoint
7928 || b->type == bp_jit_event
7929 || b->type == bp_hardware_breakpoint)
7930 && (loc->loc_type == bp_loc_hardware_breakpoint
7931 || loc->loc_type == bp_loc_software_breakpoint))
7932 || is_tracepoint (b))
7933 && solib_contains_address_p (solib, loc->address))
7934 {
7935 loc->shlib_disabled = 1;
7936 /* At this point, we cannot rely on remove_breakpoint
7937 succeeding so we must mark the breakpoint as not inserted
7938 to prevent future errors occurring in remove_breakpoints. */
7939 loc->inserted = 0;
7940
7941 /* This may cause duplicate notifications for the same breakpoint. */
7942 observer_notify_breakpoint_modified (b);
7943
7944 if (!disabled_shlib_breaks)
7945 {
7946 target_terminal::ours_for_output ();
7947 warning (_("Temporarily disabling breakpoints "
7948 "for unloaded shared library \"%s\""),
7949 solib->so_name);
7950 }
7951 disabled_shlib_breaks = 1;
7952 }
7953 }
7954 }
7955
7956 /* Disable any breakpoints and tracepoints in OBJFILE upon
7957 notification of free_objfile. Only apply to enabled breakpoints,
7958 disabled ones can just stay disabled. */
7959
7960 static void
7961 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7962 {
7963 struct breakpoint *b;
7964
7965 if (objfile == NULL)
7966 return;
7967
7968 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7969 managed by the user with add-symbol-file/remove-symbol-file.
7970 Similarly to how breakpoints in shared libraries are handled in
7971 response to "nosharedlibrary", mark breakpoints in such modules
7972 shlib_disabled so they end up uninserted on the next global
7973 location list update. Shared libraries not loaded by the user
7974 aren't handled here -- they're already handled in
7975 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7976 solib_unloaded observer. We skip objfiles that are not
7977 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7978 main objfile). */
7979 if ((objfile->flags & OBJF_SHARED) == 0
7980 || (objfile->flags & OBJF_USERLOADED) == 0)
7981 return;
7982
7983 ALL_BREAKPOINTS (b)
7984 {
7985 struct bp_location *loc;
7986 int bp_modified = 0;
7987
7988 if (!is_breakpoint (b) && !is_tracepoint (b))
7989 continue;
7990
7991 for (loc = b->loc; loc != NULL; loc = loc->next)
7992 {
7993 CORE_ADDR loc_addr = loc->address;
7994
7995 if (loc->loc_type != bp_loc_hardware_breakpoint
7996 && loc->loc_type != bp_loc_software_breakpoint)
7997 continue;
7998
7999 if (loc->shlib_disabled != 0)
8000 continue;
8001
8002 if (objfile->pspace != loc->pspace)
8003 continue;
8004
8005 if (loc->loc_type != bp_loc_hardware_breakpoint
8006 && loc->loc_type != bp_loc_software_breakpoint)
8007 continue;
8008
8009 if (is_addr_in_objfile (loc_addr, objfile))
8010 {
8011 loc->shlib_disabled = 1;
8012 /* At this point, we don't know whether the object was
8013 unmapped from the inferior or not, so leave the
8014 inserted flag alone. We'll handle failure to
8015 uninsert quietly, in case the object was indeed
8016 unmapped. */
8017
8018 mark_breakpoint_location_modified (loc);
8019
8020 bp_modified = 1;
8021 }
8022 }
8023
8024 if (bp_modified)
8025 observer_notify_breakpoint_modified (b);
8026 }
8027 }
8028
8029 /* FORK & VFORK catchpoints. */
8030
8031 /* An instance of this type is used to represent a fork or vfork
8032 catchpoint. A breakpoint is really of this type iff its ops pointer points
8033 to CATCH_FORK_BREAKPOINT_OPS. */
8034
8035 struct fork_catchpoint : public breakpoint
8036 {
8037 /* Process id of a child process whose forking triggered this
8038 catchpoint. This field is only valid immediately after this
8039 catchpoint has triggered. */
8040 ptid_t forked_inferior_pid;
8041 };
8042
8043 /* Implement the "insert" breakpoint_ops method for fork
8044 catchpoints. */
8045
8046 static int
8047 insert_catch_fork (struct bp_location *bl)
8048 {
8049 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8050 }
8051
8052 /* Implement the "remove" breakpoint_ops method for fork
8053 catchpoints. */
8054
8055 static int
8056 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8057 {
8058 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8059 }
8060
8061 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8062 catchpoints. */
8063
8064 static int
8065 breakpoint_hit_catch_fork (const struct bp_location *bl,
8066 struct address_space *aspace, CORE_ADDR bp_addr,
8067 const struct target_waitstatus *ws)
8068 {
8069 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8070
8071 if (ws->kind != TARGET_WAITKIND_FORKED)
8072 return 0;
8073
8074 c->forked_inferior_pid = ws->value.related_pid;
8075 return 1;
8076 }
8077
8078 /* Implement the "print_it" breakpoint_ops method for fork
8079 catchpoints. */
8080
8081 static enum print_stop_action
8082 print_it_catch_fork (bpstat bs)
8083 {
8084 struct ui_out *uiout = current_uiout;
8085 struct breakpoint *b = bs->breakpoint_at;
8086 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8087
8088 annotate_catchpoint (b->number);
8089 maybe_print_thread_hit_breakpoint (uiout);
8090 if (b->disposition == disp_del)
8091 uiout->text ("Temporary catchpoint ");
8092 else
8093 uiout->text ("Catchpoint ");
8094 if (uiout->is_mi_like_p ())
8095 {
8096 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8097 uiout->field_string ("disp", bpdisp_text (b->disposition));
8098 }
8099 uiout->field_int ("bkptno", b->number);
8100 uiout->text (" (forked process ");
8101 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8102 uiout->text ("), ");
8103 return PRINT_SRC_AND_LOC;
8104 }
8105
8106 /* Implement the "print_one" breakpoint_ops method for fork
8107 catchpoints. */
8108
8109 static void
8110 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8111 {
8112 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8113 struct value_print_options opts;
8114 struct ui_out *uiout = current_uiout;
8115
8116 get_user_print_options (&opts);
8117
8118 /* Field 4, the address, is omitted (which makes the columns not
8119 line up too nicely with the headers, but the effect is relatively
8120 readable). */
8121 if (opts.addressprint)
8122 uiout->field_skip ("addr");
8123 annotate_field (5);
8124 uiout->text ("fork");
8125 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8126 {
8127 uiout->text (", process ");
8128 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8129 uiout->spaces (1);
8130 }
8131
8132 if (uiout->is_mi_like_p ())
8133 uiout->field_string ("catch-type", "fork");
8134 }
8135
8136 /* Implement the "print_mention" breakpoint_ops method for fork
8137 catchpoints. */
8138
8139 static void
8140 print_mention_catch_fork (struct breakpoint *b)
8141 {
8142 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8143 }
8144
8145 /* Implement the "print_recreate" breakpoint_ops method for fork
8146 catchpoints. */
8147
8148 static void
8149 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8150 {
8151 fprintf_unfiltered (fp, "catch fork");
8152 print_recreate_thread (b, fp);
8153 }
8154
8155 /* The breakpoint_ops structure to be used in fork catchpoints. */
8156
8157 static struct breakpoint_ops catch_fork_breakpoint_ops;
8158
8159 /* Implement the "insert" breakpoint_ops method for vfork
8160 catchpoints. */
8161
8162 static int
8163 insert_catch_vfork (struct bp_location *bl)
8164 {
8165 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8166 }
8167
8168 /* Implement the "remove" breakpoint_ops method for vfork
8169 catchpoints. */
8170
8171 static int
8172 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8173 {
8174 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8175 }
8176
8177 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8178 catchpoints. */
8179
8180 static int
8181 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8182 struct address_space *aspace, CORE_ADDR bp_addr,
8183 const struct target_waitstatus *ws)
8184 {
8185 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8186
8187 if (ws->kind != TARGET_WAITKIND_VFORKED)
8188 return 0;
8189
8190 c->forked_inferior_pid = ws->value.related_pid;
8191 return 1;
8192 }
8193
8194 /* Implement the "print_it" breakpoint_ops method for vfork
8195 catchpoints. */
8196
8197 static enum print_stop_action
8198 print_it_catch_vfork (bpstat bs)
8199 {
8200 struct ui_out *uiout = current_uiout;
8201 struct breakpoint *b = bs->breakpoint_at;
8202 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8203
8204 annotate_catchpoint (b->number);
8205 maybe_print_thread_hit_breakpoint (uiout);
8206 if (b->disposition == disp_del)
8207 uiout->text ("Temporary catchpoint ");
8208 else
8209 uiout->text ("Catchpoint ");
8210 if (uiout->is_mi_like_p ())
8211 {
8212 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8213 uiout->field_string ("disp", bpdisp_text (b->disposition));
8214 }
8215 uiout->field_int ("bkptno", b->number);
8216 uiout->text (" (vforked process ");
8217 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8218 uiout->text ("), ");
8219 return PRINT_SRC_AND_LOC;
8220 }
8221
8222 /* Implement the "print_one" breakpoint_ops method for vfork
8223 catchpoints. */
8224
8225 static void
8226 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8227 {
8228 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8229 struct value_print_options opts;
8230 struct ui_out *uiout = current_uiout;
8231
8232 get_user_print_options (&opts);
8233 /* Field 4, the address, is omitted (which makes the columns not
8234 line up too nicely with the headers, but the effect is relatively
8235 readable). */
8236 if (opts.addressprint)
8237 uiout->field_skip ("addr");
8238 annotate_field (5);
8239 uiout->text ("vfork");
8240 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8241 {
8242 uiout->text (", process ");
8243 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8244 uiout->spaces (1);
8245 }
8246
8247 if (uiout->is_mi_like_p ())
8248 uiout->field_string ("catch-type", "vfork");
8249 }
8250
8251 /* Implement the "print_mention" breakpoint_ops method for vfork
8252 catchpoints. */
8253
8254 static void
8255 print_mention_catch_vfork (struct breakpoint *b)
8256 {
8257 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8258 }
8259
8260 /* Implement the "print_recreate" breakpoint_ops method for vfork
8261 catchpoints. */
8262
8263 static void
8264 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8265 {
8266 fprintf_unfiltered (fp, "catch vfork");
8267 print_recreate_thread (b, fp);
8268 }
8269
8270 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8271
8272 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8273
8274 /* An instance of this type is used to represent an solib catchpoint.
8275 A breakpoint is really of this type iff its ops pointer points to
8276 CATCH_SOLIB_BREAKPOINT_OPS. */
8277
8278 struct solib_catchpoint : public breakpoint
8279 {
8280 ~solib_catchpoint () override;
8281
8282 /* True for "catch load", false for "catch unload". */
8283 unsigned char is_load;
8284
8285 /* Regular expression to match, if any. COMPILED is only valid when
8286 REGEX is non-NULL. */
8287 char *regex;
8288 std::unique_ptr<compiled_regex> compiled;
8289 };
8290
8291 solib_catchpoint::~solib_catchpoint ()
8292 {
8293 xfree (this->regex);
8294 }
8295
8296 static int
8297 insert_catch_solib (struct bp_location *ignore)
8298 {
8299 return 0;
8300 }
8301
8302 static int
8303 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8304 {
8305 return 0;
8306 }
8307
8308 static int
8309 breakpoint_hit_catch_solib (const struct bp_location *bl,
8310 struct address_space *aspace,
8311 CORE_ADDR bp_addr,
8312 const struct target_waitstatus *ws)
8313 {
8314 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8315 struct breakpoint *other;
8316
8317 if (ws->kind == TARGET_WAITKIND_LOADED)
8318 return 1;
8319
8320 ALL_BREAKPOINTS (other)
8321 {
8322 struct bp_location *other_bl;
8323
8324 if (other == bl->owner)
8325 continue;
8326
8327 if (other->type != bp_shlib_event)
8328 continue;
8329
8330 if (self->pspace != NULL && other->pspace != self->pspace)
8331 continue;
8332
8333 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8334 {
8335 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8336 return 1;
8337 }
8338 }
8339
8340 return 0;
8341 }
8342
8343 static void
8344 check_status_catch_solib (struct bpstats *bs)
8345 {
8346 struct solib_catchpoint *self
8347 = (struct solib_catchpoint *) bs->breakpoint_at;
8348 int ix;
8349
8350 if (self->is_load)
8351 {
8352 struct so_list *iter;
8353
8354 for (ix = 0;
8355 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8356 ix, iter);
8357 ++ix)
8358 {
8359 if (!self->regex
8360 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8361 return;
8362 }
8363 }
8364 else
8365 {
8366 char *iter;
8367
8368 for (ix = 0;
8369 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8370 ix, iter);
8371 ++ix)
8372 {
8373 if (!self->regex
8374 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8375 return;
8376 }
8377 }
8378
8379 bs->stop = 0;
8380 bs->print_it = print_it_noop;
8381 }
8382
8383 static enum print_stop_action
8384 print_it_catch_solib (bpstat bs)
8385 {
8386 struct breakpoint *b = bs->breakpoint_at;
8387 struct ui_out *uiout = current_uiout;
8388
8389 annotate_catchpoint (b->number);
8390 maybe_print_thread_hit_breakpoint (uiout);
8391 if (b->disposition == disp_del)
8392 uiout->text ("Temporary catchpoint ");
8393 else
8394 uiout->text ("Catchpoint ");
8395 uiout->field_int ("bkptno", b->number);
8396 uiout->text ("\n");
8397 if (uiout->is_mi_like_p ())
8398 uiout->field_string ("disp", bpdisp_text (b->disposition));
8399 print_solib_event (1);
8400 return PRINT_SRC_AND_LOC;
8401 }
8402
8403 static void
8404 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8405 {
8406 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8407 struct value_print_options opts;
8408 struct ui_out *uiout = current_uiout;
8409 char *msg;
8410
8411 get_user_print_options (&opts);
8412 /* Field 4, the address, is omitted (which makes the columns not
8413 line up too nicely with the headers, but the effect is relatively
8414 readable). */
8415 if (opts.addressprint)
8416 {
8417 annotate_field (4);
8418 uiout->field_skip ("addr");
8419 }
8420
8421 annotate_field (5);
8422 if (self->is_load)
8423 {
8424 if (self->regex)
8425 msg = xstrprintf (_("load of library matching %s"), self->regex);
8426 else
8427 msg = xstrdup (_("load of library"));
8428 }
8429 else
8430 {
8431 if (self->regex)
8432 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8433 else
8434 msg = xstrdup (_("unload of library"));
8435 }
8436 uiout->field_string ("what", msg);
8437 xfree (msg);
8438
8439 if (uiout->is_mi_like_p ())
8440 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8441 }
8442
8443 static void
8444 print_mention_catch_solib (struct breakpoint *b)
8445 {
8446 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8447
8448 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8449 self->is_load ? "load" : "unload");
8450 }
8451
8452 static void
8453 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8454 {
8455 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8456
8457 fprintf_unfiltered (fp, "%s %s",
8458 b->disposition == disp_del ? "tcatch" : "catch",
8459 self->is_load ? "load" : "unload");
8460 if (self->regex)
8461 fprintf_unfiltered (fp, " %s", self->regex);
8462 fprintf_unfiltered (fp, "\n");
8463 }
8464
8465 static struct breakpoint_ops catch_solib_breakpoint_ops;
8466
8467 /* Shared helper function (MI and CLI) for creating and installing
8468 a shared object event catchpoint. If IS_LOAD is non-zero then
8469 the events to be caught are load events, otherwise they are
8470 unload events. If IS_TEMP is non-zero the catchpoint is a
8471 temporary one. If ENABLED is non-zero the catchpoint is
8472 created in an enabled state. */
8473
8474 void
8475 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8476 {
8477 struct gdbarch *gdbarch = get_current_arch ();
8478
8479 if (!arg)
8480 arg = "";
8481 arg = skip_spaces (arg);
8482
8483 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
8484
8485 if (*arg != '\0')
8486 {
8487 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8488 _("Invalid regexp")));
8489 c->regex = xstrdup (arg);
8490 }
8491
8492 c->is_load = is_load;
8493 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
8494 &catch_solib_breakpoint_ops);
8495
8496 c->enable_state = enabled ? bp_enabled : bp_disabled;
8497
8498 install_breakpoint (0, std::move (c), 1);
8499 }
8500
8501 /* A helper function that does all the work for "catch load" and
8502 "catch unload". */
8503
8504 static void
8505 catch_load_or_unload (char *arg, int from_tty, int is_load,
8506 struct cmd_list_element *command)
8507 {
8508 int tempflag;
8509 const int enabled = 1;
8510
8511 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8512
8513 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8514 }
8515
8516 static void
8517 catch_load_command_1 (char *arg, int from_tty,
8518 struct cmd_list_element *command)
8519 {
8520 catch_load_or_unload (arg, from_tty, 1, command);
8521 }
8522
8523 static void
8524 catch_unload_command_1 (char *arg, int from_tty,
8525 struct cmd_list_element *command)
8526 {
8527 catch_load_or_unload (arg, from_tty, 0, command);
8528 }
8529
8530 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8531 is non-zero, then make the breakpoint temporary. If COND_STRING is
8532 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8533 the breakpoint_ops structure associated to the catchpoint. */
8534
8535 void
8536 init_catchpoint (struct breakpoint *b,
8537 struct gdbarch *gdbarch, int tempflag,
8538 const char *cond_string,
8539 const struct breakpoint_ops *ops)
8540 {
8541 symtab_and_line sal;
8542 sal.pspace = current_program_space;
8543
8544 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8545
8546 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8547 b->disposition = tempflag ? disp_del : disp_donttouch;
8548 }
8549
8550 void
8551 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8552 {
8553 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8554 set_breakpoint_number (internal, b);
8555 if (is_tracepoint (b))
8556 set_tracepoint_count (breakpoint_count);
8557 if (!internal)
8558 mention (b);
8559 observer_notify_breakpoint_created (b);
8560
8561 if (update_gll)
8562 update_global_location_list (UGLL_MAY_INSERT);
8563 }
8564
8565 static void
8566 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8567 int tempflag, const char *cond_string,
8568 const struct breakpoint_ops *ops)
8569 {
8570 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
8571
8572 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
8573
8574 c->forked_inferior_pid = null_ptid;
8575
8576 install_breakpoint (0, std::move (c), 1);
8577 }
8578
8579 /* Exec catchpoints. */
8580
8581 /* An instance of this type is used to represent an exec catchpoint.
8582 A breakpoint is really of this type iff its ops pointer points to
8583 CATCH_EXEC_BREAKPOINT_OPS. */
8584
8585 struct exec_catchpoint : public breakpoint
8586 {
8587 ~exec_catchpoint () override;
8588
8589 /* Filename of a program whose exec triggered this catchpoint.
8590 This field is only valid immediately after this catchpoint has
8591 triggered. */
8592 char *exec_pathname;
8593 };
8594
8595 /* Exec catchpoint destructor. */
8596
8597 exec_catchpoint::~exec_catchpoint ()
8598 {
8599 xfree (this->exec_pathname);
8600 }
8601
8602 static int
8603 insert_catch_exec (struct bp_location *bl)
8604 {
8605 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8606 }
8607
8608 static int
8609 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8610 {
8611 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8612 }
8613
8614 static int
8615 breakpoint_hit_catch_exec (const struct bp_location *bl,
8616 struct address_space *aspace, CORE_ADDR bp_addr,
8617 const struct target_waitstatus *ws)
8618 {
8619 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8620
8621 if (ws->kind != TARGET_WAITKIND_EXECD)
8622 return 0;
8623
8624 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8625 return 1;
8626 }
8627
8628 static enum print_stop_action
8629 print_it_catch_exec (bpstat bs)
8630 {
8631 struct ui_out *uiout = current_uiout;
8632 struct breakpoint *b = bs->breakpoint_at;
8633 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8634
8635 annotate_catchpoint (b->number);
8636 maybe_print_thread_hit_breakpoint (uiout);
8637 if (b->disposition == disp_del)
8638 uiout->text ("Temporary catchpoint ");
8639 else
8640 uiout->text ("Catchpoint ");
8641 if (uiout->is_mi_like_p ())
8642 {
8643 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8644 uiout->field_string ("disp", bpdisp_text (b->disposition));
8645 }
8646 uiout->field_int ("bkptno", b->number);
8647 uiout->text (" (exec'd ");
8648 uiout->field_string ("new-exec", c->exec_pathname);
8649 uiout->text ("), ");
8650
8651 return PRINT_SRC_AND_LOC;
8652 }
8653
8654 static void
8655 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8656 {
8657 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8658 struct value_print_options opts;
8659 struct ui_out *uiout = current_uiout;
8660
8661 get_user_print_options (&opts);
8662
8663 /* Field 4, the address, is omitted (which makes the columns
8664 not line up too nicely with the headers, but the effect
8665 is relatively readable). */
8666 if (opts.addressprint)
8667 uiout->field_skip ("addr");
8668 annotate_field (5);
8669 uiout->text ("exec");
8670 if (c->exec_pathname != NULL)
8671 {
8672 uiout->text (", program \"");
8673 uiout->field_string ("what", c->exec_pathname);
8674 uiout->text ("\" ");
8675 }
8676
8677 if (uiout->is_mi_like_p ())
8678 uiout->field_string ("catch-type", "exec");
8679 }
8680
8681 static void
8682 print_mention_catch_exec (struct breakpoint *b)
8683 {
8684 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8685 }
8686
8687 /* Implement the "print_recreate" breakpoint_ops method for exec
8688 catchpoints. */
8689
8690 static void
8691 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8692 {
8693 fprintf_unfiltered (fp, "catch exec");
8694 print_recreate_thread (b, fp);
8695 }
8696
8697 static struct breakpoint_ops catch_exec_breakpoint_ops;
8698
8699 static int
8700 hw_breakpoint_used_count (void)
8701 {
8702 int i = 0;
8703 struct breakpoint *b;
8704 struct bp_location *bl;
8705
8706 ALL_BREAKPOINTS (b)
8707 {
8708 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8709 for (bl = b->loc; bl; bl = bl->next)
8710 {
8711 /* Special types of hardware breakpoints may use more than
8712 one register. */
8713 i += b->ops->resources_needed (bl);
8714 }
8715 }
8716
8717 return i;
8718 }
8719
8720 /* Returns the resources B would use if it were a hardware
8721 watchpoint. */
8722
8723 static int
8724 hw_watchpoint_use_count (struct breakpoint *b)
8725 {
8726 int i = 0;
8727 struct bp_location *bl;
8728
8729 if (!breakpoint_enabled (b))
8730 return 0;
8731
8732 for (bl = b->loc; bl; bl = bl->next)
8733 {
8734 /* Special types of hardware watchpoints may use more than
8735 one register. */
8736 i += b->ops->resources_needed (bl);
8737 }
8738
8739 return i;
8740 }
8741
8742 /* Returns the sum the used resources of all hardware watchpoints of
8743 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8744 the sum of the used resources of all hardware watchpoints of other
8745 types _not_ TYPE. */
8746
8747 static int
8748 hw_watchpoint_used_count_others (struct breakpoint *except,
8749 enum bptype type, int *other_type_used)
8750 {
8751 int i = 0;
8752 struct breakpoint *b;
8753
8754 *other_type_used = 0;
8755 ALL_BREAKPOINTS (b)
8756 {
8757 if (b == except)
8758 continue;
8759 if (!breakpoint_enabled (b))
8760 continue;
8761
8762 if (b->type == type)
8763 i += hw_watchpoint_use_count (b);
8764 else if (is_hardware_watchpoint (b))
8765 *other_type_used = 1;
8766 }
8767
8768 return i;
8769 }
8770
8771 void
8772 disable_watchpoints_before_interactive_call_start (void)
8773 {
8774 struct breakpoint *b;
8775
8776 ALL_BREAKPOINTS (b)
8777 {
8778 if (is_watchpoint (b) && breakpoint_enabled (b))
8779 {
8780 b->enable_state = bp_call_disabled;
8781 update_global_location_list (UGLL_DONT_INSERT);
8782 }
8783 }
8784 }
8785
8786 void
8787 enable_watchpoints_after_interactive_call_stop (void)
8788 {
8789 struct breakpoint *b;
8790
8791 ALL_BREAKPOINTS (b)
8792 {
8793 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8794 {
8795 b->enable_state = bp_enabled;
8796 update_global_location_list (UGLL_MAY_INSERT);
8797 }
8798 }
8799 }
8800
8801 void
8802 disable_breakpoints_before_startup (void)
8803 {
8804 current_program_space->executing_startup = 1;
8805 update_global_location_list (UGLL_DONT_INSERT);
8806 }
8807
8808 void
8809 enable_breakpoints_after_startup (void)
8810 {
8811 current_program_space->executing_startup = 0;
8812 breakpoint_re_set ();
8813 }
8814
8815 /* Create a new single-step breakpoint for thread THREAD, with no
8816 locations. */
8817
8818 static struct breakpoint *
8819 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8820 {
8821 std::unique_ptr<breakpoint> b (new breakpoint ());
8822
8823 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
8824 &momentary_breakpoint_ops);
8825
8826 b->disposition = disp_donttouch;
8827 b->frame_id = null_frame_id;
8828
8829 b->thread = thread;
8830 gdb_assert (b->thread != 0);
8831
8832 return add_to_breakpoint_chain (std::move (b));
8833 }
8834
8835 /* Set a momentary breakpoint of type TYPE at address specified by
8836 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8837 frame. */
8838
8839 struct breakpoint *
8840 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8841 struct frame_id frame_id, enum bptype type)
8842 {
8843 struct breakpoint *b;
8844
8845 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8846 tail-called one. */
8847 gdb_assert (!frame_id_artificial_p (frame_id));
8848
8849 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8850 b->enable_state = bp_enabled;
8851 b->disposition = disp_donttouch;
8852 b->frame_id = frame_id;
8853
8854 /* If we're debugging a multi-threaded program, then we want
8855 momentary breakpoints to be active in only a single thread of
8856 control. */
8857 if (in_thread_list (inferior_ptid))
8858 b->thread = ptid_to_global_thread_id (inferior_ptid);
8859
8860 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8861
8862 return b;
8863 }
8864
8865 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8866 The new breakpoint will have type TYPE, use OPS as its
8867 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8868
8869 static struct breakpoint *
8870 momentary_breakpoint_from_master (struct breakpoint *orig,
8871 enum bptype type,
8872 const struct breakpoint_ops *ops,
8873 int loc_enabled)
8874 {
8875 struct breakpoint *copy;
8876
8877 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8878 copy->loc = allocate_bp_location (copy);
8879 set_breakpoint_location_function (copy->loc, 1);
8880
8881 copy->loc->gdbarch = orig->loc->gdbarch;
8882 copy->loc->requested_address = orig->loc->requested_address;
8883 copy->loc->address = orig->loc->address;
8884 copy->loc->section = orig->loc->section;
8885 copy->loc->pspace = orig->loc->pspace;
8886 copy->loc->probe = orig->loc->probe;
8887 copy->loc->line_number = orig->loc->line_number;
8888 copy->loc->symtab = orig->loc->symtab;
8889 copy->loc->enabled = loc_enabled;
8890 copy->frame_id = orig->frame_id;
8891 copy->thread = orig->thread;
8892 copy->pspace = orig->pspace;
8893
8894 copy->enable_state = bp_enabled;
8895 copy->disposition = disp_donttouch;
8896 copy->number = internal_breakpoint_number--;
8897
8898 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8899 return copy;
8900 }
8901
8902 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8903 ORIG is NULL. */
8904
8905 struct breakpoint *
8906 clone_momentary_breakpoint (struct breakpoint *orig)
8907 {
8908 /* If there's nothing to clone, then return nothing. */
8909 if (orig == NULL)
8910 return NULL;
8911
8912 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8913 }
8914
8915 struct breakpoint *
8916 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8917 enum bptype type)
8918 {
8919 struct symtab_and_line sal;
8920
8921 sal = find_pc_line (pc, 0);
8922 sal.pc = pc;
8923 sal.section = find_pc_overlay (pc);
8924 sal.explicit_pc = 1;
8925
8926 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8927 }
8928 \f
8929
8930 /* Tell the user we have just set a breakpoint B. */
8931
8932 static void
8933 mention (struct breakpoint *b)
8934 {
8935 b->ops->print_mention (b);
8936 if (current_uiout->is_mi_like_p ())
8937 return;
8938 printf_filtered ("\n");
8939 }
8940 \f
8941
8942 static int bp_loc_is_permanent (struct bp_location *loc);
8943
8944 static struct bp_location *
8945 add_location_to_breakpoint (struct breakpoint *b,
8946 const struct symtab_and_line *sal)
8947 {
8948 struct bp_location *loc, **tmp;
8949 CORE_ADDR adjusted_address;
8950 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8951
8952 if (loc_gdbarch == NULL)
8953 loc_gdbarch = b->gdbarch;
8954
8955 /* Adjust the breakpoint's address prior to allocating a location.
8956 Once we call allocate_bp_location(), that mostly uninitialized
8957 location will be placed on the location chain. Adjustment of the
8958 breakpoint may cause target_read_memory() to be called and we do
8959 not want its scan of the location chain to find a breakpoint and
8960 location that's only been partially initialized. */
8961 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8962 sal->pc, b->type);
8963
8964 /* Sort the locations by their ADDRESS. */
8965 loc = allocate_bp_location (b);
8966 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8967 tmp = &((*tmp)->next))
8968 ;
8969 loc->next = *tmp;
8970 *tmp = loc;
8971
8972 loc->requested_address = sal->pc;
8973 loc->address = adjusted_address;
8974 loc->pspace = sal->pspace;
8975 loc->probe.probe = sal->probe;
8976 loc->probe.objfile = sal->objfile;
8977 gdb_assert (loc->pspace != NULL);
8978 loc->section = sal->section;
8979 loc->gdbarch = loc_gdbarch;
8980 loc->line_number = sal->line;
8981 loc->symtab = sal->symtab;
8982
8983 set_breakpoint_location_function (loc,
8984 sal->explicit_pc || sal->explicit_line);
8985
8986 /* While by definition, permanent breakpoints are already present in the
8987 code, we don't mark the location as inserted. Normally one would expect
8988 that GDB could rely on that breakpoint instruction to stop the program,
8989 thus removing the need to insert its own breakpoint, except that executing
8990 the breakpoint instruction can kill the target instead of reporting a
8991 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8992 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8993 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8994 breakpoint be inserted normally results in QEMU knowing about the GDB
8995 breakpoint, and thus trap before the breakpoint instruction is executed.
8996 (If GDB later needs to continue execution past the permanent breakpoint,
8997 it manually increments the PC, thus avoiding executing the breakpoint
8998 instruction.) */
8999 if (bp_loc_is_permanent (loc))
9000 loc->permanent = 1;
9001
9002 return loc;
9003 }
9004 \f
9005
9006 /* See breakpoint.h. */
9007
9008 int
9009 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9010 {
9011 int len;
9012 CORE_ADDR addr;
9013 const gdb_byte *bpoint;
9014 gdb_byte *target_mem;
9015
9016 addr = address;
9017 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9018
9019 /* Software breakpoints unsupported? */
9020 if (bpoint == NULL)
9021 return 0;
9022
9023 target_mem = (gdb_byte *) alloca (len);
9024
9025 /* Enable the automatic memory restoration from breakpoints while
9026 we read the memory. Otherwise we could say about our temporary
9027 breakpoints they are permanent. */
9028 scoped_restore restore_memory
9029 = make_scoped_restore_show_memory_breakpoints (0);
9030
9031 if (target_read_memory (address, target_mem, len) == 0
9032 && memcmp (target_mem, bpoint, len) == 0)
9033 return 1;
9034
9035 return 0;
9036 }
9037
9038 /* Return 1 if LOC is pointing to a permanent breakpoint,
9039 return 0 otherwise. */
9040
9041 static int
9042 bp_loc_is_permanent (struct bp_location *loc)
9043 {
9044 gdb_assert (loc != NULL);
9045
9046 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9047 attempt to read from the addresses the locations of these breakpoint types
9048 point to. program_breakpoint_here_p, below, will attempt to read
9049 memory. */
9050 if (!breakpoint_address_is_meaningful (loc->owner))
9051 return 0;
9052
9053 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9054 switch_to_program_space_and_thread (loc->pspace);
9055 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9056 }
9057
9058 /* Build a command list for the dprintf corresponding to the current
9059 settings of the dprintf style options. */
9060
9061 static void
9062 update_dprintf_command_list (struct breakpoint *b)
9063 {
9064 char *dprintf_args = b->extra_string;
9065 char *printf_line = NULL;
9066
9067 if (!dprintf_args)
9068 return;
9069
9070 dprintf_args = skip_spaces (dprintf_args);
9071
9072 /* Allow a comma, as it may have terminated a location, but don't
9073 insist on it. */
9074 if (*dprintf_args == ',')
9075 ++dprintf_args;
9076 dprintf_args = skip_spaces (dprintf_args);
9077
9078 if (*dprintf_args != '"')
9079 error (_("Bad format string, missing '\"'."));
9080
9081 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9082 printf_line = xstrprintf ("printf %s", dprintf_args);
9083 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9084 {
9085 if (!dprintf_function)
9086 error (_("No function supplied for dprintf call"));
9087
9088 if (dprintf_channel && strlen (dprintf_channel) > 0)
9089 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9090 dprintf_function,
9091 dprintf_channel,
9092 dprintf_args);
9093 else
9094 printf_line = xstrprintf ("call (void) %s (%s)",
9095 dprintf_function,
9096 dprintf_args);
9097 }
9098 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9099 {
9100 if (target_can_run_breakpoint_commands ())
9101 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9102 else
9103 {
9104 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9105 printf_line = xstrprintf ("printf %s", dprintf_args);
9106 }
9107 }
9108 else
9109 internal_error (__FILE__, __LINE__,
9110 _("Invalid dprintf style."));
9111
9112 gdb_assert (printf_line != NULL);
9113 /* Manufacture a printf sequence. */
9114 {
9115 struct command_line *printf_cmd_line = XNEW (struct command_line);
9116
9117 printf_cmd_line->control_type = simple_control;
9118 printf_cmd_line->body_count = 0;
9119 printf_cmd_line->body_list = NULL;
9120 printf_cmd_line->next = NULL;
9121 printf_cmd_line->line = printf_line;
9122
9123 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9124 }
9125 }
9126
9127 /* Update all dprintf commands, making their command lists reflect
9128 current style settings. */
9129
9130 static void
9131 update_dprintf_commands (char *args, int from_tty,
9132 struct cmd_list_element *c)
9133 {
9134 struct breakpoint *b;
9135
9136 ALL_BREAKPOINTS (b)
9137 {
9138 if (b->type == bp_dprintf)
9139 update_dprintf_command_list (b);
9140 }
9141 }
9142
9143 /* Create a breakpoint with SAL as location. Use LOCATION
9144 as a description of the location, and COND_STRING
9145 as condition expression. If LOCATION is NULL then create an
9146 "address location" from the address in the SAL. */
9147
9148 static void
9149 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9150 gdb::array_view<const symtab_and_line> sals,
9151 event_location_up &&location,
9152 gdb::unique_xmalloc_ptr<char> filter,
9153 gdb::unique_xmalloc_ptr<char> cond_string,
9154 gdb::unique_xmalloc_ptr<char> extra_string,
9155 enum bptype type, enum bpdisp disposition,
9156 int thread, int task, int ignore_count,
9157 const struct breakpoint_ops *ops, int from_tty,
9158 int enabled, int internal, unsigned flags,
9159 int display_canonical)
9160 {
9161 int i;
9162
9163 if (type == bp_hardware_breakpoint)
9164 {
9165 int target_resources_ok;
9166
9167 i = hw_breakpoint_used_count ();
9168 target_resources_ok =
9169 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9170 i + 1, 0);
9171 if (target_resources_ok == 0)
9172 error (_("No hardware breakpoint support in the target."));
9173 else if (target_resources_ok < 0)
9174 error (_("Hardware breakpoints used exceeds limit."));
9175 }
9176
9177 gdb_assert (!sals.empty ());
9178
9179 for (const auto &sal : sals)
9180 {
9181 struct bp_location *loc;
9182
9183 if (from_tty)
9184 {
9185 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9186 if (!loc_gdbarch)
9187 loc_gdbarch = gdbarch;
9188
9189 describe_other_breakpoints (loc_gdbarch,
9190 sal.pspace, sal.pc, sal.section, thread);
9191 }
9192
9193 if (&sal == &sals[0])
9194 {
9195 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9196 b->thread = thread;
9197 b->task = task;
9198
9199 b->cond_string = cond_string.release ();
9200 b->extra_string = extra_string.release ();
9201 b->ignore_count = ignore_count;
9202 b->enable_state = enabled ? bp_enabled : bp_disabled;
9203 b->disposition = disposition;
9204
9205 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9206 b->loc->inserted = 1;
9207
9208 if (type == bp_static_tracepoint)
9209 {
9210 struct tracepoint *t = (struct tracepoint *) b;
9211 struct static_tracepoint_marker marker;
9212
9213 if (strace_marker_p (b))
9214 {
9215 /* We already know the marker exists, otherwise, we
9216 wouldn't see a sal for it. */
9217 const char *p
9218 = &event_location_to_string (b->location.get ())[3];
9219 const char *endp;
9220 char *marker_str;
9221
9222 p = skip_spaces (p);
9223
9224 endp = skip_to_space (p);
9225
9226 marker_str = savestring (p, endp - p);
9227 t->static_trace_marker_id = marker_str;
9228
9229 printf_filtered (_("Probed static tracepoint "
9230 "marker \"%s\"\n"),
9231 t->static_trace_marker_id);
9232 }
9233 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9234 {
9235 t->static_trace_marker_id = xstrdup (marker.str_id);
9236 release_static_tracepoint_marker (&marker);
9237
9238 printf_filtered (_("Probed static tracepoint "
9239 "marker \"%s\"\n"),
9240 t->static_trace_marker_id);
9241 }
9242 else
9243 warning (_("Couldn't determine the static "
9244 "tracepoint marker to probe"));
9245 }
9246
9247 loc = b->loc;
9248 }
9249 else
9250 {
9251 loc = add_location_to_breakpoint (b, &sal);
9252 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9253 loc->inserted = 1;
9254 }
9255
9256 if (b->cond_string)
9257 {
9258 const char *arg = b->cond_string;
9259
9260 loc->cond = parse_exp_1 (&arg, loc->address,
9261 block_for_pc (loc->address), 0);
9262 if (*arg)
9263 error (_("Garbage '%s' follows condition"), arg);
9264 }
9265
9266 /* Dynamic printf requires and uses additional arguments on the
9267 command line, otherwise it's an error. */
9268 if (type == bp_dprintf)
9269 {
9270 if (b->extra_string)
9271 update_dprintf_command_list (b);
9272 else
9273 error (_("Format string required"));
9274 }
9275 else if (b->extra_string)
9276 error (_("Garbage '%s' at end of command"), b->extra_string);
9277 }
9278
9279 b->display_canonical = display_canonical;
9280 if (location != NULL)
9281 b->location = std::move (location);
9282 else
9283 b->location = new_address_location (b->loc->address, NULL, 0);
9284 b->filter = filter.release ();
9285 }
9286
9287 static void
9288 create_breakpoint_sal (struct gdbarch *gdbarch,
9289 gdb::array_view<const symtab_and_line> sals,
9290 event_location_up &&location,
9291 gdb::unique_xmalloc_ptr<char> filter,
9292 gdb::unique_xmalloc_ptr<char> cond_string,
9293 gdb::unique_xmalloc_ptr<char> extra_string,
9294 enum bptype type, enum bpdisp disposition,
9295 int thread, int task, int ignore_count,
9296 const struct breakpoint_ops *ops, int from_tty,
9297 int enabled, int internal, unsigned flags,
9298 int display_canonical)
9299 {
9300 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9301
9302 init_breakpoint_sal (b.get (), gdbarch,
9303 sals, std::move (location),
9304 std::move (filter),
9305 std::move (cond_string),
9306 std::move (extra_string),
9307 type, disposition,
9308 thread, task, ignore_count,
9309 ops, from_tty,
9310 enabled, internal, flags,
9311 display_canonical);
9312
9313 install_breakpoint (internal, std::move (b), 0);
9314 }
9315
9316 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9317 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9318 value. COND_STRING, if not NULL, specified the condition to be
9319 used for all breakpoints. Essentially the only case where
9320 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9321 function. In that case, it's still not possible to specify
9322 separate conditions for different overloaded functions, so
9323 we take just a single condition string.
9324
9325 NOTE: If the function succeeds, the caller is expected to cleanup
9326 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9327 array contents). If the function fails (error() is called), the
9328 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9329 COND and SALS arrays and each of those arrays contents. */
9330
9331 static void
9332 create_breakpoints_sal (struct gdbarch *gdbarch,
9333 struct linespec_result *canonical,
9334 gdb::unique_xmalloc_ptr<char> cond_string,
9335 gdb::unique_xmalloc_ptr<char> extra_string,
9336 enum bptype type, enum bpdisp disposition,
9337 int thread, int task, int ignore_count,
9338 const struct breakpoint_ops *ops, int from_tty,
9339 int enabled, int internal, unsigned flags)
9340 {
9341 if (canonical->pre_expanded)
9342 gdb_assert (canonical->lsals.size () == 1);
9343
9344 for (const auto &lsal : canonical->lsals)
9345 {
9346 /* Note that 'location' can be NULL in the case of a plain
9347 'break', without arguments. */
9348 event_location_up location
9349 = (canonical->location != NULL
9350 ? copy_event_location (canonical->location.get ()) : NULL);
9351 gdb::unique_xmalloc_ptr<char> filter_string
9352 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
9353
9354 create_breakpoint_sal (gdbarch, lsal.sals,
9355 std::move (location),
9356 std::move (filter_string),
9357 std::move (cond_string),
9358 std::move (extra_string),
9359 type, disposition,
9360 thread, task, ignore_count, ops,
9361 from_tty, enabled, internal, flags,
9362 canonical->special_display);
9363 }
9364 }
9365
9366 /* Parse LOCATION which is assumed to be a SAL specification possibly
9367 followed by conditionals. On return, SALS contains an array of SAL
9368 addresses found. LOCATION points to the end of the SAL (for
9369 linespec locations).
9370
9371 The array and the line spec strings are allocated on the heap, it is
9372 the caller's responsibility to free them. */
9373
9374 static void
9375 parse_breakpoint_sals (const struct event_location *location,
9376 struct linespec_result *canonical)
9377 {
9378 struct symtab_and_line cursal;
9379
9380 if (event_location_type (location) == LINESPEC_LOCATION)
9381 {
9382 const char *address = get_linespec_location (location);
9383
9384 if (address == NULL)
9385 {
9386 /* The last displayed codepoint, if it's valid, is our default
9387 breakpoint address. */
9388 if (last_displayed_sal_is_valid ())
9389 {
9390 /* Set sal's pspace, pc, symtab, and line to the values
9391 corresponding to the last call to print_frame_info.
9392 Be sure to reinitialize LINE with NOTCURRENT == 0
9393 as the breakpoint line number is inappropriate otherwise.
9394 find_pc_line would adjust PC, re-set it back. */
9395 symtab_and_line sal = get_last_displayed_sal ();
9396 CORE_ADDR pc = sal.pc;
9397
9398 sal = find_pc_line (pc, 0);
9399
9400 /* "break" without arguments is equivalent to "break *PC"
9401 where PC is the last displayed codepoint's address. So
9402 make sure to set sal.explicit_pc to prevent GDB from
9403 trying to expand the list of sals to include all other
9404 instances with the same symtab and line. */
9405 sal.pc = pc;
9406 sal.explicit_pc = 1;
9407
9408 struct linespec_sals lsal;
9409 lsal.sals = {sal};
9410 lsal.canonical = NULL;
9411
9412 canonical->lsals.push_back (std::move (lsal));
9413 return;
9414 }
9415 else
9416 error (_("No default breakpoint address now."));
9417 }
9418 }
9419
9420 /* Force almost all breakpoints to be in terms of the
9421 current_source_symtab (which is decode_line_1's default).
9422 This should produce the results we want almost all of the
9423 time while leaving default_breakpoint_* alone.
9424
9425 ObjC: However, don't match an Objective-C method name which
9426 may have a '+' or '-' succeeded by a '['. */
9427 cursal = get_current_source_symtab_and_line ();
9428 if (last_displayed_sal_is_valid ())
9429 {
9430 const char *address = NULL;
9431
9432 if (event_location_type (location) == LINESPEC_LOCATION)
9433 address = get_linespec_location (location);
9434
9435 if (!cursal.symtab
9436 || (address != NULL
9437 && strchr ("+-", address[0]) != NULL
9438 && address[1] != '['))
9439 {
9440 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9441 get_last_displayed_symtab (),
9442 get_last_displayed_line (),
9443 canonical, NULL, NULL);
9444 return;
9445 }
9446 }
9447
9448 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9449 cursal.symtab, cursal.line, canonical, NULL, NULL);
9450 }
9451
9452
9453 /* Convert each SAL into a real PC. Verify that the PC can be
9454 inserted as a breakpoint. If it can't throw an error. */
9455
9456 static void
9457 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9458 {
9459 for (auto &sal : sals)
9460 resolve_sal_pc (&sal);
9461 }
9462
9463 /* Fast tracepoints may have restrictions on valid locations. For
9464 instance, a fast tracepoint using a jump instead of a trap will
9465 likely have to overwrite more bytes than a trap would, and so can
9466 only be placed where the instruction is longer than the jump, or a
9467 multi-instruction sequence does not have a jump into the middle of
9468 it, etc. */
9469
9470 static void
9471 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9472 gdb::array_view<const symtab_and_line> sals)
9473 {
9474 int rslt;
9475 char *msg;
9476 struct cleanup *old_chain;
9477
9478 for (const auto &sal : sals)
9479 {
9480 struct gdbarch *sarch;
9481
9482 sarch = get_sal_arch (sal);
9483 /* We fall back to GDBARCH if there is no architecture
9484 associated with SAL. */
9485 if (sarch == NULL)
9486 sarch = gdbarch;
9487 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
9488 old_chain = make_cleanup (xfree, msg);
9489
9490 if (!rslt)
9491 error (_("May not have a fast tracepoint at %s%s"),
9492 paddress (sarch, sal.pc), (msg ? msg : ""));
9493
9494 do_cleanups (old_chain);
9495 }
9496 }
9497
9498 /* Given TOK, a string specification of condition and thread, as
9499 accepted by the 'break' command, extract the condition
9500 string and thread number and set *COND_STRING and *THREAD.
9501 PC identifies the context at which the condition should be parsed.
9502 If no condition is found, *COND_STRING is set to NULL.
9503 If no thread is found, *THREAD is set to -1. */
9504
9505 static void
9506 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9507 char **cond_string, int *thread, int *task,
9508 char **rest)
9509 {
9510 *cond_string = NULL;
9511 *thread = -1;
9512 *task = 0;
9513 *rest = NULL;
9514
9515 while (tok && *tok)
9516 {
9517 const char *end_tok;
9518 int toklen;
9519 const char *cond_start = NULL;
9520 const char *cond_end = NULL;
9521
9522 tok = skip_spaces (tok);
9523
9524 if ((*tok == '"' || *tok == ',') && rest)
9525 {
9526 *rest = savestring (tok, strlen (tok));
9527 return;
9528 }
9529
9530 end_tok = skip_to_space (tok);
9531
9532 toklen = end_tok - tok;
9533
9534 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9535 {
9536 tok = cond_start = end_tok + 1;
9537 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9538 cond_end = tok;
9539 *cond_string = savestring (cond_start, cond_end - cond_start);
9540 }
9541 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9542 {
9543 const char *tmptok;
9544 struct thread_info *thr;
9545
9546 tok = end_tok + 1;
9547 thr = parse_thread_id (tok, &tmptok);
9548 if (tok == tmptok)
9549 error (_("Junk after thread keyword."));
9550 *thread = thr->global_num;
9551 tok = tmptok;
9552 }
9553 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9554 {
9555 char *tmptok;
9556
9557 tok = end_tok + 1;
9558 *task = strtol (tok, &tmptok, 0);
9559 if (tok == tmptok)
9560 error (_("Junk after task keyword."));
9561 if (!valid_task_id (*task))
9562 error (_("Unknown task %d."), *task);
9563 tok = tmptok;
9564 }
9565 else if (rest)
9566 {
9567 *rest = savestring (tok, strlen (tok));
9568 return;
9569 }
9570 else
9571 error (_("Junk at end of arguments."));
9572 }
9573 }
9574
9575 /* Decode a static tracepoint marker spec. */
9576
9577 static std::vector<symtab_and_line>
9578 decode_static_tracepoint_spec (const char **arg_p)
9579 {
9580 VEC(static_tracepoint_marker_p) *markers = NULL;
9581 struct cleanup *old_chain;
9582 const char *p = &(*arg_p)[3];
9583 const char *endp;
9584 char *marker_str;
9585 int i;
9586
9587 p = skip_spaces (p);
9588
9589 endp = skip_to_space (p);
9590
9591 marker_str = savestring (p, endp - p);
9592 old_chain = make_cleanup (xfree, marker_str);
9593
9594 markers = target_static_tracepoint_markers_by_strid (marker_str);
9595 if (VEC_empty(static_tracepoint_marker_p, markers))
9596 error (_("No known static tracepoint marker named %s"), marker_str);
9597
9598 std::vector<symtab_and_line> sals;
9599 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
9600
9601 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
9602 {
9603 struct static_tracepoint_marker *marker;
9604
9605 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9606
9607 symtab_and_line sal = find_pc_line (marker->address, 0);
9608 sal.pc = marker->address;
9609 sals.push_back (sal);
9610
9611 release_static_tracepoint_marker (marker);
9612 }
9613
9614 do_cleanups (old_chain);
9615
9616 *arg_p = endp;
9617 return sals;
9618 }
9619
9620 /* See breakpoint.h. */
9621
9622 int
9623 create_breakpoint (struct gdbarch *gdbarch,
9624 const struct event_location *location,
9625 const char *cond_string,
9626 int thread, const char *extra_string,
9627 int parse_extra,
9628 int tempflag, enum bptype type_wanted,
9629 int ignore_count,
9630 enum auto_boolean pending_break_support,
9631 const struct breakpoint_ops *ops,
9632 int from_tty, int enabled, int internal,
9633 unsigned flags)
9634 {
9635 struct linespec_result canonical;
9636 struct cleanup *bkpt_chain = NULL;
9637 int pending = 0;
9638 int task = 0;
9639 int prev_bkpt_count = breakpoint_count;
9640
9641 gdb_assert (ops != NULL);
9642
9643 /* If extra_string isn't useful, set it to NULL. */
9644 if (extra_string != NULL && *extra_string == '\0')
9645 extra_string = NULL;
9646
9647 TRY
9648 {
9649 ops->create_sals_from_location (location, &canonical, type_wanted);
9650 }
9651 CATCH (e, RETURN_MASK_ERROR)
9652 {
9653 /* If caller is interested in rc value from parse, set
9654 value. */
9655 if (e.error == NOT_FOUND_ERROR)
9656 {
9657 /* If pending breakpoint support is turned off, throw
9658 error. */
9659
9660 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9661 throw_exception (e);
9662
9663 exception_print (gdb_stderr, e);
9664
9665 /* If pending breakpoint support is auto query and the user
9666 selects no, then simply return the error code. */
9667 if (pending_break_support == AUTO_BOOLEAN_AUTO
9668 && !nquery (_("Make %s pending on future shared library load? "),
9669 bptype_string (type_wanted)))
9670 return 0;
9671
9672 /* At this point, either the user was queried about setting
9673 a pending breakpoint and selected yes, or pending
9674 breakpoint behavior is on and thus a pending breakpoint
9675 is defaulted on behalf of the user. */
9676 pending = 1;
9677 }
9678 else
9679 throw_exception (e);
9680 }
9681 END_CATCH
9682
9683 if (!pending && canonical.lsals.empty ())
9684 return 0;
9685
9686 /* ----------------------------- SNIP -----------------------------
9687 Anything added to the cleanup chain beyond this point is assumed
9688 to be part of a breakpoint. If the breakpoint create succeeds
9689 then the memory is not reclaimed. */
9690 bkpt_chain = make_cleanup (null_cleanup, 0);
9691
9692 /* Resolve all line numbers to PC's and verify that the addresses
9693 are ok for the target. */
9694 if (!pending)
9695 {
9696 for (auto &lsal : canonical.lsals)
9697 breakpoint_sals_to_pc (lsal.sals);
9698 }
9699
9700 /* Fast tracepoints may have additional restrictions on location. */
9701 if (!pending && type_wanted == bp_fast_tracepoint)
9702 {
9703 for (const auto &lsal : canonical.lsals)
9704 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9705 }
9706
9707 /* Verify that condition can be parsed, before setting any
9708 breakpoints. Allocate a separate condition expression for each
9709 breakpoint. */
9710 if (!pending)
9711 {
9712 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9713 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9714
9715 if (parse_extra)
9716 {
9717 char *rest;
9718 char *cond;
9719
9720 const linespec_sals &lsal = canonical.lsals[0];
9721
9722 /* Here we only parse 'arg' to separate condition
9723 from thread number, so parsing in context of first
9724 sal is OK. When setting the breakpoint we'll
9725 re-parse it in context of each sal. */
9726
9727 find_condition_and_thread (extra_string, lsal.sals[0].pc,
9728 &cond, &thread, &task, &rest);
9729 cond_string_copy.reset (cond);
9730 extra_string_copy.reset (rest);
9731 }
9732 else
9733 {
9734 if (type_wanted != bp_dprintf
9735 && extra_string != NULL && *extra_string != '\0')
9736 error (_("Garbage '%s' at end of location"), extra_string);
9737
9738 /* Create a private copy of condition string. */
9739 if (cond_string)
9740 cond_string_copy.reset (xstrdup (cond_string));
9741 /* Create a private copy of any extra string. */
9742 if (extra_string)
9743 extra_string_copy.reset (xstrdup (extra_string));
9744 }
9745
9746 ops->create_breakpoints_sal (gdbarch, &canonical,
9747 std::move (cond_string_copy),
9748 std::move (extra_string_copy),
9749 type_wanted,
9750 tempflag ? disp_del : disp_donttouch,
9751 thread, task, ignore_count, ops,
9752 from_tty, enabled, internal, flags);
9753 }
9754 else
9755 {
9756 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9757
9758 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9759 b->location = copy_event_location (location);
9760
9761 if (parse_extra)
9762 b->cond_string = NULL;
9763 else
9764 {
9765 /* Create a private copy of condition string. */
9766 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9767 b->thread = thread;
9768 }
9769
9770 /* Create a private copy of any extra string. */
9771 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9772 b->ignore_count = ignore_count;
9773 b->disposition = tempflag ? disp_del : disp_donttouch;
9774 b->condition_not_parsed = 1;
9775 b->enable_state = enabled ? bp_enabled : bp_disabled;
9776 if ((type_wanted != bp_breakpoint
9777 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9778 b->pspace = current_program_space;
9779
9780 install_breakpoint (internal, std::move (b), 0);
9781 }
9782
9783 if (canonical.lsals.size () > 1)
9784 {
9785 warning (_("Multiple breakpoints were set.\nUse the "
9786 "\"delete\" command to delete unwanted breakpoints."));
9787 prev_breakpoint_count = prev_bkpt_count;
9788 }
9789
9790 /* That's it. Discard the cleanups for data inserted into the
9791 breakpoint. */
9792 discard_cleanups (bkpt_chain);
9793
9794 /* error call may happen here - have BKPT_CHAIN already discarded. */
9795 update_global_location_list (UGLL_MAY_INSERT);
9796
9797 return 1;
9798 }
9799
9800 /* Set a breakpoint.
9801 ARG is a string describing breakpoint address,
9802 condition, and thread.
9803 FLAG specifies if a breakpoint is hardware on,
9804 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9805 and BP_TEMPFLAG. */
9806
9807 static void
9808 break_command_1 (char *arg, int flag, int from_tty)
9809 {
9810 int tempflag = flag & BP_TEMPFLAG;
9811 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9812 ? bp_hardware_breakpoint
9813 : bp_breakpoint);
9814 struct breakpoint_ops *ops;
9815
9816 event_location_up location = string_to_event_location (&arg, current_language);
9817
9818 /* Matching breakpoints on probes. */
9819 if (location != NULL
9820 && event_location_type (location.get ()) == PROBE_LOCATION)
9821 ops = &bkpt_probe_breakpoint_ops;
9822 else
9823 ops = &bkpt_breakpoint_ops;
9824
9825 create_breakpoint (get_current_arch (),
9826 location.get (),
9827 NULL, 0, arg, 1 /* parse arg */,
9828 tempflag, type_wanted,
9829 0 /* Ignore count */,
9830 pending_break_support,
9831 ops,
9832 from_tty,
9833 1 /* enabled */,
9834 0 /* internal */,
9835 0);
9836 }
9837
9838 /* Helper function for break_command_1 and disassemble_command. */
9839
9840 void
9841 resolve_sal_pc (struct symtab_and_line *sal)
9842 {
9843 CORE_ADDR pc;
9844
9845 if (sal->pc == 0 && sal->symtab != NULL)
9846 {
9847 if (!find_line_pc (sal->symtab, sal->line, &pc))
9848 error (_("No line %d in file \"%s\"."),
9849 sal->line, symtab_to_filename_for_display (sal->symtab));
9850 sal->pc = pc;
9851
9852 /* If this SAL corresponds to a breakpoint inserted using a line
9853 number, then skip the function prologue if necessary. */
9854 if (sal->explicit_line)
9855 skip_prologue_sal (sal);
9856 }
9857
9858 if (sal->section == 0 && sal->symtab != NULL)
9859 {
9860 const struct blockvector *bv;
9861 const struct block *b;
9862 struct symbol *sym;
9863
9864 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9865 SYMTAB_COMPUNIT (sal->symtab));
9866 if (bv != NULL)
9867 {
9868 sym = block_linkage_function (b);
9869 if (sym != NULL)
9870 {
9871 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9872 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9873 sym);
9874 }
9875 else
9876 {
9877 /* It really is worthwhile to have the section, so we'll
9878 just have to look harder. This case can be executed
9879 if we have line numbers but no functions (as can
9880 happen in assembly source). */
9881
9882 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9883 switch_to_program_space_and_thread (sal->pspace);
9884
9885 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9886 if (msym.minsym)
9887 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9888 }
9889 }
9890 }
9891 }
9892
9893 void
9894 break_command (char *arg, int from_tty)
9895 {
9896 break_command_1 (arg, 0, from_tty);
9897 }
9898
9899 void
9900 tbreak_command (char *arg, int from_tty)
9901 {
9902 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9903 }
9904
9905 static void
9906 hbreak_command (char *arg, int from_tty)
9907 {
9908 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9909 }
9910
9911 static void
9912 thbreak_command (char *arg, int from_tty)
9913 {
9914 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9915 }
9916
9917 static void
9918 stop_command (char *arg, int from_tty)
9919 {
9920 printf_filtered (_("Specify the type of breakpoint to set.\n\
9921 Usage: stop in <function | address>\n\
9922 stop at <line>\n"));
9923 }
9924
9925 static void
9926 stopin_command (char *arg, int from_tty)
9927 {
9928 int badInput = 0;
9929
9930 if (arg == (char *) NULL)
9931 badInput = 1;
9932 else if (*arg != '*')
9933 {
9934 char *argptr = arg;
9935 int hasColon = 0;
9936
9937 /* Look for a ':'. If this is a line number specification, then
9938 say it is bad, otherwise, it should be an address or
9939 function/method name. */
9940 while (*argptr && !hasColon)
9941 {
9942 hasColon = (*argptr == ':');
9943 argptr++;
9944 }
9945
9946 if (hasColon)
9947 badInput = (*argptr != ':'); /* Not a class::method */
9948 else
9949 badInput = isdigit (*arg); /* a simple line number */
9950 }
9951
9952 if (badInput)
9953 printf_filtered (_("Usage: stop in <function | address>\n"));
9954 else
9955 break_command_1 (arg, 0, from_tty);
9956 }
9957
9958 static void
9959 stopat_command (char *arg, int from_tty)
9960 {
9961 int badInput = 0;
9962
9963 if (arg == (char *) NULL || *arg == '*') /* no line number */
9964 badInput = 1;
9965 else
9966 {
9967 char *argptr = arg;
9968 int hasColon = 0;
9969
9970 /* Look for a ':'. If there is a '::' then get out, otherwise
9971 it is probably a line number. */
9972 while (*argptr && !hasColon)
9973 {
9974 hasColon = (*argptr == ':');
9975 argptr++;
9976 }
9977
9978 if (hasColon)
9979 badInput = (*argptr == ':'); /* we have class::method */
9980 else
9981 badInput = !isdigit (*arg); /* not a line number */
9982 }
9983
9984 if (badInput)
9985 printf_filtered (_("Usage: stop at <line>\n"));
9986 else
9987 break_command_1 (arg, 0, from_tty);
9988 }
9989
9990 /* The dynamic printf command is mostly like a regular breakpoint, but
9991 with a prewired command list consisting of a single output command,
9992 built from extra arguments supplied on the dprintf command
9993 line. */
9994
9995 static void
9996 dprintf_command (char *arg, int from_tty)
9997 {
9998 event_location_up location = string_to_event_location (&arg, current_language);
9999
10000 /* If non-NULL, ARG should have been advanced past the location;
10001 the next character must be ','. */
10002 if (arg != NULL)
10003 {
10004 if (arg[0] != ',' || arg[1] == '\0')
10005 error (_("Format string required"));
10006 else
10007 {
10008 /* Skip the comma. */
10009 ++arg;
10010 }
10011 }
10012
10013 create_breakpoint (get_current_arch (),
10014 location.get (),
10015 NULL, 0, arg, 1 /* parse arg */,
10016 0, bp_dprintf,
10017 0 /* Ignore count */,
10018 pending_break_support,
10019 &dprintf_breakpoint_ops,
10020 from_tty,
10021 1 /* enabled */,
10022 0 /* internal */,
10023 0);
10024 }
10025
10026 static void
10027 agent_printf_command (char *arg, int from_tty)
10028 {
10029 error (_("May only run agent-printf on the target"));
10030 }
10031
10032 /* Implement the "breakpoint_hit" breakpoint_ops method for
10033 ranged breakpoints. */
10034
10035 static int
10036 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10037 struct address_space *aspace,
10038 CORE_ADDR bp_addr,
10039 const struct target_waitstatus *ws)
10040 {
10041 if (ws->kind != TARGET_WAITKIND_STOPPED
10042 || ws->value.sig != GDB_SIGNAL_TRAP)
10043 return 0;
10044
10045 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10046 bl->length, aspace, bp_addr);
10047 }
10048
10049 /* Implement the "resources_needed" breakpoint_ops method for
10050 ranged breakpoints. */
10051
10052 static int
10053 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10054 {
10055 return target_ranged_break_num_registers ();
10056 }
10057
10058 /* Implement the "print_it" breakpoint_ops method for
10059 ranged breakpoints. */
10060
10061 static enum print_stop_action
10062 print_it_ranged_breakpoint (bpstat bs)
10063 {
10064 struct breakpoint *b = bs->breakpoint_at;
10065 struct bp_location *bl = b->loc;
10066 struct ui_out *uiout = current_uiout;
10067
10068 gdb_assert (b->type == bp_hardware_breakpoint);
10069
10070 /* Ranged breakpoints have only one location. */
10071 gdb_assert (bl && bl->next == NULL);
10072
10073 annotate_breakpoint (b->number);
10074
10075 maybe_print_thread_hit_breakpoint (uiout);
10076
10077 if (b->disposition == disp_del)
10078 uiout->text ("Temporary ranged breakpoint ");
10079 else
10080 uiout->text ("Ranged breakpoint ");
10081 if (uiout->is_mi_like_p ())
10082 {
10083 uiout->field_string ("reason",
10084 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10085 uiout->field_string ("disp", bpdisp_text (b->disposition));
10086 }
10087 uiout->field_int ("bkptno", b->number);
10088 uiout->text (", ");
10089
10090 return PRINT_SRC_AND_LOC;
10091 }
10092
10093 /* Implement the "print_one" breakpoint_ops method for
10094 ranged breakpoints. */
10095
10096 static void
10097 print_one_ranged_breakpoint (struct breakpoint *b,
10098 struct bp_location **last_loc)
10099 {
10100 struct bp_location *bl = b->loc;
10101 struct value_print_options opts;
10102 struct ui_out *uiout = current_uiout;
10103
10104 /* Ranged breakpoints have only one location. */
10105 gdb_assert (bl && bl->next == NULL);
10106
10107 get_user_print_options (&opts);
10108
10109 if (opts.addressprint)
10110 /* We don't print the address range here, it will be printed later
10111 by print_one_detail_ranged_breakpoint. */
10112 uiout->field_skip ("addr");
10113 annotate_field (5);
10114 print_breakpoint_location (b, bl);
10115 *last_loc = bl;
10116 }
10117
10118 /* Implement the "print_one_detail" breakpoint_ops method for
10119 ranged breakpoints. */
10120
10121 static void
10122 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10123 struct ui_out *uiout)
10124 {
10125 CORE_ADDR address_start, address_end;
10126 struct bp_location *bl = b->loc;
10127 string_file stb;
10128
10129 gdb_assert (bl);
10130
10131 address_start = bl->address;
10132 address_end = address_start + bl->length - 1;
10133
10134 uiout->text ("\taddress range: ");
10135 stb.printf ("[%s, %s]",
10136 print_core_address (bl->gdbarch, address_start),
10137 print_core_address (bl->gdbarch, address_end));
10138 uiout->field_stream ("addr", stb);
10139 uiout->text ("\n");
10140 }
10141
10142 /* Implement the "print_mention" breakpoint_ops method for
10143 ranged breakpoints. */
10144
10145 static void
10146 print_mention_ranged_breakpoint (struct breakpoint *b)
10147 {
10148 struct bp_location *bl = b->loc;
10149 struct ui_out *uiout = current_uiout;
10150
10151 gdb_assert (bl);
10152 gdb_assert (b->type == bp_hardware_breakpoint);
10153
10154 if (uiout->is_mi_like_p ())
10155 return;
10156
10157 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10158 b->number, paddress (bl->gdbarch, bl->address),
10159 paddress (bl->gdbarch, bl->address + bl->length - 1));
10160 }
10161
10162 /* Implement the "print_recreate" breakpoint_ops method for
10163 ranged breakpoints. */
10164
10165 static void
10166 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10167 {
10168 fprintf_unfiltered (fp, "break-range %s, %s",
10169 event_location_to_string (b->location.get ()),
10170 event_location_to_string (b->location_range_end.get ()));
10171 print_recreate_thread (b, fp);
10172 }
10173
10174 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10175
10176 static struct breakpoint_ops ranged_breakpoint_ops;
10177
10178 /* Find the address where the end of the breakpoint range should be
10179 placed, given the SAL of the end of the range. This is so that if
10180 the user provides a line number, the end of the range is set to the
10181 last instruction of the given line. */
10182
10183 static CORE_ADDR
10184 find_breakpoint_range_end (struct symtab_and_line sal)
10185 {
10186 CORE_ADDR end;
10187
10188 /* If the user provided a PC value, use it. Otherwise,
10189 find the address of the end of the given location. */
10190 if (sal.explicit_pc)
10191 end = sal.pc;
10192 else
10193 {
10194 int ret;
10195 CORE_ADDR start;
10196
10197 ret = find_line_pc_range (sal, &start, &end);
10198 if (!ret)
10199 error (_("Could not find location of the end of the range."));
10200
10201 /* find_line_pc_range returns the start of the next line. */
10202 end--;
10203 }
10204
10205 return end;
10206 }
10207
10208 /* Implement the "break-range" CLI command. */
10209
10210 static void
10211 break_range_command (char *arg, int from_tty)
10212 {
10213 char *arg_start, *addr_string_start;
10214 struct linespec_result canonical_start, canonical_end;
10215 int bp_count, can_use_bp, length;
10216 CORE_ADDR end;
10217 struct breakpoint *b;
10218 struct cleanup *cleanup_bkpt;
10219
10220 /* We don't support software ranged breakpoints. */
10221 if (target_ranged_break_num_registers () < 0)
10222 error (_("This target does not support hardware ranged breakpoints."));
10223
10224 bp_count = hw_breakpoint_used_count ();
10225 bp_count += target_ranged_break_num_registers ();
10226 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10227 bp_count, 0);
10228 if (can_use_bp < 0)
10229 error (_("Hardware breakpoints used exceeds limit."));
10230
10231 arg = skip_spaces (arg);
10232 if (arg == NULL || arg[0] == '\0')
10233 error(_("No address range specified."));
10234
10235 arg_start = arg;
10236 event_location_up start_location = string_to_event_location (&arg,
10237 current_language);
10238 parse_breakpoint_sals (start_location.get (), &canonical_start);
10239
10240 if (arg[0] != ',')
10241 error (_("Too few arguments."));
10242 else if (canonical_start.lsals.empty ())
10243 error (_("Could not find location of the beginning of the range."));
10244
10245 const linespec_sals &lsal_start = canonical_start.lsals[0];
10246
10247 if (canonical_start.lsals.size () > 1
10248 || lsal_start.sals.size () != 1)
10249 error (_("Cannot create a ranged breakpoint with multiple locations."));
10250
10251 const symtab_and_line &sal_start = lsal_start.sals[0];
10252 addr_string_start = savestring (arg_start, arg - arg_start);
10253 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10254
10255 arg++; /* Skip the comma. */
10256 arg = skip_spaces (arg);
10257
10258 /* Parse the end location. */
10259
10260 arg_start = arg;
10261
10262 /* We call decode_line_full directly here instead of using
10263 parse_breakpoint_sals because we need to specify the start location's
10264 symtab and line as the default symtab and line for the end of the
10265 range. This makes it possible to have ranges like "foo.c:27, +14",
10266 where +14 means 14 lines from the start location. */
10267 event_location_up end_location = string_to_event_location (&arg,
10268 current_language);
10269 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10270 sal_start.symtab, sal_start.line,
10271 &canonical_end, NULL, NULL);
10272
10273 if (canonical_end.lsals.empty ())
10274 error (_("Could not find location of the end of the range."));
10275
10276 const linespec_sals &lsal_end = canonical_end.lsals[0];
10277 if (canonical_end.lsals.size () > 1
10278 || lsal_end.sals.size () != 1)
10279 error (_("Cannot create a ranged breakpoint with multiple locations."));
10280
10281 const symtab_and_line &sal_end = lsal_end.sals[0];
10282
10283 end = find_breakpoint_range_end (sal_end);
10284 if (sal_start.pc > end)
10285 error (_("Invalid address range, end precedes start."));
10286
10287 length = end - sal_start.pc + 1;
10288 if (length < 0)
10289 /* Length overflowed. */
10290 error (_("Address range too large."));
10291 else if (length == 1)
10292 {
10293 /* This range is simple enough to be handled by
10294 the `hbreak' command. */
10295 hbreak_command (addr_string_start, 1);
10296
10297 do_cleanups (cleanup_bkpt);
10298
10299 return;
10300 }
10301
10302 /* Now set up the breakpoint. */
10303 b = set_raw_breakpoint (get_current_arch (), sal_start,
10304 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10305 set_breakpoint_count (breakpoint_count + 1);
10306 b->number = breakpoint_count;
10307 b->disposition = disp_donttouch;
10308 b->location = std::move (start_location);
10309 b->location_range_end = std::move (end_location);
10310 b->loc->length = length;
10311
10312 do_cleanups (cleanup_bkpt);
10313
10314 mention (b);
10315 observer_notify_breakpoint_created (b);
10316 update_global_location_list (UGLL_MAY_INSERT);
10317 }
10318
10319 /* Return non-zero if EXP is verified as constant. Returned zero
10320 means EXP is variable. Also the constant detection may fail for
10321 some constant expressions and in such case still falsely return
10322 zero. */
10323
10324 static int
10325 watchpoint_exp_is_const (const struct expression *exp)
10326 {
10327 int i = exp->nelts;
10328
10329 while (i > 0)
10330 {
10331 int oplenp, argsp;
10332
10333 /* We are only interested in the descriptor of each element. */
10334 operator_length (exp, i, &oplenp, &argsp);
10335 i -= oplenp;
10336
10337 switch (exp->elts[i].opcode)
10338 {
10339 case BINOP_ADD:
10340 case BINOP_SUB:
10341 case BINOP_MUL:
10342 case BINOP_DIV:
10343 case BINOP_REM:
10344 case BINOP_MOD:
10345 case BINOP_LSH:
10346 case BINOP_RSH:
10347 case BINOP_LOGICAL_AND:
10348 case BINOP_LOGICAL_OR:
10349 case BINOP_BITWISE_AND:
10350 case BINOP_BITWISE_IOR:
10351 case BINOP_BITWISE_XOR:
10352 case BINOP_EQUAL:
10353 case BINOP_NOTEQUAL:
10354 case BINOP_LESS:
10355 case BINOP_GTR:
10356 case BINOP_LEQ:
10357 case BINOP_GEQ:
10358 case BINOP_REPEAT:
10359 case BINOP_COMMA:
10360 case BINOP_EXP:
10361 case BINOP_MIN:
10362 case BINOP_MAX:
10363 case BINOP_INTDIV:
10364 case BINOP_CONCAT:
10365 case TERNOP_COND:
10366 case TERNOP_SLICE:
10367
10368 case OP_LONG:
10369 case OP_DOUBLE:
10370 case OP_DECFLOAT:
10371 case OP_LAST:
10372 case OP_COMPLEX:
10373 case OP_STRING:
10374 case OP_ARRAY:
10375 case OP_TYPE:
10376 case OP_TYPEOF:
10377 case OP_DECLTYPE:
10378 case OP_TYPEID:
10379 case OP_NAME:
10380 case OP_OBJC_NSSTRING:
10381
10382 case UNOP_NEG:
10383 case UNOP_LOGICAL_NOT:
10384 case UNOP_COMPLEMENT:
10385 case UNOP_ADDR:
10386 case UNOP_HIGH:
10387 case UNOP_CAST:
10388
10389 case UNOP_CAST_TYPE:
10390 case UNOP_REINTERPRET_CAST:
10391 case UNOP_DYNAMIC_CAST:
10392 /* Unary, binary and ternary operators: We have to check
10393 their operands. If they are constant, then so is the
10394 result of that operation. For instance, if A and B are
10395 determined to be constants, then so is "A + B".
10396
10397 UNOP_IND is one exception to the rule above, because the
10398 value of *ADDR is not necessarily a constant, even when
10399 ADDR is. */
10400 break;
10401
10402 case OP_VAR_VALUE:
10403 /* Check whether the associated symbol is a constant.
10404
10405 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10406 possible that a buggy compiler could mark a variable as
10407 constant even when it is not, and TYPE_CONST would return
10408 true in this case, while SYMBOL_CLASS wouldn't.
10409
10410 We also have to check for function symbols because they
10411 are always constant. */
10412 {
10413 struct symbol *s = exp->elts[i + 2].symbol;
10414
10415 if (SYMBOL_CLASS (s) != LOC_BLOCK
10416 && SYMBOL_CLASS (s) != LOC_CONST
10417 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10418 return 0;
10419 break;
10420 }
10421
10422 /* The default action is to return 0 because we are using
10423 the optimistic approach here: If we don't know something,
10424 then it is not a constant. */
10425 default:
10426 return 0;
10427 }
10428 }
10429
10430 return 1;
10431 }
10432
10433 /* Watchpoint destructor. */
10434
10435 watchpoint::~watchpoint ()
10436 {
10437 xfree (this->exp_string);
10438 xfree (this->exp_string_reparse);
10439 value_free (this->val);
10440 }
10441
10442 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10443
10444 static void
10445 re_set_watchpoint (struct breakpoint *b)
10446 {
10447 struct watchpoint *w = (struct watchpoint *) b;
10448
10449 /* Watchpoint can be either on expression using entirely global
10450 variables, or it can be on local variables.
10451
10452 Watchpoints of the first kind are never auto-deleted, and even
10453 persist across program restarts. Since they can use variables
10454 from shared libraries, we need to reparse expression as libraries
10455 are loaded and unloaded.
10456
10457 Watchpoints on local variables can also change meaning as result
10458 of solib event. For example, if a watchpoint uses both a local
10459 and a global variables in expression, it's a local watchpoint,
10460 but unloading of a shared library will make the expression
10461 invalid. This is not a very common use case, but we still
10462 re-evaluate expression, to avoid surprises to the user.
10463
10464 Note that for local watchpoints, we re-evaluate it only if
10465 watchpoints frame id is still valid. If it's not, it means the
10466 watchpoint is out of scope and will be deleted soon. In fact,
10467 I'm not sure we'll ever be called in this case.
10468
10469 If a local watchpoint's frame id is still valid, then
10470 w->exp_valid_block is likewise valid, and we can safely use it.
10471
10472 Don't do anything about disabled watchpoints, since they will be
10473 reevaluated again when enabled. */
10474 update_watchpoint (w, 1 /* reparse */);
10475 }
10476
10477 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10478
10479 static int
10480 insert_watchpoint (struct bp_location *bl)
10481 {
10482 struct watchpoint *w = (struct watchpoint *) bl->owner;
10483 int length = w->exact ? 1 : bl->length;
10484
10485 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10486 w->cond_exp.get ());
10487 }
10488
10489 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10490
10491 static int
10492 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10493 {
10494 struct watchpoint *w = (struct watchpoint *) bl->owner;
10495 int length = w->exact ? 1 : bl->length;
10496
10497 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10498 w->cond_exp.get ());
10499 }
10500
10501 static int
10502 breakpoint_hit_watchpoint (const struct bp_location *bl,
10503 struct address_space *aspace, CORE_ADDR bp_addr,
10504 const struct target_waitstatus *ws)
10505 {
10506 struct breakpoint *b = bl->owner;
10507 struct watchpoint *w = (struct watchpoint *) b;
10508
10509 /* Continuable hardware watchpoints are treated as non-existent if the
10510 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10511 some data address). Otherwise gdb won't stop on a break instruction
10512 in the code (not from a breakpoint) when a hardware watchpoint has
10513 been defined. Also skip watchpoints which we know did not trigger
10514 (did not match the data address). */
10515 if (is_hardware_watchpoint (b)
10516 && w->watchpoint_triggered == watch_triggered_no)
10517 return 0;
10518
10519 return 1;
10520 }
10521
10522 static void
10523 check_status_watchpoint (bpstat bs)
10524 {
10525 gdb_assert (is_watchpoint (bs->breakpoint_at));
10526
10527 bpstat_check_watchpoint (bs);
10528 }
10529
10530 /* Implement the "resources_needed" breakpoint_ops method for
10531 hardware watchpoints. */
10532
10533 static int
10534 resources_needed_watchpoint (const struct bp_location *bl)
10535 {
10536 struct watchpoint *w = (struct watchpoint *) bl->owner;
10537 int length = w->exact? 1 : bl->length;
10538
10539 return target_region_ok_for_hw_watchpoint (bl->address, length);
10540 }
10541
10542 /* Implement the "works_in_software_mode" breakpoint_ops method for
10543 hardware watchpoints. */
10544
10545 static int
10546 works_in_software_mode_watchpoint (const struct breakpoint *b)
10547 {
10548 /* Read and access watchpoints only work with hardware support. */
10549 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10550 }
10551
10552 static enum print_stop_action
10553 print_it_watchpoint (bpstat bs)
10554 {
10555 struct breakpoint *b;
10556 enum print_stop_action result;
10557 struct watchpoint *w;
10558 struct ui_out *uiout = current_uiout;
10559
10560 gdb_assert (bs->bp_location_at != NULL);
10561
10562 b = bs->breakpoint_at;
10563 w = (struct watchpoint *) b;
10564
10565 annotate_watchpoint (b->number);
10566 maybe_print_thread_hit_breakpoint (uiout);
10567
10568 string_file stb;
10569
10570 gdb::optional<ui_out_emit_tuple> tuple_emitter;
10571 switch (b->type)
10572 {
10573 case bp_watchpoint:
10574 case bp_hardware_watchpoint:
10575 if (uiout->is_mi_like_p ())
10576 uiout->field_string
10577 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10578 mention (b);
10579 tuple_emitter.emplace (uiout, "value");
10580 uiout->text ("\nOld value = ");
10581 watchpoint_value_print (bs->old_val, &stb);
10582 uiout->field_stream ("old", stb);
10583 uiout->text ("\nNew value = ");
10584 watchpoint_value_print (w->val, &stb);
10585 uiout->field_stream ("new", stb);
10586 uiout->text ("\n");
10587 /* More than one watchpoint may have been triggered. */
10588 result = PRINT_UNKNOWN;
10589 break;
10590
10591 case bp_read_watchpoint:
10592 if (uiout->is_mi_like_p ())
10593 uiout->field_string
10594 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10595 mention (b);
10596 tuple_emitter.emplace (uiout, "value");
10597 uiout->text ("\nValue = ");
10598 watchpoint_value_print (w->val, &stb);
10599 uiout->field_stream ("value", stb);
10600 uiout->text ("\n");
10601 result = PRINT_UNKNOWN;
10602 break;
10603
10604 case bp_access_watchpoint:
10605 if (bs->old_val != NULL)
10606 {
10607 if (uiout->is_mi_like_p ())
10608 uiout->field_string
10609 ("reason",
10610 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10611 mention (b);
10612 tuple_emitter.emplace (uiout, "value");
10613 uiout->text ("\nOld value = ");
10614 watchpoint_value_print (bs->old_val, &stb);
10615 uiout->field_stream ("old", stb);
10616 uiout->text ("\nNew value = ");
10617 }
10618 else
10619 {
10620 mention (b);
10621 if (uiout->is_mi_like_p ())
10622 uiout->field_string
10623 ("reason",
10624 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10625 tuple_emitter.emplace (uiout, "value");
10626 uiout->text ("\nValue = ");
10627 }
10628 watchpoint_value_print (w->val, &stb);
10629 uiout->field_stream ("new", stb);
10630 uiout->text ("\n");
10631 result = PRINT_UNKNOWN;
10632 break;
10633 default:
10634 result = PRINT_UNKNOWN;
10635 }
10636
10637 return result;
10638 }
10639
10640 /* Implement the "print_mention" breakpoint_ops method for hardware
10641 watchpoints. */
10642
10643 static void
10644 print_mention_watchpoint (struct breakpoint *b)
10645 {
10646 struct watchpoint *w = (struct watchpoint *) b;
10647 struct ui_out *uiout = current_uiout;
10648 const char *tuple_name;
10649
10650 switch (b->type)
10651 {
10652 case bp_watchpoint:
10653 uiout->text ("Watchpoint ");
10654 tuple_name = "wpt";
10655 break;
10656 case bp_hardware_watchpoint:
10657 uiout->text ("Hardware watchpoint ");
10658 tuple_name = "wpt";
10659 break;
10660 case bp_read_watchpoint:
10661 uiout->text ("Hardware read watchpoint ");
10662 tuple_name = "hw-rwpt";
10663 break;
10664 case bp_access_watchpoint:
10665 uiout->text ("Hardware access (read/write) watchpoint ");
10666 tuple_name = "hw-awpt";
10667 break;
10668 default:
10669 internal_error (__FILE__, __LINE__,
10670 _("Invalid hardware watchpoint type."));
10671 }
10672
10673 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10674 uiout->field_int ("number", b->number);
10675 uiout->text (": ");
10676 uiout->field_string ("exp", w->exp_string);
10677 }
10678
10679 /* Implement the "print_recreate" breakpoint_ops method for
10680 watchpoints. */
10681
10682 static void
10683 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10684 {
10685 struct watchpoint *w = (struct watchpoint *) b;
10686
10687 switch (b->type)
10688 {
10689 case bp_watchpoint:
10690 case bp_hardware_watchpoint:
10691 fprintf_unfiltered (fp, "watch");
10692 break;
10693 case bp_read_watchpoint:
10694 fprintf_unfiltered (fp, "rwatch");
10695 break;
10696 case bp_access_watchpoint:
10697 fprintf_unfiltered (fp, "awatch");
10698 break;
10699 default:
10700 internal_error (__FILE__, __LINE__,
10701 _("Invalid watchpoint type."));
10702 }
10703
10704 fprintf_unfiltered (fp, " %s", w->exp_string);
10705 print_recreate_thread (b, fp);
10706 }
10707
10708 /* Implement the "explains_signal" breakpoint_ops method for
10709 watchpoints. */
10710
10711 static int
10712 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10713 {
10714 /* A software watchpoint cannot cause a signal other than
10715 GDB_SIGNAL_TRAP. */
10716 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10717 return 0;
10718
10719 return 1;
10720 }
10721
10722 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10723
10724 static struct breakpoint_ops watchpoint_breakpoint_ops;
10725
10726 /* Implement the "insert" breakpoint_ops method for
10727 masked hardware watchpoints. */
10728
10729 static int
10730 insert_masked_watchpoint (struct bp_location *bl)
10731 {
10732 struct watchpoint *w = (struct watchpoint *) bl->owner;
10733
10734 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10735 bl->watchpoint_type);
10736 }
10737
10738 /* Implement the "remove" breakpoint_ops method for
10739 masked hardware watchpoints. */
10740
10741 static int
10742 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10743 {
10744 struct watchpoint *w = (struct watchpoint *) bl->owner;
10745
10746 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10747 bl->watchpoint_type);
10748 }
10749
10750 /* Implement the "resources_needed" breakpoint_ops method for
10751 masked hardware watchpoints. */
10752
10753 static int
10754 resources_needed_masked_watchpoint (const struct bp_location *bl)
10755 {
10756 struct watchpoint *w = (struct watchpoint *) bl->owner;
10757
10758 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10759 }
10760
10761 /* Implement the "works_in_software_mode" breakpoint_ops method for
10762 masked hardware watchpoints. */
10763
10764 static int
10765 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10766 {
10767 return 0;
10768 }
10769
10770 /* Implement the "print_it" breakpoint_ops method for
10771 masked hardware watchpoints. */
10772
10773 static enum print_stop_action
10774 print_it_masked_watchpoint (bpstat bs)
10775 {
10776 struct breakpoint *b = bs->breakpoint_at;
10777 struct ui_out *uiout = current_uiout;
10778
10779 /* Masked watchpoints have only one location. */
10780 gdb_assert (b->loc && b->loc->next == NULL);
10781
10782 annotate_watchpoint (b->number);
10783 maybe_print_thread_hit_breakpoint (uiout);
10784
10785 switch (b->type)
10786 {
10787 case bp_hardware_watchpoint:
10788 if (uiout->is_mi_like_p ())
10789 uiout->field_string
10790 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10791 break;
10792
10793 case bp_read_watchpoint:
10794 if (uiout->is_mi_like_p ())
10795 uiout->field_string
10796 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10797 break;
10798
10799 case bp_access_watchpoint:
10800 if (uiout->is_mi_like_p ())
10801 uiout->field_string
10802 ("reason",
10803 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10804 break;
10805 default:
10806 internal_error (__FILE__, __LINE__,
10807 _("Invalid hardware watchpoint type."));
10808 }
10809
10810 mention (b);
10811 uiout->text (_("\n\
10812 Check the underlying instruction at PC for the memory\n\
10813 address and value which triggered this watchpoint.\n"));
10814 uiout->text ("\n");
10815
10816 /* More than one watchpoint may have been triggered. */
10817 return PRINT_UNKNOWN;
10818 }
10819
10820 /* Implement the "print_one_detail" breakpoint_ops method for
10821 masked hardware watchpoints. */
10822
10823 static void
10824 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10825 struct ui_out *uiout)
10826 {
10827 struct watchpoint *w = (struct watchpoint *) b;
10828
10829 /* Masked watchpoints have only one location. */
10830 gdb_assert (b->loc && b->loc->next == NULL);
10831
10832 uiout->text ("\tmask ");
10833 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10834 uiout->text ("\n");
10835 }
10836
10837 /* Implement the "print_mention" breakpoint_ops method for
10838 masked hardware watchpoints. */
10839
10840 static void
10841 print_mention_masked_watchpoint (struct breakpoint *b)
10842 {
10843 struct watchpoint *w = (struct watchpoint *) b;
10844 struct ui_out *uiout = current_uiout;
10845 const char *tuple_name;
10846
10847 switch (b->type)
10848 {
10849 case bp_hardware_watchpoint:
10850 uiout->text ("Masked hardware watchpoint ");
10851 tuple_name = "wpt";
10852 break;
10853 case bp_read_watchpoint:
10854 uiout->text ("Masked hardware read watchpoint ");
10855 tuple_name = "hw-rwpt";
10856 break;
10857 case bp_access_watchpoint:
10858 uiout->text ("Masked hardware access (read/write) watchpoint ");
10859 tuple_name = "hw-awpt";
10860 break;
10861 default:
10862 internal_error (__FILE__, __LINE__,
10863 _("Invalid hardware watchpoint type."));
10864 }
10865
10866 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10867 uiout->field_int ("number", b->number);
10868 uiout->text (": ");
10869 uiout->field_string ("exp", w->exp_string);
10870 }
10871
10872 /* Implement the "print_recreate" breakpoint_ops method for
10873 masked hardware watchpoints. */
10874
10875 static void
10876 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10877 {
10878 struct watchpoint *w = (struct watchpoint *) b;
10879 char tmp[40];
10880
10881 switch (b->type)
10882 {
10883 case bp_hardware_watchpoint:
10884 fprintf_unfiltered (fp, "watch");
10885 break;
10886 case bp_read_watchpoint:
10887 fprintf_unfiltered (fp, "rwatch");
10888 break;
10889 case bp_access_watchpoint:
10890 fprintf_unfiltered (fp, "awatch");
10891 break;
10892 default:
10893 internal_error (__FILE__, __LINE__,
10894 _("Invalid hardware watchpoint type."));
10895 }
10896
10897 sprintf_vma (tmp, w->hw_wp_mask);
10898 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10899 print_recreate_thread (b, fp);
10900 }
10901
10902 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10903
10904 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10905
10906 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10907
10908 static int
10909 is_masked_watchpoint (const struct breakpoint *b)
10910 {
10911 return b->ops == &masked_watchpoint_breakpoint_ops;
10912 }
10913
10914 /* accessflag: hw_write: watch write,
10915 hw_read: watch read,
10916 hw_access: watch access (read or write) */
10917 static void
10918 watch_command_1 (const char *arg, int accessflag, int from_tty,
10919 int just_location, int internal)
10920 {
10921 struct breakpoint *scope_breakpoint = NULL;
10922 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10923 struct value *val, *mark, *result;
10924 int saved_bitpos = 0, saved_bitsize = 0;
10925 const char *exp_start = NULL;
10926 const char *exp_end = NULL;
10927 const char *tok, *end_tok;
10928 int toklen = -1;
10929 const char *cond_start = NULL;
10930 const char *cond_end = NULL;
10931 enum bptype bp_type;
10932 int thread = -1;
10933 int pc = 0;
10934 /* Flag to indicate whether we are going to use masks for
10935 the hardware watchpoint. */
10936 int use_mask = 0;
10937 CORE_ADDR mask = 0;
10938 char *expression;
10939 struct cleanup *back_to;
10940
10941 /* Make sure that we actually have parameters to parse. */
10942 if (arg != NULL && arg[0] != '\0')
10943 {
10944 const char *value_start;
10945
10946 exp_end = arg + strlen (arg);
10947
10948 /* Look for "parameter value" pairs at the end
10949 of the arguments string. */
10950 for (tok = exp_end - 1; tok > arg; tok--)
10951 {
10952 /* Skip whitespace at the end of the argument list. */
10953 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10954 tok--;
10955
10956 /* Find the beginning of the last token.
10957 This is the value of the parameter. */
10958 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10959 tok--;
10960 value_start = tok + 1;
10961
10962 /* Skip whitespace. */
10963 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10964 tok--;
10965
10966 end_tok = tok;
10967
10968 /* Find the beginning of the second to last token.
10969 This is the parameter itself. */
10970 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10971 tok--;
10972 tok++;
10973 toklen = end_tok - tok + 1;
10974
10975 if (toklen == 6 && startswith (tok, "thread"))
10976 {
10977 struct thread_info *thr;
10978 /* At this point we've found a "thread" token, which means
10979 the user is trying to set a watchpoint that triggers
10980 only in a specific thread. */
10981 const char *endp;
10982
10983 if (thread != -1)
10984 error(_("You can specify only one thread."));
10985
10986 /* Extract the thread ID from the next token. */
10987 thr = parse_thread_id (value_start, &endp);
10988
10989 /* Check if the user provided a valid thread ID. */
10990 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10991 invalid_thread_id_error (value_start);
10992
10993 thread = thr->global_num;
10994 }
10995 else if (toklen == 4 && startswith (tok, "mask"))
10996 {
10997 /* We've found a "mask" token, which means the user wants to
10998 create a hardware watchpoint that is going to have the mask
10999 facility. */
11000 struct value *mask_value, *mark;
11001
11002 if (use_mask)
11003 error(_("You can specify only one mask."));
11004
11005 use_mask = just_location = 1;
11006
11007 mark = value_mark ();
11008 mask_value = parse_to_comma_and_eval (&value_start);
11009 mask = value_as_address (mask_value);
11010 value_free_to_mark (mark);
11011 }
11012 else
11013 /* We didn't recognize what we found. We should stop here. */
11014 break;
11015
11016 /* Truncate the string and get rid of the "parameter value" pair before
11017 the arguments string is parsed by the parse_exp_1 function. */
11018 exp_end = tok;
11019 }
11020 }
11021 else
11022 exp_end = arg;
11023
11024 /* Parse the rest of the arguments. From here on out, everything
11025 is in terms of a newly allocated string instead of the original
11026 ARG. */
11027 innermost_block = NULL;
11028 expression = savestring (arg, exp_end - arg);
11029 back_to = make_cleanup (xfree, expression);
11030 exp_start = arg = expression;
11031 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11032 exp_end = arg;
11033 /* Remove trailing whitespace from the expression before saving it.
11034 This makes the eventual display of the expression string a bit
11035 prettier. */
11036 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11037 --exp_end;
11038
11039 /* Checking if the expression is not constant. */
11040 if (watchpoint_exp_is_const (exp.get ()))
11041 {
11042 int len;
11043
11044 len = exp_end - exp_start;
11045 while (len > 0 && isspace (exp_start[len - 1]))
11046 len--;
11047 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11048 }
11049
11050 exp_valid_block = innermost_block;
11051 mark = value_mark ();
11052 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11053
11054 if (val != NULL && just_location)
11055 {
11056 saved_bitpos = value_bitpos (val);
11057 saved_bitsize = value_bitsize (val);
11058 }
11059
11060 if (just_location)
11061 {
11062 int ret;
11063
11064 exp_valid_block = NULL;
11065 val = value_addr (result);
11066 release_value (val);
11067 value_free_to_mark (mark);
11068
11069 if (use_mask)
11070 {
11071 ret = target_masked_watch_num_registers (value_as_address (val),
11072 mask);
11073 if (ret == -1)
11074 error (_("This target does not support masked watchpoints."));
11075 else if (ret == -2)
11076 error (_("Invalid mask or memory region."));
11077 }
11078 }
11079 else if (val != NULL)
11080 release_value (val);
11081
11082 tok = skip_spaces (arg);
11083 end_tok = skip_to_space (tok);
11084
11085 toklen = end_tok - tok;
11086 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11087 {
11088 innermost_block = NULL;
11089 tok = cond_start = end_tok + 1;
11090 parse_exp_1 (&tok, 0, 0, 0);
11091
11092 /* The watchpoint expression may not be local, but the condition
11093 may still be. E.g.: `watch global if local > 0'. */
11094 cond_exp_valid_block = innermost_block;
11095
11096 cond_end = tok;
11097 }
11098 if (*tok)
11099 error (_("Junk at end of command."));
11100
11101 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11102
11103 /* Save this because create_internal_breakpoint below invalidates
11104 'wp_frame'. */
11105 frame_id watchpoint_frame = get_frame_id (wp_frame);
11106
11107 /* If the expression is "local", then set up a "watchpoint scope"
11108 breakpoint at the point where we've left the scope of the watchpoint
11109 expression. Create the scope breakpoint before the watchpoint, so
11110 that we will encounter it first in bpstat_stop_status. */
11111 if (exp_valid_block != NULL && wp_frame != NULL)
11112 {
11113 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11114
11115 if (frame_id_p (caller_frame_id))
11116 {
11117 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11118 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11119
11120 scope_breakpoint
11121 = create_internal_breakpoint (caller_arch, caller_pc,
11122 bp_watchpoint_scope,
11123 &momentary_breakpoint_ops);
11124
11125 /* create_internal_breakpoint could invalidate WP_FRAME. */
11126 wp_frame = NULL;
11127
11128 scope_breakpoint->enable_state = bp_enabled;
11129
11130 /* Automatically delete the breakpoint when it hits. */
11131 scope_breakpoint->disposition = disp_del;
11132
11133 /* Only break in the proper frame (help with recursion). */
11134 scope_breakpoint->frame_id = caller_frame_id;
11135
11136 /* Set the address at which we will stop. */
11137 scope_breakpoint->loc->gdbarch = caller_arch;
11138 scope_breakpoint->loc->requested_address = caller_pc;
11139 scope_breakpoint->loc->address
11140 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11141 scope_breakpoint->loc->requested_address,
11142 scope_breakpoint->type);
11143 }
11144 }
11145
11146 /* Now set up the breakpoint. We create all watchpoints as hardware
11147 watchpoints here even if hardware watchpoints are turned off, a call
11148 to update_watchpoint later in this function will cause the type to
11149 drop back to bp_watchpoint (software watchpoint) if required. */
11150
11151 if (accessflag == hw_read)
11152 bp_type = bp_read_watchpoint;
11153 else if (accessflag == hw_access)
11154 bp_type = bp_access_watchpoint;
11155 else
11156 bp_type = bp_hardware_watchpoint;
11157
11158 std::unique_ptr<watchpoint> w (new watchpoint ());
11159
11160 if (use_mask)
11161 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11162 &masked_watchpoint_breakpoint_ops);
11163 else
11164 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
11165 &watchpoint_breakpoint_ops);
11166 w->thread = thread;
11167 w->disposition = disp_donttouch;
11168 w->pspace = current_program_space;
11169 w->exp = std::move (exp);
11170 w->exp_valid_block = exp_valid_block;
11171 w->cond_exp_valid_block = cond_exp_valid_block;
11172 if (just_location)
11173 {
11174 struct type *t = value_type (val);
11175 CORE_ADDR addr = value_as_address (val);
11176
11177 w->exp_string_reparse
11178 = current_language->la_watch_location_expression (t, addr).release ();
11179
11180 w->exp_string = xstrprintf ("-location %.*s",
11181 (int) (exp_end - exp_start), exp_start);
11182 }
11183 else
11184 w->exp_string = savestring (exp_start, exp_end - exp_start);
11185
11186 if (use_mask)
11187 {
11188 w->hw_wp_mask = mask;
11189 }
11190 else
11191 {
11192 w->val = val;
11193 w->val_bitpos = saved_bitpos;
11194 w->val_bitsize = saved_bitsize;
11195 w->val_valid = 1;
11196 }
11197
11198 if (cond_start)
11199 w->cond_string = savestring (cond_start, cond_end - cond_start);
11200 else
11201 w->cond_string = 0;
11202
11203 if (frame_id_p (watchpoint_frame))
11204 {
11205 w->watchpoint_frame = watchpoint_frame;
11206 w->watchpoint_thread = inferior_ptid;
11207 }
11208 else
11209 {
11210 w->watchpoint_frame = null_frame_id;
11211 w->watchpoint_thread = null_ptid;
11212 }
11213
11214 if (scope_breakpoint != NULL)
11215 {
11216 /* The scope breakpoint is related to the watchpoint. We will
11217 need to act on them together. */
11218 w->related_breakpoint = scope_breakpoint;
11219 scope_breakpoint->related_breakpoint = w.get ();
11220 }
11221
11222 if (!just_location)
11223 value_free_to_mark (mark);
11224
11225 /* Finally update the new watchpoint. This creates the locations
11226 that should be inserted. */
11227 update_watchpoint (w.get (), 1);
11228
11229 install_breakpoint (internal, std::move (w), 1);
11230 do_cleanups (back_to);
11231 }
11232
11233 /* Return count of debug registers needed to watch the given expression.
11234 If the watchpoint cannot be handled in hardware return zero. */
11235
11236 static int
11237 can_use_hardware_watchpoint (struct value *v)
11238 {
11239 int found_memory_cnt = 0;
11240 struct value *head = v;
11241
11242 /* Did the user specifically forbid us to use hardware watchpoints? */
11243 if (!can_use_hw_watchpoints)
11244 return 0;
11245
11246 /* Make sure that the value of the expression depends only upon
11247 memory contents, and values computed from them within GDB. If we
11248 find any register references or function calls, we can't use a
11249 hardware watchpoint.
11250
11251 The idea here is that evaluating an expression generates a series
11252 of values, one holding the value of every subexpression. (The
11253 expression a*b+c has five subexpressions: a, b, a*b, c, and
11254 a*b+c.) GDB's values hold almost enough information to establish
11255 the criteria given above --- they identify memory lvalues,
11256 register lvalues, computed values, etcetera. So we can evaluate
11257 the expression, and then scan the chain of values that leaves
11258 behind to decide whether we can detect any possible change to the
11259 expression's final value using only hardware watchpoints.
11260
11261 However, I don't think that the values returned by inferior
11262 function calls are special in any way. So this function may not
11263 notice that an expression involving an inferior function call
11264 can't be watched with hardware watchpoints. FIXME. */
11265 for (; v; v = value_next (v))
11266 {
11267 if (VALUE_LVAL (v) == lval_memory)
11268 {
11269 if (v != head && value_lazy (v))
11270 /* A lazy memory lvalue in the chain is one that GDB never
11271 needed to fetch; we either just used its address (e.g.,
11272 `a' in `a.b') or we never needed it at all (e.g., `a'
11273 in `a,b'). This doesn't apply to HEAD; if that is
11274 lazy then it was not readable, but watch it anyway. */
11275 ;
11276 else
11277 {
11278 /* Ahh, memory we actually used! Check if we can cover
11279 it with hardware watchpoints. */
11280 struct type *vtype = check_typedef (value_type (v));
11281
11282 /* We only watch structs and arrays if user asked for it
11283 explicitly, never if they just happen to appear in a
11284 middle of some value chain. */
11285 if (v == head
11286 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11287 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11288 {
11289 CORE_ADDR vaddr = value_address (v);
11290 int len;
11291 int num_regs;
11292
11293 len = (target_exact_watchpoints
11294 && is_scalar_type_recursive (vtype))?
11295 1 : TYPE_LENGTH (value_type (v));
11296
11297 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11298 if (!num_regs)
11299 return 0;
11300 else
11301 found_memory_cnt += num_regs;
11302 }
11303 }
11304 }
11305 else if (VALUE_LVAL (v) != not_lval
11306 && deprecated_value_modifiable (v) == 0)
11307 return 0; /* These are values from the history (e.g., $1). */
11308 else if (VALUE_LVAL (v) == lval_register)
11309 return 0; /* Cannot watch a register with a HW watchpoint. */
11310 }
11311
11312 /* The expression itself looks suitable for using a hardware
11313 watchpoint, but give the target machine a chance to reject it. */
11314 return found_memory_cnt;
11315 }
11316
11317 void
11318 watch_command_wrapper (char *arg, int from_tty, int internal)
11319 {
11320 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11321 }
11322
11323 /* A helper function that looks for the "-location" argument and then
11324 calls watch_command_1. */
11325
11326 static void
11327 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11328 {
11329 int just_location = 0;
11330
11331 if (arg
11332 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11333 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11334 {
11335 arg = skip_spaces (arg);
11336 just_location = 1;
11337 }
11338
11339 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11340 }
11341
11342 static void
11343 watch_command (char *arg, int from_tty)
11344 {
11345 watch_maybe_just_location (arg, hw_write, from_tty);
11346 }
11347
11348 void
11349 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11350 {
11351 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11352 }
11353
11354 static void
11355 rwatch_command (char *arg, int from_tty)
11356 {
11357 watch_maybe_just_location (arg, hw_read, from_tty);
11358 }
11359
11360 void
11361 awatch_command_wrapper (char *arg, int from_tty, int internal)
11362 {
11363 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11364 }
11365
11366 static void
11367 awatch_command (char *arg, int from_tty)
11368 {
11369 watch_maybe_just_location (arg, hw_access, from_tty);
11370 }
11371 \f
11372
11373 /* Data for the FSM that manages the until(location)/advance commands
11374 in infcmd.c. Here because it uses the mechanisms of
11375 breakpoints. */
11376
11377 struct until_break_fsm
11378 {
11379 /* The base class. */
11380 struct thread_fsm thread_fsm;
11381
11382 /* The thread that as current when the command was executed. */
11383 int thread;
11384
11385 /* The breakpoint set at the destination location. */
11386 struct breakpoint *location_breakpoint;
11387
11388 /* Breakpoint set at the return address in the caller frame. May be
11389 NULL. */
11390 struct breakpoint *caller_breakpoint;
11391 };
11392
11393 static void until_break_fsm_clean_up (struct thread_fsm *self,
11394 struct thread_info *thread);
11395 static int until_break_fsm_should_stop (struct thread_fsm *self,
11396 struct thread_info *thread);
11397 static enum async_reply_reason
11398 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11399
11400 /* until_break_fsm's vtable. */
11401
11402 static struct thread_fsm_ops until_break_fsm_ops =
11403 {
11404 NULL, /* dtor */
11405 until_break_fsm_clean_up,
11406 until_break_fsm_should_stop,
11407 NULL, /* return_value */
11408 until_break_fsm_async_reply_reason,
11409 };
11410
11411 /* Allocate a new until_break_command_fsm. */
11412
11413 static struct until_break_fsm *
11414 new_until_break_fsm (struct interp *cmd_interp, int thread,
11415 struct breakpoint *location_breakpoint,
11416 struct breakpoint *caller_breakpoint)
11417 {
11418 struct until_break_fsm *sm;
11419
11420 sm = XCNEW (struct until_break_fsm);
11421 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11422
11423 sm->thread = thread;
11424 sm->location_breakpoint = location_breakpoint;
11425 sm->caller_breakpoint = caller_breakpoint;
11426
11427 return sm;
11428 }
11429
11430 /* Implementation of the 'should_stop' FSM method for the
11431 until(location)/advance commands. */
11432
11433 static int
11434 until_break_fsm_should_stop (struct thread_fsm *self,
11435 struct thread_info *tp)
11436 {
11437 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11438
11439 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11440 sm->location_breakpoint) != NULL
11441 || (sm->caller_breakpoint != NULL
11442 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11443 sm->caller_breakpoint) != NULL))
11444 thread_fsm_set_finished (self);
11445
11446 return 1;
11447 }
11448
11449 /* Implementation of the 'clean_up' FSM method for the
11450 until(location)/advance commands. */
11451
11452 static void
11453 until_break_fsm_clean_up (struct thread_fsm *self,
11454 struct thread_info *thread)
11455 {
11456 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11457
11458 /* Clean up our temporary breakpoints. */
11459 if (sm->location_breakpoint != NULL)
11460 {
11461 delete_breakpoint (sm->location_breakpoint);
11462 sm->location_breakpoint = NULL;
11463 }
11464 if (sm->caller_breakpoint != NULL)
11465 {
11466 delete_breakpoint (sm->caller_breakpoint);
11467 sm->caller_breakpoint = NULL;
11468 }
11469 delete_longjmp_breakpoint (sm->thread);
11470 }
11471
11472 /* Implementation of the 'async_reply_reason' FSM method for the
11473 until(location)/advance commands. */
11474
11475 static enum async_reply_reason
11476 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11477 {
11478 return EXEC_ASYNC_LOCATION_REACHED;
11479 }
11480
11481 void
11482 until_break_command (char *arg, int from_tty, int anywhere)
11483 {
11484 struct frame_info *frame;
11485 struct gdbarch *frame_gdbarch;
11486 struct frame_id stack_frame_id;
11487 struct frame_id caller_frame_id;
11488 struct breakpoint *location_breakpoint;
11489 struct breakpoint *caller_breakpoint = NULL;
11490 struct cleanup *old_chain;
11491 int thread;
11492 struct thread_info *tp;
11493 struct until_break_fsm *sm;
11494
11495 clear_proceed_status (0);
11496
11497 /* Set a breakpoint where the user wants it and at return from
11498 this function. */
11499
11500 event_location_up location = string_to_event_location (&arg, current_language);
11501
11502 std::vector<symtab_and_line> sals
11503 = (last_displayed_sal_is_valid ()
11504 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11505 get_last_displayed_symtab (),
11506 get_last_displayed_line ())
11507 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11508 NULL, (struct symtab *) NULL, 0));
11509
11510 if (sals.size () != 1)
11511 error (_("Couldn't get information on specified line."));
11512
11513 symtab_and_line &sal = sals[0];
11514
11515 if (*arg)
11516 error (_("Junk at end of arguments."));
11517
11518 resolve_sal_pc (&sal);
11519
11520 tp = inferior_thread ();
11521 thread = tp->global_num;
11522
11523 old_chain = make_cleanup (null_cleanup, NULL);
11524
11525 /* Note linespec handling above invalidates the frame chain.
11526 Installing a breakpoint also invalidates the frame chain (as it
11527 may need to switch threads), so do any frame handling before
11528 that. */
11529
11530 frame = get_selected_frame (NULL);
11531 frame_gdbarch = get_frame_arch (frame);
11532 stack_frame_id = get_stack_frame_id (frame);
11533 caller_frame_id = frame_unwind_caller_id (frame);
11534
11535 /* Keep within the current frame, or in frames called by the current
11536 one. */
11537
11538 if (frame_id_p (caller_frame_id))
11539 {
11540 struct symtab_and_line sal2;
11541 struct gdbarch *caller_gdbarch;
11542
11543 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11544 sal2.pc = frame_unwind_caller_pc (frame);
11545 caller_gdbarch = frame_unwind_caller_arch (frame);
11546 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11547 sal2,
11548 caller_frame_id,
11549 bp_until);
11550 make_cleanup_delete_breakpoint (caller_breakpoint);
11551
11552 set_longjmp_breakpoint (tp, caller_frame_id);
11553 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11554 }
11555
11556 /* set_momentary_breakpoint could invalidate FRAME. */
11557 frame = NULL;
11558
11559 if (anywhere)
11560 /* If the user told us to continue until a specified location,
11561 we don't specify a frame at which we need to stop. */
11562 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11563 null_frame_id, bp_until);
11564 else
11565 /* Otherwise, specify the selected frame, because we want to stop
11566 only at the very same frame. */
11567 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11568 stack_frame_id, bp_until);
11569 make_cleanup_delete_breakpoint (location_breakpoint);
11570
11571 sm = new_until_break_fsm (command_interp (), tp->global_num,
11572 location_breakpoint, caller_breakpoint);
11573 tp->thread_fsm = &sm->thread_fsm;
11574
11575 discard_cleanups (old_chain);
11576
11577 proceed (-1, GDB_SIGNAL_DEFAULT);
11578 }
11579
11580 /* This function attempts to parse an optional "if <cond>" clause
11581 from the arg string. If one is not found, it returns NULL.
11582
11583 Else, it returns a pointer to the condition string. (It does not
11584 attempt to evaluate the string against a particular block.) And,
11585 it updates arg to point to the first character following the parsed
11586 if clause in the arg string. */
11587
11588 const char *
11589 ep_parse_optional_if_clause (const char **arg)
11590 {
11591 const char *cond_string;
11592
11593 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11594 return NULL;
11595
11596 /* Skip the "if" keyword. */
11597 (*arg) += 2;
11598
11599 /* Skip any extra leading whitespace, and record the start of the
11600 condition string. */
11601 *arg = skip_spaces (*arg);
11602 cond_string = *arg;
11603
11604 /* Assume that the condition occupies the remainder of the arg
11605 string. */
11606 (*arg) += strlen (cond_string);
11607
11608 return cond_string;
11609 }
11610
11611 /* Commands to deal with catching events, such as signals, exceptions,
11612 process start/exit, etc. */
11613
11614 typedef enum
11615 {
11616 catch_fork_temporary, catch_vfork_temporary,
11617 catch_fork_permanent, catch_vfork_permanent
11618 }
11619 catch_fork_kind;
11620
11621 static void
11622 catch_fork_command_1 (char *arg_entry, int from_tty,
11623 struct cmd_list_element *command)
11624 {
11625 const char *arg = arg_entry;
11626 struct gdbarch *gdbarch = get_current_arch ();
11627 const char *cond_string = NULL;
11628 catch_fork_kind fork_kind;
11629 int tempflag;
11630
11631 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11632 tempflag = (fork_kind == catch_fork_temporary
11633 || fork_kind == catch_vfork_temporary);
11634
11635 if (!arg)
11636 arg = "";
11637 arg = skip_spaces (arg);
11638
11639 /* The allowed syntax is:
11640 catch [v]fork
11641 catch [v]fork if <cond>
11642
11643 First, check if there's an if clause. */
11644 cond_string = ep_parse_optional_if_clause (&arg);
11645
11646 if ((*arg != '\0') && !isspace (*arg))
11647 error (_("Junk at end of arguments."));
11648
11649 /* If this target supports it, create a fork or vfork catchpoint
11650 and enable reporting of such events. */
11651 switch (fork_kind)
11652 {
11653 case catch_fork_temporary:
11654 case catch_fork_permanent:
11655 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11656 &catch_fork_breakpoint_ops);
11657 break;
11658 case catch_vfork_temporary:
11659 case catch_vfork_permanent:
11660 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11661 &catch_vfork_breakpoint_ops);
11662 break;
11663 default:
11664 error (_("unsupported or unknown fork kind; cannot catch it"));
11665 break;
11666 }
11667 }
11668
11669 static void
11670 catch_exec_command_1 (char *arg_entry, int from_tty,
11671 struct cmd_list_element *command)
11672 {
11673 const char *arg = arg_entry;
11674 struct gdbarch *gdbarch = get_current_arch ();
11675 int tempflag;
11676 const char *cond_string = NULL;
11677
11678 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11679
11680 if (!arg)
11681 arg = "";
11682 arg = skip_spaces (arg);
11683
11684 /* The allowed syntax is:
11685 catch exec
11686 catch exec if <cond>
11687
11688 First, check if there's an if clause. */
11689 cond_string = ep_parse_optional_if_clause (&arg);
11690
11691 if ((*arg != '\0') && !isspace (*arg))
11692 error (_("Junk at end of arguments."));
11693
11694 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11695 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
11696 &catch_exec_breakpoint_ops);
11697 c->exec_pathname = NULL;
11698
11699 install_breakpoint (0, std::move (c), 1);
11700 }
11701
11702 void
11703 init_ada_exception_breakpoint (struct breakpoint *b,
11704 struct gdbarch *gdbarch,
11705 struct symtab_and_line sal,
11706 char *addr_string,
11707 const struct breakpoint_ops *ops,
11708 int tempflag,
11709 int enabled,
11710 int from_tty)
11711 {
11712 if (from_tty)
11713 {
11714 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11715 if (!loc_gdbarch)
11716 loc_gdbarch = gdbarch;
11717
11718 describe_other_breakpoints (loc_gdbarch,
11719 sal.pspace, sal.pc, sal.section, -1);
11720 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11721 version for exception catchpoints, because two catchpoints
11722 used for different exception names will use the same address.
11723 In this case, a "breakpoint ... also set at..." warning is
11724 unproductive. Besides, the warning phrasing is also a bit
11725 inappropriate, we should use the word catchpoint, and tell
11726 the user what type of catchpoint it is. The above is good
11727 enough for now, though. */
11728 }
11729
11730 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11731
11732 b->enable_state = enabled ? bp_enabled : bp_disabled;
11733 b->disposition = tempflag ? disp_del : disp_donttouch;
11734 b->location = string_to_event_location (&addr_string,
11735 language_def (language_ada));
11736 b->language = language_ada;
11737 }
11738
11739 static void
11740 catch_command (char *arg, int from_tty)
11741 {
11742 error (_("Catch requires an event name."));
11743 }
11744 \f
11745
11746 static void
11747 tcatch_command (char *arg, int from_tty)
11748 {
11749 error (_("Catch requires an event name."));
11750 }
11751
11752 /* A qsort comparison function that sorts breakpoints in order. */
11753
11754 static int
11755 compare_breakpoints (const void *a, const void *b)
11756 {
11757 const breakpoint_p *ba = (const breakpoint_p *) a;
11758 uintptr_t ua = (uintptr_t) *ba;
11759 const breakpoint_p *bb = (const breakpoint_p *) b;
11760 uintptr_t ub = (uintptr_t) *bb;
11761
11762 if ((*ba)->number < (*bb)->number)
11763 return -1;
11764 else if ((*ba)->number > (*bb)->number)
11765 return 1;
11766
11767 /* Now sort by address, in case we see, e..g, two breakpoints with
11768 the number 0. */
11769 if (ua < ub)
11770 return -1;
11771 return ua > ub ? 1 : 0;
11772 }
11773
11774 /* Delete breakpoints by address or line. */
11775
11776 static void
11777 clear_command (char *arg, int from_tty)
11778 {
11779 struct breakpoint *b, *prev;
11780 VEC(breakpoint_p) *found = 0;
11781 int ix;
11782 int default_match;
11783 int i;
11784 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11785
11786 std::vector<symtab_and_line> decoded_sals;
11787 symtab_and_line last_sal;
11788 gdb::array_view<symtab_and_line> sals;
11789 if (arg)
11790 {
11791 decoded_sals
11792 = decode_line_with_current_source (arg,
11793 (DECODE_LINE_FUNFIRSTLINE
11794 | DECODE_LINE_LIST_MODE));
11795 default_match = 0;
11796 sals = decoded_sals;
11797 }
11798 else
11799 {
11800 /* Set sal's line, symtab, pc, and pspace to the values
11801 corresponding to the last call to print_frame_info. If the
11802 codepoint is not valid, this will set all the fields to 0. */
11803 last_sal = get_last_displayed_sal ();
11804 if (last_sal.symtab == 0)
11805 error (_("No source file specified."));
11806
11807 default_match = 1;
11808 sals = last_sal;
11809 }
11810
11811 /* We don't call resolve_sal_pc here. That's not as bad as it
11812 seems, because all existing breakpoints typically have both
11813 file/line and pc set. So, if clear is given file/line, we can
11814 match this to existing breakpoint without obtaining pc at all.
11815
11816 We only support clearing given the address explicitly
11817 present in breakpoint table. Say, we've set breakpoint
11818 at file:line. There were several PC values for that file:line,
11819 due to optimization, all in one block.
11820
11821 We've picked one PC value. If "clear" is issued with another
11822 PC corresponding to the same file:line, the breakpoint won't
11823 be cleared. We probably can still clear the breakpoint, but
11824 since the other PC value is never presented to user, user
11825 can only find it by guessing, and it does not seem important
11826 to support that. */
11827
11828 /* For each line spec given, delete bps which correspond to it. Do
11829 it in two passes, solely to preserve the current behavior that
11830 from_tty is forced true if we delete more than one
11831 breakpoint. */
11832
11833 found = NULL;
11834 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11835 for (const auto &sal : sals)
11836 {
11837 const char *sal_fullname;
11838
11839 /* If exact pc given, clear bpts at that pc.
11840 If line given (pc == 0), clear all bpts on specified line.
11841 If defaulting, clear all bpts on default line
11842 or at default pc.
11843
11844 defaulting sal.pc != 0 tests to do
11845
11846 0 1 pc
11847 1 1 pc _and_ line
11848 0 0 line
11849 1 0 <can't happen> */
11850
11851 sal_fullname = (sal.symtab == NULL
11852 ? NULL : symtab_to_fullname (sal.symtab));
11853
11854 /* Find all matching breakpoints and add them to 'found'. */
11855 ALL_BREAKPOINTS (b)
11856 {
11857 int match = 0;
11858 /* Are we going to delete b? */
11859 if (b->type != bp_none && !is_watchpoint (b))
11860 {
11861 struct bp_location *loc = b->loc;
11862 for (; loc; loc = loc->next)
11863 {
11864 /* If the user specified file:line, don't allow a PC
11865 match. This matches historical gdb behavior. */
11866 int pc_match = (!sal.explicit_line
11867 && sal.pc
11868 && (loc->pspace == sal.pspace)
11869 && (loc->address == sal.pc)
11870 && (!section_is_overlay (loc->section)
11871 || loc->section == sal.section));
11872 int line_match = 0;
11873
11874 if ((default_match || sal.explicit_line)
11875 && loc->symtab != NULL
11876 && sal_fullname != NULL
11877 && sal.pspace == loc->pspace
11878 && loc->line_number == sal.line
11879 && filename_cmp (symtab_to_fullname (loc->symtab),
11880 sal_fullname) == 0)
11881 line_match = 1;
11882
11883 if (pc_match || line_match)
11884 {
11885 match = 1;
11886 break;
11887 }
11888 }
11889 }
11890
11891 if (match)
11892 VEC_safe_push(breakpoint_p, found, b);
11893 }
11894 }
11895
11896 /* Now go thru the 'found' chain and delete them. */
11897 if (VEC_empty(breakpoint_p, found))
11898 {
11899 if (arg)
11900 error (_("No breakpoint at %s."), arg);
11901 else
11902 error (_("No breakpoint at this line."));
11903 }
11904
11905 /* Remove duplicates from the vec. */
11906 qsort (VEC_address (breakpoint_p, found),
11907 VEC_length (breakpoint_p, found),
11908 sizeof (breakpoint_p),
11909 compare_breakpoints);
11910 prev = VEC_index (breakpoint_p, found, 0);
11911 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11912 {
11913 if (b == prev)
11914 {
11915 VEC_ordered_remove (breakpoint_p, found, ix);
11916 --ix;
11917 }
11918 }
11919
11920 if (VEC_length(breakpoint_p, found) > 1)
11921 from_tty = 1; /* Always report if deleted more than one. */
11922 if (from_tty)
11923 {
11924 if (VEC_length(breakpoint_p, found) == 1)
11925 printf_unfiltered (_("Deleted breakpoint "));
11926 else
11927 printf_unfiltered (_("Deleted breakpoints "));
11928 }
11929
11930 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11931 {
11932 if (from_tty)
11933 printf_unfiltered ("%d ", b->number);
11934 delete_breakpoint (b);
11935 }
11936 if (from_tty)
11937 putchar_unfiltered ('\n');
11938
11939 do_cleanups (cleanups);
11940 }
11941 \f
11942 /* Delete breakpoint in BS if they are `delete' breakpoints and
11943 all breakpoints that are marked for deletion, whether hit or not.
11944 This is called after any breakpoint is hit, or after errors. */
11945
11946 void
11947 breakpoint_auto_delete (bpstat bs)
11948 {
11949 struct breakpoint *b, *b_tmp;
11950
11951 for (; bs; bs = bs->next)
11952 if (bs->breakpoint_at
11953 && bs->breakpoint_at->disposition == disp_del
11954 && bs->stop)
11955 delete_breakpoint (bs->breakpoint_at);
11956
11957 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11958 {
11959 if (b->disposition == disp_del_at_next_stop)
11960 delete_breakpoint (b);
11961 }
11962 }
11963
11964 /* A comparison function for bp_location AP and BP being interfaced to
11965 qsort. Sort elements primarily by their ADDRESS (no matter what
11966 does breakpoint_address_is_meaningful say for its OWNER),
11967 secondarily by ordering first permanent elements and
11968 terciarily just ensuring the array is sorted stable way despite
11969 qsort being an unstable algorithm. */
11970
11971 static int
11972 bp_locations_compare (const void *ap, const void *bp)
11973 {
11974 const struct bp_location *a = *(const struct bp_location **) ap;
11975 const struct bp_location *b = *(const struct bp_location **) bp;
11976
11977 if (a->address != b->address)
11978 return (a->address > b->address) - (a->address < b->address);
11979
11980 /* Sort locations at the same address by their pspace number, keeping
11981 locations of the same inferior (in a multi-inferior environment)
11982 grouped. */
11983
11984 if (a->pspace->num != b->pspace->num)
11985 return ((a->pspace->num > b->pspace->num)
11986 - (a->pspace->num < b->pspace->num));
11987
11988 /* Sort permanent breakpoints first. */
11989 if (a->permanent != b->permanent)
11990 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11991
11992 /* Make the internal GDB representation stable across GDB runs
11993 where A and B memory inside GDB can differ. Breakpoint locations of
11994 the same type at the same address can be sorted in arbitrary order. */
11995
11996 if (a->owner->number != b->owner->number)
11997 return ((a->owner->number > b->owner->number)
11998 - (a->owner->number < b->owner->number));
11999
12000 return (a > b) - (a < b);
12001 }
12002
12003 /* Set bp_locations_placed_address_before_address_max and
12004 bp_locations_shadow_len_after_address_max according to the current
12005 content of the bp_locations array. */
12006
12007 static void
12008 bp_locations_target_extensions_update (void)
12009 {
12010 struct bp_location *bl, **blp_tmp;
12011
12012 bp_locations_placed_address_before_address_max = 0;
12013 bp_locations_shadow_len_after_address_max = 0;
12014
12015 ALL_BP_LOCATIONS (bl, blp_tmp)
12016 {
12017 CORE_ADDR start, end, addr;
12018
12019 if (!bp_location_has_shadow (bl))
12020 continue;
12021
12022 start = bl->target_info.placed_address;
12023 end = start + bl->target_info.shadow_len;
12024
12025 gdb_assert (bl->address >= start);
12026 addr = bl->address - start;
12027 if (addr > bp_locations_placed_address_before_address_max)
12028 bp_locations_placed_address_before_address_max = addr;
12029
12030 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12031
12032 gdb_assert (bl->address < end);
12033 addr = end - bl->address;
12034 if (addr > bp_locations_shadow_len_after_address_max)
12035 bp_locations_shadow_len_after_address_max = addr;
12036 }
12037 }
12038
12039 /* Download tracepoint locations if they haven't been. */
12040
12041 static void
12042 download_tracepoint_locations (void)
12043 {
12044 struct breakpoint *b;
12045 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12046
12047 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12048
12049 ALL_TRACEPOINTS (b)
12050 {
12051 struct bp_location *bl;
12052 struct tracepoint *t;
12053 int bp_location_downloaded = 0;
12054
12055 if ((b->type == bp_fast_tracepoint
12056 ? !may_insert_fast_tracepoints
12057 : !may_insert_tracepoints))
12058 continue;
12059
12060 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12061 {
12062 if (target_can_download_tracepoint ())
12063 can_download_tracepoint = TRIBOOL_TRUE;
12064 else
12065 can_download_tracepoint = TRIBOOL_FALSE;
12066 }
12067
12068 if (can_download_tracepoint == TRIBOOL_FALSE)
12069 break;
12070
12071 for (bl = b->loc; bl; bl = bl->next)
12072 {
12073 /* In tracepoint, locations are _never_ duplicated, so
12074 should_be_inserted is equivalent to
12075 unduplicated_should_be_inserted. */
12076 if (!should_be_inserted (bl) || bl->inserted)
12077 continue;
12078
12079 switch_to_program_space_and_thread (bl->pspace);
12080
12081 target_download_tracepoint (bl);
12082
12083 bl->inserted = 1;
12084 bp_location_downloaded = 1;
12085 }
12086 t = (struct tracepoint *) b;
12087 t->number_on_target = b->number;
12088 if (bp_location_downloaded)
12089 observer_notify_breakpoint_modified (b);
12090 }
12091 }
12092
12093 /* Swap the insertion/duplication state between two locations. */
12094
12095 static void
12096 swap_insertion (struct bp_location *left, struct bp_location *right)
12097 {
12098 const int left_inserted = left->inserted;
12099 const int left_duplicate = left->duplicate;
12100 const int left_needs_update = left->needs_update;
12101 const struct bp_target_info left_target_info = left->target_info;
12102
12103 /* Locations of tracepoints can never be duplicated. */
12104 if (is_tracepoint (left->owner))
12105 gdb_assert (!left->duplicate);
12106 if (is_tracepoint (right->owner))
12107 gdb_assert (!right->duplicate);
12108
12109 left->inserted = right->inserted;
12110 left->duplicate = right->duplicate;
12111 left->needs_update = right->needs_update;
12112 left->target_info = right->target_info;
12113 right->inserted = left_inserted;
12114 right->duplicate = left_duplicate;
12115 right->needs_update = left_needs_update;
12116 right->target_info = left_target_info;
12117 }
12118
12119 /* Force the re-insertion of the locations at ADDRESS. This is called
12120 once a new/deleted/modified duplicate location is found and we are evaluating
12121 conditions on the target's side. Such conditions need to be updated on
12122 the target. */
12123
12124 static void
12125 force_breakpoint_reinsertion (struct bp_location *bl)
12126 {
12127 struct bp_location **locp = NULL, **loc2p;
12128 struct bp_location *loc;
12129 CORE_ADDR address = 0;
12130 int pspace_num;
12131
12132 address = bl->address;
12133 pspace_num = bl->pspace->num;
12134
12135 /* This is only meaningful if the target is
12136 evaluating conditions and if the user has
12137 opted for condition evaluation on the target's
12138 side. */
12139 if (gdb_evaluates_breakpoint_condition_p ()
12140 || !target_supports_evaluation_of_breakpoint_conditions ())
12141 return;
12142
12143 /* Flag all breakpoint locations with this address and
12144 the same program space as the location
12145 as "its condition has changed". We need to
12146 update the conditions on the target's side. */
12147 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12148 {
12149 loc = *loc2p;
12150
12151 if (!is_breakpoint (loc->owner)
12152 || pspace_num != loc->pspace->num)
12153 continue;
12154
12155 /* Flag the location appropriately. We use a different state to
12156 let everyone know that we already updated the set of locations
12157 with addr bl->address and program space bl->pspace. This is so
12158 we don't have to keep calling these functions just to mark locations
12159 that have already been marked. */
12160 loc->condition_changed = condition_updated;
12161
12162 /* Free the agent expression bytecode as well. We will compute
12163 it later on. */
12164 loc->cond_bytecode.reset ();
12165 }
12166 }
12167 /* Called whether new breakpoints are created, or existing breakpoints
12168 deleted, to update the global location list and recompute which
12169 locations are duplicate of which.
12170
12171 The INSERT_MODE flag determines whether locations may not, may, or
12172 shall be inserted now. See 'enum ugll_insert_mode' for more
12173 info. */
12174
12175 static void
12176 update_global_location_list (enum ugll_insert_mode insert_mode)
12177 {
12178 struct breakpoint *b;
12179 struct bp_location **locp, *loc;
12180 struct cleanup *cleanups;
12181 /* Last breakpoint location address that was marked for update. */
12182 CORE_ADDR last_addr = 0;
12183 /* Last breakpoint location program space that was marked for update. */
12184 int last_pspace_num = -1;
12185
12186 /* Used in the duplicates detection below. When iterating over all
12187 bp_locations, points to the first bp_location of a given address.
12188 Breakpoints and watchpoints of different types are never
12189 duplicates of each other. Keep one pointer for each type of
12190 breakpoint/watchpoint, so we only need to loop over all locations
12191 once. */
12192 struct bp_location *bp_loc_first; /* breakpoint */
12193 struct bp_location *wp_loc_first; /* hardware watchpoint */
12194 struct bp_location *awp_loc_first; /* access watchpoint */
12195 struct bp_location *rwp_loc_first; /* read watchpoint */
12196
12197 /* Saved former bp_locations array which we compare against the newly
12198 built bp_locations from the current state of ALL_BREAKPOINTS. */
12199 struct bp_location **old_locations, **old_locp;
12200 unsigned old_locations_count;
12201
12202 old_locations = bp_locations;
12203 old_locations_count = bp_locations_count;
12204 bp_locations = NULL;
12205 bp_locations_count = 0;
12206 cleanups = make_cleanup (xfree, old_locations);
12207
12208 ALL_BREAKPOINTS (b)
12209 for (loc = b->loc; loc; loc = loc->next)
12210 bp_locations_count++;
12211
12212 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12213 locp = bp_locations;
12214 ALL_BREAKPOINTS (b)
12215 for (loc = b->loc; loc; loc = loc->next)
12216 *locp++ = loc;
12217 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12218 bp_locations_compare);
12219
12220 bp_locations_target_extensions_update ();
12221
12222 /* Identify bp_location instances that are no longer present in the
12223 new list, and therefore should be freed. Note that it's not
12224 necessary that those locations should be removed from inferior --
12225 if there's another location at the same address (previously
12226 marked as duplicate), we don't need to remove/insert the
12227 location.
12228
12229 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12230 and former bp_location array state respectively. */
12231
12232 locp = bp_locations;
12233 for (old_locp = old_locations;
12234 old_locp < old_locations + old_locations_count;
12235 old_locp++)
12236 {
12237 struct bp_location *old_loc = *old_locp;
12238 struct bp_location **loc2p;
12239
12240 /* Tells if 'old_loc' is found among the new locations. If
12241 not, we have to free it. */
12242 int found_object = 0;
12243 /* Tells if the location should remain inserted in the target. */
12244 int keep_in_target = 0;
12245 int removed = 0;
12246
12247 /* Skip LOCP entries which will definitely never be needed.
12248 Stop either at or being the one matching OLD_LOC. */
12249 while (locp < bp_locations + bp_locations_count
12250 && (*locp)->address < old_loc->address)
12251 locp++;
12252
12253 for (loc2p = locp;
12254 (loc2p < bp_locations + bp_locations_count
12255 && (*loc2p)->address == old_loc->address);
12256 loc2p++)
12257 {
12258 /* Check if this is a new/duplicated location or a duplicated
12259 location that had its condition modified. If so, we want to send
12260 its condition to the target if evaluation of conditions is taking
12261 place there. */
12262 if ((*loc2p)->condition_changed == condition_modified
12263 && (last_addr != old_loc->address
12264 || last_pspace_num != old_loc->pspace->num))
12265 {
12266 force_breakpoint_reinsertion (*loc2p);
12267 last_pspace_num = old_loc->pspace->num;
12268 }
12269
12270 if (*loc2p == old_loc)
12271 found_object = 1;
12272 }
12273
12274 /* We have already handled this address, update it so that we don't
12275 have to go through updates again. */
12276 last_addr = old_loc->address;
12277
12278 /* Target-side condition evaluation: Handle deleted locations. */
12279 if (!found_object)
12280 force_breakpoint_reinsertion (old_loc);
12281
12282 /* If this location is no longer present, and inserted, look if
12283 there's maybe a new location at the same address. If so,
12284 mark that one inserted, and don't remove this one. This is
12285 needed so that we don't have a time window where a breakpoint
12286 at certain location is not inserted. */
12287
12288 if (old_loc->inserted)
12289 {
12290 /* If the location is inserted now, we might have to remove
12291 it. */
12292
12293 if (found_object && should_be_inserted (old_loc))
12294 {
12295 /* The location is still present in the location list,
12296 and still should be inserted. Don't do anything. */
12297 keep_in_target = 1;
12298 }
12299 else
12300 {
12301 /* This location still exists, but it won't be kept in the
12302 target since it may have been disabled. We proceed to
12303 remove its target-side condition. */
12304
12305 /* The location is either no longer present, or got
12306 disabled. See if there's another location at the
12307 same address, in which case we don't need to remove
12308 this one from the target. */
12309
12310 /* OLD_LOC comes from existing struct breakpoint. */
12311 if (breakpoint_address_is_meaningful (old_loc->owner))
12312 {
12313 for (loc2p = locp;
12314 (loc2p < bp_locations + bp_locations_count
12315 && (*loc2p)->address == old_loc->address);
12316 loc2p++)
12317 {
12318 struct bp_location *loc2 = *loc2p;
12319
12320 if (breakpoint_locations_match (loc2, old_loc))
12321 {
12322 /* Read watchpoint locations are switched to
12323 access watchpoints, if the former are not
12324 supported, but the latter are. */
12325 if (is_hardware_watchpoint (old_loc->owner))
12326 {
12327 gdb_assert (is_hardware_watchpoint (loc2->owner));
12328 loc2->watchpoint_type = old_loc->watchpoint_type;
12329 }
12330
12331 /* loc2 is a duplicated location. We need to check
12332 if it should be inserted in case it will be
12333 unduplicated. */
12334 if (loc2 != old_loc
12335 && unduplicated_should_be_inserted (loc2))
12336 {
12337 swap_insertion (old_loc, loc2);
12338 keep_in_target = 1;
12339 break;
12340 }
12341 }
12342 }
12343 }
12344 }
12345
12346 if (!keep_in_target)
12347 {
12348 if (remove_breakpoint (old_loc))
12349 {
12350 /* This is just about all we can do. We could keep
12351 this location on the global list, and try to
12352 remove it next time, but there's no particular
12353 reason why we will succeed next time.
12354
12355 Note that at this point, old_loc->owner is still
12356 valid, as delete_breakpoint frees the breakpoint
12357 only after calling us. */
12358 printf_filtered (_("warning: Error removing "
12359 "breakpoint %d\n"),
12360 old_loc->owner->number);
12361 }
12362 removed = 1;
12363 }
12364 }
12365
12366 if (!found_object)
12367 {
12368 if (removed && target_is_non_stop_p ()
12369 && need_moribund_for_location_type (old_loc))
12370 {
12371 /* This location was removed from the target. In
12372 non-stop mode, a race condition is possible where
12373 we've removed a breakpoint, but stop events for that
12374 breakpoint are already queued and will arrive later.
12375 We apply an heuristic to be able to distinguish such
12376 SIGTRAPs from other random SIGTRAPs: we keep this
12377 breakpoint location for a bit, and will retire it
12378 after we see some number of events. The theory here
12379 is that reporting of events should, "on the average",
12380 be fair, so after a while we'll see events from all
12381 threads that have anything of interest, and no longer
12382 need to keep this breakpoint location around. We
12383 don't hold locations forever so to reduce chances of
12384 mistaking a non-breakpoint SIGTRAP for a breakpoint
12385 SIGTRAP.
12386
12387 The heuristic failing can be disastrous on
12388 decr_pc_after_break targets.
12389
12390 On decr_pc_after_break targets, like e.g., x86-linux,
12391 if we fail to recognize a late breakpoint SIGTRAP,
12392 because events_till_retirement has reached 0 too
12393 soon, we'll fail to do the PC adjustment, and report
12394 a random SIGTRAP to the user. When the user resumes
12395 the inferior, it will most likely immediately crash
12396 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12397 corrupted, because of being resumed e.g., in the
12398 middle of a multi-byte instruction, or skipped a
12399 one-byte instruction. This was actually seen happen
12400 on native x86-linux, and should be less rare on
12401 targets that do not support new thread events, like
12402 remote, due to the heuristic depending on
12403 thread_count.
12404
12405 Mistaking a random SIGTRAP for a breakpoint trap
12406 causes similar symptoms (PC adjustment applied when
12407 it shouldn't), but then again, playing with SIGTRAPs
12408 behind the debugger's back is asking for trouble.
12409
12410 Since hardware watchpoint traps are always
12411 distinguishable from other traps, so we don't need to
12412 apply keep hardware watchpoint moribund locations
12413 around. We simply always ignore hardware watchpoint
12414 traps we can no longer explain. */
12415
12416 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12417 old_loc->owner = NULL;
12418
12419 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12420 }
12421 else
12422 {
12423 old_loc->owner = NULL;
12424 decref_bp_location (&old_loc);
12425 }
12426 }
12427 }
12428
12429 /* Rescan breakpoints at the same address and section, marking the
12430 first one as "first" and any others as "duplicates". This is so
12431 that the bpt instruction is only inserted once. If we have a
12432 permanent breakpoint at the same place as BPT, make that one the
12433 official one, and the rest as duplicates. Permanent breakpoints
12434 are sorted first for the same address.
12435
12436 Do the same for hardware watchpoints, but also considering the
12437 watchpoint's type (regular/access/read) and length. */
12438
12439 bp_loc_first = NULL;
12440 wp_loc_first = NULL;
12441 awp_loc_first = NULL;
12442 rwp_loc_first = NULL;
12443 ALL_BP_LOCATIONS (loc, locp)
12444 {
12445 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12446 non-NULL. */
12447 struct bp_location **loc_first_p;
12448 b = loc->owner;
12449
12450 if (!unduplicated_should_be_inserted (loc)
12451 || !breakpoint_address_is_meaningful (b)
12452 /* Don't detect duplicate for tracepoint locations because they are
12453 never duplicated. See the comments in field `duplicate' of
12454 `struct bp_location'. */
12455 || is_tracepoint (b))
12456 {
12457 /* Clear the condition modification flag. */
12458 loc->condition_changed = condition_unchanged;
12459 continue;
12460 }
12461
12462 if (b->type == bp_hardware_watchpoint)
12463 loc_first_p = &wp_loc_first;
12464 else if (b->type == bp_read_watchpoint)
12465 loc_first_p = &rwp_loc_first;
12466 else if (b->type == bp_access_watchpoint)
12467 loc_first_p = &awp_loc_first;
12468 else
12469 loc_first_p = &bp_loc_first;
12470
12471 if (*loc_first_p == NULL
12472 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12473 || !breakpoint_locations_match (loc, *loc_first_p))
12474 {
12475 *loc_first_p = loc;
12476 loc->duplicate = 0;
12477
12478 if (is_breakpoint (loc->owner) && loc->condition_changed)
12479 {
12480 loc->needs_update = 1;
12481 /* Clear the condition modification flag. */
12482 loc->condition_changed = condition_unchanged;
12483 }
12484 continue;
12485 }
12486
12487
12488 /* This and the above ensure the invariant that the first location
12489 is not duplicated, and is the inserted one.
12490 All following are marked as duplicated, and are not inserted. */
12491 if (loc->inserted)
12492 swap_insertion (loc, *loc_first_p);
12493 loc->duplicate = 1;
12494
12495 /* Clear the condition modification flag. */
12496 loc->condition_changed = condition_unchanged;
12497 }
12498
12499 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12500 {
12501 if (insert_mode != UGLL_DONT_INSERT)
12502 insert_breakpoint_locations ();
12503 else
12504 {
12505 /* Even though the caller told us to not insert new
12506 locations, we may still need to update conditions on the
12507 target's side of breakpoints that were already inserted
12508 if the target is evaluating breakpoint conditions. We
12509 only update conditions for locations that are marked
12510 "needs_update". */
12511 update_inserted_breakpoint_locations ();
12512 }
12513 }
12514
12515 if (insert_mode != UGLL_DONT_INSERT)
12516 download_tracepoint_locations ();
12517
12518 do_cleanups (cleanups);
12519 }
12520
12521 void
12522 breakpoint_retire_moribund (void)
12523 {
12524 struct bp_location *loc;
12525 int ix;
12526
12527 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12528 if (--(loc->events_till_retirement) == 0)
12529 {
12530 decref_bp_location (&loc);
12531 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12532 --ix;
12533 }
12534 }
12535
12536 static void
12537 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12538 {
12539
12540 TRY
12541 {
12542 update_global_location_list (insert_mode);
12543 }
12544 CATCH (e, RETURN_MASK_ERROR)
12545 {
12546 }
12547 END_CATCH
12548 }
12549
12550 /* Clear BKP from a BPS. */
12551
12552 static void
12553 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12554 {
12555 bpstat bs;
12556
12557 for (bs = bps; bs; bs = bs->next)
12558 if (bs->breakpoint_at == bpt)
12559 {
12560 bs->breakpoint_at = NULL;
12561 bs->old_val = NULL;
12562 /* bs->commands will be freed later. */
12563 }
12564 }
12565
12566 /* Callback for iterate_over_threads. */
12567 static int
12568 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12569 {
12570 struct breakpoint *bpt = (struct breakpoint *) data;
12571
12572 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12573 return 0;
12574 }
12575
12576 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12577 callbacks. */
12578
12579 static void
12580 say_where (struct breakpoint *b)
12581 {
12582 struct value_print_options opts;
12583
12584 get_user_print_options (&opts);
12585
12586 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12587 single string. */
12588 if (b->loc == NULL)
12589 {
12590 /* For pending locations, the output differs slightly based
12591 on b->extra_string. If this is non-NULL, it contains either
12592 a condition or dprintf arguments. */
12593 if (b->extra_string == NULL)
12594 {
12595 printf_filtered (_(" (%s) pending."),
12596 event_location_to_string (b->location.get ()));
12597 }
12598 else if (b->type == bp_dprintf)
12599 {
12600 printf_filtered (_(" (%s,%s) pending."),
12601 event_location_to_string (b->location.get ()),
12602 b->extra_string);
12603 }
12604 else
12605 {
12606 printf_filtered (_(" (%s %s) pending."),
12607 event_location_to_string (b->location.get ()),
12608 b->extra_string);
12609 }
12610 }
12611 else
12612 {
12613 if (opts.addressprint || b->loc->symtab == NULL)
12614 {
12615 printf_filtered (" at ");
12616 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12617 gdb_stdout);
12618 }
12619 if (b->loc->symtab != NULL)
12620 {
12621 /* If there is a single location, we can print the location
12622 more nicely. */
12623 if (b->loc->next == NULL)
12624 printf_filtered (": file %s, line %d.",
12625 symtab_to_filename_for_display (b->loc->symtab),
12626 b->loc->line_number);
12627 else
12628 /* This is not ideal, but each location may have a
12629 different file name, and this at least reflects the
12630 real situation somewhat. */
12631 printf_filtered (": %s.",
12632 event_location_to_string (b->location.get ()));
12633 }
12634
12635 if (b->loc->next)
12636 {
12637 struct bp_location *loc = b->loc;
12638 int n = 0;
12639 for (; loc; loc = loc->next)
12640 ++n;
12641 printf_filtered (" (%d locations)", n);
12642 }
12643 }
12644 }
12645
12646 /* Default bp_location_ops methods. */
12647
12648 static void
12649 bp_location_dtor (struct bp_location *self)
12650 {
12651 xfree (self->function_name);
12652 }
12653
12654 static const struct bp_location_ops bp_location_ops =
12655 {
12656 bp_location_dtor
12657 };
12658
12659 /* Destructor for the breakpoint base class. */
12660
12661 breakpoint::~breakpoint ()
12662 {
12663 decref_counted_command_line (&this->commands);
12664 xfree (this->cond_string);
12665 xfree (this->extra_string);
12666 xfree (this->filter);
12667 }
12668
12669 static struct bp_location *
12670 base_breakpoint_allocate_location (struct breakpoint *self)
12671 {
12672 return new bp_location (&bp_location_ops, self);
12673 }
12674
12675 static void
12676 base_breakpoint_re_set (struct breakpoint *b)
12677 {
12678 /* Nothing to re-set. */
12679 }
12680
12681 #define internal_error_pure_virtual_called() \
12682 gdb_assert_not_reached ("pure virtual function called")
12683
12684 static int
12685 base_breakpoint_insert_location (struct bp_location *bl)
12686 {
12687 internal_error_pure_virtual_called ();
12688 }
12689
12690 static int
12691 base_breakpoint_remove_location (struct bp_location *bl,
12692 enum remove_bp_reason reason)
12693 {
12694 internal_error_pure_virtual_called ();
12695 }
12696
12697 static int
12698 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12699 struct address_space *aspace,
12700 CORE_ADDR bp_addr,
12701 const struct target_waitstatus *ws)
12702 {
12703 internal_error_pure_virtual_called ();
12704 }
12705
12706 static void
12707 base_breakpoint_check_status (bpstat bs)
12708 {
12709 /* Always stop. */
12710 }
12711
12712 /* A "works_in_software_mode" breakpoint_ops method that just internal
12713 errors. */
12714
12715 static int
12716 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12717 {
12718 internal_error_pure_virtual_called ();
12719 }
12720
12721 /* A "resources_needed" breakpoint_ops method that just internal
12722 errors. */
12723
12724 static int
12725 base_breakpoint_resources_needed (const struct bp_location *bl)
12726 {
12727 internal_error_pure_virtual_called ();
12728 }
12729
12730 static enum print_stop_action
12731 base_breakpoint_print_it (bpstat bs)
12732 {
12733 internal_error_pure_virtual_called ();
12734 }
12735
12736 static void
12737 base_breakpoint_print_one_detail (const struct breakpoint *self,
12738 struct ui_out *uiout)
12739 {
12740 /* nothing */
12741 }
12742
12743 static void
12744 base_breakpoint_print_mention (struct breakpoint *b)
12745 {
12746 internal_error_pure_virtual_called ();
12747 }
12748
12749 static void
12750 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12751 {
12752 internal_error_pure_virtual_called ();
12753 }
12754
12755 static void
12756 base_breakpoint_create_sals_from_location
12757 (const struct event_location *location,
12758 struct linespec_result *canonical,
12759 enum bptype type_wanted)
12760 {
12761 internal_error_pure_virtual_called ();
12762 }
12763
12764 static void
12765 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12766 struct linespec_result *c,
12767 gdb::unique_xmalloc_ptr<char> cond_string,
12768 gdb::unique_xmalloc_ptr<char> extra_string,
12769 enum bptype type_wanted,
12770 enum bpdisp disposition,
12771 int thread,
12772 int task, int ignore_count,
12773 const struct breakpoint_ops *o,
12774 int from_tty, int enabled,
12775 int internal, unsigned flags)
12776 {
12777 internal_error_pure_virtual_called ();
12778 }
12779
12780 static std::vector<symtab_and_line>
12781 base_breakpoint_decode_location (struct breakpoint *b,
12782 const struct event_location *location,
12783 struct program_space *search_pspace)
12784 {
12785 internal_error_pure_virtual_called ();
12786 }
12787
12788 /* The default 'explains_signal' method. */
12789
12790 static int
12791 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12792 {
12793 return 1;
12794 }
12795
12796 /* The default "after_condition_true" method. */
12797
12798 static void
12799 base_breakpoint_after_condition_true (struct bpstats *bs)
12800 {
12801 /* Nothing to do. */
12802 }
12803
12804 struct breakpoint_ops base_breakpoint_ops =
12805 {
12806 base_breakpoint_allocate_location,
12807 base_breakpoint_re_set,
12808 base_breakpoint_insert_location,
12809 base_breakpoint_remove_location,
12810 base_breakpoint_breakpoint_hit,
12811 base_breakpoint_check_status,
12812 base_breakpoint_resources_needed,
12813 base_breakpoint_works_in_software_mode,
12814 base_breakpoint_print_it,
12815 NULL,
12816 base_breakpoint_print_one_detail,
12817 base_breakpoint_print_mention,
12818 base_breakpoint_print_recreate,
12819 base_breakpoint_create_sals_from_location,
12820 base_breakpoint_create_breakpoints_sal,
12821 base_breakpoint_decode_location,
12822 base_breakpoint_explains_signal,
12823 base_breakpoint_after_condition_true,
12824 };
12825
12826 /* Default breakpoint_ops methods. */
12827
12828 static void
12829 bkpt_re_set (struct breakpoint *b)
12830 {
12831 /* FIXME: is this still reachable? */
12832 if (breakpoint_event_location_empty_p (b))
12833 {
12834 /* Anything without a location can't be re-set. */
12835 delete_breakpoint (b);
12836 return;
12837 }
12838
12839 breakpoint_re_set_default (b);
12840 }
12841
12842 static int
12843 bkpt_insert_location (struct bp_location *bl)
12844 {
12845 CORE_ADDR addr = bl->target_info.reqstd_address;
12846
12847 bl->target_info.kind = breakpoint_kind (bl, &addr);
12848 bl->target_info.placed_address = addr;
12849
12850 if (bl->loc_type == bp_loc_hardware_breakpoint)
12851 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12852 else
12853 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12854 }
12855
12856 static int
12857 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12858 {
12859 if (bl->loc_type == bp_loc_hardware_breakpoint)
12860 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12861 else
12862 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12863 }
12864
12865 static int
12866 bkpt_breakpoint_hit (const struct bp_location *bl,
12867 struct address_space *aspace, CORE_ADDR bp_addr,
12868 const struct target_waitstatus *ws)
12869 {
12870 if (ws->kind != TARGET_WAITKIND_STOPPED
12871 || ws->value.sig != GDB_SIGNAL_TRAP)
12872 return 0;
12873
12874 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12875 aspace, bp_addr))
12876 return 0;
12877
12878 if (overlay_debugging /* unmapped overlay section */
12879 && section_is_overlay (bl->section)
12880 && !section_is_mapped (bl->section))
12881 return 0;
12882
12883 return 1;
12884 }
12885
12886 static int
12887 dprintf_breakpoint_hit (const struct bp_location *bl,
12888 struct address_space *aspace, CORE_ADDR bp_addr,
12889 const struct target_waitstatus *ws)
12890 {
12891 if (dprintf_style == dprintf_style_agent
12892 && target_can_run_breakpoint_commands ())
12893 {
12894 /* An agent-style dprintf never causes a stop. If we see a trap
12895 for this address it must be for a breakpoint that happens to
12896 be set at the same address. */
12897 return 0;
12898 }
12899
12900 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12901 }
12902
12903 static int
12904 bkpt_resources_needed (const struct bp_location *bl)
12905 {
12906 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12907
12908 return 1;
12909 }
12910
12911 static enum print_stop_action
12912 bkpt_print_it (bpstat bs)
12913 {
12914 struct breakpoint *b;
12915 const struct bp_location *bl;
12916 int bp_temp;
12917 struct ui_out *uiout = current_uiout;
12918
12919 gdb_assert (bs->bp_location_at != NULL);
12920
12921 bl = bs->bp_location_at;
12922 b = bs->breakpoint_at;
12923
12924 bp_temp = b->disposition == disp_del;
12925 if (bl->address != bl->requested_address)
12926 breakpoint_adjustment_warning (bl->requested_address,
12927 bl->address,
12928 b->number, 1);
12929 annotate_breakpoint (b->number);
12930 maybe_print_thread_hit_breakpoint (uiout);
12931
12932 if (bp_temp)
12933 uiout->text ("Temporary breakpoint ");
12934 else
12935 uiout->text ("Breakpoint ");
12936 if (uiout->is_mi_like_p ())
12937 {
12938 uiout->field_string ("reason",
12939 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12940 uiout->field_string ("disp", bpdisp_text (b->disposition));
12941 }
12942 uiout->field_int ("bkptno", b->number);
12943 uiout->text (", ");
12944
12945 return PRINT_SRC_AND_LOC;
12946 }
12947
12948 static void
12949 bkpt_print_mention (struct breakpoint *b)
12950 {
12951 if (current_uiout->is_mi_like_p ())
12952 return;
12953
12954 switch (b->type)
12955 {
12956 case bp_breakpoint:
12957 case bp_gnu_ifunc_resolver:
12958 if (b->disposition == disp_del)
12959 printf_filtered (_("Temporary breakpoint"));
12960 else
12961 printf_filtered (_("Breakpoint"));
12962 printf_filtered (_(" %d"), b->number);
12963 if (b->type == bp_gnu_ifunc_resolver)
12964 printf_filtered (_(" at gnu-indirect-function resolver"));
12965 break;
12966 case bp_hardware_breakpoint:
12967 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12968 break;
12969 case bp_dprintf:
12970 printf_filtered (_("Dprintf %d"), b->number);
12971 break;
12972 }
12973
12974 say_where (b);
12975 }
12976
12977 static void
12978 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12979 {
12980 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12981 fprintf_unfiltered (fp, "tbreak");
12982 else if (tp->type == bp_breakpoint)
12983 fprintf_unfiltered (fp, "break");
12984 else if (tp->type == bp_hardware_breakpoint
12985 && tp->disposition == disp_del)
12986 fprintf_unfiltered (fp, "thbreak");
12987 else if (tp->type == bp_hardware_breakpoint)
12988 fprintf_unfiltered (fp, "hbreak");
12989 else
12990 internal_error (__FILE__, __LINE__,
12991 _("unhandled breakpoint type %d"), (int) tp->type);
12992
12993 fprintf_unfiltered (fp, " %s",
12994 event_location_to_string (tp->location.get ()));
12995
12996 /* Print out extra_string if this breakpoint is pending. It might
12997 contain, for example, conditions that were set by the user. */
12998 if (tp->loc == NULL && tp->extra_string != NULL)
12999 fprintf_unfiltered (fp, " %s", tp->extra_string);
13000
13001 print_recreate_thread (tp, fp);
13002 }
13003
13004 static void
13005 bkpt_create_sals_from_location (const struct event_location *location,
13006 struct linespec_result *canonical,
13007 enum bptype type_wanted)
13008 {
13009 create_sals_from_location_default (location, canonical, type_wanted);
13010 }
13011
13012 static void
13013 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13014 struct linespec_result *canonical,
13015 gdb::unique_xmalloc_ptr<char> cond_string,
13016 gdb::unique_xmalloc_ptr<char> extra_string,
13017 enum bptype type_wanted,
13018 enum bpdisp disposition,
13019 int thread,
13020 int task, int ignore_count,
13021 const struct breakpoint_ops *ops,
13022 int from_tty, int enabled,
13023 int internal, unsigned flags)
13024 {
13025 create_breakpoints_sal_default (gdbarch, canonical,
13026 std::move (cond_string),
13027 std::move (extra_string),
13028 type_wanted,
13029 disposition, thread, task,
13030 ignore_count, ops, from_tty,
13031 enabled, internal, flags);
13032 }
13033
13034 static std::vector<symtab_and_line>
13035 bkpt_decode_location (struct breakpoint *b,
13036 const struct event_location *location,
13037 struct program_space *search_pspace)
13038 {
13039 return decode_location_default (b, location, search_pspace);
13040 }
13041
13042 /* Virtual table for internal breakpoints. */
13043
13044 static void
13045 internal_bkpt_re_set (struct breakpoint *b)
13046 {
13047 switch (b->type)
13048 {
13049 /* Delete overlay event and longjmp master breakpoints; they
13050 will be reset later by breakpoint_re_set. */
13051 case bp_overlay_event:
13052 case bp_longjmp_master:
13053 case bp_std_terminate_master:
13054 case bp_exception_master:
13055 delete_breakpoint (b);
13056 break;
13057
13058 /* This breakpoint is special, it's set up when the inferior
13059 starts and we really don't want to touch it. */
13060 case bp_shlib_event:
13061
13062 /* Like bp_shlib_event, this breakpoint type is special. Once
13063 it is set up, we do not want to touch it. */
13064 case bp_thread_event:
13065 break;
13066 }
13067 }
13068
13069 static void
13070 internal_bkpt_check_status (bpstat bs)
13071 {
13072 if (bs->breakpoint_at->type == bp_shlib_event)
13073 {
13074 /* If requested, stop when the dynamic linker notifies GDB of
13075 events. This allows the user to get control and place
13076 breakpoints in initializer routines for dynamically loaded
13077 objects (among other things). */
13078 bs->stop = stop_on_solib_events;
13079 bs->print = stop_on_solib_events;
13080 }
13081 else
13082 bs->stop = 0;
13083 }
13084
13085 static enum print_stop_action
13086 internal_bkpt_print_it (bpstat bs)
13087 {
13088 struct breakpoint *b;
13089
13090 b = bs->breakpoint_at;
13091
13092 switch (b->type)
13093 {
13094 case bp_shlib_event:
13095 /* Did we stop because the user set the stop_on_solib_events
13096 variable? (If so, we report this as a generic, "Stopped due
13097 to shlib event" message.) */
13098 print_solib_event (0);
13099 break;
13100
13101 case bp_thread_event:
13102 /* Not sure how we will get here.
13103 GDB should not stop for these breakpoints. */
13104 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13105 break;
13106
13107 case bp_overlay_event:
13108 /* By analogy with the thread event, GDB should not stop for these. */
13109 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13110 break;
13111
13112 case bp_longjmp_master:
13113 /* These should never be enabled. */
13114 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13115 break;
13116
13117 case bp_std_terminate_master:
13118 /* These should never be enabled. */
13119 printf_filtered (_("std::terminate Master Breakpoint: "
13120 "gdb should not stop!\n"));
13121 break;
13122
13123 case bp_exception_master:
13124 /* These should never be enabled. */
13125 printf_filtered (_("Exception Master Breakpoint: "
13126 "gdb should not stop!\n"));
13127 break;
13128 }
13129
13130 return PRINT_NOTHING;
13131 }
13132
13133 static void
13134 internal_bkpt_print_mention (struct breakpoint *b)
13135 {
13136 /* Nothing to mention. These breakpoints are internal. */
13137 }
13138
13139 /* Virtual table for momentary breakpoints */
13140
13141 static void
13142 momentary_bkpt_re_set (struct breakpoint *b)
13143 {
13144 /* Keep temporary breakpoints, which can be encountered when we step
13145 over a dlopen call and solib_add is resetting the breakpoints.
13146 Otherwise these should have been blown away via the cleanup chain
13147 or by breakpoint_init_inferior when we rerun the executable. */
13148 }
13149
13150 static void
13151 momentary_bkpt_check_status (bpstat bs)
13152 {
13153 /* Nothing. The point of these breakpoints is causing a stop. */
13154 }
13155
13156 static enum print_stop_action
13157 momentary_bkpt_print_it (bpstat bs)
13158 {
13159 return PRINT_UNKNOWN;
13160 }
13161
13162 static void
13163 momentary_bkpt_print_mention (struct breakpoint *b)
13164 {
13165 /* Nothing to mention. These breakpoints are internal. */
13166 }
13167
13168 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13169
13170 It gets cleared already on the removal of the first one of such placed
13171 breakpoints. This is OK as they get all removed altogether. */
13172
13173 longjmp_breakpoint::~longjmp_breakpoint ()
13174 {
13175 thread_info *tp = find_thread_global_id (this->thread);
13176
13177 if (tp != NULL)
13178 tp->initiating_frame = null_frame_id;
13179 }
13180
13181 /* Specific methods for probe breakpoints. */
13182
13183 static int
13184 bkpt_probe_insert_location (struct bp_location *bl)
13185 {
13186 int v = bkpt_insert_location (bl);
13187
13188 if (v == 0)
13189 {
13190 /* The insertion was successful, now let's set the probe's semaphore
13191 if needed. */
13192 if (bl->probe.probe->pops->set_semaphore != NULL)
13193 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13194 bl->probe.objfile,
13195 bl->gdbarch);
13196 }
13197
13198 return v;
13199 }
13200
13201 static int
13202 bkpt_probe_remove_location (struct bp_location *bl,
13203 enum remove_bp_reason reason)
13204 {
13205 /* Let's clear the semaphore before removing the location. */
13206 if (bl->probe.probe->pops->clear_semaphore != NULL)
13207 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13208 bl->probe.objfile,
13209 bl->gdbarch);
13210
13211 return bkpt_remove_location (bl, reason);
13212 }
13213
13214 static void
13215 bkpt_probe_create_sals_from_location (const struct event_location *location,
13216 struct linespec_result *canonical,
13217 enum bptype type_wanted)
13218 {
13219 struct linespec_sals lsal;
13220
13221 lsal.sals = parse_probes (location, NULL, canonical);
13222 lsal.canonical
13223 = xstrdup (event_location_to_string (canonical->location.get ()));
13224 canonical->lsals.push_back (std::move (lsal));
13225 }
13226
13227 static std::vector<symtab_and_line>
13228 bkpt_probe_decode_location (struct breakpoint *b,
13229 const struct event_location *location,
13230 struct program_space *search_pspace)
13231 {
13232 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13233 if (sals.empty ())
13234 error (_("probe not found"));
13235 return sals;
13236 }
13237
13238 /* The breakpoint_ops structure to be used in tracepoints. */
13239
13240 static void
13241 tracepoint_re_set (struct breakpoint *b)
13242 {
13243 breakpoint_re_set_default (b);
13244 }
13245
13246 static int
13247 tracepoint_breakpoint_hit (const struct bp_location *bl,
13248 struct address_space *aspace, CORE_ADDR bp_addr,
13249 const struct target_waitstatus *ws)
13250 {
13251 /* By definition, the inferior does not report stops at
13252 tracepoints. */
13253 return 0;
13254 }
13255
13256 static void
13257 tracepoint_print_one_detail (const struct breakpoint *self,
13258 struct ui_out *uiout)
13259 {
13260 struct tracepoint *tp = (struct tracepoint *) self;
13261 if (tp->static_trace_marker_id)
13262 {
13263 gdb_assert (self->type == bp_static_tracepoint);
13264
13265 uiout->text ("\tmarker id is ");
13266 uiout->field_string ("static-tracepoint-marker-string-id",
13267 tp->static_trace_marker_id);
13268 uiout->text ("\n");
13269 }
13270 }
13271
13272 static void
13273 tracepoint_print_mention (struct breakpoint *b)
13274 {
13275 if (current_uiout->is_mi_like_p ())
13276 return;
13277
13278 switch (b->type)
13279 {
13280 case bp_tracepoint:
13281 printf_filtered (_("Tracepoint"));
13282 printf_filtered (_(" %d"), b->number);
13283 break;
13284 case bp_fast_tracepoint:
13285 printf_filtered (_("Fast tracepoint"));
13286 printf_filtered (_(" %d"), b->number);
13287 break;
13288 case bp_static_tracepoint:
13289 printf_filtered (_("Static tracepoint"));
13290 printf_filtered (_(" %d"), b->number);
13291 break;
13292 default:
13293 internal_error (__FILE__, __LINE__,
13294 _("unhandled tracepoint type %d"), (int) b->type);
13295 }
13296
13297 say_where (b);
13298 }
13299
13300 static void
13301 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13302 {
13303 struct tracepoint *tp = (struct tracepoint *) self;
13304
13305 if (self->type == bp_fast_tracepoint)
13306 fprintf_unfiltered (fp, "ftrace");
13307 else if (self->type == bp_static_tracepoint)
13308 fprintf_unfiltered (fp, "strace");
13309 else if (self->type == bp_tracepoint)
13310 fprintf_unfiltered (fp, "trace");
13311 else
13312 internal_error (__FILE__, __LINE__,
13313 _("unhandled tracepoint type %d"), (int) self->type);
13314
13315 fprintf_unfiltered (fp, " %s",
13316 event_location_to_string (self->location.get ()));
13317 print_recreate_thread (self, fp);
13318
13319 if (tp->pass_count)
13320 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13321 }
13322
13323 static void
13324 tracepoint_create_sals_from_location (const struct event_location *location,
13325 struct linespec_result *canonical,
13326 enum bptype type_wanted)
13327 {
13328 create_sals_from_location_default (location, canonical, type_wanted);
13329 }
13330
13331 static void
13332 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13333 struct linespec_result *canonical,
13334 gdb::unique_xmalloc_ptr<char> cond_string,
13335 gdb::unique_xmalloc_ptr<char> extra_string,
13336 enum bptype type_wanted,
13337 enum bpdisp disposition,
13338 int thread,
13339 int task, int ignore_count,
13340 const struct breakpoint_ops *ops,
13341 int from_tty, int enabled,
13342 int internal, unsigned flags)
13343 {
13344 create_breakpoints_sal_default (gdbarch, canonical,
13345 std::move (cond_string),
13346 std::move (extra_string),
13347 type_wanted,
13348 disposition, thread, task,
13349 ignore_count, ops, from_tty,
13350 enabled, internal, flags);
13351 }
13352
13353 static std::vector<symtab_and_line>
13354 tracepoint_decode_location (struct breakpoint *b,
13355 const struct event_location *location,
13356 struct program_space *search_pspace)
13357 {
13358 return decode_location_default (b, location, search_pspace);
13359 }
13360
13361 struct breakpoint_ops tracepoint_breakpoint_ops;
13362
13363 /* The breakpoint_ops structure to be use on tracepoints placed in a
13364 static probe. */
13365
13366 static void
13367 tracepoint_probe_create_sals_from_location
13368 (const struct event_location *location,
13369 struct linespec_result *canonical,
13370 enum bptype type_wanted)
13371 {
13372 /* We use the same method for breakpoint on probes. */
13373 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13374 }
13375
13376 static std::vector<symtab_and_line>
13377 tracepoint_probe_decode_location (struct breakpoint *b,
13378 const struct event_location *location,
13379 struct program_space *search_pspace)
13380 {
13381 /* We use the same method for breakpoint on probes. */
13382 return bkpt_probe_decode_location (b, location, search_pspace);
13383 }
13384
13385 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13386
13387 /* Dprintf breakpoint_ops methods. */
13388
13389 static void
13390 dprintf_re_set (struct breakpoint *b)
13391 {
13392 breakpoint_re_set_default (b);
13393
13394 /* extra_string should never be non-NULL for dprintf. */
13395 gdb_assert (b->extra_string != NULL);
13396
13397 /* 1 - connect to target 1, that can run breakpoint commands.
13398 2 - create a dprintf, which resolves fine.
13399 3 - disconnect from target 1
13400 4 - connect to target 2, that can NOT run breakpoint commands.
13401
13402 After steps #3/#4, you'll want the dprintf command list to
13403 be updated, because target 1 and 2 may well return different
13404 answers for target_can_run_breakpoint_commands().
13405 Given absence of finer grained resetting, we get to do
13406 it all the time. */
13407 if (b->extra_string != NULL)
13408 update_dprintf_command_list (b);
13409 }
13410
13411 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13412
13413 static void
13414 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13415 {
13416 fprintf_unfiltered (fp, "dprintf %s,%s",
13417 event_location_to_string (tp->location.get ()),
13418 tp->extra_string);
13419 print_recreate_thread (tp, fp);
13420 }
13421
13422 /* Implement the "after_condition_true" breakpoint_ops method for
13423 dprintf.
13424
13425 dprintf's are implemented with regular commands in their command
13426 list, but we run the commands here instead of before presenting the
13427 stop to the user, as dprintf's don't actually cause a stop. This
13428 also makes it so that the commands of multiple dprintfs at the same
13429 address are all handled. */
13430
13431 static void
13432 dprintf_after_condition_true (struct bpstats *bs)
13433 {
13434 struct bpstats tmp_bs;
13435 struct bpstats *tmp_bs_p = &tmp_bs;
13436
13437 /* dprintf's never cause a stop. This wasn't set in the
13438 check_status hook instead because that would make the dprintf's
13439 condition not be evaluated. */
13440 bs->stop = 0;
13441
13442 /* Run the command list here. Take ownership of it instead of
13443 copying. We never want these commands to run later in
13444 bpstat_do_actions, if a breakpoint that causes a stop happens to
13445 be set at same address as this dprintf, or even if running the
13446 commands here throws. */
13447 tmp_bs.commands = bs->commands;
13448 bs->commands = NULL;
13449
13450 bpstat_do_actions_1 (&tmp_bs_p);
13451
13452 /* 'tmp_bs.commands' will usually be NULL by now, but
13453 bpstat_do_actions_1 may return early without processing the whole
13454 list. */
13455 }
13456
13457 /* The breakpoint_ops structure to be used on static tracepoints with
13458 markers (`-m'). */
13459
13460 static void
13461 strace_marker_create_sals_from_location (const struct event_location *location,
13462 struct linespec_result *canonical,
13463 enum bptype type_wanted)
13464 {
13465 struct linespec_sals lsal;
13466 const char *arg_start, *arg;
13467 char *str;
13468 struct cleanup *cleanup;
13469
13470 arg = arg_start = get_linespec_location (location);
13471 lsal.sals = decode_static_tracepoint_spec (&arg);
13472
13473 str = savestring (arg_start, arg - arg_start);
13474 cleanup = make_cleanup (xfree, str);
13475 canonical->location = new_linespec_location (&str);
13476 do_cleanups (cleanup);
13477
13478 lsal.canonical
13479 = xstrdup (event_location_to_string (canonical->location.get ()));
13480 canonical->lsals.push_back (std::move (lsal));
13481 }
13482
13483 static void
13484 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13485 struct linespec_result *canonical,
13486 gdb::unique_xmalloc_ptr<char> cond_string,
13487 gdb::unique_xmalloc_ptr<char> extra_string,
13488 enum bptype type_wanted,
13489 enum bpdisp disposition,
13490 int thread,
13491 int task, int ignore_count,
13492 const struct breakpoint_ops *ops,
13493 int from_tty, int enabled,
13494 int internal, unsigned flags)
13495 {
13496 const linespec_sals &lsal = canonical->lsals[0];
13497
13498 /* If the user is creating a static tracepoint by marker id
13499 (strace -m MARKER_ID), then store the sals index, so that
13500 breakpoint_re_set can try to match up which of the newly
13501 found markers corresponds to this one, and, don't try to
13502 expand multiple locations for each sal, given than SALS
13503 already should contain all sals for MARKER_ID. */
13504
13505 for (size_t i = 0; i < lsal.sals.size (); i++)
13506 {
13507 event_location_up location
13508 = copy_event_location (canonical->location.get ());
13509
13510 std::unique_ptr<tracepoint> tp (new tracepoint ());
13511 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
13512 std::move (location), NULL,
13513 std::move (cond_string),
13514 std::move (extra_string),
13515 type_wanted, disposition,
13516 thread, task, ignore_count, ops,
13517 from_tty, enabled, internal, flags,
13518 canonical->special_display);
13519 /* Given that its possible to have multiple markers with
13520 the same string id, if the user is creating a static
13521 tracepoint by marker id ("strace -m MARKER_ID"), then
13522 store the sals index, so that breakpoint_re_set can
13523 try to match up which of the newly found markers
13524 corresponds to this one */
13525 tp->static_trace_marker_id_idx = i;
13526
13527 install_breakpoint (internal, std::move (tp), 0);
13528 }
13529 }
13530
13531 static std::vector<symtab_and_line>
13532 strace_marker_decode_location (struct breakpoint *b,
13533 const struct event_location *location,
13534 struct program_space *search_pspace)
13535 {
13536 struct tracepoint *tp = (struct tracepoint *) b;
13537 const char *s = get_linespec_location (location);
13538
13539 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13540 if (sals.size () > tp->static_trace_marker_id_idx)
13541 {
13542 sals[0] = sals[tp->static_trace_marker_id_idx];
13543 sals.resize (1);
13544 return sals;
13545 }
13546 else
13547 error (_("marker %s not found"), tp->static_trace_marker_id);
13548 }
13549
13550 static struct breakpoint_ops strace_marker_breakpoint_ops;
13551
13552 static int
13553 strace_marker_p (struct breakpoint *b)
13554 {
13555 return b->ops == &strace_marker_breakpoint_ops;
13556 }
13557
13558 /* Delete a breakpoint and clean up all traces of it in the data
13559 structures. */
13560
13561 void
13562 delete_breakpoint (struct breakpoint *bpt)
13563 {
13564 struct breakpoint *b;
13565
13566 gdb_assert (bpt != NULL);
13567
13568 /* Has this bp already been deleted? This can happen because
13569 multiple lists can hold pointers to bp's. bpstat lists are
13570 especial culprits.
13571
13572 One example of this happening is a watchpoint's scope bp. When
13573 the scope bp triggers, we notice that the watchpoint is out of
13574 scope, and delete it. We also delete its scope bp. But the
13575 scope bp is marked "auto-deleting", and is already on a bpstat.
13576 That bpstat is then checked for auto-deleting bp's, which are
13577 deleted.
13578
13579 A real solution to this problem might involve reference counts in
13580 bp's, and/or giving them pointers back to their referencing
13581 bpstat's, and teaching delete_breakpoint to only free a bp's
13582 storage when no more references were extent. A cheaper bandaid
13583 was chosen. */
13584 if (bpt->type == bp_none)
13585 return;
13586
13587 /* At least avoid this stale reference until the reference counting
13588 of breakpoints gets resolved. */
13589 if (bpt->related_breakpoint != bpt)
13590 {
13591 struct breakpoint *related;
13592 struct watchpoint *w;
13593
13594 if (bpt->type == bp_watchpoint_scope)
13595 w = (struct watchpoint *) bpt->related_breakpoint;
13596 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13597 w = (struct watchpoint *) bpt;
13598 else
13599 w = NULL;
13600 if (w != NULL)
13601 watchpoint_del_at_next_stop (w);
13602
13603 /* Unlink bpt from the bpt->related_breakpoint ring. */
13604 for (related = bpt; related->related_breakpoint != bpt;
13605 related = related->related_breakpoint);
13606 related->related_breakpoint = bpt->related_breakpoint;
13607 bpt->related_breakpoint = bpt;
13608 }
13609
13610 /* watch_command_1 creates a watchpoint but only sets its number if
13611 update_watchpoint succeeds in creating its bp_locations. If there's
13612 a problem in that process, we'll be asked to delete the half-created
13613 watchpoint. In that case, don't announce the deletion. */
13614 if (bpt->number)
13615 observer_notify_breakpoint_deleted (bpt);
13616
13617 if (breakpoint_chain == bpt)
13618 breakpoint_chain = bpt->next;
13619
13620 ALL_BREAKPOINTS (b)
13621 if (b->next == bpt)
13622 {
13623 b->next = bpt->next;
13624 break;
13625 }
13626
13627 /* Be sure no bpstat's are pointing at the breakpoint after it's
13628 been freed. */
13629 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13630 in all threads for now. Note that we cannot just remove bpstats
13631 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13632 commands are associated with the bpstat; if we remove it here,
13633 then the later call to bpstat_do_actions (&stop_bpstat); in
13634 event-top.c won't do anything, and temporary breakpoints with
13635 commands won't work. */
13636
13637 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13638
13639 /* Now that breakpoint is removed from breakpoint list, update the
13640 global location list. This will remove locations that used to
13641 belong to this breakpoint. Do this before freeing the breakpoint
13642 itself, since remove_breakpoint looks at location's owner. It
13643 might be better design to have location completely
13644 self-contained, but it's not the case now. */
13645 update_global_location_list (UGLL_DONT_INSERT);
13646
13647 /* On the chance that someone will soon try again to delete this
13648 same bp, we mark it as deleted before freeing its storage. */
13649 bpt->type = bp_none;
13650 delete bpt;
13651 }
13652
13653 static void
13654 do_delete_breakpoint_cleanup (void *b)
13655 {
13656 delete_breakpoint ((struct breakpoint *) b);
13657 }
13658
13659 struct cleanup *
13660 make_cleanup_delete_breakpoint (struct breakpoint *b)
13661 {
13662 return make_cleanup (do_delete_breakpoint_cleanup, b);
13663 }
13664
13665 /* Iterator function to call a user-provided callback function once
13666 for each of B and its related breakpoints. */
13667
13668 static void
13669 iterate_over_related_breakpoints (struct breakpoint *b,
13670 void (*function) (struct breakpoint *,
13671 void *),
13672 void *data)
13673 {
13674 struct breakpoint *related;
13675
13676 related = b;
13677 do
13678 {
13679 struct breakpoint *next;
13680
13681 /* FUNCTION may delete RELATED. */
13682 next = related->related_breakpoint;
13683
13684 if (next == related)
13685 {
13686 /* RELATED is the last ring entry. */
13687 function (related, data);
13688
13689 /* FUNCTION may have deleted it, so we'd never reach back to
13690 B. There's nothing left to do anyway, so just break
13691 out. */
13692 break;
13693 }
13694 else
13695 function (related, data);
13696
13697 related = next;
13698 }
13699 while (related != b);
13700 }
13701
13702 static void
13703 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13704 {
13705 delete_breakpoint (b);
13706 }
13707
13708 /* A callback for map_breakpoint_numbers that calls
13709 delete_breakpoint. */
13710
13711 static void
13712 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13713 {
13714 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13715 }
13716
13717 void
13718 delete_command (char *arg, int from_tty)
13719 {
13720 struct breakpoint *b, *b_tmp;
13721
13722 dont_repeat ();
13723
13724 if (arg == 0)
13725 {
13726 int breaks_to_delete = 0;
13727
13728 /* Delete all breakpoints if no argument. Do not delete
13729 internal breakpoints, these have to be deleted with an
13730 explicit breakpoint number argument. */
13731 ALL_BREAKPOINTS (b)
13732 if (user_breakpoint_p (b))
13733 {
13734 breaks_to_delete = 1;
13735 break;
13736 }
13737
13738 /* Ask user only if there are some breakpoints to delete. */
13739 if (!from_tty
13740 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13741 {
13742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13743 if (user_breakpoint_p (b))
13744 delete_breakpoint (b);
13745 }
13746 }
13747 else
13748 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13749 }
13750
13751 /* Return true if all locations of B bound to PSPACE are pending. If
13752 PSPACE is NULL, all locations of all program spaces are
13753 considered. */
13754
13755 static int
13756 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13757 {
13758 struct bp_location *loc;
13759
13760 for (loc = b->loc; loc != NULL; loc = loc->next)
13761 if ((pspace == NULL
13762 || loc->pspace == pspace)
13763 && !loc->shlib_disabled
13764 && !loc->pspace->executing_startup)
13765 return 0;
13766 return 1;
13767 }
13768
13769 /* Subroutine of update_breakpoint_locations to simplify it.
13770 Return non-zero if multiple fns in list LOC have the same name.
13771 Null names are ignored. */
13772
13773 static int
13774 ambiguous_names_p (struct bp_location *loc)
13775 {
13776 struct bp_location *l;
13777 htab_t htab = htab_create_alloc (13, htab_hash_string,
13778 (int (*) (const void *,
13779 const void *)) streq,
13780 NULL, xcalloc, xfree);
13781
13782 for (l = loc; l != NULL; l = l->next)
13783 {
13784 const char **slot;
13785 const char *name = l->function_name;
13786
13787 /* Allow for some names to be NULL, ignore them. */
13788 if (name == NULL)
13789 continue;
13790
13791 slot = (const char **) htab_find_slot (htab, (const void *) name,
13792 INSERT);
13793 /* NOTE: We can assume slot != NULL here because xcalloc never
13794 returns NULL. */
13795 if (*slot != NULL)
13796 {
13797 htab_delete (htab);
13798 return 1;
13799 }
13800 *slot = name;
13801 }
13802
13803 htab_delete (htab);
13804 return 0;
13805 }
13806
13807 /* When symbols change, it probably means the sources changed as well,
13808 and it might mean the static tracepoint markers are no longer at
13809 the same address or line numbers they used to be at last we
13810 checked. Losing your static tracepoints whenever you rebuild is
13811 undesirable. This function tries to resync/rematch gdb static
13812 tracepoints with the markers on the target, for static tracepoints
13813 that have not been set by marker id. Static tracepoint that have
13814 been set by marker id are reset by marker id in breakpoint_re_set.
13815 The heuristic is:
13816
13817 1) For a tracepoint set at a specific address, look for a marker at
13818 the old PC. If one is found there, assume to be the same marker.
13819 If the name / string id of the marker found is different from the
13820 previous known name, assume that means the user renamed the marker
13821 in the sources, and output a warning.
13822
13823 2) For a tracepoint set at a given line number, look for a marker
13824 at the new address of the old line number. If one is found there,
13825 assume to be the same marker. If the name / string id of the
13826 marker found is different from the previous known name, assume that
13827 means the user renamed the marker in the sources, and output a
13828 warning.
13829
13830 3) If a marker is no longer found at the same address or line, it
13831 may mean the marker no longer exists. But it may also just mean
13832 the code changed a bit. Maybe the user added a few lines of code
13833 that made the marker move up or down (in line number terms). Ask
13834 the target for info about the marker with the string id as we knew
13835 it. If found, update line number and address in the matching
13836 static tracepoint. This will get confused if there's more than one
13837 marker with the same ID (possible in UST, although unadvised
13838 precisely because it confuses tools). */
13839
13840 static struct symtab_and_line
13841 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13842 {
13843 struct tracepoint *tp = (struct tracepoint *) b;
13844 struct static_tracepoint_marker marker;
13845 CORE_ADDR pc;
13846
13847 pc = sal.pc;
13848 if (sal.line)
13849 find_line_pc (sal.symtab, sal.line, &pc);
13850
13851 if (target_static_tracepoint_marker_at (pc, &marker))
13852 {
13853 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13854 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13855 b->number,
13856 tp->static_trace_marker_id, marker.str_id);
13857
13858 xfree (tp->static_trace_marker_id);
13859 tp->static_trace_marker_id = xstrdup (marker.str_id);
13860 release_static_tracepoint_marker (&marker);
13861
13862 return sal;
13863 }
13864
13865 /* Old marker wasn't found on target at lineno. Try looking it up
13866 by string ID. */
13867 if (!sal.explicit_pc
13868 && sal.line != 0
13869 && sal.symtab != NULL
13870 && tp->static_trace_marker_id != NULL)
13871 {
13872 VEC(static_tracepoint_marker_p) *markers;
13873
13874 markers
13875 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13876
13877 if (!VEC_empty(static_tracepoint_marker_p, markers))
13878 {
13879 struct symbol *sym;
13880 struct static_tracepoint_marker *tpmarker;
13881 struct ui_out *uiout = current_uiout;
13882 struct explicit_location explicit_loc;
13883
13884 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13885
13886 xfree (tp->static_trace_marker_id);
13887 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13888
13889 warning (_("marker for static tracepoint %d (%s) not "
13890 "found at previous line number"),
13891 b->number, tp->static_trace_marker_id);
13892
13893 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
13894 sym = find_pc_sect_function (tpmarker->address, NULL);
13895 uiout->text ("Now in ");
13896 if (sym)
13897 {
13898 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13899 uiout->text (" at ");
13900 }
13901 uiout->field_string ("file",
13902 symtab_to_filename_for_display (sal2.symtab));
13903 uiout->text (":");
13904
13905 if (uiout->is_mi_like_p ())
13906 {
13907 const char *fullname = symtab_to_fullname (sal2.symtab);
13908
13909 uiout->field_string ("fullname", fullname);
13910 }
13911
13912 uiout->field_int ("line", sal2.line);
13913 uiout->text ("\n");
13914
13915 b->loc->line_number = sal2.line;
13916 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13917
13918 b->location.reset (NULL);
13919 initialize_explicit_location (&explicit_loc);
13920 explicit_loc.source_filename
13921 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13922 explicit_loc.line_offset.offset = b->loc->line_number;
13923 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13924 b->location = new_explicit_location (&explicit_loc);
13925
13926 /* Might be nice to check if function changed, and warn if
13927 so. */
13928
13929 release_static_tracepoint_marker (tpmarker);
13930 }
13931 }
13932 return sal;
13933 }
13934
13935 /* Returns 1 iff locations A and B are sufficiently same that
13936 we don't need to report breakpoint as changed. */
13937
13938 static int
13939 locations_are_equal (struct bp_location *a, struct bp_location *b)
13940 {
13941 while (a && b)
13942 {
13943 if (a->address != b->address)
13944 return 0;
13945
13946 if (a->shlib_disabled != b->shlib_disabled)
13947 return 0;
13948
13949 if (a->enabled != b->enabled)
13950 return 0;
13951
13952 a = a->next;
13953 b = b->next;
13954 }
13955
13956 if ((a == NULL) != (b == NULL))
13957 return 0;
13958
13959 return 1;
13960 }
13961
13962 /* Split all locations of B that are bound to PSPACE out of B's
13963 location list to a separate list and return that list's head. If
13964 PSPACE is NULL, hoist out all locations of B. */
13965
13966 static struct bp_location *
13967 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13968 {
13969 struct bp_location head;
13970 struct bp_location *i = b->loc;
13971 struct bp_location **i_link = &b->loc;
13972 struct bp_location *hoisted = &head;
13973
13974 if (pspace == NULL)
13975 {
13976 i = b->loc;
13977 b->loc = NULL;
13978 return i;
13979 }
13980
13981 head.next = NULL;
13982
13983 while (i != NULL)
13984 {
13985 if (i->pspace == pspace)
13986 {
13987 *i_link = i->next;
13988 i->next = NULL;
13989 hoisted->next = i;
13990 hoisted = i;
13991 }
13992 else
13993 i_link = &i->next;
13994 i = *i_link;
13995 }
13996
13997 return head.next;
13998 }
13999
14000 /* Create new breakpoint locations for B (a hardware or software
14001 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14002 zero, then B is a ranged breakpoint. Only recreates locations for
14003 FILTER_PSPACE. Locations of other program spaces are left
14004 untouched. */
14005
14006 void
14007 update_breakpoint_locations (struct breakpoint *b,
14008 struct program_space *filter_pspace,
14009 gdb::array_view<const symtab_and_line> sals,
14010 gdb::array_view<const symtab_and_line> sals_end)
14011 {
14012 int i;
14013 struct bp_location *existing_locations;
14014
14015 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
14016 {
14017 /* Ranged breakpoints have only one start location and one end
14018 location. */
14019 b->enable_state = bp_disabled;
14020 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14021 "multiple locations found\n"),
14022 b->number);
14023 return;
14024 }
14025
14026 /* If there's no new locations, and all existing locations are
14027 pending, don't do anything. This optimizes the common case where
14028 all locations are in the same shared library, that was unloaded.
14029 We'd like to retain the location, so that when the library is
14030 loaded again, we don't loose the enabled/disabled status of the
14031 individual locations. */
14032 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
14033 return;
14034
14035 existing_locations = hoist_existing_locations (b, filter_pspace);
14036
14037 for (const auto &sal : sals)
14038 {
14039 struct bp_location *new_loc;
14040
14041 switch_to_program_space_and_thread (sal.pspace);
14042
14043 new_loc = add_location_to_breakpoint (b, &sal);
14044
14045 /* Reparse conditions, they might contain references to the
14046 old symtab. */
14047 if (b->cond_string != NULL)
14048 {
14049 const char *s;
14050
14051 s = b->cond_string;
14052 TRY
14053 {
14054 new_loc->cond = parse_exp_1 (&s, sal.pc,
14055 block_for_pc (sal.pc),
14056 0);
14057 }
14058 CATCH (e, RETURN_MASK_ERROR)
14059 {
14060 warning (_("failed to reevaluate condition "
14061 "for breakpoint %d: %s"),
14062 b->number, e.message);
14063 new_loc->enabled = 0;
14064 }
14065 END_CATCH
14066 }
14067
14068 if (!sals_end.empty ())
14069 {
14070 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
14071
14072 new_loc->length = end - sals[0].pc + 1;
14073 }
14074 }
14075
14076 /* If possible, carry over 'disable' status from existing
14077 breakpoints. */
14078 {
14079 struct bp_location *e = existing_locations;
14080 /* If there are multiple breakpoints with the same function name,
14081 e.g. for inline functions, comparing function names won't work.
14082 Instead compare pc addresses; this is just a heuristic as things
14083 may have moved, but in practice it gives the correct answer
14084 often enough until a better solution is found. */
14085 int have_ambiguous_names = ambiguous_names_p (b->loc);
14086
14087 for (; e; e = e->next)
14088 {
14089 if (!e->enabled && e->function_name)
14090 {
14091 struct bp_location *l = b->loc;
14092 if (have_ambiguous_names)
14093 {
14094 for (; l; l = l->next)
14095 if (breakpoint_locations_match (e, l))
14096 {
14097 l->enabled = 0;
14098 break;
14099 }
14100 }
14101 else
14102 {
14103 for (; l; l = l->next)
14104 if (l->function_name
14105 && strcmp (e->function_name, l->function_name) == 0)
14106 {
14107 l->enabled = 0;
14108 break;
14109 }
14110 }
14111 }
14112 }
14113 }
14114
14115 if (!locations_are_equal (existing_locations, b->loc))
14116 observer_notify_breakpoint_modified (b);
14117 }
14118
14119 /* Find the SaL locations corresponding to the given LOCATION.
14120 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14121
14122 static std::vector<symtab_and_line>
14123 location_to_sals (struct breakpoint *b, struct event_location *location,
14124 struct program_space *search_pspace, int *found)
14125 {
14126 struct gdb_exception exception = exception_none;
14127
14128 gdb_assert (b->ops != NULL);
14129
14130 std::vector<symtab_and_line> sals;
14131
14132 TRY
14133 {
14134 sals = b->ops->decode_location (b, location, search_pspace);
14135 }
14136 CATCH (e, RETURN_MASK_ERROR)
14137 {
14138 int not_found_and_ok = 0;
14139
14140 exception = e;
14141
14142 /* For pending breakpoints, it's expected that parsing will
14143 fail until the right shared library is loaded. User has
14144 already told to create pending breakpoints and don't need
14145 extra messages. If breakpoint is in bp_shlib_disabled
14146 state, then user already saw the message about that
14147 breakpoint being disabled, and don't want to see more
14148 errors. */
14149 if (e.error == NOT_FOUND_ERROR
14150 && (b->condition_not_parsed
14151 || (b->loc != NULL
14152 && search_pspace != NULL
14153 && b->loc->pspace != search_pspace)
14154 || (b->loc && b->loc->shlib_disabled)
14155 || (b->loc && b->loc->pspace->executing_startup)
14156 || b->enable_state == bp_disabled))
14157 not_found_and_ok = 1;
14158
14159 if (!not_found_and_ok)
14160 {
14161 /* We surely don't want to warn about the same breakpoint
14162 10 times. One solution, implemented here, is disable
14163 the breakpoint on error. Another solution would be to
14164 have separate 'warning emitted' flag. Since this
14165 happens only when a binary has changed, I don't know
14166 which approach is better. */
14167 b->enable_state = bp_disabled;
14168 throw_exception (e);
14169 }
14170 }
14171 END_CATCH
14172
14173 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14174 {
14175 for (auto &sal : sals)
14176 resolve_sal_pc (&sal);
14177 if (b->condition_not_parsed && b->extra_string != NULL)
14178 {
14179 char *cond_string, *extra_string;
14180 int thread, task;
14181
14182 find_condition_and_thread (b->extra_string, sals[0].pc,
14183 &cond_string, &thread, &task,
14184 &extra_string);
14185 gdb_assert (b->cond_string == NULL);
14186 if (cond_string)
14187 b->cond_string = cond_string;
14188 b->thread = thread;
14189 b->task = task;
14190 if (extra_string)
14191 {
14192 xfree (b->extra_string);
14193 b->extra_string = extra_string;
14194 }
14195 b->condition_not_parsed = 0;
14196 }
14197
14198 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14199 sals[0] = update_static_tracepoint (b, sals[0]);
14200
14201 *found = 1;
14202 }
14203 else
14204 *found = 0;
14205
14206 return sals;
14207 }
14208
14209 /* The default re_set method, for typical hardware or software
14210 breakpoints. Reevaluate the breakpoint and recreate its
14211 locations. */
14212
14213 static void
14214 breakpoint_re_set_default (struct breakpoint *b)
14215 {
14216 struct program_space *filter_pspace = current_program_space;
14217 std::vector<symtab_and_line> expanded, expanded_end;
14218
14219 int found;
14220 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14221 filter_pspace, &found);
14222 if (found)
14223 expanded = std::move (sals);
14224
14225 if (b->location_range_end != NULL)
14226 {
14227 std::vector<symtab_and_line> sals_end
14228 = location_to_sals (b, b->location_range_end.get (),
14229 filter_pspace, &found);
14230 if (found)
14231 expanded_end = std::move (sals_end);
14232 }
14233
14234 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14235 }
14236
14237 /* Default method for creating SALs from an address string. It basically
14238 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14239
14240 static void
14241 create_sals_from_location_default (const struct event_location *location,
14242 struct linespec_result *canonical,
14243 enum bptype type_wanted)
14244 {
14245 parse_breakpoint_sals (location, canonical);
14246 }
14247
14248 /* Call create_breakpoints_sal for the given arguments. This is the default
14249 function for the `create_breakpoints_sal' method of
14250 breakpoint_ops. */
14251
14252 static void
14253 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14254 struct linespec_result *canonical,
14255 gdb::unique_xmalloc_ptr<char> cond_string,
14256 gdb::unique_xmalloc_ptr<char> extra_string,
14257 enum bptype type_wanted,
14258 enum bpdisp disposition,
14259 int thread,
14260 int task, int ignore_count,
14261 const struct breakpoint_ops *ops,
14262 int from_tty, int enabled,
14263 int internal, unsigned flags)
14264 {
14265 create_breakpoints_sal (gdbarch, canonical,
14266 std::move (cond_string),
14267 std::move (extra_string),
14268 type_wanted, disposition,
14269 thread, task, ignore_count, ops, from_tty,
14270 enabled, internal, flags);
14271 }
14272
14273 /* Decode the line represented by S by calling decode_line_full. This is the
14274 default function for the `decode_location' method of breakpoint_ops. */
14275
14276 static std::vector<symtab_and_line>
14277 decode_location_default (struct breakpoint *b,
14278 const struct event_location *location,
14279 struct program_space *search_pspace)
14280 {
14281 struct linespec_result canonical;
14282
14283 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14284 (struct symtab *) NULL, 0,
14285 &canonical, multiple_symbols_all,
14286 b->filter);
14287
14288 /* We should get 0 or 1 resulting SALs. */
14289 gdb_assert (canonical.lsals.size () < 2);
14290
14291 if (!canonical.lsals.empty ())
14292 {
14293 const linespec_sals &lsal = canonical.lsals[0];
14294 return std::move (lsal.sals);
14295 }
14296 return {};
14297 }
14298
14299 /* Prepare the global context for a re-set of breakpoint B. */
14300
14301 static struct cleanup *
14302 prepare_re_set_context (struct breakpoint *b)
14303 {
14304 input_radix = b->input_radix;
14305 set_language (b->language);
14306
14307 return make_cleanup (null_cleanup, NULL);
14308 }
14309
14310 /* Reset a breakpoint given it's struct breakpoint * BINT.
14311 The value we return ends up being the return value from catch_errors.
14312 Unused in this case. */
14313
14314 static int
14315 breakpoint_re_set_one (void *bint)
14316 {
14317 /* Get past catch_errs. */
14318 struct breakpoint *b = (struct breakpoint *) bint;
14319 struct cleanup *cleanups;
14320
14321 cleanups = prepare_re_set_context (b);
14322 b->ops->re_set (b);
14323 do_cleanups (cleanups);
14324 return 0;
14325 }
14326
14327 /* Re-set breakpoint locations for the current program space.
14328 Locations bound to other program spaces are left untouched. */
14329
14330 void
14331 breakpoint_re_set (void)
14332 {
14333 struct breakpoint *b, *b_tmp;
14334 enum language save_language;
14335 int save_input_radix;
14336
14337 save_language = current_language->la_language;
14338 save_input_radix = input_radix;
14339
14340 {
14341 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14342
14343 /* Note: we must not try to insert locations until after all
14344 breakpoints have been re-set. Otherwise, e.g., when re-setting
14345 breakpoint 1, we'd insert the locations of breakpoint 2, which
14346 hadn't been re-set yet, and thus may have stale locations. */
14347
14348 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14349 {
14350 /* Format possible error msg. */
14351 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14352 b->number);
14353 struct cleanup *cleanups = make_cleanup (xfree, message);
14354 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14355 do_cleanups (cleanups);
14356 }
14357 set_language (save_language);
14358 input_radix = save_input_radix;
14359
14360 jit_breakpoint_re_set ();
14361 }
14362
14363 create_overlay_event_breakpoint ();
14364 create_longjmp_master_breakpoint ();
14365 create_std_terminate_master_breakpoint ();
14366 create_exception_master_breakpoint ();
14367
14368 /* Now we can insert. */
14369 update_global_location_list (UGLL_MAY_INSERT);
14370 }
14371 \f
14372 /* Reset the thread number of this breakpoint:
14373
14374 - If the breakpoint is for all threads, leave it as-is.
14375 - Else, reset it to the current thread for inferior_ptid. */
14376 void
14377 breakpoint_re_set_thread (struct breakpoint *b)
14378 {
14379 if (b->thread != -1)
14380 {
14381 if (in_thread_list (inferior_ptid))
14382 b->thread = ptid_to_global_thread_id (inferior_ptid);
14383
14384 /* We're being called after following a fork. The new fork is
14385 selected as current, and unless this was a vfork will have a
14386 different program space from the original thread. Reset that
14387 as well. */
14388 b->loc->pspace = current_program_space;
14389 }
14390 }
14391
14392 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14393 If from_tty is nonzero, it prints a message to that effect,
14394 which ends with a period (no newline). */
14395
14396 void
14397 set_ignore_count (int bptnum, int count, int from_tty)
14398 {
14399 struct breakpoint *b;
14400
14401 if (count < 0)
14402 count = 0;
14403
14404 ALL_BREAKPOINTS (b)
14405 if (b->number == bptnum)
14406 {
14407 if (is_tracepoint (b))
14408 {
14409 if (from_tty && count != 0)
14410 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14411 bptnum);
14412 return;
14413 }
14414
14415 b->ignore_count = count;
14416 if (from_tty)
14417 {
14418 if (count == 0)
14419 printf_filtered (_("Will stop next time "
14420 "breakpoint %d is reached."),
14421 bptnum);
14422 else if (count == 1)
14423 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14424 bptnum);
14425 else
14426 printf_filtered (_("Will ignore next %d "
14427 "crossings of breakpoint %d."),
14428 count, bptnum);
14429 }
14430 observer_notify_breakpoint_modified (b);
14431 return;
14432 }
14433
14434 error (_("No breakpoint number %d."), bptnum);
14435 }
14436
14437 /* Command to set ignore-count of breakpoint N to COUNT. */
14438
14439 static void
14440 ignore_command (char *args, int from_tty)
14441 {
14442 char *p = args;
14443 int num;
14444
14445 if (p == 0)
14446 error_no_arg (_("a breakpoint number"));
14447
14448 num = get_number (&p);
14449 if (num == 0)
14450 error (_("bad breakpoint number: '%s'"), args);
14451 if (*p == 0)
14452 error (_("Second argument (specified ignore-count) is missing."));
14453
14454 set_ignore_count (num,
14455 longest_to_int (value_as_long (parse_and_eval (p))),
14456 from_tty);
14457 if (from_tty)
14458 printf_filtered ("\n");
14459 }
14460 \f
14461 /* Call FUNCTION on each of the breakpoints
14462 whose numbers are given in ARGS. */
14463
14464 static void
14465 map_breakpoint_numbers (const char *args,
14466 void (*function) (struct breakpoint *,
14467 void *),
14468 void *data)
14469 {
14470 int num;
14471 struct breakpoint *b, *tmp;
14472
14473 if (args == 0 || *args == '\0')
14474 error_no_arg (_("one or more breakpoint numbers"));
14475
14476 number_or_range_parser parser (args);
14477
14478 while (!parser.finished ())
14479 {
14480 const char *p = parser.cur_tok ();
14481 bool match = false;
14482
14483 num = parser.get_number ();
14484 if (num == 0)
14485 {
14486 warning (_("bad breakpoint number at or near '%s'"), p);
14487 }
14488 else
14489 {
14490 ALL_BREAKPOINTS_SAFE (b, tmp)
14491 if (b->number == num)
14492 {
14493 match = true;
14494 function (b, data);
14495 break;
14496 }
14497 if (!match)
14498 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14499 }
14500 }
14501 }
14502
14503 static struct bp_location *
14504 find_location_by_number (const char *number)
14505 {
14506 const char *p1;
14507 int bp_num;
14508 int loc_num;
14509 struct breakpoint *b;
14510 struct bp_location *loc;
14511
14512 p1 = number;
14513 bp_num = get_number_trailer (&p1, '.');
14514 if (bp_num == 0 || p1[0] != '.')
14515 error (_("Bad breakpoint number '%s'"), number);
14516
14517 ALL_BREAKPOINTS (b)
14518 if (b->number == bp_num)
14519 {
14520 break;
14521 }
14522
14523 if (!b || b->number != bp_num)
14524 error (_("Bad breakpoint number '%s'"), number);
14525
14526 /* Skip the dot. */
14527 ++p1;
14528 const char *save = p1;
14529 loc_num = get_number (&p1);
14530 if (loc_num == 0)
14531 error (_("Bad breakpoint location number '%s'"), number);
14532
14533 --loc_num;
14534 loc = b->loc;
14535 for (;loc_num && loc; --loc_num, loc = loc->next)
14536 ;
14537 if (!loc)
14538 error (_("Bad breakpoint location number '%s'"), save);
14539
14540 return loc;
14541 }
14542
14543
14544 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14545 If from_tty is nonzero, it prints a message to that effect,
14546 which ends with a period (no newline). */
14547
14548 void
14549 disable_breakpoint (struct breakpoint *bpt)
14550 {
14551 /* Never disable a watchpoint scope breakpoint; we want to
14552 hit them when we leave scope so we can delete both the
14553 watchpoint and its scope breakpoint at that time. */
14554 if (bpt->type == bp_watchpoint_scope)
14555 return;
14556
14557 bpt->enable_state = bp_disabled;
14558
14559 /* Mark breakpoint locations modified. */
14560 mark_breakpoint_modified (bpt);
14561
14562 if (target_supports_enable_disable_tracepoint ()
14563 && current_trace_status ()->running && is_tracepoint (bpt))
14564 {
14565 struct bp_location *location;
14566
14567 for (location = bpt->loc; location; location = location->next)
14568 target_disable_tracepoint (location);
14569 }
14570
14571 update_global_location_list (UGLL_DONT_INSERT);
14572
14573 observer_notify_breakpoint_modified (bpt);
14574 }
14575
14576 /* A callback for iterate_over_related_breakpoints. */
14577
14578 static void
14579 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14580 {
14581 disable_breakpoint (b);
14582 }
14583
14584 /* A callback for map_breakpoint_numbers that calls
14585 disable_breakpoint. */
14586
14587 static void
14588 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14589 {
14590 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14591 }
14592
14593 static void
14594 disable_command (char *args, int from_tty)
14595 {
14596 if (args == 0)
14597 {
14598 struct breakpoint *bpt;
14599
14600 ALL_BREAKPOINTS (bpt)
14601 if (user_breakpoint_p (bpt))
14602 disable_breakpoint (bpt);
14603 }
14604 else
14605 {
14606 std::string num = extract_arg (&args);
14607
14608 while (!num.empty ())
14609 {
14610 if (num.find ('.') != std::string::npos)
14611 {
14612 struct bp_location *loc = find_location_by_number (num.c_str ());
14613
14614 if (loc)
14615 {
14616 if (loc->enabled)
14617 {
14618 loc->enabled = 0;
14619 mark_breakpoint_location_modified (loc);
14620 }
14621 if (target_supports_enable_disable_tracepoint ()
14622 && current_trace_status ()->running && loc->owner
14623 && is_tracepoint (loc->owner))
14624 target_disable_tracepoint (loc);
14625 }
14626 update_global_location_list (UGLL_DONT_INSERT);
14627 }
14628 else
14629 map_breakpoint_numbers (num.c_str (), do_map_disable_breakpoint,
14630 NULL);
14631 num = extract_arg (&args);
14632 }
14633 }
14634 }
14635
14636 static void
14637 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14638 int count)
14639 {
14640 int target_resources_ok;
14641
14642 if (bpt->type == bp_hardware_breakpoint)
14643 {
14644 int i;
14645 i = hw_breakpoint_used_count ();
14646 target_resources_ok =
14647 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14648 i + 1, 0);
14649 if (target_resources_ok == 0)
14650 error (_("No hardware breakpoint support in the target."));
14651 else if (target_resources_ok < 0)
14652 error (_("Hardware breakpoints used exceeds limit."));
14653 }
14654
14655 if (is_watchpoint (bpt))
14656 {
14657 /* Initialize it just to avoid a GCC false warning. */
14658 enum enable_state orig_enable_state = bp_disabled;
14659
14660 TRY
14661 {
14662 struct watchpoint *w = (struct watchpoint *) bpt;
14663
14664 orig_enable_state = bpt->enable_state;
14665 bpt->enable_state = bp_enabled;
14666 update_watchpoint (w, 1 /* reparse */);
14667 }
14668 CATCH (e, RETURN_MASK_ALL)
14669 {
14670 bpt->enable_state = orig_enable_state;
14671 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14672 bpt->number);
14673 return;
14674 }
14675 END_CATCH
14676 }
14677
14678 bpt->enable_state = bp_enabled;
14679
14680 /* Mark breakpoint locations modified. */
14681 mark_breakpoint_modified (bpt);
14682
14683 if (target_supports_enable_disable_tracepoint ()
14684 && current_trace_status ()->running && is_tracepoint (bpt))
14685 {
14686 struct bp_location *location;
14687
14688 for (location = bpt->loc; location; location = location->next)
14689 target_enable_tracepoint (location);
14690 }
14691
14692 bpt->disposition = disposition;
14693 bpt->enable_count = count;
14694 update_global_location_list (UGLL_MAY_INSERT);
14695
14696 observer_notify_breakpoint_modified (bpt);
14697 }
14698
14699
14700 void
14701 enable_breakpoint (struct breakpoint *bpt)
14702 {
14703 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14704 }
14705
14706 static void
14707 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14708 {
14709 enable_breakpoint (bpt);
14710 }
14711
14712 /* A callback for map_breakpoint_numbers that calls
14713 enable_breakpoint. */
14714
14715 static void
14716 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14717 {
14718 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14719 }
14720
14721 /* The enable command enables the specified breakpoints (or all defined
14722 breakpoints) so they once again become (or continue to be) effective
14723 in stopping the inferior. */
14724
14725 static void
14726 enable_command (char *args, int from_tty)
14727 {
14728 if (args == 0)
14729 {
14730 struct breakpoint *bpt;
14731
14732 ALL_BREAKPOINTS (bpt)
14733 if (user_breakpoint_p (bpt))
14734 enable_breakpoint (bpt);
14735 }
14736 else
14737 {
14738 std::string num = extract_arg (&args);
14739
14740 while (!num.empty ())
14741 {
14742 if (num.find ('.') != std::string::npos)
14743 {
14744 struct bp_location *loc = find_location_by_number (num.c_str ());
14745
14746 if (loc)
14747 {
14748 if (!loc->enabled)
14749 {
14750 loc->enabled = 1;
14751 mark_breakpoint_location_modified (loc);
14752 }
14753 if (target_supports_enable_disable_tracepoint ()
14754 && current_trace_status ()->running && loc->owner
14755 && is_tracepoint (loc->owner))
14756 target_enable_tracepoint (loc);
14757 }
14758 update_global_location_list (UGLL_MAY_INSERT);
14759 }
14760 else
14761 map_breakpoint_numbers (num.c_str (), do_map_enable_breakpoint,
14762 NULL);
14763 num = extract_arg (&args);
14764 }
14765 }
14766 }
14767
14768 /* This struct packages up disposition data for application to multiple
14769 breakpoints. */
14770
14771 struct disp_data
14772 {
14773 enum bpdisp disp;
14774 int count;
14775 };
14776
14777 static void
14778 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14779 {
14780 struct disp_data disp_data = *(struct disp_data *) arg;
14781
14782 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14783 }
14784
14785 static void
14786 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14787 {
14788 struct disp_data disp = { disp_disable, 1 };
14789
14790 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14791 }
14792
14793 static void
14794 enable_once_command (char *args, int from_tty)
14795 {
14796 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14797 }
14798
14799 static void
14800 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14801 {
14802 struct disp_data disp = { disp_disable, *(int *) countptr };
14803
14804 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14805 }
14806
14807 static void
14808 enable_count_command (char *args, int from_tty)
14809 {
14810 int count;
14811
14812 if (args == NULL)
14813 error_no_arg (_("hit count"));
14814
14815 count = get_number (&args);
14816
14817 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14818 }
14819
14820 static void
14821 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14822 {
14823 struct disp_data disp = { disp_del, 1 };
14824
14825 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14826 }
14827
14828 static void
14829 enable_delete_command (char *args, int from_tty)
14830 {
14831 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14832 }
14833 \f
14834 static void
14835 set_breakpoint_cmd (char *args, int from_tty)
14836 {
14837 }
14838
14839 static void
14840 show_breakpoint_cmd (char *args, int from_tty)
14841 {
14842 }
14843
14844 /* Invalidate last known value of any hardware watchpoint if
14845 the memory which that value represents has been written to by
14846 GDB itself. */
14847
14848 static void
14849 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14850 CORE_ADDR addr, ssize_t len,
14851 const bfd_byte *data)
14852 {
14853 struct breakpoint *bp;
14854
14855 ALL_BREAKPOINTS (bp)
14856 if (bp->enable_state == bp_enabled
14857 && bp->type == bp_hardware_watchpoint)
14858 {
14859 struct watchpoint *wp = (struct watchpoint *) bp;
14860
14861 if (wp->val_valid && wp->val)
14862 {
14863 struct bp_location *loc;
14864
14865 for (loc = bp->loc; loc != NULL; loc = loc->next)
14866 if (loc->loc_type == bp_loc_hardware_watchpoint
14867 && loc->address + loc->length > addr
14868 && addr + len > loc->address)
14869 {
14870 value_free (wp->val);
14871 wp->val = NULL;
14872 wp->val_valid = 0;
14873 }
14874 }
14875 }
14876 }
14877
14878 /* Create and insert a breakpoint for software single step. */
14879
14880 void
14881 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14882 struct address_space *aspace,
14883 CORE_ADDR next_pc)
14884 {
14885 struct thread_info *tp = inferior_thread ();
14886 struct symtab_and_line sal;
14887 CORE_ADDR pc = next_pc;
14888
14889 if (tp->control.single_step_breakpoints == NULL)
14890 {
14891 tp->control.single_step_breakpoints
14892 = new_single_step_breakpoint (tp->global_num, gdbarch);
14893 }
14894
14895 sal = find_pc_line (pc, 0);
14896 sal.pc = pc;
14897 sal.section = find_pc_overlay (pc);
14898 sal.explicit_pc = 1;
14899 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14900
14901 update_global_location_list (UGLL_INSERT);
14902 }
14903
14904 /* Insert single step breakpoints according to the current state. */
14905
14906 int
14907 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14908 {
14909 struct regcache *regcache = get_current_regcache ();
14910 std::vector<CORE_ADDR> next_pcs;
14911
14912 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14913
14914 if (!next_pcs.empty ())
14915 {
14916 struct frame_info *frame = get_current_frame ();
14917 struct address_space *aspace = get_frame_address_space (frame);
14918
14919 for (CORE_ADDR pc : next_pcs)
14920 insert_single_step_breakpoint (gdbarch, aspace, pc);
14921
14922 return 1;
14923 }
14924 else
14925 return 0;
14926 }
14927
14928 /* See breakpoint.h. */
14929
14930 int
14931 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14932 struct address_space *aspace,
14933 CORE_ADDR pc)
14934 {
14935 struct bp_location *loc;
14936
14937 for (loc = bp->loc; loc != NULL; loc = loc->next)
14938 if (loc->inserted
14939 && breakpoint_location_address_match (loc, aspace, pc))
14940 return 1;
14941
14942 return 0;
14943 }
14944
14945 /* Check whether a software single-step breakpoint is inserted at
14946 PC. */
14947
14948 int
14949 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14950 CORE_ADDR pc)
14951 {
14952 struct breakpoint *bpt;
14953
14954 ALL_BREAKPOINTS (bpt)
14955 {
14956 if (bpt->type == bp_single_step
14957 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14958 return 1;
14959 }
14960 return 0;
14961 }
14962
14963 /* Tracepoint-specific operations. */
14964
14965 /* Set tracepoint count to NUM. */
14966 static void
14967 set_tracepoint_count (int num)
14968 {
14969 tracepoint_count = num;
14970 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14971 }
14972
14973 static void
14974 trace_command (char *arg, int from_tty)
14975 {
14976 struct breakpoint_ops *ops;
14977
14978 event_location_up location = string_to_event_location (&arg,
14979 current_language);
14980 if (location != NULL
14981 && event_location_type (location.get ()) == PROBE_LOCATION)
14982 ops = &tracepoint_probe_breakpoint_ops;
14983 else
14984 ops = &tracepoint_breakpoint_ops;
14985
14986 create_breakpoint (get_current_arch (),
14987 location.get (),
14988 NULL, 0, arg, 1 /* parse arg */,
14989 0 /* tempflag */,
14990 bp_tracepoint /* type_wanted */,
14991 0 /* Ignore count */,
14992 pending_break_support,
14993 ops,
14994 from_tty,
14995 1 /* enabled */,
14996 0 /* internal */, 0);
14997 }
14998
14999 static void
15000 ftrace_command (char *arg, int from_tty)
15001 {
15002 event_location_up location = string_to_event_location (&arg,
15003 current_language);
15004 create_breakpoint (get_current_arch (),
15005 location.get (),
15006 NULL, 0, arg, 1 /* parse arg */,
15007 0 /* tempflag */,
15008 bp_fast_tracepoint /* type_wanted */,
15009 0 /* Ignore count */,
15010 pending_break_support,
15011 &tracepoint_breakpoint_ops,
15012 from_tty,
15013 1 /* enabled */,
15014 0 /* internal */, 0);
15015 }
15016
15017 /* strace command implementation. Creates a static tracepoint. */
15018
15019 static void
15020 strace_command (char *arg, int from_tty)
15021 {
15022 struct breakpoint_ops *ops;
15023 event_location_up location;
15024 struct cleanup *back_to;
15025
15026 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15027 or with a normal static tracepoint. */
15028 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15029 {
15030 ops = &strace_marker_breakpoint_ops;
15031 location = new_linespec_location (&arg);
15032 }
15033 else
15034 {
15035 ops = &tracepoint_breakpoint_ops;
15036 location = string_to_event_location (&arg, current_language);
15037 }
15038
15039 create_breakpoint (get_current_arch (),
15040 location.get (),
15041 NULL, 0, arg, 1 /* parse arg */,
15042 0 /* tempflag */,
15043 bp_static_tracepoint /* type_wanted */,
15044 0 /* Ignore count */,
15045 pending_break_support,
15046 ops,
15047 from_tty,
15048 1 /* enabled */,
15049 0 /* internal */, 0);
15050 }
15051
15052 /* Set up a fake reader function that gets command lines from a linked
15053 list that was acquired during tracepoint uploading. */
15054
15055 static struct uploaded_tp *this_utp;
15056 static int next_cmd;
15057
15058 static char *
15059 read_uploaded_action (void)
15060 {
15061 char *rslt;
15062
15063 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15064
15065 next_cmd++;
15066
15067 return rslt;
15068 }
15069
15070 /* Given information about a tracepoint as recorded on a target (which
15071 can be either a live system or a trace file), attempt to create an
15072 equivalent GDB tracepoint. This is not a reliable process, since
15073 the target does not necessarily have all the information used when
15074 the tracepoint was originally defined. */
15075
15076 struct tracepoint *
15077 create_tracepoint_from_upload (struct uploaded_tp *utp)
15078 {
15079 char *addr_str, small_buf[100];
15080 struct tracepoint *tp;
15081
15082 if (utp->at_string)
15083 addr_str = utp->at_string;
15084 else
15085 {
15086 /* In the absence of a source location, fall back to raw
15087 address. Since there is no way to confirm that the address
15088 means the same thing as when the trace was started, warn the
15089 user. */
15090 warning (_("Uploaded tracepoint %d has no "
15091 "source location, using raw address"),
15092 utp->number);
15093 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15094 addr_str = small_buf;
15095 }
15096
15097 /* There's not much we can do with a sequence of bytecodes. */
15098 if (utp->cond && !utp->cond_string)
15099 warning (_("Uploaded tracepoint %d condition "
15100 "has no source form, ignoring it"),
15101 utp->number);
15102
15103 event_location_up location = string_to_event_location (&addr_str,
15104 current_language);
15105 if (!create_breakpoint (get_current_arch (),
15106 location.get (),
15107 utp->cond_string, -1, addr_str,
15108 0 /* parse cond/thread */,
15109 0 /* tempflag */,
15110 utp->type /* type_wanted */,
15111 0 /* Ignore count */,
15112 pending_break_support,
15113 &tracepoint_breakpoint_ops,
15114 0 /* from_tty */,
15115 utp->enabled /* enabled */,
15116 0 /* internal */,
15117 CREATE_BREAKPOINT_FLAGS_INSERTED))
15118 return NULL;
15119
15120 /* Get the tracepoint we just created. */
15121 tp = get_tracepoint (tracepoint_count);
15122 gdb_assert (tp != NULL);
15123
15124 if (utp->pass > 0)
15125 {
15126 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15127 tp->number);
15128
15129 trace_pass_command (small_buf, 0);
15130 }
15131
15132 /* If we have uploaded versions of the original commands, set up a
15133 special-purpose "reader" function and call the usual command line
15134 reader, then pass the result to the breakpoint command-setting
15135 function. */
15136 if (!VEC_empty (char_ptr, utp->cmd_strings))
15137 {
15138 command_line_up cmd_list;
15139
15140 this_utp = utp;
15141 next_cmd = 0;
15142
15143 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15144
15145 breakpoint_set_commands (tp, std::move (cmd_list));
15146 }
15147 else if (!VEC_empty (char_ptr, utp->actions)
15148 || !VEC_empty (char_ptr, utp->step_actions))
15149 warning (_("Uploaded tracepoint %d actions "
15150 "have no source form, ignoring them"),
15151 utp->number);
15152
15153 /* Copy any status information that might be available. */
15154 tp->hit_count = utp->hit_count;
15155 tp->traceframe_usage = utp->traceframe_usage;
15156
15157 return tp;
15158 }
15159
15160 /* Print information on tracepoint number TPNUM_EXP, or all if
15161 omitted. */
15162
15163 static void
15164 info_tracepoints_command (char *args, int from_tty)
15165 {
15166 struct ui_out *uiout = current_uiout;
15167 int num_printed;
15168
15169 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15170
15171 if (num_printed == 0)
15172 {
15173 if (args == NULL || *args == '\0')
15174 uiout->message ("No tracepoints.\n");
15175 else
15176 uiout->message ("No tracepoint matching '%s'.\n", args);
15177 }
15178
15179 default_collect_info ();
15180 }
15181
15182 /* The 'enable trace' command enables tracepoints.
15183 Not supported by all targets. */
15184 static void
15185 enable_trace_command (char *args, int from_tty)
15186 {
15187 enable_command (args, from_tty);
15188 }
15189
15190 /* The 'disable trace' command disables tracepoints.
15191 Not supported by all targets. */
15192 static void
15193 disable_trace_command (char *args, int from_tty)
15194 {
15195 disable_command (args, from_tty);
15196 }
15197
15198 /* Remove a tracepoint (or all if no argument). */
15199 static void
15200 delete_trace_command (char *arg, int from_tty)
15201 {
15202 struct breakpoint *b, *b_tmp;
15203
15204 dont_repeat ();
15205
15206 if (arg == 0)
15207 {
15208 int breaks_to_delete = 0;
15209
15210 /* Delete all breakpoints if no argument.
15211 Do not delete internal or call-dummy breakpoints, these
15212 have to be deleted with an explicit breakpoint number
15213 argument. */
15214 ALL_TRACEPOINTS (b)
15215 if (is_tracepoint (b) && user_breakpoint_p (b))
15216 {
15217 breaks_to_delete = 1;
15218 break;
15219 }
15220
15221 /* Ask user only if there are some breakpoints to delete. */
15222 if (!from_tty
15223 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15224 {
15225 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15226 if (is_tracepoint (b) && user_breakpoint_p (b))
15227 delete_breakpoint (b);
15228 }
15229 }
15230 else
15231 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15232 }
15233
15234 /* Helper function for trace_pass_command. */
15235
15236 static void
15237 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15238 {
15239 tp->pass_count = count;
15240 observer_notify_breakpoint_modified (tp);
15241 if (from_tty)
15242 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15243 tp->number, count);
15244 }
15245
15246 /* Set passcount for tracepoint.
15247
15248 First command argument is passcount, second is tracepoint number.
15249 If tracepoint number omitted, apply to most recently defined.
15250 Also accepts special argument "all". */
15251
15252 static void
15253 trace_pass_command (char *args, int from_tty)
15254 {
15255 struct tracepoint *t1;
15256 unsigned int count;
15257
15258 if (args == 0 || *args == 0)
15259 error (_("passcount command requires an "
15260 "argument (count + optional TP num)"));
15261
15262 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15263
15264 args = skip_spaces (args);
15265 if (*args && strncasecmp (args, "all", 3) == 0)
15266 {
15267 struct breakpoint *b;
15268
15269 args += 3; /* Skip special argument "all". */
15270 if (*args)
15271 error (_("Junk at end of arguments."));
15272
15273 ALL_TRACEPOINTS (b)
15274 {
15275 t1 = (struct tracepoint *) b;
15276 trace_pass_set_count (t1, count, from_tty);
15277 }
15278 }
15279 else if (*args == '\0')
15280 {
15281 t1 = get_tracepoint_by_number (&args, NULL);
15282 if (t1)
15283 trace_pass_set_count (t1, count, from_tty);
15284 }
15285 else
15286 {
15287 number_or_range_parser parser (args);
15288 while (!parser.finished ())
15289 {
15290 t1 = get_tracepoint_by_number (&args, &parser);
15291 if (t1)
15292 trace_pass_set_count (t1, count, from_tty);
15293 }
15294 }
15295 }
15296
15297 struct tracepoint *
15298 get_tracepoint (int num)
15299 {
15300 struct breakpoint *t;
15301
15302 ALL_TRACEPOINTS (t)
15303 if (t->number == num)
15304 return (struct tracepoint *) t;
15305
15306 return NULL;
15307 }
15308
15309 /* Find the tracepoint with the given target-side number (which may be
15310 different from the tracepoint number after disconnecting and
15311 reconnecting). */
15312
15313 struct tracepoint *
15314 get_tracepoint_by_number_on_target (int num)
15315 {
15316 struct breakpoint *b;
15317
15318 ALL_TRACEPOINTS (b)
15319 {
15320 struct tracepoint *t = (struct tracepoint *) b;
15321
15322 if (t->number_on_target == num)
15323 return t;
15324 }
15325
15326 return NULL;
15327 }
15328
15329 /* Utility: parse a tracepoint number and look it up in the list.
15330 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15331 If the argument is missing, the most recent tracepoint
15332 (tracepoint_count) is returned. */
15333
15334 struct tracepoint *
15335 get_tracepoint_by_number (char **arg,
15336 number_or_range_parser *parser)
15337 {
15338 struct breakpoint *t;
15339 int tpnum;
15340 char *instring = arg == NULL ? NULL : *arg;
15341
15342 if (parser != NULL)
15343 {
15344 gdb_assert (!parser->finished ());
15345 tpnum = parser->get_number ();
15346 }
15347 else if (arg == NULL || *arg == NULL || ! **arg)
15348 tpnum = tracepoint_count;
15349 else
15350 tpnum = get_number (arg);
15351
15352 if (tpnum <= 0)
15353 {
15354 if (instring && *instring)
15355 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15356 instring);
15357 else
15358 printf_filtered (_("No previous tracepoint\n"));
15359 return NULL;
15360 }
15361
15362 ALL_TRACEPOINTS (t)
15363 if (t->number == tpnum)
15364 {
15365 return (struct tracepoint *) t;
15366 }
15367
15368 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15369 return NULL;
15370 }
15371
15372 void
15373 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15374 {
15375 if (b->thread != -1)
15376 fprintf_unfiltered (fp, " thread %d", b->thread);
15377
15378 if (b->task != 0)
15379 fprintf_unfiltered (fp, " task %d", b->task);
15380
15381 fprintf_unfiltered (fp, "\n");
15382 }
15383
15384 /* Save information on user settable breakpoints (watchpoints, etc) to
15385 a new script file named FILENAME. If FILTER is non-NULL, call it
15386 on each breakpoint and only include the ones for which it returns
15387 non-zero. */
15388
15389 static void
15390 save_breakpoints (char *filename, int from_tty,
15391 int (*filter) (const struct breakpoint *))
15392 {
15393 struct breakpoint *tp;
15394 int any = 0;
15395 int extra_trace_bits = 0;
15396
15397 if (filename == 0 || *filename == 0)
15398 error (_("Argument required (file name in which to save)"));
15399
15400 /* See if we have anything to save. */
15401 ALL_BREAKPOINTS (tp)
15402 {
15403 /* Skip internal and momentary breakpoints. */
15404 if (!user_breakpoint_p (tp))
15405 continue;
15406
15407 /* If we have a filter, only save the breakpoints it accepts. */
15408 if (filter && !filter (tp))
15409 continue;
15410
15411 any = 1;
15412
15413 if (is_tracepoint (tp))
15414 {
15415 extra_trace_bits = 1;
15416
15417 /* We can stop searching. */
15418 break;
15419 }
15420 }
15421
15422 if (!any)
15423 {
15424 warning (_("Nothing to save."));
15425 return;
15426 }
15427
15428 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15429
15430 stdio_file fp;
15431
15432 if (!fp.open (expanded_filename.get (), "w"))
15433 error (_("Unable to open file '%s' for saving (%s)"),
15434 expanded_filename.get (), safe_strerror (errno));
15435
15436 if (extra_trace_bits)
15437 save_trace_state_variables (&fp);
15438
15439 ALL_BREAKPOINTS (tp)
15440 {
15441 /* Skip internal and momentary breakpoints. */
15442 if (!user_breakpoint_p (tp))
15443 continue;
15444
15445 /* If we have a filter, only save the breakpoints it accepts. */
15446 if (filter && !filter (tp))
15447 continue;
15448
15449 tp->ops->print_recreate (tp, &fp);
15450
15451 /* Note, we can't rely on tp->number for anything, as we can't
15452 assume the recreated breakpoint numbers will match. Use $bpnum
15453 instead. */
15454
15455 if (tp->cond_string)
15456 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15457
15458 if (tp->ignore_count)
15459 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15460
15461 if (tp->type != bp_dprintf && tp->commands)
15462 {
15463 fp.puts (" commands\n");
15464
15465 current_uiout->redirect (&fp);
15466 TRY
15467 {
15468 print_command_lines (current_uiout, tp->commands->commands, 2);
15469 }
15470 CATCH (ex, RETURN_MASK_ALL)
15471 {
15472 current_uiout->redirect (NULL);
15473 throw_exception (ex);
15474 }
15475 END_CATCH
15476
15477 current_uiout->redirect (NULL);
15478 fp.puts (" end\n");
15479 }
15480
15481 if (tp->enable_state == bp_disabled)
15482 fp.puts ("disable $bpnum\n");
15483
15484 /* If this is a multi-location breakpoint, check if the locations
15485 should be individually disabled. Watchpoint locations are
15486 special, and not user visible. */
15487 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15488 {
15489 struct bp_location *loc;
15490 int n = 1;
15491
15492 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15493 if (!loc->enabled)
15494 fp.printf ("disable $bpnum.%d\n", n);
15495 }
15496 }
15497
15498 if (extra_trace_bits && *default_collect)
15499 fp.printf ("set default-collect %s\n", default_collect);
15500
15501 if (from_tty)
15502 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15503 }
15504
15505 /* The `save breakpoints' command. */
15506
15507 static void
15508 save_breakpoints_command (char *args, int from_tty)
15509 {
15510 save_breakpoints (args, from_tty, NULL);
15511 }
15512
15513 /* The `save tracepoints' command. */
15514
15515 static void
15516 save_tracepoints_command (char *args, int from_tty)
15517 {
15518 save_breakpoints (args, from_tty, is_tracepoint);
15519 }
15520
15521 /* Create a vector of all tracepoints. */
15522
15523 VEC(breakpoint_p) *
15524 all_tracepoints (void)
15525 {
15526 VEC(breakpoint_p) *tp_vec = 0;
15527 struct breakpoint *tp;
15528
15529 ALL_TRACEPOINTS (tp)
15530 {
15531 VEC_safe_push (breakpoint_p, tp_vec, tp);
15532 }
15533
15534 return tp_vec;
15535 }
15536
15537 \f
15538 /* This help string is used to consolidate all the help string for specifying
15539 locations used by several commands. */
15540
15541 #define LOCATION_HELP_STRING \
15542 "Linespecs are colon-separated lists of location parameters, such as\n\
15543 source filename, function name, label name, and line number.\n\
15544 Example: To specify the start of a label named \"the_top\" in the\n\
15545 function \"fact\" in the file \"factorial.c\", use\n\
15546 \"factorial.c:fact:the_top\".\n\
15547 \n\
15548 Address locations begin with \"*\" and specify an exact address in the\n\
15549 program. Example: To specify the fourth byte past the start function\n\
15550 \"main\", use \"*main + 4\".\n\
15551 \n\
15552 Explicit locations are similar to linespecs but use an option/argument\n\
15553 syntax to specify location parameters.\n\
15554 Example: To specify the start of the label named \"the_top\" in the\n\
15555 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15556 -function fact -label the_top\".\n"
15557
15558 /* This help string is used for the break, hbreak, tbreak and thbreak
15559 commands. It is defined as a macro to prevent duplication.
15560 COMMAND should be a string constant containing the name of the
15561 command. */
15562
15563 #define BREAK_ARGS_HELP(command) \
15564 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15565 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15566 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15567 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15568 `-probe-dtrace' (for a DTrace probe).\n\
15569 LOCATION may be a linespec, address, or explicit location as described\n\
15570 below.\n\
15571 \n\
15572 With no LOCATION, uses current execution address of the selected\n\
15573 stack frame. This is useful for breaking on return to a stack frame.\n\
15574 \n\
15575 THREADNUM is the number from \"info threads\".\n\
15576 CONDITION is a boolean expression.\n\
15577 \n" LOCATION_HELP_STRING "\n\
15578 Multiple breakpoints at one place are permitted, and useful if their\n\
15579 conditions are different.\n\
15580 \n\
15581 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15582
15583 /* List of subcommands for "catch". */
15584 static struct cmd_list_element *catch_cmdlist;
15585
15586 /* List of subcommands for "tcatch". */
15587 static struct cmd_list_element *tcatch_cmdlist;
15588
15589 void
15590 add_catch_command (const char *name, const char *docstring,
15591 cmd_sfunc_ftype *sfunc,
15592 completer_ftype *completer,
15593 void *user_data_catch,
15594 void *user_data_tcatch)
15595 {
15596 struct cmd_list_element *command;
15597
15598 command = add_cmd (name, class_breakpoint, NULL, docstring,
15599 &catch_cmdlist);
15600 set_cmd_sfunc (command, sfunc);
15601 set_cmd_context (command, user_data_catch);
15602 set_cmd_completer (command, completer);
15603
15604 command = add_cmd (name, class_breakpoint, NULL, docstring,
15605 &tcatch_cmdlist);
15606 set_cmd_sfunc (command, sfunc);
15607 set_cmd_context (command, user_data_tcatch);
15608 set_cmd_completer (command, completer);
15609 }
15610
15611 static void
15612 save_command (char *arg, int from_tty)
15613 {
15614 printf_unfiltered (_("\"save\" must be followed by "
15615 "the name of a save subcommand.\n"));
15616 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15617 }
15618
15619 struct breakpoint *
15620 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15621 void *data)
15622 {
15623 struct breakpoint *b, *b_tmp;
15624
15625 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15626 {
15627 if ((*callback) (b, data))
15628 return b;
15629 }
15630
15631 return NULL;
15632 }
15633
15634 /* Zero if any of the breakpoint's locations could be a location where
15635 functions have been inlined, nonzero otherwise. */
15636
15637 static int
15638 is_non_inline_function (struct breakpoint *b)
15639 {
15640 /* The shared library event breakpoint is set on the address of a
15641 non-inline function. */
15642 if (b->type == bp_shlib_event)
15643 return 1;
15644
15645 return 0;
15646 }
15647
15648 /* Nonzero if the specified PC cannot be a location where functions
15649 have been inlined. */
15650
15651 int
15652 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15653 const struct target_waitstatus *ws)
15654 {
15655 struct breakpoint *b;
15656 struct bp_location *bl;
15657
15658 ALL_BREAKPOINTS (b)
15659 {
15660 if (!is_non_inline_function (b))
15661 continue;
15662
15663 for (bl = b->loc; bl != NULL; bl = bl->next)
15664 {
15665 if (!bl->shlib_disabled
15666 && bpstat_check_location (bl, aspace, pc, ws))
15667 return 1;
15668 }
15669 }
15670
15671 return 0;
15672 }
15673
15674 /* Remove any references to OBJFILE which is going to be freed. */
15675
15676 void
15677 breakpoint_free_objfile (struct objfile *objfile)
15678 {
15679 struct bp_location **locp, *loc;
15680
15681 ALL_BP_LOCATIONS (loc, locp)
15682 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15683 loc->symtab = NULL;
15684 }
15685
15686 void
15687 initialize_breakpoint_ops (void)
15688 {
15689 static int initialized = 0;
15690
15691 struct breakpoint_ops *ops;
15692
15693 if (initialized)
15694 return;
15695 initialized = 1;
15696
15697 /* The breakpoint_ops structure to be inherit by all kinds of
15698 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15699 internal and momentary breakpoints, etc.). */
15700 ops = &bkpt_base_breakpoint_ops;
15701 *ops = base_breakpoint_ops;
15702 ops->re_set = bkpt_re_set;
15703 ops->insert_location = bkpt_insert_location;
15704 ops->remove_location = bkpt_remove_location;
15705 ops->breakpoint_hit = bkpt_breakpoint_hit;
15706 ops->create_sals_from_location = bkpt_create_sals_from_location;
15707 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15708 ops->decode_location = bkpt_decode_location;
15709
15710 /* The breakpoint_ops structure to be used in regular breakpoints. */
15711 ops = &bkpt_breakpoint_ops;
15712 *ops = bkpt_base_breakpoint_ops;
15713 ops->re_set = bkpt_re_set;
15714 ops->resources_needed = bkpt_resources_needed;
15715 ops->print_it = bkpt_print_it;
15716 ops->print_mention = bkpt_print_mention;
15717 ops->print_recreate = bkpt_print_recreate;
15718
15719 /* Ranged breakpoints. */
15720 ops = &ranged_breakpoint_ops;
15721 *ops = bkpt_breakpoint_ops;
15722 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15723 ops->resources_needed = resources_needed_ranged_breakpoint;
15724 ops->print_it = print_it_ranged_breakpoint;
15725 ops->print_one = print_one_ranged_breakpoint;
15726 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15727 ops->print_mention = print_mention_ranged_breakpoint;
15728 ops->print_recreate = print_recreate_ranged_breakpoint;
15729
15730 /* Internal breakpoints. */
15731 ops = &internal_breakpoint_ops;
15732 *ops = bkpt_base_breakpoint_ops;
15733 ops->re_set = internal_bkpt_re_set;
15734 ops->check_status = internal_bkpt_check_status;
15735 ops->print_it = internal_bkpt_print_it;
15736 ops->print_mention = internal_bkpt_print_mention;
15737
15738 /* Momentary breakpoints. */
15739 ops = &momentary_breakpoint_ops;
15740 *ops = bkpt_base_breakpoint_ops;
15741 ops->re_set = momentary_bkpt_re_set;
15742 ops->check_status = momentary_bkpt_check_status;
15743 ops->print_it = momentary_bkpt_print_it;
15744 ops->print_mention = momentary_bkpt_print_mention;
15745
15746 /* Probe breakpoints. */
15747 ops = &bkpt_probe_breakpoint_ops;
15748 *ops = bkpt_breakpoint_ops;
15749 ops->insert_location = bkpt_probe_insert_location;
15750 ops->remove_location = bkpt_probe_remove_location;
15751 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15752 ops->decode_location = bkpt_probe_decode_location;
15753
15754 /* Watchpoints. */
15755 ops = &watchpoint_breakpoint_ops;
15756 *ops = base_breakpoint_ops;
15757 ops->re_set = re_set_watchpoint;
15758 ops->insert_location = insert_watchpoint;
15759 ops->remove_location = remove_watchpoint;
15760 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15761 ops->check_status = check_status_watchpoint;
15762 ops->resources_needed = resources_needed_watchpoint;
15763 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15764 ops->print_it = print_it_watchpoint;
15765 ops->print_mention = print_mention_watchpoint;
15766 ops->print_recreate = print_recreate_watchpoint;
15767 ops->explains_signal = explains_signal_watchpoint;
15768
15769 /* Masked watchpoints. */
15770 ops = &masked_watchpoint_breakpoint_ops;
15771 *ops = watchpoint_breakpoint_ops;
15772 ops->insert_location = insert_masked_watchpoint;
15773 ops->remove_location = remove_masked_watchpoint;
15774 ops->resources_needed = resources_needed_masked_watchpoint;
15775 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15776 ops->print_it = print_it_masked_watchpoint;
15777 ops->print_one_detail = print_one_detail_masked_watchpoint;
15778 ops->print_mention = print_mention_masked_watchpoint;
15779 ops->print_recreate = print_recreate_masked_watchpoint;
15780
15781 /* Tracepoints. */
15782 ops = &tracepoint_breakpoint_ops;
15783 *ops = base_breakpoint_ops;
15784 ops->re_set = tracepoint_re_set;
15785 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15786 ops->print_one_detail = tracepoint_print_one_detail;
15787 ops->print_mention = tracepoint_print_mention;
15788 ops->print_recreate = tracepoint_print_recreate;
15789 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15790 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15791 ops->decode_location = tracepoint_decode_location;
15792
15793 /* Probe tracepoints. */
15794 ops = &tracepoint_probe_breakpoint_ops;
15795 *ops = tracepoint_breakpoint_ops;
15796 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15797 ops->decode_location = tracepoint_probe_decode_location;
15798
15799 /* Static tracepoints with marker (`-m'). */
15800 ops = &strace_marker_breakpoint_ops;
15801 *ops = tracepoint_breakpoint_ops;
15802 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15803 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15804 ops->decode_location = strace_marker_decode_location;
15805
15806 /* Fork catchpoints. */
15807 ops = &catch_fork_breakpoint_ops;
15808 *ops = base_breakpoint_ops;
15809 ops->insert_location = insert_catch_fork;
15810 ops->remove_location = remove_catch_fork;
15811 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15812 ops->print_it = print_it_catch_fork;
15813 ops->print_one = print_one_catch_fork;
15814 ops->print_mention = print_mention_catch_fork;
15815 ops->print_recreate = print_recreate_catch_fork;
15816
15817 /* Vfork catchpoints. */
15818 ops = &catch_vfork_breakpoint_ops;
15819 *ops = base_breakpoint_ops;
15820 ops->insert_location = insert_catch_vfork;
15821 ops->remove_location = remove_catch_vfork;
15822 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15823 ops->print_it = print_it_catch_vfork;
15824 ops->print_one = print_one_catch_vfork;
15825 ops->print_mention = print_mention_catch_vfork;
15826 ops->print_recreate = print_recreate_catch_vfork;
15827
15828 /* Exec catchpoints. */
15829 ops = &catch_exec_breakpoint_ops;
15830 *ops = base_breakpoint_ops;
15831 ops->insert_location = insert_catch_exec;
15832 ops->remove_location = remove_catch_exec;
15833 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15834 ops->print_it = print_it_catch_exec;
15835 ops->print_one = print_one_catch_exec;
15836 ops->print_mention = print_mention_catch_exec;
15837 ops->print_recreate = print_recreate_catch_exec;
15838
15839 /* Solib-related catchpoints. */
15840 ops = &catch_solib_breakpoint_ops;
15841 *ops = base_breakpoint_ops;
15842 ops->insert_location = insert_catch_solib;
15843 ops->remove_location = remove_catch_solib;
15844 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15845 ops->check_status = check_status_catch_solib;
15846 ops->print_it = print_it_catch_solib;
15847 ops->print_one = print_one_catch_solib;
15848 ops->print_mention = print_mention_catch_solib;
15849 ops->print_recreate = print_recreate_catch_solib;
15850
15851 ops = &dprintf_breakpoint_ops;
15852 *ops = bkpt_base_breakpoint_ops;
15853 ops->re_set = dprintf_re_set;
15854 ops->resources_needed = bkpt_resources_needed;
15855 ops->print_it = bkpt_print_it;
15856 ops->print_mention = bkpt_print_mention;
15857 ops->print_recreate = dprintf_print_recreate;
15858 ops->after_condition_true = dprintf_after_condition_true;
15859 ops->breakpoint_hit = dprintf_breakpoint_hit;
15860 }
15861
15862 /* Chain containing all defined "enable breakpoint" subcommands. */
15863
15864 static struct cmd_list_element *enablebreaklist = NULL;
15865
15866 void
15867 _initialize_breakpoint (void)
15868 {
15869 struct cmd_list_element *c;
15870
15871 initialize_breakpoint_ops ();
15872
15873 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15874 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15875 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15876
15877 breakpoint_objfile_key
15878 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15879
15880 breakpoint_chain = 0;
15881 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15882 before a breakpoint is set. */
15883 breakpoint_count = 0;
15884
15885 tracepoint_count = 0;
15886
15887 add_com ("ignore", class_breakpoint, ignore_command, _("\
15888 Set ignore-count of breakpoint number N to COUNT.\n\
15889 Usage is `ignore N COUNT'."));
15890
15891 add_com ("commands", class_breakpoint, commands_command, _("\
15892 Set commands to be executed when the given breakpoints are hit.\n\
15893 Give a space-separated breakpoint list as argument after \"commands\".\n\
15894 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15895 (e.g. `5-7').\n\
15896 With no argument, the targeted breakpoint is the last one set.\n\
15897 The commands themselves follow starting on the next line.\n\
15898 Type a line containing \"end\" to indicate the end of them.\n\
15899 Give \"silent\" as the first line to make the breakpoint silent;\n\
15900 then no output is printed when it is hit, except what the commands print."));
15901
15902 c = add_com ("condition", class_breakpoint, condition_command, _("\
15903 Specify breakpoint number N to break only if COND is true.\n\
15904 Usage is `condition N COND', where N is an integer and COND is an\n\
15905 expression to be evaluated whenever breakpoint N is reached."));
15906 set_cmd_completer (c, condition_completer);
15907
15908 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15909 Set a temporary breakpoint.\n\
15910 Like \"break\" except the breakpoint is only temporary,\n\
15911 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15912 by using \"enable delete\" on the breakpoint number.\n\
15913 \n"
15914 BREAK_ARGS_HELP ("tbreak")));
15915 set_cmd_completer (c, location_completer);
15916
15917 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15918 Set a hardware assisted breakpoint.\n\
15919 Like \"break\" except the breakpoint requires hardware support,\n\
15920 some target hardware may not have this support.\n\
15921 \n"
15922 BREAK_ARGS_HELP ("hbreak")));
15923 set_cmd_completer (c, location_completer);
15924
15925 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15926 Set a temporary hardware assisted breakpoint.\n\
15927 Like \"hbreak\" except the breakpoint is only temporary,\n\
15928 so it will be deleted when hit.\n\
15929 \n"
15930 BREAK_ARGS_HELP ("thbreak")));
15931 set_cmd_completer (c, location_completer);
15932
15933 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15934 Enable some breakpoints.\n\
15935 Give breakpoint numbers (separated by spaces) as arguments.\n\
15936 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15937 This is used to cancel the effect of the \"disable\" command.\n\
15938 With a subcommand you can enable temporarily."),
15939 &enablelist, "enable ", 1, &cmdlist);
15940
15941 add_com_alias ("en", "enable", class_breakpoint, 1);
15942
15943 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15944 Enable some breakpoints.\n\
15945 Give breakpoint numbers (separated by spaces) as arguments.\n\
15946 This is used to cancel the effect of the \"disable\" command.\n\
15947 May be abbreviated to simply \"enable\".\n"),
15948 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15949
15950 add_cmd ("once", no_class, enable_once_command, _("\
15951 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15952 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15953 &enablebreaklist);
15954
15955 add_cmd ("delete", no_class, enable_delete_command, _("\
15956 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15957 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15958 &enablebreaklist);
15959
15960 add_cmd ("count", no_class, enable_count_command, _("\
15961 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15962 If a breakpoint is hit while enabled in this fashion,\n\
15963 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15964 &enablebreaklist);
15965
15966 add_cmd ("delete", no_class, enable_delete_command, _("\
15967 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15968 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15969 &enablelist);
15970
15971 add_cmd ("once", no_class, enable_once_command, _("\
15972 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15973 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15974 &enablelist);
15975
15976 add_cmd ("count", no_class, enable_count_command, _("\
15977 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15978 If a breakpoint is hit while enabled in this fashion,\n\
15979 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15980 &enablelist);
15981
15982 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15983 Disable some breakpoints.\n\
15984 Arguments are breakpoint numbers with spaces in between.\n\
15985 To disable all breakpoints, give no argument.\n\
15986 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15987 &disablelist, "disable ", 1, &cmdlist);
15988 add_com_alias ("dis", "disable", class_breakpoint, 1);
15989 add_com_alias ("disa", "disable", class_breakpoint, 1);
15990
15991 add_cmd ("breakpoints", class_alias, disable_command, _("\
15992 Disable some breakpoints.\n\
15993 Arguments are breakpoint numbers with spaces in between.\n\
15994 To disable all breakpoints, give no argument.\n\
15995 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15996 This command may be abbreviated \"disable\"."),
15997 &disablelist);
15998
15999 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16000 Delete some breakpoints or auto-display expressions.\n\
16001 Arguments are breakpoint numbers with spaces in between.\n\
16002 To delete all breakpoints, give no argument.\n\
16003 \n\
16004 Also a prefix command for deletion of other GDB objects.\n\
16005 The \"unset\" command is also an alias for \"delete\"."),
16006 &deletelist, "delete ", 1, &cmdlist);
16007 add_com_alias ("d", "delete", class_breakpoint, 1);
16008 add_com_alias ("del", "delete", class_breakpoint, 1);
16009
16010 add_cmd ("breakpoints", class_alias, delete_command, _("\
16011 Delete some breakpoints or auto-display expressions.\n\
16012 Arguments are breakpoint numbers with spaces in between.\n\
16013 To delete all breakpoints, give no argument.\n\
16014 This command may be abbreviated \"delete\"."),
16015 &deletelist);
16016
16017 add_com ("clear", class_breakpoint, clear_command, _("\
16018 Clear breakpoint at specified location.\n\
16019 Argument may be a linespec, explicit, or address location as described below.\n\
16020 \n\
16021 With no argument, clears all breakpoints in the line that the selected frame\n\
16022 is executing in.\n"
16023 "\n" LOCATION_HELP_STRING "\n\
16024 See also the \"delete\" command which clears breakpoints by number."));
16025 add_com_alias ("cl", "clear", class_breakpoint, 1);
16026
16027 c = add_com ("break", class_breakpoint, break_command, _("\
16028 Set breakpoint at specified location.\n"
16029 BREAK_ARGS_HELP ("break")));
16030 set_cmd_completer (c, location_completer);
16031
16032 add_com_alias ("b", "break", class_run, 1);
16033 add_com_alias ("br", "break", class_run, 1);
16034 add_com_alias ("bre", "break", class_run, 1);
16035 add_com_alias ("brea", "break", class_run, 1);
16036
16037 if (dbx_commands)
16038 {
16039 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16040 Break in function/address or break at a line in the current file."),
16041 &stoplist, "stop ", 1, &cmdlist);
16042 add_cmd ("in", class_breakpoint, stopin_command,
16043 _("Break in function or address."), &stoplist);
16044 add_cmd ("at", class_breakpoint, stopat_command,
16045 _("Break at a line in the current file."), &stoplist);
16046 add_com ("status", class_info, info_breakpoints_command, _("\
16047 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16048 The \"Type\" column indicates one of:\n\
16049 \tbreakpoint - normal breakpoint\n\
16050 \twatchpoint - watchpoint\n\
16051 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16052 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16053 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16054 address and file/line number respectively.\n\
16055 \n\
16056 Convenience variable \"$_\" and default examine address for \"x\"\n\
16057 are set to the address of the last breakpoint listed unless the command\n\
16058 is prefixed with \"server \".\n\n\
16059 Convenience variable \"$bpnum\" contains the number of the last\n\
16060 breakpoint set."));
16061 }
16062
16063 add_info ("breakpoints", info_breakpoints_command, _("\
16064 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16065 The \"Type\" column indicates one of:\n\
16066 \tbreakpoint - normal breakpoint\n\
16067 \twatchpoint - watchpoint\n\
16068 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16069 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16070 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16071 address and file/line number respectively.\n\
16072 \n\
16073 Convenience variable \"$_\" and default examine address for \"x\"\n\
16074 are set to the address of the last breakpoint listed unless the command\n\
16075 is prefixed with \"server \".\n\n\
16076 Convenience variable \"$bpnum\" contains the number of the last\n\
16077 breakpoint set."));
16078
16079 add_info_alias ("b", "breakpoints", 1);
16080
16081 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16082 Status of all breakpoints, or breakpoint number NUMBER.\n\
16083 The \"Type\" column indicates one of:\n\
16084 \tbreakpoint - normal breakpoint\n\
16085 \twatchpoint - watchpoint\n\
16086 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16087 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16088 \tuntil - internal breakpoint used by the \"until\" command\n\
16089 \tfinish - internal breakpoint used by the \"finish\" command\n\
16090 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16091 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16092 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16093 address and file/line number respectively.\n\
16094 \n\
16095 Convenience variable \"$_\" and default examine address for \"x\"\n\
16096 are set to the address of the last breakpoint listed unless the command\n\
16097 is prefixed with \"server \".\n\n\
16098 Convenience variable \"$bpnum\" contains the number of the last\n\
16099 breakpoint set."),
16100 &maintenanceinfolist);
16101
16102 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16103 Set catchpoints to catch events."),
16104 &catch_cmdlist, "catch ",
16105 0/*allow-unknown*/, &cmdlist);
16106
16107 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16108 Set temporary catchpoints to catch events."),
16109 &tcatch_cmdlist, "tcatch ",
16110 0/*allow-unknown*/, &cmdlist);
16111
16112 add_catch_command ("fork", _("Catch calls to fork."),
16113 catch_fork_command_1,
16114 NULL,
16115 (void *) (uintptr_t) catch_fork_permanent,
16116 (void *) (uintptr_t) catch_fork_temporary);
16117 add_catch_command ("vfork", _("Catch calls to vfork."),
16118 catch_fork_command_1,
16119 NULL,
16120 (void *) (uintptr_t) catch_vfork_permanent,
16121 (void *) (uintptr_t) catch_vfork_temporary);
16122 add_catch_command ("exec", _("Catch calls to exec."),
16123 catch_exec_command_1,
16124 NULL,
16125 CATCH_PERMANENT,
16126 CATCH_TEMPORARY);
16127 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16128 Usage: catch load [REGEX]\n\
16129 If REGEX is given, only stop for libraries matching the regular expression."),
16130 catch_load_command_1,
16131 NULL,
16132 CATCH_PERMANENT,
16133 CATCH_TEMPORARY);
16134 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16135 Usage: catch unload [REGEX]\n\
16136 If REGEX is given, only stop for libraries matching the regular expression."),
16137 catch_unload_command_1,
16138 NULL,
16139 CATCH_PERMANENT,
16140 CATCH_TEMPORARY);
16141
16142 c = add_com ("watch", class_breakpoint, watch_command, _("\
16143 Set a watchpoint for an expression.\n\
16144 Usage: watch [-l|-location] EXPRESSION\n\
16145 A watchpoint stops execution of your program whenever the value of\n\
16146 an expression changes.\n\
16147 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16148 the memory to which it refers."));
16149 set_cmd_completer (c, expression_completer);
16150
16151 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16152 Set a read watchpoint for an expression.\n\
16153 Usage: rwatch [-l|-location] EXPRESSION\n\
16154 A watchpoint stops execution of your program whenever the value of\n\
16155 an expression is read.\n\
16156 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16157 the memory to which it refers."));
16158 set_cmd_completer (c, expression_completer);
16159
16160 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16161 Set a watchpoint for an expression.\n\
16162 Usage: awatch [-l|-location] EXPRESSION\n\
16163 A watchpoint stops execution of your program whenever the value of\n\
16164 an expression is either read or written.\n\
16165 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16166 the memory to which it refers."));
16167 set_cmd_completer (c, expression_completer);
16168
16169 add_info ("watchpoints", info_watchpoints_command, _("\
16170 Status of specified watchpoints (all watchpoints if no argument)."));
16171
16172 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16173 respond to changes - contrary to the description. */
16174 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16175 &can_use_hw_watchpoints, _("\
16176 Set debugger's willingness to use watchpoint hardware."), _("\
16177 Show debugger's willingness to use watchpoint hardware."), _("\
16178 If zero, gdb will not use hardware for new watchpoints, even if\n\
16179 such is available. (However, any hardware watchpoints that were\n\
16180 created before setting this to nonzero, will continue to use watchpoint\n\
16181 hardware.)"),
16182 NULL,
16183 show_can_use_hw_watchpoints,
16184 &setlist, &showlist);
16185
16186 can_use_hw_watchpoints = 1;
16187
16188 /* Tracepoint manipulation commands. */
16189
16190 c = add_com ("trace", class_breakpoint, trace_command, _("\
16191 Set a tracepoint at specified location.\n\
16192 \n"
16193 BREAK_ARGS_HELP ("trace") "\n\
16194 Do \"help tracepoints\" for info on other tracepoint commands."));
16195 set_cmd_completer (c, location_completer);
16196
16197 add_com_alias ("tp", "trace", class_alias, 0);
16198 add_com_alias ("tr", "trace", class_alias, 1);
16199 add_com_alias ("tra", "trace", class_alias, 1);
16200 add_com_alias ("trac", "trace", class_alias, 1);
16201
16202 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16203 Set a fast tracepoint at specified location.\n\
16204 \n"
16205 BREAK_ARGS_HELP ("ftrace") "\n\
16206 Do \"help tracepoints\" for info on other tracepoint commands."));
16207 set_cmd_completer (c, location_completer);
16208
16209 c = add_com ("strace", class_breakpoint, strace_command, _("\
16210 Set a static tracepoint at location or marker.\n\
16211 \n\
16212 strace [LOCATION] [if CONDITION]\n\
16213 LOCATION may be a linespec, explicit, or address location (described below) \n\
16214 or -m MARKER_ID.\n\n\
16215 If a marker id is specified, probe the marker with that name. With\n\
16216 no LOCATION, uses current execution address of the selected stack frame.\n\
16217 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16218 This collects arbitrary user data passed in the probe point call to the\n\
16219 tracing library. You can inspect it when analyzing the trace buffer,\n\
16220 by printing the $_sdata variable like any other convenience variable.\n\
16221 \n\
16222 CONDITION is a boolean expression.\n\
16223 \n" LOCATION_HELP_STRING "\n\
16224 Multiple tracepoints at one place are permitted, and useful if their\n\
16225 conditions are different.\n\
16226 \n\
16227 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16228 Do \"help tracepoints\" for info on other tracepoint commands."));
16229 set_cmd_completer (c, location_completer);
16230
16231 add_info ("tracepoints", info_tracepoints_command, _("\
16232 Status of specified tracepoints (all tracepoints if no argument).\n\
16233 Convenience variable \"$tpnum\" contains the number of the\n\
16234 last tracepoint set."));
16235
16236 add_info_alias ("tp", "tracepoints", 1);
16237
16238 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16239 Delete specified tracepoints.\n\
16240 Arguments are tracepoint numbers, separated by spaces.\n\
16241 No argument means delete all tracepoints."),
16242 &deletelist);
16243 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16244
16245 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16246 Disable specified tracepoints.\n\
16247 Arguments are tracepoint numbers, separated by spaces.\n\
16248 No argument means disable all tracepoints."),
16249 &disablelist);
16250 deprecate_cmd (c, "disable");
16251
16252 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16253 Enable specified tracepoints.\n\
16254 Arguments are tracepoint numbers, separated by spaces.\n\
16255 No argument means enable all tracepoints."),
16256 &enablelist);
16257 deprecate_cmd (c, "enable");
16258
16259 add_com ("passcount", class_trace, trace_pass_command, _("\
16260 Set the passcount for a tracepoint.\n\
16261 The trace will end when the tracepoint has been passed 'count' times.\n\
16262 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16263 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16264
16265 add_prefix_cmd ("save", class_breakpoint, save_command,
16266 _("Save breakpoint definitions as a script."),
16267 &save_cmdlist, "save ",
16268 0/*allow-unknown*/, &cmdlist);
16269
16270 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16271 Save current breakpoint definitions as a script.\n\
16272 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16273 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16274 session to restore them."),
16275 &save_cmdlist);
16276 set_cmd_completer (c, filename_completer);
16277
16278 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16279 Save current tracepoint definitions as a script.\n\
16280 Use the 'source' command in another debug session to restore them."),
16281 &save_cmdlist);
16282 set_cmd_completer (c, filename_completer);
16283
16284 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16285 deprecate_cmd (c, "save tracepoints");
16286
16287 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16288 Breakpoint specific settings\n\
16289 Configure various breakpoint-specific variables such as\n\
16290 pending breakpoint behavior"),
16291 &breakpoint_set_cmdlist, "set breakpoint ",
16292 0/*allow-unknown*/, &setlist);
16293 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16294 Breakpoint specific settings\n\
16295 Configure various breakpoint-specific variables such as\n\
16296 pending breakpoint behavior"),
16297 &breakpoint_show_cmdlist, "show breakpoint ",
16298 0/*allow-unknown*/, &showlist);
16299
16300 add_setshow_auto_boolean_cmd ("pending", no_class,
16301 &pending_break_support, _("\
16302 Set debugger's behavior regarding pending breakpoints."), _("\
16303 Show debugger's behavior regarding pending breakpoints."), _("\
16304 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16305 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16306 an error. If auto, an unrecognized breakpoint location results in a\n\
16307 user-query to see if a pending breakpoint should be created."),
16308 NULL,
16309 show_pending_break_support,
16310 &breakpoint_set_cmdlist,
16311 &breakpoint_show_cmdlist);
16312
16313 pending_break_support = AUTO_BOOLEAN_AUTO;
16314
16315 add_setshow_boolean_cmd ("auto-hw", no_class,
16316 &automatic_hardware_breakpoints, _("\
16317 Set automatic usage of hardware breakpoints."), _("\
16318 Show automatic usage of hardware breakpoints."), _("\
16319 If set, the debugger will automatically use hardware breakpoints for\n\
16320 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16321 a warning will be emitted for such breakpoints."),
16322 NULL,
16323 show_automatic_hardware_breakpoints,
16324 &breakpoint_set_cmdlist,
16325 &breakpoint_show_cmdlist);
16326
16327 add_setshow_boolean_cmd ("always-inserted", class_support,
16328 &always_inserted_mode, _("\
16329 Set mode for inserting breakpoints."), _("\
16330 Show mode for inserting breakpoints."), _("\
16331 When this mode is on, breakpoints are inserted immediately as soon as\n\
16332 they're created, kept inserted even when execution stops, and removed\n\
16333 only when the user deletes them. When this mode is off (the default),\n\
16334 breakpoints are inserted only when execution continues, and removed\n\
16335 when execution stops."),
16336 NULL,
16337 &show_always_inserted_mode,
16338 &breakpoint_set_cmdlist,
16339 &breakpoint_show_cmdlist);
16340
16341 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16342 condition_evaluation_enums,
16343 &condition_evaluation_mode_1, _("\
16344 Set mode of breakpoint condition evaluation."), _("\
16345 Show mode of breakpoint condition evaluation."), _("\
16346 When this is set to \"host\", breakpoint conditions will be\n\
16347 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16348 breakpoint conditions will be downloaded to the target (if the target\n\
16349 supports such feature) and conditions will be evaluated on the target's side.\n\
16350 If this is set to \"auto\" (default), this will be automatically set to\n\
16351 \"target\" if it supports condition evaluation, otherwise it will\n\
16352 be set to \"gdb\""),
16353 &set_condition_evaluation_mode,
16354 &show_condition_evaluation_mode,
16355 &breakpoint_set_cmdlist,
16356 &breakpoint_show_cmdlist);
16357
16358 add_com ("break-range", class_breakpoint, break_range_command, _("\
16359 Set a breakpoint for an address range.\n\
16360 break-range START-LOCATION, END-LOCATION\n\
16361 where START-LOCATION and END-LOCATION can be one of the following:\n\
16362 LINENUM, for that line in the current file,\n\
16363 FILE:LINENUM, for that line in that file,\n\
16364 +OFFSET, for that number of lines after the current line\n\
16365 or the start of the range\n\
16366 FUNCTION, for the first line in that function,\n\
16367 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16368 *ADDRESS, for the instruction at that address.\n\
16369 \n\
16370 The breakpoint will stop execution of the inferior whenever it executes\n\
16371 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16372 range (including START-LOCATION and END-LOCATION)."));
16373
16374 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16375 Set a dynamic printf at specified location.\n\
16376 dprintf location,format string,arg1,arg2,...\n\
16377 location may be a linespec, explicit, or address location.\n"
16378 "\n" LOCATION_HELP_STRING));
16379 set_cmd_completer (c, location_completer);
16380
16381 add_setshow_enum_cmd ("dprintf-style", class_support,
16382 dprintf_style_enums, &dprintf_style, _("\
16383 Set the style of usage for dynamic printf."), _("\
16384 Show the style of usage for dynamic printf."), _("\
16385 This setting chooses how GDB will do a dynamic printf.\n\
16386 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16387 console, as with the \"printf\" command.\n\
16388 If the value is \"call\", the print is done by calling a function in your\n\
16389 program; by default printf(), but you can choose a different function or\n\
16390 output stream by setting dprintf-function and dprintf-channel."),
16391 update_dprintf_commands, NULL,
16392 &setlist, &showlist);
16393
16394 dprintf_function = xstrdup ("printf");
16395 add_setshow_string_cmd ("dprintf-function", class_support,
16396 &dprintf_function, _("\
16397 Set the function to use for dynamic printf"), _("\
16398 Show the function to use for dynamic printf"), NULL,
16399 update_dprintf_commands, NULL,
16400 &setlist, &showlist);
16401
16402 dprintf_channel = xstrdup ("");
16403 add_setshow_string_cmd ("dprintf-channel", class_support,
16404 &dprintf_channel, _("\
16405 Set the channel to use for dynamic printf"), _("\
16406 Show the channel to use for dynamic printf"), NULL,
16407 update_dprintf_commands, NULL,
16408 &setlist, &showlist);
16409
16410 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16411 &disconnected_dprintf, _("\
16412 Set whether dprintf continues after GDB disconnects."), _("\
16413 Show whether dprintf continues after GDB disconnects."), _("\
16414 Use this to let dprintf commands continue to hit and produce output\n\
16415 even if GDB disconnects or detaches from the target."),
16416 NULL,
16417 NULL,
16418 &setlist, &showlist);
16419
16420 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16421 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16422 (target agent only) This is useful for formatted output in user-defined commands."));
16423
16424 automatic_hardware_breakpoints = 1;
16425
16426 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16427 observer_attach_thread_exit (remove_threaded_breakpoints);
16428 }
This page took 0.393647 seconds and 4 git commands to generate.