Change psymtab_search_name to return a unique_xmalloc_ptr
[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
85 /* Enums for exception-handling support. */
86 enum exception_event_kind
87 {
88 EX_EVENT_THROW,
89 EX_EVENT_RETHROW,
90 EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions. */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (const char *,
106 void (*) (struct breakpoint *,
107 void *),
108 void *);
109
110 static void ignore_command (char *, int);
111
112 static int breakpoint_re_set_one (void *);
113
114 static void breakpoint_re_set_default (struct breakpoint *);
115
116 static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
120
121 static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
123 gdb::unique_xmalloc_ptr<char>,
124 gdb::unique_xmalloc_ptr<char>,
125 enum bptype,
126 enum bpdisp, int, int,
127 int,
128 const struct breakpoint_ops *,
129 int, int, int, unsigned);
130
131 static void decode_location_default (struct breakpoint *b,
132 const struct event_location *location,
133 struct program_space *search_pspace,
134 struct symtabs_and_lines *sals);
135
136 static void clear_command (char *, int);
137
138 static void catch_command (char *, int);
139
140 static int can_use_hardware_watchpoint (struct value *);
141
142 static void break_command_1 (char *, int, int);
143
144 static void mention (struct breakpoint *);
145
146 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
147 enum bptype,
148 const struct breakpoint_ops *);
149 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
150 const struct symtab_and_line *);
151
152 /* This function is used in gdbtk sources and thus can not be made
153 static. */
154 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
155 struct symtab_and_line,
156 enum bptype,
157 const struct breakpoint_ops *);
158
159 static struct breakpoint *
160 momentary_breakpoint_from_master (struct breakpoint *orig,
161 enum bptype type,
162 const struct breakpoint_ops *ops,
163 int loc_enabled);
164
165 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
166
167 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
168 CORE_ADDR bpaddr,
169 enum bptype bptype);
170
171 static void describe_other_breakpoints (struct gdbarch *,
172 struct program_space *, CORE_ADDR,
173 struct obj_section *, int);
174
175 static int watchpoint_locations_match (struct bp_location *loc1,
176 struct bp_location *loc2);
177
178 static int breakpoint_location_address_match (struct bp_location *bl,
179 struct address_space *aspace,
180 CORE_ADDR addr);
181
182 static int breakpoint_location_address_range_overlap (struct bp_location *,
183 struct address_space *,
184 CORE_ADDR, int);
185
186 static void breakpoints_info (char *, int);
187
188 static void watchpoints_info (char *, int);
189
190 static int breakpoint_1 (char *, int,
191 int (*) (const struct breakpoint *));
192
193 static int breakpoint_cond_eval (void *);
194
195 static void cleanup_executing_breakpoints (void *);
196
197 static void commands_command (char *, int);
198
199 static void condition_command (char *, int);
200
201 static int remove_breakpoint (struct bp_location *);
202 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
203
204 static enum print_stop_action print_bp_stop_message (bpstat bs);
205
206 static int watchpoint_check (void *);
207
208 static void maintenance_info_breakpoints (char *, int);
209
210 static int hw_breakpoint_used_count (void);
211
212 static int hw_watchpoint_use_count (struct breakpoint *);
213
214 static int hw_watchpoint_used_count_others (struct breakpoint *except,
215 enum bptype type,
216 int *other_type_used);
217
218 static void hbreak_command (char *, int);
219
220 static void thbreak_command (char *, int);
221
222 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
223 int count);
224
225 static void stop_command (char *arg, int from_tty);
226
227 static void stopin_command (char *arg, int from_tty);
228
229 static void stopat_command (char *arg, int from_tty);
230
231 static void tcatch_command (char *arg, int from_tty);
232
233 static void free_bp_location (struct bp_location *loc);
234 static void incref_bp_location (struct bp_location *loc);
235 static void decref_bp_location (struct bp_location **loc);
236
237 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
238
239 /* update_global_location_list's modes of operation wrt to whether to
240 insert locations now. */
241 enum ugll_insert_mode
242 {
243 /* Don't insert any breakpoint locations into the inferior, only
244 remove already-inserted locations that no longer should be
245 inserted. Functions that delete a breakpoint or breakpoints
246 should specify this mode, so that deleting a breakpoint doesn't
247 have the side effect of inserting the locations of other
248 breakpoints that are marked not-inserted, but should_be_inserted
249 returns true on them.
250
251 This behavior is useful is situations close to tear-down -- e.g.,
252 after an exec, while the target still has execution, but
253 breakpoint shadows of the previous executable image should *NOT*
254 be restored to the new image; or before detaching, where the
255 target still has execution and wants to delete breakpoints from
256 GDB's lists, and all breakpoints had already been removed from
257 the inferior. */
258 UGLL_DONT_INSERT,
259
260 /* May insert breakpoints iff breakpoints_should_be_inserted_now
261 claims breakpoints should be inserted now. */
262 UGLL_MAY_INSERT,
263
264 /* Insert locations now, irrespective of
265 breakpoints_should_be_inserted_now. E.g., say all threads are
266 stopped right now, and the user did "continue". We need to
267 insert breakpoints _before_ resuming the target, but
268 UGLL_MAY_INSERT wouldn't insert them, because
269 breakpoints_should_be_inserted_now returns false at that point,
270 as no thread is running yet. */
271 UGLL_INSERT
272 };
273
274 static void update_global_location_list (enum ugll_insert_mode);
275
276 static void update_global_location_list_nothrow (enum ugll_insert_mode);
277
278 static int is_hardware_watchpoint (const struct breakpoint *bpt);
279
280 static void insert_breakpoint_locations (void);
281
282 static void tracepoints_info (char *, int);
283
284 static void delete_trace_command (char *, int);
285
286 static void enable_trace_command (char *, int);
287
288 static void disable_trace_command (char *, int);
289
290 static void trace_pass_command (char *, int);
291
292 static void set_tracepoint_count (int num);
293
294 static int is_masked_watchpoint (const struct breakpoint *b);
295
296 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
297
298 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
299 otherwise. */
300
301 static int strace_marker_p (struct breakpoint *b);
302
303 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
304 that are implemented on top of software or hardware breakpoints
305 (user breakpoints, internal and momentary breakpoints, etc.). */
306 static struct breakpoint_ops bkpt_base_breakpoint_ops;
307
308 /* Internal breakpoints class type. */
309 static struct breakpoint_ops internal_breakpoint_ops;
310
311 /* Momentary breakpoints class type. */
312 static struct breakpoint_ops momentary_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function;
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel;
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 struct thread_info *tp;
475
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
483 if (threads_are_executing ())
484 return 1;
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
492 }
493 return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation. */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation. */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to. */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552 }
553
554 void _initialize_breakpoint (void);
555
556 /* Are we executing breakpoint commands? */
557 static int executing_breakpoint_commands;
558
559 /* Are overlay event breakpoints enabled? */
560 static int overlay_events_enabled;
561
562 /* See description in breakpoint.h. */
563 int target_exact_watchpoints = 0;
564
565 /* Walk the following statement or block through all breakpoints.
566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
567 current breakpoint. */
568
569 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
570
571 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
575
576 /* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
579
580 #define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_locations; \
582 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
583 BP_TMP++)
584
585 /* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
599 /* Iterator for tracepoints only. */
600
601 #define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
603 if (is_tracepoint (B))
604
605 /* Chains of all breakpoints defined. */
606
607 struct breakpoint *breakpoint_chain;
608
609 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
610
611 static struct bp_location **bp_locations;
612
613 /* Number of elements of BP_LOCATIONS. */
614
615 static unsigned bp_locations_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATIONS which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
621 an address you need to read. */
622
623 static CORE_ADDR bp_locations_placed_address_before_address_max;
624
625 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATIONS to
629 scan for shadow bytes for an address you need to read. */
630
631 static CORE_ADDR bp_locations_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634 from the bp_locations array, but for which a hit may still be
635 reported by a target. */
636 VEC(bp_location_p) *moribund_locations = NULL;
637
638 /* Number of last breakpoint made. */
639
640 static int breakpoint_count;
641
642 /* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646 static int prev_breakpoint_count;
647
648 /* Number of last tracepoint made. */
649
650 static int tracepoint_count;
651
652 static struct cmd_list_element *breakpoint_set_cmdlist;
653 static struct cmd_list_element *breakpoint_show_cmdlist;
654 struct cmd_list_element *save_cmdlist;
655
656 /* See declaration at breakpoint.h. */
657
658 struct breakpoint *
659 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661 {
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671 }
672
673 /* Return whether a breakpoint is an active enabled breakpoint. */
674 static int
675 breakpoint_enabled (struct breakpoint *b)
676 {
677 return (b->enable_state == bp_enabled);
678 }
679
680 /* Set breakpoint count to NUM. */
681
682 static void
683 set_breakpoint_count (int num)
684 {
685 prev_breakpoint_count = breakpoint_count;
686 breakpoint_count = num;
687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
688 }
689
690 /* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692 static int rbreak_start_breakpoint_count;
693
694 /* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
696
697 void
698 start_rbreak_breakpoints (void)
699 {
700 rbreak_start_breakpoint_count = breakpoint_count;
701 }
702
703 /* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
705
706 void
707 end_rbreak_breakpoints (void)
708 {
709 prev_breakpoint_count = rbreak_start_breakpoint_count;
710 }
711
712 /* Used in run_command to zero the hit count when a new run starts. */
713
714 void
715 clear_breakpoint_hit_counts (void)
716 {
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721 }
722
723 /* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726 static struct counted_command_line *
727 alloc_counted_command_line (struct command_line *commands)
728 {
729 struct counted_command_line *result = XNEW (struct counted_command_line);
730
731 result->refc = 1;
732 result->commands = commands;
733
734 return result;
735 }
736
737 /* Increment reference count. This does nothing if CMD is NULL. */
738
739 static void
740 incref_counted_command_line (struct counted_command_line *cmd)
741 {
742 if (cmd)
743 ++cmd->refc;
744 }
745
746 /* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750 static void
751 decref_counted_command_line (struct counted_command_line **cmdp)
752 {
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762 }
763
764 /* A cleanup function that calls decref_counted_command_line. */
765
766 static void
767 do_cleanup_counted_command_line (void *arg)
768 {
769 decref_counted_command_line ((struct counted_command_line **) arg);
770 }
771
772 /* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775 static struct cleanup *
776 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777 {
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779 }
780
781 \f
782 /* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785 struct breakpoint *
786 get_breakpoint (int num)
787 {
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795 }
796
797 \f
798
799 /* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802 static void
803 mark_breakpoint_modified (struct breakpoint *b)
804 {
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820 }
821
822 /* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825 static void
826 mark_breakpoint_location_modified (struct bp_location *loc)
827 {
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841 }
842
843 /* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846 static void
847 set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849 {
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
897 update_global_location_list (UGLL_MAY_INSERT);
898 }
899
900 return;
901 }
902
903 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906 static void
907 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909 {
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919 }
920
921 /* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_locations_compare function. */
924
925 static int
926 bp_locations_compare_addrs (const void *ap, const void *bp)
927 {
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935 }
936
937 /* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942 static struct bp_location **
943 get_first_locp_gte_addr (CORE_ADDR address)
944 {
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
953 locp_found = ((struct bp_location **)
954 bsearch (&dummy_locp, bp_locations, bp_locations_count,
955 sizeof (struct bp_location **),
956 bp_locations_compare_addrs));
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
964 while ((locp_found - 1) >= bp_locations
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969 }
970
971 void
972 set_breakpoint_condition (struct breakpoint *b, const char *exp,
973 int from_tty)
974 {
975 xfree (b->cond_string);
976 b->cond_string = NULL;
977
978 if (is_watchpoint (b))
979 {
980 struct watchpoint *w = (struct watchpoint *) b;
981
982 w->cond_exp.reset ();
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
990 loc->cond.reset ();
991
992 /* No need to free the condition agent expression
993 bytecode (if we have one). We will handle this
994 when we go through update_global_location_list. */
995 }
996 }
997
998 if (*exp == 0)
999 {
1000 if (from_tty)
1001 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1002 }
1003 else
1004 {
1005 const char *arg = exp;
1006
1007 /* I don't know if it matters whether this is the string the user
1008 typed in or the decompiled expression. */
1009 b->cond_string = xstrdup (arg);
1010 b->condition_not_parsed = 0;
1011
1012 if (is_watchpoint (b))
1013 {
1014 struct watchpoint *w = (struct watchpoint *) b;
1015
1016 innermost_block = NULL;
1017 arg = exp;
1018 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1019 if (*arg)
1020 error (_("Junk at end of expression"));
1021 w->cond_exp_valid_block = innermost_block;
1022 }
1023 else
1024 {
1025 struct bp_location *loc;
1026
1027 for (loc = b->loc; loc; loc = loc->next)
1028 {
1029 arg = exp;
1030 loc->cond =
1031 parse_exp_1 (&arg, loc->address,
1032 block_for_pc (loc->address), 0);
1033 if (*arg)
1034 error (_("Junk at end of expression"));
1035 }
1036 }
1037 }
1038 mark_breakpoint_modified (b);
1039
1040 observer_notify_breakpoint_modified (b);
1041 }
1042
1043 /* Completion for the "condition" command. */
1044
1045 static void
1046 condition_completer (struct cmd_list_element *cmd,
1047 completion_tracker &tracker,
1048 const char *text, const char *word)
1049 {
1050 const char *space;
1051
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (!isdigit (text[1]))
1064 complete_internalvar (tracker, &text[1]);
1065 return;
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 {
1079 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1080 tracker.add_completion (std::move (copy));
1081 }
1082 }
1083
1084 return;
1085 }
1086
1087 /* We're completing the expression part. */
1088 text = skip_spaces_const (space);
1089 expression_completer (cmd, tracker, text, word);
1090 }
1091
1092 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1093
1094 static void
1095 condition_command (char *arg, int from_tty)
1096 {
1097 struct breakpoint *b;
1098 char *p;
1099 int bnum;
1100
1101 if (arg == 0)
1102 error_no_arg (_("breakpoint number"));
1103
1104 p = arg;
1105 bnum = get_number (&p);
1106 if (bnum == 0)
1107 error (_("Bad breakpoint argument: '%s'"), arg);
1108
1109 ALL_BREAKPOINTS (b)
1110 if (b->number == bnum)
1111 {
1112 /* Check if this breakpoint has a "stop" method implemented in an
1113 extension language. This method and conditions entered into GDB
1114 from the CLI are mutually exclusive. */
1115 const struct extension_language_defn *extlang
1116 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1117
1118 if (extlang != NULL)
1119 {
1120 error (_("Only one stop condition allowed. There is currently"
1121 " a %s stop condition defined for this breakpoint."),
1122 ext_lang_capitalized_name (extlang));
1123 }
1124 set_breakpoint_condition (b, p, from_tty);
1125
1126 if (is_breakpoint (b))
1127 update_global_location_list (UGLL_MAY_INSERT);
1128
1129 return;
1130 }
1131
1132 error (_("No breakpoint number %d."), bnum);
1133 }
1134
1135 /* Check that COMMAND do not contain commands that are suitable
1136 only for tracepoints and not suitable for ordinary breakpoints.
1137 Throw if any such commands is found. */
1138
1139 static void
1140 check_no_tracepoint_commands (struct command_line *commands)
1141 {
1142 struct command_line *c;
1143
1144 for (c = commands; c; c = c->next)
1145 {
1146 int i;
1147
1148 if (c->control_type == while_stepping_control)
1149 error (_("The 'while-stepping' command can "
1150 "only be used for tracepoints"));
1151
1152 for (i = 0; i < c->body_count; ++i)
1153 check_no_tracepoint_commands ((c->body_list)[i]);
1154
1155 /* Not that command parsing removes leading whitespace and comment
1156 lines and also empty lines. So, we only need to check for
1157 command directly. */
1158 if (strstr (c->line, "collect ") == c->line)
1159 error (_("The 'collect' command can only be used for tracepoints"));
1160
1161 if (strstr (c->line, "teval ") == c->line)
1162 error (_("The 'teval' command can only be used for tracepoints"));
1163 }
1164 }
1165
1166 struct longjmp_breakpoint : public breakpoint
1167 {
1168 ~longjmp_breakpoint () override;
1169 };
1170
1171 /* Encapsulate tests for different types of tracepoints. */
1172
1173 static bool
1174 is_tracepoint_type (bptype type)
1175 {
1176 return (type == bp_tracepoint
1177 || type == bp_fast_tracepoint
1178 || type == bp_static_tracepoint);
1179 }
1180
1181 static bool
1182 is_longjmp_type (bptype type)
1183 {
1184 return type == bp_longjmp || type == bp_exception;
1185 }
1186
1187 int
1188 is_tracepoint (const struct breakpoint *b)
1189 {
1190 return is_tracepoint_type (b->type);
1191 }
1192
1193 /* Factory function to create an appropriate instance of breakpoint given
1194 TYPE. */
1195
1196 static std::unique_ptr<breakpoint>
1197 new_breakpoint_from_type (bptype type)
1198 {
1199 breakpoint *b;
1200
1201 if (is_tracepoint_type (type))
1202 b = new tracepoint ();
1203 else if (is_longjmp_type (type))
1204 b = new longjmp_breakpoint ();
1205 else
1206 b = new breakpoint ();
1207
1208 return std::unique_ptr<breakpoint> (b);
1209 }
1210
1211 /* A helper function that validates that COMMANDS are valid for a
1212 breakpoint. This function will throw an exception if a problem is
1213 found. */
1214
1215 static void
1216 validate_commands_for_breakpoint (struct breakpoint *b,
1217 struct command_line *commands)
1218 {
1219 if (is_tracepoint (b))
1220 {
1221 struct tracepoint *t = (struct tracepoint *) b;
1222 struct command_line *c;
1223 struct command_line *while_stepping = 0;
1224
1225 /* Reset the while-stepping step count. The previous commands
1226 might have included a while-stepping action, while the new
1227 ones might not. */
1228 t->step_count = 0;
1229
1230 /* We need to verify that each top-level element of commands is
1231 valid for tracepoints, that there's at most one
1232 while-stepping element, and that the while-stepping's body
1233 has valid tracing commands excluding nested while-stepping.
1234 We also need to validate the tracepoint action line in the
1235 context of the tracepoint --- validate_actionline actually
1236 has side effects, like setting the tracepoint's
1237 while-stepping STEP_COUNT, in addition to checking if the
1238 collect/teval actions parse and make sense in the
1239 tracepoint's context. */
1240 for (c = commands; c; c = c->next)
1241 {
1242 if (c->control_type == while_stepping_control)
1243 {
1244 if (b->type == bp_fast_tracepoint)
1245 error (_("The 'while-stepping' command "
1246 "cannot be used for fast tracepoint"));
1247 else if (b->type == bp_static_tracepoint)
1248 error (_("The 'while-stepping' command "
1249 "cannot be used for static tracepoint"));
1250
1251 if (while_stepping)
1252 error (_("The 'while-stepping' command "
1253 "can be used only once"));
1254 else
1255 while_stepping = c;
1256 }
1257
1258 validate_actionline (c->line, b);
1259 }
1260 if (while_stepping)
1261 {
1262 struct command_line *c2;
1263
1264 gdb_assert (while_stepping->body_count == 1);
1265 c2 = while_stepping->body_list[0];
1266 for (; c2; c2 = c2->next)
1267 {
1268 if (c2->control_type == while_stepping_control)
1269 error (_("The 'while-stepping' command cannot be nested"));
1270 }
1271 }
1272 }
1273 else
1274 {
1275 check_no_tracepoint_commands (commands);
1276 }
1277 }
1278
1279 /* Return a vector of all the static tracepoints set at ADDR. The
1280 caller is responsible for releasing the vector. */
1281
1282 VEC(breakpoint_p) *
1283 static_tracepoints_here (CORE_ADDR addr)
1284 {
1285 struct breakpoint *b;
1286 VEC(breakpoint_p) *found = 0;
1287 struct bp_location *loc;
1288
1289 ALL_BREAKPOINTS (b)
1290 if (b->type == bp_static_tracepoint)
1291 {
1292 for (loc = b->loc; loc; loc = loc->next)
1293 if (loc->address == addr)
1294 VEC_safe_push(breakpoint_p, found, b);
1295 }
1296
1297 return found;
1298 }
1299
1300 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1301 validate that only allowed commands are included. */
1302
1303 void
1304 breakpoint_set_commands (struct breakpoint *b,
1305 command_line_up &&commands)
1306 {
1307 validate_commands_for_breakpoint (b, commands.get ());
1308
1309 decref_counted_command_line (&b->commands);
1310 b->commands = alloc_counted_command_line (commands.release ());
1311 observer_notify_breakpoint_modified (b);
1312 }
1313
1314 /* Set the internal `silent' flag on the breakpoint. Note that this
1315 is not the same as the "silent" that may appear in the breakpoint's
1316 commands. */
1317
1318 void
1319 breakpoint_set_silent (struct breakpoint *b, int silent)
1320 {
1321 int old_silent = b->silent;
1322
1323 b->silent = silent;
1324 if (old_silent != silent)
1325 observer_notify_breakpoint_modified (b);
1326 }
1327
1328 /* Set the thread for this breakpoint. If THREAD is -1, make the
1329 breakpoint work for any thread. */
1330
1331 void
1332 breakpoint_set_thread (struct breakpoint *b, int thread)
1333 {
1334 int old_thread = b->thread;
1335
1336 b->thread = thread;
1337 if (old_thread != thread)
1338 observer_notify_breakpoint_modified (b);
1339 }
1340
1341 /* Set the task for this breakpoint. If TASK is 0, make the
1342 breakpoint work for any task. */
1343
1344 void
1345 breakpoint_set_task (struct breakpoint *b, int task)
1346 {
1347 int old_task = b->task;
1348
1349 b->task = task;
1350 if (old_task != task)
1351 observer_notify_breakpoint_modified (b);
1352 }
1353
1354 void
1355 check_tracepoint_command (char *line, void *closure)
1356 {
1357 struct breakpoint *b = (struct breakpoint *) closure;
1358
1359 validate_actionline (line, b);
1360 }
1361
1362 /* A structure used to pass information through
1363 map_breakpoint_numbers. */
1364
1365 struct commands_info
1366 {
1367 /* True if the command was typed at a tty. */
1368 int from_tty;
1369
1370 /* The breakpoint range spec. */
1371 const char *arg;
1372
1373 /* Non-NULL if the body of the commands are being read from this
1374 already-parsed command. */
1375 struct command_line *control;
1376
1377 /* The command lines read from the user, or NULL if they have not
1378 yet been read. */
1379 struct counted_command_line *cmd;
1380 };
1381
1382 /* A callback for map_breakpoint_numbers that sets the commands for
1383 commands_command. */
1384
1385 static void
1386 do_map_commands_command (struct breakpoint *b, void *data)
1387 {
1388 struct commands_info *info = (struct commands_info *) data;
1389
1390 if (info->cmd == NULL)
1391 {
1392 command_line_up l;
1393
1394 if (info->control != NULL)
1395 l = copy_command_lines (info->control->body_list[0]);
1396 else
1397 {
1398 struct cleanup *old_chain;
1399 char *str;
1400
1401 str = xstrprintf (_("Type commands for breakpoint(s) "
1402 "%s, one per line."),
1403 info->arg);
1404
1405 old_chain = make_cleanup (xfree, str);
1406
1407 l = read_command_lines (str,
1408 info->from_tty, 1,
1409 (is_tracepoint (b)
1410 ? check_tracepoint_command : 0),
1411 b);
1412
1413 do_cleanups (old_chain);
1414 }
1415
1416 info->cmd = alloc_counted_command_line (l.release ());
1417 }
1418
1419 /* If a breakpoint was on the list more than once, we don't need to
1420 do anything. */
1421 if (b->commands != info->cmd)
1422 {
1423 validate_commands_for_breakpoint (b, info->cmd->commands);
1424 incref_counted_command_line (info->cmd);
1425 decref_counted_command_line (&b->commands);
1426 b->commands = info->cmd;
1427 observer_notify_breakpoint_modified (b);
1428 }
1429 }
1430
1431 static void
1432 commands_command_1 (const char *arg, int from_tty,
1433 struct command_line *control)
1434 {
1435 struct cleanup *cleanups;
1436 struct commands_info info;
1437
1438 info.from_tty = from_tty;
1439 info.control = control;
1440 info.cmd = NULL;
1441 /* If we read command lines from the user, then `info' will hold an
1442 extra reference to the commands that we must clean up. */
1443 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1444
1445 std::string new_arg;
1446
1447 if (arg == NULL || !*arg)
1448 {
1449 if (breakpoint_count - prev_breakpoint_count > 1)
1450 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1451 breakpoint_count);
1452 else if (breakpoint_count > 0)
1453 new_arg = string_printf ("%d", breakpoint_count);
1454 }
1455 else
1456 new_arg = arg;
1457
1458 info.arg = new_arg.c_str ();
1459
1460 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1461
1462 if (info.cmd == NULL)
1463 error (_("No breakpoints specified."));
1464
1465 do_cleanups (cleanups);
1466 }
1467
1468 static void
1469 commands_command (char *arg, int from_tty)
1470 {
1471 commands_command_1 (arg, from_tty, NULL);
1472 }
1473
1474 /* Like commands_command, but instead of reading the commands from
1475 input stream, takes them from an already parsed command structure.
1476
1477 This is used by cli-script.c to DTRT with breakpoint commands
1478 that are part of if and while bodies. */
1479 enum command_control_type
1480 commands_from_control_command (const char *arg, struct command_line *cmd)
1481 {
1482 commands_command_1 (arg, 0, cmd);
1483 return simple_control;
1484 }
1485
1486 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1487
1488 static int
1489 bp_location_has_shadow (struct bp_location *bl)
1490 {
1491 if (bl->loc_type != bp_loc_software_breakpoint)
1492 return 0;
1493 if (!bl->inserted)
1494 return 0;
1495 if (bl->target_info.shadow_len == 0)
1496 /* BL isn't valid, or doesn't shadow memory. */
1497 return 0;
1498 return 1;
1499 }
1500
1501 /* Update BUF, which is LEN bytes read from the target address
1502 MEMADDR, by replacing a memory breakpoint with its shadowed
1503 contents.
1504
1505 If READBUF is not NULL, this buffer must not overlap with the of
1506 the breakpoint location's shadow_contents buffer. Otherwise, a
1507 failed assertion internal error will be raised. */
1508
1509 static void
1510 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1511 const gdb_byte *writebuf_org,
1512 ULONGEST memaddr, LONGEST len,
1513 struct bp_target_info *target_info,
1514 struct gdbarch *gdbarch)
1515 {
1516 /* Now do full processing of the found relevant range of elements. */
1517 CORE_ADDR bp_addr = 0;
1518 int bp_size = 0;
1519 int bptoffset = 0;
1520
1521 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1522 current_program_space->aspace, 0))
1523 {
1524 /* The breakpoint is inserted in a different address space. */
1525 return;
1526 }
1527
1528 /* Addresses and length of the part of the breakpoint that
1529 we need to copy. */
1530 bp_addr = target_info->placed_address;
1531 bp_size = target_info->shadow_len;
1532
1533 if (bp_addr + bp_size <= memaddr)
1534 {
1535 /* The breakpoint is entirely before the chunk of memory we are
1536 reading. */
1537 return;
1538 }
1539
1540 if (bp_addr >= memaddr + len)
1541 {
1542 /* The breakpoint is entirely after the chunk of memory we are
1543 reading. */
1544 return;
1545 }
1546
1547 /* Offset within shadow_contents. */
1548 if (bp_addr < memaddr)
1549 {
1550 /* Only copy the second part of the breakpoint. */
1551 bp_size -= memaddr - bp_addr;
1552 bptoffset = memaddr - bp_addr;
1553 bp_addr = memaddr;
1554 }
1555
1556 if (bp_addr + bp_size > memaddr + len)
1557 {
1558 /* Only copy the first part of the breakpoint. */
1559 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1560 }
1561
1562 if (readbuf != NULL)
1563 {
1564 /* Verify that the readbuf buffer does not overlap with the
1565 shadow_contents buffer. */
1566 gdb_assert (target_info->shadow_contents >= readbuf + len
1567 || readbuf >= (target_info->shadow_contents
1568 + target_info->shadow_len));
1569
1570 /* Update the read buffer with this inserted breakpoint's
1571 shadow. */
1572 memcpy (readbuf + bp_addr - memaddr,
1573 target_info->shadow_contents + bptoffset, bp_size);
1574 }
1575 else
1576 {
1577 const unsigned char *bp;
1578 CORE_ADDR addr = target_info->reqstd_address;
1579 int placed_size;
1580
1581 /* Update the shadow with what we want to write to memory. */
1582 memcpy (target_info->shadow_contents + bptoffset,
1583 writebuf_org + bp_addr - memaddr, bp_size);
1584
1585 /* Determine appropriate breakpoint contents and size for this
1586 address. */
1587 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1588
1589 /* Update the final write buffer with this inserted
1590 breakpoint's INSN. */
1591 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1592 }
1593 }
1594
1595 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1596 by replacing any memory breakpoints with their shadowed contents.
1597
1598 If READBUF is not NULL, this buffer must not overlap with any of
1599 the breakpoint location's shadow_contents buffers. Otherwise,
1600 a failed assertion internal error will be raised.
1601
1602 The range of shadowed area by each bp_location is:
1603 bl->address - bp_locations_placed_address_before_address_max
1604 up to bl->address + bp_locations_shadow_len_after_address_max
1605 The range we were requested to resolve shadows for is:
1606 memaddr ... memaddr + len
1607 Thus the safe cutoff boundaries for performance optimization are
1608 memaddr + len <= (bl->address
1609 - bp_locations_placed_address_before_address_max)
1610 and:
1611 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1612
1613 void
1614 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1615 const gdb_byte *writebuf_org,
1616 ULONGEST memaddr, LONGEST len)
1617 {
1618 /* Left boundary, right boundary and median element of our binary
1619 search. */
1620 unsigned bc_l, bc_r, bc;
1621
1622 /* Find BC_L which is a leftmost element which may affect BUF
1623 content. It is safe to report lower value but a failure to
1624 report higher one. */
1625
1626 bc_l = 0;
1627 bc_r = bp_locations_count;
1628 while (bc_l + 1 < bc_r)
1629 {
1630 struct bp_location *bl;
1631
1632 bc = (bc_l + bc_r) / 2;
1633 bl = bp_locations[bc];
1634
1635 /* Check first BL->ADDRESS will not overflow due to the added
1636 constant. Then advance the left boundary only if we are sure
1637 the BC element can in no way affect the BUF content (MEMADDR
1638 to MEMADDR + LEN range).
1639
1640 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1641 offset so that we cannot miss a breakpoint with its shadow
1642 range tail still reaching MEMADDR. */
1643
1644 if ((bl->address + bp_locations_shadow_len_after_address_max
1645 >= bl->address)
1646 && (bl->address + bp_locations_shadow_len_after_address_max
1647 <= memaddr))
1648 bc_l = bc;
1649 else
1650 bc_r = bc;
1651 }
1652
1653 /* Due to the binary search above, we need to make sure we pick the
1654 first location that's at BC_L's address. E.g., if there are
1655 multiple locations at the same address, BC_L may end up pointing
1656 at a duplicate location, and miss the "master"/"inserted"
1657 location. Say, given locations L1, L2 and L3 at addresses A and
1658 B:
1659
1660 L1@A, L2@A, L3@B, ...
1661
1662 BC_L could end up pointing at location L2, while the "master"
1663 location could be L1. Since the `loc->inserted' flag is only set
1664 on "master" locations, we'd forget to restore the shadow of L1
1665 and L2. */
1666 while (bc_l > 0
1667 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1668 bc_l--;
1669
1670 /* Now do full processing of the found relevant range of elements. */
1671
1672 for (bc = bc_l; bc < bp_locations_count; bc++)
1673 {
1674 struct bp_location *bl = bp_locations[bc];
1675
1676 /* bp_location array has BL->OWNER always non-NULL. */
1677 if (bl->owner->type == bp_none)
1678 warning (_("reading through apparently deleted breakpoint #%d?"),
1679 bl->owner->number);
1680
1681 /* Performance optimization: any further element can no longer affect BUF
1682 content. */
1683
1684 if (bl->address >= bp_locations_placed_address_before_address_max
1685 && memaddr + len <= (bl->address
1686 - bp_locations_placed_address_before_address_max))
1687 break;
1688
1689 if (!bp_location_has_shadow (bl))
1690 continue;
1691
1692 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1693 memaddr, len, &bl->target_info, bl->gdbarch);
1694 }
1695 }
1696
1697 \f
1698
1699 /* Return true if BPT is either a software breakpoint or a hardware
1700 breakpoint. */
1701
1702 int
1703 is_breakpoint (const struct breakpoint *bpt)
1704 {
1705 return (bpt->type == bp_breakpoint
1706 || bpt->type == bp_hardware_breakpoint
1707 || bpt->type == bp_dprintf);
1708 }
1709
1710 /* Return true if BPT is of any hardware watchpoint kind. */
1711
1712 static int
1713 is_hardware_watchpoint (const struct breakpoint *bpt)
1714 {
1715 return (bpt->type == bp_hardware_watchpoint
1716 || bpt->type == bp_read_watchpoint
1717 || bpt->type == bp_access_watchpoint);
1718 }
1719
1720 /* Return true if BPT is of any watchpoint kind, hardware or
1721 software. */
1722
1723 int
1724 is_watchpoint (const struct breakpoint *bpt)
1725 {
1726 return (is_hardware_watchpoint (bpt)
1727 || bpt->type == bp_watchpoint);
1728 }
1729
1730 /* Returns true if the current thread and its running state are safe
1731 to evaluate or update watchpoint B. Watchpoints on local
1732 expressions need to be evaluated in the context of the thread that
1733 was current when the watchpoint was created, and, that thread needs
1734 to be stopped to be able to select the correct frame context.
1735 Watchpoints on global expressions can be evaluated on any thread,
1736 and in any state. It is presently left to the target allowing
1737 memory accesses when threads are running. */
1738
1739 static int
1740 watchpoint_in_thread_scope (struct watchpoint *b)
1741 {
1742 return (b->pspace == current_program_space
1743 && (ptid_equal (b->watchpoint_thread, null_ptid)
1744 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1745 && !is_executing (inferior_ptid))));
1746 }
1747
1748 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1749 associated bp_watchpoint_scope breakpoint. */
1750
1751 static void
1752 watchpoint_del_at_next_stop (struct watchpoint *w)
1753 {
1754 if (w->related_breakpoint != w)
1755 {
1756 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1757 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1758 w->related_breakpoint->disposition = disp_del_at_next_stop;
1759 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1760 w->related_breakpoint = w;
1761 }
1762 w->disposition = disp_del_at_next_stop;
1763 }
1764
1765 /* Extract a bitfield value from value VAL using the bit parameters contained in
1766 watchpoint W. */
1767
1768 static struct value *
1769 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1770 {
1771 struct value *bit_val;
1772
1773 if (val == NULL)
1774 return NULL;
1775
1776 bit_val = allocate_value (value_type (val));
1777
1778 unpack_value_bitfield (bit_val,
1779 w->val_bitpos,
1780 w->val_bitsize,
1781 value_contents_for_printing (val),
1782 value_offset (val),
1783 val);
1784
1785 return bit_val;
1786 }
1787
1788 /* Allocate a dummy location and add it to B, which must be a software
1789 watchpoint. This is required because even if a software watchpoint
1790 is not watching any memory, bpstat_stop_status requires a location
1791 to be able to report stops. */
1792
1793 static void
1794 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1795 struct program_space *pspace)
1796 {
1797 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1798
1799 b->loc = allocate_bp_location (b);
1800 b->loc->pspace = pspace;
1801 b->loc->address = -1;
1802 b->loc->length = -1;
1803 }
1804
1805 /* Returns true if B is a software watchpoint that is not watching any
1806 memory (e.g., "watch $pc"). */
1807
1808 static int
1809 is_no_memory_software_watchpoint (struct breakpoint *b)
1810 {
1811 return (b->type == bp_watchpoint
1812 && b->loc != NULL
1813 && b->loc->next == NULL
1814 && b->loc->address == -1
1815 && b->loc->length == -1);
1816 }
1817
1818 /* Assuming that B is a watchpoint:
1819 - Reparse watchpoint expression, if REPARSE is non-zero
1820 - Evaluate expression and store the result in B->val
1821 - Evaluate the condition if there is one, and store the result
1822 in b->loc->cond.
1823 - Update the list of values that must be watched in B->loc.
1824
1825 If the watchpoint disposition is disp_del_at_next_stop, then do
1826 nothing. If this is local watchpoint that is out of scope, delete
1827 it.
1828
1829 Even with `set breakpoint always-inserted on' the watchpoints are
1830 removed + inserted on each stop here. Normal breakpoints must
1831 never be removed because they might be missed by a running thread
1832 when debugging in non-stop mode. On the other hand, hardware
1833 watchpoints (is_hardware_watchpoint; processed here) are specific
1834 to each LWP since they are stored in each LWP's hardware debug
1835 registers. Therefore, such LWP must be stopped first in order to
1836 be able to modify its hardware watchpoints.
1837
1838 Hardware watchpoints must be reset exactly once after being
1839 presented to the user. It cannot be done sooner, because it would
1840 reset the data used to present the watchpoint hit to the user. And
1841 it must not be done later because it could display the same single
1842 watchpoint hit during multiple GDB stops. Note that the latter is
1843 relevant only to the hardware watchpoint types bp_read_watchpoint
1844 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1845 not user-visible - its hit is suppressed if the memory content has
1846 not changed.
1847
1848 The following constraints influence the location where we can reset
1849 hardware watchpoints:
1850
1851 * target_stopped_by_watchpoint and target_stopped_data_address are
1852 called several times when GDB stops.
1853
1854 [linux]
1855 * Multiple hardware watchpoints can be hit at the same time,
1856 causing GDB to stop. GDB only presents one hardware watchpoint
1857 hit at a time as the reason for stopping, and all the other hits
1858 are presented later, one after the other, each time the user
1859 requests the execution to be resumed. Execution is not resumed
1860 for the threads still having pending hit event stored in
1861 LWP_INFO->STATUS. While the watchpoint is already removed from
1862 the inferior on the first stop the thread hit event is kept being
1863 reported from its cached value by linux_nat_stopped_data_address
1864 until the real thread resume happens after the watchpoint gets
1865 presented and thus its LWP_INFO->STATUS gets reset.
1866
1867 Therefore the hardware watchpoint hit can get safely reset on the
1868 watchpoint removal from inferior. */
1869
1870 static void
1871 update_watchpoint (struct watchpoint *b, int reparse)
1872 {
1873 int within_current_scope;
1874 struct frame_id saved_frame_id;
1875 int frame_saved;
1876
1877 /* If this is a local watchpoint, we only want to check if the
1878 watchpoint frame is in scope if the current thread is the thread
1879 that was used to create the watchpoint. */
1880 if (!watchpoint_in_thread_scope (b))
1881 return;
1882
1883 if (b->disposition == disp_del_at_next_stop)
1884 return;
1885
1886 frame_saved = 0;
1887
1888 /* Determine if the watchpoint is within scope. */
1889 if (b->exp_valid_block == NULL)
1890 within_current_scope = 1;
1891 else
1892 {
1893 struct frame_info *fi = get_current_frame ();
1894 struct gdbarch *frame_arch = get_frame_arch (fi);
1895 CORE_ADDR frame_pc = get_frame_pc (fi);
1896
1897 /* If we're at a point where the stack has been destroyed
1898 (e.g. in a function epilogue), unwinding may not work
1899 properly. Do not attempt to recreate locations at this
1900 point. See similar comments in watchpoint_check. */
1901 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1902 return;
1903
1904 /* Save the current frame's ID so we can restore it after
1905 evaluating the watchpoint expression on its own frame. */
1906 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1907 took a frame parameter, so that we didn't have to change the
1908 selected frame. */
1909 frame_saved = 1;
1910 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1911
1912 fi = frame_find_by_id (b->watchpoint_frame);
1913 within_current_scope = (fi != NULL);
1914 if (within_current_scope)
1915 select_frame (fi);
1916 }
1917
1918 /* We don't free locations. They are stored in the bp_location array
1919 and update_global_location_list will eventually delete them and
1920 remove breakpoints if needed. */
1921 b->loc = NULL;
1922
1923 if (within_current_scope && reparse)
1924 {
1925 const char *s;
1926
1927 b->exp.reset ();
1928 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1929 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1930 /* If the meaning of expression itself changed, the old value is
1931 no longer relevant. We don't want to report a watchpoint hit
1932 to the user when the old value and the new value may actually
1933 be completely different objects. */
1934 value_free (b->val);
1935 b->val = NULL;
1936 b->val_valid = 0;
1937
1938 /* Note that unlike with breakpoints, the watchpoint's condition
1939 expression is stored in the breakpoint object, not in the
1940 locations (re)created below. */
1941 if (b->cond_string != NULL)
1942 {
1943 b->cond_exp.reset ();
1944
1945 s = b->cond_string;
1946 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1947 }
1948 }
1949
1950 /* If we failed to parse the expression, for example because
1951 it refers to a global variable in a not-yet-loaded shared library,
1952 don't try to insert watchpoint. We don't automatically delete
1953 such watchpoint, though, since failure to parse expression
1954 is different from out-of-scope watchpoint. */
1955 if (!target_has_execution)
1956 {
1957 /* Without execution, memory can't change. No use to try and
1958 set watchpoint locations. The watchpoint will be reset when
1959 the target gains execution, through breakpoint_re_set. */
1960 if (!can_use_hw_watchpoints)
1961 {
1962 if (b->ops->works_in_software_mode (b))
1963 b->type = bp_watchpoint;
1964 else
1965 error (_("Can't set read/access watchpoint when "
1966 "hardware watchpoints are disabled."));
1967 }
1968 }
1969 else if (within_current_scope && b->exp)
1970 {
1971 int pc = 0;
1972 struct value *val_chain, *v, *result, *next;
1973 struct program_space *frame_pspace;
1974
1975 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1976
1977 /* Avoid setting b->val if it's already set. The meaning of
1978 b->val is 'the last value' user saw, and we should update
1979 it only if we reported that last value to user. As it
1980 happens, the code that reports it updates b->val directly.
1981 We don't keep track of the memory value for masked
1982 watchpoints. */
1983 if (!b->val_valid && !is_masked_watchpoint (b))
1984 {
1985 if (b->val_bitsize != 0)
1986 {
1987 v = extract_bitfield_from_watchpoint_value (b, v);
1988 if (v != NULL)
1989 release_value (v);
1990 }
1991 b->val = v;
1992 b->val_valid = 1;
1993 }
1994
1995 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1996
1997 /* Look at each value on the value chain. */
1998 for (v = val_chain; v; v = value_next (v))
1999 {
2000 /* If it's a memory location, and GDB actually needed
2001 its contents to evaluate the expression, then we
2002 must watch it. If the first value returned is
2003 still lazy, that means an error occurred reading it;
2004 watch it anyway in case it becomes readable. */
2005 if (VALUE_LVAL (v) == lval_memory
2006 && (v == val_chain || ! value_lazy (v)))
2007 {
2008 struct type *vtype = check_typedef (value_type (v));
2009
2010 /* We only watch structs and arrays if user asked
2011 for it explicitly, never if they just happen to
2012 appear in the middle of some value chain. */
2013 if (v == result
2014 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2015 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2016 {
2017 CORE_ADDR addr;
2018 enum target_hw_bp_type type;
2019 struct bp_location *loc, **tmp;
2020 int bitpos = 0, bitsize = 0;
2021
2022 if (value_bitsize (v) != 0)
2023 {
2024 /* Extract the bit parameters out from the bitfield
2025 sub-expression. */
2026 bitpos = value_bitpos (v);
2027 bitsize = value_bitsize (v);
2028 }
2029 else if (v == result && b->val_bitsize != 0)
2030 {
2031 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2032 lvalue whose bit parameters are saved in the fields
2033 VAL_BITPOS and VAL_BITSIZE. */
2034 bitpos = b->val_bitpos;
2035 bitsize = b->val_bitsize;
2036 }
2037
2038 addr = value_address (v);
2039 if (bitsize != 0)
2040 {
2041 /* Skip the bytes that don't contain the bitfield. */
2042 addr += bitpos / 8;
2043 }
2044
2045 type = hw_write;
2046 if (b->type == bp_read_watchpoint)
2047 type = hw_read;
2048 else if (b->type == bp_access_watchpoint)
2049 type = hw_access;
2050
2051 loc = allocate_bp_location (b);
2052 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2053 ;
2054 *tmp = loc;
2055 loc->gdbarch = get_type_arch (value_type (v));
2056
2057 loc->pspace = frame_pspace;
2058 loc->address = addr;
2059
2060 if (bitsize != 0)
2061 {
2062 /* Just cover the bytes that make up the bitfield. */
2063 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2064 }
2065 else
2066 loc->length = TYPE_LENGTH (value_type (v));
2067
2068 loc->watchpoint_type = type;
2069 }
2070 }
2071 }
2072
2073 /* Change the type of breakpoint between hardware assisted or
2074 an ordinary watchpoint depending on the hardware support
2075 and free hardware slots. REPARSE is set when the inferior
2076 is started. */
2077 if (reparse)
2078 {
2079 int reg_cnt;
2080 enum bp_loc_type loc_type;
2081 struct bp_location *bl;
2082
2083 reg_cnt = can_use_hardware_watchpoint (val_chain);
2084
2085 if (reg_cnt)
2086 {
2087 int i, target_resources_ok, other_type_used;
2088 enum bptype type;
2089
2090 /* Use an exact watchpoint when there's only one memory region to be
2091 watched, and only one debug register is needed to watch it. */
2092 b->exact = target_exact_watchpoints && reg_cnt == 1;
2093
2094 /* We need to determine how many resources are already
2095 used for all other hardware watchpoints plus this one
2096 to see if we still have enough resources to also fit
2097 this watchpoint in as well. */
2098
2099 /* If this is a software watchpoint, we try to turn it
2100 to a hardware one -- count resources as if B was of
2101 hardware watchpoint type. */
2102 type = b->type;
2103 if (type == bp_watchpoint)
2104 type = bp_hardware_watchpoint;
2105
2106 /* This watchpoint may or may not have been placed on
2107 the list yet at this point (it won't be in the list
2108 if we're trying to create it for the first time,
2109 through watch_command), so always account for it
2110 manually. */
2111
2112 /* Count resources used by all watchpoints except B. */
2113 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2114
2115 /* Add in the resources needed for B. */
2116 i += hw_watchpoint_use_count (b);
2117
2118 target_resources_ok
2119 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2120 if (target_resources_ok <= 0)
2121 {
2122 int sw_mode = b->ops->works_in_software_mode (b);
2123
2124 if (target_resources_ok == 0 && !sw_mode)
2125 error (_("Target does not support this type of "
2126 "hardware watchpoint."));
2127 else if (target_resources_ok < 0 && !sw_mode)
2128 error (_("There are not enough available hardware "
2129 "resources for this watchpoint."));
2130
2131 /* Downgrade to software watchpoint. */
2132 b->type = bp_watchpoint;
2133 }
2134 else
2135 {
2136 /* If this was a software watchpoint, we've just
2137 found we have enough resources to turn it to a
2138 hardware watchpoint. Otherwise, this is a
2139 nop. */
2140 b->type = type;
2141 }
2142 }
2143 else if (!b->ops->works_in_software_mode (b))
2144 {
2145 if (!can_use_hw_watchpoints)
2146 error (_("Can't set read/access watchpoint when "
2147 "hardware watchpoints are disabled."));
2148 else
2149 error (_("Expression cannot be implemented with "
2150 "read/access watchpoint."));
2151 }
2152 else
2153 b->type = bp_watchpoint;
2154
2155 loc_type = (b->type == bp_watchpoint? bp_loc_other
2156 : bp_loc_hardware_watchpoint);
2157 for (bl = b->loc; bl; bl = bl->next)
2158 bl->loc_type = loc_type;
2159 }
2160
2161 for (v = val_chain; v; v = next)
2162 {
2163 next = value_next (v);
2164 if (v != b->val)
2165 value_free (v);
2166 }
2167
2168 /* If a software watchpoint is not watching any memory, then the
2169 above left it without any location set up. But,
2170 bpstat_stop_status requires a location to be able to report
2171 stops, so make sure there's at least a dummy one. */
2172 if (b->type == bp_watchpoint && b->loc == NULL)
2173 software_watchpoint_add_no_memory_location (b, frame_pspace);
2174 }
2175 else if (!within_current_scope)
2176 {
2177 printf_filtered (_("\
2178 Watchpoint %d deleted because the program has left the block\n\
2179 in which its expression is valid.\n"),
2180 b->number);
2181 watchpoint_del_at_next_stop (b);
2182 }
2183
2184 /* Restore the selected frame. */
2185 if (frame_saved)
2186 select_frame (frame_find_by_id (saved_frame_id));
2187 }
2188
2189
2190 /* Returns 1 iff breakpoint location should be
2191 inserted in the inferior. We don't differentiate the type of BL's owner
2192 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2193 breakpoint_ops is not defined, because in insert_bp_location,
2194 tracepoint's insert_location will not be called. */
2195 static int
2196 should_be_inserted (struct bp_location *bl)
2197 {
2198 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2199 return 0;
2200
2201 if (bl->owner->disposition == disp_del_at_next_stop)
2202 return 0;
2203
2204 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2205 return 0;
2206
2207 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2208 return 0;
2209
2210 /* This is set for example, when we're attached to the parent of a
2211 vfork, and have detached from the child. The child is running
2212 free, and we expect it to do an exec or exit, at which point the
2213 OS makes the parent schedulable again (and the target reports
2214 that the vfork is done). Until the child is done with the shared
2215 memory region, do not insert breakpoints in the parent, otherwise
2216 the child could still trip on the parent's breakpoints. Since
2217 the parent is blocked anyway, it won't miss any breakpoint. */
2218 if (bl->pspace->breakpoints_not_allowed)
2219 return 0;
2220
2221 /* Don't insert a breakpoint if we're trying to step past its
2222 location, except if the breakpoint is a single-step breakpoint,
2223 and the breakpoint's thread is the thread which is stepping past
2224 a breakpoint. */
2225 if ((bl->loc_type == bp_loc_software_breakpoint
2226 || bl->loc_type == bp_loc_hardware_breakpoint)
2227 && stepping_past_instruction_at (bl->pspace->aspace,
2228 bl->address)
2229 /* The single-step breakpoint may be inserted at the location
2230 we're trying to step if the instruction branches to itself.
2231 However, the instruction won't be executed at all and it may
2232 break the semantics of the instruction, for example, the
2233 instruction is a conditional branch or updates some flags.
2234 We can't fix it unless GDB is able to emulate the instruction
2235 or switch to displaced stepping. */
2236 && !(bl->owner->type == bp_single_step
2237 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2238 {
2239 if (debug_infrun)
2240 {
2241 fprintf_unfiltered (gdb_stdlog,
2242 "infrun: skipping breakpoint: "
2243 "stepping past insn at: %s\n",
2244 paddress (bl->gdbarch, bl->address));
2245 }
2246 return 0;
2247 }
2248
2249 /* Don't insert watchpoints if we're trying to step past the
2250 instruction that triggered one. */
2251 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2252 && stepping_past_nonsteppable_watchpoint ())
2253 {
2254 if (debug_infrun)
2255 {
2256 fprintf_unfiltered (gdb_stdlog,
2257 "infrun: stepping past non-steppable watchpoint. "
2258 "skipping watchpoint at %s:%d\n",
2259 paddress (bl->gdbarch, bl->address),
2260 bl->length);
2261 }
2262 return 0;
2263 }
2264
2265 return 1;
2266 }
2267
2268 /* Same as should_be_inserted but does the check assuming
2269 that the location is not duplicated. */
2270
2271 static int
2272 unduplicated_should_be_inserted (struct bp_location *bl)
2273 {
2274 int result;
2275 const int save_duplicate = bl->duplicate;
2276
2277 bl->duplicate = 0;
2278 result = should_be_inserted (bl);
2279 bl->duplicate = save_duplicate;
2280 return result;
2281 }
2282
2283 /* Parses a conditional described by an expression COND into an
2284 agent expression bytecode suitable for evaluation
2285 by the bytecode interpreter. Return NULL if there was
2286 any error during parsing. */
2287
2288 static agent_expr_up
2289 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2290 {
2291 if (cond == NULL)
2292 return NULL;
2293
2294 agent_expr_up aexpr;
2295
2296 /* We don't want to stop processing, so catch any errors
2297 that may show up. */
2298 TRY
2299 {
2300 aexpr = gen_eval_for_expr (scope, cond);
2301 }
2302
2303 CATCH (ex, RETURN_MASK_ERROR)
2304 {
2305 /* If we got here, it means the condition could not be parsed to a valid
2306 bytecode expression and thus can't be evaluated on the target's side.
2307 It's no use iterating through the conditions. */
2308 }
2309 END_CATCH
2310
2311 /* We have a valid agent expression. */
2312 return aexpr;
2313 }
2314
2315 /* Based on location BL, create a list of breakpoint conditions to be
2316 passed on to the target. If we have duplicated locations with different
2317 conditions, we will add such conditions to the list. The idea is that the
2318 target will evaluate the list of conditions and will only notify GDB when
2319 one of them is true. */
2320
2321 static void
2322 build_target_condition_list (struct bp_location *bl)
2323 {
2324 struct bp_location **locp = NULL, **loc2p;
2325 int null_condition_or_parse_error = 0;
2326 int modified = bl->needs_update;
2327 struct bp_location *loc;
2328
2329 /* Release conditions left over from a previous insert. */
2330 bl->target_info.conditions.clear ();
2331
2332 /* This is only meaningful if the target is
2333 evaluating conditions and if the user has
2334 opted for condition evaluation on the target's
2335 side. */
2336 if (gdb_evaluates_breakpoint_condition_p ()
2337 || !target_supports_evaluation_of_breakpoint_conditions ())
2338 return;
2339
2340 /* Do a first pass to check for locations with no assigned
2341 conditions or conditions that fail to parse to a valid agent expression
2342 bytecode. If any of these happen, then it's no use to send conditions
2343 to the target since this location will always trigger and generate a
2344 response back to GDB. */
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 if (modified)
2351 {
2352 /* Re-parse the conditions since something changed. In that
2353 case we already freed the condition bytecodes (see
2354 force_breakpoint_reinsertion). We just
2355 need to parse the condition to bytecodes again. */
2356 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2357 loc->cond.get ());
2358 }
2359
2360 /* If we have a NULL bytecode expression, it means something
2361 went wrong or we have a null condition expression. */
2362 if (!loc->cond_bytecode)
2363 {
2364 null_condition_or_parse_error = 1;
2365 break;
2366 }
2367 }
2368 }
2369
2370 /* If any of these happened, it means we will have to evaluate the conditions
2371 for the location's address on gdb's side. It is no use keeping bytecodes
2372 for all the other duplicate locations, thus we free all of them here.
2373
2374 This is so we have a finer control over which locations' conditions are
2375 being evaluated by GDB or the remote stub. */
2376 if (null_condition_or_parse_error)
2377 {
2378 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2379 {
2380 loc = (*loc2p);
2381 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2382 {
2383 /* Only go as far as the first NULL bytecode is
2384 located. */
2385 if (!loc->cond_bytecode)
2386 return;
2387
2388 loc->cond_bytecode.reset ();
2389 }
2390 }
2391 }
2392
2393 /* No NULL conditions or failed bytecode generation. Build a condition list
2394 for this location's address. */
2395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2396 {
2397 loc = (*loc2p);
2398 if (loc->cond
2399 && is_breakpoint (loc->owner)
2400 && loc->pspace->num == bl->pspace->num
2401 && loc->owner->enable_state == bp_enabled
2402 && loc->enabled)
2403 {
2404 /* Add the condition to the vector. This will be used later
2405 to send the conditions to the target. */
2406 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2407 }
2408 }
2409
2410 return;
2411 }
2412
2413 /* Parses a command described by string CMD into an agent expression
2414 bytecode suitable for evaluation by the bytecode interpreter.
2415 Return NULL if there was any error during parsing. */
2416
2417 static agent_expr_up
2418 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2419 {
2420 struct cleanup *old_cleanups = 0;
2421 struct expression **argvec;
2422 const char *cmdrest;
2423 const char *format_start, *format_end;
2424 struct format_piece *fpieces;
2425 int nargs;
2426 struct gdbarch *gdbarch = get_current_arch ();
2427
2428 if (cmd == NULL)
2429 return NULL;
2430
2431 cmdrest = cmd;
2432
2433 if (*cmdrest == ',')
2434 ++cmdrest;
2435 cmdrest = skip_spaces_const (cmdrest);
2436
2437 if (*cmdrest++ != '"')
2438 error (_("No format string following the location"));
2439
2440 format_start = cmdrest;
2441
2442 fpieces = parse_format_string (&cmdrest);
2443
2444 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2445
2446 format_end = cmdrest;
2447
2448 if (*cmdrest++ != '"')
2449 error (_("Bad format string, non-terminated '\"'."));
2450
2451 cmdrest = skip_spaces_const (cmdrest);
2452
2453 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2454 error (_("Invalid argument syntax"));
2455
2456 if (*cmdrest == ',')
2457 cmdrest++;
2458 cmdrest = skip_spaces_const (cmdrest);
2459
2460 /* For each argument, make an expression. */
2461
2462 argvec = (struct expression **) alloca (strlen (cmd)
2463 * sizeof (struct expression *));
2464
2465 nargs = 0;
2466 while (*cmdrest != '\0')
2467 {
2468 const char *cmd1;
2469
2470 cmd1 = cmdrest;
2471 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2472 argvec[nargs++] = expr.release ();
2473 cmdrest = cmd1;
2474 if (*cmdrest == ',')
2475 ++cmdrest;
2476 }
2477
2478 agent_expr_up aexpr;
2479
2480 /* We don't want to stop processing, so catch any errors
2481 that may show up. */
2482 TRY
2483 {
2484 aexpr = gen_printf (scope, gdbarch, 0, 0,
2485 format_start, format_end - format_start,
2486 fpieces, nargs, argvec);
2487 }
2488 CATCH (ex, RETURN_MASK_ERROR)
2489 {
2490 /* If we got here, it means the command could not be parsed to a valid
2491 bytecode expression and thus can't be evaluated on the target's side.
2492 It's no use iterating through the other commands. */
2493 }
2494 END_CATCH
2495
2496 do_cleanups (old_cleanups);
2497
2498 /* We have a valid agent expression, return it. */
2499 return aexpr;
2500 }
2501
2502 /* Based on location BL, create a list of breakpoint commands to be
2503 passed on to the target. If we have duplicated locations with
2504 different commands, we will add any such to the list. */
2505
2506 static void
2507 build_target_command_list (struct bp_location *bl)
2508 {
2509 struct bp_location **locp = NULL, **loc2p;
2510 int null_command_or_parse_error = 0;
2511 int modified = bl->needs_update;
2512 struct bp_location *loc;
2513
2514 /* Clear commands left over from a previous insert. */
2515 bl->target_info.tcommands.clear ();
2516
2517 if (!target_can_run_breakpoint_commands ())
2518 return;
2519
2520 /* For now, limit to agent-style dprintf breakpoints. */
2521 if (dprintf_style != dprintf_style_agent)
2522 return;
2523
2524 /* For now, if we have any duplicate location that isn't a dprintf,
2525 don't install the target-side commands, as that would make the
2526 breakpoint not be reported to the core, and we'd lose
2527 control. */
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner)
2532 && loc->pspace->num == bl->pspace->num
2533 && loc->owner->type != bp_dprintf)
2534 return;
2535 }
2536
2537 /* Do a first pass to check for locations with no assigned
2538 conditions or conditions that fail to parse to a valid agent expression
2539 bytecode. If any of these happen, then it's no use to send conditions
2540 to the target since this location will always trigger and generate a
2541 response back to GDB. */
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2546 {
2547 if (modified)
2548 {
2549 /* Re-parse the commands since something changed. In that
2550 case we already freed the command bytecodes (see
2551 force_breakpoint_reinsertion). We just
2552 need to parse the command to bytecodes again. */
2553 loc->cmd_bytecode
2554 = parse_cmd_to_aexpr (bl->address,
2555 loc->owner->extra_string);
2556 }
2557
2558 /* If we have a NULL bytecode expression, it means something
2559 went wrong or we have a null command expression. */
2560 if (!loc->cmd_bytecode)
2561 {
2562 null_command_or_parse_error = 1;
2563 break;
2564 }
2565 }
2566 }
2567
2568 /* If anything failed, then we're not doing target-side commands,
2569 and so clean up. */
2570 if (null_command_or_parse_error)
2571 {
2572 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2573 {
2574 loc = (*loc2p);
2575 if (is_breakpoint (loc->owner)
2576 && loc->pspace->num == bl->pspace->num)
2577 {
2578 /* Only go as far as the first NULL bytecode is
2579 located. */
2580 if (loc->cmd_bytecode == NULL)
2581 return;
2582
2583 loc->cmd_bytecode.reset ();
2584 }
2585 }
2586 }
2587
2588 /* No NULL commands or failed bytecode generation. Build a command list
2589 for this location's address. */
2590 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2591 {
2592 loc = (*loc2p);
2593 if (loc->owner->extra_string
2594 && is_breakpoint (loc->owner)
2595 && loc->pspace->num == bl->pspace->num
2596 && loc->owner->enable_state == bp_enabled
2597 && loc->enabled)
2598 {
2599 /* Add the command to the vector. This will be used later
2600 to send the commands to the target. */
2601 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2602 }
2603 }
2604
2605 bl->target_info.persist = 0;
2606 /* Maybe flag this location as persistent. */
2607 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2608 bl->target_info.persist = 1;
2609 }
2610
2611 /* Return the kind of breakpoint on address *ADDR. Get the kind
2612 of breakpoint according to ADDR except single-step breakpoint.
2613 Get the kind of single-step breakpoint according to the current
2614 registers state. */
2615
2616 static int
2617 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2618 {
2619 if (bl->owner->type == bp_single_step)
2620 {
2621 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2622 struct regcache *regcache;
2623
2624 regcache = get_thread_regcache (thr->ptid);
2625
2626 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2627 regcache, addr);
2628 }
2629 else
2630 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2631 }
2632
2633 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2634 location. Any error messages are printed to TMP_ERROR_STREAM; and
2635 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2636 Returns 0 for success, 1 if the bp_location type is not supported or
2637 -1 for failure.
2638
2639 NOTE drow/2003-09-09: This routine could be broken down to an
2640 object-style method for each breakpoint or catchpoint type. */
2641 static int
2642 insert_bp_location (struct bp_location *bl,
2643 struct ui_file *tmp_error_stream,
2644 int *disabled_breaks,
2645 int *hw_breakpoint_error,
2646 int *hw_bp_error_explained_already)
2647 {
2648 enum errors bp_err = GDB_NO_ERROR;
2649 const char *bp_err_message = NULL;
2650
2651 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2652 return 0;
2653
2654 /* Note we don't initialize bl->target_info, as that wipes out
2655 the breakpoint location's shadow_contents if the breakpoint
2656 is still inserted at that location. This in turn breaks
2657 target_read_memory which depends on these buffers when
2658 a memory read is requested at the breakpoint location:
2659 Once the target_info has been wiped, we fail to see that
2660 we have a breakpoint inserted at that address and thus
2661 read the breakpoint instead of returning the data saved in
2662 the breakpoint location's shadow contents. */
2663 bl->target_info.reqstd_address = bl->address;
2664 bl->target_info.placed_address_space = bl->pspace->aspace;
2665 bl->target_info.length = bl->length;
2666
2667 /* When working with target-side conditions, we must pass all the conditions
2668 for the same breakpoint address down to the target since GDB will not
2669 insert those locations. With a list of breakpoint conditions, the target
2670 can decide when to stop and notify GDB. */
2671
2672 if (is_breakpoint (bl->owner))
2673 {
2674 build_target_condition_list (bl);
2675 build_target_command_list (bl);
2676 /* Reset the modification marker. */
2677 bl->needs_update = 0;
2678 }
2679
2680 if (bl->loc_type == bp_loc_software_breakpoint
2681 || bl->loc_type == bp_loc_hardware_breakpoint)
2682 {
2683 if (bl->owner->type != bp_hardware_breakpoint)
2684 {
2685 /* If the explicitly specified breakpoint type
2686 is not hardware breakpoint, check the memory map to see
2687 if the breakpoint address is in read only memory or not.
2688
2689 Two important cases are:
2690 - location type is not hardware breakpoint, memory
2691 is readonly. We change the type of the location to
2692 hardware breakpoint.
2693 - location type is hardware breakpoint, memory is
2694 read-write. This means we've previously made the
2695 location hardware one, but then the memory map changed,
2696 so we undo.
2697
2698 When breakpoints are removed, remove_breakpoints will use
2699 location types we've just set here, the only possible
2700 problem is that memory map has changed during running
2701 program, but it's not going to work anyway with current
2702 gdb. */
2703 struct mem_region *mr
2704 = lookup_mem_region (bl->target_info.reqstd_address);
2705
2706 if (mr)
2707 {
2708 if (automatic_hardware_breakpoints)
2709 {
2710 enum bp_loc_type new_type;
2711
2712 if (mr->attrib.mode != MEM_RW)
2713 new_type = bp_loc_hardware_breakpoint;
2714 else
2715 new_type = bp_loc_software_breakpoint;
2716
2717 if (new_type != bl->loc_type)
2718 {
2719 static int said = 0;
2720
2721 bl->loc_type = new_type;
2722 if (!said)
2723 {
2724 fprintf_filtered (gdb_stdout,
2725 _("Note: automatically using "
2726 "hardware breakpoints for "
2727 "read-only addresses.\n"));
2728 said = 1;
2729 }
2730 }
2731 }
2732 else if (bl->loc_type == bp_loc_software_breakpoint
2733 && mr->attrib.mode != MEM_RW)
2734 {
2735 fprintf_unfiltered (tmp_error_stream,
2736 _("Cannot insert breakpoint %d.\n"
2737 "Cannot set software breakpoint "
2738 "at read-only address %s\n"),
2739 bl->owner->number,
2740 paddress (bl->gdbarch, bl->address));
2741 return 1;
2742 }
2743 }
2744 }
2745
2746 /* First check to see if we have to handle an overlay. */
2747 if (overlay_debugging == ovly_off
2748 || bl->section == NULL
2749 || !(section_is_overlay (bl->section)))
2750 {
2751 /* No overlay handling: just set the breakpoint. */
2752 TRY
2753 {
2754 int val;
2755
2756 val = bl->owner->ops->insert_location (bl);
2757 if (val)
2758 bp_err = GENERIC_ERROR;
2759 }
2760 CATCH (e, RETURN_MASK_ALL)
2761 {
2762 bp_err = e.error;
2763 bp_err_message = e.message;
2764 }
2765 END_CATCH
2766 }
2767 else
2768 {
2769 /* This breakpoint is in an overlay section.
2770 Shall we set a breakpoint at the LMA? */
2771 if (!overlay_events_enabled)
2772 {
2773 /* Yes -- overlay event support is not active,
2774 so we must try to set a breakpoint at the LMA.
2775 This will not work for a hardware breakpoint. */
2776 if (bl->loc_type == bp_loc_hardware_breakpoint)
2777 warning (_("hardware breakpoint %d not supported in overlay!"),
2778 bl->owner->number);
2779 else
2780 {
2781 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2782 bl->section);
2783 /* Set a software (trap) breakpoint at the LMA. */
2784 bl->overlay_target_info = bl->target_info;
2785 bl->overlay_target_info.reqstd_address = addr;
2786
2787 /* No overlay handling: just set the breakpoint. */
2788 TRY
2789 {
2790 int val;
2791
2792 bl->overlay_target_info.kind
2793 = breakpoint_kind (bl, &addr);
2794 bl->overlay_target_info.placed_address = addr;
2795 val = target_insert_breakpoint (bl->gdbarch,
2796 &bl->overlay_target_info);
2797 if (val)
2798 bp_err = GENERIC_ERROR;
2799 }
2800 CATCH (e, RETURN_MASK_ALL)
2801 {
2802 bp_err = e.error;
2803 bp_err_message = e.message;
2804 }
2805 END_CATCH
2806
2807 if (bp_err != GDB_NO_ERROR)
2808 fprintf_unfiltered (tmp_error_stream,
2809 "Overlay breakpoint %d "
2810 "failed: in ROM?\n",
2811 bl->owner->number);
2812 }
2813 }
2814 /* Shall we set a breakpoint at the VMA? */
2815 if (section_is_mapped (bl->section))
2816 {
2817 /* Yes. This overlay section is mapped into memory. */
2818 TRY
2819 {
2820 int val;
2821
2822 val = bl->owner->ops->insert_location (bl);
2823 if (val)
2824 bp_err = GENERIC_ERROR;
2825 }
2826 CATCH (e, RETURN_MASK_ALL)
2827 {
2828 bp_err = e.error;
2829 bp_err_message = e.message;
2830 }
2831 END_CATCH
2832 }
2833 else
2834 {
2835 /* No. This breakpoint will not be inserted.
2836 No error, but do not mark the bp as 'inserted'. */
2837 return 0;
2838 }
2839 }
2840
2841 if (bp_err != GDB_NO_ERROR)
2842 {
2843 /* Can't set the breakpoint. */
2844
2845 /* In some cases, we might not be able to insert a
2846 breakpoint in a shared library that has already been
2847 removed, but we have not yet processed the shlib unload
2848 event. Unfortunately, some targets that implement
2849 breakpoint insertion themselves can't tell why the
2850 breakpoint insertion failed (e.g., the remote target
2851 doesn't define error codes), so we must treat generic
2852 errors as memory errors. */
2853 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2854 && bl->loc_type == bp_loc_software_breakpoint
2855 && (solib_name_from_address (bl->pspace, bl->address)
2856 || shared_objfile_contains_address_p (bl->pspace,
2857 bl->address)))
2858 {
2859 /* See also: disable_breakpoints_in_shlibs. */
2860 bl->shlib_disabled = 1;
2861 observer_notify_breakpoint_modified (bl->owner);
2862 if (!*disabled_breaks)
2863 {
2864 fprintf_unfiltered (tmp_error_stream,
2865 "Cannot insert breakpoint %d.\n",
2866 bl->owner->number);
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Temporarily disabling shared "
2869 "library breakpoints:\n");
2870 }
2871 *disabled_breaks = 1;
2872 fprintf_unfiltered (tmp_error_stream,
2873 "breakpoint #%d\n", bl->owner->number);
2874 return 0;
2875 }
2876 else
2877 {
2878 if (bl->loc_type == bp_loc_hardware_breakpoint)
2879 {
2880 *hw_breakpoint_error = 1;
2881 *hw_bp_error_explained_already = bp_err_message != NULL;
2882 fprintf_unfiltered (tmp_error_stream,
2883 "Cannot insert hardware breakpoint %d%s",
2884 bl->owner->number, bp_err_message ? ":" : ".\n");
2885 if (bp_err_message != NULL)
2886 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2887 }
2888 else
2889 {
2890 if (bp_err_message == NULL)
2891 {
2892 char *message
2893 = memory_error_message (TARGET_XFER_E_IO,
2894 bl->gdbarch, bl->address);
2895 struct cleanup *old_chain = make_cleanup (xfree, message);
2896
2897 fprintf_unfiltered (tmp_error_stream,
2898 "Cannot insert breakpoint %d.\n"
2899 "%s\n",
2900 bl->owner->number, message);
2901 do_cleanups (old_chain);
2902 }
2903 else
2904 {
2905 fprintf_unfiltered (tmp_error_stream,
2906 "Cannot insert breakpoint %d: %s\n",
2907 bl->owner->number,
2908 bp_err_message);
2909 }
2910 }
2911 return 1;
2912
2913 }
2914 }
2915 else
2916 bl->inserted = 1;
2917
2918 return 0;
2919 }
2920
2921 else if (bl->loc_type == bp_loc_hardware_watchpoint
2922 /* NOTE drow/2003-09-08: This state only exists for removing
2923 watchpoints. It's not clear that it's necessary... */
2924 && bl->owner->disposition != disp_del_at_next_stop)
2925 {
2926 int val;
2927
2928 gdb_assert (bl->owner->ops != NULL
2929 && bl->owner->ops->insert_location != NULL);
2930
2931 val = bl->owner->ops->insert_location (bl);
2932
2933 /* If trying to set a read-watchpoint, and it turns out it's not
2934 supported, try emulating one with an access watchpoint. */
2935 if (val == 1 && bl->watchpoint_type == hw_read)
2936 {
2937 struct bp_location *loc, **loc_temp;
2938
2939 /* But don't try to insert it, if there's already another
2940 hw_access location that would be considered a duplicate
2941 of this one. */
2942 ALL_BP_LOCATIONS (loc, loc_temp)
2943 if (loc != bl
2944 && loc->watchpoint_type == hw_access
2945 && watchpoint_locations_match (bl, loc))
2946 {
2947 bl->duplicate = 1;
2948 bl->inserted = 1;
2949 bl->target_info = loc->target_info;
2950 bl->watchpoint_type = hw_access;
2951 val = 0;
2952 break;
2953 }
2954
2955 if (val == 1)
2956 {
2957 bl->watchpoint_type = hw_access;
2958 val = bl->owner->ops->insert_location (bl);
2959
2960 if (val)
2961 /* Back to the original value. */
2962 bl->watchpoint_type = hw_read;
2963 }
2964 }
2965
2966 bl->inserted = (val == 0);
2967 }
2968
2969 else if (bl->owner->type == bp_catchpoint)
2970 {
2971 int val;
2972
2973 gdb_assert (bl->owner->ops != NULL
2974 && bl->owner->ops->insert_location != NULL);
2975
2976 val = bl->owner->ops->insert_location (bl);
2977 if (val)
2978 {
2979 bl->owner->enable_state = bp_disabled;
2980
2981 if (val == 1)
2982 warning (_("\
2983 Error inserting catchpoint %d: Your system does not support this type\n\
2984 of catchpoint."), bl->owner->number);
2985 else
2986 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2987 }
2988
2989 bl->inserted = (val == 0);
2990
2991 /* We've already printed an error message if there was a problem
2992 inserting this catchpoint, and we've disabled the catchpoint,
2993 so just return success. */
2994 return 0;
2995 }
2996
2997 return 0;
2998 }
2999
3000 /* This function is called when program space PSPACE is about to be
3001 deleted. It takes care of updating breakpoints to not reference
3002 PSPACE anymore. */
3003
3004 void
3005 breakpoint_program_space_exit (struct program_space *pspace)
3006 {
3007 struct breakpoint *b, *b_temp;
3008 struct bp_location *loc, **loc_temp;
3009
3010 /* Remove any breakpoint that was set through this program space. */
3011 ALL_BREAKPOINTS_SAFE (b, b_temp)
3012 {
3013 if (b->pspace == pspace)
3014 delete_breakpoint (b);
3015 }
3016
3017 /* Breakpoints set through other program spaces could have locations
3018 bound to PSPACE as well. Remove those. */
3019 ALL_BP_LOCATIONS (loc, loc_temp)
3020 {
3021 struct bp_location *tmp;
3022
3023 if (loc->pspace == pspace)
3024 {
3025 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3026 if (loc->owner->loc == loc)
3027 loc->owner->loc = loc->next;
3028 else
3029 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3030 if (tmp->next == loc)
3031 {
3032 tmp->next = loc->next;
3033 break;
3034 }
3035 }
3036 }
3037
3038 /* Now update the global location list to permanently delete the
3039 removed locations above. */
3040 update_global_location_list (UGLL_DONT_INSERT);
3041 }
3042
3043 /* Make sure all breakpoints are inserted in inferior.
3044 Throws exception on any error.
3045 A breakpoint that is already inserted won't be inserted
3046 again, so calling this function twice is safe. */
3047 void
3048 insert_breakpoints (void)
3049 {
3050 struct breakpoint *bpt;
3051
3052 ALL_BREAKPOINTS (bpt)
3053 if (is_hardware_watchpoint (bpt))
3054 {
3055 struct watchpoint *w = (struct watchpoint *) bpt;
3056
3057 update_watchpoint (w, 0 /* don't reparse. */);
3058 }
3059
3060 /* Updating watchpoints creates new locations, so update the global
3061 location list. Explicitly tell ugll to insert locations and
3062 ignore breakpoints_always_inserted_mode. */
3063 update_global_location_list (UGLL_INSERT);
3064 }
3065
3066 /* Invoke CALLBACK for each of bp_location. */
3067
3068 void
3069 iterate_over_bp_locations (walk_bp_location_callback callback)
3070 {
3071 struct bp_location *loc, **loc_tmp;
3072
3073 ALL_BP_LOCATIONS (loc, loc_tmp)
3074 {
3075 callback (loc, NULL);
3076 }
3077 }
3078
3079 /* This is used when we need to synch breakpoint conditions between GDB and the
3080 target. It is the case with deleting and disabling of breakpoints when using
3081 always-inserted mode. */
3082
3083 static void
3084 update_inserted_breakpoint_locations (void)
3085 {
3086 struct bp_location *bl, **blp_tmp;
3087 int error_flag = 0;
3088 int val = 0;
3089 int disabled_breaks = 0;
3090 int hw_breakpoint_error = 0;
3091 int hw_bp_details_reported = 0;
3092
3093 string_file tmp_error_stream;
3094
3095 /* Explicitly mark the warning -- this will only be printed if
3096 there was an error. */
3097 tmp_error_stream.puts ("Warning:\n");
3098
3099 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3100
3101 ALL_BP_LOCATIONS (bl, blp_tmp)
3102 {
3103 /* We only want to update software breakpoints and hardware
3104 breakpoints. */
3105 if (!is_breakpoint (bl->owner))
3106 continue;
3107
3108 /* We only want to update locations that are already inserted
3109 and need updating. This is to avoid unwanted insertion during
3110 deletion of breakpoints. */
3111 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3112 continue;
3113
3114 switch_to_program_space_and_thread (bl->pspace);
3115
3116 /* For targets that support global breakpoints, there's no need
3117 to select an inferior to insert breakpoint to. In fact, even
3118 if we aren't attached to any process yet, we should still
3119 insert breakpoints. */
3120 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3121 && ptid_equal (inferior_ptid, null_ptid))
3122 continue;
3123
3124 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3125 &hw_breakpoint_error, &hw_bp_details_reported);
3126 if (val)
3127 error_flag = val;
3128 }
3129
3130 if (error_flag)
3131 {
3132 target_terminal_ours_for_output ();
3133 error_stream (tmp_error_stream);
3134 }
3135 }
3136
3137 /* Used when starting or continuing the program. */
3138
3139 static void
3140 insert_breakpoint_locations (void)
3141 {
3142 struct breakpoint *bpt;
3143 struct bp_location *bl, **blp_tmp;
3144 int error_flag = 0;
3145 int val = 0;
3146 int disabled_breaks = 0;
3147 int hw_breakpoint_error = 0;
3148 int hw_bp_error_explained_already = 0;
3149
3150 string_file tmp_error_stream;
3151
3152 /* Explicitly mark the warning -- this will only be printed if
3153 there was an error. */
3154 tmp_error_stream.puts ("Warning:\n");
3155
3156 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3157
3158 ALL_BP_LOCATIONS (bl, blp_tmp)
3159 {
3160 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3161 continue;
3162
3163 /* There is no point inserting thread-specific breakpoints if
3164 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3165 has BL->OWNER always non-NULL. */
3166 if (bl->owner->thread != -1
3167 && !valid_global_thread_id (bl->owner->thread))
3168 continue;
3169
3170 switch_to_program_space_and_thread (bl->pspace);
3171
3172 /* For targets that support global breakpoints, there's no need
3173 to select an inferior to insert breakpoint to. In fact, even
3174 if we aren't attached to any process yet, we should still
3175 insert breakpoints. */
3176 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3177 && ptid_equal (inferior_ptid, null_ptid))
3178 continue;
3179
3180 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3181 &hw_breakpoint_error, &hw_bp_error_explained_already);
3182 if (val)
3183 error_flag = val;
3184 }
3185
3186 /* If we failed to insert all locations of a watchpoint, remove
3187 them, as half-inserted watchpoint is of limited use. */
3188 ALL_BREAKPOINTS (bpt)
3189 {
3190 int some_failed = 0;
3191 struct bp_location *loc;
3192
3193 if (!is_hardware_watchpoint (bpt))
3194 continue;
3195
3196 if (!breakpoint_enabled (bpt))
3197 continue;
3198
3199 if (bpt->disposition == disp_del_at_next_stop)
3200 continue;
3201
3202 for (loc = bpt->loc; loc; loc = loc->next)
3203 if (!loc->inserted && should_be_inserted (loc))
3204 {
3205 some_failed = 1;
3206 break;
3207 }
3208 if (some_failed)
3209 {
3210 for (loc = bpt->loc; loc; loc = loc->next)
3211 if (loc->inserted)
3212 remove_breakpoint (loc);
3213
3214 hw_breakpoint_error = 1;
3215 tmp_error_stream.printf ("Could not insert "
3216 "hardware watchpoint %d.\n",
3217 bpt->number);
3218 error_flag = -1;
3219 }
3220 }
3221
3222 if (error_flag)
3223 {
3224 /* If a hardware breakpoint or watchpoint was inserted, add a
3225 message about possibly exhausted resources. */
3226 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3227 {
3228 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3229 You may have requested too many hardware breakpoints/watchpoints.\n");
3230 }
3231 target_terminal_ours_for_output ();
3232 error_stream (tmp_error_stream);
3233 }
3234 }
3235
3236 /* Used when the program stops.
3237 Returns zero if successful, or non-zero if there was a problem
3238 removing a breakpoint location. */
3239
3240 int
3241 remove_breakpoints (void)
3242 {
3243 struct bp_location *bl, **blp_tmp;
3244 int val = 0;
3245
3246 ALL_BP_LOCATIONS (bl, blp_tmp)
3247 {
3248 if (bl->inserted && !is_tracepoint (bl->owner))
3249 val |= remove_breakpoint (bl);
3250 }
3251 return val;
3252 }
3253
3254 /* When a thread exits, remove breakpoints that are related to
3255 that thread. */
3256
3257 static void
3258 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3259 {
3260 struct breakpoint *b, *b_tmp;
3261
3262 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3263 {
3264 if (b->thread == tp->global_num && user_breakpoint_p (b))
3265 {
3266 b->disposition = disp_del_at_next_stop;
3267
3268 printf_filtered (_("\
3269 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3270 b->number, print_thread_id (tp));
3271
3272 /* Hide it from the user. */
3273 b->number = 0;
3274 }
3275 }
3276 }
3277
3278 /* Remove breakpoints of process PID. */
3279
3280 int
3281 remove_breakpoints_pid (int pid)
3282 {
3283 struct bp_location *bl, **blp_tmp;
3284 int val;
3285 struct inferior *inf = find_inferior_pid (pid);
3286
3287 ALL_BP_LOCATIONS (bl, blp_tmp)
3288 {
3289 if (bl->pspace != inf->pspace)
3290 continue;
3291
3292 if (bl->inserted && !bl->target_info.persist)
3293 {
3294 val = remove_breakpoint (bl);
3295 if (val != 0)
3296 return val;
3297 }
3298 }
3299 return 0;
3300 }
3301
3302 int
3303 reattach_breakpoints (int pid)
3304 {
3305 struct bp_location *bl, **blp_tmp;
3306 int val;
3307 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3308 struct inferior *inf;
3309 struct thread_info *tp;
3310
3311 tp = any_live_thread_of_process (pid);
3312 if (tp == NULL)
3313 return 1;
3314
3315 inf = find_inferior_pid (pid);
3316
3317 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3318 inferior_ptid = tp->ptid;
3319
3320 string_file tmp_error_stream;
3321
3322 ALL_BP_LOCATIONS (bl, blp_tmp)
3323 {
3324 if (bl->pspace != inf->pspace)
3325 continue;
3326
3327 if (bl->inserted)
3328 {
3329 bl->inserted = 0;
3330 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3331 if (val != 0)
3332 return val;
3333 }
3334 }
3335 return 0;
3336 }
3337
3338 static int internal_breakpoint_number = -1;
3339
3340 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3341 If INTERNAL is non-zero, the breakpoint number will be populated
3342 from internal_breakpoint_number and that variable decremented.
3343 Otherwise the breakpoint number will be populated from
3344 breakpoint_count and that value incremented. Internal breakpoints
3345 do not set the internal var bpnum. */
3346 static void
3347 set_breakpoint_number (int internal, struct breakpoint *b)
3348 {
3349 if (internal)
3350 b->number = internal_breakpoint_number--;
3351 else
3352 {
3353 set_breakpoint_count (breakpoint_count + 1);
3354 b->number = breakpoint_count;
3355 }
3356 }
3357
3358 static struct breakpoint *
3359 create_internal_breakpoint (struct gdbarch *gdbarch,
3360 CORE_ADDR address, enum bptype type,
3361 const struct breakpoint_ops *ops)
3362 {
3363 struct symtab_and_line sal;
3364 struct breakpoint *b;
3365
3366 init_sal (&sal); /* Initialize to zeroes. */
3367
3368 sal.pc = address;
3369 sal.section = find_pc_overlay (sal.pc);
3370 sal.pspace = current_program_space;
3371
3372 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3373 b->number = internal_breakpoint_number--;
3374 b->disposition = disp_donttouch;
3375
3376 return b;
3377 }
3378
3379 static const char *const longjmp_names[] =
3380 {
3381 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3382 };
3383 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3384
3385 /* Per-objfile data private to breakpoint.c. */
3386 struct breakpoint_objfile_data
3387 {
3388 /* Minimal symbol for "_ovly_debug_event" (if any). */
3389 struct bound_minimal_symbol overlay_msym;
3390
3391 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3392 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3393
3394 /* True if we have looked for longjmp probes. */
3395 int longjmp_searched;
3396
3397 /* SystemTap probe points for longjmp (if any). */
3398 VEC (probe_p) *longjmp_probes;
3399
3400 /* Minimal symbol for "std::terminate()" (if any). */
3401 struct bound_minimal_symbol terminate_msym;
3402
3403 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3404 struct bound_minimal_symbol exception_msym;
3405
3406 /* True if we have looked for exception probes. */
3407 int exception_searched;
3408
3409 /* SystemTap probe points for unwinding (if any). */
3410 VEC (probe_p) *exception_probes;
3411 };
3412
3413 static const struct objfile_data *breakpoint_objfile_key;
3414
3415 /* Minimal symbol not found sentinel. */
3416 static struct minimal_symbol msym_not_found;
3417
3418 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3419
3420 static int
3421 msym_not_found_p (const struct minimal_symbol *msym)
3422 {
3423 return msym == &msym_not_found;
3424 }
3425
3426 /* Return per-objfile data needed by breakpoint.c.
3427 Allocate the data if necessary. */
3428
3429 static struct breakpoint_objfile_data *
3430 get_breakpoint_objfile_data (struct objfile *objfile)
3431 {
3432 struct breakpoint_objfile_data *bp_objfile_data;
3433
3434 bp_objfile_data = ((struct breakpoint_objfile_data *)
3435 objfile_data (objfile, breakpoint_objfile_key));
3436 if (bp_objfile_data == NULL)
3437 {
3438 bp_objfile_data =
3439 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3440
3441 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3442 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3443 }
3444 return bp_objfile_data;
3445 }
3446
3447 static void
3448 free_breakpoint_probes (struct objfile *obj, void *data)
3449 {
3450 struct breakpoint_objfile_data *bp_objfile_data
3451 = (struct breakpoint_objfile_data *) data;
3452
3453 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3454 VEC_free (probe_p, bp_objfile_data->exception_probes);
3455 }
3456
3457 static void
3458 create_overlay_event_breakpoint (void)
3459 {
3460 struct objfile *objfile;
3461 const char *const func_name = "_ovly_debug_event";
3462
3463 ALL_OBJFILES (objfile)
3464 {
3465 struct breakpoint *b;
3466 struct breakpoint_objfile_data *bp_objfile_data;
3467 CORE_ADDR addr;
3468 struct explicit_location explicit_loc;
3469
3470 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3471
3472 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3473 continue;
3474
3475 if (bp_objfile_data->overlay_msym.minsym == NULL)
3476 {
3477 struct bound_minimal_symbol m;
3478
3479 m = lookup_minimal_symbol_text (func_name, objfile);
3480 if (m.minsym == NULL)
3481 {
3482 /* Avoid future lookups in this objfile. */
3483 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3484 continue;
3485 }
3486 bp_objfile_data->overlay_msym = m;
3487 }
3488
3489 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3490 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3491 bp_overlay_event,
3492 &internal_breakpoint_ops);
3493 initialize_explicit_location (&explicit_loc);
3494 explicit_loc.function_name = ASTRDUP (func_name);
3495 b->location = new_explicit_location (&explicit_loc);
3496
3497 if (overlay_debugging == ovly_auto)
3498 {
3499 b->enable_state = bp_enabled;
3500 overlay_events_enabled = 1;
3501 }
3502 else
3503 {
3504 b->enable_state = bp_disabled;
3505 overlay_events_enabled = 0;
3506 }
3507 }
3508 }
3509
3510 static void
3511 create_longjmp_master_breakpoint (void)
3512 {
3513 struct program_space *pspace;
3514
3515 scoped_restore_current_program_space restore_pspace;
3516
3517 ALL_PSPACES (pspace)
3518 {
3519 struct objfile *objfile;
3520
3521 set_current_program_space (pspace);
3522
3523 ALL_OBJFILES (objfile)
3524 {
3525 int i;
3526 struct gdbarch *gdbarch;
3527 struct breakpoint_objfile_data *bp_objfile_data;
3528
3529 gdbarch = get_objfile_arch (objfile);
3530
3531 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3532
3533 if (!bp_objfile_data->longjmp_searched)
3534 {
3535 VEC (probe_p) *ret;
3536
3537 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3538 if (ret != NULL)
3539 {
3540 /* We are only interested in checking one element. */
3541 struct probe *p = VEC_index (probe_p, ret, 0);
3542
3543 if (!can_evaluate_probe_arguments (p))
3544 {
3545 /* We cannot use the probe interface here, because it does
3546 not know how to evaluate arguments. */
3547 VEC_free (probe_p, ret);
3548 ret = NULL;
3549 }
3550 }
3551 bp_objfile_data->longjmp_probes = ret;
3552 bp_objfile_data->longjmp_searched = 1;
3553 }
3554
3555 if (bp_objfile_data->longjmp_probes != NULL)
3556 {
3557 int i;
3558 struct probe *probe;
3559 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3560
3561 for (i = 0;
3562 VEC_iterate (probe_p,
3563 bp_objfile_data->longjmp_probes,
3564 i, probe);
3565 ++i)
3566 {
3567 struct breakpoint *b;
3568
3569 b = create_internal_breakpoint (gdbarch,
3570 get_probe_address (probe,
3571 objfile),
3572 bp_longjmp_master,
3573 &internal_breakpoint_ops);
3574 b->location = new_probe_location ("-probe-stap libc:longjmp");
3575 b->enable_state = bp_disabled;
3576 }
3577
3578 continue;
3579 }
3580
3581 if (!gdbarch_get_longjmp_target_p (gdbarch))
3582 continue;
3583
3584 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3585 {
3586 struct breakpoint *b;
3587 const char *func_name;
3588 CORE_ADDR addr;
3589 struct explicit_location explicit_loc;
3590
3591 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3592 continue;
3593
3594 func_name = longjmp_names[i];
3595 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3596 {
3597 struct bound_minimal_symbol m;
3598
3599 m = lookup_minimal_symbol_text (func_name, objfile);
3600 if (m.minsym == NULL)
3601 {
3602 /* Prevent future lookups in this objfile. */
3603 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3604 continue;
3605 }
3606 bp_objfile_data->longjmp_msym[i] = m;
3607 }
3608
3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3610 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3611 &internal_breakpoint_ops);
3612 initialize_explicit_location (&explicit_loc);
3613 explicit_loc.function_name = ASTRDUP (func_name);
3614 b->location = new_explicit_location (&explicit_loc);
3615 b->enable_state = bp_disabled;
3616 }
3617 }
3618 }
3619 }
3620
3621 /* Create a master std::terminate breakpoint. */
3622 static void
3623 create_std_terminate_master_breakpoint (void)
3624 {
3625 struct program_space *pspace;
3626 const char *const func_name = "std::terminate()";
3627
3628 scoped_restore_current_program_space restore_pspace;
3629
3630 ALL_PSPACES (pspace)
3631 {
3632 struct objfile *objfile;
3633 CORE_ADDR addr;
3634
3635 set_current_program_space (pspace);
3636
3637 ALL_OBJFILES (objfile)
3638 {
3639 struct breakpoint *b;
3640 struct breakpoint_objfile_data *bp_objfile_data;
3641 struct explicit_location explicit_loc;
3642
3643 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3644
3645 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3646 continue;
3647
3648 if (bp_objfile_data->terminate_msym.minsym == NULL)
3649 {
3650 struct bound_minimal_symbol m;
3651
3652 m = lookup_minimal_symbol (func_name, NULL, objfile);
3653 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3654 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3655 {
3656 /* Prevent future lookups in this objfile. */
3657 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3658 continue;
3659 }
3660 bp_objfile_data->terminate_msym = m;
3661 }
3662
3663 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3664 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3665 bp_std_terminate_master,
3666 &internal_breakpoint_ops);
3667 initialize_explicit_location (&explicit_loc);
3668 explicit_loc.function_name = ASTRDUP (func_name);
3669 b->location = new_explicit_location (&explicit_loc);
3670 b->enable_state = bp_disabled;
3671 }
3672 }
3673 }
3674
3675 /* Install a master breakpoint on the unwinder's debug hook. */
3676
3677 static void
3678 create_exception_master_breakpoint (void)
3679 {
3680 struct objfile *objfile;
3681 const char *const func_name = "_Unwind_DebugHook";
3682
3683 ALL_OBJFILES (objfile)
3684 {
3685 struct breakpoint *b;
3686 struct gdbarch *gdbarch;
3687 struct breakpoint_objfile_data *bp_objfile_data;
3688 CORE_ADDR addr;
3689 struct explicit_location explicit_loc;
3690
3691 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3692
3693 /* We prefer the SystemTap probe point if it exists. */
3694 if (!bp_objfile_data->exception_searched)
3695 {
3696 VEC (probe_p) *ret;
3697
3698 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3699
3700 if (ret != NULL)
3701 {
3702 /* We are only interested in checking one element. */
3703 struct probe *p = VEC_index (probe_p, ret, 0);
3704
3705 if (!can_evaluate_probe_arguments (p))
3706 {
3707 /* We cannot use the probe interface here, because it does
3708 not know how to evaluate arguments. */
3709 VEC_free (probe_p, ret);
3710 ret = NULL;
3711 }
3712 }
3713 bp_objfile_data->exception_probes = ret;
3714 bp_objfile_data->exception_searched = 1;
3715 }
3716
3717 if (bp_objfile_data->exception_probes != NULL)
3718 {
3719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3720 int i;
3721 struct probe *probe;
3722
3723 for (i = 0;
3724 VEC_iterate (probe_p,
3725 bp_objfile_data->exception_probes,
3726 i, probe);
3727 ++i)
3728 {
3729 struct breakpoint *b;
3730
3731 b = create_internal_breakpoint (gdbarch,
3732 get_probe_address (probe,
3733 objfile),
3734 bp_exception_master,
3735 &internal_breakpoint_ops);
3736 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3737 b->enable_state = bp_disabled;
3738 }
3739
3740 continue;
3741 }
3742
3743 /* Otherwise, try the hook function. */
3744
3745 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3746 continue;
3747
3748 gdbarch = get_objfile_arch (objfile);
3749
3750 if (bp_objfile_data->exception_msym.minsym == NULL)
3751 {
3752 struct bound_minimal_symbol debug_hook;
3753
3754 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3755 if (debug_hook.minsym == NULL)
3756 {
3757 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3758 continue;
3759 }
3760
3761 bp_objfile_data->exception_msym = debug_hook;
3762 }
3763
3764 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3765 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3766 &current_target);
3767 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3768 &internal_breakpoint_ops);
3769 initialize_explicit_location (&explicit_loc);
3770 explicit_loc.function_name = ASTRDUP (func_name);
3771 b->location = new_explicit_location (&explicit_loc);
3772 b->enable_state = bp_disabled;
3773 }
3774 }
3775
3776 /* Does B have a location spec? */
3777
3778 static int
3779 breakpoint_event_location_empty_p (const struct breakpoint *b)
3780 {
3781 return b->location != NULL && event_location_empty_p (b->location.get ());
3782 }
3783
3784 void
3785 update_breakpoints_after_exec (void)
3786 {
3787 struct breakpoint *b, *b_tmp;
3788 struct bp_location *bploc, **bplocp_tmp;
3789
3790 /* We're about to delete breakpoints from GDB's lists. If the
3791 INSERTED flag is true, GDB will try to lift the breakpoints by
3792 writing the breakpoints' "shadow contents" back into memory. The
3793 "shadow contents" are NOT valid after an exec, so GDB should not
3794 do that. Instead, the target is responsible from marking
3795 breakpoints out as soon as it detects an exec. We don't do that
3796 here instead, because there may be other attempts to delete
3797 breakpoints after detecting an exec and before reaching here. */
3798 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3799 if (bploc->pspace == current_program_space)
3800 gdb_assert (!bploc->inserted);
3801
3802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3803 {
3804 if (b->pspace != current_program_space)
3805 continue;
3806
3807 /* Solib breakpoints must be explicitly reset after an exec(). */
3808 if (b->type == bp_shlib_event)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
3813
3814 /* JIT breakpoints must be explicitly reset after an exec(). */
3815 if (b->type == bp_jit_event)
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820
3821 /* Thread event breakpoints must be set anew after an exec(),
3822 as must overlay event and longjmp master breakpoints. */
3823 if (b->type == bp_thread_event || b->type == bp_overlay_event
3824 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3825 || b->type == bp_exception_master)
3826 {
3827 delete_breakpoint (b);
3828 continue;
3829 }
3830
3831 /* Step-resume breakpoints are meaningless after an exec(). */
3832 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
3838 /* Just like single-step breakpoints. */
3839 if (b->type == bp_single_step)
3840 {
3841 delete_breakpoint (b);
3842 continue;
3843 }
3844
3845 /* Longjmp and longjmp-resume breakpoints are also meaningless
3846 after an exec. */
3847 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3848 || b->type == bp_longjmp_call_dummy
3849 || b->type == bp_exception || b->type == bp_exception_resume)
3850 {
3851 delete_breakpoint (b);
3852 continue;
3853 }
3854
3855 if (b->type == bp_catchpoint)
3856 {
3857 /* For now, none of the bp_catchpoint breakpoints need to
3858 do anything at this point. In the future, if some of
3859 the catchpoints need to something, we will need to add
3860 a new method, and call this method from here. */
3861 continue;
3862 }
3863
3864 /* bp_finish is a special case. The only way we ought to be able
3865 to see one of these when an exec() has happened, is if the user
3866 caught a vfork, and then said "finish". Ordinarily a finish just
3867 carries them to the call-site of the current callee, by setting
3868 a temporary bp there and resuming. But in this case, the finish
3869 will carry them entirely through the vfork & exec.
3870
3871 We don't want to allow a bp_finish to remain inserted now. But
3872 we can't safely delete it, 'cause finish_command has a handle to
3873 the bp on a bpstat, and will later want to delete it. There's a
3874 chance (and I've seen it happen) that if we delete the bp_finish
3875 here, that its storage will get reused by the time finish_command
3876 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3877 We really must allow finish_command to delete a bp_finish.
3878
3879 In the absence of a general solution for the "how do we know
3880 it's safe to delete something others may have handles to?"
3881 problem, what we'll do here is just uninsert the bp_finish, and
3882 let finish_command delete it.
3883
3884 (We know the bp_finish is "doomed" in the sense that it's
3885 momentary, and will be deleted as soon as finish_command sees
3886 the inferior stopped. So it doesn't matter that the bp's
3887 address is probably bogus in the new a.out, unlike e.g., the
3888 solib breakpoints.) */
3889
3890 if (b->type == bp_finish)
3891 {
3892 continue;
3893 }
3894
3895 /* Without a symbolic address, we have little hope of the
3896 pre-exec() address meaning the same thing in the post-exec()
3897 a.out. */
3898 if (breakpoint_event_location_empty_p (b))
3899 {
3900 delete_breakpoint (b);
3901 continue;
3902 }
3903 }
3904 }
3905
3906 int
3907 detach_breakpoints (ptid_t ptid)
3908 {
3909 struct bp_location *bl, **blp_tmp;
3910 int val = 0;
3911 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3912 struct inferior *inf = current_inferior ();
3913
3914 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3915 error (_("Cannot detach breakpoints of inferior_ptid"));
3916
3917 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3918 inferior_ptid = ptid;
3919 ALL_BP_LOCATIONS (bl, blp_tmp)
3920 {
3921 if (bl->pspace != inf->pspace)
3922 continue;
3923
3924 /* This function must physically remove breakpoints locations
3925 from the specified ptid, without modifying the breakpoint
3926 package's state. Locations of type bp_loc_other are only
3927 maintained at GDB side. So, there is no need to remove
3928 these bp_loc_other locations. Moreover, removing these
3929 would modify the breakpoint package's state. */
3930 if (bl->loc_type == bp_loc_other)
3931 continue;
3932
3933 if (bl->inserted)
3934 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3935 }
3936
3937 return val;
3938 }
3939
3940 /* Remove the breakpoint location BL from the current address space.
3941 Note that this is used to detach breakpoints from a child fork.
3942 When we get here, the child isn't in the inferior list, and neither
3943 do we have objects to represent its address space --- we should
3944 *not* look at bl->pspace->aspace here. */
3945
3946 static int
3947 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3948 {
3949 int val;
3950
3951 /* BL is never in moribund_locations by our callers. */
3952 gdb_assert (bl->owner != NULL);
3953
3954 /* The type of none suggests that owner is actually deleted.
3955 This should not ever happen. */
3956 gdb_assert (bl->owner->type != bp_none);
3957
3958 if (bl->loc_type == bp_loc_software_breakpoint
3959 || bl->loc_type == bp_loc_hardware_breakpoint)
3960 {
3961 /* "Normal" instruction breakpoint: either the standard
3962 trap-instruction bp (bp_breakpoint), or a
3963 bp_hardware_breakpoint. */
3964
3965 /* First check to see if we have to handle an overlay. */
3966 if (overlay_debugging == ovly_off
3967 || bl->section == NULL
3968 || !(section_is_overlay (bl->section)))
3969 {
3970 /* No overlay handling: just remove the breakpoint. */
3971
3972 /* If we're trying to uninsert a memory breakpoint that we
3973 know is set in a dynamic object that is marked
3974 shlib_disabled, then either the dynamic object was
3975 removed with "remove-symbol-file" or with
3976 "nosharedlibrary". In the former case, we don't know
3977 whether another dynamic object might have loaded over the
3978 breakpoint's address -- the user might well let us know
3979 about it next with add-symbol-file (the whole point of
3980 add-symbol-file is letting the user manually maintain a
3981 list of dynamically loaded objects). If we have the
3982 breakpoint's shadow memory, that is, this is a software
3983 breakpoint managed by GDB, check whether the breakpoint
3984 is still inserted in memory, to avoid overwriting wrong
3985 code with stale saved shadow contents. Note that HW
3986 breakpoints don't have shadow memory, as they're
3987 implemented using a mechanism that is not dependent on
3988 being able to modify the target's memory, and as such
3989 they should always be removed. */
3990 if (bl->shlib_disabled
3991 && bl->target_info.shadow_len != 0
3992 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3993 val = 0;
3994 else
3995 val = bl->owner->ops->remove_location (bl, reason);
3996 }
3997 else
3998 {
3999 /* This breakpoint is in an overlay section.
4000 Did we set a breakpoint at the LMA? */
4001 if (!overlay_events_enabled)
4002 {
4003 /* Yes -- overlay event support is not active, so we
4004 should have set a breakpoint at the LMA. Remove it.
4005 */
4006 /* Ignore any failures: if the LMA is in ROM, we will
4007 have already warned when we failed to insert it. */
4008 if (bl->loc_type == bp_loc_hardware_breakpoint)
4009 target_remove_hw_breakpoint (bl->gdbarch,
4010 &bl->overlay_target_info);
4011 else
4012 target_remove_breakpoint (bl->gdbarch,
4013 &bl->overlay_target_info,
4014 reason);
4015 }
4016 /* Did we set a breakpoint at the VMA?
4017 If so, we will have marked the breakpoint 'inserted'. */
4018 if (bl->inserted)
4019 {
4020 /* Yes -- remove it. Previously we did not bother to
4021 remove the breakpoint if the section had been
4022 unmapped, but let's not rely on that being safe. We
4023 don't know what the overlay manager might do. */
4024
4025 /* However, we should remove *software* breakpoints only
4026 if the section is still mapped, or else we overwrite
4027 wrong code with the saved shadow contents. */
4028 if (bl->loc_type == bp_loc_hardware_breakpoint
4029 || section_is_mapped (bl->section))
4030 val = bl->owner->ops->remove_location (bl, reason);
4031 else
4032 val = 0;
4033 }
4034 else
4035 {
4036 /* No -- not inserted, so no need to remove. No error. */
4037 val = 0;
4038 }
4039 }
4040
4041 /* In some cases, we might not be able to remove a breakpoint in
4042 a shared library that has already been removed, but we have
4043 not yet processed the shlib unload event. Similarly for an
4044 unloaded add-symbol-file object - the user might not yet have
4045 had the chance to remove-symbol-file it. shlib_disabled will
4046 be set if the library/object has already been removed, but
4047 the breakpoint hasn't been uninserted yet, e.g., after
4048 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4049 always-inserted mode. */
4050 if (val
4051 && (bl->loc_type == bp_loc_software_breakpoint
4052 && (bl->shlib_disabled
4053 || solib_name_from_address (bl->pspace, bl->address)
4054 || shared_objfile_contains_address_p (bl->pspace,
4055 bl->address))))
4056 val = 0;
4057
4058 if (val)
4059 return val;
4060 bl->inserted = (reason == DETACH_BREAKPOINT);
4061 }
4062 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4063 {
4064 gdb_assert (bl->owner->ops != NULL
4065 && bl->owner->ops->remove_location != NULL);
4066
4067 bl->inserted = (reason == DETACH_BREAKPOINT);
4068 bl->owner->ops->remove_location (bl, reason);
4069
4070 /* Failure to remove any of the hardware watchpoints comes here. */
4071 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4072 warning (_("Could not remove hardware watchpoint %d."),
4073 bl->owner->number);
4074 }
4075 else if (bl->owner->type == bp_catchpoint
4076 && breakpoint_enabled (bl->owner)
4077 && !bl->duplicate)
4078 {
4079 gdb_assert (bl->owner->ops != NULL
4080 && bl->owner->ops->remove_location != NULL);
4081
4082 val = bl->owner->ops->remove_location (bl, reason);
4083 if (val)
4084 return val;
4085
4086 bl->inserted = (reason == DETACH_BREAKPOINT);
4087 }
4088
4089 return 0;
4090 }
4091
4092 static int
4093 remove_breakpoint (struct bp_location *bl)
4094 {
4095 /* BL is never in moribund_locations by our callers. */
4096 gdb_assert (bl->owner != NULL);
4097
4098 /* The type of none suggests that owner is actually deleted.
4099 This should not ever happen. */
4100 gdb_assert (bl->owner->type != bp_none);
4101
4102 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4103
4104 switch_to_program_space_and_thread (bl->pspace);
4105
4106 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4107 }
4108
4109 /* Clear the "inserted" flag in all breakpoints. */
4110
4111 void
4112 mark_breakpoints_out (void)
4113 {
4114 struct bp_location *bl, **blp_tmp;
4115
4116 ALL_BP_LOCATIONS (bl, blp_tmp)
4117 if (bl->pspace == current_program_space)
4118 bl->inserted = 0;
4119 }
4120
4121 /* Clear the "inserted" flag in all breakpoints and delete any
4122 breakpoints which should go away between runs of the program.
4123
4124 Plus other such housekeeping that has to be done for breakpoints
4125 between runs.
4126
4127 Note: this function gets called at the end of a run (by
4128 generic_mourn_inferior) and when a run begins (by
4129 init_wait_for_inferior). */
4130
4131
4132
4133 void
4134 breakpoint_init_inferior (enum inf_context context)
4135 {
4136 struct breakpoint *b, *b_tmp;
4137 struct bp_location *bl;
4138 int ix;
4139 struct program_space *pspace = current_program_space;
4140
4141 /* If breakpoint locations are shared across processes, then there's
4142 nothing to do. */
4143 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4144 return;
4145
4146 mark_breakpoints_out ();
4147
4148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4149 {
4150 if (b->loc && b->loc->pspace != pspace)
4151 continue;
4152
4153 switch (b->type)
4154 {
4155 case bp_call_dummy:
4156 case bp_longjmp_call_dummy:
4157
4158 /* If the call dummy breakpoint is at the entry point it will
4159 cause problems when the inferior is rerun, so we better get
4160 rid of it. */
4161
4162 case bp_watchpoint_scope:
4163
4164 /* Also get rid of scope breakpoints. */
4165
4166 case bp_shlib_event:
4167
4168 /* Also remove solib event breakpoints. Their addresses may
4169 have changed since the last time we ran the program.
4170 Actually we may now be debugging against different target;
4171 and so the solib backend that installed this breakpoint may
4172 not be used in by the target. E.g.,
4173
4174 (gdb) file prog-linux
4175 (gdb) run # native linux target
4176 ...
4177 (gdb) kill
4178 (gdb) file prog-win.exe
4179 (gdb) tar rem :9999 # remote Windows gdbserver.
4180 */
4181
4182 case bp_step_resume:
4183
4184 /* Also remove step-resume breakpoints. */
4185
4186 case bp_single_step:
4187
4188 /* Also remove single-step breakpoints. */
4189
4190 delete_breakpoint (b);
4191 break;
4192
4193 case bp_watchpoint:
4194 case bp_hardware_watchpoint:
4195 case bp_read_watchpoint:
4196 case bp_access_watchpoint:
4197 {
4198 struct watchpoint *w = (struct watchpoint *) b;
4199
4200 /* Likewise for watchpoints on local expressions. */
4201 if (w->exp_valid_block != NULL)
4202 delete_breakpoint (b);
4203 else
4204 {
4205 /* Get rid of existing locations, which are no longer
4206 valid. New ones will be created in
4207 update_watchpoint, when the inferior is restarted.
4208 The next update_global_location_list call will
4209 garbage collect them. */
4210 b->loc = NULL;
4211
4212 if (context == inf_starting)
4213 {
4214 /* Reset val field to force reread of starting value in
4215 insert_breakpoints. */
4216 if (w->val)
4217 value_free (w->val);
4218 w->val = NULL;
4219 w->val_valid = 0;
4220 }
4221 }
4222 }
4223 break;
4224 default:
4225 break;
4226 }
4227 }
4228
4229 /* Get rid of the moribund locations. */
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4231 decref_bp_location (&bl);
4232 VEC_free (bp_location_p, moribund_locations);
4233 }
4234
4235 /* These functions concern about actual breakpoints inserted in the
4236 target --- to e.g. check if we need to do decr_pc adjustment or if
4237 we need to hop over the bkpt --- so we check for address space
4238 match, not program space. */
4239
4240 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4241 exists at PC. It returns ordinary_breakpoint_here if it's an
4242 ordinary breakpoint, or permanent_breakpoint_here if it's a
4243 permanent breakpoint.
4244 - When continuing from a location with an ordinary breakpoint, we
4245 actually single step once before calling insert_breakpoints.
4246 - When continuing from a location with a permanent breakpoint, we
4247 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4248 the target, to advance the PC past the breakpoint. */
4249
4250 enum breakpoint_here
4251 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4252 {
4253 struct bp_location *bl, **blp_tmp;
4254 int any_breakpoint_here = 0;
4255
4256 ALL_BP_LOCATIONS (bl, blp_tmp)
4257 {
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
4260 continue;
4261
4262 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4263 if ((breakpoint_enabled (bl->owner)
4264 || bl->permanent)
4265 && breakpoint_location_address_match (bl, aspace, pc))
4266 {
4267 if (overlay_debugging
4268 && section_is_overlay (bl->section)
4269 && !section_is_mapped (bl->section))
4270 continue; /* unmapped overlay -- can't be a match */
4271 else if (bl->permanent)
4272 return permanent_breakpoint_here;
4273 else
4274 any_breakpoint_here = 1;
4275 }
4276 }
4277
4278 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4279 }
4280
4281 /* See breakpoint.h. */
4282
4283 int
4284 breakpoint_in_range_p (struct address_space *aspace,
4285 CORE_ADDR addr, ULONGEST len)
4286 {
4287 struct bp_location *bl, **blp_tmp;
4288
4289 ALL_BP_LOCATIONS (bl, blp_tmp)
4290 {
4291 if (bl->loc_type != bp_loc_software_breakpoint
4292 && bl->loc_type != bp_loc_hardware_breakpoint)
4293 continue;
4294
4295 if ((breakpoint_enabled (bl->owner)
4296 || bl->permanent)
4297 && breakpoint_location_address_range_overlap (bl, aspace,
4298 addr, len))
4299 {
4300 if (overlay_debugging
4301 && section_is_overlay (bl->section)
4302 && !section_is_mapped (bl->section))
4303 {
4304 /* Unmapped overlay -- can't be a match. */
4305 continue;
4306 }
4307
4308 return 1;
4309 }
4310 }
4311
4312 return 0;
4313 }
4314
4315 /* Return true if there's a moribund breakpoint at PC. */
4316
4317 int
4318 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4319 {
4320 struct bp_location *loc;
4321 int ix;
4322
4323 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4324 if (breakpoint_location_address_match (loc, aspace, pc))
4325 return 1;
4326
4327 return 0;
4328 }
4329
4330 /* Returns non-zero iff BL is inserted at PC, in address space
4331 ASPACE. */
4332
4333 static int
4334 bp_location_inserted_here_p (struct bp_location *bl,
4335 struct address_space *aspace, CORE_ADDR pc)
4336 {
4337 if (bl->inserted
4338 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4339 aspace, pc))
4340 {
4341 if (overlay_debugging
4342 && section_is_overlay (bl->section)
4343 && !section_is_mapped (bl->section))
4344 return 0; /* unmapped overlay -- can't be a match */
4345 else
4346 return 1;
4347 }
4348 return 0;
4349 }
4350
4351 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4352
4353 int
4354 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4355 {
4356 struct bp_location **blp, **blp_tmp = NULL;
4357
4358 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4359 {
4360 struct bp_location *bl = *blp;
4361
4362 if (bl->loc_type != bp_loc_software_breakpoint
4363 && bl->loc_type != bp_loc_hardware_breakpoint)
4364 continue;
4365
4366 if (bp_location_inserted_here_p (bl, aspace, pc))
4367 return 1;
4368 }
4369 return 0;
4370 }
4371
4372 /* This function returns non-zero iff there is a software breakpoint
4373 inserted at PC. */
4374
4375 int
4376 software_breakpoint_inserted_here_p (struct address_space *aspace,
4377 CORE_ADDR pc)
4378 {
4379 struct bp_location **blp, **blp_tmp = NULL;
4380
4381 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4382 {
4383 struct bp_location *bl = *blp;
4384
4385 if (bl->loc_type != bp_loc_software_breakpoint)
4386 continue;
4387
4388 if (bp_location_inserted_here_p (bl, aspace, pc))
4389 return 1;
4390 }
4391
4392 return 0;
4393 }
4394
4395 /* See breakpoint.h. */
4396
4397 int
4398 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4399 CORE_ADDR pc)
4400 {
4401 struct bp_location **blp, **blp_tmp = NULL;
4402
4403 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4404 {
4405 struct bp_location *bl = *blp;
4406
4407 if (bl->loc_type != bp_loc_hardware_breakpoint)
4408 continue;
4409
4410 if (bp_location_inserted_here_p (bl, aspace, pc))
4411 return 1;
4412 }
4413
4414 return 0;
4415 }
4416
4417 int
4418 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4419 CORE_ADDR addr, ULONGEST len)
4420 {
4421 struct breakpoint *bpt;
4422
4423 ALL_BREAKPOINTS (bpt)
4424 {
4425 struct bp_location *loc;
4426
4427 if (bpt->type != bp_hardware_watchpoint
4428 && bpt->type != bp_access_watchpoint)
4429 continue;
4430
4431 if (!breakpoint_enabled (bpt))
4432 continue;
4433
4434 for (loc = bpt->loc; loc; loc = loc->next)
4435 if (loc->pspace->aspace == aspace && loc->inserted)
4436 {
4437 CORE_ADDR l, h;
4438
4439 /* Check for intersection. */
4440 l = std::max<CORE_ADDR> (loc->address, addr);
4441 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4442 if (l < h)
4443 return 1;
4444 }
4445 }
4446 return 0;
4447 }
4448 \f
4449
4450 /* bpstat stuff. External routines' interfaces are documented
4451 in breakpoint.h. */
4452
4453 int
4454 is_catchpoint (struct breakpoint *ep)
4455 {
4456 return (ep->type == bp_catchpoint);
4457 }
4458
4459 /* Frees any storage that is part of a bpstat. Does not walk the
4460 'next' chain. */
4461
4462 static void
4463 bpstat_free (bpstat bs)
4464 {
4465 if (bs->old_val != NULL)
4466 value_free (bs->old_val);
4467 decref_counted_command_line (&bs->commands);
4468 decref_bp_location (&bs->bp_location_at);
4469 xfree (bs);
4470 }
4471
4472 /* Clear a bpstat so that it says we are not at any breakpoint.
4473 Also free any storage that is part of a bpstat. */
4474
4475 void
4476 bpstat_clear (bpstat *bsp)
4477 {
4478 bpstat p;
4479 bpstat q;
4480
4481 if (bsp == 0)
4482 return;
4483 p = *bsp;
4484 while (p != NULL)
4485 {
4486 q = p->next;
4487 bpstat_free (p);
4488 p = q;
4489 }
4490 *bsp = NULL;
4491 }
4492
4493 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4494 is part of the bpstat is copied as well. */
4495
4496 bpstat
4497 bpstat_copy (bpstat bs)
4498 {
4499 bpstat p = NULL;
4500 bpstat tmp;
4501 bpstat retval = NULL;
4502
4503 if (bs == NULL)
4504 return bs;
4505
4506 for (; bs != NULL; bs = bs->next)
4507 {
4508 tmp = (bpstat) xmalloc (sizeof (*tmp));
4509 memcpy (tmp, bs, sizeof (*tmp));
4510 incref_counted_command_line (tmp->commands);
4511 incref_bp_location (tmp->bp_location_at);
4512 if (bs->old_val != NULL)
4513 {
4514 tmp->old_val = value_copy (bs->old_val);
4515 release_value (tmp->old_val);
4516 }
4517
4518 if (p == NULL)
4519 /* This is the first thing in the chain. */
4520 retval = tmp;
4521 else
4522 p->next = tmp;
4523 p = tmp;
4524 }
4525 p->next = NULL;
4526 return retval;
4527 }
4528
4529 /* Find the bpstat associated with this breakpoint. */
4530
4531 bpstat
4532 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4533 {
4534 if (bsp == NULL)
4535 return NULL;
4536
4537 for (; bsp != NULL; bsp = bsp->next)
4538 {
4539 if (bsp->breakpoint_at == breakpoint)
4540 return bsp;
4541 }
4542 return NULL;
4543 }
4544
4545 /* See breakpoint.h. */
4546
4547 int
4548 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4549 {
4550 for (; bsp != NULL; bsp = bsp->next)
4551 {
4552 if (bsp->breakpoint_at == NULL)
4553 {
4554 /* A moribund location can never explain a signal other than
4555 GDB_SIGNAL_TRAP. */
4556 if (sig == GDB_SIGNAL_TRAP)
4557 return 1;
4558 }
4559 else
4560 {
4561 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4562 sig))
4563 return 1;
4564 }
4565 }
4566
4567 return 0;
4568 }
4569
4570 /* Put in *NUM the breakpoint number of the first breakpoint we are
4571 stopped at. *BSP upon return is a bpstat which points to the
4572 remaining breakpoints stopped at (but which is not guaranteed to be
4573 good for anything but further calls to bpstat_num).
4574
4575 Return 0 if passed a bpstat which does not indicate any breakpoints.
4576 Return -1 if stopped at a breakpoint that has been deleted since
4577 we set it.
4578 Return 1 otherwise. */
4579
4580 int
4581 bpstat_num (bpstat *bsp, int *num)
4582 {
4583 struct breakpoint *b;
4584
4585 if ((*bsp) == NULL)
4586 return 0; /* No more breakpoint values */
4587
4588 /* We assume we'll never have several bpstats that correspond to a
4589 single breakpoint -- otherwise, this function might return the
4590 same number more than once and this will look ugly. */
4591 b = (*bsp)->breakpoint_at;
4592 *bsp = (*bsp)->next;
4593 if (b == NULL)
4594 return -1; /* breakpoint that's been deleted since */
4595
4596 *num = b->number; /* We have its number */
4597 return 1;
4598 }
4599
4600 /* See breakpoint.h. */
4601
4602 void
4603 bpstat_clear_actions (void)
4604 {
4605 struct thread_info *tp;
4606 bpstat bs;
4607
4608 if (ptid_equal (inferior_ptid, null_ptid))
4609 return;
4610
4611 tp = find_thread_ptid (inferior_ptid);
4612 if (tp == NULL)
4613 return;
4614
4615 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4616 {
4617 decref_counted_command_line (&bs->commands);
4618
4619 if (bs->old_val != NULL)
4620 {
4621 value_free (bs->old_val);
4622 bs->old_val = NULL;
4623 }
4624 }
4625 }
4626
4627 /* Called when a command is about to proceed the inferior. */
4628
4629 static void
4630 breakpoint_about_to_proceed (void)
4631 {
4632 if (!ptid_equal (inferior_ptid, null_ptid))
4633 {
4634 struct thread_info *tp = inferior_thread ();
4635
4636 /* Allow inferior function calls in breakpoint commands to not
4637 interrupt the command list. When the call finishes
4638 successfully, the inferior will be standing at the same
4639 breakpoint as if nothing happened. */
4640 if (tp->control.in_infcall)
4641 return;
4642 }
4643
4644 breakpoint_proceeded = 1;
4645 }
4646
4647 /* Stub for cleaning up our state if we error-out of a breakpoint
4648 command. */
4649 static void
4650 cleanup_executing_breakpoints (void *ignore)
4651 {
4652 executing_breakpoint_commands = 0;
4653 }
4654
4655 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4656 or its equivalent. */
4657
4658 static int
4659 command_line_is_silent (struct command_line *cmd)
4660 {
4661 return cmd && (strcmp ("silent", cmd->line) == 0);
4662 }
4663
4664 /* Execute all the commands associated with all the breakpoints at
4665 this location. Any of these commands could cause the process to
4666 proceed beyond this point, etc. We look out for such changes by
4667 checking the global "breakpoint_proceeded" after each command.
4668
4669 Returns true if a breakpoint command resumed the inferior. In that
4670 case, it is the caller's responsibility to recall it again with the
4671 bpstat of the current thread. */
4672
4673 static int
4674 bpstat_do_actions_1 (bpstat *bsp)
4675 {
4676 bpstat bs;
4677 struct cleanup *old_chain;
4678 int again = 0;
4679
4680 /* Avoid endless recursion if a `source' command is contained
4681 in bs->commands. */
4682 if (executing_breakpoint_commands)
4683 return 0;
4684
4685 executing_breakpoint_commands = 1;
4686 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4687
4688 scoped_restore preventer = prevent_dont_repeat ();
4689
4690 /* This pointer will iterate over the list of bpstat's. */
4691 bs = *bsp;
4692
4693 breakpoint_proceeded = 0;
4694 for (; bs != NULL; bs = bs->next)
4695 {
4696 struct counted_command_line *ccmd;
4697 struct command_line *cmd;
4698 struct cleanup *this_cmd_tree_chain;
4699
4700 /* Take ownership of the BSP's command tree, if it has one.
4701
4702 The command tree could legitimately contain commands like
4703 'step' and 'next', which call clear_proceed_status, which
4704 frees stop_bpstat's command tree. To make sure this doesn't
4705 free the tree we're executing out from under us, we need to
4706 take ownership of the tree ourselves. Since a given bpstat's
4707 commands are only executed once, we don't need to copy it; we
4708 can clear the pointer in the bpstat, and make sure we free
4709 the tree when we're done. */
4710 ccmd = bs->commands;
4711 bs->commands = NULL;
4712 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4713 cmd = ccmd ? ccmd->commands : NULL;
4714 if (command_line_is_silent (cmd))
4715 {
4716 /* The action has been already done by bpstat_stop_status. */
4717 cmd = cmd->next;
4718 }
4719
4720 while (cmd != NULL)
4721 {
4722 execute_control_command (cmd);
4723
4724 if (breakpoint_proceeded)
4725 break;
4726 else
4727 cmd = cmd->next;
4728 }
4729
4730 /* We can free this command tree now. */
4731 do_cleanups (this_cmd_tree_chain);
4732
4733 if (breakpoint_proceeded)
4734 {
4735 if (current_ui->async)
4736 /* If we are in async mode, then the target might be still
4737 running, not stopped at any breakpoint, so nothing for
4738 us to do here -- just return to the event loop. */
4739 ;
4740 else
4741 /* In sync mode, when execute_control_command returns
4742 we're already standing on the next breakpoint.
4743 Breakpoint commands for that stop were not run, since
4744 execute_command does not run breakpoint commands --
4745 only command_line_handler does, but that one is not
4746 involved in execution of breakpoint commands. So, we
4747 can now execute breakpoint commands. It should be
4748 noted that making execute_command do bpstat actions is
4749 not an option -- in this case we'll have recursive
4750 invocation of bpstat for each breakpoint with a
4751 command, and can easily blow up GDB stack. Instead, we
4752 return true, which will trigger the caller to recall us
4753 with the new stop_bpstat. */
4754 again = 1;
4755 break;
4756 }
4757 }
4758 do_cleanups (old_chain);
4759 return again;
4760 }
4761
4762 void
4763 bpstat_do_actions (void)
4764 {
4765 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4766
4767 /* Do any commands attached to breakpoint we are stopped at. */
4768 while (!ptid_equal (inferior_ptid, null_ptid)
4769 && target_has_execution
4770 && !is_exited (inferior_ptid)
4771 && !is_executing (inferior_ptid))
4772 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4773 and only return when it is stopped at the next breakpoint, we
4774 keep doing breakpoint actions until it returns false to
4775 indicate the inferior was not resumed. */
4776 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4777 break;
4778
4779 discard_cleanups (cleanup_if_error);
4780 }
4781
4782 /* Print out the (old or new) value associated with a watchpoint. */
4783
4784 static void
4785 watchpoint_value_print (struct value *val, struct ui_file *stream)
4786 {
4787 if (val == NULL)
4788 fprintf_unfiltered (stream, _("<unreadable>"));
4789 else
4790 {
4791 struct value_print_options opts;
4792 get_user_print_options (&opts);
4793 value_print (val, stream, &opts);
4794 }
4795 }
4796
4797 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4798 debugging multiple threads. */
4799
4800 void
4801 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4802 {
4803 if (uiout->is_mi_like_p ())
4804 return;
4805
4806 uiout->text ("\n");
4807
4808 if (show_thread_that_caused_stop ())
4809 {
4810 const char *name;
4811 struct thread_info *thr = inferior_thread ();
4812
4813 uiout->text ("Thread ");
4814 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4815
4816 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4817 if (name != NULL)
4818 {
4819 uiout->text (" \"");
4820 uiout->field_fmt ("name", "%s", name);
4821 uiout->text ("\"");
4822 }
4823
4824 uiout->text (" hit ");
4825 }
4826 }
4827
4828 /* Generic routine for printing messages indicating why we
4829 stopped. The behavior of this function depends on the value
4830 'print_it' in the bpstat structure. Under some circumstances we
4831 may decide not to print anything here and delegate the task to
4832 normal_stop(). */
4833
4834 static enum print_stop_action
4835 print_bp_stop_message (bpstat bs)
4836 {
4837 switch (bs->print_it)
4838 {
4839 case print_it_noop:
4840 /* Nothing should be printed for this bpstat entry. */
4841 return PRINT_UNKNOWN;
4842 break;
4843
4844 case print_it_done:
4845 /* We still want to print the frame, but we already printed the
4846 relevant messages. */
4847 return PRINT_SRC_AND_LOC;
4848 break;
4849
4850 case print_it_normal:
4851 {
4852 struct breakpoint *b = bs->breakpoint_at;
4853
4854 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4855 which has since been deleted. */
4856 if (b == NULL)
4857 return PRINT_UNKNOWN;
4858
4859 /* Normal case. Call the breakpoint's print_it method. */
4860 return b->ops->print_it (bs);
4861 }
4862 break;
4863
4864 default:
4865 internal_error (__FILE__, __LINE__,
4866 _("print_bp_stop_message: unrecognized enum value"));
4867 break;
4868 }
4869 }
4870
4871 /* A helper function that prints a shared library stopped event. */
4872
4873 static void
4874 print_solib_event (int is_catchpoint)
4875 {
4876 int any_deleted
4877 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4878 int any_added
4879 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4880
4881 if (!is_catchpoint)
4882 {
4883 if (any_added || any_deleted)
4884 current_uiout->text (_("Stopped due to shared library event:\n"));
4885 else
4886 current_uiout->text (_("Stopped due to shared library event (no "
4887 "libraries added or removed)\n"));
4888 }
4889
4890 if (current_uiout->is_mi_like_p ())
4891 current_uiout->field_string ("reason",
4892 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4893
4894 if (any_deleted)
4895 {
4896 char *name;
4897 int ix;
4898
4899 current_uiout->text (_(" Inferior unloaded "));
4900 ui_out_emit_list list_emitter (current_uiout, "removed");
4901 for (ix = 0;
4902 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4903 ix, name);
4904 ++ix)
4905 {
4906 if (ix > 0)
4907 current_uiout->text (" ");
4908 current_uiout->field_string ("library", name);
4909 current_uiout->text ("\n");
4910 }
4911 }
4912
4913 if (any_added)
4914 {
4915 struct so_list *iter;
4916 int ix;
4917
4918 current_uiout->text (_(" Inferior loaded "));
4919 ui_out_emit_list list_emitter (current_uiout, "added");
4920 for (ix = 0;
4921 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4922 ix, iter);
4923 ++ix)
4924 {
4925 if (ix > 0)
4926 current_uiout->text (" ");
4927 current_uiout->field_string ("library", iter->so_name);
4928 current_uiout->text ("\n");
4929 }
4930 }
4931 }
4932
4933 /* Print a message indicating what happened. This is called from
4934 normal_stop(). The input to this routine is the head of the bpstat
4935 list - a list of the eventpoints that caused this stop. KIND is
4936 the target_waitkind for the stopping event. This
4937 routine calls the generic print routine for printing a message
4938 about reasons for stopping. This will print (for example) the
4939 "Breakpoint n," part of the output. The return value of this
4940 routine is one of:
4941
4942 PRINT_UNKNOWN: Means we printed nothing.
4943 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4944 code to print the location. An example is
4945 "Breakpoint 1, " which should be followed by
4946 the location.
4947 PRINT_SRC_ONLY: Means we printed something, but there is no need
4948 to also print the location part of the message.
4949 An example is the catch/throw messages, which
4950 don't require a location appended to the end.
4951 PRINT_NOTHING: We have done some printing and we don't need any
4952 further info to be printed. */
4953
4954 enum print_stop_action
4955 bpstat_print (bpstat bs, int kind)
4956 {
4957 enum print_stop_action val;
4958
4959 /* Maybe another breakpoint in the chain caused us to stop.
4960 (Currently all watchpoints go on the bpstat whether hit or not.
4961 That probably could (should) be changed, provided care is taken
4962 with respect to bpstat_explains_signal). */
4963 for (; bs; bs = bs->next)
4964 {
4965 val = print_bp_stop_message (bs);
4966 if (val == PRINT_SRC_ONLY
4967 || val == PRINT_SRC_AND_LOC
4968 || val == PRINT_NOTHING)
4969 return val;
4970 }
4971
4972 /* If we had hit a shared library event breakpoint,
4973 print_bp_stop_message would print out this message. If we hit an
4974 OS-level shared library event, do the same thing. */
4975 if (kind == TARGET_WAITKIND_LOADED)
4976 {
4977 print_solib_event (0);
4978 return PRINT_NOTHING;
4979 }
4980
4981 /* We reached the end of the chain, or we got a null BS to start
4982 with and nothing was printed. */
4983 return PRINT_UNKNOWN;
4984 }
4985
4986 /* Evaluate the expression EXP and return 1 if value is zero.
4987 This returns the inverse of the condition because it is called
4988 from catch_errors which returns 0 if an exception happened, and if an
4989 exception happens we want execution to stop.
4990 The argument is a "struct expression *" that has been cast to a
4991 "void *" to make it pass through catch_errors. */
4992
4993 static int
4994 breakpoint_cond_eval (void *exp)
4995 {
4996 struct value *mark = value_mark ();
4997 int i = !value_true (evaluate_expression ((struct expression *) exp));
4998
4999 value_free_to_mark (mark);
5000 return i;
5001 }
5002
5003 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5004
5005 static bpstat
5006 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5007 {
5008 bpstat bs;
5009
5010 bs = (bpstat) xmalloc (sizeof (*bs));
5011 bs->next = NULL;
5012 **bs_link_pointer = bs;
5013 *bs_link_pointer = &bs->next;
5014 bs->breakpoint_at = bl->owner;
5015 bs->bp_location_at = bl;
5016 incref_bp_location (bl);
5017 /* If the condition is false, etc., don't do the commands. */
5018 bs->commands = NULL;
5019 bs->old_val = NULL;
5020 bs->print_it = print_it_normal;
5021 return bs;
5022 }
5023 \f
5024 /* The target has stopped with waitstatus WS. Check if any hardware
5025 watchpoints have triggered, according to the target. */
5026
5027 int
5028 watchpoints_triggered (struct target_waitstatus *ws)
5029 {
5030 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5031 CORE_ADDR addr;
5032 struct breakpoint *b;
5033
5034 if (!stopped_by_watchpoint)
5035 {
5036 /* We were not stopped by a watchpoint. Mark all watchpoints
5037 as not triggered. */
5038 ALL_BREAKPOINTS (b)
5039 if (is_hardware_watchpoint (b))
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042
5043 w->watchpoint_triggered = watch_triggered_no;
5044 }
5045
5046 return 0;
5047 }
5048
5049 if (!target_stopped_data_address (&current_target, &addr))
5050 {
5051 /* We were stopped by a watchpoint, but we don't know where.
5052 Mark all watchpoints as unknown. */
5053 ALL_BREAKPOINTS (b)
5054 if (is_hardware_watchpoint (b))
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_unknown;
5059 }
5060
5061 return 1;
5062 }
5063
5064 /* The target could report the data address. Mark watchpoints
5065 affected by this data address as triggered, and all others as not
5066 triggered. */
5067
5068 ALL_BREAKPOINTS (b)
5069 if (is_hardware_watchpoint (b))
5070 {
5071 struct watchpoint *w = (struct watchpoint *) b;
5072 struct bp_location *loc;
5073
5074 w->watchpoint_triggered = watch_triggered_no;
5075 for (loc = b->loc; loc; loc = loc->next)
5076 {
5077 if (is_masked_watchpoint (b))
5078 {
5079 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5080 CORE_ADDR start = loc->address & w->hw_wp_mask;
5081
5082 if (newaddr == start)
5083 {
5084 w->watchpoint_triggered = watch_triggered_yes;
5085 break;
5086 }
5087 }
5088 /* Exact match not required. Within range is sufficient. */
5089 else if (target_watchpoint_addr_within_range (&current_target,
5090 addr, loc->address,
5091 loc->length))
5092 {
5093 w->watchpoint_triggered = watch_triggered_yes;
5094 break;
5095 }
5096 }
5097 }
5098
5099 return 1;
5100 }
5101
5102 /* Possible return values for watchpoint_check (this can't be an enum
5103 because of check_errors). */
5104 /* The watchpoint has been deleted. */
5105 #define WP_DELETED 1
5106 /* The value has changed. */
5107 #define WP_VALUE_CHANGED 2
5108 /* The value has not changed. */
5109 #define WP_VALUE_NOT_CHANGED 3
5110 /* Ignore this watchpoint, no matter if the value changed or not. */
5111 #define WP_IGNORE 4
5112
5113 #define BP_TEMPFLAG 1
5114 #define BP_HARDWAREFLAG 2
5115
5116 /* Evaluate watchpoint condition expression and check if its value
5117 changed.
5118
5119 P should be a pointer to struct bpstat, but is defined as a void *
5120 in order for this function to be usable with catch_errors. */
5121
5122 static int
5123 watchpoint_check (void *p)
5124 {
5125 bpstat bs = (bpstat) p;
5126 struct watchpoint *b;
5127 struct frame_info *fr;
5128 int within_current_scope;
5129
5130 /* BS is built from an existing struct breakpoint. */
5131 gdb_assert (bs->breakpoint_at != NULL);
5132 b = (struct watchpoint *) bs->breakpoint_at;
5133
5134 /* If this is a local watchpoint, we only want to check if the
5135 watchpoint frame is in scope if the current thread is the thread
5136 that was used to create the watchpoint. */
5137 if (!watchpoint_in_thread_scope (b))
5138 return WP_IGNORE;
5139
5140 if (b->exp_valid_block == NULL)
5141 within_current_scope = 1;
5142 else
5143 {
5144 struct frame_info *frame = get_current_frame ();
5145 struct gdbarch *frame_arch = get_frame_arch (frame);
5146 CORE_ADDR frame_pc = get_frame_pc (frame);
5147
5148 /* stack_frame_destroyed_p() returns a non-zero value if we're
5149 still in the function but the stack frame has already been
5150 invalidated. Since we can't rely on the values of local
5151 variables after the stack has been destroyed, we are treating
5152 the watchpoint in that state as `not changed' without further
5153 checking. Don't mark watchpoints as changed if the current
5154 frame is in an epilogue - even if they are in some other
5155 frame, our view of the stack is likely to be wrong and
5156 frame_find_by_id could error out. */
5157 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5158 return WP_IGNORE;
5159
5160 fr = frame_find_by_id (b->watchpoint_frame);
5161 within_current_scope = (fr != NULL);
5162
5163 /* If we've gotten confused in the unwinder, we might have
5164 returned a frame that can't describe this variable. */
5165 if (within_current_scope)
5166 {
5167 struct symbol *function;
5168
5169 function = get_frame_function (fr);
5170 if (function == NULL
5171 || !contained_in (b->exp_valid_block,
5172 SYMBOL_BLOCK_VALUE (function)))
5173 within_current_scope = 0;
5174 }
5175
5176 if (within_current_scope)
5177 /* If we end up stopping, the current frame will get selected
5178 in normal_stop. So this call to select_frame won't affect
5179 the user. */
5180 select_frame (fr);
5181 }
5182
5183 if (within_current_scope)
5184 {
5185 /* We use value_{,free_to_}mark because it could be a *long*
5186 time before we return to the command level and call
5187 free_all_values. We can't call free_all_values because we
5188 might be in the middle of evaluating a function call. */
5189
5190 int pc = 0;
5191 struct value *mark;
5192 struct value *new_val;
5193
5194 if (is_masked_watchpoint (b))
5195 /* Since we don't know the exact trigger address (from
5196 stopped_data_address), just tell the user we've triggered
5197 a mask watchpoint. */
5198 return WP_VALUE_CHANGED;
5199
5200 mark = value_mark ();
5201 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5202
5203 if (b->val_bitsize != 0)
5204 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5205
5206 /* We use value_equal_contents instead of value_equal because
5207 the latter coerces an array to a pointer, thus comparing just
5208 the address of the array instead of its contents. This is
5209 not what we want. */
5210 if ((b->val != NULL) != (new_val != NULL)
5211 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5212 {
5213 if (new_val != NULL)
5214 {
5215 release_value (new_val);
5216 value_free_to_mark (mark);
5217 }
5218 bs->old_val = b->val;
5219 b->val = new_val;
5220 b->val_valid = 1;
5221 return WP_VALUE_CHANGED;
5222 }
5223 else
5224 {
5225 /* Nothing changed. */
5226 value_free_to_mark (mark);
5227 return WP_VALUE_NOT_CHANGED;
5228 }
5229 }
5230 else
5231 {
5232 /* This seems like the only logical thing to do because
5233 if we temporarily ignored the watchpoint, then when
5234 we reenter the block in which it is valid it contains
5235 garbage (in the case of a function, it may have two
5236 garbage values, one before and one after the prologue).
5237 So we can't even detect the first assignment to it and
5238 watch after that (since the garbage may or may not equal
5239 the first value assigned). */
5240 /* We print all the stop information in
5241 breakpoint_ops->print_it, but in this case, by the time we
5242 call breakpoint_ops->print_it this bp will be deleted
5243 already. So we have no choice but print the information
5244 here. */
5245
5246 SWITCH_THRU_ALL_UIS ()
5247 {
5248 struct ui_out *uiout = current_uiout;
5249
5250 if (uiout->is_mi_like_p ())
5251 uiout->field_string
5252 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5253 uiout->text ("\nWatchpoint ");
5254 uiout->field_int ("wpnum", b->number);
5255 uiout->text (" deleted because the program has left the block in\n"
5256 "which its expression is valid.\n");
5257 }
5258
5259 /* Make sure the watchpoint's commands aren't executed. */
5260 decref_counted_command_line (&b->commands);
5261 watchpoint_del_at_next_stop (b);
5262
5263 return WP_DELETED;
5264 }
5265 }
5266
5267 /* Return true if it looks like target has stopped due to hitting
5268 breakpoint location BL. This function does not check if we should
5269 stop, only if BL explains the stop. */
5270
5271 static int
5272 bpstat_check_location (const struct bp_location *bl,
5273 struct address_space *aspace, CORE_ADDR bp_addr,
5274 const struct target_waitstatus *ws)
5275 {
5276 struct breakpoint *b = bl->owner;
5277
5278 /* BL is from an existing breakpoint. */
5279 gdb_assert (b != NULL);
5280
5281 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5282 }
5283
5284 /* Determine if the watched values have actually changed, and we
5285 should stop. If not, set BS->stop to 0. */
5286
5287 static void
5288 bpstat_check_watchpoint (bpstat bs)
5289 {
5290 const struct bp_location *bl;
5291 struct watchpoint *b;
5292
5293 /* BS is built for existing struct breakpoint. */
5294 bl = bs->bp_location_at;
5295 gdb_assert (bl != NULL);
5296 b = (struct watchpoint *) bs->breakpoint_at;
5297 gdb_assert (b != NULL);
5298
5299 {
5300 int must_check_value = 0;
5301
5302 if (b->type == bp_watchpoint)
5303 /* For a software watchpoint, we must always check the
5304 watched value. */
5305 must_check_value = 1;
5306 else if (b->watchpoint_triggered == watch_triggered_yes)
5307 /* We have a hardware watchpoint (read, write, or access)
5308 and the target earlier reported an address watched by
5309 this watchpoint. */
5310 must_check_value = 1;
5311 else if (b->watchpoint_triggered == watch_triggered_unknown
5312 && b->type == bp_hardware_watchpoint)
5313 /* We were stopped by a hardware watchpoint, but the target could
5314 not report the data address. We must check the watchpoint's
5315 value. Access and read watchpoints are out of luck; without
5316 a data address, we can't figure it out. */
5317 must_check_value = 1;
5318
5319 if (must_check_value)
5320 {
5321 char *message
5322 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5323 b->number);
5324 struct cleanup *cleanups = make_cleanup (xfree, message);
5325 int e = catch_errors (watchpoint_check, bs, message,
5326 RETURN_MASK_ALL);
5327 do_cleanups (cleanups);
5328 switch (e)
5329 {
5330 case WP_DELETED:
5331 /* We've already printed what needs to be printed. */
5332 bs->print_it = print_it_done;
5333 /* Stop. */
5334 break;
5335 case WP_IGNORE:
5336 bs->print_it = print_it_noop;
5337 bs->stop = 0;
5338 break;
5339 case WP_VALUE_CHANGED:
5340 if (b->type == bp_read_watchpoint)
5341 {
5342 /* There are two cases to consider here:
5343
5344 1. We're watching the triggered memory for reads.
5345 In that case, trust the target, and always report
5346 the watchpoint hit to the user. Even though
5347 reads don't cause value changes, the value may
5348 have changed since the last time it was read, and
5349 since we're not trapping writes, we will not see
5350 those, and as such we should ignore our notion of
5351 old value.
5352
5353 2. We're watching the triggered memory for both
5354 reads and writes. There are two ways this may
5355 happen:
5356
5357 2.1. This is a target that can't break on data
5358 reads only, but can break on accesses (reads or
5359 writes), such as e.g., x86. We detect this case
5360 at the time we try to insert read watchpoints.
5361
5362 2.2. Otherwise, the target supports read
5363 watchpoints, but, the user set an access or write
5364 watchpoint watching the same memory as this read
5365 watchpoint.
5366
5367 If we're watching memory writes as well as reads,
5368 ignore watchpoint hits when we find that the
5369 value hasn't changed, as reads don't cause
5370 changes. This still gives false positives when
5371 the program writes the same value to memory as
5372 what there was already in memory (we will confuse
5373 it for a read), but it's much better than
5374 nothing. */
5375
5376 int other_write_watchpoint = 0;
5377
5378 if (bl->watchpoint_type == hw_read)
5379 {
5380 struct breakpoint *other_b;
5381
5382 ALL_BREAKPOINTS (other_b)
5383 if (other_b->type == bp_hardware_watchpoint
5384 || other_b->type == bp_access_watchpoint)
5385 {
5386 struct watchpoint *other_w =
5387 (struct watchpoint *) other_b;
5388
5389 if (other_w->watchpoint_triggered
5390 == watch_triggered_yes)
5391 {
5392 other_write_watchpoint = 1;
5393 break;
5394 }
5395 }
5396 }
5397
5398 if (other_write_watchpoint
5399 || bl->watchpoint_type == hw_access)
5400 {
5401 /* We're watching the same memory for writes,
5402 and the value changed since the last time we
5403 updated it, so this trap must be for a write.
5404 Ignore it. */
5405 bs->print_it = print_it_noop;
5406 bs->stop = 0;
5407 }
5408 }
5409 break;
5410 case WP_VALUE_NOT_CHANGED:
5411 if (b->type == bp_hardware_watchpoint
5412 || b->type == bp_watchpoint)
5413 {
5414 /* Don't stop: write watchpoints shouldn't fire if
5415 the value hasn't changed. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
5419 /* Stop. */
5420 break;
5421 default:
5422 /* Can't happen. */
5423 case 0:
5424 /* Error from catch_errors. */
5425 {
5426 SWITCH_THRU_ALL_UIS ()
5427 {
5428 printf_filtered (_("Watchpoint %d deleted.\n"),
5429 b->number);
5430 }
5431 watchpoint_del_at_next_stop (b);
5432 /* We've already printed what needs to be printed. */
5433 bs->print_it = print_it_done;
5434 }
5435 break;
5436 }
5437 }
5438 else /* must_check_value == 0 */
5439 {
5440 /* This is a case where some watchpoint(s) triggered, but
5441 not at the address of this watchpoint, or else no
5442 watchpoint triggered after all. So don't print
5443 anything for this watchpoint. */
5444 bs->print_it = print_it_noop;
5445 bs->stop = 0;
5446 }
5447 }
5448 }
5449
5450 /* For breakpoints that are currently marked as telling gdb to stop,
5451 check conditions (condition proper, frame, thread and ignore count)
5452 of breakpoint referred to by BS. If we should not stop for this
5453 breakpoint, set BS->stop to 0. */
5454
5455 static void
5456 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5457 {
5458 const struct bp_location *bl;
5459 struct breakpoint *b;
5460 int value_is_zero = 0;
5461 struct expression *cond;
5462
5463 gdb_assert (bs->stop);
5464
5465 /* BS is built for existing struct breakpoint. */
5466 bl = bs->bp_location_at;
5467 gdb_assert (bl != NULL);
5468 b = bs->breakpoint_at;
5469 gdb_assert (b != NULL);
5470
5471 /* Even if the target evaluated the condition on its end and notified GDB, we
5472 need to do so again since GDB does not know if we stopped due to a
5473 breakpoint or a single step breakpoint. */
5474
5475 if (frame_id_p (b->frame_id)
5476 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5477 {
5478 bs->stop = 0;
5479 return;
5480 }
5481
5482 /* If this is a thread/task-specific breakpoint, don't waste cpu
5483 evaluating the condition if this isn't the specified
5484 thread/task. */
5485 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5486 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5487
5488 {
5489 bs->stop = 0;
5490 return;
5491 }
5492
5493 /* Evaluate extension language breakpoints that have a "stop" method
5494 implemented. */
5495 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5496
5497 if (is_watchpoint (b))
5498 {
5499 struct watchpoint *w = (struct watchpoint *) b;
5500
5501 cond = w->cond_exp.get ();
5502 }
5503 else
5504 cond = bl->cond.get ();
5505
5506 if (cond && b->disposition != disp_del_at_next_stop)
5507 {
5508 int within_current_scope = 1;
5509 struct watchpoint * w;
5510
5511 /* We use value_mark and value_free_to_mark because it could
5512 be a long time before we return to the command level and
5513 call free_all_values. We can't call free_all_values
5514 because we might be in the middle of evaluating a
5515 function call. */
5516 struct value *mark = value_mark ();
5517
5518 if (is_watchpoint (b))
5519 w = (struct watchpoint *) b;
5520 else
5521 w = NULL;
5522
5523 /* Need to select the frame, with all that implies so that
5524 the conditions will have the right context. Because we
5525 use the frame, we will not see an inlined function's
5526 variables when we arrive at a breakpoint at the start
5527 of the inlined function; the current frame will be the
5528 call site. */
5529 if (w == NULL || w->cond_exp_valid_block == NULL)
5530 select_frame (get_current_frame ());
5531 else
5532 {
5533 struct frame_info *frame;
5534
5535 /* For local watchpoint expressions, which particular
5536 instance of a local is being watched matters, so we
5537 keep track of the frame to evaluate the expression
5538 in. To evaluate the condition however, it doesn't
5539 really matter which instantiation of the function
5540 where the condition makes sense triggers the
5541 watchpoint. This allows an expression like "watch
5542 global if q > 10" set in `func', catch writes to
5543 global on all threads that call `func', or catch
5544 writes on all recursive calls of `func' by a single
5545 thread. We simply always evaluate the condition in
5546 the innermost frame that's executing where it makes
5547 sense to evaluate the condition. It seems
5548 intuitive. */
5549 frame = block_innermost_frame (w->cond_exp_valid_block);
5550 if (frame != NULL)
5551 select_frame (frame);
5552 else
5553 within_current_scope = 0;
5554 }
5555 if (within_current_scope)
5556 value_is_zero
5557 = catch_errors (breakpoint_cond_eval, cond,
5558 "Error in testing breakpoint condition:\n",
5559 RETURN_MASK_ALL);
5560 else
5561 {
5562 warning (_("Watchpoint condition cannot be tested "
5563 "in the current scope"));
5564 /* If we failed to set the right context for this
5565 watchpoint, unconditionally report it. */
5566 value_is_zero = 0;
5567 }
5568 /* FIXME-someday, should give breakpoint #. */
5569 value_free_to_mark (mark);
5570 }
5571
5572 if (cond && value_is_zero)
5573 {
5574 bs->stop = 0;
5575 }
5576 else if (b->ignore_count > 0)
5577 {
5578 b->ignore_count--;
5579 bs->stop = 0;
5580 /* Increase the hit count even though we don't stop. */
5581 ++(b->hit_count);
5582 observer_notify_breakpoint_modified (b);
5583 }
5584 }
5585
5586 /* Returns true if we need to track moribund locations of LOC's type
5587 on the current target. */
5588
5589 static int
5590 need_moribund_for_location_type (struct bp_location *loc)
5591 {
5592 return ((loc->loc_type == bp_loc_software_breakpoint
5593 && !target_supports_stopped_by_sw_breakpoint ())
5594 || (loc->loc_type == bp_loc_hardware_breakpoint
5595 && !target_supports_stopped_by_hw_breakpoint ()));
5596 }
5597
5598
5599 /* Get a bpstat associated with having just stopped at address
5600 BP_ADDR in thread PTID.
5601
5602 Determine whether we stopped at a breakpoint, etc, or whether we
5603 don't understand this stop. Result is a chain of bpstat's such
5604 that:
5605
5606 if we don't understand the stop, the result is a null pointer.
5607
5608 if we understand why we stopped, the result is not null.
5609
5610 Each element of the chain refers to a particular breakpoint or
5611 watchpoint at which we have stopped. (We may have stopped for
5612 several reasons concurrently.)
5613
5614 Each element of the chain has valid next, breakpoint_at,
5615 commands, FIXME??? fields. */
5616
5617 bpstat
5618 bpstat_stop_status (struct address_space *aspace,
5619 CORE_ADDR bp_addr, ptid_t ptid,
5620 const struct target_waitstatus *ws)
5621 {
5622 struct breakpoint *b = NULL;
5623 struct bp_location *bl;
5624 struct bp_location *loc;
5625 /* First item of allocated bpstat's. */
5626 bpstat bs_head = NULL, *bs_link = &bs_head;
5627 /* Pointer to the last thing in the chain currently. */
5628 bpstat bs;
5629 int ix;
5630 int need_remove_insert;
5631 int removed_any;
5632
5633 /* First, build the bpstat chain with locations that explain a
5634 target stop, while being careful to not set the target running,
5635 as that may invalidate locations (in particular watchpoint
5636 locations are recreated). Resuming will happen here with
5637 breakpoint conditions or watchpoint expressions that include
5638 inferior function calls. */
5639
5640 ALL_BREAKPOINTS (b)
5641 {
5642 if (!breakpoint_enabled (b))
5643 continue;
5644
5645 for (bl = b->loc; bl != NULL; bl = bl->next)
5646 {
5647 /* For hardware watchpoints, we look only at the first
5648 location. The watchpoint_check function will work on the
5649 entire expression, not the individual locations. For
5650 read watchpoints, the watchpoints_triggered function has
5651 checked all locations already. */
5652 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5653 break;
5654
5655 if (!bl->enabled || bl->shlib_disabled)
5656 continue;
5657
5658 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5659 continue;
5660
5661 /* Come here if it's a watchpoint, or if the break address
5662 matches. */
5663
5664 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5665 explain stop. */
5666
5667 /* Assume we stop. Should we find a watchpoint that is not
5668 actually triggered, or if the condition of the breakpoint
5669 evaluates as false, we'll reset 'stop' to 0. */
5670 bs->stop = 1;
5671 bs->print = 1;
5672
5673 /* If this is a scope breakpoint, mark the associated
5674 watchpoint as triggered so that we will handle the
5675 out-of-scope event. We'll get to the watchpoint next
5676 iteration. */
5677 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5678 {
5679 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5680
5681 w->watchpoint_triggered = watch_triggered_yes;
5682 }
5683 }
5684 }
5685
5686 /* Check if a moribund breakpoint explains the stop. */
5687 if (!target_supports_stopped_by_sw_breakpoint ()
5688 || !target_supports_stopped_by_hw_breakpoint ())
5689 {
5690 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5691 {
5692 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5693 && need_moribund_for_location_type (loc))
5694 {
5695 bs = bpstat_alloc (loc, &bs_link);
5696 /* For hits of moribund locations, we should just proceed. */
5697 bs->stop = 0;
5698 bs->print = 0;
5699 bs->print_it = print_it_noop;
5700 }
5701 }
5702 }
5703
5704 /* A bit of special processing for shlib breakpoints. We need to
5705 process solib loading here, so that the lists of loaded and
5706 unloaded libraries are correct before we handle "catch load" and
5707 "catch unload". */
5708 for (bs = bs_head; bs != NULL; bs = bs->next)
5709 {
5710 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5711 {
5712 handle_solib_event ();
5713 break;
5714 }
5715 }
5716
5717 /* Now go through the locations that caused the target to stop, and
5718 check whether we're interested in reporting this stop to higher
5719 layers, or whether we should resume the target transparently. */
5720
5721 removed_any = 0;
5722
5723 for (bs = bs_head; bs != NULL; bs = bs->next)
5724 {
5725 if (!bs->stop)
5726 continue;
5727
5728 b = bs->breakpoint_at;
5729 b->ops->check_status (bs);
5730 if (bs->stop)
5731 {
5732 bpstat_check_breakpoint_conditions (bs, ptid);
5733
5734 if (bs->stop)
5735 {
5736 ++(b->hit_count);
5737 observer_notify_breakpoint_modified (b);
5738
5739 /* We will stop here. */
5740 if (b->disposition == disp_disable)
5741 {
5742 --(b->enable_count);
5743 if (b->enable_count <= 0)
5744 b->enable_state = bp_disabled;
5745 removed_any = 1;
5746 }
5747 if (b->silent)
5748 bs->print = 0;
5749 bs->commands = b->commands;
5750 incref_counted_command_line (bs->commands);
5751 if (command_line_is_silent (bs->commands
5752 ? bs->commands->commands : NULL))
5753 bs->print = 0;
5754
5755 b->ops->after_condition_true (bs);
5756 }
5757
5758 }
5759
5760 /* Print nothing for this entry if we don't stop or don't
5761 print. */
5762 if (!bs->stop || !bs->print)
5763 bs->print_it = print_it_noop;
5764 }
5765
5766 /* If we aren't stopping, the value of some hardware watchpoint may
5767 not have changed, but the intermediate memory locations we are
5768 watching may have. Don't bother if we're stopping; this will get
5769 done later. */
5770 need_remove_insert = 0;
5771 if (! bpstat_causes_stop (bs_head))
5772 for (bs = bs_head; bs != NULL; bs = bs->next)
5773 if (!bs->stop
5774 && bs->breakpoint_at
5775 && is_hardware_watchpoint (bs->breakpoint_at))
5776 {
5777 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5778
5779 update_watchpoint (w, 0 /* don't reparse. */);
5780 need_remove_insert = 1;
5781 }
5782
5783 if (need_remove_insert)
5784 update_global_location_list (UGLL_MAY_INSERT);
5785 else if (removed_any)
5786 update_global_location_list (UGLL_DONT_INSERT);
5787
5788 return bs_head;
5789 }
5790
5791 static void
5792 handle_jit_event (void)
5793 {
5794 struct frame_info *frame;
5795 struct gdbarch *gdbarch;
5796
5797 if (debug_infrun)
5798 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5799
5800 /* Switch terminal for any messages produced by
5801 breakpoint_re_set. */
5802 target_terminal_ours_for_output ();
5803
5804 frame = get_current_frame ();
5805 gdbarch = get_frame_arch (frame);
5806
5807 jit_event_handler (gdbarch);
5808
5809 target_terminal_inferior ();
5810 }
5811
5812 /* Prepare WHAT final decision for infrun. */
5813
5814 /* Decide what infrun needs to do with this bpstat. */
5815
5816 struct bpstat_what
5817 bpstat_what (bpstat bs_head)
5818 {
5819 struct bpstat_what retval;
5820 bpstat bs;
5821
5822 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 retval.call_dummy = STOP_NONE;
5824 retval.is_longjmp = 0;
5825
5826 for (bs = bs_head; bs != NULL; bs = bs->next)
5827 {
5828 /* Extract this BS's action. After processing each BS, we check
5829 if its action overrides all we've seem so far. */
5830 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5831 enum bptype bptype;
5832
5833 if (bs->breakpoint_at == NULL)
5834 {
5835 /* I suspect this can happen if it was a momentary
5836 breakpoint which has since been deleted. */
5837 bptype = bp_none;
5838 }
5839 else
5840 bptype = bs->breakpoint_at->type;
5841
5842 switch (bptype)
5843 {
5844 case bp_none:
5845 break;
5846 case bp_breakpoint:
5847 case bp_hardware_breakpoint:
5848 case bp_single_step:
5849 case bp_until:
5850 case bp_finish:
5851 case bp_shlib_event:
5852 if (bs->stop)
5853 {
5854 if (bs->print)
5855 this_action = BPSTAT_WHAT_STOP_NOISY;
5856 else
5857 this_action = BPSTAT_WHAT_STOP_SILENT;
5858 }
5859 else
5860 this_action = BPSTAT_WHAT_SINGLE;
5861 break;
5862 case bp_watchpoint:
5863 case bp_hardware_watchpoint:
5864 case bp_read_watchpoint:
5865 case bp_access_watchpoint:
5866 if (bs->stop)
5867 {
5868 if (bs->print)
5869 this_action = BPSTAT_WHAT_STOP_NOISY;
5870 else
5871 this_action = BPSTAT_WHAT_STOP_SILENT;
5872 }
5873 else
5874 {
5875 /* There was a watchpoint, but we're not stopping.
5876 This requires no further action. */
5877 }
5878 break;
5879 case bp_longjmp:
5880 case bp_longjmp_call_dummy:
5881 case bp_exception:
5882 if (bs->stop)
5883 {
5884 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5885 retval.is_longjmp = bptype != bp_exception;
5886 }
5887 else
5888 this_action = BPSTAT_WHAT_SINGLE;
5889 break;
5890 case bp_longjmp_resume:
5891 case bp_exception_resume:
5892 if (bs->stop)
5893 {
5894 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5895 retval.is_longjmp = bptype == bp_longjmp_resume;
5896 }
5897 else
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 break;
5900 case bp_step_resume:
5901 if (bs->stop)
5902 this_action = BPSTAT_WHAT_STEP_RESUME;
5903 else
5904 {
5905 /* It is for the wrong frame. */
5906 this_action = BPSTAT_WHAT_SINGLE;
5907 }
5908 break;
5909 case bp_hp_step_resume:
5910 if (bs->stop)
5911 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5912 else
5913 {
5914 /* It is for the wrong frame. */
5915 this_action = BPSTAT_WHAT_SINGLE;
5916 }
5917 break;
5918 case bp_watchpoint_scope:
5919 case bp_thread_event:
5920 case bp_overlay_event:
5921 case bp_longjmp_master:
5922 case bp_std_terminate_master:
5923 case bp_exception_master:
5924 this_action = BPSTAT_WHAT_SINGLE;
5925 break;
5926 case bp_catchpoint:
5927 if (bs->stop)
5928 {
5929 if (bs->print)
5930 this_action = BPSTAT_WHAT_STOP_NOISY;
5931 else
5932 this_action = BPSTAT_WHAT_STOP_SILENT;
5933 }
5934 else
5935 {
5936 /* There was a catchpoint, but we're not stopping.
5937 This requires no further action. */
5938 }
5939 break;
5940 case bp_jit_event:
5941 this_action = BPSTAT_WHAT_SINGLE;
5942 break;
5943 case bp_call_dummy:
5944 /* Make sure the action is stop (silent or noisy),
5945 so infrun.c pops the dummy frame. */
5946 retval.call_dummy = STOP_STACK_DUMMY;
5947 this_action = BPSTAT_WHAT_STOP_SILENT;
5948 break;
5949 case bp_std_terminate:
5950 /* Make sure the action is stop (silent or noisy),
5951 so infrun.c pops the dummy frame. */
5952 retval.call_dummy = STOP_STD_TERMINATE;
5953 this_action = BPSTAT_WHAT_STOP_SILENT;
5954 break;
5955 case bp_tracepoint:
5956 case bp_fast_tracepoint:
5957 case bp_static_tracepoint:
5958 /* Tracepoint hits should not be reported back to GDB, and
5959 if one got through somehow, it should have been filtered
5960 out already. */
5961 internal_error (__FILE__, __LINE__,
5962 _("bpstat_what: tracepoint encountered"));
5963 break;
5964 case bp_gnu_ifunc_resolver:
5965 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5966 this_action = BPSTAT_WHAT_SINGLE;
5967 break;
5968 case bp_gnu_ifunc_resolver_return:
5969 /* The breakpoint will be removed, execution will restart from the
5970 PC of the former breakpoint. */
5971 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5972 break;
5973
5974 case bp_dprintf:
5975 if (bs->stop)
5976 this_action = BPSTAT_WHAT_STOP_SILENT;
5977 else
5978 this_action = BPSTAT_WHAT_SINGLE;
5979 break;
5980
5981 default:
5982 internal_error (__FILE__, __LINE__,
5983 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5984 }
5985
5986 retval.main_action = std::max (retval.main_action, this_action);
5987 }
5988
5989 return retval;
5990 }
5991
5992 void
5993 bpstat_run_callbacks (bpstat bs_head)
5994 {
5995 bpstat bs;
5996
5997 for (bs = bs_head; bs != NULL; bs = bs->next)
5998 {
5999 struct breakpoint *b = bs->breakpoint_at;
6000
6001 if (b == NULL)
6002 continue;
6003 switch (b->type)
6004 {
6005 case bp_jit_event:
6006 handle_jit_event ();
6007 break;
6008 case bp_gnu_ifunc_resolver:
6009 gnu_ifunc_resolver_stop (b);
6010 break;
6011 case bp_gnu_ifunc_resolver_return:
6012 gnu_ifunc_resolver_return_stop (b);
6013 break;
6014 }
6015 }
6016 }
6017
6018 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6019 without hardware support). This isn't related to a specific bpstat,
6020 just to things like whether watchpoints are set. */
6021
6022 int
6023 bpstat_should_step (void)
6024 {
6025 struct breakpoint *b;
6026
6027 ALL_BREAKPOINTS (b)
6028 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6029 return 1;
6030 return 0;
6031 }
6032
6033 int
6034 bpstat_causes_stop (bpstat bs)
6035 {
6036 for (; bs != NULL; bs = bs->next)
6037 if (bs->stop)
6038 return 1;
6039
6040 return 0;
6041 }
6042
6043 \f
6044
6045 /* Compute a string of spaces suitable to indent the next line
6046 so it starts at the position corresponding to the table column
6047 named COL_NAME in the currently active table of UIOUT. */
6048
6049 static char *
6050 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6051 {
6052 static char wrap_indent[80];
6053 int i, total_width, width, align;
6054 const char *text;
6055
6056 total_width = 0;
6057 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6058 {
6059 if (strcmp (text, col_name) == 0)
6060 {
6061 gdb_assert (total_width < sizeof wrap_indent);
6062 memset (wrap_indent, ' ', total_width);
6063 wrap_indent[total_width] = 0;
6064
6065 return wrap_indent;
6066 }
6067
6068 total_width += width + 1;
6069 }
6070
6071 return NULL;
6072 }
6073
6074 /* Determine if the locations of this breakpoint will have their conditions
6075 evaluated by the target, host or a mix of both. Returns the following:
6076
6077 "host": Host evals condition.
6078 "host or target": Host or Target evals condition.
6079 "target": Target evals condition.
6080 */
6081
6082 static const char *
6083 bp_condition_evaluator (struct breakpoint *b)
6084 {
6085 struct bp_location *bl;
6086 char host_evals = 0;
6087 char target_evals = 0;
6088
6089 if (!b)
6090 return NULL;
6091
6092 if (!is_breakpoint (b))
6093 return NULL;
6094
6095 if (gdb_evaluates_breakpoint_condition_p ()
6096 || !target_supports_evaluation_of_breakpoint_conditions ())
6097 return condition_evaluation_host;
6098
6099 for (bl = b->loc; bl; bl = bl->next)
6100 {
6101 if (bl->cond_bytecode)
6102 target_evals++;
6103 else
6104 host_evals++;
6105 }
6106
6107 if (host_evals && target_evals)
6108 return condition_evaluation_both;
6109 else if (target_evals)
6110 return condition_evaluation_target;
6111 else
6112 return condition_evaluation_host;
6113 }
6114
6115 /* Determine the breakpoint location's condition evaluator. This is
6116 similar to bp_condition_evaluator, but for locations. */
6117
6118 static const char *
6119 bp_location_condition_evaluator (struct bp_location *bl)
6120 {
6121 if (bl && !is_breakpoint (bl->owner))
6122 return NULL;
6123
6124 if (gdb_evaluates_breakpoint_condition_p ()
6125 || !target_supports_evaluation_of_breakpoint_conditions ())
6126 return condition_evaluation_host;
6127
6128 if (bl && bl->cond_bytecode)
6129 return condition_evaluation_target;
6130 else
6131 return condition_evaluation_host;
6132 }
6133
6134 /* Print the LOC location out of the list of B->LOC locations. */
6135
6136 static void
6137 print_breakpoint_location (struct breakpoint *b,
6138 struct bp_location *loc)
6139 {
6140 struct ui_out *uiout = current_uiout;
6141
6142 scoped_restore_current_program_space restore_pspace;
6143
6144 if (loc != NULL && loc->shlib_disabled)
6145 loc = NULL;
6146
6147 if (loc != NULL)
6148 set_current_program_space (loc->pspace);
6149
6150 if (b->display_canonical)
6151 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6152 else if (loc && loc->symtab)
6153 {
6154 struct symbol *sym
6155 = find_pc_sect_function (loc->address, loc->section);
6156 if (sym)
6157 {
6158 uiout->text ("in ");
6159 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6160 uiout->text (" ");
6161 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6162 uiout->text ("at ");
6163 }
6164 uiout->field_string ("file",
6165 symtab_to_filename_for_display (loc->symtab));
6166 uiout->text (":");
6167
6168 if (uiout->is_mi_like_p ())
6169 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6170
6171 uiout->field_int ("line", loc->line_number);
6172 }
6173 else if (loc)
6174 {
6175 string_file stb;
6176
6177 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6178 demangle, "");
6179 uiout->field_stream ("at", stb);
6180 }
6181 else
6182 {
6183 uiout->field_string ("pending",
6184 event_location_to_string (b->location.get ()));
6185 /* If extra_string is available, it could be holding a condition
6186 or dprintf arguments. In either case, make sure it is printed,
6187 too, but only for non-MI streams. */
6188 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6189 {
6190 if (b->type == bp_dprintf)
6191 uiout->text (",");
6192 else
6193 uiout->text (" ");
6194 uiout->text (b->extra_string);
6195 }
6196 }
6197
6198 if (loc && is_breakpoint (b)
6199 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6200 && bp_condition_evaluator (b) == condition_evaluation_both)
6201 {
6202 uiout->text (" (");
6203 uiout->field_string ("evaluated-by",
6204 bp_location_condition_evaluator (loc));
6205 uiout->text (")");
6206 }
6207 }
6208
6209 static const char *
6210 bptype_string (enum bptype type)
6211 {
6212 struct ep_type_description
6213 {
6214 enum bptype type;
6215 const char *description;
6216 };
6217 static struct ep_type_description bptypes[] =
6218 {
6219 {bp_none, "?deleted?"},
6220 {bp_breakpoint, "breakpoint"},
6221 {bp_hardware_breakpoint, "hw breakpoint"},
6222 {bp_single_step, "sw single-step"},
6223 {bp_until, "until"},
6224 {bp_finish, "finish"},
6225 {bp_watchpoint, "watchpoint"},
6226 {bp_hardware_watchpoint, "hw watchpoint"},
6227 {bp_read_watchpoint, "read watchpoint"},
6228 {bp_access_watchpoint, "acc watchpoint"},
6229 {bp_longjmp, "longjmp"},
6230 {bp_longjmp_resume, "longjmp resume"},
6231 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6232 {bp_exception, "exception"},
6233 {bp_exception_resume, "exception resume"},
6234 {bp_step_resume, "step resume"},
6235 {bp_hp_step_resume, "high-priority step resume"},
6236 {bp_watchpoint_scope, "watchpoint scope"},
6237 {bp_call_dummy, "call dummy"},
6238 {bp_std_terminate, "std::terminate"},
6239 {bp_shlib_event, "shlib events"},
6240 {bp_thread_event, "thread events"},
6241 {bp_overlay_event, "overlay events"},
6242 {bp_longjmp_master, "longjmp master"},
6243 {bp_std_terminate_master, "std::terminate master"},
6244 {bp_exception_master, "exception master"},
6245 {bp_catchpoint, "catchpoint"},
6246 {bp_tracepoint, "tracepoint"},
6247 {bp_fast_tracepoint, "fast tracepoint"},
6248 {bp_static_tracepoint, "static tracepoint"},
6249 {bp_dprintf, "dprintf"},
6250 {bp_jit_event, "jit events"},
6251 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6252 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6253 };
6254
6255 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6256 || ((int) type != bptypes[(int) type].type))
6257 internal_error (__FILE__, __LINE__,
6258 _("bptypes table does not describe type #%d."),
6259 (int) type);
6260
6261 return bptypes[(int) type].description;
6262 }
6263
6264 /* For MI, output a field named 'thread-groups' with a list as the value.
6265 For CLI, prefix the list with the string 'inf'. */
6266
6267 static void
6268 output_thread_groups (struct ui_out *uiout,
6269 const char *field_name,
6270 VEC(int) *inf_num,
6271 int mi_only)
6272 {
6273 int is_mi = uiout->is_mi_like_p ();
6274 int inf;
6275 int i;
6276
6277 /* For backward compatibility, don't display inferiors in CLI unless
6278 there are several. Always display them for MI. */
6279 if (!is_mi && mi_only)
6280 return;
6281
6282 ui_out_emit_list list_emitter (uiout, field_name);
6283
6284 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6285 {
6286 if (is_mi)
6287 {
6288 char mi_group[10];
6289
6290 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6291 uiout->field_string (NULL, mi_group);
6292 }
6293 else
6294 {
6295 if (i == 0)
6296 uiout->text (" inf ");
6297 else
6298 uiout->text (", ");
6299
6300 uiout->text (plongest (inf));
6301 }
6302 }
6303 }
6304
6305 /* Print B to gdb_stdout. */
6306
6307 static void
6308 print_one_breakpoint_location (struct breakpoint *b,
6309 struct bp_location *loc,
6310 int loc_number,
6311 struct bp_location **last_loc,
6312 int allflag)
6313 {
6314 struct command_line *l;
6315 static char bpenables[] = "nynny";
6316
6317 struct ui_out *uiout = current_uiout;
6318 int header_of_multiple = 0;
6319 int part_of_multiple = (loc != NULL);
6320 struct value_print_options opts;
6321
6322 get_user_print_options (&opts);
6323
6324 gdb_assert (!loc || loc_number != 0);
6325 /* See comment in print_one_breakpoint concerning treatment of
6326 breakpoints with single disabled location. */
6327 if (loc == NULL
6328 && (b->loc != NULL
6329 && (b->loc->next != NULL || !b->loc->enabled)))
6330 header_of_multiple = 1;
6331 if (loc == NULL)
6332 loc = b->loc;
6333
6334 annotate_record ();
6335
6336 /* 1 */
6337 annotate_field (0);
6338 if (part_of_multiple)
6339 {
6340 char *formatted;
6341 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6342 uiout->field_string ("number", formatted);
6343 xfree (formatted);
6344 }
6345 else
6346 {
6347 uiout->field_int ("number", b->number);
6348 }
6349
6350 /* 2 */
6351 annotate_field (1);
6352 if (part_of_multiple)
6353 uiout->field_skip ("type");
6354 else
6355 uiout->field_string ("type", bptype_string (b->type));
6356
6357 /* 3 */
6358 annotate_field (2);
6359 if (part_of_multiple)
6360 uiout->field_skip ("disp");
6361 else
6362 uiout->field_string ("disp", bpdisp_text (b->disposition));
6363
6364
6365 /* 4 */
6366 annotate_field (3);
6367 if (part_of_multiple)
6368 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6369 else
6370 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6371 uiout->spaces (2);
6372
6373
6374 /* 5 and 6 */
6375 if (b->ops != NULL && b->ops->print_one != NULL)
6376 {
6377 /* Although the print_one can possibly print all locations,
6378 calling it here is not likely to get any nice result. So,
6379 make sure there's just one location. */
6380 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6381 b->ops->print_one (b, last_loc);
6382 }
6383 else
6384 switch (b->type)
6385 {
6386 case bp_none:
6387 internal_error (__FILE__, __LINE__,
6388 _("print_one_breakpoint: bp_none encountered\n"));
6389 break;
6390
6391 case bp_watchpoint:
6392 case bp_hardware_watchpoint:
6393 case bp_read_watchpoint:
6394 case bp_access_watchpoint:
6395 {
6396 struct watchpoint *w = (struct watchpoint *) b;
6397
6398 /* Field 4, the address, is omitted (which makes the columns
6399 not line up too nicely with the headers, but the effect
6400 is relatively readable). */
6401 if (opts.addressprint)
6402 uiout->field_skip ("addr");
6403 annotate_field (5);
6404 uiout->field_string ("what", w->exp_string);
6405 }
6406 break;
6407
6408 case bp_breakpoint:
6409 case bp_hardware_breakpoint:
6410 case bp_single_step:
6411 case bp_until:
6412 case bp_finish:
6413 case bp_longjmp:
6414 case bp_longjmp_resume:
6415 case bp_longjmp_call_dummy:
6416 case bp_exception:
6417 case bp_exception_resume:
6418 case bp_step_resume:
6419 case bp_hp_step_resume:
6420 case bp_watchpoint_scope:
6421 case bp_call_dummy:
6422 case bp_std_terminate:
6423 case bp_shlib_event:
6424 case bp_thread_event:
6425 case bp_overlay_event:
6426 case bp_longjmp_master:
6427 case bp_std_terminate_master:
6428 case bp_exception_master:
6429 case bp_tracepoint:
6430 case bp_fast_tracepoint:
6431 case bp_static_tracepoint:
6432 case bp_dprintf:
6433 case bp_jit_event:
6434 case bp_gnu_ifunc_resolver:
6435 case bp_gnu_ifunc_resolver_return:
6436 if (opts.addressprint)
6437 {
6438 annotate_field (4);
6439 if (header_of_multiple)
6440 uiout->field_string ("addr", "<MULTIPLE>");
6441 else if (b->loc == NULL || loc->shlib_disabled)
6442 uiout->field_string ("addr", "<PENDING>");
6443 else
6444 uiout->field_core_addr ("addr",
6445 loc->gdbarch, loc->address);
6446 }
6447 annotate_field (5);
6448 if (!header_of_multiple)
6449 print_breakpoint_location (b, loc);
6450 if (b->loc)
6451 *last_loc = b->loc;
6452 break;
6453 }
6454
6455
6456 if (loc != NULL && !header_of_multiple)
6457 {
6458 struct inferior *inf;
6459 VEC(int) *inf_num = NULL;
6460 int mi_only = 1;
6461
6462 ALL_INFERIORS (inf)
6463 {
6464 if (inf->pspace == loc->pspace)
6465 VEC_safe_push (int, inf_num, inf->num);
6466 }
6467
6468 /* For backward compatibility, don't display inferiors in CLI unless
6469 there are several. Always display for MI. */
6470 if (allflag
6471 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6472 && (number_of_program_spaces () > 1
6473 || number_of_inferiors () > 1)
6474 /* LOC is for existing B, it cannot be in
6475 moribund_locations and thus having NULL OWNER. */
6476 && loc->owner->type != bp_catchpoint))
6477 mi_only = 0;
6478 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6479 VEC_free (int, inf_num);
6480 }
6481
6482 if (!part_of_multiple)
6483 {
6484 if (b->thread != -1)
6485 {
6486 /* FIXME: This seems to be redundant and lost here; see the
6487 "stop only in" line a little further down. */
6488 uiout->text (" thread ");
6489 uiout->field_int ("thread", b->thread);
6490 }
6491 else if (b->task != 0)
6492 {
6493 uiout->text (" task ");
6494 uiout->field_int ("task", b->task);
6495 }
6496 }
6497
6498 uiout->text ("\n");
6499
6500 if (!part_of_multiple)
6501 b->ops->print_one_detail (b, uiout);
6502
6503 if (part_of_multiple && frame_id_p (b->frame_id))
6504 {
6505 annotate_field (6);
6506 uiout->text ("\tstop only in stack frame at ");
6507 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6508 the frame ID. */
6509 uiout->field_core_addr ("frame",
6510 b->gdbarch, b->frame_id.stack_addr);
6511 uiout->text ("\n");
6512 }
6513
6514 if (!part_of_multiple && b->cond_string)
6515 {
6516 annotate_field (7);
6517 if (is_tracepoint (b))
6518 uiout->text ("\ttrace only if ");
6519 else
6520 uiout->text ("\tstop only if ");
6521 uiout->field_string ("cond", b->cond_string);
6522
6523 /* Print whether the target is doing the breakpoint's condition
6524 evaluation. If GDB is doing the evaluation, don't print anything. */
6525 if (is_breakpoint (b)
6526 && breakpoint_condition_evaluation_mode ()
6527 == condition_evaluation_target)
6528 {
6529 uiout->text (" (");
6530 uiout->field_string ("evaluated-by",
6531 bp_condition_evaluator (b));
6532 uiout->text (" evals)");
6533 }
6534 uiout->text ("\n");
6535 }
6536
6537 if (!part_of_multiple && b->thread != -1)
6538 {
6539 /* FIXME should make an annotation for this. */
6540 uiout->text ("\tstop only in thread ");
6541 if (uiout->is_mi_like_p ())
6542 uiout->field_int ("thread", b->thread);
6543 else
6544 {
6545 struct thread_info *thr = find_thread_global_id (b->thread);
6546
6547 uiout->field_string ("thread", print_thread_id (thr));
6548 }
6549 uiout->text ("\n");
6550 }
6551
6552 if (!part_of_multiple)
6553 {
6554 if (b->hit_count)
6555 {
6556 /* FIXME should make an annotation for this. */
6557 if (is_catchpoint (b))
6558 uiout->text ("\tcatchpoint");
6559 else if (is_tracepoint (b))
6560 uiout->text ("\ttracepoint");
6561 else
6562 uiout->text ("\tbreakpoint");
6563 uiout->text (" already hit ");
6564 uiout->field_int ("times", b->hit_count);
6565 if (b->hit_count == 1)
6566 uiout->text (" time\n");
6567 else
6568 uiout->text (" times\n");
6569 }
6570 else
6571 {
6572 /* Output the count also if it is zero, but only if this is mi. */
6573 if (uiout->is_mi_like_p ())
6574 uiout->field_int ("times", b->hit_count);
6575 }
6576 }
6577
6578 if (!part_of_multiple && b->ignore_count)
6579 {
6580 annotate_field (8);
6581 uiout->text ("\tignore next ");
6582 uiout->field_int ("ignore", b->ignore_count);
6583 uiout->text (" hits\n");
6584 }
6585
6586 /* Note that an enable count of 1 corresponds to "enable once"
6587 behavior, which is reported by the combination of enablement and
6588 disposition, so we don't need to mention it here. */
6589 if (!part_of_multiple && b->enable_count > 1)
6590 {
6591 annotate_field (8);
6592 uiout->text ("\tdisable after ");
6593 /* Tweak the wording to clarify that ignore and enable counts
6594 are distinct, and have additive effect. */
6595 if (b->ignore_count)
6596 uiout->text ("additional ");
6597 else
6598 uiout->text ("next ");
6599 uiout->field_int ("enable", b->enable_count);
6600 uiout->text (" hits\n");
6601 }
6602
6603 if (!part_of_multiple && is_tracepoint (b))
6604 {
6605 struct tracepoint *tp = (struct tracepoint *) b;
6606
6607 if (tp->traceframe_usage)
6608 {
6609 uiout->text ("\ttrace buffer usage ");
6610 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6611 uiout->text (" bytes\n");
6612 }
6613 }
6614
6615 l = b->commands ? b->commands->commands : NULL;
6616 if (!part_of_multiple && l)
6617 {
6618 annotate_field (9);
6619 ui_out_emit_tuple tuple_emitter (uiout, "script");
6620 print_command_lines (uiout, l, 4);
6621 }
6622
6623 if (is_tracepoint (b))
6624 {
6625 struct tracepoint *t = (struct tracepoint *) b;
6626
6627 if (!part_of_multiple && t->pass_count)
6628 {
6629 annotate_field (10);
6630 uiout->text ("\tpass count ");
6631 uiout->field_int ("pass", t->pass_count);
6632 uiout->text (" \n");
6633 }
6634
6635 /* Don't display it when tracepoint or tracepoint location is
6636 pending. */
6637 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6638 {
6639 annotate_field (11);
6640
6641 if (uiout->is_mi_like_p ())
6642 uiout->field_string ("installed",
6643 loc->inserted ? "y" : "n");
6644 else
6645 {
6646 if (loc->inserted)
6647 uiout->text ("\t");
6648 else
6649 uiout->text ("\tnot ");
6650 uiout->text ("installed on target\n");
6651 }
6652 }
6653 }
6654
6655 if (uiout->is_mi_like_p () && !part_of_multiple)
6656 {
6657 if (is_watchpoint (b))
6658 {
6659 struct watchpoint *w = (struct watchpoint *) b;
6660
6661 uiout->field_string ("original-location", w->exp_string);
6662 }
6663 else if (b->location != NULL
6664 && event_location_to_string (b->location.get ()) != NULL)
6665 uiout->field_string ("original-location",
6666 event_location_to_string (b->location.get ()));
6667 }
6668 }
6669
6670 static void
6671 print_one_breakpoint (struct breakpoint *b,
6672 struct bp_location **last_loc,
6673 int allflag)
6674 {
6675 struct ui_out *uiout = current_uiout;
6676
6677 {
6678 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6679
6680 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6681 }
6682
6683 /* If this breakpoint has custom print function,
6684 it's already printed. Otherwise, print individual
6685 locations, if any. */
6686 if (b->ops == NULL || b->ops->print_one == NULL)
6687 {
6688 /* If breakpoint has a single location that is disabled, we
6689 print it as if it had several locations, since otherwise it's
6690 hard to represent "breakpoint enabled, location disabled"
6691 situation.
6692
6693 Note that while hardware watchpoints have several locations
6694 internally, that's not a property exposed to user. */
6695 if (b->loc
6696 && !is_hardware_watchpoint (b)
6697 && (b->loc->next || !b->loc->enabled))
6698 {
6699 struct bp_location *loc;
6700 int n = 1;
6701
6702 for (loc = b->loc; loc; loc = loc->next, ++n)
6703 {
6704 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6705 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6706 }
6707 }
6708 }
6709 }
6710
6711 static int
6712 breakpoint_address_bits (struct breakpoint *b)
6713 {
6714 int print_address_bits = 0;
6715 struct bp_location *loc;
6716
6717 /* Software watchpoints that aren't watching memory don't have an
6718 address to print. */
6719 if (is_no_memory_software_watchpoint (b))
6720 return 0;
6721
6722 for (loc = b->loc; loc; loc = loc->next)
6723 {
6724 int addr_bit;
6725
6726 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6727 if (addr_bit > print_address_bits)
6728 print_address_bits = addr_bit;
6729 }
6730
6731 return print_address_bits;
6732 }
6733
6734 struct captured_breakpoint_query_args
6735 {
6736 int bnum;
6737 };
6738
6739 static int
6740 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6741 {
6742 struct captured_breakpoint_query_args *args
6743 = (struct captured_breakpoint_query_args *) data;
6744 struct breakpoint *b;
6745 struct bp_location *dummy_loc = NULL;
6746
6747 ALL_BREAKPOINTS (b)
6748 {
6749 if (args->bnum == b->number)
6750 {
6751 print_one_breakpoint (b, &dummy_loc, 0);
6752 return GDB_RC_OK;
6753 }
6754 }
6755 return GDB_RC_NONE;
6756 }
6757
6758 enum gdb_rc
6759 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6760 char **error_message)
6761 {
6762 struct captured_breakpoint_query_args args;
6763
6764 args.bnum = bnum;
6765 /* For the moment we don't trust print_one_breakpoint() to not throw
6766 an error. */
6767 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6768 error_message, RETURN_MASK_ALL) < 0)
6769 return GDB_RC_FAIL;
6770 else
6771 return GDB_RC_OK;
6772 }
6773
6774 /* Return true if this breakpoint was set by the user, false if it is
6775 internal or momentary. */
6776
6777 int
6778 user_breakpoint_p (struct breakpoint *b)
6779 {
6780 return b->number > 0;
6781 }
6782
6783 /* See breakpoint.h. */
6784
6785 int
6786 pending_breakpoint_p (struct breakpoint *b)
6787 {
6788 return b->loc == NULL;
6789 }
6790
6791 /* Print information on user settable breakpoint (watchpoint, etc)
6792 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6793 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6794 FILTER is non-NULL, call it on each breakpoint and only include the
6795 ones for which it returns non-zero. Return the total number of
6796 breakpoints listed. */
6797
6798 static int
6799 breakpoint_1 (char *args, int allflag,
6800 int (*filter) (const struct breakpoint *))
6801 {
6802 struct breakpoint *b;
6803 struct bp_location *last_loc = NULL;
6804 int nr_printable_breakpoints;
6805 struct value_print_options opts;
6806 int print_address_bits = 0;
6807 int print_type_col_width = 14;
6808 struct ui_out *uiout = current_uiout;
6809
6810 get_user_print_options (&opts);
6811
6812 /* Compute the number of rows in the table, as well as the size
6813 required for address fields. */
6814 nr_printable_breakpoints = 0;
6815 ALL_BREAKPOINTS (b)
6816 {
6817 /* If we have a filter, only list the breakpoints it accepts. */
6818 if (filter && !filter (b))
6819 continue;
6820
6821 /* If we have an "args" string, it is a list of breakpoints to
6822 accept. Skip the others. */
6823 if (args != NULL && *args != '\0')
6824 {
6825 if (allflag && parse_and_eval_long (args) != b->number)
6826 continue;
6827 if (!allflag && !number_is_in_list (args, b->number))
6828 continue;
6829 }
6830
6831 if (allflag || user_breakpoint_p (b))
6832 {
6833 int addr_bit, type_len;
6834
6835 addr_bit = breakpoint_address_bits (b);
6836 if (addr_bit > print_address_bits)
6837 print_address_bits = addr_bit;
6838
6839 type_len = strlen (bptype_string (b->type));
6840 if (type_len > print_type_col_width)
6841 print_type_col_width = type_len;
6842
6843 nr_printable_breakpoints++;
6844 }
6845 }
6846
6847 {
6848 ui_out_emit_table table_emitter (uiout,
6849 opts.addressprint ? 6 : 5,
6850 nr_printable_breakpoints,
6851 "BreakpointTable");
6852
6853 if (nr_printable_breakpoints > 0)
6854 annotate_breakpoints_headers ();
6855 if (nr_printable_breakpoints > 0)
6856 annotate_field (0);
6857 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6858 if (nr_printable_breakpoints > 0)
6859 annotate_field (1);
6860 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (2);
6863 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6864 if (nr_printable_breakpoints > 0)
6865 annotate_field (3);
6866 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6867 if (opts.addressprint)
6868 {
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (4);
6871 if (print_address_bits <= 32)
6872 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6873 else
6874 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6875 }
6876 if (nr_printable_breakpoints > 0)
6877 annotate_field (5);
6878 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6879 uiout->table_body ();
6880 if (nr_printable_breakpoints > 0)
6881 annotate_breakpoints_table ();
6882
6883 ALL_BREAKPOINTS (b)
6884 {
6885 QUIT;
6886 /* If we have a filter, only list the breakpoints it accepts. */
6887 if (filter && !filter (b))
6888 continue;
6889
6890 /* If we have an "args" string, it is a list of breakpoints to
6891 accept. Skip the others. */
6892
6893 if (args != NULL && *args != '\0')
6894 {
6895 if (allflag) /* maintenance info breakpoint */
6896 {
6897 if (parse_and_eval_long (args) != b->number)
6898 continue;
6899 }
6900 else /* all others */
6901 {
6902 if (!number_is_in_list (args, b->number))
6903 continue;
6904 }
6905 }
6906 /* We only print out user settable breakpoints unless the
6907 allflag is set. */
6908 if (allflag || user_breakpoint_p (b))
6909 print_one_breakpoint (b, &last_loc, allflag);
6910 }
6911 }
6912
6913 if (nr_printable_breakpoints == 0)
6914 {
6915 /* If there's a filter, let the caller decide how to report
6916 empty list. */
6917 if (!filter)
6918 {
6919 if (args == NULL || *args == '\0')
6920 uiout->message ("No breakpoints or watchpoints.\n");
6921 else
6922 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6923 args);
6924 }
6925 }
6926 else
6927 {
6928 if (last_loc && !server_command)
6929 set_next_address (last_loc->gdbarch, last_loc->address);
6930 }
6931
6932 /* FIXME? Should this be moved up so that it is only called when
6933 there have been breakpoints? */
6934 annotate_breakpoints_table_end ();
6935
6936 return nr_printable_breakpoints;
6937 }
6938
6939 /* Display the value of default-collect in a way that is generally
6940 compatible with the breakpoint list. */
6941
6942 static void
6943 default_collect_info (void)
6944 {
6945 struct ui_out *uiout = current_uiout;
6946
6947 /* If it has no value (which is frequently the case), say nothing; a
6948 message like "No default-collect." gets in user's face when it's
6949 not wanted. */
6950 if (!*default_collect)
6951 return;
6952
6953 /* The following phrase lines up nicely with per-tracepoint collect
6954 actions. */
6955 uiout->text ("default collect ");
6956 uiout->field_string ("default-collect", default_collect);
6957 uiout->text (" \n");
6958 }
6959
6960 static void
6961 breakpoints_info (char *args, int from_tty)
6962 {
6963 breakpoint_1 (args, 0, NULL);
6964
6965 default_collect_info ();
6966 }
6967
6968 static void
6969 watchpoints_info (char *args, int from_tty)
6970 {
6971 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6972 struct ui_out *uiout = current_uiout;
6973
6974 if (num_printed == 0)
6975 {
6976 if (args == NULL || *args == '\0')
6977 uiout->message ("No watchpoints.\n");
6978 else
6979 uiout->message ("No watchpoint matching '%s'.\n", args);
6980 }
6981 }
6982
6983 static void
6984 maintenance_info_breakpoints (char *args, int from_tty)
6985 {
6986 breakpoint_1 (args, 1, NULL);
6987
6988 default_collect_info ();
6989 }
6990
6991 static int
6992 breakpoint_has_pc (struct breakpoint *b,
6993 struct program_space *pspace,
6994 CORE_ADDR pc, struct obj_section *section)
6995 {
6996 struct bp_location *bl = b->loc;
6997
6998 for (; bl; bl = bl->next)
6999 {
7000 if (bl->pspace == pspace
7001 && bl->address == pc
7002 && (!overlay_debugging || bl->section == section))
7003 return 1;
7004 }
7005 return 0;
7006 }
7007
7008 /* Print a message describing any user-breakpoints set at PC. This
7009 concerns with logical breakpoints, so we match program spaces, not
7010 address spaces. */
7011
7012 static void
7013 describe_other_breakpoints (struct gdbarch *gdbarch,
7014 struct program_space *pspace, CORE_ADDR pc,
7015 struct obj_section *section, int thread)
7016 {
7017 int others = 0;
7018 struct breakpoint *b;
7019
7020 ALL_BREAKPOINTS (b)
7021 others += (user_breakpoint_p (b)
7022 && breakpoint_has_pc (b, pspace, pc, section));
7023 if (others > 0)
7024 {
7025 if (others == 1)
7026 printf_filtered (_("Note: breakpoint "));
7027 else /* if (others == ???) */
7028 printf_filtered (_("Note: breakpoints "));
7029 ALL_BREAKPOINTS (b)
7030 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7031 {
7032 others--;
7033 printf_filtered ("%d", b->number);
7034 if (b->thread == -1 && thread != -1)
7035 printf_filtered (" (all threads)");
7036 else if (b->thread != -1)
7037 printf_filtered (" (thread %d)", b->thread);
7038 printf_filtered ("%s%s ",
7039 ((b->enable_state == bp_disabled
7040 || b->enable_state == bp_call_disabled)
7041 ? " (disabled)"
7042 : ""),
7043 (others > 1) ? ","
7044 : ((others == 1) ? " and" : ""));
7045 }
7046 printf_filtered (_("also set at pc "));
7047 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7048 printf_filtered (".\n");
7049 }
7050 }
7051 \f
7052
7053 /* Return true iff it is meaningful to use the address member of
7054 BPT locations. For some breakpoint types, the locations' address members
7055 are irrelevant and it makes no sense to attempt to compare them to other
7056 addresses (or use them for any other purpose either).
7057
7058 More specifically, each of the following breakpoint types will
7059 always have a zero valued location address and we don't want to mark
7060 breakpoints of any of these types to be a duplicate of an actual
7061 breakpoint location at address zero:
7062
7063 bp_watchpoint
7064 bp_catchpoint
7065
7066 */
7067
7068 static int
7069 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7070 {
7071 enum bptype type = bpt->type;
7072
7073 return (type != bp_watchpoint && type != bp_catchpoint);
7074 }
7075
7076 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7077 true if LOC1 and LOC2 represent the same watchpoint location. */
7078
7079 static int
7080 watchpoint_locations_match (struct bp_location *loc1,
7081 struct bp_location *loc2)
7082 {
7083 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7084 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7085
7086 /* Both of them must exist. */
7087 gdb_assert (w1 != NULL);
7088 gdb_assert (w2 != NULL);
7089
7090 /* If the target can evaluate the condition expression in hardware,
7091 then we we need to insert both watchpoints even if they are at
7092 the same place. Otherwise the watchpoint will only trigger when
7093 the condition of whichever watchpoint was inserted evaluates to
7094 true, not giving a chance for GDB to check the condition of the
7095 other watchpoint. */
7096 if ((w1->cond_exp
7097 && target_can_accel_watchpoint_condition (loc1->address,
7098 loc1->length,
7099 loc1->watchpoint_type,
7100 w1->cond_exp.get ()))
7101 || (w2->cond_exp
7102 && target_can_accel_watchpoint_condition (loc2->address,
7103 loc2->length,
7104 loc2->watchpoint_type,
7105 w2->cond_exp.get ())))
7106 return 0;
7107
7108 /* Note that this checks the owner's type, not the location's. In
7109 case the target does not support read watchpoints, but does
7110 support access watchpoints, we'll have bp_read_watchpoint
7111 watchpoints with hw_access locations. Those should be considered
7112 duplicates of hw_read locations. The hw_read locations will
7113 become hw_access locations later. */
7114 return (loc1->owner->type == loc2->owner->type
7115 && loc1->pspace->aspace == loc2->pspace->aspace
7116 && loc1->address == loc2->address
7117 && loc1->length == loc2->length);
7118 }
7119
7120 /* See breakpoint.h. */
7121
7122 int
7123 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7124 struct address_space *aspace2, CORE_ADDR addr2)
7125 {
7126 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7127 || aspace1 == aspace2)
7128 && addr1 == addr2);
7129 }
7130
7131 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7132 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7133 matches ASPACE2. On targets that have global breakpoints, the address
7134 space doesn't really matter. */
7135
7136 static int
7137 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7138 int len1, struct address_space *aspace2,
7139 CORE_ADDR addr2)
7140 {
7141 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7142 || aspace1 == aspace2)
7143 && addr2 >= addr1 && addr2 < addr1 + len1);
7144 }
7145
7146 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7147 a ranged breakpoint. In most targets, a match happens only if ASPACE
7148 matches the breakpoint's address space. On targets that have global
7149 breakpoints, the address space doesn't really matter. */
7150
7151 static int
7152 breakpoint_location_address_match (struct bp_location *bl,
7153 struct address_space *aspace,
7154 CORE_ADDR addr)
7155 {
7156 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7157 aspace, addr)
7158 || (bl->length
7159 && breakpoint_address_match_range (bl->pspace->aspace,
7160 bl->address, bl->length,
7161 aspace, addr)));
7162 }
7163
7164 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7165 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7166 match happens only if ASPACE matches the breakpoint's address
7167 space. On targets that have global breakpoints, the address space
7168 doesn't really matter. */
7169
7170 static int
7171 breakpoint_location_address_range_overlap (struct bp_location *bl,
7172 struct address_space *aspace,
7173 CORE_ADDR addr, int len)
7174 {
7175 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7176 || bl->pspace->aspace == aspace)
7177 {
7178 int bl_len = bl->length != 0 ? bl->length : 1;
7179
7180 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7181 return 1;
7182 }
7183 return 0;
7184 }
7185
7186 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7187 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7188 true, otherwise returns false. */
7189
7190 static int
7191 tracepoint_locations_match (struct bp_location *loc1,
7192 struct bp_location *loc2)
7193 {
7194 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7195 /* Since tracepoint locations are never duplicated with others', tracepoint
7196 locations at the same address of different tracepoints are regarded as
7197 different locations. */
7198 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7199 else
7200 return 0;
7201 }
7202
7203 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7204 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7205 represent the same location. */
7206
7207 static int
7208 breakpoint_locations_match (struct bp_location *loc1,
7209 struct bp_location *loc2)
7210 {
7211 int hw_point1, hw_point2;
7212
7213 /* Both of them must not be in moribund_locations. */
7214 gdb_assert (loc1->owner != NULL);
7215 gdb_assert (loc2->owner != NULL);
7216
7217 hw_point1 = is_hardware_watchpoint (loc1->owner);
7218 hw_point2 = is_hardware_watchpoint (loc2->owner);
7219
7220 if (hw_point1 != hw_point2)
7221 return 0;
7222 else if (hw_point1)
7223 return watchpoint_locations_match (loc1, loc2);
7224 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7225 return tracepoint_locations_match (loc1, loc2);
7226 else
7227 /* We compare bp_location.length in order to cover ranged breakpoints. */
7228 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7229 loc2->pspace->aspace, loc2->address)
7230 && loc1->length == loc2->length);
7231 }
7232
7233 static void
7234 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7235 int bnum, int have_bnum)
7236 {
7237 /* The longest string possibly returned by hex_string_custom
7238 is 50 chars. These must be at least that big for safety. */
7239 char astr1[64];
7240 char astr2[64];
7241
7242 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7243 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7244 if (have_bnum)
7245 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7246 bnum, astr1, astr2);
7247 else
7248 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7249 }
7250
7251 /* Adjust a breakpoint's address to account for architectural
7252 constraints on breakpoint placement. Return the adjusted address.
7253 Note: Very few targets require this kind of adjustment. For most
7254 targets, this function is simply the identity function. */
7255
7256 static CORE_ADDR
7257 adjust_breakpoint_address (struct gdbarch *gdbarch,
7258 CORE_ADDR bpaddr, enum bptype bptype)
7259 {
7260 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7261 {
7262 /* Very few targets need any kind of breakpoint adjustment. */
7263 return bpaddr;
7264 }
7265 else if (bptype == bp_watchpoint
7266 || bptype == bp_hardware_watchpoint
7267 || bptype == bp_read_watchpoint
7268 || bptype == bp_access_watchpoint
7269 || bptype == bp_catchpoint)
7270 {
7271 /* Watchpoints and the various bp_catch_* eventpoints should not
7272 have their addresses modified. */
7273 return bpaddr;
7274 }
7275 else if (bptype == bp_single_step)
7276 {
7277 /* Single-step breakpoints should not have their addresses
7278 modified. If there's any architectural constrain that
7279 applies to this address, then it should have already been
7280 taken into account when the breakpoint was created in the
7281 first place. If we didn't do this, stepping through e.g.,
7282 Thumb-2 IT blocks would break. */
7283 return bpaddr;
7284 }
7285 else
7286 {
7287 CORE_ADDR adjusted_bpaddr;
7288
7289 /* Some targets have architectural constraints on the placement
7290 of breakpoint instructions. Obtain the adjusted address. */
7291 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7292
7293 /* An adjusted breakpoint address can significantly alter
7294 a user's expectations. Print a warning if an adjustment
7295 is required. */
7296 if (adjusted_bpaddr != bpaddr)
7297 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7298
7299 return adjusted_bpaddr;
7300 }
7301 }
7302
7303 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7304 {
7305 bp_location *loc = this;
7306
7307 gdb_assert (ops != NULL);
7308
7309 loc->ops = ops;
7310 loc->owner = owner;
7311 loc->cond_bytecode = NULL;
7312 loc->shlib_disabled = 0;
7313 loc->enabled = 1;
7314
7315 switch (owner->type)
7316 {
7317 case bp_breakpoint:
7318 case bp_single_step:
7319 case bp_until:
7320 case bp_finish:
7321 case bp_longjmp:
7322 case bp_longjmp_resume:
7323 case bp_longjmp_call_dummy:
7324 case bp_exception:
7325 case bp_exception_resume:
7326 case bp_step_resume:
7327 case bp_hp_step_resume:
7328 case bp_watchpoint_scope:
7329 case bp_call_dummy:
7330 case bp_std_terminate:
7331 case bp_shlib_event:
7332 case bp_thread_event:
7333 case bp_overlay_event:
7334 case bp_jit_event:
7335 case bp_longjmp_master:
7336 case bp_std_terminate_master:
7337 case bp_exception_master:
7338 case bp_gnu_ifunc_resolver:
7339 case bp_gnu_ifunc_resolver_return:
7340 case bp_dprintf:
7341 loc->loc_type = bp_loc_software_breakpoint;
7342 mark_breakpoint_location_modified (loc);
7343 break;
7344 case bp_hardware_breakpoint:
7345 loc->loc_type = bp_loc_hardware_breakpoint;
7346 mark_breakpoint_location_modified (loc);
7347 break;
7348 case bp_hardware_watchpoint:
7349 case bp_read_watchpoint:
7350 case bp_access_watchpoint:
7351 loc->loc_type = bp_loc_hardware_watchpoint;
7352 break;
7353 case bp_watchpoint:
7354 case bp_catchpoint:
7355 case bp_tracepoint:
7356 case bp_fast_tracepoint:
7357 case bp_static_tracepoint:
7358 loc->loc_type = bp_loc_other;
7359 break;
7360 default:
7361 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7362 }
7363
7364 loc->refc = 1;
7365 }
7366
7367 /* Allocate a struct bp_location. */
7368
7369 static struct bp_location *
7370 allocate_bp_location (struct breakpoint *bpt)
7371 {
7372 return bpt->ops->allocate_location (bpt);
7373 }
7374
7375 static void
7376 free_bp_location (struct bp_location *loc)
7377 {
7378 loc->ops->dtor (loc);
7379 delete loc;
7380 }
7381
7382 /* Increment reference count. */
7383
7384 static void
7385 incref_bp_location (struct bp_location *bl)
7386 {
7387 ++bl->refc;
7388 }
7389
7390 /* Decrement reference count. If the reference count reaches 0,
7391 destroy the bp_location. Sets *BLP to NULL. */
7392
7393 static void
7394 decref_bp_location (struct bp_location **blp)
7395 {
7396 gdb_assert ((*blp)->refc > 0);
7397
7398 if (--(*blp)->refc == 0)
7399 free_bp_location (*blp);
7400 *blp = NULL;
7401 }
7402
7403 /* Add breakpoint B at the end of the global breakpoint chain. */
7404
7405 static void
7406 add_to_breakpoint_chain (struct breakpoint *b)
7407 {
7408 struct breakpoint *b1;
7409
7410 /* Add this breakpoint to the end of the chain so that a list of
7411 breakpoints will come out in order of increasing numbers. */
7412
7413 b1 = breakpoint_chain;
7414 if (b1 == 0)
7415 breakpoint_chain = b;
7416 else
7417 {
7418 while (b1->next)
7419 b1 = b1->next;
7420 b1->next = b;
7421 }
7422 }
7423
7424 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7425
7426 static void
7427 init_raw_breakpoint_without_location (struct breakpoint *b,
7428 struct gdbarch *gdbarch,
7429 enum bptype bptype,
7430 const struct breakpoint_ops *ops)
7431 {
7432 gdb_assert (ops != NULL);
7433
7434 b->ops = ops;
7435 b->type = bptype;
7436 b->gdbarch = gdbarch;
7437 b->language = current_language->la_language;
7438 b->input_radix = input_radix;
7439 b->related_breakpoint = b;
7440 }
7441
7442 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7443 that has type BPTYPE and has no locations as yet. */
7444
7445 static struct breakpoint *
7446 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7447 enum bptype bptype,
7448 const struct breakpoint_ops *ops)
7449 {
7450 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7451
7452 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7453 add_to_breakpoint_chain (b.get ());
7454
7455 return b.release ();
7456 }
7457
7458 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7459 resolutions should be made as the user specified the location explicitly
7460 enough. */
7461
7462 static void
7463 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7464 {
7465 gdb_assert (loc->owner != NULL);
7466
7467 if (loc->owner->type == bp_breakpoint
7468 || loc->owner->type == bp_hardware_breakpoint
7469 || is_tracepoint (loc->owner))
7470 {
7471 int is_gnu_ifunc;
7472 const char *function_name;
7473 CORE_ADDR func_addr;
7474
7475 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7476 &func_addr, NULL, &is_gnu_ifunc);
7477
7478 if (is_gnu_ifunc && !explicit_loc)
7479 {
7480 struct breakpoint *b = loc->owner;
7481
7482 gdb_assert (loc->pspace == current_program_space);
7483 if (gnu_ifunc_resolve_name (function_name,
7484 &loc->requested_address))
7485 {
7486 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7487 loc->address = adjust_breakpoint_address (loc->gdbarch,
7488 loc->requested_address,
7489 b->type);
7490 }
7491 else if (b->type == bp_breakpoint && b->loc == loc
7492 && loc->next == NULL && b->related_breakpoint == b)
7493 {
7494 /* Create only the whole new breakpoint of this type but do not
7495 mess more complicated breakpoints with multiple locations. */
7496 b->type = bp_gnu_ifunc_resolver;
7497 /* Remember the resolver's address for use by the return
7498 breakpoint. */
7499 loc->related_address = func_addr;
7500 }
7501 }
7502
7503 if (function_name)
7504 loc->function_name = xstrdup (function_name);
7505 }
7506 }
7507
7508 /* Attempt to determine architecture of location identified by SAL. */
7509 struct gdbarch *
7510 get_sal_arch (struct symtab_and_line sal)
7511 {
7512 if (sal.section)
7513 return get_objfile_arch (sal.section->objfile);
7514 if (sal.symtab)
7515 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7516
7517 return NULL;
7518 }
7519
7520 /* Low level routine for partially initializing a breakpoint of type
7521 BPTYPE. The newly created breakpoint's address, section, source
7522 file name, and line number are provided by SAL.
7523
7524 It is expected that the caller will complete the initialization of
7525 the newly created breakpoint struct as well as output any status
7526 information regarding the creation of a new breakpoint. */
7527
7528 static void
7529 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7530 struct symtab_and_line sal, enum bptype bptype,
7531 const struct breakpoint_ops *ops)
7532 {
7533 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7534
7535 add_location_to_breakpoint (b, &sal);
7536
7537 if (bptype != bp_catchpoint)
7538 gdb_assert (sal.pspace != NULL);
7539
7540 /* Store the program space that was used to set the breakpoint,
7541 except for ordinary breakpoints, which are independent of the
7542 program space. */
7543 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7544 b->pspace = sal.pspace;
7545 }
7546
7547 /* set_raw_breakpoint is a low level routine for allocating and
7548 partially initializing a breakpoint of type BPTYPE. The newly
7549 created breakpoint's address, section, source file name, and line
7550 number are provided by SAL. The newly created and partially
7551 initialized breakpoint is added to the breakpoint chain and
7552 is also returned as the value of this function.
7553
7554 It is expected that the caller will complete the initialization of
7555 the newly created breakpoint struct as well as output any status
7556 information regarding the creation of a new breakpoint. In
7557 particular, set_raw_breakpoint does NOT set the breakpoint
7558 number! Care should be taken to not allow an error to occur
7559 prior to completing the initialization of the breakpoint. If this
7560 should happen, a bogus breakpoint will be left on the chain. */
7561
7562 struct breakpoint *
7563 set_raw_breakpoint (struct gdbarch *gdbarch,
7564 struct symtab_and_line sal, enum bptype bptype,
7565 const struct breakpoint_ops *ops)
7566 {
7567 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7568
7569 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7570 add_to_breakpoint_chain (b.get ());
7571
7572 return b.release ();
7573 }
7574
7575 /* Call this routine when stepping and nexting to enable a breakpoint
7576 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7577 initiated the operation. */
7578
7579 void
7580 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7581 {
7582 struct breakpoint *b, *b_tmp;
7583 int thread = tp->global_num;
7584
7585 /* To avoid having to rescan all objfile symbols at every step,
7586 we maintain a list of continually-inserted but always disabled
7587 longjmp "master" breakpoints. Here, we simply create momentary
7588 clones of those and enable them for the requested thread. */
7589 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7590 if (b->pspace == current_program_space
7591 && (b->type == bp_longjmp_master
7592 || b->type == bp_exception_master))
7593 {
7594 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7595 struct breakpoint *clone;
7596
7597 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7598 after their removal. */
7599 clone = momentary_breakpoint_from_master (b, type,
7600 &momentary_breakpoint_ops, 1);
7601 clone->thread = thread;
7602 }
7603
7604 tp->initiating_frame = frame;
7605 }
7606
7607 /* Delete all longjmp breakpoints from THREAD. */
7608 void
7609 delete_longjmp_breakpoint (int thread)
7610 {
7611 struct breakpoint *b, *b_tmp;
7612
7613 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7614 if (b->type == bp_longjmp || b->type == bp_exception)
7615 {
7616 if (b->thread == thread)
7617 delete_breakpoint (b);
7618 }
7619 }
7620
7621 void
7622 delete_longjmp_breakpoint_at_next_stop (int thread)
7623 {
7624 struct breakpoint *b, *b_tmp;
7625
7626 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7627 if (b->type == bp_longjmp || b->type == bp_exception)
7628 {
7629 if (b->thread == thread)
7630 b->disposition = disp_del_at_next_stop;
7631 }
7632 }
7633
7634 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7635 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7636 pointer to any of them. Return NULL if this system cannot place longjmp
7637 breakpoints. */
7638
7639 struct breakpoint *
7640 set_longjmp_breakpoint_for_call_dummy (void)
7641 {
7642 struct breakpoint *b, *retval = NULL;
7643
7644 ALL_BREAKPOINTS (b)
7645 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7646 {
7647 struct breakpoint *new_b;
7648
7649 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7650 &momentary_breakpoint_ops,
7651 1);
7652 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7653
7654 /* Link NEW_B into the chain of RETVAL breakpoints. */
7655
7656 gdb_assert (new_b->related_breakpoint == new_b);
7657 if (retval == NULL)
7658 retval = new_b;
7659 new_b->related_breakpoint = retval;
7660 while (retval->related_breakpoint != new_b->related_breakpoint)
7661 retval = retval->related_breakpoint;
7662 retval->related_breakpoint = new_b;
7663 }
7664
7665 return retval;
7666 }
7667
7668 /* Verify all existing dummy frames and their associated breakpoints for
7669 TP. Remove those which can no longer be found in the current frame
7670 stack.
7671
7672 You should call this function only at places where it is safe to currently
7673 unwind the whole stack. Failed stack unwind would discard live dummy
7674 frames. */
7675
7676 void
7677 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7678 {
7679 struct breakpoint *b, *b_tmp;
7680
7681 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7682 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7683 {
7684 struct breakpoint *dummy_b = b->related_breakpoint;
7685
7686 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7687 dummy_b = dummy_b->related_breakpoint;
7688 if (dummy_b->type != bp_call_dummy
7689 || frame_find_by_id (dummy_b->frame_id) != NULL)
7690 continue;
7691
7692 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7693
7694 while (b->related_breakpoint != b)
7695 {
7696 if (b_tmp == b->related_breakpoint)
7697 b_tmp = b->related_breakpoint->next;
7698 delete_breakpoint (b->related_breakpoint);
7699 }
7700 delete_breakpoint (b);
7701 }
7702 }
7703
7704 void
7705 enable_overlay_breakpoints (void)
7706 {
7707 struct breakpoint *b;
7708
7709 ALL_BREAKPOINTS (b)
7710 if (b->type == bp_overlay_event)
7711 {
7712 b->enable_state = bp_enabled;
7713 update_global_location_list (UGLL_MAY_INSERT);
7714 overlay_events_enabled = 1;
7715 }
7716 }
7717
7718 void
7719 disable_overlay_breakpoints (void)
7720 {
7721 struct breakpoint *b;
7722
7723 ALL_BREAKPOINTS (b)
7724 if (b->type == bp_overlay_event)
7725 {
7726 b->enable_state = bp_disabled;
7727 update_global_location_list (UGLL_DONT_INSERT);
7728 overlay_events_enabled = 0;
7729 }
7730 }
7731
7732 /* Set an active std::terminate breakpoint for each std::terminate
7733 master breakpoint. */
7734 void
7735 set_std_terminate_breakpoint (void)
7736 {
7737 struct breakpoint *b, *b_tmp;
7738
7739 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7740 if (b->pspace == current_program_space
7741 && b->type == bp_std_terminate_master)
7742 {
7743 momentary_breakpoint_from_master (b, bp_std_terminate,
7744 &momentary_breakpoint_ops, 1);
7745 }
7746 }
7747
7748 /* Delete all the std::terminate breakpoints. */
7749 void
7750 delete_std_terminate_breakpoint (void)
7751 {
7752 struct breakpoint *b, *b_tmp;
7753
7754 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7755 if (b->type == bp_std_terminate)
7756 delete_breakpoint (b);
7757 }
7758
7759 struct breakpoint *
7760 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7761 {
7762 struct breakpoint *b;
7763
7764 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7765 &internal_breakpoint_ops);
7766
7767 b->enable_state = bp_enabled;
7768 /* location has to be used or breakpoint_re_set will delete me. */
7769 b->location = new_address_location (b->loc->address, NULL, 0);
7770
7771 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7772
7773 return b;
7774 }
7775
7776 struct lang_and_radix
7777 {
7778 enum language lang;
7779 int radix;
7780 };
7781
7782 /* Create a breakpoint for JIT code registration and unregistration. */
7783
7784 struct breakpoint *
7785 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7786 {
7787 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7788 &internal_breakpoint_ops);
7789 }
7790
7791 /* Remove JIT code registration and unregistration breakpoint(s). */
7792
7793 void
7794 remove_jit_event_breakpoints (void)
7795 {
7796 struct breakpoint *b, *b_tmp;
7797
7798 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7799 if (b->type == bp_jit_event
7800 && b->loc->pspace == current_program_space)
7801 delete_breakpoint (b);
7802 }
7803
7804 void
7805 remove_solib_event_breakpoints (void)
7806 {
7807 struct breakpoint *b, *b_tmp;
7808
7809 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7810 if (b->type == bp_shlib_event
7811 && b->loc->pspace == current_program_space)
7812 delete_breakpoint (b);
7813 }
7814
7815 /* See breakpoint.h. */
7816
7817 void
7818 remove_solib_event_breakpoints_at_next_stop (void)
7819 {
7820 struct breakpoint *b, *b_tmp;
7821
7822 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7823 if (b->type == bp_shlib_event
7824 && b->loc->pspace == current_program_space)
7825 b->disposition = disp_del_at_next_stop;
7826 }
7827
7828 /* Helper for create_solib_event_breakpoint /
7829 create_and_insert_solib_event_breakpoint. Allows specifying which
7830 INSERT_MODE to pass through to update_global_location_list. */
7831
7832 static struct breakpoint *
7833 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7834 enum ugll_insert_mode insert_mode)
7835 {
7836 struct breakpoint *b;
7837
7838 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7839 &internal_breakpoint_ops);
7840 update_global_location_list_nothrow (insert_mode);
7841 return b;
7842 }
7843
7844 struct breakpoint *
7845 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7846 {
7847 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7848 }
7849
7850 /* See breakpoint.h. */
7851
7852 struct breakpoint *
7853 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7854 {
7855 struct breakpoint *b;
7856
7857 /* Explicitly tell update_global_location_list to insert
7858 locations. */
7859 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7860 if (!b->loc->inserted)
7861 {
7862 delete_breakpoint (b);
7863 return NULL;
7864 }
7865 return b;
7866 }
7867
7868 /* Disable any breakpoints that are on code in shared libraries. Only
7869 apply to enabled breakpoints, disabled ones can just stay disabled. */
7870
7871 void
7872 disable_breakpoints_in_shlibs (void)
7873 {
7874 struct bp_location *loc, **locp_tmp;
7875
7876 ALL_BP_LOCATIONS (loc, locp_tmp)
7877 {
7878 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7879 struct breakpoint *b = loc->owner;
7880
7881 /* We apply the check to all breakpoints, including disabled for
7882 those with loc->duplicate set. This is so that when breakpoint
7883 becomes enabled, or the duplicate is removed, gdb will try to
7884 insert all breakpoints. If we don't set shlib_disabled here,
7885 we'll try to insert those breakpoints and fail. */
7886 if (((b->type == bp_breakpoint)
7887 || (b->type == bp_jit_event)
7888 || (b->type == bp_hardware_breakpoint)
7889 || (is_tracepoint (b)))
7890 && loc->pspace == current_program_space
7891 && !loc->shlib_disabled
7892 && solib_name_from_address (loc->pspace, loc->address)
7893 )
7894 {
7895 loc->shlib_disabled = 1;
7896 }
7897 }
7898 }
7899
7900 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7901 notification of unloaded_shlib. Only apply to enabled breakpoints,
7902 disabled ones can just stay disabled. */
7903
7904 static void
7905 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7906 {
7907 struct bp_location *loc, **locp_tmp;
7908 int disabled_shlib_breaks = 0;
7909
7910 ALL_BP_LOCATIONS (loc, locp_tmp)
7911 {
7912 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7913 struct breakpoint *b = loc->owner;
7914
7915 if (solib->pspace == loc->pspace
7916 && !loc->shlib_disabled
7917 && (((b->type == bp_breakpoint
7918 || b->type == bp_jit_event
7919 || b->type == bp_hardware_breakpoint)
7920 && (loc->loc_type == bp_loc_hardware_breakpoint
7921 || loc->loc_type == bp_loc_software_breakpoint))
7922 || is_tracepoint (b))
7923 && solib_contains_address_p (solib, loc->address))
7924 {
7925 loc->shlib_disabled = 1;
7926 /* At this point, we cannot rely on remove_breakpoint
7927 succeeding so we must mark the breakpoint as not inserted
7928 to prevent future errors occurring in remove_breakpoints. */
7929 loc->inserted = 0;
7930
7931 /* This may cause duplicate notifications for the same breakpoint. */
7932 observer_notify_breakpoint_modified (b);
7933
7934 if (!disabled_shlib_breaks)
7935 {
7936 target_terminal_ours_for_output ();
7937 warning (_("Temporarily disabling breakpoints "
7938 "for unloaded shared library \"%s\""),
7939 solib->so_name);
7940 }
7941 disabled_shlib_breaks = 1;
7942 }
7943 }
7944 }
7945
7946 /* Disable any breakpoints and tracepoints in OBJFILE upon
7947 notification of free_objfile. Only apply to enabled breakpoints,
7948 disabled ones can just stay disabled. */
7949
7950 static void
7951 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7952 {
7953 struct breakpoint *b;
7954
7955 if (objfile == NULL)
7956 return;
7957
7958 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7959 managed by the user with add-symbol-file/remove-symbol-file.
7960 Similarly to how breakpoints in shared libraries are handled in
7961 response to "nosharedlibrary", mark breakpoints in such modules
7962 shlib_disabled so they end up uninserted on the next global
7963 location list update. Shared libraries not loaded by the user
7964 aren't handled here -- they're already handled in
7965 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7966 solib_unloaded observer. We skip objfiles that are not
7967 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7968 main objfile). */
7969 if ((objfile->flags & OBJF_SHARED) == 0
7970 || (objfile->flags & OBJF_USERLOADED) == 0)
7971 return;
7972
7973 ALL_BREAKPOINTS (b)
7974 {
7975 struct bp_location *loc;
7976 int bp_modified = 0;
7977
7978 if (!is_breakpoint (b) && !is_tracepoint (b))
7979 continue;
7980
7981 for (loc = b->loc; loc != NULL; loc = loc->next)
7982 {
7983 CORE_ADDR loc_addr = loc->address;
7984
7985 if (loc->loc_type != bp_loc_hardware_breakpoint
7986 && loc->loc_type != bp_loc_software_breakpoint)
7987 continue;
7988
7989 if (loc->shlib_disabled != 0)
7990 continue;
7991
7992 if (objfile->pspace != loc->pspace)
7993 continue;
7994
7995 if (loc->loc_type != bp_loc_hardware_breakpoint
7996 && loc->loc_type != bp_loc_software_breakpoint)
7997 continue;
7998
7999 if (is_addr_in_objfile (loc_addr, objfile))
8000 {
8001 loc->shlib_disabled = 1;
8002 /* At this point, we don't know whether the object was
8003 unmapped from the inferior or not, so leave the
8004 inserted flag alone. We'll handle failure to
8005 uninsert quietly, in case the object was indeed
8006 unmapped. */
8007
8008 mark_breakpoint_location_modified (loc);
8009
8010 bp_modified = 1;
8011 }
8012 }
8013
8014 if (bp_modified)
8015 observer_notify_breakpoint_modified (b);
8016 }
8017 }
8018
8019 /* FORK & VFORK catchpoints. */
8020
8021 /* An instance of this type is used to represent a fork or vfork
8022 catchpoint. A breakpoint is really of this type iff its ops pointer points
8023 to CATCH_FORK_BREAKPOINT_OPS. */
8024
8025 struct fork_catchpoint : public breakpoint
8026 {
8027 /* Process id of a child process whose forking triggered this
8028 catchpoint. This field is only valid immediately after this
8029 catchpoint has triggered. */
8030 ptid_t forked_inferior_pid;
8031 };
8032
8033 /* Implement the "insert" breakpoint_ops method for fork
8034 catchpoints. */
8035
8036 static int
8037 insert_catch_fork (struct bp_location *bl)
8038 {
8039 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8040 }
8041
8042 /* Implement the "remove" breakpoint_ops method for fork
8043 catchpoints. */
8044
8045 static int
8046 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8047 {
8048 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8049 }
8050
8051 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8052 catchpoints. */
8053
8054 static int
8055 breakpoint_hit_catch_fork (const struct bp_location *bl,
8056 struct address_space *aspace, CORE_ADDR bp_addr,
8057 const struct target_waitstatus *ws)
8058 {
8059 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8060
8061 if (ws->kind != TARGET_WAITKIND_FORKED)
8062 return 0;
8063
8064 c->forked_inferior_pid = ws->value.related_pid;
8065 return 1;
8066 }
8067
8068 /* Implement the "print_it" breakpoint_ops method for fork
8069 catchpoints. */
8070
8071 static enum print_stop_action
8072 print_it_catch_fork (bpstat bs)
8073 {
8074 struct ui_out *uiout = current_uiout;
8075 struct breakpoint *b = bs->breakpoint_at;
8076 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8077
8078 annotate_catchpoint (b->number);
8079 maybe_print_thread_hit_breakpoint (uiout);
8080 if (b->disposition == disp_del)
8081 uiout->text ("Temporary catchpoint ");
8082 else
8083 uiout->text ("Catchpoint ");
8084 if (uiout->is_mi_like_p ())
8085 {
8086 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8087 uiout->field_string ("disp", bpdisp_text (b->disposition));
8088 }
8089 uiout->field_int ("bkptno", b->number);
8090 uiout->text (" (forked process ");
8091 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8092 uiout->text ("), ");
8093 return PRINT_SRC_AND_LOC;
8094 }
8095
8096 /* Implement the "print_one" breakpoint_ops method for fork
8097 catchpoints. */
8098
8099 static void
8100 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8101 {
8102 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8103 struct value_print_options opts;
8104 struct ui_out *uiout = current_uiout;
8105
8106 get_user_print_options (&opts);
8107
8108 /* Field 4, the address, is omitted (which makes the columns not
8109 line up too nicely with the headers, but the effect is relatively
8110 readable). */
8111 if (opts.addressprint)
8112 uiout->field_skip ("addr");
8113 annotate_field (5);
8114 uiout->text ("fork");
8115 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8116 {
8117 uiout->text (", process ");
8118 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8119 uiout->spaces (1);
8120 }
8121
8122 if (uiout->is_mi_like_p ())
8123 uiout->field_string ("catch-type", "fork");
8124 }
8125
8126 /* Implement the "print_mention" breakpoint_ops method for fork
8127 catchpoints. */
8128
8129 static void
8130 print_mention_catch_fork (struct breakpoint *b)
8131 {
8132 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8133 }
8134
8135 /* Implement the "print_recreate" breakpoint_ops method for fork
8136 catchpoints. */
8137
8138 static void
8139 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8140 {
8141 fprintf_unfiltered (fp, "catch fork");
8142 print_recreate_thread (b, fp);
8143 }
8144
8145 /* The breakpoint_ops structure to be used in fork catchpoints. */
8146
8147 static struct breakpoint_ops catch_fork_breakpoint_ops;
8148
8149 /* Implement the "insert" breakpoint_ops method for vfork
8150 catchpoints. */
8151
8152 static int
8153 insert_catch_vfork (struct bp_location *bl)
8154 {
8155 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8156 }
8157
8158 /* Implement the "remove" breakpoint_ops method for vfork
8159 catchpoints. */
8160
8161 static int
8162 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8163 {
8164 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8165 }
8166
8167 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8168 catchpoints. */
8169
8170 static int
8171 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8172 struct address_space *aspace, CORE_ADDR bp_addr,
8173 const struct target_waitstatus *ws)
8174 {
8175 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8176
8177 if (ws->kind != TARGET_WAITKIND_VFORKED)
8178 return 0;
8179
8180 c->forked_inferior_pid = ws->value.related_pid;
8181 return 1;
8182 }
8183
8184 /* Implement the "print_it" breakpoint_ops method for vfork
8185 catchpoints. */
8186
8187 static enum print_stop_action
8188 print_it_catch_vfork (bpstat bs)
8189 {
8190 struct ui_out *uiout = current_uiout;
8191 struct breakpoint *b = bs->breakpoint_at;
8192 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8193
8194 annotate_catchpoint (b->number);
8195 maybe_print_thread_hit_breakpoint (uiout);
8196 if (b->disposition == disp_del)
8197 uiout->text ("Temporary catchpoint ");
8198 else
8199 uiout->text ("Catchpoint ");
8200 if (uiout->is_mi_like_p ())
8201 {
8202 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8203 uiout->field_string ("disp", bpdisp_text (b->disposition));
8204 }
8205 uiout->field_int ("bkptno", b->number);
8206 uiout->text (" (vforked process ");
8207 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8208 uiout->text ("), ");
8209 return PRINT_SRC_AND_LOC;
8210 }
8211
8212 /* Implement the "print_one" breakpoint_ops method for vfork
8213 catchpoints. */
8214
8215 static void
8216 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8217 {
8218 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8219 struct value_print_options opts;
8220 struct ui_out *uiout = current_uiout;
8221
8222 get_user_print_options (&opts);
8223 /* Field 4, the address, is omitted (which makes the columns not
8224 line up too nicely with the headers, but the effect is relatively
8225 readable). */
8226 if (opts.addressprint)
8227 uiout->field_skip ("addr");
8228 annotate_field (5);
8229 uiout->text ("vfork");
8230 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8231 {
8232 uiout->text (", process ");
8233 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8234 uiout->spaces (1);
8235 }
8236
8237 if (uiout->is_mi_like_p ())
8238 uiout->field_string ("catch-type", "vfork");
8239 }
8240
8241 /* Implement the "print_mention" breakpoint_ops method for vfork
8242 catchpoints. */
8243
8244 static void
8245 print_mention_catch_vfork (struct breakpoint *b)
8246 {
8247 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8248 }
8249
8250 /* Implement the "print_recreate" breakpoint_ops method for vfork
8251 catchpoints. */
8252
8253 static void
8254 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8255 {
8256 fprintf_unfiltered (fp, "catch vfork");
8257 print_recreate_thread (b, fp);
8258 }
8259
8260 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8261
8262 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8263
8264 /* An instance of this type is used to represent an solib catchpoint.
8265 A breakpoint is really of this type iff its ops pointer points to
8266 CATCH_SOLIB_BREAKPOINT_OPS. */
8267
8268 struct solib_catchpoint : public breakpoint
8269 {
8270 ~solib_catchpoint () override;
8271
8272 /* True for "catch load", false for "catch unload". */
8273 unsigned char is_load;
8274
8275 /* Regular expression to match, if any. COMPILED is only valid when
8276 REGEX is non-NULL. */
8277 char *regex;
8278 std::unique_ptr<compiled_regex> compiled;
8279 };
8280
8281 solib_catchpoint::~solib_catchpoint ()
8282 {
8283 xfree (this->regex);
8284 }
8285
8286 static int
8287 insert_catch_solib (struct bp_location *ignore)
8288 {
8289 return 0;
8290 }
8291
8292 static int
8293 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8294 {
8295 return 0;
8296 }
8297
8298 static int
8299 breakpoint_hit_catch_solib (const struct bp_location *bl,
8300 struct address_space *aspace,
8301 CORE_ADDR bp_addr,
8302 const struct target_waitstatus *ws)
8303 {
8304 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8305 struct breakpoint *other;
8306
8307 if (ws->kind == TARGET_WAITKIND_LOADED)
8308 return 1;
8309
8310 ALL_BREAKPOINTS (other)
8311 {
8312 struct bp_location *other_bl;
8313
8314 if (other == bl->owner)
8315 continue;
8316
8317 if (other->type != bp_shlib_event)
8318 continue;
8319
8320 if (self->pspace != NULL && other->pspace != self->pspace)
8321 continue;
8322
8323 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8324 {
8325 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8326 return 1;
8327 }
8328 }
8329
8330 return 0;
8331 }
8332
8333 static void
8334 check_status_catch_solib (struct bpstats *bs)
8335 {
8336 struct solib_catchpoint *self
8337 = (struct solib_catchpoint *) bs->breakpoint_at;
8338 int ix;
8339
8340 if (self->is_load)
8341 {
8342 struct so_list *iter;
8343
8344 for (ix = 0;
8345 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8346 ix, iter);
8347 ++ix)
8348 {
8349 if (!self->regex
8350 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
8351 return;
8352 }
8353 }
8354 else
8355 {
8356 char *iter;
8357
8358 for (ix = 0;
8359 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8360 ix, iter);
8361 ++ix)
8362 {
8363 if (!self->regex
8364 || self->compiled->exec (iter, 0, NULL, 0) == 0)
8365 return;
8366 }
8367 }
8368
8369 bs->stop = 0;
8370 bs->print_it = print_it_noop;
8371 }
8372
8373 static enum print_stop_action
8374 print_it_catch_solib (bpstat bs)
8375 {
8376 struct breakpoint *b = bs->breakpoint_at;
8377 struct ui_out *uiout = current_uiout;
8378
8379 annotate_catchpoint (b->number);
8380 maybe_print_thread_hit_breakpoint (uiout);
8381 if (b->disposition == disp_del)
8382 uiout->text ("Temporary catchpoint ");
8383 else
8384 uiout->text ("Catchpoint ");
8385 uiout->field_int ("bkptno", b->number);
8386 uiout->text ("\n");
8387 if (uiout->is_mi_like_p ())
8388 uiout->field_string ("disp", bpdisp_text (b->disposition));
8389 print_solib_event (1);
8390 return PRINT_SRC_AND_LOC;
8391 }
8392
8393 static void
8394 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8395 {
8396 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8397 struct value_print_options opts;
8398 struct ui_out *uiout = current_uiout;
8399 char *msg;
8400
8401 get_user_print_options (&opts);
8402 /* Field 4, the address, is omitted (which makes the columns not
8403 line up too nicely with the headers, but the effect is relatively
8404 readable). */
8405 if (opts.addressprint)
8406 {
8407 annotate_field (4);
8408 uiout->field_skip ("addr");
8409 }
8410
8411 annotate_field (5);
8412 if (self->is_load)
8413 {
8414 if (self->regex)
8415 msg = xstrprintf (_("load of library matching %s"), self->regex);
8416 else
8417 msg = xstrdup (_("load of library"));
8418 }
8419 else
8420 {
8421 if (self->regex)
8422 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8423 else
8424 msg = xstrdup (_("unload of library"));
8425 }
8426 uiout->field_string ("what", msg);
8427 xfree (msg);
8428
8429 if (uiout->is_mi_like_p ())
8430 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8431 }
8432
8433 static void
8434 print_mention_catch_solib (struct breakpoint *b)
8435 {
8436 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8437
8438 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8439 self->is_load ? "load" : "unload");
8440 }
8441
8442 static void
8443 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8444 {
8445 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8446
8447 fprintf_unfiltered (fp, "%s %s",
8448 b->disposition == disp_del ? "tcatch" : "catch",
8449 self->is_load ? "load" : "unload");
8450 if (self->regex)
8451 fprintf_unfiltered (fp, " %s", self->regex);
8452 fprintf_unfiltered (fp, "\n");
8453 }
8454
8455 static struct breakpoint_ops catch_solib_breakpoint_ops;
8456
8457 /* Shared helper function (MI and CLI) for creating and installing
8458 a shared object event catchpoint. If IS_LOAD is non-zero then
8459 the events to be caught are load events, otherwise they are
8460 unload events. If IS_TEMP is non-zero the catchpoint is a
8461 temporary one. If ENABLED is non-zero the catchpoint is
8462 created in an enabled state. */
8463
8464 void
8465 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8466 {
8467 struct solib_catchpoint *c;
8468 struct gdbarch *gdbarch = get_current_arch ();
8469 struct cleanup *cleanup;
8470
8471 if (!arg)
8472 arg = "";
8473 arg = skip_spaces_const (arg);
8474
8475 c = new solib_catchpoint ();
8476 cleanup = make_cleanup (xfree, c);
8477
8478 if (*arg != '\0')
8479 {
8480 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8481 _("Invalid regexp")));
8482 c->regex = xstrdup (arg);
8483 }
8484
8485 c->is_load = is_load;
8486 init_catchpoint (c, gdbarch, is_temp, NULL,
8487 &catch_solib_breakpoint_ops);
8488
8489 c->enable_state = enabled ? bp_enabled : bp_disabled;
8490
8491 discard_cleanups (cleanup);
8492 install_breakpoint (0, c, 1);
8493 }
8494
8495 /* A helper function that does all the work for "catch load" and
8496 "catch unload". */
8497
8498 static void
8499 catch_load_or_unload (char *arg, int from_tty, int is_load,
8500 struct cmd_list_element *command)
8501 {
8502 int tempflag;
8503 const int enabled = 1;
8504
8505 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8506
8507 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8508 }
8509
8510 static void
8511 catch_load_command_1 (char *arg, int from_tty,
8512 struct cmd_list_element *command)
8513 {
8514 catch_load_or_unload (arg, from_tty, 1, command);
8515 }
8516
8517 static void
8518 catch_unload_command_1 (char *arg, int from_tty,
8519 struct cmd_list_element *command)
8520 {
8521 catch_load_or_unload (arg, from_tty, 0, command);
8522 }
8523
8524 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8525 is non-zero, then make the breakpoint temporary. If COND_STRING is
8526 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8527 the breakpoint_ops structure associated to the catchpoint. */
8528
8529 void
8530 init_catchpoint (struct breakpoint *b,
8531 struct gdbarch *gdbarch, int tempflag,
8532 const char *cond_string,
8533 const struct breakpoint_ops *ops)
8534 {
8535 struct symtab_and_line sal;
8536
8537 init_sal (&sal);
8538 sal.pspace = current_program_space;
8539
8540 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8541
8542 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8543 b->disposition = tempflag ? disp_del : disp_donttouch;
8544 }
8545
8546 void
8547 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8548 {
8549 add_to_breakpoint_chain (b);
8550 set_breakpoint_number (internal, b);
8551 if (is_tracepoint (b))
8552 set_tracepoint_count (breakpoint_count);
8553 if (!internal)
8554 mention (b);
8555 observer_notify_breakpoint_created (b);
8556
8557 if (update_gll)
8558 update_global_location_list (UGLL_MAY_INSERT);
8559 }
8560
8561 static void
8562 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8563 int tempflag, const char *cond_string,
8564 const struct breakpoint_ops *ops)
8565 {
8566 struct fork_catchpoint *c = new fork_catchpoint ();
8567
8568 init_catchpoint (c, gdbarch, tempflag, cond_string, ops);
8569
8570 c->forked_inferior_pid = null_ptid;
8571
8572 install_breakpoint (0, c, 1);
8573 }
8574
8575 /* Exec catchpoints. */
8576
8577 /* An instance of this type is used to represent an exec catchpoint.
8578 A breakpoint is really of this type iff its ops pointer points to
8579 CATCH_EXEC_BREAKPOINT_OPS. */
8580
8581 struct exec_catchpoint : public breakpoint
8582 {
8583 ~exec_catchpoint () override;
8584
8585 /* Filename of a program whose exec triggered this catchpoint.
8586 This field is only valid immediately after this catchpoint has
8587 triggered. */
8588 char *exec_pathname;
8589 };
8590
8591 /* Exec catchpoint destructor. */
8592
8593 exec_catchpoint::~exec_catchpoint ()
8594 {
8595 xfree (this->exec_pathname);
8596 }
8597
8598 static int
8599 insert_catch_exec (struct bp_location *bl)
8600 {
8601 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8602 }
8603
8604 static int
8605 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8606 {
8607 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8608 }
8609
8610 static int
8611 breakpoint_hit_catch_exec (const struct bp_location *bl,
8612 struct address_space *aspace, CORE_ADDR bp_addr,
8613 const struct target_waitstatus *ws)
8614 {
8615 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8616
8617 if (ws->kind != TARGET_WAITKIND_EXECD)
8618 return 0;
8619
8620 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8621 return 1;
8622 }
8623
8624 static enum print_stop_action
8625 print_it_catch_exec (bpstat bs)
8626 {
8627 struct ui_out *uiout = current_uiout;
8628 struct breakpoint *b = bs->breakpoint_at;
8629 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8630
8631 annotate_catchpoint (b->number);
8632 maybe_print_thread_hit_breakpoint (uiout);
8633 if (b->disposition == disp_del)
8634 uiout->text ("Temporary catchpoint ");
8635 else
8636 uiout->text ("Catchpoint ");
8637 if (uiout->is_mi_like_p ())
8638 {
8639 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8640 uiout->field_string ("disp", bpdisp_text (b->disposition));
8641 }
8642 uiout->field_int ("bkptno", b->number);
8643 uiout->text (" (exec'd ");
8644 uiout->field_string ("new-exec", c->exec_pathname);
8645 uiout->text ("), ");
8646
8647 return PRINT_SRC_AND_LOC;
8648 }
8649
8650 static void
8651 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8652 {
8653 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8654 struct value_print_options opts;
8655 struct ui_out *uiout = current_uiout;
8656
8657 get_user_print_options (&opts);
8658
8659 /* Field 4, the address, is omitted (which makes the columns
8660 not line up too nicely with the headers, but the effect
8661 is relatively readable). */
8662 if (opts.addressprint)
8663 uiout->field_skip ("addr");
8664 annotate_field (5);
8665 uiout->text ("exec");
8666 if (c->exec_pathname != NULL)
8667 {
8668 uiout->text (", program \"");
8669 uiout->field_string ("what", c->exec_pathname);
8670 uiout->text ("\" ");
8671 }
8672
8673 if (uiout->is_mi_like_p ())
8674 uiout->field_string ("catch-type", "exec");
8675 }
8676
8677 static void
8678 print_mention_catch_exec (struct breakpoint *b)
8679 {
8680 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8681 }
8682
8683 /* Implement the "print_recreate" breakpoint_ops method for exec
8684 catchpoints. */
8685
8686 static void
8687 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8688 {
8689 fprintf_unfiltered (fp, "catch exec");
8690 print_recreate_thread (b, fp);
8691 }
8692
8693 static struct breakpoint_ops catch_exec_breakpoint_ops;
8694
8695 static int
8696 hw_breakpoint_used_count (void)
8697 {
8698 int i = 0;
8699 struct breakpoint *b;
8700 struct bp_location *bl;
8701
8702 ALL_BREAKPOINTS (b)
8703 {
8704 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8705 for (bl = b->loc; bl; bl = bl->next)
8706 {
8707 /* Special types of hardware breakpoints may use more than
8708 one register. */
8709 i += b->ops->resources_needed (bl);
8710 }
8711 }
8712
8713 return i;
8714 }
8715
8716 /* Returns the resources B would use if it were a hardware
8717 watchpoint. */
8718
8719 static int
8720 hw_watchpoint_use_count (struct breakpoint *b)
8721 {
8722 int i = 0;
8723 struct bp_location *bl;
8724
8725 if (!breakpoint_enabled (b))
8726 return 0;
8727
8728 for (bl = b->loc; bl; bl = bl->next)
8729 {
8730 /* Special types of hardware watchpoints may use more than
8731 one register. */
8732 i += b->ops->resources_needed (bl);
8733 }
8734
8735 return i;
8736 }
8737
8738 /* Returns the sum the used resources of all hardware watchpoints of
8739 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8740 the sum of the used resources of all hardware watchpoints of other
8741 types _not_ TYPE. */
8742
8743 static int
8744 hw_watchpoint_used_count_others (struct breakpoint *except,
8745 enum bptype type, int *other_type_used)
8746 {
8747 int i = 0;
8748 struct breakpoint *b;
8749
8750 *other_type_used = 0;
8751 ALL_BREAKPOINTS (b)
8752 {
8753 if (b == except)
8754 continue;
8755 if (!breakpoint_enabled (b))
8756 continue;
8757
8758 if (b->type == type)
8759 i += hw_watchpoint_use_count (b);
8760 else if (is_hardware_watchpoint (b))
8761 *other_type_used = 1;
8762 }
8763
8764 return i;
8765 }
8766
8767 void
8768 disable_watchpoints_before_interactive_call_start (void)
8769 {
8770 struct breakpoint *b;
8771
8772 ALL_BREAKPOINTS (b)
8773 {
8774 if (is_watchpoint (b) && breakpoint_enabled (b))
8775 {
8776 b->enable_state = bp_call_disabled;
8777 update_global_location_list (UGLL_DONT_INSERT);
8778 }
8779 }
8780 }
8781
8782 void
8783 enable_watchpoints_after_interactive_call_stop (void)
8784 {
8785 struct breakpoint *b;
8786
8787 ALL_BREAKPOINTS (b)
8788 {
8789 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8790 {
8791 b->enable_state = bp_enabled;
8792 update_global_location_list (UGLL_MAY_INSERT);
8793 }
8794 }
8795 }
8796
8797 void
8798 disable_breakpoints_before_startup (void)
8799 {
8800 current_program_space->executing_startup = 1;
8801 update_global_location_list (UGLL_DONT_INSERT);
8802 }
8803
8804 void
8805 enable_breakpoints_after_startup (void)
8806 {
8807 current_program_space->executing_startup = 0;
8808 breakpoint_re_set ();
8809 }
8810
8811 /* Create a new single-step breakpoint for thread THREAD, with no
8812 locations. */
8813
8814 static struct breakpoint *
8815 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8816 {
8817 struct breakpoint *b = new breakpoint ();
8818
8819 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8820 &momentary_breakpoint_ops);
8821
8822 b->disposition = disp_donttouch;
8823 b->frame_id = null_frame_id;
8824
8825 b->thread = thread;
8826 gdb_assert (b->thread != 0);
8827
8828 add_to_breakpoint_chain (b);
8829
8830 return b;
8831 }
8832
8833 /* Set a momentary breakpoint of type TYPE at address specified by
8834 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8835 frame. */
8836
8837 struct breakpoint *
8838 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8839 struct frame_id frame_id, enum bptype type)
8840 {
8841 struct breakpoint *b;
8842
8843 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8844 tail-called one. */
8845 gdb_assert (!frame_id_artificial_p (frame_id));
8846
8847 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8848 b->enable_state = bp_enabled;
8849 b->disposition = disp_donttouch;
8850 b->frame_id = frame_id;
8851
8852 /* If we're debugging a multi-threaded program, then we want
8853 momentary breakpoints to be active in only a single thread of
8854 control. */
8855 if (in_thread_list (inferior_ptid))
8856 b->thread = ptid_to_global_thread_id (inferior_ptid);
8857
8858 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8859
8860 return b;
8861 }
8862
8863 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8864 The new breakpoint will have type TYPE, use OPS as its
8865 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8866
8867 static struct breakpoint *
8868 momentary_breakpoint_from_master (struct breakpoint *orig,
8869 enum bptype type,
8870 const struct breakpoint_ops *ops,
8871 int loc_enabled)
8872 {
8873 struct breakpoint *copy;
8874
8875 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8876 copy->loc = allocate_bp_location (copy);
8877 set_breakpoint_location_function (copy->loc, 1);
8878
8879 copy->loc->gdbarch = orig->loc->gdbarch;
8880 copy->loc->requested_address = orig->loc->requested_address;
8881 copy->loc->address = orig->loc->address;
8882 copy->loc->section = orig->loc->section;
8883 copy->loc->pspace = orig->loc->pspace;
8884 copy->loc->probe = orig->loc->probe;
8885 copy->loc->line_number = orig->loc->line_number;
8886 copy->loc->symtab = orig->loc->symtab;
8887 copy->loc->enabled = loc_enabled;
8888 copy->frame_id = orig->frame_id;
8889 copy->thread = orig->thread;
8890 copy->pspace = orig->pspace;
8891
8892 copy->enable_state = bp_enabled;
8893 copy->disposition = disp_donttouch;
8894 copy->number = internal_breakpoint_number--;
8895
8896 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8897 return copy;
8898 }
8899
8900 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8901 ORIG is NULL. */
8902
8903 struct breakpoint *
8904 clone_momentary_breakpoint (struct breakpoint *orig)
8905 {
8906 /* If there's nothing to clone, then return nothing. */
8907 if (orig == NULL)
8908 return NULL;
8909
8910 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8911 }
8912
8913 struct breakpoint *
8914 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8915 enum bptype type)
8916 {
8917 struct symtab_and_line sal;
8918
8919 sal = find_pc_line (pc, 0);
8920 sal.pc = pc;
8921 sal.section = find_pc_overlay (pc);
8922 sal.explicit_pc = 1;
8923
8924 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8925 }
8926 \f
8927
8928 /* Tell the user we have just set a breakpoint B. */
8929
8930 static void
8931 mention (struct breakpoint *b)
8932 {
8933 b->ops->print_mention (b);
8934 if (current_uiout->is_mi_like_p ())
8935 return;
8936 printf_filtered ("\n");
8937 }
8938 \f
8939
8940 static int bp_loc_is_permanent (struct bp_location *loc);
8941
8942 static struct bp_location *
8943 add_location_to_breakpoint (struct breakpoint *b,
8944 const struct symtab_and_line *sal)
8945 {
8946 struct bp_location *loc, **tmp;
8947 CORE_ADDR adjusted_address;
8948 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8949
8950 if (loc_gdbarch == NULL)
8951 loc_gdbarch = b->gdbarch;
8952
8953 /* Adjust the breakpoint's address prior to allocating a location.
8954 Once we call allocate_bp_location(), that mostly uninitialized
8955 location will be placed on the location chain. Adjustment of the
8956 breakpoint may cause target_read_memory() to be called and we do
8957 not want its scan of the location chain to find a breakpoint and
8958 location that's only been partially initialized. */
8959 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8960 sal->pc, b->type);
8961
8962 /* Sort the locations by their ADDRESS. */
8963 loc = allocate_bp_location (b);
8964 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8965 tmp = &((*tmp)->next))
8966 ;
8967 loc->next = *tmp;
8968 *tmp = loc;
8969
8970 loc->requested_address = sal->pc;
8971 loc->address = adjusted_address;
8972 loc->pspace = sal->pspace;
8973 loc->probe.probe = sal->probe;
8974 loc->probe.objfile = sal->objfile;
8975 gdb_assert (loc->pspace != NULL);
8976 loc->section = sal->section;
8977 loc->gdbarch = loc_gdbarch;
8978 loc->line_number = sal->line;
8979 loc->symtab = sal->symtab;
8980
8981 set_breakpoint_location_function (loc,
8982 sal->explicit_pc || sal->explicit_line);
8983
8984 /* While by definition, permanent breakpoints are already present in the
8985 code, we don't mark the location as inserted. Normally one would expect
8986 that GDB could rely on that breakpoint instruction to stop the program,
8987 thus removing the need to insert its own breakpoint, except that executing
8988 the breakpoint instruction can kill the target instead of reporting a
8989 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8990 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8991 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8992 breakpoint be inserted normally results in QEMU knowing about the GDB
8993 breakpoint, and thus trap before the breakpoint instruction is executed.
8994 (If GDB later needs to continue execution past the permanent breakpoint,
8995 it manually increments the PC, thus avoiding executing the breakpoint
8996 instruction.) */
8997 if (bp_loc_is_permanent (loc))
8998 loc->permanent = 1;
8999
9000 return loc;
9001 }
9002 \f
9003
9004 /* See breakpoint.h. */
9005
9006 int
9007 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9008 {
9009 int len;
9010 CORE_ADDR addr;
9011 const gdb_byte *bpoint;
9012 gdb_byte *target_mem;
9013 struct cleanup *cleanup;
9014 int retval = 0;
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 cleanup = make_show_memory_breakpoints_cleanup (0);
9029
9030 if (target_read_memory (address, target_mem, len) == 0
9031 && memcmp (target_mem, bpoint, len) == 0)
9032 retval = 1;
9033
9034 do_cleanups (cleanup);
9035
9036 return retval;
9037 }
9038
9039 /* Return 1 if LOC is pointing to a permanent breakpoint,
9040 return 0 otherwise. */
9041
9042 static int
9043 bp_loc_is_permanent (struct bp_location *loc)
9044 {
9045 gdb_assert (loc != NULL);
9046
9047 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9048 attempt to read from the addresses the locations of these breakpoint types
9049 point to. program_breakpoint_here_p, below, will attempt to read
9050 memory. */
9051 if (!breakpoint_address_is_meaningful (loc->owner))
9052 return 0;
9053
9054 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9055 switch_to_program_space_and_thread (loc->pspace);
9056 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9057 }
9058
9059 /* Build a command list for the dprintf corresponding to the current
9060 settings of the dprintf style options. */
9061
9062 static void
9063 update_dprintf_command_list (struct breakpoint *b)
9064 {
9065 char *dprintf_args = b->extra_string;
9066 char *printf_line = NULL;
9067
9068 if (!dprintf_args)
9069 return;
9070
9071 dprintf_args = skip_spaces (dprintf_args);
9072
9073 /* Allow a comma, as it may have terminated a location, but don't
9074 insist on it. */
9075 if (*dprintf_args == ',')
9076 ++dprintf_args;
9077 dprintf_args = skip_spaces (dprintf_args);
9078
9079 if (*dprintf_args != '"')
9080 error (_("Bad format string, missing '\"'."));
9081
9082 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9083 printf_line = xstrprintf ("printf %s", dprintf_args);
9084 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9085 {
9086 if (!dprintf_function)
9087 error (_("No function supplied for dprintf call"));
9088
9089 if (dprintf_channel && strlen (dprintf_channel) > 0)
9090 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9091 dprintf_function,
9092 dprintf_channel,
9093 dprintf_args);
9094 else
9095 printf_line = xstrprintf ("call (void) %s (%s)",
9096 dprintf_function,
9097 dprintf_args);
9098 }
9099 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9100 {
9101 if (target_can_run_breakpoint_commands ())
9102 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9103 else
9104 {
9105 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9106 printf_line = xstrprintf ("printf %s", dprintf_args);
9107 }
9108 }
9109 else
9110 internal_error (__FILE__, __LINE__,
9111 _("Invalid dprintf style."));
9112
9113 gdb_assert (printf_line != NULL);
9114 /* Manufacture a printf sequence. */
9115 {
9116 struct command_line *printf_cmd_line = XNEW (struct command_line);
9117
9118 printf_cmd_line->control_type = simple_control;
9119 printf_cmd_line->body_count = 0;
9120 printf_cmd_line->body_list = NULL;
9121 printf_cmd_line->next = NULL;
9122 printf_cmd_line->line = printf_line;
9123
9124 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9125 }
9126 }
9127
9128 /* Update all dprintf commands, making their command lists reflect
9129 current style settings. */
9130
9131 static void
9132 update_dprintf_commands (char *args, int from_tty,
9133 struct cmd_list_element *c)
9134 {
9135 struct breakpoint *b;
9136
9137 ALL_BREAKPOINTS (b)
9138 {
9139 if (b->type == bp_dprintf)
9140 update_dprintf_command_list (b);
9141 }
9142 }
9143
9144 /* Create a breakpoint with SAL as location. Use LOCATION
9145 as a description of the location, and COND_STRING
9146 as condition expression. If LOCATION is NULL then create an
9147 "address location" from the address in the SAL. */
9148
9149 static void
9150 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9151 struct symtabs_and_lines sals,
9152 event_location_up &&location,
9153 gdb::unique_xmalloc_ptr<char> filter,
9154 gdb::unique_xmalloc_ptr<char> cond_string,
9155 gdb::unique_xmalloc_ptr<char> extra_string,
9156 enum bptype type, enum bpdisp disposition,
9157 int thread, int task, int ignore_count,
9158 const struct breakpoint_ops *ops, int from_tty,
9159 int enabled, int internal, unsigned flags,
9160 int display_canonical)
9161 {
9162 int i;
9163
9164 if (type == bp_hardware_breakpoint)
9165 {
9166 int target_resources_ok;
9167
9168 i = hw_breakpoint_used_count ();
9169 target_resources_ok =
9170 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9171 i + 1, 0);
9172 if (target_resources_ok == 0)
9173 error (_("No hardware breakpoint support in the target."));
9174 else if (target_resources_ok < 0)
9175 error (_("Hardware breakpoints used exceeds limit."));
9176 }
9177
9178 gdb_assert (sals.nelts > 0);
9179
9180 for (i = 0; i < sals.nelts; ++i)
9181 {
9182 struct symtab_and_line sal = sals.sals[i];
9183 struct bp_location *loc;
9184
9185 if (from_tty)
9186 {
9187 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9188 if (!loc_gdbarch)
9189 loc_gdbarch = gdbarch;
9190
9191 describe_other_breakpoints (loc_gdbarch,
9192 sal.pspace, sal.pc, sal.section, thread);
9193 }
9194
9195 if (i == 0)
9196 {
9197 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9198 b->thread = thread;
9199 b->task = task;
9200
9201 b->cond_string = cond_string.release ();
9202 b->extra_string = extra_string.release ();
9203 b->ignore_count = ignore_count;
9204 b->enable_state = enabled ? bp_enabled : bp_disabled;
9205 b->disposition = disposition;
9206
9207 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9208 b->loc->inserted = 1;
9209
9210 if (type == bp_static_tracepoint)
9211 {
9212 struct tracepoint *t = (struct tracepoint *) b;
9213 struct static_tracepoint_marker marker;
9214
9215 if (strace_marker_p (b))
9216 {
9217 /* We already know the marker exists, otherwise, we
9218 wouldn't see a sal for it. */
9219 const char *p
9220 = &event_location_to_string (b->location.get ())[3];
9221 const char *endp;
9222 char *marker_str;
9223
9224 p = skip_spaces_const (p);
9225
9226 endp = skip_to_space_const (p);
9227
9228 marker_str = savestring (p, endp - p);
9229 t->static_trace_marker_id = marker_str;
9230
9231 printf_filtered (_("Probed static tracepoint "
9232 "marker \"%s\"\n"),
9233 t->static_trace_marker_id);
9234 }
9235 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9236 {
9237 t->static_trace_marker_id = xstrdup (marker.str_id);
9238 release_static_tracepoint_marker (&marker);
9239
9240 printf_filtered (_("Probed static tracepoint "
9241 "marker \"%s\"\n"),
9242 t->static_trace_marker_id);
9243 }
9244 else
9245 warning (_("Couldn't determine the static "
9246 "tracepoint marker to probe"));
9247 }
9248
9249 loc = b->loc;
9250 }
9251 else
9252 {
9253 loc = add_location_to_breakpoint (b, &sal);
9254 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9255 loc->inserted = 1;
9256 }
9257
9258 if (b->cond_string)
9259 {
9260 const char *arg = b->cond_string;
9261
9262 loc->cond = parse_exp_1 (&arg, loc->address,
9263 block_for_pc (loc->address), 0);
9264 if (*arg)
9265 error (_("Garbage '%s' follows condition"), arg);
9266 }
9267
9268 /* Dynamic printf requires and uses additional arguments on the
9269 command line, otherwise it's an error. */
9270 if (type == bp_dprintf)
9271 {
9272 if (b->extra_string)
9273 update_dprintf_command_list (b);
9274 else
9275 error (_("Format string required"));
9276 }
9277 else if (b->extra_string)
9278 error (_("Garbage '%s' at end of command"), b->extra_string);
9279 }
9280
9281 b->display_canonical = display_canonical;
9282 if (location != NULL)
9283 b->location = std::move (location);
9284 else
9285 b->location = new_address_location (b->loc->address, NULL, 0);
9286 b->filter = filter.release ();
9287 }
9288
9289 static void
9290 create_breakpoint_sal (struct gdbarch *gdbarch,
9291 struct symtabs_and_lines sals,
9292 event_location_up &&location,
9293 gdb::unique_xmalloc_ptr<char> filter,
9294 gdb::unique_xmalloc_ptr<char> cond_string,
9295 gdb::unique_xmalloc_ptr<char> extra_string,
9296 enum bptype type, enum bpdisp disposition,
9297 int thread, int task, int ignore_count,
9298 const struct breakpoint_ops *ops, int from_tty,
9299 int enabled, int internal, unsigned flags,
9300 int display_canonical)
9301 {
9302 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9303
9304 init_breakpoint_sal (b.get (), gdbarch,
9305 sals, std::move (location),
9306 std::move (filter),
9307 std::move (cond_string),
9308 std::move (extra_string),
9309 type, disposition,
9310 thread, task, ignore_count,
9311 ops, from_tty,
9312 enabled, internal, flags,
9313 display_canonical);
9314
9315 install_breakpoint (internal, b.release (), 0);
9316 }
9317
9318 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9319 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9320 value. COND_STRING, if not NULL, specified the condition to be
9321 used for all breakpoints. Essentially the only case where
9322 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9323 function. In that case, it's still not possible to specify
9324 separate conditions for different overloaded functions, so
9325 we take just a single condition string.
9326
9327 NOTE: If the function succeeds, the caller is expected to cleanup
9328 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9329 array contents). If the function fails (error() is called), the
9330 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9331 COND and SALS arrays and each of those arrays contents. */
9332
9333 static void
9334 create_breakpoints_sal (struct gdbarch *gdbarch,
9335 struct linespec_result *canonical,
9336 gdb::unique_xmalloc_ptr<char> cond_string,
9337 gdb::unique_xmalloc_ptr<char> extra_string,
9338 enum bptype type, enum bpdisp disposition,
9339 int thread, int task, int ignore_count,
9340 const struct breakpoint_ops *ops, int from_tty,
9341 int enabled, int internal, unsigned flags)
9342 {
9343 int i;
9344 struct linespec_sals *lsal;
9345
9346 if (canonical->pre_expanded)
9347 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9348
9349 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9350 {
9351 /* Note that 'location' can be NULL in the case of a plain
9352 'break', without arguments. */
9353 event_location_up location
9354 = (canonical->location != NULL
9355 ? copy_event_location (canonical->location.get ()) : NULL);
9356 gdb::unique_xmalloc_ptr<char> filter_string
9357 (lsal->canonical != NULL ? xstrdup (lsal->canonical) : NULL);
9358
9359 create_breakpoint_sal (gdbarch, lsal->sals,
9360 std::move (location),
9361 std::move (filter_string),
9362 std::move (cond_string),
9363 std::move (extra_string),
9364 type, disposition,
9365 thread, task, ignore_count, ops,
9366 from_tty, enabled, internal, flags,
9367 canonical->special_display);
9368 }
9369 }
9370
9371 /* Parse LOCATION which is assumed to be a SAL specification possibly
9372 followed by conditionals. On return, SALS contains an array of SAL
9373 addresses found. LOCATION points to the end of the SAL (for
9374 linespec locations).
9375
9376 The array and the line spec strings are allocated on the heap, it is
9377 the caller's responsibility to free them. */
9378
9379 static void
9380 parse_breakpoint_sals (const struct event_location *location,
9381 struct linespec_result *canonical)
9382 {
9383 struct symtab_and_line cursal;
9384
9385 if (event_location_type (location) == LINESPEC_LOCATION)
9386 {
9387 const char *address = get_linespec_location (location);
9388
9389 if (address == NULL)
9390 {
9391 /* The last displayed codepoint, if it's valid, is our default
9392 breakpoint address. */
9393 if (last_displayed_sal_is_valid ())
9394 {
9395 struct linespec_sals lsal;
9396 struct symtab_and_line sal;
9397 CORE_ADDR pc;
9398
9399 init_sal (&sal); /* Initialize to zeroes. */
9400 lsal.sals.sals = XNEW (struct symtab_and_line);
9401
9402 /* Set sal's pspace, pc, symtab, and line to the values
9403 corresponding to the last call to print_frame_info.
9404 Be sure to reinitialize LINE with NOTCURRENT == 0
9405 as the breakpoint line number is inappropriate otherwise.
9406 find_pc_line would adjust PC, re-set it back. */
9407 get_last_displayed_sal (&sal);
9408 pc = sal.pc;
9409 sal = find_pc_line (pc, 0);
9410
9411 /* "break" without arguments is equivalent to "break *PC"
9412 where PC is the last displayed codepoint's address. So
9413 make sure to set sal.explicit_pc to prevent GDB from
9414 trying to expand the list of sals to include all other
9415 instances with the same symtab and line. */
9416 sal.pc = pc;
9417 sal.explicit_pc = 1;
9418
9419 lsal.sals.sals[0] = sal;
9420 lsal.sals.nelts = 1;
9421 lsal.canonical = NULL;
9422
9423 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9424 return;
9425 }
9426 else
9427 error (_("No default breakpoint address now."));
9428 }
9429 }
9430
9431 /* Force almost all breakpoints to be in terms of the
9432 current_source_symtab (which is decode_line_1's default).
9433 This should produce the results we want almost all of the
9434 time while leaving default_breakpoint_* alone.
9435
9436 ObjC: However, don't match an Objective-C method name which
9437 may have a '+' or '-' succeeded by a '['. */
9438 cursal = get_current_source_symtab_and_line ();
9439 if (last_displayed_sal_is_valid ())
9440 {
9441 const char *address = NULL;
9442
9443 if (event_location_type (location) == LINESPEC_LOCATION)
9444 address = get_linespec_location (location);
9445
9446 if (!cursal.symtab
9447 || (address != NULL
9448 && strchr ("+-", address[0]) != NULL
9449 && address[1] != '['))
9450 {
9451 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9452 get_last_displayed_symtab (),
9453 get_last_displayed_line (),
9454 canonical, NULL, NULL);
9455 return;
9456 }
9457 }
9458
9459 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9460 cursal.symtab, cursal.line, canonical, NULL, NULL);
9461 }
9462
9463
9464 /* Convert each SAL into a real PC. Verify that the PC can be
9465 inserted as a breakpoint. If it can't throw an error. */
9466
9467 static void
9468 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9469 {
9470 int i;
9471
9472 for (i = 0; i < sals->nelts; i++)
9473 resolve_sal_pc (&sals->sals[i]);
9474 }
9475
9476 /* Fast tracepoints may have restrictions on valid locations. For
9477 instance, a fast tracepoint using a jump instead of a trap will
9478 likely have to overwrite more bytes than a trap would, and so can
9479 only be placed where the instruction is longer than the jump, or a
9480 multi-instruction sequence does not have a jump into the middle of
9481 it, etc. */
9482
9483 static void
9484 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9485 struct symtabs_and_lines *sals)
9486 {
9487 int i, rslt;
9488 struct symtab_and_line *sal;
9489 char *msg;
9490 struct cleanup *old_chain;
9491
9492 for (i = 0; i < sals->nelts; i++)
9493 {
9494 struct gdbarch *sarch;
9495
9496 sal = &sals->sals[i];
9497
9498 sarch = get_sal_arch (*sal);
9499 /* We fall back to GDBARCH if there is no architecture
9500 associated with SAL. */
9501 if (sarch == NULL)
9502 sarch = gdbarch;
9503 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9504 old_chain = make_cleanup (xfree, msg);
9505
9506 if (!rslt)
9507 error (_("May not have a fast tracepoint at %s%s"),
9508 paddress (sarch, sal->pc), (msg ? msg : ""));
9509
9510 do_cleanups (old_chain);
9511 }
9512 }
9513
9514 /* Given TOK, a string specification of condition and thread, as
9515 accepted by the 'break' command, extract the condition
9516 string and thread number and set *COND_STRING and *THREAD.
9517 PC identifies the context at which the condition should be parsed.
9518 If no condition is found, *COND_STRING is set to NULL.
9519 If no thread is found, *THREAD is set to -1. */
9520
9521 static void
9522 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9523 char **cond_string, int *thread, int *task,
9524 char **rest)
9525 {
9526 *cond_string = NULL;
9527 *thread = -1;
9528 *task = 0;
9529 *rest = NULL;
9530
9531 while (tok && *tok)
9532 {
9533 const char *end_tok;
9534 int toklen;
9535 const char *cond_start = NULL;
9536 const char *cond_end = NULL;
9537
9538 tok = skip_spaces_const (tok);
9539
9540 if ((*tok == '"' || *tok == ',') && rest)
9541 {
9542 *rest = savestring (tok, strlen (tok));
9543 return;
9544 }
9545
9546 end_tok = skip_to_space_const (tok);
9547
9548 toklen = end_tok - tok;
9549
9550 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9551 {
9552 tok = cond_start = end_tok + 1;
9553 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9554 cond_end = tok;
9555 *cond_string = savestring (cond_start, cond_end - cond_start);
9556 }
9557 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9558 {
9559 const char *tmptok;
9560 struct thread_info *thr;
9561
9562 tok = end_tok + 1;
9563 thr = parse_thread_id (tok, &tmptok);
9564 if (tok == tmptok)
9565 error (_("Junk after thread keyword."));
9566 *thread = thr->global_num;
9567 tok = tmptok;
9568 }
9569 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9570 {
9571 char *tmptok;
9572
9573 tok = end_tok + 1;
9574 *task = strtol (tok, &tmptok, 0);
9575 if (tok == tmptok)
9576 error (_("Junk after task keyword."));
9577 if (!valid_task_id (*task))
9578 error (_("Unknown task %d."), *task);
9579 tok = tmptok;
9580 }
9581 else if (rest)
9582 {
9583 *rest = savestring (tok, strlen (tok));
9584 return;
9585 }
9586 else
9587 error (_("Junk at end of arguments."));
9588 }
9589 }
9590
9591 /* Decode a static tracepoint marker spec. */
9592
9593 static struct symtabs_and_lines
9594 decode_static_tracepoint_spec (const char **arg_p)
9595 {
9596 VEC(static_tracepoint_marker_p) *markers = NULL;
9597 struct symtabs_and_lines sals;
9598 struct cleanup *old_chain;
9599 const char *p = &(*arg_p)[3];
9600 const char *endp;
9601 char *marker_str;
9602 int i;
9603
9604 p = skip_spaces_const (p);
9605
9606 endp = skip_to_space_const (p);
9607
9608 marker_str = savestring (p, endp - p);
9609 old_chain = make_cleanup (xfree, marker_str);
9610
9611 markers = target_static_tracepoint_markers_by_strid (marker_str);
9612 if (VEC_empty(static_tracepoint_marker_p, markers))
9613 error (_("No known static tracepoint marker named %s"), marker_str);
9614
9615 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9616 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9617
9618 for (i = 0; i < sals.nelts; i++)
9619 {
9620 struct static_tracepoint_marker *marker;
9621
9622 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9623
9624 init_sal (&sals.sals[i]);
9625
9626 sals.sals[i] = find_pc_line (marker->address, 0);
9627 sals.sals[i].pc = marker->address;
9628
9629 release_static_tracepoint_marker (marker);
9630 }
9631
9632 do_cleanups (old_chain);
9633
9634 *arg_p = endp;
9635 return sals;
9636 }
9637
9638 /* See breakpoint.h. */
9639
9640 int
9641 create_breakpoint (struct gdbarch *gdbarch,
9642 const struct event_location *location,
9643 const char *cond_string,
9644 int thread, const char *extra_string,
9645 int parse_extra,
9646 int tempflag, enum bptype type_wanted,
9647 int ignore_count,
9648 enum auto_boolean pending_break_support,
9649 const struct breakpoint_ops *ops,
9650 int from_tty, int enabled, int internal,
9651 unsigned flags)
9652 {
9653 struct linespec_result canonical;
9654 struct cleanup *bkpt_chain = NULL;
9655 int pending = 0;
9656 int task = 0;
9657 int prev_bkpt_count = breakpoint_count;
9658
9659 gdb_assert (ops != NULL);
9660
9661 /* If extra_string isn't useful, set it to NULL. */
9662 if (extra_string != NULL && *extra_string == '\0')
9663 extra_string = NULL;
9664
9665 TRY
9666 {
9667 ops->create_sals_from_location (location, &canonical, type_wanted);
9668 }
9669 CATCH (e, RETURN_MASK_ERROR)
9670 {
9671 /* If caller is interested in rc value from parse, set
9672 value. */
9673 if (e.error == NOT_FOUND_ERROR)
9674 {
9675 /* If pending breakpoint support is turned off, throw
9676 error. */
9677
9678 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9679 throw_exception (e);
9680
9681 exception_print (gdb_stderr, e);
9682
9683 /* If pending breakpoint support is auto query and the user
9684 selects no, then simply return the error code. */
9685 if (pending_break_support == AUTO_BOOLEAN_AUTO
9686 && !nquery (_("Make %s pending on future shared library load? "),
9687 bptype_string (type_wanted)))
9688 return 0;
9689
9690 /* At this point, either the user was queried about setting
9691 a pending breakpoint and selected yes, or pending
9692 breakpoint behavior is on and thus a pending breakpoint
9693 is defaulted on behalf of the user. */
9694 pending = 1;
9695 }
9696 else
9697 throw_exception (e);
9698 }
9699 END_CATCH
9700
9701 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9702 return 0;
9703
9704 /* ----------------------------- SNIP -----------------------------
9705 Anything added to the cleanup chain beyond this point is assumed
9706 to be part of a breakpoint. If the breakpoint create succeeds
9707 then the memory is not reclaimed. */
9708 bkpt_chain = make_cleanup (null_cleanup, 0);
9709
9710 /* Resolve all line numbers to PC's and verify that the addresses
9711 are ok for the target. */
9712 if (!pending)
9713 {
9714 int ix;
9715 struct linespec_sals *iter;
9716
9717 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9718 breakpoint_sals_to_pc (&iter->sals);
9719 }
9720
9721 /* Fast tracepoints may have additional restrictions on location. */
9722 if (!pending && type_wanted == bp_fast_tracepoint)
9723 {
9724 int ix;
9725 struct linespec_sals *iter;
9726
9727 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9728 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9729 }
9730
9731 /* Verify that condition can be parsed, before setting any
9732 breakpoints. Allocate a separate condition expression for each
9733 breakpoint. */
9734 if (!pending)
9735 {
9736 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9737 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9738
9739 if (parse_extra)
9740 {
9741 char *rest;
9742 char *cond;
9743 struct linespec_sals *lsal;
9744
9745 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9746
9747 /* Here we only parse 'arg' to separate condition
9748 from thread number, so parsing in context of first
9749 sal is OK. When setting the breakpoint we'll
9750 re-parse it in context of each sal. */
9751
9752 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9753 &cond, &thread, &task, &rest);
9754 cond_string_copy.reset (cond);
9755 extra_string_copy.reset (rest);
9756 }
9757 else
9758 {
9759 if (type_wanted != bp_dprintf
9760 && extra_string != NULL && *extra_string != '\0')
9761 error (_("Garbage '%s' at end of location"), extra_string);
9762
9763 /* Create a private copy of condition string. */
9764 if (cond_string)
9765 cond_string_copy.reset (xstrdup (cond_string));
9766 /* Create a private copy of any extra string. */
9767 if (extra_string)
9768 extra_string_copy.reset (xstrdup (extra_string));
9769 }
9770
9771 ops->create_breakpoints_sal (gdbarch, &canonical,
9772 std::move (cond_string_copy),
9773 std::move (extra_string_copy),
9774 type_wanted,
9775 tempflag ? disp_del : disp_donttouch,
9776 thread, task, ignore_count, ops,
9777 from_tty, enabled, internal, flags);
9778 }
9779 else
9780 {
9781 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9782
9783 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9784 b->location = copy_event_location (location);
9785
9786 if (parse_extra)
9787 b->cond_string = NULL;
9788 else
9789 {
9790 /* Create a private copy of condition string. */
9791 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
9792 b->thread = thread;
9793 }
9794
9795 /* Create a private copy of any extra string. */
9796 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
9797 b->ignore_count = ignore_count;
9798 b->disposition = tempflag ? disp_del : disp_donttouch;
9799 b->condition_not_parsed = 1;
9800 b->enable_state = enabled ? bp_enabled : bp_disabled;
9801 if ((type_wanted != bp_breakpoint
9802 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9803 b->pspace = current_program_space;
9804
9805 install_breakpoint (internal, b.release (), 0);
9806 }
9807
9808 if (VEC_length (linespec_sals, canonical.sals) > 1)
9809 {
9810 warning (_("Multiple breakpoints were set.\nUse the "
9811 "\"delete\" command to delete unwanted breakpoints."));
9812 prev_breakpoint_count = prev_bkpt_count;
9813 }
9814
9815 /* That's it. Discard the cleanups for data inserted into the
9816 breakpoint. */
9817 discard_cleanups (bkpt_chain);
9818
9819 /* error call may happen here - have BKPT_CHAIN already discarded. */
9820 update_global_location_list (UGLL_MAY_INSERT);
9821
9822 return 1;
9823 }
9824
9825 /* Set a breakpoint.
9826 ARG is a string describing breakpoint address,
9827 condition, and thread.
9828 FLAG specifies if a breakpoint is hardware on,
9829 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9830 and BP_TEMPFLAG. */
9831
9832 static void
9833 break_command_1 (char *arg, int flag, int from_tty)
9834 {
9835 int tempflag = flag & BP_TEMPFLAG;
9836 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9837 ? bp_hardware_breakpoint
9838 : bp_breakpoint);
9839 struct breakpoint_ops *ops;
9840
9841 event_location_up location = string_to_event_location (&arg, current_language);
9842
9843 /* Matching breakpoints on probes. */
9844 if (location != NULL
9845 && event_location_type (location.get ()) == PROBE_LOCATION)
9846 ops = &bkpt_probe_breakpoint_ops;
9847 else
9848 ops = &bkpt_breakpoint_ops;
9849
9850 create_breakpoint (get_current_arch (),
9851 location.get (),
9852 NULL, 0, arg, 1 /* parse arg */,
9853 tempflag, type_wanted,
9854 0 /* Ignore count */,
9855 pending_break_support,
9856 ops,
9857 from_tty,
9858 1 /* enabled */,
9859 0 /* internal */,
9860 0);
9861 }
9862
9863 /* Helper function for break_command_1 and disassemble_command. */
9864
9865 void
9866 resolve_sal_pc (struct symtab_and_line *sal)
9867 {
9868 CORE_ADDR pc;
9869
9870 if (sal->pc == 0 && sal->symtab != NULL)
9871 {
9872 if (!find_line_pc (sal->symtab, sal->line, &pc))
9873 error (_("No line %d in file \"%s\"."),
9874 sal->line, symtab_to_filename_for_display (sal->symtab));
9875 sal->pc = pc;
9876
9877 /* If this SAL corresponds to a breakpoint inserted using a line
9878 number, then skip the function prologue if necessary. */
9879 if (sal->explicit_line)
9880 skip_prologue_sal (sal);
9881 }
9882
9883 if (sal->section == 0 && sal->symtab != NULL)
9884 {
9885 const struct blockvector *bv;
9886 const struct block *b;
9887 struct symbol *sym;
9888
9889 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9890 SYMTAB_COMPUNIT (sal->symtab));
9891 if (bv != NULL)
9892 {
9893 sym = block_linkage_function (b);
9894 if (sym != NULL)
9895 {
9896 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9897 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9898 sym);
9899 }
9900 else
9901 {
9902 /* It really is worthwhile to have the section, so we'll
9903 just have to look harder. This case can be executed
9904 if we have line numbers but no functions (as can
9905 happen in assembly source). */
9906
9907 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9908 switch_to_program_space_and_thread (sal->pspace);
9909
9910 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9911 if (msym.minsym)
9912 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9913 }
9914 }
9915 }
9916 }
9917
9918 void
9919 break_command (char *arg, int from_tty)
9920 {
9921 break_command_1 (arg, 0, from_tty);
9922 }
9923
9924 void
9925 tbreak_command (char *arg, int from_tty)
9926 {
9927 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9928 }
9929
9930 static void
9931 hbreak_command (char *arg, int from_tty)
9932 {
9933 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9934 }
9935
9936 static void
9937 thbreak_command (char *arg, int from_tty)
9938 {
9939 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9940 }
9941
9942 static void
9943 stop_command (char *arg, int from_tty)
9944 {
9945 printf_filtered (_("Specify the type of breakpoint to set.\n\
9946 Usage: stop in <function | address>\n\
9947 stop at <line>\n"));
9948 }
9949
9950 static void
9951 stopin_command (char *arg, int from_tty)
9952 {
9953 int badInput = 0;
9954
9955 if (arg == (char *) NULL)
9956 badInput = 1;
9957 else if (*arg != '*')
9958 {
9959 char *argptr = arg;
9960 int hasColon = 0;
9961
9962 /* Look for a ':'. If this is a line number specification, then
9963 say it is bad, otherwise, it should be an address or
9964 function/method name. */
9965 while (*argptr && !hasColon)
9966 {
9967 hasColon = (*argptr == ':');
9968 argptr++;
9969 }
9970
9971 if (hasColon)
9972 badInput = (*argptr != ':'); /* Not a class::method */
9973 else
9974 badInput = isdigit (*arg); /* a simple line number */
9975 }
9976
9977 if (badInput)
9978 printf_filtered (_("Usage: stop in <function | address>\n"));
9979 else
9980 break_command_1 (arg, 0, from_tty);
9981 }
9982
9983 static void
9984 stopat_command (char *arg, int from_tty)
9985 {
9986 int badInput = 0;
9987
9988 if (arg == (char *) NULL || *arg == '*') /* no line number */
9989 badInput = 1;
9990 else
9991 {
9992 char *argptr = arg;
9993 int hasColon = 0;
9994
9995 /* Look for a ':'. If there is a '::' then get out, otherwise
9996 it is probably a line number. */
9997 while (*argptr && !hasColon)
9998 {
9999 hasColon = (*argptr == ':');
10000 argptr++;
10001 }
10002
10003 if (hasColon)
10004 badInput = (*argptr == ':'); /* we have class::method */
10005 else
10006 badInput = !isdigit (*arg); /* not a line number */
10007 }
10008
10009 if (badInput)
10010 printf_filtered (_("Usage: stop at <line>\n"));
10011 else
10012 break_command_1 (arg, 0, from_tty);
10013 }
10014
10015 /* The dynamic printf command is mostly like a regular breakpoint, but
10016 with a prewired command list consisting of a single output command,
10017 built from extra arguments supplied on the dprintf command
10018 line. */
10019
10020 static void
10021 dprintf_command (char *arg, int from_tty)
10022 {
10023 event_location_up location = string_to_event_location (&arg, current_language);
10024
10025 /* If non-NULL, ARG should have been advanced past the location;
10026 the next character must be ','. */
10027 if (arg != NULL)
10028 {
10029 if (arg[0] != ',' || arg[1] == '\0')
10030 error (_("Format string required"));
10031 else
10032 {
10033 /* Skip the comma. */
10034 ++arg;
10035 }
10036 }
10037
10038 create_breakpoint (get_current_arch (),
10039 location.get (),
10040 NULL, 0, arg, 1 /* parse arg */,
10041 0, bp_dprintf,
10042 0 /* Ignore count */,
10043 pending_break_support,
10044 &dprintf_breakpoint_ops,
10045 from_tty,
10046 1 /* enabled */,
10047 0 /* internal */,
10048 0);
10049 }
10050
10051 static void
10052 agent_printf_command (char *arg, int from_tty)
10053 {
10054 error (_("May only run agent-printf on the target"));
10055 }
10056
10057 /* Implement the "breakpoint_hit" breakpoint_ops method for
10058 ranged breakpoints. */
10059
10060 static int
10061 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10062 struct address_space *aspace,
10063 CORE_ADDR bp_addr,
10064 const struct target_waitstatus *ws)
10065 {
10066 if (ws->kind != TARGET_WAITKIND_STOPPED
10067 || ws->value.sig != GDB_SIGNAL_TRAP)
10068 return 0;
10069
10070 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10071 bl->length, aspace, bp_addr);
10072 }
10073
10074 /* Implement the "resources_needed" breakpoint_ops method for
10075 ranged breakpoints. */
10076
10077 static int
10078 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10079 {
10080 return target_ranged_break_num_registers ();
10081 }
10082
10083 /* Implement the "print_it" breakpoint_ops method for
10084 ranged breakpoints. */
10085
10086 static enum print_stop_action
10087 print_it_ranged_breakpoint (bpstat bs)
10088 {
10089 struct breakpoint *b = bs->breakpoint_at;
10090 struct bp_location *bl = b->loc;
10091 struct ui_out *uiout = current_uiout;
10092
10093 gdb_assert (b->type == bp_hardware_breakpoint);
10094
10095 /* Ranged breakpoints have only one location. */
10096 gdb_assert (bl && bl->next == NULL);
10097
10098 annotate_breakpoint (b->number);
10099
10100 maybe_print_thread_hit_breakpoint (uiout);
10101
10102 if (b->disposition == disp_del)
10103 uiout->text ("Temporary ranged breakpoint ");
10104 else
10105 uiout->text ("Ranged breakpoint ");
10106 if (uiout->is_mi_like_p ())
10107 {
10108 uiout->field_string ("reason",
10109 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10110 uiout->field_string ("disp", bpdisp_text (b->disposition));
10111 }
10112 uiout->field_int ("bkptno", b->number);
10113 uiout->text (", ");
10114
10115 return PRINT_SRC_AND_LOC;
10116 }
10117
10118 /* Implement the "print_one" breakpoint_ops method for
10119 ranged breakpoints. */
10120
10121 static void
10122 print_one_ranged_breakpoint (struct breakpoint *b,
10123 struct bp_location **last_loc)
10124 {
10125 struct bp_location *bl = b->loc;
10126 struct value_print_options opts;
10127 struct ui_out *uiout = current_uiout;
10128
10129 /* Ranged breakpoints have only one location. */
10130 gdb_assert (bl && bl->next == NULL);
10131
10132 get_user_print_options (&opts);
10133
10134 if (opts.addressprint)
10135 /* We don't print the address range here, it will be printed later
10136 by print_one_detail_ranged_breakpoint. */
10137 uiout->field_skip ("addr");
10138 annotate_field (5);
10139 print_breakpoint_location (b, bl);
10140 *last_loc = bl;
10141 }
10142
10143 /* Implement the "print_one_detail" breakpoint_ops method for
10144 ranged breakpoints. */
10145
10146 static void
10147 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10148 struct ui_out *uiout)
10149 {
10150 CORE_ADDR address_start, address_end;
10151 struct bp_location *bl = b->loc;
10152 string_file stb;
10153
10154 gdb_assert (bl);
10155
10156 address_start = bl->address;
10157 address_end = address_start + bl->length - 1;
10158
10159 uiout->text ("\taddress range: ");
10160 stb.printf ("[%s, %s]",
10161 print_core_address (bl->gdbarch, address_start),
10162 print_core_address (bl->gdbarch, address_end));
10163 uiout->field_stream ("addr", stb);
10164 uiout->text ("\n");
10165 }
10166
10167 /* Implement the "print_mention" breakpoint_ops method for
10168 ranged breakpoints. */
10169
10170 static void
10171 print_mention_ranged_breakpoint (struct breakpoint *b)
10172 {
10173 struct bp_location *bl = b->loc;
10174 struct ui_out *uiout = current_uiout;
10175
10176 gdb_assert (bl);
10177 gdb_assert (b->type == bp_hardware_breakpoint);
10178
10179 if (uiout->is_mi_like_p ())
10180 return;
10181
10182 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10183 b->number, paddress (bl->gdbarch, bl->address),
10184 paddress (bl->gdbarch, bl->address + bl->length - 1));
10185 }
10186
10187 /* Implement the "print_recreate" breakpoint_ops method for
10188 ranged breakpoints. */
10189
10190 static void
10191 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10192 {
10193 fprintf_unfiltered (fp, "break-range %s, %s",
10194 event_location_to_string (b->location.get ()),
10195 event_location_to_string (b->location_range_end.get ()));
10196 print_recreate_thread (b, fp);
10197 }
10198
10199 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10200
10201 static struct breakpoint_ops ranged_breakpoint_ops;
10202
10203 /* Find the address where the end of the breakpoint range should be
10204 placed, given the SAL of the end of the range. This is so that if
10205 the user provides a line number, the end of the range is set to the
10206 last instruction of the given line. */
10207
10208 static CORE_ADDR
10209 find_breakpoint_range_end (struct symtab_and_line sal)
10210 {
10211 CORE_ADDR end;
10212
10213 /* If the user provided a PC value, use it. Otherwise,
10214 find the address of the end of the given location. */
10215 if (sal.explicit_pc)
10216 end = sal.pc;
10217 else
10218 {
10219 int ret;
10220 CORE_ADDR start;
10221
10222 ret = find_line_pc_range (sal, &start, &end);
10223 if (!ret)
10224 error (_("Could not find location of the end of the range."));
10225
10226 /* find_line_pc_range returns the start of the next line. */
10227 end--;
10228 }
10229
10230 return end;
10231 }
10232
10233 /* Implement the "break-range" CLI command. */
10234
10235 static void
10236 break_range_command (char *arg, int from_tty)
10237 {
10238 char *arg_start, *addr_string_start;
10239 struct linespec_result canonical_start, canonical_end;
10240 int bp_count, can_use_bp, length;
10241 CORE_ADDR end;
10242 struct breakpoint *b;
10243 struct symtab_and_line sal_start, sal_end;
10244 struct cleanup *cleanup_bkpt;
10245 struct linespec_sals *lsal_start, *lsal_end;
10246
10247 /* We don't support software ranged breakpoints. */
10248 if (target_ranged_break_num_registers () < 0)
10249 error (_("This target does not support hardware ranged breakpoints."));
10250
10251 bp_count = hw_breakpoint_used_count ();
10252 bp_count += target_ranged_break_num_registers ();
10253 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10254 bp_count, 0);
10255 if (can_use_bp < 0)
10256 error (_("Hardware breakpoints used exceeds limit."));
10257
10258 arg = skip_spaces (arg);
10259 if (arg == NULL || arg[0] == '\0')
10260 error(_("No address range specified."));
10261
10262 arg_start = arg;
10263 event_location_up start_location = string_to_event_location (&arg,
10264 current_language);
10265 parse_breakpoint_sals (start_location.get (), &canonical_start);
10266
10267 if (arg[0] != ',')
10268 error (_("Too few arguments."));
10269 else if (VEC_empty (linespec_sals, canonical_start.sals))
10270 error (_("Could not find location of the beginning of the range."));
10271
10272 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10273
10274 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10275 || lsal_start->sals.nelts != 1)
10276 error (_("Cannot create a ranged breakpoint with multiple locations."));
10277
10278 sal_start = lsal_start->sals.sals[0];
10279 addr_string_start = savestring (arg_start, arg - arg_start);
10280 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10281
10282 arg++; /* Skip the comma. */
10283 arg = skip_spaces (arg);
10284
10285 /* Parse the end location. */
10286
10287 arg_start = arg;
10288
10289 /* We call decode_line_full directly here instead of using
10290 parse_breakpoint_sals because we need to specify the start location's
10291 symtab and line as the default symtab and line for the end of the
10292 range. This makes it possible to have ranges like "foo.c:27, +14",
10293 where +14 means 14 lines from the start location. */
10294 event_location_up end_location = string_to_event_location (&arg,
10295 current_language);
10296 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10297 sal_start.symtab, sal_start.line,
10298 &canonical_end, NULL, NULL);
10299
10300 if (VEC_empty (linespec_sals, canonical_end.sals))
10301 error (_("Could not find location of the end of the range."));
10302
10303 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10304 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10305 || lsal_end->sals.nelts != 1)
10306 error (_("Cannot create a ranged breakpoint with multiple locations."));
10307
10308 sal_end = lsal_end->sals.sals[0];
10309
10310 end = find_breakpoint_range_end (sal_end);
10311 if (sal_start.pc > end)
10312 error (_("Invalid address range, end precedes start."));
10313
10314 length = end - sal_start.pc + 1;
10315 if (length < 0)
10316 /* Length overflowed. */
10317 error (_("Address range too large."));
10318 else if (length == 1)
10319 {
10320 /* This range is simple enough to be handled by
10321 the `hbreak' command. */
10322 hbreak_command (addr_string_start, 1);
10323
10324 do_cleanups (cleanup_bkpt);
10325
10326 return;
10327 }
10328
10329 /* Now set up the breakpoint. */
10330 b = set_raw_breakpoint (get_current_arch (), sal_start,
10331 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10332 set_breakpoint_count (breakpoint_count + 1);
10333 b->number = breakpoint_count;
10334 b->disposition = disp_donttouch;
10335 b->location = std::move (start_location);
10336 b->location_range_end = std::move (end_location);
10337 b->loc->length = length;
10338
10339 do_cleanups (cleanup_bkpt);
10340
10341 mention (b);
10342 observer_notify_breakpoint_created (b);
10343 update_global_location_list (UGLL_MAY_INSERT);
10344 }
10345
10346 /* Return non-zero if EXP is verified as constant. Returned zero
10347 means EXP is variable. Also the constant detection may fail for
10348 some constant expressions and in such case still falsely return
10349 zero. */
10350
10351 static int
10352 watchpoint_exp_is_const (const struct expression *exp)
10353 {
10354 int i = exp->nelts;
10355
10356 while (i > 0)
10357 {
10358 int oplenp, argsp;
10359
10360 /* We are only interested in the descriptor of each element. */
10361 operator_length (exp, i, &oplenp, &argsp);
10362 i -= oplenp;
10363
10364 switch (exp->elts[i].opcode)
10365 {
10366 case BINOP_ADD:
10367 case BINOP_SUB:
10368 case BINOP_MUL:
10369 case BINOP_DIV:
10370 case BINOP_REM:
10371 case BINOP_MOD:
10372 case BINOP_LSH:
10373 case BINOP_RSH:
10374 case BINOP_LOGICAL_AND:
10375 case BINOP_LOGICAL_OR:
10376 case BINOP_BITWISE_AND:
10377 case BINOP_BITWISE_IOR:
10378 case BINOP_BITWISE_XOR:
10379 case BINOP_EQUAL:
10380 case BINOP_NOTEQUAL:
10381 case BINOP_LESS:
10382 case BINOP_GTR:
10383 case BINOP_LEQ:
10384 case BINOP_GEQ:
10385 case BINOP_REPEAT:
10386 case BINOP_COMMA:
10387 case BINOP_EXP:
10388 case BINOP_MIN:
10389 case BINOP_MAX:
10390 case BINOP_INTDIV:
10391 case BINOP_CONCAT:
10392 case TERNOP_COND:
10393 case TERNOP_SLICE:
10394
10395 case OP_LONG:
10396 case OP_DOUBLE:
10397 case OP_DECFLOAT:
10398 case OP_LAST:
10399 case OP_COMPLEX:
10400 case OP_STRING:
10401 case OP_ARRAY:
10402 case OP_TYPE:
10403 case OP_TYPEOF:
10404 case OP_DECLTYPE:
10405 case OP_TYPEID:
10406 case OP_NAME:
10407 case OP_OBJC_NSSTRING:
10408
10409 case UNOP_NEG:
10410 case UNOP_LOGICAL_NOT:
10411 case UNOP_COMPLEMENT:
10412 case UNOP_ADDR:
10413 case UNOP_HIGH:
10414 case UNOP_CAST:
10415
10416 case UNOP_CAST_TYPE:
10417 case UNOP_REINTERPRET_CAST:
10418 case UNOP_DYNAMIC_CAST:
10419 /* Unary, binary and ternary operators: We have to check
10420 their operands. If they are constant, then so is the
10421 result of that operation. For instance, if A and B are
10422 determined to be constants, then so is "A + B".
10423
10424 UNOP_IND is one exception to the rule above, because the
10425 value of *ADDR is not necessarily a constant, even when
10426 ADDR is. */
10427 break;
10428
10429 case OP_VAR_VALUE:
10430 /* Check whether the associated symbol is a constant.
10431
10432 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10433 possible that a buggy compiler could mark a variable as
10434 constant even when it is not, and TYPE_CONST would return
10435 true in this case, while SYMBOL_CLASS wouldn't.
10436
10437 We also have to check for function symbols because they
10438 are always constant. */
10439 {
10440 struct symbol *s = exp->elts[i + 2].symbol;
10441
10442 if (SYMBOL_CLASS (s) != LOC_BLOCK
10443 && SYMBOL_CLASS (s) != LOC_CONST
10444 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10445 return 0;
10446 break;
10447 }
10448
10449 /* The default action is to return 0 because we are using
10450 the optimistic approach here: If we don't know something,
10451 then it is not a constant. */
10452 default:
10453 return 0;
10454 }
10455 }
10456
10457 return 1;
10458 }
10459
10460 /* Watchpoint destructor. */
10461
10462 watchpoint::~watchpoint ()
10463 {
10464 xfree (this->exp_string);
10465 xfree (this->exp_string_reparse);
10466 value_free (this->val);
10467 }
10468
10469 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10470
10471 static void
10472 re_set_watchpoint (struct breakpoint *b)
10473 {
10474 struct watchpoint *w = (struct watchpoint *) b;
10475
10476 /* Watchpoint can be either on expression using entirely global
10477 variables, or it can be on local variables.
10478
10479 Watchpoints of the first kind are never auto-deleted, and even
10480 persist across program restarts. Since they can use variables
10481 from shared libraries, we need to reparse expression as libraries
10482 are loaded and unloaded.
10483
10484 Watchpoints on local variables can also change meaning as result
10485 of solib event. For example, if a watchpoint uses both a local
10486 and a global variables in expression, it's a local watchpoint,
10487 but unloading of a shared library will make the expression
10488 invalid. This is not a very common use case, but we still
10489 re-evaluate expression, to avoid surprises to the user.
10490
10491 Note that for local watchpoints, we re-evaluate it only if
10492 watchpoints frame id is still valid. If it's not, it means the
10493 watchpoint is out of scope and will be deleted soon. In fact,
10494 I'm not sure we'll ever be called in this case.
10495
10496 If a local watchpoint's frame id is still valid, then
10497 w->exp_valid_block is likewise valid, and we can safely use it.
10498
10499 Don't do anything about disabled watchpoints, since they will be
10500 reevaluated again when enabled. */
10501 update_watchpoint (w, 1 /* reparse */);
10502 }
10503
10504 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10505
10506 static int
10507 insert_watchpoint (struct bp_location *bl)
10508 {
10509 struct watchpoint *w = (struct watchpoint *) bl->owner;
10510 int length = w->exact ? 1 : bl->length;
10511
10512 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10513 w->cond_exp.get ());
10514 }
10515
10516 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10517
10518 static int
10519 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10520 {
10521 struct watchpoint *w = (struct watchpoint *) bl->owner;
10522 int length = w->exact ? 1 : bl->length;
10523
10524 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10525 w->cond_exp.get ());
10526 }
10527
10528 static int
10529 breakpoint_hit_watchpoint (const struct bp_location *bl,
10530 struct address_space *aspace, CORE_ADDR bp_addr,
10531 const struct target_waitstatus *ws)
10532 {
10533 struct breakpoint *b = bl->owner;
10534 struct watchpoint *w = (struct watchpoint *) b;
10535
10536 /* Continuable hardware watchpoints are treated as non-existent if the
10537 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10538 some data address). Otherwise gdb won't stop on a break instruction
10539 in the code (not from a breakpoint) when a hardware watchpoint has
10540 been defined. Also skip watchpoints which we know did not trigger
10541 (did not match the data address). */
10542 if (is_hardware_watchpoint (b)
10543 && w->watchpoint_triggered == watch_triggered_no)
10544 return 0;
10545
10546 return 1;
10547 }
10548
10549 static void
10550 check_status_watchpoint (bpstat bs)
10551 {
10552 gdb_assert (is_watchpoint (bs->breakpoint_at));
10553
10554 bpstat_check_watchpoint (bs);
10555 }
10556
10557 /* Implement the "resources_needed" breakpoint_ops method for
10558 hardware watchpoints. */
10559
10560 static int
10561 resources_needed_watchpoint (const struct bp_location *bl)
10562 {
10563 struct watchpoint *w = (struct watchpoint *) bl->owner;
10564 int length = w->exact? 1 : bl->length;
10565
10566 return target_region_ok_for_hw_watchpoint (bl->address, length);
10567 }
10568
10569 /* Implement the "works_in_software_mode" breakpoint_ops method for
10570 hardware watchpoints. */
10571
10572 static int
10573 works_in_software_mode_watchpoint (const struct breakpoint *b)
10574 {
10575 /* Read and access watchpoints only work with hardware support. */
10576 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10577 }
10578
10579 static enum print_stop_action
10580 print_it_watchpoint (bpstat bs)
10581 {
10582 struct cleanup *old_chain;
10583 struct breakpoint *b;
10584 enum print_stop_action result;
10585 struct watchpoint *w;
10586 struct ui_out *uiout = current_uiout;
10587
10588 gdb_assert (bs->bp_location_at != NULL);
10589
10590 b = bs->breakpoint_at;
10591 w = (struct watchpoint *) b;
10592
10593 old_chain = make_cleanup (null_cleanup, NULL);
10594
10595 annotate_watchpoint (b->number);
10596 maybe_print_thread_hit_breakpoint (uiout);
10597
10598 string_file stb;
10599
10600 switch (b->type)
10601 {
10602 case bp_watchpoint:
10603 case bp_hardware_watchpoint:
10604 if (uiout->is_mi_like_p ())
10605 uiout->field_string
10606 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10607 mention (b);
10608 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10609 uiout->text ("\nOld value = ");
10610 watchpoint_value_print (bs->old_val, &stb);
10611 uiout->field_stream ("old", stb);
10612 uiout->text ("\nNew value = ");
10613 watchpoint_value_print (w->val, &stb);
10614 uiout->field_stream ("new", stb);
10615 uiout->text ("\n");
10616 /* More than one watchpoint may have been triggered. */
10617 result = PRINT_UNKNOWN;
10618 break;
10619
10620 case bp_read_watchpoint:
10621 if (uiout->is_mi_like_p ())
10622 uiout->field_string
10623 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10624 mention (b);
10625 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10626 uiout->text ("\nValue = ");
10627 watchpoint_value_print (w->val, &stb);
10628 uiout->field_stream ("value", stb);
10629 uiout->text ("\n");
10630 result = PRINT_UNKNOWN;
10631 break;
10632
10633 case bp_access_watchpoint:
10634 if (bs->old_val != NULL)
10635 {
10636 if (uiout->is_mi_like_p ())
10637 uiout->field_string
10638 ("reason",
10639 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10640 mention (b);
10641 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10642 uiout->text ("\nOld value = ");
10643 watchpoint_value_print (bs->old_val, &stb);
10644 uiout->field_stream ("old", stb);
10645 uiout->text ("\nNew value = ");
10646 }
10647 else
10648 {
10649 mention (b);
10650 if (uiout->is_mi_like_p ())
10651 uiout->field_string
10652 ("reason",
10653 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10654 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10655 uiout->text ("\nValue = ");
10656 }
10657 watchpoint_value_print (w->val, &stb);
10658 uiout->field_stream ("new", stb);
10659 uiout->text ("\n");
10660 result = PRINT_UNKNOWN;
10661 break;
10662 default:
10663 result = PRINT_UNKNOWN;
10664 }
10665
10666 do_cleanups (old_chain);
10667 return result;
10668 }
10669
10670 /* Implement the "print_mention" breakpoint_ops method for hardware
10671 watchpoints. */
10672
10673 static void
10674 print_mention_watchpoint (struct breakpoint *b)
10675 {
10676 struct watchpoint *w = (struct watchpoint *) b;
10677 struct ui_out *uiout = current_uiout;
10678 const char *tuple_name;
10679
10680 switch (b->type)
10681 {
10682 case bp_watchpoint:
10683 uiout->text ("Watchpoint ");
10684 tuple_name = "wpt";
10685 break;
10686 case bp_hardware_watchpoint:
10687 uiout->text ("Hardware watchpoint ");
10688 tuple_name = "wpt";
10689 break;
10690 case bp_read_watchpoint:
10691 uiout->text ("Hardware read watchpoint ");
10692 tuple_name = "hw-rwpt";
10693 break;
10694 case bp_access_watchpoint:
10695 uiout->text ("Hardware access (read/write) watchpoint ");
10696 tuple_name = "hw-awpt";
10697 break;
10698 default:
10699 internal_error (__FILE__, __LINE__,
10700 _("Invalid hardware watchpoint type."));
10701 }
10702
10703 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10704 uiout->field_int ("number", b->number);
10705 uiout->text (": ");
10706 uiout->field_string ("exp", w->exp_string);
10707 }
10708
10709 /* Implement the "print_recreate" breakpoint_ops method for
10710 watchpoints. */
10711
10712 static void
10713 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10714 {
10715 struct watchpoint *w = (struct watchpoint *) b;
10716
10717 switch (b->type)
10718 {
10719 case bp_watchpoint:
10720 case bp_hardware_watchpoint:
10721 fprintf_unfiltered (fp, "watch");
10722 break;
10723 case bp_read_watchpoint:
10724 fprintf_unfiltered (fp, "rwatch");
10725 break;
10726 case bp_access_watchpoint:
10727 fprintf_unfiltered (fp, "awatch");
10728 break;
10729 default:
10730 internal_error (__FILE__, __LINE__,
10731 _("Invalid watchpoint type."));
10732 }
10733
10734 fprintf_unfiltered (fp, " %s", w->exp_string);
10735 print_recreate_thread (b, fp);
10736 }
10737
10738 /* Implement the "explains_signal" breakpoint_ops method for
10739 watchpoints. */
10740
10741 static int
10742 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10743 {
10744 /* A software watchpoint cannot cause a signal other than
10745 GDB_SIGNAL_TRAP. */
10746 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10747 return 0;
10748
10749 return 1;
10750 }
10751
10752 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10753
10754 static struct breakpoint_ops watchpoint_breakpoint_ops;
10755
10756 /* Implement the "insert" breakpoint_ops method for
10757 masked hardware watchpoints. */
10758
10759 static int
10760 insert_masked_watchpoint (struct bp_location *bl)
10761 {
10762 struct watchpoint *w = (struct watchpoint *) bl->owner;
10763
10764 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10765 bl->watchpoint_type);
10766 }
10767
10768 /* Implement the "remove" breakpoint_ops method for
10769 masked hardware watchpoints. */
10770
10771 static int
10772 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10773 {
10774 struct watchpoint *w = (struct watchpoint *) bl->owner;
10775
10776 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10777 bl->watchpoint_type);
10778 }
10779
10780 /* Implement the "resources_needed" breakpoint_ops method for
10781 masked hardware watchpoints. */
10782
10783 static int
10784 resources_needed_masked_watchpoint (const struct bp_location *bl)
10785 {
10786 struct watchpoint *w = (struct watchpoint *) bl->owner;
10787
10788 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10789 }
10790
10791 /* Implement the "works_in_software_mode" breakpoint_ops method for
10792 masked hardware watchpoints. */
10793
10794 static int
10795 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10796 {
10797 return 0;
10798 }
10799
10800 /* Implement the "print_it" breakpoint_ops method for
10801 masked hardware watchpoints. */
10802
10803 static enum print_stop_action
10804 print_it_masked_watchpoint (bpstat bs)
10805 {
10806 struct breakpoint *b = bs->breakpoint_at;
10807 struct ui_out *uiout = current_uiout;
10808
10809 /* Masked watchpoints have only one location. */
10810 gdb_assert (b->loc && b->loc->next == NULL);
10811
10812 annotate_watchpoint (b->number);
10813 maybe_print_thread_hit_breakpoint (uiout);
10814
10815 switch (b->type)
10816 {
10817 case bp_hardware_watchpoint:
10818 if (uiout->is_mi_like_p ())
10819 uiout->field_string
10820 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10821 break;
10822
10823 case bp_read_watchpoint:
10824 if (uiout->is_mi_like_p ())
10825 uiout->field_string
10826 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10827 break;
10828
10829 case bp_access_watchpoint:
10830 if (uiout->is_mi_like_p ())
10831 uiout->field_string
10832 ("reason",
10833 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10834 break;
10835 default:
10836 internal_error (__FILE__, __LINE__,
10837 _("Invalid hardware watchpoint type."));
10838 }
10839
10840 mention (b);
10841 uiout->text (_("\n\
10842 Check the underlying instruction at PC for the memory\n\
10843 address and value which triggered this watchpoint.\n"));
10844 uiout->text ("\n");
10845
10846 /* More than one watchpoint may have been triggered. */
10847 return PRINT_UNKNOWN;
10848 }
10849
10850 /* Implement the "print_one_detail" breakpoint_ops method for
10851 masked hardware watchpoints. */
10852
10853 static void
10854 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10855 struct ui_out *uiout)
10856 {
10857 struct watchpoint *w = (struct watchpoint *) b;
10858
10859 /* Masked watchpoints have only one location. */
10860 gdb_assert (b->loc && b->loc->next == NULL);
10861
10862 uiout->text ("\tmask ");
10863 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10864 uiout->text ("\n");
10865 }
10866
10867 /* Implement the "print_mention" breakpoint_ops method for
10868 masked hardware watchpoints. */
10869
10870 static void
10871 print_mention_masked_watchpoint (struct breakpoint *b)
10872 {
10873 struct watchpoint *w = (struct watchpoint *) b;
10874 struct ui_out *uiout = current_uiout;
10875 const char *tuple_name;
10876
10877 switch (b->type)
10878 {
10879 case bp_hardware_watchpoint:
10880 uiout->text ("Masked hardware watchpoint ");
10881 tuple_name = "wpt";
10882 break;
10883 case bp_read_watchpoint:
10884 uiout->text ("Masked hardware read watchpoint ");
10885 tuple_name = "hw-rwpt";
10886 break;
10887 case bp_access_watchpoint:
10888 uiout->text ("Masked hardware access (read/write) watchpoint ");
10889 tuple_name = "hw-awpt";
10890 break;
10891 default:
10892 internal_error (__FILE__, __LINE__,
10893 _("Invalid hardware watchpoint type."));
10894 }
10895
10896 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10897 uiout->field_int ("number", b->number);
10898 uiout->text (": ");
10899 uiout->field_string ("exp", w->exp_string);
10900 }
10901
10902 /* Implement the "print_recreate" breakpoint_ops method for
10903 masked hardware watchpoints. */
10904
10905 static void
10906 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10907 {
10908 struct watchpoint *w = (struct watchpoint *) b;
10909 char tmp[40];
10910
10911 switch (b->type)
10912 {
10913 case bp_hardware_watchpoint:
10914 fprintf_unfiltered (fp, "watch");
10915 break;
10916 case bp_read_watchpoint:
10917 fprintf_unfiltered (fp, "rwatch");
10918 break;
10919 case bp_access_watchpoint:
10920 fprintf_unfiltered (fp, "awatch");
10921 break;
10922 default:
10923 internal_error (__FILE__, __LINE__,
10924 _("Invalid hardware watchpoint type."));
10925 }
10926
10927 sprintf_vma (tmp, w->hw_wp_mask);
10928 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10929 print_recreate_thread (b, fp);
10930 }
10931
10932 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10933
10934 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10935
10936 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10937
10938 static int
10939 is_masked_watchpoint (const struct breakpoint *b)
10940 {
10941 return b->ops == &masked_watchpoint_breakpoint_ops;
10942 }
10943
10944 /* accessflag: hw_write: watch write,
10945 hw_read: watch read,
10946 hw_access: watch access (read or write) */
10947 static void
10948 watch_command_1 (const char *arg, int accessflag, int from_tty,
10949 int just_location, int internal)
10950 {
10951 struct breakpoint *scope_breakpoint = NULL;
10952 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10953 struct value *val, *mark, *result;
10954 int saved_bitpos = 0, saved_bitsize = 0;
10955 const char *exp_start = NULL;
10956 const char *exp_end = NULL;
10957 const char *tok, *end_tok;
10958 int toklen = -1;
10959 const char *cond_start = NULL;
10960 const char *cond_end = NULL;
10961 enum bptype bp_type;
10962 int thread = -1;
10963 int pc = 0;
10964 /* Flag to indicate whether we are going to use masks for
10965 the hardware watchpoint. */
10966 int use_mask = 0;
10967 CORE_ADDR mask = 0;
10968 struct watchpoint *w;
10969 char *expression;
10970 struct cleanup *back_to;
10971
10972 /* Make sure that we actually have parameters to parse. */
10973 if (arg != NULL && arg[0] != '\0')
10974 {
10975 const char *value_start;
10976
10977 exp_end = arg + strlen (arg);
10978
10979 /* Look for "parameter value" pairs at the end
10980 of the arguments string. */
10981 for (tok = exp_end - 1; tok > arg; tok--)
10982 {
10983 /* Skip whitespace at the end of the argument list. */
10984 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10985 tok--;
10986
10987 /* Find the beginning of the last token.
10988 This is the value of the parameter. */
10989 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10990 tok--;
10991 value_start = tok + 1;
10992
10993 /* Skip whitespace. */
10994 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10995 tok--;
10996
10997 end_tok = tok;
10998
10999 /* Find the beginning of the second to last token.
11000 This is the parameter itself. */
11001 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11002 tok--;
11003 tok++;
11004 toklen = end_tok - tok + 1;
11005
11006 if (toklen == 6 && startswith (tok, "thread"))
11007 {
11008 struct thread_info *thr;
11009 /* At this point we've found a "thread" token, which means
11010 the user is trying to set a watchpoint that triggers
11011 only in a specific thread. */
11012 const char *endp;
11013
11014 if (thread != -1)
11015 error(_("You can specify only one thread."));
11016
11017 /* Extract the thread ID from the next token. */
11018 thr = parse_thread_id (value_start, &endp);
11019
11020 /* Check if the user provided a valid thread ID. */
11021 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11022 invalid_thread_id_error (value_start);
11023
11024 thread = thr->global_num;
11025 }
11026 else if (toklen == 4 && startswith (tok, "mask"))
11027 {
11028 /* We've found a "mask" token, which means the user wants to
11029 create a hardware watchpoint that is going to have the mask
11030 facility. */
11031 struct value *mask_value, *mark;
11032
11033 if (use_mask)
11034 error(_("You can specify only one mask."));
11035
11036 use_mask = just_location = 1;
11037
11038 mark = value_mark ();
11039 mask_value = parse_to_comma_and_eval (&value_start);
11040 mask = value_as_address (mask_value);
11041 value_free_to_mark (mark);
11042 }
11043 else
11044 /* We didn't recognize what we found. We should stop here. */
11045 break;
11046
11047 /* Truncate the string and get rid of the "parameter value" pair before
11048 the arguments string is parsed by the parse_exp_1 function. */
11049 exp_end = tok;
11050 }
11051 }
11052 else
11053 exp_end = arg;
11054
11055 /* Parse the rest of the arguments. From here on out, everything
11056 is in terms of a newly allocated string instead of the original
11057 ARG. */
11058 innermost_block = NULL;
11059 expression = savestring (arg, exp_end - arg);
11060 back_to = make_cleanup (xfree, expression);
11061 exp_start = arg = expression;
11062 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11063 exp_end = arg;
11064 /* Remove trailing whitespace from the expression before saving it.
11065 This makes the eventual display of the expression string a bit
11066 prettier. */
11067 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11068 --exp_end;
11069
11070 /* Checking if the expression is not constant. */
11071 if (watchpoint_exp_is_const (exp.get ()))
11072 {
11073 int len;
11074
11075 len = exp_end - exp_start;
11076 while (len > 0 && isspace (exp_start[len - 1]))
11077 len--;
11078 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11079 }
11080
11081 exp_valid_block = innermost_block;
11082 mark = value_mark ();
11083 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11084
11085 if (val != NULL && just_location)
11086 {
11087 saved_bitpos = value_bitpos (val);
11088 saved_bitsize = value_bitsize (val);
11089 }
11090
11091 if (just_location)
11092 {
11093 int ret;
11094
11095 exp_valid_block = NULL;
11096 val = value_addr (result);
11097 release_value (val);
11098 value_free_to_mark (mark);
11099
11100 if (use_mask)
11101 {
11102 ret = target_masked_watch_num_registers (value_as_address (val),
11103 mask);
11104 if (ret == -1)
11105 error (_("This target does not support masked watchpoints."));
11106 else if (ret == -2)
11107 error (_("Invalid mask or memory region."));
11108 }
11109 }
11110 else if (val != NULL)
11111 release_value (val);
11112
11113 tok = skip_spaces_const (arg);
11114 end_tok = skip_to_space_const (tok);
11115
11116 toklen = end_tok - tok;
11117 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11118 {
11119 innermost_block = NULL;
11120 tok = cond_start = end_tok + 1;
11121 parse_exp_1 (&tok, 0, 0, 0);
11122
11123 /* The watchpoint expression may not be local, but the condition
11124 may still be. E.g.: `watch global if local > 0'. */
11125 cond_exp_valid_block = innermost_block;
11126
11127 cond_end = tok;
11128 }
11129 if (*tok)
11130 error (_("Junk at end of command."));
11131
11132 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11133
11134 /* Save this because create_internal_breakpoint below invalidates
11135 'wp_frame'. */
11136 frame_id watchpoint_frame = get_frame_id (wp_frame);
11137
11138 /* If the expression is "local", then set up a "watchpoint scope"
11139 breakpoint at the point where we've left the scope of the watchpoint
11140 expression. Create the scope breakpoint before the watchpoint, so
11141 that we will encounter it first in bpstat_stop_status. */
11142 if (exp_valid_block != NULL && wp_frame != NULL)
11143 {
11144 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11145
11146 if (frame_id_p (caller_frame_id))
11147 {
11148 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11149 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11150
11151 scope_breakpoint
11152 = create_internal_breakpoint (caller_arch, caller_pc,
11153 bp_watchpoint_scope,
11154 &momentary_breakpoint_ops);
11155
11156 /* create_internal_breakpoint could invalidate WP_FRAME. */
11157 wp_frame = NULL;
11158
11159 scope_breakpoint->enable_state = bp_enabled;
11160
11161 /* Automatically delete the breakpoint when it hits. */
11162 scope_breakpoint->disposition = disp_del;
11163
11164 /* Only break in the proper frame (help with recursion). */
11165 scope_breakpoint->frame_id = caller_frame_id;
11166
11167 /* Set the address at which we will stop. */
11168 scope_breakpoint->loc->gdbarch = caller_arch;
11169 scope_breakpoint->loc->requested_address = caller_pc;
11170 scope_breakpoint->loc->address
11171 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11172 scope_breakpoint->loc->requested_address,
11173 scope_breakpoint->type);
11174 }
11175 }
11176
11177 /* Now set up the breakpoint. We create all watchpoints as hardware
11178 watchpoints here even if hardware watchpoints are turned off, a call
11179 to update_watchpoint later in this function will cause the type to
11180 drop back to bp_watchpoint (software watchpoint) if required. */
11181
11182 if (accessflag == hw_read)
11183 bp_type = bp_read_watchpoint;
11184 else if (accessflag == hw_access)
11185 bp_type = bp_access_watchpoint;
11186 else
11187 bp_type = bp_hardware_watchpoint;
11188
11189 w = new watchpoint ();
11190
11191 if (use_mask)
11192 init_raw_breakpoint_without_location (w, NULL, bp_type,
11193 &masked_watchpoint_breakpoint_ops);
11194 else
11195 init_raw_breakpoint_without_location (w, NULL, bp_type,
11196 &watchpoint_breakpoint_ops);
11197 w->thread = thread;
11198 w->disposition = disp_donttouch;
11199 w->pspace = current_program_space;
11200 w->exp = std::move (exp);
11201 w->exp_valid_block = exp_valid_block;
11202 w->cond_exp_valid_block = cond_exp_valid_block;
11203 if (just_location)
11204 {
11205 struct type *t = value_type (val);
11206 CORE_ADDR addr = value_as_address (val);
11207
11208 w->exp_string_reparse
11209 = current_language->la_watch_location_expression (t, addr).release ();
11210
11211 w->exp_string = xstrprintf ("-location %.*s",
11212 (int) (exp_end - exp_start), exp_start);
11213 }
11214 else
11215 w->exp_string = savestring (exp_start, exp_end - exp_start);
11216
11217 if (use_mask)
11218 {
11219 w->hw_wp_mask = mask;
11220 }
11221 else
11222 {
11223 w->val = val;
11224 w->val_bitpos = saved_bitpos;
11225 w->val_bitsize = saved_bitsize;
11226 w->val_valid = 1;
11227 }
11228
11229 if (cond_start)
11230 w->cond_string = savestring (cond_start, cond_end - cond_start);
11231 else
11232 w->cond_string = 0;
11233
11234 if (frame_id_p (watchpoint_frame))
11235 {
11236 w->watchpoint_frame = watchpoint_frame;
11237 w->watchpoint_thread = inferior_ptid;
11238 }
11239 else
11240 {
11241 w->watchpoint_frame = null_frame_id;
11242 w->watchpoint_thread = null_ptid;
11243 }
11244
11245 if (scope_breakpoint != NULL)
11246 {
11247 /* The scope breakpoint is related to the watchpoint. We will
11248 need to act on them together. */
11249 w->related_breakpoint = scope_breakpoint;
11250 scope_breakpoint->related_breakpoint = w;
11251 }
11252
11253 if (!just_location)
11254 value_free_to_mark (mark);
11255
11256 TRY
11257 {
11258 /* Finally update the new watchpoint. This creates the locations
11259 that should be inserted. */
11260 update_watchpoint (w, 1);
11261 }
11262 CATCH (e, RETURN_MASK_ALL)
11263 {
11264 delete_breakpoint (w);
11265 throw_exception (e);
11266 }
11267 END_CATCH
11268
11269 install_breakpoint (internal, w, 1);
11270 do_cleanups (back_to);
11271 }
11272
11273 /* Return count of debug registers needed to watch the given expression.
11274 If the watchpoint cannot be handled in hardware return zero. */
11275
11276 static int
11277 can_use_hardware_watchpoint (struct value *v)
11278 {
11279 int found_memory_cnt = 0;
11280 struct value *head = v;
11281
11282 /* Did the user specifically forbid us to use hardware watchpoints? */
11283 if (!can_use_hw_watchpoints)
11284 return 0;
11285
11286 /* Make sure that the value of the expression depends only upon
11287 memory contents, and values computed from them within GDB. If we
11288 find any register references or function calls, we can't use a
11289 hardware watchpoint.
11290
11291 The idea here is that evaluating an expression generates a series
11292 of values, one holding the value of every subexpression. (The
11293 expression a*b+c has five subexpressions: a, b, a*b, c, and
11294 a*b+c.) GDB's values hold almost enough information to establish
11295 the criteria given above --- they identify memory lvalues,
11296 register lvalues, computed values, etcetera. So we can evaluate
11297 the expression, and then scan the chain of values that leaves
11298 behind to decide whether we can detect any possible change to the
11299 expression's final value using only hardware watchpoints.
11300
11301 However, I don't think that the values returned by inferior
11302 function calls are special in any way. So this function may not
11303 notice that an expression involving an inferior function call
11304 can't be watched with hardware watchpoints. FIXME. */
11305 for (; v; v = value_next (v))
11306 {
11307 if (VALUE_LVAL (v) == lval_memory)
11308 {
11309 if (v != head && value_lazy (v))
11310 /* A lazy memory lvalue in the chain is one that GDB never
11311 needed to fetch; we either just used its address (e.g.,
11312 `a' in `a.b') or we never needed it at all (e.g., `a'
11313 in `a,b'). This doesn't apply to HEAD; if that is
11314 lazy then it was not readable, but watch it anyway. */
11315 ;
11316 else
11317 {
11318 /* Ahh, memory we actually used! Check if we can cover
11319 it with hardware watchpoints. */
11320 struct type *vtype = check_typedef (value_type (v));
11321
11322 /* We only watch structs and arrays if user asked for it
11323 explicitly, never if they just happen to appear in a
11324 middle of some value chain. */
11325 if (v == head
11326 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11327 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11328 {
11329 CORE_ADDR vaddr = value_address (v);
11330 int len;
11331 int num_regs;
11332
11333 len = (target_exact_watchpoints
11334 && is_scalar_type_recursive (vtype))?
11335 1 : TYPE_LENGTH (value_type (v));
11336
11337 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11338 if (!num_regs)
11339 return 0;
11340 else
11341 found_memory_cnt += num_regs;
11342 }
11343 }
11344 }
11345 else if (VALUE_LVAL (v) != not_lval
11346 && deprecated_value_modifiable (v) == 0)
11347 return 0; /* These are values from the history (e.g., $1). */
11348 else if (VALUE_LVAL (v) == lval_register)
11349 return 0; /* Cannot watch a register with a HW watchpoint. */
11350 }
11351
11352 /* The expression itself looks suitable for using a hardware
11353 watchpoint, but give the target machine a chance to reject it. */
11354 return found_memory_cnt;
11355 }
11356
11357 void
11358 watch_command_wrapper (char *arg, int from_tty, int internal)
11359 {
11360 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11361 }
11362
11363 /* A helper function that looks for the "-location" argument and then
11364 calls watch_command_1. */
11365
11366 static void
11367 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11368 {
11369 int just_location = 0;
11370
11371 if (arg
11372 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11373 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11374 {
11375 arg = skip_spaces (arg);
11376 just_location = 1;
11377 }
11378
11379 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11380 }
11381
11382 static void
11383 watch_command (char *arg, int from_tty)
11384 {
11385 watch_maybe_just_location (arg, hw_write, from_tty);
11386 }
11387
11388 void
11389 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11390 {
11391 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11392 }
11393
11394 static void
11395 rwatch_command (char *arg, int from_tty)
11396 {
11397 watch_maybe_just_location (arg, hw_read, from_tty);
11398 }
11399
11400 void
11401 awatch_command_wrapper (char *arg, int from_tty, int internal)
11402 {
11403 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11404 }
11405
11406 static void
11407 awatch_command (char *arg, int from_tty)
11408 {
11409 watch_maybe_just_location (arg, hw_access, from_tty);
11410 }
11411 \f
11412
11413 /* Data for the FSM that manages the until(location)/advance commands
11414 in infcmd.c. Here because it uses the mechanisms of
11415 breakpoints. */
11416
11417 struct until_break_fsm
11418 {
11419 /* The base class. */
11420 struct thread_fsm thread_fsm;
11421
11422 /* The thread that as current when the command was executed. */
11423 int thread;
11424
11425 /* The breakpoint set at the destination location. */
11426 struct breakpoint *location_breakpoint;
11427
11428 /* Breakpoint set at the return address in the caller frame. May be
11429 NULL. */
11430 struct breakpoint *caller_breakpoint;
11431 };
11432
11433 static void until_break_fsm_clean_up (struct thread_fsm *self,
11434 struct thread_info *thread);
11435 static int until_break_fsm_should_stop (struct thread_fsm *self,
11436 struct thread_info *thread);
11437 static enum async_reply_reason
11438 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11439
11440 /* until_break_fsm's vtable. */
11441
11442 static struct thread_fsm_ops until_break_fsm_ops =
11443 {
11444 NULL, /* dtor */
11445 until_break_fsm_clean_up,
11446 until_break_fsm_should_stop,
11447 NULL, /* return_value */
11448 until_break_fsm_async_reply_reason,
11449 };
11450
11451 /* Allocate a new until_break_command_fsm. */
11452
11453 static struct until_break_fsm *
11454 new_until_break_fsm (struct interp *cmd_interp, int thread,
11455 struct breakpoint *location_breakpoint,
11456 struct breakpoint *caller_breakpoint)
11457 {
11458 struct until_break_fsm *sm;
11459
11460 sm = XCNEW (struct until_break_fsm);
11461 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11462
11463 sm->thread = thread;
11464 sm->location_breakpoint = location_breakpoint;
11465 sm->caller_breakpoint = caller_breakpoint;
11466
11467 return sm;
11468 }
11469
11470 /* Implementation of the 'should_stop' FSM method for the
11471 until(location)/advance commands. */
11472
11473 static int
11474 until_break_fsm_should_stop (struct thread_fsm *self,
11475 struct thread_info *tp)
11476 {
11477 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11478
11479 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11480 sm->location_breakpoint) != NULL
11481 || (sm->caller_breakpoint != NULL
11482 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11483 sm->caller_breakpoint) != NULL))
11484 thread_fsm_set_finished (self);
11485
11486 return 1;
11487 }
11488
11489 /* Implementation of the 'clean_up' FSM method for the
11490 until(location)/advance commands. */
11491
11492 static void
11493 until_break_fsm_clean_up (struct thread_fsm *self,
11494 struct thread_info *thread)
11495 {
11496 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11497
11498 /* Clean up our temporary breakpoints. */
11499 if (sm->location_breakpoint != NULL)
11500 {
11501 delete_breakpoint (sm->location_breakpoint);
11502 sm->location_breakpoint = NULL;
11503 }
11504 if (sm->caller_breakpoint != NULL)
11505 {
11506 delete_breakpoint (sm->caller_breakpoint);
11507 sm->caller_breakpoint = NULL;
11508 }
11509 delete_longjmp_breakpoint (sm->thread);
11510 }
11511
11512 /* Implementation of the 'async_reply_reason' FSM method for the
11513 until(location)/advance commands. */
11514
11515 static enum async_reply_reason
11516 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11517 {
11518 return EXEC_ASYNC_LOCATION_REACHED;
11519 }
11520
11521 void
11522 until_break_command (char *arg, int from_tty, int anywhere)
11523 {
11524 struct symtabs_and_lines sals;
11525 struct symtab_and_line sal;
11526 struct frame_info *frame;
11527 struct gdbarch *frame_gdbarch;
11528 struct frame_id stack_frame_id;
11529 struct frame_id caller_frame_id;
11530 struct breakpoint *location_breakpoint;
11531 struct breakpoint *caller_breakpoint = NULL;
11532 struct cleanup *old_chain;
11533 int thread;
11534 struct thread_info *tp;
11535 struct until_break_fsm *sm;
11536
11537 clear_proceed_status (0);
11538
11539 /* Set a breakpoint where the user wants it and at return from
11540 this function. */
11541
11542 event_location_up location = string_to_event_location (&arg, current_language);
11543
11544 if (last_displayed_sal_is_valid ())
11545 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11546 get_last_displayed_symtab (),
11547 get_last_displayed_line ());
11548 else
11549 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11550 NULL, (struct symtab *) NULL, 0);
11551
11552 if (sals.nelts != 1)
11553 error (_("Couldn't get information on specified line."));
11554
11555 sal = sals.sals[0];
11556 xfree (sals.sals); /* malloc'd, so freed. */
11557
11558 if (*arg)
11559 error (_("Junk at end of arguments."));
11560
11561 resolve_sal_pc (&sal);
11562
11563 tp = inferior_thread ();
11564 thread = tp->global_num;
11565
11566 old_chain = make_cleanup (null_cleanup, NULL);
11567
11568 /* Note linespec handling above invalidates the frame chain.
11569 Installing a breakpoint also invalidates the frame chain (as it
11570 may need to switch threads), so do any frame handling before
11571 that. */
11572
11573 frame = get_selected_frame (NULL);
11574 frame_gdbarch = get_frame_arch (frame);
11575 stack_frame_id = get_stack_frame_id (frame);
11576 caller_frame_id = frame_unwind_caller_id (frame);
11577
11578 /* Keep within the current frame, or in frames called by the current
11579 one. */
11580
11581 if (frame_id_p (caller_frame_id))
11582 {
11583 struct symtab_and_line sal2;
11584 struct gdbarch *caller_gdbarch;
11585
11586 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11587 sal2.pc = frame_unwind_caller_pc (frame);
11588 caller_gdbarch = frame_unwind_caller_arch (frame);
11589 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11590 sal2,
11591 caller_frame_id,
11592 bp_until);
11593 make_cleanup_delete_breakpoint (caller_breakpoint);
11594
11595 set_longjmp_breakpoint (tp, caller_frame_id);
11596 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11597 }
11598
11599 /* set_momentary_breakpoint could invalidate FRAME. */
11600 frame = NULL;
11601
11602 if (anywhere)
11603 /* If the user told us to continue until a specified location,
11604 we don't specify a frame at which we need to stop. */
11605 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11606 null_frame_id, bp_until);
11607 else
11608 /* Otherwise, specify the selected frame, because we want to stop
11609 only at the very same frame. */
11610 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11611 stack_frame_id, bp_until);
11612 make_cleanup_delete_breakpoint (location_breakpoint);
11613
11614 sm = new_until_break_fsm (command_interp (), tp->global_num,
11615 location_breakpoint, caller_breakpoint);
11616 tp->thread_fsm = &sm->thread_fsm;
11617
11618 discard_cleanups (old_chain);
11619
11620 proceed (-1, GDB_SIGNAL_DEFAULT);
11621 }
11622
11623 /* This function attempts to parse an optional "if <cond>" clause
11624 from the arg string. If one is not found, it returns NULL.
11625
11626 Else, it returns a pointer to the condition string. (It does not
11627 attempt to evaluate the string against a particular block.) And,
11628 it updates arg to point to the first character following the parsed
11629 if clause in the arg string. */
11630
11631 const char *
11632 ep_parse_optional_if_clause (const char **arg)
11633 {
11634 const char *cond_string;
11635
11636 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11637 return NULL;
11638
11639 /* Skip the "if" keyword. */
11640 (*arg) += 2;
11641
11642 /* Skip any extra leading whitespace, and record the start of the
11643 condition string. */
11644 *arg = skip_spaces_const (*arg);
11645 cond_string = *arg;
11646
11647 /* Assume that the condition occupies the remainder of the arg
11648 string. */
11649 (*arg) += strlen (cond_string);
11650
11651 return cond_string;
11652 }
11653
11654 /* Commands to deal with catching events, such as signals, exceptions,
11655 process start/exit, etc. */
11656
11657 typedef enum
11658 {
11659 catch_fork_temporary, catch_vfork_temporary,
11660 catch_fork_permanent, catch_vfork_permanent
11661 }
11662 catch_fork_kind;
11663
11664 static void
11665 catch_fork_command_1 (char *arg_entry, int from_tty,
11666 struct cmd_list_element *command)
11667 {
11668 const char *arg = arg_entry;
11669 struct gdbarch *gdbarch = get_current_arch ();
11670 const char *cond_string = NULL;
11671 catch_fork_kind fork_kind;
11672 int tempflag;
11673
11674 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11675 tempflag = (fork_kind == catch_fork_temporary
11676 || fork_kind == catch_vfork_temporary);
11677
11678 if (!arg)
11679 arg = "";
11680 arg = skip_spaces_const (arg);
11681
11682 /* The allowed syntax is:
11683 catch [v]fork
11684 catch [v]fork if <cond>
11685
11686 First, check if there's an if clause. */
11687 cond_string = ep_parse_optional_if_clause (&arg);
11688
11689 if ((*arg != '\0') && !isspace (*arg))
11690 error (_("Junk at end of arguments."));
11691
11692 /* If this target supports it, create a fork or vfork catchpoint
11693 and enable reporting of such events. */
11694 switch (fork_kind)
11695 {
11696 case catch_fork_temporary:
11697 case catch_fork_permanent:
11698 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11699 &catch_fork_breakpoint_ops);
11700 break;
11701 case catch_vfork_temporary:
11702 case catch_vfork_permanent:
11703 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11704 &catch_vfork_breakpoint_ops);
11705 break;
11706 default:
11707 error (_("unsupported or unknown fork kind; cannot catch it"));
11708 break;
11709 }
11710 }
11711
11712 static void
11713 catch_exec_command_1 (char *arg_entry, int from_tty,
11714 struct cmd_list_element *command)
11715 {
11716 const char *arg = arg_entry;
11717 struct exec_catchpoint *c;
11718 struct gdbarch *gdbarch = get_current_arch ();
11719 int tempflag;
11720 const char *cond_string = NULL;
11721
11722 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11723
11724 if (!arg)
11725 arg = "";
11726 arg = skip_spaces_const (arg);
11727
11728 /* The allowed syntax is:
11729 catch exec
11730 catch exec if <cond>
11731
11732 First, check if there's an if clause. */
11733 cond_string = ep_parse_optional_if_clause (&arg);
11734
11735 if ((*arg != '\0') && !isspace (*arg))
11736 error (_("Junk at end of arguments."));
11737
11738 c = new exec_catchpoint ();
11739 init_catchpoint (c, gdbarch, tempflag, cond_string,
11740 &catch_exec_breakpoint_ops);
11741 c->exec_pathname = NULL;
11742
11743 install_breakpoint (0, c, 1);
11744 }
11745
11746 void
11747 init_ada_exception_breakpoint (struct breakpoint *b,
11748 struct gdbarch *gdbarch,
11749 struct symtab_and_line sal,
11750 char *addr_string,
11751 const struct breakpoint_ops *ops,
11752 int tempflag,
11753 int enabled,
11754 int from_tty)
11755 {
11756 if (from_tty)
11757 {
11758 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11759 if (!loc_gdbarch)
11760 loc_gdbarch = gdbarch;
11761
11762 describe_other_breakpoints (loc_gdbarch,
11763 sal.pspace, sal.pc, sal.section, -1);
11764 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11765 version for exception catchpoints, because two catchpoints
11766 used for different exception names will use the same address.
11767 In this case, a "breakpoint ... also set at..." warning is
11768 unproductive. Besides, the warning phrasing is also a bit
11769 inappropriate, we should use the word catchpoint, and tell
11770 the user what type of catchpoint it is. The above is good
11771 enough for now, though. */
11772 }
11773
11774 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11775
11776 b->enable_state = enabled ? bp_enabled : bp_disabled;
11777 b->disposition = tempflag ? disp_del : disp_donttouch;
11778 b->location = string_to_event_location (&addr_string,
11779 language_def (language_ada));
11780 b->language = language_ada;
11781 }
11782
11783 static void
11784 catch_command (char *arg, int from_tty)
11785 {
11786 error (_("Catch requires an event name."));
11787 }
11788 \f
11789
11790 static void
11791 tcatch_command (char *arg, int from_tty)
11792 {
11793 error (_("Catch requires an event name."));
11794 }
11795
11796 /* A qsort comparison function that sorts breakpoints in order. */
11797
11798 static int
11799 compare_breakpoints (const void *a, const void *b)
11800 {
11801 const breakpoint_p *ba = (const breakpoint_p *) a;
11802 uintptr_t ua = (uintptr_t) *ba;
11803 const breakpoint_p *bb = (const breakpoint_p *) b;
11804 uintptr_t ub = (uintptr_t) *bb;
11805
11806 if ((*ba)->number < (*bb)->number)
11807 return -1;
11808 else if ((*ba)->number > (*bb)->number)
11809 return 1;
11810
11811 /* Now sort by address, in case we see, e..g, two breakpoints with
11812 the number 0. */
11813 if (ua < ub)
11814 return -1;
11815 return ua > ub ? 1 : 0;
11816 }
11817
11818 /* Delete breakpoints by address or line. */
11819
11820 static void
11821 clear_command (char *arg, int from_tty)
11822 {
11823 struct breakpoint *b, *prev;
11824 VEC(breakpoint_p) *found = 0;
11825 int ix;
11826 int default_match;
11827 struct symtabs_and_lines sals;
11828 struct symtab_and_line sal;
11829 int i;
11830 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11831
11832 if (arg)
11833 {
11834 sals = decode_line_with_current_source (arg,
11835 (DECODE_LINE_FUNFIRSTLINE
11836 | DECODE_LINE_LIST_MODE));
11837 make_cleanup (xfree, sals.sals);
11838 default_match = 0;
11839 }
11840 else
11841 {
11842 sals.sals = XNEW (struct symtab_and_line);
11843 make_cleanup (xfree, sals.sals);
11844 init_sal (&sal); /* Initialize to zeroes. */
11845
11846 /* Set sal's line, symtab, pc, and pspace to the values
11847 corresponding to the last call to print_frame_info. If the
11848 codepoint is not valid, this will set all the fields to 0. */
11849 get_last_displayed_sal (&sal);
11850 if (sal.symtab == 0)
11851 error (_("No source file specified."));
11852
11853 sals.sals[0] = sal;
11854 sals.nelts = 1;
11855
11856 default_match = 1;
11857 }
11858
11859 /* We don't call resolve_sal_pc here. That's not as bad as it
11860 seems, because all existing breakpoints typically have both
11861 file/line and pc set. So, if clear is given file/line, we can
11862 match this to existing breakpoint without obtaining pc at all.
11863
11864 We only support clearing given the address explicitly
11865 present in breakpoint table. Say, we've set breakpoint
11866 at file:line. There were several PC values for that file:line,
11867 due to optimization, all in one block.
11868
11869 We've picked one PC value. If "clear" is issued with another
11870 PC corresponding to the same file:line, the breakpoint won't
11871 be cleared. We probably can still clear the breakpoint, but
11872 since the other PC value is never presented to user, user
11873 can only find it by guessing, and it does not seem important
11874 to support that. */
11875
11876 /* For each line spec given, delete bps which correspond to it. Do
11877 it in two passes, solely to preserve the current behavior that
11878 from_tty is forced true if we delete more than one
11879 breakpoint. */
11880
11881 found = NULL;
11882 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11883 for (i = 0; i < sals.nelts; i++)
11884 {
11885 const char *sal_fullname;
11886
11887 /* If exact pc given, clear bpts at that pc.
11888 If line given (pc == 0), clear all bpts on specified line.
11889 If defaulting, clear all bpts on default line
11890 or at default pc.
11891
11892 defaulting sal.pc != 0 tests to do
11893
11894 0 1 pc
11895 1 1 pc _and_ line
11896 0 0 line
11897 1 0 <can't happen> */
11898
11899 sal = sals.sals[i];
11900 sal_fullname = (sal.symtab == NULL
11901 ? NULL : symtab_to_fullname (sal.symtab));
11902
11903 /* Find all matching breakpoints and add them to 'found'. */
11904 ALL_BREAKPOINTS (b)
11905 {
11906 int match = 0;
11907 /* Are we going to delete b? */
11908 if (b->type != bp_none && !is_watchpoint (b))
11909 {
11910 struct bp_location *loc = b->loc;
11911 for (; loc; loc = loc->next)
11912 {
11913 /* If the user specified file:line, don't allow a PC
11914 match. This matches historical gdb behavior. */
11915 int pc_match = (!sal.explicit_line
11916 && sal.pc
11917 && (loc->pspace == sal.pspace)
11918 && (loc->address == sal.pc)
11919 && (!section_is_overlay (loc->section)
11920 || loc->section == sal.section));
11921 int line_match = 0;
11922
11923 if ((default_match || sal.explicit_line)
11924 && loc->symtab != NULL
11925 && sal_fullname != NULL
11926 && sal.pspace == loc->pspace
11927 && loc->line_number == sal.line
11928 && filename_cmp (symtab_to_fullname (loc->symtab),
11929 sal_fullname) == 0)
11930 line_match = 1;
11931
11932 if (pc_match || line_match)
11933 {
11934 match = 1;
11935 break;
11936 }
11937 }
11938 }
11939
11940 if (match)
11941 VEC_safe_push(breakpoint_p, found, b);
11942 }
11943 }
11944
11945 /* Now go thru the 'found' chain and delete them. */
11946 if (VEC_empty(breakpoint_p, found))
11947 {
11948 if (arg)
11949 error (_("No breakpoint at %s."), arg);
11950 else
11951 error (_("No breakpoint at this line."));
11952 }
11953
11954 /* Remove duplicates from the vec. */
11955 qsort (VEC_address (breakpoint_p, found),
11956 VEC_length (breakpoint_p, found),
11957 sizeof (breakpoint_p),
11958 compare_breakpoints);
11959 prev = VEC_index (breakpoint_p, found, 0);
11960 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11961 {
11962 if (b == prev)
11963 {
11964 VEC_ordered_remove (breakpoint_p, found, ix);
11965 --ix;
11966 }
11967 }
11968
11969 if (VEC_length(breakpoint_p, found) > 1)
11970 from_tty = 1; /* Always report if deleted more than one. */
11971 if (from_tty)
11972 {
11973 if (VEC_length(breakpoint_p, found) == 1)
11974 printf_unfiltered (_("Deleted breakpoint "));
11975 else
11976 printf_unfiltered (_("Deleted breakpoints "));
11977 }
11978
11979 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11980 {
11981 if (from_tty)
11982 printf_unfiltered ("%d ", b->number);
11983 delete_breakpoint (b);
11984 }
11985 if (from_tty)
11986 putchar_unfiltered ('\n');
11987
11988 do_cleanups (cleanups);
11989 }
11990 \f
11991 /* Delete breakpoint in BS if they are `delete' breakpoints and
11992 all breakpoints that are marked for deletion, whether hit or not.
11993 This is called after any breakpoint is hit, or after errors. */
11994
11995 void
11996 breakpoint_auto_delete (bpstat bs)
11997 {
11998 struct breakpoint *b, *b_tmp;
11999
12000 for (; bs; bs = bs->next)
12001 if (bs->breakpoint_at
12002 && bs->breakpoint_at->disposition == disp_del
12003 && bs->stop)
12004 delete_breakpoint (bs->breakpoint_at);
12005
12006 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12007 {
12008 if (b->disposition == disp_del_at_next_stop)
12009 delete_breakpoint (b);
12010 }
12011 }
12012
12013 /* A comparison function for bp_location AP and BP being interfaced to
12014 qsort. Sort elements primarily by their ADDRESS (no matter what
12015 does breakpoint_address_is_meaningful say for its OWNER),
12016 secondarily by ordering first permanent elements and
12017 terciarily just ensuring the array is sorted stable way despite
12018 qsort being an unstable algorithm. */
12019
12020 static int
12021 bp_locations_compare (const void *ap, const void *bp)
12022 {
12023 const struct bp_location *a = *(const struct bp_location **) ap;
12024 const struct bp_location *b = *(const struct bp_location **) bp;
12025
12026 if (a->address != b->address)
12027 return (a->address > b->address) - (a->address < b->address);
12028
12029 /* Sort locations at the same address by their pspace number, keeping
12030 locations of the same inferior (in a multi-inferior environment)
12031 grouped. */
12032
12033 if (a->pspace->num != b->pspace->num)
12034 return ((a->pspace->num > b->pspace->num)
12035 - (a->pspace->num < b->pspace->num));
12036
12037 /* Sort permanent breakpoints first. */
12038 if (a->permanent != b->permanent)
12039 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12040
12041 /* Make the internal GDB representation stable across GDB runs
12042 where A and B memory inside GDB can differ. Breakpoint locations of
12043 the same type at the same address can be sorted in arbitrary order. */
12044
12045 if (a->owner->number != b->owner->number)
12046 return ((a->owner->number > b->owner->number)
12047 - (a->owner->number < b->owner->number));
12048
12049 return (a > b) - (a < b);
12050 }
12051
12052 /* Set bp_locations_placed_address_before_address_max and
12053 bp_locations_shadow_len_after_address_max according to the current
12054 content of the bp_locations array. */
12055
12056 static void
12057 bp_locations_target_extensions_update (void)
12058 {
12059 struct bp_location *bl, **blp_tmp;
12060
12061 bp_locations_placed_address_before_address_max = 0;
12062 bp_locations_shadow_len_after_address_max = 0;
12063
12064 ALL_BP_LOCATIONS (bl, blp_tmp)
12065 {
12066 CORE_ADDR start, end, addr;
12067
12068 if (!bp_location_has_shadow (bl))
12069 continue;
12070
12071 start = bl->target_info.placed_address;
12072 end = start + bl->target_info.shadow_len;
12073
12074 gdb_assert (bl->address >= start);
12075 addr = bl->address - start;
12076 if (addr > bp_locations_placed_address_before_address_max)
12077 bp_locations_placed_address_before_address_max = addr;
12078
12079 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12080
12081 gdb_assert (bl->address < end);
12082 addr = end - bl->address;
12083 if (addr > bp_locations_shadow_len_after_address_max)
12084 bp_locations_shadow_len_after_address_max = addr;
12085 }
12086 }
12087
12088 /* Download tracepoint locations if they haven't been. */
12089
12090 static void
12091 download_tracepoint_locations (void)
12092 {
12093 struct breakpoint *b;
12094 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12095
12096 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12097
12098 ALL_TRACEPOINTS (b)
12099 {
12100 struct bp_location *bl;
12101 struct tracepoint *t;
12102 int bp_location_downloaded = 0;
12103
12104 if ((b->type == bp_fast_tracepoint
12105 ? !may_insert_fast_tracepoints
12106 : !may_insert_tracepoints))
12107 continue;
12108
12109 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12110 {
12111 if (target_can_download_tracepoint ())
12112 can_download_tracepoint = TRIBOOL_TRUE;
12113 else
12114 can_download_tracepoint = TRIBOOL_FALSE;
12115 }
12116
12117 if (can_download_tracepoint == TRIBOOL_FALSE)
12118 break;
12119
12120 for (bl = b->loc; bl; bl = bl->next)
12121 {
12122 /* In tracepoint, locations are _never_ duplicated, so
12123 should_be_inserted is equivalent to
12124 unduplicated_should_be_inserted. */
12125 if (!should_be_inserted (bl) || bl->inserted)
12126 continue;
12127
12128 switch_to_program_space_and_thread (bl->pspace);
12129
12130 target_download_tracepoint (bl);
12131
12132 bl->inserted = 1;
12133 bp_location_downloaded = 1;
12134 }
12135 t = (struct tracepoint *) b;
12136 t->number_on_target = b->number;
12137 if (bp_location_downloaded)
12138 observer_notify_breakpoint_modified (b);
12139 }
12140 }
12141
12142 /* Swap the insertion/duplication state between two locations. */
12143
12144 static void
12145 swap_insertion (struct bp_location *left, struct bp_location *right)
12146 {
12147 const int left_inserted = left->inserted;
12148 const int left_duplicate = left->duplicate;
12149 const int left_needs_update = left->needs_update;
12150 const struct bp_target_info left_target_info = left->target_info;
12151
12152 /* Locations of tracepoints can never be duplicated. */
12153 if (is_tracepoint (left->owner))
12154 gdb_assert (!left->duplicate);
12155 if (is_tracepoint (right->owner))
12156 gdb_assert (!right->duplicate);
12157
12158 left->inserted = right->inserted;
12159 left->duplicate = right->duplicate;
12160 left->needs_update = right->needs_update;
12161 left->target_info = right->target_info;
12162 right->inserted = left_inserted;
12163 right->duplicate = left_duplicate;
12164 right->needs_update = left_needs_update;
12165 right->target_info = left_target_info;
12166 }
12167
12168 /* Force the re-insertion of the locations at ADDRESS. This is called
12169 once a new/deleted/modified duplicate location is found and we are evaluating
12170 conditions on the target's side. Such conditions need to be updated on
12171 the target. */
12172
12173 static void
12174 force_breakpoint_reinsertion (struct bp_location *bl)
12175 {
12176 struct bp_location **locp = NULL, **loc2p;
12177 struct bp_location *loc;
12178 CORE_ADDR address = 0;
12179 int pspace_num;
12180
12181 address = bl->address;
12182 pspace_num = bl->pspace->num;
12183
12184 /* This is only meaningful if the target is
12185 evaluating conditions and if the user has
12186 opted for condition evaluation on the target's
12187 side. */
12188 if (gdb_evaluates_breakpoint_condition_p ()
12189 || !target_supports_evaluation_of_breakpoint_conditions ())
12190 return;
12191
12192 /* Flag all breakpoint locations with this address and
12193 the same program space as the location
12194 as "its condition has changed". We need to
12195 update the conditions on the target's side. */
12196 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12197 {
12198 loc = *loc2p;
12199
12200 if (!is_breakpoint (loc->owner)
12201 || pspace_num != loc->pspace->num)
12202 continue;
12203
12204 /* Flag the location appropriately. We use a different state to
12205 let everyone know that we already updated the set of locations
12206 with addr bl->address and program space bl->pspace. This is so
12207 we don't have to keep calling these functions just to mark locations
12208 that have already been marked. */
12209 loc->condition_changed = condition_updated;
12210
12211 /* Free the agent expression bytecode as well. We will compute
12212 it later on. */
12213 loc->cond_bytecode.reset ();
12214 }
12215 }
12216 /* Called whether new breakpoints are created, or existing breakpoints
12217 deleted, to update the global location list and recompute which
12218 locations are duplicate of which.
12219
12220 The INSERT_MODE flag determines whether locations may not, may, or
12221 shall be inserted now. See 'enum ugll_insert_mode' for more
12222 info. */
12223
12224 static void
12225 update_global_location_list (enum ugll_insert_mode insert_mode)
12226 {
12227 struct breakpoint *b;
12228 struct bp_location **locp, *loc;
12229 struct cleanup *cleanups;
12230 /* Last breakpoint location address that was marked for update. */
12231 CORE_ADDR last_addr = 0;
12232 /* Last breakpoint location program space that was marked for update. */
12233 int last_pspace_num = -1;
12234
12235 /* Used in the duplicates detection below. When iterating over all
12236 bp_locations, points to the first bp_location of a given address.
12237 Breakpoints and watchpoints of different types are never
12238 duplicates of each other. Keep one pointer for each type of
12239 breakpoint/watchpoint, so we only need to loop over all locations
12240 once. */
12241 struct bp_location *bp_loc_first; /* breakpoint */
12242 struct bp_location *wp_loc_first; /* hardware watchpoint */
12243 struct bp_location *awp_loc_first; /* access watchpoint */
12244 struct bp_location *rwp_loc_first; /* read watchpoint */
12245
12246 /* Saved former bp_locations array which we compare against the newly
12247 built bp_locations from the current state of ALL_BREAKPOINTS. */
12248 struct bp_location **old_locations, **old_locp;
12249 unsigned old_locations_count;
12250
12251 old_locations = bp_locations;
12252 old_locations_count = bp_locations_count;
12253 bp_locations = NULL;
12254 bp_locations_count = 0;
12255 cleanups = make_cleanup (xfree, old_locations);
12256
12257 ALL_BREAKPOINTS (b)
12258 for (loc = b->loc; loc; loc = loc->next)
12259 bp_locations_count++;
12260
12261 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12262 locp = bp_locations;
12263 ALL_BREAKPOINTS (b)
12264 for (loc = b->loc; loc; loc = loc->next)
12265 *locp++ = loc;
12266 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12267 bp_locations_compare);
12268
12269 bp_locations_target_extensions_update ();
12270
12271 /* Identify bp_location instances that are no longer present in the
12272 new list, and therefore should be freed. Note that it's not
12273 necessary that those locations should be removed from inferior --
12274 if there's another location at the same address (previously
12275 marked as duplicate), we don't need to remove/insert the
12276 location.
12277
12278 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12279 and former bp_location array state respectively. */
12280
12281 locp = bp_locations;
12282 for (old_locp = old_locations;
12283 old_locp < old_locations + old_locations_count;
12284 old_locp++)
12285 {
12286 struct bp_location *old_loc = *old_locp;
12287 struct bp_location **loc2p;
12288
12289 /* Tells if 'old_loc' is found among the new locations. If
12290 not, we have to free it. */
12291 int found_object = 0;
12292 /* Tells if the location should remain inserted in the target. */
12293 int keep_in_target = 0;
12294 int removed = 0;
12295
12296 /* Skip LOCP entries which will definitely never be needed.
12297 Stop either at or being the one matching OLD_LOC. */
12298 while (locp < bp_locations + bp_locations_count
12299 && (*locp)->address < old_loc->address)
12300 locp++;
12301
12302 for (loc2p = locp;
12303 (loc2p < bp_locations + bp_locations_count
12304 && (*loc2p)->address == old_loc->address);
12305 loc2p++)
12306 {
12307 /* Check if this is a new/duplicated location or a duplicated
12308 location that had its condition modified. If so, we want to send
12309 its condition to the target if evaluation of conditions is taking
12310 place there. */
12311 if ((*loc2p)->condition_changed == condition_modified
12312 && (last_addr != old_loc->address
12313 || last_pspace_num != old_loc->pspace->num))
12314 {
12315 force_breakpoint_reinsertion (*loc2p);
12316 last_pspace_num = old_loc->pspace->num;
12317 }
12318
12319 if (*loc2p == old_loc)
12320 found_object = 1;
12321 }
12322
12323 /* We have already handled this address, update it so that we don't
12324 have to go through updates again. */
12325 last_addr = old_loc->address;
12326
12327 /* Target-side condition evaluation: Handle deleted locations. */
12328 if (!found_object)
12329 force_breakpoint_reinsertion (old_loc);
12330
12331 /* If this location is no longer present, and inserted, look if
12332 there's maybe a new location at the same address. If so,
12333 mark that one inserted, and don't remove this one. This is
12334 needed so that we don't have a time window where a breakpoint
12335 at certain location is not inserted. */
12336
12337 if (old_loc->inserted)
12338 {
12339 /* If the location is inserted now, we might have to remove
12340 it. */
12341
12342 if (found_object && should_be_inserted (old_loc))
12343 {
12344 /* The location is still present in the location list,
12345 and still should be inserted. Don't do anything. */
12346 keep_in_target = 1;
12347 }
12348 else
12349 {
12350 /* This location still exists, but it won't be kept in the
12351 target since it may have been disabled. We proceed to
12352 remove its target-side condition. */
12353
12354 /* The location is either no longer present, or got
12355 disabled. See if there's another location at the
12356 same address, in which case we don't need to remove
12357 this one from the target. */
12358
12359 /* OLD_LOC comes from existing struct breakpoint. */
12360 if (breakpoint_address_is_meaningful (old_loc->owner))
12361 {
12362 for (loc2p = locp;
12363 (loc2p < bp_locations + bp_locations_count
12364 && (*loc2p)->address == old_loc->address);
12365 loc2p++)
12366 {
12367 struct bp_location *loc2 = *loc2p;
12368
12369 if (breakpoint_locations_match (loc2, old_loc))
12370 {
12371 /* Read watchpoint locations are switched to
12372 access watchpoints, if the former are not
12373 supported, but the latter are. */
12374 if (is_hardware_watchpoint (old_loc->owner))
12375 {
12376 gdb_assert (is_hardware_watchpoint (loc2->owner));
12377 loc2->watchpoint_type = old_loc->watchpoint_type;
12378 }
12379
12380 /* loc2 is a duplicated location. We need to check
12381 if it should be inserted in case it will be
12382 unduplicated. */
12383 if (loc2 != old_loc
12384 && unduplicated_should_be_inserted (loc2))
12385 {
12386 swap_insertion (old_loc, loc2);
12387 keep_in_target = 1;
12388 break;
12389 }
12390 }
12391 }
12392 }
12393 }
12394
12395 if (!keep_in_target)
12396 {
12397 if (remove_breakpoint (old_loc))
12398 {
12399 /* This is just about all we can do. We could keep
12400 this location on the global list, and try to
12401 remove it next time, but there's no particular
12402 reason why we will succeed next time.
12403
12404 Note that at this point, old_loc->owner is still
12405 valid, as delete_breakpoint frees the breakpoint
12406 only after calling us. */
12407 printf_filtered (_("warning: Error removing "
12408 "breakpoint %d\n"),
12409 old_loc->owner->number);
12410 }
12411 removed = 1;
12412 }
12413 }
12414
12415 if (!found_object)
12416 {
12417 if (removed && target_is_non_stop_p ()
12418 && need_moribund_for_location_type (old_loc))
12419 {
12420 /* This location was removed from the target. In
12421 non-stop mode, a race condition is possible where
12422 we've removed a breakpoint, but stop events for that
12423 breakpoint are already queued and will arrive later.
12424 We apply an heuristic to be able to distinguish such
12425 SIGTRAPs from other random SIGTRAPs: we keep this
12426 breakpoint location for a bit, and will retire it
12427 after we see some number of events. The theory here
12428 is that reporting of events should, "on the average",
12429 be fair, so after a while we'll see events from all
12430 threads that have anything of interest, and no longer
12431 need to keep this breakpoint location around. We
12432 don't hold locations forever so to reduce chances of
12433 mistaking a non-breakpoint SIGTRAP for a breakpoint
12434 SIGTRAP.
12435
12436 The heuristic failing can be disastrous on
12437 decr_pc_after_break targets.
12438
12439 On decr_pc_after_break targets, like e.g., x86-linux,
12440 if we fail to recognize a late breakpoint SIGTRAP,
12441 because events_till_retirement has reached 0 too
12442 soon, we'll fail to do the PC adjustment, and report
12443 a random SIGTRAP to the user. When the user resumes
12444 the inferior, it will most likely immediately crash
12445 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12446 corrupted, because of being resumed e.g., in the
12447 middle of a multi-byte instruction, or skipped a
12448 one-byte instruction. This was actually seen happen
12449 on native x86-linux, and should be less rare on
12450 targets that do not support new thread events, like
12451 remote, due to the heuristic depending on
12452 thread_count.
12453
12454 Mistaking a random SIGTRAP for a breakpoint trap
12455 causes similar symptoms (PC adjustment applied when
12456 it shouldn't), but then again, playing with SIGTRAPs
12457 behind the debugger's back is asking for trouble.
12458
12459 Since hardware watchpoint traps are always
12460 distinguishable from other traps, so we don't need to
12461 apply keep hardware watchpoint moribund locations
12462 around. We simply always ignore hardware watchpoint
12463 traps we can no longer explain. */
12464
12465 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12466 old_loc->owner = NULL;
12467
12468 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12469 }
12470 else
12471 {
12472 old_loc->owner = NULL;
12473 decref_bp_location (&old_loc);
12474 }
12475 }
12476 }
12477
12478 /* Rescan breakpoints at the same address and section, marking the
12479 first one as "first" and any others as "duplicates". This is so
12480 that the bpt instruction is only inserted once. If we have a
12481 permanent breakpoint at the same place as BPT, make that one the
12482 official one, and the rest as duplicates. Permanent breakpoints
12483 are sorted first for the same address.
12484
12485 Do the same for hardware watchpoints, but also considering the
12486 watchpoint's type (regular/access/read) and length. */
12487
12488 bp_loc_first = NULL;
12489 wp_loc_first = NULL;
12490 awp_loc_first = NULL;
12491 rwp_loc_first = NULL;
12492 ALL_BP_LOCATIONS (loc, locp)
12493 {
12494 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12495 non-NULL. */
12496 struct bp_location **loc_first_p;
12497 b = loc->owner;
12498
12499 if (!unduplicated_should_be_inserted (loc)
12500 || !breakpoint_address_is_meaningful (b)
12501 /* Don't detect duplicate for tracepoint locations because they are
12502 never duplicated. See the comments in field `duplicate' of
12503 `struct bp_location'. */
12504 || is_tracepoint (b))
12505 {
12506 /* Clear the condition modification flag. */
12507 loc->condition_changed = condition_unchanged;
12508 continue;
12509 }
12510
12511 if (b->type == bp_hardware_watchpoint)
12512 loc_first_p = &wp_loc_first;
12513 else if (b->type == bp_read_watchpoint)
12514 loc_first_p = &rwp_loc_first;
12515 else if (b->type == bp_access_watchpoint)
12516 loc_first_p = &awp_loc_first;
12517 else
12518 loc_first_p = &bp_loc_first;
12519
12520 if (*loc_first_p == NULL
12521 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12522 || !breakpoint_locations_match (loc, *loc_first_p))
12523 {
12524 *loc_first_p = loc;
12525 loc->duplicate = 0;
12526
12527 if (is_breakpoint (loc->owner) && loc->condition_changed)
12528 {
12529 loc->needs_update = 1;
12530 /* Clear the condition modification flag. */
12531 loc->condition_changed = condition_unchanged;
12532 }
12533 continue;
12534 }
12535
12536
12537 /* This and the above ensure the invariant that the first location
12538 is not duplicated, and is the inserted one.
12539 All following are marked as duplicated, and are not inserted. */
12540 if (loc->inserted)
12541 swap_insertion (loc, *loc_first_p);
12542 loc->duplicate = 1;
12543
12544 /* Clear the condition modification flag. */
12545 loc->condition_changed = condition_unchanged;
12546 }
12547
12548 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12549 {
12550 if (insert_mode != UGLL_DONT_INSERT)
12551 insert_breakpoint_locations ();
12552 else
12553 {
12554 /* Even though the caller told us to not insert new
12555 locations, we may still need to update conditions on the
12556 target's side of breakpoints that were already inserted
12557 if the target is evaluating breakpoint conditions. We
12558 only update conditions for locations that are marked
12559 "needs_update". */
12560 update_inserted_breakpoint_locations ();
12561 }
12562 }
12563
12564 if (insert_mode != UGLL_DONT_INSERT)
12565 download_tracepoint_locations ();
12566
12567 do_cleanups (cleanups);
12568 }
12569
12570 void
12571 breakpoint_retire_moribund (void)
12572 {
12573 struct bp_location *loc;
12574 int ix;
12575
12576 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12577 if (--(loc->events_till_retirement) == 0)
12578 {
12579 decref_bp_location (&loc);
12580 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12581 --ix;
12582 }
12583 }
12584
12585 static void
12586 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12587 {
12588
12589 TRY
12590 {
12591 update_global_location_list (insert_mode);
12592 }
12593 CATCH (e, RETURN_MASK_ERROR)
12594 {
12595 }
12596 END_CATCH
12597 }
12598
12599 /* Clear BKP from a BPS. */
12600
12601 static void
12602 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12603 {
12604 bpstat bs;
12605
12606 for (bs = bps; bs; bs = bs->next)
12607 if (bs->breakpoint_at == bpt)
12608 {
12609 bs->breakpoint_at = NULL;
12610 bs->old_val = NULL;
12611 /* bs->commands will be freed later. */
12612 }
12613 }
12614
12615 /* Callback for iterate_over_threads. */
12616 static int
12617 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12618 {
12619 struct breakpoint *bpt = (struct breakpoint *) data;
12620
12621 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12622 return 0;
12623 }
12624
12625 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12626 callbacks. */
12627
12628 static void
12629 say_where (struct breakpoint *b)
12630 {
12631 struct value_print_options opts;
12632
12633 get_user_print_options (&opts);
12634
12635 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12636 single string. */
12637 if (b->loc == NULL)
12638 {
12639 /* For pending locations, the output differs slightly based
12640 on b->extra_string. If this is non-NULL, it contains either
12641 a condition or dprintf arguments. */
12642 if (b->extra_string == NULL)
12643 {
12644 printf_filtered (_(" (%s) pending."),
12645 event_location_to_string (b->location.get ()));
12646 }
12647 else if (b->type == bp_dprintf)
12648 {
12649 printf_filtered (_(" (%s,%s) pending."),
12650 event_location_to_string (b->location.get ()),
12651 b->extra_string);
12652 }
12653 else
12654 {
12655 printf_filtered (_(" (%s %s) pending."),
12656 event_location_to_string (b->location.get ()),
12657 b->extra_string);
12658 }
12659 }
12660 else
12661 {
12662 if (opts.addressprint || b->loc->symtab == NULL)
12663 {
12664 printf_filtered (" at ");
12665 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12666 gdb_stdout);
12667 }
12668 if (b->loc->symtab != NULL)
12669 {
12670 /* If there is a single location, we can print the location
12671 more nicely. */
12672 if (b->loc->next == NULL)
12673 printf_filtered (": file %s, line %d.",
12674 symtab_to_filename_for_display (b->loc->symtab),
12675 b->loc->line_number);
12676 else
12677 /* This is not ideal, but each location may have a
12678 different file name, and this at least reflects the
12679 real situation somewhat. */
12680 printf_filtered (": %s.",
12681 event_location_to_string (b->location.get ()));
12682 }
12683
12684 if (b->loc->next)
12685 {
12686 struct bp_location *loc = b->loc;
12687 int n = 0;
12688 for (; loc; loc = loc->next)
12689 ++n;
12690 printf_filtered (" (%d locations)", n);
12691 }
12692 }
12693 }
12694
12695 /* Default bp_location_ops methods. */
12696
12697 static void
12698 bp_location_dtor (struct bp_location *self)
12699 {
12700 xfree (self->function_name);
12701 }
12702
12703 static const struct bp_location_ops bp_location_ops =
12704 {
12705 bp_location_dtor
12706 };
12707
12708 /* Destructor for the breakpoint base class. */
12709
12710 breakpoint::~breakpoint ()
12711 {
12712 decref_counted_command_line (&this->commands);
12713 xfree (this->cond_string);
12714 xfree (this->extra_string);
12715 xfree (this->filter);
12716 }
12717
12718 static struct bp_location *
12719 base_breakpoint_allocate_location (struct breakpoint *self)
12720 {
12721 return new bp_location (&bp_location_ops, self);
12722 }
12723
12724 static void
12725 base_breakpoint_re_set (struct breakpoint *b)
12726 {
12727 /* Nothing to re-set. */
12728 }
12729
12730 #define internal_error_pure_virtual_called() \
12731 gdb_assert_not_reached ("pure virtual function called")
12732
12733 static int
12734 base_breakpoint_insert_location (struct bp_location *bl)
12735 {
12736 internal_error_pure_virtual_called ();
12737 }
12738
12739 static int
12740 base_breakpoint_remove_location (struct bp_location *bl,
12741 enum remove_bp_reason reason)
12742 {
12743 internal_error_pure_virtual_called ();
12744 }
12745
12746 static int
12747 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12748 struct address_space *aspace,
12749 CORE_ADDR bp_addr,
12750 const struct target_waitstatus *ws)
12751 {
12752 internal_error_pure_virtual_called ();
12753 }
12754
12755 static void
12756 base_breakpoint_check_status (bpstat bs)
12757 {
12758 /* Always stop. */
12759 }
12760
12761 /* A "works_in_software_mode" breakpoint_ops method that just internal
12762 errors. */
12763
12764 static int
12765 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12766 {
12767 internal_error_pure_virtual_called ();
12768 }
12769
12770 /* A "resources_needed" breakpoint_ops method that just internal
12771 errors. */
12772
12773 static int
12774 base_breakpoint_resources_needed (const struct bp_location *bl)
12775 {
12776 internal_error_pure_virtual_called ();
12777 }
12778
12779 static enum print_stop_action
12780 base_breakpoint_print_it (bpstat bs)
12781 {
12782 internal_error_pure_virtual_called ();
12783 }
12784
12785 static void
12786 base_breakpoint_print_one_detail (const struct breakpoint *self,
12787 struct ui_out *uiout)
12788 {
12789 /* nothing */
12790 }
12791
12792 static void
12793 base_breakpoint_print_mention (struct breakpoint *b)
12794 {
12795 internal_error_pure_virtual_called ();
12796 }
12797
12798 static void
12799 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12800 {
12801 internal_error_pure_virtual_called ();
12802 }
12803
12804 static void
12805 base_breakpoint_create_sals_from_location
12806 (const struct event_location *location,
12807 struct linespec_result *canonical,
12808 enum bptype type_wanted)
12809 {
12810 internal_error_pure_virtual_called ();
12811 }
12812
12813 static void
12814 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12815 struct linespec_result *c,
12816 gdb::unique_xmalloc_ptr<char> cond_string,
12817 gdb::unique_xmalloc_ptr<char> extra_string,
12818 enum bptype type_wanted,
12819 enum bpdisp disposition,
12820 int thread,
12821 int task, int ignore_count,
12822 const struct breakpoint_ops *o,
12823 int from_tty, int enabled,
12824 int internal, unsigned flags)
12825 {
12826 internal_error_pure_virtual_called ();
12827 }
12828
12829 static void
12830 base_breakpoint_decode_location (struct breakpoint *b,
12831 const struct event_location *location,
12832 struct program_space *search_pspace,
12833 struct symtabs_and_lines *sals)
12834 {
12835 internal_error_pure_virtual_called ();
12836 }
12837
12838 /* The default 'explains_signal' method. */
12839
12840 static int
12841 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12842 {
12843 return 1;
12844 }
12845
12846 /* The default "after_condition_true" method. */
12847
12848 static void
12849 base_breakpoint_after_condition_true (struct bpstats *bs)
12850 {
12851 /* Nothing to do. */
12852 }
12853
12854 struct breakpoint_ops base_breakpoint_ops =
12855 {
12856 base_breakpoint_allocate_location,
12857 base_breakpoint_re_set,
12858 base_breakpoint_insert_location,
12859 base_breakpoint_remove_location,
12860 base_breakpoint_breakpoint_hit,
12861 base_breakpoint_check_status,
12862 base_breakpoint_resources_needed,
12863 base_breakpoint_works_in_software_mode,
12864 base_breakpoint_print_it,
12865 NULL,
12866 base_breakpoint_print_one_detail,
12867 base_breakpoint_print_mention,
12868 base_breakpoint_print_recreate,
12869 base_breakpoint_create_sals_from_location,
12870 base_breakpoint_create_breakpoints_sal,
12871 base_breakpoint_decode_location,
12872 base_breakpoint_explains_signal,
12873 base_breakpoint_after_condition_true,
12874 };
12875
12876 /* Default breakpoint_ops methods. */
12877
12878 static void
12879 bkpt_re_set (struct breakpoint *b)
12880 {
12881 /* FIXME: is this still reachable? */
12882 if (breakpoint_event_location_empty_p (b))
12883 {
12884 /* Anything without a location can't be re-set. */
12885 delete_breakpoint (b);
12886 return;
12887 }
12888
12889 breakpoint_re_set_default (b);
12890 }
12891
12892 static int
12893 bkpt_insert_location (struct bp_location *bl)
12894 {
12895 CORE_ADDR addr = bl->target_info.reqstd_address;
12896
12897 bl->target_info.kind = breakpoint_kind (bl, &addr);
12898 bl->target_info.placed_address = addr;
12899
12900 if (bl->loc_type == bp_loc_hardware_breakpoint)
12901 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12902 else
12903 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12904 }
12905
12906 static int
12907 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12908 {
12909 if (bl->loc_type == bp_loc_hardware_breakpoint)
12910 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12911 else
12912 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12913 }
12914
12915 static int
12916 bkpt_breakpoint_hit (const struct bp_location *bl,
12917 struct address_space *aspace, CORE_ADDR bp_addr,
12918 const struct target_waitstatus *ws)
12919 {
12920 if (ws->kind != TARGET_WAITKIND_STOPPED
12921 || ws->value.sig != GDB_SIGNAL_TRAP)
12922 return 0;
12923
12924 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12925 aspace, bp_addr))
12926 return 0;
12927
12928 if (overlay_debugging /* unmapped overlay section */
12929 && section_is_overlay (bl->section)
12930 && !section_is_mapped (bl->section))
12931 return 0;
12932
12933 return 1;
12934 }
12935
12936 static int
12937 dprintf_breakpoint_hit (const struct bp_location *bl,
12938 struct address_space *aspace, CORE_ADDR bp_addr,
12939 const struct target_waitstatus *ws)
12940 {
12941 if (dprintf_style == dprintf_style_agent
12942 && target_can_run_breakpoint_commands ())
12943 {
12944 /* An agent-style dprintf never causes a stop. If we see a trap
12945 for this address it must be for a breakpoint that happens to
12946 be set at the same address. */
12947 return 0;
12948 }
12949
12950 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12951 }
12952
12953 static int
12954 bkpt_resources_needed (const struct bp_location *bl)
12955 {
12956 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12957
12958 return 1;
12959 }
12960
12961 static enum print_stop_action
12962 bkpt_print_it (bpstat bs)
12963 {
12964 struct breakpoint *b;
12965 const struct bp_location *bl;
12966 int bp_temp;
12967 struct ui_out *uiout = current_uiout;
12968
12969 gdb_assert (bs->bp_location_at != NULL);
12970
12971 bl = bs->bp_location_at;
12972 b = bs->breakpoint_at;
12973
12974 bp_temp = b->disposition == disp_del;
12975 if (bl->address != bl->requested_address)
12976 breakpoint_adjustment_warning (bl->requested_address,
12977 bl->address,
12978 b->number, 1);
12979 annotate_breakpoint (b->number);
12980 maybe_print_thread_hit_breakpoint (uiout);
12981
12982 if (bp_temp)
12983 uiout->text ("Temporary breakpoint ");
12984 else
12985 uiout->text ("Breakpoint ");
12986 if (uiout->is_mi_like_p ())
12987 {
12988 uiout->field_string ("reason",
12989 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12990 uiout->field_string ("disp", bpdisp_text (b->disposition));
12991 }
12992 uiout->field_int ("bkptno", b->number);
12993 uiout->text (", ");
12994
12995 return PRINT_SRC_AND_LOC;
12996 }
12997
12998 static void
12999 bkpt_print_mention (struct breakpoint *b)
13000 {
13001 if (current_uiout->is_mi_like_p ())
13002 return;
13003
13004 switch (b->type)
13005 {
13006 case bp_breakpoint:
13007 case bp_gnu_ifunc_resolver:
13008 if (b->disposition == disp_del)
13009 printf_filtered (_("Temporary breakpoint"));
13010 else
13011 printf_filtered (_("Breakpoint"));
13012 printf_filtered (_(" %d"), b->number);
13013 if (b->type == bp_gnu_ifunc_resolver)
13014 printf_filtered (_(" at gnu-indirect-function resolver"));
13015 break;
13016 case bp_hardware_breakpoint:
13017 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13018 break;
13019 case bp_dprintf:
13020 printf_filtered (_("Dprintf %d"), b->number);
13021 break;
13022 }
13023
13024 say_where (b);
13025 }
13026
13027 static void
13028 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13029 {
13030 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13031 fprintf_unfiltered (fp, "tbreak");
13032 else if (tp->type == bp_breakpoint)
13033 fprintf_unfiltered (fp, "break");
13034 else if (tp->type == bp_hardware_breakpoint
13035 && tp->disposition == disp_del)
13036 fprintf_unfiltered (fp, "thbreak");
13037 else if (tp->type == bp_hardware_breakpoint)
13038 fprintf_unfiltered (fp, "hbreak");
13039 else
13040 internal_error (__FILE__, __LINE__,
13041 _("unhandled breakpoint type %d"), (int) tp->type);
13042
13043 fprintf_unfiltered (fp, " %s",
13044 event_location_to_string (tp->location.get ()));
13045
13046 /* Print out extra_string if this breakpoint is pending. It might
13047 contain, for example, conditions that were set by the user. */
13048 if (tp->loc == NULL && tp->extra_string != NULL)
13049 fprintf_unfiltered (fp, " %s", tp->extra_string);
13050
13051 print_recreate_thread (tp, fp);
13052 }
13053
13054 static void
13055 bkpt_create_sals_from_location (const struct event_location *location,
13056 struct linespec_result *canonical,
13057 enum bptype type_wanted)
13058 {
13059 create_sals_from_location_default (location, canonical, type_wanted);
13060 }
13061
13062 static void
13063 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13064 struct linespec_result *canonical,
13065 gdb::unique_xmalloc_ptr<char> cond_string,
13066 gdb::unique_xmalloc_ptr<char> extra_string,
13067 enum bptype type_wanted,
13068 enum bpdisp disposition,
13069 int thread,
13070 int task, int ignore_count,
13071 const struct breakpoint_ops *ops,
13072 int from_tty, int enabled,
13073 int internal, unsigned flags)
13074 {
13075 create_breakpoints_sal_default (gdbarch, canonical,
13076 std::move (cond_string),
13077 std::move (extra_string),
13078 type_wanted,
13079 disposition, thread, task,
13080 ignore_count, ops, from_tty,
13081 enabled, internal, flags);
13082 }
13083
13084 static void
13085 bkpt_decode_location (struct breakpoint *b,
13086 const struct event_location *location,
13087 struct program_space *search_pspace,
13088 struct symtabs_and_lines *sals)
13089 {
13090 decode_location_default (b, location, search_pspace, sals);
13091 }
13092
13093 /* Virtual table for internal breakpoints. */
13094
13095 static void
13096 internal_bkpt_re_set (struct breakpoint *b)
13097 {
13098 switch (b->type)
13099 {
13100 /* Delete overlay event and longjmp master breakpoints; they
13101 will be reset later by breakpoint_re_set. */
13102 case bp_overlay_event:
13103 case bp_longjmp_master:
13104 case bp_std_terminate_master:
13105 case bp_exception_master:
13106 delete_breakpoint (b);
13107 break;
13108
13109 /* This breakpoint is special, it's set up when the inferior
13110 starts and we really don't want to touch it. */
13111 case bp_shlib_event:
13112
13113 /* Like bp_shlib_event, this breakpoint type is special. Once
13114 it is set up, we do not want to touch it. */
13115 case bp_thread_event:
13116 break;
13117 }
13118 }
13119
13120 static void
13121 internal_bkpt_check_status (bpstat bs)
13122 {
13123 if (bs->breakpoint_at->type == bp_shlib_event)
13124 {
13125 /* If requested, stop when the dynamic linker notifies GDB of
13126 events. This allows the user to get control and place
13127 breakpoints in initializer routines for dynamically loaded
13128 objects (among other things). */
13129 bs->stop = stop_on_solib_events;
13130 bs->print = stop_on_solib_events;
13131 }
13132 else
13133 bs->stop = 0;
13134 }
13135
13136 static enum print_stop_action
13137 internal_bkpt_print_it (bpstat bs)
13138 {
13139 struct breakpoint *b;
13140
13141 b = bs->breakpoint_at;
13142
13143 switch (b->type)
13144 {
13145 case bp_shlib_event:
13146 /* Did we stop because the user set the stop_on_solib_events
13147 variable? (If so, we report this as a generic, "Stopped due
13148 to shlib event" message.) */
13149 print_solib_event (0);
13150 break;
13151
13152 case bp_thread_event:
13153 /* Not sure how we will get here.
13154 GDB should not stop for these breakpoints. */
13155 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13156 break;
13157
13158 case bp_overlay_event:
13159 /* By analogy with the thread event, GDB should not stop for these. */
13160 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13161 break;
13162
13163 case bp_longjmp_master:
13164 /* These should never be enabled. */
13165 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13166 break;
13167
13168 case bp_std_terminate_master:
13169 /* These should never be enabled. */
13170 printf_filtered (_("std::terminate Master Breakpoint: "
13171 "gdb should not stop!\n"));
13172 break;
13173
13174 case bp_exception_master:
13175 /* These should never be enabled. */
13176 printf_filtered (_("Exception Master Breakpoint: "
13177 "gdb should not stop!\n"));
13178 break;
13179 }
13180
13181 return PRINT_NOTHING;
13182 }
13183
13184 static void
13185 internal_bkpt_print_mention (struct breakpoint *b)
13186 {
13187 /* Nothing to mention. These breakpoints are internal. */
13188 }
13189
13190 /* Virtual table for momentary breakpoints */
13191
13192 static void
13193 momentary_bkpt_re_set (struct breakpoint *b)
13194 {
13195 /* Keep temporary breakpoints, which can be encountered when we step
13196 over a dlopen call and solib_add is resetting the breakpoints.
13197 Otherwise these should have been blown away via the cleanup chain
13198 or by breakpoint_init_inferior when we rerun the executable. */
13199 }
13200
13201 static void
13202 momentary_bkpt_check_status (bpstat bs)
13203 {
13204 /* Nothing. The point of these breakpoints is causing a stop. */
13205 }
13206
13207 static enum print_stop_action
13208 momentary_bkpt_print_it (bpstat bs)
13209 {
13210 return PRINT_UNKNOWN;
13211 }
13212
13213 static void
13214 momentary_bkpt_print_mention (struct breakpoint *b)
13215 {
13216 /* Nothing to mention. These breakpoints are internal. */
13217 }
13218
13219 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13220
13221 It gets cleared already on the removal of the first one of such placed
13222 breakpoints. This is OK as they get all removed altogether. */
13223
13224 longjmp_breakpoint::~longjmp_breakpoint ()
13225 {
13226 thread_info *tp = find_thread_global_id (this->thread);
13227
13228 if (tp != NULL)
13229 tp->initiating_frame = null_frame_id;
13230 }
13231
13232 /* Specific methods for probe breakpoints. */
13233
13234 static int
13235 bkpt_probe_insert_location (struct bp_location *bl)
13236 {
13237 int v = bkpt_insert_location (bl);
13238
13239 if (v == 0)
13240 {
13241 /* The insertion was successful, now let's set the probe's semaphore
13242 if needed. */
13243 if (bl->probe.probe->pops->set_semaphore != NULL)
13244 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13245 bl->probe.objfile,
13246 bl->gdbarch);
13247 }
13248
13249 return v;
13250 }
13251
13252 static int
13253 bkpt_probe_remove_location (struct bp_location *bl,
13254 enum remove_bp_reason reason)
13255 {
13256 /* Let's clear the semaphore before removing the location. */
13257 if (bl->probe.probe->pops->clear_semaphore != NULL)
13258 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13259 bl->probe.objfile,
13260 bl->gdbarch);
13261
13262 return bkpt_remove_location (bl, reason);
13263 }
13264
13265 static void
13266 bkpt_probe_create_sals_from_location (const struct event_location *location,
13267 struct linespec_result *canonical,
13268 enum bptype type_wanted)
13269 {
13270 struct linespec_sals lsal;
13271
13272 lsal.sals = parse_probes (location, NULL, canonical);
13273 lsal.canonical
13274 = xstrdup (event_location_to_string (canonical->location.get ()));
13275 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13276 }
13277
13278 static void
13279 bkpt_probe_decode_location (struct breakpoint *b,
13280 const struct event_location *location,
13281 struct program_space *search_pspace,
13282 struct symtabs_and_lines *sals)
13283 {
13284 *sals = parse_probes (location, search_pspace, NULL);
13285 if (!sals->sals)
13286 error (_("probe not found"));
13287 }
13288
13289 /* The breakpoint_ops structure to be used in tracepoints. */
13290
13291 static void
13292 tracepoint_re_set (struct breakpoint *b)
13293 {
13294 breakpoint_re_set_default (b);
13295 }
13296
13297 static int
13298 tracepoint_breakpoint_hit (const struct bp_location *bl,
13299 struct address_space *aspace, CORE_ADDR bp_addr,
13300 const struct target_waitstatus *ws)
13301 {
13302 /* By definition, the inferior does not report stops at
13303 tracepoints. */
13304 return 0;
13305 }
13306
13307 static void
13308 tracepoint_print_one_detail (const struct breakpoint *self,
13309 struct ui_out *uiout)
13310 {
13311 struct tracepoint *tp = (struct tracepoint *) self;
13312 if (tp->static_trace_marker_id)
13313 {
13314 gdb_assert (self->type == bp_static_tracepoint);
13315
13316 uiout->text ("\tmarker id is ");
13317 uiout->field_string ("static-tracepoint-marker-string-id",
13318 tp->static_trace_marker_id);
13319 uiout->text ("\n");
13320 }
13321 }
13322
13323 static void
13324 tracepoint_print_mention (struct breakpoint *b)
13325 {
13326 if (current_uiout->is_mi_like_p ())
13327 return;
13328
13329 switch (b->type)
13330 {
13331 case bp_tracepoint:
13332 printf_filtered (_("Tracepoint"));
13333 printf_filtered (_(" %d"), b->number);
13334 break;
13335 case bp_fast_tracepoint:
13336 printf_filtered (_("Fast tracepoint"));
13337 printf_filtered (_(" %d"), b->number);
13338 break;
13339 case bp_static_tracepoint:
13340 printf_filtered (_("Static tracepoint"));
13341 printf_filtered (_(" %d"), b->number);
13342 break;
13343 default:
13344 internal_error (__FILE__, __LINE__,
13345 _("unhandled tracepoint type %d"), (int) b->type);
13346 }
13347
13348 say_where (b);
13349 }
13350
13351 static void
13352 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13353 {
13354 struct tracepoint *tp = (struct tracepoint *) self;
13355
13356 if (self->type == bp_fast_tracepoint)
13357 fprintf_unfiltered (fp, "ftrace");
13358 else if (self->type == bp_static_tracepoint)
13359 fprintf_unfiltered (fp, "strace");
13360 else if (self->type == bp_tracepoint)
13361 fprintf_unfiltered (fp, "trace");
13362 else
13363 internal_error (__FILE__, __LINE__,
13364 _("unhandled tracepoint type %d"), (int) self->type);
13365
13366 fprintf_unfiltered (fp, " %s",
13367 event_location_to_string (self->location.get ()));
13368 print_recreate_thread (self, fp);
13369
13370 if (tp->pass_count)
13371 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13372 }
13373
13374 static void
13375 tracepoint_create_sals_from_location (const struct event_location *location,
13376 struct linespec_result *canonical,
13377 enum bptype type_wanted)
13378 {
13379 create_sals_from_location_default (location, canonical, type_wanted);
13380 }
13381
13382 static void
13383 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13384 struct linespec_result *canonical,
13385 gdb::unique_xmalloc_ptr<char> cond_string,
13386 gdb::unique_xmalloc_ptr<char> extra_string,
13387 enum bptype type_wanted,
13388 enum bpdisp disposition,
13389 int thread,
13390 int task, int ignore_count,
13391 const struct breakpoint_ops *ops,
13392 int from_tty, int enabled,
13393 int internal, unsigned flags)
13394 {
13395 create_breakpoints_sal_default (gdbarch, canonical,
13396 std::move (cond_string),
13397 std::move (extra_string),
13398 type_wanted,
13399 disposition, thread, task,
13400 ignore_count, ops, from_tty,
13401 enabled, internal, flags);
13402 }
13403
13404 static void
13405 tracepoint_decode_location (struct breakpoint *b,
13406 const struct event_location *location,
13407 struct program_space *search_pspace,
13408 struct symtabs_and_lines *sals)
13409 {
13410 decode_location_default (b, location, search_pspace, sals);
13411 }
13412
13413 struct breakpoint_ops tracepoint_breakpoint_ops;
13414
13415 /* The breakpoint_ops structure to be use on tracepoints placed in a
13416 static probe. */
13417
13418 static void
13419 tracepoint_probe_create_sals_from_location
13420 (const struct event_location *location,
13421 struct linespec_result *canonical,
13422 enum bptype type_wanted)
13423 {
13424 /* We use the same method for breakpoint on probes. */
13425 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13426 }
13427
13428 static void
13429 tracepoint_probe_decode_location (struct breakpoint *b,
13430 const struct event_location *location,
13431 struct program_space *search_pspace,
13432 struct symtabs_and_lines *sals)
13433 {
13434 /* We use the same method for breakpoint on probes. */
13435 bkpt_probe_decode_location (b, location, search_pspace, sals);
13436 }
13437
13438 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13439
13440 /* Dprintf breakpoint_ops methods. */
13441
13442 static void
13443 dprintf_re_set (struct breakpoint *b)
13444 {
13445 breakpoint_re_set_default (b);
13446
13447 /* extra_string should never be non-NULL for dprintf. */
13448 gdb_assert (b->extra_string != NULL);
13449
13450 /* 1 - connect to target 1, that can run breakpoint commands.
13451 2 - create a dprintf, which resolves fine.
13452 3 - disconnect from target 1
13453 4 - connect to target 2, that can NOT run breakpoint commands.
13454
13455 After steps #3/#4, you'll want the dprintf command list to
13456 be updated, because target 1 and 2 may well return different
13457 answers for target_can_run_breakpoint_commands().
13458 Given absence of finer grained resetting, we get to do
13459 it all the time. */
13460 if (b->extra_string != NULL)
13461 update_dprintf_command_list (b);
13462 }
13463
13464 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13465
13466 static void
13467 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13468 {
13469 fprintf_unfiltered (fp, "dprintf %s,%s",
13470 event_location_to_string (tp->location.get ()),
13471 tp->extra_string);
13472 print_recreate_thread (tp, fp);
13473 }
13474
13475 /* Implement the "after_condition_true" breakpoint_ops method for
13476 dprintf.
13477
13478 dprintf's are implemented with regular commands in their command
13479 list, but we run the commands here instead of before presenting the
13480 stop to the user, as dprintf's don't actually cause a stop. This
13481 also makes it so that the commands of multiple dprintfs at the same
13482 address are all handled. */
13483
13484 static void
13485 dprintf_after_condition_true (struct bpstats *bs)
13486 {
13487 struct cleanup *old_chain;
13488 struct bpstats tmp_bs = { NULL };
13489 struct bpstats *tmp_bs_p = &tmp_bs;
13490
13491 /* dprintf's never cause a stop. This wasn't set in the
13492 check_status hook instead because that would make the dprintf's
13493 condition not be evaluated. */
13494 bs->stop = 0;
13495
13496 /* Run the command list here. Take ownership of it instead of
13497 copying. We never want these commands to run later in
13498 bpstat_do_actions, if a breakpoint that causes a stop happens to
13499 be set at same address as this dprintf, or even if running the
13500 commands here throws. */
13501 tmp_bs.commands = bs->commands;
13502 bs->commands = NULL;
13503 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13504
13505 bpstat_do_actions_1 (&tmp_bs_p);
13506
13507 /* 'tmp_bs.commands' will usually be NULL by now, but
13508 bpstat_do_actions_1 may return early without processing the whole
13509 list. */
13510 do_cleanups (old_chain);
13511 }
13512
13513 /* The breakpoint_ops structure to be used on static tracepoints with
13514 markers (`-m'). */
13515
13516 static void
13517 strace_marker_create_sals_from_location (const struct event_location *location,
13518 struct linespec_result *canonical,
13519 enum bptype type_wanted)
13520 {
13521 struct linespec_sals lsal;
13522 const char *arg_start, *arg;
13523 char *str;
13524 struct cleanup *cleanup;
13525
13526 arg = arg_start = get_linespec_location (location);
13527 lsal.sals = decode_static_tracepoint_spec (&arg);
13528
13529 str = savestring (arg_start, arg - arg_start);
13530 cleanup = make_cleanup (xfree, str);
13531 canonical->location = new_linespec_location (&str);
13532 do_cleanups (cleanup);
13533
13534 lsal.canonical
13535 = xstrdup (event_location_to_string (canonical->location.get ()));
13536 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13537 }
13538
13539 static void
13540 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13541 struct linespec_result *canonical,
13542 gdb::unique_xmalloc_ptr<char> cond_string,
13543 gdb::unique_xmalloc_ptr<char> extra_string,
13544 enum bptype type_wanted,
13545 enum bpdisp disposition,
13546 int thread,
13547 int task, int ignore_count,
13548 const struct breakpoint_ops *ops,
13549 int from_tty, int enabled,
13550 int internal, unsigned flags)
13551 {
13552 int i;
13553 struct linespec_sals *lsal = VEC_index (linespec_sals,
13554 canonical->sals, 0);
13555
13556 /* If the user is creating a static tracepoint by marker id
13557 (strace -m MARKER_ID), then store the sals index, so that
13558 breakpoint_re_set can try to match up which of the newly
13559 found markers corresponds to this one, and, don't try to
13560 expand multiple locations for each sal, given than SALS
13561 already should contain all sals for MARKER_ID. */
13562
13563 for (i = 0; i < lsal->sals.nelts; ++i)
13564 {
13565 struct symtabs_and_lines expanded;
13566 struct tracepoint *tp;
13567 event_location_up location;
13568
13569 expanded.nelts = 1;
13570 expanded.sals = &lsal->sals.sals[i];
13571
13572 location = copy_event_location (canonical->location.get ());
13573
13574 tp = new tracepoint ();
13575 init_breakpoint_sal (tp, gdbarch, expanded,
13576 std::move (location), NULL,
13577 std::move (cond_string),
13578 std::move (extra_string),
13579 type_wanted, disposition,
13580 thread, task, ignore_count, ops,
13581 from_tty, enabled, internal, flags,
13582 canonical->special_display);
13583 /* Given that its possible to have multiple markers with
13584 the same string id, if the user is creating a static
13585 tracepoint by marker id ("strace -m MARKER_ID"), then
13586 store the sals index, so that breakpoint_re_set can
13587 try to match up which of the newly found markers
13588 corresponds to this one */
13589 tp->static_trace_marker_id_idx = i;
13590
13591 install_breakpoint (internal, tp, 0);
13592 }
13593 }
13594
13595 static void
13596 strace_marker_decode_location (struct breakpoint *b,
13597 const struct event_location *location,
13598 struct program_space *search_pspace,
13599 struct symtabs_and_lines *sals)
13600 {
13601 struct tracepoint *tp = (struct tracepoint *) b;
13602 const char *s = get_linespec_location (location);
13603
13604 *sals = decode_static_tracepoint_spec (&s);
13605 if (sals->nelts > tp->static_trace_marker_id_idx)
13606 {
13607 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13608 sals->nelts = 1;
13609 }
13610 else
13611 error (_("marker %s not found"), tp->static_trace_marker_id);
13612 }
13613
13614 static struct breakpoint_ops strace_marker_breakpoint_ops;
13615
13616 static int
13617 strace_marker_p (struct breakpoint *b)
13618 {
13619 return b->ops == &strace_marker_breakpoint_ops;
13620 }
13621
13622 /* Delete a breakpoint and clean up all traces of it in the data
13623 structures. */
13624
13625 void
13626 delete_breakpoint (struct breakpoint *bpt)
13627 {
13628 struct breakpoint *b;
13629
13630 gdb_assert (bpt != NULL);
13631
13632 /* Has this bp already been deleted? This can happen because
13633 multiple lists can hold pointers to bp's. bpstat lists are
13634 especial culprits.
13635
13636 One example of this happening is a watchpoint's scope bp. When
13637 the scope bp triggers, we notice that the watchpoint is out of
13638 scope, and delete it. We also delete its scope bp. But the
13639 scope bp is marked "auto-deleting", and is already on a bpstat.
13640 That bpstat is then checked for auto-deleting bp's, which are
13641 deleted.
13642
13643 A real solution to this problem might involve reference counts in
13644 bp's, and/or giving them pointers back to their referencing
13645 bpstat's, and teaching delete_breakpoint to only free a bp's
13646 storage when no more references were extent. A cheaper bandaid
13647 was chosen. */
13648 if (bpt->type == bp_none)
13649 return;
13650
13651 /* At least avoid this stale reference until the reference counting
13652 of breakpoints gets resolved. */
13653 if (bpt->related_breakpoint != bpt)
13654 {
13655 struct breakpoint *related;
13656 struct watchpoint *w;
13657
13658 if (bpt->type == bp_watchpoint_scope)
13659 w = (struct watchpoint *) bpt->related_breakpoint;
13660 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13661 w = (struct watchpoint *) bpt;
13662 else
13663 w = NULL;
13664 if (w != NULL)
13665 watchpoint_del_at_next_stop (w);
13666
13667 /* Unlink bpt from the bpt->related_breakpoint ring. */
13668 for (related = bpt; related->related_breakpoint != bpt;
13669 related = related->related_breakpoint);
13670 related->related_breakpoint = bpt->related_breakpoint;
13671 bpt->related_breakpoint = bpt;
13672 }
13673
13674 /* watch_command_1 creates a watchpoint but only sets its number if
13675 update_watchpoint succeeds in creating its bp_locations. If there's
13676 a problem in that process, we'll be asked to delete the half-created
13677 watchpoint. In that case, don't announce the deletion. */
13678 if (bpt->number)
13679 observer_notify_breakpoint_deleted (bpt);
13680
13681 if (breakpoint_chain == bpt)
13682 breakpoint_chain = bpt->next;
13683
13684 ALL_BREAKPOINTS (b)
13685 if (b->next == bpt)
13686 {
13687 b->next = bpt->next;
13688 break;
13689 }
13690
13691 /* Be sure no bpstat's are pointing at the breakpoint after it's
13692 been freed. */
13693 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13694 in all threads for now. Note that we cannot just remove bpstats
13695 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13696 commands are associated with the bpstat; if we remove it here,
13697 then the later call to bpstat_do_actions (&stop_bpstat); in
13698 event-top.c won't do anything, and temporary breakpoints with
13699 commands won't work. */
13700
13701 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13702
13703 /* Now that breakpoint is removed from breakpoint list, update the
13704 global location list. This will remove locations that used to
13705 belong to this breakpoint. Do this before freeing the breakpoint
13706 itself, since remove_breakpoint looks at location's owner. It
13707 might be better design to have location completely
13708 self-contained, but it's not the case now. */
13709 update_global_location_list (UGLL_DONT_INSERT);
13710
13711 /* On the chance that someone will soon try again to delete this
13712 same bp, we mark it as deleted before freeing its storage. */
13713 bpt->type = bp_none;
13714 delete bpt;
13715 }
13716
13717 static void
13718 do_delete_breakpoint_cleanup (void *b)
13719 {
13720 delete_breakpoint ((struct breakpoint *) b);
13721 }
13722
13723 struct cleanup *
13724 make_cleanup_delete_breakpoint (struct breakpoint *b)
13725 {
13726 return make_cleanup (do_delete_breakpoint_cleanup, b);
13727 }
13728
13729 /* Iterator function to call a user-provided callback function once
13730 for each of B and its related breakpoints. */
13731
13732 static void
13733 iterate_over_related_breakpoints (struct breakpoint *b,
13734 void (*function) (struct breakpoint *,
13735 void *),
13736 void *data)
13737 {
13738 struct breakpoint *related;
13739
13740 related = b;
13741 do
13742 {
13743 struct breakpoint *next;
13744
13745 /* FUNCTION may delete RELATED. */
13746 next = related->related_breakpoint;
13747
13748 if (next == related)
13749 {
13750 /* RELATED is the last ring entry. */
13751 function (related, data);
13752
13753 /* FUNCTION may have deleted it, so we'd never reach back to
13754 B. There's nothing left to do anyway, so just break
13755 out. */
13756 break;
13757 }
13758 else
13759 function (related, data);
13760
13761 related = next;
13762 }
13763 while (related != b);
13764 }
13765
13766 static void
13767 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13768 {
13769 delete_breakpoint (b);
13770 }
13771
13772 /* A callback for map_breakpoint_numbers that calls
13773 delete_breakpoint. */
13774
13775 static void
13776 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13777 {
13778 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13779 }
13780
13781 void
13782 delete_command (char *arg, int from_tty)
13783 {
13784 struct breakpoint *b, *b_tmp;
13785
13786 dont_repeat ();
13787
13788 if (arg == 0)
13789 {
13790 int breaks_to_delete = 0;
13791
13792 /* Delete all breakpoints if no argument. Do not delete
13793 internal breakpoints, these have to be deleted with an
13794 explicit breakpoint number argument. */
13795 ALL_BREAKPOINTS (b)
13796 if (user_breakpoint_p (b))
13797 {
13798 breaks_to_delete = 1;
13799 break;
13800 }
13801
13802 /* Ask user only if there are some breakpoints to delete. */
13803 if (!from_tty
13804 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13805 {
13806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13807 if (user_breakpoint_p (b))
13808 delete_breakpoint (b);
13809 }
13810 }
13811 else
13812 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13813 }
13814
13815 /* Return true if all locations of B bound to PSPACE are pending. If
13816 PSPACE is NULL, all locations of all program spaces are
13817 considered. */
13818
13819 static int
13820 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13821 {
13822 struct bp_location *loc;
13823
13824 for (loc = b->loc; loc != NULL; loc = loc->next)
13825 if ((pspace == NULL
13826 || loc->pspace == pspace)
13827 && !loc->shlib_disabled
13828 && !loc->pspace->executing_startup)
13829 return 0;
13830 return 1;
13831 }
13832
13833 /* Subroutine of update_breakpoint_locations to simplify it.
13834 Return non-zero if multiple fns in list LOC have the same name.
13835 Null names are ignored. */
13836
13837 static int
13838 ambiguous_names_p (struct bp_location *loc)
13839 {
13840 struct bp_location *l;
13841 htab_t htab = htab_create_alloc (13, htab_hash_string,
13842 (int (*) (const void *,
13843 const void *)) streq,
13844 NULL, xcalloc, xfree);
13845
13846 for (l = loc; l != NULL; l = l->next)
13847 {
13848 const char **slot;
13849 const char *name = l->function_name;
13850
13851 /* Allow for some names to be NULL, ignore them. */
13852 if (name == NULL)
13853 continue;
13854
13855 slot = (const char **) htab_find_slot (htab, (const void *) name,
13856 INSERT);
13857 /* NOTE: We can assume slot != NULL here because xcalloc never
13858 returns NULL. */
13859 if (*slot != NULL)
13860 {
13861 htab_delete (htab);
13862 return 1;
13863 }
13864 *slot = name;
13865 }
13866
13867 htab_delete (htab);
13868 return 0;
13869 }
13870
13871 /* When symbols change, it probably means the sources changed as well,
13872 and it might mean the static tracepoint markers are no longer at
13873 the same address or line numbers they used to be at last we
13874 checked. Losing your static tracepoints whenever you rebuild is
13875 undesirable. This function tries to resync/rematch gdb static
13876 tracepoints with the markers on the target, for static tracepoints
13877 that have not been set by marker id. Static tracepoint that have
13878 been set by marker id are reset by marker id in breakpoint_re_set.
13879 The heuristic is:
13880
13881 1) For a tracepoint set at a specific address, look for a marker at
13882 the old PC. If one is found there, assume to be the same marker.
13883 If the name / string id of the marker found is different from the
13884 previous known name, assume that means the user renamed the marker
13885 in the sources, and output a warning.
13886
13887 2) For a tracepoint set at a given line number, look for a marker
13888 at the new address of the old line number. If one is found there,
13889 assume to be the same marker. If the name / string id of the
13890 marker found is different from the previous known name, assume that
13891 means the user renamed the marker in the sources, and output a
13892 warning.
13893
13894 3) If a marker is no longer found at the same address or line, it
13895 may mean the marker no longer exists. But it may also just mean
13896 the code changed a bit. Maybe the user added a few lines of code
13897 that made the marker move up or down (in line number terms). Ask
13898 the target for info about the marker with the string id as we knew
13899 it. If found, update line number and address in the matching
13900 static tracepoint. This will get confused if there's more than one
13901 marker with the same ID (possible in UST, although unadvised
13902 precisely because it confuses tools). */
13903
13904 static struct symtab_and_line
13905 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13906 {
13907 struct tracepoint *tp = (struct tracepoint *) b;
13908 struct static_tracepoint_marker marker;
13909 CORE_ADDR pc;
13910
13911 pc = sal.pc;
13912 if (sal.line)
13913 find_line_pc (sal.symtab, sal.line, &pc);
13914
13915 if (target_static_tracepoint_marker_at (pc, &marker))
13916 {
13917 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13918 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13919 b->number,
13920 tp->static_trace_marker_id, marker.str_id);
13921
13922 xfree (tp->static_trace_marker_id);
13923 tp->static_trace_marker_id = xstrdup (marker.str_id);
13924 release_static_tracepoint_marker (&marker);
13925
13926 return sal;
13927 }
13928
13929 /* Old marker wasn't found on target at lineno. Try looking it up
13930 by string ID. */
13931 if (!sal.explicit_pc
13932 && sal.line != 0
13933 && sal.symtab != NULL
13934 && tp->static_trace_marker_id != NULL)
13935 {
13936 VEC(static_tracepoint_marker_p) *markers;
13937
13938 markers
13939 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13940
13941 if (!VEC_empty(static_tracepoint_marker_p, markers))
13942 {
13943 struct symtab_and_line sal2;
13944 struct symbol *sym;
13945 struct static_tracepoint_marker *tpmarker;
13946 struct ui_out *uiout = current_uiout;
13947 struct explicit_location explicit_loc;
13948
13949 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13950
13951 xfree (tp->static_trace_marker_id);
13952 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13953
13954 warning (_("marker for static tracepoint %d (%s) not "
13955 "found at previous line number"),
13956 b->number, tp->static_trace_marker_id);
13957
13958 init_sal (&sal2);
13959
13960 sal2.pc = tpmarker->address;
13961
13962 sal2 = find_pc_line (tpmarker->address, 0);
13963 sym = find_pc_sect_function (tpmarker->address, NULL);
13964 uiout->text ("Now in ");
13965 if (sym)
13966 {
13967 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13968 uiout->text (" at ");
13969 }
13970 uiout->field_string ("file",
13971 symtab_to_filename_for_display (sal2.symtab));
13972 uiout->text (":");
13973
13974 if (uiout->is_mi_like_p ())
13975 {
13976 const char *fullname = symtab_to_fullname (sal2.symtab);
13977
13978 uiout->field_string ("fullname", fullname);
13979 }
13980
13981 uiout->field_int ("line", sal2.line);
13982 uiout->text ("\n");
13983
13984 b->loc->line_number = sal2.line;
13985 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13986
13987 b->location.reset (NULL);
13988 initialize_explicit_location (&explicit_loc);
13989 explicit_loc.source_filename
13990 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
13991 explicit_loc.line_offset.offset = b->loc->line_number;
13992 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13993 b->location = new_explicit_location (&explicit_loc);
13994
13995 /* Might be nice to check if function changed, and warn if
13996 so. */
13997
13998 release_static_tracepoint_marker (tpmarker);
13999 }
14000 }
14001 return sal;
14002 }
14003
14004 /* Returns 1 iff locations A and B are sufficiently same that
14005 we don't need to report breakpoint as changed. */
14006
14007 static int
14008 locations_are_equal (struct bp_location *a, struct bp_location *b)
14009 {
14010 while (a && b)
14011 {
14012 if (a->address != b->address)
14013 return 0;
14014
14015 if (a->shlib_disabled != b->shlib_disabled)
14016 return 0;
14017
14018 if (a->enabled != b->enabled)
14019 return 0;
14020
14021 a = a->next;
14022 b = b->next;
14023 }
14024
14025 if ((a == NULL) != (b == NULL))
14026 return 0;
14027
14028 return 1;
14029 }
14030
14031 /* Split all locations of B that are bound to PSPACE out of B's
14032 location list to a separate list and return that list's head. If
14033 PSPACE is NULL, hoist out all locations of B. */
14034
14035 static struct bp_location *
14036 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14037 {
14038 struct bp_location head;
14039 struct bp_location *i = b->loc;
14040 struct bp_location **i_link = &b->loc;
14041 struct bp_location *hoisted = &head;
14042
14043 if (pspace == NULL)
14044 {
14045 i = b->loc;
14046 b->loc = NULL;
14047 return i;
14048 }
14049
14050 head.next = NULL;
14051
14052 while (i != NULL)
14053 {
14054 if (i->pspace == pspace)
14055 {
14056 *i_link = i->next;
14057 i->next = NULL;
14058 hoisted->next = i;
14059 hoisted = i;
14060 }
14061 else
14062 i_link = &i->next;
14063 i = *i_link;
14064 }
14065
14066 return head.next;
14067 }
14068
14069 /* Create new breakpoint locations for B (a hardware or software
14070 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14071 zero, then B is a ranged breakpoint. Only recreates locations for
14072 FILTER_PSPACE. Locations of other program spaces are left
14073 untouched. */
14074
14075 void
14076 update_breakpoint_locations (struct breakpoint *b,
14077 struct program_space *filter_pspace,
14078 struct symtabs_and_lines sals,
14079 struct symtabs_and_lines sals_end)
14080 {
14081 int i;
14082 struct bp_location *existing_locations;
14083
14084 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14085 {
14086 /* Ranged breakpoints have only one start location and one end
14087 location. */
14088 b->enable_state = bp_disabled;
14089 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14090 "multiple locations found\n"),
14091 b->number);
14092 return;
14093 }
14094
14095 /* If there's no new locations, and all existing locations are
14096 pending, don't do anything. This optimizes the common case where
14097 all locations are in the same shared library, that was unloaded.
14098 We'd like to retain the location, so that when the library is
14099 loaded again, we don't loose the enabled/disabled status of the
14100 individual locations. */
14101 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14102 return;
14103
14104 existing_locations = hoist_existing_locations (b, filter_pspace);
14105
14106 for (i = 0; i < sals.nelts; ++i)
14107 {
14108 struct bp_location *new_loc;
14109
14110 switch_to_program_space_and_thread (sals.sals[i].pspace);
14111
14112 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14113
14114 /* Reparse conditions, they might contain references to the
14115 old symtab. */
14116 if (b->cond_string != NULL)
14117 {
14118 const char *s;
14119
14120 s = b->cond_string;
14121 TRY
14122 {
14123 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14124 block_for_pc (sals.sals[i].pc),
14125 0);
14126 }
14127 CATCH (e, RETURN_MASK_ERROR)
14128 {
14129 warning (_("failed to reevaluate condition "
14130 "for breakpoint %d: %s"),
14131 b->number, e.message);
14132 new_loc->enabled = 0;
14133 }
14134 END_CATCH
14135 }
14136
14137 if (sals_end.nelts)
14138 {
14139 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14140
14141 new_loc->length = end - sals.sals[0].pc + 1;
14142 }
14143 }
14144
14145 /* If possible, carry over 'disable' status from existing
14146 breakpoints. */
14147 {
14148 struct bp_location *e = existing_locations;
14149 /* If there are multiple breakpoints with the same function name,
14150 e.g. for inline functions, comparing function names won't work.
14151 Instead compare pc addresses; this is just a heuristic as things
14152 may have moved, but in practice it gives the correct answer
14153 often enough until a better solution is found. */
14154 int have_ambiguous_names = ambiguous_names_p (b->loc);
14155
14156 for (; e; e = e->next)
14157 {
14158 if (!e->enabled && e->function_name)
14159 {
14160 struct bp_location *l = b->loc;
14161 if (have_ambiguous_names)
14162 {
14163 for (; l; l = l->next)
14164 if (breakpoint_locations_match (e, l))
14165 {
14166 l->enabled = 0;
14167 break;
14168 }
14169 }
14170 else
14171 {
14172 for (; l; l = l->next)
14173 if (l->function_name
14174 && strcmp (e->function_name, l->function_name) == 0)
14175 {
14176 l->enabled = 0;
14177 break;
14178 }
14179 }
14180 }
14181 }
14182 }
14183
14184 if (!locations_are_equal (existing_locations, b->loc))
14185 observer_notify_breakpoint_modified (b);
14186 }
14187
14188 /* Find the SaL locations corresponding to the given LOCATION.
14189 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14190
14191 static struct symtabs_and_lines
14192 location_to_sals (struct breakpoint *b, struct event_location *location,
14193 struct program_space *search_pspace, int *found)
14194 {
14195 struct symtabs_and_lines sals = {0};
14196 struct gdb_exception exception = exception_none;
14197
14198 gdb_assert (b->ops != NULL);
14199
14200 TRY
14201 {
14202 b->ops->decode_location (b, location, search_pspace, &sals);
14203 }
14204 CATCH (e, RETURN_MASK_ERROR)
14205 {
14206 int not_found_and_ok = 0;
14207
14208 exception = e;
14209
14210 /* For pending breakpoints, it's expected that parsing will
14211 fail until the right shared library is loaded. User has
14212 already told to create pending breakpoints and don't need
14213 extra messages. If breakpoint is in bp_shlib_disabled
14214 state, then user already saw the message about that
14215 breakpoint being disabled, and don't want to see more
14216 errors. */
14217 if (e.error == NOT_FOUND_ERROR
14218 && (b->condition_not_parsed
14219 || (b->loc != NULL
14220 && search_pspace != NULL
14221 && b->loc->pspace != search_pspace)
14222 || (b->loc && b->loc->shlib_disabled)
14223 || (b->loc && b->loc->pspace->executing_startup)
14224 || b->enable_state == bp_disabled))
14225 not_found_and_ok = 1;
14226
14227 if (!not_found_and_ok)
14228 {
14229 /* We surely don't want to warn about the same breakpoint
14230 10 times. One solution, implemented here, is disable
14231 the breakpoint on error. Another solution would be to
14232 have separate 'warning emitted' flag. Since this
14233 happens only when a binary has changed, I don't know
14234 which approach is better. */
14235 b->enable_state = bp_disabled;
14236 throw_exception (e);
14237 }
14238 }
14239 END_CATCH
14240
14241 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14242 {
14243 int i;
14244
14245 for (i = 0; i < sals.nelts; ++i)
14246 resolve_sal_pc (&sals.sals[i]);
14247 if (b->condition_not_parsed && b->extra_string != NULL)
14248 {
14249 char *cond_string, *extra_string;
14250 int thread, task;
14251
14252 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14253 &cond_string, &thread, &task,
14254 &extra_string);
14255 gdb_assert (b->cond_string == NULL);
14256 if (cond_string)
14257 b->cond_string = cond_string;
14258 b->thread = thread;
14259 b->task = task;
14260 if (extra_string)
14261 {
14262 xfree (b->extra_string);
14263 b->extra_string = extra_string;
14264 }
14265 b->condition_not_parsed = 0;
14266 }
14267
14268 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14269 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14270
14271 *found = 1;
14272 }
14273 else
14274 *found = 0;
14275
14276 return sals;
14277 }
14278
14279 /* The default re_set method, for typical hardware or software
14280 breakpoints. Reevaluate the breakpoint and recreate its
14281 locations. */
14282
14283 static void
14284 breakpoint_re_set_default (struct breakpoint *b)
14285 {
14286 int found;
14287 struct symtabs_and_lines sals, sals_end;
14288 struct symtabs_and_lines expanded = {0};
14289 struct symtabs_and_lines expanded_end = {0};
14290 struct program_space *filter_pspace = current_program_space;
14291
14292 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14293 if (found)
14294 {
14295 make_cleanup (xfree, sals.sals);
14296 expanded = sals;
14297 }
14298
14299 if (b->location_range_end != NULL)
14300 {
14301 sals_end = location_to_sals (b, b->location_range_end.get (),
14302 filter_pspace, &found);
14303 if (found)
14304 {
14305 make_cleanup (xfree, sals_end.sals);
14306 expanded_end = sals_end;
14307 }
14308 }
14309
14310 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14311 }
14312
14313 /* Default method for creating SALs from an address string. It basically
14314 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14315
14316 static void
14317 create_sals_from_location_default (const struct event_location *location,
14318 struct linespec_result *canonical,
14319 enum bptype type_wanted)
14320 {
14321 parse_breakpoint_sals (location, canonical);
14322 }
14323
14324 /* Call create_breakpoints_sal for the given arguments. This is the default
14325 function for the `create_breakpoints_sal' method of
14326 breakpoint_ops. */
14327
14328 static void
14329 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14330 struct linespec_result *canonical,
14331 gdb::unique_xmalloc_ptr<char> cond_string,
14332 gdb::unique_xmalloc_ptr<char> extra_string,
14333 enum bptype type_wanted,
14334 enum bpdisp disposition,
14335 int thread,
14336 int task, int ignore_count,
14337 const struct breakpoint_ops *ops,
14338 int from_tty, int enabled,
14339 int internal, unsigned flags)
14340 {
14341 create_breakpoints_sal (gdbarch, canonical,
14342 std::move (cond_string),
14343 std::move (extra_string),
14344 type_wanted, disposition,
14345 thread, task, ignore_count, ops, from_tty,
14346 enabled, internal, flags);
14347 }
14348
14349 /* Decode the line represented by S by calling decode_line_full. This is the
14350 default function for the `decode_location' method of breakpoint_ops. */
14351
14352 static void
14353 decode_location_default (struct breakpoint *b,
14354 const struct event_location *location,
14355 struct program_space *search_pspace,
14356 struct symtabs_and_lines *sals)
14357 {
14358 struct linespec_result canonical;
14359
14360 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14361 (struct symtab *) NULL, 0,
14362 &canonical, multiple_symbols_all,
14363 b->filter);
14364
14365 /* We should get 0 or 1 resulting SALs. */
14366 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14367
14368 if (VEC_length (linespec_sals, canonical.sals) > 0)
14369 {
14370 struct linespec_sals *lsal;
14371
14372 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14373 *sals = lsal->sals;
14374 /* Arrange it so the destructor does not free the
14375 contents. */
14376 lsal->sals.sals = NULL;
14377 }
14378 }
14379
14380 /* Prepare the global context for a re-set of breakpoint B. */
14381
14382 static struct cleanup *
14383 prepare_re_set_context (struct breakpoint *b)
14384 {
14385 input_radix = b->input_radix;
14386 set_language (b->language);
14387
14388 return make_cleanup (null_cleanup, NULL);
14389 }
14390
14391 /* Reset a breakpoint given it's struct breakpoint * BINT.
14392 The value we return ends up being the return value from catch_errors.
14393 Unused in this case. */
14394
14395 static int
14396 breakpoint_re_set_one (void *bint)
14397 {
14398 /* Get past catch_errs. */
14399 struct breakpoint *b = (struct breakpoint *) bint;
14400 struct cleanup *cleanups;
14401
14402 cleanups = prepare_re_set_context (b);
14403 b->ops->re_set (b);
14404 do_cleanups (cleanups);
14405 return 0;
14406 }
14407
14408 /* Re-set breakpoint locations for the current program space.
14409 Locations bound to other program spaces are left untouched. */
14410
14411 void
14412 breakpoint_re_set (void)
14413 {
14414 struct breakpoint *b, *b_tmp;
14415 enum language save_language;
14416 int save_input_radix;
14417
14418 save_language = current_language->la_language;
14419 save_input_radix = input_radix;
14420
14421 {
14422 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14423
14424 /* Note: we must not try to insert locations until after all
14425 breakpoints have been re-set. Otherwise, e.g., when re-setting
14426 breakpoint 1, we'd insert the locations of breakpoint 2, which
14427 hadn't been re-set yet, and thus may have stale locations. */
14428
14429 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14430 {
14431 /* Format possible error msg. */
14432 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14433 b->number);
14434 struct cleanup *cleanups = make_cleanup (xfree, message);
14435 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14436 do_cleanups (cleanups);
14437 }
14438 set_language (save_language);
14439 input_radix = save_input_radix;
14440
14441 jit_breakpoint_re_set ();
14442 }
14443
14444 create_overlay_event_breakpoint ();
14445 create_longjmp_master_breakpoint ();
14446 create_std_terminate_master_breakpoint ();
14447 create_exception_master_breakpoint ();
14448
14449 /* Now we can insert. */
14450 update_global_location_list (UGLL_MAY_INSERT);
14451 }
14452 \f
14453 /* Reset the thread number of this breakpoint:
14454
14455 - If the breakpoint is for all threads, leave it as-is.
14456 - Else, reset it to the current thread for inferior_ptid. */
14457 void
14458 breakpoint_re_set_thread (struct breakpoint *b)
14459 {
14460 if (b->thread != -1)
14461 {
14462 if (in_thread_list (inferior_ptid))
14463 b->thread = ptid_to_global_thread_id (inferior_ptid);
14464
14465 /* We're being called after following a fork. The new fork is
14466 selected as current, and unless this was a vfork will have a
14467 different program space from the original thread. Reset that
14468 as well. */
14469 b->loc->pspace = current_program_space;
14470 }
14471 }
14472
14473 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14474 If from_tty is nonzero, it prints a message to that effect,
14475 which ends with a period (no newline). */
14476
14477 void
14478 set_ignore_count (int bptnum, int count, int from_tty)
14479 {
14480 struct breakpoint *b;
14481
14482 if (count < 0)
14483 count = 0;
14484
14485 ALL_BREAKPOINTS (b)
14486 if (b->number == bptnum)
14487 {
14488 if (is_tracepoint (b))
14489 {
14490 if (from_tty && count != 0)
14491 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14492 bptnum);
14493 return;
14494 }
14495
14496 b->ignore_count = count;
14497 if (from_tty)
14498 {
14499 if (count == 0)
14500 printf_filtered (_("Will stop next time "
14501 "breakpoint %d is reached."),
14502 bptnum);
14503 else if (count == 1)
14504 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14505 bptnum);
14506 else
14507 printf_filtered (_("Will ignore next %d "
14508 "crossings of breakpoint %d."),
14509 count, bptnum);
14510 }
14511 observer_notify_breakpoint_modified (b);
14512 return;
14513 }
14514
14515 error (_("No breakpoint number %d."), bptnum);
14516 }
14517
14518 /* Command to set ignore-count of breakpoint N to COUNT. */
14519
14520 static void
14521 ignore_command (char *args, int from_tty)
14522 {
14523 char *p = args;
14524 int num;
14525
14526 if (p == 0)
14527 error_no_arg (_("a breakpoint number"));
14528
14529 num = get_number (&p);
14530 if (num == 0)
14531 error (_("bad breakpoint number: '%s'"), args);
14532 if (*p == 0)
14533 error (_("Second argument (specified ignore-count) is missing."));
14534
14535 set_ignore_count (num,
14536 longest_to_int (value_as_long (parse_and_eval (p))),
14537 from_tty);
14538 if (from_tty)
14539 printf_filtered ("\n");
14540 }
14541 \f
14542 /* Call FUNCTION on each of the breakpoints
14543 whose numbers are given in ARGS. */
14544
14545 static void
14546 map_breakpoint_numbers (const char *args,
14547 void (*function) (struct breakpoint *,
14548 void *),
14549 void *data)
14550 {
14551 int num;
14552 struct breakpoint *b, *tmp;
14553
14554 if (args == 0 || *args == '\0')
14555 error_no_arg (_("one or more breakpoint numbers"));
14556
14557 number_or_range_parser parser (args);
14558
14559 while (!parser.finished ())
14560 {
14561 const char *p = parser.cur_tok ();
14562 bool match = false;
14563
14564 num = parser.get_number ();
14565 if (num == 0)
14566 {
14567 warning (_("bad breakpoint number at or near '%s'"), p);
14568 }
14569 else
14570 {
14571 ALL_BREAKPOINTS_SAFE (b, tmp)
14572 if (b->number == num)
14573 {
14574 match = true;
14575 function (b, data);
14576 break;
14577 }
14578 if (!match)
14579 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14580 }
14581 }
14582 }
14583
14584 static struct bp_location *
14585 find_location_by_number (char *number)
14586 {
14587 char *dot = strchr (number, '.');
14588 char *p1;
14589 int bp_num;
14590 int loc_num;
14591 struct breakpoint *b;
14592 struct bp_location *loc;
14593
14594 *dot = '\0';
14595
14596 p1 = number;
14597 bp_num = get_number (&p1);
14598 if (bp_num == 0)
14599 error (_("Bad breakpoint number '%s'"), number);
14600
14601 ALL_BREAKPOINTS (b)
14602 if (b->number == bp_num)
14603 {
14604 break;
14605 }
14606
14607 if (!b || b->number != bp_num)
14608 error (_("Bad breakpoint number '%s'"), number);
14609
14610 p1 = dot+1;
14611 loc_num = get_number (&p1);
14612 if (loc_num == 0)
14613 error (_("Bad breakpoint location number '%s'"), number);
14614
14615 --loc_num;
14616 loc = b->loc;
14617 for (;loc_num && loc; --loc_num, loc = loc->next)
14618 ;
14619 if (!loc)
14620 error (_("Bad breakpoint location number '%s'"), dot+1);
14621
14622 return loc;
14623 }
14624
14625
14626 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14627 If from_tty is nonzero, it prints a message to that effect,
14628 which ends with a period (no newline). */
14629
14630 void
14631 disable_breakpoint (struct breakpoint *bpt)
14632 {
14633 /* Never disable a watchpoint scope breakpoint; we want to
14634 hit them when we leave scope so we can delete both the
14635 watchpoint and its scope breakpoint at that time. */
14636 if (bpt->type == bp_watchpoint_scope)
14637 return;
14638
14639 bpt->enable_state = bp_disabled;
14640
14641 /* Mark breakpoint locations modified. */
14642 mark_breakpoint_modified (bpt);
14643
14644 if (target_supports_enable_disable_tracepoint ()
14645 && current_trace_status ()->running && is_tracepoint (bpt))
14646 {
14647 struct bp_location *location;
14648
14649 for (location = bpt->loc; location; location = location->next)
14650 target_disable_tracepoint (location);
14651 }
14652
14653 update_global_location_list (UGLL_DONT_INSERT);
14654
14655 observer_notify_breakpoint_modified (bpt);
14656 }
14657
14658 /* A callback for iterate_over_related_breakpoints. */
14659
14660 static void
14661 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14662 {
14663 disable_breakpoint (b);
14664 }
14665
14666 /* A callback for map_breakpoint_numbers that calls
14667 disable_breakpoint. */
14668
14669 static void
14670 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14671 {
14672 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14673 }
14674
14675 static void
14676 disable_command (char *args, int from_tty)
14677 {
14678 if (args == 0)
14679 {
14680 struct breakpoint *bpt;
14681
14682 ALL_BREAKPOINTS (bpt)
14683 if (user_breakpoint_p (bpt))
14684 disable_breakpoint (bpt);
14685 }
14686 else
14687 {
14688 char *num = extract_arg (&args);
14689
14690 while (num)
14691 {
14692 if (strchr (num, '.'))
14693 {
14694 struct bp_location *loc = find_location_by_number (num);
14695
14696 if (loc)
14697 {
14698 if (loc->enabled)
14699 {
14700 loc->enabled = 0;
14701 mark_breakpoint_location_modified (loc);
14702 }
14703 if (target_supports_enable_disable_tracepoint ()
14704 && current_trace_status ()->running && loc->owner
14705 && is_tracepoint (loc->owner))
14706 target_disable_tracepoint (loc);
14707 }
14708 update_global_location_list (UGLL_DONT_INSERT);
14709 }
14710 else
14711 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14712 num = extract_arg (&args);
14713 }
14714 }
14715 }
14716
14717 static void
14718 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14719 int count)
14720 {
14721 int target_resources_ok;
14722
14723 if (bpt->type == bp_hardware_breakpoint)
14724 {
14725 int i;
14726 i = hw_breakpoint_used_count ();
14727 target_resources_ok =
14728 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14729 i + 1, 0);
14730 if (target_resources_ok == 0)
14731 error (_("No hardware breakpoint support in the target."));
14732 else if (target_resources_ok < 0)
14733 error (_("Hardware breakpoints used exceeds limit."));
14734 }
14735
14736 if (is_watchpoint (bpt))
14737 {
14738 /* Initialize it just to avoid a GCC false warning. */
14739 enum enable_state orig_enable_state = bp_disabled;
14740
14741 TRY
14742 {
14743 struct watchpoint *w = (struct watchpoint *) bpt;
14744
14745 orig_enable_state = bpt->enable_state;
14746 bpt->enable_state = bp_enabled;
14747 update_watchpoint (w, 1 /* reparse */);
14748 }
14749 CATCH (e, RETURN_MASK_ALL)
14750 {
14751 bpt->enable_state = orig_enable_state;
14752 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14753 bpt->number);
14754 return;
14755 }
14756 END_CATCH
14757 }
14758
14759 bpt->enable_state = bp_enabled;
14760
14761 /* Mark breakpoint locations modified. */
14762 mark_breakpoint_modified (bpt);
14763
14764 if (target_supports_enable_disable_tracepoint ()
14765 && current_trace_status ()->running && is_tracepoint (bpt))
14766 {
14767 struct bp_location *location;
14768
14769 for (location = bpt->loc; location; location = location->next)
14770 target_enable_tracepoint (location);
14771 }
14772
14773 bpt->disposition = disposition;
14774 bpt->enable_count = count;
14775 update_global_location_list (UGLL_MAY_INSERT);
14776
14777 observer_notify_breakpoint_modified (bpt);
14778 }
14779
14780
14781 void
14782 enable_breakpoint (struct breakpoint *bpt)
14783 {
14784 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14785 }
14786
14787 static void
14788 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14789 {
14790 enable_breakpoint (bpt);
14791 }
14792
14793 /* A callback for map_breakpoint_numbers that calls
14794 enable_breakpoint. */
14795
14796 static void
14797 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14798 {
14799 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14800 }
14801
14802 /* The enable command enables the specified breakpoints (or all defined
14803 breakpoints) so they once again become (or continue to be) effective
14804 in stopping the inferior. */
14805
14806 static void
14807 enable_command (char *args, int from_tty)
14808 {
14809 if (args == 0)
14810 {
14811 struct breakpoint *bpt;
14812
14813 ALL_BREAKPOINTS (bpt)
14814 if (user_breakpoint_p (bpt))
14815 enable_breakpoint (bpt);
14816 }
14817 else
14818 {
14819 char *num = extract_arg (&args);
14820
14821 while (num)
14822 {
14823 if (strchr (num, '.'))
14824 {
14825 struct bp_location *loc = find_location_by_number (num);
14826
14827 if (loc)
14828 {
14829 if (!loc->enabled)
14830 {
14831 loc->enabled = 1;
14832 mark_breakpoint_location_modified (loc);
14833 }
14834 if (target_supports_enable_disable_tracepoint ()
14835 && current_trace_status ()->running && loc->owner
14836 && is_tracepoint (loc->owner))
14837 target_enable_tracepoint (loc);
14838 }
14839 update_global_location_list (UGLL_MAY_INSERT);
14840 }
14841 else
14842 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14843 num = extract_arg (&args);
14844 }
14845 }
14846 }
14847
14848 /* This struct packages up disposition data for application to multiple
14849 breakpoints. */
14850
14851 struct disp_data
14852 {
14853 enum bpdisp disp;
14854 int count;
14855 };
14856
14857 static void
14858 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14859 {
14860 struct disp_data disp_data = *(struct disp_data *) arg;
14861
14862 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14863 }
14864
14865 static void
14866 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14867 {
14868 struct disp_data disp = { disp_disable, 1 };
14869
14870 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14871 }
14872
14873 static void
14874 enable_once_command (char *args, int from_tty)
14875 {
14876 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14877 }
14878
14879 static void
14880 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14881 {
14882 struct disp_data disp = { disp_disable, *(int *) countptr };
14883
14884 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14885 }
14886
14887 static void
14888 enable_count_command (char *args, int from_tty)
14889 {
14890 int count;
14891
14892 if (args == NULL)
14893 error_no_arg (_("hit count"));
14894
14895 count = get_number (&args);
14896
14897 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14898 }
14899
14900 static void
14901 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14902 {
14903 struct disp_data disp = { disp_del, 1 };
14904
14905 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14906 }
14907
14908 static void
14909 enable_delete_command (char *args, int from_tty)
14910 {
14911 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14912 }
14913 \f
14914 static void
14915 set_breakpoint_cmd (char *args, int from_tty)
14916 {
14917 }
14918
14919 static void
14920 show_breakpoint_cmd (char *args, int from_tty)
14921 {
14922 }
14923
14924 /* Invalidate last known value of any hardware watchpoint if
14925 the memory which that value represents has been written to by
14926 GDB itself. */
14927
14928 static void
14929 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14930 CORE_ADDR addr, ssize_t len,
14931 const bfd_byte *data)
14932 {
14933 struct breakpoint *bp;
14934
14935 ALL_BREAKPOINTS (bp)
14936 if (bp->enable_state == bp_enabled
14937 && bp->type == bp_hardware_watchpoint)
14938 {
14939 struct watchpoint *wp = (struct watchpoint *) bp;
14940
14941 if (wp->val_valid && wp->val)
14942 {
14943 struct bp_location *loc;
14944
14945 for (loc = bp->loc; loc != NULL; loc = loc->next)
14946 if (loc->loc_type == bp_loc_hardware_watchpoint
14947 && loc->address + loc->length > addr
14948 && addr + len > loc->address)
14949 {
14950 value_free (wp->val);
14951 wp->val = NULL;
14952 wp->val_valid = 0;
14953 }
14954 }
14955 }
14956 }
14957
14958 /* Create and insert a breakpoint for software single step. */
14959
14960 void
14961 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14962 struct address_space *aspace,
14963 CORE_ADDR next_pc)
14964 {
14965 struct thread_info *tp = inferior_thread ();
14966 struct symtab_and_line sal;
14967 CORE_ADDR pc = next_pc;
14968
14969 if (tp->control.single_step_breakpoints == NULL)
14970 {
14971 tp->control.single_step_breakpoints
14972 = new_single_step_breakpoint (tp->global_num, gdbarch);
14973 }
14974
14975 sal = find_pc_line (pc, 0);
14976 sal.pc = pc;
14977 sal.section = find_pc_overlay (pc);
14978 sal.explicit_pc = 1;
14979 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14980
14981 update_global_location_list (UGLL_INSERT);
14982 }
14983
14984 /* Insert single step breakpoints according to the current state. */
14985
14986 int
14987 insert_single_step_breakpoints (struct gdbarch *gdbarch)
14988 {
14989 struct regcache *regcache = get_current_regcache ();
14990 std::vector<CORE_ADDR> next_pcs;
14991
14992 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
14993
14994 if (!next_pcs.empty ())
14995 {
14996 struct frame_info *frame = get_current_frame ();
14997 struct address_space *aspace = get_frame_address_space (frame);
14998
14999 for (CORE_ADDR pc : next_pcs)
15000 insert_single_step_breakpoint (gdbarch, aspace, pc);
15001
15002 return 1;
15003 }
15004 else
15005 return 0;
15006 }
15007
15008 /* See breakpoint.h. */
15009
15010 int
15011 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15012 struct address_space *aspace,
15013 CORE_ADDR pc)
15014 {
15015 struct bp_location *loc;
15016
15017 for (loc = bp->loc; loc != NULL; loc = loc->next)
15018 if (loc->inserted
15019 && breakpoint_location_address_match (loc, aspace, pc))
15020 return 1;
15021
15022 return 0;
15023 }
15024
15025 /* Check whether a software single-step breakpoint is inserted at
15026 PC. */
15027
15028 int
15029 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15030 CORE_ADDR pc)
15031 {
15032 struct breakpoint *bpt;
15033
15034 ALL_BREAKPOINTS (bpt)
15035 {
15036 if (bpt->type == bp_single_step
15037 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15038 return 1;
15039 }
15040 return 0;
15041 }
15042
15043 /* Tracepoint-specific operations. */
15044
15045 /* Set tracepoint count to NUM. */
15046 static void
15047 set_tracepoint_count (int num)
15048 {
15049 tracepoint_count = num;
15050 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15051 }
15052
15053 static void
15054 trace_command (char *arg, int from_tty)
15055 {
15056 struct breakpoint_ops *ops;
15057
15058 event_location_up location = string_to_event_location (&arg,
15059 current_language);
15060 if (location != NULL
15061 && event_location_type (location.get ()) == PROBE_LOCATION)
15062 ops = &tracepoint_probe_breakpoint_ops;
15063 else
15064 ops = &tracepoint_breakpoint_ops;
15065
15066 create_breakpoint (get_current_arch (),
15067 location.get (),
15068 NULL, 0, arg, 1 /* parse arg */,
15069 0 /* tempflag */,
15070 bp_tracepoint /* type_wanted */,
15071 0 /* Ignore count */,
15072 pending_break_support,
15073 ops,
15074 from_tty,
15075 1 /* enabled */,
15076 0 /* internal */, 0);
15077 }
15078
15079 static void
15080 ftrace_command (char *arg, int from_tty)
15081 {
15082 event_location_up location = string_to_event_location (&arg,
15083 current_language);
15084 create_breakpoint (get_current_arch (),
15085 location.get (),
15086 NULL, 0, arg, 1 /* parse arg */,
15087 0 /* tempflag */,
15088 bp_fast_tracepoint /* type_wanted */,
15089 0 /* Ignore count */,
15090 pending_break_support,
15091 &tracepoint_breakpoint_ops,
15092 from_tty,
15093 1 /* enabled */,
15094 0 /* internal */, 0);
15095 }
15096
15097 /* strace command implementation. Creates a static tracepoint. */
15098
15099 static void
15100 strace_command (char *arg, int from_tty)
15101 {
15102 struct breakpoint_ops *ops;
15103 event_location_up location;
15104 struct cleanup *back_to;
15105
15106 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15107 or with a normal static tracepoint. */
15108 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15109 {
15110 ops = &strace_marker_breakpoint_ops;
15111 location = new_linespec_location (&arg);
15112 }
15113 else
15114 {
15115 ops = &tracepoint_breakpoint_ops;
15116 location = string_to_event_location (&arg, current_language);
15117 }
15118
15119 create_breakpoint (get_current_arch (),
15120 location.get (),
15121 NULL, 0, arg, 1 /* parse arg */,
15122 0 /* tempflag */,
15123 bp_static_tracepoint /* type_wanted */,
15124 0 /* Ignore count */,
15125 pending_break_support,
15126 ops,
15127 from_tty,
15128 1 /* enabled */,
15129 0 /* internal */, 0);
15130 }
15131
15132 /* Set up a fake reader function that gets command lines from a linked
15133 list that was acquired during tracepoint uploading. */
15134
15135 static struct uploaded_tp *this_utp;
15136 static int next_cmd;
15137
15138 static char *
15139 read_uploaded_action (void)
15140 {
15141 char *rslt;
15142
15143 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15144
15145 next_cmd++;
15146
15147 return rslt;
15148 }
15149
15150 /* Given information about a tracepoint as recorded on a target (which
15151 can be either a live system or a trace file), attempt to create an
15152 equivalent GDB tracepoint. This is not a reliable process, since
15153 the target does not necessarily have all the information used when
15154 the tracepoint was originally defined. */
15155
15156 struct tracepoint *
15157 create_tracepoint_from_upload (struct uploaded_tp *utp)
15158 {
15159 char *addr_str, small_buf[100];
15160 struct tracepoint *tp;
15161
15162 if (utp->at_string)
15163 addr_str = utp->at_string;
15164 else
15165 {
15166 /* In the absence of a source location, fall back to raw
15167 address. Since there is no way to confirm that the address
15168 means the same thing as when the trace was started, warn the
15169 user. */
15170 warning (_("Uploaded tracepoint %d has no "
15171 "source location, using raw address"),
15172 utp->number);
15173 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15174 addr_str = small_buf;
15175 }
15176
15177 /* There's not much we can do with a sequence of bytecodes. */
15178 if (utp->cond && !utp->cond_string)
15179 warning (_("Uploaded tracepoint %d condition "
15180 "has no source form, ignoring it"),
15181 utp->number);
15182
15183 event_location_up location = string_to_event_location (&addr_str,
15184 current_language);
15185 if (!create_breakpoint (get_current_arch (),
15186 location.get (),
15187 utp->cond_string, -1, addr_str,
15188 0 /* parse cond/thread */,
15189 0 /* tempflag */,
15190 utp->type /* type_wanted */,
15191 0 /* Ignore count */,
15192 pending_break_support,
15193 &tracepoint_breakpoint_ops,
15194 0 /* from_tty */,
15195 utp->enabled /* enabled */,
15196 0 /* internal */,
15197 CREATE_BREAKPOINT_FLAGS_INSERTED))
15198 return NULL;
15199
15200 /* Get the tracepoint we just created. */
15201 tp = get_tracepoint (tracepoint_count);
15202 gdb_assert (tp != NULL);
15203
15204 if (utp->pass > 0)
15205 {
15206 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15207 tp->number);
15208
15209 trace_pass_command (small_buf, 0);
15210 }
15211
15212 /* If we have uploaded versions of the original commands, set up a
15213 special-purpose "reader" function and call the usual command line
15214 reader, then pass the result to the breakpoint command-setting
15215 function. */
15216 if (!VEC_empty (char_ptr, utp->cmd_strings))
15217 {
15218 command_line_up cmd_list;
15219
15220 this_utp = utp;
15221 next_cmd = 0;
15222
15223 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15224
15225 breakpoint_set_commands (tp, std::move (cmd_list));
15226 }
15227 else if (!VEC_empty (char_ptr, utp->actions)
15228 || !VEC_empty (char_ptr, utp->step_actions))
15229 warning (_("Uploaded tracepoint %d actions "
15230 "have no source form, ignoring them"),
15231 utp->number);
15232
15233 /* Copy any status information that might be available. */
15234 tp->hit_count = utp->hit_count;
15235 tp->traceframe_usage = utp->traceframe_usage;
15236
15237 return tp;
15238 }
15239
15240 /* Print information on tracepoint number TPNUM_EXP, or all if
15241 omitted. */
15242
15243 static void
15244 tracepoints_info (char *args, int from_tty)
15245 {
15246 struct ui_out *uiout = current_uiout;
15247 int num_printed;
15248
15249 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15250
15251 if (num_printed == 0)
15252 {
15253 if (args == NULL || *args == '\0')
15254 uiout->message ("No tracepoints.\n");
15255 else
15256 uiout->message ("No tracepoint matching '%s'.\n", args);
15257 }
15258
15259 default_collect_info ();
15260 }
15261
15262 /* The 'enable trace' command enables tracepoints.
15263 Not supported by all targets. */
15264 static void
15265 enable_trace_command (char *args, int from_tty)
15266 {
15267 enable_command (args, from_tty);
15268 }
15269
15270 /* The 'disable trace' command disables tracepoints.
15271 Not supported by all targets. */
15272 static void
15273 disable_trace_command (char *args, int from_tty)
15274 {
15275 disable_command (args, from_tty);
15276 }
15277
15278 /* Remove a tracepoint (or all if no argument). */
15279 static void
15280 delete_trace_command (char *arg, int from_tty)
15281 {
15282 struct breakpoint *b, *b_tmp;
15283
15284 dont_repeat ();
15285
15286 if (arg == 0)
15287 {
15288 int breaks_to_delete = 0;
15289
15290 /* Delete all breakpoints if no argument.
15291 Do not delete internal or call-dummy breakpoints, these
15292 have to be deleted with an explicit breakpoint number
15293 argument. */
15294 ALL_TRACEPOINTS (b)
15295 if (is_tracepoint (b) && user_breakpoint_p (b))
15296 {
15297 breaks_to_delete = 1;
15298 break;
15299 }
15300
15301 /* Ask user only if there are some breakpoints to delete. */
15302 if (!from_tty
15303 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15304 {
15305 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15306 if (is_tracepoint (b) && user_breakpoint_p (b))
15307 delete_breakpoint (b);
15308 }
15309 }
15310 else
15311 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15312 }
15313
15314 /* Helper function for trace_pass_command. */
15315
15316 static void
15317 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15318 {
15319 tp->pass_count = count;
15320 observer_notify_breakpoint_modified (tp);
15321 if (from_tty)
15322 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15323 tp->number, count);
15324 }
15325
15326 /* Set passcount for tracepoint.
15327
15328 First command argument is passcount, second is tracepoint number.
15329 If tracepoint number omitted, apply to most recently defined.
15330 Also accepts special argument "all". */
15331
15332 static void
15333 trace_pass_command (char *args, int from_tty)
15334 {
15335 struct tracepoint *t1;
15336 unsigned int count;
15337
15338 if (args == 0 || *args == 0)
15339 error (_("passcount command requires an "
15340 "argument (count + optional TP num)"));
15341
15342 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15343
15344 args = skip_spaces (args);
15345 if (*args && strncasecmp (args, "all", 3) == 0)
15346 {
15347 struct breakpoint *b;
15348
15349 args += 3; /* Skip special argument "all". */
15350 if (*args)
15351 error (_("Junk at end of arguments."));
15352
15353 ALL_TRACEPOINTS (b)
15354 {
15355 t1 = (struct tracepoint *) b;
15356 trace_pass_set_count (t1, count, from_tty);
15357 }
15358 }
15359 else if (*args == '\0')
15360 {
15361 t1 = get_tracepoint_by_number (&args, NULL);
15362 if (t1)
15363 trace_pass_set_count (t1, count, from_tty);
15364 }
15365 else
15366 {
15367 number_or_range_parser parser (args);
15368 while (!parser.finished ())
15369 {
15370 t1 = get_tracepoint_by_number (&args, &parser);
15371 if (t1)
15372 trace_pass_set_count (t1, count, from_tty);
15373 }
15374 }
15375 }
15376
15377 struct tracepoint *
15378 get_tracepoint (int num)
15379 {
15380 struct breakpoint *t;
15381
15382 ALL_TRACEPOINTS (t)
15383 if (t->number == num)
15384 return (struct tracepoint *) t;
15385
15386 return NULL;
15387 }
15388
15389 /* Find the tracepoint with the given target-side number (which may be
15390 different from the tracepoint number after disconnecting and
15391 reconnecting). */
15392
15393 struct tracepoint *
15394 get_tracepoint_by_number_on_target (int num)
15395 {
15396 struct breakpoint *b;
15397
15398 ALL_TRACEPOINTS (b)
15399 {
15400 struct tracepoint *t = (struct tracepoint *) b;
15401
15402 if (t->number_on_target == num)
15403 return t;
15404 }
15405
15406 return NULL;
15407 }
15408
15409 /* Utility: parse a tracepoint number and look it up in the list.
15410 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15411 If the argument is missing, the most recent tracepoint
15412 (tracepoint_count) is returned. */
15413
15414 struct tracepoint *
15415 get_tracepoint_by_number (char **arg,
15416 number_or_range_parser *parser)
15417 {
15418 struct breakpoint *t;
15419 int tpnum;
15420 char *instring = arg == NULL ? NULL : *arg;
15421
15422 if (parser != NULL)
15423 {
15424 gdb_assert (!parser->finished ());
15425 tpnum = parser->get_number ();
15426 }
15427 else if (arg == NULL || *arg == NULL || ! **arg)
15428 tpnum = tracepoint_count;
15429 else
15430 tpnum = get_number (arg);
15431
15432 if (tpnum <= 0)
15433 {
15434 if (instring && *instring)
15435 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15436 instring);
15437 else
15438 printf_filtered (_("No previous tracepoint\n"));
15439 return NULL;
15440 }
15441
15442 ALL_TRACEPOINTS (t)
15443 if (t->number == tpnum)
15444 {
15445 return (struct tracepoint *) t;
15446 }
15447
15448 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15449 return NULL;
15450 }
15451
15452 void
15453 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15454 {
15455 if (b->thread != -1)
15456 fprintf_unfiltered (fp, " thread %d", b->thread);
15457
15458 if (b->task != 0)
15459 fprintf_unfiltered (fp, " task %d", b->task);
15460
15461 fprintf_unfiltered (fp, "\n");
15462 }
15463
15464 /* Save information on user settable breakpoints (watchpoints, etc) to
15465 a new script file named FILENAME. If FILTER is non-NULL, call it
15466 on each breakpoint and only include the ones for which it returns
15467 non-zero. */
15468
15469 static void
15470 save_breakpoints (char *filename, int from_tty,
15471 int (*filter) (const struct breakpoint *))
15472 {
15473 struct breakpoint *tp;
15474 int any = 0;
15475 int extra_trace_bits = 0;
15476
15477 if (filename == 0 || *filename == 0)
15478 error (_("Argument required (file name in which to save)"));
15479
15480 /* See if we have anything to save. */
15481 ALL_BREAKPOINTS (tp)
15482 {
15483 /* Skip internal and momentary breakpoints. */
15484 if (!user_breakpoint_p (tp))
15485 continue;
15486
15487 /* If we have a filter, only save the breakpoints it accepts. */
15488 if (filter && !filter (tp))
15489 continue;
15490
15491 any = 1;
15492
15493 if (is_tracepoint (tp))
15494 {
15495 extra_trace_bits = 1;
15496
15497 /* We can stop searching. */
15498 break;
15499 }
15500 }
15501
15502 if (!any)
15503 {
15504 warning (_("Nothing to save."));
15505 return;
15506 }
15507
15508 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
15509
15510 stdio_file fp;
15511
15512 if (!fp.open (expanded_filename.get (), "w"))
15513 error (_("Unable to open file '%s' for saving (%s)"),
15514 expanded_filename.get (), safe_strerror (errno));
15515
15516 if (extra_trace_bits)
15517 save_trace_state_variables (&fp);
15518
15519 ALL_BREAKPOINTS (tp)
15520 {
15521 /* Skip internal and momentary breakpoints. */
15522 if (!user_breakpoint_p (tp))
15523 continue;
15524
15525 /* If we have a filter, only save the breakpoints it accepts. */
15526 if (filter && !filter (tp))
15527 continue;
15528
15529 tp->ops->print_recreate (tp, &fp);
15530
15531 /* Note, we can't rely on tp->number for anything, as we can't
15532 assume the recreated breakpoint numbers will match. Use $bpnum
15533 instead. */
15534
15535 if (tp->cond_string)
15536 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15537
15538 if (tp->ignore_count)
15539 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15540
15541 if (tp->type != bp_dprintf && tp->commands)
15542 {
15543 fp.puts (" commands\n");
15544
15545 current_uiout->redirect (&fp);
15546 TRY
15547 {
15548 print_command_lines (current_uiout, tp->commands->commands, 2);
15549 }
15550 CATCH (ex, RETURN_MASK_ALL)
15551 {
15552 current_uiout->redirect (NULL);
15553 throw_exception (ex);
15554 }
15555 END_CATCH
15556
15557 current_uiout->redirect (NULL);
15558 fp.puts (" end\n");
15559 }
15560
15561 if (tp->enable_state == bp_disabled)
15562 fp.puts ("disable $bpnum\n");
15563
15564 /* If this is a multi-location breakpoint, check if the locations
15565 should be individually disabled. Watchpoint locations are
15566 special, and not user visible. */
15567 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15568 {
15569 struct bp_location *loc;
15570 int n = 1;
15571
15572 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15573 if (!loc->enabled)
15574 fp.printf ("disable $bpnum.%d\n", n);
15575 }
15576 }
15577
15578 if (extra_trace_bits && *default_collect)
15579 fp.printf ("set default-collect %s\n", default_collect);
15580
15581 if (from_tty)
15582 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
15583 }
15584
15585 /* The `save breakpoints' command. */
15586
15587 static void
15588 save_breakpoints_command (char *args, int from_tty)
15589 {
15590 save_breakpoints (args, from_tty, NULL);
15591 }
15592
15593 /* The `save tracepoints' command. */
15594
15595 static void
15596 save_tracepoints_command (char *args, int from_tty)
15597 {
15598 save_breakpoints (args, from_tty, is_tracepoint);
15599 }
15600
15601 /* Create a vector of all tracepoints. */
15602
15603 VEC(breakpoint_p) *
15604 all_tracepoints (void)
15605 {
15606 VEC(breakpoint_p) *tp_vec = 0;
15607 struct breakpoint *tp;
15608
15609 ALL_TRACEPOINTS (tp)
15610 {
15611 VEC_safe_push (breakpoint_p, tp_vec, tp);
15612 }
15613
15614 return tp_vec;
15615 }
15616
15617 \f
15618 /* This help string is used to consolidate all the help string for specifying
15619 locations used by several commands. */
15620
15621 #define LOCATION_HELP_STRING \
15622 "Linespecs are colon-separated lists of location parameters, such as\n\
15623 source filename, function name, label name, and line number.\n\
15624 Example: To specify the start of a label named \"the_top\" in the\n\
15625 function \"fact\" in the file \"factorial.c\", use\n\
15626 \"factorial.c:fact:the_top\".\n\
15627 \n\
15628 Address locations begin with \"*\" and specify an exact address in the\n\
15629 program. Example: To specify the fourth byte past the start function\n\
15630 \"main\", use \"*main + 4\".\n\
15631 \n\
15632 Explicit locations are similar to linespecs but use an option/argument\n\
15633 syntax to specify location parameters.\n\
15634 Example: To specify the start of the label named \"the_top\" in the\n\
15635 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15636 -function fact -label the_top\".\n"
15637
15638 /* This help string is used for the break, hbreak, tbreak and thbreak
15639 commands. It is defined as a macro to prevent duplication.
15640 COMMAND should be a string constant containing the name of the
15641 command. */
15642
15643 #define BREAK_ARGS_HELP(command) \
15644 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15645 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15646 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15647 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15648 `-probe-dtrace' (for a DTrace probe).\n\
15649 LOCATION may be a linespec, address, or explicit location as described\n\
15650 below.\n\
15651 \n\
15652 With no LOCATION, uses current execution address of the selected\n\
15653 stack frame. This is useful for breaking on return to a stack frame.\n\
15654 \n\
15655 THREADNUM is the number from \"info threads\".\n\
15656 CONDITION is a boolean expression.\n\
15657 \n" LOCATION_HELP_STRING "\n\
15658 Multiple breakpoints at one place are permitted, and useful if their\n\
15659 conditions are different.\n\
15660 \n\
15661 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15662
15663 /* List of subcommands for "catch". */
15664 static struct cmd_list_element *catch_cmdlist;
15665
15666 /* List of subcommands for "tcatch". */
15667 static struct cmd_list_element *tcatch_cmdlist;
15668
15669 void
15670 add_catch_command (const char *name, const char *docstring,
15671 cmd_sfunc_ftype *sfunc,
15672 completer_ftype *completer,
15673 void *user_data_catch,
15674 void *user_data_tcatch)
15675 {
15676 struct cmd_list_element *command;
15677
15678 command = add_cmd (name, class_breakpoint, NULL, docstring,
15679 &catch_cmdlist);
15680 set_cmd_sfunc (command, sfunc);
15681 set_cmd_context (command, user_data_catch);
15682 set_cmd_completer (command, completer);
15683
15684 command = add_cmd (name, class_breakpoint, NULL, docstring,
15685 &tcatch_cmdlist);
15686 set_cmd_sfunc (command, sfunc);
15687 set_cmd_context (command, user_data_tcatch);
15688 set_cmd_completer (command, completer);
15689 }
15690
15691 static void
15692 save_command (char *arg, int from_tty)
15693 {
15694 printf_unfiltered (_("\"save\" must be followed by "
15695 "the name of a save subcommand.\n"));
15696 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15697 }
15698
15699 struct breakpoint *
15700 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15701 void *data)
15702 {
15703 struct breakpoint *b, *b_tmp;
15704
15705 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15706 {
15707 if ((*callback) (b, data))
15708 return b;
15709 }
15710
15711 return NULL;
15712 }
15713
15714 /* Zero if any of the breakpoint's locations could be a location where
15715 functions have been inlined, nonzero otherwise. */
15716
15717 static int
15718 is_non_inline_function (struct breakpoint *b)
15719 {
15720 /* The shared library event breakpoint is set on the address of a
15721 non-inline function. */
15722 if (b->type == bp_shlib_event)
15723 return 1;
15724
15725 return 0;
15726 }
15727
15728 /* Nonzero if the specified PC cannot be a location where functions
15729 have been inlined. */
15730
15731 int
15732 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15733 const struct target_waitstatus *ws)
15734 {
15735 struct breakpoint *b;
15736 struct bp_location *bl;
15737
15738 ALL_BREAKPOINTS (b)
15739 {
15740 if (!is_non_inline_function (b))
15741 continue;
15742
15743 for (bl = b->loc; bl != NULL; bl = bl->next)
15744 {
15745 if (!bl->shlib_disabled
15746 && bpstat_check_location (bl, aspace, pc, ws))
15747 return 1;
15748 }
15749 }
15750
15751 return 0;
15752 }
15753
15754 /* Remove any references to OBJFILE which is going to be freed. */
15755
15756 void
15757 breakpoint_free_objfile (struct objfile *objfile)
15758 {
15759 struct bp_location **locp, *loc;
15760
15761 ALL_BP_LOCATIONS (loc, locp)
15762 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15763 loc->symtab = NULL;
15764 }
15765
15766 void
15767 initialize_breakpoint_ops (void)
15768 {
15769 static int initialized = 0;
15770
15771 struct breakpoint_ops *ops;
15772
15773 if (initialized)
15774 return;
15775 initialized = 1;
15776
15777 /* The breakpoint_ops structure to be inherit by all kinds of
15778 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15779 internal and momentary breakpoints, etc.). */
15780 ops = &bkpt_base_breakpoint_ops;
15781 *ops = base_breakpoint_ops;
15782 ops->re_set = bkpt_re_set;
15783 ops->insert_location = bkpt_insert_location;
15784 ops->remove_location = bkpt_remove_location;
15785 ops->breakpoint_hit = bkpt_breakpoint_hit;
15786 ops->create_sals_from_location = bkpt_create_sals_from_location;
15787 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15788 ops->decode_location = bkpt_decode_location;
15789
15790 /* The breakpoint_ops structure to be used in regular breakpoints. */
15791 ops = &bkpt_breakpoint_ops;
15792 *ops = bkpt_base_breakpoint_ops;
15793 ops->re_set = bkpt_re_set;
15794 ops->resources_needed = bkpt_resources_needed;
15795 ops->print_it = bkpt_print_it;
15796 ops->print_mention = bkpt_print_mention;
15797 ops->print_recreate = bkpt_print_recreate;
15798
15799 /* Ranged breakpoints. */
15800 ops = &ranged_breakpoint_ops;
15801 *ops = bkpt_breakpoint_ops;
15802 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15803 ops->resources_needed = resources_needed_ranged_breakpoint;
15804 ops->print_it = print_it_ranged_breakpoint;
15805 ops->print_one = print_one_ranged_breakpoint;
15806 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15807 ops->print_mention = print_mention_ranged_breakpoint;
15808 ops->print_recreate = print_recreate_ranged_breakpoint;
15809
15810 /* Internal breakpoints. */
15811 ops = &internal_breakpoint_ops;
15812 *ops = bkpt_base_breakpoint_ops;
15813 ops->re_set = internal_bkpt_re_set;
15814 ops->check_status = internal_bkpt_check_status;
15815 ops->print_it = internal_bkpt_print_it;
15816 ops->print_mention = internal_bkpt_print_mention;
15817
15818 /* Momentary breakpoints. */
15819 ops = &momentary_breakpoint_ops;
15820 *ops = bkpt_base_breakpoint_ops;
15821 ops->re_set = momentary_bkpt_re_set;
15822 ops->check_status = momentary_bkpt_check_status;
15823 ops->print_it = momentary_bkpt_print_it;
15824 ops->print_mention = momentary_bkpt_print_mention;
15825
15826 /* Probe breakpoints. */
15827 ops = &bkpt_probe_breakpoint_ops;
15828 *ops = bkpt_breakpoint_ops;
15829 ops->insert_location = bkpt_probe_insert_location;
15830 ops->remove_location = bkpt_probe_remove_location;
15831 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15832 ops->decode_location = bkpt_probe_decode_location;
15833
15834 /* Watchpoints. */
15835 ops = &watchpoint_breakpoint_ops;
15836 *ops = base_breakpoint_ops;
15837 ops->re_set = re_set_watchpoint;
15838 ops->insert_location = insert_watchpoint;
15839 ops->remove_location = remove_watchpoint;
15840 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15841 ops->check_status = check_status_watchpoint;
15842 ops->resources_needed = resources_needed_watchpoint;
15843 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15844 ops->print_it = print_it_watchpoint;
15845 ops->print_mention = print_mention_watchpoint;
15846 ops->print_recreate = print_recreate_watchpoint;
15847 ops->explains_signal = explains_signal_watchpoint;
15848
15849 /* Masked watchpoints. */
15850 ops = &masked_watchpoint_breakpoint_ops;
15851 *ops = watchpoint_breakpoint_ops;
15852 ops->insert_location = insert_masked_watchpoint;
15853 ops->remove_location = remove_masked_watchpoint;
15854 ops->resources_needed = resources_needed_masked_watchpoint;
15855 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15856 ops->print_it = print_it_masked_watchpoint;
15857 ops->print_one_detail = print_one_detail_masked_watchpoint;
15858 ops->print_mention = print_mention_masked_watchpoint;
15859 ops->print_recreate = print_recreate_masked_watchpoint;
15860
15861 /* Tracepoints. */
15862 ops = &tracepoint_breakpoint_ops;
15863 *ops = base_breakpoint_ops;
15864 ops->re_set = tracepoint_re_set;
15865 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15866 ops->print_one_detail = tracepoint_print_one_detail;
15867 ops->print_mention = tracepoint_print_mention;
15868 ops->print_recreate = tracepoint_print_recreate;
15869 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15870 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15871 ops->decode_location = tracepoint_decode_location;
15872
15873 /* Probe tracepoints. */
15874 ops = &tracepoint_probe_breakpoint_ops;
15875 *ops = tracepoint_breakpoint_ops;
15876 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15877 ops->decode_location = tracepoint_probe_decode_location;
15878
15879 /* Static tracepoints with marker (`-m'). */
15880 ops = &strace_marker_breakpoint_ops;
15881 *ops = tracepoint_breakpoint_ops;
15882 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15883 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15884 ops->decode_location = strace_marker_decode_location;
15885
15886 /* Fork catchpoints. */
15887 ops = &catch_fork_breakpoint_ops;
15888 *ops = base_breakpoint_ops;
15889 ops->insert_location = insert_catch_fork;
15890 ops->remove_location = remove_catch_fork;
15891 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15892 ops->print_it = print_it_catch_fork;
15893 ops->print_one = print_one_catch_fork;
15894 ops->print_mention = print_mention_catch_fork;
15895 ops->print_recreate = print_recreate_catch_fork;
15896
15897 /* Vfork catchpoints. */
15898 ops = &catch_vfork_breakpoint_ops;
15899 *ops = base_breakpoint_ops;
15900 ops->insert_location = insert_catch_vfork;
15901 ops->remove_location = remove_catch_vfork;
15902 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15903 ops->print_it = print_it_catch_vfork;
15904 ops->print_one = print_one_catch_vfork;
15905 ops->print_mention = print_mention_catch_vfork;
15906 ops->print_recreate = print_recreate_catch_vfork;
15907
15908 /* Exec catchpoints. */
15909 ops = &catch_exec_breakpoint_ops;
15910 *ops = base_breakpoint_ops;
15911 ops->insert_location = insert_catch_exec;
15912 ops->remove_location = remove_catch_exec;
15913 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15914 ops->print_it = print_it_catch_exec;
15915 ops->print_one = print_one_catch_exec;
15916 ops->print_mention = print_mention_catch_exec;
15917 ops->print_recreate = print_recreate_catch_exec;
15918
15919 /* Solib-related catchpoints. */
15920 ops = &catch_solib_breakpoint_ops;
15921 *ops = base_breakpoint_ops;
15922 ops->insert_location = insert_catch_solib;
15923 ops->remove_location = remove_catch_solib;
15924 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15925 ops->check_status = check_status_catch_solib;
15926 ops->print_it = print_it_catch_solib;
15927 ops->print_one = print_one_catch_solib;
15928 ops->print_mention = print_mention_catch_solib;
15929 ops->print_recreate = print_recreate_catch_solib;
15930
15931 ops = &dprintf_breakpoint_ops;
15932 *ops = bkpt_base_breakpoint_ops;
15933 ops->re_set = dprintf_re_set;
15934 ops->resources_needed = bkpt_resources_needed;
15935 ops->print_it = bkpt_print_it;
15936 ops->print_mention = bkpt_print_mention;
15937 ops->print_recreate = dprintf_print_recreate;
15938 ops->after_condition_true = dprintf_after_condition_true;
15939 ops->breakpoint_hit = dprintf_breakpoint_hit;
15940 }
15941
15942 /* Chain containing all defined "enable breakpoint" subcommands. */
15943
15944 static struct cmd_list_element *enablebreaklist = NULL;
15945
15946 void
15947 _initialize_breakpoint (void)
15948 {
15949 struct cmd_list_element *c;
15950
15951 initialize_breakpoint_ops ();
15952
15953 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15954 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15955 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15956
15957 breakpoint_objfile_key
15958 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15959
15960 breakpoint_chain = 0;
15961 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15962 before a breakpoint is set. */
15963 breakpoint_count = 0;
15964
15965 tracepoint_count = 0;
15966
15967 add_com ("ignore", class_breakpoint, ignore_command, _("\
15968 Set ignore-count of breakpoint number N to COUNT.\n\
15969 Usage is `ignore N COUNT'."));
15970
15971 add_com ("commands", class_breakpoint, commands_command, _("\
15972 Set commands to be executed when the given breakpoints are hit.\n\
15973 Give a space-separated breakpoint list as argument after \"commands\".\n\
15974 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15975 (e.g. `5-7').\n\
15976 With no argument, the targeted breakpoint is the last one set.\n\
15977 The commands themselves follow starting on the next line.\n\
15978 Type a line containing \"end\" to indicate the end of them.\n\
15979 Give \"silent\" as the first line to make the breakpoint silent;\n\
15980 then no output is printed when it is hit, except what the commands print."));
15981
15982 c = add_com ("condition", class_breakpoint, condition_command, _("\
15983 Specify breakpoint number N to break only if COND is true.\n\
15984 Usage is `condition N COND', where N is an integer and COND is an\n\
15985 expression to be evaluated whenever breakpoint N is reached."));
15986 set_cmd_completer (c, condition_completer);
15987
15988 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15989 Set a temporary breakpoint.\n\
15990 Like \"break\" except the breakpoint is only temporary,\n\
15991 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15992 by using \"enable delete\" on the breakpoint number.\n\
15993 \n"
15994 BREAK_ARGS_HELP ("tbreak")));
15995 set_cmd_completer (c, location_completer);
15996
15997 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15998 Set a hardware assisted breakpoint.\n\
15999 Like \"break\" except the breakpoint requires hardware support,\n\
16000 some target hardware may not have this support.\n\
16001 \n"
16002 BREAK_ARGS_HELP ("hbreak")));
16003 set_cmd_completer (c, location_completer);
16004
16005 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16006 Set a temporary hardware assisted breakpoint.\n\
16007 Like \"hbreak\" except the breakpoint is only temporary,\n\
16008 so it will be deleted when hit.\n\
16009 \n"
16010 BREAK_ARGS_HELP ("thbreak")));
16011 set_cmd_completer (c, location_completer);
16012
16013 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16014 Enable some breakpoints.\n\
16015 Give breakpoint numbers (separated by spaces) as arguments.\n\
16016 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16017 This is used to cancel the effect of the \"disable\" command.\n\
16018 With a subcommand you can enable temporarily."),
16019 &enablelist, "enable ", 1, &cmdlist);
16020
16021 add_com_alias ("en", "enable", class_breakpoint, 1);
16022
16023 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16024 Enable some breakpoints.\n\
16025 Give breakpoint numbers (separated by spaces) as arguments.\n\
16026 This is used to cancel the effect of the \"disable\" command.\n\
16027 May be abbreviated to simply \"enable\".\n"),
16028 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16029
16030 add_cmd ("once", no_class, enable_once_command, _("\
16031 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16032 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16033 &enablebreaklist);
16034
16035 add_cmd ("delete", no_class, enable_delete_command, _("\
16036 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16037 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16038 &enablebreaklist);
16039
16040 add_cmd ("count", no_class, enable_count_command, _("\
16041 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16042 If a breakpoint is hit while enabled in this fashion,\n\
16043 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16044 &enablebreaklist);
16045
16046 add_cmd ("delete", no_class, enable_delete_command, _("\
16047 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16048 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16049 &enablelist);
16050
16051 add_cmd ("once", no_class, enable_once_command, _("\
16052 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16053 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16054 &enablelist);
16055
16056 add_cmd ("count", no_class, enable_count_command, _("\
16057 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16058 If a breakpoint is hit while enabled in this fashion,\n\
16059 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16060 &enablelist);
16061
16062 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16063 Disable some breakpoints.\n\
16064 Arguments are breakpoint numbers with spaces in between.\n\
16065 To disable all breakpoints, give no argument.\n\
16066 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16067 &disablelist, "disable ", 1, &cmdlist);
16068 add_com_alias ("dis", "disable", class_breakpoint, 1);
16069 add_com_alias ("disa", "disable", class_breakpoint, 1);
16070
16071 add_cmd ("breakpoints", class_alias, disable_command, _("\
16072 Disable some breakpoints.\n\
16073 Arguments are breakpoint numbers with spaces in between.\n\
16074 To disable all breakpoints, give no argument.\n\
16075 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16076 This command may be abbreviated \"disable\"."),
16077 &disablelist);
16078
16079 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16080 Delete some breakpoints or auto-display expressions.\n\
16081 Arguments are breakpoint numbers with spaces in between.\n\
16082 To delete all breakpoints, give no argument.\n\
16083 \n\
16084 Also a prefix command for deletion of other GDB objects.\n\
16085 The \"unset\" command is also an alias for \"delete\"."),
16086 &deletelist, "delete ", 1, &cmdlist);
16087 add_com_alias ("d", "delete", class_breakpoint, 1);
16088 add_com_alias ("del", "delete", class_breakpoint, 1);
16089
16090 add_cmd ("breakpoints", class_alias, delete_command, _("\
16091 Delete some breakpoints or auto-display expressions.\n\
16092 Arguments are breakpoint numbers with spaces in between.\n\
16093 To delete all breakpoints, give no argument.\n\
16094 This command may be abbreviated \"delete\"."),
16095 &deletelist);
16096
16097 add_com ("clear", class_breakpoint, clear_command, _("\
16098 Clear breakpoint at specified location.\n\
16099 Argument may be a linespec, explicit, or address location as described below.\n\
16100 \n\
16101 With no argument, clears all breakpoints in the line that the selected frame\n\
16102 is executing in.\n"
16103 "\n" LOCATION_HELP_STRING "\n\
16104 See also the \"delete\" command which clears breakpoints by number."));
16105 add_com_alias ("cl", "clear", class_breakpoint, 1);
16106
16107 c = add_com ("break", class_breakpoint, break_command, _("\
16108 Set breakpoint at specified location.\n"
16109 BREAK_ARGS_HELP ("break")));
16110 set_cmd_completer (c, location_completer);
16111
16112 add_com_alias ("b", "break", class_run, 1);
16113 add_com_alias ("br", "break", class_run, 1);
16114 add_com_alias ("bre", "break", class_run, 1);
16115 add_com_alias ("brea", "break", class_run, 1);
16116
16117 if (dbx_commands)
16118 {
16119 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16120 Break in function/address or break at a line in the current file."),
16121 &stoplist, "stop ", 1, &cmdlist);
16122 add_cmd ("in", class_breakpoint, stopin_command,
16123 _("Break in function or address."), &stoplist);
16124 add_cmd ("at", class_breakpoint, stopat_command,
16125 _("Break at a line in the current file."), &stoplist);
16126 add_com ("status", class_info, breakpoints_info, _("\
16127 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16128 The \"Type\" column indicates one of:\n\
16129 \tbreakpoint - normal breakpoint\n\
16130 \twatchpoint - watchpoint\n\
16131 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16132 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16133 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16134 address and file/line number respectively.\n\
16135 \n\
16136 Convenience variable \"$_\" and default examine address for \"x\"\n\
16137 are set to the address of the last breakpoint listed unless the command\n\
16138 is prefixed with \"server \".\n\n\
16139 Convenience variable \"$bpnum\" contains the number of the last\n\
16140 breakpoint set."));
16141 }
16142
16143 add_info ("breakpoints", breakpoints_info, _("\
16144 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16145 The \"Type\" column indicates one of:\n\
16146 \tbreakpoint - normal breakpoint\n\
16147 \twatchpoint - watchpoint\n\
16148 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16149 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16150 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16151 address and file/line number respectively.\n\
16152 \n\
16153 Convenience variable \"$_\" and default examine address for \"x\"\n\
16154 are set to the address of the last breakpoint listed unless the command\n\
16155 is prefixed with \"server \".\n\n\
16156 Convenience variable \"$bpnum\" contains the number of the last\n\
16157 breakpoint set."));
16158
16159 add_info_alias ("b", "breakpoints", 1);
16160
16161 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16162 Status of all breakpoints, or breakpoint number NUMBER.\n\
16163 The \"Type\" column indicates one of:\n\
16164 \tbreakpoint - normal breakpoint\n\
16165 \twatchpoint - watchpoint\n\
16166 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16167 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16168 \tuntil - internal breakpoint used by the \"until\" command\n\
16169 \tfinish - internal breakpoint used by the \"finish\" command\n\
16170 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16171 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16172 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16173 address and file/line number respectively.\n\
16174 \n\
16175 Convenience variable \"$_\" and default examine address for \"x\"\n\
16176 are set to the address of the last breakpoint listed unless the command\n\
16177 is prefixed with \"server \".\n\n\
16178 Convenience variable \"$bpnum\" contains the number of the last\n\
16179 breakpoint set."),
16180 &maintenanceinfolist);
16181
16182 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16183 Set catchpoints to catch events."),
16184 &catch_cmdlist, "catch ",
16185 0/*allow-unknown*/, &cmdlist);
16186
16187 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16188 Set temporary catchpoints to catch events."),
16189 &tcatch_cmdlist, "tcatch ",
16190 0/*allow-unknown*/, &cmdlist);
16191
16192 add_catch_command ("fork", _("Catch calls to fork."),
16193 catch_fork_command_1,
16194 NULL,
16195 (void *) (uintptr_t) catch_fork_permanent,
16196 (void *) (uintptr_t) catch_fork_temporary);
16197 add_catch_command ("vfork", _("Catch calls to vfork."),
16198 catch_fork_command_1,
16199 NULL,
16200 (void *) (uintptr_t) catch_vfork_permanent,
16201 (void *) (uintptr_t) catch_vfork_temporary);
16202 add_catch_command ("exec", _("Catch calls to exec."),
16203 catch_exec_command_1,
16204 NULL,
16205 CATCH_PERMANENT,
16206 CATCH_TEMPORARY);
16207 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16208 Usage: catch load [REGEX]\n\
16209 If REGEX is given, only stop for libraries matching the regular expression."),
16210 catch_load_command_1,
16211 NULL,
16212 CATCH_PERMANENT,
16213 CATCH_TEMPORARY);
16214 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16215 Usage: catch unload [REGEX]\n\
16216 If REGEX is given, only stop for libraries matching the regular expression."),
16217 catch_unload_command_1,
16218 NULL,
16219 CATCH_PERMANENT,
16220 CATCH_TEMPORARY);
16221
16222 c = add_com ("watch", class_breakpoint, watch_command, _("\
16223 Set a watchpoint for an expression.\n\
16224 Usage: watch [-l|-location] EXPRESSION\n\
16225 A watchpoint stops execution of your program whenever the value of\n\
16226 an expression changes.\n\
16227 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16228 the memory to which it refers."));
16229 set_cmd_completer (c, expression_completer);
16230
16231 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16232 Set a read watchpoint for an expression.\n\
16233 Usage: rwatch [-l|-location] EXPRESSION\n\
16234 A watchpoint stops execution of your program whenever the value of\n\
16235 an expression is read.\n\
16236 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16237 the memory to which it refers."));
16238 set_cmd_completer (c, expression_completer);
16239
16240 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16241 Set a watchpoint for an expression.\n\
16242 Usage: awatch [-l|-location] EXPRESSION\n\
16243 A watchpoint stops execution of your program whenever the value of\n\
16244 an expression is either read or written.\n\
16245 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16246 the memory to which it refers."));
16247 set_cmd_completer (c, expression_completer);
16248
16249 add_info ("watchpoints", watchpoints_info, _("\
16250 Status of specified watchpoints (all watchpoints if no argument)."));
16251
16252 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16253 respond to changes - contrary to the description. */
16254 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16255 &can_use_hw_watchpoints, _("\
16256 Set debugger's willingness to use watchpoint hardware."), _("\
16257 Show debugger's willingness to use watchpoint hardware."), _("\
16258 If zero, gdb will not use hardware for new watchpoints, even if\n\
16259 such is available. (However, any hardware watchpoints that were\n\
16260 created before setting this to nonzero, will continue to use watchpoint\n\
16261 hardware.)"),
16262 NULL,
16263 show_can_use_hw_watchpoints,
16264 &setlist, &showlist);
16265
16266 can_use_hw_watchpoints = 1;
16267
16268 /* Tracepoint manipulation commands. */
16269
16270 c = add_com ("trace", class_breakpoint, trace_command, _("\
16271 Set a tracepoint at specified location.\n\
16272 \n"
16273 BREAK_ARGS_HELP ("trace") "\n\
16274 Do \"help tracepoints\" for info on other tracepoint commands."));
16275 set_cmd_completer (c, location_completer);
16276
16277 add_com_alias ("tp", "trace", class_alias, 0);
16278 add_com_alias ("tr", "trace", class_alias, 1);
16279 add_com_alias ("tra", "trace", class_alias, 1);
16280 add_com_alias ("trac", "trace", class_alias, 1);
16281
16282 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16283 Set a fast tracepoint at specified location.\n\
16284 \n"
16285 BREAK_ARGS_HELP ("ftrace") "\n\
16286 Do \"help tracepoints\" for info on other tracepoint commands."));
16287 set_cmd_completer (c, location_completer);
16288
16289 c = add_com ("strace", class_breakpoint, strace_command, _("\
16290 Set a static tracepoint at location or marker.\n\
16291 \n\
16292 strace [LOCATION] [if CONDITION]\n\
16293 LOCATION may be a linespec, explicit, or address location (described below) \n\
16294 or -m MARKER_ID.\n\n\
16295 If a marker id is specified, probe the marker with that name. With\n\
16296 no LOCATION, uses current execution address of the selected stack frame.\n\
16297 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16298 This collects arbitrary user data passed in the probe point call to the\n\
16299 tracing library. You can inspect it when analyzing the trace buffer,\n\
16300 by printing the $_sdata variable like any other convenience variable.\n\
16301 \n\
16302 CONDITION is a boolean expression.\n\
16303 \n" LOCATION_HELP_STRING "\n\
16304 Multiple tracepoints at one place are permitted, and useful if their\n\
16305 conditions are different.\n\
16306 \n\
16307 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16308 Do \"help tracepoints\" for info on other tracepoint commands."));
16309 set_cmd_completer (c, location_completer);
16310
16311 add_info ("tracepoints", tracepoints_info, _("\
16312 Status of specified tracepoints (all tracepoints if no argument).\n\
16313 Convenience variable \"$tpnum\" contains the number of the\n\
16314 last tracepoint set."));
16315
16316 add_info_alias ("tp", "tracepoints", 1);
16317
16318 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16319 Delete specified tracepoints.\n\
16320 Arguments are tracepoint numbers, separated by spaces.\n\
16321 No argument means delete all tracepoints."),
16322 &deletelist);
16323 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16324
16325 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16326 Disable specified tracepoints.\n\
16327 Arguments are tracepoint numbers, separated by spaces.\n\
16328 No argument means disable all tracepoints."),
16329 &disablelist);
16330 deprecate_cmd (c, "disable");
16331
16332 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16333 Enable specified tracepoints.\n\
16334 Arguments are tracepoint numbers, separated by spaces.\n\
16335 No argument means enable all tracepoints."),
16336 &enablelist);
16337 deprecate_cmd (c, "enable");
16338
16339 add_com ("passcount", class_trace, trace_pass_command, _("\
16340 Set the passcount for a tracepoint.\n\
16341 The trace will end when the tracepoint has been passed 'count' times.\n\
16342 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16343 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16344
16345 add_prefix_cmd ("save", class_breakpoint, save_command,
16346 _("Save breakpoint definitions as a script."),
16347 &save_cmdlist, "save ",
16348 0/*allow-unknown*/, &cmdlist);
16349
16350 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16351 Save current breakpoint definitions as a script.\n\
16352 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16353 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16354 session to restore them."),
16355 &save_cmdlist);
16356 set_cmd_completer (c, filename_completer);
16357
16358 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16359 Save current tracepoint definitions as a script.\n\
16360 Use the 'source' command in another debug session to restore them."),
16361 &save_cmdlist);
16362 set_cmd_completer (c, filename_completer);
16363
16364 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16365 deprecate_cmd (c, "save tracepoints");
16366
16367 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16368 Breakpoint specific settings\n\
16369 Configure various breakpoint-specific variables such as\n\
16370 pending breakpoint behavior"),
16371 &breakpoint_set_cmdlist, "set breakpoint ",
16372 0/*allow-unknown*/, &setlist);
16373 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16374 Breakpoint specific settings\n\
16375 Configure various breakpoint-specific variables such as\n\
16376 pending breakpoint behavior"),
16377 &breakpoint_show_cmdlist, "show breakpoint ",
16378 0/*allow-unknown*/, &showlist);
16379
16380 add_setshow_auto_boolean_cmd ("pending", no_class,
16381 &pending_break_support, _("\
16382 Set debugger's behavior regarding pending breakpoints."), _("\
16383 Show debugger's behavior regarding pending breakpoints."), _("\
16384 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16385 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16386 an error. If auto, an unrecognized breakpoint location results in a\n\
16387 user-query to see if a pending breakpoint should be created."),
16388 NULL,
16389 show_pending_break_support,
16390 &breakpoint_set_cmdlist,
16391 &breakpoint_show_cmdlist);
16392
16393 pending_break_support = AUTO_BOOLEAN_AUTO;
16394
16395 add_setshow_boolean_cmd ("auto-hw", no_class,
16396 &automatic_hardware_breakpoints, _("\
16397 Set automatic usage of hardware breakpoints."), _("\
16398 Show automatic usage of hardware breakpoints."), _("\
16399 If set, the debugger will automatically use hardware breakpoints for\n\
16400 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16401 a warning will be emitted for such breakpoints."),
16402 NULL,
16403 show_automatic_hardware_breakpoints,
16404 &breakpoint_set_cmdlist,
16405 &breakpoint_show_cmdlist);
16406
16407 add_setshow_boolean_cmd ("always-inserted", class_support,
16408 &always_inserted_mode, _("\
16409 Set mode for inserting breakpoints."), _("\
16410 Show mode for inserting breakpoints."), _("\
16411 When this mode is on, breakpoints are inserted immediately as soon as\n\
16412 they're created, kept inserted even when execution stops, and removed\n\
16413 only when the user deletes them. When this mode is off (the default),\n\
16414 breakpoints are inserted only when execution continues, and removed\n\
16415 when execution stops."),
16416 NULL,
16417 &show_always_inserted_mode,
16418 &breakpoint_set_cmdlist,
16419 &breakpoint_show_cmdlist);
16420
16421 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16422 condition_evaluation_enums,
16423 &condition_evaluation_mode_1, _("\
16424 Set mode of breakpoint condition evaluation."), _("\
16425 Show mode of breakpoint condition evaluation."), _("\
16426 When this is set to \"host\", breakpoint conditions will be\n\
16427 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16428 breakpoint conditions will be downloaded to the target (if the target\n\
16429 supports such feature) and conditions will be evaluated on the target's side.\n\
16430 If this is set to \"auto\" (default), this will be automatically set to\n\
16431 \"target\" if it supports condition evaluation, otherwise it will\n\
16432 be set to \"gdb\""),
16433 &set_condition_evaluation_mode,
16434 &show_condition_evaluation_mode,
16435 &breakpoint_set_cmdlist,
16436 &breakpoint_show_cmdlist);
16437
16438 add_com ("break-range", class_breakpoint, break_range_command, _("\
16439 Set a breakpoint for an address range.\n\
16440 break-range START-LOCATION, END-LOCATION\n\
16441 where START-LOCATION and END-LOCATION can be one of the following:\n\
16442 LINENUM, for that line in the current file,\n\
16443 FILE:LINENUM, for that line in that file,\n\
16444 +OFFSET, for that number of lines after the current line\n\
16445 or the start of the range\n\
16446 FUNCTION, for the first line in that function,\n\
16447 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16448 *ADDRESS, for the instruction at that address.\n\
16449 \n\
16450 The breakpoint will stop execution of the inferior whenever it executes\n\
16451 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16452 range (including START-LOCATION and END-LOCATION)."));
16453
16454 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16455 Set a dynamic printf at specified location.\n\
16456 dprintf location,format string,arg1,arg2,...\n\
16457 location may be a linespec, explicit, or address location.\n"
16458 "\n" LOCATION_HELP_STRING));
16459 set_cmd_completer (c, location_completer);
16460
16461 add_setshow_enum_cmd ("dprintf-style", class_support,
16462 dprintf_style_enums, &dprintf_style, _("\
16463 Set the style of usage for dynamic printf."), _("\
16464 Show the style of usage for dynamic printf."), _("\
16465 This setting chooses how GDB will do a dynamic printf.\n\
16466 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16467 console, as with the \"printf\" command.\n\
16468 If the value is \"call\", the print is done by calling a function in your\n\
16469 program; by default printf(), but you can choose a different function or\n\
16470 output stream by setting dprintf-function and dprintf-channel."),
16471 update_dprintf_commands, NULL,
16472 &setlist, &showlist);
16473
16474 dprintf_function = xstrdup ("printf");
16475 add_setshow_string_cmd ("dprintf-function", class_support,
16476 &dprintf_function, _("\
16477 Set the function to use for dynamic printf"), _("\
16478 Show the function to use for dynamic printf"), NULL,
16479 update_dprintf_commands, NULL,
16480 &setlist, &showlist);
16481
16482 dprintf_channel = xstrdup ("");
16483 add_setshow_string_cmd ("dprintf-channel", class_support,
16484 &dprintf_channel, _("\
16485 Set the channel to use for dynamic printf"), _("\
16486 Show the channel to use for dynamic printf"), NULL,
16487 update_dprintf_commands, NULL,
16488 &setlist, &showlist);
16489
16490 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16491 &disconnected_dprintf, _("\
16492 Set whether dprintf continues after GDB disconnects."), _("\
16493 Show whether dprintf continues after GDB disconnects."), _("\
16494 Use this to let dprintf commands continue to hit and produce output\n\
16495 even if GDB disconnects or detaches from the target."),
16496 NULL,
16497 NULL,
16498 &setlist, &showlist);
16499
16500 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16501 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16502 (target agent only) This is useful for formatted output in user-defined commands."));
16503
16504 automatic_hardware_breakpoints = 1;
16505
16506 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16507 observer_attach_thread_exit (remove_threaded_breakpoints);
16508 }
This page took 0.34882 seconds and 5 git commands to generate.