C++ify breakpoint class hierarchy (destructors only)
[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 char *, char *, enum bptype,
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
127 int, int, int, unsigned);
128
129 static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
131 struct program_space *search_pspace,
132 struct symtabs_and_lines *sals);
133
134 static void clear_command (char *, int);
135
136 static void catch_command (char *, int);
137
138 static int can_use_hardware_watchpoint (struct value *);
139
140 static void break_command_1 (char *, int, int);
141
142 static void mention (struct breakpoint *);
143
144 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
146 const struct breakpoint_ops *);
147 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
150 /* This function is used in gdbtk sources and thus can not be made
151 static. */
152 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
153 struct symtab_and_line,
154 enum bptype,
155 const struct breakpoint_ops *);
156
157 static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
162
163 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
165 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
167 enum bptype bptype);
168
169 static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
171 struct obj_section *, int);
172
173 static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
176 static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
180 static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
184 static void breakpoints_info (char *, int);
185
186 static void watchpoints_info (char *, int);
187
188 static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
190
191 static int breakpoint_cond_eval (void *);
192
193 static void cleanup_executing_breakpoints (void *);
194
195 static void commands_command (char *, int);
196
197 static void condition_command (char *, int);
198
199 static int remove_breakpoint (struct bp_location *);
200 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
201
202 static enum print_stop_action print_bp_stop_message (bpstat bs);
203
204 static int watchpoint_check (void *);
205
206 static void maintenance_info_breakpoints (char *, int);
207
208 static int hw_breakpoint_used_count (void);
209
210 static int hw_watchpoint_use_count (struct breakpoint *);
211
212 static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
215
216 static void hbreak_command (char *, int);
217
218 static void thbreak_command (char *, int);
219
220 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
222
223 static void stop_command (char *arg, int from_tty);
224
225 static void stopin_command (char *arg, int from_tty);
226
227 static void stopat_command (char *arg, int from_tty);
228
229 static void tcatch_command (char *arg, int from_tty);
230
231 static void free_bp_location (struct bp_location *loc);
232 static void incref_bp_location (struct bp_location *loc);
233 static void decref_bp_location (struct bp_location **loc);
234
235 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
236
237 /* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239 enum ugll_insert_mode
240 {
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
260 UGLL_MAY_INSERT,
261
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
269 UGLL_INSERT
270 };
271
272 static void update_global_location_list (enum ugll_insert_mode);
273
274 static void update_global_location_list_nothrow (enum ugll_insert_mode);
275
276 static int is_hardware_watchpoint (const struct breakpoint *bpt);
277
278 static void insert_breakpoint_locations (void);
279
280 static void tracepoints_info (char *, int);
281
282 static void delete_trace_command (char *, int);
283
284 static void enable_trace_command (char *, int);
285
286 static void disable_trace_command (char *, int);
287
288 static void trace_pass_command (char *, int);
289
290 static void set_tracepoint_count (int num);
291
292 static int is_masked_watchpoint (const struct breakpoint *b);
293
294 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
296 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299 static int strace_marker_p (struct breakpoint *b);
300
301 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304 static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306 /* Internal breakpoints class type. */
307 static struct breakpoint_ops internal_breakpoint_ops;
308
309 /* Momentary breakpoints class type. */
310 static struct breakpoint_ops momentary_breakpoint_ops;
311
312 /* The breakpoint_ops structure to be used in regular user created
313 breakpoints. */
314 struct breakpoint_ops bkpt_breakpoint_ops;
315
316 /* Breakpoints set on probes. */
317 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
318
319 /* Dynamic printf class type. */
320 struct breakpoint_ops dprintf_breakpoint_ops;
321
322 /* The style in which to perform a dynamic printf. This is a user
323 option because different output options have different tradeoffs;
324 if GDB does the printing, there is better error handling if there
325 is a problem with any of the arguments, but using an inferior
326 function lets you have special-purpose printers and sending of
327 output to the same place as compiled-in print functions. */
328
329 static const char dprintf_style_gdb[] = "gdb";
330 static const char dprintf_style_call[] = "call";
331 static const char dprintf_style_agent[] = "agent";
332 static const char *const dprintf_style_enums[] = {
333 dprintf_style_gdb,
334 dprintf_style_call,
335 dprintf_style_agent,
336 NULL
337 };
338 static const char *dprintf_style = dprintf_style_gdb;
339
340 /* The function to use for dynamic printf if the preferred style is to
341 call into the inferior. The value is simply a string that is
342 copied into the command, so it can be anything that GDB can
343 evaluate to a callable address, not necessarily a function name. */
344
345 static char *dprintf_function;
346
347 /* The channel to use for dynamic printf if the preferred style is to
348 call into the inferior; if a nonempty string, it will be passed to
349 the call as the first argument, with the format string as the
350 second. As with the dprintf function, this can be anything that
351 GDB knows how to evaluate, so in addition to common choices like
352 "stderr", this could be an app-specific expression like
353 "mystreams[curlogger]". */
354
355 static char *dprintf_channel;
356
357 /* True if dprintf commands should continue to operate even if GDB
358 has disconnected. */
359 static int disconnected_dprintf = 1;
360
361 /* A reference-counted struct command_line. This lets multiple
362 breakpoints share a single command list. */
363 struct counted_command_line
364 {
365 /* The reference count. */
366 int refc;
367
368 /* The command list. */
369 struct command_line *commands;
370 };
371
372 struct command_line *
373 breakpoint_commands (struct breakpoint *b)
374 {
375 return b->commands ? b->commands->commands : NULL;
376 }
377
378 /* Flag indicating that a command has proceeded the inferior past the
379 current breakpoint. */
380
381 static int breakpoint_proceeded;
382
383 const char *
384 bpdisp_text (enum bpdisp disp)
385 {
386 /* NOTE: the following values are a part of MI protocol and
387 represent values of 'disp' field returned when inferior stops at
388 a breakpoint. */
389 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
390
391 return bpdisps[(int) disp];
392 }
393
394 /* Prototypes for exported functions. */
395 /* If FALSE, gdb will not use hardware support for watchpoints, even
396 if such is available. */
397 static int can_use_hw_watchpoints;
398
399 static void
400 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
401 struct cmd_list_element *c,
402 const char *value)
403 {
404 fprintf_filtered (file,
405 _("Debugger's willingness to use "
406 "watchpoint hardware is %s.\n"),
407 value);
408 }
409
410 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
411 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
412 for unrecognized breakpoint locations.
413 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
414 static enum auto_boolean pending_break_support;
415 static void
416 show_pending_break_support (struct ui_file *file, int from_tty,
417 struct cmd_list_element *c,
418 const char *value)
419 {
420 fprintf_filtered (file,
421 _("Debugger's behavior regarding "
422 "pending breakpoints is %s.\n"),
423 value);
424 }
425
426 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
427 set with "break" but falling in read-only memory.
428 If 0, gdb will warn about such breakpoints, but won't automatically
429 use hardware breakpoints. */
430 static int automatic_hardware_breakpoints;
431 static void
432 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
433 struct cmd_list_element *c,
434 const char *value)
435 {
436 fprintf_filtered (file,
437 _("Automatic usage of hardware breakpoints is %s.\n"),
438 value);
439 }
440
441 /* If on, GDB keeps breakpoints inserted even if the inferior is
442 stopped, and immediately inserts any new breakpoints as soon as
443 they're created. If off (default), GDB keeps breakpoints off of
444 the target as long as possible. That is, it delays inserting
445 breakpoints until the next resume, and removes them again when the
446 target fully stops. This is a bit safer in case GDB crashes while
447 processing user input. */
448 static int always_inserted_mode = 0;
449
450 static void
451 show_always_inserted_mode (struct ui_file *file, int from_tty,
452 struct cmd_list_element *c, const char *value)
453 {
454 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
455 value);
456 }
457
458 /* See breakpoint.h. */
459
460 int
461 breakpoints_should_be_inserted_now (void)
462 {
463 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
464 {
465 /* If breakpoints are global, they should be inserted even if no
466 thread under gdb's control is running, or even if there are
467 no threads under GDB's control yet. */
468 return 1;
469 }
470 else if (target_has_execution)
471 {
472 struct thread_info *tp;
473
474 if (always_inserted_mode)
475 {
476 /* The user wants breakpoints inserted even if all threads
477 are stopped. */
478 return 1;
479 }
480
481 if (threads_are_executing ())
482 return 1;
483
484 /* Don't remove breakpoints yet if, even though all threads are
485 stopped, we still have events to process. */
486 ALL_NON_EXITED_THREADS (tp)
487 if (tp->resumed
488 && tp->suspend.waitstatus_pending_p)
489 return 1;
490 }
491 return 0;
492 }
493
494 static const char condition_evaluation_both[] = "host or target";
495
496 /* Modes for breakpoint condition evaluation. */
497 static const char condition_evaluation_auto[] = "auto";
498 static const char condition_evaluation_host[] = "host";
499 static const char condition_evaluation_target[] = "target";
500 static const char *const condition_evaluation_enums[] = {
501 condition_evaluation_auto,
502 condition_evaluation_host,
503 condition_evaluation_target,
504 NULL
505 };
506
507 /* Global that holds the current mode for breakpoint condition evaluation. */
508 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
509
510 /* Global that we use to display information to the user (gets its value from
511 condition_evaluation_mode_1. */
512 static const char *condition_evaluation_mode = condition_evaluation_auto;
513
514 /* Translate a condition evaluation mode MODE into either "host"
515 or "target". This is used mostly to translate from "auto" to the
516 real setting that is being used. It returns the translated
517 evaluation mode. */
518
519 static const char *
520 translate_condition_evaluation_mode (const char *mode)
521 {
522 if (mode == condition_evaluation_auto)
523 {
524 if (target_supports_evaluation_of_breakpoint_conditions ())
525 return condition_evaluation_target;
526 else
527 return condition_evaluation_host;
528 }
529 else
530 return mode;
531 }
532
533 /* Discovers what condition_evaluation_auto translates to. */
534
535 static const char *
536 breakpoint_condition_evaluation_mode (void)
537 {
538 return translate_condition_evaluation_mode (condition_evaluation_mode);
539 }
540
541 /* Return true if GDB should evaluate breakpoint conditions or false
542 otherwise. */
543
544 static int
545 gdb_evaluates_breakpoint_condition_p (void)
546 {
547 const char *mode = breakpoint_condition_evaluation_mode ();
548
549 return (mode == condition_evaluation_host);
550 }
551
552 void _initialize_breakpoint (void);
553
554 /* Are we executing breakpoint commands? */
555 static int executing_breakpoint_commands;
556
557 /* Are overlay event breakpoints enabled? */
558 static int overlay_events_enabled;
559
560 /* See description in breakpoint.h. */
561 int target_exact_watchpoints = 0;
562
563 /* Walk the following statement or block through all breakpoints.
564 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
565 current breakpoint. */
566
567 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
568
569 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
570 for (B = breakpoint_chain; \
571 B ? (TMP=B->next, 1): 0; \
572 B = TMP)
573
574 /* Similar iterator for the low-level breakpoints. SAFE variant is
575 not provided so update_global_location_list must not be called
576 while executing the block of ALL_BP_LOCATIONS. */
577
578 #define ALL_BP_LOCATIONS(B,BP_TMP) \
579 for (BP_TMP = bp_locations; \
580 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
581 BP_TMP++)
582
583 /* Iterates through locations with address ADDRESS for the currently selected
584 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
585 to where the loop should start from.
586 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
587 appropriate location to start with. */
588
589 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
590 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
591 BP_LOCP_TMP = BP_LOCP_START; \
592 BP_LOCP_START \
593 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
594 && (*BP_LOCP_TMP)->address == ADDRESS); \
595 BP_LOCP_TMP++)
596
597 /* Iterator for tracepoints only. */
598
599 #define ALL_TRACEPOINTS(B) \
600 for (B = breakpoint_chain; B; B = B->next) \
601 if (is_tracepoint (B))
602
603 /* Chains of all breakpoints defined. */
604
605 struct breakpoint *breakpoint_chain;
606
607 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
608
609 static struct bp_location **bp_locations;
610
611 /* Number of elements of BP_LOCATIONS. */
612
613 static unsigned bp_locations_count;
614
615 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
616 ADDRESS for the current elements of BP_LOCATIONS which get a valid
617 result from bp_location_has_shadow. You can use it for roughly
618 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
619 an address you need to read. */
620
621 static CORE_ADDR bp_locations_placed_address_before_address_max;
622
623 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
624 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
625 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
626 You can use it for roughly limiting the subrange of BP_LOCATIONS to
627 scan for shadow bytes for an address you need to read. */
628
629 static CORE_ADDR bp_locations_shadow_len_after_address_max;
630
631 /* The locations that no longer correspond to any breakpoint, unlinked
632 from the bp_locations array, but for which a hit may still be
633 reported by a target. */
634 VEC(bp_location_p) *moribund_locations = NULL;
635
636 /* Number of last breakpoint made. */
637
638 static int breakpoint_count;
639
640 /* The value of `breakpoint_count' before the last command that
641 created breakpoints. If the last (break-like) command created more
642 than one breakpoint, then the difference between BREAKPOINT_COUNT
643 and PREV_BREAKPOINT_COUNT is more than one. */
644 static int prev_breakpoint_count;
645
646 /* Number of last tracepoint made. */
647
648 static int tracepoint_count;
649
650 static struct cmd_list_element *breakpoint_set_cmdlist;
651 static struct cmd_list_element *breakpoint_show_cmdlist;
652 struct cmd_list_element *save_cmdlist;
653
654 /* See declaration at breakpoint.h. */
655
656 struct breakpoint *
657 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
658 void *user_data)
659 {
660 struct breakpoint *b = NULL;
661
662 ALL_BREAKPOINTS (b)
663 {
664 if (func (b, user_data) != 0)
665 break;
666 }
667
668 return b;
669 }
670
671 /* Return whether a breakpoint is an active enabled breakpoint. */
672 static int
673 breakpoint_enabled (struct breakpoint *b)
674 {
675 return (b->enable_state == bp_enabled);
676 }
677
678 /* Set breakpoint count to NUM. */
679
680 static void
681 set_breakpoint_count (int num)
682 {
683 prev_breakpoint_count = breakpoint_count;
684 breakpoint_count = num;
685 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
686 }
687
688 /* Used by `start_rbreak_breakpoints' below, to record the current
689 breakpoint count before "rbreak" creates any breakpoint. */
690 static int rbreak_start_breakpoint_count;
691
692 /* Called at the start an "rbreak" command to record the first
693 breakpoint made. */
694
695 void
696 start_rbreak_breakpoints (void)
697 {
698 rbreak_start_breakpoint_count = breakpoint_count;
699 }
700
701 /* Called at the end of an "rbreak" command to record the last
702 breakpoint made. */
703
704 void
705 end_rbreak_breakpoints (void)
706 {
707 prev_breakpoint_count = rbreak_start_breakpoint_count;
708 }
709
710 /* Used in run_command to zero the hit count when a new run starts. */
711
712 void
713 clear_breakpoint_hit_counts (void)
714 {
715 struct breakpoint *b;
716
717 ALL_BREAKPOINTS (b)
718 b->hit_count = 0;
719 }
720
721 /* Allocate a new counted_command_line with reference count of 1.
722 The new structure owns COMMANDS. */
723
724 static struct counted_command_line *
725 alloc_counted_command_line (struct command_line *commands)
726 {
727 struct counted_command_line *result = XNEW (struct counted_command_line);
728
729 result->refc = 1;
730 result->commands = commands;
731
732 return result;
733 }
734
735 /* Increment reference count. This does nothing if CMD is NULL. */
736
737 static void
738 incref_counted_command_line (struct counted_command_line *cmd)
739 {
740 if (cmd)
741 ++cmd->refc;
742 }
743
744 /* Decrement reference count. If the reference count reaches 0,
745 destroy the counted_command_line. Sets *CMDP to NULL. This does
746 nothing if *CMDP is NULL. */
747
748 static void
749 decref_counted_command_line (struct counted_command_line **cmdp)
750 {
751 if (*cmdp)
752 {
753 if (--(*cmdp)->refc == 0)
754 {
755 free_command_lines (&(*cmdp)->commands);
756 xfree (*cmdp);
757 }
758 *cmdp = NULL;
759 }
760 }
761
762 /* A cleanup function that calls decref_counted_command_line. */
763
764 static void
765 do_cleanup_counted_command_line (void *arg)
766 {
767 decref_counted_command_line ((struct counted_command_line **) arg);
768 }
769
770 /* Create a cleanup that calls decref_counted_command_line on the
771 argument. */
772
773 static struct cleanup *
774 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
775 {
776 return make_cleanup (do_cleanup_counted_command_line, cmdp);
777 }
778
779 \f
780 /* Return the breakpoint with the specified number, or NULL
781 if the number does not refer to an existing breakpoint. */
782
783 struct breakpoint *
784 get_breakpoint (int num)
785 {
786 struct breakpoint *b;
787
788 ALL_BREAKPOINTS (b)
789 if (b->number == num)
790 return b;
791
792 return NULL;
793 }
794
795 \f
796
797 /* Mark locations as "conditions have changed" in case the target supports
798 evaluating conditions on its side. */
799
800 static void
801 mark_breakpoint_modified (struct breakpoint *b)
802 {
803 struct bp_location *loc;
804
805 /* This is only meaningful if the target is
806 evaluating conditions and if the user has
807 opted for condition evaluation on the target's
808 side. */
809 if (gdb_evaluates_breakpoint_condition_p ()
810 || !target_supports_evaluation_of_breakpoint_conditions ())
811 return;
812
813 if (!is_breakpoint (b))
814 return;
815
816 for (loc = b->loc; loc; loc = loc->next)
817 loc->condition_changed = condition_modified;
818 }
819
820 /* Mark location as "conditions have changed" in case the target supports
821 evaluating conditions on its side. */
822
823 static void
824 mark_breakpoint_location_modified (struct bp_location *loc)
825 {
826 /* This is only meaningful if the target is
827 evaluating conditions and if the user has
828 opted for condition evaluation on the target's
829 side. */
830 if (gdb_evaluates_breakpoint_condition_p ()
831 || !target_supports_evaluation_of_breakpoint_conditions ())
832
833 return;
834
835 if (!is_breakpoint (loc->owner))
836 return;
837
838 loc->condition_changed = condition_modified;
839 }
840
841 /* Sets the condition-evaluation mode using the static global
842 condition_evaluation_mode. */
843
844 static void
845 set_condition_evaluation_mode (char *args, int from_tty,
846 struct cmd_list_element *c)
847 {
848 const char *old_mode, *new_mode;
849
850 if ((condition_evaluation_mode_1 == condition_evaluation_target)
851 && !target_supports_evaluation_of_breakpoint_conditions ())
852 {
853 condition_evaluation_mode_1 = condition_evaluation_mode;
854 warning (_("Target does not support breakpoint condition evaluation.\n"
855 "Using host evaluation mode instead."));
856 return;
857 }
858
859 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
860 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
861
862 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
863 settings was "auto". */
864 condition_evaluation_mode = condition_evaluation_mode_1;
865
866 /* Only update the mode if the user picked a different one. */
867 if (new_mode != old_mode)
868 {
869 struct bp_location *loc, **loc_tmp;
870 /* If the user switched to a different evaluation mode, we
871 need to synch the changes with the target as follows:
872
873 "host" -> "target": Send all (valid) conditions to the target.
874 "target" -> "host": Remove all the conditions from the target.
875 */
876
877 if (new_mode == condition_evaluation_target)
878 {
879 /* Mark everything modified and synch conditions with the
880 target. */
881 ALL_BP_LOCATIONS (loc, loc_tmp)
882 mark_breakpoint_location_modified (loc);
883 }
884 else
885 {
886 /* Manually mark non-duplicate locations to synch conditions
887 with the target. We do this to remove all the conditions the
888 target knows about. */
889 ALL_BP_LOCATIONS (loc, loc_tmp)
890 if (is_breakpoint (loc->owner) && loc->inserted)
891 loc->needs_update = 1;
892 }
893
894 /* Do the update. */
895 update_global_location_list (UGLL_MAY_INSERT);
896 }
897
898 return;
899 }
900
901 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
902 what "auto" is translating to. */
903
904 static void
905 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
906 struct cmd_list_element *c, const char *value)
907 {
908 if (condition_evaluation_mode == condition_evaluation_auto)
909 fprintf_filtered (file,
910 _("Breakpoint condition evaluation "
911 "mode is %s (currently %s).\n"),
912 value,
913 breakpoint_condition_evaluation_mode ());
914 else
915 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
916 value);
917 }
918
919 /* A comparison function for bp_location AP and BP that is used by
920 bsearch. This comparison function only cares about addresses, unlike
921 the more general bp_locations_compare function. */
922
923 static int
924 bp_locations_compare_addrs (const void *ap, const void *bp)
925 {
926 const struct bp_location *a = *(const struct bp_location **) ap;
927 const struct bp_location *b = *(const struct bp_location **) bp;
928
929 if (a->address == b->address)
930 return 0;
931 else
932 return ((a->address > b->address) - (a->address < b->address));
933 }
934
935 /* Helper function to skip all bp_locations with addresses
936 less than ADDRESS. It returns the first bp_location that
937 is greater than or equal to ADDRESS. If none is found, just
938 return NULL. */
939
940 static struct bp_location **
941 get_first_locp_gte_addr (CORE_ADDR address)
942 {
943 struct bp_location dummy_loc;
944 struct bp_location *dummy_locp = &dummy_loc;
945 struct bp_location **locp_found = NULL;
946
947 /* Initialize the dummy location's address field. */
948 dummy_loc.address = address;
949
950 /* Find a close match to the first location at ADDRESS. */
951 locp_found = ((struct bp_location **)
952 bsearch (&dummy_locp, bp_locations, bp_locations_count,
953 sizeof (struct bp_location **),
954 bp_locations_compare_addrs));
955
956 /* Nothing was found, nothing left to do. */
957 if (locp_found == NULL)
958 return NULL;
959
960 /* We may have found a location that is at ADDRESS but is not the first in the
961 location's list. Go backwards (if possible) and locate the first one. */
962 while ((locp_found - 1) >= bp_locations
963 && (*(locp_found - 1))->address == address)
964 locp_found--;
965
966 return locp_found;
967 }
968
969 void
970 set_breakpoint_condition (struct breakpoint *b, const char *exp,
971 int from_tty)
972 {
973 xfree (b->cond_string);
974 b->cond_string = NULL;
975
976 if (is_watchpoint (b))
977 {
978 struct watchpoint *w = (struct watchpoint *) b;
979
980 w->cond_exp.reset ();
981 }
982 else
983 {
984 struct bp_location *loc;
985
986 for (loc = b->loc; loc; loc = loc->next)
987 {
988 loc->cond.reset ();
989
990 /* No need to free the condition agent expression
991 bytecode (if we have one). We will handle this
992 when we go through update_global_location_list. */
993 }
994 }
995
996 if (*exp == 0)
997 {
998 if (from_tty)
999 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1000 }
1001 else
1002 {
1003 const char *arg = exp;
1004
1005 /* I don't know if it matters whether this is the string the user
1006 typed in or the decompiled expression. */
1007 b->cond_string = xstrdup (arg);
1008 b->condition_not_parsed = 0;
1009
1010 if (is_watchpoint (b))
1011 {
1012 struct watchpoint *w = (struct watchpoint *) b;
1013
1014 innermost_block = NULL;
1015 arg = exp;
1016 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1017 if (*arg)
1018 error (_("Junk at end of expression"));
1019 w->cond_exp_valid_block = innermost_block;
1020 }
1021 else
1022 {
1023 struct bp_location *loc;
1024
1025 for (loc = b->loc; loc; loc = loc->next)
1026 {
1027 arg = exp;
1028 loc->cond =
1029 parse_exp_1 (&arg, loc->address,
1030 block_for_pc (loc->address), 0);
1031 if (*arg)
1032 error (_("Junk at end of expression"));
1033 }
1034 }
1035 }
1036 mark_breakpoint_modified (b);
1037
1038 observer_notify_breakpoint_modified (b);
1039 }
1040
1041 /* Completion for the "condition" command. */
1042
1043 static VEC (char_ptr) *
1044 condition_completer (struct cmd_list_element *cmd,
1045 const char *text, const char *word)
1046 {
1047 const char *space;
1048
1049 text = skip_spaces_const (text);
1050 space = skip_to_space_const (text);
1051 if (*space == '\0')
1052 {
1053 int len;
1054 struct breakpoint *b;
1055 VEC (char_ptr) *result = NULL;
1056
1057 if (text[0] == '$')
1058 {
1059 /* We don't support completion of history indices. */
1060 if (isdigit (text[1]))
1061 return NULL;
1062 return complete_internalvar (&text[1]);
1063 }
1064
1065 /* We're completing the breakpoint number. */
1066 len = strlen (text);
1067
1068 ALL_BREAKPOINTS (b)
1069 {
1070 char number[50];
1071
1072 xsnprintf (number, sizeof (number), "%d", b->number);
1073
1074 if (strncmp (number, text, len) == 0)
1075 VEC_safe_push (char_ptr, result, xstrdup (number));
1076 }
1077
1078 return result;
1079 }
1080
1081 /* We're completing the expression part. */
1082 text = skip_spaces_const (space);
1083 return expression_completer (cmd, text, word);
1084 }
1085
1086 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1087
1088 static void
1089 condition_command (char *arg, int from_tty)
1090 {
1091 struct breakpoint *b;
1092 char *p;
1093 int bnum;
1094
1095 if (arg == 0)
1096 error_no_arg (_("breakpoint number"));
1097
1098 p = arg;
1099 bnum = get_number (&p);
1100 if (bnum == 0)
1101 error (_("Bad breakpoint argument: '%s'"), arg);
1102
1103 ALL_BREAKPOINTS (b)
1104 if (b->number == bnum)
1105 {
1106 /* Check if this breakpoint has a "stop" method implemented in an
1107 extension language. This method and conditions entered into GDB
1108 from the CLI are mutually exclusive. */
1109 const struct extension_language_defn *extlang
1110 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1111
1112 if (extlang != NULL)
1113 {
1114 error (_("Only one stop condition allowed. There is currently"
1115 " a %s stop condition defined for this breakpoint."),
1116 ext_lang_capitalized_name (extlang));
1117 }
1118 set_breakpoint_condition (b, p, from_tty);
1119
1120 if (is_breakpoint (b))
1121 update_global_location_list (UGLL_MAY_INSERT);
1122
1123 return;
1124 }
1125
1126 error (_("No breakpoint number %d."), bnum);
1127 }
1128
1129 /* Check that COMMAND do not contain commands that are suitable
1130 only for tracepoints and not suitable for ordinary breakpoints.
1131 Throw if any such commands is found. */
1132
1133 static void
1134 check_no_tracepoint_commands (struct command_line *commands)
1135 {
1136 struct command_line *c;
1137
1138 for (c = commands; c; c = c->next)
1139 {
1140 int i;
1141
1142 if (c->control_type == while_stepping_control)
1143 error (_("The 'while-stepping' command can "
1144 "only be used for tracepoints"));
1145
1146 for (i = 0; i < c->body_count; ++i)
1147 check_no_tracepoint_commands ((c->body_list)[i]);
1148
1149 /* Not that command parsing removes leading whitespace and comment
1150 lines and also empty lines. So, we only need to check for
1151 command directly. */
1152 if (strstr (c->line, "collect ") == c->line)
1153 error (_("The 'collect' command can only be used for tracepoints"));
1154
1155 if (strstr (c->line, "teval ") == c->line)
1156 error (_("The 'teval' command can only be used for tracepoints"));
1157 }
1158 }
1159
1160 struct longjmp_breakpoint : public breakpoint
1161 {
1162 ~longjmp_breakpoint () override;
1163 };
1164
1165 /* Encapsulate tests for different types of tracepoints. */
1166
1167 static bool
1168 is_tracepoint_type (bptype type)
1169 {
1170 return (type == bp_tracepoint
1171 || type == bp_fast_tracepoint
1172 || type == bp_static_tracepoint);
1173 }
1174
1175 static bool
1176 is_longjmp_type (bptype type)
1177 {
1178 return type == bp_longjmp || type == bp_exception;
1179 }
1180
1181 int
1182 is_tracepoint (const struct breakpoint *b)
1183 {
1184 return is_tracepoint_type (b->type);
1185 }
1186
1187 /* Factory function to create an appropriate instance of breakpoint given
1188 TYPE. */
1189
1190 static std::unique_ptr<breakpoint>
1191 new_breakpoint_from_type (bptype type)
1192 {
1193 breakpoint *b;
1194
1195 if (is_tracepoint_type (type))
1196 b = new tracepoint ();
1197 else if (is_longjmp_type (type))
1198 b = new longjmp_breakpoint ();
1199 else
1200 b = new breakpoint ();
1201
1202 return std::unique_ptr<breakpoint> (b);
1203 }
1204
1205 /* A helper function that validates that COMMANDS are valid for a
1206 breakpoint. This function will throw an exception if a problem is
1207 found. */
1208
1209 static void
1210 validate_commands_for_breakpoint (struct breakpoint *b,
1211 struct command_line *commands)
1212 {
1213 if (is_tracepoint (b))
1214 {
1215 struct tracepoint *t = (struct tracepoint *) b;
1216 struct command_line *c;
1217 struct command_line *while_stepping = 0;
1218
1219 /* Reset the while-stepping step count. The previous commands
1220 might have included a while-stepping action, while the new
1221 ones might not. */
1222 t->step_count = 0;
1223
1224 /* We need to verify that each top-level element of commands is
1225 valid for tracepoints, that there's at most one
1226 while-stepping element, and that the while-stepping's body
1227 has valid tracing commands excluding nested while-stepping.
1228 We also need to validate the tracepoint action line in the
1229 context of the tracepoint --- validate_actionline actually
1230 has side effects, like setting the tracepoint's
1231 while-stepping STEP_COUNT, in addition to checking if the
1232 collect/teval actions parse and make sense in the
1233 tracepoint's context. */
1234 for (c = commands; c; c = c->next)
1235 {
1236 if (c->control_type == while_stepping_control)
1237 {
1238 if (b->type == bp_fast_tracepoint)
1239 error (_("The 'while-stepping' command "
1240 "cannot be used for fast tracepoint"));
1241 else if (b->type == bp_static_tracepoint)
1242 error (_("The 'while-stepping' command "
1243 "cannot be used for static tracepoint"));
1244
1245 if (while_stepping)
1246 error (_("The 'while-stepping' command "
1247 "can be used only once"));
1248 else
1249 while_stepping = c;
1250 }
1251
1252 validate_actionline (c->line, b);
1253 }
1254 if (while_stepping)
1255 {
1256 struct command_line *c2;
1257
1258 gdb_assert (while_stepping->body_count == 1);
1259 c2 = while_stepping->body_list[0];
1260 for (; c2; c2 = c2->next)
1261 {
1262 if (c2->control_type == while_stepping_control)
1263 error (_("The 'while-stepping' command cannot be nested"));
1264 }
1265 }
1266 }
1267 else
1268 {
1269 check_no_tracepoint_commands (commands);
1270 }
1271 }
1272
1273 /* Return a vector of all the static tracepoints set at ADDR. The
1274 caller is responsible for releasing the vector. */
1275
1276 VEC(breakpoint_p) *
1277 static_tracepoints_here (CORE_ADDR addr)
1278 {
1279 struct breakpoint *b;
1280 VEC(breakpoint_p) *found = 0;
1281 struct bp_location *loc;
1282
1283 ALL_BREAKPOINTS (b)
1284 if (b->type == bp_static_tracepoint)
1285 {
1286 for (loc = b->loc; loc; loc = loc->next)
1287 if (loc->address == addr)
1288 VEC_safe_push(breakpoint_p, found, b);
1289 }
1290
1291 return found;
1292 }
1293
1294 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1295 validate that only allowed commands are included. */
1296
1297 void
1298 breakpoint_set_commands (struct breakpoint *b,
1299 command_line_up &&commands)
1300 {
1301 validate_commands_for_breakpoint (b, commands.get ());
1302
1303 decref_counted_command_line (&b->commands);
1304 b->commands = alloc_counted_command_line (commands.release ());
1305 observer_notify_breakpoint_modified (b);
1306 }
1307
1308 /* Set the internal `silent' flag on the breakpoint. Note that this
1309 is not the same as the "silent" that may appear in the breakpoint's
1310 commands. */
1311
1312 void
1313 breakpoint_set_silent (struct breakpoint *b, int silent)
1314 {
1315 int old_silent = b->silent;
1316
1317 b->silent = silent;
1318 if (old_silent != silent)
1319 observer_notify_breakpoint_modified (b);
1320 }
1321
1322 /* Set the thread for this breakpoint. If THREAD is -1, make the
1323 breakpoint work for any thread. */
1324
1325 void
1326 breakpoint_set_thread (struct breakpoint *b, int thread)
1327 {
1328 int old_thread = b->thread;
1329
1330 b->thread = thread;
1331 if (old_thread != thread)
1332 observer_notify_breakpoint_modified (b);
1333 }
1334
1335 /* Set the task for this breakpoint. If TASK is 0, make the
1336 breakpoint work for any task. */
1337
1338 void
1339 breakpoint_set_task (struct breakpoint *b, int task)
1340 {
1341 int old_task = b->task;
1342
1343 b->task = task;
1344 if (old_task != task)
1345 observer_notify_breakpoint_modified (b);
1346 }
1347
1348 void
1349 check_tracepoint_command (char *line, void *closure)
1350 {
1351 struct breakpoint *b = (struct breakpoint *) closure;
1352
1353 validate_actionline (line, b);
1354 }
1355
1356 /* A structure used to pass information through
1357 map_breakpoint_numbers. */
1358
1359 struct commands_info
1360 {
1361 /* True if the command was typed at a tty. */
1362 int from_tty;
1363
1364 /* The breakpoint range spec. */
1365 const char *arg;
1366
1367 /* Non-NULL if the body of the commands are being read from this
1368 already-parsed command. */
1369 struct command_line *control;
1370
1371 /* The command lines read from the user, or NULL if they have not
1372 yet been read. */
1373 struct counted_command_line *cmd;
1374 };
1375
1376 /* A callback for map_breakpoint_numbers that sets the commands for
1377 commands_command. */
1378
1379 static void
1380 do_map_commands_command (struct breakpoint *b, void *data)
1381 {
1382 struct commands_info *info = (struct commands_info *) data;
1383
1384 if (info->cmd == NULL)
1385 {
1386 command_line_up l;
1387
1388 if (info->control != NULL)
1389 l = copy_command_lines (info->control->body_list[0]);
1390 else
1391 {
1392 struct cleanup *old_chain;
1393 char *str;
1394
1395 str = xstrprintf (_("Type commands for breakpoint(s) "
1396 "%s, one per line."),
1397 info->arg);
1398
1399 old_chain = make_cleanup (xfree, str);
1400
1401 l = read_command_lines (str,
1402 info->from_tty, 1,
1403 (is_tracepoint (b)
1404 ? check_tracepoint_command : 0),
1405 b);
1406
1407 do_cleanups (old_chain);
1408 }
1409
1410 info->cmd = alloc_counted_command_line (l.release ());
1411 }
1412
1413 /* If a breakpoint was on the list more than once, we don't need to
1414 do anything. */
1415 if (b->commands != info->cmd)
1416 {
1417 validate_commands_for_breakpoint (b, info->cmd->commands);
1418 incref_counted_command_line (info->cmd);
1419 decref_counted_command_line (&b->commands);
1420 b->commands = info->cmd;
1421 observer_notify_breakpoint_modified (b);
1422 }
1423 }
1424
1425 static void
1426 commands_command_1 (const char *arg, int from_tty,
1427 struct command_line *control)
1428 {
1429 struct cleanup *cleanups;
1430 struct commands_info info;
1431
1432 info.from_tty = from_tty;
1433 info.control = control;
1434 info.cmd = NULL;
1435 /* If we read command lines from the user, then `info' will hold an
1436 extra reference to the commands that we must clean up. */
1437 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1438
1439 std::string new_arg;
1440
1441 if (arg == NULL || !*arg)
1442 {
1443 if (breakpoint_count - prev_breakpoint_count > 1)
1444 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1445 breakpoint_count);
1446 else if (breakpoint_count > 0)
1447 new_arg = string_printf ("%d", breakpoint_count);
1448 }
1449 else
1450 new_arg = arg;
1451
1452 info.arg = new_arg.c_str ();
1453
1454 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1455
1456 if (info.cmd == NULL)
1457 error (_("No breakpoints specified."));
1458
1459 do_cleanups (cleanups);
1460 }
1461
1462 static void
1463 commands_command (char *arg, int from_tty)
1464 {
1465 commands_command_1 (arg, from_tty, NULL);
1466 }
1467
1468 /* Like commands_command, but instead of reading the commands from
1469 input stream, takes them from an already parsed command structure.
1470
1471 This is used by cli-script.c to DTRT with breakpoint commands
1472 that are part of if and while bodies. */
1473 enum command_control_type
1474 commands_from_control_command (const char *arg, struct command_line *cmd)
1475 {
1476 commands_command_1 (arg, 0, cmd);
1477 return simple_control;
1478 }
1479
1480 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1481
1482 static int
1483 bp_location_has_shadow (struct bp_location *bl)
1484 {
1485 if (bl->loc_type != bp_loc_software_breakpoint)
1486 return 0;
1487 if (!bl->inserted)
1488 return 0;
1489 if (bl->target_info.shadow_len == 0)
1490 /* BL isn't valid, or doesn't shadow memory. */
1491 return 0;
1492 return 1;
1493 }
1494
1495 /* Update BUF, which is LEN bytes read from the target address
1496 MEMADDR, by replacing a memory breakpoint with its shadowed
1497 contents.
1498
1499 If READBUF is not NULL, this buffer must not overlap with the of
1500 the breakpoint location's shadow_contents buffer. Otherwise, a
1501 failed assertion internal error will be raised. */
1502
1503 static void
1504 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1505 const gdb_byte *writebuf_org,
1506 ULONGEST memaddr, LONGEST len,
1507 struct bp_target_info *target_info,
1508 struct gdbarch *gdbarch)
1509 {
1510 /* Now do full processing of the found relevant range of elements. */
1511 CORE_ADDR bp_addr = 0;
1512 int bp_size = 0;
1513 int bptoffset = 0;
1514
1515 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1516 current_program_space->aspace, 0))
1517 {
1518 /* The breakpoint is inserted in a different address space. */
1519 return;
1520 }
1521
1522 /* Addresses and length of the part of the breakpoint that
1523 we need to copy. */
1524 bp_addr = target_info->placed_address;
1525 bp_size = target_info->shadow_len;
1526
1527 if (bp_addr + bp_size <= memaddr)
1528 {
1529 /* The breakpoint is entirely before the chunk of memory we are
1530 reading. */
1531 return;
1532 }
1533
1534 if (bp_addr >= memaddr + len)
1535 {
1536 /* The breakpoint is entirely after the chunk of memory we are
1537 reading. */
1538 return;
1539 }
1540
1541 /* Offset within shadow_contents. */
1542 if (bp_addr < memaddr)
1543 {
1544 /* Only copy the second part of the breakpoint. */
1545 bp_size -= memaddr - bp_addr;
1546 bptoffset = memaddr - bp_addr;
1547 bp_addr = memaddr;
1548 }
1549
1550 if (bp_addr + bp_size > memaddr + len)
1551 {
1552 /* Only copy the first part of the breakpoint. */
1553 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1554 }
1555
1556 if (readbuf != NULL)
1557 {
1558 /* Verify that the readbuf buffer does not overlap with the
1559 shadow_contents buffer. */
1560 gdb_assert (target_info->shadow_contents >= readbuf + len
1561 || readbuf >= (target_info->shadow_contents
1562 + target_info->shadow_len));
1563
1564 /* Update the read buffer with this inserted breakpoint's
1565 shadow. */
1566 memcpy (readbuf + bp_addr - memaddr,
1567 target_info->shadow_contents + bptoffset, bp_size);
1568 }
1569 else
1570 {
1571 const unsigned char *bp;
1572 CORE_ADDR addr = target_info->reqstd_address;
1573 int placed_size;
1574
1575 /* Update the shadow with what we want to write to memory. */
1576 memcpy (target_info->shadow_contents + bptoffset,
1577 writebuf_org + bp_addr - memaddr, bp_size);
1578
1579 /* Determine appropriate breakpoint contents and size for this
1580 address. */
1581 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1582
1583 /* Update the final write buffer with this inserted
1584 breakpoint's INSN. */
1585 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1586 }
1587 }
1588
1589 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1590 by replacing any memory breakpoints with their shadowed contents.
1591
1592 If READBUF is not NULL, this buffer must not overlap with any of
1593 the breakpoint location's shadow_contents buffers. Otherwise,
1594 a failed assertion internal error will be raised.
1595
1596 The range of shadowed area by each bp_location is:
1597 bl->address - bp_locations_placed_address_before_address_max
1598 up to bl->address + bp_locations_shadow_len_after_address_max
1599 The range we were requested to resolve shadows for is:
1600 memaddr ... memaddr + len
1601 Thus the safe cutoff boundaries for performance optimization are
1602 memaddr + len <= (bl->address
1603 - bp_locations_placed_address_before_address_max)
1604 and:
1605 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1606
1607 void
1608 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1609 const gdb_byte *writebuf_org,
1610 ULONGEST memaddr, LONGEST len)
1611 {
1612 /* Left boundary, right boundary and median element of our binary
1613 search. */
1614 unsigned bc_l, bc_r, bc;
1615
1616 /* Find BC_L which is a leftmost element which may affect BUF
1617 content. It is safe to report lower value but a failure to
1618 report higher one. */
1619
1620 bc_l = 0;
1621 bc_r = bp_locations_count;
1622 while (bc_l + 1 < bc_r)
1623 {
1624 struct bp_location *bl;
1625
1626 bc = (bc_l + bc_r) / 2;
1627 bl = bp_locations[bc];
1628
1629 /* Check first BL->ADDRESS will not overflow due to the added
1630 constant. Then advance the left boundary only if we are sure
1631 the BC element can in no way affect the BUF content (MEMADDR
1632 to MEMADDR + LEN range).
1633
1634 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1635 offset so that we cannot miss a breakpoint with its shadow
1636 range tail still reaching MEMADDR. */
1637
1638 if ((bl->address + bp_locations_shadow_len_after_address_max
1639 >= bl->address)
1640 && (bl->address + bp_locations_shadow_len_after_address_max
1641 <= memaddr))
1642 bc_l = bc;
1643 else
1644 bc_r = bc;
1645 }
1646
1647 /* Due to the binary search above, we need to make sure we pick the
1648 first location that's at BC_L's address. E.g., if there are
1649 multiple locations at the same address, BC_L may end up pointing
1650 at a duplicate location, and miss the "master"/"inserted"
1651 location. Say, given locations L1, L2 and L3 at addresses A and
1652 B:
1653
1654 L1@A, L2@A, L3@B, ...
1655
1656 BC_L could end up pointing at location L2, while the "master"
1657 location could be L1. Since the `loc->inserted' flag is only set
1658 on "master" locations, we'd forget to restore the shadow of L1
1659 and L2. */
1660 while (bc_l > 0
1661 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1662 bc_l--;
1663
1664 /* Now do full processing of the found relevant range of elements. */
1665
1666 for (bc = bc_l; bc < bp_locations_count; bc++)
1667 {
1668 struct bp_location *bl = bp_locations[bc];
1669
1670 /* bp_location array has BL->OWNER always non-NULL. */
1671 if (bl->owner->type == bp_none)
1672 warning (_("reading through apparently deleted breakpoint #%d?"),
1673 bl->owner->number);
1674
1675 /* Performance optimization: any further element can no longer affect BUF
1676 content. */
1677
1678 if (bl->address >= bp_locations_placed_address_before_address_max
1679 && memaddr + len <= (bl->address
1680 - bp_locations_placed_address_before_address_max))
1681 break;
1682
1683 if (!bp_location_has_shadow (bl))
1684 continue;
1685
1686 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1687 memaddr, len, &bl->target_info, bl->gdbarch);
1688 }
1689 }
1690
1691 \f
1692
1693 /* Return true if BPT is either a software breakpoint or a hardware
1694 breakpoint. */
1695
1696 int
1697 is_breakpoint (const struct breakpoint *bpt)
1698 {
1699 return (bpt->type == bp_breakpoint
1700 || bpt->type == bp_hardware_breakpoint
1701 || bpt->type == bp_dprintf);
1702 }
1703
1704 /* Return true if BPT is of any hardware watchpoint kind. */
1705
1706 static int
1707 is_hardware_watchpoint (const struct breakpoint *bpt)
1708 {
1709 return (bpt->type == bp_hardware_watchpoint
1710 || bpt->type == bp_read_watchpoint
1711 || bpt->type == bp_access_watchpoint);
1712 }
1713
1714 /* Return true if BPT is of any watchpoint kind, hardware or
1715 software. */
1716
1717 int
1718 is_watchpoint (const struct breakpoint *bpt)
1719 {
1720 return (is_hardware_watchpoint (bpt)
1721 || bpt->type == bp_watchpoint);
1722 }
1723
1724 /* Returns true if the current thread and its running state are safe
1725 to evaluate or update watchpoint B. Watchpoints on local
1726 expressions need to be evaluated in the context of the thread that
1727 was current when the watchpoint was created, and, that thread needs
1728 to be stopped to be able to select the correct frame context.
1729 Watchpoints on global expressions can be evaluated on any thread,
1730 and in any state. It is presently left to the target allowing
1731 memory accesses when threads are running. */
1732
1733 static int
1734 watchpoint_in_thread_scope (struct watchpoint *b)
1735 {
1736 return (b->pspace == current_program_space
1737 && (ptid_equal (b->watchpoint_thread, null_ptid)
1738 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1739 && !is_executing (inferior_ptid))));
1740 }
1741
1742 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1743 associated bp_watchpoint_scope breakpoint. */
1744
1745 static void
1746 watchpoint_del_at_next_stop (struct watchpoint *w)
1747 {
1748 if (w->related_breakpoint != w)
1749 {
1750 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1751 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1752 w->related_breakpoint->disposition = disp_del_at_next_stop;
1753 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1754 w->related_breakpoint = w;
1755 }
1756 w->disposition = disp_del_at_next_stop;
1757 }
1758
1759 /* Extract a bitfield value from value VAL using the bit parameters contained in
1760 watchpoint W. */
1761
1762 static struct value *
1763 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1764 {
1765 struct value *bit_val;
1766
1767 if (val == NULL)
1768 return NULL;
1769
1770 bit_val = allocate_value (value_type (val));
1771
1772 unpack_value_bitfield (bit_val,
1773 w->val_bitpos,
1774 w->val_bitsize,
1775 value_contents_for_printing (val),
1776 value_offset (val),
1777 val);
1778
1779 return bit_val;
1780 }
1781
1782 /* Allocate a dummy location and add it to B, which must be a software
1783 watchpoint. This is required because even if a software watchpoint
1784 is not watching any memory, bpstat_stop_status requires a location
1785 to be able to report stops. */
1786
1787 static void
1788 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1789 struct program_space *pspace)
1790 {
1791 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1792
1793 b->loc = allocate_bp_location (b);
1794 b->loc->pspace = pspace;
1795 b->loc->address = -1;
1796 b->loc->length = -1;
1797 }
1798
1799 /* Returns true if B is a software watchpoint that is not watching any
1800 memory (e.g., "watch $pc"). */
1801
1802 static int
1803 is_no_memory_software_watchpoint (struct breakpoint *b)
1804 {
1805 return (b->type == bp_watchpoint
1806 && b->loc != NULL
1807 && b->loc->next == NULL
1808 && b->loc->address == -1
1809 && b->loc->length == -1);
1810 }
1811
1812 /* Assuming that B is a watchpoint:
1813 - Reparse watchpoint expression, if REPARSE is non-zero
1814 - Evaluate expression and store the result in B->val
1815 - Evaluate the condition if there is one, and store the result
1816 in b->loc->cond.
1817 - Update the list of values that must be watched in B->loc.
1818
1819 If the watchpoint disposition is disp_del_at_next_stop, then do
1820 nothing. If this is local watchpoint that is out of scope, delete
1821 it.
1822
1823 Even with `set breakpoint always-inserted on' the watchpoints are
1824 removed + inserted on each stop here. Normal breakpoints must
1825 never be removed because they might be missed by a running thread
1826 when debugging in non-stop mode. On the other hand, hardware
1827 watchpoints (is_hardware_watchpoint; processed here) are specific
1828 to each LWP since they are stored in each LWP's hardware debug
1829 registers. Therefore, such LWP must be stopped first in order to
1830 be able to modify its hardware watchpoints.
1831
1832 Hardware watchpoints must be reset exactly once after being
1833 presented to the user. It cannot be done sooner, because it would
1834 reset the data used to present the watchpoint hit to the user. And
1835 it must not be done later because it could display the same single
1836 watchpoint hit during multiple GDB stops. Note that the latter is
1837 relevant only to the hardware watchpoint types bp_read_watchpoint
1838 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1839 not user-visible - its hit is suppressed if the memory content has
1840 not changed.
1841
1842 The following constraints influence the location where we can reset
1843 hardware watchpoints:
1844
1845 * target_stopped_by_watchpoint and target_stopped_data_address are
1846 called several times when GDB stops.
1847
1848 [linux]
1849 * Multiple hardware watchpoints can be hit at the same time,
1850 causing GDB to stop. GDB only presents one hardware watchpoint
1851 hit at a time as the reason for stopping, and all the other hits
1852 are presented later, one after the other, each time the user
1853 requests the execution to be resumed. Execution is not resumed
1854 for the threads still having pending hit event stored in
1855 LWP_INFO->STATUS. While the watchpoint is already removed from
1856 the inferior on the first stop the thread hit event is kept being
1857 reported from its cached value by linux_nat_stopped_data_address
1858 until the real thread resume happens after the watchpoint gets
1859 presented and thus its LWP_INFO->STATUS gets reset.
1860
1861 Therefore the hardware watchpoint hit can get safely reset on the
1862 watchpoint removal from inferior. */
1863
1864 static void
1865 update_watchpoint (struct watchpoint *b, int reparse)
1866 {
1867 int within_current_scope;
1868 struct frame_id saved_frame_id;
1869 int frame_saved;
1870
1871 /* If this is a local watchpoint, we only want to check if the
1872 watchpoint frame is in scope if the current thread is the thread
1873 that was used to create the watchpoint. */
1874 if (!watchpoint_in_thread_scope (b))
1875 return;
1876
1877 if (b->disposition == disp_del_at_next_stop)
1878 return;
1879
1880 frame_saved = 0;
1881
1882 /* Determine if the watchpoint is within scope. */
1883 if (b->exp_valid_block == NULL)
1884 within_current_scope = 1;
1885 else
1886 {
1887 struct frame_info *fi = get_current_frame ();
1888 struct gdbarch *frame_arch = get_frame_arch (fi);
1889 CORE_ADDR frame_pc = get_frame_pc (fi);
1890
1891 /* If we're at a point where the stack has been destroyed
1892 (e.g. in a function epilogue), unwinding may not work
1893 properly. Do not attempt to recreate locations at this
1894 point. See similar comments in watchpoint_check. */
1895 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1896 return;
1897
1898 /* Save the current frame's ID so we can restore it after
1899 evaluating the watchpoint expression on its own frame. */
1900 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1901 took a frame parameter, so that we didn't have to change the
1902 selected frame. */
1903 frame_saved = 1;
1904 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1905
1906 fi = frame_find_by_id (b->watchpoint_frame);
1907 within_current_scope = (fi != NULL);
1908 if (within_current_scope)
1909 select_frame (fi);
1910 }
1911
1912 /* We don't free locations. They are stored in the bp_location array
1913 and update_global_location_list will eventually delete them and
1914 remove breakpoints if needed. */
1915 b->loc = NULL;
1916
1917 if (within_current_scope && reparse)
1918 {
1919 const char *s;
1920
1921 b->exp.reset ();
1922 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1923 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1924 /* If the meaning of expression itself changed, the old value is
1925 no longer relevant. We don't want to report a watchpoint hit
1926 to the user when the old value and the new value may actually
1927 be completely different objects. */
1928 value_free (b->val);
1929 b->val = NULL;
1930 b->val_valid = 0;
1931
1932 /* Note that unlike with breakpoints, the watchpoint's condition
1933 expression is stored in the breakpoint object, not in the
1934 locations (re)created below. */
1935 if (b->cond_string != NULL)
1936 {
1937 b->cond_exp.reset ();
1938
1939 s = b->cond_string;
1940 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1941 }
1942 }
1943
1944 /* If we failed to parse the expression, for example because
1945 it refers to a global variable in a not-yet-loaded shared library,
1946 don't try to insert watchpoint. We don't automatically delete
1947 such watchpoint, though, since failure to parse expression
1948 is different from out-of-scope watchpoint. */
1949 if (!target_has_execution)
1950 {
1951 /* Without execution, memory can't change. No use to try and
1952 set watchpoint locations. The watchpoint will be reset when
1953 the target gains execution, through breakpoint_re_set. */
1954 if (!can_use_hw_watchpoints)
1955 {
1956 if (b->ops->works_in_software_mode (b))
1957 b->type = bp_watchpoint;
1958 else
1959 error (_("Can't set read/access watchpoint when "
1960 "hardware watchpoints are disabled."));
1961 }
1962 }
1963 else if (within_current_scope && b->exp)
1964 {
1965 int pc = 0;
1966 struct value *val_chain, *v, *result, *next;
1967 struct program_space *frame_pspace;
1968
1969 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1970
1971 /* Avoid setting b->val if it's already set. The meaning of
1972 b->val is 'the last value' user saw, and we should update
1973 it only if we reported that last value to user. As it
1974 happens, the code that reports it updates b->val directly.
1975 We don't keep track of the memory value for masked
1976 watchpoints. */
1977 if (!b->val_valid && !is_masked_watchpoint (b))
1978 {
1979 if (b->val_bitsize != 0)
1980 {
1981 v = extract_bitfield_from_watchpoint_value (b, v);
1982 if (v != NULL)
1983 release_value (v);
1984 }
1985 b->val = v;
1986 b->val_valid = 1;
1987 }
1988
1989 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1990
1991 /* Look at each value on the value chain. */
1992 for (v = val_chain; v; v = value_next (v))
1993 {
1994 /* If it's a memory location, and GDB actually needed
1995 its contents to evaluate the expression, then we
1996 must watch it. If the first value returned is
1997 still lazy, that means an error occurred reading it;
1998 watch it anyway in case it becomes readable. */
1999 if (VALUE_LVAL (v) == lval_memory
2000 && (v == val_chain || ! value_lazy (v)))
2001 {
2002 struct type *vtype = check_typedef (value_type (v));
2003
2004 /* We only watch structs and arrays if user asked
2005 for it explicitly, never if they just happen to
2006 appear in the middle of some value chain. */
2007 if (v == result
2008 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2009 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2010 {
2011 CORE_ADDR addr;
2012 enum target_hw_bp_type type;
2013 struct bp_location *loc, **tmp;
2014 int bitpos = 0, bitsize = 0;
2015
2016 if (value_bitsize (v) != 0)
2017 {
2018 /* Extract the bit parameters out from the bitfield
2019 sub-expression. */
2020 bitpos = value_bitpos (v);
2021 bitsize = value_bitsize (v);
2022 }
2023 else if (v == result && b->val_bitsize != 0)
2024 {
2025 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2026 lvalue whose bit parameters are saved in the fields
2027 VAL_BITPOS and VAL_BITSIZE. */
2028 bitpos = b->val_bitpos;
2029 bitsize = b->val_bitsize;
2030 }
2031
2032 addr = value_address (v);
2033 if (bitsize != 0)
2034 {
2035 /* Skip the bytes that don't contain the bitfield. */
2036 addr += bitpos / 8;
2037 }
2038
2039 type = hw_write;
2040 if (b->type == bp_read_watchpoint)
2041 type = hw_read;
2042 else if (b->type == bp_access_watchpoint)
2043 type = hw_access;
2044
2045 loc = allocate_bp_location (b);
2046 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2047 ;
2048 *tmp = loc;
2049 loc->gdbarch = get_type_arch (value_type (v));
2050
2051 loc->pspace = frame_pspace;
2052 loc->address = addr;
2053
2054 if (bitsize != 0)
2055 {
2056 /* Just cover the bytes that make up the bitfield. */
2057 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2058 }
2059 else
2060 loc->length = TYPE_LENGTH (value_type (v));
2061
2062 loc->watchpoint_type = type;
2063 }
2064 }
2065 }
2066
2067 /* Change the type of breakpoint between hardware assisted or
2068 an ordinary watchpoint depending on the hardware support
2069 and free hardware slots. REPARSE is set when the inferior
2070 is started. */
2071 if (reparse)
2072 {
2073 int reg_cnt;
2074 enum bp_loc_type loc_type;
2075 struct bp_location *bl;
2076
2077 reg_cnt = can_use_hardware_watchpoint (val_chain);
2078
2079 if (reg_cnt)
2080 {
2081 int i, target_resources_ok, other_type_used;
2082 enum bptype type;
2083
2084 /* Use an exact watchpoint when there's only one memory region to be
2085 watched, and only one debug register is needed to watch it. */
2086 b->exact = target_exact_watchpoints && reg_cnt == 1;
2087
2088 /* We need to determine how many resources are already
2089 used for all other hardware watchpoints plus this one
2090 to see if we still have enough resources to also fit
2091 this watchpoint in as well. */
2092
2093 /* If this is a software watchpoint, we try to turn it
2094 to a hardware one -- count resources as if B was of
2095 hardware watchpoint type. */
2096 type = b->type;
2097 if (type == bp_watchpoint)
2098 type = bp_hardware_watchpoint;
2099
2100 /* This watchpoint may or may not have been placed on
2101 the list yet at this point (it won't be in the list
2102 if we're trying to create it for the first time,
2103 through watch_command), so always account for it
2104 manually. */
2105
2106 /* Count resources used by all watchpoints except B. */
2107 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2108
2109 /* Add in the resources needed for B. */
2110 i += hw_watchpoint_use_count (b);
2111
2112 target_resources_ok
2113 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2114 if (target_resources_ok <= 0)
2115 {
2116 int sw_mode = b->ops->works_in_software_mode (b);
2117
2118 if (target_resources_ok == 0 && !sw_mode)
2119 error (_("Target does not support this type of "
2120 "hardware watchpoint."));
2121 else if (target_resources_ok < 0 && !sw_mode)
2122 error (_("There are not enough available hardware "
2123 "resources for this watchpoint."));
2124
2125 /* Downgrade to software watchpoint. */
2126 b->type = bp_watchpoint;
2127 }
2128 else
2129 {
2130 /* If this was a software watchpoint, we've just
2131 found we have enough resources to turn it to a
2132 hardware watchpoint. Otherwise, this is a
2133 nop. */
2134 b->type = type;
2135 }
2136 }
2137 else if (!b->ops->works_in_software_mode (b))
2138 {
2139 if (!can_use_hw_watchpoints)
2140 error (_("Can't set read/access watchpoint when "
2141 "hardware watchpoints are disabled."));
2142 else
2143 error (_("Expression cannot be implemented with "
2144 "read/access watchpoint."));
2145 }
2146 else
2147 b->type = bp_watchpoint;
2148
2149 loc_type = (b->type == bp_watchpoint? bp_loc_other
2150 : bp_loc_hardware_watchpoint);
2151 for (bl = b->loc; bl; bl = bl->next)
2152 bl->loc_type = loc_type;
2153 }
2154
2155 for (v = val_chain; v; v = next)
2156 {
2157 next = value_next (v);
2158 if (v != b->val)
2159 value_free (v);
2160 }
2161
2162 /* If a software watchpoint is not watching any memory, then the
2163 above left it without any location set up. But,
2164 bpstat_stop_status requires a location to be able to report
2165 stops, so make sure there's at least a dummy one. */
2166 if (b->type == bp_watchpoint && b->loc == NULL)
2167 software_watchpoint_add_no_memory_location (b, frame_pspace);
2168 }
2169 else if (!within_current_scope)
2170 {
2171 printf_filtered (_("\
2172 Watchpoint %d deleted because the program has left the block\n\
2173 in which its expression is valid.\n"),
2174 b->number);
2175 watchpoint_del_at_next_stop (b);
2176 }
2177
2178 /* Restore the selected frame. */
2179 if (frame_saved)
2180 select_frame (frame_find_by_id (saved_frame_id));
2181 }
2182
2183
2184 /* Returns 1 iff breakpoint location should be
2185 inserted in the inferior. We don't differentiate the type of BL's owner
2186 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2187 breakpoint_ops is not defined, because in insert_bp_location,
2188 tracepoint's insert_location will not be called. */
2189 static int
2190 should_be_inserted (struct bp_location *bl)
2191 {
2192 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2193 return 0;
2194
2195 if (bl->owner->disposition == disp_del_at_next_stop)
2196 return 0;
2197
2198 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2199 return 0;
2200
2201 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2202 return 0;
2203
2204 /* This is set for example, when we're attached to the parent of a
2205 vfork, and have detached from the child. The child is running
2206 free, and we expect it to do an exec or exit, at which point the
2207 OS makes the parent schedulable again (and the target reports
2208 that the vfork is done). Until the child is done with the shared
2209 memory region, do not insert breakpoints in the parent, otherwise
2210 the child could still trip on the parent's breakpoints. Since
2211 the parent is blocked anyway, it won't miss any breakpoint. */
2212 if (bl->pspace->breakpoints_not_allowed)
2213 return 0;
2214
2215 /* Don't insert a breakpoint if we're trying to step past its
2216 location, except if the breakpoint is a single-step breakpoint,
2217 and the breakpoint's thread is the thread which is stepping past
2218 a breakpoint. */
2219 if ((bl->loc_type == bp_loc_software_breakpoint
2220 || bl->loc_type == bp_loc_hardware_breakpoint)
2221 && stepping_past_instruction_at (bl->pspace->aspace,
2222 bl->address)
2223 /* The single-step breakpoint may be inserted at the location
2224 we're trying to step if the instruction branches to itself.
2225 However, the instruction won't be executed at all and it may
2226 break the semantics of the instruction, for example, the
2227 instruction is a conditional branch or updates some flags.
2228 We can't fix it unless GDB is able to emulate the instruction
2229 or switch to displaced stepping. */
2230 && !(bl->owner->type == bp_single_step
2231 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2232 {
2233 if (debug_infrun)
2234 {
2235 fprintf_unfiltered (gdb_stdlog,
2236 "infrun: skipping breakpoint: "
2237 "stepping past insn at: %s\n",
2238 paddress (bl->gdbarch, bl->address));
2239 }
2240 return 0;
2241 }
2242
2243 /* Don't insert watchpoints if we're trying to step past the
2244 instruction that triggered one. */
2245 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2246 && stepping_past_nonsteppable_watchpoint ())
2247 {
2248 if (debug_infrun)
2249 {
2250 fprintf_unfiltered (gdb_stdlog,
2251 "infrun: stepping past non-steppable watchpoint. "
2252 "skipping watchpoint at %s:%d\n",
2253 paddress (bl->gdbarch, bl->address),
2254 bl->length);
2255 }
2256 return 0;
2257 }
2258
2259 return 1;
2260 }
2261
2262 /* Same as should_be_inserted but does the check assuming
2263 that the location is not duplicated. */
2264
2265 static int
2266 unduplicated_should_be_inserted (struct bp_location *bl)
2267 {
2268 int result;
2269 const int save_duplicate = bl->duplicate;
2270
2271 bl->duplicate = 0;
2272 result = should_be_inserted (bl);
2273 bl->duplicate = save_duplicate;
2274 return result;
2275 }
2276
2277 /* Parses a conditional described by an expression COND into an
2278 agent expression bytecode suitable for evaluation
2279 by the bytecode interpreter. Return NULL if there was
2280 any error during parsing. */
2281
2282 static agent_expr_up
2283 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2284 {
2285 if (cond == NULL)
2286 return NULL;
2287
2288 agent_expr_up aexpr;
2289
2290 /* We don't want to stop processing, so catch any errors
2291 that may show up. */
2292 TRY
2293 {
2294 aexpr = gen_eval_for_expr (scope, cond);
2295 }
2296
2297 CATCH (ex, RETURN_MASK_ERROR)
2298 {
2299 /* If we got here, it means the condition could not be parsed to a valid
2300 bytecode expression and thus can't be evaluated on the target's side.
2301 It's no use iterating through the conditions. */
2302 }
2303 END_CATCH
2304
2305 /* We have a valid agent expression. */
2306 return aexpr;
2307 }
2308
2309 /* Based on location BL, create a list of breakpoint conditions to be
2310 passed on to the target. If we have duplicated locations with different
2311 conditions, we will add such conditions to the list. The idea is that the
2312 target will evaluate the list of conditions and will only notify GDB when
2313 one of them is true. */
2314
2315 static void
2316 build_target_condition_list (struct bp_location *bl)
2317 {
2318 struct bp_location **locp = NULL, **loc2p;
2319 int null_condition_or_parse_error = 0;
2320 int modified = bl->needs_update;
2321 struct bp_location *loc;
2322
2323 /* Release conditions left over from a previous insert. */
2324 bl->target_info.conditions.clear ();
2325
2326 /* This is only meaningful if the target is
2327 evaluating conditions and if the user has
2328 opted for condition evaluation on the target's
2329 side. */
2330 if (gdb_evaluates_breakpoint_condition_p ()
2331 || !target_supports_evaluation_of_breakpoint_conditions ())
2332 return;
2333
2334 /* Do a first pass to check for locations with no assigned
2335 conditions or conditions that fail to parse to a valid agent expression
2336 bytecode. If any of these happen, then it's no use to send conditions
2337 to the target since this location will always trigger and generate a
2338 response back to GDB. */
2339 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2340 {
2341 loc = (*loc2p);
2342 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2343 {
2344 if (modified)
2345 {
2346 /* Re-parse the conditions since something changed. In that
2347 case we already freed the condition bytecodes (see
2348 force_breakpoint_reinsertion). We just
2349 need to parse the condition to bytecodes again. */
2350 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2351 loc->cond.get ());
2352 }
2353
2354 /* If we have a NULL bytecode expression, it means something
2355 went wrong or we have a null condition expression. */
2356 if (!loc->cond_bytecode)
2357 {
2358 null_condition_or_parse_error = 1;
2359 break;
2360 }
2361 }
2362 }
2363
2364 /* If any of these happened, it means we will have to evaluate the conditions
2365 for the location's address on gdb's side. It is no use keeping bytecodes
2366 for all the other duplicate locations, thus we free all of them here.
2367
2368 This is so we have a finer control over which locations' conditions are
2369 being evaluated by GDB or the remote stub. */
2370 if (null_condition_or_parse_error)
2371 {
2372 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2373 {
2374 loc = (*loc2p);
2375 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2376 {
2377 /* Only go as far as the first NULL bytecode is
2378 located. */
2379 if (!loc->cond_bytecode)
2380 return;
2381
2382 loc->cond_bytecode.reset ();
2383 }
2384 }
2385 }
2386
2387 /* No NULL conditions or failed bytecode generation. Build a condition list
2388 for this location's address. */
2389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390 {
2391 loc = (*loc2p);
2392 if (loc->cond
2393 && is_breakpoint (loc->owner)
2394 && loc->pspace->num == bl->pspace->num
2395 && loc->owner->enable_state == bp_enabled
2396 && loc->enabled)
2397 {
2398 /* Add the condition to the vector. This will be used later
2399 to send the conditions to the target. */
2400 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2401 }
2402 }
2403
2404 return;
2405 }
2406
2407 /* Parses a command described by string CMD into an agent expression
2408 bytecode suitable for evaluation by the bytecode interpreter.
2409 Return NULL if there was any error during parsing. */
2410
2411 static agent_expr_up
2412 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2413 {
2414 struct cleanup *old_cleanups = 0;
2415 struct expression **argvec;
2416 const char *cmdrest;
2417 const char *format_start, *format_end;
2418 struct format_piece *fpieces;
2419 int nargs;
2420 struct gdbarch *gdbarch = get_current_arch ();
2421
2422 if (cmd == NULL)
2423 return NULL;
2424
2425 cmdrest = cmd;
2426
2427 if (*cmdrest == ',')
2428 ++cmdrest;
2429 cmdrest = skip_spaces_const (cmdrest);
2430
2431 if (*cmdrest++ != '"')
2432 error (_("No format string following the location"));
2433
2434 format_start = cmdrest;
2435
2436 fpieces = parse_format_string (&cmdrest);
2437
2438 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2439
2440 format_end = cmdrest;
2441
2442 if (*cmdrest++ != '"')
2443 error (_("Bad format string, non-terminated '\"'."));
2444
2445 cmdrest = skip_spaces_const (cmdrest);
2446
2447 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2448 error (_("Invalid argument syntax"));
2449
2450 if (*cmdrest == ',')
2451 cmdrest++;
2452 cmdrest = skip_spaces_const (cmdrest);
2453
2454 /* For each argument, make an expression. */
2455
2456 argvec = (struct expression **) alloca (strlen (cmd)
2457 * sizeof (struct expression *));
2458
2459 nargs = 0;
2460 while (*cmdrest != '\0')
2461 {
2462 const char *cmd1;
2463
2464 cmd1 = cmdrest;
2465 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2466 argvec[nargs++] = expr.release ();
2467 cmdrest = cmd1;
2468 if (*cmdrest == ',')
2469 ++cmdrest;
2470 }
2471
2472 agent_expr_up aexpr;
2473
2474 /* We don't want to stop processing, so catch any errors
2475 that may show up. */
2476 TRY
2477 {
2478 aexpr = gen_printf (scope, gdbarch, 0, 0,
2479 format_start, format_end - format_start,
2480 fpieces, nargs, argvec);
2481 }
2482 CATCH (ex, RETURN_MASK_ERROR)
2483 {
2484 /* If we got here, it means the command could not be parsed to a valid
2485 bytecode expression and thus can't be evaluated on the target's side.
2486 It's no use iterating through the other commands. */
2487 }
2488 END_CATCH
2489
2490 do_cleanups (old_cleanups);
2491
2492 /* We have a valid agent expression, return it. */
2493 return aexpr;
2494 }
2495
2496 /* Based on location BL, create a list of breakpoint commands to be
2497 passed on to the target. If we have duplicated locations with
2498 different commands, we will add any such to the list. */
2499
2500 static void
2501 build_target_command_list (struct bp_location *bl)
2502 {
2503 struct bp_location **locp = NULL, **loc2p;
2504 int null_command_or_parse_error = 0;
2505 int modified = bl->needs_update;
2506 struct bp_location *loc;
2507
2508 /* Clear commands left over from a previous insert. */
2509 bl->target_info.tcommands.clear ();
2510
2511 if (!target_can_run_breakpoint_commands ())
2512 return;
2513
2514 /* For now, limit to agent-style dprintf breakpoints. */
2515 if (dprintf_style != dprintf_style_agent)
2516 return;
2517
2518 /* For now, if we have any duplicate location that isn't a dprintf,
2519 don't install the target-side commands, as that would make the
2520 breakpoint not be reported to the core, and we'd lose
2521 control. */
2522 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2523 {
2524 loc = (*loc2p);
2525 if (is_breakpoint (loc->owner)
2526 && loc->pspace->num == bl->pspace->num
2527 && loc->owner->type != bp_dprintf)
2528 return;
2529 }
2530
2531 /* Do a first pass to check for locations with no assigned
2532 conditions or conditions that fail to parse to a valid agent expression
2533 bytecode. If any of these happen, then it's no use to send conditions
2534 to the target since this location will always trigger and generate a
2535 response back to GDB. */
2536 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2537 {
2538 loc = (*loc2p);
2539 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2540 {
2541 if (modified)
2542 {
2543 /* Re-parse the commands since something changed. In that
2544 case we already freed the command bytecodes (see
2545 force_breakpoint_reinsertion). We just
2546 need to parse the command to bytecodes again. */
2547 loc->cmd_bytecode
2548 = parse_cmd_to_aexpr (bl->address,
2549 loc->owner->extra_string);
2550 }
2551
2552 /* If we have a NULL bytecode expression, it means something
2553 went wrong or we have a null command expression. */
2554 if (!loc->cmd_bytecode)
2555 {
2556 null_command_or_parse_error = 1;
2557 break;
2558 }
2559 }
2560 }
2561
2562 /* If anything failed, then we're not doing target-side commands,
2563 and so clean up. */
2564 if (null_command_or_parse_error)
2565 {
2566 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2567 {
2568 loc = (*loc2p);
2569 if (is_breakpoint (loc->owner)
2570 && loc->pspace->num == bl->pspace->num)
2571 {
2572 /* Only go as far as the first NULL bytecode is
2573 located. */
2574 if (loc->cmd_bytecode == NULL)
2575 return;
2576
2577 loc->cmd_bytecode.reset ();
2578 }
2579 }
2580 }
2581
2582 /* No NULL commands or failed bytecode generation. Build a command list
2583 for this location's address. */
2584 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2585 {
2586 loc = (*loc2p);
2587 if (loc->owner->extra_string
2588 && is_breakpoint (loc->owner)
2589 && loc->pspace->num == bl->pspace->num
2590 && loc->owner->enable_state == bp_enabled
2591 && loc->enabled)
2592 {
2593 /* Add the command to the vector. This will be used later
2594 to send the commands to the target. */
2595 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2596 }
2597 }
2598
2599 bl->target_info.persist = 0;
2600 /* Maybe flag this location as persistent. */
2601 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2602 bl->target_info.persist = 1;
2603 }
2604
2605 /* Return the kind of breakpoint on address *ADDR. Get the kind
2606 of breakpoint according to ADDR except single-step breakpoint.
2607 Get the kind of single-step breakpoint according to the current
2608 registers state. */
2609
2610 static int
2611 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2612 {
2613 if (bl->owner->type == bp_single_step)
2614 {
2615 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2616 struct regcache *regcache;
2617
2618 regcache = get_thread_regcache (thr->ptid);
2619
2620 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2621 regcache, addr);
2622 }
2623 else
2624 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2625 }
2626
2627 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2628 location. Any error messages are printed to TMP_ERROR_STREAM; and
2629 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2630 Returns 0 for success, 1 if the bp_location type is not supported or
2631 -1 for failure.
2632
2633 NOTE drow/2003-09-09: This routine could be broken down to an
2634 object-style method for each breakpoint or catchpoint type. */
2635 static int
2636 insert_bp_location (struct bp_location *bl,
2637 struct ui_file *tmp_error_stream,
2638 int *disabled_breaks,
2639 int *hw_breakpoint_error,
2640 int *hw_bp_error_explained_already)
2641 {
2642 enum errors bp_err = GDB_NO_ERROR;
2643 const char *bp_err_message = NULL;
2644
2645 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2646 return 0;
2647
2648 /* Note we don't initialize bl->target_info, as that wipes out
2649 the breakpoint location's shadow_contents if the breakpoint
2650 is still inserted at that location. This in turn breaks
2651 target_read_memory which depends on these buffers when
2652 a memory read is requested at the breakpoint location:
2653 Once the target_info has been wiped, we fail to see that
2654 we have a breakpoint inserted at that address and thus
2655 read the breakpoint instead of returning the data saved in
2656 the breakpoint location's shadow contents. */
2657 bl->target_info.reqstd_address = bl->address;
2658 bl->target_info.placed_address_space = bl->pspace->aspace;
2659 bl->target_info.length = bl->length;
2660
2661 /* When working with target-side conditions, we must pass all the conditions
2662 for the same breakpoint address down to the target since GDB will not
2663 insert those locations. With a list of breakpoint conditions, the target
2664 can decide when to stop and notify GDB. */
2665
2666 if (is_breakpoint (bl->owner))
2667 {
2668 build_target_condition_list (bl);
2669 build_target_command_list (bl);
2670 /* Reset the modification marker. */
2671 bl->needs_update = 0;
2672 }
2673
2674 if (bl->loc_type == bp_loc_software_breakpoint
2675 || bl->loc_type == bp_loc_hardware_breakpoint)
2676 {
2677 if (bl->owner->type != bp_hardware_breakpoint)
2678 {
2679 /* If the explicitly specified breakpoint type
2680 is not hardware breakpoint, check the memory map to see
2681 if the breakpoint address is in read only memory or not.
2682
2683 Two important cases are:
2684 - location type is not hardware breakpoint, memory
2685 is readonly. We change the type of the location to
2686 hardware breakpoint.
2687 - location type is hardware breakpoint, memory is
2688 read-write. This means we've previously made the
2689 location hardware one, but then the memory map changed,
2690 so we undo.
2691
2692 When breakpoints are removed, remove_breakpoints will use
2693 location types we've just set here, the only possible
2694 problem is that memory map has changed during running
2695 program, but it's not going to work anyway with current
2696 gdb. */
2697 struct mem_region *mr
2698 = lookup_mem_region (bl->target_info.reqstd_address);
2699
2700 if (mr)
2701 {
2702 if (automatic_hardware_breakpoints)
2703 {
2704 enum bp_loc_type new_type;
2705
2706 if (mr->attrib.mode != MEM_RW)
2707 new_type = bp_loc_hardware_breakpoint;
2708 else
2709 new_type = bp_loc_software_breakpoint;
2710
2711 if (new_type != bl->loc_type)
2712 {
2713 static int said = 0;
2714
2715 bl->loc_type = new_type;
2716 if (!said)
2717 {
2718 fprintf_filtered (gdb_stdout,
2719 _("Note: automatically using "
2720 "hardware breakpoints for "
2721 "read-only addresses.\n"));
2722 said = 1;
2723 }
2724 }
2725 }
2726 else if (bl->loc_type == bp_loc_software_breakpoint
2727 && mr->attrib.mode != MEM_RW)
2728 {
2729 fprintf_unfiltered (tmp_error_stream,
2730 _("Cannot insert breakpoint %d.\n"
2731 "Cannot set software breakpoint "
2732 "at read-only address %s\n"),
2733 bl->owner->number,
2734 paddress (bl->gdbarch, bl->address));
2735 return 1;
2736 }
2737 }
2738 }
2739
2740 /* First check to see if we have to handle an overlay. */
2741 if (overlay_debugging == ovly_off
2742 || bl->section == NULL
2743 || !(section_is_overlay (bl->section)))
2744 {
2745 /* No overlay handling: just set the breakpoint. */
2746 TRY
2747 {
2748 int val;
2749
2750 val = bl->owner->ops->insert_location (bl);
2751 if (val)
2752 bp_err = GENERIC_ERROR;
2753 }
2754 CATCH (e, RETURN_MASK_ALL)
2755 {
2756 bp_err = e.error;
2757 bp_err_message = e.message;
2758 }
2759 END_CATCH
2760 }
2761 else
2762 {
2763 /* This breakpoint is in an overlay section.
2764 Shall we set a breakpoint at the LMA? */
2765 if (!overlay_events_enabled)
2766 {
2767 /* Yes -- overlay event support is not active,
2768 so we must try to set a breakpoint at the LMA.
2769 This will not work for a hardware breakpoint. */
2770 if (bl->loc_type == bp_loc_hardware_breakpoint)
2771 warning (_("hardware breakpoint %d not supported in overlay!"),
2772 bl->owner->number);
2773 else
2774 {
2775 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2776 bl->section);
2777 /* Set a software (trap) breakpoint at the LMA. */
2778 bl->overlay_target_info = bl->target_info;
2779 bl->overlay_target_info.reqstd_address = addr;
2780
2781 /* No overlay handling: just set the breakpoint. */
2782 TRY
2783 {
2784 int val;
2785
2786 bl->overlay_target_info.kind
2787 = breakpoint_kind (bl, &addr);
2788 bl->overlay_target_info.placed_address = addr;
2789 val = target_insert_breakpoint (bl->gdbarch,
2790 &bl->overlay_target_info);
2791 if (val)
2792 bp_err = GENERIC_ERROR;
2793 }
2794 CATCH (e, RETURN_MASK_ALL)
2795 {
2796 bp_err = e.error;
2797 bp_err_message = e.message;
2798 }
2799 END_CATCH
2800
2801 if (bp_err != GDB_NO_ERROR)
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Overlay breakpoint %d "
2804 "failed: in ROM?\n",
2805 bl->owner->number);
2806 }
2807 }
2808 /* Shall we set a breakpoint at the VMA? */
2809 if (section_is_mapped (bl->section))
2810 {
2811 /* Yes. This overlay section is mapped into memory. */
2812 TRY
2813 {
2814 int val;
2815
2816 val = bl->owner->ops->insert_location (bl);
2817 if (val)
2818 bp_err = GENERIC_ERROR;
2819 }
2820 CATCH (e, RETURN_MASK_ALL)
2821 {
2822 bp_err = e.error;
2823 bp_err_message = e.message;
2824 }
2825 END_CATCH
2826 }
2827 else
2828 {
2829 /* No. This breakpoint will not be inserted.
2830 No error, but do not mark the bp as 'inserted'. */
2831 return 0;
2832 }
2833 }
2834
2835 if (bp_err != GDB_NO_ERROR)
2836 {
2837 /* Can't set the breakpoint. */
2838
2839 /* In some cases, we might not be able to insert a
2840 breakpoint in a shared library that has already been
2841 removed, but we have not yet processed the shlib unload
2842 event. Unfortunately, some targets that implement
2843 breakpoint insertion themselves can't tell why the
2844 breakpoint insertion failed (e.g., the remote target
2845 doesn't define error codes), so we must treat generic
2846 errors as memory errors. */
2847 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2848 && bl->loc_type == bp_loc_software_breakpoint
2849 && (solib_name_from_address (bl->pspace, bl->address)
2850 || shared_objfile_contains_address_p (bl->pspace,
2851 bl->address)))
2852 {
2853 /* See also: disable_breakpoints_in_shlibs. */
2854 bl->shlib_disabled = 1;
2855 observer_notify_breakpoint_modified (bl->owner);
2856 if (!*disabled_breaks)
2857 {
2858 fprintf_unfiltered (tmp_error_stream,
2859 "Cannot insert breakpoint %d.\n",
2860 bl->owner->number);
2861 fprintf_unfiltered (tmp_error_stream,
2862 "Temporarily disabling shared "
2863 "library breakpoints:\n");
2864 }
2865 *disabled_breaks = 1;
2866 fprintf_unfiltered (tmp_error_stream,
2867 "breakpoint #%d\n", bl->owner->number);
2868 return 0;
2869 }
2870 else
2871 {
2872 if (bl->loc_type == bp_loc_hardware_breakpoint)
2873 {
2874 *hw_breakpoint_error = 1;
2875 *hw_bp_error_explained_already = bp_err_message != NULL;
2876 fprintf_unfiltered (tmp_error_stream,
2877 "Cannot insert hardware breakpoint %d%s",
2878 bl->owner->number, bp_err_message ? ":" : ".\n");
2879 if (bp_err_message != NULL)
2880 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2881 }
2882 else
2883 {
2884 if (bp_err_message == NULL)
2885 {
2886 char *message
2887 = memory_error_message (TARGET_XFER_E_IO,
2888 bl->gdbarch, bl->address);
2889 struct cleanup *old_chain = make_cleanup (xfree, message);
2890
2891 fprintf_unfiltered (tmp_error_stream,
2892 "Cannot insert breakpoint %d.\n"
2893 "%s\n",
2894 bl->owner->number, message);
2895 do_cleanups (old_chain);
2896 }
2897 else
2898 {
2899 fprintf_unfiltered (tmp_error_stream,
2900 "Cannot insert breakpoint %d: %s\n",
2901 bl->owner->number,
2902 bp_err_message);
2903 }
2904 }
2905 return 1;
2906
2907 }
2908 }
2909 else
2910 bl->inserted = 1;
2911
2912 return 0;
2913 }
2914
2915 else if (bl->loc_type == bp_loc_hardware_watchpoint
2916 /* NOTE drow/2003-09-08: This state only exists for removing
2917 watchpoints. It's not clear that it's necessary... */
2918 && bl->owner->disposition != disp_del_at_next_stop)
2919 {
2920 int val;
2921
2922 gdb_assert (bl->owner->ops != NULL
2923 && bl->owner->ops->insert_location != NULL);
2924
2925 val = bl->owner->ops->insert_location (bl);
2926
2927 /* If trying to set a read-watchpoint, and it turns out it's not
2928 supported, try emulating one with an access watchpoint. */
2929 if (val == 1 && bl->watchpoint_type == hw_read)
2930 {
2931 struct bp_location *loc, **loc_temp;
2932
2933 /* But don't try to insert it, if there's already another
2934 hw_access location that would be considered a duplicate
2935 of this one. */
2936 ALL_BP_LOCATIONS (loc, loc_temp)
2937 if (loc != bl
2938 && loc->watchpoint_type == hw_access
2939 && watchpoint_locations_match (bl, loc))
2940 {
2941 bl->duplicate = 1;
2942 bl->inserted = 1;
2943 bl->target_info = loc->target_info;
2944 bl->watchpoint_type = hw_access;
2945 val = 0;
2946 break;
2947 }
2948
2949 if (val == 1)
2950 {
2951 bl->watchpoint_type = hw_access;
2952 val = bl->owner->ops->insert_location (bl);
2953
2954 if (val)
2955 /* Back to the original value. */
2956 bl->watchpoint_type = hw_read;
2957 }
2958 }
2959
2960 bl->inserted = (val == 0);
2961 }
2962
2963 else if (bl->owner->type == bp_catchpoint)
2964 {
2965 int val;
2966
2967 gdb_assert (bl->owner->ops != NULL
2968 && bl->owner->ops->insert_location != NULL);
2969
2970 val = bl->owner->ops->insert_location (bl);
2971 if (val)
2972 {
2973 bl->owner->enable_state = bp_disabled;
2974
2975 if (val == 1)
2976 warning (_("\
2977 Error inserting catchpoint %d: Your system does not support this type\n\
2978 of catchpoint."), bl->owner->number);
2979 else
2980 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2981 }
2982
2983 bl->inserted = (val == 0);
2984
2985 /* We've already printed an error message if there was a problem
2986 inserting this catchpoint, and we've disabled the catchpoint,
2987 so just return success. */
2988 return 0;
2989 }
2990
2991 return 0;
2992 }
2993
2994 /* This function is called when program space PSPACE is about to be
2995 deleted. It takes care of updating breakpoints to not reference
2996 PSPACE anymore. */
2997
2998 void
2999 breakpoint_program_space_exit (struct program_space *pspace)
3000 {
3001 struct breakpoint *b, *b_temp;
3002 struct bp_location *loc, **loc_temp;
3003
3004 /* Remove any breakpoint that was set through this program space. */
3005 ALL_BREAKPOINTS_SAFE (b, b_temp)
3006 {
3007 if (b->pspace == pspace)
3008 delete_breakpoint (b);
3009 }
3010
3011 /* Breakpoints set through other program spaces could have locations
3012 bound to PSPACE as well. Remove those. */
3013 ALL_BP_LOCATIONS (loc, loc_temp)
3014 {
3015 struct bp_location *tmp;
3016
3017 if (loc->pspace == pspace)
3018 {
3019 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3020 if (loc->owner->loc == loc)
3021 loc->owner->loc = loc->next;
3022 else
3023 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3024 if (tmp->next == loc)
3025 {
3026 tmp->next = loc->next;
3027 break;
3028 }
3029 }
3030 }
3031
3032 /* Now update the global location list to permanently delete the
3033 removed locations above. */
3034 update_global_location_list (UGLL_DONT_INSERT);
3035 }
3036
3037 /* Make sure all breakpoints are inserted in inferior.
3038 Throws exception on any error.
3039 A breakpoint that is already inserted won't be inserted
3040 again, so calling this function twice is safe. */
3041 void
3042 insert_breakpoints (void)
3043 {
3044 struct breakpoint *bpt;
3045
3046 ALL_BREAKPOINTS (bpt)
3047 if (is_hardware_watchpoint (bpt))
3048 {
3049 struct watchpoint *w = (struct watchpoint *) bpt;
3050
3051 update_watchpoint (w, 0 /* don't reparse. */);
3052 }
3053
3054 /* Updating watchpoints creates new locations, so update the global
3055 location list. Explicitly tell ugll to insert locations and
3056 ignore breakpoints_always_inserted_mode. */
3057 update_global_location_list (UGLL_INSERT);
3058 }
3059
3060 /* Invoke CALLBACK for each of bp_location. */
3061
3062 void
3063 iterate_over_bp_locations (walk_bp_location_callback callback)
3064 {
3065 struct bp_location *loc, **loc_tmp;
3066
3067 ALL_BP_LOCATIONS (loc, loc_tmp)
3068 {
3069 callback (loc, NULL);
3070 }
3071 }
3072
3073 /* This is used when we need to synch breakpoint conditions between GDB and the
3074 target. It is the case with deleting and disabling of breakpoints when using
3075 always-inserted mode. */
3076
3077 static void
3078 update_inserted_breakpoint_locations (void)
3079 {
3080 struct bp_location *bl, **blp_tmp;
3081 int error_flag = 0;
3082 int val = 0;
3083 int disabled_breaks = 0;
3084 int hw_breakpoint_error = 0;
3085 int hw_bp_details_reported = 0;
3086
3087 string_file tmp_error_stream;
3088
3089 /* Explicitly mark the warning -- this will only be printed if
3090 there was an error. */
3091 tmp_error_stream.puts ("Warning:\n");
3092
3093 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3094
3095 ALL_BP_LOCATIONS (bl, blp_tmp)
3096 {
3097 /* We only want to update software breakpoints and hardware
3098 breakpoints. */
3099 if (!is_breakpoint (bl->owner))
3100 continue;
3101
3102 /* We only want to update locations that are already inserted
3103 and need updating. This is to avoid unwanted insertion during
3104 deletion of breakpoints. */
3105 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3106 continue;
3107
3108 switch_to_program_space_and_thread (bl->pspace);
3109
3110 /* For targets that support global breakpoints, there's no need
3111 to select an inferior to insert breakpoint to. In fact, even
3112 if we aren't attached to any process yet, we should still
3113 insert breakpoints. */
3114 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3115 && ptid_equal (inferior_ptid, null_ptid))
3116 continue;
3117
3118 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3119 &hw_breakpoint_error, &hw_bp_details_reported);
3120 if (val)
3121 error_flag = val;
3122 }
3123
3124 if (error_flag)
3125 {
3126 target_terminal_ours_for_output ();
3127 error_stream (tmp_error_stream);
3128 }
3129 }
3130
3131 /* Used when starting or continuing the program. */
3132
3133 static void
3134 insert_breakpoint_locations (void)
3135 {
3136 struct breakpoint *bpt;
3137 struct bp_location *bl, **blp_tmp;
3138 int error_flag = 0;
3139 int val = 0;
3140 int disabled_breaks = 0;
3141 int hw_breakpoint_error = 0;
3142 int hw_bp_error_explained_already = 0;
3143
3144 string_file tmp_error_stream;
3145
3146 /* Explicitly mark the warning -- this will only be printed if
3147 there was an error. */
3148 tmp_error_stream.puts ("Warning:\n");
3149
3150 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3151
3152 ALL_BP_LOCATIONS (bl, blp_tmp)
3153 {
3154 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3155 continue;
3156
3157 /* There is no point inserting thread-specific breakpoints if
3158 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3159 has BL->OWNER always non-NULL. */
3160 if (bl->owner->thread != -1
3161 && !valid_global_thread_id (bl->owner->thread))
3162 continue;
3163
3164 switch_to_program_space_and_thread (bl->pspace);
3165
3166 /* For targets that support global breakpoints, there's no need
3167 to select an inferior to insert breakpoint to. In fact, even
3168 if we aren't attached to any process yet, we should still
3169 insert breakpoints. */
3170 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3171 && ptid_equal (inferior_ptid, null_ptid))
3172 continue;
3173
3174 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3175 &hw_breakpoint_error, &hw_bp_error_explained_already);
3176 if (val)
3177 error_flag = val;
3178 }
3179
3180 /* If we failed to insert all locations of a watchpoint, remove
3181 them, as half-inserted watchpoint is of limited use. */
3182 ALL_BREAKPOINTS (bpt)
3183 {
3184 int some_failed = 0;
3185 struct bp_location *loc;
3186
3187 if (!is_hardware_watchpoint (bpt))
3188 continue;
3189
3190 if (!breakpoint_enabled (bpt))
3191 continue;
3192
3193 if (bpt->disposition == disp_del_at_next_stop)
3194 continue;
3195
3196 for (loc = bpt->loc; loc; loc = loc->next)
3197 if (!loc->inserted && should_be_inserted (loc))
3198 {
3199 some_failed = 1;
3200 break;
3201 }
3202 if (some_failed)
3203 {
3204 for (loc = bpt->loc; loc; loc = loc->next)
3205 if (loc->inserted)
3206 remove_breakpoint (loc);
3207
3208 hw_breakpoint_error = 1;
3209 tmp_error_stream.printf ("Could not insert "
3210 "hardware watchpoint %d.\n",
3211 bpt->number);
3212 error_flag = -1;
3213 }
3214 }
3215
3216 if (error_flag)
3217 {
3218 /* If a hardware breakpoint or watchpoint was inserted, add a
3219 message about possibly exhausted resources. */
3220 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3221 {
3222 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3223 You may have requested too many hardware breakpoints/watchpoints.\n");
3224 }
3225 target_terminal_ours_for_output ();
3226 error_stream (tmp_error_stream);
3227 }
3228 }
3229
3230 /* Used when the program stops.
3231 Returns zero if successful, or non-zero if there was a problem
3232 removing a breakpoint location. */
3233
3234 int
3235 remove_breakpoints (void)
3236 {
3237 struct bp_location *bl, **blp_tmp;
3238 int val = 0;
3239
3240 ALL_BP_LOCATIONS (bl, blp_tmp)
3241 {
3242 if (bl->inserted && !is_tracepoint (bl->owner))
3243 val |= remove_breakpoint (bl);
3244 }
3245 return val;
3246 }
3247
3248 /* When a thread exits, remove breakpoints that are related to
3249 that thread. */
3250
3251 static void
3252 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3253 {
3254 struct breakpoint *b, *b_tmp;
3255
3256 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3257 {
3258 if (b->thread == tp->global_num && user_breakpoint_p (b))
3259 {
3260 b->disposition = disp_del_at_next_stop;
3261
3262 printf_filtered (_("\
3263 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3264 b->number, print_thread_id (tp));
3265
3266 /* Hide it from the user. */
3267 b->number = 0;
3268 }
3269 }
3270 }
3271
3272 /* Remove breakpoints of process PID. */
3273
3274 int
3275 remove_breakpoints_pid (int pid)
3276 {
3277 struct bp_location *bl, **blp_tmp;
3278 int val;
3279 struct inferior *inf = find_inferior_pid (pid);
3280
3281 ALL_BP_LOCATIONS (bl, blp_tmp)
3282 {
3283 if (bl->pspace != inf->pspace)
3284 continue;
3285
3286 if (bl->inserted && !bl->target_info.persist)
3287 {
3288 val = remove_breakpoint (bl);
3289 if (val != 0)
3290 return val;
3291 }
3292 }
3293 return 0;
3294 }
3295
3296 int
3297 reattach_breakpoints (int pid)
3298 {
3299 struct cleanup *old_chain;
3300 struct bp_location *bl, **blp_tmp;
3301 int val;
3302 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3303 struct inferior *inf;
3304 struct thread_info *tp;
3305
3306 tp = any_live_thread_of_process (pid);
3307 if (tp == NULL)
3308 return 1;
3309
3310 inf = find_inferior_pid (pid);
3311 old_chain = save_inferior_ptid ();
3312
3313 inferior_ptid = tp->ptid;
3314
3315 string_file tmp_error_stream;
3316
3317 ALL_BP_LOCATIONS (bl, blp_tmp)
3318 {
3319 if (bl->pspace != inf->pspace)
3320 continue;
3321
3322 if (bl->inserted)
3323 {
3324 bl->inserted = 0;
3325 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3326 if (val != 0)
3327 {
3328 do_cleanups (old_chain);
3329 return val;
3330 }
3331 }
3332 }
3333 do_cleanups (old_chain);
3334 return 0;
3335 }
3336
3337 static int internal_breakpoint_number = -1;
3338
3339 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3340 If INTERNAL is non-zero, the breakpoint number will be populated
3341 from internal_breakpoint_number and that variable decremented.
3342 Otherwise the breakpoint number will be populated from
3343 breakpoint_count and that value incremented. Internal breakpoints
3344 do not set the internal var bpnum. */
3345 static void
3346 set_breakpoint_number (int internal, struct breakpoint *b)
3347 {
3348 if (internal)
3349 b->number = internal_breakpoint_number--;
3350 else
3351 {
3352 set_breakpoint_count (breakpoint_count + 1);
3353 b->number = breakpoint_count;
3354 }
3355 }
3356
3357 static struct breakpoint *
3358 create_internal_breakpoint (struct gdbarch *gdbarch,
3359 CORE_ADDR address, enum bptype type,
3360 const struct breakpoint_ops *ops)
3361 {
3362 struct symtab_and_line sal;
3363 struct breakpoint *b;
3364
3365 init_sal (&sal); /* Initialize to zeroes. */
3366
3367 sal.pc = address;
3368 sal.section = find_pc_overlay (sal.pc);
3369 sal.pspace = current_program_space;
3370
3371 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3372 b->number = internal_breakpoint_number--;
3373 b->disposition = disp_donttouch;
3374
3375 return b;
3376 }
3377
3378 static const char *const longjmp_names[] =
3379 {
3380 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3381 };
3382 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3383
3384 /* Per-objfile data private to breakpoint.c. */
3385 struct breakpoint_objfile_data
3386 {
3387 /* Minimal symbol for "_ovly_debug_event" (if any). */
3388 struct bound_minimal_symbol overlay_msym;
3389
3390 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3391 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3392
3393 /* True if we have looked for longjmp probes. */
3394 int longjmp_searched;
3395
3396 /* SystemTap probe points for longjmp (if any). */
3397 VEC (probe_p) *longjmp_probes;
3398
3399 /* Minimal symbol for "std::terminate()" (if any). */
3400 struct bound_minimal_symbol terminate_msym;
3401
3402 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3403 struct bound_minimal_symbol exception_msym;
3404
3405 /* True if we have looked for exception probes. */
3406 int exception_searched;
3407
3408 /* SystemTap probe points for unwinding (if any). */
3409 VEC (probe_p) *exception_probes;
3410 };
3411
3412 static const struct objfile_data *breakpoint_objfile_key;
3413
3414 /* Minimal symbol not found sentinel. */
3415 static struct minimal_symbol msym_not_found;
3416
3417 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3418
3419 static int
3420 msym_not_found_p (const struct minimal_symbol *msym)
3421 {
3422 return msym == &msym_not_found;
3423 }
3424
3425 /* Return per-objfile data needed by breakpoint.c.
3426 Allocate the data if necessary. */
3427
3428 static struct breakpoint_objfile_data *
3429 get_breakpoint_objfile_data (struct objfile *objfile)
3430 {
3431 struct breakpoint_objfile_data *bp_objfile_data;
3432
3433 bp_objfile_data = ((struct breakpoint_objfile_data *)
3434 objfile_data (objfile, breakpoint_objfile_key));
3435 if (bp_objfile_data == NULL)
3436 {
3437 bp_objfile_data =
3438 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3439
3440 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3441 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3442 }
3443 return bp_objfile_data;
3444 }
3445
3446 static void
3447 free_breakpoint_probes (struct objfile *obj, void *data)
3448 {
3449 struct breakpoint_objfile_data *bp_objfile_data
3450 = (struct breakpoint_objfile_data *) data;
3451
3452 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3453 VEC_free (probe_p, bp_objfile_data->exception_probes);
3454 }
3455
3456 static void
3457 create_overlay_event_breakpoint (void)
3458 {
3459 struct objfile *objfile;
3460 const char *const func_name = "_ovly_debug_event";
3461
3462 ALL_OBJFILES (objfile)
3463 {
3464 struct breakpoint *b;
3465 struct breakpoint_objfile_data *bp_objfile_data;
3466 CORE_ADDR addr;
3467 struct explicit_location explicit_loc;
3468
3469 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3470
3471 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3472 continue;
3473
3474 if (bp_objfile_data->overlay_msym.minsym == NULL)
3475 {
3476 struct bound_minimal_symbol m;
3477
3478 m = lookup_minimal_symbol_text (func_name, objfile);
3479 if (m.minsym == NULL)
3480 {
3481 /* Avoid future lookups in this objfile. */
3482 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3483 continue;
3484 }
3485 bp_objfile_data->overlay_msym = m;
3486 }
3487
3488 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3489 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3490 bp_overlay_event,
3491 &internal_breakpoint_ops);
3492 initialize_explicit_location (&explicit_loc);
3493 explicit_loc.function_name = ASTRDUP (func_name);
3494 b->location = new_explicit_location (&explicit_loc);
3495
3496 if (overlay_debugging == ovly_auto)
3497 {
3498 b->enable_state = bp_enabled;
3499 overlay_events_enabled = 1;
3500 }
3501 else
3502 {
3503 b->enable_state = bp_disabled;
3504 overlay_events_enabled = 0;
3505 }
3506 }
3507 }
3508
3509 static void
3510 create_longjmp_master_breakpoint (void)
3511 {
3512 struct program_space *pspace;
3513
3514 scoped_restore_current_program_space restore_pspace;
3515
3516 ALL_PSPACES (pspace)
3517 {
3518 struct objfile *objfile;
3519
3520 set_current_program_space (pspace);
3521
3522 ALL_OBJFILES (objfile)
3523 {
3524 int i;
3525 struct gdbarch *gdbarch;
3526 struct breakpoint_objfile_data *bp_objfile_data;
3527
3528 gdbarch = get_objfile_arch (objfile);
3529
3530 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3531
3532 if (!bp_objfile_data->longjmp_searched)
3533 {
3534 VEC (probe_p) *ret;
3535
3536 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3537 if (ret != NULL)
3538 {
3539 /* We are only interested in checking one element. */
3540 struct probe *p = VEC_index (probe_p, ret, 0);
3541
3542 if (!can_evaluate_probe_arguments (p))
3543 {
3544 /* We cannot use the probe interface here, because it does
3545 not know how to evaluate arguments. */
3546 VEC_free (probe_p, ret);
3547 ret = NULL;
3548 }
3549 }
3550 bp_objfile_data->longjmp_probes = ret;
3551 bp_objfile_data->longjmp_searched = 1;
3552 }
3553
3554 if (bp_objfile_data->longjmp_probes != NULL)
3555 {
3556 int i;
3557 struct probe *probe;
3558 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3559
3560 for (i = 0;
3561 VEC_iterate (probe_p,
3562 bp_objfile_data->longjmp_probes,
3563 i, probe);
3564 ++i)
3565 {
3566 struct breakpoint *b;
3567
3568 b = create_internal_breakpoint (gdbarch,
3569 get_probe_address (probe,
3570 objfile),
3571 bp_longjmp_master,
3572 &internal_breakpoint_ops);
3573 b->location = new_probe_location ("-probe-stap libc:longjmp");
3574 b->enable_state = bp_disabled;
3575 }
3576
3577 continue;
3578 }
3579
3580 if (!gdbarch_get_longjmp_target_p (gdbarch))
3581 continue;
3582
3583 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3584 {
3585 struct breakpoint *b;
3586 const char *func_name;
3587 CORE_ADDR addr;
3588 struct explicit_location explicit_loc;
3589
3590 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3591 continue;
3592
3593 func_name = longjmp_names[i];
3594 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3595 {
3596 struct bound_minimal_symbol m;
3597
3598 m = lookup_minimal_symbol_text (func_name, objfile);
3599 if (m.minsym == NULL)
3600 {
3601 /* Prevent future lookups in this objfile. */
3602 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3603 continue;
3604 }
3605 bp_objfile_data->longjmp_msym[i] = m;
3606 }
3607
3608 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3609 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3610 &internal_breakpoint_ops);
3611 initialize_explicit_location (&explicit_loc);
3612 explicit_loc.function_name = ASTRDUP (func_name);
3613 b->location = new_explicit_location (&explicit_loc);
3614 b->enable_state = bp_disabled;
3615 }
3616 }
3617 }
3618 }
3619
3620 /* Create a master std::terminate breakpoint. */
3621 static void
3622 create_std_terminate_master_breakpoint (void)
3623 {
3624 struct program_space *pspace;
3625 const char *const func_name = "std::terminate()";
3626
3627 scoped_restore_current_program_space restore_pspace;
3628
3629 ALL_PSPACES (pspace)
3630 {
3631 struct objfile *objfile;
3632 CORE_ADDR addr;
3633
3634 set_current_program_space (pspace);
3635
3636 ALL_OBJFILES (objfile)
3637 {
3638 struct breakpoint *b;
3639 struct breakpoint_objfile_data *bp_objfile_data;
3640 struct explicit_location explicit_loc;
3641
3642 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3643
3644 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3645 continue;
3646
3647 if (bp_objfile_data->terminate_msym.minsym == NULL)
3648 {
3649 struct bound_minimal_symbol m;
3650
3651 m = lookup_minimal_symbol (func_name, NULL, objfile);
3652 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3653 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3654 {
3655 /* Prevent future lookups in this objfile. */
3656 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3657 continue;
3658 }
3659 bp_objfile_data->terminate_msym = m;
3660 }
3661
3662 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3663 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3664 bp_std_terminate_master,
3665 &internal_breakpoint_ops);
3666 initialize_explicit_location (&explicit_loc);
3667 explicit_loc.function_name = ASTRDUP (func_name);
3668 b->location = new_explicit_location (&explicit_loc);
3669 b->enable_state = bp_disabled;
3670 }
3671 }
3672 }
3673
3674 /* Install a master breakpoint on the unwinder's debug hook. */
3675
3676 static void
3677 create_exception_master_breakpoint (void)
3678 {
3679 struct objfile *objfile;
3680 const char *const func_name = "_Unwind_DebugHook";
3681
3682 ALL_OBJFILES (objfile)
3683 {
3684 struct breakpoint *b;
3685 struct gdbarch *gdbarch;
3686 struct breakpoint_objfile_data *bp_objfile_data;
3687 CORE_ADDR addr;
3688 struct explicit_location explicit_loc;
3689
3690 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3691
3692 /* We prefer the SystemTap probe point if it exists. */
3693 if (!bp_objfile_data->exception_searched)
3694 {
3695 VEC (probe_p) *ret;
3696
3697 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3698
3699 if (ret != NULL)
3700 {
3701 /* We are only interested in checking one element. */
3702 struct probe *p = VEC_index (probe_p, ret, 0);
3703
3704 if (!can_evaluate_probe_arguments (p))
3705 {
3706 /* We cannot use the probe interface here, because it does
3707 not know how to evaluate arguments. */
3708 VEC_free (probe_p, ret);
3709 ret = NULL;
3710 }
3711 }
3712 bp_objfile_data->exception_probes = ret;
3713 bp_objfile_data->exception_searched = 1;
3714 }
3715
3716 if (bp_objfile_data->exception_probes != NULL)
3717 {
3718 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3719 int i;
3720 struct probe *probe;
3721
3722 for (i = 0;
3723 VEC_iterate (probe_p,
3724 bp_objfile_data->exception_probes,
3725 i, probe);
3726 ++i)
3727 {
3728 struct breakpoint *b;
3729
3730 b = create_internal_breakpoint (gdbarch,
3731 get_probe_address (probe,
3732 objfile),
3733 bp_exception_master,
3734 &internal_breakpoint_ops);
3735 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3736 b->enable_state = bp_disabled;
3737 }
3738
3739 continue;
3740 }
3741
3742 /* Otherwise, try the hook function. */
3743
3744 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3745 continue;
3746
3747 gdbarch = get_objfile_arch (objfile);
3748
3749 if (bp_objfile_data->exception_msym.minsym == NULL)
3750 {
3751 struct bound_minimal_symbol debug_hook;
3752
3753 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3754 if (debug_hook.minsym == NULL)
3755 {
3756 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3757 continue;
3758 }
3759
3760 bp_objfile_data->exception_msym = debug_hook;
3761 }
3762
3763 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3764 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3765 &current_target);
3766 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3767 &internal_breakpoint_ops);
3768 initialize_explicit_location (&explicit_loc);
3769 explicit_loc.function_name = ASTRDUP (func_name);
3770 b->location = new_explicit_location (&explicit_loc);
3771 b->enable_state = bp_disabled;
3772 }
3773 }
3774
3775 /* Does B have a location spec? */
3776
3777 static int
3778 breakpoint_event_location_empty_p (const struct breakpoint *b)
3779 {
3780 return b->location != NULL && event_location_empty_p (b->location.get ());
3781 }
3782
3783 void
3784 update_breakpoints_after_exec (void)
3785 {
3786 struct breakpoint *b, *b_tmp;
3787 struct bp_location *bploc, **bplocp_tmp;
3788
3789 /* We're about to delete breakpoints from GDB's lists. If the
3790 INSERTED flag is true, GDB will try to lift the breakpoints by
3791 writing the breakpoints' "shadow contents" back into memory. The
3792 "shadow contents" are NOT valid after an exec, so GDB should not
3793 do that. Instead, the target is responsible from marking
3794 breakpoints out as soon as it detects an exec. We don't do that
3795 here instead, because there may be other attempts to delete
3796 breakpoints after detecting an exec and before reaching here. */
3797 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3798 if (bploc->pspace == current_program_space)
3799 gdb_assert (!bploc->inserted);
3800
3801 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3802 {
3803 if (b->pspace != current_program_space)
3804 continue;
3805
3806 /* Solib breakpoints must be explicitly reset after an exec(). */
3807 if (b->type == bp_shlib_event)
3808 {
3809 delete_breakpoint (b);
3810 continue;
3811 }
3812
3813 /* JIT breakpoints must be explicitly reset after an exec(). */
3814 if (b->type == bp_jit_event)
3815 {
3816 delete_breakpoint (b);
3817 continue;
3818 }
3819
3820 /* Thread event breakpoints must be set anew after an exec(),
3821 as must overlay event and longjmp master breakpoints. */
3822 if (b->type == bp_thread_event || b->type == bp_overlay_event
3823 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3824 || b->type == bp_exception_master)
3825 {
3826 delete_breakpoint (b);
3827 continue;
3828 }
3829
3830 /* Step-resume breakpoints are meaningless after an exec(). */
3831 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3832 {
3833 delete_breakpoint (b);
3834 continue;
3835 }
3836
3837 /* Just like single-step breakpoints. */
3838 if (b->type == bp_single_step)
3839 {
3840 delete_breakpoint (b);
3841 continue;
3842 }
3843
3844 /* Longjmp and longjmp-resume breakpoints are also meaningless
3845 after an exec. */
3846 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3847 || b->type == bp_longjmp_call_dummy
3848 || b->type == bp_exception || b->type == bp_exception_resume)
3849 {
3850 delete_breakpoint (b);
3851 continue;
3852 }
3853
3854 if (b->type == bp_catchpoint)
3855 {
3856 /* For now, none of the bp_catchpoint breakpoints need to
3857 do anything at this point. In the future, if some of
3858 the catchpoints need to something, we will need to add
3859 a new method, and call this method from here. */
3860 continue;
3861 }
3862
3863 /* bp_finish is a special case. The only way we ought to be able
3864 to see one of these when an exec() has happened, is if the user
3865 caught a vfork, and then said "finish". Ordinarily a finish just
3866 carries them to the call-site of the current callee, by setting
3867 a temporary bp there and resuming. But in this case, the finish
3868 will carry them entirely through the vfork & exec.
3869
3870 We don't want to allow a bp_finish to remain inserted now. But
3871 we can't safely delete it, 'cause finish_command has a handle to
3872 the bp on a bpstat, and will later want to delete it. There's a
3873 chance (and I've seen it happen) that if we delete the bp_finish
3874 here, that its storage will get reused by the time finish_command
3875 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3876 We really must allow finish_command to delete a bp_finish.
3877
3878 In the absence of a general solution for the "how do we know
3879 it's safe to delete something others may have handles to?"
3880 problem, what we'll do here is just uninsert the bp_finish, and
3881 let finish_command delete it.
3882
3883 (We know the bp_finish is "doomed" in the sense that it's
3884 momentary, and will be deleted as soon as finish_command sees
3885 the inferior stopped. So it doesn't matter that the bp's
3886 address is probably bogus in the new a.out, unlike e.g., the
3887 solib breakpoints.) */
3888
3889 if (b->type == bp_finish)
3890 {
3891 continue;
3892 }
3893
3894 /* Without a symbolic address, we have little hope of the
3895 pre-exec() address meaning the same thing in the post-exec()
3896 a.out. */
3897 if (breakpoint_event_location_empty_p (b))
3898 {
3899 delete_breakpoint (b);
3900 continue;
3901 }
3902 }
3903 }
3904
3905 int
3906 detach_breakpoints (ptid_t ptid)
3907 {
3908 struct bp_location *bl, **blp_tmp;
3909 int val = 0;
3910 struct cleanup *old_chain = save_inferior_ptid ();
3911 struct inferior *inf = current_inferior ();
3912
3913 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3914 error (_("Cannot detach breakpoints of inferior_ptid"));
3915
3916 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3917 inferior_ptid = ptid;
3918 ALL_BP_LOCATIONS (bl, blp_tmp)
3919 {
3920 if (bl->pspace != inf->pspace)
3921 continue;
3922
3923 /* This function must physically remove breakpoints locations
3924 from the specified ptid, without modifying the breakpoint
3925 package's state. Locations of type bp_loc_other are only
3926 maintained at GDB side. So, there is no need to remove
3927 these bp_loc_other locations. Moreover, removing these
3928 would modify the breakpoint package's state. */
3929 if (bl->loc_type == bp_loc_other)
3930 continue;
3931
3932 if (bl->inserted)
3933 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3934 }
3935
3936 do_cleanups (old_chain);
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 cleanup *bkpttbl_chain;
6806 struct value_print_options opts;
6807 int print_address_bits = 0;
6808 int print_type_col_width = 14;
6809 struct ui_out *uiout = current_uiout;
6810
6811 get_user_print_options (&opts);
6812
6813 /* Compute the number of rows in the table, as well as the size
6814 required for address fields. */
6815 nr_printable_breakpoints = 0;
6816 ALL_BREAKPOINTS (b)
6817 {
6818 /* If we have a filter, only list the breakpoints it accepts. */
6819 if (filter && !filter (b))
6820 continue;
6821
6822 /* If we have an "args" string, it is a list of breakpoints to
6823 accept. Skip the others. */
6824 if (args != NULL && *args != '\0')
6825 {
6826 if (allflag && parse_and_eval_long (args) != b->number)
6827 continue;
6828 if (!allflag && !number_is_in_list (args, b->number))
6829 continue;
6830 }
6831
6832 if (allflag || user_breakpoint_p (b))
6833 {
6834 int addr_bit, type_len;
6835
6836 addr_bit = breakpoint_address_bits (b);
6837 if (addr_bit > print_address_bits)
6838 print_address_bits = addr_bit;
6839
6840 type_len = strlen (bptype_string (b->type));
6841 if (type_len > print_type_col_width)
6842 print_type_col_width = type_len;
6843
6844 nr_printable_breakpoints++;
6845 }
6846 }
6847
6848 if (opts.addressprint)
6849 bkpttbl_chain
6850 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6851 nr_printable_breakpoints,
6852 "BreakpointTable");
6853 else
6854 bkpttbl_chain
6855 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6856 nr_printable_breakpoints,
6857 "BreakpointTable");
6858
6859 if (nr_printable_breakpoints > 0)
6860 annotate_breakpoints_headers ();
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (0);
6863 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6864 if (nr_printable_breakpoints > 0)
6865 annotate_field (1);
6866 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (2);
6869 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6870 if (nr_printable_breakpoints > 0)
6871 annotate_field (3);
6872 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6873 if (opts.addressprint)
6874 {
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (4);
6877 if (print_address_bits <= 32)
6878 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6879 else
6880 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6881 }
6882 if (nr_printable_breakpoints > 0)
6883 annotate_field (5);
6884 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6885 uiout->table_body ();
6886 if (nr_printable_breakpoints > 0)
6887 annotate_breakpoints_table ();
6888
6889 ALL_BREAKPOINTS (b)
6890 {
6891 QUIT;
6892 /* If we have a filter, only list the breakpoints it accepts. */
6893 if (filter && !filter (b))
6894 continue;
6895
6896 /* If we have an "args" string, it is a list of breakpoints to
6897 accept. Skip the others. */
6898
6899 if (args != NULL && *args != '\0')
6900 {
6901 if (allflag) /* maintenance info breakpoint */
6902 {
6903 if (parse_and_eval_long (args) != b->number)
6904 continue;
6905 }
6906 else /* all others */
6907 {
6908 if (!number_is_in_list (args, b->number))
6909 continue;
6910 }
6911 }
6912 /* We only print out user settable breakpoints unless the
6913 allflag is set. */
6914 if (allflag || user_breakpoint_p (b))
6915 print_one_breakpoint (b, &last_loc, allflag);
6916 }
6917
6918 do_cleanups (bkpttbl_chain);
6919
6920 if (nr_printable_breakpoints == 0)
6921 {
6922 /* If there's a filter, let the caller decide how to report
6923 empty list. */
6924 if (!filter)
6925 {
6926 if (args == NULL || *args == '\0')
6927 uiout->message ("No breakpoints or watchpoints.\n");
6928 else
6929 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6930 args);
6931 }
6932 }
6933 else
6934 {
6935 if (last_loc && !server_command)
6936 set_next_address (last_loc->gdbarch, last_loc->address);
6937 }
6938
6939 /* FIXME? Should this be moved up so that it is only called when
6940 there have been breakpoints? */
6941 annotate_breakpoints_table_end ();
6942
6943 return nr_printable_breakpoints;
6944 }
6945
6946 /* Display the value of default-collect in a way that is generally
6947 compatible with the breakpoint list. */
6948
6949 static void
6950 default_collect_info (void)
6951 {
6952 struct ui_out *uiout = current_uiout;
6953
6954 /* If it has no value (which is frequently the case), say nothing; a
6955 message like "No default-collect." gets in user's face when it's
6956 not wanted. */
6957 if (!*default_collect)
6958 return;
6959
6960 /* The following phrase lines up nicely with per-tracepoint collect
6961 actions. */
6962 uiout->text ("default collect ");
6963 uiout->field_string ("default-collect", default_collect);
6964 uiout->text (" \n");
6965 }
6966
6967 static void
6968 breakpoints_info (char *args, int from_tty)
6969 {
6970 breakpoint_1 (args, 0, NULL);
6971
6972 default_collect_info ();
6973 }
6974
6975 static void
6976 watchpoints_info (char *args, int from_tty)
6977 {
6978 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6979 struct ui_out *uiout = current_uiout;
6980
6981 if (num_printed == 0)
6982 {
6983 if (args == NULL || *args == '\0')
6984 uiout->message ("No watchpoints.\n");
6985 else
6986 uiout->message ("No watchpoint matching '%s'.\n", args);
6987 }
6988 }
6989
6990 static void
6991 maintenance_info_breakpoints (char *args, int from_tty)
6992 {
6993 breakpoint_1 (args, 1, NULL);
6994
6995 default_collect_info ();
6996 }
6997
6998 static int
6999 breakpoint_has_pc (struct breakpoint *b,
7000 struct program_space *pspace,
7001 CORE_ADDR pc, struct obj_section *section)
7002 {
7003 struct bp_location *bl = b->loc;
7004
7005 for (; bl; bl = bl->next)
7006 {
7007 if (bl->pspace == pspace
7008 && bl->address == pc
7009 && (!overlay_debugging || bl->section == section))
7010 return 1;
7011 }
7012 return 0;
7013 }
7014
7015 /* Print a message describing any user-breakpoints set at PC. This
7016 concerns with logical breakpoints, so we match program spaces, not
7017 address spaces. */
7018
7019 static void
7020 describe_other_breakpoints (struct gdbarch *gdbarch,
7021 struct program_space *pspace, CORE_ADDR pc,
7022 struct obj_section *section, int thread)
7023 {
7024 int others = 0;
7025 struct breakpoint *b;
7026
7027 ALL_BREAKPOINTS (b)
7028 others += (user_breakpoint_p (b)
7029 && breakpoint_has_pc (b, pspace, pc, section));
7030 if (others > 0)
7031 {
7032 if (others == 1)
7033 printf_filtered (_("Note: breakpoint "));
7034 else /* if (others == ???) */
7035 printf_filtered (_("Note: breakpoints "));
7036 ALL_BREAKPOINTS (b)
7037 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7038 {
7039 others--;
7040 printf_filtered ("%d", b->number);
7041 if (b->thread == -1 && thread != -1)
7042 printf_filtered (" (all threads)");
7043 else if (b->thread != -1)
7044 printf_filtered (" (thread %d)", b->thread);
7045 printf_filtered ("%s%s ",
7046 ((b->enable_state == bp_disabled
7047 || b->enable_state == bp_call_disabled)
7048 ? " (disabled)"
7049 : ""),
7050 (others > 1) ? ","
7051 : ((others == 1) ? " and" : ""));
7052 }
7053 printf_filtered (_("also set at pc "));
7054 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7055 printf_filtered (".\n");
7056 }
7057 }
7058 \f
7059
7060 /* Return true iff it is meaningful to use the address member of
7061 BPT locations. For some breakpoint types, the locations' address members
7062 are irrelevant and it makes no sense to attempt to compare them to other
7063 addresses (or use them for any other purpose either).
7064
7065 More specifically, each of the following breakpoint types will
7066 always have a zero valued location address and we don't want to mark
7067 breakpoints of any of these types to be a duplicate of an actual
7068 breakpoint location at address zero:
7069
7070 bp_watchpoint
7071 bp_catchpoint
7072
7073 */
7074
7075 static int
7076 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7077 {
7078 enum bptype type = bpt->type;
7079
7080 return (type != bp_watchpoint && type != bp_catchpoint);
7081 }
7082
7083 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7084 true if LOC1 and LOC2 represent the same watchpoint location. */
7085
7086 static int
7087 watchpoint_locations_match (struct bp_location *loc1,
7088 struct bp_location *loc2)
7089 {
7090 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7091 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7092
7093 /* Both of them must exist. */
7094 gdb_assert (w1 != NULL);
7095 gdb_assert (w2 != NULL);
7096
7097 /* If the target can evaluate the condition expression in hardware,
7098 then we we need to insert both watchpoints even if they are at
7099 the same place. Otherwise the watchpoint will only trigger when
7100 the condition of whichever watchpoint was inserted evaluates to
7101 true, not giving a chance for GDB to check the condition of the
7102 other watchpoint. */
7103 if ((w1->cond_exp
7104 && target_can_accel_watchpoint_condition (loc1->address,
7105 loc1->length,
7106 loc1->watchpoint_type,
7107 w1->cond_exp.get ()))
7108 || (w2->cond_exp
7109 && target_can_accel_watchpoint_condition (loc2->address,
7110 loc2->length,
7111 loc2->watchpoint_type,
7112 w2->cond_exp.get ())))
7113 return 0;
7114
7115 /* Note that this checks the owner's type, not the location's. In
7116 case the target does not support read watchpoints, but does
7117 support access watchpoints, we'll have bp_read_watchpoint
7118 watchpoints with hw_access locations. Those should be considered
7119 duplicates of hw_read locations. The hw_read locations will
7120 become hw_access locations later. */
7121 return (loc1->owner->type == loc2->owner->type
7122 && loc1->pspace->aspace == loc2->pspace->aspace
7123 && loc1->address == loc2->address
7124 && loc1->length == loc2->length);
7125 }
7126
7127 /* See breakpoint.h. */
7128
7129 int
7130 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7131 struct address_space *aspace2, CORE_ADDR addr2)
7132 {
7133 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7134 || aspace1 == aspace2)
7135 && addr1 == addr2);
7136 }
7137
7138 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7139 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7140 matches ASPACE2. On targets that have global breakpoints, the address
7141 space doesn't really matter. */
7142
7143 static int
7144 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7145 int len1, struct address_space *aspace2,
7146 CORE_ADDR addr2)
7147 {
7148 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7149 || aspace1 == aspace2)
7150 && addr2 >= addr1 && addr2 < addr1 + len1);
7151 }
7152
7153 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7154 a ranged breakpoint. In most targets, a match happens only if ASPACE
7155 matches the breakpoint's address space. On targets that have global
7156 breakpoints, the address space doesn't really matter. */
7157
7158 static int
7159 breakpoint_location_address_match (struct bp_location *bl,
7160 struct address_space *aspace,
7161 CORE_ADDR addr)
7162 {
7163 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7164 aspace, addr)
7165 || (bl->length
7166 && breakpoint_address_match_range (bl->pspace->aspace,
7167 bl->address, bl->length,
7168 aspace, addr)));
7169 }
7170
7171 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7172 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7173 match happens only if ASPACE matches the breakpoint's address
7174 space. On targets that have global breakpoints, the address space
7175 doesn't really matter. */
7176
7177 static int
7178 breakpoint_location_address_range_overlap (struct bp_location *bl,
7179 struct address_space *aspace,
7180 CORE_ADDR addr, int len)
7181 {
7182 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7183 || bl->pspace->aspace == aspace)
7184 {
7185 int bl_len = bl->length != 0 ? bl->length : 1;
7186
7187 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7188 return 1;
7189 }
7190 return 0;
7191 }
7192
7193 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7194 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7195 true, otherwise returns false. */
7196
7197 static int
7198 tracepoint_locations_match (struct bp_location *loc1,
7199 struct bp_location *loc2)
7200 {
7201 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7202 /* Since tracepoint locations are never duplicated with others', tracepoint
7203 locations at the same address of different tracepoints are regarded as
7204 different locations. */
7205 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7206 else
7207 return 0;
7208 }
7209
7210 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7211 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7212 represent the same location. */
7213
7214 static int
7215 breakpoint_locations_match (struct bp_location *loc1,
7216 struct bp_location *loc2)
7217 {
7218 int hw_point1, hw_point2;
7219
7220 /* Both of them must not be in moribund_locations. */
7221 gdb_assert (loc1->owner != NULL);
7222 gdb_assert (loc2->owner != NULL);
7223
7224 hw_point1 = is_hardware_watchpoint (loc1->owner);
7225 hw_point2 = is_hardware_watchpoint (loc2->owner);
7226
7227 if (hw_point1 != hw_point2)
7228 return 0;
7229 else if (hw_point1)
7230 return watchpoint_locations_match (loc1, loc2);
7231 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7232 return tracepoint_locations_match (loc1, loc2);
7233 else
7234 /* We compare bp_location.length in order to cover ranged breakpoints. */
7235 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7236 loc2->pspace->aspace, loc2->address)
7237 && loc1->length == loc2->length);
7238 }
7239
7240 static void
7241 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7242 int bnum, int have_bnum)
7243 {
7244 /* The longest string possibly returned by hex_string_custom
7245 is 50 chars. These must be at least that big for safety. */
7246 char astr1[64];
7247 char astr2[64];
7248
7249 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7250 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7251 if (have_bnum)
7252 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7253 bnum, astr1, astr2);
7254 else
7255 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7256 }
7257
7258 /* Adjust a breakpoint's address to account for architectural
7259 constraints on breakpoint placement. Return the adjusted address.
7260 Note: Very few targets require this kind of adjustment. For most
7261 targets, this function is simply the identity function. */
7262
7263 static CORE_ADDR
7264 adjust_breakpoint_address (struct gdbarch *gdbarch,
7265 CORE_ADDR bpaddr, enum bptype bptype)
7266 {
7267 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7268 {
7269 /* Very few targets need any kind of breakpoint adjustment. */
7270 return bpaddr;
7271 }
7272 else if (bptype == bp_watchpoint
7273 || bptype == bp_hardware_watchpoint
7274 || bptype == bp_read_watchpoint
7275 || bptype == bp_access_watchpoint
7276 || bptype == bp_catchpoint)
7277 {
7278 /* Watchpoints and the various bp_catch_* eventpoints should not
7279 have their addresses modified. */
7280 return bpaddr;
7281 }
7282 else if (bptype == bp_single_step)
7283 {
7284 /* Single-step breakpoints should not have their addresses
7285 modified. If there's any architectural constrain that
7286 applies to this address, then it should have already been
7287 taken into account when the breakpoint was created in the
7288 first place. If we didn't do this, stepping through e.g.,
7289 Thumb-2 IT blocks would break. */
7290 return bpaddr;
7291 }
7292 else
7293 {
7294 CORE_ADDR adjusted_bpaddr;
7295
7296 /* Some targets have architectural constraints on the placement
7297 of breakpoint instructions. Obtain the adjusted address. */
7298 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7299
7300 /* An adjusted breakpoint address can significantly alter
7301 a user's expectations. Print a warning if an adjustment
7302 is required. */
7303 if (adjusted_bpaddr != bpaddr)
7304 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7305
7306 return adjusted_bpaddr;
7307 }
7308 }
7309
7310 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7311 {
7312 bp_location *loc = this;
7313
7314 gdb_assert (ops != NULL);
7315
7316 loc->ops = ops;
7317 loc->owner = owner;
7318 loc->cond_bytecode = NULL;
7319 loc->shlib_disabled = 0;
7320 loc->enabled = 1;
7321
7322 switch (owner->type)
7323 {
7324 case bp_breakpoint:
7325 case bp_single_step:
7326 case bp_until:
7327 case bp_finish:
7328 case bp_longjmp:
7329 case bp_longjmp_resume:
7330 case bp_longjmp_call_dummy:
7331 case bp_exception:
7332 case bp_exception_resume:
7333 case bp_step_resume:
7334 case bp_hp_step_resume:
7335 case bp_watchpoint_scope:
7336 case bp_call_dummy:
7337 case bp_std_terminate:
7338 case bp_shlib_event:
7339 case bp_thread_event:
7340 case bp_overlay_event:
7341 case bp_jit_event:
7342 case bp_longjmp_master:
7343 case bp_std_terminate_master:
7344 case bp_exception_master:
7345 case bp_gnu_ifunc_resolver:
7346 case bp_gnu_ifunc_resolver_return:
7347 case bp_dprintf:
7348 loc->loc_type = bp_loc_software_breakpoint;
7349 mark_breakpoint_location_modified (loc);
7350 break;
7351 case bp_hardware_breakpoint:
7352 loc->loc_type = bp_loc_hardware_breakpoint;
7353 mark_breakpoint_location_modified (loc);
7354 break;
7355 case bp_hardware_watchpoint:
7356 case bp_read_watchpoint:
7357 case bp_access_watchpoint:
7358 loc->loc_type = bp_loc_hardware_watchpoint;
7359 break;
7360 case bp_watchpoint:
7361 case bp_catchpoint:
7362 case bp_tracepoint:
7363 case bp_fast_tracepoint:
7364 case bp_static_tracepoint:
7365 loc->loc_type = bp_loc_other;
7366 break;
7367 default:
7368 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7369 }
7370
7371 loc->refc = 1;
7372 }
7373
7374 /* Allocate a struct bp_location. */
7375
7376 static struct bp_location *
7377 allocate_bp_location (struct breakpoint *bpt)
7378 {
7379 return bpt->ops->allocate_location (bpt);
7380 }
7381
7382 static void
7383 free_bp_location (struct bp_location *loc)
7384 {
7385 loc->ops->dtor (loc);
7386 delete loc;
7387 }
7388
7389 /* Increment reference count. */
7390
7391 static void
7392 incref_bp_location (struct bp_location *bl)
7393 {
7394 ++bl->refc;
7395 }
7396
7397 /* Decrement reference count. If the reference count reaches 0,
7398 destroy the bp_location. Sets *BLP to NULL. */
7399
7400 static void
7401 decref_bp_location (struct bp_location **blp)
7402 {
7403 gdb_assert ((*blp)->refc > 0);
7404
7405 if (--(*blp)->refc == 0)
7406 free_bp_location (*blp);
7407 *blp = NULL;
7408 }
7409
7410 /* Add breakpoint B at the end of the global breakpoint chain. */
7411
7412 static void
7413 add_to_breakpoint_chain (struct breakpoint *b)
7414 {
7415 struct breakpoint *b1;
7416
7417 /* Add this breakpoint to the end of the chain so that a list of
7418 breakpoints will come out in order of increasing numbers. */
7419
7420 b1 = breakpoint_chain;
7421 if (b1 == 0)
7422 breakpoint_chain = b;
7423 else
7424 {
7425 while (b1->next)
7426 b1 = b1->next;
7427 b1->next = b;
7428 }
7429 }
7430
7431 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7432
7433 static void
7434 init_raw_breakpoint_without_location (struct breakpoint *b,
7435 struct gdbarch *gdbarch,
7436 enum bptype bptype,
7437 const struct breakpoint_ops *ops)
7438 {
7439 gdb_assert (ops != NULL);
7440
7441 b->ops = ops;
7442 b->type = bptype;
7443 b->gdbarch = gdbarch;
7444 b->language = current_language->la_language;
7445 b->input_radix = input_radix;
7446 b->related_breakpoint = b;
7447 }
7448
7449 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7450 that has type BPTYPE and has no locations as yet. */
7451
7452 static struct breakpoint *
7453 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7454 enum bptype bptype,
7455 const struct breakpoint_ops *ops)
7456 {
7457 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7458
7459 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7460 add_to_breakpoint_chain (b.get ());
7461
7462 return b.release ();
7463 }
7464
7465 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7466 resolutions should be made as the user specified the location explicitly
7467 enough. */
7468
7469 static void
7470 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7471 {
7472 gdb_assert (loc->owner != NULL);
7473
7474 if (loc->owner->type == bp_breakpoint
7475 || loc->owner->type == bp_hardware_breakpoint
7476 || is_tracepoint (loc->owner))
7477 {
7478 int is_gnu_ifunc;
7479 const char *function_name;
7480 CORE_ADDR func_addr;
7481
7482 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7483 &func_addr, NULL, &is_gnu_ifunc);
7484
7485 if (is_gnu_ifunc && !explicit_loc)
7486 {
7487 struct breakpoint *b = loc->owner;
7488
7489 gdb_assert (loc->pspace == current_program_space);
7490 if (gnu_ifunc_resolve_name (function_name,
7491 &loc->requested_address))
7492 {
7493 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7494 loc->address = adjust_breakpoint_address (loc->gdbarch,
7495 loc->requested_address,
7496 b->type);
7497 }
7498 else if (b->type == bp_breakpoint && b->loc == loc
7499 && loc->next == NULL && b->related_breakpoint == b)
7500 {
7501 /* Create only the whole new breakpoint of this type but do not
7502 mess more complicated breakpoints with multiple locations. */
7503 b->type = bp_gnu_ifunc_resolver;
7504 /* Remember the resolver's address for use by the return
7505 breakpoint. */
7506 loc->related_address = func_addr;
7507 }
7508 }
7509
7510 if (function_name)
7511 loc->function_name = xstrdup (function_name);
7512 }
7513 }
7514
7515 /* Attempt to determine architecture of location identified by SAL. */
7516 struct gdbarch *
7517 get_sal_arch (struct symtab_and_line sal)
7518 {
7519 if (sal.section)
7520 return get_objfile_arch (sal.section->objfile);
7521 if (sal.symtab)
7522 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7523
7524 return NULL;
7525 }
7526
7527 /* Low level routine for partially initializing a breakpoint of type
7528 BPTYPE. The newly created breakpoint's address, section, source
7529 file name, and line number are provided by SAL.
7530
7531 It is expected that the caller will complete the initialization of
7532 the newly created breakpoint struct as well as output any status
7533 information regarding the creation of a new breakpoint. */
7534
7535 static void
7536 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7537 struct symtab_and_line sal, enum bptype bptype,
7538 const struct breakpoint_ops *ops)
7539 {
7540 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7541
7542 add_location_to_breakpoint (b, &sal);
7543
7544 if (bptype != bp_catchpoint)
7545 gdb_assert (sal.pspace != NULL);
7546
7547 /* Store the program space that was used to set the breakpoint,
7548 except for ordinary breakpoints, which are independent of the
7549 program space. */
7550 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7551 b->pspace = sal.pspace;
7552 }
7553
7554 /* set_raw_breakpoint is a low level routine for allocating and
7555 partially initializing a breakpoint of type BPTYPE. The newly
7556 created breakpoint's address, section, source file name, and line
7557 number are provided by SAL. The newly created and partially
7558 initialized breakpoint is added to the breakpoint chain and
7559 is also returned as the value of this function.
7560
7561 It is expected that the caller will complete the initialization of
7562 the newly created breakpoint struct as well as output any status
7563 information regarding the creation of a new breakpoint. In
7564 particular, set_raw_breakpoint does NOT set the breakpoint
7565 number! Care should be taken to not allow an error to occur
7566 prior to completing the initialization of the breakpoint. If this
7567 should happen, a bogus breakpoint will be left on the chain. */
7568
7569 struct breakpoint *
7570 set_raw_breakpoint (struct gdbarch *gdbarch,
7571 struct symtab_and_line sal, enum bptype bptype,
7572 const struct breakpoint_ops *ops)
7573 {
7574 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7575
7576 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7577 add_to_breakpoint_chain (b.get ());
7578
7579 return b.release ();
7580 }
7581
7582 /* Call this routine when stepping and nexting to enable a breakpoint
7583 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7584 initiated the operation. */
7585
7586 void
7587 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7588 {
7589 struct breakpoint *b, *b_tmp;
7590 int thread = tp->global_num;
7591
7592 /* To avoid having to rescan all objfile symbols at every step,
7593 we maintain a list of continually-inserted but always disabled
7594 longjmp "master" breakpoints. Here, we simply create momentary
7595 clones of those and enable them for the requested thread. */
7596 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7597 if (b->pspace == current_program_space
7598 && (b->type == bp_longjmp_master
7599 || b->type == bp_exception_master))
7600 {
7601 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7602 struct breakpoint *clone;
7603
7604 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7605 after their removal. */
7606 clone = momentary_breakpoint_from_master (b, type,
7607 &momentary_breakpoint_ops, 1);
7608 clone->thread = thread;
7609 }
7610
7611 tp->initiating_frame = frame;
7612 }
7613
7614 /* Delete all longjmp breakpoints from THREAD. */
7615 void
7616 delete_longjmp_breakpoint (int thread)
7617 {
7618 struct breakpoint *b, *b_tmp;
7619
7620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7621 if (b->type == bp_longjmp || b->type == bp_exception)
7622 {
7623 if (b->thread == thread)
7624 delete_breakpoint (b);
7625 }
7626 }
7627
7628 void
7629 delete_longjmp_breakpoint_at_next_stop (int thread)
7630 {
7631 struct breakpoint *b, *b_tmp;
7632
7633 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7634 if (b->type == bp_longjmp || b->type == bp_exception)
7635 {
7636 if (b->thread == thread)
7637 b->disposition = disp_del_at_next_stop;
7638 }
7639 }
7640
7641 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7642 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7643 pointer to any of them. Return NULL if this system cannot place longjmp
7644 breakpoints. */
7645
7646 struct breakpoint *
7647 set_longjmp_breakpoint_for_call_dummy (void)
7648 {
7649 struct breakpoint *b, *retval = NULL;
7650
7651 ALL_BREAKPOINTS (b)
7652 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7653 {
7654 struct breakpoint *new_b;
7655
7656 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7657 &momentary_breakpoint_ops,
7658 1);
7659 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7660
7661 /* Link NEW_B into the chain of RETVAL breakpoints. */
7662
7663 gdb_assert (new_b->related_breakpoint == new_b);
7664 if (retval == NULL)
7665 retval = new_b;
7666 new_b->related_breakpoint = retval;
7667 while (retval->related_breakpoint != new_b->related_breakpoint)
7668 retval = retval->related_breakpoint;
7669 retval->related_breakpoint = new_b;
7670 }
7671
7672 return retval;
7673 }
7674
7675 /* Verify all existing dummy frames and their associated breakpoints for
7676 TP. Remove those which can no longer be found in the current frame
7677 stack.
7678
7679 You should call this function only at places where it is safe to currently
7680 unwind the whole stack. Failed stack unwind would discard live dummy
7681 frames. */
7682
7683 void
7684 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7685 {
7686 struct breakpoint *b, *b_tmp;
7687
7688 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7689 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7690 {
7691 struct breakpoint *dummy_b = b->related_breakpoint;
7692
7693 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7694 dummy_b = dummy_b->related_breakpoint;
7695 if (dummy_b->type != bp_call_dummy
7696 || frame_find_by_id (dummy_b->frame_id) != NULL)
7697 continue;
7698
7699 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7700
7701 while (b->related_breakpoint != b)
7702 {
7703 if (b_tmp == b->related_breakpoint)
7704 b_tmp = b->related_breakpoint->next;
7705 delete_breakpoint (b->related_breakpoint);
7706 }
7707 delete_breakpoint (b);
7708 }
7709 }
7710
7711 void
7712 enable_overlay_breakpoints (void)
7713 {
7714 struct breakpoint *b;
7715
7716 ALL_BREAKPOINTS (b)
7717 if (b->type == bp_overlay_event)
7718 {
7719 b->enable_state = bp_enabled;
7720 update_global_location_list (UGLL_MAY_INSERT);
7721 overlay_events_enabled = 1;
7722 }
7723 }
7724
7725 void
7726 disable_overlay_breakpoints (void)
7727 {
7728 struct breakpoint *b;
7729
7730 ALL_BREAKPOINTS (b)
7731 if (b->type == bp_overlay_event)
7732 {
7733 b->enable_state = bp_disabled;
7734 update_global_location_list (UGLL_DONT_INSERT);
7735 overlay_events_enabled = 0;
7736 }
7737 }
7738
7739 /* Set an active std::terminate breakpoint for each std::terminate
7740 master breakpoint. */
7741 void
7742 set_std_terminate_breakpoint (void)
7743 {
7744 struct breakpoint *b, *b_tmp;
7745
7746 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7747 if (b->pspace == current_program_space
7748 && b->type == bp_std_terminate_master)
7749 {
7750 momentary_breakpoint_from_master (b, bp_std_terminate,
7751 &momentary_breakpoint_ops, 1);
7752 }
7753 }
7754
7755 /* Delete all the std::terminate breakpoints. */
7756 void
7757 delete_std_terminate_breakpoint (void)
7758 {
7759 struct breakpoint *b, *b_tmp;
7760
7761 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7762 if (b->type == bp_std_terminate)
7763 delete_breakpoint (b);
7764 }
7765
7766 struct breakpoint *
7767 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7768 {
7769 struct breakpoint *b;
7770
7771 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7772 &internal_breakpoint_ops);
7773
7774 b->enable_state = bp_enabled;
7775 /* location has to be used or breakpoint_re_set will delete me. */
7776 b->location = new_address_location (b->loc->address, NULL, 0);
7777
7778 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7779
7780 return b;
7781 }
7782
7783 struct lang_and_radix
7784 {
7785 enum language lang;
7786 int radix;
7787 };
7788
7789 /* Create a breakpoint for JIT code registration and unregistration. */
7790
7791 struct breakpoint *
7792 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7793 {
7794 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7795 &internal_breakpoint_ops);
7796 }
7797
7798 /* Remove JIT code registration and unregistration breakpoint(s). */
7799
7800 void
7801 remove_jit_event_breakpoints (void)
7802 {
7803 struct breakpoint *b, *b_tmp;
7804
7805 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7806 if (b->type == bp_jit_event
7807 && b->loc->pspace == current_program_space)
7808 delete_breakpoint (b);
7809 }
7810
7811 void
7812 remove_solib_event_breakpoints (void)
7813 {
7814 struct breakpoint *b, *b_tmp;
7815
7816 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7817 if (b->type == bp_shlib_event
7818 && b->loc->pspace == current_program_space)
7819 delete_breakpoint (b);
7820 }
7821
7822 /* See breakpoint.h. */
7823
7824 void
7825 remove_solib_event_breakpoints_at_next_stop (void)
7826 {
7827 struct breakpoint *b, *b_tmp;
7828
7829 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7830 if (b->type == bp_shlib_event
7831 && b->loc->pspace == current_program_space)
7832 b->disposition = disp_del_at_next_stop;
7833 }
7834
7835 /* Helper for create_solib_event_breakpoint /
7836 create_and_insert_solib_event_breakpoint. Allows specifying which
7837 INSERT_MODE to pass through to update_global_location_list. */
7838
7839 static struct breakpoint *
7840 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7841 enum ugll_insert_mode insert_mode)
7842 {
7843 struct breakpoint *b;
7844
7845 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7846 &internal_breakpoint_ops);
7847 update_global_location_list_nothrow (insert_mode);
7848 return b;
7849 }
7850
7851 struct breakpoint *
7852 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7853 {
7854 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7855 }
7856
7857 /* See breakpoint.h. */
7858
7859 struct breakpoint *
7860 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7861 {
7862 struct breakpoint *b;
7863
7864 /* Explicitly tell update_global_location_list to insert
7865 locations. */
7866 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7867 if (!b->loc->inserted)
7868 {
7869 delete_breakpoint (b);
7870 return NULL;
7871 }
7872 return b;
7873 }
7874
7875 /* Disable any breakpoints that are on code in shared libraries. Only
7876 apply to enabled breakpoints, disabled ones can just stay disabled. */
7877
7878 void
7879 disable_breakpoints_in_shlibs (void)
7880 {
7881 struct bp_location *loc, **locp_tmp;
7882
7883 ALL_BP_LOCATIONS (loc, locp_tmp)
7884 {
7885 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7886 struct breakpoint *b = loc->owner;
7887
7888 /* We apply the check to all breakpoints, including disabled for
7889 those with loc->duplicate set. This is so that when breakpoint
7890 becomes enabled, or the duplicate is removed, gdb will try to
7891 insert all breakpoints. If we don't set shlib_disabled here,
7892 we'll try to insert those breakpoints and fail. */
7893 if (((b->type == bp_breakpoint)
7894 || (b->type == bp_jit_event)
7895 || (b->type == bp_hardware_breakpoint)
7896 || (is_tracepoint (b)))
7897 && loc->pspace == current_program_space
7898 && !loc->shlib_disabled
7899 && solib_name_from_address (loc->pspace, loc->address)
7900 )
7901 {
7902 loc->shlib_disabled = 1;
7903 }
7904 }
7905 }
7906
7907 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7908 notification of unloaded_shlib. Only apply to enabled breakpoints,
7909 disabled ones can just stay disabled. */
7910
7911 static void
7912 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7913 {
7914 struct bp_location *loc, **locp_tmp;
7915 int disabled_shlib_breaks = 0;
7916
7917 ALL_BP_LOCATIONS (loc, locp_tmp)
7918 {
7919 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7920 struct breakpoint *b = loc->owner;
7921
7922 if (solib->pspace == loc->pspace
7923 && !loc->shlib_disabled
7924 && (((b->type == bp_breakpoint
7925 || b->type == bp_jit_event
7926 || b->type == bp_hardware_breakpoint)
7927 && (loc->loc_type == bp_loc_hardware_breakpoint
7928 || loc->loc_type == bp_loc_software_breakpoint))
7929 || is_tracepoint (b))
7930 && solib_contains_address_p (solib, loc->address))
7931 {
7932 loc->shlib_disabled = 1;
7933 /* At this point, we cannot rely on remove_breakpoint
7934 succeeding so we must mark the breakpoint as not inserted
7935 to prevent future errors occurring in remove_breakpoints. */
7936 loc->inserted = 0;
7937
7938 /* This may cause duplicate notifications for the same breakpoint. */
7939 observer_notify_breakpoint_modified (b);
7940
7941 if (!disabled_shlib_breaks)
7942 {
7943 target_terminal_ours_for_output ();
7944 warning (_("Temporarily disabling breakpoints "
7945 "for unloaded shared library \"%s\""),
7946 solib->so_name);
7947 }
7948 disabled_shlib_breaks = 1;
7949 }
7950 }
7951 }
7952
7953 /* Disable any breakpoints and tracepoints in OBJFILE upon
7954 notification of free_objfile. Only apply to enabled breakpoints,
7955 disabled ones can just stay disabled. */
7956
7957 static void
7958 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7959 {
7960 struct breakpoint *b;
7961
7962 if (objfile == NULL)
7963 return;
7964
7965 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7966 managed by the user with add-symbol-file/remove-symbol-file.
7967 Similarly to how breakpoints in shared libraries are handled in
7968 response to "nosharedlibrary", mark breakpoints in such modules
7969 shlib_disabled so they end up uninserted on the next global
7970 location list update. Shared libraries not loaded by the user
7971 aren't handled here -- they're already handled in
7972 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7973 solib_unloaded observer. We skip objfiles that are not
7974 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7975 main objfile). */
7976 if ((objfile->flags & OBJF_SHARED) == 0
7977 || (objfile->flags & OBJF_USERLOADED) == 0)
7978 return;
7979
7980 ALL_BREAKPOINTS (b)
7981 {
7982 struct bp_location *loc;
7983 int bp_modified = 0;
7984
7985 if (!is_breakpoint (b) && !is_tracepoint (b))
7986 continue;
7987
7988 for (loc = b->loc; loc != NULL; loc = loc->next)
7989 {
7990 CORE_ADDR loc_addr = loc->address;
7991
7992 if (loc->loc_type != bp_loc_hardware_breakpoint
7993 && loc->loc_type != bp_loc_software_breakpoint)
7994 continue;
7995
7996 if (loc->shlib_disabled != 0)
7997 continue;
7998
7999 if (objfile->pspace != loc->pspace)
8000 continue;
8001
8002 if (loc->loc_type != bp_loc_hardware_breakpoint
8003 && loc->loc_type != bp_loc_software_breakpoint)
8004 continue;
8005
8006 if (is_addr_in_objfile (loc_addr, objfile))
8007 {
8008 loc->shlib_disabled = 1;
8009 /* At this point, we don't know whether the object was
8010 unmapped from the inferior or not, so leave the
8011 inserted flag alone. We'll handle failure to
8012 uninsert quietly, in case the object was indeed
8013 unmapped. */
8014
8015 mark_breakpoint_location_modified (loc);
8016
8017 bp_modified = 1;
8018 }
8019 }
8020
8021 if (bp_modified)
8022 observer_notify_breakpoint_modified (b);
8023 }
8024 }
8025
8026 /* FORK & VFORK catchpoints. */
8027
8028 /* An instance of this type is used to represent a fork or vfork
8029 catchpoint. A breakpoint is really of this type iff its ops pointer points
8030 to CATCH_FORK_BREAKPOINT_OPS. */
8031
8032 struct fork_catchpoint : public breakpoint
8033 {
8034 /* Process id of a child process whose forking triggered this
8035 catchpoint. This field is only valid immediately after this
8036 catchpoint has triggered. */
8037 ptid_t forked_inferior_pid;
8038 };
8039
8040 /* Implement the "insert" breakpoint_ops method for fork
8041 catchpoints. */
8042
8043 static int
8044 insert_catch_fork (struct bp_location *bl)
8045 {
8046 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8047 }
8048
8049 /* Implement the "remove" breakpoint_ops method for fork
8050 catchpoints. */
8051
8052 static int
8053 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8054 {
8055 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8056 }
8057
8058 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8059 catchpoints. */
8060
8061 static int
8062 breakpoint_hit_catch_fork (const struct bp_location *bl,
8063 struct address_space *aspace, CORE_ADDR bp_addr,
8064 const struct target_waitstatus *ws)
8065 {
8066 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8067
8068 if (ws->kind != TARGET_WAITKIND_FORKED)
8069 return 0;
8070
8071 c->forked_inferior_pid = ws->value.related_pid;
8072 return 1;
8073 }
8074
8075 /* Implement the "print_it" breakpoint_ops method for fork
8076 catchpoints. */
8077
8078 static enum print_stop_action
8079 print_it_catch_fork (bpstat bs)
8080 {
8081 struct ui_out *uiout = current_uiout;
8082 struct breakpoint *b = bs->breakpoint_at;
8083 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8084
8085 annotate_catchpoint (b->number);
8086 maybe_print_thread_hit_breakpoint (uiout);
8087 if (b->disposition == disp_del)
8088 uiout->text ("Temporary catchpoint ");
8089 else
8090 uiout->text ("Catchpoint ");
8091 if (uiout->is_mi_like_p ())
8092 {
8093 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8094 uiout->field_string ("disp", bpdisp_text (b->disposition));
8095 }
8096 uiout->field_int ("bkptno", b->number);
8097 uiout->text (" (forked process ");
8098 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8099 uiout->text ("), ");
8100 return PRINT_SRC_AND_LOC;
8101 }
8102
8103 /* Implement the "print_one" breakpoint_ops method for fork
8104 catchpoints. */
8105
8106 static void
8107 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8108 {
8109 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8110 struct value_print_options opts;
8111 struct ui_out *uiout = current_uiout;
8112
8113 get_user_print_options (&opts);
8114
8115 /* Field 4, the address, is omitted (which makes the columns not
8116 line up too nicely with the headers, but the effect is relatively
8117 readable). */
8118 if (opts.addressprint)
8119 uiout->field_skip ("addr");
8120 annotate_field (5);
8121 uiout->text ("fork");
8122 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8123 {
8124 uiout->text (", process ");
8125 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8126 uiout->spaces (1);
8127 }
8128
8129 if (uiout->is_mi_like_p ())
8130 uiout->field_string ("catch-type", "fork");
8131 }
8132
8133 /* Implement the "print_mention" breakpoint_ops method for fork
8134 catchpoints. */
8135
8136 static void
8137 print_mention_catch_fork (struct breakpoint *b)
8138 {
8139 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8140 }
8141
8142 /* Implement the "print_recreate" breakpoint_ops method for fork
8143 catchpoints. */
8144
8145 static void
8146 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8147 {
8148 fprintf_unfiltered (fp, "catch fork");
8149 print_recreate_thread (b, fp);
8150 }
8151
8152 /* The breakpoint_ops structure to be used in fork catchpoints. */
8153
8154 static struct breakpoint_ops catch_fork_breakpoint_ops;
8155
8156 /* Implement the "insert" breakpoint_ops method for vfork
8157 catchpoints. */
8158
8159 static int
8160 insert_catch_vfork (struct bp_location *bl)
8161 {
8162 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8163 }
8164
8165 /* Implement the "remove" breakpoint_ops method for vfork
8166 catchpoints. */
8167
8168 static int
8169 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8170 {
8171 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8172 }
8173
8174 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8175 catchpoints. */
8176
8177 static int
8178 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8179 struct address_space *aspace, CORE_ADDR bp_addr,
8180 const struct target_waitstatus *ws)
8181 {
8182 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8183
8184 if (ws->kind != TARGET_WAITKIND_VFORKED)
8185 return 0;
8186
8187 c->forked_inferior_pid = ws->value.related_pid;
8188 return 1;
8189 }
8190
8191 /* Implement the "print_it" breakpoint_ops method for vfork
8192 catchpoints. */
8193
8194 static enum print_stop_action
8195 print_it_catch_vfork (bpstat bs)
8196 {
8197 struct ui_out *uiout = current_uiout;
8198 struct breakpoint *b = bs->breakpoint_at;
8199 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8200
8201 annotate_catchpoint (b->number);
8202 maybe_print_thread_hit_breakpoint (uiout);
8203 if (b->disposition == disp_del)
8204 uiout->text ("Temporary catchpoint ");
8205 else
8206 uiout->text ("Catchpoint ");
8207 if (uiout->is_mi_like_p ())
8208 {
8209 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8210 uiout->field_string ("disp", bpdisp_text (b->disposition));
8211 }
8212 uiout->field_int ("bkptno", b->number);
8213 uiout->text (" (vforked process ");
8214 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8215 uiout->text ("), ");
8216 return PRINT_SRC_AND_LOC;
8217 }
8218
8219 /* Implement the "print_one" breakpoint_ops method for vfork
8220 catchpoints. */
8221
8222 static void
8223 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8224 {
8225 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8226 struct value_print_options opts;
8227 struct ui_out *uiout = current_uiout;
8228
8229 get_user_print_options (&opts);
8230 /* Field 4, the address, is omitted (which makes the columns not
8231 line up too nicely with the headers, but the effect is relatively
8232 readable). */
8233 if (opts.addressprint)
8234 uiout->field_skip ("addr");
8235 annotate_field (5);
8236 uiout->text ("vfork");
8237 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8238 {
8239 uiout->text (", process ");
8240 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8241 uiout->spaces (1);
8242 }
8243
8244 if (uiout->is_mi_like_p ())
8245 uiout->field_string ("catch-type", "vfork");
8246 }
8247
8248 /* Implement the "print_mention" breakpoint_ops method for vfork
8249 catchpoints. */
8250
8251 static void
8252 print_mention_catch_vfork (struct breakpoint *b)
8253 {
8254 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8255 }
8256
8257 /* Implement the "print_recreate" breakpoint_ops method for vfork
8258 catchpoints. */
8259
8260 static void
8261 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8262 {
8263 fprintf_unfiltered (fp, "catch vfork");
8264 print_recreate_thread (b, fp);
8265 }
8266
8267 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8268
8269 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8270
8271 /* An instance of this type is used to represent an solib catchpoint.
8272 A breakpoint is really of this type iff its ops pointer points to
8273 CATCH_SOLIB_BREAKPOINT_OPS. */
8274
8275 struct solib_catchpoint : public breakpoint
8276 {
8277 ~solib_catchpoint () override;
8278
8279 /* True for "catch load", false for "catch unload". */
8280 unsigned char is_load;
8281
8282 /* Regular expression to match, if any. COMPILED is only valid when
8283 REGEX is non-NULL. */
8284 char *regex;
8285 regex_t compiled;
8286 };
8287
8288 solib_catchpoint::~solib_catchpoint ()
8289 {
8290 if (this->regex)
8291 regfree (&this->compiled);
8292 xfree (this->regex);
8293 }
8294
8295 static int
8296 insert_catch_solib (struct bp_location *ignore)
8297 {
8298 return 0;
8299 }
8300
8301 static int
8302 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8303 {
8304 return 0;
8305 }
8306
8307 static int
8308 breakpoint_hit_catch_solib (const struct bp_location *bl,
8309 struct address_space *aspace,
8310 CORE_ADDR bp_addr,
8311 const struct target_waitstatus *ws)
8312 {
8313 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8314 struct breakpoint *other;
8315
8316 if (ws->kind == TARGET_WAITKIND_LOADED)
8317 return 1;
8318
8319 ALL_BREAKPOINTS (other)
8320 {
8321 struct bp_location *other_bl;
8322
8323 if (other == bl->owner)
8324 continue;
8325
8326 if (other->type != bp_shlib_event)
8327 continue;
8328
8329 if (self->pspace != NULL && other->pspace != self->pspace)
8330 continue;
8331
8332 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8333 {
8334 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8335 return 1;
8336 }
8337 }
8338
8339 return 0;
8340 }
8341
8342 static void
8343 check_status_catch_solib (struct bpstats *bs)
8344 {
8345 struct solib_catchpoint *self
8346 = (struct solib_catchpoint *) bs->breakpoint_at;
8347 int ix;
8348
8349 if (self->is_load)
8350 {
8351 struct so_list *iter;
8352
8353 for (ix = 0;
8354 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8355 ix, iter);
8356 ++ix)
8357 {
8358 if (!self->regex
8359 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8360 return;
8361 }
8362 }
8363 else
8364 {
8365 char *iter;
8366
8367 for (ix = 0;
8368 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8369 ix, iter);
8370 ++ix)
8371 {
8372 if (!self->regex
8373 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8374 return;
8375 }
8376 }
8377
8378 bs->stop = 0;
8379 bs->print_it = print_it_noop;
8380 }
8381
8382 static enum print_stop_action
8383 print_it_catch_solib (bpstat bs)
8384 {
8385 struct breakpoint *b = bs->breakpoint_at;
8386 struct ui_out *uiout = current_uiout;
8387
8388 annotate_catchpoint (b->number);
8389 maybe_print_thread_hit_breakpoint (uiout);
8390 if (b->disposition == disp_del)
8391 uiout->text ("Temporary catchpoint ");
8392 else
8393 uiout->text ("Catchpoint ");
8394 uiout->field_int ("bkptno", b->number);
8395 uiout->text ("\n");
8396 if (uiout->is_mi_like_p ())
8397 uiout->field_string ("disp", bpdisp_text (b->disposition));
8398 print_solib_event (1);
8399 return PRINT_SRC_AND_LOC;
8400 }
8401
8402 static void
8403 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8404 {
8405 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8406 struct value_print_options opts;
8407 struct ui_out *uiout = current_uiout;
8408 char *msg;
8409
8410 get_user_print_options (&opts);
8411 /* Field 4, the address, is omitted (which makes the columns not
8412 line up too nicely with the headers, but the effect is relatively
8413 readable). */
8414 if (opts.addressprint)
8415 {
8416 annotate_field (4);
8417 uiout->field_skip ("addr");
8418 }
8419
8420 annotate_field (5);
8421 if (self->is_load)
8422 {
8423 if (self->regex)
8424 msg = xstrprintf (_("load of library matching %s"), self->regex);
8425 else
8426 msg = xstrdup (_("load of library"));
8427 }
8428 else
8429 {
8430 if (self->regex)
8431 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8432 else
8433 msg = xstrdup (_("unload of library"));
8434 }
8435 uiout->field_string ("what", msg);
8436 xfree (msg);
8437
8438 if (uiout->is_mi_like_p ())
8439 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8440 }
8441
8442 static void
8443 print_mention_catch_solib (struct breakpoint *b)
8444 {
8445 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8446
8447 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8448 self->is_load ? "load" : "unload");
8449 }
8450
8451 static void
8452 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8453 {
8454 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8455
8456 fprintf_unfiltered (fp, "%s %s",
8457 b->disposition == disp_del ? "tcatch" : "catch",
8458 self->is_load ? "load" : "unload");
8459 if (self->regex)
8460 fprintf_unfiltered (fp, " %s", self->regex);
8461 fprintf_unfiltered (fp, "\n");
8462 }
8463
8464 static struct breakpoint_ops catch_solib_breakpoint_ops;
8465
8466 /* Shared helper function (MI and CLI) for creating and installing
8467 a shared object event catchpoint. If IS_LOAD is non-zero then
8468 the events to be caught are load events, otherwise they are
8469 unload events. If IS_TEMP is non-zero the catchpoint is a
8470 temporary one. If ENABLED is non-zero the catchpoint is
8471 created in an enabled state. */
8472
8473 void
8474 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8475 {
8476 struct solib_catchpoint *c;
8477 struct gdbarch *gdbarch = get_current_arch ();
8478 struct cleanup *cleanup;
8479
8480 if (!arg)
8481 arg = "";
8482 arg = skip_spaces_const (arg);
8483
8484 c = new solib_catchpoint ();
8485 cleanup = make_cleanup (xfree, c);
8486
8487 if (*arg != '\0')
8488 {
8489 int errcode;
8490
8491 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8492 if (errcode != 0)
8493 {
8494 char *err = get_regcomp_error (errcode, &c->compiled);
8495
8496 make_cleanup (xfree, err);
8497 error (_("Invalid regexp (%s): %s"), err, arg);
8498 }
8499 c->regex = xstrdup (arg);
8500 }
8501
8502 c->is_load = is_load;
8503 init_catchpoint (c, gdbarch, is_temp, NULL,
8504 &catch_solib_breakpoint_ops);
8505
8506 c->enable_state = enabled ? bp_enabled : bp_disabled;
8507
8508 discard_cleanups (cleanup);
8509 install_breakpoint (0, c, 1);
8510 }
8511
8512 /* A helper function that does all the work for "catch load" and
8513 "catch unload". */
8514
8515 static void
8516 catch_load_or_unload (char *arg, int from_tty, int is_load,
8517 struct cmd_list_element *command)
8518 {
8519 int tempflag;
8520 const int enabled = 1;
8521
8522 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8523
8524 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8525 }
8526
8527 static void
8528 catch_load_command_1 (char *arg, int from_tty,
8529 struct cmd_list_element *command)
8530 {
8531 catch_load_or_unload (arg, from_tty, 1, command);
8532 }
8533
8534 static void
8535 catch_unload_command_1 (char *arg, int from_tty,
8536 struct cmd_list_element *command)
8537 {
8538 catch_load_or_unload (arg, from_tty, 0, command);
8539 }
8540
8541 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8542 is non-zero, then make the breakpoint temporary. If COND_STRING is
8543 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8544 the breakpoint_ops structure associated to the catchpoint. */
8545
8546 void
8547 init_catchpoint (struct breakpoint *b,
8548 struct gdbarch *gdbarch, int tempflag,
8549 const char *cond_string,
8550 const struct breakpoint_ops *ops)
8551 {
8552 struct symtab_and_line sal;
8553
8554 init_sal (&sal);
8555 sal.pspace = current_program_space;
8556
8557 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8558
8559 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8560 b->disposition = tempflag ? disp_del : disp_donttouch;
8561 }
8562
8563 void
8564 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8565 {
8566 add_to_breakpoint_chain (b);
8567 set_breakpoint_number (internal, b);
8568 if (is_tracepoint (b))
8569 set_tracepoint_count (breakpoint_count);
8570 if (!internal)
8571 mention (b);
8572 observer_notify_breakpoint_created (b);
8573
8574 if (update_gll)
8575 update_global_location_list (UGLL_MAY_INSERT);
8576 }
8577
8578 static void
8579 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8580 int tempflag, const char *cond_string,
8581 const struct breakpoint_ops *ops)
8582 {
8583 struct fork_catchpoint *c = new fork_catchpoint ();
8584
8585 init_catchpoint (c, gdbarch, tempflag, cond_string, ops);
8586
8587 c->forked_inferior_pid = null_ptid;
8588
8589 install_breakpoint (0, c, 1);
8590 }
8591
8592 /* Exec catchpoints. */
8593
8594 /* An instance of this type is used to represent an exec catchpoint.
8595 A breakpoint is really of this type iff its ops pointer points to
8596 CATCH_EXEC_BREAKPOINT_OPS. */
8597
8598 struct exec_catchpoint : public breakpoint
8599 {
8600 ~exec_catchpoint () override;
8601
8602 /* Filename of a program whose exec triggered this catchpoint.
8603 This field is only valid immediately after this catchpoint has
8604 triggered. */
8605 char *exec_pathname;
8606 };
8607
8608 /* Exec catchpoint destructor. */
8609
8610 exec_catchpoint::~exec_catchpoint ()
8611 {
8612 xfree (this->exec_pathname);
8613 }
8614
8615 static int
8616 insert_catch_exec (struct bp_location *bl)
8617 {
8618 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8619 }
8620
8621 static int
8622 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8623 {
8624 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8625 }
8626
8627 static int
8628 breakpoint_hit_catch_exec (const struct bp_location *bl,
8629 struct address_space *aspace, CORE_ADDR bp_addr,
8630 const struct target_waitstatus *ws)
8631 {
8632 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8633
8634 if (ws->kind != TARGET_WAITKIND_EXECD)
8635 return 0;
8636
8637 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8638 return 1;
8639 }
8640
8641 static enum print_stop_action
8642 print_it_catch_exec (bpstat bs)
8643 {
8644 struct ui_out *uiout = current_uiout;
8645 struct breakpoint *b = bs->breakpoint_at;
8646 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8647
8648 annotate_catchpoint (b->number);
8649 maybe_print_thread_hit_breakpoint (uiout);
8650 if (b->disposition == disp_del)
8651 uiout->text ("Temporary catchpoint ");
8652 else
8653 uiout->text ("Catchpoint ");
8654 if (uiout->is_mi_like_p ())
8655 {
8656 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8657 uiout->field_string ("disp", bpdisp_text (b->disposition));
8658 }
8659 uiout->field_int ("bkptno", b->number);
8660 uiout->text (" (exec'd ");
8661 uiout->field_string ("new-exec", c->exec_pathname);
8662 uiout->text ("), ");
8663
8664 return PRINT_SRC_AND_LOC;
8665 }
8666
8667 static void
8668 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8669 {
8670 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8671 struct value_print_options opts;
8672 struct ui_out *uiout = current_uiout;
8673
8674 get_user_print_options (&opts);
8675
8676 /* Field 4, the address, is omitted (which makes the columns
8677 not line up too nicely with the headers, but the effect
8678 is relatively readable). */
8679 if (opts.addressprint)
8680 uiout->field_skip ("addr");
8681 annotate_field (5);
8682 uiout->text ("exec");
8683 if (c->exec_pathname != NULL)
8684 {
8685 uiout->text (", program \"");
8686 uiout->field_string ("what", c->exec_pathname);
8687 uiout->text ("\" ");
8688 }
8689
8690 if (uiout->is_mi_like_p ())
8691 uiout->field_string ("catch-type", "exec");
8692 }
8693
8694 static void
8695 print_mention_catch_exec (struct breakpoint *b)
8696 {
8697 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8698 }
8699
8700 /* Implement the "print_recreate" breakpoint_ops method for exec
8701 catchpoints. */
8702
8703 static void
8704 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8705 {
8706 fprintf_unfiltered (fp, "catch exec");
8707 print_recreate_thread (b, fp);
8708 }
8709
8710 static struct breakpoint_ops catch_exec_breakpoint_ops;
8711
8712 static int
8713 hw_breakpoint_used_count (void)
8714 {
8715 int i = 0;
8716 struct breakpoint *b;
8717 struct bp_location *bl;
8718
8719 ALL_BREAKPOINTS (b)
8720 {
8721 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8722 for (bl = b->loc; bl; bl = bl->next)
8723 {
8724 /* Special types of hardware breakpoints may use more than
8725 one register. */
8726 i += b->ops->resources_needed (bl);
8727 }
8728 }
8729
8730 return i;
8731 }
8732
8733 /* Returns the resources B would use if it were a hardware
8734 watchpoint. */
8735
8736 static int
8737 hw_watchpoint_use_count (struct breakpoint *b)
8738 {
8739 int i = 0;
8740 struct bp_location *bl;
8741
8742 if (!breakpoint_enabled (b))
8743 return 0;
8744
8745 for (bl = b->loc; bl; bl = bl->next)
8746 {
8747 /* Special types of hardware watchpoints may use more than
8748 one register. */
8749 i += b->ops->resources_needed (bl);
8750 }
8751
8752 return i;
8753 }
8754
8755 /* Returns the sum the used resources of all hardware watchpoints of
8756 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8757 the sum of the used resources of all hardware watchpoints of other
8758 types _not_ TYPE. */
8759
8760 static int
8761 hw_watchpoint_used_count_others (struct breakpoint *except,
8762 enum bptype type, int *other_type_used)
8763 {
8764 int i = 0;
8765 struct breakpoint *b;
8766
8767 *other_type_used = 0;
8768 ALL_BREAKPOINTS (b)
8769 {
8770 if (b == except)
8771 continue;
8772 if (!breakpoint_enabled (b))
8773 continue;
8774
8775 if (b->type == type)
8776 i += hw_watchpoint_use_count (b);
8777 else if (is_hardware_watchpoint (b))
8778 *other_type_used = 1;
8779 }
8780
8781 return i;
8782 }
8783
8784 void
8785 disable_watchpoints_before_interactive_call_start (void)
8786 {
8787 struct breakpoint *b;
8788
8789 ALL_BREAKPOINTS (b)
8790 {
8791 if (is_watchpoint (b) && breakpoint_enabled (b))
8792 {
8793 b->enable_state = bp_call_disabled;
8794 update_global_location_list (UGLL_DONT_INSERT);
8795 }
8796 }
8797 }
8798
8799 void
8800 enable_watchpoints_after_interactive_call_stop (void)
8801 {
8802 struct breakpoint *b;
8803
8804 ALL_BREAKPOINTS (b)
8805 {
8806 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8807 {
8808 b->enable_state = bp_enabled;
8809 update_global_location_list (UGLL_MAY_INSERT);
8810 }
8811 }
8812 }
8813
8814 void
8815 disable_breakpoints_before_startup (void)
8816 {
8817 current_program_space->executing_startup = 1;
8818 update_global_location_list (UGLL_DONT_INSERT);
8819 }
8820
8821 void
8822 enable_breakpoints_after_startup (void)
8823 {
8824 current_program_space->executing_startup = 0;
8825 breakpoint_re_set ();
8826 }
8827
8828 /* Create a new single-step breakpoint for thread THREAD, with no
8829 locations. */
8830
8831 static struct breakpoint *
8832 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8833 {
8834 struct breakpoint *b = new breakpoint ();
8835
8836 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8837 &momentary_breakpoint_ops);
8838
8839 b->disposition = disp_donttouch;
8840 b->frame_id = null_frame_id;
8841
8842 b->thread = thread;
8843 gdb_assert (b->thread != 0);
8844
8845 add_to_breakpoint_chain (b);
8846
8847 return b;
8848 }
8849
8850 /* Set a momentary breakpoint of type TYPE at address specified by
8851 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8852 frame. */
8853
8854 struct breakpoint *
8855 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8856 struct frame_id frame_id, enum bptype type)
8857 {
8858 struct breakpoint *b;
8859
8860 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8861 tail-called one. */
8862 gdb_assert (!frame_id_artificial_p (frame_id));
8863
8864 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8865 b->enable_state = bp_enabled;
8866 b->disposition = disp_donttouch;
8867 b->frame_id = frame_id;
8868
8869 /* If we're debugging a multi-threaded program, then we want
8870 momentary breakpoints to be active in only a single thread of
8871 control. */
8872 if (in_thread_list (inferior_ptid))
8873 b->thread = ptid_to_global_thread_id (inferior_ptid);
8874
8875 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8876
8877 return b;
8878 }
8879
8880 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8881 The new breakpoint will have type TYPE, use OPS as its
8882 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8883
8884 static struct breakpoint *
8885 momentary_breakpoint_from_master (struct breakpoint *orig,
8886 enum bptype type,
8887 const struct breakpoint_ops *ops,
8888 int loc_enabled)
8889 {
8890 struct breakpoint *copy;
8891
8892 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8893 copy->loc = allocate_bp_location (copy);
8894 set_breakpoint_location_function (copy->loc, 1);
8895
8896 copy->loc->gdbarch = orig->loc->gdbarch;
8897 copy->loc->requested_address = orig->loc->requested_address;
8898 copy->loc->address = orig->loc->address;
8899 copy->loc->section = orig->loc->section;
8900 copy->loc->pspace = orig->loc->pspace;
8901 copy->loc->probe = orig->loc->probe;
8902 copy->loc->line_number = orig->loc->line_number;
8903 copy->loc->symtab = orig->loc->symtab;
8904 copy->loc->enabled = loc_enabled;
8905 copy->frame_id = orig->frame_id;
8906 copy->thread = orig->thread;
8907 copy->pspace = orig->pspace;
8908
8909 copy->enable_state = bp_enabled;
8910 copy->disposition = disp_donttouch;
8911 copy->number = internal_breakpoint_number--;
8912
8913 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8914 return copy;
8915 }
8916
8917 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8918 ORIG is NULL. */
8919
8920 struct breakpoint *
8921 clone_momentary_breakpoint (struct breakpoint *orig)
8922 {
8923 /* If there's nothing to clone, then return nothing. */
8924 if (orig == NULL)
8925 return NULL;
8926
8927 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8928 }
8929
8930 struct breakpoint *
8931 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8932 enum bptype type)
8933 {
8934 struct symtab_and_line sal;
8935
8936 sal = find_pc_line (pc, 0);
8937 sal.pc = pc;
8938 sal.section = find_pc_overlay (pc);
8939 sal.explicit_pc = 1;
8940
8941 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8942 }
8943 \f
8944
8945 /* Tell the user we have just set a breakpoint B. */
8946
8947 static void
8948 mention (struct breakpoint *b)
8949 {
8950 b->ops->print_mention (b);
8951 if (current_uiout->is_mi_like_p ())
8952 return;
8953 printf_filtered ("\n");
8954 }
8955 \f
8956
8957 static int bp_loc_is_permanent (struct bp_location *loc);
8958
8959 static struct bp_location *
8960 add_location_to_breakpoint (struct breakpoint *b,
8961 const struct symtab_and_line *sal)
8962 {
8963 struct bp_location *loc, **tmp;
8964 CORE_ADDR adjusted_address;
8965 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8966
8967 if (loc_gdbarch == NULL)
8968 loc_gdbarch = b->gdbarch;
8969
8970 /* Adjust the breakpoint's address prior to allocating a location.
8971 Once we call allocate_bp_location(), that mostly uninitialized
8972 location will be placed on the location chain. Adjustment of the
8973 breakpoint may cause target_read_memory() to be called and we do
8974 not want its scan of the location chain to find a breakpoint and
8975 location that's only been partially initialized. */
8976 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8977 sal->pc, b->type);
8978
8979 /* Sort the locations by their ADDRESS. */
8980 loc = allocate_bp_location (b);
8981 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8982 tmp = &((*tmp)->next))
8983 ;
8984 loc->next = *tmp;
8985 *tmp = loc;
8986
8987 loc->requested_address = sal->pc;
8988 loc->address = adjusted_address;
8989 loc->pspace = sal->pspace;
8990 loc->probe.probe = sal->probe;
8991 loc->probe.objfile = sal->objfile;
8992 gdb_assert (loc->pspace != NULL);
8993 loc->section = sal->section;
8994 loc->gdbarch = loc_gdbarch;
8995 loc->line_number = sal->line;
8996 loc->symtab = sal->symtab;
8997
8998 set_breakpoint_location_function (loc,
8999 sal->explicit_pc || sal->explicit_line);
9000
9001 /* While by definition, permanent breakpoints are already present in the
9002 code, we don't mark the location as inserted. Normally one would expect
9003 that GDB could rely on that breakpoint instruction to stop the program,
9004 thus removing the need to insert its own breakpoint, except that executing
9005 the breakpoint instruction can kill the target instead of reporting a
9006 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9007 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9008 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9009 breakpoint be inserted normally results in QEMU knowing about the GDB
9010 breakpoint, and thus trap before the breakpoint instruction is executed.
9011 (If GDB later needs to continue execution past the permanent breakpoint,
9012 it manually increments the PC, thus avoiding executing the breakpoint
9013 instruction.) */
9014 if (bp_loc_is_permanent (loc))
9015 loc->permanent = 1;
9016
9017 return loc;
9018 }
9019 \f
9020
9021 /* See breakpoint.h. */
9022
9023 int
9024 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9025 {
9026 int len;
9027 CORE_ADDR addr;
9028 const gdb_byte *bpoint;
9029 gdb_byte *target_mem;
9030 struct cleanup *cleanup;
9031 int retval = 0;
9032
9033 addr = address;
9034 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9035
9036 /* Software breakpoints unsupported? */
9037 if (bpoint == NULL)
9038 return 0;
9039
9040 target_mem = (gdb_byte *) alloca (len);
9041
9042 /* Enable the automatic memory restoration from breakpoints while
9043 we read the memory. Otherwise we could say about our temporary
9044 breakpoints they are permanent. */
9045 cleanup = make_show_memory_breakpoints_cleanup (0);
9046
9047 if (target_read_memory (address, target_mem, len) == 0
9048 && memcmp (target_mem, bpoint, len) == 0)
9049 retval = 1;
9050
9051 do_cleanups (cleanup);
9052
9053 return retval;
9054 }
9055
9056 /* Return 1 if LOC is pointing to a permanent breakpoint,
9057 return 0 otherwise. */
9058
9059 static int
9060 bp_loc_is_permanent (struct bp_location *loc)
9061 {
9062 gdb_assert (loc != NULL);
9063
9064 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9065 attempt to read from the addresses the locations of these breakpoint types
9066 point to. program_breakpoint_here_p, below, will attempt to read
9067 memory. */
9068 if (!breakpoint_address_is_meaningful (loc->owner))
9069 return 0;
9070
9071 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9072 switch_to_program_space_and_thread (loc->pspace);
9073 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9074 }
9075
9076 /* Build a command list for the dprintf corresponding to the current
9077 settings of the dprintf style options. */
9078
9079 static void
9080 update_dprintf_command_list (struct breakpoint *b)
9081 {
9082 char *dprintf_args = b->extra_string;
9083 char *printf_line = NULL;
9084
9085 if (!dprintf_args)
9086 return;
9087
9088 dprintf_args = skip_spaces (dprintf_args);
9089
9090 /* Allow a comma, as it may have terminated a location, but don't
9091 insist on it. */
9092 if (*dprintf_args == ',')
9093 ++dprintf_args;
9094 dprintf_args = skip_spaces (dprintf_args);
9095
9096 if (*dprintf_args != '"')
9097 error (_("Bad format string, missing '\"'."));
9098
9099 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9100 printf_line = xstrprintf ("printf %s", dprintf_args);
9101 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9102 {
9103 if (!dprintf_function)
9104 error (_("No function supplied for dprintf call"));
9105
9106 if (dprintf_channel && strlen (dprintf_channel) > 0)
9107 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9108 dprintf_function,
9109 dprintf_channel,
9110 dprintf_args);
9111 else
9112 printf_line = xstrprintf ("call (void) %s (%s)",
9113 dprintf_function,
9114 dprintf_args);
9115 }
9116 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9117 {
9118 if (target_can_run_breakpoint_commands ())
9119 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9120 else
9121 {
9122 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9123 printf_line = xstrprintf ("printf %s", dprintf_args);
9124 }
9125 }
9126 else
9127 internal_error (__FILE__, __LINE__,
9128 _("Invalid dprintf style."));
9129
9130 gdb_assert (printf_line != NULL);
9131 /* Manufacture a printf sequence. */
9132 {
9133 struct command_line *printf_cmd_line = XNEW (struct command_line);
9134
9135 printf_cmd_line->control_type = simple_control;
9136 printf_cmd_line->body_count = 0;
9137 printf_cmd_line->body_list = NULL;
9138 printf_cmd_line->next = NULL;
9139 printf_cmd_line->line = printf_line;
9140
9141 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9142 }
9143 }
9144
9145 /* Update all dprintf commands, making their command lists reflect
9146 current style settings. */
9147
9148 static void
9149 update_dprintf_commands (char *args, int from_tty,
9150 struct cmd_list_element *c)
9151 {
9152 struct breakpoint *b;
9153
9154 ALL_BREAKPOINTS (b)
9155 {
9156 if (b->type == bp_dprintf)
9157 update_dprintf_command_list (b);
9158 }
9159 }
9160
9161 /* Create a breakpoint with SAL as location. Use LOCATION
9162 as a description of the location, and COND_STRING
9163 as condition expression. If LOCATION is NULL then create an
9164 "address location" from the address in the SAL. */
9165
9166 static void
9167 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9168 struct symtabs_and_lines sals,
9169 event_location_up &&location,
9170 char *filter, char *cond_string,
9171 char *extra_string,
9172 enum bptype type, enum bpdisp disposition,
9173 int thread, int task, int ignore_count,
9174 const struct breakpoint_ops *ops, int from_tty,
9175 int enabled, int internal, unsigned flags,
9176 int display_canonical)
9177 {
9178 int i;
9179
9180 if (type == bp_hardware_breakpoint)
9181 {
9182 int target_resources_ok;
9183
9184 i = hw_breakpoint_used_count ();
9185 target_resources_ok =
9186 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9187 i + 1, 0);
9188 if (target_resources_ok == 0)
9189 error (_("No hardware breakpoint support in the target."));
9190 else if (target_resources_ok < 0)
9191 error (_("Hardware breakpoints used exceeds limit."));
9192 }
9193
9194 gdb_assert (sals.nelts > 0);
9195
9196 for (i = 0; i < sals.nelts; ++i)
9197 {
9198 struct symtab_and_line sal = sals.sals[i];
9199 struct bp_location *loc;
9200
9201 if (from_tty)
9202 {
9203 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9204 if (!loc_gdbarch)
9205 loc_gdbarch = gdbarch;
9206
9207 describe_other_breakpoints (loc_gdbarch,
9208 sal.pspace, sal.pc, sal.section, thread);
9209 }
9210
9211 if (i == 0)
9212 {
9213 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9214 b->thread = thread;
9215 b->task = task;
9216
9217 b->cond_string = cond_string;
9218 b->extra_string = extra_string;
9219 b->ignore_count = ignore_count;
9220 b->enable_state = enabled ? bp_enabled : bp_disabled;
9221 b->disposition = disposition;
9222
9223 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9224 b->loc->inserted = 1;
9225
9226 if (type == bp_static_tracepoint)
9227 {
9228 struct tracepoint *t = (struct tracepoint *) b;
9229 struct static_tracepoint_marker marker;
9230
9231 if (strace_marker_p (b))
9232 {
9233 /* We already know the marker exists, otherwise, we
9234 wouldn't see a sal for it. */
9235 const char *p
9236 = &event_location_to_string (b->location.get ())[3];
9237 const char *endp;
9238 char *marker_str;
9239
9240 p = skip_spaces_const (p);
9241
9242 endp = skip_to_space_const (p);
9243
9244 marker_str = savestring (p, endp - p);
9245 t->static_trace_marker_id = marker_str;
9246
9247 printf_filtered (_("Probed static tracepoint "
9248 "marker \"%s\"\n"),
9249 t->static_trace_marker_id);
9250 }
9251 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9252 {
9253 t->static_trace_marker_id = xstrdup (marker.str_id);
9254 release_static_tracepoint_marker (&marker);
9255
9256 printf_filtered (_("Probed static tracepoint "
9257 "marker \"%s\"\n"),
9258 t->static_trace_marker_id);
9259 }
9260 else
9261 warning (_("Couldn't determine the static "
9262 "tracepoint marker to probe"));
9263 }
9264
9265 loc = b->loc;
9266 }
9267 else
9268 {
9269 loc = add_location_to_breakpoint (b, &sal);
9270 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9271 loc->inserted = 1;
9272 }
9273
9274 if (b->cond_string)
9275 {
9276 const char *arg = b->cond_string;
9277
9278 loc->cond = parse_exp_1 (&arg, loc->address,
9279 block_for_pc (loc->address), 0);
9280 if (*arg)
9281 error (_("Garbage '%s' follows condition"), arg);
9282 }
9283
9284 /* Dynamic printf requires and uses additional arguments on the
9285 command line, otherwise it's an error. */
9286 if (type == bp_dprintf)
9287 {
9288 if (b->extra_string)
9289 update_dprintf_command_list (b);
9290 else
9291 error (_("Format string required"));
9292 }
9293 else if (b->extra_string)
9294 error (_("Garbage '%s' at end of command"), b->extra_string);
9295 }
9296
9297 b->display_canonical = display_canonical;
9298 if (location != NULL)
9299 b->location = std::move (location);
9300 else
9301 b->location = new_address_location (b->loc->address, NULL, 0);
9302 b->filter = filter;
9303 }
9304
9305 static void
9306 create_breakpoint_sal (struct gdbarch *gdbarch,
9307 struct symtabs_and_lines sals,
9308 event_location_up &&location,
9309 char *filter, char *cond_string,
9310 char *extra_string,
9311 enum bptype type, enum bpdisp disposition,
9312 int thread, int task, int ignore_count,
9313 const struct breakpoint_ops *ops, int from_tty,
9314 int enabled, int internal, unsigned flags,
9315 int display_canonical)
9316 {
9317 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9318
9319 init_breakpoint_sal (b.get (), gdbarch,
9320 sals, std::move (location),
9321 filter, cond_string, extra_string,
9322 type, disposition,
9323 thread, task, ignore_count,
9324 ops, from_tty,
9325 enabled, internal, flags,
9326 display_canonical);
9327
9328 install_breakpoint (internal, b.release (), 0);
9329 }
9330
9331 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9332 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9333 value. COND_STRING, if not NULL, specified the condition to be
9334 used for all breakpoints. Essentially the only case where
9335 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9336 function. In that case, it's still not possible to specify
9337 separate conditions for different overloaded functions, so
9338 we take just a single condition string.
9339
9340 NOTE: If the function succeeds, the caller is expected to cleanup
9341 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9342 array contents). If the function fails (error() is called), the
9343 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9344 COND and SALS arrays and each of those arrays contents. */
9345
9346 static void
9347 create_breakpoints_sal (struct gdbarch *gdbarch,
9348 struct linespec_result *canonical,
9349 char *cond_string, char *extra_string,
9350 enum bptype type, enum bpdisp disposition,
9351 int thread, int task, int ignore_count,
9352 const struct breakpoint_ops *ops, int from_tty,
9353 int enabled, int internal, unsigned flags)
9354 {
9355 int i;
9356 struct linespec_sals *lsal;
9357
9358 if (canonical->pre_expanded)
9359 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9360
9361 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9362 {
9363 /* Note that 'location' can be NULL in the case of a plain
9364 'break', without arguments. */
9365 event_location_up location
9366 = (canonical->location != NULL
9367 ? copy_event_location (canonical->location.get ()) : NULL);
9368 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9369
9370 make_cleanup (xfree, filter_string);
9371 create_breakpoint_sal (gdbarch, lsal->sals,
9372 std::move (location),
9373 filter_string,
9374 cond_string, extra_string,
9375 type, disposition,
9376 thread, task, ignore_count, ops,
9377 from_tty, enabled, internal, flags,
9378 canonical->special_display);
9379 }
9380 }
9381
9382 /* Parse LOCATION which is assumed to be a SAL specification possibly
9383 followed by conditionals. On return, SALS contains an array of SAL
9384 addresses found. LOCATION points to the end of the SAL (for
9385 linespec locations).
9386
9387 The array and the line spec strings are allocated on the heap, it is
9388 the caller's responsibility to free them. */
9389
9390 static void
9391 parse_breakpoint_sals (const struct event_location *location,
9392 struct linespec_result *canonical)
9393 {
9394 struct symtab_and_line cursal;
9395
9396 if (event_location_type (location) == LINESPEC_LOCATION)
9397 {
9398 const char *address = get_linespec_location (location);
9399
9400 if (address == NULL)
9401 {
9402 /* The last displayed codepoint, if it's valid, is our default
9403 breakpoint address. */
9404 if (last_displayed_sal_is_valid ())
9405 {
9406 struct linespec_sals lsal;
9407 struct symtab_and_line sal;
9408 CORE_ADDR pc;
9409
9410 init_sal (&sal); /* Initialize to zeroes. */
9411 lsal.sals.sals = XNEW (struct symtab_and_line);
9412
9413 /* Set sal's pspace, pc, symtab, and line to the values
9414 corresponding to the last call to print_frame_info.
9415 Be sure to reinitialize LINE with NOTCURRENT == 0
9416 as the breakpoint line number is inappropriate otherwise.
9417 find_pc_line would adjust PC, re-set it back. */
9418 get_last_displayed_sal (&sal);
9419 pc = sal.pc;
9420 sal = find_pc_line (pc, 0);
9421
9422 /* "break" without arguments is equivalent to "break *PC"
9423 where PC is the last displayed codepoint's address. So
9424 make sure to set sal.explicit_pc to prevent GDB from
9425 trying to expand the list of sals to include all other
9426 instances with the same symtab and line. */
9427 sal.pc = pc;
9428 sal.explicit_pc = 1;
9429
9430 lsal.sals.sals[0] = sal;
9431 lsal.sals.nelts = 1;
9432 lsal.canonical = NULL;
9433
9434 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9435 return;
9436 }
9437 else
9438 error (_("No default breakpoint address now."));
9439 }
9440 }
9441
9442 /* Force almost all breakpoints to be in terms of the
9443 current_source_symtab (which is decode_line_1's default).
9444 This should produce the results we want almost all of the
9445 time while leaving default_breakpoint_* alone.
9446
9447 ObjC: However, don't match an Objective-C method name which
9448 may have a '+' or '-' succeeded by a '['. */
9449 cursal = get_current_source_symtab_and_line ();
9450 if (last_displayed_sal_is_valid ())
9451 {
9452 const char *address = NULL;
9453
9454 if (event_location_type (location) == LINESPEC_LOCATION)
9455 address = get_linespec_location (location);
9456
9457 if (!cursal.symtab
9458 || (address != NULL
9459 && strchr ("+-", address[0]) != NULL
9460 && address[1] != '['))
9461 {
9462 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9463 get_last_displayed_symtab (),
9464 get_last_displayed_line (),
9465 canonical, NULL, NULL);
9466 return;
9467 }
9468 }
9469
9470 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9471 cursal.symtab, cursal.line, canonical, NULL, NULL);
9472 }
9473
9474
9475 /* Convert each SAL into a real PC. Verify that the PC can be
9476 inserted as a breakpoint. If it can't throw an error. */
9477
9478 static void
9479 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9480 {
9481 int i;
9482
9483 for (i = 0; i < sals->nelts; i++)
9484 resolve_sal_pc (&sals->sals[i]);
9485 }
9486
9487 /* Fast tracepoints may have restrictions on valid locations. For
9488 instance, a fast tracepoint using a jump instead of a trap will
9489 likely have to overwrite more bytes than a trap would, and so can
9490 only be placed where the instruction is longer than the jump, or a
9491 multi-instruction sequence does not have a jump into the middle of
9492 it, etc. */
9493
9494 static void
9495 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9496 struct symtabs_and_lines *sals)
9497 {
9498 int i, rslt;
9499 struct symtab_and_line *sal;
9500 char *msg;
9501 struct cleanup *old_chain;
9502
9503 for (i = 0; i < sals->nelts; i++)
9504 {
9505 struct gdbarch *sarch;
9506
9507 sal = &sals->sals[i];
9508
9509 sarch = get_sal_arch (*sal);
9510 /* We fall back to GDBARCH if there is no architecture
9511 associated with SAL. */
9512 if (sarch == NULL)
9513 sarch = gdbarch;
9514 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9515 old_chain = make_cleanup (xfree, msg);
9516
9517 if (!rslt)
9518 error (_("May not have a fast tracepoint at %s%s"),
9519 paddress (sarch, sal->pc), (msg ? msg : ""));
9520
9521 do_cleanups (old_chain);
9522 }
9523 }
9524
9525 /* Given TOK, a string specification of condition and thread, as
9526 accepted by the 'break' command, extract the condition
9527 string and thread number and set *COND_STRING and *THREAD.
9528 PC identifies the context at which the condition should be parsed.
9529 If no condition is found, *COND_STRING is set to NULL.
9530 If no thread is found, *THREAD is set to -1. */
9531
9532 static void
9533 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9534 char **cond_string, int *thread, int *task,
9535 char **rest)
9536 {
9537 *cond_string = NULL;
9538 *thread = -1;
9539 *task = 0;
9540 *rest = NULL;
9541
9542 while (tok && *tok)
9543 {
9544 const char *end_tok;
9545 int toklen;
9546 const char *cond_start = NULL;
9547 const char *cond_end = NULL;
9548
9549 tok = skip_spaces_const (tok);
9550
9551 if ((*tok == '"' || *tok == ',') && rest)
9552 {
9553 *rest = savestring (tok, strlen (tok));
9554 return;
9555 }
9556
9557 end_tok = skip_to_space_const (tok);
9558
9559 toklen = end_tok - tok;
9560
9561 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9562 {
9563 tok = cond_start = end_tok + 1;
9564 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9565 cond_end = tok;
9566 *cond_string = savestring (cond_start, cond_end - cond_start);
9567 }
9568 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9569 {
9570 const char *tmptok;
9571 struct thread_info *thr;
9572
9573 tok = end_tok + 1;
9574 thr = parse_thread_id (tok, &tmptok);
9575 if (tok == tmptok)
9576 error (_("Junk after thread keyword."));
9577 *thread = thr->global_num;
9578 tok = tmptok;
9579 }
9580 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9581 {
9582 char *tmptok;
9583
9584 tok = end_tok + 1;
9585 *task = strtol (tok, &tmptok, 0);
9586 if (tok == tmptok)
9587 error (_("Junk after task keyword."));
9588 if (!valid_task_id (*task))
9589 error (_("Unknown task %d."), *task);
9590 tok = tmptok;
9591 }
9592 else if (rest)
9593 {
9594 *rest = savestring (tok, strlen (tok));
9595 return;
9596 }
9597 else
9598 error (_("Junk at end of arguments."));
9599 }
9600 }
9601
9602 /* Decode a static tracepoint marker spec. */
9603
9604 static struct symtabs_and_lines
9605 decode_static_tracepoint_spec (const char **arg_p)
9606 {
9607 VEC(static_tracepoint_marker_p) *markers = NULL;
9608 struct symtabs_and_lines sals;
9609 struct cleanup *old_chain;
9610 const char *p = &(*arg_p)[3];
9611 const char *endp;
9612 char *marker_str;
9613 int i;
9614
9615 p = skip_spaces_const (p);
9616
9617 endp = skip_to_space_const (p);
9618
9619 marker_str = savestring (p, endp - p);
9620 old_chain = make_cleanup (xfree, marker_str);
9621
9622 markers = target_static_tracepoint_markers_by_strid (marker_str);
9623 if (VEC_empty(static_tracepoint_marker_p, markers))
9624 error (_("No known static tracepoint marker named %s"), marker_str);
9625
9626 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9627 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9628
9629 for (i = 0; i < sals.nelts; i++)
9630 {
9631 struct static_tracepoint_marker *marker;
9632
9633 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9634
9635 init_sal (&sals.sals[i]);
9636
9637 sals.sals[i] = find_pc_line (marker->address, 0);
9638 sals.sals[i].pc = marker->address;
9639
9640 release_static_tracepoint_marker (marker);
9641 }
9642
9643 do_cleanups (old_chain);
9644
9645 *arg_p = endp;
9646 return sals;
9647 }
9648
9649 /* See breakpoint.h. */
9650
9651 int
9652 create_breakpoint (struct gdbarch *gdbarch,
9653 const struct event_location *location, char *cond_string,
9654 int thread, char *extra_string,
9655 int parse_extra,
9656 int tempflag, enum bptype type_wanted,
9657 int ignore_count,
9658 enum auto_boolean pending_break_support,
9659 const struct breakpoint_ops *ops,
9660 int from_tty, int enabled, int internal,
9661 unsigned flags)
9662 {
9663 struct linespec_result canonical;
9664 struct cleanup *bkpt_chain = NULL;
9665 int pending = 0;
9666 int task = 0;
9667 int prev_bkpt_count = breakpoint_count;
9668
9669 gdb_assert (ops != NULL);
9670
9671 /* If extra_string isn't useful, set it to NULL. */
9672 if (extra_string != NULL && *extra_string == '\0')
9673 extra_string = NULL;
9674
9675 TRY
9676 {
9677 ops->create_sals_from_location (location, &canonical, type_wanted);
9678 }
9679 CATCH (e, RETURN_MASK_ERROR)
9680 {
9681 /* If caller is interested in rc value from parse, set
9682 value. */
9683 if (e.error == NOT_FOUND_ERROR)
9684 {
9685 /* If pending breakpoint support is turned off, throw
9686 error. */
9687
9688 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9689 throw_exception (e);
9690
9691 exception_print (gdb_stderr, e);
9692
9693 /* If pending breakpoint support is auto query and the user
9694 selects no, then simply return the error code. */
9695 if (pending_break_support == AUTO_BOOLEAN_AUTO
9696 && !nquery (_("Make %s pending on future shared library load? "),
9697 bptype_string (type_wanted)))
9698 return 0;
9699
9700 /* At this point, either the user was queried about setting
9701 a pending breakpoint and selected yes, or pending
9702 breakpoint behavior is on and thus a pending breakpoint
9703 is defaulted on behalf of the user. */
9704 pending = 1;
9705 }
9706 else
9707 throw_exception (e);
9708 }
9709 END_CATCH
9710
9711 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9712 return 0;
9713
9714 /* ----------------------------- SNIP -----------------------------
9715 Anything added to the cleanup chain beyond this point is assumed
9716 to be part of a breakpoint. If the breakpoint create succeeds
9717 then the memory is not reclaimed. */
9718 bkpt_chain = make_cleanup (null_cleanup, 0);
9719
9720 /* Resolve all line numbers to PC's and verify that the addresses
9721 are ok for the target. */
9722 if (!pending)
9723 {
9724 int ix;
9725 struct linespec_sals *iter;
9726
9727 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9728 breakpoint_sals_to_pc (&iter->sals);
9729 }
9730
9731 /* Fast tracepoints may have additional restrictions on location. */
9732 if (!pending && type_wanted == bp_fast_tracepoint)
9733 {
9734 int ix;
9735 struct linespec_sals *iter;
9736
9737 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9738 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9739 }
9740
9741 /* Verify that condition can be parsed, before setting any
9742 breakpoints. Allocate a separate condition expression for each
9743 breakpoint. */
9744 if (!pending)
9745 {
9746 if (parse_extra)
9747 {
9748 char *rest;
9749 struct linespec_sals *lsal;
9750
9751 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9752
9753 /* Here we only parse 'arg' to separate condition
9754 from thread number, so parsing in context of first
9755 sal is OK. When setting the breakpoint we'll
9756 re-parse it in context of each sal. */
9757
9758 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9759 &cond_string, &thread, &task, &rest);
9760 if (cond_string)
9761 make_cleanup (xfree, cond_string);
9762 if (rest)
9763 make_cleanup (xfree, rest);
9764 if (rest)
9765 extra_string = rest;
9766 else
9767 extra_string = NULL;
9768 }
9769 else
9770 {
9771 if (type_wanted != bp_dprintf
9772 && extra_string != NULL && *extra_string != '\0')
9773 error (_("Garbage '%s' at end of location"), extra_string);
9774
9775 /* Create a private copy of condition string. */
9776 if (cond_string)
9777 {
9778 cond_string = xstrdup (cond_string);
9779 make_cleanup (xfree, cond_string);
9780 }
9781 /* Create a private copy of any extra string. */
9782 if (extra_string)
9783 {
9784 extra_string = xstrdup (extra_string);
9785 make_cleanup (xfree, extra_string);
9786 }
9787 }
9788
9789 ops->create_breakpoints_sal (gdbarch, &canonical,
9790 cond_string, extra_string, type_wanted,
9791 tempflag ? disp_del : disp_donttouch,
9792 thread, task, ignore_count, ops,
9793 from_tty, enabled, internal, flags);
9794 }
9795 else
9796 {
9797 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9798
9799 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9800 b->location = copy_event_location (location);
9801
9802 if (parse_extra)
9803 b->cond_string = NULL;
9804 else
9805 {
9806 /* Create a private copy of condition string. */
9807 if (cond_string)
9808 {
9809 cond_string = xstrdup (cond_string);
9810 make_cleanup (xfree, cond_string);
9811 }
9812 b->cond_string = cond_string;
9813 b->thread = thread;
9814 }
9815
9816 /* Create a private copy of any extra string. */
9817 if (extra_string != NULL)
9818 {
9819 extra_string = xstrdup (extra_string);
9820 make_cleanup (xfree, extra_string);
9821 }
9822 b->extra_string = extra_string;
9823 b->ignore_count = ignore_count;
9824 b->disposition = tempflag ? disp_del : disp_donttouch;
9825 b->condition_not_parsed = 1;
9826 b->enable_state = enabled ? bp_enabled : bp_disabled;
9827 if ((type_wanted != bp_breakpoint
9828 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9829 b->pspace = current_program_space;
9830
9831 install_breakpoint (internal, b.release (), 0);
9832 }
9833
9834 if (VEC_length (linespec_sals, canonical.sals) > 1)
9835 {
9836 warning (_("Multiple breakpoints were set.\nUse the "
9837 "\"delete\" command to delete unwanted breakpoints."));
9838 prev_breakpoint_count = prev_bkpt_count;
9839 }
9840
9841 /* That's it. Discard the cleanups for data inserted into the
9842 breakpoint. */
9843 discard_cleanups (bkpt_chain);
9844
9845 /* error call may happen here - have BKPT_CHAIN already discarded. */
9846 update_global_location_list (UGLL_MAY_INSERT);
9847
9848 return 1;
9849 }
9850
9851 /* Set a breakpoint.
9852 ARG is a string describing breakpoint address,
9853 condition, and thread.
9854 FLAG specifies if a breakpoint is hardware on,
9855 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9856 and BP_TEMPFLAG. */
9857
9858 static void
9859 break_command_1 (char *arg, int flag, int from_tty)
9860 {
9861 int tempflag = flag & BP_TEMPFLAG;
9862 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9863 ? bp_hardware_breakpoint
9864 : bp_breakpoint);
9865 struct breakpoint_ops *ops;
9866
9867 event_location_up location = string_to_event_location (&arg, current_language);
9868
9869 /* Matching breakpoints on probes. */
9870 if (location != NULL
9871 && event_location_type (location.get ()) == PROBE_LOCATION)
9872 ops = &bkpt_probe_breakpoint_ops;
9873 else
9874 ops = &bkpt_breakpoint_ops;
9875
9876 create_breakpoint (get_current_arch (),
9877 location.get (),
9878 NULL, 0, arg, 1 /* parse arg */,
9879 tempflag, type_wanted,
9880 0 /* Ignore count */,
9881 pending_break_support,
9882 ops,
9883 from_tty,
9884 1 /* enabled */,
9885 0 /* internal */,
9886 0);
9887 }
9888
9889 /* Helper function for break_command_1 and disassemble_command. */
9890
9891 void
9892 resolve_sal_pc (struct symtab_and_line *sal)
9893 {
9894 CORE_ADDR pc;
9895
9896 if (sal->pc == 0 && sal->symtab != NULL)
9897 {
9898 if (!find_line_pc (sal->symtab, sal->line, &pc))
9899 error (_("No line %d in file \"%s\"."),
9900 sal->line, symtab_to_filename_for_display (sal->symtab));
9901 sal->pc = pc;
9902
9903 /* If this SAL corresponds to a breakpoint inserted using a line
9904 number, then skip the function prologue if necessary. */
9905 if (sal->explicit_line)
9906 skip_prologue_sal (sal);
9907 }
9908
9909 if (sal->section == 0 && sal->symtab != NULL)
9910 {
9911 const struct blockvector *bv;
9912 const struct block *b;
9913 struct symbol *sym;
9914
9915 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9916 SYMTAB_COMPUNIT (sal->symtab));
9917 if (bv != NULL)
9918 {
9919 sym = block_linkage_function (b);
9920 if (sym != NULL)
9921 {
9922 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9923 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9924 sym);
9925 }
9926 else
9927 {
9928 /* It really is worthwhile to have the section, so we'll
9929 just have to look harder. This case can be executed
9930 if we have line numbers but no functions (as can
9931 happen in assembly source). */
9932
9933 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9934 switch_to_program_space_and_thread (sal->pspace);
9935
9936 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9937 if (msym.minsym)
9938 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9939 }
9940 }
9941 }
9942 }
9943
9944 void
9945 break_command (char *arg, int from_tty)
9946 {
9947 break_command_1 (arg, 0, from_tty);
9948 }
9949
9950 void
9951 tbreak_command (char *arg, int from_tty)
9952 {
9953 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9954 }
9955
9956 static void
9957 hbreak_command (char *arg, int from_tty)
9958 {
9959 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9960 }
9961
9962 static void
9963 thbreak_command (char *arg, int from_tty)
9964 {
9965 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9966 }
9967
9968 static void
9969 stop_command (char *arg, int from_tty)
9970 {
9971 printf_filtered (_("Specify the type of breakpoint to set.\n\
9972 Usage: stop in <function | address>\n\
9973 stop at <line>\n"));
9974 }
9975
9976 static void
9977 stopin_command (char *arg, int from_tty)
9978 {
9979 int badInput = 0;
9980
9981 if (arg == (char *) NULL)
9982 badInput = 1;
9983 else if (*arg != '*')
9984 {
9985 char *argptr = arg;
9986 int hasColon = 0;
9987
9988 /* Look for a ':'. If this is a line number specification, then
9989 say it is bad, otherwise, it should be an address or
9990 function/method name. */
9991 while (*argptr && !hasColon)
9992 {
9993 hasColon = (*argptr == ':');
9994 argptr++;
9995 }
9996
9997 if (hasColon)
9998 badInput = (*argptr != ':'); /* Not a class::method */
9999 else
10000 badInput = isdigit (*arg); /* a simple line number */
10001 }
10002
10003 if (badInput)
10004 printf_filtered (_("Usage: stop in <function | address>\n"));
10005 else
10006 break_command_1 (arg, 0, from_tty);
10007 }
10008
10009 static void
10010 stopat_command (char *arg, int from_tty)
10011 {
10012 int badInput = 0;
10013
10014 if (arg == (char *) NULL || *arg == '*') /* no line number */
10015 badInput = 1;
10016 else
10017 {
10018 char *argptr = arg;
10019 int hasColon = 0;
10020
10021 /* Look for a ':'. If there is a '::' then get out, otherwise
10022 it is probably a line number. */
10023 while (*argptr && !hasColon)
10024 {
10025 hasColon = (*argptr == ':');
10026 argptr++;
10027 }
10028
10029 if (hasColon)
10030 badInput = (*argptr == ':'); /* we have class::method */
10031 else
10032 badInput = !isdigit (*arg); /* not a line number */
10033 }
10034
10035 if (badInput)
10036 printf_filtered (_("Usage: stop at <line>\n"));
10037 else
10038 break_command_1 (arg, 0, from_tty);
10039 }
10040
10041 /* The dynamic printf command is mostly like a regular breakpoint, but
10042 with a prewired command list consisting of a single output command,
10043 built from extra arguments supplied on the dprintf command
10044 line. */
10045
10046 static void
10047 dprintf_command (char *arg, int from_tty)
10048 {
10049 event_location_up location = string_to_event_location (&arg, current_language);
10050
10051 /* If non-NULL, ARG should have been advanced past the location;
10052 the next character must be ','. */
10053 if (arg != NULL)
10054 {
10055 if (arg[0] != ',' || arg[1] == '\0')
10056 error (_("Format string required"));
10057 else
10058 {
10059 /* Skip the comma. */
10060 ++arg;
10061 }
10062 }
10063
10064 create_breakpoint (get_current_arch (),
10065 location.get (),
10066 NULL, 0, arg, 1 /* parse arg */,
10067 0, bp_dprintf,
10068 0 /* Ignore count */,
10069 pending_break_support,
10070 &dprintf_breakpoint_ops,
10071 from_tty,
10072 1 /* enabled */,
10073 0 /* internal */,
10074 0);
10075 }
10076
10077 static void
10078 agent_printf_command (char *arg, int from_tty)
10079 {
10080 error (_("May only run agent-printf on the target"));
10081 }
10082
10083 /* Implement the "breakpoint_hit" breakpoint_ops method for
10084 ranged breakpoints. */
10085
10086 static int
10087 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10088 struct address_space *aspace,
10089 CORE_ADDR bp_addr,
10090 const struct target_waitstatus *ws)
10091 {
10092 if (ws->kind != TARGET_WAITKIND_STOPPED
10093 || ws->value.sig != GDB_SIGNAL_TRAP)
10094 return 0;
10095
10096 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10097 bl->length, aspace, bp_addr);
10098 }
10099
10100 /* Implement the "resources_needed" breakpoint_ops method for
10101 ranged breakpoints. */
10102
10103 static int
10104 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10105 {
10106 return target_ranged_break_num_registers ();
10107 }
10108
10109 /* Implement the "print_it" breakpoint_ops method for
10110 ranged breakpoints. */
10111
10112 static enum print_stop_action
10113 print_it_ranged_breakpoint (bpstat bs)
10114 {
10115 struct breakpoint *b = bs->breakpoint_at;
10116 struct bp_location *bl = b->loc;
10117 struct ui_out *uiout = current_uiout;
10118
10119 gdb_assert (b->type == bp_hardware_breakpoint);
10120
10121 /* Ranged breakpoints have only one location. */
10122 gdb_assert (bl && bl->next == NULL);
10123
10124 annotate_breakpoint (b->number);
10125
10126 maybe_print_thread_hit_breakpoint (uiout);
10127
10128 if (b->disposition == disp_del)
10129 uiout->text ("Temporary ranged breakpoint ");
10130 else
10131 uiout->text ("Ranged breakpoint ");
10132 if (uiout->is_mi_like_p ())
10133 {
10134 uiout->field_string ("reason",
10135 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10136 uiout->field_string ("disp", bpdisp_text (b->disposition));
10137 }
10138 uiout->field_int ("bkptno", b->number);
10139 uiout->text (", ");
10140
10141 return PRINT_SRC_AND_LOC;
10142 }
10143
10144 /* Implement the "print_one" breakpoint_ops method for
10145 ranged breakpoints. */
10146
10147 static void
10148 print_one_ranged_breakpoint (struct breakpoint *b,
10149 struct bp_location **last_loc)
10150 {
10151 struct bp_location *bl = b->loc;
10152 struct value_print_options opts;
10153 struct ui_out *uiout = current_uiout;
10154
10155 /* Ranged breakpoints have only one location. */
10156 gdb_assert (bl && bl->next == NULL);
10157
10158 get_user_print_options (&opts);
10159
10160 if (opts.addressprint)
10161 /* We don't print the address range here, it will be printed later
10162 by print_one_detail_ranged_breakpoint. */
10163 uiout->field_skip ("addr");
10164 annotate_field (5);
10165 print_breakpoint_location (b, bl);
10166 *last_loc = bl;
10167 }
10168
10169 /* Implement the "print_one_detail" breakpoint_ops method for
10170 ranged breakpoints. */
10171
10172 static void
10173 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10174 struct ui_out *uiout)
10175 {
10176 CORE_ADDR address_start, address_end;
10177 struct bp_location *bl = b->loc;
10178 string_file stb;
10179
10180 gdb_assert (bl);
10181
10182 address_start = bl->address;
10183 address_end = address_start + bl->length - 1;
10184
10185 uiout->text ("\taddress range: ");
10186 stb.printf ("[%s, %s]",
10187 print_core_address (bl->gdbarch, address_start),
10188 print_core_address (bl->gdbarch, address_end));
10189 uiout->field_stream ("addr", stb);
10190 uiout->text ("\n");
10191 }
10192
10193 /* Implement the "print_mention" breakpoint_ops method for
10194 ranged breakpoints. */
10195
10196 static void
10197 print_mention_ranged_breakpoint (struct breakpoint *b)
10198 {
10199 struct bp_location *bl = b->loc;
10200 struct ui_out *uiout = current_uiout;
10201
10202 gdb_assert (bl);
10203 gdb_assert (b->type == bp_hardware_breakpoint);
10204
10205 if (uiout->is_mi_like_p ())
10206 return;
10207
10208 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10209 b->number, paddress (bl->gdbarch, bl->address),
10210 paddress (bl->gdbarch, bl->address + bl->length - 1));
10211 }
10212
10213 /* Implement the "print_recreate" breakpoint_ops method for
10214 ranged breakpoints. */
10215
10216 static void
10217 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10218 {
10219 fprintf_unfiltered (fp, "break-range %s, %s",
10220 event_location_to_string (b->location.get ()),
10221 event_location_to_string (b->location_range_end.get ()));
10222 print_recreate_thread (b, fp);
10223 }
10224
10225 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10226
10227 static struct breakpoint_ops ranged_breakpoint_ops;
10228
10229 /* Find the address where the end of the breakpoint range should be
10230 placed, given the SAL of the end of the range. This is so that if
10231 the user provides a line number, the end of the range is set to the
10232 last instruction of the given line. */
10233
10234 static CORE_ADDR
10235 find_breakpoint_range_end (struct symtab_and_line sal)
10236 {
10237 CORE_ADDR end;
10238
10239 /* If the user provided a PC value, use it. Otherwise,
10240 find the address of the end of the given location. */
10241 if (sal.explicit_pc)
10242 end = sal.pc;
10243 else
10244 {
10245 int ret;
10246 CORE_ADDR start;
10247
10248 ret = find_line_pc_range (sal, &start, &end);
10249 if (!ret)
10250 error (_("Could not find location of the end of the range."));
10251
10252 /* find_line_pc_range returns the start of the next line. */
10253 end--;
10254 }
10255
10256 return end;
10257 }
10258
10259 /* Implement the "break-range" CLI command. */
10260
10261 static void
10262 break_range_command (char *arg, int from_tty)
10263 {
10264 char *arg_start, *addr_string_start;
10265 struct linespec_result canonical_start, canonical_end;
10266 int bp_count, can_use_bp, length;
10267 CORE_ADDR end;
10268 struct breakpoint *b;
10269 struct symtab_and_line sal_start, sal_end;
10270 struct cleanup *cleanup_bkpt;
10271 struct linespec_sals *lsal_start, *lsal_end;
10272
10273 /* We don't support software ranged breakpoints. */
10274 if (target_ranged_break_num_registers () < 0)
10275 error (_("This target does not support hardware ranged breakpoints."));
10276
10277 bp_count = hw_breakpoint_used_count ();
10278 bp_count += target_ranged_break_num_registers ();
10279 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10280 bp_count, 0);
10281 if (can_use_bp < 0)
10282 error (_("Hardware breakpoints used exceeds limit."));
10283
10284 arg = skip_spaces (arg);
10285 if (arg == NULL || arg[0] == '\0')
10286 error(_("No address range specified."));
10287
10288 arg_start = arg;
10289 event_location_up start_location = string_to_event_location (&arg,
10290 current_language);
10291 parse_breakpoint_sals (start_location.get (), &canonical_start);
10292
10293 if (arg[0] != ',')
10294 error (_("Too few arguments."));
10295 else if (VEC_empty (linespec_sals, canonical_start.sals))
10296 error (_("Could not find location of the beginning of the range."));
10297
10298 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10299
10300 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10301 || lsal_start->sals.nelts != 1)
10302 error (_("Cannot create a ranged breakpoint with multiple locations."));
10303
10304 sal_start = lsal_start->sals.sals[0];
10305 addr_string_start = savestring (arg_start, arg - arg_start);
10306 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10307
10308 arg++; /* Skip the comma. */
10309 arg = skip_spaces (arg);
10310
10311 /* Parse the end location. */
10312
10313 arg_start = arg;
10314
10315 /* We call decode_line_full directly here instead of using
10316 parse_breakpoint_sals because we need to specify the start location's
10317 symtab and line as the default symtab and line for the end of the
10318 range. This makes it possible to have ranges like "foo.c:27, +14",
10319 where +14 means 14 lines from the start location. */
10320 event_location_up end_location = string_to_event_location (&arg,
10321 current_language);
10322 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10323 sal_start.symtab, sal_start.line,
10324 &canonical_end, NULL, NULL);
10325
10326 if (VEC_empty (linespec_sals, canonical_end.sals))
10327 error (_("Could not find location of the end of the range."));
10328
10329 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10330 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10331 || lsal_end->sals.nelts != 1)
10332 error (_("Cannot create a ranged breakpoint with multiple locations."));
10333
10334 sal_end = lsal_end->sals.sals[0];
10335
10336 end = find_breakpoint_range_end (sal_end);
10337 if (sal_start.pc > end)
10338 error (_("Invalid address range, end precedes start."));
10339
10340 length = end - sal_start.pc + 1;
10341 if (length < 0)
10342 /* Length overflowed. */
10343 error (_("Address range too large."));
10344 else if (length == 1)
10345 {
10346 /* This range is simple enough to be handled by
10347 the `hbreak' command. */
10348 hbreak_command (addr_string_start, 1);
10349
10350 do_cleanups (cleanup_bkpt);
10351
10352 return;
10353 }
10354
10355 /* Now set up the breakpoint. */
10356 b = set_raw_breakpoint (get_current_arch (), sal_start,
10357 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10358 set_breakpoint_count (breakpoint_count + 1);
10359 b->number = breakpoint_count;
10360 b->disposition = disp_donttouch;
10361 b->location = std::move (start_location);
10362 b->location_range_end = std::move (end_location);
10363 b->loc->length = length;
10364
10365 do_cleanups (cleanup_bkpt);
10366
10367 mention (b);
10368 observer_notify_breakpoint_created (b);
10369 update_global_location_list (UGLL_MAY_INSERT);
10370 }
10371
10372 /* Return non-zero if EXP is verified as constant. Returned zero
10373 means EXP is variable. Also the constant detection may fail for
10374 some constant expressions and in such case still falsely return
10375 zero. */
10376
10377 static int
10378 watchpoint_exp_is_const (const struct expression *exp)
10379 {
10380 int i = exp->nelts;
10381
10382 while (i > 0)
10383 {
10384 int oplenp, argsp;
10385
10386 /* We are only interested in the descriptor of each element. */
10387 operator_length (exp, i, &oplenp, &argsp);
10388 i -= oplenp;
10389
10390 switch (exp->elts[i].opcode)
10391 {
10392 case BINOP_ADD:
10393 case BINOP_SUB:
10394 case BINOP_MUL:
10395 case BINOP_DIV:
10396 case BINOP_REM:
10397 case BINOP_MOD:
10398 case BINOP_LSH:
10399 case BINOP_RSH:
10400 case BINOP_LOGICAL_AND:
10401 case BINOP_LOGICAL_OR:
10402 case BINOP_BITWISE_AND:
10403 case BINOP_BITWISE_IOR:
10404 case BINOP_BITWISE_XOR:
10405 case BINOP_EQUAL:
10406 case BINOP_NOTEQUAL:
10407 case BINOP_LESS:
10408 case BINOP_GTR:
10409 case BINOP_LEQ:
10410 case BINOP_GEQ:
10411 case BINOP_REPEAT:
10412 case BINOP_COMMA:
10413 case BINOP_EXP:
10414 case BINOP_MIN:
10415 case BINOP_MAX:
10416 case BINOP_INTDIV:
10417 case BINOP_CONCAT:
10418 case TERNOP_COND:
10419 case TERNOP_SLICE:
10420
10421 case OP_LONG:
10422 case OP_DOUBLE:
10423 case OP_DECFLOAT:
10424 case OP_LAST:
10425 case OP_COMPLEX:
10426 case OP_STRING:
10427 case OP_ARRAY:
10428 case OP_TYPE:
10429 case OP_TYPEOF:
10430 case OP_DECLTYPE:
10431 case OP_TYPEID:
10432 case OP_NAME:
10433 case OP_OBJC_NSSTRING:
10434
10435 case UNOP_NEG:
10436 case UNOP_LOGICAL_NOT:
10437 case UNOP_COMPLEMENT:
10438 case UNOP_ADDR:
10439 case UNOP_HIGH:
10440 case UNOP_CAST:
10441
10442 case UNOP_CAST_TYPE:
10443 case UNOP_REINTERPRET_CAST:
10444 case UNOP_DYNAMIC_CAST:
10445 /* Unary, binary and ternary operators: We have to check
10446 their operands. If they are constant, then so is the
10447 result of that operation. For instance, if A and B are
10448 determined to be constants, then so is "A + B".
10449
10450 UNOP_IND is one exception to the rule above, because the
10451 value of *ADDR is not necessarily a constant, even when
10452 ADDR is. */
10453 break;
10454
10455 case OP_VAR_VALUE:
10456 /* Check whether the associated symbol is a constant.
10457
10458 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10459 possible that a buggy compiler could mark a variable as
10460 constant even when it is not, and TYPE_CONST would return
10461 true in this case, while SYMBOL_CLASS wouldn't.
10462
10463 We also have to check for function symbols because they
10464 are always constant. */
10465 {
10466 struct symbol *s = exp->elts[i + 2].symbol;
10467
10468 if (SYMBOL_CLASS (s) != LOC_BLOCK
10469 && SYMBOL_CLASS (s) != LOC_CONST
10470 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10471 return 0;
10472 break;
10473 }
10474
10475 /* The default action is to return 0 because we are using
10476 the optimistic approach here: If we don't know something,
10477 then it is not a constant. */
10478 default:
10479 return 0;
10480 }
10481 }
10482
10483 return 1;
10484 }
10485
10486 /* Watchpoint destructor. */
10487
10488 watchpoint::~watchpoint ()
10489 {
10490 xfree (this->exp_string);
10491 xfree (this->exp_string_reparse);
10492 value_free (this->val);
10493 }
10494
10495 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10496
10497 static void
10498 re_set_watchpoint (struct breakpoint *b)
10499 {
10500 struct watchpoint *w = (struct watchpoint *) b;
10501
10502 /* Watchpoint can be either on expression using entirely global
10503 variables, or it can be on local variables.
10504
10505 Watchpoints of the first kind are never auto-deleted, and even
10506 persist across program restarts. Since they can use variables
10507 from shared libraries, we need to reparse expression as libraries
10508 are loaded and unloaded.
10509
10510 Watchpoints on local variables can also change meaning as result
10511 of solib event. For example, if a watchpoint uses both a local
10512 and a global variables in expression, it's a local watchpoint,
10513 but unloading of a shared library will make the expression
10514 invalid. This is not a very common use case, but we still
10515 re-evaluate expression, to avoid surprises to the user.
10516
10517 Note that for local watchpoints, we re-evaluate it only if
10518 watchpoints frame id is still valid. If it's not, it means the
10519 watchpoint is out of scope and will be deleted soon. In fact,
10520 I'm not sure we'll ever be called in this case.
10521
10522 If a local watchpoint's frame id is still valid, then
10523 w->exp_valid_block is likewise valid, and we can safely use it.
10524
10525 Don't do anything about disabled watchpoints, since they will be
10526 reevaluated again when enabled. */
10527 update_watchpoint (w, 1 /* reparse */);
10528 }
10529
10530 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10531
10532 static int
10533 insert_watchpoint (struct bp_location *bl)
10534 {
10535 struct watchpoint *w = (struct watchpoint *) bl->owner;
10536 int length = w->exact ? 1 : bl->length;
10537
10538 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10539 w->cond_exp.get ());
10540 }
10541
10542 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10543
10544 static int
10545 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10546 {
10547 struct watchpoint *w = (struct watchpoint *) bl->owner;
10548 int length = w->exact ? 1 : bl->length;
10549
10550 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10551 w->cond_exp.get ());
10552 }
10553
10554 static int
10555 breakpoint_hit_watchpoint (const struct bp_location *bl,
10556 struct address_space *aspace, CORE_ADDR bp_addr,
10557 const struct target_waitstatus *ws)
10558 {
10559 struct breakpoint *b = bl->owner;
10560 struct watchpoint *w = (struct watchpoint *) b;
10561
10562 /* Continuable hardware watchpoints are treated as non-existent if the
10563 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10564 some data address). Otherwise gdb won't stop on a break instruction
10565 in the code (not from a breakpoint) when a hardware watchpoint has
10566 been defined. Also skip watchpoints which we know did not trigger
10567 (did not match the data address). */
10568 if (is_hardware_watchpoint (b)
10569 && w->watchpoint_triggered == watch_triggered_no)
10570 return 0;
10571
10572 return 1;
10573 }
10574
10575 static void
10576 check_status_watchpoint (bpstat bs)
10577 {
10578 gdb_assert (is_watchpoint (bs->breakpoint_at));
10579
10580 bpstat_check_watchpoint (bs);
10581 }
10582
10583 /* Implement the "resources_needed" breakpoint_ops method for
10584 hardware watchpoints. */
10585
10586 static int
10587 resources_needed_watchpoint (const struct bp_location *bl)
10588 {
10589 struct watchpoint *w = (struct watchpoint *) bl->owner;
10590 int length = w->exact? 1 : bl->length;
10591
10592 return target_region_ok_for_hw_watchpoint (bl->address, length);
10593 }
10594
10595 /* Implement the "works_in_software_mode" breakpoint_ops method for
10596 hardware watchpoints. */
10597
10598 static int
10599 works_in_software_mode_watchpoint (const struct breakpoint *b)
10600 {
10601 /* Read and access watchpoints only work with hardware support. */
10602 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10603 }
10604
10605 static enum print_stop_action
10606 print_it_watchpoint (bpstat bs)
10607 {
10608 struct cleanup *old_chain;
10609 struct breakpoint *b;
10610 enum print_stop_action result;
10611 struct watchpoint *w;
10612 struct ui_out *uiout = current_uiout;
10613
10614 gdb_assert (bs->bp_location_at != NULL);
10615
10616 b = bs->breakpoint_at;
10617 w = (struct watchpoint *) b;
10618
10619 old_chain = make_cleanup (null_cleanup, NULL);
10620
10621 annotate_watchpoint (b->number);
10622 maybe_print_thread_hit_breakpoint (uiout);
10623
10624 string_file stb;
10625
10626 switch (b->type)
10627 {
10628 case bp_watchpoint:
10629 case bp_hardware_watchpoint:
10630 if (uiout->is_mi_like_p ())
10631 uiout->field_string
10632 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10633 mention (b);
10634 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10635 uiout->text ("\nOld value = ");
10636 watchpoint_value_print (bs->old_val, &stb);
10637 uiout->field_stream ("old", stb);
10638 uiout->text ("\nNew value = ");
10639 watchpoint_value_print (w->val, &stb);
10640 uiout->field_stream ("new", stb);
10641 uiout->text ("\n");
10642 /* More than one watchpoint may have been triggered. */
10643 result = PRINT_UNKNOWN;
10644 break;
10645
10646 case bp_read_watchpoint:
10647 if (uiout->is_mi_like_p ())
10648 uiout->field_string
10649 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10650 mention (b);
10651 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10652 uiout->text ("\nValue = ");
10653 watchpoint_value_print (w->val, &stb);
10654 uiout->field_stream ("value", stb);
10655 uiout->text ("\n");
10656 result = PRINT_UNKNOWN;
10657 break;
10658
10659 case bp_access_watchpoint:
10660 if (bs->old_val != NULL)
10661 {
10662 if (uiout->is_mi_like_p ())
10663 uiout->field_string
10664 ("reason",
10665 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10666 mention (b);
10667 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10668 uiout->text ("\nOld value = ");
10669 watchpoint_value_print (bs->old_val, &stb);
10670 uiout->field_stream ("old", stb);
10671 uiout->text ("\nNew value = ");
10672 }
10673 else
10674 {
10675 mention (b);
10676 if (uiout->is_mi_like_p ())
10677 uiout->field_string
10678 ("reason",
10679 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10680 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10681 uiout->text ("\nValue = ");
10682 }
10683 watchpoint_value_print (w->val, &stb);
10684 uiout->field_stream ("new", stb);
10685 uiout->text ("\n");
10686 result = PRINT_UNKNOWN;
10687 break;
10688 default:
10689 result = PRINT_UNKNOWN;
10690 }
10691
10692 do_cleanups (old_chain);
10693 return result;
10694 }
10695
10696 /* Implement the "print_mention" breakpoint_ops method for hardware
10697 watchpoints. */
10698
10699 static void
10700 print_mention_watchpoint (struct breakpoint *b)
10701 {
10702 struct watchpoint *w = (struct watchpoint *) b;
10703 struct ui_out *uiout = current_uiout;
10704 const char *tuple_name;
10705
10706 switch (b->type)
10707 {
10708 case bp_watchpoint:
10709 uiout->text ("Watchpoint ");
10710 tuple_name = "wpt";
10711 break;
10712 case bp_hardware_watchpoint:
10713 uiout->text ("Hardware watchpoint ");
10714 tuple_name = "wpt";
10715 break;
10716 case bp_read_watchpoint:
10717 uiout->text ("Hardware read watchpoint ");
10718 tuple_name = "hw-rwpt";
10719 break;
10720 case bp_access_watchpoint:
10721 uiout->text ("Hardware access (read/write) watchpoint ");
10722 tuple_name = "hw-awpt";
10723 break;
10724 default:
10725 internal_error (__FILE__, __LINE__,
10726 _("Invalid hardware watchpoint type."));
10727 }
10728
10729 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10730 uiout->field_int ("number", b->number);
10731 uiout->text (": ");
10732 uiout->field_string ("exp", w->exp_string);
10733 }
10734
10735 /* Implement the "print_recreate" breakpoint_ops method for
10736 watchpoints. */
10737
10738 static void
10739 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10740 {
10741 struct watchpoint *w = (struct watchpoint *) b;
10742
10743 switch (b->type)
10744 {
10745 case bp_watchpoint:
10746 case bp_hardware_watchpoint:
10747 fprintf_unfiltered (fp, "watch");
10748 break;
10749 case bp_read_watchpoint:
10750 fprintf_unfiltered (fp, "rwatch");
10751 break;
10752 case bp_access_watchpoint:
10753 fprintf_unfiltered (fp, "awatch");
10754 break;
10755 default:
10756 internal_error (__FILE__, __LINE__,
10757 _("Invalid watchpoint type."));
10758 }
10759
10760 fprintf_unfiltered (fp, " %s", w->exp_string);
10761 print_recreate_thread (b, fp);
10762 }
10763
10764 /* Implement the "explains_signal" breakpoint_ops method for
10765 watchpoints. */
10766
10767 static int
10768 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10769 {
10770 /* A software watchpoint cannot cause a signal other than
10771 GDB_SIGNAL_TRAP. */
10772 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10773 return 0;
10774
10775 return 1;
10776 }
10777
10778 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10779
10780 static struct breakpoint_ops watchpoint_breakpoint_ops;
10781
10782 /* Implement the "insert" breakpoint_ops method for
10783 masked hardware watchpoints. */
10784
10785 static int
10786 insert_masked_watchpoint (struct bp_location *bl)
10787 {
10788 struct watchpoint *w = (struct watchpoint *) bl->owner;
10789
10790 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10791 bl->watchpoint_type);
10792 }
10793
10794 /* Implement the "remove" breakpoint_ops method for
10795 masked hardware watchpoints. */
10796
10797 static int
10798 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10799 {
10800 struct watchpoint *w = (struct watchpoint *) bl->owner;
10801
10802 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10803 bl->watchpoint_type);
10804 }
10805
10806 /* Implement the "resources_needed" breakpoint_ops method for
10807 masked hardware watchpoints. */
10808
10809 static int
10810 resources_needed_masked_watchpoint (const struct bp_location *bl)
10811 {
10812 struct watchpoint *w = (struct watchpoint *) bl->owner;
10813
10814 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10815 }
10816
10817 /* Implement the "works_in_software_mode" breakpoint_ops method for
10818 masked hardware watchpoints. */
10819
10820 static int
10821 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10822 {
10823 return 0;
10824 }
10825
10826 /* Implement the "print_it" breakpoint_ops method for
10827 masked hardware watchpoints. */
10828
10829 static enum print_stop_action
10830 print_it_masked_watchpoint (bpstat bs)
10831 {
10832 struct breakpoint *b = bs->breakpoint_at;
10833 struct ui_out *uiout = current_uiout;
10834
10835 /* Masked watchpoints have only one location. */
10836 gdb_assert (b->loc && b->loc->next == NULL);
10837
10838 annotate_watchpoint (b->number);
10839 maybe_print_thread_hit_breakpoint (uiout);
10840
10841 switch (b->type)
10842 {
10843 case bp_hardware_watchpoint:
10844 if (uiout->is_mi_like_p ())
10845 uiout->field_string
10846 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10847 break;
10848
10849 case bp_read_watchpoint:
10850 if (uiout->is_mi_like_p ())
10851 uiout->field_string
10852 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10853 break;
10854
10855 case bp_access_watchpoint:
10856 if (uiout->is_mi_like_p ())
10857 uiout->field_string
10858 ("reason",
10859 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10860 break;
10861 default:
10862 internal_error (__FILE__, __LINE__,
10863 _("Invalid hardware watchpoint type."));
10864 }
10865
10866 mention (b);
10867 uiout->text (_("\n\
10868 Check the underlying instruction at PC for the memory\n\
10869 address and value which triggered this watchpoint.\n"));
10870 uiout->text ("\n");
10871
10872 /* More than one watchpoint may have been triggered. */
10873 return PRINT_UNKNOWN;
10874 }
10875
10876 /* Implement the "print_one_detail" breakpoint_ops method for
10877 masked hardware watchpoints. */
10878
10879 static void
10880 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10881 struct ui_out *uiout)
10882 {
10883 struct watchpoint *w = (struct watchpoint *) b;
10884
10885 /* Masked watchpoints have only one location. */
10886 gdb_assert (b->loc && b->loc->next == NULL);
10887
10888 uiout->text ("\tmask ");
10889 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10890 uiout->text ("\n");
10891 }
10892
10893 /* Implement the "print_mention" breakpoint_ops method for
10894 masked hardware watchpoints. */
10895
10896 static void
10897 print_mention_masked_watchpoint (struct breakpoint *b)
10898 {
10899 struct watchpoint *w = (struct watchpoint *) b;
10900 struct ui_out *uiout = current_uiout;
10901 const char *tuple_name;
10902
10903 switch (b->type)
10904 {
10905 case bp_hardware_watchpoint:
10906 uiout->text ("Masked hardware watchpoint ");
10907 tuple_name = "wpt";
10908 break;
10909 case bp_read_watchpoint:
10910 uiout->text ("Masked hardware read watchpoint ");
10911 tuple_name = "hw-rwpt";
10912 break;
10913 case bp_access_watchpoint:
10914 uiout->text ("Masked hardware access (read/write) watchpoint ");
10915 tuple_name = "hw-awpt";
10916 break;
10917 default:
10918 internal_error (__FILE__, __LINE__,
10919 _("Invalid hardware watchpoint type."));
10920 }
10921
10922 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10923 uiout->field_int ("number", b->number);
10924 uiout->text (": ");
10925 uiout->field_string ("exp", w->exp_string);
10926 }
10927
10928 /* Implement the "print_recreate" breakpoint_ops method for
10929 masked hardware watchpoints. */
10930
10931 static void
10932 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10933 {
10934 struct watchpoint *w = (struct watchpoint *) b;
10935 char tmp[40];
10936
10937 switch (b->type)
10938 {
10939 case bp_hardware_watchpoint:
10940 fprintf_unfiltered (fp, "watch");
10941 break;
10942 case bp_read_watchpoint:
10943 fprintf_unfiltered (fp, "rwatch");
10944 break;
10945 case bp_access_watchpoint:
10946 fprintf_unfiltered (fp, "awatch");
10947 break;
10948 default:
10949 internal_error (__FILE__, __LINE__,
10950 _("Invalid hardware watchpoint type."));
10951 }
10952
10953 sprintf_vma (tmp, w->hw_wp_mask);
10954 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10955 print_recreate_thread (b, fp);
10956 }
10957
10958 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10959
10960 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10961
10962 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10963
10964 static int
10965 is_masked_watchpoint (const struct breakpoint *b)
10966 {
10967 return b->ops == &masked_watchpoint_breakpoint_ops;
10968 }
10969
10970 /* accessflag: hw_write: watch write,
10971 hw_read: watch read,
10972 hw_access: watch access (read or write) */
10973 static void
10974 watch_command_1 (const char *arg, int accessflag, int from_tty,
10975 int just_location, int internal)
10976 {
10977 struct breakpoint *scope_breakpoint = NULL;
10978 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10979 struct value *val, *mark, *result;
10980 int saved_bitpos = 0, saved_bitsize = 0;
10981 const char *exp_start = NULL;
10982 const char *exp_end = NULL;
10983 const char *tok, *end_tok;
10984 int toklen = -1;
10985 const char *cond_start = NULL;
10986 const char *cond_end = NULL;
10987 enum bptype bp_type;
10988 int thread = -1;
10989 int pc = 0;
10990 /* Flag to indicate whether we are going to use masks for
10991 the hardware watchpoint. */
10992 int use_mask = 0;
10993 CORE_ADDR mask = 0;
10994 struct watchpoint *w;
10995 char *expression;
10996 struct cleanup *back_to;
10997
10998 /* Make sure that we actually have parameters to parse. */
10999 if (arg != NULL && arg[0] != '\0')
11000 {
11001 const char *value_start;
11002
11003 exp_end = arg + strlen (arg);
11004
11005 /* Look for "parameter value" pairs at the end
11006 of the arguments string. */
11007 for (tok = exp_end - 1; tok > arg; tok--)
11008 {
11009 /* Skip whitespace at the end of the argument list. */
11010 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11011 tok--;
11012
11013 /* Find the beginning of the last token.
11014 This is the value of the parameter. */
11015 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11016 tok--;
11017 value_start = tok + 1;
11018
11019 /* Skip whitespace. */
11020 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11021 tok--;
11022
11023 end_tok = tok;
11024
11025 /* Find the beginning of the second to last token.
11026 This is the parameter itself. */
11027 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11028 tok--;
11029 tok++;
11030 toklen = end_tok - tok + 1;
11031
11032 if (toklen == 6 && startswith (tok, "thread"))
11033 {
11034 struct thread_info *thr;
11035 /* At this point we've found a "thread" token, which means
11036 the user is trying to set a watchpoint that triggers
11037 only in a specific thread. */
11038 const char *endp;
11039
11040 if (thread != -1)
11041 error(_("You can specify only one thread."));
11042
11043 /* Extract the thread ID from the next token. */
11044 thr = parse_thread_id (value_start, &endp);
11045
11046 /* Check if the user provided a valid thread ID. */
11047 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11048 invalid_thread_id_error (value_start);
11049
11050 thread = thr->global_num;
11051 }
11052 else if (toklen == 4 && startswith (tok, "mask"))
11053 {
11054 /* We've found a "mask" token, which means the user wants to
11055 create a hardware watchpoint that is going to have the mask
11056 facility. */
11057 struct value *mask_value, *mark;
11058
11059 if (use_mask)
11060 error(_("You can specify only one mask."));
11061
11062 use_mask = just_location = 1;
11063
11064 mark = value_mark ();
11065 mask_value = parse_to_comma_and_eval (&value_start);
11066 mask = value_as_address (mask_value);
11067 value_free_to_mark (mark);
11068 }
11069 else
11070 /* We didn't recognize what we found. We should stop here. */
11071 break;
11072
11073 /* Truncate the string and get rid of the "parameter value" pair before
11074 the arguments string is parsed by the parse_exp_1 function. */
11075 exp_end = tok;
11076 }
11077 }
11078 else
11079 exp_end = arg;
11080
11081 /* Parse the rest of the arguments. From here on out, everything
11082 is in terms of a newly allocated string instead of the original
11083 ARG. */
11084 innermost_block = NULL;
11085 expression = savestring (arg, exp_end - arg);
11086 back_to = make_cleanup (xfree, expression);
11087 exp_start = arg = expression;
11088 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11089 exp_end = arg;
11090 /* Remove trailing whitespace from the expression before saving it.
11091 This makes the eventual display of the expression string a bit
11092 prettier. */
11093 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11094 --exp_end;
11095
11096 /* Checking if the expression is not constant. */
11097 if (watchpoint_exp_is_const (exp.get ()))
11098 {
11099 int len;
11100
11101 len = exp_end - exp_start;
11102 while (len > 0 && isspace (exp_start[len - 1]))
11103 len--;
11104 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11105 }
11106
11107 exp_valid_block = innermost_block;
11108 mark = value_mark ();
11109 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11110
11111 if (val != NULL && just_location)
11112 {
11113 saved_bitpos = value_bitpos (val);
11114 saved_bitsize = value_bitsize (val);
11115 }
11116
11117 if (just_location)
11118 {
11119 int ret;
11120
11121 exp_valid_block = NULL;
11122 val = value_addr (result);
11123 release_value (val);
11124 value_free_to_mark (mark);
11125
11126 if (use_mask)
11127 {
11128 ret = target_masked_watch_num_registers (value_as_address (val),
11129 mask);
11130 if (ret == -1)
11131 error (_("This target does not support masked watchpoints."));
11132 else if (ret == -2)
11133 error (_("Invalid mask or memory region."));
11134 }
11135 }
11136 else if (val != NULL)
11137 release_value (val);
11138
11139 tok = skip_spaces_const (arg);
11140 end_tok = skip_to_space_const (tok);
11141
11142 toklen = end_tok - tok;
11143 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11144 {
11145 innermost_block = NULL;
11146 tok = cond_start = end_tok + 1;
11147 parse_exp_1 (&tok, 0, 0, 0);
11148
11149 /* The watchpoint expression may not be local, but the condition
11150 may still be. E.g.: `watch global if local > 0'. */
11151 cond_exp_valid_block = innermost_block;
11152
11153 cond_end = tok;
11154 }
11155 if (*tok)
11156 error (_("Junk at end of command."));
11157
11158 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11159
11160 /* Save this because create_internal_breakpoint below invalidates
11161 'wp_frame'. */
11162 frame_id watchpoint_frame = get_frame_id (wp_frame);
11163
11164 /* If the expression is "local", then set up a "watchpoint scope"
11165 breakpoint at the point where we've left the scope of the watchpoint
11166 expression. Create the scope breakpoint before the watchpoint, so
11167 that we will encounter it first in bpstat_stop_status. */
11168 if (exp_valid_block != NULL && wp_frame != NULL)
11169 {
11170 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11171
11172 if (frame_id_p (caller_frame_id))
11173 {
11174 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11175 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11176
11177 scope_breakpoint
11178 = create_internal_breakpoint (caller_arch, caller_pc,
11179 bp_watchpoint_scope,
11180 &momentary_breakpoint_ops);
11181
11182 /* create_internal_breakpoint could invalidate WP_FRAME. */
11183 wp_frame = NULL;
11184
11185 scope_breakpoint->enable_state = bp_enabled;
11186
11187 /* Automatically delete the breakpoint when it hits. */
11188 scope_breakpoint->disposition = disp_del;
11189
11190 /* Only break in the proper frame (help with recursion). */
11191 scope_breakpoint->frame_id = caller_frame_id;
11192
11193 /* Set the address at which we will stop. */
11194 scope_breakpoint->loc->gdbarch = caller_arch;
11195 scope_breakpoint->loc->requested_address = caller_pc;
11196 scope_breakpoint->loc->address
11197 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11198 scope_breakpoint->loc->requested_address,
11199 scope_breakpoint->type);
11200 }
11201 }
11202
11203 /* Now set up the breakpoint. We create all watchpoints as hardware
11204 watchpoints here even if hardware watchpoints are turned off, a call
11205 to update_watchpoint later in this function will cause the type to
11206 drop back to bp_watchpoint (software watchpoint) if required. */
11207
11208 if (accessflag == hw_read)
11209 bp_type = bp_read_watchpoint;
11210 else if (accessflag == hw_access)
11211 bp_type = bp_access_watchpoint;
11212 else
11213 bp_type = bp_hardware_watchpoint;
11214
11215 w = new watchpoint ();
11216
11217 if (use_mask)
11218 init_raw_breakpoint_without_location (w, NULL, bp_type,
11219 &masked_watchpoint_breakpoint_ops);
11220 else
11221 init_raw_breakpoint_without_location (w, NULL, bp_type,
11222 &watchpoint_breakpoint_ops);
11223 w->thread = thread;
11224 w->disposition = disp_donttouch;
11225 w->pspace = current_program_space;
11226 w->exp = std::move (exp);
11227 w->exp_valid_block = exp_valid_block;
11228 w->cond_exp_valid_block = cond_exp_valid_block;
11229 if (just_location)
11230 {
11231 struct type *t = value_type (val);
11232 CORE_ADDR addr = value_as_address (val);
11233
11234 w->exp_string_reparse
11235 = current_language->la_watch_location_expression (t, addr).release ();
11236
11237 w->exp_string = xstrprintf ("-location %.*s",
11238 (int) (exp_end - exp_start), exp_start);
11239 }
11240 else
11241 w->exp_string = savestring (exp_start, exp_end - exp_start);
11242
11243 if (use_mask)
11244 {
11245 w->hw_wp_mask = mask;
11246 }
11247 else
11248 {
11249 w->val = val;
11250 w->val_bitpos = saved_bitpos;
11251 w->val_bitsize = saved_bitsize;
11252 w->val_valid = 1;
11253 }
11254
11255 if (cond_start)
11256 w->cond_string = savestring (cond_start, cond_end - cond_start);
11257 else
11258 w->cond_string = 0;
11259
11260 if (frame_id_p (watchpoint_frame))
11261 {
11262 w->watchpoint_frame = watchpoint_frame;
11263 w->watchpoint_thread = inferior_ptid;
11264 }
11265 else
11266 {
11267 w->watchpoint_frame = null_frame_id;
11268 w->watchpoint_thread = null_ptid;
11269 }
11270
11271 if (scope_breakpoint != NULL)
11272 {
11273 /* The scope breakpoint is related to the watchpoint. We will
11274 need to act on them together. */
11275 w->related_breakpoint = scope_breakpoint;
11276 scope_breakpoint->related_breakpoint = w;
11277 }
11278
11279 if (!just_location)
11280 value_free_to_mark (mark);
11281
11282 TRY
11283 {
11284 /* Finally update the new watchpoint. This creates the locations
11285 that should be inserted. */
11286 update_watchpoint (w, 1);
11287 }
11288 CATCH (e, RETURN_MASK_ALL)
11289 {
11290 delete_breakpoint (w);
11291 throw_exception (e);
11292 }
11293 END_CATCH
11294
11295 install_breakpoint (internal, w, 1);
11296 do_cleanups (back_to);
11297 }
11298
11299 /* Return count of debug registers needed to watch the given expression.
11300 If the watchpoint cannot be handled in hardware return zero. */
11301
11302 static int
11303 can_use_hardware_watchpoint (struct value *v)
11304 {
11305 int found_memory_cnt = 0;
11306 struct value *head = v;
11307
11308 /* Did the user specifically forbid us to use hardware watchpoints? */
11309 if (!can_use_hw_watchpoints)
11310 return 0;
11311
11312 /* Make sure that the value of the expression depends only upon
11313 memory contents, and values computed from them within GDB. If we
11314 find any register references or function calls, we can't use a
11315 hardware watchpoint.
11316
11317 The idea here is that evaluating an expression generates a series
11318 of values, one holding the value of every subexpression. (The
11319 expression a*b+c has five subexpressions: a, b, a*b, c, and
11320 a*b+c.) GDB's values hold almost enough information to establish
11321 the criteria given above --- they identify memory lvalues,
11322 register lvalues, computed values, etcetera. So we can evaluate
11323 the expression, and then scan the chain of values that leaves
11324 behind to decide whether we can detect any possible change to the
11325 expression's final value using only hardware watchpoints.
11326
11327 However, I don't think that the values returned by inferior
11328 function calls are special in any way. So this function may not
11329 notice that an expression involving an inferior function call
11330 can't be watched with hardware watchpoints. FIXME. */
11331 for (; v; v = value_next (v))
11332 {
11333 if (VALUE_LVAL (v) == lval_memory)
11334 {
11335 if (v != head && value_lazy (v))
11336 /* A lazy memory lvalue in the chain is one that GDB never
11337 needed to fetch; we either just used its address (e.g.,
11338 `a' in `a.b') or we never needed it at all (e.g., `a'
11339 in `a,b'). This doesn't apply to HEAD; if that is
11340 lazy then it was not readable, but watch it anyway. */
11341 ;
11342 else
11343 {
11344 /* Ahh, memory we actually used! Check if we can cover
11345 it with hardware watchpoints. */
11346 struct type *vtype = check_typedef (value_type (v));
11347
11348 /* We only watch structs and arrays if user asked for it
11349 explicitly, never if they just happen to appear in a
11350 middle of some value chain. */
11351 if (v == head
11352 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11353 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11354 {
11355 CORE_ADDR vaddr = value_address (v);
11356 int len;
11357 int num_regs;
11358
11359 len = (target_exact_watchpoints
11360 && is_scalar_type_recursive (vtype))?
11361 1 : TYPE_LENGTH (value_type (v));
11362
11363 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11364 if (!num_regs)
11365 return 0;
11366 else
11367 found_memory_cnt += num_regs;
11368 }
11369 }
11370 }
11371 else if (VALUE_LVAL (v) != not_lval
11372 && deprecated_value_modifiable (v) == 0)
11373 return 0; /* These are values from the history (e.g., $1). */
11374 else if (VALUE_LVAL (v) == lval_register)
11375 return 0; /* Cannot watch a register with a HW watchpoint. */
11376 }
11377
11378 /* The expression itself looks suitable for using a hardware
11379 watchpoint, but give the target machine a chance to reject it. */
11380 return found_memory_cnt;
11381 }
11382
11383 void
11384 watch_command_wrapper (char *arg, int from_tty, int internal)
11385 {
11386 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11387 }
11388
11389 /* A helper function that looks for the "-location" argument and then
11390 calls watch_command_1. */
11391
11392 static void
11393 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11394 {
11395 int just_location = 0;
11396
11397 if (arg
11398 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11399 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11400 {
11401 arg = skip_spaces (arg);
11402 just_location = 1;
11403 }
11404
11405 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11406 }
11407
11408 static void
11409 watch_command (char *arg, int from_tty)
11410 {
11411 watch_maybe_just_location (arg, hw_write, from_tty);
11412 }
11413
11414 void
11415 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11416 {
11417 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11418 }
11419
11420 static void
11421 rwatch_command (char *arg, int from_tty)
11422 {
11423 watch_maybe_just_location (arg, hw_read, from_tty);
11424 }
11425
11426 void
11427 awatch_command_wrapper (char *arg, int from_tty, int internal)
11428 {
11429 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11430 }
11431
11432 static void
11433 awatch_command (char *arg, int from_tty)
11434 {
11435 watch_maybe_just_location (arg, hw_access, from_tty);
11436 }
11437 \f
11438
11439 /* Data for the FSM that manages the until(location)/advance commands
11440 in infcmd.c. Here because it uses the mechanisms of
11441 breakpoints. */
11442
11443 struct until_break_fsm
11444 {
11445 /* The base class. */
11446 struct thread_fsm thread_fsm;
11447
11448 /* The thread that as current when the command was executed. */
11449 int thread;
11450
11451 /* The breakpoint set at the destination location. */
11452 struct breakpoint *location_breakpoint;
11453
11454 /* Breakpoint set at the return address in the caller frame. May be
11455 NULL. */
11456 struct breakpoint *caller_breakpoint;
11457 };
11458
11459 static void until_break_fsm_clean_up (struct thread_fsm *self,
11460 struct thread_info *thread);
11461 static int until_break_fsm_should_stop (struct thread_fsm *self,
11462 struct thread_info *thread);
11463 static enum async_reply_reason
11464 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11465
11466 /* until_break_fsm's vtable. */
11467
11468 static struct thread_fsm_ops until_break_fsm_ops =
11469 {
11470 NULL, /* dtor */
11471 until_break_fsm_clean_up,
11472 until_break_fsm_should_stop,
11473 NULL, /* return_value */
11474 until_break_fsm_async_reply_reason,
11475 };
11476
11477 /* Allocate a new until_break_command_fsm. */
11478
11479 static struct until_break_fsm *
11480 new_until_break_fsm (struct interp *cmd_interp, int thread,
11481 struct breakpoint *location_breakpoint,
11482 struct breakpoint *caller_breakpoint)
11483 {
11484 struct until_break_fsm *sm;
11485
11486 sm = XCNEW (struct until_break_fsm);
11487 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11488
11489 sm->thread = thread;
11490 sm->location_breakpoint = location_breakpoint;
11491 sm->caller_breakpoint = caller_breakpoint;
11492
11493 return sm;
11494 }
11495
11496 /* Implementation of the 'should_stop' FSM method for the
11497 until(location)/advance commands. */
11498
11499 static int
11500 until_break_fsm_should_stop (struct thread_fsm *self,
11501 struct thread_info *tp)
11502 {
11503 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11504
11505 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11506 sm->location_breakpoint) != NULL
11507 || (sm->caller_breakpoint != NULL
11508 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11509 sm->caller_breakpoint) != NULL))
11510 thread_fsm_set_finished (self);
11511
11512 return 1;
11513 }
11514
11515 /* Implementation of the 'clean_up' FSM method for the
11516 until(location)/advance commands. */
11517
11518 static void
11519 until_break_fsm_clean_up (struct thread_fsm *self,
11520 struct thread_info *thread)
11521 {
11522 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11523
11524 /* Clean up our temporary breakpoints. */
11525 if (sm->location_breakpoint != NULL)
11526 {
11527 delete_breakpoint (sm->location_breakpoint);
11528 sm->location_breakpoint = NULL;
11529 }
11530 if (sm->caller_breakpoint != NULL)
11531 {
11532 delete_breakpoint (sm->caller_breakpoint);
11533 sm->caller_breakpoint = NULL;
11534 }
11535 delete_longjmp_breakpoint (sm->thread);
11536 }
11537
11538 /* Implementation of the 'async_reply_reason' FSM method for the
11539 until(location)/advance commands. */
11540
11541 static enum async_reply_reason
11542 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11543 {
11544 return EXEC_ASYNC_LOCATION_REACHED;
11545 }
11546
11547 void
11548 until_break_command (char *arg, int from_tty, int anywhere)
11549 {
11550 struct symtabs_and_lines sals;
11551 struct symtab_and_line sal;
11552 struct frame_info *frame;
11553 struct gdbarch *frame_gdbarch;
11554 struct frame_id stack_frame_id;
11555 struct frame_id caller_frame_id;
11556 struct breakpoint *location_breakpoint;
11557 struct breakpoint *caller_breakpoint = NULL;
11558 struct cleanup *old_chain;
11559 int thread;
11560 struct thread_info *tp;
11561 struct until_break_fsm *sm;
11562
11563 clear_proceed_status (0);
11564
11565 /* Set a breakpoint where the user wants it and at return from
11566 this function. */
11567
11568 event_location_up location = string_to_event_location (&arg, current_language);
11569
11570 if (last_displayed_sal_is_valid ())
11571 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11572 get_last_displayed_symtab (),
11573 get_last_displayed_line ());
11574 else
11575 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11576 NULL, (struct symtab *) NULL, 0);
11577
11578 if (sals.nelts != 1)
11579 error (_("Couldn't get information on specified line."));
11580
11581 sal = sals.sals[0];
11582 xfree (sals.sals); /* malloc'd, so freed. */
11583
11584 if (*arg)
11585 error (_("Junk at end of arguments."));
11586
11587 resolve_sal_pc (&sal);
11588
11589 tp = inferior_thread ();
11590 thread = tp->global_num;
11591
11592 old_chain = make_cleanup (null_cleanup, NULL);
11593
11594 /* Note linespec handling above invalidates the frame chain.
11595 Installing a breakpoint also invalidates the frame chain (as it
11596 may need to switch threads), so do any frame handling before
11597 that. */
11598
11599 frame = get_selected_frame (NULL);
11600 frame_gdbarch = get_frame_arch (frame);
11601 stack_frame_id = get_stack_frame_id (frame);
11602 caller_frame_id = frame_unwind_caller_id (frame);
11603
11604 /* Keep within the current frame, or in frames called by the current
11605 one. */
11606
11607 if (frame_id_p (caller_frame_id))
11608 {
11609 struct symtab_and_line sal2;
11610 struct gdbarch *caller_gdbarch;
11611
11612 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11613 sal2.pc = frame_unwind_caller_pc (frame);
11614 caller_gdbarch = frame_unwind_caller_arch (frame);
11615 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11616 sal2,
11617 caller_frame_id,
11618 bp_until);
11619 make_cleanup_delete_breakpoint (caller_breakpoint);
11620
11621 set_longjmp_breakpoint (tp, caller_frame_id);
11622 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11623 }
11624
11625 /* set_momentary_breakpoint could invalidate FRAME. */
11626 frame = NULL;
11627
11628 if (anywhere)
11629 /* If the user told us to continue until a specified location,
11630 we don't specify a frame at which we need to stop. */
11631 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11632 null_frame_id, bp_until);
11633 else
11634 /* Otherwise, specify the selected frame, because we want to stop
11635 only at the very same frame. */
11636 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11637 stack_frame_id, bp_until);
11638 make_cleanup_delete_breakpoint (location_breakpoint);
11639
11640 sm = new_until_break_fsm (command_interp (), tp->global_num,
11641 location_breakpoint, caller_breakpoint);
11642 tp->thread_fsm = &sm->thread_fsm;
11643
11644 discard_cleanups (old_chain);
11645
11646 proceed (-1, GDB_SIGNAL_DEFAULT);
11647 }
11648
11649 /* This function attempts to parse an optional "if <cond>" clause
11650 from the arg string. If one is not found, it returns NULL.
11651
11652 Else, it returns a pointer to the condition string. (It does not
11653 attempt to evaluate the string against a particular block.) And,
11654 it updates arg to point to the first character following the parsed
11655 if clause in the arg string. */
11656
11657 const char *
11658 ep_parse_optional_if_clause (const char **arg)
11659 {
11660 const char *cond_string;
11661
11662 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11663 return NULL;
11664
11665 /* Skip the "if" keyword. */
11666 (*arg) += 2;
11667
11668 /* Skip any extra leading whitespace, and record the start of the
11669 condition string. */
11670 *arg = skip_spaces_const (*arg);
11671 cond_string = *arg;
11672
11673 /* Assume that the condition occupies the remainder of the arg
11674 string. */
11675 (*arg) += strlen (cond_string);
11676
11677 return cond_string;
11678 }
11679
11680 /* Commands to deal with catching events, such as signals, exceptions,
11681 process start/exit, etc. */
11682
11683 typedef enum
11684 {
11685 catch_fork_temporary, catch_vfork_temporary,
11686 catch_fork_permanent, catch_vfork_permanent
11687 }
11688 catch_fork_kind;
11689
11690 static void
11691 catch_fork_command_1 (char *arg_entry, int from_tty,
11692 struct cmd_list_element *command)
11693 {
11694 const char *arg = arg_entry;
11695 struct gdbarch *gdbarch = get_current_arch ();
11696 const char *cond_string = NULL;
11697 catch_fork_kind fork_kind;
11698 int tempflag;
11699
11700 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11701 tempflag = (fork_kind == catch_fork_temporary
11702 || fork_kind == catch_vfork_temporary);
11703
11704 if (!arg)
11705 arg = "";
11706 arg = skip_spaces_const (arg);
11707
11708 /* The allowed syntax is:
11709 catch [v]fork
11710 catch [v]fork if <cond>
11711
11712 First, check if there's an if clause. */
11713 cond_string = ep_parse_optional_if_clause (&arg);
11714
11715 if ((*arg != '\0') && !isspace (*arg))
11716 error (_("Junk at end of arguments."));
11717
11718 /* If this target supports it, create a fork or vfork catchpoint
11719 and enable reporting of such events. */
11720 switch (fork_kind)
11721 {
11722 case catch_fork_temporary:
11723 case catch_fork_permanent:
11724 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11725 &catch_fork_breakpoint_ops);
11726 break;
11727 case catch_vfork_temporary:
11728 case catch_vfork_permanent:
11729 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11730 &catch_vfork_breakpoint_ops);
11731 break;
11732 default:
11733 error (_("unsupported or unknown fork kind; cannot catch it"));
11734 break;
11735 }
11736 }
11737
11738 static void
11739 catch_exec_command_1 (char *arg_entry, int from_tty,
11740 struct cmd_list_element *command)
11741 {
11742 const char *arg = arg_entry;
11743 struct exec_catchpoint *c;
11744 struct gdbarch *gdbarch = get_current_arch ();
11745 int tempflag;
11746 const char *cond_string = NULL;
11747
11748 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11749
11750 if (!arg)
11751 arg = "";
11752 arg = skip_spaces_const (arg);
11753
11754 /* The allowed syntax is:
11755 catch exec
11756 catch exec if <cond>
11757
11758 First, check if there's an if clause. */
11759 cond_string = ep_parse_optional_if_clause (&arg);
11760
11761 if ((*arg != '\0') && !isspace (*arg))
11762 error (_("Junk at end of arguments."));
11763
11764 c = new exec_catchpoint ();
11765 init_catchpoint (c, gdbarch, tempflag, cond_string,
11766 &catch_exec_breakpoint_ops);
11767 c->exec_pathname = NULL;
11768
11769 install_breakpoint (0, c, 1);
11770 }
11771
11772 void
11773 init_ada_exception_breakpoint (struct breakpoint *b,
11774 struct gdbarch *gdbarch,
11775 struct symtab_and_line sal,
11776 char *addr_string,
11777 const struct breakpoint_ops *ops,
11778 int tempflag,
11779 int enabled,
11780 int from_tty)
11781 {
11782 if (from_tty)
11783 {
11784 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11785 if (!loc_gdbarch)
11786 loc_gdbarch = gdbarch;
11787
11788 describe_other_breakpoints (loc_gdbarch,
11789 sal.pspace, sal.pc, sal.section, -1);
11790 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11791 version for exception catchpoints, because two catchpoints
11792 used for different exception names will use the same address.
11793 In this case, a "breakpoint ... also set at..." warning is
11794 unproductive. Besides, the warning phrasing is also a bit
11795 inappropriate, we should use the word catchpoint, and tell
11796 the user what type of catchpoint it is. The above is good
11797 enough for now, though. */
11798 }
11799
11800 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11801
11802 b->enable_state = enabled ? bp_enabled : bp_disabled;
11803 b->disposition = tempflag ? disp_del : disp_donttouch;
11804 b->location = string_to_event_location (&addr_string,
11805 language_def (language_ada));
11806 b->language = language_ada;
11807 }
11808
11809 static void
11810 catch_command (char *arg, int from_tty)
11811 {
11812 error (_("Catch requires an event name."));
11813 }
11814 \f
11815
11816 static void
11817 tcatch_command (char *arg, int from_tty)
11818 {
11819 error (_("Catch requires an event name."));
11820 }
11821
11822 /* A qsort comparison function that sorts breakpoints in order. */
11823
11824 static int
11825 compare_breakpoints (const void *a, const void *b)
11826 {
11827 const breakpoint_p *ba = (const breakpoint_p *) a;
11828 uintptr_t ua = (uintptr_t) *ba;
11829 const breakpoint_p *bb = (const breakpoint_p *) b;
11830 uintptr_t ub = (uintptr_t) *bb;
11831
11832 if ((*ba)->number < (*bb)->number)
11833 return -1;
11834 else if ((*ba)->number > (*bb)->number)
11835 return 1;
11836
11837 /* Now sort by address, in case we see, e..g, two breakpoints with
11838 the number 0. */
11839 if (ua < ub)
11840 return -1;
11841 return ua > ub ? 1 : 0;
11842 }
11843
11844 /* Delete breakpoints by address or line. */
11845
11846 static void
11847 clear_command (char *arg, int from_tty)
11848 {
11849 struct breakpoint *b, *prev;
11850 VEC(breakpoint_p) *found = 0;
11851 int ix;
11852 int default_match;
11853 struct symtabs_and_lines sals;
11854 struct symtab_and_line sal;
11855 int i;
11856 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11857
11858 if (arg)
11859 {
11860 sals = decode_line_with_current_source (arg,
11861 (DECODE_LINE_FUNFIRSTLINE
11862 | DECODE_LINE_LIST_MODE));
11863 make_cleanup (xfree, sals.sals);
11864 default_match = 0;
11865 }
11866 else
11867 {
11868 sals.sals = XNEW (struct symtab_and_line);
11869 make_cleanup (xfree, sals.sals);
11870 init_sal (&sal); /* Initialize to zeroes. */
11871
11872 /* Set sal's line, symtab, pc, and pspace to the values
11873 corresponding to the last call to print_frame_info. If the
11874 codepoint is not valid, this will set all the fields to 0. */
11875 get_last_displayed_sal (&sal);
11876 if (sal.symtab == 0)
11877 error (_("No source file specified."));
11878
11879 sals.sals[0] = sal;
11880 sals.nelts = 1;
11881
11882 default_match = 1;
11883 }
11884
11885 /* We don't call resolve_sal_pc here. That's not as bad as it
11886 seems, because all existing breakpoints typically have both
11887 file/line and pc set. So, if clear is given file/line, we can
11888 match this to existing breakpoint without obtaining pc at all.
11889
11890 We only support clearing given the address explicitly
11891 present in breakpoint table. Say, we've set breakpoint
11892 at file:line. There were several PC values for that file:line,
11893 due to optimization, all in one block.
11894
11895 We've picked one PC value. If "clear" is issued with another
11896 PC corresponding to the same file:line, the breakpoint won't
11897 be cleared. We probably can still clear the breakpoint, but
11898 since the other PC value is never presented to user, user
11899 can only find it by guessing, and it does not seem important
11900 to support that. */
11901
11902 /* For each line spec given, delete bps which correspond to it. Do
11903 it in two passes, solely to preserve the current behavior that
11904 from_tty is forced true if we delete more than one
11905 breakpoint. */
11906
11907 found = NULL;
11908 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11909 for (i = 0; i < sals.nelts; i++)
11910 {
11911 const char *sal_fullname;
11912
11913 /* If exact pc given, clear bpts at that pc.
11914 If line given (pc == 0), clear all bpts on specified line.
11915 If defaulting, clear all bpts on default line
11916 or at default pc.
11917
11918 defaulting sal.pc != 0 tests to do
11919
11920 0 1 pc
11921 1 1 pc _and_ line
11922 0 0 line
11923 1 0 <can't happen> */
11924
11925 sal = sals.sals[i];
11926 sal_fullname = (sal.symtab == NULL
11927 ? NULL : symtab_to_fullname (sal.symtab));
11928
11929 /* Find all matching breakpoints and add them to 'found'. */
11930 ALL_BREAKPOINTS (b)
11931 {
11932 int match = 0;
11933 /* Are we going to delete b? */
11934 if (b->type != bp_none && !is_watchpoint (b))
11935 {
11936 struct bp_location *loc = b->loc;
11937 for (; loc; loc = loc->next)
11938 {
11939 /* If the user specified file:line, don't allow a PC
11940 match. This matches historical gdb behavior. */
11941 int pc_match = (!sal.explicit_line
11942 && sal.pc
11943 && (loc->pspace == sal.pspace)
11944 && (loc->address == sal.pc)
11945 && (!section_is_overlay (loc->section)
11946 || loc->section == sal.section));
11947 int line_match = 0;
11948
11949 if ((default_match || sal.explicit_line)
11950 && loc->symtab != NULL
11951 && sal_fullname != NULL
11952 && sal.pspace == loc->pspace
11953 && loc->line_number == sal.line
11954 && filename_cmp (symtab_to_fullname (loc->symtab),
11955 sal_fullname) == 0)
11956 line_match = 1;
11957
11958 if (pc_match || line_match)
11959 {
11960 match = 1;
11961 break;
11962 }
11963 }
11964 }
11965
11966 if (match)
11967 VEC_safe_push(breakpoint_p, found, b);
11968 }
11969 }
11970
11971 /* Now go thru the 'found' chain and delete them. */
11972 if (VEC_empty(breakpoint_p, found))
11973 {
11974 if (arg)
11975 error (_("No breakpoint at %s."), arg);
11976 else
11977 error (_("No breakpoint at this line."));
11978 }
11979
11980 /* Remove duplicates from the vec. */
11981 qsort (VEC_address (breakpoint_p, found),
11982 VEC_length (breakpoint_p, found),
11983 sizeof (breakpoint_p),
11984 compare_breakpoints);
11985 prev = VEC_index (breakpoint_p, found, 0);
11986 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11987 {
11988 if (b == prev)
11989 {
11990 VEC_ordered_remove (breakpoint_p, found, ix);
11991 --ix;
11992 }
11993 }
11994
11995 if (VEC_length(breakpoint_p, found) > 1)
11996 from_tty = 1; /* Always report if deleted more than one. */
11997 if (from_tty)
11998 {
11999 if (VEC_length(breakpoint_p, found) == 1)
12000 printf_unfiltered (_("Deleted breakpoint "));
12001 else
12002 printf_unfiltered (_("Deleted breakpoints "));
12003 }
12004
12005 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12006 {
12007 if (from_tty)
12008 printf_unfiltered ("%d ", b->number);
12009 delete_breakpoint (b);
12010 }
12011 if (from_tty)
12012 putchar_unfiltered ('\n');
12013
12014 do_cleanups (cleanups);
12015 }
12016 \f
12017 /* Delete breakpoint in BS if they are `delete' breakpoints and
12018 all breakpoints that are marked for deletion, whether hit or not.
12019 This is called after any breakpoint is hit, or after errors. */
12020
12021 void
12022 breakpoint_auto_delete (bpstat bs)
12023 {
12024 struct breakpoint *b, *b_tmp;
12025
12026 for (; bs; bs = bs->next)
12027 if (bs->breakpoint_at
12028 && bs->breakpoint_at->disposition == disp_del
12029 && bs->stop)
12030 delete_breakpoint (bs->breakpoint_at);
12031
12032 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12033 {
12034 if (b->disposition == disp_del_at_next_stop)
12035 delete_breakpoint (b);
12036 }
12037 }
12038
12039 /* A comparison function for bp_location AP and BP being interfaced to
12040 qsort. Sort elements primarily by their ADDRESS (no matter what
12041 does breakpoint_address_is_meaningful say for its OWNER),
12042 secondarily by ordering first permanent elements and
12043 terciarily just ensuring the array is sorted stable way despite
12044 qsort being an unstable algorithm. */
12045
12046 static int
12047 bp_locations_compare (const void *ap, const void *bp)
12048 {
12049 const struct bp_location *a = *(const struct bp_location **) ap;
12050 const struct bp_location *b = *(const struct bp_location **) bp;
12051
12052 if (a->address != b->address)
12053 return (a->address > b->address) - (a->address < b->address);
12054
12055 /* Sort locations at the same address by their pspace number, keeping
12056 locations of the same inferior (in a multi-inferior environment)
12057 grouped. */
12058
12059 if (a->pspace->num != b->pspace->num)
12060 return ((a->pspace->num > b->pspace->num)
12061 - (a->pspace->num < b->pspace->num));
12062
12063 /* Sort permanent breakpoints first. */
12064 if (a->permanent != b->permanent)
12065 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12066
12067 /* Make the internal GDB representation stable across GDB runs
12068 where A and B memory inside GDB can differ. Breakpoint locations of
12069 the same type at the same address can be sorted in arbitrary order. */
12070
12071 if (a->owner->number != b->owner->number)
12072 return ((a->owner->number > b->owner->number)
12073 - (a->owner->number < b->owner->number));
12074
12075 return (a > b) - (a < b);
12076 }
12077
12078 /* Set bp_locations_placed_address_before_address_max and
12079 bp_locations_shadow_len_after_address_max according to the current
12080 content of the bp_locations array. */
12081
12082 static void
12083 bp_locations_target_extensions_update (void)
12084 {
12085 struct bp_location *bl, **blp_tmp;
12086
12087 bp_locations_placed_address_before_address_max = 0;
12088 bp_locations_shadow_len_after_address_max = 0;
12089
12090 ALL_BP_LOCATIONS (bl, blp_tmp)
12091 {
12092 CORE_ADDR start, end, addr;
12093
12094 if (!bp_location_has_shadow (bl))
12095 continue;
12096
12097 start = bl->target_info.placed_address;
12098 end = start + bl->target_info.shadow_len;
12099
12100 gdb_assert (bl->address >= start);
12101 addr = bl->address - start;
12102 if (addr > bp_locations_placed_address_before_address_max)
12103 bp_locations_placed_address_before_address_max = addr;
12104
12105 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12106
12107 gdb_assert (bl->address < end);
12108 addr = end - bl->address;
12109 if (addr > bp_locations_shadow_len_after_address_max)
12110 bp_locations_shadow_len_after_address_max = addr;
12111 }
12112 }
12113
12114 /* Download tracepoint locations if they haven't been. */
12115
12116 static void
12117 download_tracepoint_locations (void)
12118 {
12119 struct breakpoint *b;
12120 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12121
12122 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12123
12124 ALL_TRACEPOINTS (b)
12125 {
12126 struct bp_location *bl;
12127 struct tracepoint *t;
12128 int bp_location_downloaded = 0;
12129
12130 if ((b->type == bp_fast_tracepoint
12131 ? !may_insert_fast_tracepoints
12132 : !may_insert_tracepoints))
12133 continue;
12134
12135 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12136 {
12137 if (target_can_download_tracepoint ())
12138 can_download_tracepoint = TRIBOOL_TRUE;
12139 else
12140 can_download_tracepoint = TRIBOOL_FALSE;
12141 }
12142
12143 if (can_download_tracepoint == TRIBOOL_FALSE)
12144 break;
12145
12146 for (bl = b->loc; bl; bl = bl->next)
12147 {
12148 /* In tracepoint, locations are _never_ duplicated, so
12149 should_be_inserted is equivalent to
12150 unduplicated_should_be_inserted. */
12151 if (!should_be_inserted (bl) || bl->inserted)
12152 continue;
12153
12154 switch_to_program_space_and_thread (bl->pspace);
12155
12156 target_download_tracepoint (bl);
12157
12158 bl->inserted = 1;
12159 bp_location_downloaded = 1;
12160 }
12161 t = (struct tracepoint *) b;
12162 t->number_on_target = b->number;
12163 if (bp_location_downloaded)
12164 observer_notify_breakpoint_modified (b);
12165 }
12166 }
12167
12168 /* Swap the insertion/duplication state between two locations. */
12169
12170 static void
12171 swap_insertion (struct bp_location *left, struct bp_location *right)
12172 {
12173 const int left_inserted = left->inserted;
12174 const int left_duplicate = left->duplicate;
12175 const int left_needs_update = left->needs_update;
12176 const struct bp_target_info left_target_info = left->target_info;
12177
12178 /* Locations of tracepoints can never be duplicated. */
12179 if (is_tracepoint (left->owner))
12180 gdb_assert (!left->duplicate);
12181 if (is_tracepoint (right->owner))
12182 gdb_assert (!right->duplicate);
12183
12184 left->inserted = right->inserted;
12185 left->duplicate = right->duplicate;
12186 left->needs_update = right->needs_update;
12187 left->target_info = right->target_info;
12188 right->inserted = left_inserted;
12189 right->duplicate = left_duplicate;
12190 right->needs_update = left_needs_update;
12191 right->target_info = left_target_info;
12192 }
12193
12194 /* Force the re-insertion of the locations at ADDRESS. This is called
12195 once a new/deleted/modified duplicate location is found and we are evaluating
12196 conditions on the target's side. Such conditions need to be updated on
12197 the target. */
12198
12199 static void
12200 force_breakpoint_reinsertion (struct bp_location *bl)
12201 {
12202 struct bp_location **locp = NULL, **loc2p;
12203 struct bp_location *loc;
12204 CORE_ADDR address = 0;
12205 int pspace_num;
12206
12207 address = bl->address;
12208 pspace_num = bl->pspace->num;
12209
12210 /* This is only meaningful if the target is
12211 evaluating conditions and if the user has
12212 opted for condition evaluation on the target's
12213 side. */
12214 if (gdb_evaluates_breakpoint_condition_p ()
12215 || !target_supports_evaluation_of_breakpoint_conditions ())
12216 return;
12217
12218 /* Flag all breakpoint locations with this address and
12219 the same program space as the location
12220 as "its condition has changed". We need to
12221 update the conditions on the target's side. */
12222 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12223 {
12224 loc = *loc2p;
12225
12226 if (!is_breakpoint (loc->owner)
12227 || pspace_num != loc->pspace->num)
12228 continue;
12229
12230 /* Flag the location appropriately. We use a different state to
12231 let everyone know that we already updated the set of locations
12232 with addr bl->address and program space bl->pspace. This is so
12233 we don't have to keep calling these functions just to mark locations
12234 that have already been marked. */
12235 loc->condition_changed = condition_updated;
12236
12237 /* Free the agent expression bytecode as well. We will compute
12238 it later on. */
12239 loc->cond_bytecode.reset ();
12240 }
12241 }
12242 /* Called whether new breakpoints are created, or existing breakpoints
12243 deleted, to update the global location list and recompute which
12244 locations are duplicate of which.
12245
12246 The INSERT_MODE flag determines whether locations may not, may, or
12247 shall be inserted now. See 'enum ugll_insert_mode' for more
12248 info. */
12249
12250 static void
12251 update_global_location_list (enum ugll_insert_mode insert_mode)
12252 {
12253 struct breakpoint *b;
12254 struct bp_location **locp, *loc;
12255 struct cleanup *cleanups;
12256 /* Last breakpoint location address that was marked for update. */
12257 CORE_ADDR last_addr = 0;
12258 /* Last breakpoint location program space that was marked for update. */
12259 int last_pspace_num = -1;
12260
12261 /* Used in the duplicates detection below. When iterating over all
12262 bp_locations, points to the first bp_location of a given address.
12263 Breakpoints and watchpoints of different types are never
12264 duplicates of each other. Keep one pointer for each type of
12265 breakpoint/watchpoint, so we only need to loop over all locations
12266 once. */
12267 struct bp_location *bp_loc_first; /* breakpoint */
12268 struct bp_location *wp_loc_first; /* hardware watchpoint */
12269 struct bp_location *awp_loc_first; /* access watchpoint */
12270 struct bp_location *rwp_loc_first; /* read watchpoint */
12271
12272 /* Saved former bp_locations array which we compare against the newly
12273 built bp_locations from the current state of ALL_BREAKPOINTS. */
12274 struct bp_location **old_locations, **old_locp;
12275 unsigned old_locations_count;
12276
12277 old_locations = bp_locations;
12278 old_locations_count = bp_locations_count;
12279 bp_locations = NULL;
12280 bp_locations_count = 0;
12281 cleanups = make_cleanup (xfree, old_locations);
12282
12283 ALL_BREAKPOINTS (b)
12284 for (loc = b->loc; loc; loc = loc->next)
12285 bp_locations_count++;
12286
12287 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12288 locp = bp_locations;
12289 ALL_BREAKPOINTS (b)
12290 for (loc = b->loc; loc; loc = loc->next)
12291 *locp++ = loc;
12292 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12293 bp_locations_compare);
12294
12295 bp_locations_target_extensions_update ();
12296
12297 /* Identify bp_location instances that are no longer present in the
12298 new list, and therefore should be freed. Note that it's not
12299 necessary that those locations should be removed from inferior --
12300 if there's another location at the same address (previously
12301 marked as duplicate), we don't need to remove/insert the
12302 location.
12303
12304 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12305 and former bp_location array state respectively. */
12306
12307 locp = bp_locations;
12308 for (old_locp = old_locations;
12309 old_locp < old_locations + old_locations_count;
12310 old_locp++)
12311 {
12312 struct bp_location *old_loc = *old_locp;
12313 struct bp_location **loc2p;
12314
12315 /* Tells if 'old_loc' is found among the new locations. If
12316 not, we have to free it. */
12317 int found_object = 0;
12318 /* Tells if the location should remain inserted in the target. */
12319 int keep_in_target = 0;
12320 int removed = 0;
12321
12322 /* Skip LOCP entries which will definitely never be needed.
12323 Stop either at or being the one matching OLD_LOC. */
12324 while (locp < bp_locations + bp_locations_count
12325 && (*locp)->address < old_loc->address)
12326 locp++;
12327
12328 for (loc2p = locp;
12329 (loc2p < bp_locations + bp_locations_count
12330 && (*loc2p)->address == old_loc->address);
12331 loc2p++)
12332 {
12333 /* Check if this is a new/duplicated location or a duplicated
12334 location that had its condition modified. If so, we want to send
12335 its condition to the target if evaluation of conditions is taking
12336 place there. */
12337 if ((*loc2p)->condition_changed == condition_modified
12338 && (last_addr != old_loc->address
12339 || last_pspace_num != old_loc->pspace->num))
12340 {
12341 force_breakpoint_reinsertion (*loc2p);
12342 last_pspace_num = old_loc->pspace->num;
12343 }
12344
12345 if (*loc2p == old_loc)
12346 found_object = 1;
12347 }
12348
12349 /* We have already handled this address, update it so that we don't
12350 have to go through updates again. */
12351 last_addr = old_loc->address;
12352
12353 /* Target-side condition evaluation: Handle deleted locations. */
12354 if (!found_object)
12355 force_breakpoint_reinsertion (old_loc);
12356
12357 /* If this location is no longer present, and inserted, look if
12358 there's maybe a new location at the same address. If so,
12359 mark that one inserted, and don't remove this one. This is
12360 needed so that we don't have a time window where a breakpoint
12361 at certain location is not inserted. */
12362
12363 if (old_loc->inserted)
12364 {
12365 /* If the location is inserted now, we might have to remove
12366 it. */
12367
12368 if (found_object && should_be_inserted (old_loc))
12369 {
12370 /* The location is still present in the location list,
12371 and still should be inserted. Don't do anything. */
12372 keep_in_target = 1;
12373 }
12374 else
12375 {
12376 /* This location still exists, but it won't be kept in the
12377 target since it may have been disabled. We proceed to
12378 remove its target-side condition. */
12379
12380 /* The location is either no longer present, or got
12381 disabled. See if there's another location at the
12382 same address, in which case we don't need to remove
12383 this one from the target. */
12384
12385 /* OLD_LOC comes from existing struct breakpoint. */
12386 if (breakpoint_address_is_meaningful (old_loc->owner))
12387 {
12388 for (loc2p = locp;
12389 (loc2p < bp_locations + bp_locations_count
12390 && (*loc2p)->address == old_loc->address);
12391 loc2p++)
12392 {
12393 struct bp_location *loc2 = *loc2p;
12394
12395 if (breakpoint_locations_match (loc2, old_loc))
12396 {
12397 /* Read watchpoint locations are switched to
12398 access watchpoints, if the former are not
12399 supported, but the latter are. */
12400 if (is_hardware_watchpoint (old_loc->owner))
12401 {
12402 gdb_assert (is_hardware_watchpoint (loc2->owner));
12403 loc2->watchpoint_type = old_loc->watchpoint_type;
12404 }
12405
12406 /* loc2 is a duplicated location. We need to check
12407 if it should be inserted in case it will be
12408 unduplicated. */
12409 if (loc2 != old_loc
12410 && unduplicated_should_be_inserted (loc2))
12411 {
12412 swap_insertion (old_loc, loc2);
12413 keep_in_target = 1;
12414 break;
12415 }
12416 }
12417 }
12418 }
12419 }
12420
12421 if (!keep_in_target)
12422 {
12423 if (remove_breakpoint (old_loc))
12424 {
12425 /* This is just about all we can do. We could keep
12426 this location on the global list, and try to
12427 remove it next time, but there's no particular
12428 reason why we will succeed next time.
12429
12430 Note that at this point, old_loc->owner is still
12431 valid, as delete_breakpoint frees the breakpoint
12432 only after calling us. */
12433 printf_filtered (_("warning: Error removing "
12434 "breakpoint %d\n"),
12435 old_loc->owner->number);
12436 }
12437 removed = 1;
12438 }
12439 }
12440
12441 if (!found_object)
12442 {
12443 if (removed && target_is_non_stop_p ()
12444 && need_moribund_for_location_type (old_loc))
12445 {
12446 /* This location was removed from the target. In
12447 non-stop mode, a race condition is possible where
12448 we've removed a breakpoint, but stop events for that
12449 breakpoint are already queued and will arrive later.
12450 We apply an heuristic to be able to distinguish such
12451 SIGTRAPs from other random SIGTRAPs: we keep this
12452 breakpoint location for a bit, and will retire it
12453 after we see some number of events. The theory here
12454 is that reporting of events should, "on the average",
12455 be fair, so after a while we'll see events from all
12456 threads that have anything of interest, and no longer
12457 need to keep this breakpoint location around. We
12458 don't hold locations forever so to reduce chances of
12459 mistaking a non-breakpoint SIGTRAP for a breakpoint
12460 SIGTRAP.
12461
12462 The heuristic failing can be disastrous on
12463 decr_pc_after_break targets.
12464
12465 On decr_pc_after_break targets, like e.g., x86-linux,
12466 if we fail to recognize a late breakpoint SIGTRAP,
12467 because events_till_retirement has reached 0 too
12468 soon, we'll fail to do the PC adjustment, and report
12469 a random SIGTRAP to the user. When the user resumes
12470 the inferior, it will most likely immediately crash
12471 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12472 corrupted, because of being resumed e.g., in the
12473 middle of a multi-byte instruction, or skipped a
12474 one-byte instruction. This was actually seen happen
12475 on native x86-linux, and should be less rare on
12476 targets that do not support new thread events, like
12477 remote, due to the heuristic depending on
12478 thread_count.
12479
12480 Mistaking a random SIGTRAP for a breakpoint trap
12481 causes similar symptoms (PC adjustment applied when
12482 it shouldn't), but then again, playing with SIGTRAPs
12483 behind the debugger's back is asking for trouble.
12484
12485 Since hardware watchpoint traps are always
12486 distinguishable from other traps, so we don't need to
12487 apply keep hardware watchpoint moribund locations
12488 around. We simply always ignore hardware watchpoint
12489 traps we can no longer explain. */
12490
12491 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12492 old_loc->owner = NULL;
12493
12494 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12495 }
12496 else
12497 {
12498 old_loc->owner = NULL;
12499 decref_bp_location (&old_loc);
12500 }
12501 }
12502 }
12503
12504 /* Rescan breakpoints at the same address and section, marking the
12505 first one as "first" and any others as "duplicates". This is so
12506 that the bpt instruction is only inserted once. If we have a
12507 permanent breakpoint at the same place as BPT, make that one the
12508 official one, and the rest as duplicates. Permanent breakpoints
12509 are sorted first for the same address.
12510
12511 Do the same for hardware watchpoints, but also considering the
12512 watchpoint's type (regular/access/read) and length. */
12513
12514 bp_loc_first = NULL;
12515 wp_loc_first = NULL;
12516 awp_loc_first = NULL;
12517 rwp_loc_first = NULL;
12518 ALL_BP_LOCATIONS (loc, locp)
12519 {
12520 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12521 non-NULL. */
12522 struct bp_location **loc_first_p;
12523 b = loc->owner;
12524
12525 if (!unduplicated_should_be_inserted (loc)
12526 || !breakpoint_address_is_meaningful (b)
12527 /* Don't detect duplicate for tracepoint locations because they are
12528 never duplicated. See the comments in field `duplicate' of
12529 `struct bp_location'. */
12530 || is_tracepoint (b))
12531 {
12532 /* Clear the condition modification flag. */
12533 loc->condition_changed = condition_unchanged;
12534 continue;
12535 }
12536
12537 if (b->type == bp_hardware_watchpoint)
12538 loc_first_p = &wp_loc_first;
12539 else if (b->type == bp_read_watchpoint)
12540 loc_first_p = &rwp_loc_first;
12541 else if (b->type == bp_access_watchpoint)
12542 loc_first_p = &awp_loc_first;
12543 else
12544 loc_first_p = &bp_loc_first;
12545
12546 if (*loc_first_p == NULL
12547 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12548 || !breakpoint_locations_match (loc, *loc_first_p))
12549 {
12550 *loc_first_p = loc;
12551 loc->duplicate = 0;
12552
12553 if (is_breakpoint (loc->owner) && loc->condition_changed)
12554 {
12555 loc->needs_update = 1;
12556 /* Clear the condition modification flag. */
12557 loc->condition_changed = condition_unchanged;
12558 }
12559 continue;
12560 }
12561
12562
12563 /* This and the above ensure the invariant that the first location
12564 is not duplicated, and is the inserted one.
12565 All following are marked as duplicated, and are not inserted. */
12566 if (loc->inserted)
12567 swap_insertion (loc, *loc_first_p);
12568 loc->duplicate = 1;
12569
12570 /* Clear the condition modification flag. */
12571 loc->condition_changed = condition_unchanged;
12572 }
12573
12574 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12575 {
12576 if (insert_mode != UGLL_DONT_INSERT)
12577 insert_breakpoint_locations ();
12578 else
12579 {
12580 /* Even though the caller told us to not insert new
12581 locations, we may still need to update conditions on the
12582 target's side of breakpoints that were already inserted
12583 if the target is evaluating breakpoint conditions. We
12584 only update conditions for locations that are marked
12585 "needs_update". */
12586 update_inserted_breakpoint_locations ();
12587 }
12588 }
12589
12590 if (insert_mode != UGLL_DONT_INSERT)
12591 download_tracepoint_locations ();
12592
12593 do_cleanups (cleanups);
12594 }
12595
12596 void
12597 breakpoint_retire_moribund (void)
12598 {
12599 struct bp_location *loc;
12600 int ix;
12601
12602 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12603 if (--(loc->events_till_retirement) == 0)
12604 {
12605 decref_bp_location (&loc);
12606 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12607 --ix;
12608 }
12609 }
12610
12611 static void
12612 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12613 {
12614
12615 TRY
12616 {
12617 update_global_location_list (insert_mode);
12618 }
12619 CATCH (e, RETURN_MASK_ERROR)
12620 {
12621 }
12622 END_CATCH
12623 }
12624
12625 /* Clear BKP from a BPS. */
12626
12627 static void
12628 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12629 {
12630 bpstat bs;
12631
12632 for (bs = bps; bs; bs = bs->next)
12633 if (bs->breakpoint_at == bpt)
12634 {
12635 bs->breakpoint_at = NULL;
12636 bs->old_val = NULL;
12637 /* bs->commands will be freed later. */
12638 }
12639 }
12640
12641 /* Callback for iterate_over_threads. */
12642 static int
12643 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12644 {
12645 struct breakpoint *bpt = (struct breakpoint *) data;
12646
12647 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12648 return 0;
12649 }
12650
12651 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12652 callbacks. */
12653
12654 static void
12655 say_where (struct breakpoint *b)
12656 {
12657 struct value_print_options opts;
12658
12659 get_user_print_options (&opts);
12660
12661 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12662 single string. */
12663 if (b->loc == NULL)
12664 {
12665 /* For pending locations, the output differs slightly based
12666 on b->extra_string. If this is non-NULL, it contains either
12667 a condition or dprintf arguments. */
12668 if (b->extra_string == NULL)
12669 {
12670 printf_filtered (_(" (%s) pending."),
12671 event_location_to_string (b->location.get ()));
12672 }
12673 else if (b->type == bp_dprintf)
12674 {
12675 printf_filtered (_(" (%s,%s) pending."),
12676 event_location_to_string (b->location.get ()),
12677 b->extra_string);
12678 }
12679 else
12680 {
12681 printf_filtered (_(" (%s %s) pending."),
12682 event_location_to_string (b->location.get ()),
12683 b->extra_string);
12684 }
12685 }
12686 else
12687 {
12688 if (opts.addressprint || b->loc->symtab == NULL)
12689 {
12690 printf_filtered (" at ");
12691 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12692 gdb_stdout);
12693 }
12694 if (b->loc->symtab != NULL)
12695 {
12696 /* If there is a single location, we can print the location
12697 more nicely. */
12698 if (b->loc->next == NULL)
12699 printf_filtered (": file %s, line %d.",
12700 symtab_to_filename_for_display (b->loc->symtab),
12701 b->loc->line_number);
12702 else
12703 /* This is not ideal, but each location may have a
12704 different file name, and this at least reflects the
12705 real situation somewhat. */
12706 printf_filtered (": %s.",
12707 event_location_to_string (b->location.get ()));
12708 }
12709
12710 if (b->loc->next)
12711 {
12712 struct bp_location *loc = b->loc;
12713 int n = 0;
12714 for (; loc; loc = loc->next)
12715 ++n;
12716 printf_filtered (" (%d locations)", n);
12717 }
12718 }
12719 }
12720
12721 /* Default bp_location_ops methods. */
12722
12723 static void
12724 bp_location_dtor (struct bp_location *self)
12725 {
12726 xfree (self->function_name);
12727 }
12728
12729 static const struct bp_location_ops bp_location_ops =
12730 {
12731 bp_location_dtor
12732 };
12733
12734 /* Destructor for the breakpoint base class. */
12735
12736 breakpoint::~breakpoint ()
12737 {
12738 decref_counted_command_line (&this->commands);
12739 xfree (this->cond_string);
12740 xfree (this->extra_string);
12741 xfree (this->filter);
12742 }
12743
12744 static struct bp_location *
12745 base_breakpoint_allocate_location (struct breakpoint *self)
12746 {
12747 return new bp_location (&bp_location_ops, self);
12748 }
12749
12750 static void
12751 base_breakpoint_re_set (struct breakpoint *b)
12752 {
12753 /* Nothing to re-set. */
12754 }
12755
12756 #define internal_error_pure_virtual_called() \
12757 gdb_assert_not_reached ("pure virtual function called")
12758
12759 static int
12760 base_breakpoint_insert_location (struct bp_location *bl)
12761 {
12762 internal_error_pure_virtual_called ();
12763 }
12764
12765 static int
12766 base_breakpoint_remove_location (struct bp_location *bl,
12767 enum remove_bp_reason reason)
12768 {
12769 internal_error_pure_virtual_called ();
12770 }
12771
12772 static int
12773 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12774 struct address_space *aspace,
12775 CORE_ADDR bp_addr,
12776 const struct target_waitstatus *ws)
12777 {
12778 internal_error_pure_virtual_called ();
12779 }
12780
12781 static void
12782 base_breakpoint_check_status (bpstat bs)
12783 {
12784 /* Always stop. */
12785 }
12786
12787 /* A "works_in_software_mode" breakpoint_ops method that just internal
12788 errors. */
12789
12790 static int
12791 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12792 {
12793 internal_error_pure_virtual_called ();
12794 }
12795
12796 /* A "resources_needed" breakpoint_ops method that just internal
12797 errors. */
12798
12799 static int
12800 base_breakpoint_resources_needed (const struct bp_location *bl)
12801 {
12802 internal_error_pure_virtual_called ();
12803 }
12804
12805 static enum print_stop_action
12806 base_breakpoint_print_it (bpstat bs)
12807 {
12808 internal_error_pure_virtual_called ();
12809 }
12810
12811 static void
12812 base_breakpoint_print_one_detail (const struct breakpoint *self,
12813 struct ui_out *uiout)
12814 {
12815 /* nothing */
12816 }
12817
12818 static void
12819 base_breakpoint_print_mention (struct breakpoint *b)
12820 {
12821 internal_error_pure_virtual_called ();
12822 }
12823
12824 static void
12825 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12826 {
12827 internal_error_pure_virtual_called ();
12828 }
12829
12830 static void
12831 base_breakpoint_create_sals_from_location
12832 (const struct event_location *location,
12833 struct linespec_result *canonical,
12834 enum bptype type_wanted)
12835 {
12836 internal_error_pure_virtual_called ();
12837 }
12838
12839 static void
12840 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12841 struct linespec_result *c,
12842 char *cond_string,
12843 char *extra_string,
12844 enum bptype type_wanted,
12845 enum bpdisp disposition,
12846 int thread,
12847 int task, int ignore_count,
12848 const struct breakpoint_ops *o,
12849 int from_tty, int enabled,
12850 int internal, unsigned flags)
12851 {
12852 internal_error_pure_virtual_called ();
12853 }
12854
12855 static void
12856 base_breakpoint_decode_location (struct breakpoint *b,
12857 const struct event_location *location,
12858 struct program_space *search_pspace,
12859 struct symtabs_and_lines *sals)
12860 {
12861 internal_error_pure_virtual_called ();
12862 }
12863
12864 /* The default 'explains_signal' method. */
12865
12866 static int
12867 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12868 {
12869 return 1;
12870 }
12871
12872 /* The default "after_condition_true" method. */
12873
12874 static void
12875 base_breakpoint_after_condition_true (struct bpstats *bs)
12876 {
12877 /* Nothing to do. */
12878 }
12879
12880 struct breakpoint_ops base_breakpoint_ops =
12881 {
12882 base_breakpoint_allocate_location,
12883 base_breakpoint_re_set,
12884 base_breakpoint_insert_location,
12885 base_breakpoint_remove_location,
12886 base_breakpoint_breakpoint_hit,
12887 base_breakpoint_check_status,
12888 base_breakpoint_resources_needed,
12889 base_breakpoint_works_in_software_mode,
12890 base_breakpoint_print_it,
12891 NULL,
12892 base_breakpoint_print_one_detail,
12893 base_breakpoint_print_mention,
12894 base_breakpoint_print_recreate,
12895 base_breakpoint_create_sals_from_location,
12896 base_breakpoint_create_breakpoints_sal,
12897 base_breakpoint_decode_location,
12898 base_breakpoint_explains_signal,
12899 base_breakpoint_after_condition_true,
12900 };
12901
12902 /* Default breakpoint_ops methods. */
12903
12904 static void
12905 bkpt_re_set (struct breakpoint *b)
12906 {
12907 /* FIXME: is this still reachable? */
12908 if (breakpoint_event_location_empty_p (b))
12909 {
12910 /* Anything without a location can't be re-set. */
12911 delete_breakpoint (b);
12912 return;
12913 }
12914
12915 breakpoint_re_set_default (b);
12916 }
12917
12918 static int
12919 bkpt_insert_location (struct bp_location *bl)
12920 {
12921 CORE_ADDR addr = bl->target_info.reqstd_address;
12922
12923 bl->target_info.kind = breakpoint_kind (bl, &addr);
12924 bl->target_info.placed_address = addr;
12925
12926 if (bl->loc_type == bp_loc_hardware_breakpoint)
12927 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12928 else
12929 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12930 }
12931
12932 static int
12933 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12934 {
12935 if (bl->loc_type == bp_loc_hardware_breakpoint)
12936 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12937 else
12938 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12939 }
12940
12941 static int
12942 bkpt_breakpoint_hit (const struct bp_location *bl,
12943 struct address_space *aspace, CORE_ADDR bp_addr,
12944 const struct target_waitstatus *ws)
12945 {
12946 if (ws->kind != TARGET_WAITKIND_STOPPED
12947 || ws->value.sig != GDB_SIGNAL_TRAP)
12948 return 0;
12949
12950 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12951 aspace, bp_addr))
12952 return 0;
12953
12954 if (overlay_debugging /* unmapped overlay section */
12955 && section_is_overlay (bl->section)
12956 && !section_is_mapped (bl->section))
12957 return 0;
12958
12959 return 1;
12960 }
12961
12962 static int
12963 dprintf_breakpoint_hit (const struct bp_location *bl,
12964 struct address_space *aspace, CORE_ADDR bp_addr,
12965 const struct target_waitstatus *ws)
12966 {
12967 if (dprintf_style == dprintf_style_agent
12968 && target_can_run_breakpoint_commands ())
12969 {
12970 /* An agent-style dprintf never causes a stop. If we see a trap
12971 for this address it must be for a breakpoint that happens to
12972 be set at the same address. */
12973 return 0;
12974 }
12975
12976 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12977 }
12978
12979 static int
12980 bkpt_resources_needed (const struct bp_location *bl)
12981 {
12982 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12983
12984 return 1;
12985 }
12986
12987 static enum print_stop_action
12988 bkpt_print_it (bpstat bs)
12989 {
12990 struct breakpoint *b;
12991 const struct bp_location *bl;
12992 int bp_temp;
12993 struct ui_out *uiout = current_uiout;
12994
12995 gdb_assert (bs->bp_location_at != NULL);
12996
12997 bl = bs->bp_location_at;
12998 b = bs->breakpoint_at;
12999
13000 bp_temp = b->disposition == disp_del;
13001 if (bl->address != bl->requested_address)
13002 breakpoint_adjustment_warning (bl->requested_address,
13003 bl->address,
13004 b->number, 1);
13005 annotate_breakpoint (b->number);
13006 maybe_print_thread_hit_breakpoint (uiout);
13007
13008 if (bp_temp)
13009 uiout->text ("Temporary breakpoint ");
13010 else
13011 uiout->text ("Breakpoint ");
13012 if (uiout->is_mi_like_p ())
13013 {
13014 uiout->field_string ("reason",
13015 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13016 uiout->field_string ("disp", bpdisp_text (b->disposition));
13017 }
13018 uiout->field_int ("bkptno", b->number);
13019 uiout->text (", ");
13020
13021 return PRINT_SRC_AND_LOC;
13022 }
13023
13024 static void
13025 bkpt_print_mention (struct breakpoint *b)
13026 {
13027 if (current_uiout->is_mi_like_p ())
13028 return;
13029
13030 switch (b->type)
13031 {
13032 case bp_breakpoint:
13033 case bp_gnu_ifunc_resolver:
13034 if (b->disposition == disp_del)
13035 printf_filtered (_("Temporary breakpoint"));
13036 else
13037 printf_filtered (_("Breakpoint"));
13038 printf_filtered (_(" %d"), b->number);
13039 if (b->type == bp_gnu_ifunc_resolver)
13040 printf_filtered (_(" at gnu-indirect-function resolver"));
13041 break;
13042 case bp_hardware_breakpoint:
13043 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13044 break;
13045 case bp_dprintf:
13046 printf_filtered (_("Dprintf %d"), b->number);
13047 break;
13048 }
13049
13050 say_where (b);
13051 }
13052
13053 static void
13054 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13055 {
13056 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13057 fprintf_unfiltered (fp, "tbreak");
13058 else if (tp->type == bp_breakpoint)
13059 fprintf_unfiltered (fp, "break");
13060 else if (tp->type == bp_hardware_breakpoint
13061 && tp->disposition == disp_del)
13062 fprintf_unfiltered (fp, "thbreak");
13063 else if (tp->type == bp_hardware_breakpoint)
13064 fprintf_unfiltered (fp, "hbreak");
13065 else
13066 internal_error (__FILE__, __LINE__,
13067 _("unhandled breakpoint type %d"), (int) tp->type);
13068
13069 fprintf_unfiltered (fp, " %s",
13070 event_location_to_string (tp->location.get ()));
13071
13072 /* Print out extra_string if this breakpoint is pending. It might
13073 contain, for example, conditions that were set by the user. */
13074 if (tp->loc == NULL && tp->extra_string != NULL)
13075 fprintf_unfiltered (fp, " %s", tp->extra_string);
13076
13077 print_recreate_thread (tp, fp);
13078 }
13079
13080 static void
13081 bkpt_create_sals_from_location (const struct event_location *location,
13082 struct linespec_result *canonical,
13083 enum bptype type_wanted)
13084 {
13085 create_sals_from_location_default (location, canonical, type_wanted);
13086 }
13087
13088 static void
13089 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13090 struct linespec_result *canonical,
13091 char *cond_string,
13092 char *extra_string,
13093 enum bptype type_wanted,
13094 enum bpdisp disposition,
13095 int thread,
13096 int task, int ignore_count,
13097 const struct breakpoint_ops *ops,
13098 int from_tty, int enabled,
13099 int internal, unsigned flags)
13100 {
13101 create_breakpoints_sal_default (gdbarch, canonical,
13102 cond_string, extra_string,
13103 type_wanted,
13104 disposition, thread, task,
13105 ignore_count, ops, from_tty,
13106 enabled, internal, flags);
13107 }
13108
13109 static void
13110 bkpt_decode_location (struct breakpoint *b,
13111 const struct event_location *location,
13112 struct program_space *search_pspace,
13113 struct symtabs_and_lines *sals)
13114 {
13115 decode_location_default (b, location, search_pspace, sals);
13116 }
13117
13118 /* Virtual table for internal breakpoints. */
13119
13120 static void
13121 internal_bkpt_re_set (struct breakpoint *b)
13122 {
13123 switch (b->type)
13124 {
13125 /* Delete overlay event and longjmp master breakpoints; they
13126 will be reset later by breakpoint_re_set. */
13127 case bp_overlay_event:
13128 case bp_longjmp_master:
13129 case bp_std_terminate_master:
13130 case bp_exception_master:
13131 delete_breakpoint (b);
13132 break;
13133
13134 /* This breakpoint is special, it's set up when the inferior
13135 starts and we really don't want to touch it. */
13136 case bp_shlib_event:
13137
13138 /* Like bp_shlib_event, this breakpoint type is special. Once
13139 it is set up, we do not want to touch it. */
13140 case bp_thread_event:
13141 break;
13142 }
13143 }
13144
13145 static void
13146 internal_bkpt_check_status (bpstat bs)
13147 {
13148 if (bs->breakpoint_at->type == bp_shlib_event)
13149 {
13150 /* If requested, stop when the dynamic linker notifies GDB of
13151 events. This allows the user to get control and place
13152 breakpoints in initializer routines for dynamically loaded
13153 objects (among other things). */
13154 bs->stop = stop_on_solib_events;
13155 bs->print = stop_on_solib_events;
13156 }
13157 else
13158 bs->stop = 0;
13159 }
13160
13161 static enum print_stop_action
13162 internal_bkpt_print_it (bpstat bs)
13163 {
13164 struct breakpoint *b;
13165
13166 b = bs->breakpoint_at;
13167
13168 switch (b->type)
13169 {
13170 case bp_shlib_event:
13171 /* Did we stop because the user set the stop_on_solib_events
13172 variable? (If so, we report this as a generic, "Stopped due
13173 to shlib event" message.) */
13174 print_solib_event (0);
13175 break;
13176
13177 case bp_thread_event:
13178 /* Not sure how we will get here.
13179 GDB should not stop for these breakpoints. */
13180 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13181 break;
13182
13183 case bp_overlay_event:
13184 /* By analogy with the thread event, GDB should not stop for these. */
13185 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13186 break;
13187
13188 case bp_longjmp_master:
13189 /* These should never be enabled. */
13190 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13191 break;
13192
13193 case bp_std_terminate_master:
13194 /* These should never be enabled. */
13195 printf_filtered (_("std::terminate Master Breakpoint: "
13196 "gdb should not stop!\n"));
13197 break;
13198
13199 case bp_exception_master:
13200 /* These should never be enabled. */
13201 printf_filtered (_("Exception Master Breakpoint: "
13202 "gdb should not stop!\n"));
13203 break;
13204 }
13205
13206 return PRINT_NOTHING;
13207 }
13208
13209 static void
13210 internal_bkpt_print_mention (struct breakpoint *b)
13211 {
13212 /* Nothing to mention. These breakpoints are internal. */
13213 }
13214
13215 /* Virtual table for momentary breakpoints */
13216
13217 static void
13218 momentary_bkpt_re_set (struct breakpoint *b)
13219 {
13220 /* Keep temporary breakpoints, which can be encountered when we step
13221 over a dlopen call and solib_add is resetting the breakpoints.
13222 Otherwise these should have been blown away via the cleanup chain
13223 or by breakpoint_init_inferior when we rerun the executable. */
13224 }
13225
13226 static void
13227 momentary_bkpt_check_status (bpstat bs)
13228 {
13229 /* Nothing. The point of these breakpoints is causing a stop. */
13230 }
13231
13232 static enum print_stop_action
13233 momentary_bkpt_print_it (bpstat bs)
13234 {
13235 return PRINT_UNKNOWN;
13236 }
13237
13238 static void
13239 momentary_bkpt_print_mention (struct breakpoint *b)
13240 {
13241 /* Nothing to mention. These breakpoints are internal. */
13242 }
13243
13244 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13245
13246 It gets cleared already on the removal of the first one of such placed
13247 breakpoints. This is OK as they get all removed altogether. */
13248
13249 longjmp_breakpoint::~longjmp_breakpoint ()
13250 {
13251 thread_info *tp = find_thread_global_id (this->thread);
13252
13253 if (tp != NULL)
13254 tp->initiating_frame = null_frame_id;
13255 }
13256
13257 /* Specific methods for probe breakpoints. */
13258
13259 static int
13260 bkpt_probe_insert_location (struct bp_location *bl)
13261 {
13262 int v = bkpt_insert_location (bl);
13263
13264 if (v == 0)
13265 {
13266 /* The insertion was successful, now let's set the probe's semaphore
13267 if needed. */
13268 if (bl->probe.probe->pops->set_semaphore != NULL)
13269 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13270 bl->probe.objfile,
13271 bl->gdbarch);
13272 }
13273
13274 return v;
13275 }
13276
13277 static int
13278 bkpt_probe_remove_location (struct bp_location *bl,
13279 enum remove_bp_reason reason)
13280 {
13281 /* Let's clear the semaphore before removing the location. */
13282 if (bl->probe.probe->pops->clear_semaphore != NULL)
13283 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13284 bl->probe.objfile,
13285 bl->gdbarch);
13286
13287 return bkpt_remove_location (bl, reason);
13288 }
13289
13290 static void
13291 bkpt_probe_create_sals_from_location (const struct event_location *location,
13292 struct linespec_result *canonical,
13293 enum bptype type_wanted)
13294 {
13295 struct linespec_sals lsal;
13296
13297 lsal.sals = parse_probes (location, NULL, canonical);
13298 lsal.canonical
13299 = xstrdup (event_location_to_string (canonical->location.get ()));
13300 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13301 }
13302
13303 static void
13304 bkpt_probe_decode_location (struct breakpoint *b,
13305 const struct event_location *location,
13306 struct program_space *search_pspace,
13307 struct symtabs_and_lines *sals)
13308 {
13309 *sals = parse_probes (location, search_pspace, NULL);
13310 if (!sals->sals)
13311 error (_("probe not found"));
13312 }
13313
13314 /* The breakpoint_ops structure to be used in tracepoints. */
13315
13316 static void
13317 tracepoint_re_set (struct breakpoint *b)
13318 {
13319 breakpoint_re_set_default (b);
13320 }
13321
13322 static int
13323 tracepoint_breakpoint_hit (const struct bp_location *bl,
13324 struct address_space *aspace, CORE_ADDR bp_addr,
13325 const struct target_waitstatus *ws)
13326 {
13327 /* By definition, the inferior does not report stops at
13328 tracepoints. */
13329 return 0;
13330 }
13331
13332 static void
13333 tracepoint_print_one_detail (const struct breakpoint *self,
13334 struct ui_out *uiout)
13335 {
13336 struct tracepoint *tp = (struct tracepoint *) self;
13337 if (tp->static_trace_marker_id)
13338 {
13339 gdb_assert (self->type == bp_static_tracepoint);
13340
13341 uiout->text ("\tmarker id is ");
13342 uiout->field_string ("static-tracepoint-marker-string-id",
13343 tp->static_trace_marker_id);
13344 uiout->text ("\n");
13345 }
13346 }
13347
13348 static void
13349 tracepoint_print_mention (struct breakpoint *b)
13350 {
13351 if (current_uiout->is_mi_like_p ())
13352 return;
13353
13354 switch (b->type)
13355 {
13356 case bp_tracepoint:
13357 printf_filtered (_("Tracepoint"));
13358 printf_filtered (_(" %d"), b->number);
13359 break;
13360 case bp_fast_tracepoint:
13361 printf_filtered (_("Fast tracepoint"));
13362 printf_filtered (_(" %d"), b->number);
13363 break;
13364 case bp_static_tracepoint:
13365 printf_filtered (_("Static tracepoint"));
13366 printf_filtered (_(" %d"), b->number);
13367 break;
13368 default:
13369 internal_error (__FILE__, __LINE__,
13370 _("unhandled tracepoint type %d"), (int) b->type);
13371 }
13372
13373 say_where (b);
13374 }
13375
13376 static void
13377 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13378 {
13379 struct tracepoint *tp = (struct tracepoint *) self;
13380
13381 if (self->type == bp_fast_tracepoint)
13382 fprintf_unfiltered (fp, "ftrace");
13383 else if (self->type == bp_static_tracepoint)
13384 fprintf_unfiltered (fp, "strace");
13385 else if (self->type == bp_tracepoint)
13386 fprintf_unfiltered (fp, "trace");
13387 else
13388 internal_error (__FILE__, __LINE__,
13389 _("unhandled tracepoint type %d"), (int) self->type);
13390
13391 fprintf_unfiltered (fp, " %s",
13392 event_location_to_string (self->location.get ()));
13393 print_recreate_thread (self, fp);
13394
13395 if (tp->pass_count)
13396 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13397 }
13398
13399 static void
13400 tracepoint_create_sals_from_location (const struct event_location *location,
13401 struct linespec_result *canonical,
13402 enum bptype type_wanted)
13403 {
13404 create_sals_from_location_default (location, canonical, type_wanted);
13405 }
13406
13407 static void
13408 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13409 struct linespec_result *canonical,
13410 char *cond_string,
13411 char *extra_string,
13412 enum bptype type_wanted,
13413 enum bpdisp disposition,
13414 int thread,
13415 int task, int ignore_count,
13416 const struct breakpoint_ops *ops,
13417 int from_tty, int enabled,
13418 int internal, unsigned flags)
13419 {
13420 create_breakpoints_sal_default (gdbarch, canonical,
13421 cond_string, extra_string,
13422 type_wanted,
13423 disposition, thread, task,
13424 ignore_count, ops, from_tty,
13425 enabled, internal, flags);
13426 }
13427
13428 static void
13429 tracepoint_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 decode_location_default (b, location, search_pspace, sals);
13435 }
13436
13437 struct breakpoint_ops tracepoint_breakpoint_ops;
13438
13439 /* The breakpoint_ops structure to be use on tracepoints placed in a
13440 static probe. */
13441
13442 static void
13443 tracepoint_probe_create_sals_from_location
13444 (const struct event_location *location,
13445 struct linespec_result *canonical,
13446 enum bptype type_wanted)
13447 {
13448 /* We use the same method for breakpoint on probes. */
13449 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13450 }
13451
13452 static void
13453 tracepoint_probe_decode_location (struct breakpoint *b,
13454 const struct event_location *location,
13455 struct program_space *search_pspace,
13456 struct symtabs_and_lines *sals)
13457 {
13458 /* We use the same method for breakpoint on probes. */
13459 bkpt_probe_decode_location (b, location, search_pspace, sals);
13460 }
13461
13462 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13463
13464 /* Dprintf breakpoint_ops methods. */
13465
13466 static void
13467 dprintf_re_set (struct breakpoint *b)
13468 {
13469 breakpoint_re_set_default (b);
13470
13471 /* extra_string should never be non-NULL for dprintf. */
13472 gdb_assert (b->extra_string != NULL);
13473
13474 /* 1 - connect to target 1, that can run breakpoint commands.
13475 2 - create a dprintf, which resolves fine.
13476 3 - disconnect from target 1
13477 4 - connect to target 2, that can NOT run breakpoint commands.
13478
13479 After steps #3/#4, you'll want the dprintf command list to
13480 be updated, because target 1 and 2 may well return different
13481 answers for target_can_run_breakpoint_commands().
13482 Given absence of finer grained resetting, we get to do
13483 it all the time. */
13484 if (b->extra_string != NULL)
13485 update_dprintf_command_list (b);
13486 }
13487
13488 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13489
13490 static void
13491 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13492 {
13493 fprintf_unfiltered (fp, "dprintf %s,%s",
13494 event_location_to_string (tp->location.get ()),
13495 tp->extra_string);
13496 print_recreate_thread (tp, fp);
13497 }
13498
13499 /* Implement the "after_condition_true" breakpoint_ops method for
13500 dprintf.
13501
13502 dprintf's are implemented with regular commands in their command
13503 list, but we run the commands here instead of before presenting the
13504 stop to the user, as dprintf's don't actually cause a stop. This
13505 also makes it so that the commands of multiple dprintfs at the same
13506 address are all handled. */
13507
13508 static void
13509 dprintf_after_condition_true (struct bpstats *bs)
13510 {
13511 struct cleanup *old_chain;
13512 struct bpstats tmp_bs = { NULL };
13513 struct bpstats *tmp_bs_p = &tmp_bs;
13514
13515 /* dprintf's never cause a stop. This wasn't set in the
13516 check_status hook instead because that would make the dprintf's
13517 condition not be evaluated. */
13518 bs->stop = 0;
13519
13520 /* Run the command list here. Take ownership of it instead of
13521 copying. We never want these commands to run later in
13522 bpstat_do_actions, if a breakpoint that causes a stop happens to
13523 be set at same address as this dprintf, or even if running the
13524 commands here throws. */
13525 tmp_bs.commands = bs->commands;
13526 bs->commands = NULL;
13527 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13528
13529 bpstat_do_actions_1 (&tmp_bs_p);
13530
13531 /* 'tmp_bs.commands' will usually be NULL by now, but
13532 bpstat_do_actions_1 may return early without processing the whole
13533 list. */
13534 do_cleanups (old_chain);
13535 }
13536
13537 /* The breakpoint_ops structure to be used on static tracepoints with
13538 markers (`-m'). */
13539
13540 static void
13541 strace_marker_create_sals_from_location (const struct event_location *location,
13542 struct linespec_result *canonical,
13543 enum bptype type_wanted)
13544 {
13545 struct linespec_sals lsal;
13546 const char *arg_start, *arg;
13547 char *str;
13548 struct cleanup *cleanup;
13549
13550 arg = arg_start = get_linespec_location (location);
13551 lsal.sals = decode_static_tracepoint_spec (&arg);
13552
13553 str = savestring (arg_start, arg - arg_start);
13554 cleanup = make_cleanup (xfree, str);
13555 canonical->location = new_linespec_location (&str);
13556 do_cleanups (cleanup);
13557
13558 lsal.canonical
13559 = xstrdup (event_location_to_string (canonical->location.get ()));
13560 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13561 }
13562
13563 static void
13564 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13565 struct linespec_result *canonical,
13566 char *cond_string,
13567 char *extra_string,
13568 enum bptype type_wanted,
13569 enum bpdisp disposition,
13570 int thread,
13571 int task, int ignore_count,
13572 const struct breakpoint_ops *ops,
13573 int from_tty, int enabled,
13574 int internal, unsigned flags)
13575 {
13576 int i;
13577 struct linespec_sals *lsal = VEC_index (linespec_sals,
13578 canonical->sals, 0);
13579
13580 /* If the user is creating a static tracepoint by marker id
13581 (strace -m MARKER_ID), then store the sals index, so that
13582 breakpoint_re_set can try to match up which of the newly
13583 found markers corresponds to this one, and, don't try to
13584 expand multiple locations for each sal, given than SALS
13585 already should contain all sals for MARKER_ID. */
13586
13587 for (i = 0; i < lsal->sals.nelts; ++i)
13588 {
13589 struct symtabs_and_lines expanded;
13590 struct tracepoint *tp;
13591 event_location_up location;
13592
13593 expanded.nelts = 1;
13594 expanded.sals = &lsal->sals.sals[i];
13595
13596 location = copy_event_location (canonical->location.get ());
13597
13598 tp = new tracepoint ();
13599 init_breakpoint_sal (tp, gdbarch, expanded,
13600 std::move (location), NULL,
13601 cond_string, extra_string,
13602 type_wanted, disposition,
13603 thread, task, ignore_count, ops,
13604 from_tty, enabled, internal, flags,
13605 canonical->special_display);
13606 /* Given that its possible to have multiple markers with
13607 the same string id, if the user is creating a static
13608 tracepoint by marker id ("strace -m MARKER_ID"), then
13609 store the sals index, so that breakpoint_re_set can
13610 try to match up which of the newly found markers
13611 corresponds to this one */
13612 tp->static_trace_marker_id_idx = i;
13613
13614 install_breakpoint (internal, tp, 0);
13615 }
13616 }
13617
13618 static void
13619 strace_marker_decode_location (struct breakpoint *b,
13620 const struct event_location *location,
13621 struct program_space *search_pspace,
13622 struct symtabs_and_lines *sals)
13623 {
13624 struct tracepoint *tp = (struct tracepoint *) b;
13625 const char *s = get_linespec_location (location);
13626
13627 *sals = decode_static_tracepoint_spec (&s);
13628 if (sals->nelts > tp->static_trace_marker_id_idx)
13629 {
13630 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13631 sals->nelts = 1;
13632 }
13633 else
13634 error (_("marker %s not found"), tp->static_trace_marker_id);
13635 }
13636
13637 static struct breakpoint_ops strace_marker_breakpoint_ops;
13638
13639 static int
13640 strace_marker_p (struct breakpoint *b)
13641 {
13642 return b->ops == &strace_marker_breakpoint_ops;
13643 }
13644
13645 /* Delete a breakpoint and clean up all traces of it in the data
13646 structures. */
13647
13648 void
13649 delete_breakpoint (struct breakpoint *bpt)
13650 {
13651 struct breakpoint *b;
13652
13653 gdb_assert (bpt != NULL);
13654
13655 /* Has this bp already been deleted? This can happen because
13656 multiple lists can hold pointers to bp's. bpstat lists are
13657 especial culprits.
13658
13659 One example of this happening is a watchpoint's scope bp. When
13660 the scope bp triggers, we notice that the watchpoint is out of
13661 scope, and delete it. We also delete its scope bp. But the
13662 scope bp is marked "auto-deleting", and is already on a bpstat.
13663 That bpstat is then checked for auto-deleting bp's, which are
13664 deleted.
13665
13666 A real solution to this problem might involve reference counts in
13667 bp's, and/or giving them pointers back to their referencing
13668 bpstat's, and teaching delete_breakpoint to only free a bp's
13669 storage when no more references were extent. A cheaper bandaid
13670 was chosen. */
13671 if (bpt->type == bp_none)
13672 return;
13673
13674 /* At least avoid this stale reference until the reference counting
13675 of breakpoints gets resolved. */
13676 if (bpt->related_breakpoint != bpt)
13677 {
13678 struct breakpoint *related;
13679 struct watchpoint *w;
13680
13681 if (bpt->type == bp_watchpoint_scope)
13682 w = (struct watchpoint *) bpt->related_breakpoint;
13683 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13684 w = (struct watchpoint *) bpt;
13685 else
13686 w = NULL;
13687 if (w != NULL)
13688 watchpoint_del_at_next_stop (w);
13689
13690 /* Unlink bpt from the bpt->related_breakpoint ring. */
13691 for (related = bpt; related->related_breakpoint != bpt;
13692 related = related->related_breakpoint);
13693 related->related_breakpoint = bpt->related_breakpoint;
13694 bpt->related_breakpoint = bpt;
13695 }
13696
13697 /* watch_command_1 creates a watchpoint but only sets its number if
13698 update_watchpoint succeeds in creating its bp_locations. If there's
13699 a problem in that process, we'll be asked to delete the half-created
13700 watchpoint. In that case, don't announce the deletion. */
13701 if (bpt->number)
13702 observer_notify_breakpoint_deleted (bpt);
13703
13704 if (breakpoint_chain == bpt)
13705 breakpoint_chain = bpt->next;
13706
13707 ALL_BREAKPOINTS (b)
13708 if (b->next == bpt)
13709 {
13710 b->next = bpt->next;
13711 break;
13712 }
13713
13714 /* Be sure no bpstat's are pointing at the breakpoint after it's
13715 been freed. */
13716 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13717 in all threads for now. Note that we cannot just remove bpstats
13718 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13719 commands are associated with the bpstat; if we remove it here,
13720 then the later call to bpstat_do_actions (&stop_bpstat); in
13721 event-top.c won't do anything, and temporary breakpoints with
13722 commands won't work. */
13723
13724 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13725
13726 /* Now that breakpoint is removed from breakpoint list, update the
13727 global location list. This will remove locations that used to
13728 belong to this breakpoint. Do this before freeing the breakpoint
13729 itself, since remove_breakpoint looks at location's owner. It
13730 might be better design to have location completely
13731 self-contained, but it's not the case now. */
13732 update_global_location_list (UGLL_DONT_INSERT);
13733
13734 /* On the chance that someone will soon try again to delete this
13735 same bp, we mark it as deleted before freeing its storage. */
13736 bpt->type = bp_none;
13737 delete bpt;
13738 }
13739
13740 static void
13741 do_delete_breakpoint_cleanup (void *b)
13742 {
13743 delete_breakpoint ((struct breakpoint *) b);
13744 }
13745
13746 struct cleanup *
13747 make_cleanup_delete_breakpoint (struct breakpoint *b)
13748 {
13749 return make_cleanup (do_delete_breakpoint_cleanup, b);
13750 }
13751
13752 /* Iterator function to call a user-provided callback function once
13753 for each of B and its related breakpoints. */
13754
13755 static void
13756 iterate_over_related_breakpoints (struct breakpoint *b,
13757 void (*function) (struct breakpoint *,
13758 void *),
13759 void *data)
13760 {
13761 struct breakpoint *related;
13762
13763 related = b;
13764 do
13765 {
13766 struct breakpoint *next;
13767
13768 /* FUNCTION may delete RELATED. */
13769 next = related->related_breakpoint;
13770
13771 if (next == related)
13772 {
13773 /* RELATED is the last ring entry. */
13774 function (related, data);
13775
13776 /* FUNCTION may have deleted it, so we'd never reach back to
13777 B. There's nothing left to do anyway, so just break
13778 out. */
13779 break;
13780 }
13781 else
13782 function (related, data);
13783
13784 related = next;
13785 }
13786 while (related != b);
13787 }
13788
13789 static void
13790 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13791 {
13792 delete_breakpoint (b);
13793 }
13794
13795 /* A callback for map_breakpoint_numbers that calls
13796 delete_breakpoint. */
13797
13798 static void
13799 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13800 {
13801 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13802 }
13803
13804 void
13805 delete_command (char *arg, int from_tty)
13806 {
13807 struct breakpoint *b, *b_tmp;
13808
13809 dont_repeat ();
13810
13811 if (arg == 0)
13812 {
13813 int breaks_to_delete = 0;
13814
13815 /* Delete all breakpoints if no argument. Do not delete
13816 internal breakpoints, these have to be deleted with an
13817 explicit breakpoint number argument. */
13818 ALL_BREAKPOINTS (b)
13819 if (user_breakpoint_p (b))
13820 {
13821 breaks_to_delete = 1;
13822 break;
13823 }
13824
13825 /* Ask user only if there are some breakpoints to delete. */
13826 if (!from_tty
13827 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13828 {
13829 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13830 if (user_breakpoint_p (b))
13831 delete_breakpoint (b);
13832 }
13833 }
13834 else
13835 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13836 }
13837
13838 /* Return true if all locations of B bound to PSPACE are pending. If
13839 PSPACE is NULL, all locations of all program spaces are
13840 considered. */
13841
13842 static int
13843 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13844 {
13845 struct bp_location *loc;
13846
13847 for (loc = b->loc; loc != NULL; loc = loc->next)
13848 if ((pspace == NULL
13849 || loc->pspace == pspace)
13850 && !loc->shlib_disabled
13851 && !loc->pspace->executing_startup)
13852 return 0;
13853 return 1;
13854 }
13855
13856 /* Subroutine of update_breakpoint_locations to simplify it.
13857 Return non-zero if multiple fns in list LOC have the same name.
13858 Null names are ignored. */
13859
13860 static int
13861 ambiguous_names_p (struct bp_location *loc)
13862 {
13863 struct bp_location *l;
13864 htab_t htab = htab_create_alloc (13, htab_hash_string,
13865 (int (*) (const void *,
13866 const void *)) streq,
13867 NULL, xcalloc, xfree);
13868
13869 for (l = loc; l != NULL; l = l->next)
13870 {
13871 const char **slot;
13872 const char *name = l->function_name;
13873
13874 /* Allow for some names to be NULL, ignore them. */
13875 if (name == NULL)
13876 continue;
13877
13878 slot = (const char **) htab_find_slot (htab, (const void *) name,
13879 INSERT);
13880 /* NOTE: We can assume slot != NULL here because xcalloc never
13881 returns NULL. */
13882 if (*slot != NULL)
13883 {
13884 htab_delete (htab);
13885 return 1;
13886 }
13887 *slot = name;
13888 }
13889
13890 htab_delete (htab);
13891 return 0;
13892 }
13893
13894 /* When symbols change, it probably means the sources changed as well,
13895 and it might mean the static tracepoint markers are no longer at
13896 the same address or line numbers they used to be at last we
13897 checked. Losing your static tracepoints whenever you rebuild is
13898 undesirable. This function tries to resync/rematch gdb static
13899 tracepoints with the markers on the target, for static tracepoints
13900 that have not been set by marker id. Static tracepoint that have
13901 been set by marker id are reset by marker id in breakpoint_re_set.
13902 The heuristic is:
13903
13904 1) For a tracepoint set at a specific address, look for a marker at
13905 the old PC. If one is found there, assume to be the same marker.
13906 If the name / string id of the marker found is different from the
13907 previous known name, assume that means the user renamed the marker
13908 in the sources, and output a warning.
13909
13910 2) For a tracepoint set at a given line number, look for a marker
13911 at the new address of the old line number. If one is found there,
13912 assume to be the same marker. If the name / string id of the
13913 marker found is different from the previous known name, assume that
13914 means the user renamed the marker in the sources, and output a
13915 warning.
13916
13917 3) If a marker is no longer found at the same address or line, it
13918 may mean the marker no longer exists. But it may also just mean
13919 the code changed a bit. Maybe the user added a few lines of code
13920 that made the marker move up or down (in line number terms). Ask
13921 the target for info about the marker with the string id as we knew
13922 it. If found, update line number and address in the matching
13923 static tracepoint. This will get confused if there's more than one
13924 marker with the same ID (possible in UST, although unadvised
13925 precisely because it confuses tools). */
13926
13927 static struct symtab_and_line
13928 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13929 {
13930 struct tracepoint *tp = (struct tracepoint *) b;
13931 struct static_tracepoint_marker marker;
13932 CORE_ADDR pc;
13933
13934 pc = sal.pc;
13935 if (sal.line)
13936 find_line_pc (sal.symtab, sal.line, &pc);
13937
13938 if (target_static_tracepoint_marker_at (pc, &marker))
13939 {
13940 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13941 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13942 b->number,
13943 tp->static_trace_marker_id, marker.str_id);
13944
13945 xfree (tp->static_trace_marker_id);
13946 tp->static_trace_marker_id = xstrdup (marker.str_id);
13947 release_static_tracepoint_marker (&marker);
13948
13949 return sal;
13950 }
13951
13952 /* Old marker wasn't found on target at lineno. Try looking it up
13953 by string ID. */
13954 if (!sal.explicit_pc
13955 && sal.line != 0
13956 && sal.symtab != NULL
13957 && tp->static_trace_marker_id != NULL)
13958 {
13959 VEC(static_tracepoint_marker_p) *markers;
13960
13961 markers
13962 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13963
13964 if (!VEC_empty(static_tracepoint_marker_p, markers))
13965 {
13966 struct symtab_and_line sal2;
13967 struct symbol *sym;
13968 struct static_tracepoint_marker *tpmarker;
13969 struct ui_out *uiout = current_uiout;
13970 struct explicit_location explicit_loc;
13971
13972 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13973
13974 xfree (tp->static_trace_marker_id);
13975 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13976
13977 warning (_("marker for static tracepoint %d (%s) not "
13978 "found at previous line number"),
13979 b->number, tp->static_trace_marker_id);
13980
13981 init_sal (&sal2);
13982
13983 sal2.pc = tpmarker->address;
13984
13985 sal2 = find_pc_line (tpmarker->address, 0);
13986 sym = find_pc_sect_function (tpmarker->address, NULL);
13987 uiout->text ("Now in ");
13988 if (sym)
13989 {
13990 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13991 uiout->text (" at ");
13992 }
13993 uiout->field_string ("file",
13994 symtab_to_filename_for_display (sal2.symtab));
13995 uiout->text (":");
13996
13997 if (uiout->is_mi_like_p ())
13998 {
13999 const char *fullname = symtab_to_fullname (sal2.symtab);
14000
14001 uiout->field_string ("fullname", fullname);
14002 }
14003
14004 uiout->field_int ("line", sal2.line);
14005 uiout->text ("\n");
14006
14007 b->loc->line_number = sal2.line;
14008 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14009
14010 b->location.reset (NULL);
14011 initialize_explicit_location (&explicit_loc);
14012 explicit_loc.source_filename
14013 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14014 explicit_loc.line_offset.offset = b->loc->line_number;
14015 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14016 b->location = new_explicit_location (&explicit_loc);
14017
14018 /* Might be nice to check if function changed, and warn if
14019 so. */
14020
14021 release_static_tracepoint_marker (tpmarker);
14022 }
14023 }
14024 return sal;
14025 }
14026
14027 /* Returns 1 iff locations A and B are sufficiently same that
14028 we don't need to report breakpoint as changed. */
14029
14030 static int
14031 locations_are_equal (struct bp_location *a, struct bp_location *b)
14032 {
14033 while (a && b)
14034 {
14035 if (a->address != b->address)
14036 return 0;
14037
14038 if (a->shlib_disabled != b->shlib_disabled)
14039 return 0;
14040
14041 if (a->enabled != b->enabled)
14042 return 0;
14043
14044 a = a->next;
14045 b = b->next;
14046 }
14047
14048 if ((a == NULL) != (b == NULL))
14049 return 0;
14050
14051 return 1;
14052 }
14053
14054 /* Split all locations of B that are bound to PSPACE out of B's
14055 location list to a separate list and return that list's head. If
14056 PSPACE is NULL, hoist out all locations of B. */
14057
14058 static struct bp_location *
14059 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14060 {
14061 struct bp_location head;
14062 struct bp_location *i = b->loc;
14063 struct bp_location **i_link = &b->loc;
14064 struct bp_location *hoisted = &head;
14065
14066 if (pspace == NULL)
14067 {
14068 i = b->loc;
14069 b->loc = NULL;
14070 return i;
14071 }
14072
14073 head.next = NULL;
14074
14075 while (i != NULL)
14076 {
14077 if (i->pspace == pspace)
14078 {
14079 *i_link = i->next;
14080 i->next = NULL;
14081 hoisted->next = i;
14082 hoisted = i;
14083 }
14084 else
14085 i_link = &i->next;
14086 i = *i_link;
14087 }
14088
14089 return head.next;
14090 }
14091
14092 /* Create new breakpoint locations for B (a hardware or software
14093 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14094 zero, then B is a ranged breakpoint. Only recreates locations for
14095 FILTER_PSPACE. Locations of other program spaces are left
14096 untouched. */
14097
14098 void
14099 update_breakpoint_locations (struct breakpoint *b,
14100 struct program_space *filter_pspace,
14101 struct symtabs_and_lines sals,
14102 struct symtabs_and_lines sals_end)
14103 {
14104 int i;
14105 struct bp_location *existing_locations;
14106
14107 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14108 {
14109 /* Ranged breakpoints have only one start location and one end
14110 location. */
14111 b->enable_state = bp_disabled;
14112 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14113 "multiple locations found\n"),
14114 b->number);
14115 return;
14116 }
14117
14118 /* If there's no new locations, and all existing locations are
14119 pending, don't do anything. This optimizes the common case where
14120 all locations are in the same shared library, that was unloaded.
14121 We'd like to retain the location, so that when the library is
14122 loaded again, we don't loose the enabled/disabled status of the
14123 individual locations. */
14124 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14125 return;
14126
14127 existing_locations = hoist_existing_locations (b, filter_pspace);
14128
14129 for (i = 0; i < sals.nelts; ++i)
14130 {
14131 struct bp_location *new_loc;
14132
14133 switch_to_program_space_and_thread (sals.sals[i].pspace);
14134
14135 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14136
14137 /* Reparse conditions, they might contain references to the
14138 old symtab. */
14139 if (b->cond_string != NULL)
14140 {
14141 const char *s;
14142
14143 s = b->cond_string;
14144 TRY
14145 {
14146 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14147 block_for_pc (sals.sals[i].pc),
14148 0);
14149 }
14150 CATCH (e, RETURN_MASK_ERROR)
14151 {
14152 warning (_("failed to reevaluate condition "
14153 "for breakpoint %d: %s"),
14154 b->number, e.message);
14155 new_loc->enabled = 0;
14156 }
14157 END_CATCH
14158 }
14159
14160 if (sals_end.nelts)
14161 {
14162 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14163
14164 new_loc->length = end - sals.sals[0].pc + 1;
14165 }
14166 }
14167
14168 /* If possible, carry over 'disable' status from existing
14169 breakpoints. */
14170 {
14171 struct bp_location *e = existing_locations;
14172 /* If there are multiple breakpoints with the same function name,
14173 e.g. for inline functions, comparing function names won't work.
14174 Instead compare pc addresses; this is just a heuristic as things
14175 may have moved, but in practice it gives the correct answer
14176 often enough until a better solution is found. */
14177 int have_ambiguous_names = ambiguous_names_p (b->loc);
14178
14179 for (; e; e = e->next)
14180 {
14181 if (!e->enabled && e->function_name)
14182 {
14183 struct bp_location *l = b->loc;
14184 if (have_ambiguous_names)
14185 {
14186 for (; l; l = l->next)
14187 if (breakpoint_locations_match (e, l))
14188 {
14189 l->enabled = 0;
14190 break;
14191 }
14192 }
14193 else
14194 {
14195 for (; l; l = l->next)
14196 if (l->function_name
14197 && strcmp (e->function_name, l->function_name) == 0)
14198 {
14199 l->enabled = 0;
14200 break;
14201 }
14202 }
14203 }
14204 }
14205 }
14206
14207 if (!locations_are_equal (existing_locations, b->loc))
14208 observer_notify_breakpoint_modified (b);
14209 }
14210
14211 /* Find the SaL locations corresponding to the given LOCATION.
14212 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14213
14214 static struct symtabs_and_lines
14215 location_to_sals (struct breakpoint *b, struct event_location *location,
14216 struct program_space *search_pspace, int *found)
14217 {
14218 struct symtabs_and_lines sals = {0};
14219 struct gdb_exception exception = exception_none;
14220
14221 gdb_assert (b->ops != NULL);
14222
14223 TRY
14224 {
14225 b->ops->decode_location (b, location, search_pspace, &sals);
14226 }
14227 CATCH (e, RETURN_MASK_ERROR)
14228 {
14229 int not_found_and_ok = 0;
14230
14231 exception = e;
14232
14233 /* For pending breakpoints, it's expected that parsing will
14234 fail until the right shared library is loaded. User has
14235 already told to create pending breakpoints and don't need
14236 extra messages. If breakpoint is in bp_shlib_disabled
14237 state, then user already saw the message about that
14238 breakpoint being disabled, and don't want to see more
14239 errors. */
14240 if (e.error == NOT_FOUND_ERROR
14241 && (b->condition_not_parsed
14242 || (b->loc != NULL
14243 && search_pspace != NULL
14244 && b->loc->pspace != search_pspace)
14245 || (b->loc && b->loc->shlib_disabled)
14246 || (b->loc && b->loc->pspace->executing_startup)
14247 || b->enable_state == bp_disabled))
14248 not_found_and_ok = 1;
14249
14250 if (!not_found_and_ok)
14251 {
14252 /* We surely don't want to warn about the same breakpoint
14253 10 times. One solution, implemented here, is disable
14254 the breakpoint on error. Another solution would be to
14255 have separate 'warning emitted' flag. Since this
14256 happens only when a binary has changed, I don't know
14257 which approach is better. */
14258 b->enable_state = bp_disabled;
14259 throw_exception (e);
14260 }
14261 }
14262 END_CATCH
14263
14264 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14265 {
14266 int i;
14267
14268 for (i = 0; i < sals.nelts; ++i)
14269 resolve_sal_pc (&sals.sals[i]);
14270 if (b->condition_not_parsed && b->extra_string != NULL)
14271 {
14272 char *cond_string, *extra_string;
14273 int thread, task;
14274
14275 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14276 &cond_string, &thread, &task,
14277 &extra_string);
14278 gdb_assert (b->cond_string == NULL);
14279 if (cond_string)
14280 b->cond_string = cond_string;
14281 b->thread = thread;
14282 b->task = task;
14283 if (extra_string)
14284 {
14285 xfree (b->extra_string);
14286 b->extra_string = extra_string;
14287 }
14288 b->condition_not_parsed = 0;
14289 }
14290
14291 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14292 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14293
14294 *found = 1;
14295 }
14296 else
14297 *found = 0;
14298
14299 return sals;
14300 }
14301
14302 /* The default re_set method, for typical hardware or software
14303 breakpoints. Reevaluate the breakpoint and recreate its
14304 locations. */
14305
14306 static void
14307 breakpoint_re_set_default (struct breakpoint *b)
14308 {
14309 int found;
14310 struct symtabs_and_lines sals, sals_end;
14311 struct symtabs_and_lines expanded = {0};
14312 struct symtabs_and_lines expanded_end = {0};
14313 struct program_space *filter_pspace = current_program_space;
14314
14315 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14316 if (found)
14317 {
14318 make_cleanup (xfree, sals.sals);
14319 expanded = sals;
14320 }
14321
14322 if (b->location_range_end != NULL)
14323 {
14324 sals_end = location_to_sals (b, b->location_range_end.get (),
14325 filter_pspace, &found);
14326 if (found)
14327 {
14328 make_cleanup (xfree, sals_end.sals);
14329 expanded_end = sals_end;
14330 }
14331 }
14332
14333 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14334 }
14335
14336 /* Default method for creating SALs from an address string. It basically
14337 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14338
14339 static void
14340 create_sals_from_location_default (const struct event_location *location,
14341 struct linespec_result *canonical,
14342 enum bptype type_wanted)
14343 {
14344 parse_breakpoint_sals (location, canonical);
14345 }
14346
14347 /* Call create_breakpoints_sal for the given arguments. This is the default
14348 function for the `create_breakpoints_sal' method of
14349 breakpoint_ops. */
14350
14351 static void
14352 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14353 struct linespec_result *canonical,
14354 char *cond_string,
14355 char *extra_string,
14356 enum bptype type_wanted,
14357 enum bpdisp disposition,
14358 int thread,
14359 int task, int ignore_count,
14360 const struct breakpoint_ops *ops,
14361 int from_tty, int enabled,
14362 int internal, unsigned flags)
14363 {
14364 create_breakpoints_sal (gdbarch, canonical, cond_string,
14365 extra_string,
14366 type_wanted, disposition,
14367 thread, task, ignore_count, ops, from_tty,
14368 enabled, internal, flags);
14369 }
14370
14371 /* Decode the line represented by S by calling decode_line_full. This is the
14372 default function for the `decode_location' method of breakpoint_ops. */
14373
14374 static void
14375 decode_location_default (struct breakpoint *b,
14376 const struct event_location *location,
14377 struct program_space *search_pspace,
14378 struct symtabs_and_lines *sals)
14379 {
14380 struct linespec_result canonical;
14381
14382 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14383 (struct symtab *) NULL, 0,
14384 &canonical, multiple_symbols_all,
14385 b->filter);
14386
14387 /* We should get 0 or 1 resulting SALs. */
14388 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14389
14390 if (VEC_length (linespec_sals, canonical.sals) > 0)
14391 {
14392 struct linespec_sals *lsal;
14393
14394 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14395 *sals = lsal->sals;
14396 /* Arrange it so the destructor does not free the
14397 contents. */
14398 lsal->sals.sals = NULL;
14399 }
14400 }
14401
14402 /* Prepare the global context for a re-set of breakpoint B. */
14403
14404 static struct cleanup *
14405 prepare_re_set_context (struct breakpoint *b)
14406 {
14407 input_radix = b->input_radix;
14408 set_language (b->language);
14409
14410 return make_cleanup (null_cleanup, NULL);
14411 }
14412
14413 /* Reset a breakpoint given it's struct breakpoint * BINT.
14414 The value we return ends up being the return value from catch_errors.
14415 Unused in this case. */
14416
14417 static int
14418 breakpoint_re_set_one (void *bint)
14419 {
14420 /* Get past catch_errs. */
14421 struct breakpoint *b = (struct breakpoint *) bint;
14422 struct cleanup *cleanups;
14423
14424 cleanups = prepare_re_set_context (b);
14425 b->ops->re_set (b);
14426 do_cleanups (cleanups);
14427 return 0;
14428 }
14429
14430 /* Re-set breakpoint locations for the current program space.
14431 Locations bound to other program spaces are left untouched. */
14432
14433 void
14434 breakpoint_re_set (void)
14435 {
14436 struct breakpoint *b, *b_tmp;
14437 enum language save_language;
14438 int save_input_radix;
14439
14440 save_language = current_language->la_language;
14441 save_input_radix = input_radix;
14442
14443 {
14444 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14445
14446 /* Note: we must not try to insert locations until after all
14447 breakpoints have been re-set. Otherwise, e.g., when re-setting
14448 breakpoint 1, we'd insert the locations of breakpoint 2, which
14449 hadn't been re-set yet, and thus may have stale locations. */
14450
14451 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14452 {
14453 /* Format possible error msg. */
14454 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14455 b->number);
14456 struct cleanup *cleanups = make_cleanup (xfree, message);
14457 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14458 do_cleanups (cleanups);
14459 }
14460 set_language (save_language);
14461 input_radix = save_input_radix;
14462
14463 jit_breakpoint_re_set ();
14464 }
14465
14466 create_overlay_event_breakpoint ();
14467 create_longjmp_master_breakpoint ();
14468 create_std_terminate_master_breakpoint ();
14469 create_exception_master_breakpoint ();
14470
14471 /* Now we can insert. */
14472 update_global_location_list (UGLL_MAY_INSERT);
14473 }
14474 \f
14475 /* Reset the thread number of this breakpoint:
14476
14477 - If the breakpoint is for all threads, leave it as-is.
14478 - Else, reset it to the current thread for inferior_ptid. */
14479 void
14480 breakpoint_re_set_thread (struct breakpoint *b)
14481 {
14482 if (b->thread != -1)
14483 {
14484 if (in_thread_list (inferior_ptid))
14485 b->thread = ptid_to_global_thread_id (inferior_ptid);
14486
14487 /* We're being called after following a fork. The new fork is
14488 selected as current, and unless this was a vfork will have a
14489 different program space from the original thread. Reset that
14490 as well. */
14491 b->loc->pspace = current_program_space;
14492 }
14493 }
14494
14495 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14496 If from_tty is nonzero, it prints a message to that effect,
14497 which ends with a period (no newline). */
14498
14499 void
14500 set_ignore_count (int bptnum, int count, int from_tty)
14501 {
14502 struct breakpoint *b;
14503
14504 if (count < 0)
14505 count = 0;
14506
14507 ALL_BREAKPOINTS (b)
14508 if (b->number == bptnum)
14509 {
14510 if (is_tracepoint (b))
14511 {
14512 if (from_tty && count != 0)
14513 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14514 bptnum);
14515 return;
14516 }
14517
14518 b->ignore_count = count;
14519 if (from_tty)
14520 {
14521 if (count == 0)
14522 printf_filtered (_("Will stop next time "
14523 "breakpoint %d is reached."),
14524 bptnum);
14525 else if (count == 1)
14526 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14527 bptnum);
14528 else
14529 printf_filtered (_("Will ignore next %d "
14530 "crossings of breakpoint %d."),
14531 count, bptnum);
14532 }
14533 observer_notify_breakpoint_modified (b);
14534 return;
14535 }
14536
14537 error (_("No breakpoint number %d."), bptnum);
14538 }
14539
14540 /* Command to set ignore-count of breakpoint N to COUNT. */
14541
14542 static void
14543 ignore_command (char *args, int from_tty)
14544 {
14545 char *p = args;
14546 int num;
14547
14548 if (p == 0)
14549 error_no_arg (_("a breakpoint number"));
14550
14551 num = get_number (&p);
14552 if (num == 0)
14553 error (_("bad breakpoint number: '%s'"), args);
14554 if (*p == 0)
14555 error (_("Second argument (specified ignore-count) is missing."));
14556
14557 set_ignore_count (num,
14558 longest_to_int (value_as_long (parse_and_eval (p))),
14559 from_tty);
14560 if (from_tty)
14561 printf_filtered ("\n");
14562 }
14563 \f
14564 /* Call FUNCTION on each of the breakpoints
14565 whose numbers are given in ARGS. */
14566
14567 static void
14568 map_breakpoint_numbers (const char *args,
14569 void (*function) (struct breakpoint *,
14570 void *),
14571 void *data)
14572 {
14573 int num;
14574 struct breakpoint *b, *tmp;
14575
14576 if (args == 0 || *args == '\0')
14577 error_no_arg (_("one or more breakpoint numbers"));
14578
14579 number_or_range_parser parser (args);
14580
14581 while (!parser.finished ())
14582 {
14583 const char *p = parser.cur_tok ();
14584 bool match = false;
14585
14586 num = parser.get_number ();
14587 if (num == 0)
14588 {
14589 warning (_("bad breakpoint number at or near '%s'"), p);
14590 }
14591 else
14592 {
14593 ALL_BREAKPOINTS_SAFE (b, tmp)
14594 if (b->number == num)
14595 {
14596 match = true;
14597 function (b, data);
14598 break;
14599 }
14600 if (!match)
14601 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14602 }
14603 }
14604 }
14605
14606 static struct bp_location *
14607 find_location_by_number (char *number)
14608 {
14609 char *dot = strchr (number, '.');
14610 char *p1;
14611 int bp_num;
14612 int loc_num;
14613 struct breakpoint *b;
14614 struct bp_location *loc;
14615
14616 *dot = '\0';
14617
14618 p1 = number;
14619 bp_num = get_number (&p1);
14620 if (bp_num == 0)
14621 error (_("Bad breakpoint number '%s'"), number);
14622
14623 ALL_BREAKPOINTS (b)
14624 if (b->number == bp_num)
14625 {
14626 break;
14627 }
14628
14629 if (!b || b->number != bp_num)
14630 error (_("Bad breakpoint number '%s'"), number);
14631
14632 p1 = dot+1;
14633 loc_num = get_number (&p1);
14634 if (loc_num == 0)
14635 error (_("Bad breakpoint location number '%s'"), number);
14636
14637 --loc_num;
14638 loc = b->loc;
14639 for (;loc_num && loc; --loc_num, loc = loc->next)
14640 ;
14641 if (!loc)
14642 error (_("Bad breakpoint location number '%s'"), dot+1);
14643
14644 return loc;
14645 }
14646
14647
14648 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14649 If from_tty is nonzero, it prints a message to that effect,
14650 which ends with a period (no newline). */
14651
14652 void
14653 disable_breakpoint (struct breakpoint *bpt)
14654 {
14655 /* Never disable a watchpoint scope breakpoint; we want to
14656 hit them when we leave scope so we can delete both the
14657 watchpoint and its scope breakpoint at that time. */
14658 if (bpt->type == bp_watchpoint_scope)
14659 return;
14660
14661 bpt->enable_state = bp_disabled;
14662
14663 /* Mark breakpoint locations modified. */
14664 mark_breakpoint_modified (bpt);
14665
14666 if (target_supports_enable_disable_tracepoint ()
14667 && current_trace_status ()->running && is_tracepoint (bpt))
14668 {
14669 struct bp_location *location;
14670
14671 for (location = bpt->loc; location; location = location->next)
14672 target_disable_tracepoint (location);
14673 }
14674
14675 update_global_location_list (UGLL_DONT_INSERT);
14676
14677 observer_notify_breakpoint_modified (bpt);
14678 }
14679
14680 /* A callback for iterate_over_related_breakpoints. */
14681
14682 static void
14683 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14684 {
14685 disable_breakpoint (b);
14686 }
14687
14688 /* A callback for map_breakpoint_numbers that calls
14689 disable_breakpoint. */
14690
14691 static void
14692 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14693 {
14694 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14695 }
14696
14697 static void
14698 disable_command (char *args, int from_tty)
14699 {
14700 if (args == 0)
14701 {
14702 struct breakpoint *bpt;
14703
14704 ALL_BREAKPOINTS (bpt)
14705 if (user_breakpoint_p (bpt))
14706 disable_breakpoint (bpt);
14707 }
14708 else
14709 {
14710 char *num = extract_arg (&args);
14711
14712 while (num)
14713 {
14714 if (strchr (num, '.'))
14715 {
14716 struct bp_location *loc = find_location_by_number (num);
14717
14718 if (loc)
14719 {
14720 if (loc->enabled)
14721 {
14722 loc->enabled = 0;
14723 mark_breakpoint_location_modified (loc);
14724 }
14725 if (target_supports_enable_disable_tracepoint ()
14726 && current_trace_status ()->running && loc->owner
14727 && is_tracepoint (loc->owner))
14728 target_disable_tracepoint (loc);
14729 }
14730 update_global_location_list (UGLL_DONT_INSERT);
14731 }
14732 else
14733 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14734 num = extract_arg (&args);
14735 }
14736 }
14737 }
14738
14739 static void
14740 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14741 int count)
14742 {
14743 int target_resources_ok;
14744
14745 if (bpt->type == bp_hardware_breakpoint)
14746 {
14747 int i;
14748 i = hw_breakpoint_used_count ();
14749 target_resources_ok =
14750 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14751 i + 1, 0);
14752 if (target_resources_ok == 0)
14753 error (_("No hardware breakpoint support in the target."));
14754 else if (target_resources_ok < 0)
14755 error (_("Hardware breakpoints used exceeds limit."));
14756 }
14757
14758 if (is_watchpoint (bpt))
14759 {
14760 /* Initialize it just to avoid a GCC false warning. */
14761 enum enable_state orig_enable_state = bp_disabled;
14762
14763 TRY
14764 {
14765 struct watchpoint *w = (struct watchpoint *) bpt;
14766
14767 orig_enable_state = bpt->enable_state;
14768 bpt->enable_state = bp_enabled;
14769 update_watchpoint (w, 1 /* reparse */);
14770 }
14771 CATCH (e, RETURN_MASK_ALL)
14772 {
14773 bpt->enable_state = orig_enable_state;
14774 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14775 bpt->number);
14776 return;
14777 }
14778 END_CATCH
14779 }
14780
14781 bpt->enable_state = bp_enabled;
14782
14783 /* Mark breakpoint locations modified. */
14784 mark_breakpoint_modified (bpt);
14785
14786 if (target_supports_enable_disable_tracepoint ()
14787 && current_trace_status ()->running && is_tracepoint (bpt))
14788 {
14789 struct bp_location *location;
14790
14791 for (location = bpt->loc; location; location = location->next)
14792 target_enable_tracepoint (location);
14793 }
14794
14795 bpt->disposition = disposition;
14796 bpt->enable_count = count;
14797 update_global_location_list (UGLL_MAY_INSERT);
14798
14799 observer_notify_breakpoint_modified (bpt);
14800 }
14801
14802
14803 void
14804 enable_breakpoint (struct breakpoint *bpt)
14805 {
14806 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14807 }
14808
14809 static void
14810 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14811 {
14812 enable_breakpoint (bpt);
14813 }
14814
14815 /* A callback for map_breakpoint_numbers that calls
14816 enable_breakpoint. */
14817
14818 static void
14819 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14820 {
14821 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14822 }
14823
14824 /* The enable command enables the specified breakpoints (or all defined
14825 breakpoints) so they once again become (or continue to be) effective
14826 in stopping the inferior. */
14827
14828 static void
14829 enable_command (char *args, int from_tty)
14830 {
14831 if (args == 0)
14832 {
14833 struct breakpoint *bpt;
14834
14835 ALL_BREAKPOINTS (bpt)
14836 if (user_breakpoint_p (bpt))
14837 enable_breakpoint (bpt);
14838 }
14839 else
14840 {
14841 char *num = extract_arg (&args);
14842
14843 while (num)
14844 {
14845 if (strchr (num, '.'))
14846 {
14847 struct bp_location *loc = find_location_by_number (num);
14848
14849 if (loc)
14850 {
14851 if (!loc->enabled)
14852 {
14853 loc->enabled = 1;
14854 mark_breakpoint_location_modified (loc);
14855 }
14856 if (target_supports_enable_disable_tracepoint ()
14857 && current_trace_status ()->running && loc->owner
14858 && is_tracepoint (loc->owner))
14859 target_enable_tracepoint (loc);
14860 }
14861 update_global_location_list (UGLL_MAY_INSERT);
14862 }
14863 else
14864 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14865 num = extract_arg (&args);
14866 }
14867 }
14868 }
14869
14870 /* This struct packages up disposition data for application to multiple
14871 breakpoints. */
14872
14873 struct disp_data
14874 {
14875 enum bpdisp disp;
14876 int count;
14877 };
14878
14879 static void
14880 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14881 {
14882 struct disp_data disp_data = *(struct disp_data *) arg;
14883
14884 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14885 }
14886
14887 static void
14888 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14889 {
14890 struct disp_data disp = { disp_disable, 1 };
14891
14892 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14893 }
14894
14895 static void
14896 enable_once_command (char *args, int from_tty)
14897 {
14898 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14899 }
14900
14901 static void
14902 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14903 {
14904 struct disp_data disp = { disp_disable, *(int *) countptr };
14905
14906 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14907 }
14908
14909 static void
14910 enable_count_command (char *args, int from_tty)
14911 {
14912 int count;
14913
14914 if (args == NULL)
14915 error_no_arg (_("hit count"));
14916
14917 count = get_number (&args);
14918
14919 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14920 }
14921
14922 static void
14923 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14924 {
14925 struct disp_data disp = { disp_del, 1 };
14926
14927 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14928 }
14929
14930 static void
14931 enable_delete_command (char *args, int from_tty)
14932 {
14933 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14934 }
14935 \f
14936 static void
14937 set_breakpoint_cmd (char *args, int from_tty)
14938 {
14939 }
14940
14941 static void
14942 show_breakpoint_cmd (char *args, int from_tty)
14943 {
14944 }
14945
14946 /* Invalidate last known value of any hardware watchpoint if
14947 the memory which that value represents has been written to by
14948 GDB itself. */
14949
14950 static void
14951 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14952 CORE_ADDR addr, ssize_t len,
14953 const bfd_byte *data)
14954 {
14955 struct breakpoint *bp;
14956
14957 ALL_BREAKPOINTS (bp)
14958 if (bp->enable_state == bp_enabled
14959 && bp->type == bp_hardware_watchpoint)
14960 {
14961 struct watchpoint *wp = (struct watchpoint *) bp;
14962
14963 if (wp->val_valid && wp->val)
14964 {
14965 struct bp_location *loc;
14966
14967 for (loc = bp->loc; loc != NULL; loc = loc->next)
14968 if (loc->loc_type == bp_loc_hardware_watchpoint
14969 && loc->address + loc->length > addr
14970 && addr + len > loc->address)
14971 {
14972 value_free (wp->val);
14973 wp->val = NULL;
14974 wp->val_valid = 0;
14975 }
14976 }
14977 }
14978 }
14979
14980 /* Create and insert a breakpoint for software single step. */
14981
14982 void
14983 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14984 struct address_space *aspace,
14985 CORE_ADDR next_pc)
14986 {
14987 struct thread_info *tp = inferior_thread ();
14988 struct symtab_and_line sal;
14989 CORE_ADDR pc = next_pc;
14990
14991 if (tp->control.single_step_breakpoints == NULL)
14992 {
14993 tp->control.single_step_breakpoints
14994 = new_single_step_breakpoint (tp->global_num, gdbarch);
14995 }
14996
14997 sal = find_pc_line (pc, 0);
14998 sal.pc = pc;
14999 sal.section = find_pc_overlay (pc);
15000 sal.explicit_pc = 1;
15001 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15002
15003 update_global_location_list (UGLL_INSERT);
15004 }
15005
15006 /* Insert single step breakpoints according to the current state. */
15007
15008 int
15009 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15010 {
15011 struct regcache *regcache = get_current_regcache ();
15012 std::vector<CORE_ADDR> next_pcs;
15013
15014 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15015
15016 if (!next_pcs.empty ())
15017 {
15018 struct frame_info *frame = get_current_frame ();
15019 struct address_space *aspace = get_frame_address_space (frame);
15020
15021 for (CORE_ADDR pc : next_pcs)
15022 insert_single_step_breakpoint (gdbarch, aspace, pc);
15023
15024 return 1;
15025 }
15026 else
15027 return 0;
15028 }
15029
15030 /* See breakpoint.h. */
15031
15032 int
15033 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15034 struct address_space *aspace,
15035 CORE_ADDR pc)
15036 {
15037 struct bp_location *loc;
15038
15039 for (loc = bp->loc; loc != NULL; loc = loc->next)
15040 if (loc->inserted
15041 && breakpoint_location_address_match (loc, aspace, pc))
15042 return 1;
15043
15044 return 0;
15045 }
15046
15047 /* Check whether a software single-step breakpoint is inserted at
15048 PC. */
15049
15050 int
15051 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15052 CORE_ADDR pc)
15053 {
15054 struct breakpoint *bpt;
15055
15056 ALL_BREAKPOINTS (bpt)
15057 {
15058 if (bpt->type == bp_single_step
15059 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15060 return 1;
15061 }
15062 return 0;
15063 }
15064
15065 /* Tracepoint-specific operations. */
15066
15067 /* Set tracepoint count to NUM. */
15068 static void
15069 set_tracepoint_count (int num)
15070 {
15071 tracepoint_count = num;
15072 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15073 }
15074
15075 static void
15076 trace_command (char *arg, int from_tty)
15077 {
15078 struct breakpoint_ops *ops;
15079
15080 event_location_up location = string_to_event_location (&arg,
15081 current_language);
15082 if (location != NULL
15083 && event_location_type (location.get ()) == PROBE_LOCATION)
15084 ops = &tracepoint_probe_breakpoint_ops;
15085 else
15086 ops = &tracepoint_breakpoint_ops;
15087
15088 create_breakpoint (get_current_arch (),
15089 location.get (),
15090 NULL, 0, arg, 1 /* parse arg */,
15091 0 /* tempflag */,
15092 bp_tracepoint /* type_wanted */,
15093 0 /* Ignore count */,
15094 pending_break_support,
15095 ops,
15096 from_tty,
15097 1 /* enabled */,
15098 0 /* internal */, 0);
15099 }
15100
15101 static void
15102 ftrace_command (char *arg, int from_tty)
15103 {
15104 event_location_up location = string_to_event_location (&arg,
15105 current_language);
15106 create_breakpoint (get_current_arch (),
15107 location.get (),
15108 NULL, 0, arg, 1 /* parse arg */,
15109 0 /* tempflag */,
15110 bp_fast_tracepoint /* type_wanted */,
15111 0 /* Ignore count */,
15112 pending_break_support,
15113 &tracepoint_breakpoint_ops,
15114 from_tty,
15115 1 /* enabled */,
15116 0 /* internal */, 0);
15117 }
15118
15119 /* strace command implementation. Creates a static tracepoint. */
15120
15121 static void
15122 strace_command (char *arg, int from_tty)
15123 {
15124 struct breakpoint_ops *ops;
15125 event_location_up location;
15126 struct cleanup *back_to;
15127
15128 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15129 or with a normal static tracepoint. */
15130 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15131 {
15132 ops = &strace_marker_breakpoint_ops;
15133 location = new_linespec_location (&arg);
15134 }
15135 else
15136 {
15137 ops = &tracepoint_breakpoint_ops;
15138 location = string_to_event_location (&arg, current_language);
15139 }
15140
15141 create_breakpoint (get_current_arch (),
15142 location.get (),
15143 NULL, 0, arg, 1 /* parse arg */,
15144 0 /* tempflag */,
15145 bp_static_tracepoint /* type_wanted */,
15146 0 /* Ignore count */,
15147 pending_break_support,
15148 ops,
15149 from_tty,
15150 1 /* enabled */,
15151 0 /* internal */, 0);
15152 }
15153
15154 /* Set up a fake reader function that gets command lines from a linked
15155 list that was acquired during tracepoint uploading. */
15156
15157 static struct uploaded_tp *this_utp;
15158 static int next_cmd;
15159
15160 static char *
15161 read_uploaded_action (void)
15162 {
15163 char *rslt;
15164
15165 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15166
15167 next_cmd++;
15168
15169 return rslt;
15170 }
15171
15172 /* Given information about a tracepoint as recorded on a target (which
15173 can be either a live system or a trace file), attempt to create an
15174 equivalent GDB tracepoint. This is not a reliable process, since
15175 the target does not necessarily have all the information used when
15176 the tracepoint was originally defined. */
15177
15178 struct tracepoint *
15179 create_tracepoint_from_upload (struct uploaded_tp *utp)
15180 {
15181 char *addr_str, small_buf[100];
15182 struct tracepoint *tp;
15183
15184 if (utp->at_string)
15185 addr_str = utp->at_string;
15186 else
15187 {
15188 /* In the absence of a source location, fall back to raw
15189 address. Since there is no way to confirm that the address
15190 means the same thing as when the trace was started, warn the
15191 user. */
15192 warning (_("Uploaded tracepoint %d has no "
15193 "source location, using raw address"),
15194 utp->number);
15195 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15196 addr_str = small_buf;
15197 }
15198
15199 /* There's not much we can do with a sequence of bytecodes. */
15200 if (utp->cond && !utp->cond_string)
15201 warning (_("Uploaded tracepoint %d condition "
15202 "has no source form, ignoring it"),
15203 utp->number);
15204
15205 event_location_up location = string_to_event_location (&addr_str,
15206 current_language);
15207 if (!create_breakpoint (get_current_arch (),
15208 location.get (),
15209 utp->cond_string, -1, addr_str,
15210 0 /* parse cond/thread */,
15211 0 /* tempflag */,
15212 utp->type /* type_wanted */,
15213 0 /* Ignore count */,
15214 pending_break_support,
15215 &tracepoint_breakpoint_ops,
15216 0 /* from_tty */,
15217 utp->enabled /* enabled */,
15218 0 /* internal */,
15219 CREATE_BREAKPOINT_FLAGS_INSERTED))
15220 return NULL;
15221
15222 /* Get the tracepoint we just created. */
15223 tp = get_tracepoint (tracepoint_count);
15224 gdb_assert (tp != NULL);
15225
15226 if (utp->pass > 0)
15227 {
15228 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15229 tp->number);
15230
15231 trace_pass_command (small_buf, 0);
15232 }
15233
15234 /* If we have uploaded versions of the original commands, set up a
15235 special-purpose "reader" function and call the usual command line
15236 reader, then pass the result to the breakpoint command-setting
15237 function. */
15238 if (!VEC_empty (char_ptr, utp->cmd_strings))
15239 {
15240 command_line_up cmd_list;
15241
15242 this_utp = utp;
15243 next_cmd = 0;
15244
15245 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15246
15247 breakpoint_set_commands (tp, std::move (cmd_list));
15248 }
15249 else if (!VEC_empty (char_ptr, utp->actions)
15250 || !VEC_empty (char_ptr, utp->step_actions))
15251 warning (_("Uploaded tracepoint %d actions "
15252 "have no source form, ignoring them"),
15253 utp->number);
15254
15255 /* Copy any status information that might be available. */
15256 tp->hit_count = utp->hit_count;
15257 tp->traceframe_usage = utp->traceframe_usage;
15258
15259 return tp;
15260 }
15261
15262 /* Print information on tracepoint number TPNUM_EXP, or all if
15263 omitted. */
15264
15265 static void
15266 tracepoints_info (char *args, int from_tty)
15267 {
15268 struct ui_out *uiout = current_uiout;
15269 int num_printed;
15270
15271 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15272
15273 if (num_printed == 0)
15274 {
15275 if (args == NULL || *args == '\0')
15276 uiout->message ("No tracepoints.\n");
15277 else
15278 uiout->message ("No tracepoint matching '%s'.\n", args);
15279 }
15280
15281 default_collect_info ();
15282 }
15283
15284 /* The 'enable trace' command enables tracepoints.
15285 Not supported by all targets. */
15286 static void
15287 enable_trace_command (char *args, int from_tty)
15288 {
15289 enable_command (args, from_tty);
15290 }
15291
15292 /* The 'disable trace' command disables tracepoints.
15293 Not supported by all targets. */
15294 static void
15295 disable_trace_command (char *args, int from_tty)
15296 {
15297 disable_command (args, from_tty);
15298 }
15299
15300 /* Remove a tracepoint (or all if no argument). */
15301 static void
15302 delete_trace_command (char *arg, int from_tty)
15303 {
15304 struct breakpoint *b, *b_tmp;
15305
15306 dont_repeat ();
15307
15308 if (arg == 0)
15309 {
15310 int breaks_to_delete = 0;
15311
15312 /* Delete all breakpoints if no argument.
15313 Do not delete internal or call-dummy breakpoints, these
15314 have to be deleted with an explicit breakpoint number
15315 argument. */
15316 ALL_TRACEPOINTS (b)
15317 if (is_tracepoint (b) && user_breakpoint_p (b))
15318 {
15319 breaks_to_delete = 1;
15320 break;
15321 }
15322
15323 /* Ask user only if there are some breakpoints to delete. */
15324 if (!from_tty
15325 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15326 {
15327 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15328 if (is_tracepoint (b) && user_breakpoint_p (b))
15329 delete_breakpoint (b);
15330 }
15331 }
15332 else
15333 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15334 }
15335
15336 /* Helper function for trace_pass_command. */
15337
15338 static void
15339 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15340 {
15341 tp->pass_count = count;
15342 observer_notify_breakpoint_modified (tp);
15343 if (from_tty)
15344 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15345 tp->number, count);
15346 }
15347
15348 /* Set passcount for tracepoint.
15349
15350 First command argument is passcount, second is tracepoint number.
15351 If tracepoint number omitted, apply to most recently defined.
15352 Also accepts special argument "all". */
15353
15354 static void
15355 trace_pass_command (char *args, int from_tty)
15356 {
15357 struct tracepoint *t1;
15358 unsigned int count;
15359
15360 if (args == 0 || *args == 0)
15361 error (_("passcount command requires an "
15362 "argument (count + optional TP num)"));
15363
15364 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15365
15366 args = skip_spaces (args);
15367 if (*args && strncasecmp (args, "all", 3) == 0)
15368 {
15369 struct breakpoint *b;
15370
15371 args += 3; /* Skip special argument "all". */
15372 if (*args)
15373 error (_("Junk at end of arguments."));
15374
15375 ALL_TRACEPOINTS (b)
15376 {
15377 t1 = (struct tracepoint *) b;
15378 trace_pass_set_count (t1, count, from_tty);
15379 }
15380 }
15381 else if (*args == '\0')
15382 {
15383 t1 = get_tracepoint_by_number (&args, NULL);
15384 if (t1)
15385 trace_pass_set_count (t1, count, from_tty);
15386 }
15387 else
15388 {
15389 number_or_range_parser parser (args);
15390 while (!parser.finished ())
15391 {
15392 t1 = get_tracepoint_by_number (&args, &parser);
15393 if (t1)
15394 trace_pass_set_count (t1, count, from_tty);
15395 }
15396 }
15397 }
15398
15399 struct tracepoint *
15400 get_tracepoint (int num)
15401 {
15402 struct breakpoint *t;
15403
15404 ALL_TRACEPOINTS (t)
15405 if (t->number == num)
15406 return (struct tracepoint *) t;
15407
15408 return NULL;
15409 }
15410
15411 /* Find the tracepoint with the given target-side number (which may be
15412 different from the tracepoint number after disconnecting and
15413 reconnecting). */
15414
15415 struct tracepoint *
15416 get_tracepoint_by_number_on_target (int num)
15417 {
15418 struct breakpoint *b;
15419
15420 ALL_TRACEPOINTS (b)
15421 {
15422 struct tracepoint *t = (struct tracepoint *) b;
15423
15424 if (t->number_on_target == num)
15425 return t;
15426 }
15427
15428 return NULL;
15429 }
15430
15431 /* Utility: parse a tracepoint number and look it up in the list.
15432 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15433 If the argument is missing, the most recent tracepoint
15434 (tracepoint_count) is returned. */
15435
15436 struct tracepoint *
15437 get_tracepoint_by_number (char **arg,
15438 number_or_range_parser *parser)
15439 {
15440 struct breakpoint *t;
15441 int tpnum;
15442 char *instring = arg == NULL ? NULL : *arg;
15443
15444 if (parser != NULL)
15445 {
15446 gdb_assert (!parser->finished ());
15447 tpnum = parser->get_number ();
15448 }
15449 else if (arg == NULL || *arg == NULL || ! **arg)
15450 tpnum = tracepoint_count;
15451 else
15452 tpnum = get_number (arg);
15453
15454 if (tpnum <= 0)
15455 {
15456 if (instring && *instring)
15457 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15458 instring);
15459 else
15460 printf_filtered (_("No previous tracepoint\n"));
15461 return NULL;
15462 }
15463
15464 ALL_TRACEPOINTS (t)
15465 if (t->number == tpnum)
15466 {
15467 return (struct tracepoint *) t;
15468 }
15469
15470 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15471 return NULL;
15472 }
15473
15474 void
15475 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15476 {
15477 if (b->thread != -1)
15478 fprintf_unfiltered (fp, " thread %d", b->thread);
15479
15480 if (b->task != 0)
15481 fprintf_unfiltered (fp, " task %d", b->task);
15482
15483 fprintf_unfiltered (fp, "\n");
15484 }
15485
15486 /* Save information on user settable breakpoints (watchpoints, etc) to
15487 a new script file named FILENAME. If FILTER is non-NULL, call it
15488 on each breakpoint and only include the ones for which it returns
15489 non-zero. */
15490
15491 static void
15492 save_breakpoints (char *filename, int from_tty,
15493 int (*filter) (const struct breakpoint *))
15494 {
15495 struct breakpoint *tp;
15496 int any = 0;
15497 struct cleanup *cleanup;
15498 int extra_trace_bits = 0;
15499
15500 if (filename == 0 || *filename == 0)
15501 error (_("Argument required (file name in which to save)"));
15502
15503 /* See if we have anything to save. */
15504 ALL_BREAKPOINTS (tp)
15505 {
15506 /* Skip internal and momentary breakpoints. */
15507 if (!user_breakpoint_p (tp))
15508 continue;
15509
15510 /* If we have a filter, only save the breakpoints it accepts. */
15511 if (filter && !filter (tp))
15512 continue;
15513
15514 any = 1;
15515
15516 if (is_tracepoint (tp))
15517 {
15518 extra_trace_bits = 1;
15519
15520 /* We can stop searching. */
15521 break;
15522 }
15523 }
15524
15525 if (!any)
15526 {
15527 warning (_("Nothing to save."));
15528 return;
15529 }
15530
15531 filename = tilde_expand (filename);
15532 cleanup = make_cleanup (xfree, filename);
15533
15534 stdio_file fp;
15535
15536 if (!fp.open (filename, "w"))
15537 error (_("Unable to open file '%s' for saving (%s)"),
15538 filename, safe_strerror (errno));
15539
15540 if (extra_trace_bits)
15541 save_trace_state_variables (&fp);
15542
15543 ALL_BREAKPOINTS (tp)
15544 {
15545 /* Skip internal and momentary breakpoints. */
15546 if (!user_breakpoint_p (tp))
15547 continue;
15548
15549 /* If we have a filter, only save the breakpoints it accepts. */
15550 if (filter && !filter (tp))
15551 continue;
15552
15553 tp->ops->print_recreate (tp, &fp);
15554
15555 /* Note, we can't rely on tp->number for anything, as we can't
15556 assume the recreated breakpoint numbers will match. Use $bpnum
15557 instead. */
15558
15559 if (tp->cond_string)
15560 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15561
15562 if (tp->ignore_count)
15563 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15564
15565 if (tp->type != bp_dprintf && tp->commands)
15566 {
15567 fp.puts (" commands\n");
15568
15569 current_uiout->redirect (&fp);
15570 TRY
15571 {
15572 print_command_lines (current_uiout, tp->commands->commands, 2);
15573 }
15574 CATCH (ex, RETURN_MASK_ALL)
15575 {
15576 current_uiout->redirect (NULL);
15577 throw_exception (ex);
15578 }
15579 END_CATCH
15580
15581 current_uiout->redirect (NULL);
15582 fp.puts (" end\n");
15583 }
15584
15585 if (tp->enable_state == bp_disabled)
15586 fp.puts ("disable $bpnum\n");
15587
15588 /* If this is a multi-location breakpoint, check if the locations
15589 should be individually disabled. Watchpoint locations are
15590 special, and not user visible. */
15591 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15592 {
15593 struct bp_location *loc;
15594 int n = 1;
15595
15596 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15597 if (!loc->enabled)
15598 fp.printf ("disable $bpnum.%d\n", n);
15599 }
15600 }
15601
15602 if (extra_trace_bits && *default_collect)
15603 fp.printf ("set default-collect %s\n", default_collect);
15604
15605 if (from_tty)
15606 printf_filtered (_("Saved to file '%s'.\n"), filename);
15607 do_cleanups (cleanup);
15608 }
15609
15610 /* The `save breakpoints' command. */
15611
15612 static void
15613 save_breakpoints_command (char *args, int from_tty)
15614 {
15615 save_breakpoints (args, from_tty, NULL);
15616 }
15617
15618 /* The `save tracepoints' command. */
15619
15620 static void
15621 save_tracepoints_command (char *args, int from_tty)
15622 {
15623 save_breakpoints (args, from_tty, is_tracepoint);
15624 }
15625
15626 /* Create a vector of all tracepoints. */
15627
15628 VEC(breakpoint_p) *
15629 all_tracepoints (void)
15630 {
15631 VEC(breakpoint_p) *tp_vec = 0;
15632 struct breakpoint *tp;
15633
15634 ALL_TRACEPOINTS (tp)
15635 {
15636 VEC_safe_push (breakpoint_p, tp_vec, tp);
15637 }
15638
15639 return tp_vec;
15640 }
15641
15642 \f
15643 /* This help string is used to consolidate all the help string for specifying
15644 locations used by several commands. */
15645
15646 #define LOCATION_HELP_STRING \
15647 "Linespecs are colon-separated lists of location parameters, such as\n\
15648 source filename, function name, label name, and line number.\n\
15649 Example: To specify the start of a label named \"the_top\" in the\n\
15650 function \"fact\" in the file \"factorial.c\", use\n\
15651 \"factorial.c:fact:the_top\".\n\
15652 \n\
15653 Address locations begin with \"*\" and specify an exact address in the\n\
15654 program. Example: To specify the fourth byte past the start function\n\
15655 \"main\", use \"*main + 4\".\n\
15656 \n\
15657 Explicit locations are similar to linespecs but use an option/argument\n\
15658 syntax to specify location parameters.\n\
15659 Example: To specify the start of the label named \"the_top\" in the\n\
15660 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15661 -function fact -label the_top\".\n"
15662
15663 /* This help string is used for the break, hbreak, tbreak and thbreak
15664 commands. It is defined as a macro to prevent duplication.
15665 COMMAND should be a string constant containing the name of the
15666 command. */
15667
15668 #define BREAK_ARGS_HELP(command) \
15669 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15670 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15671 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15672 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15673 `-probe-dtrace' (for a DTrace probe).\n\
15674 LOCATION may be a linespec, address, or explicit location as described\n\
15675 below.\n\
15676 \n\
15677 With no LOCATION, uses current execution address of the selected\n\
15678 stack frame. This is useful for breaking on return to a stack frame.\n\
15679 \n\
15680 THREADNUM is the number from \"info threads\".\n\
15681 CONDITION is a boolean expression.\n\
15682 \n" LOCATION_HELP_STRING "\n\
15683 Multiple breakpoints at one place are permitted, and useful if their\n\
15684 conditions are different.\n\
15685 \n\
15686 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15687
15688 /* List of subcommands for "catch". */
15689 static struct cmd_list_element *catch_cmdlist;
15690
15691 /* List of subcommands for "tcatch". */
15692 static struct cmd_list_element *tcatch_cmdlist;
15693
15694 void
15695 add_catch_command (const char *name, const char *docstring,
15696 cmd_sfunc_ftype *sfunc,
15697 completer_ftype *completer,
15698 void *user_data_catch,
15699 void *user_data_tcatch)
15700 {
15701 struct cmd_list_element *command;
15702
15703 command = add_cmd (name, class_breakpoint, NULL, docstring,
15704 &catch_cmdlist);
15705 set_cmd_sfunc (command, sfunc);
15706 set_cmd_context (command, user_data_catch);
15707 set_cmd_completer (command, completer);
15708
15709 command = add_cmd (name, class_breakpoint, NULL, docstring,
15710 &tcatch_cmdlist);
15711 set_cmd_sfunc (command, sfunc);
15712 set_cmd_context (command, user_data_tcatch);
15713 set_cmd_completer (command, completer);
15714 }
15715
15716 static void
15717 save_command (char *arg, int from_tty)
15718 {
15719 printf_unfiltered (_("\"save\" must be followed by "
15720 "the name of a save subcommand.\n"));
15721 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15722 }
15723
15724 struct breakpoint *
15725 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15726 void *data)
15727 {
15728 struct breakpoint *b, *b_tmp;
15729
15730 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15731 {
15732 if ((*callback) (b, data))
15733 return b;
15734 }
15735
15736 return NULL;
15737 }
15738
15739 /* Zero if any of the breakpoint's locations could be a location where
15740 functions have been inlined, nonzero otherwise. */
15741
15742 static int
15743 is_non_inline_function (struct breakpoint *b)
15744 {
15745 /* The shared library event breakpoint is set on the address of a
15746 non-inline function. */
15747 if (b->type == bp_shlib_event)
15748 return 1;
15749
15750 return 0;
15751 }
15752
15753 /* Nonzero if the specified PC cannot be a location where functions
15754 have been inlined. */
15755
15756 int
15757 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15758 const struct target_waitstatus *ws)
15759 {
15760 struct breakpoint *b;
15761 struct bp_location *bl;
15762
15763 ALL_BREAKPOINTS (b)
15764 {
15765 if (!is_non_inline_function (b))
15766 continue;
15767
15768 for (bl = b->loc; bl != NULL; bl = bl->next)
15769 {
15770 if (!bl->shlib_disabled
15771 && bpstat_check_location (bl, aspace, pc, ws))
15772 return 1;
15773 }
15774 }
15775
15776 return 0;
15777 }
15778
15779 /* Remove any references to OBJFILE which is going to be freed. */
15780
15781 void
15782 breakpoint_free_objfile (struct objfile *objfile)
15783 {
15784 struct bp_location **locp, *loc;
15785
15786 ALL_BP_LOCATIONS (loc, locp)
15787 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15788 loc->symtab = NULL;
15789 }
15790
15791 void
15792 initialize_breakpoint_ops (void)
15793 {
15794 static int initialized = 0;
15795
15796 struct breakpoint_ops *ops;
15797
15798 if (initialized)
15799 return;
15800 initialized = 1;
15801
15802 /* The breakpoint_ops structure to be inherit by all kinds of
15803 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15804 internal and momentary breakpoints, etc.). */
15805 ops = &bkpt_base_breakpoint_ops;
15806 *ops = base_breakpoint_ops;
15807 ops->re_set = bkpt_re_set;
15808 ops->insert_location = bkpt_insert_location;
15809 ops->remove_location = bkpt_remove_location;
15810 ops->breakpoint_hit = bkpt_breakpoint_hit;
15811 ops->create_sals_from_location = bkpt_create_sals_from_location;
15812 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15813 ops->decode_location = bkpt_decode_location;
15814
15815 /* The breakpoint_ops structure to be used in regular breakpoints. */
15816 ops = &bkpt_breakpoint_ops;
15817 *ops = bkpt_base_breakpoint_ops;
15818 ops->re_set = bkpt_re_set;
15819 ops->resources_needed = bkpt_resources_needed;
15820 ops->print_it = bkpt_print_it;
15821 ops->print_mention = bkpt_print_mention;
15822 ops->print_recreate = bkpt_print_recreate;
15823
15824 /* Ranged breakpoints. */
15825 ops = &ranged_breakpoint_ops;
15826 *ops = bkpt_breakpoint_ops;
15827 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15828 ops->resources_needed = resources_needed_ranged_breakpoint;
15829 ops->print_it = print_it_ranged_breakpoint;
15830 ops->print_one = print_one_ranged_breakpoint;
15831 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15832 ops->print_mention = print_mention_ranged_breakpoint;
15833 ops->print_recreate = print_recreate_ranged_breakpoint;
15834
15835 /* Internal breakpoints. */
15836 ops = &internal_breakpoint_ops;
15837 *ops = bkpt_base_breakpoint_ops;
15838 ops->re_set = internal_bkpt_re_set;
15839 ops->check_status = internal_bkpt_check_status;
15840 ops->print_it = internal_bkpt_print_it;
15841 ops->print_mention = internal_bkpt_print_mention;
15842
15843 /* Momentary breakpoints. */
15844 ops = &momentary_breakpoint_ops;
15845 *ops = bkpt_base_breakpoint_ops;
15846 ops->re_set = momentary_bkpt_re_set;
15847 ops->check_status = momentary_bkpt_check_status;
15848 ops->print_it = momentary_bkpt_print_it;
15849 ops->print_mention = momentary_bkpt_print_mention;
15850
15851 /* Probe breakpoints. */
15852 ops = &bkpt_probe_breakpoint_ops;
15853 *ops = bkpt_breakpoint_ops;
15854 ops->insert_location = bkpt_probe_insert_location;
15855 ops->remove_location = bkpt_probe_remove_location;
15856 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15857 ops->decode_location = bkpt_probe_decode_location;
15858
15859 /* Watchpoints. */
15860 ops = &watchpoint_breakpoint_ops;
15861 *ops = base_breakpoint_ops;
15862 ops->re_set = re_set_watchpoint;
15863 ops->insert_location = insert_watchpoint;
15864 ops->remove_location = remove_watchpoint;
15865 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15866 ops->check_status = check_status_watchpoint;
15867 ops->resources_needed = resources_needed_watchpoint;
15868 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15869 ops->print_it = print_it_watchpoint;
15870 ops->print_mention = print_mention_watchpoint;
15871 ops->print_recreate = print_recreate_watchpoint;
15872 ops->explains_signal = explains_signal_watchpoint;
15873
15874 /* Masked watchpoints. */
15875 ops = &masked_watchpoint_breakpoint_ops;
15876 *ops = watchpoint_breakpoint_ops;
15877 ops->insert_location = insert_masked_watchpoint;
15878 ops->remove_location = remove_masked_watchpoint;
15879 ops->resources_needed = resources_needed_masked_watchpoint;
15880 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15881 ops->print_it = print_it_masked_watchpoint;
15882 ops->print_one_detail = print_one_detail_masked_watchpoint;
15883 ops->print_mention = print_mention_masked_watchpoint;
15884 ops->print_recreate = print_recreate_masked_watchpoint;
15885
15886 /* Tracepoints. */
15887 ops = &tracepoint_breakpoint_ops;
15888 *ops = base_breakpoint_ops;
15889 ops->re_set = tracepoint_re_set;
15890 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15891 ops->print_one_detail = tracepoint_print_one_detail;
15892 ops->print_mention = tracepoint_print_mention;
15893 ops->print_recreate = tracepoint_print_recreate;
15894 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15895 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15896 ops->decode_location = tracepoint_decode_location;
15897
15898 /* Probe tracepoints. */
15899 ops = &tracepoint_probe_breakpoint_ops;
15900 *ops = tracepoint_breakpoint_ops;
15901 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15902 ops->decode_location = tracepoint_probe_decode_location;
15903
15904 /* Static tracepoints with marker (`-m'). */
15905 ops = &strace_marker_breakpoint_ops;
15906 *ops = tracepoint_breakpoint_ops;
15907 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15908 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15909 ops->decode_location = strace_marker_decode_location;
15910
15911 /* Fork catchpoints. */
15912 ops = &catch_fork_breakpoint_ops;
15913 *ops = base_breakpoint_ops;
15914 ops->insert_location = insert_catch_fork;
15915 ops->remove_location = remove_catch_fork;
15916 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15917 ops->print_it = print_it_catch_fork;
15918 ops->print_one = print_one_catch_fork;
15919 ops->print_mention = print_mention_catch_fork;
15920 ops->print_recreate = print_recreate_catch_fork;
15921
15922 /* Vfork catchpoints. */
15923 ops = &catch_vfork_breakpoint_ops;
15924 *ops = base_breakpoint_ops;
15925 ops->insert_location = insert_catch_vfork;
15926 ops->remove_location = remove_catch_vfork;
15927 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15928 ops->print_it = print_it_catch_vfork;
15929 ops->print_one = print_one_catch_vfork;
15930 ops->print_mention = print_mention_catch_vfork;
15931 ops->print_recreate = print_recreate_catch_vfork;
15932
15933 /* Exec catchpoints. */
15934 ops = &catch_exec_breakpoint_ops;
15935 *ops = base_breakpoint_ops;
15936 ops->insert_location = insert_catch_exec;
15937 ops->remove_location = remove_catch_exec;
15938 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15939 ops->print_it = print_it_catch_exec;
15940 ops->print_one = print_one_catch_exec;
15941 ops->print_mention = print_mention_catch_exec;
15942 ops->print_recreate = print_recreate_catch_exec;
15943
15944 /* Solib-related catchpoints. */
15945 ops = &catch_solib_breakpoint_ops;
15946 *ops = base_breakpoint_ops;
15947 ops->insert_location = insert_catch_solib;
15948 ops->remove_location = remove_catch_solib;
15949 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15950 ops->check_status = check_status_catch_solib;
15951 ops->print_it = print_it_catch_solib;
15952 ops->print_one = print_one_catch_solib;
15953 ops->print_mention = print_mention_catch_solib;
15954 ops->print_recreate = print_recreate_catch_solib;
15955
15956 ops = &dprintf_breakpoint_ops;
15957 *ops = bkpt_base_breakpoint_ops;
15958 ops->re_set = dprintf_re_set;
15959 ops->resources_needed = bkpt_resources_needed;
15960 ops->print_it = bkpt_print_it;
15961 ops->print_mention = bkpt_print_mention;
15962 ops->print_recreate = dprintf_print_recreate;
15963 ops->after_condition_true = dprintf_after_condition_true;
15964 ops->breakpoint_hit = dprintf_breakpoint_hit;
15965 }
15966
15967 /* Chain containing all defined "enable breakpoint" subcommands. */
15968
15969 static struct cmd_list_element *enablebreaklist = NULL;
15970
15971 void
15972 _initialize_breakpoint (void)
15973 {
15974 struct cmd_list_element *c;
15975
15976 initialize_breakpoint_ops ();
15977
15978 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15979 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15980 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15981
15982 breakpoint_objfile_key
15983 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15984
15985 breakpoint_chain = 0;
15986 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15987 before a breakpoint is set. */
15988 breakpoint_count = 0;
15989
15990 tracepoint_count = 0;
15991
15992 add_com ("ignore", class_breakpoint, ignore_command, _("\
15993 Set ignore-count of breakpoint number N to COUNT.\n\
15994 Usage is `ignore N COUNT'."));
15995
15996 add_com ("commands", class_breakpoint, commands_command, _("\
15997 Set commands to be executed when the given breakpoints are hit.\n\
15998 Give a space-separated breakpoint list as argument after \"commands\".\n\
15999 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16000 (e.g. `5-7').\n\
16001 With no argument, the targeted breakpoint is the last one set.\n\
16002 The commands themselves follow starting on the next line.\n\
16003 Type a line containing \"end\" to indicate the end of them.\n\
16004 Give \"silent\" as the first line to make the breakpoint silent;\n\
16005 then no output is printed when it is hit, except what the commands print."));
16006
16007 c = add_com ("condition", class_breakpoint, condition_command, _("\
16008 Specify breakpoint number N to break only if COND is true.\n\
16009 Usage is `condition N COND', where N is an integer and COND is an\n\
16010 expression to be evaluated whenever breakpoint N is reached."));
16011 set_cmd_completer (c, condition_completer);
16012
16013 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16014 Set a temporary breakpoint.\n\
16015 Like \"break\" except the breakpoint is only temporary,\n\
16016 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16017 by using \"enable delete\" on the breakpoint number.\n\
16018 \n"
16019 BREAK_ARGS_HELP ("tbreak")));
16020 set_cmd_completer (c, location_completer);
16021
16022 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16023 Set a hardware assisted breakpoint.\n\
16024 Like \"break\" except the breakpoint requires hardware support,\n\
16025 some target hardware may not have this support.\n\
16026 \n"
16027 BREAK_ARGS_HELP ("hbreak")));
16028 set_cmd_completer (c, location_completer);
16029
16030 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16031 Set a temporary hardware assisted breakpoint.\n\
16032 Like \"hbreak\" except the breakpoint is only temporary,\n\
16033 so it will be deleted when hit.\n\
16034 \n"
16035 BREAK_ARGS_HELP ("thbreak")));
16036 set_cmd_completer (c, location_completer);
16037
16038 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16039 Enable some breakpoints.\n\
16040 Give breakpoint numbers (separated by spaces) as arguments.\n\
16041 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16042 This is used to cancel the effect of the \"disable\" command.\n\
16043 With a subcommand you can enable temporarily."),
16044 &enablelist, "enable ", 1, &cmdlist);
16045
16046 add_com_alias ("en", "enable", class_breakpoint, 1);
16047
16048 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16049 Enable some breakpoints.\n\
16050 Give breakpoint numbers (separated by spaces) as arguments.\n\
16051 This is used to cancel the effect of the \"disable\" command.\n\
16052 May be abbreviated to simply \"enable\".\n"),
16053 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16054
16055 add_cmd ("once", no_class, enable_once_command, _("\
16056 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16057 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16058 &enablebreaklist);
16059
16060 add_cmd ("delete", no_class, enable_delete_command, _("\
16061 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16062 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16063 &enablebreaklist);
16064
16065 add_cmd ("count", no_class, enable_count_command, _("\
16066 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16067 If a breakpoint is hit while enabled in this fashion,\n\
16068 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16069 &enablebreaklist);
16070
16071 add_cmd ("delete", no_class, enable_delete_command, _("\
16072 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16073 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16074 &enablelist);
16075
16076 add_cmd ("once", no_class, enable_once_command, _("\
16077 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16078 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16079 &enablelist);
16080
16081 add_cmd ("count", no_class, enable_count_command, _("\
16082 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16083 If a breakpoint is hit while enabled in this fashion,\n\
16084 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16085 &enablelist);
16086
16087 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16088 Disable some breakpoints.\n\
16089 Arguments are breakpoint numbers with spaces in between.\n\
16090 To disable all breakpoints, give no argument.\n\
16091 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16092 &disablelist, "disable ", 1, &cmdlist);
16093 add_com_alias ("dis", "disable", class_breakpoint, 1);
16094 add_com_alias ("disa", "disable", class_breakpoint, 1);
16095
16096 add_cmd ("breakpoints", class_alias, disable_command, _("\
16097 Disable some breakpoints.\n\
16098 Arguments are breakpoint numbers with spaces in between.\n\
16099 To disable all breakpoints, give no argument.\n\
16100 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16101 This command may be abbreviated \"disable\"."),
16102 &disablelist);
16103
16104 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16105 Delete some breakpoints or auto-display expressions.\n\
16106 Arguments are breakpoint numbers with spaces in between.\n\
16107 To delete all breakpoints, give no argument.\n\
16108 \n\
16109 Also a prefix command for deletion of other GDB objects.\n\
16110 The \"unset\" command is also an alias for \"delete\"."),
16111 &deletelist, "delete ", 1, &cmdlist);
16112 add_com_alias ("d", "delete", class_breakpoint, 1);
16113 add_com_alias ("del", "delete", class_breakpoint, 1);
16114
16115 add_cmd ("breakpoints", class_alias, delete_command, _("\
16116 Delete some breakpoints or auto-display expressions.\n\
16117 Arguments are breakpoint numbers with spaces in between.\n\
16118 To delete all breakpoints, give no argument.\n\
16119 This command may be abbreviated \"delete\"."),
16120 &deletelist);
16121
16122 add_com ("clear", class_breakpoint, clear_command, _("\
16123 Clear breakpoint at specified location.\n\
16124 Argument may be a linespec, explicit, or address location as described below.\n\
16125 \n\
16126 With no argument, clears all breakpoints in the line that the selected frame\n\
16127 is executing in.\n"
16128 "\n" LOCATION_HELP_STRING "\n\
16129 See also the \"delete\" command which clears breakpoints by number."));
16130 add_com_alias ("cl", "clear", class_breakpoint, 1);
16131
16132 c = add_com ("break", class_breakpoint, break_command, _("\
16133 Set breakpoint at specified location.\n"
16134 BREAK_ARGS_HELP ("break")));
16135 set_cmd_completer (c, location_completer);
16136
16137 add_com_alias ("b", "break", class_run, 1);
16138 add_com_alias ("br", "break", class_run, 1);
16139 add_com_alias ("bre", "break", class_run, 1);
16140 add_com_alias ("brea", "break", class_run, 1);
16141
16142 if (dbx_commands)
16143 {
16144 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16145 Break in function/address or break at a line in the current file."),
16146 &stoplist, "stop ", 1, &cmdlist);
16147 add_cmd ("in", class_breakpoint, stopin_command,
16148 _("Break in function or address."), &stoplist);
16149 add_cmd ("at", class_breakpoint, stopat_command,
16150 _("Break at a line in the current file."), &stoplist);
16151 add_com ("status", class_info, breakpoints_info, _("\
16152 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16153 The \"Type\" column indicates one of:\n\
16154 \tbreakpoint - normal breakpoint\n\
16155 \twatchpoint - watchpoint\n\
16156 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16157 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16158 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16159 address and file/line number respectively.\n\
16160 \n\
16161 Convenience variable \"$_\" and default examine address for \"x\"\n\
16162 are set to the address of the last breakpoint listed unless the command\n\
16163 is prefixed with \"server \".\n\n\
16164 Convenience variable \"$bpnum\" contains the number of the last\n\
16165 breakpoint set."));
16166 }
16167
16168 add_info ("breakpoints", breakpoints_info, _("\
16169 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16170 The \"Type\" column indicates one of:\n\
16171 \tbreakpoint - normal breakpoint\n\
16172 \twatchpoint - watchpoint\n\
16173 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16174 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16175 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16176 address and file/line number respectively.\n\
16177 \n\
16178 Convenience variable \"$_\" and default examine address for \"x\"\n\
16179 are set to the address of the last breakpoint listed unless the command\n\
16180 is prefixed with \"server \".\n\n\
16181 Convenience variable \"$bpnum\" contains the number of the last\n\
16182 breakpoint set."));
16183
16184 add_info_alias ("b", "breakpoints", 1);
16185
16186 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16187 Status of all breakpoints, or breakpoint number NUMBER.\n\
16188 The \"Type\" column indicates one of:\n\
16189 \tbreakpoint - normal breakpoint\n\
16190 \twatchpoint - watchpoint\n\
16191 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16192 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16193 \tuntil - internal breakpoint used by the \"until\" command\n\
16194 \tfinish - internal breakpoint used by the \"finish\" command\n\
16195 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16196 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16197 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16198 address and file/line number respectively.\n\
16199 \n\
16200 Convenience variable \"$_\" and default examine address for \"x\"\n\
16201 are set to the address of the last breakpoint listed unless the command\n\
16202 is prefixed with \"server \".\n\n\
16203 Convenience variable \"$bpnum\" contains the number of the last\n\
16204 breakpoint set."),
16205 &maintenanceinfolist);
16206
16207 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16208 Set catchpoints to catch events."),
16209 &catch_cmdlist, "catch ",
16210 0/*allow-unknown*/, &cmdlist);
16211
16212 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16213 Set temporary catchpoints to catch events."),
16214 &tcatch_cmdlist, "tcatch ",
16215 0/*allow-unknown*/, &cmdlist);
16216
16217 add_catch_command ("fork", _("Catch calls to fork."),
16218 catch_fork_command_1,
16219 NULL,
16220 (void *) (uintptr_t) catch_fork_permanent,
16221 (void *) (uintptr_t) catch_fork_temporary);
16222 add_catch_command ("vfork", _("Catch calls to vfork."),
16223 catch_fork_command_1,
16224 NULL,
16225 (void *) (uintptr_t) catch_vfork_permanent,
16226 (void *) (uintptr_t) catch_vfork_temporary);
16227 add_catch_command ("exec", _("Catch calls to exec."),
16228 catch_exec_command_1,
16229 NULL,
16230 CATCH_PERMANENT,
16231 CATCH_TEMPORARY);
16232 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16233 Usage: catch load [REGEX]\n\
16234 If REGEX is given, only stop for libraries matching the regular expression."),
16235 catch_load_command_1,
16236 NULL,
16237 CATCH_PERMANENT,
16238 CATCH_TEMPORARY);
16239 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16240 Usage: catch unload [REGEX]\n\
16241 If REGEX is given, only stop for libraries matching the regular expression."),
16242 catch_unload_command_1,
16243 NULL,
16244 CATCH_PERMANENT,
16245 CATCH_TEMPORARY);
16246
16247 c = add_com ("watch", class_breakpoint, watch_command, _("\
16248 Set a watchpoint for an expression.\n\
16249 Usage: watch [-l|-location] EXPRESSION\n\
16250 A watchpoint stops execution of your program whenever the value of\n\
16251 an expression changes.\n\
16252 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16253 the memory to which it refers."));
16254 set_cmd_completer (c, expression_completer);
16255
16256 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16257 Set a read watchpoint for an expression.\n\
16258 Usage: rwatch [-l|-location] EXPRESSION\n\
16259 A watchpoint stops execution of your program whenever the value of\n\
16260 an expression is read.\n\
16261 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16262 the memory to which it refers."));
16263 set_cmd_completer (c, expression_completer);
16264
16265 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16266 Set a watchpoint for an expression.\n\
16267 Usage: awatch [-l|-location] EXPRESSION\n\
16268 A watchpoint stops execution of your program whenever the value of\n\
16269 an expression is either read or written.\n\
16270 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16271 the memory to which it refers."));
16272 set_cmd_completer (c, expression_completer);
16273
16274 add_info ("watchpoints", watchpoints_info, _("\
16275 Status of specified watchpoints (all watchpoints if no argument)."));
16276
16277 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16278 respond to changes - contrary to the description. */
16279 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16280 &can_use_hw_watchpoints, _("\
16281 Set debugger's willingness to use watchpoint hardware."), _("\
16282 Show debugger's willingness to use watchpoint hardware."), _("\
16283 If zero, gdb will not use hardware for new watchpoints, even if\n\
16284 such is available. (However, any hardware watchpoints that were\n\
16285 created before setting this to nonzero, will continue to use watchpoint\n\
16286 hardware.)"),
16287 NULL,
16288 show_can_use_hw_watchpoints,
16289 &setlist, &showlist);
16290
16291 can_use_hw_watchpoints = 1;
16292
16293 /* Tracepoint manipulation commands. */
16294
16295 c = add_com ("trace", class_breakpoint, trace_command, _("\
16296 Set a tracepoint at specified location.\n\
16297 \n"
16298 BREAK_ARGS_HELP ("trace") "\n\
16299 Do \"help tracepoints\" for info on other tracepoint commands."));
16300 set_cmd_completer (c, location_completer);
16301
16302 add_com_alias ("tp", "trace", class_alias, 0);
16303 add_com_alias ("tr", "trace", class_alias, 1);
16304 add_com_alias ("tra", "trace", class_alias, 1);
16305 add_com_alias ("trac", "trace", class_alias, 1);
16306
16307 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16308 Set a fast tracepoint at specified location.\n\
16309 \n"
16310 BREAK_ARGS_HELP ("ftrace") "\n\
16311 Do \"help tracepoints\" for info on other tracepoint commands."));
16312 set_cmd_completer (c, location_completer);
16313
16314 c = add_com ("strace", class_breakpoint, strace_command, _("\
16315 Set a static tracepoint at location or marker.\n\
16316 \n\
16317 strace [LOCATION] [if CONDITION]\n\
16318 LOCATION may be a linespec, explicit, or address location (described below) \n\
16319 or -m MARKER_ID.\n\n\
16320 If a marker id is specified, probe the marker with that name. With\n\
16321 no LOCATION, uses current execution address of the selected stack frame.\n\
16322 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16323 This collects arbitrary user data passed in the probe point call to the\n\
16324 tracing library. You can inspect it when analyzing the trace buffer,\n\
16325 by printing the $_sdata variable like any other convenience variable.\n\
16326 \n\
16327 CONDITION is a boolean expression.\n\
16328 \n" LOCATION_HELP_STRING "\n\
16329 Multiple tracepoints at one place are permitted, and useful if their\n\
16330 conditions are different.\n\
16331 \n\
16332 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16333 Do \"help tracepoints\" for info on other tracepoint commands."));
16334 set_cmd_completer (c, location_completer);
16335
16336 add_info ("tracepoints", tracepoints_info, _("\
16337 Status of specified tracepoints (all tracepoints if no argument).\n\
16338 Convenience variable \"$tpnum\" contains the number of the\n\
16339 last tracepoint set."));
16340
16341 add_info_alias ("tp", "tracepoints", 1);
16342
16343 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16344 Delete specified tracepoints.\n\
16345 Arguments are tracepoint numbers, separated by spaces.\n\
16346 No argument means delete all tracepoints."),
16347 &deletelist);
16348 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16349
16350 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16351 Disable specified tracepoints.\n\
16352 Arguments are tracepoint numbers, separated by spaces.\n\
16353 No argument means disable all tracepoints."),
16354 &disablelist);
16355 deprecate_cmd (c, "disable");
16356
16357 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16358 Enable specified tracepoints.\n\
16359 Arguments are tracepoint numbers, separated by spaces.\n\
16360 No argument means enable all tracepoints."),
16361 &enablelist);
16362 deprecate_cmd (c, "enable");
16363
16364 add_com ("passcount", class_trace, trace_pass_command, _("\
16365 Set the passcount for a tracepoint.\n\
16366 The trace will end when the tracepoint has been passed 'count' times.\n\
16367 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16368 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16369
16370 add_prefix_cmd ("save", class_breakpoint, save_command,
16371 _("Save breakpoint definitions as a script."),
16372 &save_cmdlist, "save ",
16373 0/*allow-unknown*/, &cmdlist);
16374
16375 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16376 Save current breakpoint definitions as a script.\n\
16377 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16378 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16379 session to restore them."),
16380 &save_cmdlist);
16381 set_cmd_completer (c, filename_completer);
16382
16383 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16384 Save current tracepoint definitions as a script.\n\
16385 Use the 'source' command in another debug session to restore them."),
16386 &save_cmdlist);
16387 set_cmd_completer (c, filename_completer);
16388
16389 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16390 deprecate_cmd (c, "save tracepoints");
16391
16392 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16393 Breakpoint specific settings\n\
16394 Configure various breakpoint-specific variables such as\n\
16395 pending breakpoint behavior"),
16396 &breakpoint_set_cmdlist, "set breakpoint ",
16397 0/*allow-unknown*/, &setlist);
16398 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16399 Breakpoint specific settings\n\
16400 Configure various breakpoint-specific variables such as\n\
16401 pending breakpoint behavior"),
16402 &breakpoint_show_cmdlist, "show breakpoint ",
16403 0/*allow-unknown*/, &showlist);
16404
16405 add_setshow_auto_boolean_cmd ("pending", no_class,
16406 &pending_break_support, _("\
16407 Set debugger's behavior regarding pending breakpoints."), _("\
16408 Show debugger's behavior regarding pending breakpoints."), _("\
16409 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16410 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16411 an error. If auto, an unrecognized breakpoint location results in a\n\
16412 user-query to see if a pending breakpoint should be created."),
16413 NULL,
16414 show_pending_break_support,
16415 &breakpoint_set_cmdlist,
16416 &breakpoint_show_cmdlist);
16417
16418 pending_break_support = AUTO_BOOLEAN_AUTO;
16419
16420 add_setshow_boolean_cmd ("auto-hw", no_class,
16421 &automatic_hardware_breakpoints, _("\
16422 Set automatic usage of hardware breakpoints."), _("\
16423 Show automatic usage of hardware breakpoints."), _("\
16424 If set, the debugger will automatically use hardware breakpoints for\n\
16425 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16426 a warning will be emitted for such breakpoints."),
16427 NULL,
16428 show_automatic_hardware_breakpoints,
16429 &breakpoint_set_cmdlist,
16430 &breakpoint_show_cmdlist);
16431
16432 add_setshow_boolean_cmd ("always-inserted", class_support,
16433 &always_inserted_mode, _("\
16434 Set mode for inserting breakpoints."), _("\
16435 Show mode for inserting breakpoints."), _("\
16436 When this mode is on, breakpoints are inserted immediately as soon as\n\
16437 they're created, kept inserted even when execution stops, and removed\n\
16438 only when the user deletes them. When this mode is off (the default),\n\
16439 breakpoints are inserted only when execution continues, and removed\n\
16440 when execution stops."),
16441 NULL,
16442 &show_always_inserted_mode,
16443 &breakpoint_set_cmdlist,
16444 &breakpoint_show_cmdlist);
16445
16446 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16447 condition_evaluation_enums,
16448 &condition_evaluation_mode_1, _("\
16449 Set mode of breakpoint condition evaluation."), _("\
16450 Show mode of breakpoint condition evaluation."), _("\
16451 When this is set to \"host\", breakpoint conditions will be\n\
16452 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16453 breakpoint conditions will be downloaded to the target (if the target\n\
16454 supports such feature) and conditions will be evaluated on the target's side.\n\
16455 If this is set to \"auto\" (default), this will be automatically set to\n\
16456 \"target\" if it supports condition evaluation, otherwise it will\n\
16457 be set to \"gdb\""),
16458 &set_condition_evaluation_mode,
16459 &show_condition_evaluation_mode,
16460 &breakpoint_set_cmdlist,
16461 &breakpoint_show_cmdlist);
16462
16463 add_com ("break-range", class_breakpoint, break_range_command, _("\
16464 Set a breakpoint for an address range.\n\
16465 break-range START-LOCATION, END-LOCATION\n\
16466 where START-LOCATION and END-LOCATION can be one of the following:\n\
16467 LINENUM, for that line in the current file,\n\
16468 FILE:LINENUM, for that line in that file,\n\
16469 +OFFSET, for that number of lines after the current line\n\
16470 or the start of the range\n\
16471 FUNCTION, for the first line in that function,\n\
16472 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16473 *ADDRESS, for the instruction at that address.\n\
16474 \n\
16475 The breakpoint will stop execution of the inferior whenever it executes\n\
16476 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16477 range (including START-LOCATION and END-LOCATION)."));
16478
16479 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16480 Set a dynamic printf at specified location.\n\
16481 dprintf location,format string,arg1,arg2,...\n\
16482 location may be a linespec, explicit, or address location.\n"
16483 "\n" LOCATION_HELP_STRING));
16484 set_cmd_completer (c, location_completer);
16485
16486 add_setshow_enum_cmd ("dprintf-style", class_support,
16487 dprintf_style_enums, &dprintf_style, _("\
16488 Set the style of usage for dynamic printf."), _("\
16489 Show the style of usage for dynamic printf."), _("\
16490 This setting chooses how GDB will do a dynamic printf.\n\
16491 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16492 console, as with the \"printf\" command.\n\
16493 If the value is \"call\", the print is done by calling a function in your\n\
16494 program; by default printf(), but you can choose a different function or\n\
16495 output stream by setting dprintf-function and dprintf-channel."),
16496 update_dprintf_commands, NULL,
16497 &setlist, &showlist);
16498
16499 dprintf_function = xstrdup ("printf");
16500 add_setshow_string_cmd ("dprintf-function", class_support,
16501 &dprintf_function, _("\
16502 Set the function to use for dynamic printf"), _("\
16503 Show the function to use for dynamic printf"), NULL,
16504 update_dprintf_commands, NULL,
16505 &setlist, &showlist);
16506
16507 dprintf_channel = xstrdup ("");
16508 add_setshow_string_cmd ("dprintf-channel", class_support,
16509 &dprintf_channel, _("\
16510 Set the channel to use for dynamic printf"), _("\
16511 Show the channel to use for dynamic printf"), NULL,
16512 update_dprintf_commands, NULL,
16513 &setlist, &showlist);
16514
16515 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16516 &disconnected_dprintf, _("\
16517 Set whether dprintf continues after GDB disconnects."), _("\
16518 Show whether dprintf continues after GDB disconnects."), _("\
16519 Use this to let dprintf commands continue to hit and produce output\n\
16520 even if GDB disconnects or detaches from the target."),
16521 NULL,
16522 NULL,
16523 &setlist, &showlist);
16524
16525 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16526 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16527 (target agent only) This is useful for formatted output in user-defined commands."));
16528
16529 automatic_hardware_breakpoints = 1;
16530
16531 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16532 observer_attach_thread_exit (remove_threaded_breakpoints);
16533 }
This page took 0.389252 seconds and 5 git commands to generate.