Create struct type for longjmp breakpoint
[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 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313 static struct breakpoint_ops longjmp_breakpoint_ops;
314
315 /* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317 struct breakpoint_ops bkpt_breakpoint_ops;
318
319 /* Breakpoints set on probes. */
320 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
322 /* Dynamic printf class type. */
323 struct breakpoint_ops dprintf_breakpoint_ops;
324
325 /* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332 static const char dprintf_style_gdb[] = "gdb";
333 static const char dprintf_style_call[] = "call";
334 static const char dprintf_style_agent[] = "agent";
335 static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340 };
341 static const char *dprintf_style = dprintf_style_gdb;
342
343 /* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348 static char *dprintf_function;
349
350 /* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358 static char *dprintf_channel;
359
360 /* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362 static int disconnected_dprintf = 1;
363
364 /* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366 struct counted_command_line
367 {
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373 };
374
375 struct command_line *
376 breakpoint_commands (struct breakpoint *b)
377 {
378 return b->commands ? b->commands->commands : NULL;
379 }
380
381 /* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384 static int breakpoint_proceeded;
385
386 const char *
387 bpdisp_text (enum bpdisp disp)
388 {
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
393
394 return bpdisps[(int) disp];
395 }
396
397 /* Prototypes for exported functions. */
398 /* If FALSE, gdb will not use hardware support for watchpoints, even
399 if such is available. */
400 static int can_use_hw_watchpoints;
401
402 static void
403 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406 {
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
410 value);
411 }
412
413 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
415 for unrecognized breakpoint locations.
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417 static enum auto_boolean pending_break_support;
418 static void
419 show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422 {
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
426 value);
427 }
428
429 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
430 set with "break" but falling in read-only memory.
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433 static int automatic_hardware_breakpoints;
434 static void
435 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438 {
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
441 value);
442 }
443
444 /* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451 static int always_inserted_mode = 0;
452
453 static void
454 show_always_inserted_mode (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
456 {
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
459 }
460
461 /* See breakpoint.h. */
462
463 int
464 breakpoints_should_be_inserted_now (void)
465 {
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
475 struct thread_info *tp;
476
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
484 if (threads_are_executing ())
485 return 1;
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
493 }
494 return 0;
495 }
496
497 static const char condition_evaluation_both[] = "host or target";
498
499 /* Modes for breakpoint condition evaluation. */
500 static const char condition_evaluation_auto[] = "auto";
501 static const char condition_evaluation_host[] = "host";
502 static const char condition_evaluation_target[] = "target";
503 static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508 };
509
510 /* Global that holds the current mode for breakpoint condition evaluation. */
511 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513 /* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515 static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517 /* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522 static const char *
523 translate_condition_evaluation_mode (const char *mode)
524 {
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534 }
535
536 /* Discovers what condition_evaluation_auto translates to. */
537
538 static const char *
539 breakpoint_condition_evaluation_mode (void)
540 {
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542 }
543
544 /* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547 static int
548 gdb_evaluates_breakpoint_condition_p (void)
549 {
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553 }
554
555 void _initialize_breakpoint (void);
556
557 /* Are we executing breakpoint commands? */
558 static int executing_breakpoint_commands;
559
560 /* Are overlay event breakpoints enabled? */
561 static int overlay_events_enabled;
562
563 /* See description in breakpoint.h. */
564 int target_exact_watchpoints = 0;
565
566 /* Walk the following statement or block through all breakpoints.
567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
568 current breakpoint. */
569
570 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
571
572 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
576
577 /* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
580
581 #define ALL_BP_LOCATIONS(B,BP_TMP) \
582 for (BP_TMP = bp_locations; \
583 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
584 BP_TMP++)
585
586 /* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
596 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
600 /* Iterator for tracepoints only. */
601
602 #define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
604 if (is_tracepoint (B))
605
606 /* Chains of all breakpoints defined. */
607
608 struct breakpoint *breakpoint_chain;
609
610 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
611
612 static struct bp_location **bp_locations;
613
614 /* Number of elements of BP_LOCATIONS. */
615
616 static unsigned bp_locations_count;
617
618 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
619 ADDRESS for the current elements of BP_LOCATIONS which get a valid
620 result from bp_location_has_shadow. You can use it for roughly
621 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
622 an address you need to read. */
623
624 static CORE_ADDR bp_locations_placed_address_before_address_max;
625
626 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
628 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATIONS to
630 scan for shadow bytes for an address you need to read. */
631
632 static CORE_ADDR bp_locations_shadow_len_after_address_max;
633
634 /* The locations that no longer correspond to any breakpoint, unlinked
635 from the bp_locations array, but for which a hit may still be
636 reported by a target. */
637 VEC(bp_location_p) *moribund_locations = NULL;
638
639 /* Number of last breakpoint made. */
640
641 static int breakpoint_count;
642
643 /* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647 static int prev_breakpoint_count;
648
649 /* Number of last tracepoint made. */
650
651 static int tracepoint_count;
652
653 static struct cmd_list_element *breakpoint_set_cmdlist;
654 static struct cmd_list_element *breakpoint_show_cmdlist;
655 struct cmd_list_element *save_cmdlist;
656
657 /* See declaration at breakpoint.h. */
658
659 struct breakpoint *
660 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662 {
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672 }
673
674 /* Return whether a breakpoint is an active enabled breakpoint. */
675 static int
676 breakpoint_enabled (struct breakpoint *b)
677 {
678 return (b->enable_state == bp_enabled);
679 }
680
681 /* Set breakpoint count to NUM. */
682
683 static void
684 set_breakpoint_count (int num)
685 {
686 prev_breakpoint_count = breakpoint_count;
687 breakpoint_count = num;
688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
689 }
690
691 /* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693 static int rbreak_start_breakpoint_count;
694
695 /* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
697
698 void
699 start_rbreak_breakpoints (void)
700 {
701 rbreak_start_breakpoint_count = breakpoint_count;
702 }
703
704 /* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
706
707 void
708 end_rbreak_breakpoints (void)
709 {
710 prev_breakpoint_count = rbreak_start_breakpoint_count;
711 }
712
713 /* Used in run_command to zero the hit count when a new run starts. */
714
715 void
716 clear_breakpoint_hit_counts (void)
717 {
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722 }
723
724 /* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727 static struct counted_command_line *
728 alloc_counted_command_line (struct command_line *commands)
729 {
730 struct counted_command_line *result = XNEW (struct counted_command_line);
731
732 result->refc = 1;
733 result->commands = commands;
734
735 return result;
736 }
737
738 /* Increment reference count. This does nothing if CMD is NULL. */
739
740 static void
741 incref_counted_command_line (struct counted_command_line *cmd)
742 {
743 if (cmd)
744 ++cmd->refc;
745 }
746
747 /* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751 static void
752 decref_counted_command_line (struct counted_command_line **cmdp)
753 {
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763 }
764
765 /* A cleanup function that calls decref_counted_command_line. */
766
767 static void
768 do_cleanup_counted_command_line (void *arg)
769 {
770 decref_counted_command_line ((struct counted_command_line **) arg);
771 }
772
773 /* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776 static struct cleanup *
777 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778 {
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780 }
781
782 \f
783 /* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786 struct breakpoint *
787 get_breakpoint (int num)
788 {
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796 }
797
798 \f
799
800 /* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803 static void
804 mark_breakpoint_modified (struct breakpoint *b)
805 {
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821 }
822
823 /* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826 static void
827 mark_breakpoint_location_modified (struct bp_location *loc)
828 {
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842 }
843
844 /* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847 static void
848 set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850 {
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
898 update_global_location_list (UGLL_MAY_INSERT);
899 }
900
901 return;
902 }
903
904 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907 static void
908 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910 {
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920 }
921
922 /* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
924 the more general bp_locations_compare function. */
925
926 static int
927 bp_locations_compare_addrs (const void *ap, const void *bp)
928 {
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936 }
937
938 /* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943 static struct bp_location **
944 get_first_locp_gte_addr (CORE_ADDR address)
945 {
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
951 dummy_loc.address = address;
952
953 /* Find a close match to the first location at ADDRESS. */
954 locp_found = ((struct bp_location **)
955 bsearch (&dummy_locp, bp_locations, bp_locations_count,
956 sizeof (struct bp_location **),
957 bp_locations_compare_addrs));
958
959 /* Nothing was found, nothing left to do. */
960 if (locp_found == NULL)
961 return NULL;
962
963 /* We may have found a location that is at ADDRESS but is not the first in the
964 location's list. Go backwards (if possible) and locate the first one. */
965 while ((locp_found - 1) >= bp_locations
966 && (*(locp_found - 1))->address == address)
967 locp_found--;
968
969 return locp_found;
970 }
971
972 void
973 set_breakpoint_condition (struct breakpoint *b, const char *exp,
974 int from_tty)
975 {
976 xfree (b->cond_string);
977 b->cond_string = NULL;
978
979 if (is_watchpoint (b))
980 {
981 struct watchpoint *w = (struct watchpoint *) b;
982
983 w->cond_exp.reset ();
984 }
985 else
986 {
987 struct bp_location *loc;
988
989 for (loc = b->loc; loc; loc = loc->next)
990 {
991 loc->cond.reset ();
992
993 /* No need to free the condition agent expression
994 bytecode (if we have one). We will handle this
995 when we go through update_global_location_list. */
996 }
997 }
998
999 if (*exp == 0)
1000 {
1001 if (from_tty)
1002 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1003 }
1004 else
1005 {
1006 const char *arg = exp;
1007
1008 /* I don't know if it matters whether this is the string the user
1009 typed in or the decompiled expression. */
1010 b->cond_string = xstrdup (arg);
1011 b->condition_not_parsed = 0;
1012
1013 if (is_watchpoint (b))
1014 {
1015 struct watchpoint *w = (struct watchpoint *) b;
1016
1017 innermost_block = NULL;
1018 arg = exp;
1019 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1020 if (*arg)
1021 error (_("Junk at end of expression"));
1022 w->cond_exp_valid_block = innermost_block;
1023 }
1024 else
1025 {
1026 struct bp_location *loc;
1027
1028 for (loc = b->loc; loc; loc = loc->next)
1029 {
1030 arg = exp;
1031 loc->cond =
1032 parse_exp_1 (&arg, loc->address,
1033 block_for_pc (loc->address), 0);
1034 if (*arg)
1035 error (_("Junk at end of expression"));
1036 }
1037 }
1038 }
1039 mark_breakpoint_modified (b);
1040
1041 observer_notify_breakpoint_modified (b);
1042 }
1043
1044 /* Completion for the "condition" command. */
1045
1046 static VEC (char_ptr) *
1047 condition_completer (struct cmd_list_element *cmd,
1048 const char *text, const char *word)
1049 {
1050 const char *space;
1051
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (isdigit (text[1]))
1064 return NULL;
1065 return complete_internalvar (&text[1]);
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 VEC_safe_push (char_ptr, result, xstrdup (number));
1079 }
1080
1081 return result;
1082 }
1083
1084 /* We're completing the expression part. */
1085 text = skip_spaces_const (space);
1086 return expression_completer (cmd, text, word);
1087 }
1088
1089 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1090
1091 static void
1092 condition_command (char *arg, int from_tty)
1093 {
1094 struct breakpoint *b;
1095 char *p;
1096 int bnum;
1097
1098 if (arg == 0)
1099 error_no_arg (_("breakpoint number"));
1100
1101 p = arg;
1102 bnum = get_number (&p);
1103 if (bnum == 0)
1104 error (_("Bad breakpoint argument: '%s'"), arg);
1105
1106 ALL_BREAKPOINTS (b)
1107 if (b->number == bnum)
1108 {
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
1121 set_breakpoint_condition (b, p, from_tty);
1122
1123 if (is_breakpoint (b))
1124 update_global_location_list (UGLL_MAY_INSERT);
1125
1126 return;
1127 }
1128
1129 error (_("No breakpoint number %d."), bnum);
1130 }
1131
1132 /* Check that COMMAND do not contain commands that are suitable
1133 only for tracepoints and not suitable for ordinary breakpoints.
1134 Throw if any such commands is found. */
1135
1136 static void
1137 check_no_tracepoint_commands (struct command_line *commands)
1138 {
1139 struct command_line *c;
1140
1141 for (c = commands; c; c = c->next)
1142 {
1143 int i;
1144
1145 if (c->control_type == while_stepping_control)
1146 error (_("The 'while-stepping' command can "
1147 "only be used for tracepoints"));
1148
1149 for (i = 0; i < c->body_count; ++i)
1150 check_no_tracepoint_commands ((c->body_list)[i]);
1151
1152 /* Not that command parsing removes leading whitespace and comment
1153 lines and also empty lines. So, we only need to check for
1154 command directly. */
1155 if (strstr (c->line, "collect ") == c->line)
1156 error (_("The 'collect' command can only be used for tracepoints"));
1157
1158 if (strstr (c->line, "teval ") == c->line)
1159 error (_("The 'teval' command can only be used for tracepoints"));
1160 }
1161 }
1162
1163 struct longjmp_breakpoint
1164 {
1165 breakpoint base;
1166 };
1167
1168 /* Encapsulate tests for different types of tracepoints. */
1169
1170 static bool
1171 is_tracepoint_type (bptype type)
1172 {
1173 return (type == bp_tracepoint
1174 || type == bp_fast_tracepoint
1175 || type == bp_static_tracepoint);
1176 }
1177
1178 static bool
1179 is_longjmp_type (bptype type)
1180 {
1181 return type == bp_longjmp || type == bp_exception;
1182 }
1183
1184 int
1185 is_tracepoint (const struct breakpoint *b)
1186 {
1187 return is_tracepoint_type (b->type);
1188 }
1189
1190 /* Factory function to create an appropriate instance of breakpoint given
1191 TYPE. */
1192
1193 static std::unique_ptr<breakpoint>
1194 new_breakpoint_from_type (bptype type)
1195 {
1196 breakpoint *b;
1197
1198 if (is_tracepoint_type (type))
1199 b = (breakpoint *) new tracepoint ();
1200 else if (is_longjmp_type (type))
1201 b = (breakpoint *) new longjmp_breakpoint ();
1202 else
1203 b = new breakpoint ();
1204
1205 return std::unique_ptr<breakpoint> (b);
1206 }
1207
1208 /* A helper function that validates that COMMANDS are valid for a
1209 breakpoint. This function will throw an exception if a problem is
1210 found. */
1211
1212 static void
1213 validate_commands_for_breakpoint (struct breakpoint *b,
1214 struct command_line *commands)
1215 {
1216 if (is_tracepoint (b))
1217 {
1218 struct tracepoint *t = (struct tracepoint *) b;
1219 struct command_line *c;
1220 struct command_line *while_stepping = 0;
1221
1222 /* Reset the while-stepping step count. The previous commands
1223 might have included a while-stepping action, while the new
1224 ones might not. */
1225 t->step_count = 0;
1226
1227 /* We need to verify that each top-level element of commands is
1228 valid for tracepoints, that there's at most one
1229 while-stepping element, and that the while-stepping's body
1230 has valid tracing commands excluding nested while-stepping.
1231 We also need to validate the tracepoint action line in the
1232 context of the tracepoint --- validate_actionline actually
1233 has side effects, like setting the tracepoint's
1234 while-stepping STEP_COUNT, in addition to checking if the
1235 collect/teval actions parse and make sense in the
1236 tracepoint's context. */
1237 for (c = commands; c; c = c->next)
1238 {
1239 if (c->control_type == while_stepping_control)
1240 {
1241 if (b->type == bp_fast_tracepoint)
1242 error (_("The 'while-stepping' command "
1243 "cannot be used for fast tracepoint"));
1244 else if (b->type == bp_static_tracepoint)
1245 error (_("The 'while-stepping' command "
1246 "cannot be used for static tracepoint"));
1247
1248 if (while_stepping)
1249 error (_("The 'while-stepping' command "
1250 "can be used only once"));
1251 else
1252 while_stepping = c;
1253 }
1254
1255 validate_actionline (c->line, b);
1256 }
1257 if (while_stepping)
1258 {
1259 struct command_line *c2;
1260
1261 gdb_assert (while_stepping->body_count == 1);
1262 c2 = while_stepping->body_list[0];
1263 for (; c2; c2 = c2->next)
1264 {
1265 if (c2->control_type == while_stepping_control)
1266 error (_("The 'while-stepping' command cannot be nested"));
1267 }
1268 }
1269 }
1270 else
1271 {
1272 check_no_tracepoint_commands (commands);
1273 }
1274 }
1275
1276 /* Return a vector of all the static tracepoints set at ADDR. The
1277 caller is responsible for releasing the vector. */
1278
1279 VEC(breakpoint_p) *
1280 static_tracepoints_here (CORE_ADDR addr)
1281 {
1282 struct breakpoint *b;
1283 VEC(breakpoint_p) *found = 0;
1284 struct bp_location *loc;
1285
1286 ALL_BREAKPOINTS (b)
1287 if (b->type == bp_static_tracepoint)
1288 {
1289 for (loc = b->loc; loc; loc = loc->next)
1290 if (loc->address == addr)
1291 VEC_safe_push(breakpoint_p, found, b);
1292 }
1293
1294 return found;
1295 }
1296
1297 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1298 validate that only allowed commands are included. */
1299
1300 void
1301 breakpoint_set_commands (struct breakpoint *b,
1302 command_line_up &&commands)
1303 {
1304 validate_commands_for_breakpoint (b, commands.get ());
1305
1306 decref_counted_command_line (&b->commands);
1307 b->commands = alloc_counted_command_line (commands.release ());
1308 observer_notify_breakpoint_modified (b);
1309 }
1310
1311 /* Set the internal `silent' flag on the breakpoint. Note that this
1312 is not the same as the "silent" that may appear in the breakpoint's
1313 commands. */
1314
1315 void
1316 breakpoint_set_silent (struct breakpoint *b, int silent)
1317 {
1318 int old_silent = b->silent;
1319
1320 b->silent = silent;
1321 if (old_silent != silent)
1322 observer_notify_breakpoint_modified (b);
1323 }
1324
1325 /* Set the thread for this breakpoint. If THREAD is -1, make the
1326 breakpoint work for any thread. */
1327
1328 void
1329 breakpoint_set_thread (struct breakpoint *b, int thread)
1330 {
1331 int old_thread = b->thread;
1332
1333 b->thread = thread;
1334 if (old_thread != thread)
1335 observer_notify_breakpoint_modified (b);
1336 }
1337
1338 /* Set the task for this breakpoint. If TASK is 0, make the
1339 breakpoint work for any task. */
1340
1341 void
1342 breakpoint_set_task (struct breakpoint *b, int task)
1343 {
1344 int old_task = b->task;
1345
1346 b->task = task;
1347 if (old_task != task)
1348 observer_notify_breakpoint_modified (b);
1349 }
1350
1351 void
1352 check_tracepoint_command (char *line, void *closure)
1353 {
1354 struct breakpoint *b = (struct breakpoint *) closure;
1355
1356 validate_actionline (line, b);
1357 }
1358
1359 /* A structure used to pass information through
1360 map_breakpoint_numbers. */
1361
1362 struct commands_info
1363 {
1364 /* True if the command was typed at a tty. */
1365 int from_tty;
1366
1367 /* The breakpoint range spec. */
1368 const char *arg;
1369
1370 /* Non-NULL if the body of the commands are being read from this
1371 already-parsed command. */
1372 struct command_line *control;
1373
1374 /* The command lines read from the user, or NULL if they have not
1375 yet been read. */
1376 struct counted_command_line *cmd;
1377 };
1378
1379 /* A callback for map_breakpoint_numbers that sets the commands for
1380 commands_command. */
1381
1382 static void
1383 do_map_commands_command (struct breakpoint *b, void *data)
1384 {
1385 struct commands_info *info = (struct commands_info *) data;
1386
1387 if (info->cmd == NULL)
1388 {
1389 command_line_up l;
1390
1391 if (info->control != NULL)
1392 l = copy_command_lines (info->control->body_list[0]);
1393 else
1394 {
1395 struct cleanup *old_chain;
1396 char *str;
1397
1398 str = xstrprintf (_("Type commands for breakpoint(s) "
1399 "%s, one per line."),
1400 info->arg);
1401
1402 old_chain = make_cleanup (xfree, str);
1403
1404 l = read_command_lines (str,
1405 info->from_tty, 1,
1406 (is_tracepoint (b)
1407 ? check_tracepoint_command : 0),
1408 b);
1409
1410 do_cleanups (old_chain);
1411 }
1412
1413 info->cmd = alloc_counted_command_line (l.release ());
1414 }
1415
1416 /* If a breakpoint was on the list more than once, we don't need to
1417 do anything. */
1418 if (b->commands != info->cmd)
1419 {
1420 validate_commands_for_breakpoint (b, info->cmd->commands);
1421 incref_counted_command_line (info->cmd);
1422 decref_counted_command_line (&b->commands);
1423 b->commands = info->cmd;
1424 observer_notify_breakpoint_modified (b);
1425 }
1426 }
1427
1428 static void
1429 commands_command_1 (const char *arg, int from_tty,
1430 struct command_line *control)
1431 {
1432 struct cleanup *cleanups;
1433 struct commands_info info;
1434
1435 info.from_tty = from_tty;
1436 info.control = control;
1437 info.cmd = NULL;
1438 /* If we read command lines from the user, then `info' will hold an
1439 extra reference to the commands that we must clean up. */
1440 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1441
1442 std::string new_arg;
1443
1444 if (arg == NULL || !*arg)
1445 {
1446 if (breakpoint_count - prev_breakpoint_count > 1)
1447 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1448 breakpoint_count);
1449 else if (breakpoint_count > 0)
1450 new_arg = string_printf ("%d", breakpoint_count);
1451 }
1452 else
1453 new_arg = arg;
1454
1455 info.arg = new_arg.c_str ();
1456
1457 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1458
1459 if (info.cmd == NULL)
1460 error (_("No breakpoints specified."));
1461
1462 do_cleanups (cleanups);
1463 }
1464
1465 static void
1466 commands_command (char *arg, int from_tty)
1467 {
1468 commands_command_1 (arg, from_tty, NULL);
1469 }
1470
1471 /* Like commands_command, but instead of reading the commands from
1472 input stream, takes them from an already parsed command structure.
1473
1474 This is used by cli-script.c to DTRT with breakpoint commands
1475 that are part of if and while bodies. */
1476 enum command_control_type
1477 commands_from_control_command (const char *arg, struct command_line *cmd)
1478 {
1479 commands_command_1 (arg, 0, cmd);
1480 return simple_control;
1481 }
1482
1483 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1484
1485 static int
1486 bp_location_has_shadow (struct bp_location *bl)
1487 {
1488 if (bl->loc_type != bp_loc_software_breakpoint)
1489 return 0;
1490 if (!bl->inserted)
1491 return 0;
1492 if (bl->target_info.shadow_len == 0)
1493 /* BL isn't valid, or doesn't shadow memory. */
1494 return 0;
1495 return 1;
1496 }
1497
1498 /* Update BUF, which is LEN bytes read from the target address
1499 MEMADDR, by replacing a memory breakpoint with its shadowed
1500 contents.
1501
1502 If READBUF is not NULL, this buffer must not overlap with the of
1503 the breakpoint location's shadow_contents buffer. Otherwise, a
1504 failed assertion internal error will be raised. */
1505
1506 static void
1507 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1508 const gdb_byte *writebuf_org,
1509 ULONGEST memaddr, LONGEST len,
1510 struct bp_target_info *target_info,
1511 struct gdbarch *gdbarch)
1512 {
1513 /* Now do full processing of the found relevant range of elements. */
1514 CORE_ADDR bp_addr = 0;
1515 int bp_size = 0;
1516 int bptoffset = 0;
1517
1518 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1519 current_program_space->aspace, 0))
1520 {
1521 /* The breakpoint is inserted in a different address space. */
1522 return;
1523 }
1524
1525 /* Addresses and length of the part of the breakpoint that
1526 we need to copy. */
1527 bp_addr = target_info->placed_address;
1528 bp_size = target_info->shadow_len;
1529
1530 if (bp_addr + bp_size <= memaddr)
1531 {
1532 /* The breakpoint is entirely before the chunk of memory we are
1533 reading. */
1534 return;
1535 }
1536
1537 if (bp_addr >= memaddr + len)
1538 {
1539 /* The breakpoint is entirely after the chunk of memory we are
1540 reading. */
1541 return;
1542 }
1543
1544 /* Offset within shadow_contents. */
1545 if (bp_addr < memaddr)
1546 {
1547 /* Only copy the second part of the breakpoint. */
1548 bp_size -= memaddr - bp_addr;
1549 bptoffset = memaddr - bp_addr;
1550 bp_addr = memaddr;
1551 }
1552
1553 if (bp_addr + bp_size > memaddr + len)
1554 {
1555 /* Only copy the first part of the breakpoint. */
1556 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1557 }
1558
1559 if (readbuf != NULL)
1560 {
1561 /* Verify that the readbuf buffer does not overlap with the
1562 shadow_contents buffer. */
1563 gdb_assert (target_info->shadow_contents >= readbuf + len
1564 || readbuf >= (target_info->shadow_contents
1565 + target_info->shadow_len));
1566
1567 /* Update the read buffer with this inserted breakpoint's
1568 shadow. */
1569 memcpy (readbuf + bp_addr - memaddr,
1570 target_info->shadow_contents + bptoffset, bp_size);
1571 }
1572 else
1573 {
1574 const unsigned char *bp;
1575 CORE_ADDR addr = target_info->reqstd_address;
1576 int placed_size;
1577
1578 /* Update the shadow with what we want to write to memory. */
1579 memcpy (target_info->shadow_contents + bptoffset,
1580 writebuf_org + bp_addr - memaddr, bp_size);
1581
1582 /* Determine appropriate breakpoint contents and size for this
1583 address. */
1584 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1585
1586 /* Update the final write buffer with this inserted
1587 breakpoint's INSN. */
1588 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1589 }
1590 }
1591
1592 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1593 by replacing any memory breakpoints with their shadowed contents.
1594
1595 If READBUF is not NULL, this buffer must not overlap with any of
1596 the breakpoint location's shadow_contents buffers. Otherwise,
1597 a failed assertion internal error will be raised.
1598
1599 The range of shadowed area by each bp_location is:
1600 bl->address - bp_locations_placed_address_before_address_max
1601 up to bl->address + bp_locations_shadow_len_after_address_max
1602 The range we were requested to resolve shadows for is:
1603 memaddr ... memaddr + len
1604 Thus the safe cutoff boundaries for performance optimization are
1605 memaddr + len <= (bl->address
1606 - bp_locations_placed_address_before_address_max)
1607 and:
1608 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1609
1610 void
1611 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1612 const gdb_byte *writebuf_org,
1613 ULONGEST memaddr, LONGEST len)
1614 {
1615 /* Left boundary, right boundary and median element of our binary
1616 search. */
1617 unsigned bc_l, bc_r, bc;
1618
1619 /* Find BC_L which is a leftmost element which may affect BUF
1620 content. It is safe to report lower value but a failure to
1621 report higher one. */
1622
1623 bc_l = 0;
1624 bc_r = bp_locations_count;
1625 while (bc_l + 1 < bc_r)
1626 {
1627 struct bp_location *bl;
1628
1629 bc = (bc_l + bc_r) / 2;
1630 bl = bp_locations[bc];
1631
1632 /* Check first BL->ADDRESS will not overflow due to the added
1633 constant. Then advance the left boundary only if we are sure
1634 the BC element can in no way affect the BUF content (MEMADDR
1635 to MEMADDR + LEN range).
1636
1637 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1638 offset so that we cannot miss a breakpoint with its shadow
1639 range tail still reaching MEMADDR. */
1640
1641 if ((bl->address + bp_locations_shadow_len_after_address_max
1642 >= bl->address)
1643 && (bl->address + bp_locations_shadow_len_after_address_max
1644 <= memaddr))
1645 bc_l = bc;
1646 else
1647 bc_r = bc;
1648 }
1649
1650 /* Due to the binary search above, we need to make sure we pick the
1651 first location that's at BC_L's address. E.g., if there are
1652 multiple locations at the same address, BC_L may end up pointing
1653 at a duplicate location, and miss the "master"/"inserted"
1654 location. Say, given locations L1, L2 and L3 at addresses A and
1655 B:
1656
1657 L1@A, L2@A, L3@B, ...
1658
1659 BC_L could end up pointing at location L2, while the "master"
1660 location could be L1. Since the `loc->inserted' flag is only set
1661 on "master" locations, we'd forget to restore the shadow of L1
1662 and L2. */
1663 while (bc_l > 0
1664 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1665 bc_l--;
1666
1667 /* Now do full processing of the found relevant range of elements. */
1668
1669 for (bc = bc_l; bc < bp_locations_count; bc++)
1670 {
1671 struct bp_location *bl = bp_locations[bc];
1672
1673 /* bp_location array has BL->OWNER always non-NULL. */
1674 if (bl->owner->type == bp_none)
1675 warning (_("reading through apparently deleted breakpoint #%d?"),
1676 bl->owner->number);
1677
1678 /* Performance optimization: any further element can no longer affect BUF
1679 content. */
1680
1681 if (bl->address >= bp_locations_placed_address_before_address_max
1682 && memaddr + len <= (bl->address
1683 - bp_locations_placed_address_before_address_max))
1684 break;
1685
1686 if (!bp_location_has_shadow (bl))
1687 continue;
1688
1689 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1690 memaddr, len, &bl->target_info, bl->gdbarch);
1691 }
1692 }
1693
1694 \f
1695
1696 /* Return true if BPT is either a software breakpoint or a hardware
1697 breakpoint. */
1698
1699 int
1700 is_breakpoint (const struct breakpoint *bpt)
1701 {
1702 return (bpt->type == bp_breakpoint
1703 || bpt->type == bp_hardware_breakpoint
1704 || bpt->type == bp_dprintf);
1705 }
1706
1707 /* Return true if BPT is of any hardware watchpoint kind. */
1708
1709 static int
1710 is_hardware_watchpoint (const struct breakpoint *bpt)
1711 {
1712 return (bpt->type == bp_hardware_watchpoint
1713 || bpt->type == bp_read_watchpoint
1714 || bpt->type == bp_access_watchpoint);
1715 }
1716
1717 /* Return true if BPT is of any watchpoint kind, hardware or
1718 software. */
1719
1720 int
1721 is_watchpoint (const struct breakpoint *bpt)
1722 {
1723 return (is_hardware_watchpoint (bpt)
1724 || bpt->type == bp_watchpoint);
1725 }
1726
1727 /* Returns true if the current thread and its running state are safe
1728 to evaluate or update watchpoint B. Watchpoints on local
1729 expressions need to be evaluated in the context of the thread that
1730 was current when the watchpoint was created, and, that thread needs
1731 to be stopped to be able to select the correct frame context.
1732 Watchpoints on global expressions can be evaluated on any thread,
1733 and in any state. It is presently left to the target allowing
1734 memory accesses when threads are running. */
1735
1736 static int
1737 watchpoint_in_thread_scope (struct watchpoint *b)
1738 {
1739 return (b->base.pspace == current_program_space
1740 && (ptid_equal (b->watchpoint_thread, null_ptid)
1741 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1742 && !is_executing (inferior_ptid))));
1743 }
1744
1745 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1746 associated bp_watchpoint_scope breakpoint. */
1747
1748 static void
1749 watchpoint_del_at_next_stop (struct watchpoint *w)
1750 {
1751 struct breakpoint *b = &w->base;
1752
1753 if (b->related_breakpoint != b)
1754 {
1755 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1756 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1757 b->related_breakpoint->disposition = disp_del_at_next_stop;
1758 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1759 b->related_breakpoint = b;
1760 }
1761 b->disposition = disp_del_at_next_stop;
1762 }
1763
1764 /* Extract a bitfield value from value VAL using the bit parameters contained in
1765 watchpoint W. */
1766
1767 static struct value *
1768 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1769 {
1770 struct value *bit_val;
1771
1772 if (val == NULL)
1773 return NULL;
1774
1775 bit_val = allocate_value (value_type (val));
1776
1777 unpack_value_bitfield (bit_val,
1778 w->val_bitpos,
1779 w->val_bitsize,
1780 value_contents_for_printing (val),
1781 value_offset (val),
1782 val);
1783
1784 return bit_val;
1785 }
1786
1787 /* Allocate a dummy location and add it to B, which must be a software
1788 watchpoint. This is required because even if a software watchpoint
1789 is not watching any memory, bpstat_stop_status requires a location
1790 to be able to report stops. */
1791
1792 static void
1793 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1794 struct program_space *pspace)
1795 {
1796 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1797
1798 b->loc = allocate_bp_location (b);
1799 b->loc->pspace = pspace;
1800 b->loc->address = -1;
1801 b->loc->length = -1;
1802 }
1803
1804 /* Returns true if B is a software watchpoint that is not watching any
1805 memory (e.g., "watch $pc"). */
1806
1807 static int
1808 is_no_memory_software_watchpoint (struct breakpoint *b)
1809 {
1810 return (b->type == bp_watchpoint
1811 && b->loc != NULL
1812 && b->loc->next == NULL
1813 && b->loc->address == -1
1814 && b->loc->length == -1);
1815 }
1816
1817 /* Assuming that B is a watchpoint:
1818 - Reparse watchpoint expression, if REPARSE is non-zero
1819 - Evaluate expression and store the result in B->val
1820 - Evaluate the condition if there is one, and store the result
1821 in b->loc->cond.
1822 - Update the list of values that must be watched in B->loc.
1823
1824 If the watchpoint disposition is disp_del_at_next_stop, then do
1825 nothing. If this is local watchpoint that is out of scope, delete
1826 it.
1827
1828 Even with `set breakpoint always-inserted on' the watchpoints are
1829 removed + inserted on each stop here. Normal breakpoints must
1830 never be removed because they might be missed by a running thread
1831 when debugging in non-stop mode. On the other hand, hardware
1832 watchpoints (is_hardware_watchpoint; processed here) are specific
1833 to each LWP since they are stored in each LWP's hardware debug
1834 registers. Therefore, such LWP must be stopped first in order to
1835 be able to modify its hardware watchpoints.
1836
1837 Hardware watchpoints must be reset exactly once after being
1838 presented to the user. It cannot be done sooner, because it would
1839 reset the data used to present the watchpoint hit to the user. And
1840 it must not be done later because it could display the same single
1841 watchpoint hit during multiple GDB stops. Note that the latter is
1842 relevant only to the hardware watchpoint types bp_read_watchpoint
1843 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1844 not user-visible - its hit is suppressed if the memory content has
1845 not changed.
1846
1847 The following constraints influence the location where we can reset
1848 hardware watchpoints:
1849
1850 * target_stopped_by_watchpoint and target_stopped_data_address are
1851 called several times when GDB stops.
1852
1853 [linux]
1854 * Multiple hardware watchpoints can be hit at the same time,
1855 causing GDB to stop. GDB only presents one hardware watchpoint
1856 hit at a time as the reason for stopping, and all the other hits
1857 are presented later, one after the other, each time the user
1858 requests the execution to be resumed. Execution is not resumed
1859 for the threads still having pending hit event stored in
1860 LWP_INFO->STATUS. While the watchpoint is already removed from
1861 the inferior on the first stop the thread hit event is kept being
1862 reported from its cached value by linux_nat_stopped_data_address
1863 until the real thread resume happens after the watchpoint gets
1864 presented and thus its LWP_INFO->STATUS gets reset.
1865
1866 Therefore the hardware watchpoint hit can get safely reset on the
1867 watchpoint removal from inferior. */
1868
1869 static void
1870 update_watchpoint (struct watchpoint *b, int reparse)
1871 {
1872 int within_current_scope;
1873 struct frame_id saved_frame_id;
1874 int frame_saved;
1875
1876 /* If this is a local watchpoint, we only want to check if the
1877 watchpoint frame is in scope if the current thread is the thread
1878 that was used to create the watchpoint. */
1879 if (!watchpoint_in_thread_scope (b))
1880 return;
1881
1882 if (b->base.disposition == disp_del_at_next_stop)
1883 return;
1884
1885 frame_saved = 0;
1886
1887 /* Determine if the watchpoint is within scope. */
1888 if (b->exp_valid_block == NULL)
1889 within_current_scope = 1;
1890 else
1891 {
1892 struct frame_info *fi = get_current_frame ();
1893 struct gdbarch *frame_arch = get_frame_arch (fi);
1894 CORE_ADDR frame_pc = get_frame_pc (fi);
1895
1896 /* If we're at a point where the stack has been destroyed
1897 (e.g. in a function epilogue), unwinding may not work
1898 properly. Do not attempt to recreate locations at this
1899 point. See similar comments in watchpoint_check. */
1900 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1901 return;
1902
1903 /* Save the current frame's ID so we can restore it after
1904 evaluating the watchpoint expression on its own frame. */
1905 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1906 took a frame parameter, so that we didn't have to change the
1907 selected frame. */
1908 frame_saved = 1;
1909 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1910
1911 fi = frame_find_by_id (b->watchpoint_frame);
1912 within_current_scope = (fi != NULL);
1913 if (within_current_scope)
1914 select_frame (fi);
1915 }
1916
1917 /* We don't free locations. They are stored in the bp_location array
1918 and update_global_location_list will eventually delete them and
1919 remove breakpoints if needed. */
1920 b->base.loc = NULL;
1921
1922 if (within_current_scope && reparse)
1923 {
1924 const char *s;
1925
1926 b->exp.reset ();
1927 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1928 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1929 /* If the meaning of expression itself changed, the old value is
1930 no longer relevant. We don't want to report a watchpoint hit
1931 to the user when the old value and the new value may actually
1932 be completely different objects. */
1933 value_free (b->val);
1934 b->val = NULL;
1935 b->val_valid = 0;
1936
1937 /* Note that unlike with breakpoints, the watchpoint's condition
1938 expression is stored in the breakpoint object, not in the
1939 locations (re)created below. */
1940 if (b->base.cond_string != NULL)
1941 {
1942 b->cond_exp.reset ();
1943
1944 s = b->base.cond_string;
1945 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1946 }
1947 }
1948
1949 /* If we failed to parse the expression, for example because
1950 it refers to a global variable in a not-yet-loaded shared library,
1951 don't try to insert watchpoint. We don't automatically delete
1952 such watchpoint, though, since failure to parse expression
1953 is different from out-of-scope watchpoint. */
1954 if (!target_has_execution)
1955 {
1956 /* Without execution, memory can't change. No use to try and
1957 set watchpoint locations. The watchpoint will be reset when
1958 the target gains execution, through breakpoint_re_set. */
1959 if (!can_use_hw_watchpoints)
1960 {
1961 if (b->base.ops->works_in_software_mode (&b->base))
1962 b->base.type = bp_watchpoint;
1963 else
1964 error (_("Can't set read/access watchpoint when "
1965 "hardware watchpoints are disabled."));
1966 }
1967 }
1968 else if (within_current_scope && b->exp)
1969 {
1970 int pc = 0;
1971 struct value *val_chain, *v, *result, *next;
1972 struct program_space *frame_pspace;
1973
1974 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1975
1976 /* Avoid setting b->val if it's already set. The meaning of
1977 b->val is 'the last value' user saw, and we should update
1978 it only if we reported that last value to user. As it
1979 happens, the code that reports it updates b->val directly.
1980 We don't keep track of the memory value for masked
1981 watchpoints. */
1982 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1983 {
1984 if (b->val_bitsize != 0)
1985 {
1986 v = extract_bitfield_from_watchpoint_value (b, v);
1987 if (v != NULL)
1988 release_value (v);
1989 }
1990 b->val = v;
1991 b->val_valid = 1;
1992 }
1993
1994 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1995
1996 /* Look at each value on the value chain. */
1997 for (v = val_chain; v; v = value_next (v))
1998 {
1999 /* If it's a memory location, and GDB actually needed
2000 its contents to evaluate the expression, then we
2001 must watch it. If the first value returned is
2002 still lazy, that means an error occurred reading it;
2003 watch it anyway in case it becomes readable. */
2004 if (VALUE_LVAL (v) == lval_memory
2005 && (v == val_chain || ! value_lazy (v)))
2006 {
2007 struct type *vtype = check_typedef (value_type (v));
2008
2009 /* We only watch structs and arrays if user asked
2010 for it explicitly, never if they just happen to
2011 appear in the middle of some value chain. */
2012 if (v == result
2013 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2014 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2015 {
2016 CORE_ADDR addr;
2017 enum target_hw_bp_type type;
2018 struct bp_location *loc, **tmp;
2019 int bitpos = 0, bitsize = 0;
2020
2021 if (value_bitsize (v) != 0)
2022 {
2023 /* Extract the bit parameters out from the bitfield
2024 sub-expression. */
2025 bitpos = value_bitpos (v);
2026 bitsize = value_bitsize (v);
2027 }
2028 else if (v == result && b->val_bitsize != 0)
2029 {
2030 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2031 lvalue whose bit parameters are saved in the fields
2032 VAL_BITPOS and VAL_BITSIZE. */
2033 bitpos = b->val_bitpos;
2034 bitsize = b->val_bitsize;
2035 }
2036
2037 addr = value_address (v);
2038 if (bitsize != 0)
2039 {
2040 /* Skip the bytes that don't contain the bitfield. */
2041 addr += bitpos / 8;
2042 }
2043
2044 type = hw_write;
2045 if (b->base.type == bp_read_watchpoint)
2046 type = hw_read;
2047 else if (b->base.type == bp_access_watchpoint)
2048 type = hw_access;
2049
2050 loc = allocate_bp_location (&b->base);
2051 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2052 ;
2053 *tmp = loc;
2054 loc->gdbarch = get_type_arch (value_type (v));
2055
2056 loc->pspace = frame_pspace;
2057 loc->address = addr;
2058
2059 if (bitsize != 0)
2060 {
2061 /* Just cover the bytes that make up the bitfield. */
2062 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2063 }
2064 else
2065 loc->length = TYPE_LENGTH (value_type (v));
2066
2067 loc->watchpoint_type = type;
2068 }
2069 }
2070 }
2071
2072 /* Change the type of breakpoint between hardware assisted or
2073 an ordinary watchpoint depending on the hardware support
2074 and free hardware slots. REPARSE is set when the inferior
2075 is started. */
2076 if (reparse)
2077 {
2078 int reg_cnt;
2079 enum bp_loc_type loc_type;
2080 struct bp_location *bl;
2081
2082 reg_cnt = can_use_hardware_watchpoint (val_chain);
2083
2084 if (reg_cnt)
2085 {
2086 int i, target_resources_ok, other_type_used;
2087 enum bptype type;
2088
2089 /* Use an exact watchpoint when there's only one memory region to be
2090 watched, and only one debug register is needed to watch it. */
2091 b->exact = target_exact_watchpoints && reg_cnt == 1;
2092
2093 /* We need to determine how many resources are already
2094 used for all other hardware watchpoints plus this one
2095 to see if we still have enough resources to also fit
2096 this watchpoint in as well. */
2097
2098 /* If this is a software watchpoint, we try to turn it
2099 to a hardware one -- count resources as if B was of
2100 hardware watchpoint type. */
2101 type = b->base.type;
2102 if (type == bp_watchpoint)
2103 type = bp_hardware_watchpoint;
2104
2105 /* This watchpoint may or may not have been placed on
2106 the list yet at this point (it won't be in the list
2107 if we're trying to create it for the first time,
2108 through watch_command), so always account for it
2109 manually. */
2110
2111 /* Count resources used by all watchpoints except B. */
2112 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2113
2114 /* Add in the resources needed for B. */
2115 i += hw_watchpoint_use_count (&b->base);
2116
2117 target_resources_ok
2118 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2119 if (target_resources_ok <= 0)
2120 {
2121 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2122
2123 if (target_resources_ok == 0 && !sw_mode)
2124 error (_("Target does not support this type of "
2125 "hardware watchpoint."));
2126 else if (target_resources_ok < 0 && !sw_mode)
2127 error (_("There are not enough available hardware "
2128 "resources for this watchpoint."));
2129
2130 /* Downgrade to software watchpoint. */
2131 b->base.type = bp_watchpoint;
2132 }
2133 else
2134 {
2135 /* If this was a software watchpoint, we've just
2136 found we have enough resources to turn it to a
2137 hardware watchpoint. Otherwise, this is a
2138 nop. */
2139 b->base.type = type;
2140 }
2141 }
2142 else if (!b->base.ops->works_in_software_mode (&b->base))
2143 {
2144 if (!can_use_hw_watchpoints)
2145 error (_("Can't set read/access watchpoint when "
2146 "hardware watchpoints are disabled."));
2147 else
2148 error (_("Expression cannot be implemented with "
2149 "read/access watchpoint."));
2150 }
2151 else
2152 b->base.type = bp_watchpoint;
2153
2154 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2155 : bp_loc_hardware_watchpoint);
2156 for (bl = b->base.loc; bl; bl = bl->next)
2157 bl->loc_type = loc_type;
2158 }
2159
2160 for (v = val_chain; v; v = next)
2161 {
2162 next = value_next (v);
2163 if (v != b->val)
2164 value_free (v);
2165 }
2166
2167 /* If a software watchpoint is not watching any memory, then the
2168 above left it without any location set up. But,
2169 bpstat_stop_status requires a location to be able to report
2170 stops, so make sure there's at least a dummy one. */
2171 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2172 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2173 }
2174 else if (!within_current_scope)
2175 {
2176 printf_filtered (_("\
2177 Watchpoint %d deleted because the program has left the block\n\
2178 in which its expression is valid.\n"),
2179 b->base.number);
2180 watchpoint_del_at_next_stop (b);
2181 }
2182
2183 /* Restore the selected frame. */
2184 if (frame_saved)
2185 select_frame (frame_find_by_id (saved_frame_id));
2186 }
2187
2188
2189 /* Returns 1 iff breakpoint location should be
2190 inserted in the inferior. We don't differentiate the type of BL's owner
2191 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2192 breakpoint_ops is not defined, because in insert_bp_location,
2193 tracepoint's insert_location will not be called. */
2194 static int
2195 should_be_inserted (struct bp_location *bl)
2196 {
2197 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2198 return 0;
2199
2200 if (bl->owner->disposition == disp_del_at_next_stop)
2201 return 0;
2202
2203 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2204 return 0;
2205
2206 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2207 return 0;
2208
2209 /* This is set for example, when we're attached to the parent of a
2210 vfork, and have detached from the child. The child is running
2211 free, and we expect it to do an exec or exit, at which point the
2212 OS makes the parent schedulable again (and the target reports
2213 that the vfork is done). Until the child is done with the shared
2214 memory region, do not insert breakpoints in the parent, otherwise
2215 the child could still trip on the parent's breakpoints. Since
2216 the parent is blocked anyway, it won't miss any breakpoint. */
2217 if (bl->pspace->breakpoints_not_allowed)
2218 return 0;
2219
2220 /* Don't insert a breakpoint if we're trying to step past its
2221 location, except if the breakpoint is a single-step breakpoint,
2222 and the breakpoint's thread is the thread which is stepping past
2223 a breakpoint. */
2224 if ((bl->loc_type == bp_loc_software_breakpoint
2225 || bl->loc_type == bp_loc_hardware_breakpoint)
2226 && stepping_past_instruction_at (bl->pspace->aspace,
2227 bl->address)
2228 /* The single-step breakpoint may be inserted at the location
2229 we're trying to step if the instruction branches to itself.
2230 However, the instruction won't be executed at all and it may
2231 break the semantics of the instruction, for example, the
2232 instruction is a conditional branch or updates some flags.
2233 We can't fix it unless GDB is able to emulate the instruction
2234 or switch to displaced stepping. */
2235 && !(bl->owner->type == bp_single_step
2236 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2237 {
2238 if (debug_infrun)
2239 {
2240 fprintf_unfiltered (gdb_stdlog,
2241 "infrun: skipping breakpoint: "
2242 "stepping past insn at: %s\n",
2243 paddress (bl->gdbarch, bl->address));
2244 }
2245 return 0;
2246 }
2247
2248 /* Don't insert watchpoints if we're trying to step past the
2249 instruction that triggered one. */
2250 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2251 && stepping_past_nonsteppable_watchpoint ())
2252 {
2253 if (debug_infrun)
2254 {
2255 fprintf_unfiltered (gdb_stdlog,
2256 "infrun: stepping past non-steppable watchpoint. "
2257 "skipping watchpoint at %s:%d\n",
2258 paddress (bl->gdbarch, bl->address),
2259 bl->length);
2260 }
2261 return 0;
2262 }
2263
2264 return 1;
2265 }
2266
2267 /* Same as should_be_inserted but does the check assuming
2268 that the location is not duplicated. */
2269
2270 static int
2271 unduplicated_should_be_inserted (struct bp_location *bl)
2272 {
2273 int result;
2274 const int save_duplicate = bl->duplicate;
2275
2276 bl->duplicate = 0;
2277 result = should_be_inserted (bl);
2278 bl->duplicate = save_duplicate;
2279 return result;
2280 }
2281
2282 /* Parses a conditional described by an expression COND into an
2283 agent expression bytecode suitable for evaluation
2284 by the bytecode interpreter. Return NULL if there was
2285 any error during parsing. */
2286
2287 static agent_expr_up
2288 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2289 {
2290 if (cond == NULL)
2291 return NULL;
2292
2293 agent_expr_up aexpr;
2294
2295 /* We don't want to stop processing, so catch any errors
2296 that may show up. */
2297 TRY
2298 {
2299 aexpr = gen_eval_for_expr (scope, cond);
2300 }
2301
2302 CATCH (ex, RETURN_MASK_ERROR)
2303 {
2304 /* If we got here, it means the condition could not be parsed to a valid
2305 bytecode expression and thus can't be evaluated on the target's side.
2306 It's no use iterating through the conditions. */
2307 }
2308 END_CATCH
2309
2310 /* We have a valid agent expression. */
2311 return aexpr;
2312 }
2313
2314 /* Based on location BL, create a list of breakpoint conditions to be
2315 passed on to the target. If we have duplicated locations with different
2316 conditions, we will add such conditions to the list. The idea is that the
2317 target will evaluate the list of conditions and will only notify GDB when
2318 one of them is true. */
2319
2320 static void
2321 build_target_condition_list (struct bp_location *bl)
2322 {
2323 struct bp_location **locp = NULL, **loc2p;
2324 int null_condition_or_parse_error = 0;
2325 int modified = bl->needs_update;
2326 struct bp_location *loc;
2327
2328 /* Release conditions left over from a previous insert. */
2329 bl->target_info.conditions.clear ();
2330
2331 /* This is only meaningful if the target is
2332 evaluating conditions and if the user has
2333 opted for condition evaluation on the target's
2334 side. */
2335 if (gdb_evaluates_breakpoint_condition_p ()
2336 || !target_supports_evaluation_of_breakpoint_conditions ())
2337 return;
2338
2339 /* Do a first pass to check for locations with no assigned
2340 conditions or conditions that fail to parse to a valid agent expression
2341 bytecode. If any of these happen, then it's no use to send conditions
2342 to the target since this location will always trigger and generate a
2343 response back to GDB. */
2344 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2345 {
2346 loc = (*loc2p);
2347 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2348 {
2349 if (modified)
2350 {
2351 /* Re-parse the conditions since something changed. In that
2352 case we already freed the condition bytecodes (see
2353 force_breakpoint_reinsertion). We just
2354 need to parse the condition to bytecodes again. */
2355 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2356 loc->cond.get ());
2357 }
2358
2359 /* If we have a NULL bytecode expression, it means something
2360 went wrong or we have a null condition expression. */
2361 if (!loc->cond_bytecode)
2362 {
2363 null_condition_or_parse_error = 1;
2364 break;
2365 }
2366 }
2367 }
2368
2369 /* If any of these happened, it means we will have to evaluate the conditions
2370 for the location's address on gdb's side. It is no use keeping bytecodes
2371 for all the other duplicate locations, thus we free all of them here.
2372
2373 This is so we have a finer control over which locations' conditions are
2374 being evaluated by GDB or the remote stub. */
2375 if (null_condition_or_parse_error)
2376 {
2377 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2378 {
2379 loc = (*loc2p);
2380 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2381 {
2382 /* Only go as far as the first NULL bytecode is
2383 located. */
2384 if (!loc->cond_bytecode)
2385 return;
2386
2387 loc->cond_bytecode.reset ();
2388 }
2389 }
2390 }
2391
2392 /* No NULL conditions or failed bytecode generation. Build a condition list
2393 for this location's address. */
2394 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2395 {
2396 loc = (*loc2p);
2397 if (loc->cond
2398 && is_breakpoint (loc->owner)
2399 && loc->pspace->num == bl->pspace->num
2400 && loc->owner->enable_state == bp_enabled
2401 && loc->enabled)
2402 {
2403 /* Add the condition to the vector. This will be used later
2404 to send the conditions to the target. */
2405 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2406 }
2407 }
2408
2409 return;
2410 }
2411
2412 /* Parses a command described by string CMD into an agent expression
2413 bytecode suitable for evaluation by the bytecode interpreter.
2414 Return NULL if there was any error during parsing. */
2415
2416 static agent_expr_up
2417 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2418 {
2419 struct cleanup *old_cleanups = 0;
2420 struct expression **argvec;
2421 const char *cmdrest;
2422 const char *format_start, *format_end;
2423 struct format_piece *fpieces;
2424 int nargs;
2425 struct gdbarch *gdbarch = get_current_arch ();
2426
2427 if (cmd == NULL)
2428 return NULL;
2429
2430 cmdrest = cmd;
2431
2432 if (*cmdrest == ',')
2433 ++cmdrest;
2434 cmdrest = skip_spaces_const (cmdrest);
2435
2436 if (*cmdrest++ != '"')
2437 error (_("No format string following the location"));
2438
2439 format_start = cmdrest;
2440
2441 fpieces = parse_format_string (&cmdrest);
2442
2443 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2444
2445 format_end = cmdrest;
2446
2447 if (*cmdrest++ != '"')
2448 error (_("Bad format string, non-terminated '\"'."));
2449
2450 cmdrest = skip_spaces_const (cmdrest);
2451
2452 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2453 error (_("Invalid argument syntax"));
2454
2455 if (*cmdrest == ',')
2456 cmdrest++;
2457 cmdrest = skip_spaces_const (cmdrest);
2458
2459 /* For each argument, make an expression. */
2460
2461 argvec = (struct expression **) alloca (strlen (cmd)
2462 * sizeof (struct expression *));
2463
2464 nargs = 0;
2465 while (*cmdrest != '\0')
2466 {
2467 const char *cmd1;
2468
2469 cmd1 = cmdrest;
2470 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2471 argvec[nargs++] = expr.release ();
2472 cmdrest = cmd1;
2473 if (*cmdrest == ',')
2474 ++cmdrest;
2475 }
2476
2477 agent_expr_up aexpr;
2478
2479 /* We don't want to stop processing, so catch any errors
2480 that may show up. */
2481 TRY
2482 {
2483 aexpr = gen_printf (scope, gdbarch, 0, 0,
2484 format_start, format_end - format_start,
2485 fpieces, nargs, argvec);
2486 }
2487 CATCH (ex, RETURN_MASK_ERROR)
2488 {
2489 /* If we got here, it means the command could not be parsed to a valid
2490 bytecode expression and thus can't be evaluated on the target's side.
2491 It's no use iterating through the other commands. */
2492 }
2493 END_CATCH
2494
2495 do_cleanups (old_cleanups);
2496
2497 /* We have a valid agent expression, return it. */
2498 return aexpr;
2499 }
2500
2501 /* Based on location BL, create a list of breakpoint commands to be
2502 passed on to the target. If we have duplicated locations with
2503 different commands, we will add any such to the list. */
2504
2505 static void
2506 build_target_command_list (struct bp_location *bl)
2507 {
2508 struct bp_location **locp = NULL, **loc2p;
2509 int null_command_or_parse_error = 0;
2510 int modified = bl->needs_update;
2511 struct bp_location *loc;
2512
2513 /* Clear commands left over from a previous insert. */
2514 bl->target_info.tcommands.clear ();
2515
2516 if (!target_can_run_breakpoint_commands ())
2517 return;
2518
2519 /* For now, limit to agent-style dprintf breakpoints. */
2520 if (dprintf_style != dprintf_style_agent)
2521 return;
2522
2523 /* For now, if we have any duplicate location that isn't a dprintf,
2524 don't install the target-side commands, as that would make the
2525 breakpoint not be reported to the core, and we'd lose
2526 control. */
2527 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2528 {
2529 loc = (*loc2p);
2530 if (is_breakpoint (loc->owner)
2531 && loc->pspace->num == bl->pspace->num
2532 && loc->owner->type != bp_dprintf)
2533 return;
2534 }
2535
2536 /* Do a first pass to check for locations with no assigned
2537 conditions or conditions that fail to parse to a valid agent expression
2538 bytecode. If any of these happen, then it's no use to send conditions
2539 to the target since this location will always trigger and generate a
2540 response back to GDB. */
2541 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2542 {
2543 loc = (*loc2p);
2544 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2545 {
2546 if (modified)
2547 {
2548 /* Re-parse the commands since something changed. In that
2549 case we already freed the command bytecodes (see
2550 force_breakpoint_reinsertion). We just
2551 need to parse the command to bytecodes again. */
2552 loc->cmd_bytecode
2553 = parse_cmd_to_aexpr (bl->address,
2554 loc->owner->extra_string);
2555 }
2556
2557 /* If we have a NULL bytecode expression, it means something
2558 went wrong or we have a null command expression. */
2559 if (!loc->cmd_bytecode)
2560 {
2561 null_command_or_parse_error = 1;
2562 break;
2563 }
2564 }
2565 }
2566
2567 /* If anything failed, then we're not doing target-side commands,
2568 and so clean up. */
2569 if (null_command_or_parse_error)
2570 {
2571 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2572 {
2573 loc = (*loc2p);
2574 if (is_breakpoint (loc->owner)
2575 && loc->pspace->num == bl->pspace->num)
2576 {
2577 /* Only go as far as the first NULL bytecode is
2578 located. */
2579 if (loc->cmd_bytecode == NULL)
2580 return;
2581
2582 loc->cmd_bytecode.reset ();
2583 }
2584 }
2585 }
2586
2587 /* No NULL commands or failed bytecode generation. Build a command list
2588 for this location's address. */
2589 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2590 {
2591 loc = (*loc2p);
2592 if (loc->owner->extra_string
2593 && is_breakpoint (loc->owner)
2594 && loc->pspace->num == bl->pspace->num
2595 && loc->owner->enable_state == bp_enabled
2596 && loc->enabled)
2597 {
2598 /* Add the command to the vector. This will be used later
2599 to send the commands to the target. */
2600 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2601 }
2602 }
2603
2604 bl->target_info.persist = 0;
2605 /* Maybe flag this location as persistent. */
2606 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2607 bl->target_info.persist = 1;
2608 }
2609
2610 /* Return the kind of breakpoint on address *ADDR. Get the kind
2611 of breakpoint according to ADDR except single-step breakpoint.
2612 Get the kind of single-step breakpoint according to the current
2613 registers state. */
2614
2615 static int
2616 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2617 {
2618 if (bl->owner->type == bp_single_step)
2619 {
2620 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2621 struct regcache *regcache;
2622
2623 regcache = get_thread_regcache (thr->ptid);
2624
2625 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2626 regcache, addr);
2627 }
2628 else
2629 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2630 }
2631
2632 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2633 location. Any error messages are printed to TMP_ERROR_STREAM; and
2634 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2635 Returns 0 for success, 1 if the bp_location type is not supported or
2636 -1 for failure.
2637
2638 NOTE drow/2003-09-09: This routine could be broken down to an
2639 object-style method for each breakpoint or catchpoint type. */
2640 static int
2641 insert_bp_location (struct bp_location *bl,
2642 struct ui_file *tmp_error_stream,
2643 int *disabled_breaks,
2644 int *hw_breakpoint_error,
2645 int *hw_bp_error_explained_already)
2646 {
2647 enum errors bp_err = GDB_NO_ERROR;
2648 const char *bp_err_message = NULL;
2649
2650 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2651 return 0;
2652
2653 /* Note we don't initialize bl->target_info, as that wipes out
2654 the breakpoint location's shadow_contents if the breakpoint
2655 is still inserted at that location. This in turn breaks
2656 target_read_memory which depends on these buffers when
2657 a memory read is requested at the breakpoint location:
2658 Once the target_info has been wiped, we fail to see that
2659 we have a breakpoint inserted at that address and thus
2660 read the breakpoint instead of returning the data saved in
2661 the breakpoint location's shadow contents. */
2662 bl->target_info.reqstd_address = bl->address;
2663 bl->target_info.placed_address_space = bl->pspace->aspace;
2664 bl->target_info.length = bl->length;
2665
2666 /* When working with target-side conditions, we must pass all the conditions
2667 for the same breakpoint address down to the target since GDB will not
2668 insert those locations. With a list of breakpoint conditions, the target
2669 can decide when to stop and notify GDB. */
2670
2671 if (is_breakpoint (bl->owner))
2672 {
2673 build_target_condition_list (bl);
2674 build_target_command_list (bl);
2675 /* Reset the modification marker. */
2676 bl->needs_update = 0;
2677 }
2678
2679 if (bl->loc_type == bp_loc_software_breakpoint
2680 || bl->loc_type == bp_loc_hardware_breakpoint)
2681 {
2682 if (bl->owner->type != bp_hardware_breakpoint)
2683 {
2684 /* If the explicitly specified breakpoint type
2685 is not hardware breakpoint, check the memory map to see
2686 if the breakpoint address is in read only memory or not.
2687
2688 Two important cases are:
2689 - location type is not hardware breakpoint, memory
2690 is readonly. We change the type of the location to
2691 hardware breakpoint.
2692 - location type is hardware breakpoint, memory is
2693 read-write. This means we've previously made the
2694 location hardware one, but then the memory map changed,
2695 so we undo.
2696
2697 When breakpoints are removed, remove_breakpoints will use
2698 location types we've just set here, the only possible
2699 problem is that memory map has changed during running
2700 program, but it's not going to work anyway with current
2701 gdb. */
2702 struct mem_region *mr
2703 = lookup_mem_region (bl->target_info.reqstd_address);
2704
2705 if (mr)
2706 {
2707 if (automatic_hardware_breakpoints)
2708 {
2709 enum bp_loc_type new_type;
2710
2711 if (mr->attrib.mode != MEM_RW)
2712 new_type = bp_loc_hardware_breakpoint;
2713 else
2714 new_type = bp_loc_software_breakpoint;
2715
2716 if (new_type != bl->loc_type)
2717 {
2718 static int said = 0;
2719
2720 bl->loc_type = new_type;
2721 if (!said)
2722 {
2723 fprintf_filtered (gdb_stdout,
2724 _("Note: automatically using "
2725 "hardware breakpoints for "
2726 "read-only addresses.\n"));
2727 said = 1;
2728 }
2729 }
2730 }
2731 else if (bl->loc_type == bp_loc_software_breakpoint
2732 && mr->attrib.mode != MEM_RW)
2733 {
2734 fprintf_unfiltered (tmp_error_stream,
2735 _("Cannot insert breakpoint %d.\n"
2736 "Cannot set software breakpoint "
2737 "at read-only address %s\n"),
2738 bl->owner->number,
2739 paddress (bl->gdbarch, bl->address));
2740 return 1;
2741 }
2742 }
2743 }
2744
2745 /* First check to see if we have to handle an overlay. */
2746 if (overlay_debugging == ovly_off
2747 || bl->section == NULL
2748 || !(section_is_overlay (bl->section)))
2749 {
2750 /* No overlay handling: just set the breakpoint. */
2751 TRY
2752 {
2753 int val;
2754
2755 val = bl->owner->ops->insert_location (bl);
2756 if (val)
2757 bp_err = GENERIC_ERROR;
2758 }
2759 CATCH (e, RETURN_MASK_ALL)
2760 {
2761 bp_err = e.error;
2762 bp_err_message = e.message;
2763 }
2764 END_CATCH
2765 }
2766 else
2767 {
2768 /* This breakpoint is in an overlay section.
2769 Shall we set a breakpoint at the LMA? */
2770 if (!overlay_events_enabled)
2771 {
2772 /* Yes -- overlay event support is not active,
2773 so we must try to set a breakpoint at the LMA.
2774 This will not work for a hardware breakpoint. */
2775 if (bl->loc_type == bp_loc_hardware_breakpoint)
2776 warning (_("hardware breakpoint %d not supported in overlay!"),
2777 bl->owner->number);
2778 else
2779 {
2780 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2781 bl->section);
2782 /* Set a software (trap) breakpoint at the LMA. */
2783 bl->overlay_target_info = bl->target_info;
2784 bl->overlay_target_info.reqstd_address = addr;
2785
2786 /* No overlay handling: just set the breakpoint. */
2787 TRY
2788 {
2789 int val;
2790
2791 bl->overlay_target_info.kind
2792 = breakpoint_kind (bl, &addr);
2793 bl->overlay_target_info.placed_address = addr;
2794 val = target_insert_breakpoint (bl->gdbarch,
2795 &bl->overlay_target_info);
2796 if (val)
2797 bp_err = GENERIC_ERROR;
2798 }
2799 CATCH (e, RETURN_MASK_ALL)
2800 {
2801 bp_err = e.error;
2802 bp_err_message = e.message;
2803 }
2804 END_CATCH
2805
2806 if (bp_err != GDB_NO_ERROR)
2807 fprintf_unfiltered (tmp_error_stream,
2808 "Overlay breakpoint %d "
2809 "failed: in ROM?\n",
2810 bl->owner->number);
2811 }
2812 }
2813 /* Shall we set a breakpoint at the VMA? */
2814 if (section_is_mapped (bl->section))
2815 {
2816 /* Yes. This overlay section is mapped into memory. */
2817 TRY
2818 {
2819 int val;
2820
2821 val = bl->owner->ops->insert_location (bl);
2822 if (val)
2823 bp_err = GENERIC_ERROR;
2824 }
2825 CATCH (e, RETURN_MASK_ALL)
2826 {
2827 bp_err = e.error;
2828 bp_err_message = e.message;
2829 }
2830 END_CATCH
2831 }
2832 else
2833 {
2834 /* No. This breakpoint will not be inserted.
2835 No error, but do not mark the bp as 'inserted'. */
2836 return 0;
2837 }
2838 }
2839
2840 if (bp_err != GDB_NO_ERROR)
2841 {
2842 /* Can't set the breakpoint. */
2843
2844 /* In some cases, we might not be able to insert a
2845 breakpoint in a shared library that has already been
2846 removed, but we have not yet processed the shlib unload
2847 event. Unfortunately, some targets that implement
2848 breakpoint insertion themselves can't tell why the
2849 breakpoint insertion failed (e.g., the remote target
2850 doesn't define error codes), so we must treat generic
2851 errors as memory errors. */
2852 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2853 && bl->loc_type == bp_loc_software_breakpoint
2854 && (solib_name_from_address (bl->pspace, bl->address)
2855 || shared_objfile_contains_address_p (bl->pspace,
2856 bl->address)))
2857 {
2858 /* See also: disable_breakpoints_in_shlibs. */
2859 bl->shlib_disabled = 1;
2860 observer_notify_breakpoint_modified (bl->owner);
2861 if (!*disabled_breaks)
2862 {
2863 fprintf_unfiltered (tmp_error_stream,
2864 "Cannot insert breakpoint %d.\n",
2865 bl->owner->number);
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Temporarily disabling shared "
2868 "library breakpoints:\n");
2869 }
2870 *disabled_breaks = 1;
2871 fprintf_unfiltered (tmp_error_stream,
2872 "breakpoint #%d\n", bl->owner->number);
2873 return 0;
2874 }
2875 else
2876 {
2877 if (bl->loc_type == bp_loc_hardware_breakpoint)
2878 {
2879 *hw_breakpoint_error = 1;
2880 *hw_bp_error_explained_already = bp_err_message != NULL;
2881 fprintf_unfiltered (tmp_error_stream,
2882 "Cannot insert hardware breakpoint %d%s",
2883 bl->owner->number, bp_err_message ? ":" : ".\n");
2884 if (bp_err_message != NULL)
2885 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2886 }
2887 else
2888 {
2889 if (bp_err_message == NULL)
2890 {
2891 char *message
2892 = memory_error_message (TARGET_XFER_E_IO,
2893 bl->gdbarch, bl->address);
2894 struct cleanup *old_chain = make_cleanup (xfree, message);
2895
2896 fprintf_unfiltered (tmp_error_stream,
2897 "Cannot insert breakpoint %d.\n"
2898 "%s\n",
2899 bl->owner->number, message);
2900 do_cleanups (old_chain);
2901 }
2902 else
2903 {
2904 fprintf_unfiltered (tmp_error_stream,
2905 "Cannot insert breakpoint %d: %s\n",
2906 bl->owner->number,
2907 bp_err_message);
2908 }
2909 }
2910 return 1;
2911
2912 }
2913 }
2914 else
2915 bl->inserted = 1;
2916
2917 return 0;
2918 }
2919
2920 else if (bl->loc_type == bp_loc_hardware_watchpoint
2921 /* NOTE drow/2003-09-08: This state only exists for removing
2922 watchpoints. It's not clear that it's necessary... */
2923 && bl->owner->disposition != disp_del_at_next_stop)
2924 {
2925 int val;
2926
2927 gdb_assert (bl->owner->ops != NULL
2928 && bl->owner->ops->insert_location != NULL);
2929
2930 val = bl->owner->ops->insert_location (bl);
2931
2932 /* If trying to set a read-watchpoint, and it turns out it's not
2933 supported, try emulating one with an access watchpoint. */
2934 if (val == 1 && bl->watchpoint_type == hw_read)
2935 {
2936 struct bp_location *loc, **loc_temp;
2937
2938 /* But don't try to insert it, if there's already another
2939 hw_access location that would be considered a duplicate
2940 of this one. */
2941 ALL_BP_LOCATIONS (loc, loc_temp)
2942 if (loc != bl
2943 && loc->watchpoint_type == hw_access
2944 && watchpoint_locations_match (bl, loc))
2945 {
2946 bl->duplicate = 1;
2947 bl->inserted = 1;
2948 bl->target_info = loc->target_info;
2949 bl->watchpoint_type = hw_access;
2950 val = 0;
2951 break;
2952 }
2953
2954 if (val == 1)
2955 {
2956 bl->watchpoint_type = hw_access;
2957 val = bl->owner->ops->insert_location (bl);
2958
2959 if (val)
2960 /* Back to the original value. */
2961 bl->watchpoint_type = hw_read;
2962 }
2963 }
2964
2965 bl->inserted = (val == 0);
2966 }
2967
2968 else if (bl->owner->type == bp_catchpoint)
2969 {
2970 int val;
2971
2972 gdb_assert (bl->owner->ops != NULL
2973 && bl->owner->ops->insert_location != NULL);
2974
2975 val = bl->owner->ops->insert_location (bl);
2976 if (val)
2977 {
2978 bl->owner->enable_state = bp_disabled;
2979
2980 if (val == 1)
2981 warning (_("\
2982 Error inserting catchpoint %d: Your system does not support this type\n\
2983 of catchpoint."), bl->owner->number);
2984 else
2985 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2986 }
2987
2988 bl->inserted = (val == 0);
2989
2990 /* We've already printed an error message if there was a problem
2991 inserting this catchpoint, and we've disabled the catchpoint,
2992 so just return success. */
2993 return 0;
2994 }
2995
2996 return 0;
2997 }
2998
2999 /* This function is called when program space PSPACE is about to be
3000 deleted. It takes care of updating breakpoints to not reference
3001 PSPACE anymore. */
3002
3003 void
3004 breakpoint_program_space_exit (struct program_space *pspace)
3005 {
3006 struct breakpoint *b, *b_temp;
3007 struct bp_location *loc, **loc_temp;
3008
3009 /* Remove any breakpoint that was set through this program space. */
3010 ALL_BREAKPOINTS_SAFE (b, b_temp)
3011 {
3012 if (b->pspace == pspace)
3013 delete_breakpoint (b);
3014 }
3015
3016 /* Breakpoints set through other program spaces could have locations
3017 bound to PSPACE as well. Remove those. */
3018 ALL_BP_LOCATIONS (loc, loc_temp)
3019 {
3020 struct bp_location *tmp;
3021
3022 if (loc->pspace == pspace)
3023 {
3024 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3025 if (loc->owner->loc == loc)
3026 loc->owner->loc = loc->next;
3027 else
3028 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3029 if (tmp->next == loc)
3030 {
3031 tmp->next = loc->next;
3032 break;
3033 }
3034 }
3035 }
3036
3037 /* Now update the global location list to permanently delete the
3038 removed locations above. */
3039 update_global_location_list (UGLL_DONT_INSERT);
3040 }
3041
3042 /* Make sure all breakpoints are inserted in inferior.
3043 Throws exception on any error.
3044 A breakpoint that is already inserted won't be inserted
3045 again, so calling this function twice is safe. */
3046 void
3047 insert_breakpoints (void)
3048 {
3049 struct breakpoint *bpt;
3050
3051 ALL_BREAKPOINTS (bpt)
3052 if (is_hardware_watchpoint (bpt))
3053 {
3054 struct watchpoint *w = (struct watchpoint *) bpt;
3055
3056 update_watchpoint (w, 0 /* don't reparse. */);
3057 }
3058
3059 /* Updating watchpoints creates new locations, so update the global
3060 location list. Explicitly tell ugll to insert locations and
3061 ignore breakpoints_always_inserted_mode. */
3062 update_global_location_list (UGLL_INSERT);
3063 }
3064
3065 /* Invoke CALLBACK for each of bp_location. */
3066
3067 void
3068 iterate_over_bp_locations (walk_bp_location_callback callback)
3069 {
3070 struct bp_location *loc, **loc_tmp;
3071
3072 ALL_BP_LOCATIONS (loc, loc_tmp)
3073 {
3074 callback (loc, NULL);
3075 }
3076 }
3077
3078 /* This is used when we need to synch breakpoint conditions between GDB and the
3079 target. It is the case with deleting and disabling of breakpoints when using
3080 always-inserted mode. */
3081
3082 static void
3083 update_inserted_breakpoint_locations (void)
3084 {
3085 struct bp_location *bl, **blp_tmp;
3086 int error_flag = 0;
3087 int val = 0;
3088 int disabled_breaks = 0;
3089 int hw_breakpoint_error = 0;
3090 int hw_bp_details_reported = 0;
3091
3092 string_file tmp_error_stream;
3093
3094 /* Explicitly mark the warning -- this will only be printed if
3095 there was an error. */
3096 tmp_error_stream.puts ("Warning:\n");
3097
3098 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3099
3100 ALL_BP_LOCATIONS (bl, blp_tmp)
3101 {
3102 /* We only want to update software breakpoints and hardware
3103 breakpoints. */
3104 if (!is_breakpoint (bl->owner))
3105 continue;
3106
3107 /* We only want to update locations that are already inserted
3108 and need updating. This is to avoid unwanted insertion during
3109 deletion of breakpoints. */
3110 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3111 continue;
3112
3113 switch_to_program_space_and_thread (bl->pspace);
3114
3115 /* For targets that support global breakpoints, there's no need
3116 to select an inferior to insert breakpoint to. In fact, even
3117 if we aren't attached to any process yet, we should still
3118 insert breakpoints. */
3119 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3120 && ptid_equal (inferior_ptid, null_ptid))
3121 continue;
3122
3123 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3124 &hw_breakpoint_error, &hw_bp_details_reported);
3125 if (val)
3126 error_flag = val;
3127 }
3128
3129 if (error_flag)
3130 {
3131 target_terminal_ours_for_output ();
3132 error_stream (tmp_error_stream);
3133 }
3134 }
3135
3136 /* Used when starting or continuing the program. */
3137
3138 static void
3139 insert_breakpoint_locations (void)
3140 {
3141 struct breakpoint *bpt;
3142 struct bp_location *bl, **blp_tmp;
3143 int error_flag = 0;
3144 int val = 0;
3145 int disabled_breaks = 0;
3146 int hw_breakpoint_error = 0;
3147 int hw_bp_error_explained_already = 0;
3148
3149 string_file tmp_error_stream;
3150
3151 /* Explicitly mark the warning -- this will only be printed if
3152 there was an error. */
3153 tmp_error_stream.puts ("Warning:\n");
3154
3155 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3156
3157 ALL_BP_LOCATIONS (bl, blp_tmp)
3158 {
3159 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3160 continue;
3161
3162 /* There is no point inserting thread-specific breakpoints if
3163 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3164 has BL->OWNER always non-NULL. */
3165 if (bl->owner->thread != -1
3166 && !valid_global_thread_id (bl->owner->thread))
3167 continue;
3168
3169 switch_to_program_space_and_thread (bl->pspace);
3170
3171 /* For targets that support global breakpoints, there's no need
3172 to select an inferior to insert breakpoint to. In fact, even
3173 if we aren't attached to any process yet, we should still
3174 insert breakpoints. */
3175 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3176 && ptid_equal (inferior_ptid, null_ptid))
3177 continue;
3178
3179 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3180 &hw_breakpoint_error, &hw_bp_error_explained_already);
3181 if (val)
3182 error_flag = val;
3183 }
3184
3185 /* If we failed to insert all locations of a watchpoint, remove
3186 them, as half-inserted watchpoint is of limited use. */
3187 ALL_BREAKPOINTS (bpt)
3188 {
3189 int some_failed = 0;
3190 struct bp_location *loc;
3191
3192 if (!is_hardware_watchpoint (bpt))
3193 continue;
3194
3195 if (!breakpoint_enabled (bpt))
3196 continue;
3197
3198 if (bpt->disposition == disp_del_at_next_stop)
3199 continue;
3200
3201 for (loc = bpt->loc; loc; loc = loc->next)
3202 if (!loc->inserted && should_be_inserted (loc))
3203 {
3204 some_failed = 1;
3205 break;
3206 }
3207 if (some_failed)
3208 {
3209 for (loc = bpt->loc; loc; loc = loc->next)
3210 if (loc->inserted)
3211 remove_breakpoint (loc);
3212
3213 hw_breakpoint_error = 1;
3214 tmp_error_stream.printf ("Could not insert "
3215 "hardware watchpoint %d.\n",
3216 bpt->number);
3217 error_flag = -1;
3218 }
3219 }
3220
3221 if (error_flag)
3222 {
3223 /* If a hardware breakpoint or watchpoint was inserted, add a
3224 message about possibly exhausted resources. */
3225 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3226 {
3227 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3228 You may have requested too many hardware breakpoints/watchpoints.\n");
3229 }
3230 target_terminal_ours_for_output ();
3231 error_stream (tmp_error_stream);
3232 }
3233 }
3234
3235 /* Used when the program stops.
3236 Returns zero if successful, or non-zero if there was a problem
3237 removing a breakpoint location. */
3238
3239 int
3240 remove_breakpoints (void)
3241 {
3242 struct bp_location *bl, **blp_tmp;
3243 int val = 0;
3244
3245 ALL_BP_LOCATIONS (bl, blp_tmp)
3246 {
3247 if (bl->inserted && !is_tracepoint (bl->owner))
3248 val |= remove_breakpoint (bl);
3249 }
3250 return val;
3251 }
3252
3253 /* When a thread exits, remove breakpoints that are related to
3254 that thread. */
3255
3256 static void
3257 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3258 {
3259 struct breakpoint *b, *b_tmp;
3260
3261 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3262 {
3263 if (b->thread == tp->global_num && user_breakpoint_p (b))
3264 {
3265 b->disposition = disp_del_at_next_stop;
3266
3267 printf_filtered (_("\
3268 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3269 b->number, print_thread_id (tp));
3270
3271 /* Hide it from the user. */
3272 b->number = 0;
3273 }
3274 }
3275 }
3276
3277 /* Remove breakpoints of process PID. */
3278
3279 int
3280 remove_breakpoints_pid (int pid)
3281 {
3282 struct bp_location *bl, **blp_tmp;
3283 int val;
3284 struct inferior *inf = find_inferior_pid (pid);
3285
3286 ALL_BP_LOCATIONS (bl, blp_tmp)
3287 {
3288 if (bl->pspace != inf->pspace)
3289 continue;
3290
3291 if (bl->inserted && !bl->target_info.persist)
3292 {
3293 val = remove_breakpoint (bl);
3294 if (val != 0)
3295 return val;
3296 }
3297 }
3298 return 0;
3299 }
3300
3301 int
3302 reattach_breakpoints (int pid)
3303 {
3304 struct cleanup *old_chain;
3305 struct bp_location *bl, **blp_tmp;
3306 int val;
3307 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3308 struct inferior *inf;
3309 struct thread_info *tp;
3310
3311 tp = any_live_thread_of_process (pid);
3312 if (tp == NULL)
3313 return 1;
3314
3315 inf = find_inferior_pid (pid);
3316 old_chain = save_inferior_ptid ();
3317
3318 inferior_ptid = tp->ptid;
3319
3320 string_file tmp_error_stream;
3321
3322 ALL_BP_LOCATIONS (bl, blp_tmp)
3323 {
3324 if (bl->pspace != inf->pspace)
3325 continue;
3326
3327 if (bl->inserted)
3328 {
3329 bl->inserted = 0;
3330 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3331 if (val != 0)
3332 {
3333 do_cleanups (old_chain);
3334 return val;
3335 }
3336 }
3337 }
3338 do_cleanups (old_chain);
3339 return 0;
3340 }
3341
3342 static int internal_breakpoint_number = -1;
3343
3344 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3345 If INTERNAL is non-zero, the breakpoint number will be populated
3346 from internal_breakpoint_number and that variable decremented.
3347 Otherwise the breakpoint number will be populated from
3348 breakpoint_count and that value incremented. Internal breakpoints
3349 do not set the internal var bpnum. */
3350 static void
3351 set_breakpoint_number (int internal, struct breakpoint *b)
3352 {
3353 if (internal)
3354 b->number = internal_breakpoint_number--;
3355 else
3356 {
3357 set_breakpoint_count (breakpoint_count + 1);
3358 b->number = breakpoint_count;
3359 }
3360 }
3361
3362 static struct breakpoint *
3363 create_internal_breakpoint (struct gdbarch *gdbarch,
3364 CORE_ADDR address, enum bptype type,
3365 const struct breakpoint_ops *ops)
3366 {
3367 struct symtab_and_line sal;
3368 struct breakpoint *b;
3369
3370 init_sal (&sal); /* Initialize to zeroes. */
3371
3372 sal.pc = address;
3373 sal.section = find_pc_overlay (sal.pc);
3374 sal.pspace = current_program_space;
3375
3376 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3377 b->number = internal_breakpoint_number--;
3378 b->disposition = disp_donttouch;
3379
3380 return b;
3381 }
3382
3383 static const char *const longjmp_names[] =
3384 {
3385 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3386 };
3387 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3388
3389 /* Per-objfile data private to breakpoint.c. */
3390 struct breakpoint_objfile_data
3391 {
3392 /* Minimal symbol for "_ovly_debug_event" (if any). */
3393 struct bound_minimal_symbol overlay_msym;
3394
3395 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3396 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3397
3398 /* True if we have looked for longjmp probes. */
3399 int longjmp_searched;
3400
3401 /* SystemTap probe points for longjmp (if any). */
3402 VEC (probe_p) *longjmp_probes;
3403
3404 /* Minimal symbol for "std::terminate()" (if any). */
3405 struct bound_minimal_symbol terminate_msym;
3406
3407 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3408 struct bound_minimal_symbol exception_msym;
3409
3410 /* True if we have looked for exception probes. */
3411 int exception_searched;
3412
3413 /* SystemTap probe points for unwinding (if any). */
3414 VEC (probe_p) *exception_probes;
3415 };
3416
3417 static const struct objfile_data *breakpoint_objfile_key;
3418
3419 /* Minimal symbol not found sentinel. */
3420 static struct minimal_symbol msym_not_found;
3421
3422 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3423
3424 static int
3425 msym_not_found_p (const struct minimal_symbol *msym)
3426 {
3427 return msym == &msym_not_found;
3428 }
3429
3430 /* Return per-objfile data needed by breakpoint.c.
3431 Allocate the data if necessary. */
3432
3433 static struct breakpoint_objfile_data *
3434 get_breakpoint_objfile_data (struct objfile *objfile)
3435 {
3436 struct breakpoint_objfile_data *bp_objfile_data;
3437
3438 bp_objfile_data = ((struct breakpoint_objfile_data *)
3439 objfile_data (objfile, breakpoint_objfile_key));
3440 if (bp_objfile_data == NULL)
3441 {
3442 bp_objfile_data =
3443 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3444
3445 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3446 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3447 }
3448 return bp_objfile_data;
3449 }
3450
3451 static void
3452 free_breakpoint_probes (struct objfile *obj, void *data)
3453 {
3454 struct breakpoint_objfile_data *bp_objfile_data
3455 = (struct breakpoint_objfile_data *) data;
3456
3457 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3458 VEC_free (probe_p, bp_objfile_data->exception_probes);
3459 }
3460
3461 static void
3462 create_overlay_event_breakpoint (void)
3463 {
3464 struct objfile *objfile;
3465 const char *const func_name = "_ovly_debug_event";
3466
3467 ALL_OBJFILES (objfile)
3468 {
3469 struct breakpoint *b;
3470 struct breakpoint_objfile_data *bp_objfile_data;
3471 CORE_ADDR addr;
3472 struct explicit_location explicit_loc;
3473
3474 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3475
3476 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3477 continue;
3478
3479 if (bp_objfile_data->overlay_msym.minsym == NULL)
3480 {
3481 struct bound_minimal_symbol m;
3482
3483 m = lookup_minimal_symbol_text (func_name, objfile);
3484 if (m.minsym == NULL)
3485 {
3486 /* Avoid future lookups in this objfile. */
3487 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3488 continue;
3489 }
3490 bp_objfile_data->overlay_msym = m;
3491 }
3492
3493 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3494 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3495 bp_overlay_event,
3496 &internal_breakpoint_ops);
3497 initialize_explicit_location (&explicit_loc);
3498 explicit_loc.function_name = ASTRDUP (func_name);
3499 b->location = new_explicit_location (&explicit_loc);
3500
3501 if (overlay_debugging == ovly_auto)
3502 {
3503 b->enable_state = bp_enabled;
3504 overlay_events_enabled = 1;
3505 }
3506 else
3507 {
3508 b->enable_state = bp_disabled;
3509 overlay_events_enabled = 0;
3510 }
3511 }
3512 }
3513
3514 static void
3515 create_longjmp_master_breakpoint (void)
3516 {
3517 struct program_space *pspace;
3518
3519 scoped_restore_current_program_space restore_pspace;
3520
3521 ALL_PSPACES (pspace)
3522 {
3523 struct objfile *objfile;
3524
3525 set_current_program_space (pspace);
3526
3527 ALL_OBJFILES (objfile)
3528 {
3529 int i;
3530 struct gdbarch *gdbarch;
3531 struct breakpoint_objfile_data *bp_objfile_data;
3532
3533 gdbarch = get_objfile_arch (objfile);
3534
3535 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3536
3537 if (!bp_objfile_data->longjmp_searched)
3538 {
3539 VEC (probe_p) *ret;
3540
3541 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3542 if (ret != NULL)
3543 {
3544 /* We are only interested in checking one element. */
3545 struct probe *p = VEC_index (probe_p, ret, 0);
3546
3547 if (!can_evaluate_probe_arguments (p))
3548 {
3549 /* We cannot use the probe interface here, because it does
3550 not know how to evaluate arguments. */
3551 VEC_free (probe_p, ret);
3552 ret = NULL;
3553 }
3554 }
3555 bp_objfile_data->longjmp_probes = ret;
3556 bp_objfile_data->longjmp_searched = 1;
3557 }
3558
3559 if (bp_objfile_data->longjmp_probes != NULL)
3560 {
3561 int i;
3562 struct probe *probe;
3563 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3564
3565 for (i = 0;
3566 VEC_iterate (probe_p,
3567 bp_objfile_data->longjmp_probes,
3568 i, probe);
3569 ++i)
3570 {
3571 struct breakpoint *b;
3572
3573 b = create_internal_breakpoint (gdbarch,
3574 get_probe_address (probe,
3575 objfile),
3576 bp_longjmp_master,
3577 &internal_breakpoint_ops);
3578 b->location = new_probe_location ("-probe-stap libc:longjmp");
3579 b->enable_state = bp_disabled;
3580 }
3581
3582 continue;
3583 }
3584
3585 if (!gdbarch_get_longjmp_target_p (gdbarch))
3586 continue;
3587
3588 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3589 {
3590 struct breakpoint *b;
3591 const char *func_name;
3592 CORE_ADDR addr;
3593 struct explicit_location explicit_loc;
3594
3595 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3596 continue;
3597
3598 func_name = longjmp_names[i];
3599 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3600 {
3601 struct bound_minimal_symbol m;
3602
3603 m = lookup_minimal_symbol_text (func_name, objfile);
3604 if (m.minsym == NULL)
3605 {
3606 /* Prevent future lookups in this objfile. */
3607 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3608 continue;
3609 }
3610 bp_objfile_data->longjmp_msym[i] = m;
3611 }
3612
3613 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3614 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3615 &internal_breakpoint_ops);
3616 initialize_explicit_location (&explicit_loc);
3617 explicit_loc.function_name = ASTRDUP (func_name);
3618 b->location = new_explicit_location (&explicit_loc);
3619 b->enable_state = bp_disabled;
3620 }
3621 }
3622 }
3623 }
3624
3625 /* Create a master std::terminate breakpoint. */
3626 static void
3627 create_std_terminate_master_breakpoint (void)
3628 {
3629 struct program_space *pspace;
3630 const char *const func_name = "std::terminate()";
3631
3632 scoped_restore_current_program_space restore_pspace;
3633
3634 ALL_PSPACES (pspace)
3635 {
3636 struct objfile *objfile;
3637 CORE_ADDR addr;
3638
3639 set_current_program_space (pspace);
3640
3641 ALL_OBJFILES (objfile)
3642 {
3643 struct breakpoint *b;
3644 struct breakpoint_objfile_data *bp_objfile_data;
3645 struct explicit_location explicit_loc;
3646
3647 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3648
3649 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3650 continue;
3651
3652 if (bp_objfile_data->terminate_msym.minsym == NULL)
3653 {
3654 struct bound_minimal_symbol m;
3655
3656 m = lookup_minimal_symbol (func_name, NULL, objfile);
3657 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3658 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3659 {
3660 /* Prevent future lookups in this objfile. */
3661 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3662 continue;
3663 }
3664 bp_objfile_data->terminate_msym = m;
3665 }
3666
3667 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3668 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3669 bp_std_terminate_master,
3670 &internal_breakpoint_ops);
3671 initialize_explicit_location (&explicit_loc);
3672 explicit_loc.function_name = ASTRDUP (func_name);
3673 b->location = new_explicit_location (&explicit_loc);
3674 b->enable_state = bp_disabled;
3675 }
3676 }
3677 }
3678
3679 /* Install a master breakpoint on the unwinder's debug hook. */
3680
3681 static void
3682 create_exception_master_breakpoint (void)
3683 {
3684 struct objfile *objfile;
3685 const char *const func_name = "_Unwind_DebugHook";
3686
3687 ALL_OBJFILES (objfile)
3688 {
3689 struct breakpoint *b;
3690 struct gdbarch *gdbarch;
3691 struct breakpoint_objfile_data *bp_objfile_data;
3692 CORE_ADDR addr;
3693 struct explicit_location explicit_loc;
3694
3695 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3696
3697 /* We prefer the SystemTap probe point if it exists. */
3698 if (!bp_objfile_data->exception_searched)
3699 {
3700 VEC (probe_p) *ret;
3701
3702 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3703
3704 if (ret != NULL)
3705 {
3706 /* We are only interested in checking one element. */
3707 struct probe *p = VEC_index (probe_p, ret, 0);
3708
3709 if (!can_evaluate_probe_arguments (p))
3710 {
3711 /* We cannot use the probe interface here, because it does
3712 not know how to evaluate arguments. */
3713 VEC_free (probe_p, ret);
3714 ret = NULL;
3715 }
3716 }
3717 bp_objfile_data->exception_probes = ret;
3718 bp_objfile_data->exception_searched = 1;
3719 }
3720
3721 if (bp_objfile_data->exception_probes != NULL)
3722 {
3723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3724 int i;
3725 struct probe *probe;
3726
3727 for (i = 0;
3728 VEC_iterate (probe_p,
3729 bp_objfile_data->exception_probes,
3730 i, probe);
3731 ++i)
3732 {
3733 struct breakpoint *b;
3734
3735 b = create_internal_breakpoint (gdbarch,
3736 get_probe_address (probe,
3737 objfile),
3738 bp_exception_master,
3739 &internal_breakpoint_ops);
3740 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3741 b->enable_state = bp_disabled;
3742 }
3743
3744 continue;
3745 }
3746
3747 /* Otherwise, try the hook function. */
3748
3749 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3750 continue;
3751
3752 gdbarch = get_objfile_arch (objfile);
3753
3754 if (bp_objfile_data->exception_msym.minsym == NULL)
3755 {
3756 struct bound_minimal_symbol debug_hook;
3757
3758 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3759 if (debug_hook.minsym == NULL)
3760 {
3761 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3762 continue;
3763 }
3764
3765 bp_objfile_data->exception_msym = debug_hook;
3766 }
3767
3768 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3769 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3770 &current_target);
3771 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3772 &internal_breakpoint_ops);
3773 initialize_explicit_location (&explicit_loc);
3774 explicit_loc.function_name = ASTRDUP (func_name);
3775 b->location = new_explicit_location (&explicit_loc);
3776 b->enable_state = bp_disabled;
3777 }
3778 }
3779
3780 /* Does B have a location spec? */
3781
3782 static int
3783 breakpoint_event_location_empty_p (const struct breakpoint *b)
3784 {
3785 return b->location != NULL && event_location_empty_p (b->location.get ());
3786 }
3787
3788 void
3789 update_breakpoints_after_exec (void)
3790 {
3791 struct breakpoint *b, *b_tmp;
3792 struct bp_location *bploc, **bplocp_tmp;
3793
3794 /* We're about to delete breakpoints from GDB's lists. If the
3795 INSERTED flag is true, GDB will try to lift the breakpoints by
3796 writing the breakpoints' "shadow contents" back into memory. The
3797 "shadow contents" are NOT valid after an exec, so GDB should not
3798 do that. Instead, the target is responsible from marking
3799 breakpoints out as soon as it detects an exec. We don't do that
3800 here instead, because there may be other attempts to delete
3801 breakpoints after detecting an exec and before reaching here. */
3802 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3803 if (bploc->pspace == current_program_space)
3804 gdb_assert (!bploc->inserted);
3805
3806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3807 {
3808 if (b->pspace != current_program_space)
3809 continue;
3810
3811 /* Solib breakpoints must be explicitly reset after an exec(). */
3812 if (b->type == bp_shlib_event)
3813 {
3814 delete_breakpoint (b);
3815 continue;
3816 }
3817
3818 /* JIT breakpoints must be explicitly reset after an exec(). */
3819 if (b->type == bp_jit_event)
3820 {
3821 delete_breakpoint (b);
3822 continue;
3823 }
3824
3825 /* Thread event breakpoints must be set anew after an exec(),
3826 as must overlay event and longjmp master breakpoints. */
3827 if (b->type == bp_thread_event || b->type == bp_overlay_event
3828 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3829 || b->type == bp_exception_master)
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
3834
3835 /* Step-resume breakpoints are meaningless after an exec(). */
3836 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3837 {
3838 delete_breakpoint (b);
3839 continue;
3840 }
3841
3842 /* Just like single-step breakpoints. */
3843 if (b->type == bp_single_step)
3844 {
3845 delete_breakpoint (b);
3846 continue;
3847 }
3848
3849 /* Longjmp and longjmp-resume breakpoints are also meaningless
3850 after an exec. */
3851 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3852 || b->type == bp_longjmp_call_dummy
3853 || b->type == bp_exception || b->type == bp_exception_resume)
3854 {
3855 delete_breakpoint (b);
3856 continue;
3857 }
3858
3859 if (b->type == bp_catchpoint)
3860 {
3861 /* For now, none of the bp_catchpoint breakpoints need to
3862 do anything at this point. In the future, if some of
3863 the catchpoints need to something, we will need to add
3864 a new method, and call this method from here. */
3865 continue;
3866 }
3867
3868 /* bp_finish is a special case. The only way we ought to be able
3869 to see one of these when an exec() has happened, is if the user
3870 caught a vfork, and then said "finish". Ordinarily a finish just
3871 carries them to the call-site of the current callee, by setting
3872 a temporary bp there and resuming. But in this case, the finish
3873 will carry them entirely through the vfork & exec.
3874
3875 We don't want to allow a bp_finish to remain inserted now. But
3876 we can't safely delete it, 'cause finish_command has a handle to
3877 the bp on a bpstat, and will later want to delete it. There's a
3878 chance (and I've seen it happen) that if we delete the bp_finish
3879 here, that its storage will get reused by the time finish_command
3880 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3881 We really must allow finish_command to delete a bp_finish.
3882
3883 In the absence of a general solution for the "how do we know
3884 it's safe to delete something others may have handles to?"
3885 problem, what we'll do here is just uninsert the bp_finish, and
3886 let finish_command delete it.
3887
3888 (We know the bp_finish is "doomed" in the sense that it's
3889 momentary, and will be deleted as soon as finish_command sees
3890 the inferior stopped. So it doesn't matter that the bp's
3891 address is probably bogus in the new a.out, unlike e.g., the
3892 solib breakpoints.) */
3893
3894 if (b->type == bp_finish)
3895 {
3896 continue;
3897 }
3898
3899 /* Without a symbolic address, we have little hope of the
3900 pre-exec() address meaning the same thing in the post-exec()
3901 a.out. */
3902 if (breakpoint_event_location_empty_p (b))
3903 {
3904 delete_breakpoint (b);
3905 continue;
3906 }
3907 }
3908 }
3909
3910 int
3911 detach_breakpoints (ptid_t ptid)
3912 {
3913 struct bp_location *bl, **blp_tmp;
3914 int val = 0;
3915 struct cleanup *old_chain = save_inferior_ptid ();
3916 struct inferior *inf = current_inferior ();
3917
3918 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3919 error (_("Cannot detach breakpoints of inferior_ptid"));
3920
3921 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3922 inferior_ptid = ptid;
3923 ALL_BP_LOCATIONS (bl, blp_tmp)
3924 {
3925 if (bl->pspace != inf->pspace)
3926 continue;
3927
3928 /* This function must physically remove breakpoints locations
3929 from the specified ptid, without modifying the breakpoint
3930 package's state. Locations of type bp_loc_other are only
3931 maintained at GDB side. So, there is no need to remove
3932 these bp_loc_other locations. Moreover, removing these
3933 would modify the breakpoint package's state. */
3934 if (bl->loc_type == bp_loc_other)
3935 continue;
3936
3937 if (bl->inserted)
3938 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3939 }
3940
3941 do_cleanups (old_chain);
3942 return val;
3943 }
3944
3945 /* Remove the breakpoint location BL from the current address space.
3946 Note that this is used to detach breakpoints from a child fork.
3947 When we get here, the child isn't in the inferior list, and neither
3948 do we have objects to represent its address space --- we should
3949 *not* look at bl->pspace->aspace here. */
3950
3951 static int
3952 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3953 {
3954 int val;
3955
3956 /* BL is never in moribund_locations by our callers. */
3957 gdb_assert (bl->owner != NULL);
3958
3959 /* The type of none suggests that owner is actually deleted.
3960 This should not ever happen. */
3961 gdb_assert (bl->owner->type != bp_none);
3962
3963 if (bl->loc_type == bp_loc_software_breakpoint
3964 || bl->loc_type == bp_loc_hardware_breakpoint)
3965 {
3966 /* "Normal" instruction breakpoint: either the standard
3967 trap-instruction bp (bp_breakpoint), or a
3968 bp_hardware_breakpoint. */
3969
3970 /* First check to see if we have to handle an overlay. */
3971 if (overlay_debugging == ovly_off
3972 || bl->section == NULL
3973 || !(section_is_overlay (bl->section)))
3974 {
3975 /* No overlay handling: just remove the breakpoint. */
3976
3977 /* If we're trying to uninsert a memory breakpoint that we
3978 know is set in a dynamic object that is marked
3979 shlib_disabled, then either the dynamic object was
3980 removed with "remove-symbol-file" or with
3981 "nosharedlibrary". In the former case, we don't know
3982 whether another dynamic object might have loaded over the
3983 breakpoint's address -- the user might well let us know
3984 about it next with add-symbol-file (the whole point of
3985 add-symbol-file is letting the user manually maintain a
3986 list of dynamically loaded objects). If we have the
3987 breakpoint's shadow memory, that is, this is a software
3988 breakpoint managed by GDB, check whether the breakpoint
3989 is still inserted in memory, to avoid overwriting wrong
3990 code with stale saved shadow contents. Note that HW
3991 breakpoints don't have shadow memory, as they're
3992 implemented using a mechanism that is not dependent on
3993 being able to modify the target's memory, and as such
3994 they should always be removed. */
3995 if (bl->shlib_disabled
3996 && bl->target_info.shadow_len != 0
3997 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3998 val = 0;
3999 else
4000 val = bl->owner->ops->remove_location (bl, reason);
4001 }
4002 else
4003 {
4004 /* This breakpoint is in an overlay section.
4005 Did we set a breakpoint at the LMA? */
4006 if (!overlay_events_enabled)
4007 {
4008 /* Yes -- overlay event support is not active, so we
4009 should have set a breakpoint at the LMA. Remove it.
4010 */
4011 /* Ignore any failures: if the LMA is in ROM, we will
4012 have already warned when we failed to insert it. */
4013 if (bl->loc_type == bp_loc_hardware_breakpoint)
4014 target_remove_hw_breakpoint (bl->gdbarch,
4015 &bl->overlay_target_info);
4016 else
4017 target_remove_breakpoint (bl->gdbarch,
4018 &bl->overlay_target_info,
4019 reason);
4020 }
4021 /* Did we set a breakpoint at the VMA?
4022 If so, we will have marked the breakpoint 'inserted'. */
4023 if (bl->inserted)
4024 {
4025 /* Yes -- remove it. Previously we did not bother to
4026 remove the breakpoint if the section had been
4027 unmapped, but let's not rely on that being safe. We
4028 don't know what the overlay manager might do. */
4029
4030 /* However, we should remove *software* breakpoints only
4031 if the section is still mapped, or else we overwrite
4032 wrong code with the saved shadow contents. */
4033 if (bl->loc_type == bp_loc_hardware_breakpoint
4034 || section_is_mapped (bl->section))
4035 val = bl->owner->ops->remove_location (bl, reason);
4036 else
4037 val = 0;
4038 }
4039 else
4040 {
4041 /* No -- not inserted, so no need to remove. No error. */
4042 val = 0;
4043 }
4044 }
4045
4046 /* In some cases, we might not be able to remove a breakpoint in
4047 a shared library that has already been removed, but we have
4048 not yet processed the shlib unload event. Similarly for an
4049 unloaded add-symbol-file object - the user might not yet have
4050 had the chance to remove-symbol-file it. shlib_disabled will
4051 be set if the library/object has already been removed, but
4052 the breakpoint hasn't been uninserted yet, e.g., after
4053 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4054 always-inserted mode. */
4055 if (val
4056 && (bl->loc_type == bp_loc_software_breakpoint
4057 && (bl->shlib_disabled
4058 || solib_name_from_address (bl->pspace, bl->address)
4059 || shared_objfile_contains_address_p (bl->pspace,
4060 bl->address))))
4061 val = 0;
4062
4063 if (val)
4064 return val;
4065 bl->inserted = (reason == DETACH_BREAKPOINT);
4066 }
4067 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4068 {
4069 gdb_assert (bl->owner->ops != NULL
4070 && bl->owner->ops->remove_location != NULL);
4071
4072 bl->inserted = (reason == DETACH_BREAKPOINT);
4073 bl->owner->ops->remove_location (bl, reason);
4074
4075 /* Failure to remove any of the hardware watchpoints comes here. */
4076 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4077 warning (_("Could not remove hardware watchpoint %d."),
4078 bl->owner->number);
4079 }
4080 else if (bl->owner->type == bp_catchpoint
4081 && breakpoint_enabled (bl->owner)
4082 && !bl->duplicate)
4083 {
4084 gdb_assert (bl->owner->ops != NULL
4085 && bl->owner->ops->remove_location != NULL);
4086
4087 val = bl->owner->ops->remove_location (bl, reason);
4088 if (val)
4089 return val;
4090
4091 bl->inserted = (reason == DETACH_BREAKPOINT);
4092 }
4093
4094 return 0;
4095 }
4096
4097 static int
4098 remove_breakpoint (struct bp_location *bl)
4099 {
4100 /* BL is never in moribund_locations by our callers. */
4101 gdb_assert (bl->owner != NULL);
4102
4103 /* The type of none suggests that owner is actually deleted.
4104 This should not ever happen. */
4105 gdb_assert (bl->owner->type != bp_none);
4106
4107 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4108
4109 switch_to_program_space_and_thread (bl->pspace);
4110
4111 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4112 }
4113
4114 /* Clear the "inserted" flag in all breakpoints. */
4115
4116 void
4117 mark_breakpoints_out (void)
4118 {
4119 struct bp_location *bl, **blp_tmp;
4120
4121 ALL_BP_LOCATIONS (bl, blp_tmp)
4122 if (bl->pspace == current_program_space)
4123 bl->inserted = 0;
4124 }
4125
4126 /* Clear the "inserted" flag in all breakpoints and delete any
4127 breakpoints which should go away between runs of the program.
4128
4129 Plus other such housekeeping that has to be done for breakpoints
4130 between runs.
4131
4132 Note: this function gets called at the end of a run (by
4133 generic_mourn_inferior) and when a run begins (by
4134 init_wait_for_inferior). */
4135
4136
4137
4138 void
4139 breakpoint_init_inferior (enum inf_context context)
4140 {
4141 struct breakpoint *b, *b_tmp;
4142 struct bp_location *bl;
4143 int ix;
4144 struct program_space *pspace = current_program_space;
4145
4146 /* If breakpoint locations are shared across processes, then there's
4147 nothing to do. */
4148 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4149 return;
4150
4151 mark_breakpoints_out ();
4152
4153 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4154 {
4155 if (b->loc && b->loc->pspace != pspace)
4156 continue;
4157
4158 switch (b->type)
4159 {
4160 case bp_call_dummy:
4161 case bp_longjmp_call_dummy:
4162
4163 /* If the call dummy breakpoint is at the entry point it will
4164 cause problems when the inferior is rerun, so we better get
4165 rid of it. */
4166
4167 case bp_watchpoint_scope:
4168
4169 /* Also get rid of scope breakpoints. */
4170
4171 case bp_shlib_event:
4172
4173 /* Also remove solib event breakpoints. Their addresses may
4174 have changed since the last time we ran the program.
4175 Actually we may now be debugging against different target;
4176 and so the solib backend that installed this breakpoint may
4177 not be used in by the target. E.g.,
4178
4179 (gdb) file prog-linux
4180 (gdb) run # native linux target
4181 ...
4182 (gdb) kill
4183 (gdb) file prog-win.exe
4184 (gdb) tar rem :9999 # remote Windows gdbserver.
4185 */
4186
4187 case bp_step_resume:
4188
4189 /* Also remove step-resume breakpoints. */
4190
4191 case bp_single_step:
4192
4193 /* Also remove single-step breakpoints. */
4194
4195 delete_breakpoint (b);
4196 break;
4197
4198 case bp_watchpoint:
4199 case bp_hardware_watchpoint:
4200 case bp_read_watchpoint:
4201 case bp_access_watchpoint:
4202 {
4203 struct watchpoint *w = (struct watchpoint *) b;
4204
4205 /* Likewise for watchpoints on local expressions. */
4206 if (w->exp_valid_block != NULL)
4207 delete_breakpoint (b);
4208 else
4209 {
4210 /* Get rid of existing locations, which are no longer
4211 valid. New ones will be created in
4212 update_watchpoint, when the inferior is restarted.
4213 The next update_global_location_list call will
4214 garbage collect them. */
4215 b->loc = NULL;
4216
4217 if (context == inf_starting)
4218 {
4219 /* Reset val field to force reread of starting value in
4220 insert_breakpoints. */
4221 if (w->val)
4222 value_free (w->val);
4223 w->val = NULL;
4224 w->val_valid = 0;
4225 }
4226 }
4227 }
4228 break;
4229 default:
4230 break;
4231 }
4232 }
4233
4234 /* Get rid of the moribund locations. */
4235 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4236 decref_bp_location (&bl);
4237 VEC_free (bp_location_p, moribund_locations);
4238 }
4239
4240 /* These functions concern about actual breakpoints inserted in the
4241 target --- to e.g. check if we need to do decr_pc adjustment or if
4242 we need to hop over the bkpt --- so we check for address space
4243 match, not program space. */
4244
4245 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4246 exists at PC. It returns ordinary_breakpoint_here if it's an
4247 ordinary breakpoint, or permanent_breakpoint_here if it's a
4248 permanent breakpoint.
4249 - When continuing from a location with an ordinary breakpoint, we
4250 actually single step once before calling insert_breakpoints.
4251 - When continuing from a location with a permanent breakpoint, we
4252 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4253 the target, to advance the PC past the breakpoint. */
4254
4255 enum breakpoint_here
4256 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4257 {
4258 struct bp_location *bl, **blp_tmp;
4259 int any_breakpoint_here = 0;
4260
4261 ALL_BP_LOCATIONS (bl, blp_tmp)
4262 {
4263 if (bl->loc_type != bp_loc_software_breakpoint
4264 && bl->loc_type != bp_loc_hardware_breakpoint)
4265 continue;
4266
4267 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4268 if ((breakpoint_enabled (bl->owner)
4269 || bl->permanent)
4270 && breakpoint_location_address_match (bl, aspace, pc))
4271 {
4272 if (overlay_debugging
4273 && section_is_overlay (bl->section)
4274 && !section_is_mapped (bl->section))
4275 continue; /* unmapped overlay -- can't be a match */
4276 else if (bl->permanent)
4277 return permanent_breakpoint_here;
4278 else
4279 any_breakpoint_here = 1;
4280 }
4281 }
4282
4283 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4284 }
4285
4286 /* See breakpoint.h. */
4287
4288 int
4289 breakpoint_in_range_p (struct address_space *aspace,
4290 CORE_ADDR addr, ULONGEST len)
4291 {
4292 struct bp_location *bl, **blp_tmp;
4293
4294 ALL_BP_LOCATIONS (bl, blp_tmp)
4295 {
4296 if (bl->loc_type != bp_loc_software_breakpoint
4297 && bl->loc_type != bp_loc_hardware_breakpoint)
4298 continue;
4299
4300 if ((breakpoint_enabled (bl->owner)
4301 || bl->permanent)
4302 && breakpoint_location_address_range_overlap (bl, aspace,
4303 addr, len))
4304 {
4305 if (overlay_debugging
4306 && section_is_overlay (bl->section)
4307 && !section_is_mapped (bl->section))
4308 {
4309 /* Unmapped overlay -- can't be a match. */
4310 continue;
4311 }
4312
4313 return 1;
4314 }
4315 }
4316
4317 return 0;
4318 }
4319
4320 /* Return true if there's a moribund breakpoint at PC. */
4321
4322 int
4323 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4324 {
4325 struct bp_location *loc;
4326 int ix;
4327
4328 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4329 if (breakpoint_location_address_match (loc, aspace, pc))
4330 return 1;
4331
4332 return 0;
4333 }
4334
4335 /* Returns non-zero iff BL is inserted at PC, in address space
4336 ASPACE. */
4337
4338 static int
4339 bp_location_inserted_here_p (struct bp_location *bl,
4340 struct address_space *aspace, CORE_ADDR pc)
4341 {
4342 if (bl->inserted
4343 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4344 aspace, pc))
4345 {
4346 if (overlay_debugging
4347 && section_is_overlay (bl->section)
4348 && !section_is_mapped (bl->section))
4349 return 0; /* unmapped overlay -- can't be a match */
4350 else
4351 return 1;
4352 }
4353 return 0;
4354 }
4355
4356 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4357
4358 int
4359 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4360 {
4361 struct bp_location **blp, **blp_tmp = NULL;
4362
4363 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4364 {
4365 struct bp_location *bl = *blp;
4366
4367 if (bl->loc_type != bp_loc_software_breakpoint
4368 && bl->loc_type != bp_loc_hardware_breakpoint)
4369 continue;
4370
4371 if (bp_location_inserted_here_p (bl, aspace, pc))
4372 return 1;
4373 }
4374 return 0;
4375 }
4376
4377 /* This function returns non-zero iff there is a software breakpoint
4378 inserted at PC. */
4379
4380 int
4381 software_breakpoint_inserted_here_p (struct address_space *aspace,
4382 CORE_ADDR pc)
4383 {
4384 struct bp_location **blp, **blp_tmp = NULL;
4385
4386 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4387 {
4388 struct bp_location *bl = *blp;
4389
4390 if (bl->loc_type != bp_loc_software_breakpoint)
4391 continue;
4392
4393 if (bp_location_inserted_here_p (bl, aspace, pc))
4394 return 1;
4395 }
4396
4397 return 0;
4398 }
4399
4400 /* See breakpoint.h. */
4401
4402 int
4403 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4404 CORE_ADDR pc)
4405 {
4406 struct bp_location **blp, **blp_tmp = NULL;
4407
4408 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4409 {
4410 struct bp_location *bl = *blp;
4411
4412 if (bl->loc_type != bp_loc_hardware_breakpoint)
4413 continue;
4414
4415 if (bp_location_inserted_here_p (bl, aspace, pc))
4416 return 1;
4417 }
4418
4419 return 0;
4420 }
4421
4422 int
4423 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4424 CORE_ADDR addr, ULONGEST len)
4425 {
4426 struct breakpoint *bpt;
4427
4428 ALL_BREAKPOINTS (bpt)
4429 {
4430 struct bp_location *loc;
4431
4432 if (bpt->type != bp_hardware_watchpoint
4433 && bpt->type != bp_access_watchpoint)
4434 continue;
4435
4436 if (!breakpoint_enabled (bpt))
4437 continue;
4438
4439 for (loc = bpt->loc; loc; loc = loc->next)
4440 if (loc->pspace->aspace == aspace && loc->inserted)
4441 {
4442 CORE_ADDR l, h;
4443
4444 /* Check for intersection. */
4445 l = std::max<CORE_ADDR> (loc->address, addr);
4446 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4447 if (l < h)
4448 return 1;
4449 }
4450 }
4451 return 0;
4452 }
4453 \f
4454
4455 /* bpstat stuff. External routines' interfaces are documented
4456 in breakpoint.h. */
4457
4458 int
4459 is_catchpoint (struct breakpoint *ep)
4460 {
4461 return (ep->type == bp_catchpoint);
4462 }
4463
4464 /* Frees any storage that is part of a bpstat. Does not walk the
4465 'next' chain. */
4466
4467 static void
4468 bpstat_free (bpstat bs)
4469 {
4470 if (bs->old_val != NULL)
4471 value_free (bs->old_val);
4472 decref_counted_command_line (&bs->commands);
4473 decref_bp_location (&bs->bp_location_at);
4474 xfree (bs);
4475 }
4476
4477 /* Clear a bpstat so that it says we are not at any breakpoint.
4478 Also free any storage that is part of a bpstat. */
4479
4480 void
4481 bpstat_clear (bpstat *bsp)
4482 {
4483 bpstat p;
4484 bpstat q;
4485
4486 if (bsp == 0)
4487 return;
4488 p = *bsp;
4489 while (p != NULL)
4490 {
4491 q = p->next;
4492 bpstat_free (p);
4493 p = q;
4494 }
4495 *bsp = NULL;
4496 }
4497
4498 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4499 is part of the bpstat is copied as well. */
4500
4501 bpstat
4502 bpstat_copy (bpstat bs)
4503 {
4504 bpstat p = NULL;
4505 bpstat tmp;
4506 bpstat retval = NULL;
4507
4508 if (bs == NULL)
4509 return bs;
4510
4511 for (; bs != NULL; bs = bs->next)
4512 {
4513 tmp = (bpstat) xmalloc (sizeof (*tmp));
4514 memcpy (tmp, bs, sizeof (*tmp));
4515 incref_counted_command_line (tmp->commands);
4516 incref_bp_location (tmp->bp_location_at);
4517 if (bs->old_val != NULL)
4518 {
4519 tmp->old_val = value_copy (bs->old_val);
4520 release_value (tmp->old_val);
4521 }
4522
4523 if (p == NULL)
4524 /* This is the first thing in the chain. */
4525 retval = tmp;
4526 else
4527 p->next = tmp;
4528 p = tmp;
4529 }
4530 p->next = NULL;
4531 return retval;
4532 }
4533
4534 /* Find the bpstat associated with this breakpoint. */
4535
4536 bpstat
4537 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4538 {
4539 if (bsp == NULL)
4540 return NULL;
4541
4542 for (; bsp != NULL; bsp = bsp->next)
4543 {
4544 if (bsp->breakpoint_at == breakpoint)
4545 return bsp;
4546 }
4547 return NULL;
4548 }
4549
4550 /* See breakpoint.h. */
4551
4552 int
4553 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4554 {
4555 for (; bsp != NULL; bsp = bsp->next)
4556 {
4557 if (bsp->breakpoint_at == NULL)
4558 {
4559 /* A moribund location can never explain a signal other than
4560 GDB_SIGNAL_TRAP. */
4561 if (sig == GDB_SIGNAL_TRAP)
4562 return 1;
4563 }
4564 else
4565 {
4566 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4567 sig))
4568 return 1;
4569 }
4570 }
4571
4572 return 0;
4573 }
4574
4575 /* Put in *NUM the breakpoint number of the first breakpoint we are
4576 stopped at. *BSP upon return is a bpstat which points to the
4577 remaining breakpoints stopped at (but which is not guaranteed to be
4578 good for anything but further calls to bpstat_num).
4579
4580 Return 0 if passed a bpstat which does not indicate any breakpoints.
4581 Return -1 if stopped at a breakpoint that has been deleted since
4582 we set it.
4583 Return 1 otherwise. */
4584
4585 int
4586 bpstat_num (bpstat *bsp, int *num)
4587 {
4588 struct breakpoint *b;
4589
4590 if ((*bsp) == NULL)
4591 return 0; /* No more breakpoint values */
4592
4593 /* We assume we'll never have several bpstats that correspond to a
4594 single breakpoint -- otherwise, this function might return the
4595 same number more than once and this will look ugly. */
4596 b = (*bsp)->breakpoint_at;
4597 *bsp = (*bsp)->next;
4598 if (b == NULL)
4599 return -1; /* breakpoint that's been deleted since */
4600
4601 *num = b->number; /* We have its number */
4602 return 1;
4603 }
4604
4605 /* See breakpoint.h. */
4606
4607 void
4608 bpstat_clear_actions (void)
4609 {
4610 struct thread_info *tp;
4611 bpstat bs;
4612
4613 if (ptid_equal (inferior_ptid, null_ptid))
4614 return;
4615
4616 tp = find_thread_ptid (inferior_ptid);
4617 if (tp == NULL)
4618 return;
4619
4620 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4621 {
4622 decref_counted_command_line (&bs->commands);
4623
4624 if (bs->old_val != NULL)
4625 {
4626 value_free (bs->old_val);
4627 bs->old_val = NULL;
4628 }
4629 }
4630 }
4631
4632 /* Called when a command is about to proceed the inferior. */
4633
4634 static void
4635 breakpoint_about_to_proceed (void)
4636 {
4637 if (!ptid_equal (inferior_ptid, null_ptid))
4638 {
4639 struct thread_info *tp = inferior_thread ();
4640
4641 /* Allow inferior function calls in breakpoint commands to not
4642 interrupt the command list. When the call finishes
4643 successfully, the inferior will be standing at the same
4644 breakpoint as if nothing happened. */
4645 if (tp->control.in_infcall)
4646 return;
4647 }
4648
4649 breakpoint_proceeded = 1;
4650 }
4651
4652 /* Stub for cleaning up our state if we error-out of a breakpoint
4653 command. */
4654 static void
4655 cleanup_executing_breakpoints (void *ignore)
4656 {
4657 executing_breakpoint_commands = 0;
4658 }
4659
4660 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4661 or its equivalent. */
4662
4663 static int
4664 command_line_is_silent (struct command_line *cmd)
4665 {
4666 return cmd && (strcmp ("silent", cmd->line) == 0);
4667 }
4668
4669 /* Execute all the commands associated with all the breakpoints at
4670 this location. Any of these commands could cause the process to
4671 proceed beyond this point, etc. We look out for such changes by
4672 checking the global "breakpoint_proceeded" after each command.
4673
4674 Returns true if a breakpoint command resumed the inferior. In that
4675 case, it is the caller's responsibility to recall it again with the
4676 bpstat of the current thread. */
4677
4678 static int
4679 bpstat_do_actions_1 (bpstat *bsp)
4680 {
4681 bpstat bs;
4682 struct cleanup *old_chain;
4683 int again = 0;
4684
4685 /* Avoid endless recursion if a `source' command is contained
4686 in bs->commands. */
4687 if (executing_breakpoint_commands)
4688 return 0;
4689
4690 executing_breakpoint_commands = 1;
4691 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4692
4693 scoped_restore preventer = prevent_dont_repeat ();
4694
4695 /* This pointer will iterate over the list of bpstat's. */
4696 bs = *bsp;
4697
4698 breakpoint_proceeded = 0;
4699 for (; bs != NULL; bs = bs->next)
4700 {
4701 struct counted_command_line *ccmd;
4702 struct command_line *cmd;
4703 struct cleanup *this_cmd_tree_chain;
4704
4705 /* Take ownership of the BSP's command tree, if it has one.
4706
4707 The command tree could legitimately contain commands like
4708 'step' and 'next', which call clear_proceed_status, which
4709 frees stop_bpstat's command tree. To make sure this doesn't
4710 free the tree we're executing out from under us, we need to
4711 take ownership of the tree ourselves. Since a given bpstat's
4712 commands are only executed once, we don't need to copy it; we
4713 can clear the pointer in the bpstat, and make sure we free
4714 the tree when we're done. */
4715 ccmd = bs->commands;
4716 bs->commands = NULL;
4717 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4718 cmd = ccmd ? ccmd->commands : NULL;
4719 if (command_line_is_silent (cmd))
4720 {
4721 /* The action has been already done by bpstat_stop_status. */
4722 cmd = cmd->next;
4723 }
4724
4725 while (cmd != NULL)
4726 {
4727 execute_control_command (cmd);
4728
4729 if (breakpoint_proceeded)
4730 break;
4731 else
4732 cmd = cmd->next;
4733 }
4734
4735 /* We can free this command tree now. */
4736 do_cleanups (this_cmd_tree_chain);
4737
4738 if (breakpoint_proceeded)
4739 {
4740 if (current_ui->async)
4741 /* If we are in async mode, then the target might be still
4742 running, not stopped at any breakpoint, so nothing for
4743 us to do here -- just return to the event loop. */
4744 ;
4745 else
4746 /* In sync mode, when execute_control_command returns
4747 we're already standing on the next breakpoint.
4748 Breakpoint commands for that stop were not run, since
4749 execute_command does not run breakpoint commands --
4750 only command_line_handler does, but that one is not
4751 involved in execution of breakpoint commands. So, we
4752 can now execute breakpoint commands. It should be
4753 noted that making execute_command do bpstat actions is
4754 not an option -- in this case we'll have recursive
4755 invocation of bpstat for each breakpoint with a
4756 command, and can easily blow up GDB stack. Instead, we
4757 return true, which will trigger the caller to recall us
4758 with the new stop_bpstat. */
4759 again = 1;
4760 break;
4761 }
4762 }
4763 do_cleanups (old_chain);
4764 return again;
4765 }
4766
4767 void
4768 bpstat_do_actions (void)
4769 {
4770 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4771
4772 /* Do any commands attached to breakpoint we are stopped at. */
4773 while (!ptid_equal (inferior_ptid, null_ptid)
4774 && target_has_execution
4775 && !is_exited (inferior_ptid)
4776 && !is_executing (inferior_ptid))
4777 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4778 and only return when it is stopped at the next breakpoint, we
4779 keep doing breakpoint actions until it returns false to
4780 indicate the inferior was not resumed. */
4781 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4782 break;
4783
4784 discard_cleanups (cleanup_if_error);
4785 }
4786
4787 /* Print out the (old or new) value associated with a watchpoint. */
4788
4789 static void
4790 watchpoint_value_print (struct value *val, struct ui_file *stream)
4791 {
4792 if (val == NULL)
4793 fprintf_unfiltered (stream, _("<unreadable>"));
4794 else
4795 {
4796 struct value_print_options opts;
4797 get_user_print_options (&opts);
4798 value_print (val, stream, &opts);
4799 }
4800 }
4801
4802 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4803 debugging multiple threads. */
4804
4805 void
4806 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4807 {
4808 if (uiout->is_mi_like_p ())
4809 return;
4810
4811 uiout->text ("\n");
4812
4813 if (show_thread_that_caused_stop ())
4814 {
4815 const char *name;
4816 struct thread_info *thr = inferior_thread ();
4817
4818 uiout->text ("Thread ");
4819 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4820
4821 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4822 if (name != NULL)
4823 {
4824 uiout->text (" \"");
4825 uiout->field_fmt ("name", "%s", name);
4826 uiout->text ("\"");
4827 }
4828
4829 uiout->text (" hit ");
4830 }
4831 }
4832
4833 /* Generic routine for printing messages indicating why we
4834 stopped. The behavior of this function depends on the value
4835 'print_it' in the bpstat structure. Under some circumstances we
4836 may decide not to print anything here and delegate the task to
4837 normal_stop(). */
4838
4839 static enum print_stop_action
4840 print_bp_stop_message (bpstat bs)
4841 {
4842 switch (bs->print_it)
4843 {
4844 case print_it_noop:
4845 /* Nothing should be printed for this bpstat entry. */
4846 return PRINT_UNKNOWN;
4847 break;
4848
4849 case print_it_done:
4850 /* We still want to print the frame, but we already printed the
4851 relevant messages. */
4852 return PRINT_SRC_AND_LOC;
4853 break;
4854
4855 case print_it_normal:
4856 {
4857 struct breakpoint *b = bs->breakpoint_at;
4858
4859 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4860 which has since been deleted. */
4861 if (b == NULL)
4862 return PRINT_UNKNOWN;
4863
4864 /* Normal case. Call the breakpoint's print_it method. */
4865 return b->ops->print_it (bs);
4866 }
4867 break;
4868
4869 default:
4870 internal_error (__FILE__, __LINE__,
4871 _("print_bp_stop_message: unrecognized enum value"));
4872 break;
4873 }
4874 }
4875
4876 /* A helper function that prints a shared library stopped event. */
4877
4878 static void
4879 print_solib_event (int is_catchpoint)
4880 {
4881 int any_deleted
4882 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4883 int any_added
4884 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4885
4886 if (!is_catchpoint)
4887 {
4888 if (any_added || any_deleted)
4889 current_uiout->text (_("Stopped due to shared library event:\n"));
4890 else
4891 current_uiout->text (_("Stopped due to shared library event (no "
4892 "libraries added or removed)\n"));
4893 }
4894
4895 if (current_uiout->is_mi_like_p ())
4896 current_uiout->field_string ("reason",
4897 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4898
4899 if (any_deleted)
4900 {
4901 char *name;
4902 int ix;
4903
4904 current_uiout->text (_(" Inferior unloaded "));
4905 ui_out_emit_list list_emitter (current_uiout, "removed");
4906 for (ix = 0;
4907 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4908 ix, name);
4909 ++ix)
4910 {
4911 if (ix > 0)
4912 current_uiout->text (" ");
4913 current_uiout->field_string ("library", name);
4914 current_uiout->text ("\n");
4915 }
4916 }
4917
4918 if (any_added)
4919 {
4920 struct so_list *iter;
4921 int ix;
4922
4923 current_uiout->text (_(" Inferior loaded "));
4924 ui_out_emit_list list_emitter (current_uiout, "added");
4925 for (ix = 0;
4926 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4927 ix, iter);
4928 ++ix)
4929 {
4930 if (ix > 0)
4931 current_uiout->text (" ");
4932 current_uiout->field_string ("library", iter->so_name);
4933 current_uiout->text ("\n");
4934 }
4935 }
4936 }
4937
4938 /* Print a message indicating what happened. This is called from
4939 normal_stop(). The input to this routine is the head of the bpstat
4940 list - a list of the eventpoints that caused this stop. KIND is
4941 the target_waitkind for the stopping event. This
4942 routine calls the generic print routine for printing a message
4943 about reasons for stopping. This will print (for example) the
4944 "Breakpoint n," part of the output. The return value of this
4945 routine is one of:
4946
4947 PRINT_UNKNOWN: Means we printed nothing.
4948 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4949 code to print the location. An example is
4950 "Breakpoint 1, " which should be followed by
4951 the location.
4952 PRINT_SRC_ONLY: Means we printed something, but there is no need
4953 to also print the location part of the message.
4954 An example is the catch/throw messages, which
4955 don't require a location appended to the end.
4956 PRINT_NOTHING: We have done some printing and we don't need any
4957 further info to be printed. */
4958
4959 enum print_stop_action
4960 bpstat_print (bpstat bs, int kind)
4961 {
4962 enum print_stop_action val;
4963
4964 /* Maybe another breakpoint in the chain caused us to stop.
4965 (Currently all watchpoints go on the bpstat whether hit or not.
4966 That probably could (should) be changed, provided care is taken
4967 with respect to bpstat_explains_signal). */
4968 for (; bs; bs = bs->next)
4969 {
4970 val = print_bp_stop_message (bs);
4971 if (val == PRINT_SRC_ONLY
4972 || val == PRINT_SRC_AND_LOC
4973 || val == PRINT_NOTHING)
4974 return val;
4975 }
4976
4977 /* If we had hit a shared library event breakpoint,
4978 print_bp_stop_message would print out this message. If we hit an
4979 OS-level shared library event, do the same thing. */
4980 if (kind == TARGET_WAITKIND_LOADED)
4981 {
4982 print_solib_event (0);
4983 return PRINT_NOTHING;
4984 }
4985
4986 /* We reached the end of the chain, or we got a null BS to start
4987 with and nothing was printed. */
4988 return PRINT_UNKNOWN;
4989 }
4990
4991 /* Evaluate the expression EXP and return 1 if value is zero.
4992 This returns the inverse of the condition because it is called
4993 from catch_errors which returns 0 if an exception happened, and if an
4994 exception happens we want execution to stop.
4995 The argument is a "struct expression *" that has been cast to a
4996 "void *" to make it pass through catch_errors. */
4997
4998 static int
4999 breakpoint_cond_eval (void *exp)
5000 {
5001 struct value *mark = value_mark ();
5002 int i = !value_true (evaluate_expression ((struct expression *) exp));
5003
5004 value_free_to_mark (mark);
5005 return i;
5006 }
5007
5008 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5009
5010 static bpstat
5011 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5012 {
5013 bpstat bs;
5014
5015 bs = (bpstat) xmalloc (sizeof (*bs));
5016 bs->next = NULL;
5017 **bs_link_pointer = bs;
5018 *bs_link_pointer = &bs->next;
5019 bs->breakpoint_at = bl->owner;
5020 bs->bp_location_at = bl;
5021 incref_bp_location (bl);
5022 /* If the condition is false, etc., don't do the commands. */
5023 bs->commands = NULL;
5024 bs->old_val = NULL;
5025 bs->print_it = print_it_normal;
5026 return bs;
5027 }
5028 \f
5029 /* The target has stopped with waitstatus WS. Check if any hardware
5030 watchpoints have triggered, according to the target. */
5031
5032 int
5033 watchpoints_triggered (struct target_waitstatus *ws)
5034 {
5035 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5036 CORE_ADDR addr;
5037 struct breakpoint *b;
5038
5039 if (!stopped_by_watchpoint)
5040 {
5041 /* We were not stopped by a watchpoint. Mark all watchpoints
5042 as not triggered. */
5043 ALL_BREAKPOINTS (b)
5044 if (is_hardware_watchpoint (b))
5045 {
5046 struct watchpoint *w = (struct watchpoint *) b;
5047
5048 w->watchpoint_triggered = watch_triggered_no;
5049 }
5050
5051 return 0;
5052 }
5053
5054 if (!target_stopped_data_address (&current_target, &addr))
5055 {
5056 /* We were stopped by a watchpoint, but we don't know where.
5057 Mark all watchpoints as unknown. */
5058 ALL_BREAKPOINTS (b)
5059 if (is_hardware_watchpoint (b))
5060 {
5061 struct watchpoint *w = (struct watchpoint *) b;
5062
5063 w->watchpoint_triggered = watch_triggered_unknown;
5064 }
5065
5066 return 1;
5067 }
5068
5069 /* The target could report the data address. Mark watchpoints
5070 affected by this data address as triggered, and all others as not
5071 triggered. */
5072
5073 ALL_BREAKPOINTS (b)
5074 if (is_hardware_watchpoint (b))
5075 {
5076 struct watchpoint *w = (struct watchpoint *) b;
5077 struct bp_location *loc;
5078
5079 w->watchpoint_triggered = watch_triggered_no;
5080 for (loc = b->loc; loc; loc = loc->next)
5081 {
5082 if (is_masked_watchpoint (b))
5083 {
5084 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5085 CORE_ADDR start = loc->address & w->hw_wp_mask;
5086
5087 if (newaddr == start)
5088 {
5089 w->watchpoint_triggered = watch_triggered_yes;
5090 break;
5091 }
5092 }
5093 /* Exact match not required. Within range is sufficient. */
5094 else if (target_watchpoint_addr_within_range (&current_target,
5095 addr, loc->address,
5096 loc->length))
5097 {
5098 w->watchpoint_triggered = watch_triggered_yes;
5099 break;
5100 }
5101 }
5102 }
5103
5104 return 1;
5105 }
5106
5107 /* Possible return values for watchpoint_check (this can't be an enum
5108 because of check_errors). */
5109 /* The watchpoint has been deleted. */
5110 #define WP_DELETED 1
5111 /* The value has changed. */
5112 #define WP_VALUE_CHANGED 2
5113 /* The value has not changed. */
5114 #define WP_VALUE_NOT_CHANGED 3
5115 /* Ignore this watchpoint, no matter if the value changed or not. */
5116 #define WP_IGNORE 4
5117
5118 #define BP_TEMPFLAG 1
5119 #define BP_HARDWAREFLAG 2
5120
5121 /* Evaluate watchpoint condition expression and check if its value
5122 changed.
5123
5124 P should be a pointer to struct bpstat, but is defined as a void *
5125 in order for this function to be usable with catch_errors. */
5126
5127 static int
5128 watchpoint_check (void *p)
5129 {
5130 bpstat bs = (bpstat) p;
5131 struct watchpoint *b;
5132 struct frame_info *fr;
5133 int within_current_scope;
5134
5135 /* BS is built from an existing struct breakpoint. */
5136 gdb_assert (bs->breakpoint_at != NULL);
5137 b = (struct watchpoint *) bs->breakpoint_at;
5138
5139 /* If this is a local watchpoint, we only want to check if the
5140 watchpoint frame is in scope if the current thread is the thread
5141 that was used to create the watchpoint. */
5142 if (!watchpoint_in_thread_scope (b))
5143 return WP_IGNORE;
5144
5145 if (b->exp_valid_block == NULL)
5146 within_current_scope = 1;
5147 else
5148 {
5149 struct frame_info *frame = get_current_frame ();
5150 struct gdbarch *frame_arch = get_frame_arch (frame);
5151 CORE_ADDR frame_pc = get_frame_pc (frame);
5152
5153 /* stack_frame_destroyed_p() returns a non-zero value if we're
5154 still in the function but the stack frame has already been
5155 invalidated. Since we can't rely on the values of local
5156 variables after the stack has been destroyed, we are treating
5157 the watchpoint in that state as `not changed' without further
5158 checking. Don't mark watchpoints as changed if the current
5159 frame is in an epilogue - even if they are in some other
5160 frame, our view of the stack is likely to be wrong and
5161 frame_find_by_id could error out. */
5162 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5163 return WP_IGNORE;
5164
5165 fr = frame_find_by_id (b->watchpoint_frame);
5166 within_current_scope = (fr != NULL);
5167
5168 /* If we've gotten confused in the unwinder, we might have
5169 returned a frame that can't describe this variable. */
5170 if (within_current_scope)
5171 {
5172 struct symbol *function;
5173
5174 function = get_frame_function (fr);
5175 if (function == NULL
5176 || !contained_in (b->exp_valid_block,
5177 SYMBOL_BLOCK_VALUE (function)))
5178 within_current_scope = 0;
5179 }
5180
5181 if (within_current_scope)
5182 /* If we end up stopping, the current frame will get selected
5183 in normal_stop. So this call to select_frame won't affect
5184 the user. */
5185 select_frame (fr);
5186 }
5187
5188 if (within_current_scope)
5189 {
5190 /* We use value_{,free_to_}mark because it could be a *long*
5191 time before we return to the command level and call
5192 free_all_values. We can't call free_all_values because we
5193 might be in the middle of evaluating a function call. */
5194
5195 int pc = 0;
5196 struct value *mark;
5197 struct value *new_val;
5198
5199 if (is_masked_watchpoint (&b->base))
5200 /* Since we don't know the exact trigger address (from
5201 stopped_data_address), just tell the user we've triggered
5202 a mask watchpoint. */
5203 return WP_VALUE_CHANGED;
5204
5205 mark = value_mark ();
5206 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5207
5208 if (b->val_bitsize != 0)
5209 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5210
5211 /* We use value_equal_contents instead of value_equal because
5212 the latter coerces an array to a pointer, thus comparing just
5213 the address of the array instead of its contents. This is
5214 not what we want. */
5215 if ((b->val != NULL) != (new_val != NULL)
5216 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5217 {
5218 if (new_val != NULL)
5219 {
5220 release_value (new_val);
5221 value_free_to_mark (mark);
5222 }
5223 bs->old_val = b->val;
5224 b->val = new_val;
5225 b->val_valid = 1;
5226 return WP_VALUE_CHANGED;
5227 }
5228 else
5229 {
5230 /* Nothing changed. */
5231 value_free_to_mark (mark);
5232 return WP_VALUE_NOT_CHANGED;
5233 }
5234 }
5235 else
5236 {
5237 /* This seems like the only logical thing to do because
5238 if we temporarily ignored the watchpoint, then when
5239 we reenter the block in which it is valid it contains
5240 garbage (in the case of a function, it may have two
5241 garbage values, one before and one after the prologue).
5242 So we can't even detect the first assignment to it and
5243 watch after that (since the garbage may or may not equal
5244 the first value assigned). */
5245 /* We print all the stop information in
5246 breakpoint_ops->print_it, but in this case, by the time we
5247 call breakpoint_ops->print_it this bp will be deleted
5248 already. So we have no choice but print the information
5249 here. */
5250
5251 SWITCH_THRU_ALL_UIS ()
5252 {
5253 struct ui_out *uiout = current_uiout;
5254
5255 if (uiout->is_mi_like_p ())
5256 uiout->field_string
5257 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5258 uiout->text ("\nWatchpoint ");
5259 uiout->field_int ("wpnum", b->base.number);
5260 uiout->text (" deleted because the program has left the block in\n"
5261 "which its expression is valid.\n");
5262 }
5263
5264 /* Make sure the watchpoint's commands aren't executed. */
5265 decref_counted_command_line (&b->base.commands);
5266 watchpoint_del_at_next_stop (b);
5267
5268 return WP_DELETED;
5269 }
5270 }
5271
5272 /* Return true if it looks like target has stopped due to hitting
5273 breakpoint location BL. This function does not check if we should
5274 stop, only if BL explains the stop. */
5275
5276 static int
5277 bpstat_check_location (const struct bp_location *bl,
5278 struct address_space *aspace, CORE_ADDR bp_addr,
5279 const struct target_waitstatus *ws)
5280 {
5281 struct breakpoint *b = bl->owner;
5282
5283 /* BL is from an existing breakpoint. */
5284 gdb_assert (b != NULL);
5285
5286 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5287 }
5288
5289 /* Determine if the watched values have actually changed, and we
5290 should stop. If not, set BS->stop to 0. */
5291
5292 static void
5293 bpstat_check_watchpoint (bpstat bs)
5294 {
5295 const struct bp_location *bl;
5296 struct watchpoint *b;
5297
5298 /* BS is built for existing struct breakpoint. */
5299 bl = bs->bp_location_at;
5300 gdb_assert (bl != NULL);
5301 b = (struct watchpoint *) bs->breakpoint_at;
5302 gdb_assert (b != NULL);
5303
5304 {
5305 int must_check_value = 0;
5306
5307 if (b->base.type == bp_watchpoint)
5308 /* For a software watchpoint, we must always check the
5309 watched value. */
5310 must_check_value = 1;
5311 else if (b->watchpoint_triggered == watch_triggered_yes)
5312 /* We have a hardware watchpoint (read, write, or access)
5313 and the target earlier reported an address watched by
5314 this watchpoint. */
5315 must_check_value = 1;
5316 else if (b->watchpoint_triggered == watch_triggered_unknown
5317 && b->base.type == bp_hardware_watchpoint)
5318 /* We were stopped by a hardware watchpoint, but the target could
5319 not report the data address. We must check the watchpoint's
5320 value. Access and read watchpoints are out of luck; without
5321 a data address, we can't figure it out. */
5322 must_check_value = 1;
5323
5324 if (must_check_value)
5325 {
5326 char *message
5327 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5328 b->base.number);
5329 struct cleanup *cleanups = make_cleanup (xfree, message);
5330 int e = catch_errors (watchpoint_check, bs, message,
5331 RETURN_MASK_ALL);
5332 do_cleanups (cleanups);
5333 switch (e)
5334 {
5335 case WP_DELETED:
5336 /* We've already printed what needs to be printed. */
5337 bs->print_it = print_it_done;
5338 /* Stop. */
5339 break;
5340 case WP_IGNORE:
5341 bs->print_it = print_it_noop;
5342 bs->stop = 0;
5343 break;
5344 case WP_VALUE_CHANGED:
5345 if (b->base.type == bp_read_watchpoint)
5346 {
5347 /* There are two cases to consider here:
5348
5349 1. We're watching the triggered memory for reads.
5350 In that case, trust the target, and always report
5351 the watchpoint hit to the user. Even though
5352 reads don't cause value changes, the value may
5353 have changed since the last time it was read, and
5354 since we're not trapping writes, we will not see
5355 those, and as such we should ignore our notion of
5356 old value.
5357
5358 2. We're watching the triggered memory for both
5359 reads and writes. There are two ways this may
5360 happen:
5361
5362 2.1. This is a target that can't break on data
5363 reads only, but can break on accesses (reads or
5364 writes), such as e.g., x86. We detect this case
5365 at the time we try to insert read watchpoints.
5366
5367 2.2. Otherwise, the target supports read
5368 watchpoints, but, the user set an access or write
5369 watchpoint watching the same memory as this read
5370 watchpoint.
5371
5372 If we're watching memory writes as well as reads,
5373 ignore watchpoint hits when we find that the
5374 value hasn't changed, as reads don't cause
5375 changes. This still gives false positives when
5376 the program writes the same value to memory as
5377 what there was already in memory (we will confuse
5378 it for a read), but it's much better than
5379 nothing. */
5380
5381 int other_write_watchpoint = 0;
5382
5383 if (bl->watchpoint_type == hw_read)
5384 {
5385 struct breakpoint *other_b;
5386
5387 ALL_BREAKPOINTS (other_b)
5388 if (other_b->type == bp_hardware_watchpoint
5389 || other_b->type == bp_access_watchpoint)
5390 {
5391 struct watchpoint *other_w =
5392 (struct watchpoint *) other_b;
5393
5394 if (other_w->watchpoint_triggered
5395 == watch_triggered_yes)
5396 {
5397 other_write_watchpoint = 1;
5398 break;
5399 }
5400 }
5401 }
5402
5403 if (other_write_watchpoint
5404 || bl->watchpoint_type == hw_access)
5405 {
5406 /* We're watching the same memory for writes,
5407 and the value changed since the last time we
5408 updated it, so this trap must be for a write.
5409 Ignore it. */
5410 bs->print_it = print_it_noop;
5411 bs->stop = 0;
5412 }
5413 }
5414 break;
5415 case WP_VALUE_NOT_CHANGED:
5416 if (b->base.type == bp_hardware_watchpoint
5417 || b->base.type == bp_watchpoint)
5418 {
5419 /* Don't stop: write watchpoints shouldn't fire if
5420 the value hasn't changed. */
5421 bs->print_it = print_it_noop;
5422 bs->stop = 0;
5423 }
5424 /* Stop. */
5425 break;
5426 default:
5427 /* Can't happen. */
5428 case 0:
5429 /* Error from catch_errors. */
5430 {
5431 SWITCH_THRU_ALL_UIS ()
5432 {
5433 printf_filtered (_("Watchpoint %d deleted.\n"),
5434 b->base.number);
5435 }
5436 watchpoint_del_at_next_stop (b);
5437 /* We've already printed what needs to be printed. */
5438 bs->print_it = print_it_done;
5439 }
5440 break;
5441 }
5442 }
5443 else /* must_check_value == 0 */
5444 {
5445 /* This is a case where some watchpoint(s) triggered, but
5446 not at the address of this watchpoint, or else no
5447 watchpoint triggered after all. So don't print
5448 anything for this watchpoint. */
5449 bs->print_it = print_it_noop;
5450 bs->stop = 0;
5451 }
5452 }
5453 }
5454
5455 /* For breakpoints that are currently marked as telling gdb to stop,
5456 check conditions (condition proper, frame, thread and ignore count)
5457 of breakpoint referred to by BS. If we should not stop for this
5458 breakpoint, set BS->stop to 0. */
5459
5460 static void
5461 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5462 {
5463 const struct bp_location *bl;
5464 struct breakpoint *b;
5465 int value_is_zero = 0;
5466 struct expression *cond;
5467
5468 gdb_assert (bs->stop);
5469
5470 /* BS is built for existing struct breakpoint. */
5471 bl = bs->bp_location_at;
5472 gdb_assert (bl != NULL);
5473 b = bs->breakpoint_at;
5474 gdb_assert (b != NULL);
5475
5476 /* Even if the target evaluated the condition on its end and notified GDB, we
5477 need to do so again since GDB does not know if we stopped due to a
5478 breakpoint or a single step breakpoint. */
5479
5480 if (frame_id_p (b->frame_id)
5481 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5482 {
5483 bs->stop = 0;
5484 return;
5485 }
5486
5487 /* If this is a thread/task-specific breakpoint, don't waste cpu
5488 evaluating the condition if this isn't the specified
5489 thread/task. */
5490 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5491 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5492
5493 {
5494 bs->stop = 0;
5495 return;
5496 }
5497
5498 /* Evaluate extension language breakpoints that have a "stop" method
5499 implemented. */
5500 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5501
5502 if (is_watchpoint (b))
5503 {
5504 struct watchpoint *w = (struct watchpoint *) b;
5505
5506 cond = w->cond_exp.get ();
5507 }
5508 else
5509 cond = bl->cond.get ();
5510
5511 if (cond && b->disposition != disp_del_at_next_stop)
5512 {
5513 int within_current_scope = 1;
5514 struct watchpoint * w;
5515
5516 /* We use value_mark and value_free_to_mark because it could
5517 be a long time before we return to the command level and
5518 call free_all_values. We can't call free_all_values
5519 because we might be in the middle of evaluating a
5520 function call. */
5521 struct value *mark = value_mark ();
5522
5523 if (is_watchpoint (b))
5524 w = (struct watchpoint *) b;
5525 else
5526 w = NULL;
5527
5528 /* Need to select the frame, with all that implies so that
5529 the conditions will have the right context. Because we
5530 use the frame, we will not see an inlined function's
5531 variables when we arrive at a breakpoint at the start
5532 of the inlined function; the current frame will be the
5533 call site. */
5534 if (w == NULL || w->cond_exp_valid_block == NULL)
5535 select_frame (get_current_frame ());
5536 else
5537 {
5538 struct frame_info *frame;
5539
5540 /* For local watchpoint expressions, which particular
5541 instance of a local is being watched matters, so we
5542 keep track of the frame to evaluate the expression
5543 in. To evaluate the condition however, it doesn't
5544 really matter which instantiation of the function
5545 where the condition makes sense triggers the
5546 watchpoint. This allows an expression like "watch
5547 global if q > 10" set in `func', catch writes to
5548 global on all threads that call `func', or catch
5549 writes on all recursive calls of `func' by a single
5550 thread. We simply always evaluate the condition in
5551 the innermost frame that's executing where it makes
5552 sense to evaluate the condition. It seems
5553 intuitive. */
5554 frame = block_innermost_frame (w->cond_exp_valid_block);
5555 if (frame != NULL)
5556 select_frame (frame);
5557 else
5558 within_current_scope = 0;
5559 }
5560 if (within_current_scope)
5561 value_is_zero
5562 = catch_errors (breakpoint_cond_eval, cond,
5563 "Error in testing breakpoint condition:\n",
5564 RETURN_MASK_ALL);
5565 else
5566 {
5567 warning (_("Watchpoint condition cannot be tested "
5568 "in the current scope"));
5569 /* If we failed to set the right context for this
5570 watchpoint, unconditionally report it. */
5571 value_is_zero = 0;
5572 }
5573 /* FIXME-someday, should give breakpoint #. */
5574 value_free_to_mark (mark);
5575 }
5576
5577 if (cond && value_is_zero)
5578 {
5579 bs->stop = 0;
5580 }
5581 else if (b->ignore_count > 0)
5582 {
5583 b->ignore_count--;
5584 bs->stop = 0;
5585 /* Increase the hit count even though we don't stop. */
5586 ++(b->hit_count);
5587 observer_notify_breakpoint_modified (b);
5588 }
5589 }
5590
5591 /* Returns true if we need to track moribund locations of LOC's type
5592 on the current target. */
5593
5594 static int
5595 need_moribund_for_location_type (struct bp_location *loc)
5596 {
5597 return ((loc->loc_type == bp_loc_software_breakpoint
5598 && !target_supports_stopped_by_sw_breakpoint ())
5599 || (loc->loc_type == bp_loc_hardware_breakpoint
5600 && !target_supports_stopped_by_hw_breakpoint ()));
5601 }
5602
5603
5604 /* Get a bpstat associated with having just stopped at address
5605 BP_ADDR in thread PTID.
5606
5607 Determine whether we stopped at a breakpoint, etc, or whether we
5608 don't understand this stop. Result is a chain of bpstat's such
5609 that:
5610
5611 if we don't understand the stop, the result is a null pointer.
5612
5613 if we understand why we stopped, the result is not null.
5614
5615 Each element of the chain refers to a particular breakpoint or
5616 watchpoint at which we have stopped. (We may have stopped for
5617 several reasons concurrently.)
5618
5619 Each element of the chain has valid next, breakpoint_at,
5620 commands, FIXME??? fields. */
5621
5622 bpstat
5623 bpstat_stop_status (struct address_space *aspace,
5624 CORE_ADDR bp_addr, ptid_t ptid,
5625 const struct target_waitstatus *ws)
5626 {
5627 struct breakpoint *b = NULL;
5628 struct bp_location *bl;
5629 struct bp_location *loc;
5630 /* First item of allocated bpstat's. */
5631 bpstat bs_head = NULL, *bs_link = &bs_head;
5632 /* Pointer to the last thing in the chain currently. */
5633 bpstat bs;
5634 int ix;
5635 int need_remove_insert;
5636 int removed_any;
5637
5638 /* First, build the bpstat chain with locations that explain a
5639 target stop, while being careful to not set the target running,
5640 as that may invalidate locations (in particular watchpoint
5641 locations are recreated). Resuming will happen here with
5642 breakpoint conditions or watchpoint expressions that include
5643 inferior function calls. */
5644
5645 ALL_BREAKPOINTS (b)
5646 {
5647 if (!breakpoint_enabled (b))
5648 continue;
5649
5650 for (bl = b->loc; bl != NULL; bl = bl->next)
5651 {
5652 /* For hardware watchpoints, we look only at the first
5653 location. The watchpoint_check function will work on the
5654 entire expression, not the individual locations. For
5655 read watchpoints, the watchpoints_triggered function has
5656 checked all locations already. */
5657 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5658 break;
5659
5660 if (!bl->enabled || bl->shlib_disabled)
5661 continue;
5662
5663 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5664 continue;
5665
5666 /* Come here if it's a watchpoint, or if the break address
5667 matches. */
5668
5669 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5670 explain stop. */
5671
5672 /* Assume we stop. Should we find a watchpoint that is not
5673 actually triggered, or if the condition of the breakpoint
5674 evaluates as false, we'll reset 'stop' to 0. */
5675 bs->stop = 1;
5676 bs->print = 1;
5677
5678 /* If this is a scope breakpoint, mark the associated
5679 watchpoint as triggered so that we will handle the
5680 out-of-scope event. We'll get to the watchpoint next
5681 iteration. */
5682 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5683 {
5684 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5685
5686 w->watchpoint_triggered = watch_triggered_yes;
5687 }
5688 }
5689 }
5690
5691 /* Check if a moribund breakpoint explains the stop. */
5692 if (!target_supports_stopped_by_sw_breakpoint ()
5693 || !target_supports_stopped_by_hw_breakpoint ())
5694 {
5695 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5696 {
5697 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5698 && need_moribund_for_location_type (loc))
5699 {
5700 bs = bpstat_alloc (loc, &bs_link);
5701 /* For hits of moribund locations, we should just proceed. */
5702 bs->stop = 0;
5703 bs->print = 0;
5704 bs->print_it = print_it_noop;
5705 }
5706 }
5707 }
5708
5709 /* A bit of special processing for shlib breakpoints. We need to
5710 process solib loading here, so that the lists of loaded and
5711 unloaded libraries are correct before we handle "catch load" and
5712 "catch unload". */
5713 for (bs = bs_head; bs != NULL; bs = bs->next)
5714 {
5715 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5716 {
5717 handle_solib_event ();
5718 break;
5719 }
5720 }
5721
5722 /* Now go through the locations that caused the target to stop, and
5723 check whether we're interested in reporting this stop to higher
5724 layers, or whether we should resume the target transparently. */
5725
5726 removed_any = 0;
5727
5728 for (bs = bs_head; bs != NULL; bs = bs->next)
5729 {
5730 if (!bs->stop)
5731 continue;
5732
5733 b = bs->breakpoint_at;
5734 b->ops->check_status (bs);
5735 if (bs->stop)
5736 {
5737 bpstat_check_breakpoint_conditions (bs, ptid);
5738
5739 if (bs->stop)
5740 {
5741 ++(b->hit_count);
5742 observer_notify_breakpoint_modified (b);
5743
5744 /* We will stop here. */
5745 if (b->disposition == disp_disable)
5746 {
5747 --(b->enable_count);
5748 if (b->enable_count <= 0)
5749 b->enable_state = bp_disabled;
5750 removed_any = 1;
5751 }
5752 if (b->silent)
5753 bs->print = 0;
5754 bs->commands = b->commands;
5755 incref_counted_command_line (bs->commands);
5756 if (command_line_is_silent (bs->commands
5757 ? bs->commands->commands : NULL))
5758 bs->print = 0;
5759
5760 b->ops->after_condition_true (bs);
5761 }
5762
5763 }
5764
5765 /* Print nothing for this entry if we don't stop or don't
5766 print. */
5767 if (!bs->stop || !bs->print)
5768 bs->print_it = print_it_noop;
5769 }
5770
5771 /* If we aren't stopping, the value of some hardware watchpoint may
5772 not have changed, but the intermediate memory locations we are
5773 watching may have. Don't bother if we're stopping; this will get
5774 done later. */
5775 need_remove_insert = 0;
5776 if (! bpstat_causes_stop (bs_head))
5777 for (bs = bs_head; bs != NULL; bs = bs->next)
5778 if (!bs->stop
5779 && bs->breakpoint_at
5780 && is_hardware_watchpoint (bs->breakpoint_at))
5781 {
5782 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5783
5784 update_watchpoint (w, 0 /* don't reparse. */);
5785 need_remove_insert = 1;
5786 }
5787
5788 if (need_remove_insert)
5789 update_global_location_list (UGLL_MAY_INSERT);
5790 else if (removed_any)
5791 update_global_location_list (UGLL_DONT_INSERT);
5792
5793 return bs_head;
5794 }
5795
5796 static void
5797 handle_jit_event (void)
5798 {
5799 struct frame_info *frame;
5800 struct gdbarch *gdbarch;
5801
5802 if (debug_infrun)
5803 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5804
5805 /* Switch terminal for any messages produced by
5806 breakpoint_re_set. */
5807 target_terminal_ours_for_output ();
5808
5809 frame = get_current_frame ();
5810 gdbarch = get_frame_arch (frame);
5811
5812 jit_event_handler (gdbarch);
5813
5814 target_terminal_inferior ();
5815 }
5816
5817 /* Prepare WHAT final decision for infrun. */
5818
5819 /* Decide what infrun needs to do with this bpstat. */
5820
5821 struct bpstat_what
5822 bpstat_what (bpstat bs_head)
5823 {
5824 struct bpstat_what retval;
5825 bpstat bs;
5826
5827 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5828 retval.call_dummy = STOP_NONE;
5829 retval.is_longjmp = 0;
5830
5831 for (bs = bs_head; bs != NULL; bs = bs->next)
5832 {
5833 /* Extract this BS's action. After processing each BS, we check
5834 if its action overrides all we've seem so far. */
5835 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5836 enum bptype bptype;
5837
5838 if (bs->breakpoint_at == NULL)
5839 {
5840 /* I suspect this can happen if it was a momentary
5841 breakpoint which has since been deleted. */
5842 bptype = bp_none;
5843 }
5844 else
5845 bptype = bs->breakpoint_at->type;
5846
5847 switch (bptype)
5848 {
5849 case bp_none:
5850 break;
5851 case bp_breakpoint:
5852 case bp_hardware_breakpoint:
5853 case bp_single_step:
5854 case bp_until:
5855 case bp_finish:
5856 case bp_shlib_event:
5857 if (bs->stop)
5858 {
5859 if (bs->print)
5860 this_action = BPSTAT_WHAT_STOP_NOISY;
5861 else
5862 this_action = BPSTAT_WHAT_STOP_SILENT;
5863 }
5864 else
5865 this_action = BPSTAT_WHAT_SINGLE;
5866 break;
5867 case bp_watchpoint:
5868 case bp_hardware_watchpoint:
5869 case bp_read_watchpoint:
5870 case bp_access_watchpoint:
5871 if (bs->stop)
5872 {
5873 if (bs->print)
5874 this_action = BPSTAT_WHAT_STOP_NOISY;
5875 else
5876 this_action = BPSTAT_WHAT_STOP_SILENT;
5877 }
5878 else
5879 {
5880 /* There was a watchpoint, but we're not stopping.
5881 This requires no further action. */
5882 }
5883 break;
5884 case bp_longjmp:
5885 case bp_longjmp_call_dummy:
5886 case bp_exception:
5887 if (bs->stop)
5888 {
5889 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5890 retval.is_longjmp = bptype != bp_exception;
5891 }
5892 else
5893 this_action = BPSTAT_WHAT_SINGLE;
5894 break;
5895 case bp_longjmp_resume:
5896 case bp_exception_resume:
5897 if (bs->stop)
5898 {
5899 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5900 retval.is_longjmp = bptype == bp_longjmp_resume;
5901 }
5902 else
5903 this_action = BPSTAT_WHAT_SINGLE;
5904 break;
5905 case bp_step_resume:
5906 if (bs->stop)
5907 this_action = BPSTAT_WHAT_STEP_RESUME;
5908 else
5909 {
5910 /* It is for the wrong frame. */
5911 this_action = BPSTAT_WHAT_SINGLE;
5912 }
5913 break;
5914 case bp_hp_step_resume:
5915 if (bs->stop)
5916 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5917 else
5918 {
5919 /* It is for the wrong frame. */
5920 this_action = BPSTAT_WHAT_SINGLE;
5921 }
5922 break;
5923 case bp_watchpoint_scope:
5924 case bp_thread_event:
5925 case bp_overlay_event:
5926 case bp_longjmp_master:
5927 case bp_std_terminate_master:
5928 case bp_exception_master:
5929 this_action = BPSTAT_WHAT_SINGLE;
5930 break;
5931 case bp_catchpoint:
5932 if (bs->stop)
5933 {
5934 if (bs->print)
5935 this_action = BPSTAT_WHAT_STOP_NOISY;
5936 else
5937 this_action = BPSTAT_WHAT_STOP_SILENT;
5938 }
5939 else
5940 {
5941 /* There was a catchpoint, but we're not stopping.
5942 This requires no further action. */
5943 }
5944 break;
5945 case bp_jit_event:
5946 this_action = BPSTAT_WHAT_SINGLE;
5947 break;
5948 case bp_call_dummy:
5949 /* Make sure the action is stop (silent or noisy),
5950 so infrun.c pops the dummy frame. */
5951 retval.call_dummy = STOP_STACK_DUMMY;
5952 this_action = BPSTAT_WHAT_STOP_SILENT;
5953 break;
5954 case bp_std_terminate:
5955 /* Make sure the action is stop (silent or noisy),
5956 so infrun.c pops the dummy frame. */
5957 retval.call_dummy = STOP_STD_TERMINATE;
5958 this_action = BPSTAT_WHAT_STOP_SILENT;
5959 break;
5960 case bp_tracepoint:
5961 case bp_fast_tracepoint:
5962 case bp_static_tracepoint:
5963 /* Tracepoint hits should not be reported back to GDB, and
5964 if one got through somehow, it should have been filtered
5965 out already. */
5966 internal_error (__FILE__, __LINE__,
5967 _("bpstat_what: tracepoint encountered"));
5968 break;
5969 case bp_gnu_ifunc_resolver:
5970 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5971 this_action = BPSTAT_WHAT_SINGLE;
5972 break;
5973 case bp_gnu_ifunc_resolver_return:
5974 /* The breakpoint will be removed, execution will restart from the
5975 PC of the former breakpoint. */
5976 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5977 break;
5978
5979 case bp_dprintf:
5980 if (bs->stop)
5981 this_action = BPSTAT_WHAT_STOP_SILENT;
5982 else
5983 this_action = BPSTAT_WHAT_SINGLE;
5984 break;
5985
5986 default:
5987 internal_error (__FILE__, __LINE__,
5988 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5989 }
5990
5991 retval.main_action = std::max (retval.main_action, this_action);
5992 }
5993
5994 return retval;
5995 }
5996
5997 void
5998 bpstat_run_callbacks (bpstat bs_head)
5999 {
6000 bpstat bs;
6001
6002 for (bs = bs_head; bs != NULL; bs = bs->next)
6003 {
6004 struct breakpoint *b = bs->breakpoint_at;
6005
6006 if (b == NULL)
6007 continue;
6008 switch (b->type)
6009 {
6010 case bp_jit_event:
6011 handle_jit_event ();
6012 break;
6013 case bp_gnu_ifunc_resolver:
6014 gnu_ifunc_resolver_stop (b);
6015 break;
6016 case bp_gnu_ifunc_resolver_return:
6017 gnu_ifunc_resolver_return_stop (b);
6018 break;
6019 }
6020 }
6021 }
6022
6023 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6024 without hardware support). This isn't related to a specific bpstat,
6025 just to things like whether watchpoints are set. */
6026
6027 int
6028 bpstat_should_step (void)
6029 {
6030 struct breakpoint *b;
6031
6032 ALL_BREAKPOINTS (b)
6033 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6034 return 1;
6035 return 0;
6036 }
6037
6038 int
6039 bpstat_causes_stop (bpstat bs)
6040 {
6041 for (; bs != NULL; bs = bs->next)
6042 if (bs->stop)
6043 return 1;
6044
6045 return 0;
6046 }
6047
6048 \f
6049
6050 /* Compute a string of spaces suitable to indent the next line
6051 so it starts at the position corresponding to the table column
6052 named COL_NAME in the currently active table of UIOUT. */
6053
6054 static char *
6055 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6056 {
6057 static char wrap_indent[80];
6058 int i, total_width, width, align;
6059 const char *text;
6060
6061 total_width = 0;
6062 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6063 {
6064 if (strcmp (text, col_name) == 0)
6065 {
6066 gdb_assert (total_width < sizeof wrap_indent);
6067 memset (wrap_indent, ' ', total_width);
6068 wrap_indent[total_width] = 0;
6069
6070 return wrap_indent;
6071 }
6072
6073 total_width += width + 1;
6074 }
6075
6076 return NULL;
6077 }
6078
6079 /* Determine if the locations of this breakpoint will have their conditions
6080 evaluated by the target, host or a mix of both. Returns the following:
6081
6082 "host": Host evals condition.
6083 "host or target": Host or Target evals condition.
6084 "target": Target evals condition.
6085 */
6086
6087 static const char *
6088 bp_condition_evaluator (struct breakpoint *b)
6089 {
6090 struct bp_location *bl;
6091 char host_evals = 0;
6092 char target_evals = 0;
6093
6094 if (!b)
6095 return NULL;
6096
6097 if (!is_breakpoint (b))
6098 return NULL;
6099
6100 if (gdb_evaluates_breakpoint_condition_p ()
6101 || !target_supports_evaluation_of_breakpoint_conditions ())
6102 return condition_evaluation_host;
6103
6104 for (bl = b->loc; bl; bl = bl->next)
6105 {
6106 if (bl->cond_bytecode)
6107 target_evals++;
6108 else
6109 host_evals++;
6110 }
6111
6112 if (host_evals && target_evals)
6113 return condition_evaluation_both;
6114 else if (target_evals)
6115 return condition_evaluation_target;
6116 else
6117 return condition_evaluation_host;
6118 }
6119
6120 /* Determine the breakpoint location's condition evaluator. This is
6121 similar to bp_condition_evaluator, but for locations. */
6122
6123 static const char *
6124 bp_location_condition_evaluator (struct bp_location *bl)
6125 {
6126 if (bl && !is_breakpoint (bl->owner))
6127 return NULL;
6128
6129 if (gdb_evaluates_breakpoint_condition_p ()
6130 || !target_supports_evaluation_of_breakpoint_conditions ())
6131 return condition_evaluation_host;
6132
6133 if (bl && bl->cond_bytecode)
6134 return condition_evaluation_target;
6135 else
6136 return condition_evaluation_host;
6137 }
6138
6139 /* Print the LOC location out of the list of B->LOC locations. */
6140
6141 static void
6142 print_breakpoint_location (struct breakpoint *b,
6143 struct bp_location *loc)
6144 {
6145 struct ui_out *uiout = current_uiout;
6146
6147 scoped_restore_current_program_space restore_pspace;
6148
6149 if (loc != NULL && loc->shlib_disabled)
6150 loc = NULL;
6151
6152 if (loc != NULL)
6153 set_current_program_space (loc->pspace);
6154
6155 if (b->display_canonical)
6156 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6157 else if (loc && loc->symtab)
6158 {
6159 struct symbol *sym
6160 = find_pc_sect_function (loc->address, loc->section);
6161 if (sym)
6162 {
6163 uiout->text ("in ");
6164 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6165 uiout->text (" ");
6166 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6167 uiout->text ("at ");
6168 }
6169 uiout->field_string ("file",
6170 symtab_to_filename_for_display (loc->symtab));
6171 uiout->text (":");
6172
6173 if (uiout->is_mi_like_p ())
6174 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6175
6176 uiout->field_int ("line", loc->line_number);
6177 }
6178 else if (loc)
6179 {
6180 string_file stb;
6181
6182 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6183 demangle, "");
6184 uiout->field_stream ("at", stb);
6185 }
6186 else
6187 {
6188 uiout->field_string ("pending",
6189 event_location_to_string (b->location.get ()));
6190 /* If extra_string is available, it could be holding a condition
6191 or dprintf arguments. In either case, make sure it is printed,
6192 too, but only for non-MI streams. */
6193 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6194 {
6195 if (b->type == bp_dprintf)
6196 uiout->text (",");
6197 else
6198 uiout->text (" ");
6199 uiout->text (b->extra_string);
6200 }
6201 }
6202
6203 if (loc && is_breakpoint (b)
6204 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6205 && bp_condition_evaluator (b) == condition_evaluation_both)
6206 {
6207 uiout->text (" (");
6208 uiout->field_string ("evaluated-by",
6209 bp_location_condition_evaluator (loc));
6210 uiout->text (")");
6211 }
6212 }
6213
6214 static const char *
6215 bptype_string (enum bptype type)
6216 {
6217 struct ep_type_description
6218 {
6219 enum bptype type;
6220 const char *description;
6221 };
6222 static struct ep_type_description bptypes[] =
6223 {
6224 {bp_none, "?deleted?"},
6225 {bp_breakpoint, "breakpoint"},
6226 {bp_hardware_breakpoint, "hw breakpoint"},
6227 {bp_single_step, "sw single-step"},
6228 {bp_until, "until"},
6229 {bp_finish, "finish"},
6230 {bp_watchpoint, "watchpoint"},
6231 {bp_hardware_watchpoint, "hw watchpoint"},
6232 {bp_read_watchpoint, "read watchpoint"},
6233 {bp_access_watchpoint, "acc watchpoint"},
6234 {bp_longjmp, "longjmp"},
6235 {bp_longjmp_resume, "longjmp resume"},
6236 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6237 {bp_exception, "exception"},
6238 {bp_exception_resume, "exception resume"},
6239 {bp_step_resume, "step resume"},
6240 {bp_hp_step_resume, "high-priority step resume"},
6241 {bp_watchpoint_scope, "watchpoint scope"},
6242 {bp_call_dummy, "call dummy"},
6243 {bp_std_terminate, "std::terminate"},
6244 {bp_shlib_event, "shlib events"},
6245 {bp_thread_event, "thread events"},
6246 {bp_overlay_event, "overlay events"},
6247 {bp_longjmp_master, "longjmp master"},
6248 {bp_std_terminate_master, "std::terminate master"},
6249 {bp_exception_master, "exception master"},
6250 {bp_catchpoint, "catchpoint"},
6251 {bp_tracepoint, "tracepoint"},
6252 {bp_fast_tracepoint, "fast tracepoint"},
6253 {bp_static_tracepoint, "static tracepoint"},
6254 {bp_dprintf, "dprintf"},
6255 {bp_jit_event, "jit events"},
6256 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6257 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6258 };
6259
6260 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6261 || ((int) type != bptypes[(int) type].type))
6262 internal_error (__FILE__, __LINE__,
6263 _("bptypes table does not describe type #%d."),
6264 (int) type);
6265
6266 return bptypes[(int) type].description;
6267 }
6268
6269 /* For MI, output a field named 'thread-groups' with a list as the value.
6270 For CLI, prefix the list with the string 'inf'. */
6271
6272 static void
6273 output_thread_groups (struct ui_out *uiout,
6274 const char *field_name,
6275 VEC(int) *inf_num,
6276 int mi_only)
6277 {
6278 int is_mi = uiout->is_mi_like_p ();
6279 int inf;
6280 int i;
6281
6282 /* For backward compatibility, don't display inferiors in CLI unless
6283 there are several. Always display them for MI. */
6284 if (!is_mi && mi_only)
6285 return;
6286
6287 ui_out_emit_list list_emitter (uiout, field_name);
6288
6289 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6290 {
6291 if (is_mi)
6292 {
6293 char mi_group[10];
6294
6295 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6296 uiout->field_string (NULL, mi_group);
6297 }
6298 else
6299 {
6300 if (i == 0)
6301 uiout->text (" inf ");
6302 else
6303 uiout->text (", ");
6304
6305 uiout->text (plongest (inf));
6306 }
6307 }
6308 }
6309
6310 /* Print B to gdb_stdout. */
6311
6312 static void
6313 print_one_breakpoint_location (struct breakpoint *b,
6314 struct bp_location *loc,
6315 int loc_number,
6316 struct bp_location **last_loc,
6317 int allflag)
6318 {
6319 struct command_line *l;
6320 static char bpenables[] = "nynny";
6321
6322 struct ui_out *uiout = current_uiout;
6323 int header_of_multiple = 0;
6324 int part_of_multiple = (loc != NULL);
6325 struct value_print_options opts;
6326
6327 get_user_print_options (&opts);
6328
6329 gdb_assert (!loc || loc_number != 0);
6330 /* See comment in print_one_breakpoint concerning treatment of
6331 breakpoints with single disabled location. */
6332 if (loc == NULL
6333 && (b->loc != NULL
6334 && (b->loc->next != NULL || !b->loc->enabled)))
6335 header_of_multiple = 1;
6336 if (loc == NULL)
6337 loc = b->loc;
6338
6339 annotate_record ();
6340
6341 /* 1 */
6342 annotate_field (0);
6343 if (part_of_multiple)
6344 {
6345 char *formatted;
6346 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6347 uiout->field_string ("number", formatted);
6348 xfree (formatted);
6349 }
6350 else
6351 {
6352 uiout->field_int ("number", b->number);
6353 }
6354
6355 /* 2 */
6356 annotate_field (1);
6357 if (part_of_multiple)
6358 uiout->field_skip ("type");
6359 else
6360 uiout->field_string ("type", bptype_string (b->type));
6361
6362 /* 3 */
6363 annotate_field (2);
6364 if (part_of_multiple)
6365 uiout->field_skip ("disp");
6366 else
6367 uiout->field_string ("disp", bpdisp_text (b->disposition));
6368
6369
6370 /* 4 */
6371 annotate_field (3);
6372 if (part_of_multiple)
6373 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6374 else
6375 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6376 uiout->spaces (2);
6377
6378
6379 /* 5 and 6 */
6380 if (b->ops != NULL && b->ops->print_one != NULL)
6381 {
6382 /* Although the print_one can possibly print all locations,
6383 calling it here is not likely to get any nice result. So,
6384 make sure there's just one location. */
6385 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6386 b->ops->print_one (b, last_loc);
6387 }
6388 else
6389 switch (b->type)
6390 {
6391 case bp_none:
6392 internal_error (__FILE__, __LINE__,
6393 _("print_one_breakpoint: bp_none encountered\n"));
6394 break;
6395
6396 case bp_watchpoint:
6397 case bp_hardware_watchpoint:
6398 case bp_read_watchpoint:
6399 case bp_access_watchpoint:
6400 {
6401 struct watchpoint *w = (struct watchpoint *) b;
6402
6403 /* Field 4, the address, is omitted (which makes the columns
6404 not line up too nicely with the headers, but the effect
6405 is relatively readable). */
6406 if (opts.addressprint)
6407 uiout->field_skip ("addr");
6408 annotate_field (5);
6409 uiout->field_string ("what", w->exp_string);
6410 }
6411 break;
6412
6413 case bp_breakpoint:
6414 case bp_hardware_breakpoint:
6415 case bp_single_step:
6416 case bp_until:
6417 case bp_finish:
6418 case bp_longjmp:
6419 case bp_longjmp_resume:
6420 case bp_longjmp_call_dummy:
6421 case bp_exception:
6422 case bp_exception_resume:
6423 case bp_step_resume:
6424 case bp_hp_step_resume:
6425 case bp_watchpoint_scope:
6426 case bp_call_dummy:
6427 case bp_std_terminate:
6428 case bp_shlib_event:
6429 case bp_thread_event:
6430 case bp_overlay_event:
6431 case bp_longjmp_master:
6432 case bp_std_terminate_master:
6433 case bp_exception_master:
6434 case bp_tracepoint:
6435 case bp_fast_tracepoint:
6436 case bp_static_tracepoint:
6437 case bp_dprintf:
6438 case bp_jit_event:
6439 case bp_gnu_ifunc_resolver:
6440 case bp_gnu_ifunc_resolver_return:
6441 if (opts.addressprint)
6442 {
6443 annotate_field (4);
6444 if (header_of_multiple)
6445 uiout->field_string ("addr", "<MULTIPLE>");
6446 else if (b->loc == NULL || loc->shlib_disabled)
6447 uiout->field_string ("addr", "<PENDING>");
6448 else
6449 uiout->field_core_addr ("addr",
6450 loc->gdbarch, loc->address);
6451 }
6452 annotate_field (5);
6453 if (!header_of_multiple)
6454 print_breakpoint_location (b, loc);
6455 if (b->loc)
6456 *last_loc = b->loc;
6457 break;
6458 }
6459
6460
6461 if (loc != NULL && !header_of_multiple)
6462 {
6463 struct inferior *inf;
6464 VEC(int) *inf_num = NULL;
6465 int mi_only = 1;
6466
6467 ALL_INFERIORS (inf)
6468 {
6469 if (inf->pspace == loc->pspace)
6470 VEC_safe_push (int, inf_num, inf->num);
6471 }
6472
6473 /* For backward compatibility, don't display inferiors in CLI unless
6474 there are several. Always display for MI. */
6475 if (allflag
6476 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6477 && (number_of_program_spaces () > 1
6478 || number_of_inferiors () > 1)
6479 /* LOC is for existing B, it cannot be in
6480 moribund_locations and thus having NULL OWNER. */
6481 && loc->owner->type != bp_catchpoint))
6482 mi_only = 0;
6483 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6484 VEC_free (int, inf_num);
6485 }
6486
6487 if (!part_of_multiple)
6488 {
6489 if (b->thread != -1)
6490 {
6491 /* FIXME: This seems to be redundant and lost here; see the
6492 "stop only in" line a little further down. */
6493 uiout->text (" thread ");
6494 uiout->field_int ("thread", b->thread);
6495 }
6496 else if (b->task != 0)
6497 {
6498 uiout->text (" task ");
6499 uiout->field_int ("task", b->task);
6500 }
6501 }
6502
6503 uiout->text ("\n");
6504
6505 if (!part_of_multiple)
6506 b->ops->print_one_detail (b, uiout);
6507
6508 if (part_of_multiple && frame_id_p (b->frame_id))
6509 {
6510 annotate_field (6);
6511 uiout->text ("\tstop only in stack frame at ");
6512 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6513 the frame ID. */
6514 uiout->field_core_addr ("frame",
6515 b->gdbarch, b->frame_id.stack_addr);
6516 uiout->text ("\n");
6517 }
6518
6519 if (!part_of_multiple && b->cond_string)
6520 {
6521 annotate_field (7);
6522 if (is_tracepoint (b))
6523 uiout->text ("\ttrace only if ");
6524 else
6525 uiout->text ("\tstop only if ");
6526 uiout->field_string ("cond", b->cond_string);
6527
6528 /* Print whether the target is doing the breakpoint's condition
6529 evaluation. If GDB is doing the evaluation, don't print anything. */
6530 if (is_breakpoint (b)
6531 && breakpoint_condition_evaluation_mode ()
6532 == condition_evaluation_target)
6533 {
6534 uiout->text (" (");
6535 uiout->field_string ("evaluated-by",
6536 bp_condition_evaluator (b));
6537 uiout->text (" evals)");
6538 }
6539 uiout->text ("\n");
6540 }
6541
6542 if (!part_of_multiple && b->thread != -1)
6543 {
6544 /* FIXME should make an annotation for this. */
6545 uiout->text ("\tstop only in thread ");
6546 if (uiout->is_mi_like_p ())
6547 uiout->field_int ("thread", b->thread);
6548 else
6549 {
6550 struct thread_info *thr = find_thread_global_id (b->thread);
6551
6552 uiout->field_string ("thread", print_thread_id (thr));
6553 }
6554 uiout->text ("\n");
6555 }
6556
6557 if (!part_of_multiple)
6558 {
6559 if (b->hit_count)
6560 {
6561 /* FIXME should make an annotation for this. */
6562 if (is_catchpoint (b))
6563 uiout->text ("\tcatchpoint");
6564 else if (is_tracepoint (b))
6565 uiout->text ("\ttracepoint");
6566 else
6567 uiout->text ("\tbreakpoint");
6568 uiout->text (" already hit ");
6569 uiout->field_int ("times", b->hit_count);
6570 if (b->hit_count == 1)
6571 uiout->text (" time\n");
6572 else
6573 uiout->text (" times\n");
6574 }
6575 else
6576 {
6577 /* Output the count also if it is zero, but only if this is mi. */
6578 if (uiout->is_mi_like_p ())
6579 uiout->field_int ("times", b->hit_count);
6580 }
6581 }
6582
6583 if (!part_of_multiple && b->ignore_count)
6584 {
6585 annotate_field (8);
6586 uiout->text ("\tignore next ");
6587 uiout->field_int ("ignore", b->ignore_count);
6588 uiout->text (" hits\n");
6589 }
6590
6591 /* Note that an enable count of 1 corresponds to "enable once"
6592 behavior, which is reported by the combination of enablement and
6593 disposition, so we don't need to mention it here. */
6594 if (!part_of_multiple && b->enable_count > 1)
6595 {
6596 annotate_field (8);
6597 uiout->text ("\tdisable after ");
6598 /* Tweak the wording to clarify that ignore and enable counts
6599 are distinct, and have additive effect. */
6600 if (b->ignore_count)
6601 uiout->text ("additional ");
6602 else
6603 uiout->text ("next ");
6604 uiout->field_int ("enable", b->enable_count);
6605 uiout->text (" hits\n");
6606 }
6607
6608 if (!part_of_multiple && is_tracepoint (b))
6609 {
6610 struct tracepoint *tp = (struct tracepoint *) b;
6611
6612 if (tp->traceframe_usage)
6613 {
6614 uiout->text ("\ttrace buffer usage ");
6615 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6616 uiout->text (" bytes\n");
6617 }
6618 }
6619
6620 l = b->commands ? b->commands->commands : NULL;
6621 if (!part_of_multiple && l)
6622 {
6623 annotate_field (9);
6624 ui_out_emit_tuple tuple_emitter (uiout, "script");
6625 print_command_lines (uiout, l, 4);
6626 }
6627
6628 if (is_tracepoint (b))
6629 {
6630 struct tracepoint *t = (struct tracepoint *) b;
6631
6632 if (!part_of_multiple && t->pass_count)
6633 {
6634 annotate_field (10);
6635 uiout->text ("\tpass count ");
6636 uiout->field_int ("pass", t->pass_count);
6637 uiout->text (" \n");
6638 }
6639
6640 /* Don't display it when tracepoint or tracepoint location is
6641 pending. */
6642 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6643 {
6644 annotate_field (11);
6645
6646 if (uiout->is_mi_like_p ())
6647 uiout->field_string ("installed",
6648 loc->inserted ? "y" : "n");
6649 else
6650 {
6651 if (loc->inserted)
6652 uiout->text ("\t");
6653 else
6654 uiout->text ("\tnot ");
6655 uiout->text ("installed on target\n");
6656 }
6657 }
6658 }
6659
6660 if (uiout->is_mi_like_p () && !part_of_multiple)
6661 {
6662 if (is_watchpoint (b))
6663 {
6664 struct watchpoint *w = (struct watchpoint *) b;
6665
6666 uiout->field_string ("original-location", w->exp_string);
6667 }
6668 else if (b->location != NULL
6669 && event_location_to_string (b->location.get ()) != NULL)
6670 uiout->field_string ("original-location",
6671 event_location_to_string (b->location.get ()));
6672 }
6673 }
6674
6675 static void
6676 print_one_breakpoint (struct breakpoint *b,
6677 struct bp_location **last_loc,
6678 int allflag)
6679 {
6680 struct ui_out *uiout = current_uiout;
6681
6682 {
6683 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6684
6685 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6686 }
6687
6688 /* If this breakpoint has custom print function,
6689 it's already printed. Otherwise, print individual
6690 locations, if any. */
6691 if (b->ops == NULL || b->ops->print_one == NULL)
6692 {
6693 /* If breakpoint has a single location that is disabled, we
6694 print it as if it had several locations, since otherwise it's
6695 hard to represent "breakpoint enabled, location disabled"
6696 situation.
6697
6698 Note that while hardware watchpoints have several locations
6699 internally, that's not a property exposed to user. */
6700 if (b->loc
6701 && !is_hardware_watchpoint (b)
6702 && (b->loc->next || !b->loc->enabled))
6703 {
6704 struct bp_location *loc;
6705 int n = 1;
6706
6707 for (loc = b->loc; loc; loc = loc->next, ++n)
6708 {
6709 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6710 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6711 }
6712 }
6713 }
6714 }
6715
6716 static int
6717 breakpoint_address_bits (struct breakpoint *b)
6718 {
6719 int print_address_bits = 0;
6720 struct bp_location *loc;
6721
6722 /* Software watchpoints that aren't watching memory don't have an
6723 address to print. */
6724 if (is_no_memory_software_watchpoint (b))
6725 return 0;
6726
6727 for (loc = b->loc; loc; loc = loc->next)
6728 {
6729 int addr_bit;
6730
6731 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6732 if (addr_bit > print_address_bits)
6733 print_address_bits = addr_bit;
6734 }
6735
6736 return print_address_bits;
6737 }
6738
6739 struct captured_breakpoint_query_args
6740 {
6741 int bnum;
6742 };
6743
6744 static int
6745 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6746 {
6747 struct captured_breakpoint_query_args *args
6748 = (struct captured_breakpoint_query_args *) data;
6749 struct breakpoint *b;
6750 struct bp_location *dummy_loc = NULL;
6751
6752 ALL_BREAKPOINTS (b)
6753 {
6754 if (args->bnum == b->number)
6755 {
6756 print_one_breakpoint (b, &dummy_loc, 0);
6757 return GDB_RC_OK;
6758 }
6759 }
6760 return GDB_RC_NONE;
6761 }
6762
6763 enum gdb_rc
6764 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6765 char **error_message)
6766 {
6767 struct captured_breakpoint_query_args args;
6768
6769 args.bnum = bnum;
6770 /* For the moment we don't trust print_one_breakpoint() to not throw
6771 an error. */
6772 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6773 error_message, RETURN_MASK_ALL) < 0)
6774 return GDB_RC_FAIL;
6775 else
6776 return GDB_RC_OK;
6777 }
6778
6779 /* Return true if this breakpoint was set by the user, false if it is
6780 internal or momentary. */
6781
6782 int
6783 user_breakpoint_p (struct breakpoint *b)
6784 {
6785 return b->number > 0;
6786 }
6787
6788 /* See breakpoint.h. */
6789
6790 int
6791 pending_breakpoint_p (struct breakpoint *b)
6792 {
6793 return b->loc == NULL;
6794 }
6795
6796 /* Print information on user settable breakpoint (watchpoint, etc)
6797 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6798 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6799 FILTER is non-NULL, call it on each breakpoint and only include the
6800 ones for which it returns non-zero. Return the total number of
6801 breakpoints listed. */
6802
6803 static int
6804 breakpoint_1 (char *args, int allflag,
6805 int (*filter) (const struct breakpoint *))
6806 {
6807 struct breakpoint *b;
6808 struct bp_location *last_loc = NULL;
6809 int nr_printable_breakpoints;
6810 struct cleanup *bkpttbl_chain;
6811 struct value_print_options opts;
6812 int print_address_bits = 0;
6813 int print_type_col_width = 14;
6814 struct ui_out *uiout = current_uiout;
6815
6816 get_user_print_options (&opts);
6817
6818 /* Compute the number of rows in the table, as well as the size
6819 required for address fields. */
6820 nr_printable_breakpoints = 0;
6821 ALL_BREAKPOINTS (b)
6822 {
6823 /* If we have a filter, only list the breakpoints it accepts. */
6824 if (filter && !filter (b))
6825 continue;
6826
6827 /* If we have an "args" string, it is a list of breakpoints to
6828 accept. Skip the others. */
6829 if (args != NULL && *args != '\0')
6830 {
6831 if (allflag && parse_and_eval_long (args) != b->number)
6832 continue;
6833 if (!allflag && !number_is_in_list (args, b->number))
6834 continue;
6835 }
6836
6837 if (allflag || user_breakpoint_p (b))
6838 {
6839 int addr_bit, type_len;
6840
6841 addr_bit = breakpoint_address_bits (b);
6842 if (addr_bit > print_address_bits)
6843 print_address_bits = addr_bit;
6844
6845 type_len = strlen (bptype_string (b->type));
6846 if (type_len > print_type_col_width)
6847 print_type_col_width = type_len;
6848
6849 nr_printable_breakpoints++;
6850 }
6851 }
6852
6853 if (opts.addressprint)
6854 bkpttbl_chain
6855 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6856 nr_printable_breakpoints,
6857 "BreakpointTable");
6858 else
6859 bkpttbl_chain
6860 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6861 nr_printable_breakpoints,
6862 "BreakpointTable");
6863
6864 if (nr_printable_breakpoints > 0)
6865 annotate_breakpoints_headers ();
6866 if (nr_printable_breakpoints > 0)
6867 annotate_field (0);
6868 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (1);
6871 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6872 if (nr_printable_breakpoints > 0)
6873 annotate_field (2);
6874 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (3);
6877 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6878 if (opts.addressprint)
6879 {
6880 if (nr_printable_breakpoints > 0)
6881 annotate_field (4);
6882 if (print_address_bits <= 32)
6883 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6884 else
6885 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6886 }
6887 if (nr_printable_breakpoints > 0)
6888 annotate_field (5);
6889 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6890 uiout->table_body ();
6891 if (nr_printable_breakpoints > 0)
6892 annotate_breakpoints_table ();
6893
6894 ALL_BREAKPOINTS (b)
6895 {
6896 QUIT;
6897 /* If we have a filter, only list the breakpoints it accepts. */
6898 if (filter && !filter (b))
6899 continue;
6900
6901 /* If we have an "args" string, it is a list of breakpoints to
6902 accept. Skip the others. */
6903
6904 if (args != NULL && *args != '\0')
6905 {
6906 if (allflag) /* maintenance info breakpoint */
6907 {
6908 if (parse_and_eval_long (args) != b->number)
6909 continue;
6910 }
6911 else /* all others */
6912 {
6913 if (!number_is_in_list (args, b->number))
6914 continue;
6915 }
6916 }
6917 /* We only print out user settable breakpoints unless the
6918 allflag is set. */
6919 if (allflag || user_breakpoint_p (b))
6920 print_one_breakpoint (b, &last_loc, allflag);
6921 }
6922
6923 do_cleanups (bkpttbl_chain);
6924
6925 if (nr_printable_breakpoints == 0)
6926 {
6927 /* If there's a filter, let the caller decide how to report
6928 empty list. */
6929 if (!filter)
6930 {
6931 if (args == NULL || *args == '\0')
6932 uiout->message ("No breakpoints or watchpoints.\n");
6933 else
6934 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6935 args);
6936 }
6937 }
6938 else
6939 {
6940 if (last_loc && !server_command)
6941 set_next_address (last_loc->gdbarch, last_loc->address);
6942 }
6943
6944 /* FIXME? Should this be moved up so that it is only called when
6945 there have been breakpoints? */
6946 annotate_breakpoints_table_end ();
6947
6948 return nr_printable_breakpoints;
6949 }
6950
6951 /* Display the value of default-collect in a way that is generally
6952 compatible with the breakpoint list. */
6953
6954 static void
6955 default_collect_info (void)
6956 {
6957 struct ui_out *uiout = current_uiout;
6958
6959 /* If it has no value (which is frequently the case), say nothing; a
6960 message like "No default-collect." gets in user's face when it's
6961 not wanted. */
6962 if (!*default_collect)
6963 return;
6964
6965 /* The following phrase lines up nicely with per-tracepoint collect
6966 actions. */
6967 uiout->text ("default collect ");
6968 uiout->field_string ("default-collect", default_collect);
6969 uiout->text (" \n");
6970 }
6971
6972 static void
6973 breakpoints_info (char *args, int from_tty)
6974 {
6975 breakpoint_1 (args, 0, NULL);
6976
6977 default_collect_info ();
6978 }
6979
6980 static void
6981 watchpoints_info (char *args, int from_tty)
6982 {
6983 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6984 struct ui_out *uiout = current_uiout;
6985
6986 if (num_printed == 0)
6987 {
6988 if (args == NULL || *args == '\0')
6989 uiout->message ("No watchpoints.\n");
6990 else
6991 uiout->message ("No watchpoint matching '%s'.\n", args);
6992 }
6993 }
6994
6995 static void
6996 maintenance_info_breakpoints (char *args, int from_tty)
6997 {
6998 breakpoint_1 (args, 1, NULL);
6999
7000 default_collect_info ();
7001 }
7002
7003 static int
7004 breakpoint_has_pc (struct breakpoint *b,
7005 struct program_space *pspace,
7006 CORE_ADDR pc, struct obj_section *section)
7007 {
7008 struct bp_location *bl = b->loc;
7009
7010 for (; bl; bl = bl->next)
7011 {
7012 if (bl->pspace == pspace
7013 && bl->address == pc
7014 && (!overlay_debugging || bl->section == section))
7015 return 1;
7016 }
7017 return 0;
7018 }
7019
7020 /* Print a message describing any user-breakpoints set at PC. This
7021 concerns with logical breakpoints, so we match program spaces, not
7022 address spaces. */
7023
7024 static void
7025 describe_other_breakpoints (struct gdbarch *gdbarch,
7026 struct program_space *pspace, CORE_ADDR pc,
7027 struct obj_section *section, int thread)
7028 {
7029 int others = 0;
7030 struct breakpoint *b;
7031
7032 ALL_BREAKPOINTS (b)
7033 others += (user_breakpoint_p (b)
7034 && breakpoint_has_pc (b, pspace, pc, section));
7035 if (others > 0)
7036 {
7037 if (others == 1)
7038 printf_filtered (_("Note: breakpoint "));
7039 else /* if (others == ???) */
7040 printf_filtered (_("Note: breakpoints "));
7041 ALL_BREAKPOINTS (b)
7042 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7043 {
7044 others--;
7045 printf_filtered ("%d", b->number);
7046 if (b->thread == -1 && thread != -1)
7047 printf_filtered (" (all threads)");
7048 else if (b->thread != -1)
7049 printf_filtered (" (thread %d)", b->thread);
7050 printf_filtered ("%s%s ",
7051 ((b->enable_state == bp_disabled
7052 || b->enable_state == bp_call_disabled)
7053 ? " (disabled)"
7054 : ""),
7055 (others > 1) ? ","
7056 : ((others == 1) ? " and" : ""));
7057 }
7058 printf_filtered (_("also set at pc "));
7059 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7060 printf_filtered (".\n");
7061 }
7062 }
7063 \f
7064
7065 /* Return true iff it is meaningful to use the address member of
7066 BPT locations. For some breakpoint types, the locations' address members
7067 are irrelevant and it makes no sense to attempt to compare them to other
7068 addresses (or use them for any other purpose either).
7069
7070 More specifically, each of the following breakpoint types will
7071 always have a zero valued location address and we don't want to mark
7072 breakpoints of any of these types to be a duplicate of an actual
7073 breakpoint location at address zero:
7074
7075 bp_watchpoint
7076 bp_catchpoint
7077
7078 */
7079
7080 static int
7081 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7082 {
7083 enum bptype type = bpt->type;
7084
7085 return (type != bp_watchpoint && type != bp_catchpoint);
7086 }
7087
7088 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7089 true if LOC1 and LOC2 represent the same watchpoint location. */
7090
7091 static int
7092 watchpoint_locations_match (struct bp_location *loc1,
7093 struct bp_location *loc2)
7094 {
7095 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7096 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7097
7098 /* Both of them must exist. */
7099 gdb_assert (w1 != NULL);
7100 gdb_assert (w2 != NULL);
7101
7102 /* If the target can evaluate the condition expression in hardware,
7103 then we we need to insert both watchpoints even if they are at
7104 the same place. Otherwise the watchpoint will only trigger when
7105 the condition of whichever watchpoint was inserted evaluates to
7106 true, not giving a chance for GDB to check the condition of the
7107 other watchpoint. */
7108 if ((w1->cond_exp
7109 && target_can_accel_watchpoint_condition (loc1->address,
7110 loc1->length,
7111 loc1->watchpoint_type,
7112 w1->cond_exp.get ()))
7113 || (w2->cond_exp
7114 && target_can_accel_watchpoint_condition (loc2->address,
7115 loc2->length,
7116 loc2->watchpoint_type,
7117 w2->cond_exp.get ())))
7118 return 0;
7119
7120 /* Note that this checks the owner's type, not the location's. In
7121 case the target does not support read watchpoints, but does
7122 support access watchpoints, we'll have bp_read_watchpoint
7123 watchpoints with hw_access locations. Those should be considered
7124 duplicates of hw_read locations. The hw_read locations will
7125 become hw_access locations later. */
7126 return (loc1->owner->type == loc2->owner->type
7127 && loc1->pspace->aspace == loc2->pspace->aspace
7128 && loc1->address == loc2->address
7129 && loc1->length == loc2->length);
7130 }
7131
7132 /* See breakpoint.h. */
7133
7134 int
7135 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7136 struct address_space *aspace2, CORE_ADDR addr2)
7137 {
7138 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7139 || aspace1 == aspace2)
7140 && addr1 == addr2);
7141 }
7142
7143 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7144 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7145 matches ASPACE2. On targets that have global breakpoints, the address
7146 space doesn't really matter. */
7147
7148 static int
7149 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7150 int len1, struct address_space *aspace2,
7151 CORE_ADDR addr2)
7152 {
7153 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7154 || aspace1 == aspace2)
7155 && addr2 >= addr1 && addr2 < addr1 + len1);
7156 }
7157
7158 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7159 a ranged breakpoint. In most targets, a match happens only if ASPACE
7160 matches the breakpoint's address space. On targets that have global
7161 breakpoints, the address space doesn't really matter. */
7162
7163 static int
7164 breakpoint_location_address_match (struct bp_location *bl,
7165 struct address_space *aspace,
7166 CORE_ADDR addr)
7167 {
7168 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7169 aspace, addr)
7170 || (bl->length
7171 && breakpoint_address_match_range (bl->pspace->aspace,
7172 bl->address, bl->length,
7173 aspace, addr)));
7174 }
7175
7176 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7177 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7178 match happens only if ASPACE matches the breakpoint's address
7179 space. On targets that have global breakpoints, the address space
7180 doesn't really matter. */
7181
7182 static int
7183 breakpoint_location_address_range_overlap (struct bp_location *bl,
7184 struct address_space *aspace,
7185 CORE_ADDR addr, int len)
7186 {
7187 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7188 || bl->pspace->aspace == aspace)
7189 {
7190 int bl_len = bl->length != 0 ? bl->length : 1;
7191
7192 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7193 return 1;
7194 }
7195 return 0;
7196 }
7197
7198 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7199 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7200 true, otherwise returns false. */
7201
7202 static int
7203 tracepoint_locations_match (struct bp_location *loc1,
7204 struct bp_location *loc2)
7205 {
7206 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7207 /* Since tracepoint locations are never duplicated with others', tracepoint
7208 locations at the same address of different tracepoints are regarded as
7209 different locations. */
7210 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7211 else
7212 return 0;
7213 }
7214
7215 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7216 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7217 represent the same location. */
7218
7219 static int
7220 breakpoint_locations_match (struct bp_location *loc1,
7221 struct bp_location *loc2)
7222 {
7223 int hw_point1, hw_point2;
7224
7225 /* Both of them must not be in moribund_locations. */
7226 gdb_assert (loc1->owner != NULL);
7227 gdb_assert (loc2->owner != NULL);
7228
7229 hw_point1 = is_hardware_watchpoint (loc1->owner);
7230 hw_point2 = is_hardware_watchpoint (loc2->owner);
7231
7232 if (hw_point1 != hw_point2)
7233 return 0;
7234 else if (hw_point1)
7235 return watchpoint_locations_match (loc1, loc2);
7236 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7237 return tracepoint_locations_match (loc1, loc2);
7238 else
7239 /* We compare bp_location.length in order to cover ranged breakpoints. */
7240 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7241 loc2->pspace->aspace, loc2->address)
7242 && loc1->length == loc2->length);
7243 }
7244
7245 static void
7246 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7247 int bnum, int have_bnum)
7248 {
7249 /* The longest string possibly returned by hex_string_custom
7250 is 50 chars. These must be at least that big for safety. */
7251 char astr1[64];
7252 char astr2[64];
7253
7254 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7255 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7256 if (have_bnum)
7257 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7258 bnum, astr1, astr2);
7259 else
7260 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7261 }
7262
7263 /* Adjust a breakpoint's address to account for architectural
7264 constraints on breakpoint placement. Return the adjusted address.
7265 Note: Very few targets require this kind of adjustment. For most
7266 targets, this function is simply the identity function. */
7267
7268 static CORE_ADDR
7269 adjust_breakpoint_address (struct gdbarch *gdbarch,
7270 CORE_ADDR bpaddr, enum bptype bptype)
7271 {
7272 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7273 {
7274 /* Very few targets need any kind of breakpoint adjustment. */
7275 return bpaddr;
7276 }
7277 else if (bptype == bp_watchpoint
7278 || bptype == bp_hardware_watchpoint
7279 || bptype == bp_read_watchpoint
7280 || bptype == bp_access_watchpoint
7281 || bptype == bp_catchpoint)
7282 {
7283 /* Watchpoints and the various bp_catch_* eventpoints should not
7284 have their addresses modified. */
7285 return bpaddr;
7286 }
7287 else if (bptype == bp_single_step)
7288 {
7289 /* Single-step breakpoints should not have their addresses
7290 modified. If there's any architectural constrain that
7291 applies to this address, then it should have already been
7292 taken into account when the breakpoint was created in the
7293 first place. If we didn't do this, stepping through e.g.,
7294 Thumb-2 IT blocks would break. */
7295 return bpaddr;
7296 }
7297 else
7298 {
7299 CORE_ADDR adjusted_bpaddr;
7300
7301 /* Some targets have architectural constraints on the placement
7302 of breakpoint instructions. Obtain the adjusted address. */
7303 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7304
7305 /* An adjusted breakpoint address can significantly alter
7306 a user's expectations. Print a warning if an adjustment
7307 is required. */
7308 if (adjusted_bpaddr != bpaddr)
7309 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7310
7311 return adjusted_bpaddr;
7312 }
7313 }
7314
7315 bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7316 {
7317 bp_location *loc = this;
7318
7319 gdb_assert (ops != NULL);
7320
7321 loc->ops = ops;
7322 loc->owner = owner;
7323 loc->cond_bytecode = NULL;
7324 loc->shlib_disabled = 0;
7325 loc->enabled = 1;
7326
7327 switch (owner->type)
7328 {
7329 case bp_breakpoint:
7330 case bp_single_step:
7331 case bp_until:
7332 case bp_finish:
7333 case bp_longjmp:
7334 case bp_longjmp_resume:
7335 case bp_longjmp_call_dummy:
7336 case bp_exception:
7337 case bp_exception_resume:
7338 case bp_step_resume:
7339 case bp_hp_step_resume:
7340 case bp_watchpoint_scope:
7341 case bp_call_dummy:
7342 case bp_std_terminate:
7343 case bp_shlib_event:
7344 case bp_thread_event:
7345 case bp_overlay_event:
7346 case bp_jit_event:
7347 case bp_longjmp_master:
7348 case bp_std_terminate_master:
7349 case bp_exception_master:
7350 case bp_gnu_ifunc_resolver:
7351 case bp_gnu_ifunc_resolver_return:
7352 case bp_dprintf:
7353 loc->loc_type = bp_loc_software_breakpoint;
7354 mark_breakpoint_location_modified (loc);
7355 break;
7356 case bp_hardware_breakpoint:
7357 loc->loc_type = bp_loc_hardware_breakpoint;
7358 mark_breakpoint_location_modified (loc);
7359 break;
7360 case bp_hardware_watchpoint:
7361 case bp_read_watchpoint:
7362 case bp_access_watchpoint:
7363 loc->loc_type = bp_loc_hardware_watchpoint;
7364 break;
7365 case bp_watchpoint:
7366 case bp_catchpoint:
7367 case bp_tracepoint:
7368 case bp_fast_tracepoint:
7369 case bp_static_tracepoint:
7370 loc->loc_type = bp_loc_other;
7371 break;
7372 default:
7373 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7374 }
7375
7376 loc->refc = 1;
7377 }
7378
7379 /* Allocate a struct bp_location. */
7380
7381 static struct bp_location *
7382 allocate_bp_location (struct breakpoint *bpt)
7383 {
7384 return bpt->ops->allocate_location (bpt);
7385 }
7386
7387 static void
7388 free_bp_location (struct bp_location *loc)
7389 {
7390 loc->ops->dtor (loc);
7391 delete loc;
7392 }
7393
7394 /* Increment reference count. */
7395
7396 static void
7397 incref_bp_location (struct bp_location *bl)
7398 {
7399 ++bl->refc;
7400 }
7401
7402 /* Decrement reference count. If the reference count reaches 0,
7403 destroy the bp_location. Sets *BLP to NULL. */
7404
7405 static void
7406 decref_bp_location (struct bp_location **blp)
7407 {
7408 gdb_assert ((*blp)->refc > 0);
7409
7410 if (--(*blp)->refc == 0)
7411 free_bp_location (*blp);
7412 *blp = NULL;
7413 }
7414
7415 /* Add breakpoint B at the end of the global breakpoint chain. */
7416
7417 static void
7418 add_to_breakpoint_chain (struct breakpoint *b)
7419 {
7420 struct breakpoint *b1;
7421
7422 /* Add this breakpoint to the end of the chain so that a list of
7423 breakpoints will come out in order of increasing numbers. */
7424
7425 b1 = breakpoint_chain;
7426 if (b1 == 0)
7427 breakpoint_chain = b;
7428 else
7429 {
7430 while (b1->next)
7431 b1 = b1->next;
7432 b1->next = b;
7433 }
7434 }
7435
7436 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7437
7438 static void
7439 init_raw_breakpoint_without_location (struct breakpoint *b,
7440 struct gdbarch *gdbarch,
7441 enum bptype bptype,
7442 const struct breakpoint_ops *ops)
7443 {
7444 gdb_assert (ops != NULL);
7445
7446 b->ops = ops;
7447 b->type = bptype;
7448 b->gdbarch = gdbarch;
7449 b->language = current_language->la_language;
7450 b->input_radix = input_radix;
7451 b->related_breakpoint = b;
7452 }
7453
7454 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7455 that has type BPTYPE and has no locations as yet. */
7456
7457 static struct breakpoint *
7458 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7459 enum bptype bptype,
7460 const struct breakpoint_ops *ops)
7461 {
7462 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7463
7464 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7465 add_to_breakpoint_chain (b.get ());
7466
7467 return b.release ();
7468 }
7469
7470 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7471 resolutions should be made as the user specified the location explicitly
7472 enough. */
7473
7474 static void
7475 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7476 {
7477 gdb_assert (loc->owner != NULL);
7478
7479 if (loc->owner->type == bp_breakpoint
7480 || loc->owner->type == bp_hardware_breakpoint
7481 || is_tracepoint (loc->owner))
7482 {
7483 int is_gnu_ifunc;
7484 const char *function_name;
7485 CORE_ADDR func_addr;
7486
7487 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7488 &func_addr, NULL, &is_gnu_ifunc);
7489
7490 if (is_gnu_ifunc && !explicit_loc)
7491 {
7492 struct breakpoint *b = loc->owner;
7493
7494 gdb_assert (loc->pspace == current_program_space);
7495 if (gnu_ifunc_resolve_name (function_name,
7496 &loc->requested_address))
7497 {
7498 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7499 loc->address = adjust_breakpoint_address (loc->gdbarch,
7500 loc->requested_address,
7501 b->type);
7502 }
7503 else if (b->type == bp_breakpoint && b->loc == loc
7504 && loc->next == NULL && b->related_breakpoint == b)
7505 {
7506 /* Create only the whole new breakpoint of this type but do not
7507 mess more complicated breakpoints with multiple locations. */
7508 b->type = bp_gnu_ifunc_resolver;
7509 /* Remember the resolver's address for use by the return
7510 breakpoint. */
7511 loc->related_address = func_addr;
7512 }
7513 }
7514
7515 if (function_name)
7516 loc->function_name = xstrdup (function_name);
7517 }
7518 }
7519
7520 /* Attempt to determine architecture of location identified by SAL. */
7521 struct gdbarch *
7522 get_sal_arch (struct symtab_and_line sal)
7523 {
7524 if (sal.section)
7525 return get_objfile_arch (sal.section->objfile);
7526 if (sal.symtab)
7527 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7528
7529 return NULL;
7530 }
7531
7532 /* Low level routine for partially initializing a breakpoint of type
7533 BPTYPE. The newly created breakpoint's address, section, source
7534 file name, and line number are provided by SAL.
7535
7536 It is expected that the caller will complete the initialization of
7537 the newly created breakpoint struct as well as output any status
7538 information regarding the creation of a new breakpoint. */
7539
7540 static void
7541 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7542 struct symtab_and_line sal, enum bptype bptype,
7543 const struct breakpoint_ops *ops)
7544 {
7545 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7546
7547 add_location_to_breakpoint (b, &sal);
7548
7549 if (bptype != bp_catchpoint)
7550 gdb_assert (sal.pspace != NULL);
7551
7552 /* Store the program space that was used to set the breakpoint,
7553 except for ordinary breakpoints, which are independent of the
7554 program space. */
7555 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7556 b->pspace = sal.pspace;
7557 }
7558
7559 /* set_raw_breakpoint is a low level routine for allocating and
7560 partially initializing a breakpoint of type BPTYPE. The newly
7561 created breakpoint's address, section, source file name, and line
7562 number are provided by SAL. The newly created and partially
7563 initialized breakpoint is added to the breakpoint chain and
7564 is also returned as the value of this function.
7565
7566 It is expected that the caller will complete the initialization of
7567 the newly created breakpoint struct as well as output any status
7568 information regarding the creation of a new breakpoint. In
7569 particular, set_raw_breakpoint does NOT set the breakpoint
7570 number! Care should be taken to not allow an error to occur
7571 prior to completing the initialization of the breakpoint. If this
7572 should happen, a bogus breakpoint will be left on the chain. */
7573
7574 struct breakpoint *
7575 set_raw_breakpoint (struct gdbarch *gdbarch,
7576 struct symtab_and_line sal, enum bptype bptype,
7577 const struct breakpoint_ops *ops)
7578 {
7579 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7580
7581 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7582 add_to_breakpoint_chain (b.get ());
7583
7584 return b.release ();
7585 }
7586
7587 /* Call this routine when stepping and nexting to enable a breakpoint
7588 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7589 initiated the operation. */
7590
7591 void
7592 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7593 {
7594 struct breakpoint *b, *b_tmp;
7595 int thread = tp->global_num;
7596
7597 /* To avoid having to rescan all objfile symbols at every step,
7598 we maintain a list of continually-inserted but always disabled
7599 longjmp "master" breakpoints. Here, we simply create momentary
7600 clones of those and enable them for the requested thread. */
7601 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7602 if (b->pspace == current_program_space
7603 && (b->type == bp_longjmp_master
7604 || b->type == bp_exception_master))
7605 {
7606 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7607 struct breakpoint *clone;
7608
7609 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7610 after their removal. */
7611 clone = momentary_breakpoint_from_master (b, type,
7612 &longjmp_breakpoint_ops, 1);
7613 clone->thread = thread;
7614 }
7615
7616 tp->initiating_frame = frame;
7617 }
7618
7619 /* Delete all longjmp breakpoints from THREAD. */
7620 void
7621 delete_longjmp_breakpoint (int thread)
7622 {
7623 struct breakpoint *b, *b_tmp;
7624
7625 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7626 if (b->type == bp_longjmp || b->type == bp_exception)
7627 {
7628 if (b->thread == thread)
7629 delete_breakpoint (b);
7630 }
7631 }
7632
7633 void
7634 delete_longjmp_breakpoint_at_next_stop (int thread)
7635 {
7636 struct breakpoint *b, *b_tmp;
7637
7638 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7639 if (b->type == bp_longjmp || b->type == bp_exception)
7640 {
7641 if (b->thread == thread)
7642 b->disposition = disp_del_at_next_stop;
7643 }
7644 }
7645
7646 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7647 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7648 pointer to any of them. Return NULL if this system cannot place longjmp
7649 breakpoints. */
7650
7651 struct breakpoint *
7652 set_longjmp_breakpoint_for_call_dummy (void)
7653 {
7654 struct breakpoint *b, *retval = NULL;
7655
7656 ALL_BREAKPOINTS (b)
7657 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7658 {
7659 struct breakpoint *new_b;
7660
7661 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7662 &momentary_breakpoint_ops,
7663 1);
7664 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7665
7666 /* Link NEW_B into the chain of RETVAL breakpoints. */
7667
7668 gdb_assert (new_b->related_breakpoint == new_b);
7669 if (retval == NULL)
7670 retval = new_b;
7671 new_b->related_breakpoint = retval;
7672 while (retval->related_breakpoint != new_b->related_breakpoint)
7673 retval = retval->related_breakpoint;
7674 retval->related_breakpoint = new_b;
7675 }
7676
7677 return retval;
7678 }
7679
7680 /* Verify all existing dummy frames and their associated breakpoints for
7681 TP. Remove those which can no longer be found in the current frame
7682 stack.
7683
7684 You should call this function only at places where it is safe to currently
7685 unwind the whole stack. Failed stack unwind would discard live dummy
7686 frames. */
7687
7688 void
7689 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7690 {
7691 struct breakpoint *b, *b_tmp;
7692
7693 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7694 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7695 {
7696 struct breakpoint *dummy_b = b->related_breakpoint;
7697
7698 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7699 dummy_b = dummy_b->related_breakpoint;
7700 if (dummy_b->type != bp_call_dummy
7701 || frame_find_by_id (dummy_b->frame_id) != NULL)
7702 continue;
7703
7704 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7705
7706 while (b->related_breakpoint != b)
7707 {
7708 if (b_tmp == b->related_breakpoint)
7709 b_tmp = b->related_breakpoint->next;
7710 delete_breakpoint (b->related_breakpoint);
7711 }
7712 delete_breakpoint (b);
7713 }
7714 }
7715
7716 void
7717 enable_overlay_breakpoints (void)
7718 {
7719 struct breakpoint *b;
7720
7721 ALL_BREAKPOINTS (b)
7722 if (b->type == bp_overlay_event)
7723 {
7724 b->enable_state = bp_enabled;
7725 update_global_location_list (UGLL_MAY_INSERT);
7726 overlay_events_enabled = 1;
7727 }
7728 }
7729
7730 void
7731 disable_overlay_breakpoints (void)
7732 {
7733 struct breakpoint *b;
7734
7735 ALL_BREAKPOINTS (b)
7736 if (b->type == bp_overlay_event)
7737 {
7738 b->enable_state = bp_disabled;
7739 update_global_location_list (UGLL_DONT_INSERT);
7740 overlay_events_enabled = 0;
7741 }
7742 }
7743
7744 /* Set an active std::terminate breakpoint for each std::terminate
7745 master breakpoint. */
7746 void
7747 set_std_terminate_breakpoint (void)
7748 {
7749 struct breakpoint *b, *b_tmp;
7750
7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7752 if (b->pspace == current_program_space
7753 && b->type == bp_std_terminate_master)
7754 {
7755 momentary_breakpoint_from_master (b, bp_std_terminate,
7756 &momentary_breakpoint_ops, 1);
7757 }
7758 }
7759
7760 /* Delete all the std::terminate breakpoints. */
7761 void
7762 delete_std_terminate_breakpoint (void)
7763 {
7764 struct breakpoint *b, *b_tmp;
7765
7766 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7767 if (b->type == bp_std_terminate)
7768 delete_breakpoint (b);
7769 }
7770
7771 struct breakpoint *
7772 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7773 {
7774 struct breakpoint *b;
7775
7776 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7777 &internal_breakpoint_ops);
7778
7779 b->enable_state = bp_enabled;
7780 /* location has to be used or breakpoint_re_set will delete me. */
7781 b->location = new_address_location (b->loc->address, NULL, 0);
7782
7783 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7784
7785 return b;
7786 }
7787
7788 struct lang_and_radix
7789 {
7790 enum language lang;
7791 int radix;
7792 };
7793
7794 /* Create a breakpoint for JIT code registration and unregistration. */
7795
7796 struct breakpoint *
7797 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7798 {
7799 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7800 &internal_breakpoint_ops);
7801 }
7802
7803 /* Remove JIT code registration and unregistration breakpoint(s). */
7804
7805 void
7806 remove_jit_event_breakpoints (void)
7807 {
7808 struct breakpoint *b, *b_tmp;
7809
7810 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7811 if (b->type == bp_jit_event
7812 && b->loc->pspace == current_program_space)
7813 delete_breakpoint (b);
7814 }
7815
7816 void
7817 remove_solib_event_breakpoints (void)
7818 {
7819 struct breakpoint *b, *b_tmp;
7820
7821 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7822 if (b->type == bp_shlib_event
7823 && b->loc->pspace == current_program_space)
7824 delete_breakpoint (b);
7825 }
7826
7827 /* See breakpoint.h. */
7828
7829 void
7830 remove_solib_event_breakpoints_at_next_stop (void)
7831 {
7832 struct breakpoint *b, *b_tmp;
7833
7834 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7835 if (b->type == bp_shlib_event
7836 && b->loc->pspace == current_program_space)
7837 b->disposition = disp_del_at_next_stop;
7838 }
7839
7840 /* Helper for create_solib_event_breakpoint /
7841 create_and_insert_solib_event_breakpoint. Allows specifying which
7842 INSERT_MODE to pass through to update_global_location_list. */
7843
7844 static struct breakpoint *
7845 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7846 enum ugll_insert_mode insert_mode)
7847 {
7848 struct breakpoint *b;
7849
7850 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7851 &internal_breakpoint_ops);
7852 update_global_location_list_nothrow (insert_mode);
7853 return b;
7854 }
7855
7856 struct breakpoint *
7857 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7858 {
7859 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7860 }
7861
7862 /* See breakpoint.h. */
7863
7864 struct breakpoint *
7865 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7866 {
7867 struct breakpoint *b;
7868
7869 /* Explicitly tell update_global_location_list to insert
7870 locations. */
7871 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7872 if (!b->loc->inserted)
7873 {
7874 delete_breakpoint (b);
7875 return NULL;
7876 }
7877 return b;
7878 }
7879
7880 /* Disable any breakpoints that are on code in shared libraries. Only
7881 apply to enabled breakpoints, disabled ones can just stay disabled. */
7882
7883 void
7884 disable_breakpoints_in_shlibs (void)
7885 {
7886 struct bp_location *loc, **locp_tmp;
7887
7888 ALL_BP_LOCATIONS (loc, locp_tmp)
7889 {
7890 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7891 struct breakpoint *b = loc->owner;
7892
7893 /* We apply the check to all breakpoints, including disabled for
7894 those with loc->duplicate set. This is so that when breakpoint
7895 becomes enabled, or the duplicate is removed, gdb will try to
7896 insert all breakpoints. If we don't set shlib_disabled here,
7897 we'll try to insert those breakpoints and fail. */
7898 if (((b->type == bp_breakpoint)
7899 || (b->type == bp_jit_event)
7900 || (b->type == bp_hardware_breakpoint)
7901 || (is_tracepoint (b)))
7902 && loc->pspace == current_program_space
7903 && !loc->shlib_disabled
7904 && solib_name_from_address (loc->pspace, loc->address)
7905 )
7906 {
7907 loc->shlib_disabled = 1;
7908 }
7909 }
7910 }
7911
7912 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7913 notification of unloaded_shlib. Only apply to enabled breakpoints,
7914 disabled ones can just stay disabled. */
7915
7916 static void
7917 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7918 {
7919 struct bp_location *loc, **locp_tmp;
7920 int disabled_shlib_breaks = 0;
7921
7922 ALL_BP_LOCATIONS (loc, locp_tmp)
7923 {
7924 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7925 struct breakpoint *b = loc->owner;
7926
7927 if (solib->pspace == loc->pspace
7928 && !loc->shlib_disabled
7929 && (((b->type == bp_breakpoint
7930 || b->type == bp_jit_event
7931 || b->type == bp_hardware_breakpoint)
7932 && (loc->loc_type == bp_loc_hardware_breakpoint
7933 || loc->loc_type == bp_loc_software_breakpoint))
7934 || is_tracepoint (b))
7935 && solib_contains_address_p (solib, loc->address))
7936 {
7937 loc->shlib_disabled = 1;
7938 /* At this point, we cannot rely on remove_breakpoint
7939 succeeding so we must mark the breakpoint as not inserted
7940 to prevent future errors occurring in remove_breakpoints. */
7941 loc->inserted = 0;
7942
7943 /* This may cause duplicate notifications for the same breakpoint. */
7944 observer_notify_breakpoint_modified (b);
7945
7946 if (!disabled_shlib_breaks)
7947 {
7948 target_terminal_ours_for_output ();
7949 warning (_("Temporarily disabling breakpoints "
7950 "for unloaded shared library \"%s\""),
7951 solib->so_name);
7952 }
7953 disabled_shlib_breaks = 1;
7954 }
7955 }
7956 }
7957
7958 /* Disable any breakpoints and tracepoints in OBJFILE upon
7959 notification of free_objfile. Only apply to enabled breakpoints,
7960 disabled ones can just stay disabled. */
7961
7962 static void
7963 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7964 {
7965 struct breakpoint *b;
7966
7967 if (objfile == NULL)
7968 return;
7969
7970 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7971 managed by the user with add-symbol-file/remove-symbol-file.
7972 Similarly to how breakpoints in shared libraries are handled in
7973 response to "nosharedlibrary", mark breakpoints in such modules
7974 shlib_disabled so they end up uninserted on the next global
7975 location list update. Shared libraries not loaded by the user
7976 aren't handled here -- they're already handled in
7977 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7978 solib_unloaded observer. We skip objfiles that are not
7979 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7980 main objfile). */
7981 if ((objfile->flags & OBJF_SHARED) == 0
7982 || (objfile->flags & OBJF_USERLOADED) == 0)
7983 return;
7984
7985 ALL_BREAKPOINTS (b)
7986 {
7987 struct bp_location *loc;
7988 int bp_modified = 0;
7989
7990 if (!is_breakpoint (b) && !is_tracepoint (b))
7991 continue;
7992
7993 for (loc = b->loc; loc != NULL; loc = loc->next)
7994 {
7995 CORE_ADDR loc_addr = loc->address;
7996
7997 if (loc->loc_type != bp_loc_hardware_breakpoint
7998 && loc->loc_type != bp_loc_software_breakpoint)
7999 continue;
8000
8001 if (loc->shlib_disabled != 0)
8002 continue;
8003
8004 if (objfile->pspace != loc->pspace)
8005 continue;
8006
8007 if (loc->loc_type != bp_loc_hardware_breakpoint
8008 && loc->loc_type != bp_loc_software_breakpoint)
8009 continue;
8010
8011 if (is_addr_in_objfile (loc_addr, objfile))
8012 {
8013 loc->shlib_disabled = 1;
8014 /* At this point, we don't know whether the object was
8015 unmapped from the inferior or not, so leave the
8016 inserted flag alone. We'll handle failure to
8017 uninsert quietly, in case the object was indeed
8018 unmapped. */
8019
8020 mark_breakpoint_location_modified (loc);
8021
8022 bp_modified = 1;
8023 }
8024 }
8025
8026 if (bp_modified)
8027 observer_notify_breakpoint_modified (b);
8028 }
8029 }
8030
8031 /* FORK & VFORK catchpoints. */
8032
8033 /* An instance of this type is used to represent a fork or vfork
8034 catchpoint. It includes a "struct breakpoint" as a kind of base
8035 class; users downcast to "struct breakpoint *" when needed. A
8036 breakpoint is really of this type iff its ops pointer points to
8037 CATCH_FORK_BREAKPOINT_OPS. */
8038
8039 struct fork_catchpoint
8040 {
8041 /* The base class. */
8042 struct breakpoint base;
8043
8044 /* Process id of a child process whose forking triggered this
8045 catchpoint. This field is only valid immediately after this
8046 catchpoint has triggered. */
8047 ptid_t forked_inferior_pid;
8048 };
8049
8050 /* Implement the "insert" breakpoint_ops method for fork
8051 catchpoints. */
8052
8053 static int
8054 insert_catch_fork (struct bp_location *bl)
8055 {
8056 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8057 }
8058
8059 /* Implement the "remove" breakpoint_ops method for fork
8060 catchpoints. */
8061
8062 static int
8063 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8064 {
8065 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8066 }
8067
8068 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8069 catchpoints. */
8070
8071 static int
8072 breakpoint_hit_catch_fork (const struct bp_location *bl,
8073 struct address_space *aspace, CORE_ADDR bp_addr,
8074 const struct target_waitstatus *ws)
8075 {
8076 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8077
8078 if (ws->kind != TARGET_WAITKIND_FORKED)
8079 return 0;
8080
8081 c->forked_inferior_pid = ws->value.related_pid;
8082 return 1;
8083 }
8084
8085 /* Implement the "print_it" breakpoint_ops method for fork
8086 catchpoints. */
8087
8088 static enum print_stop_action
8089 print_it_catch_fork (bpstat bs)
8090 {
8091 struct ui_out *uiout = current_uiout;
8092 struct breakpoint *b = bs->breakpoint_at;
8093 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8094
8095 annotate_catchpoint (b->number);
8096 maybe_print_thread_hit_breakpoint (uiout);
8097 if (b->disposition == disp_del)
8098 uiout->text ("Temporary catchpoint ");
8099 else
8100 uiout->text ("Catchpoint ");
8101 if (uiout->is_mi_like_p ())
8102 {
8103 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8104 uiout->field_string ("disp", bpdisp_text (b->disposition));
8105 }
8106 uiout->field_int ("bkptno", b->number);
8107 uiout->text (" (forked process ");
8108 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8109 uiout->text ("), ");
8110 return PRINT_SRC_AND_LOC;
8111 }
8112
8113 /* Implement the "print_one" breakpoint_ops method for fork
8114 catchpoints. */
8115
8116 static void
8117 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8118 {
8119 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8120 struct value_print_options opts;
8121 struct ui_out *uiout = current_uiout;
8122
8123 get_user_print_options (&opts);
8124
8125 /* Field 4, the address, is omitted (which makes the columns not
8126 line up too nicely with the headers, but the effect is relatively
8127 readable). */
8128 if (opts.addressprint)
8129 uiout->field_skip ("addr");
8130 annotate_field (5);
8131 uiout->text ("fork");
8132 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8133 {
8134 uiout->text (", process ");
8135 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8136 uiout->spaces (1);
8137 }
8138
8139 if (uiout->is_mi_like_p ())
8140 uiout->field_string ("catch-type", "fork");
8141 }
8142
8143 /* Implement the "print_mention" breakpoint_ops method for fork
8144 catchpoints. */
8145
8146 static void
8147 print_mention_catch_fork (struct breakpoint *b)
8148 {
8149 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8150 }
8151
8152 /* Implement the "print_recreate" breakpoint_ops method for fork
8153 catchpoints. */
8154
8155 static void
8156 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8157 {
8158 fprintf_unfiltered (fp, "catch fork");
8159 print_recreate_thread (b, fp);
8160 }
8161
8162 /* The breakpoint_ops structure to be used in fork catchpoints. */
8163
8164 static struct breakpoint_ops catch_fork_breakpoint_ops;
8165
8166 /* Implement the "insert" breakpoint_ops method for vfork
8167 catchpoints. */
8168
8169 static int
8170 insert_catch_vfork (struct bp_location *bl)
8171 {
8172 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8173 }
8174
8175 /* Implement the "remove" breakpoint_ops method for vfork
8176 catchpoints. */
8177
8178 static int
8179 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8180 {
8181 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8182 }
8183
8184 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8185 catchpoints. */
8186
8187 static int
8188 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8189 struct address_space *aspace, CORE_ADDR bp_addr,
8190 const struct target_waitstatus *ws)
8191 {
8192 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8193
8194 if (ws->kind != TARGET_WAITKIND_VFORKED)
8195 return 0;
8196
8197 c->forked_inferior_pid = ws->value.related_pid;
8198 return 1;
8199 }
8200
8201 /* Implement the "print_it" breakpoint_ops method for vfork
8202 catchpoints. */
8203
8204 static enum print_stop_action
8205 print_it_catch_vfork (bpstat bs)
8206 {
8207 struct ui_out *uiout = current_uiout;
8208 struct breakpoint *b = bs->breakpoint_at;
8209 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8210
8211 annotate_catchpoint (b->number);
8212 maybe_print_thread_hit_breakpoint (uiout);
8213 if (b->disposition == disp_del)
8214 uiout->text ("Temporary catchpoint ");
8215 else
8216 uiout->text ("Catchpoint ");
8217 if (uiout->is_mi_like_p ())
8218 {
8219 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8220 uiout->field_string ("disp", bpdisp_text (b->disposition));
8221 }
8222 uiout->field_int ("bkptno", b->number);
8223 uiout->text (" (vforked process ");
8224 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8225 uiout->text ("), ");
8226 return PRINT_SRC_AND_LOC;
8227 }
8228
8229 /* Implement the "print_one" breakpoint_ops method for vfork
8230 catchpoints. */
8231
8232 static void
8233 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8234 {
8235 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8236 struct value_print_options opts;
8237 struct ui_out *uiout = current_uiout;
8238
8239 get_user_print_options (&opts);
8240 /* Field 4, the address, is omitted (which makes the columns not
8241 line up too nicely with the headers, but the effect is relatively
8242 readable). */
8243 if (opts.addressprint)
8244 uiout->field_skip ("addr");
8245 annotate_field (5);
8246 uiout->text ("vfork");
8247 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8248 {
8249 uiout->text (", process ");
8250 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8251 uiout->spaces (1);
8252 }
8253
8254 if (uiout->is_mi_like_p ())
8255 uiout->field_string ("catch-type", "vfork");
8256 }
8257
8258 /* Implement the "print_mention" breakpoint_ops method for vfork
8259 catchpoints. */
8260
8261 static void
8262 print_mention_catch_vfork (struct breakpoint *b)
8263 {
8264 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8265 }
8266
8267 /* Implement the "print_recreate" breakpoint_ops method for vfork
8268 catchpoints. */
8269
8270 static void
8271 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8272 {
8273 fprintf_unfiltered (fp, "catch vfork");
8274 print_recreate_thread (b, fp);
8275 }
8276
8277 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8278
8279 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8280
8281 /* An instance of this type is used to represent an solib catchpoint.
8282 It includes a "struct breakpoint" as a kind of base class; users
8283 downcast to "struct breakpoint *" when needed. A breakpoint is
8284 really of this type iff its ops pointer points to
8285 CATCH_SOLIB_BREAKPOINT_OPS. */
8286
8287 struct solib_catchpoint
8288 {
8289 /* The base class. */
8290 struct breakpoint base;
8291
8292 /* True for "catch load", false for "catch unload". */
8293 unsigned char is_load;
8294
8295 /* Regular expression to match, if any. COMPILED is only valid when
8296 REGEX is non-NULL. */
8297 char *regex;
8298 regex_t compiled;
8299 };
8300
8301 static void
8302 dtor_catch_solib (struct breakpoint *b)
8303 {
8304 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8305
8306 if (self->regex)
8307 regfree (&self->compiled);
8308 xfree (self->regex);
8309
8310 base_breakpoint_ops.dtor (b);
8311 }
8312
8313 static int
8314 insert_catch_solib (struct bp_location *ignore)
8315 {
8316 return 0;
8317 }
8318
8319 static int
8320 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8321 {
8322 return 0;
8323 }
8324
8325 static int
8326 breakpoint_hit_catch_solib (const struct bp_location *bl,
8327 struct address_space *aspace,
8328 CORE_ADDR bp_addr,
8329 const struct target_waitstatus *ws)
8330 {
8331 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8332 struct breakpoint *other;
8333
8334 if (ws->kind == TARGET_WAITKIND_LOADED)
8335 return 1;
8336
8337 ALL_BREAKPOINTS (other)
8338 {
8339 struct bp_location *other_bl;
8340
8341 if (other == bl->owner)
8342 continue;
8343
8344 if (other->type != bp_shlib_event)
8345 continue;
8346
8347 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8348 continue;
8349
8350 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8351 {
8352 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8353 return 1;
8354 }
8355 }
8356
8357 return 0;
8358 }
8359
8360 static void
8361 check_status_catch_solib (struct bpstats *bs)
8362 {
8363 struct solib_catchpoint *self
8364 = (struct solib_catchpoint *) bs->breakpoint_at;
8365 int ix;
8366
8367 if (self->is_load)
8368 {
8369 struct so_list *iter;
8370
8371 for (ix = 0;
8372 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8373 ix, iter);
8374 ++ix)
8375 {
8376 if (!self->regex
8377 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8378 return;
8379 }
8380 }
8381 else
8382 {
8383 char *iter;
8384
8385 for (ix = 0;
8386 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8387 ix, iter);
8388 ++ix)
8389 {
8390 if (!self->regex
8391 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8392 return;
8393 }
8394 }
8395
8396 bs->stop = 0;
8397 bs->print_it = print_it_noop;
8398 }
8399
8400 static enum print_stop_action
8401 print_it_catch_solib (bpstat bs)
8402 {
8403 struct breakpoint *b = bs->breakpoint_at;
8404 struct ui_out *uiout = current_uiout;
8405
8406 annotate_catchpoint (b->number);
8407 maybe_print_thread_hit_breakpoint (uiout);
8408 if (b->disposition == disp_del)
8409 uiout->text ("Temporary catchpoint ");
8410 else
8411 uiout->text ("Catchpoint ");
8412 uiout->field_int ("bkptno", b->number);
8413 uiout->text ("\n");
8414 if (uiout->is_mi_like_p ())
8415 uiout->field_string ("disp", bpdisp_text (b->disposition));
8416 print_solib_event (1);
8417 return PRINT_SRC_AND_LOC;
8418 }
8419
8420 static void
8421 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8422 {
8423 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8424 struct value_print_options opts;
8425 struct ui_out *uiout = current_uiout;
8426 char *msg;
8427
8428 get_user_print_options (&opts);
8429 /* Field 4, the address, is omitted (which makes the columns not
8430 line up too nicely with the headers, but the effect is relatively
8431 readable). */
8432 if (opts.addressprint)
8433 {
8434 annotate_field (4);
8435 uiout->field_skip ("addr");
8436 }
8437
8438 annotate_field (5);
8439 if (self->is_load)
8440 {
8441 if (self->regex)
8442 msg = xstrprintf (_("load of library matching %s"), self->regex);
8443 else
8444 msg = xstrdup (_("load of library"));
8445 }
8446 else
8447 {
8448 if (self->regex)
8449 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8450 else
8451 msg = xstrdup (_("unload of library"));
8452 }
8453 uiout->field_string ("what", msg);
8454 xfree (msg);
8455
8456 if (uiout->is_mi_like_p ())
8457 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8458 }
8459
8460 static void
8461 print_mention_catch_solib (struct breakpoint *b)
8462 {
8463 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8464
8465 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8466 self->is_load ? "load" : "unload");
8467 }
8468
8469 static void
8470 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8471 {
8472 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8473
8474 fprintf_unfiltered (fp, "%s %s",
8475 b->disposition == disp_del ? "tcatch" : "catch",
8476 self->is_load ? "load" : "unload");
8477 if (self->regex)
8478 fprintf_unfiltered (fp, " %s", self->regex);
8479 fprintf_unfiltered (fp, "\n");
8480 }
8481
8482 static struct breakpoint_ops catch_solib_breakpoint_ops;
8483
8484 /* Shared helper function (MI and CLI) for creating and installing
8485 a shared object event catchpoint. If IS_LOAD is non-zero then
8486 the events to be caught are load events, otherwise they are
8487 unload events. If IS_TEMP is non-zero the catchpoint is a
8488 temporary one. If ENABLED is non-zero the catchpoint is
8489 created in an enabled state. */
8490
8491 void
8492 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8493 {
8494 struct solib_catchpoint *c;
8495 struct gdbarch *gdbarch = get_current_arch ();
8496 struct cleanup *cleanup;
8497
8498 if (!arg)
8499 arg = "";
8500 arg = skip_spaces_const (arg);
8501
8502 c = new solib_catchpoint ();
8503 cleanup = make_cleanup (xfree, c);
8504
8505 if (*arg != '\0')
8506 {
8507 int errcode;
8508
8509 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8510 if (errcode != 0)
8511 {
8512 char *err = get_regcomp_error (errcode, &c->compiled);
8513
8514 make_cleanup (xfree, err);
8515 error (_("Invalid regexp (%s): %s"), err, arg);
8516 }
8517 c->regex = xstrdup (arg);
8518 }
8519
8520 c->is_load = is_load;
8521 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8522 &catch_solib_breakpoint_ops);
8523
8524 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8525
8526 discard_cleanups (cleanup);
8527 install_breakpoint (0, &c->base, 1);
8528 }
8529
8530 /* A helper function that does all the work for "catch load" and
8531 "catch unload". */
8532
8533 static void
8534 catch_load_or_unload (char *arg, int from_tty, int is_load,
8535 struct cmd_list_element *command)
8536 {
8537 int tempflag;
8538 const int enabled = 1;
8539
8540 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8541
8542 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8543 }
8544
8545 static void
8546 catch_load_command_1 (char *arg, int from_tty,
8547 struct cmd_list_element *command)
8548 {
8549 catch_load_or_unload (arg, from_tty, 1, command);
8550 }
8551
8552 static void
8553 catch_unload_command_1 (char *arg, int from_tty,
8554 struct cmd_list_element *command)
8555 {
8556 catch_load_or_unload (arg, from_tty, 0, command);
8557 }
8558
8559 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8560 is non-zero, then make the breakpoint temporary. If COND_STRING is
8561 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8562 the breakpoint_ops structure associated to the catchpoint. */
8563
8564 void
8565 init_catchpoint (struct breakpoint *b,
8566 struct gdbarch *gdbarch, int tempflag,
8567 const char *cond_string,
8568 const struct breakpoint_ops *ops)
8569 {
8570 struct symtab_and_line sal;
8571
8572 init_sal (&sal);
8573 sal.pspace = current_program_space;
8574
8575 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8576
8577 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8578 b->disposition = tempflag ? disp_del : disp_donttouch;
8579 }
8580
8581 void
8582 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8583 {
8584 add_to_breakpoint_chain (b);
8585 set_breakpoint_number (internal, b);
8586 if (is_tracepoint (b))
8587 set_tracepoint_count (breakpoint_count);
8588 if (!internal)
8589 mention (b);
8590 observer_notify_breakpoint_created (b);
8591
8592 if (update_gll)
8593 update_global_location_list (UGLL_MAY_INSERT);
8594 }
8595
8596 static void
8597 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8598 int tempflag, const char *cond_string,
8599 const struct breakpoint_ops *ops)
8600 {
8601 struct fork_catchpoint *c = new fork_catchpoint ();
8602
8603 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8604
8605 c->forked_inferior_pid = null_ptid;
8606
8607 install_breakpoint (0, &c->base, 1);
8608 }
8609
8610 /* Exec catchpoints. */
8611
8612 /* An instance of this type is used to represent an exec catchpoint.
8613 It includes a "struct breakpoint" as a kind of base class; users
8614 downcast to "struct breakpoint *" when needed. A breakpoint is
8615 really of this type iff its ops pointer points to
8616 CATCH_EXEC_BREAKPOINT_OPS. */
8617
8618 struct exec_catchpoint
8619 {
8620 /* The base class. */
8621 struct breakpoint base;
8622
8623 /* Filename of a program whose exec triggered this catchpoint.
8624 This field is only valid immediately after this catchpoint has
8625 triggered. */
8626 char *exec_pathname;
8627 };
8628
8629 /* Implement the "dtor" breakpoint_ops method for exec
8630 catchpoints. */
8631
8632 static void
8633 dtor_catch_exec (struct breakpoint *b)
8634 {
8635 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8636
8637 xfree (c->exec_pathname);
8638
8639 base_breakpoint_ops.dtor (b);
8640 }
8641
8642 static int
8643 insert_catch_exec (struct bp_location *bl)
8644 {
8645 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8646 }
8647
8648 static int
8649 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8650 {
8651 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8652 }
8653
8654 static int
8655 breakpoint_hit_catch_exec (const struct bp_location *bl,
8656 struct address_space *aspace, CORE_ADDR bp_addr,
8657 const struct target_waitstatus *ws)
8658 {
8659 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8660
8661 if (ws->kind != TARGET_WAITKIND_EXECD)
8662 return 0;
8663
8664 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8665 return 1;
8666 }
8667
8668 static enum print_stop_action
8669 print_it_catch_exec (bpstat bs)
8670 {
8671 struct ui_out *uiout = current_uiout;
8672 struct breakpoint *b = bs->breakpoint_at;
8673 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8674
8675 annotate_catchpoint (b->number);
8676 maybe_print_thread_hit_breakpoint (uiout);
8677 if (b->disposition == disp_del)
8678 uiout->text ("Temporary catchpoint ");
8679 else
8680 uiout->text ("Catchpoint ");
8681 if (uiout->is_mi_like_p ())
8682 {
8683 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8684 uiout->field_string ("disp", bpdisp_text (b->disposition));
8685 }
8686 uiout->field_int ("bkptno", b->number);
8687 uiout->text (" (exec'd ");
8688 uiout->field_string ("new-exec", c->exec_pathname);
8689 uiout->text ("), ");
8690
8691 return PRINT_SRC_AND_LOC;
8692 }
8693
8694 static void
8695 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8696 {
8697 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8698 struct value_print_options opts;
8699 struct ui_out *uiout = current_uiout;
8700
8701 get_user_print_options (&opts);
8702
8703 /* Field 4, the address, is omitted (which makes the columns
8704 not line up too nicely with the headers, but the effect
8705 is relatively readable). */
8706 if (opts.addressprint)
8707 uiout->field_skip ("addr");
8708 annotate_field (5);
8709 uiout->text ("exec");
8710 if (c->exec_pathname != NULL)
8711 {
8712 uiout->text (", program \"");
8713 uiout->field_string ("what", c->exec_pathname);
8714 uiout->text ("\" ");
8715 }
8716
8717 if (uiout->is_mi_like_p ())
8718 uiout->field_string ("catch-type", "exec");
8719 }
8720
8721 static void
8722 print_mention_catch_exec (struct breakpoint *b)
8723 {
8724 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8725 }
8726
8727 /* Implement the "print_recreate" breakpoint_ops method for exec
8728 catchpoints. */
8729
8730 static void
8731 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8732 {
8733 fprintf_unfiltered (fp, "catch exec");
8734 print_recreate_thread (b, fp);
8735 }
8736
8737 static struct breakpoint_ops catch_exec_breakpoint_ops;
8738
8739 static int
8740 hw_breakpoint_used_count (void)
8741 {
8742 int i = 0;
8743 struct breakpoint *b;
8744 struct bp_location *bl;
8745
8746 ALL_BREAKPOINTS (b)
8747 {
8748 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8749 for (bl = b->loc; bl; bl = bl->next)
8750 {
8751 /* Special types of hardware breakpoints may use more than
8752 one register. */
8753 i += b->ops->resources_needed (bl);
8754 }
8755 }
8756
8757 return i;
8758 }
8759
8760 /* Returns the resources B would use if it were a hardware
8761 watchpoint. */
8762
8763 static int
8764 hw_watchpoint_use_count (struct breakpoint *b)
8765 {
8766 int i = 0;
8767 struct bp_location *bl;
8768
8769 if (!breakpoint_enabled (b))
8770 return 0;
8771
8772 for (bl = b->loc; bl; bl = bl->next)
8773 {
8774 /* Special types of hardware watchpoints may use more than
8775 one register. */
8776 i += b->ops->resources_needed (bl);
8777 }
8778
8779 return i;
8780 }
8781
8782 /* Returns the sum the used resources of all hardware watchpoints of
8783 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8784 the sum of the used resources of all hardware watchpoints of other
8785 types _not_ TYPE. */
8786
8787 static int
8788 hw_watchpoint_used_count_others (struct breakpoint *except,
8789 enum bptype type, int *other_type_used)
8790 {
8791 int i = 0;
8792 struct breakpoint *b;
8793
8794 *other_type_used = 0;
8795 ALL_BREAKPOINTS (b)
8796 {
8797 if (b == except)
8798 continue;
8799 if (!breakpoint_enabled (b))
8800 continue;
8801
8802 if (b->type == type)
8803 i += hw_watchpoint_use_count (b);
8804 else if (is_hardware_watchpoint (b))
8805 *other_type_used = 1;
8806 }
8807
8808 return i;
8809 }
8810
8811 void
8812 disable_watchpoints_before_interactive_call_start (void)
8813 {
8814 struct breakpoint *b;
8815
8816 ALL_BREAKPOINTS (b)
8817 {
8818 if (is_watchpoint (b) && breakpoint_enabled (b))
8819 {
8820 b->enable_state = bp_call_disabled;
8821 update_global_location_list (UGLL_DONT_INSERT);
8822 }
8823 }
8824 }
8825
8826 void
8827 enable_watchpoints_after_interactive_call_stop (void)
8828 {
8829 struct breakpoint *b;
8830
8831 ALL_BREAKPOINTS (b)
8832 {
8833 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8834 {
8835 b->enable_state = bp_enabled;
8836 update_global_location_list (UGLL_MAY_INSERT);
8837 }
8838 }
8839 }
8840
8841 void
8842 disable_breakpoints_before_startup (void)
8843 {
8844 current_program_space->executing_startup = 1;
8845 update_global_location_list (UGLL_DONT_INSERT);
8846 }
8847
8848 void
8849 enable_breakpoints_after_startup (void)
8850 {
8851 current_program_space->executing_startup = 0;
8852 breakpoint_re_set ();
8853 }
8854
8855 /* Create a new single-step breakpoint for thread THREAD, with no
8856 locations. */
8857
8858 static struct breakpoint *
8859 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8860 {
8861 struct breakpoint *b = new breakpoint ();
8862
8863 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8864 &momentary_breakpoint_ops);
8865
8866 b->disposition = disp_donttouch;
8867 b->frame_id = null_frame_id;
8868
8869 b->thread = thread;
8870 gdb_assert (b->thread != 0);
8871
8872 add_to_breakpoint_chain (b);
8873
8874 return b;
8875 }
8876
8877 /* Set a momentary breakpoint of type TYPE at address specified by
8878 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8879 frame. */
8880
8881 struct breakpoint *
8882 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8883 struct frame_id frame_id, enum bptype type)
8884 {
8885 struct breakpoint *b;
8886
8887 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8888 tail-called one. */
8889 gdb_assert (!frame_id_artificial_p (frame_id));
8890
8891 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8892 b->enable_state = bp_enabled;
8893 b->disposition = disp_donttouch;
8894 b->frame_id = frame_id;
8895
8896 /* If we're debugging a multi-threaded program, then we want
8897 momentary breakpoints to be active in only a single thread of
8898 control. */
8899 if (in_thread_list (inferior_ptid))
8900 b->thread = ptid_to_global_thread_id (inferior_ptid);
8901
8902 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8903
8904 return b;
8905 }
8906
8907 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8908 The new breakpoint will have type TYPE, use OPS as its
8909 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8910
8911 static struct breakpoint *
8912 momentary_breakpoint_from_master (struct breakpoint *orig,
8913 enum bptype type,
8914 const struct breakpoint_ops *ops,
8915 int loc_enabled)
8916 {
8917 struct breakpoint *copy;
8918
8919 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8920 copy->loc = allocate_bp_location (copy);
8921 set_breakpoint_location_function (copy->loc, 1);
8922
8923 copy->loc->gdbarch = orig->loc->gdbarch;
8924 copy->loc->requested_address = orig->loc->requested_address;
8925 copy->loc->address = orig->loc->address;
8926 copy->loc->section = orig->loc->section;
8927 copy->loc->pspace = orig->loc->pspace;
8928 copy->loc->probe = orig->loc->probe;
8929 copy->loc->line_number = orig->loc->line_number;
8930 copy->loc->symtab = orig->loc->symtab;
8931 copy->loc->enabled = loc_enabled;
8932 copy->frame_id = orig->frame_id;
8933 copy->thread = orig->thread;
8934 copy->pspace = orig->pspace;
8935
8936 copy->enable_state = bp_enabled;
8937 copy->disposition = disp_donttouch;
8938 copy->number = internal_breakpoint_number--;
8939
8940 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8941 return copy;
8942 }
8943
8944 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8945 ORIG is NULL. */
8946
8947 struct breakpoint *
8948 clone_momentary_breakpoint (struct breakpoint *orig)
8949 {
8950 /* If there's nothing to clone, then return nothing. */
8951 if (orig == NULL)
8952 return NULL;
8953
8954 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8955 }
8956
8957 struct breakpoint *
8958 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8959 enum bptype type)
8960 {
8961 struct symtab_and_line sal;
8962
8963 sal = find_pc_line (pc, 0);
8964 sal.pc = pc;
8965 sal.section = find_pc_overlay (pc);
8966 sal.explicit_pc = 1;
8967
8968 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8969 }
8970 \f
8971
8972 /* Tell the user we have just set a breakpoint B. */
8973
8974 static void
8975 mention (struct breakpoint *b)
8976 {
8977 b->ops->print_mention (b);
8978 if (current_uiout->is_mi_like_p ())
8979 return;
8980 printf_filtered ("\n");
8981 }
8982 \f
8983
8984 static int bp_loc_is_permanent (struct bp_location *loc);
8985
8986 static struct bp_location *
8987 add_location_to_breakpoint (struct breakpoint *b,
8988 const struct symtab_and_line *sal)
8989 {
8990 struct bp_location *loc, **tmp;
8991 CORE_ADDR adjusted_address;
8992 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8993
8994 if (loc_gdbarch == NULL)
8995 loc_gdbarch = b->gdbarch;
8996
8997 /* Adjust the breakpoint's address prior to allocating a location.
8998 Once we call allocate_bp_location(), that mostly uninitialized
8999 location will be placed on the location chain. Adjustment of the
9000 breakpoint may cause target_read_memory() to be called and we do
9001 not want its scan of the location chain to find a breakpoint and
9002 location that's only been partially initialized. */
9003 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9004 sal->pc, b->type);
9005
9006 /* Sort the locations by their ADDRESS. */
9007 loc = allocate_bp_location (b);
9008 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9009 tmp = &((*tmp)->next))
9010 ;
9011 loc->next = *tmp;
9012 *tmp = loc;
9013
9014 loc->requested_address = sal->pc;
9015 loc->address = adjusted_address;
9016 loc->pspace = sal->pspace;
9017 loc->probe.probe = sal->probe;
9018 loc->probe.objfile = sal->objfile;
9019 gdb_assert (loc->pspace != NULL);
9020 loc->section = sal->section;
9021 loc->gdbarch = loc_gdbarch;
9022 loc->line_number = sal->line;
9023 loc->symtab = sal->symtab;
9024
9025 set_breakpoint_location_function (loc,
9026 sal->explicit_pc || sal->explicit_line);
9027
9028 /* While by definition, permanent breakpoints are already present in the
9029 code, we don't mark the location as inserted. Normally one would expect
9030 that GDB could rely on that breakpoint instruction to stop the program,
9031 thus removing the need to insert its own breakpoint, except that executing
9032 the breakpoint instruction can kill the target instead of reporting a
9033 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9034 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9035 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9036 breakpoint be inserted normally results in QEMU knowing about the GDB
9037 breakpoint, and thus trap before the breakpoint instruction is executed.
9038 (If GDB later needs to continue execution past the permanent breakpoint,
9039 it manually increments the PC, thus avoiding executing the breakpoint
9040 instruction.) */
9041 if (bp_loc_is_permanent (loc))
9042 loc->permanent = 1;
9043
9044 return loc;
9045 }
9046 \f
9047
9048 /* See breakpoint.h. */
9049
9050 int
9051 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9052 {
9053 int len;
9054 CORE_ADDR addr;
9055 const gdb_byte *bpoint;
9056 gdb_byte *target_mem;
9057 struct cleanup *cleanup;
9058 int retval = 0;
9059
9060 addr = address;
9061 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9062
9063 /* Software breakpoints unsupported? */
9064 if (bpoint == NULL)
9065 return 0;
9066
9067 target_mem = (gdb_byte *) alloca (len);
9068
9069 /* Enable the automatic memory restoration from breakpoints while
9070 we read the memory. Otherwise we could say about our temporary
9071 breakpoints they are permanent. */
9072 cleanup = make_show_memory_breakpoints_cleanup (0);
9073
9074 if (target_read_memory (address, target_mem, len) == 0
9075 && memcmp (target_mem, bpoint, len) == 0)
9076 retval = 1;
9077
9078 do_cleanups (cleanup);
9079
9080 return retval;
9081 }
9082
9083 /* Return 1 if LOC is pointing to a permanent breakpoint,
9084 return 0 otherwise. */
9085
9086 static int
9087 bp_loc_is_permanent (struct bp_location *loc)
9088 {
9089 gdb_assert (loc != NULL);
9090
9091 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9092 attempt to read from the addresses the locations of these breakpoint types
9093 point to. program_breakpoint_here_p, below, will attempt to read
9094 memory. */
9095 if (!breakpoint_address_is_meaningful (loc->owner))
9096 return 0;
9097
9098 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9099 switch_to_program_space_and_thread (loc->pspace);
9100 return program_breakpoint_here_p (loc->gdbarch, loc->address);
9101 }
9102
9103 /* Build a command list for the dprintf corresponding to the current
9104 settings of the dprintf style options. */
9105
9106 static void
9107 update_dprintf_command_list (struct breakpoint *b)
9108 {
9109 char *dprintf_args = b->extra_string;
9110 char *printf_line = NULL;
9111
9112 if (!dprintf_args)
9113 return;
9114
9115 dprintf_args = skip_spaces (dprintf_args);
9116
9117 /* Allow a comma, as it may have terminated a location, but don't
9118 insist on it. */
9119 if (*dprintf_args == ',')
9120 ++dprintf_args;
9121 dprintf_args = skip_spaces (dprintf_args);
9122
9123 if (*dprintf_args != '"')
9124 error (_("Bad format string, missing '\"'."));
9125
9126 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9127 printf_line = xstrprintf ("printf %s", dprintf_args);
9128 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9129 {
9130 if (!dprintf_function)
9131 error (_("No function supplied for dprintf call"));
9132
9133 if (dprintf_channel && strlen (dprintf_channel) > 0)
9134 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9135 dprintf_function,
9136 dprintf_channel,
9137 dprintf_args);
9138 else
9139 printf_line = xstrprintf ("call (void) %s (%s)",
9140 dprintf_function,
9141 dprintf_args);
9142 }
9143 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9144 {
9145 if (target_can_run_breakpoint_commands ())
9146 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9147 else
9148 {
9149 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9150 printf_line = xstrprintf ("printf %s", dprintf_args);
9151 }
9152 }
9153 else
9154 internal_error (__FILE__, __LINE__,
9155 _("Invalid dprintf style."));
9156
9157 gdb_assert (printf_line != NULL);
9158 /* Manufacture a printf sequence. */
9159 {
9160 struct command_line *printf_cmd_line = XNEW (struct command_line);
9161
9162 printf_cmd_line->control_type = simple_control;
9163 printf_cmd_line->body_count = 0;
9164 printf_cmd_line->body_list = NULL;
9165 printf_cmd_line->next = NULL;
9166 printf_cmd_line->line = printf_line;
9167
9168 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9169 }
9170 }
9171
9172 /* Update all dprintf commands, making their command lists reflect
9173 current style settings. */
9174
9175 static void
9176 update_dprintf_commands (char *args, int from_tty,
9177 struct cmd_list_element *c)
9178 {
9179 struct breakpoint *b;
9180
9181 ALL_BREAKPOINTS (b)
9182 {
9183 if (b->type == bp_dprintf)
9184 update_dprintf_command_list (b);
9185 }
9186 }
9187
9188 /* Create a breakpoint with SAL as location. Use LOCATION
9189 as a description of the location, and COND_STRING
9190 as condition expression. If LOCATION is NULL then create an
9191 "address location" from the address in the SAL. */
9192
9193 static void
9194 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9195 struct symtabs_and_lines sals,
9196 event_location_up &&location,
9197 char *filter, char *cond_string,
9198 char *extra_string,
9199 enum bptype type, enum bpdisp disposition,
9200 int thread, int task, int ignore_count,
9201 const struct breakpoint_ops *ops, int from_tty,
9202 int enabled, int internal, unsigned flags,
9203 int display_canonical)
9204 {
9205 int i;
9206
9207 if (type == bp_hardware_breakpoint)
9208 {
9209 int target_resources_ok;
9210
9211 i = hw_breakpoint_used_count ();
9212 target_resources_ok =
9213 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9214 i + 1, 0);
9215 if (target_resources_ok == 0)
9216 error (_("No hardware breakpoint support in the target."));
9217 else if (target_resources_ok < 0)
9218 error (_("Hardware breakpoints used exceeds limit."));
9219 }
9220
9221 gdb_assert (sals.nelts > 0);
9222
9223 for (i = 0; i < sals.nelts; ++i)
9224 {
9225 struct symtab_and_line sal = sals.sals[i];
9226 struct bp_location *loc;
9227
9228 if (from_tty)
9229 {
9230 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9231 if (!loc_gdbarch)
9232 loc_gdbarch = gdbarch;
9233
9234 describe_other_breakpoints (loc_gdbarch,
9235 sal.pspace, sal.pc, sal.section, thread);
9236 }
9237
9238 if (i == 0)
9239 {
9240 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9241 b->thread = thread;
9242 b->task = task;
9243
9244 b->cond_string = cond_string;
9245 b->extra_string = extra_string;
9246 b->ignore_count = ignore_count;
9247 b->enable_state = enabled ? bp_enabled : bp_disabled;
9248 b->disposition = disposition;
9249
9250 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9251 b->loc->inserted = 1;
9252
9253 if (type == bp_static_tracepoint)
9254 {
9255 struct tracepoint *t = (struct tracepoint *) b;
9256 struct static_tracepoint_marker marker;
9257
9258 if (strace_marker_p (b))
9259 {
9260 /* We already know the marker exists, otherwise, we
9261 wouldn't see a sal for it. */
9262 const char *p
9263 = &event_location_to_string (b->location.get ())[3];
9264 const char *endp;
9265 char *marker_str;
9266
9267 p = skip_spaces_const (p);
9268
9269 endp = skip_to_space_const (p);
9270
9271 marker_str = savestring (p, endp - p);
9272 t->static_trace_marker_id = marker_str;
9273
9274 printf_filtered (_("Probed static tracepoint "
9275 "marker \"%s\"\n"),
9276 t->static_trace_marker_id);
9277 }
9278 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9279 {
9280 t->static_trace_marker_id = xstrdup (marker.str_id);
9281 release_static_tracepoint_marker (&marker);
9282
9283 printf_filtered (_("Probed static tracepoint "
9284 "marker \"%s\"\n"),
9285 t->static_trace_marker_id);
9286 }
9287 else
9288 warning (_("Couldn't determine the static "
9289 "tracepoint marker to probe"));
9290 }
9291
9292 loc = b->loc;
9293 }
9294 else
9295 {
9296 loc = add_location_to_breakpoint (b, &sal);
9297 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9298 loc->inserted = 1;
9299 }
9300
9301 if (b->cond_string)
9302 {
9303 const char *arg = b->cond_string;
9304
9305 loc->cond = parse_exp_1 (&arg, loc->address,
9306 block_for_pc (loc->address), 0);
9307 if (*arg)
9308 error (_("Garbage '%s' follows condition"), arg);
9309 }
9310
9311 /* Dynamic printf requires and uses additional arguments on the
9312 command line, otherwise it's an error. */
9313 if (type == bp_dprintf)
9314 {
9315 if (b->extra_string)
9316 update_dprintf_command_list (b);
9317 else
9318 error (_("Format string required"));
9319 }
9320 else if (b->extra_string)
9321 error (_("Garbage '%s' at end of command"), b->extra_string);
9322 }
9323
9324 b->display_canonical = display_canonical;
9325 if (location != NULL)
9326 b->location = std::move (location);
9327 else
9328 b->location = new_address_location (b->loc->address, NULL, 0);
9329 b->filter = filter;
9330 }
9331
9332 static void
9333 create_breakpoint_sal (struct gdbarch *gdbarch,
9334 struct symtabs_and_lines sals,
9335 event_location_up &&location,
9336 char *filter, char *cond_string,
9337 char *extra_string,
9338 enum bptype type, enum bpdisp disposition,
9339 int thread, int task, int ignore_count,
9340 const struct breakpoint_ops *ops, int from_tty,
9341 int enabled, int internal, unsigned flags,
9342 int display_canonical)
9343 {
9344 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
9345
9346 init_breakpoint_sal (b.get (), gdbarch,
9347 sals, std::move (location),
9348 filter, cond_string, extra_string,
9349 type, disposition,
9350 thread, task, ignore_count,
9351 ops, from_tty,
9352 enabled, internal, flags,
9353 display_canonical);
9354
9355 install_breakpoint (internal, b.release (), 0);
9356 }
9357
9358 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9359 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9360 value. COND_STRING, if not NULL, specified the condition to be
9361 used for all breakpoints. Essentially the only case where
9362 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9363 function. In that case, it's still not possible to specify
9364 separate conditions for different overloaded functions, so
9365 we take just a single condition string.
9366
9367 NOTE: If the function succeeds, the caller is expected to cleanup
9368 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9369 array contents). If the function fails (error() is called), the
9370 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9371 COND and SALS arrays and each of those arrays contents. */
9372
9373 static void
9374 create_breakpoints_sal (struct gdbarch *gdbarch,
9375 struct linespec_result *canonical,
9376 char *cond_string, char *extra_string,
9377 enum bptype type, enum bpdisp disposition,
9378 int thread, int task, int ignore_count,
9379 const struct breakpoint_ops *ops, int from_tty,
9380 int enabled, int internal, unsigned flags)
9381 {
9382 int i;
9383 struct linespec_sals *lsal;
9384
9385 if (canonical->pre_expanded)
9386 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9387
9388 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9389 {
9390 /* Note that 'location' can be NULL in the case of a plain
9391 'break', without arguments. */
9392 event_location_up location
9393 = (canonical->location != NULL
9394 ? copy_event_location (canonical->location.get ()) : NULL);
9395 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9396
9397 make_cleanup (xfree, filter_string);
9398 create_breakpoint_sal (gdbarch, lsal->sals,
9399 std::move (location),
9400 filter_string,
9401 cond_string, extra_string,
9402 type, disposition,
9403 thread, task, ignore_count, ops,
9404 from_tty, enabled, internal, flags,
9405 canonical->special_display);
9406 }
9407 }
9408
9409 /* Parse LOCATION which is assumed to be a SAL specification possibly
9410 followed by conditionals. On return, SALS contains an array of SAL
9411 addresses found. LOCATION points to the end of the SAL (for
9412 linespec locations).
9413
9414 The array and the line spec strings are allocated on the heap, it is
9415 the caller's responsibility to free them. */
9416
9417 static void
9418 parse_breakpoint_sals (const struct event_location *location,
9419 struct linespec_result *canonical)
9420 {
9421 struct symtab_and_line cursal;
9422
9423 if (event_location_type (location) == LINESPEC_LOCATION)
9424 {
9425 const char *address = get_linespec_location (location);
9426
9427 if (address == NULL)
9428 {
9429 /* The last displayed codepoint, if it's valid, is our default
9430 breakpoint address. */
9431 if (last_displayed_sal_is_valid ())
9432 {
9433 struct linespec_sals lsal;
9434 struct symtab_and_line sal;
9435 CORE_ADDR pc;
9436
9437 init_sal (&sal); /* Initialize to zeroes. */
9438 lsal.sals.sals = XNEW (struct symtab_and_line);
9439
9440 /* Set sal's pspace, pc, symtab, and line to the values
9441 corresponding to the last call to print_frame_info.
9442 Be sure to reinitialize LINE with NOTCURRENT == 0
9443 as the breakpoint line number is inappropriate otherwise.
9444 find_pc_line would adjust PC, re-set it back. */
9445 get_last_displayed_sal (&sal);
9446 pc = sal.pc;
9447 sal = find_pc_line (pc, 0);
9448
9449 /* "break" without arguments is equivalent to "break *PC"
9450 where PC is the last displayed codepoint's address. So
9451 make sure to set sal.explicit_pc to prevent GDB from
9452 trying to expand the list of sals to include all other
9453 instances with the same symtab and line. */
9454 sal.pc = pc;
9455 sal.explicit_pc = 1;
9456
9457 lsal.sals.sals[0] = sal;
9458 lsal.sals.nelts = 1;
9459 lsal.canonical = NULL;
9460
9461 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9462 return;
9463 }
9464 else
9465 error (_("No default breakpoint address now."));
9466 }
9467 }
9468
9469 /* Force almost all breakpoints to be in terms of the
9470 current_source_symtab (which is decode_line_1's default).
9471 This should produce the results we want almost all of the
9472 time while leaving default_breakpoint_* alone.
9473
9474 ObjC: However, don't match an Objective-C method name which
9475 may have a '+' or '-' succeeded by a '['. */
9476 cursal = get_current_source_symtab_and_line ();
9477 if (last_displayed_sal_is_valid ())
9478 {
9479 const char *address = NULL;
9480
9481 if (event_location_type (location) == LINESPEC_LOCATION)
9482 address = get_linespec_location (location);
9483
9484 if (!cursal.symtab
9485 || (address != NULL
9486 && strchr ("+-", address[0]) != NULL
9487 && address[1] != '['))
9488 {
9489 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9490 get_last_displayed_symtab (),
9491 get_last_displayed_line (),
9492 canonical, NULL, NULL);
9493 return;
9494 }
9495 }
9496
9497 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9498 cursal.symtab, cursal.line, canonical, NULL, NULL);
9499 }
9500
9501
9502 /* Convert each SAL into a real PC. Verify that the PC can be
9503 inserted as a breakpoint. If it can't throw an error. */
9504
9505 static void
9506 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9507 {
9508 int i;
9509
9510 for (i = 0; i < sals->nelts; i++)
9511 resolve_sal_pc (&sals->sals[i]);
9512 }
9513
9514 /* Fast tracepoints may have restrictions on valid locations. For
9515 instance, a fast tracepoint using a jump instead of a trap will
9516 likely have to overwrite more bytes than a trap would, and so can
9517 only be placed where the instruction is longer than the jump, or a
9518 multi-instruction sequence does not have a jump into the middle of
9519 it, etc. */
9520
9521 static void
9522 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9523 struct symtabs_and_lines *sals)
9524 {
9525 int i, rslt;
9526 struct symtab_and_line *sal;
9527 char *msg;
9528 struct cleanup *old_chain;
9529
9530 for (i = 0; i < sals->nelts; i++)
9531 {
9532 struct gdbarch *sarch;
9533
9534 sal = &sals->sals[i];
9535
9536 sarch = get_sal_arch (*sal);
9537 /* We fall back to GDBARCH if there is no architecture
9538 associated with SAL. */
9539 if (sarch == NULL)
9540 sarch = gdbarch;
9541 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9542 old_chain = make_cleanup (xfree, msg);
9543
9544 if (!rslt)
9545 error (_("May not have a fast tracepoint at %s%s"),
9546 paddress (sarch, sal->pc), (msg ? msg : ""));
9547
9548 do_cleanups (old_chain);
9549 }
9550 }
9551
9552 /* Given TOK, a string specification of condition and thread, as
9553 accepted by the 'break' command, extract the condition
9554 string and thread number and set *COND_STRING and *THREAD.
9555 PC identifies the context at which the condition should be parsed.
9556 If no condition is found, *COND_STRING is set to NULL.
9557 If no thread is found, *THREAD is set to -1. */
9558
9559 static void
9560 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9561 char **cond_string, int *thread, int *task,
9562 char **rest)
9563 {
9564 *cond_string = NULL;
9565 *thread = -1;
9566 *task = 0;
9567 *rest = NULL;
9568
9569 while (tok && *tok)
9570 {
9571 const char *end_tok;
9572 int toklen;
9573 const char *cond_start = NULL;
9574 const char *cond_end = NULL;
9575
9576 tok = skip_spaces_const (tok);
9577
9578 if ((*tok == '"' || *tok == ',') && rest)
9579 {
9580 *rest = savestring (tok, strlen (tok));
9581 return;
9582 }
9583
9584 end_tok = skip_to_space_const (tok);
9585
9586 toklen = end_tok - tok;
9587
9588 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9589 {
9590 tok = cond_start = end_tok + 1;
9591 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9592 cond_end = tok;
9593 *cond_string = savestring (cond_start, cond_end - cond_start);
9594 }
9595 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9596 {
9597 const char *tmptok;
9598 struct thread_info *thr;
9599
9600 tok = end_tok + 1;
9601 thr = parse_thread_id (tok, &tmptok);
9602 if (tok == tmptok)
9603 error (_("Junk after thread keyword."));
9604 *thread = thr->global_num;
9605 tok = tmptok;
9606 }
9607 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9608 {
9609 char *tmptok;
9610
9611 tok = end_tok + 1;
9612 *task = strtol (tok, &tmptok, 0);
9613 if (tok == tmptok)
9614 error (_("Junk after task keyword."));
9615 if (!valid_task_id (*task))
9616 error (_("Unknown task %d."), *task);
9617 tok = tmptok;
9618 }
9619 else if (rest)
9620 {
9621 *rest = savestring (tok, strlen (tok));
9622 return;
9623 }
9624 else
9625 error (_("Junk at end of arguments."));
9626 }
9627 }
9628
9629 /* Decode a static tracepoint marker spec. */
9630
9631 static struct symtabs_and_lines
9632 decode_static_tracepoint_spec (const char **arg_p)
9633 {
9634 VEC(static_tracepoint_marker_p) *markers = NULL;
9635 struct symtabs_and_lines sals;
9636 struct cleanup *old_chain;
9637 const char *p = &(*arg_p)[3];
9638 const char *endp;
9639 char *marker_str;
9640 int i;
9641
9642 p = skip_spaces_const (p);
9643
9644 endp = skip_to_space_const (p);
9645
9646 marker_str = savestring (p, endp - p);
9647 old_chain = make_cleanup (xfree, marker_str);
9648
9649 markers = target_static_tracepoint_markers_by_strid (marker_str);
9650 if (VEC_empty(static_tracepoint_marker_p, markers))
9651 error (_("No known static tracepoint marker named %s"), marker_str);
9652
9653 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9654 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9655
9656 for (i = 0; i < sals.nelts; i++)
9657 {
9658 struct static_tracepoint_marker *marker;
9659
9660 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9661
9662 init_sal (&sals.sals[i]);
9663
9664 sals.sals[i] = find_pc_line (marker->address, 0);
9665 sals.sals[i].pc = marker->address;
9666
9667 release_static_tracepoint_marker (marker);
9668 }
9669
9670 do_cleanups (old_chain);
9671
9672 *arg_p = endp;
9673 return sals;
9674 }
9675
9676 /* See breakpoint.h. */
9677
9678 int
9679 create_breakpoint (struct gdbarch *gdbarch,
9680 const struct event_location *location, char *cond_string,
9681 int thread, char *extra_string,
9682 int parse_extra,
9683 int tempflag, enum bptype type_wanted,
9684 int ignore_count,
9685 enum auto_boolean pending_break_support,
9686 const struct breakpoint_ops *ops,
9687 int from_tty, int enabled, int internal,
9688 unsigned flags)
9689 {
9690 struct linespec_result canonical;
9691 struct cleanup *bkpt_chain = NULL;
9692 int pending = 0;
9693 int task = 0;
9694 int prev_bkpt_count = breakpoint_count;
9695
9696 gdb_assert (ops != NULL);
9697
9698 /* If extra_string isn't useful, set it to NULL. */
9699 if (extra_string != NULL && *extra_string == '\0')
9700 extra_string = NULL;
9701
9702 TRY
9703 {
9704 ops->create_sals_from_location (location, &canonical, type_wanted);
9705 }
9706 CATCH (e, RETURN_MASK_ERROR)
9707 {
9708 /* If caller is interested in rc value from parse, set
9709 value. */
9710 if (e.error == NOT_FOUND_ERROR)
9711 {
9712 /* If pending breakpoint support is turned off, throw
9713 error. */
9714
9715 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9716 throw_exception (e);
9717
9718 exception_print (gdb_stderr, e);
9719
9720 /* If pending breakpoint support is auto query and the user
9721 selects no, then simply return the error code. */
9722 if (pending_break_support == AUTO_BOOLEAN_AUTO
9723 && !nquery (_("Make %s pending on future shared library load? "),
9724 bptype_string (type_wanted)))
9725 return 0;
9726
9727 /* At this point, either the user was queried about setting
9728 a pending breakpoint and selected yes, or pending
9729 breakpoint behavior is on and thus a pending breakpoint
9730 is defaulted on behalf of the user. */
9731 pending = 1;
9732 }
9733 else
9734 throw_exception (e);
9735 }
9736 END_CATCH
9737
9738 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9739 return 0;
9740
9741 /* ----------------------------- SNIP -----------------------------
9742 Anything added to the cleanup chain beyond this point is assumed
9743 to be part of a breakpoint. If the breakpoint create succeeds
9744 then the memory is not reclaimed. */
9745 bkpt_chain = make_cleanup (null_cleanup, 0);
9746
9747 /* Resolve all line numbers to PC's and verify that the addresses
9748 are ok for the target. */
9749 if (!pending)
9750 {
9751 int ix;
9752 struct linespec_sals *iter;
9753
9754 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9755 breakpoint_sals_to_pc (&iter->sals);
9756 }
9757
9758 /* Fast tracepoints may have additional restrictions on location. */
9759 if (!pending && type_wanted == bp_fast_tracepoint)
9760 {
9761 int ix;
9762 struct linespec_sals *iter;
9763
9764 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9765 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9766 }
9767
9768 /* Verify that condition can be parsed, before setting any
9769 breakpoints. Allocate a separate condition expression for each
9770 breakpoint. */
9771 if (!pending)
9772 {
9773 if (parse_extra)
9774 {
9775 char *rest;
9776 struct linespec_sals *lsal;
9777
9778 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9779
9780 /* Here we only parse 'arg' to separate condition
9781 from thread number, so parsing in context of first
9782 sal is OK. When setting the breakpoint we'll
9783 re-parse it in context of each sal. */
9784
9785 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9786 &cond_string, &thread, &task, &rest);
9787 if (cond_string)
9788 make_cleanup (xfree, cond_string);
9789 if (rest)
9790 make_cleanup (xfree, rest);
9791 if (rest)
9792 extra_string = rest;
9793 else
9794 extra_string = NULL;
9795 }
9796 else
9797 {
9798 if (type_wanted != bp_dprintf
9799 && extra_string != NULL && *extra_string != '\0')
9800 error (_("Garbage '%s' at end of location"), extra_string);
9801
9802 /* Create a private copy of condition string. */
9803 if (cond_string)
9804 {
9805 cond_string = xstrdup (cond_string);
9806 make_cleanup (xfree, cond_string);
9807 }
9808 /* Create a private copy of any extra string. */
9809 if (extra_string)
9810 {
9811 extra_string = xstrdup (extra_string);
9812 make_cleanup (xfree, extra_string);
9813 }
9814 }
9815
9816 ops->create_breakpoints_sal (gdbarch, &canonical,
9817 cond_string, extra_string, type_wanted,
9818 tempflag ? disp_del : disp_donttouch,
9819 thread, task, ignore_count, ops,
9820 from_tty, enabled, internal, flags);
9821 }
9822 else
9823 {
9824 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
9825
9826 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
9827 b->location = copy_event_location (location);
9828
9829 if (parse_extra)
9830 b->cond_string = NULL;
9831 else
9832 {
9833 /* Create a private copy of condition string. */
9834 if (cond_string)
9835 {
9836 cond_string = xstrdup (cond_string);
9837 make_cleanup (xfree, cond_string);
9838 }
9839 b->cond_string = cond_string;
9840 b->thread = thread;
9841 }
9842
9843 /* Create a private copy of any extra string. */
9844 if (extra_string != NULL)
9845 {
9846 extra_string = xstrdup (extra_string);
9847 make_cleanup (xfree, extra_string);
9848 }
9849 b->extra_string = extra_string;
9850 b->ignore_count = ignore_count;
9851 b->disposition = tempflag ? disp_del : disp_donttouch;
9852 b->condition_not_parsed = 1;
9853 b->enable_state = enabled ? bp_enabled : bp_disabled;
9854 if ((type_wanted != bp_breakpoint
9855 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9856 b->pspace = current_program_space;
9857
9858 install_breakpoint (internal, b.release (), 0);
9859 }
9860
9861 if (VEC_length (linespec_sals, canonical.sals) > 1)
9862 {
9863 warning (_("Multiple breakpoints were set.\nUse the "
9864 "\"delete\" command to delete unwanted breakpoints."));
9865 prev_breakpoint_count = prev_bkpt_count;
9866 }
9867
9868 /* That's it. Discard the cleanups for data inserted into the
9869 breakpoint. */
9870 discard_cleanups (bkpt_chain);
9871
9872 /* error call may happen here - have BKPT_CHAIN already discarded. */
9873 update_global_location_list (UGLL_MAY_INSERT);
9874
9875 return 1;
9876 }
9877
9878 /* Set a breakpoint.
9879 ARG is a string describing breakpoint address,
9880 condition, and thread.
9881 FLAG specifies if a breakpoint is hardware on,
9882 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9883 and BP_TEMPFLAG. */
9884
9885 static void
9886 break_command_1 (char *arg, int flag, int from_tty)
9887 {
9888 int tempflag = flag & BP_TEMPFLAG;
9889 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9890 ? bp_hardware_breakpoint
9891 : bp_breakpoint);
9892 struct breakpoint_ops *ops;
9893
9894 event_location_up location = string_to_event_location (&arg, current_language);
9895
9896 /* Matching breakpoints on probes. */
9897 if (location != NULL
9898 && event_location_type (location.get ()) == PROBE_LOCATION)
9899 ops = &bkpt_probe_breakpoint_ops;
9900 else
9901 ops = &bkpt_breakpoint_ops;
9902
9903 create_breakpoint (get_current_arch (),
9904 location.get (),
9905 NULL, 0, arg, 1 /* parse arg */,
9906 tempflag, type_wanted,
9907 0 /* Ignore count */,
9908 pending_break_support,
9909 ops,
9910 from_tty,
9911 1 /* enabled */,
9912 0 /* internal */,
9913 0);
9914 }
9915
9916 /* Helper function for break_command_1 and disassemble_command. */
9917
9918 void
9919 resolve_sal_pc (struct symtab_and_line *sal)
9920 {
9921 CORE_ADDR pc;
9922
9923 if (sal->pc == 0 && sal->symtab != NULL)
9924 {
9925 if (!find_line_pc (sal->symtab, sal->line, &pc))
9926 error (_("No line %d in file \"%s\"."),
9927 sal->line, symtab_to_filename_for_display (sal->symtab));
9928 sal->pc = pc;
9929
9930 /* If this SAL corresponds to a breakpoint inserted using a line
9931 number, then skip the function prologue if necessary. */
9932 if (sal->explicit_line)
9933 skip_prologue_sal (sal);
9934 }
9935
9936 if (sal->section == 0 && sal->symtab != NULL)
9937 {
9938 const struct blockvector *bv;
9939 const struct block *b;
9940 struct symbol *sym;
9941
9942 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9943 SYMTAB_COMPUNIT (sal->symtab));
9944 if (bv != NULL)
9945 {
9946 sym = block_linkage_function (b);
9947 if (sym != NULL)
9948 {
9949 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9950 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9951 sym);
9952 }
9953 else
9954 {
9955 /* It really is worthwhile to have the section, so we'll
9956 just have to look harder. This case can be executed
9957 if we have line numbers but no functions (as can
9958 happen in assembly source). */
9959
9960 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9961 switch_to_program_space_and_thread (sal->pspace);
9962
9963 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9964 if (msym.minsym)
9965 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9966 }
9967 }
9968 }
9969 }
9970
9971 void
9972 break_command (char *arg, int from_tty)
9973 {
9974 break_command_1 (arg, 0, from_tty);
9975 }
9976
9977 void
9978 tbreak_command (char *arg, int from_tty)
9979 {
9980 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9981 }
9982
9983 static void
9984 hbreak_command (char *arg, int from_tty)
9985 {
9986 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9987 }
9988
9989 static void
9990 thbreak_command (char *arg, int from_tty)
9991 {
9992 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9993 }
9994
9995 static void
9996 stop_command (char *arg, int from_tty)
9997 {
9998 printf_filtered (_("Specify the type of breakpoint to set.\n\
9999 Usage: stop in <function | address>\n\
10000 stop at <line>\n"));
10001 }
10002
10003 static void
10004 stopin_command (char *arg, int from_tty)
10005 {
10006 int badInput = 0;
10007
10008 if (arg == (char *) NULL)
10009 badInput = 1;
10010 else if (*arg != '*')
10011 {
10012 char *argptr = arg;
10013 int hasColon = 0;
10014
10015 /* Look for a ':'. If this is a line number specification, then
10016 say it is bad, otherwise, it should be an address or
10017 function/method name. */
10018 while (*argptr && !hasColon)
10019 {
10020 hasColon = (*argptr == ':');
10021 argptr++;
10022 }
10023
10024 if (hasColon)
10025 badInput = (*argptr != ':'); /* Not a class::method */
10026 else
10027 badInput = isdigit (*arg); /* a simple line number */
10028 }
10029
10030 if (badInput)
10031 printf_filtered (_("Usage: stop in <function | address>\n"));
10032 else
10033 break_command_1 (arg, 0, from_tty);
10034 }
10035
10036 static void
10037 stopat_command (char *arg, int from_tty)
10038 {
10039 int badInput = 0;
10040
10041 if (arg == (char *) NULL || *arg == '*') /* no line number */
10042 badInput = 1;
10043 else
10044 {
10045 char *argptr = arg;
10046 int hasColon = 0;
10047
10048 /* Look for a ':'. If there is a '::' then get out, otherwise
10049 it is probably a line number. */
10050 while (*argptr && !hasColon)
10051 {
10052 hasColon = (*argptr == ':');
10053 argptr++;
10054 }
10055
10056 if (hasColon)
10057 badInput = (*argptr == ':'); /* we have class::method */
10058 else
10059 badInput = !isdigit (*arg); /* not a line number */
10060 }
10061
10062 if (badInput)
10063 printf_filtered (_("Usage: stop at <line>\n"));
10064 else
10065 break_command_1 (arg, 0, from_tty);
10066 }
10067
10068 /* The dynamic printf command is mostly like a regular breakpoint, but
10069 with a prewired command list consisting of a single output command,
10070 built from extra arguments supplied on the dprintf command
10071 line. */
10072
10073 static void
10074 dprintf_command (char *arg, int from_tty)
10075 {
10076 event_location_up location = string_to_event_location (&arg, current_language);
10077
10078 /* If non-NULL, ARG should have been advanced past the location;
10079 the next character must be ','. */
10080 if (arg != NULL)
10081 {
10082 if (arg[0] != ',' || arg[1] == '\0')
10083 error (_("Format string required"));
10084 else
10085 {
10086 /* Skip the comma. */
10087 ++arg;
10088 }
10089 }
10090
10091 create_breakpoint (get_current_arch (),
10092 location.get (),
10093 NULL, 0, arg, 1 /* parse arg */,
10094 0, bp_dprintf,
10095 0 /* Ignore count */,
10096 pending_break_support,
10097 &dprintf_breakpoint_ops,
10098 from_tty,
10099 1 /* enabled */,
10100 0 /* internal */,
10101 0);
10102 }
10103
10104 static void
10105 agent_printf_command (char *arg, int from_tty)
10106 {
10107 error (_("May only run agent-printf on the target"));
10108 }
10109
10110 /* Implement the "breakpoint_hit" breakpoint_ops method for
10111 ranged breakpoints. */
10112
10113 static int
10114 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10115 struct address_space *aspace,
10116 CORE_ADDR bp_addr,
10117 const struct target_waitstatus *ws)
10118 {
10119 if (ws->kind != TARGET_WAITKIND_STOPPED
10120 || ws->value.sig != GDB_SIGNAL_TRAP)
10121 return 0;
10122
10123 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10124 bl->length, aspace, bp_addr);
10125 }
10126
10127 /* Implement the "resources_needed" breakpoint_ops method for
10128 ranged breakpoints. */
10129
10130 static int
10131 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10132 {
10133 return target_ranged_break_num_registers ();
10134 }
10135
10136 /* Implement the "print_it" breakpoint_ops method for
10137 ranged breakpoints. */
10138
10139 static enum print_stop_action
10140 print_it_ranged_breakpoint (bpstat bs)
10141 {
10142 struct breakpoint *b = bs->breakpoint_at;
10143 struct bp_location *bl = b->loc;
10144 struct ui_out *uiout = current_uiout;
10145
10146 gdb_assert (b->type == bp_hardware_breakpoint);
10147
10148 /* Ranged breakpoints have only one location. */
10149 gdb_assert (bl && bl->next == NULL);
10150
10151 annotate_breakpoint (b->number);
10152
10153 maybe_print_thread_hit_breakpoint (uiout);
10154
10155 if (b->disposition == disp_del)
10156 uiout->text ("Temporary ranged breakpoint ");
10157 else
10158 uiout->text ("Ranged breakpoint ");
10159 if (uiout->is_mi_like_p ())
10160 {
10161 uiout->field_string ("reason",
10162 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10163 uiout->field_string ("disp", bpdisp_text (b->disposition));
10164 }
10165 uiout->field_int ("bkptno", b->number);
10166 uiout->text (", ");
10167
10168 return PRINT_SRC_AND_LOC;
10169 }
10170
10171 /* Implement the "print_one" breakpoint_ops method for
10172 ranged breakpoints. */
10173
10174 static void
10175 print_one_ranged_breakpoint (struct breakpoint *b,
10176 struct bp_location **last_loc)
10177 {
10178 struct bp_location *bl = b->loc;
10179 struct value_print_options opts;
10180 struct ui_out *uiout = current_uiout;
10181
10182 /* Ranged breakpoints have only one location. */
10183 gdb_assert (bl && bl->next == NULL);
10184
10185 get_user_print_options (&opts);
10186
10187 if (opts.addressprint)
10188 /* We don't print the address range here, it will be printed later
10189 by print_one_detail_ranged_breakpoint. */
10190 uiout->field_skip ("addr");
10191 annotate_field (5);
10192 print_breakpoint_location (b, bl);
10193 *last_loc = bl;
10194 }
10195
10196 /* Implement the "print_one_detail" breakpoint_ops method for
10197 ranged breakpoints. */
10198
10199 static void
10200 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10201 struct ui_out *uiout)
10202 {
10203 CORE_ADDR address_start, address_end;
10204 struct bp_location *bl = b->loc;
10205 string_file stb;
10206
10207 gdb_assert (bl);
10208
10209 address_start = bl->address;
10210 address_end = address_start + bl->length - 1;
10211
10212 uiout->text ("\taddress range: ");
10213 stb.printf ("[%s, %s]",
10214 print_core_address (bl->gdbarch, address_start),
10215 print_core_address (bl->gdbarch, address_end));
10216 uiout->field_stream ("addr", stb);
10217 uiout->text ("\n");
10218 }
10219
10220 /* Implement the "print_mention" breakpoint_ops method for
10221 ranged breakpoints. */
10222
10223 static void
10224 print_mention_ranged_breakpoint (struct breakpoint *b)
10225 {
10226 struct bp_location *bl = b->loc;
10227 struct ui_out *uiout = current_uiout;
10228
10229 gdb_assert (bl);
10230 gdb_assert (b->type == bp_hardware_breakpoint);
10231
10232 if (uiout->is_mi_like_p ())
10233 return;
10234
10235 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10236 b->number, paddress (bl->gdbarch, bl->address),
10237 paddress (bl->gdbarch, bl->address + bl->length - 1));
10238 }
10239
10240 /* Implement the "print_recreate" breakpoint_ops method for
10241 ranged breakpoints. */
10242
10243 static void
10244 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10245 {
10246 fprintf_unfiltered (fp, "break-range %s, %s",
10247 event_location_to_string (b->location.get ()),
10248 event_location_to_string (b->location_range_end.get ()));
10249 print_recreate_thread (b, fp);
10250 }
10251
10252 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10253
10254 static struct breakpoint_ops ranged_breakpoint_ops;
10255
10256 /* Find the address where the end of the breakpoint range should be
10257 placed, given the SAL of the end of the range. This is so that if
10258 the user provides a line number, the end of the range is set to the
10259 last instruction of the given line. */
10260
10261 static CORE_ADDR
10262 find_breakpoint_range_end (struct symtab_and_line sal)
10263 {
10264 CORE_ADDR end;
10265
10266 /* If the user provided a PC value, use it. Otherwise,
10267 find the address of the end of the given location. */
10268 if (sal.explicit_pc)
10269 end = sal.pc;
10270 else
10271 {
10272 int ret;
10273 CORE_ADDR start;
10274
10275 ret = find_line_pc_range (sal, &start, &end);
10276 if (!ret)
10277 error (_("Could not find location of the end of the range."));
10278
10279 /* find_line_pc_range returns the start of the next line. */
10280 end--;
10281 }
10282
10283 return end;
10284 }
10285
10286 /* Implement the "break-range" CLI command. */
10287
10288 static void
10289 break_range_command (char *arg, int from_tty)
10290 {
10291 char *arg_start, *addr_string_start;
10292 struct linespec_result canonical_start, canonical_end;
10293 int bp_count, can_use_bp, length;
10294 CORE_ADDR end;
10295 struct breakpoint *b;
10296 struct symtab_and_line sal_start, sal_end;
10297 struct cleanup *cleanup_bkpt;
10298 struct linespec_sals *lsal_start, *lsal_end;
10299
10300 /* We don't support software ranged breakpoints. */
10301 if (target_ranged_break_num_registers () < 0)
10302 error (_("This target does not support hardware ranged breakpoints."));
10303
10304 bp_count = hw_breakpoint_used_count ();
10305 bp_count += target_ranged_break_num_registers ();
10306 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10307 bp_count, 0);
10308 if (can_use_bp < 0)
10309 error (_("Hardware breakpoints used exceeds limit."));
10310
10311 arg = skip_spaces (arg);
10312 if (arg == NULL || arg[0] == '\0')
10313 error(_("No address range specified."));
10314
10315 arg_start = arg;
10316 event_location_up start_location = string_to_event_location (&arg,
10317 current_language);
10318 parse_breakpoint_sals (start_location.get (), &canonical_start);
10319
10320 if (arg[0] != ',')
10321 error (_("Too few arguments."));
10322 else if (VEC_empty (linespec_sals, canonical_start.sals))
10323 error (_("Could not find location of the beginning of the range."));
10324
10325 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10326
10327 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10328 || lsal_start->sals.nelts != 1)
10329 error (_("Cannot create a ranged breakpoint with multiple locations."));
10330
10331 sal_start = lsal_start->sals.sals[0];
10332 addr_string_start = savestring (arg_start, arg - arg_start);
10333 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10334
10335 arg++; /* Skip the comma. */
10336 arg = skip_spaces (arg);
10337
10338 /* Parse the end location. */
10339
10340 arg_start = arg;
10341
10342 /* We call decode_line_full directly here instead of using
10343 parse_breakpoint_sals because we need to specify the start location's
10344 symtab and line as the default symtab and line for the end of the
10345 range. This makes it possible to have ranges like "foo.c:27, +14",
10346 where +14 means 14 lines from the start location. */
10347 event_location_up end_location = string_to_event_location (&arg,
10348 current_language);
10349 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10350 sal_start.symtab, sal_start.line,
10351 &canonical_end, NULL, NULL);
10352
10353 if (VEC_empty (linespec_sals, canonical_end.sals))
10354 error (_("Could not find location of the end of the range."));
10355
10356 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10357 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10358 || lsal_end->sals.nelts != 1)
10359 error (_("Cannot create a ranged breakpoint with multiple locations."));
10360
10361 sal_end = lsal_end->sals.sals[0];
10362
10363 end = find_breakpoint_range_end (sal_end);
10364 if (sal_start.pc > end)
10365 error (_("Invalid address range, end precedes start."));
10366
10367 length = end - sal_start.pc + 1;
10368 if (length < 0)
10369 /* Length overflowed. */
10370 error (_("Address range too large."));
10371 else if (length == 1)
10372 {
10373 /* This range is simple enough to be handled by
10374 the `hbreak' command. */
10375 hbreak_command (addr_string_start, 1);
10376
10377 do_cleanups (cleanup_bkpt);
10378
10379 return;
10380 }
10381
10382 /* Now set up the breakpoint. */
10383 b = set_raw_breakpoint (get_current_arch (), sal_start,
10384 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10385 set_breakpoint_count (breakpoint_count + 1);
10386 b->number = breakpoint_count;
10387 b->disposition = disp_donttouch;
10388 b->location = std::move (start_location);
10389 b->location_range_end = std::move (end_location);
10390 b->loc->length = length;
10391
10392 do_cleanups (cleanup_bkpt);
10393
10394 mention (b);
10395 observer_notify_breakpoint_created (b);
10396 update_global_location_list (UGLL_MAY_INSERT);
10397 }
10398
10399 /* Return non-zero if EXP is verified as constant. Returned zero
10400 means EXP is variable. Also the constant detection may fail for
10401 some constant expressions and in such case still falsely return
10402 zero. */
10403
10404 static int
10405 watchpoint_exp_is_const (const struct expression *exp)
10406 {
10407 int i = exp->nelts;
10408
10409 while (i > 0)
10410 {
10411 int oplenp, argsp;
10412
10413 /* We are only interested in the descriptor of each element. */
10414 operator_length (exp, i, &oplenp, &argsp);
10415 i -= oplenp;
10416
10417 switch (exp->elts[i].opcode)
10418 {
10419 case BINOP_ADD:
10420 case BINOP_SUB:
10421 case BINOP_MUL:
10422 case BINOP_DIV:
10423 case BINOP_REM:
10424 case BINOP_MOD:
10425 case BINOP_LSH:
10426 case BINOP_RSH:
10427 case BINOP_LOGICAL_AND:
10428 case BINOP_LOGICAL_OR:
10429 case BINOP_BITWISE_AND:
10430 case BINOP_BITWISE_IOR:
10431 case BINOP_BITWISE_XOR:
10432 case BINOP_EQUAL:
10433 case BINOP_NOTEQUAL:
10434 case BINOP_LESS:
10435 case BINOP_GTR:
10436 case BINOP_LEQ:
10437 case BINOP_GEQ:
10438 case BINOP_REPEAT:
10439 case BINOP_COMMA:
10440 case BINOP_EXP:
10441 case BINOP_MIN:
10442 case BINOP_MAX:
10443 case BINOP_INTDIV:
10444 case BINOP_CONCAT:
10445 case TERNOP_COND:
10446 case TERNOP_SLICE:
10447
10448 case OP_LONG:
10449 case OP_DOUBLE:
10450 case OP_DECFLOAT:
10451 case OP_LAST:
10452 case OP_COMPLEX:
10453 case OP_STRING:
10454 case OP_ARRAY:
10455 case OP_TYPE:
10456 case OP_TYPEOF:
10457 case OP_DECLTYPE:
10458 case OP_TYPEID:
10459 case OP_NAME:
10460 case OP_OBJC_NSSTRING:
10461
10462 case UNOP_NEG:
10463 case UNOP_LOGICAL_NOT:
10464 case UNOP_COMPLEMENT:
10465 case UNOP_ADDR:
10466 case UNOP_HIGH:
10467 case UNOP_CAST:
10468
10469 case UNOP_CAST_TYPE:
10470 case UNOP_REINTERPRET_CAST:
10471 case UNOP_DYNAMIC_CAST:
10472 /* Unary, binary and ternary operators: We have to check
10473 their operands. If they are constant, then so is the
10474 result of that operation. For instance, if A and B are
10475 determined to be constants, then so is "A + B".
10476
10477 UNOP_IND is one exception to the rule above, because the
10478 value of *ADDR is not necessarily a constant, even when
10479 ADDR is. */
10480 break;
10481
10482 case OP_VAR_VALUE:
10483 /* Check whether the associated symbol is a constant.
10484
10485 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10486 possible that a buggy compiler could mark a variable as
10487 constant even when it is not, and TYPE_CONST would return
10488 true in this case, while SYMBOL_CLASS wouldn't.
10489
10490 We also have to check for function symbols because they
10491 are always constant. */
10492 {
10493 struct symbol *s = exp->elts[i + 2].symbol;
10494
10495 if (SYMBOL_CLASS (s) != LOC_BLOCK
10496 && SYMBOL_CLASS (s) != LOC_CONST
10497 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10498 return 0;
10499 break;
10500 }
10501
10502 /* The default action is to return 0 because we are using
10503 the optimistic approach here: If we don't know something,
10504 then it is not a constant. */
10505 default:
10506 return 0;
10507 }
10508 }
10509
10510 return 1;
10511 }
10512
10513 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10514
10515 static void
10516 dtor_watchpoint (struct breakpoint *self)
10517 {
10518 struct watchpoint *w = (struct watchpoint *) self;
10519
10520 xfree (w->exp_string);
10521 xfree (w->exp_string_reparse);
10522 value_free (w->val);
10523
10524 base_breakpoint_ops.dtor (self);
10525 }
10526
10527 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10528
10529 static void
10530 re_set_watchpoint (struct breakpoint *b)
10531 {
10532 struct watchpoint *w = (struct watchpoint *) b;
10533
10534 /* Watchpoint can be either on expression using entirely global
10535 variables, or it can be on local variables.
10536
10537 Watchpoints of the first kind are never auto-deleted, and even
10538 persist across program restarts. Since they can use variables
10539 from shared libraries, we need to reparse expression as libraries
10540 are loaded and unloaded.
10541
10542 Watchpoints on local variables can also change meaning as result
10543 of solib event. For example, if a watchpoint uses both a local
10544 and a global variables in expression, it's a local watchpoint,
10545 but unloading of a shared library will make the expression
10546 invalid. This is not a very common use case, but we still
10547 re-evaluate expression, to avoid surprises to the user.
10548
10549 Note that for local watchpoints, we re-evaluate it only if
10550 watchpoints frame id is still valid. If it's not, it means the
10551 watchpoint is out of scope and will be deleted soon. In fact,
10552 I'm not sure we'll ever be called in this case.
10553
10554 If a local watchpoint's frame id is still valid, then
10555 w->exp_valid_block is likewise valid, and we can safely use it.
10556
10557 Don't do anything about disabled watchpoints, since they will be
10558 reevaluated again when enabled. */
10559 update_watchpoint (w, 1 /* reparse */);
10560 }
10561
10562 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10563
10564 static int
10565 insert_watchpoint (struct bp_location *bl)
10566 {
10567 struct watchpoint *w = (struct watchpoint *) bl->owner;
10568 int length = w->exact ? 1 : bl->length;
10569
10570 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10571 w->cond_exp.get ());
10572 }
10573
10574 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10575
10576 static int
10577 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10578 {
10579 struct watchpoint *w = (struct watchpoint *) bl->owner;
10580 int length = w->exact ? 1 : bl->length;
10581
10582 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10583 w->cond_exp.get ());
10584 }
10585
10586 static int
10587 breakpoint_hit_watchpoint (const struct bp_location *bl,
10588 struct address_space *aspace, CORE_ADDR bp_addr,
10589 const struct target_waitstatus *ws)
10590 {
10591 struct breakpoint *b = bl->owner;
10592 struct watchpoint *w = (struct watchpoint *) b;
10593
10594 /* Continuable hardware watchpoints are treated as non-existent if the
10595 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10596 some data address). Otherwise gdb won't stop on a break instruction
10597 in the code (not from a breakpoint) when a hardware watchpoint has
10598 been defined. Also skip watchpoints which we know did not trigger
10599 (did not match the data address). */
10600 if (is_hardware_watchpoint (b)
10601 && w->watchpoint_triggered == watch_triggered_no)
10602 return 0;
10603
10604 return 1;
10605 }
10606
10607 static void
10608 check_status_watchpoint (bpstat bs)
10609 {
10610 gdb_assert (is_watchpoint (bs->breakpoint_at));
10611
10612 bpstat_check_watchpoint (bs);
10613 }
10614
10615 /* Implement the "resources_needed" breakpoint_ops method for
10616 hardware watchpoints. */
10617
10618 static int
10619 resources_needed_watchpoint (const struct bp_location *bl)
10620 {
10621 struct watchpoint *w = (struct watchpoint *) bl->owner;
10622 int length = w->exact? 1 : bl->length;
10623
10624 return target_region_ok_for_hw_watchpoint (bl->address, length);
10625 }
10626
10627 /* Implement the "works_in_software_mode" breakpoint_ops method for
10628 hardware watchpoints. */
10629
10630 static int
10631 works_in_software_mode_watchpoint (const struct breakpoint *b)
10632 {
10633 /* Read and access watchpoints only work with hardware support. */
10634 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10635 }
10636
10637 static enum print_stop_action
10638 print_it_watchpoint (bpstat bs)
10639 {
10640 struct cleanup *old_chain;
10641 struct breakpoint *b;
10642 enum print_stop_action result;
10643 struct watchpoint *w;
10644 struct ui_out *uiout = current_uiout;
10645
10646 gdb_assert (bs->bp_location_at != NULL);
10647
10648 b = bs->breakpoint_at;
10649 w = (struct watchpoint *) b;
10650
10651 old_chain = make_cleanup (null_cleanup, NULL);
10652
10653 annotate_watchpoint (b->number);
10654 maybe_print_thread_hit_breakpoint (uiout);
10655
10656 string_file stb;
10657
10658 switch (b->type)
10659 {
10660 case bp_watchpoint:
10661 case bp_hardware_watchpoint:
10662 if (uiout->is_mi_like_p ())
10663 uiout->field_string
10664 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10665 mention (b);
10666 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10667 uiout->text ("\nOld value = ");
10668 watchpoint_value_print (bs->old_val, &stb);
10669 uiout->field_stream ("old", stb);
10670 uiout->text ("\nNew value = ");
10671 watchpoint_value_print (w->val, &stb);
10672 uiout->field_stream ("new", stb);
10673 uiout->text ("\n");
10674 /* More than one watchpoint may have been triggered. */
10675 result = PRINT_UNKNOWN;
10676 break;
10677
10678 case bp_read_watchpoint:
10679 if (uiout->is_mi_like_p ())
10680 uiout->field_string
10681 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10682 mention (b);
10683 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10684 uiout->text ("\nValue = ");
10685 watchpoint_value_print (w->val, &stb);
10686 uiout->field_stream ("value", stb);
10687 uiout->text ("\n");
10688 result = PRINT_UNKNOWN;
10689 break;
10690
10691 case bp_access_watchpoint:
10692 if (bs->old_val != NULL)
10693 {
10694 if (uiout->is_mi_like_p ())
10695 uiout->field_string
10696 ("reason",
10697 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10698 mention (b);
10699 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10700 uiout->text ("\nOld value = ");
10701 watchpoint_value_print (bs->old_val, &stb);
10702 uiout->field_stream ("old", stb);
10703 uiout->text ("\nNew value = ");
10704 }
10705 else
10706 {
10707 mention (b);
10708 if (uiout->is_mi_like_p ())
10709 uiout->field_string
10710 ("reason",
10711 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10712 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10713 uiout->text ("\nValue = ");
10714 }
10715 watchpoint_value_print (w->val, &stb);
10716 uiout->field_stream ("new", stb);
10717 uiout->text ("\n");
10718 result = PRINT_UNKNOWN;
10719 break;
10720 default:
10721 result = PRINT_UNKNOWN;
10722 }
10723
10724 do_cleanups (old_chain);
10725 return result;
10726 }
10727
10728 /* Implement the "print_mention" breakpoint_ops method for hardware
10729 watchpoints. */
10730
10731 static void
10732 print_mention_watchpoint (struct breakpoint *b)
10733 {
10734 struct watchpoint *w = (struct watchpoint *) b;
10735 struct ui_out *uiout = current_uiout;
10736 const char *tuple_name;
10737
10738 switch (b->type)
10739 {
10740 case bp_watchpoint:
10741 uiout->text ("Watchpoint ");
10742 tuple_name = "wpt";
10743 break;
10744 case bp_hardware_watchpoint:
10745 uiout->text ("Hardware watchpoint ");
10746 tuple_name = "wpt";
10747 break;
10748 case bp_read_watchpoint:
10749 uiout->text ("Hardware read watchpoint ");
10750 tuple_name = "hw-rwpt";
10751 break;
10752 case bp_access_watchpoint:
10753 uiout->text ("Hardware access (read/write) watchpoint ");
10754 tuple_name = "hw-awpt";
10755 break;
10756 default:
10757 internal_error (__FILE__, __LINE__,
10758 _("Invalid hardware watchpoint type."));
10759 }
10760
10761 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10762 uiout->field_int ("number", b->number);
10763 uiout->text (": ");
10764 uiout->field_string ("exp", w->exp_string);
10765 }
10766
10767 /* Implement the "print_recreate" breakpoint_ops method for
10768 watchpoints. */
10769
10770 static void
10771 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10772 {
10773 struct watchpoint *w = (struct watchpoint *) b;
10774
10775 switch (b->type)
10776 {
10777 case bp_watchpoint:
10778 case bp_hardware_watchpoint:
10779 fprintf_unfiltered (fp, "watch");
10780 break;
10781 case bp_read_watchpoint:
10782 fprintf_unfiltered (fp, "rwatch");
10783 break;
10784 case bp_access_watchpoint:
10785 fprintf_unfiltered (fp, "awatch");
10786 break;
10787 default:
10788 internal_error (__FILE__, __LINE__,
10789 _("Invalid watchpoint type."));
10790 }
10791
10792 fprintf_unfiltered (fp, " %s", w->exp_string);
10793 print_recreate_thread (b, fp);
10794 }
10795
10796 /* Implement the "explains_signal" breakpoint_ops method for
10797 watchpoints. */
10798
10799 static int
10800 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10801 {
10802 /* A software watchpoint cannot cause a signal other than
10803 GDB_SIGNAL_TRAP. */
10804 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10805 return 0;
10806
10807 return 1;
10808 }
10809
10810 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10811
10812 static struct breakpoint_ops watchpoint_breakpoint_ops;
10813
10814 /* Implement the "insert" breakpoint_ops method for
10815 masked hardware watchpoints. */
10816
10817 static int
10818 insert_masked_watchpoint (struct bp_location *bl)
10819 {
10820 struct watchpoint *w = (struct watchpoint *) bl->owner;
10821
10822 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10823 bl->watchpoint_type);
10824 }
10825
10826 /* Implement the "remove" breakpoint_ops method for
10827 masked hardware watchpoints. */
10828
10829 static int
10830 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10831 {
10832 struct watchpoint *w = (struct watchpoint *) bl->owner;
10833
10834 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10835 bl->watchpoint_type);
10836 }
10837
10838 /* Implement the "resources_needed" breakpoint_ops method for
10839 masked hardware watchpoints. */
10840
10841 static int
10842 resources_needed_masked_watchpoint (const struct bp_location *bl)
10843 {
10844 struct watchpoint *w = (struct watchpoint *) bl->owner;
10845
10846 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10847 }
10848
10849 /* Implement the "works_in_software_mode" breakpoint_ops method for
10850 masked hardware watchpoints. */
10851
10852 static int
10853 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10854 {
10855 return 0;
10856 }
10857
10858 /* Implement the "print_it" breakpoint_ops method for
10859 masked hardware watchpoints. */
10860
10861 static enum print_stop_action
10862 print_it_masked_watchpoint (bpstat bs)
10863 {
10864 struct breakpoint *b = bs->breakpoint_at;
10865 struct ui_out *uiout = current_uiout;
10866
10867 /* Masked watchpoints have only one location. */
10868 gdb_assert (b->loc && b->loc->next == NULL);
10869
10870 annotate_watchpoint (b->number);
10871 maybe_print_thread_hit_breakpoint (uiout);
10872
10873 switch (b->type)
10874 {
10875 case bp_hardware_watchpoint:
10876 if (uiout->is_mi_like_p ())
10877 uiout->field_string
10878 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10879 break;
10880
10881 case bp_read_watchpoint:
10882 if (uiout->is_mi_like_p ())
10883 uiout->field_string
10884 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10885 break;
10886
10887 case bp_access_watchpoint:
10888 if (uiout->is_mi_like_p ())
10889 uiout->field_string
10890 ("reason",
10891 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10892 break;
10893 default:
10894 internal_error (__FILE__, __LINE__,
10895 _("Invalid hardware watchpoint type."));
10896 }
10897
10898 mention (b);
10899 uiout->text (_("\n\
10900 Check the underlying instruction at PC for the memory\n\
10901 address and value which triggered this watchpoint.\n"));
10902 uiout->text ("\n");
10903
10904 /* More than one watchpoint may have been triggered. */
10905 return PRINT_UNKNOWN;
10906 }
10907
10908 /* Implement the "print_one_detail" breakpoint_ops method for
10909 masked hardware watchpoints. */
10910
10911 static void
10912 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10913 struct ui_out *uiout)
10914 {
10915 struct watchpoint *w = (struct watchpoint *) b;
10916
10917 /* Masked watchpoints have only one location. */
10918 gdb_assert (b->loc && b->loc->next == NULL);
10919
10920 uiout->text ("\tmask ");
10921 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10922 uiout->text ("\n");
10923 }
10924
10925 /* Implement the "print_mention" breakpoint_ops method for
10926 masked hardware watchpoints. */
10927
10928 static void
10929 print_mention_masked_watchpoint (struct breakpoint *b)
10930 {
10931 struct watchpoint *w = (struct watchpoint *) b;
10932 struct ui_out *uiout = current_uiout;
10933 const char *tuple_name;
10934
10935 switch (b->type)
10936 {
10937 case bp_hardware_watchpoint:
10938 uiout->text ("Masked hardware watchpoint ");
10939 tuple_name = "wpt";
10940 break;
10941 case bp_read_watchpoint:
10942 uiout->text ("Masked hardware read watchpoint ");
10943 tuple_name = "hw-rwpt";
10944 break;
10945 case bp_access_watchpoint:
10946 uiout->text ("Masked hardware access (read/write) watchpoint ");
10947 tuple_name = "hw-awpt";
10948 break;
10949 default:
10950 internal_error (__FILE__, __LINE__,
10951 _("Invalid hardware watchpoint type."));
10952 }
10953
10954 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10955 uiout->field_int ("number", b->number);
10956 uiout->text (": ");
10957 uiout->field_string ("exp", w->exp_string);
10958 }
10959
10960 /* Implement the "print_recreate" breakpoint_ops method for
10961 masked hardware watchpoints. */
10962
10963 static void
10964 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10965 {
10966 struct watchpoint *w = (struct watchpoint *) b;
10967 char tmp[40];
10968
10969 switch (b->type)
10970 {
10971 case bp_hardware_watchpoint:
10972 fprintf_unfiltered (fp, "watch");
10973 break;
10974 case bp_read_watchpoint:
10975 fprintf_unfiltered (fp, "rwatch");
10976 break;
10977 case bp_access_watchpoint:
10978 fprintf_unfiltered (fp, "awatch");
10979 break;
10980 default:
10981 internal_error (__FILE__, __LINE__,
10982 _("Invalid hardware watchpoint type."));
10983 }
10984
10985 sprintf_vma (tmp, w->hw_wp_mask);
10986 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10987 print_recreate_thread (b, fp);
10988 }
10989
10990 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10991
10992 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10993
10994 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10995
10996 static int
10997 is_masked_watchpoint (const struct breakpoint *b)
10998 {
10999 return b->ops == &masked_watchpoint_breakpoint_ops;
11000 }
11001
11002 /* accessflag: hw_write: watch write,
11003 hw_read: watch read,
11004 hw_access: watch access (read or write) */
11005 static void
11006 watch_command_1 (const char *arg, int accessflag, int from_tty,
11007 int just_location, int internal)
11008 {
11009 struct breakpoint *b, *scope_breakpoint = NULL;
11010 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11011 struct value *val, *mark, *result;
11012 int saved_bitpos = 0, saved_bitsize = 0;
11013 const char *exp_start = NULL;
11014 const char *exp_end = NULL;
11015 const char *tok, *end_tok;
11016 int toklen = -1;
11017 const char *cond_start = NULL;
11018 const char *cond_end = NULL;
11019 enum bptype bp_type;
11020 int thread = -1;
11021 int pc = 0;
11022 /* Flag to indicate whether we are going to use masks for
11023 the hardware watchpoint. */
11024 int use_mask = 0;
11025 CORE_ADDR mask = 0;
11026 struct watchpoint *w;
11027 char *expression;
11028 struct cleanup *back_to;
11029
11030 /* Make sure that we actually have parameters to parse. */
11031 if (arg != NULL && arg[0] != '\0')
11032 {
11033 const char *value_start;
11034
11035 exp_end = arg + strlen (arg);
11036
11037 /* Look for "parameter value" pairs at the end
11038 of the arguments string. */
11039 for (tok = exp_end - 1; tok > arg; tok--)
11040 {
11041 /* Skip whitespace at the end of the argument list. */
11042 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11043 tok--;
11044
11045 /* Find the beginning of the last token.
11046 This is the value of the parameter. */
11047 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11048 tok--;
11049 value_start = tok + 1;
11050
11051 /* Skip whitespace. */
11052 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11053 tok--;
11054
11055 end_tok = tok;
11056
11057 /* Find the beginning of the second to last token.
11058 This is the parameter itself. */
11059 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11060 tok--;
11061 tok++;
11062 toklen = end_tok - tok + 1;
11063
11064 if (toklen == 6 && startswith (tok, "thread"))
11065 {
11066 struct thread_info *thr;
11067 /* At this point we've found a "thread" token, which means
11068 the user is trying to set a watchpoint that triggers
11069 only in a specific thread. */
11070 const char *endp;
11071
11072 if (thread != -1)
11073 error(_("You can specify only one thread."));
11074
11075 /* Extract the thread ID from the next token. */
11076 thr = parse_thread_id (value_start, &endp);
11077
11078 /* Check if the user provided a valid thread ID. */
11079 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11080 invalid_thread_id_error (value_start);
11081
11082 thread = thr->global_num;
11083 }
11084 else if (toklen == 4 && startswith (tok, "mask"))
11085 {
11086 /* We've found a "mask" token, which means the user wants to
11087 create a hardware watchpoint that is going to have the mask
11088 facility. */
11089 struct value *mask_value, *mark;
11090
11091 if (use_mask)
11092 error(_("You can specify only one mask."));
11093
11094 use_mask = just_location = 1;
11095
11096 mark = value_mark ();
11097 mask_value = parse_to_comma_and_eval (&value_start);
11098 mask = value_as_address (mask_value);
11099 value_free_to_mark (mark);
11100 }
11101 else
11102 /* We didn't recognize what we found. We should stop here. */
11103 break;
11104
11105 /* Truncate the string and get rid of the "parameter value" pair before
11106 the arguments string is parsed by the parse_exp_1 function. */
11107 exp_end = tok;
11108 }
11109 }
11110 else
11111 exp_end = arg;
11112
11113 /* Parse the rest of the arguments. From here on out, everything
11114 is in terms of a newly allocated string instead of the original
11115 ARG. */
11116 innermost_block = NULL;
11117 expression = savestring (arg, exp_end - arg);
11118 back_to = make_cleanup (xfree, expression);
11119 exp_start = arg = expression;
11120 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11121 exp_end = arg;
11122 /* Remove trailing whitespace from the expression before saving it.
11123 This makes the eventual display of the expression string a bit
11124 prettier. */
11125 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11126 --exp_end;
11127
11128 /* Checking if the expression is not constant. */
11129 if (watchpoint_exp_is_const (exp.get ()))
11130 {
11131 int len;
11132
11133 len = exp_end - exp_start;
11134 while (len > 0 && isspace (exp_start[len - 1]))
11135 len--;
11136 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11137 }
11138
11139 exp_valid_block = innermost_block;
11140 mark = value_mark ();
11141 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11142
11143 if (val != NULL && just_location)
11144 {
11145 saved_bitpos = value_bitpos (val);
11146 saved_bitsize = value_bitsize (val);
11147 }
11148
11149 if (just_location)
11150 {
11151 int ret;
11152
11153 exp_valid_block = NULL;
11154 val = value_addr (result);
11155 release_value (val);
11156 value_free_to_mark (mark);
11157
11158 if (use_mask)
11159 {
11160 ret = target_masked_watch_num_registers (value_as_address (val),
11161 mask);
11162 if (ret == -1)
11163 error (_("This target does not support masked watchpoints."));
11164 else if (ret == -2)
11165 error (_("Invalid mask or memory region."));
11166 }
11167 }
11168 else if (val != NULL)
11169 release_value (val);
11170
11171 tok = skip_spaces_const (arg);
11172 end_tok = skip_to_space_const (tok);
11173
11174 toklen = end_tok - tok;
11175 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11176 {
11177 innermost_block = NULL;
11178 tok = cond_start = end_tok + 1;
11179 parse_exp_1 (&tok, 0, 0, 0);
11180
11181 /* The watchpoint expression may not be local, but the condition
11182 may still be. E.g.: `watch global if local > 0'. */
11183 cond_exp_valid_block = innermost_block;
11184
11185 cond_end = tok;
11186 }
11187 if (*tok)
11188 error (_("Junk at end of command."));
11189
11190 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11191
11192 /* Save this because create_internal_breakpoint below invalidates
11193 'wp_frame'. */
11194 frame_id watchpoint_frame = get_frame_id (wp_frame);
11195
11196 /* If the expression is "local", then set up a "watchpoint scope"
11197 breakpoint at the point where we've left the scope of the watchpoint
11198 expression. Create the scope breakpoint before the watchpoint, so
11199 that we will encounter it first in bpstat_stop_status. */
11200 if (exp_valid_block != NULL && wp_frame != NULL)
11201 {
11202 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11203
11204 if (frame_id_p (caller_frame_id))
11205 {
11206 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11207 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11208
11209 scope_breakpoint
11210 = create_internal_breakpoint (caller_arch, caller_pc,
11211 bp_watchpoint_scope,
11212 &momentary_breakpoint_ops);
11213
11214 /* create_internal_breakpoint could invalidate WP_FRAME. */
11215 wp_frame = NULL;
11216
11217 scope_breakpoint->enable_state = bp_enabled;
11218
11219 /* Automatically delete the breakpoint when it hits. */
11220 scope_breakpoint->disposition = disp_del;
11221
11222 /* Only break in the proper frame (help with recursion). */
11223 scope_breakpoint->frame_id = caller_frame_id;
11224
11225 /* Set the address at which we will stop. */
11226 scope_breakpoint->loc->gdbarch = caller_arch;
11227 scope_breakpoint->loc->requested_address = caller_pc;
11228 scope_breakpoint->loc->address
11229 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11230 scope_breakpoint->loc->requested_address,
11231 scope_breakpoint->type);
11232 }
11233 }
11234
11235 /* Now set up the breakpoint. We create all watchpoints as hardware
11236 watchpoints here even if hardware watchpoints are turned off, a call
11237 to update_watchpoint later in this function will cause the type to
11238 drop back to bp_watchpoint (software watchpoint) if required. */
11239
11240 if (accessflag == hw_read)
11241 bp_type = bp_read_watchpoint;
11242 else if (accessflag == hw_access)
11243 bp_type = bp_access_watchpoint;
11244 else
11245 bp_type = bp_hardware_watchpoint;
11246
11247 w = new watchpoint ();
11248 b = &w->base;
11249 if (use_mask)
11250 init_raw_breakpoint_without_location (b, NULL, bp_type,
11251 &masked_watchpoint_breakpoint_ops);
11252 else
11253 init_raw_breakpoint_without_location (b, NULL, bp_type,
11254 &watchpoint_breakpoint_ops);
11255 b->thread = thread;
11256 b->disposition = disp_donttouch;
11257 b->pspace = current_program_space;
11258 w->exp = std::move (exp);
11259 w->exp_valid_block = exp_valid_block;
11260 w->cond_exp_valid_block = cond_exp_valid_block;
11261 if (just_location)
11262 {
11263 struct type *t = value_type (val);
11264 CORE_ADDR addr = value_as_address (val);
11265
11266 w->exp_string_reparse
11267 = current_language->la_watch_location_expression (t, addr).release ();
11268
11269 w->exp_string = xstrprintf ("-location %.*s",
11270 (int) (exp_end - exp_start), exp_start);
11271 }
11272 else
11273 w->exp_string = savestring (exp_start, exp_end - exp_start);
11274
11275 if (use_mask)
11276 {
11277 w->hw_wp_mask = mask;
11278 }
11279 else
11280 {
11281 w->val = val;
11282 w->val_bitpos = saved_bitpos;
11283 w->val_bitsize = saved_bitsize;
11284 w->val_valid = 1;
11285 }
11286
11287 if (cond_start)
11288 b->cond_string = savestring (cond_start, cond_end - cond_start);
11289 else
11290 b->cond_string = 0;
11291
11292 if (frame_id_p (watchpoint_frame))
11293 {
11294 w->watchpoint_frame = watchpoint_frame;
11295 w->watchpoint_thread = inferior_ptid;
11296 }
11297 else
11298 {
11299 w->watchpoint_frame = null_frame_id;
11300 w->watchpoint_thread = null_ptid;
11301 }
11302
11303 if (scope_breakpoint != NULL)
11304 {
11305 /* The scope breakpoint is related to the watchpoint. We will
11306 need to act on them together. */
11307 b->related_breakpoint = scope_breakpoint;
11308 scope_breakpoint->related_breakpoint = b;
11309 }
11310
11311 if (!just_location)
11312 value_free_to_mark (mark);
11313
11314 TRY
11315 {
11316 /* Finally update the new watchpoint. This creates the locations
11317 that should be inserted. */
11318 update_watchpoint (w, 1);
11319 }
11320 CATCH (e, RETURN_MASK_ALL)
11321 {
11322 delete_breakpoint (b);
11323 throw_exception (e);
11324 }
11325 END_CATCH
11326
11327 install_breakpoint (internal, b, 1);
11328 do_cleanups (back_to);
11329 }
11330
11331 /* Return count of debug registers needed to watch the given expression.
11332 If the watchpoint cannot be handled in hardware return zero. */
11333
11334 static int
11335 can_use_hardware_watchpoint (struct value *v)
11336 {
11337 int found_memory_cnt = 0;
11338 struct value *head = v;
11339
11340 /* Did the user specifically forbid us to use hardware watchpoints? */
11341 if (!can_use_hw_watchpoints)
11342 return 0;
11343
11344 /* Make sure that the value of the expression depends only upon
11345 memory contents, and values computed from them within GDB. If we
11346 find any register references or function calls, we can't use a
11347 hardware watchpoint.
11348
11349 The idea here is that evaluating an expression generates a series
11350 of values, one holding the value of every subexpression. (The
11351 expression a*b+c has five subexpressions: a, b, a*b, c, and
11352 a*b+c.) GDB's values hold almost enough information to establish
11353 the criteria given above --- they identify memory lvalues,
11354 register lvalues, computed values, etcetera. So we can evaluate
11355 the expression, and then scan the chain of values that leaves
11356 behind to decide whether we can detect any possible change to the
11357 expression's final value using only hardware watchpoints.
11358
11359 However, I don't think that the values returned by inferior
11360 function calls are special in any way. So this function may not
11361 notice that an expression involving an inferior function call
11362 can't be watched with hardware watchpoints. FIXME. */
11363 for (; v; v = value_next (v))
11364 {
11365 if (VALUE_LVAL (v) == lval_memory)
11366 {
11367 if (v != head && value_lazy (v))
11368 /* A lazy memory lvalue in the chain is one that GDB never
11369 needed to fetch; we either just used its address (e.g.,
11370 `a' in `a.b') or we never needed it at all (e.g., `a'
11371 in `a,b'). This doesn't apply to HEAD; if that is
11372 lazy then it was not readable, but watch it anyway. */
11373 ;
11374 else
11375 {
11376 /* Ahh, memory we actually used! Check if we can cover
11377 it with hardware watchpoints. */
11378 struct type *vtype = check_typedef (value_type (v));
11379
11380 /* We only watch structs and arrays if user asked for it
11381 explicitly, never if they just happen to appear in a
11382 middle of some value chain. */
11383 if (v == head
11384 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11385 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11386 {
11387 CORE_ADDR vaddr = value_address (v);
11388 int len;
11389 int num_regs;
11390
11391 len = (target_exact_watchpoints
11392 && is_scalar_type_recursive (vtype))?
11393 1 : TYPE_LENGTH (value_type (v));
11394
11395 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11396 if (!num_regs)
11397 return 0;
11398 else
11399 found_memory_cnt += num_regs;
11400 }
11401 }
11402 }
11403 else if (VALUE_LVAL (v) != not_lval
11404 && deprecated_value_modifiable (v) == 0)
11405 return 0; /* These are values from the history (e.g., $1). */
11406 else if (VALUE_LVAL (v) == lval_register)
11407 return 0; /* Cannot watch a register with a HW watchpoint. */
11408 }
11409
11410 /* The expression itself looks suitable for using a hardware
11411 watchpoint, but give the target machine a chance to reject it. */
11412 return found_memory_cnt;
11413 }
11414
11415 void
11416 watch_command_wrapper (char *arg, int from_tty, int internal)
11417 {
11418 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11419 }
11420
11421 /* A helper function that looks for the "-location" argument and then
11422 calls watch_command_1. */
11423
11424 static void
11425 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11426 {
11427 int just_location = 0;
11428
11429 if (arg
11430 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11431 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11432 {
11433 arg = skip_spaces (arg);
11434 just_location = 1;
11435 }
11436
11437 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11438 }
11439
11440 static void
11441 watch_command (char *arg, int from_tty)
11442 {
11443 watch_maybe_just_location (arg, hw_write, from_tty);
11444 }
11445
11446 void
11447 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11448 {
11449 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11450 }
11451
11452 static void
11453 rwatch_command (char *arg, int from_tty)
11454 {
11455 watch_maybe_just_location (arg, hw_read, from_tty);
11456 }
11457
11458 void
11459 awatch_command_wrapper (char *arg, int from_tty, int internal)
11460 {
11461 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11462 }
11463
11464 static void
11465 awatch_command (char *arg, int from_tty)
11466 {
11467 watch_maybe_just_location (arg, hw_access, from_tty);
11468 }
11469 \f
11470
11471 /* Data for the FSM that manages the until(location)/advance commands
11472 in infcmd.c. Here because it uses the mechanisms of
11473 breakpoints. */
11474
11475 struct until_break_fsm
11476 {
11477 /* The base class. */
11478 struct thread_fsm thread_fsm;
11479
11480 /* The thread that as current when the command was executed. */
11481 int thread;
11482
11483 /* The breakpoint set at the destination location. */
11484 struct breakpoint *location_breakpoint;
11485
11486 /* Breakpoint set at the return address in the caller frame. May be
11487 NULL. */
11488 struct breakpoint *caller_breakpoint;
11489 };
11490
11491 static void until_break_fsm_clean_up (struct thread_fsm *self,
11492 struct thread_info *thread);
11493 static int until_break_fsm_should_stop (struct thread_fsm *self,
11494 struct thread_info *thread);
11495 static enum async_reply_reason
11496 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11497
11498 /* until_break_fsm's vtable. */
11499
11500 static struct thread_fsm_ops until_break_fsm_ops =
11501 {
11502 NULL, /* dtor */
11503 until_break_fsm_clean_up,
11504 until_break_fsm_should_stop,
11505 NULL, /* return_value */
11506 until_break_fsm_async_reply_reason,
11507 };
11508
11509 /* Allocate a new until_break_command_fsm. */
11510
11511 static struct until_break_fsm *
11512 new_until_break_fsm (struct interp *cmd_interp, int thread,
11513 struct breakpoint *location_breakpoint,
11514 struct breakpoint *caller_breakpoint)
11515 {
11516 struct until_break_fsm *sm;
11517
11518 sm = XCNEW (struct until_break_fsm);
11519 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11520
11521 sm->thread = thread;
11522 sm->location_breakpoint = location_breakpoint;
11523 sm->caller_breakpoint = caller_breakpoint;
11524
11525 return sm;
11526 }
11527
11528 /* Implementation of the 'should_stop' FSM method for the
11529 until(location)/advance commands. */
11530
11531 static int
11532 until_break_fsm_should_stop (struct thread_fsm *self,
11533 struct thread_info *tp)
11534 {
11535 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11536
11537 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11538 sm->location_breakpoint) != NULL
11539 || (sm->caller_breakpoint != NULL
11540 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11541 sm->caller_breakpoint) != NULL))
11542 thread_fsm_set_finished (self);
11543
11544 return 1;
11545 }
11546
11547 /* Implementation of the 'clean_up' FSM method for the
11548 until(location)/advance commands. */
11549
11550 static void
11551 until_break_fsm_clean_up (struct thread_fsm *self,
11552 struct thread_info *thread)
11553 {
11554 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11555
11556 /* Clean up our temporary breakpoints. */
11557 if (sm->location_breakpoint != NULL)
11558 {
11559 delete_breakpoint (sm->location_breakpoint);
11560 sm->location_breakpoint = NULL;
11561 }
11562 if (sm->caller_breakpoint != NULL)
11563 {
11564 delete_breakpoint (sm->caller_breakpoint);
11565 sm->caller_breakpoint = NULL;
11566 }
11567 delete_longjmp_breakpoint (sm->thread);
11568 }
11569
11570 /* Implementation of the 'async_reply_reason' FSM method for the
11571 until(location)/advance commands. */
11572
11573 static enum async_reply_reason
11574 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11575 {
11576 return EXEC_ASYNC_LOCATION_REACHED;
11577 }
11578
11579 void
11580 until_break_command (char *arg, int from_tty, int anywhere)
11581 {
11582 struct symtabs_and_lines sals;
11583 struct symtab_and_line sal;
11584 struct frame_info *frame;
11585 struct gdbarch *frame_gdbarch;
11586 struct frame_id stack_frame_id;
11587 struct frame_id caller_frame_id;
11588 struct breakpoint *location_breakpoint;
11589 struct breakpoint *caller_breakpoint = NULL;
11590 struct cleanup *old_chain;
11591 int thread;
11592 struct thread_info *tp;
11593 struct until_break_fsm *sm;
11594
11595 clear_proceed_status (0);
11596
11597 /* Set a breakpoint where the user wants it and at return from
11598 this function. */
11599
11600 event_location_up location = string_to_event_location (&arg, current_language);
11601
11602 if (last_displayed_sal_is_valid ())
11603 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11604 get_last_displayed_symtab (),
11605 get_last_displayed_line ());
11606 else
11607 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11608 NULL, (struct symtab *) NULL, 0);
11609
11610 if (sals.nelts != 1)
11611 error (_("Couldn't get information on specified line."));
11612
11613 sal = sals.sals[0];
11614 xfree (sals.sals); /* malloc'd, so freed. */
11615
11616 if (*arg)
11617 error (_("Junk at end of arguments."));
11618
11619 resolve_sal_pc (&sal);
11620
11621 tp = inferior_thread ();
11622 thread = tp->global_num;
11623
11624 old_chain = make_cleanup (null_cleanup, NULL);
11625
11626 /* Note linespec handling above invalidates the frame chain.
11627 Installing a breakpoint also invalidates the frame chain (as it
11628 may need to switch threads), so do any frame handling before
11629 that. */
11630
11631 frame = get_selected_frame (NULL);
11632 frame_gdbarch = get_frame_arch (frame);
11633 stack_frame_id = get_stack_frame_id (frame);
11634 caller_frame_id = frame_unwind_caller_id (frame);
11635
11636 /* Keep within the current frame, or in frames called by the current
11637 one. */
11638
11639 if (frame_id_p (caller_frame_id))
11640 {
11641 struct symtab_and_line sal2;
11642 struct gdbarch *caller_gdbarch;
11643
11644 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11645 sal2.pc = frame_unwind_caller_pc (frame);
11646 caller_gdbarch = frame_unwind_caller_arch (frame);
11647 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11648 sal2,
11649 caller_frame_id,
11650 bp_until);
11651 make_cleanup_delete_breakpoint (caller_breakpoint);
11652
11653 set_longjmp_breakpoint (tp, caller_frame_id);
11654 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11655 }
11656
11657 /* set_momentary_breakpoint could invalidate FRAME. */
11658 frame = NULL;
11659
11660 if (anywhere)
11661 /* If the user told us to continue until a specified location,
11662 we don't specify a frame at which we need to stop. */
11663 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11664 null_frame_id, bp_until);
11665 else
11666 /* Otherwise, specify the selected frame, because we want to stop
11667 only at the very same frame. */
11668 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11669 stack_frame_id, bp_until);
11670 make_cleanup_delete_breakpoint (location_breakpoint);
11671
11672 sm = new_until_break_fsm (command_interp (), tp->global_num,
11673 location_breakpoint, caller_breakpoint);
11674 tp->thread_fsm = &sm->thread_fsm;
11675
11676 discard_cleanups (old_chain);
11677
11678 proceed (-1, GDB_SIGNAL_DEFAULT);
11679 }
11680
11681 /* This function attempts to parse an optional "if <cond>" clause
11682 from the arg string. If one is not found, it returns NULL.
11683
11684 Else, it returns a pointer to the condition string. (It does not
11685 attempt to evaluate the string against a particular block.) And,
11686 it updates arg to point to the first character following the parsed
11687 if clause in the arg string. */
11688
11689 const char *
11690 ep_parse_optional_if_clause (const char **arg)
11691 {
11692 const char *cond_string;
11693
11694 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11695 return NULL;
11696
11697 /* Skip the "if" keyword. */
11698 (*arg) += 2;
11699
11700 /* Skip any extra leading whitespace, and record the start of the
11701 condition string. */
11702 *arg = skip_spaces_const (*arg);
11703 cond_string = *arg;
11704
11705 /* Assume that the condition occupies the remainder of the arg
11706 string. */
11707 (*arg) += strlen (cond_string);
11708
11709 return cond_string;
11710 }
11711
11712 /* Commands to deal with catching events, such as signals, exceptions,
11713 process start/exit, etc. */
11714
11715 typedef enum
11716 {
11717 catch_fork_temporary, catch_vfork_temporary,
11718 catch_fork_permanent, catch_vfork_permanent
11719 }
11720 catch_fork_kind;
11721
11722 static void
11723 catch_fork_command_1 (char *arg_entry, int from_tty,
11724 struct cmd_list_element *command)
11725 {
11726 const char *arg = arg_entry;
11727 struct gdbarch *gdbarch = get_current_arch ();
11728 const char *cond_string = NULL;
11729 catch_fork_kind fork_kind;
11730 int tempflag;
11731
11732 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11733 tempflag = (fork_kind == catch_fork_temporary
11734 || fork_kind == catch_vfork_temporary);
11735
11736 if (!arg)
11737 arg = "";
11738 arg = skip_spaces_const (arg);
11739
11740 /* The allowed syntax is:
11741 catch [v]fork
11742 catch [v]fork if <cond>
11743
11744 First, check if there's an if clause. */
11745 cond_string = ep_parse_optional_if_clause (&arg);
11746
11747 if ((*arg != '\0') && !isspace (*arg))
11748 error (_("Junk at end of arguments."));
11749
11750 /* If this target supports it, create a fork or vfork catchpoint
11751 and enable reporting of such events. */
11752 switch (fork_kind)
11753 {
11754 case catch_fork_temporary:
11755 case catch_fork_permanent:
11756 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11757 &catch_fork_breakpoint_ops);
11758 break;
11759 case catch_vfork_temporary:
11760 case catch_vfork_permanent:
11761 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11762 &catch_vfork_breakpoint_ops);
11763 break;
11764 default:
11765 error (_("unsupported or unknown fork kind; cannot catch it"));
11766 break;
11767 }
11768 }
11769
11770 static void
11771 catch_exec_command_1 (char *arg_entry, int from_tty,
11772 struct cmd_list_element *command)
11773 {
11774 const char *arg = arg_entry;
11775 struct exec_catchpoint *c;
11776 struct gdbarch *gdbarch = get_current_arch ();
11777 int tempflag;
11778 const char *cond_string = NULL;
11779
11780 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11781
11782 if (!arg)
11783 arg = "";
11784 arg = skip_spaces_const (arg);
11785
11786 /* The allowed syntax is:
11787 catch exec
11788 catch exec if <cond>
11789
11790 First, check if there's an if clause. */
11791 cond_string = ep_parse_optional_if_clause (&arg);
11792
11793 if ((*arg != '\0') && !isspace (*arg))
11794 error (_("Junk at end of arguments."));
11795
11796 c = new exec_catchpoint ();
11797 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11798 &catch_exec_breakpoint_ops);
11799 c->exec_pathname = NULL;
11800
11801 install_breakpoint (0, &c->base, 1);
11802 }
11803
11804 void
11805 init_ada_exception_breakpoint (struct breakpoint *b,
11806 struct gdbarch *gdbarch,
11807 struct symtab_and_line sal,
11808 char *addr_string,
11809 const struct breakpoint_ops *ops,
11810 int tempflag,
11811 int enabled,
11812 int from_tty)
11813 {
11814 if (from_tty)
11815 {
11816 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11817 if (!loc_gdbarch)
11818 loc_gdbarch = gdbarch;
11819
11820 describe_other_breakpoints (loc_gdbarch,
11821 sal.pspace, sal.pc, sal.section, -1);
11822 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11823 version for exception catchpoints, because two catchpoints
11824 used for different exception names will use the same address.
11825 In this case, a "breakpoint ... also set at..." warning is
11826 unproductive. Besides, the warning phrasing is also a bit
11827 inappropriate, we should use the word catchpoint, and tell
11828 the user what type of catchpoint it is. The above is good
11829 enough for now, though. */
11830 }
11831
11832 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11833
11834 b->enable_state = enabled ? bp_enabled : bp_disabled;
11835 b->disposition = tempflag ? disp_del : disp_donttouch;
11836 b->location = string_to_event_location (&addr_string,
11837 language_def (language_ada));
11838 b->language = language_ada;
11839 }
11840
11841 static void
11842 catch_command (char *arg, int from_tty)
11843 {
11844 error (_("Catch requires an event name."));
11845 }
11846 \f
11847
11848 static void
11849 tcatch_command (char *arg, int from_tty)
11850 {
11851 error (_("Catch requires an event name."));
11852 }
11853
11854 /* A qsort comparison function that sorts breakpoints in order. */
11855
11856 static int
11857 compare_breakpoints (const void *a, const void *b)
11858 {
11859 const breakpoint_p *ba = (const breakpoint_p *) a;
11860 uintptr_t ua = (uintptr_t) *ba;
11861 const breakpoint_p *bb = (const breakpoint_p *) b;
11862 uintptr_t ub = (uintptr_t) *bb;
11863
11864 if ((*ba)->number < (*bb)->number)
11865 return -1;
11866 else if ((*ba)->number > (*bb)->number)
11867 return 1;
11868
11869 /* Now sort by address, in case we see, e..g, two breakpoints with
11870 the number 0. */
11871 if (ua < ub)
11872 return -1;
11873 return ua > ub ? 1 : 0;
11874 }
11875
11876 /* Delete breakpoints by address or line. */
11877
11878 static void
11879 clear_command (char *arg, int from_tty)
11880 {
11881 struct breakpoint *b, *prev;
11882 VEC(breakpoint_p) *found = 0;
11883 int ix;
11884 int default_match;
11885 struct symtabs_and_lines sals;
11886 struct symtab_and_line sal;
11887 int i;
11888 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11889
11890 if (arg)
11891 {
11892 sals = decode_line_with_current_source (arg,
11893 (DECODE_LINE_FUNFIRSTLINE
11894 | DECODE_LINE_LIST_MODE));
11895 make_cleanup (xfree, sals.sals);
11896 default_match = 0;
11897 }
11898 else
11899 {
11900 sals.sals = XNEW (struct symtab_and_line);
11901 make_cleanup (xfree, sals.sals);
11902 init_sal (&sal); /* Initialize to zeroes. */
11903
11904 /* Set sal's line, symtab, pc, and pspace to the values
11905 corresponding to the last call to print_frame_info. If the
11906 codepoint is not valid, this will set all the fields to 0. */
11907 get_last_displayed_sal (&sal);
11908 if (sal.symtab == 0)
11909 error (_("No source file specified."));
11910
11911 sals.sals[0] = sal;
11912 sals.nelts = 1;
11913
11914 default_match = 1;
11915 }
11916
11917 /* We don't call resolve_sal_pc here. That's not as bad as it
11918 seems, because all existing breakpoints typically have both
11919 file/line and pc set. So, if clear is given file/line, we can
11920 match this to existing breakpoint without obtaining pc at all.
11921
11922 We only support clearing given the address explicitly
11923 present in breakpoint table. Say, we've set breakpoint
11924 at file:line. There were several PC values for that file:line,
11925 due to optimization, all in one block.
11926
11927 We've picked one PC value. If "clear" is issued with another
11928 PC corresponding to the same file:line, the breakpoint won't
11929 be cleared. We probably can still clear the breakpoint, but
11930 since the other PC value is never presented to user, user
11931 can only find it by guessing, and it does not seem important
11932 to support that. */
11933
11934 /* For each line spec given, delete bps which correspond to it. Do
11935 it in two passes, solely to preserve the current behavior that
11936 from_tty is forced true if we delete more than one
11937 breakpoint. */
11938
11939 found = NULL;
11940 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11941 for (i = 0; i < sals.nelts; i++)
11942 {
11943 const char *sal_fullname;
11944
11945 /* If exact pc given, clear bpts at that pc.
11946 If line given (pc == 0), clear all bpts on specified line.
11947 If defaulting, clear all bpts on default line
11948 or at default pc.
11949
11950 defaulting sal.pc != 0 tests to do
11951
11952 0 1 pc
11953 1 1 pc _and_ line
11954 0 0 line
11955 1 0 <can't happen> */
11956
11957 sal = sals.sals[i];
11958 sal_fullname = (sal.symtab == NULL
11959 ? NULL : symtab_to_fullname (sal.symtab));
11960
11961 /* Find all matching breakpoints and add them to 'found'. */
11962 ALL_BREAKPOINTS (b)
11963 {
11964 int match = 0;
11965 /* Are we going to delete b? */
11966 if (b->type != bp_none && !is_watchpoint (b))
11967 {
11968 struct bp_location *loc = b->loc;
11969 for (; loc; loc = loc->next)
11970 {
11971 /* If the user specified file:line, don't allow a PC
11972 match. This matches historical gdb behavior. */
11973 int pc_match = (!sal.explicit_line
11974 && sal.pc
11975 && (loc->pspace == sal.pspace)
11976 && (loc->address == sal.pc)
11977 && (!section_is_overlay (loc->section)
11978 || loc->section == sal.section));
11979 int line_match = 0;
11980
11981 if ((default_match || sal.explicit_line)
11982 && loc->symtab != NULL
11983 && sal_fullname != NULL
11984 && sal.pspace == loc->pspace
11985 && loc->line_number == sal.line
11986 && filename_cmp (symtab_to_fullname (loc->symtab),
11987 sal_fullname) == 0)
11988 line_match = 1;
11989
11990 if (pc_match || line_match)
11991 {
11992 match = 1;
11993 break;
11994 }
11995 }
11996 }
11997
11998 if (match)
11999 VEC_safe_push(breakpoint_p, found, b);
12000 }
12001 }
12002
12003 /* Now go thru the 'found' chain and delete them. */
12004 if (VEC_empty(breakpoint_p, found))
12005 {
12006 if (arg)
12007 error (_("No breakpoint at %s."), arg);
12008 else
12009 error (_("No breakpoint at this line."));
12010 }
12011
12012 /* Remove duplicates from the vec. */
12013 qsort (VEC_address (breakpoint_p, found),
12014 VEC_length (breakpoint_p, found),
12015 sizeof (breakpoint_p),
12016 compare_breakpoints);
12017 prev = VEC_index (breakpoint_p, found, 0);
12018 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12019 {
12020 if (b == prev)
12021 {
12022 VEC_ordered_remove (breakpoint_p, found, ix);
12023 --ix;
12024 }
12025 }
12026
12027 if (VEC_length(breakpoint_p, found) > 1)
12028 from_tty = 1; /* Always report if deleted more than one. */
12029 if (from_tty)
12030 {
12031 if (VEC_length(breakpoint_p, found) == 1)
12032 printf_unfiltered (_("Deleted breakpoint "));
12033 else
12034 printf_unfiltered (_("Deleted breakpoints "));
12035 }
12036
12037 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12038 {
12039 if (from_tty)
12040 printf_unfiltered ("%d ", b->number);
12041 delete_breakpoint (b);
12042 }
12043 if (from_tty)
12044 putchar_unfiltered ('\n');
12045
12046 do_cleanups (cleanups);
12047 }
12048 \f
12049 /* Delete breakpoint in BS if they are `delete' breakpoints and
12050 all breakpoints that are marked for deletion, whether hit or not.
12051 This is called after any breakpoint is hit, or after errors. */
12052
12053 void
12054 breakpoint_auto_delete (bpstat bs)
12055 {
12056 struct breakpoint *b, *b_tmp;
12057
12058 for (; bs; bs = bs->next)
12059 if (bs->breakpoint_at
12060 && bs->breakpoint_at->disposition == disp_del
12061 && bs->stop)
12062 delete_breakpoint (bs->breakpoint_at);
12063
12064 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12065 {
12066 if (b->disposition == disp_del_at_next_stop)
12067 delete_breakpoint (b);
12068 }
12069 }
12070
12071 /* A comparison function for bp_location AP and BP being interfaced to
12072 qsort. Sort elements primarily by their ADDRESS (no matter what
12073 does breakpoint_address_is_meaningful say for its OWNER),
12074 secondarily by ordering first permanent elements and
12075 terciarily just ensuring the array is sorted stable way despite
12076 qsort being an unstable algorithm. */
12077
12078 static int
12079 bp_locations_compare (const void *ap, const void *bp)
12080 {
12081 const struct bp_location *a = *(const struct bp_location **) ap;
12082 const struct bp_location *b = *(const struct bp_location **) bp;
12083
12084 if (a->address != b->address)
12085 return (a->address > b->address) - (a->address < b->address);
12086
12087 /* Sort locations at the same address by their pspace number, keeping
12088 locations of the same inferior (in a multi-inferior environment)
12089 grouped. */
12090
12091 if (a->pspace->num != b->pspace->num)
12092 return ((a->pspace->num > b->pspace->num)
12093 - (a->pspace->num < b->pspace->num));
12094
12095 /* Sort permanent breakpoints first. */
12096 if (a->permanent != b->permanent)
12097 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12098
12099 /* Make the internal GDB representation stable across GDB runs
12100 where A and B memory inside GDB can differ. Breakpoint locations of
12101 the same type at the same address can be sorted in arbitrary order. */
12102
12103 if (a->owner->number != b->owner->number)
12104 return ((a->owner->number > b->owner->number)
12105 - (a->owner->number < b->owner->number));
12106
12107 return (a > b) - (a < b);
12108 }
12109
12110 /* Set bp_locations_placed_address_before_address_max and
12111 bp_locations_shadow_len_after_address_max according to the current
12112 content of the bp_locations array. */
12113
12114 static void
12115 bp_locations_target_extensions_update (void)
12116 {
12117 struct bp_location *bl, **blp_tmp;
12118
12119 bp_locations_placed_address_before_address_max = 0;
12120 bp_locations_shadow_len_after_address_max = 0;
12121
12122 ALL_BP_LOCATIONS (bl, blp_tmp)
12123 {
12124 CORE_ADDR start, end, addr;
12125
12126 if (!bp_location_has_shadow (bl))
12127 continue;
12128
12129 start = bl->target_info.placed_address;
12130 end = start + bl->target_info.shadow_len;
12131
12132 gdb_assert (bl->address >= start);
12133 addr = bl->address - start;
12134 if (addr > bp_locations_placed_address_before_address_max)
12135 bp_locations_placed_address_before_address_max = addr;
12136
12137 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12138
12139 gdb_assert (bl->address < end);
12140 addr = end - bl->address;
12141 if (addr > bp_locations_shadow_len_after_address_max)
12142 bp_locations_shadow_len_after_address_max = addr;
12143 }
12144 }
12145
12146 /* Download tracepoint locations if they haven't been. */
12147
12148 static void
12149 download_tracepoint_locations (void)
12150 {
12151 struct breakpoint *b;
12152 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12153
12154 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12155
12156 ALL_TRACEPOINTS (b)
12157 {
12158 struct bp_location *bl;
12159 struct tracepoint *t;
12160 int bp_location_downloaded = 0;
12161
12162 if ((b->type == bp_fast_tracepoint
12163 ? !may_insert_fast_tracepoints
12164 : !may_insert_tracepoints))
12165 continue;
12166
12167 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12168 {
12169 if (target_can_download_tracepoint ())
12170 can_download_tracepoint = TRIBOOL_TRUE;
12171 else
12172 can_download_tracepoint = TRIBOOL_FALSE;
12173 }
12174
12175 if (can_download_tracepoint == TRIBOOL_FALSE)
12176 break;
12177
12178 for (bl = b->loc; bl; bl = bl->next)
12179 {
12180 /* In tracepoint, locations are _never_ duplicated, so
12181 should_be_inserted is equivalent to
12182 unduplicated_should_be_inserted. */
12183 if (!should_be_inserted (bl) || bl->inserted)
12184 continue;
12185
12186 switch_to_program_space_and_thread (bl->pspace);
12187
12188 target_download_tracepoint (bl);
12189
12190 bl->inserted = 1;
12191 bp_location_downloaded = 1;
12192 }
12193 t = (struct tracepoint *) b;
12194 t->number_on_target = b->number;
12195 if (bp_location_downloaded)
12196 observer_notify_breakpoint_modified (b);
12197 }
12198 }
12199
12200 /* Swap the insertion/duplication state between two locations. */
12201
12202 static void
12203 swap_insertion (struct bp_location *left, struct bp_location *right)
12204 {
12205 const int left_inserted = left->inserted;
12206 const int left_duplicate = left->duplicate;
12207 const int left_needs_update = left->needs_update;
12208 const struct bp_target_info left_target_info = left->target_info;
12209
12210 /* Locations of tracepoints can never be duplicated. */
12211 if (is_tracepoint (left->owner))
12212 gdb_assert (!left->duplicate);
12213 if (is_tracepoint (right->owner))
12214 gdb_assert (!right->duplicate);
12215
12216 left->inserted = right->inserted;
12217 left->duplicate = right->duplicate;
12218 left->needs_update = right->needs_update;
12219 left->target_info = right->target_info;
12220 right->inserted = left_inserted;
12221 right->duplicate = left_duplicate;
12222 right->needs_update = left_needs_update;
12223 right->target_info = left_target_info;
12224 }
12225
12226 /* Force the re-insertion of the locations at ADDRESS. This is called
12227 once a new/deleted/modified duplicate location is found and we are evaluating
12228 conditions on the target's side. Such conditions need to be updated on
12229 the target. */
12230
12231 static void
12232 force_breakpoint_reinsertion (struct bp_location *bl)
12233 {
12234 struct bp_location **locp = NULL, **loc2p;
12235 struct bp_location *loc;
12236 CORE_ADDR address = 0;
12237 int pspace_num;
12238
12239 address = bl->address;
12240 pspace_num = bl->pspace->num;
12241
12242 /* This is only meaningful if the target is
12243 evaluating conditions and if the user has
12244 opted for condition evaluation on the target's
12245 side. */
12246 if (gdb_evaluates_breakpoint_condition_p ()
12247 || !target_supports_evaluation_of_breakpoint_conditions ())
12248 return;
12249
12250 /* Flag all breakpoint locations with this address and
12251 the same program space as the location
12252 as "its condition has changed". We need to
12253 update the conditions on the target's side. */
12254 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12255 {
12256 loc = *loc2p;
12257
12258 if (!is_breakpoint (loc->owner)
12259 || pspace_num != loc->pspace->num)
12260 continue;
12261
12262 /* Flag the location appropriately. We use a different state to
12263 let everyone know that we already updated the set of locations
12264 with addr bl->address and program space bl->pspace. This is so
12265 we don't have to keep calling these functions just to mark locations
12266 that have already been marked. */
12267 loc->condition_changed = condition_updated;
12268
12269 /* Free the agent expression bytecode as well. We will compute
12270 it later on. */
12271 loc->cond_bytecode.reset ();
12272 }
12273 }
12274 /* Called whether new breakpoints are created, or existing breakpoints
12275 deleted, to update the global location list and recompute which
12276 locations are duplicate of which.
12277
12278 The INSERT_MODE flag determines whether locations may not, may, or
12279 shall be inserted now. See 'enum ugll_insert_mode' for more
12280 info. */
12281
12282 static void
12283 update_global_location_list (enum ugll_insert_mode insert_mode)
12284 {
12285 struct breakpoint *b;
12286 struct bp_location **locp, *loc;
12287 struct cleanup *cleanups;
12288 /* Last breakpoint location address that was marked for update. */
12289 CORE_ADDR last_addr = 0;
12290 /* Last breakpoint location program space that was marked for update. */
12291 int last_pspace_num = -1;
12292
12293 /* Used in the duplicates detection below. When iterating over all
12294 bp_locations, points to the first bp_location of a given address.
12295 Breakpoints and watchpoints of different types are never
12296 duplicates of each other. Keep one pointer for each type of
12297 breakpoint/watchpoint, so we only need to loop over all locations
12298 once. */
12299 struct bp_location *bp_loc_first; /* breakpoint */
12300 struct bp_location *wp_loc_first; /* hardware watchpoint */
12301 struct bp_location *awp_loc_first; /* access watchpoint */
12302 struct bp_location *rwp_loc_first; /* read watchpoint */
12303
12304 /* Saved former bp_locations array which we compare against the newly
12305 built bp_locations from the current state of ALL_BREAKPOINTS. */
12306 struct bp_location **old_locations, **old_locp;
12307 unsigned old_locations_count;
12308
12309 old_locations = bp_locations;
12310 old_locations_count = bp_locations_count;
12311 bp_locations = NULL;
12312 bp_locations_count = 0;
12313 cleanups = make_cleanup (xfree, old_locations);
12314
12315 ALL_BREAKPOINTS (b)
12316 for (loc = b->loc; loc; loc = loc->next)
12317 bp_locations_count++;
12318
12319 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12320 locp = bp_locations;
12321 ALL_BREAKPOINTS (b)
12322 for (loc = b->loc; loc; loc = loc->next)
12323 *locp++ = loc;
12324 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12325 bp_locations_compare);
12326
12327 bp_locations_target_extensions_update ();
12328
12329 /* Identify bp_location instances that are no longer present in the
12330 new list, and therefore should be freed. Note that it's not
12331 necessary that those locations should be removed from inferior --
12332 if there's another location at the same address (previously
12333 marked as duplicate), we don't need to remove/insert the
12334 location.
12335
12336 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12337 and former bp_location array state respectively. */
12338
12339 locp = bp_locations;
12340 for (old_locp = old_locations;
12341 old_locp < old_locations + old_locations_count;
12342 old_locp++)
12343 {
12344 struct bp_location *old_loc = *old_locp;
12345 struct bp_location **loc2p;
12346
12347 /* Tells if 'old_loc' is found among the new locations. If
12348 not, we have to free it. */
12349 int found_object = 0;
12350 /* Tells if the location should remain inserted in the target. */
12351 int keep_in_target = 0;
12352 int removed = 0;
12353
12354 /* Skip LOCP entries which will definitely never be needed.
12355 Stop either at or being the one matching OLD_LOC. */
12356 while (locp < bp_locations + bp_locations_count
12357 && (*locp)->address < old_loc->address)
12358 locp++;
12359
12360 for (loc2p = locp;
12361 (loc2p < bp_locations + bp_locations_count
12362 && (*loc2p)->address == old_loc->address);
12363 loc2p++)
12364 {
12365 /* Check if this is a new/duplicated location or a duplicated
12366 location that had its condition modified. If so, we want to send
12367 its condition to the target if evaluation of conditions is taking
12368 place there. */
12369 if ((*loc2p)->condition_changed == condition_modified
12370 && (last_addr != old_loc->address
12371 || last_pspace_num != old_loc->pspace->num))
12372 {
12373 force_breakpoint_reinsertion (*loc2p);
12374 last_pspace_num = old_loc->pspace->num;
12375 }
12376
12377 if (*loc2p == old_loc)
12378 found_object = 1;
12379 }
12380
12381 /* We have already handled this address, update it so that we don't
12382 have to go through updates again. */
12383 last_addr = old_loc->address;
12384
12385 /* Target-side condition evaluation: Handle deleted locations. */
12386 if (!found_object)
12387 force_breakpoint_reinsertion (old_loc);
12388
12389 /* If this location is no longer present, and inserted, look if
12390 there's maybe a new location at the same address. If so,
12391 mark that one inserted, and don't remove this one. This is
12392 needed so that we don't have a time window where a breakpoint
12393 at certain location is not inserted. */
12394
12395 if (old_loc->inserted)
12396 {
12397 /* If the location is inserted now, we might have to remove
12398 it. */
12399
12400 if (found_object && should_be_inserted (old_loc))
12401 {
12402 /* The location is still present in the location list,
12403 and still should be inserted. Don't do anything. */
12404 keep_in_target = 1;
12405 }
12406 else
12407 {
12408 /* This location still exists, but it won't be kept in the
12409 target since it may have been disabled. We proceed to
12410 remove its target-side condition. */
12411
12412 /* The location is either no longer present, or got
12413 disabled. See if there's another location at the
12414 same address, in which case we don't need to remove
12415 this one from the target. */
12416
12417 /* OLD_LOC comes from existing struct breakpoint. */
12418 if (breakpoint_address_is_meaningful (old_loc->owner))
12419 {
12420 for (loc2p = locp;
12421 (loc2p < bp_locations + bp_locations_count
12422 && (*loc2p)->address == old_loc->address);
12423 loc2p++)
12424 {
12425 struct bp_location *loc2 = *loc2p;
12426
12427 if (breakpoint_locations_match (loc2, old_loc))
12428 {
12429 /* Read watchpoint locations are switched to
12430 access watchpoints, if the former are not
12431 supported, but the latter are. */
12432 if (is_hardware_watchpoint (old_loc->owner))
12433 {
12434 gdb_assert (is_hardware_watchpoint (loc2->owner));
12435 loc2->watchpoint_type = old_loc->watchpoint_type;
12436 }
12437
12438 /* loc2 is a duplicated location. We need to check
12439 if it should be inserted in case it will be
12440 unduplicated. */
12441 if (loc2 != old_loc
12442 && unduplicated_should_be_inserted (loc2))
12443 {
12444 swap_insertion (old_loc, loc2);
12445 keep_in_target = 1;
12446 break;
12447 }
12448 }
12449 }
12450 }
12451 }
12452
12453 if (!keep_in_target)
12454 {
12455 if (remove_breakpoint (old_loc))
12456 {
12457 /* This is just about all we can do. We could keep
12458 this location on the global list, and try to
12459 remove it next time, but there's no particular
12460 reason why we will succeed next time.
12461
12462 Note that at this point, old_loc->owner is still
12463 valid, as delete_breakpoint frees the breakpoint
12464 only after calling us. */
12465 printf_filtered (_("warning: Error removing "
12466 "breakpoint %d\n"),
12467 old_loc->owner->number);
12468 }
12469 removed = 1;
12470 }
12471 }
12472
12473 if (!found_object)
12474 {
12475 if (removed && target_is_non_stop_p ()
12476 && need_moribund_for_location_type (old_loc))
12477 {
12478 /* This location was removed from the target. In
12479 non-stop mode, a race condition is possible where
12480 we've removed a breakpoint, but stop events for that
12481 breakpoint are already queued and will arrive later.
12482 We apply an heuristic to be able to distinguish such
12483 SIGTRAPs from other random SIGTRAPs: we keep this
12484 breakpoint location for a bit, and will retire it
12485 after we see some number of events. The theory here
12486 is that reporting of events should, "on the average",
12487 be fair, so after a while we'll see events from all
12488 threads that have anything of interest, and no longer
12489 need to keep this breakpoint location around. We
12490 don't hold locations forever so to reduce chances of
12491 mistaking a non-breakpoint SIGTRAP for a breakpoint
12492 SIGTRAP.
12493
12494 The heuristic failing can be disastrous on
12495 decr_pc_after_break targets.
12496
12497 On decr_pc_after_break targets, like e.g., x86-linux,
12498 if we fail to recognize a late breakpoint SIGTRAP,
12499 because events_till_retirement has reached 0 too
12500 soon, we'll fail to do the PC adjustment, and report
12501 a random SIGTRAP to the user. When the user resumes
12502 the inferior, it will most likely immediately crash
12503 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12504 corrupted, because of being resumed e.g., in the
12505 middle of a multi-byte instruction, or skipped a
12506 one-byte instruction. This was actually seen happen
12507 on native x86-linux, and should be less rare on
12508 targets that do not support new thread events, like
12509 remote, due to the heuristic depending on
12510 thread_count.
12511
12512 Mistaking a random SIGTRAP for a breakpoint trap
12513 causes similar symptoms (PC adjustment applied when
12514 it shouldn't), but then again, playing with SIGTRAPs
12515 behind the debugger's back is asking for trouble.
12516
12517 Since hardware watchpoint traps are always
12518 distinguishable from other traps, so we don't need to
12519 apply keep hardware watchpoint moribund locations
12520 around. We simply always ignore hardware watchpoint
12521 traps we can no longer explain. */
12522
12523 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12524 old_loc->owner = NULL;
12525
12526 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12527 }
12528 else
12529 {
12530 old_loc->owner = NULL;
12531 decref_bp_location (&old_loc);
12532 }
12533 }
12534 }
12535
12536 /* Rescan breakpoints at the same address and section, marking the
12537 first one as "first" and any others as "duplicates". This is so
12538 that the bpt instruction is only inserted once. If we have a
12539 permanent breakpoint at the same place as BPT, make that one the
12540 official one, and the rest as duplicates. Permanent breakpoints
12541 are sorted first for the same address.
12542
12543 Do the same for hardware watchpoints, but also considering the
12544 watchpoint's type (regular/access/read) and length. */
12545
12546 bp_loc_first = NULL;
12547 wp_loc_first = NULL;
12548 awp_loc_first = NULL;
12549 rwp_loc_first = NULL;
12550 ALL_BP_LOCATIONS (loc, locp)
12551 {
12552 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12553 non-NULL. */
12554 struct bp_location **loc_first_p;
12555 b = loc->owner;
12556
12557 if (!unduplicated_should_be_inserted (loc)
12558 || !breakpoint_address_is_meaningful (b)
12559 /* Don't detect duplicate for tracepoint locations because they are
12560 never duplicated. See the comments in field `duplicate' of
12561 `struct bp_location'. */
12562 || is_tracepoint (b))
12563 {
12564 /* Clear the condition modification flag. */
12565 loc->condition_changed = condition_unchanged;
12566 continue;
12567 }
12568
12569 if (b->type == bp_hardware_watchpoint)
12570 loc_first_p = &wp_loc_first;
12571 else if (b->type == bp_read_watchpoint)
12572 loc_first_p = &rwp_loc_first;
12573 else if (b->type == bp_access_watchpoint)
12574 loc_first_p = &awp_loc_first;
12575 else
12576 loc_first_p = &bp_loc_first;
12577
12578 if (*loc_first_p == NULL
12579 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12580 || !breakpoint_locations_match (loc, *loc_first_p))
12581 {
12582 *loc_first_p = loc;
12583 loc->duplicate = 0;
12584
12585 if (is_breakpoint (loc->owner) && loc->condition_changed)
12586 {
12587 loc->needs_update = 1;
12588 /* Clear the condition modification flag. */
12589 loc->condition_changed = condition_unchanged;
12590 }
12591 continue;
12592 }
12593
12594
12595 /* This and the above ensure the invariant that the first location
12596 is not duplicated, and is the inserted one.
12597 All following are marked as duplicated, and are not inserted. */
12598 if (loc->inserted)
12599 swap_insertion (loc, *loc_first_p);
12600 loc->duplicate = 1;
12601
12602 /* Clear the condition modification flag. */
12603 loc->condition_changed = condition_unchanged;
12604 }
12605
12606 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12607 {
12608 if (insert_mode != UGLL_DONT_INSERT)
12609 insert_breakpoint_locations ();
12610 else
12611 {
12612 /* Even though the caller told us to not insert new
12613 locations, we may still need to update conditions on the
12614 target's side of breakpoints that were already inserted
12615 if the target is evaluating breakpoint conditions. We
12616 only update conditions for locations that are marked
12617 "needs_update". */
12618 update_inserted_breakpoint_locations ();
12619 }
12620 }
12621
12622 if (insert_mode != UGLL_DONT_INSERT)
12623 download_tracepoint_locations ();
12624
12625 do_cleanups (cleanups);
12626 }
12627
12628 void
12629 breakpoint_retire_moribund (void)
12630 {
12631 struct bp_location *loc;
12632 int ix;
12633
12634 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12635 if (--(loc->events_till_retirement) == 0)
12636 {
12637 decref_bp_location (&loc);
12638 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12639 --ix;
12640 }
12641 }
12642
12643 static void
12644 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12645 {
12646
12647 TRY
12648 {
12649 update_global_location_list (insert_mode);
12650 }
12651 CATCH (e, RETURN_MASK_ERROR)
12652 {
12653 }
12654 END_CATCH
12655 }
12656
12657 /* Clear BKP from a BPS. */
12658
12659 static void
12660 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12661 {
12662 bpstat bs;
12663
12664 for (bs = bps; bs; bs = bs->next)
12665 if (bs->breakpoint_at == bpt)
12666 {
12667 bs->breakpoint_at = NULL;
12668 bs->old_val = NULL;
12669 /* bs->commands will be freed later. */
12670 }
12671 }
12672
12673 /* Callback for iterate_over_threads. */
12674 static int
12675 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12676 {
12677 struct breakpoint *bpt = (struct breakpoint *) data;
12678
12679 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12680 return 0;
12681 }
12682
12683 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12684 callbacks. */
12685
12686 static void
12687 say_where (struct breakpoint *b)
12688 {
12689 struct value_print_options opts;
12690
12691 get_user_print_options (&opts);
12692
12693 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12694 single string. */
12695 if (b->loc == NULL)
12696 {
12697 /* For pending locations, the output differs slightly based
12698 on b->extra_string. If this is non-NULL, it contains either
12699 a condition or dprintf arguments. */
12700 if (b->extra_string == NULL)
12701 {
12702 printf_filtered (_(" (%s) pending."),
12703 event_location_to_string (b->location.get ()));
12704 }
12705 else if (b->type == bp_dprintf)
12706 {
12707 printf_filtered (_(" (%s,%s) pending."),
12708 event_location_to_string (b->location.get ()),
12709 b->extra_string);
12710 }
12711 else
12712 {
12713 printf_filtered (_(" (%s %s) pending."),
12714 event_location_to_string (b->location.get ()),
12715 b->extra_string);
12716 }
12717 }
12718 else
12719 {
12720 if (opts.addressprint || b->loc->symtab == NULL)
12721 {
12722 printf_filtered (" at ");
12723 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12724 gdb_stdout);
12725 }
12726 if (b->loc->symtab != NULL)
12727 {
12728 /* If there is a single location, we can print the location
12729 more nicely. */
12730 if (b->loc->next == NULL)
12731 printf_filtered (": file %s, line %d.",
12732 symtab_to_filename_for_display (b->loc->symtab),
12733 b->loc->line_number);
12734 else
12735 /* This is not ideal, but each location may have a
12736 different file name, and this at least reflects the
12737 real situation somewhat. */
12738 printf_filtered (": %s.",
12739 event_location_to_string (b->location.get ()));
12740 }
12741
12742 if (b->loc->next)
12743 {
12744 struct bp_location *loc = b->loc;
12745 int n = 0;
12746 for (; loc; loc = loc->next)
12747 ++n;
12748 printf_filtered (" (%d locations)", n);
12749 }
12750 }
12751 }
12752
12753 /* Default bp_location_ops methods. */
12754
12755 static void
12756 bp_location_dtor (struct bp_location *self)
12757 {
12758 xfree (self->function_name);
12759 }
12760
12761 static const struct bp_location_ops bp_location_ops =
12762 {
12763 bp_location_dtor
12764 };
12765
12766 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12767 inherit from. */
12768
12769 static void
12770 base_breakpoint_dtor (struct breakpoint *self)
12771 {
12772 decref_counted_command_line (&self->commands);
12773 xfree (self->cond_string);
12774 xfree (self->extra_string);
12775 xfree (self->filter);
12776 }
12777
12778 static struct bp_location *
12779 base_breakpoint_allocate_location (struct breakpoint *self)
12780 {
12781 return new bp_location (&bp_location_ops, self);
12782 }
12783
12784 static void
12785 base_breakpoint_re_set (struct breakpoint *b)
12786 {
12787 /* Nothing to re-set. */
12788 }
12789
12790 #define internal_error_pure_virtual_called() \
12791 gdb_assert_not_reached ("pure virtual function called")
12792
12793 static int
12794 base_breakpoint_insert_location (struct bp_location *bl)
12795 {
12796 internal_error_pure_virtual_called ();
12797 }
12798
12799 static int
12800 base_breakpoint_remove_location (struct bp_location *bl,
12801 enum remove_bp_reason reason)
12802 {
12803 internal_error_pure_virtual_called ();
12804 }
12805
12806 static int
12807 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12808 struct address_space *aspace,
12809 CORE_ADDR bp_addr,
12810 const struct target_waitstatus *ws)
12811 {
12812 internal_error_pure_virtual_called ();
12813 }
12814
12815 static void
12816 base_breakpoint_check_status (bpstat bs)
12817 {
12818 /* Always stop. */
12819 }
12820
12821 /* A "works_in_software_mode" breakpoint_ops method that just internal
12822 errors. */
12823
12824 static int
12825 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12826 {
12827 internal_error_pure_virtual_called ();
12828 }
12829
12830 /* A "resources_needed" breakpoint_ops method that just internal
12831 errors. */
12832
12833 static int
12834 base_breakpoint_resources_needed (const struct bp_location *bl)
12835 {
12836 internal_error_pure_virtual_called ();
12837 }
12838
12839 static enum print_stop_action
12840 base_breakpoint_print_it (bpstat bs)
12841 {
12842 internal_error_pure_virtual_called ();
12843 }
12844
12845 static void
12846 base_breakpoint_print_one_detail (const struct breakpoint *self,
12847 struct ui_out *uiout)
12848 {
12849 /* nothing */
12850 }
12851
12852 static void
12853 base_breakpoint_print_mention (struct breakpoint *b)
12854 {
12855 internal_error_pure_virtual_called ();
12856 }
12857
12858 static void
12859 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12860 {
12861 internal_error_pure_virtual_called ();
12862 }
12863
12864 static void
12865 base_breakpoint_create_sals_from_location
12866 (const struct event_location *location,
12867 struct linespec_result *canonical,
12868 enum bptype type_wanted)
12869 {
12870 internal_error_pure_virtual_called ();
12871 }
12872
12873 static void
12874 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12875 struct linespec_result *c,
12876 char *cond_string,
12877 char *extra_string,
12878 enum bptype type_wanted,
12879 enum bpdisp disposition,
12880 int thread,
12881 int task, int ignore_count,
12882 const struct breakpoint_ops *o,
12883 int from_tty, int enabled,
12884 int internal, unsigned flags)
12885 {
12886 internal_error_pure_virtual_called ();
12887 }
12888
12889 static void
12890 base_breakpoint_decode_location (struct breakpoint *b,
12891 const struct event_location *location,
12892 struct program_space *search_pspace,
12893 struct symtabs_and_lines *sals)
12894 {
12895 internal_error_pure_virtual_called ();
12896 }
12897
12898 /* The default 'explains_signal' method. */
12899
12900 static int
12901 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12902 {
12903 return 1;
12904 }
12905
12906 /* The default "after_condition_true" method. */
12907
12908 static void
12909 base_breakpoint_after_condition_true (struct bpstats *bs)
12910 {
12911 /* Nothing to do. */
12912 }
12913
12914 struct breakpoint_ops base_breakpoint_ops =
12915 {
12916 base_breakpoint_dtor,
12917 base_breakpoint_allocate_location,
12918 base_breakpoint_re_set,
12919 base_breakpoint_insert_location,
12920 base_breakpoint_remove_location,
12921 base_breakpoint_breakpoint_hit,
12922 base_breakpoint_check_status,
12923 base_breakpoint_resources_needed,
12924 base_breakpoint_works_in_software_mode,
12925 base_breakpoint_print_it,
12926 NULL,
12927 base_breakpoint_print_one_detail,
12928 base_breakpoint_print_mention,
12929 base_breakpoint_print_recreate,
12930 base_breakpoint_create_sals_from_location,
12931 base_breakpoint_create_breakpoints_sal,
12932 base_breakpoint_decode_location,
12933 base_breakpoint_explains_signal,
12934 base_breakpoint_after_condition_true,
12935 };
12936
12937 /* Default breakpoint_ops methods. */
12938
12939 static void
12940 bkpt_re_set (struct breakpoint *b)
12941 {
12942 /* FIXME: is this still reachable? */
12943 if (breakpoint_event_location_empty_p (b))
12944 {
12945 /* Anything without a location can't be re-set. */
12946 delete_breakpoint (b);
12947 return;
12948 }
12949
12950 breakpoint_re_set_default (b);
12951 }
12952
12953 static int
12954 bkpt_insert_location (struct bp_location *bl)
12955 {
12956 CORE_ADDR addr = bl->target_info.reqstd_address;
12957
12958 bl->target_info.kind = breakpoint_kind (bl, &addr);
12959 bl->target_info.placed_address = addr;
12960
12961 if (bl->loc_type == bp_loc_hardware_breakpoint)
12962 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12963 else
12964 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12965 }
12966
12967 static int
12968 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
12969 {
12970 if (bl->loc_type == bp_loc_hardware_breakpoint)
12971 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12972 else
12973 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12974 }
12975
12976 static int
12977 bkpt_breakpoint_hit (const struct bp_location *bl,
12978 struct address_space *aspace, CORE_ADDR bp_addr,
12979 const struct target_waitstatus *ws)
12980 {
12981 if (ws->kind != TARGET_WAITKIND_STOPPED
12982 || ws->value.sig != GDB_SIGNAL_TRAP)
12983 return 0;
12984
12985 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12986 aspace, bp_addr))
12987 return 0;
12988
12989 if (overlay_debugging /* unmapped overlay section */
12990 && section_is_overlay (bl->section)
12991 && !section_is_mapped (bl->section))
12992 return 0;
12993
12994 return 1;
12995 }
12996
12997 static int
12998 dprintf_breakpoint_hit (const struct bp_location *bl,
12999 struct address_space *aspace, CORE_ADDR bp_addr,
13000 const struct target_waitstatus *ws)
13001 {
13002 if (dprintf_style == dprintf_style_agent
13003 && target_can_run_breakpoint_commands ())
13004 {
13005 /* An agent-style dprintf never causes a stop. If we see a trap
13006 for this address it must be for a breakpoint that happens to
13007 be set at the same address. */
13008 return 0;
13009 }
13010
13011 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13012 }
13013
13014 static int
13015 bkpt_resources_needed (const struct bp_location *bl)
13016 {
13017 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13018
13019 return 1;
13020 }
13021
13022 static enum print_stop_action
13023 bkpt_print_it (bpstat bs)
13024 {
13025 struct breakpoint *b;
13026 const struct bp_location *bl;
13027 int bp_temp;
13028 struct ui_out *uiout = current_uiout;
13029
13030 gdb_assert (bs->bp_location_at != NULL);
13031
13032 bl = bs->bp_location_at;
13033 b = bs->breakpoint_at;
13034
13035 bp_temp = b->disposition == disp_del;
13036 if (bl->address != bl->requested_address)
13037 breakpoint_adjustment_warning (bl->requested_address,
13038 bl->address,
13039 b->number, 1);
13040 annotate_breakpoint (b->number);
13041 maybe_print_thread_hit_breakpoint (uiout);
13042
13043 if (bp_temp)
13044 uiout->text ("Temporary breakpoint ");
13045 else
13046 uiout->text ("Breakpoint ");
13047 if (uiout->is_mi_like_p ())
13048 {
13049 uiout->field_string ("reason",
13050 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13051 uiout->field_string ("disp", bpdisp_text (b->disposition));
13052 }
13053 uiout->field_int ("bkptno", b->number);
13054 uiout->text (", ");
13055
13056 return PRINT_SRC_AND_LOC;
13057 }
13058
13059 static void
13060 bkpt_print_mention (struct breakpoint *b)
13061 {
13062 if (current_uiout->is_mi_like_p ())
13063 return;
13064
13065 switch (b->type)
13066 {
13067 case bp_breakpoint:
13068 case bp_gnu_ifunc_resolver:
13069 if (b->disposition == disp_del)
13070 printf_filtered (_("Temporary breakpoint"));
13071 else
13072 printf_filtered (_("Breakpoint"));
13073 printf_filtered (_(" %d"), b->number);
13074 if (b->type == bp_gnu_ifunc_resolver)
13075 printf_filtered (_(" at gnu-indirect-function resolver"));
13076 break;
13077 case bp_hardware_breakpoint:
13078 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13079 break;
13080 case bp_dprintf:
13081 printf_filtered (_("Dprintf %d"), b->number);
13082 break;
13083 }
13084
13085 say_where (b);
13086 }
13087
13088 static void
13089 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13090 {
13091 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13092 fprintf_unfiltered (fp, "tbreak");
13093 else if (tp->type == bp_breakpoint)
13094 fprintf_unfiltered (fp, "break");
13095 else if (tp->type == bp_hardware_breakpoint
13096 && tp->disposition == disp_del)
13097 fprintf_unfiltered (fp, "thbreak");
13098 else if (tp->type == bp_hardware_breakpoint)
13099 fprintf_unfiltered (fp, "hbreak");
13100 else
13101 internal_error (__FILE__, __LINE__,
13102 _("unhandled breakpoint type %d"), (int) tp->type);
13103
13104 fprintf_unfiltered (fp, " %s",
13105 event_location_to_string (tp->location.get ()));
13106
13107 /* Print out extra_string if this breakpoint is pending. It might
13108 contain, for example, conditions that were set by the user. */
13109 if (tp->loc == NULL && tp->extra_string != NULL)
13110 fprintf_unfiltered (fp, " %s", tp->extra_string);
13111
13112 print_recreate_thread (tp, fp);
13113 }
13114
13115 static void
13116 bkpt_create_sals_from_location (const struct event_location *location,
13117 struct linespec_result *canonical,
13118 enum bptype type_wanted)
13119 {
13120 create_sals_from_location_default (location, canonical, type_wanted);
13121 }
13122
13123 static void
13124 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13125 struct linespec_result *canonical,
13126 char *cond_string,
13127 char *extra_string,
13128 enum bptype type_wanted,
13129 enum bpdisp disposition,
13130 int thread,
13131 int task, int ignore_count,
13132 const struct breakpoint_ops *ops,
13133 int from_tty, int enabled,
13134 int internal, unsigned flags)
13135 {
13136 create_breakpoints_sal_default (gdbarch, canonical,
13137 cond_string, extra_string,
13138 type_wanted,
13139 disposition, thread, task,
13140 ignore_count, ops, from_tty,
13141 enabled, internal, flags);
13142 }
13143
13144 static void
13145 bkpt_decode_location (struct breakpoint *b,
13146 const struct event_location *location,
13147 struct program_space *search_pspace,
13148 struct symtabs_and_lines *sals)
13149 {
13150 decode_location_default (b, location, search_pspace, sals);
13151 }
13152
13153 /* Virtual table for internal breakpoints. */
13154
13155 static void
13156 internal_bkpt_re_set (struct breakpoint *b)
13157 {
13158 switch (b->type)
13159 {
13160 /* Delete overlay event and longjmp master breakpoints; they
13161 will be reset later by breakpoint_re_set. */
13162 case bp_overlay_event:
13163 case bp_longjmp_master:
13164 case bp_std_terminate_master:
13165 case bp_exception_master:
13166 delete_breakpoint (b);
13167 break;
13168
13169 /* This breakpoint is special, it's set up when the inferior
13170 starts and we really don't want to touch it. */
13171 case bp_shlib_event:
13172
13173 /* Like bp_shlib_event, this breakpoint type is special. Once
13174 it is set up, we do not want to touch it. */
13175 case bp_thread_event:
13176 break;
13177 }
13178 }
13179
13180 static void
13181 internal_bkpt_check_status (bpstat bs)
13182 {
13183 if (bs->breakpoint_at->type == bp_shlib_event)
13184 {
13185 /* If requested, stop when the dynamic linker notifies GDB of
13186 events. This allows the user to get control and place
13187 breakpoints in initializer routines for dynamically loaded
13188 objects (among other things). */
13189 bs->stop = stop_on_solib_events;
13190 bs->print = stop_on_solib_events;
13191 }
13192 else
13193 bs->stop = 0;
13194 }
13195
13196 static enum print_stop_action
13197 internal_bkpt_print_it (bpstat bs)
13198 {
13199 struct breakpoint *b;
13200
13201 b = bs->breakpoint_at;
13202
13203 switch (b->type)
13204 {
13205 case bp_shlib_event:
13206 /* Did we stop because the user set the stop_on_solib_events
13207 variable? (If so, we report this as a generic, "Stopped due
13208 to shlib event" message.) */
13209 print_solib_event (0);
13210 break;
13211
13212 case bp_thread_event:
13213 /* Not sure how we will get here.
13214 GDB should not stop for these breakpoints. */
13215 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13216 break;
13217
13218 case bp_overlay_event:
13219 /* By analogy with the thread event, GDB should not stop for these. */
13220 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13221 break;
13222
13223 case bp_longjmp_master:
13224 /* These should never be enabled. */
13225 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13226 break;
13227
13228 case bp_std_terminate_master:
13229 /* These should never be enabled. */
13230 printf_filtered (_("std::terminate Master Breakpoint: "
13231 "gdb should not stop!\n"));
13232 break;
13233
13234 case bp_exception_master:
13235 /* These should never be enabled. */
13236 printf_filtered (_("Exception Master Breakpoint: "
13237 "gdb should not stop!\n"));
13238 break;
13239 }
13240
13241 return PRINT_NOTHING;
13242 }
13243
13244 static void
13245 internal_bkpt_print_mention (struct breakpoint *b)
13246 {
13247 /* Nothing to mention. These breakpoints are internal. */
13248 }
13249
13250 /* Virtual table for momentary breakpoints */
13251
13252 static void
13253 momentary_bkpt_re_set (struct breakpoint *b)
13254 {
13255 /* Keep temporary breakpoints, which can be encountered when we step
13256 over a dlopen call and solib_add is resetting the breakpoints.
13257 Otherwise these should have been blown away via the cleanup chain
13258 or by breakpoint_init_inferior when we rerun the executable. */
13259 }
13260
13261 static void
13262 momentary_bkpt_check_status (bpstat bs)
13263 {
13264 /* Nothing. The point of these breakpoints is causing a stop. */
13265 }
13266
13267 static enum print_stop_action
13268 momentary_bkpt_print_it (bpstat bs)
13269 {
13270 return PRINT_UNKNOWN;
13271 }
13272
13273 static void
13274 momentary_bkpt_print_mention (struct breakpoint *b)
13275 {
13276 /* Nothing to mention. These breakpoints are internal. */
13277 }
13278
13279 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13280
13281 It gets cleared already on the removal of the first one of such placed
13282 breakpoints. This is OK as they get all removed altogether. */
13283
13284 static void
13285 longjmp_bkpt_dtor (struct breakpoint *self)
13286 {
13287 struct thread_info *tp = find_thread_global_id (self->thread);
13288
13289 if (tp)
13290 tp->initiating_frame = null_frame_id;
13291
13292 momentary_breakpoint_ops.dtor (self);
13293 }
13294
13295 /* Specific methods for probe breakpoints. */
13296
13297 static int
13298 bkpt_probe_insert_location (struct bp_location *bl)
13299 {
13300 int v = bkpt_insert_location (bl);
13301
13302 if (v == 0)
13303 {
13304 /* The insertion was successful, now let's set the probe's semaphore
13305 if needed. */
13306 if (bl->probe.probe->pops->set_semaphore != NULL)
13307 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13308 bl->probe.objfile,
13309 bl->gdbarch);
13310 }
13311
13312 return v;
13313 }
13314
13315 static int
13316 bkpt_probe_remove_location (struct bp_location *bl,
13317 enum remove_bp_reason reason)
13318 {
13319 /* Let's clear the semaphore before removing the location. */
13320 if (bl->probe.probe->pops->clear_semaphore != NULL)
13321 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13322 bl->probe.objfile,
13323 bl->gdbarch);
13324
13325 return bkpt_remove_location (bl, reason);
13326 }
13327
13328 static void
13329 bkpt_probe_create_sals_from_location (const struct event_location *location,
13330 struct linespec_result *canonical,
13331 enum bptype type_wanted)
13332 {
13333 struct linespec_sals lsal;
13334
13335 lsal.sals = parse_probes (location, NULL, canonical);
13336 lsal.canonical
13337 = xstrdup (event_location_to_string (canonical->location.get ()));
13338 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13339 }
13340
13341 static void
13342 bkpt_probe_decode_location (struct breakpoint *b,
13343 const struct event_location *location,
13344 struct program_space *search_pspace,
13345 struct symtabs_and_lines *sals)
13346 {
13347 *sals = parse_probes (location, search_pspace, NULL);
13348 if (!sals->sals)
13349 error (_("probe not found"));
13350 }
13351
13352 /* The breakpoint_ops structure to be used in tracepoints. */
13353
13354 static void
13355 tracepoint_re_set (struct breakpoint *b)
13356 {
13357 breakpoint_re_set_default (b);
13358 }
13359
13360 static int
13361 tracepoint_breakpoint_hit (const struct bp_location *bl,
13362 struct address_space *aspace, CORE_ADDR bp_addr,
13363 const struct target_waitstatus *ws)
13364 {
13365 /* By definition, the inferior does not report stops at
13366 tracepoints. */
13367 return 0;
13368 }
13369
13370 static void
13371 tracepoint_print_one_detail (const struct breakpoint *self,
13372 struct ui_out *uiout)
13373 {
13374 struct tracepoint *tp = (struct tracepoint *) self;
13375 if (tp->static_trace_marker_id)
13376 {
13377 gdb_assert (self->type == bp_static_tracepoint);
13378
13379 uiout->text ("\tmarker id is ");
13380 uiout->field_string ("static-tracepoint-marker-string-id",
13381 tp->static_trace_marker_id);
13382 uiout->text ("\n");
13383 }
13384 }
13385
13386 static void
13387 tracepoint_print_mention (struct breakpoint *b)
13388 {
13389 if (current_uiout->is_mi_like_p ())
13390 return;
13391
13392 switch (b->type)
13393 {
13394 case bp_tracepoint:
13395 printf_filtered (_("Tracepoint"));
13396 printf_filtered (_(" %d"), b->number);
13397 break;
13398 case bp_fast_tracepoint:
13399 printf_filtered (_("Fast tracepoint"));
13400 printf_filtered (_(" %d"), b->number);
13401 break;
13402 case bp_static_tracepoint:
13403 printf_filtered (_("Static tracepoint"));
13404 printf_filtered (_(" %d"), b->number);
13405 break;
13406 default:
13407 internal_error (__FILE__, __LINE__,
13408 _("unhandled tracepoint type %d"), (int) b->type);
13409 }
13410
13411 say_where (b);
13412 }
13413
13414 static void
13415 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13416 {
13417 struct tracepoint *tp = (struct tracepoint *) self;
13418
13419 if (self->type == bp_fast_tracepoint)
13420 fprintf_unfiltered (fp, "ftrace");
13421 else if (self->type == bp_static_tracepoint)
13422 fprintf_unfiltered (fp, "strace");
13423 else if (self->type == bp_tracepoint)
13424 fprintf_unfiltered (fp, "trace");
13425 else
13426 internal_error (__FILE__, __LINE__,
13427 _("unhandled tracepoint type %d"), (int) self->type);
13428
13429 fprintf_unfiltered (fp, " %s",
13430 event_location_to_string (self->location.get ()));
13431 print_recreate_thread (self, fp);
13432
13433 if (tp->pass_count)
13434 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13435 }
13436
13437 static void
13438 tracepoint_create_sals_from_location (const struct event_location *location,
13439 struct linespec_result *canonical,
13440 enum bptype type_wanted)
13441 {
13442 create_sals_from_location_default (location, canonical, type_wanted);
13443 }
13444
13445 static void
13446 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13447 struct linespec_result *canonical,
13448 char *cond_string,
13449 char *extra_string,
13450 enum bptype type_wanted,
13451 enum bpdisp disposition,
13452 int thread,
13453 int task, int ignore_count,
13454 const struct breakpoint_ops *ops,
13455 int from_tty, int enabled,
13456 int internal, unsigned flags)
13457 {
13458 create_breakpoints_sal_default (gdbarch, canonical,
13459 cond_string, extra_string,
13460 type_wanted,
13461 disposition, thread, task,
13462 ignore_count, ops, from_tty,
13463 enabled, internal, flags);
13464 }
13465
13466 static void
13467 tracepoint_decode_location (struct breakpoint *b,
13468 const struct event_location *location,
13469 struct program_space *search_pspace,
13470 struct symtabs_and_lines *sals)
13471 {
13472 decode_location_default (b, location, search_pspace, sals);
13473 }
13474
13475 struct breakpoint_ops tracepoint_breakpoint_ops;
13476
13477 /* The breakpoint_ops structure to be use on tracepoints placed in a
13478 static probe. */
13479
13480 static void
13481 tracepoint_probe_create_sals_from_location
13482 (const struct event_location *location,
13483 struct linespec_result *canonical,
13484 enum bptype type_wanted)
13485 {
13486 /* We use the same method for breakpoint on probes. */
13487 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13488 }
13489
13490 static void
13491 tracepoint_probe_decode_location (struct breakpoint *b,
13492 const struct event_location *location,
13493 struct program_space *search_pspace,
13494 struct symtabs_and_lines *sals)
13495 {
13496 /* We use the same method for breakpoint on probes. */
13497 bkpt_probe_decode_location (b, location, search_pspace, sals);
13498 }
13499
13500 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13501
13502 /* Dprintf breakpoint_ops methods. */
13503
13504 static void
13505 dprintf_re_set (struct breakpoint *b)
13506 {
13507 breakpoint_re_set_default (b);
13508
13509 /* extra_string should never be non-NULL for dprintf. */
13510 gdb_assert (b->extra_string != NULL);
13511
13512 /* 1 - connect to target 1, that can run breakpoint commands.
13513 2 - create a dprintf, which resolves fine.
13514 3 - disconnect from target 1
13515 4 - connect to target 2, that can NOT run breakpoint commands.
13516
13517 After steps #3/#4, you'll want the dprintf command list to
13518 be updated, because target 1 and 2 may well return different
13519 answers for target_can_run_breakpoint_commands().
13520 Given absence of finer grained resetting, we get to do
13521 it all the time. */
13522 if (b->extra_string != NULL)
13523 update_dprintf_command_list (b);
13524 }
13525
13526 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13527
13528 static void
13529 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13530 {
13531 fprintf_unfiltered (fp, "dprintf %s,%s",
13532 event_location_to_string (tp->location.get ()),
13533 tp->extra_string);
13534 print_recreate_thread (tp, fp);
13535 }
13536
13537 /* Implement the "after_condition_true" breakpoint_ops method for
13538 dprintf.
13539
13540 dprintf's are implemented with regular commands in their command
13541 list, but we run the commands here instead of before presenting the
13542 stop to the user, as dprintf's don't actually cause a stop. This
13543 also makes it so that the commands of multiple dprintfs at the same
13544 address are all handled. */
13545
13546 static void
13547 dprintf_after_condition_true (struct bpstats *bs)
13548 {
13549 struct cleanup *old_chain;
13550 struct bpstats tmp_bs = { NULL };
13551 struct bpstats *tmp_bs_p = &tmp_bs;
13552
13553 /* dprintf's never cause a stop. This wasn't set in the
13554 check_status hook instead because that would make the dprintf's
13555 condition not be evaluated. */
13556 bs->stop = 0;
13557
13558 /* Run the command list here. Take ownership of it instead of
13559 copying. We never want these commands to run later in
13560 bpstat_do_actions, if a breakpoint that causes a stop happens to
13561 be set at same address as this dprintf, or even if running the
13562 commands here throws. */
13563 tmp_bs.commands = bs->commands;
13564 bs->commands = NULL;
13565 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13566
13567 bpstat_do_actions_1 (&tmp_bs_p);
13568
13569 /* 'tmp_bs.commands' will usually be NULL by now, but
13570 bpstat_do_actions_1 may return early without processing the whole
13571 list. */
13572 do_cleanups (old_chain);
13573 }
13574
13575 /* The breakpoint_ops structure to be used on static tracepoints with
13576 markers (`-m'). */
13577
13578 static void
13579 strace_marker_create_sals_from_location (const struct event_location *location,
13580 struct linespec_result *canonical,
13581 enum bptype type_wanted)
13582 {
13583 struct linespec_sals lsal;
13584 const char *arg_start, *arg;
13585 char *str;
13586 struct cleanup *cleanup;
13587
13588 arg = arg_start = get_linespec_location (location);
13589 lsal.sals = decode_static_tracepoint_spec (&arg);
13590
13591 str = savestring (arg_start, arg - arg_start);
13592 cleanup = make_cleanup (xfree, str);
13593 canonical->location = new_linespec_location (&str);
13594 do_cleanups (cleanup);
13595
13596 lsal.canonical
13597 = xstrdup (event_location_to_string (canonical->location.get ()));
13598 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13599 }
13600
13601 static void
13602 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13603 struct linespec_result *canonical,
13604 char *cond_string,
13605 char *extra_string,
13606 enum bptype type_wanted,
13607 enum bpdisp disposition,
13608 int thread,
13609 int task, int ignore_count,
13610 const struct breakpoint_ops *ops,
13611 int from_tty, int enabled,
13612 int internal, unsigned flags)
13613 {
13614 int i;
13615 struct linespec_sals *lsal = VEC_index (linespec_sals,
13616 canonical->sals, 0);
13617
13618 /* If the user is creating a static tracepoint by marker id
13619 (strace -m MARKER_ID), then store the sals index, so that
13620 breakpoint_re_set can try to match up which of the newly
13621 found markers corresponds to this one, and, don't try to
13622 expand multiple locations for each sal, given than SALS
13623 already should contain all sals for MARKER_ID. */
13624
13625 for (i = 0; i < lsal->sals.nelts; ++i)
13626 {
13627 struct symtabs_and_lines expanded;
13628 struct tracepoint *tp;
13629 event_location_up location;
13630
13631 expanded.nelts = 1;
13632 expanded.sals = &lsal->sals.sals[i];
13633
13634 location = copy_event_location (canonical->location.get ());
13635
13636 tp = new tracepoint ();
13637 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13638 std::move (location), NULL,
13639 cond_string, extra_string,
13640 type_wanted, disposition,
13641 thread, task, ignore_count, ops,
13642 from_tty, enabled, internal, flags,
13643 canonical->special_display);
13644 /* Given that its possible to have multiple markers with
13645 the same string id, if the user is creating a static
13646 tracepoint by marker id ("strace -m MARKER_ID"), then
13647 store the sals index, so that breakpoint_re_set can
13648 try to match up which of the newly found markers
13649 corresponds to this one */
13650 tp->static_trace_marker_id_idx = i;
13651
13652 install_breakpoint (internal, &tp->base, 0);
13653 }
13654 }
13655
13656 static void
13657 strace_marker_decode_location (struct breakpoint *b,
13658 const struct event_location *location,
13659 struct program_space *search_pspace,
13660 struct symtabs_and_lines *sals)
13661 {
13662 struct tracepoint *tp = (struct tracepoint *) b;
13663 const char *s = get_linespec_location (location);
13664
13665 *sals = decode_static_tracepoint_spec (&s);
13666 if (sals->nelts > tp->static_trace_marker_id_idx)
13667 {
13668 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13669 sals->nelts = 1;
13670 }
13671 else
13672 error (_("marker %s not found"), tp->static_trace_marker_id);
13673 }
13674
13675 static struct breakpoint_ops strace_marker_breakpoint_ops;
13676
13677 static int
13678 strace_marker_p (struct breakpoint *b)
13679 {
13680 return b->ops == &strace_marker_breakpoint_ops;
13681 }
13682
13683 /* Delete a breakpoint and clean up all traces of it in the data
13684 structures. */
13685
13686 void
13687 delete_breakpoint (struct breakpoint *bpt)
13688 {
13689 struct breakpoint *b;
13690
13691 gdb_assert (bpt != NULL);
13692
13693 /* Has this bp already been deleted? This can happen because
13694 multiple lists can hold pointers to bp's. bpstat lists are
13695 especial culprits.
13696
13697 One example of this happening is a watchpoint's scope bp. When
13698 the scope bp triggers, we notice that the watchpoint is out of
13699 scope, and delete it. We also delete its scope bp. But the
13700 scope bp is marked "auto-deleting", and is already on a bpstat.
13701 That bpstat is then checked for auto-deleting bp's, which are
13702 deleted.
13703
13704 A real solution to this problem might involve reference counts in
13705 bp's, and/or giving them pointers back to their referencing
13706 bpstat's, and teaching delete_breakpoint to only free a bp's
13707 storage when no more references were extent. A cheaper bandaid
13708 was chosen. */
13709 if (bpt->type == bp_none)
13710 return;
13711
13712 /* At least avoid this stale reference until the reference counting
13713 of breakpoints gets resolved. */
13714 if (bpt->related_breakpoint != bpt)
13715 {
13716 struct breakpoint *related;
13717 struct watchpoint *w;
13718
13719 if (bpt->type == bp_watchpoint_scope)
13720 w = (struct watchpoint *) bpt->related_breakpoint;
13721 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13722 w = (struct watchpoint *) bpt;
13723 else
13724 w = NULL;
13725 if (w != NULL)
13726 watchpoint_del_at_next_stop (w);
13727
13728 /* Unlink bpt from the bpt->related_breakpoint ring. */
13729 for (related = bpt; related->related_breakpoint != bpt;
13730 related = related->related_breakpoint);
13731 related->related_breakpoint = bpt->related_breakpoint;
13732 bpt->related_breakpoint = bpt;
13733 }
13734
13735 /* watch_command_1 creates a watchpoint but only sets its number if
13736 update_watchpoint succeeds in creating its bp_locations. If there's
13737 a problem in that process, we'll be asked to delete the half-created
13738 watchpoint. In that case, don't announce the deletion. */
13739 if (bpt->number)
13740 observer_notify_breakpoint_deleted (bpt);
13741
13742 if (breakpoint_chain == bpt)
13743 breakpoint_chain = bpt->next;
13744
13745 ALL_BREAKPOINTS (b)
13746 if (b->next == bpt)
13747 {
13748 b->next = bpt->next;
13749 break;
13750 }
13751
13752 /* Be sure no bpstat's are pointing at the breakpoint after it's
13753 been freed. */
13754 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13755 in all threads for now. Note that we cannot just remove bpstats
13756 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13757 commands are associated with the bpstat; if we remove it here,
13758 then the later call to bpstat_do_actions (&stop_bpstat); in
13759 event-top.c won't do anything, and temporary breakpoints with
13760 commands won't work. */
13761
13762 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13763
13764 /* Now that breakpoint is removed from breakpoint list, update the
13765 global location list. This will remove locations that used to
13766 belong to this breakpoint. Do this before freeing the breakpoint
13767 itself, since remove_breakpoint looks at location's owner. It
13768 might be better design to have location completely
13769 self-contained, but it's not the case now. */
13770 update_global_location_list (UGLL_DONT_INSERT);
13771
13772 bpt->ops->dtor (bpt);
13773 /* On the chance that someone will soon try again to delete this
13774 same bp, we mark it as deleted before freeing its storage. */
13775 bpt->type = bp_none;
13776 delete bpt;
13777 }
13778
13779 static void
13780 do_delete_breakpoint_cleanup (void *b)
13781 {
13782 delete_breakpoint ((struct breakpoint *) b);
13783 }
13784
13785 struct cleanup *
13786 make_cleanup_delete_breakpoint (struct breakpoint *b)
13787 {
13788 return make_cleanup (do_delete_breakpoint_cleanup, b);
13789 }
13790
13791 /* Iterator function to call a user-provided callback function once
13792 for each of B and its related breakpoints. */
13793
13794 static void
13795 iterate_over_related_breakpoints (struct breakpoint *b,
13796 void (*function) (struct breakpoint *,
13797 void *),
13798 void *data)
13799 {
13800 struct breakpoint *related;
13801
13802 related = b;
13803 do
13804 {
13805 struct breakpoint *next;
13806
13807 /* FUNCTION may delete RELATED. */
13808 next = related->related_breakpoint;
13809
13810 if (next == related)
13811 {
13812 /* RELATED is the last ring entry. */
13813 function (related, data);
13814
13815 /* FUNCTION may have deleted it, so we'd never reach back to
13816 B. There's nothing left to do anyway, so just break
13817 out. */
13818 break;
13819 }
13820 else
13821 function (related, data);
13822
13823 related = next;
13824 }
13825 while (related != b);
13826 }
13827
13828 static void
13829 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13830 {
13831 delete_breakpoint (b);
13832 }
13833
13834 /* A callback for map_breakpoint_numbers that calls
13835 delete_breakpoint. */
13836
13837 static void
13838 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13839 {
13840 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13841 }
13842
13843 void
13844 delete_command (char *arg, int from_tty)
13845 {
13846 struct breakpoint *b, *b_tmp;
13847
13848 dont_repeat ();
13849
13850 if (arg == 0)
13851 {
13852 int breaks_to_delete = 0;
13853
13854 /* Delete all breakpoints if no argument. Do not delete
13855 internal breakpoints, these have to be deleted with an
13856 explicit breakpoint number argument. */
13857 ALL_BREAKPOINTS (b)
13858 if (user_breakpoint_p (b))
13859 {
13860 breaks_to_delete = 1;
13861 break;
13862 }
13863
13864 /* Ask user only if there are some breakpoints to delete. */
13865 if (!from_tty
13866 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13867 {
13868 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13869 if (user_breakpoint_p (b))
13870 delete_breakpoint (b);
13871 }
13872 }
13873 else
13874 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13875 }
13876
13877 /* Return true if all locations of B bound to PSPACE are pending. If
13878 PSPACE is NULL, all locations of all program spaces are
13879 considered. */
13880
13881 static int
13882 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13883 {
13884 struct bp_location *loc;
13885
13886 for (loc = b->loc; loc != NULL; loc = loc->next)
13887 if ((pspace == NULL
13888 || loc->pspace == pspace)
13889 && !loc->shlib_disabled
13890 && !loc->pspace->executing_startup)
13891 return 0;
13892 return 1;
13893 }
13894
13895 /* Subroutine of update_breakpoint_locations to simplify it.
13896 Return non-zero if multiple fns in list LOC have the same name.
13897 Null names are ignored. */
13898
13899 static int
13900 ambiguous_names_p (struct bp_location *loc)
13901 {
13902 struct bp_location *l;
13903 htab_t htab = htab_create_alloc (13, htab_hash_string,
13904 (int (*) (const void *,
13905 const void *)) streq,
13906 NULL, xcalloc, xfree);
13907
13908 for (l = loc; l != NULL; l = l->next)
13909 {
13910 const char **slot;
13911 const char *name = l->function_name;
13912
13913 /* Allow for some names to be NULL, ignore them. */
13914 if (name == NULL)
13915 continue;
13916
13917 slot = (const char **) htab_find_slot (htab, (const void *) name,
13918 INSERT);
13919 /* NOTE: We can assume slot != NULL here because xcalloc never
13920 returns NULL. */
13921 if (*slot != NULL)
13922 {
13923 htab_delete (htab);
13924 return 1;
13925 }
13926 *slot = name;
13927 }
13928
13929 htab_delete (htab);
13930 return 0;
13931 }
13932
13933 /* When symbols change, it probably means the sources changed as well,
13934 and it might mean the static tracepoint markers are no longer at
13935 the same address or line numbers they used to be at last we
13936 checked. Losing your static tracepoints whenever you rebuild is
13937 undesirable. This function tries to resync/rematch gdb static
13938 tracepoints with the markers on the target, for static tracepoints
13939 that have not been set by marker id. Static tracepoint that have
13940 been set by marker id are reset by marker id in breakpoint_re_set.
13941 The heuristic is:
13942
13943 1) For a tracepoint set at a specific address, look for a marker at
13944 the old PC. If one is found there, assume to be the same marker.
13945 If the name / string id of the marker found is different from the
13946 previous known name, assume that means the user renamed the marker
13947 in the sources, and output a warning.
13948
13949 2) For a tracepoint set at a given line number, look for a marker
13950 at the new address of the old line number. If one is found there,
13951 assume to be the same marker. If the name / string id of the
13952 marker found is different from the previous known name, assume that
13953 means the user renamed the marker in the sources, and output a
13954 warning.
13955
13956 3) If a marker is no longer found at the same address or line, it
13957 may mean the marker no longer exists. But it may also just mean
13958 the code changed a bit. Maybe the user added a few lines of code
13959 that made the marker move up or down (in line number terms). Ask
13960 the target for info about the marker with the string id as we knew
13961 it. If found, update line number and address in the matching
13962 static tracepoint. This will get confused if there's more than one
13963 marker with the same ID (possible in UST, although unadvised
13964 precisely because it confuses tools). */
13965
13966 static struct symtab_and_line
13967 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13968 {
13969 struct tracepoint *tp = (struct tracepoint *) b;
13970 struct static_tracepoint_marker marker;
13971 CORE_ADDR pc;
13972
13973 pc = sal.pc;
13974 if (sal.line)
13975 find_line_pc (sal.symtab, sal.line, &pc);
13976
13977 if (target_static_tracepoint_marker_at (pc, &marker))
13978 {
13979 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13980 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13981 b->number,
13982 tp->static_trace_marker_id, marker.str_id);
13983
13984 xfree (tp->static_trace_marker_id);
13985 tp->static_trace_marker_id = xstrdup (marker.str_id);
13986 release_static_tracepoint_marker (&marker);
13987
13988 return sal;
13989 }
13990
13991 /* Old marker wasn't found on target at lineno. Try looking it up
13992 by string ID. */
13993 if (!sal.explicit_pc
13994 && sal.line != 0
13995 && sal.symtab != NULL
13996 && tp->static_trace_marker_id != NULL)
13997 {
13998 VEC(static_tracepoint_marker_p) *markers;
13999
14000 markers
14001 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14002
14003 if (!VEC_empty(static_tracepoint_marker_p, markers))
14004 {
14005 struct symtab_and_line sal2;
14006 struct symbol *sym;
14007 struct static_tracepoint_marker *tpmarker;
14008 struct ui_out *uiout = current_uiout;
14009 struct explicit_location explicit_loc;
14010
14011 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14012
14013 xfree (tp->static_trace_marker_id);
14014 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14015
14016 warning (_("marker for static tracepoint %d (%s) not "
14017 "found at previous line number"),
14018 b->number, tp->static_trace_marker_id);
14019
14020 init_sal (&sal2);
14021
14022 sal2.pc = tpmarker->address;
14023
14024 sal2 = find_pc_line (tpmarker->address, 0);
14025 sym = find_pc_sect_function (tpmarker->address, NULL);
14026 uiout->text ("Now in ");
14027 if (sym)
14028 {
14029 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14030 uiout->text (" at ");
14031 }
14032 uiout->field_string ("file",
14033 symtab_to_filename_for_display (sal2.symtab));
14034 uiout->text (":");
14035
14036 if (uiout->is_mi_like_p ())
14037 {
14038 const char *fullname = symtab_to_fullname (sal2.symtab);
14039
14040 uiout->field_string ("fullname", fullname);
14041 }
14042
14043 uiout->field_int ("line", sal2.line);
14044 uiout->text ("\n");
14045
14046 b->loc->line_number = sal2.line;
14047 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14048
14049 b->location.reset (NULL);
14050 initialize_explicit_location (&explicit_loc);
14051 explicit_loc.source_filename
14052 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14053 explicit_loc.line_offset.offset = b->loc->line_number;
14054 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14055 b->location = new_explicit_location (&explicit_loc);
14056
14057 /* Might be nice to check if function changed, and warn if
14058 so. */
14059
14060 release_static_tracepoint_marker (tpmarker);
14061 }
14062 }
14063 return sal;
14064 }
14065
14066 /* Returns 1 iff locations A and B are sufficiently same that
14067 we don't need to report breakpoint as changed. */
14068
14069 static int
14070 locations_are_equal (struct bp_location *a, struct bp_location *b)
14071 {
14072 while (a && b)
14073 {
14074 if (a->address != b->address)
14075 return 0;
14076
14077 if (a->shlib_disabled != b->shlib_disabled)
14078 return 0;
14079
14080 if (a->enabled != b->enabled)
14081 return 0;
14082
14083 a = a->next;
14084 b = b->next;
14085 }
14086
14087 if ((a == NULL) != (b == NULL))
14088 return 0;
14089
14090 return 1;
14091 }
14092
14093 /* Split all locations of B that are bound to PSPACE out of B's
14094 location list to a separate list and return that list's head. If
14095 PSPACE is NULL, hoist out all locations of B. */
14096
14097 static struct bp_location *
14098 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14099 {
14100 struct bp_location head;
14101 struct bp_location *i = b->loc;
14102 struct bp_location **i_link = &b->loc;
14103 struct bp_location *hoisted = &head;
14104
14105 if (pspace == NULL)
14106 {
14107 i = b->loc;
14108 b->loc = NULL;
14109 return i;
14110 }
14111
14112 head.next = NULL;
14113
14114 while (i != NULL)
14115 {
14116 if (i->pspace == pspace)
14117 {
14118 *i_link = i->next;
14119 i->next = NULL;
14120 hoisted->next = i;
14121 hoisted = i;
14122 }
14123 else
14124 i_link = &i->next;
14125 i = *i_link;
14126 }
14127
14128 return head.next;
14129 }
14130
14131 /* Create new breakpoint locations for B (a hardware or software
14132 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14133 zero, then B is a ranged breakpoint. Only recreates locations for
14134 FILTER_PSPACE. Locations of other program spaces are left
14135 untouched. */
14136
14137 void
14138 update_breakpoint_locations (struct breakpoint *b,
14139 struct program_space *filter_pspace,
14140 struct symtabs_and_lines sals,
14141 struct symtabs_and_lines sals_end)
14142 {
14143 int i;
14144 struct bp_location *existing_locations;
14145
14146 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14147 {
14148 /* Ranged breakpoints have only one start location and one end
14149 location. */
14150 b->enable_state = bp_disabled;
14151 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14152 "multiple locations found\n"),
14153 b->number);
14154 return;
14155 }
14156
14157 /* If there's no new locations, and all existing locations are
14158 pending, don't do anything. This optimizes the common case where
14159 all locations are in the same shared library, that was unloaded.
14160 We'd like to retain the location, so that when the library is
14161 loaded again, we don't loose the enabled/disabled status of the
14162 individual locations. */
14163 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14164 return;
14165
14166 existing_locations = hoist_existing_locations (b, filter_pspace);
14167
14168 for (i = 0; i < sals.nelts; ++i)
14169 {
14170 struct bp_location *new_loc;
14171
14172 switch_to_program_space_and_thread (sals.sals[i].pspace);
14173
14174 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14175
14176 /* Reparse conditions, they might contain references to the
14177 old symtab. */
14178 if (b->cond_string != NULL)
14179 {
14180 const char *s;
14181
14182 s = b->cond_string;
14183 TRY
14184 {
14185 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14186 block_for_pc (sals.sals[i].pc),
14187 0);
14188 }
14189 CATCH (e, RETURN_MASK_ERROR)
14190 {
14191 warning (_("failed to reevaluate condition "
14192 "for breakpoint %d: %s"),
14193 b->number, e.message);
14194 new_loc->enabled = 0;
14195 }
14196 END_CATCH
14197 }
14198
14199 if (sals_end.nelts)
14200 {
14201 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14202
14203 new_loc->length = end - sals.sals[0].pc + 1;
14204 }
14205 }
14206
14207 /* If possible, carry over 'disable' status from existing
14208 breakpoints. */
14209 {
14210 struct bp_location *e = existing_locations;
14211 /* If there are multiple breakpoints with the same function name,
14212 e.g. for inline functions, comparing function names won't work.
14213 Instead compare pc addresses; this is just a heuristic as things
14214 may have moved, but in practice it gives the correct answer
14215 often enough until a better solution is found. */
14216 int have_ambiguous_names = ambiguous_names_p (b->loc);
14217
14218 for (; e; e = e->next)
14219 {
14220 if (!e->enabled && e->function_name)
14221 {
14222 struct bp_location *l = b->loc;
14223 if (have_ambiguous_names)
14224 {
14225 for (; l; l = l->next)
14226 if (breakpoint_locations_match (e, l))
14227 {
14228 l->enabled = 0;
14229 break;
14230 }
14231 }
14232 else
14233 {
14234 for (; l; l = l->next)
14235 if (l->function_name
14236 && strcmp (e->function_name, l->function_name) == 0)
14237 {
14238 l->enabled = 0;
14239 break;
14240 }
14241 }
14242 }
14243 }
14244 }
14245
14246 if (!locations_are_equal (existing_locations, b->loc))
14247 observer_notify_breakpoint_modified (b);
14248 }
14249
14250 /* Find the SaL locations corresponding to the given LOCATION.
14251 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14252
14253 static struct symtabs_and_lines
14254 location_to_sals (struct breakpoint *b, struct event_location *location,
14255 struct program_space *search_pspace, int *found)
14256 {
14257 struct symtabs_and_lines sals = {0};
14258 struct gdb_exception exception = exception_none;
14259
14260 gdb_assert (b->ops != NULL);
14261
14262 TRY
14263 {
14264 b->ops->decode_location (b, location, search_pspace, &sals);
14265 }
14266 CATCH (e, RETURN_MASK_ERROR)
14267 {
14268 int not_found_and_ok = 0;
14269
14270 exception = e;
14271
14272 /* For pending breakpoints, it's expected that parsing will
14273 fail until the right shared library is loaded. User has
14274 already told to create pending breakpoints and don't need
14275 extra messages. If breakpoint is in bp_shlib_disabled
14276 state, then user already saw the message about that
14277 breakpoint being disabled, and don't want to see more
14278 errors. */
14279 if (e.error == NOT_FOUND_ERROR
14280 && (b->condition_not_parsed
14281 || (b->loc != NULL
14282 && search_pspace != NULL
14283 && b->loc->pspace != search_pspace)
14284 || (b->loc && b->loc->shlib_disabled)
14285 || (b->loc && b->loc->pspace->executing_startup)
14286 || b->enable_state == bp_disabled))
14287 not_found_and_ok = 1;
14288
14289 if (!not_found_and_ok)
14290 {
14291 /* We surely don't want to warn about the same breakpoint
14292 10 times. One solution, implemented here, is disable
14293 the breakpoint on error. Another solution would be to
14294 have separate 'warning emitted' flag. Since this
14295 happens only when a binary has changed, I don't know
14296 which approach is better. */
14297 b->enable_state = bp_disabled;
14298 throw_exception (e);
14299 }
14300 }
14301 END_CATCH
14302
14303 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14304 {
14305 int i;
14306
14307 for (i = 0; i < sals.nelts; ++i)
14308 resolve_sal_pc (&sals.sals[i]);
14309 if (b->condition_not_parsed && b->extra_string != NULL)
14310 {
14311 char *cond_string, *extra_string;
14312 int thread, task;
14313
14314 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14315 &cond_string, &thread, &task,
14316 &extra_string);
14317 gdb_assert (b->cond_string == NULL);
14318 if (cond_string)
14319 b->cond_string = cond_string;
14320 b->thread = thread;
14321 b->task = task;
14322 if (extra_string)
14323 {
14324 xfree (b->extra_string);
14325 b->extra_string = extra_string;
14326 }
14327 b->condition_not_parsed = 0;
14328 }
14329
14330 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14331 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14332
14333 *found = 1;
14334 }
14335 else
14336 *found = 0;
14337
14338 return sals;
14339 }
14340
14341 /* The default re_set method, for typical hardware or software
14342 breakpoints. Reevaluate the breakpoint and recreate its
14343 locations. */
14344
14345 static void
14346 breakpoint_re_set_default (struct breakpoint *b)
14347 {
14348 int found;
14349 struct symtabs_and_lines sals, sals_end;
14350 struct symtabs_and_lines expanded = {0};
14351 struct symtabs_and_lines expanded_end = {0};
14352 struct program_space *filter_pspace = current_program_space;
14353
14354 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14355 if (found)
14356 {
14357 make_cleanup (xfree, sals.sals);
14358 expanded = sals;
14359 }
14360
14361 if (b->location_range_end != NULL)
14362 {
14363 sals_end = location_to_sals (b, b->location_range_end.get (),
14364 filter_pspace, &found);
14365 if (found)
14366 {
14367 make_cleanup (xfree, sals_end.sals);
14368 expanded_end = sals_end;
14369 }
14370 }
14371
14372 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14373 }
14374
14375 /* Default method for creating SALs from an address string. It basically
14376 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14377
14378 static void
14379 create_sals_from_location_default (const struct event_location *location,
14380 struct linespec_result *canonical,
14381 enum bptype type_wanted)
14382 {
14383 parse_breakpoint_sals (location, canonical);
14384 }
14385
14386 /* Call create_breakpoints_sal for the given arguments. This is the default
14387 function for the `create_breakpoints_sal' method of
14388 breakpoint_ops. */
14389
14390 static void
14391 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14392 struct linespec_result *canonical,
14393 char *cond_string,
14394 char *extra_string,
14395 enum bptype type_wanted,
14396 enum bpdisp disposition,
14397 int thread,
14398 int task, int ignore_count,
14399 const struct breakpoint_ops *ops,
14400 int from_tty, int enabled,
14401 int internal, unsigned flags)
14402 {
14403 create_breakpoints_sal (gdbarch, canonical, cond_string,
14404 extra_string,
14405 type_wanted, disposition,
14406 thread, task, ignore_count, ops, from_tty,
14407 enabled, internal, flags);
14408 }
14409
14410 /* Decode the line represented by S by calling decode_line_full. This is the
14411 default function for the `decode_location' method of breakpoint_ops. */
14412
14413 static void
14414 decode_location_default (struct breakpoint *b,
14415 const struct event_location *location,
14416 struct program_space *search_pspace,
14417 struct symtabs_and_lines *sals)
14418 {
14419 struct linespec_result canonical;
14420
14421 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14422 (struct symtab *) NULL, 0,
14423 &canonical, multiple_symbols_all,
14424 b->filter);
14425
14426 /* We should get 0 or 1 resulting SALs. */
14427 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14428
14429 if (VEC_length (linespec_sals, canonical.sals) > 0)
14430 {
14431 struct linespec_sals *lsal;
14432
14433 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14434 *sals = lsal->sals;
14435 /* Arrange it so the destructor does not free the
14436 contents. */
14437 lsal->sals.sals = NULL;
14438 }
14439 }
14440
14441 /* Prepare the global context for a re-set of breakpoint B. */
14442
14443 static struct cleanup *
14444 prepare_re_set_context (struct breakpoint *b)
14445 {
14446 input_radix = b->input_radix;
14447 set_language (b->language);
14448
14449 return make_cleanup (null_cleanup, NULL);
14450 }
14451
14452 /* Reset a breakpoint given it's struct breakpoint * BINT.
14453 The value we return ends up being the return value from catch_errors.
14454 Unused in this case. */
14455
14456 static int
14457 breakpoint_re_set_one (void *bint)
14458 {
14459 /* Get past catch_errs. */
14460 struct breakpoint *b = (struct breakpoint *) bint;
14461 struct cleanup *cleanups;
14462
14463 cleanups = prepare_re_set_context (b);
14464 b->ops->re_set (b);
14465 do_cleanups (cleanups);
14466 return 0;
14467 }
14468
14469 /* Re-set breakpoint locations for the current program space.
14470 Locations bound to other program spaces are left untouched. */
14471
14472 void
14473 breakpoint_re_set (void)
14474 {
14475 struct breakpoint *b, *b_tmp;
14476 enum language save_language;
14477 int save_input_radix;
14478
14479 save_language = current_language->la_language;
14480 save_input_radix = input_radix;
14481
14482 {
14483 scoped_restore_current_pspace_and_thread restore_pspace_thread;
14484
14485 /* Note: we must not try to insert locations until after all
14486 breakpoints have been re-set. Otherwise, e.g., when re-setting
14487 breakpoint 1, we'd insert the locations of breakpoint 2, which
14488 hadn't been re-set yet, and thus may have stale locations. */
14489
14490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14491 {
14492 /* Format possible error msg. */
14493 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14494 b->number);
14495 struct cleanup *cleanups = make_cleanup (xfree, message);
14496 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14497 do_cleanups (cleanups);
14498 }
14499 set_language (save_language);
14500 input_radix = save_input_radix;
14501
14502 jit_breakpoint_re_set ();
14503 }
14504
14505 create_overlay_event_breakpoint ();
14506 create_longjmp_master_breakpoint ();
14507 create_std_terminate_master_breakpoint ();
14508 create_exception_master_breakpoint ();
14509
14510 /* Now we can insert. */
14511 update_global_location_list (UGLL_MAY_INSERT);
14512 }
14513 \f
14514 /* Reset the thread number of this breakpoint:
14515
14516 - If the breakpoint is for all threads, leave it as-is.
14517 - Else, reset it to the current thread for inferior_ptid. */
14518 void
14519 breakpoint_re_set_thread (struct breakpoint *b)
14520 {
14521 if (b->thread != -1)
14522 {
14523 if (in_thread_list (inferior_ptid))
14524 b->thread = ptid_to_global_thread_id (inferior_ptid);
14525
14526 /* We're being called after following a fork. The new fork is
14527 selected as current, and unless this was a vfork will have a
14528 different program space from the original thread. Reset that
14529 as well. */
14530 b->loc->pspace = current_program_space;
14531 }
14532 }
14533
14534 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14535 If from_tty is nonzero, it prints a message to that effect,
14536 which ends with a period (no newline). */
14537
14538 void
14539 set_ignore_count (int bptnum, int count, int from_tty)
14540 {
14541 struct breakpoint *b;
14542
14543 if (count < 0)
14544 count = 0;
14545
14546 ALL_BREAKPOINTS (b)
14547 if (b->number == bptnum)
14548 {
14549 if (is_tracepoint (b))
14550 {
14551 if (from_tty && count != 0)
14552 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14553 bptnum);
14554 return;
14555 }
14556
14557 b->ignore_count = count;
14558 if (from_tty)
14559 {
14560 if (count == 0)
14561 printf_filtered (_("Will stop next time "
14562 "breakpoint %d is reached."),
14563 bptnum);
14564 else if (count == 1)
14565 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14566 bptnum);
14567 else
14568 printf_filtered (_("Will ignore next %d "
14569 "crossings of breakpoint %d."),
14570 count, bptnum);
14571 }
14572 observer_notify_breakpoint_modified (b);
14573 return;
14574 }
14575
14576 error (_("No breakpoint number %d."), bptnum);
14577 }
14578
14579 /* Command to set ignore-count of breakpoint N to COUNT. */
14580
14581 static void
14582 ignore_command (char *args, int from_tty)
14583 {
14584 char *p = args;
14585 int num;
14586
14587 if (p == 0)
14588 error_no_arg (_("a breakpoint number"));
14589
14590 num = get_number (&p);
14591 if (num == 0)
14592 error (_("bad breakpoint number: '%s'"), args);
14593 if (*p == 0)
14594 error (_("Second argument (specified ignore-count) is missing."));
14595
14596 set_ignore_count (num,
14597 longest_to_int (value_as_long (parse_and_eval (p))),
14598 from_tty);
14599 if (from_tty)
14600 printf_filtered ("\n");
14601 }
14602 \f
14603 /* Call FUNCTION on each of the breakpoints
14604 whose numbers are given in ARGS. */
14605
14606 static void
14607 map_breakpoint_numbers (const char *args,
14608 void (*function) (struct breakpoint *,
14609 void *),
14610 void *data)
14611 {
14612 int num;
14613 struct breakpoint *b, *tmp;
14614
14615 if (args == 0 || *args == '\0')
14616 error_no_arg (_("one or more breakpoint numbers"));
14617
14618 number_or_range_parser parser (args);
14619
14620 while (!parser.finished ())
14621 {
14622 const char *p = parser.cur_tok ();
14623 bool match = false;
14624
14625 num = parser.get_number ();
14626 if (num == 0)
14627 {
14628 warning (_("bad breakpoint number at or near '%s'"), p);
14629 }
14630 else
14631 {
14632 ALL_BREAKPOINTS_SAFE (b, tmp)
14633 if (b->number == num)
14634 {
14635 match = true;
14636 function (b, data);
14637 break;
14638 }
14639 if (!match)
14640 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14641 }
14642 }
14643 }
14644
14645 static struct bp_location *
14646 find_location_by_number (char *number)
14647 {
14648 char *dot = strchr (number, '.');
14649 char *p1;
14650 int bp_num;
14651 int loc_num;
14652 struct breakpoint *b;
14653 struct bp_location *loc;
14654
14655 *dot = '\0';
14656
14657 p1 = number;
14658 bp_num = get_number (&p1);
14659 if (bp_num == 0)
14660 error (_("Bad breakpoint number '%s'"), number);
14661
14662 ALL_BREAKPOINTS (b)
14663 if (b->number == bp_num)
14664 {
14665 break;
14666 }
14667
14668 if (!b || b->number != bp_num)
14669 error (_("Bad breakpoint number '%s'"), number);
14670
14671 p1 = dot+1;
14672 loc_num = get_number (&p1);
14673 if (loc_num == 0)
14674 error (_("Bad breakpoint location number '%s'"), number);
14675
14676 --loc_num;
14677 loc = b->loc;
14678 for (;loc_num && loc; --loc_num, loc = loc->next)
14679 ;
14680 if (!loc)
14681 error (_("Bad breakpoint location number '%s'"), dot+1);
14682
14683 return loc;
14684 }
14685
14686
14687 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14688 If from_tty is nonzero, it prints a message to that effect,
14689 which ends with a period (no newline). */
14690
14691 void
14692 disable_breakpoint (struct breakpoint *bpt)
14693 {
14694 /* Never disable a watchpoint scope breakpoint; we want to
14695 hit them when we leave scope so we can delete both the
14696 watchpoint and its scope breakpoint at that time. */
14697 if (bpt->type == bp_watchpoint_scope)
14698 return;
14699
14700 bpt->enable_state = bp_disabled;
14701
14702 /* Mark breakpoint locations modified. */
14703 mark_breakpoint_modified (bpt);
14704
14705 if (target_supports_enable_disable_tracepoint ()
14706 && current_trace_status ()->running && is_tracepoint (bpt))
14707 {
14708 struct bp_location *location;
14709
14710 for (location = bpt->loc; location; location = location->next)
14711 target_disable_tracepoint (location);
14712 }
14713
14714 update_global_location_list (UGLL_DONT_INSERT);
14715
14716 observer_notify_breakpoint_modified (bpt);
14717 }
14718
14719 /* A callback for iterate_over_related_breakpoints. */
14720
14721 static void
14722 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14723 {
14724 disable_breakpoint (b);
14725 }
14726
14727 /* A callback for map_breakpoint_numbers that calls
14728 disable_breakpoint. */
14729
14730 static void
14731 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14732 {
14733 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14734 }
14735
14736 static void
14737 disable_command (char *args, int from_tty)
14738 {
14739 if (args == 0)
14740 {
14741 struct breakpoint *bpt;
14742
14743 ALL_BREAKPOINTS (bpt)
14744 if (user_breakpoint_p (bpt))
14745 disable_breakpoint (bpt);
14746 }
14747 else
14748 {
14749 char *num = extract_arg (&args);
14750
14751 while (num)
14752 {
14753 if (strchr (num, '.'))
14754 {
14755 struct bp_location *loc = find_location_by_number (num);
14756
14757 if (loc)
14758 {
14759 if (loc->enabled)
14760 {
14761 loc->enabled = 0;
14762 mark_breakpoint_location_modified (loc);
14763 }
14764 if (target_supports_enable_disable_tracepoint ()
14765 && current_trace_status ()->running && loc->owner
14766 && is_tracepoint (loc->owner))
14767 target_disable_tracepoint (loc);
14768 }
14769 update_global_location_list (UGLL_DONT_INSERT);
14770 }
14771 else
14772 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14773 num = extract_arg (&args);
14774 }
14775 }
14776 }
14777
14778 static void
14779 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14780 int count)
14781 {
14782 int target_resources_ok;
14783
14784 if (bpt->type == bp_hardware_breakpoint)
14785 {
14786 int i;
14787 i = hw_breakpoint_used_count ();
14788 target_resources_ok =
14789 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14790 i + 1, 0);
14791 if (target_resources_ok == 0)
14792 error (_("No hardware breakpoint support in the target."));
14793 else if (target_resources_ok < 0)
14794 error (_("Hardware breakpoints used exceeds limit."));
14795 }
14796
14797 if (is_watchpoint (bpt))
14798 {
14799 /* Initialize it just to avoid a GCC false warning. */
14800 enum enable_state orig_enable_state = bp_disabled;
14801
14802 TRY
14803 {
14804 struct watchpoint *w = (struct watchpoint *) bpt;
14805
14806 orig_enable_state = bpt->enable_state;
14807 bpt->enable_state = bp_enabled;
14808 update_watchpoint (w, 1 /* reparse */);
14809 }
14810 CATCH (e, RETURN_MASK_ALL)
14811 {
14812 bpt->enable_state = orig_enable_state;
14813 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14814 bpt->number);
14815 return;
14816 }
14817 END_CATCH
14818 }
14819
14820 bpt->enable_state = bp_enabled;
14821
14822 /* Mark breakpoint locations modified. */
14823 mark_breakpoint_modified (bpt);
14824
14825 if (target_supports_enable_disable_tracepoint ()
14826 && current_trace_status ()->running && is_tracepoint (bpt))
14827 {
14828 struct bp_location *location;
14829
14830 for (location = bpt->loc; location; location = location->next)
14831 target_enable_tracepoint (location);
14832 }
14833
14834 bpt->disposition = disposition;
14835 bpt->enable_count = count;
14836 update_global_location_list (UGLL_MAY_INSERT);
14837
14838 observer_notify_breakpoint_modified (bpt);
14839 }
14840
14841
14842 void
14843 enable_breakpoint (struct breakpoint *bpt)
14844 {
14845 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14846 }
14847
14848 static void
14849 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14850 {
14851 enable_breakpoint (bpt);
14852 }
14853
14854 /* A callback for map_breakpoint_numbers that calls
14855 enable_breakpoint. */
14856
14857 static void
14858 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14859 {
14860 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14861 }
14862
14863 /* The enable command enables the specified breakpoints (or all defined
14864 breakpoints) so they once again become (or continue to be) effective
14865 in stopping the inferior. */
14866
14867 static void
14868 enable_command (char *args, int from_tty)
14869 {
14870 if (args == 0)
14871 {
14872 struct breakpoint *bpt;
14873
14874 ALL_BREAKPOINTS (bpt)
14875 if (user_breakpoint_p (bpt))
14876 enable_breakpoint (bpt);
14877 }
14878 else
14879 {
14880 char *num = extract_arg (&args);
14881
14882 while (num)
14883 {
14884 if (strchr (num, '.'))
14885 {
14886 struct bp_location *loc = find_location_by_number (num);
14887
14888 if (loc)
14889 {
14890 if (!loc->enabled)
14891 {
14892 loc->enabled = 1;
14893 mark_breakpoint_location_modified (loc);
14894 }
14895 if (target_supports_enable_disable_tracepoint ()
14896 && current_trace_status ()->running && loc->owner
14897 && is_tracepoint (loc->owner))
14898 target_enable_tracepoint (loc);
14899 }
14900 update_global_location_list (UGLL_MAY_INSERT);
14901 }
14902 else
14903 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14904 num = extract_arg (&args);
14905 }
14906 }
14907 }
14908
14909 /* This struct packages up disposition data for application to multiple
14910 breakpoints. */
14911
14912 struct disp_data
14913 {
14914 enum bpdisp disp;
14915 int count;
14916 };
14917
14918 static void
14919 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14920 {
14921 struct disp_data disp_data = *(struct disp_data *) arg;
14922
14923 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14924 }
14925
14926 static void
14927 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14928 {
14929 struct disp_data disp = { disp_disable, 1 };
14930
14931 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14932 }
14933
14934 static void
14935 enable_once_command (char *args, int from_tty)
14936 {
14937 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14938 }
14939
14940 static void
14941 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14942 {
14943 struct disp_data disp = { disp_disable, *(int *) countptr };
14944
14945 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14946 }
14947
14948 static void
14949 enable_count_command (char *args, int from_tty)
14950 {
14951 int count;
14952
14953 if (args == NULL)
14954 error_no_arg (_("hit count"));
14955
14956 count = get_number (&args);
14957
14958 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14959 }
14960
14961 static void
14962 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14963 {
14964 struct disp_data disp = { disp_del, 1 };
14965
14966 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14967 }
14968
14969 static void
14970 enable_delete_command (char *args, int from_tty)
14971 {
14972 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14973 }
14974 \f
14975 static void
14976 set_breakpoint_cmd (char *args, int from_tty)
14977 {
14978 }
14979
14980 static void
14981 show_breakpoint_cmd (char *args, int from_tty)
14982 {
14983 }
14984
14985 /* Invalidate last known value of any hardware watchpoint if
14986 the memory which that value represents has been written to by
14987 GDB itself. */
14988
14989 static void
14990 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14991 CORE_ADDR addr, ssize_t len,
14992 const bfd_byte *data)
14993 {
14994 struct breakpoint *bp;
14995
14996 ALL_BREAKPOINTS (bp)
14997 if (bp->enable_state == bp_enabled
14998 && bp->type == bp_hardware_watchpoint)
14999 {
15000 struct watchpoint *wp = (struct watchpoint *) bp;
15001
15002 if (wp->val_valid && wp->val)
15003 {
15004 struct bp_location *loc;
15005
15006 for (loc = bp->loc; loc != NULL; loc = loc->next)
15007 if (loc->loc_type == bp_loc_hardware_watchpoint
15008 && loc->address + loc->length > addr
15009 && addr + len > loc->address)
15010 {
15011 value_free (wp->val);
15012 wp->val = NULL;
15013 wp->val_valid = 0;
15014 }
15015 }
15016 }
15017 }
15018
15019 /* Create and insert a breakpoint for software single step. */
15020
15021 void
15022 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15023 struct address_space *aspace,
15024 CORE_ADDR next_pc)
15025 {
15026 struct thread_info *tp = inferior_thread ();
15027 struct symtab_and_line sal;
15028 CORE_ADDR pc = next_pc;
15029
15030 if (tp->control.single_step_breakpoints == NULL)
15031 {
15032 tp->control.single_step_breakpoints
15033 = new_single_step_breakpoint (tp->global_num, gdbarch);
15034 }
15035
15036 sal = find_pc_line (pc, 0);
15037 sal.pc = pc;
15038 sal.section = find_pc_overlay (pc);
15039 sal.explicit_pc = 1;
15040 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15041
15042 update_global_location_list (UGLL_INSERT);
15043 }
15044
15045 /* Insert single step breakpoints according to the current state. */
15046
15047 int
15048 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15049 {
15050 struct regcache *regcache = get_current_regcache ();
15051 std::vector<CORE_ADDR> next_pcs;
15052
15053 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15054
15055 if (!next_pcs.empty ())
15056 {
15057 struct frame_info *frame = get_current_frame ();
15058 struct address_space *aspace = get_frame_address_space (frame);
15059
15060 for (CORE_ADDR pc : next_pcs)
15061 insert_single_step_breakpoint (gdbarch, aspace, pc);
15062
15063 return 1;
15064 }
15065 else
15066 return 0;
15067 }
15068
15069 /* See breakpoint.h. */
15070
15071 int
15072 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15073 struct address_space *aspace,
15074 CORE_ADDR pc)
15075 {
15076 struct bp_location *loc;
15077
15078 for (loc = bp->loc; loc != NULL; loc = loc->next)
15079 if (loc->inserted
15080 && breakpoint_location_address_match (loc, aspace, pc))
15081 return 1;
15082
15083 return 0;
15084 }
15085
15086 /* Check whether a software single-step breakpoint is inserted at
15087 PC. */
15088
15089 int
15090 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15091 CORE_ADDR pc)
15092 {
15093 struct breakpoint *bpt;
15094
15095 ALL_BREAKPOINTS (bpt)
15096 {
15097 if (bpt->type == bp_single_step
15098 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15099 return 1;
15100 }
15101 return 0;
15102 }
15103
15104 /* Tracepoint-specific operations. */
15105
15106 /* Set tracepoint count to NUM. */
15107 static void
15108 set_tracepoint_count (int num)
15109 {
15110 tracepoint_count = num;
15111 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15112 }
15113
15114 static void
15115 trace_command (char *arg, int from_tty)
15116 {
15117 struct breakpoint_ops *ops;
15118
15119 event_location_up location = string_to_event_location (&arg,
15120 current_language);
15121 if (location != NULL
15122 && event_location_type (location.get ()) == PROBE_LOCATION)
15123 ops = &tracepoint_probe_breakpoint_ops;
15124 else
15125 ops = &tracepoint_breakpoint_ops;
15126
15127 create_breakpoint (get_current_arch (),
15128 location.get (),
15129 NULL, 0, arg, 1 /* parse arg */,
15130 0 /* tempflag */,
15131 bp_tracepoint /* type_wanted */,
15132 0 /* Ignore count */,
15133 pending_break_support,
15134 ops,
15135 from_tty,
15136 1 /* enabled */,
15137 0 /* internal */, 0);
15138 }
15139
15140 static void
15141 ftrace_command (char *arg, int from_tty)
15142 {
15143 event_location_up location = string_to_event_location (&arg,
15144 current_language);
15145 create_breakpoint (get_current_arch (),
15146 location.get (),
15147 NULL, 0, arg, 1 /* parse arg */,
15148 0 /* tempflag */,
15149 bp_fast_tracepoint /* type_wanted */,
15150 0 /* Ignore count */,
15151 pending_break_support,
15152 &tracepoint_breakpoint_ops,
15153 from_tty,
15154 1 /* enabled */,
15155 0 /* internal */, 0);
15156 }
15157
15158 /* strace command implementation. Creates a static tracepoint. */
15159
15160 static void
15161 strace_command (char *arg, int from_tty)
15162 {
15163 struct breakpoint_ops *ops;
15164 event_location_up location;
15165 struct cleanup *back_to;
15166
15167 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15168 or with a normal static tracepoint. */
15169 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15170 {
15171 ops = &strace_marker_breakpoint_ops;
15172 location = new_linespec_location (&arg);
15173 }
15174 else
15175 {
15176 ops = &tracepoint_breakpoint_ops;
15177 location = string_to_event_location (&arg, current_language);
15178 }
15179
15180 create_breakpoint (get_current_arch (),
15181 location.get (),
15182 NULL, 0, arg, 1 /* parse arg */,
15183 0 /* tempflag */,
15184 bp_static_tracepoint /* type_wanted */,
15185 0 /* Ignore count */,
15186 pending_break_support,
15187 ops,
15188 from_tty,
15189 1 /* enabled */,
15190 0 /* internal */, 0);
15191 }
15192
15193 /* Set up a fake reader function that gets command lines from a linked
15194 list that was acquired during tracepoint uploading. */
15195
15196 static struct uploaded_tp *this_utp;
15197 static int next_cmd;
15198
15199 static char *
15200 read_uploaded_action (void)
15201 {
15202 char *rslt;
15203
15204 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15205
15206 next_cmd++;
15207
15208 return rslt;
15209 }
15210
15211 /* Given information about a tracepoint as recorded on a target (which
15212 can be either a live system or a trace file), attempt to create an
15213 equivalent GDB tracepoint. This is not a reliable process, since
15214 the target does not necessarily have all the information used when
15215 the tracepoint was originally defined. */
15216
15217 struct tracepoint *
15218 create_tracepoint_from_upload (struct uploaded_tp *utp)
15219 {
15220 char *addr_str, small_buf[100];
15221 struct tracepoint *tp;
15222
15223 if (utp->at_string)
15224 addr_str = utp->at_string;
15225 else
15226 {
15227 /* In the absence of a source location, fall back to raw
15228 address. Since there is no way to confirm that the address
15229 means the same thing as when the trace was started, warn the
15230 user. */
15231 warning (_("Uploaded tracepoint %d has no "
15232 "source location, using raw address"),
15233 utp->number);
15234 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15235 addr_str = small_buf;
15236 }
15237
15238 /* There's not much we can do with a sequence of bytecodes. */
15239 if (utp->cond && !utp->cond_string)
15240 warning (_("Uploaded tracepoint %d condition "
15241 "has no source form, ignoring it"),
15242 utp->number);
15243
15244 event_location_up location = string_to_event_location (&addr_str,
15245 current_language);
15246 if (!create_breakpoint (get_current_arch (),
15247 location.get (),
15248 utp->cond_string, -1, addr_str,
15249 0 /* parse cond/thread */,
15250 0 /* tempflag */,
15251 utp->type /* type_wanted */,
15252 0 /* Ignore count */,
15253 pending_break_support,
15254 &tracepoint_breakpoint_ops,
15255 0 /* from_tty */,
15256 utp->enabled /* enabled */,
15257 0 /* internal */,
15258 CREATE_BREAKPOINT_FLAGS_INSERTED))
15259 return NULL;
15260
15261 /* Get the tracepoint we just created. */
15262 tp = get_tracepoint (tracepoint_count);
15263 gdb_assert (tp != NULL);
15264
15265 if (utp->pass > 0)
15266 {
15267 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15268 tp->base.number);
15269
15270 trace_pass_command (small_buf, 0);
15271 }
15272
15273 /* If we have uploaded versions of the original commands, set up a
15274 special-purpose "reader" function and call the usual command line
15275 reader, then pass the result to the breakpoint command-setting
15276 function. */
15277 if (!VEC_empty (char_ptr, utp->cmd_strings))
15278 {
15279 command_line_up cmd_list;
15280
15281 this_utp = utp;
15282 next_cmd = 0;
15283
15284 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15285
15286 breakpoint_set_commands (&tp->base, std::move (cmd_list));
15287 }
15288 else if (!VEC_empty (char_ptr, utp->actions)
15289 || !VEC_empty (char_ptr, utp->step_actions))
15290 warning (_("Uploaded tracepoint %d actions "
15291 "have no source form, ignoring them"),
15292 utp->number);
15293
15294 /* Copy any status information that might be available. */
15295 tp->base.hit_count = utp->hit_count;
15296 tp->traceframe_usage = utp->traceframe_usage;
15297
15298 return tp;
15299 }
15300
15301 /* Print information on tracepoint number TPNUM_EXP, or all if
15302 omitted. */
15303
15304 static void
15305 tracepoints_info (char *args, int from_tty)
15306 {
15307 struct ui_out *uiout = current_uiout;
15308 int num_printed;
15309
15310 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15311
15312 if (num_printed == 0)
15313 {
15314 if (args == NULL || *args == '\0')
15315 uiout->message ("No tracepoints.\n");
15316 else
15317 uiout->message ("No tracepoint matching '%s'.\n", args);
15318 }
15319
15320 default_collect_info ();
15321 }
15322
15323 /* The 'enable trace' command enables tracepoints.
15324 Not supported by all targets. */
15325 static void
15326 enable_trace_command (char *args, int from_tty)
15327 {
15328 enable_command (args, from_tty);
15329 }
15330
15331 /* The 'disable trace' command disables tracepoints.
15332 Not supported by all targets. */
15333 static void
15334 disable_trace_command (char *args, int from_tty)
15335 {
15336 disable_command (args, from_tty);
15337 }
15338
15339 /* Remove a tracepoint (or all if no argument). */
15340 static void
15341 delete_trace_command (char *arg, int from_tty)
15342 {
15343 struct breakpoint *b, *b_tmp;
15344
15345 dont_repeat ();
15346
15347 if (arg == 0)
15348 {
15349 int breaks_to_delete = 0;
15350
15351 /* Delete all breakpoints if no argument.
15352 Do not delete internal or call-dummy breakpoints, these
15353 have to be deleted with an explicit breakpoint number
15354 argument. */
15355 ALL_TRACEPOINTS (b)
15356 if (is_tracepoint (b) && user_breakpoint_p (b))
15357 {
15358 breaks_to_delete = 1;
15359 break;
15360 }
15361
15362 /* Ask user only if there are some breakpoints to delete. */
15363 if (!from_tty
15364 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15365 {
15366 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15367 if (is_tracepoint (b) && user_breakpoint_p (b))
15368 delete_breakpoint (b);
15369 }
15370 }
15371 else
15372 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15373 }
15374
15375 /* Helper function for trace_pass_command. */
15376
15377 static void
15378 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15379 {
15380 tp->pass_count = count;
15381 observer_notify_breakpoint_modified (&tp->base);
15382 if (from_tty)
15383 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15384 tp->base.number, count);
15385 }
15386
15387 /* Set passcount for tracepoint.
15388
15389 First command argument is passcount, second is tracepoint number.
15390 If tracepoint number omitted, apply to most recently defined.
15391 Also accepts special argument "all". */
15392
15393 static void
15394 trace_pass_command (char *args, int from_tty)
15395 {
15396 struct tracepoint *t1;
15397 unsigned int count;
15398
15399 if (args == 0 || *args == 0)
15400 error (_("passcount command requires an "
15401 "argument (count + optional TP num)"));
15402
15403 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15404
15405 args = skip_spaces (args);
15406 if (*args && strncasecmp (args, "all", 3) == 0)
15407 {
15408 struct breakpoint *b;
15409
15410 args += 3; /* Skip special argument "all". */
15411 if (*args)
15412 error (_("Junk at end of arguments."));
15413
15414 ALL_TRACEPOINTS (b)
15415 {
15416 t1 = (struct tracepoint *) b;
15417 trace_pass_set_count (t1, count, from_tty);
15418 }
15419 }
15420 else if (*args == '\0')
15421 {
15422 t1 = get_tracepoint_by_number (&args, NULL);
15423 if (t1)
15424 trace_pass_set_count (t1, count, from_tty);
15425 }
15426 else
15427 {
15428 number_or_range_parser parser (args);
15429 while (!parser.finished ())
15430 {
15431 t1 = get_tracepoint_by_number (&args, &parser);
15432 if (t1)
15433 trace_pass_set_count (t1, count, from_tty);
15434 }
15435 }
15436 }
15437
15438 struct tracepoint *
15439 get_tracepoint (int num)
15440 {
15441 struct breakpoint *t;
15442
15443 ALL_TRACEPOINTS (t)
15444 if (t->number == num)
15445 return (struct tracepoint *) t;
15446
15447 return NULL;
15448 }
15449
15450 /* Find the tracepoint with the given target-side number (which may be
15451 different from the tracepoint number after disconnecting and
15452 reconnecting). */
15453
15454 struct tracepoint *
15455 get_tracepoint_by_number_on_target (int num)
15456 {
15457 struct breakpoint *b;
15458
15459 ALL_TRACEPOINTS (b)
15460 {
15461 struct tracepoint *t = (struct tracepoint *) b;
15462
15463 if (t->number_on_target == num)
15464 return t;
15465 }
15466
15467 return NULL;
15468 }
15469
15470 /* Utility: parse a tracepoint number and look it up in the list.
15471 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15472 If the argument is missing, the most recent tracepoint
15473 (tracepoint_count) is returned. */
15474
15475 struct tracepoint *
15476 get_tracepoint_by_number (char **arg,
15477 number_or_range_parser *parser)
15478 {
15479 struct breakpoint *t;
15480 int tpnum;
15481 char *instring = arg == NULL ? NULL : *arg;
15482
15483 if (parser != NULL)
15484 {
15485 gdb_assert (!parser->finished ());
15486 tpnum = parser->get_number ();
15487 }
15488 else if (arg == NULL || *arg == NULL || ! **arg)
15489 tpnum = tracepoint_count;
15490 else
15491 tpnum = get_number (arg);
15492
15493 if (tpnum <= 0)
15494 {
15495 if (instring && *instring)
15496 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15497 instring);
15498 else
15499 printf_filtered (_("No previous tracepoint\n"));
15500 return NULL;
15501 }
15502
15503 ALL_TRACEPOINTS (t)
15504 if (t->number == tpnum)
15505 {
15506 return (struct tracepoint *) t;
15507 }
15508
15509 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15510 return NULL;
15511 }
15512
15513 void
15514 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15515 {
15516 if (b->thread != -1)
15517 fprintf_unfiltered (fp, " thread %d", b->thread);
15518
15519 if (b->task != 0)
15520 fprintf_unfiltered (fp, " task %d", b->task);
15521
15522 fprintf_unfiltered (fp, "\n");
15523 }
15524
15525 /* Save information on user settable breakpoints (watchpoints, etc) to
15526 a new script file named FILENAME. If FILTER is non-NULL, call it
15527 on each breakpoint and only include the ones for which it returns
15528 non-zero. */
15529
15530 static void
15531 save_breakpoints (char *filename, int from_tty,
15532 int (*filter) (const struct breakpoint *))
15533 {
15534 struct breakpoint *tp;
15535 int any = 0;
15536 struct cleanup *cleanup;
15537 int extra_trace_bits = 0;
15538
15539 if (filename == 0 || *filename == 0)
15540 error (_("Argument required (file name in which to save)"));
15541
15542 /* See if we have anything to save. */
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 any = 1;
15554
15555 if (is_tracepoint (tp))
15556 {
15557 extra_trace_bits = 1;
15558
15559 /* We can stop searching. */
15560 break;
15561 }
15562 }
15563
15564 if (!any)
15565 {
15566 warning (_("Nothing to save."));
15567 return;
15568 }
15569
15570 filename = tilde_expand (filename);
15571 cleanup = make_cleanup (xfree, filename);
15572
15573 stdio_file fp;
15574
15575 if (!fp.open (filename, "w"))
15576 error (_("Unable to open file '%s' for saving (%s)"),
15577 filename, safe_strerror (errno));
15578
15579 if (extra_trace_bits)
15580 save_trace_state_variables (&fp);
15581
15582 ALL_BREAKPOINTS (tp)
15583 {
15584 /* Skip internal and momentary breakpoints. */
15585 if (!user_breakpoint_p (tp))
15586 continue;
15587
15588 /* If we have a filter, only save the breakpoints it accepts. */
15589 if (filter && !filter (tp))
15590 continue;
15591
15592 tp->ops->print_recreate (tp, &fp);
15593
15594 /* Note, we can't rely on tp->number for anything, as we can't
15595 assume the recreated breakpoint numbers will match. Use $bpnum
15596 instead. */
15597
15598 if (tp->cond_string)
15599 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15600
15601 if (tp->ignore_count)
15602 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15603
15604 if (tp->type != bp_dprintf && tp->commands)
15605 {
15606 fp.puts (" commands\n");
15607
15608 current_uiout->redirect (&fp);
15609 TRY
15610 {
15611 print_command_lines (current_uiout, tp->commands->commands, 2);
15612 }
15613 CATCH (ex, RETURN_MASK_ALL)
15614 {
15615 current_uiout->redirect (NULL);
15616 throw_exception (ex);
15617 }
15618 END_CATCH
15619
15620 current_uiout->redirect (NULL);
15621 fp.puts (" end\n");
15622 }
15623
15624 if (tp->enable_state == bp_disabled)
15625 fp.puts ("disable $bpnum\n");
15626
15627 /* If this is a multi-location breakpoint, check if the locations
15628 should be individually disabled. Watchpoint locations are
15629 special, and not user visible. */
15630 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15631 {
15632 struct bp_location *loc;
15633 int n = 1;
15634
15635 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15636 if (!loc->enabled)
15637 fp.printf ("disable $bpnum.%d\n", n);
15638 }
15639 }
15640
15641 if (extra_trace_bits && *default_collect)
15642 fp.printf ("set default-collect %s\n", default_collect);
15643
15644 if (from_tty)
15645 printf_filtered (_("Saved to file '%s'.\n"), filename);
15646 do_cleanups (cleanup);
15647 }
15648
15649 /* The `save breakpoints' command. */
15650
15651 static void
15652 save_breakpoints_command (char *args, int from_tty)
15653 {
15654 save_breakpoints (args, from_tty, NULL);
15655 }
15656
15657 /* The `save tracepoints' command. */
15658
15659 static void
15660 save_tracepoints_command (char *args, int from_tty)
15661 {
15662 save_breakpoints (args, from_tty, is_tracepoint);
15663 }
15664
15665 /* Create a vector of all tracepoints. */
15666
15667 VEC(breakpoint_p) *
15668 all_tracepoints (void)
15669 {
15670 VEC(breakpoint_p) *tp_vec = 0;
15671 struct breakpoint *tp;
15672
15673 ALL_TRACEPOINTS (tp)
15674 {
15675 VEC_safe_push (breakpoint_p, tp_vec, tp);
15676 }
15677
15678 return tp_vec;
15679 }
15680
15681 \f
15682 /* This help string is used to consolidate all the help string for specifying
15683 locations used by several commands. */
15684
15685 #define LOCATION_HELP_STRING \
15686 "Linespecs are colon-separated lists of location parameters, such as\n\
15687 source filename, function name, label name, and line number.\n\
15688 Example: To specify the start of a label named \"the_top\" in the\n\
15689 function \"fact\" in the file \"factorial.c\", use\n\
15690 \"factorial.c:fact:the_top\".\n\
15691 \n\
15692 Address locations begin with \"*\" and specify an exact address in the\n\
15693 program. Example: To specify the fourth byte past the start function\n\
15694 \"main\", use \"*main + 4\".\n\
15695 \n\
15696 Explicit locations are similar to linespecs but use an option/argument\n\
15697 syntax to specify location parameters.\n\
15698 Example: To specify the start of the label named \"the_top\" in the\n\
15699 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15700 -function fact -label the_top\".\n"
15701
15702 /* This help string is used for the break, hbreak, tbreak and thbreak
15703 commands. It is defined as a macro to prevent duplication.
15704 COMMAND should be a string constant containing the name of the
15705 command. */
15706
15707 #define BREAK_ARGS_HELP(command) \
15708 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15709 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15710 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15711 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15712 `-probe-dtrace' (for a DTrace probe).\n\
15713 LOCATION may be a linespec, address, or explicit location as described\n\
15714 below.\n\
15715 \n\
15716 With no LOCATION, uses current execution address of the selected\n\
15717 stack frame. This is useful for breaking on return to a stack frame.\n\
15718 \n\
15719 THREADNUM is the number from \"info threads\".\n\
15720 CONDITION is a boolean expression.\n\
15721 \n" LOCATION_HELP_STRING "\n\
15722 Multiple breakpoints at one place are permitted, and useful if their\n\
15723 conditions are different.\n\
15724 \n\
15725 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15726
15727 /* List of subcommands for "catch". */
15728 static struct cmd_list_element *catch_cmdlist;
15729
15730 /* List of subcommands for "tcatch". */
15731 static struct cmd_list_element *tcatch_cmdlist;
15732
15733 void
15734 add_catch_command (const char *name, const char *docstring,
15735 cmd_sfunc_ftype *sfunc,
15736 completer_ftype *completer,
15737 void *user_data_catch,
15738 void *user_data_tcatch)
15739 {
15740 struct cmd_list_element *command;
15741
15742 command = add_cmd (name, class_breakpoint, NULL, docstring,
15743 &catch_cmdlist);
15744 set_cmd_sfunc (command, sfunc);
15745 set_cmd_context (command, user_data_catch);
15746 set_cmd_completer (command, completer);
15747
15748 command = add_cmd (name, class_breakpoint, NULL, docstring,
15749 &tcatch_cmdlist);
15750 set_cmd_sfunc (command, sfunc);
15751 set_cmd_context (command, user_data_tcatch);
15752 set_cmd_completer (command, completer);
15753 }
15754
15755 static void
15756 save_command (char *arg, int from_tty)
15757 {
15758 printf_unfiltered (_("\"save\" must be followed by "
15759 "the name of a save subcommand.\n"));
15760 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15761 }
15762
15763 struct breakpoint *
15764 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15765 void *data)
15766 {
15767 struct breakpoint *b, *b_tmp;
15768
15769 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15770 {
15771 if ((*callback) (b, data))
15772 return b;
15773 }
15774
15775 return NULL;
15776 }
15777
15778 /* Zero if any of the breakpoint's locations could be a location where
15779 functions have been inlined, nonzero otherwise. */
15780
15781 static int
15782 is_non_inline_function (struct breakpoint *b)
15783 {
15784 /* The shared library event breakpoint is set on the address of a
15785 non-inline function. */
15786 if (b->type == bp_shlib_event)
15787 return 1;
15788
15789 return 0;
15790 }
15791
15792 /* Nonzero if the specified PC cannot be a location where functions
15793 have been inlined. */
15794
15795 int
15796 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15797 const struct target_waitstatus *ws)
15798 {
15799 struct breakpoint *b;
15800 struct bp_location *bl;
15801
15802 ALL_BREAKPOINTS (b)
15803 {
15804 if (!is_non_inline_function (b))
15805 continue;
15806
15807 for (bl = b->loc; bl != NULL; bl = bl->next)
15808 {
15809 if (!bl->shlib_disabled
15810 && bpstat_check_location (bl, aspace, pc, ws))
15811 return 1;
15812 }
15813 }
15814
15815 return 0;
15816 }
15817
15818 /* Remove any references to OBJFILE which is going to be freed. */
15819
15820 void
15821 breakpoint_free_objfile (struct objfile *objfile)
15822 {
15823 struct bp_location **locp, *loc;
15824
15825 ALL_BP_LOCATIONS (loc, locp)
15826 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15827 loc->symtab = NULL;
15828 }
15829
15830 void
15831 initialize_breakpoint_ops (void)
15832 {
15833 static int initialized = 0;
15834
15835 struct breakpoint_ops *ops;
15836
15837 if (initialized)
15838 return;
15839 initialized = 1;
15840
15841 /* The breakpoint_ops structure to be inherit by all kinds of
15842 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15843 internal and momentary breakpoints, etc.). */
15844 ops = &bkpt_base_breakpoint_ops;
15845 *ops = base_breakpoint_ops;
15846 ops->re_set = bkpt_re_set;
15847 ops->insert_location = bkpt_insert_location;
15848 ops->remove_location = bkpt_remove_location;
15849 ops->breakpoint_hit = bkpt_breakpoint_hit;
15850 ops->create_sals_from_location = bkpt_create_sals_from_location;
15851 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15852 ops->decode_location = bkpt_decode_location;
15853
15854 /* The breakpoint_ops structure to be used in regular breakpoints. */
15855 ops = &bkpt_breakpoint_ops;
15856 *ops = bkpt_base_breakpoint_ops;
15857 ops->re_set = bkpt_re_set;
15858 ops->resources_needed = bkpt_resources_needed;
15859 ops->print_it = bkpt_print_it;
15860 ops->print_mention = bkpt_print_mention;
15861 ops->print_recreate = bkpt_print_recreate;
15862
15863 /* Ranged breakpoints. */
15864 ops = &ranged_breakpoint_ops;
15865 *ops = bkpt_breakpoint_ops;
15866 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15867 ops->resources_needed = resources_needed_ranged_breakpoint;
15868 ops->print_it = print_it_ranged_breakpoint;
15869 ops->print_one = print_one_ranged_breakpoint;
15870 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15871 ops->print_mention = print_mention_ranged_breakpoint;
15872 ops->print_recreate = print_recreate_ranged_breakpoint;
15873
15874 /* Internal breakpoints. */
15875 ops = &internal_breakpoint_ops;
15876 *ops = bkpt_base_breakpoint_ops;
15877 ops->re_set = internal_bkpt_re_set;
15878 ops->check_status = internal_bkpt_check_status;
15879 ops->print_it = internal_bkpt_print_it;
15880 ops->print_mention = internal_bkpt_print_mention;
15881
15882 /* Momentary breakpoints. */
15883 ops = &momentary_breakpoint_ops;
15884 *ops = bkpt_base_breakpoint_ops;
15885 ops->re_set = momentary_bkpt_re_set;
15886 ops->check_status = momentary_bkpt_check_status;
15887 ops->print_it = momentary_bkpt_print_it;
15888 ops->print_mention = momentary_bkpt_print_mention;
15889
15890 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15891 ops = &longjmp_breakpoint_ops;
15892 *ops = momentary_breakpoint_ops;
15893 ops->dtor = longjmp_bkpt_dtor;
15894
15895 /* Probe breakpoints. */
15896 ops = &bkpt_probe_breakpoint_ops;
15897 *ops = bkpt_breakpoint_ops;
15898 ops->insert_location = bkpt_probe_insert_location;
15899 ops->remove_location = bkpt_probe_remove_location;
15900 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15901 ops->decode_location = bkpt_probe_decode_location;
15902
15903 /* Watchpoints. */
15904 ops = &watchpoint_breakpoint_ops;
15905 *ops = base_breakpoint_ops;
15906 ops->dtor = dtor_watchpoint;
15907 ops->re_set = re_set_watchpoint;
15908 ops->insert_location = insert_watchpoint;
15909 ops->remove_location = remove_watchpoint;
15910 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15911 ops->check_status = check_status_watchpoint;
15912 ops->resources_needed = resources_needed_watchpoint;
15913 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15914 ops->print_it = print_it_watchpoint;
15915 ops->print_mention = print_mention_watchpoint;
15916 ops->print_recreate = print_recreate_watchpoint;
15917 ops->explains_signal = explains_signal_watchpoint;
15918
15919 /* Masked watchpoints. */
15920 ops = &masked_watchpoint_breakpoint_ops;
15921 *ops = watchpoint_breakpoint_ops;
15922 ops->insert_location = insert_masked_watchpoint;
15923 ops->remove_location = remove_masked_watchpoint;
15924 ops->resources_needed = resources_needed_masked_watchpoint;
15925 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15926 ops->print_it = print_it_masked_watchpoint;
15927 ops->print_one_detail = print_one_detail_masked_watchpoint;
15928 ops->print_mention = print_mention_masked_watchpoint;
15929 ops->print_recreate = print_recreate_masked_watchpoint;
15930
15931 /* Tracepoints. */
15932 ops = &tracepoint_breakpoint_ops;
15933 *ops = base_breakpoint_ops;
15934 ops->re_set = tracepoint_re_set;
15935 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15936 ops->print_one_detail = tracepoint_print_one_detail;
15937 ops->print_mention = tracepoint_print_mention;
15938 ops->print_recreate = tracepoint_print_recreate;
15939 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15940 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15941 ops->decode_location = tracepoint_decode_location;
15942
15943 /* Probe tracepoints. */
15944 ops = &tracepoint_probe_breakpoint_ops;
15945 *ops = tracepoint_breakpoint_ops;
15946 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15947 ops->decode_location = tracepoint_probe_decode_location;
15948
15949 /* Static tracepoints with marker (`-m'). */
15950 ops = &strace_marker_breakpoint_ops;
15951 *ops = tracepoint_breakpoint_ops;
15952 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15953 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15954 ops->decode_location = strace_marker_decode_location;
15955
15956 /* Fork catchpoints. */
15957 ops = &catch_fork_breakpoint_ops;
15958 *ops = base_breakpoint_ops;
15959 ops->insert_location = insert_catch_fork;
15960 ops->remove_location = remove_catch_fork;
15961 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15962 ops->print_it = print_it_catch_fork;
15963 ops->print_one = print_one_catch_fork;
15964 ops->print_mention = print_mention_catch_fork;
15965 ops->print_recreate = print_recreate_catch_fork;
15966
15967 /* Vfork catchpoints. */
15968 ops = &catch_vfork_breakpoint_ops;
15969 *ops = base_breakpoint_ops;
15970 ops->insert_location = insert_catch_vfork;
15971 ops->remove_location = remove_catch_vfork;
15972 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15973 ops->print_it = print_it_catch_vfork;
15974 ops->print_one = print_one_catch_vfork;
15975 ops->print_mention = print_mention_catch_vfork;
15976 ops->print_recreate = print_recreate_catch_vfork;
15977
15978 /* Exec catchpoints. */
15979 ops = &catch_exec_breakpoint_ops;
15980 *ops = base_breakpoint_ops;
15981 ops->dtor = dtor_catch_exec;
15982 ops->insert_location = insert_catch_exec;
15983 ops->remove_location = remove_catch_exec;
15984 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15985 ops->print_it = print_it_catch_exec;
15986 ops->print_one = print_one_catch_exec;
15987 ops->print_mention = print_mention_catch_exec;
15988 ops->print_recreate = print_recreate_catch_exec;
15989
15990 /* Solib-related catchpoints. */
15991 ops = &catch_solib_breakpoint_ops;
15992 *ops = base_breakpoint_ops;
15993 ops->dtor = dtor_catch_solib;
15994 ops->insert_location = insert_catch_solib;
15995 ops->remove_location = remove_catch_solib;
15996 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15997 ops->check_status = check_status_catch_solib;
15998 ops->print_it = print_it_catch_solib;
15999 ops->print_one = print_one_catch_solib;
16000 ops->print_mention = print_mention_catch_solib;
16001 ops->print_recreate = print_recreate_catch_solib;
16002
16003 ops = &dprintf_breakpoint_ops;
16004 *ops = bkpt_base_breakpoint_ops;
16005 ops->re_set = dprintf_re_set;
16006 ops->resources_needed = bkpt_resources_needed;
16007 ops->print_it = bkpt_print_it;
16008 ops->print_mention = bkpt_print_mention;
16009 ops->print_recreate = dprintf_print_recreate;
16010 ops->after_condition_true = dprintf_after_condition_true;
16011 ops->breakpoint_hit = dprintf_breakpoint_hit;
16012 }
16013
16014 /* Chain containing all defined "enable breakpoint" subcommands. */
16015
16016 static struct cmd_list_element *enablebreaklist = NULL;
16017
16018 void
16019 _initialize_breakpoint (void)
16020 {
16021 struct cmd_list_element *c;
16022
16023 initialize_breakpoint_ops ();
16024
16025 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16026 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16027 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16028
16029 breakpoint_objfile_key
16030 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16031
16032 breakpoint_chain = 0;
16033 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16034 before a breakpoint is set. */
16035 breakpoint_count = 0;
16036
16037 tracepoint_count = 0;
16038
16039 add_com ("ignore", class_breakpoint, ignore_command, _("\
16040 Set ignore-count of breakpoint number N to COUNT.\n\
16041 Usage is `ignore N COUNT'."));
16042
16043 add_com ("commands", class_breakpoint, commands_command, _("\
16044 Set commands to be executed when the given breakpoints are hit.\n\
16045 Give a space-separated breakpoint list as argument after \"commands\".\n\
16046 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16047 (e.g. `5-7').\n\
16048 With no argument, the targeted breakpoint is the last one set.\n\
16049 The commands themselves follow starting on the next line.\n\
16050 Type a line containing \"end\" to indicate the end of them.\n\
16051 Give \"silent\" as the first line to make the breakpoint silent;\n\
16052 then no output is printed when it is hit, except what the commands print."));
16053
16054 c = add_com ("condition", class_breakpoint, condition_command, _("\
16055 Specify breakpoint number N to break only if COND is true.\n\
16056 Usage is `condition N COND', where N is an integer and COND is an\n\
16057 expression to be evaluated whenever breakpoint N is reached."));
16058 set_cmd_completer (c, condition_completer);
16059
16060 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16061 Set a temporary breakpoint.\n\
16062 Like \"break\" except the breakpoint is only temporary,\n\
16063 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16064 by using \"enable delete\" on the breakpoint number.\n\
16065 \n"
16066 BREAK_ARGS_HELP ("tbreak")));
16067 set_cmd_completer (c, location_completer);
16068
16069 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16070 Set a hardware assisted breakpoint.\n\
16071 Like \"break\" except the breakpoint requires hardware support,\n\
16072 some target hardware may not have this support.\n\
16073 \n"
16074 BREAK_ARGS_HELP ("hbreak")));
16075 set_cmd_completer (c, location_completer);
16076
16077 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16078 Set a temporary hardware assisted breakpoint.\n\
16079 Like \"hbreak\" except the breakpoint is only temporary,\n\
16080 so it will be deleted when hit.\n\
16081 \n"
16082 BREAK_ARGS_HELP ("thbreak")));
16083 set_cmd_completer (c, location_completer);
16084
16085 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16086 Enable some breakpoints.\n\
16087 Give breakpoint numbers (separated by spaces) as arguments.\n\
16088 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16089 This is used to cancel the effect of the \"disable\" command.\n\
16090 With a subcommand you can enable temporarily."),
16091 &enablelist, "enable ", 1, &cmdlist);
16092
16093 add_com_alias ("en", "enable", class_breakpoint, 1);
16094
16095 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16096 Enable some breakpoints.\n\
16097 Give breakpoint numbers (separated by spaces) as arguments.\n\
16098 This is used to cancel the effect of the \"disable\" command.\n\
16099 May be abbreviated to simply \"enable\".\n"),
16100 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16101
16102 add_cmd ("once", no_class, enable_once_command, _("\
16103 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16104 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16105 &enablebreaklist);
16106
16107 add_cmd ("delete", no_class, enable_delete_command, _("\
16108 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16109 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16110 &enablebreaklist);
16111
16112 add_cmd ("count", no_class, enable_count_command, _("\
16113 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16114 If a breakpoint is hit while enabled in this fashion,\n\
16115 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16116 &enablebreaklist);
16117
16118 add_cmd ("delete", no_class, enable_delete_command, _("\
16119 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16120 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16121 &enablelist);
16122
16123 add_cmd ("once", no_class, enable_once_command, _("\
16124 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16125 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16126 &enablelist);
16127
16128 add_cmd ("count", no_class, enable_count_command, _("\
16129 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16130 If a breakpoint is hit while enabled in this fashion,\n\
16131 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16132 &enablelist);
16133
16134 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16135 Disable some breakpoints.\n\
16136 Arguments are breakpoint numbers with spaces in between.\n\
16137 To disable all breakpoints, give no argument.\n\
16138 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16139 &disablelist, "disable ", 1, &cmdlist);
16140 add_com_alias ("dis", "disable", class_breakpoint, 1);
16141 add_com_alias ("disa", "disable", class_breakpoint, 1);
16142
16143 add_cmd ("breakpoints", class_alias, disable_command, _("\
16144 Disable some breakpoints.\n\
16145 Arguments are breakpoint numbers with spaces in between.\n\
16146 To disable all breakpoints, give no argument.\n\
16147 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16148 This command may be abbreviated \"disable\"."),
16149 &disablelist);
16150
16151 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16152 Delete some breakpoints or auto-display expressions.\n\
16153 Arguments are breakpoint numbers with spaces in between.\n\
16154 To delete all breakpoints, give no argument.\n\
16155 \n\
16156 Also a prefix command for deletion of other GDB objects.\n\
16157 The \"unset\" command is also an alias for \"delete\"."),
16158 &deletelist, "delete ", 1, &cmdlist);
16159 add_com_alias ("d", "delete", class_breakpoint, 1);
16160 add_com_alias ("del", "delete", class_breakpoint, 1);
16161
16162 add_cmd ("breakpoints", class_alias, delete_command, _("\
16163 Delete some breakpoints or auto-display expressions.\n\
16164 Arguments are breakpoint numbers with spaces in between.\n\
16165 To delete all breakpoints, give no argument.\n\
16166 This command may be abbreviated \"delete\"."),
16167 &deletelist);
16168
16169 add_com ("clear", class_breakpoint, clear_command, _("\
16170 Clear breakpoint at specified location.\n\
16171 Argument may be a linespec, explicit, or address location as described below.\n\
16172 \n\
16173 With no argument, clears all breakpoints in the line that the selected frame\n\
16174 is executing in.\n"
16175 "\n" LOCATION_HELP_STRING "\n\
16176 See also the \"delete\" command which clears breakpoints by number."));
16177 add_com_alias ("cl", "clear", class_breakpoint, 1);
16178
16179 c = add_com ("break", class_breakpoint, break_command, _("\
16180 Set breakpoint at specified location.\n"
16181 BREAK_ARGS_HELP ("break")));
16182 set_cmd_completer (c, location_completer);
16183
16184 add_com_alias ("b", "break", class_run, 1);
16185 add_com_alias ("br", "break", class_run, 1);
16186 add_com_alias ("bre", "break", class_run, 1);
16187 add_com_alias ("brea", "break", class_run, 1);
16188
16189 if (dbx_commands)
16190 {
16191 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16192 Break in function/address or break at a line in the current file."),
16193 &stoplist, "stop ", 1, &cmdlist);
16194 add_cmd ("in", class_breakpoint, stopin_command,
16195 _("Break in function or address."), &stoplist);
16196 add_cmd ("at", class_breakpoint, stopat_command,
16197 _("Break at a line in the current file."), &stoplist);
16198 add_com ("status", class_info, breakpoints_info, _("\
16199 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16200 The \"Type\" column indicates one of:\n\
16201 \tbreakpoint - normal breakpoint\n\
16202 \twatchpoint - watchpoint\n\
16203 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16204 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16205 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16206 address and file/line number respectively.\n\
16207 \n\
16208 Convenience variable \"$_\" and default examine address for \"x\"\n\
16209 are set to the address of the last breakpoint listed unless the command\n\
16210 is prefixed with \"server \".\n\n\
16211 Convenience variable \"$bpnum\" contains the number of the last\n\
16212 breakpoint set."));
16213 }
16214
16215 add_info ("breakpoints", breakpoints_info, _("\
16216 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16217 The \"Type\" column indicates one of:\n\
16218 \tbreakpoint - normal breakpoint\n\
16219 \twatchpoint - watchpoint\n\
16220 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16221 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16222 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16223 address and file/line number respectively.\n\
16224 \n\
16225 Convenience variable \"$_\" and default examine address for \"x\"\n\
16226 are set to the address of the last breakpoint listed unless the command\n\
16227 is prefixed with \"server \".\n\n\
16228 Convenience variable \"$bpnum\" contains the number of the last\n\
16229 breakpoint set."));
16230
16231 add_info_alias ("b", "breakpoints", 1);
16232
16233 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16234 Status of all breakpoints, or breakpoint number NUMBER.\n\
16235 The \"Type\" column indicates one of:\n\
16236 \tbreakpoint - normal breakpoint\n\
16237 \twatchpoint - watchpoint\n\
16238 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16239 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16240 \tuntil - internal breakpoint used by the \"until\" command\n\
16241 \tfinish - internal breakpoint used by the \"finish\" command\n\
16242 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16243 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16244 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16245 address and file/line number respectively.\n\
16246 \n\
16247 Convenience variable \"$_\" and default examine address for \"x\"\n\
16248 are set to the address of the last breakpoint listed unless the command\n\
16249 is prefixed with \"server \".\n\n\
16250 Convenience variable \"$bpnum\" contains the number of the last\n\
16251 breakpoint set."),
16252 &maintenanceinfolist);
16253
16254 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16255 Set catchpoints to catch events."),
16256 &catch_cmdlist, "catch ",
16257 0/*allow-unknown*/, &cmdlist);
16258
16259 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16260 Set temporary catchpoints to catch events."),
16261 &tcatch_cmdlist, "tcatch ",
16262 0/*allow-unknown*/, &cmdlist);
16263
16264 add_catch_command ("fork", _("Catch calls to fork."),
16265 catch_fork_command_1,
16266 NULL,
16267 (void *) (uintptr_t) catch_fork_permanent,
16268 (void *) (uintptr_t) catch_fork_temporary);
16269 add_catch_command ("vfork", _("Catch calls to vfork."),
16270 catch_fork_command_1,
16271 NULL,
16272 (void *) (uintptr_t) catch_vfork_permanent,
16273 (void *) (uintptr_t) catch_vfork_temporary);
16274 add_catch_command ("exec", _("Catch calls to exec."),
16275 catch_exec_command_1,
16276 NULL,
16277 CATCH_PERMANENT,
16278 CATCH_TEMPORARY);
16279 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16280 Usage: catch load [REGEX]\n\
16281 If REGEX is given, only stop for libraries matching the regular expression."),
16282 catch_load_command_1,
16283 NULL,
16284 CATCH_PERMANENT,
16285 CATCH_TEMPORARY);
16286 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16287 Usage: catch unload [REGEX]\n\
16288 If REGEX is given, only stop for libraries matching the regular expression."),
16289 catch_unload_command_1,
16290 NULL,
16291 CATCH_PERMANENT,
16292 CATCH_TEMPORARY);
16293
16294 c = add_com ("watch", class_breakpoint, watch_command, _("\
16295 Set a watchpoint for an expression.\n\
16296 Usage: watch [-l|-location] EXPRESSION\n\
16297 A watchpoint stops execution of your program whenever the value of\n\
16298 an expression changes.\n\
16299 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16300 the memory to which it refers."));
16301 set_cmd_completer (c, expression_completer);
16302
16303 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16304 Set a read watchpoint for an expression.\n\
16305 Usage: rwatch [-l|-location] EXPRESSION\n\
16306 A watchpoint stops execution of your program whenever the value of\n\
16307 an expression is read.\n\
16308 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16309 the memory to which it refers."));
16310 set_cmd_completer (c, expression_completer);
16311
16312 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16313 Set a watchpoint for an expression.\n\
16314 Usage: awatch [-l|-location] EXPRESSION\n\
16315 A watchpoint stops execution of your program whenever the value of\n\
16316 an expression is either read or written.\n\
16317 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16318 the memory to which it refers."));
16319 set_cmd_completer (c, expression_completer);
16320
16321 add_info ("watchpoints", watchpoints_info, _("\
16322 Status of specified watchpoints (all watchpoints if no argument)."));
16323
16324 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16325 respond to changes - contrary to the description. */
16326 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16327 &can_use_hw_watchpoints, _("\
16328 Set debugger's willingness to use watchpoint hardware."), _("\
16329 Show debugger's willingness to use watchpoint hardware."), _("\
16330 If zero, gdb will not use hardware for new watchpoints, even if\n\
16331 such is available. (However, any hardware watchpoints that were\n\
16332 created before setting this to nonzero, will continue to use watchpoint\n\
16333 hardware.)"),
16334 NULL,
16335 show_can_use_hw_watchpoints,
16336 &setlist, &showlist);
16337
16338 can_use_hw_watchpoints = 1;
16339
16340 /* Tracepoint manipulation commands. */
16341
16342 c = add_com ("trace", class_breakpoint, trace_command, _("\
16343 Set a tracepoint at specified location.\n\
16344 \n"
16345 BREAK_ARGS_HELP ("trace") "\n\
16346 Do \"help tracepoints\" for info on other tracepoint commands."));
16347 set_cmd_completer (c, location_completer);
16348
16349 add_com_alias ("tp", "trace", class_alias, 0);
16350 add_com_alias ("tr", "trace", class_alias, 1);
16351 add_com_alias ("tra", "trace", class_alias, 1);
16352 add_com_alias ("trac", "trace", class_alias, 1);
16353
16354 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16355 Set a fast tracepoint at specified location.\n\
16356 \n"
16357 BREAK_ARGS_HELP ("ftrace") "\n\
16358 Do \"help tracepoints\" for info on other tracepoint commands."));
16359 set_cmd_completer (c, location_completer);
16360
16361 c = add_com ("strace", class_breakpoint, strace_command, _("\
16362 Set a static tracepoint at location or marker.\n\
16363 \n\
16364 strace [LOCATION] [if CONDITION]\n\
16365 LOCATION may be a linespec, explicit, or address location (described below) \n\
16366 or -m MARKER_ID.\n\n\
16367 If a marker id is specified, probe the marker with that name. With\n\
16368 no LOCATION, uses current execution address of the selected stack frame.\n\
16369 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16370 This collects arbitrary user data passed in the probe point call to the\n\
16371 tracing library. You can inspect it when analyzing the trace buffer,\n\
16372 by printing the $_sdata variable like any other convenience variable.\n\
16373 \n\
16374 CONDITION is a boolean expression.\n\
16375 \n" LOCATION_HELP_STRING "\n\
16376 Multiple tracepoints at one place are permitted, and useful if their\n\
16377 conditions are different.\n\
16378 \n\
16379 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16380 Do \"help tracepoints\" for info on other tracepoint commands."));
16381 set_cmd_completer (c, location_completer);
16382
16383 add_info ("tracepoints", tracepoints_info, _("\
16384 Status of specified tracepoints (all tracepoints if no argument).\n\
16385 Convenience variable \"$tpnum\" contains the number of the\n\
16386 last tracepoint set."));
16387
16388 add_info_alias ("tp", "tracepoints", 1);
16389
16390 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16391 Delete specified tracepoints.\n\
16392 Arguments are tracepoint numbers, separated by spaces.\n\
16393 No argument means delete all tracepoints."),
16394 &deletelist);
16395 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16396
16397 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16398 Disable specified tracepoints.\n\
16399 Arguments are tracepoint numbers, separated by spaces.\n\
16400 No argument means disable all tracepoints."),
16401 &disablelist);
16402 deprecate_cmd (c, "disable");
16403
16404 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16405 Enable specified tracepoints.\n\
16406 Arguments are tracepoint numbers, separated by spaces.\n\
16407 No argument means enable all tracepoints."),
16408 &enablelist);
16409 deprecate_cmd (c, "enable");
16410
16411 add_com ("passcount", class_trace, trace_pass_command, _("\
16412 Set the passcount for a tracepoint.\n\
16413 The trace will end when the tracepoint has been passed 'count' times.\n\
16414 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16415 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16416
16417 add_prefix_cmd ("save", class_breakpoint, save_command,
16418 _("Save breakpoint definitions as a script."),
16419 &save_cmdlist, "save ",
16420 0/*allow-unknown*/, &cmdlist);
16421
16422 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16423 Save current breakpoint definitions as a script.\n\
16424 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16425 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16426 session to restore them."),
16427 &save_cmdlist);
16428 set_cmd_completer (c, filename_completer);
16429
16430 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16431 Save current tracepoint definitions as a script.\n\
16432 Use the 'source' command in another debug session to restore them."),
16433 &save_cmdlist);
16434 set_cmd_completer (c, filename_completer);
16435
16436 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16437 deprecate_cmd (c, "save tracepoints");
16438
16439 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16440 Breakpoint specific settings\n\
16441 Configure various breakpoint-specific variables such as\n\
16442 pending breakpoint behavior"),
16443 &breakpoint_set_cmdlist, "set breakpoint ",
16444 0/*allow-unknown*/, &setlist);
16445 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16446 Breakpoint specific settings\n\
16447 Configure various breakpoint-specific variables such as\n\
16448 pending breakpoint behavior"),
16449 &breakpoint_show_cmdlist, "show breakpoint ",
16450 0/*allow-unknown*/, &showlist);
16451
16452 add_setshow_auto_boolean_cmd ("pending", no_class,
16453 &pending_break_support, _("\
16454 Set debugger's behavior regarding pending breakpoints."), _("\
16455 Show debugger's behavior regarding pending breakpoints."), _("\
16456 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16457 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16458 an error. If auto, an unrecognized breakpoint location results in a\n\
16459 user-query to see if a pending breakpoint should be created."),
16460 NULL,
16461 show_pending_break_support,
16462 &breakpoint_set_cmdlist,
16463 &breakpoint_show_cmdlist);
16464
16465 pending_break_support = AUTO_BOOLEAN_AUTO;
16466
16467 add_setshow_boolean_cmd ("auto-hw", no_class,
16468 &automatic_hardware_breakpoints, _("\
16469 Set automatic usage of hardware breakpoints."), _("\
16470 Show automatic usage of hardware breakpoints."), _("\
16471 If set, the debugger will automatically use hardware breakpoints for\n\
16472 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16473 a warning will be emitted for such breakpoints."),
16474 NULL,
16475 show_automatic_hardware_breakpoints,
16476 &breakpoint_set_cmdlist,
16477 &breakpoint_show_cmdlist);
16478
16479 add_setshow_boolean_cmd ("always-inserted", class_support,
16480 &always_inserted_mode, _("\
16481 Set mode for inserting breakpoints."), _("\
16482 Show mode for inserting breakpoints."), _("\
16483 When this mode is on, breakpoints are inserted immediately as soon as\n\
16484 they're created, kept inserted even when execution stops, and removed\n\
16485 only when the user deletes them. When this mode is off (the default),\n\
16486 breakpoints are inserted only when execution continues, and removed\n\
16487 when execution stops."),
16488 NULL,
16489 &show_always_inserted_mode,
16490 &breakpoint_set_cmdlist,
16491 &breakpoint_show_cmdlist);
16492
16493 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16494 condition_evaluation_enums,
16495 &condition_evaluation_mode_1, _("\
16496 Set mode of breakpoint condition evaluation."), _("\
16497 Show mode of breakpoint condition evaluation."), _("\
16498 When this is set to \"host\", breakpoint conditions will be\n\
16499 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16500 breakpoint conditions will be downloaded to the target (if the target\n\
16501 supports such feature) and conditions will be evaluated on the target's side.\n\
16502 If this is set to \"auto\" (default), this will be automatically set to\n\
16503 \"target\" if it supports condition evaluation, otherwise it will\n\
16504 be set to \"gdb\""),
16505 &set_condition_evaluation_mode,
16506 &show_condition_evaluation_mode,
16507 &breakpoint_set_cmdlist,
16508 &breakpoint_show_cmdlist);
16509
16510 add_com ("break-range", class_breakpoint, break_range_command, _("\
16511 Set a breakpoint for an address range.\n\
16512 break-range START-LOCATION, END-LOCATION\n\
16513 where START-LOCATION and END-LOCATION can be one of the following:\n\
16514 LINENUM, for that line in the current file,\n\
16515 FILE:LINENUM, for that line in that file,\n\
16516 +OFFSET, for that number of lines after the current line\n\
16517 or the start of the range\n\
16518 FUNCTION, for the first line in that function,\n\
16519 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16520 *ADDRESS, for the instruction at that address.\n\
16521 \n\
16522 The breakpoint will stop execution of the inferior whenever it executes\n\
16523 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16524 range (including START-LOCATION and END-LOCATION)."));
16525
16526 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16527 Set a dynamic printf at specified location.\n\
16528 dprintf location,format string,arg1,arg2,...\n\
16529 location may be a linespec, explicit, or address location.\n"
16530 "\n" LOCATION_HELP_STRING));
16531 set_cmd_completer (c, location_completer);
16532
16533 add_setshow_enum_cmd ("dprintf-style", class_support,
16534 dprintf_style_enums, &dprintf_style, _("\
16535 Set the style of usage for dynamic printf."), _("\
16536 Show the style of usage for dynamic printf."), _("\
16537 This setting chooses how GDB will do a dynamic printf.\n\
16538 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16539 console, as with the \"printf\" command.\n\
16540 If the value is \"call\", the print is done by calling a function in your\n\
16541 program; by default printf(), but you can choose a different function or\n\
16542 output stream by setting dprintf-function and dprintf-channel."),
16543 update_dprintf_commands, NULL,
16544 &setlist, &showlist);
16545
16546 dprintf_function = xstrdup ("printf");
16547 add_setshow_string_cmd ("dprintf-function", class_support,
16548 &dprintf_function, _("\
16549 Set the function to use for dynamic printf"), _("\
16550 Show the function to use for dynamic printf"), NULL,
16551 update_dprintf_commands, NULL,
16552 &setlist, &showlist);
16553
16554 dprintf_channel = xstrdup ("");
16555 add_setshow_string_cmd ("dprintf-channel", class_support,
16556 &dprintf_channel, _("\
16557 Set the channel to use for dynamic printf"), _("\
16558 Show the channel to use for dynamic printf"), NULL,
16559 update_dprintf_commands, NULL,
16560 &setlist, &showlist);
16561
16562 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16563 &disconnected_dprintf, _("\
16564 Set whether dprintf continues after GDB disconnects."), _("\
16565 Show whether dprintf continues after GDB disconnects."), _("\
16566 Use this to let dprintf commands continue to hit and produce output\n\
16567 even if GDB disconnects or detaches from the target."),
16568 NULL,
16569 NULL,
16570 &setlist, &showlist);
16571
16572 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16573 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16574 (target agent only) This is useful for formatted output in user-defined commands."));
16575
16576 automatic_hardware_breakpoints = 1;
16577
16578 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16579 observer_attach_thread_exit (remove_threaded_breakpoints);
16580 }
This page took 0.461027 seconds and 5 git commands to generate.